blob: 9c09326ad668baa730ece5e899a6ad7e6f4c254e [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
17#define DEBUG_TYPE "x86tti"
18#include "X86.h"
19#include "X86TargetMachine.h"
Chandler Carruthd3e73552013-01-07 03:08:10 +000020#include "llvm/Analysis/TargetTransformInfo.h"
Juergen Ributzkaf26beda2014-01-25 02:02:55 +000021#include "llvm/IR/IntrinsicInst.h"
Chandler Carruth664e3542013-01-07 01:37:14 +000022#include "llvm/Support/Debug.h"
Renato Golind4c392e2013-01-24 23:01:00 +000023#include "llvm/Target/CostTable.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000024#include "llvm/Target/TargetLowering.h"
Chandler Carruth664e3542013-01-07 01:37:14 +000025using 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.
30namespace llvm {
31void initializeX86TTIPass(PassRegistry &);
32}
33
34namespace {
35
Craig Topper77dfe452014-03-02 08:08:51 +000036class X86TTI final : public ImmutablePass, public TargetTransformInfo {
Chandler Carruth664e3542013-01-07 01:37:14 +000037 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
44public:
Nadav Rotem02dd93e2013-06-27 17:54:10 +000045 X86TTI() : ImmutablePass(ID), ST(0), TLI(0) {
Chandler Carruth664e3542013-01-07 01:37:14 +000046 llvm_unreachable("This pass cannot be directly constructed");
47 }
48
49 X86TTI(const X86TargetMachine *TM)
Juergen Ributzka3e752e72014-01-24 18:22:59 +000050 : ImmutablePass(ID), ST(TM->getSubtargetImpl()),
51 TLI(TM->getTargetLowering()) {
Chandler Carruth664e3542013-01-07 01:37:14 +000052 initializeX86TTIPass(*PassRegistry::getPassRegistry());
53 }
54
Juergen Ributzka3e752e72014-01-24 18:22:59 +000055 virtual void initializePass() LLVM_OVERRIDE {
Chandler Carruth664e3542013-01-07 01:37:14 +000056 pushTTIStack(this);
57 }
58
59 virtual void finalizePass() {
60 popTTIStack();
61 }
62
Juergen Ributzka3e752e72014-01-24 18:22:59 +000063 virtual void getAnalysisUsage(AnalysisUsage &AU) const LLVM_OVERRIDE {
Chandler Carruth664e3542013-01-07 01:37:14 +000064 TargetTransformInfo::getAnalysisUsage(AU);
65 }
66
67 /// Pass identification.
68 static char ID;
69
70 /// Provide necessary pointer adjustments for the two base classes.
Juergen Ributzka3e752e72014-01-24 18:22:59 +000071 virtual void *getAdjustedAnalysisPointer(const void *ID) LLVM_OVERRIDE {
Chandler Carruth664e3542013-01-07 01:37:14 +000072 if (ID == &TargetTransformInfo::ID)
73 return (TargetTransformInfo*)this;
74 return this;
75 }
76
77 /// \name Scalar TTI Implementations
78 /// @{
Juergen Ributzka3e752e72014-01-24 18:22:59 +000079 virtual PopcntSupportKind
80 getPopcntSupport(unsigned TyWidth) const LLVM_OVERRIDE;
Chandler Carruth664e3542013-01-07 01:37:14 +000081
82 /// @}
83
84 /// \name Vector TTI Implementations
85 /// @{
86
Juergen Ributzka3e752e72014-01-24 18:22:59 +000087 virtual unsigned getNumberOfRegisters(bool Vector) const LLVM_OVERRIDE;
88 virtual unsigned getRegisterBitWidth(bool Vector) const LLVM_OVERRIDE;
89 virtual unsigned getMaximumUnrollFactor() const LLVM_OVERRIDE;
Arnold Schwaighoferb9773872013-04-04 23:26:21 +000090 virtual unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty,
91 OperandValueKind,
Juergen Ributzka3e752e72014-01-24 18:22:59 +000092 OperandValueKind) const LLVM_OVERRIDE;
Chandler Carruth664e3542013-01-07 01:37:14 +000093 virtual unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
Juergen Ributzka3e752e72014-01-24 18:22:59 +000094 int Index, Type *SubTp) const LLVM_OVERRIDE;
Chandler Carruth664e3542013-01-07 01:37:14 +000095 virtual unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
Juergen Ributzka3e752e72014-01-24 18:22:59 +000096 Type *Src) const LLVM_OVERRIDE;
Chandler Carruth664e3542013-01-07 01:37:14 +000097 virtual unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
Juergen Ributzka3e752e72014-01-24 18:22:59 +000098 Type *CondTy) const LLVM_OVERRIDE;
Chandler Carruth664e3542013-01-07 01:37:14 +000099 virtual unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
Juergen Ributzka3e752e72014-01-24 18:22:59 +0000100 unsigned Index) const LLVM_OVERRIDE;
Chandler Carruth664e3542013-01-07 01:37:14 +0000101 virtual unsigned getMemoryOpCost(unsigned Opcode, Type *Src,
102 unsigned Alignment,
Juergen Ributzka3e752e72014-01-24 18:22:59 +0000103 unsigned AddressSpace) const LLVM_OVERRIDE;
Chandler Carruth664e3542013-01-07 01:37:14 +0000104
Juergen Ributzka3e752e72014-01-24 18:22:59 +0000105 virtual unsigned
106 getAddressComputationCost(Type *PtrTy, bool IsComplex) const LLVM_OVERRIDE;
Yi Jiang5c343de2013-09-19 17:48:48 +0000107
108 virtual unsigned getReductionCost(unsigned Opcode, Type *Ty,
Juergen Ributzka3e752e72014-01-24 18:22:59 +0000109 bool IsPairwiseForm) const LLVM_OVERRIDE;
Arnold Schwaighofer6042a262013-07-12 19:16:07 +0000110
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000111 virtual unsigned getIntImmCost(const APInt &Imm,
112 Type *Ty) const LLVM_OVERRIDE;
113
114 virtual unsigned getIntImmCost(unsigned Opcode, const APInt &Imm,
115 Type *Ty) const LLVM_OVERRIDE;
116 virtual unsigned getIntImmCost(Intrinsic::ID IID, const APInt &Imm,
117 Type *Ty) const LLVM_OVERRIDE;
118
Chandler Carruth664e3542013-01-07 01:37:14 +0000119 /// @}
120};
121
122} // end anonymous namespace
123
124INITIALIZE_AG_PASS(X86TTI, TargetTransformInfo, "x86tti",
125 "X86 Target Transform Info", true, true, false)
126char X86TTI::ID = 0;
127
128ImmutablePass *
129llvm::createX86TargetTransformInfoPass(const X86TargetMachine *TM) {
130 return new X86TTI(TM);
131}
132
133
134//===----------------------------------------------------------------------===//
135//
136// X86 cost model.
137//
138//===----------------------------------------------------------------------===//
139
Chandler Carruth50a36cd2013-01-07 03:16:03 +0000140X86TTI::PopcntSupportKind X86TTI::getPopcntSupport(unsigned TyWidth) const {
Chandler Carruth664e3542013-01-07 01:37:14 +0000141 assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
142 // TODO: Currently the __builtin_popcount() implementation using SSE3
143 // instructions is inefficient. Once the problem is fixed, we should
Craig Topper0a63e1d2013-09-08 00:47:31 +0000144 // call ST->hasSSE3() instead of ST->hasPOPCNT().
145 return ST->hasPOPCNT() ? PSK_FastHardware : PSK_Software;
Chandler Carruth664e3542013-01-07 01:37:14 +0000146}
147
148unsigned X86TTI::getNumberOfRegisters(bool Vector) const {
Nadav Rotemb1791a72013-01-09 22:29:00 +0000149 if (Vector && !ST->hasSSE1())
150 return 0;
151
Chandler Carruth664e3542013-01-07 01:37:14 +0000152 if (ST->is64Bit())
153 return 16;
154 return 8;
155}
156
Nadav Rotemb1791a72013-01-09 22:29:00 +0000157unsigned X86TTI::getRegisterBitWidth(bool Vector) const {
158 if (Vector) {
159 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
Nadav Rotemb696c362013-01-09 01:15:42 +0000170unsigned X86TTI::getMaximumUnrollFactor() const {
171 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
Arnold Schwaighoferb9773872013-04-04 23:26:21 +0000182unsigned X86TTI::getArithmeticInstrCost(unsigned Opcode, Type *Ty,
183 OperandValueKind Op1Info,
184 OperandValueKind Op2Info) const {
Chandler Carruth664e3542013-01-07 01:37:14 +0000185 // Legalize the type.
186 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty);
187
188 int ISD = TLI->InstructionOpcodeToISD(Opcode);
189 assert(ISD && "Invalid opcode");
190
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000191 static const CostTblEntry<MVT::SimpleValueType> AVX2CostTable[] = {
Michael Liao70dd7f92013-03-20 22:01:10 +0000192 // Shifts on v4i64/v8i32 on AVX2 is legal even though we declare to
193 // customize them to detect the cases where shift amount is a scalar one.
194 { ISD::SHL, MVT::v4i32, 1 },
195 { ISD::SRL, MVT::v4i32, 1 },
196 { ISD::SRA, MVT::v4i32, 1 },
197 { ISD::SHL, MVT::v8i32, 1 },
198 { ISD::SRL, MVT::v8i32, 1 },
199 { ISD::SRA, MVT::v8i32, 1 },
200 { ISD::SHL, MVT::v2i64, 1 },
201 { ISD::SRL, MVT::v2i64, 1 },
202 { ISD::SHL, MVT::v4i64, 1 },
203 { ISD::SRL, MVT::v4i64, 1 },
Arnold Schwaighofere9b50162013-04-03 21:46:05 +0000204
205 { ISD::SHL, MVT::v32i8, 42 }, // cmpeqb sequence.
206 { ISD::SHL, MVT::v16i16, 16*10 }, // Scalarized.
207
208 { ISD::SRL, MVT::v32i8, 32*10 }, // Scalarized.
209 { ISD::SRL, MVT::v16i16, 8*10 }, // Scalarized.
210
211 { ISD::SRA, MVT::v32i8, 32*10 }, // Scalarized.
212 { ISD::SRA, MVT::v16i16, 16*10 }, // Scalarized.
213 { ISD::SRA, MVT::v4i64, 4*10 }, // Scalarized.
Arnold Schwaighofera04b9ef2013-06-25 19:14:09 +0000214
215 // Vectorizing division is a bad idea. See the SSE2 table for more comments.
216 { ISD::SDIV, MVT::v32i8, 32*20 },
217 { ISD::SDIV, MVT::v16i16, 16*20 },
218 { ISD::SDIV, MVT::v8i32, 8*20 },
219 { ISD::SDIV, MVT::v4i64, 4*20 },
220 { ISD::UDIV, MVT::v32i8, 32*20 },
221 { ISD::UDIV, MVT::v16i16, 16*20 },
222 { ISD::UDIV, MVT::v8i32, 8*20 },
223 { ISD::UDIV, MVT::v4i64, 4*20 },
Michael Liao70dd7f92013-03-20 22:01:10 +0000224 };
225
226 // Look for AVX2 lowering tricks.
227 if (ST->hasAVX2()) {
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000228 if (ISD == ISD::SHL && LT.second == MVT::v16i16 &&
229 (Op2Info == TargetTransformInfo::OK_UniformConstantValue ||
230 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue))
231 // On AVX2, a packed v16i16 shift left by a constant build_vector
232 // is lowered into a vector multiply (vpmullw).
233 return LT.first;
234
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000235 int Idx = CostTableLookup(AVX2CostTable, ISD, LT.second);
Michael Liao70dd7f92013-03-20 22:01:10 +0000236 if (Idx != -1)
237 return LT.first * AVX2CostTable[Idx].Cost;
238 }
239
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000240 static const CostTblEntry<MVT::SimpleValueType>
241 SSE2UniformConstCostTable[] = {
Arnold Schwaighofer44f902e2013-04-04 23:26:24 +0000242 // We don't correctly identify costs of casts because they are marked as
243 // custom.
244 // Constant splats are cheaper for the following instructions.
245 { ISD::SHL, MVT::v16i8, 1 }, // psllw.
246 { ISD::SHL, MVT::v8i16, 1 }, // psllw.
247 { ISD::SHL, MVT::v4i32, 1 }, // pslld
248 { ISD::SHL, MVT::v2i64, 1 }, // psllq.
249
250 { ISD::SRL, MVT::v16i8, 1 }, // psrlw.
251 { ISD::SRL, MVT::v8i16, 1 }, // psrlw.
252 { ISD::SRL, MVT::v4i32, 1 }, // psrld.
253 { ISD::SRL, MVT::v2i64, 1 }, // psrlq.
254
255 { ISD::SRA, MVT::v16i8, 4 }, // psrlw, pand, pxor, psubb.
256 { ISD::SRA, MVT::v8i16, 1 }, // psraw.
257 { ISD::SRA, MVT::v4i32, 1 }, // psrad.
258 };
259
260 if (Op2Info == TargetTransformInfo::OK_UniformConstantValue &&
261 ST->hasSSE2()) {
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000262 int Idx = CostTableLookup(SSE2UniformConstCostTable, ISD, LT.second);
Arnold Schwaighofer44f902e2013-04-04 23:26:24 +0000263 if (Idx != -1)
264 return LT.first * SSE2UniformConstCostTable[Idx].Cost;
265 }
266
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000267 if (ISD == ISD::SHL &&
268 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue) {
269 EVT VT = LT.second;
270 if ((VT == MVT::v8i16 && ST->hasSSE2()) ||
271 (VT == MVT::v4i32 && ST->hasSSE41()))
272 // Vector shift left by non uniform constant can be lowered
273 // into vector multiply (pmullw/pmulld).
274 return LT.first;
275 if (VT == MVT::v4i32 && ST->hasSSE2())
276 // A vector shift left by non uniform constant is converted
277 // into a vector multiply; the new multiply is eventually
278 // lowered into a sequence of shuffles and 2 x pmuludq.
279 ISD = ISD::MUL;
280 }
Arnold Schwaighofer44f902e2013-04-04 23:26:24 +0000281
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000282 static const CostTblEntry<MVT::SimpleValueType> SSE2CostTable[] = {
Arnold Schwaighofere9b50162013-04-03 21:46:05 +0000283 // We don't correctly identify costs of casts because they are marked as
284 // custom.
285 // For some cases, where the shift amount is a scalar we would be able
286 // to generate better code. Unfortunately, when this is the case the value
287 // (the splat) will get hoisted out of the loop, thereby making it invisible
288 // to ISel. The cost model must return worst case assumptions because it is
289 // used for vectorization and we don't want to make vectorized code worse
290 // than scalar code.
291 { ISD::SHL, MVT::v16i8, 30 }, // cmpeqb sequence.
292 { ISD::SHL, MVT::v8i16, 8*10 }, // Scalarized.
293 { ISD::SHL, MVT::v4i32, 2*5 }, // We optimized this using mul.
294 { ISD::SHL, MVT::v2i64, 2*10 }, // Scalarized.
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000295 { ISD::SHL, MVT::v4i64, 4*10 }, // Scalarized.
Arnold Schwaighofere9b50162013-04-03 21:46:05 +0000296
297 { ISD::SRL, MVT::v16i8, 16*10 }, // Scalarized.
298 { ISD::SRL, MVT::v8i16, 8*10 }, // Scalarized.
299 { ISD::SRL, MVT::v4i32, 4*10 }, // Scalarized.
300 { ISD::SRL, MVT::v2i64, 2*10 }, // Scalarized.
301
302 { ISD::SRA, MVT::v16i8, 16*10 }, // Scalarized.
303 { ISD::SRA, MVT::v8i16, 8*10 }, // Scalarized.
304 { ISD::SRA, MVT::v4i32, 4*10 }, // Scalarized.
305 { ISD::SRA, MVT::v2i64, 2*10 }, // Scalarized.
Arnold Schwaighofera04b9ef2013-06-25 19:14:09 +0000306
307 // It is not a good idea to vectorize division. We have to scalarize it and
308 // in the process we will often end up having to spilling regular
309 // registers. The overhead of division is going to dominate most kernels
310 // anyways so try hard to prevent vectorization of division - it is
311 // generally a bad idea. Assume somewhat arbitrarily that we have to be able
312 // to hide "20 cycles" for each lane.
313 { ISD::SDIV, MVT::v16i8, 16*20 },
314 { ISD::SDIV, MVT::v8i16, 8*20 },
315 { ISD::SDIV, MVT::v4i32, 4*20 },
316 { ISD::SDIV, MVT::v2i64, 2*20 },
317 { ISD::UDIV, MVT::v16i8, 16*20 },
318 { ISD::UDIV, MVT::v8i16, 8*20 },
319 { ISD::UDIV, MVT::v4i32, 4*20 },
320 { ISD::UDIV, MVT::v2i64, 2*20 },
Arnold Schwaighofere9b50162013-04-03 21:46:05 +0000321 };
322
323 if (ST->hasSSE2()) {
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000324 int Idx = CostTableLookup(SSE2CostTable, ISD, LT.second);
Arnold Schwaighofere9b50162013-04-03 21:46:05 +0000325 if (Idx != -1)
326 return LT.first * SSE2CostTable[Idx].Cost;
327 }
328
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000329 static const CostTblEntry<MVT::SimpleValueType> AVX1CostTable[] = {
Renato Goline1fb0592013-01-20 20:57:20 +0000330 // We don't have to scalarize unsupported ops. We can issue two half-sized
331 // operations and we only need to extract the upper YMM half.
332 // Two ops + 1 extract + 1 insert = 4.
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000333 { ISD::MUL, MVT::v16i16, 4 },
Renato Goline1fb0592013-01-20 20:57:20 +0000334 { ISD::MUL, MVT::v8i32, 4 },
335 { ISD::SUB, MVT::v8i32, 4 },
336 { ISD::ADD, MVT::v8i32, 4 },
Renato Goline1fb0592013-01-20 20:57:20 +0000337 { ISD::SUB, MVT::v4i64, 4 },
338 { ISD::ADD, MVT::v4i64, 4 },
Arnold Schwaighofer20ef54f2013-03-02 04:02:52 +0000339 // A v4i64 multiply is custom lowered as two split v2i64 vectors that then
340 // are lowered as a series of long multiplies(3), shifts(4) and adds(2)
341 // Because we believe v4i64 to be a legal type, we must also include the
342 // split factor of two in the cost table. Therefore, the cost here is 18
343 // instead of 9.
344 { ISD::MUL, MVT::v4i64, 18 },
345 };
Chandler Carruth664e3542013-01-07 01:37:14 +0000346
347 // Look for AVX1 lowering tricks.
Arnold Schwaighofer20ef54f2013-03-02 04:02:52 +0000348 if (ST->hasAVX() && !ST->hasAVX2()) {
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000349 EVT VT = LT.second;
350
351 // v16i16 and v8i32 shifts by non-uniform constants are lowered into a
352 // sequence of extract + two vector multiply + insert.
353 if (ISD == ISD::SHL && (VT == MVT::v8i32 || VT == MVT::v16i16) &&
354 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue)
355 ISD = ISD::MUL;
356
357 int Idx = CostTableLookup(AVX1CostTable, ISD, VT);
Renato Goline1fb0592013-01-20 20:57:20 +0000358 if (Idx != -1)
359 return LT.first * AVX1CostTable[Idx].Cost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000360 }
Arnold Schwaighofer20ef54f2013-03-02 04:02:52 +0000361
362 // Custom lowering of vectors.
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000363 static const CostTblEntry<MVT::SimpleValueType> CustomLowered[] = {
Arnold Schwaighofer20ef54f2013-03-02 04:02:52 +0000364 // A v2i64/v4i64 and multiply is custom lowered as a series of long
365 // multiplies(3), shifts(4) and adds(2).
366 { ISD::MUL, MVT::v2i64, 9 },
367 { ISD::MUL, MVT::v4i64, 9 },
368 };
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000369 int Idx = CostTableLookup(CustomLowered, ISD, LT.second);
Arnold Schwaighofer20ef54f2013-03-02 04:02:52 +0000370 if (Idx != -1)
371 return LT.first * CustomLowered[Idx].Cost;
372
373 // Special lowering of v4i32 mul on sse2, sse3: Lower v4i32 mul as 2x shuffle,
374 // 2x pmuludq, 2x shuffle.
375 if (ISD == ISD::MUL && LT.second == MVT::v4i32 && ST->hasSSE2() &&
376 !ST->hasSSE41())
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000377 return LT.first * 6;
Arnold Schwaighofer20ef54f2013-03-02 04:02:52 +0000378
Chandler Carruth664e3542013-01-07 01:37:14 +0000379 // Fallback to the default implementation.
Arnold Schwaighoferb9773872013-04-04 23:26:21 +0000380 return TargetTransformInfo::getArithmeticInstrCost(Opcode, Ty, Op1Info,
381 Op2Info);
Chandler Carruth664e3542013-01-07 01:37:14 +0000382}
383
384unsigned X86TTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index,
385 Type *SubTp) const {
386 // We only estimate the cost of reverse shuffles.
Chandler Carruth2109f472013-01-07 03:20:02 +0000387 if (Kind != SK_Reverse)
Chandler Carruth664e3542013-01-07 01:37:14 +0000388 return TargetTransformInfo::getShuffleCost(Kind, Tp, Index, SubTp);
389
390 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Tp);
391 unsigned Cost = 1;
392 if (LT.second.getSizeInBits() > 128)
393 Cost = 3; // Extract + insert + copy.
394
395 // Multiple by the number of parts.
396 return Cost * LT.first;
397}
398
399unsigned X86TTI::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) const {
400 int ISD = TLI->InstructionOpcodeToISD(Opcode);
401 assert(ISD && "Invalid opcode");
402
Arnold Schwaighoferf47d2d72013-04-08 18:05:48 +0000403 std::pair<unsigned, MVT> LTSrc = TLI->getTypeLegalizationCost(Src);
404 std::pair<unsigned, MVT> LTDest = TLI->getTypeLegalizationCost(Dst);
405
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000406 static const TypeConversionCostTblEntry<MVT::SimpleValueType>
407 SSE2ConvTbl[] = {
Arnold Schwaighoferf47d2d72013-04-08 18:05:48 +0000408 // These are somewhat magic numbers justified by looking at the output of
409 // Intel's IACA, running some kernels and making sure when we take
410 // legalization into account the throughput will be overestimated.
411 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 },
412 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 },
413 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 },
414 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 },
415 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 },
416 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 },
417 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 },
418 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 },
419 // There are faster sequences for float conversions.
420 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 },
421 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 15 },
422 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 },
423 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 },
424 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 },
425 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 15 },
426 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 },
427 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 },
428 };
429
430 if (ST->hasSSE2() && !ST->hasAVX()) {
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000431 int Idx =
432 ConvertCostTableLookup(SSE2ConvTbl, ISD, LTDest.second, LTSrc.second);
Arnold Schwaighoferf47d2d72013-04-08 18:05:48 +0000433 if (Idx != -1)
434 return LTSrc.first * SSE2ConvTbl[Idx].Cost;
435 }
436
Chandler Carruth664e3542013-01-07 01:37:14 +0000437 EVT SrcTy = TLI->getValueType(Src);
438 EVT DstTy = TLI->getValueType(Dst);
439
Arnold Schwaighoferc0c7ff42013-04-17 20:04:53 +0000440 // The function getSimpleVT only handles simple value types.
441 if (!SrcTy.isSimple() || !DstTy.isSimple())
442 return TargetTransformInfo::getCastInstrCost(Opcode, Dst, Src);
443
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000444 static const TypeConversionCostTblEntry<MVT::SimpleValueType>
Tim Northoverf0e21612014-02-06 18:18:36 +0000445 AVX2ConversionTbl[] = {
446 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 1 },
447 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 1 },
448 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 3 },
449 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 3 },
450 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 3 },
451 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 3 },
452 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
453 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
454 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i1, 3 },
455 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i1, 3 },
456 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 3 },
457 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 3 },
458 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
459 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
460 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
461 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
462
463 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i64, 2 },
464 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i64, 2 },
465 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 2 },
466 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 2 },
467 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 2 },
468 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 4 },
469 };
470
471 static const TypeConversionCostTblEntry<MVT::SimpleValueType>
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000472 AVXConversionTbl[] = {
Tim Northoverf0e21612014-02-06 18:18:36 +0000473 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 4 },
474 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 4 },
475 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 7 },
476 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 4 },
477 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 7 },
478 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 4 },
479 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 4 },
480 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 4 },
481 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i1, 6 },
482 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i1, 4 },
483 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 6 },
484 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 4 },
485 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 6 },
486 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
487 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 4 },
488 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 4 },
489
490 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i64, 4 },
491 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i64, 4 },
492 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 4 },
493 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 4 },
494 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 5 },
495 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i16, 4 },
496 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 9 },
Benjamin Kramer52ceb442013-04-01 10:23:49 +0000497
498 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i1, 8 },
499 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i8, 8 },
500 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i16, 5 },
501 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i32, 1 },
502 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i1, 3 },
503 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i8, 3 },
504 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i16, 3 },
505 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 },
506 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i1, 3 },
507 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i8, 3 },
508 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i16, 3 },
509 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i32, 1 },
510
511 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i1, 6 },
512 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i8, 5 },
513 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i16, 5 },
514 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 9 },
515 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i1, 7 },
516 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i8, 2 },
517 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i16, 2 },
518 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 6 },
519 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i1, 7 },
520 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i8, 2 },
521 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i16, 2 },
522 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i32, 6 },
523
Renato Goline1fb0592013-01-20 20:57:20 +0000524 { ISD::FP_TO_SINT, MVT::v8i8, MVT::v8f32, 1 },
525 { ISD::FP_TO_SINT, MVT::v4i8, MVT::v4f32, 1 },
Chandler Carruth664e3542013-01-07 01:37:14 +0000526 };
527
Tim Northoverf0e21612014-02-06 18:18:36 +0000528 if (ST->hasAVX2()) {
529 int Idx = ConvertCostTableLookup(AVX2ConversionTbl, ISD,
530 DstTy.getSimpleVT(), SrcTy.getSimpleVT());
531 if (Idx != -1)
532 return AVX2ConversionTbl[Idx].Cost;
533 }
534
Chandler Carruth664e3542013-01-07 01:37:14 +0000535 if (ST->hasAVX()) {
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000536 int Idx = ConvertCostTableLookup(AVXConversionTbl, ISD, DstTy.getSimpleVT(),
537 SrcTy.getSimpleVT());
Renato Goline1fb0592013-01-20 20:57:20 +0000538 if (Idx != -1)
539 return AVXConversionTbl[Idx].Cost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000540 }
541
542 return TargetTransformInfo::getCastInstrCost(Opcode, Dst, Src);
543}
544
545unsigned X86TTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
546 Type *CondTy) const {
547 // Legalize the type.
548 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
549
550 MVT MTy = LT.second;
551
552 int ISD = TLI->InstructionOpcodeToISD(Opcode);
553 assert(ISD && "Invalid opcode");
554
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000555 static const CostTblEntry<MVT::SimpleValueType> SSE42CostTbl[] = {
Renato Goline1fb0592013-01-20 20:57:20 +0000556 { ISD::SETCC, MVT::v2f64, 1 },
557 { ISD::SETCC, MVT::v4f32, 1 },
558 { ISD::SETCC, MVT::v2i64, 1 },
559 { ISD::SETCC, MVT::v4i32, 1 },
560 { ISD::SETCC, MVT::v8i16, 1 },
561 { ISD::SETCC, MVT::v16i8, 1 },
Chandler Carruth664e3542013-01-07 01:37:14 +0000562 };
563
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000564 static const CostTblEntry<MVT::SimpleValueType> AVX1CostTbl[] = {
Renato Goline1fb0592013-01-20 20:57:20 +0000565 { ISD::SETCC, MVT::v4f64, 1 },
566 { ISD::SETCC, MVT::v8f32, 1 },
Chandler Carruth664e3542013-01-07 01:37:14 +0000567 // AVX1 does not support 8-wide integer compare.
Renato Goline1fb0592013-01-20 20:57:20 +0000568 { ISD::SETCC, MVT::v4i64, 4 },
569 { ISD::SETCC, MVT::v8i32, 4 },
570 { ISD::SETCC, MVT::v16i16, 4 },
571 { ISD::SETCC, MVT::v32i8, 4 },
Chandler Carruth664e3542013-01-07 01:37:14 +0000572 };
573
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000574 static const CostTblEntry<MVT::SimpleValueType> AVX2CostTbl[] = {
Renato Goline1fb0592013-01-20 20:57:20 +0000575 { ISD::SETCC, MVT::v4i64, 1 },
576 { ISD::SETCC, MVT::v8i32, 1 },
577 { ISD::SETCC, MVT::v16i16, 1 },
578 { ISD::SETCC, MVT::v32i8, 1 },
Chandler Carruth664e3542013-01-07 01:37:14 +0000579 };
580
581 if (ST->hasAVX2()) {
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000582 int Idx = CostTableLookup(AVX2CostTbl, ISD, MTy);
Renato Goline1fb0592013-01-20 20:57:20 +0000583 if (Idx != -1)
584 return LT.first * AVX2CostTbl[Idx].Cost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000585 }
586
587 if (ST->hasAVX()) {
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000588 int Idx = CostTableLookup(AVX1CostTbl, ISD, MTy);
Renato Goline1fb0592013-01-20 20:57:20 +0000589 if (Idx != -1)
590 return LT.first * AVX1CostTbl[Idx].Cost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000591 }
592
593 if (ST->hasSSE42()) {
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000594 int Idx = CostTableLookup(SSE42CostTbl, ISD, MTy);
Renato Goline1fb0592013-01-20 20:57:20 +0000595 if (Idx != -1)
596 return LT.first * SSE42CostTbl[Idx].Cost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000597 }
598
599 return TargetTransformInfo::getCmpSelInstrCost(Opcode, ValTy, CondTy);
600}
601
602unsigned X86TTI::getVectorInstrCost(unsigned Opcode, Type *Val,
603 unsigned Index) const {
604 assert(Val->isVectorTy() && "This must be a vector type");
605
606 if (Index != -1U) {
607 // Legalize the type.
608 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Val);
609
610 // This type is legalized to a scalar type.
611 if (!LT.second.isVector())
612 return 0;
613
614 // The type may be split. Normalize the index to the new type.
615 unsigned Width = LT.second.getVectorNumElements();
616 Index = Index % Width;
617
618 // Floating point scalars are already located in index #0.
619 if (Val->getScalarType()->isFloatingPointTy() && Index == 0)
620 return 0;
621 }
622
623 return TargetTransformInfo::getVectorInstrCost(Opcode, Val, Index);
624}
625
Nadav Rotemf9ecbcb2013-06-27 17:52:04 +0000626unsigned X86TTI::getScalarizationOverhead(Type *Ty, bool Insert,
627 bool Extract) const {
628 assert (Ty->isVectorTy() && "Can only scalarize vectors");
629 unsigned Cost = 0;
630
631 for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
632 if (Insert)
633 Cost += TopTTI->getVectorInstrCost(Instruction::InsertElement, Ty, i);
634 if (Extract)
635 Cost += TopTTI->getVectorInstrCost(Instruction::ExtractElement, Ty, i);
636 }
637
638 return Cost;
639}
640
Chandler Carruth664e3542013-01-07 01:37:14 +0000641unsigned X86TTI::getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
642 unsigned AddressSpace) const {
Alp Tokerf907b892013-12-05 05:44:44 +0000643 // Handle non-power-of-two vectors such as <3 x float>
Nadav Rotemf9ecbcb2013-06-27 17:52:04 +0000644 if (VectorType *VTy = dyn_cast<VectorType>(Src)) {
645 unsigned NumElem = VTy->getVectorNumElements();
646
647 // Handle a few common cases:
648 // <3 x float>
649 if (NumElem == 3 && VTy->getScalarSizeInBits() == 32)
650 // Cost = 64 bit store + extract + 32 bit store.
651 return 3;
652
653 // <3 x double>
654 if (NumElem == 3 && VTy->getScalarSizeInBits() == 64)
655 // Cost = 128 bit store + unpack + 64 bit store.
656 return 3;
657
Alp Tokerf907b892013-12-05 05:44:44 +0000658 // Assume that all other non-power-of-two numbers are scalarized.
Nadav Rotemf9ecbcb2013-06-27 17:52:04 +0000659 if (!isPowerOf2_32(NumElem)) {
660 unsigned Cost = TargetTransformInfo::getMemoryOpCost(Opcode,
661 VTy->getScalarType(),
662 Alignment,
663 AddressSpace);
664 unsigned SplitCost = getScalarizationOverhead(Src,
665 Opcode == Instruction::Load,
666 Opcode==Instruction::Store);
667 return NumElem * Cost + SplitCost;
668 }
669 }
670
Chandler Carruth664e3542013-01-07 01:37:14 +0000671 // Legalize the type.
672 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Src);
673 assert((Opcode == Instruction::Load || Opcode == Instruction::Store) &&
674 "Invalid Opcode");
675
676 // Each load/store unit costs 1.
677 unsigned Cost = LT.first * 1;
678
679 // On Sandybridge 256bit load/stores are double pumped
680 // (but not on Haswell).
681 if (LT.second.getSizeInBits() > 128 && !ST->hasAVX2())
682 Cost*=2;
683
684 return Cost;
685}
Arnold Schwaighofer6042a262013-07-12 19:16:07 +0000686
687unsigned X86TTI::getAddressComputationCost(Type *Ty, bool IsComplex) const {
688 // Address computations in vectorized code with non-consecutive addresses will
689 // likely result in more instructions compared to scalar code where the
690 // computation can more often be merged into the index mode. The resulting
691 // extra micro-ops can significantly decrease throughput.
692 unsigned NumVectorInstToHideOverhead = 10;
693
694 if (Ty->isVectorTy() && IsComplex)
695 return NumVectorInstToHideOverhead;
696
697 return TargetTransformInfo::getAddressComputationCost(Ty, IsComplex);
698}
Yi Jiang5c343de2013-09-19 17:48:48 +0000699
700unsigned X86TTI::getReductionCost(unsigned Opcode, Type *ValTy,
701 bool IsPairwise) const {
702
703 std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
704
705 MVT MTy = LT.second;
706
707 int ISD = TLI->InstructionOpcodeToISD(Opcode);
708 assert(ISD && "Invalid opcode");
709
710 // We use the Intel Architecture Code Analyzer(IACA) to measure the throughput
711 // and make it as the cost.
712
713 static const CostTblEntry<MVT::SimpleValueType> SSE42CostTblPairWise[] = {
714 { ISD::FADD, MVT::v2f64, 2 },
715 { ISD::FADD, MVT::v4f32, 4 },
716 { ISD::ADD, MVT::v2i64, 2 }, // The data reported by the IACA tool is "1.6".
717 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.5".
718 { ISD::ADD, MVT::v8i16, 5 },
719 };
720
721 static const CostTblEntry<MVT::SimpleValueType> AVX1CostTblPairWise[] = {
722 { ISD::FADD, MVT::v4f32, 4 },
723 { ISD::FADD, MVT::v4f64, 5 },
724 { ISD::FADD, MVT::v8f32, 7 },
725 { ISD::ADD, MVT::v2i64, 1 }, // The data reported by the IACA tool is "1.5".
726 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.5".
727 { ISD::ADD, MVT::v4i64, 5 }, // The data reported by the IACA tool is "4.8".
728 { ISD::ADD, MVT::v8i16, 5 },
729 { ISD::ADD, MVT::v8i32, 5 },
730 };
731
732 static const CostTblEntry<MVT::SimpleValueType> SSE42CostTblNoPairWise[] = {
733 { ISD::FADD, MVT::v2f64, 2 },
734 { ISD::FADD, MVT::v4f32, 4 },
735 { ISD::ADD, MVT::v2i64, 2 }, // The data reported by the IACA tool is "1.6".
736 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.3".
737 { ISD::ADD, MVT::v8i16, 4 }, // The data reported by the IACA tool is "4.3".
738 };
739
740 static const CostTblEntry<MVT::SimpleValueType> AVX1CostTblNoPairWise[] = {
741 { ISD::FADD, MVT::v4f32, 3 },
742 { ISD::FADD, MVT::v4f64, 3 },
743 { ISD::FADD, MVT::v8f32, 4 },
744 { ISD::ADD, MVT::v2i64, 1 }, // The data reported by the IACA tool is "1.5".
745 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "2.8".
746 { ISD::ADD, MVT::v4i64, 3 },
747 { ISD::ADD, MVT::v8i16, 4 },
748 { ISD::ADD, MVT::v8i32, 5 },
749 };
750
751 if (IsPairwise) {
752 if (ST->hasAVX()) {
753 int Idx = CostTableLookup(AVX1CostTblPairWise, ISD, MTy);
754 if (Idx != -1)
755 return LT.first * AVX1CostTblPairWise[Idx].Cost;
756 }
757
758 if (ST->hasSSE42()) {
759 int Idx = CostTableLookup(SSE42CostTblPairWise, ISD, MTy);
760 if (Idx != -1)
761 return LT.first * SSE42CostTblPairWise[Idx].Cost;
762 }
763 } else {
764 if (ST->hasAVX()) {
765 int Idx = CostTableLookup(AVX1CostTblNoPairWise, ISD, MTy);
766 if (Idx != -1)
767 return LT.first * AVX1CostTblNoPairWise[Idx].Cost;
768 }
769
770 if (ST->hasSSE42()) {
771 int Idx = CostTableLookup(SSE42CostTblNoPairWise, ISD, MTy);
772 if (Idx != -1)
773 return LT.first * SSE42CostTblNoPairWise[Idx].Cost;
774 }
775 }
776
777 return TargetTransformInfo::getReductionCost(Opcode, ValTy, IsPairwise);
778}
779
Juergen Ributzkaf26beda2014-01-25 02:02:55 +0000780unsigned X86TTI::getIntImmCost(const APInt &Imm, Type *Ty) const {
781 assert(Ty->isIntegerTy());
782
783 unsigned BitSize = Ty->getPrimitiveSizeInBits();
784 if (BitSize == 0)
785 return ~0U;
786
787 if (Imm.getBitWidth() <= 64 &&
788 (isInt<32>(Imm.getSExtValue()) || isUInt<32>(Imm.getZExtValue())))
789 return TCC_Basic;
790 else
791 return 2 * TCC_Basic;
792}
793
794unsigned X86TTI::getIntImmCost(unsigned Opcode, const APInt &Imm,
795 Type *Ty) const {
796 assert(Ty->isIntegerTy());
797
798 unsigned BitSize = Ty->getPrimitiveSizeInBits();
799 if (BitSize == 0)
800 return ~0U;
801
802 switch (Opcode) {
803 case Instruction::Add:
804 case Instruction::Sub:
805 case Instruction::Mul:
806 case Instruction::UDiv:
807 case Instruction::SDiv:
808 case Instruction::URem:
809 case Instruction::SRem:
810 case Instruction::Shl:
811 case Instruction::LShr:
812 case Instruction::AShr:
813 case Instruction::And:
814 case Instruction::Or:
815 case Instruction::Xor:
816 case Instruction::ICmp:
817 if (Imm.getBitWidth() <= 64 && isInt<32>(Imm.getSExtValue()))
818 return TCC_Free;
819 else
820 return X86TTI::getIntImmCost(Imm, Ty);
821 case Instruction::Trunc:
822 case Instruction::ZExt:
823 case Instruction::SExt:
824 case Instruction::IntToPtr:
825 case Instruction::PtrToInt:
826 case Instruction::BitCast:
827 case Instruction::Call:
828 case Instruction::Select:
829 case Instruction::Ret:
830 case Instruction::Load:
831 case Instruction::Store:
832 return X86TTI::getIntImmCost(Imm, Ty);
833 }
834 return TargetTransformInfo::getIntImmCost(Opcode, Imm, Ty);
835}
836
837unsigned X86TTI::getIntImmCost(Intrinsic::ID IID, const APInt &Imm,
838 Type *Ty) const {
839 assert(Ty->isIntegerTy());
840
841 unsigned BitSize = Ty->getPrimitiveSizeInBits();
842 if (BitSize == 0)
843 return ~0U;
844
845 switch (IID) {
846 default: return TargetTransformInfo::getIntImmCost(IID, Imm, Ty);
847 case Intrinsic::sadd_with_overflow:
848 case Intrinsic::uadd_with_overflow:
849 case Intrinsic::ssub_with_overflow:
850 case Intrinsic::usub_with_overflow:
851 case Intrinsic::smul_with_overflow:
852 case Intrinsic::umul_with_overflow:
853 if (Imm.getBitWidth() <= 64 && isInt<32>(Imm.getSExtValue()))
854 return TCC_Free;
855 else
856 return X86TTI::getIntImmCost(Imm, Ty);
857 case Intrinsic::experimental_stackmap:
858 case Intrinsic::experimental_patchpoint_void:
859 case Intrinsic::experimental_patchpoint_i64:
860 if (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue()))
861 return TCC_Free;
862 else
863 return X86TTI::getIntImmCost(Imm, Ty);
864 }
865}