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 #include "llvm/Analysis/TargetTransformInfo.h"
11 #include "llvm/IR/CallSite.h"
12 #include "llvm/IR/DataLayout.h"
13 #include "llvm/IR/Instruction.h"
14 #include "llvm/IR/Instructions.h"
15 #include "llvm/IR/IntrinsicInst.h"
16 #include "llvm/IR/Operator.h"
17 #include "llvm/Support/ErrorHandling.h"
21 #define DEBUG_TYPE "tti"
23 // Setup the analysis group to manage the TargetTransformInfo passes.
24 INITIALIZE_ANALYSIS_GROUP(TargetTransformInfo, "Target Information", NoTTI)
25 char TargetTransformInfo::ID = 0;
27 TargetTransformInfo::~TargetTransformInfo() {
30 void TargetTransformInfo::pushTTIStack(Pass *P) {
32 PrevTTI = &P->getAnalysis<TargetTransformInfo>();
34 // Walk up the chain and update the top TTI pointer.
35 for (TargetTransformInfo *PTTI = PrevTTI; PTTI; PTTI = PTTI->PrevTTI)
39 void TargetTransformInfo::getAnalysisUsage(AnalysisUsage &AU) const {
40 AU.addRequired<TargetTransformInfo>();
43 unsigned TargetTransformInfo::getOperationCost(unsigned Opcode, Type *Ty,
45 return PrevTTI->getOperationCost(Opcode, Ty, OpTy);
48 unsigned TargetTransformInfo::getGEPCost(
49 const Value *Ptr, ArrayRef<const Value *> Operands) const {
50 return PrevTTI->getGEPCost(Ptr, Operands);
53 unsigned TargetTransformInfo::getCallCost(FunctionType *FTy,
55 return PrevTTI->getCallCost(FTy, NumArgs);
58 unsigned TargetTransformInfo::getCallCost(const Function *F,
60 return PrevTTI->getCallCost(F, NumArgs);
63 unsigned TargetTransformInfo::getCallCost(
64 const Function *F, ArrayRef<const Value *> Arguments) const {
65 return PrevTTI->getCallCost(F, Arguments);
68 unsigned TargetTransformInfo::getIntrinsicCost(
69 Intrinsic::ID IID, Type *RetTy, ArrayRef<Type *> ParamTys) const {
70 return PrevTTI->getIntrinsicCost(IID, RetTy, ParamTys);
73 unsigned TargetTransformInfo::getIntrinsicCost(
74 Intrinsic::ID IID, Type *RetTy, ArrayRef<const Value *> Arguments) const {
75 return PrevTTI->getIntrinsicCost(IID, RetTy, Arguments);
78 unsigned TargetTransformInfo::getUserCost(const User *U) const {
79 return PrevTTI->getUserCost(U);
82 bool TargetTransformInfo::hasBranchDivergence() const {
83 return PrevTTI->hasBranchDivergence();
86 bool TargetTransformInfo::isLoweredToCall(const Function *F) const {
87 return PrevTTI->isLoweredToCall(F);
90 void TargetTransformInfo::getUnrollingPreferences(Loop *L,
91 UnrollingPreferences &UP) const {
92 PrevTTI->getUnrollingPreferences(L, UP);
95 bool TargetTransformInfo::isLegalAddImmediate(int64_t Imm) const {
96 return PrevTTI->isLegalAddImmediate(Imm);
99 bool TargetTransformInfo::isLegalICmpImmediate(int64_t Imm) const {
100 return PrevTTI->isLegalICmpImmediate(Imm);
103 bool TargetTransformInfo::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
106 int64_t Scale) const {
107 return PrevTTI->isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
111 int TargetTransformInfo::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
114 int64_t Scale) const {
115 return PrevTTI->getScalingFactorCost(Ty, BaseGV, BaseOffset, HasBaseReg,
119 bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const {
120 return PrevTTI->isTruncateFree(Ty1, Ty2);
123 bool TargetTransformInfo::isTypeLegal(Type *Ty) const {
124 return PrevTTI->isTypeLegal(Ty);
127 unsigned TargetTransformInfo::getJumpBufAlignment() const {
128 return PrevTTI->getJumpBufAlignment();
131 unsigned TargetTransformInfo::getJumpBufSize() const {
132 return PrevTTI->getJumpBufSize();
135 bool TargetTransformInfo::shouldBuildLookupTables() const {
136 return PrevTTI->shouldBuildLookupTables();
139 TargetTransformInfo::PopcntSupportKind
140 TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const {
141 return PrevTTI->getPopcntSupport(IntTyWidthInBit);
144 bool TargetTransformInfo::haveFastSqrt(Type *Ty) const {
145 return PrevTTI->haveFastSqrt(Ty);
148 unsigned TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
149 return PrevTTI->getIntImmCost(Imm, Ty);
152 unsigned TargetTransformInfo::getIntImmCost(unsigned Opc, unsigned Idx,
153 const APInt &Imm, Type *Ty) const {
154 return PrevTTI->getIntImmCost(Opc, Idx, Imm, Ty);
157 unsigned TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
158 const APInt &Imm, Type *Ty) const {
159 return PrevTTI->getIntImmCost(IID, Idx, Imm, Ty);
162 unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const {
163 return PrevTTI->getNumberOfRegisters(Vector);
166 unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const {
167 return PrevTTI->getRegisterBitWidth(Vector);
170 unsigned TargetTransformInfo::getMaximumUnrollFactor() const {
171 return PrevTTI->getMaximumUnrollFactor();
174 unsigned TargetTransformInfo::getArithmeticInstrCost(unsigned Opcode,
176 OperandValueKind Op1Info,
177 OperandValueKind Op2Info) const {
178 return PrevTTI->getArithmeticInstrCost(Opcode, Ty, Op1Info, Op2Info);
181 unsigned TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Tp,
182 int Index, Type *SubTp) const {
183 return PrevTTI->getShuffleCost(Kind, Tp, Index, SubTp);
186 unsigned TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
188 return PrevTTI->getCastInstrCost(Opcode, Dst, Src);
191 unsigned TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
192 return PrevTTI->getCFInstrCost(Opcode);
195 unsigned TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
196 Type *CondTy) const {
197 return PrevTTI->getCmpSelInstrCost(Opcode, ValTy, CondTy);
200 unsigned TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
201 unsigned Index) const {
202 return PrevTTI->getVectorInstrCost(Opcode, Val, Index);
205 unsigned TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
207 unsigned AddressSpace) const {
208 return PrevTTI->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
213 TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID,
215 ArrayRef<Type *> Tys) const {
216 return PrevTTI->getIntrinsicInstrCost(ID, RetTy, Tys);
219 unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
220 return PrevTTI->getNumberOfParts(Tp);
223 unsigned TargetTransformInfo::getAddressComputationCost(Type *Tp,
224 bool IsComplex) const {
225 return PrevTTI->getAddressComputationCost(Tp, IsComplex);
228 unsigned TargetTransformInfo::getReductionCost(unsigned Opcode, Type *Ty,
229 bool IsPairwise) const {
230 return PrevTTI->getReductionCost(Opcode, Ty, IsPairwise);
233 unsigned TargetTransformInfo::getCostOfKeepingLiveOverCall(ArrayRef<Type*> Tys)
235 return PrevTTI->getCostOfKeepingLiveOverCall(Tys);
240 struct NoTTI final : ImmutablePass, TargetTransformInfo {
241 const DataLayout *DL;
243 NoTTI() : ImmutablePass(ID), DL(nullptr) {
244 initializeNoTTIPass(*PassRegistry::getPassRegistry());
247 virtual void initializePass() override {
248 // Note that this subclass is special, and must *not* call initializeTTI as
249 // it does not chain.
252 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
253 DL = DLP ? &DLP->getDataLayout() : nullptr;
256 virtual void getAnalysisUsage(AnalysisUsage &AU) const override {
257 // Note that this subclass is special, and must *not* call
258 // TTI::getAnalysisUsage as it breaks the recursion.
261 /// Pass identification.
264 /// Provide necessary pointer adjustments for the two base classes.
265 virtual void *getAdjustedAnalysisPointer(const void *ID) override {
266 if (ID == &TargetTransformInfo::ID)
267 return (TargetTransformInfo*)this;
271 unsigned getOperationCost(unsigned Opcode, Type *Ty,
272 Type *OpTy) const override {
275 // By default, just classify everything as 'basic'.
278 case Instruction::GetElementPtr:
279 llvm_unreachable("Use getGEPCost for GEP operations!");
281 case Instruction::BitCast:
282 assert(OpTy && "Cast instructions must provide the operand type");
283 if (Ty == OpTy || (Ty->isPointerTy() && OpTy->isPointerTy()))
284 // Identity and pointer-to-pointer casts are free.
287 // Otherwise, the default basic cost is used.
290 case Instruction::IntToPtr: {
294 // An inttoptr cast is free so long as the input is a legal integer type
295 // which doesn't contain values outside the range of a pointer.
296 unsigned OpSize = OpTy->getScalarSizeInBits();
297 if (DL->isLegalInteger(OpSize) &&
298 OpSize <= DL->getPointerTypeSizeInBits(Ty))
301 // Otherwise it's not a no-op.
304 case Instruction::PtrToInt: {
308 // A ptrtoint cast is free so long as the result is large enough to store
309 // the pointer, and a legal integer type.
310 unsigned DestSize = Ty->getScalarSizeInBits();
311 if (DL->isLegalInteger(DestSize) &&
312 DestSize >= DL->getPointerTypeSizeInBits(OpTy))
315 // Otherwise it's not a no-op.
318 case Instruction::Trunc:
319 // trunc to a native type is free (assuming the target has compare and
320 // shift-right of the same width).
321 if (DL && DL->isLegalInteger(DL->getTypeSizeInBits(Ty)))
328 unsigned getGEPCost(const Value *Ptr,
329 ArrayRef<const Value *> Operands) const override {
330 // In the basic model, we just assume that all-constant GEPs will be folded
331 // into their uses via addressing modes.
332 for (unsigned Idx = 0, Size = Operands.size(); Idx != Size; ++Idx)
333 if (!isa<Constant>(Operands[Idx]))
339 unsigned getCallCost(FunctionType *FTy, int NumArgs = -1) const override
341 assert(FTy && "FunctionType must be provided to this routine.");
343 // The target-independent implementation just measures the size of the
344 // function by approximating that each argument will take on average one
345 // instruction to prepare.
348 // Set the argument number to the number of explicit arguments in the
350 NumArgs = FTy->getNumParams();
352 return TCC_Basic * (NumArgs + 1);
355 unsigned getCallCost(const Function *F, int NumArgs = -1) const override
357 assert(F && "A concrete function must be provided to this routine.");
360 // Set the argument number to the number of explicit arguments in the
362 NumArgs = F->arg_size();
364 if (Intrinsic::ID IID = (Intrinsic::ID)F->getIntrinsicID()) {
365 FunctionType *FTy = F->getFunctionType();
366 SmallVector<Type *, 8> ParamTys(FTy->param_begin(), FTy->param_end());
367 return TopTTI->getIntrinsicCost(IID, FTy->getReturnType(), ParamTys);
370 if (!TopTTI->isLoweredToCall(F))
371 return TCC_Basic; // Give a basic cost if it will be lowered directly.
373 return TopTTI->getCallCost(F->getFunctionType(), NumArgs);
376 unsigned getCallCost(const Function *F,
377 ArrayRef<const Value *> Arguments) const override {
378 // Simply delegate to generic handling of the call.
379 // FIXME: We should use instsimplify or something else to catch calls which
380 // will constant fold with these arguments.
381 return TopTTI->getCallCost(F, Arguments.size());
384 unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy,
385 ArrayRef<Type *> ParamTys) const override {
388 // Intrinsics rarely (if ever) have normal argument setup constraints.
389 // Model them as having a basic instruction cost.
390 // FIXME: This is wrong for libc intrinsics.
393 case Intrinsic::assume:
394 case Intrinsic::dbg_declare:
395 case Intrinsic::dbg_value:
396 case Intrinsic::invariant_start:
397 case Intrinsic::invariant_end:
398 case Intrinsic::lifetime_start:
399 case Intrinsic::lifetime_end:
400 case Intrinsic::objectsize:
401 case Intrinsic::ptr_annotation:
402 case Intrinsic::var_annotation:
403 // These intrinsics don't actually represent code after lowering.
409 getIntrinsicCost(Intrinsic::ID IID, Type *RetTy,
410 ArrayRef<const Value *> Arguments) const override {
411 // Delegate to the generic intrinsic handling code. This mostly provides an
412 // opportunity for targets to (for example) special case the cost of
413 // certain intrinsics based on constants used as arguments.
414 SmallVector<Type *, 8> ParamTys;
415 ParamTys.reserve(Arguments.size());
416 for (unsigned Idx = 0, Size = Arguments.size(); Idx != Size; ++Idx)
417 ParamTys.push_back(Arguments[Idx]->getType());
418 return TopTTI->getIntrinsicCost(IID, RetTy, ParamTys);
421 unsigned getUserCost(const User *U) const override {
423 return TCC_Free; // Model all PHI nodes as free.
425 if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
426 SmallVector<const Value *, 4> Indices(GEP->idx_begin(), GEP->idx_end());
427 return TopTTI->getGEPCost(GEP->getPointerOperand(), Indices);
430 if (ImmutableCallSite CS = U) {
431 const Function *F = CS.getCalledFunction();
433 // Just use the called value type.
434 Type *FTy = CS.getCalledValue()->getType()->getPointerElementType();
435 return TopTTI->getCallCost(cast<FunctionType>(FTy), CS.arg_size());
438 SmallVector<const Value *, 8> Arguments(CS.arg_begin(), CS.arg_end());
439 return TopTTI->getCallCost(F, Arguments);
442 if (const CastInst *CI = dyn_cast<CastInst>(U)) {
443 // Result of a cmp instruction is often extended (to be used by other
444 // cmp instructions, logical or return instructions). These are usually
445 // nop on most sane targets.
446 if (isa<CmpInst>(CI->getOperand(0)))
450 // Otherwise delegate to the fully generic implementations.
451 return getOperationCost(Operator::getOpcode(U), U->getType(),
452 U->getNumOperands() == 1 ?
453 U->getOperand(0)->getType() : nullptr);
456 bool hasBranchDivergence() const override { return false; }
458 bool isLoweredToCall(const Function *F) const override {
459 // FIXME: These should almost certainly not be handled here, and instead
460 // handled with the help of TLI or the target itself. This was largely
461 // ported from existing analysis heuristics here so that such refactorings
462 // can take place in the future.
464 if (F->isIntrinsic())
467 if (F->hasLocalLinkage() || !F->hasName())
470 StringRef Name = F->getName();
472 // These will all likely lower to a single selection DAG node.
473 if (Name == "copysign" || Name == "copysignf" || Name == "copysignl" ||
474 Name == "fabs" || Name == "fabsf" || Name == "fabsl" || Name == "sin" ||
475 Name == "sinf" || Name == "sinl" || Name == "cos" || Name == "cosf" ||
476 Name == "cosl" || Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl")
479 // These are all likely to be optimized into something smaller.
480 if (Name == "pow" || Name == "powf" || Name == "powl" || Name == "exp2" ||
481 Name == "exp2l" || Name == "exp2f" || Name == "floor" || Name ==
482 "floorf" || Name == "ceil" || Name == "round" || Name == "ffs" ||
483 Name == "ffsl" || Name == "abs" || Name == "labs" || Name == "llabs")
489 void getUnrollingPreferences(Loop *, UnrollingPreferences &) const override {
492 bool isLegalAddImmediate(int64_t Imm) const override {
496 bool isLegalICmpImmediate(int64_t Imm) const override {
500 bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset,
501 bool HasBaseReg, int64_t Scale) const override
503 // Guess that reg+reg addressing is allowed. This heuristic is taken from
504 // the implementation of LSR.
505 return !BaseGV && BaseOffset == 0 && Scale <= 1;
508 int getScalingFactorCost(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset,
509 bool HasBaseReg, int64_t Scale) const override {
510 // Guess that all legal addressing mode are free.
511 if(isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg, Scale))
516 bool isTruncateFree(Type *Ty1, Type *Ty2) const override {
520 bool isTypeLegal(Type *Ty) const override {
524 unsigned getJumpBufAlignment() const override {
528 unsigned getJumpBufSize() const override {
532 bool shouldBuildLookupTables() const override {
537 getPopcntSupport(unsigned IntTyWidthInBit) const override {
541 bool haveFastSqrt(Type *Ty) const override {
545 unsigned getIntImmCost(const APInt &Imm, Type *Ty) const override {
549 unsigned getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
550 Type *Ty) const override {
554 unsigned getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm,
555 Type *Ty) const override {
559 unsigned getNumberOfRegisters(bool Vector) const override {
563 unsigned getRegisterBitWidth(bool Vector) const override {
567 unsigned getMaximumUnrollFactor() const override {
571 unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty, OperandValueKind,
572 OperandValueKind) const override {
576 unsigned getShuffleCost(ShuffleKind Kind, Type *Ty,
577 int Index = 0, Type *SubTp = nullptr) const override {
581 unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
582 Type *Src) const override {
586 unsigned getCFInstrCost(unsigned Opcode) const override {
590 unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
591 Type *CondTy = nullptr) const override {
595 unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
596 unsigned Index = -1) const override {
600 unsigned getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
601 unsigned AddressSpace) const override {
605 unsigned getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
606 ArrayRef<Type*> Tys) const override {
610 unsigned getNumberOfParts(Type *Tp) const override {
614 unsigned getAddressComputationCost(Type *Tp, bool) const override {
618 unsigned getReductionCost(unsigned, Type *, bool) const override {
622 unsigned getCostOfKeepingLiveOverCall(ArrayRef<Type*> Tys) const override {
628 } // end anonymous namespace
630 INITIALIZE_AG_PASS(NoTTI, TargetTransformInfo, "notti",
631 "No target information", true, true, true)
634 ImmutablePass *llvm::createNoTargetTransformInfoPass() {