LLVM  3.7.0
TargetTransformInfoImpl.h
Go to the documentation of this file.
1 //===- TargetTransformInfoImpl.h --------------------------------*- C++ -*-===//
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 /// \file
10 /// This file provides helpers for the implementation of
11 /// a TargetTransformInfo-conforming class.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_ANALYSIS_TARGETTRANSFORMINFOIMPL_H
16 #define LLVM_ANALYSIS_TARGETTRANSFORMINFOIMPL_H
17 
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"
24 
25 namespace llvm {
26 
27 /// \brief Base class for use as a mix-in that aids implementing
28 /// a TargetTransformInfo-compatible class.
30 protected:
32 
33  const DataLayout &DL;
34 
35  explicit TargetTransformInfoImplBase(const DataLayout &DL) : DL(DL) {}
36 
37 public:
38  // Provide value semantics. MSVC requires that we spell all of these out.
40  : DL(Arg.DL) {}
42 
43  const DataLayout &getDataLayout() const { return DL; }
44 
45  unsigned getOperationCost(unsigned Opcode, Type *Ty, Type *OpTy) {
46  switch (Opcode) {
47  default:
48  // By default, just classify everything as 'basic'.
49  return TTI::TCC_Basic;
50 
51  case Instruction::GetElementPtr:
52  llvm_unreachable("Use getGEPCost for GEP operations!");
53 
54  case Instruction::BitCast:
55  assert(OpTy && "Cast instructions must provide the operand type");
56  if (Ty == OpTy || (Ty->isPointerTy() && OpTy->isPointerTy()))
57  // Identity and pointer-to-pointer casts are free.
58  return TTI::TCC_Free;
59 
60  // Otherwise, the default basic cost is used.
61  return TTI::TCC_Basic;
62 
63  case Instruction::IntToPtr: {
64  // An inttoptr cast is free so long as the input is a legal integer type
65  // which doesn't contain values outside the range of a pointer.
66  unsigned OpSize = OpTy->getScalarSizeInBits();
67  if (DL.isLegalInteger(OpSize) &&
68  OpSize <= DL.getPointerTypeSizeInBits(Ty))
69  return TTI::TCC_Free;
70 
71  // Otherwise it's not a no-op.
72  return TTI::TCC_Basic;
73  }
74  case Instruction::PtrToInt: {
75  // A ptrtoint cast is free so long as the result is large enough to store
76  // the pointer, and a legal integer type.
77  unsigned DestSize = Ty->getScalarSizeInBits();
78  if (DL.isLegalInteger(DestSize) &&
79  DestSize >= DL.getPointerTypeSizeInBits(OpTy))
80  return TTI::TCC_Free;
81 
82  // Otherwise it's not a no-op.
83  return TTI::TCC_Basic;
84  }
85  case Instruction::Trunc:
86  // trunc to a native type is free (assuming the target has compare and
87  // shift-right of the same width).
89  return TTI::TCC_Free;
90 
91  return TTI::TCC_Basic;
92  }
93  }
94 
96  // In the basic model, we just assume that all-constant GEPs will be folded
97  // into their uses via addressing modes.
98  for (unsigned Idx = 0, Size = Operands.size(); Idx != Size; ++Idx)
99  if (!isa<Constant>(Operands[Idx]))
100  return TTI::TCC_Basic;
101 
102  return TTI::TCC_Free;
103  }
104 
105  unsigned getCallCost(FunctionType *FTy, int NumArgs) {
106  assert(FTy && "FunctionType must be provided to this routine.");
107 
108  // The target-independent implementation just measures the size of the
109  // function by approximating that each argument will take on average one
110  // instruction to prepare.
111 
112  if (NumArgs < 0)
113  // Set the argument number to the number of explicit arguments in the
114  // function.
115  NumArgs = FTy->getNumParams();
116 
117  return TTI::TCC_Basic * (NumArgs + 1);
118  }
119 
120  unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy,
121  ArrayRef<Type *> ParamTys) {
122  switch (IID) {
123  default:
124  // Intrinsics rarely (if ever) have normal argument setup constraints.
125  // Model them as having a basic instruction cost.
126  // FIXME: This is wrong for libc intrinsics.
127  return TTI::TCC_Basic;
128 
129  case Intrinsic::annotation:
130  case Intrinsic::assume:
131  case Intrinsic::dbg_declare:
132  case Intrinsic::dbg_value:
133  case Intrinsic::invariant_start:
134  case Intrinsic::invariant_end:
135  case Intrinsic::lifetime_start:
136  case Intrinsic::lifetime_end:
137  case Intrinsic::objectsize:
138  case Intrinsic::ptr_annotation:
139  case Intrinsic::var_annotation:
140  case Intrinsic::experimental_gc_result_int:
141  case Intrinsic::experimental_gc_result_float:
142  case Intrinsic::experimental_gc_result_ptr:
143  case Intrinsic::experimental_gc_result:
144  case Intrinsic::experimental_gc_relocate:
145  // These intrinsics don't actually represent code after lowering.
146  return TTI::TCC_Free;
147  }
148  }
149 
150  bool hasBranchDivergence() { return false; }
151 
152  bool isSourceOfDivergence(const Value *V) { return false; }
153 
154  bool isLoweredToCall(const Function *F) {
155  // FIXME: These should almost certainly not be handled here, and instead
156  // handled with the help of TLI or the target itself. This was largely
157  // ported from existing analysis heuristics here so that such refactorings
158  // can take place in the future.
159 
160  if (F->isIntrinsic())
161  return false;
162 
163  if (F->hasLocalLinkage() || !F->hasName())
164  return true;
165 
166  StringRef Name = F->getName();
167 
168  // These will all likely lower to a single selection DAG node.
169  if (Name == "copysign" || Name == "copysignf" || Name == "copysignl" ||
170  Name == "fabs" || Name == "fabsf" || Name == "fabsl" || Name == "sin" ||
171  Name == "fmin" || Name == "fminf" || Name == "fminl" ||
172  Name == "fmax" || Name == "fmaxf" || Name == "fmaxl" ||
173  Name == "sinf" || Name == "sinl" || Name == "cos" || Name == "cosf" ||
174  Name == "cosl" || Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl")
175  return false;
176 
177  // These are all likely to be optimized into something smaller.
178  if (Name == "pow" || Name == "powf" || Name == "powl" || Name == "exp2" ||
179  Name == "exp2l" || Name == "exp2f" || Name == "floor" ||
180  Name == "floorf" || Name == "ceil" || Name == "round" ||
181  Name == "ffs" || Name == "ffsl" || Name == "abs" || Name == "labs" ||
182  Name == "llabs")
183  return false;
184 
185  return true;
186  }
187 
189 
190  bool isLegalAddImmediate(int64_t Imm) { return false; }
191 
192  bool isLegalICmpImmediate(int64_t Imm) { return false; }
193 
194  bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset,
195  bool HasBaseReg, int64_t Scale,
196  unsigned AddrSpace) {
197  // Guess that only reg and reg+reg addressing is allowed. This heuristic is
198  // taken from the implementation of LSR.
199  return !BaseGV && BaseOffset == 0 && (Scale == 0 || Scale == 1);
200  }
201 
202  bool isLegalMaskedStore(Type *DataType, int Consecutive) { return false; }
203 
204  bool isLegalMaskedLoad(Type *DataType, int Consecutive) { return false; }
205 
206  int getScalingFactorCost(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset,
207  bool HasBaseReg, int64_t Scale, unsigned AddrSpace) {
208  // Guess that all legal addressing mode are free.
209  if (isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
210  Scale, AddrSpace))
211  return 0;
212  return -1;
213  }
214 
215  bool isTruncateFree(Type *Ty1, Type *Ty2) { return false; }
216 
217  bool isProfitableToHoist(Instruction *I) { return true; }
218 
219  bool isTypeLegal(Type *Ty) { return false; }
220 
221  unsigned getJumpBufAlignment() { return 0; }
222 
223  unsigned getJumpBufSize() { return 0; }
224 
225  bool shouldBuildLookupTables() { return true; }
226 
227  bool enableAggressiveInterleaving(bool LoopHasReductions) { return false; }
228 
229  TTI::PopcntSupportKind getPopcntSupport(unsigned IntTyWidthInBit) {
230  return TTI::PSK_Software;
231  }
232 
233  bool haveFastSqrt(Type *Ty) { return false; }
234 
236 
237  unsigned getIntImmCost(const APInt &Imm, Type *Ty) { return TTI::TCC_Basic; }
238 
239  unsigned getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
240  Type *Ty) {
241  return TTI::TCC_Free;
242  }
243 
244  unsigned getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm,
245  Type *Ty) {
246  return TTI::TCC_Free;
247  }
248 
249  unsigned getNumberOfRegisters(bool Vector) { return 8; }
250 
251  unsigned getRegisterBitWidth(bool Vector) { return 32; }
252 
253  unsigned getMaxInterleaveFactor(unsigned VF) { return 1; }
254 
255  unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty,
256  TTI::OperandValueKind Opd1Info,
257  TTI::OperandValueKind Opd2Info,
258  TTI::OperandValueProperties Opd1PropInfo,
259  TTI::OperandValueProperties Opd2PropInfo) {
260  return 1;
261  }
262 
263  unsigned getShuffleCost(TTI::ShuffleKind Kind, Type *Ty, int Index,
264  Type *SubTp) {
265  return 1;
266  }
267 
268  unsigned getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) { return 1; }
269 
270  unsigned getCFInstrCost(unsigned Opcode) { return 1; }
271 
272  unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy) {
273  return 1;
274  }
275 
276  unsigned getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index) {
277  return 1;
278  }
279 
280  unsigned getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
281  unsigned AddressSpace) {
282  return 1;
283  }
284 
285  unsigned getMaskedMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
286  unsigned AddressSpace) {
287  return 1;
288  }
289 
290  unsigned getInterleavedMemoryOpCost(unsigned Opcode, Type *VecTy,
291  unsigned Factor,
292  ArrayRef<unsigned> Indices,
293  unsigned Alignment,
294  unsigned AddressSpace) {
295  return 1;
296  }
297 
299  ArrayRef<Type *> Tys) {
300  return 1;
301  }
302 
303  unsigned getCallInstrCost(Function *F, Type *RetTy, ArrayRef<Type *> Tys) {
304  return 1;
305  }
306 
307  unsigned getNumberOfParts(Type *Tp) { return 0; }
308 
309  unsigned getAddressComputationCost(Type *Tp, bool) { return 0; }
310 
311  unsigned getReductionCost(unsigned, Type *, bool) { return 1; }
312 
313  unsigned getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) { return 0; }
314 
316  return false;
317  }
318 
320  Type *ExpectedType) {
321  return nullptr;
322  }
323 
325  const Function *Callee) const {
326  return (Caller->getFnAttribute("target-cpu") ==
327  Callee->getFnAttribute("target-cpu")) &&
328  (Caller->getFnAttribute("target-features") ==
329  Callee->getFnAttribute("target-features"));
330  }
331 };
332 
333 /// \brief CRTP base class for use as a mix-in that aids implementing
334 /// a TargetTransformInfo-compatible class.
335 template <typename T>
337 private:
339 
340 protected:
342 
343 public:
344  // Provide value semantics. MSVC requires that we spell all of these out.
346  : BaseT(static_cast<const BaseT &>(Arg)) {}
348  : BaseT(std::move(static_cast<BaseT &>(Arg))) {}
349 
350  using BaseT::getCallCost;
351 
352  unsigned getCallCost(const Function *F, int NumArgs) {
353  assert(F && "A concrete function must be provided to this routine.");
354 
355  if (NumArgs < 0)
356  // Set the argument number to the number of explicit arguments in the
357  // function.
358  NumArgs = F->arg_size();
359 
360  if (Intrinsic::ID IID = F->getIntrinsicID()) {
361  FunctionType *FTy = F->getFunctionType();
362  SmallVector<Type *, 8> ParamTys(FTy->param_begin(), FTy->param_end());
363  return static_cast<T *>(this)
364  ->getIntrinsicCost(IID, FTy->getReturnType(), ParamTys);
365  }
366 
367  if (!static_cast<T *>(this)->isLoweredToCall(F))
368  return TTI::TCC_Basic; // Give a basic cost if it will be lowered
369  // directly.
370 
371  return static_cast<T *>(this)->getCallCost(F->getFunctionType(), NumArgs);
372  }
373 
374  unsigned getCallCost(const Function *F, ArrayRef<const Value *> Arguments) {
375  // Simply delegate to generic handling of the call.
376  // FIXME: We should use instsimplify or something else to catch calls which
377  // will constant fold with these arguments.
378  return static_cast<T *>(this)->getCallCost(F, Arguments.size());
379  }
380 
382 
383  unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy,
384  ArrayRef<const Value *> Arguments) {
385  // Delegate to the generic intrinsic handling code. This mostly provides an
386  // opportunity for targets to (for example) special case the cost of
387  // certain intrinsics based on constants used as arguments.
388  SmallVector<Type *, 8> ParamTys;
389  ParamTys.reserve(Arguments.size());
390  for (unsigned Idx = 0, Size = Arguments.size(); Idx != Size; ++Idx)
391  ParamTys.push_back(Arguments[Idx]->getType());
392  return static_cast<T *>(this)->getIntrinsicCost(IID, RetTy, ParamTys);
393  }
394 
395  unsigned getUserCost(const User *U) {
396  if (isa<PHINode>(U))
397  return TTI::TCC_Free; // Model all PHI nodes as free.
398 
399  if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
400  SmallVector<const Value *, 4> Indices(GEP->idx_begin(), GEP->idx_end());
401  return static_cast<T *>(this)
402  ->getGEPCost(GEP->getPointerOperand(), Indices);
403  }
404 
405  if (auto CS = ImmutableCallSite(U)) {
406  const Function *F = CS.getCalledFunction();
407  if (!F) {
408  // Just use the called value type.
409  Type *FTy = CS.getCalledValue()->getType()->getPointerElementType();
410  return static_cast<T *>(this)
411  ->getCallCost(cast<FunctionType>(FTy), CS.arg_size());
412  }
413 
414  SmallVector<const Value *, 8> Arguments(CS.arg_begin(), CS.arg_end());
415  return static_cast<T *>(this)->getCallCost(F, Arguments);
416  }
417 
418  if (const CastInst *CI = dyn_cast<CastInst>(U)) {
419  // Result of a cmp instruction is often extended (to be used by other
420  // cmp instructions, logical or return instructions). These are usually
421  // nop on most sane targets.
422  if (isa<CmpInst>(CI->getOperand(0)))
423  return TTI::TCC_Free;
424  }
425 
426  return static_cast<T *>(this)->getOperationCost(
427  Operator::getOpcode(U), U->getType(),
428  U->getNumOperands() == 1 ? U->getOperand(0)->getType() : nullptr);
429  }
430 };
431 }
432 
433 #endif
Base class for use as a mix-in that aids implementing a TargetTransformInfo-compatible class...
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:104
bool hasName() const
Definition: Value.h:228
unsigned getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index)
unsigned getNumParams() const
getNumParams - Return the number of fixed parameters this function type requires. ...
Definition: DerivedTypes.h:136
unsigned getNumOperands() const
Definition: User.h:138
unsigned getPointerTypeSizeInBits(Type *) const
Layout pointer size, in bits, based on the type.
Definition: DataLayout.cpp:602
unsigned getCallCost(const Function *F, ArrayRef< const Value * > Arguments)
bool isIntrinsic() const
Definition: Function.h:160
unsigned getCallCost(const Function *F, int NumArgs)
TargetTransformInfoImplCRTPBase(TargetTransformInfoImplCRTPBase &&Arg)
unsigned getOperationCost(unsigned Opcode, Type *Ty, Type *OpTy)
bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset, bool HasBaseReg, int64_t Scale, unsigned AddrSpace)
unsigned getCostOfKeepingLiveOverCall(ArrayRef< Type * > Tys)
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition: Function.h:225
F(f)
Hexagon Common GEP
void reserve(size_type N)
Definition: SmallVector.h:401
unsigned getIntImmCost(const APInt &Imm, Type *Ty)
size_t arg_size() const
Definition: Function.cpp:301
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:188
CRTP base class for use as a mix-in that aids implementing a TargetTransformInfo-compatible class...
bool getTgtMemIntrinsic(IntrinsicInst *Inst, MemIntrinsicInfo &Info)
This is the base class for all instructions that perform data casts.
Definition: InstrTypes.h:389
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
param_iterator param_end() const
Definition: DerivedTypes.h:125
bool isTruncateFree(Type *Ty1, Type *Ty2)
bool enableAggressiveInterleaving(bool LoopHasReductions)
FunctionType - Class to represent function types.
Definition: DerivedTypes.h:96
unsigned getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy, ArrayRef< Type * > Tys)
PopcntSupportKind
Flags indicating the kind of support for population count.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:31
unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty, TTI::OperandValueKind Opd1Info, TTI::OperandValueKind Opd2Info, TTI::OperandValueProperties Opd1PropInfo, TTI::OperandValueProperties Opd2PropInfo)
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:134
unsigned getShuffleCost(TTI::ShuffleKind Kind, Type *Ty, int Index, Type *SubTp)
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
TargetTransformInfoImplBase(const TargetTransformInfoImplBase &Arg)
bool isLegalMaskedStore(Type *DataType, int Consecutive)
unsigned getAddressComputationCost(Type *Tp, bool)
param_iterator param_begin() const
Definition: DerivedTypes.h:124
unsigned getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm, Type *Ty)
Expected to fold away in lowering.
bool isLegalMaskedLoad(Type *DataType, int Consecutive)
Value * getOperand(unsigned i) const
Definition: User.h:118
SI Fold Operands
bool isPointerTy() const
isPointerTy - True if this is an instance of PointerType.
Definition: Type.h:217
unsigned getCallCost(FunctionType *FTy, int NumArgs)
TargetTransformInfoImplCRTPBase(const TargetTransformInfoImplCRTPBase &Arg)
OperandValueProperties
Additional properties of an operand's values.
unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy, ArrayRef< const Value * > Arguments)
unsigned getGEPCost(const Value *Ptr, ArrayRef< const Value * > Operands)
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
unsigned getScalarSizeInBits() const LLVM_READONLY
getScalarSizeInBits - If this is a vector type, return the getPrimitiveSizeInBits value for the eleme...
Definition: Type.cpp:139
TargetTransformInfoImplBase(const DataLayout &DL)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:222
AddressSpace
Definition: NVPTXBaseInfo.h:22
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
Definition: Function.h:159
int getScalingFactorCost(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset, bool HasBaseReg, int64_t Scale, unsigned AddrSpace)
unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy, ArrayRef< Type * > ParamTys)
Class for arbitrary precision integers.
Definition: APInt.h:73
unsigned getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src)
const DataLayout & getDataLayout() const
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
Definition: Operator.h:48
unsigned getReductionCost(unsigned, Type *, bool)
unsigned getMaskedMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment, unsigned AddressSpace)
void getUnrollingPreferences(Loop *, TTI::UnrollingPreferences &)
unsigned getCFInstrCost(unsigned Opcode)
bool isLegalInteger(unsigned Width) const
Returns true if the specified type is known to be a native integer type supported by the CPU...
Definition: DataLayout.h:239
Parameters that control the generic loop unrolling transformation.
ImmutableCallSite - establish a view to a call site for examination.
Definition: CallSite.h:418
#define I(x, y, z)
Definition: MD5.cpp:54
FunctionType * getFunctionType() const
Definition: Function.cpp:227
unsigned getInterleavedMemoryOpCost(unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef< unsigned > Indices, unsigned Alignment, unsigned AddressSpace)
bool hasLocalLinkage() const
Definition: GlobalValue.h:280
TargetTransformInfoImplBase(TargetTransformInfoImplBase &&Arg)
Type * getReturnType() const
Definition: DerivedTypes.h:121
The cost of a typical 'add' instruction.
const ARM::ArchExtKind Kind
aarch64 promote const
LLVM Value Representation.
Definition: Value.h:69
unsigned getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm, Type *Ty)
unsigned getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment, unsigned AddressSpace)
uint64_t getTypeSizeInBits(Type *Ty) const
Size examples:
Definition: DataLayout.h:507
OperandValueKind
Additional information about an operand's possible values.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy)
This pass exposes codegen information to IR-level passes.
bool hasCompatibleFunctionAttributes(const Function *Caller, const Function *Callee) const
unsigned getCallInstrCost(Function *F, Type *RetTy, ArrayRef< Type * > Tys)
Information about a load/store intrinsic defined by the target.
TTI::PopcntSupportKind getPopcntSupport(unsigned IntTyWidthInBit)
Value * getOrCreateResultFromMemIntrinsic(IntrinsicInst *Inst, Type *ExpectedType)
IntrinsicInst - A useful wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:37
ShuffleKind
The various kinds of shuffle patterns for vector queries.