From 9a3fc17ad47bf23e5b65e42f7600d91791d2c82e Mon Sep 17 00:00:00 2001 From: Easwaran Raman Date: Fri, 8 Apr 2016 21:28:02 +0000 Subject: [PATCH] Refactor Threshold computation. NFC. This is part of changes reviewed in http://reviews.llvm.org/D17584. llvm-svn: 265852 --- llvm/lib/Analysis/InlineCost.cpp | 57 ++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/llvm/lib/Analysis/InlineCost.cpp b/llvm/lib/Analysis/InlineCost.cpp index ceab8331a116..e29c09e7cab3 100644 --- a/llvm/lib/Analysis/InlineCost.cpp +++ b/llvm/lib/Analysis/InlineCost.cpp @@ -154,6 +154,9 @@ class CallAnalyzer : public InstVisitor { /// analysis. void updateThreshold(CallSite CS, Function &Callee); + /// Return true if size growth is allowed when inlining the callee at CS. + bool allowSizeGrowth(CallSite CS); + // Custom analysis routines. bool analyzeBlock(BasicBlock *BB, SmallPtrSetImpl &EphValues); @@ -572,7 +575,39 @@ bool CallAnalyzer::isKnownNonNullInCallee(Value *V) { return false; } +bool CallAnalyzer::allowSizeGrowth(CallSite CS) { + // If the normal destination of the invoke or the parent block of the call + // site is unreachable-terminated, there is little point in inlining this + // unless there is literally zero cost. + // FIXME: Note that it is possible that an unreachable-terminated block has a + // hot entry. For example, in below scenario inlining hot_call_X() may be + // beneficial : + // main() { + // hot_call_1(); + // ... + // hot_call_N() + // exit(0); + // } + // For now, we are not handling this corner case here as it is rare in real + // code. In future, we should elaborate this based on BPI and BFI in more + // general threshold adjusting heuristics in updateThreshold(). + Instruction *Instr = CS.getInstruction(); + if (InvokeInst *II = dyn_cast(Instr)) { + if (isa(II->getNormalDest()->getTerminator())) + return false; + } else if (isa(Instr->getParent()->getTerminator())) + return false; + + return true; +} + void CallAnalyzer::updateThreshold(CallSite CS, Function &Callee) { + // If no size growth is allowed for this inlining, set Threshold to 0. + if (!allowSizeGrowth(CS)) { + Threshold = 0; + return; + } + // If -inline-threshold is not given, listen to the optsize and minsize // attributes when they would decrease the threshold. Function *Caller = CS.getCaller(); @@ -1214,28 +1249,6 @@ bool CallAnalyzer::analyzeCall(CallSite CS) { if (OnlyOneCallAndLocalLinkage) Cost += InlineConstants::LastCallToStaticBonus; - // If the normal destination of the invoke or the parent block of the call - // site is unreachable-terminated, there is little point in inlining this - // unless there is literally zero cost. - // FIXME: Note that it is possible that an unreachable-terminated block has a - // hot entry. For example, in below scenario inlining hot_call_X() may be - // beneficial : - // main() { - // hot_call_1(); - // ... - // hot_call_N() - // exit(0); - // } - // For now, we are not handling this corner case here as it is rare in real - // code. In future, we should elaborate this based on BPI and BFI in more - // general threshold adjusting heuristics in updateThreshold(). - Instruction *Instr = CS.getInstruction(); - if (InvokeInst *II = dyn_cast(Instr)) { - if (isa(II->getNormalDest()->getTerminator())) - Threshold = 0; - } else if (isa(Instr->getParent()->getTerminator())) - Threshold = 0; - // If this function uses the coldcc calling convention, prefer not to inline // it. if (F.getCallingConv() == CallingConv::Cold)