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 | |
| 17 | #define DEBUG_TYPE "x86tti" |
| 18 | #include "X86.h" |
| 19 | #include "X86TargetMachine.h" |
Chandler Carruth | d3e7355 | 2013-01-07 03:08:10 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/TargetTransformInfo.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 | |
| 27 | // Declare the pass initialization routine locally as target-specific passes |
| 28 | // don't havve a target-wide initialization entry point, and so we rely on the |
| 29 | // pass constructor initialization. |
| 30 | namespace llvm { |
| 31 | void initializeX86TTIPass(PassRegistry &); |
| 32 | } |
| 33 | |
| 34 | namespace { |
| 35 | |
Craig Topper | 77dfe45 | 2014-03-02 08:08:51 +0000 | [diff] [blame] | 36 | class X86TTI final : public ImmutablePass, public TargetTransformInfo { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 37 | const X86Subtarget *ST; |
| 38 | const X86TargetLowering *TLI; |
| 39 | |
| 40 | /// Estimate the overhead of scalarizing an instruction. Insert and Extract |
| 41 | /// are set if the result needs to be inserted and/or extracted from vectors. |
| 42 | unsigned getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const; |
| 43 | |
| 44 | public: |
Nadav Rotem | 02dd93e | 2013-06-27 17:54:10 +0000 | [diff] [blame] | 45 | X86TTI() : ImmutablePass(ID), ST(0), TLI(0) { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 46 | llvm_unreachable("This pass cannot be directly constructed"); |
| 47 | } |
| 48 | |
| 49 | X86TTI(const X86TargetMachine *TM) |
Juergen Ributzka | 3e752e7 | 2014-01-24 18:22:59 +0000 | [diff] [blame] | 50 | : ImmutablePass(ID), ST(TM->getSubtargetImpl()), |
| 51 | TLI(TM->getTargetLowering()) { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 52 | initializeX86TTIPass(*PassRegistry::getPassRegistry()); |
| 53 | } |
| 54 | |
Craig Topper | 24e685f | 2014-03-10 05:29:18 +0000 | [diff] [blame] | 55 | void initializePass() override { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 56 | pushTTIStack(this); |
| 57 | } |
| 58 | |
Craig Topper | 24e685f | 2014-03-10 05:29:18 +0000 | [diff] [blame] | 59 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 60 | TargetTransformInfo::getAnalysisUsage(AU); |
| 61 | } |
| 62 | |
| 63 | /// Pass identification. |
| 64 | static char ID; |
| 65 | |
| 66 | /// Provide necessary pointer adjustments for the two base classes. |
Craig Topper | 24e685f | 2014-03-10 05:29:18 +0000 | [diff] [blame] | 67 | void *getAdjustedAnalysisPointer(const void *ID) override { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 68 | if (ID == &TargetTransformInfo::ID) |
| 69 | return (TargetTransformInfo*)this; |
| 70 | return this; |
| 71 | } |
| 72 | |
| 73 | /// \name Scalar TTI Implementations |
| 74 | /// @{ |
Craig Topper | 24e685f | 2014-03-10 05:29:18 +0000 | [diff] [blame] | 75 | PopcntSupportKind getPopcntSupport(unsigned TyWidth) const override; |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 76 | |
| 77 | /// @} |
| 78 | |
| 79 | /// \name Vector TTI Implementations |
| 80 | /// @{ |
| 81 | |
Craig Topper | 24e685f | 2014-03-10 05:29:18 +0000 | [diff] [blame] | 82 | unsigned getNumberOfRegisters(bool Vector) const override; |
| 83 | unsigned getRegisterBitWidth(bool Vector) const override; |
| 84 | unsigned getMaximumUnrollFactor() const override; |
| 85 | unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty, OperandValueKind, |
| 86 | OperandValueKind) const override; |
| 87 | unsigned getShuffleCost(ShuffleKind Kind, Type *Tp, |
| 88 | int Index, Type *SubTp) const override; |
| 89 | unsigned getCastInstrCost(unsigned Opcode, Type *Dst, |
| 90 | Type *Src) const override; |
| 91 | unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy, |
| 92 | Type *CondTy) const override; |
| 93 | unsigned getVectorInstrCost(unsigned Opcode, Type *Val, |
| 94 | unsigned Index) const override; |
| 95 | unsigned getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment, |
| 96 | unsigned AddressSpace) const override; |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 97 | |
Craig Topper | 24e685f | 2014-03-10 05:29:18 +0000 | [diff] [blame] | 98 | unsigned getAddressComputationCost(Type *PtrTy, |
| 99 | bool IsComplex) const override; |
Arnold Schwaighofer | 6042a26 | 2013-07-12 19:16:07 +0000 | [diff] [blame] | 100 | |
Craig Topper | 24e685f | 2014-03-10 05:29:18 +0000 | [diff] [blame] | 101 | unsigned getReductionCost(unsigned Opcode, Type *Ty, |
| 102 | bool IsPairwiseForm) const override; |
Craig Topper | 7315602 | 2014-03-02 09:09:27 +0000 | [diff] [blame] | 103 | |
Craig Topper | 24e685f | 2014-03-10 05:29:18 +0000 | [diff] [blame] | 104 | unsigned getIntImmCost(const APInt &Imm, Type *Ty) const override; |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 105 | |
Craig Topper | 24e685f | 2014-03-10 05:29:18 +0000 | [diff] [blame] | 106 | unsigned getIntImmCost(unsigned Opcode, const APInt &Imm, |
| 107 | Type *Ty) const override; |
| 108 | unsigned getIntImmCost(Intrinsic::ID IID, const APInt &Imm, |
| 109 | Type *Ty) const override; |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 110 | |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 111 | /// @} |
| 112 | }; |
| 113 | |
| 114 | } // end anonymous namespace |
| 115 | |
| 116 | INITIALIZE_AG_PASS(X86TTI, TargetTransformInfo, "x86tti", |
| 117 | "X86 Target Transform Info", true, true, false) |
| 118 | char X86TTI::ID = 0; |
| 119 | |
| 120 | ImmutablePass * |
| 121 | llvm::createX86TargetTransformInfoPass(const X86TargetMachine *TM) { |
| 122 | return new X86TTI(TM); |
| 123 | } |
| 124 | |
| 125 | |
| 126 | //===----------------------------------------------------------------------===// |
| 127 | // |
| 128 | // X86 cost model. |
| 129 | // |
| 130 | //===----------------------------------------------------------------------===// |
| 131 | |
Chandler Carruth | 50a36cd | 2013-01-07 03:16:03 +0000 | [diff] [blame] | 132 | X86TTI::PopcntSupportKind X86TTI::getPopcntSupport(unsigned TyWidth) const { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 133 | assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2"); |
| 134 | // TODO: Currently the __builtin_popcount() implementation using SSE3 |
| 135 | // instructions is inefficient. Once the problem is fixed, we should |
Craig Topper | 0a63e1d | 2013-09-08 00:47:31 +0000 | [diff] [blame] | 136 | // call ST->hasSSE3() instead of ST->hasPOPCNT(). |
| 137 | return ST->hasPOPCNT() ? PSK_FastHardware : PSK_Software; |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | unsigned X86TTI::getNumberOfRegisters(bool Vector) const { |
Nadav Rotem | b1791a7 | 2013-01-09 22:29:00 +0000 | [diff] [blame] | 141 | if (Vector && !ST->hasSSE1()) |
| 142 | return 0; |
| 143 | |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 144 | if (ST->is64Bit()) |
| 145 | return 16; |
| 146 | return 8; |
| 147 | } |
| 148 | |
Nadav Rotem | b1791a7 | 2013-01-09 22:29:00 +0000 | [diff] [blame] | 149 | unsigned X86TTI::getRegisterBitWidth(bool Vector) const { |
| 150 | if (Vector) { |
| 151 | if (ST->hasAVX()) return 256; |
| 152 | if (ST->hasSSE1()) return 128; |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | if (ST->is64Bit()) |
| 157 | return 64; |
| 158 | return 32; |
| 159 | |
| 160 | } |
| 161 | |
Nadav Rotem | b696c36 | 2013-01-09 01:15:42 +0000 | [diff] [blame] | 162 | unsigned X86TTI::getMaximumUnrollFactor() const { |
| 163 | if (ST->isAtom()) |
| 164 | return 1; |
| 165 | |
| 166 | // Sandybridge and Haswell have multiple execution ports and pipelined |
| 167 | // vector units. |
| 168 | if (ST->hasAVX()) |
| 169 | return 4; |
| 170 | |
| 171 | return 2; |
| 172 | } |
| 173 | |
Arnold Schwaighofer | b977387 | 2013-04-04 23:26:21 +0000 | [diff] [blame] | 174 | unsigned X86TTI::getArithmeticInstrCost(unsigned Opcode, Type *Ty, |
| 175 | OperandValueKind Op1Info, |
| 176 | OperandValueKind Op2Info) const { |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 177 | // Legalize the type. |
| 178 | std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty); |
| 179 | |
| 180 | int ISD = TLI->InstructionOpcodeToISD(Opcode); |
| 181 | assert(ISD && "Invalid opcode"); |
| 182 | |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 183 | static const CostTblEntry<MVT::SimpleValueType> AVX2CostTable[] = { |
Michael Liao | 70dd7f9 | 2013-03-20 22:01:10 +0000 | [diff] [blame] | 184 | // Shifts on v4i64/v8i32 on AVX2 is legal even though we declare to |
| 185 | // customize them to detect the cases where shift amount is a scalar one. |
| 186 | { ISD::SHL, MVT::v4i32, 1 }, |
| 187 | { ISD::SRL, MVT::v4i32, 1 }, |
| 188 | { ISD::SRA, MVT::v4i32, 1 }, |
| 189 | { ISD::SHL, MVT::v8i32, 1 }, |
| 190 | { ISD::SRL, MVT::v8i32, 1 }, |
| 191 | { ISD::SRA, MVT::v8i32, 1 }, |
| 192 | { ISD::SHL, MVT::v2i64, 1 }, |
| 193 | { ISD::SRL, MVT::v2i64, 1 }, |
| 194 | { ISD::SHL, MVT::v4i64, 1 }, |
| 195 | { ISD::SRL, MVT::v4i64, 1 }, |
Arnold Schwaighofer | e9b5016 | 2013-04-03 21:46:05 +0000 | [diff] [blame] | 196 | |
| 197 | { ISD::SHL, MVT::v32i8, 42 }, // cmpeqb sequence. |
| 198 | { ISD::SHL, MVT::v16i16, 16*10 }, // Scalarized. |
| 199 | |
| 200 | { ISD::SRL, MVT::v32i8, 32*10 }, // Scalarized. |
| 201 | { ISD::SRL, MVT::v16i16, 8*10 }, // Scalarized. |
| 202 | |
| 203 | { ISD::SRA, MVT::v32i8, 32*10 }, // Scalarized. |
| 204 | { ISD::SRA, MVT::v16i16, 16*10 }, // Scalarized. |
| 205 | { ISD::SRA, MVT::v4i64, 4*10 }, // Scalarized. |
Arnold Schwaighofer | a04b9ef | 2013-06-25 19:14:09 +0000 | [diff] [blame] | 206 | |
| 207 | // Vectorizing division is a bad idea. See the SSE2 table for more comments. |
| 208 | { ISD::SDIV, MVT::v32i8, 32*20 }, |
| 209 | { ISD::SDIV, MVT::v16i16, 16*20 }, |
| 210 | { ISD::SDIV, MVT::v8i32, 8*20 }, |
| 211 | { ISD::SDIV, MVT::v4i64, 4*20 }, |
| 212 | { ISD::UDIV, MVT::v32i8, 32*20 }, |
| 213 | { ISD::UDIV, MVT::v16i16, 16*20 }, |
| 214 | { ISD::UDIV, MVT::v8i32, 8*20 }, |
| 215 | { ISD::UDIV, MVT::v4i64, 4*20 }, |
Michael Liao | 70dd7f9 | 2013-03-20 22:01:10 +0000 | [diff] [blame] | 216 | }; |
| 217 | |
| 218 | // Look for AVX2 lowering tricks. |
| 219 | if (ST->hasAVX2()) { |
Andrea Di Biagio | b7882b3 | 2014-02-12 23:43:47 +0000 | [diff] [blame] | 220 | if (ISD == ISD::SHL && LT.second == MVT::v16i16 && |
| 221 | (Op2Info == TargetTransformInfo::OK_UniformConstantValue || |
| 222 | Op2Info == TargetTransformInfo::OK_NonUniformConstantValue)) |
| 223 | // On AVX2, a packed v16i16 shift left by a constant build_vector |
| 224 | // is lowered into a vector multiply (vpmullw). |
| 225 | return LT.first; |
| 226 | |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 227 | int Idx = CostTableLookup(AVX2CostTable, ISD, LT.second); |
Michael Liao | 70dd7f9 | 2013-03-20 22:01:10 +0000 | [diff] [blame] | 228 | if (Idx != -1) |
| 229 | return LT.first * AVX2CostTable[Idx].Cost; |
| 230 | } |
| 231 | |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 232 | static const CostTblEntry<MVT::SimpleValueType> |
| 233 | SSE2UniformConstCostTable[] = { |
Arnold Schwaighofer | 44f902e | 2013-04-04 23:26:24 +0000 | [diff] [blame] | 234 | // We don't correctly identify costs of casts because they are marked as |
| 235 | // custom. |
| 236 | // Constant splats are cheaper for the following instructions. |
| 237 | { ISD::SHL, MVT::v16i8, 1 }, // psllw. |
| 238 | { ISD::SHL, MVT::v8i16, 1 }, // psllw. |
| 239 | { ISD::SHL, MVT::v4i32, 1 }, // pslld |
| 240 | { ISD::SHL, MVT::v2i64, 1 }, // psllq. |
| 241 | |
| 242 | { ISD::SRL, MVT::v16i8, 1 }, // psrlw. |
| 243 | { ISD::SRL, MVT::v8i16, 1 }, // psrlw. |
| 244 | { ISD::SRL, MVT::v4i32, 1 }, // psrld. |
| 245 | { ISD::SRL, MVT::v2i64, 1 }, // psrlq. |
| 246 | |
| 247 | { ISD::SRA, MVT::v16i8, 4 }, // psrlw, pand, pxor, psubb. |
| 248 | { ISD::SRA, MVT::v8i16, 1 }, // psraw. |
| 249 | { ISD::SRA, MVT::v4i32, 1 }, // psrad. |
| 250 | }; |
| 251 | |
| 252 | if (Op2Info == TargetTransformInfo::OK_UniformConstantValue && |
| 253 | ST->hasSSE2()) { |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 254 | int Idx = CostTableLookup(SSE2UniformConstCostTable, ISD, LT.second); |
Arnold Schwaighofer | 44f902e | 2013-04-04 23:26:24 +0000 | [diff] [blame] | 255 | if (Idx != -1) |
| 256 | return LT.first * SSE2UniformConstCostTable[Idx].Cost; |
| 257 | } |
| 258 | |
Andrea Di Biagio | b7882b3 | 2014-02-12 23:43:47 +0000 | [diff] [blame] | 259 | if (ISD == ISD::SHL && |
| 260 | Op2Info == TargetTransformInfo::OK_NonUniformConstantValue) { |
| 261 | EVT VT = LT.second; |
| 262 | if ((VT == MVT::v8i16 && ST->hasSSE2()) || |
| 263 | (VT == MVT::v4i32 && ST->hasSSE41())) |
| 264 | // Vector shift left by non uniform constant can be lowered |
| 265 | // into vector multiply (pmullw/pmulld). |
| 266 | return LT.first; |
| 267 | if (VT == MVT::v4i32 && ST->hasSSE2()) |
| 268 | // A vector shift left by non uniform constant is converted |
| 269 | // into a vector multiply; the new multiply is eventually |
| 270 | // lowered into a sequence of shuffles and 2 x pmuludq. |
| 271 | ISD = ISD::MUL; |
| 272 | } |
Arnold Schwaighofer | 44f902e | 2013-04-04 23:26:24 +0000 | [diff] [blame] | 273 | |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 274 | static const CostTblEntry<MVT::SimpleValueType> SSE2CostTable[] = { |
Arnold Schwaighofer | e9b5016 | 2013-04-03 21:46:05 +0000 | [diff] [blame] | 275 | // We don't correctly identify costs of casts because they are marked as |
| 276 | // custom. |
| 277 | // For some cases, where the shift amount is a scalar we would be able |
| 278 | // to generate better code. Unfortunately, when this is the case the value |
| 279 | // (the splat) will get hoisted out of the loop, thereby making it invisible |
| 280 | // to ISel. The cost model must return worst case assumptions because it is |
| 281 | // used for vectorization and we don't want to make vectorized code worse |
| 282 | // than scalar code. |
| 283 | { ISD::SHL, MVT::v16i8, 30 }, // cmpeqb sequence. |
| 284 | { ISD::SHL, MVT::v8i16, 8*10 }, // Scalarized. |
| 285 | { ISD::SHL, MVT::v4i32, 2*5 }, // We optimized this using mul. |
| 286 | { ISD::SHL, MVT::v2i64, 2*10 }, // Scalarized. |
Andrea Di Biagio | b7882b3 | 2014-02-12 23:43:47 +0000 | [diff] [blame] | 287 | { ISD::SHL, MVT::v4i64, 4*10 }, // Scalarized. |
Arnold Schwaighofer | e9b5016 | 2013-04-03 21:46:05 +0000 | [diff] [blame] | 288 | |
| 289 | { ISD::SRL, MVT::v16i8, 16*10 }, // Scalarized. |
| 290 | { ISD::SRL, MVT::v8i16, 8*10 }, // Scalarized. |
| 291 | { ISD::SRL, MVT::v4i32, 4*10 }, // Scalarized. |
| 292 | { ISD::SRL, MVT::v2i64, 2*10 }, // Scalarized. |
| 293 | |
| 294 | { ISD::SRA, MVT::v16i8, 16*10 }, // Scalarized. |
| 295 | { ISD::SRA, MVT::v8i16, 8*10 }, // Scalarized. |
| 296 | { ISD::SRA, MVT::v4i32, 4*10 }, // Scalarized. |
| 297 | { ISD::SRA, MVT::v2i64, 2*10 }, // Scalarized. |
Arnold Schwaighofer | a04b9ef | 2013-06-25 19:14:09 +0000 | [diff] [blame] | 298 | |
| 299 | // It is not a good idea to vectorize division. We have to scalarize it and |
| 300 | // in the process we will often end up having to spilling regular |
| 301 | // registers. The overhead of division is going to dominate most kernels |
| 302 | // anyways so try hard to prevent vectorization of division - it is |
| 303 | // generally a bad idea. Assume somewhat arbitrarily that we have to be able |
| 304 | // to hide "20 cycles" for each lane. |
| 305 | { ISD::SDIV, MVT::v16i8, 16*20 }, |
| 306 | { ISD::SDIV, MVT::v8i16, 8*20 }, |
| 307 | { ISD::SDIV, MVT::v4i32, 4*20 }, |
| 308 | { ISD::SDIV, MVT::v2i64, 2*20 }, |
| 309 | { ISD::UDIV, MVT::v16i8, 16*20 }, |
| 310 | { ISD::UDIV, MVT::v8i16, 8*20 }, |
| 311 | { ISD::UDIV, MVT::v4i32, 4*20 }, |
| 312 | { ISD::UDIV, MVT::v2i64, 2*20 }, |
Arnold Schwaighofer | e9b5016 | 2013-04-03 21:46:05 +0000 | [diff] [blame] | 313 | }; |
| 314 | |
| 315 | if (ST->hasSSE2()) { |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 316 | int Idx = CostTableLookup(SSE2CostTable, ISD, LT.second); |
Arnold Schwaighofer | e9b5016 | 2013-04-03 21:46:05 +0000 | [diff] [blame] | 317 | if (Idx != -1) |
| 318 | return LT.first * SSE2CostTable[Idx].Cost; |
| 319 | } |
| 320 | |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 321 | static const CostTblEntry<MVT::SimpleValueType> AVX1CostTable[] = { |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 322 | // We don't have to scalarize unsupported ops. We can issue two half-sized |
| 323 | // operations and we only need to extract the upper YMM half. |
| 324 | // Two ops + 1 extract + 1 insert = 4. |
Andrea Di Biagio | b7882b3 | 2014-02-12 23:43:47 +0000 | [diff] [blame] | 325 | { ISD::MUL, MVT::v16i16, 4 }, |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 326 | { ISD::MUL, MVT::v8i32, 4 }, |
| 327 | { ISD::SUB, MVT::v8i32, 4 }, |
| 328 | { ISD::ADD, MVT::v8i32, 4 }, |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 329 | { ISD::SUB, MVT::v4i64, 4 }, |
| 330 | { ISD::ADD, MVT::v4i64, 4 }, |
Arnold Schwaighofer | 20ef54f | 2013-03-02 04:02:52 +0000 | [diff] [blame] | 331 | // A v4i64 multiply is custom lowered as two split v2i64 vectors that then |
| 332 | // are lowered as a series of long multiplies(3), shifts(4) and adds(2) |
| 333 | // Because we believe v4i64 to be a legal type, we must also include the |
| 334 | // split factor of two in the cost table. Therefore, the cost here is 18 |
| 335 | // instead of 9. |
| 336 | { ISD::MUL, MVT::v4i64, 18 }, |
| 337 | }; |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 338 | |
| 339 | // Look for AVX1 lowering tricks. |
Arnold Schwaighofer | 20ef54f | 2013-03-02 04:02:52 +0000 | [diff] [blame] | 340 | if (ST->hasAVX() && !ST->hasAVX2()) { |
Andrea Di Biagio | b7882b3 | 2014-02-12 23:43:47 +0000 | [diff] [blame] | 341 | EVT VT = LT.second; |
| 342 | |
| 343 | // v16i16 and v8i32 shifts by non-uniform constants are lowered into a |
| 344 | // sequence of extract + two vector multiply + insert. |
| 345 | if (ISD == ISD::SHL && (VT == MVT::v8i32 || VT == MVT::v16i16) && |
| 346 | Op2Info == TargetTransformInfo::OK_NonUniformConstantValue) |
| 347 | ISD = ISD::MUL; |
| 348 | |
| 349 | int Idx = CostTableLookup(AVX1CostTable, ISD, VT); |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 350 | if (Idx != -1) |
| 351 | return LT.first * AVX1CostTable[Idx].Cost; |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 352 | } |
Arnold Schwaighofer | 20ef54f | 2013-03-02 04:02:52 +0000 | [diff] [blame] | 353 | |
| 354 | // Custom lowering of vectors. |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 355 | static const CostTblEntry<MVT::SimpleValueType> CustomLowered[] = { |
Arnold Schwaighofer | 20ef54f | 2013-03-02 04:02:52 +0000 | [diff] [blame] | 356 | // A v2i64/v4i64 and multiply is custom lowered as a series of long |
| 357 | // multiplies(3), shifts(4) and adds(2). |
| 358 | { ISD::MUL, MVT::v2i64, 9 }, |
| 359 | { ISD::MUL, MVT::v4i64, 9 }, |
| 360 | }; |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 361 | int Idx = CostTableLookup(CustomLowered, ISD, LT.second); |
Arnold Schwaighofer | 20ef54f | 2013-03-02 04:02:52 +0000 | [diff] [blame] | 362 | if (Idx != -1) |
| 363 | return LT.first * CustomLowered[Idx].Cost; |
| 364 | |
| 365 | // Special lowering of v4i32 mul on sse2, sse3: Lower v4i32 mul as 2x shuffle, |
| 366 | // 2x pmuludq, 2x shuffle. |
| 367 | if (ISD == ISD::MUL && LT.second == MVT::v4i32 && ST->hasSSE2() && |
| 368 | !ST->hasSSE41()) |
Andrea Di Biagio | b7882b3 | 2014-02-12 23:43:47 +0000 | [diff] [blame] | 369 | return LT.first * 6; |
Arnold Schwaighofer | 20ef54f | 2013-03-02 04:02:52 +0000 | [diff] [blame] | 370 | |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 371 | // Fallback to the default implementation. |
Arnold Schwaighofer | b977387 | 2013-04-04 23:26:21 +0000 | [diff] [blame] | 372 | return TargetTransformInfo::getArithmeticInstrCost(Opcode, Ty, Op1Info, |
| 373 | Op2Info); |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | unsigned X86TTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index, |
| 377 | Type *SubTp) const { |
| 378 | // We only estimate the cost of reverse shuffles. |
Chandler Carruth | 2109f47 | 2013-01-07 03:20:02 +0000 | [diff] [blame] | 379 | if (Kind != SK_Reverse) |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 380 | return TargetTransformInfo::getShuffleCost(Kind, Tp, Index, SubTp); |
| 381 | |
| 382 | std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Tp); |
| 383 | unsigned Cost = 1; |
| 384 | if (LT.second.getSizeInBits() > 128) |
| 385 | Cost = 3; // Extract + insert + copy. |
| 386 | |
| 387 | // Multiple by the number of parts. |
| 388 | return Cost * LT.first; |
| 389 | } |
| 390 | |
| 391 | unsigned X86TTI::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) const { |
| 392 | int ISD = TLI->InstructionOpcodeToISD(Opcode); |
| 393 | assert(ISD && "Invalid opcode"); |
| 394 | |
Arnold Schwaighofer | f47d2d7 | 2013-04-08 18:05:48 +0000 | [diff] [blame] | 395 | std::pair<unsigned, MVT> LTSrc = TLI->getTypeLegalizationCost(Src); |
| 396 | std::pair<unsigned, MVT> LTDest = TLI->getTypeLegalizationCost(Dst); |
| 397 | |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 398 | static const TypeConversionCostTblEntry<MVT::SimpleValueType> |
| 399 | SSE2ConvTbl[] = { |
Arnold Schwaighofer | f47d2d7 | 2013-04-08 18:05:48 +0000 | [diff] [blame] | 400 | // These are somewhat magic numbers justified by looking at the output of |
| 401 | // Intel's IACA, running some kernels and making sure when we take |
| 402 | // legalization into account the throughput will be overestimated. |
| 403 | { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 }, |
| 404 | { ISD::UINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 }, |
| 405 | { ISD::UINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 }, |
| 406 | { ISD::UINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 }, |
| 407 | { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 }, |
| 408 | { ISD::SINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 }, |
| 409 | { ISD::SINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 }, |
| 410 | { ISD::SINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 }, |
| 411 | // There are faster sequences for float conversions. |
| 412 | { ISD::UINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 }, |
| 413 | { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 15 }, |
| 414 | { ISD::UINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 }, |
| 415 | { ISD::UINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 }, |
| 416 | { ISD::SINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 }, |
| 417 | { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 15 }, |
| 418 | { ISD::SINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 }, |
| 419 | { ISD::SINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 }, |
| 420 | }; |
| 421 | |
| 422 | if (ST->hasSSE2() && !ST->hasAVX()) { |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 423 | int Idx = |
| 424 | ConvertCostTableLookup(SSE2ConvTbl, ISD, LTDest.second, LTSrc.second); |
Arnold Schwaighofer | f47d2d7 | 2013-04-08 18:05:48 +0000 | [diff] [blame] | 425 | if (Idx != -1) |
| 426 | return LTSrc.first * SSE2ConvTbl[Idx].Cost; |
| 427 | } |
| 428 | |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 429 | EVT SrcTy = TLI->getValueType(Src); |
| 430 | EVT DstTy = TLI->getValueType(Dst); |
| 431 | |
Arnold Schwaighofer | c0c7ff4 | 2013-04-17 20:04:53 +0000 | [diff] [blame] | 432 | // The function getSimpleVT only handles simple value types. |
| 433 | if (!SrcTy.isSimple() || !DstTy.isSimple()) |
| 434 | return TargetTransformInfo::getCastInstrCost(Opcode, Dst, Src); |
| 435 | |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 436 | static const TypeConversionCostTblEntry<MVT::SimpleValueType> |
Tim Northover | f0e2161 | 2014-02-06 18:18:36 +0000 | [diff] [blame] | 437 | AVX2ConversionTbl[] = { |
| 438 | { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 1 }, |
| 439 | { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 1 }, |
| 440 | { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 3 }, |
| 441 | { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 3 }, |
| 442 | { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 3 }, |
| 443 | { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 3 }, |
| 444 | { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 1 }, |
| 445 | { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 1 }, |
| 446 | { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i1, 3 }, |
| 447 | { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i1, 3 }, |
| 448 | { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 3 }, |
| 449 | { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 3 }, |
| 450 | { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 3 }, |
| 451 | { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 }, |
| 452 | { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 1 }, |
| 453 | { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 1 }, |
| 454 | |
| 455 | { ISD::TRUNCATE, MVT::v4i8, MVT::v4i64, 2 }, |
| 456 | { ISD::TRUNCATE, MVT::v4i16, MVT::v4i64, 2 }, |
| 457 | { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 2 }, |
| 458 | { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 2 }, |
| 459 | { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 2 }, |
| 460 | { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 4 }, |
| 461 | }; |
| 462 | |
| 463 | static const TypeConversionCostTblEntry<MVT::SimpleValueType> |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 464 | AVXConversionTbl[] = { |
Tim Northover | f0e2161 | 2014-02-06 18:18:36 +0000 | [diff] [blame] | 465 | { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 4 }, |
| 466 | { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 4 }, |
| 467 | { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 7 }, |
| 468 | { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 4 }, |
| 469 | { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 7 }, |
| 470 | { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 4 }, |
| 471 | { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 4 }, |
| 472 | { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 4 }, |
| 473 | { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i1, 6 }, |
| 474 | { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i1, 4 }, |
| 475 | { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 6 }, |
| 476 | { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 4 }, |
| 477 | { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 6 }, |
| 478 | { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 }, |
| 479 | { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 4 }, |
| 480 | { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 4 }, |
| 481 | |
| 482 | { ISD::TRUNCATE, MVT::v4i8, MVT::v4i64, 4 }, |
| 483 | { ISD::TRUNCATE, MVT::v4i16, MVT::v4i64, 4 }, |
| 484 | { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 4 }, |
| 485 | { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 4 }, |
| 486 | { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 5 }, |
| 487 | { ISD::TRUNCATE, MVT::v16i8, MVT::v16i16, 4 }, |
| 488 | { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 9 }, |
Benjamin Kramer | 52ceb44 | 2013-04-01 10:23:49 +0000 | [diff] [blame] | 489 | |
| 490 | { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i1, 8 }, |
| 491 | { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i8, 8 }, |
| 492 | { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i16, 5 }, |
| 493 | { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i32, 1 }, |
| 494 | { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i1, 3 }, |
| 495 | { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i8, 3 }, |
| 496 | { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i16, 3 }, |
| 497 | { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 }, |
| 498 | { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i1, 3 }, |
| 499 | { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i8, 3 }, |
| 500 | { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i16, 3 }, |
| 501 | { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i32, 1 }, |
| 502 | |
| 503 | { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i1, 6 }, |
| 504 | { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i8, 5 }, |
| 505 | { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i16, 5 }, |
| 506 | { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 9 }, |
| 507 | { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i1, 7 }, |
| 508 | { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i8, 2 }, |
| 509 | { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i16, 2 }, |
| 510 | { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 6 }, |
| 511 | { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i1, 7 }, |
| 512 | { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i8, 2 }, |
| 513 | { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i16, 2 }, |
| 514 | { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i32, 6 }, |
| 515 | |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 516 | { ISD::FP_TO_SINT, MVT::v8i8, MVT::v8f32, 1 }, |
| 517 | { ISD::FP_TO_SINT, MVT::v4i8, MVT::v4f32, 1 }, |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 518 | }; |
| 519 | |
Tim Northover | f0e2161 | 2014-02-06 18:18:36 +0000 | [diff] [blame] | 520 | if (ST->hasAVX2()) { |
| 521 | int Idx = ConvertCostTableLookup(AVX2ConversionTbl, ISD, |
| 522 | DstTy.getSimpleVT(), SrcTy.getSimpleVT()); |
| 523 | if (Idx != -1) |
| 524 | return AVX2ConversionTbl[Idx].Cost; |
| 525 | } |
| 526 | |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 527 | if (ST->hasAVX()) { |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 528 | int Idx = ConvertCostTableLookup(AVXConversionTbl, ISD, DstTy.getSimpleVT(), |
| 529 | SrcTy.getSimpleVT()); |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 530 | if (Idx != -1) |
| 531 | return AVXConversionTbl[Idx].Cost; |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | return TargetTransformInfo::getCastInstrCost(Opcode, Dst, Src); |
| 535 | } |
| 536 | |
| 537 | unsigned X86TTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, |
| 538 | Type *CondTy) const { |
| 539 | // Legalize the type. |
| 540 | std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy); |
| 541 | |
| 542 | MVT MTy = LT.second; |
| 543 | |
| 544 | int ISD = TLI->InstructionOpcodeToISD(Opcode); |
| 545 | assert(ISD && "Invalid opcode"); |
| 546 | |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 547 | static const CostTblEntry<MVT::SimpleValueType> SSE42CostTbl[] = { |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 548 | { ISD::SETCC, MVT::v2f64, 1 }, |
| 549 | { ISD::SETCC, MVT::v4f32, 1 }, |
| 550 | { ISD::SETCC, MVT::v2i64, 1 }, |
| 551 | { ISD::SETCC, MVT::v4i32, 1 }, |
| 552 | { ISD::SETCC, MVT::v8i16, 1 }, |
| 553 | { ISD::SETCC, MVT::v16i8, 1 }, |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 554 | }; |
| 555 | |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 556 | static const CostTblEntry<MVT::SimpleValueType> AVX1CostTbl[] = { |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 557 | { ISD::SETCC, MVT::v4f64, 1 }, |
| 558 | { ISD::SETCC, MVT::v8f32, 1 }, |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 559 | // AVX1 does not support 8-wide integer compare. |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 560 | { ISD::SETCC, MVT::v4i64, 4 }, |
| 561 | { ISD::SETCC, MVT::v8i32, 4 }, |
| 562 | { ISD::SETCC, MVT::v16i16, 4 }, |
| 563 | { ISD::SETCC, MVT::v32i8, 4 }, |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 564 | }; |
| 565 | |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 566 | static const CostTblEntry<MVT::SimpleValueType> AVX2CostTbl[] = { |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 567 | { ISD::SETCC, MVT::v4i64, 1 }, |
| 568 | { ISD::SETCC, MVT::v8i32, 1 }, |
| 569 | { ISD::SETCC, MVT::v16i16, 1 }, |
| 570 | { ISD::SETCC, MVT::v32i8, 1 }, |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 571 | }; |
| 572 | |
| 573 | if (ST->hasAVX2()) { |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 574 | int Idx = CostTableLookup(AVX2CostTbl, ISD, MTy); |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 575 | if (Idx != -1) |
| 576 | return LT.first * AVX2CostTbl[Idx].Cost; |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | if (ST->hasAVX()) { |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 580 | int Idx = CostTableLookup(AVX1CostTbl, ISD, MTy); |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 581 | if (Idx != -1) |
| 582 | return LT.first * AVX1CostTbl[Idx].Cost; |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | if (ST->hasSSE42()) { |
Benjamin Kramer | 21585fd | 2013-08-09 19:33:32 +0000 | [diff] [blame] | 586 | int Idx = CostTableLookup(SSE42CostTbl, ISD, MTy); |
Renato Golin | e1fb059 | 2013-01-20 20:57:20 +0000 | [diff] [blame] | 587 | if (Idx != -1) |
| 588 | return LT.first * SSE42CostTbl[Idx].Cost; |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | return TargetTransformInfo::getCmpSelInstrCost(Opcode, ValTy, CondTy); |
| 592 | } |
| 593 | |
| 594 | unsigned X86TTI::getVectorInstrCost(unsigned Opcode, Type *Val, |
| 595 | unsigned Index) const { |
| 596 | assert(Val->isVectorTy() && "This must be a vector type"); |
| 597 | |
| 598 | if (Index != -1U) { |
| 599 | // Legalize the type. |
| 600 | std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Val); |
| 601 | |
| 602 | // This type is legalized to a scalar type. |
| 603 | if (!LT.second.isVector()) |
| 604 | return 0; |
| 605 | |
| 606 | // The type may be split. Normalize the index to the new type. |
| 607 | unsigned Width = LT.second.getVectorNumElements(); |
| 608 | Index = Index % Width; |
| 609 | |
| 610 | // Floating point scalars are already located in index #0. |
| 611 | if (Val->getScalarType()->isFloatingPointTy() && Index == 0) |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | return TargetTransformInfo::getVectorInstrCost(Opcode, Val, Index); |
| 616 | } |
| 617 | |
Nadav Rotem | f9ecbcb | 2013-06-27 17:52:04 +0000 | [diff] [blame] | 618 | unsigned X86TTI::getScalarizationOverhead(Type *Ty, bool Insert, |
| 619 | bool Extract) const { |
| 620 | assert (Ty->isVectorTy() && "Can only scalarize vectors"); |
| 621 | unsigned Cost = 0; |
| 622 | |
| 623 | for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) { |
| 624 | if (Insert) |
| 625 | Cost += TopTTI->getVectorInstrCost(Instruction::InsertElement, Ty, i); |
| 626 | if (Extract) |
| 627 | Cost += TopTTI->getVectorInstrCost(Instruction::ExtractElement, Ty, i); |
| 628 | } |
| 629 | |
| 630 | return Cost; |
| 631 | } |
| 632 | |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 633 | unsigned X86TTI::getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment, |
| 634 | unsigned AddressSpace) const { |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 635 | // Handle non-power-of-two vectors such as <3 x float> |
Nadav Rotem | f9ecbcb | 2013-06-27 17:52:04 +0000 | [diff] [blame] | 636 | if (VectorType *VTy = dyn_cast<VectorType>(Src)) { |
| 637 | unsigned NumElem = VTy->getVectorNumElements(); |
| 638 | |
| 639 | // Handle a few common cases: |
| 640 | // <3 x float> |
| 641 | if (NumElem == 3 && VTy->getScalarSizeInBits() == 32) |
| 642 | // Cost = 64 bit store + extract + 32 bit store. |
| 643 | return 3; |
| 644 | |
| 645 | // <3 x double> |
| 646 | if (NumElem == 3 && VTy->getScalarSizeInBits() == 64) |
| 647 | // Cost = 128 bit store + unpack + 64 bit store. |
| 648 | return 3; |
| 649 | |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 650 | // Assume that all other non-power-of-two numbers are scalarized. |
Nadav Rotem | f9ecbcb | 2013-06-27 17:52:04 +0000 | [diff] [blame] | 651 | if (!isPowerOf2_32(NumElem)) { |
| 652 | unsigned Cost = TargetTransformInfo::getMemoryOpCost(Opcode, |
| 653 | VTy->getScalarType(), |
| 654 | Alignment, |
| 655 | AddressSpace); |
| 656 | unsigned SplitCost = getScalarizationOverhead(Src, |
| 657 | Opcode == Instruction::Load, |
| 658 | Opcode==Instruction::Store); |
| 659 | return NumElem * Cost + SplitCost; |
| 660 | } |
| 661 | } |
| 662 | |
Chandler Carruth | 664e354 | 2013-01-07 01:37:14 +0000 | [diff] [blame] | 663 | // Legalize the type. |
| 664 | std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Src); |
| 665 | assert((Opcode == Instruction::Load || Opcode == Instruction::Store) && |
| 666 | "Invalid Opcode"); |
| 667 | |
| 668 | // Each load/store unit costs 1. |
| 669 | unsigned Cost = LT.first * 1; |
| 670 | |
| 671 | // On Sandybridge 256bit load/stores are double pumped |
| 672 | // (but not on Haswell). |
| 673 | if (LT.second.getSizeInBits() > 128 && !ST->hasAVX2()) |
| 674 | Cost*=2; |
| 675 | |
| 676 | return Cost; |
| 677 | } |
Arnold Schwaighofer | 6042a26 | 2013-07-12 19:16:07 +0000 | [diff] [blame] | 678 | |
| 679 | unsigned X86TTI::getAddressComputationCost(Type *Ty, bool IsComplex) const { |
| 680 | // Address computations in vectorized code with non-consecutive addresses will |
| 681 | // likely result in more instructions compared to scalar code where the |
| 682 | // computation can more often be merged into the index mode. The resulting |
| 683 | // extra micro-ops can significantly decrease throughput. |
| 684 | unsigned NumVectorInstToHideOverhead = 10; |
| 685 | |
| 686 | if (Ty->isVectorTy() && IsComplex) |
| 687 | return NumVectorInstToHideOverhead; |
| 688 | |
| 689 | return TargetTransformInfo::getAddressComputationCost(Ty, IsComplex); |
| 690 | } |
Yi Jiang | 5c343de | 2013-09-19 17:48:48 +0000 | [diff] [blame] | 691 | |
| 692 | unsigned X86TTI::getReductionCost(unsigned Opcode, Type *ValTy, |
| 693 | bool IsPairwise) const { |
| 694 | |
| 695 | std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy); |
| 696 | |
| 697 | MVT MTy = LT.second; |
| 698 | |
| 699 | int ISD = TLI->InstructionOpcodeToISD(Opcode); |
| 700 | assert(ISD && "Invalid opcode"); |
| 701 | |
| 702 | // We use the Intel Architecture Code Analyzer(IACA) to measure the throughput |
| 703 | // and make it as the cost. |
| 704 | |
| 705 | static const CostTblEntry<MVT::SimpleValueType> SSE42CostTblPairWise[] = { |
| 706 | { ISD::FADD, MVT::v2f64, 2 }, |
| 707 | { ISD::FADD, MVT::v4f32, 4 }, |
| 708 | { ISD::ADD, MVT::v2i64, 2 }, // The data reported by the IACA tool is "1.6". |
| 709 | { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.5". |
| 710 | { ISD::ADD, MVT::v8i16, 5 }, |
| 711 | }; |
| 712 | |
| 713 | static const CostTblEntry<MVT::SimpleValueType> AVX1CostTblPairWise[] = { |
| 714 | { ISD::FADD, MVT::v4f32, 4 }, |
| 715 | { ISD::FADD, MVT::v4f64, 5 }, |
| 716 | { ISD::FADD, MVT::v8f32, 7 }, |
| 717 | { ISD::ADD, MVT::v2i64, 1 }, // The data reported by the IACA tool is "1.5". |
| 718 | { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.5". |
| 719 | { ISD::ADD, MVT::v4i64, 5 }, // The data reported by the IACA tool is "4.8". |
| 720 | { ISD::ADD, MVT::v8i16, 5 }, |
| 721 | { ISD::ADD, MVT::v8i32, 5 }, |
| 722 | }; |
| 723 | |
| 724 | static const CostTblEntry<MVT::SimpleValueType> SSE42CostTblNoPairWise[] = { |
| 725 | { ISD::FADD, MVT::v2f64, 2 }, |
| 726 | { ISD::FADD, MVT::v4f32, 4 }, |
| 727 | { ISD::ADD, MVT::v2i64, 2 }, // The data reported by the IACA tool is "1.6". |
| 728 | { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.3". |
| 729 | { ISD::ADD, MVT::v8i16, 4 }, // The data reported by the IACA tool is "4.3". |
| 730 | }; |
| 731 | |
| 732 | static const CostTblEntry<MVT::SimpleValueType> AVX1CostTblNoPairWise[] = { |
| 733 | { ISD::FADD, MVT::v4f32, 3 }, |
| 734 | { ISD::FADD, MVT::v4f64, 3 }, |
| 735 | { ISD::FADD, MVT::v8f32, 4 }, |
| 736 | { ISD::ADD, MVT::v2i64, 1 }, // The data reported by the IACA tool is "1.5". |
| 737 | { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "2.8". |
| 738 | { ISD::ADD, MVT::v4i64, 3 }, |
| 739 | { ISD::ADD, MVT::v8i16, 4 }, |
| 740 | { ISD::ADD, MVT::v8i32, 5 }, |
| 741 | }; |
| 742 | |
| 743 | if (IsPairwise) { |
| 744 | if (ST->hasAVX()) { |
| 745 | int Idx = CostTableLookup(AVX1CostTblPairWise, ISD, MTy); |
| 746 | if (Idx != -1) |
| 747 | return LT.first * AVX1CostTblPairWise[Idx].Cost; |
| 748 | } |
| 749 | |
| 750 | if (ST->hasSSE42()) { |
| 751 | int Idx = CostTableLookup(SSE42CostTblPairWise, ISD, MTy); |
| 752 | if (Idx != -1) |
| 753 | return LT.first * SSE42CostTblPairWise[Idx].Cost; |
| 754 | } |
| 755 | } else { |
| 756 | if (ST->hasAVX()) { |
| 757 | int Idx = CostTableLookup(AVX1CostTblNoPairWise, ISD, MTy); |
| 758 | if (Idx != -1) |
| 759 | return LT.first * AVX1CostTblNoPairWise[Idx].Cost; |
| 760 | } |
| 761 | |
| 762 | if (ST->hasSSE42()) { |
| 763 | int Idx = CostTableLookup(SSE42CostTblNoPairWise, ISD, MTy); |
| 764 | if (Idx != -1) |
| 765 | return LT.first * SSE42CostTblNoPairWise[Idx].Cost; |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | return TargetTransformInfo::getReductionCost(Opcode, ValTy, IsPairwise); |
| 770 | } |
| 771 | |
Juergen Ributzka | f26beda | 2014-01-25 02:02:55 +0000 | [diff] [blame] | 772 | unsigned X86TTI::getIntImmCost(const APInt &Imm, Type *Ty) const { |
| 773 | assert(Ty->isIntegerTy()); |
| 774 | |
| 775 | unsigned BitSize = Ty->getPrimitiveSizeInBits(); |
| 776 | if (BitSize == 0) |
| 777 | return ~0U; |
| 778 | |
| 779 | if (Imm.getBitWidth() <= 64 && |
| 780 | (isInt<32>(Imm.getSExtValue()) || isUInt<32>(Imm.getZExtValue()))) |
| 781 | return TCC_Basic; |
| 782 | else |
| 783 | return 2 * TCC_Basic; |
| 784 | } |
| 785 | |
| 786 | unsigned X86TTI::getIntImmCost(unsigned Opcode, const APInt &Imm, |
| 787 | Type *Ty) const { |
| 788 | assert(Ty->isIntegerTy()); |
| 789 | |
| 790 | unsigned BitSize = Ty->getPrimitiveSizeInBits(); |
| 791 | if (BitSize == 0) |
| 792 | return ~0U; |
| 793 | |
| 794 | switch (Opcode) { |
| 795 | case Instruction::Add: |
| 796 | case Instruction::Sub: |
| 797 | case Instruction::Mul: |
| 798 | case Instruction::UDiv: |
| 799 | case Instruction::SDiv: |
| 800 | case Instruction::URem: |
| 801 | case Instruction::SRem: |
| 802 | case Instruction::Shl: |
| 803 | case Instruction::LShr: |
| 804 | case Instruction::AShr: |
| 805 | case Instruction::And: |
| 806 | case Instruction::Or: |
| 807 | case Instruction::Xor: |
| 808 | case Instruction::ICmp: |
| 809 | if (Imm.getBitWidth() <= 64 && isInt<32>(Imm.getSExtValue())) |
| 810 | return TCC_Free; |
| 811 | else |
| 812 | return X86TTI::getIntImmCost(Imm, Ty); |
| 813 | case Instruction::Trunc: |
| 814 | case Instruction::ZExt: |
| 815 | case Instruction::SExt: |
| 816 | case Instruction::IntToPtr: |
| 817 | case Instruction::PtrToInt: |
| 818 | case Instruction::BitCast: |
| 819 | case Instruction::Call: |
| 820 | case Instruction::Select: |
| 821 | case Instruction::Ret: |
| 822 | case Instruction::Load: |
| 823 | case Instruction::Store: |
| 824 | return X86TTI::getIntImmCost(Imm, Ty); |
| 825 | } |
| 826 | return TargetTransformInfo::getIntImmCost(Opcode, Imm, Ty); |
| 827 | } |
| 828 | |
| 829 | unsigned X86TTI::getIntImmCost(Intrinsic::ID IID, const APInt &Imm, |
| 830 | Type *Ty) const { |
| 831 | assert(Ty->isIntegerTy()); |
| 832 | |
| 833 | unsigned BitSize = Ty->getPrimitiveSizeInBits(); |
| 834 | if (BitSize == 0) |
| 835 | return ~0U; |
| 836 | |
| 837 | switch (IID) { |
| 838 | default: return TargetTransformInfo::getIntImmCost(IID, Imm, Ty); |
| 839 | case Intrinsic::sadd_with_overflow: |
| 840 | case Intrinsic::uadd_with_overflow: |
| 841 | case Intrinsic::ssub_with_overflow: |
| 842 | case Intrinsic::usub_with_overflow: |
| 843 | case Intrinsic::smul_with_overflow: |
| 844 | case Intrinsic::umul_with_overflow: |
| 845 | if (Imm.getBitWidth() <= 64 && isInt<32>(Imm.getSExtValue())) |
| 846 | return TCC_Free; |
| 847 | else |
| 848 | return X86TTI::getIntImmCost(Imm, Ty); |
| 849 | case Intrinsic::experimental_stackmap: |
| 850 | case Intrinsic::experimental_patchpoint_void: |
| 851 | case Intrinsic::experimental_patchpoint_i64: |
| 852 | if (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())) |
| 853 | return TCC_Free; |
| 854 | else |
| 855 | return X86TTI::getIntImmCost(Imm, Ty); |
| 856 | } |
| 857 | } |