1 //===-- X86TargetTransformInfo.cpp - X86 specific TTI pass ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
15 //===----------------------------------------------------------------------===//
16 /// About Cost Model numbers used below it's necessary to say the following:
17 /// the numbers correspond to some "generic" X86 CPU instead of usage of
18 /// concrete CPU model. Usually the numbers correspond to CPU where the feature
19 /// apeared at the first time. For example, if we do Subtarget.hasSSE42() in
20 /// the lookups below the cost is based on Nehalem as that was the first CPU
21 /// to support that feature level and thus has most likely the worst case cost.
22 /// Some examples of other technologies/CPUs:
23 /// SSE 3 - Pentium4 / Athlon64
26 /// AVX - Sandy Bridge
28 /// AVX-512 - Xeon Phi / Skylake
29 /// And some examples of instruction target dependent costs (latency)
30 /// divss sqrtss rsqrtss
32 /// Piledriver 9-24 13-15 5
34 /// Pentium II,III 18 30 2
35 /// Nehalem 7-14 7-18 3
36 /// Haswell 10-13 11 5
37 /// TODO: Develop and implement the target dependent cost model and
38 /// specialize cost numbers for different Cost Model Targets such as throughput,
39 /// code size, latency and uop count.
40 //===----------------------------------------------------------------------===//
42 #include "X86TargetTransformInfo.h"
43 #include "llvm/Analysis/TargetTransformInfo.h"
44 #include "llvm/CodeGen/BasicTTIImpl.h"
45 #include "llvm/IR/IntrinsicInst.h"
46 #include "llvm/Support/Debug.h"
47 #include "llvm/Target/CostTable.h"
48 #include "llvm/Target/TargetLowering.h"
52 #define DEBUG_TYPE "x86tti"
54 //===----------------------------------------------------------------------===//
58 //===----------------------------------------------------------------------===//
60 TargetTransformInfo::PopcntSupportKind
61 X86TTIImpl::getPopcntSupport(unsigned TyWidth) {
62 assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2");
63 // TODO: Currently the __builtin_popcount() implementation using SSE3
64 // instructions is inefficient. Once the problem is fixed, we should
65 // call ST->hasSSE3() instead of ST->hasPOPCNT().
66 return ST->hasPOPCNT() ? TTI::PSK_FastHardware : TTI::PSK_Software;
69 unsigned X86TTIImpl::getNumberOfRegisters(bool Vector) {
70 if (Vector && !ST->hasSSE1())
74 if (Vector && ST->hasAVX512())
81 unsigned X86TTIImpl::getRegisterBitWidth(bool Vector) {
98 unsigned X86TTIImpl::getMaxInterleaveFactor(unsigned VF) {
99 // If the loop will not be vectorized, don't interleave the loop.
100 // Let regular unroll to unroll the loop, which saves the overflow
101 // check and memory check cost.
108 // Sandybridge and Haswell have multiple execution ports and pipelined
116 int X86TTIImpl::getArithmeticInstrCost(
117 unsigned Opcode, Type *Ty, TTI::OperandValueKind Op1Info,
118 TTI::OperandValueKind Op2Info, TTI::OperandValueProperties Opd1PropInfo,
119 TTI::OperandValueProperties Opd2PropInfo) {
120 // Legalize the type.
121 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty);
123 int ISD = TLI->InstructionOpcodeToISD(Opcode);
124 assert(ISD && "Invalid opcode");
126 if (ISD == ISD::SDIV &&
127 Op2Info == TargetTransformInfo::OK_UniformConstantValue &&
128 Opd2PropInfo == TargetTransformInfo::OP_PowerOf2) {
129 // On X86, vector signed division by constants power-of-two are
130 // normally expanded to the sequence SRA + SRL + ADD + SRA.
131 // The OperandValue properties many not be same as that of previous
132 // operation;conservatively assume OP_None.
133 int Cost = 2 * getArithmeticInstrCost(Instruction::AShr, Ty, Op1Info,
134 Op2Info, TargetTransformInfo::OP_None,
135 TargetTransformInfo::OP_None);
136 Cost += getArithmeticInstrCost(Instruction::LShr, Ty, Op1Info, Op2Info,
137 TargetTransformInfo::OP_None,
138 TargetTransformInfo::OP_None);
139 Cost += getArithmeticInstrCost(Instruction::Add, Ty, Op1Info, Op2Info,
140 TargetTransformInfo::OP_None,
141 TargetTransformInfo::OP_None);
146 static const CostTblEntry AVX512BWUniformConstCostTable[] = {
147 { ISD::SHL, MVT::v64i8, 2 }, // psllw + pand.
148 { ISD::SRL, MVT::v64i8, 2 }, // psrlw + pand.
149 { ISD::SRA, MVT::v64i8, 4 }, // psrlw, pand, pxor, psubb.
151 { ISD::SDIV, MVT::v32i16, 6 }, // vpmulhw sequence
152 { ISD::UDIV, MVT::v32i16, 6 }, // vpmulhuw sequence
155 if (Op2Info == TargetTransformInfo::OK_UniformConstantValue &&
157 if (const auto *Entry = CostTableLookup(AVX512BWUniformConstCostTable, ISD,
159 return LT.first * Entry->Cost;
162 static const CostTblEntry AVX512UniformConstCostTable[] = {
163 { ISD::SDIV, MVT::v16i32, 15 }, // vpmuldq sequence
164 { ISD::UDIV, MVT::v16i32, 15 }, // vpmuludq sequence
167 if (Op2Info == TargetTransformInfo::OK_UniformConstantValue &&
169 if (const auto *Entry = CostTableLookup(AVX512UniformConstCostTable, ISD,
171 return LT.first * Entry->Cost;
174 static const CostTblEntry AVX2UniformConstCostTable[] = {
175 { ISD::SHL, MVT::v32i8, 2 }, // psllw + pand.
176 { ISD::SRL, MVT::v32i8, 2 }, // psrlw + pand.
177 { ISD::SRA, MVT::v32i8, 4 }, // psrlw, pand, pxor, psubb.
179 { ISD::SRA, MVT::v4i64, 4 }, // 2 x psrad + shuffle.
181 { ISD::SDIV, MVT::v16i16, 6 }, // vpmulhw sequence
182 { ISD::UDIV, MVT::v16i16, 6 }, // vpmulhuw sequence
183 { ISD::SDIV, MVT::v8i32, 15 }, // vpmuldq sequence
184 { ISD::UDIV, MVT::v8i32, 15 }, // vpmuludq sequence
187 if (Op2Info == TargetTransformInfo::OK_UniformConstantValue &&
189 if (const auto *Entry = CostTableLookup(AVX2UniformConstCostTable, ISD,
191 return LT.first * Entry->Cost;
194 static const CostTblEntry SSE2UniformConstCostTable[] = {
195 { ISD::SHL, MVT::v16i8, 2 }, // psllw + pand.
196 { ISD::SRL, MVT::v16i8, 2 }, // psrlw + pand.
197 { ISD::SRA, MVT::v16i8, 4 }, // psrlw, pand, pxor, psubb.
199 { ISD::SHL, MVT::v32i8, 4 }, // 2*(psllw + pand).
200 { ISD::SRL, MVT::v32i8, 4 }, // 2*(psrlw + pand).
201 { ISD::SRA, MVT::v32i8, 8 }, // 2*(psrlw, pand, pxor, psubb).
203 { ISD::SDIV, MVT::v16i16, 12 }, // pmulhw sequence
204 { ISD::SDIV, MVT::v8i16, 6 }, // pmulhw sequence
205 { ISD::UDIV, MVT::v16i16, 12 }, // pmulhuw sequence
206 { ISD::UDIV, MVT::v8i16, 6 }, // pmulhuw sequence
207 { ISD::SDIV, MVT::v8i32, 38 }, // pmuludq sequence
208 { ISD::SDIV, MVT::v4i32, 19 }, // pmuludq sequence
209 { ISD::UDIV, MVT::v8i32, 30 }, // pmuludq sequence
210 { ISD::UDIV, MVT::v4i32, 15 }, // pmuludq sequence
213 if (Op2Info == TargetTransformInfo::OK_UniformConstantValue &&
216 if (ISD == ISD::SDIV && LT.second == MVT::v8i32 && ST->hasAVX())
217 return LT.first * 30;
218 if (ISD == ISD::SDIV && LT.second == MVT::v4i32 && ST->hasSSE41())
219 return LT.first * 15;
221 if (const auto *Entry = CostTableLookup(SSE2UniformConstCostTable, ISD,
223 return LT.first * Entry->Cost;
226 static const CostTblEntry AVX2UniformCostTable[] = {
227 // Uniform splats are cheaper for the following instructions.
228 { ISD::SHL, MVT::v16i16, 1 }, // psllw.
229 { ISD::SRL, MVT::v16i16, 1 }, // psrlw.
230 { ISD::SRA, MVT::v16i16, 1 }, // psraw.
234 ((Op2Info == TargetTransformInfo::OK_UniformConstantValue) ||
235 (Op2Info == TargetTransformInfo::OK_UniformValue))) {
236 if (const auto *Entry =
237 CostTableLookup(AVX2UniformCostTable, ISD, LT.second))
238 return LT.first * Entry->Cost;
241 static const CostTblEntry SSE2UniformCostTable[] = {
242 // Uniform splats are cheaper for the following instructions.
243 { ISD::SHL, MVT::v8i16, 1 }, // psllw.
244 { ISD::SHL, MVT::v4i32, 1 }, // pslld
245 { ISD::SHL, MVT::v2i64, 1 }, // psllq.
247 { ISD::SRL, MVT::v8i16, 1 }, // psrlw.
248 { ISD::SRL, MVT::v4i32, 1 }, // psrld.
249 { ISD::SRL, MVT::v2i64, 1 }, // psrlq.
251 { ISD::SRA, MVT::v8i16, 1 }, // psraw.
252 { ISD::SRA, MVT::v4i32, 1 }, // psrad.
256 ((Op2Info == TargetTransformInfo::OK_UniformConstantValue) ||
257 (Op2Info == TargetTransformInfo::OK_UniformValue))) {
258 if (const auto *Entry =
259 CostTableLookup(SSE2UniformCostTable, ISD, LT.second))
260 return LT.first * Entry->Cost;
263 static const CostTblEntry AVX512DQCostTable[] = {
264 { ISD::MUL, MVT::v2i64, 1 },
265 { ISD::MUL, MVT::v4i64, 1 },
266 { ISD::MUL, MVT::v8i64, 1 }
269 // Look for AVX512DQ lowering tricks for custom cases.
271 if (const auto *Entry = CostTableLookup(AVX512DQCostTable, ISD, LT.second))
272 return LT.first * Entry->Cost;
274 static const CostTblEntry AVX512BWCostTable[] = {
275 { ISD::SHL, MVT::v32i16, 1 }, // vpsllvw
276 { ISD::SRL, MVT::v32i16, 1 }, // vpsrlvw
277 { ISD::SRA, MVT::v32i16, 1 }, // vpsravw
279 { ISD::MUL, MVT::v64i8, 11 }, // extend/pmullw/trunc sequence.
280 { ISD::MUL, MVT::v32i8, 4 }, // extend/pmullw/trunc sequence.
281 { ISD::MUL, MVT::v16i8, 4 }, // extend/pmullw/trunc sequence.
283 // Vectorizing division is a bad idea. See the SSE2 table for more comments.
284 { ISD::SDIV, MVT::v64i8, 64*20 },
285 { ISD::SDIV, MVT::v32i16, 32*20 },
286 { ISD::UDIV, MVT::v64i8, 64*20 },
287 { ISD::UDIV, MVT::v32i16, 32*20 }
290 // Look for AVX512BW lowering tricks for custom cases.
292 if (const auto *Entry = CostTableLookup(AVX512BWCostTable, ISD, LT.second))
293 return LT.first * Entry->Cost;
295 static const CostTblEntry AVX512CostTable[] = {
296 { ISD::SHL, MVT::v16i32, 1 },
297 { ISD::SRL, MVT::v16i32, 1 },
298 { ISD::SRA, MVT::v16i32, 1 },
299 { ISD::SHL, MVT::v8i64, 1 },
300 { ISD::SRL, MVT::v8i64, 1 },
301 { ISD::SRA, MVT::v8i64, 1 },
303 { ISD::MUL, MVT::v32i8, 13 }, // extend/pmullw/trunc sequence.
304 { ISD::MUL, MVT::v16i8, 5 }, // extend/pmullw/trunc sequence.
305 { ISD::MUL, MVT::v16i32, 1 }, // pmulld
306 { ISD::MUL, MVT::v8i64, 8 }, // 3*pmuludq/3*shift/2*add
308 // Vectorizing division is a bad idea. See the SSE2 table for more comments.
309 { ISD::SDIV, MVT::v16i32, 16*20 },
310 { ISD::SDIV, MVT::v8i64, 8*20 },
311 { ISD::UDIV, MVT::v16i32, 16*20 },
312 { ISD::UDIV, MVT::v8i64, 8*20 }
316 if (const auto *Entry = CostTableLookup(AVX512CostTable, ISD, LT.second))
317 return LT.first * Entry->Cost;
319 static const CostTblEntry AVX2ShiftCostTable[] = {
320 // Shifts on v4i64/v8i32 on AVX2 is legal even though we declare to
321 // customize them to detect the cases where shift amount is a scalar one.
322 { ISD::SHL, MVT::v4i32, 1 },
323 { ISD::SRL, MVT::v4i32, 1 },
324 { ISD::SRA, MVT::v4i32, 1 },
325 { ISD::SHL, MVT::v8i32, 1 },
326 { ISD::SRL, MVT::v8i32, 1 },
327 { ISD::SRA, MVT::v8i32, 1 },
328 { ISD::SHL, MVT::v2i64, 1 },
329 { ISD::SRL, MVT::v2i64, 1 },
330 { ISD::SHL, MVT::v4i64, 1 },
331 { ISD::SRL, MVT::v4i64, 1 },
334 // Look for AVX2 lowering tricks.
336 if (ISD == ISD::SHL && LT.second == MVT::v16i16 &&
337 (Op2Info == TargetTransformInfo::OK_UniformConstantValue ||
338 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue))
339 // On AVX2, a packed v16i16 shift left by a constant build_vector
340 // is lowered into a vector multiply (vpmullw).
343 if (const auto *Entry = CostTableLookup(AVX2ShiftCostTable, ISD, LT.second))
344 return LT.first * Entry->Cost;
347 static const CostTblEntry XOPShiftCostTable[] = {
348 // 128bit shifts take 1cy, but right shifts require negation beforehand.
349 { ISD::SHL, MVT::v16i8, 1 },
350 { ISD::SRL, MVT::v16i8, 2 },
351 { ISD::SRA, MVT::v16i8, 2 },
352 { ISD::SHL, MVT::v8i16, 1 },
353 { ISD::SRL, MVT::v8i16, 2 },
354 { ISD::SRA, MVT::v8i16, 2 },
355 { ISD::SHL, MVT::v4i32, 1 },
356 { ISD::SRL, MVT::v4i32, 2 },
357 { ISD::SRA, MVT::v4i32, 2 },
358 { ISD::SHL, MVT::v2i64, 1 },
359 { ISD::SRL, MVT::v2i64, 2 },
360 { ISD::SRA, MVT::v2i64, 2 },
361 // 256bit shifts require splitting if AVX2 didn't catch them above.
362 { ISD::SHL, MVT::v32i8, 2 },
363 { ISD::SRL, MVT::v32i8, 4 },
364 { ISD::SRA, MVT::v32i8, 4 },
365 { ISD::SHL, MVT::v16i16, 2 },
366 { ISD::SRL, MVT::v16i16, 4 },
367 { ISD::SRA, MVT::v16i16, 4 },
368 { ISD::SHL, MVT::v8i32, 2 },
369 { ISD::SRL, MVT::v8i32, 4 },
370 { ISD::SRA, MVT::v8i32, 4 },
371 { ISD::SHL, MVT::v4i64, 2 },
372 { ISD::SRL, MVT::v4i64, 4 },
373 { ISD::SRA, MVT::v4i64, 4 },
376 // Look for XOP lowering tricks.
378 if (const auto *Entry = CostTableLookup(XOPShiftCostTable, ISD, LT.second))
379 return LT.first * Entry->Cost;
381 static const CostTblEntry SSE2UniformShiftCostTable[] = {
382 // Uniform splats are cheaper for the following instructions.
383 { ISD::SHL, MVT::v16i16, 2 }, // psllw.
384 { ISD::SHL, MVT::v8i32, 2 }, // pslld
385 { ISD::SHL, MVT::v4i64, 2 }, // psllq.
387 { ISD::SRL, MVT::v16i16, 2 }, // psrlw.
388 { ISD::SRL, MVT::v8i32, 2 }, // psrld.
389 { ISD::SRL, MVT::v4i64, 2 }, // psrlq.
391 { ISD::SRA, MVT::v16i16, 2 }, // psraw.
392 { ISD::SRA, MVT::v8i32, 2 }, // psrad.
393 { ISD::SRA, MVT::v2i64, 4 }, // 2 x psrad + shuffle.
394 { ISD::SRA, MVT::v4i64, 8 }, // 2 x psrad + shuffle.
398 ((Op2Info == TargetTransformInfo::OK_UniformConstantValue) ||
399 (Op2Info == TargetTransformInfo::OK_UniformValue))) {
400 if (const auto *Entry =
401 CostTableLookup(SSE2UniformShiftCostTable, ISD, LT.second))
402 return LT.first * Entry->Cost;
405 if (ISD == ISD::SHL &&
406 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue) {
408 // Vector shift left by non uniform constant can be lowered
409 // into vector multiply.
410 if (((VT == MVT::v8i16 || VT == MVT::v4i32) && ST->hasSSE2()) ||
411 ((VT == MVT::v16i16 || VT == MVT::v8i32) && ST->hasAVX()))
415 static const CostTblEntry AVX2CostTable[] = {
416 { ISD::SHL, MVT::v32i8, 11 }, // vpblendvb sequence.
417 { ISD::SHL, MVT::v16i16, 10 }, // extend/vpsrlvd/pack sequence.
419 { ISD::SRL, MVT::v32i8, 11 }, // vpblendvb sequence.
420 { ISD::SRL, MVT::v16i16, 10 }, // extend/vpsrlvd/pack sequence.
422 { ISD::SRA, MVT::v32i8, 24 }, // vpblendvb sequence.
423 { ISD::SRA, MVT::v16i16, 10 }, // extend/vpsravd/pack sequence.
424 { ISD::SRA, MVT::v2i64, 4 }, // srl/xor/sub sequence.
425 { ISD::SRA, MVT::v4i64, 4 }, // srl/xor/sub sequence.
427 { ISD::SUB, MVT::v32i8, 1 }, // psubb
428 { ISD::ADD, MVT::v32i8, 1 }, // paddb
429 { ISD::SUB, MVT::v16i16, 1 }, // psubw
430 { ISD::ADD, MVT::v16i16, 1 }, // paddw
431 { ISD::SUB, MVT::v8i32, 1 }, // psubd
432 { ISD::ADD, MVT::v8i32, 1 }, // paddd
433 { ISD::SUB, MVT::v4i64, 1 }, // psubq
434 { ISD::ADD, MVT::v4i64, 1 }, // paddq
436 { ISD::MUL, MVT::v32i8, 17 }, // extend/pmullw/trunc sequence.
437 { ISD::MUL, MVT::v16i8, 7 }, // extend/pmullw/trunc sequence.
438 { ISD::MUL, MVT::v16i16, 1 }, // pmullw
439 { ISD::MUL, MVT::v8i32, 1 }, // pmulld
440 { ISD::MUL, MVT::v4i64, 8 }, // 3*pmuludq/3*shift/2*add
442 { ISD::FDIV, MVT::f32, 7 }, // Haswell from http://www.agner.org/
443 { ISD::FDIV, MVT::v4f32, 7 }, // Haswell from http://www.agner.org/
444 { ISD::FDIV, MVT::v8f32, 14 }, // Haswell from http://www.agner.org/
445 { ISD::FDIV, MVT::f64, 14 }, // Haswell from http://www.agner.org/
446 { ISD::FDIV, MVT::v2f64, 14 }, // Haswell from http://www.agner.org/
447 { ISD::FDIV, MVT::v4f64, 28 }, // Haswell from http://www.agner.org/
450 // Look for AVX2 lowering tricks for custom cases.
452 if (const auto *Entry = CostTableLookup(AVX2CostTable, ISD, LT.second))
453 return LT.first * Entry->Cost;
455 static const CostTblEntry AVX1CostTable[] = {
456 // We don't have to scalarize unsupported ops. We can issue two half-sized
457 // operations and we only need to extract the upper YMM half.
458 // Two ops + 1 extract + 1 insert = 4.
459 { ISD::MUL, MVT::v16i16, 4 },
460 { ISD::MUL, MVT::v8i32, 4 },
461 { ISD::SUB, MVT::v32i8, 4 },
462 { ISD::ADD, MVT::v32i8, 4 },
463 { ISD::SUB, MVT::v16i16, 4 },
464 { ISD::ADD, MVT::v16i16, 4 },
465 { ISD::SUB, MVT::v8i32, 4 },
466 { ISD::ADD, MVT::v8i32, 4 },
467 { ISD::SUB, MVT::v4i64, 4 },
468 { ISD::ADD, MVT::v4i64, 4 },
470 // A v4i64 multiply is custom lowered as two split v2i64 vectors that then
471 // are lowered as a series of long multiplies(3), shifts(3) and adds(2)
472 // Because we believe v4i64 to be a legal type, we must also include the
473 // extract+insert in the cost table. Therefore, the cost here is 18
475 { ISD::MUL, MVT::v4i64, 18 },
477 { ISD::MUL, MVT::v32i8, 26 }, // extend/pmullw/trunc sequence.
479 { ISD::FDIV, MVT::f32, 14 }, // SNB from http://www.agner.org/
480 { ISD::FDIV, MVT::v4f32, 14 }, // SNB from http://www.agner.org/
481 { ISD::FDIV, MVT::v8f32, 28 }, // SNB from http://www.agner.org/
482 { ISD::FDIV, MVT::f64, 22 }, // SNB from http://www.agner.org/
483 { ISD::FDIV, MVT::v2f64, 22 }, // SNB from http://www.agner.org/
484 { ISD::FDIV, MVT::v4f64, 44 }, // SNB from http://www.agner.org/
486 // Vectorizing division is a bad idea. See the SSE2 table for more comments.
487 { ISD::SDIV, MVT::v32i8, 32*20 },
488 { ISD::SDIV, MVT::v16i16, 16*20 },
489 { ISD::SDIV, MVT::v8i32, 8*20 },
490 { ISD::SDIV, MVT::v4i64, 4*20 },
491 { ISD::UDIV, MVT::v32i8, 32*20 },
492 { ISD::UDIV, MVT::v16i16, 16*20 },
493 { ISD::UDIV, MVT::v8i32, 8*20 },
494 { ISD::UDIV, MVT::v4i64, 4*20 },
498 if (const auto *Entry = CostTableLookup(AVX1CostTable, ISD, LT.second))
499 return LT.first * Entry->Cost;
501 static const CostTblEntry SSE42CostTable[] = {
502 { ISD::FDIV, MVT::f32, 14 }, // Nehalem from http://www.agner.org/
503 { ISD::FDIV, MVT::v4f32, 14 }, // Nehalem from http://www.agner.org/
504 { ISD::FDIV, MVT::f64, 22 }, // Nehalem from http://www.agner.org/
505 { ISD::FDIV, MVT::v2f64, 22 }, // Nehalem from http://www.agner.org/
509 if (const auto *Entry = CostTableLookup(SSE42CostTable, ISD, LT.second))
510 return LT.first * Entry->Cost;
512 static const CostTblEntry SSE41CostTable[] = {
513 { ISD::SHL, MVT::v16i8, 11 }, // pblendvb sequence.
514 { ISD::SHL, MVT::v32i8, 2*11 }, // pblendvb sequence.
515 { ISD::SHL, MVT::v8i16, 14 }, // pblendvb sequence.
516 { ISD::SHL, MVT::v16i16, 2*14 }, // pblendvb sequence.
517 { ISD::SHL, MVT::v4i32, 4 }, // pslld/paddd/cvttps2dq/pmulld
518 { ISD::SHL, MVT::v8i32, 2*4 }, // pslld/paddd/cvttps2dq/pmulld
520 { ISD::SRL, MVT::v16i8, 12 }, // pblendvb sequence.
521 { ISD::SRL, MVT::v32i8, 2*12 }, // pblendvb sequence.
522 { ISD::SRL, MVT::v8i16, 14 }, // pblendvb sequence.
523 { ISD::SRL, MVT::v16i16, 2*14 }, // pblendvb sequence.
524 { ISD::SRL, MVT::v4i32, 11 }, // Shift each lane + blend.
525 { ISD::SRL, MVT::v8i32, 2*11 }, // Shift each lane + blend.
527 { ISD::SRA, MVT::v16i8, 24 }, // pblendvb sequence.
528 { ISD::SRA, MVT::v32i8, 2*24 }, // pblendvb sequence.
529 { ISD::SRA, MVT::v8i16, 14 }, // pblendvb sequence.
530 { ISD::SRA, MVT::v16i16, 2*14 }, // pblendvb sequence.
531 { ISD::SRA, MVT::v4i32, 12 }, // Shift each lane + blend.
532 { ISD::SRA, MVT::v8i32, 2*12 }, // Shift each lane + blend.
534 { ISD::MUL, MVT::v4i32, 1 } // pmulld
538 if (const auto *Entry = CostTableLookup(SSE41CostTable, ISD, LT.second))
539 return LT.first * Entry->Cost;
541 static const CostTblEntry SSE2CostTable[] = {
542 // We don't correctly identify costs of casts because they are marked as
544 { ISD::SHL, MVT::v16i8, 26 }, // cmpgtb sequence.
545 { ISD::SHL, MVT::v8i16, 32 }, // cmpgtb sequence.
546 { ISD::SHL, MVT::v4i32, 2*5 }, // We optimized this using mul.
547 { ISD::SHL, MVT::v8i32, 2*2*5 }, // We optimized this using mul.
548 { ISD::SHL, MVT::v2i64, 4 }, // splat+shuffle sequence.
549 { ISD::SHL, MVT::v4i64, 2*4 }, // splat+shuffle sequence.
551 { ISD::SRL, MVT::v16i8, 26 }, // cmpgtb sequence.
552 { ISD::SRL, MVT::v8i16, 32 }, // cmpgtb sequence.
553 { ISD::SRL, MVT::v4i32, 16 }, // Shift each lane + blend.
554 { ISD::SRL, MVT::v2i64, 4 }, // splat+shuffle sequence.
555 { ISD::SRL, MVT::v4i64, 2*4 }, // splat+shuffle sequence.
557 { ISD::SRA, MVT::v16i8, 54 }, // unpacked cmpgtb sequence.
558 { ISD::SRA, MVT::v8i16, 32 }, // cmpgtb sequence.
559 { ISD::SRA, MVT::v4i32, 16 }, // Shift each lane + blend.
560 { ISD::SRA, MVT::v2i64, 12 }, // srl/xor/sub sequence.
561 { ISD::SRA, MVT::v4i64, 2*12 }, // srl/xor/sub sequence.
563 { ISD::MUL, MVT::v16i8, 12 }, // extend/pmullw/trunc sequence.
564 { ISD::MUL, MVT::v8i16, 1 }, // pmullw
565 { ISD::MUL, MVT::v4i32, 6 }, // 3*pmuludq/4*shuffle
566 { ISD::MUL, MVT::v2i64, 8 }, // 3*pmuludq/3*shift/2*add
568 { ISD::FDIV, MVT::f32, 23 }, // Pentium IV from http://www.agner.org/
569 { ISD::FDIV, MVT::v4f32, 39 }, // Pentium IV from http://www.agner.org/
570 { ISD::FDIV, MVT::f64, 38 }, // Pentium IV from http://www.agner.org/
571 { ISD::FDIV, MVT::v2f64, 69 }, // Pentium IV from http://www.agner.org/
573 // It is not a good idea to vectorize division. We have to scalarize it and
574 // in the process we will often end up having to spilling regular
575 // registers. The overhead of division is going to dominate most kernels
576 // anyways so try hard to prevent vectorization of division - it is
577 // generally a bad idea. Assume somewhat arbitrarily that we have to be able
578 // to hide "20 cycles" for each lane.
579 { ISD::SDIV, MVT::v16i8, 16*20 },
580 { ISD::SDIV, MVT::v8i16, 8*20 },
581 { ISD::SDIV, MVT::v4i32, 4*20 },
582 { ISD::SDIV, MVT::v2i64, 2*20 },
583 { ISD::UDIV, MVT::v16i8, 16*20 },
584 { ISD::UDIV, MVT::v8i16, 8*20 },
585 { ISD::UDIV, MVT::v4i32, 4*20 },
586 { ISD::UDIV, MVT::v2i64, 2*20 },
590 if (const auto *Entry = CostTableLookup(SSE2CostTable, ISD, LT.second))
591 return LT.first * Entry->Cost;
593 static const CostTblEntry SSE1CostTable[] = {
594 { ISD::FDIV, MVT::f32, 17 }, // Pentium III from http://www.agner.org/
595 { ISD::FDIV, MVT::v4f32, 34 }, // Pentium III from http://www.agner.org/
599 if (const auto *Entry = CostTableLookup(SSE1CostTable, ISD, LT.second))
600 return LT.first * Entry->Cost;
602 // Fallback to the default implementation.
603 return BaseT::getArithmeticInstrCost(Opcode, Ty, Op1Info, Op2Info);
606 int X86TTIImpl::getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index,
608 // 64-bit packed float vectors (v2f32) are widened to type v4f32.
609 // 64-bit packed integer vectors (v2i32) are promoted to type v2i64.
610 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp);
612 // For Broadcasts we are splatting the first element from the first input
613 // register, so only need to reference that input and all the output
614 // registers are the same.
615 if (Kind == TTI::SK_Broadcast)
618 // We are going to permute multiple sources and the result will be in multiple
619 // destinations. Providing an accurate cost only for splits where the element
620 // type remains the same.
621 if (Kind == TTI::SK_PermuteSingleSrc && LT.first != 1) {
622 MVT LegalVT = LT.second;
623 if (LegalVT.getVectorElementType().getSizeInBits() ==
624 Tp->getVectorElementType()->getPrimitiveSizeInBits() &&
625 LegalVT.getVectorNumElements() < Tp->getVectorNumElements()) {
627 unsigned VecTySize = DL.getTypeStoreSize(Tp);
628 unsigned LegalVTSize = LegalVT.getStoreSize();
629 // Number of source vectors after legalization:
630 unsigned NumOfSrcs = (VecTySize + LegalVTSize - 1) / LegalVTSize;
631 // Number of destination vectors after legalization:
632 unsigned NumOfDests = LT.first;
634 Type *SingleOpTy = VectorType::get(Tp->getVectorElementType(),
635 LegalVT.getVectorNumElements());
637 unsigned NumOfShuffles = (NumOfSrcs - 1) * NumOfDests;
638 return NumOfShuffles *
639 getShuffleCost(TTI::SK_PermuteTwoSrc, SingleOpTy, 0, nullptr);
642 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
645 // For 2-input shuffles, we must account for splitting the 2 inputs into many.
646 if (Kind == TTI::SK_PermuteTwoSrc && LT.first != 1) {
647 // We assume that source and destination have the same vector type.
648 int NumOfDests = LT.first;
649 int NumOfShufflesPerDest = LT.first * 2 - 1;
650 LT.first = NumOfDests * NumOfShufflesPerDest;
653 static const CostTblEntry AVX512VBMIShuffleTbl[] = {
654 { TTI::SK_Reverse, MVT::v64i8, 1 }, // vpermb
655 { TTI::SK_Reverse, MVT::v32i8, 1 }, // vpermb
657 { TTI::SK_PermuteSingleSrc, MVT::v64i8, 1 }, // vpermb
658 { TTI::SK_PermuteSingleSrc, MVT::v32i8, 1 }, // vpermb
660 { TTI::SK_PermuteTwoSrc, MVT::v64i8, 1 }, // vpermt2b
661 { TTI::SK_PermuteTwoSrc, MVT::v32i8, 1 }, // vpermt2b
662 { TTI::SK_PermuteTwoSrc, MVT::v16i8, 1 } // vpermt2b
666 if (const auto *Entry =
667 CostTableLookup(AVX512VBMIShuffleTbl, Kind, LT.second))
668 return LT.first * Entry->Cost;
670 static const CostTblEntry AVX512BWShuffleTbl[] = {
671 { TTI::SK_Broadcast, MVT::v32i16, 1 }, // vpbroadcastw
672 { TTI::SK_Broadcast, MVT::v64i8, 1 }, // vpbroadcastb
674 { TTI::SK_Reverse, MVT::v32i16, 1 }, // vpermw
675 { TTI::SK_Reverse, MVT::v16i16, 1 }, // vpermw
676 { TTI::SK_Reverse, MVT::v64i8, 2 }, // pshufb + vshufi64x2
678 { TTI::SK_PermuteSingleSrc, MVT::v32i16, 1 }, // vpermw
679 { TTI::SK_PermuteSingleSrc, MVT::v16i16, 1 }, // vpermw
680 { TTI::SK_PermuteSingleSrc, MVT::v8i16, 1 }, // vpermw
681 { TTI::SK_PermuteSingleSrc, MVT::v64i8, 8 }, // extend to v32i16
682 { TTI::SK_PermuteSingleSrc, MVT::v32i8, 3 }, // vpermw + zext/trunc
684 { TTI::SK_PermuteTwoSrc, MVT::v32i16, 1 }, // vpermt2w
685 { TTI::SK_PermuteTwoSrc, MVT::v16i16, 1 }, // vpermt2w
686 { TTI::SK_PermuteTwoSrc, MVT::v8i16, 1 }, // vpermt2w
687 { TTI::SK_PermuteTwoSrc, MVT::v32i8, 3 }, // zext + vpermt2w + trunc
688 { TTI::SK_PermuteTwoSrc, MVT::v64i8, 19 }, // 6 * v32i8 + 1
689 { TTI::SK_PermuteTwoSrc, MVT::v16i8, 3 } // zext + vpermt2w + trunc
693 if (const auto *Entry =
694 CostTableLookup(AVX512BWShuffleTbl, Kind, LT.second))
695 return LT.first * Entry->Cost;
697 static const CostTblEntry AVX512ShuffleTbl[] = {
698 { TTI::SK_Broadcast, MVT::v8f64, 1 }, // vbroadcastpd
699 { TTI::SK_Broadcast, MVT::v16f32, 1 }, // vbroadcastps
700 { TTI::SK_Broadcast, MVT::v8i64, 1 }, // vpbroadcastq
701 { TTI::SK_Broadcast, MVT::v16i32, 1 }, // vpbroadcastd
703 { TTI::SK_Reverse, MVT::v8f64, 1 }, // vpermpd
704 { TTI::SK_Reverse, MVT::v16f32, 1 }, // vpermps
705 { TTI::SK_Reverse, MVT::v8i64, 1 }, // vpermq
706 { TTI::SK_Reverse, MVT::v16i32, 1 }, // vpermd
708 { TTI::SK_PermuteSingleSrc, MVT::v8f64, 1 }, // vpermpd
709 { TTI::SK_PermuteSingleSrc, MVT::v4f64, 1 }, // vpermpd
710 { TTI::SK_PermuteSingleSrc, MVT::v2f64, 1 }, // vpermpd
711 { TTI::SK_PermuteSingleSrc, MVT::v16f32, 1 }, // vpermps
712 { TTI::SK_PermuteSingleSrc, MVT::v8f32, 1 }, // vpermps
713 { TTI::SK_PermuteSingleSrc, MVT::v4f32, 1 }, // vpermps
714 { TTI::SK_PermuteSingleSrc, MVT::v8i64, 1 }, // vpermq
715 { TTI::SK_PermuteSingleSrc, MVT::v4i64, 1 }, // vpermq
716 { TTI::SK_PermuteSingleSrc, MVT::v2i64, 1 }, // vpermq
717 { TTI::SK_PermuteSingleSrc, MVT::v16i32, 1 }, // vpermd
718 { TTI::SK_PermuteSingleSrc, MVT::v8i32, 1 }, // vpermd
719 { TTI::SK_PermuteSingleSrc, MVT::v4i32, 1 }, // vpermd
720 { TTI::SK_PermuteSingleSrc, MVT::v16i8, 1 }, // pshufb
722 { TTI::SK_PermuteTwoSrc, MVT::v8f64, 1 }, // vpermt2pd
723 { TTI::SK_PermuteTwoSrc, MVT::v16f32, 1 }, // vpermt2ps
724 { TTI::SK_PermuteTwoSrc, MVT::v8i64, 1 }, // vpermt2q
725 { TTI::SK_PermuteTwoSrc, MVT::v16i32, 1 }, // vpermt2d
726 { TTI::SK_PermuteTwoSrc, MVT::v4f64, 1 }, // vpermt2pd
727 { TTI::SK_PermuteTwoSrc, MVT::v8f32, 1 }, // vpermt2ps
728 { TTI::SK_PermuteTwoSrc, MVT::v4i64, 1 }, // vpermt2q
729 { TTI::SK_PermuteTwoSrc, MVT::v8i32, 1 }, // vpermt2d
730 { TTI::SK_PermuteTwoSrc, MVT::v2f64, 1 }, // vpermt2pd
731 { TTI::SK_PermuteTwoSrc, MVT::v4f32, 1 }, // vpermt2ps
732 { TTI::SK_PermuteTwoSrc, MVT::v2i64, 1 }, // vpermt2q
733 { TTI::SK_PermuteTwoSrc, MVT::v4i32, 1 } // vpermt2d
737 if (const auto *Entry = CostTableLookup(AVX512ShuffleTbl, Kind, LT.second))
738 return LT.first * Entry->Cost;
740 static const CostTblEntry AVX2ShuffleTbl[] = {
741 { TTI::SK_Broadcast, MVT::v4f64, 1 }, // vbroadcastpd
742 { TTI::SK_Broadcast, MVT::v8f32, 1 }, // vbroadcastps
743 { TTI::SK_Broadcast, MVT::v4i64, 1 }, // vpbroadcastq
744 { TTI::SK_Broadcast, MVT::v8i32, 1 }, // vpbroadcastd
745 { TTI::SK_Broadcast, MVT::v16i16, 1 }, // vpbroadcastw
746 { TTI::SK_Broadcast, MVT::v32i8, 1 }, // vpbroadcastb
748 { TTI::SK_Reverse, MVT::v4f64, 1 }, // vpermpd
749 { TTI::SK_Reverse, MVT::v8f32, 1 }, // vpermps
750 { TTI::SK_Reverse, MVT::v4i64, 1 }, // vpermq
751 { TTI::SK_Reverse, MVT::v8i32, 1 }, // vpermd
752 { TTI::SK_Reverse, MVT::v16i16, 2 }, // vperm2i128 + pshufb
753 { TTI::SK_Reverse, MVT::v32i8, 2 }, // vperm2i128 + pshufb
755 { TTI::SK_Alternate, MVT::v16i16, 1 }, // vpblendw
756 { TTI::SK_Alternate, MVT::v32i8, 1 } // vpblendvb
760 if (const auto *Entry = CostTableLookup(AVX2ShuffleTbl, Kind, LT.second))
761 return LT.first * Entry->Cost;
763 static const CostTblEntry AVX1ShuffleTbl[] = {
764 { TTI::SK_Broadcast, MVT::v4f64, 2 }, // vperm2f128 + vpermilpd
765 { TTI::SK_Broadcast, MVT::v8f32, 2 }, // vperm2f128 + vpermilps
766 { TTI::SK_Broadcast, MVT::v4i64, 2 }, // vperm2f128 + vpermilpd
767 { TTI::SK_Broadcast, MVT::v8i32, 2 }, // vperm2f128 + vpermilps
768 { TTI::SK_Broadcast, MVT::v16i16, 3 }, // vpshuflw + vpshufd + vinsertf128
769 { TTI::SK_Broadcast, MVT::v32i8, 2 }, // vpshufb + vinsertf128
771 { TTI::SK_Reverse, MVT::v4f64, 2 }, // vperm2f128 + vpermilpd
772 { TTI::SK_Reverse, MVT::v8f32, 2 }, // vperm2f128 + vpermilps
773 { TTI::SK_Reverse, MVT::v4i64, 2 }, // vperm2f128 + vpermilpd
774 { TTI::SK_Reverse, MVT::v8i32, 2 }, // vperm2f128 + vpermilps
775 { TTI::SK_Reverse, MVT::v16i16, 4 }, // vextractf128 + 2*pshufb
777 { TTI::SK_Reverse, MVT::v32i8, 4 }, // vextractf128 + 2*pshufb
780 { TTI::SK_Alternate, MVT::v4i64, 1 }, // vblendpd
781 { TTI::SK_Alternate, MVT::v4f64, 1 }, // vblendpd
782 { TTI::SK_Alternate, MVT::v8i32, 1 }, // vblendps
783 { TTI::SK_Alternate, MVT::v8f32, 1 }, // vblendps
784 { TTI::SK_Alternate, MVT::v16i16, 3 }, // vpand + vpandn + vpor
785 { TTI::SK_Alternate, MVT::v32i8, 3 } // vpand + vpandn + vpor
789 if (const auto *Entry = CostTableLookup(AVX1ShuffleTbl, Kind, LT.second))
790 return LT.first * Entry->Cost;
792 static const CostTblEntry SSE41ShuffleTbl[] = {
793 { TTI::SK_Alternate, MVT::v2i64, 1 }, // pblendw
794 { TTI::SK_Alternate, MVT::v2f64, 1 }, // movsd
795 { TTI::SK_Alternate, MVT::v4i32, 1 }, // pblendw
796 { TTI::SK_Alternate, MVT::v4f32, 1 }, // blendps
797 { TTI::SK_Alternate, MVT::v8i16, 1 }, // pblendw
798 { TTI::SK_Alternate, MVT::v16i8, 1 } // pblendvb
802 if (const auto *Entry = CostTableLookup(SSE41ShuffleTbl, Kind, LT.second))
803 return LT.first * Entry->Cost;
805 static const CostTblEntry SSSE3ShuffleTbl[] = {
806 { TTI::SK_Broadcast, MVT::v8i16, 1 }, // pshufb
807 { TTI::SK_Broadcast, MVT::v16i8, 1 }, // pshufb
809 { TTI::SK_Reverse, MVT::v8i16, 1 }, // pshufb
810 { TTI::SK_Reverse, MVT::v16i8, 1 }, // pshufb
812 { TTI::SK_Alternate, MVT::v8i16, 3 }, // pshufb + pshufb + por
813 { TTI::SK_Alternate, MVT::v16i8, 3 } // pshufb + pshufb + por
817 if (const auto *Entry = CostTableLookup(SSSE3ShuffleTbl, Kind, LT.second))
818 return LT.first * Entry->Cost;
820 static const CostTblEntry SSE2ShuffleTbl[] = {
821 { TTI::SK_Broadcast, MVT::v2f64, 1 }, // shufpd
822 { TTI::SK_Broadcast, MVT::v2i64, 1 }, // pshufd
823 { TTI::SK_Broadcast, MVT::v4i32, 1 }, // pshufd
824 { TTI::SK_Broadcast, MVT::v8i16, 2 }, // pshuflw + pshufd
825 { TTI::SK_Broadcast, MVT::v16i8, 3 }, // unpck + pshuflw + pshufd
827 { TTI::SK_Reverse, MVT::v2f64, 1 }, // shufpd
828 { TTI::SK_Reverse, MVT::v2i64, 1 }, // pshufd
829 { TTI::SK_Reverse, MVT::v4i32, 1 }, // pshufd
830 { TTI::SK_Reverse, MVT::v8i16, 3 }, // pshuflw + pshufhw + pshufd
831 { TTI::SK_Reverse, MVT::v16i8, 9 }, // 2*pshuflw + 2*pshufhw
832 // + 2*pshufd + 2*unpck + packus
834 { TTI::SK_Alternate, MVT::v2i64, 1 }, // movsd
835 { TTI::SK_Alternate, MVT::v2f64, 1 }, // movsd
836 { TTI::SK_Alternate, MVT::v4i32, 2 }, // 2*shufps
837 { TTI::SK_Alternate, MVT::v8i16, 3 }, // pand + pandn + por
838 { TTI::SK_Alternate, MVT::v16i8, 3 } // pand + pandn + por
842 if (const auto *Entry = CostTableLookup(SSE2ShuffleTbl, Kind, LT.second))
843 return LT.first * Entry->Cost;
845 static const CostTblEntry SSE1ShuffleTbl[] = {
846 { TTI::SK_Broadcast, MVT::v4f32, 1 }, // shufps
847 { TTI::SK_Reverse, MVT::v4f32, 1 }, // shufps
848 { TTI::SK_Alternate, MVT::v4f32, 2 } // 2*shufps
852 if (const auto *Entry = CostTableLookup(SSE1ShuffleTbl, Kind, LT.second))
853 return LT.first * Entry->Cost;
855 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
858 int X86TTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src) {
859 int ISD = TLI->InstructionOpcodeToISD(Opcode);
860 assert(ISD && "Invalid opcode");
862 // FIXME: Need a better design of the cost table to handle non-simple types of
863 // potential massive combinations (elem_num x src_type x dst_type).
865 static const TypeConversionCostTblEntry AVX512DQConversionTbl[] = {
866 { ISD::SINT_TO_FP, MVT::v2f32, MVT::v2i64, 1 },
867 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i64, 1 },
868 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i64, 1 },
869 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i64, 1 },
870 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i64, 1 },
871 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i64, 1 },
873 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i64, 1 },
874 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 1 },
875 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i64, 1 },
876 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i64, 1 },
877 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i64, 1 },
878 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i64, 1 },
880 { ISD::FP_TO_SINT, MVT::v2i64, MVT::v2f32, 1 },
881 { ISD::FP_TO_SINT, MVT::v4i64, MVT::v4f32, 1 },
882 { ISD::FP_TO_SINT, MVT::v8i64, MVT::v8f32, 1 },
883 { ISD::FP_TO_SINT, MVT::v2i64, MVT::v2f64, 1 },
884 { ISD::FP_TO_SINT, MVT::v4i64, MVT::v4f64, 1 },
885 { ISD::FP_TO_SINT, MVT::v8i64, MVT::v8f64, 1 },
887 { ISD::FP_TO_UINT, MVT::v2i64, MVT::v2f32, 1 },
888 { ISD::FP_TO_UINT, MVT::v4i64, MVT::v4f32, 1 },
889 { ISD::FP_TO_UINT, MVT::v8i64, MVT::v8f32, 1 },
890 { ISD::FP_TO_UINT, MVT::v2i64, MVT::v2f64, 1 },
891 { ISD::FP_TO_UINT, MVT::v4i64, MVT::v4f64, 1 },
892 { ISD::FP_TO_UINT, MVT::v8i64, MVT::v8f64, 1 },
895 // TODO: For AVX512DQ + AVX512VL, we also have cheap casts for 128-bit and
896 // 256-bit wide vectors.
898 static const TypeConversionCostTblEntry AVX512FConversionTbl[] = {
899 { ISD::FP_EXTEND, MVT::v8f64, MVT::v8f32, 1 },
900 { ISD::FP_EXTEND, MVT::v8f64, MVT::v16f32, 3 },
901 { ISD::FP_ROUND, MVT::v8f32, MVT::v8f64, 1 },
903 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i32, 1 },
904 { ISD::TRUNCATE, MVT::v16i16, MVT::v16i32, 1 },
905 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i64, 1 },
906 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 1 },
908 // v16i1 -> v16i32 - load + broadcast
909 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i1, 2 },
910 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i1, 2 },
911 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i8, 1 },
912 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i8, 1 },
913 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i16, 1 },
914 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i16, 1 },
915 { ISD::ZERO_EXTEND, MVT::v8i64, MVT::v8i16, 1 },
916 { ISD::SIGN_EXTEND, MVT::v8i64, MVT::v8i16, 1 },
917 { ISD::SIGN_EXTEND, MVT::v8i64, MVT::v8i32, 1 },
918 { ISD::ZERO_EXTEND, MVT::v8i64, MVT::v8i32, 1 },
920 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i1, 4 },
921 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i1, 3 },
922 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i8, 2 },
923 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i8, 2 },
924 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i16, 2 },
925 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i16, 2 },
926 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i32, 1 },
927 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i32, 1 },
928 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i64, 26 },
929 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i64, 26 },
931 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i1, 4 },
932 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i1, 3 },
933 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i8, 2 },
934 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i8, 2 },
935 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i8, 2 },
936 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i8, 2 },
937 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i8, 2 },
938 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i16, 5 },
939 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i16, 2 },
940 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i16, 2 },
941 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i16, 2 },
942 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i16, 2 },
943 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i32, 2 },
944 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i32, 1 },
945 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 },
946 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i32, 1 },
947 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 1 },
948 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i32, 1 },
949 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i32, 1 },
950 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i64, 5 },
951 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 5 },
952 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i64, 12 },
953 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i64, 26 },
955 { ISD::FP_TO_UINT, MVT::v2i32, MVT::v2f32, 1 },
956 { ISD::FP_TO_UINT, MVT::v4i32, MVT::v4f32, 1 },
957 { ISD::FP_TO_UINT, MVT::v8i32, MVT::v8f32, 1 },
958 { ISD::FP_TO_UINT, MVT::v16i32, MVT::v16f32, 1 },
961 static const TypeConversionCostTblEntry AVX2ConversionTbl[] = {
962 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i1, 3 },
963 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i1, 3 },
964 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 3 },
965 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 3 },
966 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 3 },
967 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 3 },
968 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 3 },
969 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 3 },
970 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 1 },
971 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 1 },
972 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
973 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
974 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
975 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
976 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
977 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
979 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i64, 2 },
980 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i64, 2 },
981 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 2 },
982 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 2 },
983 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 2 },
984 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 4 },
986 { ISD::FP_EXTEND, MVT::v8f64, MVT::v8f32, 3 },
987 { ISD::FP_ROUND, MVT::v8f32, MVT::v8f64, 3 },
989 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 8 },
992 static const TypeConversionCostTblEntry AVXConversionTbl[] = {
993 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i1, 6 },
994 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i1, 4 },
995 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 7 },
996 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 4 },
997 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 6 },
998 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 4 },
999 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 7 },
1000 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 4 },
1001 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 4 },
1002 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 4 },
1003 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 6 },
1004 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
1005 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 4 },
1006 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 4 },
1007 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 4 },
1008 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 4 },
1010 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i16, 4 },
1011 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 4 },
1012 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 5 },
1013 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i64, 4 },
1014 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i64, 4 },
1015 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 4 },
1016 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 9 },
1018 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i1, 3 },
1019 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i1, 3 },
1020 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i1, 8 },
1021 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i8, 3 },
1022 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i8, 3 },
1023 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i8, 8 },
1024 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i16, 3 },
1025 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i16, 3 },
1026 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i16, 5 },
1027 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 },
1028 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i32, 1 },
1029 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i32, 1 },
1031 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i1, 7 },
1032 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i1, 7 },
1033 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i1, 6 },
1034 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i8, 2 },
1035 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i8, 2 },
1036 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i8, 5 },
1037 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i16, 2 },
1038 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i16, 2 },
1039 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i16, 5 },
1040 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i32, 6 },
1041 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 6 },
1042 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i32, 6 },
1043 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 9 },
1044 // The generic code to compute the scalar overhead is currently broken.
1045 // Workaround this limitation by estimating the scalarization overhead
1046 // here. We have roughly 10 instructions per scalar element.
1047 // Multiply that by the vector width.
1048 // FIXME: remove that when PR19268 is fixed.
1049 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 10 },
1050 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i64, 20 },
1051 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i64, 13 },
1052 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i64, 13 },
1054 { ISD::FP_TO_SINT, MVT::v4i8, MVT::v4f32, 1 },
1055 { ISD::FP_TO_SINT, MVT::v8i8, MVT::v8f32, 7 },
1056 // This node is expanded into scalarized operations but BasicTTI is overly
1057 // optimistic estimating its cost. It computes 3 per element (one
1058 // vector-extract, one scalar conversion and one vector-insert). The
1059 // problem is that the inserts form a read-modify-write chain so latency
1060 // should be factored in too. Inflating the cost per element by 1.
1061 { ISD::FP_TO_UINT, MVT::v8i32, MVT::v8f32, 8*4 },
1062 { ISD::FP_TO_UINT, MVT::v4i32, MVT::v4f64, 4*4 },
1064 { ISD::FP_EXTEND, MVT::v4f64, MVT::v4f32, 1 },
1065 { ISD::FP_ROUND, MVT::v4f32, MVT::v4f64, 1 },
1068 static const TypeConversionCostTblEntry SSE41ConversionTbl[] = {
1069 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 2 },
1070 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 2 },
1071 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 2 },
1072 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 2 },
1073 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 2 },
1074 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 2 },
1076 { ISD::ZERO_EXTEND, MVT::v4i16, MVT::v4i8, 1 },
1077 { ISD::SIGN_EXTEND, MVT::v4i16, MVT::v4i8, 2 },
1078 { ISD::ZERO_EXTEND, MVT::v4i32, MVT::v4i8, 1 },
1079 { ISD::SIGN_EXTEND, MVT::v4i32, MVT::v4i8, 1 },
1080 { ISD::ZERO_EXTEND, MVT::v8i16, MVT::v8i8, 1 },
1081 { ISD::SIGN_EXTEND, MVT::v8i16, MVT::v8i8, 1 },
1082 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 2 },
1083 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 2 },
1084 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 2 },
1085 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 2 },
1086 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i8, 4 },
1087 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i8, 4 },
1088 { ISD::ZERO_EXTEND, MVT::v4i32, MVT::v4i16, 1 },
1089 { ISD::SIGN_EXTEND, MVT::v4i32, MVT::v4i16, 1 },
1090 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 2 },
1091 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 2 },
1092 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i16, 4 },
1093 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i16, 4 },
1095 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i16, 2 },
1096 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i16, 1 },
1097 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i32, 1 },
1098 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i32, 1 },
1099 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 3 },
1100 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 3 },
1101 { ISD::TRUNCATE, MVT::v16i16, MVT::v16i32, 6 },
1105 static const TypeConversionCostTblEntry SSE2ConversionTbl[] = {
1106 // These are somewhat magic numbers justified by looking at the output of
1107 // Intel's IACA, running some kernels and making sure when we take
1108 // legalization into account the throughput will be overestimated.
1109 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 },
1110 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 },
1111 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 },
1112 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 },
1113 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 5 },
1114 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 },
1115 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 },
1116 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 },
1118 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 },
1119 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 },
1120 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 },
1121 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 },
1122 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 },
1123 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 8 },
1124 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 },
1125 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 },
1127 { ISD::FP_TO_SINT, MVT::v2i32, MVT::v2f64, 3 },
1129 { ISD::ZERO_EXTEND, MVT::v4i16, MVT::v4i8, 1 },
1130 { ISD::SIGN_EXTEND, MVT::v4i16, MVT::v4i8, 6 },
1131 { ISD::ZERO_EXTEND, MVT::v4i32, MVT::v4i8, 2 },
1132 { ISD::SIGN_EXTEND, MVT::v4i32, MVT::v4i8, 3 },
1133 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 4 },
1134 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 8 },
1135 { ISD::ZERO_EXTEND, MVT::v8i16, MVT::v8i8, 1 },
1136 { ISD::SIGN_EXTEND, MVT::v8i16, MVT::v8i8, 2 },
1137 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 6 },
1138 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 6 },
1139 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 3 },
1140 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 4 },
1141 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i8, 9 },
1142 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i8, 12 },
1143 { ISD::ZERO_EXTEND, MVT::v4i32, MVT::v4i16, 1 },
1144 { ISD::SIGN_EXTEND, MVT::v4i32, MVT::v4i16, 2 },
1145 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
1146 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 10 },
1147 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 3 },
1148 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 4 },
1149 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i16, 6 },
1150 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i16, 8 },
1151 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 3 },
1152 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 5 },
1154 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i16, 4 },
1155 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i16, 2 },
1156 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i16, 3 },
1157 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i32, 3 },
1158 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i32, 3 },
1159 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 4 },
1160 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i32, 7 },
1161 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 5 },
1162 { ISD::TRUNCATE, MVT::v16i16, MVT::v16i32, 10 },
1165 std::pair<int, MVT> LTSrc = TLI->getTypeLegalizationCost(DL, Src);
1166 std::pair<int, MVT> LTDest = TLI->getTypeLegalizationCost(DL, Dst);
1168 if (ST->hasSSE2() && !ST->hasAVX()) {
1169 if (const auto *Entry = ConvertCostTableLookup(SSE2ConversionTbl, ISD,
1170 LTDest.second, LTSrc.second))
1171 return LTSrc.first * Entry->Cost;
1174 EVT SrcTy = TLI->getValueType(DL, Src);
1175 EVT DstTy = TLI->getValueType(DL, Dst);
1177 // The function getSimpleVT only handles simple value types.
1178 if (!SrcTy.isSimple() || !DstTy.isSimple())
1179 return BaseT::getCastInstrCost(Opcode, Dst, Src);
1182 if (const auto *Entry = ConvertCostTableLookup(AVX512DQConversionTbl, ISD,
1183 DstTy.getSimpleVT(),
1184 SrcTy.getSimpleVT()))
1187 if (ST->hasAVX512())
1188 if (const auto *Entry = ConvertCostTableLookup(AVX512FConversionTbl, ISD,
1189 DstTy.getSimpleVT(),
1190 SrcTy.getSimpleVT()))
1193 if (ST->hasAVX2()) {
1194 if (const auto *Entry = ConvertCostTableLookup(AVX2ConversionTbl, ISD,
1195 DstTy.getSimpleVT(),
1196 SrcTy.getSimpleVT()))
1201 if (const auto *Entry = ConvertCostTableLookup(AVXConversionTbl, ISD,
1202 DstTy.getSimpleVT(),
1203 SrcTy.getSimpleVT()))
1207 if (ST->hasSSE41()) {
1208 if (const auto *Entry = ConvertCostTableLookup(SSE41ConversionTbl, ISD,
1209 DstTy.getSimpleVT(),
1210 SrcTy.getSimpleVT()))
1214 if (ST->hasSSE2()) {
1215 if (const auto *Entry = ConvertCostTableLookup(SSE2ConversionTbl, ISD,
1216 DstTy.getSimpleVT(),
1217 SrcTy.getSimpleVT()))
1221 return BaseT::getCastInstrCost(Opcode, Dst, Src);
1224 int X86TTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy) {
1225 // Legalize the type.
1226 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, ValTy);
1228 MVT MTy = LT.second;
1230 int ISD = TLI->InstructionOpcodeToISD(Opcode);
1231 assert(ISD && "Invalid opcode");
1233 static const CostTblEntry SSE2CostTbl[] = {
1234 { ISD::SETCC, MVT::v2i64, 8 },
1235 { ISD::SETCC, MVT::v4i32, 1 },
1236 { ISD::SETCC, MVT::v8i16, 1 },
1237 { ISD::SETCC, MVT::v16i8, 1 },
1240 static const CostTblEntry SSE42CostTbl[] = {
1241 { ISD::SETCC, MVT::v2f64, 1 },
1242 { ISD::SETCC, MVT::v4f32, 1 },
1243 { ISD::SETCC, MVT::v2i64, 1 },
1246 static const CostTblEntry AVX1CostTbl[] = {
1247 { ISD::SETCC, MVT::v4f64, 1 },
1248 { ISD::SETCC, MVT::v8f32, 1 },
1249 // AVX1 does not support 8-wide integer compare.
1250 { ISD::SETCC, MVT::v4i64, 4 },
1251 { ISD::SETCC, MVT::v8i32, 4 },
1252 { ISD::SETCC, MVT::v16i16, 4 },
1253 { ISD::SETCC, MVT::v32i8, 4 },
1256 static const CostTblEntry AVX2CostTbl[] = {
1257 { ISD::SETCC, MVT::v4i64, 1 },
1258 { ISD::SETCC, MVT::v8i32, 1 },
1259 { ISD::SETCC, MVT::v16i16, 1 },
1260 { ISD::SETCC, MVT::v32i8, 1 },
1263 static const CostTblEntry AVX512CostTbl[] = {
1264 { ISD::SETCC, MVT::v8i64, 1 },
1265 { ISD::SETCC, MVT::v16i32, 1 },
1266 { ISD::SETCC, MVT::v8f64, 1 },
1267 { ISD::SETCC, MVT::v16f32, 1 },
1270 if (ST->hasAVX512())
1271 if (const auto *Entry = CostTableLookup(AVX512CostTbl, ISD, MTy))
1272 return LT.first * Entry->Cost;
1275 if (const auto *Entry = CostTableLookup(AVX2CostTbl, ISD, MTy))
1276 return LT.first * Entry->Cost;
1279 if (const auto *Entry = CostTableLookup(AVX1CostTbl, ISD, MTy))
1280 return LT.first * Entry->Cost;
1283 if (const auto *Entry = CostTableLookup(SSE42CostTbl, ISD, MTy))
1284 return LT.first * Entry->Cost;
1287 if (const auto *Entry = CostTableLookup(SSE2CostTbl, ISD, MTy))
1288 return LT.first * Entry->Cost;
1290 return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy);
1293 int X86TTIImpl::getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy,
1294 ArrayRef<Type *> Tys, FastMathFlags FMF) {
1295 // Costs should match the codegen from:
1296 // BITREVERSE: llvm\test\CodeGen\X86\vector-bitreverse.ll
1297 // BSWAP: llvm\test\CodeGen\X86\bswap-vector.ll
1298 // CTLZ: llvm\test\CodeGen\X86\vector-lzcnt-*.ll
1299 // CTPOP: llvm\test\CodeGen\X86\vector-popcnt-*.ll
1300 // CTTZ: llvm\test\CodeGen\X86\vector-tzcnt-*.ll
1301 static const CostTblEntry XOPCostTbl[] = {
1302 { ISD::BITREVERSE, MVT::v4i64, 4 },
1303 { ISD::BITREVERSE, MVT::v8i32, 4 },
1304 { ISD::BITREVERSE, MVT::v16i16, 4 },
1305 { ISD::BITREVERSE, MVT::v32i8, 4 },
1306 { ISD::BITREVERSE, MVT::v2i64, 1 },
1307 { ISD::BITREVERSE, MVT::v4i32, 1 },
1308 { ISD::BITREVERSE, MVT::v8i16, 1 },
1309 { ISD::BITREVERSE, MVT::v16i8, 1 },
1310 { ISD::BITREVERSE, MVT::i64, 3 },
1311 { ISD::BITREVERSE, MVT::i32, 3 },
1312 { ISD::BITREVERSE, MVT::i16, 3 },
1313 { ISD::BITREVERSE, MVT::i8, 3 }
1315 static const CostTblEntry AVX2CostTbl[] = {
1316 { ISD::BITREVERSE, MVT::v4i64, 5 },
1317 { ISD::BITREVERSE, MVT::v8i32, 5 },
1318 { ISD::BITREVERSE, MVT::v16i16, 5 },
1319 { ISD::BITREVERSE, MVT::v32i8, 5 },
1320 { ISD::BSWAP, MVT::v4i64, 1 },
1321 { ISD::BSWAP, MVT::v8i32, 1 },
1322 { ISD::BSWAP, MVT::v16i16, 1 },
1323 { ISD::CTLZ, MVT::v4i64, 23 },
1324 { ISD::CTLZ, MVT::v8i32, 18 },
1325 { ISD::CTLZ, MVT::v16i16, 14 },
1326 { ISD::CTLZ, MVT::v32i8, 9 },
1327 { ISD::CTPOP, MVT::v4i64, 7 },
1328 { ISD::CTPOP, MVT::v8i32, 11 },
1329 { ISD::CTPOP, MVT::v16i16, 9 },
1330 { ISD::CTPOP, MVT::v32i8, 6 },
1331 { ISD::CTTZ, MVT::v4i64, 10 },
1332 { ISD::CTTZ, MVT::v8i32, 14 },
1333 { ISD::CTTZ, MVT::v16i16, 12 },
1334 { ISD::CTTZ, MVT::v32i8, 9 },
1335 { ISD::FSQRT, MVT::f32, 7 }, // Haswell from http://www.agner.org/
1336 { ISD::FSQRT, MVT::v4f32, 7 }, // Haswell from http://www.agner.org/
1337 { ISD::FSQRT, MVT::v8f32, 14 }, // Haswell from http://www.agner.org/
1338 { ISD::FSQRT, MVT::f64, 14 }, // Haswell from http://www.agner.org/
1339 { ISD::FSQRT, MVT::v2f64, 14 }, // Haswell from http://www.agner.org/
1340 { ISD::FSQRT, MVT::v4f64, 28 }, // Haswell from http://www.agner.org/
1342 static const CostTblEntry AVX1CostTbl[] = {
1343 { ISD::BITREVERSE, MVT::v4i64, 10 },
1344 { ISD::BITREVERSE, MVT::v8i32, 10 },
1345 { ISD::BITREVERSE, MVT::v16i16, 10 },
1346 { ISD::BITREVERSE, MVT::v32i8, 10 },
1347 { ISD::BSWAP, MVT::v4i64, 4 },
1348 { ISD::BSWAP, MVT::v8i32, 4 },
1349 { ISD::BSWAP, MVT::v16i16, 4 },
1350 { ISD::CTLZ, MVT::v4i64, 46 },
1351 { ISD::CTLZ, MVT::v8i32, 36 },
1352 { ISD::CTLZ, MVT::v16i16, 28 },
1353 { ISD::CTLZ, MVT::v32i8, 18 },
1354 { ISD::CTPOP, MVT::v4i64, 14 },
1355 { ISD::CTPOP, MVT::v8i32, 22 },
1356 { ISD::CTPOP, MVT::v16i16, 18 },
1357 { ISD::CTPOP, MVT::v32i8, 12 },
1358 { ISD::CTTZ, MVT::v4i64, 20 },
1359 { ISD::CTTZ, MVT::v8i32, 28 },
1360 { ISD::CTTZ, MVT::v16i16, 24 },
1361 { ISD::CTTZ, MVT::v32i8, 18 },
1362 { ISD::FSQRT, MVT::f32, 14 }, // SNB from http://www.agner.org/
1363 { ISD::FSQRT, MVT::v4f32, 14 }, // SNB from http://www.agner.org/
1364 { ISD::FSQRT, MVT::v8f32, 28 }, // SNB from http://www.agner.org/
1365 { ISD::FSQRT, MVT::f64, 21 }, // SNB from http://www.agner.org/
1366 { ISD::FSQRT, MVT::v2f64, 21 }, // SNB from http://www.agner.org/
1367 { ISD::FSQRT, MVT::v4f64, 43 }, // SNB from http://www.agner.org/
1369 static const CostTblEntry SSE42CostTbl[] = {
1370 { ISD::FSQRT, MVT::f32, 18 }, // Nehalem from http://www.agner.org/
1371 { ISD::FSQRT, MVT::v4f32, 18 }, // Nehalem from http://www.agner.org/
1373 static const CostTblEntry SSSE3CostTbl[] = {
1374 { ISD::BITREVERSE, MVT::v2i64, 5 },
1375 { ISD::BITREVERSE, MVT::v4i32, 5 },
1376 { ISD::BITREVERSE, MVT::v8i16, 5 },
1377 { ISD::BITREVERSE, MVT::v16i8, 5 },
1378 { ISD::BSWAP, MVT::v2i64, 1 },
1379 { ISD::BSWAP, MVT::v4i32, 1 },
1380 { ISD::BSWAP, MVT::v8i16, 1 },
1381 { ISD::CTLZ, MVT::v2i64, 23 },
1382 { ISD::CTLZ, MVT::v4i32, 18 },
1383 { ISD::CTLZ, MVT::v8i16, 14 },
1384 { ISD::CTLZ, MVT::v16i8, 9 },
1385 { ISD::CTPOP, MVT::v2i64, 7 },
1386 { ISD::CTPOP, MVT::v4i32, 11 },
1387 { ISD::CTPOP, MVT::v8i16, 9 },
1388 { ISD::CTPOP, MVT::v16i8, 6 },
1389 { ISD::CTTZ, MVT::v2i64, 10 },
1390 { ISD::CTTZ, MVT::v4i32, 14 },
1391 { ISD::CTTZ, MVT::v8i16, 12 },
1392 { ISD::CTTZ, MVT::v16i8, 9 }
1394 static const CostTblEntry SSE2CostTbl[] = {
1395 { ISD::BSWAP, MVT::v2i64, 7 },
1396 { ISD::BSWAP, MVT::v4i32, 7 },
1397 { ISD::BSWAP, MVT::v8i16, 7 },
1398 { ISD::CTLZ, MVT::v2i64, 25 },
1399 { ISD::CTLZ, MVT::v4i32, 26 },
1400 { ISD::CTLZ, MVT::v8i16, 20 },
1401 { ISD::CTLZ, MVT::v16i8, 17 },
1402 { ISD::CTPOP, MVT::v2i64, 12 },
1403 { ISD::CTPOP, MVT::v4i32, 15 },
1404 { ISD::CTPOP, MVT::v8i16, 13 },
1405 { ISD::CTPOP, MVT::v16i8, 10 },
1406 { ISD::CTTZ, MVT::v2i64, 14 },
1407 { ISD::CTTZ, MVT::v4i32, 18 },
1408 { ISD::CTTZ, MVT::v8i16, 16 },
1409 { ISD::CTTZ, MVT::v16i8, 13 },
1410 { ISD::FSQRT, MVT::f64, 32 }, // Nehalem from http://www.agner.org/
1411 { ISD::FSQRT, MVT::v2f64, 32 }, // Nehalem from http://www.agner.org/
1413 static const CostTblEntry SSE1CostTbl[] = {
1414 { ISD::FSQRT, MVT::f32, 28 }, // Pentium III from http://www.agner.org/
1415 { ISD::FSQRT, MVT::v4f32, 56 }, // Pentium III from http://www.agner.org/
1418 unsigned ISD = ISD::DELETED_NODE;
1422 case Intrinsic::bitreverse:
1423 ISD = ISD::BITREVERSE;
1425 case Intrinsic::bswap:
1428 case Intrinsic::ctlz:
1431 case Intrinsic::ctpop:
1434 case Intrinsic::cttz:
1437 case Intrinsic::sqrt:
1442 // Legalize the type.
1443 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, RetTy);
1444 MVT MTy = LT.second;
1446 // Attempt to lookup cost.
1448 if (const auto *Entry = CostTableLookup(XOPCostTbl, ISD, MTy))
1449 return LT.first * Entry->Cost;
1452 if (const auto *Entry = CostTableLookup(AVX2CostTbl, ISD, MTy))
1453 return LT.first * Entry->Cost;
1456 if (const auto *Entry = CostTableLookup(AVX1CostTbl, ISD, MTy))
1457 return LT.first * Entry->Cost;
1460 if (const auto *Entry = CostTableLookup(SSE42CostTbl, ISD, MTy))
1461 return LT.first * Entry->Cost;
1464 if (const auto *Entry = CostTableLookup(SSSE3CostTbl, ISD, MTy))
1465 return LT.first * Entry->Cost;
1468 if (const auto *Entry = CostTableLookup(SSE2CostTbl, ISD, MTy))
1469 return LT.first * Entry->Cost;
1472 if (const auto *Entry = CostTableLookup(SSE1CostTbl, ISD, MTy))
1473 return LT.first * Entry->Cost;
1475 return BaseT::getIntrinsicInstrCost(IID, RetTy, Tys, FMF);
1478 int X86TTIImpl::getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy,
1479 ArrayRef<Value *> Args, FastMathFlags FMF) {
1480 return BaseT::getIntrinsicInstrCost(IID, RetTy, Args, FMF);
1483 int X86TTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index) {
1484 assert(Val->isVectorTy() && "This must be a vector type");
1486 Type *ScalarType = Val->getScalarType();
1489 // Legalize the type.
1490 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Val);
1492 // This type is legalized to a scalar type.
1493 if (!LT.second.isVector())
1496 // The type may be split. Normalize the index to the new type.
1497 unsigned Width = LT.second.getVectorNumElements();
1498 Index = Index % Width;
1500 // Floating point scalars are already located in index #0.
1501 if (ScalarType->isFloatingPointTy() && Index == 0)
1505 // Add to the base cost if we know that the extracted element of a vector is
1506 // destined to be moved to and used in the integer register file.
1507 int RegisterFileMoveCost = 0;
1508 if (Opcode == Instruction::ExtractElement && ScalarType->isPointerTy())
1509 RegisterFileMoveCost = 1;
1511 return BaseT::getVectorInstrCost(Opcode, Val, Index) + RegisterFileMoveCost;
1514 int X86TTIImpl::getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) {
1515 assert (Ty->isVectorTy() && "Can only scalarize vectors");
1518 for (int i = 0, e = Ty->getVectorNumElements(); i < e; ++i) {
1520 Cost += getVectorInstrCost(Instruction::InsertElement, Ty, i);
1522 Cost += getVectorInstrCost(Instruction::ExtractElement, Ty, i);
1528 int X86TTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
1529 unsigned AddressSpace) {
1530 // Handle non-power-of-two vectors such as <3 x float>
1531 if (VectorType *VTy = dyn_cast<VectorType>(Src)) {
1532 unsigned NumElem = VTy->getVectorNumElements();
1534 // Handle a few common cases:
1536 if (NumElem == 3 && VTy->getScalarSizeInBits() == 32)
1537 // Cost = 64 bit store + extract + 32 bit store.
1541 if (NumElem == 3 && VTy->getScalarSizeInBits() == 64)
1542 // Cost = 128 bit store + unpack + 64 bit store.
1545 // Assume that all other non-power-of-two numbers are scalarized.
1546 if (!isPowerOf2_32(NumElem)) {
1547 int Cost = BaseT::getMemoryOpCost(Opcode, VTy->getScalarType(), Alignment,
1549 int SplitCost = getScalarizationOverhead(Src, Opcode == Instruction::Load,
1550 Opcode == Instruction::Store);
1551 return NumElem * Cost + SplitCost;
1555 // Legalize the type.
1556 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Src);
1557 assert((Opcode == Instruction::Load || Opcode == Instruction::Store) &&
1560 // Each load/store unit costs 1.
1561 int Cost = LT.first * 1;
1563 // This isn't exactly right. We're using slow unaligned 32-byte accesses as a
1564 // proxy for a double-pumped AVX memory interface such as on Sandybridge.
1565 if (LT.second.getStoreSize() == 32 && ST->isUnalignedMem32Slow())
1571 int X86TTIImpl::getMaskedMemoryOpCost(unsigned Opcode, Type *SrcTy,
1573 unsigned AddressSpace) {
1574 VectorType *SrcVTy = dyn_cast<VectorType>(SrcTy);
1576 // To calculate scalar take the regular cost, without mask
1577 return getMemoryOpCost(Opcode, SrcTy, Alignment, AddressSpace);
1579 unsigned NumElem = SrcVTy->getVectorNumElements();
1580 VectorType *MaskTy =
1581 VectorType::get(Type::getInt8Ty(SrcVTy->getContext()), NumElem);
1582 if ((Opcode == Instruction::Load && !isLegalMaskedLoad(SrcVTy)) ||
1583 (Opcode == Instruction::Store && !isLegalMaskedStore(SrcVTy)) ||
1584 !isPowerOf2_32(NumElem)) {
1586 int MaskSplitCost = getScalarizationOverhead(MaskTy, false, true);
1587 int ScalarCompareCost = getCmpSelInstrCost(
1588 Instruction::ICmp, Type::getInt8Ty(SrcVTy->getContext()), nullptr);
1589 int BranchCost = getCFInstrCost(Instruction::Br);
1590 int MaskCmpCost = NumElem * (BranchCost + ScalarCompareCost);
1592 int ValueSplitCost = getScalarizationOverhead(
1593 SrcVTy, Opcode == Instruction::Load, Opcode == Instruction::Store);
1595 NumElem * BaseT::getMemoryOpCost(Opcode, SrcVTy->getScalarType(),
1596 Alignment, AddressSpace);
1597 return MemopCost + ValueSplitCost + MaskSplitCost + MaskCmpCost;
1600 // Legalize the type.
1601 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, SrcVTy);
1602 auto VT = TLI->getValueType(DL, SrcVTy);
1604 if (VT.isSimple() && LT.second != VT.getSimpleVT() &&
1605 LT.second.getVectorNumElements() == NumElem)
1606 // Promotion requires expand/truncate for data and a shuffle for mask.
1607 Cost += getShuffleCost(TTI::SK_Alternate, SrcVTy, 0, nullptr) +
1608 getShuffleCost(TTI::SK_Alternate, MaskTy, 0, nullptr);
1610 else if (LT.second.getVectorNumElements() > NumElem) {
1611 VectorType *NewMaskTy = VectorType::get(MaskTy->getVectorElementType(),
1612 LT.second.getVectorNumElements());
1613 // Expanding requires fill mask with zeroes
1614 Cost += getShuffleCost(TTI::SK_InsertSubvector, NewMaskTy, 0, MaskTy);
1616 if (!ST->hasAVX512())
1617 return Cost + LT.first*4; // Each maskmov costs 4
1619 // AVX-512 masked load/store is cheapper
1620 return Cost+LT.first;
1623 int X86TTIImpl::getAddressComputationCost(Type *Ty, ScalarEvolution *SE,
1625 // Address computations in vectorized code with non-consecutive addresses will
1626 // likely result in more instructions compared to scalar code where the
1627 // computation can more often be merged into the index mode. The resulting
1628 // extra micro-ops can significantly decrease throughput.
1629 unsigned NumVectorInstToHideOverhead = 10;
1631 // Cost modeling of Strided Access Computation is hidden by the indexing
1632 // modes of X86 regardless of the stride value. We dont believe that there
1633 // is a difference between constant strided access in gerenal and constant
1634 // strided value which is less than or equal to 64.
1635 // Even in the case of (loop invariant) stride whose value is not known at
1636 // compile time, the address computation will not incur more than one extra
1638 if (Ty->isVectorTy() && SE) {
1639 if (!BaseT::isStridedAccess(Ptr))
1640 return NumVectorInstToHideOverhead;
1641 if (!BaseT::getConstantStrideStep(SE, Ptr))
1645 return BaseT::getAddressComputationCost(Ty, SE, Ptr);
1648 int X86TTIImpl::getReductionCost(unsigned Opcode, Type *ValTy,
1651 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, ValTy);
1653 MVT MTy = LT.second;
1655 int ISD = TLI->InstructionOpcodeToISD(Opcode);
1656 assert(ISD && "Invalid opcode");
1658 // We use the Intel Architecture Code Analyzer(IACA) to measure the throughput
1659 // and make it as the cost.
1661 static const CostTblEntry SSE42CostTblPairWise[] = {
1662 { ISD::FADD, MVT::v2f64, 2 },
1663 { ISD::FADD, MVT::v4f32, 4 },
1664 { ISD::ADD, MVT::v2i64, 2 }, // The data reported by the IACA tool is "1.6".
1665 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.5".
1666 { ISD::ADD, MVT::v8i16, 5 },
1669 static const CostTblEntry AVX1CostTblPairWise[] = {
1670 { ISD::FADD, MVT::v4f32, 4 },
1671 { ISD::FADD, MVT::v4f64, 5 },
1672 { ISD::FADD, MVT::v8f32, 7 },
1673 { ISD::ADD, MVT::v2i64, 1 }, // The data reported by the IACA tool is "1.5".
1674 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.5".
1675 { ISD::ADD, MVT::v4i64, 5 }, // The data reported by the IACA tool is "4.8".
1676 { ISD::ADD, MVT::v8i16, 5 },
1677 { ISD::ADD, MVT::v8i32, 5 },
1680 static const CostTblEntry SSE42CostTblNoPairWise[] = {
1681 { ISD::FADD, MVT::v2f64, 2 },
1682 { ISD::FADD, MVT::v4f32, 4 },
1683 { ISD::ADD, MVT::v2i64, 2 }, // The data reported by the IACA tool is "1.6".
1684 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.3".
1685 { ISD::ADD, MVT::v8i16, 4 }, // The data reported by the IACA tool is "4.3".
1688 static const CostTblEntry AVX1CostTblNoPairWise[] = {
1689 { ISD::FADD, MVT::v4f32, 3 },
1690 { ISD::FADD, MVT::v4f64, 3 },
1691 { ISD::FADD, MVT::v8f32, 4 },
1692 { ISD::ADD, MVT::v2i64, 1 }, // The data reported by the IACA tool is "1.5".
1693 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "2.8".
1694 { ISD::ADD, MVT::v4i64, 3 },
1695 { ISD::ADD, MVT::v8i16, 4 },
1696 { ISD::ADD, MVT::v8i32, 5 },
1701 if (const auto *Entry = CostTableLookup(AVX1CostTblPairWise, ISD, MTy))
1702 return LT.first * Entry->Cost;
1705 if (const auto *Entry = CostTableLookup(SSE42CostTblPairWise, ISD, MTy))
1706 return LT.first * Entry->Cost;
1709 if (const auto *Entry = CostTableLookup(AVX1CostTblNoPairWise, ISD, MTy))
1710 return LT.first * Entry->Cost;
1713 if (const auto *Entry = CostTableLookup(SSE42CostTblNoPairWise, ISD, MTy))
1714 return LT.first * Entry->Cost;
1717 return BaseT::getReductionCost(Opcode, ValTy, IsPairwise);
1720 /// \brief Calculate the cost of materializing a 64-bit value. This helper
1721 /// method might only calculate a fraction of a larger immediate. Therefore it
1722 /// is valid to return a cost of ZERO.
1723 int X86TTIImpl::getIntImmCost(int64_t Val) {
1725 return TTI::TCC_Free;
1728 return TTI::TCC_Basic;
1730 return 2 * TTI::TCC_Basic;
1733 int X86TTIImpl::getIntImmCost(const APInt &Imm, Type *Ty) {
1734 assert(Ty->isIntegerTy());
1736 unsigned BitSize = Ty->getPrimitiveSizeInBits();
1740 // Never hoist constants larger than 128bit, because this might lead to
1741 // incorrect code generation or assertions in codegen.
1742 // Fixme: Create a cost model for types larger than i128 once the codegen
1743 // issues have been fixed.
1745 return TTI::TCC_Free;
1748 return TTI::TCC_Free;
1750 // Sign-extend all constants to a multiple of 64-bit.
1753 ImmVal = Imm.sext((BitSize + 63) & ~0x3fU);
1755 // Split the constant into 64-bit chunks and calculate the cost for each
1758 for (unsigned ShiftVal = 0; ShiftVal < BitSize; ShiftVal += 64) {
1759 APInt Tmp = ImmVal.ashr(ShiftVal).sextOrTrunc(64);
1760 int64_t Val = Tmp.getSExtValue();
1761 Cost += getIntImmCost(Val);
1763 // We need at least one instruction to materialize the constant.
1764 return std::max(1, Cost);
1767 int X86TTIImpl::getIntImmCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
1769 assert(Ty->isIntegerTy());
1771 unsigned BitSize = Ty->getPrimitiveSizeInBits();
1772 // There is no cost model for constants with a bit size of 0. Return TCC_Free
1773 // here, so that constant hoisting will ignore this constant.
1775 return TTI::TCC_Free;
1777 unsigned ImmIdx = ~0U;
1780 return TTI::TCC_Free;
1781 case Instruction::GetElementPtr:
1782 // Always hoist the base address of a GetElementPtr. This prevents the
1783 // creation of new constants for every base constant that gets constant
1784 // folded with the offset.
1786 return 2 * TTI::TCC_Basic;
1787 return TTI::TCC_Free;
1788 case Instruction::Store:
1791 case Instruction::ICmp:
1792 // This is an imperfect hack to prevent constant hoisting of
1793 // compares that might be trying to check if a 64-bit value fits in
1794 // 32-bits. The backend can optimize these cases using a right shift by 32.
1795 // Ideally we would check the compare predicate here. There also other
1796 // similar immediates the backend can use shifts for.
1797 if (Idx == 1 && Imm.getBitWidth() == 64) {
1798 uint64_t ImmVal = Imm.getZExtValue();
1799 if (ImmVal == 0x100000000ULL || ImmVal == 0xffffffff)
1800 return TTI::TCC_Free;
1804 case Instruction::And:
1805 // We support 64-bit ANDs with immediates with 32-bits of leading zeroes
1806 // by using a 32-bit operation with implicit zero extension. Detect such
1807 // immediates here as the normal path expects bit 31 to be sign extended.
1808 if (Idx == 1 && Imm.getBitWidth() == 64 && isUInt<32>(Imm.getZExtValue()))
1809 return TTI::TCC_Free;
1811 case Instruction::Add:
1812 case Instruction::Sub:
1813 case Instruction::Mul:
1814 case Instruction::UDiv:
1815 case Instruction::SDiv:
1816 case Instruction::URem:
1817 case Instruction::SRem:
1818 case Instruction::Or:
1819 case Instruction::Xor:
1822 // Always return TCC_Free for the shift value of a shift instruction.
1823 case Instruction::Shl:
1824 case Instruction::LShr:
1825 case Instruction::AShr:
1827 return TTI::TCC_Free;
1829 case Instruction::Trunc:
1830 case Instruction::ZExt:
1831 case Instruction::SExt:
1832 case Instruction::IntToPtr:
1833 case Instruction::PtrToInt:
1834 case Instruction::BitCast:
1835 case Instruction::PHI:
1836 case Instruction::Call:
1837 case Instruction::Select:
1838 case Instruction::Ret:
1839 case Instruction::Load:
1843 if (Idx == ImmIdx) {
1844 int NumConstants = (BitSize + 63) / 64;
1845 int Cost = X86TTIImpl::getIntImmCost(Imm, Ty);
1846 return (Cost <= NumConstants * TTI::TCC_Basic)
1847 ? static_cast<int>(TTI::TCC_Free)
1851 return X86TTIImpl::getIntImmCost(Imm, Ty);
1854 int X86TTIImpl::getIntImmCost(Intrinsic::ID IID, unsigned Idx, const APInt &Imm,
1856 assert(Ty->isIntegerTy());
1858 unsigned BitSize = Ty->getPrimitiveSizeInBits();
1859 // There is no cost model for constants with a bit size of 0. Return TCC_Free
1860 // here, so that constant hoisting will ignore this constant.
1862 return TTI::TCC_Free;
1866 return TTI::TCC_Free;
1867 case Intrinsic::sadd_with_overflow:
1868 case Intrinsic::uadd_with_overflow:
1869 case Intrinsic::ssub_with_overflow:
1870 case Intrinsic::usub_with_overflow:
1871 case Intrinsic::smul_with_overflow:
1872 case Intrinsic::umul_with_overflow:
1873 if ((Idx == 1) && Imm.getBitWidth() <= 64 && isInt<32>(Imm.getSExtValue()))
1874 return TTI::TCC_Free;
1876 case Intrinsic::experimental_stackmap:
1877 if ((Idx < 2) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
1878 return TTI::TCC_Free;
1880 case Intrinsic::experimental_patchpoint_void:
1881 case Intrinsic::experimental_patchpoint_i64:
1882 if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
1883 return TTI::TCC_Free;
1886 return X86TTIImpl::getIntImmCost(Imm, Ty);
1889 // Return an average cost of Gather / Scatter instruction, maybe improved later
1890 int X86TTIImpl::getGSVectorCost(unsigned Opcode, Type *SrcVTy, Value *Ptr,
1891 unsigned Alignment, unsigned AddressSpace) {
1893 assert(isa<VectorType>(SrcVTy) && "Unexpected type in getGSVectorCost");
1894 unsigned VF = SrcVTy->getVectorNumElements();
1896 // Try to reduce index size from 64 bit (default for GEP)
1897 // to 32. It is essential for VF 16. If the index can't be reduced to 32, the
1898 // operation will use 16 x 64 indices which do not fit in a zmm and needs
1899 // to split. Also check that the base pointer is the same for all lanes,
1900 // and that there's at most one variable index.
1901 auto getIndexSizeInBits = [](Value *Ptr, const DataLayout& DL) {
1902 unsigned IndexSize = DL.getPointerSizeInBits();
1903 GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr);
1904 if (IndexSize < 64 || !GEP)
1907 unsigned NumOfVarIndices = 0;
1908 Value *Ptrs = GEP->getPointerOperand();
1909 if (Ptrs->getType()->isVectorTy() && !getSplatValue(Ptrs))
1911 for (unsigned i = 1; i < GEP->getNumOperands(); ++i) {
1912 if (isa<Constant>(GEP->getOperand(i)))
1914 Type *IndxTy = GEP->getOperand(i)->getType();
1915 if (IndxTy->isVectorTy())
1916 IndxTy = IndxTy->getVectorElementType();
1917 if ((IndxTy->getPrimitiveSizeInBits() == 64 &&
1918 !isa<SExtInst>(GEP->getOperand(i))) ||
1919 ++NumOfVarIndices > 1)
1920 return IndexSize; // 64
1922 return (unsigned)32;
1926 // Trying to reduce IndexSize to 32 bits for vector 16.
1927 // By default the IndexSize is equal to pointer size.
1928 unsigned IndexSize = (VF >= 16) ? getIndexSizeInBits(Ptr, DL) :
1929 DL.getPointerSizeInBits();
1931 Type *IndexVTy = VectorType::get(IntegerType::get(SrcVTy->getContext(),
1933 std::pair<int, MVT> IdxsLT = TLI->getTypeLegalizationCost(DL, IndexVTy);
1934 std::pair<int, MVT> SrcLT = TLI->getTypeLegalizationCost(DL, SrcVTy);
1935 int SplitFactor = std::max(IdxsLT.first, SrcLT.first);
1936 if (SplitFactor > 1) {
1937 // Handle splitting of vector of pointers
1938 Type *SplitSrcTy = VectorType::get(SrcVTy->getScalarType(), VF / SplitFactor);
1939 return SplitFactor * getGSVectorCost(Opcode, SplitSrcTy, Ptr, Alignment,
1943 // The gather / scatter cost is given by Intel architects. It is a rough
1944 // number since we are looking at one instruction in a time.
1945 const int GSOverhead = 2;
1946 return GSOverhead + VF * getMemoryOpCost(Opcode, SrcVTy->getScalarType(),
1947 Alignment, AddressSpace);
1950 /// Return the cost of full scalarization of gather / scatter operation.
1952 /// Opcode - Load or Store instruction.
1953 /// SrcVTy - The type of the data vector that should be gathered or scattered.
1954 /// VariableMask - The mask is non-constant at compile time.
1955 /// Alignment - Alignment for one element.
1956 /// AddressSpace - pointer[s] address space.
1958 int X86TTIImpl::getGSScalarCost(unsigned Opcode, Type *SrcVTy,
1959 bool VariableMask, unsigned Alignment,
1960 unsigned AddressSpace) {
1961 unsigned VF = SrcVTy->getVectorNumElements();
1963 int MaskUnpackCost = 0;
1965 VectorType *MaskTy =
1966 VectorType::get(Type::getInt1Ty(SrcVTy->getContext()), VF);
1967 MaskUnpackCost = getScalarizationOverhead(MaskTy, false, true);
1968 int ScalarCompareCost =
1969 getCmpSelInstrCost(Instruction::ICmp, Type::getInt1Ty(SrcVTy->getContext()),
1971 int BranchCost = getCFInstrCost(Instruction::Br);
1972 MaskUnpackCost += VF * (BranchCost + ScalarCompareCost);
1975 // The cost of the scalar loads/stores.
1976 int MemoryOpCost = VF * getMemoryOpCost(Opcode, SrcVTy->getScalarType(),
1977 Alignment, AddressSpace);
1979 int InsertExtractCost = 0;
1980 if (Opcode == Instruction::Load)
1981 for (unsigned i = 0; i < VF; ++i)
1982 // Add the cost of inserting each scalar load into the vector
1983 InsertExtractCost +=
1984 getVectorInstrCost(Instruction::InsertElement, SrcVTy, i);
1986 for (unsigned i = 0; i < VF; ++i)
1987 // Add the cost of extracting each element out of the data vector
1988 InsertExtractCost +=
1989 getVectorInstrCost(Instruction::ExtractElement, SrcVTy, i);
1991 return MemoryOpCost + MaskUnpackCost + InsertExtractCost;
1994 /// Calculate the cost of Gather / Scatter operation
1995 int X86TTIImpl::getGatherScatterOpCost(unsigned Opcode, Type *SrcVTy,
1996 Value *Ptr, bool VariableMask,
1997 unsigned Alignment) {
1998 assert(SrcVTy->isVectorTy() && "Unexpected data type for Gather/Scatter");
1999 unsigned VF = SrcVTy->getVectorNumElements();
2000 PointerType *PtrTy = dyn_cast<PointerType>(Ptr->getType());
2001 if (!PtrTy && Ptr->getType()->isVectorTy())
2002 PtrTy = dyn_cast<PointerType>(Ptr->getType()->getVectorElementType());
2003 assert(PtrTy && "Unexpected type for Ptr argument");
2004 unsigned AddressSpace = PtrTy->getAddressSpace();
2006 bool Scalarize = false;
2007 if ((Opcode == Instruction::Load && !isLegalMaskedGather(SrcVTy)) ||
2008 (Opcode == Instruction::Store && !isLegalMaskedScatter(SrcVTy)))
2010 // Gather / Scatter for vector 2 is not profitable on KNL / SKX
2011 // Vector-4 of gather/scatter instruction does not exist on KNL.
2012 // We can extend it to 8 elements, but zeroing upper bits of
2013 // the mask vector will add more instructions. Right now we give the scalar
2014 // cost of vector-4 for KNL. TODO: Check, maybe the gather/scatter instruction
2015 // is better in the VariableMask case.
2016 if (VF == 2 || (VF == 4 && !ST->hasVLX()))
2020 return getGSScalarCost(Opcode, SrcVTy, VariableMask, Alignment,
2023 return getGSVectorCost(Opcode, SrcVTy, Ptr, Alignment, AddressSpace);
2026 bool X86TTIImpl::isLegalMaskedLoad(Type *DataTy) {
2027 Type *ScalarTy = DataTy->getScalarType();
2028 int DataWidth = isa<PointerType>(ScalarTy) ?
2029 DL.getPointerSizeInBits() : ScalarTy->getPrimitiveSizeInBits();
2031 return ((DataWidth == 32 || DataWidth == 64) && ST->hasAVX()) ||
2032 ((DataWidth == 8 || DataWidth == 16) && ST->hasBWI());
2035 bool X86TTIImpl::isLegalMaskedStore(Type *DataType) {
2036 return isLegalMaskedLoad(DataType);
2039 bool X86TTIImpl::isLegalMaskedGather(Type *DataTy) {
2040 // This function is called now in two cases: from the Loop Vectorizer
2041 // and from the Scalarizer.
2042 // When the Loop Vectorizer asks about legality of the feature,
2043 // the vectorization factor is not calculated yet. The Loop Vectorizer
2044 // sends a scalar type and the decision is based on the width of the
2046 // Later on, the cost model will estimate usage this intrinsic based on
2048 // The Scalarizer asks again about legality. It sends a vector type.
2049 // In this case we can reject non-power-of-2 vectors.
2050 if (isa<VectorType>(DataTy) && !isPowerOf2_32(DataTy->getVectorNumElements()))
2052 Type *ScalarTy = DataTy->getScalarType();
2053 int DataWidth = isa<PointerType>(ScalarTy) ?
2054 DL.getPointerSizeInBits() : ScalarTy->getPrimitiveSizeInBits();
2056 // AVX-512 allows gather and scatter
2057 return (DataWidth == 32 || DataWidth == 64) && ST->hasAVX512();
2060 bool X86TTIImpl::isLegalMaskedScatter(Type *DataType) {
2061 return isLegalMaskedGather(DataType);
2064 bool X86TTIImpl::areInlineCompatible(const Function *Caller,
2065 const Function *Callee) const {
2066 const TargetMachine &TM = getTLI()->getTargetMachine();
2068 // Work this as a subsetting of subtarget features.
2069 const FeatureBitset &CallerBits =
2070 TM.getSubtargetImpl(*Caller)->getFeatureBits();
2071 const FeatureBitset &CalleeBits =
2072 TM.getSubtargetImpl(*Callee)->getFeatureBits();
2074 // FIXME: This is likely too limiting as it will include subtarget features
2075 // that we might not care about for inlining, but it is conservatively
2077 return (CallerBits & CalleeBits) == CalleeBits;
2080 bool X86TTIImpl::enableInterleavedAccessVectorization() {
2081 // TODO: We expect this to be beneficial regardless of arch,
2082 // but there are currently some unexplained performance artifacts on Atom.
2083 // As a temporary solution, disable on Atom.
2084 return !(ST->isAtom() || ST->isSLM());
2087 // Get estimation for interleaved load/store operations and strided load.
2088 // \p Indices contains indices for strided load.
2089 // \p Factor - the factor of interleaving.
2090 // AVX-512 provides 3-src shuffles that significantly reduces the cost.
2091 int X86TTIImpl::getInterleavedMemoryOpCostAVX512(unsigned Opcode, Type *VecTy,
2093 ArrayRef<unsigned> Indices,
2095 unsigned AddressSpace) {
2097 // VecTy for interleave memop is <VF*Factor x Elt>.
2098 // So, for VF=4, Interleave Factor = 3, Element type = i32 we have
2099 // VecTy = <12 x i32>.
2101 // Calculate the number of memory operations (NumOfMemOps), required
2102 // for load/store the VecTy.
2103 MVT LegalVT = getTLI()->getTypeLegalizationCost(DL, VecTy).second;
2104 unsigned VecTySize = DL.getTypeStoreSize(VecTy);
2105 unsigned LegalVTSize = LegalVT.getStoreSize();
2106 unsigned NumOfMemOps = (VecTySize + LegalVTSize - 1) / LegalVTSize;
2108 // Get the cost of one memory operation.
2109 Type *SingleMemOpTy = VectorType::get(VecTy->getVectorElementType(),
2110 LegalVT.getVectorNumElements());
2111 unsigned MemOpCost =
2112 getMemoryOpCost(Opcode, SingleMemOpTy, Alignment, AddressSpace);
2114 if (Opcode == Instruction::Load) {
2115 // Kind of shuffle depends on number of loaded values.
2116 // If we load the entire data in one register, we can use a 1-src shuffle.
2117 // Otherwise, we'll merge 2 sources in each operation.
2118 TTI::ShuffleKind ShuffleKind =
2119 (NumOfMemOps > 1) ? TTI::SK_PermuteTwoSrc : TTI::SK_PermuteSingleSrc;
2121 unsigned ShuffleCost =
2122 getShuffleCost(ShuffleKind, SingleMemOpTy, 0, nullptr);
2124 unsigned NumOfLoadsInInterleaveGrp =
2125 Indices.size() ? Indices.size() : Factor;
2126 Type *ResultTy = VectorType::get(VecTy->getVectorElementType(),
2127 VecTy->getVectorNumElements() / Factor);
2128 unsigned NumOfResults =
2129 getTLI()->getTypeLegalizationCost(DL, ResultTy).first *
2130 NumOfLoadsInInterleaveGrp;
2132 // About a half of the loads may be folded in shuffles when we have only
2133 // one result. If we have more than one result, we do not fold loads at all.
2134 unsigned NumOfUnfoldedLoads =
2135 NumOfResults > 1 ? NumOfMemOps : NumOfMemOps / 2;
2137 // Get a number of shuffle operations per result.
2138 unsigned NumOfShufflesPerResult =
2139 std::max((unsigned)1, (unsigned)(NumOfMemOps - 1));
2141 // The SK_MergeTwoSrc shuffle clobbers one of src operands.
2142 // When we have more than one destination, we need additional instructions
2144 unsigned NumOfMoves = 0;
2145 if (NumOfResults > 1 && ShuffleKind == TTI::SK_PermuteTwoSrc)
2146 NumOfMoves = NumOfResults * NumOfShufflesPerResult / 2;
2148 int Cost = NumOfResults * NumOfShufflesPerResult * ShuffleCost +
2149 NumOfUnfoldedLoads * MemOpCost + NumOfMoves;
2155 assert(Opcode == Instruction::Store &&
2156 "Expected Store Instruction at this point");
2158 // There is no strided stores meanwhile. And store can't be folded in
2160 unsigned NumOfSources = Factor; // The number of values to be merged.
2161 unsigned ShuffleCost =
2162 getShuffleCost(TTI::SK_PermuteTwoSrc, SingleMemOpTy, 0, nullptr);
2163 unsigned NumOfShufflesPerStore = NumOfSources - 1;
2165 // The SK_MergeTwoSrc shuffle clobbers one of src operands.
2166 // We need additional instructions to keep sources.
2167 unsigned NumOfMoves = NumOfMemOps * NumOfShufflesPerStore / 2;
2168 int Cost = NumOfMemOps * (MemOpCost + NumOfShufflesPerStore * ShuffleCost) +
2173 int X86TTIImpl::getInterleavedMemoryOpCost(unsigned Opcode, Type *VecTy,
2175 ArrayRef<unsigned> Indices,
2177 unsigned AddressSpace) {
2178 auto isSupportedOnAVX512 = [](Type *VecTy, bool &RequiresBW) {
2180 Type *EltTy = VecTy->getVectorElementType();
2181 if (EltTy->isFloatTy() || EltTy->isDoubleTy() || EltTy->isIntegerTy(64) ||
2182 EltTy->isIntegerTy(32) || EltTy->isPointerTy())
2184 if (EltTy->isIntegerTy(16) || EltTy->isIntegerTy(8)) {
2191 bool HasAVX512Solution = isSupportedOnAVX512(VecTy, RequiresBW);
2192 if (ST->hasAVX512() && HasAVX512Solution && (!RequiresBW || ST->hasBWI()))
2193 return getInterleavedMemoryOpCostAVX512(Opcode, VecTy, Factor, Indices,
2194 Alignment, AddressSpace);
2195 return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
2196 Alignment, AddressSpace);