Index: lib/Transforms/Scalar/Reassociate.cpp =================================================================== --- lib/Transforms/Scalar/Reassociate.cpp +++ lib/Transforms/Scalar/Reassociate.cpp @@ -2264,7 +2264,7 @@ MadeChange = false; for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) { // Optimize every instruction in the basic block. - for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE; ) + for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE; ) { if (isInstructionTriviallyDead(II)) { EraseInst(II++); } else { @@ -2273,13 +2273,17 @@ ++II; } - // If this produced extra instructions to optimize, handle them now. - while (!RedoInsts.empty()) { - Instruction *I = RedoInsts.pop_back_val(); - if (isInstructionTriviallyDead(I)) - EraseInst(I); - else - OptimizeInst(I); + // If this produced extra instructions to optimize, handle them now. + while (!RedoInsts.empty()) { + Instruction *I = RedoInsts.pop_back_val(); + if (I == II) + // Will be processed next iteration on the basic block + continue; + if (isInstructionTriviallyDead(I)) + EraseInst(I); + else + OptimizeInst(I); + } } } Index: test/Transforms/Reassociate/crash3.ll =================================================================== --- /dev/null +++ test/Transforms/Reassociate/crash3.ll @@ -0,0 +1,15 @@ +; RUN: opt -reassociate -o - -S %s | FileCheck %s + +declare void @bar(i32) + +define i32 @foo(i32 %in) { +; CHECK-LABEL: @foo +; CHECK: %_factor = mul i32 %_3, -4 + %_0 = add i32 %in, 1 + %_1 = mul i32 %in, -2 + %_2 = add i32 %_0, %_1 + %_3 = add i32 %_1, %_2 + %_4 = add i32 %_3, 1 + %_5 = add i32 %in, %_3 + ret i32 %_5 +}