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