Chandler Carruth | 93dcdc4 | 2015-01-31 11:17:59 +0000 | [diff] [blame] | 1 | //===-- PPCTargetTransformInfo.cpp - PPC specific TTI ---------------------===// |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 9 | |
Chandler Carruth | 93dcdc4 | 2015-01-31 11:17:59 +0000 | [diff] [blame] | 10 | #include "PPCTargetTransformInfo.h" |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 11 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 12 | #include "llvm/CodeGen/BasicTTIImpl.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 13 | #include "llvm/CodeGen/CostTable.h" |
| 14 | #include "llvm/CodeGen/TargetLowering.h" |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 15 | #include "llvm/Support/CommandLine.h" |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Debug.h" |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 17 | using namespace llvm; |
| 18 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 19 | #define DEBUG_TYPE "ppctti" |
| 20 | |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 21 | static cl::opt<bool> DisablePPCConstHoist("disable-ppc-constant-hoisting", |
| 22 | cl::desc("disable constant hoisting on PPC"), cl::init(false), cl::Hidden); |
| 23 | |
Adam Nemet | af76110 | 2016-01-21 18:28:36 +0000 | [diff] [blame] | 24 | // This is currently only used for the data prefetch pass which is only enabled |
| 25 | // for BG/Q by default. |
| 26 | static cl::opt<unsigned> |
| 27 | CacheLineSize("ppc-loop-prefetch-cache-line", cl::Hidden, cl::init(64), |
| 28 | cl::desc("The loop prefetch cache line size")); |
| 29 | |
Zaara Syeda | 1f59ae3 | 2018-01-30 16:17:22 +0000 | [diff] [blame] | 30 | static cl::opt<bool> |
| 31 | EnablePPCColdCC("ppc-enable-coldcc", cl::Hidden, cl::init(false), |
| 32 | cl::desc("Enable using coldcc calling conv for cold " |
| 33 | "internal functions")); |
| 34 | |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 35 | //===----------------------------------------------------------------------===// |
| 36 | // |
| 37 | // PPC cost model. |
| 38 | // |
| 39 | //===----------------------------------------------------------------------===// |
| 40 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 41 | TargetTransformInfo::PopcntSupportKind |
| 42 | PPCTTIImpl::getPopcntSupport(unsigned TyWidth) { |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 43 | assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2"); |
Hal Finkel | fa7057a | 2016-03-29 01:36:01 +0000 | [diff] [blame] | 44 | if (ST->hasPOPCNTD() != PPCSubtarget::POPCNTD_Unavailable && TyWidth <= 64) |
| 45 | return ST->hasPOPCNTD() == PPCSubtarget::POPCNTD_Slow ? |
| 46 | TTI::PSK_SlowHardware : TTI::PSK_FastHardware; |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 47 | return TTI::PSK_Software; |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 50 | int PPCTTIImpl::getIntImmCost(const APInt &Imm, Type *Ty) { |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 51 | if (DisablePPCConstHoist) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 52 | return BaseT::getIntImmCost(Imm, Ty); |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 53 | |
| 54 | assert(Ty->isIntegerTy()); |
| 55 | |
| 56 | unsigned BitSize = Ty->getPrimitiveSizeInBits(); |
| 57 | if (BitSize == 0) |
| 58 | return ~0U; |
| 59 | |
| 60 | if (Imm == 0) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 61 | return TTI::TCC_Free; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 62 | |
| 63 | if (Imm.getBitWidth() <= 64) { |
| 64 | if (isInt<16>(Imm.getSExtValue())) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 65 | return TTI::TCC_Basic; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 66 | |
| 67 | if (isInt<32>(Imm.getSExtValue())) { |
| 68 | // A constant that can be materialized using lis. |
| 69 | if ((Imm.getZExtValue() & 0xFFFF) == 0) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 70 | return TTI::TCC_Basic; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 71 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 72 | return 2 * TTI::TCC_Basic; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 76 | return 4 * TTI::TCC_Basic; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 79 | int PPCTTIImpl::getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm, |
| 80 | Type *Ty) { |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 81 | if (DisablePPCConstHoist) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 82 | return BaseT::getIntImmCost(IID, Idx, Imm, Ty); |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 83 | |
| 84 | assert(Ty->isIntegerTy()); |
| 85 | |
| 86 | unsigned BitSize = Ty->getPrimitiveSizeInBits(); |
| 87 | if (BitSize == 0) |
| 88 | return ~0U; |
| 89 | |
| 90 | switch (IID) { |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 91 | default: |
| 92 | return TTI::TCC_Free; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 93 | case Intrinsic::sadd_with_overflow: |
| 94 | case Intrinsic::uadd_with_overflow: |
| 95 | case Intrinsic::ssub_with_overflow: |
| 96 | case Intrinsic::usub_with_overflow: |
| 97 | if ((Idx == 1) && Imm.getBitWidth() <= 64 && isInt<16>(Imm.getSExtValue())) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 98 | return TTI::TCC_Free; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 99 | break; |
Hal Finkel | 934361a | 2015-01-14 01:07:51 +0000 | [diff] [blame] | 100 | case Intrinsic::experimental_stackmap: |
| 101 | if ((Idx < 2) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue()))) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 102 | return TTI::TCC_Free; |
Hal Finkel | 934361a | 2015-01-14 01:07:51 +0000 | [diff] [blame] | 103 | break; |
| 104 | case Intrinsic::experimental_patchpoint_void: |
| 105 | case Intrinsic::experimental_patchpoint_i64: |
| 106 | if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue()))) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 107 | return TTI::TCC_Free; |
Hal Finkel | 934361a | 2015-01-14 01:07:51 +0000 | [diff] [blame] | 108 | break; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 109 | } |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 110 | return PPCTTIImpl::getIntImmCost(Imm, Ty); |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 113 | int PPCTTIImpl::getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm, |
| 114 | Type *Ty) { |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 115 | if (DisablePPCConstHoist) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 116 | return BaseT::getIntImmCost(Opcode, Idx, Imm, Ty); |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 117 | |
| 118 | assert(Ty->isIntegerTy()); |
| 119 | |
| 120 | unsigned BitSize = Ty->getPrimitiveSizeInBits(); |
| 121 | if (BitSize == 0) |
| 122 | return ~0U; |
| 123 | |
| 124 | unsigned ImmIdx = ~0U; |
| 125 | bool ShiftedFree = false, RunFree = false, UnsignedFree = false, |
| 126 | ZeroFree = false; |
| 127 | switch (Opcode) { |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 128 | default: |
| 129 | return TTI::TCC_Free; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 130 | case Instruction::GetElementPtr: |
| 131 | // Always hoist the base address of a GetElementPtr. This prevents the |
| 132 | // creation of new constants for every base constant that gets constant |
| 133 | // folded with the offset. |
| 134 | if (Idx == 0) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 135 | return 2 * TTI::TCC_Basic; |
| 136 | return TTI::TCC_Free; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 137 | case Instruction::And: |
| 138 | RunFree = true; // (for the rotate-and-mask instructions) |
Justin Bogner | b03fd12 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 139 | LLVM_FALLTHROUGH; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 140 | case Instruction::Add: |
| 141 | case Instruction::Or: |
| 142 | case Instruction::Xor: |
| 143 | ShiftedFree = true; |
Justin Bogner | b03fd12 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 144 | LLVM_FALLTHROUGH; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 145 | case Instruction::Sub: |
| 146 | case Instruction::Mul: |
| 147 | case Instruction::Shl: |
| 148 | case Instruction::LShr: |
| 149 | case Instruction::AShr: |
| 150 | ImmIdx = 1; |
| 151 | break; |
| 152 | case Instruction::ICmp: |
| 153 | UnsignedFree = true; |
| 154 | ImmIdx = 1; |
Justin Bogner | b03fd12 | 2016-08-17 05:10:15 +0000 | [diff] [blame] | 155 | // Zero comparisons can use record-form instructions. |
| 156 | LLVM_FALLTHROUGH; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 157 | case Instruction::Select: |
| 158 | ZeroFree = true; |
| 159 | break; |
| 160 | case Instruction::PHI: |
| 161 | case Instruction::Call: |
| 162 | case Instruction::Ret: |
| 163 | case Instruction::Load: |
| 164 | case Instruction::Store: |
| 165 | break; |
| 166 | } |
| 167 | |
| 168 | if (ZeroFree && Imm == 0) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 169 | return TTI::TCC_Free; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 170 | |
| 171 | if (Idx == ImmIdx && Imm.getBitWidth() <= 64) { |
| 172 | if (isInt<16>(Imm.getSExtValue())) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 173 | return TTI::TCC_Free; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 174 | |
| 175 | if (RunFree) { |
| 176 | if (Imm.getBitWidth() <= 32 && |
| 177 | (isShiftedMask_32(Imm.getZExtValue()) || |
| 178 | isShiftedMask_32(~Imm.getZExtValue()))) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 179 | return TTI::TCC_Free; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 180 | |
| 181 | if (ST->isPPC64() && |
| 182 | (isShiftedMask_64(Imm.getZExtValue()) || |
| 183 | isShiftedMask_64(~Imm.getZExtValue()))) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 184 | return TTI::TCC_Free; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | if (UnsignedFree && isUInt<16>(Imm.getZExtValue())) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 188 | return TTI::TCC_Free; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 189 | |
| 190 | if (ShiftedFree && (Imm.getZExtValue() & 0xFFFF) == 0) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 191 | return TTI::TCC_Free; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 194 | return PPCTTIImpl::getIntImmCost(Imm, Ty); |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Graham Yiu | 488782e | 2017-10-19 18:16:31 +0000 | [diff] [blame] | 197 | unsigned PPCTTIImpl::getUserCost(const User *U, |
| 198 | ArrayRef<const Value *> Operands) { |
| 199 | if (U->getType()->isVectorTy()) { |
| 200 | // Instructions that need to be split should cost more. |
| 201 | std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, U->getType()); |
| 202 | return LT.first * BaseT::getUserCost(U, Operands); |
| 203 | } |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 204 | |
Graham Yiu | 488782e | 2017-10-19 18:16:31 +0000 | [diff] [blame] | 205 | return BaseT::getUserCost(U, Operands); |
| 206 | } |
| 207 | |
Geoff Berry | 66d9bdb | 2017-06-28 15:53:17 +0000 | [diff] [blame] | 208 | void PPCTTIImpl::getUnrollingPreferences(Loop *L, ScalarEvolution &SE, |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 209 | TTI::UnrollingPreferences &UP) { |
Chandler Carruth | c956ab66 | 2015-02-01 14:22:17 +0000 | [diff] [blame] | 210 | if (ST->getDarwinDirective() == PPC::DIR_A2) { |
Hal Finkel | 71780ec | 2013-09-11 21:20:40 +0000 | [diff] [blame] | 211 | // The A2 is in-order with a deep pipeline, and concatenation unrolling |
| 212 | // helps expose latency-hiding opportunities to the instruction scheduler. |
| 213 | UP.Partial = UP.Runtime = true; |
Hal Finkel | 3b3c9c3 | 2015-05-21 20:30:23 +0000 | [diff] [blame] | 214 | |
| 215 | // We unroll a lot on the A2 (hundreds of instructions), and the benefits |
| 216 | // often outweigh the cost of a division to compute the trip count. |
| 217 | UP.AllowExpensiveTripCount = true; |
Hal Finkel | 71780ec | 2013-09-11 21:20:40 +0000 | [diff] [blame] | 218 | } |
Hal Finkel | b359b73 | 2015-01-09 15:51:16 +0000 | [diff] [blame] | 219 | |
Geoff Berry | 66d9bdb | 2017-06-28 15:53:17 +0000 | [diff] [blame] | 220 | BaseT::getUnrollingPreferences(L, SE, UP); |
Hal Finkel | 71780ec | 2013-09-11 21:20:40 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Zaara Syeda | 1f59ae3 | 2018-01-30 16:17:22 +0000 | [diff] [blame] | 223 | // This function returns true to allow using coldcc calling convention. |
| 224 | // Returning true results in coldcc being used for functions which are cold at |
| 225 | // all call sites when the callers of the functions are not calling any other |
| 226 | // non coldcc functions. |
| 227 | bool PPCTTIImpl::useColdCCForColdCall(Function &F) { |
| 228 | return EnablePPCColdCC; |
| 229 | } |
| 230 | |
Olivier Sallenave | 049d803 | 2015-03-06 23:12:04 +0000 | [diff] [blame] | 231 | bool PPCTTIImpl::enableAggressiveInterleaving(bool LoopHasReductions) { |
Hal Finkel | 75afa2b | 2015-09-03 23:23:00 +0000 | [diff] [blame] | 232 | // On the A2, always unroll aggressively. For QPX unaligned loads, we depend |
| 233 | // on combining the loads generated for consecutive accesses, and failure to |
| 234 | // do so is particularly expensive. This makes it much more likely (compared |
| 235 | // to only using concatenation unrolling). |
| 236 | if (ST->getDarwinDirective() == PPC::DIR_A2) |
| 237 | return true; |
| 238 | |
Olivier Sallenave | 049d803 | 2015-03-06 23:12:04 +0000 | [diff] [blame] | 239 | return LoopHasReductions; |
| 240 | } |
| 241 | |
Clement Courbet | b2c3eb8 | 2017-10-30 14:19:33 +0000 | [diff] [blame] | 242 | const PPCTTIImpl::TTI::MemCmpExpansionOptions * |
| 243 | PPCTTIImpl::enableMemCmpExpansion(bool IsZeroCmp) const { |
| 244 | static const auto Options = []() { |
| 245 | TTI::MemCmpExpansionOptions Options; |
| 246 | Options.LoadSizes.push_back(8); |
| 247 | Options.LoadSizes.push_back(4); |
| 248 | Options.LoadSizes.push_back(2); |
| 249 | Options.LoadSizes.push_back(1); |
| 250 | return Options; |
| 251 | }(); |
| 252 | return &Options; |
Zaara Syeda | 3a7578c | 2017-05-31 17:12:38 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Hal Finkel | 4a7be23 | 2015-09-04 00:10:41 +0000 | [diff] [blame] | 255 | bool PPCTTIImpl::enableInterleavedAccessVectorization() { |
| 256 | return true; |
| 257 | } |
| 258 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 259 | unsigned PPCTTIImpl::getNumberOfRegisters(bool Vector) { |
Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 260 | if (Vector && !ST->hasAltivec() && !ST->hasQPX()) |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 261 | return 0; |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 262 | return ST->hasVSX() ? 64 : 32; |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Daniel Neilson | c0112ae | 2017-06-12 14:22:21 +0000 | [diff] [blame] | 265 | unsigned PPCTTIImpl::getRegisterBitWidth(bool Vector) const { |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 266 | if (Vector) { |
Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 267 | if (ST->hasQPX()) return 256; |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 268 | if (ST->hasAltivec()) return 128; |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | if (ST->isPPC64()) |
| 273 | return 64; |
| 274 | return 32; |
| 275 | |
| 276 | } |
| 277 | |
Adam Nemet | af76110 | 2016-01-21 18:28:36 +0000 | [diff] [blame] | 278 | unsigned PPCTTIImpl::getCacheLineSize() { |
Sean Fertile | 457ddd3 | 2017-05-31 18:20:17 +0000 | [diff] [blame] | 279 | // Check first if the user specified a custom line size. |
| 280 | if (CacheLineSize.getNumOccurrences() > 0) |
| 281 | return CacheLineSize; |
| 282 | |
| 283 | // On P7, P8 or P9 we have a cache line size of 128. |
| 284 | unsigned Directive = ST->getDarwinDirective(); |
| 285 | if (Directive == PPC::DIR_PWR7 || Directive == PPC::DIR_PWR8 || |
| 286 | Directive == PPC::DIR_PWR9) |
| 287 | return 128; |
| 288 | |
| 289 | // On other processors return a default of 64 bytes. |
| 290 | return 64; |
Adam Nemet | af76110 | 2016-01-21 18:28:36 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Adam Nemet | b81f1e0 | 2016-03-29 23:45:56 +0000 | [diff] [blame] | 293 | unsigned PPCTTIImpl::getPrefetchDistance() { |
| 294 | // This seems like a reasonable default for the BG/Q (this pass is enabled, by |
| 295 | // default, only on the BG/Q). |
| 296 | return 300; |
| 297 | } |
Adam Nemet | dadfbb5 | 2016-01-27 22:21:25 +0000 | [diff] [blame] | 298 | |
Wei Mi | 062c744 | 2015-05-06 17:12:25 +0000 | [diff] [blame] | 299 | unsigned PPCTTIImpl::getMaxInterleaveFactor(unsigned VF) { |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 300 | unsigned Directive = ST->getDarwinDirective(); |
| 301 | // The 440 has no SIMD support, but floating-point instructions |
| 302 | // have a 5-cycle latency, so unroll by 5x for latency hiding. |
| 303 | if (Directive == PPC::DIR_440) |
| 304 | return 5; |
| 305 | |
| 306 | // The A2 has no SIMD support, but floating-point instructions |
| 307 | // have a 6-cycle latency, so unroll by 6x for latency hiding. |
| 308 | if (Directive == PPC::DIR_A2) |
| 309 | return 6; |
| 310 | |
| 311 | // FIXME: For lack of any better information, do no harm... |
| 312 | if (Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500) |
| 313 | return 1; |
| 314 | |
Olivier Sallenave | 05e6915 | 2015-02-12 22:57:58 +0000 | [diff] [blame] | 315 | // For P7 and P8, floating-point instructions have a 6-cycle latency and |
| 316 | // there are two execution units, so unroll by 12x for latency hiding. |
Nemanja Ivanovic | 6e29baf | 2016-05-09 18:54:58 +0000 | [diff] [blame] | 317 | // FIXME: the same for P9 as previous gen until POWER9 scheduling is ready |
| 318 | if (Directive == PPC::DIR_PWR7 || Directive == PPC::DIR_PWR8 || |
| 319 | Directive == PPC::DIR_PWR9) |
Olivier Sallenave | 05e6915 | 2015-02-12 22:57:58 +0000 | [diff] [blame] | 320 | return 12; |
| 321 | |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 322 | // For most things, modern systems have two execution units (and |
| 323 | // out-of-order execution). |
| 324 | return 2; |
| 325 | } |
| 326 | |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 327 | int PPCTTIImpl::getArithmeticInstrCost( |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 328 | unsigned Opcode, Type *Ty, TTI::OperandValueKind Op1Info, |
| 329 | TTI::OperandValueKind Op2Info, TTI::OperandValueProperties Opd1PropInfo, |
Mohammed Agabaria | 2c96c43 | 2017-01-11 08:23:37 +0000 | [diff] [blame] | 330 | TTI::OperandValueProperties Opd2PropInfo, ArrayRef<const Value *> Args) { |
Dmitri Gribenko | c451bdf | 2013-01-25 23:17:21 +0000 | [diff] [blame] | 331 | assert(TLI->InstructionOpcodeToISD(Opcode) && "Invalid opcode"); |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 332 | |
| 333 | // Fallback to the default implementation. |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 334 | return BaseT::getArithmeticInstrCost(Opcode, Ty, Op1Info, Op2Info, |
| 335 | Opd1PropInfo, Opd2PropInfo); |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 338 | int PPCTTIImpl::getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index, |
| 339 | Type *SubTp) { |
Hal Finkel | 4a7be23 | 2015-09-04 00:10:41 +0000 | [diff] [blame] | 340 | // Legalize the type. |
| 341 | std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp); |
| 342 | |
| 343 | // PPC, for both Altivec/VSX and QPX, support cheap arbitrary permutations |
| 344 | // (at least in the sense that there need only be one non-loop-invariant |
| 345 | // instruction). We need one such shuffle instruction for each actual |
| 346 | // register (this is not true for arbitrary shuffles, but is true for the |
| 347 | // structured types of shuffles covered by TTI::ShuffleKind). |
| 348 | return LT.first; |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Jonas Paulsson | fccc7d6 | 2017-04-12 11:49:08 +0000 | [diff] [blame] | 351 | int PPCTTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src, |
| 352 | const Instruction *I) { |
Dmitri Gribenko | c451bdf | 2013-01-25 23:17:21 +0000 | [diff] [blame] | 353 | assert(TLI->InstructionOpcodeToISD(Opcode) && "Invalid opcode"); |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 354 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 355 | return BaseT::getCastInstrCost(Opcode, Dst, Src); |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Jonas Paulsson | fccc7d6 | 2017-04-12 11:49:08 +0000 | [diff] [blame] | 358 | int PPCTTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy, |
| 359 | const Instruction *I) { |
| 360 | return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, I); |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 363 | int PPCTTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index) { |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 364 | assert(Val->isVectorTy() && "This must be a vector type"); |
| 365 | |
Bill Schmidt | 62fe7a5b | 2013-02-08 18:19:17 +0000 | [diff] [blame] | 366 | int ISD = TLI->InstructionOpcodeToISD(Opcode); |
| 367 | assert(ISD && "Invalid opcode"); |
Bill Schmidt | b3cece1 | 2013-02-07 20:33:57 +0000 | [diff] [blame] | 368 | |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 369 | if (ST->hasVSX() && Val->getScalarType()->isDoubleTy()) { |
| 370 | // Double-precision scalars are already located in index #0. |
| 371 | if (Index == 0) |
| 372 | return 0; |
| 373 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 374 | return BaseT::getVectorInstrCost(Opcode, Val, Index); |
Hal Finkel | c93a9a2 | 2015-02-25 01:06:45 +0000 | [diff] [blame] | 375 | } else if (ST->hasQPX() && Val->getScalarType()->isFloatingPointTy()) { |
| 376 | // Floating point scalars are already located in index #0. |
| 377 | if (Index == 0) |
| 378 | return 0; |
| 379 | |
| 380 | return BaseT::getVectorInstrCost(Opcode, Val, Index); |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Bill Schmidt | 62fe7a5b | 2013-02-08 18:19:17 +0000 | [diff] [blame] | 383 | // Estimated cost of a load-hit-store delay. This was obtained |
| 384 | // experimentally as a minimum needed to prevent unprofitable |
| 385 | // vectorization for the paq8p benchmark. It may need to be |
| 386 | // raised further if other unprofitable cases remain. |
Hal Finkel | de0b413 | 2014-04-04 23:51:18 +0000 | [diff] [blame] | 387 | unsigned LHSPenalty = 2; |
| 388 | if (ISD == ISD::INSERT_VECTOR_ELT) |
| 389 | LHSPenalty += 7; |
Bill Schmidt | b3cece1 | 2013-02-07 20:33:57 +0000 | [diff] [blame] | 390 | |
Bill Schmidt | 62fe7a5b | 2013-02-08 18:19:17 +0000 | [diff] [blame] | 391 | // Vector element insert/extract with Altivec is very expensive, |
| 392 | // because they require store and reload with the attendant |
| 393 | // processor stall for load-hit-store. Until VSX is available, |
| 394 | // these need to be estimated as very costly. |
| 395 | if (ISD == ISD::EXTRACT_VECTOR_ELT || |
| 396 | ISD == ISD::INSERT_VECTOR_ELT) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 397 | return LHSPenalty + BaseT::getVectorInstrCost(Opcode, Val, Index); |
Bill Schmidt | b3cece1 | 2013-02-07 20:33:57 +0000 | [diff] [blame] | 398 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame] | 399 | return BaseT::getVectorInstrCost(Opcode, Val, Index); |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 402 | int PPCTTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment, |
Jonas Paulsson | fccc7d6 | 2017-04-12 11:49:08 +0000 | [diff] [blame] | 403 | unsigned AddressSpace, const Instruction *I) { |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 404 | // Legalize the type. |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 405 | std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Src); |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 406 | assert((Opcode == Instruction::Load || Opcode == Instruction::Store) && |
| 407 | "Invalid Opcode"); |
| 408 | |
Chandler Carruth | 93205eb | 2015-08-05 18:08:10 +0000 | [diff] [blame] | 409 | int Cost = BaseT::getMemoryOpCost(Opcode, Src, Alignment, AddressSpace); |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 410 | |
Hal Finkel | 79dbf5b | 2015-09-02 21:03:28 +0000 | [diff] [blame] | 411 | bool IsAltivecType = ST->hasAltivec() && |
| 412 | (LT.second == MVT::v16i8 || LT.second == MVT::v8i16 || |
| 413 | LT.second == MVT::v4i32 || LT.second == MVT::v4f32); |
| 414 | bool IsVSXType = ST->hasVSX() && |
| 415 | (LT.second == MVT::v2f64 || LT.second == MVT::v2i64); |
| 416 | bool IsQPXType = ST->hasQPX() && |
| 417 | (LT.second == MVT::v4f64 || LT.second == MVT::v4f32); |
| 418 | |
Guozhi Wei | 835de1f | 2016-12-03 00:41:43 +0000 | [diff] [blame] | 419 | // VSX has 32b/64b load instructions. Legalization can handle loading of |
| 420 | // 32b/64b to VSR correctly and cheaply. But BaseT::getMemoryOpCost and |
| 421 | // PPCTargetLowering can't compute the cost appropriately. So here we |
| 422 | // explicitly check this case. |
| 423 | unsigned MemBytes = Src->getPrimitiveSizeInBits(); |
| 424 | if (Opcode == Instruction::Load && ST->hasVSX() && IsAltivecType && |
| 425 | (MemBytes == 64 || (ST->hasP8Vector() && MemBytes == 32))) |
| 426 | return 1; |
| 427 | |
| 428 | // Aligned loads and stores are easy. |
| 429 | unsigned SrcBytes = LT.second.getStoreSize(); |
| 430 | if (!SrcBytes || !Alignment || Alignment >= SrcBytes) |
| 431 | return Cost; |
| 432 | |
Hal Finkel | f11bc76 | 2015-09-03 21:23:18 +0000 | [diff] [blame] | 433 | // If we can use the permutation-based load sequence, then this is also |
| 434 | // relatively cheap (not counting loop-invariant instructions): one load plus |
| 435 | // one permute (the last load in a series has extra cost, but we're |
Hal Finkel | 69ada2f | 2016-03-28 22:39:35 +0000 | [diff] [blame] | 436 | // neglecting that here). Note that on the P7, we could do unaligned loads |
Hal Finkel | f11bc76 | 2015-09-03 21:23:18 +0000 | [diff] [blame] | 437 | // for Altivec types using the VSX instructions, but that's more expensive |
| 438 | // than using the permutation-based load sequence. On the P8, that's no |
| 439 | // longer true. |
| 440 | if (Opcode == Instruction::Load && |
| 441 | ((!ST->hasP8Vector() && IsAltivecType) || IsQPXType) && |
| 442 | Alignment >= LT.second.getScalarType().getStoreSize()) |
| 443 | return Cost + LT.first; // Add the cost of the permutations. |
| 444 | |
Hal Finkel | 79dbf5b | 2015-09-02 21:03:28 +0000 | [diff] [blame] | 445 | // For VSX, we can do unaligned loads and stores on Altivec/VSX types. On the |
| 446 | // P7, unaligned vector loads are more expensive than the permutation-based |
| 447 | // load sequence, so that might be used instead, but regardless, the net cost |
| 448 | // is about the same (not counting loop-invariant instructions). |
| 449 | if (IsVSXType || (ST->hasVSX() && IsAltivecType)) |
| 450 | return Cost; |
| 451 | |
Guozhi Wei | 7ec2c72 | 2017-02-17 22:29:39 +0000 | [diff] [blame] | 452 | // Newer PPC supports unaligned memory access. |
| 453 | if (TLI->allowsMisalignedMemoryAccesses(LT.second, 0)) |
| 454 | return Cost; |
| 455 | |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 456 | // PPC in general does not support unaligned loads and stores. They'll need |
| 457 | // to be decomposed based on the alignment factor. |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 458 | |
Hal Finkel | 79dbf5b | 2015-09-02 21:03:28 +0000 | [diff] [blame] | 459 | // Add the cost of each scalar load or store. |
| 460 | Cost += LT.first*(SrcBytes/Alignment-1); |
| 461 | |
| 462 | // For a vector type, there is also scalarization overhead (only for |
| 463 | // stores, loads are expanded using the vector-load + permutation sequence, |
| 464 | // which is much less expensive). |
| 465 | if (Src->isVectorTy() && Opcode == Instruction::Store) |
| 466 | for (int i = 0, e = Src->getVectorNumElements(); i < e; ++i) |
| 467 | Cost += getVectorInstrCost(Instruction::ExtractElement, Src, i); |
Hal Finkel | de0b413 | 2014-04-04 23:51:18 +0000 | [diff] [blame] | 468 | |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 469 | return Cost; |
| 470 | } |
| 471 | |
Hal Finkel | 4a7be23 | 2015-09-04 00:10:41 +0000 | [diff] [blame] | 472 | int PPCTTIImpl::getInterleavedMemoryOpCost(unsigned Opcode, Type *VecTy, |
| 473 | unsigned Factor, |
| 474 | ArrayRef<unsigned> Indices, |
| 475 | unsigned Alignment, |
Dorit Nuzman | 38bbf81 | 2018-10-14 08:50:06 +0000 | [diff] [blame^] | 476 | unsigned AddressSpace, |
| 477 | bool IsMasked) { |
| 478 | if (IsMasked) |
| 479 | return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices, |
| 480 | Alignment, AddressSpace, IsMasked); |
| 481 | |
Hal Finkel | 4a7be23 | 2015-09-04 00:10:41 +0000 | [diff] [blame] | 482 | assert(isa<VectorType>(VecTy) && |
| 483 | "Expect a vector type for interleaved memory op"); |
| 484 | |
| 485 | // Legalize the type. |
| 486 | std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, VecTy); |
| 487 | |
| 488 | // Firstly, the cost of load/store operation. |
| 489 | int Cost = getMemoryOpCost(Opcode, VecTy, Alignment, AddressSpace); |
| 490 | |
| 491 | // PPC, for both Altivec/VSX and QPX, support cheap arbitrary permutations |
| 492 | // (at least in the sense that there need only be one non-loop-invariant |
| 493 | // instruction). For each result vector, we need one shuffle per incoming |
| 494 | // vector (except that the first shuffle can take two incoming vectors |
| 495 | // because it does not need to take itself). |
| 496 | Cost += Factor*(LT.first-1); |
| 497 | |
| 498 | return Cost; |
| 499 | } |
| 500 | |