blob: e23d1b95ceba1c9fc0828b48e62a007688774f86 [file] [log] [blame]
Chandler Carruth664e3542013-01-07 01:37:14 +00001//===-- 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 Carruth664e3542013-01-07 01:37:14 +000017#include "X86.h"
18#include "X86TargetMachine.h"
Chandler Carruthd3e73552013-01-07 03:08:10 +000019#include "llvm/Analysis/TargetTransformInfo.h"
Juergen Ributzkaf26beda2014-01-25 02:02:55 +000020#include "llvm/IR/IntrinsicInst.h"
Chandler Carruth664e3542013-01-07 01:37:14 +000021#include "llvm/Support/Debug.h"
Renato Golind4c392e2013-01-24 23:01:00 +000022#include "llvm/Target/CostTable.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000023#include "llvm/Target/TargetLowering.h"
Chandler Carruth664e3542013-01-07 01:37:14 +000024using namespace llvm;
25
Chandler Carruth84e68b22014-04-22 02:41:26 +000026#define DEBUG_TYPE "x86tti"
27
Chandler Carruth664e3542013-01-07 01:37:14 +000028// Declare the pass initialization routine locally as target-specific passes
Eric Christopher89f18802014-05-22 01:21:44 +000029// don't have a target-wide initialization entry point, and so we rely on the
Chandler Carruth664e3542013-01-07 01:37:14 +000030// pass constructor initialization.
31namespace llvm {
32void initializeX86TTIPass(PassRegistry &);
33}
34
35namespace {
36
Craig Topper77dfe452014-03-02 08:08:51 +000037class X86TTI final : public ImmutablePass, public TargetTransformInfo {
Chandler Carruth664e3542013-01-07 01:37:14 +000038 const X86Subtarget *ST;
39 const X86TargetLowering *TLI;
40
41 /// Estimate the overhead of scalarizing an instruction. Insert and Extract
42 /// are set if the result needs to be inserted and/or extracted from vectors.
43 unsigned getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const;
44
45public:
Craig Topper062a2ba2014-04-25 05:30:21 +000046 X86TTI() : ImmutablePass(ID), ST(nullptr), TLI(nullptr) {
Chandler Carruth664e3542013-01-07 01:37:14 +000047 llvm_unreachable("This pass cannot be directly constructed");
48 }
49
50 X86TTI(const X86TargetMachine *TM)
Eric Christopherd9134482014-08-04 21:25:23 +000051 : ImmutablePass(ID), ST(TM->getSubtargetImpl()),
52 TLI(TM->getSubtargetImpl()->getTargetLowering()) {
Chandler Carruth664e3542013-01-07 01:37:14 +000053 initializeX86TTIPass(*PassRegistry::getPassRegistry());
54 }
55
Craig Topper24e685f2014-03-10 05:29:18 +000056 void initializePass() override {
Chandler Carruth664e3542013-01-07 01:37:14 +000057 pushTTIStack(this);
58 }
59
Craig Topper24e685f2014-03-10 05:29:18 +000060 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chandler Carruth664e3542013-01-07 01:37:14 +000061 TargetTransformInfo::getAnalysisUsage(AU);
62 }
63
64 /// Pass identification.
65 static char ID;
66
67 /// Provide necessary pointer adjustments for the two base classes.
Craig Topper24e685f2014-03-10 05:29:18 +000068 void *getAdjustedAnalysisPointer(const void *ID) override {
Chandler Carruth664e3542013-01-07 01:37:14 +000069 if (ID == &TargetTransformInfo::ID)
70 return (TargetTransformInfo*)this;
71 return this;
72 }
73
74 /// \name Scalar TTI Implementations
75 /// @{
Craig Topper24e685f2014-03-10 05:29:18 +000076 PopcntSupportKind getPopcntSupport(unsigned TyWidth) const override;
Chandler Carruth664e3542013-01-07 01:37:14 +000077
78 /// @}
79
80 /// \name Vector TTI Implementations
81 /// @{
82
Craig Topper24e685f2014-03-10 05:29:18 +000083 unsigned getNumberOfRegisters(bool Vector) const override;
84 unsigned getRegisterBitWidth(bool Vector) const override;
Sanjay Patelb653de12014-09-10 17:58:16 +000085 unsigned getMaxInterleaveFactor() const override;
Craig Topper24e685f2014-03-10 05:29:18 +000086 unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty, OperandValueKind,
Karthik Bhat7f33ff72014-08-25 04:56:54 +000087 OperandValueKind, OperandValueProperties,
88 OperandValueProperties) const override;
Craig Topper24e685f2014-03-10 05:29:18 +000089 unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
90 int Index, Type *SubTp) const override;
91 unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
92 Type *Src) const override;
93 unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
94 Type *CondTy) const override;
95 unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
96 unsigned Index) const override;
97 unsigned getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
98 unsigned AddressSpace) const override;
Chandler Carruth664e3542013-01-07 01:37:14 +000099
Craig Topper24e685f2014-03-10 05:29:18 +0000100 unsigned getAddressComputationCost(Type *PtrTy,
101 bool IsComplex) const override;
Arnold Schwaighofer6042a262013-07-12 19:16:07 +0000102
Craig Topper24e685f2014-03-10 05:29:18 +0000103 unsigned getReductionCost(unsigned Opcode, Type *Ty,
104 bool IsPairwiseForm) const override;
Craig Topper73156022014-03-02 09:09:27 +0000105
Juergen Ributzkab2e4edb2014-06-10 00:32:29 +0000106 unsigned getIntImmCost(int64_t) const;
107
Craig Topper24e685f2014-03-10 05:29:18 +0000108 unsigned getIntImmCost(const APInt &Imm, Type *Ty) const override;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000109
Juergen Ributzkaf0dff492014-03-21 06:04:45 +0000110 unsigned getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
Craig Topper24e685f2014-03-10 05:29:18 +0000111 Type *Ty) const override;
Juergen Ributzkaf0dff492014-03-21 06:04:45 +0000112 unsigned getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm,
Craig Topper24e685f2014-03-10 05:29:18 +0000113 Type *Ty) const override;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000114
Chandler Carruth664e3542013-01-07 01:37:14 +0000115 /// @}
116};
117
118} // end anonymous namespace
119
120INITIALIZE_AG_PASS(X86TTI, TargetTransformInfo, "x86tti",
121 "X86 Target Transform Info", true, true, false)
122char X86TTI::ID = 0;
123
124ImmutablePass *
125llvm::createX86TargetTransformInfoPass(const X86TargetMachine *TM) {
126 return new X86TTI(TM);
127}
128
129
130//===----------------------------------------------------------------------===//
131//
132// X86 cost model.
133//
134//===----------------------------------------------------------------------===//
135
Chandler Carruth50a36cd2013-01-07 03:16:03 +0000136X86TTI::PopcntSupportKind X86TTI::getPopcntSupport(unsigned TyWidth) const {
Chandler Carruth664e3542013-01-07 01:37:14 +0000137 assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
138 // TODO: Currently the __builtin_popcount() implementation using SSE3
139 // instructions is inefficient. Once the problem is fixed, we should
Craig Topper0a63e1d2013-09-08 00:47:31 +0000140 // call ST->hasSSE3() instead of ST->hasPOPCNT().
141 return ST->hasPOPCNT() ? PSK_FastHardware : PSK_Software;
Chandler Carruth664e3542013-01-07 01:37:14 +0000142}
143
144unsigned X86TTI::getNumberOfRegisters(bool Vector) const {
Nadav Rotemb1791a72013-01-09 22:29:00 +0000145 if (Vector && !ST->hasSSE1())
146 return 0;
147
Adam Nemet2820a5b2014-07-09 18:22:33 +0000148 if (ST->is64Bit()) {
149 if (Vector && ST->hasAVX512())
150 return 32;
Chandler Carruth664e3542013-01-07 01:37:14 +0000151 return 16;
Adam Nemet2820a5b2014-07-09 18:22:33 +0000152 }
Chandler Carruth664e3542013-01-07 01:37:14 +0000153 return 8;
154}
155
Nadav Rotemb1791a72013-01-09 22:29:00 +0000156unsigned X86TTI::getRegisterBitWidth(bool Vector) const {
157 if (Vector) {
Adam Nemet2820a5b2014-07-09 18:22:33 +0000158 if (ST->hasAVX512()) return 512;
Nadav Rotemb1791a72013-01-09 22:29:00 +0000159 if (ST->hasAVX()) return 256;
160 if (ST->hasSSE1()) return 128;
161 return 0;
162 }
163
164 if (ST->is64Bit())
165 return 64;
166 return 32;
167
168}
169
Sanjay Patelb653de12014-09-10 17:58:16 +0000170unsigned X86TTI::getMaxInterleaveFactor() const {
Nadav Rotemb696c362013-01-09 01:15:42 +0000171 if (ST->isAtom())
172 return 1;
173
174 // Sandybridge and Haswell have multiple execution ports and pipelined
175 // vector units.
176 if (ST->hasAVX())
177 return 4;
178
179 return 2;
180}
181
Karthik Bhat7f33ff72014-08-25 04:56:54 +0000182unsigned X86TTI::getArithmeticInstrCost(
183 unsigned Opcode, Type *Ty, OperandValueKind Op1Info,
184 OperandValueKind Op2Info, OperandValueProperties Opd1PropInfo,
185 OperandValueProperties Opd2PropInfo) const {
Chandler Carruth664e3542013-01-07 01:37:14 +0000186 // Legalize the type.
187 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty);
188
189 int ISD = TLI->InstructionOpcodeToISD(Opcode);
190 assert(ISD && "Invalid opcode");
191
Karthik Bhat7f33ff72014-08-25 04:56:54 +0000192 if (ISD == ISD::SDIV &&
193 Op2Info == TargetTransformInfo::OK_UniformConstantValue &&
194 Opd2PropInfo == TargetTransformInfo::OP_PowerOf2) {
195 // On X86, vector signed division by constants power-of-two are
196 // normally expanded to the sequence SRA + SRL + ADD + SRA.
197 // The OperandValue properties many not be same as that of previous
198 // operation;conservatively assume OP_None.
199 unsigned Cost =
200 2 * getArithmeticInstrCost(Instruction::AShr, Ty, Op1Info, Op2Info,
201 TargetTransformInfo::OP_None,
202 TargetTransformInfo::OP_None);
203 Cost += getArithmeticInstrCost(Instruction::LShr, Ty, Op1Info, Op2Info,
204 TargetTransformInfo::OP_None,
205 TargetTransformInfo::OP_None);
206 Cost += getArithmeticInstrCost(Instruction::Add, Ty, Op1Info, Op2Info,
207 TargetTransformInfo::OP_None,
208 TargetTransformInfo::OP_None);
209
210 return Cost;
211 }
212
Benjamin Kramer7c372272014-04-26 14:53:05 +0000213 static const CostTblEntry<MVT::SimpleValueType>
214 AVX2UniformConstCostTable[] = {
215 { ISD::SDIV, MVT::v16i16, 6 }, // vpmulhw sequence
216 { ISD::UDIV, MVT::v16i16, 6 }, // vpmulhuw sequence
217 { ISD::SDIV, MVT::v8i32, 15 }, // vpmuldq sequence
218 { ISD::UDIV, MVT::v8i32, 15 }, // vpmuludq sequence
219 };
220
221 if (Op2Info == TargetTransformInfo::OK_UniformConstantValue &&
222 ST->hasAVX2()) {
223 int Idx = CostTableLookup(AVX2UniformConstCostTable, ISD, LT.second);
224 if (Idx != -1)
225 return LT.first * AVX2UniformConstCostTable[Idx].Cost;
226 }
227
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000228 static const CostTblEntry<MVT::SimpleValueType> AVX2CostTable[] = {
Michael Liao70dd7f92013-03-20 22:01:10 +0000229 // Shifts on v4i64/v8i32 on AVX2 is legal even though we declare to
230 // customize them to detect the cases where shift amount is a scalar one.
231 { ISD::SHL, MVT::v4i32, 1 },
232 { ISD::SRL, MVT::v4i32, 1 },
233 { ISD::SRA, MVT::v4i32, 1 },
234 { ISD::SHL, MVT::v8i32, 1 },
235 { ISD::SRL, MVT::v8i32, 1 },
236 { ISD::SRA, MVT::v8i32, 1 },
237 { ISD::SHL, MVT::v2i64, 1 },
238 { ISD::SRL, MVT::v2i64, 1 },
239 { ISD::SHL, MVT::v4i64, 1 },
240 { ISD::SRL, MVT::v4i64, 1 },
Arnold Schwaighofere9b50162013-04-03 21:46:05 +0000241
242 { ISD::SHL, MVT::v32i8, 42 }, // cmpeqb sequence.
243 { ISD::SHL, MVT::v16i16, 16*10 }, // Scalarized.
244
245 { ISD::SRL, MVT::v32i8, 32*10 }, // Scalarized.
246 { ISD::SRL, MVT::v16i16, 8*10 }, // Scalarized.
247
248 { ISD::SRA, MVT::v32i8, 32*10 }, // Scalarized.
249 { ISD::SRA, MVT::v16i16, 16*10 }, // Scalarized.
250 { ISD::SRA, MVT::v4i64, 4*10 }, // Scalarized.
Arnold Schwaighofera04b9ef2013-06-25 19:14:09 +0000251
252 // Vectorizing division is a bad idea. See the SSE2 table for more comments.
253 { ISD::SDIV, MVT::v32i8, 32*20 },
254 { ISD::SDIV, MVT::v16i16, 16*20 },
255 { ISD::SDIV, MVT::v8i32, 8*20 },
256 { ISD::SDIV, MVT::v4i64, 4*20 },
257 { ISD::UDIV, MVT::v32i8, 32*20 },
258 { ISD::UDIV, MVT::v16i16, 16*20 },
259 { ISD::UDIV, MVT::v8i32, 8*20 },
260 { ISD::UDIV, MVT::v4i64, 4*20 },
Michael Liao70dd7f92013-03-20 22:01:10 +0000261 };
262
263 // Look for AVX2 lowering tricks.
264 if (ST->hasAVX2()) {
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000265 if (ISD == ISD::SHL && LT.second == MVT::v16i16 &&
266 (Op2Info == TargetTransformInfo::OK_UniformConstantValue ||
267 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue))
268 // On AVX2, a packed v16i16 shift left by a constant build_vector
269 // is lowered into a vector multiply (vpmullw).
270 return LT.first;
271
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000272 int Idx = CostTableLookup(AVX2CostTable, ISD, LT.second);
Michael Liao70dd7f92013-03-20 22:01:10 +0000273 if (Idx != -1)
274 return LT.first * AVX2CostTable[Idx].Cost;
275 }
276
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000277 static const CostTblEntry<MVT::SimpleValueType>
278 SSE2UniformConstCostTable[] = {
Arnold Schwaighofer44f902e2013-04-04 23:26:24 +0000279 // We don't correctly identify costs of casts because they are marked as
280 // custom.
281 // Constant splats are cheaper for the following instructions.
282 { ISD::SHL, MVT::v16i8, 1 }, // psllw.
283 { ISD::SHL, MVT::v8i16, 1 }, // psllw.
284 { ISD::SHL, MVT::v4i32, 1 }, // pslld
285 { ISD::SHL, MVT::v2i64, 1 }, // psllq.
286
287 { ISD::SRL, MVT::v16i8, 1 }, // psrlw.
288 { ISD::SRL, MVT::v8i16, 1 }, // psrlw.
289 { ISD::SRL, MVT::v4i32, 1 }, // psrld.
290 { ISD::SRL, MVT::v2i64, 1 }, // psrlq.
291
292 { ISD::SRA, MVT::v16i8, 4 }, // psrlw, pand, pxor, psubb.
293 { ISD::SRA, MVT::v8i16, 1 }, // psraw.
294 { ISD::SRA, MVT::v4i32, 1 }, // psrad.
Benjamin Kramer7c372272014-04-26 14:53:05 +0000295
296 { ISD::SDIV, MVT::v8i16, 6 }, // pmulhw sequence
297 { ISD::UDIV, MVT::v8i16, 6 }, // pmulhuw sequence
Benjamin Kramerce4b3fe2014-04-27 18:47:54 +0000298 { ISD::SDIV, MVT::v4i32, 19 }, // pmuludq sequence
Benjamin Kramer7c372272014-04-26 14:53:05 +0000299 { ISD::UDIV, MVT::v4i32, 15 }, // pmuludq sequence
Arnold Schwaighofer44f902e2013-04-04 23:26:24 +0000300 };
301
302 if (Op2Info == TargetTransformInfo::OK_UniformConstantValue &&
303 ST->hasSSE2()) {
Benjamin Kramerce4b3fe2014-04-27 18:47:54 +0000304 // pmuldq sequence.
305 if (ISD == ISD::SDIV && LT.second == MVT::v4i32 && ST->hasSSE41())
306 return LT.first * 15;
307
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000308 int Idx = CostTableLookup(SSE2UniformConstCostTable, ISD, LT.second);
Arnold Schwaighofer44f902e2013-04-04 23:26:24 +0000309 if (Idx != -1)
310 return LT.first * SSE2UniformConstCostTable[Idx].Cost;
311 }
312
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000313 if (ISD == ISD::SHL &&
314 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue) {
315 EVT VT = LT.second;
316 if ((VT == MVT::v8i16 && ST->hasSSE2()) ||
317 (VT == MVT::v4i32 && ST->hasSSE41()))
318 // Vector shift left by non uniform constant can be lowered
319 // into vector multiply (pmullw/pmulld).
320 return LT.first;
321 if (VT == MVT::v4i32 && ST->hasSSE2())
322 // A vector shift left by non uniform constant is converted
323 // into a vector multiply; the new multiply is eventually
324 // lowered into a sequence of shuffles and 2 x pmuludq.
325 ISD = ISD::MUL;
326 }
Arnold Schwaighofer44f902e2013-04-04 23:26:24 +0000327
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000328 static const CostTblEntry<MVT::SimpleValueType> SSE2CostTable[] = {
Arnold Schwaighofere9b50162013-04-03 21:46:05 +0000329 // We don't correctly identify costs of casts because they are marked as
330 // custom.
331 // For some cases, where the shift amount is a scalar we would be able
332 // to generate better code. Unfortunately, when this is the case the value
333 // (the splat) will get hoisted out of the loop, thereby making it invisible
334 // to ISel. The cost model must return worst case assumptions because it is
335 // used for vectorization and we don't want to make vectorized code worse
336 // than scalar code.
337 { ISD::SHL, MVT::v16i8, 30 }, // cmpeqb sequence.
338 { ISD::SHL, MVT::v8i16, 8*10 }, // Scalarized.
339 { ISD::SHL, MVT::v4i32, 2*5 }, // We optimized this using mul.
340 { ISD::SHL, MVT::v2i64, 2*10 }, // Scalarized.
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000341 { ISD::SHL, MVT::v4i64, 4*10 }, // Scalarized.
Arnold Schwaighofere9b50162013-04-03 21:46:05 +0000342
343 { ISD::SRL, MVT::v16i8, 16*10 }, // Scalarized.
344 { ISD::SRL, MVT::v8i16, 8*10 }, // Scalarized.
345 { ISD::SRL, MVT::v4i32, 4*10 }, // Scalarized.
346 { ISD::SRL, MVT::v2i64, 2*10 }, // Scalarized.
347
348 { ISD::SRA, MVT::v16i8, 16*10 }, // Scalarized.
349 { ISD::SRA, MVT::v8i16, 8*10 }, // Scalarized.
350 { ISD::SRA, MVT::v4i32, 4*10 }, // Scalarized.
351 { ISD::SRA, MVT::v2i64, 2*10 }, // Scalarized.
Arnold Schwaighofera04b9ef2013-06-25 19:14:09 +0000352
353 // It is not a good idea to vectorize division. We have to scalarize it and
354 // in the process we will often end up having to spilling regular
355 // registers. The overhead of division is going to dominate most kernels
356 // anyways so try hard to prevent vectorization of division - it is
357 // generally a bad idea. Assume somewhat arbitrarily that we have to be able
358 // to hide "20 cycles" for each lane.
359 { ISD::SDIV, MVT::v16i8, 16*20 },
360 { ISD::SDIV, MVT::v8i16, 8*20 },
361 { ISD::SDIV, MVT::v4i32, 4*20 },
362 { ISD::SDIV, MVT::v2i64, 2*20 },
363 { ISD::UDIV, MVT::v16i8, 16*20 },
364 { ISD::UDIV, MVT::v8i16, 8*20 },
365 { ISD::UDIV, MVT::v4i32, 4*20 },
366 { ISD::UDIV, MVT::v2i64, 2*20 },
Arnold Schwaighofere9b50162013-04-03 21:46:05 +0000367 };
368
369 if (ST->hasSSE2()) {
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000370 int Idx = CostTableLookup(SSE2CostTable, ISD, LT.second);
Arnold Schwaighofere9b50162013-04-03 21:46:05 +0000371 if (Idx != -1)
372 return LT.first * SSE2CostTable[Idx].Cost;
373 }
374
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000375 static const CostTblEntry<MVT::SimpleValueType> AVX1CostTable[] = {
Renato Goline1fb0592013-01-20 20:57:20 +0000376 // We don't have to scalarize unsupported ops. We can issue two half-sized
377 // operations and we only need to extract the upper YMM half.
378 // Two ops + 1 extract + 1 insert = 4.
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000379 { ISD::MUL, MVT::v16i16, 4 },
Renato Goline1fb0592013-01-20 20:57:20 +0000380 { ISD::MUL, MVT::v8i32, 4 },
381 { ISD::SUB, MVT::v8i32, 4 },
382 { ISD::ADD, MVT::v8i32, 4 },
Renato Goline1fb0592013-01-20 20:57:20 +0000383 { ISD::SUB, MVT::v4i64, 4 },
384 { ISD::ADD, MVT::v4i64, 4 },
Arnold Schwaighofer20ef54f2013-03-02 04:02:52 +0000385 // A v4i64 multiply is custom lowered as two split v2i64 vectors that then
386 // are lowered as a series of long multiplies(3), shifts(4) and adds(2)
387 // Because we believe v4i64 to be a legal type, we must also include the
388 // split factor of two in the cost table. Therefore, the cost here is 18
389 // instead of 9.
390 { ISD::MUL, MVT::v4i64, 18 },
391 };
Chandler Carruth664e3542013-01-07 01:37:14 +0000392
393 // Look for AVX1 lowering tricks.
Arnold Schwaighofer20ef54f2013-03-02 04:02:52 +0000394 if (ST->hasAVX() && !ST->hasAVX2()) {
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000395 EVT VT = LT.second;
396
397 // v16i16 and v8i32 shifts by non-uniform constants are lowered into a
398 // sequence of extract + two vector multiply + insert.
399 if (ISD == ISD::SHL && (VT == MVT::v8i32 || VT == MVT::v16i16) &&
400 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue)
401 ISD = ISD::MUL;
402
403 int Idx = CostTableLookup(AVX1CostTable, ISD, VT);
Renato Goline1fb0592013-01-20 20:57:20 +0000404 if (Idx != -1)
405 return LT.first * AVX1CostTable[Idx].Cost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000406 }
Arnold Schwaighofer20ef54f2013-03-02 04:02:52 +0000407
408 // Custom lowering of vectors.
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000409 static const CostTblEntry<MVT::SimpleValueType> CustomLowered[] = {
Arnold Schwaighofer20ef54f2013-03-02 04:02:52 +0000410 // A v2i64/v4i64 and multiply is custom lowered as a series of long
411 // multiplies(3), shifts(4) and adds(2).
412 { ISD::MUL, MVT::v2i64, 9 },
413 { ISD::MUL, MVT::v4i64, 9 },
414 };
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000415 int Idx = CostTableLookup(CustomLowered, ISD, LT.second);
Arnold Schwaighofer20ef54f2013-03-02 04:02:52 +0000416 if (Idx != -1)
417 return LT.first * CustomLowered[Idx].Cost;
418
419 // Special lowering of v4i32 mul on sse2, sse3: Lower v4i32 mul as 2x shuffle,
420 // 2x pmuludq, 2x shuffle.
421 if (ISD == ISD::MUL && LT.second == MVT::v4i32 && ST->hasSSE2() &&
422 !ST->hasSSE41())
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000423 return LT.first * 6;
Arnold Schwaighofer20ef54f2013-03-02 04:02:52 +0000424
Chandler Carruth664e3542013-01-07 01:37:14 +0000425 // Fallback to the default implementation.
Arnold Schwaighoferb9773872013-04-04 23:26:21 +0000426 return TargetTransformInfo::getArithmeticInstrCost(Opcode, Ty, Op1Info,
427 Op2Info);
Chandler Carruth664e3542013-01-07 01:37:14 +0000428}
429
430unsigned X86TTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index,
431 Type *SubTp) const {
Karthik Bhate03a25d2014-06-20 04:32:48 +0000432 // We only estimate the cost of reverse and alternate shuffles.
433 if (Kind != SK_Reverse && Kind != SK_Alternate)
Chandler Carruth664e3542013-01-07 01:37:14 +0000434 return TargetTransformInfo::getShuffleCost(Kind, Tp, Index, SubTp);
435
Karthik Bhate03a25d2014-06-20 04:32:48 +0000436 if (Kind == SK_Reverse) {
437 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Tp);
438 unsigned Cost = 1;
439 if (LT.second.getSizeInBits() > 128)
440 Cost = 3; // Extract + insert + copy.
Chandler Carruth664e3542013-01-07 01:37:14 +0000441
Karthik Bhate03a25d2014-06-20 04:32:48 +0000442 // Multiple by the number of parts.
443 return Cost * LT.first;
444 }
445
446 if (Kind == SK_Alternate) {
Andrea Di Biagioc8e8bda2014-07-03 22:24:18 +0000447 // 64-bit packed float vectors (v2f32) are widened to type v4f32.
448 // 64-bit packed integer vectors (v2i32) are promoted to type v2i64.
Karthik Bhate03a25d2014-06-20 04:32:48 +0000449 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Tp);
450
Andrea Di Biagioc8e8bda2014-07-03 22:24:18 +0000451 // The backend knows how to generate a single VEX.256 version of
452 // instruction VPBLENDW if the target supports AVX2.
453 if (ST->hasAVX2() && LT.second == MVT::v16i16)
454 return LT.first;
455
456 static const CostTblEntry<MVT::SimpleValueType> AVXAltShuffleTbl[] = {
457 {ISD::VECTOR_SHUFFLE, MVT::v4i64, 1}, // vblendpd
458 {ISD::VECTOR_SHUFFLE, MVT::v4f64, 1}, // vblendpd
459
460 {ISD::VECTOR_SHUFFLE, MVT::v8i32, 1}, // vblendps
461 {ISD::VECTOR_SHUFFLE, MVT::v8f32, 1}, // vblendps
462
463 // This shuffle is custom lowered into a sequence of:
464 // 2x vextractf128 , 2x vpblendw , 1x vinsertf128
465 {ISD::VECTOR_SHUFFLE, MVT::v16i16, 5},
466
467 // This shuffle is custom lowered into a long sequence of:
468 // 2x vextractf128 , 4x vpshufb , 2x vpor , 1x vinsertf128
469 {ISD::VECTOR_SHUFFLE, MVT::v32i8, 9}
470 };
471
472 if (ST->hasAVX()) {
473 int Idx = CostTableLookup(AVXAltShuffleTbl, ISD::VECTOR_SHUFFLE, LT.second);
474 if (Idx != -1)
475 return LT.first * AVXAltShuffleTbl[Idx].Cost;
476 }
477
478 static const CostTblEntry<MVT::SimpleValueType> SSE41AltShuffleTbl[] = {
479 // These are lowered into movsd.
480 {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1},
481 {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1},
482
483 // packed float vectors with four elements are lowered into BLENDI dag
484 // nodes. A v4i32/v4f32 BLENDI generates a single 'blendps'/'blendpd'.
485 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 1},
486 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 1},
487
488 // This shuffle generates a single pshufw.
489 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 1},
490
491 // There is no instruction that matches a v16i8 alternate shuffle.
492 // The backend will expand it into the sequence 'pshufb + pshufb + or'.
493 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 3}
494 };
495
496 if (ST->hasSSE41()) {
497 int Idx = CostTableLookup(SSE41AltShuffleTbl, ISD::VECTOR_SHUFFLE, LT.second);
498 if (Idx != -1)
499 return LT.first * SSE41AltShuffleTbl[Idx].Cost;
500 }
501
502 static const CostTblEntry<MVT::SimpleValueType> SSSE3AltShuffleTbl[] = {
503 {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1}, // movsd
504 {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1}, // movsd
505
506 // SSE3 doesn't have 'blendps'. The following shuffles are expanded into
507 // the sequence 'shufps + pshufd'
508 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 2},
509 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 2},
510
511 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 3}, // pshufb + pshufb + or
512 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 3} // pshufb + pshufb + or
513 };
514
515 if (ST->hasSSSE3()) {
516 int Idx = CostTableLookup(SSSE3AltShuffleTbl, ISD::VECTOR_SHUFFLE, LT.second);
517 if (Idx != -1)
518 return LT.first * SSSE3AltShuffleTbl[Idx].Cost;
519 }
520
521 static const CostTblEntry<MVT::SimpleValueType> SSEAltShuffleTbl[] = {
522 {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1}, // movsd
523 {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1}, // movsd
524
525 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 2}, // shufps + pshufd
526 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 2}, // shufps + pshufd
527
528 // This is expanded into a long sequence of four extract + four insert.
529 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 8}, // 4 x pextrw + 4 pinsrw.
530
531 // 8 x (pinsrw + pextrw + and + movb + movzb + or)
532 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 48}
533 };
534
535 // Fall-back (SSE3 and SSE2).
536 int Idx = CostTableLookup(SSEAltShuffleTbl, ISD::VECTOR_SHUFFLE, LT.second);
537 if (Idx != -1)
538 return LT.first * SSEAltShuffleTbl[Idx].Cost;
539 return TargetTransformInfo::getShuffleCost(Kind, Tp, Index, SubTp);
Karthik Bhate03a25d2014-06-20 04:32:48 +0000540 }
541
542 return TargetTransformInfo::getShuffleCost(Kind, Tp, Index, SubTp);
Chandler Carruth664e3542013-01-07 01:37:14 +0000543}
544
545unsigned X86TTI::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) const {
546 int ISD = TLI->InstructionOpcodeToISD(Opcode);
547 assert(ISD && "Invalid opcode");
548
Arnold Schwaighoferf47d2d72013-04-08 18:05:48 +0000549 std::pair<unsigned, MVT> LTSrc = TLI->getTypeLegalizationCost(Src);
550 std::pair<unsigned, MVT> LTDest = TLI->getTypeLegalizationCost(Dst);
551
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000552 static const TypeConversionCostTblEntry<MVT::SimpleValueType>
553 SSE2ConvTbl[] = {
Arnold Schwaighoferf47d2d72013-04-08 18:05:48 +0000554 // These are somewhat magic numbers justified by looking at the output of
555 // Intel's IACA, running some kernels and making sure when we take
556 // legalization into account the throughput will be overestimated.
557 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 },
558 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 },
559 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 },
560 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 },
561 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 },
562 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 },
563 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 },
564 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 },
565 // There are faster sequences for float conversions.
566 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 },
567 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 15 },
568 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 },
569 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 },
570 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 },
571 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 15 },
572 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 },
573 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 },
574 };
575
576 if (ST->hasSSE2() && !ST->hasAVX()) {
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000577 int Idx =
578 ConvertCostTableLookup(SSE2ConvTbl, ISD, LTDest.second, LTSrc.second);
Arnold Schwaighoferf47d2d72013-04-08 18:05:48 +0000579 if (Idx != -1)
580 return LTSrc.first * SSE2ConvTbl[Idx].Cost;
581 }
582
Chandler Carruth664e3542013-01-07 01:37:14 +0000583 EVT SrcTy = TLI->getValueType(Src);
584 EVT DstTy = TLI->getValueType(Dst);
585
Arnold Schwaighoferc0c7ff42013-04-17 20:04:53 +0000586 // The function getSimpleVT only handles simple value types.
587 if (!SrcTy.isSimple() || !DstTy.isSimple())
588 return TargetTransformInfo::getCastInstrCost(Opcode, Dst, Src);
589
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000590 static const TypeConversionCostTblEntry<MVT::SimpleValueType>
Tim Northoverf0e21612014-02-06 18:18:36 +0000591 AVX2ConversionTbl[] = {
592 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 1 },
593 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 1 },
594 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 3 },
595 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 3 },
596 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 3 },
597 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 3 },
598 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
599 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
600 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i1, 3 },
601 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i1, 3 },
602 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 3 },
603 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 3 },
604 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
605 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
606 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
607 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
608
609 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i64, 2 },
610 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i64, 2 },
611 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 2 },
612 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 2 },
613 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 2 },
614 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 4 },
615 };
616
617 static const TypeConversionCostTblEntry<MVT::SimpleValueType>
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000618 AVXConversionTbl[] = {
Tim Northoverf0e21612014-02-06 18:18:36 +0000619 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 4 },
620 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 4 },
621 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 7 },
622 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 4 },
623 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 7 },
624 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 4 },
625 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 4 },
626 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 4 },
627 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i1, 6 },
628 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i1, 4 },
629 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 6 },
630 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 4 },
631 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 6 },
632 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
633 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 4 },
634 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 4 },
635
636 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i64, 4 },
637 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i64, 4 },
638 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 4 },
639 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 4 },
640 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 5 },
641 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i16, 4 },
642 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 9 },
Benjamin Kramer52ceb442013-04-01 10:23:49 +0000643
644 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i1, 8 },
645 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i8, 8 },
646 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i16, 5 },
647 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i32, 1 },
648 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i1, 3 },
649 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i8, 3 },
650 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i16, 3 },
651 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 },
652 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i1, 3 },
653 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i8, 3 },
654 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i16, 3 },
655 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i32, 1 },
656
657 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i1, 6 },
658 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i8, 5 },
659 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i16, 5 },
660 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 9 },
661 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i1, 7 },
662 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i8, 2 },
663 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i16, 2 },
664 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 6 },
665 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i1, 7 },
666 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i8, 2 },
667 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i16, 2 },
668 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i32, 6 },
Quentin Colombet85b904d2014-03-27 22:27:41 +0000669 // The generic code to compute the scalar overhead is currently broken.
670 // Workaround this limitation by estimating the scalarization overhead
671 // here. We have roughly 10 instructions per scalar element.
672 // Multiply that by the vector width.
673 // FIXME: remove that when PR19268 is fixed.
Quentin Colombet3914bf52014-03-27 00:52:16 +0000674 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 },
675 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i64, 4*10 },
Benjamin Kramer52ceb442013-04-01 10:23:49 +0000676
Jim Grosbach72fbde82014-03-27 00:04:11 +0000677 { ISD::FP_TO_SINT, MVT::v8i8, MVT::v8f32, 7 },
Renato Goline1fb0592013-01-20 20:57:20 +0000678 { ISD::FP_TO_SINT, MVT::v4i8, MVT::v4f32, 1 },
Adam Nemet6dafe972014-03-30 18:07:13 +0000679 // This node is expanded into scalarized operations but BasicTTI is overly
680 // optimistic estimating its cost. It computes 3 per element (one
681 // vector-extract, one scalar conversion and one vector-insert). The
682 // problem is that the inserts form a read-modify-write chain so latency
683 // should be factored in too. Inflating the cost per element by 1.
684 { ISD::FP_TO_UINT, MVT::v8i32, MVT::v8f32, 8*4 },
Adam Nemet10c4ce22014-03-31 21:54:48 +0000685 { ISD::FP_TO_UINT, MVT::v4i32, MVT::v4f64, 4*4 },
Chandler Carruth664e3542013-01-07 01:37:14 +0000686 };
687
Tim Northoverf0e21612014-02-06 18:18:36 +0000688 if (ST->hasAVX2()) {
689 int Idx = ConvertCostTableLookup(AVX2ConversionTbl, ISD,
690 DstTy.getSimpleVT(), SrcTy.getSimpleVT());
691 if (Idx != -1)
692 return AVX2ConversionTbl[Idx].Cost;
693 }
694
Chandler Carruth664e3542013-01-07 01:37:14 +0000695 if (ST->hasAVX()) {
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000696 int Idx = ConvertCostTableLookup(AVXConversionTbl, ISD, DstTy.getSimpleVT(),
697 SrcTy.getSimpleVT());
Renato Goline1fb0592013-01-20 20:57:20 +0000698 if (Idx != -1)
699 return AVXConversionTbl[Idx].Cost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000700 }
701
702 return TargetTransformInfo::getCastInstrCost(Opcode, Dst, Src);
703}
704
705unsigned X86TTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
706 Type *CondTy) const {
707 // Legalize the type.
708 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
709
710 MVT MTy = LT.second;
711
712 int ISD = TLI->InstructionOpcodeToISD(Opcode);
713 assert(ISD && "Invalid opcode");
714
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000715 static const CostTblEntry<MVT::SimpleValueType> SSE42CostTbl[] = {
Renato Goline1fb0592013-01-20 20:57:20 +0000716 { ISD::SETCC, MVT::v2f64, 1 },
717 { ISD::SETCC, MVT::v4f32, 1 },
718 { ISD::SETCC, MVT::v2i64, 1 },
719 { ISD::SETCC, MVT::v4i32, 1 },
720 { ISD::SETCC, MVT::v8i16, 1 },
721 { ISD::SETCC, MVT::v16i8, 1 },
Chandler Carruth664e3542013-01-07 01:37:14 +0000722 };
723
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000724 static const CostTblEntry<MVT::SimpleValueType> AVX1CostTbl[] = {
Renato Goline1fb0592013-01-20 20:57:20 +0000725 { ISD::SETCC, MVT::v4f64, 1 },
726 { ISD::SETCC, MVT::v8f32, 1 },
Chandler Carruth664e3542013-01-07 01:37:14 +0000727 // AVX1 does not support 8-wide integer compare.
Renato Goline1fb0592013-01-20 20:57:20 +0000728 { ISD::SETCC, MVT::v4i64, 4 },
729 { ISD::SETCC, MVT::v8i32, 4 },
730 { ISD::SETCC, MVT::v16i16, 4 },
731 { ISD::SETCC, MVT::v32i8, 4 },
Chandler Carruth664e3542013-01-07 01:37:14 +0000732 };
733
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000734 static const CostTblEntry<MVT::SimpleValueType> AVX2CostTbl[] = {
Renato Goline1fb0592013-01-20 20:57:20 +0000735 { ISD::SETCC, MVT::v4i64, 1 },
736 { ISD::SETCC, MVT::v8i32, 1 },
737 { ISD::SETCC, MVT::v16i16, 1 },
738 { ISD::SETCC, MVT::v32i8, 1 },
Chandler Carruth664e3542013-01-07 01:37:14 +0000739 };
740
741 if (ST->hasAVX2()) {
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000742 int Idx = CostTableLookup(AVX2CostTbl, ISD, MTy);
Renato Goline1fb0592013-01-20 20:57:20 +0000743 if (Idx != -1)
744 return LT.first * AVX2CostTbl[Idx].Cost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000745 }
746
747 if (ST->hasAVX()) {
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000748 int Idx = CostTableLookup(AVX1CostTbl, ISD, MTy);
Renato Goline1fb0592013-01-20 20:57:20 +0000749 if (Idx != -1)
750 return LT.first * AVX1CostTbl[Idx].Cost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000751 }
752
753 if (ST->hasSSE42()) {
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000754 int Idx = CostTableLookup(SSE42CostTbl, ISD, MTy);
Renato Goline1fb0592013-01-20 20:57:20 +0000755 if (Idx != -1)
756 return LT.first * SSE42CostTbl[Idx].Cost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000757 }
758
759 return TargetTransformInfo::getCmpSelInstrCost(Opcode, ValTy, CondTy);
760}
761
762unsigned X86TTI::getVectorInstrCost(unsigned Opcode, Type *Val,
763 unsigned Index) const {
764 assert(Val->isVectorTy() && "This must be a vector type");
765
766 if (Index != -1U) {
767 // Legalize the type.
768 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Val);
769
770 // This type is legalized to a scalar type.
771 if (!LT.second.isVector())
772 return 0;
773
774 // The type may be split. Normalize the index to the new type.
775 unsigned Width = LT.second.getVectorNumElements();
776 Index = Index % Width;
777
778 // Floating point scalars are already located in index #0.
779 if (Val->getScalarType()->isFloatingPointTy() && Index == 0)
780 return 0;
781 }
782
783 return TargetTransformInfo::getVectorInstrCost(Opcode, Val, Index);
784}
785
Nadav Rotemf9ecbcb2013-06-27 17:52:04 +0000786unsigned X86TTI::getScalarizationOverhead(Type *Ty, bool Insert,
787 bool Extract) const {
788 assert (Ty->isVectorTy() && "Can only scalarize vectors");
789 unsigned Cost = 0;
790
791 for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
792 if (Insert)
793 Cost += TopTTI->getVectorInstrCost(Instruction::InsertElement, Ty, i);
794 if (Extract)
795 Cost += TopTTI->getVectorInstrCost(Instruction::ExtractElement, Ty, i);
796 }
797
798 return Cost;
799}
800
Chandler Carruth664e3542013-01-07 01:37:14 +0000801unsigned X86TTI::getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
802 unsigned AddressSpace) const {
Alp Tokerf907b892013-12-05 05:44:44 +0000803 // Handle non-power-of-two vectors such as <3 x float>
Nadav Rotemf9ecbcb2013-06-27 17:52:04 +0000804 if (VectorType *VTy = dyn_cast<VectorType>(Src)) {
805 unsigned NumElem = VTy->getVectorNumElements();
806
807 // Handle a few common cases:
808 // <3 x float>
809 if (NumElem == 3 && VTy->getScalarSizeInBits() == 32)
810 // Cost = 64 bit store + extract + 32 bit store.
811 return 3;
812
813 // <3 x double>
814 if (NumElem == 3 && VTy->getScalarSizeInBits() == 64)
815 // Cost = 128 bit store + unpack + 64 bit store.
816 return 3;
817
Alp Tokerf907b892013-12-05 05:44:44 +0000818 // Assume that all other non-power-of-two numbers are scalarized.
Nadav Rotemf9ecbcb2013-06-27 17:52:04 +0000819 if (!isPowerOf2_32(NumElem)) {
820 unsigned Cost = TargetTransformInfo::getMemoryOpCost(Opcode,
821 VTy->getScalarType(),
822 Alignment,
823 AddressSpace);
824 unsigned SplitCost = getScalarizationOverhead(Src,
825 Opcode == Instruction::Load,
826 Opcode==Instruction::Store);
827 return NumElem * Cost + SplitCost;
828 }
829 }
830
Chandler Carruth664e3542013-01-07 01:37:14 +0000831 // Legalize the type.
832 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Src);
833 assert((Opcode == Instruction::Load || Opcode == Instruction::Store) &&
834 "Invalid Opcode");
835
836 // Each load/store unit costs 1.
837 unsigned Cost = LT.first * 1;
838
839 // On Sandybridge 256bit load/stores are double pumped
840 // (but not on Haswell).
841 if (LT.second.getSizeInBits() > 128 && !ST->hasAVX2())
842 Cost*=2;
843
844 return Cost;
845}
Arnold Schwaighofer6042a262013-07-12 19:16:07 +0000846
847unsigned X86TTI::getAddressComputationCost(Type *Ty, bool IsComplex) const {
848 // Address computations in vectorized code with non-consecutive addresses will
849 // likely result in more instructions compared to scalar code where the
850 // computation can more often be merged into the index mode. The resulting
851 // extra micro-ops can significantly decrease throughput.
852 unsigned NumVectorInstToHideOverhead = 10;
853
854 if (Ty->isVectorTy() && IsComplex)
855 return NumVectorInstToHideOverhead;
856
857 return TargetTransformInfo::getAddressComputationCost(Ty, IsComplex);
858}
Yi Jiang5c343de2013-09-19 17:48:48 +0000859
860unsigned X86TTI::getReductionCost(unsigned Opcode, Type *ValTy,
861 bool IsPairwise) const {
862
863 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
864
865 MVT MTy = LT.second;
866
867 int ISD = TLI->InstructionOpcodeToISD(Opcode);
868 assert(ISD && "Invalid opcode");
869
870 // We use the Intel Architecture Code Analyzer(IACA) to measure the throughput
871 // and make it as the cost.
872
873 static const CostTblEntry<MVT::SimpleValueType> SSE42CostTblPairWise[] = {
874 { ISD::FADD, MVT::v2f64, 2 },
875 { ISD::FADD, MVT::v4f32, 4 },
876 { ISD::ADD, MVT::v2i64, 2 }, // The data reported by the IACA tool is "1.6".
877 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.5".
878 { ISD::ADD, MVT::v8i16, 5 },
879 };
880
881 static const CostTblEntry<MVT::SimpleValueType> AVX1CostTblPairWise[] = {
882 { ISD::FADD, MVT::v4f32, 4 },
883 { ISD::FADD, MVT::v4f64, 5 },
884 { ISD::FADD, MVT::v8f32, 7 },
885 { ISD::ADD, MVT::v2i64, 1 }, // The data reported by the IACA tool is "1.5".
886 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.5".
887 { ISD::ADD, MVT::v4i64, 5 }, // The data reported by the IACA tool is "4.8".
888 { ISD::ADD, MVT::v8i16, 5 },
889 { ISD::ADD, MVT::v8i32, 5 },
890 };
891
892 static const CostTblEntry<MVT::SimpleValueType> SSE42CostTblNoPairWise[] = {
893 { ISD::FADD, MVT::v2f64, 2 },
894 { ISD::FADD, MVT::v4f32, 4 },
895 { ISD::ADD, MVT::v2i64, 2 }, // The data reported by the IACA tool is "1.6".
896 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.3".
897 { ISD::ADD, MVT::v8i16, 4 }, // The data reported by the IACA tool is "4.3".
898 };
899
900 static const CostTblEntry<MVT::SimpleValueType> AVX1CostTblNoPairWise[] = {
901 { ISD::FADD, MVT::v4f32, 3 },
902 { ISD::FADD, MVT::v4f64, 3 },
903 { ISD::FADD, MVT::v8f32, 4 },
904 { ISD::ADD, MVT::v2i64, 1 }, // The data reported by the IACA tool is "1.5".
905 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "2.8".
906 { ISD::ADD, MVT::v4i64, 3 },
907 { ISD::ADD, MVT::v8i16, 4 },
908 { ISD::ADD, MVT::v8i32, 5 },
909 };
910
911 if (IsPairwise) {
912 if (ST->hasAVX()) {
913 int Idx = CostTableLookup(AVX1CostTblPairWise, ISD, MTy);
914 if (Idx != -1)
915 return LT.first * AVX1CostTblPairWise[Idx].Cost;
916 }
917
918 if (ST->hasSSE42()) {
919 int Idx = CostTableLookup(SSE42CostTblPairWise, ISD, MTy);
920 if (Idx != -1)
921 return LT.first * SSE42CostTblPairWise[Idx].Cost;
922 }
923 } else {
924 if (ST->hasAVX()) {
925 int Idx = CostTableLookup(AVX1CostTblNoPairWise, ISD, MTy);
926 if (Idx != -1)
927 return LT.first * AVX1CostTblNoPairWise[Idx].Cost;
928 }
929
930 if (ST->hasSSE42()) {
931 int Idx = CostTableLookup(SSE42CostTblNoPairWise, ISD, MTy);
932 if (Idx != -1)
933 return LT.first * SSE42CostTblNoPairWise[Idx].Cost;
934 }
935 }
936
937 return TargetTransformInfo::getReductionCost(Opcode, ValTy, IsPairwise);
938}
939
Juergen Ributzkab2e4edb2014-06-10 00:32:29 +0000940/// \brief Calculate the cost of materializing a 64-bit value. This helper
941/// method might only calculate a fraction of a larger immediate. Therefore it
942/// is valid to return a cost of ZERO.
943unsigned X86TTI::getIntImmCost(int64_t Val) const {
944 if (Val == 0)
945 return TCC_Free;
946
947 if (isInt<32>(Val))
948 return TCC_Basic;
949
950 return 2 * TCC_Basic;
951}
952
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000953unsigned X86TTI::getIntImmCost(const APInt &Imm, Type *Ty) const {
954 assert(Ty->isIntegerTy());
955
956 unsigned BitSize = Ty->getPrimitiveSizeInBits();
957 if (BitSize == 0)
958 return ~0U;
959
Juergen Ributzka43176172014-05-19 21:00:53 +0000960 // Never hoist constants larger than 128bit, because this might lead to
961 // incorrect code generation or assertions in codegen.
962 // Fixme: Create a cost model for types larger than i128 once the codegen
963 // issues have been fixed.
964 if (BitSize > 128)
965 return TCC_Free;
966
Juergen Ributzkaf0dff492014-03-21 06:04:45 +0000967 if (Imm == 0)
968 return TCC_Free;
969
Juergen Ributzkab2e4edb2014-06-10 00:32:29 +0000970 // Sign-extend all constants to a multiple of 64-bit.
971 APInt ImmVal = Imm;
972 if (BitSize & 0x3f)
973 ImmVal = Imm.sext((BitSize + 63) & ~0x3fU);
974
975 // Split the constant into 64-bit chunks and calculate the cost for each
976 // chunk.
977 unsigned Cost = 0;
978 for (unsigned ShiftVal = 0; ShiftVal < BitSize; ShiftVal += 64) {
979 APInt Tmp = ImmVal.ashr(ShiftVal).sextOrTrunc(64);
980 int64_t Val = Tmp.getSExtValue();
981 Cost += getIntImmCost(Val);
982 }
983 // We need at least one instruction to materialze the constant.
984 return std::max(1U, Cost);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000985}
986
Juergen Ributzkaf0dff492014-03-21 06:04:45 +0000987unsigned X86TTI::getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000988 Type *Ty) const {
989 assert(Ty->isIntegerTy());
990
991 unsigned BitSize = Ty->getPrimitiveSizeInBits();
Juergen Ributzka43176172014-05-19 21:00:53 +0000992 // There is no cost model for constants with a bit size of 0. Return TCC_Free
993 // here, so that constant hoisting will ignore this constant.
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000994 if (BitSize == 0)
Juergen Ributzka43176172014-05-19 21:00:53 +0000995 return TCC_Free;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000996
Juergen Ributzkaf0dff492014-03-21 06:04:45 +0000997 unsigned ImmIdx = ~0U;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000998 switch (Opcode) {
Juergen Ributzkaf0dff492014-03-21 06:04:45 +0000999 default: return TCC_Free;
1000 case Instruction::GetElementPtr:
Juergen Ributzka27435b32014-04-02 21:45:36 +00001001 // Always hoist the base address of a GetElementPtr. This prevents the
1002 // creation of new constants for every base constant that gets constant
1003 // folded with the offset.
Juergen Ributzka631c4912014-03-25 18:01:25 +00001004 if (Idx == 0)
1005 return 2 * TCC_Basic;
1006 return TCC_Free;
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001007 case Instruction::Store:
1008 ImmIdx = 0;
1009 break;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001010 case Instruction::Add:
1011 case Instruction::Sub:
1012 case Instruction::Mul:
1013 case Instruction::UDiv:
1014 case Instruction::SDiv:
1015 case Instruction::URem:
1016 case Instruction::SRem:
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001017 case Instruction::And:
1018 case Instruction::Or:
1019 case Instruction::Xor:
1020 case Instruction::ICmp:
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001021 ImmIdx = 1;
1022 break;
Michael Zolotukhin1f4a9602014-04-30 19:17:32 +00001023 // Always return TCC_Free for the shift value of a shift instruction.
1024 case Instruction::Shl:
1025 case Instruction::LShr:
1026 case Instruction::AShr:
1027 if (Idx == 1)
1028 return TCC_Free;
1029 break;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001030 case Instruction::Trunc:
1031 case Instruction::ZExt:
1032 case Instruction::SExt:
1033 case Instruction::IntToPtr:
1034 case Instruction::PtrToInt:
1035 case Instruction::BitCast:
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001036 case Instruction::PHI:
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001037 case Instruction::Call:
1038 case Instruction::Select:
1039 case Instruction::Ret:
1040 case Instruction::Load:
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001041 break;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001042 }
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001043
Juergen Ributzkab2e4edb2014-06-10 00:32:29 +00001044 if (Idx == ImmIdx) {
1045 unsigned NumConstants = (BitSize + 63) / 64;
1046 unsigned Cost = X86TTI::getIntImmCost(Imm, Ty);
Saleem Abdulrasool3c890c42014-06-12 17:56:18 +00001047 return (Cost <= NumConstants * TCC_Basic)
1048 ? static_cast<unsigned>(TCC_Free)
1049 : Cost;
Juergen Ributzkab2e4edb2014-06-10 00:32:29 +00001050 }
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001051
1052 return X86TTI::getIntImmCost(Imm, Ty);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001053}
1054
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001055unsigned X86TTI::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
1056 const APInt &Imm, Type *Ty) const {
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001057 assert(Ty->isIntegerTy());
1058
1059 unsigned BitSize = Ty->getPrimitiveSizeInBits();
Juergen Ributzka43176172014-05-19 21:00:53 +00001060 // There is no cost model for constants with a bit size of 0. Return TCC_Free
1061 // here, so that constant hoisting will ignore this constant.
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001062 if (BitSize == 0)
Juergen Ributzka43176172014-05-19 21:00:53 +00001063 return TCC_Free;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001064
1065 switch (IID) {
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001066 default: return TCC_Free;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001067 case Intrinsic::sadd_with_overflow:
1068 case Intrinsic::uadd_with_overflow:
1069 case Intrinsic::ssub_with_overflow:
1070 case Intrinsic::usub_with_overflow:
1071 case Intrinsic::smul_with_overflow:
1072 case Intrinsic::umul_with_overflow:
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001073 if ((Idx == 1) && Imm.getBitWidth() <= 64 && isInt<32>(Imm.getSExtValue()))
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001074 return TCC_Free;
Juergen Ributzka5eef98c2014-03-25 18:01:23 +00001075 break;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001076 case Intrinsic::experimental_stackmap:
Juergen Ributzka5eef98c2014-03-25 18:01:23 +00001077 if ((Idx < 2) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001078 return TCC_Free;
Juergen Ributzka5eef98c2014-03-25 18:01:23 +00001079 break;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001080 case Intrinsic::experimental_patchpoint_void:
1081 case Intrinsic::experimental_patchpoint_i64:
Juergen Ributzka5eef98c2014-03-25 18:01:23 +00001082 if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001083 return TCC_Free;
Juergen Ributzka5eef98c2014-03-25 18:01:23 +00001084 break;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001085 }
Juergen Ributzka5eef98c2014-03-25 18:01:23 +00001086 return X86TTI::getIntImmCost(Imm, Ty);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001087}