mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-11-02 07:11:49 +00:00
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.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@142572 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
d7e5264b46
commit
c66330504c
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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.
|
||||
|
@ -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);
|
||||
|
@ -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 {
|
||||
|
27
test/Transforms/GlobalOpt/deadfunction.ll
Normal file
27
test/Transforms/GlobalOpt/deadfunction.ll
Normal file
@ -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
|
||||
}
|
Loading…
Reference in New Issue
Block a user