BasicTargetTransformInfo.cpp revision 9eb366acba65b5779d2129db3a6fb6a0414572d4
1//===- BasicTargetTransformInfo.cpp - Basic target-independent TTI impl ---===// 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 the implementation of a basic TargetTransformInfo pass 11/// predicated on the target abstractions present in the target independent 12/// code generator. It uses these (primarily TargetLowering) to model as much 13/// of the TTI query interface as possible. It is included by most targets so 14/// that they can specialize only a small subset of the query space. 15/// 16//===----------------------------------------------------------------------===// 17 18#define DEBUG_TYPE "basictti" 19#include "llvm/CodeGen/Passes.h" 20#include "llvm/Analysis/TargetTransformInfo.h" 21#include "llvm/Target/TargetLowering.h" 22#include <utility> 23 24using namespace llvm; 25 26namespace { 27 28class BasicTTI : public ImmutablePass, public TargetTransformInfo { 29 const TargetLoweringBase *TLI; 30 31 /// Estimate the overhead of scalarizing an instruction. Insert and Extract 32 /// are set if the result needs to be inserted and/or extracted from vectors. 33 unsigned getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const; 34 35public: 36 BasicTTI() : ImmutablePass(ID), TLI(0) { 37 llvm_unreachable("This pass cannot be directly constructed"); 38 } 39 40 BasicTTI(const TargetLoweringBase *TLI) : ImmutablePass(ID), TLI(TLI) { 41 initializeBasicTTIPass(*PassRegistry::getPassRegistry()); 42 } 43 44 virtual void initializePass() { 45 pushTTIStack(this); 46 } 47 48 virtual void finalizePass() { 49 popTTIStack(); 50 } 51 52 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 53 TargetTransformInfo::getAnalysisUsage(AU); 54 } 55 56 /// Pass identification. 57 static char ID; 58 59 /// Provide necessary pointer adjustments for the two base classes. 60 virtual void *getAdjustedAnalysisPointer(const void *ID) { 61 if (ID == &TargetTransformInfo::ID) 62 return (TargetTransformInfo*)this; 63 return this; 64 } 65 66 /// \name Scalar TTI Implementations 67 /// @{ 68 69 virtual bool isLegalAddImmediate(int64_t imm) const; 70 virtual bool isLegalICmpImmediate(int64_t imm) const; 71 virtual bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, 72 int64_t BaseOffset, bool HasBaseReg, 73 int64_t Scale) const; 74 virtual bool isTruncateFree(Type *Ty1, Type *Ty2) const; 75 virtual bool isTypeLegal(Type *Ty) const; 76 virtual unsigned getJumpBufAlignment() const; 77 virtual unsigned getJumpBufSize() const; 78 virtual bool shouldBuildLookupTables() const; 79 80 /// @} 81 82 /// \name Vector TTI Implementations 83 /// @{ 84 85 virtual unsigned getNumberOfRegisters(bool Vector) const; 86 virtual unsigned getMaximumUnrollFactor() const; 87 virtual unsigned getRegisterBitWidth(bool Vector) const; 88 virtual unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty, 89 OperandValueKind, 90 OperandValueKind) const; 91 virtual unsigned getShuffleCost(ShuffleKind Kind, Type *Tp, 92 int Index, Type *SubTp) const; 93 virtual unsigned getCastInstrCost(unsigned Opcode, Type *Dst, 94 Type *Src) const; 95 virtual unsigned getCFInstrCost(unsigned Opcode) const; 96 virtual unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy, 97 Type *CondTy) const; 98 virtual unsigned getVectorInstrCost(unsigned Opcode, Type *Val, 99 unsigned Index) const; 100 virtual unsigned getMemoryOpCost(unsigned Opcode, Type *Src, 101 unsigned Alignment, 102 unsigned AddressSpace) const; 103 virtual unsigned getIntrinsicInstrCost(Intrinsic::ID, Type *RetTy, 104 ArrayRef<Type*> Tys) const; 105 virtual unsigned getNumberOfParts(Type *Tp) const; 106 virtual unsigned getAddressComputationCost(Type *Ty) const; 107 108 /// @} 109}; 110 111} 112 113INITIALIZE_AG_PASS(BasicTTI, TargetTransformInfo, "basictti", 114 "Target independent code generator's TTI", true, true, false) 115char BasicTTI::ID = 0; 116 117ImmutablePass * 118llvm::createBasicTargetTransformInfoPass(const TargetLoweringBase *TLI) { 119 return new BasicTTI(TLI); 120} 121 122 123bool BasicTTI::isLegalAddImmediate(int64_t imm) const { 124 return TLI->isLegalAddImmediate(imm); 125} 126 127bool BasicTTI::isLegalICmpImmediate(int64_t imm) const { 128 return TLI->isLegalICmpImmediate(imm); 129} 130 131bool BasicTTI::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, 132 int64_t BaseOffset, bool HasBaseReg, 133 int64_t Scale) const { 134 TargetLoweringBase::AddrMode AM; 135 AM.BaseGV = BaseGV; 136 AM.BaseOffs = BaseOffset; 137 AM.HasBaseReg = HasBaseReg; 138 AM.Scale = Scale; 139 return TLI->isLegalAddressingMode(AM, Ty); 140} 141 142bool BasicTTI::isTruncateFree(Type *Ty1, Type *Ty2) const { 143 return TLI->isTruncateFree(Ty1, Ty2); 144} 145 146bool BasicTTI::isTypeLegal(Type *Ty) const { 147 EVT T = TLI->getValueType(Ty); 148 return TLI->isTypeLegal(T); 149} 150 151unsigned BasicTTI::getJumpBufAlignment() const { 152 return TLI->getJumpBufAlignment(); 153} 154 155unsigned BasicTTI::getJumpBufSize() const { 156 return TLI->getJumpBufSize(); 157} 158 159bool BasicTTI::shouldBuildLookupTables() const { 160 return TLI->supportJumpTables() && 161 (TLI->isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) || 162 TLI->isOperationLegalOrCustom(ISD::BRIND, MVT::Other)); 163} 164 165//===----------------------------------------------------------------------===// 166// 167// Calls used by the vectorizers. 168// 169//===----------------------------------------------------------------------===// 170 171unsigned BasicTTI::getScalarizationOverhead(Type *Ty, bool Insert, 172 bool Extract) const { 173 assert (Ty->isVectorTy() && "Can only scalarize vectors"); 174 unsigned Cost = 0; 175 176 for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) { 177 if (Insert) 178 Cost += TopTTI->getVectorInstrCost(Instruction::InsertElement, Ty, i); 179 if (Extract) 180 Cost += TopTTI->getVectorInstrCost(Instruction::ExtractElement, Ty, i); 181 } 182 183 return Cost; 184} 185 186unsigned BasicTTI::getNumberOfRegisters(bool Vector) const { 187 return 1; 188} 189 190unsigned BasicTTI::getRegisterBitWidth(bool Vector) const { 191 return 32; 192} 193 194unsigned BasicTTI::getMaximumUnrollFactor() const { 195 return 1; 196} 197 198unsigned BasicTTI::getArithmeticInstrCost(unsigned Opcode, Type *Ty, 199 OperandValueKind, 200 OperandValueKind) const { 201 // Check if any of the operands are vector operands. 202 int ISD = TLI->InstructionOpcodeToISD(Opcode); 203 assert(ISD && "Invalid opcode"); 204 205 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty); 206 207 bool IsFloat = Ty->getScalarType()->isFloatingPointTy(); 208 unsigned OpCost = (IsFloat ? 2 : 1); 209 210 if (TLI->isOperationLegalOrPromote(ISD, LT.second)) { 211 // The operation is legal. Assume it costs 1. 212 // If the type is split to multiple registers, assume that thre is some 213 // overhead to this. 214 // TODO: Once we have extract/insert subvector cost we need to use them. 215 if (LT.first > 1) 216 return LT.first * 2 * OpCost; 217 return LT.first * 1 * OpCost; 218 } 219 220 if (!TLI->isOperationExpand(ISD, LT.second)) { 221 // If the operation is custom lowered then assume 222 // thare the code is twice as expensive. 223 return LT.first * 2 * OpCost; 224 } 225 226 // Else, assume that we need to scalarize this op. 227 if (Ty->isVectorTy()) { 228 unsigned Num = Ty->getVectorNumElements(); 229 unsigned Cost = TopTTI->getArithmeticInstrCost(Opcode, Ty->getScalarType()); 230 // return the cost of multiple scalar invocation plus the cost of inserting 231 // and extracting the values. 232 return getScalarizationOverhead(Ty, true, true) + Num * Cost; 233 } 234 235 // We don't know anything about this scalar instruction. 236 return OpCost; 237} 238 239unsigned BasicTTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index, 240 Type *SubTp) const { 241 return 1; 242} 243 244unsigned BasicTTI::getCastInstrCost(unsigned Opcode, Type *Dst, 245 Type *Src) const { 246 int ISD = TLI->InstructionOpcodeToISD(Opcode); 247 assert(ISD && "Invalid opcode"); 248 249 std::pair<unsigned, MVT> SrcLT = TLI->getTypeLegalizationCost(Src); 250 std::pair<unsigned, MVT> DstLT = TLI->getTypeLegalizationCost(Dst); 251 252 // Check for NOOP conversions. 253 if (SrcLT.first == DstLT.first && 254 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) { 255 256 // Bitcast between types that are legalized to the same type are free. 257 if (Opcode == Instruction::BitCast || Opcode == Instruction::Trunc) 258 return 0; 259 } 260 261 if (Opcode == Instruction::Trunc && 262 TLI->isTruncateFree(SrcLT.second, DstLT.second)) 263 return 0; 264 265 if (Opcode == Instruction::ZExt && 266 TLI->isZExtFree(SrcLT.second, DstLT.second)) 267 return 0; 268 269 // If the cast is marked as legal (or promote) then assume low cost. 270 if (TLI->isOperationLegalOrPromote(ISD, DstLT.second)) 271 return 1; 272 273 // Handle scalar conversions. 274 if (!Src->isVectorTy() && !Dst->isVectorTy()) { 275 276 // Scalar bitcasts are usually free. 277 if (Opcode == Instruction::BitCast) 278 return 0; 279 280 // Just check the op cost. If the operation is legal then assume it costs 1. 281 if (!TLI->isOperationExpand(ISD, DstLT.second)) 282 return 1; 283 284 // Assume that illegal scalar instruction are expensive. 285 return 4; 286 } 287 288 // Check vector-to-vector casts. 289 if (Dst->isVectorTy() && Src->isVectorTy()) { 290 291 // If the cast is between same-sized registers, then the check is simple. 292 if (SrcLT.first == DstLT.first && 293 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) { 294 295 // Assume that Zext is done using AND. 296 if (Opcode == Instruction::ZExt) 297 return 1; 298 299 // Assume that sext is done using SHL and SRA. 300 if (Opcode == Instruction::SExt) 301 return 2; 302 303 // Just check the op cost. If the operation is legal then assume it costs 304 // 1 and multiply by the type-legalization overhead. 305 if (!TLI->isOperationExpand(ISD, DstLT.second)) 306 return SrcLT.first * 1; 307 } 308 309 // If we are converting vectors and the operation is illegal, or 310 // if the vectors are legalized to different types, estimate the 311 // scalarization costs. 312 unsigned Num = Dst->getVectorNumElements(); 313 unsigned Cost = TopTTI->getCastInstrCost(Opcode, Dst->getScalarType(), 314 Src->getScalarType()); 315 316 // Return the cost of multiple scalar invocation plus the cost of 317 // inserting and extracting the values. 318 return getScalarizationOverhead(Dst, true, true) + Num * Cost; 319 } 320 321 // We already handled vector-to-vector and scalar-to-scalar conversions. This 322 // is where we handle bitcast between vectors and scalars. We need to assume 323 // that the conversion is scalarized in one way or another. 324 if (Opcode == Instruction::BitCast) 325 // Illegal bitcasts are done by storing and loading from a stack slot. 326 return (Src->isVectorTy()? getScalarizationOverhead(Src, false, true):0) + 327 (Dst->isVectorTy()? getScalarizationOverhead(Dst, true, false):0); 328 329 llvm_unreachable("Unhandled cast"); 330 } 331 332unsigned BasicTTI::getCFInstrCost(unsigned Opcode) const { 333 // Branches are assumed to be predicted. 334 return 0; 335} 336 337unsigned BasicTTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, 338 Type *CondTy) const { 339 int ISD = TLI->InstructionOpcodeToISD(Opcode); 340 assert(ISD && "Invalid opcode"); 341 342 // Selects on vectors are actually vector selects. 343 if (ISD == ISD::SELECT) { 344 assert(CondTy && "CondTy must exist"); 345 if (CondTy->isVectorTy()) 346 ISD = ISD::VSELECT; 347 } 348 349 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy); 350 351 if (!TLI->isOperationExpand(ISD, LT.second)) { 352 // The operation is legal. Assume it costs 1. Multiply 353 // by the type-legalization overhead. 354 return LT.first * 1; 355 } 356 357 // Otherwise, assume that the cast is scalarized. 358 if (ValTy->isVectorTy()) { 359 unsigned Num = ValTy->getVectorNumElements(); 360 if (CondTy) 361 CondTy = CondTy->getScalarType(); 362 unsigned Cost = TopTTI->getCmpSelInstrCost(Opcode, ValTy->getScalarType(), 363 CondTy); 364 365 // Return the cost of multiple scalar invocation plus the cost of inserting 366 // and extracting the values. 367 return getScalarizationOverhead(ValTy, true, false) + Num * Cost; 368 } 369 370 // Unknown scalar opcode. 371 return 1; 372} 373 374unsigned BasicTTI::getVectorInstrCost(unsigned Opcode, Type *Val, 375 unsigned Index) const { 376 return 1; 377} 378 379unsigned BasicTTI::getMemoryOpCost(unsigned Opcode, Type *Src, 380 unsigned Alignment, 381 unsigned AddressSpace) const { 382 assert(!Src->isVoidTy() && "Invalid type"); 383 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Src); 384 385 // Assume that all loads of legal types cost 1. 386 return LT.first; 387} 388 389unsigned BasicTTI::getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy, 390 ArrayRef<Type *> Tys) const { 391 unsigned ISD = 0; 392 switch (IID) { 393 default: { 394 // Assume that we need to scalarize this intrinsic. 395 unsigned ScalarizationCost = 0; 396 unsigned ScalarCalls = 1; 397 if (RetTy->isVectorTy()) { 398 ScalarizationCost = getScalarizationOverhead(RetTy, true, false); 399 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements()); 400 } 401 for (unsigned i = 0, ie = Tys.size(); i != ie; ++i) { 402 if (Tys[i]->isVectorTy()) { 403 ScalarizationCost += getScalarizationOverhead(Tys[i], false, true); 404 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements()); 405 } 406 } 407 408 return ScalarCalls + ScalarizationCost; 409 } 410 // Look for intrinsics that can be lowered directly or turned into a scalar 411 // intrinsic call. 412 case Intrinsic::sqrt: ISD = ISD::FSQRT; break; 413 case Intrinsic::sin: ISD = ISD::FSIN; break; 414 case Intrinsic::cos: ISD = ISD::FCOS; break; 415 case Intrinsic::exp: ISD = ISD::FEXP; break; 416 case Intrinsic::exp2: ISD = ISD::FEXP2; break; 417 case Intrinsic::log: ISD = ISD::FLOG; break; 418 case Intrinsic::log10: ISD = ISD::FLOG10; break; 419 case Intrinsic::log2: ISD = ISD::FLOG2; break; 420 case Intrinsic::fabs: ISD = ISD::FABS; break; 421 case Intrinsic::floor: ISD = ISD::FFLOOR; break; 422 case Intrinsic::ceil: ISD = ISD::FCEIL; break; 423 case Intrinsic::trunc: ISD = ISD::FTRUNC; break; 424 case Intrinsic::rint: ISD = ISD::FRINT; break; 425 case Intrinsic::pow: ISD = ISD::FPOW; break; 426 case Intrinsic::fma: ISD = ISD::FMA; break; 427 case Intrinsic::fmuladd: ISD = ISD::FMA; break; // FIXME: mul + add? 428 } 429 430 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(RetTy); 431 432 if (TLI->isOperationLegalOrPromote(ISD, LT.second)) { 433 // The operation is legal. Assume it costs 1. 434 // If the type is split to multiple registers, assume that thre is some 435 // overhead to this. 436 // TODO: Once we have extract/insert subvector cost we need to use them. 437 if (LT.first > 1) 438 return LT.first * 2; 439 return LT.first * 1; 440 } 441 442 if (!TLI->isOperationExpand(ISD, LT.second)) { 443 // If the operation is custom lowered then assume 444 // thare the code is twice as expensive. 445 return LT.first * 2; 446 } 447 448 // Else, assume that we need to scalarize this intrinsic. For math builtins 449 // this will emit a costly libcall, adding call overhead and spills. Make it 450 // very expensive. 451 if (RetTy->isVectorTy()) { 452 unsigned Num = RetTy->getVectorNumElements(); 453 unsigned Cost = TopTTI->getIntrinsicInstrCost(IID, RetTy->getScalarType(), 454 Tys); 455 return 10 * Cost * Num; 456 } 457 458 // This is going to be turned into a library call, make it expensive. 459 return 10; 460} 461 462unsigned BasicTTI::getNumberOfParts(Type *Tp) const { 463 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Tp); 464 return LT.first; 465} 466 467unsigned BasicTTI::getAddressComputationCost(Type *Ty) const { 468 return 0; 469} 470