blob: 7f1db70d6146d34debe6af978a581ab14721ed7f [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"
Chandler Carruth664e3542013-01-07 01:37:14 +000024using namespace llvm;
25
Chandler Carruth84e68b22014-04-22 02:41:26 +000026#define DEBUG_TYPE "x86tti"
27
Chandler Carruth664e3542013-01-07 01:37:14 +000028//===----------------------------------------------------------------------===//
29//
30// X86 cost model.
31//
32//===----------------------------------------------------------------------===//
33
Chandler Carruth705b1852015-01-31 03:43:40 +000034TargetTransformInfo::PopcntSupportKind
35X86TTIImpl::getPopcntSupport(unsigned TyWidth) {
Chandler Carruth664e3542013-01-07 01:37:14 +000036 assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
37 // TODO: Currently the __builtin_popcount() implementation using SSE3
38 // instructions is inefficient. Once the problem is fixed, we should
Craig Topper0a63e1d2013-09-08 00:47:31 +000039 // call ST->hasSSE3() instead of ST->hasPOPCNT().
Chandler Carruth705b1852015-01-31 03:43:40 +000040 return ST->hasPOPCNT() ? TTI::PSK_FastHardware : TTI::PSK_Software;
Chandler Carruth664e3542013-01-07 01:37:14 +000041}
42
Chandler Carruth705b1852015-01-31 03:43:40 +000043unsigned X86TTIImpl::getNumberOfRegisters(bool Vector) {
Nadav Rotemb1791a72013-01-09 22:29:00 +000044 if (Vector && !ST->hasSSE1())
45 return 0;
46
Adam Nemet2820a5b2014-07-09 18:22:33 +000047 if (ST->is64Bit()) {
48 if (Vector && ST->hasAVX512())
49 return 32;
Chandler Carruth664e3542013-01-07 01:37:14 +000050 return 16;
Adam Nemet2820a5b2014-07-09 18:22:33 +000051 }
Chandler Carruth664e3542013-01-07 01:37:14 +000052 return 8;
53}
54
Chandler Carruth705b1852015-01-31 03:43:40 +000055unsigned X86TTIImpl::getRegisterBitWidth(bool Vector) {
Nadav Rotemb1791a72013-01-09 22:29:00 +000056 if (Vector) {
Adam Nemet2820a5b2014-07-09 18:22:33 +000057 if (ST->hasAVX512()) return 512;
Nadav Rotemb1791a72013-01-09 22:29:00 +000058 if (ST->hasAVX()) return 256;
59 if (ST->hasSSE1()) return 128;
60 return 0;
61 }
62
63 if (ST->is64Bit())
64 return 64;
65 return 32;
66
67}
68
Wei Mi062c7442015-05-06 17:12:25 +000069unsigned X86TTIImpl::getMaxInterleaveFactor(unsigned VF) {
70 // If the loop will not be vectorized, don't interleave the loop.
71 // Let regular unroll to unroll the loop, which saves the overflow
72 // check and memory check cost.
73 if (VF == 1)
74 return 1;
75
Nadav Rotemb696c362013-01-09 01:15:42 +000076 if (ST->isAtom())
77 return 1;
78
79 // Sandybridge and Haswell have multiple execution ports and pipelined
80 // vector units.
81 if (ST->hasAVX())
82 return 4;
83
84 return 2;
85}
86
Chandler Carruth93205eb2015-08-05 18:08:10 +000087int X86TTIImpl::getArithmeticInstrCost(
Chandler Carruth705b1852015-01-31 03:43:40 +000088 unsigned Opcode, Type *Ty, TTI::OperandValueKind Op1Info,
89 TTI::OperandValueKind Op2Info, TTI::OperandValueProperties Opd1PropInfo,
90 TTI::OperandValueProperties Opd2PropInfo) {
Chandler Carruth664e3542013-01-07 01:37:14 +000091 // Legalize the type.
Chandler Carruth93205eb2015-08-05 18:08:10 +000092 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty);
Chandler Carruth664e3542013-01-07 01:37:14 +000093
94 int ISD = TLI->InstructionOpcodeToISD(Opcode);
95 assert(ISD && "Invalid opcode");
96
Karthik Bhat7f33ff72014-08-25 04:56:54 +000097 if (ISD == ISD::SDIV &&
98 Op2Info == TargetTransformInfo::OK_UniformConstantValue &&
99 Opd2PropInfo == TargetTransformInfo::OP_PowerOf2) {
100 // On X86, vector signed division by constants power-of-two are
101 // normally expanded to the sequence SRA + SRL + ADD + SRA.
102 // The OperandValue properties many not be same as that of previous
103 // operation;conservatively assume OP_None.
Chandler Carruth93205eb2015-08-05 18:08:10 +0000104 int Cost = 2 * getArithmeticInstrCost(Instruction::AShr, Ty, Op1Info,
105 Op2Info, TargetTransformInfo::OP_None,
106 TargetTransformInfo::OP_None);
Karthik Bhat7f33ff72014-08-25 04:56:54 +0000107 Cost += getArithmeticInstrCost(Instruction::LShr, Ty, Op1Info, Op2Info,
108 TargetTransformInfo::OP_None,
109 TargetTransformInfo::OP_None);
110 Cost += getArithmeticInstrCost(Instruction::Add, Ty, Op1Info, Op2Info,
111 TargetTransformInfo::OP_None,
112 TargetTransformInfo::OP_None);
113
114 return Cost;
115 }
116
Benjamin Kramer7c372272014-04-26 14:53:05 +0000117 static const CostTblEntry<MVT::SimpleValueType>
118 AVX2UniformConstCostTable[] = {
Simon Pilgrim8fbf1c12015-07-06 22:35:19 +0000119 { ISD::SRA, MVT::v4i64, 4 }, // 2 x psrad + shuffle.
120
Benjamin Kramer7c372272014-04-26 14:53:05 +0000121 { ISD::SDIV, MVT::v16i16, 6 }, // vpmulhw sequence
122 { ISD::UDIV, MVT::v16i16, 6 }, // vpmulhuw sequence
123 { ISD::SDIV, MVT::v8i32, 15 }, // vpmuldq sequence
124 { ISD::UDIV, MVT::v8i32, 15 }, // vpmuludq sequence
125 };
126
127 if (Op2Info == TargetTransformInfo::OK_UniformConstantValue &&
128 ST->hasAVX2()) {
129 int Idx = CostTableLookup(AVX2UniformConstCostTable, ISD, LT.second);
130 if (Idx != -1)
131 return LT.first * AVX2UniformConstCostTable[Idx].Cost;
132 }
133
Elena Demikhovsky27012472014-09-16 07:57:37 +0000134 static const CostTblEntry<MVT::SimpleValueType> AVX512CostTable[] = {
135 { ISD::SHL, MVT::v16i32, 1 },
136 { ISD::SRL, MVT::v16i32, 1 },
137 { ISD::SRA, MVT::v16i32, 1 },
138 { ISD::SHL, MVT::v8i64, 1 },
139 { ISD::SRL, MVT::v8i64, 1 },
140 { ISD::SRA, MVT::v8i64, 1 },
141 };
142
Simon Pilgrim3d11c992015-09-30 08:17:50 +0000143 if (ST->hasAVX512()) {
144 int Idx = CostTableLookup(AVX512CostTable, ISD, LT.second);
145 if (Idx != -1)
146 return LT.first * AVX512CostTable[Idx].Cost;
147 }
148
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000149 static const CostTblEntry<MVT::SimpleValueType> AVX2CostTable[] = {
Michael Liao70dd7f92013-03-20 22:01:10 +0000150 // Shifts on v4i64/v8i32 on AVX2 is legal even though we declare to
151 // customize them to detect the cases where shift amount is a scalar one.
152 { ISD::SHL, MVT::v4i32, 1 },
153 { ISD::SRL, MVT::v4i32, 1 },
154 { ISD::SRA, MVT::v4i32, 1 },
155 { ISD::SHL, MVT::v8i32, 1 },
156 { ISD::SRL, MVT::v8i32, 1 },
157 { ISD::SRA, MVT::v8i32, 1 },
158 { ISD::SHL, MVT::v2i64, 1 },
159 { ISD::SRL, MVT::v2i64, 1 },
160 { ISD::SHL, MVT::v4i64, 1 },
161 { ISD::SRL, MVT::v4i64, 1 },
Simon Pilgrim3d11c992015-09-30 08:17:50 +0000162 };
Arnold Schwaighofere9b50162013-04-03 21:46:05 +0000163
Simon Pilgrim3d11c992015-09-30 08:17:50 +0000164 // Look for AVX2 lowering tricks.
165 if (ST->hasAVX2()) {
166 if (ISD == ISD::SHL && LT.second == MVT::v16i16 &&
167 (Op2Info == TargetTransformInfo::OK_UniformConstantValue ||
168 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue))
169 // On AVX2, a packed v16i16 shift left by a constant build_vector
170 // is lowered into a vector multiply (vpmullw).
171 return LT.first;
172
173 int Idx = CostTableLookup(AVX2CostTable, ISD, LT.second);
174 if (Idx != -1)
175 return LT.first * AVX2CostTable[Idx].Cost;
176 }
177
178 static const CostTblEntry<MVT::SimpleValueType> XOPCostTable[] = {
179 // 128bit shifts take 1cy, but right shifts require negation beforehand.
180 { ISD::SHL, MVT::v16i8, 1 },
181 { ISD::SRL, MVT::v16i8, 2 },
182 { ISD::SRA, MVT::v16i8, 2 },
183 { ISD::SHL, MVT::v8i16, 1 },
184 { ISD::SRL, MVT::v8i16, 2 },
185 { ISD::SRA, MVT::v8i16, 2 },
186 { ISD::SHL, MVT::v4i32, 1 },
187 { ISD::SRL, MVT::v4i32, 2 },
188 { ISD::SRA, MVT::v4i32, 2 },
189 { ISD::SHL, MVT::v2i64, 1 },
190 { ISD::SRL, MVT::v2i64, 2 },
191 { ISD::SRA, MVT::v2i64, 2 },
192 // 256bit shifts require splitting if AVX2 didn't catch them above.
193 { ISD::SHL, MVT::v32i8, 2 },
194 { ISD::SRL, MVT::v32i8, 4 },
195 { ISD::SRA, MVT::v32i8, 4 },
196 { ISD::SHL, MVT::v16i16, 2 },
197 { ISD::SRL, MVT::v16i16, 4 },
198 { ISD::SRA, MVT::v16i16, 4 },
199 { ISD::SHL, MVT::v8i32, 2 },
200 { ISD::SRL, MVT::v8i32, 4 },
201 { ISD::SRA, MVT::v8i32, 4 },
202 { ISD::SHL, MVT::v4i64, 2 },
203 { ISD::SRL, MVT::v4i64, 4 },
204 { ISD::SRA, MVT::v4i64, 4 },
205 };
206
207 // Look for XOP lowering tricks.
208 if (ST->hasXOP()) {
209 int Idx = CostTableLookup(XOPCostTable, ISD, LT.second);
210 if (Idx != -1)
211 return LT.first * XOPCostTable[Idx].Cost;
212 }
213
214 static const CostTblEntry<MVT::SimpleValueType> AVX2CustomCostTable[] = {
Simon Pilgrim59656802015-06-11 07:46:37 +0000215 { ISD::SHL, MVT::v32i8, 11 }, // vpblendvb sequence.
Simon Pilgrim0be4fa72015-05-25 17:49:13 +0000216 { ISD::SHL, MVT::v16i16, 10 }, // extend/vpsrlvd/pack sequence.
Arnold Schwaighofere9b50162013-04-03 21:46:05 +0000217
Simon Pilgrim59656802015-06-11 07:46:37 +0000218 { ISD::SRL, MVT::v32i8, 11 }, // vpblendvb sequence.
Simon Pilgrim0be4fa72015-05-25 17:49:13 +0000219 { ISD::SRL, MVT::v16i16, 10 }, // extend/vpsrlvd/pack sequence.
Arnold Schwaighofere9b50162013-04-03 21:46:05 +0000220
Simon Pilgrim59656802015-06-11 07:46:37 +0000221 { ISD::SRA, MVT::v32i8, 24 }, // vpblendvb sequence.
Simon Pilgrim0be4fa72015-05-25 17:49:13 +0000222 { ISD::SRA, MVT::v16i16, 10 }, // extend/vpsravd/pack sequence.
Simon Pilgrim86478c62015-07-29 20:31:45 +0000223 { ISD::SRA, MVT::v2i64, 4 }, // srl/xor/sub sequence.
224 { ISD::SRA, MVT::v4i64, 4 }, // srl/xor/sub sequence.
Arnold Schwaighofera04b9ef2013-06-25 19:14:09 +0000225
226 // Vectorizing division is a bad idea. See the SSE2 table for more comments.
227 { ISD::SDIV, MVT::v32i8, 32*20 },
228 { ISD::SDIV, MVT::v16i16, 16*20 },
229 { ISD::SDIV, MVT::v8i32, 8*20 },
230 { ISD::SDIV, MVT::v4i64, 4*20 },
231 { ISD::UDIV, MVT::v32i8, 32*20 },
232 { ISD::UDIV, MVT::v16i16, 16*20 },
233 { ISD::UDIV, MVT::v8i32, 8*20 },
234 { ISD::UDIV, MVT::v4i64, 4*20 },
Michael Liao70dd7f92013-03-20 22:01:10 +0000235 };
236
Simon Pilgrim3d11c992015-09-30 08:17:50 +0000237 // Look for AVX2 lowering tricks for custom cases.
Michael Liao70dd7f92013-03-20 22:01:10 +0000238 if (ST->hasAVX2()) {
Simon Pilgrim3d11c992015-09-30 08:17:50 +0000239 int Idx = CostTableLookup(AVX2CustomCostTable, ISD, LT.second);
Michael Liao70dd7f92013-03-20 22:01:10 +0000240 if (Idx != -1)
Simon Pilgrim3d11c992015-09-30 08:17:50 +0000241 return LT.first * AVX2CustomCostTable[Idx].Cost;
Michael Liao70dd7f92013-03-20 22:01:10 +0000242 }
243
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000244 static const CostTblEntry<MVT::SimpleValueType>
245 SSE2UniformConstCostTable[] = {
Arnold Schwaighofer44f902e2013-04-04 23:26:24 +0000246 // We don't correctly identify costs of casts because they are marked as
247 // custom.
248 // Constant splats are cheaper for the following instructions.
249 { ISD::SHL, MVT::v16i8, 1 }, // psllw.
250 { ISD::SHL, MVT::v8i16, 1 }, // psllw.
251 { ISD::SHL, MVT::v4i32, 1 }, // pslld
252 { ISD::SHL, MVT::v2i64, 1 }, // psllq.
253
254 { ISD::SRL, MVT::v16i8, 1 }, // psrlw.
255 { ISD::SRL, MVT::v8i16, 1 }, // psrlw.
256 { ISD::SRL, MVT::v4i32, 1 }, // psrld.
257 { ISD::SRL, MVT::v2i64, 1 }, // psrlq.
258
259 { ISD::SRA, MVT::v16i8, 4 }, // psrlw, pand, pxor, psubb.
260 { ISD::SRA, MVT::v8i16, 1 }, // psraw.
261 { ISD::SRA, MVT::v4i32, 1 }, // psrad.
Simon Pilgrim8fbf1c12015-07-06 22:35:19 +0000262 { ISD::SRA, MVT::v2i64, 4 }, // 2 x psrad + shuffle.
Benjamin Kramer7c372272014-04-26 14:53:05 +0000263
264 { ISD::SDIV, MVT::v8i16, 6 }, // pmulhw sequence
265 { ISD::UDIV, MVT::v8i16, 6 }, // pmulhuw sequence
Benjamin Kramerce4b3fe2014-04-27 18:47:54 +0000266 { ISD::SDIV, MVT::v4i32, 19 }, // pmuludq sequence
Benjamin Kramer7c372272014-04-26 14:53:05 +0000267 { ISD::UDIV, MVT::v4i32, 15 }, // pmuludq sequence
Arnold Schwaighofer44f902e2013-04-04 23:26:24 +0000268 };
269
270 if (Op2Info == TargetTransformInfo::OK_UniformConstantValue &&
271 ST->hasSSE2()) {
Benjamin Kramerce4b3fe2014-04-27 18:47:54 +0000272 // pmuldq sequence.
273 if (ISD == ISD::SDIV && LT.second == MVT::v4i32 && ST->hasSSE41())
274 return LT.first * 15;
275
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000276 int Idx = CostTableLookup(SSE2UniformConstCostTable, ISD, LT.second);
Arnold Schwaighofer44f902e2013-04-04 23:26:24 +0000277 if (Idx != -1)
278 return LT.first * SSE2UniformConstCostTable[Idx].Cost;
279 }
280
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000281 if (ISD == ISD::SHL &&
282 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue) {
283 EVT VT = LT.second;
284 if ((VT == MVT::v8i16 && ST->hasSSE2()) ||
285 (VT == MVT::v4i32 && ST->hasSSE41()))
286 // Vector shift left by non uniform constant can be lowered
287 // into vector multiply (pmullw/pmulld).
288 return LT.first;
289 if (VT == MVT::v4i32 && ST->hasSSE2())
290 // A vector shift left by non uniform constant is converted
291 // into a vector multiply; the new multiply is eventually
292 // lowered into a sequence of shuffles and 2 x pmuludq.
293 ISD = ISD::MUL;
294 }
Arnold Schwaighofer44f902e2013-04-04 23:26:24 +0000295
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000296 static const CostTblEntry<MVT::SimpleValueType> SSE2CostTable[] = {
Arnold Schwaighofere9b50162013-04-03 21:46:05 +0000297 // We don't correctly identify costs of casts because they are marked as
298 // custom.
299 // For some cases, where the shift amount is a scalar we would be able
300 // to generate better code. Unfortunately, when this is the case the value
301 // (the splat) will get hoisted out of the loop, thereby making it invisible
302 // to ISel. The cost model must return worst case assumptions because it is
303 // used for vectorization and we don't want to make vectorized code worse
304 // than scalar code.
Simon Pilgrim59656802015-06-11 07:46:37 +0000305 { ISD::SHL, MVT::v16i8, 26 }, // cmpgtb sequence.
306 { ISD::SHL, MVT::v8i16, 32 }, // cmpgtb sequence.
307 { ISD::SHL, MVT::v4i32, 2*5 }, // We optimized this using mul.
Simon Pilgrim59764dc2015-07-18 20:06:30 +0000308 { ISD::SHL, MVT::v2i64, 4 }, // splat+shuffle sequence.
309 { ISD::SHL, MVT::v4i64, 8 }, // splat+shuffle sequence.
NAKAMURA Takumi0b305db2015-07-14 04:03:49 +0000310
311 { ISD::SRL, MVT::v16i8, 26 }, // cmpgtb sequence.
312 { ISD::SRL, MVT::v8i16, 32 }, // cmpgtb sequence.
313 { ISD::SRL, MVT::v4i32, 16 }, // Shift each lane + blend.
Simon Pilgrim59764dc2015-07-18 20:06:30 +0000314 { ISD::SRL, MVT::v2i64, 4 }, // splat+shuffle sequence.
NAKAMURA Takumi0b305db2015-07-14 04:03:49 +0000315
316 { ISD::SRA, MVT::v16i8, 54 }, // unpacked cmpgtb sequence.
317 { ISD::SRA, MVT::v8i16, 32 }, // cmpgtb sequence.
318 { ISD::SRA, MVT::v4i32, 16 }, // Shift each lane + blend.
Simon Pilgrim86478c62015-07-29 20:31:45 +0000319 { ISD::SRA, MVT::v2i64, 12 }, // srl/xor/sub sequence.
NAKAMURA Takumi0b305db2015-07-14 04:03:49 +0000320
321 // It is not a good idea to vectorize division. We have to scalarize it and
Arnold Schwaighofera04b9ef2013-06-25 19:14:09 +0000322 // in the process we will often end up having to spilling regular
323 // registers. The overhead of division is going to dominate most kernels
324 // anyways so try hard to prevent vectorization of division - it is
325 // generally a bad idea. Assume somewhat arbitrarily that we have to be able
326 // to hide "20 cycles" for each lane.
327 { ISD::SDIV, MVT::v16i8, 16*20 },
328 { ISD::SDIV, MVT::v8i16, 8*20 },
329 { ISD::SDIV, MVT::v4i32, 4*20 },
330 { ISD::SDIV, MVT::v2i64, 2*20 },
331 { ISD::UDIV, MVT::v16i8, 16*20 },
332 { ISD::UDIV, MVT::v8i16, 8*20 },
333 { ISD::UDIV, MVT::v4i32, 4*20 },
334 { ISD::UDIV, MVT::v2i64, 2*20 },
Arnold Schwaighofere9b50162013-04-03 21:46:05 +0000335 };
336
337 if (ST->hasSSE2()) {
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000338 int Idx = CostTableLookup(SSE2CostTable, ISD, LT.second);
Arnold Schwaighofere9b50162013-04-03 21:46:05 +0000339 if (Idx != -1)
340 return LT.first * SSE2CostTable[Idx].Cost;
341 }
342
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000343 static const CostTblEntry<MVT::SimpleValueType> AVX1CostTable[] = {
Renato Goline1fb0592013-01-20 20:57:20 +0000344 // We don't have to scalarize unsupported ops. We can issue two half-sized
345 // operations and we only need to extract the upper YMM half.
346 // Two ops + 1 extract + 1 insert = 4.
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000347 { ISD::MUL, MVT::v16i16, 4 },
Renato Goline1fb0592013-01-20 20:57:20 +0000348 { ISD::MUL, MVT::v8i32, 4 },
349 { ISD::SUB, MVT::v8i32, 4 },
350 { ISD::ADD, MVT::v8i32, 4 },
Renato Goline1fb0592013-01-20 20:57:20 +0000351 { ISD::SUB, MVT::v4i64, 4 },
352 { ISD::ADD, MVT::v4i64, 4 },
Arnold Schwaighofer20ef54f2013-03-02 04:02:52 +0000353 // A v4i64 multiply is custom lowered as two split v2i64 vectors that then
354 // are lowered as a series of long multiplies(3), shifts(4) and adds(2)
355 // Because we believe v4i64 to be a legal type, we must also include the
356 // split factor of two in the cost table. Therefore, the cost here is 18
357 // instead of 9.
358 { ISD::MUL, MVT::v4i64, 18 },
359 };
Chandler Carruth664e3542013-01-07 01:37:14 +0000360
361 // Look for AVX1 lowering tricks.
Arnold Schwaighofer20ef54f2013-03-02 04:02:52 +0000362 if (ST->hasAVX() && !ST->hasAVX2()) {
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000363 EVT VT = LT.second;
364
365 // v16i16 and v8i32 shifts by non-uniform constants are lowered into a
366 // sequence of extract + two vector multiply + insert.
367 if (ISD == ISD::SHL && (VT == MVT::v8i32 || VT == MVT::v16i16) &&
368 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue)
369 ISD = ISD::MUL;
370
371 int Idx = CostTableLookup(AVX1CostTable, ISD, VT);
Renato Goline1fb0592013-01-20 20:57:20 +0000372 if (Idx != -1)
373 return LT.first * AVX1CostTable[Idx].Cost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000374 }
Arnold Schwaighofer20ef54f2013-03-02 04:02:52 +0000375
376 // Custom lowering of vectors.
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000377 static const CostTblEntry<MVT::SimpleValueType> CustomLowered[] = {
Arnold Schwaighofer20ef54f2013-03-02 04:02:52 +0000378 // A v2i64/v4i64 and multiply is custom lowered as a series of long
379 // multiplies(3), shifts(4) and adds(2).
380 { ISD::MUL, MVT::v2i64, 9 },
381 { ISD::MUL, MVT::v4i64, 9 },
382 };
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000383 int Idx = CostTableLookup(CustomLowered, ISD, LT.second);
Arnold Schwaighofer20ef54f2013-03-02 04:02:52 +0000384 if (Idx != -1)
385 return LT.first * CustomLowered[Idx].Cost;
386
387 // Special lowering of v4i32 mul on sse2, sse3: Lower v4i32 mul as 2x shuffle,
388 // 2x pmuludq, 2x shuffle.
389 if (ISD == ISD::MUL && LT.second == MVT::v4i32 && ST->hasSSE2() &&
390 !ST->hasSSE41())
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000391 return LT.first * 6;
Arnold Schwaighofer20ef54f2013-03-02 04:02:52 +0000392
Chandler Carruth664e3542013-01-07 01:37:14 +0000393 // Fallback to the default implementation.
Chandler Carruth705b1852015-01-31 03:43:40 +0000394 return BaseT::getArithmeticInstrCost(Opcode, Ty, Op1Info, Op2Info);
Chandler Carruth664e3542013-01-07 01:37:14 +0000395}
396
Chandler Carruth93205eb2015-08-05 18:08:10 +0000397int X86TTIImpl::getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index,
398 Type *SubTp) {
Karthik Bhate03a25d2014-06-20 04:32:48 +0000399 // We only estimate the cost of reverse and alternate shuffles.
Chandler Carruth705b1852015-01-31 03:43:40 +0000400 if (Kind != TTI::SK_Reverse && Kind != TTI::SK_Alternate)
401 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
Chandler Carruth664e3542013-01-07 01:37:14 +0000402
Chandler Carruth705b1852015-01-31 03:43:40 +0000403 if (Kind == TTI::SK_Reverse) {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000404 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp);
405 int Cost = 1;
Karthik Bhate03a25d2014-06-20 04:32:48 +0000406 if (LT.second.getSizeInBits() > 128)
407 Cost = 3; // Extract + insert + copy.
Chandler Carruth664e3542013-01-07 01:37:14 +0000408
Karthik Bhate03a25d2014-06-20 04:32:48 +0000409 // Multiple by the number of parts.
410 return Cost * LT.first;
411 }
412
Chandler Carruth705b1852015-01-31 03:43:40 +0000413 if (Kind == TTI::SK_Alternate) {
Andrea Di Biagioc8e8bda2014-07-03 22:24:18 +0000414 // 64-bit packed float vectors (v2f32) are widened to type v4f32.
415 // 64-bit packed integer vectors (v2i32) are promoted to type v2i64.
Chandler Carruth93205eb2015-08-05 18:08:10 +0000416 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp);
Karthik Bhate03a25d2014-06-20 04:32:48 +0000417
Andrea Di Biagioc8e8bda2014-07-03 22:24:18 +0000418 // The backend knows how to generate a single VEX.256 version of
419 // instruction VPBLENDW if the target supports AVX2.
420 if (ST->hasAVX2() && LT.second == MVT::v16i16)
421 return LT.first;
422
423 static const CostTblEntry<MVT::SimpleValueType> AVXAltShuffleTbl[] = {
424 {ISD::VECTOR_SHUFFLE, MVT::v4i64, 1}, // vblendpd
425 {ISD::VECTOR_SHUFFLE, MVT::v4f64, 1}, // vblendpd
426
427 {ISD::VECTOR_SHUFFLE, MVT::v8i32, 1}, // vblendps
428 {ISD::VECTOR_SHUFFLE, MVT::v8f32, 1}, // vblendps
429
430 // This shuffle is custom lowered into a sequence of:
431 // 2x vextractf128 , 2x vpblendw , 1x vinsertf128
432 {ISD::VECTOR_SHUFFLE, MVT::v16i16, 5},
433
434 // This shuffle is custom lowered into a long sequence of:
435 // 2x vextractf128 , 4x vpshufb , 2x vpor , 1x vinsertf128
436 {ISD::VECTOR_SHUFFLE, MVT::v32i8, 9}
437 };
438
439 if (ST->hasAVX()) {
440 int Idx = CostTableLookup(AVXAltShuffleTbl, ISD::VECTOR_SHUFFLE, LT.second);
441 if (Idx != -1)
442 return LT.first * AVXAltShuffleTbl[Idx].Cost;
443 }
444
445 static const CostTblEntry<MVT::SimpleValueType> SSE41AltShuffleTbl[] = {
446 // These are lowered into movsd.
447 {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1},
448 {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1},
449
450 // packed float vectors with four elements are lowered into BLENDI dag
451 // nodes. A v4i32/v4f32 BLENDI generates a single 'blendps'/'blendpd'.
452 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 1},
453 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 1},
454
455 // This shuffle generates a single pshufw.
456 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 1},
457
458 // There is no instruction that matches a v16i8 alternate shuffle.
459 // The backend will expand it into the sequence 'pshufb + pshufb + or'.
460 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 3}
461 };
462
463 if (ST->hasSSE41()) {
464 int Idx = CostTableLookup(SSE41AltShuffleTbl, ISD::VECTOR_SHUFFLE, LT.second);
465 if (Idx != -1)
466 return LT.first * SSE41AltShuffleTbl[Idx].Cost;
467 }
468
469 static const CostTblEntry<MVT::SimpleValueType> SSSE3AltShuffleTbl[] = {
470 {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1}, // movsd
471 {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1}, // movsd
472
473 // SSE3 doesn't have 'blendps'. The following shuffles are expanded into
474 // the sequence 'shufps + pshufd'
475 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 2},
476 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 2},
477
478 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 3}, // pshufb + pshufb + or
479 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 3} // pshufb + pshufb + or
480 };
Michael Liao5bf95782014-12-04 05:20:33 +0000481
Andrea Di Biagioc8e8bda2014-07-03 22:24:18 +0000482 if (ST->hasSSSE3()) {
483 int Idx = CostTableLookup(SSSE3AltShuffleTbl, ISD::VECTOR_SHUFFLE, LT.second);
484 if (Idx != -1)
485 return LT.first * SSSE3AltShuffleTbl[Idx].Cost;
486 }
487
488 static const CostTblEntry<MVT::SimpleValueType> SSEAltShuffleTbl[] = {
489 {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1}, // movsd
490 {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1}, // movsd
491
492 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 2}, // shufps + pshufd
493 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 2}, // shufps + pshufd
Michael Liao5bf95782014-12-04 05:20:33 +0000494
Andrea Di Biagioc8e8bda2014-07-03 22:24:18 +0000495 // This is expanded into a long sequence of four extract + four insert.
496 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 8}, // 4 x pextrw + 4 pinsrw.
497
498 // 8 x (pinsrw + pextrw + and + movb + movzb + or)
499 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 48}
500 };
501
Michael Liao5bf95782014-12-04 05:20:33 +0000502 // Fall-back (SSE3 and SSE2).
Andrea Di Biagioc8e8bda2014-07-03 22:24:18 +0000503 int Idx = CostTableLookup(SSEAltShuffleTbl, ISD::VECTOR_SHUFFLE, LT.second);
504 if (Idx != -1)
505 return LT.first * SSEAltShuffleTbl[Idx].Cost;
Chandler Carruth705b1852015-01-31 03:43:40 +0000506 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
Karthik Bhate03a25d2014-06-20 04:32:48 +0000507 }
508
Chandler Carruth705b1852015-01-31 03:43:40 +0000509 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
Chandler Carruth664e3542013-01-07 01:37:14 +0000510}
511
Chandler Carruth93205eb2015-08-05 18:08:10 +0000512int X86TTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) {
Chandler Carruth664e3542013-01-07 01:37:14 +0000513 int ISD = TLI->InstructionOpcodeToISD(Opcode);
514 assert(ISD && "Invalid opcode");
515
Elena Demikhovsky27012472014-09-16 07:57:37 +0000516 static const TypeConversionCostTblEntry<MVT::SimpleValueType>
517 AVX512ConversionTbl[] = {
518 { ISD::FP_EXTEND, MVT::v8f64, MVT::v8f32, 1 },
519 { ISD::FP_EXTEND, MVT::v8f64, MVT::v16f32, 3 },
520 { ISD::FP_ROUND, MVT::v8f32, MVT::v8f64, 1 },
521 { ISD::FP_ROUND, MVT::v16f32, MVT::v8f64, 3 },
522
523 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i32, 1 },
524 { ISD::TRUNCATE, MVT::v16i16, MVT::v16i32, 1 },
525 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i64, 1 },
526 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 1 },
527 { ISD::TRUNCATE, MVT::v16i32, MVT::v8i64, 4 },
528
529 // v16i1 -> v16i32 - load + broadcast
530 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i1, 2 },
531 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i1, 2 },
532
533 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i8, 1 },
534 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i8, 1 },
535 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i16, 1 },
536 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i16, 1 },
537 { ISD::SIGN_EXTEND, MVT::v8i64, MVT::v16i32, 3 },
538 { ISD::ZERO_EXTEND, MVT::v8i64, MVT::v16i32, 3 },
539
Elena Demikhovskyd5e95b52014-11-13 11:46:16 +0000540 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i1, 3 },
541 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i8, 2 },
542 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i16, 2 },
543 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i32, 1 },
544 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i1, 4 },
545 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i16, 2 },
546 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i32, 1 },
Elena Demikhovsky27012472014-09-16 07:57:37 +0000547 };
548
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000549 static const TypeConversionCostTblEntry<MVT::SimpleValueType>
Tim Northoverf0e21612014-02-06 18:18:36 +0000550 AVX2ConversionTbl[] = {
551 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 1 },
552 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 1 },
553 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 3 },
554 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 3 },
555 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 3 },
556 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 3 },
557 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
558 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
559 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i1, 3 },
560 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i1, 3 },
561 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 3 },
562 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 3 },
563 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
564 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
565 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
566 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
567
568 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i64, 2 },
569 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i64, 2 },
570 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 2 },
571 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 2 },
572 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 2 },
573 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 4 },
Elena Demikhovsky27012472014-09-16 07:57:37 +0000574
575 { ISD::FP_EXTEND, MVT::v8f64, MVT::v8f32, 3 },
576 { ISD::FP_ROUND, MVT::v8f32, MVT::v8f64, 3 },
Quentin Colombet360460b2014-11-11 02:23:47 +0000577
578 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 8 },
Tim Northoverf0e21612014-02-06 18:18:36 +0000579 };
580
581 static const TypeConversionCostTblEntry<MVT::SimpleValueType>
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000582 AVXConversionTbl[] = {
Tim Northoverf0e21612014-02-06 18:18:36 +0000583 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 4 },
584 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 4 },
585 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 7 },
586 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 4 },
587 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 7 },
588 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 4 },
589 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 4 },
590 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 4 },
591 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i1, 6 },
592 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i1, 4 },
593 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 6 },
594 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 4 },
595 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 6 },
596 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
597 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 4 },
598 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 4 },
599
600 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i64, 4 },
601 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i64, 4 },
602 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 4 },
603 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 4 },
604 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 5 },
605 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i16, 4 },
606 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 9 },
Benjamin Kramer52ceb442013-04-01 10:23:49 +0000607
608 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i1, 8 },
609 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i8, 8 },
610 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i16, 5 },
611 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i32, 1 },
612 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i1, 3 },
613 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i8, 3 },
614 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i16, 3 },
615 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 },
616 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i1, 3 },
617 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i8, 3 },
618 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i16, 3 },
619 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i32, 1 },
620
621 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i1, 6 },
622 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i8, 5 },
623 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i16, 5 },
624 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 9 },
625 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i1, 7 },
626 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i8, 2 },
627 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i16, 2 },
628 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 6 },
629 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i1, 7 },
630 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i8, 2 },
631 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i16, 2 },
632 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i32, 6 },
Quentin Colombet85b904d2014-03-27 22:27:41 +0000633 // The generic code to compute the scalar overhead is currently broken.
634 // Workaround this limitation by estimating the scalarization overhead
635 // here. We have roughly 10 instructions per scalar element.
636 // Multiply that by the vector width.
637 // FIXME: remove that when PR19268 is fixed.
Quentin Colombet3914bf52014-03-27 00:52:16 +0000638 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 },
639 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i64, 4*10 },
Benjamin Kramer52ceb442013-04-01 10:23:49 +0000640
Jim Grosbach72fbde82014-03-27 00:04:11 +0000641 { ISD::FP_TO_SINT, MVT::v8i8, MVT::v8f32, 7 },
Renato Goline1fb0592013-01-20 20:57:20 +0000642 { ISD::FP_TO_SINT, MVT::v4i8, MVT::v4f32, 1 },
Adam Nemet6dafe972014-03-30 18:07:13 +0000643 // This node is expanded into scalarized operations but BasicTTI is overly
644 // optimistic estimating its cost. It computes 3 per element (one
645 // vector-extract, one scalar conversion and one vector-insert). The
646 // problem is that the inserts form a read-modify-write chain so latency
647 // should be factored in too. Inflating the cost per element by 1.
648 { ISD::FP_TO_UINT, MVT::v8i32, MVT::v8f32, 8*4 },
Adam Nemet10c4ce22014-03-31 21:54:48 +0000649 { ISD::FP_TO_UINT, MVT::v4i32, MVT::v4f64, 4*4 },
Chandler Carruth664e3542013-01-07 01:37:14 +0000650 };
651
Simon Pilgrime2c244f2015-07-19 15:36:12 +0000652 static const TypeConversionCostTblEntry<MVT::SimpleValueType>
653 SSE2ConvTbl[] = {
654 // These are somewhat magic numbers justified by looking at the output of
655 // Intel's IACA, running some kernels and making sure when we take
656 // legalization into account the throughput will be overestimated.
657 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 },
658 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 },
659 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 },
660 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 },
661 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 },
662 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 },
663 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 },
664 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 },
665 // There are faster sequences for float conversions.
666 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 },
667 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 8 },
668 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 },
669 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 },
670 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 },
671 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 15 },
672 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 },
673 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 },
674 };
675
Chandler Carruth93205eb2015-08-05 18:08:10 +0000676 std::pair<int, MVT> LTSrc = TLI->getTypeLegalizationCost(DL, Src);
677 std::pair<int, MVT> LTDest = TLI->getTypeLegalizationCost(DL, Dst);
Simon Pilgrime2c244f2015-07-19 15:36:12 +0000678
679 if (ST->hasSSE2() && !ST->hasAVX()) {
680 int Idx =
681 ConvertCostTableLookup(SSE2ConvTbl, ISD, LTDest.second, LTSrc.second);
682 if (Idx != -1)
683 return LTSrc.first * SSE2ConvTbl[Idx].Cost;
684 }
685
686 if (ST->hasAVX512()) {
687 int Idx = ConvertCostTableLookup(AVX512ConversionTbl, ISD, LTDest.second,
688 LTSrc.second);
689 if (Idx != -1)
690 return AVX512ConversionTbl[Idx].Cost;
691 }
692
693 EVT SrcTy = TLI->getValueType(DL, Src);
694 EVT DstTy = TLI->getValueType(DL, Dst);
695
696 // The function getSimpleVT only handles simple value types.
697 if (!SrcTy.isSimple() || !DstTy.isSimple())
698 return BaseT::getCastInstrCost(Opcode, Dst, Src);
699
Tim Northoverf0e21612014-02-06 18:18:36 +0000700 if (ST->hasAVX2()) {
701 int Idx = ConvertCostTableLookup(AVX2ConversionTbl, ISD,
702 DstTy.getSimpleVT(), SrcTy.getSimpleVT());
703 if (Idx != -1)
704 return AVX2ConversionTbl[Idx].Cost;
705 }
706
Chandler Carruth664e3542013-01-07 01:37:14 +0000707 if (ST->hasAVX()) {
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000708 int Idx = ConvertCostTableLookup(AVXConversionTbl, ISD, DstTy.getSimpleVT(),
709 SrcTy.getSimpleVT());
Renato Goline1fb0592013-01-20 20:57:20 +0000710 if (Idx != -1)
711 return AVXConversionTbl[Idx].Cost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000712 }
713
Chandler Carruth705b1852015-01-31 03:43:40 +0000714 return BaseT::getCastInstrCost(Opcode, Dst, Src);
Chandler Carruth664e3542013-01-07 01:37:14 +0000715}
716
Chandler Carruth93205eb2015-08-05 18:08:10 +0000717int X86TTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy) {
Chandler Carruth664e3542013-01-07 01:37:14 +0000718 // Legalize the type.
Chandler Carruth93205eb2015-08-05 18:08:10 +0000719 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, ValTy);
Chandler Carruth664e3542013-01-07 01:37:14 +0000720
721 MVT MTy = LT.second;
722
723 int ISD = TLI->InstructionOpcodeToISD(Opcode);
724 assert(ISD && "Invalid opcode");
725
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000726 static const CostTblEntry<MVT::SimpleValueType> SSE42CostTbl[] = {
Renato Goline1fb0592013-01-20 20:57:20 +0000727 { ISD::SETCC, MVT::v2f64, 1 },
728 { ISD::SETCC, MVT::v4f32, 1 },
729 { ISD::SETCC, MVT::v2i64, 1 },
730 { ISD::SETCC, MVT::v4i32, 1 },
731 { ISD::SETCC, MVT::v8i16, 1 },
732 { ISD::SETCC, MVT::v16i8, 1 },
Chandler Carruth664e3542013-01-07 01:37:14 +0000733 };
734
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000735 static const CostTblEntry<MVT::SimpleValueType> AVX1CostTbl[] = {
Renato Goline1fb0592013-01-20 20:57:20 +0000736 { ISD::SETCC, MVT::v4f64, 1 },
737 { ISD::SETCC, MVT::v8f32, 1 },
Chandler Carruth664e3542013-01-07 01:37:14 +0000738 // AVX1 does not support 8-wide integer compare.
Renato Goline1fb0592013-01-20 20:57:20 +0000739 { ISD::SETCC, MVT::v4i64, 4 },
740 { ISD::SETCC, MVT::v8i32, 4 },
741 { ISD::SETCC, MVT::v16i16, 4 },
742 { ISD::SETCC, MVT::v32i8, 4 },
Chandler Carruth664e3542013-01-07 01:37:14 +0000743 };
744
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000745 static const CostTblEntry<MVT::SimpleValueType> AVX2CostTbl[] = {
Renato Goline1fb0592013-01-20 20:57:20 +0000746 { ISD::SETCC, MVT::v4i64, 1 },
747 { ISD::SETCC, MVT::v8i32, 1 },
748 { ISD::SETCC, MVT::v16i16, 1 },
749 { ISD::SETCC, MVT::v32i8, 1 },
Chandler Carruth664e3542013-01-07 01:37:14 +0000750 };
751
Elena Demikhovsky27012472014-09-16 07:57:37 +0000752 static const CostTblEntry<MVT::SimpleValueType> AVX512CostTbl[] = {
753 { ISD::SETCC, MVT::v8i64, 1 },
754 { ISD::SETCC, MVT::v16i32, 1 },
755 { ISD::SETCC, MVT::v8f64, 1 },
756 { ISD::SETCC, MVT::v16f32, 1 },
757 };
758
759 if (ST->hasAVX512()) {
760 int Idx = CostTableLookup(AVX512CostTbl, ISD, MTy);
761 if (Idx != -1)
762 return LT.first * AVX512CostTbl[Idx].Cost;
763 }
764
Chandler Carruth664e3542013-01-07 01:37:14 +0000765 if (ST->hasAVX2()) {
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000766 int Idx = CostTableLookup(AVX2CostTbl, ISD, MTy);
Renato Goline1fb0592013-01-20 20:57:20 +0000767 if (Idx != -1)
768 return LT.first * AVX2CostTbl[Idx].Cost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000769 }
770
771 if (ST->hasAVX()) {
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000772 int Idx = CostTableLookup(AVX1CostTbl, ISD, MTy);
Renato Goline1fb0592013-01-20 20:57:20 +0000773 if (Idx != -1)
774 return LT.first * AVX1CostTbl[Idx].Cost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000775 }
776
777 if (ST->hasSSE42()) {
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000778 int Idx = CostTableLookup(SSE42CostTbl, ISD, MTy);
Renato Goline1fb0592013-01-20 20:57:20 +0000779 if (Idx != -1)
780 return LT.first * SSE42CostTbl[Idx].Cost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000781 }
782
Chandler Carruth705b1852015-01-31 03:43:40 +0000783 return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy);
Chandler Carruth664e3542013-01-07 01:37:14 +0000784}
785
Chandler Carruth93205eb2015-08-05 18:08:10 +0000786int X86TTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index) {
Chandler Carruth664e3542013-01-07 01:37:14 +0000787 assert(Val->isVectorTy() && "This must be a vector type");
788
789 if (Index != -1U) {
790 // Legalize the type.
Chandler Carruth93205eb2015-08-05 18:08:10 +0000791 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Val);
Chandler Carruth664e3542013-01-07 01:37:14 +0000792
793 // This type is legalized to a scalar type.
794 if (!LT.second.isVector())
795 return 0;
796
797 // The type may be split. Normalize the index to the new type.
798 unsigned Width = LT.second.getVectorNumElements();
799 Index = Index % Width;
800
801 // Floating point scalars are already located in index #0.
802 if (Val->getScalarType()->isFloatingPointTy() && Index == 0)
803 return 0;
804 }
805
Chandler Carruth705b1852015-01-31 03:43:40 +0000806 return BaseT::getVectorInstrCost(Opcode, Val, Index);
Chandler Carruth664e3542013-01-07 01:37:14 +0000807}
808
Chandler Carruth93205eb2015-08-05 18:08:10 +0000809int X86TTIImpl::getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) {
Nadav Rotemf9ecbcb2013-06-27 17:52:04 +0000810 assert (Ty->isVectorTy() && "Can only scalarize vectors");
Chandler Carruth93205eb2015-08-05 18:08:10 +0000811 int Cost = 0;
Nadav Rotemf9ecbcb2013-06-27 17:52:04 +0000812
813 for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
814 if (Insert)
Chandler Carruth705b1852015-01-31 03:43:40 +0000815 Cost += getVectorInstrCost(Instruction::InsertElement, Ty, i);
Nadav Rotemf9ecbcb2013-06-27 17:52:04 +0000816 if (Extract)
Chandler Carruth705b1852015-01-31 03:43:40 +0000817 Cost += getVectorInstrCost(Instruction::ExtractElement, Ty, i);
Nadav Rotemf9ecbcb2013-06-27 17:52:04 +0000818 }
819
820 return Cost;
821}
822
Chandler Carruth93205eb2015-08-05 18:08:10 +0000823int X86TTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
824 unsigned AddressSpace) {
Alp Tokerf907b892013-12-05 05:44:44 +0000825 // Handle non-power-of-two vectors such as <3 x float>
Nadav Rotemf9ecbcb2013-06-27 17:52:04 +0000826 if (VectorType *VTy = dyn_cast<VectorType>(Src)) {
827 unsigned NumElem = VTy->getVectorNumElements();
828
829 // Handle a few common cases:
830 // <3 x float>
831 if (NumElem == 3 && VTy->getScalarSizeInBits() == 32)
832 // Cost = 64 bit store + extract + 32 bit store.
833 return 3;
834
835 // <3 x double>
836 if (NumElem == 3 && VTy->getScalarSizeInBits() == 64)
837 // Cost = 128 bit store + unpack + 64 bit store.
838 return 3;
839
Alp Tokerf907b892013-12-05 05:44:44 +0000840 // Assume that all other non-power-of-two numbers are scalarized.
Nadav Rotemf9ecbcb2013-06-27 17:52:04 +0000841 if (!isPowerOf2_32(NumElem)) {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000842 int Cost = BaseT::getMemoryOpCost(Opcode, VTy->getScalarType(), Alignment,
843 AddressSpace);
844 int SplitCost = getScalarizationOverhead(Src, Opcode == Instruction::Load,
845 Opcode == Instruction::Store);
Nadav Rotemf9ecbcb2013-06-27 17:52:04 +0000846 return NumElem * Cost + SplitCost;
847 }
848 }
849
Chandler Carruth664e3542013-01-07 01:37:14 +0000850 // Legalize the type.
Chandler Carruth93205eb2015-08-05 18:08:10 +0000851 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Src);
Chandler Carruth664e3542013-01-07 01:37:14 +0000852 assert((Opcode == Instruction::Load || Opcode == Instruction::Store) &&
853 "Invalid Opcode");
854
855 // Each load/store unit costs 1.
Chandler Carruth93205eb2015-08-05 18:08:10 +0000856 int Cost = LT.first * 1;
Chandler Carruth664e3542013-01-07 01:37:14 +0000857
858 // On Sandybridge 256bit load/stores are double pumped
859 // (but not on Haswell).
860 if (LT.second.getSizeInBits() > 128 && !ST->hasAVX2())
861 Cost*=2;
862
863 return Cost;
864}
Arnold Schwaighofer6042a262013-07-12 19:16:07 +0000865
Chandler Carruth93205eb2015-08-05 18:08:10 +0000866int X86TTIImpl::getMaskedMemoryOpCost(unsigned Opcode, Type *SrcTy,
867 unsigned Alignment,
868 unsigned AddressSpace) {
Elena Demikhovskya3232f72015-01-25 08:44:46 +0000869 VectorType *SrcVTy = dyn_cast<VectorType>(SrcTy);
870 if (!SrcVTy)
871 // To calculate scalar take the regular cost, without mask
872 return getMemoryOpCost(Opcode, SrcTy, Alignment, AddressSpace);
873
874 unsigned NumElem = SrcVTy->getVectorNumElements();
875 VectorType *MaskTy =
876 VectorType::get(Type::getInt8Ty(getGlobalContext()), NumElem);
877 if ((Opcode == Instruction::Load && !isLegalMaskedLoad(SrcVTy, 1)) ||
878 (Opcode == Instruction::Store && !isLegalMaskedStore(SrcVTy, 1)) ||
879 !isPowerOf2_32(NumElem)) {
880 // Scalarization
Chandler Carruth93205eb2015-08-05 18:08:10 +0000881 int MaskSplitCost = getScalarizationOverhead(MaskTy, false, true);
882 int ScalarCompareCost = getCmpSelInstrCost(
883 Instruction::ICmp, Type::getInt8Ty(getGlobalContext()), NULL);
884 int BranchCost = getCFInstrCost(Instruction::Br);
885 int MaskCmpCost = NumElem * (BranchCost + ScalarCompareCost);
Elena Demikhovskya3232f72015-01-25 08:44:46 +0000886
Chandler Carruth93205eb2015-08-05 18:08:10 +0000887 int ValueSplitCost = getScalarizationOverhead(
888 SrcVTy, Opcode == Instruction::Load, Opcode == Instruction::Store);
889 int MemopCost =
Chandler Carruth705b1852015-01-31 03:43:40 +0000890 NumElem * BaseT::getMemoryOpCost(Opcode, SrcVTy->getScalarType(),
891 Alignment, AddressSpace);
Elena Demikhovskya3232f72015-01-25 08:44:46 +0000892 return MemopCost + ValueSplitCost + MaskSplitCost + MaskCmpCost;
893 }
894
895 // Legalize the type.
Chandler Carruth93205eb2015-08-05 18:08:10 +0000896 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, SrcVTy);
897 int Cost = 0;
Mehdi Amini44ede332015-07-09 02:09:04 +0000898 if (LT.second != TLI->getValueType(DL, SrcVTy).getSimpleVT() &&
Elena Demikhovskya3232f72015-01-25 08:44:46 +0000899 LT.second.getVectorNumElements() == NumElem)
900 // Promotion requires expand/truncate for data and a shuffle for mask.
Chandler Carruth705b1852015-01-31 03:43:40 +0000901 Cost += getShuffleCost(TTI::SK_Alternate, SrcVTy, 0, 0) +
902 getShuffleCost(TTI::SK_Alternate, MaskTy, 0, 0);
903
Elena Demikhovskya3232f72015-01-25 08:44:46 +0000904 else if (LT.second.getVectorNumElements() > NumElem) {
905 VectorType *NewMaskTy = VectorType::get(MaskTy->getVectorElementType(),
906 LT.second.getVectorNumElements());
907 // Expanding requires fill mask with zeroes
Chandler Carruth705b1852015-01-31 03:43:40 +0000908 Cost += getShuffleCost(TTI::SK_InsertSubvector, NewMaskTy, 0, MaskTy);
Elena Demikhovskya3232f72015-01-25 08:44:46 +0000909 }
910 if (!ST->hasAVX512())
911 return Cost + LT.first*4; // Each maskmov costs 4
912
913 // AVX-512 masked load/store is cheapper
914 return Cost+LT.first;
915}
916
Chandler Carruth93205eb2015-08-05 18:08:10 +0000917int X86TTIImpl::getAddressComputationCost(Type *Ty, bool IsComplex) {
Arnold Schwaighofer6042a262013-07-12 19:16:07 +0000918 // Address computations in vectorized code with non-consecutive addresses will
919 // likely result in more instructions compared to scalar code where the
920 // computation can more often be merged into the index mode. The resulting
921 // extra micro-ops can significantly decrease throughput.
922 unsigned NumVectorInstToHideOverhead = 10;
923
924 if (Ty->isVectorTy() && IsComplex)
925 return NumVectorInstToHideOverhead;
926
Chandler Carruth705b1852015-01-31 03:43:40 +0000927 return BaseT::getAddressComputationCost(Ty, IsComplex);
Arnold Schwaighofer6042a262013-07-12 19:16:07 +0000928}
Yi Jiang5c343de2013-09-19 17:48:48 +0000929
Chandler Carruth93205eb2015-08-05 18:08:10 +0000930int X86TTIImpl::getReductionCost(unsigned Opcode, Type *ValTy,
931 bool IsPairwise) {
Michael Liao5bf95782014-12-04 05:20:33 +0000932
Chandler Carruth93205eb2015-08-05 18:08:10 +0000933 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, ValTy);
Michael Liao5bf95782014-12-04 05:20:33 +0000934
Yi Jiang5c343de2013-09-19 17:48:48 +0000935 MVT MTy = LT.second;
Michael Liao5bf95782014-12-04 05:20:33 +0000936
Yi Jiang5c343de2013-09-19 17:48:48 +0000937 int ISD = TLI->InstructionOpcodeToISD(Opcode);
938 assert(ISD && "Invalid opcode");
Michael Liao5bf95782014-12-04 05:20:33 +0000939
940 // We use the Intel Architecture Code Analyzer(IACA) to measure the throughput
941 // and make it as the cost.
942
Yi Jiang5c343de2013-09-19 17:48:48 +0000943 static const CostTblEntry<MVT::SimpleValueType> SSE42CostTblPairWise[] = {
944 { ISD::FADD, MVT::v2f64, 2 },
945 { ISD::FADD, MVT::v4f32, 4 },
946 { ISD::ADD, MVT::v2i64, 2 }, // The data reported by the IACA tool is "1.6".
947 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.5".
948 { ISD::ADD, MVT::v8i16, 5 },
949 };
Michael Liao5bf95782014-12-04 05:20:33 +0000950
Yi Jiang5c343de2013-09-19 17:48:48 +0000951 static const CostTblEntry<MVT::SimpleValueType> AVX1CostTblPairWise[] = {
952 { ISD::FADD, MVT::v4f32, 4 },
953 { ISD::FADD, MVT::v4f64, 5 },
954 { ISD::FADD, MVT::v8f32, 7 },
955 { ISD::ADD, MVT::v2i64, 1 }, // The data reported by the IACA tool is "1.5".
956 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.5".
957 { ISD::ADD, MVT::v4i64, 5 }, // The data reported by the IACA tool is "4.8".
958 { ISD::ADD, MVT::v8i16, 5 },
959 { ISD::ADD, MVT::v8i32, 5 },
960 };
961
962 static const CostTblEntry<MVT::SimpleValueType> SSE42CostTblNoPairWise[] = {
963 { ISD::FADD, MVT::v2f64, 2 },
964 { ISD::FADD, MVT::v4f32, 4 },
965 { ISD::ADD, MVT::v2i64, 2 }, // The data reported by the IACA tool is "1.6".
966 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.3".
967 { ISD::ADD, MVT::v8i16, 4 }, // The data reported by the IACA tool is "4.3".
968 };
Michael Liao5bf95782014-12-04 05:20:33 +0000969
Yi Jiang5c343de2013-09-19 17:48:48 +0000970 static const CostTblEntry<MVT::SimpleValueType> AVX1CostTblNoPairWise[] = {
971 { ISD::FADD, MVT::v4f32, 3 },
972 { ISD::FADD, MVT::v4f64, 3 },
973 { ISD::FADD, MVT::v8f32, 4 },
974 { ISD::ADD, MVT::v2i64, 1 }, // The data reported by the IACA tool is "1.5".
975 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "2.8".
976 { ISD::ADD, MVT::v4i64, 3 },
977 { ISD::ADD, MVT::v8i16, 4 },
978 { ISD::ADD, MVT::v8i32, 5 },
979 };
Michael Liao5bf95782014-12-04 05:20:33 +0000980
Yi Jiang5c343de2013-09-19 17:48:48 +0000981 if (IsPairwise) {
982 if (ST->hasAVX()) {
983 int Idx = CostTableLookup(AVX1CostTblPairWise, ISD, MTy);
984 if (Idx != -1)
985 return LT.first * AVX1CostTblPairWise[Idx].Cost;
986 }
Michael Liao5bf95782014-12-04 05:20:33 +0000987
Yi Jiang5c343de2013-09-19 17:48:48 +0000988 if (ST->hasSSE42()) {
989 int Idx = CostTableLookup(SSE42CostTblPairWise, ISD, MTy);
990 if (Idx != -1)
991 return LT.first * SSE42CostTblPairWise[Idx].Cost;
992 }
993 } else {
994 if (ST->hasAVX()) {
995 int Idx = CostTableLookup(AVX1CostTblNoPairWise, ISD, MTy);
996 if (Idx != -1)
997 return LT.first * AVX1CostTblNoPairWise[Idx].Cost;
998 }
Michael Liao5bf95782014-12-04 05:20:33 +0000999
Yi Jiang5c343de2013-09-19 17:48:48 +00001000 if (ST->hasSSE42()) {
1001 int Idx = CostTableLookup(SSE42CostTblNoPairWise, ISD, MTy);
1002 if (Idx != -1)
1003 return LT.first * SSE42CostTblNoPairWise[Idx].Cost;
1004 }
1005 }
1006
Chandler Carruth705b1852015-01-31 03:43:40 +00001007 return BaseT::getReductionCost(Opcode, ValTy, IsPairwise);
Yi Jiang5c343de2013-09-19 17:48:48 +00001008}
1009
Juergen Ributzkab2e4edb2014-06-10 00:32:29 +00001010/// \brief Calculate the cost of materializing a 64-bit value. This helper
1011/// method might only calculate a fraction of a larger immediate. Therefore it
1012/// is valid to return a cost of ZERO.
Chandler Carruth93205eb2015-08-05 18:08:10 +00001013int X86TTIImpl::getIntImmCost(int64_t Val) {
Juergen Ributzkab2e4edb2014-06-10 00:32:29 +00001014 if (Val == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +00001015 return TTI::TCC_Free;
Juergen Ributzkab2e4edb2014-06-10 00:32:29 +00001016
1017 if (isInt<32>(Val))
Chandler Carruth705b1852015-01-31 03:43:40 +00001018 return TTI::TCC_Basic;
Juergen Ributzkab2e4edb2014-06-10 00:32:29 +00001019
Chandler Carruth705b1852015-01-31 03:43:40 +00001020 return 2 * TTI::TCC_Basic;
Juergen Ributzkab2e4edb2014-06-10 00:32:29 +00001021}
1022
Chandler Carruth93205eb2015-08-05 18:08:10 +00001023int X86TTIImpl::getIntImmCost(const APInt &Imm, Type *Ty) {
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001024 assert(Ty->isIntegerTy());
1025
1026 unsigned BitSize = Ty->getPrimitiveSizeInBits();
1027 if (BitSize == 0)
1028 return ~0U;
1029
Juergen Ributzka43176172014-05-19 21:00:53 +00001030 // Never hoist constants larger than 128bit, because this might lead to
1031 // incorrect code generation or assertions in codegen.
1032 // Fixme: Create a cost model for types larger than i128 once the codegen
1033 // issues have been fixed.
1034 if (BitSize > 128)
Chandler Carruth705b1852015-01-31 03:43:40 +00001035 return TTI::TCC_Free;
Juergen Ributzka43176172014-05-19 21:00:53 +00001036
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001037 if (Imm == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +00001038 return TTI::TCC_Free;
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001039
Juergen Ributzkab2e4edb2014-06-10 00:32:29 +00001040 // Sign-extend all constants to a multiple of 64-bit.
1041 APInt ImmVal = Imm;
1042 if (BitSize & 0x3f)
1043 ImmVal = Imm.sext((BitSize + 63) & ~0x3fU);
1044
1045 // Split the constant into 64-bit chunks and calculate the cost for each
1046 // chunk.
Chandler Carruth93205eb2015-08-05 18:08:10 +00001047 int Cost = 0;
Juergen Ributzkab2e4edb2014-06-10 00:32:29 +00001048 for (unsigned ShiftVal = 0; ShiftVal < BitSize; ShiftVal += 64) {
1049 APInt Tmp = ImmVal.ashr(ShiftVal).sextOrTrunc(64);
1050 int64_t Val = Tmp.getSExtValue();
1051 Cost += getIntImmCost(Val);
1052 }
1053 // We need at least one instruction to materialze the constant.
Chandler Carruth93205eb2015-08-05 18:08:10 +00001054 return std::max(1, Cost);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001055}
1056
Chandler Carruth93205eb2015-08-05 18:08:10 +00001057int X86TTIImpl::getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
1058 Type *Ty) {
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001059 assert(Ty->isIntegerTy());
1060
1061 unsigned BitSize = Ty->getPrimitiveSizeInBits();
Juergen Ributzka43176172014-05-19 21:00:53 +00001062 // There is no cost model for constants with a bit size of 0. Return TCC_Free
1063 // here, so that constant hoisting will ignore this constant.
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001064 if (BitSize == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +00001065 return TTI::TCC_Free;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001066
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001067 unsigned ImmIdx = ~0U;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001068 switch (Opcode) {
Chandler Carruth705b1852015-01-31 03:43:40 +00001069 default:
1070 return TTI::TCC_Free;
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001071 case Instruction::GetElementPtr:
Juergen Ributzka27435b32014-04-02 21:45:36 +00001072 // Always hoist the base address of a GetElementPtr. This prevents the
1073 // creation of new constants for every base constant that gets constant
1074 // folded with the offset.
Juergen Ributzka631c4912014-03-25 18:01:25 +00001075 if (Idx == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +00001076 return 2 * TTI::TCC_Basic;
1077 return TTI::TCC_Free;
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001078 case Instruction::Store:
1079 ImmIdx = 0;
1080 break;
Craig Topper79dd1bf2015-10-06 02:50:24 +00001081 case Instruction::And:
1082 // We support 64-bit ANDs with immediates with 32-bits of leading zeroes
1083 // by using a 32-bit operation with implicit zero extension. Detect such
1084 // immediates here as the normal path expects bit 31 to be sign extended.
1085 if (Idx == 1 && Imm.getBitWidth() == 64 && isUInt<32>(Imm.getZExtValue()))
1086 return TTI::TCC_Free;
1087 // Fallthrough
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001088 case Instruction::Add:
1089 case Instruction::Sub:
1090 case Instruction::Mul:
1091 case Instruction::UDiv:
1092 case Instruction::SDiv:
1093 case Instruction::URem:
1094 case Instruction::SRem:
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001095 case Instruction::Or:
1096 case Instruction::Xor:
1097 case Instruction::ICmp:
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001098 ImmIdx = 1;
1099 break;
Michael Zolotukhin1f4a9602014-04-30 19:17:32 +00001100 // Always return TCC_Free for the shift value of a shift instruction.
1101 case Instruction::Shl:
1102 case Instruction::LShr:
1103 case Instruction::AShr:
1104 if (Idx == 1)
Chandler Carruth705b1852015-01-31 03:43:40 +00001105 return TTI::TCC_Free;
Michael Zolotukhin1f4a9602014-04-30 19:17:32 +00001106 break;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001107 case Instruction::Trunc:
1108 case Instruction::ZExt:
1109 case Instruction::SExt:
1110 case Instruction::IntToPtr:
1111 case Instruction::PtrToInt:
1112 case Instruction::BitCast:
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001113 case Instruction::PHI:
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001114 case Instruction::Call:
1115 case Instruction::Select:
1116 case Instruction::Ret:
1117 case Instruction::Load:
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001118 break;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001119 }
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001120
Juergen Ributzkab2e4edb2014-06-10 00:32:29 +00001121 if (Idx == ImmIdx) {
Chandler Carruth93205eb2015-08-05 18:08:10 +00001122 int NumConstants = (BitSize + 63) / 64;
1123 int Cost = X86TTIImpl::getIntImmCost(Imm, Ty);
Chandler Carruth705b1852015-01-31 03:43:40 +00001124 return (Cost <= NumConstants * TTI::TCC_Basic)
Chandler Carruth93205eb2015-08-05 18:08:10 +00001125 ? static_cast<int>(TTI::TCC_Free)
Chandler Carruth705b1852015-01-31 03:43:40 +00001126 : Cost;
Juergen Ributzkab2e4edb2014-06-10 00:32:29 +00001127 }
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001128
Chandler Carruth705b1852015-01-31 03:43:40 +00001129 return X86TTIImpl::getIntImmCost(Imm, Ty);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001130}
1131
Chandler Carruth93205eb2015-08-05 18:08:10 +00001132int X86TTIImpl::getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm,
1133 Type *Ty) {
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001134 assert(Ty->isIntegerTy());
1135
1136 unsigned BitSize = Ty->getPrimitiveSizeInBits();
Juergen Ributzka43176172014-05-19 21:00:53 +00001137 // There is no cost model for constants with a bit size of 0. Return TCC_Free
1138 // here, so that constant hoisting will ignore this constant.
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001139 if (BitSize == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +00001140 return TTI::TCC_Free;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001141
1142 switch (IID) {
Chandler Carruth705b1852015-01-31 03:43:40 +00001143 default:
1144 return TTI::TCC_Free;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001145 case Intrinsic::sadd_with_overflow:
1146 case Intrinsic::uadd_with_overflow:
1147 case Intrinsic::ssub_with_overflow:
1148 case Intrinsic::usub_with_overflow:
1149 case Intrinsic::smul_with_overflow:
1150 case Intrinsic::umul_with_overflow:
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001151 if ((Idx == 1) && Imm.getBitWidth() <= 64 && isInt<32>(Imm.getSExtValue()))
Chandler Carruth705b1852015-01-31 03:43:40 +00001152 return TTI::TCC_Free;
Juergen Ributzka5eef98c2014-03-25 18:01:23 +00001153 break;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001154 case Intrinsic::experimental_stackmap:
Juergen Ributzka5eef98c2014-03-25 18:01:23 +00001155 if ((Idx < 2) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
Chandler Carruth705b1852015-01-31 03:43:40 +00001156 return TTI::TCC_Free;
Juergen Ributzka5eef98c2014-03-25 18:01:23 +00001157 break;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001158 case Intrinsic::experimental_patchpoint_void:
1159 case Intrinsic::experimental_patchpoint_i64:
Juergen Ributzka5eef98c2014-03-25 18:01:23 +00001160 if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
Chandler Carruth705b1852015-01-31 03:43:40 +00001161 return TTI::TCC_Free;
Juergen Ributzka5eef98c2014-03-25 18:01:23 +00001162 break;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001163 }
Chandler Carruth705b1852015-01-31 03:43:40 +00001164 return X86TTIImpl::getIntImmCost(Imm, Ty);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001165}
NAKAMURA Takumi0b305db2015-07-14 04:03:49 +00001166
1167bool X86TTIImpl::isLegalMaskedLoad(Type *DataTy, int Consecutive) {
1168 int DataWidth = DataTy->getPrimitiveSizeInBits();
1169
1170 // Todo: AVX512 allows gather/scatter, works with strided and random as well
1171 if ((DataWidth < 32) || (Consecutive == 0))
1172 return false;
1173 if (ST->hasAVX512() || ST->hasAVX2())
1174 return true;
1175 return false;
1176}
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00001177
Chandler Carruth705b1852015-01-31 03:43:40 +00001178bool X86TTIImpl::isLegalMaskedStore(Type *DataType, int Consecutive) {
Elena Demikhovsky3fcafa22014-12-14 09:43:50 +00001179 return isLegalMaskedLoad(DataType, Consecutive);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00001180}
1181
Eric Christopherd566fb12015-07-29 22:09:48 +00001182bool X86TTIImpl::areInlineCompatible(const Function *Caller,
1183 const Function *Callee) const {
Eric Christophere1002262015-07-02 01:11:50 +00001184 const TargetMachine &TM = getTLI()->getTargetMachine();
1185
1186 // Work this as a subsetting of subtarget features.
1187 const FeatureBitset &CallerBits =
1188 TM.getSubtargetImpl(*Caller)->getFeatureBits();
1189 const FeatureBitset &CalleeBits =
1190 TM.getSubtargetImpl(*Callee)->getFeatureBits();
1191
1192 // FIXME: This is likely too limiting as it will include subtarget features
1193 // that we might not care about for inlining, but it is conservatively
1194 // correct.
1195 return (CallerBits & CalleeBits) == CalleeBits;
1196}