[PATCH] D38856: [IPSCCP] Remove calls without side effects

Chris Bieneman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 12 10:47:39 PDT 2017


beanz created this revision.

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.


https://reviews.llvm.org/D38856

Files:
  lib/Transforms/Scalar/SCCP.cpp
  test/Transforms/IPConstantProp/remove-call-inst.ll
  test/Transforms/IPConstantProp/user-with-multiple-uses.ll


Index: test/Transforms/IPConstantProp/user-with-multiple-uses.ll
===================================================================
--- test/Transforms/IPConstantProp/user-with-multiple-uses.ll
+++ 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 }
Index: test/Transforms/IPConstantProp/remove-call-inst.ll
===================================================================
--- test/Transforms/IPConstantProp/remove-call-inst.ll
+++ test/Transforms/IPConstantProp/remove-call-inst.ll
@@ -6,7 +6,7 @@
 
 ; CHECK: define i32 @main() #0 {
 ; CHECK-NEXT: entry:
-; CHECK-NEXT: %call2 = tail call i32 @wwrite(i64 0) [[NUW:#[0-9]+]]
+; CHECK-NOT: call
 ; CHECK-NEXT: ret i32 123
 
 define i32 @main() noreturn nounwind {
@@ -31,4 +31,3 @@
 
 ; CHECK: attributes #0 = { noreturn nounwind }
 ; CHECK: attributes #1 = { nounwind readnone }
-; CHECK: attributes [[NUW]] = { nounwind }
Index: lib/Transforms/Scalar/SCCP.cpp
===================================================================
--- lib/Transforms/Scalar/SCCP.cpp
+++ lib/Transforms/Scalar/SCCP.cpp
@@ -1926,7 +1926,8 @@
         if (Inst->getType()->isVoidTy())
           continue;
         if (tryToReplaceWithConstant(Solver, Inst)) {
-          if (!isa<CallInst>(Inst) && !isa<TerminatorInst>(Inst))
+          if ((!isa<CallInst>(Inst) || !Inst->mayHaveSideEffects()) &&
+              !isa<TerminatorInst>(Inst))
             Inst->eraseFromParent();
           // Hey, we just changed something!
           MadeChanges = true;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D38856.118809.patch
Type: text/x-patch
Size: 1917 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171012/29b22a82/attachment.bin>


More information about the llvm-commits mailing list