BasicTargetTransformInfo.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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. #include "llvm/CodeGen/Passes.h"
  18. #include "llvm/Analysis/LoopInfo.h"
  19. #include "llvm/Analysis/TargetTransformInfo.h"
  20. #include "llvm/Support/CommandLine.h"
  21. #include "llvm/Target/TargetLowering.h"
  22. #include "llvm/Target/TargetSubtargetInfo.h"
  23. #include <utility>
  24. using namespace llvm;
  25. static cl::opt<unsigned>
  26. PartialUnrollingThreshold("partial-unrolling-threshold", cl::init(0),
  27. cl::desc("Threshold for partial unrolling"), cl::Hidden);
  28. #define DEBUG_TYPE "basictti"
  29. namespace {
  30. class BasicTTI final : public ImmutablePass, public TargetTransformInfo {
  31. const TargetMachine *TM;
  32. /// Estimate the overhead of scalarizing an instruction. Insert and Extract
  33. /// are set if the result needs to be inserted and/or extracted from vectors.
  34. unsigned getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const;
  35. /// Estimate the cost overhead of SK_Alternate shuffle.
  36. unsigned getAltShuffleOverhead(Type *Ty) const;
  37. const TargetLoweringBase *getTLI() const {
  38. return TM->getSubtargetImpl()->getTargetLowering();
  39. }
  40. public:
  41. BasicTTI() : ImmutablePass(ID), TM(nullptr) {
  42. llvm_unreachable("This pass cannot be directly constructed");
  43. }
  44. BasicTTI(const TargetMachine *TM) : ImmutablePass(ID), TM(TM) {
  45. initializeBasicTTIPass(*PassRegistry::getPassRegistry());
  46. }
  47. void initializePass() override {
  48. pushTTIStack(this);
  49. }
  50. void getAnalysisUsage(AnalysisUsage &AU) const override {
  51. TargetTransformInfo::getAnalysisUsage(AU);
  52. }
  53. /// Pass identification.
  54. static char ID;
  55. /// Provide necessary pointer adjustments for the two base classes.
  56. void *getAdjustedAnalysisPointer(const void *ID) override {
  57. if (ID == &TargetTransformInfo::ID)
  58. return (TargetTransformInfo*)this;
  59. return this;
  60. }
  61. bool hasBranchDivergence() const override;
  62. /// \name Scalar TTI Implementations
  63. /// @{
  64. bool isLegalAddImmediate(int64_t imm) const override;
  65. bool isLegalICmpImmediate(int64_t imm) const override;
  66. bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
  67. int64_t BaseOffset, bool HasBaseReg,
  68. int64_t Scale) const override;
  69. int getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
  70. int64_t BaseOffset, bool HasBaseReg,
  71. int64_t Scale) const override;
  72. bool isTruncateFree(Type *Ty1, Type *Ty2) const override;
  73. bool isTypeLegal(Type *Ty) const override;
  74. unsigned getJumpBufAlignment() const override;
  75. unsigned getJumpBufSize() const override;
  76. bool shouldBuildLookupTables() const override;
  77. bool haveFastSqrt(Type *Ty) const override;
  78. void getUnrollingPreferences(Loop *L,
  79. UnrollingPreferences &UP) const override;
  80. /// @}
  81. /// \name Vector TTI Implementations
  82. /// @{
  83. unsigned getNumberOfRegisters(bool Vector) const override;
  84. unsigned getMaximumUnrollFactor() const override;
  85. unsigned getRegisterBitWidth(bool Vector) const override;
  86. unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty, OperandValueKind,
  87. OperandValueKind) const override;
  88. unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
  89. int Index, Type *SubTp) const override;
  90. unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
  91. Type *Src) const override;
  92. unsigned getCFInstrCost(unsigned Opcode) const override;
  93. unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
  94. Type *CondTy) const override;
  95. unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
  96. unsigned Index) const override;
  97. unsigned getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
  98. unsigned AddressSpace) const override;
  99. unsigned getIntrinsicInstrCost(Intrinsic::ID, Type *RetTy,
  100. ArrayRef<Type*> Tys) const override;
  101. unsigned getNumberOfParts(Type *Tp) const override;
  102. unsigned getAddressComputationCost( Type *Ty, bool IsComplex) const override;
  103. unsigned getReductionCost(unsigned Opcode, Type *Ty,
  104. bool IsPairwise) const override;
  105. /// @}
  106. };
  107. }
  108. INITIALIZE_AG_PASS(BasicTTI, TargetTransformInfo, "basictti",
  109. "Target independent code generator's TTI", true, true, false)
  110. char BasicTTI::ID = 0;
  111. ImmutablePass *
  112. llvm::createBasicTargetTransformInfoPass(const TargetMachine *TM) {
  113. return new BasicTTI(TM);
  114. }
  115. bool BasicTTI::hasBranchDivergence() const { return false; }
  116. bool BasicTTI::isLegalAddImmediate(int64_t imm) const {
  117. return getTLI()->isLegalAddImmediate(imm);
  118. }
  119. bool BasicTTI::isLegalICmpImmediate(int64_t imm) const {
  120. return getTLI()->isLegalICmpImmediate(imm);
  121. }
  122. bool BasicTTI::isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV,
  123. int64_t BaseOffset, bool HasBaseReg,
  124. int64_t Scale) const {
  125. TargetLoweringBase::AddrMode AM;
  126. AM.BaseGV = BaseGV;
  127. AM.BaseOffs = BaseOffset;
  128. AM.HasBaseReg = HasBaseReg;
  129. AM.Scale = Scale;
  130. return getTLI()->isLegalAddressingMode(AM, Ty);
  131. }
  132. int BasicTTI::getScalingFactorCost(Type *Ty, GlobalValue *BaseGV,
  133. int64_t BaseOffset, bool HasBaseReg,
  134. int64_t Scale) const {
  135. TargetLoweringBase::AddrMode AM;
  136. AM.BaseGV = BaseGV;
  137. AM.BaseOffs = BaseOffset;
  138. AM.HasBaseReg = HasBaseReg;
  139. AM.Scale = Scale;
  140. return getTLI()->getScalingFactorCost(AM, Ty);
  141. }
  142. bool BasicTTI::isTruncateFree(Type *Ty1, Type *Ty2) const {
  143. return getTLI()->isTruncateFree(Ty1, Ty2);
  144. }
  145. bool BasicTTI::isTypeLegal(Type *Ty) const {
  146. EVT T = getTLI()->getValueType(Ty);
  147. return getTLI()->isTypeLegal(T);
  148. }
  149. unsigned BasicTTI::getJumpBufAlignment() const {
  150. return getTLI()->getJumpBufAlignment();
  151. }
  152. unsigned BasicTTI::getJumpBufSize() const {
  153. return getTLI()->getJumpBufSize();
  154. }
  155. bool BasicTTI::shouldBuildLookupTables() const {
  156. const TargetLoweringBase *TLI = getTLI();
  157. return TLI->supportJumpTables() &&
  158. (TLI->isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
  159. TLI->isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
  160. }
  161. bool BasicTTI::haveFastSqrt(Type *Ty) const {
  162. const TargetLoweringBase *TLI = getTLI();
  163. EVT VT = TLI->getValueType(Ty);
  164. return TLI->isTypeLegal(VT) && TLI->isOperationLegalOrCustom(ISD::FSQRT, VT);
  165. }
  166. void BasicTTI::getUnrollingPreferences(Loop *L,
  167. UnrollingPreferences &UP) const {
  168. // This unrolling functionality is target independent, but to provide some
  169. // motivation for its intended use, for x86:
  170. // According to the Intel 64 and IA-32 Architectures Optimization Reference
  171. // Manual, Intel Core models and later have a loop stream detector
  172. // (and associated uop queue) that can benefit from partial unrolling.
  173. // The relevant requirements are:
  174. // - The loop must have no more than 4 (8 for Nehalem and later) branches
  175. // taken, and none of them may be calls.
  176. // - The loop can have no more than 18 (28 for Nehalem and later) uops.
  177. // According to the Software Optimization Guide for AMD Family 15h Processors,
  178. // models 30h-4fh (Steamroller and later) have a loop predictor and loop
  179. // buffer which can benefit from partial unrolling.
  180. // The relevant requirements are:
  181. // - The loop must have fewer than 16 branches
  182. // - The loop must have less than 40 uops in all executed loop branches
  183. // The number of taken branches in a loop is hard to estimate here, and
  184. // benchmarking has revealed that it is better not to be conservative when
  185. // estimating the branch count. As a result, we'll ignore the branch limits
  186. // until someone finds a case where it matters in practice.
  187. unsigned MaxOps;
  188. const TargetSubtargetInfo *ST = &TM->getSubtarget<TargetSubtargetInfo>();
  189. if (PartialUnrollingThreshold.getNumOccurrences() > 0)
  190. MaxOps = PartialUnrollingThreshold;
  191. else if (ST->getSchedModel()->LoopMicroOpBufferSize > 0)
  192. MaxOps = ST->getSchedModel()->LoopMicroOpBufferSize;
  193. else
  194. return;
  195. // Scan the loop: don't unroll loops with calls.
  196. for (Loop::block_iterator I = L->block_begin(), E = L->block_end();
  197. I != E; ++I) {
  198. BasicBlock *BB = *I;
  199. for (BasicBlock::iterator J = BB->begin(), JE = BB->end(); J != JE; ++J)
  200. if (isa<CallInst>(J) || isa<InvokeInst>(J)) {
  201. ImmutableCallSite CS(J);
  202. if (const Function *F = CS.getCalledFunction()) {
  203. if (!TopTTI->isLoweredToCall(F))
  204. continue;
  205. }
  206. return;
  207. }
  208. }
  209. // Enable runtime and partial unrolling up to the specified size.
  210. UP.Partial = UP.Runtime = true;
  211. UP.PartialThreshold = UP.PartialOptSizeThreshold = MaxOps;
  212. }
  213. //===----------------------------------------------------------------------===//
  214. //
  215. // Calls used by the vectorizers.
  216. //
  217. //===----------------------------------------------------------------------===//
  218. unsigned BasicTTI::getScalarizationOverhead(Type *Ty, bool Insert,
  219. bool Extract) const {
  220. assert (Ty->isVectorTy() && "Can only scalarize vectors");
  221. unsigned Cost = 0;
  222. for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
  223. if (Insert)
  224. Cost += TopTTI->getVectorInstrCost(Instruction::InsertElement, Ty, i);
  225. if (Extract)
  226. Cost += TopTTI->getVectorInstrCost(Instruction::ExtractElement, Ty, i);
  227. }
  228. return Cost;
  229. }
  230. unsigned BasicTTI::getNumberOfRegisters(bool Vector) const {
  231. return 1;
  232. }
  233. unsigned BasicTTI::getRegisterBitWidth(bool Vector) const {
  234. return 32;
  235. }
  236. unsigned BasicTTI::getMaximumUnrollFactor() const {
  237. return 1;
  238. }
  239. unsigned BasicTTI::getArithmeticInstrCost(unsigned Opcode, Type *Ty,
  240. OperandValueKind,
  241. OperandValueKind) const {
  242. // Check if any of the operands are vector operands.
  243. const TargetLoweringBase *TLI = getTLI();
  244. int ISD = TLI->InstructionOpcodeToISD(Opcode);
  245. assert(ISD && "Invalid opcode");
  246. std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty);
  247. bool IsFloat = Ty->getScalarType()->isFloatingPointTy();
  248. // Assume that floating point arithmetic operations cost twice as much as
  249. // integer operations.
  250. unsigned OpCost = (IsFloat ? 2 : 1);
  251. if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
  252. // The operation is legal. Assume it costs 1.
  253. // If the type is split to multiple registers, assume that there is some
  254. // overhead to this.
  255. // TODO: Once we have extract/insert subvector cost we need to use them.
  256. if (LT.first > 1)
  257. return LT.first * 2 * OpCost;
  258. return LT.first * 1 * OpCost;
  259. }
  260. if (!TLI->isOperationExpand(ISD, LT.second)) {
  261. // If the operation is custom lowered then assume
  262. // thare the code is twice as expensive.
  263. return LT.first * 2 * OpCost;
  264. }
  265. // Else, assume that we need to scalarize this op.
  266. if (Ty->isVectorTy()) {
  267. unsigned Num = Ty->getVectorNumElements();
  268. unsigned Cost = TopTTI->getArithmeticInstrCost(Opcode, Ty->getScalarType());
  269. // return the cost of multiple scalar invocation plus the cost of inserting
  270. // and extracting the values.
  271. return getScalarizationOverhead(Ty, true, true) + Num * Cost;
  272. }
  273. // We don't know anything about this scalar instruction.
  274. return OpCost;
  275. }
  276. unsigned BasicTTI::getAltShuffleOverhead(Type *Ty) const {
  277. assert(Ty->isVectorTy() && "Can only shuffle vectors");
  278. unsigned Cost = 0;
  279. // Shuffle cost is equal to the cost of extracting element from its argument
  280. // plus the cost of inserting them onto the result vector.
  281. // e.g. <4 x float> has a mask of <0,5,2,7> i.e we need to extract from index
  282. // 0 of first vector, index 1 of second vector,index 2 of first vector and
  283. // finally index 3 of second vector and insert them at index <0,1,2,3> of
  284. // result vector.
  285. for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
  286. Cost += TopTTI->getVectorInstrCost(Instruction::InsertElement, Ty, i);
  287. Cost += TopTTI->getVectorInstrCost(Instruction::ExtractElement, Ty, i);
  288. }
  289. return Cost;
  290. }
  291. unsigned BasicTTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index,
  292. Type *SubTp) const {
  293. if (Kind == SK_Alternate) {
  294. return getAltShuffleOverhead(Tp);
  295. }
  296. return 1;
  297. }
  298. unsigned BasicTTI::getCastInstrCost(unsigned Opcode, Type *Dst,
  299. Type *Src) const {
  300. const TargetLoweringBase *TLI = getTLI();
  301. int ISD = TLI->InstructionOpcodeToISD(Opcode);
  302. assert(ISD && "Invalid opcode");
  303. std::pair<unsigned, MVT> SrcLT = TLI->getTypeLegalizationCost(Src);
  304. std::pair<unsigned, MVT> DstLT = TLI->getTypeLegalizationCost(Dst);
  305. // Check for NOOP conversions.
  306. if (SrcLT.first == DstLT.first &&
  307. SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
  308. // Bitcast between types that are legalized to the same type are free.
  309. if (Opcode == Instruction::BitCast || Opcode == Instruction::Trunc)
  310. return 0;
  311. }
  312. if (Opcode == Instruction::Trunc &&
  313. TLI->isTruncateFree(SrcLT.second, DstLT.second))
  314. return 0;
  315. if (Opcode == Instruction::ZExt &&
  316. TLI->isZExtFree(SrcLT.second, DstLT.second))
  317. return 0;
  318. // If the cast is marked as legal (or promote) then assume low cost.
  319. if (SrcLT.first == DstLT.first &&
  320. TLI->isOperationLegalOrPromote(ISD, DstLT.second))
  321. return 1;
  322. // Handle scalar conversions.
  323. if (!Src->isVectorTy() && !Dst->isVectorTy()) {
  324. // Scalar bitcasts are usually free.
  325. if (Opcode == Instruction::BitCast)
  326. return 0;
  327. // Just check the op cost. If the operation is legal then assume it costs 1.
  328. if (!TLI->isOperationExpand(ISD, DstLT.second))
  329. return 1;
  330. // Assume that illegal scalar instruction are expensive.
  331. return 4;
  332. }
  333. // Check vector-to-vector casts.
  334. if (Dst->isVectorTy() && Src->isVectorTy()) {
  335. // If the cast is between same-sized registers, then the check is simple.
  336. if (SrcLT.first == DstLT.first &&
  337. SrcLT.second.getSizeInBits() == DstLT.second.getSizeInBits()) {
  338. // Assume that Zext is done using AND.
  339. if (Opcode == Instruction::ZExt)
  340. return 1;
  341. // Assume that sext is done using SHL and SRA.
  342. if (Opcode == Instruction::SExt)
  343. return 2;
  344. // Just check the op cost. If the operation is legal then assume it costs
  345. // 1 and multiply by the type-legalization overhead.
  346. if (!TLI->isOperationExpand(ISD, DstLT.second))
  347. return SrcLT.first * 1;
  348. }
  349. // If we are converting vectors and the operation is illegal, or
  350. // if the vectors are legalized to different types, estimate the
  351. // scalarization costs.
  352. unsigned Num = Dst->getVectorNumElements();
  353. unsigned Cost = TopTTI->getCastInstrCost(Opcode, Dst->getScalarType(),
  354. Src->getScalarType());
  355. // Return the cost of multiple scalar invocation plus the cost of
  356. // inserting and extracting the values.
  357. return getScalarizationOverhead(Dst, true, true) + Num * Cost;
  358. }
  359. // We already handled vector-to-vector and scalar-to-scalar conversions. This
  360. // is where we handle bitcast between vectors and scalars. We need to assume
  361. // that the conversion is scalarized in one way or another.
  362. if (Opcode == Instruction::BitCast)
  363. // Illegal bitcasts are done by storing and loading from a stack slot.
  364. return (Src->isVectorTy()? getScalarizationOverhead(Src, false, true):0) +
  365. (Dst->isVectorTy()? getScalarizationOverhead(Dst, true, false):0);
  366. llvm_unreachable("Unhandled cast");
  367. }
  368. unsigned BasicTTI::getCFInstrCost(unsigned Opcode) const {
  369. // Branches are assumed to be predicted.
  370. return 0;
  371. }
  372. unsigned BasicTTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
  373. Type *CondTy) const {
  374. const TargetLoweringBase *TLI = getTLI();
  375. int ISD = TLI->InstructionOpcodeToISD(Opcode);
  376. assert(ISD && "Invalid opcode");
  377. // Selects on vectors are actually vector selects.
  378. if (ISD == ISD::SELECT) {
  379. assert(CondTy && "CondTy must exist");
  380. if (CondTy->isVectorTy())
  381. ISD = ISD::VSELECT;
  382. }
  383. std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
  384. if (!TLI->isOperationExpand(ISD, LT.second)) {
  385. // The operation is legal. Assume it costs 1. Multiply
  386. // by the type-legalization overhead.
  387. return LT.first * 1;
  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. // Return the cost of multiple scalar invocation plus the cost of inserting
  397. // and extracting the values.
  398. return getScalarizationOverhead(ValTy, true, false) + Num * Cost;
  399. }
  400. // Unknown scalar opcode.
  401. return 1;
  402. }
  403. unsigned BasicTTI::getVectorInstrCost(unsigned Opcode, Type *Val,
  404. unsigned Index) const {
  405. std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Val->getScalarType());
  406. return LT.first;
  407. }
  408. unsigned BasicTTI::getMemoryOpCost(unsigned Opcode, Type *Src,
  409. unsigned Alignment,
  410. unsigned AddressSpace) const {
  411. assert(!Src->isVoidTy() && "Invalid type");
  412. std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Src);
  413. // Assuming that all loads of legal types cost 1.
  414. unsigned Cost = LT.first;
  415. if (Src->isVectorTy() &&
  416. Src->getPrimitiveSizeInBits() < LT.second.getSizeInBits()) {
  417. // This is a vector load that legalizes to a larger type than the vector
  418. // itself. Unless the corresponding extending load or truncating store is
  419. // legal, then this will scalarize.
  420. TargetLowering::LegalizeAction LA = TargetLowering::Expand;
  421. EVT MemVT = getTLI()->getValueType(Src, true);
  422. if (MemVT.isSimple() && MemVT != MVT::Other) {
  423. if (Opcode == Instruction::Store)
  424. LA = getTLI()->getTruncStoreAction(LT.second, MemVT.getSimpleVT());
  425. else
  426. LA = getTLI()->getLoadExtAction(ISD::EXTLOAD, MemVT.getSimpleVT());
  427. }
  428. if (LA != TargetLowering::Legal && LA != TargetLowering::Custom) {
  429. // This is a vector load/store for some illegal type that is scalarized.
  430. // We must account for the cost of building or decomposing the vector.
  431. Cost += getScalarizationOverhead(Src, Opcode != Instruction::Store,
  432. Opcode == Instruction::Store);
  433. }
  434. }
  435. return Cost;
  436. }
  437. unsigned BasicTTI::getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy,
  438. ArrayRef<Type *> Tys) const {
  439. unsigned ISD = 0;
  440. switch (IID) {
  441. default: {
  442. // Assume that we need to scalarize this intrinsic.
  443. unsigned ScalarizationCost = 0;
  444. unsigned ScalarCalls = 1;
  445. if (RetTy->isVectorTy()) {
  446. ScalarizationCost = getScalarizationOverhead(RetTy, true, false);
  447. ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
  448. }
  449. for (unsigned i = 0, ie = Tys.size(); i != ie; ++i) {
  450. if (Tys[i]->isVectorTy()) {
  451. ScalarizationCost += getScalarizationOverhead(Tys[i], false, true);
  452. ScalarCalls = std::max(ScalarCalls, RetTy->getVectorNumElements());
  453. }
  454. }
  455. return ScalarCalls + ScalarizationCost;
  456. }
  457. // Look for intrinsics that can be lowered directly or turned into a scalar
  458. // intrinsic call.
  459. case Intrinsic::sqrt: ISD = ISD::FSQRT; break;
  460. case Intrinsic::sin: ISD = ISD::FSIN; break;
  461. case Intrinsic::cos: ISD = ISD::FCOS; break;
  462. case Intrinsic::exp: ISD = ISD::FEXP; break;
  463. case Intrinsic::exp2: ISD = ISD::FEXP2; break;
  464. case Intrinsic::log: ISD = ISD::FLOG; break;
  465. case Intrinsic::log10: ISD = ISD::FLOG10; break;
  466. case Intrinsic::log2: ISD = ISD::FLOG2; break;
  467. case Intrinsic::fabs: ISD = ISD::FABS; break;
  468. case Intrinsic::copysign: ISD = ISD::FCOPYSIGN; break;
  469. case Intrinsic::floor: ISD = ISD::FFLOOR; break;
  470. case Intrinsic::ceil: ISD = ISD::FCEIL; break;
  471. case Intrinsic::trunc: ISD = ISD::FTRUNC; break;
  472. case Intrinsic::nearbyint:
  473. ISD = ISD::FNEARBYINT; break;
  474. case Intrinsic::rint: ISD = ISD::FRINT; break;
  475. case Intrinsic::round: ISD = ISD::FROUND; break;
  476. case Intrinsic::pow: ISD = ISD::FPOW; break;
  477. case Intrinsic::fma: ISD = ISD::FMA; break;
  478. case Intrinsic::fmuladd: ISD = ISD::FMA; break;
  479. // FIXME: We should return 0 whenever getIntrinsicCost == TCC_Free.
  480. case Intrinsic::lifetime_start:
  481. case Intrinsic::lifetime_end:
  482. return 0;
  483. }
  484. const TargetLoweringBase *TLI = getTLI();
  485. std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(RetTy);
  486. if (TLI->isOperationLegalOrPromote(ISD, LT.second)) {
  487. // The operation is legal. Assume it costs 1.
  488. // If the type is split to multiple registers, assume that thre is some
  489. // overhead to this.
  490. // TODO: Once we have extract/insert subvector cost we need to use them.
  491. if (LT.first > 1)
  492. return LT.first * 2;
  493. return LT.first * 1;
  494. }
  495. if (!TLI->isOperationExpand(ISD, LT.second)) {
  496. // If the operation is custom lowered then assume
  497. // thare the code is twice as expensive.
  498. return LT.first * 2;
  499. }
  500. // If we can't lower fmuladd into an FMA estimate the cost as a floating
  501. // point mul followed by an add.
  502. if (IID == Intrinsic::fmuladd)
  503. return TopTTI->getArithmeticInstrCost(BinaryOperator::FMul, RetTy) +
  504. TopTTI->getArithmeticInstrCost(BinaryOperator::FAdd, RetTy);
  505. // Else, assume that we need to scalarize this intrinsic. For math builtins
  506. // this will emit a costly libcall, adding call overhead and spills. Make it
  507. // very expensive.
  508. if (RetTy->isVectorTy()) {
  509. unsigned Num = RetTy->getVectorNumElements();
  510. unsigned Cost = TopTTI->getIntrinsicInstrCost(IID, RetTy->getScalarType(),
  511. Tys);
  512. return 10 * Cost * Num;
  513. }
  514. // This is going to be turned into a library call, make it expensive.
  515. return 10;
  516. }
  517. unsigned BasicTTI::getNumberOfParts(Type *Tp) const {
  518. std::pair<unsigned, MVT> LT = getTLI()->getTypeLegalizationCost(Tp);
  519. return LT.first;
  520. }
  521. unsigned BasicTTI::getAddressComputationCost(Type *Ty, bool IsComplex) const {
  522. return 0;
  523. }
  524. unsigned BasicTTI::getReductionCost(unsigned Opcode, Type *Ty,
  525. bool IsPairwise) const {
  526. assert(Ty->isVectorTy() && "Expect a vector type");
  527. unsigned NumVecElts = Ty->getVectorNumElements();
  528. unsigned NumReduxLevels = Log2_32(NumVecElts);
  529. unsigned ArithCost = NumReduxLevels *
  530. TopTTI->getArithmeticInstrCost(Opcode, Ty);
  531. // Assume the pairwise shuffles add a cost.
  532. unsigned ShuffleCost =
  533. NumReduxLevels * (IsPairwise + 1) *
  534. TopTTI->getShuffleCost(SK_ExtractSubvector, Ty, NumVecElts / 2, Ty);
  535. return ShuffleCost + ArithCost + getScalarizationOverhead(Ty, false, true);
  536. }