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/IR/CallSite.h" 12 #include "llvm/IR/DataLayout.h" 13 #include "llvm/IR/Instruction.h" 14 #include "llvm/IR/Instructions.h" 15 #include "llvm/IR/IntrinsicInst.h" 16 #include "llvm/IR/Operator.h" 17 #include "llvm/Support/ErrorHandling.h" 18 19 using namespace llvm; 20 21 #define DEBUG_TYPE "tti" 22 23 // Setup the analysis group to manage the TargetTransformInfo passes. 24 INITIALIZE_ANALYSIS_GROUP(TargetTransformInfo, "Target Information", NoTTI) 25 char TargetTransformInfo::ID = 0; 26 27 TargetTransformInfo::~TargetTransformInfo() { 28 } 29 30 void TargetTransformInfo::pushTTIStack(Pass *P) { 31 TopTTI = this; 32 PrevTTI = &P->getAnalysis<TargetTransformInfo>(); 33 34 // Walk up the chain and update the top TTI pointer. 35 for (TargetTransformInfo *PTTI = PrevTTI; PTTI; PTTI = PTTI->PrevTTI) 36 PTTI->TopTTI = this; 37 } 38 39 void TargetTransformInfo::getAnalysisUsage(AnalysisUsage &AU) const { 40 AU.addRequired<TargetTransformInfo>(); 41 } 42 43 unsigned TargetTransformInfo::getOperationCost(unsigned Opcode, Type *Ty, 44 Type *OpTy) const { 45 return PrevTTI->getOperationCost(Opcode, Ty, OpTy); 46 } 47 48 unsigned TargetTransformInfo::getGEPCost( 49 const Value *Ptr, ArrayRef<const Value *> Operands) const { 50 return PrevTTI->getGEPCost(Ptr, Operands); 51 } 52 53 unsigned TargetTransformInfo::getCallCost(FunctionType *FTy, 54 int NumArgs) const { 55 return PrevTTI->getCallCost(FTy, NumArgs); 56 } 57 58 unsigned TargetTransformInfo::getCallCost(const Function *F, 59 int NumArgs) const { 60 return PrevTTI->getCallCost(F, NumArgs); 61 } 62 63 unsigned TargetTransformInfo::getCallCost( 64 const Function *F, ArrayRef<const Value *> Arguments) const { 65 return PrevTTI->getCallCost(F, Arguments); 66 } 67 68 unsigned TargetTransformInfo::getIntrinsicCost( 69 Intrinsic::ID IID, Type *RetTy, ArrayRef<Type *> ParamTys) const { 70 return PrevTTI->getIntrinsicCost(IID, RetTy, ParamTys); 71 } 72 73 unsigned TargetTransformInfo::getIntrinsicCost( 74 Intrinsic::ID IID, Type *RetTy, ArrayRef<const Value *> Arguments) const { 75 return PrevTTI->getIntrinsicCost(IID, RetTy, Arguments); 76 } 77 78 unsigned TargetTransformInfo::getUserCost(const User *U) const { 79 return PrevTTI->getUserCost(U); 80 } 81 82 bool TargetTransformInfo::hasBranchDivergence() const { 83 return PrevTTI->hasBranchDivergence(); 84 } 85 86 bool TargetTransformInfo::isLoweredToCall(const Function *F) const { 87 return PrevTTI->isLoweredToCall(F); 88 } 89 90 void TargetTransformInfo::getUnrollingPreferences(Loop *L, 91 UnrollingPreferences &UP) const { 92 PrevTTI->getUnrollingPreferences(L, UP); 93 } 94 95 bool TargetTransformInfo::isLegalAddImmediate(int64_t Imm) const { 96 return PrevTTI->isLegalAddImmediate(Imm); 97 } 98 99 bool TargetTransformInfo::isLegalICmpImmediate(int64_t Imm) const { 100 return PrevTTI->isLegalICmpImmediate(Imm); 101 } 102 103 bool TargetTransformInfo::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, 104 int64_t BaseOffset, 105 bool HasBaseReg, 106 int64_t Scale) const { 107 return PrevTTI->isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg, 108 Scale); 109 } 110 111 int TargetTransformInfo::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV, 112 int64_t BaseOffset, 113 bool HasBaseReg, 114 int64_t Scale) const { 115 return PrevTTI->getScalingFactorCost(Ty, BaseGV, BaseOffset, HasBaseReg, 116 Scale); 117 } 118 119 bool TargetTransformInfo::isTruncateFree(Type *Ty1, Type *Ty2) const { 120 return PrevTTI->isTruncateFree(Ty1, Ty2); 121 } 122 123 bool TargetTransformInfo::isTypeLegal(Type *Ty) const { 124 return PrevTTI->isTypeLegal(Ty); 125 } 126 127 unsigned TargetTransformInfo::getJumpBufAlignment() const { 128 return PrevTTI->getJumpBufAlignment(); 129 } 130 131 unsigned TargetTransformInfo::getJumpBufSize() const { 132 return PrevTTI->getJumpBufSize(); 133 } 134 135 bool TargetTransformInfo::shouldBuildLookupTables() const { 136 return PrevTTI->shouldBuildLookupTables(); 137 } 138 139 TargetTransformInfo::PopcntSupportKind 140 TargetTransformInfo::getPopcntSupport(unsigned IntTyWidthInBit) const { 141 return PrevTTI->getPopcntSupport(IntTyWidthInBit); 142 } 143 144 bool TargetTransformInfo::haveFastSqrt(Type *Ty) const { 145 return PrevTTI->haveFastSqrt(Ty); 146 } 147 148 unsigned TargetTransformInfo::getIntImmCost(const APInt &Imm, Type *Ty) const { 149 return PrevTTI->getIntImmCost(Imm, Ty); 150 } 151 152 unsigned TargetTransformInfo::getIntImmCost(unsigned Opc, unsigned Idx, 153 const APInt &Imm, Type *Ty) const { 154 return PrevTTI->getIntImmCost(Opc, Idx, Imm, Ty); 155 } 156 157 unsigned TargetTransformInfo::getIntImmCost(Intrinsic::ID IID, unsigned Idx, 158 const APInt &Imm, Type *Ty) const { 159 return PrevTTI->getIntImmCost(IID, Idx, Imm, Ty); 160 } 161 162 unsigned TargetTransformInfo::getNumberOfRegisters(bool Vector) const { 163 return PrevTTI->getNumberOfRegisters(Vector); 164 } 165 166 unsigned TargetTransformInfo::getRegisterBitWidth(bool Vector) const { 167 return PrevTTI->getRegisterBitWidth(Vector); 168 } 169 170 unsigned TargetTransformInfo::getMaximumUnrollFactor() const { 171 return PrevTTI->getMaximumUnrollFactor(); 172 } 173 174 unsigned TargetTransformInfo::getArithmeticInstrCost(unsigned Opcode, 175 Type *Ty, 176 OperandValueKind Op1Info, 177 OperandValueKind Op2Info) const { 178 return PrevTTI->getArithmeticInstrCost(Opcode, Ty, Op1Info, Op2Info); 179 } 180 181 unsigned TargetTransformInfo::getShuffleCost(ShuffleKind Kind, Type *Tp, 182 int Index, Type *SubTp) const { 183 return PrevTTI->getShuffleCost(Kind, Tp, Index, SubTp); 184 } 185 186 unsigned TargetTransformInfo::getCastInstrCost(unsigned Opcode, Type *Dst, 187 Type *Src) const { 188 return PrevTTI->getCastInstrCost(Opcode, Dst, Src); 189 } 190 191 unsigned TargetTransformInfo::getCFInstrCost(unsigned Opcode) const { 192 return PrevTTI->getCFInstrCost(Opcode); 193 } 194 195 unsigned TargetTransformInfo::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, 196 Type *CondTy) const { 197 return PrevTTI->getCmpSelInstrCost(Opcode, ValTy, CondTy); 198 } 199 200 unsigned TargetTransformInfo::getVectorInstrCost(unsigned Opcode, Type *Val, 201 unsigned Index) const { 202 return PrevTTI->getVectorInstrCost(Opcode, Val, Index); 203 } 204 205 unsigned TargetTransformInfo::getMemoryOpCost(unsigned Opcode, Type *Src, 206 unsigned Alignment, 207 unsigned AddressSpace) const { 208 return PrevTTI->getMemoryOpCost(Opcode, Src, Alignment, AddressSpace); 209 ; 210 } 211 212 unsigned 213 TargetTransformInfo::getIntrinsicInstrCost(Intrinsic::ID ID, 214 Type *RetTy, 215 ArrayRef<Type *> Tys) const { 216 return PrevTTI->getIntrinsicInstrCost(ID, RetTy, Tys); 217 } 218 219 unsigned TargetTransformInfo::getNumberOfParts(Type *Tp) const { 220 return PrevTTI->getNumberOfParts(Tp); 221 } 222 223 unsigned TargetTransformInfo::getAddressComputationCost(Type *Tp, 224 bool IsComplex) const { 225 return PrevTTI->getAddressComputationCost(Tp, IsComplex); 226 } 227 228 unsigned TargetTransformInfo::getReductionCost(unsigned Opcode, Type *Ty, 229 bool IsPairwise) const { 230 return PrevTTI->getReductionCost(Opcode, Ty, IsPairwise); 231 } 232 233 unsigned TargetTransformInfo::getCostOfKeepingLiveOverCall(ArrayRef<Type*> Tys) 234 const { 235 return PrevTTI->getCostOfKeepingLiveOverCall(Tys); 236 } 237 238 namespace { 239 240 struct NoTTI final : ImmutablePass, TargetTransformInfo { 241 const DataLayout *DL; 242 243 NoTTI() : ImmutablePass(ID), DL(nullptr) { 244 initializeNoTTIPass(*PassRegistry::getPassRegistry()); 245 } 246 247 virtual void initializePass() override { 248 // Note that this subclass is special, and must *not* call initializeTTI as 249 // it does not chain. 250 TopTTI = this; 251 PrevTTI = nullptr; 252 DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>(); 253 DL = DLP ? &DLP->getDataLayout() : nullptr; 254 } 255 256 virtual void getAnalysisUsage(AnalysisUsage &AU) const override { 257 // Note that this subclass is special, and must *not* call 258 // TTI::getAnalysisUsage as it breaks the recursion. 259 } 260 261 /// Pass identification. 262 static char ID; 263 264 /// Provide necessary pointer adjustments for the two base classes. 265 virtual void *getAdjustedAnalysisPointer(const void *ID) override { 266 if (ID == &TargetTransformInfo::ID) 267 return (TargetTransformInfo*)this; 268 return this; 269 } 270 271 unsigned getOperationCost(unsigned Opcode, Type *Ty, 272 Type *OpTy) const override { 273 switch (Opcode) { 274 default: 275 // By default, just classify everything as 'basic'. 276 return TCC_Basic; 277 278 case Instruction::GetElementPtr: 279 llvm_unreachable("Use getGEPCost for GEP operations!"); 280 281 case Instruction::BitCast: 282 assert(OpTy && "Cast instructions must provide the operand type"); 283 if (Ty == OpTy || (Ty->isPointerTy() && OpTy->isPointerTy())) 284 // Identity and pointer-to-pointer casts are free. 285 return TCC_Free; 286 287 // Otherwise, the default basic cost is used. 288 return TCC_Basic; 289 290 case Instruction::IntToPtr: { 291 if (!DL) 292 return TCC_Basic; 293 294 // An inttoptr cast is free so long as the input is a legal integer type 295 // which doesn't contain values outside the range of a pointer. 296 unsigned OpSize = OpTy->getScalarSizeInBits(); 297 if (DL->isLegalInteger(OpSize) && 298 OpSize <= DL->getPointerTypeSizeInBits(Ty)) 299 return TCC_Free; 300 301 // Otherwise it's not a no-op. 302 return TCC_Basic; 303 } 304 case Instruction::PtrToInt: { 305 if (!DL) 306 return TCC_Basic; 307 308 // A ptrtoint cast is free so long as the result is large enough to store 309 // the pointer, and a legal integer type. 310 unsigned DestSize = Ty->getScalarSizeInBits(); 311 if (DL->isLegalInteger(DestSize) && 312 DestSize >= DL->getPointerTypeSizeInBits(OpTy)) 313 return TCC_Free; 314 315 // Otherwise it's not a no-op. 316 return TCC_Basic; 317 } 318 case Instruction::Trunc: 319 // trunc to a native type is free (assuming the target has compare and 320 // shift-right of the same width). 321 if (DL && DL->isLegalInteger(DL->getTypeSizeInBits(Ty))) 322 return TCC_Free; 323 324 return TCC_Basic; 325 } 326 } 327 328 unsigned getGEPCost(const Value *Ptr, 329 ArrayRef<const Value *> Operands) const override { 330 // In the basic model, we just assume that all-constant GEPs will be folded 331 // into their uses via addressing modes. 332 for (unsigned Idx = 0, Size = Operands.size(); Idx != Size; ++Idx) 333 if (!isa<Constant>(Operands[Idx])) 334 return TCC_Basic; 335 336 return TCC_Free; 337 } 338 339 unsigned getCallCost(FunctionType *FTy, int NumArgs = -1) const override 340 { 341 assert(FTy && "FunctionType must be provided to this routine."); 342 343 // The target-independent implementation just measures the size of the 344 // function by approximating that each argument will take on average one 345 // instruction to prepare. 346 347 if (NumArgs < 0) 348 // Set the argument number to the number of explicit arguments in the 349 // function. 350 NumArgs = FTy->getNumParams(); 351 352 return TCC_Basic * (NumArgs + 1); 353 } 354 355 unsigned getCallCost(const Function *F, int NumArgs = -1) const override 356 { 357 assert(F && "A concrete function must be provided to this routine."); 358 359 if (NumArgs < 0) 360 // Set the argument number to the number of explicit arguments in the 361 // function. 362 NumArgs = F->arg_size(); 363 364 if (Intrinsic::ID IID = (Intrinsic::ID)F->getIntrinsicID()) { 365 FunctionType *FTy = F->getFunctionType(); 366 SmallVector<Type *, 8> ParamTys(FTy->param_begin(), FTy->param_end()); 367 return TopTTI->getIntrinsicCost(IID, FTy->getReturnType(), ParamTys); 368 } 369 370 if (!TopTTI->isLoweredToCall(F)) 371 return TCC_Basic; // Give a basic cost if it will be lowered directly. 372 373 return TopTTI->getCallCost(F->getFunctionType(), NumArgs); 374 } 375 376 unsigned getCallCost(const Function *F, 377 ArrayRef<const Value *> Arguments) const override { 378 // Simply delegate to generic handling of the call. 379 // FIXME: We should use instsimplify or something else to catch calls which 380 // will constant fold with these arguments. 381 return TopTTI->getCallCost(F, Arguments.size()); 382 } 383 384 unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy, 385 ArrayRef<Type *> ParamTys) const override { 386 switch (IID) { 387 default: 388 // Intrinsics rarely (if ever) have normal argument setup constraints. 389 // Model them as having a basic instruction cost. 390 // FIXME: This is wrong for libc intrinsics. 391 return TCC_Basic; 392 393 case Intrinsic::assume: 394 case Intrinsic::dbg_declare: 395 case Intrinsic::dbg_value: 396 case Intrinsic::invariant_start: 397 case Intrinsic::invariant_end: 398 case Intrinsic::lifetime_start: 399 case Intrinsic::lifetime_end: 400 case Intrinsic::objectsize: 401 case Intrinsic::ptr_annotation: 402 case Intrinsic::var_annotation: 403 // These intrinsics don't actually represent code after lowering. 404 return TCC_Free; 405 } 406 } 407 408 unsigned 409 getIntrinsicCost(Intrinsic::ID IID, Type *RetTy, 410 ArrayRef<const Value *> Arguments) const override { 411 // Delegate to the generic intrinsic handling code. This mostly provides an 412 // opportunity for targets to (for example) special case the cost of 413 // certain intrinsics based on constants used as arguments. 414 SmallVector<Type *, 8> ParamTys; 415 ParamTys.reserve(Arguments.size()); 416 for (unsigned Idx = 0, Size = Arguments.size(); Idx != Size; ++Idx) 417 ParamTys.push_back(Arguments[Idx]->getType()); 418 return TopTTI->getIntrinsicCost(IID, RetTy, ParamTys); 419 } 420 421 unsigned getUserCost(const User *U) const override { 422 if (isa<PHINode>(U)) 423 return TCC_Free; // Model all PHI nodes as free. 424 425 if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) { 426 SmallVector<const Value *, 4> Indices(GEP->idx_begin(), GEP->idx_end()); 427 return TopTTI->getGEPCost(GEP->getPointerOperand(), Indices); 428 } 429 430 if (ImmutableCallSite CS = U) { 431 const Function *F = CS.getCalledFunction(); 432 if (!F) { 433 // Just use the called value type. 434 Type *FTy = CS.getCalledValue()->getType()->getPointerElementType(); 435 return TopTTI->getCallCost(cast<FunctionType>(FTy), CS.arg_size()); 436 } 437 438 SmallVector<const Value *, 8> Arguments(CS.arg_begin(), CS.arg_end()); 439 return TopTTI->getCallCost(F, Arguments); 440 } 441 442 if (const CastInst *CI = dyn_cast<CastInst>(U)) { 443 // Result of a cmp instruction is often extended (to be used by other 444 // cmp instructions, logical or return instructions). These are usually 445 // nop on most sane targets. 446 if (isa<CmpInst>(CI->getOperand(0))) 447 return TCC_Free; 448 } 449 450 // Otherwise delegate to the fully generic implementations. 451 return getOperationCost(Operator::getOpcode(U), U->getType(), 452 U->getNumOperands() == 1 ? 453 U->getOperand(0)->getType() : nullptr); 454 } 455 456 bool hasBranchDivergence() const override { return false; } 457 458 bool isLoweredToCall(const Function *F) const override { 459 // FIXME: These should almost certainly not be handled here, and instead 460 // handled with the help of TLI or the target itself. This was largely 461 // ported from existing analysis heuristics here so that such refactorings 462 // can take place in the future. 463 464 if (F->isIntrinsic()) 465 return false; 466 467 if (F->hasLocalLinkage() || !F->hasName()) 468 return true; 469 470 StringRef Name = F->getName(); 471 472 // These will all likely lower to a single selection DAG node. 473 if (Name == "copysign" || Name == "copysignf" || Name == "copysignl" || 474 Name == "fabs" || Name == "fabsf" || Name == "fabsl" || Name == "sin" || 475 Name == "sinf" || Name == "sinl" || Name == "cos" || Name == "cosf" || 476 Name == "cosl" || Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") 477 return false; 478 479 // These are all likely to be optimized into something smaller. 480 if (Name == "pow" || Name == "powf" || Name == "powl" || Name == "exp2" || 481 Name == "exp2l" || Name == "exp2f" || Name == "floor" || Name == 482 "floorf" || Name == "ceil" || Name == "round" || Name == "ffs" || 483 Name == "ffsl" || Name == "abs" || Name == "labs" || Name == "llabs") 484 return false; 485 486 return true; 487 } 488 489 void getUnrollingPreferences(Loop *, UnrollingPreferences &) const override { 490 } 491 492 bool isLegalAddImmediate(int64_t Imm) const override { 493 return false; 494 } 495 496 bool isLegalICmpImmediate(int64_t Imm) const override { 497 return false; 498 } 499 500 bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset, 501 bool HasBaseReg, int64_t Scale) const override 502 { 503 // Guess that reg+reg addressing is allowed. This heuristic is taken from 504 // the implementation of LSR. 505 return !BaseGV && BaseOffset == 0 && Scale <= 1; 506 } 507 508 int getScalingFactorCost(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset, 509 bool HasBaseReg, int64_t Scale) const override { 510 // Guess that all legal addressing mode are free. 511 if(isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg, Scale)) 512 return 0; 513 return -1; 514 } 515 516 bool isTruncateFree(Type *Ty1, Type *Ty2) const override { 517 return false; 518 } 519 520 bool isTypeLegal(Type *Ty) const override { 521 return false; 522 } 523 524 unsigned getJumpBufAlignment() const override { 525 return 0; 526 } 527 528 unsigned getJumpBufSize() const override { 529 return 0; 530 } 531 532 bool shouldBuildLookupTables() const override { 533 return true; 534 } 535 536 PopcntSupportKind 537 getPopcntSupport(unsigned IntTyWidthInBit) const override { 538 return PSK_Software; 539 } 540 541 bool haveFastSqrt(Type *Ty) const override { 542 return false; 543 } 544 545 unsigned getIntImmCost(const APInt &Imm, Type *Ty) const override { 546 return TCC_Basic; 547 } 548 549 unsigned getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm, 550 Type *Ty) const override { 551 return TCC_Free; 552 } 553 554 unsigned getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm, 555 Type *Ty) const override { 556 return TCC_Free; 557 } 558 559 unsigned getNumberOfRegisters(bool Vector) const override { 560 return 8; 561 } 562 563 unsigned getRegisterBitWidth(bool Vector) const override { 564 return 32; 565 } 566 567 unsigned getMaximumUnrollFactor() const override { 568 return 1; 569 } 570 571 unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty, OperandValueKind, 572 OperandValueKind) const override { 573 return 1; 574 } 575 576 unsigned getShuffleCost(ShuffleKind Kind, Type *Ty, 577 int Index = 0, Type *SubTp = nullptr) const override { 578 return 1; 579 } 580 581 unsigned getCastInstrCost(unsigned Opcode, Type *Dst, 582 Type *Src) const override { 583 return 1; 584 } 585 586 unsigned getCFInstrCost(unsigned Opcode) const override { 587 return 1; 588 } 589 590 unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy, 591 Type *CondTy = nullptr) const override { 592 return 1; 593 } 594 595 unsigned getVectorInstrCost(unsigned Opcode, Type *Val, 596 unsigned Index = -1) const override { 597 return 1; 598 } 599 600 unsigned getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment, 601 unsigned AddressSpace) const override { 602 return 1; 603 } 604 605 unsigned getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy, 606 ArrayRef<Type*> Tys) const override { 607 return 1; 608 } 609 610 unsigned getNumberOfParts(Type *Tp) const override { 611 return 0; 612 } 613 614 unsigned getAddressComputationCost(Type *Tp, bool) const override { 615 return 0; 616 } 617 618 unsigned getReductionCost(unsigned, Type *, bool) const override { 619 return 1; 620 } 621 622 unsigned getCostOfKeepingLiveOverCall(ArrayRef<Type*> Tys) const override { 623 return 0; 624 } 625 626 }; 627 628 } // end anonymous namespace 629 630 INITIALIZE_AG_PASS(NoTTI, TargetTransformInfo, "notti", 631 "No target information", true, true, true) 632 char NoTTI::ID = 0; 633 634 ImmutablePass *llvm::createNoTargetTransformInfoPass() { 635 return new NoTTI(); 636 } 637