Dan Gohman | 4552e3c | 2009-10-13 18:30:07 +0000 | [diff] [blame] | 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 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 14 | #define DEBUG_TYPE "inline-cost" |
Dan Gohman | 4552e3c | 2009-10-13 18:30:07 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/InlineCost.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/STLExtras.h" |
| 17 | #include "llvm/ADT/SetVector.h" |
| 18 | #include "llvm/ADT/SmallPtrSet.h" |
| 19 | #include "llvm/ADT/SmallVector.h" |
| 20 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/ConstantFolding.h" |
| 22 | #include "llvm/Analysis/InstructionSimplify.h" |
Chandler Carruth | 42f3dce | 2013-01-21 11:55:09 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 24 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 25 | #include "llvm/IR/CallingConv.h" |
| 26 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 03eb0de | 2014-03-04 10:40:04 +0000 | [diff] [blame] | 27 | #include "llvm/IR/GetElementPtrTypeIterator.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 28 | #include "llvm/IR/GlobalAlias.h" |
Chandler Carruth | 7da14f1 | 2014-03-06 03:23:41 +0000 | [diff] [blame] | 29 | #include "llvm/IR/InstVisitor.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 30 | #include "llvm/IR/IntrinsicInst.h" |
| 31 | #include "llvm/IR/Operator.h" |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Debug.h" |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 33 | #include "llvm/Support/raw_ostream.h" |
Eric Christopher | 2dfbd7e | 2011-02-05 00:49:15 +0000 | [diff] [blame] | 34 | |
Dan Gohman | 4552e3c | 2009-10-13 18:30:07 +0000 | [diff] [blame] | 35 | using namespace llvm; |
| 36 | |
Chandler Carruth | 7ae90d4 | 2012-04-11 10:15:10 +0000 | [diff] [blame] | 37 | STATISTIC(NumCallsAnalyzed, "Number of call sites analyzed"); |
| 38 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 39 | namespace { |
Chandler Carruth | a308955 | 2012-03-14 07:32:53 +0000 | [diff] [blame] | 40 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 41 | class CallAnalyzer : public InstVisitor<CallAnalyzer, bool> { |
| 42 | typedef InstVisitor<CallAnalyzer, bool> Base; |
| 43 | friend class InstVisitor<CallAnalyzer, bool>; |
Owen Anderson | a08318a | 2010-09-09 16:56:42 +0000 | [diff] [blame] | 44 | |
Micah Villmow | cdfe20b | 2012-10-08 16:38:25 +0000 | [diff] [blame] | 45 | // DataLayout if available, or null. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 46 | const DataLayout *const DL; |
Owen Anderson | a08318a | 2010-09-09 16:56:42 +0000 | [diff] [blame] | 47 | |
Chandler Carruth | 42f3dce | 2013-01-21 11:55:09 +0000 | [diff] [blame] | 48 | /// The TargetTransformInfo available for this compilation. |
| 49 | const TargetTransformInfo &TTI; |
| 50 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 51 | // The called function. |
| 52 | Function &F; |
Owen Anderson | a08318a | 2010-09-09 16:56:42 +0000 | [diff] [blame] | 53 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 54 | int Threshold; |
| 55 | int Cost; |
Owen Anderson | a08318a | 2010-09-09 16:56:42 +0000 | [diff] [blame] | 56 | |
Nadav Rotem | 4eb3d4b | 2012-09-19 08:08:04 +0000 | [diff] [blame] | 57 | bool IsCallerRecursive; |
| 58 | bool IsRecursiveCall; |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 59 | bool ExposesReturnsTwice; |
| 60 | bool HasDynamicAlloca; |
James Molloy | 4f6fb95 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 61 | bool ContainsNoDuplicateCall; |
Chandler Carruth | 0814d2a | 2013-12-13 07:59:56 +0000 | [diff] [blame] | 62 | bool HasReturn; |
| 63 | bool HasIndirectBr; |
James Molloy | 4f6fb95 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 64 | |
Nadav Rotem | 4eb3d4b | 2012-09-19 08:08:04 +0000 | [diff] [blame] | 65 | /// Number of bytes allocated statically by the callee. |
| 66 | uint64_t AllocatedSize; |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 67 | unsigned NumInstructions, NumVectorInstructions; |
| 68 | int FiftyPercentVectorBonus, TenPercentVectorBonus; |
| 69 | int VectorBonus; |
| 70 | |
| 71 | // While we walk the potentially-inlined instructions, we build up and |
| 72 | // maintain a mapping of simplified values specific to this callsite. The |
| 73 | // idea is to propagate any special information we have about arguments to |
| 74 | // this call through the inlinable section of the function, and account for |
| 75 | // likely simplifications post-inlining. The most important aspect we track |
| 76 | // is CFG altering simplifications -- when we prove a basic block dead, that |
| 77 | // can cause dramatic shifts in the cost of inlining a function. |
| 78 | DenseMap<Value *, Constant *> SimplifiedValues; |
| 79 | |
| 80 | // Keep track of the values which map back (through function arguments) to |
| 81 | // allocas on the caller stack which could be simplified through SROA. |
| 82 | DenseMap<Value *, Value *> SROAArgValues; |
| 83 | |
| 84 | // The mapping of caller Alloca values to their accumulated cost savings. If |
| 85 | // we have to disable SROA for one of the allocas, this tells us how much |
| 86 | // cost must be added. |
| 87 | DenseMap<Value *, int> SROAArgCosts; |
| 88 | |
| 89 | // Keep track of values which map to a pointer base and constant offset. |
| 90 | DenseMap<Value *, std::pair<Value *, APInt> > ConstantOffsetPtrs; |
| 91 | |
| 92 | // Custom simplification helper routines. |
| 93 | bool isAllocaDerivedArg(Value *V); |
| 94 | bool lookupSROAArgAndCost(Value *V, Value *&Arg, |
| 95 | DenseMap<Value *, int>::iterator &CostIt); |
| 96 | void disableSROA(DenseMap<Value *, int>::iterator CostIt); |
| 97 | void disableSROA(Value *V); |
| 98 | void accumulateSROACost(DenseMap<Value *, int>::iterator CostIt, |
| 99 | int InstructionCost); |
| 100 | bool handleSROACandidate(bool IsSROAValid, |
| 101 | DenseMap<Value *, int>::iterator CostIt, |
| 102 | int InstructionCost); |
| 103 | bool isGEPOffsetConstant(GetElementPtrInst &GEP); |
| 104 | bool accumulateGEPOffset(GEPOperator &GEP, APInt &Offset); |
Chandler Carruth | 753e21d | 2012-12-28 14:23:32 +0000 | [diff] [blame] | 105 | bool simplifyCallSite(Function *F, CallSite CS); |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 106 | ConstantInt *stripAndComputeInBoundsConstantOffsets(Value *&V); |
| 107 | |
| 108 | // Custom analysis routines. |
| 109 | bool analyzeBlock(BasicBlock *BB); |
| 110 | |
| 111 | // Disable several entry points to the visitor so we don't accidentally use |
| 112 | // them by declaring but not defining them here. |
| 113 | void visit(Module *); void visit(Module &); |
| 114 | void visit(Function *); void visit(Function &); |
| 115 | void visit(BasicBlock *); void visit(BasicBlock &); |
| 116 | |
| 117 | // Provide base case for our instruction visit. |
| 118 | bool visitInstruction(Instruction &I); |
| 119 | |
| 120 | // Our visit overrides. |
| 121 | bool visitAlloca(AllocaInst &I); |
| 122 | bool visitPHI(PHINode &I); |
| 123 | bool visitGetElementPtr(GetElementPtrInst &I); |
| 124 | bool visitBitCast(BitCastInst &I); |
| 125 | bool visitPtrToInt(PtrToIntInst &I); |
| 126 | bool visitIntToPtr(IntToPtrInst &I); |
| 127 | bool visitCastInst(CastInst &I); |
| 128 | bool visitUnaryInstruction(UnaryInstruction &I); |
Matt Arsenault | 727aa34 | 2013-07-20 04:09:00 +0000 | [diff] [blame] | 129 | bool visitCmpInst(CmpInst &I); |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 130 | bool visitSub(BinaryOperator &I); |
| 131 | bool visitBinaryOperator(BinaryOperator &I); |
| 132 | bool visitLoad(LoadInst &I); |
| 133 | bool visitStore(StoreInst &I); |
Chandler Carruth | 753e21d | 2012-12-28 14:23:32 +0000 | [diff] [blame] | 134 | bool visitExtractValue(ExtractValueInst &I); |
| 135 | bool visitInsertValue(InsertValueInst &I); |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 136 | bool visitCallSite(CallSite CS); |
Chandler Carruth | 0814d2a | 2013-12-13 07:59:56 +0000 | [diff] [blame] | 137 | bool visitReturnInst(ReturnInst &RI); |
| 138 | bool visitBranchInst(BranchInst &BI); |
| 139 | bool visitSwitchInst(SwitchInst &SI); |
| 140 | bool visitIndirectBrInst(IndirectBrInst &IBI); |
| 141 | bool visitResumeInst(ResumeInst &RI); |
| 142 | bool visitUnreachableInst(UnreachableInst &I); |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 143 | |
| 144 | public: |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 145 | CallAnalyzer(const DataLayout *DL, const TargetTransformInfo &TTI, |
Chandler Carruth | 42f3dce | 2013-01-21 11:55:09 +0000 | [diff] [blame] | 146 | Function &Callee, int Threshold) |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 147 | : DL(DL), TTI(TTI), F(Callee), Threshold(Threshold), Cost(0), |
Chandler Carruth | 42f3dce | 2013-01-21 11:55:09 +0000 | [diff] [blame] | 148 | IsCallerRecursive(false), IsRecursiveCall(false), |
| 149 | ExposesReturnsTwice(false), HasDynamicAlloca(false), |
Chandler Carruth | 0814d2a | 2013-12-13 07:59:56 +0000 | [diff] [blame] | 150 | ContainsNoDuplicateCall(false), HasReturn(false), HasIndirectBr(false), |
| 151 | AllocatedSize(0), NumInstructions(0), NumVectorInstructions(0), |
| 152 | FiftyPercentVectorBonus(0), TenPercentVectorBonus(0), VectorBonus(0), |
| 153 | NumConstantArgs(0), NumConstantOffsetPtrArgs(0), NumAllocaArgs(0), |
| 154 | NumConstantPtrCmps(0), NumConstantPtrDiffs(0), |
| 155 | NumInstructionsSimplified(0), SROACostSavings(0), |
| 156 | SROACostSavingsLost(0) {} |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 157 | |
| 158 | bool analyzeCall(CallSite CS); |
| 159 | |
| 160 | int getThreshold() { return Threshold; } |
| 161 | int getCost() { return Cost; } |
| 162 | |
| 163 | // Keep a bunch of stats about the cost savings found so we can print them |
| 164 | // out when debugging. |
| 165 | unsigned NumConstantArgs; |
| 166 | unsigned NumConstantOffsetPtrArgs; |
| 167 | unsigned NumAllocaArgs; |
| 168 | unsigned NumConstantPtrCmps; |
| 169 | unsigned NumConstantPtrDiffs; |
| 170 | unsigned NumInstructionsSimplified; |
| 171 | unsigned SROACostSavings; |
| 172 | unsigned SROACostSavingsLost; |
| 173 | |
| 174 | void dump(); |
| 175 | }; |
| 176 | |
| 177 | } // namespace |
| 178 | |
| 179 | /// \brief Test whether the given value is an Alloca-derived function argument. |
| 180 | bool CallAnalyzer::isAllocaDerivedArg(Value *V) { |
| 181 | return SROAArgValues.count(V); |
Owen Anderson | a08318a | 2010-09-09 16:56:42 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 184 | /// \brief Lookup the SROA-candidate argument and cost iterator which V maps to. |
| 185 | /// Returns false if V does not map to a SROA-candidate. |
| 186 | bool CallAnalyzer::lookupSROAArgAndCost( |
| 187 | Value *V, Value *&Arg, DenseMap<Value *, int>::iterator &CostIt) { |
| 188 | if (SROAArgValues.empty() || SROAArgCosts.empty()) |
| 189 | return false; |
Chandler Carruth | 783b719 | 2012-03-09 02:49:36 +0000 | [diff] [blame] | 190 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 191 | DenseMap<Value *, Value *>::iterator ArgIt = SROAArgValues.find(V); |
| 192 | if (ArgIt == SROAArgValues.end()) |
| 193 | return false; |
Chandler Carruth | 783b719 | 2012-03-09 02:49:36 +0000 | [diff] [blame] | 194 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 195 | Arg = ArgIt->second; |
| 196 | CostIt = SROAArgCosts.find(Arg); |
| 197 | return CostIt != SROAArgCosts.end(); |
Chandler Carruth | 783b719 | 2012-03-09 02:49:36 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 200 | /// \brief Disable SROA for the candidate marked by this cost iterator. |
Chandler Carruth | 783b719 | 2012-03-09 02:49:36 +0000 | [diff] [blame] | 201 | /// |
Benjamin Kramer | bde9176 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 202 | /// This marks the candidate as no longer viable for SROA, and adds the cost |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 203 | /// savings associated with it back into the inline cost measurement. |
| 204 | void CallAnalyzer::disableSROA(DenseMap<Value *, int>::iterator CostIt) { |
| 205 | // If we're no longer able to perform SROA we need to undo its cost savings |
| 206 | // and prevent subsequent analysis. |
| 207 | Cost += CostIt->second; |
| 208 | SROACostSavings -= CostIt->second; |
| 209 | SROACostSavingsLost += CostIt->second; |
| 210 | SROAArgCosts.erase(CostIt); |
| 211 | } |
| 212 | |
| 213 | /// \brief If 'V' maps to a SROA candidate, disable SROA for it. |
| 214 | void CallAnalyzer::disableSROA(Value *V) { |
| 215 | Value *SROAArg; |
| 216 | DenseMap<Value *, int>::iterator CostIt; |
| 217 | if (lookupSROAArgAndCost(V, SROAArg, CostIt)) |
| 218 | disableSROA(CostIt); |
| 219 | } |
| 220 | |
| 221 | /// \brief Accumulate the given cost for a particular SROA candidate. |
| 222 | void CallAnalyzer::accumulateSROACost(DenseMap<Value *, int>::iterator CostIt, |
| 223 | int InstructionCost) { |
| 224 | CostIt->second += InstructionCost; |
| 225 | SROACostSavings += InstructionCost; |
| 226 | } |
| 227 | |
| 228 | /// \brief Helper for the common pattern of handling a SROA candidate. |
| 229 | /// Either accumulates the cost savings if the SROA remains valid, or disables |
| 230 | /// SROA for the candidate. |
| 231 | bool CallAnalyzer::handleSROACandidate(bool IsSROAValid, |
| 232 | DenseMap<Value *, int>::iterator CostIt, |
| 233 | int InstructionCost) { |
| 234 | if (IsSROAValid) { |
| 235 | accumulateSROACost(CostIt, InstructionCost); |
| 236 | return true; |
| 237 | } |
| 238 | |
| 239 | disableSROA(CostIt); |
| 240 | return false; |
| 241 | } |
| 242 | |
| 243 | /// \brief Check whether a GEP's indices are all constant. |
| 244 | /// |
| 245 | /// Respects any simplified values known during the analysis of this callsite. |
| 246 | bool CallAnalyzer::isGEPOffsetConstant(GetElementPtrInst &GEP) { |
| 247 | for (User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end(); I != E; ++I) |
| 248 | if (!isa<Constant>(*I) && !SimplifiedValues.lookup(*I)) |
Chandler Carruth | 783b719 | 2012-03-09 02:49:36 +0000 | [diff] [blame] | 249 | return false; |
Chandler Carruth | 783b719 | 2012-03-09 02:49:36 +0000 | [diff] [blame] | 250 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 251 | return true; |
| 252 | } |
| 253 | |
| 254 | /// \brief Accumulate a constant GEP offset into an APInt if possible. |
| 255 | /// |
| 256 | /// Returns false if unable to compute the offset for any reason. Respects any |
| 257 | /// simplified values known during the analysis of this callsite. |
| 258 | bool CallAnalyzer::accumulateGEPOffset(GEPOperator &GEP, APInt &Offset) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 259 | if (!DL) |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 260 | return false; |
| 261 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 262 | unsigned IntPtrWidth = DL->getPointerSizeInBits(); |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 263 | assert(IntPtrWidth == Offset.getBitWidth()); |
| 264 | |
| 265 | for (gep_type_iterator GTI = gep_type_begin(GEP), GTE = gep_type_end(GEP); |
| 266 | GTI != GTE; ++GTI) { |
| 267 | ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand()); |
| 268 | if (!OpC) |
| 269 | if (Constant *SimpleOp = SimplifiedValues.lookup(GTI.getOperand())) |
| 270 | OpC = dyn_cast<ConstantInt>(SimpleOp); |
| 271 | if (!OpC) |
Chandler Carruth | 783b719 | 2012-03-09 02:49:36 +0000 | [diff] [blame] | 272 | return false; |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 273 | if (OpC->isZero()) continue; |
Chandler Carruth | 783b719 | 2012-03-09 02:49:36 +0000 | [diff] [blame] | 274 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 275 | // Handle a struct index, which adds its field offset to the pointer. |
| 276 | if (StructType *STy = dyn_cast<StructType>(*GTI)) { |
| 277 | unsigned ElementIdx = OpC->getZExtValue(); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 278 | const StructLayout *SL = DL->getStructLayout(STy); |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 279 | Offset += APInt(IntPtrWidth, SL->getElementOffset(ElementIdx)); |
| 280 | continue; |
Chandler Carruth | 783b719 | 2012-03-09 02:49:36 +0000 | [diff] [blame] | 281 | } |
Chandler Carruth | 783b719 | 2012-03-09 02:49:36 +0000 | [diff] [blame] | 282 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 283 | APInt TypeSize(IntPtrWidth, DL->getTypeAllocSize(GTI.getIndexedType())); |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 284 | Offset += OpC->getValue().sextOrTrunc(IntPtrWidth) * TypeSize; |
| 285 | } |
| 286 | return true; |
| 287 | } |
| 288 | |
| 289 | bool CallAnalyzer::visitAlloca(AllocaInst &I) { |
Eric Christopher | beb2cd6 | 2014-04-07 13:36:21 +0000 | [diff] [blame] | 290 | // Check whether inlining will turn a dynamic alloca into a static |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 291 | // alloca, and handle that case. |
Eric Christopher | beb2cd6 | 2014-04-07 13:36:21 +0000 | [diff] [blame] | 292 | if (I.isArrayAllocation()) { |
| 293 | if (Constant *Size = SimplifiedValues.lookup(I.getArraySize())) { |
| 294 | ConstantInt *AllocSize = dyn_cast<ConstantInt>(Size); |
| 295 | assert(AllocSize && "Allocation size not a constant int?"); |
| 296 | Type *Ty = I.getAllocatedType(); |
| 297 | AllocatedSize += Ty->getPrimitiveSizeInBits() * AllocSize->getZExtValue(); |
| 298 | return Base::visitAlloca(I); |
| 299 | } |
| 300 | } |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 301 | |
Nadav Rotem | 4eb3d4b | 2012-09-19 08:08:04 +0000 | [diff] [blame] | 302 | // Accumulate the allocated size. |
| 303 | if (I.isStaticAlloca()) { |
| 304 | Type *Ty = I.getAllocatedType(); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 305 | AllocatedSize += (DL ? DL->getTypeAllocSize(Ty) : |
Nadav Rotem | 4eb3d4b | 2012-09-19 08:08:04 +0000 | [diff] [blame] | 306 | Ty->getPrimitiveSizeInBits()); |
| 307 | } |
| 308 | |
Bob Wilson | a5b0dc8 | 2012-11-19 07:04:35 +0000 | [diff] [blame] | 309 | // We will happily inline static alloca instructions. |
| 310 | if (I.isStaticAlloca()) |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 311 | return Base::visitAlloca(I); |
| 312 | |
| 313 | // FIXME: This is overly conservative. Dynamic allocas are inefficient for |
| 314 | // a variety of reasons, and so we would like to not inline them into |
| 315 | // functions which don't currently have a dynamic alloca. This simply |
| 316 | // disables inlining altogether in the presence of a dynamic alloca. |
| 317 | HasDynamicAlloca = true; |
| 318 | return false; |
| 319 | } |
| 320 | |
| 321 | bool CallAnalyzer::visitPHI(PHINode &I) { |
| 322 | // FIXME: We should potentially be tracking values through phi nodes, |
| 323 | // especially when they collapse to a single value due to deleted CFG edges |
| 324 | // during inlining. |
| 325 | |
| 326 | // FIXME: We need to propagate SROA *disabling* through phi nodes, even |
| 327 | // though we don't want to propagate it's bonuses. The idea is to disable |
| 328 | // SROA if it *might* be used in an inappropriate manner. |
| 329 | |
| 330 | // Phi nodes are always zero-cost. |
| 331 | return true; |
| 332 | } |
| 333 | |
| 334 | bool CallAnalyzer::visitGetElementPtr(GetElementPtrInst &I) { |
| 335 | Value *SROAArg; |
| 336 | DenseMap<Value *, int>::iterator CostIt; |
| 337 | bool SROACandidate = lookupSROAArgAndCost(I.getPointerOperand(), |
| 338 | SROAArg, CostIt); |
| 339 | |
| 340 | // Try to fold GEPs of constant-offset call site argument pointers. This |
| 341 | // requires target data and inbounds GEPs. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 342 | if (DL && I.isInBounds()) { |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 343 | // Check if we have a base + offset for the pointer. |
| 344 | Value *Ptr = I.getPointerOperand(); |
| 345 | std::pair<Value *, APInt> BaseAndOffset = ConstantOffsetPtrs.lookup(Ptr); |
| 346 | if (BaseAndOffset.first) { |
| 347 | // Check if the offset of this GEP is constant, and if so accumulate it |
| 348 | // into Offset. |
| 349 | if (!accumulateGEPOffset(cast<GEPOperator>(I), BaseAndOffset.second)) { |
| 350 | // Non-constant GEPs aren't folded, and disable SROA. |
| 351 | if (SROACandidate) |
| 352 | disableSROA(CostIt); |
| 353 | return false; |
| 354 | } |
| 355 | |
| 356 | // Add the result as a new mapping to Base + Offset. |
| 357 | ConstantOffsetPtrs[&I] = BaseAndOffset; |
| 358 | |
| 359 | // Also handle SROA candidates here, we already know that the GEP is |
| 360 | // all-constant indexed. |
| 361 | if (SROACandidate) |
| 362 | SROAArgValues[&I] = SROAArg; |
| 363 | |
Chandler Carruth | 783b719 | 2012-03-09 02:49:36 +0000 | [diff] [blame] | 364 | return true; |
| 365 | } |
| 366 | } |
| 367 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 368 | if (isGEPOffsetConstant(I)) { |
| 369 | if (SROACandidate) |
| 370 | SROAArgValues[&I] = SROAArg; |
| 371 | |
| 372 | // Constant GEPs are modeled as free. |
| 373 | return true; |
| 374 | } |
| 375 | |
| 376 | // Variable GEPs will require math and will disable SROA. |
| 377 | if (SROACandidate) |
| 378 | disableSROA(CostIt); |
Chandler Carruth | 783b719 | 2012-03-09 02:49:36 +0000 | [diff] [blame] | 379 | return false; |
| 380 | } |
| 381 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 382 | bool CallAnalyzer::visitBitCast(BitCastInst &I) { |
| 383 | // Propagate constants through bitcasts. |
Chandler Carruth | 86ed530 | 2012-12-28 14:43:42 +0000 | [diff] [blame] | 384 | Constant *COp = dyn_cast<Constant>(I.getOperand(0)); |
| 385 | if (!COp) |
| 386 | COp = SimplifiedValues.lookup(I.getOperand(0)); |
| 387 | if (COp) |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 388 | if (Constant *C = ConstantExpr::getBitCast(COp, I.getType())) { |
| 389 | SimplifiedValues[&I] = C; |
| 390 | return true; |
Owen Anderson | a08318a | 2010-09-09 16:56:42 +0000 | [diff] [blame] | 391 | } |
Owen Anderson | a08318a | 2010-09-09 16:56:42 +0000 | [diff] [blame] | 392 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 393 | // Track base/offsets through casts |
| 394 | std::pair<Value *, APInt> BaseAndOffset |
| 395 | = ConstantOffsetPtrs.lookup(I.getOperand(0)); |
| 396 | // Casts don't change the offset, just wrap it up. |
| 397 | if (BaseAndOffset.first) |
| 398 | ConstantOffsetPtrs[&I] = BaseAndOffset; |
| 399 | |
| 400 | // Also look for SROA candidates here. |
| 401 | Value *SROAArg; |
| 402 | DenseMap<Value *, int>::iterator CostIt; |
| 403 | if (lookupSROAArgAndCost(I.getOperand(0), SROAArg, CostIt)) |
| 404 | SROAArgValues[&I] = SROAArg; |
| 405 | |
| 406 | // Bitcasts are always zero cost. |
| 407 | return true; |
Owen Anderson | a08318a | 2010-09-09 16:56:42 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 410 | bool CallAnalyzer::visitPtrToInt(PtrToIntInst &I) { |
Rafael Espindola | 339430f | 2014-02-25 23:25:17 +0000 | [diff] [blame] | 411 | const DataLayout *DL = I.getDataLayout(); |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 412 | // Propagate constants through ptrtoint. |
Chandler Carruth | 86ed530 | 2012-12-28 14:43:42 +0000 | [diff] [blame] | 413 | Constant *COp = dyn_cast<Constant>(I.getOperand(0)); |
| 414 | if (!COp) |
| 415 | COp = SimplifiedValues.lookup(I.getOperand(0)); |
| 416 | if (COp) |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 417 | if (Constant *C = ConstantExpr::getPtrToInt(COp, I.getType())) { |
| 418 | SimplifiedValues[&I] = C; |
| 419 | return true; |
Chandler Carruth | 4d1d34f | 2012-03-14 23:19:53 +0000 | [diff] [blame] | 420 | } |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 421 | |
| 422 | // Track base/offset pairs when converted to a plain integer provided the |
| 423 | // integer is large enough to represent the pointer. |
| 424 | unsigned IntegerSize = I.getType()->getScalarSizeInBits(); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 425 | if (DL && IntegerSize >= DL->getPointerSizeInBits()) { |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 426 | std::pair<Value *, APInt> BaseAndOffset |
| 427 | = ConstantOffsetPtrs.lookup(I.getOperand(0)); |
| 428 | if (BaseAndOffset.first) |
| 429 | ConstantOffsetPtrs[&I] = BaseAndOffset; |
| 430 | } |
| 431 | |
| 432 | // This is really weird. Technically, ptrtoint will disable SROA. However, |
| 433 | // unless that ptrtoint is *used* somewhere in the live basic blocks after |
| 434 | // inlining, it will be nuked, and SROA should proceed. All of the uses which |
| 435 | // would block SROA would also block SROA if applied directly to a pointer, |
| 436 | // and so we can just add the integer in here. The only places where SROA is |
| 437 | // preserved either cannot fire on an integer, or won't in-and-of themselves |
| 438 | // disable SROA (ext) w/o some later use that we would see and disable. |
| 439 | Value *SROAArg; |
| 440 | DenseMap<Value *, int>::iterator CostIt; |
| 441 | if (lookupSROAArgAndCost(I.getOperand(0), SROAArg, CostIt)) |
| 442 | SROAArgValues[&I] = SROAArg; |
| 443 | |
Chandler Carruth | b8cf510 | 2013-01-21 12:05:16 +0000 | [diff] [blame] | 444 | return TargetTransformInfo::TCC_Free == TTI.getUserCost(&I); |
Chandler Carruth | 4d1d34f | 2012-03-14 23:19:53 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 447 | bool CallAnalyzer::visitIntToPtr(IntToPtrInst &I) { |
Rafael Espindola | 339430f | 2014-02-25 23:25:17 +0000 | [diff] [blame] | 448 | const DataLayout *DL = I.getDataLayout(); |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 449 | // Propagate constants through ptrtoint. |
Chandler Carruth | 86ed530 | 2012-12-28 14:43:42 +0000 | [diff] [blame] | 450 | Constant *COp = dyn_cast<Constant>(I.getOperand(0)); |
| 451 | if (!COp) |
| 452 | COp = SimplifiedValues.lookup(I.getOperand(0)); |
| 453 | if (COp) |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 454 | if (Constant *C = ConstantExpr::getIntToPtr(COp, I.getType())) { |
| 455 | SimplifiedValues[&I] = C; |
| 456 | return true; |
| 457 | } |
Dan Gohman | 4552e3c | 2009-10-13 18:30:07 +0000 | [diff] [blame] | 458 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 459 | // Track base/offset pairs when round-tripped through a pointer without |
| 460 | // modifications provided the integer is not too large. |
| 461 | Value *Op = I.getOperand(0); |
| 462 | unsigned IntegerSize = Op->getType()->getScalarSizeInBits(); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 463 | if (DL && IntegerSize <= DL->getPointerSizeInBits()) { |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 464 | std::pair<Value *, APInt> BaseAndOffset = ConstantOffsetPtrs.lookup(Op); |
| 465 | if (BaseAndOffset.first) |
| 466 | ConstantOffsetPtrs[&I] = BaseAndOffset; |
| 467 | } |
Dan Gohman | 4552e3c | 2009-10-13 18:30:07 +0000 | [diff] [blame] | 468 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 469 | // "Propagate" SROA here in the same manner as we do for ptrtoint above. |
| 470 | Value *SROAArg; |
| 471 | DenseMap<Value *, int>::iterator CostIt; |
| 472 | if (lookupSROAArgAndCost(Op, SROAArg, CostIt)) |
| 473 | SROAArgValues[&I] = SROAArg; |
Chandler Carruth | 4d1d34f | 2012-03-14 23:19:53 +0000 | [diff] [blame] | 474 | |
Chandler Carruth | b8cf510 | 2013-01-21 12:05:16 +0000 | [diff] [blame] | 475 | return TargetTransformInfo::TCC_Free == TTI.getUserCost(&I); |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | bool CallAnalyzer::visitCastInst(CastInst &I) { |
| 479 | // Propagate constants through ptrtoint. |
Chandler Carruth | 86ed530 | 2012-12-28 14:43:42 +0000 | [diff] [blame] | 480 | Constant *COp = dyn_cast<Constant>(I.getOperand(0)); |
| 481 | if (!COp) |
| 482 | COp = SimplifiedValues.lookup(I.getOperand(0)); |
| 483 | if (COp) |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 484 | if (Constant *C = ConstantExpr::getCast(I.getOpcode(), COp, I.getType())) { |
| 485 | SimplifiedValues[&I] = C; |
| 486 | return true; |
| 487 | } |
| 488 | |
| 489 | // Disable SROA in the face of arbitrary casts we don't whitelist elsewhere. |
| 490 | disableSROA(I.getOperand(0)); |
| 491 | |
Chandler Carruth | b8cf510 | 2013-01-21 12:05:16 +0000 | [diff] [blame] | 492 | return TargetTransformInfo::TCC_Free == TTI.getUserCost(&I); |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | bool CallAnalyzer::visitUnaryInstruction(UnaryInstruction &I) { |
| 496 | Value *Operand = I.getOperand(0); |
Jakub Staszak | 7b9e0b9 | 2013-03-07 20:01:19 +0000 | [diff] [blame] | 497 | Constant *COp = dyn_cast<Constant>(Operand); |
| 498 | if (!COp) |
| 499 | COp = SimplifiedValues.lookup(Operand); |
| 500 | if (COp) |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 501 | if (Constant *C = ConstantFoldInstOperands(I.getOpcode(), I.getType(), |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 502 | COp, DL)) { |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 503 | SimplifiedValues[&I] = C; |
| 504 | return true; |
| 505 | } |
| 506 | |
| 507 | // Disable any SROA on the argument to arbitrary unary operators. |
| 508 | disableSROA(Operand); |
| 509 | |
| 510 | return false; |
| 511 | } |
| 512 | |
Matt Arsenault | 727aa34 | 2013-07-20 04:09:00 +0000 | [diff] [blame] | 513 | bool CallAnalyzer::visitCmpInst(CmpInst &I) { |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 514 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
| 515 | // First try to handle simplified comparisons. |
| 516 | if (!isa<Constant>(LHS)) |
| 517 | if (Constant *SimpleLHS = SimplifiedValues.lookup(LHS)) |
| 518 | LHS = SimpleLHS; |
| 519 | if (!isa<Constant>(RHS)) |
| 520 | if (Constant *SimpleRHS = SimplifiedValues.lookup(RHS)) |
| 521 | RHS = SimpleRHS; |
Matt Arsenault | 727aa34 | 2013-07-20 04:09:00 +0000 | [diff] [blame] | 522 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) { |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 523 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
Matt Arsenault | 727aa34 | 2013-07-20 04:09:00 +0000 | [diff] [blame] | 524 | if (Constant *C = ConstantExpr::getCompare(I.getPredicate(), CLHS, CRHS)) { |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 525 | SimplifiedValues[&I] = C; |
| 526 | return true; |
| 527 | } |
Matt Arsenault | 727aa34 | 2013-07-20 04:09:00 +0000 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | if (I.getOpcode() == Instruction::FCmp) |
| 531 | return false; |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 532 | |
| 533 | // Otherwise look for a comparison between constant offset pointers with |
| 534 | // a common base. |
| 535 | Value *LHSBase, *RHSBase; |
| 536 | APInt LHSOffset, RHSOffset; |
Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 537 | std::tie(LHSBase, LHSOffset) = ConstantOffsetPtrs.lookup(LHS); |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 538 | if (LHSBase) { |
Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 539 | std::tie(RHSBase, RHSOffset) = ConstantOffsetPtrs.lookup(RHS); |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 540 | if (RHSBase && LHSBase == RHSBase) { |
| 541 | // We have common bases, fold the icmp to a constant based on the |
| 542 | // offsets. |
| 543 | Constant *CLHS = ConstantInt::get(LHS->getContext(), LHSOffset); |
| 544 | Constant *CRHS = ConstantInt::get(RHS->getContext(), RHSOffset); |
| 545 | if (Constant *C = ConstantExpr::getICmp(I.getPredicate(), CLHS, CRHS)) { |
| 546 | SimplifiedValues[&I] = C; |
| 547 | ++NumConstantPtrCmps; |
| 548 | return true; |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | // If the comparison is an equality comparison with null, we can simplify it |
| 554 | // for any alloca-derived argument. |
| 555 | if (I.isEquality() && isa<ConstantPointerNull>(I.getOperand(1))) |
| 556 | if (isAllocaDerivedArg(I.getOperand(0))) { |
| 557 | // We can actually predict the result of comparisons between an |
| 558 | // alloca-derived value and null. Note that this fires regardless of |
| 559 | // SROA firing. |
| 560 | bool IsNotEqual = I.getPredicate() == CmpInst::ICMP_NE; |
| 561 | SimplifiedValues[&I] = IsNotEqual ? ConstantInt::getTrue(I.getType()) |
| 562 | : ConstantInt::getFalse(I.getType()); |
| 563 | return true; |
| 564 | } |
| 565 | |
| 566 | // Finally check for SROA candidates in comparisons. |
| 567 | Value *SROAArg; |
| 568 | DenseMap<Value *, int>::iterator CostIt; |
| 569 | if (lookupSROAArgAndCost(I.getOperand(0), SROAArg, CostIt)) { |
| 570 | if (isa<ConstantPointerNull>(I.getOperand(1))) { |
| 571 | accumulateSROACost(CostIt, InlineConstants::InstrCost); |
| 572 | return true; |
| 573 | } |
| 574 | |
| 575 | disableSROA(CostIt); |
| 576 | } |
| 577 | |
| 578 | return false; |
| 579 | } |
| 580 | |
| 581 | bool CallAnalyzer::visitSub(BinaryOperator &I) { |
| 582 | // Try to handle a special case: we can fold computing the difference of two |
| 583 | // constant-related pointers. |
| 584 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
| 585 | Value *LHSBase, *RHSBase; |
| 586 | APInt LHSOffset, RHSOffset; |
Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 587 | std::tie(LHSBase, LHSOffset) = ConstantOffsetPtrs.lookup(LHS); |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 588 | if (LHSBase) { |
Benjamin Kramer | d6f1f84 | 2014-03-02 13:30:33 +0000 | [diff] [blame] | 589 | std::tie(RHSBase, RHSOffset) = ConstantOffsetPtrs.lookup(RHS); |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 590 | if (RHSBase && LHSBase == RHSBase) { |
| 591 | // We have common bases, fold the subtract to a constant based on the |
| 592 | // offsets. |
| 593 | Constant *CLHS = ConstantInt::get(LHS->getContext(), LHSOffset); |
| 594 | Constant *CRHS = ConstantInt::get(RHS->getContext(), RHSOffset); |
| 595 | if (Constant *C = ConstantExpr::getSub(CLHS, CRHS)) { |
| 596 | SimplifiedValues[&I] = C; |
| 597 | ++NumConstantPtrDiffs; |
| 598 | return true; |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | // Otherwise, fall back to the generic logic for simplifying and handling |
| 604 | // instructions. |
| 605 | return Base::visitSub(I); |
| 606 | } |
| 607 | |
| 608 | bool CallAnalyzer::visitBinaryOperator(BinaryOperator &I) { |
| 609 | Value *LHS = I.getOperand(0), *RHS = I.getOperand(1); |
| 610 | if (!isa<Constant>(LHS)) |
| 611 | if (Constant *SimpleLHS = SimplifiedValues.lookup(LHS)) |
| 612 | LHS = SimpleLHS; |
| 613 | if (!isa<Constant>(RHS)) |
| 614 | if (Constant *SimpleRHS = SimplifiedValues.lookup(RHS)) |
| 615 | RHS = SimpleRHS; |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 616 | Value *SimpleV = SimplifyBinOp(I.getOpcode(), LHS, RHS, DL); |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 617 | if (Constant *C = dyn_cast_or_null<Constant>(SimpleV)) { |
| 618 | SimplifiedValues[&I] = C; |
| 619 | return true; |
| 620 | } |
| 621 | |
| 622 | // Disable any SROA on arguments to arbitrary, unsimplified binary operators. |
| 623 | disableSROA(LHS); |
| 624 | disableSROA(RHS); |
| 625 | |
| 626 | return false; |
| 627 | } |
| 628 | |
| 629 | bool CallAnalyzer::visitLoad(LoadInst &I) { |
| 630 | Value *SROAArg; |
| 631 | DenseMap<Value *, int>::iterator CostIt; |
| 632 | if (lookupSROAArgAndCost(I.getOperand(0), SROAArg, CostIt)) { |
| 633 | if (I.isSimple()) { |
| 634 | accumulateSROACost(CostIt, InlineConstants::InstrCost); |
| 635 | return true; |
| 636 | } |
| 637 | |
| 638 | disableSROA(CostIt); |
| 639 | } |
| 640 | |
| 641 | return false; |
| 642 | } |
| 643 | |
| 644 | bool CallAnalyzer::visitStore(StoreInst &I) { |
| 645 | Value *SROAArg; |
| 646 | DenseMap<Value *, int>::iterator CostIt; |
| 647 | if (lookupSROAArgAndCost(I.getOperand(0), SROAArg, CostIt)) { |
| 648 | if (I.isSimple()) { |
| 649 | accumulateSROACost(CostIt, InlineConstants::InstrCost); |
| 650 | return true; |
| 651 | } |
| 652 | |
| 653 | disableSROA(CostIt); |
| 654 | } |
| 655 | |
| 656 | return false; |
| 657 | } |
| 658 | |
Chandler Carruth | 753e21d | 2012-12-28 14:23:32 +0000 | [diff] [blame] | 659 | bool CallAnalyzer::visitExtractValue(ExtractValueInst &I) { |
| 660 | // Constant folding for extract value is trivial. |
| 661 | Constant *C = dyn_cast<Constant>(I.getAggregateOperand()); |
| 662 | if (!C) |
| 663 | C = SimplifiedValues.lookup(I.getAggregateOperand()); |
| 664 | if (C) { |
| 665 | SimplifiedValues[&I] = ConstantExpr::getExtractValue(C, I.getIndices()); |
| 666 | return true; |
| 667 | } |
| 668 | |
| 669 | // SROA can look through these but give them a cost. |
| 670 | return false; |
| 671 | } |
| 672 | |
| 673 | bool CallAnalyzer::visitInsertValue(InsertValueInst &I) { |
| 674 | // Constant folding for insert value is trivial. |
| 675 | Constant *AggC = dyn_cast<Constant>(I.getAggregateOperand()); |
| 676 | if (!AggC) |
| 677 | AggC = SimplifiedValues.lookup(I.getAggregateOperand()); |
| 678 | Constant *InsertedC = dyn_cast<Constant>(I.getInsertedValueOperand()); |
| 679 | if (!InsertedC) |
| 680 | InsertedC = SimplifiedValues.lookup(I.getInsertedValueOperand()); |
| 681 | if (AggC && InsertedC) { |
| 682 | SimplifiedValues[&I] = ConstantExpr::getInsertValue(AggC, InsertedC, |
| 683 | I.getIndices()); |
| 684 | return true; |
| 685 | } |
| 686 | |
| 687 | // SROA can look through these but give them a cost. |
| 688 | return false; |
| 689 | } |
| 690 | |
| 691 | /// \brief Try to simplify a call site. |
| 692 | /// |
| 693 | /// Takes a concrete function and callsite and tries to actually simplify it by |
| 694 | /// analyzing the arguments and call itself with instsimplify. Returns true if |
| 695 | /// it has simplified the callsite to some other entity (a constant), making it |
| 696 | /// free. |
| 697 | bool CallAnalyzer::simplifyCallSite(Function *F, CallSite CS) { |
| 698 | // FIXME: Using the instsimplify logic directly for this is inefficient |
| 699 | // because we have to continually rebuild the argument list even when no |
| 700 | // simplifications can be performed. Until that is fixed with remapping |
| 701 | // inside of instsimplify, directly constant fold calls here. |
| 702 | if (!canConstantFoldCallTo(F)) |
| 703 | return false; |
| 704 | |
| 705 | // Try to re-map the arguments to constants. |
| 706 | SmallVector<Constant *, 4> ConstantArgs; |
| 707 | ConstantArgs.reserve(CS.arg_size()); |
| 708 | for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); |
| 709 | I != E; ++I) { |
| 710 | Constant *C = dyn_cast<Constant>(*I); |
| 711 | if (!C) |
| 712 | C = dyn_cast_or_null<Constant>(SimplifiedValues.lookup(*I)); |
| 713 | if (!C) |
| 714 | return false; // This argument doesn't map to a constant. |
| 715 | |
| 716 | ConstantArgs.push_back(C); |
| 717 | } |
| 718 | if (Constant *C = ConstantFoldCall(F, ConstantArgs)) { |
| 719 | SimplifiedValues[CS.getInstruction()] = C; |
| 720 | return true; |
| 721 | } |
| 722 | |
| 723 | return false; |
| 724 | } |
| 725 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 726 | bool CallAnalyzer::visitCallSite(CallSite CS) { |
Chandler Carruth | 37d25de | 2013-12-13 08:00:01 +0000 | [diff] [blame] | 727 | if (CS.hasFnAttr(Attribute::ReturnsTwice) && |
Bill Wendling | 698e84f | 2012-12-30 10:32:01 +0000 | [diff] [blame] | 728 | !F.getAttributes().hasAttribute(AttributeSet::FunctionIndex, |
| 729 | Attribute::ReturnsTwice)) { |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 730 | // This aborts the entire analysis. |
| 731 | ExposesReturnsTwice = true; |
| 732 | return false; |
| 733 | } |
James Molloy | 4f6fb95 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 734 | if (CS.isCall() && |
Eli Bendersky | 576ef3c | 2014-03-17 16:19:07 +0000 | [diff] [blame] | 735 | cast<CallInst>(CS.getInstruction())->cannotDuplicate()) |
James Molloy | 4f6fb95 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 736 | ContainsNoDuplicateCall = true; |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 737 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 738 | if (Function *F = CS.getCalledFunction()) { |
Chandler Carruth | 753e21d | 2012-12-28 14:23:32 +0000 | [diff] [blame] | 739 | // When we have a concrete function, first try to simplify it directly. |
| 740 | if (simplifyCallSite(F, CS)) |
| 741 | return true; |
| 742 | |
| 743 | // Next check if it is an intrinsic we know about. |
| 744 | // FIXME: Lift this into part of the InstVisitor. |
| 745 | if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CS.getInstruction())) { |
| 746 | switch (II->getIntrinsicID()) { |
| 747 | default: |
| 748 | return Base::visitCallSite(CS); |
| 749 | |
| 750 | case Intrinsic::memset: |
| 751 | case Intrinsic::memcpy: |
| 752 | case Intrinsic::memmove: |
| 753 | // SROA can usually chew through these intrinsics, but they aren't free. |
| 754 | return false; |
| 755 | } |
| 756 | } |
| 757 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 758 | if (F == CS.getInstruction()->getParent()->getParent()) { |
| 759 | // This flag will fully abort the analysis, so don't bother with anything |
| 760 | // else. |
Nadav Rotem | 4eb3d4b | 2012-09-19 08:08:04 +0000 | [diff] [blame] | 761 | IsRecursiveCall = true; |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 762 | return false; |
| 763 | } |
| 764 | |
Chandler Carruth | 0ba8db4 | 2013-01-22 11:26:02 +0000 | [diff] [blame] | 765 | if (TTI.isLoweredToCall(F)) { |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 766 | // We account for the average 1 instruction per call argument setup |
| 767 | // here. |
| 768 | Cost += CS.arg_size() * InlineConstants::InstrCost; |
| 769 | |
| 770 | // Everything other than inline ASM will also have a significant cost |
| 771 | // merely from making the call. |
| 772 | if (!isa<InlineAsm>(CS.getCalledValue())) |
| 773 | Cost += InlineConstants::CallPenalty; |
| 774 | } |
| 775 | |
| 776 | return Base::visitCallSite(CS); |
| 777 | } |
| 778 | |
| 779 | // Otherwise we're in a very special case -- an indirect function call. See |
| 780 | // if we can be particularly clever about this. |
| 781 | Value *Callee = CS.getCalledValue(); |
| 782 | |
| 783 | // First, pay the price of the argument setup. We account for the average |
| 784 | // 1 instruction per call argument setup here. |
| 785 | Cost += CS.arg_size() * InlineConstants::InstrCost; |
| 786 | |
| 787 | // Next, check if this happens to be an indirect function call to a known |
| 788 | // function in this inline context. If not, we've done all we can. |
| 789 | Function *F = dyn_cast_or_null<Function>(SimplifiedValues.lookup(Callee)); |
| 790 | if (!F) |
| 791 | return Base::visitCallSite(CS); |
| 792 | |
| 793 | // If we have a constant that we are calling as a function, we can peer |
| 794 | // through it and see the function target. This happens not infrequently |
| 795 | // during devirtualization and so we want to give it a hefty bonus for |
| 796 | // inlining, but cap that bonus in the event that inlining wouldn't pan |
| 797 | // out. Pretend to inline the function, with a custom threshold. |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 798 | CallAnalyzer CA(DL, TTI, *F, InlineConstants::IndirectCallThreshold); |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 799 | if (CA.analyzeCall(CS)) { |
| 800 | // We were able to inline the indirect call! Subtract the cost from the |
| 801 | // bonus we want to apply, but don't go below zero. |
| 802 | Cost -= std::max(0, InlineConstants::IndirectCallThreshold - CA.getCost()); |
| 803 | } |
| 804 | |
| 805 | return Base::visitCallSite(CS); |
| 806 | } |
| 807 | |
Chandler Carruth | 0814d2a | 2013-12-13 07:59:56 +0000 | [diff] [blame] | 808 | bool CallAnalyzer::visitReturnInst(ReturnInst &RI) { |
| 809 | // At least one return instruction will be free after inlining. |
| 810 | bool Free = !HasReturn; |
| 811 | HasReturn = true; |
| 812 | return Free; |
| 813 | } |
| 814 | |
| 815 | bool CallAnalyzer::visitBranchInst(BranchInst &BI) { |
| 816 | // We model unconditional branches as essentially free -- they really |
| 817 | // shouldn't exist at all, but handling them makes the behavior of the |
| 818 | // inliner more regular and predictable. Interestingly, conditional branches |
| 819 | // which will fold away are also free. |
| 820 | return BI.isUnconditional() || isa<ConstantInt>(BI.getCondition()) || |
| 821 | dyn_cast_or_null<ConstantInt>( |
| 822 | SimplifiedValues.lookup(BI.getCondition())); |
| 823 | } |
| 824 | |
| 825 | bool CallAnalyzer::visitSwitchInst(SwitchInst &SI) { |
| 826 | // We model unconditional switches as free, see the comments on handling |
| 827 | // branches. |
| 828 | return isa<ConstantInt>(SI.getCondition()) || |
| 829 | dyn_cast_or_null<ConstantInt>( |
| 830 | SimplifiedValues.lookup(SI.getCondition())); |
| 831 | } |
| 832 | |
| 833 | bool CallAnalyzer::visitIndirectBrInst(IndirectBrInst &IBI) { |
| 834 | // We never want to inline functions that contain an indirectbr. This is |
| 835 | // incorrect because all the blockaddress's (in static global initializers |
| 836 | // for example) would be referring to the original function, and this |
| 837 | // indirect jump would jump from the inlined copy of the function into the |
| 838 | // original function which is extremely undefined behavior. |
| 839 | // FIXME: This logic isn't really right; we can safely inline functions with |
| 840 | // indirectbr's as long as no other function or global references the |
| 841 | // blockaddress of a block within the current function. And as a QOI issue, |
| 842 | // if someone is using a blockaddress without an indirectbr, and that |
| 843 | // reference somehow ends up in another function or global, we probably don't |
| 844 | // want to inline this function. |
| 845 | HasIndirectBr = true; |
| 846 | return false; |
| 847 | } |
| 848 | |
| 849 | bool CallAnalyzer::visitResumeInst(ResumeInst &RI) { |
| 850 | // FIXME: It's not clear that a single instruction is an accurate model for |
| 851 | // the inline cost of a resume instruction. |
| 852 | return false; |
| 853 | } |
| 854 | |
| 855 | bool CallAnalyzer::visitUnreachableInst(UnreachableInst &I) { |
| 856 | // FIXME: It might be reasonably to discount the cost of instructions leading |
| 857 | // to unreachable as they have the lowest possible impact on both runtime and |
| 858 | // code size. |
| 859 | return true; // No actual code is needed for unreachable. |
| 860 | } |
| 861 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 862 | bool CallAnalyzer::visitInstruction(Instruction &I) { |
Chandler Carruth | da7513a | 2012-05-04 00:58:03 +0000 | [diff] [blame] | 863 | // Some instructions are free. All of the free intrinsics can also be |
| 864 | // handled by SROA, etc. |
Chandler Carruth | b8cf510 | 2013-01-21 12:05:16 +0000 | [diff] [blame] | 865 | if (TargetTransformInfo::TCC_Free == TTI.getUserCost(&I)) |
Chandler Carruth | da7513a | 2012-05-04 00:58:03 +0000 | [diff] [blame] | 866 | return true; |
| 867 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 868 | // We found something we don't understand or can't handle. Mark any SROA-able |
| 869 | // values in the operand list as no longer viable. |
| 870 | for (User::op_iterator OI = I.op_begin(), OE = I.op_end(); OI != OE; ++OI) |
| 871 | disableSROA(*OI); |
| 872 | |
| 873 | return false; |
| 874 | } |
| 875 | |
| 876 | |
| 877 | /// \brief Analyze a basic block for its contribution to the inline cost. |
| 878 | /// |
| 879 | /// This method walks the analyzer over every instruction in the given basic |
| 880 | /// block and accounts for their cost during inlining at this callsite. It |
| 881 | /// aborts early if the threshold has been exceeded or an impossible to inline |
| 882 | /// construct has been detected. It returns false if inlining is no longer |
| 883 | /// viable, and true if inlining remains viable. |
| 884 | bool CallAnalyzer::analyzeBlock(BasicBlock *BB) { |
Chandler Carruth | 0814d2a | 2013-12-13 07:59:56 +0000 | [diff] [blame] | 885 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { |
Chandler Carruth | 6b4cc8b | 2014-02-01 10:38:17 +0000 | [diff] [blame] | 886 | // FIXME: Currently, the number of instructions in a function regardless of |
| 887 | // our ability to simplify them during inline to constants or dead code, |
| 888 | // are actually used by the vector bonus heuristic. As long as that's true, |
| 889 | // we have to special case debug intrinsics here to prevent differences in |
| 890 | // inlining due to debug symbols. Eventually, the number of unsimplified |
| 891 | // instructions shouldn't factor into the cost computation, but until then, |
| 892 | // hack around it here. |
| 893 | if (isa<DbgInfoIntrinsic>(I)) |
| 894 | continue; |
| 895 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 896 | ++NumInstructions; |
| 897 | if (isa<ExtractElementInst>(I) || I->getType()->isVectorTy()) |
| 898 | ++NumVectorInstructions; |
| 899 | |
| 900 | // If the instruction simplified to a constant, there is no cost to this |
| 901 | // instruction. Visit the instructions using our InstVisitor to account for |
| 902 | // all of the per-instruction logic. The visit tree returns true if we |
| 903 | // consumed the instruction in any way, and false if the instruction's base |
| 904 | // cost should count against inlining. |
| 905 | if (Base::visit(I)) |
| 906 | ++NumInstructionsSimplified; |
| 907 | else |
| 908 | Cost += InlineConstants::InstrCost; |
| 909 | |
| 910 | // If the visit this instruction detected an uninlinable pattern, abort. |
Chandler Carruth | 0814d2a | 2013-12-13 07:59:56 +0000 | [diff] [blame] | 911 | if (IsRecursiveCall || ExposesReturnsTwice || HasDynamicAlloca || |
| 912 | HasIndirectBr) |
Nadav Rotem | 4eb3d4b | 2012-09-19 08:08:04 +0000 | [diff] [blame] | 913 | return false; |
| 914 | |
| 915 | // If the caller is a recursive function then we don't want to inline |
| 916 | // functions which allocate a lot of stack space because it would increase |
| 917 | // the caller stack usage dramatically. |
| 918 | if (IsCallerRecursive && |
| 919 | AllocatedSize > InlineConstants::TotalAllocaSizeRecursiveCaller) |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 920 | return false; |
| 921 | |
| 922 | if (NumVectorInstructions > NumInstructions/2) |
| 923 | VectorBonus = FiftyPercentVectorBonus; |
| 924 | else if (NumVectorInstructions > NumInstructions/10) |
| 925 | VectorBonus = TenPercentVectorBonus; |
| 926 | else |
| 927 | VectorBonus = 0; |
| 928 | |
| 929 | // Check if we've past the threshold so we don't spin in huge basic |
| 930 | // blocks that will never inline. |
Bob Wilson | a5b0dc8 | 2012-11-19 07:04:35 +0000 | [diff] [blame] | 931 | if (Cost > (Threshold + VectorBonus)) |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 932 | return false; |
| 933 | } |
| 934 | |
| 935 | return true; |
| 936 | } |
| 937 | |
| 938 | /// \brief Compute the base pointer and cumulative constant offsets for V. |
| 939 | /// |
| 940 | /// This strips all constant offsets off of V, leaving it the base pointer, and |
| 941 | /// accumulates the total constant offset applied in the returned constant. It |
| 942 | /// returns 0 if V is not a pointer, and returns the constant '0' if there are |
| 943 | /// no constant offsets applied. |
| 944 | ConstantInt *CallAnalyzer::stripAndComputeInBoundsConstantOffsets(Value *&V) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 945 | if (!DL || !V->getType()->isPointerTy()) |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 946 | return 0; |
| 947 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 948 | unsigned IntPtrWidth = DL->getPointerSizeInBits(); |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 949 | APInt Offset = APInt::getNullValue(IntPtrWidth); |
| 950 | |
| 951 | // Even though we don't look through PHI nodes, we could be called on an |
| 952 | // instruction in an unreachable block, which may be on a cycle. |
| 953 | SmallPtrSet<Value *, 4> Visited; |
| 954 | Visited.insert(V); |
| 955 | do { |
| 956 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { |
| 957 | if (!GEP->isInBounds() || !accumulateGEPOffset(*GEP, Offset)) |
| 958 | return 0; |
| 959 | V = GEP->getPointerOperand(); |
| 960 | } else if (Operator::getOpcode(V) == Instruction::BitCast) { |
| 961 | V = cast<Operator>(V)->getOperand(0); |
| 962 | } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { |
| 963 | if (GA->mayBeOverridden()) |
| 964 | break; |
| 965 | V = GA->getAliasee(); |
| 966 | } else { |
| 967 | break; |
| 968 | } |
| 969 | assert(V->getType()->isPointerTy() && "Unexpected operand type!"); |
| 970 | } while (Visited.insert(V)); |
| 971 | |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 972 | Type *IntPtrTy = DL->getIntPtrType(V->getContext()); |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 973 | return cast<ConstantInt>(ConstantInt::get(IntPtrTy, Offset)); |
| 974 | } |
| 975 | |
| 976 | /// \brief Analyze a call site for potential inlining. |
| 977 | /// |
| 978 | /// Returns true if inlining this call is viable, and false if it is not |
| 979 | /// viable. It computes the cost and adjusts the threshold based on numerous |
| 980 | /// factors and heuristics. If this method returns false but the computed cost |
| 981 | /// is below the computed threshold, then inlining was forcibly disabled by |
Bob Wilson | 266802d | 2012-11-19 07:04:30 +0000 | [diff] [blame] | 982 | /// some artifact of the routine. |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 983 | bool CallAnalyzer::analyzeCall(CallSite CS) { |
Chandler Carruth | 7ae90d4 | 2012-04-11 10:15:10 +0000 | [diff] [blame] | 984 | ++NumCallsAnalyzed; |
| 985 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 986 | // Track whether the post-inlining function would have more than one basic |
| 987 | // block. A single basic block is often intended for inlining. Balloon the |
| 988 | // threshold by 50% until we pass the single-BB phase. |
| 989 | bool SingleBB = true; |
| 990 | int SingleBBBonus = Threshold / 2; |
| 991 | Threshold += SingleBBBonus; |
| 992 | |
Bob Wilson | a5b0dc8 | 2012-11-19 07:04:35 +0000 | [diff] [blame] | 993 | // Perform some tweaks to the cost and threshold based on the direct |
| 994 | // callsite information. |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 995 | |
Bob Wilson | a5b0dc8 | 2012-11-19 07:04:35 +0000 | [diff] [blame] | 996 | // We want to more aggressively inline vector-dense kernels, so up the |
| 997 | // threshold, and we'll lower it if the % of vector instructions gets too |
| 998 | // low. |
| 999 | assert(NumInstructions == 0); |
| 1000 | assert(NumVectorInstructions == 0); |
| 1001 | FiftyPercentVectorBonus = Threshold; |
| 1002 | TenPercentVectorBonus = Threshold / 2; |
Benjamin Kramer | c99d0e9 | 2012-08-07 11:13:19 +0000 | [diff] [blame] | 1003 | |
Bob Wilson | a5b0dc8 | 2012-11-19 07:04:35 +0000 | [diff] [blame] | 1004 | // Give out bonuses per argument, as the instructions setting them up will |
| 1005 | // be gone after inlining. |
| 1006 | for (unsigned I = 0, E = CS.arg_size(); I != E; ++I) { |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1007 | if (DL && CS.isByValArgument(I)) { |
Bob Wilson | a5b0dc8 | 2012-11-19 07:04:35 +0000 | [diff] [blame] | 1008 | // We approximate the number of loads and stores needed by dividing the |
| 1009 | // size of the byval type by the target's pointer size. |
| 1010 | PointerType *PTy = cast<PointerType>(CS.getArgument(I)->getType()); |
Rafael Espindola | 37dc9e1 | 2014-02-21 00:06:31 +0000 | [diff] [blame] | 1011 | unsigned TypeSize = DL->getTypeSizeInBits(PTy->getElementType()); |
| 1012 | unsigned PointerSize = DL->getPointerSizeInBits(); |
Bob Wilson | a5b0dc8 | 2012-11-19 07:04:35 +0000 | [diff] [blame] | 1013 | // Ceiling division. |
| 1014 | unsigned NumStores = (TypeSize + PointerSize - 1) / PointerSize; |
Benjamin Kramer | c99d0e9 | 2012-08-07 11:13:19 +0000 | [diff] [blame] | 1015 | |
Bob Wilson | a5b0dc8 | 2012-11-19 07:04:35 +0000 | [diff] [blame] | 1016 | // If it generates more than 8 stores it is likely to be expanded as an |
| 1017 | // inline memcpy so we take that as an upper bound. Otherwise we assume |
| 1018 | // one load and one store per word copied. |
| 1019 | // FIXME: The maxStoresPerMemcpy setting from the target should be used |
| 1020 | // here instead of a magic number of 8, but it's not available via |
| 1021 | // DataLayout. |
| 1022 | NumStores = std::min(NumStores, 8U); |
| 1023 | |
| 1024 | Cost -= 2 * NumStores * InlineConstants::InstrCost; |
| 1025 | } else { |
| 1026 | // For non-byval arguments subtract off one instruction per call |
| 1027 | // argument. |
| 1028 | Cost -= InlineConstants::InstrCost; |
Benjamin Kramer | c99d0e9 | 2012-08-07 11:13:19 +0000 | [diff] [blame] | 1029 | } |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
Bob Wilson | a5b0dc8 | 2012-11-19 07:04:35 +0000 | [diff] [blame] | 1032 | // If there is only one call of the function, and it has internal linkage, |
| 1033 | // the cost of inlining it drops dramatically. |
James Molloy | 4f6fb95 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 1034 | bool OnlyOneCallAndLocalLinkage = F.hasLocalLinkage() && F.hasOneUse() && |
| 1035 | &F == CS.getCalledFunction(); |
| 1036 | if (OnlyOneCallAndLocalLinkage) |
Bob Wilson | a5b0dc8 | 2012-11-19 07:04:35 +0000 | [diff] [blame] | 1037 | Cost += InlineConstants::LastCallToStaticBonus; |
| 1038 | |
| 1039 | // If the instruction after the call, or if the normal destination of the |
| 1040 | // invoke is an unreachable instruction, the function is noreturn. As such, |
| 1041 | // there is little point in inlining this unless there is literally zero |
| 1042 | // cost. |
| 1043 | Instruction *Instr = CS.getInstruction(); |
| 1044 | if (InvokeInst *II = dyn_cast<InvokeInst>(Instr)) { |
| 1045 | if (isa<UnreachableInst>(II->getNormalDest()->begin())) |
| 1046 | Threshold = 1; |
| 1047 | } else if (isa<UnreachableInst>(++BasicBlock::iterator(Instr))) |
| 1048 | Threshold = 1; |
| 1049 | |
| 1050 | // If this function uses the coldcc calling convention, prefer not to inline |
| 1051 | // it. |
| 1052 | if (F.getCallingConv() == CallingConv::Cold) |
| 1053 | Cost += InlineConstants::ColdccPenalty; |
| 1054 | |
| 1055 | // Check if we're done. This can happen due to bonuses and penalties. |
| 1056 | if (Cost > Threshold) |
| 1057 | return false; |
| 1058 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 1059 | if (F.empty()) |
| 1060 | return true; |
| 1061 | |
Nadav Rotem | 4eb3d4b | 2012-09-19 08:08:04 +0000 | [diff] [blame] | 1062 | Function *Caller = CS.getInstruction()->getParent()->getParent(); |
| 1063 | // Check if the caller function is recursive itself. |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1064 | for (User *U : Caller->users()) { |
| 1065 | CallSite Site(U); |
Nadav Rotem | 4eb3d4b | 2012-09-19 08:08:04 +0000 | [diff] [blame] | 1066 | if (!Site) |
| 1067 | continue; |
| 1068 | Instruction *I = Site.getInstruction(); |
| 1069 | if (I->getParent()->getParent() == Caller) { |
| 1070 | IsCallerRecursive = true; |
| 1071 | break; |
| 1072 | } |
| 1073 | } |
| 1074 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 1075 | // Populate our simplified values by mapping from function arguments to call |
| 1076 | // arguments with known important simplifications. |
| 1077 | CallSite::arg_iterator CAI = CS.arg_begin(); |
| 1078 | for (Function::arg_iterator FAI = F.arg_begin(), FAE = F.arg_end(); |
| 1079 | FAI != FAE; ++FAI, ++CAI) { |
| 1080 | assert(CAI != CS.arg_end()); |
| 1081 | if (Constant *C = dyn_cast<Constant>(CAI)) |
| 1082 | SimplifiedValues[FAI] = C; |
| 1083 | |
| 1084 | Value *PtrArg = *CAI; |
| 1085 | if (ConstantInt *C = stripAndComputeInBoundsConstantOffsets(PtrArg)) { |
| 1086 | ConstantOffsetPtrs[FAI] = std::make_pair(PtrArg, C->getValue()); |
| 1087 | |
| 1088 | // We can SROA any pointer arguments derived from alloca instructions. |
| 1089 | if (isa<AllocaInst>(PtrArg)) { |
| 1090 | SROAArgValues[FAI] = PtrArg; |
| 1091 | SROAArgCosts[PtrArg] = 0; |
| 1092 | } |
| 1093 | } |
| 1094 | } |
| 1095 | NumConstantArgs = SimplifiedValues.size(); |
| 1096 | NumConstantOffsetPtrArgs = ConstantOffsetPtrs.size(); |
| 1097 | NumAllocaArgs = SROAArgValues.size(); |
| 1098 | |
| 1099 | // The worklist of live basic blocks in the callee *after* inlining. We avoid |
| 1100 | // adding basic blocks of the callee which can be proven to be dead for this |
| 1101 | // particular call site in order to get more accurate cost estimates. This |
| 1102 | // requires a somewhat heavyweight iteration pattern: we need to walk the |
| 1103 | // basic blocks in a breadth-first order as we insert live successors. To |
| 1104 | // accomplish this, prioritizing for small iterations because we exit after |
| 1105 | // crossing our threshold, we use a small-size optimized SetVector. |
| 1106 | typedef SetVector<BasicBlock *, SmallVector<BasicBlock *, 16>, |
| 1107 | SmallPtrSet<BasicBlock *, 16> > BBSetVector; |
| 1108 | BBSetVector BBWorklist; |
| 1109 | BBWorklist.insert(&F.getEntryBlock()); |
| 1110 | // Note that we *must not* cache the size, this loop grows the worklist. |
| 1111 | for (unsigned Idx = 0; Idx != BBWorklist.size(); ++Idx) { |
| 1112 | // Bail out the moment we cross the threshold. This means we'll under-count |
| 1113 | // the cost, but only when undercounting doesn't matter. |
Bob Wilson | a5b0dc8 | 2012-11-19 07:04:35 +0000 | [diff] [blame] | 1114 | if (Cost > (Threshold + VectorBonus)) |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 1115 | break; |
| 1116 | |
| 1117 | BasicBlock *BB = BBWorklist[Idx]; |
| 1118 | if (BB->empty()) |
Chandler Carruth | 4d1d34f | 2012-03-14 23:19:53 +0000 | [diff] [blame] | 1119 | continue; |
Dan Gohman | 4552e3c | 2009-10-13 18:30:07 +0000 | [diff] [blame] | 1120 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 1121 | // Analyze the cost of this block. If we blow through the threshold, this |
| 1122 | // returns false, and we can bail on out. |
| 1123 | if (!analyzeBlock(BB)) { |
Chandler Carruth | 0814d2a | 2013-12-13 07:59:56 +0000 | [diff] [blame] | 1124 | if (IsRecursiveCall || ExposesReturnsTwice || HasDynamicAlloca || |
| 1125 | HasIndirectBr) |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 1126 | return false; |
Nadav Rotem | 4eb3d4b | 2012-09-19 08:08:04 +0000 | [diff] [blame] | 1127 | |
| 1128 | // If the caller is a recursive function then we don't want to inline |
| 1129 | // functions which allocate a lot of stack space because it would increase |
| 1130 | // the caller stack usage dramatically. |
| 1131 | if (IsCallerRecursive && |
| 1132 | AllocatedSize > InlineConstants::TotalAllocaSizeRecursiveCaller) |
| 1133 | return false; |
| 1134 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 1135 | break; |
Eric Christopher | 46308e6 | 2011-02-01 01:16:32 +0000 | [diff] [blame] | 1136 | } |
Eric Christopher | 46308e6 | 2011-02-01 01:16:32 +0000 | [diff] [blame] | 1137 | |
Chandler Carruth | 0814d2a | 2013-12-13 07:59:56 +0000 | [diff] [blame] | 1138 | TerminatorInst *TI = BB->getTerminator(); |
| 1139 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 1140 | // Add in the live successors by first checking whether we have terminator |
| 1141 | // that may be simplified based on the values simplified by this call. |
| 1142 | if (BranchInst *BI = dyn_cast<BranchInst>(TI)) { |
| 1143 | if (BI->isConditional()) { |
| 1144 | Value *Cond = BI->getCondition(); |
| 1145 | if (ConstantInt *SimpleCond |
| 1146 | = dyn_cast_or_null<ConstantInt>(SimplifiedValues.lookup(Cond))) { |
| 1147 | BBWorklist.insert(BI->getSuccessor(SimpleCond->isZero() ? 1 : 0)); |
| 1148 | continue; |
Eric Christopher | 46308e6 | 2011-02-01 01:16:32 +0000 | [diff] [blame] | 1149 | } |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 1150 | } |
| 1151 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) { |
| 1152 | Value *Cond = SI->getCondition(); |
| 1153 | if (ConstantInt *SimpleCond |
| 1154 | = dyn_cast_or_null<ConstantInt>(SimplifiedValues.lookup(Cond))) { |
| 1155 | BBWorklist.insert(SI->findCaseValue(SimpleCond).getCaseSuccessor()); |
| 1156 | continue; |
| 1157 | } |
| 1158 | } |
Eric Christopher | 46308e6 | 2011-02-01 01:16:32 +0000 | [diff] [blame] | 1159 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 1160 | // If we're unable to select a particular successor, just count all of |
| 1161 | // them. |
Nadav Rotem | 4eb3d4b | 2012-09-19 08:08:04 +0000 | [diff] [blame] | 1162 | for (unsigned TIdx = 0, TSize = TI->getNumSuccessors(); TIdx != TSize; |
| 1163 | ++TIdx) |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 1164 | BBWorklist.insert(TI->getSuccessor(TIdx)); |
| 1165 | |
| 1166 | // If we had any successors at this point, than post-inlining is likely to |
| 1167 | // have them as well. Note that we assume any basic blocks which existed |
| 1168 | // due to branches or switches which folded above will also fold after |
| 1169 | // inlining. |
| 1170 | if (SingleBB && TI->getNumSuccessors() > 1) { |
| 1171 | // Take off the bonus we applied to the threshold. |
| 1172 | Threshold -= SingleBBBonus; |
| 1173 | SingleBB = false; |
Eric Christopher | 46308e6 | 2011-02-01 01:16:32 +0000 | [diff] [blame] | 1174 | } |
| 1175 | } |
Andrew Trick | caa500b | 2011-10-01 01:27:56 +0000 | [diff] [blame] | 1176 | |
Chandler Carruth | cb5beb3 | 2013-12-12 11:59:26 +0000 | [diff] [blame] | 1177 | // If this is a noduplicate call, we can still inline as long as |
James Molloy | 4f6fb95 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 1178 | // inlining this would cause the removal of the caller (so the instruction |
| 1179 | // is not actually duplicated, just moved). |
| 1180 | if (!OnlyOneCallAndLocalLinkage && ContainsNoDuplicateCall) |
| 1181 | return false; |
| 1182 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 1183 | Threshold += VectorBonus; |
| 1184 | |
Bob Wilson | a5b0dc8 | 2012-11-19 07:04:35 +0000 | [diff] [blame] | 1185 | return Cost < Threshold; |
Eric Christopher | 2dfbd7e | 2011-02-05 00:49:15 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
Manman Ren | 49d684e | 2012-09-12 05:06:18 +0000 | [diff] [blame] | 1188 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 1189 | /// \brief Dump stats about this call's analysis. |
| 1190 | void CallAnalyzer::dump() { |
Eric Christopher | a13839f | 2014-02-26 23:27:16 +0000 | [diff] [blame] | 1191 | #define DEBUG_PRINT_STAT(x) dbgs() << " " #x ": " << x << "\n" |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 1192 | DEBUG_PRINT_STAT(NumConstantArgs); |
| 1193 | DEBUG_PRINT_STAT(NumConstantOffsetPtrArgs); |
| 1194 | DEBUG_PRINT_STAT(NumAllocaArgs); |
| 1195 | DEBUG_PRINT_STAT(NumConstantPtrCmps); |
| 1196 | DEBUG_PRINT_STAT(NumConstantPtrDiffs); |
| 1197 | DEBUG_PRINT_STAT(NumInstructionsSimplified); |
| 1198 | DEBUG_PRINT_STAT(SROACostSavings); |
| 1199 | DEBUG_PRINT_STAT(SROACostSavingsLost); |
James Molloy | 4f6fb95 | 2012-12-20 16:04:27 +0000 | [diff] [blame] | 1200 | DEBUG_PRINT_STAT(ContainsNoDuplicateCall); |
Chandler Carruth | 394e34f | 2014-01-31 22:32:32 +0000 | [diff] [blame] | 1201 | DEBUG_PRINT_STAT(Cost); |
| 1202 | DEBUG_PRINT_STAT(Threshold); |
| 1203 | DEBUG_PRINT_STAT(VectorBonus); |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 1204 | #undef DEBUG_PRINT_STAT |
Eric Christopher | 2dfbd7e | 2011-02-05 00:49:15 +0000 | [diff] [blame] | 1205 | } |
Manman Ren | c3366cc | 2012-09-06 19:55:56 +0000 | [diff] [blame] | 1206 | #endif |
Eric Christopher | 2dfbd7e | 2011-02-05 00:49:15 +0000 | [diff] [blame] | 1207 | |
Chandler Carruth | 4319e29 | 2013-01-21 11:39:18 +0000 | [diff] [blame] | 1208 | INITIALIZE_PASS_BEGIN(InlineCostAnalysis, "inline-cost", "Inline Cost Analysis", |
| 1209 | true, true) |
Chandler Carruth | 42f3dce | 2013-01-21 11:55:09 +0000 | [diff] [blame] | 1210 | INITIALIZE_AG_DEPENDENCY(TargetTransformInfo) |
Chandler Carruth | 4319e29 | 2013-01-21 11:39:18 +0000 | [diff] [blame] | 1211 | INITIALIZE_PASS_END(InlineCostAnalysis, "inline-cost", "Inline Cost Analysis", |
| 1212 | true, true) |
| 1213 | |
| 1214 | char InlineCostAnalysis::ID = 0; |
| 1215 | |
Rafael Espindola | 339430f | 2014-02-25 23:25:17 +0000 | [diff] [blame] | 1216 | InlineCostAnalysis::InlineCostAnalysis() : CallGraphSCCPass(ID) {} |
Chandler Carruth | 4319e29 | 2013-01-21 11:39:18 +0000 | [diff] [blame] | 1217 | |
| 1218 | InlineCostAnalysis::~InlineCostAnalysis() {} |
| 1219 | |
| 1220 | void InlineCostAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { |
| 1221 | AU.setPreservesAll(); |
Chandler Carruth | 42f3dce | 2013-01-21 11:55:09 +0000 | [diff] [blame] | 1222 | AU.addRequired<TargetTransformInfo>(); |
Chandler Carruth | 4319e29 | 2013-01-21 11:39:18 +0000 | [diff] [blame] | 1223 | CallGraphSCCPass::getAnalysisUsage(AU); |
| 1224 | } |
| 1225 | |
| 1226 | bool InlineCostAnalysis::runOnSCC(CallGraphSCC &SCC) { |
Chandler Carruth | 42f3dce | 2013-01-21 11:55:09 +0000 | [diff] [blame] | 1227 | TTI = &getAnalysis<TargetTransformInfo>(); |
Chandler Carruth | 4319e29 | 2013-01-21 11:39:18 +0000 | [diff] [blame] | 1228 | return false; |
| 1229 | } |
| 1230 | |
| 1231 | InlineCost InlineCostAnalysis::getInlineCost(CallSite CS, int Threshold) { |
David Chisnall | c1c9cda | 2012-04-06 17:27:41 +0000 | [diff] [blame] | 1232 | return getInlineCost(CS, CS.getCalledFunction(), Threshold); |
| 1233 | } |
Dan Gohman | 4552e3c | 2009-10-13 18:30:07 +0000 | [diff] [blame] | 1234 | |
Evgeniy Stepanov | 2ad3698 | 2013-08-08 08:22:39 +0000 | [diff] [blame] | 1235 | /// \brief Test that two functions either have or have not the given attribute |
| 1236 | /// at the same time. |
| 1237 | static bool attributeMatches(Function *F1, Function *F2, |
| 1238 | Attribute::AttrKind Attr) { |
| 1239 | return F1->hasFnAttribute(Attr) == F2->hasFnAttribute(Attr); |
| 1240 | } |
| 1241 | |
| 1242 | /// \brief Test that there are no attribute conflicts between Caller and Callee |
| 1243 | /// that prevent inlining. |
| 1244 | static bool functionsHaveCompatibleAttributes(Function *Caller, |
| 1245 | Function *Callee) { |
| 1246 | return attributeMatches(Caller, Callee, Attribute::SanitizeAddress) && |
| 1247 | attributeMatches(Caller, Callee, Attribute::SanitizeMemory) && |
| 1248 | attributeMatches(Caller, Callee, Attribute::SanitizeThread); |
| 1249 | } |
| 1250 | |
Chandler Carruth | 4319e29 | 2013-01-21 11:39:18 +0000 | [diff] [blame] | 1251 | InlineCost InlineCostAnalysis::getInlineCost(CallSite CS, Function *Callee, |
David Chisnall | c1c9cda | 2012-04-06 17:27:41 +0000 | [diff] [blame] | 1252 | int Threshold) { |
Bob Wilson | a5b0dc8 | 2012-11-19 07:04:35 +0000 | [diff] [blame] | 1253 | // Cannot inline indirect calls. |
| 1254 | if (!Callee) |
| 1255 | return llvm::InlineCost::getNever(); |
| 1256 | |
| 1257 | // Calls to functions with always-inline attributes should be inlined |
| 1258 | // whenever possible. |
Evgeniy Stepanov | 2ad3698 | 2013-08-08 08:22:39 +0000 | [diff] [blame] | 1259 | if (Callee->hasFnAttribute(Attribute::AlwaysInline)) { |
Bob Wilson | a5b0dc8 | 2012-11-19 07:04:35 +0000 | [diff] [blame] | 1260 | if (isInlineViable(*Callee)) |
| 1261 | return llvm::InlineCost::getAlways(); |
| 1262 | return llvm::InlineCost::getNever(); |
| 1263 | } |
| 1264 | |
Evgeniy Stepanov | 2ad3698 | 2013-08-08 08:22:39 +0000 | [diff] [blame] | 1265 | // Never inline functions with conflicting attributes (unless callee has |
| 1266 | // always-inline attribute). |
| 1267 | if (!functionsHaveCompatibleAttributes(CS.getCaller(), Callee)) |
| 1268 | return llvm::InlineCost::getNever(); |
| 1269 | |
Paul Robinson | dcbe35b | 2013-11-18 21:44:03 +0000 | [diff] [blame] | 1270 | // Don't inline this call if the caller has the optnone attribute. |
| 1271 | if (CS.getCaller()->hasFnAttribute(Attribute::OptimizeNone)) |
| 1272 | return llvm::InlineCost::getNever(); |
| 1273 | |
Dan Gohman | 4552e3c | 2009-10-13 18:30:07 +0000 | [diff] [blame] | 1274 | // Don't inline functions which can be redefined at link-time to mean |
Eric Christopher | b1a382d | 2010-03-25 04:49:10 +0000 | [diff] [blame] | 1275 | // something else. Don't inline functions marked noinline or call sites |
| 1276 | // marked noinline. |
Bob Wilson | a5b0dc8 | 2012-11-19 07:04:35 +0000 | [diff] [blame] | 1277 | if (Callee->mayBeOverridden() || |
Evgeniy Stepanov | 2ad3698 | 2013-08-08 08:22:39 +0000 | [diff] [blame] | 1278 | Callee->hasFnAttribute(Attribute::NoInline) || CS.isNoInline()) |
Dan Gohman | 4552e3c | 2009-10-13 18:30:07 +0000 | [diff] [blame] | 1279 | return llvm::InlineCost::getNever(); |
| 1280 | |
Nadav Rotem | 4eb3d4b | 2012-09-19 08:08:04 +0000 | [diff] [blame] | 1281 | DEBUG(llvm::dbgs() << " Analyzing call of " << Callee->getName() |
| 1282 | << "...\n"); |
Andrew Trick | caa500b | 2011-10-01 01:27:56 +0000 | [diff] [blame] | 1283 | |
Rafael Espindola | 339430f | 2014-02-25 23:25:17 +0000 | [diff] [blame] | 1284 | CallAnalyzer CA(Callee->getDataLayout(), *TTI, *Callee, Threshold); |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 1285 | bool ShouldInline = CA.analyzeCall(CS); |
Dan Gohman | 4552e3c | 2009-10-13 18:30:07 +0000 | [diff] [blame] | 1286 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 1287 | DEBUG(CA.dump()); |
| 1288 | |
| 1289 | // Check if there was a reason to force inlining or no inlining. |
| 1290 | if (!ShouldInline && CA.getCost() < CA.getThreshold()) |
Dan Gohman | 4552e3c | 2009-10-13 18:30:07 +0000 | [diff] [blame] | 1291 | return InlineCost::getNever(); |
Bob Wilson | a5b0dc8 | 2012-11-19 07:04:35 +0000 | [diff] [blame] | 1292 | if (ShouldInline && CA.getCost() >= CA.getThreshold()) |
Dan Gohman | 4552e3c | 2009-10-13 18:30:07 +0000 | [diff] [blame] | 1293 | return InlineCost::getAlways(); |
Andrew Trick | caa500b | 2011-10-01 01:27:56 +0000 | [diff] [blame] | 1294 | |
Chandler Carruth | 0539c07 | 2012-03-31 12:42:41 +0000 | [diff] [blame] | 1295 | return llvm::InlineCost::get(CA.getCost(), CA.getThreshold()); |
Dan Gohman | 4552e3c | 2009-10-13 18:30:07 +0000 | [diff] [blame] | 1296 | } |
Bob Wilson | a5b0dc8 | 2012-11-19 07:04:35 +0000 | [diff] [blame] | 1297 | |
Chandler Carruth | 4319e29 | 2013-01-21 11:39:18 +0000 | [diff] [blame] | 1298 | bool InlineCostAnalysis::isInlineViable(Function &F) { |
| 1299 | bool ReturnsTwice = |
| 1300 | F.getAttributes().hasAttribute(AttributeSet::FunctionIndex, |
| 1301 | Attribute::ReturnsTwice); |
Bob Wilson | a5b0dc8 | 2012-11-19 07:04:35 +0000 | [diff] [blame] | 1302 | for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) { |
Gerolf Hoflehner | ecebc37 | 2014-04-17 19:14:06 +0000 | [diff] [blame^] | 1303 | // Disallow inlining of functions which contain an indirect branch. |
| 1304 | if (isa<IndirectBrInst>(BI->getTerminator())) |
Bob Wilson | a5b0dc8 | 2012-11-19 07:04:35 +0000 | [diff] [blame] | 1305 | return false; |
| 1306 | |
| 1307 | for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE; |
| 1308 | ++II) { |
| 1309 | CallSite CS(II); |
| 1310 | if (!CS) |
| 1311 | continue; |
| 1312 | |
| 1313 | // Disallow recursive calls. |
| 1314 | if (&F == CS.getCalledFunction()) |
| 1315 | return false; |
| 1316 | |
| 1317 | // Disallow calls which expose returns-twice to a function not previously |
| 1318 | // attributed as such. |
| 1319 | if (!ReturnsTwice && CS.isCall() && |
| 1320 | cast<CallInst>(CS.getInstruction())->canReturnTwice()) |
| 1321 | return false; |
| 1322 | } |
| 1323 | } |
| 1324 | |
| 1325 | return true; |
| 1326 | } |