LLVM  3.7.0
TargetTransformInfo.cpp
Go to the documentation of this file.
1 //===- llvm/Analysis/TargetTransformInfo.cpp ------------------------------===//
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 
12 #include "llvm/IR/CallSite.h"
13 #include "llvm/IR/DataLayout.h"
14 #include "llvm/IR/Instruction.h"
15 #include "llvm/IR/Instructions.h"
16 #include "llvm/IR/IntrinsicInst.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/IR/Operator.h"
20 
21 using namespace llvm;
22 
23 #define DEBUG_TYPE "tti"
24 
25 namespace {
26 /// \brief No-op implementation of the TTI interface using the utility base
27 /// classes.
28 ///
29 /// This is used when no target specific information is available.
30 struct NoTTIImpl : TargetTransformInfoImplCRTPBase<NoTTIImpl> {
31  explicit NoTTIImpl(const DataLayout &DL)
33 };
34 }
35 
37  : TTIImpl(new Model<NoTTIImpl>(NoTTIImpl(DL))) {}
38 
40 
42  : TTIImpl(std::move(Arg.TTIImpl)) {}
43 
45  TTIImpl = std::move(RHS.TTIImpl);
46  return *this;
47 }
48 
49 unsigned TargetTransformInfo::getOperationCost(unsigned Opcode, Type *Ty,
50  Type *OpTy) const {
51  return TTIImpl->getOperationCost(Opcode, Ty, OpTy);
52 }
53 
55  int NumArgs) const {
56  return TTIImpl->getCallCost(FTy, NumArgs);
57 }
58 
59 unsigned
61  ArrayRef<const Value *> Arguments) const {
62  return TTIImpl->getCallCost(F, Arguments);
63 }
64 
65 unsigned
67  ArrayRef<const Value *> Arguments) const {
68  return TTIImpl->getIntrinsicCost(IID, RetTy, Arguments);
69 }
70 
71 unsigned TargetTransformInfo::getUserCost(const User *U) const {
72  return TTIImpl->getUserCost(U);
73 }
74 
76  return TTIImpl->hasBranchDivergence();
77 }
78 
80  return TTIImpl->isSourceOfDivergence(V);
81 }
82 
84  return TTIImpl->isLoweredToCall(F);
85 }
86 
88  Loop *L, UnrollingPreferences &UP) const {
89  return TTIImpl->getUnrollingPreferences(L, UP);
90 }
91 
93  return TTIImpl->isLegalAddImmediate(Imm);
94 }
95 
97  return TTIImpl->isLegalICmpImmediate(Imm);
98 }
99 
101  int64_t BaseOffset,
102  bool HasBaseReg,
103  int64_t Scale,
104  unsigned AddrSpace) const {
105  return TTIImpl->isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
106  Scale, AddrSpace);
107 }
108 
110  int Consecutive) const {
111  return TTIImpl->isLegalMaskedStore(DataType, Consecutive);
112 }
113 
115  int Consecutive) const {
116  return TTIImpl->isLegalMaskedLoad(DataType, Consecutive);
117 }
118 
120  int64_t BaseOffset,
121  bool HasBaseReg,
122  int64_t Scale,
123  unsigned AddrSpace) const {
124  return TTIImpl->getScalingFactorCost(Ty, BaseGV, BaseOffset, HasBaseReg,
125  Scale, AddrSpace);
126 }
127 
129  return TTIImpl->isTruncateFree(Ty1, Ty2);
130 }
131 
133  return TTIImpl->isProfitableToHoist(I);
134 }
135 
137  return TTIImpl->isTypeLegal(Ty);
138 }
139 
141  return TTIImpl->getJumpBufAlignment();
142 }
143 
145  return TTIImpl->getJumpBufSize();
146 }
147 
149  return TTIImpl->shouldBuildLookupTables();
150 }
151 
152 bool TargetTransformInfo::enableAggressiveInterleaving(bool LoopHasReductions) const {
153  return TTIImpl->enableAggressiveInterleaving(LoopHasReductions);
154 }
155 
157 TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const {
158  return TTIImpl->getPopcntSupport(IntTyWidthInBit);
159 }
160 
162  return TTIImpl->haveFastSqrt(Ty);
163 }
164 
166  return TTIImpl->getFPOpCost(Ty);
167 }
168 
169 unsigned TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
170  return TTIImpl->getIntImmCost(Imm, Ty);
171 }
172 
173 unsigned TargetTransformInfo::getIntImmCost(unsigned Opcode, unsigned Idx,
174  const APInt &Imm, Type *Ty) const {
175  return TTIImpl->getIntImmCost(Opcode, Idx, Imm, Ty);
176 }
177 
179  const APInt &Imm, Type *Ty) const {
180  return TTIImpl->getIntImmCost(IID, Idx, Imm, Ty);
181 }
182 
183 unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const {
184  return TTIImpl->getNumberOfRegisters(Vector);
185 }
186 
187 unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const {
188  return TTIImpl->getRegisterBitWidth(Vector);
189 }
190 
191 unsigned TargetTransformInfo::getMaxInterleaveFactor(unsigned VF) const {
192  return TTIImpl->getMaxInterleaveFactor(VF);
193 }
194 
196  unsigned Opcode, Type *Ty, OperandValueKind Opd1Info,
197  OperandValueKind Opd2Info, OperandValueProperties Opd1PropInfo,
198  OperandValueProperties Opd2PropInfo) const {
199  return TTIImpl->getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
200  Opd1PropInfo, Opd2PropInfo);
201 }
202 
204  int Index, Type *SubTp) const {
205  return TTIImpl->getShuffleCost(Kind, Ty, Index, SubTp);
206 }
207 
208 unsigned TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
209  Type *Src) const {
210  return TTIImpl->getCastInstrCost(Opcode, Dst, Src);
211 }
212 
213 unsigned TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
214  return TTIImpl->getCFInstrCost(Opcode);
215 }
216 
217 unsigned TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
218  Type *CondTy) const {
219  return TTIImpl->getCmpSelInstrCost(Opcode, ValTy, CondTy);
220 }
221 
222 unsigned TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
223  unsigned Index) const {
224  return TTIImpl->getVectorInstrCost(Opcode, Val, Index);
225 }
226 
227 unsigned TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
228  unsigned Alignment,
229  unsigned AddressSpace) const {
230  return TTIImpl->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
231 }
232 
233 unsigned
235  unsigned Alignment,
236  unsigned AddressSpace) const {
237  return TTIImpl->getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
238 }
239 
241  unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
242  unsigned Alignment, unsigned AddressSpace) const {
243  return TTIImpl->getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
244  Alignment, AddressSpace);
245 }
246 
247 unsigned
249  ArrayRef<Type *> Tys) const {
250  return TTIImpl->getIntrinsicInstrCost(ID, RetTy, Tys);
251 }
252 
254  ArrayRef<Type *> Tys) const {
255  return TTIImpl->getCallInstrCost(F, RetTy, Tys);
256 }
257 
259  return TTIImpl->getNumberOfParts(Tp);
260 }
261 
263  bool IsComplex) const {
264  return TTIImpl->getAddressComputationCost(Tp, IsComplex);
265 }
266 
267 unsigned TargetTransformInfo::getReductionCost(unsigned Opcode, Type *Ty,
268  bool IsPairwiseForm) const {
269  return TTIImpl->getReductionCost(Opcode, Ty, IsPairwiseForm);
270 }
271 
272 unsigned
274  return TTIImpl->getCostOfKeepingLiveOverCall(Tys);
275 }
276 
278  MemIntrinsicInfo &Info) const {
279  return TTIImpl->getTgtMemIntrinsic(Inst, Info);
280 }
281 
283  IntrinsicInst *Inst, Type *ExpectedType) const {
284  return TTIImpl->getOrCreateResultFromMemIntrinsic(Inst, ExpectedType);
285 }
286 
288  const Function *Caller, const Function *Callee) const {
289  return TTIImpl->hasCompatibleFunctionAttributes(Caller, Callee);
290 }
291 
293 
294 TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {}
295 
297  std::function<Result(Function &)> TTICallback)
298  : TTICallback(TTICallback) {}
299 
301  return TTICallback(F);
302 }
303 
304 char TargetIRAnalysis::PassID;
305 
306 TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(Function &F) {
307  return Result(F.getParent()->getDataLayout());
308 }
309 
310 // Register the basic pass.
312  "Target Transform Information", false, true)
314 
315 void TargetTransformInfoWrapperPass::anchor() {}
316 
318  : ImmutablePass(ID) {
321 }
322 
324  TargetIRAnalysis TIRA)
325  : ImmutablePass(ID), TIRA(std::move(TIRA)) {
328 }
329 
331  TTI = TIRA.run(F);
332  return *TTI;
333 }
334 
337  return new TargetTransformInfoWrapperPass(std::move(TIRA));
338 }
PopcntSupportKind getPopcntSupport(unsigned IntTyWidthInBit) const
Return hardware support for population count.
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:104
unsigned getCallCost(FunctionType *FTy, int NumArgs=-1) const
Estimate the cost of a function call when lowered.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
TargetTransformInfo & operator=(TargetTransformInfo &&RHS)
bool isSourceOfDivergence(const Value *V) const
Returns whether V is a source of divergence.
unsigned getOperationCost(unsigned Opcode, Type *Ty, Type *OpTy=nullptr) const
Estimate the cost of a specific operation when lowered.
bool hasBranchDivergence() const
Return true if branch divergence exists.
TargetTransformInfoWrapperPass()
We must provide a default constructor for the pass but it should never be used.
unsigned getJumpBufSize() const
Returns the target's jmp_buf size in bytes.
ImmutablePass * createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA)
Create an analysis pass wrapper around a TTI object.
unsigned getMaxInterleaveFactor(unsigned VF) const
Analysis pass providing the TargetTransformInfo.
unsigned getNumberOfParts(Type *Tp) const
F(f)
bool isLegalMaskedLoad(Type *DataType, int Consecutive) const
TargetTransformInfo & getTTI(Function &F)
bool isLegalAddImmediate(int64_t Imm) const
Return true if the specified immediate is legal add immediate, that is the target has add instruction...
This file provides helpers for the implementation of a TargetTransformInfo-conforming class...
unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty, OperandValueKind Opd1Info=OK_AnyValue, OperandValueKind Opd2Info=OK_AnyValue, OperandValueProperties Opd1PropInfo=OP_None, OperandValueProperties Opd2PropInfo=OP_None) const
CRTP base class for use as a mix-in that aids implementing a TargetTransformInfo-compatible class...
unsigned getCFInstrCost(unsigned Opcode) const
unsigned getInterleavedMemoryOpCost(unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef< unsigned > Indices, unsigned Alignment, unsigned AddressSpace) const
unsigned getCallInstrCost(Function *F, Type *RetTy, ArrayRef< Type * > Tys) const
unsigned getAddressComputationCost(Type *Ty, bool IsComplex=false) const
unsigned getMaskedMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment, unsigned AddressSpace) const
FunctionType - Class to represent function types.
Definition: DerivedTypes.h:96
bool isTypeLegal(Type *Ty) const
Return true if this type is legal.
PopcntSupportKind
Flags indicating the kind of support for population count.
unsigned getRegisterBitWidth(bool Vector) const
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:31
unsigned getUserCost(const User *U) const
Estimate the cost of a given IR user when lowered.
void initializeTargetTransformInfoWrapperPassPass(PassRegistry &)
void getUnrollingPreferences(Loop *L, UnrollingPreferences &UP) const
Get target-customized preferences for the generic loop unrolling transformation.
bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset, bool HasBaseReg, int64_t Scale, unsigned AddrSpace=0) const
Return true if the addressing mode represented by AM is legal for this target, for a load/store of th...
unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy=nullptr) const
Wrapper pass for TargetTransformInfo.
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
TargetIRAnalysis()
Default construct a target IR analysis.
bool enableAggressiveInterleaving(bool LoopHasReductions) const
Don't restrict interleaved unrolling to small loops.
bool isLegalMaskedStore(Type *DataType, int Consecutive) const
Return true if the target works with masked instruction AVX2 allows masks for consecutive load and st...
unsigned getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) const
unsigned getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy, ArrayRef< Type * > Tys) const
int getScalingFactorCost(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset, bool HasBaseReg, int64_t Scale, unsigned AddrSpace=0) const
Return the cost of the scaling factor used in the addressing mode represented by AM for this target...
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:56
OperandValueProperties
Additional properties of an operand's values.
Value * getOrCreateResultFromMemIntrinsic(IntrinsicInst *Inst, Type *ExpectedType) const
unsigned getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment, unsigned AddressSpace) const
ImmutablePass class - This class is used to provide information that does not need to be run...
Definition: Pass.h:262
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
Module.h This file contains the declarations for the Module class.
bool hasCompatibleFunctionAttributes(const Function *Caller, const Function *Callee) const
AddressSpace
Definition: NVPTXBaseInfo.h:22
unsigned getShuffleCost(ShuffleKind Kind, Type *Tp, int Index=0, Type *SubTp=nullptr) const
bool haveFastSqrt(Type *Ty) const
Return true if the hardware has a fast square-root instruction.
bool getTgtMemIntrinsic(IntrinsicInst *Inst, MemIntrinsicInfo &Info) const
Class for arbitrary precision integers.
Definition: APInt.h:73
unsigned getJumpBufAlignment() const
Returns the target's jmp_buf alignment in bytes.
TargetTransformInfo(T Impl)
Construct a TTI object using a type implementing the Concept API below.
bool isLegalICmpImmediate(int64_t Imm) const
Return true if the specified immediate is legal icmp immediate, that is the target has icmp instructi...
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:372
bool shouldBuildLookupTables() const
Return true if switches should be turned into lookup tables for the target.
unsigned getReductionCost(unsigned Opcode, Type *Ty, bool IsPairwiseForm) const
Calculate the cost of performing a vector reduction.
Parameters that control the generic loop unrolling transformation.
#define I(x, y, z)
Definition: MD5.cpp:54
unsigned getIntImmCost(const APInt &Imm, Type *Ty) const
Return the expected cost of materializing for the given integer immediate of the specified type...
bool isLoweredToCall(const Function *F) const
Test whether calls to a function lower to actual program function calls.
const ARM::ArchExtKind Kind
TargetTransformInfo Result
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:365
LLVM Value Representation.
Definition: Value.h:69
unsigned getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index=-1) const
bool isTruncateFree(Type *Ty1, Type *Ty2) const
Return true if it's free to truncate a value of type Ty1 to type Ty2.
unsigned getNumberOfRegisters(bool Vector) const
print Print MemDeps of function
unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy, ArrayRef< Type * > ParamTys) const
Estimate the cost of an intrinsic when lowered.
OperandValueKind
Additional information about an operand's possible values.
This pass exposes codegen information to IR-level passes.
unsigned getCostOfKeepingLiveOverCall(ArrayRef< Type * > Tys) const
Information about a load/store intrinsic defined by the target.
bool isProfitableToHoist(Instruction *I) const
Return true if it is profitable to hoist instruction in the then/else to before if.
unsigned getFPOpCost(Type *Ty) const
Return the expected cost of supporting the floating point operation of the specified type...
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.