Skip to content

Commit cce08c1

Browse files
committed
[SandboxIR][NFC] GenericSetter tracker class
This patch introduces the `GenericSetter` tracker class that can be used to track and revert simple instruction setters. This patch also replaces several setter tracker classes with the generic one.
1 parent 36f0d64 commit cce08c1

File tree

3 files changed

+65
-172
lines changed

3 files changed

+65
-172
lines changed

llvm/include/llvm/SandboxIR/Tracker.h

Lines changed: 31 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -239,71 +239,45 @@ class RemoveFromParent : public IRChangeBase {
239239
#endif // NDEBUG
240240
};
241241

242-
class CallBrInstSetDefaultDest : public IRChangeBase {
243-
CallBrInst *CallBr;
244-
BasicBlock *OrigDefaultDest;
245-
246-
public:
247-
CallBrInstSetDefaultDest(CallBrInst *CallBr, Tracker &Tracker);
248-
void revert() final;
249-
void accept() final {}
250-
#ifndef NDEBUG
251-
void dump(raw_ostream &OS) const final {
252-
dumpCommon(OS);
253-
OS << "CallBrInstSetDefaultDest";
254-
}
255-
LLVM_DUMP_METHOD void dump() const final;
256-
#endif
257-
};
258-
259-
class AllocaSetAllocatedType final : public IRChangeBase {
260-
AllocaInst *Alloca;
261-
Type *OrigType;
262-
263-
public:
264-
AllocaSetAllocatedType(AllocaInst *Alloca, Tracker &Tracker);
265-
void revert() final;
266-
void accept() final {}
267-
#ifndef NDEBUG
268-
void dump(raw_ostream &OS) const final {
269-
dumpCommon(OS);
270-
OS << "AllocaSetAllocatedType";
271-
}
272-
LLVM_DUMP_METHOD void dump() const final;
273-
#endif
274-
};
275-
276-
class AllocaSetAlignment final : public IRChangeBase {
277-
AllocaInst *Alloca;
278-
Align OrigAlign;
242+
/// This class can be used for tracking most instruction setters.
243+
/// The two template arguments are:
244+
/// - GetterFn: The getter member function pointer (e.g., `&Foo::get`)
245+
/// - SetterFn: The setter member function pointer (e.g., `&Foo::set`)
246+
/// Upon construction, it saves a copy of the original value by calling the
247+
/// getter function. Revert sets the value back to the one saved, using the
248+
/// setter function provided.
249+
///
250+
/// Example:
251+
/// Tracker.track(std::make_unique<
252+
/// GenericSetter<&FooInst::get, &FooInst::set>>(I, Tracker));
253+
///
254+
template <auto GetterFn, auto SetterFn>
255+
class GenericSetter final : public IRChangeBase {
256+
/// Helper for getting the class type from the getter
257+
template <typename ClassT, typename RetT>
258+
static ClassT getClassTypeFromGetter(RetT (ClassT::*Fn)() const);
259+
template <typename ClassT, typename RetT>
260+
static ClassT getClassTypeFromGetter(RetT (ClassT::*Fn)());
261+
262+
using InstrT = decltype(getClassTypeFromGetter(GetterFn));
263+
using SavedValT = std::invoke_result_t<decltype(GetterFn), InstrT>;
264+
InstrT *I;
265+
SavedValT OrigVal;
279266

280267
public:
281-
AllocaSetAlignment(AllocaInst *Alloca, Tracker &Tracker);
282-
void revert() final;
268+
GenericSetter(InstrT *I, Tracker &Tracker)
269+
: IRChangeBase(Tracker), I(I), OrigVal((I->*GetterFn)()) {}
270+
void revert() final { (I->*SetterFn)(OrigVal); }
283271
void accept() final {}
284272
#ifndef NDEBUG
285273
void dump(raw_ostream &OS) const final {
286274
dumpCommon(OS);
287-
OS << "AllocaSetAlignment";
275+
OS << "GenericSetter";
288276
}
289-
LLVM_DUMP_METHOD void dump() const final;
290-
#endif
291-
};
292-
293-
class AllocaSetUsedWithInAlloca final : public IRChangeBase {
294-
AllocaInst *Alloca;
295-
bool Orig;
296-
297-
public:
298-
AllocaSetUsedWithInAlloca(AllocaInst *Alloca, Tracker &Tracker);
299-
void revert() final;
300-
void accept() final {}
301-
#ifndef NDEBUG
302-
void dump(raw_ostream &OS) const final {
303-
dumpCommon(OS);
304-
OS << "AllocaSetUsedWithInAlloca";
277+
LLVM_DUMP_METHOD void dump() const final {
278+
dump(dbgs());
279+
dbgs() << "\n";
305280
}
306-
LLVM_DUMP_METHOD void dump() const final;
307281
#endif
308282
};
309283

@@ -325,25 +299,6 @@ class CallBrInstSetIndirectDest : public IRChangeBase {
325299
#endif
326300
};
327301

328-
class SetVolatile : public IRChangeBase {
329-
/// This holds the properties of whether LoadInst or StoreInst was volatile
330-
bool WasVolatile;
331-
/// This could either be StoreInst or LoadInst
332-
PointerUnion<StoreInst *, LoadInst *> StoreOrLoad;
333-
334-
public:
335-
SetVolatile(Instruction *I, Tracker &Tracker);
336-
void revert() final;
337-
void accept() final {}
338-
#ifndef NDEBUG
339-
void dump(raw_ostream &OS) const final {
340-
dumpCommon(OS);
341-
OS << "SetVolatile";
342-
}
343-
LLVM_DUMP_METHOD void dump() const final;
344-
#endif
345-
};
346-
347302
class MoveInstr : public IRChangeBase {
348303
/// The instruction that moved.
349304
Instruction *MovedI;

llvm/lib/SandboxIR/SandboxIR.cpp

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -624,8 +624,11 @@ void BranchInst::dump() const {
624624

625625
void LoadInst::setVolatile(bool V) {
626626
auto &Tracker = Ctx.getTracker();
627-
if (Tracker.isTracking())
628-
Tracker.track(std::make_unique<SetVolatile>(this, Tracker));
627+
if (Tracker.isTracking()) {
628+
Tracker.track(std::make_unique<
629+
GenericSetter<&LoadInst::isVolatile, &LoadInst::setVolatile>>(
630+
this, Tracker));
631+
}
629632
cast<llvm::LoadInst>(Val)->setVolatile(V);
630633
}
631634

@@ -686,8 +689,12 @@ void LoadInst::dump() const {
686689

687690
void StoreInst::setVolatile(bool V) {
688691
auto &Tracker = Ctx.getTracker();
689-
if (Tracker.isTracking())
690-
Tracker.track(std::make_unique<SetVolatile>(this, Tracker));
692+
if (Tracker.isTracking()) {
693+
Tracker.track(
694+
std::make_unique<
695+
GenericSetter<&StoreInst::isVolatile, &StoreInst::setVolatile>>(
696+
this, Tracker));
697+
}
691698
cast<llvm::StoreInst>(Val)->setVolatile(V);
692699
}
693700

@@ -1007,8 +1014,11 @@ llvm::SmallVector<BasicBlock *, 16> CallBrInst::getIndirectDests() const {
10071014
}
10081015
void CallBrInst::setDefaultDest(BasicBlock *BB) {
10091016
auto &Tracker = Ctx.getTracker();
1010-
if (Tracker.isTracking())
1011-
Tracker.track(std::make_unique<CallBrInstSetDefaultDest>(this, Tracker));
1017+
if (Tracker.isTracking()) {
1018+
Tracker.track(std::make_unique<GenericSetter<&CallBrInst::getDefaultDest,
1019+
&CallBrInst::setDefaultDest>>(
1020+
this, Tracker));
1021+
}
10121022
cast<llvm::CallBrInst>(Val)->setDefaultDest(cast<llvm::BasicBlock>(BB->Val));
10131023
}
10141024
void CallBrInst::setIndirectDest(unsigned Idx, BasicBlock *BB) {
@@ -1260,22 +1270,34 @@ AllocaInst *AllocaInst::create(Type *Ty, unsigned AddrSpace,
12601270

12611271
void AllocaInst::setAllocatedType(Type *Ty) {
12621272
auto &Tracker = Ctx.getTracker();
1263-
if (Tracker.isTracking())
1264-
Tracker.track(std::make_unique<AllocaSetAllocatedType>(this, Tracker));
1273+
if (Tracker.isTracking()) {
1274+
Tracker.track(
1275+
std::make_unique<GenericSetter<&AllocaInst::getAllocatedType,
1276+
&AllocaInst::setAllocatedType>>(
1277+
this, Tracker));
1278+
}
12651279
cast<llvm::AllocaInst>(Val)->setAllocatedType(Ty);
12661280
}
12671281

12681282
void AllocaInst::setAlignment(Align Align) {
12691283
auto &Tracker = Ctx.getTracker();
1270-
if (Tracker.isTracking())
1271-
Tracker.track(std::make_unique<AllocaSetAlignment>(this, Tracker));
1284+
if (Tracker.isTracking()) {
1285+
Tracker.track(
1286+
std::make_unique<
1287+
GenericSetter<&AllocaInst::getAlign, &AllocaInst::setAlignment>>(
1288+
this, Tracker));
1289+
}
12721290
cast<llvm::AllocaInst>(Val)->setAlignment(Align);
12731291
}
12741292

12751293
void AllocaInst::setUsedWithInAlloca(bool V) {
12761294
auto &Tracker = Ctx.getTracker();
1277-
if (Tracker.isTracking())
1278-
Tracker.track(std::make_unique<AllocaSetUsedWithInAlloca>(this, Tracker));
1295+
if (Tracker.isTracking()) {
1296+
Tracker.track(
1297+
std::make_unique<GenericSetter<&AllocaInst::isUsedWithInAlloca,
1298+
&AllocaInst::setUsedWithInAlloca>>(
1299+
this, Tracker));
1300+
}
12791301
cast<llvm::AllocaInst>(Val)->setUsedWithInAlloca(V);
12801302
}
12811303

llvm/lib/SandboxIR/Tracker.cpp

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -204,61 +204,6 @@ void RemoveFromParent::dump() const {
204204
}
205205
#endif
206206

207-
AllocaSetAllocatedType::AllocaSetAllocatedType(AllocaInst *Alloca,
208-
Tracker &Tracker)
209-
: IRChangeBase(Tracker), Alloca(Alloca),
210-
OrigType(Alloca->getAllocatedType()) {}
211-
212-
void AllocaSetAllocatedType::revert() { Alloca->setAllocatedType(OrigType); }
213-
214-
#ifndef NDEBUG
215-
void AllocaSetAllocatedType::dump() const {
216-
dump(dbgs());
217-
dbgs() << "\n";
218-
}
219-
#endif // NDEBUG
220-
221-
AllocaSetAlignment::AllocaSetAlignment(AllocaInst *Alloca, Tracker &Tracker)
222-
: IRChangeBase(Tracker), Alloca(Alloca), OrigAlign(Alloca->getAlign()) {}
223-
224-
void AllocaSetAlignment::revert() { Alloca->setAlignment(OrigAlign); }
225-
226-
#ifndef NDEBUG
227-
void AllocaSetAlignment::dump() const {
228-
dump(dbgs());
229-
dbgs() << "\n";
230-
}
231-
#endif // NDEBUG
232-
233-
AllocaSetUsedWithInAlloca::AllocaSetUsedWithInAlloca(AllocaInst *Alloca,
234-
Tracker &Tracker)
235-
: IRChangeBase(Tracker), Alloca(Alloca),
236-
Orig(Alloca->isUsedWithInAlloca()) {}
237-
238-
void AllocaSetUsedWithInAlloca::revert() { Alloca->setUsedWithInAlloca(Orig); }
239-
240-
#ifndef NDEBUG
241-
void AllocaSetUsedWithInAlloca::dump() const {
242-
dump(dbgs());
243-
dbgs() << "\n";
244-
}
245-
#endif // NDEBUG
246-
247-
CallBrInstSetDefaultDest::CallBrInstSetDefaultDest(CallBrInst *CallBr,
248-
Tracker &Tracker)
249-
: IRChangeBase(Tracker), CallBr(CallBr) {
250-
OrigDefaultDest = CallBr->getDefaultDest();
251-
}
252-
void CallBrInstSetDefaultDest::revert() {
253-
CallBr->setDefaultDest(OrigDefaultDest);
254-
}
255-
#ifndef NDEBUG
256-
void CallBrInstSetDefaultDest::dump() const {
257-
dump(dbgs());
258-
dbgs() << "\n";
259-
}
260-
#endif
261-
262207
CallBrInstSetIndirectDest::CallBrInstSetIndirectDest(CallBrInst *CallBr,
263208
unsigned Idx,
264209
Tracker &Tracker)
@@ -275,35 +220,6 @@ void CallBrInstSetIndirectDest::dump() const {
275220
}
276221
#endif
277222

278-
SetVolatile::SetVolatile(Instruction *I, Tracker &Tracker)
279-
: IRChangeBase(Tracker) {
280-
if (auto *Load = dyn_cast<LoadInst>(I)) {
281-
WasVolatile = Load->isVolatile();
282-
StoreOrLoad = Load;
283-
} else if (auto *Store = dyn_cast<StoreInst>(I)) {
284-
WasVolatile = Store->isVolatile();
285-
StoreOrLoad = Store;
286-
} else {
287-
llvm_unreachable("Expected LoadInst or StoreInst");
288-
}
289-
}
290-
291-
void SetVolatile::revert() {
292-
if (auto *Load = StoreOrLoad.dyn_cast<LoadInst *>()) {
293-
Load->setVolatile(WasVolatile);
294-
} else if (auto *Store = StoreOrLoad.dyn_cast<StoreInst *>()) {
295-
Store->setVolatile(WasVolatile);
296-
} else {
297-
llvm_unreachable("Expected LoadInst or StoreInst");
298-
}
299-
}
300-
#ifndef NDEBUG
301-
void SetVolatile::dump() const {
302-
dump(dbgs());
303-
dbgs() << "\n";
304-
}
305-
#endif
306-
307223
MoveInstr::MoveInstr(Instruction *MovedI, Tracker &Tracker)
308224
: IRChangeBase(Tracker), MovedI(MovedI) {
309225
if (auto *NextI = MovedI->getNextNode())

0 commit comments

Comments
 (0)