blob: 1baa49c3c08dd95920e70aa51ece283d63d9bca5 [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
Craig Topper4b275762015-10-28 04:02:12 +0000118 static const CostTblEntry 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()) {
Craig Topperee0c8592015-10-27 04:14:24 +0000129 if (const auto *Entry = CostTableLookup(AVX2UniformConstCostTable, ISD,
130 LT.second))
131 return LT.first * Entry->Cost;
Benjamin Kramer7c372272014-04-26 14:53:05 +0000132 }
133
Craig Topper4b275762015-10-28 04:02:12 +0000134 static const CostTblEntry AVX512CostTable[] = {
Elena Demikhovsky27012472014-09-16 07:57:37 +0000135 { 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()) {
Craig Topperee0c8592015-10-27 04:14:24 +0000144 if (const auto *Entry = CostTableLookup(AVX512CostTable, ISD, LT.second))
145 return LT.first * Entry->Cost;
Simon Pilgrim3d11c992015-09-30 08:17:50 +0000146 }
147
Craig Topper4b275762015-10-28 04:02:12 +0000148 static const CostTblEntry AVX2CostTable[] = {
Michael Liao70dd7f92013-03-20 22:01:10 +0000149 // Shifts on v4i64/v8i32 on AVX2 is legal even though we declare to
150 // customize them to detect the cases where shift amount is a scalar one.
151 { ISD::SHL, MVT::v4i32, 1 },
152 { ISD::SRL, MVT::v4i32, 1 },
153 { ISD::SRA, MVT::v4i32, 1 },
154 { ISD::SHL, MVT::v8i32, 1 },
155 { ISD::SRL, MVT::v8i32, 1 },
156 { ISD::SRA, MVT::v8i32, 1 },
157 { ISD::SHL, MVT::v2i64, 1 },
158 { ISD::SRL, MVT::v2i64, 1 },
159 { ISD::SHL, MVT::v4i64, 1 },
160 { ISD::SRL, MVT::v4i64, 1 },
Simon Pilgrim3d11c992015-09-30 08:17:50 +0000161 };
Arnold Schwaighofere9b50162013-04-03 21:46:05 +0000162
Simon Pilgrim3d11c992015-09-30 08:17:50 +0000163 // Look for AVX2 lowering tricks.
164 if (ST->hasAVX2()) {
165 if (ISD == ISD::SHL && LT.second == MVT::v16i16 &&
166 (Op2Info == TargetTransformInfo::OK_UniformConstantValue ||
167 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue))
168 // On AVX2, a packed v16i16 shift left by a constant build_vector
169 // is lowered into a vector multiply (vpmullw).
170 return LT.first;
171
Craig Topperee0c8592015-10-27 04:14:24 +0000172 if (const auto *Entry = CostTableLookup(AVX2CostTable, ISD, LT.second))
173 return LT.first * Entry->Cost;
Simon Pilgrim3d11c992015-09-30 08:17:50 +0000174 }
175
Craig Topper4b275762015-10-28 04:02:12 +0000176 static const CostTblEntry XOPCostTable[] = {
Simon Pilgrim3d11c992015-09-30 08:17:50 +0000177 // 128bit shifts take 1cy, but right shifts require negation beforehand.
178 { ISD::SHL, MVT::v16i8, 1 },
179 { ISD::SRL, MVT::v16i8, 2 },
180 { ISD::SRA, MVT::v16i8, 2 },
181 { ISD::SHL, MVT::v8i16, 1 },
182 { ISD::SRL, MVT::v8i16, 2 },
183 { ISD::SRA, MVT::v8i16, 2 },
184 { ISD::SHL, MVT::v4i32, 1 },
185 { ISD::SRL, MVT::v4i32, 2 },
186 { ISD::SRA, MVT::v4i32, 2 },
187 { ISD::SHL, MVT::v2i64, 1 },
188 { ISD::SRL, MVT::v2i64, 2 },
189 { ISD::SRA, MVT::v2i64, 2 },
190 // 256bit shifts require splitting if AVX2 didn't catch them above.
191 { ISD::SHL, MVT::v32i8, 2 },
192 { ISD::SRL, MVT::v32i8, 4 },
193 { ISD::SRA, MVT::v32i8, 4 },
194 { ISD::SHL, MVT::v16i16, 2 },
195 { ISD::SRL, MVT::v16i16, 4 },
196 { ISD::SRA, MVT::v16i16, 4 },
197 { ISD::SHL, MVT::v8i32, 2 },
198 { ISD::SRL, MVT::v8i32, 4 },
199 { ISD::SRA, MVT::v8i32, 4 },
200 { ISD::SHL, MVT::v4i64, 2 },
201 { ISD::SRL, MVT::v4i64, 4 },
202 { ISD::SRA, MVT::v4i64, 4 },
203 };
204
205 // Look for XOP lowering tricks.
206 if (ST->hasXOP()) {
Craig Topperee0c8592015-10-27 04:14:24 +0000207 if (const auto *Entry = CostTableLookup(XOPCostTable, ISD, LT.second))
208 return LT.first * Entry->Cost;
Simon Pilgrim3d11c992015-09-30 08:17:50 +0000209 }
210
Craig Topper4b275762015-10-28 04:02:12 +0000211 static const CostTblEntry AVX2CustomCostTable[] = {
Simon Pilgrim59656802015-06-11 07:46:37 +0000212 { ISD::SHL, MVT::v32i8, 11 }, // vpblendvb sequence.
Simon Pilgrim0be4fa72015-05-25 17:49:13 +0000213 { ISD::SHL, MVT::v16i16, 10 }, // extend/vpsrlvd/pack sequence.
Arnold Schwaighofere9b50162013-04-03 21:46:05 +0000214
Simon Pilgrim59656802015-06-11 07:46:37 +0000215 { ISD::SRL, MVT::v32i8, 11 }, // vpblendvb sequence.
Simon Pilgrim0be4fa72015-05-25 17:49:13 +0000216 { ISD::SRL, MVT::v16i16, 10 }, // extend/vpsrlvd/pack sequence.
Arnold Schwaighofere9b50162013-04-03 21:46:05 +0000217
Simon Pilgrim59656802015-06-11 07:46:37 +0000218 { ISD::SRA, MVT::v32i8, 24 }, // vpblendvb sequence.
Simon Pilgrim0be4fa72015-05-25 17:49:13 +0000219 { ISD::SRA, MVT::v16i16, 10 }, // extend/vpsravd/pack sequence.
Simon Pilgrim86478c62015-07-29 20:31:45 +0000220 { ISD::SRA, MVT::v2i64, 4 }, // srl/xor/sub sequence.
221 { ISD::SRA, MVT::v4i64, 4 }, // srl/xor/sub sequence.
Arnold Schwaighofera04b9ef2013-06-25 19:14:09 +0000222
223 // Vectorizing division is a bad idea. See the SSE2 table for more comments.
224 { ISD::SDIV, MVT::v32i8, 32*20 },
225 { ISD::SDIV, MVT::v16i16, 16*20 },
226 { ISD::SDIV, MVT::v8i32, 8*20 },
227 { ISD::SDIV, MVT::v4i64, 4*20 },
228 { ISD::UDIV, MVT::v32i8, 32*20 },
229 { ISD::UDIV, MVT::v16i16, 16*20 },
230 { ISD::UDIV, MVT::v8i32, 8*20 },
231 { ISD::UDIV, MVT::v4i64, 4*20 },
Michael Liao70dd7f92013-03-20 22:01:10 +0000232 };
233
Simon Pilgrim3d11c992015-09-30 08:17:50 +0000234 // Look for AVX2 lowering tricks for custom cases.
Michael Liao70dd7f92013-03-20 22:01:10 +0000235 if (ST->hasAVX2()) {
Craig Topperee0c8592015-10-27 04:14:24 +0000236 if (const auto *Entry = CostTableLookup(AVX2CustomCostTable, ISD,
237 LT.second))
238 return LT.first * Entry->Cost;
Michael Liao70dd7f92013-03-20 22:01:10 +0000239 }
240
Craig Topper4b275762015-10-28 04:02:12 +0000241 static const CostTblEntry
Benjamin Kramer21585fd2013-08-09 19:33:32 +0000242 SSE2UniformConstCostTable[] = {
Arnold Schwaighofer44f902e2013-04-04 23:26:24 +0000243 // We don't correctly identify costs of casts because they are marked as
244 // custom.
245 // Constant splats are cheaper for the following instructions.
246 { ISD::SHL, MVT::v16i8, 1 }, // psllw.
Simon Pilgrima18ae9b2015-10-17 13:23:38 +0000247 { ISD::SHL, MVT::v32i8, 2 }, // psllw.
Arnold Schwaighofer44f902e2013-04-04 23:26:24 +0000248 { ISD::SHL, MVT::v8i16, 1 }, // psllw.
Simon Pilgrima18ae9b2015-10-17 13:23:38 +0000249 { ISD::SHL, MVT::v16i16, 2 }, // psllw.
Arnold Schwaighofer44f902e2013-04-04 23:26:24 +0000250 { ISD::SHL, MVT::v4i32, 1 }, // pslld
Simon Pilgrima18ae9b2015-10-17 13:23:38 +0000251 { ISD::SHL, MVT::v8i32, 2 }, // pslld
Arnold Schwaighofer44f902e2013-04-04 23:26:24 +0000252 { ISD::SHL, MVT::v2i64, 1 }, // psllq.
Simon Pilgrima18ae9b2015-10-17 13:23:38 +0000253 { ISD::SHL, MVT::v4i64, 2 }, // psllq.
Arnold Schwaighofer44f902e2013-04-04 23:26:24 +0000254
255 { ISD::SRL, MVT::v16i8, 1 }, // psrlw.
Simon Pilgrima18ae9b2015-10-17 13:23:38 +0000256 { ISD::SRL, MVT::v32i8, 2 }, // psrlw.
Arnold Schwaighofer44f902e2013-04-04 23:26:24 +0000257 { ISD::SRL, MVT::v8i16, 1 }, // psrlw.
Simon Pilgrima18ae9b2015-10-17 13:23:38 +0000258 { ISD::SRL, MVT::v16i16, 2 }, // psrlw.
Arnold Schwaighofer44f902e2013-04-04 23:26:24 +0000259 { ISD::SRL, MVT::v4i32, 1 }, // psrld.
Simon Pilgrima18ae9b2015-10-17 13:23:38 +0000260 { ISD::SRL, MVT::v8i32, 2 }, // psrld.
Arnold Schwaighofer44f902e2013-04-04 23:26:24 +0000261 { ISD::SRL, MVT::v2i64, 1 }, // psrlq.
Simon Pilgrima18ae9b2015-10-17 13:23:38 +0000262 { ISD::SRL, MVT::v4i64, 2 }, // psrlq.
Arnold Schwaighofer44f902e2013-04-04 23:26:24 +0000263
264 { ISD::SRA, MVT::v16i8, 4 }, // psrlw, pand, pxor, psubb.
Simon Pilgrima18ae9b2015-10-17 13:23:38 +0000265 { ISD::SRA, MVT::v32i8, 8 }, // psrlw, pand, pxor, psubb.
Arnold Schwaighofer44f902e2013-04-04 23:26:24 +0000266 { ISD::SRA, MVT::v8i16, 1 }, // psraw.
Simon Pilgrima18ae9b2015-10-17 13:23:38 +0000267 { ISD::SRA, MVT::v16i16, 2 }, // psraw.
Arnold Schwaighofer44f902e2013-04-04 23:26:24 +0000268 { ISD::SRA, MVT::v4i32, 1 }, // psrad.
Simon Pilgrima18ae9b2015-10-17 13:23:38 +0000269 { ISD::SRA, MVT::v8i32, 2 }, // psrad.
Simon Pilgrim8fbf1c12015-07-06 22:35:19 +0000270 { ISD::SRA, MVT::v2i64, 4 }, // 2 x psrad + shuffle.
Simon Pilgrima18ae9b2015-10-17 13:23:38 +0000271 { ISD::SRA, MVT::v4i64, 8 }, // 2 x psrad + shuffle.
Benjamin Kramer7c372272014-04-26 14:53:05 +0000272
273 { ISD::SDIV, MVT::v8i16, 6 }, // pmulhw sequence
274 { ISD::UDIV, MVT::v8i16, 6 }, // pmulhuw sequence
Benjamin Kramerce4b3fe2014-04-27 18:47:54 +0000275 { ISD::SDIV, MVT::v4i32, 19 }, // pmuludq sequence
Benjamin Kramer7c372272014-04-26 14:53:05 +0000276 { ISD::UDIV, MVT::v4i32, 15 }, // pmuludq sequence
Arnold Schwaighofer44f902e2013-04-04 23:26:24 +0000277 };
278
279 if (Op2Info == TargetTransformInfo::OK_UniformConstantValue &&
280 ST->hasSSE2()) {
Benjamin Kramerce4b3fe2014-04-27 18:47:54 +0000281 // pmuldq sequence.
282 if (ISD == ISD::SDIV && LT.second == MVT::v4i32 && ST->hasSSE41())
283 return LT.first * 15;
284
Craig Topperee0c8592015-10-27 04:14:24 +0000285 if (const auto *Entry = CostTableLookup(SSE2UniformConstCostTable, ISD,
286 LT.second))
287 return LT.first * Entry->Cost;
Arnold Schwaighofer44f902e2013-04-04 23:26:24 +0000288 }
289
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000290 if (ISD == ISD::SHL &&
291 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue) {
Craig Toppereda02a92015-10-25 03:15:29 +0000292 MVT VT = LT.second;
Simon Pilgrima18ae9b2015-10-17 13:23:38 +0000293 // Vector shift left by non uniform constant can be lowered
294 // into vector multiply (pmullw/pmulld).
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000295 if ((VT == MVT::v8i16 && ST->hasSSE2()) ||
296 (VT == MVT::v4i32 && ST->hasSSE41()))
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000297 return LT.first;
Simon Pilgrima18ae9b2015-10-17 13:23:38 +0000298
299 // v16i16 and v8i32 shifts by non-uniform constants are lowered into a
300 // sequence of extract + two vector multiply + insert.
301 if ((VT == MVT::v8i32 || VT == MVT::v16i16) &&
302 (ST->hasAVX() && !ST->hasAVX2()))
303 ISD = ISD::MUL;
304
305 // A vector shift left by non uniform constant is converted
306 // into a vector multiply; the new multiply is eventually
307 // lowered into a sequence of shuffles and 2 x pmuludq.
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000308 if (VT == MVT::v4i32 && ST->hasSSE2())
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000309 ISD = ISD::MUL;
310 }
Arnold Schwaighofer44f902e2013-04-04 23:26:24 +0000311
Craig Topper4b275762015-10-28 04:02:12 +0000312 static const CostTblEntry SSE2CostTable[] = {
Arnold Schwaighofere9b50162013-04-03 21:46:05 +0000313 // We don't correctly identify costs of casts because they are marked as
314 // custom.
315 // For some cases, where the shift amount is a scalar we would be able
316 // to generate better code. Unfortunately, when this is the case the value
317 // (the splat) will get hoisted out of the loop, thereby making it invisible
318 // to ISel. The cost model must return worst case assumptions because it is
319 // used for vectorization and we don't want to make vectorized code worse
320 // than scalar code.
Simon Pilgrim59656802015-06-11 07:46:37 +0000321 { ISD::SHL, MVT::v16i8, 26 }, // cmpgtb sequence.
Simon Pilgrima18ae9b2015-10-17 13:23:38 +0000322 { ISD::SHL, MVT::v32i8, 2*26 }, // cmpgtb sequence.
Simon Pilgrim59656802015-06-11 07:46:37 +0000323 { ISD::SHL, MVT::v8i16, 32 }, // cmpgtb sequence.
Simon Pilgrima18ae9b2015-10-17 13:23:38 +0000324 { ISD::SHL, MVT::v16i16, 2*32 }, // cmpgtb sequence.
Simon Pilgrim59656802015-06-11 07:46:37 +0000325 { ISD::SHL, MVT::v4i32, 2*5 }, // We optimized this using mul.
Simon Pilgrima18ae9b2015-10-17 13:23:38 +0000326 { ISD::SHL, MVT::v8i32, 2*2*5 }, // We optimized this using mul.
Simon Pilgrim59764dc2015-07-18 20:06:30 +0000327 { ISD::SHL, MVT::v2i64, 4 }, // splat+shuffle sequence.
Simon Pilgrima18ae9b2015-10-17 13:23:38 +0000328 { ISD::SHL, MVT::v4i64, 2*4 }, // splat+shuffle sequence.
NAKAMURA Takumi0b305db2015-07-14 04:03:49 +0000329
330 { ISD::SRL, MVT::v16i8, 26 }, // cmpgtb sequence.
Simon Pilgrima18ae9b2015-10-17 13:23:38 +0000331 { ISD::SRL, MVT::v32i8, 2*26 }, // cmpgtb sequence.
NAKAMURA Takumi0b305db2015-07-14 04:03:49 +0000332 { ISD::SRL, MVT::v8i16, 32 }, // cmpgtb sequence.
Simon Pilgrima18ae9b2015-10-17 13:23:38 +0000333 { ISD::SRL, MVT::v16i16, 2*32 }, // cmpgtb sequence.
NAKAMURA Takumi0b305db2015-07-14 04:03:49 +0000334 { ISD::SRL, MVT::v4i32, 16 }, // Shift each lane + blend.
Simon Pilgrima18ae9b2015-10-17 13:23:38 +0000335 { ISD::SRL, MVT::v8i32, 2*16 }, // Shift each lane + blend.
Simon Pilgrim59764dc2015-07-18 20:06:30 +0000336 { ISD::SRL, MVT::v2i64, 4 }, // splat+shuffle sequence.
Simon Pilgrima18ae9b2015-10-17 13:23:38 +0000337 { ISD::SRL, MVT::v4i64, 2*4 }, // splat+shuffle sequence.
NAKAMURA Takumi0b305db2015-07-14 04:03:49 +0000338
339 { ISD::SRA, MVT::v16i8, 54 }, // unpacked cmpgtb sequence.
Simon Pilgrima18ae9b2015-10-17 13:23:38 +0000340 { ISD::SRA, MVT::v32i8, 2*54 }, // unpacked cmpgtb sequence.
NAKAMURA Takumi0b305db2015-07-14 04:03:49 +0000341 { ISD::SRA, MVT::v8i16, 32 }, // cmpgtb sequence.
Simon Pilgrima18ae9b2015-10-17 13:23:38 +0000342 { ISD::SRA, MVT::v16i16, 2*32 }, // cmpgtb sequence.
NAKAMURA Takumi0b305db2015-07-14 04:03:49 +0000343 { ISD::SRA, MVT::v4i32, 16 }, // Shift each lane + blend.
Simon Pilgrima18ae9b2015-10-17 13:23:38 +0000344 { ISD::SRA, MVT::v8i32, 2*16 }, // Shift each lane + blend.
Simon Pilgrim86478c62015-07-29 20:31:45 +0000345 { ISD::SRA, MVT::v2i64, 12 }, // srl/xor/sub sequence.
Simon Pilgrima18ae9b2015-10-17 13:23:38 +0000346 { ISD::SRA, MVT::v4i64, 2*12 }, // srl/xor/sub sequence.
NAKAMURA Takumi0b305db2015-07-14 04:03:49 +0000347
348 // It is not a good idea to vectorize division. We have to scalarize it and
Arnold Schwaighofera04b9ef2013-06-25 19:14:09 +0000349 // in the process we will often end up having to spilling regular
350 // registers. The overhead of division is going to dominate most kernels
351 // anyways so try hard to prevent vectorization of division - it is
352 // generally a bad idea. Assume somewhat arbitrarily that we have to be able
353 // to hide "20 cycles" for each lane.
354 { ISD::SDIV, MVT::v16i8, 16*20 },
355 { ISD::SDIV, MVT::v8i16, 8*20 },
356 { ISD::SDIV, MVT::v4i32, 4*20 },
357 { ISD::SDIV, MVT::v2i64, 2*20 },
358 { ISD::UDIV, MVT::v16i8, 16*20 },
359 { ISD::UDIV, MVT::v8i16, 8*20 },
360 { ISD::UDIV, MVT::v4i32, 4*20 },
361 { ISD::UDIV, MVT::v2i64, 2*20 },
Arnold Schwaighofere9b50162013-04-03 21:46:05 +0000362 };
363
364 if (ST->hasSSE2()) {
Craig Topperee0c8592015-10-27 04:14:24 +0000365 if (const auto *Entry = CostTableLookup(SSE2CostTable, ISD, LT.second))
366 return LT.first * Entry->Cost;
Arnold Schwaighofere9b50162013-04-03 21:46:05 +0000367 }
368
Craig Topper4b275762015-10-28 04:02:12 +0000369 static const CostTblEntry AVX1CostTable[] = {
Renato Goline1fb0592013-01-20 20:57:20 +0000370 // We don't have to scalarize unsupported ops. We can issue two half-sized
371 // operations and we only need to extract the upper YMM half.
372 // Two ops + 1 extract + 1 insert = 4.
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000373 { ISD::MUL, MVT::v16i16, 4 },
Renato Goline1fb0592013-01-20 20:57:20 +0000374 { ISD::MUL, MVT::v8i32, 4 },
375 { ISD::SUB, MVT::v8i32, 4 },
376 { ISD::ADD, MVT::v8i32, 4 },
Renato Goline1fb0592013-01-20 20:57:20 +0000377 { ISD::SUB, MVT::v4i64, 4 },
378 { ISD::ADD, MVT::v4i64, 4 },
Arnold Schwaighofer20ef54f2013-03-02 04:02:52 +0000379 // A v4i64 multiply is custom lowered as two split v2i64 vectors that then
380 // are lowered as a series of long multiplies(3), shifts(4) and adds(2)
381 // Because we believe v4i64 to be a legal type, we must also include the
382 // split factor of two in the cost table. Therefore, the cost here is 18
383 // instead of 9.
384 { ISD::MUL, MVT::v4i64, 18 },
385 };
Chandler Carruth664e3542013-01-07 01:37:14 +0000386
387 // Look for AVX1 lowering tricks.
Arnold Schwaighofer20ef54f2013-03-02 04:02:52 +0000388 if (ST->hasAVX() && !ST->hasAVX2()) {
Craig Toppereda02a92015-10-25 03:15:29 +0000389 MVT VT = LT.second;
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000390
Craig Topperee0c8592015-10-27 04:14:24 +0000391 if (const auto *Entry = CostTableLookup(AVX1CostTable, ISD, VT))
392 return LT.first * Entry->Cost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000393 }
Arnold Schwaighofer20ef54f2013-03-02 04:02:52 +0000394
395 // Custom lowering of vectors.
Craig Topper4b275762015-10-28 04:02:12 +0000396 static const CostTblEntry CustomLowered[] = {
Arnold Schwaighofer20ef54f2013-03-02 04:02:52 +0000397 // A v2i64/v4i64 and multiply is custom lowered as a series of long
398 // multiplies(3), shifts(4) and adds(2).
399 { ISD::MUL, MVT::v2i64, 9 },
400 { ISD::MUL, MVT::v4i64, 9 },
401 };
Craig Topperee0c8592015-10-27 04:14:24 +0000402 if (const auto *Entry = CostTableLookup(CustomLowered, ISD, LT.second))
403 return LT.first * Entry->Cost;
Arnold Schwaighofer20ef54f2013-03-02 04:02:52 +0000404
405 // Special lowering of v4i32 mul on sse2, sse3: Lower v4i32 mul as 2x shuffle,
406 // 2x pmuludq, 2x shuffle.
407 if (ISD == ISD::MUL && LT.second == MVT::v4i32 && ST->hasSSE2() &&
408 !ST->hasSSE41())
Andrea Di Biagiob7882b32014-02-12 23:43:47 +0000409 return LT.first * 6;
Arnold Schwaighofer20ef54f2013-03-02 04:02:52 +0000410
Chandler Carruth664e3542013-01-07 01:37:14 +0000411 // Fallback to the default implementation.
Chandler Carruth705b1852015-01-31 03:43:40 +0000412 return BaseT::getArithmeticInstrCost(Opcode, Ty, Op1Info, Op2Info);
Chandler Carruth664e3542013-01-07 01:37:14 +0000413}
414
Chandler Carruth93205eb2015-08-05 18:08:10 +0000415int X86TTIImpl::getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index,
416 Type *SubTp) {
Karthik Bhate03a25d2014-06-20 04:32:48 +0000417 // We only estimate the cost of reverse and alternate shuffles.
Chandler Carruth705b1852015-01-31 03:43:40 +0000418 if (Kind != TTI::SK_Reverse && Kind != TTI::SK_Alternate)
419 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
Chandler Carruth664e3542013-01-07 01:37:14 +0000420
Chandler Carruth705b1852015-01-31 03:43:40 +0000421 if (Kind == TTI::SK_Reverse) {
Chandler Carruth93205eb2015-08-05 18:08:10 +0000422 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp);
423 int Cost = 1;
Karthik Bhate03a25d2014-06-20 04:32:48 +0000424 if (LT.second.getSizeInBits() > 128)
425 Cost = 3; // Extract + insert + copy.
Chandler Carruth664e3542013-01-07 01:37:14 +0000426
Karthik Bhate03a25d2014-06-20 04:32:48 +0000427 // Multiple by the number of parts.
428 return Cost * LT.first;
429 }
430
Chandler Carruth705b1852015-01-31 03:43:40 +0000431 if (Kind == TTI::SK_Alternate) {
Andrea Di Biagioc8e8bda2014-07-03 22:24:18 +0000432 // 64-bit packed float vectors (v2f32) are widened to type v4f32.
433 // 64-bit packed integer vectors (v2i32) are promoted to type v2i64.
Chandler Carruth93205eb2015-08-05 18:08:10 +0000434 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp);
Karthik Bhate03a25d2014-06-20 04:32:48 +0000435
Andrea Di Biagioc8e8bda2014-07-03 22:24:18 +0000436 // The backend knows how to generate a single VEX.256 version of
437 // instruction VPBLENDW if the target supports AVX2.
438 if (ST->hasAVX2() && LT.second == MVT::v16i16)
439 return LT.first;
440
Craig Topper4b275762015-10-28 04:02:12 +0000441 static const CostTblEntry AVXAltShuffleTbl[] = {
Andrea Di Biagioc8e8bda2014-07-03 22:24:18 +0000442 {ISD::VECTOR_SHUFFLE, MVT::v4i64, 1}, // vblendpd
443 {ISD::VECTOR_SHUFFLE, MVT::v4f64, 1}, // vblendpd
444
445 {ISD::VECTOR_SHUFFLE, MVT::v8i32, 1}, // vblendps
446 {ISD::VECTOR_SHUFFLE, MVT::v8f32, 1}, // vblendps
447
448 // This shuffle is custom lowered into a sequence of:
449 // 2x vextractf128 , 2x vpblendw , 1x vinsertf128
450 {ISD::VECTOR_SHUFFLE, MVT::v16i16, 5},
451
452 // This shuffle is custom lowered into a long sequence of:
453 // 2x vextractf128 , 4x vpshufb , 2x vpor , 1x vinsertf128
454 {ISD::VECTOR_SHUFFLE, MVT::v32i8, 9}
455 };
456
Craig Topperee0c8592015-10-27 04:14:24 +0000457 if (ST->hasAVX())
458 if (const auto *Entry = CostTableLookup(AVXAltShuffleTbl,
459 ISD::VECTOR_SHUFFLE, LT.second))
460 return LT.first * Entry->Cost;
Andrea Di Biagioc8e8bda2014-07-03 22:24:18 +0000461
Craig Topper4b275762015-10-28 04:02:12 +0000462 static const CostTblEntry SSE41AltShuffleTbl[] = {
Andrea Di Biagioc8e8bda2014-07-03 22:24:18 +0000463 // These are lowered into movsd.
464 {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1},
465 {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1},
466
467 // packed float vectors with four elements are lowered into BLENDI dag
468 // nodes. A v4i32/v4f32 BLENDI generates a single 'blendps'/'blendpd'.
469 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 1},
470 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 1},
471
472 // This shuffle generates a single pshufw.
473 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 1},
474
475 // There is no instruction that matches a v16i8 alternate shuffle.
476 // The backend will expand it into the sequence 'pshufb + pshufb + or'.
477 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 3}
478 };
479
Craig Topperee0c8592015-10-27 04:14:24 +0000480 if (ST->hasSSE41())
481 if (const auto *Entry = CostTableLookup(SSE41AltShuffleTbl, ISD::VECTOR_SHUFFLE,
482 LT.second))
483 return LT.first * Entry->Cost;
Andrea Di Biagioc8e8bda2014-07-03 22:24:18 +0000484
Craig Topper4b275762015-10-28 04:02:12 +0000485 static const CostTblEntry SSSE3AltShuffleTbl[] = {
Andrea Di Biagioc8e8bda2014-07-03 22:24:18 +0000486 {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1}, // movsd
487 {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1}, // movsd
488
489 // SSE3 doesn't have 'blendps'. The following shuffles are expanded into
490 // the sequence 'shufps + pshufd'
491 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 2},
492 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 2},
493
494 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 3}, // pshufb + pshufb + or
495 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 3} // pshufb + pshufb + or
496 };
Michael Liao5bf95782014-12-04 05:20:33 +0000497
Craig Topperee0c8592015-10-27 04:14:24 +0000498 if (ST->hasSSSE3())
499 if (const auto *Entry = CostTableLookup(SSSE3AltShuffleTbl,
500 ISD::VECTOR_SHUFFLE, LT.second))
501 return LT.first * Entry->Cost;
Andrea Di Biagioc8e8bda2014-07-03 22:24:18 +0000502
Craig Topper4b275762015-10-28 04:02:12 +0000503 static const CostTblEntry SSEAltShuffleTbl[] = {
Andrea Di Biagioc8e8bda2014-07-03 22:24:18 +0000504 {ISD::VECTOR_SHUFFLE, MVT::v2i64, 1}, // movsd
505 {ISD::VECTOR_SHUFFLE, MVT::v2f64, 1}, // movsd
506
507 {ISD::VECTOR_SHUFFLE, MVT::v4i32, 2}, // shufps + pshufd
508 {ISD::VECTOR_SHUFFLE, MVT::v4f32, 2}, // shufps + pshufd
Michael Liao5bf95782014-12-04 05:20:33 +0000509
Andrea Di Biagioc8e8bda2014-07-03 22:24:18 +0000510 // This is expanded into a long sequence of four extract + four insert.
511 {ISD::VECTOR_SHUFFLE, MVT::v8i16, 8}, // 4 x pextrw + 4 pinsrw.
512
513 // 8 x (pinsrw + pextrw + and + movb + movzb + or)
514 {ISD::VECTOR_SHUFFLE, MVT::v16i8, 48}
515 };
516
Michael Liao5bf95782014-12-04 05:20:33 +0000517 // Fall-back (SSE3 and SSE2).
Craig Topperee0c8592015-10-27 04:14:24 +0000518 if (const auto *Entry = CostTableLookup(SSEAltShuffleTbl,
519 ISD::VECTOR_SHUFFLE, LT.second))
520 return LT.first * Entry->Cost;
Chandler Carruth705b1852015-01-31 03:43:40 +0000521 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
Karthik Bhate03a25d2014-06-20 04:32:48 +0000522 }
523
Chandler Carruth705b1852015-01-31 03:43:40 +0000524 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
Chandler Carruth664e3542013-01-07 01:37:14 +0000525}
526
Chandler Carruth93205eb2015-08-05 18:08:10 +0000527int X86TTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) {
Chandler Carruth664e3542013-01-07 01:37:14 +0000528 int ISD = TLI->InstructionOpcodeToISD(Opcode);
529 assert(ISD && "Invalid opcode");
530
Cong Hou59898d82015-12-11 00:31:39 +0000531 // FIXME: Need a better design of the cost table to handle non-simple types of
532 // potential massive combinations (elem_num x src_type x dst_type).
533
Elena Demikhovskya1a40cc2015-12-02 08:59:47 +0000534 static const TypeConversionCostTblEntry AVX512DQConversionTbl[] = {
535 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 1 },
536 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i64, 1 },
537 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i64, 1 },
538 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i64, 1 },
539 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i64, 1 },
540 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i64, 1 },
541
542 { ISD::FP_TO_UINT, MVT::v2i64, MVT::v2f64, 1 },
543 { ISD::FP_TO_UINT, MVT::v4i64, MVT::v4f64, 1 },
544 { ISD::FP_TO_UINT, MVT::v8i64, MVT::v8f64, 1 },
545 { ISD::FP_TO_UINT, MVT::v2i64, MVT::v2f32, 1 },
546 { ISD::FP_TO_UINT, MVT::v4i64, MVT::v4f32, 1 },
547 { ISD::FP_TO_UINT, MVT::v8i64, MVT::v8f32, 1 },
548 };
549
550 static const TypeConversionCostTblEntry AVX512FConversionTbl[] = {
Elena Demikhovsky27012472014-09-16 07:57:37 +0000551 { ISD::FP_EXTEND, MVT::v8f64, MVT::v8f32, 1 },
552 { ISD::FP_EXTEND, MVT::v8f64, MVT::v16f32, 3 },
553 { ISD::FP_ROUND, MVT::v8f32, MVT::v8f64, 1 },
Elena Demikhovsky27012472014-09-16 07:57:37 +0000554
555 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i32, 1 },
556 { ISD::TRUNCATE, MVT::v16i16, MVT::v16i32, 1 },
557 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i64, 1 },
558 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 1 },
Elena Demikhovsky27012472014-09-16 07:57:37 +0000559
560 // v16i1 -> v16i32 - load + broadcast
561 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i1, 2 },
562 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i1, 2 },
563
564 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i8, 1 },
565 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i8, 1 },
566 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i16, 1 },
567 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i16, 1 },
Elena Demikhovskya1a40cc2015-12-02 08:59:47 +0000568 { ISD::SIGN_EXTEND, MVT::v8i64, MVT::v8i32, 1 },
569 { ISD::ZERO_EXTEND, MVT::v8i64, MVT::v8i32, 1 },
570 { ISD::ZERO_EXTEND, MVT::v8i64, MVT::v8i16, 1 },
571 { ISD::SIGN_EXTEND, MVT::v8i64, MVT::v8i16, 1 },
Elena Demikhovsky27012472014-09-16 07:57:37 +0000572
Elena Demikhovskyd5e95b52014-11-13 11:46:16 +0000573 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i1, 3 },
574 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i8, 2 },
575 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i16, 2 },
576 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i32, 1 },
577 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i1, 4 },
Elena Demikhovskya1a40cc2015-12-02 08:59:47 +0000578 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i8, 2 },
Elena Demikhovskyd5e95b52014-11-13 11:46:16 +0000579 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i16, 2 },
580 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i32, 1 },
Elena Demikhovskya1a40cc2015-12-02 08:59:47 +0000581
582 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i1, 3 },
583 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i8, 2 },
584 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i16, 2 },
585 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i32, 1 },
586 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 1 },
587 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 },
588 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i1, 4 },
589 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i16, 2 },
590 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i32, 1 },
591 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i8, 2 },
592 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i8, 2 },
593 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i16, 2 },
594 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i8, 2 },
595 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i16, 2 },
596 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i32, 1 },
597 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i8, 2 },
598 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i16, 5 },
599 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i32, 2 },
600 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 5 },
601 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i64, 12 },
602 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i64, 26 },
603
604 { ISD::FP_TO_UINT, MVT::v2i32, MVT::v2f32, 1 },
605 { ISD::FP_TO_UINT, MVT::v4i32, MVT::v4f32, 1 },
606 { ISD::FP_TO_UINT, MVT::v8i32, MVT::v8f32, 1 },
607 { ISD::FP_TO_UINT, MVT::v16i32, MVT::v16f32, 1 },
Elena Demikhovsky27012472014-09-16 07:57:37 +0000608 };
609
Craig Topper4b275762015-10-28 04:02:12 +0000610 static const TypeConversionCostTblEntry AVX2ConversionTbl[] = {
Tim Northoverf0e21612014-02-06 18:18:36 +0000611 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 1 },
612 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 1 },
613 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 3 },
614 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 3 },
615 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 3 },
616 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 3 },
617 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
618 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
619 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i1, 3 },
620 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i1, 3 },
621 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 3 },
622 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 3 },
623 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
624 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
625 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
626 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
627
628 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i64, 2 },
629 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i64, 2 },
630 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 2 },
631 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 2 },
632 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 2 },
633 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 4 },
Elena Demikhovsky27012472014-09-16 07:57:37 +0000634
635 { ISD::FP_EXTEND, MVT::v8f64, MVT::v8f32, 3 },
636 { ISD::FP_ROUND, MVT::v8f32, MVT::v8f64, 3 },
Quentin Colombet360460b2014-11-11 02:23:47 +0000637
638 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 8 },
Tim Northoverf0e21612014-02-06 18:18:36 +0000639 };
640
Craig Topper4b275762015-10-28 04:02:12 +0000641 static const TypeConversionCostTblEntry AVXConversionTbl[] = {
Tim Northoverf0e21612014-02-06 18:18:36 +0000642 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 4 },
643 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 4 },
644 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 7 },
645 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 4 },
646 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 7 },
647 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 4 },
648 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 4 },
649 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 4 },
650 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i1, 6 },
651 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i1, 4 },
652 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 6 },
653 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 4 },
654 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 6 },
655 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
656 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 4 },
657 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 4 },
658
659 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i64, 4 },
660 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i64, 4 },
661 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 4 },
662 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 4 },
663 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 5 },
664 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i16, 4 },
665 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 9 },
Benjamin Kramer52ceb442013-04-01 10:23:49 +0000666
667 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i1, 8 },
668 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i8, 8 },
669 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i16, 5 },
670 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i32, 1 },
671 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i1, 3 },
672 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i8, 3 },
673 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i16, 3 },
674 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 },
675 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i1, 3 },
676 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i8, 3 },
677 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i16, 3 },
678 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i32, 1 },
679
680 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i1, 6 },
681 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i8, 5 },
682 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i16, 5 },
683 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 9 },
684 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i1, 7 },
685 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i8, 2 },
686 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i16, 2 },
687 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 6 },
688 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i1, 7 },
689 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i8, 2 },
690 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i16, 2 },
691 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i32, 6 },
Quentin Colombet85b904d2014-03-27 22:27:41 +0000692 // The generic code to compute the scalar overhead is currently broken.
693 // Workaround this limitation by estimating the scalarization overhead
694 // here. We have roughly 10 instructions per scalar element.
695 // Multiply that by the vector width.
696 // FIXME: remove that when PR19268 is fixed.
Quentin Colombet3914bf52014-03-27 00:52:16 +0000697 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 },
698 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i64, 4*10 },
Benjamin Kramer52ceb442013-04-01 10:23:49 +0000699
Jim Grosbach72fbde82014-03-27 00:04:11 +0000700 { ISD::FP_TO_SINT, MVT::v8i8, MVT::v8f32, 7 },
Renato Goline1fb0592013-01-20 20:57:20 +0000701 { ISD::FP_TO_SINT, MVT::v4i8, MVT::v4f32, 1 },
Adam Nemet6dafe972014-03-30 18:07:13 +0000702 // This node is expanded into scalarized operations but BasicTTI is overly
703 // optimistic estimating its cost. It computes 3 per element (one
704 // vector-extract, one scalar conversion and one vector-insert). The
705 // problem is that the inserts form a read-modify-write chain so latency
706 // should be factored in too. Inflating the cost per element by 1.
707 { ISD::FP_TO_UINT, MVT::v8i32, MVT::v8f32, 8*4 },
Adam Nemet10c4ce22014-03-31 21:54:48 +0000708 { ISD::FP_TO_UINT, MVT::v4i32, MVT::v4f64, 4*4 },
Chandler Carruth664e3542013-01-07 01:37:14 +0000709 };
710
Cong Hou59898d82015-12-11 00:31:39 +0000711 static const TypeConversionCostTblEntry SSE41ConversionTbl[] = {
712 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i16, 4 },
713 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i16, 4 },
714 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 2 },
715 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 2 },
716 { ISD::ZERO_EXTEND, MVT::v4i32, MVT::v4i16, 1 },
717 { ISD::SIGN_EXTEND, MVT::v4i32, MVT::v4i16, 1 },
718 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i8, 4 },
719 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i8, 4 },
720 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 2 },
721 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 2 },
722 { ISD::ZERO_EXTEND, MVT::v4i32, MVT::v4i8, 1 },
723 { ISD::SIGN_EXTEND, MVT::v4i32, MVT::v4i8, 1 },
724 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 2 },
725 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 2 },
726 { ISD::ZERO_EXTEND, MVT::v8i16, MVT::v8i8, 1 },
727 { ISD::SIGN_EXTEND, MVT::v8i16, MVT::v8i8, 1 },
728 { ISD::ZERO_EXTEND, MVT::v4i16, MVT::v4i8, 1 },
729 { ISD::SIGN_EXTEND, MVT::v4i16, MVT::v4i8, 2 },
730
731 { ISD::TRUNCATE, MVT::v16i16, MVT::v16i32, 6 },
732 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 3 },
733 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i32, 1 },
Cong Hou59898d82015-12-11 00:31:39 +0000734 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 3 },
735 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i32, 1 },
Cong Hou59898d82015-12-11 00:31:39 +0000736 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i16, 1 },
737 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i16, 2 },
738 };
739
740 static const TypeConversionCostTblEntry SSE2ConversionTbl[] = {
Simon Pilgrime2c244f2015-07-19 15:36:12 +0000741 // These are somewhat magic numbers justified by looking at the output of
742 // Intel's IACA, running some kernels and making sure when we take
743 // legalization into account the throughput will be overestimated.
744 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 },
745 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 },
746 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 },
747 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 },
748 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 },
749 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 },
750 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 },
751 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 },
752 // There are faster sequences for float conversions.
753 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 },
754 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 8 },
755 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 },
756 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 },
757 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 },
758 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 15 },
759 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 },
760 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 },
Cong Hou59898d82015-12-11 00:31:39 +0000761
762 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i16, 6 },
763 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i16, 8 },
764 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 3 },
765 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 4 },
766 { ISD::ZERO_EXTEND, MVT::v4i32, MVT::v4i16, 1 },
767 { ISD::SIGN_EXTEND, MVT::v4i32, MVT::v4i16, 2 },
768 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i8, 9 },
769 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i8, 12 },
770 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 6 },
771 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 6 },
772 { ISD::ZERO_EXTEND, MVT::v4i32, MVT::v4i8, 2 },
773 { ISD::SIGN_EXTEND, MVT::v4i32, MVT::v4i8, 3 },
774 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 3 },
775 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 4 },
776 { ISD::ZERO_EXTEND, MVT::v8i16, MVT::v8i8, 1 },
777 { ISD::SIGN_EXTEND, MVT::v8i16, MVT::v8i8, 2 },
778 { ISD::ZERO_EXTEND, MVT::v4i16, MVT::v4i8, 1 },
779 { ISD::SIGN_EXTEND, MVT::v4i16, MVT::v4i8, 6 },
780
Cong Hou8df93ce2015-12-21 20:42:43 +0000781 { ISD::TRUNCATE, MVT::v16i16, MVT::v16i32, 10 },
782 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 5 },
Cong Hou59898d82015-12-11 00:31:39 +0000783 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i32, 3 },
Cong Hou8df93ce2015-12-21 20:42:43 +0000784 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i32, 7 },
Cong Hou59898d82015-12-11 00:31:39 +0000785 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 4 },
786 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i32, 3 },
787 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i16, 3 },
788 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i16, 2 },
789 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i16, 4 },
Simon Pilgrime2c244f2015-07-19 15:36:12 +0000790 };
791
Chandler Carruth93205eb2015-08-05 18:08:10 +0000792 std::pair<int, MVT> LTSrc = TLI->getTypeLegalizationCost(DL, Src);
793 std::pair<int, MVT> LTDest = TLI->getTypeLegalizationCost(DL, Dst);
Simon Pilgrime2c244f2015-07-19 15:36:12 +0000794
795 if (ST->hasSSE2() && !ST->hasAVX()) {
Cong Hou59898d82015-12-11 00:31:39 +0000796 if (const auto *Entry = ConvertCostTableLookup(SSE2ConversionTbl, ISD,
Craig Topperee0c8592015-10-27 04:14:24 +0000797 LTDest.second, LTSrc.second))
798 return LTSrc.first * Entry->Cost;
Simon Pilgrime2c244f2015-07-19 15:36:12 +0000799 }
800
Simon Pilgrime2c244f2015-07-19 15:36:12 +0000801 EVT SrcTy = TLI->getValueType(DL, Src);
802 EVT DstTy = TLI->getValueType(DL, Dst);
803
804 // The function getSimpleVT only handles simple value types.
805 if (!SrcTy.isSimple() || !DstTy.isSimple())
806 return BaseT::getCastInstrCost(Opcode, Dst, Src);
807
Elena Demikhovskya1a40cc2015-12-02 08:59:47 +0000808 if (ST->hasDQI())
809 if (const auto *Entry = ConvertCostTableLookup(AVX512DQConversionTbl, ISD,
810 DstTy.getSimpleVT(),
811 SrcTy.getSimpleVT()))
812 return Entry->Cost;
813
814 if (ST->hasAVX512())
815 if (const auto *Entry = ConvertCostTableLookup(AVX512FConversionTbl, ISD,
816 DstTy.getSimpleVT(),
817 SrcTy.getSimpleVT()))
818 return Entry->Cost;
819
Tim Northoverf0e21612014-02-06 18:18:36 +0000820 if (ST->hasAVX2()) {
Craig Topperee0c8592015-10-27 04:14:24 +0000821 if (const auto *Entry = ConvertCostTableLookup(AVX2ConversionTbl, ISD,
822 DstTy.getSimpleVT(),
823 SrcTy.getSimpleVT()))
824 return Entry->Cost;
Tim Northoverf0e21612014-02-06 18:18:36 +0000825 }
826
Chandler Carruth664e3542013-01-07 01:37:14 +0000827 if (ST->hasAVX()) {
Craig Topperee0c8592015-10-27 04:14:24 +0000828 if (const auto *Entry = ConvertCostTableLookup(AVXConversionTbl, ISD,
829 DstTy.getSimpleVT(),
830 SrcTy.getSimpleVT()))
831 return Entry->Cost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000832 }
833
Cong Hou59898d82015-12-11 00:31:39 +0000834 if (ST->hasSSE41()) {
835 if (const auto *Entry = ConvertCostTableLookup(SSE41ConversionTbl, ISD,
836 DstTy.getSimpleVT(),
837 SrcTy.getSimpleVT()))
838 return Entry->Cost;
839 }
840
841 if (ST->hasSSE2()) {
842 if (const auto *Entry = ConvertCostTableLookup(SSE2ConversionTbl, ISD,
843 DstTy.getSimpleVT(),
844 SrcTy.getSimpleVT()))
845 return Entry->Cost;
846 }
847
Chandler Carruth705b1852015-01-31 03:43:40 +0000848 return BaseT::getCastInstrCost(Opcode, Dst, Src);
Chandler Carruth664e3542013-01-07 01:37:14 +0000849}
850
Chandler Carruth93205eb2015-08-05 18:08:10 +0000851int X86TTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy) {
Chandler Carruth664e3542013-01-07 01:37:14 +0000852 // Legalize the type.
Chandler Carruth93205eb2015-08-05 18:08:10 +0000853 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, ValTy);
Chandler Carruth664e3542013-01-07 01:37:14 +0000854
855 MVT MTy = LT.second;
856
857 int ISD = TLI->InstructionOpcodeToISD(Opcode);
858 assert(ISD && "Invalid opcode");
859
Simon Pilgrimeec3a952016-05-09 21:14:38 +0000860 static const CostTblEntry SSE2CostTbl[] = {
861 { ISD::SETCC, MVT::v2i64, 8 },
862 { ISD::SETCC, MVT::v4i32, 1 },
863 { ISD::SETCC, MVT::v8i16, 1 },
864 { ISD::SETCC, MVT::v16i8, 1 },
865 };
866
Craig Topper4b275762015-10-28 04:02:12 +0000867 static const CostTblEntry SSE42CostTbl[] = {
Renato Goline1fb0592013-01-20 20:57:20 +0000868 { ISD::SETCC, MVT::v2f64, 1 },
869 { ISD::SETCC, MVT::v4f32, 1 },
870 { ISD::SETCC, MVT::v2i64, 1 },
Chandler Carruth664e3542013-01-07 01:37:14 +0000871 };
872
Craig Topper4b275762015-10-28 04:02:12 +0000873 static const CostTblEntry AVX1CostTbl[] = {
Renato Goline1fb0592013-01-20 20:57:20 +0000874 { ISD::SETCC, MVT::v4f64, 1 },
875 { ISD::SETCC, MVT::v8f32, 1 },
Chandler Carruth664e3542013-01-07 01:37:14 +0000876 // AVX1 does not support 8-wide integer compare.
Renato Goline1fb0592013-01-20 20:57:20 +0000877 { ISD::SETCC, MVT::v4i64, 4 },
878 { ISD::SETCC, MVT::v8i32, 4 },
879 { ISD::SETCC, MVT::v16i16, 4 },
880 { ISD::SETCC, MVT::v32i8, 4 },
Chandler Carruth664e3542013-01-07 01:37:14 +0000881 };
882
Craig Topper4b275762015-10-28 04:02:12 +0000883 static const CostTblEntry AVX2CostTbl[] = {
Renato Goline1fb0592013-01-20 20:57:20 +0000884 { ISD::SETCC, MVT::v4i64, 1 },
885 { ISD::SETCC, MVT::v8i32, 1 },
886 { ISD::SETCC, MVT::v16i16, 1 },
887 { ISD::SETCC, MVT::v32i8, 1 },
Chandler Carruth664e3542013-01-07 01:37:14 +0000888 };
889
Craig Topper4b275762015-10-28 04:02:12 +0000890 static const CostTblEntry AVX512CostTbl[] = {
Elena Demikhovsky27012472014-09-16 07:57:37 +0000891 { ISD::SETCC, MVT::v8i64, 1 },
892 { ISD::SETCC, MVT::v16i32, 1 },
893 { ISD::SETCC, MVT::v8f64, 1 },
894 { ISD::SETCC, MVT::v16f32, 1 },
895 };
896
Craig Topperee0c8592015-10-27 04:14:24 +0000897 if (ST->hasAVX512())
898 if (const auto *Entry = CostTableLookup(AVX512CostTbl, ISD, MTy))
899 return LT.first * Entry->Cost;
Elena Demikhovsky27012472014-09-16 07:57:37 +0000900
Craig Topperee0c8592015-10-27 04:14:24 +0000901 if (ST->hasAVX2())
902 if (const auto *Entry = CostTableLookup(AVX2CostTbl, ISD, MTy))
903 return LT.first * Entry->Cost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000904
Craig Topperee0c8592015-10-27 04:14:24 +0000905 if (ST->hasAVX())
906 if (const auto *Entry = CostTableLookup(AVX1CostTbl, ISD, MTy))
907 return LT.first * Entry->Cost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000908
Craig Topperee0c8592015-10-27 04:14:24 +0000909 if (ST->hasSSE42())
910 if (const auto *Entry = CostTableLookup(SSE42CostTbl, ISD, MTy))
911 return LT.first * Entry->Cost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000912
Simon Pilgrimeec3a952016-05-09 21:14:38 +0000913 if (ST->hasSSE2())
914 if (const auto *Entry = CostTableLookup(SSE2CostTbl, ISD, MTy))
915 return LT.first * Entry->Cost;
916
Chandler Carruth705b1852015-01-31 03:43:40 +0000917 return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy);
Chandler Carruth664e3542013-01-07 01:37:14 +0000918}
919
Simon Pilgrim14000b32016-05-24 08:17:50 +0000920int X86TTIImpl::getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy,
921 ArrayRef<Type *> Tys, FastMathFlags FMF) {
922 static const CostTblEntry XOPCostTbl[] = {
923 { ISD::BITREVERSE, MVT::v4i64, 4 },
924 { ISD::BITREVERSE, MVT::v8i32, 4 },
925 { ISD::BITREVERSE, MVT::v16i16, 4 },
926 { ISD::BITREVERSE, MVT::v32i8, 4 },
927 { ISD::BITREVERSE, MVT::v2i64, 1 },
928 { ISD::BITREVERSE, MVT::v4i32, 1 },
929 { ISD::BITREVERSE, MVT::v8i16, 1 },
930 { ISD::BITREVERSE, MVT::v16i8, 1 },
931 { ISD::BITREVERSE, MVT::i64, 3 },
932 { ISD::BITREVERSE, MVT::i32, 3 },
933 { ISD::BITREVERSE, MVT::i16, 3 },
934 { ISD::BITREVERSE, MVT::i8, 3 }
935 };
936
937 unsigned ISD = ISD::DELETED_NODE;
938 switch (IID) {
939 default:
940 break;
941 case Intrinsic::bitreverse:
942 ISD = ISD::BITREVERSE;
943 break;
944 }
945
946 // Legalize the type.
947 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, RetTy);
948 MVT MTy = LT.second;
949
950 // Attempt to lookup cost.
951 if (ST->hasXOP())
952 if (const auto *Entry = CostTableLookup(XOPCostTbl, ISD, MTy))
953 return LT.first * Entry->Cost;
954
955 return BaseT::getIntrinsicInstrCost(IID, RetTy, Tys, FMF);
956}
957
958int X86TTIImpl::getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy,
959 ArrayRef<Value *> Args, FastMathFlags FMF) {
960 return BaseT::getIntrinsicInstrCost(IID, RetTy, Args, FMF);
961}
962
Chandler Carruth93205eb2015-08-05 18:08:10 +0000963int X86TTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index) {
Chandler Carruth664e3542013-01-07 01:37:14 +0000964 assert(Val->isVectorTy() && "This must be a vector type");
965
Sanjay Patelaedc3472016-05-25 17:27:54 +0000966 Type *ScalarType = Val->getScalarType();
967
Chandler Carruth664e3542013-01-07 01:37:14 +0000968 if (Index != -1U) {
969 // Legalize the type.
Chandler Carruth93205eb2015-08-05 18:08:10 +0000970 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Val);
Chandler Carruth664e3542013-01-07 01:37:14 +0000971
972 // This type is legalized to a scalar type.
973 if (!LT.second.isVector())
974 return 0;
975
976 // The type may be split. Normalize the index to the new type.
977 unsigned Width = LT.second.getVectorNumElements();
978 Index = Index % Width;
979
980 // Floating point scalars are already located in index #0.
Sanjay Patelaedc3472016-05-25 17:27:54 +0000981 if (ScalarType->isFloatingPointTy() && Index == 0)
Chandler Carruth664e3542013-01-07 01:37:14 +0000982 return 0;
983 }
984
Sanjay Patelaedc3472016-05-25 17:27:54 +0000985 // Add to the base cost if we know that the extracted element of a vector is
986 // destined to be moved to and used in the integer register file.
987 int RegisterFileMoveCost = 0;
988 if (Opcode == Instruction::ExtractElement && ScalarType->isPointerTy())
989 RegisterFileMoveCost = 1;
990
991 return BaseT::getVectorInstrCost(Opcode, Val, Index) + RegisterFileMoveCost;
Chandler Carruth664e3542013-01-07 01:37:14 +0000992}
993
Chandler Carruth93205eb2015-08-05 18:08:10 +0000994int X86TTIImpl::getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) {
Nadav Rotemf9ecbcb2013-06-27 17:52:04 +0000995 assert (Ty->isVectorTy() && "Can only scalarize vectors");
Chandler Carruth93205eb2015-08-05 18:08:10 +0000996 int Cost = 0;
Nadav Rotemf9ecbcb2013-06-27 17:52:04 +0000997
998 for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
999 if (Insert)
Chandler Carruth705b1852015-01-31 03:43:40 +00001000 Cost += getVectorInstrCost(Instruction::InsertElement, Ty, i);
Nadav Rotemf9ecbcb2013-06-27 17:52:04 +00001001 if (Extract)
Chandler Carruth705b1852015-01-31 03:43:40 +00001002 Cost += getVectorInstrCost(Instruction::ExtractElement, Ty, i);
Nadav Rotemf9ecbcb2013-06-27 17:52:04 +00001003 }
1004
1005 return Cost;
1006}
1007
Chandler Carruth93205eb2015-08-05 18:08:10 +00001008int X86TTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
1009 unsigned AddressSpace) {
Alp Tokerf907b892013-12-05 05:44:44 +00001010 // Handle non-power-of-two vectors such as <3 x float>
Nadav Rotemf9ecbcb2013-06-27 17:52:04 +00001011 if (VectorType *VTy = dyn_cast<VectorType>(Src)) {
1012 unsigned NumElem = VTy->getVectorNumElements();
1013
1014 // Handle a few common cases:
1015 // <3 x float>
1016 if (NumElem == 3 && VTy->getScalarSizeInBits() == 32)
1017 // Cost = 64 bit store + extract + 32 bit store.
1018 return 3;
1019
1020 // <3 x double>
1021 if (NumElem == 3 && VTy->getScalarSizeInBits() == 64)
1022 // Cost = 128 bit store + unpack + 64 bit store.
1023 return 3;
1024
Alp Tokerf907b892013-12-05 05:44:44 +00001025 // Assume that all other non-power-of-two numbers are scalarized.
Nadav Rotemf9ecbcb2013-06-27 17:52:04 +00001026 if (!isPowerOf2_32(NumElem)) {
Chandler Carruth93205eb2015-08-05 18:08:10 +00001027 int Cost = BaseT::getMemoryOpCost(Opcode, VTy->getScalarType(), Alignment,
1028 AddressSpace);
1029 int SplitCost = getScalarizationOverhead(Src, Opcode == Instruction::Load,
1030 Opcode == Instruction::Store);
Nadav Rotemf9ecbcb2013-06-27 17:52:04 +00001031 return NumElem * Cost + SplitCost;
1032 }
1033 }
1034
Chandler Carruth664e3542013-01-07 01:37:14 +00001035 // Legalize the type.
Chandler Carruth93205eb2015-08-05 18:08:10 +00001036 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Src);
Chandler Carruth664e3542013-01-07 01:37:14 +00001037 assert((Opcode == Instruction::Load || Opcode == Instruction::Store) &&
1038 "Invalid Opcode");
1039
1040 // Each load/store unit costs 1.
Chandler Carruth93205eb2015-08-05 18:08:10 +00001041 int Cost = LT.first * 1;
Chandler Carruth664e3542013-01-07 01:37:14 +00001042
Sanjay Patel9f6c4d52016-03-09 22:23:33 +00001043 // This isn't exactly right. We're using slow unaligned 32-byte accesses as a
1044 // proxy for a double-pumped AVX memory interface such as on Sandybridge.
1045 if (LT.second.getStoreSize() == 32 && ST->isUnalignedMem32Slow())
1046 Cost *= 2;
Chandler Carruth664e3542013-01-07 01:37:14 +00001047
1048 return Cost;
1049}
Arnold Schwaighofer6042a262013-07-12 19:16:07 +00001050
Chandler Carruth93205eb2015-08-05 18:08:10 +00001051int X86TTIImpl::getMaskedMemoryOpCost(unsigned Opcode, Type *SrcTy,
1052 unsigned Alignment,
1053 unsigned AddressSpace) {
Elena Demikhovskya3232f72015-01-25 08:44:46 +00001054 VectorType *SrcVTy = dyn_cast<VectorType>(SrcTy);
1055 if (!SrcVTy)
1056 // To calculate scalar take the regular cost, without mask
1057 return getMemoryOpCost(Opcode, SrcTy, Alignment, AddressSpace);
1058
1059 unsigned NumElem = SrcVTy->getVectorNumElements();
1060 VectorType *MaskTy =
Mehdi Amini867e9142016-04-14 04:36:40 +00001061 VectorType::get(Type::getInt8Ty(SrcVTy->getContext()), NumElem);
Elena Demikhovsky20662e32015-10-19 07:43:38 +00001062 if ((Opcode == Instruction::Load && !isLegalMaskedLoad(SrcVTy)) ||
1063 (Opcode == Instruction::Store && !isLegalMaskedStore(SrcVTy)) ||
Elena Demikhovskya3232f72015-01-25 08:44:46 +00001064 !isPowerOf2_32(NumElem)) {
1065 // Scalarization
Chandler Carruth93205eb2015-08-05 18:08:10 +00001066 int MaskSplitCost = getScalarizationOverhead(MaskTy, false, true);
1067 int ScalarCompareCost = getCmpSelInstrCost(
Mehdi Amini867e9142016-04-14 04:36:40 +00001068 Instruction::ICmp, Type::getInt8Ty(SrcVTy->getContext()), nullptr);
Chandler Carruth93205eb2015-08-05 18:08:10 +00001069 int BranchCost = getCFInstrCost(Instruction::Br);
1070 int MaskCmpCost = NumElem * (BranchCost + ScalarCompareCost);
Elena Demikhovskya3232f72015-01-25 08:44:46 +00001071
Chandler Carruth93205eb2015-08-05 18:08:10 +00001072 int ValueSplitCost = getScalarizationOverhead(
1073 SrcVTy, Opcode == Instruction::Load, Opcode == Instruction::Store);
1074 int MemopCost =
Chandler Carruth705b1852015-01-31 03:43:40 +00001075 NumElem * BaseT::getMemoryOpCost(Opcode, SrcVTy->getScalarType(),
1076 Alignment, AddressSpace);
Elena Demikhovskya3232f72015-01-25 08:44:46 +00001077 return MemopCost + ValueSplitCost + MaskSplitCost + MaskCmpCost;
1078 }
1079
1080 // Legalize the type.
Chandler Carruth93205eb2015-08-05 18:08:10 +00001081 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, SrcVTy);
Cong Houda4e8ae2015-10-28 18:15:46 +00001082 auto VT = TLI->getValueType(DL, SrcVTy);
Chandler Carruth93205eb2015-08-05 18:08:10 +00001083 int Cost = 0;
Cong Houda4e8ae2015-10-28 18:15:46 +00001084 if (VT.isSimple() && LT.second != VT.getSimpleVT() &&
Elena Demikhovskya3232f72015-01-25 08:44:46 +00001085 LT.second.getVectorNumElements() == NumElem)
1086 // Promotion requires expand/truncate for data and a shuffle for mask.
Hans Wennborg083ca9b2015-10-06 23:24:35 +00001087 Cost += getShuffleCost(TTI::SK_Alternate, SrcVTy, 0, nullptr) +
1088 getShuffleCost(TTI::SK_Alternate, MaskTy, 0, nullptr);
Chandler Carruth705b1852015-01-31 03:43:40 +00001089
Elena Demikhovskya3232f72015-01-25 08:44:46 +00001090 else if (LT.second.getVectorNumElements() > NumElem) {
1091 VectorType *NewMaskTy = VectorType::get(MaskTy->getVectorElementType(),
1092 LT.second.getVectorNumElements());
1093 // Expanding requires fill mask with zeroes
Chandler Carruth705b1852015-01-31 03:43:40 +00001094 Cost += getShuffleCost(TTI::SK_InsertSubvector, NewMaskTy, 0, MaskTy);
Elena Demikhovskya3232f72015-01-25 08:44:46 +00001095 }
1096 if (!ST->hasAVX512())
1097 return Cost + LT.first*4; // Each maskmov costs 4
1098
1099 // AVX-512 masked load/store is cheapper
1100 return Cost+LT.first;
1101}
1102
Chandler Carruth93205eb2015-08-05 18:08:10 +00001103int X86TTIImpl::getAddressComputationCost(Type *Ty, bool IsComplex) {
Arnold Schwaighofer6042a262013-07-12 19:16:07 +00001104 // Address computations in vectorized code with non-consecutive addresses will
1105 // likely result in more instructions compared to scalar code where the
1106 // computation can more often be merged into the index mode. The resulting
1107 // extra micro-ops can significantly decrease throughput.
1108 unsigned NumVectorInstToHideOverhead = 10;
1109
1110 if (Ty->isVectorTy() && IsComplex)
1111 return NumVectorInstToHideOverhead;
1112
Chandler Carruth705b1852015-01-31 03:43:40 +00001113 return BaseT::getAddressComputationCost(Ty, IsComplex);
Arnold Schwaighofer6042a262013-07-12 19:16:07 +00001114}
Yi Jiang5c343de2013-09-19 17:48:48 +00001115
Chandler Carruth93205eb2015-08-05 18:08:10 +00001116int X86TTIImpl::getReductionCost(unsigned Opcode, Type *ValTy,
1117 bool IsPairwise) {
Michael Liao5bf95782014-12-04 05:20:33 +00001118
Chandler Carruth93205eb2015-08-05 18:08:10 +00001119 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, ValTy);
Michael Liao5bf95782014-12-04 05:20:33 +00001120
Yi Jiang5c343de2013-09-19 17:48:48 +00001121 MVT MTy = LT.second;
Michael Liao5bf95782014-12-04 05:20:33 +00001122
Yi Jiang5c343de2013-09-19 17:48:48 +00001123 int ISD = TLI->InstructionOpcodeToISD(Opcode);
1124 assert(ISD && "Invalid opcode");
Michael Liao5bf95782014-12-04 05:20:33 +00001125
1126 // We use the Intel Architecture Code Analyzer(IACA) to measure the throughput
1127 // and make it as the cost.
1128
Craig Topper4b275762015-10-28 04:02:12 +00001129 static const CostTblEntry SSE42CostTblPairWise[] = {
Yi Jiang5c343de2013-09-19 17:48:48 +00001130 { ISD::FADD, MVT::v2f64, 2 },
1131 { ISD::FADD, MVT::v4f32, 4 },
1132 { ISD::ADD, MVT::v2i64, 2 }, // The data reported by the IACA tool is "1.6".
1133 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.5".
1134 { ISD::ADD, MVT::v8i16, 5 },
1135 };
Michael Liao5bf95782014-12-04 05:20:33 +00001136
Craig Topper4b275762015-10-28 04:02:12 +00001137 static const CostTblEntry AVX1CostTblPairWise[] = {
Yi Jiang5c343de2013-09-19 17:48:48 +00001138 { ISD::FADD, MVT::v4f32, 4 },
1139 { ISD::FADD, MVT::v4f64, 5 },
1140 { ISD::FADD, MVT::v8f32, 7 },
1141 { ISD::ADD, MVT::v2i64, 1 }, // The data reported by the IACA tool is "1.5".
1142 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.5".
1143 { ISD::ADD, MVT::v4i64, 5 }, // The data reported by the IACA tool is "4.8".
1144 { ISD::ADD, MVT::v8i16, 5 },
1145 { ISD::ADD, MVT::v8i32, 5 },
1146 };
1147
Craig Topper4b275762015-10-28 04:02:12 +00001148 static const CostTblEntry SSE42CostTblNoPairWise[] = {
Yi Jiang5c343de2013-09-19 17:48:48 +00001149 { ISD::FADD, MVT::v2f64, 2 },
1150 { ISD::FADD, MVT::v4f32, 4 },
1151 { ISD::ADD, MVT::v2i64, 2 }, // The data reported by the IACA tool is "1.6".
1152 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.3".
1153 { ISD::ADD, MVT::v8i16, 4 }, // The data reported by the IACA tool is "4.3".
1154 };
Michael Liao5bf95782014-12-04 05:20:33 +00001155
Craig Topper4b275762015-10-28 04:02:12 +00001156 static const CostTblEntry AVX1CostTblNoPairWise[] = {
Yi Jiang5c343de2013-09-19 17:48:48 +00001157 { ISD::FADD, MVT::v4f32, 3 },
1158 { ISD::FADD, MVT::v4f64, 3 },
1159 { ISD::FADD, MVT::v8f32, 4 },
1160 { ISD::ADD, MVT::v2i64, 1 }, // The data reported by the IACA tool is "1.5".
1161 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "2.8".
1162 { ISD::ADD, MVT::v4i64, 3 },
1163 { ISD::ADD, MVT::v8i16, 4 },
1164 { ISD::ADD, MVT::v8i32, 5 },
1165 };
Michael Liao5bf95782014-12-04 05:20:33 +00001166
Yi Jiang5c343de2013-09-19 17:48:48 +00001167 if (IsPairwise) {
Craig Topperee0c8592015-10-27 04:14:24 +00001168 if (ST->hasAVX())
1169 if (const auto *Entry = CostTableLookup(AVX1CostTblPairWise, ISD, MTy))
1170 return LT.first * Entry->Cost;
Michael Liao5bf95782014-12-04 05:20:33 +00001171
Craig Topperee0c8592015-10-27 04:14:24 +00001172 if (ST->hasSSE42())
1173 if (const auto *Entry = CostTableLookup(SSE42CostTblPairWise, ISD, MTy))
1174 return LT.first * Entry->Cost;
Yi Jiang5c343de2013-09-19 17:48:48 +00001175 } else {
Craig Topperee0c8592015-10-27 04:14:24 +00001176 if (ST->hasAVX())
1177 if (const auto *Entry = CostTableLookup(AVX1CostTblNoPairWise, ISD, MTy))
1178 return LT.first * Entry->Cost;
Michael Liao5bf95782014-12-04 05:20:33 +00001179
Craig Topperee0c8592015-10-27 04:14:24 +00001180 if (ST->hasSSE42())
1181 if (const auto *Entry = CostTableLookup(SSE42CostTblNoPairWise, ISD, MTy))
1182 return LT.first * Entry->Cost;
Yi Jiang5c343de2013-09-19 17:48:48 +00001183 }
1184
Chandler Carruth705b1852015-01-31 03:43:40 +00001185 return BaseT::getReductionCost(Opcode, ValTy, IsPairwise);
Yi Jiang5c343de2013-09-19 17:48:48 +00001186}
1187
Juergen Ributzkab2e4edb2014-06-10 00:32:29 +00001188/// \brief Calculate the cost of materializing a 64-bit value. This helper
1189/// method might only calculate a fraction of a larger immediate. Therefore it
1190/// is valid to return a cost of ZERO.
Chandler Carruth93205eb2015-08-05 18:08:10 +00001191int X86TTIImpl::getIntImmCost(int64_t Val) {
Juergen Ributzkab2e4edb2014-06-10 00:32:29 +00001192 if (Val == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +00001193 return TTI::TCC_Free;
Juergen Ributzkab2e4edb2014-06-10 00:32:29 +00001194
1195 if (isInt<32>(Val))
Chandler Carruth705b1852015-01-31 03:43:40 +00001196 return TTI::TCC_Basic;
Juergen Ributzkab2e4edb2014-06-10 00:32:29 +00001197
Chandler Carruth705b1852015-01-31 03:43:40 +00001198 return 2 * TTI::TCC_Basic;
Juergen Ributzkab2e4edb2014-06-10 00:32:29 +00001199}
1200
Chandler Carruth93205eb2015-08-05 18:08:10 +00001201int X86TTIImpl::getIntImmCost(const APInt &Imm, Type *Ty) {
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001202 assert(Ty->isIntegerTy());
1203
1204 unsigned BitSize = Ty->getPrimitiveSizeInBits();
1205 if (BitSize == 0)
1206 return ~0U;
1207
Juergen Ributzka43176172014-05-19 21:00:53 +00001208 // Never hoist constants larger than 128bit, because this might lead to
1209 // incorrect code generation or assertions in codegen.
1210 // Fixme: Create a cost model for types larger than i128 once the codegen
1211 // issues have been fixed.
1212 if (BitSize > 128)
Chandler Carruth705b1852015-01-31 03:43:40 +00001213 return TTI::TCC_Free;
Juergen Ributzka43176172014-05-19 21:00:53 +00001214
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001215 if (Imm == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +00001216 return TTI::TCC_Free;
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001217
Juergen Ributzkab2e4edb2014-06-10 00:32:29 +00001218 // Sign-extend all constants to a multiple of 64-bit.
1219 APInt ImmVal = Imm;
1220 if (BitSize & 0x3f)
1221 ImmVal = Imm.sext((BitSize + 63) & ~0x3fU);
1222
1223 // Split the constant into 64-bit chunks and calculate the cost for each
1224 // chunk.
Chandler Carruth93205eb2015-08-05 18:08:10 +00001225 int Cost = 0;
Juergen Ributzkab2e4edb2014-06-10 00:32:29 +00001226 for (unsigned ShiftVal = 0; ShiftVal < BitSize; ShiftVal += 64) {
1227 APInt Tmp = ImmVal.ashr(ShiftVal).sextOrTrunc(64);
1228 int64_t Val = Tmp.getSExtValue();
1229 Cost += getIntImmCost(Val);
1230 }
Sanjay Patel4c7d0942016-04-05 19:27:39 +00001231 // We need at least one instruction to materialize the constant.
Chandler Carruth93205eb2015-08-05 18:08:10 +00001232 return std::max(1, Cost);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001233}
1234
Chandler Carruth93205eb2015-08-05 18:08:10 +00001235int X86TTIImpl::getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
1236 Type *Ty) {
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001237 assert(Ty->isIntegerTy());
1238
1239 unsigned BitSize = Ty->getPrimitiveSizeInBits();
Juergen Ributzka43176172014-05-19 21:00:53 +00001240 // There is no cost model for constants with a bit size of 0. Return TCC_Free
1241 // here, so that constant hoisting will ignore this constant.
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001242 if (BitSize == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +00001243 return TTI::TCC_Free;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001244
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001245 unsigned ImmIdx = ~0U;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001246 switch (Opcode) {
Chandler Carruth705b1852015-01-31 03:43:40 +00001247 default:
1248 return TTI::TCC_Free;
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001249 case Instruction::GetElementPtr:
Juergen Ributzka27435b32014-04-02 21:45:36 +00001250 // Always hoist the base address of a GetElementPtr. This prevents the
1251 // creation of new constants for every base constant that gets constant
1252 // folded with the offset.
Juergen Ributzka631c4912014-03-25 18:01:25 +00001253 if (Idx == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +00001254 return 2 * TTI::TCC_Basic;
1255 return TTI::TCC_Free;
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001256 case Instruction::Store:
1257 ImmIdx = 0;
1258 break;
Craig Topper074e8452015-12-20 18:41:54 +00001259 case Instruction::ICmp:
1260 // This is an imperfect hack to prevent constant hoisting of
1261 // compares that might be trying to check if a 64-bit value fits in
1262 // 32-bits. The backend can optimize these cases using a right shift by 32.
1263 // Ideally we would check the compare predicate here. There also other
1264 // similar immediates the backend can use shifts for.
1265 if (Idx == 1 && Imm.getBitWidth() == 64) {
1266 uint64_t ImmVal = Imm.getZExtValue();
1267 if (ImmVal == 0x100000000ULL || ImmVal == 0xffffffff)
1268 return TTI::TCC_Free;
1269 }
1270 ImmIdx = 1;
1271 break;
Craig Topper79dd1bf2015-10-06 02:50:24 +00001272 case Instruction::And:
1273 // We support 64-bit ANDs with immediates with 32-bits of leading zeroes
1274 // by using a 32-bit operation with implicit zero extension. Detect such
1275 // immediates here as the normal path expects bit 31 to be sign extended.
1276 if (Idx == 1 && Imm.getBitWidth() == 64 && isUInt<32>(Imm.getZExtValue()))
1277 return TTI::TCC_Free;
1278 // Fallthrough
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001279 case Instruction::Add:
1280 case Instruction::Sub:
1281 case Instruction::Mul:
1282 case Instruction::UDiv:
1283 case Instruction::SDiv:
1284 case Instruction::URem:
1285 case Instruction::SRem:
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001286 case Instruction::Or:
1287 case Instruction::Xor:
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001288 ImmIdx = 1;
1289 break;
Michael Zolotukhin1f4a9602014-04-30 19:17:32 +00001290 // Always return TCC_Free for the shift value of a shift instruction.
1291 case Instruction::Shl:
1292 case Instruction::LShr:
1293 case Instruction::AShr:
1294 if (Idx == 1)
Chandler Carruth705b1852015-01-31 03:43:40 +00001295 return TTI::TCC_Free;
Michael Zolotukhin1f4a9602014-04-30 19:17:32 +00001296 break;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001297 case Instruction::Trunc:
1298 case Instruction::ZExt:
1299 case Instruction::SExt:
1300 case Instruction::IntToPtr:
1301 case Instruction::PtrToInt:
1302 case Instruction::BitCast:
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001303 case Instruction::PHI:
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001304 case Instruction::Call:
1305 case Instruction::Select:
1306 case Instruction::Ret:
1307 case Instruction::Load:
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001308 break;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001309 }
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001310
Juergen Ributzkab2e4edb2014-06-10 00:32:29 +00001311 if (Idx == ImmIdx) {
Chandler Carruth93205eb2015-08-05 18:08:10 +00001312 int NumConstants = (BitSize + 63) / 64;
1313 int Cost = X86TTIImpl::getIntImmCost(Imm, Ty);
Chandler Carruth705b1852015-01-31 03:43:40 +00001314 return (Cost <= NumConstants * TTI::TCC_Basic)
Chandler Carruth93205eb2015-08-05 18:08:10 +00001315 ? static_cast<int>(TTI::TCC_Free)
Chandler Carruth705b1852015-01-31 03:43:40 +00001316 : Cost;
Juergen Ributzkab2e4edb2014-06-10 00:32:29 +00001317 }
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001318
Chandler Carruth705b1852015-01-31 03:43:40 +00001319 return X86TTIImpl::getIntImmCost(Imm, Ty);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001320}
1321
Chandler Carruth93205eb2015-08-05 18:08:10 +00001322int X86TTIImpl::getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm,
1323 Type *Ty) {
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001324 assert(Ty->isIntegerTy());
1325
1326 unsigned BitSize = Ty->getPrimitiveSizeInBits();
Juergen Ributzka43176172014-05-19 21:00:53 +00001327 // There is no cost model for constants with a bit size of 0. Return TCC_Free
1328 // here, so that constant hoisting will ignore this constant.
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001329 if (BitSize == 0)
Chandler Carruth705b1852015-01-31 03:43:40 +00001330 return TTI::TCC_Free;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001331
1332 switch (IID) {
Chandler Carruth705b1852015-01-31 03:43:40 +00001333 default:
1334 return TTI::TCC_Free;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001335 case Intrinsic::sadd_with_overflow:
1336 case Intrinsic::uadd_with_overflow:
1337 case Intrinsic::ssub_with_overflow:
1338 case Intrinsic::usub_with_overflow:
1339 case Intrinsic::smul_with_overflow:
1340 case Intrinsic::umul_with_overflow:
Juergen Ributzkaf0dff492014-03-21 06:04:45 +00001341 if ((Idx == 1) && Imm.getBitWidth() <= 64 && isInt<32>(Imm.getSExtValue()))
Chandler Carruth705b1852015-01-31 03:43:40 +00001342 return TTI::TCC_Free;
Juergen Ributzka5eef98c2014-03-25 18:01:23 +00001343 break;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001344 case Intrinsic::experimental_stackmap:
Juergen Ributzka5eef98c2014-03-25 18:01:23 +00001345 if ((Idx < 2) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
Chandler Carruth705b1852015-01-31 03:43:40 +00001346 return TTI::TCC_Free;
Juergen Ributzka5eef98c2014-03-25 18:01:23 +00001347 break;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001348 case Intrinsic::experimental_patchpoint_void:
1349 case Intrinsic::experimental_patchpoint_i64:
Juergen Ributzka5eef98c2014-03-25 18:01:23 +00001350 if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
Chandler Carruth705b1852015-01-31 03:43:40 +00001351 return TTI::TCC_Free;
Juergen Ributzka5eef98c2014-03-25 18:01:23 +00001352 break;
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001353 }
Chandler Carruth705b1852015-01-31 03:43:40 +00001354 return X86TTIImpl::getIntImmCost(Imm, Ty);
Juergen Ributzkaf26beda2014-01-25 02:02:55 +00001355}
NAKAMURA Takumi0b305db2015-07-14 04:03:49 +00001356
Elena Demikhovsky54946982015-12-28 20:10:59 +00001357// Return an average cost of Gather / Scatter instruction, maybe improved later
1358int X86TTIImpl::getGSVectorCost(unsigned Opcode, Type *SrcVTy, Value *Ptr,
1359 unsigned Alignment, unsigned AddressSpace) {
1360
1361 assert(isa<VectorType>(SrcVTy) && "Unexpected type in getGSVectorCost");
1362 unsigned VF = SrcVTy->getVectorNumElements();
1363
1364 // Try to reduce index size from 64 bit (default for GEP)
1365 // to 32. It is essential for VF 16. If the index can't be reduced to 32, the
1366 // operation will use 16 x 64 indices which do not fit in a zmm and needs
1367 // to split. Also check that the base pointer is the same for all lanes,
1368 // and that there's at most one variable index.
1369 auto getIndexSizeInBits = [](Value *Ptr, const DataLayout& DL) {
1370 unsigned IndexSize = DL.getPointerSizeInBits();
1371 GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr);
1372 if (IndexSize < 64 || !GEP)
1373 return IndexSize;
Simon Pilgrim14000b32016-05-24 08:17:50 +00001374
Elena Demikhovsky54946982015-12-28 20:10:59 +00001375 unsigned NumOfVarIndices = 0;
1376 Value *Ptrs = GEP->getPointerOperand();
1377 if (Ptrs->getType()->isVectorTy() && !getSplatValue(Ptrs))
1378 return IndexSize;
1379 for (unsigned i = 1; i < GEP->getNumOperands(); ++i) {
1380 if (isa<Constant>(GEP->getOperand(i)))
1381 continue;
1382 Type *IndxTy = GEP->getOperand(i)->getType();
1383 if (IndxTy->isVectorTy())
1384 IndxTy = IndxTy->getVectorElementType();
1385 if ((IndxTy->getPrimitiveSizeInBits() == 64 &&
1386 !isa<SExtInst>(GEP->getOperand(i))) ||
1387 ++NumOfVarIndices > 1)
1388 return IndexSize; // 64
1389 }
1390 return (unsigned)32;
1391 };
1392
1393
1394 // Trying to reduce IndexSize to 32 bits for vector 16.
1395 // By default the IndexSize is equal to pointer size.
1396 unsigned IndexSize = (VF >= 16) ? getIndexSizeInBits(Ptr, DL) :
1397 DL.getPointerSizeInBits();
1398
Mehdi Amini867e9142016-04-14 04:36:40 +00001399 Type *IndexVTy = VectorType::get(IntegerType::get(SrcVTy->getContext(),
Elena Demikhovsky54946982015-12-28 20:10:59 +00001400 IndexSize), VF);
1401 std::pair<int, MVT> IdxsLT = TLI->getTypeLegalizationCost(DL, IndexVTy);
1402 std::pair<int, MVT> SrcLT = TLI->getTypeLegalizationCost(DL, SrcVTy);
1403 int SplitFactor = std::max(IdxsLT.first, SrcLT.first);
1404 if (SplitFactor > 1) {
1405 // Handle splitting of vector of pointers
1406 Type *SplitSrcTy = VectorType::get(SrcVTy->getScalarType(), VF / SplitFactor);
1407 return SplitFactor * getGSVectorCost(Opcode, SplitSrcTy, Ptr, Alignment,
1408 AddressSpace);
1409 }
1410
1411 // The gather / scatter cost is given by Intel architects. It is a rough
1412 // number since we are looking at one instruction in a time.
1413 const int GSOverhead = 2;
1414 return GSOverhead + VF * getMemoryOpCost(Opcode, SrcVTy->getScalarType(),
1415 Alignment, AddressSpace);
1416}
1417
1418/// Return the cost of full scalarization of gather / scatter operation.
1419///
1420/// Opcode - Load or Store instruction.
1421/// SrcVTy - The type of the data vector that should be gathered or scattered.
1422/// VariableMask - The mask is non-constant at compile time.
1423/// Alignment - Alignment for one element.
1424/// AddressSpace - pointer[s] address space.
1425///
1426int X86TTIImpl::getGSScalarCost(unsigned Opcode, Type *SrcVTy,
1427 bool VariableMask, unsigned Alignment,
1428 unsigned AddressSpace) {
1429 unsigned VF = SrcVTy->getVectorNumElements();
1430
1431 int MaskUnpackCost = 0;
1432 if (VariableMask) {
1433 VectorType *MaskTy =
Mehdi Amini867e9142016-04-14 04:36:40 +00001434 VectorType::get(Type::getInt1Ty(SrcVTy->getContext()), VF);
Elena Demikhovsky54946982015-12-28 20:10:59 +00001435 MaskUnpackCost = getScalarizationOverhead(MaskTy, false, true);
1436 int ScalarCompareCost =
Mehdi Amini867e9142016-04-14 04:36:40 +00001437 getCmpSelInstrCost(Instruction::ICmp, Type::getInt1Ty(SrcVTy->getContext()),
Elena Demikhovsky54946982015-12-28 20:10:59 +00001438 nullptr);
1439 int BranchCost = getCFInstrCost(Instruction::Br);
1440 MaskUnpackCost += VF * (BranchCost + ScalarCompareCost);
1441 }
1442
1443 // The cost of the scalar loads/stores.
1444 int MemoryOpCost = VF * getMemoryOpCost(Opcode, SrcVTy->getScalarType(),
1445 Alignment, AddressSpace);
1446
1447 int InsertExtractCost = 0;
1448 if (Opcode == Instruction::Load)
1449 for (unsigned i = 0; i < VF; ++i)
1450 // Add the cost of inserting each scalar load into the vector
1451 InsertExtractCost +=
1452 getVectorInstrCost(Instruction::InsertElement, SrcVTy, i);
1453 else
1454 for (unsigned i = 0; i < VF; ++i)
1455 // Add the cost of extracting each element out of the data vector
1456 InsertExtractCost +=
1457 getVectorInstrCost(Instruction::ExtractElement, SrcVTy, i);
1458
1459 return MemoryOpCost + MaskUnpackCost + InsertExtractCost;
1460}
1461
1462/// Calculate the cost of Gather / Scatter operation
1463int X86TTIImpl::getGatherScatterOpCost(unsigned Opcode, Type *SrcVTy,
1464 Value *Ptr, bool VariableMask,
1465 unsigned Alignment) {
1466 assert(SrcVTy->isVectorTy() && "Unexpected data type for Gather/Scatter");
1467 unsigned VF = SrcVTy->getVectorNumElements();
1468 PointerType *PtrTy = dyn_cast<PointerType>(Ptr->getType());
1469 if (!PtrTy && Ptr->getType()->isVectorTy())
1470 PtrTy = dyn_cast<PointerType>(Ptr->getType()->getVectorElementType());
1471 assert(PtrTy && "Unexpected type for Ptr argument");
1472 unsigned AddressSpace = PtrTy->getAddressSpace();
1473
1474 bool Scalarize = false;
1475 if ((Opcode == Instruction::Load && !isLegalMaskedGather(SrcVTy)) ||
1476 (Opcode == Instruction::Store && !isLegalMaskedScatter(SrcVTy)))
1477 Scalarize = true;
1478 // Gather / Scatter for vector 2 is not profitable on KNL / SKX
1479 // Vector-4 of gather/scatter instruction does not exist on KNL.
1480 // We can extend it to 8 elements, but zeroing upper bits of
1481 // the mask vector will add more instructions. Right now we give the scalar
1482 // cost of vector-4 for KNL. TODO: Check, maybe the gather/scatter instruction is
1483 // better in the VariableMask case.
1484 if (VF == 2 || (VF == 4 && !ST->hasVLX()))
1485 Scalarize = true;
1486
1487 if (Scalarize)
1488 return getGSScalarCost(Opcode, SrcVTy, VariableMask, Alignment, AddressSpace);
1489
1490 return getGSVectorCost(Opcode, SrcVTy, Ptr, Alignment, AddressSpace);
1491}
1492
Elena Demikhovsky20662e32015-10-19 07:43:38 +00001493bool X86TTIImpl::isLegalMaskedLoad(Type *DataTy) {
1494 Type *ScalarTy = DataTy->getScalarType();
Elena Demikhovsky1ca72e12015-11-19 07:17:16 +00001495 int DataWidth = isa<PointerType>(ScalarTy) ?
1496 DL.getPointerSizeInBits() : ScalarTy->getPrimitiveSizeInBits();
NAKAMURA Takumi0b305db2015-07-14 04:03:49 +00001497
Igor Breger4d94d4d2016-03-06 12:38:58 +00001498 return (DataWidth >= 32 && ST->hasAVX()) ||
1499 (DataWidth >= 8 && ST->hasBWI());
NAKAMURA Takumi0b305db2015-07-14 04:03:49 +00001500}
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00001501
Elena Demikhovsky20662e32015-10-19 07:43:38 +00001502bool X86TTIImpl::isLegalMaskedStore(Type *DataType) {
1503 return isLegalMaskedLoad(DataType);
Elena Demikhovskyf1de34b2014-12-04 09:40:44 +00001504}
1505
Elena Demikhovsky09285852015-10-25 15:37:55 +00001506bool X86TTIImpl::isLegalMaskedGather(Type *DataTy) {
1507 // This function is called now in two cases: from the Loop Vectorizer
1508 // and from the Scalarizer.
1509 // When the Loop Vectorizer asks about legality of the feature,
1510 // the vectorization factor is not calculated yet. The Loop Vectorizer
1511 // sends a scalar type and the decision is based on the width of the
1512 // scalar element.
1513 // Later on, the cost model will estimate usage this intrinsic based on
1514 // the vector type.
1515 // The Scalarizer asks again about legality. It sends a vector type.
1516 // In this case we can reject non-power-of-2 vectors.
1517 if (isa<VectorType>(DataTy) && !isPowerOf2_32(DataTy->getVectorNumElements()))
1518 return false;
1519 Type *ScalarTy = DataTy->getScalarType();
Elena Demikhovsky1ca72e12015-11-19 07:17:16 +00001520 int DataWidth = isa<PointerType>(ScalarTy) ?
1521 DL.getPointerSizeInBits() : ScalarTy->getPrimitiveSizeInBits();
Elena Demikhovsky09285852015-10-25 15:37:55 +00001522
1523 // AVX-512 allows gather and scatter
1524 return DataWidth >= 32 && ST->hasAVX512();
1525}
1526
1527bool X86TTIImpl::isLegalMaskedScatter(Type *DataType) {
1528 return isLegalMaskedGather(DataType);
1529}
1530
Eric Christopherd566fb12015-07-29 22:09:48 +00001531bool X86TTIImpl::areInlineCompatible(const Function *Caller,
1532 const Function *Callee) const {
Eric Christophere1002262015-07-02 01:11:50 +00001533 const TargetMachine &TM = getTLI()->getTargetMachine();
1534
1535 // Work this as a subsetting of subtarget features.
1536 const FeatureBitset &CallerBits =
1537 TM.getSubtargetImpl(*Caller)->getFeatureBits();
1538 const FeatureBitset &CalleeBits =
1539 TM.getSubtargetImpl(*Callee)->getFeatureBits();
1540
1541 // FIXME: This is likely too limiting as it will include subtarget features
1542 // that we might not care about for inlining, but it is conservatively
1543 // correct.
1544 return (CallerBits & CalleeBits) == CalleeBits;
1545}