Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 1 | //===-- PPCTargetTransformInfo.cpp - PPC specific TTI pass ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// \file |
| 10 | /// This file implements a TargetTransformInfo analysis pass specific to the |
| 11 | /// PPC target machine. It uses the target's detailed information to provide |
| 12 | /// more precise answers to certain TTI queries, while letting the target |
| 13 | /// independent and default TTI implementations handle the rest. |
| 14 | /// |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 17 | #include "PPC.h" |
| 18 | #include "PPCTargetMachine.h" |
| 19 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 20 | #include "llvm/CodeGen/BasicTTIImpl.h" |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 21 | #include "llvm/Support/CommandLine.h" |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Debug.h" |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 23 | #include "llvm/Target/CostTable.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetLowering.h" |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 27 | #define DEBUG_TYPE "ppctti" |
| 28 | |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 29 | static cl::opt<bool> DisablePPCConstHoist("disable-ppc-constant-hoisting", |
| 30 | cl::desc("disable constant hoisting on PPC"), cl::init(false), cl::Hidden); |
| 31 | |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 32 | namespace { |
| 33 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 34 | class PPCTTIImpl : public BasicTTIImplBase<PPCTTIImpl> { |
| 35 | typedef BasicTTIImplBase<PPCTTIImpl> BaseT; |
| 36 | typedef TargetTransformInfo TTI; |
| 37 | |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 38 | const PPCSubtarget *ST; |
| 39 | const PPCTargetLowering *TLI; |
| 40 | |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 41 | public: |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 42 | explicit PPCTTIImpl(const PPCTargetMachine *TM = nullptr) |
| 43 | : BaseT(TM), ST(TM->getSubtargetImpl()), TLI(ST->getTargetLowering()) {} |
| 44 | |
| 45 | // Provide value semantics. MSVC requires that we spell all of these out. |
| 46 | PPCTTIImpl(const PPCTTIImpl &Arg) |
| 47 | : BaseT(static_cast<const BaseT &>(Arg)), ST(Arg.ST), TLI(Arg.TLI) {} |
| 48 | PPCTTIImpl(PPCTTIImpl &&Arg) |
| 49 | : BaseT(std::move(static_cast<BaseT &>(Arg))), ST(std::move(Arg.ST)), |
| 50 | TLI(std::move(Arg.TLI)) {} |
| 51 | PPCTTIImpl &operator=(const PPCTTIImpl &RHS) { |
| 52 | BaseT::operator=(static_cast<const BaseT &>(RHS)); |
| 53 | ST = RHS.ST; |
| 54 | TLI = RHS.TLI; |
| 55 | return *this; |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 56 | } |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 57 | PPCTTIImpl &operator=(PPCTTIImpl &&RHS) { |
| 58 | BaseT::operator=(std::move(static_cast<BaseT &>(RHS))); |
| 59 | ST = std::move(RHS.ST); |
| 60 | TLI = std::move(RHS.TLI); |
| 61 | return *this; |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /// \name Scalar TTI Implementations |
| 65 | /// @{ |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 66 | |
| 67 | using BaseT::getIntImmCost; |
| 68 | unsigned getIntImmCost(const APInt &Imm, Type *Ty); |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 69 | |
| 70 | unsigned getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm, |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 71 | Type *Ty); |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 72 | unsigned getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm, |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 73 | Type *Ty); |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 74 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 75 | TTI::PopcntSupportKind getPopcntSupport(unsigned TyWidth); |
Eric Christopher | d85ffb1 | 2014-09-18 00:34:14 +0000 | [diff] [blame] | 76 | void getUnrollingPreferences(const Function *F, Loop *L, |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 77 | TTI::UnrollingPreferences &UP); |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 78 | |
| 79 | /// @} |
| 80 | |
| 81 | /// \name Vector TTI Implementations |
| 82 | /// @{ |
| 83 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 84 | unsigned getNumberOfRegisters(bool Vector); |
| 85 | unsigned getRegisterBitWidth(bool Vector); |
| 86 | unsigned getMaxInterleaveFactor(); |
| 87 | unsigned getArithmeticInstrCost( |
| 88 | unsigned Opcode, Type *Ty, |
| 89 | TTI::OperandValueKind Opd1Info = TTI::OK_AnyValue, |
| 90 | TTI::OperandValueKind Opd2Info = TTI::OK_AnyValue, |
| 91 | TTI::OperandValueProperties Opd1PropInfo = TTI::OP_None, |
| 92 | TTI::OperandValueProperties Opd2PropInfo = TTI::OP_None); |
| 93 | unsigned getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index, |
| 94 | Type *SubTp); |
| 95 | unsigned getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src); |
| 96 | unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy); |
| 97 | unsigned getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index); |
Craig Topper | fd38cbe | 2014-08-30 16:48:34 +0000 | [diff] [blame] | 98 | unsigned getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment, |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 99 | unsigned AddressSpace); |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 100 | |
| 101 | /// @} |
| 102 | }; |
| 103 | |
| 104 | } // end anonymous namespace |
| 105 | |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 106 | ImmutablePass * |
| 107 | llvm::createPPCTargetTransformInfoPass(const PPCTargetMachine *TM) { |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 108 | return new TargetTransformInfoWrapperPass(PPCTTIImpl(TM)); |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | |
| 112 | //===----------------------------------------------------------------------===// |
| 113 | // |
| 114 | // PPC cost model. |
| 115 | // |
| 116 | //===----------------------------------------------------------------------===// |
| 117 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 118 | TargetTransformInfo::PopcntSupportKind |
| 119 | PPCTTIImpl::getPopcntSupport(unsigned TyWidth) { |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 120 | assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2"); |
Hal Finkel | a4d0748 | 2013-03-28 13:29:47 +0000 | [diff] [blame] | 121 | if (ST->hasPOPCNTD() && TyWidth <= 64) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 122 | return TTI::PSK_FastHardware; |
| 123 | return TTI::PSK_Software; |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 126 | unsigned PPCTTIImpl::getIntImmCost(const APInt &Imm, Type *Ty) { |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 127 | if (DisablePPCConstHoist) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 128 | return BaseT::getIntImmCost(Imm, Ty); |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 129 | |
| 130 | assert(Ty->isIntegerTy()); |
| 131 | |
| 132 | unsigned BitSize = Ty->getPrimitiveSizeInBits(); |
| 133 | if (BitSize == 0) |
| 134 | return ~0U; |
| 135 | |
| 136 | if (Imm == 0) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 137 | return TTI::TCC_Free; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 138 | |
| 139 | if (Imm.getBitWidth() <= 64) { |
| 140 | if (isInt<16>(Imm.getSExtValue())) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 141 | return TTI::TCC_Basic; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 142 | |
| 143 | if (isInt<32>(Imm.getSExtValue())) { |
| 144 | // A constant that can be materialized using lis. |
| 145 | if ((Imm.getZExtValue() & 0xFFFF) == 0) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 146 | return TTI::TCC_Basic; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 147 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 148 | return 2 * TTI::TCC_Basic; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 152 | return 4 * TTI::TCC_Basic; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 155 | unsigned PPCTTIImpl::getIntImmCost(Intrinsic::ID IID, unsigned Idx, |
| 156 | const APInt &Imm, Type *Ty) { |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 157 | if (DisablePPCConstHoist) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 158 | return BaseT::getIntImmCost(IID, Idx, Imm, Ty); |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 159 | |
| 160 | assert(Ty->isIntegerTy()); |
| 161 | |
| 162 | unsigned BitSize = Ty->getPrimitiveSizeInBits(); |
| 163 | if (BitSize == 0) |
| 164 | return ~0U; |
| 165 | |
| 166 | switch (IID) { |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 167 | default: |
| 168 | return TTI::TCC_Free; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 169 | case Intrinsic::sadd_with_overflow: |
| 170 | case Intrinsic::uadd_with_overflow: |
| 171 | case Intrinsic::ssub_with_overflow: |
| 172 | case Intrinsic::usub_with_overflow: |
| 173 | if ((Idx == 1) && Imm.getBitWidth() <= 64 && isInt<16>(Imm.getSExtValue())) |
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 | break; |
Hal Finkel | 934361a | 2015-01-14 01:07:51 +0000 | [diff] [blame] | 176 | case Intrinsic::experimental_stackmap: |
| 177 | if ((Idx < 2) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue()))) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 178 | return TTI::TCC_Free; |
Hal Finkel | 934361a | 2015-01-14 01:07:51 +0000 | [diff] [blame] | 179 | break; |
| 180 | case Intrinsic::experimental_patchpoint_void: |
| 181 | case Intrinsic::experimental_patchpoint_i64: |
| 182 | if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue()))) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 183 | return TTI::TCC_Free; |
Hal Finkel | 934361a | 2015-01-14 01:07:51 +0000 | [diff] [blame] | 184 | break; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 185 | } |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 186 | return PPCTTIImpl::getIntImmCost(Imm, Ty); |
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 | unsigned PPCTTIImpl::getIntImmCost(unsigned Opcode, unsigned Idx, |
| 190 | const APInt &Imm, Type *Ty) { |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 191 | if (DisablePPCConstHoist) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 192 | return BaseT::getIntImmCost(Opcode, Idx, Imm, Ty); |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 193 | |
| 194 | assert(Ty->isIntegerTy()); |
| 195 | |
| 196 | unsigned BitSize = Ty->getPrimitiveSizeInBits(); |
| 197 | if (BitSize == 0) |
| 198 | return ~0U; |
| 199 | |
| 200 | unsigned ImmIdx = ~0U; |
| 201 | bool ShiftedFree = false, RunFree = false, UnsignedFree = false, |
| 202 | ZeroFree = false; |
| 203 | switch (Opcode) { |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 204 | default: |
| 205 | return TTI::TCC_Free; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 206 | case Instruction::GetElementPtr: |
| 207 | // Always hoist the base address of a GetElementPtr. This prevents the |
| 208 | // creation of new constants for every base constant that gets constant |
| 209 | // folded with the offset. |
| 210 | if (Idx == 0) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 211 | return 2 * TTI::TCC_Basic; |
| 212 | return TTI::TCC_Free; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 213 | case Instruction::And: |
| 214 | RunFree = true; // (for the rotate-and-mask instructions) |
| 215 | // Fallthrough... |
| 216 | case Instruction::Add: |
| 217 | case Instruction::Or: |
| 218 | case Instruction::Xor: |
| 219 | ShiftedFree = true; |
| 220 | // Fallthrough... |
| 221 | case Instruction::Sub: |
| 222 | case Instruction::Mul: |
| 223 | case Instruction::Shl: |
| 224 | case Instruction::LShr: |
| 225 | case Instruction::AShr: |
| 226 | ImmIdx = 1; |
| 227 | break; |
| 228 | case Instruction::ICmp: |
| 229 | UnsignedFree = true; |
| 230 | ImmIdx = 1; |
| 231 | // Fallthrough... (zero comparisons can use record-form instructions) |
| 232 | case Instruction::Select: |
| 233 | ZeroFree = true; |
| 234 | break; |
| 235 | case Instruction::PHI: |
| 236 | case Instruction::Call: |
| 237 | case Instruction::Ret: |
| 238 | case Instruction::Load: |
| 239 | case Instruction::Store: |
| 240 | break; |
| 241 | } |
| 242 | |
| 243 | if (ZeroFree && Imm == 0) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 244 | return TTI::TCC_Free; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 245 | |
| 246 | if (Idx == ImmIdx && Imm.getBitWidth() <= 64) { |
| 247 | if (isInt<16>(Imm.getSExtValue())) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 248 | return TTI::TCC_Free; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 249 | |
| 250 | if (RunFree) { |
| 251 | if (Imm.getBitWidth() <= 32 && |
| 252 | (isShiftedMask_32(Imm.getZExtValue()) || |
| 253 | isShiftedMask_32(~Imm.getZExtValue()))) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 254 | return TTI::TCC_Free; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 255 | |
| 256 | if (ST->isPPC64() && |
| 257 | (isShiftedMask_64(Imm.getZExtValue()) || |
| 258 | isShiftedMask_64(~Imm.getZExtValue()))) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 259 | return TTI::TCC_Free; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | if (UnsignedFree && isUInt<16>(Imm.getZExtValue())) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 263 | return TTI::TCC_Free; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 264 | |
| 265 | if (ShiftedFree && (Imm.getZExtValue() & 0xFFFF) == 0) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 266 | return TTI::TCC_Free; |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 269 | return PPCTTIImpl::getIntImmCost(Imm, Ty); |
Hal Finkel | 0192cba | 2014-04-13 23:02:40 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 272 | void PPCTTIImpl::getUnrollingPreferences(const Function *F, Loop *L, |
| 273 | TTI::UnrollingPreferences &UP) { |
Eric Christopher | d85ffb1 | 2014-09-18 00:34:14 +0000 | [diff] [blame] | 274 | if (TM->getSubtarget<PPCSubtarget>(F).getDarwinDirective() == PPC::DIR_A2) { |
Hal Finkel | 71780ec | 2013-09-11 21:20:40 +0000 | [diff] [blame] | 275 | // The A2 is in-order with a deep pipeline, and concatenation unrolling |
| 276 | // helps expose latency-hiding opportunities to the instruction scheduler. |
| 277 | UP.Partial = UP.Runtime = true; |
| 278 | } |
Hal Finkel | b359b73 | 2015-01-09 15:51:16 +0000 | [diff] [blame] | 279 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 280 | BaseT::getUnrollingPreferences(F, L, UP); |
Hal Finkel | 71780ec | 2013-09-11 21:20:40 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 283 | unsigned PPCTTIImpl::getNumberOfRegisters(bool Vector) { |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 284 | if (Vector && !ST->hasAltivec()) |
| 285 | return 0; |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 286 | return ST->hasVSX() ? 64 : 32; |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 289 | unsigned PPCTTIImpl::getRegisterBitWidth(bool Vector) { |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 290 | if (Vector) { |
| 291 | if (ST->hasAltivec()) return 128; |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | if (ST->isPPC64()) |
| 296 | return 64; |
| 297 | return 32; |
| 298 | |
| 299 | } |
| 300 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 301 | unsigned PPCTTIImpl::getMaxInterleaveFactor() { |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 302 | unsigned Directive = ST->getDarwinDirective(); |
| 303 | // The 440 has no SIMD support, but floating-point instructions |
| 304 | // have a 5-cycle latency, so unroll by 5x for latency hiding. |
| 305 | if (Directive == PPC::DIR_440) |
| 306 | return 5; |
| 307 | |
| 308 | // The A2 has no SIMD support, but floating-point instructions |
| 309 | // have a 6-cycle latency, so unroll by 6x for latency hiding. |
| 310 | if (Directive == PPC::DIR_A2) |
| 311 | return 6; |
| 312 | |
| 313 | // FIXME: For lack of any better information, do no harm... |
| 314 | if (Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500) |
| 315 | return 1; |
| 316 | |
| 317 | // For most things, modern systems have two execution units (and |
| 318 | // out-of-order execution). |
| 319 | return 2; |
| 320 | } |
| 321 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 322 | unsigned PPCTTIImpl::getArithmeticInstrCost( |
| 323 | unsigned Opcode, Type *Ty, TTI::OperandValueKind Op1Info, |
| 324 | TTI::OperandValueKind Op2Info, TTI::OperandValueProperties Opd1PropInfo, |
| 325 | TTI::OperandValueProperties Opd2PropInfo) { |
Dmitri Gribenko | c451bdf | 2013-01-25 23:17:21 +0000 | [diff] [blame] | 326 | assert(TLI->InstructionOpcodeToISD(Opcode) && "Invalid opcode"); |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 327 | |
| 328 | // Fallback to the default implementation. |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 329 | return BaseT::getArithmeticInstrCost(Opcode, Ty, Op1Info, Op2Info, |
| 330 | Opd1PropInfo, Opd2PropInfo); |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 333 | unsigned PPCTTIImpl::getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index, |
| 334 | Type *SubTp) { |
| 335 | return BaseT::getShuffleCost(Kind, Tp, Index, SubTp); |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 338 | unsigned PPCTTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) { |
Dmitri Gribenko | c451bdf | 2013-01-25 23:17:21 +0000 | [diff] [blame] | 339 | assert(TLI->InstructionOpcodeToISD(Opcode) && "Invalid opcode"); |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 340 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 341 | return BaseT::getCastInstrCost(Opcode, Dst, Src); |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 344 | unsigned PPCTTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, |
| 345 | Type *CondTy) { |
| 346 | return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy); |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 347 | } |
| 348 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 349 | unsigned PPCTTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val, |
| 350 | unsigned Index) { |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 351 | assert(Val->isVectorTy() && "This must be a vector type"); |
| 352 | |
Bill Schmidt | 62fe7a5b | 2013-02-08 18:19:17 +0000 | [diff] [blame] | 353 | int ISD = TLI->InstructionOpcodeToISD(Opcode); |
| 354 | assert(ISD && "Invalid opcode"); |
Bill Schmidt | b3cece1 | 2013-02-07 20:33:57 +0000 | [diff] [blame] | 355 | |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 356 | if (ST->hasVSX() && Val->getScalarType()->isDoubleTy()) { |
| 357 | // Double-precision scalars are already located in index #0. |
| 358 | if (Index == 0) |
| 359 | return 0; |
| 360 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 361 | return BaseT::getVectorInstrCost(Opcode, Val, Index); |
Hal Finkel | 27774d9 | 2014-03-13 07:58:58 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Bill Schmidt | 62fe7a5b | 2013-02-08 18:19:17 +0000 | [diff] [blame] | 364 | // Estimated cost of a load-hit-store delay. This was obtained |
| 365 | // experimentally as a minimum needed to prevent unprofitable |
| 366 | // vectorization for the paq8p benchmark. It may need to be |
| 367 | // raised further if other unprofitable cases remain. |
Hal Finkel | de0b413 | 2014-04-04 23:51:18 +0000 | [diff] [blame] | 368 | unsigned LHSPenalty = 2; |
| 369 | if (ISD == ISD::INSERT_VECTOR_ELT) |
| 370 | LHSPenalty += 7; |
Bill Schmidt | b3cece1 | 2013-02-07 20:33:57 +0000 | [diff] [blame] | 371 | |
Bill Schmidt | 62fe7a5b | 2013-02-08 18:19:17 +0000 | [diff] [blame] | 372 | // Vector element insert/extract with Altivec is very expensive, |
| 373 | // because they require store and reload with the attendant |
| 374 | // processor stall for load-hit-store. Until VSX is available, |
| 375 | // these need to be estimated as very costly. |
| 376 | if (ISD == ISD::EXTRACT_VECTOR_ELT || |
| 377 | ISD == ISD::INSERT_VECTOR_ELT) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 378 | return LHSPenalty + BaseT::getVectorInstrCost(Opcode, Val, Index); |
Bill Schmidt | b3cece1 | 2013-02-07 20:33:57 +0000 | [diff] [blame] | 379 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 380 | return BaseT::getVectorInstrCost(Opcode, Val, Index); |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 383 | unsigned PPCTTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src, |
| 384 | unsigned Alignment, |
| 385 | unsigned AddressSpace) { |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 386 | // Legalize the type. |
| 387 | std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Src); |
| 388 | assert((Opcode == Instruction::Load || Opcode == Instruction::Store) && |
| 389 | "Invalid Opcode"); |
| 390 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 391 | unsigned Cost = BaseT::getMemoryOpCost(Opcode, Src, Alignment, AddressSpace); |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 392 | |
Hal Finkel | de0b413 | 2014-04-04 23:51:18 +0000 | [diff] [blame] | 393 | // VSX loads/stores support unaligned access. |
| 394 | if (ST->hasVSX()) { |
| 395 | if (LT.second == MVT::v2f64 || LT.second == MVT::v2i64) |
| 396 | return Cost; |
| 397 | } |
| 398 | |
| 399 | bool UnalignedAltivec = |
| 400 | Src->isVectorTy() && |
| 401 | Src->getPrimitiveSizeInBits() >= LT.second.getSizeInBits() && |
| 402 | LT.second.getSizeInBits() == 128 && |
| 403 | Opcode == Instruction::Load; |
Hal Finkel | 6e28e6a | 2014-03-26 19:39:09 +0000 | [diff] [blame] | 404 | |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 405 | // PPC in general does not support unaligned loads and stores. They'll need |
| 406 | // to be decomposed based on the alignment factor. |
| 407 | unsigned SrcBytes = LT.second.getStoreSize(); |
Hal Finkel | de0b413 | 2014-04-04 23:51:18 +0000 | [diff] [blame] | 408 | if (SrcBytes && Alignment && Alignment < SrcBytes && !UnalignedAltivec) { |
Hal Finkel | f823380 | 2014-04-02 22:43:49 +0000 | [diff] [blame] | 409 | Cost += LT.first*(SrcBytes/Alignment-1); |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 410 | |
Hal Finkel | de0b413 | 2014-04-04 23:51:18 +0000 | [diff] [blame] | 411 | // For a vector type, there is also scalarization overhead (only for |
| 412 | // stores, loads are expanded using the vector-load + permutation sequence, |
| 413 | // which is much less expensive). |
| 414 | if (Src->isVectorTy() && Opcode == Instruction::Store) |
| 415 | for (int i = 0, e = Src->getVectorNumElements(); i < e; ++i) |
| 416 | Cost += getVectorInstrCost(Instruction::ExtractElement, Src, i); |
| 417 | } |
| 418 | |
Hal Finkel | 4e5ca9e | 2013-01-25 23:05:59 +0000 | [diff] [blame] | 419 | return Cost; |
| 420 | } |
| 421 | |