X86TTI: Adjust sdiv cost now that we can lower it on plain SSE2.
[oota-llvm.git] / lib / Target / X86 / X86TargetTransformInfo.cpp
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 #include "X86.h"
18 #include "X86TargetMachine.h"
19 #include "llvm/ADT/DepthFirstIterator.h"
20 #include "llvm/Analysis/LoopInfo.h"
21 #include "llvm/Analysis/TargetTransformInfo.h"
22 #include "llvm/IR/IntrinsicInst.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Target/CostTable.h"
26 #include "llvm/Target/TargetLowering.h"
27 using namespace llvm;
28
29 #define DEBUG_TYPE "x86tti"
30
31 // Declare the pass initialization routine locally as target-specific passes
32 // don't havve a target-wide initialization entry point, and so we rely on the
33 // pass constructor initialization.
34 namespace llvm {
35 void initializeX86TTIPass(PassRegistry &);
36 }
37
38 static cl::opt<bool>
39 UsePartialUnrolling("x86-use-partial-unrolling", cl::init(true),
40   cl::desc("Use partial unrolling for some X86 targets"), cl::Hidden);
41 static cl::opt<unsigned>
42 PartialUnrollingThreshold("x86-partial-unrolling-threshold", cl::init(0),
43   cl::desc("Threshold for X86 partial unrolling"), cl::Hidden);
44 static cl::opt<unsigned>
45 PartialUnrollingMaxBranches("x86-partial-max-branches", cl::init(2),
46   cl::desc("Threshold for taken branches in X86 partial unrolling"),
47   cl::Hidden);
48
49 namespace {
50
51 class X86TTI final : public ImmutablePass, public TargetTransformInfo {
52   const X86Subtarget *ST;
53   const X86TargetLowering *TLI;
54
55   /// Estimate the overhead of scalarizing an instruction. Insert and Extract
56   /// are set if the result needs to be inserted and/or extracted from vectors.
57   unsigned getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) const;
58
59 public:
60   X86TTI() : ImmutablePass(ID), ST(nullptr), TLI(nullptr) {
61     llvm_unreachable("This pass cannot be directly constructed");
62   }
63
64   X86TTI(const X86TargetMachine *TM)
65     : ImmutablePass(ID), ST(TM->getSubtargetImpl()),
66       TLI(TM->getTargetLowering()) {
67     initializeX86TTIPass(*PassRegistry::getPassRegistry());
68   }
69
70   void initializePass() override {
71     pushTTIStack(this);
72   }
73
74   void getAnalysisUsage(AnalysisUsage &AU) const override {
75     TargetTransformInfo::getAnalysisUsage(AU);
76   }
77
78   /// Pass identification.
79   static char ID;
80
81   /// Provide necessary pointer adjustments for the two base classes.
82   void *getAdjustedAnalysisPointer(const void *ID) override {
83     if (ID == &TargetTransformInfo::ID)
84       return (TargetTransformInfo*)this;
85     return this;
86   }
87
88   /// \name Scalar TTI Implementations
89   /// @{
90   PopcntSupportKind getPopcntSupport(unsigned TyWidth) const override;
91   void getUnrollingPreferences(Loop *L,
92                                UnrollingPreferences &UP) const override;
93
94   /// @}
95
96   /// \name Vector TTI Implementations
97   /// @{
98
99   unsigned getNumberOfRegisters(bool Vector) const override;
100   unsigned getRegisterBitWidth(bool Vector) const override;
101   unsigned getMaximumUnrollFactor() const override;
102   unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty, OperandValueKind,
103                                   OperandValueKind) const override;
104   unsigned getShuffleCost(ShuffleKind Kind, Type *Tp,
105                           int Index, Type *SubTp) const override;
106   unsigned getCastInstrCost(unsigned Opcode, Type *Dst,
107                             Type *Src) const override;
108   unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
109                               Type *CondTy) const override;
110   unsigned getVectorInstrCost(unsigned Opcode, Type *Val,
111                               unsigned Index) const override;
112   unsigned getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
113                            unsigned AddressSpace) const override;
114
115   unsigned getAddressComputationCost(Type *PtrTy,
116                                      bool IsComplex) const override;
117
118   unsigned getReductionCost(unsigned Opcode, Type *Ty,
119                             bool IsPairwiseForm) const override;
120
121   unsigned getIntImmCost(const APInt &Imm, Type *Ty) const override;
122
123   unsigned getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
124                          Type *Ty) const override;
125   unsigned getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm,
126                          Type *Ty) const override;
127
128   /// @}
129 };
130
131 } // end anonymous namespace
132
133 INITIALIZE_AG_PASS(X86TTI, TargetTransformInfo, "x86tti",
134                    "X86 Target Transform Info", true, true, false)
135 char X86TTI::ID = 0;
136
137 ImmutablePass *
138 llvm::createX86TargetTransformInfoPass(const X86TargetMachine *TM) {
139   return new X86TTI(TM);
140 }
141
142
143 //===----------------------------------------------------------------------===//
144 //
145 // X86 cost model.
146 //
147 //===----------------------------------------------------------------------===//
148
149 X86TTI::PopcntSupportKind X86TTI::getPopcntSupport(unsigned TyWidth) const {
150   assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
151   // TODO: Currently the __builtin_popcount() implementation using SSE3
152   //   instructions is inefficient. Once the problem is fixed, we should
153   //   call ST->hasSSE3() instead of ST->hasPOPCNT().
154   return ST->hasPOPCNT() ? PSK_FastHardware : PSK_Software;
155 }
156
157 void X86TTI::getUnrollingPreferences(Loop *L, UnrollingPreferences &UP) const {
158   if (!UsePartialUnrolling)
159     return;
160   // According to the Intel 64 and IA-32 Architectures Optimization Reference
161   // Manual, Intel Core models and later have a loop stream detector
162   // (and associated uop queue) that can benefit from partial unrolling.
163   // The relevant requirements are:
164   //  - The loop must have no more than 4 (8 for Nehalem and later) branches
165   //    taken, and none of them may be calls.
166   //  - The loop can have no more than 18 (28 for Nehalem and later) uops.
167
168   // According to the Software Optimization Guide for AMD Family 15h Processors,
169   // models 30h-4fh (Steamroller and later) have a loop predictor and loop
170   // buffer which can benefit from partial unrolling.
171   // The relevant requirements are:
172   //  - The loop must have fewer than 16 branches
173   //  - The loop must have less than 40 uops in all executed loop branches
174
175   unsigned MaxBranches, MaxOps;
176   if (PartialUnrollingThreshold.getNumOccurrences() > 0) {
177     MaxBranches = PartialUnrollingMaxBranches;
178     MaxOps = PartialUnrollingThreshold;
179   } else if (ST->isAtom()) {
180     // On the Atom, the throughput for taken branches is 2 cycles. For small
181     // simple loops, expand by a small factor to hide the backedge cost.
182     MaxBranches = 2;
183     MaxOps = 10;
184   } else if (ST->hasFSGSBase() && ST->hasXOP() /* Steamroller and later */) {
185     MaxBranches = 16;
186     MaxOps = 40;
187   } else if (ST->hasFMA4() /* Any other recent AMD */) {
188     return;
189   } else if (ST->hasAVX() || ST->hasSSE42() /* Nehalem and later */) {
190     MaxBranches = 8;
191     MaxOps = 28;
192   } else if (ST->hasSSSE3() /* Intel Core */) {
193     MaxBranches = 4;
194     MaxOps = 18;
195   } else {
196     return;
197   }
198
199   // Scan the loop: don't unroll loops with calls, and count the potential
200   // number of taken branches (this is somewhat conservative because we're
201   // counting all block transitions as potential branches while in reality some
202   // of these will become implicit via block placement).
203   unsigned MaxDepth = 0;
204   for (df_iterator<BasicBlock*> DI = df_begin(L->getHeader()),
205        DE = df_end(L->getHeader()); DI != DE;) {
206     if (!L->contains(*DI)) {
207       DI.skipChildren();
208       continue;
209     }
210
211     MaxDepth = std::max(MaxDepth, DI.getPathLength());
212     if (MaxDepth > MaxBranches)
213       return;
214
215     for (BasicBlock::iterator I = DI->begin(), IE = DI->end(); I != IE; ++I)
216       if (isa<CallInst>(I) || isa<InvokeInst>(I)) {
217         ImmutableCallSite CS(I);
218         if (const Function *F = CS.getCalledFunction()) {
219           if (!isLoweredToCall(F))
220             continue;
221         }
222
223         return;
224       }
225
226     ++DI;
227   }
228
229   // Enable runtime and partial unrolling up to the specified size.
230   UP.Partial = UP.Runtime = true;
231   UP.PartialThreshold = UP.PartialOptSizeThreshold = MaxOps;
232
233   // Set the maximum count based on the loop depth. The maximum number of
234   // branches taken in a loop (including the backedge) is equal to the maximum
235   // loop depth (the DFS path length from the loop header to any block in the
236   // loop). When the loop is unrolled, this depth (except for the backedge
237   // itself) is multiplied by the unrolling factor. This new unrolled depth
238   // must be less than the target-specific maximum branch count (which limits
239   // the number of taken branches in the uop buffer).
240   if (MaxDepth > 1)
241     UP.MaxCount = (MaxBranches-1)/(MaxDepth-1);
242 }
243
244 unsigned X86TTI::getNumberOfRegisters(bool Vector) const {
245   if (Vector && !ST->hasSSE1())
246     return 0;
247
248   if (ST->is64Bit())
249     return 16;
250   return 8;
251 }
252
253 unsigned X86TTI::getRegisterBitWidth(bool Vector) const {
254   if (Vector) {
255     if (ST->hasAVX()) return 256;
256     if (ST->hasSSE1()) return 128;
257     return 0;
258   }
259
260   if (ST->is64Bit())
261     return 64;
262   return 32;
263
264 }
265
266 unsigned X86TTI::getMaximumUnrollFactor() const {
267   if (ST->isAtom())
268     return 1;
269
270   // Sandybridge and Haswell have multiple execution ports and pipelined
271   // vector units.
272   if (ST->hasAVX())
273     return 4;
274
275   return 2;
276 }
277
278 unsigned X86TTI::getArithmeticInstrCost(unsigned Opcode, Type *Ty,
279                                         OperandValueKind Op1Info,
280                                         OperandValueKind Op2Info) const {
281   // Legalize the type.
282   std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Ty);
283
284   int ISD = TLI->InstructionOpcodeToISD(Opcode);
285   assert(ISD && "Invalid opcode");
286
287   static const CostTblEntry<MVT::SimpleValueType>
288   AVX2UniformConstCostTable[] = {
289     { ISD::SDIV, MVT::v16i16,  6 }, // vpmulhw sequence
290     { ISD::UDIV, MVT::v16i16,  6 }, // vpmulhuw sequence
291     { ISD::SDIV, MVT::v8i32,  15 }, // vpmuldq sequence
292     { ISD::UDIV, MVT::v8i32,  15 }, // vpmuludq sequence
293   };
294
295   if (Op2Info == TargetTransformInfo::OK_UniformConstantValue &&
296       ST->hasAVX2()) {
297     int Idx = CostTableLookup(AVX2UniformConstCostTable, ISD, LT.second);
298     if (Idx != -1)
299       return LT.first * AVX2UniformConstCostTable[Idx].Cost;
300   }
301
302   static const CostTblEntry<MVT::SimpleValueType> AVX2CostTable[] = {
303     // Shifts on v4i64/v8i32 on AVX2 is legal even though we declare to
304     // customize them to detect the cases where shift amount is a scalar one.
305     { ISD::SHL,     MVT::v4i32,    1 },
306     { ISD::SRL,     MVT::v4i32,    1 },
307     { ISD::SRA,     MVT::v4i32,    1 },
308     { ISD::SHL,     MVT::v8i32,    1 },
309     { ISD::SRL,     MVT::v8i32,    1 },
310     { ISD::SRA,     MVT::v8i32,    1 },
311     { ISD::SHL,     MVT::v2i64,    1 },
312     { ISD::SRL,     MVT::v2i64,    1 },
313     { ISD::SHL,     MVT::v4i64,    1 },
314     { ISD::SRL,     MVT::v4i64,    1 },
315
316     { ISD::SHL,  MVT::v32i8,  42 }, // cmpeqb sequence.
317     { ISD::SHL,  MVT::v16i16,  16*10 }, // Scalarized.
318
319     { ISD::SRL,  MVT::v32i8,  32*10 }, // Scalarized.
320     { ISD::SRL,  MVT::v16i16,  8*10 }, // Scalarized.
321
322     { ISD::SRA,  MVT::v32i8,  32*10 }, // Scalarized.
323     { ISD::SRA,  MVT::v16i16,  16*10 }, // Scalarized.
324     { ISD::SRA,  MVT::v4i64,  4*10 }, // Scalarized.
325
326     // Vectorizing division is a bad idea. See the SSE2 table for more comments.
327     { ISD::SDIV,  MVT::v32i8,  32*20 },
328     { ISD::SDIV,  MVT::v16i16, 16*20 },
329     { ISD::SDIV,  MVT::v8i32,  8*20 },
330     { ISD::SDIV,  MVT::v4i64,  4*20 },
331     { ISD::UDIV,  MVT::v32i8,  32*20 },
332     { ISD::UDIV,  MVT::v16i16, 16*20 },
333     { ISD::UDIV,  MVT::v8i32,  8*20 },
334     { ISD::UDIV,  MVT::v4i64,  4*20 },
335   };
336
337   // Look for AVX2 lowering tricks.
338   if (ST->hasAVX2()) {
339     if (ISD == ISD::SHL && LT.second == MVT::v16i16 &&
340         (Op2Info == TargetTransformInfo::OK_UniformConstantValue ||
341          Op2Info == TargetTransformInfo::OK_NonUniformConstantValue))
342       // On AVX2, a packed v16i16 shift left by a constant build_vector
343       // is lowered into a vector multiply (vpmullw).
344       return LT.first;
345
346     int Idx = CostTableLookup(AVX2CostTable, ISD, LT.second);
347     if (Idx != -1)
348       return LT.first * AVX2CostTable[Idx].Cost;
349   }
350
351   static const CostTblEntry<MVT::SimpleValueType>
352   SSE2UniformConstCostTable[] = {
353     // We don't correctly identify costs of casts because they are marked as
354     // custom.
355     // Constant splats are cheaper for the following instructions.
356     { ISD::SHL,  MVT::v16i8,  1 }, // psllw.
357     { ISD::SHL,  MVT::v8i16,  1 }, // psllw.
358     { ISD::SHL,  MVT::v4i32,  1 }, // pslld
359     { ISD::SHL,  MVT::v2i64,  1 }, // psllq.
360
361     { ISD::SRL,  MVT::v16i8,  1 }, // psrlw.
362     { ISD::SRL,  MVT::v8i16,  1 }, // psrlw.
363     { ISD::SRL,  MVT::v4i32,  1 }, // psrld.
364     { ISD::SRL,  MVT::v2i64,  1 }, // psrlq.
365
366     { ISD::SRA,  MVT::v16i8,  4 }, // psrlw, pand, pxor, psubb.
367     { ISD::SRA,  MVT::v8i16,  1 }, // psraw.
368     { ISD::SRA,  MVT::v4i32,  1 }, // psrad.
369
370     { ISD::SDIV, MVT::v8i16,  6 }, // pmulhw sequence
371     { ISD::UDIV, MVT::v8i16,  6 }, // pmulhuw sequence
372     { ISD::SDIV, MVT::v4i32, 19 }, // pmuludq sequence
373     { ISD::UDIV, MVT::v4i32, 15 }, // pmuludq sequence
374   };
375
376   if (Op2Info == TargetTransformInfo::OK_UniformConstantValue &&
377       ST->hasSSE2()) {
378     // pmuldq sequence.
379     if (ISD == ISD::SDIV && LT.second == MVT::v4i32 && ST->hasSSE41())
380       return LT.first * 15;
381
382     int Idx = CostTableLookup(SSE2UniformConstCostTable, ISD, LT.second);
383     if (Idx != -1)
384       return LT.first * SSE2UniformConstCostTable[Idx].Cost;
385   }
386
387   if (ISD == ISD::SHL &&
388       Op2Info == TargetTransformInfo::OK_NonUniformConstantValue) {
389     EVT VT = LT.second;
390     if ((VT == MVT::v8i16 && ST->hasSSE2()) ||
391         (VT == MVT::v4i32 && ST->hasSSE41()))
392       // Vector shift left by non uniform constant can be lowered
393       // into vector multiply (pmullw/pmulld).
394       return LT.first;
395     if (VT == MVT::v4i32 && ST->hasSSE2())
396       // A vector shift left by non uniform constant is converted
397       // into a vector multiply; the new multiply is eventually
398       // lowered into a sequence of shuffles and 2 x pmuludq.
399       ISD = ISD::MUL;
400   }
401
402   static const CostTblEntry<MVT::SimpleValueType> SSE2CostTable[] = {
403     // We don't correctly identify costs of casts because they are marked as
404     // custom.
405     // For some cases, where the shift amount is a scalar we would be able
406     // to generate better code. Unfortunately, when this is the case the value
407     // (the splat) will get hoisted out of the loop, thereby making it invisible
408     // to ISel. The cost model must return worst case assumptions because it is
409     // used for vectorization and we don't want to make vectorized code worse
410     // than scalar code.
411     { ISD::SHL,  MVT::v16i8,  30 }, // cmpeqb sequence.
412     { ISD::SHL,  MVT::v8i16,  8*10 }, // Scalarized.
413     { ISD::SHL,  MVT::v4i32,  2*5 }, // We optimized this using mul.
414     { ISD::SHL,  MVT::v2i64,  2*10 }, // Scalarized.
415     { ISD::SHL,  MVT::v4i64,  4*10 }, // Scalarized. 
416
417     { ISD::SRL,  MVT::v16i8,  16*10 }, // Scalarized.
418     { ISD::SRL,  MVT::v8i16,  8*10 }, // Scalarized.
419     { ISD::SRL,  MVT::v4i32,  4*10 }, // Scalarized.
420     { ISD::SRL,  MVT::v2i64,  2*10 }, // Scalarized.
421
422     { ISD::SRA,  MVT::v16i8,  16*10 }, // Scalarized.
423     { ISD::SRA,  MVT::v8i16,  8*10 }, // Scalarized.
424     { ISD::SRA,  MVT::v4i32,  4*10 }, // Scalarized.
425     { ISD::SRA,  MVT::v2i64,  2*10 }, // Scalarized.
426
427     // It is not a good idea to vectorize division. We have to scalarize it and
428     // in the process we will often end up having to spilling regular
429     // registers. The overhead of division is going to dominate most kernels
430     // anyways so try hard to prevent vectorization of division - it is
431     // generally a bad idea. Assume somewhat arbitrarily that we have to be able
432     // to hide "20 cycles" for each lane.
433     { ISD::SDIV,  MVT::v16i8,  16*20 },
434     { ISD::SDIV,  MVT::v8i16,  8*20 },
435     { ISD::SDIV,  MVT::v4i32,  4*20 },
436     { ISD::SDIV,  MVT::v2i64,  2*20 },
437     { ISD::UDIV,  MVT::v16i8,  16*20 },
438     { ISD::UDIV,  MVT::v8i16,  8*20 },
439     { ISD::UDIV,  MVT::v4i32,  4*20 },
440     { ISD::UDIV,  MVT::v2i64,  2*20 },
441   };
442
443   if (ST->hasSSE2()) {
444     int Idx = CostTableLookup(SSE2CostTable, ISD, LT.second);
445     if (Idx != -1)
446       return LT.first * SSE2CostTable[Idx].Cost;
447   }
448
449   static const CostTblEntry<MVT::SimpleValueType> AVX1CostTable[] = {
450     // We don't have to scalarize unsupported ops. We can issue two half-sized
451     // operations and we only need to extract the upper YMM half.
452     // Two ops + 1 extract + 1 insert = 4.
453     { ISD::MUL,     MVT::v16i16,   4 },
454     { ISD::MUL,     MVT::v8i32,    4 },
455     { ISD::SUB,     MVT::v8i32,    4 },
456     { ISD::ADD,     MVT::v8i32,    4 },
457     { ISD::SUB,     MVT::v4i64,    4 },
458     { ISD::ADD,     MVT::v4i64,    4 },
459     // A v4i64 multiply is custom lowered as two split v2i64 vectors that then
460     // are lowered as a series of long multiplies(3), shifts(4) and adds(2)
461     // Because we believe v4i64 to be a legal type, we must also include the
462     // split factor of two in the cost table. Therefore, the cost here is 18
463     // instead of 9.
464     { ISD::MUL,     MVT::v4i64,    18 },
465   };
466
467   // Look for AVX1 lowering tricks.
468   if (ST->hasAVX() && !ST->hasAVX2()) {
469     EVT VT = LT.second;
470
471     // v16i16 and v8i32 shifts by non-uniform constants are lowered into a
472     // sequence of extract + two vector multiply + insert.
473     if (ISD == ISD::SHL && (VT == MVT::v8i32 || VT == MVT::v16i16) &&
474         Op2Info == TargetTransformInfo::OK_NonUniformConstantValue)
475       ISD = ISD::MUL;
476
477     int Idx = CostTableLookup(AVX1CostTable, ISD, VT);
478     if (Idx != -1)
479       return LT.first * AVX1CostTable[Idx].Cost;
480   }
481
482   // Custom lowering of vectors.
483   static const CostTblEntry<MVT::SimpleValueType> CustomLowered[] = {
484     // A v2i64/v4i64 and multiply is custom lowered as a series of long
485     // multiplies(3), shifts(4) and adds(2).
486     { ISD::MUL,     MVT::v2i64,    9 },
487     { ISD::MUL,     MVT::v4i64,    9 },
488   };
489   int Idx = CostTableLookup(CustomLowered, ISD, LT.second);
490   if (Idx != -1)
491     return LT.first * CustomLowered[Idx].Cost;
492
493   // Special lowering of v4i32 mul on sse2, sse3: Lower v4i32 mul as 2x shuffle,
494   // 2x pmuludq, 2x shuffle.
495   if (ISD == ISD::MUL && LT.second == MVT::v4i32 && ST->hasSSE2() &&
496       !ST->hasSSE41())
497     return LT.first * 6;
498
499   // Fallback to the default implementation.
500   return TargetTransformInfo::getArithmeticInstrCost(Opcode, Ty, Op1Info,
501                                                      Op2Info);
502 }
503
504 unsigned X86TTI::getShuffleCost(ShuffleKind Kind, Type *Tp, int Index,
505                                 Type *SubTp) const {
506   // We only estimate the cost of reverse shuffles.
507   if (Kind != SK_Reverse)
508     return TargetTransformInfo::getShuffleCost(Kind, Tp, Index, SubTp);
509
510   std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Tp);
511   unsigned Cost = 1;
512   if (LT.second.getSizeInBits() > 128)
513     Cost = 3; // Extract + insert + copy.
514
515   // Multiple by the number of parts.
516   return Cost * LT.first;
517 }
518
519 unsigned X86TTI::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) const {
520   int ISD = TLI->InstructionOpcodeToISD(Opcode);
521   assert(ISD && "Invalid opcode");
522
523   std::pair<unsigned, MVT> LTSrc = TLI->getTypeLegalizationCost(Src);
524   std::pair<unsigned, MVT> LTDest = TLI->getTypeLegalizationCost(Dst);
525
526   static const TypeConversionCostTblEntry<MVT::SimpleValueType>
527   SSE2ConvTbl[] = {
528     // These are somewhat magic numbers justified by looking at the output of
529     // Intel's IACA, running some kernels and making sure when we take
530     // legalization into account the throughput will be overestimated.
531     { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 },
532     { ISD::UINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 },
533     { ISD::UINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 },
534     { ISD::UINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 },
535     { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 },
536     { ISD::SINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 },
537     { ISD::SINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 },
538     { ISD::SINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 },
539     // There are faster sequences for float conversions.
540     { ISD::UINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 },
541     { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 15 },
542     { ISD::UINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 },
543     { ISD::UINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 },
544     { ISD::SINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 },
545     { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 15 },
546     { ISD::SINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 },
547     { ISD::SINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 },
548   };
549
550   if (ST->hasSSE2() && !ST->hasAVX()) {
551     int Idx =
552         ConvertCostTableLookup(SSE2ConvTbl, ISD, LTDest.second, LTSrc.second);
553     if (Idx != -1)
554       return LTSrc.first * SSE2ConvTbl[Idx].Cost;
555   }
556
557   EVT SrcTy = TLI->getValueType(Src);
558   EVT DstTy = TLI->getValueType(Dst);
559
560   // The function getSimpleVT only handles simple value types.
561   if (!SrcTy.isSimple() || !DstTy.isSimple())
562     return TargetTransformInfo::getCastInstrCost(Opcode, Dst, Src);
563
564   static const TypeConversionCostTblEntry<MVT::SimpleValueType>
565   AVX2ConversionTbl[] = {
566     { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8,  1 },
567     { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8,  1 },
568     { ISD::SIGN_EXTEND, MVT::v8i32,  MVT::v8i1,   3 },
569     { ISD::ZERO_EXTEND, MVT::v8i32,  MVT::v8i1,   3 },
570     { ISD::SIGN_EXTEND, MVT::v8i32,  MVT::v8i8,   3 },
571     { ISD::ZERO_EXTEND, MVT::v8i32,  MVT::v8i8,   3 },
572     { ISD::SIGN_EXTEND, MVT::v8i32,  MVT::v8i16,  1 },
573     { ISD::ZERO_EXTEND, MVT::v8i32,  MVT::v8i16,  1 },
574     { ISD::SIGN_EXTEND, MVT::v4i64,  MVT::v4i1,   3 },
575     { ISD::ZERO_EXTEND, MVT::v4i64,  MVT::v4i1,   3 },
576     { ISD::SIGN_EXTEND, MVT::v4i64,  MVT::v4i8,   3 },
577     { ISD::ZERO_EXTEND, MVT::v4i64,  MVT::v4i8,   3 },
578     { ISD::SIGN_EXTEND, MVT::v4i64,  MVT::v4i16,  3 },
579     { ISD::ZERO_EXTEND, MVT::v4i64,  MVT::v4i16,  3 },
580     { ISD::SIGN_EXTEND, MVT::v4i64,  MVT::v4i32,  1 },
581     { ISD::ZERO_EXTEND, MVT::v4i64,  MVT::v4i32,  1 },
582
583     { ISD::TRUNCATE,    MVT::v4i8,   MVT::v4i64,  2 },
584     { ISD::TRUNCATE,    MVT::v4i16,  MVT::v4i64,  2 },
585     { ISD::TRUNCATE,    MVT::v4i32,  MVT::v4i64,  2 },
586     { ISD::TRUNCATE,    MVT::v8i8,   MVT::v8i32,  2 },
587     { ISD::TRUNCATE,    MVT::v8i16,  MVT::v8i32,  2 },
588     { ISD::TRUNCATE,    MVT::v8i32,  MVT::v8i64,  4 },
589   };
590
591   static const TypeConversionCostTblEntry<MVT::SimpleValueType>
592   AVXConversionTbl[] = {
593     { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 4 },
594     { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 4 },
595     { ISD::SIGN_EXTEND, MVT::v8i32,  MVT::v8i1,  7 },
596     { ISD::ZERO_EXTEND, MVT::v8i32,  MVT::v8i1,  4 },
597     { ISD::SIGN_EXTEND, MVT::v8i32,  MVT::v8i8,  7 },
598     { ISD::ZERO_EXTEND, MVT::v8i32,  MVT::v8i8,  4 },
599     { ISD::SIGN_EXTEND, MVT::v8i32,  MVT::v8i16, 4 },
600     { ISD::ZERO_EXTEND, MVT::v8i32,  MVT::v8i16, 4 },
601     { ISD::SIGN_EXTEND, MVT::v4i64,  MVT::v4i1,  6 },
602     { ISD::ZERO_EXTEND, MVT::v4i64,  MVT::v4i1,  4 },
603     { ISD::SIGN_EXTEND, MVT::v4i64,  MVT::v4i8,  6 },
604     { ISD::ZERO_EXTEND, MVT::v4i64,  MVT::v4i8,  4 },
605     { ISD::SIGN_EXTEND, MVT::v4i64,  MVT::v4i16, 6 },
606     { ISD::ZERO_EXTEND, MVT::v4i64,  MVT::v4i16, 3 },
607     { ISD::SIGN_EXTEND, MVT::v4i64,  MVT::v4i32, 4 },
608     { ISD::ZERO_EXTEND, MVT::v4i64,  MVT::v4i32, 4 },
609
610     { ISD::TRUNCATE,    MVT::v4i8,  MVT::v4i64,  4 },
611     { ISD::TRUNCATE,    MVT::v4i16, MVT::v4i64,  4 },
612     { ISD::TRUNCATE,    MVT::v4i32, MVT::v4i64,  4 },
613     { ISD::TRUNCATE,    MVT::v8i8,  MVT::v8i32,  4 },
614     { ISD::TRUNCATE,    MVT::v8i16, MVT::v8i32,  5 },
615     { ISD::TRUNCATE,    MVT::v16i8, MVT::v16i16, 4 },
616     { ISD::TRUNCATE,    MVT::v8i32, MVT::v8i64,  9 },
617
618     { ISD::SINT_TO_FP,  MVT::v8f32, MVT::v8i1,  8 },
619     { ISD::SINT_TO_FP,  MVT::v8f32, MVT::v8i8,  8 },
620     { ISD::SINT_TO_FP,  MVT::v8f32, MVT::v8i16, 5 },
621     { ISD::SINT_TO_FP,  MVT::v8f32, MVT::v8i32, 1 },
622     { ISD::SINT_TO_FP,  MVT::v4f32, MVT::v4i1,  3 },
623     { ISD::SINT_TO_FP,  MVT::v4f32, MVT::v4i8,  3 },
624     { ISD::SINT_TO_FP,  MVT::v4f32, MVT::v4i16, 3 },
625     { ISD::SINT_TO_FP,  MVT::v4f32, MVT::v4i32, 1 },
626     { ISD::SINT_TO_FP,  MVT::v4f64, MVT::v4i1,  3 },
627     { ISD::SINT_TO_FP,  MVT::v4f64, MVT::v4i8,  3 },
628     { ISD::SINT_TO_FP,  MVT::v4f64, MVT::v4i16, 3 },
629     { ISD::SINT_TO_FP,  MVT::v4f64, MVT::v4i32, 1 },
630
631     { ISD::UINT_TO_FP,  MVT::v8f32, MVT::v8i1,  6 },
632     { ISD::UINT_TO_FP,  MVT::v8f32, MVT::v8i8,  5 },
633     { ISD::UINT_TO_FP,  MVT::v8f32, MVT::v8i16, 5 },
634     { ISD::UINT_TO_FP,  MVT::v8f32, MVT::v8i32, 9 },
635     { ISD::UINT_TO_FP,  MVT::v4f32, MVT::v4i1,  7 },
636     { ISD::UINT_TO_FP,  MVT::v4f32, MVT::v4i8,  2 },
637     { ISD::UINT_TO_FP,  MVT::v4f32, MVT::v4i16, 2 },
638     { ISD::UINT_TO_FP,  MVT::v4f32, MVT::v4i32, 6 },
639     { ISD::UINT_TO_FP,  MVT::v4f64, MVT::v4i1,  7 },
640     { ISD::UINT_TO_FP,  MVT::v4f64, MVT::v4i8,  2 },
641     { ISD::UINT_TO_FP,  MVT::v4f64, MVT::v4i16, 2 },
642     { ISD::UINT_TO_FP,  MVT::v4f64, MVT::v4i32, 6 },
643     // The generic code to compute the scalar overhead is currently broken.
644     // Workaround this limitation by estimating the scalarization overhead
645     // here. We have roughly 10 instructions per scalar element.
646     // Multiply that by the vector width.
647     // FIXME: remove that when PR19268 is fixed.
648     { ISD::UINT_TO_FP,  MVT::v2f64, MVT::v2i64, 2*10 },
649     { ISD::UINT_TO_FP,  MVT::v4f64, MVT::v4i64, 4*10 },
650
651     { ISD::FP_TO_SINT,  MVT::v8i8,  MVT::v8f32, 7 },
652     { ISD::FP_TO_SINT,  MVT::v4i8,  MVT::v4f32, 1 },
653     // This node is expanded into scalarized operations but BasicTTI is overly
654     // optimistic estimating its cost.  It computes 3 per element (one
655     // vector-extract, one scalar conversion and one vector-insert).  The
656     // problem is that the inserts form a read-modify-write chain so latency
657     // should be factored in too.  Inflating the cost per element by 1.
658     { ISD::FP_TO_UINT,  MVT::v8i32, MVT::v8f32, 8*4 },
659     { ISD::FP_TO_UINT,  MVT::v4i32, MVT::v4f64, 4*4 },
660   };
661
662   if (ST->hasAVX2()) {
663     int Idx = ConvertCostTableLookup(AVX2ConversionTbl, ISD,
664                                      DstTy.getSimpleVT(), SrcTy.getSimpleVT());
665     if (Idx != -1)
666       return AVX2ConversionTbl[Idx].Cost;
667   }
668
669   if (ST->hasAVX()) {
670     int Idx = ConvertCostTableLookup(AVXConversionTbl, ISD, DstTy.getSimpleVT(),
671                                      SrcTy.getSimpleVT());
672     if (Idx != -1)
673       return AVXConversionTbl[Idx].Cost;
674   }
675
676   return TargetTransformInfo::getCastInstrCost(Opcode, Dst, Src);
677 }
678
679 unsigned X86TTI::getCmpSelInstrCost(unsigned Opcode, Type *ValTy,
680                                     Type *CondTy) const {
681   // Legalize the type.
682   std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
683
684   MVT MTy = LT.second;
685
686   int ISD = TLI->InstructionOpcodeToISD(Opcode);
687   assert(ISD && "Invalid opcode");
688
689   static const CostTblEntry<MVT::SimpleValueType> SSE42CostTbl[] = {
690     { ISD::SETCC,   MVT::v2f64,   1 },
691     { ISD::SETCC,   MVT::v4f32,   1 },
692     { ISD::SETCC,   MVT::v2i64,   1 },
693     { ISD::SETCC,   MVT::v4i32,   1 },
694     { ISD::SETCC,   MVT::v8i16,   1 },
695     { ISD::SETCC,   MVT::v16i8,   1 },
696   };
697
698   static const CostTblEntry<MVT::SimpleValueType> AVX1CostTbl[] = {
699     { ISD::SETCC,   MVT::v4f64,   1 },
700     { ISD::SETCC,   MVT::v8f32,   1 },
701     // AVX1 does not support 8-wide integer compare.
702     { ISD::SETCC,   MVT::v4i64,   4 },
703     { ISD::SETCC,   MVT::v8i32,   4 },
704     { ISD::SETCC,   MVT::v16i16,  4 },
705     { ISD::SETCC,   MVT::v32i8,   4 },
706   };
707
708   static const CostTblEntry<MVT::SimpleValueType> AVX2CostTbl[] = {
709     { ISD::SETCC,   MVT::v4i64,   1 },
710     { ISD::SETCC,   MVT::v8i32,   1 },
711     { ISD::SETCC,   MVT::v16i16,  1 },
712     { ISD::SETCC,   MVT::v32i8,   1 },
713   };
714
715   if (ST->hasAVX2()) {
716     int Idx = CostTableLookup(AVX2CostTbl, ISD, MTy);
717     if (Idx != -1)
718       return LT.first * AVX2CostTbl[Idx].Cost;
719   }
720
721   if (ST->hasAVX()) {
722     int Idx = CostTableLookup(AVX1CostTbl, ISD, MTy);
723     if (Idx != -1)
724       return LT.first * AVX1CostTbl[Idx].Cost;
725   }
726
727   if (ST->hasSSE42()) {
728     int Idx = CostTableLookup(SSE42CostTbl, ISD, MTy);
729     if (Idx != -1)
730       return LT.first * SSE42CostTbl[Idx].Cost;
731   }
732
733   return TargetTransformInfo::getCmpSelInstrCost(Opcode, ValTy, CondTy);
734 }
735
736 unsigned X86TTI::getVectorInstrCost(unsigned Opcode, Type *Val,
737                                     unsigned Index) const {
738   assert(Val->isVectorTy() && "This must be a vector type");
739
740   if (Index != -1U) {
741     // Legalize the type.
742     std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Val);
743
744     // This type is legalized to a scalar type.
745     if (!LT.second.isVector())
746       return 0;
747
748     // The type may be split. Normalize the index to the new type.
749     unsigned Width = LT.second.getVectorNumElements();
750     Index = Index % Width;
751
752     // Floating point scalars are already located in index #0.
753     if (Val->getScalarType()->isFloatingPointTy() && Index == 0)
754       return 0;
755   }
756
757   return TargetTransformInfo::getVectorInstrCost(Opcode, Val, Index);
758 }
759
760 unsigned X86TTI::getScalarizationOverhead(Type *Ty, bool Insert,
761                                             bool Extract) const {
762   assert (Ty->isVectorTy() && "Can only scalarize vectors");
763   unsigned Cost = 0;
764
765   for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
766     if (Insert)
767       Cost += TopTTI->getVectorInstrCost(Instruction::InsertElement, Ty, i);
768     if (Extract)
769       Cost += TopTTI->getVectorInstrCost(Instruction::ExtractElement, Ty, i);
770   }
771
772   return Cost;
773 }
774
775 unsigned X86TTI::getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
776                                  unsigned AddressSpace) const {
777   // Handle non-power-of-two vectors such as <3 x float>
778   if (VectorType *VTy = dyn_cast<VectorType>(Src)) {
779     unsigned NumElem = VTy->getVectorNumElements();
780
781     // Handle a few common cases:
782     // <3 x float>
783     if (NumElem == 3 && VTy->getScalarSizeInBits() == 32)
784       // Cost = 64 bit store + extract + 32 bit store.
785       return 3;
786
787     // <3 x double>
788     if (NumElem == 3 && VTy->getScalarSizeInBits() == 64)
789       // Cost = 128 bit store + unpack + 64 bit store.
790       return 3;
791
792     // Assume that all other non-power-of-two numbers are scalarized.
793     if (!isPowerOf2_32(NumElem)) {
794       unsigned Cost = TargetTransformInfo::getMemoryOpCost(Opcode,
795                                                            VTy->getScalarType(),
796                                                            Alignment,
797                                                            AddressSpace);
798       unsigned SplitCost = getScalarizationOverhead(Src,
799                                                     Opcode == Instruction::Load,
800                                                     Opcode==Instruction::Store);
801       return NumElem * Cost + SplitCost;
802     }
803   }
804
805   // Legalize the type.
806   std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(Src);
807   assert((Opcode == Instruction::Load || Opcode == Instruction::Store) &&
808          "Invalid Opcode");
809
810   // Each load/store unit costs 1.
811   unsigned Cost = LT.first * 1;
812
813   // On Sandybridge 256bit load/stores are double pumped
814   // (but not on Haswell).
815   if (LT.second.getSizeInBits() > 128 && !ST->hasAVX2())
816     Cost*=2;
817
818   return Cost;
819 }
820
821 unsigned X86TTI::getAddressComputationCost(Type *Ty, bool IsComplex) const {
822   // Address computations in vectorized code with non-consecutive addresses will
823   // likely result in more instructions compared to scalar code where the
824   // computation can more often be merged into the index mode. The resulting
825   // extra micro-ops can significantly decrease throughput.
826   unsigned NumVectorInstToHideOverhead = 10;
827
828   if (Ty->isVectorTy() && IsComplex)
829     return NumVectorInstToHideOverhead;
830
831   return TargetTransformInfo::getAddressComputationCost(Ty, IsComplex);
832 }
833
834 unsigned X86TTI::getReductionCost(unsigned Opcode, Type *ValTy,
835                                   bool IsPairwise) const {
836   
837   std::pair<unsigned, MVT> LT = TLI->getTypeLegalizationCost(ValTy);
838   
839   MVT MTy = LT.second;
840   
841   int ISD = TLI->InstructionOpcodeToISD(Opcode);
842   assert(ISD && "Invalid opcode");
843  
844   // We use the Intel Architecture Code Analyzer(IACA) to measure the throughput 
845   // and make it as the cost. 
846  
847   static const CostTblEntry<MVT::SimpleValueType> SSE42CostTblPairWise[] = {
848     { ISD::FADD,  MVT::v2f64,   2 },
849     { ISD::FADD,  MVT::v4f32,   4 },
850     { ISD::ADD,   MVT::v2i64,   2 },      // The data reported by the IACA tool is "1.6".
851     { ISD::ADD,   MVT::v4i32,   3 },      // The data reported by the IACA tool is "3.5".
852     { ISD::ADD,   MVT::v8i16,   5 },
853   };
854  
855   static const CostTblEntry<MVT::SimpleValueType> AVX1CostTblPairWise[] = {
856     { ISD::FADD,  MVT::v4f32,   4 },
857     { ISD::FADD,  MVT::v4f64,   5 },
858     { ISD::FADD,  MVT::v8f32,   7 },
859     { ISD::ADD,   MVT::v2i64,   1 },      // The data reported by the IACA tool is "1.5".
860     { ISD::ADD,   MVT::v4i32,   3 },      // The data reported by the IACA tool is "3.5".
861     { ISD::ADD,   MVT::v4i64,   5 },      // The data reported by the IACA tool is "4.8".
862     { ISD::ADD,   MVT::v8i16,   5 },
863     { ISD::ADD,   MVT::v8i32,   5 },
864   };
865
866   static const CostTblEntry<MVT::SimpleValueType> SSE42CostTblNoPairWise[] = {
867     { ISD::FADD,  MVT::v2f64,   2 },
868     { ISD::FADD,  MVT::v4f32,   4 },
869     { ISD::ADD,   MVT::v2i64,   2 },      // The data reported by the IACA tool is "1.6".
870     { ISD::ADD,   MVT::v4i32,   3 },      // The data reported by the IACA tool is "3.3".
871     { ISD::ADD,   MVT::v8i16,   4 },      // The data reported by the IACA tool is "4.3".
872   };
873   
874   static const CostTblEntry<MVT::SimpleValueType> AVX1CostTblNoPairWise[] = {
875     { ISD::FADD,  MVT::v4f32,   3 },
876     { ISD::FADD,  MVT::v4f64,   3 },
877     { ISD::FADD,  MVT::v8f32,   4 },
878     { ISD::ADD,   MVT::v2i64,   1 },      // The data reported by the IACA tool is "1.5".
879     { ISD::ADD,   MVT::v4i32,   3 },      // The data reported by the IACA tool is "2.8".
880     { ISD::ADD,   MVT::v4i64,   3 },
881     { ISD::ADD,   MVT::v8i16,   4 },
882     { ISD::ADD,   MVT::v8i32,   5 },
883   };
884   
885   if (IsPairwise) {
886     if (ST->hasAVX()) {
887       int Idx = CostTableLookup(AVX1CostTblPairWise, ISD, MTy);
888       if (Idx != -1)
889         return LT.first * AVX1CostTblPairWise[Idx].Cost;
890     }
891   
892     if (ST->hasSSE42()) {
893       int Idx = CostTableLookup(SSE42CostTblPairWise, ISD, MTy);
894       if (Idx != -1)
895         return LT.first * SSE42CostTblPairWise[Idx].Cost;
896     }
897   } else {
898     if (ST->hasAVX()) {
899       int Idx = CostTableLookup(AVX1CostTblNoPairWise, ISD, MTy);
900       if (Idx != -1)
901         return LT.first * AVX1CostTblNoPairWise[Idx].Cost;
902     }
903     
904     if (ST->hasSSE42()) {
905       int Idx = CostTableLookup(SSE42CostTblNoPairWise, ISD, MTy);
906       if (Idx != -1)
907         return LT.first * SSE42CostTblNoPairWise[Idx].Cost;
908     }
909   }
910
911   return TargetTransformInfo::getReductionCost(Opcode, ValTy, IsPairwise);
912 }
913
914 unsigned X86TTI::getIntImmCost(const APInt &Imm, Type *Ty) const {
915   assert(Ty->isIntegerTy());
916
917   unsigned BitSize = Ty->getPrimitiveSizeInBits();
918   if (BitSize == 0)
919     return ~0U;
920
921   if (Imm == 0)
922     return TCC_Free;
923
924   if (Imm.getBitWidth() <= 64 &&
925       (isInt<32>(Imm.getSExtValue()) || isUInt<32>(Imm.getZExtValue())))
926     return TCC_Basic;
927   else
928     return 2 * TCC_Basic;
929 }
930
931 unsigned X86TTI::getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
932                                Type *Ty) const {
933   assert(Ty->isIntegerTy());
934
935   unsigned BitSize = Ty->getPrimitiveSizeInBits();
936   if (BitSize == 0)
937     return ~0U;
938
939   unsigned ImmIdx = ~0U;
940   switch (Opcode) {
941   default: return TCC_Free;
942   case Instruction::GetElementPtr:
943     // Always hoist the base address of a GetElementPtr. This prevents the
944     // creation of new constants for every base constant that gets constant
945     // folded with the offset.
946     if (Idx == 0)
947       return 2 * TCC_Basic;
948     return TCC_Free;
949   case Instruction::Store:
950     ImmIdx = 0;
951     break;
952   case Instruction::Add:
953   case Instruction::Sub:
954   case Instruction::Mul:
955   case Instruction::UDiv:
956   case Instruction::SDiv:
957   case Instruction::URem:
958   case Instruction::SRem:
959   case Instruction::Shl:
960   case Instruction::LShr:
961   case Instruction::AShr:
962   case Instruction::And:
963   case Instruction::Or:
964   case Instruction::Xor:
965   case Instruction::ICmp:
966     ImmIdx = 1;
967     break;
968   case Instruction::Trunc:
969   case Instruction::ZExt:
970   case Instruction::SExt:
971   case Instruction::IntToPtr:
972   case Instruction::PtrToInt:
973   case Instruction::BitCast:
974   case Instruction::PHI:
975   case Instruction::Call:
976   case Instruction::Select:
977   case Instruction::Ret:
978   case Instruction::Load:
979     break;
980   }
981
982   if ((Idx == ImmIdx) &&
983       Imm.getBitWidth() <= 64 && isInt<32>(Imm.getSExtValue()))
984     return TCC_Free;
985
986   return X86TTI::getIntImmCost(Imm, Ty);
987 }
988
989 unsigned X86TTI::getIntImmCost(Intrinsic::ID IID, unsigned Idx,
990                                const APInt &Imm, Type *Ty) const {
991   assert(Ty->isIntegerTy());
992
993   unsigned BitSize = Ty->getPrimitiveSizeInBits();
994   if (BitSize == 0)
995     return ~0U;
996
997   switch (IID) {
998   default: return TCC_Free;
999   case Intrinsic::sadd_with_overflow:
1000   case Intrinsic::uadd_with_overflow:
1001   case Intrinsic::ssub_with_overflow:
1002   case Intrinsic::usub_with_overflow:
1003   case Intrinsic::smul_with_overflow:
1004   case Intrinsic::umul_with_overflow:
1005     if ((Idx == 1) && Imm.getBitWidth() <= 64 && isInt<32>(Imm.getSExtValue()))
1006       return TCC_Free;
1007     break;
1008   case Intrinsic::experimental_stackmap:
1009     if ((Idx < 2) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
1010       return TCC_Free;
1011     break;
1012   case Intrinsic::experimental_patchpoint_void:
1013   case Intrinsic::experimental_patchpoint_i64:
1014     if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
1015       return TCC_Free;
1016     break;
1017   }
1018   return X86TTI::getIntImmCost(Imm, Ty);
1019 }