From 5489d322ef9f6498d8454c3a89c45a086dd32b40 Mon Sep 17 00:00:00 2001 From: Chris Bieneman Date: Tue, 9 Jan 2018 21:58:46 +0000 Subject: [PATCH] [IPSCCP] Remove calls without side effects Summary: When performing constant propagation for call instructions we have historically replaced all uses of the return from a call, but not removed the call itself. This is required for correctness if the calls have side effects, however the compiler should be able to safely remove calls that don't have side effects. This allows the compiler to completely fold away calls to functions that have no side effects if the inputs are constant and the output can be determined at compile time. Reviewers: davide, sanjoy, bruno, dberlin Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D38856 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@322125 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/IR/Instruction.h | 8 +++++ lib/IR/Instruction.cpp | 5 +++ lib/Transforms/Scalar/SCCP.cpp | 2 +- .../IPConstantProp/remove-call-inst.ll | 33 +++++++++++++++++++ .../IPConstantProp/user-with-multiple-uses.ll | 5 ++- 5 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 test/Transforms/IPConstantProp/remove-call-inst.ll diff --git a/include/llvm/IR/Instruction.h b/include/llvm/IR/Instruction.h index 6af9cbfae5d..76bc4010d8c 100644 --- a/include/llvm/IR/Instruction.h +++ b/include/llvm/IR/Instruction.h @@ -535,6 +535,14 @@ public: /// 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()) { diff --git a/lib/IR/Instruction.cpp b/lib/IR/Instruction.cpp index 5f2a6146ad8..215c6907256 100644 --- a/lib/IR/Instruction.cpp +++ b/lib/IR/Instruction.cpp @@ -589,6 +589,11 @@ bool Instruction::mayThrow() const { return isa(this); } +bool Instruction::isSafeToRemove() const { + return (!isa(this) || !this->mayHaveSideEffects()) && + !isa(this); +} + bool Instruction::isAssociative() const { unsigned Opcode = getOpcode(); if (isAssociative(Opcode)) diff --git a/lib/Transforms/Scalar/SCCP.cpp b/lib/Transforms/Scalar/SCCP.cpp index df6053ac201..b6d034e9fb9 100644 --- a/lib/Transforms/Scalar/SCCP.cpp +++ b/lib/Transforms/Scalar/SCCP.cpp @@ -1900,7 +1900,7 @@ static bool runIPSCCP(Module &M, const DataLayout &DL, 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; diff --git a/test/Transforms/IPConstantProp/remove-call-inst.ll b/test/Transforms/IPConstantProp/remove-call-inst.ll new file mode 100644 index 00000000000..943086ab466 --- /dev/null +++ b/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 } diff --git a/test/Transforms/IPConstantProp/user-with-multiple-uses.ll b/test/Transforms/IPConstantProp/user-with-multiple-uses.ll index 968718084e4..3146709aec6 100644 --- a/test/Transforms/IPConstantProp/user-with-multiple-uses.ll +++ b/test/Transforms/IPConstantProp/user-with-multiple-uses.ll @@ -15,7 +15,7 @@ entry: 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 @@ return: } ; CHECK: attributes #0 = { noreturn nounwind } -; CHECK: attributes #1 = { nounwind readnone } -; CHECK: attributes [[NUW]] = { nounwind } +; CHECK: attributes #1 = { nounwind }