Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 1 | //===-- X86TargetTransformInfo.cpp - X86 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 | /// X86 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 | |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 17 | #include "X86.h" |
| 18 | #include "X86TargetMachine.h" |
Chandler Carruth | d3e7355 | 2013-01-07 03:08:10 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 20 | #include "llvm/CodeGen/BasicTTIImpl.h" |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 21 | #include "llvm/IR/IntrinsicInst.h" |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Debug.h" |
Renato Golin | d4c392e | 2013-01-24 23:01:00 +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" |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 27 | #define DEBUG_TYPE "x86tti" |
| 28 | |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 29 | namespace { |
| 30 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 31 | class X86TTIImpl : public BasicTTIImplBase<X86TTIImpl> { |
| 32 | typedef BasicTTIImplBase<X86TTIImpl> BaseT; |
| 33 | typedef TargetTransformInfo TTI; |
| 34 | |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 35 | const X86Subtarget *ST; |
| 36 | const X86TargetLowering *TLI; |
| 37 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 38 | unsigned getScalarizationOverhead(Type *Ty, bool Insert, bool Extract); |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 39 | |
| 40 | public: |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 41 | explicit X86TTIImpl(const X86TargetMachine *TM = nullptr) |
| 42 | : BaseT(TM), ST(TM ? TM->getSubtargetImpl() : nullptr), |
| 43 | TLI(ST ? ST->getTargetLowering() : nullptr) {} |
| 44 | |
| 45 | // Provide value semantics. MSVC requires that we spell all of these out. |
| 46 | X86TTIImpl(const X86TTIImpl &Arg) |
| 47 | : BaseT(static_cast<const BaseT &>(Arg)), ST(Arg.ST), TLI(Arg.TLI) {} |
| 48 | X86TTIImpl(X86TTIImpl &&Arg) |
| 49 | : BaseT(std::move(static_cast<BaseT &>(Arg))), ST(std::move(Arg.ST)), |
| 50 | TLI(std::move(Arg.TLI)) {} |
| 51 | X86TTIImpl &operator=(const X86TTIImpl &RHS) { |
| 52 | BaseT::operator=(static_cast<const BaseT &>(RHS)); |
| 53 | ST = RHS.ST; |
| 54 | TLI = RHS.TLI; |
| 55 | return *this; |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 56 | } |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 57 | X86TTIImpl &operator=(X86TTIImpl &&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; |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /// \name Scalar TTI Implementations |
| 65 | /// @{ |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 66 | TTI::PopcntSupportKind getPopcntSupport(unsigned TyWidth); |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 67 | |
| 68 | /// @} |
| 69 | |
| 70 | /// \name Vector TTI Implementations |
| 71 | /// @{ |
| 72 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 73 | unsigned getNumberOfRegisters(bool Vector); |
| 74 | unsigned getRegisterBitWidth(bool Vector); |
| 75 | unsigned getMaxInterleaveFactor(); |
| 76 | unsigned getArithmeticInstrCost( |
| 77 | unsigned Opcode, Type *Ty, |
| 78 | TTI::OperandValueKind Opd1Info = TTI::OK_AnyValue, |
| 79 | TTI::OperandValueKind Opd2Info = TTI::OK_AnyValue, |
| 80 | TTI::OperandValueProperties Opd1PropInfo = TTI::OP_None, |
| 81 | TTI::OperandValueProperties Opd2PropInfo = TTI::OP_None); |
| 82 | unsigned getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index, |
| 83 | Type *SubTp); |
| 84 | unsigned getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src); |
| 85 | unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy); |
| 86 | unsigned getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index); |
Craig Topper | 24e685f | 2014-03-10 05:29:18 +0000 | [diff] [blame] | 87 | unsigned getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment, |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 88 | unsigned AddressSpace); |
| 89 | unsigned getMaskedMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment, |
| 90 | unsigned AddressSpace); |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 91 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 92 | unsigned getAddressComputationCost(Type *PtrTy, bool IsComplex); |
Arnold Schwaighofer | 6042a26 | 2013-07-12 19:16:07 +0000 | [diff] [blame] | 93 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 94 | unsigned getReductionCost(unsigned Opcode, Type *Ty, bool IsPairwiseForm); |
Craig Topper | 7315602 | 2014-03-02 09:09:27 +0000 | [diff] [blame] | 95 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 96 | unsigned getIntImmCost(int64_t); |
Juergen Ributzka | b2e4edb | 2014-06-10 00:32:29 +0000 | [diff] [blame] | 97 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 98 | unsigned getIntImmCost(const APInt &Imm, Type *Ty); |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 99 | |
Juergen Ributzka | f0dff49 | 2014-03-21 06:04:45 +0000 | [diff] [blame] | 100 | unsigned getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm, |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 101 | Type *Ty); |
Juergen Ributzka | f0dff49 | 2014-03-21 06:04:45 +0000 | [diff] [blame] | 102 | unsigned getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm, |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 103 | Type *Ty); |
| 104 | bool isLegalMaskedLoad(Type *DataType, int Consecutive); |
| 105 | bool isLegalMaskedStore(Type *DataType, int Consecutive); |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 106 | |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 107 | /// @} |
| 108 | }; |
| 109 | |
| 110 | } // end anonymous namespace |
| 111 | |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 112 | ImmutablePass * |
| 113 | llvm::createX86TargetTransformInfoPass(const X86TargetMachine *TM) { |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 114 | return new TargetTransformInfoWrapperPass(X86TTIImpl(TM)); |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | |
| 118 | //===----------------------------------------------------------------------===// |
| 119 | // |
| 120 | // X86 cost model. |
| 121 | // |
| 122 | //===----------------------------------------------------------------------===// |
| 123 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 124 | TargetTransformInfo::PopcntSupportKind |
| 125 | X86TTIImpl::getPopcntSupport(unsigned TyWidth) { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 126 | assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2"); |
| 127 | // TODO: Currently the __builtin_popcount() implementation using SSE3 |
| 128 | // instructions is inefficient. Once the problem is fixed, we should |
Craig Topper | 0a63e1d | 2013-09-08 00:47:31 +0000 | [diff] [blame] | 129 | // call ST->hasSSE3() instead of ST->hasPOPCNT(). |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 130 | return ST->hasPOPCNT() ? TTI::PSK_FastHardware : TTI::PSK_Software; |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 133 | unsigned X86TTIImpl::getNumberOfRegisters(bool Vector) { |
Nadav Rotem | b1791a7 | 2013-01-09 22:29:00 +0000 | [diff] [blame] | 134 | if (Vector && !ST->hasSSE1()) |
| 135 | return 0; |
| 136 | |
Adam Nemet | 2820a5b | 2014-07-09 18:22:33 +0000 | [diff] [blame] | 137 | if (ST->is64Bit()) { |
| 138 | if (Vector && ST->hasAVX512()) |
| 139 | return 32; |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 140 | return 16; |
Adam Nemet | 2820a5b | 2014-07-09 18:22:33 +0000 | [diff] [blame] | 141 | } |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 142 | return 8; |
| 143 | } |
| 144 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 145 | unsigned X86TTIImpl::getRegisterBitWidth(bool Vector) { |
Nadav Rotem | b1791a7 | 2013-01-09 22:29:00 +0000 | [diff] [blame] | 146 | if (Vector) { |
Adam Nemet | 2820a5b | 2014-07-09 18:22:33 +0000 | [diff] [blame] | 147 | if (ST->hasAVX512()) return 512; |
Nadav Rotem | b1791a7 | 2013-01-09 22:29:00 +0000 | [diff] [blame] | 148 | if (ST->hasAVX()) return 256; |
| 149 | if (ST->hasSSE1()) return 128; |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | if (ST->is64Bit()) |
| 154 | return 64; |
| 155 | return 32; |
| 156 | |
| 157 | } |
| 158 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 159 | unsigned X86TTIImpl::getMaxInterleaveFactor() { |
Nadav Rotem | b696c36 | 2013-01-09 01:15:42 +0000 | [diff] [blame] | 160 | if (ST->isAtom()) |
| 161 | return 1; |
| 162 | |
| 163 | // Sandybridge and Haswell have multiple execution ports and pipelined |
| 164 | // vector units. |
| 165 | if (ST->hasAVX()) |
| 166 | return 4; |
| 167 | |
| 168 | return 2; |
| 169 | } |
| 170 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 171 | unsigned X86TTIImpl::getArithmeticInstrCost( |
| 172 | unsigned Opcode, Type *Ty, TTI::OperandValueKind Op1Info, |
| 173 | TTI::OperandValueKind Op2Info, TTI::OperandValueProperties Opd1PropInfo, |
| 174 | TTI::OperandValueProperties Opd2PropInfo) { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 175 | // Legalize the type. |
| 176 | std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty); |
| 177 | |
| 178 | int ISD = TLI->InstructionOpcodeToISD(Opcode); |
| 179 | assert(ISD && "Invalid opcode"); |
| 180 | |
Karthik Bhat | 7f33ff7 | 2014-08-25 04:56:54 +0000 | [diff] [blame] | 181 | if (ISD == ISD::SDIV && |
| 182 | Op2Info == TargetTransformInfo::OK_UniformConstantValue && |
| 183 | Opd2PropInfo == TargetTransformInfo::OP_PowerOf2) { |
| 184 | // On X86, vector signed division by constants power-of-two are |
| 185 | // normally expanded to the sequence SRA + SRL + ADD + SRA. |
| 186 | // The OperandValue properties many not be same as that of previous |
| 187 | // operation;conservatively assume OP_None. |
| 188 | unsigned Cost = |
| 189 | 2 * getArithmeticInstrCost(Instruction::AShr, Ty, Op1Info, Op2Info, |
| 190 | TargetTransformInfo::OP_None, |
| 191 | TargetTransformInfo::OP_None); |
| 192 | Cost += getArithmeticInstrCost(Instruction::LShr, Ty, Op1Info, Op2Info, |
| 193 | TargetTransformInfo::OP_None, |
| 194 | TargetTransformInfo::OP_None); |
| 195 | Cost += getArithmeticInstrCost(Instruction::Add, Ty, Op1Info, Op2Info, |
| 196 | TargetTransformInfo::OP_None, |
| 197 | TargetTransformInfo::OP_None); |
| 198 | |
| 199 | return Cost; |
| 200 | } |
| 201 | |
Benjamin Kramer | 7c37227 | 2014-04-26 14:53:05 +0000 | [diff] [blame] | 202 | static const CostTblEntry<MVT::SimpleValueType> |
| 203 | AVX2UniformConstCostTable[] = { |
| 204 | { ISD::SDIV, MVT::v16i16, 6 }, // vpmulhw sequence |
| 205 | { ISD::UDIV, MVT::v16i16, 6 }, // vpmulhuw sequence |
| 206 | { ISD::SDIV, MVT::v8i32, 15 }, // vpmuldq sequence |
| 207 | { ISD::UDIV, MVT::v8i32, 15 }, // vpmuludq sequence |
| 208 | }; |
| 209 | |
| 210 | if (Op2Info == TargetTransformInfo::OK_UniformConstantValue && |
| 211 | ST->hasAVX2()) { |
| 212 | int Idx = CostTableLookup(AVX2UniformConstCostTable, ISD, LT.second); |
| 213 | if (Idx != -1) |
| 214 | return LT.first * AVX2UniformConstCostTable[Idx].Cost; |
| 215 | } |
| 216 | |
Elena Demikhovsky | 2701247 | 2014-09-16 07:57:37 +0000 | [diff] [blame] | 217 | static const CostTblEntry<MVT::SimpleValueType> AVX512CostTable[] = { |
| 218 | { ISD::SHL, MVT::v16i32, 1 }, |
| 219 | { ISD::SRL, MVT::v16i32, 1 }, |
| 220 | { ISD::SRA, MVT::v16i32, 1 }, |
| 221 | { ISD::SHL, MVT::v8i64, 1 }, |
| 222 | { ISD::SRL, MVT::v8i64, 1 }, |
| 223 | { ISD::SRA, MVT::v8i64, 1 }, |
| 224 | }; |
| 225 | |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 226 | static const CostTblEntry<MVT::SimpleValueType> AVX2CostTable[] = { |
Michael Liao | 70dd7f9 | 2013-03-20 22:01:10 +0000 | [diff] [blame] | 227 | // Shifts on v4i64/v8i32 on AVX2 is legal even though we declare to |
| 228 | // customize them to detect the cases where shift amount is a scalar one. |
| 229 | { ISD::SHL, MVT::v4i32, 1 }, |
| 230 | { ISD::SRL, MVT::v4i32, 1 }, |
| 231 | { ISD::SRA, MVT::v4i32, 1 }, |
| 232 | { ISD::SHL, MVT::v8i32, 1 }, |
| 233 | { ISD::SRL, MVT::v8i32, 1 }, |
| 234 | { ISD::SRA, MVT::v8i32, 1 }, |
| 235 | { ISD::SHL, MVT::v2i64, 1 }, |
| 236 | { ISD::SRL, MVT::v2i64, 1 }, |
| 237 | { ISD::SHL, MVT::v4i64, 1 }, |
| 238 | { ISD::SRL, MVT::v4i64, 1 }, |
Arnold Schwaighofer | e9b5016 | 2013-04-03 21:46:05 +0000 | [diff] [blame] | 239 | |
| 240 | { ISD::SHL, MVT::v32i8, 42 }, // cmpeqb sequence. |
| 241 | { ISD::SHL, MVT::v16i16, 16*10 }, // Scalarized. |
| 242 | |
| 243 | { ISD::SRL, MVT::v32i8, 32*10 }, // Scalarized. |
| 244 | { ISD::SRL, MVT::v16i16, 8*10 }, // Scalarized. |
| 245 | |
| 246 | { ISD::SRA, MVT::v32i8, 32*10 }, // Scalarized. |
| 247 | { ISD::SRA, MVT::v16i16, 16*10 }, // Scalarized. |
| 248 | { ISD::SRA, MVT::v4i64, 4*10 }, // Scalarized. |
Arnold Schwaighofer | a04b9ef | 2013-06-25 19:14:09 +0000 | [diff] [blame] | 249 | |
| 250 | // Vectorizing division is a bad idea. See the SSE2 table for more comments. |
| 251 | { ISD::SDIV, MVT::v32i8, 32*20 }, |
| 252 | { ISD::SDIV, MVT::v16i16, 16*20 }, |
| 253 | { ISD::SDIV, MVT::v8i32, 8*20 }, |
| 254 | { ISD::SDIV, MVT::v4i64, 4*20 }, |
| 255 | { ISD::UDIV, MVT::v32i8, 32*20 }, |
| 256 | { ISD::UDIV, MVT::v16i16, 16*20 }, |
| 257 | { ISD::UDIV, MVT::v8i32, 8*20 }, |
| 258 | { ISD::UDIV, MVT::v4i64, 4*20 }, |
Michael Liao | 70dd7f9 | 2013-03-20 22:01:10 +0000 | [diff] [blame] | 259 | }; |
| 260 | |
Elena Demikhovsky | 2701247 | 2014-09-16 07:57:37 +0000 | [diff] [blame] | 261 | if (ST->hasAVX512()) { |
| 262 | int Idx = CostTableLookup(AVX512CostTable, ISD, LT.second); |
| 263 | if (Idx != -1) |
| 264 | return LT.first * AVX512CostTable[Idx].Cost; |
| 265 | } |
Michael Liao | 70dd7f9 | 2013-03-20 22:01:10 +0000 | [diff] [blame] | 266 | // Look for AVX2 lowering tricks. |
| 267 | if (ST->hasAVX2()) { |
Andrea Di Biagio | b7882b3 | 2014-02-12 23:43:47 +0000 | [diff] [blame] | 268 | if (ISD == ISD::SHL && LT.second == MVT::v16i16 && |
| 269 | (Op2Info == TargetTransformInfo::OK_UniformConstantValue || |
| 270 | Op2Info == TargetTransformInfo::OK_NonUniformConstantValue)) |
| 271 | // On AVX2, a packed v16i16 shift left by a constant build_vector |
| 272 | // is lowered into a vector multiply (vpmullw). |
| 273 | return LT.first; |
| 274 | |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 275 | int Idx = CostTableLookup(AVX2CostTable, ISD, LT.second); |
Michael Liao | 70dd7f9 | 2013-03-20 22:01:10 +0000 | [diff] [blame] | 276 | if (Idx != -1) |
| 277 | return LT.first * AVX2CostTable[Idx].Cost; |
| 278 | } |
| 279 | |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 280 | static const CostTblEntry<MVT::SimpleValueType> |
| 281 | SSE2UniformConstCostTable[] = { |
Arnold Schwaighofer | 44f902e | 2013-04-04 23:26:24 +0000 | [diff] [blame] | 282 | // We don't correctly identify costs of casts because they are marked as |
| 283 | // custom. |
| 284 | // Constant splats are cheaper for the following instructions. |
| 285 | { ISD::SHL, MVT::v16i8, 1 }, // psllw. |
| 286 | { ISD::SHL, MVT::v8i16, 1 }, // psllw. |
| 287 | { ISD::SHL, MVT::v4i32, 1 }, // pslld |
| 288 | { ISD::SHL, MVT::v2i64, 1 }, // psllq. |
| 289 | |
| 290 | { ISD::SRL, MVT::v16i8, 1 }, // psrlw. |
| 291 | { ISD::SRL, MVT::v8i16, 1 }, // psrlw. |
| 292 | { ISD::SRL, MVT::v4i32, 1 }, // psrld. |
| 293 | { ISD::SRL, MVT::v2i64, 1 }, // psrlq. |
| 294 | |
| 295 | { ISD::SRA, MVT::v16i8, 4 }, // psrlw, pand, pxor, psubb. |
| 296 | { ISD::SRA, MVT::v8i16, 1 }, // psraw. |
| 297 | { ISD::SRA, MVT::v4i32, 1 }, // psrad. |
Benjamin Kramer | 7c37227 | 2014-04-26 14:53:05 +0000 | [diff] [blame] | 298 | |
| 299 | { ISD::SDIV, MVT::v8i16, 6 }, // pmulhw sequence |
| 300 | { ISD::UDIV, MVT::v8i16, 6 }, // pmulhuw sequence |
Benjamin Kramer | ce4b3fe | 2014-04-27 18:47:54 +0000 | [diff] [blame] | 301 | { ISD::SDIV, MVT::v4i32, 19 }, // pmuludq sequence |
Benjamin Kramer | 7c37227 | 2014-04-26 14:53:05 +0000 | [diff] [blame] | 302 | { ISD::UDIV, MVT::v4i32, 15 }, // pmuludq sequence |
Arnold Schwaighofer | 44f902e | 2013-04-04 23:26:24 +0000 | [diff] [blame] | 303 | }; |
| 304 | |
| 305 | if (Op2Info == TargetTransformInfo::OK_UniformConstantValue && |
| 306 | ST->hasSSE2()) { |
Benjamin Kramer | ce4b3fe | 2014-04-27 18:47:54 +0000 | [diff] [blame] | 307 | // pmuldq sequence. |
| 308 | if (ISD == ISD::SDIV && LT.second == MVT::v4i32 && ST->hasSSE41()) |
| 309 | return LT.first * 15; |
| 310 | |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 311 | int Idx = CostTableLookup(SSE2UniformConstCostTable, ISD, LT.second); |
Arnold Schwaighofer | 44f902e | 2013-04-04 23:26:24 +0000 | [diff] [blame] | 312 | if (Idx != -1) |
| 313 | return LT.first * SSE2UniformConstCostTable[Idx].Cost; |
| 314 | } |
| 315 | |
Andrea Di Biagio | b7882b3 | 2014-02-12 23:43:47 +0000 | [diff] [blame] | 316 | if (ISD == ISD::SHL && |
| 317 | Op2Info == TargetTransformInfo::OK_NonUniformConstantValue) { |
| 318 | EVT VT = LT.second; |
| 319 | if ((VT == MVT::v8i16 && ST->hasSSE2()) || |
| 320 | (VT == MVT::v4i32 && ST->hasSSE41())) |
| 321 | // Vector shift left by non uniform constant can be lowered |
| 322 | // into vector multiply (pmullw/pmulld). |
| 323 | return LT.first; |
| 324 | if (VT == MVT::v4i32 && ST->hasSSE2()) |
| 325 | // A vector shift left by non uniform constant is converted |
| 326 | // into a vector multiply; the new multiply is eventually |
| 327 | // lowered into a sequence of shuffles and 2 x pmuludq. |
| 328 | ISD = ISD::MUL; |
| 329 | } |
Arnold Schwaighofer | 44f902e | 2013-04-04 23:26:24 +0000 | [diff] [blame] | 330 | |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 331 | static const CostTblEntry<MVT::SimpleValueType> SSE2CostTable[] = { |
Arnold Schwaighofer | e9b5016 | 2013-04-03 21:46:05 +0000 | [diff] [blame] | 332 | // We don't correctly identify costs of casts because they are marked as |
| 333 | // custom. |
| 334 | // For some cases, where the shift amount is a scalar we would be able |
| 335 | // to generate better code. Unfortunately, when this is the case the value |
| 336 | // (the splat) will get hoisted out of the loop, thereby making it invisible |
| 337 | // to ISel. The cost model must return worst case assumptions because it is |
| 338 | // used for vectorization and we don't want to make vectorized code worse |
| 339 | // than scalar code. |
| 340 | { ISD::SHL, MVT::v16i8, 30 }, // cmpeqb sequence. |
| 341 | { ISD::SHL, MVT::v8i16, 8*10 }, // Scalarized. |
| 342 | { ISD::SHL, MVT::v4i32, 2*5 }, // We optimized this using mul. |
| 343 | { ISD::SHL, MVT::v2i64, 2*10 }, // Scalarized. |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 344 | { ISD::SHL, MVT::v4i64, 4*10 }, // Scalarized. |
Arnold Schwaighofer | e9b5016 | 2013-04-03 21:46:05 +0000 | [diff] [blame] | 345 | |
| 346 | { ISD::SRL, MVT::v16i8, 16*10 }, // Scalarized. |
| 347 | { ISD::SRL, MVT::v8i16, 8*10 }, // Scalarized. |
| 348 | { ISD::SRL, MVT::v4i32, 4*10 }, // Scalarized. |
| 349 | { ISD::SRL, MVT::v2i64, 2*10 }, // Scalarized. |
| 350 | |
| 351 | { ISD::SRA, MVT::v16i8, 16*10 }, // Scalarized. |
| 352 | { ISD::SRA, MVT::v8i16, 8*10 }, // Scalarized. |
| 353 | { ISD::SRA, MVT::v4i32, 4*10 }, // Scalarized. |
| 354 | { ISD::SRA, MVT::v2i64, 2*10 }, // Scalarized. |
Arnold Schwaighofer | a04b9ef | 2013-06-25 19:14:09 +0000 | [diff] [blame] | 355 | |
| 356 | // It is not a good idea to vectorize division. We have to scalarize it and |
| 357 | // in the process we will often end up having to spilling regular |
| 358 | // registers. The overhead of division is going to dominate most kernels |
| 359 | // anyways so try hard to prevent vectorization of division - it is |
| 360 | // generally a bad idea. Assume somewhat arbitrarily that we have to be able |
| 361 | // to hide "20 cycles" for each lane. |
| 362 | { ISD::SDIV, MVT::v16i8, 16*20 }, |
| 363 | { ISD::SDIV, MVT::v8i16, 8*20 }, |
| 364 | { ISD::SDIV, MVT::v4i32, 4*20 }, |
| 365 | { ISD::SDIV, MVT::v2i64, 2*20 }, |
| 366 | { ISD::UDIV, MVT::v16i8, 16*20 }, |
| 367 | { ISD::UDIV, MVT::v8i16, 8*20 }, |
| 368 | { ISD::UDIV, MVT::v4i32, 4*20 }, |
| 369 | { ISD::UDIV, MVT::v2i64, 2*20 }, |
Arnold Schwaighofer | e9b5016 | 2013-04-03 21:46:05 +0000 | [diff] [blame] | 370 | }; |
| 371 | |
| 372 | if (ST->hasSSE2()) { |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 373 | int Idx = CostTableLookup(SSE2CostTable, ISD, LT.second); |
Arnold Schwaighofer | e9b5016 | 2013-04-03 21:46:05 +0000 | [diff] [blame] | 374 | if (Idx != -1) |
| 375 | return LT.first * SSE2CostTable[Idx].Cost; |
| 376 | } |
| 377 | |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 378 | static const CostTblEntry<MVT::SimpleValueType> AVX1CostTable[] = { |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 379 | // We don't have to scalarize unsupported ops. We can issue two half-sized |
| 380 | // operations and we only need to extract the upper YMM half. |
| 381 | // Two ops + 1 extract + 1 insert = 4. |
Andrea Di Biagio | b7882b3 | 2014-02-12 23:43:47 +0000 | [diff] [blame] | 382 | { ISD::MUL, MVT::v16i16, 4 }, |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 383 | { ISD::MUL, MVT::v8i32, 4 }, |
| 384 | { ISD::SUB, MVT::v8i32, 4 }, |
| 385 | { ISD::ADD, MVT::v8i32, 4 }, |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 386 | { ISD::SUB, MVT::v4i64, 4 }, |
| 387 | { ISD::ADD, MVT::v4i64, 4 }, |
Arnold Schwaighofer | 20ef54f | 2013-03-02 04:02:52 +0000 | [diff] [blame] | 388 | // A v4i64 multiply is custom lowered as two split v2i64 vectors that then |
| 389 | // are lowered as a series of long multiplies(3), shifts(4) and adds(2) |
| 390 | // Because we believe v4i64 to be a legal type, we must also include the |
| 391 | // split factor of two in the cost table. Therefore, the cost here is 18 |
| 392 | // instead of 9. |
| 393 | { ISD::MUL, MVT::v4i64, 18 }, |
| 394 | }; |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 395 | |
| 396 | // Look for AVX1 lowering tricks. |
Arnold Schwaighofer | 20ef54f | 2013-03-02 04:02:52 +0000 | [diff] [blame] | 397 | if (ST->hasAVX() && !ST->hasAVX2()) { |
Andrea Di Biagio | b7882b3 | 2014-02-12 23:43:47 +0000 | [diff] [blame] | 398 | EVT VT = LT.second; |
| 399 | |
| 400 | // v16i16 and v8i32 shifts by non-uniform constants are lowered into a |
| 401 | // sequence of extract + two vector multiply + insert. |
| 402 | if (ISD == ISD::SHL && (VT == MVT::v8i32 || VT == MVT::v16i16) && |
| 403 | Op2Info == TargetTransformInfo::OK_NonUniformConstantValue) |
| 404 | ISD = ISD::MUL; |
| 405 | |
| 406 | int Idx = CostTableLookup(AVX1CostTable, ISD, VT); |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 407 | if (Idx != -1) |
| 408 | return LT.first * AVX1CostTable[Idx].Cost; |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 409 | } |
Arnold Schwaighofer | 20ef54f | 2013-03-02 04:02:52 +0000 | [diff] [blame] | 410 | |
| 411 | // Custom lowering of vectors. |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 412 | static const CostTblEntry<MVT::SimpleValueType> CustomLowered[] = { |
Arnold Schwaighofer | 20ef54f | 2013-03-02 04:02:52 +0000 | [diff] [blame] | 413 | // A v2i64/v4i64 and multiply is custom lowered as a series of long |
| 414 | // multiplies(3), shifts(4) and adds(2). |
| 415 | { ISD::MUL, MVT::v2i64, 9 }, |
| 416 | { ISD::MUL, MVT::v4i64, 9 }, |
| 417 | }; |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 418 | int Idx = CostTableLookup(CustomLowered, ISD, LT.second); |
Arnold Schwaighofer | 20ef54f | 2013-03-02 04:02:52 +0000 | [diff] [blame] | 419 | if (Idx != -1) |
| 420 | return LT.first * CustomLowered[Idx].Cost; |
| 421 | |
| 422 | // Special lowering of v4i32 mul on sse2, sse3: Lower v4i32 mul as 2x shuffle, |
| 423 | // 2x pmuludq, 2x shuffle. |
| 424 | if (ISD == ISD::MUL && LT.second == MVT::v4i32 && ST->hasSSE2() && |
| 425 | !ST->hasSSE41()) |
Andrea Di Biagio | b7882b3 | 2014-02-12 23:43:47 +0000 | [diff] [blame] | 426 | return LT.first * 6; |
Arnold Schwaighofer | 20ef54f | 2013-03-02 04:02:52 +0000 | [diff] [blame] | 427 | |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 428 | // Fallback to the default implementation. |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 429 | return BaseT::getArithmeticInstrCost(Opcode, Ty, Op1Info, Op2Info); |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 432 | unsigned X86TTIImpl::getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index, |
| 433 | Type *SubTp) { |
Karthik Bhat | e03a25d | 2014-06-20 04:32:48 +0000 | [diff] [blame] | 434 | // We only estimate the cost of reverse and alternate shuffles. |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 435 | if (Kind != TTI::SK_Reverse && Kind != TTI::SK_Alternate) |
| 436 | return BaseT::getShuffleCost(Kind, Tp, Index, SubTp); |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 437 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 438 | if (Kind == TTI::SK_Reverse) { |
Karthik Bhat | e03a25d | 2014-06-20 04:32:48 +0000 | [diff] [blame] | 439 | std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Tp); |
| 440 | unsigned Cost = 1; |
| 441 | if (LT.second.getSizeInBits() > 128) |
| 442 | Cost = 3; // Extract + insert + copy. |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 443 | |
Karthik Bhat | e03a25d | 2014-06-20 04:32:48 +0000 | [diff] [blame] | 444 | // Multiple by the number of parts. |
| 445 | return Cost * LT.first; |
| 446 | } |
| 447 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 448 | if (Kind == TTI::SK_Alternate) { |
Andrea Di Biagio | c8e8bda | 2014-07-03 22:24:18 +0000 | [diff] [blame] | 449 | // 64-bit packed float vectors (v2f32) are widened to type v4f32. |
| 450 | // 64-bit packed integer vectors (v2i32) are promoted to type v2i64. |
Karthik Bhat | e03a25d | 2014-06-20 04:32:48 +0000 | [diff] [blame] | 451 | std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Tp); |
| 452 | |
Andrea Di Biagio | c8e8bda | 2014-07-03 22:24:18 +0000 | [diff] [blame] | 453 | // The backend knows how to generate a single VEX.256 version of |
| 454 | // instruction VPBLENDW if the target supports AVX2. |
| 455 | if (ST->hasAVX2() && LT.second == MVT::v16i16) |
| 456 | return LT.first; |
| 457 | |
| 458 | static const CostTblEntry<MVT::SimpleValueType> AVXAltShuffleTbl[] = { |
| 459 | {ISD::VECTOR_SHUFFLE, MVT::v4i64, 1}, // vblendpd |
| 460 | {ISD::VECTOR_SHUFFLE, MVT::v4f64, 1}, // vblendpd |
| 461 | |
| 462 | {ISD::VECTOR_SHUFFLE, MVT::v8i32, 1}, // vblendps |
| 463 | {ISD::VECTOR_SHUFFLE, MVT::v8f32, 1}, // vblendps |
| 464 | |
| 465 | // This shuffle is custom lowered into a sequence of: |
| 466 | // 2x vextractf128 , 2x vpblendw , 1x vinsertf128 |
| 467 | {ISD::VECTOR_SHUFFLE, MVT::v16i16, 5}, |
| 468 | |
| 469 | // This shuffle is custom lowered into a long sequence of: |
| 470 | // 2x vextractf128 , 4x vpshufb , 2x vpor , 1x vinsertf128 |
| 471 | {ISD::VECTOR_SHUFFLE, MVT::v32i8, 9} |
| 472 | }; |
| 473 | |
| 474 | if (ST->hasAVX()) { |
| 475 | int Idx = CostTableLookup(AVXAltShuffleTbl, ISD::VECTOR_SHUFFLE, LT.second); |
| 476 | if (Idx != -1) |
| 477 | return LT.first * AVXAltShuffleTbl[Idx].Cost; |
| 478 | } |
| 479 | |
| 480 | static const CostTblEntry<MVT::SimpleValueType> SSE41AltShuffleTbl[] = { |
| 481 | // These are lowered into movsd. |
| 482 | {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1}, |
| 483 | {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1}, |
| 484 | |
| 485 | // packed float vectors with four elements are lowered into BLENDI dag |
| 486 | // nodes. A v4i32/v4f32 BLENDI generates a single 'blendps'/'blendpd'. |
| 487 | {ISD::VECTOR_SHUFFLE, MVT::v4i32, 1}, |
| 488 | {ISD::VECTOR_SHUFFLE, MVT::v4f32, 1}, |
| 489 | |
| 490 | // This shuffle generates a single pshufw. |
| 491 | {ISD::VECTOR_SHUFFLE, MVT::v8i16, 1}, |
| 492 | |
| 493 | // There is no instruction that matches a v16i8 alternate shuffle. |
| 494 | // The backend will expand it into the sequence 'pshufb + pshufb + or'. |
| 495 | {ISD::VECTOR_SHUFFLE, MVT::v16i8, 3} |
| 496 | }; |
| 497 | |
| 498 | if (ST->hasSSE41()) { |
| 499 | int Idx = CostTableLookup(SSE41AltShuffleTbl, ISD::VECTOR_SHUFFLE, LT.second); |
| 500 | if (Idx != -1) |
| 501 | return LT.first * SSE41AltShuffleTbl[Idx].Cost; |
| 502 | } |
| 503 | |
| 504 | static const CostTblEntry<MVT::SimpleValueType> SSSE3AltShuffleTbl[] = { |
| 505 | {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1}, // movsd |
| 506 | {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1}, // movsd |
| 507 | |
| 508 | // SSE3 doesn't have 'blendps'. The following shuffles are expanded into |
| 509 | // the sequence 'shufps + pshufd' |
| 510 | {ISD::VECTOR_SHUFFLE, MVT::v4i32, 2}, |
| 511 | {ISD::VECTOR_SHUFFLE, MVT::v4f32, 2}, |
| 512 | |
| 513 | {ISD::VECTOR_SHUFFLE, MVT::v8i16, 3}, // pshufb + pshufb + or |
| 514 | {ISD::VECTOR_SHUFFLE, MVT::v16i8, 3} // pshufb + pshufb + or |
| 515 | }; |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 516 | |
Andrea Di Biagio | c8e8bda | 2014-07-03 22:24:18 +0000 | [diff] [blame] | 517 | if (ST->hasSSSE3()) { |
| 518 | int Idx = CostTableLookup(SSSE3AltShuffleTbl, ISD::VECTOR_SHUFFLE, LT.second); |
| 519 | if (Idx != -1) |
| 520 | return LT.first * SSSE3AltShuffleTbl[Idx].Cost; |
| 521 | } |
| 522 | |
| 523 | static const CostTblEntry<MVT::SimpleValueType> SSEAltShuffleTbl[] = { |
| 524 | {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1}, // movsd |
| 525 | {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1}, // movsd |
| 526 | |
| 527 | {ISD::VECTOR_SHUFFLE, MVT::v4i32, 2}, // shufps + pshufd |
| 528 | {ISD::VECTOR_SHUFFLE, MVT::v4f32, 2}, // shufps + pshufd |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 529 | |
Andrea Di Biagio | c8e8bda | 2014-07-03 22:24:18 +0000 | [diff] [blame] | 530 | // This is expanded into a long sequence of four extract + four insert. |
| 531 | {ISD::VECTOR_SHUFFLE, MVT::v8i16, 8}, // 4 x pextrw + 4 pinsrw. |
| 532 | |
| 533 | // 8 x (pinsrw + pextrw + and + movb + movzb + or) |
| 534 | {ISD::VECTOR_SHUFFLE, MVT::v16i8, 48} |
| 535 | }; |
| 536 | |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 537 | // Fall-back (SSE3 and SSE2). |
Andrea Di Biagio | c8e8bda | 2014-07-03 22:24:18 +0000 | [diff] [blame] | 538 | int Idx = CostTableLookup(SSEAltShuffleTbl, ISD::VECTOR_SHUFFLE, LT.second); |
| 539 | if (Idx != -1) |
| 540 | return LT.first * SSEAltShuffleTbl[Idx].Cost; |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 541 | return BaseT::getShuffleCost(Kind, Tp, Index, SubTp); |
Karthik Bhat | e03a25d | 2014-06-20 04:32:48 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 544 | return BaseT::getShuffleCost(Kind, Tp, Index, SubTp); |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 545 | } |
| 546 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 547 | unsigned X86TTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 548 | int ISD = TLI->InstructionOpcodeToISD(Opcode); |
| 549 | assert(ISD && "Invalid opcode"); |
| 550 | |
Arnold Schwaighofer | f47d2d7 | 2013-04-08 18:05:48 +0000 | [diff] [blame] | 551 | std::pair<unsigned, MVT> LTSrc = TLI->getTypeLegalizationCost(Src); |
| 552 | std::pair<unsigned, MVT> LTDest = TLI->getTypeLegalizationCost(Dst); |
| 553 | |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 554 | static const TypeConversionCostTblEntry<MVT::SimpleValueType> |
| 555 | SSE2ConvTbl[] = { |
Arnold Schwaighofer | f47d2d7 | 2013-04-08 18:05:48 +0000 | [diff] [blame] | 556 | // These are somewhat magic numbers justified by looking at the output of |
| 557 | // Intel's IACA, running some kernels and making sure when we take |
| 558 | // legalization into account the throughput will be overestimated. |
| 559 | { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 }, |
| 560 | { ISD::UINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 }, |
| 561 | { ISD::UINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 }, |
| 562 | { ISD::UINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 }, |
| 563 | { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 }, |
| 564 | { ISD::SINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 }, |
| 565 | { ISD::SINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 }, |
| 566 | { ISD::SINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 }, |
| 567 | // There are faster sequences for float conversions. |
| 568 | { ISD::UINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 }, |
Quentin Colombet | 360460b | 2014-11-11 02:23:47 +0000 | [diff] [blame] | 569 | { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 8 }, |
Arnold Schwaighofer | f47d2d7 | 2013-04-08 18:05:48 +0000 | [diff] [blame] | 570 | { ISD::UINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 }, |
| 571 | { ISD::UINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 }, |
| 572 | { ISD::SINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 }, |
| 573 | { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 15 }, |
| 574 | { ISD::SINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 }, |
| 575 | { ISD::SINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 }, |
| 576 | }; |
| 577 | |
| 578 | if (ST->hasSSE2() && !ST->hasAVX()) { |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 579 | int Idx = |
| 580 | ConvertCostTableLookup(SSE2ConvTbl, ISD, LTDest.second, LTSrc.second); |
Arnold Schwaighofer | f47d2d7 | 2013-04-08 18:05:48 +0000 | [diff] [blame] | 581 | if (Idx != -1) |
| 582 | return LTSrc.first * SSE2ConvTbl[Idx].Cost; |
| 583 | } |
| 584 | |
Elena Demikhovsky | 2701247 | 2014-09-16 07:57:37 +0000 | [diff] [blame] | 585 | static const TypeConversionCostTblEntry<MVT::SimpleValueType> |
| 586 | AVX512ConversionTbl[] = { |
| 587 | { ISD::FP_EXTEND, MVT::v8f64, MVT::v8f32, 1 }, |
| 588 | { ISD::FP_EXTEND, MVT::v8f64, MVT::v16f32, 3 }, |
| 589 | { ISD::FP_ROUND, MVT::v8f32, MVT::v8f64, 1 }, |
| 590 | { ISD::FP_ROUND, MVT::v16f32, MVT::v8f64, 3 }, |
| 591 | |
| 592 | { ISD::TRUNCATE, MVT::v16i8, MVT::v16i32, 1 }, |
| 593 | { ISD::TRUNCATE, MVT::v16i16, MVT::v16i32, 1 }, |
| 594 | { ISD::TRUNCATE, MVT::v8i16, MVT::v8i64, 1 }, |
| 595 | { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 1 }, |
| 596 | { ISD::TRUNCATE, MVT::v16i32, MVT::v8i64, 4 }, |
| 597 | |
| 598 | // v16i1 -> v16i32 - load + broadcast |
| 599 | { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i1, 2 }, |
| 600 | { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i1, 2 }, |
| 601 | |
| 602 | { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i8, 1 }, |
| 603 | { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i8, 1 }, |
| 604 | { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i16, 1 }, |
| 605 | { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i16, 1 }, |
| 606 | { ISD::SIGN_EXTEND, MVT::v8i64, MVT::v16i32, 3 }, |
| 607 | { ISD::ZERO_EXTEND, MVT::v8i64, MVT::v16i32, 3 }, |
| 608 | |
Elena Demikhovsky | d5e95b5 | 2014-11-13 11:46:16 +0000 | [diff] [blame] | 609 | { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i1, 3 }, |
| 610 | { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i8, 2 }, |
| 611 | { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i16, 2 }, |
| 612 | { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i32, 1 }, |
| 613 | { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i1, 4 }, |
| 614 | { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i16, 2 }, |
| 615 | { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i32, 1 }, |
Elena Demikhovsky | 2701247 | 2014-09-16 07:57:37 +0000 | [diff] [blame] | 616 | }; |
| 617 | |
| 618 | if (ST->hasAVX512()) { |
| 619 | int Idx = ConvertCostTableLookup(AVX512ConversionTbl, ISD, LTDest.second, |
| 620 | LTSrc.second); |
| 621 | if (Idx != -1) |
| 622 | return AVX512ConversionTbl[Idx].Cost; |
| 623 | } |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 624 | EVT SrcTy = TLI->getValueType(Src); |
| 625 | EVT DstTy = TLI->getValueType(Dst); |
| 626 | |
Arnold Schwaighofer | c0c7ff4 | 2013-04-17 20:04:53 +0000 | [diff] [blame] | 627 | // The function getSimpleVT only handles simple value types. |
| 628 | if (!SrcTy.isSimple() || !DstTy.isSimple()) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 629 | return BaseT::getCastInstrCost(Opcode, Dst, Src); |
Arnold Schwaighofer | c0c7ff4 | 2013-04-17 20:04:53 +0000 | [diff] [blame] | 630 | |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 631 | static const TypeConversionCostTblEntry<MVT::SimpleValueType> |
Tim Northover | f0e2161 | 2014-02-06 18:18:36 +0000 | [diff] [blame] | 632 | AVX2ConversionTbl[] = { |
| 633 | { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 1 }, |
| 634 | { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 1 }, |
| 635 | { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 3 }, |
| 636 | { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 3 }, |
| 637 | { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 3 }, |
| 638 | { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 3 }, |
| 639 | { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 1 }, |
| 640 | { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 1 }, |
| 641 | { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i1, 3 }, |
| 642 | { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i1, 3 }, |
| 643 | { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 3 }, |
| 644 | { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 3 }, |
| 645 | { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 3 }, |
| 646 | { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 }, |
| 647 | { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 1 }, |
| 648 | { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 1 }, |
| 649 | |
| 650 | { ISD::TRUNCATE, MVT::v4i8, MVT::v4i64, 2 }, |
| 651 | { ISD::TRUNCATE, MVT::v4i16, MVT::v4i64, 2 }, |
| 652 | { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 2 }, |
| 653 | { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 2 }, |
| 654 | { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 2 }, |
| 655 | { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 4 }, |
Elena Demikhovsky | 2701247 | 2014-09-16 07:57:37 +0000 | [diff] [blame] | 656 | |
| 657 | { ISD::FP_EXTEND, MVT::v8f64, MVT::v8f32, 3 }, |
| 658 | { ISD::FP_ROUND, MVT::v8f32, MVT::v8f64, 3 }, |
Quentin Colombet | 360460b | 2014-11-11 02:23:47 +0000 | [diff] [blame] | 659 | |
| 660 | { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 8 }, |
Tim Northover | f0e2161 | 2014-02-06 18:18:36 +0000 | [diff] [blame] | 661 | }; |
| 662 | |
| 663 | static const TypeConversionCostTblEntry<MVT::SimpleValueType> |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 664 | AVXConversionTbl[] = { |
Tim Northover | f0e2161 | 2014-02-06 18:18:36 +0000 | [diff] [blame] | 665 | { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 4 }, |
| 666 | { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 4 }, |
| 667 | { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 7 }, |
| 668 | { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 4 }, |
| 669 | { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 7 }, |
| 670 | { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 4 }, |
| 671 | { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 4 }, |
| 672 | { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 4 }, |
| 673 | { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i1, 6 }, |
| 674 | { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i1, 4 }, |
| 675 | { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 6 }, |
| 676 | { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 4 }, |
| 677 | { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 6 }, |
| 678 | { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 }, |
| 679 | { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 4 }, |
| 680 | { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 4 }, |
| 681 | |
| 682 | { ISD::TRUNCATE, MVT::v4i8, MVT::v4i64, 4 }, |
| 683 | { ISD::TRUNCATE, MVT::v4i16, MVT::v4i64, 4 }, |
| 684 | { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 4 }, |
| 685 | { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 4 }, |
| 686 | { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 5 }, |
| 687 | { ISD::TRUNCATE, MVT::v16i8, MVT::v16i16, 4 }, |
| 688 | { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 9 }, |
Benjamin Kramer | 52ceb44 | 2013-04-01 10:23:49 +0000 | [diff] [blame] | 689 | |
| 690 | { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i1, 8 }, |
| 691 | { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i8, 8 }, |
| 692 | { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i16, 5 }, |
| 693 | { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i32, 1 }, |
| 694 | { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i1, 3 }, |
| 695 | { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i8, 3 }, |
| 696 | { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i16, 3 }, |
| 697 | { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 }, |
| 698 | { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i1, 3 }, |
| 699 | { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i8, 3 }, |
| 700 | { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i16, 3 }, |
| 701 | { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i32, 1 }, |
| 702 | |
| 703 | { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i1, 6 }, |
| 704 | { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i8, 5 }, |
| 705 | { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i16, 5 }, |
| 706 | { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 9 }, |
| 707 | { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i1, 7 }, |
| 708 | { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i8, 2 }, |
| 709 | { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i16, 2 }, |
| 710 | { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 6 }, |
| 711 | { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i1, 7 }, |
| 712 | { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i8, 2 }, |
| 713 | { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i16, 2 }, |
| 714 | { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i32, 6 }, |
Quentin Colombet | 85b904d | 2014-03-27 22:27:41 +0000 | [diff] [blame] | 715 | // The generic code to compute the scalar overhead is currently broken. |
| 716 | // Workaround this limitation by estimating the scalarization overhead |
| 717 | // here. We have roughly 10 instructions per scalar element. |
| 718 | // Multiply that by the vector width. |
| 719 | // FIXME: remove that when PR19268 is fixed. |
Quentin Colombet | 3914bf5 | 2014-03-27 00:52:16 +0000 | [diff] [blame] | 720 | { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 }, |
| 721 | { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i64, 4*10 }, |
Benjamin Kramer | 52ceb44 | 2013-04-01 10:23:49 +0000 | [diff] [blame] | 722 | |
Jim Grosbach | 72fbde8 | 2014-03-27 00:04:11 +0000 | [diff] [blame] | 723 | { ISD::FP_TO_SINT, MVT::v8i8, MVT::v8f32, 7 }, |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 724 | { ISD::FP_TO_SINT, MVT::v4i8, MVT::v4f32, 1 }, |
Adam Nemet | 6dafe97 | 2014-03-30 18:07:13 +0000 | [diff] [blame] | 725 | // This node is expanded into scalarized operations but BasicTTI is overly |
| 726 | // optimistic estimating its cost. It computes 3 per element (one |
| 727 | // vector-extract, one scalar conversion and one vector-insert). The |
| 728 | // problem is that the inserts form a read-modify-write chain so latency |
| 729 | // should be factored in too. Inflating the cost per element by 1. |
| 730 | { ISD::FP_TO_UINT, MVT::v8i32, MVT::v8f32, 8*4 }, |
Adam Nemet | 10c4ce2 | 2014-03-31 21:54:48 +0000 | [diff] [blame] | 731 | { ISD::FP_TO_UINT, MVT::v4i32, MVT::v4f64, 4*4 }, |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 732 | }; |
| 733 | |
Tim Northover | f0e2161 | 2014-02-06 18:18:36 +0000 | [diff] [blame] | 734 | if (ST->hasAVX2()) { |
| 735 | int Idx = ConvertCostTableLookup(AVX2ConversionTbl, ISD, |
| 736 | DstTy.getSimpleVT(), SrcTy.getSimpleVT()); |
| 737 | if (Idx != -1) |
| 738 | return AVX2ConversionTbl[Idx].Cost; |
| 739 | } |
| 740 | |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 741 | if (ST->hasAVX()) { |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 742 | int Idx = ConvertCostTableLookup(AVXConversionTbl, ISD, DstTy.getSimpleVT(), |
| 743 | SrcTy.getSimpleVT()); |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 744 | if (Idx != -1) |
| 745 | return AVXConversionTbl[Idx].Cost; |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 746 | } |
| 747 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 748 | return BaseT::getCastInstrCost(Opcode, Dst, Src); |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 751 | unsigned X86TTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, |
| 752 | Type *CondTy) { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 753 | // Legalize the type. |
| 754 | std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy); |
| 755 | |
| 756 | MVT MTy = LT.second; |
| 757 | |
| 758 | int ISD = TLI->InstructionOpcodeToISD(Opcode); |
| 759 | assert(ISD && "Invalid opcode"); |
| 760 | |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 761 | static const CostTblEntry<MVT::SimpleValueType> SSE42CostTbl[] = { |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 762 | { ISD::SETCC, MVT::v2f64, 1 }, |
| 763 | { ISD::SETCC, MVT::v4f32, 1 }, |
| 764 | { ISD::SETCC, MVT::v2i64, 1 }, |
| 765 | { ISD::SETCC, MVT::v4i32, 1 }, |
| 766 | { ISD::SETCC, MVT::v8i16, 1 }, |
| 767 | { ISD::SETCC, MVT::v16i8, 1 }, |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 768 | }; |
| 769 | |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 770 | static const CostTblEntry<MVT::SimpleValueType> AVX1CostTbl[] = { |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 771 | { ISD::SETCC, MVT::v4f64, 1 }, |
| 772 | { ISD::SETCC, MVT::v8f32, 1 }, |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 773 | // AVX1 does not support 8-wide integer compare. |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 774 | { ISD::SETCC, MVT::v4i64, 4 }, |
| 775 | { ISD::SETCC, MVT::v8i32, 4 }, |
| 776 | { ISD::SETCC, MVT::v16i16, 4 }, |
| 777 | { ISD::SETCC, MVT::v32i8, 4 }, |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 778 | }; |
| 779 | |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 780 | static const CostTblEntry<MVT::SimpleValueType> AVX2CostTbl[] = { |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 781 | { ISD::SETCC, MVT::v4i64, 1 }, |
| 782 | { ISD::SETCC, MVT::v8i32, 1 }, |
| 783 | { ISD::SETCC, MVT::v16i16, 1 }, |
| 784 | { ISD::SETCC, MVT::v32i8, 1 }, |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 785 | }; |
| 786 | |
Elena Demikhovsky | 2701247 | 2014-09-16 07:57:37 +0000 | [diff] [blame] | 787 | static const CostTblEntry<MVT::SimpleValueType> AVX512CostTbl[] = { |
| 788 | { ISD::SETCC, MVT::v8i64, 1 }, |
| 789 | { ISD::SETCC, MVT::v16i32, 1 }, |
| 790 | { ISD::SETCC, MVT::v8f64, 1 }, |
| 791 | { ISD::SETCC, MVT::v16f32, 1 }, |
| 792 | }; |
| 793 | |
| 794 | if (ST->hasAVX512()) { |
| 795 | int Idx = CostTableLookup(AVX512CostTbl, ISD, MTy); |
| 796 | if (Idx != -1) |
| 797 | return LT.first * AVX512CostTbl[Idx].Cost; |
| 798 | } |
| 799 | |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 800 | if (ST->hasAVX2()) { |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 801 | int Idx = CostTableLookup(AVX2CostTbl, ISD, MTy); |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 802 | if (Idx != -1) |
| 803 | return LT.first * AVX2CostTbl[Idx].Cost; |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | if (ST->hasAVX()) { |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 807 | int Idx = CostTableLookup(AVX1CostTbl, ISD, MTy); |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 808 | if (Idx != -1) |
| 809 | return LT.first * AVX1CostTbl[Idx].Cost; |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | if (ST->hasSSE42()) { |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 813 | int Idx = CostTableLookup(SSE42CostTbl, ISD, MTy); |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 814 | if (Idx != -1) |
| 815 | return LT.first * SSE42CostTbl[Idx].Cost; |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 818 | return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy); |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 821 | unsigned X86TTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val, |
| 822 | unsigned Index) { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 823 | assert(Val->isVectorTy() && "This must be a vector type"); |
| 824 | |
| 825 | if (Index != -1U) { |
| 826 | // Legalize the type. |
| 827 | std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Val); |
| 828 | |
| 829 | // This type is legalized to a scalar type. |
| 830 | if (!LT.second.isVector()) |
| 831 | return 0; |
| 832 | |
| 833 | // The type may be split. Normalize the index to the new type. |
| 834 | unsigned Width = LT.second.getVectorNumElements(); |
| 835 | Index = Index % Width; |
| 836 | |
| 837 | // Floating point scalars are already located in index #0. |
| 838 | if (Val->getScalarType()->isFloatingPointTy() && Index == 0) |
| 839 | return 0; |
| 840 | } |
| 841 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 842 | return BaseT::getVectorInstrCost(Opcode, Val, Index); |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 845 | unsigned X86TTIImpl::getScalarizationOverhead(Type *Ty, bool Insert, |
| 846 | bool Extract) { |
Nadav Rotem | f9ecbcb | 2013-06-27 17:52:04 +0000 | [diff] [blame] | 847 | assert (Ty->isVectorTy() && "Can only scalarize vectors"); |
| 848 | unsigned Cost = 0; |
| 849 | |
| 850 | for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) { |
| 851 | if (Insert) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 852 | Cost += getVectorInstrCost(Instruction::InsertElement, Ty, i); |
Nadav Rotem | f9ecbcb | 2013-06-27 17:52:04 +0000 | [diff] [blame] | 853 | if (Extract) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 854 | Cost += getVectorInstrCost(Instruction::ExtractElement, Ty, i); |
Nadav Rotem | f9ecbcb | 2013-06-27 17:52:04 +0000 | [diff] [blame] | 855 | } |
| 856 | |
| 857 | return Cost; |
| 858 | } |
| 859 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 860 | unsigned X86TTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src, |
| 861 | unsigned Alignment, |
| 862 | unsigned AddressSpace) { |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 863 | // Handle non-power-of-two vectors such as <3 x float> |
Nadav Rotem | f9ecbcb | 2013-06-27 17:52:04 +0000 | [diff] [blame] | 864 | if (VectorType *VTy = dyn_cast<VectorType>(Src)) { |
| 865 | unsigned NumElem = VTy->getVectorNumElements(); |
| 866 | |
| 867 | // Handle a few common cases: |
| 868 | // <3 x float> |
| 869 | if (NumElem == 3 && VTy->getScalarSizeInBits() == 32) |
| 870 | // Cost = 64 bit store + extract + 32 bit store. |
| 871 | return 3; |
| 872 | |
| 873 | // <3 x double> |
| 874 | if (NumElem == 3 && VTy->getScalarSizeInBits() == 64) |
| 875 | // Cost = 128 bit store + unpack + 64 bit store. |
| 876 | return 3; |
| 877 | |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 878 | // Assume that all other non-power-of-two numbers are scalarized. |
Nadav Rotem | f9ecbcb | 2013-06-27 17:52:04 +0000 | [diff] [blame] | 879 | if (!isPowerOf2_32(NumElem)) { |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 880 | unsigned Cost = BaseT::getMemoryOpCost(Opcode, VTy->getScalarType(), |
| 881 | Alignment, AddressSpace); |
Nadav Rotem | f9ecbcb | 2013-06-27 17:52:04 +0000 | [diff] [blame] | 882 | unsigned SplitCost = getScalarizationOverhead(Src, |
| 883 | Opcode == Instruction::Load, |
| 884 | Opcode==Instruction::Store); |
| 885 | return NumElem * Cost + SplitCost; |
| 886 | } |
| 887 | } |
| 888 | |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 889 | // Legalize the type. |
| 890 | std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Src); |
| 891 | assert((Opcode == Instruction::Load || Opcode == Instruction::Store) && |
| 892 | "Invalid Opcode"); |
| 893 | |
| 894 | // Each load/store unit costs 1. |
| 895 | unsigned Cost = LT.first * 1; |
| 896 | |
| 897 | // On Sandybridge 256bit load/stores are double pumped |
| 898 | // (but not on Haswell). |
| 899 | if (LT.second.getSizeInBits() > 128 && !ST->hasAVX2()) |
| 900 | Cost*=2; |
| 901 | |
| 902 | return Cost; |
| 903 | } |
Arnold Schwaighofer | 6042a26 | 2013-07-12 19:16:07 +0000 | [diff] [blame] | 904 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 905 | unsigned X86TTIImpl::getMaskedMemoryOpCost(unsigned Opcode, Type *SrcTy, |
| 906 | unsigned Alignment, |
| 907 | unsigned AddressSpace) { |
Elena Demikhovsky | a3232f7 | 2015-01-25 08:44:46 +0000 | [diff] [blame] | 908 | VectorType *SrcVTy = dyn_cast<VectorType>(SrcTy); |
| 909 | if (!SrcVTy) |
| 910 | // To calculate scalar take the regular cost, without mask |
| 911 | return getMemoryOpCost(Opcode, SrcTy, Alignment, AddressSpace); |
| 912 | |
| 913 | unsigned NumElem = SrcVTy->getVectorNumElements(); |
| 914 | VectorType *MaskTy = |
| 915 | VectorType::get(Type::getInt8Ty(getGlobalContext()), NumElem); |
| 916 | if ((Opcode == Instruction::Load && !isLegalMaskedLoad(SrcVTy, 1)) || |
| 917 | (Opcode == Instruction::Store && !isLegalMaskedStore(SrcVTy, 1)) || |
| 918 | !isPowerOf2_32(NumElem)) { |
| 919 | // Scalarization |
| 920 | unsigned MaskSplitCost = getScalarizationOverhead(MaskTy, false, true); |
| 921 | unsigned ScalarCompareCost = |
| 922 | getCmpSelInstrCost(Instruction::ICmp, |
| 923 | Type::getInt8Ty(getGlobalContext()), NULL); |
| 924 | unsigned BranchCost = getCFInstrCost(Instruction::Br); |
| 925 | unsigned MaskCmpCost = NumElem * (BranchCost + ScalarCompareCost); |
| 926 | |
| 927 | unsigned ValueSplitCost = |
| 928 | getScalarizationOverhead(SrcVTy, Opcode == Instruction::Load, |
| 929 | Opcode == Instruction::Store); |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 930 | unsigned MemopCost = |
| 931 | NumElem * BaseT::getMemoryOpCost(Opcode, SrcVTy->getScalarType(), |
| 932 | Alignment, AddressSpace); |
Elena Demikhovsky | a3232f7 | 2015-01-25 08:44:46 +0000 | [diff] [blame] | 933 | return MemopCost + ValueSplitCost + MaskSplitCost + MaskCmpCost; |
| 934 | } |
| 935 | |
| 936 | // Legalize the type. |
| 937 | std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(SrcVTy); |
| 938 | unsigned Cost = 0; |
| 939 | if (LT.second != TLI->getValueType(SrcVTy).getSimpleVT() && |
| 940 | LT.second.getVectorNumElements() == NumElem) |
| 941 | // Promotion requires expand/truncate for data and a shuffle for mask. |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 942 | Cost += getShuffleCost(TTI::SK_Alternate, SrcVTy, 0, 0) + |
| 943 | getShuffleCost(TTI::SK_Alternate, MaskTy, 0, 0); |
| 944 | |
Elena Demikhovsky | a3232f7 | 2015-01-25 08:44:46 +0000 | [diff] [blame] | 945 | else if (LT.second.getVectorNumElements() > NumElem) { |
| 946 | VectorType *NewMaskTy = VectorType::get(MaskTy->getVectorElementType(), |
| 947 | LT.second.getVectorNumElements()); |
| 948 | // Expanding requires fill mask with zeroes |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 949 | Cost += getShuffleCost(TTI::SK_InsertSubvector, NewMaskTy, 0, MaskTy); |
Elena Demikhovsky | a3232f7 | 2015-01-25 08:44:46 +0000 | [diff] [blame] | 950 | } |
| 951 | if (!ST->hasAVX512()) |
| 952 | return Cost + LT.first*4; // Each maskmov costs 4 |
| 953 | |
| 954 | // AVX-512 masked load/store is cheapper |
| 955 | return Cost+LT.first; |
| 956 | } |
| 957 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 958 | unsigned X86TTIImpl::getAddressComputationCost(Type *Ty, bool IsComplex) { |
Arnold Schwaighofer | 6042a26 | 2013-07-12 19:16:07 +0000 | [diff] [blame] | 959 | // Address computations in vectorized code with non-consecutive addresses will |
| 960 | // likely result in more instructions compared to scalar code where the |
| 961 | // computation can more often be merged into the index mode. The resulting |
| 962 | // extra micro-ops can significantly decrease throughput. |
| 963 | unsigned NumVectorInstToHideOverhead = 10; |
| 964 | |
| 965 | if (Ty->isVectorTy() && IsComplex) |
| 966 | return NumVectorInstToHideOverhead; |
| 967 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 968 | return BaseT::getAddressComputationCost(Ty, IsComplex); |
Arnold Schwaighofer | 6042a26 | 2013-07-12 19:16:07 +0000 | [diff] [blame] | 969 | } |
Yi Jiang | 5c343de | 2013-09-19 17:48:48 +0000 | [diff] [blame] | 970 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 971 | unsigned X86TTIImpl::getReductionCost(unsigned Opcode, Type *ValTy, |
| 972 | bool IsPairwise) { |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 973 | |
Yi Jiang | 5c343de | 2013-09-19 17:48:48 +0000 | [diff] [blame] | 974 | std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy); |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 975 | |
Yi Jiang | 5c343de | 2013-09-19 17:48:48 +0000 | [diff] [blame] | 976 | MVT MTy = LT.second; |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 977 | |
Yi Jiang | 5c343de | 2013-09-19 17:48:48 +0000 | [diff] [blame] | 978 | int ISD = TLI->InstructionOpcodeToISD(Opcode); |
| 979 | assert(ISD && "Invalid opcode"); |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 980 | |
| 981 | // We use the Intel Architecture Code Analyzer(IACA) to measure the throughput |
| 982 | // and make it as the cost. |
| 983 | |
Yi Jiang | 5c343de | 2013-09-19 17:48:48 +0000 | [diff] [blame] | 984 | static const CostTblEntry<MVT::SimpleValueType> SSE42CostTblPairWise[] = { |
| 985 | { ISD::FADD, MVT::v2f64, 2 }, |
| 986 | { ISD::FADD, MVT::v4f32, 4 }, |
| 987 | { ISD::ADD, MVT::v2i64, 2 }, // The data reported by the IACA tool is "1.6". |
| 988 | { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.5". |
| 989 | { ISD::ADD, MVT::v8i16, 5 }, |
| 990 | }; |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 991 | |
Yi Jiang | 5c343de | 2013-09-19 17:48:48 +0000 | [diff] [blame] | 992 | static const CostTblEntry<MVT::SimpleValueType> AVX1CostTblPairWise[] = { |
| 993 | { ISD::FADD, MVT::v4f32, 4 }, |
| 994 | { ISD::FADD, MVT::v4f64, 5 }, |
| 995 | { ISD::FADD, MVT::v8f32, 7 }, |
| 996 | { ISD::ADD, MVT::v2i64, 1 }, // The data reported by the IACA tool is "1.5". |
| 997 | { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.5". |
| 998 | { ISD::ADD, MVT::v4i64, 5 }, // The data reported by the IACA tool is "4.8". |
| 999 | { ISD::ADD, MVT::v8i16, 5 }, |
| 1000 | { ISD::ADD, MVT::v8i32, 5 }, |
| 1001 | }; |
| 1002 | |
| 1003 | static const CostTblEntry<MVT::SimpleValueType> SSE42CostTblNoPairWise[] = { |
| 1004 | { ISD::FADD, MVT::v2f64, 2 }, |
| 1005 | { ISD::FADD, MVT::v4f32, 4 }, |
| 1006 | { ISD::ADD, MVT::v2i64, 2 }, // The data reported by the IACA tool is "1.6". |
| 1007 | { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.3". |
| 1008 | { ISD::ADD, MVT::v8i16, 4 }, // The data reported by the IACA tool is "4.3". |
| 1009 | }; |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 1010 | |
Yi Jiang | 5c343de | 2013-09-19 17:48:48 +0000 | [diff] [blame] | 1011 | static const CostTblEntry<MVT::SimpleValueType> AVX1CostTblNoPairWise[] = { |
| 1012 | { ISD::FADD, MVT::v4f32, 3 }, |
| 1013 | { ISD::FADD, MVT::v4f64, 3 }, |
| 1014 | { ISD::FADD, MVT::v8f32, 4 }, |
| 1015 | { ISD::ADD, MVT::v2i64, 1 }, // The data reported by the IACA tool is "1.5". |
| 1016 | { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "2.8". |
| 1017 | { ISD::ADD, MVT::v4i64, 3 }, |
| 1018 | { ISD::ADD, MVT::v8i16, 4 }, |
| 1019 | { ISD::ADD, MVT::v8i32, 5 }, |
| 1020 | }; |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 1021 | |
Yi Jiang | 5c343de | 2013-09-19 17:48:48 +0000 | [diff] [blame] | 1022 | if (IsPairwise) { |
| 1023 | if (ST->hasAVX()) { |
| 1024 | int Idx = CostTableLookup(AVX1CostTblPairWise, ISD, MTy); |
| 1025 | if (Idx != -1) |
| 1026 | return LT.first * AVX1CostTblPairWise[Idx].Cost; |
| 1027 | } |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 1028 | |
Yi Jiang | 5c343de | 2013-09-19 17:48:48 +0000 | [diff] [blame] | 1029 | if (ST->hasSSE42()) { |
| 1030 | int Idx = CostTableLookup(SSE42CostTblPairWise, ISD, MTy); |
| 1031 | if (Idx != -1) |
| 1032 | return LT.first * SSE42CostTblPairWise[Idx].Cost; |
| 1033 | } |
| 1034 | } else { |
| 1035 | if (ST->hasAVX()) { |
| 1036 | int Idx = CostTableLookup(AVX1CostTblNoPairWise, ISD, MTy); |
| 1037 | if (Idx != -1) |
| 1038 | return LT.first * AVX1CostTblNoPairWise[Idx].Cost; |
| 1039 | } |
Michael Liao | 5bf9578 | 2014-12-04 05:20:33 +0000 | [diff] [blame] | 1040 | |
Yi Jiang | 5c343de | 2013-09-19 17:48:48 +0000 | [diff] [blame] | 1041 | if (ST->hasSSE42()) { |
| 1042 | int Idx = CostTableLookup(SSE42CostTblNoPairWise, ISD, MTy); |
| 1043 | if (Idx != -1) |
| 1044 | return LT.first * SSE42CostTblNoPairWise[Idx].Cost; |
| 1045 | } |
| 1046 | } |
| 1047 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 1048 | return BaseT::getReductionCost(Opcode, ValTy, IsPairwise); |
Yi Jiang | 5c343de | 2013-09-19 17:48:48 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
Juergen Ributzka | b2e4edb | 2014-06-10 00:32:29 +0000 | [diff] [blame] | 1051 | /// \brief Calculate the cost of materializing a 64-bit value. This helper |
| 1052 | /// method might only calculate a fraction of a larger immediate. Therefore it |
| 1053 | /// is valid to return a cost of ZERO. |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 1054 | unsigned X86TTIImpl::getIntImmCost(int64_t Val) { |
Juergen Ributzka | b2e4edb | 2014-06-10 00:32:29 +0000 | [diff] [blame] | 1055 | if (Val == 0) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 1056 | return TTI::TCC_Free; |
Juergen Ributzka | b2e4edb | 2014-06-10 00:32:29 +0000 | [diff] [blame] | 1057 | |
| 1058 | if (isInt<32>(Val)) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 1059 | return TTI::TCC_Basic; |
Juergen Ributzka | b2e4edb | 2014-06-10 00:32:29 +0000 | [diff] [blame] | 1060 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 1061 | return 2 * TTI::TCC_Basic; |
Juergen Ributzka | b2e4edb | 2014-06-10 00:32:29 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 1064 | unsigned X86TTIImpl::getIntImmCost(const APInt &Imm, Type *Ty) { |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1065 | assert(Ty->isIntegerTy()); |
| 1066 | |
| 1067 | unsigned BitSize = Ty->getPrimitiveSizeInBits(); |
| 1068 | if (BitSize == 0) |
| 1069 | return ~0U; |
| 1070 | |
Juergen Ributzka | 4317617 | 2014-05-19 21:00:53 +0000 | [diff] [blame] | 1071 | // Never hoist constants larger than 128bit, because this might lead to |
| 1072 | // incorrect code generation or assertions in codegen. |
| 1073 | // Fixme: Create a cost model for types larger than i128 once the codegen |
| 1074 | // issues have been fixed. |
| 1075 | if (BitSize > 128) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 1076 | return TTI::TCC_Free; |
Juergen Ributzka | 4317617 | 2014-05-19 21:00:53 +0000 | [diff] [blame] | 1077 | |
Juergen Ributzka | f0dff49 | 2014-03-21 06:04:45 +0000 | [diff] [blame] | 1078 | if (Imm == 0) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 1079 | return TTI::TCC_Free; |
Juergen Ributzka | f0dff49 | 2014-03-21 06:04:45 +0000 | [diff] [blame] | 1080 | |
Juergen Ributzka | b2e4edb | 2014-06-10 00:32:29 +0000 | [diff] [blame] | 1081 | // Sign-extend all constants to a multiple of 64-bit. |
| 1082 | APInt ImmVal = Imm; |
| 1083 | if (BitSize & 0x3f) |
| 1084 | ImmVal = Imm.sext((BitSize + 63) & ~0x3fU); |
| 1085 | |
| 1086 | // Split the constant into 64-bit chunks and calculate the cost for each |
| 1087 | // chunk. |
| 1088 | unsigned Cost = 0; |
| 1089 | for (unsigned ShiftVal = 0; ShiftVal < BitSize; ShiftVal += 64) { |
| 1090 | APInt Tmp = ImmVal.ashr(ShiftVal).sextOrTrunc(64); |
| 1091 | int64_t Val = Tmp.getSExtValue(); |
| 1092 | Cost += getIntImmCost(Val); |
| 1093 | } |
| 1094 | // We need at least one instruction to materialze the constant. |
| 1095 | return std::max(1U, Cost); |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 1098 | unsigned X86TTIImpl::getIntImmCost(unsigned Opcode, unsigned Idx, |
| 1099 | const APInt &Imm, Type *Ty) { |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1100 | assert(Ty->isIntegerTy()); |
| 1101 | |
| 1102 | unsigned BitSize = Ty->getPrimitiveSizeInBits(); |
Juergen Ributzka | 4317617 | 2014-05-19 21:00:53 +0000 | [diff] [blame] | 1103 | // There is no cost model for constants with a bit size of 0. Return TCC_Free |
| 1104 | // here, so that constant hoisting will ignore this constant. |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1105 | if (BitSize == 0) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 1106 | return TTI::TCC_Free; |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1107 | |
Juergen Ributzka | f0dff49 | 2014-03-21 06:04:45 +0000 | [diff] [blame] | 1108 | unsigned ImmIdx = ~0U; |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1109 | switch (Opcode) { |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 1110 | default: |
| 1111 | return TTI::TCC_Free; |
Juergen Ributzka | f0dff49 | 2014-03-21 06:04:45 +0000 | [diff] [blame] | 1112 | case Instruction::GetElementPtr: |
Juergen Ributzka | 27435b3 | 2014-04-02 21:45:36 +0000 | [diff] [blame] | 1113 | // Always hoist the base address of a GetElementPtr. This prevents the |
| 1114 | // creation of new constants for every base constant that gets constant |
| 1115 | // folded with the offset. |
Juergen Ributzka | 631c491 | 2014-03-25 18:01:25 +0000 | [diff] [blame] | 1116 | if (Idx == 0) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 1117 | return 2 * TTI::TCC_Basic; |
| 1118 | return TTI::TCC_Free; |
Juergen Ributzka | f0dff49 | 2014-03-21 06:04:45 +0000 | [diff] [blame] | 1119 | case Instruction::Store: |
| 1120 | ImmIdx = 0; |
| 1121 | break; |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1122 | case Instruction::Add: |
| 1123 | case Instruction::Sub: |
| 1124 | case Instruction::Mul: |
| 1125 | case Instruction::UDiv: |
| 1126 | case Instruction::SDiv: |
| 1127 | case Instruction::URem: |
| 1128 | case Instruction::SRem: |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1129 | case Instruction::And: |
| 1130 | case Instruction::Or: |
| 1131 | case Instruction::Xor: |
| 1132 | case Instruction::ICmp: |
Juergen Ributzka | f0dff49 | 2014-03-21 06:04:45 +0000 | [diff] [blame] | 1133 | ImmIdx = 1; |
| 1134 | break; |
Michael Zolotukhin | 1f4a960 | 2014-04-30 19:17:32 +0000 | [diff] [blame] | 1135 | // Always return TCC_Free for the shift value of a shift instruction. |
| 1136 | case Instruction::Shl: |
| 1137 | case Instruction::LShr: |
| 1138 | case Instruction::AShr: |
| 1139 | if (Idx == 1) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 1140 | return TTI::TCC_Free; |
Michael Zolotukhin | 1f4a960 | 2014-04-30 19:17:32 +0000 | [diff] [blame] | 1141 | break; |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1142 | case Instruction::Trunc: |
| 1143 | case Instruction::ZExt: |
| 1144 | case Instruction::SExt: |
| 1145 | case Instruction::IntToPtr: |
| 1146 | case Instruction::PtrToInt: |
| 1147 | case Instruction::BitCast: |
Juergen Ributzka | f0dff49 | 2014-03-21 06:04:45 +0000 | [diff] [blame] | 1148 | case Instruction::PHI: |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1149 | case Instruction::Call: |
| 1150 | case Instruction::Select: |
| 1151 | case Instruction::Ret: |
| 1152 | case Instruction::Load: |
Juergen Ributzka | f0dff49 | 2014-03-21 06:04:45 +0000 | [diff] [blame] | 1153 | break; |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1154 | } |
Juergen Ributzka | f0dff49 | 2014-03-21 06:04:45 +0000 | [diff] [blame] | 1155 | |
Juergen Ributzka | b2e4edb | 2014-06-10 00:32:29 +0000 | [diff] [blame] | 1156 | if (Idx == ImmIdx) { |
| 1157 | unsigned NumConstants = (BitSize + 63) / 64; |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 1158 | unsigned Cost = X86TTIImpl::getIntImmCost(Imm, Ty); |
| 1159 | return (Cost <= NumConstants * TTI::TCC_Basic) |
| 1160 | ? static_cast<unsigned>(TTI::TCC_Free) |
| 1161 | : Cost; |
Juergen Ributzka | b2e4edb | 2014-06-10 00:32:29 +0000 | [diff] [blame] | 1162 | } |
Juergen Ributzka | f0dff49 | 2014-03-21 06:04:45 +0000 | [diff] [blame] | 1163 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 1164 | return X86TTIImpl::getIntImmCost(Imm, Ty); |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 1167 | unsigned X86TTIImpl::getIntImmCost(Intrinsic::ID IID, unsigned Idx, |
| 1168 | const APInt &Imm, Type *Ty) { |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1169 | assert(Ty->isIntegerTy()); |
| 1170 | |
| 1171 | unsigned BitSize = Ty->getPrimitiveSizeInBits(); |
Juergen Ributzka | 4317617 | 2014-05-19 21:00:53 +0000 | [diff] [blame] | 1172 | // There is no cost model for constants with a bit size of 0. Return TCC_Free |
| 1173 | // here, so that constant hoisting will ignore this constant. |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1174 | if (BitSize == 0) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 1175 | return TTI::TCC_Free; |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1176 | |
| 1177 | switch (IID) { |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 1178 | default: |
| 1179 | return TTI::TCC_Free; |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1180 | case Intrinsic::sadd_with_overflow: |
| 1181 | case Intrinsic::uadd_with_overflow: |
| 1182 | case Intrinsic::ssub_with_overflow: |
| 1183 | case Intrinsic::usub_with_overflow: |
| 1184 | case Intrinsic::smul_with_overflow: |
| 1185 | case Intrinsic::umul_with_overflow: |
Juergen Ributzka | f0dff49 | 2014-03-21 06:04:45 +0000 | [diff] [blame] | 1186 | if ((Idx == 1) && Imm.getBitWidth() <= 64 && isInt<32>(Imm.getSExtValue())) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 1187 | return TTI::TCC_Free; |
Juergen Ributzka | 5eef98c | 2014-03-25 18:01:23 +0000 | [diff] [blame] | 1188 | break; |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1189 | case Intrinsic::experimental_stackmap: |
Juergen Ributzka | 5eef98c | 2014-03-25 18:01:23 +0000 | [diff] [blame] | 1190 | if ((Idx < 2) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue()))) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 1191 | return TTI::TCC_Free; |
Juergen Ributzka | 5eef98c | 2014-03-25 18:01:23 +0000 | [diff] [blame] | 1192 | break; |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1193 | case Intrinsic::experimental_patchpoint_void: |
| 1194 | case Intrinsic::experimental_patchpoint_i64: |
Juergen Ributzka | 5eef98c | 2014-03-25 18:01:23 +0000 | [diff] [blame] | 1195 | if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue()))) |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 1196 | return TTI::TCC_Free; |
Juergen Ributzka | 5eef98c | 2014-03-25 18:01:23 +0000 | [diff] [blame] | 1197 | break; |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1198 | } |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 1199 | return X86TTIImpl::getIntImmCost(Imm, Ty); |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 1200 | } |
Elena Demikhovsky | f1de34b | 2014-12-04 09:40:44 +0000 | [diff] [blame] | 1201 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 1202 | bool X86TTIImpl::isLegalMaskedLoad(Type *DataTy, int Consecutive) { |
Elena Demikhovsky | fb81b93 | 2014-12-25 07:49:20 +0000 | [diff] [blame] | 1203 | int DataWidth = DataTy->getPrimitiveSizeInBits(); |
Elena Demikhovsky | f1de34b | 2014-12-04 09:40:44 +0000 | [diff] [blame] | 1204 | |
| 1205 | // Todo: AVX512 allows gather/scatter, works with strided and random as well |
Elena Demikhovsky | fb81b93 | 2014-12-25 07:49:20 +0000 | [diff] [blame] | 1206 | if ((DataWidth < 32) || (Consecutive == 0)) |
Elena Demikhovsky | f1de34b | 2014-12-04 09:40:44 +0000 | [diff] [blame] | 1207 | return false; |
| 1208 | if (ST->hasAVX512() || ST->hasAVX2()) |
| 1209 | return true; |
| 1210 | return false; |
| 1211 | } |
| 1212 | |
Chandler Carruth | 705b185 | 2015-01-31 03:43:40 +0000 | [diff] [blame^] | 1213 | bool X86TTIImpl::isLegalMaskedStore(Type *DataType, int Consecutive) { |
Elena Demikhovsky | 3fcafa2 | 2014-12-14 09:43:50 +0000 | [diff] [blame] | 1214 | return isLegalMaskedLoad(DataType, Consecutive); |
Elena Demikhovsky | f1de34b | 2014-12-04 09:40:44 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |