[llvm-commits] [llvm] r142572 - in /llvm/trunk: 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
Eli Friedman
eli.friedman at gmail.com
Wed Oct 19 22:23:42 PDT 2011
Author: efriedma
Date: Thu Oct 20 00:23:42 2011
New Revision: 142572
URL: http://llvm.org/viewvc/llvm-project?rev=142572&view=rev
Log:
Refactor code from inlining and globalopt that checks whether a function definition is unused, and enhance it so it can tell that functions which are only used by a blockaddress are in fact dead. This probably doesn't happen much on most code, but the Linux kernel's _THIS_IP_ can trigger this issue with blockaddress. (GlobalDCE can also handle the given tescase, but we only run that at -O3.) Found while looking at PR11180.
Added:
llvm/trunk/test/Transforms/GlobalOpt/deadfunction.ll
Modified:
llvm/trunk/include/llvm/Function.h
llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp
llvm/trunk/lib/Transforms/IPO/Inliner.cpp
llvm/trunk/lib/Transforms/Utils/BasicInliner.cpp
llvm/trunk/lib/VMCore/Function.cpp
Modified: llvm/trunk/include/llvm/Function.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Function.h?rev=142572&r1=142571&r2=142572&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Function.h (original)
+++ llvm/trunk/include/llvm/Function.h Thu Oct 20 00:23:42 2011
@@ -425,6 +425,12 @@
///
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;
Modified: llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp?rev=142572&r1=142571&r2=142572&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp (original)
+++ llvm/trunk/lib/Transforms/IPO/GlobalOpt.cpp Thu Oct 20 00:23:42 2011
@@ -1890,7 +1890,7 @@
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;
Modified: llvm/trunk/lib/Transforms/IPO/Inliner.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/Inliner.cpp?rev=142572&r1=142571&r2=142572&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/IPO/Inliner.cpp (original)
+++ llvm/trunk/lib/Transforms/IPO/Inliner.cpp Thu Oct 20 00:23:42 2011
@@ -533,10 +533,7 @@
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.
Modified: llvm/trunk/lib/Transforms/Utils/BasicInliner.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/BasicInliner.cpp?rev=142572&r1=142571&r2=142572&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/BasicInliner.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/BasicInliner.cpp Thu Oct 20 00:23:42 2011
@@ -131,8 +131,8 @@
// 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);
Modified: llvm/trunk/lib/VMCore/Function.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/VMCore/Function.cpp?rev=142572&r1=142571&r2=142572&view=diff
==============================================================================
--- llvm/trunk/lib/VMCore/Function.cpp (original)
+++ llvm/trunk/lib/VMCore/Function.cpp Thu Oct 20 00:23:42 2011
@@ -402,6 +402,7 @@
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 @@
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 {
Added: llvm/trunk/test/Transforms/GlobalOpt/deadfunction.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/GlobalOpt/deadfunction.ll?rev=142572&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/GlobalOpt/deadfunction.ll (added)
+++ llvm/trunk/test/Transforms/GlobalOpt/deadfunction.ll Thu Oct 20 00:23:42 2011
@@ -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
+ at 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
+}
More information about the llvm-commits
mailing list