Index: llvm/trunk/include/llvm/IR/Instruction.h =================================================================== --- llvm/trunk/include/llvm/IR/Instruction.h +++ llvm/trunk/include/llvm/IR/Instruction.h @@ -535,6 +535,14 @@ /// matters, isSafeToSpeculativelyExecute may be more appropriate. bool mayHaveSideEffects() const { return mayWriteToMemory() || mayThrow(); } + /// Return true if the instruction can be removed if the result is unused. + /// + /// When constant folding some instructions cannot be removed even if their + /// results are unused. Specifically terminator instructions and calls that + /// may have side effects cannot be removed without semantically changing the + /// generated program. + bool isSafeToRemove() const; + /// Return true if the instruction is a variety of EH-block. bool isEHPad() const { switch (getOpcode()) { Index: llvm/trunk/lib/IR/Instruction.cpp =================================================================== --- llvm/trunk/lib/IR/Instruction.cpp +++ llvm/trunk/lib/IR/Instruction.cpp @@ -589,6 +589,11 @@ return isa(this); } +bool Instruction::isSafeToRemove() const { + return (!isa(this) || !this->mayHaveSideEffects()) && + !isa(this); +} + bool Instruction::isAssociative() const { unsigned Opcode = getOpcode(); if (isAssociative(Opcode)) Index: llvm/trunk/lib/Transforms/Scalar/SCCP.cpp =================================================================== --- llvm/trunk/lib/Transforms/Scalar/SCCP.cpp +++ llvm/trunk/lib/Transforms/Scalar/SCCP.cpp @@ -1900,7 +1900,7 @@ if (Inst->getType()->isVoidTy()) continue; if (tryToReplaceWithConstant(Solver, Inst)) { - if (!isa(Inst) && !isa(Inst)) + if (Inst->isSafeToRemove()) Inst->eraseFromParent(); // Hey, we just changed something! MadeChanges = true; Index: llvm/trunk/test/Transforms/IPConstantProp/remove-call-inst.ll =================================================================== --- llvm/trunk/test/Transforms/IPConstantProp/remove-call-inst.ll +++ llvm/trunk/test/Transforms/IPConstantProp/remove-call-inst.ll @@ -0,0 +1,33 @@ +; RUN: opt < %s -S -ipsccp | FileCheck %s +; PR5596 + +; IPSCCP should propagate the 0 argument, eliminate the switch, and propagate +; the result. + +; CHECK: define i32 @main() #0 { +; CHECK-NEXT: entry: +; CHECK-NOT: call +; CHECK-NEXT: ret i32 123 + +define i32 @main() noreturn nounwind { +entry: + %call2 = tail call i32 @wwrite(i64 0) nounwind + ret i32 %call2 +} + +define internal i32 @wwrite(i64 %i) nounwind readnone { +entry: + switch i64 %i, label %sw.default [ + i64 3, label %return + i64 10, label %return + ] + +sw.default: + ret i32 123 + +return: + ret i32 0 +} + +; CHECK: attributes #0 = { noreturn nounwind } +; CHECK: attributes #1 = { nounwind readnone } Index: llvm/trunk/test/Transforms/IPConstantProp/user-with-multiple-uses.ll =================================================================== --- llvm/trunk/test/Transforms/IPConstantProp/user-with-multiple-uses.ll +++ llvm/trunk/test/Transforms/IPConstantProp/user-with-multiple-uses.ll @@ -15,7 +15,7 @@ ret i32 %call2 } -define internal i32 @wwrite(i64 %i) nounwind readnone { +define internal i32 @wwrite(i64 %i) nounwind { entry: switch i64 %i, label %sw.default [ i64 3, label %return @@ -30,5 +30,4 @@ } ; CHECK: attributes #0 = { noreturn nounwind } -; CHECK: attributes #1 = { nounwind readnone } -; CHECK: attributes [[NUW]] = { nounwind } +; CHECK: attributes #1 = { nounwind }