Skip to content

Commit 9e66c4e

Browse files
committed
[Utils] Use WeakTrackingVH in vector used as scratch storage.
The utility method RecursivelyDeleteTriviallyDeadInstructions receives as input a vector of Instructions, where all inputs are valid instructions. This same vector is used as a scratch storage (per the header comment) to recursively delete instructions. If an instruction is added as an operand of multiple other instructions, it may be added twice, then deleted once, then the second reference in the vector is invalid. Switch to using a Vector<WeakTrackingVH>. This change facilitates a clean-up in LoopStrengthReduction.
1 parent 2af74e2 commit 9e66c4e

File tree

5 files changed

+16
-13
lines changed

5 files changed

+16
-13
lines changed

llvm/include/llvm/Transforms/Utils/Local.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ bool RecursivelyDeleteTriviallyDeadInstructions(
153153
/// `DeadInsts` will be used as scratch storage for this routine and will be
154154
/// empty afterward.
155155
void RecursivelyDeleteTriviallyDeadInstructions(
156-
SmallVectorImpl<Instruction *> &DeadInsts,
156+
SmallVectorImpl<WeakTrackingVH> &DeadInsts,
157157
const TargetLibraryInfo *TLI = nullptr, MemorySSAUpdater *MSSAU = nullptr);
158158

159159
/// If the specified value is an effectively dead PHI node, due to being a

llvm/lib/Transforms/IPO/Attributor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6041,7 +6041,7 @@ ChangeStatus Attributor::run(Module &M) {
60416041
<< ToBeDeletedInsts.size() << " instructions and "
60426042
<< ToBeChangedUses.size() << " uses\n");
60436043

6044-
SmallVector<Instruction *, 32> DeadInsts;
6044+
SmallVector<WeakTrackingVH, 32> DeadInsts;
60456045
SmallVector<Instruction *, 32> TerminatorsToFold;
60466046

60476047
for (auto &It : ToBeChangedUses) {

llvm/lib/Transforms/Scalar/InstSimplifyPass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static bool runImpl(Function &F, const SimplifyQuery &SQ,
4040
if (!SQ.DT->isReachableFromEntry(&BB))
4141
continue;
4242

43-
SmallVector<Instruction *, 8> DeadInstsInBB;
43+
SmallVector<WeakTrackingVH, 8> DeadInstsInBB;
4444
for (Instruction &I : BB) {
4545
// The first time through the loop, ToSimplify is empty and we try to
4646
// simplify all instructions. On later iterations, ToSimplify is not

llvm/lib/Transforms/Scalar/LoopInstSimplify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ static bool simplifyLoopInst(Loop &L, DominatorTree &DT, LoopInfo &LI,
6868

6969
// While simplifying we may discover dead code or cause code to become dead.
7070
// Keep track of all such instructions and we will delete them at the end.
71-
SmallVector<Instruction *, 8> DeadInsts;
71+
SmallVector<WeakTrackingVH, 8> DeadInsts;
7272

7373
// First we want to create an RPO traversal of the loop body. By processing in
7474
// RPO we can ensure that definitions are processed prior to uses (for non PHI

llvm/lib/Transforms/Utils/Local.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -443,29 +443,32 @@ bool llvm::RecursivelyDeleteTriviallyDeadInstructions(
443443
if (!I || !isInstructionTriviallyDead(I, TLI))
444444
return false;
445445

446-
SmallVector<Instruction*, 16> DeadInsts;
446+
SmallVector<WeakTrackingVH, 16> DeadInsts;
447447
DeadInsts.push_back(I);
448448
RecursivelyDeleteTriviallyDeadInstructions(DeadInsts, TLI, MSSAU);
449449

450450
return true;
451451
}
452452

453453
void llvm::RecursivelyDeleteTriviallyDeadInstructions(
454-
SmallVectorImpl<Instruction *> &DeadInsts, const TargetLibraryInfo *TLI,
454+
SmallVectorImpl<WeakTrackingVH> &DeadInsts, const TargetLibraryInfo *TLI,
455455
MemorySSAUpdater *MSSAU) {
456456
// Process the dead instruction list until empty.
457457
while (!DeadInsts.empty()) {
458-
Instruction &I = *DeadInsts.pop_back_val();
459-
assert(I.use_empty() && "Instructions with uses are not dead.");
460-
assert(isInstructionTriviallyDead(&I, TLI) &&
458+
Value *V = DeadInsts.pop_back_val();
459+
Instruction *I = cast_or_null<Instruction>(V);
460+
if (!I)
461+
continue;
462+
assert(isInstructionTriviallyDead(I, TLI) &&
461463
"Live instruction found in dead worklist!");
464+
assert(I->use_empty() && "Instructions with uses are not dead.");
462465

463466
// Don't lose the debug info while deleting the instructions.
464-
salvageDebugInfo(I);
467+
salvageDebugInfo(*I);
465468

466469
// Null out all of the instruction's operands to see if any operand becomes
467470
// dead as we go.
468-
for (Use &OpU : I.operands()) {
471+
for (Use &OpU : I->operands()) {
469472
Value *OpV = OpU.get();
470473
OpU.set(nullptr);
471474

@@ -480,9 +483,9 @@ void llvm::RecursivelyDeleteTriviallyDeadInstructions(
480483
DeadInsts.push_back(OpI);
481484
}
482485
if (MSSAU)
483-
MSSAU->removeMemoryAccess(&I);
486+
MSSAU->removeMemoryAccess(I);
484487

485-
I.eraseFromParent();
488+
I->eraseFromParent();
486489
}
487490
}
488491

0 commit comments

Comments
 (0)