BasicTargetTransformInfo.cpp revision 36b56886974eae4f9c5ebc96befd3e7bfe5de338
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===- BasicTargetTransformInfo.cpp - Basic target-independent TTI impl ---===// 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The LLVM Compiler Infrastructure 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file is distributed under the University of Illinois Open Source 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// License. See LICENSE.TXT for details. 76e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// 86e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)//===----------------------------------------------------------------------===// 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// \file 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// This file provides the implementation of a basic TargetTransformInfo pass 111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci/// predicated on the target abstractions present in the target independent 12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)/// code generator. It uses these (primarily TargetLowering) to model as much 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// of the TTI query interface as possible. It is included by most targets so 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// that they can specialize only a small subset of the query space. 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===// 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DEBUG_TYPE "basictti" 191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "llvm/CodeGen/Passes.h" 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Analysis/TargetTransformInfo.h" 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/Target/TargetLowering.h" 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <utility> 2303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)using namespace llvm; 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace { 26eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class BasicTTI final : public ImmutablePass, public TargetTransformInfo { 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const TargetMachine *TM; 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// Estimate the overhead of scalarizing an instruction. Insert and Extract 31eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch /// are set if the result needs to be inserted and/or extracted from vectors. 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const; 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const TargetLoweringBase *getTLI() const { return TM->getTargetLowering(); } 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)public: 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) BasicTTI() : ImmutablePass(ID), TM(0) { 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) llvm_unreachable("This pass cannot be directly constructed"); 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) BasicTTI(const TargetMachine *TM) : ImmutablePass(ID), TM(TM) { 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) initializeBasicTTIPass(*PassRegistry::getPassRegistry()); 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void initializePass() override { 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) pushTTIStack(this); 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void getAnalysisUsage(AnalysisUsage &AU) const override { 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) TargetTransformInfo::getAnalysisUsage(AU); 516e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) } 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) /// Pass identification. 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static char ID; 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// Provide necessary pointer adjustments for the two base classes. 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void *getAdjustedAnalysisPointer(const void *ID) override { 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (ID == &TargetTransformInfo::ID) 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return (TargetTransformInfo*)this; 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return this; 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool hasBranchDivergence() const override; 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// \name Scalar TTI Implementations 666e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) /// @{ 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool isLegalAddImmediate(int64_t imm) const override; 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool isLegalICmpImmediate(int64_t imm) const override; 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int64_t BaseOffset, bool HasBaseReg, 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int64_t Scale) const override; 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int getScalingFactorCost(Type *Ty, GlobalValue *BaseGV, 746e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) int64_t BaseOffset, bool HasBaseReg, 7503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) int64_t Scale) const override; 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool isTruncateFree(Type *Ty1, Type *Ty2) const override; 7703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) bool isTypeLegal(Type *Ty) const override; 7803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) unsigned getJumpBufAlignment() const override; 7903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) unsigned getJumpBufSize() const override; 8003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) bool shouldBuildLookupTables() const override; 81eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch bool haveFastSqrt(Type *Ty) const override; 82eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch void getUnrollingPreferences(Loop *L, 83eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch UnrollingPreferences &UP) const override; 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// @} 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) /// \name Vector TTI Implementations 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// @{ 895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned getNumberOfRegisters(bool Vector) const override; 912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) unsigned getMaximumUnrollFactor() const override; 927d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) unsigned getRegisterBitWidth(bool Vector) const override; 935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty, OperandValueKind, 94a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) OperandValueKind) const override; 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned getShuffleCost(ShuffleKind Kind, Type *Tp, 96eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch int Index, Type *SubTp) const override; 975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) unsigned getCastInstrCost(unsigned Opcode, Type *Dst, 985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) Type *Src) const override; 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned getCFInstrCost(unsigned Opcode) const override; 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy, 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Type *CondTy) const override; 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned getVectorInstrCost(unsigned Opcode, Type *Val, 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned Index) const override; 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment, 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned AddressSpace) const override; 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned getIntrinsicInstrCost(Intrinsic::ID, Type *RetTy, 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ArrayRef<Type*> Tys) const override; 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned getNumberOfParts(Type *Tp) const override; 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned getAddressComputationCost( Type *Ty, bool IsComplex) const override; 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned getReductionCost(unsigned Opcode, Type *Ty, 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool IsPairwise) const override; 1126e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// @} 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)INITIALIZE_AG_PASS(BasicTTI, TargetTransformInfo, "basictti", 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "Target independent code generator's TTI", true, true, false) 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)char BasicTTI::ID = 0; 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ImmutablePass * 1236e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)llvm::createBasicTargetTransformInfoPass(const TargetMachine *TM) { 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return new BasicTTI(TM); 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool BasicTTI::hasBranchDivergence() const { return false; } 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool BasicTTI::isLegalAddImmediate(int64_t imm) const { 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return getTLI()->isLegalAddImmediate(imm); 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool BasicTTI::isLegalICmpImmediate(int64_t imm) const { 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return getTLI()->isLegalICmpImmediate(imm); 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool BasicTTI::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int64_t BaseOffset, bool HasBaseReg, 1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int64_t Scale) const { 1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) TargetLoweringBase::AddrMode AM; 1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AM.BaseGV = BaseGV; 1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AM.BaseOffs = BaseOffset; 1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AM.HasBaseReg = HasBaseReg; 1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AM.Scale = Scale; 1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return getTLI()->isLegalAddressingMode(AM, Ty); 1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int BasicTTI::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV, 1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int64_t BaseOffset, bool HasBaseReg, 1501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci int64_t Scale) const { 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) TargetLoweringBase::AddrMode AM; 1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AM.BaseGV = BaseGV; 1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AM.BaseOffs = BaseOffset; 1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AM.HasBaseReg = HasBaseReg; 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AM.Scale = Scale; 1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return getTLI()->getScalingFactorCost(AM, Ty); 1576e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)} 1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool BasicTTI::isTruncateFree(Type *Ty1, Type *Ty2) const { 1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return getTLI()->isTruncateFree(Ty1, Ty2); 1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool BasicTTI::isTypeLegal(Type *Ty) const { 1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EVT T = getTLI()->getValueType(Ty); 1656e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) return getTLI()->isTypeLegal(T); 1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)unsigned BasicTTI::getJumpBufAlignment() const { 1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return getTLI()->getJumpBufAlignment(); 1706e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)} 1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)unsigned BasicTTI::getJumpBufSize() const { 1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return getTLI()->getJumpBufSize(); 1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1756e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool BasicTTI::shouldBuildLookupTables() const { 1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const TargetLoweringBase *TLI = getTLI(); 1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return TLI->supportJumpTables() && 1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) (TLI->isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) || 1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) TLI->isOperationLegalOrCustom(ISD::BRIND, MVT::Other)); 1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool BasicTTI::haveFastSqrt(Type *Ty) const { 1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const TargetLoweringBase *TLI = getTLI(); 1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EVT VT = TLI->getValueType(Ty); 1866e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) return TLI->isTypeLegal(VT) && TLI->isOperationLegalOrCustom(ISD::FSQRT, VT); 1875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void BasicTTI::getUnrollingPreferences(Loop *, UnrollingPreferences &) const { } 1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===// 1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 193eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Calls used by the vectorizers. 194eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// 1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===// 1965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 197eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochunsigned BasicTTI::getScalarizationOverhead(Type *Ty, bool Insert, 1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool Extract) const { 1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) assert (Ty->isVectorTy() && "Can only scalarize vectors"); 2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) unsigned Cost = 0; 2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) { 2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (Insert) 2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Cost += TopTTI->getVectorInstrCost(Instruction::InsertElement, Ty, i); 2055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (Extract) 2065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) Cost += TopTTI->getVectorInstrCost(Instruction::ExtractElement, Ty, i); 2075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 2085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 2095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return Cost; 2105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 2115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 21203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)unsigned BasicTTI::getNumberOfRegisters(bool Vector) const { 2135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return 1; 2145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 2155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 2165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)unsigned BasicTTI::getRegisterBitWidth(bool Vector) const { 2175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return 32; 2185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 2195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 2205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)unsigned BasicTTI::getMaximumUnrollFactor() const { 2215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return 1; 2225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 224eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochunsigned BasicTTI::getArithmeticInstrCost(unsigned Opcode, Type *Ty, 225eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch OperandValueKind, 226 OperandValueKind) const { 227 // Check if any of the operands are vector operands. 228 const TargetLoweringBase *TLI = getTLI(); 229 int ISD = TLI->InstructionOpcodeToISD(Opcode); 230 assert(ISD && "Invalid opcode"); 231 232 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty); 233 234 bool IsFloat = Ty->getScalarType()->isFloatingPointTy(); 235 // Assume that floating point arithmetic operations cost twice as much as 236 // integer operations. 237 unsigned OpCost = (IsFloat ? 2 : 1); 238 239 if (TLI->isOperationLegalOrPromote(ISD, LT.second)) { 240 // The operation is legal. Assume it costs 1. 241 // If the type is split to multiple registers, assume that there is some 242 // overhead to this. 243 // TODO: Once we have extract/insert subvector cost we need to use them. 244 if (LT.first > 1) 245 return LT.first * 2 * OpCost; 246 return LT.first * 1 * OpCost; 247 } 248 249 if (!TLI->isOperationExpand(ISD, LT.second)) { 250 // If the operation is custom lowered then assume 251 // thare the code is twice as expensive. 252 return LT.first * 2 * OpCost; 253 } 254 255 // Else, assume that we need to scalarize this op. 256 if (Ty->isVectorTy()) { 257 unsigned Num = Ty->getVectorNumElements(); 258 unsigned Cost = TopTTI->getArithmeticInstrCost(Opcode, Ty->getScalarType()); 259 // return the cost of multiple scalar invocation plus the cost of inserting 260 // and extracting the values. 261 return getScalarizationOverhead(Ty, true, true) + Num * Cost; 262 } 263 264 // We don't know anything about this scalar instruction. 265 return OpCost; 266} 267 268unsigned BasicTTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index, 269 Type *SubTp) const { 270 return 1; 271} 272 273unsigned BasicTTI::getCastInstrCost(unsigned Opcode, Type *Dst, 274 Type *Src) const { 275 const TargetLoweringBase *TLI = getTLI(); 276 int ISD = TLI->InstructionOpcodeToISD(Opcode); 277 assert(ISD && "Invalid opcode"); 278 279 std::pair<unsigned, MVT> SrcLT = TLI->getTypeLegalizationCost(Src); 280 std::pair<unsigned, MVT> DstLT = TLI->getTypeLegalizationCost(Dst); 281 282 // Check for NOOP conversions. 283 if (SrcLT.first == DstLT.first && 284 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) { 285 286 // Bitcast between types that are legalized to the same type are free. 287 if (Opcode == Instruction::BitCast || Opcode == Instruction::Trunc) 288 return 0; 289 } 290 291 if (Opcode == Instruction::Trunc && 292 TLI->isTruncateFree(SrcLT.second, DstLT.second)) 293 return 0; 294 295 if (Opcode == Instruction::ZExt && 296 TLI->isZExtFree(SrcLT.second, DstLT.second)) 297 return 0; 298 299 // If the cast is marked as legal (or promote) then assume low cost. 300 if (SrcLT.first == DstLT.first && 301 TLI->isOperationLegalOrPromote(ISD, DstLT.second)) 302 return 1; 303 304 // Handle scalar conversions. 305 if (!Src->isVectorTy() && !Dst->isVectorTy()) { 306 307 // Scalar bitcasts are usually free. 308 if (Opcode == Instruction::BitCast) 309 return 0; 310 311 // Just check the op cost. If the operation is legal then assume it costs 1. 312 if (!TLI->isOperationExpand(ISD, DstLT.second)) 313 return 1; 314 315 // Assume that illegal scalar instruction are expensive. 316 return 4; 317 } 318 319 // Check vector-to-vector casts. 320 if (Dst->isVectorTy() && Src->isVectorTy()) { 321 322 // If the cast is between same-sized registers, then the check is simple. 323 if (SrcLT.first == DstLT.first && 324 SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) { 325 326 // Assume that Zext is done using AND. 327 if (Opcode == Instruction::ZExt) 328 return 1; 329 330 // Assume that sext is done using SHL and SRA. 331 if (Opcode == Instruction::SExt) 332 return 2; 333 334 // Just check the op cost. If the operation is legal then assume it costs 335 // 1 and multiply by the type-legalization overhead. 336 if (!TLI->isOperationExpand(ISD, DstLT.second)) 337 return SrcLT.first * 1; 338 } 339 340 // If we are converting vectors and the operation is illegal, or 341 // if the vectors are legalized to different types, estimate the 342 // scalarization costs. 343 unsigned Num = Dst->getVectorNumElements(); 344 unsigned Cost = TopTTI->getCastInstrCost(Opcode, Dst->getScalarType(), 345 Src->getScalarType()); 346 347 // Return the cost of multiple scalar invocation plus the cost of 348 // inserting and extracting the values. 349 return getScalarizationOverhead(Dst, true, true) + Num * Cost; 350 } 351 352 // We already handled vector-to-vector and scalar-to-scalar conversions. This 353 // is where we handle bitcast between vectors and scalars. We need to assume 354 // that the conversion is scalarized in one way or another. 355 if (Opcode == Instruction::BitCast) 356 // Illegal bitcasts are done by storing and loading from a stack slot. 357 return (Src->isVectorTy()? getScalarizationOverhead(Src, false, true):0) + 358 (Dst->isVectorTy()? getScalarizationOverhead(Dst, true, false):0); 359 360 llvm_unreachable("Unhandled cast"); 361 } 362 363unsigned BasicTTI::getCFInstrCost(unsigned Opcode) const { 364 // Branches are assumed to be predicted. 365 return 0; 366} 367 368unsigned BasicTTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, 369 Type *CondTy) const { 370 const TargetLoweringBase *TLI = getTLI(); 371 int ISD = TLI->InstructionOpcodeToISD(Opcode); 372 assert(ISD && "Invalid opcode"); 373 374 // Selects on vectors are actually vector selects. 375 if (ISD == ISD::SELECT) { 376 assert(CondTy && "CondTy must exist"); 377 if (CondTy->isVectorTy()) 378 ISD = ISD::VSELECT; 379 } 380 381 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy); 382 383 if (!TLI->isOperationExpand(ISD, LT.second)) { 384 // The operation is legal. Assume it costs 1. Multiply 385 // by the type-legalization overhead. 386 return LT.first * 1; 387 } 388 389 // Otherwise, assume that the cast is scalarized. 390 if (ValTy->isVectorTy()) { 391 unsigned Num = ValTy->getVectorNumElements(); 392 if (CondTy) 393 CondTy = CondTy->getScalarType(); 394 unsigned Cost = TopTTI->getCmpSelInstrCost(Opcode, ValTy->getScalarType(), 395 CondTy); 396 397 // Return the cost of multiple scalar invocation plus the cost of inserting 398 // and extracting the values. 399 return getScalarizationOverhead(ValTy, true, false) + Num * Cost; 400 } 401 402 // Unknown scalar opcode. 403 return 1; 404} 405 406unsigned BasicTTI::getVectorInstrCost(unsigned Opcode, Type *Val, 407 unsigned Index) const { 408 std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Val->getScalarType()); 409 410 return LT.first; 411} 412 413unsigned BasicTTI::getMemoryOpCost(unsigned Opcode, Type *Src, 414 unsigned Alignment, 415 unsigned AddressSpace) const { 416 assert(!Src->isVoidTy() && "Invalid type"); 417 std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Src); 418 419 // Assuming that all loads of legal types cost 1. 420 unsigned Cost = LT.first; 421 422 if (Src->isVectorTy() && 423 Src->getPrimitiveSizeInBits() < LT.second.getSizeInBits()) { 424 // This is a vector load that legalizes to a larger type than the vector 425 // itself. Unless the corresponding extending load or truncating store is 426 // legal, then this will scalarize. 427 TargetLowering::LegalizeAction LA; 428 MVT MemVT = getTLI()->getSimpleValueType(Src, true); 429 if (Opcode == Instruction::Store) 430 LA = getTLI()->getTruncStoreAction(LT.second, MemVT); 431 else 432 LA = getTLI()->getLoadExtAction(ISD::EXTLOAD, MemVT); 433 434 if (LA != TargetLowering::Legal && LA != TargetLowering::Custom) { 435 // This is a vector load/store for some illegal type that is scalarized. 436 // We must account for the cost of building or decomposing the vector. 437 Cost += getScalarizationOverhead(Src, Opcode != Instruction::Store, 438 Opcode == Instruction::Store); 439 } 440 } 441 442 return Cost; 443} 444 445unsigned BasicTTI::getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy, 446 ArrayRef<Type *> Tys) const { 447 unsigned ISD = 0; 448 switch (IID) { 449 default: { 450 // Assume that we need to scalarize this intrinsic. 451 unsigned ScalarizationCost = 0; 452 unsigned ScalarCalls = 1; 453 if (RetTy->isVectorTy()) { 454 ScalarizationCost = getScalarizationOverhead(RetTy, true, false); 455 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements()); 456 } 457 for (unsigned i = 0, ie = Tys.size(); i != ie; ++i) { 458 if (Tys[i]->isVectorTy()) { 459 ScalarizationCost += getScalarizationOverhead(Tys[i], false, true); 460 ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements()); 461 } 462 } 463 464 return ScalarCalls + ScalarizationCost; 465 } 466 // Look for intrinsics that can be lowered directly or turned into a scalar 467 // intrinsic call. 468 case Intrinsic::sqrt: ISD = ISD::FSQRT; break; 469 case Intrinsic::sin: ISD = ISD::FSIN; break; 470 case Intrinsic::cos: ISD = ISD::FCOS; break; 471 case Intrinsic::exp: ISD = ISD::FEXP; break; 472 case Intrinsic::exp2: ISD = ISD::FEXP2; break; 473 case Intrinsic::log: ISD = ISD::FLOG; break; 474 case Intrinsic::log10: ISD = ISD::FLOG10; break; 475 case Intrinsic::log2: ISD = ISD::FLOG2; break; 476 case Intrinsic::fabs: ISD = ISD::FABS; break; 477 case Intrinsic::copysign: ISD = ISD::FCOPYSIGN; break; 478 case Intrinsic::floor: ISD = ISD::FFLOOR; break; 479 case Intrinsic::ceil: ISD = ISD::FCEIL; break; 480 case Intrinsic::trunc: ISD = ISD::FTRUNC; break; 481 case Intrinsic::nearbyint: 482 ISD = ISD::FNEARBYINT; break; 483 case Intrinsic::rint: ISD = ISD::FRINT; break; 484 case Intrinsic::round: ISD = ISD::FROUND; break; 485 case Intrinsic::pow: ISD = ISD::FPOW; break; 486 case Intrinsic::fma: ISD = ISD::FMA; break; 487 case Intrinsic::fmuladd: ISD = ISD::FMA; break; // FIXME: mul + add? 488 case Intrinsic::lifetime_start: 489 case Intrinsic::lifetime_end: 490 return 0; 491 } 492 493 const TargetLoweringBase *TLI = getTLI(); 494 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(RetTy); 495 496 if (TLI->isOperationLegalOrPromote(ISD, LT.second)) { 497 // The operation is legal. Assume it costs 1. 498 // If the type is split to multiple registers, assume that thre is some 499 // overhead to this. 500 // TODO: Once we have extract/insert subvector cost we need to use them. 501 if (LT.first > 1) 502 return LT.first * 2; 503 return LT.first * 1; 504 } 505 506 if (!TLI->isOperationExpand(ISD, LT.second)) { 507 // If the operation is custom lowered then assume 508 // thare the code is twice as expensive. 509 return LT.first * 2; 510 } 511 512 // Else, assume that we need to scalarize this intrinsic. For math builtins 513 // this will emit a costly libcall, adding call overhead and spills. Make it 514 // very expensive. 515 if (RetTy->isVectorTy()) { 516 unsigned Num = RetTy->getVectorNumElements(); 517 unsigned Cost = TopTTI->getIntrinsicInstrCost(IID, RetTy->getScalarType(), 518 Tys); 519 return 10 * Cost * Num; 520 } 521 522 // This is going to be turned into a library call, make it expensive. 523 return 10; 524} 525 526unsigned BasicTTI::getNumberOfParts(Type *Tp) const { 527 std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Tp); 528 return LT.first; 529} 530 531unsigned BasicTTI::getAddressComputationCost(Type *Ty, bool IsComplex) const { 532 return 0; 533} 534 535unsigned BasicTTI::getReductionCost(unsigned Opcode, Type *Ty, 536 bool IsPairwise) const { 537 assert(Ty->isVectorTy() && "Expect a vector type"); 538 unsigned NumVecElts = Ty->getVectorNumElements(); 539 unsigned NumReduxLevels = Log2_32(NumVecElts); 540 unsigned ArithCost = NumReduxLevels * 541 TopTTI->getArithmeticInstrCost(Opcode, Ty); 542 // Assume the pairwise shuffles add a cost. 543 unsigned ShuffleCost = 544 NumReduxLevels * (IsPairwise + 1) * 545 TopTTI->getShuffleCost(SK_ExtractSubvector, Ty, NumVecElts / 2, Ty); 546 return ShuffleCost + ArithCost + getScalarizationOverhead(Ty, false, true); 547} 548