blob: 32e0f986695dd899442b2b228f9c20ff73a12a1b [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 Carruth93dcdc42015-01-31 11:17:59 +000017#include "X86TargetTransformInfo.h"
Chandler Carruthd3e73552013-01-07 03:08:10 +000018#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruth705b1852015-01-31 03:43:40 +000019#include "llvm/CodeGen/BasicTTIImpl.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"
Hans Wennborg083ca9b2015-10-06 23:24:35 +000024
Chandler Carruth664e3542013-01-07 01:37:14 +000025using namespace llvm;
26
Chandler Carruth84e68b22014-04-22 02:41:26 +000027#define DEBUG_TYPE "x86tti"
28
Chandler Carruth664e3542013-01-07 01:37:14 +000029//===----------------------------------------------------------------------===//
30//
31// X86 cost model.
32//
33//===----------------------------------------------------------------------===//
34
Chandler Carruth705b1852015-01-31 03:43:40 +000035TargetTransformInfo::PopcntSupportKind
36X86TTIImpl::getPopcntSupport(unsigned TyWidth) {
Chandler Carruth664e3542013-01-07 01:37:14 +000037 assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
38 // TODO: Currently the __builtin_popcount() implementation using SSE3
39 // instructions is inefficient. Once the problem is fixed, we should
Craig Topper0a63e1d2013-09-08 00:47:31 +000040 // call ST->hasSSE3() instead of ST->hasPOPCNT().
Chandler Carruth705b1852015-01-31 03:43:40 +000041 return ST->hasPOPCNT() ? TTI::PSK_FastHardware : TTI::PSK_Software;
Chandler Carruth664e3542013-01-07 01:37:14 +000042}
43
Chandler Carruth705b1852015-01-31 03:43:40 +000044unsigned X86TTIImpl::getNumberOfRegisters(bool Vector) {
Nadav Rotemb1791a72013-01-09 22:29:00 +000045 if (Vector && !ST->hasSSE1())
46 return 0;
47
Adam Nemet2820a5b2014-07-09 18:22:33 +000048 if (ST->is64Bit()) {
49 if (Vector && ST->hasAVX512())
50 return 32;
Chandler Carruth664e3542013-01-07 01:37:14 +000051 return 16;
Adam Nemet2820a5b2014-07-09 18:22:33 +000052 }
Chandler Carruth664e3542013-01-07 01:37:14 +000053 return 8;
54}
55
Chandler Carruth705b1852015-01-31 03:43:40 +000056unsigned X86TTIImpl::getRegisterBitWidth(bool Vector) {
Nadav Rotemb1791a72013-01-09 22:29:00 +000057 if (Vector) {
Adam Nemet2820a5b2014-07-09 18:22:33 +000058 if (ST->hasAVX512()) return 512;
Nadav Rotemb1791a72013-01-09 22:29:00 +000059 if (ST->hasAVX()) return 256;
60 if (ST->hasSSE1()) return 128;
61 return 0;
62 }
63
64 if (ST->is64Bit())
65 return 64;
Nadav Rotemb1791a72013-01-09 22:29:00 +000066
Hans Wennborg083ca9b2015-10-06 23:24:35 +000067 return 32;
Nadav Rotemb1791a72013-01-09 22:29:00 +000068}
69
Wei Mi062c7442015-05-06 17:12:25 +000070unsigned X86TTIImpl::getMaxInterleaveFactor(unsigned VF) {
71 // If the loop will not be vectorized, don't interleave the loop.
72 // Let regular unroll to unroll the loop, which saves the overflow
73 // check and memory check cost.
74 if (VF == 1)
75 return 1;
76
Nadav Rotemb696c362013-01-09 01:15:42 +000077 if (ST->isAtom())
78 return 1;
79
80 // Sandybridge and Haswell have multiple execution ports and pipelined
81 // vector units.
82 if (ST->hasAVX())
83 return 4;
84
85 return 2;
86}
87
Chandler Carruth93205eb2015-08-05 18:08:10 +000088int X86TTIImpl::getArithmeticInstrCost(
Chandler Carruth705b1852015-01-31 03:43:40 +000089 unsigned Opcode, Type *Ty, TTI::OperandValueKind Op1Info,
90 TTI::OperandValueKind Op2Info, TTI::OperandValueProperties Opd1PropInfo,
91 TTI::OperandValueProperties Opd2PropInfo) {
Chandler Carruth664e3542013-01-07 01:37:14 +000092 // Legalize the type.
Chandler Carruth93205eb2015-08-05 18:08:10 +000093 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty);
Chandler Carruth664e3542013-01-07 01:37:14 +000094
95 int ISD = TLI->InstructionOpcodeToISD(Opcode);
96 assert(ISD && "Invalid opcode");
97
Karthik Bhat7f33ff72014-08-25 04:56:54 +000098 if (ISD == ISD::SDIV &&
99 Op2Info == TargetTransformInfo::OK_UniformConstantValue &&
100 Opd2PropInfo == TargetTransformInfo::OP_PowerOf2) {
101 // On X86, vector signed division by constants power-of-two are
102 // normally expanded to the sequence SRA + SRL + ADD + SRA.
103 // The OperandValue properties many not be same as that of previous
104 // operation;conservatively assume OP_None.
Chandler Carruth93205eb2015-08-05 18:08:10 +0000105 int Cost = 2 * getArithmeticInstrCost(Instruction::AShr, Ty, Op1Info,
106 Op2Info, TargetTransformInfo::OP_None,
107 TargetTransformInfo::OP_None);
Karthik Bhat7f33ff72014-08-25 04:56:54 +0000108 Cost += getArithmeticInstrCost(Instruction::LShr, Ty, Op1Info, Op2Info,
109 TargetTransformInfo::OP_None,
110 TargetTransformInfo::OP_None);
111 Cost += getArithmeticInstrCost(Instruction::Add, Ty, Op1Info, Op2Info,
112 TargetTransformInfo::OP_None,
113 TargetTransformInfo::OP_None);
114
115 return Cost;
116 }
117
Benjamin Kramer7c372272014-04-26 14:53:05 +0000118 static const CostTblEntry<MVT::SimpleValueType>
119 AVX2UniformConstCostTable[] = {
Simon Pilgrim8fbf1c12015-07-06 22:35:19 +0000120 { ISD::SRA, MVT::v4i64, 4 }, // 2 x psrad + shuffle.
121
Benjamin Kramer7c372272014-04-26 14:53:05 +0000122 { ISD::SDIV, MVT::v16i16, 6 }, // vpmulhw sequence
123 { ISD::UDIV, MVT::v16i16, 6 }, // vpmulhuw sequence
124 { ISD::SDIV, MVT::v8i32, 15 }, // vpmuldq sequence
125 { ISD::UDIV, MVT::v8i32, 15 }, // vpmuludq sequence
126 };
127
128 if (Op2Info == TargetTransformInfo::OK_UniformConstantValue &&
129 ST->hasAVX2()) {
130 int Idx = CostTableLookup(AVX2UniformConstCostTable, ISD, LT.second);
131 if (Idx != -1)
132 return LT.first * AVX2UniformConstCostTable[Idx].Cost;
133 }
134
Elena Demikhovsky27012472014-09-16 07:57:37 +0000135 static const CostTblEntry<MVT::SimpleValueType> AVX512CostTable[] = {
136 { ISD::SHL, MVT::v16i32, 1 },
137 { ISD::SRL, MVT::v16i32, 1 },
138 { ISD::SRA, MVT::v16i32, 1 },
139 { ISD::SHL, MVT::v8i64, 1 },
140 { ISD::SRL, MVT::v8i64, 1 },
141 { ISD::SRA, MVT::v8i64, 1 },
142 };
143
Simon Pilgrim3d11c992015-09-30 08:17:50 +0000144 if (ST->hasAVX512()) {
145 int Idx = CostTableLookup(AVX512CostTable, ISD, LT.second);
146 if (Idx != -1)
147 return LT.first * AVX512CostTable[Idx].Cost;
148 }
149
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000150 static const CostTblEntry<MVT::SimpleValueType> AVX2CostTable[] = {
Michael Liao70dd7f92013-03-20 22:01:10 +0000151 // Shifts on v4i64/v8i32 on AVX2 is legal even though we declare to
152 // customize them to detect the cases where shift amount is a scalar one.
153 { ISD::SHL, MVT::v4i32, 1 },
154 { ISD::SRL, MVT::v4i32, 1 },
155 { ISD::SRA, MVT::v4i32, 1 },
156 { ISD::SHL, MVT::v8i32, 1 },
157 { ISD::SRL, MVT::v8i32, 1 },
158 { ISD::SRA, MVT::v8i32, 1 },
159 { ISD::SHL, MVT::v2i64, 1 },
160 { ISD::SRL, MVT::v2i64, 1 },
161 { ISD::SHL, MVT::v4i64, 1 },
162 { ISD::SRL, MVT::v4i64, 1 },
Simon Pilgrim3d11c992015-09-30 08:17:50 +0000163 };
Arnold Schwaighofere9b50162013-04-03 21:46:05 +0000164
Simon Pilgrim3d11c992015-09-30 08:17:50 +0000165 // Look for AVX2 lowering tricks.
166 if (ST->hasAVX2()) {
167 if (ISD == ISD::SHL && LT.second == MVT::v16i16 &&
168 (Op2Info == TargetTransformInfo::OK_UniformConstantValue ||
169 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue))
170 // On AVX2, a packed v16i16 shift left by a constant build_vector
171 // is lowered into a vector multiply (vpmullw).
172 return LT.first;
173
174 int Idx = CostTableLookup(AVX2CostTable, ISD, LT.second);
175 if (Idx != -1)
176 return LT.first * AVX2CostTable[Idx].Cost;
177 }
178
179 static const CostTblEntry<MVT::SimpleValueType> XOPCostTable[] = {
180 // 128bit shifts take 1cy, but right shifts require negation beforehand.
181 { ISD::SHL, MVT::v16i8, 1 },
182 { ISD::SRL, MVT::v16i8, 2 },
183 { ISD::SRA, MVT::v16i8, 2 },
184 { ISD::SHL, MVT::v8i16, 1 },
185 { ISD::SRL, MVT::v8i16, 2 },
186 { ISD::SRA, MVT::v8i16, 2 },
187 { ISD::SHL, MVT::v4i32, 1 },
188 { ISD::SRL, MVT::v4i32, 2 },
189 { ISD::SRA, MVT::v4i32, 2 },
190 { ISD::SHL, MVT::v2i64, 1 },
191 { ISD::SRL, MVT::v2i64, 2 },
192 { ISD::SRA, MVT::v2i64, 2 },
193 // 256bit shifts require splitting if AVX2 didn't catch them above.
194 { ISD::SHL, MVT::v32i8, 2 },
195 { ISD::SRL, MVT::v32i8, 4 },
196 { ISD::SRA, MVT::v32i8, 4 },
197 { ISD::SHL, MVT::v16i16, 2 },
198 { ISD::SRL, MVT::v16i16, 4 },
199 { ISD::SRA, MVT::v16i16, 4 },
200 { ISD::SHL, MVT::v8i32, 2 },
201 { ISD::SRL, MVT::v8i32, 4 },
202 { ISD::SRA, MVT::v8i32, 4 },
203 { ISD::SHL, MVT::v4i64, 2 },
204 { ISD::SRL, MVT::v4i64, 4 },
205 { ISD::SRA, MVT::v4i64, 4 },
206 };
207
208 // Look for XOP lowering tricks.
209 if (ST->hasXOP()) {
210 int Idx = CostTableLookup(XOPCostTable, ISD, LT.second);
211 if (Idx != -1)
212 return LT.first * XOPCostTable[Idx].Cost;
213 }
214
215 static const CostTblEntry<MVT::SimpleValueType> AVX2CustomCostTable[] = {
Simon Pilgrim59656802015-06-11 07:46:37 +0000216 { ISD::SHL, MVT::v32i8, 11 }, // vpblendvb sequence.
Simon Pilgrim0be4fa72015-05-25 17:49:13 +0000217 { ISD::SHL, MVT::v16i16, 10 }, // extend/vpsrlvd/pack sequence.
Arnold Schwaighofere9b50162013-04-03 21:46:05 +0000218
Simon Pilgrim59656802015-06-11 07:46:37 +0000219 { ISD::SRL, MVT::v32i8, 11 }, // vpblendvb sequence.
Simon Pilgrim0be4fa72015-05-25 17:49:13 +0000220 { ISD::SRL, MVT::v16i16, 10 }, // extend/vpsrlvd/pack sequence.
Arnold Schwaighofere9b50162013-04-03 21:46:05 +0000221
Simon Pilgrim59656802015-06-11 07:46:37 +0000222 { ISD::SRA, MVT::v32i8, 24 }, // vpblendvb sequence.
Simon Pilgrim0be4fa72015-05-25 17:49:13 +0000223 { ISD::SRA, MVT::v16i16, 10 }, // extend/vpsravd/pack sequence.
Simon Pilgrim86478c62015-07-29 20:31:45 +0000224 { ISD::SRA, MVT::v2i64, 4 }, // srl/xor/sub sequence.
225 { ISD::SRA, MVT::v4i64, 4 }, // srl/xor/sub sequence.
Arnold Schwaighofera04b9ef2013-06-25 19:14:09 +0000226
227 // Vectorizing division is a bad idea. See the SSE2 table for more comments.
228 { ISD::SDIV, MVT::v32i8, 32*20 },
229 { ISD::SDIV, MVT::v16i16, 16*20 },
230 { ISD::SDIV, MVT::v8i32, 8*20 },
231 { ISD::SDIV, MVT::v4i64, 4*20 },
232 { ISD::UDIV, MVT::v32i8, 32*20 },
233 { ISD::UDIV, MVT::v16i16, 16*20 },
234 { ISD::UDIV, MVT::v8i32, 8*20 },
235 { ISD::UDIV, MVT::v4i64, 4*20 },
Michael Liao70dd7f92013-03-20 22:01:10 +0000236 };
237
Simon Pilgrim3d11c992015-09-30 08:17:50 +0000238 // Look for AVX2 lowering tricks for custom cases.
Michael Liao70dd7f92013-03-20 22:01:10 +0000239 if (ST->hasAVX2()) {
Simon Pilgrim3d11c992015-09-30 08:17:50 +0000240 int Idx = CostTableLookup(AVX2CustomCostTable, ISD, LT.second);
Michael Liao70dd7f92013-03-20 22:01:10 +0000241 if (Idx != -1)
Simon Pilgrim3d11c992015-09-30 08:17:50 +0000242 return LT.first * AVX2CustomCostTable[Idx].Cost;
Michael Liao70dd7f92013-03-20 22:01:10 +0000243 }
244
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000245 static const CostTblEntry<MVT::SimpleValueType>
246 SSE2UniformConstCostTable[] = {
Arnold Schwaighofer44f902e2013-04-04 23:26:24 +0000247 // We don't correctly identify costs of casts because they are marked as
248 // custom.
249 // Constant splats are cheaper for the following instructions.
250 { ISD::SHL, MVT::v16i8, 1 }, // psllw.
251 { ISD::SHL, MVT::v8i16, 1 }, // psllw.
252 { ISD::SHL, MVT::v4i32, 1 }, // pslld
253 { ISD::SHL, MVT::v2i64, 1 }, // psllq.
254
255 { ISD::SRL, MVT::v16i8, 1 }, // psrlw.
256 { ISD::SRL, MVT::v8i16, 1 }, // psrlw.
257 { ISD::SRL, MVT::v4i32, 1 }, // psrld.
258 { ISD::SRL, MVT::v2i64, 1 }, // psrlq.
259
260 { ISD::SRA, MVT::v16i8, 4 }, // psrlw, pand, pxor, psubb.
261 { ISD::SRA, MVT::v8i16, 1 }, // psraw.
262 { ISD::SRA, MVT::v4i32, 1 }, // psrad.
Simon Pilgrim8fbf1c12015-07-06 22:35:19 +0000263 { ISD::SRA, MVT::v2i64, 4 }, // 2 x psrad + shuffle.
Benjamin Kramer7c372272014-04-26 14:53:05 +0000264
265 { ISD::SDIV, MVT::v8i16, 6 }, // pmulhw sequence
266 { ISD::UDIV, MVT::v8i16, 6 }, // pmulhuw sequence
Benjamin Kramerce4b3fe2014-04-27 18:47:54 +0000267 { ISD::SDIV, MVT::v4i32, 19 }, // pmuludq sequence
Benjamin Kramer7c372272014-04-26 14:53:05 +0000268 { ISD::UDIV, MVT::v4i32, 15 }, // pmuludq sequence
Arnold Schwaighofer44f902e2013-04-04 23:26:24 +0000269 };
270
271 if (Op2Info == TargetTransformInfo::OK_UniformConstantValue &&
272 ST->hasSSE2()) {
Benjamin Kramerce4b3fe2014-04-27 18:47:54 +0000273 // pmuldq sequence.
274 if (ISD == ISD::SDIV && LT.second == MVT::v4i32 && ST->hasSSE41())
275 return LT.first * 15;
276
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000277 int Idx = CostTableLookup(SSE2UniformConstCostTable, ISD, LT.second);
Arnold Schwaighofer44f902e2013-04-04 23:26:24 +0000278 if (Idx != -1)
279 return LT.first * SSE2UniformConstCostTable[Idx].Cost;
280 }
281
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000282 if (ISD == ISD::SHL &&
283 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue) {
284 EVT VT = LT.second;
285 if ((VT == MVT::v8i16 && ST->hasSSE2()) ||
286 (VT == MVT::v4i32 && ST->hasSSE41()))
287 // Vector shift left by non uniform constant can be lowered
288 // into vector multiply (pmullw/pmulld).
289 return LT.first;
290 if (VT == MVT::v4i32 && ST->hasSSE2())
291 // A vector shift left by non uniform constant is converted
292 // into a vector multiply; the new multiply is eventually
293 // lowered into a sequence of shuffles and 2 x pmuludq.
294 ISD = ISD::MUL;
295 }
Arnold Schwaighofer44f902e2013-04-04 23:26:24 +0000296
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000297 static const CostTblEntry<MVT::SimpleValueType> SSE2CostTable[] = {
Arnold Schwaighofere9b50162013-04-03 21:46:05 +0000298 // We don't correctly identify costs of casts because they are marked as
299 // custom.
300 // For some cases, where the shift amount is a scalar we would be able
301 // to generate better code. Unfortunately, when this is the case the value
302 // (the splat) will get hoisted out of the loop, thereby making it invisible
303 // to ISel. The cost model must return worst case assumptions because it is
304 // used for vectorization and we don't want to make vectorized code worse
305 // than scalar code.
Simon Pilgrim59656802015-06-11 07:46:37 +0000306 { ISD::SHL, MVT::v16i8, 26 }, // cmpgtb sequence.
307 { ISD::SHL, MVT::v8i16, 32 }, // cmpgtb sequence.
308 { ISD::SHL, MVT::v4i32, 2*5 }, // We optimized this using mul.
Simon Pilgrim59764dc2015-07-18 20:06:30 +0000309 { ISD::SHL, MVT::v2i64, 4 }, // splat+shuffle sequence.
310 { ISD::SHL, MVT::v4i64, 8 }, // splat+shuffle sequence.
NAKAMURA Takumi0b305db2015-07-14 04:03:49 +0000311
312 { ISD::SRL, MVT::v16i8, 26 }, // cmpgtb sequence.
313 { ISD::SRL, MVT::v8i16, 32 }, // cmpgtb sequence.
314 { ISD::SRL, MVT::v4i32, 16 }, // Shift each lane + blend.
Simon Pilgrim59764dc2015-07-18 20:06:30 +0000315 { ISD::SRL, MVT::v2i64, 4 }, // splat+shuffle sequence.
NAKAMURA Takumi0b305db2015-07-14 04:03:49 +0000316
317 { ISD::SRA, MVT::v16i8, 54 }, // unpacked cmpgtb sequence.
318 { ISD::SRA, MVT::v8i16, 32 }, // cmpgtb sequence.
319 { ISD::SRA, MVT::v4i32, 16 }, // Shift each lane + blend.
Simon Pilgrim86478c62015-07-29 20:31:45 +0000320 { ISD::SRA, MVT::v2i64, 12 }, // srl/xor/sub sequence.
NAKAMURA Takumi0b305db2015-07-14 04:03:49 +0000321
322 // It is not a good idea to vectorize division. We have to scalarize it and
Arnold Schwaighofera04b9ef2013-06-25 19:14:09 +0000323 // in the process we will often end up having to spilling regular
324 // registers. The overhead of division is going to dominate most kernels
325 // anyways so try hard to prevent vectorization of division - it is
326 // generally a bad idea. Assume somewhat arbitrarily that we have to be able
327 // to hide "20 cycles" for each lane.
328 { ISD::SDIV, MVT::v16i8, 16*20 },
329 { ISD::SDIV, MVT::v8i16, 8*20 },
330 { ISD::SDIV, MVT::v4i32, 4*20 },
331 { ISD::SDIV, MVT::v2i64, 2*20 },
332 { ISD::UDIV, MVT::v16i8, 16*20 },
333 { ISD::UDIV, MVT::v8i16, 8*20 },
334 { ISD::UDIV, MVT::v4i32, 4*20 },
335 { ISD::UDIV, MVT::v2i64, 2*20 },
Arnold Schwaighofere9b50162013-04-03 21:46:05 +0000336 };
337
338 if (ST->hasSSE2()) {
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000339 int Idx = CostTableLookup(SSE2CostTable, ISD, LT.second);
Arnold Schwaighofere9b50162013-04-03 21:46:05 +0000340 if (Idx != -1)
341 return LT.first * SSE2CostTable[Idx].Cost;
342 }
343
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000344 static const CostTblEntry<MVT::SimpleValueType> AVX1CostTable[] = {
Renato Goline1fb0592013-01-20 20:57:20 +0000345 // We don't have to scalarize unsupported ops. We can issue two half-sized
346 // operations and we only need to extract the upper YMM half.
347 // Two ops + 1 extract + 1 insert = 4.
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000348 { ISD::MUL, MVT::v16i16, 4 },
Renato Goline1fb0592013-01-20 20:57:20 +0000349 { ISD::MUL, MVT::v8i32, 4 },
350 { ISD::SUB, MVT::v8i32, 4 },
351 { ISD::ADD, MVT::v8i32, 4 },
Renato Goline1fb0592013-01-20 20:57:20 +0000352 { ISD::SUB, MVT::v4i64, 4 },
353 { ISD::ADD, MVT::v4i64, 4 },
Arnold Schwaighofer20ef54f2013-03-02 04:02:52 +0000354 // A v4i64 multiply is custom lowered as two split v2i64 vectors that then
355 // are lowered as a series of long multiplies(3), shifts(4) and adds(2)
356 // Because we believe v4i64 to be a legal type, we must also include the
357 // split factor of two in the cost table. Therefore, the cost here is 18
358 // instead of 9.
359 { ISD::MUL, MVT::v4i64, 18 },
360 };
Chandler Carruth664e3542013-01-07 01:37:14 +0000361
362 // Look for AVX1 lowering tricks.
Arnold Schwaighofer20ef54f2013-03-02 04:02:52 +0000363 if (ST->hasAVX() && !ST->hasAVX2()) {
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000364 EVT VT = LT.second;
365
366 // v16i16 and v8i32 shifts by non-uniform constants are lowered into a
367 // sequence of extract + two vector multiply + insert.
368 if (ISD == ISD::SHL && (VT == MVT::v8i32 || VT == MVT::v16i16) &&
369 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue)
370 ISD = ISD::MUL;
371
372 int Idx = CostTableLookup(AVX1CostTable, ISD, VT);
Renato Goline1fb0592013-01-20 20:57:20 +0000373 if (Idx != -1)
374 return LT.first * AVX1CostTable[Idx].Cost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000375 }
Arnold Schwaighofer20ef54f2013-03-02 04:02:52 +0000376
377 // Custom lowering of vectors.
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000378 static const CostTblEntry<MVT::SimpleValueType> CustomLowered[] = {
Arnold Schwaighofer20ef54f2013-03-02 04:02:52 +0000379 // A v2i64/v4i64 and multiply is custom lowered as a series of long
380 // multiplies(3), shifts(4) and adds(2).
381 { ISD::MUL, MVT::v2i64, 9 },
382 { ISD::MUL, MVT::v4i64, 9 },
383 };
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000384 int Idx = CostTableLookup(CustomLowered, ISD, LT.second);
Arnold Schwaighofer20ef54f2013-03-02 04:02:52 +0000385 if (Idx != -1)
386 return LT.first * CustomLowered[Idx].Cost;
387
388 // Special lowering of v4i32 mul on sse2, sse3: Lower v4i32 mul as 2x shuffle,
389 // 2x pmuludq, 2x shuffle.
390 if (ISD == ISD::MUL && LT.second == MVT::v4i32 && ST->hasSSE2() &&
391 !ST->hasSSE41())
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000392 return LT.first * 6;
Arnold Schwaighofer20ef54f2013-03-02 04:02:52 +0000393
Chandler Carruth664e3542013-01-07 01:37:14 +0000394 // Fallback to the default implementation.
Chandler Carruth705b1852015-01-31 03:43:40 +0000395 return BaseT::getArithmeticInstrCost(Opcode, Ty, Op1Info, Op2Info);
Chandler Carruth664e3542013-01-07 01:37:14 +0000396}
397
Chandler Carruth93205eb2015-08-05 18:08:10 +0000398int X86TTIImpl::getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index,
399 Type *SubTp) {
Karthik Bhate03a25d2014-06-20 04:32:48 +0000400 // We only estimate the cost of reverse and alternate shuffles.
Chandler Carruth705b1852015-01-31 03:43:40 +0000401 if (Kind != TTI::SK_Reverse && Kind != TTI::SK_Alternate)
402 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
Chandler Carruth664e3542013-01-07 01:37:14 +0000403
Chandler Carruth705b1852015-01-31 03:43:40 +0000404 if (Kind == TTI::SK_Reverse) {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000405 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp);
406 int Cost = 1;
Karthik Bhate03a25d2014-06-20 04:32:48 +0000407 if (LT.second.getSizeInBits() > 128)
408 Cost = 3; // Extract + insert + copy.
Chandler Carruth664e3542013-01-07 01:37:14 +0000409
Karthik Bhate03a25d2014-06-20 04:32:48 +0000410 // Multiple by the number of parts.
411 return Cost * LT.first;
412 }
413
Chandler Carruth705b1852015-01-31 03:43:40 +0000414 if (Kind == TTI::SK_Alternate) {
Andrea Di Biagioc8e8bda2014-07-03 22:24:18 +0000415 // 64-bit packed float vectors (v2f32) are widened to type v4f32.
416 // 64-bit packed integer vectors (v2i32) are promoted to type v2i64.
Chandler Carruth93205eb2015-08-05 18:08:10 +0000417 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp);
Karthik Bhate03a25d2014-06-20 04:32:48 +0000418
Andrea Di Biagioc8e8bda2014-07-03 22:24:18 +0000419 // The backend knows how to generate a single VEX.256 version of
420 // instruction VPBLENDW if the target supports AVX2.
421 if (ST->hasAVX2() && LT.second == MVT::v16i16)
422 return LT.first;
423
424 static const CostTblEntry<MVT::SimpleValueType> AVXAltShuffleTbl[] = {
425 {ISD::VECTOR_SHUFFLE, MVT::v4i64, 1}, // vblendpd
426 {ISD::VECTOR_SHUFFLE, MVT::v4f64, 1}, // vblendpd
427
428 {ISD::VECTOR_SHUFFLE, MVT::v8i32, 1}, // vblendps
429 {ISD::VECTOR_SHUFFLE, MVT::v8f32, 1}, // vblendps
430
431 // This shuffle is custom lowered into a sequence of:
432 // 2x vextractf128 , 2x vpblendw , 1x vinsertf128
433 {ISD::VECTOR_SHUFFLE, MVT::v16i16, 5},
434
435 // This shuffle is custom lowered into a long sequence of:
436 // 2x vextractf128 , 4x vpshufb , 2x vpor , 1x vinsertf128
437 {ISD::VECTOR_SHUFFLE, MVT::v32i8, 9}
438 };
439
440 if (ST->hasAVX()) {
441 int Idx = CostTableLookup(AVXAltShuffleTbl, ISD::VECTOR_SHUFFLE, LT.second);
442 if (Idx != -1)
443 return LT.first * AVXAltShuffleTbl[Idx].Cost;
444 }
445
446 static const CostTblEntry<MVT::SimpleValueType> SSE41AltShuffleTbl[] = {
447 // These are lowered into movsd.
448 {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1},
449 {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1},
450
451 // packed float vectors with four elements are lowered into BLENDI dag
452 // nodes. A v4i32/v4f32 BLENDI generates a single 'blendps'/'blendpd'.
453 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 1},
454 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 1},
455
456 // This shuffle generates a single pshufw.
457 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 1},
458
459 // There is no instruction that matches a v16i8 alternate shuffle.
460 // The backend will expand it into the sequence 'pshufb + pshufb + or'.
461 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 3}
462 };
463
464 if (ST->hasSSE41()) {
465 int Idx = CostTableLookup(SSE41AltShuffleTbl, ISD::VECTOR_SHUFFLE, LT.second);
466 if (Idx != -1)
467 return LT.first * SSE41AltShuffleTbl[Idx].Cost;
468 }
469
470 static const CostTblEntry<MVT::SimpleValueType> SSSE3AltShuffleTbl[] = {
471 {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1}, // movsd
472 {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1}, // movsd
473
474 // SSE3 doesn't have 'blendps'. The following shuffles are expanded into
475 // the sequence 'shufps + pshufd'
476 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 2},
477 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 2},
478
479 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 3}, // pshufb + pshufb + or
480 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 3} // pshufb + pshufb + or
481 };
Michael Liao5bf95782014-12-04 05:20:33 +0000482
Andrea Di Biagioc8e8bda2014-07-03 22:24:18 +0000483 if (ST->hasSSSE3()) {
484 int Idx = CostTableLookup(SSSE3AltShuffleTbl, ISD::VECTOR_SHUFFLE, LT.second);
485 if (Idx != -1)
486 return LT.first * SSSE3AltShuffleTbl[Idx].Cost;
487 }
488
489 static const CostTblEntry<MVT::SimpleValueType> SSEAltShuffleTbl[] = {
490 {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1}, // movsd
491 {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1}, // movsd
492
493 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 2}, // shufps + pshufd
494 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 2}, // shufps + pshufd
Michael Liao5bf95782014-12-04 05:20:33 +0000495
Andrea Di Biagioc8e8bda2014-07-03 22:24:18 +0000496 // This is expanded into a long sequence of four extract + four insert.
497 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 8}, // 4 x pextrw + 4 pinsrw.
498
499 // 8 x (pinsrw + pextrw + and + movb + movzb + or)
500 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 48}
501 };
502
Michael Liao5bf95782014-12-04 05:20:33 +0000503 // Fall-back (SSE3 and SSE2).
Andrea Di Biagioc8e8bda2014-07-03 22:24:18 +0000504 int Idx = CostTableLookup(SSEAltShuffleTbl, ISD::VECTOR_SHUFFLE, LT.second);
505 if (Idx != -1)
506 return LT.first * SSEAltShuffleTbl[Idx].Cost;
Chandler Carruth705b1852015-01-31 03:43:40 +0000507 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
Karthik Bhate03a25d2014-06-20 04:32:48 +0000508 }
509
Chandler Carruth705b1852015-01-31 03:43:40 +0000510 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
Chandler Carruth664e3542013-01-07 01:37:14 +0000511}
512
Chandler Carruth93205eb2015-08-05 18:08:10 +0000513int X86TTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) {
Chandler Carruth664e3542013-01-07 01:37:14 +0000514 int ISD = TLI->InstructionOpcodeToISD(Opcode);
515 assert(ISD && "Invalid opcode");
516
Elena Demikhovsky27012472014-09-16 07:57:37 +0000517 static const TypeConversionCostTblEntry<MVT::SimpleValueType>
518 AVX512ConversionTbl[] = {
519 { ISD::FP_EXTEND, MVT::v8f64, MVT::v8f32, 1 },
520 { ISD::FP_EXTEND, MVT::v8f64, MVT::v16f32, 3 },
521 { ISD::FP_ROUND, MVT::v8f32, MVT::v8f64, 1 },
522 { ISD::FP_ROUND, MVT::v16f32, MVT::v8f64, 3 },
523
524 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i32, 1 },
525 { ISD::TRUNCATE, MVT::v16i16, MVT::v16i32, 1 },
526 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i64, 1 },
527 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 1 },
528 { ISD::TRUNCATE, MVT::v16i32, MVT::v8i64, 4 },
529
530 // v16i1 -> v16i32 - load + broadcast
531 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i1, 2 },
532 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i1, 2 },
533
534 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i8, 1 },
535 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i8, 1 },
536 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i16, 1 },
537 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i16, 1 },
538 { ISD::SIGN_EXTEND, MVT::v8i64, MVT::v16i32, 3 },
539 { ISD::ZERO_EXTEND, MVT::v8i64, MVT::v16i32, 3 },
540
Elena Demikhovskyd5e95b52014-11-13 11:46:16 +0000541 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i1, 3 },
542 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i8, 2 },
543 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i16, 2 },
544 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i32, 1 },
545 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i1, 4 },
546 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i16, 2 },
547 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i32, 1 },
Elena Demikhovsky27012472014-09-16 07:57:37 +0000548 };
549
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000550 static const TypeConversionCostTblEntry<MVT::SimpleValueType>
Tim Northoverf0e21612014-02-06 18:18:36 +0000551 AVX2ConversionTbl[] = {
552 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 1 },
553 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 1 },
554 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 3 },
555 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 3 },
556 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 3 },
557 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 3 },
558 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
559 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
560 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i1, 3 },
561 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i1, 3 },
562 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 3 },
563 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 3 },
564 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
565 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
566 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
567 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
568
569 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i64, 2 },
570 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i64, 2 },
571 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 2 },
572 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 2 },
573 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 2 },
574 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 4 },
Elena Demikhovsky27012472014-09-16 07:57:37 +0000575
576 { ISD::FP_EXTEND, MVT::v8f64, MVT::v8f32, 3 },
577 { ISD::FP_ROUND, MVT::v8f32, MVT::v8f64, 3 },
Quentin Colombet360460b2014-11-11 02:23:47 +0000578
579 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 8 },
Tim Northoverf0e21612014-02-06 18:18:36 +0000580 };
581
582 static const TypeConversionCostTblEntry<MVT::SimpleValueType>
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000583 AVXConversionTbl[] = {
Tim Northoverf0e21612014-02-06 18:18:36 +0000584 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 4 },
585 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 4 },
586 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 7 },
587 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 4 },
588 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 7 },
589 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 4 },
590 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 4 },
591 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 4 },
592 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i1, 6 },
593 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i1, 4 },
594 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 6 },
595 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 4 },
596 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 6 },
597 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
598 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 4 },
599 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 4 },
600
601 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i64, 4 },
602 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i64, 4 },
603 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 4 },
604 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 4 },
605 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 5 },
606 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i16, 4 },
607 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 9 },
Benjamin Kramer52ceb442013-04-01 10:23:49 +0000608
609 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i1, 8 },
610 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i8, 8 },
611 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i16, 5 },
612 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i32, 1 },
613 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i1, 3 },
614 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i8, 3 },
615 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i16, 3 },
616 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 },
617 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i1, 3 },
618 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i8, 3 },
619 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i16, 3 },
620 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i32, 1 },
621
622 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i1, 6 },
623 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i8, 5 },
624 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i16, 5 },
625 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 9 },
626 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i1, 7 },
627 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i8, 2 },
628 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i16, 2 },
629 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 6 },
630 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i1, 7 },
631 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i8, 2 },
632 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i16, 2 },
633 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i32, 6 },
Quentin Colombet85b904d2014-03-27 22:27:41 +0000634 // The generic code to compute the scalar overhead is currently broken.
635 // Workaround this limitation by estimating the scalarization overhead
636 // here. We have roughly 10 instructions per scalar element.
637 // Multiply that by the vector width.
638 // FIXME: remove that when PR19268 is fixed.
Quentin Colombet3914bf52014-03-27 00:52:16 +0000639 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 },
640 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i64, 4*10 },
Benjamin Kramer52ceb442013-04-01 10:23:49 +0000641
Jim Grosbach72fbde82014-03-27 00:04:11 +0000642 { ISD::FP_TO_SINT, MVT::v8i8, MVT::v8f32, 7 },
Renato Goline1fb0592013-01-20 20:57:20 +0000643 { ISD::FP_TO_SINT, MVT::v4i8, MVT::v4f32, 1 },
Adam Nemet6dafe972014-03-30 18:07:13 +0000644 // This node is expanded into scalarized operations but BasicTTI is overly
645 // optimistic estimating its cost. It computes 3 per element (one
646 // vector-extract, one scalar conversion and one vector-insert). The
647 // problem is that the inserts form a read-modify-write chain so latency
648 // should be factored in too. Inflating the cost per element by 1.
649 { ISD::FP_TO_UINT, MVT::v8i32, MVT::v8f32, 8*4 },
Adam Nemet10c4ce22014-03-31 21:54:48 +0000650 { ISD::FP_TO_UINT, MVT::v4i32, MVT::v4f64, 4*4 },
Chandler Carruth664e3542013-01-07 01:37:14 +0000651 };
652
Simon Pilgrime2c244f2015-07-19 15:36:12 +0000653 static const TypeConversionCostTblEntry<MVT::SimpleValueType>
654 SSE2ConvTbl[] = {
655 // These are somewhat magic numbers justified by looking at the output of
656 // Intel's IACA, running some kernels and making sure when we take
657 // legalization into account the throughput will be overestimated.
658 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 },
659 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 },
660 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 },
661 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 },
662 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 },
663 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 },
664 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 },
665 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 },
666 // There are faster sequences for float conversions.
667 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 },
668 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 8 },
669 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 },
670 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 },
671 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 },
672 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 15 },
673 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 },
674 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 },
675 };
676
Chandler Carruth93205eb2015-08-05 18:08:10 +0000677 std::pair<int, MVT> LTSrc = TLI->getTypeLegalizationCost(DL, Src);
678 std::pair<int, MVT> LTDest = TLI->getTypeLegalizationCost(DL, Dst);
Simon Pilgrime2c244f2015-07-19 15:36:12 +0000679
680 if (ST->hasSSE2() && !ST->hasAVX()) {
681 int Idx =
682 ConvertCostTableLookup(SSE2ConvTbl, ISD, LTDest.second, LTSrc.second);
683 if (Idx != -1)
684 return LTSrc.first * SSE2ConvTbl[Idx].Cost;
685 }
686
687 if (ST->hasAVX512()) {
688 int Idx = ConvertCostTableLookup(AVX512ConversionTbl, ISD, LTDest.second,
689 LTSrc.second);
690 if (Idx != -1)
691 return AVX512ConversionTbl[Idx].Cost;
692 }
693
694 EVT SrcTy = TLI->getValueType(DL, Src);
695 EVT DstTy = TLI->getValueType(DL, Dst);
696
697 // The function getSimpleVT only handles simple value types.
698 if (!SrcTy.isSimple() || !DstTy.isSimple())
699 return BaseT::getCastInstrCost(Opcode, Dst, Src);
700
Tim Northoverf0e21612014-02-06 18:18:36 +0000701 if (ST->hasAVX2()) {
702 int Idx = ConvertCostTableLookup(AVX2ConversionTbl, ISD,
703 DstTy.getSimpleVT(), SrcTy.getSimpleVT());
704 if (Idx != -1)
705 return AVX2ConversionTbl[Idx].Cost;
706 }
707
Chandler Carruth664e3542013-01-07 01:37:14 +0000708 if (ST->hasAVX()) {
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000709 int Idx = ConvertCostTableLookup(AVXConversionTbl, ISD, DstTy.getSimpleVT(),
710 SrcTy.getSimpleVT());
Renato Goline1fb0592013-01-20 20:57:20 +0000711 if (Idx != -1)
712 return AVXConversionTbl[Idx].Cost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000713 }
714
Chandler Carruth705b1852015-01-31 03:43:40 +0000715 return BaseT::getCastInstrCost(Opcode, Dst, Src);
Chandler Carruth664e3542013-01-07 01:37:14 +0000716}
717
Chandler Carruth93205eb2015-08-05 18:08:10 +0000718int X86TTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy) {
Chandler Carruth664e3542013-01-07 01:37:14 +0000719 // Legalize the type.
Chandler Carruth93205eb2015-08-05 18:08:10 +0000720 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, ValTy);
Chandler Carruth664e3542013-01-07 01:37:14 +0000721
722 MVT MTy = LT.second;
723
724 int ISD = TLI->InstructionOpcodeToISD(Opcode);
725 assert(ISD && "Invalid opcode");
726
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000727 static const CostTblEntry<MVT::SimpleValueType> SSE42CostTbl[] = {
Renato Goline1fb0592013-01-20 20:57:20 +0000728 { ISD::SETCC, MVT::v2f64, 1 },
729 { ISD::SETCC, MVT::v4f32, 1 },
730 { ISD::SETCC, MVT::v2i64, 1 },
731 { ISD::SETCC, MVT::v4i32, 1 },
732 { ISD::SETCC, MVT::v8i16, 1 },
733 { ISD::SETCC, MVT::v16i8, 1 },
Chandler Carruth664e3542013-01-07 01:37:14 +0000734 };
735
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000736 static const CostTblEntry<MVT::SimpleValueType> AVX1CostTbl[] = {
Renato Goline1fb0592013-01-20 20:57:20 +0000737 { ISD::SETCC, MVT::v4f64, 1 },
738 { ISD::SETCC, MVT::v8f32, 1 },
Chandler Carruth664e3542013-01-07 01:37:14 +0000739 // AVX1 does not support 8-wide integer compare.
Renato Goline1fb0592013-01-20 20:57:20 +0000740 { ISD::SETCC, MVT::v4i64, 4 },
741 { ISD::SETCC, MVT::v8i32, 4 },
742 { ISD::SETCC, MVT::v16i16, 4 },
743 { ISD::SETCC, MVT::v32i8, 4 },
Chandler Carruth664e3542013-01-07 01:37:14 +0000744 };
745
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000746 static const CostTblEntry<MVT::SimpleValueType> AVX2CostTbl[] = {
Renato Goline1fb0592013-01-20 20:57:20 +0000747 { ISD::SETCC, MVT::v4i64, 1 },
748 { ISD::SETCC, MVT::v8i32, 1 },
749 { ISD::SETCC, MVT::v16i16, 1 },
750 { ISD::SETCC, MVT::v32i8, 1 },
Chandler Carruth664e3542013-01-07 01:37:14 +0000751 };
752
Elena Demikhovsky27012472014-09-16 07:57:37 +0000753 static const CostTblEntry<MVT::SimpleValueType> AVX512CostTbl[] = {
754 { ISD::SETCC, MVT::v8i64, 1 },
755 { ISD::SETCC, MVT::v16i32, 1 },
756 { ISD::SETCC, MVT::v8f64, 1 },
757 { ISD::SETCC, MVT::v16f32, 1 },
758 };
759
760 if (ST->hasAVX512()) {
761 int Idx = CostTableLookup(AVX512CostTbl, ISD, MTy);
762 if (Idx != -1)
763 return LT.first * AVX512CostTbl[Idx].Cost;
764 }
765
Chandler Carruth664e3542013-01-07 01:37:14 +0000766 if (ST->hasAVX2()) {
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000767 int Idx = CostTableLookup(AVX2CostTbl, ISD, MTy);
Renato Goline1fb0592013-01-20 20:57:20 +0000768 if (Idx != -1)
769 return LT.first * AVX2CostTbl[Idx].Cost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000770 }
771
772 if (ST->hasAVX()) {
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000773 int Idx = CostTableLookup(AVX1CostTbl, ISD, MTy);
Renato Goline1fb0592013-01-20 20:57:20 +0000774 if (Idx != -1)
775 return LT.first * AVX1CostTbl[Idx].Cost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000776 }
777
778 if (ST->hasSSE42()) {
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000779 int Idx = CostTableLookup(SSE42CostTbl, ISD, MTy);
Renato Goline1fb0592013-01-20 20:57:20 +0000780 if (Idx != -1)
781 return LT.first * SSE42CostTbl[Idx].Cost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000782 }
783
Chandler Carruth705b1852015-01-31 03:43:40 +0000784 return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy);
Chandler Carruth664e3542013-01-07 01:37:14 +0000785}
786
Chandler Carruth93205eb2015-08-05 18:08:10 +0000787int X86TTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index) {
Chandler Carruth664e3542013-01-07 01:37:14 +0000788 assert(Val->isVectorTy() && "This must be a vector type");
789
790 if (Index != -1U) {
791 // Legalize the type.
Chandler Carruth93205eb2015-08-05 18:08:10 +0000792 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Val);
Chandler Carruth664e3542013-01-07 01:37:14 +0000793
794 // This type is legalized to a scalar type.
795 if (!LT.second.isVector())
796 return 0;
797
798 // The type may be split. Normalize the index to the new type.
799 unsigned Width = LT.second.getVectorNumElements();
800 Index = Index % Width;
801
802 // Floating point scalars are already located in index #0.
803 if (Val->getScalarType()->isFloatingPointTy() && Index == 0)
804 return 0;
805 }
806
Chandler Carruth705b1852015-01-31 03:43:40 +0000807 return BaseT::getVectorInstrCost(Opcode, Val, Index);
Chandler Carruth664e3542013-01-07 01:37:14 +0000808}
809
Chandler Carruth93205eb2015-08-05 18:08:10 +0000810int X86TTIImpl::getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) {
Nadav Rotemf9ecbcb2013-06-27 17:52:04 +0000811 assert (Ty->isVectorTy() && "Can only scalarize vectors");
Chandler Carruth93205eb2015-08-05 18:08:10 +0000812 int Cost = 0;
Nadav Rotemf9ecbcb2013-06-27 17:52:04 +0000813
814 for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
815 if (Insert)
Chandler Carruth705b1852015-01-31 03:43:40 +0000816 Cost += getVectorInstrCost(Instruction::InsertElement, Ty, i);
Nadav Rotemf9ecbcb2013-06-27 17:52:04 +0000817 if (Extract)
Chandler Carruth705b1852015-01-31 03:43:40 +0000818 Cost += getVectorInstrCost(Instruction::ExtractElement, Ty, i);
Nadav Rotemf9ecbcb2013-06-27 17:52:04 +0000819 }
820
821 return Cost;
822}
823
Chandler Carruth93205eb2015-08-05 18:08:10 +0000824int X86TTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
825 unsigned AddressSpace) {
Alp Tokerf907b892013-12-05 05:44:44 +0000826 // Handle non-power-of-two vectors such as <3 x float>
Nadav Rotemf9ecbcb2013-06-27 17:52:04 +0000827 if (VectorType *VTy = dyn_cast<VectorType>(Src)) {
828 unsigned NumElem = VTy->getVectorNumElements();
829
830 // Handle a few common cases:
831 // <3 x float>
832 if (NumElem == 3 && VTy->getScalarSizeInBits() == 32)
833 // Cost = 64 bit store + extract + 32 bit store.
834 return 3;
835
836 // <3 x double>
837 if (NumElem == 3 && VTy->getScalarSizeInBits() == 64)
838 // Cost = 128 bit store + unpack + 64 bit store.
839 return 3;
840
Alp Tokerf907b892013-12-05 05:44:44 +0000841 // Assume that all other non-power-of-two numbers are scalarized.
Nadav Rotemf9ecbcb2013-06-27 17:52:04 +0000842 if (!isPowerOf2_32(NumElem)) {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000843 int Cost = BaseT::getMemoryOpCost(Opcode, VTy->getScalarType(), Alignment,
844 AddressSpace);
845 int SplitCost = getScalarizationOverhead(Src, Opcode == Instruction::Load,
846 Opcode == Instruction::Store);
Nadav Rotemf9ecbcb2013-06-27 17:52:04 +0000847 return NumElem * Cost + SplitCost;
848 }
849 }
850
Chandler Carruth664e3542013-01-07 01:37:14 +0000851 // Legalize the type.
Chandler Carruth93205eb2015-08-05 18:08:10 +0000852 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Src);
Chandler Carruth664e3542013-01-07 01:37:14 +0000853 assert((Opcode == Instruction::Load || Opcode == Instruction::Store) &&
854 "Invalid Opcode");
855
856 // Each load/store unit costs 1.
Chandler Carruth93205eb2015-08-05 18:08:10 +0000857 int Cost = LT.first * 1;
Chandler Carruth664e3542013-01-07 01:37:14 +0000858
859 // On Sandybridge 256bit load/stores are double pumped
860 // (but not on Haswell).
861 if (LT.second.getSizeInBits() > 128 && !ST->hasAVX2())
862 Cost*=2;
863
864 return Cost;
865}
Arnold Schwaighofer6042a262013-07-12 19:16:07 +0000866
Chandler Carruth93205eb2015-08-05 18:08:10 +0000867int X86TTIImpl::getMaskedMemoryOpCost(unsigned Opcode, Type *SrcTy,
868 unsigned Alignment,
869 unsigned AddressSpace) {
Elena Demikhovskya3232f72015-01-25 08:44:46 +0000870 VectorType *SrcVTy = dyn_cast<VectorType>(SrcTy);
871 if (!SrcVTy)
872 // To calculate scalar take the regular cost, without mask
873 return getMemoryOpCost(Opcode, SrcTy, Alignment, AddressSpace);
874
875 unsigned NumElem = SrcVTy->getVectorNumElements();
876 VectorType *MaskTy =
877 VectorType::get(Type::getInt8Ty(getGlobalContext()), NumElem);
878 if ((Opcode == Instruction::Load && !isLegalMaskedLoad(SrcVTy, 1)) ||
879 (Opcode == Instruction::Store && !isLegalMaskedStore(SrcVTy, 1)) ||
880 !isPowerOf2_32(NumElem)) {
881 // Scalarization
Chandler Carruth93205eb2015-08-05 18:08:10 +0000882 int MaskSplitCost = getScalarizationOverhead(MaskTy, false, true);
883 int ScalarCompareCost = getCmpSelInstrCost(
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000884 Instruction::ICmp, Type::getInt8Ty(getGlobalContext()), nullptr);
Chandler Carruth93205eb2015-08-05 18:08:10 +0000885 int BranchCost = getCFInstrCost(Instruction::Br);
886 int MaskCmpCost = NumElem * (BranchCost + ScalarCompareCost);
Elena Demikhovskya3232f72015-01-25 08:44:46 +0000887
Chandler Carruth93205eb2015-08-05 18:08:10 +0000888 int ValueSplitCost = getScalarizationOverhead(
889 SrcVTy, Opcode == Instruction::Load, Opcode == Instruction::Store);
890 int MemopCost =
Chandler Carruth705b1852015-01-31 03:43:40 +0000891 NumElem * BaseT::getMemoryOpCost(Opcode, SrcVTy->getScalarType(),
892 Alignment, AddressSpace);
Elena Demikhovskya3232f72015-01-25 08:44:46 +0000893 return MemopCost + ValueSplitCost + MaskSplitCost + MaskCmpCost;
894 }
895
896 // Legalize the type.
Chandler Carruth93205eb2015-08-05 18:08:10 +0000897 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, SrcVTy);
898 int Cost = 0;
Mehdi Amini44ede332015-07-09 02:09:04 +0000899 if (LT.second != TLI->getValueType(DL, SrcVTy).getSimpleVT() &&
Elena Demikhovskya3232f72015-01-25 08:44:46 +0000900 LT.second.getVectorNumElements() == NumElem)
901 // Promotion requires expand/truncate for data and a shuffle for mask.
Hans Wennborg083ca9b2015-10-06 23:24:35 +0000902 Cost += getShuffleCost(TTI::SK_Alternate, SrcVTy, 0, nullptr) +
903 getShuffleCost(TTI::SK_Alternate, MaskTy, 0, nullptr);
Chandler Carruth705b1852015-01-31 03:43:40 +0000904
Elena Demikhovskya3232f72015-01-25 08:44:46 +0000905 else if (LT.second.getVectorNumElements() > NumElem) {
906 VectorType *NewMaskTy = VectorType::get(MaskTy->getVectorElementType(),
907 LT.second.getVectorNumElements());
908 // Expanding requires fill mask with zeroes
Chandler Carruth705b1852015-01-31 03:43:40 +0000909 Cost += getShuffleCost(TTI::SK_InsertSubvector, NewMaskTy, 0, MaskTy);
Elena Demikhovskya3232f72015-01-25 08:44:46 +0000910 }
911 if (!ST->hasAVX512())
912 return Cost + LT.first*4; // Each maskmov costs 4
913
914 // AVX-512 masked load/store is cheapper
915 return Cost+LT.first;
916}
917
Chandler Carruth93205eb2015-08-05 18:08:10 +0000918int X86TTIImpl::getAddressComputationCost(Type *Ty, bool IsComplex) {
Arnold Schwaighofer6042a262013-07-12 19:16:07 +0000919 // Address computations in vectorized code with non-consecutive addresses will
920 // likely result in more instructions compared to scalar code where the
921 // computation can more often be merged into the index mode. The resulting
922 // extra micro-ops can significantly decrease throughput.
923 unsigned NumVectorInstToHideOverhead = 10;
924
925 if (Ty->isVectorTy() && IsComplex)
926 return NumVectorInstToHideOverhead;
927
Chandler Carruth705b1852015-01-31 03:43:40 +0000928 return BaseT::getAddressComputationCost(Ty, IsComplex);
Arnold Schwaighofer6042a262013-07-12 19:16:07 +0000929}
Yi Jiang5c343de2013-09-19 17:48:48 +0000930
Chandler Carruth93205eb2015-08-05 18:08:10 +0000931int X86TTIImpl::getReductionCost(unsigned Opcode, Type *ValTy,
932 bool IsPairwise) {
Michael Liao5bf95782014-12-04 05:20:33 +0000933
Chandler Carruth93205eb2015-08-05 18:08:10 +0000934 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, ValTy);
Michael Liao5bf95782014-12-04 05:20:33 +0000935
Yi Jiang5c343de2013-09-19 17:48:48 +0000936 MVT MTy = LT.second;
Michael Liao5bf95782014-12-04 05:20:33 +0000937
Yi Jiang5c343de2013-09-19 17:48:48 +0000938 int ISD = TLI->InstructionOpcodeToISD(Opcode);
939 assert(ISD && "Invalid opcode");
Michael Liao5bf95782014-12-04 05:20:33 +0000940
941 // We use the Intel Architecture Code Analyzer(IACA) to measure the throughput
942 // and make it as the cost.
943
Yi Jiang5c343de2013-09-19 17:48:48 +0000944 static const CostTblEntry<MVT::SimpleValueType> SSE42CostTblPairWise[] = {
945 { ISD::FADD, MVT::v2f64, 2 },
946 { ISD::FADD, MVT::v4f32, 4 },
947 { ISD::ADD, MVT::v2i64, 2 }, // The data reported by the IACA tool is "1.6".
948 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.5".
949 { ISD::ADD, MVT::v8i16, 5 },
950 };
Michael Liao5bf95782014-12-04 05:20:33 +0000951
Yi Jiang5c343de2013-09-19 17:48:48 +0000952 static const CostTblEntry<MVT::SimpleValueType> AVX1CostTblPairWise[] = {
953 { ISD::FADD, MVT::v4f32, 4 },
954 { ISD::FADD, MVT::v4f64, 5 },
955 { ISD::FADD, MVT::v8f32, 7 },
956 { ISD::ADD, MVT::v2i64, 1 }, // The data reported by the IACA tool is "1.5".
957 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.5".
958 { ISD::ADD, MVT::v4i64, 5 }, // The data reported by the IACA tool is "4.8".
959 { ISD::ADD, MVT::v8i16, 5 },
960 { ISD::ADD, MVT::v8i32, 5 },
961 };
962
963 static const CostTblEntry<MVT::SimpleValueType> SSE42CostTblNoPairWise[] = {
964 { ISD::FADD, MVT::v2f64, 2 },
965 { ISD::FADD, MVT::v4f32, 4 },
966 { ISD::ADD, MVT::v2i64, 2 }, // The data reported by the IACA tool is "1.6".
967 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.3".
968 { ISD::ADD, MVT::v8i16, 4 }, // The data reported by the IACA tool is "4.3".
969 };
Michael Liao5bf95782014-12-04 05:20:33 +0000970
Yi Jiang5c343de2013-09-19 17:48:48 +0000971 static const CostTblEntry<MVT::SimpleValueType> AVX1CostTblNoPairWise[] = {
972 { ISD::FADD, MVT::v4f32, 3 },
973 { ISD::FADD, MVT::v4f64, 3 },
974 { ISD::FADD, MVT::v8f32, 4 },
975 { ISD::ADD, MVT::v2i64, 1 }, // The data reported by the IACA tool is "1.5".
976 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "2.8".
977 { ISD::ADD, MVT::v4i64, 3 },
978 { ISD::ADD, MVT::v8i16, 4 },
979 { ISD::ADD, MVT::v8i32, 5 },
980 };
Michael Liao5bf95782014-12-04 05:20:33 +0000981
Yi Jiang5c343de2013-09-19 17:48:48 +0000982 if (IsPairwise) {
983 if (ST->hasAVX()) {
984 int Idx = CostTableLookup(AVX1CostTblPairWise, ISD, MTy);
985 if (Idx != -1)
986 return LT.first * AVX1CostTblPairWise[Idx].Cost;
987 }
Michael Liao5bf95782014-12-04 05:20:33 +0000988
Yi Jiang5c343de2013-09-19 17:48:48 +0000989 if (ST->hasSSE42()) {
990 int Idx = CostTableLookup(SSE42CostTblPairWise, ISD, MTy);
991 if (Idx != -1)
992 return LT.first * SSE42CostTblPairWise[Idx].Cost;
993 }
994 } else {
995 if (ST->hasAVX()) {
996 int Idx = CostTableLookup(AVX1CostTblNoPairWise, ISD, MTy);
997 if (Idx != -1)
998 return LT.first * AVX1CostTblNoPairWise[Idx].Cost;
999 }
Michael Liao5bf95782014-12-04 05:20:33 +00001000
Yi Jiang5c343de2013-09-19 17:48:48 +00001001 if (ST->hasSSE42()) {
1002 int Idx = CostTableLookup(SSE42CostTblNoPairWise, ISD, MTy);
1003 if (Idx != -1)
1004 return LT.first * SSE42CostTblNoPairWise[Idx].Cost;
1005 }
1006 }
1007
Chandler Carruth705b1852015-01-31 03:43:40 +00001008 return BaseT::getReductionCost(Opcode, ValTy, IsPairwise);
Yi Jiang5c343de2013-09-19 17:48:48 +00001009}
1010
Juergen Ributzkab2e4edb2014-06-10 00:32:29 +00001011/// \brief Calculate the cost of materializing a 64-bit value. This helper
1012/// method might only calculate a fraction of a larger immediate. Therefore it
1013/// is valid to return a cost of ZERO.
Chandler Carruth93205eb2015-08-05 18:08:10 +00001014int X86TTIImpl::getIntImmCost(int64_t Val) {
Juergen Ributzkab2e4edb2014-06-10 00:32:29 +00001015 if (Val == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +00001016 return TTI::TCC_Free;
Juergen Ributzkab2e4edb2014-06-10 00:32:29 +00001017
1018 if (isInt<32>(Val))
Chandler Carruth705b1852015-01-31 03:43:40 +00001019 return TTI::TCC_Basic;
Juergen Ributzkab2e4edb2014-06-10 00:32:29 +00001020
Chandler Carruth705b1852015-01-31 03:43:40 +00001021 return 2 * TTI::TCC_Basic;
Juergen Ributzkab2e4edb2014-06-10 00:32:29 +00001022}
1023
Chandler Carruth93205eb2015-08-05 18:08:10 +00001024int X86TTIImpl::getIntImmCost(const APInt &Imm, Type *Ty) {
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001025 assert(Ty->isIntegerTy());
1026
1027 unsigned BitSize = Ty->getPrimitiveSizeInBits();
1028 if (BitSize == 0)
1029 return ~0U;
1030
Juergen Ributzka43176172014-05-19 21:00:53 +00001031 // Never hoist constants larger than 128bit, because this might lead to
1032 // incorrect code generation or assertions in codegen.
1033 // Fixme: Create a cost model for types larger than i128 once the codegen
1034 // issues have been fixed.
1035 if (BitSize > 128)
Chandler Carruth705b1852015-01-31 03:43:40 +00001036 return TTI::TCC_Free;
Juergen Ributzka43176172014-05-19 21:00:53 +00001037
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001038 if (Imm == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +00001039 return TTI::TCC_Free;
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001040
Juergen Ributzkab2e4edb2014-06-10 00:32:29 +00001041 // Sign-extend all constants to a multiple of 64-bit.
1042 APInt ImmVal = Imm;
1043 if (BitSize & 0x3f)
1044 ImmVal = Imm.sext((BitSize + 63) & ~0x3fU);
1045
1046 // Split the constant into 64-bit chunks and calculate the cost for each
1047 // chunk.
Chandler Carruth93205eb2015-08-05 18:08:10 +00001048 int Cost = 0;
Juergen Ributzkab2e4edb2014-06-10 00:32:29 +00001049 for (unsigned ShiftVal = 0; ShiftVal < BitSize; ShiftVal += 64) {
1050 APInt Tmp = ImmVal.ashr(ShiftVal).sextOrTrunc(64);
1051 int64_t Val = Tmp.getSExtValue();
1052 Cost += getIntImmCost(Val);
1053 }
1054 // We need at least one instruction to materialze the constant.
Chandler Carruth93205eb2015-08-05 18:08:10 +00001055 return std::max(1, Cost);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001056}
1057
Chandler Carruth93205eb2015-08-05 18:08:10 +00001058int X86TTIImpl::getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
1059 Type *Ty) {
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001060 assert(Ty->isIntegerTy());
1061
1062 unsigned BitSize = Ty->getPrimitiveSizeInBits();
Juergen Ributzka43176172014-05-19 21:00:53 +00001063 // There is no cost model for constants with a bit size of 0. Return TCC_Free
1064 // here, so that constant hoisting will ignore this constant.
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001065 if (BitSize == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +00001066 return TTI::TCC_Free;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001067
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001068 unsigned ImmIdx = ~0U;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001069 switch (Opcode) {
Chandler Carruth705b1852015-01-31 03:43:40 +00001070 default:
1071 return TTI::TCC_Free;
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001072 case Instruction::GetElementPtr:
Juergen Ributzka27435b32014-04-02 21:45:36 +00001073 // Always hoist the base address of a GetElementPtr. This prevents the
1074 // creation of new constants for every base constant that gets constant
1075 // folded with the offset.
Juergen Ributzka631c4912014-03-25 18:01:25 +00001076 if (Idx == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +00001077 return 2 * TTI::TCC_Basic;
1078 return TTI::TCC_Free;
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001079 case Instruction::Store:
1080 ImmIdx = 0;
1081 break;
Craig Topper79dd1bf2015-10-06 02:50:24 +00001082 case Instruction::And:
1083 // We support 64-bit ANDs with immediates with 32-bits of leading zeroes
1084 // by using a 32-bit operation with implicit zero extension. Detect such
1085 // immediates here as the normal path expects bit 31 to be sign extended.
1086 if (Idx == 1 && Imm.getBitWidth() == 64 && isUInt<32>(Imm.getZExtValue()))
1087 return TTI::TCC_Free;
1088 // Fallthrough
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001089 case Instruction::Add:
1090 case Instruction::Sub:
1091 case Instruction::Mul:
1092 case Instruction::UDiv:
1093 case Instruction::SDiv:
1094 case Instruction::URem:
1095 case Instruction::SRem:
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001096 case Instruction::Or:
1097 case Instruction::Xor:
1098 case Instruction::ICmp:
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001099 ImmIdx = 1;
1100 break;
Michael Zolotukhin1f4a9602014-04-30 19:17:32 +00001101 // Always return TCC_Free for the shift value of a shift instruction.
1102 case Instruction::Shl:
1103 case Instruction::LShr:
1104 case Instruction::AShr:
1105 if (Idx == 1)
Chandler Carruth705b1852015-01-31 03:43:40 +00001106 return TTI::TCC_Free;
Michael Zolotukhin1f4a9602014-04-30 19:17:32 +00001107 break;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001108 case Instruction::Trunc:
1109 case Instruction::ZExt:
1110 case Instruction::SExt:
1111 case Instruction::IntToPtr:
1112 case Instruction::PtrToInt:
1113 case Instruction::BitCast:
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001114 case Instruction::PHI:
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001115 case Instruction::Call:
1116 case Instruction::Select:
1117 case Instruction::Ret:
1118 case Instruction::Load:
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001119 break;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001120 }
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001121
Juergen Ributzkab2e4edb2014-06-10 00:32:29 +00001122 if (Idx == ImmIdx) {
Chandler Carruth93205eb2015-08-05 18:08:10 +00001123 int NumConstants = (BitSize + 63) / 64;
1124 int Cost = X86TTIImpl::getIntImmCost(Imm, Ty);
Chandler Carruth705b1852015-01-31 03:43:40 +00001125 return (Cost <= NumConstants * TTI::TCC_Basic)
Chandler Carruth93205eb2015-08-05 18:08:10 +00001126 ? static_cast<int>(TTI::TCC_Free)
Chandler Carruth705b1852015-01-31 03:43:40 +00001127 : Cost;
Juergen Ributzkab2e4edb2014-06-10 00:32:29 +00001128 }
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001129
Chandler Carruth705b1852015-01-31 03:43:40 +00001130 return X86TTIImpl::getIntImmCost(Imm, Ty);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001131}
1132
Chandler Carruth93205eb2015-08-05 18:08:10 +00001133int X86TTIImpl::getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm,
1134 Type *Ty) {
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001135 assert(Ty->isIntegerTy());
1136
1137 unsigned BitSize = Ty->getPrimitiveSizeInBits();
Juergen Ributzka43176172014-05-19 21:00:53 +00001138 // There is no cost model for constants with a bit size of 0. Return TCC_Free
1139 // here, so that constant hoisting will ignore this constant.
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001140 if (BitSize == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +00001141 return TTI::TCC_Free;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001142
1143 switch (IID) {
Chandler Carruth705b1852015-01-31 03:43:40 +00001144 default:
1145 return TTI::TCC_Free;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001146 case Intrinsic::sadd_with_overflow:
1147 case Intrinsic::uadd_with_overflow:
1148 case Intrinsic::ssub_with_overflow:
1149 case Intrinsic::usub_with_overflow:
1150 case Intrinsic::smul_with_overflow:
1151 case Intrinsic::umul_with_overflow:
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001152 if ((Idx == 1) && Imm.getBitWidth() <= 64 && isInt<32>(Imm.getSExtValue()))
Chandler Carruth705b1852015-01-31 03:43:40 +00001153 return TTI::TCC_Free;
Juergen Ributzka5eef98c2014-03-25 18:01:23 +00001154 break;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001155 case Intrinsic::experimental_stackmap:
Juergen Ributzka5eef98c2014-03-25 18:01:23 +00001156 if ((Idx < 2) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
Chandler Carruth705b1852015-01-31 03:43:40 +00001157 return TTI::TCC_Free;
Juergen Ributzka5eef98c2014-03-25 18:01:23 +00001158 break;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001159 case Intrinsic::experimental_patchpoint_void:
1160 case Intrinsic::experimental_patchpoint_i64:
Juergen Ributzka5eef98c2014-03-25 18:01:23 +00001161 if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
Chandler Carruth705b1852015-01-31 03:43:40 +00001162 return TTI::TCC_Free;
Juergen Ributzka5eef98c2014-03-25 18:01:23 +00001163 break;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001164 }
Chandler Carruth705b1852015-01-31 03:43:40 +00001165 return X86TTIImpl::getIntImmCost(Imm, Ty);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001166}
NAKAMURA Takumi0b305db2015-07-14 04:03:49 +00001167
1168bool X86TTIImpl::isLegalMaskedLoad(Type *DataTy, int Consecutive) {
1169 int DataWidth = DataTy->getPrimitiveSizeInBits();
1170
1171 // Todo: AVX512 allows gather/scatter, works with strided and random as well
1172 if ((DataWidth < 32) || (Consecutive == 0))
1173 return false;
1174 if (ST->hasAVX512() || ST->hasAVX2())
1175 return true;
1176 return false;
1177}
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00001178
Chandler Carruth705b1852015-01-31 03:43:40 +00001179bool X86TTIImpl::isLegalMaskedStore(Type *DataType, int Consecutive) {
Elena Demikhovsky3fcafa22014-12-14 09:43:50 +00001180 return isLegalMaskedLoad(DataType, Consecutive);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00001181}
1182
Eric Christopherd566fb12015-07-29 22:09:48 +00001183bool X86TTIImpl::areInlineCompatible(const Function *Caller,
1184 const Function *Callee) const {
Eric Christophere1002262015-07-02 01:11:50 +00001185 const TargetMachine &TM = getTLI()->getTargetMachine();
1186
1187 // Work this as a subsetting of subtarget features.
1188 const FeatureBitset &CallerBits =
1189 TM.getSubtargetImpl(*Caller)->getFeatureBits();
1190 const FeatureBitset &CalleeBits =
1191 TM.getSubtargetImpl(*Callee)->getFeatureBits();
1192
1193 // FIXME: This is likely too limiting as it will include subtarget features
1194 // that we might not care about for inlining, but it is conservatively
1195 // correct.
1196 return (CallerBits & CalleeBits) == CalleeBits;
1197}