llvm.org GIT mirror llvm / 2f3e898
Reapply r268107 after fixing a bug breaks debug build. Makes the new method to set data needed by debug dump. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@268130 91177308-0d34-0410-b5e6-96231b3b80d8 Xinliang David Li 3 years ago
2 changed file(s) with 88 addition(s) and 71 deletion(s). Raw diff Collapse all Expand all
6868 /// shouldInline - Return true if the inliner should attempt to
6969 /// inline at the given CallSite.
7070 bool shouldInline(CallSite CS);
71 /// Return true if inlining of CS can block the caller from being
72 /// inlined which is proved to be more beneficial. \p IC is the
73 /// estimated inline cost associated with callsite \p CS.
74 /// \p TotalAltCost will be set to the estimated cost of inlining the caller
75 /// if \p CS is suppressed for inlining.
76 bool shouldBeDeferred(Function *Caller, CallSite CS, InlineCost IC,
77 int &TotalAltCost);
7178
7279 protected:
7380 AssumptionCacheTracker *ACT;
226226 emitOptimizationRemarkAnalysis(Ctx, DEBUG_TYPE, *Caller, DLoc, Msg);
227227 }
228228
229 bool Inliner::shouldBeDeferred(Function *Caller, CallSite CS, InlineCost IC,
230 int &TotalSecondaryCost) {
231
232 // For now we only handle local or inline functions.
233 if (!Caller->hasLocalLinkage() && !Caller->hasLinkOnceODRLinkage())
234 return false;
235 // Try to detect the case where the current inlining candidate caller (call
236 // it B) is a static or linkonce-ODR function and is an inlining candidate
237 // elsewhere, and the current candidate callee (call it C) is large enough
238 // that inlining it into B would make B too big to inline later. In these
239 // circumstances it may be best not to inline C into B, but to inline B into
240 // its callers.
241 //
242 // This only applies to static and linkonce-ODR functions because those are
243 // expected to be available for inlining in the translation units where they
244 // are used. Thus we will always have the opportunity to make local inlining
245 // decisions. Importantly the linkonce-ODR linkage covers inline functions
246 // and templates in C++.
247 //
248 // FIXME: All of this logic should be sunk into getInlineCost. It relies on
249 // the internal implementation of the inline cost metrics rather than
250 // treating them as truly abstract units etc.
251 TotalSecondaryCost = 0;
252 // The candidate cost to be imposed upon the current function.
253 int CandidateCost = IC.getCost() - (InlineConstants::CallPenalty + 1);
254 // This bool tracks what happens if we do NOT inline C into B.
255 bool callerWillBeRemoved = Caller->hasLocalLinkage();
256 // This bool tracks what happens if we DO inline C into B.
257 bool inliningPreventsSomeOuterInline = false;
258 for (User *U : Caller->users()) {
259 CallSite CS2(U);
260
261 // If this isn't a call to Caller (it could be some other sort
262 // of reference) skip it. Such references will prevent the caller
263 // from being removed.
264 if (!CS2 || CS2.getCalledFunction() != Caller) {
265 callerWillBeRemoved = false;
266 continue;
267 }
268
269 InlineCost IC2 = getInlineCost(CS2);
270 ++NumCallerCallersAnalyzed;
271 if (!IC2) {
272 callerWillBeRemoved = false;
273 continue;
274 }
275 if (IC2.isAlways())
276 continue;
277
278 // See if inlining or original callsite would erase the cost delta of
279 // this callsite. We subtract off the penalty for the call instruction,
280 // which we would be deleting.
281 if (IC2.getCostDelta() <= CandidateCost) {
282 inliningPreventsSomeOuterInline = true;
283 TotalSecondaryCost += IC2.getCost();
284 }
285 }
286 // If all outer calls to Caller would get inlined, the cost for the last
287 // one is set very low by getInlineCost, in anticipation that Caller will
288 // be removed entirely. We did not account for this above unless there
289 // is only one caller of Caller.
290 if (callerWillBeRemoved && !Caller->use_empty())
291 TotalSecondaryCost += InlineConstants::LastCallToStaticBonus;
292
293 if (inliningPreventsSomeOuterInline && TotalSecondaryCost < IC.getCost())
294 return true;
295
296 return false;
297 }
298
229299 /// Return true if the inliner should attempt to inline at the given CallSite.
230300 bool Inliner::shouldInline(CallSite CS) {
231301 InlineCost IC = getInlineCost(CS);
257327 Twine(IC.getCostDelta() + IC.getCost()) + ")");
258328 return false;
259329 }
260
261 // Try to detect the case where the current inlining candidate caller (call
262 // it B) is a static or linkonce-ODR function and is an inlining candidate
263 // elsewhere, and the current candidate callee (call it C) is large enough
264 // that inlining it into B would make B too big to inline later. In these
265 // circumstances it may be best not to inline C into B, but to inline B into
266 // its callers.
267 //
268 // This only applies to static and linkonce-ODR functions because those are
269 // expected to be available for inlining in the translation units where they
270 // are used. Thus we will always have the opportunity to make local inlining
271 // decisions. Importantly the linkonce-ODR linkage covers inline functions
272 // and templates in C++.
273 //
274 // FIXME: All of this logic should be sunk into getInlineCost. It relies on
275 // the internal implementation of the inline cost metrics rather than
276 // treating them as truly abstract units etc.
277 if (Caller->hasLocalLinkage() || Caller->hasLinkOnceODRLinkage()) {
278 int TotalSecondaryCost = 0;
279 // The candidate cost to be imposed upon the current function.
280 int CandidateCost = IC.getCost() - (InlineConstants::CallPenalty + 1);
281 // This bool tracks what happens if we do NOT inline C into B.
282 bool callerWillBeRemoved = Caller->hasLocalLinkage();
283 // This bool tracks what happens if we DO inline C into B.
284 bool inliningPreventsSomeOuterInline = false;
285 for (User *U : Caller->users()) {
286 CallSite CS2(U);
287
288 // If this isn't a call to Caller (it could be some other sort
289 // of reference) skip it. Such references will prevent the caller
290 // from being removed.
291 if (!CS2 || CS2.getCalledFunction() != Caller) {
292 callerWillBeRemoved = false;
293 continue;
294 }
295
296 InlineCost IC2 = getInlineCost(CS2);
297 ++NumCallerCallersAnalyzed;
298 if (!IC2) {
299 callerWillBeRemoved = false;
300 continue;
301 }
302 if (IC2.isAlways())
303 continue;
304
305 // See if inlining or original callsite would erase the cost delta of
306 // this callsite. We subtract off the penalty for the call instruction,
307 // which we would be deleting.
308 if (IC2.getCostDelta() <= CandidateCost) {
309 inliningPreventsSomeOuterInline = true;
310 TotalSecondaryCost += IC2.getCost();
311 }
312 }
313 // If all outer calls to Caller would get inlined, the cost for the last
314 // one is set very low by getInlineCost, in anticipation that Caller will
315 // be removed entirely. We did not account for this above unless there
316 // is only one caller of Caller.
317 if (callerWillBeRemoved && !Caller->use_empty())
318 TotalSecondaryCost += InlineConstants::LastCallToStaticBonus;
319
320 if (inliningPreventsSomeOuterInline && TotalSecondaryCost < IC.getCost()) {
321 DEBUG(dbgs() << " NOT Inlining: " << *CS.getInstruction() <<
322 " Cost = " << IC.getCost() <<
323 ", outer Cost = " << TotalSecondaryCost << '\n');
324 emitAnalysis(
325 CS, Twine("Not inlining. Cost of inlining " +
326 CS.getCalledFunction()->getName() +
327 " increases the cost of inlining " +
328 CS.getCaller()->getName() + " in other contexts"));
329 return false;
330 }
330
331 int TotalSecondaryCost = 0;
332 if (shouldBeDeferred(Caller, CS, IC, TotalSecondaryCost)) {
333 DEBUG(dbgs() << " NOT Inlining: " << *CS.getInstruction()
334 << " Cost = " << IC.getCost()
335 << ", outer Cost = " << TotalSecondaryCost << '\n');
336 emitAnalysis(CS, Twine("Not inlining. Cost of inlining " +
337 CS.getCalledFunction()->getName() +
338 " increases the cost of inlining " +
339 CS.getCaller()->getName() + " in other contexts"));
340 return false;
331341 }
332342
333343 DEBUG(dbgs() << " Inlining: cost=" << IC.getCost()