Index: lib/Transforms/Scalar/Reassociate.cpp =================================================================== --- lib/Transforms/Scalar/Reassociate.cpp +++ lib/Transforms/Scalar/Reassociate.cpp @@ -2261,7 +2261,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 { @@ -2270,13 +2270,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/crash2.ll =================================================================== --- /dev/null +++ test/Transforms/Reassociate/crash2.ll @@ -0,0 +1,19 @@ +; RUN: opt -reassociate -disable-output < %s +; ModuleID = 'bugpoint-reduced-simplified.bc' + +define i32 @foo() { +wrapper_entry: + %0 = udiv i32 1, undef + %1 = mul i32 undef, %0 + %2 = add i32 %1, undef + %3 = add i32 %2, 1 + %4 = add i32 %3, undef + %5 = add i32 %3, 1 + %6 = mul i32 %4, -2 + %7 = add i32 %5, %6 + %8 = add i32 %6, %7 + ; this instruction is intentionally dead + %9 = add i32 %8, 1 + %10 = add i32 undef, %8 + ret i32 %10 +}