1 //===- llvm/Analysis/TargetTransformInfo.cpp ------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #define DEBUG_TYPE "tti"
11 #include "llvm/Analysis/TargetTransformInfo.h"
12 #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"
17 #include "llvm/Support/ErrorHandling.h"
21 // Setup the analysis group to manage the TargetTransformInfo passes.
22 INITIALIZE_ANALYSIS_GROUP(TargetTransformInfo, "Target Information", NoTTI)
23 char TargetTransformInfo::ID = 0;
25 TargetTransformInfo::~TargetTransformInfo() {
28 void TargetTransformInfo::pushTTIStack(Pass *P) {
30 PrevTTI = &P->getAnalysis<TargetTransformInfo>();
32 // Walk up the chain and update the top TTI pointer.
33 for (TargetTransformInfo *PTTI = PrevTTI; PTTI; PTTI = PTTI->PrevTTI)
37 void TargetTransformInfo::popTTIStack() {
40 // Walk up the chain and update the top TTI pointer.
41 for (TargetTransformInfo *PTTI = PrevTTI; PTTI; PTTI = PTTI->PrevTTI)
42 PTTI->TopTTI = PrevTTI;
47 void TargetTransformInfo::getAnalysisUsage(AnalysisUsage &AU) const {
48 AU.addRequired<TargetTransformInfo>();
51 unsigned TargetTransformInfo::getOperationCost(unsigned Opcode, Type *Ty,
53 return PrevTTI->getOperationCost(Opcode, Ty, OpTy);
56 unsigned TargetTransformInfo::getGEPCost(
57 const Value *Ptr, ArrayRef<const Value *> Operands) const {
58 return PrevTTI->getGEPCost(Ptr, Operands);
61 unsigned TargetTransformInfo::getUserCost(const User *U) const {
62 return PrevTTI->getUserCost(U);
65 bool TargetTransformInfo::isLegalAddImmediate(int64_t Imm) const {
66 return PrevTTI->isLegalAddImmediate(Imm);
69 bool TargetTransformInfo::isLegalICmpImmediate(int64_t Imm) const {
70 return PrevTTI->isLegalICmpImmediate(Imm);
73 bool TargetTransformInfo::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
76 int64_t Scale) const {
77 return PrevTTI->isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
81 bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const {
82 return PrevTTI->isTruncateFree(Ty1, Ty2);
85 bool TargetTransformInfo::isTypeLegal(Type *Ty) const {
86 return PrevTTI->isTypeLegal(Ty);
89 unsigned TargetTransformInfo::getJumpBufAlignment() const {
90 return PrevTTI->getJumpBufAlignment();
93 unsigned TargetTransformInfo::getJumpBufSize() const {
94 return PrevTTI->getJumpBufSize();
97 bool TargetTransformInfo::shouldBuildLookupTables() const {
98 return PrevTTI->shouldBuildLookupTables();
101 TargetTransformInfo::PopcntSupportKind
102 TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const {
103 return PrevTTI->getPopcntSupport(IntTyWidthInBit);
106 unsigned TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
107 return PrevTTI->getIntImmCost(Imm, Ty);
110 unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const {
111 return PrevTTI->getNumberOfRegisters(Vector);
114 unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const {
115 return PrevTTI->getRegisterBitWidth(Vector);
118 unsigned TargetTransformInfo::getMaximumUnrollFactor() const {
119 return PrevTTI->getMaximumUnrollFactor();
122 unsigned TargetTransformInfo::getArithmeticInstrCost(unsigned Opcode,
124 return PrevTTI->getArithmeticInstrCost(Opcode, Ty);
127 unsigned TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Tp,
128 int Index, Type *SubTp) const {
129 return PrevTTI->getShuffleCost(Kind, Tp, Index, SubTp);
132 unsigned TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
134 return PrevTTI->getCastInstrCost(Opcode, Dst, Src);
137 unsigned TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
138 return PrevTTI->getCFInstrCost(Opcode);
141 unsigned TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
142 Type *CondTy) const {
143 return PrevTTI->getCmpSelInstrCost(Opcode, ValTy, CondTy);
146 unsigned TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
147 unsigned Index) const {
148 return PrevTTI->getVectorInstrCost(Opcode, Val, Index);
151 unsigned TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
153 unsigned AddressSpace) const {
154 return PrevTTI->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
159 TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID,
161 ArrayRef<Type *> Tys) const {
162 return PrevTTI->getIntrinsicInstrCost(ID, RetTy, Tys);
165 unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
166 return PrevTTI->getNumberOfParts(Tp);
172 struct NoTTI : ImmutablePass, TargetTransformInfo {
173 const DataLayout *DL;
175 NoTTI() : ImmutablePass(ID), DL(0) {
176 initializeNoTTIPass(*PassRegistry::getPassRegistry());
179 virtual void initializePass() {
180 // Note that this subclass is special, and must *not* call initializeTTI as
181 // it does not chain.
183 DL = getAnalysisIfAvailable<DataLayout>();
186 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
187 // Note that this subclass is special, and must *not* call
188 // TTI::getAnalysisUsage as it breaks the recursion.
191 /// Pass identification.
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;
201 unsigned getOperationCost(unsigned Opcode, Type *Ty, Type *OpTy) const {
204 // By default, just classify everything as 'basic'.
207 case Instruction::GetElementPtr:
208 llvm_unreachable("Use getGEPCost for GEP operations!");
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.
216 // Otherwise, the default basic cost is used.
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())
226 // Otherwise it's not a no-op.
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())
236 // Otherwise it's not a no-op.
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)))
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]))
260 unsigned getUserCost(const User *U) const {
262 return TCC_Free; // Model all PHI nodes as free.
264 if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U))
265 // In the basic model we just assume that all-constant GEPs will be
266 // folded into their uses via addressing modes.
267 return GEP->hasAllConstantIndices() ? TCC_Free : TCC_Basic;
269 // If we have a call of an intrinsic we can provide more detailed analysis
270 // by inspecting the particular intrinsic called.
271 // FIXME: Hoist this out into a getIntrinsicCost routine.
272 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U)) {
273 switch (II->getIntrinsicID()) {
276 case Intrinsic::dbg_declare:
277 case Intrinsic::dbg_value:
278 case Intrinsic::invariant_start:
279 case Intrinsic::invariant_end:
280 case Intrinsic::lifetime_start:
281 case Intrinsic::lifetime_end:
282 case Intrinsic::objectsize:
283 case Intrinsic::ptr_annotation:
284 case Intrinsic::var_annotation:
285 // These intrinsics don't count as size.
290 if (const CastInst *CI = dyn_cast<CastInst>(U)) {
291 // Result of a cmp instruction is often extended (to be used by other
292 // cmp instructions, logical or return instructions). These are usually
293 // nop on most sane targets.
294 if (isa<CmpInst>(CI->getOperand(0)))
298 // Otherwise delegate to the fully generic implementations.
299 return getOperationCost(Operator::getOpcode(U), U->getType(),
300 U->getNumOperands() == 1 ?
301 U->getOperand(0)->getType() : 0);
304 bool isLegalAddImmediate(int64_t Imm) const {
308 bool isLegalICmpImmediate(int64_t Imm) const {
312 bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset,
313 bool HasBaseReg, int64_t Scale) const {
314 // Guess that reg+reg addressing is allowed. This heuristic is taken from
315 // the implementation of LSR.
316 return !BaseGV && BaseOffset == 0 && Scale <= 1;
319 bool isTruncateFree(Type *Ty1, Type *Ty2) const {
323 bool isTypeLegal(Type *Ty) const {
327 unsigned getJumpBufAlignment() const {
331 unsigned getJumpBufSize() const {
335 bool shouldBuildLookupTables() const {
339 PopcntSupportKind getPopcntSupport(unsigned IntTyWidthInBit) const {
343 unsigned getIntImmCost(const APInt &Imm, Type *Ty) const {
347 unsigned getNumberOfRegisters(bool Vector) const {
351 unsigned getRegisterBitWidth(bool Vector) const {
355 unsigned getMaximumUnrollFactor() const {
359 unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty) const {
363 unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
364 int Index = 0, Type *SubTp = 0) const {
368 unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
373 unsigned getCFInstrCost(unsigned Opcode) const {
377 unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
378 Type *CondTy = 0) const {
382 unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
383 unsigned Index = -1) const {
387 unsigned getMemoryOpCost(unsigned Opcode, Type *Src,
389 unsigned AddressSpace) const {
393 unsigned getIntrinsicInstrCost(Intrinsic::ID ID,
395 ArrayRef<Type*> Tys) const {
399 unsigned getNumberOfParts(Type *Tp) const {
404 } // end anonymous namespace
406 INITIALIZE_AG_PASS(NoTTI, TargetTransformInfo, "notti",
407 "No target information", true, true, true)
410 ImmutablePass *llvm::createNoTargetTransformInfoPass() {