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 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" 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) 32 : TargetTransformInfoImplCRTPBase<NoTTIImpl>(DL) {} 33 }; 34 } 35 36 TargetTransformInfo::TargetTransformInfo(const DataLayout *DL) 37 : TTIImpl(new Model<NoTTIImpl>(NoTTIImpl(DL))) {} 38 39 TargetTransformInfo::~TargetTransformInfo() {} 40 41 TargetTransformInfo::TargetTransformInfo(TargetTransformInfo &&Arg) 42 : TTIImpl(std::move(Arg.TTIImpl)) {} 43 44 TargetTransformInfo &TargetTransformInfo::operator=(TargetTransformInfo &&RHS) { 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 54 unsigned TargetTransformInfo::getCallCost(FunctionType *FTy, 55 int NumArgs) const { 56 return TTIImpl->getCallCost(FTy, NumArgs); 57 } 58 59 unsigned 60 TargetTransformInfo::getCallCost(const Function *F, 61 ArrayRef<const Value *> Arguments) const { 62 return TTIImpl->getCallCost(F, Arguments); 63 } 64 65 unsigned 66 TargetTransformInfo::getIntrinsicCost(Intrinsic::ID IID, Type *RetTy, 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 75 bool TargetTransformInfo::hasBranchDivergence() const { 76 return TTIImpl->hasBranchDivergence(); 77 } 78 79 bool TargetTransformInfo::isLoweredToCall(const Function *F) const { 80 return TTIImpl->isLoweredToCall(F); 81 } 82 83 void TargetTransformInfo::getUnrollingPreferences( 84 Loop *L, UnrollingPreferences &UP) const { 85 return TTIImpl->getUnrollingPreferences(L, UP); 86 } 87 88 bool TargetTransformInfo::isLegalAddImmediate(int64_t Imm) const { 89 return TTIImpl->isLegalAddImmediate(Imm); 90 } 91 92 bool TargetTransformInfo::isLegalICmpImmediate(int64_t Imm) const { 93 return TTIImpl->isLegalICmpImmediate(Imm); 94 } 95 96 bool TargetTransformInfo::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, 97 int64_t BaseOffset, 98 bool HasBaseReg, 99 int64_t Scale) const { 100 return TTIImpl->isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg, 101 Scale); 102 } 103 104 bool TargetTransformInfo::isLegalMaskedStore(Type *DataType, 105 int Consecutive) const { 106 return TTIImpl->isLegalMaskedStore(DataType, Consecutive); 107 } 108 109 bool TargetTransformInfo::isLegalMaskedLoad(Type *DataType, 110 int Consecutive) const { 111 return TTIImpl->isLegalMaskedLoad(DataType, Consecutive); 112 } 113 114 int TargetTransformInfo::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV, 115 int64_t BaseOffset, 116 bool HasBaseReg, 117 int64_t Scale) const { 118 return TTIImpl->getScalingFactorCost(Ty, BaseGV, BaseOffset, HasBaseReg, 119 Scale); 120 } 121 122 bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const { 123 return TTIImpl->isTruncateFree(Ty1, Ty2); 124 } 125 126 bool TargetTransformInfo::isProfitableToHoist(Instruction *I) const { 127 return TTIImpl->isProfitableToHoist(I); 128 } 129 130 bool TargetTransformInfo::isTypeLegal(Type *Ty) const { 131 return TTIImpl->isTypeLegal(Ty); 132 } 133 134 unsigned TargetTransformInfo::getJumpBufAlignment() const { 135 return TTIImpl->getJumpBufAlignment(); 136 } 137 138 unsigned TargetTransformInfo::getJumpBufSize() const { 139 return TTIImpl->getJumpBufSize(); 140 } 141 142 bool TargetTransformInfo::shouldBuildLookupTables() const { 143 return TTIImpl->shouldBuildLookupTables(); 144 } 145 146 bool TargetTransformInfo::enableAggressiveInterleaving(bool LoopHasReductions) const { 147 return TTIImpl->enableAggressiveInterleaving(LoopHasReductions); 148 } 149 150 TargetTransformInfo::PopcntSupportKind 151 TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const { 152 return TTIImpl->getPopcntSupport(IntTyWidthInBit); 153 } 154 155 bool TargetTransformInfo::haveFastSqrt(Type *Ty) const { 156 return TTIImpl->haveFastSqrt(Ty); 157 } 158 159 unsigned TargetTransformInfo::getFPOpCost(Type *Ty) const { 160 return TTIImpl->getFPOpCost(Ty); 161 } 162 163 unsigned TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const { 164 return TTIImpl->getIntImmCost(Imm, Ty); 165 } 166 167 unsigned TargetTransformInfo::getIntImmCost(unsigned Opcode, unsigned Idx, 168 const APInt &Imm, Type *Ty) const { 169 return TTIImpl->getIntImmCost(Opcode, Idx, Imm, Ty); 170 } 171 172 unsigned TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, unsigned Idx, 173 const APInt &Imm, Type *Ty) const { 174 return TTIImpl->getIntImmCost(IID, Idx, Imm, Ty); 175 } 176 177 unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const { 178 return TTIImpl->getNumberOfRegisters(Vector); 179 } 180 181 unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const { 182 return TTIImpl->getRegisterBitWidth(Vector); 183 } 184 185 unsigned TargetTransformInfo::getMaxInterleaveFactor() const { 186 return TTIImpl->getMaxInterleaveFactor(); 187 } 188 189 unsigned TargetTransformInfo::getArithmeticInstrCost( 190 unsigned Opcode, Type *Ty, OperandValueKind Opd1Info, 191 OperandValueKind Opd2Info, OperandValueProperties Opd1PropInfo, 192 OperandValueProperties Opd2PropInfo) const { 193 return TTIImpl->getArithmeticInstrCost(Opcode, Ty, Opd1Info, Opd2Info, 194 Opd1PropInfo, Opd2PropInfo); 195 } 196 197 unsigned TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Ty, 198 int Index, Type *SubTp) const { 199 return TTIImpl->getShuffleCost(Kind, Ty, Index, SubTp); 200 } 201 202 unsigned TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst, 203 Type *Src) const { 204 return TTIImpl->getCastInstrCost(Opcode, Dst, Src); 205 } 206 207 unsigned TargetTransformInfo::getCFInstrCost(unsigned Opcode) const { 208 return TTIImpl->getCFInstrCost(Opcode); 209 } 210 211 unsigned TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, 212 Type *CondTy) const { 213 return TTIImpl->getCmpSelInstrCost(Opcode, ValTy, CondTy); 214 } 215 216 unsigned TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val, 217 unsigned Index) const { 218 return TTIImpl->getVectorInstrCost(Opcode, Val, Index); 219 } 220 221 unsigned TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src, 222 unsigned Alignment, 223 unsigned AddressSpace) const { 224 return TTIImpl->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace); 225 } 226 227 unsigned 228 TargetTransformInfo::getMaskedMemoryOpCost(unsigned Opcode, Type *Src, 229 unsigned Alignment, 230 unsigned AddressSpace) const { 231 return TTIImpl->getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace); 232 } 233 234 unsigned 235 TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy, 236 ArrayRef<Type *> Tys) const { 237 return TTIImpl->getIntrinsicInstrCost(ID, RetTy, Tys); 238 } 239 240 unsigned TargetTransformInfo::getCallInstrCost(Function *F, Type *RetTy, 241 ArrayRef<Type *> Tys) const { 242 return TTIImpl->getCallInstrCost(F, RetTy, Tys); 243 } 244 245 unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const { 246 return TTIImpl->getNumberOfParts(Tp); 247 } 248 249 unsigned TargetTransformInfo::getAddressComputationCost(Type *Tp, 250 bool IsComplex) const { 251 return TTIImpl->getAddressComputationCost(Tp, IsComplex); 252 } 253 254 unsigned TargetTransformInfo::getReductionCost(unsigned Opcode, Type *Ty, 255 bool IsPairwiseForm) const { 256 return TTIImpl->getReductionCost(Opcode, Ty, IsPairwiseForm); 257 } 258 259 unsigned 260 TargetTransformInfo::getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) const { 261 return TTIImpl->getCostOfKeepingLiveOverCall(Tys); 262 } 263 264 bool TargetTransformInfo::getTgtMemIntrinsic(IntrinsicInst *Inst, 265 MemIntrinsicInfo &Info) const { 266 return TTIImpl->getTgtMemIntrinsic(Inst, Info); 267 } 268 269 Value *TargetTransformInfo::getOrCreateResultFromMemIntrinsic( 270 IntrinsicInst *Inst, Type *ExpectedType) const { 271 return TTIImpl->getOrCreateResultFromMemIntrinsic(Inst, ExpectedType); 272 } 273 274 TargetTransformInfo::Concept::~Concept() {} 275 276 TargetIRAnalysis::TargetIRAnalysis() : TTICallback(&getDefaultTTI) {} 277 278 TargetIRAnalysis::TargetIRAnalysis( 279 std::function<Result(Function &)> TTICallback) 280 : TTICallback(TTICallback) {} 281 282 TargetIRAnalysis::Result TargetIRAnalysis::run(Function &F) { 283 return TTICallback(F); 284 } 285 286 char TargetIRAnalysis::PassID; 287 288 TargetIRAnalysis::Result TargetIRAnalysis::getDefaultTTI(Function &F) { 289 return Result(&F.getParent()->getDataLayout()); 290 } 291 292 // Register the basic pass. 293 INITIALIZE_PASS(TargetTransformInfoWrapperPass, "tti", 294 "Target Transform Information", false, true) 295 char TargetTransformInfoWrapperPass::ID = 0; 296 297 void TargetTransformInfoWrapperPass::anchor() {} 298 299 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass() 300 : ImmutablePass(ID) { 301 initializeTargetTransformInfoWrapperPassPass( 302 *PassRegistry::getPassRegistry()); 303 } 304 305 TargetTransformInfoWrapperPass::TargetTransformInfoWrapperPass( 306 TargetIRAnalysis TIRA) 307 : ImmutablePass(ID), TIRA(std::move(TIRA)) { 308 initializeTargetTransformInfoWrapperPassPass( 309 *PassRegistry::getPassRegistry()); 310 } 311 312 TargetTransformInfo &TargetTransformInfoWrapperPass::getTTI(Function &F) { 313 TTI = TIRA.run(F); 314 return *TTI; 315 } 316 317 ImmutablePass * 318 llvm::createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA) { 319 return new TargetTransformInfoWrapperPass(std::move(TIRA)); 320 } 321