[PATCH] D38896: [InstCombine] Drop reference to callee from an operand of erased call instruction

Eugene Leviant via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 13 10:27:33 PDT 2017


evgeny777 created this revision.
Herald added a subscriber: eraman.

LLVM inliner gives a huge bonus (15000) to internal functions which have a single call site (hasOneUse is true). However this doesn't happen call uses bitcast expression referencing callee, because reference from that expression to callee is not being dropped by instcombine.

In practice this leads to a funny side effect when those two commands:

  opt -instcombine -inline -S file.ll

and

  opt -instcombine -S file.ll | opt -inline -S

may produce different results.


Repository:
  rL LLVM

https://reviews.llvm.org/D38896

Files:
  lib/Transforms/InstCombine/InstCombineCalls.cpp
  test/Transforms/Inline/inline-constexpr-cast-call.ll


Index: test/Transforms/Inline/inline-constexpr-cast-call.ll
===================================================================
--- test/Transforms/Inline/inline-constexpr-cast-call.ll
+++ test/Transforms/Inline/inline-constexpr-cast-call.ll
@@ -0,0 +1,22 @@
+; RUN: opt < %s -instcombine -inline -inline-threshold=0
+
+; CHECK: define i32 @main() {
+; CHECK:   %1 = call i32 @rand()
+; CHECK:   %2 = call i32 @rand()
+; CHECK:   %3 = add nsw i32 %1, %2
+; CHECK:   ret i32 %3
+; CHECK: }
+
+define internal i32 @foo() #0 {
+  %1 = call i32 @rand() #2
+  %2 = call i32 @rand() #2
+  %3 = add nsw i32 %1, %2
+  ret i32 %3
+}
+
+declare i32 @rand() #1
+
+define i32 @main() #0 {
+  %1 = tail call i32 (...) bitcast (i32 ()* @foo to i32 (...)*)() #2
+  ret i32 %1
+}
Index: lib/Transforms/InstCombine/InstCombineCalls.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -4233,6 +4233,11 @@
       ValueHandleBase::ValueIsDeleted(Caller);
   }
 
+  // Drop references from bitcast constant expression to the callee.
+  if (auto *Op = dyn_cast<User>(Caller->getOperand(0)))
+    if (Op->hasOneUse())
+      Op->dropAllReferences();
+      
   eraseInstFromFunction(*Caller);
   return true;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D38896.118937.patch
Type: text/x-patch
Size: 1316 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171013/9de386d5/attachment.bin>


More information about the llvm-commits mailing list