blob: 89dcd819ee2cedeaffdcc2d0e18f6bdeba1f1b5c [file] [log] [blame]
Dan Gohman4552e3c2009-10-13 18:30:07 +00001//===- 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 Carruth0539c072012-03-31 12:42:41 +000014#define DEBUG_TYPE "inline-cost"
Dan Gohman4552e3c2009-10-13 18:30:07 +000015#include "llvm/Analysis/InlineCost.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#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 Carruth0539c072012-03-31 12:42:41 +000021#include "llvm/Analysis/ConstantFolding.h"
22#include "llvm/Analysis/InstructionSimplify.h"
Chandler Carruth42f3dce2013-01-21 11:55:09 +000023#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/CallingConv.h"
25#include "llvm/IR/DataLayout.h"
26#include "llvm/IR/GlobalAlias.h"
27#include "llvm/IR/IntrinsicInst.h"
28#include "llvm/IR/Operator.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/InstVisitor.h"
Dan Gohman4552e3c2009-10-13 18:30:07 +000030#include "llvm/Support/CallSite.h"
Chandler Carruth0539c072012-03-31 12:42:41 +000031#include "llvm/Support/Debug.h"
Chandler Carruth0539c072012-03-31 12:42:41 +000032#include "llvm/Support/GetElementPtrTypeIterator.h"
33#include "llvm/Support/raw_ostream.h"
Eric Christopher2dfbd7e2011-02-05 00:49:15 +000034
Dan Gohman4552e3c2009-10-13 18:30:07 +000035using namespace llvm;
36
Chandler Carruth7ae90d42012-04-11 10:15:10 +000037STATISTIC(NumCallsAnalyzed, "Number of call sites analyzed");
38
Chandler Carruth0539c072012-03-31 12:42:41 +000039namespace {
Chandler Carrutha3089552012-03-14 07:32:53 +000040
Chandler Carruth0539c072012-03-31 12:42:41 +000041class CallAnalyzer : public InstVisitor<CallAnalyzer, bool> {
42 typedef InstVisitor<CallAnalyzer, bool> Base;
43 friend class InstVisitor<CallAnalyzer, bool>;
Owen Andersona08318a2010-09-09 16:56:42 +000044
Micah Villmowcdfe20b2012-10-08 16:38:25 +000045 // DataLayout if available, or null.
46 const DataLayout *const TD;
Owen Andersona08318a2010-09-09 16:56:42 +000047
Chandler Carruth42f3dce2013-01-21 11:55:09 +000048 /// The TargetTransformInfo available for this compilation.
49 const TargetTransformInfo &TTI;
50
Chandler Carruth0539c072012-03-31 12:42:41 +000051 // The called function.
52 Function &F;
Owen Andersona08318a2010-09-09 16:56:42 +000053
Chandler Carruth0539c072012-03-31 12:42:41 +000054 int Threshold;
55 int Cost;
Owen Andersona08318a2010-09-09 16:56:42 +000056
Nadav Rotem4eb3d4b2012-09-19 08:08:04 +000057 bool IsCallerRecursive;
58 bool IsRecursiveCall;
Chandler Carruth0539c072012-03-31 12:42:41 +000059 bool ExposesReturnsTwice;
60 bool HasDynamicAlloca;
James Molloy4f6fb952012-12-20 16:04:27 +000061 bool ContainsNoDuplicateCall;
62
Nadav Rotem4eb3d4b2012-09-19 08:08:04 +000063 /// Number of bytes allocated statically by the callee.
64 uint64_t AllocatedSize;
Chandler Carruth0539c072012-03-31 12:42:41 +000065 unsigned NumInstructions, NumVectorInstructions;
66 int FiftyPercentVectorBonus, TenPercentVectorBonus;
67 int VectorBonus;
68
69 // While we walk the potentially-inlined instructions, we build up and
70 // maintain a mapping of simplified values specific to this callsite. The
71 // idea is to propagate any special information we have about arguments to
72 // this call through the inlinable section of the function, and account for
73 // likely simplifications post-inlining. The most important aspect we track
74 // is CFG altering simplifications -- when we prove a basic block dead, that
75 // can cause dramatic shifts in the cost of inlining a function.
76 DenseMap<Value *, Constant *> SimplifiedValues;
77
78 // Keep track of the values which map back (through function arguments) to
79 // allocas on the caller stack which could be simplified through SROA.
80 DenseMap<Value *, Value *> SROAArgValues;
81
82 // The mapping of caller Alloca values to their accumulated cost savings. If
83 // we have to disable SROA for one of the allocas, this tells us how much
84 // cost must be added.
85 DenseMap<Value *, int> SROAArgCosts;
86
87 // Keep track of values which map to a pointer base and constant offset.
88 DenseMap<Value *, std::pair<Value *, APInt> > ConstantOffsetPtrs;
89
90 // Custom simplification helper routines.
91 bool isAllocaDerivedArg(Value *V);
92 bool lookupSROAArgAndCost(Value *V, Value *&Arg,
93 DenseMap<Value *, int>::iterator &CostIt);
94 void disableSROA(DenseMap<Value *, int>::iterator CostIt);
95 void disableSROA(Value *V);
96 void accumulateSROACost(DenseMap<Value *, int>::iterator CostIt,
97 int InstructionCost);
98 bool handleSROACandidate(bool IsSROAValid,
99 DenseMap<Value *, int>::iterator CostIt,
100 int InstructionCost);
101 bool isGEPOffsetConstant(GetElementPtrInst &GEP);
102 bool accumulateGEPOffset(GEPOperator &GEP, APInt &Offset);
Chandler Carruth753e21d2012-12-28 14:23:32 +0000103 bool simplifyCallSite(Function *F, CallSite CS);
Chandler Carruth0539c072012-03-31 12:42:41 +0000104 ConstantInt *stripAndComputeInBoundsConstantOffsets(Value *&V);
105
106 // Custom analysis routines.
107 bool analyzeBlock(BasicBlock *BB);
108
109 // Disable several entry points to the visitor so we don't accidentally use
110 // them by declaring but not defining them here.
111 void visit(Module *); void visit(Module &);
112 void visit(Function *); void visit(Function &);
113 void visit(BasicBlock *); void visit(BasicBlock &);
114
115 // Provide base case for our instruction visit.
116 bool visitInstruction(Instruction &I);
117
118 // Our visit overrides.
119 bool visitAlloca(AllocaInst &I);
120 bool visitPHI(PHINode &I);
121 bool visitGetElementPtr(GetElementPtrInst &I);
122 bool visitBitCast(BitCastInst &I);
123 bool visitPtrToInt(PtrToIntInst &I);
124 bool visitIntToPtr(IntToPtrInst &I);
125 bool visitCastInst(CastInst &I);
126 bool visitUnaryInstruction(UnaryInstruction &I);
Matt Arsenault727aa342013-07-20 04:09:00 +0000127 bool visitCmpInst(CmpInst &I);
Chandler Carruth0539c072012-03-31 12:42:41 +0000128 bool visitSub(BinaryOperator &I);
129 bool visitBinaryOperator(BinaryOperator &I);
130 bool visitLoad(LoadInst &I);
131 bool visitStore(StoreInst &I);
Chandler Carruth753e21d2012-12-28 14:23:32 +0000132 bool visitExtractValue(ExtractValueInst &I);
133 bool visitInsertValue(InsertValueInst &I);
Chandler Carruth0539c072012-03-31 12:42:41 +0000134 bool visitCallSite(CallSite CS);
135
136public:
Chandler Carruth42f3dce2013-01-21 11:55:09 +0000137 CallAnalyzer(const DataLayout *TD, const TargetTransformInfo &TTI,
138 Function &Callee, int Threshold)
139 : TD(TD), TTI(TTI), F(Callee), Threshold(Threshold), Cost(0),
140 IsCallerRecursive(false), IsRecursiveCall(false),
141 ExposesReturnsTwice(false), HasDynamicAlloca(false),
142 ContainsNoDuplicateCall(false), AllocatedSize(0), NumInstructions(0),
143 NumVectorInstructions(0), FiftyPercentVectorBonus(0),
144 TenPercentVectorBonus(0), VectorBonus(0), NumConstantArgs(0),
145 NumConstantOffsetPtrArgs(0), NumAllocaArgs(0), NumConstantPtrCmps(0),
146 NumConstantPtrDiffs(0), NumInstructionsSimplified(0),
147 SROACostSavings(0), SROACostSavingsLost(0) {}
Chandler Carruth0539c072012-03-31 12:42:41 +0000148
149 bool analyzeCall(CallSite CS);
150
151 int getThreshold() { return Threshold; }
152 int getCost() { return Cost; }
153
154 // Keep a bunch of stats about the cost savings found so we can print them
155 // out when debugging.
156 unsigned NumConstantArgs;
157 unsigned NumConstantOffsetPtrArgs;
158 unsigned NumAllocaArgs;
159 unsigned NumConstantPtrCmps;
160 unsigned NumConstantPtrDiffs;
161 unsigned NumInstructionsSimplified;
162 unsigned SROACostSavings;
163 unsigned SROACostSavingsLost;
164
165 void dump();
166};
167
168} // namespace
169
170/// \brief Test whether the given value is an Alloca-derived function argument.
171bool CallAnalyzer::isAllocaDerivedArg(Value *V) {
172 return SROAArgValues.count(V);
Owen Andersona08318a2010-09-09 16:56:42 +0000173}
174
Chandler Carruth0539c072012-03-31 12:42:41 +0000175/// \brief Lookup the SROA-candidate argument and cost iterator which V maps to.
176/// Returns false if V does not map to a SROA-candidate.
177bool CallAnalyzer::lookupSROAArgAndCost(
178 Value *V, Value *&Arg, DenseMap<Value *, int>::iterator &CostIt) {
179 if (SROAArgValues.empty() || SROAArgCosts.empty())
180 return false;
Chandler Carruth783b7192012-03-09 02:49:36 +0000181
Chandler Carruth0539c072012-03-31 12:42:41 +0000182 DenseMap<Value *, Value *>::iterator ArgIt = SROAArgValues.find(V);
183 if (ArgIt == SROAArgValues.end())
184 return false;
Chandler Carruth783b7192012-03-09 02:49:36 +0000185
Chandler Carruth0539c072012-03-31 12:42:41 +0000186 Arg = ArgIt->second;
187 CostIt = SROAArgCosts.find(Arg);
188 return CostIt != SROAArgCosts.end();
Chandler Carruth783b7192012-03-09 02:49:36 +0000189}
190
Chandler Carruth0539c072012-03-31 12:42:41 +0000191/// \brief Disable SROA for the candidate marked by this cost iterator.
Chandler Carruth783b7192012-03-09 02:49:36 +0000192///
Benjamin Kramerbde91762012-06-02 10:20:22 +0000193/// This marks the candidate as no longer viable for SROA, and adds the cost
Chandler Carruth0539c072012-03-31 12:42:41 +0000194/// savings associated with it back into the inline cost measurement.
195void CallAnalyzer::disableSROA(DenseMap<Value *, int>::iterator CostIt) {
196 // If we're no longer able to perform SROA we need to undo its cost savings
197 // and prevent subsequent analysis.
198 Cost += CostIt->second;
199 SROACostSavings -= CostIt->second;
200 SROACostSavingsLost += CostIt->second;
201 SROAArgCosts.erase(CostIt);
202}
203
204/// \brief If 'V' maps to a SROA candidate, disable SROA for it.
205void CallAnalyzer::disableSROA(Value *V) {
206 Value *SROAArg;
207 DenseMap<Value *, int>::iterator CostIt;
208 if (lookupSROAArgAndCost(V, SROAArg, CostIt))
209 disableSROA(CostIt);
210}
211
212/// \brief Accumulate the given cost for a particular SROA candidate.
213void CallAnalyzer::accumulateSROACost(DenseMap<Value *, int>::iterator CostIt,
214 int InstructionCost) {
215 CostIt->second += InstructionCost;
216 SROACostSavings += InstructionCost;
217}
218
219/// \brief Helper for the common pattern of handling a SROA candidate.
220/// Either accumulates the cost savings if the SROA remains valid, or disables
221/// SROA for the candidate.
222bool CallAnalyzer::handleSROACandidate(bool IsSROAValid,
223 DenseMap<Value *, int>::iterator CostIt,
224 int InstructionCost) {
225 if (IsSROAValid) {
226 accumulateSROACost(CostIt, InstructionCost);
227 return true;
228 }
229
230 disableSROA(CostIt);
231 return false;
232}
233
234/// \brief Check whether a GEP's indices are all constant.
235///
236/// Respects any simplified values known during the analysis of this callsite.
237bool CallAnalyzer::isGEPOffsetConstant(GetElementPtrInst &GEP) {
238 for (User::op_iterator I = GEP.idx_begin(), E = GEP.idx_end(); I != E; ++I)
239 if (!isa<Constant>(*I) && !SimplifiedValues.lookup(*I))
Chandler Carruth783b7192012-03-09 02:49:36 +0000240 return false;
Chandler Carruth783b7192012-03-09 02:49:36 +0000241
Chandler Carruth0539c072012-03-31 12:42:41 +0000242 return true;
243}
244
245/// \brief Accumulate a constant GEP offset into an APInt if possible.
246///
247/// Returns false if unable to compute the offset for any reason. Respects any
248/// simplified values known during the analysis of this callsite.
249bool CallAnalyzer::accumulateGEPOffset(GEPOperator &GEP, APInt &Offset) {
250 if (!TD)
251 return false;
252
Chandler Carruth5da3f052012-11-01 09:14:31 +0000253 unsigned IntPtrWidth = TD->getPointerSizeInBits();
Chandler Carruth0539c072012-03-31 12:42:41 +0000254 assert(IntPtrWidth == Offset.getBitWidth());
255
256 for (gep_type_iterator GTI = gep_type_begin(GEP), GTE = gep_type_end(GEP);
257 GTI != GTE; ++GTI) {
258 ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand());
259 if (!OpC)
260 if (Constant *SimpleOp = SimplifiedValues.lookup(GTI.getOperand()))
261 OpC = dyn_cast<ConstantInt>(SimpleOp);
262 if (!OpC)
Chandler Carruth783b7192012-03-09 02:49:36 +0000263 return false;
Chandler Carruth0539c072012-03-31 12:42:41 +0000264 if (OpC->isZero()) continue;
Chandler Carruth783b7192012-03-09 02:49:36 +0000265
Chandler Carruth0539c072012-03-31 12:42:41 +0000266 // Handle a struct index, which adds its field offset to the pointer.
267 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
268 unsigned ElementIdx = OpC->getZExtValue();
269 const StructLayout *SL = TD->getStructLayout(STy);
270 Offset += APInt(IntPtrWidth, SL->getElementOffset(ElementIdx));
271 continue;
Chandler Carruth783b7192012-03-09 02:49:36 +0000272 }
Chandler Carruth783b7192012-03-09 02:49:36 +0000273
Chandler Carruth0539c072012-03-31 12:42:41 +0000274 APInt TypeSize(IntPtrWidth, TD->getTypeAllocSize(GTI.getIndexedType()));
275 Offset += OpC->getValue().sextOrTrunc(IntPtrWidth) * TypeSize;
276 }
277 return true;
278}
279
280bool CallAnalyzer::visitAlloca(AllocaInst &I) {
281 // FIXME: Check whether inlining will turn a dynamic alloca into a static
282 // alloca, and handle that case.
283
Nadav Rotem4eb3d4b2012-09-19 08:08:04 +0000284 // Accumulate the allocated size.
285 if (I.isStaticAlloca()) {
286 Type *Ty = I.getAllocatedType();
287 AllocatedSize += (TD ? TD->getTypeAllocSize(Ty) :
288 Ty->getPrimitiveSizeInBits());
289 }
290
Bob Wilsona5b0dc82012-11-19 07:04:35 +0000291 // We will happily inline static alloca instructions.
292 if (I.isStaticAlloca())
Chandler Carruth0539c072012-03-31 12:42:41 +0000293 return Base::visitAlloca(I);
294
295 // FIXME: This is overly conservative. Dynamic allocas are inefficient for
296 // a variety of reasons, and so we would like to not inline them into
297 // functions which don't currently have a dynamic alloca. This simply
298 // disables inlining altogether in the presence of a dynamic alloca.
299 HasDynamicAlloca = true;
300 return false;
301}
302
303bool CallAnalyzer::visitPHI(PHINode &I) {
304 // FIXME: We should potentially be tracking values through phi nodes,
305 // especially when they collapse to a single value due to deleted CFG edges
306 // during inlining.
307
308 // FIXME: We need to propagate SROA *disabling* through phi nodes, even
309 // though we don't want to propagate it's bonuses. The idea is to disable
310 // SROA if it *might* be used in an inappropriate manner.
311
312 // Phi nodes are always zero-cost.
313 return true;
314}
315
316bool CallAnalyzer::visitGetElementPtr(GetElementPtrInst &I) {
317 Value *SROAArg;
318 DenseMap<Value *, int>::iterator CostIt;
319 bool SROACandidate = lookupSROAArgAndCost(I.getPointerOperand(),
320 SROAArg, CostIt);
321
322 // Try to fold GEPs of constant-offset call site argument pointers. This
323 // requires target data and inbounds GEPs.
324 if (TD && I.isInBounds()) {
325 // Check if we have a base + offset for the pointer.
326 Value *Ptr = I.getPointerOperand();
327 std::pair<Value *, APInt> BaseAndOffset = ConstantOffsetPtrs.lookup(Ptr);
328 if (BaseAndOffset.first) {
329 // Check if the offset of this GEP is constant, and if so accumulate it
330 // into Offset.
331 if (!accumulateGEPOffset(cast<GEPOperator>(I), BaseAndOffset.second)) {
332 // Non-constant GEPs aren't folded, and disable SROA.
333 if (SROACandidate)
334 disableSROA(CostIt);
335 return false;
336 }
337
338 // Add the result as a new mapping to Base + Offset.
339 ConstantOffsetPtrs[&I] = BaseAndOffset;
340
341 // Also handle SROA candidates here, we already know that the GEP is
342 // all-constant indexed.
343 if (SROACandidate)
344 SROAArgValues[&I] = SROAArg;
345
Chandler Carruth783b7192012-03-09 02:49:36 +0000346 return true;
347 }
348 }
349
Chandler Carruth0539c072012-03-31 12:42:41 +0000350 if (isGEPOffsetConstant(I)) {
351 if (SROACandidate)
352 SROAArgValues[&I] = SROAArg;
353
354 // Constant GEPs are modeled as free.
355 return true;
356 }
357
358 // Variable GEPs will require math and will disable SROA.
359 if (SROACandidate)
360 disableSROA(CostIt);
Chandler Carruth783b7192012-03-09 02:49:36 +0000361 return false;
362}
363
Chandler Carruth0539c072012-03-31 12:42:41 +0000364bool CallAnalyzer::visitBitCast(BitCastInst &I) {
365 // Propagate constants through bitcasts.
Chandler Carruth86ed5302012-12-28 14:43:42 +0000366 Constant *COp = dyn_cast<Constant>(I.getOperand(0));
367 if (!COp)
368 COp = SimplifiedValues.lookup(I.getOperand(0));
369 if (COp)
Chandler Carruth0539c072012-03-31 12:42:41 +0000370 if (Constant *C = ConstantExpr::getBitCast(COp, I.getType())) {
371 SimplifiedValues[&I] = C;
372 return true;
Owen Andersona08318a2010-09-09 16:56:42 +0000373 }
Owen Andersona08318a2010-09-09 16:56:42 +0000374
Chandler Carruth0539c072012-03-31 12:42:41 +0000375 // Track base/offsets through casts
376 std::pair<Value *, APInt> BaseAndOffset
377 = ConstantOffsetPtrs.lookup(I.getOperand(0));
378 // Casts don't change the offset, just wrap it up.
379 if (BaseAndOffset.first)
380 ConstantOffsetPtrs[&I] = BaseAndOffset;
381
382 // Also look for SROA candidates here.
383 Value *SROAArg;
384 DenseMap<Value *, int>::iterator CostIt;
385 if (lookupSROAArgAndCost(I.getOperand(0), SROAArg, CostIt))
386 SROAArgValues[&I] = SROAArg;
387
388 // Bitcasts are always zero cost.
389 return true;
Owen Andersona08318a2010-09-09 16:56:42 +0000390}
391
Chandler Carruth0539c072012-03-31 12:42:41 +0000392bool CallAnalyzer::visitPtrToInt(PtrToIntInst &I) {
393 // Propagate constants through ptrtoint.
Chandler Carruth86ed5302012-12-28 14:43:42 +0000394 Constant *COp = dyn_cast<Constant>(I.getOperand(0));
395 if (!COp)
396 COp = SimplifiedValues.lookup(I.getOperand(0));
397 if (COp)
Chandler Carruth0539c072012-03-31 12:42:41 +0000398 if (Constant *C = ConstantExpr::getPtrToInt(COp, I.getType())) {
399 SimplifiedValues[&I] = C;
400 return true;
Chandler Carruth4d1d34f2012-03-14 23:19:53 +0000401 }
Chandler Carruth0539c072012-03-31 12:42:41 +0000402
403 // Track base/offset pairs when converted to a plain integer provided the
404 // integer is large enough to represent the pointer.
405 unsigned IntegerSize = I.getType()->getScalarSizeInBits();
Chandler Carruth5da3f052012-11-01 09:14:31 +0000406 if (TD && IntegerSize >= TD->getPointerSizeInBits()) {
Chandler Carruth0539c072012-03-31 12:42:41 +0000407 std::pair<Value *, APInt> BaseAndOffset
408 = ConstantOffsetPtrs.lookup(I.getOperand(0));
409 if (BaseAndOffset.first)
410 ConstantOffsetPtrs[&I] = BaseAndOffset;
411 }
412
413 // This is really weird. Technically, ptrtoint will disable SROA. However,
414 // unless that ptrtoint is *used* somewhere in the live basic blocks after
415 // inlining, it will be nuked, and SROA should proceed. All of the uses which
416 // would block SROA would also block SROA if applied directly to a pointer,
417 // and so we can just add the integer in here. The only places where SROA is
418 // preserved either cannot fire on an integer, or won't in-and-of themselves
419 // disable SROA (ext) w/o some later use that we would see and disable.
420 Value *SROAArg;
421 DenseMap<Value *, int>::iterator CostIt;
422 if (lookupSROAArgAndCost(I.getOperand(0), SROAArg, CostIt))
423 SROAArgValues[&I] = SROAArg;
424
Chandler Carruthb8cf5102013-01-21 12:05:16 +0000425 return TargetTransformInfo::TCC_Free == TTI.getUserCost(&I);
Chandler Carruth4d1d34f2012-03-14 23:19:53 +0000426}
427
Chandler Carruth0539c072012-03-31 12:42:41 +0000428bool CallAnalyzer::visitIntToPtr(IntToPtrInst &I) {
429 // Propagate constants through ptrtoint.
Chandler Carruth86ed5302012-12-28 14:43:42 +0000430 Constant *COp = dyn_cast<Constant>(I.getOperand(0));
431 if (!COp)
432 COp = SimplifiedValues.lookup(I.getOperand(0));
433 if (COp)
Chandler Carruth0539c072012-03-31 12:42:41 +0000434 if (Constant *C = ConstantExpr::getIntToPtr(COp, I.getType())) {
435 SimplifiedValues[&I] = C;
436 return true;
437 }
Dan Gohman4552e3c2009-10-13 18:30:07 +0000438
Chandler Carruth0539c072012-03-31 12:42:41 +0000439 // Track base/offset pairs when round-tripped through a pointer without
440 // modifications provided the integer is not too large.
441 Value *Op = I.getOperand(0);
442 unsigned IntegerSize = Op->getType()->getScalarSizeInBits();
Chandler Carruth5da3f052012-11-01 09:14:31 +0000443 if (TD && IntegerSize <= TD->getPointerSizeInBits()) {
Chandler Carruth0539c072012-03-31 12:42:41 +0000444 std::pair<Value *, APInt> BaseAndOffset = ConstantOffsetPtrs.lookup(Op);
445 if (BaseAndOffset.first)
446 ConstantOffsetPtrs[&I] = BaseAndOffset;
447 }
Dan Gohman4552e3c2009-10-13 18:30:07 +0000448
Chandler Carruth0539c072012-03-31 12:42:41 +0000449 // "Propagate" SROA here in the same manner as we do for ptrtoint above.
450 Value *SROAArg;
451 DenseMap<Value *, int>::iterator CostIt;
452 if (lookupSROAArgAndCost(Op, SROAArg, CostIt))
453 SROAArgValues[&I] = SROAArg;
Chandler Carruth4d1d34f2012-03-14 23:19:53 +0000454
Chandler Carruthb8cf5102013-01-21 12:05:16 +0000455 return TargetTransformInfo::TCC_Free == TTI.getUserCost(&I);
Chandler Carruth0539c072012-03-31 12:42:41 +0000456}
457
458bool CallAnalyzer::visitCastInst(CastInst &I) {
459 // Propagate constants through ptrtoint.
Chandler Carruth86ed5302012-12-28 14:43:42 +0000460 Constant *COp = dyn_cast<Constant>(I.getOperand(0));
461 if (!COp)
462 COp = SimplifiedValues.lookup(I.getOperand(0));
463 if (COp)
Chandler Carruth0539c072012-03-31 12:42:41 +0000464 if (Constant *C = ConstantExpr::getCast(I.getOpcode(), COp, I.getType())) {
465 SimplifiedValues[&I] = C;
466 return true;
467 }
468
469 // Disable SROA in the face of arbitrary casts we don't whitelist elsewhere.
470 disableSROA(I.getOperand(0));
471
Chandler Carruthb8cf5102013-01-21 12:05:16 +0000472 return TargetTransformInfo::TCC_Free == TTI.getUserCost(&I);
Chandler Carruth0539c072012-03-31 12:42:41 +0000473}
474
475bool CallAnalyzer::visitUnaryInstruction(UnaryInstruction &I) {
476 Value *Operand = I.getOperand(0);
Jakub Staszak7b9e0b92013-03-07 20:01:19 +0000477 Constant *COp = dyn_cast<Constant>(Operand);
478 if (!COp)
479 COp = SimplifiedValues.lookup(Operand);
480 if (COp)
Chandler Carruth0539c072012-03-31 12:42:41 +0000481 if (Constant *C = ConstantFoldInstOperands(I.getOpcode(), I.getType(),
Jakub Staszak7b9e0b92013-03-07 20:01:19 +0000482 COp, TD)) {
Chandler Carruth0539c072012-03-31 12:42:41 +0000483 SimplifiedValues[&I] = C;
484 return true;
485 }
486
487 // Disable any SROA on the argument to arbitrary unary operators.
488 disableSROA(Operand);
489
490 return false;
491}
492
Matt Arsenault727aa342013-07-20 04:09:00 +0000493bool CallAnalyzer::visitCmpInst(CmpInst &I) {
Chandler Carruth0539c072012-03-31 12:42:41 +0000494 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
495 // First try to handle simplified comparisons.
496 if (!isa<Constant>(LHS))
497 if (Constant *SimpleLHS = SimplifiedValues.lookup(LHS))
498 LHS = SimpleLHS;
499 if (!isa<Constant>(RHS))
500 if (Constant *SimpleRHS = SimplifiedValues.lookup(RHS))
501 RHS = SimpleRHS;
Matt Arsenault727aa342013-07-20 04:09:00 +0000502 if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
Chandler Carruth0539c072012-03-31 12:42:41 +0000503 if (Constant *CRHS = dyn_cast<Constant>(RHS))
Matt Arsenault727aa342013-07-20 04:09:00 +0000504 if (Constant *C = ConstantExpr::getCompare(I.getPredicate(), CLHS, CRHS)) {
Chandler Carruth0539c072012-03-31 12:42:41 +0000505 SimplifiedValues[&I] = C;
506 return true;
507 }
Matt Arsenault727aa342013-07-20 04:09:00 +0000508 }
509
510 if (I.getOpcode() == Instruction::FCmp)
511 return false;
Chandler Carruth0539c072012-03-31 12:42:41 +0000512
513 // Otherwise look for a comparison between constant offset pointers with
514 // a common base.
515 Value *LHSBase, *RHSBase;
516 APInt LHSOffset, RHSOffset;
517 llvm::tie(LHSBase, LHSOffset) = ConstantOffsetPtrs.lookup(LHS);
518 if (LHSBase) {
519 llvm::tie(RHSBase, RHSOffset) = ConstantOffsetPtrs.lookup(RHS);
520 if (RHSBase && LHSBase == RHSBase) {
521 // We have common bases, fold the icmp to a constant based on the
522 // offsets.
523 Constant *CLHS = ConstantInt::get(LHS->getContext(), LHSOffset);
524 Constant *CRHS = ConstantInt::get(RHS->getContext(), RHSOffset);
525 if (Constant *C = ConstantExpr::getICmp(I.getPredicate(), CLHS, CRHS)) {
526 SimplifiedValues[&I] = C;
527 ++NumConstantPtrCmps;
528 return true;
529 }
530 }
531 }
532
533 // If the comparison is an equality comparison with null, we can simplify it
534 // for any alloca-derived argument.
535 if (I.isEquality() && isa<ConstantPointerNull>(I.getOperand(1)))
536 if (isAllocaDerivedArg(I.getOperand(0))) {
537 // We can actually predict the result of comparisons between an
538 // alloca-derived value and null. Note that this fires regardless of
539 // SROA firing.
540 bool IsNotEqual = I.getPredicate() == CmpInst::ICMP_NE;
541 SimplifiedValues[&I] = IsNotEqual ? ConstantInt::getTrue(I.getType())
542 : ConstantInt::getFalse(I.getType());
543 return true;
544 }
545
546 // Finally check for SROA candidates in comparisons.
547 Value *SROAArg;
548 DenseMap<Value *, int>::iterator CostIt;
549 if (lookupSROAArgAndCost(I.getOperand(0), SROAArg, CostIt)) {
550 if (isa<ConstantPointerNull>(I.getOperand(1))) {
551 accumulateSROACost(CostIt, InlineConstants::InstrCost);
552 return true;
553 }
554
555 disableSROA(CostIt);
556 }
557
558 return false;
559}
560
561bool CallAnalyzer::visitSub(BinaryOperator &I) {
562 // Try to handle a special case: we can fold computing the difference of two
563 // constant-related pointers.
564 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
565 Value *LHSBase, *RHSBase;
566 APInt LHSOffset, RHSOffset;
567 llvm::tie(LHSBase, LHSOffset) = ConstantOffsetPtrs.lookup(LHS);
568 if (LHSBase) {
569 llvm::tie(RHSBase, RHSOffset) = ConstantOffsetPtrs.lookup(RHS);
570 if (RHSBase && LHSBase == RHSBase) {
571 // We have common bases, fold the subtract to a constant based on the
572 // offsets.
573 Constant *CLHS = ConstantInt::get(LHS->getContext(), LHSOffset);
574 Constant *CRHS = ConstantInt::get(RHS->getContext(), RHSOffset);
575 if (Constant *C = ConstantExpr::getSub(CLHS, CRHS)) {
576 SimplifiedValues[&I] = C;
577 ++NumConstantPtrDiffs;
578 return true;
579 }
580 }
581 }
582
583 // Otherwise, fall back to the generic logic for simplifying and handling
584 // instructions.
585 return Base::visitSub(I);
586}
587
588bool CallAnalyzer::visitBinaryOperator(BinaryOperator &I) {
589 Value *LHS = I.getOperand(0), *RHS = I.getOperand(1);
590 if (!isa<Constant>(LHS))
591 if (Constant *SimpleLHS = SimplifiedValues.lookup(LHS))
592 LHS = SimpleLHS;
593 if (!isa<Constant>(RHS))
594 if (Constant *SimpleRHS = SimplifiedValues.lookup(RHS))
595 RHS = SimpleRHS;
596 Value *SimpleV = SimplifyBinOp(I.getOpcode(), LHS, RHS, TD);
597 if (Constant *C = dyn_cast_or_null<Constant>(SimpleV)) {
598 SimplifiedValues[&I] = C;
599 return true;
600 }
601
602 // Disable any SROA on arguments to arbitrary, unsimplified binary operators.
603 disableSROA(LHS);
604 disableSROA(RHS);
605
606 return false;
607}
608
609bool CallAnalyzer::visitLoad(LoadInst &I) {
610 Value *SROAArg;
611 DenseMap<Value *, int>::iterator CostIt;
612 if (lookupSROAArgAndCost(I.getOperand(0), SROAArg, CostIt)) {
613 if (I.isSimple()) {
614 accumulateSROACost(CostIt, InlineConstants::InstrCost);
615 return true;
616 }
617
618 disableSROA(CostIt);
619 }
620
621 return false;
622}
623
624bool CallAnalyzer::visitStore(StoreInst &I) {
625 Value *SROAArg;
626 DenseMap<Value *, int>::iterator CostIt;
627 if (lookupSROAArgAndCost(I.getOperand(0), SROAArg, CostIt)) {
628 if (I.isSimple()) {
629 accumulateSROACost(CostIt, InlineConstants::InstrCost);
630 return true;
631 }
632
633 disableSROA(CostIt);
634 }
635
636 return false;
637}
638
Chandler Carruth753e21d2012-12-28 14:23:32 +0000639bool CallAnalyzer::visitExtractValue(ExtractValueInst &I) {
640 // Constant folding for extract value is trivial.
641 Constant *C = dyn_cast<Constant>(I.getAggregateOperand());
642 if (!C)
643 C = SimplifiedValues.lookup(I.getAggregateOperand());
644 if (C) {
645 SimplifiedValues[&I] = ConstantExpr::getExtractValue(C, I.getIndices());
646 return true;
647 }
648
649 // SROA can look through these but give them a cost.
650 return false;
651}
652
653bool CallAnalyzer::visitInsertValue(InsertValueInst &I) {
654 // Constant folding for insert value is trivial.
655 Constant *AggC = dyn_cast<Constant>(I.getAggregateOperand());
656 if (!AggC)
657 AggC = SimplifiedValues.lookup(I.getAggregateOperand());
658 Constant *InsertedC = dyn_cast<Constant>(I.getInsertedValueOperand());
659 if (!InsertedC)
660 InsertedC = SimplifiedValues.lookup(I.getInsertedValueOperand());
661 if (AggC && InsertedC) {
662 SimplifiedValues[&I] = ConstantExpr::getInsertValue(AggC, InsertedC,
663 I.getIndices());
664 return true;
665 }
666
667 // SROA can look through these but give them a cost.
668 return false;
669}
670
671/// \brief Try to simplify a call site.
672///
673/// Takes a concrete function and callsite and tries to actually simplify it by
674/// analyzing the arguments and call itself with instsimplify. Returns true if
675/// it has simplified the callsite to some other entity (a constant), making it
676/// free.
677bool CallAnalyzer::simplifyCallSite(Function *F, CallSite CS) {
678 // FIXME: Using the instsimplify logic directly for this is inefficient
679 // because we have to continually rebuild the argument list even when no
680 // simplifications can be performed. Until that is fixed with remapping
681 // inside of instsimplify, directly constant fold calls here.
682 if (!canConstantFoldCallTo(F))
683 return false;
684
685 // Try to re-map the arguments to constants.
686 SmallVector<Constant *, 4> ConstantArgs;
687 ConstantArgs.reserve(CS.arg_size());
688 for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
689 I != E; ++I) {
690 Constant *C = dyn_cast<Constant>(*I);
691 if (!C)
692 C = dyn_cast_or_null<Constant>(SimplifiedValues.lookup(*I));
693 if (!C)
694 return false; // This argument doesn't map to a constant.
695
696 ConstantArgs.push_back(C);
697 }
698 if (Constant *C = ConstantFoldCall(F, ConstantArgs)) {
699 SimplifiedValues[CS.getInstruction()] = C;
700 return true;
701 }
702
703 return false;
704}
705
Chandler Carruth0539c072012-03-31 12:42:41 +0000706bool CallAnalyzer::visitCallSite(CallSite CS) {
707 if (CS.isCall() && cast<CallInst>(CS.getInstruction())->canReturnTwice() &&
Bill Wendling698e84f2012-12-30 10:32:01 +0000708 !F.getAttributes().hasAttribute(AttributeSet::FunctionIndex,
709 Attribute::ReturnsTwice)) {
Chandler Carruth0539c072012-03-31 12:42:41 +0000710 // This aborts the entire analysis.
711 ExposesReturnsTwice = true;
712 return false;
713 }
James Molloy4f6fb952012-12-20 16:04:27 +0000714 if (CS.isCall() &&
715 cast<CallInst>(CS.getInstruction())->hasFnAttr(Attribute::NoDuplicate))
716 ContainsNoDuplicateCall = true;
Chandler Carruth0539c072012-03-31 12:42:41 +0000717
Chandler Carruth0539c072012-03-31 12:42:41 +0000718 if (Function *F = CS.getCalledFunction()) {
Chandler Carruth753e21d2012-12-28 14:23:32 +0000719 // When we have a concrete function, first try to simplify it directly.
720 if (simplifyCallSite(F, CS))
721 return true;
722
723 // Next check if it is an intrinsic we know about.
724 // FIXME: Lift this into part of the InstVisitor.
725 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(CS.getInstruction())) {
726 switch (II->getIntrinsicID()) {
727 default:
728 return Base::visitCallSite(CS);
729
730 case Intrinsic::memset:
731 case Intrinsic::memcpy:
732 case Intrinsic::memmove:
733 // SROA can usually chew through these intrinsics, but they aren't free.
734 return false;
735 }
736 }
737
Chandler Carruth0539c072012-03-31 12:42:41 +0000738 if (F == CS.getInstruction()->getParent()->getParent()) {
739 // This flag will fully abort the analysis, so don't bother with anything
740 // else.
Nadav Rotem4eb3d4b2012-09-19 08:08:04 +0000741 IsRecursiveCall = true;
Chandler Carruth0539c072012-03-31 12:42:41 +0000742 return false;
743 }
744
Chandler Carruth0ba8db42013-01-22 11:26:02 +0000745 if (TTI.isLoweredToCall(F)) {
Chandler Carruth0539c072012-03-31 12:42:41 +0000746 // We account for the average 1 instruction per call argument setup
747 // here.
748 Cost += CS.arg_size() * InlineConstants::InstrCost;
749
750 // Everything other than inline ASM will also have a significant cost
751 // merely from making the call.
752 if (!isa<InlineAsm>(CS.getCalledValue()))
753 Cost += InlineConstants::CallPenalty;
754 }
755
756 return Base::visitCallSite(CS);
757 }
758
759 // Otherwise we're in a very special case -- an indirect function call. See
760 // if we can be particularly clever about this.
761 Value *Callee = CS.getCalledValue();
762
763 // First, pay the price of the argument setup. We account for the average
764 // 1 instruction per call argument setup here.
765 Cost += CS.arg_size() * InlineConstants::InstrCost;
766
767 // Next, check if this happens to be an indirect function call to a known
768 // function in this inline context. If not, we've done all we can.
769 Function *F = dyn_cast_or_null<Function>(SimplifiedValues.lookup(Callee));
770 if (!F)
771 return Base::visitCallSite(CS);
772
773 // If we have a constant that we are calling as a function, we can peer
774 // through it and see the function target. This happens not infrequently
775 // during devirtualization and so we want to give it a hefty bonus for
776 // inlining, but cap that bonus in the event that inlining wouldn't pan
777 // out. Pretend to inline the function, with a custom threshold.
Chandler Carruth42f3dce2013-01-21 11:55:09 +0000778 CallAnalyzer CA(TD, TTI, *F, InlineConstants::IndirectCallThreshold);
Chandler Carruth0539c072012-03-31 12:42:41 +0000779 if (CA.analyzeCall(CS)) {
780 // We were able to inline the indirect call! Subtract the cost from the
781 // bonus we want to apply, but don't go below zero.
782 Cost -= std::max(0, InlineConstants::IndirectCallThreshold - CA.getCost());
783 }
784
785 return Base::visitCallSite(CS);
786}
787
788bool CallAnalyzer::visitInstruction(Instruction &I) {
Chandler Carruthda7513a2012-05-04 00:58:03 +0000789 // Some instructions are free. All of the free intrinsics can also be
790 // handled by SROA, etc.
Chandler Carruthb8cf5102013-01-21 12:05:16 +0000791 if (TargetTransformInfo::TCC_Free == TTI.getUserCost(&I))
Chandler Carruthda7513a2012-05-04 00:58:03 +0000792 return true;
793
Chandler Carruth0539c072012-03-31 12:42:41 +0000794 // We found something we don't understand or can't handle. Mark any SROA-able
795 // values in the operand list as no longer viable.
796 for (User::op_iterator OI = I.op_begin(), OE = I.op_end(); OI != OE; ++OI)
797 disableSROA(*OI);
798
799 return false;
800}
801
802
803/// \brief Analyze a basic block for its contribution to the inline cost.
804///
805/// This method walks the analyzer over every instruction in the given basic
806/// block and accounts for their cost during inlining at this callsite. It
807/// aborts early if the threshold has been exceeded or an impossible to inline
808/// construct has been detected. It returns false if inlining is no longer
809/// viable, and true if inlining remains viable.
810bool CallAnalyzer::analyzeBlock(BasicBlock *BB) {
811 for (BasicBlock::iterator I = BB->begin(), E = llvm::prior(BB->end());
812 I != E; ++I) {
813 ++NumInstructions;
814 if (isa<ExtractElementInst>(I) || I->getType()->isVectorTy())
815 ++NumVectorInstructions;
816
817 // If the instruction simplified to a constant, there is no cost to this
818 // instruction. Visit the instructions using our InstVisitor to account for
819 // all of the per-instruction logic. The visit tree returns true if we
820 // consumed the instruction in any way, and false if the instruction's base
821 // cost should count against inlining.
822 if (Base::visit(I))
823 ++NumInstructionsSimplified;
824 else
825 Cost += InlineConstants::InstrCost;
826
827 // If the visit this instruction detected an uninlinable pattern, abort.
Nadav Rotem4eb3d4b2012-09-19 08:08:04 +0000828 if (IsRecursiveCall || ExposesReturnsTwice || HasDynamicAlloca)
829 return false;
830
831 // If the caller is a recursive function then we don't want to inline
832 // functions which allocate a lot of stack space because it would increase
833 // the caller stack usage dramatically.
834 if (IsCallerRecursive &&
835 AllocatedSize > InlineConstants::TotalAllocaSizeRecursiveCaller)
Chandler Carruth0539c072012-03-31 12:42:41 +0000836 return false;
837
838 if (NumVectorInstructions > NumInstructions/2)
839 VectorBonus = FiftyPercentVectorBonus;
840 else if (NumVectorInstructions > NumInstructions/10)
841 VectorBonus = TenPercentVectorBonus;
842 else
843 VectorBonus = 0;
844
845 // Check if we've past the threshold so we don't spin in huge basic
846 // blocks that will never inline.
Bob Wilsona5b0dc82012-11-19 07:04:35 +0000847 if (Cost > (Threshold + VectorBonus))
Chandler Carruth0539c072012-03-31 12:42:41 +0000848 return false;
849 }
850
851 return true;
852}
853
854/// \brief Compute the base pointer and cumulative constant offsets for V.
855///
856/// This strips all constant offsets off of V, leaving it the base pointer, and
857/// accumulates the total constant offset applied in the returned constant. It
858/// returns 0 if V is not a pointer, and returns the constant '0' if there are
859/// no constant offsets applied.
860ConstantInt *CallAnalyzer::stripAndComputeInBoundsConstantOffsets(Value *&V) {
861 if (!TD || !V->getType()->isPointerTy())
862 return 0;
863
Chandler Carruth5da3f052012-11-01 09:14:31 +0000864 unsigned IntPtrWidth = TD->getPointerSizeInBits();
Chandler Carruth0539c072012-03-31 12:42:41 +0000865 APInt Offset = APInt::getNullValue(IntPtrWidth);
866
867 // Even though we don't look through PHI nodes, we could be called on an
868 // instruction in an unreachable block, which may be on a cycle.
869 SmallPtrSet<Value *, 4> Visited;
870 Visited.insert(V);
871 do {
872 if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) {
873 if (!GEP->isInBounds() || !accumulateGEPOffset(*GEP, Offset))
874 return 0;
875 V = GEP->getPointerOperand();
876 } else if (Operator::getOpcode(V) == Instruction::BitCast) {
877 V = cast<Operator>(V)->getOperand(0);
878 } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
879 if (GA->mayBeOverridden())
880 break;
881 V = GA->getAliasee();
882 } else {
883 break;
884 }
885 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
886 } while (Visited.insert(V));
887
Chandler Carruth7ec50852012-11-01 08:07:29 +0000888 Type *IntPtrTy = TD->getIntPtrType(V->getContext());
Chandler Carruth0539c072012-03-31 12:42:41 +0000889 return cast<ConstantInt>(ConstantInt::get(IntPtrTy, Offset));
890}
891
892/// \brief Analyze a call site for potential inlining.
893///
894/// Returns true if inlining this call is viable, and false if it is not
895/// viable. It computes the cost and adjusts the threshold based on numerous
896/// factors and heuristics. If this method returns false but the computed cost
897/// is below the computed threshold, then inlining was forcibly disabled by
Bob Wilson266802d2012-11-19 07:04:30 +0000898/// some artifact of the routine.
Chandler Carruth0539c072012-03-31 12:42:41 +0000899bool CallAnalyzer::analyzeCall(CallSite CS) {
Chandler Carruth7ae90d42012-04-11 10:15:10 +0000900 ++NumCallsAnalyzed;
901
Chandler Carruth0539c072012-03-31 12:42:41 +0000902 // Track whether the post-inlining function would have more than one basic
903 // block. A single basic block is often intended for inlining. Balloon the
904 // threshold by 50% until we pass the single-BB phase.
905 bool SingleBB = true;
906 int SingleBBBonus = Threshold / 2;
907 Threshold += SingleBBBonus;
908
Bob Wilsona5b0dc82012-11-19 07:04:35 +0000909 // Perform some tweaks to the cost and threshold based on the direct
910 // callsite information.
Chandler Carruth0539c072012-03-31 12:42:41 +0000911
Bob Wilsona5b0dc82012-11-19 07:04:35 +0000912 // We want to more aggressively inline vector-dense kernels, so up the
913 // threshold, and we'll lower it if the % of vector instructions gets too
914 // low.
915 assert(NumInstructions == 0);
916 assert(NumVectorInstructions == 0);
917 FiftyPercentVectorBonus = Threshold;
918 TenPercentVectorBonus = Threshold / 2;
Benjamin Kramerc99d0e92012-08-07 11:13:19 +0000919
Bob Wilsona5b0dc82012-11-19 07:04:35 +0000920 // Give out bonuses per argument, as the instructions setting them up will
921 // be gone after inlining.
922 for (unsigned I = 0, E = CS.arg_size(); I != E; ++I) {
923 if (TD && CS.isByValArgument(I)) {
924 // We approximate the number of loads and stores needed by dividing the
925 // size of the byval type by the target's pointer size.
926 PointerType *PTy = cast<PointerType>(CS.getArgument(I)->getType());
927 unsigned TypeSize = TD->getTypeSizeInBits(PTy->getElementType());
928 unsigned PointerSize = TD->getPointerSizeInBits();
929 // Ceiling division.
930 unsigned NumStores = (TypeSize + PointerSize - 1) / PointerSize;
Benjamin Kramerc99d0e92012-08-07 11:13:19 +0000931
Bob Wilsona5b0dc82012-11-19 07:04:35 +0000932 // If it generates more than 8 stores it is likely to be expanded as an
933 // inline memcpy so we take that as an upper bound. Otherwise we assume
934 // one load and one store per word copied.
935 // FIXME: The maxStoresPerMemcpy setting from the target should be used
936 // here instead of a magic number of 8, but it's not available via
937 // DataLayout.
938 NumStores = std::min(NumStores, 8U);
939
940 Cost -= 2 * NumStores * InlineConstants::InstrCost;
941 } else {
942 // For non-byval arguments subtract off one instruction per call
943 // argument.
944 Cost -= InlineConstants::InstrCost;
Benjamin Kramerc99d0e92012-08-07 11:13:19 +0000945 }
Chandler Carruth0539c072012-03-31 12:42:41 +0000946 }
947
Bob Wilsona5b0dc82012-11-19 07:04:35 +0000948 // If there is only one call of the function, and it has internal linkage,
949 // the cost of inlining it drops dramatically.
James Molloy4f6fb952012-12-20 16:04:27 +0000950 bool OnlyOneCallAndLocalLinkage = F.hasLocalLinkage() && F.hasOneUse() &&
951 &F == CS.getCalledFunction();
952 if (OnlyOneCallAndLocalLinkage)
Bob Wilsona5b0dc82012-11-19 07:04:35 +0000953 Cost += InlineConstants::LastCallToStaticBonus;
954
955 // If the instruction after the call, or if the normal destination of the
956 // invoke is an unreachable instruction, the function is noreturn. As such,
957 // there is little point in inlining this unless there is literally zero
958 // cost.
959 Instruction *Instr = CS.getInstruction();
960 if (InvokeInst *II = dyn_cast<InvokeInst>(Instr)) {
961 if (isa<UnreachableInst>(II->getNormalDest()->begin()))
962 Threshold = 1;
963 } else if (isa<UnreachableInst>(++BasicBlock::iterator(Instr)))
964 Threshold = 1;
965
966 // If this function uses the coldcc calling convention, prefer not to inline
967 // it.
968 if (F.getCallingConv() == CallingConv::Cold)
969 Cost += InlineConstants::ColdccPenalty;
970
971 // Check if we're done. This can happen due to bonuses and penalties.
972 if (Cost > Threshold)
973 return false;
974
Chandler Carruth0539c072012-03-31 12:42:41 +0000975 if (F.empty())
976 return true;
977
Nadav Rotem4eb3d4b2012-09-19 08:08:04 +0000978 Function *Caller = CS.getInstruction()->getParent()->getParent();
979 // Check if the caller function is recursive itself.
980 for (Value::use_iterator U = Caller->use_begin(), E = Caller->use_end();
981 U != E; ++U) {
982 CallSite Site(cast<Value>(*U));
983 if (!Site)
984 continue;
985 Instruction *I = Site.getInstruction();
986 if (I->getParent()->getParent() == Caller) {
987 IsCallerRecursive = true;
988 break;
989 }
990 }
991
Chandler Carruth0539c072012-03-31 12:42:41 +0000992 // Track whether we've seen a return instruction. The first return
993 // instruction is free, as at least one will usually disappear in inlining.
994 bool HasReturn = false;
995
996 // Populate our simplified values by mapping from function arguments to call
997 // arguments with known important simplifications.
998 CallSite::arg_iterator CAI = CS.arg_begin();
999 for (Function::arg_iterator FAI = F.arg_begin(), FAE = F.arg_end();
1000 FAI != FAE; ++FAI, ++CAI) {
1001 assert(CAI != CS.arg_end());
1002 if (Constant *C = dyn_cast<Constant>(CAI))
1003 SimplifiedValues[FAI] = C;
1004
1005 Value *PtrArg = *CAI;
1006 if (ConstantInt *C = stripAndComputeInBoundsConstantOffsets(PtrArg)) {
1007 ConstantOffsetPtrs[FAI] = std::make_pair(PtrArg, C->getValue());
1008
1009 // We can SROA any pointer arguments derived from alloca instructions.
1010 if (isa<AllocaInst>(PtrArg)) {
1011 SROAArgValues[FAI] = PtrArg;
1012 SROAArgCosts[PtrArg] = 0;
1013 }
1014 }
1015 }
1016 NumConstantArgs = SimplifiedValues.size();
1017 NumConstantOffsetPtrArgs = ConstantOffsetPtrs.size();
1018 NumAllocaArgs = SROAArgValues.size();
1019
1020 // The worklist of live basic blocks in the callee *after* inlining. We avoid
1021 // adding basic blocks of the callee which can be proven to be dead for this
1022 // particular call site in order to get more accurate cost estimates. This
1023 // requires a somewhat heavyweight iteration pattern: we need to walk the
1024 // basic blocks in a breadth-first order as we insert live successors. To
1025 // accomplish this, prioritizing for small iterations because we exit after
1026 // crossing our threshold, we use a small-size optimized SetVector.
1027 typedef SetVector<BasicBlock *, SmallVector<BasicBlock *, 16>,
1028 SmallPtrSet<BasicBlock *, 16> > BBSetVector;
1029 BBSetVector BBWorklist;
1030 BBWorklist.insert(&F.getEntryBlock());
1031 // Note that we *must not* cache the size, this loop grows the worklist.
1032 for (unsigned Idx = 0; Idx != BBWorklist.size(); ++Idx) {
1033 // Bail out the moment we cross the threshold. This means we'll under-count
1034 // the cost, but only when undercounting doesn't matter.
Bob Wilsona5b0dc82012-11-19 07:04:35 +00001035 if (Cost > (Threshold + VectorBonus))
Chandler Carruth0539c072012-03-31 12:42:41 +00001036 break;
1037
1038 BasicBlock *BB = BBWorklist[Idx];
1039 if (BB->empty())
Chandler Carruth4d1d34f2012-03-14 23:19:53 +00001040 continue;
Dan Gohman4552e3c2009-10-13 18:30:07 +00001041
Chandler Carruth0539c072012-03-31 12:42:41 +00001042 // Handle the terminator cost here where we can track returns and other
1043 // function-wide constructs.
1044 TerminatorInst *TI = BB->getTerminator();
Kenneth Uildriksb8d7efe2010-10-09 22:06:36 +00001045
Chandler Carruth0539c072012-03-31 12:42:41 +00001046 // We never want to inline functions that contain an indirectbr. This is
1047 // incorrect because all the blockaddress's (in static global initializers
Nadav Rotem4eb3d4b2012-09-19 08:08:04 +00001048 // for example) would be referring to the original function, and this
1049 // indirect jump would jump from the inlined copy of the function into the
1050 // original function which is extremely undefined behavior.
Chandler Carruth0539c072012-03-31 12:42:41 +00001051 // FIXME: This logic isn't really right; we can safely inline functions
1052 // with indirectbr's as long as no other function or global references the
1053 // blockaddress of a block within the current function. And as a QOI issue,
1054 // if someone is using a blockaddress without an indirectbr, and that
1055 // reference somehow ends up in another function or global, we probably
1056 // don't want to inline this function.
1057 if (isa<IndirectBrInst>(TI))
1058 return false;
Andrew Trickcaa500b2011-10-01 01:27:56 +00001059
Chandler Carruth0539c072012-03-31 12:42:41 +00001060 if (!HasReturn && isa<ReturnInst>(TI))
1061 HasReturn = true;
1062 else
1063 Cost += InlineConstants::InstrCost;
Andrew Trickcaa500b2011-10-01 01:27:56 +00001064
Chandler Carruth0539c072012-03-31 12:42:41 +00001065 // Analyze the cost of this block. If we blow through the threshold, this
1066 // returns false, and we can bail on out.
1067 if (!analyzeBlock(BB)) {
Nadav Rotem4eb3d4b2012-09-19 08:08:04 +00001068 if (IsRecursiveCall || ExposesReturnsTwice || HasDynamicAlloca)
Chandler Carruth0539c072012-03-31 12:42:41 +00001069 return false;
Nadav Rotem4eb3d4b2012-09-19 08:08:04 +00001070
1071 // If the caller is a recursive function then we don't want to inline
1072 // functions which allocate a lot of stack space because it would increase
1073 // the caller stack usage dramatically.
1074 if (IsCallerRecursive &&
1075 AllocatedSize > InlineConstants::TotalAllocaSizeRecursiveCaller)
1076 return false;
1077
Chandler Carruth0539c072012-03-31 12:42:41 +00001078 break;
Eric Christopher46308e62011-02-01 01:16:32 +00001079 }
Eric Christopher46308e62011-02-01 01:16:32 +00001080
Chandler Carruth0539c072012-03-31 12:42:41 +00001081 // Add in the live successors by first checking whether we have terminator
1082 // that may be simplified based on the values simplified by this call.
1083 if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
1084 if (BI->isConditional()) {
1085 Value *Cond = BI->getCondition();
1086 if (ConstantInt *SimpleCond
1087 = dyn_cast_or_null<ConstantInt>(SimplifiedValues.lookup(Cond))) {
1088 BBWorklist.insert(BI->getSuccessor(SimpleCond->isZero() ? 1 : 0));
1089 continue;
Eric Christopher46308e62011-02-01 01:16:32 +00001090 }
Chandler Carruth0539c072012-03-31 12:42:41 +00001091 }
1092 } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
1093 Value *Cond = SI->getCondition();
1094 if (ConstantInt *SimpleCond
1095 = dyn_cast_or_null<ConstantInt>(SimplifiedValues.lookup(Cond))) {
1096 BBWorklist.insert(SI->findCaseValue(SimpleCond).getCaseSuccessor());
1097 continue;
1098 }
1099 }
Eric Christopher46308e62011-02-01 01:16:32 +00001100
Chandler Carruth0539c072012-03-31 12:42:41 +00001101 // If we're unable to select a particular successor, just count all of
1102 // them.
Nadav Rotem4eb3d4b2012-09-19 08:08:04 +00001103 for (unsigned TIdx = 0, TSize = TI->getNumSuccessors(); TIdx != TSize;
1104 ++TIdx)
Chandler Carruth0539c072012-03-31 12:42:41 +00001105 BBWorklist.insert(TI->getSuccessor(TIdx));
1106
1107 // If we had any successors at this point, than post-inlining is likely to
1108 // have them as well. Note that we assume any basic blocks which existed
1109 // due to branches or switches which folded above will also fold after
1110 // inlining.
1111 if (SingleBB && TI->getNumSuccessors() > 1) {
1112 // Take off the bonus we applied to the threshold.
1113 Threshold -= SingleBBBonus;
1114 SingleBB = false;
Eric Christopher46308e62011-02-01 01:16:32 +00001115 }
1116 }
Andrew Trickcaa500b2011-10-01 01:27:56 +00001117
James Molloy4f6fb952012-12-20 16:04:27 +00001118 // If this is a noduplicate call, we can still inline as long as
1119 // inlining this would cause the removal of the caller (so the instruction
1120 // is not actually duplicated, just moved).
1121 if (!OnlyOneCallAndLocalLinkage && ContainsNoDuplicateCall)
1122 return false;
1123
Chandler Carruth0539c072012-03-31 12:42:41 +00001124 Threshold += VectorBonus;
1125
Bob Wilsona5b0dc82012-11-19 07:04:35 +00001126 return Cost < Threshold;
Eric Christopher2dfbd7e2011-02-05 00:49:15 +00001127}
1128
Manman Ren49d684e2012-09-12 05:06:18 +00001129#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Chandler Carruth0539c072012-03-31 12:42:41 +00001130/// \brief Dump stats about this call's analysis.
1131void CallAnalyzer::dump() {
1132#define DEBUG_PRINT_STAT(x) llvm::dbgs() << " " #x ": " << x << "\n"
1133 DEBUG_PRINT_STAT(NumConstantArgs);
1134 DEBUG_PRINT_STAT(NumConstantOffsetPtrArgs);
1135 DEBUG_PRINT_STAT(NumAllocaArgs);
1136 DEBUG_PRINT_STAT(NumConstantPtrCmps);
1137 DEBUG_PRINT_STAT(NumConstantPtrDiffs);
1138 DEBUG_PRINT_STAT(NumInstructionsSimplified);
1139 DEBUG_PRINT_STAT(SROACostSavings);
1140 DEBUG_PRINT_STAT(SROACostSavingsLost);
James Molloy4f6fb952012-12-20 16:04:27 +00001141 DEBUG_PRINT_STAT(ContainsNoDuplicateCall);
Chandler Carruth0539c072012-03-31 12:42:41 +00001142#undef DEBUG_PRINT_STAT
Eric Christopher2dfbd7e2011-02-05 00:49:15 +00001143}
Manman Renc3366cc2012-09-06 19:55:56 +00001144#endif
Eric Christopher2dfbd7e2011-02-05 00:49:15 +00001145
Chandler Carruth4319e292013-01-21 11:39:18 +00001146INITIALIZE_PASS_BEGIN(InlineCostAnalysis, "inline-cost", "Inline Cost Analysis",
1147 true, true)
Chandler Carruth42f3dce2013-01-21 11:55:09 +00001148INITIALIZE_AG_DEPENDENCY(TargetTransformInfo)
Chandler Carruth4319e292013-01-21 11:39:18 +00001149INITIALIZE_PASS_END(InlineCostAnalysis, "inline-cost", "Inline Cost Analysis",
1150 true, true)
1151
1152char InlineCostAnalysis::ID = 0;
1153
1154InlineCostAnalysis::InlineCostAnalysis() : CallGraphSCCPass(ID), TD(0) {}
1155
1156InlineCostAnalysis::~InlineCostAnalysis() {}
1157
1158void InlineCostAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
1159 AU.setPreservesAll();
Chandler Carruth42f3dce2013-01-21 11:55:09 +00001160 AU.addRequired<TargetTransformInfo>();
Chandler Carruth4319e292013-01-21 11:39:18 +00001161 CallGraphSCCPass::getAnalysisUsage(AU);
1162}
1163
1164bool InlineCostAnalysis::runOnSCC(CallGraphSCC &SCC) {
1165 TD = getAnalysisIfAvailable<DataLayout>();
Chandler Carruth42f3dce2013-01-21 11:55:09 +00001166 TTI = &getAnalysis<TargetTransformInfo>();
Chandler Carruth4319e292013-01-21 11:39:18 +00001167 return false;
1168}
1169
1170InlineCost InlineCostAnalysis::getInlineCost(CallSite CS, int Threshold) {
David Chisnallc1c9cda2012-04-06 17:27:41 +00001171 return getInlineCost(CS, CS.getCalledFunction(), Threshold);
1172}
Dan Gohman4552e3c2009-10-13 18:30:07 +00001173
Evgeniy Stepanov2ad36982013-08-08 08:22:39 +00001174/// \brief Test that two functions either have or have not the given attribute
1175/// at the same time.
1176static bool attributeMatches(Function *F1, Function *F2,
1177 Attribute::AttrKind Attr) {
1178 return F1->hasFnAttribute(Attr) == F2->hasFnAttribute(Attr);
1179}
1180
1181/// \brief Test that there are no attribute conflicts between Caller and Callee
1182/// that prevent inlining.
1183static bool functionsHaveCompatibleAttributes(Function *Caller,
1184 Function *Callee) {
1185 return attributeMatches(Caller, Callee, Attribute::SanitizeAddress) &&
1186 attributeMatches(Caller, Callee, Attribute::SanitizeMemory) &&
1187 attributeMatches(Caller, Callee, Attribute::SanitizeThread);
1188}
1189
Chandler Carruth4319e292013-01-21 11:39:18 +00001190InlineCost InlineCostAnalysis::getInlineCost(CallSite CS, Function *Callee,
David Chisnallc1c9cda2012-04-06 17:27:41 +00001191 int Threshold) {
Bob Wilsona5b0dc82012-11-19 07:04:35 +00001192 // Cannot inline indirect calls.
1193 if (!Callee)
1194 return llvm::InlineCost::getNever();
1195
1196 // Calls to functions with always-inline attributes should be inlined
1197 // whenever possible.
Evgeniy Stepanov2ad36982013-08-08 08:22:39 +00001198 if (Callee->hasFnAttribute(Attribute::AlwaysInline)) {
Bob Wilsona5b0dc82012-11-19 07:04:35 +00001199 if (isInlineViable(*Callee))
1200 return llvm::InlineCost::getAlways();
1201 return llvm::InlineCost::getNever();
1202 }
1203
Evgeniy Stepanov2ad36982013-08-08 08:22:39 +00001204 // Never inline functions with conflicting attributes (unless callee has
1205 // always-inline attribute).
1206 if (!functionsHaveCompatibleAttributes(CS.getCaller(), Callee))
1207 return llvm::InlineCost::getNever();
1208
Dan Gohman4552e3c2009-10-13 18:30:07 +00001209 // Don't inline functions which can be redefined at link-time to mean
Eric Christopherb1a382d2010-03-25 04:49:10 +00001210 // something else. Don't inline functions marked noinline or call sites
1211 // marked noinline.
Bob Wilsona5b0dc82012-11-19 07:04:35 +00001212 if (Callee->mayBeOverridden() ||
Evgeniy Stepanov2ad36982013-08-08 08:22:39 +00001213 Callee->hasFnAttribute(Attribute::NoInline) || CS.isNoInline())
Dan Gohman4552e3c2009-10-13 18:30:07 +00001214 return llvm::InlineCost::getNever();
1215
Nadav Rotem4eb3d4b2012-09-19 08:08:04 +00001216 DEBUG(llvm::dbgs() << " Analyzing call of " << Callee->getName()
1217 << "...\n");
Andrew Trickcaa500b2011-10-01 01:27:56 +00001218
Chandler Carruth42f3dce2013-01-21 11:55:09 +00001219 CallAnalyzer CA(TD, *TTI, *Callee, Threshold);
Chandler Carruth0539c072012-03-31 12:42:41 +00001220 bool ShouldInline = CA.analyzeCall(CS);
Dan Gohman4552e3c2009-10-13 18:30:07 +00001221
Chandler Carruth0539c072012-03-31 12:42:41 +00001222 DEBUG(CA.dump());
1223
1224 // Check if there was a reason to force inlining or no inlining.
1225 if (!ShouldInline && CA.getCost() < CA.getThreshold())
Dan Gohman4552e3c2009-10-13 18:30:07 +00001226 return InlineCost::getNever();
Bob Wilsona5b0dc82012-11-19 07:04:35 +00001227 if (ShouldInline && CA.getCost() >= CA.getThreshold())
Dan Gohman4552e3c2009-10-13 18:30:07 +00001228 return InlineCost::getAlways();
Andrew Trickcaa500b2011-10-01 01:27:56 +00001229
Chandler Carruth0539c072012-03-31 12:42:41 +00001230 return llvm::InlineCost::get(CA.getCost(), CA.getThreshold());
Dan Gohman4552e3c2009-10-13 18:30:07 +00001231}
Bob Wilsona5b0dc82012-11-19 07:04:35 +00001232
Chandler Carruth4319e292013-01-21 11:39:18 +00001233bool InlineCostAnalysis::isInlineViable(Function &F) {
1234 bool ReturnsTwice =
1235 F.getAttributes().hasAttribute(AttributeSet::FunctionIndex,
1236 Attribute::ReturnsTwice);
Bob Wilsona5b0dc82012-11-19 07:04:35 +00001237 for (Function::iterator BI = F.begin(), BE = F.end(); BI != BE; ++BI) {
1238 // Disallow inlining of functions which contain an indirect branch.
1239 if (isa<IndirectBrInst>(BI->getTerminator()))
1240 return false;
1241
1242 for (BasicBlock::iterator II = BI->begin(), IE = BI->end(); II != IE;
1243 ++II) {
1244 CallSite CS(II);
1245 if (!CS)
1246 continue;
1247
1248 // Disallow recursive calls.
1249 if (&F == CS.getCalledFunction())
1250 return false;
1251
1252 // Disallow calls which expose returns-twice to a function not previously
1253 // attributed as such.
1254 if (!ReturnsTwice && CS.isCall() &&
1255 cast<CallInst>(CS.getInstruction())->canReturnTwice())
1256 return false;
1257 }
1258 }
1259
1260 return true;
1261}