PPCTargetTransformInfo.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  1. //===-- PPCTargetTransformInfo.cpp - PPC specific TTI ---------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "PPCTargetTransformInfo.h"
  9. #include "llvm/Analysis/CodeMetrics.h"
  10. #include "llvm/Analysis/TargetLibraryInfo.h"
  11. #include "llvm/Analysis/TargetTransformInfo.h"
  12. #include "llvm/CodeGen/BasicTTIImpl.h"
  13. #include "llvm/CodeGen/CostTable.h"
  14. #include "llvm/CodeGen/TargetLowering.h"
  15. #include "llvm/CodeGen/TargetSchedule.h"
  16. #include "llvm/IR/IntrinsicsPowerPC.h"
  17. #include "llvm/IR/ProfDataUtils.h"
  18. #include "llvm/Support/CommandLine.h"
  19. #include "llvm/Support/Debug.h"
  20. #include "llvm/Support/KnownBits.h"
  21. #include "llvm/Transforms/InstCombine/InstCombiner.h"
  22. #include "llvm/Transforms/Utils/Local.h"
  23. #include <optional>
  24. using namespace llvm;
  25. #define DEBUG_TYPE "ppctti"
  26. static cl::opt<bool> DisablePPCConstHoist("disable-ppc-constant-hoisting",
  27. cl::desc("disable constant hoisting on PPC"), cl::init(false), cl::Hidden);
  28. static cl::opt<bool>
  29. EnablePPCColdCC("ppc-enable-coldcc", cl::Hidden, cl::init(false),
  30. cl::desc("Enable using coldcc calling conv for cold "
  31. "internal functions"));
  32. static cl::opt<bool>
  33. LsrNoInsnsCost("ppc-lsr-no-insns-cost", cl::Hidden, cl::init(false),
  34. cl::desc("Do not add instruction count to lsr cost model"));
  35. // The latency of mtctr is only justified if there are more than 4
  36. // comparisons that will be removed as a result.
  37. static cl::opt<unsigned>
  38. SmallCTRLoopThreshold("min-ctr-loop-threshold", cl::init(4), cl::Hidden,
  39. cl::desc("Loops with a constant trip count smaller than "
  40. "this value will not use the count register."));
  41. //===----------------------------------------------------------------------===//
  42. //
  43. // PPC cost model.
  44. //
  45. //===----------------------------------------------------------------------===//
  46. TargetTransformInfo::PopcntSupportKind
  47. PPCTTIImpl::getPopcntSupport(unsigned TyWidth) {
  48. assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
  49. if (ST->hasPOPCNTD() != PPCSubtarget::POPCNTD_Unavailable && TyWidth <= 64)
  50. return ST->hasPOPCNTD() == PPCSubtarget::POPCNTD_Slow ?
  51. TTI::PSK_SlowHardware : TTI::PSK_FastHardware;
  52. return TTI::PSK_Software;
  53. }
  54. std::optional<Instruction *>
  55. PPCTTIImpl::instCombineIntrinsic(InstCombiner &IC, IntrinsicInst &II) const {
  56. Intrinsic::ID IID = II.getIntrinsicID();
  57. switch (IID) {
  58. default:
  59. break;
  60. case Intrinsic::ppc_altivec_lvx:
  61. case Intrinsic::ppc_altivec_lvxl:
  62. // Turn PPC lvx -> load if the pointer is known aligned.
  63. if (getOrEnforceKnownAlignment(
  64. II.getArgOperand(0), Align(16), IC.getDataLayout(), &II,
  65. &IC.getAssumptionCache(), &IC.getDominatorTree()) >= 16) {
  66. Value *Ptr = IC.Builder.CreateBitCast(
  67. II.getArgOperand(0), PointerType::getUnqual(II.getType()));
  68. return new LoadInst(II.getType(), Ptr, "", false, Align(16));
  69. }
  70. break;
  71. case Intrinsic::ppc_vsx_lxvw4x:
  72. case Intrinsic::ppc_vsx_lxvd2x: {
  73. // Turn PPC VSX loads into normal loads.
  74. Value *Ptr = IC.Builder.CreateBitCast(II.getArgOperand(0),
  75. PointerType::getUnqual(II.getType()));
  76. return new LoadInst(II.getType(), Ptr, Twine(""), false, Align(1));
  77. }
  78. case Intrinsic::ppc_altivec_stvx:
  79. case Intrinsic::ppc_altivec_stvxl:
  80. // Turn stvx -> store if the pointer is known aligned.
  81. if (getOrEnforceKnownAlignment(
  82. II.getArgOperand(1), Align(16), IC.getDataLayout(), &II,
  83. &IC.getAssumptionCache(), &IC.getDominatorTree()) >= 16) {
  84. Type *OpPtrTy = PointerType::getUnqual(II.getArgOperand(0)->getType());
  85. Value *Ptr = IC.Builder.CreateBitCast(II.getArgOperand(1), OpPtrTy);
  86. return new StoreInst(II.getArgOperand(0), Ptr, false, Align(16));
  87. }
  88. break;
  89. case Intrinsic::ppc_vsx_stxvw4x:
  90. case Intrinsic::ppc_vsx_stxvd2x: {
  91. // Turn PPC VSX stores into normal stores.
  92. Type *OpPtrTy = PointerType::getUnqual(II.getArgOperand(0)->getType());
  93. Value *Ptr = IC.Builder.CreateBitCast(II.getArgOperand(1), OpPtrTy);
  94. return new StoreInst(II.getArgOperand(0), Ptr, false, Align(1));
  95. }
  96. case Intrinsic::ppc_altivec_vperm:
  97. // Turn vperm(V1,V2,mask) -> shuffle(V1,V2,mask) if mask is a constant.
  98. // Note that ppc_altivec_vperm has a big-endian bias, so when creating
  99. // a vectorshuffle for little endian, we must undo the transformation
  100. // performed on vec_perm in altivec.h. That is, we must complement
  101. // the permutation mask with respect to 31 and reverse the order of
  102. // V1 and V2.
  103. if (Constant *Mask = dyn_cast<Constant>(II.getArgOperand(2))) {
  104. assert(cast<FixedVectorType>(Mask->getType())->getNumElements() == 16 &&
  105. "Bad type for intrinsic!");
  106. // Check that all of the elements are integer constants or undefs.
  107. bool AllEltsOk = true;
  108. for (unsigned i = 0; i != 16; ++i) {
  109. Constant *Elt = Mask->getAggregateElement(i);
  110. if (!Elt || !(isa<ConstantInt>(Elt) || isa<UndefValue>(Elt))) {
  111. AllEltsOk = false;
  112. break;
  113. }
  114. }
  115. if (AllEltsOk) {
  116. // Cast the input vectors to byte vectors.
  117. Value *Op0 =
  118. IC.Builder.CreateBitCast(II.getArgOperand(0), Mask->getType());
  119. Value *Op1 =
  120. IC.Builder.CreateBitCast(II.getArgOperand(1), Mask->getType());
  121. Value *Result = UndefValue::get(Op0->getType());
  122. // Only extract each element once.
  123. Value *ExtractedElts[32];
  124. memset(ExtractedElts, 0, sizeof(ExtractedElts));
  125. for (unsigned i = 0; i != 16; ++i) {
  126. if (isa<UndefValue>(Mask->getAggregateElement(i)))
  127. continue;
  128. unsigned Idx =
  129. cast<ConstantInt>(Mask->getAggregateElement(i))->getZExtValue();
  130. Idx &= 31; // Match the hardware behavior.
  131. if (DL.isLittleEndian())
  132. Idx = 31 - Idx;
  133. if (!ExtractedElts[Idx]) {
  134. Value *Op0ToUse = (DL.isLittleEndian()) ? Op1 : Op0;
  135. Value *Op1ToUse = (DL.isLittleEndian()) ? Op0 : Op1;
  136. ExtractedElts[Idx] = IC.Builder.CreateExtractElement(
  137. Idx < 16 ? Op0ToUse : Op1ToUse, IC.Builder.getInt32(Idx & 15));
  138. }
  139. // Insert this value into the result vector.
  140. Result = IC.Builder.CreateInsertElement(Result, ExtractedElts[Idx],
  141. IC.Builder.getInt32(i));
  142. }
  143. return CastInst::Create(Instruction::BitCast, Result, II.getType());
  144. }
  145. }
  146. break;
  147. }
  148. return std::nullopt;
  149. }
  150. InstructionCost PPCTTIImpl::getIntImmCost(const APInt &Imm, Type *Ty,
  151. TTI::TargetCostKind CostKind) {
  152. if (DisablePPCConstHoist)
  153. return BaseT::getIntImmCost(Imm, Ty, CostKind);
  154. assert(Ty->isIntegerTy());
  155. unsigned BitSize = Ty->getPrimitiveSizeInBits();
  156. if (BitSize == 0)
  157. return ~0U;
  158. if (Imm == 0)
  159. return TTI::TCC_Free;
  160. if (Imm.getBitWidth() <= 64) {
  161. if (isInt<16>(Imm.getSExtValue()))
  162. return TTI::TCC_Basic;
  163. if (isInt<32>(Imm.getSExtValue())) {
  164. // A constant that can be materialized using lis.
  165. if ((Imm.getZExtValue() & 0xFFFF) == 0)
  166. return TTI::TCC_Basic;
  167. return 2 * TTI::TCC_Basic;
  168. }
  169. }
  170. return 4 * TTI::TCC_Basic;
  171. }
  172. InstructionCost PPCTTIImpl::getIntImmCostIntrin(Intrinsic::ID IID, unsigned Idx,
  173. const APInt &Imm, Type *Ty,
  174. TTI::TargetCostKind CostKind) {
  175. if (DisablePPCConstHoist)
  176. return BaseT::getIntImmCostIntrin(IID, Idx, Imm, Ty, CostKind);
  177. assert(Ty->isIntegerTy());
  178. unsigned BitSize = Ty->getPrimitiveSizeInBits();
  179. if (BitSize == 0)
  180. return ~0U;
  181. switch (IID) {
  182. default:
  183. return TTI::TCC_Free;
  184. case Intrinsic::sadd_with_overflow:
  185. case Intrinsic::uadd_with_overflow:
  186. case Intrinsic::ssub_with_overflow:
  187. case Intrinsic::usub_with_overflow:
  188. if ((Idx == 1) && Imm.getBitWidth() <= 64 && isInt<16>(Imm.getSExtValue()))
  189. return TTI::TCC_Free;
  190. break;
  191. case Intrinsic::experimental_stackmap:
  192. if ((Idx < 2) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
  193. return TTI::TCC_Free;
  194. break;
  195. case Intrinsic::experimental_patchpoint_void:
  196. case Intrinsic::experimental_patchpoint_i64:
  197. if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
  198. return TTI::TCC_Free;
  199. break;
  200. }
  201. return PPCTTIImpl::getIntImmCost(Imm, Ty, CostKind);
  202. }
  203. InstructionCost PPCTTIImpl::getIntImmCostInst(unsigned Opcode, unsigned Idx,
  204. const APInt &Imm, Type *Ty,
  205. TTI::TargetCostKind CostKind,
  206. Instruction *Inst) {
  207. if (DisablePPCConstHoist)
  208. return BaseT::getIntImmCostInst(Opcode, Idx, Imm, Ty, CostKind, Inst);
  209. assert(Ty->isIntegerTy());
  210. unsigned BitSize = Ty->getPrimitiveSizeInBits();
  211. if (BitSize == 0)
  212. return ~0U;
  213. unsigned ImmIdx = ~0U;
  214. bool ShiftedFree = false, RunFree = false, UnsignedFree = false,
  215. ZeroFree = false;
  216. switch (Opcode) {
  217. default:
  218. return TTI::TCC_Free;
  219. case Instruction::GetElementPtr:
  220. // Always hoist the base address of a GetElementPtr. This prevents the
  221. // creation of new constants for every base constant that gets constant
  222. // folded with the offset.
  223. if (Idx == 0)
  224. return 2 * TTI::TCC_Basic;
  225. return TTI::TCC_Free;
  226. case Instruction::And:
  227. RunFree = true; // (for the rotate-and-mask instructions)
  228. [[fallthrough]];
  229. case Instruction::Add:
  230. case Instruction::Or:
  231. case Instruction::Xor:
  232. ShiftedFree = true;
  233. [[fallthrough]];
  234. case Instruction::Sub:
  235. case Instruction::Mul:
  236. case Instruction::Shl:
  237. case Instruction::LShr:
  238. case Instruction::AShr:
  239. ImmIdx = 1;
  240. break;
  241. case Instruction::ICmp:
  242. UnsignedFree = true;
  243. ImmIdx = 1;
  244. // Zero comparisons can use record-form instructions.
  245. [[fallthrough]];
  246. case Instruction::Select:
  247. ZeroFree = true;
  248. break;
  249. case Instruction::PHI:
  250. case Instruction::Call:
  251. case Instruction::Ret:
  252. case Instruction::Load:
  253. case Instruction::Store:
  254. break;
  255. }
  256. if (ZeroFree && Imm == 0)
  257. return TTI::TCC_Free;
  258. if (Idx == ImmIdx && Imm.getBitWidth() <= 64) {
  259. if (isInt<16>(Imm.getSExtValue()))
  260. return TTI::TCC_Free;
  261. if (RunFree) {
  262. if (Imm.getBitWidth() <= 32 &&
  263. (isShiftedMask_32(Imm.getZExtValue()) ||
  264. isShiftedMask_32(~Imm.getZExtValue())))
  265. return TTI::TCC_Free;
  266. if (ST->isPPC64() &&
  267. (isShiftedMask_64(Imm.getZExtValue()) ||
  268. isShiftedMask_64(~Imm.getZExtValue())))
  269. return TTI::TCC_Free;
  270. }
  271. if (UnsignedFree && isUInt<16>(Imm.getZExtValue()))
  272. return TTI::TCC_Free;
  273. if (ShiftedFree && (Imm.getZExtValue() & 0xFFFF) == 0)
  274. return TTI::TCC_Free;
  275. }
  276. return PPCTTIImpl::getIntImmCost(Imm, Ty, CostKind);
  277. }
  278. // Check if the current Type is an MMA vector type. Valid MMA types are
  279. // v256i1 and v512i1 respectively.
  280. static bool isMMAType(Type *Ty) {
  281. return Ty->isVectorTy() && (Ty->getScalarSizeInBits() == 1) &&
  282. (Ty->getPrimitiveSizeInBits() > 128);
  283. }
  284. InstructionCost PPCTTIImpl::getInstructionCost(const User *U,
  285. ArrayRef<const Value *> Operands,
  286. TTI::TargetCostKind CostKind) {
  287. // We already implement getCastInstrCost and getMemoryOpCost where we perform
  288. // the vector adjustment there.
  289. if (isa<CastInst>(U) || isa<LoadInst>(U) || isa<StoreInst>(U))
  290. return BaseT::getInstructionCost(U, Operands, CostKind);
  291. if (U->getType()->isVectorTy()) {
  292. // Instructions that need to be split should cost more.
  293. std::pair<InstructionCost, MVT> LT = getTypeLegalizationCost(U->getType());
  294. return LT.first * BaseT::getInstructionCost(U, Operands, CostKind);
  295. }
  296. return BaseT::getInstructionCost(U, Operands, CostKind);
  297. }
  298. bool PPCTTIImpl::isHardwareLoopProfitable(Loop *L, ScalarEvolution &SE,
  299. AssumptionCache &AC,
  300. TargetLibraryInfo *LibInfo,
  301. HardwareLoopInfo &HWLoopInfo) {
  302. const PPCTargetMachine &TM = ST->getTargetMachine();
  303. TargetSchedModel SchedModel;
  304. SchedModel.init(ST);
  305. // Do not convert small short loops to CTR loop.
  306. unsigned ConstTripCount = SE.getSmallConstantTripCount(L);
  307. if (ConstTripCount && ConstTripCount < SmallCTRLoopThreshold) {
  308. SmallPtrSet<const Value *, 32> EphValues;
  309. CodeMetrics::collectEphemeralValues(L, &AC, EphValues);
  310. CodeMetrics Metrics;
  311. for (BasicBlock *BB : L->blocks())
  312. Metrics.analyzeBasicBlock(BB, *this, EphValues);
  313. // 6 is an approximate latency for the mtctr instruction.
  314. if (Metrics.NumInsts <= (6 * SchedModel.getIssueWidth()))
  315. return false;
  316. }
  317. // Check that there is no hardware loop related intrinsics in the loop.
  318. for (auto *BB : L->getBlocks())
  319. for (auto &I : *BB)
  320. if (auto *Call = dyn_cast<IntrinsicInst>(&I))
  321. if (Call->getIntrinsicID() == Intrinsic::set_loop_iterations ||
  322. Call->getIntrinsicID() == Intrinsic::loop_decrement)
  323. return false;
  324. SmallVector<BasicBlock*, 4> ExitingBlocks;
  325. L->getExitingBlocks(ExitingBlocks);
  326. // If there is an exit edge known to be frequently taken,
  327. // we should not transform this loop.
  328. for (auto &BB : ExitingBlocks) {
  329. Instruction *TI = BB->getTerminator();
  330. if (!TI) continue;
  331. if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
  332. uint64_t TrueWeight = 0, FalseWeight = 0;
  333. if (!BI->isConditional() ||
  334. !extractBranchWeights(*BI, TrueWeight, FalseWeight))
  335. continue;
  336. // If the exit path is more frequent than the loop path,
  337. // we return here without further analysis for this loop.
  338. bool TrueIsExit = !L->contains(BI->getSuccessor(0));
  339. if (( TrueIsExit && FalseWeight < TrueWeight) ||
  340. (!TrueIsExit && FalseWeight > TrueWeight))
  341. return false;
  342. }
  343. }
  344. LLVMContext &C = L->getHeader()->getContext();
  345. HWLoopInfo.CountType = TM.isPPC64() ?
  346. Type::getInt64Ty(C) : Type::getInt32Ty(C);
  347. HWLoopInfo.LoopDecrement = ConstantInt::get(HWLoopInfo.CountType, 1);
  348. return true;
  349. }
  350. void PPCTTIImpl::getUnrollingPreferences(Loop *L, ScalarEvolution &SE,
  351. TTI::UnrollingPreferences &UP,
  352. OptimizationRemarkEmitter *ORE) {
  353. if (ST->getCPUDirective() == PPC::DIR_A2) {
  354. // The A2 is in-order with a deep pipeline, and concatenation unrolling
  355. // helps expose latency-hiding opportunities to the instruction scheduler.
  356. UP.Partial = UP.Runtime = true;
  357. // We unroll a lot on the A2 (hundreds of instructions), and the benefits
  358. // often outweigh the cost of a division to compute the trip count.
  359. UP.AllowExpensiveTripCount = true;
  360. }
  361. BaseT::getUnrollingPreferences(L, SE, UP, ORE);
  362. }
  363. void PPCTTIImpl::getPeelingPreferences(Loop *L, ScalarEvolution &SE,
  364. TTI::PeelingPreferences &PP) {
  365. BaseT::getPeelingPreferences(L, SE, PP);
  366. }
  367. // This function returns true to allow using coldcc calling convention.
  368. // Returning true results in coldcc being used for functions which are cold at
  369. // all call sites when the callers of the functions are not calling any other
  370. // non coldcc functions.
  371. bool PPCTTIImpl::useColdCCForColdCall(Function &F) {
  372. return EnablePPCColdCC;
  373. }
  374. bool PPCTTIImpl::enableAggressiveInterleaving(bool LoopHasReductions) {
  375. // On the A2, always unroll aggressively.
  376. if (ST->getCPUDirective() == PPC::DIR_A2)
  377. return true;
  378. return LoopHasReductions;
  379. }
  380. PPCTTIImpl::TTI::MemCmpExpansionOptions
  381. PPCTTIImpl::enableMemCmpExpansion(bool OptSize, bool IsZeroCmp) const {
  382. TTI::MemCmpExpansionOptions Options;
  383. Options.LoadSizes = {8, 4, 2, 1};
  384. Options.MaxNumLoads = TLI->getMaxExpandSizeMemcmp(OptSize);
  385. return Options;
  386. }
  387. bool PPCTTIImpl::enableInterleavedAccessVectorization() {
  388. return true;
  389. }
  390. unsigned PPCTTIImpl::getNumberOfRegisters(unsigned ClassID) const {
  391. assert(ClassID == GPRRC || ClassID == FPRRC ||
  392. ClassID == VRRC || ClassID == VSXRC);
  393. if (ST->hasVSX()) {
  394. assert(ClassID == GPRRC || ClassID == VSXRC || ClassID == VRRC);
  395. return ClassID == VSXRC ? 64 : 32;
  396. }
  397. assert(ClassID == GPRRC || ClassID == FPRRC || ClassID == VRRC);
  398. return 32;
  399. }
  400. unsigned PPCTTIImpl::getRegisterClassForType(bool Vector, Type *Ty) const {
  401. if (Vector)
  402. return ST->hasVSX() ? VSXRC : VRRC;
  403. else if (Ty && (Ty->getScalarType()->isFloatTy() ||
  404. Ty->getScalarType()->isDoubleTy()))
  405. return ST->hasVSX() ? VSXRC : FPRRC;
  406. else if (Ty && (Ty->getScalarType()->isFP128Ty() ||
  407. Ty->getScalarType()->isPPC_FP128Ty()))
  408. return VRRC;
  409. else if (Ty && Ty->getScalarType()->isHalfTy())
  410. return VSXRC;
  411. else
  412. return GPRRC;
  413. }
  414. const char* PPCTTIImpl::getRegisterClassName(unsigned ClassID) const {
  415. switch (ClassID) {
  416. default:
  417. llvm_unreachable("unknown register class");
  418. return "PPC::unknown register class";
  419. case GPRRC: return "PPC::GPRRC";
  420. case FPRRC: return "PPC::FPRRC";
  421. case VRRC: return "PPC::VRRC";
  422. case VSXRC: return "PPC::VSXRC";
  423. }
  424. }
  425. TypeSize
  426. PPCTTIImpl::getRegisterBitWidth(TargetTransformInfo::RegisterKind K) const {
  427. switch (K) {
  428. case TargetTransformInfo::RGK_Scalar:
  429. return TypeSize::getFixed(ST->isPPC64() ? 64 : 32);
  430. case TargetTransformInfo::RGK_FixedWidthVector:
  431. return TypeSize::getFixed(ST->hasAltivec() ? 128 : 0);
  432. case TargetTransformInfo::RGK_ScalableVector:
  433. return TypeSize::getScalable(0);
  434. }
  435. llvm_unreachable("Unsupported register kind");
  436. }
  437. unsigned PPCTTIImpl::getCacheLineSize() const {
  438. // Starting with P7 we have a cache line size of 128.
  439. unsigned Directive = ST->getCPUDirective();
  440. // Assume that Future CPU has the same cache line size as the others.
  441. if (Directive == PPC::DIR_PWR7 || Directive == PPC::DIR_PWR8 ||
  442. Directive == PPC::DIR_PWR9 || Directive == PPC::DIR_PWR10 ||
  443. Directive == PPC::DIR_PWR_FUTURE)
  444. return 128;
  445. // On other processors return a default of 64 bytes.
  446. return 64;
  447. }
  448. unsigned PPCTTIImpl::getPrefetchDistance() const {
  449. return 300;
  450. }
  451. unsigned PPCTTIImpl::getMaxInterleaveFactor(unsigned VF) {
  452. unsigned Directive = ST->getCPUDirective();
  453. // The 440 has no SIMD support, but floating-point instructions
  454. // have a 5-cycle latency, so unroll by 5x for latency hiding.
  455. if (Directive == PPC::DIR_440)
  456. return 5;
  457. // The A2 has no SIMD support, but floating-point instructions
  458. // have a 6-cycle latency, so unroll by 6x for latency hiding.
  459. if (Directive == PPC::DIR_A2)
  460. return 6;
  461. // FIXME: For lack of any better information, do no harm...
  462. if (Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500)
  463. return 1;
  464. // For P7 and P8, floating-point instructions have a 6-cycle latency and
  465. // there are two execution units, so unroll by 12x for latency hiding.
  466. // FIXME: the same for P9 as previous gen until POWER9 scheduling is ready
  467. // FIXME: the same for P10 as previous gen until POWER10 scheduling is ready
  468. // Assume that future is the same as the others.
  469. if (Directive == PPC::DIR_PWR7 || Directive == PPC::DIR_PWR8 ||
  470. Directive == PPC::DIR_PWR9 || Directive == PPC::DIR_PWR10 ||
  471. Directive == PPC::DIR_PWR_FUTURE)
  472. return 12;
  473. // For most things, modern systems have two execution units (and
  474. // out-of-order execution).
  475. return 2;
  476. }
  477. // Returns a cost adjustment factor to adjust the cost of vector instructions
  478. // on targets which there is overlap between the vector and scalar units,
  479. // thereby reducing the overall throughput of vector code wrt. scalar code.
  480. // An invalid instruction cost is returned if the type is an MMA vector type.
  481. InstructionCost PPCTTIImpl::vectorCostAdjustmentFactor(unsigned Opcode,
  482. Type *Ty1, Type *Ty2) {
  483. // If the vector type is of an MMA type (v256i1, v512i1), an invalid
  484. // instruction cost is returned. This is to signify to other cost computing
  485. // functions to return the maximum instruction cost in order to prevent any
  486. // opportunities for the optimizer to produce MMA types within the IR.
  487. if (isMMAType(Ty1))
  488. return InstructionCost::getInvalid();
  489. if (!ST->vectorsUseTwoUnits() || !Ty1->isVectorTy())
  490. return InstructionCost(1);
  491. std::pair<InstructionCost, MVT> LT1 = getTypeLegalizationCost(Ty1);
  492. // If type legalization involves splitting the vector, we don't want to
  493. // double the cost at every step - only the last step.
  494. if (LT1.first != 1 || !LT1.second.isVector())
  495. return InstructionCost(1);
  496. int ISD = TLI->InstructionOpcodeToISD(Opcode);
  497. if (TLI->isOperationExpand(ISD, LT1.second))
  498. return InstructionCost(1);
  499. if (Ty2) {
  500. std::pair<InstructionCost, MVT> LT2 = getTypeLegalizationCost(Ty2);
  501. if (LT2.first != 1 || !LT2.second.isVector())
  502. return InstructionCost(1);
  503. }
  504. return InstructionCost(2);
  505. }
  506. InstructionCost PPCTTIImpl::getArithmeticInstrCost(
  507. unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind,
  508. TTI::OperandValueInfo Op1Info, TTI::OperandValueInfo Op2Info,
  509. ArrayRef<const Value *> Args,
  510. const Instruction *CxtI) {
  511. assert(TLI->InstructionOpcodeToISD(Opcode) && "Invalid opcode");
  512. InstructionCost CostFactor = vectorCostAdjustmentFactor(Opcode, Ty, nullptr);
  513. if (!CostFactor.isValid())
  514. return InstructionCost::getMax();
  515. // TODO: Handle more cost kinds.
  516. if (CostKind != TTI::TCK_RecipThroughput)
  517. return BaseT::getArithmeticInstrCost(Opcode, Ty, CostKind, Op1Info,
  518. Op2Info, Args, CxtI);
  519. // Fallback to the default implementation.
  520. InstructionCost Cost = BaseT::getArithmeticInstrCost(
  521. Opcode, Ty, CostKind, Op1Info, Op2Info);
  522. return Cost * CostFactor;
  523. }
  524. InstructionCost PPCTTIImpl::getShuffleCost(TTI::ShuffleKind Kind, Type *Tp,
  525. ArrayRef<int> Mask,
  526. TTI::TargetCostKind CostKind,
  527. int Index, Type *SubTp,
  528. ArrayRef<const Value *> Args) {
  529. InstructionCost CostFactor =
  530. vectorCostAdjustmentFactor(Instruction::ShuffleVector, Tp, nullptr);
  531. if (!CostFactor.isValid())
  532. return InstructionCost::getMax();
  533. // Legalize the type.
  534. std::pair<InstructionCost, MVT> LT = getTypeLegalizationCost(Tp);
  535. // PPC, for both Altivec/VSX, support cheap arbitrary permutations
  536. // (at least in the sense that there need only be one non-loop-invariant
  537. // instruction). We need one such shuffle instruction for each actual
  538. // register (this is not true for arbitrary shuffles, but is true for the
  539. // structured types of shuffles covered by TTI::ShuffleKind).
  540. return LT.first * CostFactor;
  541. }
  542. InstructionCost PPCTTIImpl::getCFInstrCost(unsigned Opcode,
  543. TTI::TargetCostKind CostKind,
  544. const Instruction *I) {
  545. if (CostKind != TTI::TCK_RecipThroughput)
  546. return Opcode == Instruction::PHI ? 0 : 1;
  547. // Branches are assumed to be predicted.
  548. return 0;
  549. }
  550. InstructionCost PPCTTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst,
  551. Type *Src,
  552. TTI::CastContextHint CCH,
  553. TTI::TargetCostKind CostKind,
  554. const Instruction *I) {
  555. assert(TLI->InstructionOpcodeToISD(Opcode) && "Invalid opcode");
  556. InstructionCost CostFactor = vectorCostAdjustmentFactor(Opcode, Dst, Src);
  557. if (!CostFactor.isValid())
  558. return InstructionCost::getMax();
  559. InstructionCost Cost =
  560. BaseT::getCastInstrCost(Opcode, Dst, Src, CCH, CostKind, I);
  561. Cost *= CostFactor;
  562. // TODO: Allow non-throughput costs that aren't binary.
  563. if (CostKind != TTI::TCK_RecipThroughput)
  564. return Cost == 0 ? 0 : 1;
  565. return Cost;
  566. }
  567. InstructionCost PPCTTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
  568. Type *CondTy,
  569. CmpInst::Predicate VecPred,
  570. TTI::TargetCostKind CostKind,
  571. const Instruction *I) {
  572. InstructionCost CostFactor =
  573. vectorCostAdjustmentFactor(Opcode, ValTy, nullptr);
  574. if (!CostFactor.isValid())
  575. return InstructionCost::getMax();
  576. InstructionCost Cost =
  577. BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, VecPred, CostKind, I);
  578. // TODO: Handle other cost kinds.
  579. if (CostKind != TTI::TCK_RecipThroughput)
  580. return Cost;
  581. return Cost * CostFactor;
  582. }
  583. InstructionCost PPCTTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val,
  584. TTI::TargetCostKind CostKind,
  585. unsigned Index, Value *Op0,
  586. Value *Op1) {
  587. assert(Val->isVectorTy() && "This must be a vector type");
  588. int ISD = TLI->InstructionOpcodeToISD(Opcode);
  589. assert(ISD && "Invalid opcode");
  590. InstructionCost CostFactor = vectorCostAdjustmentFactor(Opcode, Val, nullptr);
  591. if (!CostFactor.isValid())
  592. return InstructionCost::getMax();
  593. InstructionCost Cost =
  594. BaseT::getVectorInstrCost(Opcode, Val, CostKind, Index, Op0, Op1);
  595. Cost *= CostFactor;
  596. if (ST->hasVSX() && Val->getScalarType()->isDoubleTy()) {
  597. // Double-precision scalars are already located in index #0 (or #1 if LE).
  598. if (ISD == ISD::EXTRACT_VECTOR_ELT &&
  599. Index == (ST->isLittleEndian() ? 1 : 0))
  600. return 0;
  601. return Cost;
  602. } else if (Val->getScalarType()->isIntegerTy() && Index != -1U) {
  603. if (ST->hasP9Altivec()) {
  604. if (ISD == ISD::INSERT_VECTOR_ELT)
  605. // A move-to VSR and a permute/insert. Assume vector operation cost
  606. // for both (cost will be 2x on P9).
  607. return 2 * CostFactor;
  608. // It's an extract. Maybe we can do a cheap move-from VSR.
  609. unsigned EltSize = Val->getScalarSizeInBits();
  610. if (EltSize == 64) {
  611. unsigned MfvsrdIndex = ST->isLittleEndian() ? 1 : 0;
  612. if (Index == MfvsrdIndex)
  613. return 1;
  614. } else if (EltSize == 32) {
  615. unsigned MfvsrwzIndex = ST->isLittleEndian() ? 2 : 1;
  616. if (Index == MfvsrwzIndex)
  617. return 1;
  618. }
  619. // We need a vector extract (or mfvsrld). Assume vector operation cost.
  620. // The cost of the load constant for a vector extract is disregarded
  621. // (invariant, easily schedulable).
  622. return CostFactor;
  623. } else if (ST->hasDirectMove())
  624. // Assume permute has standard cost.
  625. // Assume move-to/move-from VSR have 2x standard cost.
  626. return 3;
  627. }
  628. // Estimated cost of a load-hit-store delay. This was obtained
  629. // experimentally as a minimum needed to prevent unprofitable
  630. // vectorization for the paq8p benchmark. It may need to be
  631. // raised further if other unprofitable cases remain.
  632. unsigned LHSPenalty = 2;
  633. if (ISD == ISD::INSERT_VECTOR_ELT)
  634. LHSPenalty += 7;
  635. // Vector element insert/extract with Altivec is very expensive,
  636. // because they require store and reload with the attendant
  637. // processor stall for load-hit-store. Until VSX is available,
  638. // these need to be estimated as very costly.
  639. if (ISD == ISD::EXTRACT_VECTOR_ELT ||
  640. ISD == ISD::INSERT_VECTOR_ELT)
  641. return LHSPenalty + Cost;
  642. return Cost;
  643. }
  644. InstructionCost PPCTTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src,
  645. MaybeAlign Alignment,
  646. unsigned AddressSpace,
  647. TTI::TargetCostKind CostKind,
  648. TTI::OperandValueInfo OpInfo,
  649. const Instruction *I) {
  650. InstructionCost CostFactor = vectorCostAdjustmentFactor(Opcode, Src, nullptr);
  651. if (!CostFactor.isValid())
  652. return InstructionCost::getMax();
  653. if (TLI->getValueType(DL, Src, true) == MVT::Other)
  654. return BaseT::getMemoryOpCost(Opcode, Src, Alignment, AddressSpace,
  655. CostKind);
  656. // Legalize the type.
  657. std::pair<InstructionCost, MVT> LT = getTypeLegalizationCost(Src);
  658. assert((Opcode == Instruction::Load || Opcode == Instruction::Store) &&
  659. "Invalid Opcode");
  660. InstructionCost Cost =
  661. BaseT::getMemoryOpCost(Opcode, Src, Alignment, AddressSpace, CostKind);
  662. // TODO: Handle other cost kinds.
  663. if (CostKind != TTI::TCK_RecipThroughput)
  664. return Cost;
  665. Cost *= CostFactor;
  666. bool IsAltivecType = ST->hasAltivec() &&
  667. (LT.second == MVT::v16i8 || LT.second == MVT::v8i16 ||
  668. LT.second == MVT::v4i32 || LT.second == MVT::v4f32);
  669. bool IsVSXType = ST->hasVSX() &&
  670. (LT.second == MVT::v2f64 || LT.second == MVT::v2i64);
  671. // VSX has 32b/64b load instructions. Legalization can handle loading of
  672. // 32b/64b to VSR correctly and cheaply. But BaseT::getMemoryOpCost and
  673. // PPCTargetLowering can't compute the cost appropriately. So here we
  674. // explicitly check this case.
  675. unsigned MemBytes = Src->getPrimitiveSizeInBits();
  676. if (Opcode == Instruction::Load && ST->hasVSX() && IsAltivecType &&
  677. (MemBytes == 64 || (ST->hasP8Vector() && MemBytes == 32)))
  678. return 1;
  679. // Aligned loads and stores are easy.
  680. unsigned SrcBytes = LT.second.getStoreSize();
  681. if (!SrcBytes || !Alignment || *Alignment >= SrcBytes)
  682. return Cost;
  683. // If we can use the permutation-based load sequence, then this is also
  684. // relatively cheap (not counting loop-invariant instructions): one load plus
  685. // one permute (the last load in a series has extra cost, but we're
  686. // neglecting that here). Note that on the P7, we could do unaligned loads
  687. // for Altivec types using the VSX instructions, but that's more expensive
  688. // than using the permutation-based load sequence. On the P8, that's no
  689. // longer true.
  690. if (Opcode == Instruction::Load && (!ST->hasP8Vector() && IsAltivecType) &&
  691. *Alignment >= LT.second.getScalarType().getStoreSize())
  692. return Cost + LT.first; // Add the cost of the permutations.
  693. // For VSX, we can do unaligned loads and stores on Altivec/VSX types. On the
  694. // P7, unaligned vector loads are more expensive than the permutation-based
  695. // load sequence, so that might be used instead, but regardless, the net cost
  696. // is about the same (not counting loop-invariant instructions).
  697. if (IsVSXType || (ST->hasVSX() && IsAltivecType))
  698. return Cost;
  699. // Newer PPC supports unaligned memory access.
  700. if (TLI->allowsMisalignedMemoryAccesses(LT.second, 0))
  701. return Cost;
  702. // PPC in general does not support unaligned loads and stores. They'll need
  703. // to be decomposed based on the alignment factor.
  704. // Add the cost of each scalar load or store.
  705. assert(Alignment);
  706. Cost += LT.first * ((SrcBytes / Alignment->value()) - 1);
  707. // For a vector type, there is also scalarization overhead (only for
  708. // stores, loads are expanded using the vector-load + permutation sequence,
  709. // which is much less expensive).
  710. if (Src->isVectorTy() && Opcode == Instruction::Store)
  711. for (int i = 0, e = cast<FixedVectorType>(Src)->getNumElements(); i < e;
  712. ++i)
  713. Cost += getVectorInstrCost(Instruction::ExtractElement, Src, CostKind, i,
  714. nullptr, nullptr);
  715. return Cost;
  716. }
  717. InstructionCost PPCTTIImpl::getInterleavedMemoryOpCost(
  718. unsigned Opcode, Type *VecTy, unsigned Factor, ArrayRef<unsigned> Indices,
  719. Align Alignment, unsigned AddressSpace, TTI::TargetCostKind CostKind,
  720. bool UseMaskForCond, bool UseMaskForGaps) {
  721. InstructionCost CostFactor =
  722. vectorCostAdjustmentFactor(Opcode, VecTy, nullptr);
  723. if (!CostFactor.isValid())
  724. return InstructionCost::getMax();
  725. if (UseMaskForCond || UseMaskForGaps)
  726. return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
  727. Alignment, AddressSpace, CostKind,
  728. UseMaskForCond, UseMaskForGaps);
  729. assert(isa<VectorType>(VecTy) &&
  730. "Expect a vector type for interleaved memory op");
  731. // Legalize the type.
  732. std::pair<InstructionCost, MVT> LT = getTypeLegalizationCost(VecTy);
  733. // Firstly, the cost of load/store operation.
  734. InstructionCost Cost = getMemoryOpCost(Opcode, VecTy, MaybeAlign(Alignment),
  735. AddressSpace, CostKind);
  736. // PPC, for both Altivec/VSX, support cheap arbitrary permutations
  737. // (at least in the sense that there need only be one non-loop-invariant
  738. // instruction). For each result vector, we need one shuffle per incoming
  739. // vector (except that the first shuffle can take two incoming vectors
  740. // because it does not need to take itself).
  741. Cost += Factor*(LT.first-1);
  742. return Cost;
  743. }
  744. InstructionCost
  745. PPCTTIImpl::getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA,
  746. TTI::TargetCostKind CostKind) {
  747. return BaseT::getIntrinsicInstrCost(ICA, CostKind);
  748. }
  749. bool PPCTTIImpl::areTypesABICompatible(const Function *Caller,
  750. const Function *Callee,
  751. const ArrayRef<Type *> &Types) const {
  752. // We need to ensure that argument promotion does not
  753. // attempt to promote pointers to MMA types (__vector_pair
  754. // and __vector_quad) since these types explicitly cannot be
  755. // passed as arguments. Both of these types are larger than
  756. // the 128-bit Altivec vectors and have a scalar size of 1 bit.
  757. if (!BaseT::areTypesABICompatible(Caller, Callee, Types))
  758. return false;
  759. return llvm::none_of(Types, [](Type *Ty) {
  760. if (Ty->isSized())
  761. return Ty->isIntOrIntVectorTy(1) && Ty->getPrimitiveSizeInBits() > 128;
  762. return false;
  763. });
  764. }
  765. bool PPCTTIImpl::canSaveCmp(Loop *L, BranchInst **BI, ScalarEvolution *SE,
  766. LoopInfo *LI, DominatorTree *DT,
  767. AssumptionCache *AC, TargetLibraryInfo *LibInfo) {
  768. // Process nested loops first.
  769. for (Loop *I : *L)
  770. if (canSaveCmp(I, BI, SE, LI, DT, AC, LibInfo))
  771. return false; // Stop search.
  772. HardwareLoopInfo HWLoopInfo(L);
  773. if (!HWLoopInfo.canAnalyze(*LI))
  774. return false;
  775. if (!isHardwareLoopProfitable(L, *SE, *AC, LibInfo, HWLoopInfo))
  776. return false;
  777. if (!HWLoopInfo.isHardwareLoopCandidate(*SE, *LI, *DT))
  778. return false;
  779. *BI = HWLoopInfo.ExitBranch;
  780. return true;
  781. }
  782. bool PPCTTIImpl::isLSRCostLess(const TargetTransformInfo::LSRCost &C1,
  783. const TargetTransformInfo::LSRCost &C2) {
  784. // PowerPC default behaviour here is "instruction number 1st priority".
  785. // If LsrNoInsnsCost is set, call default implementation.
  786. if (!LsrNoInsnsCost)
  787. return std::tie(C1.Insns, C1.NumRegs, C1.AddRecCost, C1.NumIVMuls,
  788. C1.NumBaseAdds, C1.ScaleCost, C1.ImmCost, C1.SetupCost) <
  789. std::tie(C2.Insns, C2.NumRegs, C2.AddRecCost, C2.NumIVMuls,
  790. C2.NumBaseAdds, C2.ScaleCost, C2.ImmCost, C2.SetupCost);
  791. else
  792. return TargetTransformInfoImplBase::isLSRCostLess(C1, C2);
  793. }
  794. bool PPCTTIImpl::isNumRegsMajorCostOfLSR() {
  795. return false;
  796. }
  797. bool PPCTTIImpl::shouldBuildRelLookupTables() const {
  798. const PPCTargetMachine &TM = ST->getTargetMachine();
  799. // XCOFF hasn't implemented lowerRelativeReference, disable non-ELF for now.
  800. if (!TM.isELFv2ABI())
  801. return false;
  802. return BaseT::shouldBuildRelLookupTables();
  803. }
  804. bool PPCTTIImpl::getTgtMemIntrinsic(IntrinsicInst *Inst,
  805. MemIntrinsicInfo &Info) {
  806. switch (Inst->getIntrinsicID()) {
  807. case Intrinsic::ppc_altivec_lvx:
  808. case Intrinsic::ppc_altivec_lvxl:
  809. case Intrinsic::ppc_altivec_lvebx:
  810. case Intrinsic::ppc_altivec_lvehx:
  811. case Intrinsic::ppc_altivec_lvewx:
  812. case Intrinsic::ppc_vsx_lxvd2x:
  813. case Intrinsic::ppc_vsx_lxvw4x:
  814. case Intrinsic::ppc_vsx_lxvd2x_be:
  815. case Intrinsic::ppc_vsx_lxvw4x_be:
  816. case Intrinsic::ppc_vsx_lxvl:
  817. case Intrinsic::ppc_vsx_lxvll:
  818. case Intrinsic::ppc_vsx_lxvp: {
  819. Info.PtrVal = Inst->getArgOperand(0);
  820. Info.ReadMem = true;
  821. Info.WriteMem = false;
  822. return true;
  823. }
  824. case Intrinsic::ppc_altivec_stvx:
  825. case Intrinsic::ppc_altivec_stvxl:
  826. case Intrinsic::ppc_altivec_stvebx:
  827. case Intrinsic::ppc_altivec_stvehx:
  828. case Intrinsic::ppc_altivec_stvewx:
  829. case Intrinsic::ppc_vsx_stxvd2x:
  830. case Intrinsic::ppc_vsx_stxvw4x:
  831. case Intrinsic::ppc_vsx_stxvd2x_be:
  832. case Intrinsic::ppc_vsx_stxvw4x_be:
  833. case Intrinsic::ppc_vsx_stxvl:
  834. case Intrinsic::ppc_vsx_stxvll:
  835. case Intrinsic::ppc_vsx_stxvp: {
  836. Info.PtrVal = Inst->getArgOperand(1);
  837. Info.ReadMem = false;
  838. Info.WriteMem = true;
  839. return true;
  840. }
  841. case Intrinsic::ppc_stbcx:
  842. case Intrinsic::ppc_sthcx:
  843. case Intrinsic::ppc_stdcx:
  844. case Intrinsic::ppc_stwcx: {
  845. Info.PtrVal = Inst->getArgOperand(0);
  846. Info.ReadMem = false;
  847. Info.WriteMem = true;
  848. return true;
  849. }
  850. default:
  851. break;
  852. }
  853. return false;
  854. }
  855. bool PPCTTIImpl::hasActiveVectorLength(unsigned Opcode, Type *DataType,
  856. Align Alignment) const {
  857. // Only load and stores instructions can have variable vector length on Power.
  858. if (Opcode != Instruction::Load && Opcode != Instruction::Store)
  859. return false;
  860. // Loads/stores with length instructions use bits 0-7 of the GPR operand and
  861. // therefore cannot be used in 32-bit mode.
  862. if ((!ST->hasP9Vector() && !ST->hasP10Vector()) || !ST->isPPC64())
  863. return false;
  864. if (isa<FixedVectorType>(DataType)) {
  865. unsigned VecWidth = DataType->getPrimitiveSizeInBits();
  866. return VecWidth == 128;
  867. }
  868. Type *ScalarTy = DataType->getScalarType();
  869. if (ScalarTy->isPointerTy())
  870. return true;
  871. if (ScalarTy->isFloatTy() || ScalarTy->isDoubleTy())
  872. return true;
  873. if (!ScalarTy->isIntegerTy())
  874. return false;
  875. unsigned IntWidth = ScalarTy->getIntegerBitWidth();
  876. return IntWidth == 8 || IntWidth == 16 || IntWidth == 32 || IntWidth == 64;
  877. }
  878. InstructionCost PPCTTIImpl::getVPMemoryOpCost(unsigned Opcode, Type *Src,
  879. Align Alignment,
  880. unsigned AddressSpace,
  881. TTI::TargetCostKind CostKind,
  882. const Instruction *I) {
  883. InstructionCost Cost = BaseT::getVPMemoryOpCost(Opcode, Src, Alignment,
  884. AddressSpace, CostKind, I);
  885. if (TLI->getValueType(DL, Src, true) == MVT::Other)
  886. return Cost;
  887. // TODO: Handle other cost kinds.
  888. if (CostKind != TTI::TCK_RecipThroughput)
  889. return Cost;
  890. assert((Opcode == Instruction::Load || Opcode == Instruction::Store) &&
  891. "Invalid Opcode");
  892. auto *SrcVTy = dyn_cast<FixedVectorType>(Src);
  893. assert(SrcVTy && "Expected a vector type for VP memory operations");
  894. if (hasActiveVectorLength(Opcode, Src, Alignment)) {
  895. std::pair<InstructionCost, MVT> LT = getTypeLegalizationCost(SrcVTy);
  896. InstructionCost CostFactor =
  897. vectorCostAdjustmentFactor(Opcode, Src, nullptr);
  898. if (!CostFactor.isValid())
  899. return InstructionCost::getMax();
  900. InstructionCost Cost = LT.first * CostFactor;
  901. assert(Cost.isValid() && "Expected valid cost");
  902. // On P9 but not on P10, if the op is misaligned then it will cause a
  903. // pipeline flush. Otherwise the VSX masked memops cost the same as unmasked
  904. // ones.
  905. const Align DesiredAlignment(16);
  906. if (Alignment >= DesiredAlignment || ST->getCPUDirective() != PPC::DIR_PWR9)
  907. return Cost;
  908. // Since alignment may be under estimated, we try to compute the probability
  909. // that the actual address is aligned to the desired boundary. For example
  910. // an 8-byte aligned load is assumed to be actually 16-byte aligned half the
  911. // time, while a 4-byte aligned load has a 25% chance of being 16-byte
  912. // aligned.
  913. float AlignmentProb = ((float)Alignment.value()) / DesiredAlignment.value();
  914. float MisalignmentProb = 1.0 - AlignmentProb;
  915. return (MisalignmentProb * P9PipelineFlushEstimate) +
  916. (AlignmentProb * *Cost.getValue());
  917. }
  918. // Usually we should not get to this point, but the following is an attempt to
  919. // model the cost of legalization. Currently we can only lower intrinsics with
  920. // evl but no mask, on Power 9/10. Otherwise, we must scalarize.
  921. return getMaskedMemoryOpCost(Opcode, Src, Alignment, AddressSpace, CostKind);
  922. }
  923. bool PPCTTIImpl::supportsTailCallFor(const CallBase *CB) const {
  924. // Subtargets using PC-Relative addressing supported.
  925. if (ST->isUsingPCRelativeCalls())
  926. return true;
  927. const Function *Callee = CB->getCalledFunction();
  928. // Indirect calls and variadic argument functions not supported.
  929. if (!Callee || Callee->isVarArg())
  930. return false;
  931. const Function *Caller = CB->getCaller();
  932. // Support if we can share TOC base.
  933. return ST->getTargetMachine().shouldAssumeDSOLocal(*Caller->getParent(),
  934. Callee);
  935. }