Refactor code from inlining and globalopt that checks whether a function definition...
authorEli Friedman <eli.friedman@gmail.com>
Thu, 20 Oct 2011 05:23:42 +0000 (05:23 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Thu, 20 Oct 2011 05:23:42 +0000 (05:23 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@142572 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Function.h
lib/Transforms/IPO/GlobalOpt.cpp
lib/Transforms/IPO/Inliner.cpp
lib/Transforms/Utils/BasicInliner.cpp
lib/VMCore/Function.cpp
test/Transforms/GlobalOpt/deadfunction.ll [new file with mode: 0644]

index 678651bbf1f8d75a3eb7292f82d429097a265d3d..b5916b7572b5366f6a66762fef2a36f319781a5a 100644 (file)
@@ -425,6 +425,12 @@ public:
   ///
   bool hasAddressTaken(const User** = 0) const;
 
+  /// isDefTriviallyDead - Return true if it is trivially safe to remove
+  /// this function definition from the module (because it isn't externally
+  /// visible, does not have its address taken, and has no callers).  To make
+  /// this more accurate, call removeDeadConstantUsers first.
+  bool isDefTriviallyDead() const;
+
   /// callsFunctionThatReturnsTwice - Return true if the function has a call to
   /// setjmp or other function that gcc recognizes as "returning twice".
   bool callsFunctionThatReturnsTwice() const;
index 3552d03919bae6604e8dee0f53840a0d2c9ce879..c57e9fc0e8d781644ee042f3af888570aff57454 100644 (file)
@@ -1890,7 +1890,7 @@ bool GlobalOpt::OptimizeFunctions(Module &M) {
     if (!F->hasName() && !F->isDeclaration())
       F->setLinkage(GlobalValue::InternalLinkage);
     F->removeDeadConstantUsers();
-    if (F->use_empty() && (F->hasLocalLinkage() || F->hasLinkOnceLinkage())) {
+    if (F->isDefTriviallyDead()) {
       F->eraseFromParent();
       Changed = true;
       ++NumFnDeleted;
index f00935b08887d41777232ced2eeb5ae258dea172..bdc9fe45d34896187eebcfcdb17187f6fa1a605e 100644 (file)
@@ -533,10 +533,7 @@ bool Inliner::removeDeadFunctions(CallGraph &CG,
 
     if (DNR && DNR->count(F))
       continue;
-    if (!F->hasLinkOnceLinkage() && !F->hasLocalLinkage() &&
-        !F->hasAvailableExternallyLinkage())
-      continue;
-    if (!F->use_empty())
+    if (!F->isDefTriviallyDead())
       continue;
     
     // Remove any call graph edges from the function to its callees.
index 23a30cc585077de604dfbc4392ac4d63c898cd39..50c91b6af87b05ca8eab9a426eafcec3a509d79a 100644 (file)
@@ -131,8 +131,8 @@ void BasicInlinerImpl::inlineFunctions() {
         // Inline
         InlineFunctionInfo IFI(0, TD);
         if (InlineFunction(CS, IFI)) {
-          if (Callee->use_empty() && (Callee->hasLocalLinkage() ||
-                                      Callee->hasAvailableExternallyLinkage()))
+          Callee->removeDeadConstantUsers();
+          if (Callee->isDefTriviallyDead())
             DeadFunctions.insert(Callee);
           Changed = true;
           CallSites.erase(CallSites.begin() + index);
index be0f05670441142b680db46699c54621597b7bfc..bb8f62ac295346ab440f2679201a4b561c219cae 100644 (file)
@@ -402,6 +402,7 @@ Function *Intrinsic::getDeclaration(Module *M, ID id, ArrayRef<Type*> Tys) {
 bool Function::hasAddressTaken(const User* *PutOffender) const {
   for (Value::const_use_iterator I = use_begin(), E = use_end(); I != E; ++I) {
     const User *U = *I;
+    // FIXME: Check for blockaddress, which does not take the address.
     if (!isa<CallInst>(U) && !isa<InvokeInst>(U))
       return PutOffender ? (*PutOffender = U, true) : true;
     ImmutableCallSite CS(cast<Instruction>(U));
@@ -411,6 +412,20 @@ bool Function::hasAddressTaken(const User* *PutOffender) const {
   return false;
 }
 
+bool Function::isDefTriviallyDead() const {
+  // Check the linkage
+  if (!hasLinkOnceLinkage() && !hasLocalLinkage() &&
+      !hasAvailableExternallyLinkage())
+    return false;
+
+  // Check if the function is used by anything other than a blockaddress.
+  for (Value::const_use_iterator I = use_begin(), E = use_end(); I != E; ++I)
+    if (!isa<BlockAddress>(*I))
+      return false;
+
+  return true;
+}
+
 /// callsFunctionThatReturnsTwice - Return true if the function has a call to
 /// setjmp or other function that gcc recognizes as "returning twice".
 bool Function::callsFunctionThatReturnsTwice() const {
diff --git a/test/Transforms/GlobalOpt/deadfunction.ll b/test/Transforms/GlobalOpt/deadfunction.ll
new file mode 100644 (file)
index 0000000..5e003c6
--- /dev/null
@@ -0,0 +1,27 @@
+; RUN: opt < %s -globalopt -S | FileCheck %s
+
+; CHECK-NOT: test
+
+declare void @aa()
+declare void @bb()
+
+; Test that we can erase a function which has a blockaddress referring to it
+@test.x = internal unnamed_addr constant [3 x i8*] [i8* blockaddress(@test, %a), i8* blockaddress(@test, %b), i8* blockaddress(@test, %c)], align 16
+define internal void @test(i32 %n) nounwind noinline {
+entry:
+  %idxprom = sext i32 %n to i64
+  %arrayidx = getelementptr inbounds [3 x i8*]* @test.x, i64 0, i64 %idxprom
+  %0 = load i8** %arrayidx, align 8
+  indirectbr i8* %0, [label %a, label %b, label %c]
+
+a:
+  tail call void @aa() nounwind
+  br label %b
+
+b:
+  tail call void @bb() nounwind
+  br label %c
+
+c:
+  ret void
+}