1 //===- InlineCost.cpp - Cost analysis for inliner -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements inline cost analysis.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "inline-cost"
15 #include "llvm/Analysis/InlineCost.h"
16 #include "llvm/Analysis/ConstantFolding.h"
17 #include "llvm/Analysis/InstructionSimplify.h"
18 #include "llvm/Support/CallSite.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/InstVisitor.h"
21 #include "llvm/Support/GetElementPtrTypeIterator.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/CallingConv.h"
24 #include "llvm/IntrinsicInst.h"
25 #include "llvm/Operator.h"
26 #include "llvm/GlobalAlias.h"
27 #include "llvm/Target/TargetData.h"
28 #include "llvm/ADT/STLExtras.h"
29 #include "llvm/ADT/SetVector.h"
30 #include "llvm/ADT/SmallVector.h"
31 #include "llvm/ADT/SmallPtrSet.h"
32 #include "llvm/ADT/Statistic.h"
36 STATISTIC(NumCallsAnalyzed, "Number of call sites analyzed");
40 class CallAnalyzer : public InstVisitor<CallAnalyzer, bool> {
41 typedef InstVisitor<CallAnalyzer, bool> Base;
42 friend class InstVisitor<CallAnalyzer, bool>;
44 // TargetData if available, or null.
45 const TargetData *const TD;
47 // The called function.
52 const bool AlwaysInline;
54 bool IsCallerRecursive;
56 bool ExposesReturnsTwice;
57 bool HasDynamicAlloca;
58 /// Number of bytes allocated statically by the callee.
59 uint64_t AllocatedSize;
60 unsigned NumInstructions, NumVectorInstructions;
61 int FiftyPercentVectorBonus, TenPercentVectorBonus;
64 // While we walk the potentially-inlined instructions, we build up and
65 // maintain a mapping of simplified values specific to this callsite. The
66 // idea is to propagate any special information we have about arguments to
67 // this call through the inlinable section of the function, and account for
68 // likely simplifications post-inlining. The most important aspect we track
69 // is CFG altering simplifications -- when we prove a basic block dead, that
70 // can cause dramatic shifts in the cost of inlining a function.
71 DenseMap<Value *, Constant *> SimplifiedValues;
73 // Keep track of the values which map back (through function arguments) to
74 // allocas on the caller stack which could be simplified through SROA.
75 DenseMap<Value *, Value *> SROAArgValues;
77 // The mapping of caller Alloca values to their accumulated cost savings. If
78 // we have to disable SROA for one of the allocas, this tells us how much
79 // cost must be added.
80 DenseMap<Value *, int> SROAArgCosts;
82 // Keep track of values which map to a pointer base and constant offset.
83 DenseMap<Value *, std::pair<Value *, APInt> > ConstantOffsetPtrs;
85 // Custom simplification helper routines.
86 bool isAllocaDerivedArg(Value *V);
87 bool lookupSROAArgAndCost(Value *V, Value *&Arg,
88 DenseMap<Value *, int>::iterator &CostIt);
89 void disableSROA(DenseMap<Value *, int>::iterator CostIt);
90 void disableSROA(Value *V);
91 void accumulateSROACost(DenseMap<Value *, int>::iterator CostIt,
93 bool handleSROACandidate(bool IsSROAValid,
94 DenseMap<Value *, int>::iterator CostIt,
96 bool isGEPOffsetConstant(GetElementPtrInst &GEP);
97 bool accumulateGEPOffset(GEPOperator &GEP, APInt &Offset);
98 ConstantInt *stripAndComputeInBoundsConstantOffsets(Value *&V);
100 // Custom analysis routines.
101 bool analyzeBlock(BasicBlock *BB);
103 // Disable several entry points to the visitor so we don't accidentally use
104 // them by declaring but not defining them here.
105 void visit(Module *); void visit(Module &);
106 void visit(Function *); void visit(Function &);
107 void visit(BasicBlock *); void visit(BasicBlock &);
109 // Provide base case for our instruction visit.
110 bool visitInstruction(Instruction &I);
112 // Our visit overrides.
113 bool visitAlloca(AllocaInst &I);
114 bool visitPHI(PHINode &I);
115 bool visitGetElementPtr(GetElementPtrInst &I);
116 bool visitBitCast(BitCastInst &I);
117 bool visitPtrToInt(PtrToIntInst &I);
118 bool visitIntToPtr(IntToPtrInst &I);
119 bool visitCastInst(CastInst &I);
120 bool visitUnaryInstruction(UnaryInstruction &I);
121 bool visitICmp(ICmpInst &I);
122 bool visitSub(BinaryOperator &I);
123 bool visitBinaryOperator(BinaryOperator &I);
124 bool visitLoad(LoadInst &I);
125 bool visitStore(StoreInst &I);
126 bool visitCallSite(CallSite CS);
129 CallAnalyzer(const TargetData *TD, Function &Callee, int Threshold)
130 : TD(TD), F(Callee), Threshold(Threshold), Cost(0),
131 AlwaysInline(F.getFnAttributes().hasAlwaysInlineAttr()),
132 IsCallerRecursive(false), IsRecursiveCall(false),
133 ExposesReturnsTwice(false), HasDynamicAlloca(false), AllocatedSize(0),
134 NumInstructions(0), NumVectorInstructions(0),
135 FiftyPercentVectorBonus(0), TenPercentVectorBonus(0), VectorBonus(0),
136 NumConstantArgs(0), NumConstantOffsetPtrArgs(0), NumAllocaArgs(0),
137 NumConstantPtrCmps(0), NumConstantPtrDiffs(0),
138 NumInstructionsSimplified(0), SROACostSavings(0), SROACostSavingsLost(0) {
141 bool analyzeCall(CallSite CS);
143 int getThreshold() { return Threshold; }
144 int getCost() { return Cost; }
146 // Keep a bunch of stats about the cost savings found so we can print them
147 // out when debugging.
148 unsigned NumConstantArgs;
149 unsigned NumConstantOffsetPtrArgs;
150 unsigned NumAllocaArgs;
151 unsigned NumConstantPtrCmps;
152 unsigned NumConstantPtrDiffs;
153 unsigned NumInstructionsSimplified;
154 unsigned SROACostSavings;
155 unsigned SROACostSavingsLost;
162 /// \brief Test whether the given value is an Alloca-derived function argument.
163 bool CallAnalyzer::isAllocaDerivedArg(Value *V) {
164 return SROAArgValues.count(V);
167 /// \brief Lookup the SROA-candidate argument and cost iterator which V maps to.
168 /// Returns false if V does not map to a SROA-candidate.
169 bool CallAnalyzer::lookupSROAArgAndCost(
170 Value *V, Value *&Arg, DenseMap<Value *, int>::iterator &CostIt) {
171 if (SROAArgValues.empty() || SROAArgCosts.empty())
174 DenseMap<Value *, Value *>::iterator ArgIt = SROAArgValues.find(V);
175 if (ArgIt == SROAArgValues.end())
179 CostIt = SROAArgCosts.find(Arg);
180 return CostIt != SROAArgCosts.end();
183 /// \brief Disable SROA for the candidate marked by this cost iterator.
185 /// This marks the candidate as no longer viable for SROA, and adds the cost
186 /// savings associated with it back into the inline cost measurement.
187 void CallAnalyzer::disableSROA(DenseMap<Value *, int>::iterator CostIt) {
188 // If we're no longer able to perform SROA we need to undo its cost savings
189 // and prevent subsequent analysis.
190 Cost += CostIt->second;
191 SROACostSavings -= CostIt->second;
192 SROACostSavingsLost += CostIt->second;
193 SROAArgCosts.erase(CostIt);
196 /// \brief If 'V' maps to a SROA candidate, disable SROA for it.
197 void CallAnalyzer::disableSROA(Value *V) {
199 DenseMap<Value *, int>::iterator CostIt;
200 if (lookupSROAArgAndCost(V, SROAArg, CostIt))
204 /// \brief Accumulate the given cost for a particular SROA candidate.
205 void CallAnalyzer::accumulateSROACost(DenseMap<Value *, int>::iterator CostIt,
206 int InstructionCost) {
207 CostIt->second += InstructionCost;
208 SROACostSavings += InstructionCost;
211 /// \brief Helper for the common pattern of handling a SROA candidate.
212 /// Either accumulates the cost savings if the SROA remains valid, or disables
213 /// SROA for the candidate.
214 bool CallAnalyzer::handleSROACandidate(bool IsSROAValid,
215 DenseMap<Value *, int>::iterator CostIt,
216 int InstructionCost) {
218 accumulateSROACost(CostIt, InstructionCost);
226 /// \brief Check whether a GEP's indices are all constant.
228 /// Respects any simplified values known during the analysis of this callsite.
229 bool CallAnalyzer::isGEPOffsetConstant(GetElementPtrInst &GEP) {
230 for (User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end(); I != E; ++I)
231 if (!isa<Constant>(*I) && !SimplifiedValues.lookup(*I))
237 /// \brief Accumulate a constant GEP offset into an APInt if possible.
239 /// Returns false if unable to compute the offset for any reason. Respects any
240 /// simplified values known during the analysis of this callsite.
241 bool CallAnalyzer::accumulateGEPOffset(GEPOperator &GEP, APInt &Offset) {
245 unsigned IntPtrWidth = TD->getPointerSizeInBits();
246 assert(IntPtrWidth == Offset.getBitWidth());
248 for (gep_type_iterator GTI = gep_type_begin(GEP), GTE = gep_type_end(GEP);
250 ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand());
252 if (Constant *SimpleOp = SimplifiedValues.lookup(GTI.getOperand()))
253 OpC = dyn_cast<ConstantInt>(SimpleOp);
256 if (OpC->isZero()) continue;
258 // Handle a struct index, which adds its field offset to the pointer.
259 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
260 unsigned ElementIdx = OpC->getZExtValue();
261 const StructLayout *SL = TD->getStructLayout(STy);
262 Offset += APInt(IntPtrWidth, SL->getElementOffset(ElementIdx));
266 APInt TypeSize(IntPtrWidth, TD->getTypeAllocSize(GTI.getIndexedType()));
267 Offset += OpC->getValue().sextOrTrunc(IntPtrWidth) * TypeSize;
272 bool CallAnalyzer::visitAlloca(AllocaInst &I) {
273 // FIXME: Check whether inlining will turn a dynamic alloca into a static
274 // alloca, and handle that case.
276 // Accumulate the allocated size.
277 if (I.isStaticAlloca()) {
278 Type *Ty = I.getAllocatedType();
279 AllocatedSize += (TD ? TD->getTypeAllocSize(Ty) :
280 Ty->getPrimitiveSizeInBits());
283 // We will happily inline static alloca instructions or dynamic alloca
284 // instructions in always-inline situations.
285 if (AlwaysInline || I.isStaticAlloca())
286 return Base::visitAlloca(I);
288 // FIXME: This is overly conservative. Dynamic allocas are inefficient for
289 // a variety of reasons, and so we would like to not inline them into
290 // functions which don't currently have a dynamic alloca. This simply
291 // disables inlining altogether in the presence of a dynamic alloca.
292 HasDynamicAlloca = true;
296 bool CallAnalyzer::visitPHI(PHINode &I) {
297 // FIXME: We should potentially be tracking values through phi nodes,
298 // especially when they collapse to a single value due to deleted CFG edges
301 // FIXME: We need to propagate SROA *disabling* through phi nodes, even
302 // though we don't want to propagate it's bonuses. The idea is to disable
303 // SROA if it *might* be used in an inappropriate manner.
305 // Phi nodes are always zero-cost.
309 bool CallAnalyzer::visitGetElementPtr(GetElementPtrInst &I) {
311 DenseMap<Value *, int>::iterator CostIt;
312 bool SROACandidate = lookupSROAArgAndCost(I.getPointerOperand(),
315 // Try to fold GEPs of constant-offset call site argument pointers. This
316 // requires target data and inbounds GEPs.
317 if (TD && I.isInBounds()) {
318 // Check if we have a base + offset for the pointer.
319 Value *Ptr = I.getPointerOperand();
320 std::pair<Value *, APInt> BaseAndOffset = ConstantOffsetPtrs.lookup(Ptr);
321 if (BaseAndOffset.first) {
322 // Check if the offset of this GEP is constant, and if so accumulate it
324 if (!accumulateGEPOffset(cast<GEPOperator>(I), BaseAndOffset.second)) {
325 // Non-constant GEPs aren't folded, and disable SROA.
331 // Add the result as a new mapping to Base + Offset.
332 ConstantOffsetPtrs[&I] = BaseAndOffset;
334 // Also handle SROA candidates here, we already know that the GEP is
335 // all-constant indexed.
337 SROAArgValues[&I] = SROAArg;
343 if (isGEPOffsetConstant(I)) {
345 SROAArgValues[&I] = SROAArg;
347 // Constant GEPs are modeled as free.
351 // Variable GEPs will require math and will disable SROA.
357 bool CallAnalyzer::visitBitCast(BitCastInst &I) {
358 // Propagate constants through bitcasts.
359 if (Constant *COp = dyn_cast<Constant>(I.getOperand(0)))
360 if (Constant *C = ConstantExpr::getBitCast(COp, I.getType())) {
361 SimplifiedValues[&I] = C;
365 // Track base/offsets through casts
366 std::pair<Value *, APInt> BaseAndOffset
367 = ConstantOffsetPtrs.lookup(I.getOperand(0));
368 // Casts don't change the offset, just wrap it up.
369 if (BaseAndOffset.first)
370 ConstantOffsetPtrs[&I] = BaseAndOffset;
372 // Also look for SROA candidates here.
374 DenseMap<Value *, int>::iterator CostIt;
375 if (lookupSROAArgAndCost(I.getOperand(0), SROAArg, CostIt))
376 SROAArgValues[&I] = SROAArg;
378 // Bitcasts are always zero cost.
382 bool CallAnalyzer::visitPtrToInt(PtrToIntInst &I) {
383 // Propagate constants through ptrtoint.
384 if (Constant *COp = dyn_cast<Constant>(I.getOperand(0)))
385 if (Constant *C = ConstantExpr::getPtrToInt(COp, I.getType())) {
386 SimplifiedValues[&I] = C;
390 // Track base/offset pairs when converted to a plain integer provided the
391 // integer is large enough to represent the pointer.
392 unsigned IntegerSize = I.getType()->getScalarSizeInBits();
393 if (TD && IntegerSize >= TD->getPointerSizeInBits()) {
394 std::pair<Value *, APInt> BaseAndOffset
395 = ConstantOffsetPtrs.lookup(I.getOperand(0));
396 if (BaseAndOffset.first)
397 ConstantOffsetPtrs[&I] = BaseAndOffset;
400 // This is really weird. Technically, ptrtoint will disable SROA. However,
401 // unless that ptrtoint is *used* somewhere in the live basic blocks after
402 // inlining, it will be nuked, and SROA should proceed. All of the uses which
403 // would block SROA would also block SROA if applied directly to a pointer,
404 // and so we can just add the integer in here. The only places where SROA is
405 // preserved either cannot fire on an integer, or won't in-and-of themselves
406 // disable SROA (ext) w/o some later use that we would see and disable.
408 DenseMap<Value *, int>::iterator CostIt;
409 if (lookupSROAArgAndCost(I.getOperand(0), SROAArg, CostIt))
410 SROAArgValues[&I] = SROAArg;
412 return isInstructionFree(&I, TD);
415 bool CallAnalyzer::visitIntToPtr(IntToPtrInst &I) {
416 // Propagate constants through ptrtoint.
417 if (Constant *COp = dyn_cast<Constant>(I.getOperand(0)))
418 if (Constant *C = ConstantExpr::getIntToPtr(COp, I.getType())) {
419 SimplifiedValues[&I] = C;
423 // Track base/offset pairs when round-tripped through a pointer without
424 // modifications provided the integer is not too large.
425 Value *Op = I.getOperand(0);
426 unsigned IntegerSize = Op->getType()->getScalarSizeInBits();
427 if (TD && IntegerSize <= TD->getPointerSizeInBits()) {
428 std::pair<Value *, APInt> BaseAndOffset = ConstantOffsetPtrs.lookup(Op);
429 if (BaseAndOffset.first)
430 ConstantOffsetPtrs[&I] = BaseAndOffset;
433 // "Propagate" SROA here in the same manner as we do for ptrtoint above.
435 DenseMap<Value *, int>::iterator CostIt;
436 if (lookupSROAArgAndCost(Op, SROAArg, CostIt))
437 SROAArgValues[&I] = SROAArg;
439 return isInstructionFree(&I, TD);
442 bool CallAnalyzer::visitCastInst(CastInst &I) {
443 // Propagate constants through ptrtoint.
444 if (Constant *COp = dyn_cast<Constant>(I.getOperand(0)))
445 if (Constant *C = ConstantExpr::getCast(I.getOpcode(), COp, I.getType())) {
446 SimplifiedValues[&I] = C;
450 // Disable SROA in the face of arbitrary casts we don't whitelist elsewhere.
451 disableSROA(I.getOperand(0));
453 return isInstructionFree(&I, TD);
456 bool CallAnalyzer::visitUnaryInstruction(UnaryInstruction &I) {
457 Value *Operand = I.getOperand(0);
458 Constant *Ops[1] = { dyn_cast<Constant>(Operand) };
459 if (Ops[0] || (Ops[0] = SimplifiedValues.lookup(Operand)))
460 if (Constant *C = ConstantFoldInstOperands(I.getOpcode(), I.getType(),
462 SimplifiedValues[&I] = C;
466 // Disable any SROA on the argument to arbitrary unary operators.
467 disableSROA(Operand);
472 bool CallAnalyzer::visitICmp(ICmpInst &I) {
473 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
474 // First try to handle simplified comparisons.
475 if (!isa<Constant>(LHS))
476 if (Constant *SimpleLHS = SimplifiedValues.lookup(LHS))
478 if (!isa<Constant>(RHS))
479 if (Constant *SimpleRHS = SimplifiedValues.lookup(RHS))
481 if (Constant *CLHS = dyn_cast<Constant>(LHS))
482 if (Constant *CRHS = dyn_cast<Constant>(RHS))
483 if (Constant *C = ConstantExpr::getICmp(I.getPredicate(), CLHS, CRHS)) {
484 SimplifiedValues[&I] = C;
488 // Otherwise look for a comparison between constant offset pointers with
490 Value *LHSBase, *RHSBase;
491 APInt LHSOffset, RHSOffset;
492 llvm::tie(LHSBase, LHSOffset) = ConstantOffsetPtrs.lookup(LHS);
494 llvm::tie(RHSBase, RHSOffset) = ConstantOffsetPtrs.lookup(RHS);
495 if (RHSBase && LHSBase == RHSBase) {
496 // We have common bases, fold the icmp to a constant based on the
498 Constant *CLHS = ConstantInt::get(LHS->getContext(), LHSOffset);
499 Constant *CRHS = ConstantInt::get(RHS->getContext(), RHSOffset);
500 if (Constant *C = ConstantExpr::getICmp(I.getPredicate(), CLHS, CRHS)) {
501 SimplifiedValues[&I] = C;
502 ++NumConstantPtrCmps;
508 // If the comparison is an equality comparison with null, we can simplify it
509 // for any alloca-derived argument.
510 if (I.isEquality() && isa<ConstantPointerNull>(I.getOperand(1)))
511 if (isAllocaDerivedArg(I.getOperand(0))) {
512 // We can actually predict the result of comparisons between an
513 // alloca-derived value and null. Note that this fires regardless of
515 bool IsNotEqual = I.getPredicate() == CmpInst::ICMP_NE;
516 SimplifiedValues[&I] = IsNotEqual ? ConstantInt::getTrue(I.getType())
517 : ConstantInt::getFalse(I.getType());
521 // Finally check for SROA candidates in comparisons.
523 DenseMap<Value *, int>::iterator CostIt;
524 if (lookupSROAArgAndCost(I.getOperand(0), SROAArg, CostIt)) {
525 if (isa<ConstantPointerNull>(I.getOperand(1))) {
526 accumulateSROACost(CostIt, InlineConstants::InstrCost);
536 bool CallAnalyzer::visitSub(BinaryOperator &I) {
537 // Try to handle a special case: we can fold computing the difference of two
538 // constant-related pointers.
539 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
540 Value *LHSBase, *RHSBase;
541 APInt LHSOffset, RHSOffset;
542 llvm::tie(LHSBase, LHSOffset) = ConstantOffsetPtrs.lookup(LHS);
544 llvm::tie(RHSBase, RHSOffset) = ConstantOffsetPtrs.lookup(RHS);
545 if (RHSBase && LHSBase == RHSBase) {
546 // We have common bases, fold the subtract to a constant based on the
548 Constant *CLHS = ConstantInt::get(LHS->getContext(), LHSOffset);
549 Constant *CRHS = ConstantInt::get(RHS->getContext(), RHSOffset);
550 if (Constant *C = ConstantExpr::getSub(CLHS, CRHS)) {
551 SimplifiedValues[&I] = C;
552 ++NumConstantPtrDiffs;
558 // Otherwise, fall back to the generic logic for simplifying and handling
560 return Base::visitSub(I);
563 bool CallAnalyzer::visitBinaryOperator(BinaryOperator &I) {
564 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
565 if (!isa<Constant>(LHS))
566 if (Constant *SimpleLHS = SimplifiedValues.lookup(LHS))
568 if (!isa<Constant>(RHS))
569 if (Constant *SimpleRHS = SimplifiedValues.lookup(RHS))
571 Value *SimpleV = SimplifyBinOp(I.getOpcode(), LHS, RHS, TD);
572 if (Constant *C = dyn_cast_or_null<Constant>(SimpleV)) {
573 SimplifiedValues[&I] = C;
577 // Disable any SROA on arguments to arbitrary, unsimplified binary operators.
584 bool CallAnalyzer::visitLoad(LoadInst &I) {
586 DenseMap<Value *, int>::iterator CostIt;
587 if (lookupSROAArgAndCost(I.getOperand(0), SROAArg, CostIt)) {
589 accumulateSROACost(CostIt, InlineConstants::InstrCost);
599 bool CallAnalyzer::visitStore(StoreInst &I) {
601 DenseMap<Value *, int>::iterator CostIt;
602 if (lookupSROAArgAndCost(I.getOperand(0), SROAArg, CostIt)) {
604 accumulateSROACost(CostIt, InlineConstants::InstrCost);
614 bool CallAnalyzer::visitCallSite(CallSite CS) {
615 if (CS.isCall() && cast<CallInst>(CS.getInstruction())->canReturnTwice() &&
616 !F.getFnAttributes().hasReturnsTwiceAttr()) {
617 // This aborts the entire analysis.
618 ExposesReturnsTwice = true;
622 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CS.getInstruction())) {
623 switch (II->getIntrinsicID()) {
625 return Base::visitCallSite(CS);
627 case Intrinsic::memset:
628 case Intrinsic::memcpy:
629 case Intrinsic::memmove:
630 // SROA can usually chew through these intrinsics, but they aren't free.
635 if (Function *F = CS.getCalledFunction()) {
636 if (F == CS.getInstruction()->getParent()->getParent()) {
637 // This flag will fully abort the analysis, so don't bother with anything
639 IsRecursiveCall = true;
643 if (!callIsSmall(CS)) {
644 // We account for the average 1 instruction per call argument setup
646 Cost += CS.arg_size() * InlineConstants::InstrCost;
648 // Everything other than inline ASM will also have a significant cost
649 // merely from making the call.
650 if (!isa<InlineAsm>(CS.getCalledValue()))
651 Cost += InlineConstants::CallPenalty;
654 return Base::visitCallSite(CS);
657 // Otherwise we're in a very special case -- an indirect function call. See
658 // if we can be particularly clever about this.
659 Value *Callee = CS.getCalledValue();
661 // First, pay the price of the argument setup. We account for the average
662 // 1 instruction per call argument setup here.
663 Cost += CS.arg_size() * InlineConstants::InstrCost;
665 // Next, check if this happens to be an indirect function call to a known
666 // function in this inline context. If not, we've done all we can.
667 Function *F = dyn_cast_or_null<Function>(SimplifiedValues.lookup(Callee));
669 return Base::visitCallSite(CS);
671 // If we have a constant that we are calling as a function, we can peer
672 // through it and see the function target. This happens not infrequently
673 // during devirtualization and so we want to give it a hefty bonus for
674 // inlining, but cap that bonus in the event that inlining wouldn't pan
675 // out. Pretend to inline the function, with a custom threshold.
676 CallAnalyzer CA(TD, *F, InlineConstants::IndirectCallThreshold);
677 if (CA.analyzeCall(CS)) {
678 // We were able to inline the indirect call! Subtract the cost from the
679 // bonus we want to apply, but don't go below zero.
680 Cost -= std::max(0, InlineConstants::IndirectCallThreshold - CA.getCost());
683 return Base::visitCallSite(CS);
686 bool CallAnalyzer::visitInstruction(Instruction &I) {
687 // Some instructions are free. All of the free intrinsics can also be
688 // handled by SROA, etc.
689 if (isInstructionFree(&I, TD))
692 // We found something we don't understand or can't handle. Mark any SROA-able
693 // values in the operand list as no longer viable.
694 for (User::op_iterator OI = I.op_begin(), OE = I.op_end(); OI != OE; ++OI)
701 /// \brief Analyze a basic block for its contribution to the inline cost.
703 /// This method walks the analyzer over every instruction in the given basic
704 /// block and accounts for their cost during inlining at this callsite. It
705 /// aborts early if the threshold has been exceeded or an impossible to inline
706 /// construct has been detected. It returns false if inlining is no longer
707 /// viable, and true if inlining remains viable.
708 bool CallAnalyzer::analyzeBlock(BasicBlock *BB) {
709 for (BasicBlock::iterator I = BB->begin(), E = llvm::prior(BB->end());
712 if (isa<ExtractElementInst>(I) || I->getType()->isVectorTy())
713 ++NumVectorInstructions;
715 // If the instruction simplified to a constant, there is no cost to this
716 // instruction. Visit the instructions using our InstVisitor to account for
717 // all of the per-instruction logic. The visit tree returns true if we
718 // consumed the instruction in any way, and false if the instruction's base
719 // cost should count against inlining.
721 ++NumInstructionsSimplified;
723 Cost += InlineConstants::InstrCost;
725 // If the visit this instruction detected an uninlinable pattern, abort.
726 if (IsRecursiveCall || ExposesReturnsTwice || HasDynamicAlloca)
729 // If the caller is a recursive function then we don't want to inline
730 // functions which allocate a lot of stack space because it would increase
731 // the caller stack usage dramatically.
732 if (IsCallerRecursive &&
733 AllocatedSize > InlineConstants::TotalAllocaSizeRecursiveCaller)
736 if (NumVectorInstructions > NumInstructions/2)
737 VectorBonus = FiftyPercentVectorBonus;
738 else if (NumVectorInstructions > NumInstructions/10)
739 VectorBonus = TenPercentVectorBonus;
743 // Check if we've past the threshold so we don't spin in huge basic
744 // blocks that will never inline.
745 if (!AlwaysInline && Cost > (Threshold + VectorBonus))
752 /// \brief Compute the base pointer and cumulative constant offsets for V.
754 /// This strips all constant offsets off of V, leaving it the base pointer, and
755 /// accumulates the total constant offset applied in the returned constant. It
756 /// returns 0 if V is not a pointer, and returns the constant '0' if there are
757 /// no constant offsets applied.
758 ConstantInt *CallAnalyzer::stripAndComputeInBoundsConstantOffsets(Value *&V) {
759 if (!TD || !V->getType()->isPointerTy())
762 unsigned IntPtrWidth = TD->getPointerSizeInBits();
763 APInt Offset = APInt::getNullValue(IntPtrWidth);
765 // Even though we don't look through PHI nodes, we could be called on an
766 // instruction in an unreachable block, which may be on a cycle.
767 SmallPtrSet<Value *, 4> Visited;
770 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
771 if (!GEP->isInBounds() || !accumulateGEPOffset(*GEP, Offset))
773 V = GEP->getPointerOperand();
774 } else if (Operator::getOpcode(V) == Instruction::BitCast) {
775 V = cast<Operator>(V)->getOperand(0);
776 } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
777 if (GA->mayBeOverridden())
779 V = GA->getAliasee();
783 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
784 } while (Visited.insert(V));
786 Type *IntPtrTy = TD->getIntPtrType(V->getContext());
787 return cast<ConstantInt>(ConstantInt::get(IntPtrTy, Offset));
790 /// \brief Analyze a call site for potential inlining.
792 /// Returns true if inlining this call is viable, and false if it is not
793 /// viable. It computes the cost and adjusts the threshold based on numerous
794 /// factors and heuristics. If this method returns false but the computed cost
795 /// is below the computed threshold, then inlining was forcibly disabled by
796 /// some artifact of the rountine.
797 bool CallAnalyzer::analyzeCall(CallSite CS) {
800 // Track whether the post-inlining function would have more than one basic
801 // block. A single basic block is often intended for inlining. Balloon the
802 // threshold by 50% until we pass the single-BB phase.
803 bool SingleBB = true;
804 int SingleBBBonus = Threshold / 2;
805 Threshold += SingleBBBonus;
807 // Unless we are always-inlining, perform some tweaks to the cost and
808 // threshold based on the direct callsite information.
810 // We want to more aggressively inline vector-dense kernels, so up the
811 // threshold, and we'll lower it if the % of vector instructions gets too
813 assert(NumInstructions == 0);
814 assert(NumVectorInstructions == 0);
815 FiftyPercentVectorBonus = Threshold;
816 TenPercentVectorBonus = Threshold / 2;
818 // Give out bonuses per argument, as the instructions setting them up will
819 // be gone after inlining.
820 for (unsigned I = 0, E = CS.arg_size(); I != E; ++I) {
821 if (TD && CS.isByValArgument(I)) {
822 // We approximate the number of loads and stores needed by dividing the
823 // size of the byval type by the target's pointer size.
824 PointerType *PTy = cast<PointerType>(CS.getArgument(I)->getType());
825 unsigned TypeSize = TD->getTypeSizeInBits(PTy->getElementType());
826 unsigned PointerSize = TD->getPointerSizeInBits();
828 unsigned NumStores = (TypeSize + PointerSize - 1) / PointerSize;
830 // If it generates more than 8 stores it is likely to be expanded as an
831 // inline memcpy so we take that as an upper bound. Otherwise we assume
832 // one load and one store per word copied.
833 // FIXME: The maxStoresPerMemcpy setting from the target should be used
834 // here instead of a magic number of 8, but it's not available via
836 NumStores = std::min(NumStores, 8U);
838 Cost -= 2 * NumStores * InlineConstants::InstrCost;
840 // For non-byval arguments subtract off one instruction per call
842 Cost -= InlineConstants::InstrCost;
846 // If there is only one call of the function, and it has internal linkage,
847 // the cost of inlining it drops dramatically.
848 if (F.hasLocalLinkage() && F.hasOneUse() && &F == CS.getCalledFunction())
849 Cost += InlineConstants::LastCallToStaticBonus;
851 // If the instruction after the call, or if the normal destination of the
852 // invoke is an unreachable instruction, the function is noreturn. As such,
853 // there is little point in inlining this unless there is literally zero
855 Instruction *Instr = CS.getInstruction();
856 if (InvokeInst *II = dyn_cast<InvokeInst>(Instr)) {
857 if (isa<UnreachableInst>(II->getNormalDest()->begin()))
859 } else if (isa<UnreachableInst>(++BasicBlock::iterator(Instr)))
862 // If this function uses the coldcc calling convention, prefer not to inline
864 if (F.getCallingConv() == CallingConv::Cold)
865 Cost += InlineConstants::ColdccPenalty;
867 // Check if we're done. This can happen due to bonuses and penalties.
868 if (Cost > Threshold)
875 Function *Caller = CS.getInstruction()->getParent()->getParent();
876 // Check if the caller function is recursive itself.
877 for (Value::use_iterator U = Caller->use_begin(), E = Caller->use_end();
879 CallSite Site(cast<Value>(*U));
882 Instruction *I = Site.getInstruction();
883 if (I->getParent()->getParent() == Caller) {
884 IsCallerRecursive = true;
889 // Track whether we've seen a return instruction. The first return
890 // instruction is free, as at least one will usually disappear in inlining.
891 bool HasReturn = false;
893 // Populate our simplified values by mapping from function arguments to call
894 // arguments with known important simplifications.
895 CallSite::arg_iterator CAI = CS.arg_begin();
896 for (Function::arg_iterator FAI = F.arg_begin(), FAE = F.arg_end();
897 FAI != FAE; ++FAI, ++CAI) {
898 assert(CAI != CS.arg_end());
899 if (Constant *C = dyn_cast<Constant>(CAI))
900 SimplifiedValues[FAI] = C;
902 Value *PtrArg = *CAI;
903 if (ConstantInt *C = stripAndComputeInBoundsConstantOffsets(PtrArg)) {
904 ConstantOffsetPtrs[FAI] = std::make_pair(PtrArg, C->getValue());
906 // We can SROA any pointer arguments derived from alloca instructions.
907 if (isa<AllocaInst>(PtrArg)) {
908 SROAArgValues[FAI] = PtrArg;
909 SROAArgCosts[PtrArg] = 0;
913 NumConstantArgs = SimplifiedValues.size();
914 NumConstantOffsetPtrArgs = ConstantOffsetPtrs.size();
915 NumAllocaArgs = SROAArgValues.size();
917 // The worklist of live basic blocks in the callee *after* inlining. We avoid
918 // adding basic blocks of the callee which can be proven to be dead for this
919 // particular call site in order to get more accurate cost estimates. This
920 // requires a somewhat heavyweight iteration pattern: we need to walk the
921 // basic blocks in a breadth-first order as we insert live successors. To
922 // accomplish this, prioritizing for small iterations because we exit after
923 // crossing our threshold, we use a small-size optimized SetVector.
924 typedef SetVector<BasicBlock *, SmallVector<BasicBlock *, 16>,
925 SmallPtrSet<BasicBlock *, 16> > BBSetVector;
926 BBSetVector BBWorklist;
927 BBWorklist.insert(&F.getEntryBlock());
928 // Note that we *must not* cache the size, this loop grows the worklist.
929 for (unsigned Idx = 0; Idx != BBWorklist.size(); ++Idx) {
930 // Bail out the moment we cross the threshold. This means we'll under-count
931 // the cost, but only when undercounting doesn't matter.
932 if (!AlwaysInline && Cost > (Threshold + VectorBonus))
935 BasicBlock *BB = BBWorklist[Idx];
939 // Handle the terminator cost here where we can track returns and other
940 // function-wide constructs.
941 TerminatorInst *TI = BB->getTerminator();
943 // We never want to inline functions that contain an indirectbr. This is
944 // incorrect because all the blockaddress's (in static global initializers
945 // for example) would be referring to the original function, and this
946 // indirect jump would jump from the inlined copy of the function into the
947 // original function which is extremely undefined behavior.
948 // FIXME: This logic isn't really right; we can safely inline functions
949 // with indirectbr's as long as no other function or global references the
950 // blockaddress of a block within the current function. And as a QOI issue,
951 // if someone is using a blockaddress without an indirectbr, and that
952 // reference somehow ends up in another function or global, we probably
953 // don't want to inline this function.
954 if (isa<IndirectBrInst>(TI))
957 if (!HasReturn && isa<ReturnInst>(TI))
960 Cost += InlineConstants::InstrCost;
962 // Analyze the cost of this block. If we blow through the threshold, this
963 // returns false, and we can bail on out.
964 if (!analyzeBlock(BB)) {
965 if (IsRecursiveCall || ExposesReturnsTwice || HasDynamicAlloca)
968 // If the caller is a recursive function then we don't want to inline
969 // functions which allocate a lot of stack space because it would increase
970 // the caller stack usage dramatically.
971 if (IsCallerRecursive &&
972 AllocatedSize > InlineConstants::TotalAllocaSizeRecursiveCaller)
978 // Add in the live successors by first checking whether we have terminator
979 // that may be simplified based on the values simplified by this call.
980 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
981 if (BI->isConditional()) {
982 Value *Cond = BI->getCondition();
983 if (ConstantInt *SimpleCond
984 = dyn_cast_or_null<ConstantInt>(SimplifiedValues.lookup(Cond))) {
985 BBWorklist.insert(BI->getSuccessor(SimpleCond->isZero() ? 1 : 0));
989 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
990 Value *Cond = SI->getCondition();
991 if (ConstantInt *SimpleCond
992 = dyn_cast_or_null<ConstantInt>(SimplifiedValues.lookup(Cond))) {
993 BBWorklist.insert(SI->findCaseValue(SimpleCond).getCaseSuccessor());
998 // If we're unable to select a particular successor, just count all of
1000 for (unsigned TIdx = 0, TSize = TI->getNumSuccessors(); TIdx != TSize;
1002 BBWorklist.insert(TI->getSuccessor(TIdx));
1004 // If we had any successors at this point, than post-inlining is likely to
1005 // have them as well. Note that we assume any basic blocks which existed
1006 // due to branches or switches which folded above will also fold after
1008 if (SingleBB && TI->getNumSuccessors() > 1) {
1009 // Take off the bonus we applied to the threshold.
1010 Threshold -= SingleBBBonus;
1015 Threshold += VectorBonus;
1017 return AlwaysInline || Cost < Threshold;
1020 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1021 /// \brief Dump stats about this call's analysis.
1022 void CallAnalyzer::dump() {
1023 #define DEBUG_PRINT_STAT(x) llvm::dbgs() << " " #x ": " << x << "\n"
1024 DEBUG_PRINT_STAT(NumConstantArgs);
1025 DEBUG_PRINT_STAT(NumConstantOffsetPtrArgs);
1026 DEBUG_PRINT_STAT(NumAllocaArgs);
1027 DEBUG_PRINT_STAT(NumConstantPtrCmps);
1028 DEBUG_PRINT_STAT(NumConstantPtrDiffs);
1029 DEBUG_PRINT_STAT(NumInstructionsSimplified);
1030 DEBUG_PRINT_STAT(SROACostSavings);
1031 DEBUG_PRINT_STAT(SROACostSavingsLost);
1032 #undef DEBUG_PRINT_STAT
1036 InlineCost InlineCostAnalyzer::getInlineCost(CallSite CS, int Threshold) {
1037 return getInlineCost(CS, CS.getCalledFunction(), Threshold);
1040 InlineCost InlineCostAnalyzer::getInlineCost(CallSite CS, Function *Callee,
1042 // Don't inline functions which can be redefined at link-time to mean
1043 // something else. Don't inline functions marked noinline or call sites
1045 if (!Callee || Callee->mayBeOverridden() ||
1046 Callee->getFnAttributes().hasNoInlineAttr() || CS.isNoInline())
1047 return llvm::InlineCost::getNever();
1049 DEBUG(llvm::dbgs() << " Analyzing call of " << Callee->getName()
1052 CallAnalyzer CA(TD, *Callee, Threshold);
1053 bool ShouldInline = CA.analyzeCall(CS);
1057 // Check if there was a reason to force inlining or no inlining.
1058 if (!ShouldInline && CA.getCost() < CA.getThreshold())
1059 return InlineCost::getNever();
1060 if (ShouldInline && CA.getCost() >= CA.getThreshold())
1061 return InlineCost::getAlways();
1063 return llvm::InlineCost::get(CA.getCost(), CA.getThreshold());