1 //===- InlineCost.cpp - Cost analysis for inliner -------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements inline cost analysis.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/Analysis/InlineCost.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/SetVector.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/Analysis/AssumptionCache.h"
20 #include "llvm/Analysis/BlockFrequencyInfo.h"
21 #include "llvm/Analysis/CFG.h"
22 #include "llvm/Analysis/CodeMetrics.h"
23 #include "llvm/Analysis/ConstantFolding.h"
24 #include "llvm/Analysis/InstructionSimplify.h"
25 #include "llvm/Analysis/LoopInfo.h"
26 #include "llvm/Analysis/ProfileSummaryInfo.h"
27 #include "llvm/Analysis/TargetLibraryInfo.h"
28 #include "llvm/Analysis/TargetTransformInfo.h"
29 #include "llvm/Analysis/ValueTracking.h"
30 #include "llvm/Config/llvm-config.h"
31 #include "llvm/IR/AssemblyAnnotationWriter.h"
32 #include "llvm/IR/CallingConv.h"
33 #include "llvm/IR/DataLayout.h"
34 #include "llvm/IR/Dominators.h"
35 #include "llvm/IR/GetElementPtrTypeIterator.h"
36 #include "llvm/IR/GlobalAlias.h"
37 #include "llvm/IR/InstVisitor.h"
38 #include "llvm/IR/IntrinsicInst.h"
39 #include "llvm/IR/Operator.h"
40 #include "llvm/IR/PatternMatch.h"
41 #include "llvm/Support/CommandLine.h"
42 #include "llvm/Support/Debug.h"
43 #include "llvm/Support/FormattedStream.h"
44 #include "llvm/Support/raw_ostream.h"
45 
46 using namespace llvm;
47 
48 #define DEBUG_TYPE "inline-cost"
49 
50 STATISTIC(NumCallsAnalyzed, "Number of call sites analyzed");
51 
52 static cl::opt<int>
53     DefaultThreshold("inlinedefault-threshold", cl::Hidden, cl::init(225),
54                      cl::ZeroOrMore,
55                      cl::desc("Default amount of inlining to perform"));
56 
57 static cl::opt<bool> PrintInstructionComments(
58     "print-instruction-comments", cl::Hidden, cl::init(false),
59     cl::desc("Prints comments for instruction based on inline cost analysis"));
60 
61 static cl::opt<int> InlineThreshold(
62     "inline-threshold", cl::Hidden, cl::init(225), cl::ZeroOrMore,
63     cl::desc("Control the amount of inlining to perform (default = 225)"));
64 
65 static cl::opt<int> HintThreshold(
66     "inlinehint-threshold", cl::Hidden, cl::init(325), cl::ZeroOrMore,
67     cl::desc("Threshold for inlining functions with inline hint"));
68 
69 static cl::opt<int>
70     ColdCallSiteThreshold("inline-cold-callsite-threshold", cl::Hidden,
71                           cl::init(45), cl::ZeroOrMore,
72                           cl::desc("Threshold for inlining cold callsites"));
73 
74 static cl::opt<bool> InlineEnableCostBenefitAnalysis(
75     "inline-enable-cost-benefit-analysis", cl::Hidden, cl::init(false),
76     cl::desc("Enable the cost-benefit analysis for the inliner"));
77 
78 static cl::opt<int> InlineSavingsMultiplier(
79     "inline-savings-multiplier", cl::Hidden, cl::init(8), cl::ZeroOrMore,
80     cl::desc("Multiplier to multiply cycle savings by during inlining"));
81 
82 static cl::opt<int>
83     InlineSizeAllowance("inline-size-allowance", cl::Hidden, cl::init(100),
84                         cl::ZeroOrMore,
85                         cl::desc("The maximum size of a callee that get's "
86                                  "inlined without sufficient cycle savings"));
87 
88 // We introduce this threshold to help performance of instrumentation based
89 // PGO before we actually hook up inliner with analysis passes such as BPI and
90 // BFI.
91 static cl::opt<int> ColdThreshold(
92     "inlinecold-threshold", cl::Hidden, cl::init(45), cl::ZeroOrMore,
93     cl::desc("Threshold for inlining functions with cold attribute"));
94 
95 static cl::opt<int>
96     HotCallSiteThreshold("hot-callsite-threshold", cl::Hidden, cl::init(3000),
97                          cl::ZeroOrMore,
98                          cl::desc("Threshold for hot callsites "));
99 
100 static cl::opt<int> LocallyHotCallSiteThreshold(
101     "locally-hot-callsite-threshold", cl::Hidden, cl::init(525), cl::ZeroOrMore,
102     cl::desc("Threshold for locally hot callsites "));
103 
104 static cl::opt<int> ColdCallSiteRelFreq(
105     "cold-callsite-rel-freq", cl::Hidden, cl::init(2), cl::ZeroOrMore,
106     cl::desc("Maximum block frequency, expressed as a percentage of caller's "
107              "entry frequency, for a callsite to be cold in the absence of "
108              "profile information."));
109 
110 static cl::opt<int> HotCallSiteRelFreq(
111     "hot-callsite-rel-freq", cl::Hidden, cl::init(60), cl::ZeroOrMore,
112     cl::desc("Minimum block frequency, expressed as a multiple of caller's "
113              "entry frequency, for a callsite to be hot in the absence of "
114              "profile information."));
115 
116 static cl::opt<bool> OptComputeFullInlineCost(
117     "inline-cost-full", cl::Hidden, cl::init(false), cl::ZeroOrMore,
118     cl::desc("Compute the full inline cost of a call site even when the cost "
119              "exceeds the threshold."));
120 
121 static cl::opt<bool> InlineCallerSupersetNoBuiltin(
122     "inline-caller-superset-nobuiltin", cl::Hidden, cl::init(true),
123     cl::ZeroOrMore,
124     cl::desc("Allow inlining when caller has a superset of callee's nobuiltin "
125              "attributes."));
126 
127 static cl::opt<bool> DisableGEPConstOperand(
128     "disable-gep-const-evaluation", cl::Hidden, cl::init(false),
129     cl::desc("Disables evaluation of GetElementPtr with constant operands"));
130 
131 namespace {
132 class InlineCostCallAnalyzer;
133 
134 // This struct is used to store information about inline cost of a
135 // particular instruction
136 struct InstructionCostDetail {
137   int CostBefore = 0;
138   int CostAfter = 0;
139   int ThresholdBefore = 0;
140   int ThresholdAfter = 0;
141 
142   int getThresholdDelta() const { return ThresholdAfter - ThresholdBefore; }
143 
144   int getCostDelta() const { return CostAfter - CostBefore; }
145 
146   bool hasThresholdChanged() const { return ThresholdAfter != ThresholdBefore; }
147 };
148 
149 class InlineCostAnnotationWriter : public AssemblyAnnotationWriter {
150 private:
151   InlineCostCallAnalyzer *const ICCA;
152 
153 public:
154   InlineCostAnnotationWriter(InlineCostCallAnalyzer *ICCA) : ICCA(ICCA) {}
155   virtual void emitInstructionAnnot(const Instruction *I,
156                                     formatted_raw_ostream &OS) override;
157 };
158 
159 /// Carry out call site analysis, in order to evaluate inlinability.
160 /// NOTE: the type is currently used as implementation detail of functions such
161 /// as llvm::getInlineCost. Note the function_ref constructor parameters - the
162 /// expectation is that they come from the outer scope, from the wrapper
163 /// functions. If we want to support constructing CallAnalyzer objects where
164 /// lambdas are provided inline at construction, or where the object needs to
165 /// otherwise survive past the scope of the provided functions, we need to
166 /// revisit the argument types.
167 class CallAnalyzer : public InstVisitor<CallAnalyzer, bool> {
168   typedef InstVisitor<CallAnalyzer, bool> Base;
169   friend class InstVisitor<CallAnalyzer, bool>;
170 
171 protected:
172   virtual ~CallAnalyzer() {}
173   /// The TargetTransformInfo available for this compilation.
174   const TargetTransformInfo &TTI;
175 
176   /// Getter for the cache of @llvm.assume intrinsics.
177   function_ref<AssumptionCache &(Function &)> GetAssumptionCache;
178 
179   /// Getter for BlockFrequencyInfo
180   function_ref<BlockFrequencyInfo &(Function &)> GetBFI;
181 
182   /// Profile summary information.
183   ProfileSummaryInfo *PSI;
184 
185   /// The called function.
186   Function &F;
187 
188   // Cache the DataLayout since we use it a lot.
189   const DataLayout &DL;
190 
191   /// The OptimizationRemarkEmitter available for this compilation.
192   OptimizationRemarkEmitter *ORE;
193 
194   /// The candidate callsite being analyzed. Please do not use this to do
195   /// analysis in the caller function; we want the inline cost query to be
196   /// easily cacheable. Instead, use the cover function paramHasAttr.
197   CallBase &CandidateCall;
198 
199   /// Extension points for handling callsite features.
200   // Called before a basic block was analyzed.
201   virtual void onBlockStart(const BasicBlock *BB) {}
202 
203   /// Called after a basic block was analyzed.
204   virtual void onBlockAnalyzed(const BasicBlock *BB) {}
205 
206   /// Called before an instruction was analyzed
207   virtual void onInstructionAnalysisStart(const Instruction *I) {}
208 
209   /// Called after an instruction was analyzed
210   virtual void onInstructionAnalysisFinish(const Instruction *I) {}
211 
212   /// Called at the end of the analysis of the callsite. Return the outcome of
213   /// the analysis, i.e. 'InlineResult(true)' if the inlining may happen, or
214   /// the reason it can't.
215   virtual InlineResult finalizeAnalysis() { return InlineResult::success(); }
216   /// Called when we're about to start processing a basic block, and every time
217   /// we are done processing an instruction. Return true if there is no point in
218   /// continuing the analysis (e.g. we've determined already the call site is
219   /// too expensive to inline)
220   virtual bool shouldStop() { return false; }
221 
222   /// Called before the analysis of the callee body starts (with callsite
223   /// contexts propagated).  It checks callsite-specific information. Return a
224   /// reason analysis can't continue if that's the case, or 'true' if it may
225   /// continue.
226   virtual InlineResult onAnalysisStart() { return InlineResult::success(); }
227   /// Called if the analysis engine decides SROA cannot be done for the given
228   /// alloca.
229   virtual void onDisableSROA(AllocaInst *Arg) {}
230 
231   /// Called the analysis engine determines load elimination won't happen.
232   virtual void onDisableLoadElimination() {}
233 
234   /// Called to account for a call.
235   virtual void onCallPenalty() {}
236 
237   /// Called to account for the expectation the inlining would result in a load
238   /// elimination.
239   virtual void onLoadEliminationOpportunity() {}
240 
241   /// Called to account for the cost of argument setup for the Call in the
242   /// callee's body (not the callsite currently under analysis).
243   virtual void onCallArgumentSetup(const CallBase &Call) {}
244 
245   /// Called to account for a load relative intrinsic.
246   virtual void onLoadRelativeIntrinsic() {}
247 
248   /// Called to account for a lowered call.
249   virtual void onLoweredCall(Function *F, CallBase &Call, bool IsIndirectCall) {
250   }
251 
252   /// Account for a jump table of given size. Return false to stop further
253   /// processing the switch instruction
254   virtual bool onJumpTable(unsigned JumpTableSize) { return true; }
255 
256   /// Account for a case cluster of given size. Return false to stop further
257   /// processing of the instruction.
258   virtual bool onCaseCluster(unsigned NumCaseCluster) { return true; }
259 
260   /// Called at the end of processing a switch instruction, with the given
261   /// number of case clusters.
262   virtual void onFinalizeSwitch(unsigned JumpTableSize,
263                                 unsigned NumCaseCluster) {}
264 
265   /// Called to account for any other instruction not specifically accounted
266   /// for.
267   virtual void onMissedSimplification() {}
268 
269   /// Start accounting potential benefits due to SROA for the given alloca.
270   virtual void onInitializeSROAArg(AllocaInst *Arg) {}
271 
272   /// Account SROA savings for the AllocaInst value.
273   virtual void onAggregateSROAUse(AllocaInst *V) {}
274 
275   bool handleSROA(Value *V, bool DoNotDisable) {
276     // Check for SROA candidates in comparisons.
277     if (auto *SROAArg = getSROAArgForValueOrNull(V)) {
278       if (DoNotDisable) {
279         onAggregateSROAUse(SROAArg);
280         return true;
281       }
282       disableSROAForArg(SROAArg);
283     }
284     return false;
285   }
286 
287   bool IsCallerRecursive = false;
288   bool IsRecursiveCall = false;
289   bool ExposesReturnsTwice = false;
290   bool HasDynamicAlloca = false;
291   bool ContainsNoDuplicateCall = false;
292   bool HasReturn = false;
293   bool HasIndirectBr = false;
294   bool HasUninlineableIntrinsic = false;
295   bool InitsVargArgs = false;
296 
297   /// Number of bytes allocated statically by the callee.
298   uint64_t AllocatedSize = 0;
299   unsigned NumInstructions = 0;
300   unsigned NumVectorInstructions = 0;
301 
302   /// While we walk the potentially-inlined instructions, we build up and
303   /// maintain a mapping of simplified values specific to this callsite. The
304   /// idea is to propagate any special information we have about arguments to
305   /// this call through the inlinable section of the function, and account for
306   /// likely simplifications post-inlining. The most important aspect we track
307   /// is CFG altering simplifications -- when we prove a basic block dead, that
308   /// can cause dramatic shifts in the cost of inlining a function.
309   DenseMap<Value *, Constant *> SimplifiedValues;
310 
311   /// Keep track of the values which map back (through function arguments) to
312   /// allocas on the caller stack which could be simplified through SROA.
313   DenseMap<Value *, AllocaInst *> SROAArgValues;
314 
315   /// Keep track of Allocas for which we believe we may get SROA optimization.
316   DenseSet<AllocaInst *> EnabledSROAAllocas;
317 
318   /// Keep track of values which map to a pointer base and constant offset.
319   DenseMap<Value *, std::pair<Value *, APInt>> ConstantOffsetPtrs;
320 
321   /// Keep track of dead blocks due to the constant arguments.
322   SetVector<BasicBlock *> DeadBlocks;
323 
324   /// The mapping of the blocks to their known unique successors due to the
325   /// constant arguments.
326   DenseMap<BasicBlock *, BasicBlock *> KnownSuccessors;
327 
328   /// Model the elimination of repeated loads that is expected to happen
329   /// whenever we simplify away the stores that would otherwise cause them to be
330   /// loads.
331   bool EnableLoadElimination;
332   SmallPtrSet<Value *, 16> LoadAddrSet;
333 
334   AllocaInst *getSROAArgForValueOrNull(Value *V) const {
335     auto It = SROAArgValues.find(V);
336     if (It == SROAArgValues.end() || EnabledSROAAllocas.count(It->second) == 0)
337       return nullptr;
338     return It->second;
339   }
340 
341   // Custom simplification helper routines.
342   bool isAllocaDerivedArg(Value *V);
343   void disableSROAForArg(AllocaInst *SROAArg);
344   void disableSROA(Value *V);
345   void findDeadBlocks(BasicBlock *CurrBB, BasicBlock *NextBB);
346   void disableLoadElimination();
347   bool isGEPFree(GetElementPtrInst &GEP);
348   bool canFoldInboundsGEP(GetElementPtrInst &I);
349   bool accumulateGEPOffset(GEPOperator &GEP, APInt &Offset);
350   bool simplifyCallSite(Function *F, CallBase &Call);
351   template <typename Callable>
352   bool simplifyInstruction(Instruction &I, Callable Evaluate);
353   ConstantInt *stripAndComputeInBoundsConstantOffsets(Value *&V);
354 
355   /// Return true if the given argument to the function being considered for
356   /// inlining has the given attribute set either at the call site or the
357   /// function declaration.  Primarily used to inspect call site specific
358   /// attributes since these can be more precise than the ones on the callee
359   /// itself.
360   bool paramHasAttr(Argument *A, Attribute::AttrKind Attr);
361 
362   /// Return true if the given value is known non null within the callee if
363   /// inlined through this particular callsite.
364   bool isKnownNonNullInCallee(Value *V);
365 
366   /// Return true if size growth is allowed when inlining the callee at \p Call.
367   bool allowSizeGrowth(CallBase &Call);
368 
369   // Custom analysis routines.
370   InlineResult analyzeBlock(BasicBlock *BB,
371                             SmallPtrSetImpl<const Value *> &EphValues);
372 
373   // Disable several entry points to the visitor so we don't accidentally use
374   // them by declaring but not defining them here.
375   void visit(Module *);
376   void visit(Module &);
377   void visit(Function *);
378   void visit(Function &);
379   void visit(BasicBlock *);
380   void visit(BasicBlock &);
381 
382   // Provide base case for our instruction visit.
383   bool visitInstruction(Instruction &I);
384 
385   // Our visit overrides.
386   bool visitAlloca(AllocaInst &I);
387   bool visitPHI(PHINode &I);
388   bool visitGetElementPtr(GetElementPtrInst &I);
389   bool visitBitCast(BitCastInst &I);
390   bool visitPtrToInt(PtrToIntInst &I);
391   bool visitIntToPtr(IntToPtrInst &I);
392   bool visitCastInst(CastInst &I);
393   bool visitUnaryInstruction(UnaryInstruction &I);
394   bool visitCmpInst(CmpInst &I);
395   bool visitSub(BinaryOperator &I);
396   bool visitBinaryOperator(BinaryOperator &I);
397   bool visitFNeg(UnaryOperator &I);
398   bool visitLoad(LoadInst &I);
399   bool visitStore(StoreInst &I);
400   bool visitExtractValue(ExtractValueInst &I);
401   bool visitInsertValue(InsertValueInst &I);
402   bool visitCallBase(CallBase &Call);
403   bool visitReturnInst(ReturnInst &RI);
404   bool visitBranchInst(BranchInst &BI);
405   bool visitSelectInst(SelectInst &SI);
406   bool visitSwitchInst(SwitchInst &SI);
407   bool visitIndirectBrInst(IndirectBrInst &IBI);
408   bool visitResumeInst(ResumeInst &RI);
409   bool visitCleanupReturnInst(CleanupReturnInst &RI);
410   bool visitCatchReturnInst(CatchReturnInst &RI);
411   bool visitUnreachableInst(UnreachableInst &I);
412 
413 public:
414   CallAnalyzer(
415       Function &Callee, CallBase &Call, const TargetTransformInfo &TTI,
416       function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
417       function_ref<BlockFrequencyInfo &(Function &)> GetBFI = nullptr,
418       ProfileSummaryInfo *PSI = nullptr,
419       OptimizationRemarkEmitter *ORE = nullptr)
420       : TTI(TTI), GetAssumptionCache(GetAssumptionCache), GetBFI(GetBFI),
421         PSI(PSI), F(Callee), DL(F.getParent()->getDataLayout()), ORE(ORE),
422         CandidateCall(Call), EnableLoadElimination(true) {}
423 
424   InlineResult analyze();
425 
426   Optional<Constant*> getSimplifiedValue(Instruction *I) {
427     if (SimplifiedValues.find(I) != SimplifiedValues.end())
428       return SimplifiedValues[I];
429     return None;
430   }
431 
432   // Keep a bunch of stats about the cost savings found so we can print them
433   // out when debugging.
434   unsigned NumConstantArgs = 0;
435   unsigned NumConstantOffsetPtrArgs = 0;
436   unsigned NumAllocaArgs = 0;
437   unsigned NumConstantPtrCmps = 0;
438   unsigned NumConstantPtrDiffs = 0;
439   unsigned NumInstructionsSimplified = 0;
440 
441   void dump();
442 };
443 
444 /// FIXME: if it is necessary to derive from InlineCostCallAnalyzer, note
445 /// the FIXME in onLoweredCall, when instantiating an InlineCostCallAnalyzer
446 class InlineCostCallAnalyzer final : public CallAnalyzer {
447   const int CostUpperBound = INT_MAX - InlineConstants::InstrCost - 1;
448   const bool ComputeFullInlineCost;
449   int LoadEliminationCost = 0;
450   /// Bonus to be applied when percentage of vector instructions in callee is
451   /// high (see more details in updateThreshold).
452   int VectorBonus = 0;
453   /// Bonus to be applied when the callee has only one reachable basic block.
454   int SingleBBBonus = 0;
455 
456   /// Tunable parameters that control the analysis.
457   const InlineParams &Params;
458 
459   // This DenseMap stores the delta change in cost and threshold after
460   // accounting for the given instruction. The map is filled only with the
461   // flag PrintInstructionComments on.
462   DenseMap<const Instruction *, InstructionCostDetail> InstructionCostDetailMap;
463 
464   /// Upper bound for the inlining cost. Bonuses are being applied to account
465   /// for speculative "expected profit" of the inlining decision.
466   int Threshold = 0;
467 
468   /// Attempt to evaluate indirect calls to boost its inline cost.
469   const bool BoostIndirectCalls;
470 
471   /// Ignore the threshold when finalizing analysis.
472   const bool IgnoreThreshold;
473 
474   // True if the cost-benefit-analysis-based inliner is enabled.
475   const bool CostBenefitAnalysisEnabled;
476 
477   /// Inlining cost measured in abstract units, accounts for all the
478   /// instructions expected to be executed for a given function invocation.
479   /// Instructions that are statically proven to be dead based on call-site
480   /// arguments are not counted here.
481   int Cost = 0;
482 
483   // The cumulative cost at the beginning of the basic block being analyzed.  At
484   // the end of analyzing each basic block, "Cost - CostAtBBStart" represents
485   // the size of that basic block.
486   int CostAtBBStart = 0;
487 
488   // The static size of live but cold basic blocks.  This is "static" in the
489   // sense that it's not weighted by profile counts at all.
490   int ColdSize = 0;
491 
492   bool SingleBB = true;
493 
494   unsigned SROACostSavings = 0;
495   unsigned SROACostSavingsLost = 0;
496 
497   /// The mapping of caller Alloca values to their accumulated cost savings. If
498   /// we have to disable SROA for one of the allocas, this tells us how much
499   /// cost must be added.
500   DenseMap<AllocaInst *, int> SROAArgCosts;
501 
502   /// Return true if \p Call is a cold callsite.
503   bool isColdCallSite(CallBase &Call, BlockFrequencyInfo *CallerBFI);
504 
505   /// Update Threshold based on callsite properties such as callee
506   /// attributes and callee hotness for PGO builds. The Callee is explicitly
507   /// passed to support analyzing indirect calls whose target is inferred by
508   /// analysis.
509   void updateThreshold(CallBase &Call, Function &Callee);
510   /// Return a higher threshold if \p Call is a hot callsite.
511   Optional<int> getHotCallSiteThreshold(CallBase &Call,
512                                         BlockFrequencyInfo *CallerBFI);
513 
514   /// Handle a capped 'int' increment for Cost.
515   void addCost(int64_t Inc, int64_t UpperBound = INT_MAX) {
516     assert(UpperBound > 0 && UpperBound <= INT_MAX && "invalid upper bound");
517     Cost = (int)std::min(UpperBound, Cost + Inc);
518   }
519 
520   void onDisableSROA(AllocaInst *Arg) override {
521     auto CostIt = SROAArgCosts.find(Arg);
522     if (CostIt == SROAArgCosts.end())
523       return;
524     addCost(CostIt->second);
525     SROACostSavings -= CostIt->second;
526     SROACostSavingsLost += CostIt->second;
527     SROAArgCosts.erase(CostIt);
528   }
529 
530   void onDisableLoadElimination() override {
531     addCost(LoadEliminationCost);
532     LoadEliminationCost = 0;
533   }
534   void onCallPenalty() override { addCost(InlineConstants::CallPenalty); }
535   void onCallArgumentSetup(const CallBase &Call) override {
536     // Pay the price of the argument setup. We account for the average 1
537     // instruction per call argument setup here.
538     addCost(Call.arg_size() * InlineConstants::InstrCost);
539   }
540   void onLoadRelativeIntrinsic() override {
541     // This is normally lowered to 4 LLVM instructions.
542     addCost(3 * InlineConstants::InstrCost);
543   }
544   void onLoweredCall(Function *F, CallBase &Call,
545                      bool IsIndirectCall) override {
546     // We account for the average 1 instruction per call argument setup here.
547     addCost(Call.arg_size() * InlineConstants::InstrCost);
548 
549     // If we have a constant that we are calling as a function, we can peer
550     // through it and see the function target. This happens not infrequently
551     // during devirtualization and so we want to give it a hefty bonus for
552     // inlining, but cap that bonus in the event that inlining wouldn't pan out.
553     // Pretend to inline the function, with a custom threshold.
554     if (IsIndirectCall && BoostIndirectCalls) {
555       auto IndirectCallParams = Params;
556       IndirectCallParams.DefaultThreshold =
557           InlineConstants::IndirectCallThreshold;
558       /// FIXME: if InlineCostCallAnalyzer is derived from, this may need
559       /// to instantiate the derived class.
560       InlineCostCallAnalyzer CA(*F, Call, IndirectCallParams, TTI,
561                                 GetAssumptionCache, GetBFI, PSI, ORE, false);
562       if (CA.analyze().isSuccess()) {
563         // We were able to inline the indirect call! Subtract the cost from the
564         // threshold to get the bonus we want to apply, but don't go below zero.
565         Cost -= std::max(0, CA.getThreshold() - CA.getCost());
566       }
567     } else
568       // Otherwise simply add the cost for merely making the call.
569       addCost(InlineConstants::CallPenalty);
570   }
571 
572   void onFinalizeSwitch(unsigned JumpTableSize,
573                         unsigned NumCaseCluster) override {
574     // If suitable for a jump table, consider the cost for the table size and
575     // branch to destination.
576     // Maximum valid cost increased in this function.
577     if (JumpTableSize) {
578       int64_t JTCost = (int64_t)JumpTableSize * InlineConstants::InstrCost +
579                        4 * InlineConstants::InstrCost;
580 
581       addCost(JTCost, (int64_t)CostUpperBound);
582       return;
583     }
584     // Considering forming a binary search, we should find the number of nodes
585     // which is same as the number of comparisons when lowered. For a given
586     // number of clusters, n, we can define a recursive function, f(n), to find
587     // the number of nodes in the tree. The recursion is :
588     // f(n) = 1 + f(n/2) + f (n - n/2), when n > 3,
589     // and f(n) = n, when n <= 3.
590     // This will lead a binary tree where the leaf should be either f(2) or f(3)
591     // when n > 3.  So, the number of comparisons from leaves should be n, while
592     // the number of non-leaf should be :
593     //   2^(log2(n) - 1) - 1
594     //   = 2^log2(n) * 2^-1 - 1
595     //   = n / 2 - 1.
596     // Considering comparisons from leaf and non-leaf nodes, we can estimate the
597     // number of comparisons in a simple closed form :
598     //   n + n / 2 - 1 = n * 3 / 2 - 1
599     if (NumCaseCluster <= 3) {
600       // Suppose a comparison includes one compare and one conditional branch.
601       addCost(NumCaseCluster * 2 * InlineConstants::InstrCost);
602       return;
603     }
604 
605     int64_t ExpectedNumberOfCompare = 3 * (int64_t)NumCaseCluster / 2 - 1;
606     int64_t SwitchCost =
607         ExpectedNumberOfCompare * 2 * InlineConstants::InstrCost;
608 
609     addCost(SwitchCost, (int64_t)CostUpperBound);
610   }
611   void onMissedSimplification() override {
612     addCost(InlineConstants::InstrCost);
613   }
614 
615   void onInitializeSROAArg(AllocaInst *Arg) override {
616     assert(Arg != nullptr &&
617            "Should not initialize SROA costs for null value.");
618     SROAArgCosts[Arg] = 0;
619   }
620 
621   void onAggregateSROAUse(AllocaInst *SROAArg) override {
622     auto CostIt = SROAArgCosts.find(SROAArg);
623     assert(CostIt != SROAArgCosts.end() &&
624            "expected this argument to have a cost");
625     CostIt->second += InlineConstants::InstrCost;
626     SROACostSavings += InlineConstants::InstrCost;
627   }
628 
629   void onBlockStart(const BasicBlock *BB) override { CostAtBBStart = Cost; }
630 
631   void onBlockAnalyzed(const BasicBlock *BB) override {
632     if (CostBenefitAnalysisEnabled) {
633       // Keep track of the static size of live but cold basic blocks.  For now,
634       // we define a cold basic block to be one that's never executed.
635       assert(GetBFI && "GetBFI must be available");
636       BlockFrequencyInfo *BFI = &(GetBFI(F));
637       assert(BFI && "BFI must be available");
638       auto ProfileCount = BFI->getBlockProfileCount(BB);
639       assert(ProfileCount.hasValue());
640       if (ProfileCount.getValue() == 0)
641         ColdSize += Cost - CostAtBBStart;
642     }
643 
644     auto *TI = BB->getTerminator();
645     // If we had any successors at this point, than post-inlining is likely to
646     // have them as well. Note that we assume any basic blocks which existed
647     // due to branches or switches which folded above will also fold after
648     // inlining.
649     if (SingleBB && TI->getNumSuccessors() > 1) {
650       // Take off the bonus we applied to the threshold.
651       Threshold -= SingleBBBonus;
652       SingleBB = false;
653     }
654   }
655 
656   void onInstructionAnalysisStart(const Instruction *I) override {
657     // This function is called to store the initial cost of inlining before
658     // the given instruction was assessed.
659     if (!PrintInstructionComments)
660       return;
661     InstructionCostDetailMap[I].CostBefore = Cost;
662     InstructionCostDetailMap[I].ThresholdBefore = Threshold;
663   }
664 
665   void onInstructionAnalysisFinish(const Instruction *I) override {
666     // This function is called to find new values of cost and threshold after
667     // the instruction has been assessed.
668     if (!PrintInstructionComments)
669       return;
670     InstructionCostDetailMap[I].CostAfter = Cost;
671     InstructionCostDetailMap[I].ThresholdAfter = Threshold;
672   }
673 
674   bool isCostBenefitAnalysisEnabled() {
675     if (!InlineEnableCostBenefitAnalysis)
676       return false;
677 
678     if (!PSI || !PSI->hasProfileSummary())
679       return false;
680 
681     if (!GetBFI)
682       return false;
683 
684     auto *Caller = CandidateCall.getParent()->getParent();
685     if (!Caller->getEntryCount())
686       return false;
687 
688     BlockFrequencyInfo *CallerBFI = &(GetBFI(*Caller));
689     if (!CallerBFI)
690       return false;
691 
692     // For now, limit to hot call site.
693     if (!PSI->isHotCallSite(CandidateCall, CallerBFI))
694       return false;
695 
696     if (!F.getEntryCount())
697       return false;
698 
699     BlockFrequencyInfo *CalleeBFI = &(GetBFI(F));
700     if (!CalleeBFI)
701       return false;
702 
703     return true;
704   }
705 
706   // Determine whether we should inline the given call site, taking into account
707   // both the size cost and the cycle savings.  Return None if we don't have
708   // suficient profiling information to determine.
709   Optional<bool> costBenefitAnalysis() {
710     if (!CostBenefitAnalysisEnabled)
711       return None;
712 
713     // buildInlinerPipeline in the pass builder sets HotCallSiteThreshold to 0
714     // for the prelink phase of the AutoFDO + ThinLTO build.  Honor the logic by
715     // falling back to the cost-based metric.
716     // TODO: Improve this hacky condition.
717     if (Threshold == 0)
718       return None;
719 
720     assert(GetBFI);
721     BlockFrequencyInfo *CalleeBFI = &(GetBFI(F));
722     assert(CalleeBFI);
723 
724     // The cycle savings expressed as the sum of InlineConstants::InstrCost
725     // multiplied by the estimated dynamic count of each instruction we can
726     // avoid.  Savings come from the call site cost, such as argument setup and
727     // the call instruction, as well as the instructions that are folded.
728     //
729     // We use 128-bit APInt here to avoid potential overflow.  This variable
730     // should stay well below 10^^24 (or 2^^80) in practice.  This "worst" case
731     // assumes that we can avoid or fold a billion instructions, each with a
732     // profile count of 10^^15 -- roughly the number of cycles for a 24-hour
733     // period on a 4GHz machine.
734     APInt CycleSavings(128, 0);
735 
736     for (auto &BB : F) {
737       APInt CurrentSavings(128, 0);
738       for (auto &I : BB) {
739         if (BranchInst *BI = dyn_cast<BranchInst>(&I)) {
740           // Count a conditional branch as savings if it becomes unconditional.
741           if (BI->isConditional() &&
742               dyn_cast_or_null<ConstantInt>(
743                   SimplifiedValues.lookup(BI->getCondition()))) {
744             CurrentSavings += InlineConstants::InstrCost;
745           }
746         } else if (Value *V = dyn_cast<Value>(&I)) {
747           // Count an instruction as savings if we can fold it.
748           if (SimplifiedValues.count(V)) {
749             CurrentSavings += InlineConstants::InstrCost;
750           }
751         }
752         // TODO: Consider other forms of savings like switch statements,
753         // indirect calls becoming direct, SROACostSavings, LoadEliminationCost,
754         // etc.
755       }
756 
757       auto ProfileCount = CalleeBFI->getBlockProfileCount(&BB);
758       assert(ProfileCount.hasValue());
759       CurrentSavings *= ProfileCount.getValue();
760       CycleSavings += CurrentSavings;
761     }
762 
763     // Compute the cycle savings per call.
764     auto EntryProfileCount = F.getEntryCount();
765     assert(EntryProfileCount.hasValue());
766     auto EntryCount = EntryProfileCount.getCount();
767     CycleSavings += EntryCount / 2;
768     CycleSavings = CycleSavings.udiv(EntryCount);
769 
770     // Compute the total savings for the call site.
771     auto *CallerBB = CandidateCall.getParent();
772     BlockFrequencyInfo *CallerBFI = &(GetBFI(*(CallerBB->getParent())));
773     CycleSavings += getCallsiteCost(this->CandidateCall, DL);
774     CycleSavings *= CallerBFI->getBlockProfileCount(CallerBB).getValue();
775 
776     // Remove the cost of the cold basic blocks.
777     int Size = Cost - ColdSize;
778 
779     // Allow tiny callees to be inlined regardless of whether they meet the
780     // savings threshold.
781     Size = Size > InlineSizeAllowance ? Size - InlineSizeAllowance : 1;
782 
783     // Return true if the savings justify the cost of inlining.  Specifically,
784     // we evaluate the following inequality:
785     //
786     //  CycleSavings      PSI->getOrCompHotCountThreshold()
787     // -------------- >= -----------------------------------
788     //       Size              InlineSavingsMultiplier
789     //
790     // Note that the left hand side is specific to a call site.  The right hand
791     // side is a constant for the entire executable.
792     APInt LHS = CycleSavings;
793     LHS *= InlineSavingsMultiplier;
794     APInt RHS(128, PSI->getOrCompHotCountThreshold());
795     RHS *= Size;
796     return LHS.uge(RHS);
797   }
798 
799   InlineResult finalizeAnalysis() override {
800     // Loops generally act a lot like calls in that they act like barriers to
801     // movement, require a certain amount of setup, etc. So when optimising for
802     // size, we penalise any call sites that perform loops. We do this after all
803     // other costs here, so will likely only be dealing with relatively small
804     // functions (and hence DT and LI will hopefully be cheap).
805     auto *Caller = CandidateCall.getFunction();
806     if (Caller->hasMinSize()) {
807       DominatorTree DT(F);
808       LoopInfo LI(DT);
809       int NumLoops = 0;
810       for (Loop *L : LI) {
811         // Ignore loops that will not be executed
812         if (DeadBlocks.count(L->getHeader()))
813           continue;
814         NumLoops++;
815       }
816       addCost(NumLoops * InlineConstants::CallPenalty);
817     }
818 
819     // We applied the maximum possible vector bonus at the beginning. Now,
820     // subtract the excess bonus, if any, from the Threshold before
821     // comparing against Cost.
822     if (NumVectorInstructions <= NumInstructions / 10)
823       Threshold -= VectorBonus;
824     else if (NumVectorInstructions <= NumInstructions / 2)
825       Threshold -= VectorBonus / 2;
826 
827     if (auto Result = costBenefitAnalysis()) {
828       if (Result.getValue())
829         return InlineResult::success();
830       else
831         return InlineResult::failure("Cost over threshold.");
832     }
833 
834     if (IgnoreThreshold || Cost < std::max(1, Threshold))
835       return InlineResult::success();
836     return InlineResult::failure("Cost over threshold.");
837   }
838   bool shouldStop() override {
839     // Bail out the moment we cross the threshold. This means we'll under-count
840     // the cost, but only when undercounting doesn't matter.
841     return !IgnoreThreshold && Cost >= Threshold && !ComputeFullInlineCost;
842   }
843 
844   void onLoadEliminationOpportunity() override {
845     LoadEliminationCost += InlineConstants::InstrCost;
846   }
847 
848   InlineResult onAnalysisStart() override {
849     // Perform some tweaks to the cost and threshold based on the direct
850     // callsite information.
851 
852     // We want to more aggressively inline vector-dense kernels, so up the
853     // threshold, and we'll lower it if the % of vector instructions gets too
854     // low. Note that these bonuses are some what arbitrary and evolved over
855     // time by accident as much as because they are principled bonuses.
856     //
857     // FIXME: It would be nice to remove all such bonuses. At least it would be
858     // nice to base the bonus values on something more scientific.
859     assert(NumInstructions == 0);
860     assert(NumVectorInstructions == 0);
861 
862     // Update the threshold based on callsite properties
863     updateThreshold(CandidateCall, F);
864 
865     // While Threshold depends on commandline options that can take negative
866     // values, we want to enforce the invariant that the computed threshold and
867     // bonuses are non-negative.
868     assert(Threshold >= 0);
869     assert(SingleBBBonus >= 0);
870     assert(VectorBonus >= 0);
871 
872     // Speculatively apply all possible bonuses to Threshold. If cost exceeds
873     // this Threshold any time, and cost cannot decrease, we can stop processing
874     // the rest of the function body.
875     Threshold += (SingleBBBonus + VectorBonus);
876 
877     // Give out bonuses for the callsite, as the instructions setting them up
878     // will be gone after inlining.
879     addCost(-getCallsiteCost(this->CandidateCall, DL));
880 
881     // If this function uses the coldcc calling convention, prefer not to inline
882     // it.
883     if (F.getCallingConv() == CallingConv::Cold)
884       Cost += InlineConstants::ColdccPenalty;
885 
886     // Check if we're done. This can happen due to bonuses and penalties.
887     if (Cost >= Threshold && !ComputeFullInlineCost)
888       return InlineResult::failure("high cost");
889 
890     return InlineResult::success();
891   }
892 
893 public:
894   InlineCostCallAnalyzer(
895       Function &Callee, CallBase &Call, const InlineParams &Params,
896       const TargetTransformInfo &TTI,
897       function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
898       function_ref<BlockFrequencyInfo &(Function &)> GetBFI = nullptr,
899       ProfileSummaryInfo *PSI = nullptr,
900       OptimizationRemarkEmitter *ORE = nullptr, bool BoostIndirect = true,
901       bool IgnoreThreshold = false)
902       : CallAnalyzer(Callee, Call, TTI, GetAssumptionCache, GetBFI, PSI, ORE),
903         ComputeFullInlineCost(OptComputeFullInlineCost ||
904                               Params.ComputeFullInlineCost || ORE ||
905                               isCostBenefitAnalysisEnabled()),
906         Params(Params), Threshold(Params.DefaultThreshold),
907         BoostIndirectCalls(BoostIndirect), IgnoreThreshold(IgnoreThreshold),
908         CostBenefitAnalysisEnabled(isCostBenefitAnalysisEnabled()),
909         Writer(this) {}
910 
911   /// Annotation Writer for instruction details
912   InlineCostAnnotationWriter Writer;
913 
914   void dump();
915 
916   // Prints the same analysis as dump(), but its definition is not dependent
917   // on the build.
918   void print();
919 
920   Optional<InstructionCostDetail> getCostDetails(const Instruction *I) {
921     if (InstructionCostDetailMap.find(I) != InstructionCostDetailMap.end())
922       return InstructionCostDetailMap[I];
923     return None;
924   }
925 
926   virtual ~InlineCostCallAnalyzer() {}
927   int getThreshold() { return Threshold; }
928   int getCost() { return Cost; }
929 };
930 } // namespace
931 
932 /// Test whether the given value is an Alloca-derived function argument.
933 bool CallAnalyzer::isAllocaDerivedArg(Value *V) {
934   return SROAArgValues.count(V);
935 }
936 
937 void CallAnalyzer::disableSROAForArg(AllocaInst *SROAArg) {
938   onDisableSROA(SROAArg);
939   EnabledSROAAllocas.erase(SROAArg);
940   disableLoadElimination();
941 }
942 
943 void InlineCostAnnotationWriter::emitInstructionAnnot(const Instruction *I,
944                                                 formatted_raw_ostream &OS) {
945   // The cost of inlining of the given instruction is printed always.
946   // The threshold delta is printed only when it is non-zero. It happens
947   // when we decided to give a bonus at a particular instruction.
948   Optional<InstructionCostDetail> Record = ICCA->getCostDetails(I);
949   if (!Record)
950     OS << "; No analysis for the instruction";
951   else {
952     OS << "; cost before = " << Record->CostBefore
953        << ", cost after = " << Record->CostAfter
954        << ", threshold before = " << Record->ThresholdBefore
955        << ", threshold after = " << Record->ThresholdAfter << ", ";
956     OS << "cost delta = " << Record->getCostDelta();
957     if (Record->hasThresholdChanged())
958       OS << ", threshold delta = " << Record->getThresholdDelta();
959   }
960   auto C = ICCA->getSimplifiedValue(const_cast<Instruction *>(I));
961   if (C) {
962     OS << ", simplified to ";
963     C.getValue()->print(OS, true);
964   }
965   OS << "\n";
966 }
967 
968 /// If 'V' maps to a SROA candidate, disable SROA for it.
969 void CallAnalyzer::disableSROA(Value *V) {
970   if (auto *SROAArg = getSROAArgForValueOrNull(V)) {
971     disableSROAForArg(SROAArg);
972   }
973 }
974 
975 void CallAnalyzer::disableLoadElimination() {
976   if (EnableLoadElimination) {
977     onDisableLoadElimination();
978     EnableLoadElimination = false;
979   }
980 }
981 
982 /// Accumulate a constant GEP offset into an APInt if possible.
983 ///
984 /// Returns false if unable to compute the offset for any reason. Respects any
985 /// simplified values known during the analysis of this callsite.
986 bool CallAnalyzer::accumulateGEPOffset(GEPOperator &GEP, APInt &Offset) {
987   unsigned IntPtrWidth = DL.getIndexTypeSizeInBits(GEP.getType());
988   assert(IntPtrWidth == Offset.getBitWidth());
989 
990   for (gep_type_iterator GTI = gep_type_begin(GEP), GTE = gep_type_end(GEP);
991        GTI != GTE; ++GTI) {
992     ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand());
993     if (!OpC)
994       if (Constant *SimpleOp = SimplifiedValues.lookup(GTI.getOperand()))
995         OpC = dyn_cast<ConstantInt>(SimpleOp);
996     if (!OpC)
997       return false;
998     if (OpC->isZero())
999       continue;
1000 
1001     // Handle a struct index, which adds its field offset to the pointer.
1002     if (StructType *STy = GTI.getStructTypeOrNull()) {
1003       unsigned ElementIdx = OpC->getZExtValue();
1004       const StructLayout *SL = DL.getStructLayout(STy);
1005       Offset += APInt(IntPtrWidth, SL->getElementOffset(ElementIdx));
1006       continue;
1007     }
1008 
1009     APInt TypeSize(IntPtrWidth, DL.getTypeAllocSize(GTI.getIndexedType()));
1010     Offset += OpC->getValue().sextOrTrunc(IntPtrWidth) * TypeSize;
1011   }
1012   return true;
1013 }
1014 
1015 /// Use TTI to check whether a GEP is free.
1016 ///
1017 /// Respects any simplified values known during the analysis of this callsite.
1018 bool CallAnalyzer::isGEPFree(GetElementPtrInst &GEP) {
1019   SmallVector<Value *, 4> Operands;
1020   Operands.push_back(GEP.getOperand(0));
1021   for (const Use &Op : GEP.indices())
1022     if (Constant *SimpleOp = SimplifiedValues.lookup(Op))
1023       Operands.push_back(SimpleOp);
1024     else
1025       Operands.push_back(Op);
1026   return TargetTransformInfo::TCC_Free ==
1027          TTI.getUserCost(&GEP, Operands,
1028                          TargetTransformInfo::TCK_SizeAndLatency);
1029 }
1030 
1031 bool CallAnalyzer::visitAlloca(AllocaInst &I) {
1032   // Check whether inlining will turn a dynamic alloca into a static
1033   // alloca and handle that case.
1034   if (I.isArrayAllocation()) {
1035     Constant *Size = SimplifiedValues.lookup(I.getArraySize());
1036     if (auto *AllocSize = dyn_cast_or_null<ConstantInt>(Size)) {
1037       // Sometimes a dynamic alloca could be converted into a static alloca
1038       // after this constant prop, and become a huge static alloca on an
1039       // unconditional CFG path. Avoid inlining if this is going to happen above
1040       // a threshold.
1041       // FIXME: If the threshold is removed or lowered too much, we could end up
1042       // being too pessimistic and prevent inlining non-problematic code. This
1043       // could result in unintended perf regressions. A better overall strategy
1044       // is needed to track stack usage during inlining.
1045       Type *Ty = I.getAllocatedType();
1046       AllocatedSize = SaturatingMultiplyAdd(
1047           AllocSize->getLimitedValue(), DL.getTypeAllocSize(Ty).getKnownMinSize(),
1048           AllocatedSize);
1049       if (AllocatedSize > InlineConstants::MaxSimplifiedDynamicAllocaToInline) {
1050         HasDynamicAlloca = true;
1051         return false;
1052       }
1053       return Base::visitAlloca(I);
1054     }
1055   }
1056 
1057   // Accumulate the allocated size.
1058   if (I.isStaticAlloca()) {
1059     Type *Ty = I.getAllocatedType();
1060     AllocatedSize =
1061         SaturatingAdd(DL.getTypeAllocSize(Ty).getKnownMinSize(), AllocatedSize);
1062   }
1063 
1064   // We will happily inline static alloca instructions.
1065   if (I.isStaticAlloca())
1066     return Base::visitAlloca(I);
1067 
1068   // FIXME: This is overly conservative. Dynamic allocas are inefficient for
1069   // a variety of reasons, and so we would like to not inline them into
1070   // functions which don't currently have a dynamic alloca. This simply
1071   // disables inlining altogether in the presence of a dynamic alloca.
1072   HasDynamicAlloca = true;
1073   return false;
1074 }
1075 
1076 bool CallAnalyzer::visitPHI(PHINode &I) {
1077   // FIXME: We need to propagate SROA *disabling* through phi nodes, even
1078   // though we don't want to propagate it's bonuses. The idea is to disable
1079   // SROA if it *might* be used in an inappropriate manner.
1080 
1081   // Phi nodes are always zero-cost.
1082   // FIXME: Pointer sizes may differ between different address spaces, so do we
1083   // need to use correct address space in the call to getPointerSizeInBits here?
1084   // Or could we skip the getPointerSizeInBits call completely? As far as I can
1085   // see the ZeroOffset is used as a dummy value, so we can probably use any
1086   // bit width for the ZeroOffset?
1087   APInt ZeroOffset = APInt::getNullValue(DL.getPointerSizeInBits(0));
1088   bool CheckSROA = I.getType()->isPointerTy();
1089 
1090   // Track the constant or pointer with constant offset we've seen so far.
1091   Constant *FirstC = nullptr;
1092   std::pair<Value *, APInt> FirstBaseAndOffset = {nullptr, ZeroOffset};
1093   Value *FirstV = nullptr;
1094 
1095   for (unsigned i = 0, e = I.getNumIncomingValues(); i != e; ++i) {
1096     BasicBlock *Pred = I.getIncomingBlock(i);
1097     // If the incoming block is dead, skip the incoming block.
1098     if (DeadBlocks.count(Pred))
1099       continue;
1100     // If the parent block of phi is not the known successor of the incoming
1101     // block, skip the incoming block.
1102     BasicBlock *KnownSuccessor = KnownSuccessors[Pred];
1103     if (KnownSuccessor && KnownSuccessor != I.getParent())
1104       continue;
1105 
1106     Value *V = I.getIncomingValue(i);
1107     // If the incoming value is this phi itself, skip the incoming value.
1108     if (&I == V)
1109       continue;
1110 
1111     Constant *C = dyn_cast<Constant>(V);
1112     if (!C)
1113       C = SimplifiedValues.lookup(V);
1114 
1115     std::pair<Value *, APInt> BaseAndOffset = {nullptr, ZeroOffset};
1116     if (!C && CheckSROA)
1117       BaseAndOffset = ConstantOffsetPtrs.lookup(V);
1118 
1119     if (!C && !BaseAndOffset.first)
1120       // The incoming value is neither a constant nor a pointer with constant
1121       // offset, exit early.
1122       return true;
1123 
1124     if (FirstC) {
1125       if (FirstC == C)
1126         // If we've seen a constant incoming value before and it is the same
1127         // constant we see this time, continue checking the next incoming value.
1128         continue;
1129       // Otherwise early exit because we either see a different constant or saw
1130       // a constant before but we have a pointer with constant offset this time.
1131       return true;
1132     }
1133 
1134     if (FirstV) {
1135       // The same logic as above, but check pointer with constant offset here.
1136       if (FirstBaseAndOffset == BaseAndOffset)
1137         continue;
1138       return true;
1139     }
1140 
1141     if (C) {
1142       // This is the 1st time we've seen a constant, record it.
1143       FirstC = C;
1144       continue;
1145     }
1146 
1147     // The remaining case is that this is the 1st time we've seen a pointer with
1148     // constant offset, record it.
1149     FirstV = V;
1150     FirstBaseAndOffset = BaseAndOffset;
1151   }
1152 
1153   // Check if we can map phi to a constant.
1154   if (FirstC) {
1155     SimplifiedValues[&I] = FirstC;
1156     return true;
1157   }
1158 
1159   // Check if we can map phi to a pointer with constant offset.
1160   if (FirstBaseAndOffset.first) {
1161     ConstantOffsetPtrs[&I] = FirstBaseAndOffset;
1162 
1163     if (auto *SROAArg = getSROAArgForValueOrNull(FirstV))
1164       SROAArgValues[&I] = SROAArg;
1165   }
1166 
1167   return true;
1168 }
1169 
1170 /// Check we can fold GEPs of constant-offset call site argument pointers.
1171 /// This requires target data and inbounds GEPs.
1172 ///
1173 /// \return true if the specified GEP can be folded.
1174 bool CallAnalyzer::canFoldInboundsGEP(GetElementPtrInst &I) {
1175   // Check if we have a base + offset for the pointer.
1176   std::pair<Value *, APInt> BaseAndOffset =
1177       ConstantOffsetPtrs.lookup(I.getPointerOperand());
1178   if (!BaseAndOffset.first)
1179     return false;
1180 
1181   // Check if the offset of this GEP is constant, and if so accumulate it
1182   // into Offset.
1183   if (!accumulateGEPOffset(cast<GEPOperator>(I), BaseAndOffset.second))
1184     return false;
1185 
1186   // Add the result as a new mapping to Base + Offset.
1187   ConstantOffsetPtrs[&I] = BaseAndOffset;
1188 
1189   return true;
1190 }
1191 
1192 bool CallAnalyzer::visitGetElementPtr(GetElementPtrInst &I) {
1193   auto *SROAArg = getSROAArgForValueOrNull(I.getPointerOperand());
1194 
1195   // Lambda to check whether a GEP's indices are all constant.
1196   auto IsGEPOffsetConstant = [&](GetElementPtrInst &GEP) {
1197     for (const Use &Op : GEP.indices())
1198       if (!isa<Constant>(Op) && !SimplifiedValues.lookup(Op))
1199         return false;
1200     return true;
1201   };
1202 
1203   if (!DisableGEPConstOperand)
1204     if (simplifyInstruction(I, [&](SmallVectorImpl<Constant *> &COps) {
1205         SmallVector<Constant *, 2> Indices;
1206         for (unsigned int Index = 1 ; Index < COps.size() ; ++Index)
1207             Indices.push_back(COps[Index]);
1208         return ConstantExpr::getGetElementPtr(I.getSourceElementType(), COps[0],
1209                                               Indices, I.isInBounds());
1210         }))
1211       return true;
1212 
1213   if ((I.isInBounds() && canFoldInboundsGEP(I)) || IsGEPOffsetConstant(I)) {
1214     if (SROAArg)
1215       SROAArgValues[&I] = SROAArg;
1216 
1217     // Constant GEPs are modeled as free.
1218     return true;
1219   }
1220 
1221   // Variable GEPs will require math and will disable SROA.
1222   if (SROAArg)
1223     disableSROAForArg(SROAArg);
1224   return isGEPFree(I);
1225 }
1226 
1227 /// Simplify \p I if its operands are constants and update SimplifiedValues.
1228 /// \p Evaluate is a callable specific to instruction type that evaluates the
1229 /// instruction when all the operands are constants.
1230 template <typename Callable>
1231 bool CallAnalyzer::simplifyInstruction(Instruction &I, Callable Evaluate) {
1232   SmallVector<Constant *, 2> COps;
1233   for (Value *Op : I.operands()) {
1234     Constant *COp = dyn_cast<Constant>(Op);
1235     if (!COp)
1236       COp = SimplifiedValues.lookup(Op);
1237     if (!COp)
1238       return false;
1239     COps.push_back(COp);
1240   }
1241   auto *C = Evaluate(COps);
1242   if (!C)
1243     return false;
1244   SimplifiedValues[&I] = C;
1245   return true;
1246 }
1247 
1248 bool CallAnalyzer::visitBitCast(BitCastInst &I) {
1249   // Propagate constants through bitcasts.
1250   if (simplifyInstruction(I, [&](SmallVectorImpl<Constant *> &COps) {
1251         return ConstantExpr::getBitCast(COps[0], I.getType());
1252       }))
1253     return true;
1254 
1255   // Track base/offsets through casts
1256   std::pair<Value *, APInt> BaseAndOffset =
1257       ConstantOffsetPtrs.lookup(I.getOperand(0));
1258   // Casts don't change the offset, just wrap it up.
1259   if (BaseAndOffset.first)
1260     ConstantOffsetPtrs[&I] = BaseAndOffset;
1261 
1262   // Also look for SROA candidates here.
1263   if (auto *SROAArg = getSROAArgForValueOrNull(I.getOperand(0)))
1264     SROAArgValues[&I] = SROAArg;
1265 
1266   // Bitcasts are always zero cost.
1267   return true;
1268 }
1269 
1270 bool CallAnalyzer::visitPtrToInt(PtrToIntInst &I) {
1271   // Propagate constants through ptrtoint.
1272   if (simplifyInstruction(I, [&](SmallVectorImpl<Constant *> &COps) {
1273         return ConstantExpr::getPtrToInt(COps[0], I.getType());
1274       }))
1275     return true;
1276 
1277   // Track base/offset pairs when converted to a plain integer provided the
1278   // integer is large enough to represent the pointer.
1279   unsigned IntegerSize = I.getType()->getScalarSizeInBits();
1280   unsigned AS = I.getOperand(0)->getType()->getPointerAddressSpace();
1281   if (IntegerSize == DL.getPointerSizeInBits(AS)) {
1282     std::pair<Value *, APInt> BaseAndOffset =
1283         ConstantOffsetPtrs.lookup(I.getOperand(0));
1284     if (BaseAndOffset.first)
1285       ConstantOffsetPtrs[&I] = BaseAndOffset;
1286   }
1287 
1288   // This is really weird. Technically, ptrtoint will disable SROA. However,
1289   // unless that ptrtoint is *used* somewhere in the live basic blocks after
1290   // inlining, it will be nuked, and SROA should proceed. All of the uses which
1291   // would block SROA would also block SROA if applied directly to a pointer,
1292   // and so we can just add the integer in here. The only places where SROA is
1293   // preserved either cannot fire on an integer, or won't in-and-of themselves
1294   // disable SROA (ext) w/o some later use that we would see and disable.
1295   if (auto *SROAArg = getSROAArgForValueOrNull(I.getOperand(0)))
1296     SROAArgValues[&I] = SROAArg;
1297 
1298   return TargetTransformInfo::TCC_Free ==
1299          TTI.getUserCost(&I, TargetTransformInfo::TCK_SizeAndLatency);
1300 }
1301 
1302 bool CallAnalyzer::visitIntToPtr(IntToPtrInst &I) {
1303   // Propagate constants through ptrtoint.
1304   if (simplifyInstruction(I, [&](SmallVectorImpl<Constant *> &COps) {
1305         return ConstantExpr::getIntToPtr(COps[0], I.getType());
1306       }))
1307     return true;
1308 
1309   // Track base/offset pairs when round-tripped through a pointer without
1310   // modifications provided the integer is not too large.
1311   Value *Op = I.getOperand(0);
1312   unsigned IntegerSize = Op->getType()->getScalarSizeInBits();
1313   if (IntegerSize <= DL.getPointerTypeSizeInBits(I.getType())) {
1314     std::pair<Value *, APInt> BaseAndOffset = ConstantOffsetPtrs.lookup(Op);
1315     if (BaseAndOffset.first)
1316       ConstantOffsetPtrs[&I] = BaseAndOffset;
1317   }
1318 
1319   // "Propagate" SROA here in the same manner as we do for ptrtoint above.
1320   if (auto *SROAArg = getSROAArgForValueOrNull(Op))
1321     SROAArgValues[&I] = SROAArg;
1322 
1323   return TargetTransformInfo::TCC_Free ==
1324          TTI.getUserCost(&I, TargetTransformInfo::TCK_SizeAndLatency);
1325 }
1326 
1327 bool CallAnalyzer::visitCastInst(CastInst &I) {
1328   // Propagate constants through casts.
1329   if (simplifyInstruction(I, [&](SmallVectorImpl<Constant *> &COps) {
1330         return ConstantExpr::getCast(I.getOpcode(), COps[0], I.getType());
1331       }))
1332     return true;
1333 
1334   // Disable SROA in the face of arbitrary casts we don't explicitly list
1335   // elsewhere.
1336   disableSROA(I.getOperand(0));
1337 
1338   // If this is a floating-point cast, and the target says this operation
1339   // is expensive, this may eventually become a library call. Treat the cost
1340   // as such.
1341   switch (I.getOpcode()) {
1342   case Instruction::FPTrunc:
1343   case Instruction::FPExt:
1344   case Instruction::UIToFP:
1345   case Instruction::SIToFP:
1346   case Instruction::FPToUI:
1347   case Instruction::FPToSI:
1348     if (TTI.getFPOpCost(I.getType()) == TargetTransformInfo::TCC_Expensive)
1349       onCallPenalty();
1350     break;
1351   default:
1352     break;
1353   }
1354 
1355   return TargetTransformInfo::TCC_Free ==
1356          TTI.getUserCost(&I, TargetTransformInfo::TCK_SizeAndLatency);
1357 }
1358 
1359 bool CallAnalyzer::visitUnaryInstruction(UnaryInstruction &I) {
1360   Value *Operand = I.getOperand(0);
1361   if (simplifyInstruction(I, [&](SmallVectorImpl<Constant *> &COps) {
1362         return ConstantFoldInstOperands(&I, COps[0], DL);
1363       }))
1364     return true;
1365 
1366   // Disable any SROA on the argument to arbitrary unary instructions.
1367   disableSROA(Operand);
1368 
1369   return false;
1370 }
1371 
1372 bool CallAnalyzer::paramHasAttr(Argument *A, Attribute::AttrKind Attr) {
1373   return CandidateCall.paramHasAttr(A->getArgNo(), Attr);
1374 }
1375 
1376 bool CallAnalyzer::isKnownNonNullInCallee(Value *V) {
1377   // Does the *call site* have the NonNull attribute set on an argument?  We
1378   // use the attribute on the call site to memoize any analysis done in the
1379   // caller. This will also trip if the callee function has a non-null
1380   // parameter attribute, but that's a less interesting case because hopefully
1381   // the callee would already have been simplified based on that.
1382   if (Argument *A = dyn_cast<Argument>(V))
1383     if (paramHasAttr(A, Attribute::NonNull))
1384       return true;
1385 
1386   // Is this an alloca in the caller?  This is distinct from the attribute case
1387   // above because attributes aren't updated within the inliner itself and we
1388   // always want to catch the alloca derived case.
1389   if (isAllocaDerivedArg(V))
1390     // We can actually predict the result of comparisons between an
1391     // alloca-derived value and null. Note that this fires regardless of
1392     // SROA firing.
1393     return true;
1394 
1395   return false;
1396 }
1397 
1398 bool CallAnalyzer::allowSizeGrowth(CallBase &Call) {
1399   // If the normal destination of the invoke or the parent block of the call
1400   // site is unreachable-terminated, there is little point in inlining this
1401   // unless there is literally zero cost.
1402   // FIXME: Note that it is possible that an unreachable-terminated block has a
1403   // hot entry. For example, in below scenario inlining hot_call_X() may be
1404   // beneficial :
1405   // main() {
1406   //   hot_call_1();
1407   //   ...
1408   //   hot_call_N()
1409   //   exit(0);
1410   // }
1411   // For now, we are not handling this corner case here as it is rare in real
1412   // code. In future, we should elaborate this based on BPI and BFI in more
1413   // general threshold adjusting heuristics in updateThreshold().
1414   if (InvokeInst *II = dyn_cast<InvokeInst>(&Call)) {
1415     if (isa<UnreachableInst>(II->getNormalDest()->getTerminator()))
1416       return false;
1417   } else if (isa<UnreachableInst>(Call.getParent()->getTerminator()))
1418     return false;
1419 
1420   return true;
1421 }
1422 
1423 bool InlineCostCallAnalyzer::isColdCallSite(CallBase &Call,
1424                                             BlockFrequencyInfo *CallerBFI) {
1425   // If global profile summary is available, then callsite's coldness is
1426   // determined based on that.
1427   if (PSI && PSI->hasProfileSummary())
1428     return PSI->isColdCallSite(Call, CallerBFI);
1429 
1430   // Otherwise we need BFI to be available.
1431   if (!CallerBFI)
1432     return false;
1433 
1434   // Determine if the callsite is cold relative to caller's entry. We could
1435   // potentially cache the computation of scaled entry frequency, but the added
1436   // complexity is not worth it unless this scaling shows up high in the
1437   // profiles.
1438   const BranchProbability ColdProb(ColdCallSiteRelFreq, 100);
1439   auto CallSiteBB = Call.getParent();
1440   auto CallSiteFreq = CallerBFI->getBlockFreq(CallSiteBB);
1441   auto CallerEntryFreq =
1442       CallerBFI->getBlockFreq(&(Call.getCaller()->getEntryBlock()));
1443   return CallSiteFreq < CallerEntryFreq * ColdProb;
1444 }
1445 
1446 Optional<int>
1447 InlineCostCallAnalyzer::getHotCallSiteThreshold(CallBase &Call,
1448                                                 BlockFrequencyInfo *CallerBFI) {
1449 
1450   // If global profile summary is available, then callsite's hotness is
1451   // determined based on that.
1452   if (PSI && PSI->hasProfileSummary() && PSI->isHotCallSite(Call, CallerBFI))
1453     return Params.HotCallSiteThreshold;
1454 
1455   // Otherwise we need BFI to be available and to have a locally hot callsite
1456   // threshold.
1457   if (!CallerBFI || !Params.LocallyHotCallSiteThreshold)
1458     return None;
1459 
1460   // Determine if the callsite is hot relative to caller's entry. We could
1461   // potentially cache the computation of scaled entry frequency, but the added
1462   // complexity is not worth it unless this scaling shows up high in the
1463   // profiles.
1464   auto CallSiteBB = Call.getParent();
1465   auto CallSiteFreq = CallerBFI->getBlockFreq(CallSiteBB).getFrequency();
1466   auto CallerEntryFreq = CallerBFI->getEntryFreq();
1467   if (CallSiteFreq >= CallerEntryFreq * HotCallSiteRelFreq)
1468     return Params.LocallyHotCallSiteThreshold;
1469 
1470   // Otherwise treat it normally.
1471   return None;
1472 }
1473 
1474 void InlineCostCallAnalyzer::updateThreshold(CallBase &Call, Function &Callee) {
1475   // If no size growth is allowed for this inlining, set Threshold to 0.
1476   if (!allowSizeGrowth(Call)) {
1477     Threshold = 0;
1478     return;
1479   }
1480 
1481   Function *Caller = Call.getCaller();
1482 
1483   // return min(A, B) if B is valid.
1484   auto MinIfValid = [](int A, Optional<int> B) {
1485     return B ? std::min(A, B.getValue()) : A;
1486   };
1487 
1488   // return max(A, B) if B is valid.
1489   auto MaxIfValid = [](int A, Optional<int> B) {
1490     return B ? std::max(A, B.getValue()) : A;
1491   };
1492 
1493   // Various bonus percentages. These are multiplied by Threshold to get the
1494   // bonus values.
1495   // SingleBBBonus: This bonus is applied if the callee has a single reachable
1496   // basic block at the given callsite context. This is speculatively applied
1497   // and withdrawn if more than one basic block is seen.
1498   //
1499   // LstCallToStaticBonus: This large bonus is applied to ensure the inlining
1500   // of the last call to a static function as inlining such functions is
1501   // guaranteed to reduce code size.
1502   //
1503   // These bonus percentages may be set to 0 based on properties of the caller
1504   // and the callsite.
1505   int SingleBBBonusPercent = 50;
1506   int VectorBonusPercent = TTI.getInlinerVectorBonusPercent();
1507   int LastCallToStaticBonus = InlineConstants::LastCallToStaticBonus;
1508 
1509   // Lambda to set all the above bonus and bonus percentages to 0.
1510   auto DisallowAllBonuses = [&]() {
1511     SingleBBBonusPercent = 0;
1512     VectorBonusPercent = 0;
1513     LastCallToStaticBonus = 0;
1514   };
1515 
1516   // Use the OptMinSizeThreshold or OptSizeThreshold knob if they are available
1517   // and reduce the threshold if the caller has the necessary attribute.
1518   if (Caller->hasMinSize()) {
1519     Threshold = MinIfValid(Threshold, Params.OptMinSizeThreshold);
1520     // For minsize, we want to disable the single BB bonus and the vector
1521     // bonuses, but not the last-call-to-static bonus. Inlining the last call to
1522     // a static function will, at the minimum, eliminate the parameter setup and
1523     // call/return instructions.
1524     SingleBBBonusPercent = 0;
1525     VectorBonusPercent = 0;
1526   } else if (Caller->hasOptSize())
1527     Threshold = MinIfValid(Threshold, Params.OptSizeThreshold);
1528 
1529   // Adjust the threshold based on inlinehint attribute and profile based
1530   // hotness information if the caller does not have MinSize attribute.
1531   if (!Caller->hasMinSize()) {
1532     if (Callee.hasFnAttribute(Attribute::InlineHint))
1533       Threshold = MaxIfValid(Threshold, Params.HintThreshold);
1534 
1535     // FIXME: After switching to the new passmanager, simplify the logic below
1536     // by checking only the callsite hotness/coldness as we will reliably
1537     // have local profile information.
1538     //
1539     // Callsite hotness and coldness can be determined if sample profile is
1540     // used (which adds hotness metadata to calls) or if caller's
1541     // BlockFrequencyInfo is available.
1542     BlockFrequencyInfo *CallerBFI = GetBFI ? &(GetBFI(*Caller)) : nullptr;
1543     auto HotCallSiteThreshold = getHotCallSiteThreshold(Call, CallerBFI);
1544     if (!Caller->hasOptSize() && HotCallSiteThreshold) {
1545       LLVM_DEBUG(dbgs() << "Hot callsite.\n");
1546       // FIXME: This should update the threshold only if it exceeds the
1547       // current threshold, but AutoFDO + ThinLTO currently relies on this
1548       // behavior to prevent inlining of hot callsites during ThinLTO
1549       // compile phase.
1550       Threshold = HotCallSiteThreshold.getValue();
1551     } else if (isColdCallSite(Call, CallerBFI)) {
1552       LLVM_DEBUG(dbgs() << "Cold callsite.\n");
1553       // Do not apply bonuses for a cold callsite including the
1554       // LastCallToStatic bonus. While this bonus might result in code size
1555       // reduction, it can cause the size of a non-cold caller to increase
1556       // preventing it from being inlined.
1557       DisallowAllBonuses();
1558       Threshold = MinIfValid(Threshold, Params.ColdCallSiteThreshold);
1559     } else if (PSI) {
1560       // Use callee's global profile information only if we have no way of
1561       // determining this via callsite information.
1562       if (PSI->isFunctionEntryHot(&Callee)) {
1563         LLVM_DEBUG(dbgs() << "Hot callee.\n");
1564         // If callsite hotness can not be determined, we may still know
1565         // that the callee is hot and treat it as a weaker hint for threshold
1566         // increase.
1567         Threshold = MaxIfValid(Threshold, Params.HintThreshold);
1568       } else if (PSI->isFunctionEntryCold(&Callee)) {
1569         LLVM_DEBUG(dbgs() << "Cold callee.\n");
1570         // Do not apply bonuses for a cold callee including the
1571         // LastCallToStatic bonus. While this bonus might result in code size
1572         // reduction, it can cause the size of a non-cold caller to increase
1573         // preventing it from being inlined.
1574         DisallowAllBonuses();
1575         Threshold = MinIfValid(Threshold, Params.ColdThreshold);
1576       }
1577     }
1578   }
1579 
1580   // Finally, take the target-specific inlining threshold multiplier into
1581   // account.
1582   Threshold *= TTI.getInliningThresholdMultiplier();
1583 
1584   SingleBBBonus = Threshold * SingleBBBonusPercent / 100;
1585   VectorBonus = Threshold * VectorBonusPercent / 100;
1586 
1587   bool OnlyOneCallAndLocalLinkage =
1588       F.hasLocalLinkage() && F.hasOneUse() && &F == Call.getCalledFunction();
1589   // If there is only one call of the function, and it has internal linkage,
1590   // the cost of inlining it drops dramatically. It may seem odd to update
1591   // Cost in updateThreshold, but the bonus depends on the logic in this method.
1592   if (OnlyOneCallAndLocalLinkage)
1593     Cost -= LastCallToStaticBonus;
1594 }
1595 
1596 bool CallAnalyzer::visitCmpInst(CmpInst &I) {
1597   Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
1598   // First try to handle simplified comparisons.
1599   if (simplifyInstruction(I, [&](SmallVectorImpl<Constant *> &COps) {
1600         return ConstantExpr::getCompare(I.getPredicate(), COps[0], COps[1]);
1601       }))
1602     return true;
1603 
1604   if (I.getOpcode() == Instruction::FCmp)
1605     return false;
1606 
1607   // Otherwise look for a comparison between constant offset pointers with
1608   // a common base.
1609   Value *LHSBase, *RHSBase;
1610   APInt LHSOffset, RHSOffset;
1611   std::tie(LHSBase, LHSOffset) = ConstantOffsetPtrs.lookup(LHS);
1612   if (LHSBase) {
1613     std::tie(RHSBase, RHSOffset) = ConstantOffsetPtrs.lookup(RHS);
1614     if (RHSBase && LHSBase == RHSBase) {
1615       // We have common bases, fold the icmp to a constant based on the
1616       // offsets.
1617       Constant *CLHS = ConstantInt::get(LHS->getContext(), LHSOffset);
1618       Constant *CRHS = ConstantInt::get(RHS->getContext(), RHSOffset);
1619       if (Constant *C = ConstantExpr::getICmp(I.getPredicate(), CLHS, CRHS)) {
1620         SimplifiedValues[&I] = C;
1621         ++NumConstantPtrCmps;
1622         return true;
1623       }
1624     }
1625   }
1626 
1627   // If the comparison is an equality comparison with null, we can simplify it
1628   // if we know the value (argument) can't be null
1629   if (I.isEquality() && isa<ConstantPointerNull>(I.getOperand(1)) &&
1630       isKnownNonNullInCallee(I.getOperand(0))) {
1631     bool IsNotEqual = I.getPredicate() == CmpInst::ICMP_NE;
1632     SimplifiedValues[&I] = IsNotEqual ? ConstantInt::getTrue(I.getType())
1633                                       : ConstantInt::getFalse(I.getType());
1634     return true;
1635   }
1636   return handleSROA(I.getOperand(0), isa<ConstantPointerNull>(I.getOperand(1)));
1637 }
1638 
1639 bool CallAnalyzer::visitSub(BinaryOperator &I) {
1640   // Try to handle a special case: we can fold computing the difference of two
1641   // constant-related pointers.
1642   Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
1643   Value *LHSBase, *RHSBase;
1644   APInt LHSOffset, RHSOffset;
1645   std::tie(LHSBase, LHSOffset) = ConstantOffsetPtrs.lookup(LHS);
1646   if (LHSBase) {
1647     std::tie(RHSBase, RHSOffset) = ConstantOffsetPtrs.lookup(RHS);
1648     if (RHSBase && LHSBase == RHSBase) {
1649       // We have common bases, fold the subtract to a constant based on the
1650       // offsets.
1651       Constant *CLHS = ConstantInt::get(LHS->getContext(), LHSOffset);
1652       Constant *CRHS = ConstantInt::get(RHS->getContext(), RHSOffset);
1653       if (Constant *C = ConstantExpr::getSub(CLHS, CRHS)) {
1654         SimplifiedValues[&I] = C;
1655         ++NumConstantPtrDiffs;
1656         return true;
1657       }
1658     }
1659   }
1660 
1661   // Otherwise, fall back to the generic logic for simplifying and handling
1662   // instructions.
1663   return Base::visitSub(I);
1664 }
1665 
1666 bool CallAnalyzer::visitBinaryOperator(BinaryOperator &I) {
1667   Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
1668   Constant *CLHS = dyn_cast<Constant>(LHS);
1669   if (!CLHS)
1670     CLHS = SimplifiedValues.lookup(LHS);
1671   Constant *CRHS = dyn_cast<Constant>(RHS);
1672   if (!CRHS)
1673     CRHS = SimplifiedValues.lookup(RHS);
1674 
1675   Value *SimpleV = nullptr;
1676   if (auto FI = dyn_cast<FPMathOperator>(&I))
1677     SimpleV = SimplifyBinOp(I.getOpcode(), CLHS ? CLHS : LHS, CRHS ? CRHS : RHS,
1678                             FI->getFastMathFlags(), DL);
1679   else
1680     SimpleV =
1681         SimplifyBinOp(I.getOpcode(), CLHS ? CLHS : LHS, CRHS ? CRHS : RHS, DL);
1682 
1683   if (Constant *C = dyn_cast_or_null<Constant>(SimpleV))
1684     SimplifiedValues[&I] = C;
1685 
1686   if (SimpleV)
1687     return true;
1688 
1689   // Disable any SROA on arguments to arbitrary, unsimplified binary operators.
1690   disableSROA(LHS);
1691   disableSROA(RHS);
1692 
1693   // If the instruction is floating point, and the target says this operation
1694   // is expensive, this may eventually become a library call. Treat the cost
1695   // as such. Unless it's fneg which can be implemented with an xor.
1696   using namespace llvm::PatternMatch;
1697   if (I.getType()->isFloatingPointTy() &&
1698       TTI.getFPOpCost(I.getType()) == TargetTransformInfo::TCC_Expensive &&
1699       !match(&I, m_FNeg(m_Value())))
1700     onCallPenalty();
1701 
1702   return false;
1703 }
1704 
1705 bool CallAnalyzer::visitFNeg(UnaryOperator &I) {
1706   Value *Op = I.getOperand(0);
1707   Constant *COp = dyn_cast<Constant>(Op);
1708   if (!COp)
1709     COp = SimplifiedValues.lookup(Op);
1710 
1711   Value *SimpleV = SimplifyFNegInst(
1712       COp ? COp : Op, cast<FPMathOperator>(I).getFastMathFlags(), DL);
1713 
1714   if (Constant *C = dyn_cast_or_null<Constant>(SimpleV))
1715     SimplifiedValues[&I] = C;
1716 
1717   if (SimpleV)
1718     return true;
1719 
1720   // Disable any SROA on arguments to arbitrary, unsimplified fneg.
1721   disableSROA(Op);
1722 
1723   return false;
1724 }
1725 
1726 bool CallAnalyzer::visitLoad(LoadInst &I) {
1727   if (handleSROA(I.getPointerOperand(), I.isSimple()))
1728     return true;
1729 
1730   // If the data is already loaded from this address and hasn't been clobbered
1731   // by any stores or calls, this load is likely to be redundant and can be
1732   // eliminated.
1733   if (EnableLoadElimination &&
1734       !LoadAddrSet.insert(I.getPointerOperand()).second && I.isUnordered()) {
1735     onLoadEliminationOpportunity();
1736     return true;
1737   }
1738 
1739   return false;
1740 }
1741 
1742 bool CallAnalyzer::visitStore(StoreInst &I) {
1743   if (handleSROA(I.getPointerOperand(), I.isSimple()))
1744     return true;
1745 
1746   // The store can potentially clobber loads and prevent repeated loads from
1747   // being eliminated.
1748   // FIXME:
1749   // 1. We can probably keep an initial set of eliminatable loads substracted
1750   // from the cost even when we finally see a store. We just need to disable
1751   // *further* accumulation of elimination savings.
1752   // 2. We should probably at some point thread MemorySSA for the callee into
1753   // this and then use that to actually compute *really* precise savings.
1754   disableLoadElimination();
1755   return false;
1756 }
1757 
1758 bool CallAnalyzer::visitExtractValue(ExtractValueInst &I) {
1759   // Constant folding for extract value is trivial.
1760   if (simplifyInstruction(I, [&](SmallVectorImpl<Constant *> &COps) {
1761         return ConstantExpr::getExtractValue(COps[0], I.getIndices());
1762       }))
1763     return true;
1764 
1765   // SROA can look through these but give them a cost.
1766   return false;
1767 }
1768 
1769 bool CallAnalyzer::visitInsertValue(InsertValueInst &I) {
1770   // Constant folding for insert value is trivial.
1771   if (simplifyInstruction(I, [&](SmallVectorImpl<Constant *> &COps) {
1772         return ConstantExpr::getInsertValue(/*AggregateOperand*/ COps[0],
1773                                             /*InsertedValueOperand*/ COps[1],
1774                                             I.getIndices());
1775       }))
1776     return true;
1777 
1778   // SROA can look through these but give them a cost.
1779   return false;
1780 }
1781 
1782 /// Try to simplify a call site.
1783 ///
1784 /// Takes a concrete function and callsite and tries to actually simplify it by
1785 /// analyzing the arguments and call itself with instsimplify. Returns true if
1786 /// it has simplified the callsite to some other entity (a constant), making it
1787 /// free.
1788 bool CallAnalyzer::simplifyCallSite(Function *F, CallBase &Call) {
1789   // FIXME: Using the instsimplify logic directly for this is inefficient
1790   // because we have to continually rebuild the argument list even when no
1791   // simplifications can be performed. Until that is fixed with remapping
1792   // inside of instsimplify, directly constant fold calls here.
1793   if (!canConstantFoldCallTo(&Call, F))
1794     return false;
1795 
1796   // Try to re-map the arguments to constants.
1797   SmallVector<Constant *, 4> ConstantArgs;
1798   ConstantArgs.reserve(Call.arg_size());
1799   for (Value *I : Call.args()) {
1800     Constant *C = dyn_cast<Constant>(I);
1801     if (!C)
1802       C = dyn_cast_or_null<Constant>(SimplifiedValues.lookup(I));
1803     if (!C)
1804       return false; // This argument doesn't map to a constant.
1805 
1806     ConstantArgs.push_back(C);
1807   }
1808   if (Constant *C = ConstantFoldCall(&Call, F, ConstantArgs)) {
1809     SimplifiedValues[&Call] = C;
1810     return true;
1811   }
1812 
1813   return false;
1814 }
1815 
1816 bool CallAnalyzer::visitCallBase(CallBase &Call) {
1817   if (Call.hasFnAttr(Attribute::ReturnsTwice) &&
1818       !F.hasFnAttribute(Attribute::ReturnsTwice)) {
1819     // This aborts the entire analysis.
1820     ExposesReturnsTwice = true;
1821     return false;
1822   }
1823   if (isa<CallInst>(Call) && cast<CallInst>(Call).cannotDuplicate())
1824     ContainsNoDuplicateCall = true;
1825 
1826   Value *Callee = Call.getCalledOperand();
1827   Function *F = dyn_cast_or_null<Function>(Callee);
1828   bool IsIndirectCall = !F;
1829   if (IsIndirectCall) {
1830     // Check if this happens to be an indirect function call to a known function
1831     // in this inline context. If not, we've done all we can.
1832     F = dyn_cast_or_null<Function>(SimplifiedValues.lookup(Callee));
1833     if (!F) {
1834       onCallArgumentSetup(Call);
1835 
1836       if (!Call.onlyReadsMemory())
1837         disableLoadElimination();
1838       return Base::visitCallBase(Call);
1839     }
1840   }
1841 
1842   assert(F && "Expected a call to a known function");
1843 
1844   // When we have a concrete function, first try to simplify it directly.
1845   if (simplifyCallSite(F, Call))
1846     return true;
1847 
1848   // Next check if it is an intrinsic we know about.
1849   // FIXME: Lift this into part of the InstVisitor.
1850   if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(&Call)) {
1851     switch (II->getIntrinsicID()) {
1852     default:
1853       if (!Call.onlyReadsMemory() && !isAssumeLikeIntrinsic(II))
1854         disableLoadElimination();
1855       return Base::visitCallBase(Call);
1856 
1857     case Intrinsic::load_relative:
1858       onLoadRelativeIntrinsic();
1859       return false;
1860 
1861     case Intrinsic::memset:
1862     case Intrinsic::memcpy:
1863     case Intrinsic::memmove:
1864       disableLoadElimination();
1865       // SROA can usually chew through these intrinsics, but they aren't free.
1866       return false;
1867     case Intrinsic::icall_branch_funnel:
1868     case Intrinsic::localescape:
1869       HasUninlineableIntrinsic = true;
1870       return false;
1871     case Intrinsic::vastart:
1872       InitsVargArgs = true;
1873       return false;
1874     }
1875   }
1876 
1877   if (F == Call.getFunction()) {
1878     // This flag will fully abort the analysis, so don't bother with anything
1879     // else.
1880     IsRecursiveCall = true;
1881     return false;
1882   }
1883 
1884   if (TTI.isLoweredToCall(F)) {
1885     onLoweredCall(F, Call, IsIndirectCall);
1886   }
1887 
1888   if (!(Call.onlyReadsMemory() || (IsIndirectCall && F->onlyReadsMemory())))
1889     disableLoadElimination();
1890   return Base::visitCallBase(Call);
1891 }
1892 
1893 bool CallAnalyzer::visitReturnInst(ReturnInst &RI) {
1894   // At least one return instruction will be free after inlining.
1895   bool Free = !HasReturn;
1896   HasReturn = true;
1897   return Free;
1898 }
1899 
1900 bool CallAnalyzer::visitBranchInst(BranchInst &BI) {
1901   // We model unconditional branches as essentially free -- they really
1902   // shouldn't exist at all, but handling them makes the behavior of the
1903   // inliner more regular and predictable. Interestingly, conditional branches
1904   // which will fold away are also free.
1905   return BI.isUnconditional() || isa<ConstantInt>(BI.getCondition()) ||
1906          dyn_cast_or_null<ConstantInt>(
1907              SimplifiedValues.lookup(BI.getCondition()));
1908 }
1909 
1910 bool CallAnalyzer::visitSelectInst(SelectInst &SI) {
1911   bool CheckSROA = SI.getType()->isPointerTy();
1912   Value *TrueVal = SI.getTrueValue();
1913   Value *FalseVal = SI.getFalseValue();
1914 
1915   Constant *TrueC = dyn_cast<Constant>(TrueVal);
1916   if (!TrueC)
1917     TrueC = SimplifiedValues.lookup(TrueVal);
1918   Constant *FalseC = dyn_cast<Constant>(FalseVal);
1919   if (!FalseC)
1920     FalseC = SimplifiedValues.lookup(FalseVal);
1921   Constant *CondC =
1922       dyn_cast_or_null<Constant>(SimplifiedValues.lookup(SI.getCondition()));
1923 
1924   if (!CondC) {
1925     // Select C, X, X => X
1926     if (TrueC == FalseC && TrueC) {
1927       SimplifiedValues[&SI] = TrueC;
1928       return true;
1929     }
1930 
1931     if (!CheckSROA)
1932       return Base::visitSelectInst(SI);
1933 
1934     std::pair<Value *, APInt> TrueBaseAndOffset =
1935         ConstantOffsetPtrs.lookup(TrueVal);
1936     std::pair<Value *, APInt> FalseBaseAndOffset =
1937         ConstantOffsetPtrs.lookup(FalseVal);
1938     if (TrueBaseAndOffset == FalseBaseAndOffset && TrueBaseAndOffset.first) {
1939       ConstantOffsetPtrs[&SI] = TrueBaseAndOffset;
1940 
1941       if (auto *SROAArg = getSROAArgForValueOrNull(TrueVal))
1942         SROAArgValues[&SI] = SROAArg;
1943       return true;
1944     }
1945 
1946     return Base::visitSelectInst(SI);
1947   }
1948 
1949   // Select condition is a constant.
1950   Value *SelectedV = CondC->isAllOnesValue()
1951                          ? TrueVal
1952                          : (CondC->isNullValue()) ? FalseVal : nullptr;
1953   if (!SelectedV) {
1954     // Condition is a vector constant that is not all 1s or all 0s.  If all
1955     // operands are constants, ConstantExpr::getSelect() can handle the cases
1956     // such as select vectors.
1957     if (TrueC && FalseC) {
1958       if (auto *C = ConstantExpr::getSelect(CondC, TrueC, FalseC)) {
1959         SimplifiedValues[&SI] = C;
1960         return true;
1961       }
1962     }
1963     return Base::visitSelectInst(SI);
1964   }
1965 
1966   // Condition is either all 1s or all 0s. SI can be simplified.
1967   if (Constant *SelectedC = dyn_cast<Constant>(SelectedV)) {
1968     SimplifiedValues[&SI] = SelectedC;
1969     return true;
1970   }
1971 
1972   if (!CheckSROA)
1973     return true;
1974 
1975   std::pair<Value *, APInt> BaseAndOffset =
1976       ConstantOffsetPtrs.lookup(SelectedV);
1977   if (BaseAndOffset.first) {
1978     ConstantOffsetPtrs[&SI] = BaseAndOffset;
1979 
1980     if (auto *SROAArg = getSROAArgForValueOrNull(SelectedV))
1981       SROAArgValues[&SI] = SROAArg;
1982   }
1983 
1984   return true;
1985 }
1986 
1987 bool CallAnalyzer::visitSwitchInst(SwitchInst &SI) {
1988   // We model unconditional switches as free, see the comments on handling
1989   // branches.
1990   if (isa<ConstantInt>(SI.getCondition()))
1991     return true;
1992   if (Value *V = SimplifiedValues.lookup(SI.getCondition()))
1993     if (isa<ConstantInt>(V))
1994       return true;
1995 
1996   // Assume the most general case where the switch is lowered into
1997   // either a jump table, bit test, or a balanced binary tree consisting of
1998   // case clusters without merging adjacent clusters with the same
1999   // destination. We do not consider the switches that are lowered with a mix
2000   // of jump table/bit test/binary search tree. The cost of the switch is
2001   // proportional to the size of the tree or the size of jump table range.
2002   //
2003   // NB: We convert large switches which are just used to initialize large phi
2004   // nodes to lookup tables instead in simplify-cfg, so this shouldn't prevent
2005   // inlining those. It will prevent inlining in cases where the optimization
2006   // does not (yet) fire.
2007 
2008   unsigned JumpTableSize = 0;
2009   BlockFrequencyInfo *BFI = GetBFI ? &(GetBFI(F)) : nullptr;
2010   unsigned NumCaseCluster =
2011       TTI.getEstimatedNumberOfCaseClusters(SI, JumpTableSize, PSI, BFI);
2012 
2013   onFinalizeSwitch(JumpTableSize, NumCaseCluster);
2014   return false;
2015 }
2016 
2017 bool CallAnalyzer::visitIndirectBrInst(IndirectBrInst &IBI) {
2018   // We never want to inline functions that contain an indirectbr.  This is
2019   // incorrect because all the blockaddress's (in static global initializers
2020   // for example) would be referring to the original function, and this
2021   // indirect jump would jump from the inlined copy of the function into the
2022   // original function which is extremely undefined behavior.
2023   // FIXME: This logic isn't really right; we can safely inline functions with
2024   // indirectbr's as long as no other function or global references the
2025   // blockaddress of a block within the current function.
2026   HasIndirectBr = true;
2027   return false;
2028 }
2029 
2030 bool CallAnalyzer::visitResumeInst(ResumeInst &RI) {
2031   // FIXME: It's not clear that a single instruction is an accurate model for
2032   // the inline cost of a resume instruction.
2033   return false;
2034 }
2035 
2036 bool CallAnalyzer::visitCleanupReturnInst(CleanupReturnInst &CRI) {
2037   // FIXME: It's not clear that a single instruction is an accurate model for
2038   // the inline cost of a cleanupret instruction.
2039   return false;
2040 }
2041 
2042 bool CallAnalyzer::visitCatchReturnInst(CatchReturnInst &CRI) {
2043   // FIXME: It's not clear that a single instruction is an accurate model for
2044   // the inline cost of a catchret instruction.
2045   return false;
2046 }
2047 
2048 bool CallAnalyzer::visitUnreachableInst(UnreachableInst &I) {
2049   // FIXME: It might be reasonably to discount the cost of instructions leading
2050   // to unreachable as they have the lowest possible impact on both runtime and
2051   // code size.
2052   return true; // No actual code is needed for unreachable.
2053 }
2054 
2055 bool CallAnalyzer::visitInstruction(Instruction &I) {
2056   // Some instructions are free. All of the free intrinsics can also be
2057   // handled by SROA, etc.
2058   if (TargetTransformInfo::TCC_Free ==
2059       TTI.getUserCost(&I, TargetTransformInfo::TCK_SizeAndLatency))
2060     return true;
2061 
2062   // We found something we don't understand or can't handle. Mark any SROA-able
2063   // values in the operand list as no longer viable.
2064   for (const Use &Op : I.operands())
2065     disableSROA(Op);
2066 
2067   return false;
2068 }
2069 
2070 /// Analyze a basic block for its contribution to the inline cost.
2071 ///
2072 /// This method walks the analyzer over every instruction in the given basic
2073 /// block and accounts for their cost during inlining at this callsite. It
2074 /// aborts early if the threshold has been exceeded or an impossible to inline
2075 /// construct has been detected. It returns false if inlining is no longer
2076 /// viable, and true if inlining remains viable.
2077 InlineResult
2078 CallAnalyzer::analyzeBlock(BasicBlock *BB,
2079                            SmallPtrSetImpl<const Value *> &EphValues) {
2080   for (Instruction &I : *BB) {
2081     // FIXME: Currently, the number of instructions in a function regardless of
2082     // our ability to simplify them during inline to constants or dead code,
2083     // are actually used by the vector bonus heuristic. As long as that's true,
2084     // we have to special case debug intrinsics here to prevent differences in
2085     // inlining due to debug symbols. Eventually, the number of unsimplified
2086     // instructions shouldn't factor into the cost computation, but until then,
2087     // hack around it here.
2088     if (isa<DbgInfoIntrinsic>(I))
2089       continue;
2090 
2091     // Skip pseudo-probes.
2092     if (isa<PseudoProbeInst>(I))
2093       continue;
2094 
2095     // Skip ephemeral values.
2096     if (EphValues.count(&I))
2097       continue;
2098 
2099     ++NumInstructions;
2100     if (isa<ExtractElementInst>(I) || I.getType()->isVectorTy())
2101       ++NumVectorInstructions;
2102 
2103     // If the instruction simplified to a constant, there is no cost to this
2104     // instruction. Visit the instructions using our InstVisitor to account for
2105     // all of the per-instruction logic. The visit tree returns true if we
2106     // consumed the instruction in any way, and false if the instruction's base
2107     // cost should count against inlining.
2108     onInstructionAnalysisStart(&I);
2109 
2110     if (Base::visit(&I))
2111       ++NumInstructionsSimplified;
2112     else
2113       onMissedSimplification();
2114 
2115     onInstructionAnalysisFinish(&I);
2116     using namespace ore;
2117     // If the visit this instruction detected an uninlinable pattern, abort.
2118     InlineResult IR = InlineResult::success();
2119     if (IsRecursiveCall)
2120       IR = InlineResult::failure("recursive");
2121     else if (ExposesReturnsTwice)
2122       IR = InlineResult::failure("exposes returns twice");
2123     else if (HasDynamicAlloca)
2124       IR = InlineResult::failure("dynamic alloca");
2125     else if (HasIndirectBr)
2126       IR = InlineResult::failure("indirect branch");
2127     else if (HasUninlineableIntrinsic)
2128       IR = InlineResult::failure("uninlinable intrinsic");
2129     else if (InitsVargArgs)
2130       IR = InlineResult::failure("varargs");
2131     if (!IR.isSuccess()) {
2132       if (ORE)
2133         ORE->emit([&]() {
2134           return OptimizationRemarkMissed(DEBUG_TYPE, "NeverInline",
2135                                           &CandidateCall)
2136                  << NV("Callee", &F) << " has uninlinable pattern ("
2137                  << NV("InlineResult", IR.getFailureReason())
2138                  << ") and cost is not fully computed";
2139         });
2140       return IR;
2141     }
2142 
2143     // If the caller is a recursive function then we don't want to inline
2144     // functions which allocate a lot of stack space because it would increase
2145     // the caller stack usage dramatically.
2146     if (IsCallerRecursive &&
2147         AllocatedSize > InlineConstants::TotalAllocaSizeRecursiveCaller) {
2148       auto IR =
2149           InlineResult::failure("recursive and allocates too much stack space");
2150       if (ORE)
2151         ORE->emit([&]() {
2152           return OptimizationRemarkMissed(DEBUG_TYPE, "NeverInline",
2153                                           &CandidateCall)
2154                  << NV("Callee", &F) << " is "
2155                  << NV("InlineResult", IR.getFailureReason())
2156                  << ". Cost is not fully computed";
2157         });
2158       return IR;
2159     }
2160 
2161     if (shouldStop())
2162       return InlineResult::failure(
2163           "Call site analysis is not favorable to inlining.");
2164   }
2165 
2166   return InlineResult::success();
2167 }
2168 
2169 /// Compute the base pointer and cumulative constant offsets for V.
2170 ///
2171 /// This strips all constant offsets off of V, leaving it the base pointer, and
2172 /// accumulates the total constant offset applied in the returned constant. It
2173 /// returns 0 if V is not a pointer, and returns the constant '0' if there are
2174 /// no constant offsets applied.
2175 ConstantInt *CallAnalyzer::stripAndComputeInBoundsConstantOffsets(Value *&V) {
2176   if (!V->getType()->isPointerTy())
2177     return nullptr;
2178 
2179   unsigned AS = V->getType()->getPointerAddressSpace();
2180   unsigned IntPtrWidth = DL.getIndexSizeInBits(AS);
2181   APInt Offset = APInt::getNullValue(IntPtrWidth);
2182 
2183   // Even though we don't look through PHI nodes, we could be called on an
2184   // instruction in an unreachable block, which may be on a cycle.
2185   SmallPtrSet<Value *, 4> Visited;
2186   Visited.insert(V);
2187   do {
2188     if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
2189       if (!GEP->isInBounds() || !accumulateGEPOffset(*GEP, Offset))
2190         return nullptr;
2191       V = GEP->getPointerOperand();
2192     } else if (Operator::getOpcode(V) == Instruction::BitCast) {
2193       V = cast<Operator>(V)->getOperand(0);
2194     } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
2195       if (GA->isInterposable())
2196         break;
2197       V = GA->getAliasee();
2198     } else {
2199       break;
2200     }
2201     assert(V->getType()->isPointerTy() && "Unexpected operand type!");
2202   } while (Visited.insert(V).second);
2203 
2204   Type *IdxPtrTy = DL.getIndexType(V->getType());
2205   return cast<ConstantInt>(ConstantInt::get(IdxPtrTy, Offset));
2206 }
2207 
2208 /// Find dead blocks due to deleted CFG edges during inlining.
2209 ///
2210 /// If we know the successor of the current block, \p CurrBB, has to be \p
2211 /// NextBB, the other successors of \p CurrBB are dead if these successors have
2212 /// no live incoming CFG edges.  If one block is found to be dead, we can
2213 /// continue growing the dead block list by checking the successors of the dead
2214 /// blocks to see if all their incoming edges are dead or not.
2215 void CallAnalyzer::findDeadBlocks(BasicBlock *CurrBB, BasicBlock *NextBB) {
2216   auto IsEdgeDead = [&](BasicBlock *Pred, BasicBlock *Succ) {
2217     // A CFG edge is dead if the predecessor is dead or the predecessor has a
2218     // known successor which is not the one under exam.
2219     return (DeadBlocks.count(Pred) ||
2220             (KnownSuccessors[Pred] && KnownSuccessors[Pred] != Succ));
2221   };
2222 
2223   auto IsNewlyDead = [&](BasicBlock *BB) {
2224     // If all the edges to a block are dead, the block is also dead.
2225     return (!DeadBlocks.count(BB) &&
2226             llvm::all_of(predecessors(BB),
2227                          [&](BasicBlock *P) { return IsEdgeDead(P, BB); }));
2228   };
2229 
2230   for (BasicBlock *Succ : successors(CurrBB)) {
2231     if (Succ == NextBB || !IsNewlyDead(Succ))
2232       continue;
2233     SmallVector<BasicBlock *, 4> NewDead;
2234     NewDead.push_back(Succ);
2235     while (!NewDead.empty()) {
2236       BasicBlock *Dead = NewDead.pop_back_val();
2237       if (DeadBlocks.insert(Dead))
2238         // Continue growing the dead block lists.
2239         for (BasicBlock *S : successors(Dead))
2240           if (IsNewlyDead(S))
2241             NewDead.push_back(S);
2242     }
2243   }
2244 }
2245 
2246 /// Analyze a call site for potential inlining.
2247 ///
2248 /// Returns true if inlining this call is viable, and false if it is not
2249 /// viable. It computes the cost and adjusts the threshold based on numerous
2250 /// factors and heuristics. If this method returns false but the computed cost
2251 /// is below the computed threshold, then inlining was forcibly disabled by
2252 /// some artifact of the routine.
2253 InlineResult CallAnalyzer::analyze() {
2254   ++NumCallsAnalyzed;
2255 
2256   auto Result = onAnalysisStart();
2257   if (!Result.isSuccess())
2258     return Result;
2259 
2260   if (F.empty())
2261     return InlineResult::success();
2262 
2263   Function *Caller = CandidateCall.getFunction();
2264   // Check if the caller function is recursive itself.
2265   for (User *U : Caller->users()) {
2266     CallBase *Call = dyn_cast<CallBase>(U);
2267     if (Call && Call->getFunction() == Caller) {
2268       IsCallerRecursive = true;
2269       break;
2270     }
2271   }
2272 
2273   // Populate our simplified values by mapping from function arguments to call
2274   // arguments with known important simplifications.
2275   auto CAI = CandidateCall.arg_begin();
2276   for (Argument &FAI : F.args()) {
2277     assert(CAI != CandidateCall.arg_end());
2278     if (Constant *C = dyn_cast<Constant>(CAI))
2279       SimplifiedValues[&FAI] = C;
2280 
2281     Value *PtrArg = *CAI;
2282     if (ConstantInt *C = stripAndComputeInBoundsConstantOffsets(PtrArg)) {
2283       ConstantOffsetPtrs[&FAI] = std::make_pair(PtrArg, C->getValue());
2284 
2285       // We can SROA any pointer arguments derived from alloca instructions.
2286       if (auto *SROAArg = dyn_cast<AllocaInst>(PtrArg)) {
2287         SROAArgValues[&FAI] = SROAArg;
2288         onInitializeSROAArg(SROAArg);
2289         EnabledSROAAllocas.insert(SROAArg);
2290       }
2291     }
2292     ++CAI;
2293   }
2294   NumConstantArgs = SimplifiedValues.size();
2295   NumConstantOffsetPtrArgs = ConstantOffsetPtrs.size();
2296   NumAllocaArgs = SROAArgValues.size();
2297 
2298   // FIXME: If a caller has multiple calls to a callee, we end up recomputing
2299   // the ephemeral values multiple times (and they're completely determined by
2300   // the callee, so this is purely duplicate work).
2301   SmallPtrSet<const Value *, 32> EphValues;
2302   CodeMetrics::collectEphemeralValues(&F, &GetAssumptionCache(F), EphValues);
2303 
2304   // The worklist of live basic blocks in the callee *after* inlining. We avoid
2305   // adding basic blocks of the callee which can be proven to be dead for this
2306   // particular call site in order to get more accurate cost estimates. This
2307   // requires a somewhat heavyweight iteration pattern: we need to walk the
2308   // basic blocks in a breadth-first order as we insert live successors. To
2309   // accomplish this, prioritizing for small iterations because we exit after
2310   // crossing our threshold, we use a small-size optimized SetVector.
2311   typedef SetVector<BasicBlock *, SmallVector<BasicBlock *, 16>,
2312                     SmallPtrSet<BasicBlock *, 16>>
2313       BBSetVector;
2314   BBSetVector BBWorklist;
2315   BBWorklist.insert(&F.getEntryBlock());
2316 
2317   // Note that we *must not* cache the size, this loop grows the worklist.
2318   for (unsigned Idx = 0; Idx != BBWorklist.size(); ++Idx) {
2319     if (shouldStop())
2320       break;
2321 
2322     BasicBlock *BB = BBWorklist[Idx];
2323     if (BB->empty())
2324       continue;
2325 
2326     onBlockStart(BB);
2327 
2328     // Disallow inlining a blockaddress with uses other than strictly callbr.
2329     // A blockaddress only has defined behavior for an indirect branch in the
2330     // same function, and we do not currently support inlining indirect
2331     // branches.  But, the inliner may not see an indirect branch that ends up
2332     // being dead code at a particular call site. If the blockaddress escapes
2333     // the function, e.g., via a global variable, inlining may lead to an
2334     // invalid cross-function reference.
2335     // FIXME: pr/39560: continue relaxing this overt restriction.
2336     if (BB->hasAddressTaken())
2337       for (User *U : BlockAddress::get(&*BB)->users())
2338         if (!isa<CallBrInst>(*U))
2339           return InlineResult::failure("blockaddress used outside of callbr");
2340 
2341     // Analyze the cost of this block. If we blow through the threshold, this
2342     // returns false, and we can bail on out.
2343     InlineResult IR = analyzeBlock(BB, EphValues);
2344     if (!IR.isSuccess())
2345       return IR;
2346 
2347     Instruction *TI = BB->getTerminator();
2348 
2349     // Add in the live successors by first checking whether we have terminator
2350     // that may be simplified based on the values simplified by this call.
2351     if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
2352       if (BI->isConditional()) {
2353         Value *Cond = BI->getCondition();
2354         if (ConstantInt *SimpleCond =
2355                 dyn_cast_or_null<ConstantInt>(SimplifiedValues.lookup(Cond))) {
2356           BasicBlock *NextBB = BI->getSuccessor(SimpleCond->isZero() ? 1 : 0);
2357           BBWorklist.insert(NextBB);
2358           KnownSuccessors[BB] = NextBB;
2359           findDeadBlocks(BB, NextBB);
2360           continue;
2361         }
2362       }
2363     } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
2364       Value *Cond = SI->getCondition();
2365       if (ConstantInt *SimpleCond =
2366               dyn_cast_or_null<ConstantInt>(SimplifiedValues.lookup(Cond))) {
2367         BasicBlock *NextBB = SI->findCaseValue(SimpleCond)->getCaseSuccessor();
2368         BBWorklist.insert(NextBB);
2369         KnownSuccessors[BB] = NextBB;
2370         findDeadBlocks(BB, NextBB);
2371         continue;
2372       }
2373     }
2374 
2375     // If we're unable to select a particular successor, just count all of
2376     // them.
2377     for (unsigned TIdx = 0, TSize = TI->getNumSuccessors(); TIdx != TSize;
2378          ++TIdx)
2379       BBWorklist.insert(TI->getSuccessor(TIdx));
2380 
2381     onBlockAnalyzed(BB);
2382   }
2383 
2384   bool OnlyOneCallAndLocalLinkage = F.hasLocalLinkage() && F.hasOneUse() &&
2385                                     &F == CandidateCall.getCalledFunction();
2386   // If this is a noduplicate call, we can still inline as long as
2387   // inlining this would cause the removal of the caller (so the instruction
2388   // is not actually duplicated, just moved).
2389   if (!OnlyOneCallAndLocalLinkage && ContainsNoDuplicateCall)
2390     return InlineResult::failure("noduplicate");
2391 
2392   return finalizeAnalysis();
2393 }
2394 
2395 void InlineCostCallAnalyzer::print() {
2396 #define DEBUG_PRINT_STAT(x) dbgs() << "      " #x ": " << x << "\n"
2397   if (PrintInstructionComments)
2398     F.print(dbgs(), &Writer);
2399   DEBUG_PRINT_STAT(NumConstantArgs);
2400   DEBUG_PRINT_STAT(NumConstantOffsetPtrArgs);
2401   DEBUG_PRINT_STAT(NumAllocaArgs);
2402   DEBUG_PRINT_STAT(NumConstantPtrCmps);
2403   DEBUG_PRINT_STAT(NumConstantPtrDiffs);
2404   DEBUG_PRINT_STAT(NumInstructionsSimplified);
2405   DEBUG_PRINT_STAT(NumInstructions);
2406   DEBUG_PRINT_STAT(SROACostSavings);
2407   DEBUG_PRINT_STAT(SROACostSavingsLost);
2408   DEBUG_PRINT_STAT(LoadEliminationCost);
2409   DEBUG_PRINT_STAT(ContainsNoDuplicateCall);
2410   DEBUG_PRINT_STAT(Cost);
2411   DEBUG_PRINT_STAT(Threshold);
2412 #undef DEBUG_PRINT_STAT
2413 }
2414 
2415 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2416 /// Dump stats about this call's analysis.
2417 LLVM_DUMP_METHOD void InlineCostCallAnalyzer::dump() {
2418   print();
2419 }
2420 #endif
2421 
2422 /// Test that there are no attribute conflicts between Caller and Callee
2423 ///        that prevent inlining.
2424 static bool functionsHaveCompatibleAttributes(
2425     Function *Caller, Function *Callee, TargetTransformInfo &TTI,
2426     function_ref<const TargetLibraryInfo &(Function &)> &GetTLI) {
2427   // Note that CalleeTLI must be a copy not a reference. The legacy pass manager
2428   // caches the most recently created TLI in the TargetLibraryInfoWrapperPass
2429   // object, and always returns the same object (which is overwritten on each
2430   // GetTLI call). Therefore we copy the first result.
2431   auto CalleeTLI = GetTLI(*Callee);
2432   return TTI.areInlineCompatible(Caller, Callee) &&
2433          GetTLI(*Caller).areInlineCompatible(CalleeTLI,
2434                                              InlineCallerSupersetNoBuiltin) &&
2435          AttributeFuncs::areInlineCompatible(*Caller, *Callee);
2436 }
2437 
2438 int llvm::getCallsiteCost(CallBase &Call, const DataLayout &DL) {
2439   int Cost = 0;
2440   for (unsigned I = 0, E = Call.arg_size(); I != E; ++I) {
2441     if (Call.isByValArgument(I)) {
2442       // We approximate the number of loads and stores needed by dividing the
2443       // size of the byval type by the target's pointer size.
2444       PointerType *PTy = cast<PointerType>(Call.getArgOperand(I)->getType());
2445       unsigned TypeSize = DL.getTypeSizeInBits(PTy->getElementType());
2446       unsigned AS = PTy->getAddressSpace();
2447       unsigned PointerSize = DL.getPointerSizeInBits(AS);
2448       // Ceiling division.
2449       unsigned NumStores = (TypeSize + PointerSize - 1) / PointerSize;
2450 
2451       // If it generates more than 8 stores it is likely to be expanded as an
2452       // inline memcpy so we take that as an upper bound. Otherwise we assume
2453       // one load and one store per word copied.
2454       // FIXME: The maxStoresPerMemcpy setting from the target should be used
2455       // here instead of a magic number of 8, but it's not available via
2456       // DataLayout.
2457       NumStores = std::min(NumStores, 8U);
2458 
2459       Cost += 2 * NumStores * InlineConstants::InstrCost;
2460     } else {
2461       // For non-byval arguments subtract off one instruction per call
2462       // argument.
2463       Cost += InlineConstants::InstrCost;
2464     }
2465   }
2466   // The call instruction also disappears after inlining.
2467   Cost += InlineConstants::InstrCost + InlineConstants::CallPenalty;
2468   return Cost;
2469 }
2470 
2471 InlineCost llvm::getInlineCost(
2472     CallBase &Call, const InlineParams &Params, TargetTransformInfo &CalleeTTI,
2473     function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
2474     function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
2475     function_ref<BlockFrequencyInfo &(Function &)> GetBFI,
2476     ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE) {
2477   return getInlineCost(Call, Call.getCalledFunction(), Params, CalleeTTI,
2478                        GetAssumptionCache, GetTLI, GetBFI, PSI, ORE);
2479 }
2480 
2481 Optional<int> llvm::getInliningCostEstimate(
2482     CallBase &Call, TargetTransformInfo &CalleeTTI,
2483     function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
2484     function_ref<BlockFrequencyInfo &(Function &)> GetBFI,
2485     ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE) {
2486   const InlineParams Params = {/* DefaultThreshold*/ 0,
2487                                /*HintThreshold*/ {},
2488                                /*ColdThreshold*/ {},
2489                                /*OptSizeThreshold*/ {},
2490                                /*OptMinSizeThreshold*/ {},
2491                                /*HotCallSiteThreshold*/ {},
2492                                /*LocallyHotCallSiteThreshold*/ {},
2493                                /*ColdCallSiteThreshold*/ {},
2494                                /*ComputeFullInlineCost*/ true,
2495                                /*EnableDeferral*/ true};
2496 
2497   InlineCostCallAnalyzer CA(*Call.getCalledFunction(), Call, Params, CalleeTTI,
2498                             GetAssumptionCache, GetBFI, PSI, ORE, true,
2499                             /*IgnoreThreshold*/ true);
2500   auto R = CA.analyze();
2501   if (!R.isSuccess())
2502     return None;
2503   return CA.getCost();
2504 }
2505 
2506 Optional<InlineResult> llvm::getAttributeBasedInliningDecision(
2507     CallBase &Call, Function *Callee, TargetTransformInfo &CalleeTTI,
2508     function_ref<const TargetLibraryInfo &(Function &)> GetTLI) {
2509 
2510   // Cannot inline indirect calls.
2511   if (!Callee)
2512     return InlineResult::failure("indirect call");
2513 
2514   // When callee coroutine function is inlined into caller coroutine function
2515   // before coro-split pass,
2516   // coro-early pass can not handle this quiet well.
2517   // So we won't inline the coroutine function if it have not been unsplited
2518   if (Callee->isPresplitCoroutine())
2519     return InlineResult::failure("unsplited coroutine call");
2520 
2521   // Never inline calls with byval arguments that does not have the alloca
2522   // address space. Since byval arguments can be replaced with a copy to an
2523   // alloca, the inlined code would need to be adjusted to handle that the
2524   // argument is in the alloca address space (so it is a little bit complicated
2525   // to solve).
2526   unsigned AllocaAS = Callee->getParent()->getDataLayout().getAllocaAddrSpace();
2527   for (unsigned I = 0, E = Call.arg_size(); I != E; ++I)
2528     if (Call.isByValArgument(I)) {
2529       PointerType *PTy = cast<PointerType>(Call.getArgOperand(I)->getType());
2530       if (PTy->getAddressSpace() != AllocaAS)
2531         return InlineResult::failure("byval arguments without alloca"
2532                                      " address space");
2533     }
2534 
2535   // Calls to functions with always-inline attributes should be inlined
2536   // whenever possible.
2537   if (Call.hasFnAttr(Attribute::AlwaysInline)) {
2538     auto IsViable = isInlineViable(*Callee);
2539     if (IsViable.isSuccess())
2540       return InlineResult::success();
2541     return InlineResult::failure(IsViable.getFailureReason());
2542   }
2543 
2544   // Never inline functions with conflicting attributes (unless callee has
2545   // always-inline attribute).
2546   Function *Caller = Call.getCaller();
2547   if (!functionsHaveCompatibleAttributes(Caller, Callee, CalleeTTI, GetTLI))
2548     return InlineResult::failure("conflicting attributes");
2549 
2550   // Don't inline this call if the caller has the optnone attribute.
2551   if (Caller->hasOptNone())
2552     return InlineResult::failure("optnone attribute");
2553 
2554   // Don't inline a function that treats null pointer as valid into a caller
2555   // that does not have this attribute.
2556   if (!Caller->nullPointerIsDefined() && Callee->nullPointerIsDefined())
2557     return InlineResult::failure("nullptr definitions incompatible");
2558 
2559   // Don't inline functions which can be interposed at link-time.
2560   if (Callee->isInterposable())
2561     return InlineResult::failure("interposable");
2562 
2563   // Don't inline functions marked noinline.
2564   if (Callee->hasFnAttribute(Attribute::NoInline))
2565     return InlineResult::failure("noinline function attribute");
2566 
2567   // Don't inline call sites marked noinline.
2568   if (Call.isNoInline())
2569     return InlineResult::failure("noinline call site attribute");
2570 
2571   // Don't inline functions if one does not have any stack protector attribute
2572   // but the other does.
2573   if (Caller->hasStackProtectorFnAttr() && !Callee->hasStackProtectorFnAttr())
2574     return InlineResult::failure(
2575         "stack protected caller but callee requested no stack protector");
2576   if (Callee->hasStackProtectorFnAttr() && !Caller->hasStackProtectorFnAttr())
2577     return InlineResult::failure(
2578         "stack protected callee but caller requested no stack protector");
2579 
2580   return None;
2581 }
2582 
2583 InlineCost llvm::getInlineCost(
2584     CallBase &Call, Function *Callee, const InlineParams &Params,
2585     TargetTransformInfo &CalleeTTI,
2586     function_ref<AssumptionCache &(Function &)> GetAssumptionCache,
2587     function_ref<const TargetLibraryInfo &(Function &)> GetTLI,
2588     function_ref<BlockFrequencyInfo &(Function &)> GetBFI,
2589     ProfileSummaryInfo *PSI, OptimizationRemarkEmitter *ORE) {
2590 
2591   auto UserDecision =
2592       llvm::getAttributeBasedInliningDecision(Call, Callee, CalleeTTI, GetTLI);
2593 
2594   if (UserDecision.hasValue()) {
2595     if (UserDecision->isSuccess())
2596       return llvm::InlineCost::getAlways("always inline attribute");
2597     return llvm::InlineCost::getNever(UserDecision->getFailureReason());
2598   }
2599 
2600   LLVM_DEBUG(llvm::dbgs() << "      Analyzing call of " << Callee->getName()
2601                           << "... (caller:" << Call.getCaller()->getName()
2602                           << ")\n");
2603 
2604   InlineCostCallAnalyzer CA(*Callee, Call, Params, CalleeTTI,
2605                             GetAssumptionCache, GetBFI, PSI, ORE);
2606   InlineResult ShouldInline = CA.analyze();
2607 
2608   LLVM_DEBUG(CA.dump());
2609 
2610   // Check if there was a reason to force inlining or no inlining.
2611   if (!ShouldInline.isSuccess() && CA.getCost() < CA.getThreshold())
2612     return InlineCost::getNever(ShouldInline.getFailureReason());
2613   if (ShouldInline.isSuccess() && CA.getCost() >= CA.getThreshold())
2614     return InlineCost::getAlways("empty function");
2615 
2616   return llvm::InlineCost::get(CA.getCost(), CA.getThreshold());
2617 }
2618 
2619 InlineResult llvm::isInlineViable(Function &F) {
2620   bool ReturnsTwice = F.hasFnAttribute(Attribute::ReturnsTwice);
2621   for (BasicBlock &BB : F) {
2622     // Disallow inlining of functions which contain indirect branches.
2623     if (isa<IndirectBrInst>(BB.getTerminator()))
2624       return InlineResult::failure("contains indirect branches");
2625 
2626     // Disallow inlining of blockaddresses which are used by non-callbr
2627     // instructions.
2628     if (BB.hasAddressTaken())
2629       for (User *U : BlockAddress::get(&BB)->users())
2630         if (!isa<CallBrInst>(*U))
2631           return InlineResult::failure("blockaddress used outside of callbr");
2632 
2633     for (auto &II : BB) {
2634       CallBase *Call = dyn_cast<CallBase>(&II);
2635       if (!Call)
2636         continue;
2637 
2638       // Disallow recursive calls.
2639       Function *Callee = Call->getCalledFunction();
2640       if (&F == Callee)
2641         return InlineResult::failure("recursive call");
2642 
2643       // Disallow calls which expose returns-twice to a function not previously
2644       // attributed as such.
2645       if (!ReturnsTwice && isa<CallInst>(Call) &&
2646           cast<CallInst>(Call)->canReturnTwice())
2647         return InlineResult::failure("exposes returns-twice attribute");
2648 
2649       if (Callee)
2650         switch (Callee->getIntrinsicID()) {
2651         default:
2652           break;
2653         case llvm::Intrinsic::icall_branch_funnel:
2654           // Disallow inlining of @llvm.icall.branch.funnel because current
2655           // backend can't separate call targets from call arguments.
2656           return InlineResult::failure(
2657               "disallowed inlining of @llvm.icall.branch.funnel");
2658         case llvm::Intrinsic::localescape:
2659           // Disallow inlining functions that call @llvm.localescape. Doing this
2660           // correctly would require major changes to the inliner.
2661           return InlineResult::failure(
2662               "disallowed inlining of @llvm.localescape");
2663         case llvm::Intrinsic::vastart:
2664           // Disallow inlining of functions that initialize VarArgs with
2665           // va_start.
2666           return InlineResult::failure(
2667               "contains VarArgs initialized with va_start");
2668         }
2669     }
2670   }
2671 
2672   return InlineResult::success();
2673 }
2674 
2675 // APIs to create InlineParams based on command line flags and/or other
2676 // parameters.
2677 
2678 InlineParams llvm::getInlineParams(int Threshold) {
2679   InlineParams Params;
2680 
2681   // This field is the threshold to use for a callee by default. This is
2682   // derived from one or more of:
2683   //  * optimization or size-optimization levels,
2684   //  * a value passed to createFunctionInliningPass function, or
2685   //  * the -inline-threshold flag.
2686   //  If the -inline-threshold flag is explicitly specified, that is used
2687   //  irrespective of anything else.
2688   if (InlineThreshold.getNumOccurrences() > 0)
2689     Params.DefaultThreshold = InlineThreshold;
2690   else
2691     Params.DefaultThreshold = Threshold;
2692 
2693   // Set the HintThreshold knob from the -inlinehint-threshold.
2694   Params.HintThreshold = HintThreshold;
2695 
2696   // Set the HotCallSiteThreshold knob from the -hot-callsite-threshold.
2697   Params.HotCallSiteThreshold = HotCallSiteThreshold;
2698 
2699   // If the -locally-hot-callsite-threshold is explicitly specified, use it to
2700   // populate LocallyHotCallSiteThreshold. Later, we populate
2701   // Params.LocallyHotCallSiteThreshold from -locally-hot-callsite-threshold if
2702   // we know that optimization level is O3 (in the getInlineParams variant that
2703   // takes the opt and size levels).
2704   // FIXME: Remove this check (and make the assignment unconditional) after
2705   // addressing size regression issues at O2.
2706   if (LocallyHotCallSiteThreshold.getNumOccurrences() > 0)
2707     Params.LocallyHotCallSiteThreshold = LocallyHotCallSiteThreshold;
2708 
2709   // Set the ColdCallSiteThreshold knob from the
2710   // -inline-cold-callsite-threshold.
2711   Params.ColdCallSiteThreshold = ColdCallSiteThreshold;
2712 
2713   // Set the OptMinSizeThreshold and OptSizeThreshold params only if the
2714   // -inlinehint-threshold commandline option is not explicitly given. If that
2715   // option is present, then its value applies even for callees with size and
2716   // minsize attributes.
2717   // If the -inline-threshold is not specified, set the ColdThreshold from the
2718   // -inlinecold-threshold even if it is not explicitly passed. If
2719   // -inline-threshold is specified, then -inlinecold-threshold needs to be
2720   // explicitly specified to set the ColdThreshold knob
2721   if (InlineThreshold.getNumOccurrences() == 0) {
2722     Params.OptMinSizeThreshold = InlineConstants::OptMinSizeThreshold;
2723     Params.OptSizeThreshold = InlineConstants::OptSizeThreshold;
2724     Params.ColdThreshold = ColdThreshold;
2725   } else if (ColdThreshold.getNumOccurrences() > 0) {
2726     Params.ColdThreshold = ColdThreshold;
2727   }
2728   return Params;
2729 }
2730 
2731 InlineParams llvm::getInlineParams() {
2732   return getInlineParams(DefaultThreshold);
2733 }
2734 
2735 // Compute the default threshold for inlining based on the opt level and the
2736 // size opt level.
2737 static int computeThresholdFromOptLevels(unsigned OptLevel,
2738                                          unsigned SizeOptLevel) {
2739   if (OptLevel > 2)
2740     return InlineConstants::OptAggressiveThreshold;
2741   if (SizeOptLevel == 1) // -Os
2742     return InlineConstants::OptSizeThreshold;
2743   if (SizeOptLevel == 2) // -Oz
2744     return InlineConstants::OptMinSizeThreshold;
2745   return DefaultThreshold;
2746 }
2747 
2748 InlineParams llvm::getInlineParams(unsigned OptLevel, unsigned SizeOptLevel) {
2749   auto Params =
2750       getInlineParams(computeThresholdFromOptLevels(OptLevel, SizeOptLevel));
2751   // At O3, use the value of -locally-hot-callsite-threshold option to populate
2752   // Params.LocallyHotCallSiteThreshold. Below O3, this flag has effect only
2753   // when it is specified explicitly.
2754   if (OptLevel > 2)
2755     Params.LocallyHotCallSiteThreshold = LocallyHotCallSiteThreshold;
2756   return Params;
2757 }
2758 
2759 PreservedAnalyses
2760 InlineCostAnnotationPrinterPass::run(Function &F,
2761                                      FunctionAnalysisManager &FAM) {
2762   PrintInstructionComments = true;
2763   std::function<AssumptionCache &(Function &)> GetAssumptionCache = [&](
2764       Function &F) -> AssumptionCache & {
2765     return FAM.getResult<AssumptionAnalysis>(F);
2766   };
2767   Module *M = F.getParent();
2768   ProfileSummaryInfo PSI(*M);
2769   DataLayout DL(M);
2770   TargetTransformInfo TTI(DL);
2771   // FIXME: Redesign the usage of InlineParams to expand the scope of this pass.
2772   // In the current implementation, the type of InlineParams doesn't matter as
2773   // the pass serves only for verification of inliner's decisions.
2774   // We can add a flag which determines InlineParams for this run. Right now,
2775   // the default InlineParams are used.
2776   const InlineParams Params = llvm::getInlineParams();
2777   for (BasicBlock &BB : F) {
2778     for (Instruction &I : BB) {
2779       if (CallInst *CI = dyn_cast<CallInst>(&I)) {
2780         Function *CalledFunction = CI->getCalledFunction();
2781         if (!CalledFunction || CalledFunction->isDeclaration())
2782           continue;
2783         OptimizationRemarkEmitter ORE(CalledFunction);
2784         InlineCostCallAnalyzer ICCA(*CalledFunction, *CI, Params, TTI,
2785                                     GetAssumptionCache, nullptr, &PSI, &ORE);
2786         ICCA.analyze();
2787         OS << "      Analyzing call of " << CalledFunction->getName()
2788            << "... (caller:" << CI->getCaller()->getName() << ")\n";
2789         ICCA.print();
2790       }
2791     }
2792   }
2793   return PreservedAnalyses::all();
2794 }
2795