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