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/Analysis/TargetTransformInfoImpl.h"
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"
19 #include "llvm/Support/ErrorHandling.h"
23 #define DEBUG_TYPE "tti"
26 /// \brief No-op implementation of the TTI interface using the utility base
29 /// This is used when no target specific information is available.
30 struct NoTTIImpl : TargetTransformInfoImplCRTPBase<NoTTIImpl> {
31 explicit NoTTIImpl(const DataLayout &DL)
32 : TargetTransformInfoImplCRTPBase<NoTTIImpl>(DL) {}
36 TargetTransformInfo::TargetTransformInfo(const DataLayout &DL)
37 : TTIImpl(new Model<NoTTIImpl>(NoTTIImpl(DL))) {}
39 TargetTransformInfo::~TargetTransformInfo() {}
41 TargetTransformInfo::TargetTransformInfo(TargetTransformInfo &&Arg)
42 : TTIImpl(std::move(Arg.TTIImpl)) {}
44 TargetTransformInfo &TargetTransformInfo::operator=(TargetTransformInfo &&RHS) {
45 TTIImpl = std::move(RHS.TTIImpl);
49 int TargetTransformInfo::getOperationCost(unsigned Opcode, Type *Ty,
51 int Cost = TTIImpl->getOperationCost(Opcode, Ty, OpTy);
52 assert(Cost >= 0 && "TTI should not produce negative costs!");
56 int TargetTransformInfo::getCallCost(FunctionType *FTy, int NumArgs) const {
57 int Cost = TTIImpl->getCallCost(FTy, NumArgs);
58 assert(Cost >= 0 && "TTI should not produce negative costs!");
62 int TargetTransformInfo::getCallCost(const Function *F,
63 ArrayRef<const Value *> Arguments) const {
64 int Cost = TTIImpl->getCallCost(F, Arguments);
65 assert(Cost >= 0 && "TTI should not produce negative costs!");
69 int TargetTransformInfo::getIntrinsicCost(
70 Intrinsic::ID IID, Type *RetTy, ArrayRef<const Value *> Arguments) const {
71 int Cost = TTIImpl->getIntrinsicCost(IID, RetTy, Arguments);
72 assert(Cost >= 0 && "TTI should not produce negative costs!");
76 int TargetTransformInfo::getUserCost(const User *U) const {
77 int Cost = TTIImpl->getUserCost(U);
78 assert(Cost >= 0 && "TTI should not produce negative costs!");
82 bool TargetTransformInfo::hasBranchDivergence() const {
83 return TTIImpl->hasBranchDivergence();
86 bool TargetTransformInfo::isSourceOfDivergence(const Value *V) const {
87 return TTIImpl->isSourceOfDivergence(V);
90 bool TargetTransformInfo::isLoweredToCall(const Function *F) const {
91 return TTIImpl->isLoweredToCall(F);
94 void TargetTransformInfo::getUnrollingPreferences(
95 Loop *L, UnrollingPreferences &UP) const {
96 return TTIImpl->getUnrollingPreferences(L, UP);
99 bool TargetTransformInfo::isLegalAddImmediate(int64_t Imm) const {
100 return TTIImpl->isLegalAddImmediate(Imm);
103 bool TargetTransformInfo::isLegalICmpImmediate(int64_t Imm) const {
104 return TTIImpl->isLegalICmpImmediate(Imm);
107 bool TargetTransformInfo::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
111 unsigned AddrSpace) const {
112 return TTIImpl->isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
116 bool TargetTransformInfo::isLegalMaskedStore(Type *DataType,
117 int Consecutive) const {
118 return TTIImpl->isLegalMaskedStore(DataType, Consecutive);
121 bool TargetTransformInfo::isLegalMaskedLoad(Type *DataType,
122 int Consecutive) const {
123 return TTIImpl->isLegalMaskedLoad(DataType, Consecutive);
126 int TargetTransformInfo::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
130 unsigned AddrSpace) const {
131 int Cost = TTIImpl->getScalingFactorCost(Ty, BaseGV, BaseOffset, HasBaseReg,
133 assert(Cost >= 0 && "TTI should not produce negative costs!");
137 bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const {
138 return TTIImpl->isTruncateFree(Ty1, Ty2);
141 bool TargetTransformInfo::isZExtFree(Type *Ty1, Type *Ty2) const {
142 return TTIImpl->isZExtFree(Ty1, Ty2);
145 bool TargetTransformInfo::isProfitableToHoist(Instruction *I) const {
146 return TTIImpl->isProfitableToHoist(I);
149 bool TargetTransformInfo::isTypeLegal(Type *Ty) const {
150 return TTIImpl->isTypeLegal(Ty);
153 unsigned TargetTransformInfo::getJumpBufAlignment() const {
154 return TTIImpl->getJumpBufAlignment();
157 unsigned TargetTransformInfo::getJumpBufSize() const {
158 return TTIImpl->getJumpBufSize();
161 bool TargetTransformInfo::shouldBuildLookupTables() const {
162 return TTIImpl->shouldBuildLookupTables();
165 bool TargetTransformInfo::enableAggressiveInterleaving(bool LoopHasReductions) const {
166 return TTIImpl->enableAggressiveInterleaving(LoopHasReductions);
169 TargetTransformInfo::PopcntSupportKind
170 TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const {
171 return TTIImpl->getPopcntSupport(IntTyWidthInBit);
174 bool TargetTransformInfo::haveFastSqrt(Type *Ty) const {
175 return TTIImpl->haveFastSqrt(Ty);
178 int TargetTransformInfo::getFPOpCost(Type *Ty) const {
179 int Cost = TTIImpl->getFPOpCost(Ty);
180 assert(Cost >= 0 && "TTI should not produce negative costs!");
184 int TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
185 int Cost = TTIImpl->getIntImmCost(Imm, Ty);
186 assert(Cost >= 0 && "TTI should not produce negative costs!");
190 int TargetTransformInfo::getIntImmCost(unsigned Opcode, unsigned Idx,
191 const APInt &Imm, Type *Ty) const {
192 int Cost = TTIImpl->getIntImmCost(Opcode, Idx, Imm, Ty);
193 assert(Cost >= 0 && "TTI should not produce negative costs!");
197 int TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
198 const APInt &Imm, Type *Ty) const {
199 int Cost = TTIImpl->getIntImmCost(IID, Idx, Imm, Ty);
200 assert(Cost >= 0 && "TTI should not produce negative costs!");
204 unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const {
205 return TTIImpl->getNumberOfRegisters(Vector);
208 unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const {
209 return TTIImpl->getRegisterBitWidth(Vector);
212 unsigned TargetTransformInfo::getMaxInterleaveFactor(unsigned VF) const {
213 return TTIImpl->getMaxInterleaveFactor(VF);
216 int TargetTransformInfo::getArithmeticInstrCost(
217 unsigned Opcode, Type *Ty, OperandValueKind Opd1Info,
218 OperandValueKind Opd2Info, OperandValueProperties Opd1PropInfo,
219 OperandValueProperties Opd2PropInfo) const {
220 int Cost = TTIImpl->getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
221 Opd1PropInfo, Opd2PropInfo);
222 assert(Cost >= 0 && "TTI should not produce negative costs!");
226 int TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Ty, int Index,
228 int Cost = TTIImpl->getShuffleCost(Kind, Ty, Index, SubTp);
229 assert(Cost >= 0 && "TTI should not produce negative costs!");
233 int TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
235 int Cost = TTIImpl->getCastInstrCost(Opcode, Dst, Src);
236 assert(Cost >= 0 && "TTI should not produce negative costs!");
240 int TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
241 int Cost = TTIImpl->getCFInstrCost(Opcode);
242 assert(Cost >= 0 && "TTI should not produce negative costs!");
246 int TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
247 Type *CondTy) const {
248 int Cost = TTIImpl->getCmpSelInstrCost(Opcode, ValTy, CondTy);
249 assert(Cost >= 0 && "TTI should not produce negative costs!");
253 int TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
254 unsigned Index) const {
255 int Cost = TTIImpl->getVectorInstrCost(Opcode, Val, Index);
256 assert(Cost >= 0 && "TTI should not produce negative costs!");
260 int TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
262 unsigned AddressSpace) const {
263 int Cost = TTIImpl->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
264 assert(Cost >= 0 && "TTI should not produce negative costs!");
268 int TargetTransformInfo::getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
270 unsigned AddressSpace) const {
272 TTIImpl->getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
273 assert(Cost >= 0 && "TTI should not produce negative costs!");
277 int TargetTransformInfo::getInterleavedMemoryOpCost(
278 unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
279 unsigned Alignment, unsigned AddressSpace) const {
280 int Cost = TTIImpl->getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
281 Alignment, AddressSpace);
282 assert(Cost >= 0 && "TTI should not produce negative costs!");
286 int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
287 ArrayRef<Type *> Tys) const {
288 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Tys);
289 assert(Cost >= 0 && "TTI should not produce negative costs!");
293 int TargetTransformInfo::getCallInstrCost(Function *F, Type *RetTy,
294 ArrayRef<Type *> Tys) const {
295 int Cost = TTIImpl->getCallInstrCost(F, RetTy, Tys);
296 assert(Cost >= 0 && "TTI should not produce negative costs!");
300 unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
301 return TTIImpl->getNumberOfParts(Tp);
304 int TargetTransformInfo::getAddressComputationCost(Type *Tp,
305 bool IsComplex) const {
306 int Cost = TTIImpl->getAddressComputationCost(Tp, IsComplex);
307 assert(Cost >= 0 && "TTI should not produce negative costs!");
311 int TargetTransformInfo::getReductionCost(unsigned Opcode, Type *Ty,
312 bool IsPairwiseForm) const {
313 int Cost = TTIImpl->getReductionCost(Opcode, Ty, IsPairwiseForm);
314 assert(Cost >= 0 && "TTI should not produce negative costs!");
319 TargetTransformInfo::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) const {
320 return TTIImpl->getCostOfKeepingLiveOverCall(Tys);
323 bool TargetTransformInfo::getTgtMemIntrinsic(IntrinsicInst *Inst,
324 MemIntrinsicInfo &Info) const {
325 return TTIImpl->getTgtMemIntrinsic(Inst, Info);
328 Value *TargetTransformInfo::getOrCreateResultFromMemIntrinsic(
329 IntrinsicInst *Inst, Type *ExpectedType) const {
330 return TTIImpl->getOrCreateResultFromMemIntrinsic(Inst, ExpectedType);
333 bool TargetTransformInfo::areInlineCompatible(const Function *Caller,
334 const Function *Callee) const {
335 return TTIImpl->areInlineCompatible(Caller, Callee);
338 TargetTransformInfo::Concept::~Concept() {}
340 TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {}
342 TargetIRAnalysis::TargetIRAnalysis(
343 std::function<Result(Function &)> TTICallback)
344 : TTICallback(TTICallback) {}
346 TargetIRAnalysis::Result TargetIRAnalysis::run(Function &F) {
347 return TTICallback(F);
350 char TargetIRAnalysis::PassID;
352 TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(Function &F) {
353 return Result(F.getParent()->getDataLayout());
356 // Register the basic pass.
357 INITIALIZE_PASS(TargetTransformInfoWrapperPass, "tti",
358 "Target Transform Information", false, true)
359 char TargetTransformInfoWrapperPass::ID = 0;
361 void TargetTransformInfoWrapperPass::anchor() {}
363 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass()
364 : ImmutablePass(ID) {
365 initializeTargetTransformInfoWrapperPassPass(
366 *PassRegistry::getPassRegistry());
369 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass(
370 TargetIRAnalysis TIRA)
371 : ImmutablePass(ID), TIRA(std::move(TIRA)) {
372 initializeTargetTransformInfoWrapperPassPass(
373 *PassRegistry::getPassRegistry());
376 TargetTransformInfo &TargetTransformInfoWrapperPass::getTTI(Function &F) {
382 llvm::createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA) {
383 return new TargetTransformInfoWrapperPass(std::move(TIRA));