llvm.org GIT mirror llvm / b594a84
Give the always-inliner its own custom filter. It shouldn't have to pay the very high overhead of the complex inline cost analysis when all it wants to do is detect three patterns which must not be inlined. Comment the code, clean it up, and leave some hints about possible performance improvements if this ever shows up on a profile. Moving this off of the (now more expensive) inline cost analysis is particularly important because we have to run this inliner even at -O0. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@153814 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 8 years ago
1 changed file(s) with 63 addition(s) and 20 deletion(s). Raw diff Collapse all Expand all
3131
3232 // AlwaysInliner only inlines functions that are mark as "always inline".
3333 class AlwaysInliner : public Inliner {
34 InlineCostAnalyzer CA;
3534 public:
3635 // Use extremely low threshold.
3736 AlwaysInliner() : Inliner(ID, -2000000000, /*InsertLifetime*/true) {
4241 initializeAlwaysInlinerPass(*PassRegistry::getPassRegistry());
4342 }
4443 static char ID; // Pass identification, replacement for typeid
45 InlineCost getInlineCost(CallSite CS) {
46 Function *Callee = CS.getCalledFunction();
47 // We assume indirect calls aren't calling an always-inline function.
48 if (!Callee) return InlineCost::getNever();
49
50 // We can't inline calls to external functions.
51 // FIXME: We shouldn't even get here.
52 if (Callee->isDeclaration()) return InlineCost::getNever();
53
54 // Return never for anything not marked as always inline.
55 if (!Callee->hasFnAttr(Attribute::AlwaysInline))
56 return InlineCost::getNever();
57
58 // We still have to check the inline cost in case there are reasons to
59 // not inline which trump the always-inline attribute such as setjmp and
60 // indirectbr.
61 return CA.getInlineCost(CS, getInlineThreshold(CS));
62 }
44 virtual InlineCost getInlineCost(CallSite CS);
6345 virtual bool doFinalization(CallGraph &CG) {
6446 return removeDeadFunctions(CG, /*AlwaysInlineOnly=*/true);
6547 }
8062 return new AlwaysInliner(InsertLifetime);
8163 }
8264
65 /// \brief Minimal filter to detect invalid constructs for inlining.
66 static bool isInlineViable(Function &F) {
67 bool ReturnsTwice = F.hasFnAttr(Attribute::ReturnsTwice);
68 for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) {
69 // Disallow inlining of functions which contain an indirect branch.
70 if (isa(BI->getTerminator()))
71 return false;
72
73 for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE;
74 ++II) {
75 CallSite CS(II);
76 if (!CS)
77 continue;
78
79 // Disallow recursive calls.
80 if (&F == CS.getCalledFunction())
81 return false;
82
83 // Disallow calls which expose returns-twice to a function not previously
84 // attributed as such.
85 if (ReturnsTwice && CS.isCall() &&
86 cast(CS.getInstruction())->canReturnTwice())
87 return false;
88 }
89 }
90
91 return true;
92 }
93
94 /// \brief Get the inline cost for the always-inliner.
95 ///
96 /// The always inliner *only* handles functions which are marked with the
97 /// attribute to force inlining. As such, it is dramatically simpler and avoids
98 /// using the powerful (but expensive) inline cost analysis. Instead it uses
99 /// a very simple and boring direct walk of the instructions looking for
100 /// impossible-to-inline constructs.
101 ///
102 /// Note, it would be possible to go to some lengths to cache the information
103 /// computed here, but as we only expect to do this for relatively few and
104 /// small functions which have the explicit attribute to force inlining, it is
105 /// likely not worth it in practice.
106 InlineCost AlwaysInliner::getInlineCost(CallSite CS) {
107 Function *Callee = CS.getCalledFunction();
108 // We assume indirect calls aren't calling an always-inline function.
109 if (!Callee) return InlineCost::getNever();
110
111 // We can't inline calls to external functions.
112 // FIXME: We shouldn't even get here.
113 if (Callee->isDeclaration()) return InlineCost::getNever();
114
115 // Return never for anything not marked as always inline.
116 if (!Callee->hasFnAttr(Attribute::AlwaysInline))
117 return InlineCost::getNever();
118
119 // Do some minimal analysis to preclude non-viable functions.
120 if (!isInlineViable(*Callee))
121 return InlineCost::getNever();
122
123 // Otherwise, force inlining.
124 return InlineCost::getAlways();
125 }
126
83127 // doInitialization - Initializes the vector of functions that have not
84128 // been annotated with the "always inline" attribute.
85129 bool AlwaysInliner::doInitialization(CallGraph &CG) {
86 CA.setTargetData(getAnalysisIfAvailable());
87130 return false;
88131 }