Skip to content

Commit 4498b9b

Browse files
committed
[VectorCombine] Avoid crash when the next node is deleted.
1 parent 839d0aa commit 4498b9b

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

llvm/lib/Transforms/Vectorize/VectorCombine.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ class VectorCombine {
9999

100100
InstructionWorklist Worklist;
101101

102+
/// Next instruction to iterate. It will be updated when it is erased by
103+
/// RecursivelyDeleteTriviallyDeadInstructions.
104+
Instruction *NextInst;
105+
102106
// TODO: Direct calls from the top-level "run" loop use a plain "Instruction"
103107
// parameter. That should be updated to specific sub-classes because the
104108
// run loop was changed to dispatch on opcode.
@@ -172,9 +176,11 @@ class VectorCombine {
172176
if (auto *OpI = dyn_cast<Instruction>(Op)) {
173177
if (RecursivelyDeleteTriviallyDeadInstructions(
174178
OpI, nullptr, nullptr, [this](Value *V) {
175-
if (auto I = dyn_cast<Instruction>(V)) {
179+
if (auto *I = dyn_cast<Instruction>(V)) {
176180
LLVM_DEBUG(dbgs() << "VC: Erased: " << *I << '\n');
177181
Worklist.remove(I);
182+
if (I == NextInst)
183+
NextInst = NextInst->getNextNode();
178184
}
179185
}))
180186
continue;
@@ -4254,13 +4260,18 @@ bool VectorCombine::run() {
42544260
if (!DT.isReachableFromEntry(&BB))
42554261
continue;
42564262
// Use early increment range so that we can erase instructions in loop.
4257-
for (Instruction &I : make_early_inc_range(BB)) {
4258-
if (I.isDebugOrPseudoInst())
4259-
continue;
4260-
MadeChange |= FoldInst(I);
4263+
Instruction *I = &BB.front();
4264+
while (I) {
4265+
NextInst = I->getNextNode();
4266+
if (!I->isDebugOrPseudoInst()) {
4267+
MadeChange |= FoldInst(*I);
4268+
}
4269+
I = NextInst;
42614270
}
42624271
}
42634272

4273+
NextInst = nullptr;
4274+
42644275
while (!Worklist.isEmpty()) {
42654276
Instruction *I = Worklist.removeOne();
42664277
if (!I)

llvm/test/Transforms/VectorCombine/X86/insert-binop-inseltpoison.ll

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,23 @@ define <4 x float> @ins3_ins3_fdiv(float %x, float %y) {
232232
%r = fdiv <4 x float> %i0, %i1
233233
ret <4 x float> %r
234234
}
235+
236+
; EEnsure we don't crash when erasing dead instructions.
237+
238+
define i32 @pr155110(i32 %x) {
239+
; CHECK-LABEL: @pr155110(
240+
; CHECK-NEXT: entry:
241+
; CHECK-NEXT: br label [[VECTOR_PH:%.*]]
242+
; CHECK: vector.ph:
243+
; CHECK-NEXT: br label [[VECTOR_PH]]
244+
;
245+
entry:
246+
br label %vector.ph
247+
248+
vector.ph: ; preds = %vector.ph, %entry
249+
%phi = phi i32 [ 0, %entry ], [ %reduce, %vector.ph ]
250+
%inselt = insertelement <4 x i32> poison, i32 %phi, i64 0
251+
%and = and <4 x i32> %inselt, zeroinitializer
252+
%reduce = call i32 @llvm.vector.reduce.and.v4i32(<4 x i32> zeroinitializer)
253+
br label %vector.ph
254+
}

0 commit comments

Comments
 (0)