llvm.org GIT mirror
13 changed file(s) with 1252 addition(s) and 861 deletion(s).
 19 19 namespace llvm { 20 20 class BasicBlock; 21 21 class Function; 22 class Instruction; 22 23 class TargetData; 23 24 class Value; 25 26 /// \brief Check whether an instruction is likely to be "free" when lowered. 27 bool isInstructionFree(const Instruction *I, const TargetData *TD = 0); 24 28 25 29 /// \brief Check whether a call will lower to something small. 26 30 ///
 15 15 16 16 #include "llvm/Function.h" 17 17 #include "llvm/ADT/DenseMap.h" 18 #include "llvm/ADT/SmallPtrSet.h" 18 19 #include "llvm/ADT/ValueMap.h" 19 20 #include "llvm/Analysis/CodeMetrics.h" 20 21 #include 24 25 namespace llvm { 25 26 26 27 class CallSite; 27 template 28 class SmallPtrSet; 29 28 class TargetData; 30 29 31 30 namespace InlineConstants { 32 31 // Various magic constants used to adjust heuristics. 33 32 const int InstrCost = 5; 34 const int IndirectCallBonus = -100;⏎ 33 const int IndirectCallThreshold = 100;⏎ 35 34 const int CallPenalty = 25; 36 35 const int LastCallToStaticBonus = -15000; 37 36 const int ColdccPenalty = 2000; 38 37 const int NoreturnPenalty = 10000; 39 38 } 40 39 41 /// InlineCost - Represent the cost of inlining a function. This 42 /// supports special values for functions which should "always" or 43 /// "never" be inlined. Otherwise, the cost represents a unitless 44 /// amount; smaller values increase the likelihood of the function 45 /// being inlined.⏎ 40 /// \brief Represents the cost of inlining a function.⏎ 41 /// 42 /// This supports special values for functions which should "always" or 43 /// "never" be inlined. Otherwise, the cost represents a unitless amount; 44 /// smaller values increase the likelihood of the function being inlined. 45 /// 46 /// Objects of this type also provide the adjusted threshold for inlining 47 /// based on the information available for a particular callsite. They can be 48 /// directly tested to determine if inlining should occur given the cost and 49 /// threshold for this cost metric. 46 50 class InlineCost { 47 enum Kind { 48 Value, 49 Always, 50 Never⏎ 51 enum CostKind {⏎ 52 CK_Variable, 53 CK_Always, 54 CK_Never 51 55 }; 52 56 53 // This is a do-it-yourself implementation of 54 // int Cost : 30; 55 // unsigned Type : 2; 56 // We used to use bitfields, but they were sometimes miscompiled (PR3822). 57 enum { TYPE_BITS = 2 }; 58 enum { COST_BITS = unsigned(sizeof(unsigned)) * CHAR_BIT - TYPE_BITS }; 59 unsigned TypedCost; // int Cost : COST_BITS; unsigned Type : TYPE_BITS;⏎ 57 const int Cost : 30; // The inlining cost if neither always nor never.⏎ 58 const unsigned Kind : 2; // The type of cost, one of CostKind above. 60 59 61 Kind getType() const { 62 return Kind(TypedCost >> COST_BITS);⏎ 60 /// \brief The adjusted threshold against which this cost should be tested.⏎ 61 const int Threshold; 62 63 // Trivial constructor, interesting logic in the factory functions below. 64 InlineCost(int Cost, CostKind Kind, int Threshold) 65 : Cost(Cost), Kind(Kind), Threshold(Threshold) {} 66 67 public: 68 static InlineCost get(int Cost, int Threshold) { 69 InlineCost Result(Cost, CK_Variable, Threshold); 70 assert(Result.Cost == Cost && "Cost exceeds InlineCost precision"); 71 return Result; 72 } 73 static InlineCost getAlways() { 74 return InlineCost(0, CK_Always, 0); 75 } 76 static InlineCost getNever() { 77 return InlineCost(0, CK_Never, 0); 63 78 } 64 79 65 int getCost() const { 66 // Sign-extend the bottom COST_BITS bits. 67 return (int(TypedCost << TYPE_BITS)) >> TYPE_BITS;⏎ 80 /// \brief Test whether the inline cost is low enough for inlining.⏎ 81 operator bool() const { 82 if (isAlways()) return true; 83 if (isNever()) return false; 84 return Cost < Threshold; 68 85 } 69 86 70 InlineCost(int C, int T) { 71 TypedCost = (unsigned(C << TYPE_BITS) >> TYPE_BITS) | (T << COST_BITS); 72 assert(getCost() == C && "Cost exceeds InlineCost precision");⏎ 87 bool isVariable() const { return Kind == CK_Variable; }⏎ 88 bool isAlways() const { return Kind == CK_Always; } 89 bool isNever() const { return Kind == CK_Never; } 90 91 /// getCost() - Return a "variable" inline cost's amount. It is 92 /// an error to call this on an "always" or "never" InlineCost. 93 int getCost() const { 94 assert(Kind == CK_Variable && "Invalid access of InlineCost"); 95 return Cost; 73 96 } 74 public: 75 static InlineCost get(int Cost) { return InlineCost(Cost, Value); } 76 static InlineCost getAlways() { return InlineCost(0, Always); } 77 static InlineCost getNever() { return InlineCost(0, Never); } 78 97 79 bool isVariable() const { return getType() == Value; } 80 bool isAlways() const { return getType() == Always; } 81 bool isNever() const { return getType() == Never; } 82 83 /// getValue() - Return a "variable" inline cost's amount. It is 84 /// an error to call this on an "always" or "never" InlineCost. 85 int getValue() const { 86 assert(getType() == Value && "Invalid access of InlineCost"); 87 return getCost();⏎ 98 /// \brief Get the cost delta from the threshold for inlining.⏎ 99 /// Only valid if the cost is of the variable kind. Returns a negative 100 /// value if the cost is too high to inline. 101 int getCostDelta() const { 102 return Threshold - getCost(); 88 103 } 89 104 }; 90 105 91 106 /// InlineCostAnalyzer - Cost analyzer used by inliner. 92 107 class InlineCostAnalyzer { 93 struct ArgInfo { 94 public: 95 unsigned ConstantWeight; 96 unsigned AllocaWeight; 97 98 ArgInfo(unsigned CWeight, unsigned AWeight) 99 : ConstantWeight(CWeight), AllocaWeight(AWeight) 100 {} 101 }; 102 103 struct FunctionInfo { 104 CodeMetrics Metrics; 105 106 /// ArgumentWeights - Each formal argument of the function is inspected to 107 /// see if it is used in any contexts where making it a constant or alloca 108 /// would reduce the code size. If so, we add some value to the argument 109 /// entry here. 110 std::vector ArgumentWeights; 111 112 /// PointerArgPairWeights - Weights to use when giving an inline bonus to 113 /// a call site due to correlated pairs of pointers. 114 DenseMap, unsigned> PointerArgPairWeights; 115 116 /// countCodeReductionForConstant - Figure out an approximation for how 117 /// many instructions will be constant folded if the specified value is 118 /// constant. 119 unsigned countCodeReductionForConstant(const CodeMetrics &Metrics, 120 Value *V); 121 122 /// countCodeReductionForAlloca - Figure out an approximation of how much 123 /// smaller the function will be if it is inlined into a context where an 124 /// argument becomes an alloca. 125 unsigned countCodeReductionForAlloca(const CodeMetrics &Metrics, 126 Value *V); 127 128 /// countCodeReductionForPointerPair - Count the bonus to apply to an 129 /// inline call site where a pair of arguments are pointers and one 130 /// argument is a constant offset from the other. The idea is to 131 /// recognize a common C++ idiom where a begin and end iterator are 132 /// actually pointers, and many operations on the pair of them will be 133 /// constants if the function is called with arguments that have 134 /// a constant offset. 135 void countCodeReductionForPointerPair( 136 const CodeMetrics &Metrics, 137 DenseMap &PointerArgs, 138 Value *V, unsigned ArgIdx); 139 140 /// analyzeFunction - Add information about the specified function 141 /// to the current structure. 142 void analyzeFunction(Function *F, const TargetData *TD); 143 144 /// NeverInline - Returns true if the function should never be 145 /// inlined into any caller. 146 bool NeverInline(); 147 }; 148 149 // The Function* for a function can be changed (by ArgumentPromotion); 150 // the ValueMap will update itself when this happens. 151 ValueMap CachedFunctionInfo; 152 153 108 // TargetData if available, or null. 154 109 const TargetData *TD; 155 110 156 int CountBonusForConstant(Value *V, Constant *C = NULL); 157 int ConstantFunctionBonus(CallSite CS, Constant *C); 158 int getInlineSize(CallSite CS, Function *Callee); 159 int getInlineBonuses(CallSite CS, Function *Callee); 160 111 public: 161 112 InlineCostAnalyzer(): TD(0) {} 162 113 163 114 void setTargetData(const TargetData *TData) { TD = TData; } 164 115 165 /// getInlineCost - The heuristic used to determine if we should inline the 166 /// function call or not.⏎ 116 /// \brief Get an InlineCost object representing the cost of inlining this⏎ 117 /// callsite. 167 118 /// 168 InlineCost getInlineCost(CallSite CS); 169 /// getCalledFunction - The heuristic used to determine if we should inline 170 /// the function call or not. The callee is explicitly specified, to allow 171 /// you to calculate the cost of inlining a function via a pointer. The 172 /// result assumes that the inlined version will always be used. You should 173 /// weight it yourself in cases where this callee will not always be called. 174 InlineCost getInlineCost(CallSite CS, Function *Callee); 175 176 /// getInlineFudgeFactor - Return a > 1.0 factor if the inliner should use a 177 /// higher threshold to determine if the function call should be inlined. 178 float getInlineFudgeFactor(CallSite CS);⏎ 119 /// Note that threshold is passed into this function. Only costs below the⏎ 120 /// threshold are computed with any accuracy. The threshold can be used to 121 /// bound the computation necessary to determine whether the cost is 122 /// sufficiently low to warrant inlining. 123 InlineCost getInlineCost(CallSite CS, int Threshold); 179 124 180 125 /// resetCachedFunctionInfo - erase any cached cost info for this function. 181 126 void resetCachedCostInfo(Function* Caller) { 182 CachedFunctionInfo[Caller] = FunctionInfo(); 183 127 } 184 128 185 129 /// growCachedCostInfo - update the cached cost info for Caller after Callee
 64 64 /// 65 65 virtual InlineCost getInlineCost(CallSite CS) = 0; 66 66 67 // getInlineFudgeFactor - Return a > 1.0 factor if the inliner should use a 68 // higher threshold to determine if the function call should be inlined. 69 /// 70 virtual float getInlineFudgeFactor(CallSite CS) = 0; 71 72 67 /// resetCachedCostInfo - erase any cached cost data from the derived class. 73 68 /// If the derived class has no such data this can be empty. 74 69 ///
 49 49 return false; 50 50 } 51 51 52 bool llvm::isInstructionFree(const Instruction *I, const TargetData *TD) { 53 if (isa(I)) 54 return true; 55 56 // If a GEP has all constant indices, it will probably be folded with 57 // a load/store. 58 if (const GetElementPtrInst *GEP = dyn_cast(I)) 59 return GEP->hasAllConstantIndices(); 60 61 if (const IntrinsicInst *II = dyn_cast(I)) { 62 switch (II->getIntrinsicID()) { 63 default: 64 return false; 65 case Intrinsic::dbg_declare: 66 case Intrinsic::dbg_value: 67 case Intrinsic::invariant_start: 68 case Intrinsic::invariant_end: 69 case Intrinsic::lifetime_start: 70 case Intrinsic::lifetime_end: 71 case Intrinsic::objectsize: 72 case Intrinsic::ptr_annotation: 73 case Intrinsic::var_annotation: 74 // These intrinsics don't count as size. 75 return true; 76 } 77 } 78 79 if (const CastInst *CI = dyn_cast(I)) { 80 // Noop casts, including ptr <-> int, don't count. 81 if (CI->isLosslessCast() || isa(CI) || isa(CI)) 82 return true; 83 // trunc to a native type is free (assuming the target has compare and 84 // shift-right of the same width). 85 if (TD && isa(CI) && 86 TD->isLegalInteger(TD->getTypeSizeInBits(CI->getType()))) 87 return true; 88 // Result of a cmp instruction is often extended (to be used by other 89 // cmp instructions, logical or return instructions). These are usually 90 // nop on most sane targets. 91 if (isa(CI->getOperand(0))) 92 return true; 93 } 94 95 return false; 96 } 97 52 98 /// analyzeBasicBlock - Fill in the current structure with information gleaned 53 99 /// from the specified block. 54 100 void CodeMetrics::analyzeBasicBlock(const BasicBlock *BB, 57 103 unsigned NumInstsBeforeThisBB = NumInsts; 58 104 for (BasicBlock::const_iterator II = BB->begin(), E = BB->end(); 59 105 II != E; ++II) { 60 if (isa(II)) continue; // PHI nodes don't count.⏎ 106 if (isInstructionFree(II, TD))⏎ 107 continue; 61 108 62 109 // Special handling for calls. 63 110 if (isa(II) || isa(II)) { 64 if (const IntrinsicInst *IntrinsicI = dyn_cast(II)) { 65 switch (IntrinsicI->getIntrinsicID()) { 66 default: break; 67 case Intrinsic::dbg_declare: 68 case Intrinsic::dbg_value: 69 case Intrinsic::invariant_start: 70 case Intrinsic::invariant_end: 71 case Intrinsic::lifetime_start: 72 case Intrinsic::lifetime_end: 73 case Intrinsic::objectsize: 74 case Intrinsic::ptr_annotation: 75 case Intrinsic::var_annotation: 76 // These intrinsics don't count as size. 77 continue; 78 } 79 } 80 81 111 ImmutableCallSite CS(cast(II)); 82 112 83 113 if (const Function *F = CS.getCalledFunction()) { 113 143 114 144 if (isa(II) || II->getType()->isVectorTy()) 115 145 ++NumVectorInsts; 116 117 if (const CastInst *CI = dyn_cast(II)) { 118 // Noop casts, including ptr <-> int, don't count. 119 if (CI->isLosslessCast() || isa(CI) || 120 isa(CI)) 121 continue; 122 // trunc to a native type is free (assuming the target has compare and 123 // shift-right of the same width). 124 if (isa(CI) && TD && 125 TD->isLegalInteger(TD->getTypeSizeInBits(CI->getType()))) 126 continue; 127 // Result of a cmp instruction is often extended (to be used by other 128 // cmp instructions, logical or return instructions). These are usually 129 // nop on most sane targets. 130 if (isa(CI->getOperand(0))) 131 continue; 132 } else if (const GetElementPtrInst *GEPI = dyn_cast(II)){ 133 // If a GEP has all constant indices, it will probably be folded with 134 // a load/store. 135 if (GEPI->hasAllConstantIndices()) 136 continue; 137 } 138 146 139 147 ++NumInsts; 140 148 }