blob: 16ee6eb35c684e658cb5df873b671c2af802b035 [file] [log] [blame]
Chandler Carruthbe049292013-01-07 03:08:10 +00001//===- llvm/Analysis/TargetTransformInfo.cpp ------------------------------===//
Nadav Rotemcbd9a192012-10-18 23:22:48 +00002//
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
Chandler Carruthaeef83c2013-01-07 01:37:14 +000010#define DEBUG_TYPE "tti"
Chandler Carruthbe049292013-01-07 03:08:10 +000011#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruth1e05bd92013-01-21 01:27:39 +000012#include "llvm/IR/DataLayout.h"
13#include "llvm/IR/Operator.h"
14#include "llvm/IR/Instruction.h"
15#include "llvm/IR/IntrinsicInst.h"
16#include "llvm/IR/Instructions.h"
Nadav Rotemcbd9a192012-10-18 23:22:48 +000017#include "llvm/Support/ErrorHandling.h"
18
19using namespace llvm;
20
Chandler Carruth7bdf6b02013-01-05 11:43:11 +000021// Setup the analysis group to manage the TargetTransformInfo passes.
22INITIALIZE_ANALYSIS_GROUP(TargetTransformInfo, "Target Information", NoTTI)
Nadav Rotemcbd9a192012-10-18 23:22:48 +000023char TargetTransformInfo::ID = 0;
24
Chandler Carruth7bdf6b02013-01-05 11:43:11 +000025TargetTransformInfo::~TargetTransformInfo() {
26}
27
Chandler Carruthaeef83c2013-01-07 01:37:14 +000028void TargetTransformInfo::pushTTIStack(Pass *P) {
29 TopTTI = this;
30 PrevTTI = &P->getAnalysis<TargetTransformInfo>();
31
32 // Walk up the chain and update the top TTI pointer.
33 for (TargetTransformInfo *PTTI = PrevTTI; PTTI; PTTI = PTTI->PrevTTI)
34 PTTI->TopTTI = this;
35}
36
37void TargetTransformInfo::popTTIStack() {
38 TopTTI = 0;
39
40 // Walk up the chain and update the top TTI pointer.
41 for (TargetTransformInfo *PTTI = PrevTTI; PTTI; PTTI = PTTI->PrevTTI)
42 PTTI->TopTTI = PrevTTI;
43
44 PrevTTI = 0;
45}
46
Chandler Carruth7bdf6b02013-01-05 11:43:11 +000047void TargetTransformInfo::getAnalysisUsage(AnalysisUsage &AU) const {
48 AU.addRequired<TargetTransformInfo>();
49}
50
Chandler Carruth1e05bd92013-01-21 01:27:39 +000051unsigned TargetTransformInfo::getOperationCost(unsigned Opcode, Type *Ty,
52 Type *OpTy) const {
53 return PrevTTI->getOperationCost(Opcode, Ty, OpTy);
54}
55
56unsigned TargetTransformInfo::getGEPCost(
57 const Value *Ptr, ArrayRef<const Value *> Operands) const {
58 return PrevTTI->getGEPCost(Ptr, Operands);
59}
60
61unsigned TargetTransformInfo::getUserCost(const User *U) const {
62 return PrevTTI->getUserCost(U);
63}
64
Chandler Carruth7bdf6b02013-01-05 11:43:11 +000065bool TargetTransformInfo::isLegalAddImmediate(int64_t Imm) const {
66 return PrevTTI->isLegalAddImmediate(Imm);
67}
68
69bool TargetTransformInfo::isLegalICmpImmediate(int64_t Imm) const {
70 return PrevTTI->isLegalICmpImmediate(Imm);
71}
72
73bool TargetTransformInfo::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
74 int64_t BaseOffset,
75 bool HasBaseReg,
76 int64_t Scale) const {
77 return PrevTTI->isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
78 Scale);
79}
80
81bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const {
82 return PrevTTI->isTruncateFree(Ty1, Ty2);
83}
84
85bool TargetTransformInfo::isTypeLegal(Type *Ty) const {
86 return PrevTTI->isTypeLegal(Ty);
87}
88
89unsigned TargetTransformInfo::getJumpBufAlignment() const {
90 return PrevTTI->getJumpBufAlignment();
91}
92
93unsigned TargetTransformInfo::getJumpBufSize() const {
94 return PrevTTI->getJumpBufSize();
95}
96
97bool TargetTransformInfo::shouldBuildLookupTables() const {
98 return PrevTTI->shouldBuildLookupTables();
99}
100
Chandler Carruthd1b8ef92013-01-07 03:16:03 +0000101TargetTransformInfo::PopcntSupportKind
102TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const {
103 return PrevTTI->getPopcntSupport(IntTyWidthInBit);
Chandler Carruth7bdf6b02013-01-05 11:43:11 +0000104}
105
106unsigned TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
107 return PrevTTI->getIntImmCost(Imm, Ty);
108}
109
110unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const {
111 return PrevTTI->getNumberOfRegisters(Vector);
112}
113
Nadav Rotem14925e62013-01-09 22:29:00 +0000114unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const {
115 return PrevTTI->getRegisterBitWidth(Vector);
116}
117
Nadav Rotem83be7b02013-01-09 01:15:42 +0000118unsigned TargetTransformInfo::getMaximumUnrollFactor() const {
119 return PrevTTI->getMaximumUnrollFactor();
120}
121
Chandler Carruth7bdf6b02013-01-05 11:43:11 +0000122unsigned TargetTransformInfo::getArithmeticInstrCost(unsigned Opcode,
123 Type *Ty) const {
124 return PrevTTI->getArithmeticInstrCost(Opcode, Ty);
125}
126
127unsigned TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Tp,
128 int Index, Type *SubTp) const {
129 return PrevTTI->getShuffleCost(Kind, Tp, Index, SubTp);
130}
131
132unsigned TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
133 Type *Src) const {
134 return PrevTTI->getCastInstrCost(Opcode, Dst, Src);
135}
136
137unsigned TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
138 return PrevTTI->getCFInstrCost(Opcode);
139}
140
141unsigned TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
142 Type *CondTy) const {
143 return PrevTTI->getCmpSelInstrCost(Opcode, ValTy, CondTy);
144}
145
146unsigned TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
147 unsigned Index) const {
148 return PrevTTI->getVectorInstrCost(Opcode, Val, Index);
149}
150
151unsigned TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
152 unsigned Alignment,
153 unsigned AddressSpace) const {
154 return PrevTTI->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
155 ;
156}
157
158unsigned
159TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID,
160 Type *RetTy,
161 ArrayRef<Type *> Tys) const {
162 return PrevTTI->getIntrinsicInstrCost(ID, RetTy, Tys);
163}
164
165unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
166 return PrevTTI->getNumberOfParts(Tp);
167}
168
169
170namespace {
171
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000172struct NoTTI : ImmutablePass, TargetTransformInfo {
Chandler Carruth1e05bd92013-01-21 01:27:39 +0000173 const DataLayout *DL;
174
175 NoTTI() : ImmutablePass(ID), DL(0) {
Chandler Carruth7bdf6b02013-01-05 11:43:11 +0000176 initializeNoTTIPass(*PassRegistry::getPassRegistry());
177 }
178
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000179 virtual void initializePass() {
180 // Note that this subclass is special, and must *not* call initializeTTI as
181 // it does not chain.
182 PrevTTI = 0;
Chandler Carruth1e05bd92013-01-21 01:27:39 +0000183 DL = getAnalysisIfAvailable<DataLayout>();
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000184 }
185
186 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chandler Carruth7bdf6b02013-01-05 11:43:11 +0000187 // Note that this subclass is special, and must *not* call
188 // TTI::getAnalysisUsage as it breaks the recursion.
189 }
190
191 /// Pass identification.
192 static char ID;
193
194 /// Provide necessary pointer adjustments for the two base classes.
195 virtual void *getAdjustedAnalysisPointer(const void *ID) {
196 if (ID == &TargetTransformInfo::ID)
197 return (TargetTransformInfo*)this;
198 return this;
199 }
200
Chandler Carruth1e05bd92013-01-21 01:27:39 +0000201 unsigned getOperationCost(unsigned Opcode, Type *Ty, Type *OpTy) const {
202 switch (Opcode) {
203 default:
204 // By default, just classify everything as 'basic'.
205 return TCC_Basic;
206
207 case Instruction::GetElementPtr:
208 llvm_unreachable("Use getGEPCost for GEP operations!");
209
210 case Instruction::BitCast:
211 assert(OpTy && "Cast instructions must provide the operand type");
212 if (Ty == OpTy || (Ty->isPointerTy() && OpTy->isPointerTy()))
213 // Identity and pointer-to-pointer casts are free.
214 return TCC_Free;
215
216 // Otherwise, the default basic cost is used.
217 return TCC_Basic;
218
219 case Instruction::IntToPtr:
220 // An inttoptr cast is free so long as the input is a legal integer type
221 // which doesn't contain values outside the range of a pointer.
222 if (DL && DL->isLegalInteger(OpTy->getScalarSizeInBits()) &&
223 OpTy->getScalarSizeInBits() <= DL->getPointerSizeInBits())
224 return TCC_Free;
225
226 // Otherwise it's not a no-op.
227 return TCC_Basic;
228
229 case Instruction::PtrToInt:
230 // A ptrtoint cast is free so long as the result is large enough to store
231 // the pointer, and a legal integer type.
232 if (DL && DL->isLegalInteger(OpTy->getScalarSizeInBits()) &&
233 OpTy->getScalarSizeInBits() >= DL->getPointerSizeInBits())
234 return TCC_Free;
235
236 // Otherwise it's not a no-op.
237 return TCC_Basic;
238
239 case Instruction::Trunc:
240 // trunc to a native type is free (assuming the target has compare and
241 // shift-right of the same width).
242 if (DL && DL->isLegalInteger(DL->getTypeSizeInBits(Ty)))
243 return TCC_Free;
244
245 return TCC_Basic;
246 }
247 }
248
249 unsigned getGEPCost(const Value *Ptr,
250 ArrayRef<const Value *> Operands) const {
251 // In the basic model, we just assume that all-constant GEPs will be folded
252 // into their uses via addressing modes.
253 for (unsigned Idx = 0, Size = Operands.size(); Idx != Size; ++Idx)
254 if (!isa<Constant>(Operands[Idx]))
255 return TCC_Basic;
256
257 return TCC_Free;
258 }
259
260 unsigned getUserCost(const User *U) const {
261 if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U))
262 // In the basic model we just assume that all-constant GEPs will be
263 // folded into their uses via addressing modes.
264 return GEP->hasAllConstantIndices() ? TCC_Free : TCC_Basic;
265
266 // If we have a call of an intrinsic we can provide more detailed analysis
267 // by inspecting the particular intrinsic called.
268 // FIXME: Hoist this out into a getIntrinsicCost routine.
269 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U)) {
270 switch (II->getIntrinsicID()) {
271 default:
272 return TCC_Basic;
273 case Intrinsic::dbg_declare:
274 case Intrinsic::dbg_value:
275 case Intrinsic::invariant_start:
276 case Intrinsic::invariant_end:
277 case Intrinsic::lifetime_start:
278 case Intrinsic::lifetime_end:
279 case Intrinsic::objectsize:
280 case Intrinsic::ptr_annotation:
281 case Intrinsic::var_annotation:
282 // These intrinsics don't count as size.
283 return TCC_Free;
284 }
285 }
286
287 if (const CastInst *CI = dyn_cast<CastInst>(U)) {
288 // Result of a cmp instruction is often extended (to be used by other
289 // cmp instructions, logical or return instructions). These are usually
290 // nop on most sane targets.
291 if (isa<CmpInst>(CI->getOperand(0)))
292 return TCC_Free;
293 }
294
295 // Otherwise delegate to the fully generic implementations.
296 return getOperationCost(Operator::getOpcode(U), U->getType(),
297 U->getNumOperands() == 1 ?
298 U->getOperand(0)->getType() : 0);
299 }
Chandler Carruth7bdf6b02013-01-05 11:43:11 +0000300
Chandler Carruth7bdf6b02013-01-05 11:43:11 +0000301 bool isLegalAddImmediate(int64_t Imm) const {
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000302 return false;
Chandler Carruth7bdf6b02013-01-05 11:43:11 +0000303 }
304
305 bool isLegalICmpImmediate(int64_t Imm) const {
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000306 return false;
Chandler Carruth7bdf6b02013-01-05 11:43:11 +0000307 }
308
309 bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset,
310 bool HasBaseReg, int64_t Scale) const {
Chandler Carruthe4ba75f2013-01-07 14:41:08 +0000311 // Guess that reg+reg addressing is allowed. This heuristic is taken from
312 // the implementation of LSR.
313 return !BaseGV && BaseOffset == 0 && Scale <= 1;
Chandler Carruth7bdf6b02013-01-05 11:43:11 +0000314 }
315
316 bool isTruncateFree(Type *Ty1, Type *Ty2) const {
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000317 return false;
Chandler Carruth7bdf6b02013-01-05 11:43:11 +0000318 }
319
320 bool isTypeLegal(Type *Ty) const {
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000321 return false;
Chandler Carruth7bdf6b02013-01-05 11:43:11 +0000322 }
323
324 unsigned getJumpBufAlignment() const {
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000325 return 0;
Chandler Carruth7bdf6b02013-01-05 11:43:11 +0000326 }
327
328 unsigned getJumpBufSize() const {
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000329 return 0;
Chandler Carruth7bdf6b02013-01-05 11:43:11 +0000330 }
331
332 bool shouldBuildLookupTables() const {
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000333 return true;
Chandler Carruth7bdf6b02013-01-05 11:43:11 +0000334 }
335
Chandler Carruthd1b8ef92013-01-07 03:16:03 +0000336 PopcntSupportKind getPopcntSupport(unsigned IntTyWidthInBit) const {
337 return PSK_Software;
Chandler Carruth7bdf6b02013-01-05 11:43:11 +0000338 }
339
340 unsigned getIntImmCost(const APInt &Imm, Type *Ty) const {
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000341 return 1;
Chandler Carruth7bdf6b02013-01-05 11:43:11 +0000342 }
343
344 unsigned getNumberOfRegisters(bool Vector) const {
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000345 return 8;
Chandler Carruth7bdf6b02013-01-05 11:43:11 +0000346 }
347
Nadav Rotem14925e62013-01-09 22:29:00 +0000348 unsigned getRegisterBitWidth(bool Vector) const {
349 return 32;
350 }
351
Nadav Rotem83be7b02013-01-09 01:15:42 +0000352 unsigned getMaximumUnrollFactor() const {
353 return 1;
354 }
355
Chandler Carruth7bdf6b02013-01-05 11:43:11 +0000356 unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty) const {
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000357 return 1;
Chandler Carruth7bdf6b02013-01-05 11:43:11 +0000358 }
359
360 unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
361 int Index = 0, Type *SubTp = 0) const {
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000362 return 1;
Chandler Carruth7bdf6b02013-01-05 11:43:11 +0000363 }
364
365 unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
366 Type *Src) const {
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000367 return 1;
Chandler Carruth7bdf6b02013-01-05 11:43:11 +0000368 }
369
370 unsigned getCFInstrCost(unsigned Opcode) const {
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000371 return 1;
Chandler Carruth7bdf6b02013-01-05 11:43:11 +0000372 }
373
374 unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
375 Type *CondTy = 0) const {
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000376 return 1;
Chandler Carruth7bdf6b02013-01-05 11:43:11 +0000377 }
378
379 unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
380 unsigned Index = -1) const {
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000381 return 1;
Chandler Carruth7bdf6b02013-01-05 11:43:11 +0000382 }
383
384 unsigned getMemoryOpCost(unsigned Opcode, Type *Src,
385 unsigned Alignment,
386 unsigned AddressSpace) const {
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000387 return 1;
Chandler Carruth7bdf6b02013-01-05 11:43:11 +0000388 }
389
390 unsigned getIntrinsicInstrCost(Intrinsic::ID ID,
391 Type *RetTy,
392 ArrayRef<Type*> Tys) const {
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000393 return 1;
Chandler Carruth7bdf6b02013-01-05 11:43:11 +0000394 }
395
396 unsigned getNumberOfParts(Type *Tp) const {
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000397 return 0;
Chandler Carruth7bdf6b02013-01-05 11:43:11 +0000398 }
399};
400
401} // end anonymous namespace
402
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000403INITIALIZE_AG_PASS(NoTTI, TargetTransformInfo, "notti",
Chandler Carruth7bdf6b02013-01-05 11:43:11 +0000404 "No target information", true, true, true)
405char NoTTI::ID = 0;
406
Chandler Carruthaeef83c2013-01-07 01:37:14 +0000407ImmutablePass *llvm::createNoTargetTransformInfoPass() {
408 return new NoTTI();
Chandler Carruth7bdf6b02013-01-05 11:43:11 +0000409}