1 //===- TargetTransformInfoImpl.h --------------------------------*- C++ -*-===//
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 /// This file provides helpers for the implementation of
11 /// a TargetTransformInfo-conforming class.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_ANALYSIS_TARGETTRANSFORMINFOIMPL_H
16 #define LLVM_ANALYSIS_TARGETTRANSFORMINFOIMPL_H
18 #include "llvm/Analysis/TargetTransformInfo.h"
19 #include "llvm/IR/CallSite.h"
20 #include "llvm/IR/DataLayout.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/Operator.h"
23 #include "llvm/IR/Type.h"
27 /// \brief Base class for use as a mix-in that aids implementing
28 /// a TargetTransformInfo-compatible class.
29 class TargetTransformInfoImplBase {
31 typedef TargetTransformInfo TTI;
35 explicit TargetTransformInfoImplBase(const DataLayout *DL)
39 // Provide value semantics. MSVC requires that we spell all of these out.
40 TargetTransformInfoImplBase(const TargetTransformInfoImplBase &Arg)
42 TargetTransformInfoImplBase(TargetTransformInfoImplBase &&Arg)
43 : DL(std::move(Arg.DL)) {}
44 TargetTransformInfoImplBase &
45 operator=(const TargetTransformInfoImplBase &RHS) {
49 TargetTransformInfoImplBase &operator=(TargetTransformInfoImplBase &&RHS) {
50 DL = std::move(RHS.DL);
54 unsigned getOperationCost(unsigned Opcode, Type *Ty, Type *OpTy) {
57 // By default, just classify everything as 'basic'.
58 return TTI::TCC_Basic;
60 case Instruction::GetElementPtr:
61 llvm_unreachable("Use getGEPCost for GEP operations!");
63 case Instruction::BitCast:
64 assert(OpTy && "Cast instructions must provide the operand type");
65 if (Ty == OpTy || (Ty->isPointerTy() && OpTy->isPointerTy()))
66 // Identity and pointer-to-pointer casts are free.
69 // Otherwise, the default basic cost is used.
70 return TTI::TCC_Basic;
72 case Instruction::IntToPtr: {
74 return TTI::TCC_Basic;
76 // An inttoptr cast is free so long as the input is a legal integer type
77 // which doesn't contain values outside the range of a pointer.
78 unsigned OpSize = OpTy->getScalarSizeInBits();
79 if (DL->isLegalInteger(OpSize) &&
80 OpSize <= DL->getPointerTypeSizeInBits(Ty))
83 // Otherwise it's not a no-op.
84 return TTI::TCC_Basic;
86 case Instruction::PtrToInt: {
88 return TTI::TCC_Basic;
90 // A ptrtoint cast is free so long as the result is large enough to store
91 // the pointer, and a legal integer type.
92 unsigned DestSize = Ty->getScalarSizeInBits();
93 if (DL->isLegalInteger(DestSize) &&
94 DestSize >= DL->getPointerTypeSizeInBits(OpTy))
97 // Otherwise it's not a no-op.
98 return TTI::TCC_Basic;
100 case Instruction::Trunc:
101 // trunc to a native type is free (assuming the target has compare and
102 // shift-right of the same width).
103 if (DL && DL->isLegalInteger(DL->getTypeSizeInBits(Ty)))
104 return TTI::TCC_Free;
106 return TTI::TCC_Basic;
110 unsigned getGEPCost(const Value *Ptr, ArrayRef<const Value *> Operands) {
111 // In the basic model, we just assume that all-constant GEPs will be folded
112 // into their uses via addressing modes.
113 for (unsigned Idx = 0, Size = Operands.size(); Idx != Size; ++Idx)
114 if (!isa<Constant>(Operands[Idx]))
115 return TTI::TCC_Basic;
117 return TTI::TCC_Free;
120 unsigned getCallCost(FunctionType *FTy, int NumArgs) {
121 assert(FTy && "FunctionType must be provided to this routine.");
123 // The target-independent implementation just measures the size of the
124 // function by approximating that each argument will take on average one
125 // instruction to prepare.
128 // Set the argument number to the number of explicit arguments in the
130 NumArgs = FTy->getNumParams();
132 return TTI::TCC_Basic * (NumArgs + 1);
135 unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy,
136 ArrayRef<Type *> ParamTys) {
139 // Intrinsics rarely (if ever) have normal argument setup constraints.
140 // Model them as having a basic instruction cost.
141 // FIXME: This is wrong for libc intrinsics.
142 return TTI::TCC_Basic;
144 case Intrinsic::annotation:
145 case Intrinsic::assume:
146 case Intrinsic::dbg_declare:
147 case Intrinsic::dbg_value:
148 case Intrinsic::invariant_start:
149 case Intrinsic::invariant_end:
150 case Intrinsic::lifetime_start:
151 case Intrinsic::lifetime_end:
152 case Intrinsic::objectsize:
153 case Intrinsic::ptr_annotation:
154 case Intrinsic::var_annotation:
155 case Intrinsic::experimental_gc_result_int:
156 case Intrinsic::experimental_gc_result_float:
157 case Intrinsic::experimental_gc_result_ptr:
158 case Intrinsic::experimental_gc_result:
159 case Intrinsic::experimental_gc_relocate:
160 // These intrinsics don't actually represent code after lowering.
161 return TTI::TCC_Free;
165 bool hasBranchDivergence() { return false; }
167 bool isLoweredToCall(const Function *F) {
168 // FIXME: These should almost certainly not be handled here, and instead
169 // handled with the help of TLI or the target itself. This was largely
170 // ported from existing analysis heuristics here so that such refactorings
171 // can take place in the future.
173 if (F->isIntrinsic())
176 if (F->hasLocalLinkage() || !F->hasName())
179 StringRef Name = F->getName();
181 // These will all likely lower to a single selection DAG node.
182 if (Name == "copysign" || Name == "copysignf" || Name == "copysignl" ||
183 Name == "fabs" || Name == "fabsf" || Name == "fabsl" || Name == "sin" ||
184 Name == "fmin" || Name == "fminf" || Name == "fminl" ||
185 Name == "fmax" || Name == "fmaxf" || Name == "fmaxl" ||
186 Name == "sinf" || Name == "sinl" || Name == "cos" || Name == "cosf" ||
187 Name == "cosl" || Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl")
190 // These are all likely to be optimized into something smaller.
191 if (Name == "pow" || Name == "powf" || Name == "powl" || Name == "exp2" ||
192 Name == "exp2l" || Name == "exp2f" || Name == "floor" ||
193 Name == "floorf" || Name == "ceil" || Name == "round" ||
194 Name == "ffs" || Name == "ffsl" || Name == "abs" || Name == "labs" ||
201 void getUnrollingPreferences(Loop *, TTI::UnrollingPreferences &) {}
203 bool isLegalAddImmediate(int64_t Imm) { return false; }
205 bool isLegalICmpImmediate(int64_t Imm) { return false; }
207 bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset,
208 bool HasBaseReg, int64_t Scale) {
209 // Guess that reg+reg addressing is allowed. This heuristic is taken from
210 // the implementation of LSR.
211 return !BaseGV && BaseOffset == 0 && Scale <= 1;
214 bool isLegalMaskedStore(Type *DataType, int Consecutive) { return false; }
216 bool isLegalMaskedLoad(Type *DataType, int Consecutive) { return false; }
218 int getScalingFactorCost(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset,
219 bool HasBaseReg, int64_t Scale) {
220 // Guess that all legal addressing mode are free.
221 if (isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg, Scale))
226 bool isTruncateFree(Type *Ty1, Type *Ty2) { return false; }
228 bool isProfitableToHoist(Instruction *I) { return true; }
230 bool isTypeLegal(Type *Ty) { return false; }
232 unsigned getJumpBufAlignment() { return 0; }
234 unsigned getJumpBufSize() { return 0; }
236 bool shouldBuildLookupTables() { return true; }
238 bool enableAggressiveInterleaving(bool LoopHasReductions) { return false; }
240 TTI::PopcntSupportKind getPopcntSupport(unsigned IntTyWidthInBit) {
241 return TTI::PSK_Software;
244 bool haveFastSqrt(Type *Ty) { return false; }
246 unsigned getFPOpCost(Type *Ty) { return TargetTransformInfo::TCC_Basic; }
248 unsigned getIntImmCost(const APInt &Imm, Type *Ty) { return TTI::TCC_Basic; }
250 unsigned getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
252 return TTI::TCC_Free;
255 unsigned getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm,
257 return TTI::TCC_Free;
260 unsigned getNumberOfRegisters(bool Vector) { return 8; }
262 unsigned getRegisterBitWidth(bool Vector) { return 32; }
264 unsigned getMaxInterleaveFactor() { return 1; }
266 unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty,
267 TTI::OperandValueKind Opd1Info,
268 TTI::OperandValueKind Opd2Info,
269 TTI::OperandValueProperties Opd1PropInfo,
270 TTI::OperandValueProperties Opd2PropInfo) {
274 unsigned getShuffleCost(TTI::ShuffleKind Kind, Type *Ty, int Index,
279 unsigned getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) { return 1; }
281 unsigned getCFInstrCost(unsigned Opcode) { return 1; }
283 unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy) {
287 unsigned getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index) {
291 unsigned getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
292 unsigned AddressSpace) {
296 unsigned getMaskedMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
297 unsigned AddressSpace) {
301 unsigned getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
302 ArrayRef<Type *> Tys) {
306 unsigned getCallInstrCost(Function *F, Type *RetTy, ArrayRef<Type *> Tys) {
310 unsigned getNumberOfParts(Type *Tp) { return 0; }
312 unsigned getAddressComputationCost(Type *Tp, bool) { return 0; }
314 unsigned getReductionCost(unsigned, Type *, bool) { return 1; }
316 unsigned getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) { return 0; }
318 bool getTgtMemIntrinsic(IntrinsicInst *Inst, MemIntrinsicInfo &Info) {
322 Value *getOrCreateResultFromMemIntrinsic(IntrinsicInst *Inst,
323 Type *ExpectedType) {
328 /// \brief CRTP base class for use as a mix-in that aids implementing
329 /// a TargetTransformInfo-compatible class.
330 template <typename T>
331 class TargetTransformInfoImplCRTPBase : public TargetTransformInfoImplBase {
333 typedef TargetTransformInfoImplBase BaseT;
336 explicit TargetTransformInfoImplCRTPBase(const DataLayout *DL)
340 // Provide value semantics. MSVC requires that we spell all of these out.
341 TargetTransformInfoImplCRTPBase(const TargetTransformInfoImplCRTPBase &Arg)
342 : BaseT(static_cast<const BaseT &>(Arg)) {}
343 TargetTransformInfoImplCRTPBase(TargetTransformInfoImplCRTPBase &&Arg)
344 : BaseT(std::move(static_cast<BaseT &>(Arg))) {}
345 TargetTransformInfoImplCRTPBase &
346 operator=(const TargetTransformInfoImplCRTPBase &RHS) {
347 BaseT::operator=(static_cast<const BaseT &>(RHS));
350 TargetTransformInfoImplCRTPBase &
351 operator=(TargetTransformInfoImplCRTPBase &&RHS) {
352 BaseT::operator=(std::move(static_cast<BaseT &>(RHS)));
356 using BaseT::getCallCost;
358 unsigned getCallCost(const Function *F, int NumArgs) {
359 assert(F && "A concrete function must be provided to this routine.");
362 // Set the argument number to the number of explicit arguments in the
364 NumArgs = F->arg_size();
366 if (Intrinsic::ID IID = (Intrinsic::ID)F->getIntrinsicID()) {
367 FunctionType *FTy = F->getFunctionType();
368 SmallVector<Type *, 8> ParamTys(FTy->param_begin(), FTy->param_end());
369 return static_cast<T *>(this)
370 ->getIntrinsicCost(IID, FTy->getReturnType(), ParamTys);
373 if (!static_cast<T *>(this)->isLoweredToCall(F))
374 return TTI::TCC_Basic; // Give a basic cost if it will be lowered
377 return static_cast<T *>(this)->getCallCost(F->getFunctionType(), NumArgs);
380 unsigned getCallCost(const Function *F, ArrayRef<const Value *> Arguments) {
381 // Simply delegate to generic handling of the call.
382 // FIXME: We should use instsimplify or something else to catch calls which
383 // will constant fold with these arguments.
384 return static_cast<T *>(this)->getCallCost(F, Arguments.size());
387 using BaseT::getIntrinsicCost;
389 unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy,
390 ArrayRef<const Value *> Arguments) {
391 // Delegate to the generic intrinsic handling code. This mostly provides an
392 // opportunity for targets to (for example) special case the cost of
393 // certain intrinsics based on constants used as arguments.
394 SmallVector<Type *, 8> ParamTys;
395 ParamTys.reserve(Arguments.size());
396 for (unsigned Idx = 0, Size = Arguments.size(); Idx != Size; ++Idx)
397 ParamTys.push_back(Arguments[Idx]->getType());
398 return static_cast<T *>(this)->getIntrinsicCost(IID, RetTy, ParamTys);
401 unsigned getUserCost(const User *U) {
403 return TTI::TCC_Free; // Model all PHI nodes as free.
405 if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
406 SmallVector<const Value *, 4> Indices(GEP->idx_begin(), GEP->idx_end());
407 return static_cast<T *>(this)
408 ->getGEPCost(GEP->getPointerOperand(), Indices);
411 if (ImmutableCallSite CS = U) {
412 const Function *F = CS.getCalledFunction();
414 // Just use the called value type.
415 Type *FTy = CS.getCalledValue()->getType()->getPointerElementType();
416 return static_cast<T *>(this)
417 ->getCallCost(cast<FunctionType>(FTy), CS.arg_size());
420 SmallVector<const Value *, 8> Arguments(CS.arg_begin(), CS.arg_end());
421 return static_cast<T *>(this)->getCallCost(F, Arguments);
424 if (const CastInst *CI = dyn_cast<CastInst>(U)) {
425 // Result of a cmp instruction is often extended (to be used by other
426 // cmp instructions, logical or return instructions). These are usually
427 // nop on most sane targets.
428 if (isa<CmpInst>(CI->getOperand(0)))
429 return TTI::TCC_Free;
432 return static_cast<T *>(this)->getOperationCost(
433 Operator::getOpcode(U), U->getType(),
434 U->getNumOperands() == 1 ? U->getOperand(0)->getType() : nullptr);