Add in the first iteration of support for llvm/clang/lldb to allow variable per addre...
[oota-llvm.git] / lib / Analysis / InlineCost.cpp
1 //===- InlineCost.cpp - Cost analysis for inliner -------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements inline cost analysis.
11 //
12 //===----------------------------------------------------------------------===//
13
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/DataLayout.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"
33
34 using namespace llvm;
35
36 STATISTIC(NumCallsAnalyzed, "Number of call sites analyzed");
37
38 namespace {
39
40 class CallAnalyzer : public InstVisitor<CallAnalyzer, bool> {
41   typedef InstVisitor<CallAnalyzer, bool> Base;
42   friend class InstVisitor<CallAnalyzer, bool>;
43
44   // DataLayout if available, or null.
45   const DataLayout *const TD;
46
47   // The called function.
48   Function &F;
49
50   int Threshold;
51   int Cost;
52   const bool AlwaysInline;
53
54   bool IsCallerRecursive;
55   bool IsRecursiveCall;
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;
62   int VectorBonus;
63
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;
72
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;
76
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;
81
82   // Keep track of values which map to a pointer base and constant offset.
83   DenseMap<Value *, std::pair<Value *, APInt> > ConstantOffsetPtrs;
84
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,
92                           int InstructionCost);
93   bool handleSROACandidate(bool IsSROAValid,
94                            DenseMap<Value *, int>::iterator CostIt,
95                            int InstructionCost);
96   bool isGEPOffsetConstant(GetElementPtrInst &GEP);
97   bool accumulateGEPOffset(GEPOperator &GEP, APInt &Offset);
98   ConstantInt *stripAndComputeInBoundsConstantOffsets(Value *&V);
99
100   // Custom analysis routines.
101   bool analyzeBlock(BasicBlock *BB);
102
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 &);
108
109   // Provide base case for our instruction visit.
110   bool visitInstruction(Instruction &I);
111
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);
127
128 public:
129   CallAnalyzer(const DataLayout *TD, Function &Callee, int Threshold)
130     : TD(TD), F(Callee), Threshold(Threshold), Cost(0),
131       AlwaysInline(F.getFnAttributes().hasAttribute(Attributes::AlwaysInline)),
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) {
139   }
140
141   bool analyzeCall(CallSite CS);
142
143   int getThreshold() { return Threshold; }
144   int getCost() { return Cost; }
145   bool isAlwaysInline() { return AlwaysInline; }
146
147   // Keep a bunch of stats about the cost savings found so we can print them
148   // out when debugging.
149   unsigned NumConstantArgs;
150   unsigned NumConstantOffsetPtrArgs;
151   unsigned NumAllocaArgs;
152   unsigned NumConstantPtrCmps;
153   unsigned NumConstantPtrDiffs;
154   unsigned NumInstructionsSimplified;
155   unsigned SROACostSavings;
156   unsigned SROACostSavingsLost;
157
158   void dump();
159 };
160
161 } // namespace
162
163 /// \brief Test whether the given value is an Alloca-derived function argument.
164 bool CallAnalyzer::isAllocaDerivedArg(Value *V) {
165   return SROAArgValues.count(V);
166 }
167
168 /// \brief Lookup the SROA-candidate argument and cost iterator which V maps to.
169 /// Returns false if V does not map to a SROA-candidate.
170 bool CallAnalyzer::lookupSROAArgAndCost(
171     Value *V, Value *&Arg, DenseMap<Value *, int>::iterator &CostIt) {
172   if (SROAArgValues.empty() || SROAArgCosts.empty())
173     return false;
174
175   DenseMap<Value *, Value *>::iterator ArgIt = SROAArgValues.find(V);
176   if (ArgIt == SROAArgValues.end())
177     return false;
178
179   Arg = ArgIt->second;
180   CostIt = SROAArgCosts.find(Arg);
181   return CostIt != SROAArgCosts.end();
182 }
183
184 /// \brief Disable SROA for the candidate marked by this cost iterator.
185 ///
186 /// This marks the candidate as no longer viable for SROA, and adds the cost
187 /// savings associated with it back into the inline cost measurement.
188 void CallAnalyzer::disableSROA(DenseMap<Value *, int>::iterator CostIt) {
189   // If we're no longer able to perform SROA we need to undo its cost savings
190   // and prevent subsequent analysis.
191   Cost += CostIt->second;
192   SROACostSavings -= CostIt->second;
193   SROACostSavingsLost += CostIt->second;
194   SROAArgCosts.erase(CostIt);
195 }
196
197 /// \brief If 'V' maps to a SROA candidate, disable SROA for it.
198 void CallAnalyzer::disableSROA(Value *V) {
199   Value *SROAArg;
200   DenseMap<Value *, int>::iterator CostIt;
201   if (lookupSROAArgAndCost(V, SROAArg, CostIt))
202     disableSROA(CostIt);
203 }
204
205 /// \brief Accumulate the given cost for a particular SROA candidate.
206 void CallAnalyzer::accumulateSROACost(DenseMap<Value *, int>::iterator CostIt,
207                                       int InstructionCost) {
208   CostIt->second += InstructionCost;
209   SROACostSavings += InstructionCost;
210 }
211
212 /// \brief Helper for the common pattern of handling a SROA candidate.
213 /// Either accumulates the cost savings if the SROA remains valid, or disables
214 /// SROA for the candidate.
215 bool CallAnalyzer::handleSROACandidate(bool IsSROAValid,
216                                        DenseMap<Value *, int>::iterator CostIt,
217                                        int InstructionCost) {
218   if (IsSROAValid) {
219     accumulateSROACost(CostIt, InstructionCost);
220     return true;
221   }
222
223   disableSROA(CostIt);
224   return false;
225 }
226
227 /// \brief Check whether a GEP's indices are all constant.
228 ///
229 /// Respects any simplified values known during the analysis of this callsite.
230 bool CallAnalyzer::isGEPOffsetConstant(GetElementPtrInst &GEP) {
231   for (User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end(); I != E; ++I)
232     if (!isa<Constant>(*I) && !SimplifiedValues.lookup(*I))
233       return false;
234
235   return true;
236 }
237
238 /// \brief Accumulate a constant GEP offset into an APInt if possible.
239 ///
240 /// Returns false if unable to compute the offset for any reason. Respects any
241 /// simplified values known during the analysis of this callsite.
242 bool CallAnalyzer::accumulateGEPOffset(GEPOperator &GEP, APInt &Offset) {
243   if (!TD)
244     return false;
245
246   unsigned AS = GEP.getPointerAddressSpace();
247   unsigned IntPtrWidth = TD->getPointerSizeInBits(AS);
248   assert(IntPtrWidth == Offset.getBitWidth());
249
250   for (gep_type_iterator GTI = gep_type_begin(GEP), GTE = gep_type_end(GEP);
251        GTI != GTE; ++GTI) {
252     ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand());
253     if (!OpC)
254       if (Constant *SimpleOp = SimplifiedValues.lookup(GTI.getOperand()))
255         OpC = dyn_cast<ConstantInt>(SimpleOp);
256     if (!OpC)
257       return false;
258     if (OpC->isZero()) continue;
259
260     // Handle a struct index, which adds its field offset to the pointer.
261     if (StructType *STy = dyn_cast<StructType>(*GTI)) {
262       unsigned ElementIdx = OpC->getZExtValue();
263       const StructLayout *SL = TD->getStructLayout(STy);
264       Offset += APInt(IntPtrWidth, SL->getElementOffset(ElementIdx));
265       continue;
266     }
267
268     APInt TypeSize(IntPtrWidth, TD->getTypeAllocSize(GTI.getIndexedType()));
269     Offset += OpC->getValue().sextOrTrunc(IntPtrWidth) * TypeSize;
270   }
271   return true;
272 }
273
274 bool CallAnalyzer::visitAlloca(AllocaInst &I) {
275   // FIXME: Check whether inlining will turn a dynamic alloca into a static
276   // alloca, and handle that case.
277
278   // Accumulate the allocated size.
279   if (I.isStaticAlloca()) {
280     Type *Ty = I.getAllocatedType();
281     AllocatedSize += (TD ? TD->getTypeAllocSize(Ty) :
282                       Ty->getPrimitiveSizeInBits());
283   }
284
285   // We will happily inline static alloca instructions or dynamic alloca
286   // instructions in always-inline situations.
287   if (AlwaysInline || I.isStaticAlloca())
288     return Base::visitAlloca(I);
289
290   // FIXME: This is overly conservative. Dynamic allocas are inefficient for
291   // a variety of reasons, and so we would like to not inline them into
292   // functions which don't currently have a dynamic alloca. This simply
293   // disables inlining altogether in the presence of a dynamic alloca.
294   HasDynamicAlloca = true;
295   return false;
296 }
297
298 bool CallAnalyzer::visitPHI(PHINode &I) {
299   // FIXME: We should potentially be tracking values through phi nodes,
300   // especially when they collapse to a single value due to deleted CFG edges
301   // during inlining.
302
303   // FIXME: We need to propagate SROA *disabling* through phi nodes, even
304   // though we don't want to propagate it's bonuses. The idea is to disable
305   // SROA if it *might* be used in an inappropriate manner.
306
307   // Phi nodes are always zero-cost.
308   return true;
309 }
310
311 bool CallAnalyzer::visitGetElementPtr(GetElementPtrInst &I) {
312   Value *SROAArg;
313   DenseMap<Value *, int>::iterator CostIt;
314   bool SROACandidate = lookupSROAArgAndCost(I.getPointerOperand(),
315                                             SROAArg, CostIt);
316
317   // Try to fold GEPs of constant-offset call site argument pointers. This
318   // requires target data and inbounds GEPs.
319   if (TD && I.isInBounds()) {
320     // Check if we have a base + offset for the pointer.
321     Value *Ptr = I.getPointerOperand();
322     std::pair<Value *, APInt> BaseAndOffset = ConstantOffsetPtrs.lookup(Ptr);
323     if (BaseAndOffset.first) {
324       // Check if the offset of this GEP is constant, and if so accumulate it
325       // into Offset.
326       if (!accumulateGEPOffset(cast<GEPOperator>(I), BaseAndOffset.second)) {
327         // Non-constant GEPs aren't folded, and disable SROA.
328         if (SROACandidate)
329           disableSROA(CostIt);
330         return false;
331       }
332
333       // Add the result as a new mapping to Base + Offset.
334       ConstantOffsetPtrs[&I] = BaseAndOffset;
335
336       // Also handle SROA candidates here, we already know that the GEP is
337       // all-constant indexed.
338       if (SROACandidate)
339         SROAArgValues[&I] = SROAArg;
340
341       return true;
342     }
343   }
344
345   if (isGEPOffsetConstant(I)) {
346     if (SROACandidate)
347       SROAArgValues[&I] = SROAArg;
348
349     // Constant GEPs are modeled as free.
350     return true;
351   }
352
353   // Variable GEPs will require math and will disable SROA.
354   if (SROACandidate)
355     disableSROA(CostIt);
356   return false;
357 }
358
359 bool CallAnalyzer::visitBitCast(BitCastInst &I) {
360   // Propagate constants through bitcasts.
361   if (Constant *COp = dyn_cast<Constant>(I.getOperand(0)))
362     if (Constant *C = ConstantExpr::getBitCast(COp, I.getType())) {
363       SimplifiedValues[&I] = C;
364       return true;
365     }
366
367   // Track base/offsets through casts
368   std::pair<Value *, APInt> BaseAndOffset
369     = ConstantOffsetPtrs.lookup(I.getOperand(0));
370   // Casts don't change the offset, just wrap it up.
371   if (BaseAndOffset.first)
372     ConstantOffsetPtrs[&I] = BaseAndOffset;
373
374   // Also look for SROA candidates here.
375   Value *SROAArg;
376   DenseMap<Value *, int>::iterator CostIt;
377   if (lookupSROAArgAndCost(I.getOperand(0), SROAArg, CostIt))
378     SROAArgValues[&I] = SROAArg;
379
380   // Bitcasts are always zero cost.
381   return true;
382 }
383
384 bool CallAnalyzer::visitPtrToInt(PtrToIntInst &I) {
385   // Propagate constants through ptrtoint.
386   if (Constant *COp = dyn_cast<Constant>(I.getOperand(0)))
387     if (Constant *C = ConstantExpr::getPtrToInt(COp, I.getType())) {
388       SimplifiedValues[&I] = C;
389       return true;
390     }
391
392   // Track base/offset pairs when converted to a plain integer provided the
393   // integer is large enough to represent the pointer.
394   unsigned IntegerSize = I.getType()->getScalarSizeInBits();
395   unsigned AS = I.getPointerAddressSpace();
396   if (TD && IntegerSize >= TD->getPointerSizeInBits(AS)) {
397     std::pair<Value *, APInt> BaseAndOffset
398       = ConstantOffsetPtrs.lookup(I.getOperand(0));
399     if (BaseAndOffset.first)
400       ConstantOffsetPtrs[&I] = BaseAndOffset;
401   }
402
403   // This is really weird. Technically, ptrtoint will disable SROA. However,
404   // unless that ptrtoint is *used* somewhere in the live basic blocks after
405   // inlining, it will be nuked, and SROA should proceed. All of the uses which
406   // would block SROA would also block SROA if applied directly to a pointer,
407   // and so we can just add the integer in here. The only places where SROA is
408   // preserved either cannot fire on an integer, or won't in-and-of themselves
409   // disable SROA (ext) w/o some later use that we would see and disable.
410   Value *SROAArg;
411   DenseMap<Value *, int>::iterator CostIt;
412   if (lookupSROAArgAndCost(I.getOperand(0), SROAArg, CostIt))
413     SROAArgValues[&I] = SROAArg;
414
415   return isInstructionFree(&I, TD);
416 }
417
418 bool CallAnalyzer::visitIntToPtr(IntToPtrInst &I) {
419   // Propagate constants through ptrtoint.
420   if (Constant *COp = dyn_cast<Constant>(I.getOperand(0)))
421     if (Constant *C = ConstantExpr::getIntToPtr(COp, I.getType())) {
422       SimplifiedValues[&I] = C;
423       return true;
424     }
425
426   // Track base/offset pairs when round-tripped through a pointer without
427   // modifications provided the integer is not too large.
428   Value *Op = I.getOperand(0);
429   unsigned IntegerSize = Op->getType()->getScalarSizeInBits();
430   unsigned AS = I.getAddressSpace();
431   if (TD && IntegerSize <= TD->getPointerSizeInBits(AS)) {
432     std::pair<Value *, APInt> BaseAndOffset = ConstantOffsetPtrs.lookup(Op);
433     if (BaseAndOffset.first)
434       ConstantOffsetPtrs[&I] = BaseAndOffset;
435   }
436
437   // "Propagate" SROA here in the same manner as we do for ptrtoint above.
438   Value *SROAArg;
439   DenseMap<Value *, int>::iterator CostIt;
440   if (lookupSROAArgAndCost(Op, SROAArg, CostIt))
441     SROAArgValues[&I] = SROAArg;
442
443   return isInstructionFree(&I, TD);
444 }
445
446 bool CallAnalyzer::visitCastInst(CastInst &I) {
447   // Propagate constants through ptrtoint.
448   if (Constant *COp = dyn_cast<Constant>(I.getOperand(0)))
449     if (Constant *C = ConstantExpr::getCast(I.getOpcode(), COp, I.getType())) {
450       SimplifiedValues[&I] = C;
451       return true;
452     }
453
454   // Disable SROA in the face of arbitrary casts we don't whitelist elsewhere.
455   disableSROA(I.getOperand(0));
456
457   return isInstructionFree(&I, TD);
458 }
459
460 bool CallAnalyzer::visitUnaryInstruction(UnaryInstruction &I) {
461   Value *Operand = I.getOperand(0);
462   Constant *Ops[1] = { dyn_cast<Constant>(Operand) };
463   if (Ops[0] || (Ops[0] = SimplifiedValues.lookup(Operand)))
464     if (Constant *C = ConstantFoldInstOperands(I.getOpcode(), I.getType(),
465                                                Ops, TD)) {
466       SimplifiedValues[&I] = C;
467       return true;
468     }
469
470   // Disable any SROA on the argument to arbitrary unary operators.
471   disableSROA(Operand);
472
473   return false;
474 }
475
476 bool CallAnalyzer::visitICmp(ICmpInst &I) {
477   Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
478   // First try to handle simplified comparisons.
479   if (!isa<Constant>(LHS))
480     if (Constant *SimpleLHS = SimplifiedValues.lookup(LHS))
481       LHS = SimpleLHS;
482   if (!isa<Constant>(RHS))
483     if (Constant *SimpleRHS = SimplifiedValues.lookup(RHS))
484       RHS = SimpleRHS;
485   if (Constant *CLHS = dyn_cast<Constant>(LHS))
486     if (Constant *CRHS = dyn_cast<Constant>(RHS))
487       if (Constant *C = ConstantExpr::getICmp(I.getPredicate(), CLHS, CRHS)) {
488         SimplifiedValues[&I] = C;
489         return true;
490       }
491
492   // Otherwise look for a comparison between constant offset pointers with
493   // a common base.
494   Value *LHSBase, *RHSBase;
495   APInt LHSOffset, RHSOffset;
496   llvm::tie(LHSBase, LHSOffset) = ConstantOffsetPtrs.lookup(LHS);
497   if (LHSBase) {
498     llvm::tie(RHSBase, RHSOffset) = ConstantOffsetPtrs.lookup(RHS);
499     if (RHSBase && LHSBase == RHSBase) {
500       // We have common bases, fold the icmp to a constant based on the
501       // offsets.
502       Constant *CLHS = ConstantInt::get(LHS->getContext(), LHSOffset);
503       Constant *CRHS = ConstantInt::get(RHS->getContext(), RHSOffset);
504       if (Constant *C = ConstantExpr::getICmp(I.getPredicate(), CLHS, CRHS)) {
505         SimplifiedValues[&I] = C;
506         ++NumConstantPtrCmps;
507         return true;
508       }
509     }
510   }
511
512   // If the comparison is an equality comparison with null, we can simplify it
513   // for any alloca-derived argument.
514   if (I.isEquality() && isa<ConstantPointerNull>(I.getOperand(1)))
515     if (isAllocaDerivedArg(I.getOperand(0))) {
516       // We can actually predict the result of comparisons between an
517       // alloca-derived value and null. Note that this fires regardless of
518       // SROA firing.
519       bool IsNotEqual = I.getPredicate() == CmpInst::ICMP_NE;
520       SimplifiedValues[&I] = IsNotEqual ? ConstantInt::getTrue(I.getType())
521                                         : ConstantInt::getFalse(I.getType());
522       return true;
523     }
524
525   // Finally check for SROA candidates in comparisons.
526   Value *SROAArg;
527   DenseMap<Value *, int>::iterator CostIt;
528   if (lookupSROAArgAndCost(I.getOperand(0), SROAArg, CostIt)) {
529     if (isa<ConstantPointerNull>(I.getOperand(1))) {
530       accumulateSROACost(CostIt, InlineConstants::InstrCost);
531       return true;
532     }
533
534     disableSROA(CostIt);
535   }
536
537   return false;
538 }
539
540 bool CallAnalyzer::visitSub(BinaryOperator &I) {
541   // Try to handle a special case: we can fold computing the difference of two
542   // constant-related pointers.
543   Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
544   Value *LHSBase, *RHSBase;
545   APInt LHSOffset, RHSOffset;
546   llvm::tie(LHSBase, LHSOffset) = ConstantOffsetPtrs.lookup(LHS);
547   if (LHSBase) {
548     llvm::tie(RHSBase, RHSOffset) = ConstantOffsetPtrs.lookup(RHS);
549     if (RHSBase && LHSBase == RHSBase) {
550       // We have common bases, fold the subtract to a constant based on the
551       // offsets.
552       Constant *CLHS = ConstantInt::get(LHS->getContext(), LHSOffset);
553       Constant *CRHS = ConstantInt::get(RHS->getContext(), RHSOffset);
554       if (Constant *C = ConstantExpr::getSub(CLHS, CRHS)) {
555         SimplifiedValues[&I] = C;
556         ++NumConstantPtrDiffs;
557         return true;
558       }
559     }
560   }
561
562   // Otherwise, fall back to the generic logic for simplifying and handling
563   // instructions.
564   return Base::visitSub(I);
565 }
566
567 bool CallAnalyzer::visitBinaryOperator(BinaryOperator &I) {
568   Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
569   if (!isa<Constant>(LHS))
570     if (Constant *SimpleLHS = SimplifiedValues.lookup(LHS))
571       LHS = SimpleLHS;
572   if (!isa<Constant>(RHS))
573     if (Constant *SimpleRHS = SimplifiedValues.lookup(RHS))
574       RHS = SimpleRHS;
575   Value *SimpleV = SimplifyBinOp(I.getOpcode(), LHS, RHS, TD);
576   if (Constant *C = dyn_cast_or_null<Constant>(SimpleV)) {
577     SimplifiedValues[&I] = C;
578     return true;
579   }
580
581   // Disable any SROA on arguments to arbitrary, unsimplified binary operators.
582   disableSROA(LHS);
583   disableSROA(RHS);
584
585   return false;
586 }
587
588 bool CallAnalyzer::visitLoad(LoadInst &I) {
589   Value *SROAArg;
590   DenseMap<Value *, int>::iterator CostIt;
591   if (lookupSROAArgAndCost(I.getOperand(0), SROAArg, CostIt)) {
592     if (I.isSimple()) {
593       accumulateSROACost(CostIt, InlineConstants::InstrCost);
594       return true;
595     }
596
597     disableSROA(CostIt);
598   }
599
600   return false;
601 }
602
603 bool CallAnalyzer::visitStore(StoreInst &I) {
604   Value *SROAArg;
605   DenseMap<Value *, int>::iterator CostIt;
606   if (lookupSROAArgAndCost(I.getOperand(0), SROAArg, CostIt)) {
607     if (I.isSimple()) {
608       accumulateSROACost(CostIt, InlineConstants::InstrCost);
609       return true;
610     }
611
612     disableSROA(CostIt);
613   }
614
615   return false;
616 }
617
618 bool CallAnalyzer::visitCallSite(CallSite CS) {
619   if (CS.isCall() && cast<CallInst>(CS.getInstruction())->canReturnTwice() &&
620       !F.getFnAttributes().hasAttribute(Attributes::ReturnsTwice)) {
621     // This aborts the entire analysis.
622     ExposesReturnsTwice = true;
623     return false;
624   }
625
626   if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CS.getInstruction())) {
627     switch (II->getIntrinsicID()) {
628     default:
629       return Base::visitCallSite(CS);
630
631     case Intrinsic::memset:
632     case Intrinsic::memcpy:
633     case Intrinsic::memmove:
634       // SROA can usually chew through these intrinsics, but they aren't free.
635       return false;
636     }
637   }
638
639   if (Function *F = CS.getCalledFunction()) {
640     if (F == CS.getInstruction()->getParent()->getParent()) {
641       // This flag will fully abort the analysis, so don't bother with anything
642       // else.
643       IsRecursiveCall = true;
644       return false;
645     }
646
647     if (!callIsSmall(CS)) {
648       // We account for the average 1 instruction per call argument setup
649       // here.
650       Cost += CS.arg_size() * InlineConstants::InstrCost;
651
652       // Everything other than inline ASM will also have a significant cost
653       // merely from making the call.
654       if (!isa<InlineAsm>(CS.getCalledValue()))
655         Cost += InlineConstants::CallPenalty;
656     }
657
658     return Base::visitCallSite(CS);
659   }
660
661   // Otherwise we're in a very special case -- an indirect function call. See
662   // if we can be particularly clever about this.
663   Value *Callee = CS.getCalledValue();
664
665   // First, pay the price of the argument setup. We account for the average
666   // 1 instruction per call argument setup here.
667   Cost += CS.arg_size() * InlineConstants::InstrCost;
668
669   // Next, check if this happens to be an indirect function call to a known
670   // function in this inline context. If not, we've done all we can.
671   Function *F = dyn_cast_or_null<Function>(SimplifiedValues.lookup(Callee));
672   if (!F)
673     return Base::visitCallSite(CS);
674
675   // If we have a constant that we are calling as a function, we can peer
676   // through it and see the function target. This happens not infrequently
677   // during devirtualization and so we want to give it a hefty bonus for
678   // inlining, but cap that bonus in the event that inlining wouldn't pan
679   // out. Pretend to inline the function, with a custom threshold.
680   CallAnalyzer CA(TD, *F, InlineConstants::IndirectCallThreshold);
681   if (CA.analyzeCall(CS)) {
682     // We were able to inline the indirect call! Subtract the cost from the
683     // bonus we want to apply, but don't go below zero.
684     Cost -= std::max(0, InlineConstants::IndirectCallThreshold - CA.getCost());
685   }
686
687   return Base::visitCallSite(CS);
688 }
689
690 bool CallAnalyzer::visitInstruction(Instruction &I) {
691   // Some instructions are free. All of the free intrinsics can also be
692   // handled by SROA, etc.
693   if (isInstructionFree(&I, TD))
694     return true;
695
696   // We found something we don't understand or can't handle. Mark any SROA-able
697   // values in the operand list as no longer viable.
698   for (User::op_iterator OI = I.op_begin(), OE = I.op_end(); OI != OE; ++OI)
699     disableSROA(*OI);
700
701   return false;
702 }
703
704
705 /// \brief Analyze a basic block for its contribution to the inline cost.
706 ///
707 /// This method walks the analyzer over every instruction in the given basic
708 /// block and accounts for their cost during inlining at this callsite. It
709 /// aborts early if the threshold has been exceeded or an impossible to inline
710 /// construct has been detected. It returns false if inlining is no longer
711 /// viable, and true if inlining remains viable.
712 bool CallAnalyzer::analyzeBlock(BasicBlock *BB) {
713   for (BasicBlock::iterator I = BB->begin(), E = llvm::prior(BB->end());
714        I != E; ++I) {
715     ++NumInstructions;
716     if (isa<ExtractElementInst>(I) || I->getType()->isVectorTy())
717       ++NumVectorInstructions;
718
719     // If the instruction simplified to a constant, there is no cost to this
720     // instruction. Visit the instructions using our InstVisitor to account for
721     // all of the per-instruction logic. The visit tree returns true if we
722     // consumed the instruction in any way, and false if the instruction's base
723     // cost should count against inlining.
724     if (Base::visit(I))
725       ++NumInstructionsSimplified;
726     else
727       Cost += InlineConstants::InstrCost;
728
729     // If the visit this instruction detected an uninlinable pattern, abort.
730     if (IsRecursiveCall || ExposesReturnsTwice || HasDynamicAlloca)
731       return false;
732
733     // If the caller is a recursive function then we don't want to inline
734     // functions which allocate a lot of stack space because it would increase
735     // the caller stack usage dramatically.
736     if (IsCallerRecursive &&
737         AllocatedSize > InlineConstants::TotalAllocaSizeRecursiveCaller)
738       return false;
739
740     if (NumVectorInstructions > NumInstructions/2)
741       VectorBonus = FiftyPercentVectorBonus;
742     else if (NumVectorInstructions > NumInstructions/10)
743       VectorBonus = TenPercentVectorBonus;
744     else
745       VectorBonus = 0;
746
747     // Check if we've past the threshold so we don't spin in huge basic
748     // blocks that will never inline.
749     if (!AlwaysInline && Cost > (Threshold + VectorBonus))
750       return false;
751   }
752
753   return true;
754 }
755
756 /// \brief Compute the base pointer and cumulative constant offsets for V.
757 ///
758 /// This strips all constant offsets off of V, leaving it the base pointer, and
759 /// accumulates the total constant offset applied in the returned constant. It
760 /// returns 0 if V is not a pointer, and returns the constant '0' if there are
761 /// no constant offsets applied.
762 ConstantInt *CallAnalyzer::stripAndComputeInBoundsConstantOffsets(Value *&V) {
763   if (!TD || !V->getType()->isPointerTy())
764     return 0;
765
766   unsigned AS = cast<PointerType>(V->getType())->getAddressSpace();;
767   unsigned IntPtrWidth = TD->getPointerSizeInBits(AS);
768   APInt Offset = APInt::getNullValue(IntPtrWidth);
769
770   // Even though we don't look through PHI nodes, we could be called on an
771   // instruction in an unreachable block, which may be on a cycle.
772   SmallPtrSet<Value *, 4> Visited;
773   Visited.insert(V);
774   do {
775     if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
776       if (!GEP->isInBounds() || !accumulateGEPOffset(*GEP, Offset))
777         return 0;
778       V = GEP->getPointerOperand();
779     } else if (Operator::getOpcode(V) == Instruction::BitCast) {
780       V = cast<Operator>(V)->getOperand(0);
781     } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
782       if (GA->mayBeOverridden())
783         break;
784       V = GA->getAliasee();
785     } else {
786       break;
787     }
788     assert(V->getType()->isPointerTy() && "Unexpected operand type!");
789   } while (Visited.insert(V));
790
791   Type *IntPtrTy = TD->getIntPtrType(V->getContext());
792   return cast<ConstantInt>(ConstantInt::get(IntPtrTy, Offset));
793 }
794
795 /// \brief Analyze a call site for potential inlining.
796 ///
797 /// Returns true if inlining this call is viable, and false if it is not
798 /// viable. It computes the cost and adjusts the threshold based on numerous
799 /// factors and heuristics. If this method returns false but the computed cost
800 /// is below the computed threshold, then inlining was forcibly disabled by
801 /// some artifact of the rountine.
802 bool CallAnalyzer::analyzeCall(CallSite CS) {
803   ++NumCallsAnalyzed;
804
805   // Track whether the post-inlining function would have more than one basic
806   // block. A single basic block is often intended for inlining. Balloon the
807   // threshold by 50% until we pass the single-BB phase.
808   bool SingleBB = true;
809   int SingleBBBonus = Threshold / 2;
810   Threshold += SingleBBBonus;
811
812   // Unless we are always-inlining, perform some tweaks to the cost and
813   // threshold based on the direct callsite information.
814   if (!AlwaysInline) {
815     // We want to more aggressively inline vector-dense kernels, so up the
816     // threshold, and we'll lower it if the % of vector instructions gets too
817     // low.
818     assert(NumInstructions == 0);
819     assert(NumVectorInstructions == 0);
820     FiftyPercentVectorBonus = Threshold;
821     TenPercentVectorBonus = Threshold / 2;
822
823     // Give out bonuses per argument, as the instructions setting them up will
824     // be gone after inlining.
825     for (unsigned I = 0, E = CS.arg_size(); I != E; ++I) {
826       if (TD && CS.isByValArgument(I)) {
827         // We approximate the number of loads and stores needed by dividing the
828         // size of the byval type by the target's pointer size.
829         PointerType *PTy = cast<PointerType>(CS.getArgument(I)->getType());
830         unsigned TypeSize = TD->getTypeSizeInBits(PTy->getElementType());
831         unsigned AS = PTy->getAddressSpace();
832         unsigned PointerSize = TD->getPointerSizeInBits(AS);
833         // Ceiling division.
834         unsigned NumStores = (TypeSize + PointerSize - 1) / PointerSize;
835
836         // If it generates more than 8 stores it is likely to be expanded as an
837         // inline memcpy so we take that as an upper bound. Otherwise we assume
838         // one load and one store per word copied.
839         // FIXME: The maxStoresPerMemcpy setting from the target should be used
840         // here instead of a magic number of 8, but it's not available via
841         // DataLayout.
842         NumStores = std::min(NumStores, 8U);
843
844         Cost -= 2 * NumStores * InlineConstants::InstrCost;
845       } else {
846         // For non-byval arguments subtract off one instruction per call
847         // argument.
848         Cost -= InlineConstants::InstrCost;
849       }
850     }
851
852     // If there is only one call of the function, and it has internal linkage,
853     // the cost of inlining it drops dramatically.
854     if (F.hasLocalLinkage() && F.hasOneUse() && &F == CS.getCalledFunction())
855       Cost += InlineConstants::LastCallToStaticBonus;
856
857     // If the instruction after the call, or if the normal destination of the
858     // invoke is an unreachable instruction, the function is noreturn. As such,
859     // there is little point in inlining this unless there is literally zero
860     // cost.
861     Instruction *Instr = CS.getInstruction();
862     if (InvokeInst *II = dyn_cast<InvokeInst>(Instr)) {
863       if (isa<UnreachableInst>(II->getNormalDest()->begin()))
864         Threshold = 1;
865     } else if (isa<UnreachableInst>(++BasicBlock::iterator(Instr)))
866       Threshold = 1;
867
868     // If this function uses the coldcc calling convention, prefer not to inline
869     // it.
870     if (F.getCallingConv() == CallingConv::Cold)
871       Cost += InlineConstants::ColdccPenalty;
872
873     // Check if we're done. This can happen due to bonuses and penalties.
874     if (Cost > Threshold)
875       return false;
876   }
877
878   if (F.empty())
879     return true;
880
881   Function *Caller = CS.getInstruction()->getParent()->getParent();
882   // Check if the caller function is recursive itself.
883   for (Value::use_iterator U = Caller->use_begin(), E = Caller->use_end();
884        U != E; ++U) {
885     CallSite Site(cast<Value>(*U));
886     if (!Site)
887       continue;
888     Instruction *I = Site.getInstruction();
889     if (I->getParent()->getParent() == Caller) {
890       IsCallerRecursive = true;
891       break;
892     }
893   }
894
895   // Track whether we've seen a return instruction. The first return
896   // instruction is free, as at least one will usually disappear in inlining.
897   bool HasReturn = false;
898
899   // Populate our simplified values by mapping from function arguments to call
900   // arguments with known important simplifications.
901   CallSite::arg_iterator CAI = CS.arg_begin();
902   for (Function::arg_iterator FAI = F.arg_begin(), FAE = F.arg_end();
903        FAI != FAE; ++FAI, ++CAI) {
904     assert(CAI != CS.arg_end());
905     if (Constant *C = dyn_cast<Constant>(CAI))
906       SimplifiedValues[FAI] = C;
907
908     Value *PtrArg = *CAI;
909     if (ConstantInt *C = stripAndComputeInBoundsConstantOffsets(PtrArg)) {
910       ConstantOffsetPtrs[FAI] = std::make_pair(PtrArg, C->getValue());
911
912       // We can SROA any pointer arguments derived from alloca instructions.
913       if (isa<AllocaInst>(PtrArg)) {
914         SROAArgValues[FAI] = PtrArg;
915         SROAArgCosts[PtrArg] = 0;
916       }
917     }
918   }
919   NumConstantArgs = SimplifiedValues.size();
920   NumConstantOffsetPtrArgs = ConstantOffsetPtrs.size();
921   NumAllocaArgs = SROAArgValues.size();
922
923   // The worklist of live basic blocks in the callee *after* inlining. We avoid
924   // adding basic blocks of the callee which can be proven to be dead for this
925   // particular call site in order to get more accurate cost estimates. This
926   // requires a somewhat heavyweight iteration pattern: we need to walk the
927   // basic blocks in a breadth-first order as we insert live successors. To
928   // accomplish this, prioritizing for small iterations because we exit after
929   // crossing our threshold, we use a small-size optimized SetVector.
930   typedef SetVector<BasicBlock *, SmallVector<BasicBlock *, 16>,
931                                   SmallPtrSet<BasicBlock *, 16> > BBSetVector;
932   BBSetVector BBWorklist;
933   BBWorklist.insert(&F.getEntryBlock());
934   // Note that we *must not* cache the size, this loop grows the worklist.
935   for (unsigned Idx = 0; Idx != BBWorklist.size(); ++Idx) {
936     // Bail out the moment we cross the threshold. This means we'll under-count
937     // the cost, but only when undercounting doesn't matter.
938     if (!AlwaysInline && Cost > (Threshold + VectorBonus))
939       break;
940
941     BasicBlock *BB = BBWorklist[Idx];
942     if (BB->empty())
943       continue;
944
945     // Handle the terminator cost here where we can track returns and other
946     // function-wide constructs.
947     TerminatorInst *TI = BB->getTerminator();
948
949     // We never want to inline functions that contain an indirectbr.  This is
950     // incorrect because all the blockaddress's (in static global initializers
951     // for example) would be referring to the original function, and this
952     // indirect jump would jump from the inlined copy of the function into the 
953     // original function which is extremely undefined behavior.
954     // FIXME: This logic isn't really right; we can safely inline functions
955     // with indirectbr's as long as no other function or global references the
956     // blockaddress of a block within the current function.  And as a QOI issue,
957     // if someone is using a blockaddress without an indirectbr, and that
958     // reference somehow ends up in another function or global, we probably
959     // don't want to inline this function.
960     if (isa<IndirectBrInst>(TI))
961       return false;
962
963     if (!HasReturn && isa<ReturnInst>(TI))
964       HasReturn = true;
965     else
966       Cost += InlineConstants::InstrCost;
967
968     // Analyze the cost of this block. If we blow through the threshold, this
969     // returns false, and we can bail on out.
970     if (!analyzeBlock(BB)) {
971       if (IsRecursiveCall || ExposesReturnsTwice || HasDynamicAlloca)
972         return false;
973
974       // If the caller is a recursive function then we don't want to inline
975       // functions which allocate a lot of stack space because it would increase
976       // the caller stack usage dramatically.
977       if (IsCallerRecursive &&
978           AllocatedSize > InlineConstants::TotalAllocaSizeRecursiveCaller)
979         return false;
980
981       break;
982     }
983
984     // Add in the live successors by first checking whether we have terminator
985     // that may be simplified based on the values simplified by this call.
986     if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
987       if (BI->isConditional()) {
988         Value *Cond = BI->getCondition();
989         if (ConstantInt *SimpleCond
990               = dyn_cast_or_null<ConstantInt>(SimplifiedValues.lookup(Cond))) {
991           BBWorklist.insert(BI->getSuccessor(SimpleCond->isZero() ? 1 : 0));
992           continue;
993         }
994       }
995     } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
996       Value *Cond = SI->getCondition();
997       if (ConstantInt *SimpleCond
998             = dyn_cast_or_null<ConstantInt>(SimplifiedValues.lookup(Cond))) {
999         BBWorklist.insert(SI->findCaseValue(SimpleCond).getCaseSuccessor());
1000         continue;
1001       }
1002     }
1003
1004     // If we're unable to select a particular successor, just count all of
1005     // them.
1006     for (unsigned TIdx = 0, TSize = TI->getNumSuccessors(); TIdx != TSize;
1007          ++TIdx)
1008       BBWorklist.insert(TI->getSuccessor(TIdx));
1009
1010     // If we had any successors at this point, than post-inlining is likely to
1011     // have them as well. Note that we assume any basic blocks which existed
1012     // due to branches or switches which folded above will also fold after
1013     // inlining.
1014     if (SingleBB && TI->getNumSuccessors() > 1) {
1015       // Take off the bonus we applied to the threshold.
1016       Threshold -= SingleBBBonus;
1017       SingleBB = false;
1018     }
1019   }
1020
1021   Threshold += VectorBonus;
1022
1023   return AlwaysInline || Cost < Threshold;
1024 }
1025
1026 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1027 /// \brief Dump stats about this call's analysis.
1028 void CallAnalyzer::dump() {
1029 #define DEBUG_PRINT_STAT(x) llvm::dbgs() << "      " #x ": " << x << "\n"
1030   DEBUG_PRINT_STAT(NumConstantArgs);
1031   DEBUG_PRINT_STAT(NumConstantOffsetPtrArgs);
1032   DEBUG_PRINT_STAT(NumAllocaArgs);
1033   DEBUG_PRINT_STAT(NumConstantPtrCmps);
1034   DEBUG_PRINT_STAT(NumConstantPtrDiffs);
1035   DEBUG_PRINT_STAT(NumInstructionsSimplified);
1036   DEBUG_PRINT_STAT(SROACostSavings);
1037   DEBUG_PRINT_STAT(SROACostSavingsLost);
1038 #undef DEBUG_PRINT_STAT
1039 }
1040 #endif
1041
1042 InlineCost InlineCostAnalyzer::getInlineCost(CallSite CS, int Threshold) {
1043   return getInlineCost(CS, CS.getCalledFunction(), Threshold);
1044 }
1045
1046 InlineCost InlineCostAnalyzer::getInlineCost(CallSite CS, Function *Callee,
1047                                              int Threshold) {
1048   // Don't inline functions which can be redefined at link-time to mean
1049   // something else.  Don't inline functions marked noinline or call sites
1050   // marked noinline.
1051   if (!Callee || Callee->mayBeOverridden() ||
1052       Callee->getFnAttributes().hasAttribute(Attributes::NoInline) ||
1053       CS.isNoInline())
1054     return llvm::InlineCost::getNever();
1055
1056   DEBUG(llvm::dbgs() << "      Analyzing call of " << Callee->getName()
1057         << "...\n");
1058
1059   CallAnalyzer CA(TD, *Callee, Threshold);
1060   bool ShouldInline = CA.analyzeCall(CS);
1061
1062   DEBUG(CA.dump());
1063
1064   // Check if there was a reason to force inlining or no inlining.
1065   if (!ShouldInline && CA.getCost() < CA.getThreshold())
1066     return InlineCost::getNever();
1067   if (ShouldInline && (CA.isAlwaysInline() ||
1068                        CA.getCost() >= CA.getThreshold()))
1069     return InlineCost::getAlways();
1070
1071   return llvm::InlineCost::get(CA.getCost(), CA.getThreshold());
1072 }