Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions llvm/lib/Transforms/Vectorize/VectorCombine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ class VectorCombine {

InstructionWorklist Worklist;

/// Next instruction to iterate. It will be updated when it is erased by
/// RecursivelyDeleteTriviallyDeadInstructions.
Instruction *NextInst;

// TODO: Direct calls from the top-level "run" loop use a plain "Instruction"
// parameter. That should be updated to specific sub-classes because the
// run loop was changed to dispatch on opcode.
Expand Down Expand Up @@ -172,9 +176,11 @@ class VectorCombine {
if (auto *OpI = dyn_cast<Instruction>(Op)) {
if (RecursivelyDeleteTriviallyDeadInstructions(
OpI, nullptr, nullptr, [this](Value *V) {
if (auto I = dyn_cast<Instruction>(V)) {
if (auto *I = dyn_cast<Instruction>(V)) {
LLVM_DEBUG(dbgs() << "VC: Erased: " << *I << '\n');
Worklist.remove(I);
if (I == NextInst)
NextInst = NextInst->getNextNode();
}
}))
continue;
Expand Down Expand Up @@ -4254,13 +4260,21 @@ bool VectorCombine::run() {
if (!DT.isReachableFromEntry(&BB))
continue;
// Use early increment range so that we can erase instructions in loop.
for (Instruction &I : make_early_inc_range(BB)) {
if (I.isDebugOrPseudoInst())
continue;
MadeChange |= FoldInst(I);
// make_early_inc_range is not applicable here, as the next iterator may
// be invalidated by RecursivelyDeleteTriviallyDeadInstructions.
// We manually maintain the next instruction and update it when it is about
// to be deleted.
Instruction *I = &BB.front();
while (I) {
NextInst = I->getNextNode();
if (!I->isDebugOrPseudoInst())
MadeChange |= FoldInst(*I);
I = NextInst;
}
}

NextInst = nullptr;

while (!Worklist.isEmpty()) {
Instruction *I = Worklist.removeOne();
if (!I)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,23 @@ define <4 x float> @ins3_ins3_fdiv(float %x, float %y) {
%r = fdiv <4 x float> %i0, %i1
ret <4 x float> %r
}

; Ensure we don't crash when erasing dead instructions.

define i32 @pr155110(i32 %x) {
; CHECK-LABEL: @pr155110(
; CHECK-NEXT: entry:
; CHECK-NEXT: br label [[VECTOR_PH:%.*]]
; CHECK: vector.ph:
; CHECK-NEXT: br label [[VECTOR_PH]]
;
entry:
br label %vector.ph

vector.ph: ; preds = %vector.ph, %entry
%phi = phi i32 [ 0, %entry ], [ %reduce, %vector.ph ]
%inselt = insertelement <4 x i32> poison, i32 %phi, i64 0
%and = and <4 x i32> %inselt, zeroinitializer
%reduce = call i32 @llvm.vector.reduce.and.v4i32(<4 x i32> zeroinitializer)
br label %vector.ph
}
Loading