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) const {
117 return TTIImpl->isLegalMaskedStore(DataType);
120 bool TargetTransformInfo::isLegalMaskedLoad(Type *DataType) const {
121 return TTIImpl->isLegalMaskedLoad(DataType);
124 bool TargetTransformInfo::isLegalMaskedGather(Type *DataType) const {
125 return TTIImpl->isLegalMaskedGather(DataType);
128 bool TargetTransformInfo::isLegalMaskedScatter(Type *DataType) const {
129 return TTIImpl->isLegalMaskedGather(DataType);
132 int TargetTransformInfo::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
136 unsigned AddrSpace) const {
137 int Cost = TTIImpl->getScalingFactorCost(Ty, BaseGV, BaseOffset, HasBaseReg,
139 assert(Cost >= 0 && "TTI should not produce negative costs!");
143 bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const {
144 return TTIImpl->isTruncateFree(Ty1, Ty2);
147 bool TargetTransformInfo::isZExtFree(Type *Ty1, Type *Ty2) const {
148 return TTIImpl->isZExtFree(Ty1, Ty2);
151 bool TargetTransformInfo::isProfitableToHoist(Instruction *I) const {
152 return TTIImpl->isProfitableToHoist(I);
155 bool TargetTransformInfo::isTypeLegal(Type *Ty) const {
156 return TTIImpl->isTypeLegal(Ty);
159 unsigned TargetTransformInfo::getJumpBufAlignment() const {
160 return TTIImpl->getJumpBufAlignment();
163 unsigned TargetTransformInfo::getJumpBufSize() const {
164 return TTIImpl->getJumpBufSize();
167 bool TargetTransformInfo::shouldBuildLookupTables() const {
168 return TTIImpl->shouldBuildLookupTables();
171 bool TargetTransformInfo::enableAggressiveInterleaving(bool LoopHasReductions) const {
172 return TTIImpl->enableAggressiveInterleaving(LoopHasReductions);
175 bool TargetTransformInfo::enableInterleavedAccessVectorization() const {
176 return TTIImpl->enableInterleavedAccessVectorization();
179 TargetTransformInfo::PopcntSupportKind
180 TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const {
181 return TTIImpl->getPopcntSupport(IntTyWidthInBit);
184 bool TargetTransformInfo::haveFastSqrt(Type *Ty) const {
185 return TTIImpl->haveFastSqrt(Ty);
188 int TargetTransformInfo::getFPOpCost(Type *Ty) const {
189 int Cost = TTIImpl->getFPOpCost(Ty);
190 assert(Cost >= 0 && "TTI should not produce negative costs!");
194 int TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const {
195 int Cost = TTIImpl->getIntImmCost(Imm, Ty);
196 assert(Cost >= 0 && "TTI should not produce negative costs!");
200 int TargetTransformInfo::getIntImmCost(unsigned Opcode, unsigned Idx,
201 const APInt &Imm, Type *Ty) const {
202 int Cost = TTIImpl->getIntImmCost(Opcode, Idx, Imm, Ty);
203 assert(Cost >= 0 && "TTI should not produce negative costs!");
207 int TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
208 const APInt &Imm, Type *Ty) const {
209 int Cost = TTIImpl->getIntImmCost(IID, Idx, Imm, Ty);
210 assert(Cost >= 0 && "TTI should not produce negative costs!");
214 unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const {
215 return TTIImpl->getNumberOfRegisters(Vector);
218 unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const {
219 return TTIImpl->getRegisterBitWidth(Vector);
222 unsigned TargetTransformInfo::getMaxInterleaveFactor(unsigned VF) const {
223 return TTIImpl->getMaxInterleaveFactor(VF);
226 int TargetTransformInfo::getArithmeticInstrCost(
227 unsigned Opcode, Type *Ty, OperandValueKind Opd1Info,
228 OperandValueKind Opd2Info, OperandValueProperties Opd1PropInfo,
229 OperandValueProperties Opd2PropInfo) const {
230 int Cost = TTIImpl->getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info,
231 Opd1PropInfo, Opd2PropInfo);
232 assert(Cost >= 0 && "TTI should not produce negative costs!");
236 int TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Ty, int Index,
238 int Cost = TTIImpl->getShuffleCost(Kind, Ty, Index, SubTp);
239 assert(Cost >= 0 && "TTI should not produce negative costs!");
243 int TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst,
245 int Cost = TTIImpl->getCastInstrCost(Opcode, Dst, Src);
246 assert(Cost >= 0 && "TTI should not produce negative costs!");
250 int TargetTransformInfo::getCFInstrCost(unsigned Opcode) const {
251 int Cost = TTIImpl->getCFInstrCost(Opcode);
252 assert(Cost >= 0 && "TTI should not produce negative costs!");
256 int TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
257 Type *CondTy) const {
258 int Cost = TTIImpl->getCmpSelInstrCost(Opcode, ValTy, CondTy);
259 assert(Cost >= 0 && "TTI should not produce negative costs!");
263 int TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val,
264 unsigned Index) const {
265 int Cost = TTIImpl->getVectorInstrCost(Opcode, Val, Index);
266 assert(Cost >= 0 && "TTI should not produce negative costs!");
270 int TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src,
272 unsigned AddressSpace) const {
273 int Cost = TTIImpl->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
274 assert(Cost >= 0 && "TTI should not produce negative costs!");
278 int TargetTransformInfo::getMaskedMemoryOpCost(unsigned Opcode, Type *Src,
280 unsigned AddressSpace) const {
282 TTIImpl->getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace);
283 assert(Cost >= 0 && "TTI should not produce negative costs!");
287 int TargetTransformInfo::getInterleavedMemoryOpCost(
288 unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
289 unsigned Alignment, unsigned AddressSpace) const {
290 int Cost = TTIImpl->getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
291 Alignment, AddressSpace);
292 assert(Cost >= 0 && "TTI should not produce negative costs!");
296 int TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
297 ArrayRef<Type *> Tys) const {
298 int Cost = TTIImpl->getIntrinsicInstrCost(ID, RetTy, Tys);
299 assert(Cost >= 0 && "TTI should not produce negative costs!");
303 int TargetTransformInfo::getCallInstrCost(Function *F, Type *RetTy,
304 ArrayRef<Type *> Tys) const {
305 int Cost = TTIImpl->getCallInstrCost(F, RetTy, Tys);
306 assert(Cost >= 0 && "TTI should not produce negative costs!");
310 unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const {
311 return TTIImpl->getNumberOfParts(Tp);
314 int TargetTransformInfo::getAddressComputationCost(Type *Tp,
315 bool IsComplex) const {
316 int Cost = TTIImpl->getAddressComputationCost(Tp, IsComplex);
317 assert(Cost >= 0 && "TTI should not produce negative costs!");
321 int TargetTransformInfo::getReductionCost(unsigned Opcode, Type *Ty,
322 bool IsPairwiseForm) const {
323 int Cost = TTIImpl->getReductionCost(Opcode, Ty, IsPairwiseForm);
324 assert(Cost >= 0 && "TTI should not produce negative costs!");
329 TargetTransformInfo::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) const {
330 return TTIImpl->getCostOfKeepingLiveOverCall(Tys);
333 bool TargetTransformInfo::getTgtMemIntrinsic(IntrinsicInst *Inst,
334 MemIntrinsicInfo &Info) const {
335 return TTIImpl->getTgtMemIntrinsic(Inst, Info);
338 Value *TargetTransformInfo::getOrCreateResultFromMemIntrinsic(
339 IntrinsicInst *Inst, Type *ExpectedType) const {
340 return TTIImpl->getOrCreateResultFromMemIntrinsic(Inst, ExpectedType);
343 bool TargetTransformInfo::areInlineCompatible(const Function *Caller,
344 const Function *Callee) const {
345 return TTIImpl->areInlineCompatible(Caller, Callee);
348 TargetTransformInfo::Concept::~Concept() {}
350 TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {}
352 TargetIRAnalysis::TargetIRAnalysis(
353 std::function<Result(const Function &)> TTICallback)
354 : TTICallback(TTICallback) {}
356 TargetIRAnalysis::Result TargetIRAnalysis::run(const Function &F) {
357 return TTICallback(F);
360 char TargetIRAnalysis::PassID;
362 TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(const Function &F) {
363 return Result(F.getParent()->getDataLayout());
366 // Register the basic pass.
367 INITIALIZE_PASS(TargetTransformInfoWrapperPass, "tti",
368 "Target Transform Information", false, true)
369 char TargetTransformInfoWrapperPass::ID = 0;
371 void TargetTransformInfoWrapperPass::anchor() {}
373 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass()
374 : ImmutablePass(ID) {
375 initializeTargetTransformInfoWrapperPassPass(
376 *PassRegistry::getPassRegistry());
379 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass(
380 TargetIRAnalysis TIRA)
381 : ImmutablePass(ID), TIRA(std::move(TIRA)) {
382 initializeTargetTransformInfoWrapperPassPass(
383 *PassRegistry::getPassRegistry());
386 TargetTransformInfo &TargetTransformInfoWrapperPass::getTTI(const Function &F) {
392 llvm::createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA) {
393 return new TargetTransformInfoWrapperPass(std::move(TIRA));