Bug Summary

File:llvm/include/llvm/CodeGen/BasicTTIImpl.h
Warning:line 428, column 36
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name X86TargetTransformInfo.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=none -fmath-errno -fno-rounding-math -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -fno-split-dwarf-inlining -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-10/lib/clang/10.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/build-llvm/lib/Target/X86 -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Target/X86 -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/build-llvm/include -I /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-10/lib/clang/10.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/build-llvm/lib/Target/X86 -fdebug-prefix-map=/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd=. -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fobjc-runtime=gcc -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2020-01-13-084841-49055-1 -x c++ /build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Target/X86/X86TargetTransformInfo.cpp

/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Target/X86/X86TargetTransformInfo.cpp

1//===-- X86TargetTransformInfo.cpp - X86 specific TTI pass ----------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8/// \file
9/// This file implements a TargetTransformInfo analysis pass specific to the
10/// X86 target machine. It uses the target's detailed information to provide
11/// more precise answers to certain TTI queries, while letting the target
12/// independent and default TTI implementations handle the rest.
13///
14//===----------------------------------------------------------------------===//
15/// About Cost Model numbers used below it's necessary to say the following:
16/// the numbers correspond to some "generic" X86 CPU instead of usage of
17/// concrete CPU model. Usually the numbers correspond to CPU where the feature
18/// apeared at the first time. For example, if we do Subtarget.hasSSE42() in
19/// the lookups below the cost is based on Nehalem as that was the first CPU
20/// to support that feature level and thus has most likely the worst case cost.
21/// Some examples of other technologies/CPUs:
22/// SSE 3 - Pentium4 / Athlon64
23/// SSE 4.1 - Penryn
24/// SSE 4.2 - Nehalem
25/// AVX - Sandy Bridge
26/// AVX2 - Haswell
27/// AVX-512 - Xeon Phi / Skylake
28/// And some examples of instruction target dependent costs (latency)
29/// divss sqrtss rsqrtss
30/// AMD K7 11-16 19 3
31/// Piledriver 9-24 13-15 5
32/// Jaguar 14 16 2
33/// Pentium II,III 18 30 2
34/// Nehalem 7-14 7-18 3
35/// Haswell 10-13 11 5
36/// TODO: Develop and implement the target dependent cost model and
37/// specialize cost numbers for different Cost Model Targets such as throughput,
38/// code size, latency and uop count.
39//===----------------------------------------------------------------------===//
40
41#include "X86TargetTransformInfo.h"
42#include "llvm/Analysis/TargetTransformInfo.h"
43#include "llvm/CodeGen/BasicTTIImpl.h"
44#include "llvm/CodeGen/CostTable.h"
45#include "llvm/CodeGen/TargetLowering.h"
46#include "llvm/IR/IntrinsicInst.h"
47#include "llvm/Support/Debug.h"
48
49using namespace llvm;
50
51#define DEBUG_TYPE"x86tti" "x86tti"
52
53//===----------------------------------------------------------------------===//
54//
55// X86 cost model.
56//
57//===----------------------------------------------------------------------===//
58
59TargetTransformInfo::PopcntSupportKind
60X86TTIImpl::getPopcntSupport(unsigned TyWidth) {
61 assert(isPowerOf2_32(TyWidth) && "Ty width must be power of 2")((isPowerOf2_32(TyWidth) && "Ty width must be power of 2"
) ? static_cast<void> (0) : __assert_fail ("isPowerOf2_32(TyWidth) && \"Ty width must be power of 2\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Target/X86/X86TargetTransformInfo.cpp"
, 61, __PRETTY_FUNCTION__))
;
62 // TODO: Currently the __builtin_popcount() implementation using SSE3
63 // instructions is inefficient. Once the problem is fixed, we should
64 // call ST->hasSSE3() instead of ST->hasPOPCNT().
65 return ST->hasPOPCNT() ? TTI::PSK_FastHardware : TTI::PSK_Software;
66}
67
68llvm::Optional<unsigned> X86TTIImpl::getCacheSize(
69 TargetTransformInfo::CacheLevel Level) const {
70 switch (Level) {
71 case TargetTransformInfo::CacheLevel::L1D:
72 // - Penryn
73 // - Nehalem
74 // - Westmere
75 // - Sandy Bridge
76 // - Ivy Bridge
77 // - Haswell
78 // - Broadwell
79 // - Skylake
80 // - Kabylake
81 return 32 * 1024; // 32 KByte
82 case TargetTransformInfo::CacheLevel::L2D:
83 // - Penryn
84 // - Nehalem
85 // - Westmere
86 // - Sandy Bridge
87 // - Ivy Bridge
88 // - Haswell
89 // - Broadwell
90 // - Skylake
91 // - Kabylake
92 return 256 * 1024; // 256 KByte
93 }
94
95 llvm_unreachable("Unknown TargetTransformInfo::CacheLevel")::llvm::llvm_unreachable_internal("Unknown TargetTransformInfo::CacheLevel"
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Target/X86/X86TargetTransformInfo.cpp"
, 95)
;
96}
97
98llvm::Optional<unsigned> X86TTIImpl::getCacheAssociativity(
99 TargetTransformInfo::CacheLevel Level) const {
100 // - Penryn
101 // - Nehalem
102 // - Westmere
103 // - Sandy Bridge
104 // - Ivy Bridge
105 // - Haswell
106 // - Broadwell
107 // - Skylake
108 // - Kabylake
109 switch (Level) {
110 case TargetTransformInfo::CacheLevel::L1D:
111 LLVM_FALLTHROUGH[[gnu::fallthrough]];
112 case TargetTransformInfo::CacheLevel::L2D:
113 return 8;
114 }
115
116 llvm_unreachable("Unknown TargetTransformInfo::CacheLevel")::llvm::llvm_unreachable_internal("Unknown TargetTransformInfo::CacheLevel"
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Target/X86/X86TargetTransformInfo.cpp"
, 116)
;
117}
118
119unsigned X86TTIImpl::getNumberOfRegisters(unsigned ClassID) const {
120 bool Vector = (ClassID == 1);
121 if (Vector && !ST->hasSSE1())
122 return 0;
123
124 if (ST->is64Bit()) {
125 if (Vector && ST->hasAVX512())
126 return 32;
127 return 16;
128 }
129 return 8;
130}
131
132unsigned X86TTIImpl::getRegisterBitWidth(bool Vector) const {
133 unsigned PreferVectorWidth = ST->getPreferVectorWidth();
134 if (Vector) {
135 if (ST->hasAVX512() && PreferVectorWidth >= 512)
136 return 512;
137 if (ST->hasAVX() && PreferVectorWidth >= 256)
138 return 256;
139 if (ST->hasSSE1() && PreferVectorWidth >= 128)
140 return 128;
141 return 0;
142 }
143
144 if (ST->is64Bit())
145 return 64;
146
147 return 32;
148}
149
150unsigned X86TTIImpl::getLoadStoreVecRegBitWidth(unsigned) const {
151 return getRegisterBitWidth(true);
152}
153
154unsigned X86TTIImpl::getMaxInterleaveFactor(unsigned VF) {
155 // If the loop will not be vectorized, don't interleave the loop.
156 // Let regular unroll to unroll the loop, which saves the overflow
157 // check and memory check cost.
158 if (VF == 1)
159 return 1;
160
161 if (ST->isAtom())
162 return 1;
163
164 // Sandybridge and Haswell have multiple execution ports and pipelined
165 // vector units.
166 if (ST->hasAVX())
167 return 4;
168
169 return 2;
170}
171
172int X86TTIImpl::getArithmeticInstrCost(unsigned Opcode, Type *Ty,
173 TTI::OperandValueKind Op1Info,
174 TTI::OperandValueKind Op2Info,
175 TTI::OperandValueProperties Opd1PropInfo,
176 TTI::OperandValueProperties Opd2PropInfo,
177 ArrayRef<const Value *> Args,
178 const Instruction *CxtI) {
179 // Legalize the type.
180 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Ty);
181
182 int ISD = TLI->InstructionOpcodeToISD(Opcode);
183 assert(ISD && "Invalid opcode")((ISD && "Invalid opcode") ? static_cast<void> (
0) : __assert_fail ("ISD && \"Invalid opcode\"", "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Target/X86/X86TargetTransformInfo.cpp"
, 183, __PRETTY_FUNCTION__))
;
184
185 static const CostTblEntry GLMCostTable[] = {
186 { ISD::FDIV, MVT::f32, 18 }, // divss
187 { ISD::FDIV, MVT::v4f32, 35 }, // divps
188 { ISD::FDIV, MVT::f64, 33 }, // divsd
189 { ISD::FDIV, MVT::v2f64, 65 }, // divpd
190 };
191
192 if (ST->useGLMDivSqrtCosts())
193 if (const auto *Entry = CostTableLookup(GLMCostTable, ISD,
194 LT.second))
195 return LT.first * Entry->Cost;
196
197 static const CostTblEntry SLMCostTable[] = {
198 { ISD::MUL, MVT::v4i32, 11 }, // pmulld
199 { ISD::MUL, MVT::v8i16, 2 }, // pmullw
200 { ISD::MUL, MVT::v16i8, 14 }, // extend/pmullw/trunc sequence.
201 { ISD::FMUL, MVT::f64, 2 }, // mulsd
202 { ISD::FMUL, MVT::v2f64, 4 }, // mulpd
203 { ISD::FMUL, MVT::v4f32, 2 }, // mulps
204 { ISD::FDIV, MVT::f32, 17 }, // divss
205 { ISD::FDIV, MVT::v4f32, 39 }, // divps
206 { ISD::FDIV, MVT::f64, 32 }, // divsd
207 { ISD::FDIV, MVT::v2f64, 69 }, // divpd
208 { ISD::FADD, MVT::v2f64, 2 }, // addpd
209 { ISD::FSUB, MVT::v2f64, 2 }, // subpd
210 // v2i64/v4i64 mul is custom lowered as a series of long:
211 // multiplies(3), shifts(3) and adds(2)
212 // slm muldq version throughput is 2 and addq throughput 4
213 // thus: 3X2 (muldq throughput) + 3X1 (shift throughput) +
214 // 3X4 (addq throughput) = 17
215 { ISD::MUL, MVT::v2i64, 17 },
216 // slm addq\subq throughput is 4
217 { ISD::ADD, MVT::v2i64, 4 },
218 { ISD::SUB, MVT::v2i64, 4 },
219 };
220
221 if (ST->isSLM()) {
222 if (Args.size() == 2 && ISD == ISD::MUL && LT.second == MVT::v4i32) {
223 // Check if the operands can be shrinked into a smaller datatype.
224 bool Op1Signed = false;
225 unsigned Op1MinSize = BaseT::minRequiredElementSize(Args[0], Op1Signed);
226 bool Op2Signed = false;
227 unsigned Op2MinSize = BaseT::minRequiredElementSize(Args[1], Op2Signed);
228
229 bool signedMode = Op1Signed | Op2Signed;
230 unsigned OpMinSize = std::max(Op1MinSize, Op2MinSize);
231
232 if (OpMinSize <= 7)
233 return LT.first * 3; // pmullw/sext
234 if (!signedMode && OpMinSize <= 8)
235 return LT.first * 3; // pmullw/zext
236 if (OpMinSize <= 15)
237 return LT.first * 5; // pmullw/pmulhw/pshuf
238 if (!signedMode && OpMinSize <= 16)
239 return LT.first * 5; // pmullw/pmulhw/pshuf
240 }
241
242 if (const auto *Entry = CostTableLookup(SLMCostTable, ISD,
243 LT.second)) {
244 return LT.first * Entry->Cost;
245 }
246 }
247
248 if ((ISD == ISD::SDIV || ISD == ISD::SREM || ISD == ISD::UDIV ||
249 ISD == ISD::UREM) &&
250 (Op2Info == TargetTransformInfo::OK_UniformConstantValue ||
251 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue) &&
252 Opd2PropInfo == TargetTransformInfo::OP_PowerOf2) {
253 if (ISD == ISD::SDIV || ISD == ISD::SREM) {
254 // On X86, vector signed division by constants power-of-two are
255 // normally expanded to the sequence SRA + SRL + ADD + SRA.
256 // The OperandValue properties may not be the same as that of the previous
257 // operation; conservatively assume OP_None.
258 int Cost =
259 2 * getArithmeticInstrCost(Instruction::AShr, Ty, Op1Info, Op2Info,
260 TargetTransformInfo::OP_None,
261 TargetTransformInfo::OP_None);
262 Cost += getArithmeticInstrCost(Instruction::LShr, Ty, Op1Info, Op2Info,
263 TargetTransformInfo::OP_None,
264 TargetTransformInfo::OP_None);
265 Cost += getArithmeticInstrCost(Instruction::Add, Ty, Op1Info, Op2Info,
266 TargetTransformInfo::OP_None,
267 TargetTransformInfo::OP_None);
268
269 if (ISD == ISD::SREM) {
270 // For SREM: (X % C) is the equivalent of (X - (X/C)*C)
271 Cost += getArithmeticInstrCost(Instruction::Mul, Ty, Op1Info, Op2Info);
272 Cost += getArithmeticInstrCost(Instruction::Sub, Ty, Op1Info, Op2Info);
273 }
274
275 return Cost;
276 }
277
278 // Vector unsigned division/remainder will be simplified to shifts/masks.
279 if (ISD == ISD::UDIV)
280 return getArithmeticInstrCost(Instruction::LShr, Ty, Op1Info, Op2Info,
281 TargetTransformInfo::OP_None,
282 TargetTransformInfo::OP_None);
283
284 else // UREM
285 return getArithmeticInstrCost(Instruction::And, Ty, Op1Info, Op2Info,
286 TargetTransformInfo::OP_None,
287 TargetTransformInfo::OP_None);
288 }
289
290 static const CostTblEntry AVX512BWUniformConstCostTable[] = {
291 { ISD::SHL, MVT::v64i8, 2 }, // psllw + pand.
292 { ISD::SRL, MVT::v64i8, 2 }, // psrlw + pand.
293 { ISD::SRA, MVT::v64i8, 4 }, // psrlw, pand, pxor, psubb.
294 };
295
296 if (Op2Info == TargetTransformInfo::OK_UniformConstantValue &&
297 ST->hasBWI()) {
298 if (const auto *Entry = CostTableLookup(AVX512BWUniformConstCostTable, ISD,
299 LT.second))
300 return LT.first * Entry->Cost;
301 }
302
303 static const CostTblEntry AVX512UniformConstCostTable[] = {
304 { ISD::SRA, MVT::v2i64, 1 },
305 { ISD::SRA, MVT::v4i64, 1 },
306 { ISD::SRA, MVT::v8i64, 1 },
307 };
308
309 if (Op2Info == TargetTransformInfo::OK_UniformConstantValue &&
310 ST->hasAVX512()) {
311 if (const auto *Entry = CostTableLookup(AVX512UniformConstCostTable, ISD,
312 LT.second))
313 return LT.first * Entry->Cost;
314 }
315
316 static const CostTblEntry AVX2UniformConstCostTable[] = {
317 { ISD::SHL, MVT::v32i8, 2 }, // psllw + pand.
318 { ISD::SRL, MVT::v32i8, 2 }, // psrlw + pand.
319 { ISD::SRA, MVT::v32i8, 4 }, // psrlw, pand, pxor, psubb.
320
321 { ISD::SRA, MVT::v4i64, 4 }, // 2 x psrad + shuffle.
322 };
323
324 if (Op2Info == TargetTransformInfo::OK_UniformConstantValue &&
325 ST->hasAVX2()) {
326 if (const auto *Entry = CostTableLookup(AVX2UniformConstCostTable, ISD,
327 LT.second))
328 return LT.first * Entry->Cost;
329 }
330
331 static const CostTblEntry SSE2UniformConstCostTable[] = {
332 { ISD::SHL, MVT::v16i8, 2 }, // psllw + pand.
333 { ISD::SRL, MVT::v16i8, 2 }, // psrlw + pand.
334 { ISD::SRA, MVT::v16i8, 4 }, // psrlw, pand, pxor, psubb.
335
336 { ISD::SHL, MVT::v32i8, 4+2 }, // 2*(psllw + pand) + split.
337 { ISD::SRL, MVT::v32i8, 4+2 }, // 2*(psrlw + pand) + split.
338 { ISD::SRA, MVT::v32i8, 8+2 }, // 2*(psrlw, pand, pxor, psubb) + split.
339 };
340
341 // XOP has faster vXi8 shifts.
342 if (Op2Info == TargetTransformInfo::OK_UniformConstantValue &&
343 ST->hasSSE2() && !ST->hasXOP()) {
344 if (const auto *Entry =
345 CostTableLookup(SSE2UniformConstCostTable, ISD, LT.second))
346 return LT.first * Entry->Cost;
347 }
348
349 static const CostTblEntry AVX512BWConstCostTable[] = {
350 { ISD::SDIV, MVT::v64i8, 14 }, // 2*ext+2*pmulhw sequence
351 { ISD::SREM, MVT::v64i8, 16 }, // 2*ext+2*pmulhw+mul+sub sequence
352 { ISD::UDIV, MVT::v64i8, 14 }, // 2*ext+2*pmulhw sequence
353 { ISD::UREM, MVT::v64i8, 16 }, // 2*ext+2*pmulhw+mul+sub sequence
354 { ISD::SDIV, MVT::v32i16, 6 }, // vpmulhw sequence
355 { ISD::SREM, MVT::v32i16, 8 }, // vpmulhw+mul+sub sequence
356 { ISD::UDIV, MVT::v32i16, 6 }, // vpmulhuw sequence
357 { ISD::UREM, MVT::v32i16, 8 }, // vpmulhuw+mul+sub sequence
358 };
359
360 if ((Op2Info == TargetTransformInfo::OK_UniformConstantValue ||
361 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue) &&
362 ST->hasBWI()) {
363 if (const auto *Entry =
364 CostTableLookup(AVX512BWConstCostTable, ISD, LT.second))
365 return LT.first * Entry->Cost;
366 }
367
368 static const CostTblEntry AVX512ConstCostTable[] = {
369 { ISD::SDIV, MVT::v16i32, 15 }, // vpmuldq sequence
370 { ISD::SREM, MVT::v16i32, 17 }, // vpmuldq+mul+sub sequence
371 { ISD::UDIV, MVT::v16i32, 15 }, // vpmuludq sequence
372 { ISD::UREM, MVT::v16i32, 17 }, // vpmuludq+mul+sub sequence
373 };
374
375 if ((Op2Info == TargetTransformInfo::OK_UniformConstantValue ||
376 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue) &&
377 ST->hasAVX512()) {
378 if (const auto *Entry =
379 CostTableLookup(AVX512ConstCostTable, ISD, LT.second))
380 return LT.first * Entry->Cost;
381 }
382
383 static const CostTblEntry AVX2ConstCostTable[] = {
384 { ISD::SDIV, MVT::v32i8, 14 }, // 2*ext+2*pmulhw sequence
385 { ISD::SREM, MVT::v32i8, 16 }, // 2*ext+2*pmulhw+mul+sub sequence
386 { ISD::UDIV, MVT::v32i8, 14 }, // 2*ext+2*pmulhw sequence
387 { ISD::UREM, MVT::v32i8, 16 }, // 2*ext+2*pmulhw+mul+sub sequence
388 { ISD::SDIV, MVT::v16i16, 6 }, // vpmulhw sequence
389 { ISD::SREM, MVT::v16i16, 8 }, // vpmulhw+mul+sub sequence
390 { ISD::UDIV, MVT::v16i16, 6 }, // vpmulhuw sequence
391 { ISD::UREM, MVT::v16i16, 8 }, // vpmulhuw+mul+sub sequence
392 { ISD::SDIV, MVT::v8i32, 15 }, // vpmuldq sequence
393 { ISD::SREM, MVT::v8i32, 19 }, // vpmuldq+mul+sub sequence
394 { ISD::UDIV, MVT::v8i32, 15 }, // vpmuludq sequence
395 { ISD::UREM, MVT::v8i32, 19 }, // vpmuludq+mul+sub sequence
396 };
397
398 if ((Op2Info == TargetTransformInfo::OK_UniformConstantValue ||
399 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue) &&
400 ST->hasAVX2()) {
401 if (const auto *Entry = CostTableLookup(AVX2ConstCostTable, ISD, LT.second))
402 return LT.first * Entry->Cost;
403 }
404
405 static const CostTblEntry SSE2ConstCostTable[] = {
406 { ISD::SDIV, MVT::v32i8, 28+2 }, // 4*ext+4*pmulhw sequence + split.
407 { ISD::SREM, MVT::v32i8, 32+2 }, // 4*ext+4*pmulhw+mul+sub sequence + split.
408 { ISD::SDIV, MVT::v16i8, 14 }, // 2*ext+2*pmulhw sequence
409 { ISD::SREM, MVT::v16i8, 16 }, // 2*ext+2*pmulhw+mul+sub sequence
410 { ISD::UDIV, MVT::v32i8, 28+2 }, // 4*ext+4*pmulhw sequence + split.
411 { ISD::UREM, MVT::v32i8, 32+2 }, // 4*ext+4*pmulhw+mul+sub sequence + split.
412 { ISD::UDIV, MVT::v16i8, 14 }, // 2*ext+2*pmulhw sequence
413 { ISD::UREM, MVT::v16i8, 16 }, // 2*ext+2*pmulhw+mul+sub sequence
414 { ISD::SDIV, MVT::v16i16, 12+2 }, // 2*pmulhw sequence + split.
415 { ISD::SREM, MVT::v16i16, 16+2 }, // 2*pmulhw+mul+sub sequence + split.
416 { ISD::SDIV, MVT::v8i16, 6 }, // pmulhw sequence
417 { ISD::SREM, MVT::v8i16, 8 }, // pmulhw+mul+sub sequence
418 { ISD::UDIV, MVT::v16i16, 12+2 }, // 2*pmulhuw sequence + split.
419 { ISD::UREM, MVT::v16i16, 16+2 }, // 2*pmulhuw+mul+sub sequence + split.
420 { ISD::UDIV, MVT::v8i16, 6 }, // pmulhuw sequence
421 { ISD::UREM, MVT::v8i16, 8 }, // pmulhuw+mul+sub sequence
422 { ISD::SDIV, MVT::v8i32, 38+2 }, // 2*pmuludq sequence + split.
423 { ISD::SREM, MVT::v8i32, 48+2 }, // 2*pmuludq+mul+sub sequence + split.
424 { ISD::SDIV, MVT::v4i32, 19 }, // pmuludq sequence
425 { ISD::SREM, MVT::v4i32, 24 }, // pmuludq+mul+sub sequence
426 { ISD::UDIV, MVT::v8i32, 30+2 }, // 2*pmuludq sequence + split.
427 { ISD::UREM, MVT::v8i32, 40+2 }, // 2*pmuludq+mul+sub sequence + split.
428 { ISD::UDIV, MVT::v4i32, 15 }, // pmuludq sequence
429 { ISD::UREM, MVT::v4i32, 20 }, // pmuludq+mul+sub sequence
430 };
431
432 if ((Op2Info == TargetTransformInfo::OK_UniformConstantValue ||
433 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue) &&
434 ST->hasSSE2()) {
435 // pmuldq sequence.
436 if (ISD == ISD::SDIV && LT.second == MVT::v8i32 && ST->hasAVX())
437 return LT.first * 32;
438 if (ISD == ISD::SREM && LT.second == MVT::v8i32 && ST->hasAVX())
439 return LT.first * 38;
440 if (ISD == ISD::SDIV && LT.second == MVT::v4i32 && ST->hasSSE41())
441 return LT.first * 15;
442 if (ISD == ISD::SREM && LT.second == MVT::v4i32 && ST->hasSSE41())
443 return LT.first * 20;
444
445 if (const auto *Entry = CostTableLookup(SSE2ConstCostTable, ISD, LT.second))
446 return LT.first * Entry->Cost;
447 }
448
449 static const CostTblEntry AVX2UniformCostTable[] = {
450 // Uniform splats are cheaper for the following instructions.
451 { ISD::SHL, MVT::v16i16, 1 }, // psllw.
452 { ISD::SRL, MVT::v16i16, 1 }, // psrlw.
453 { ISD::SRA, MVT::v16i16, 1 }, // psraw.
454 };
455
456 if (ST->hasAVX2() &&
457 ((Op2Info == TargetTransformInfo::OK_UniformConstantValue) ||
458 (Op2Info == TargetTransformInfo::OK_UniformValue))) {
459 if (const auto *Entry =
460 CostTableLookup(AVX2UniformCostTable, ISD, LT.second))
461 return LT.first * Entry->Cost;
462 }
463
464 static const CostTblEntry SSE2UniformCostTable[] = {
465 // Uniform splats are cheaper for the following instructions.
466 { ISD::SHL, MVT::v8i16, 1 }, // psllw.
467 { ISD::SHL, MVT::v4i32, 1 }, // pslld
468 { ISD::SHL, MVT::v2i64, 1 }, // psllq.
469
470 { ISD::SRL, MVT::v8i16, 1 }, // psrlw.
471 { ISD::SRL, MVT::v4i32, 1 }, // psrld.
472 { ISD::SRL, MVT::v2i64, 1 }, // psrlq.
473
474 { ISD::SRA, MVT::v8i16, 1 }, // psraw.
475 { ISD::SRA, MVT::v4i32, 1 }, // psrad.
476 };
477
478 if (ST->hasSSE2() &&
479 ((Op2Info == TargetTransformInfo::OK_UniformConstantValue) ||
480 (Op2Info == TargetTransformInfo::OK_UniformValue))) {
481 if (const auto *Entry =
482 CostTableLookup(SSE2UniformCostTable, ISD, LT.second))
483 return LT.first * Entry->Cost;
484 }
485
486 static const CostTblEntry AVX512DQCostTable[] = {
487 { ISD::MUL, MVT::v2i64, 1 },
488 { ISD::MUL, MVT::v4i64, 1 },
489 { ISD::MUL, MVT::v8i64, 1 }
490 };
491
492 // Look for AVX512DQ lowering tricks for custom cases.
493 if (ST->hasDQI())
494 if (const auto *Entry = CostTableLookup(AVX512DQCostTable, ISD, LT.second))
495 return LT.first * Entry->Cost;
496
497 static const CostTblEntry AVX512BWCostTable[] = {
498 { ISD::SHL, MVT::v8i16, 1 }, // vpsllvw
499 { ISD::SRL, MVT::v8i16, 1 }, // vpsrlvw
500 { ISD::SRA, MVT::v8i16, 1 }, // vpsravw
501
502 { ISD::SHL, MVT::v16i16, 1 }, // vpsllvw
503 { ISD::SRL, MVT::v16i16, 1 }, // vpsrlvw
504 { ISD::SRA, MVT::v16i16, 1 }, // vpsravw
505
506 { ISD::SHL, MVT::v32i16, 1 }, // vpsllvw
507 { ISD::SRL, MVT::v32i16, 1 }, // vpsrlvw
508 { ISD::SRA, MVT::v32i16, 1 }, // vpsravw
509
510 { ISD::SHL, MVT::v64i8, 11 }, // vpblendvb sequence.
511 { ISD::SRL, MVT::v64i8, 11 }, // vpblendvb sequence.
512 { ISD::SRA, MVT::v64i8, 24 }, // vpblendvb sequence.
513
514 { ISD::MUL, MVT::v64i8, 11 }, // extend/pmullw/trunc sequence.
515 { ISD::MUL, MVT::v32i8, 4 }, // extend/pmullw/trunc sequence.
516 { ISD::MUL, MVT::v16i8, 4 }, // extend/pmullw/trunc sequence.
517 };
518
519 // Look for AVX512BW lowering tricks for custom cases.
520 if (ST->hasBWI())
521 if (const auto *Entry = CostTableLookup(AVX512BWCostTable, ISD, LT.second))
522 return LT.first * Entry->Cost;
523
524 static const CostTblEntry AVX512CostTable[] = {
525 { ISD::SHL, MVT::v16i32, 1 },
526 { ISD::SRL, MVT::v16i32, 1 },
527 { ISD::SRA, MVT::v16i32, 1 },
528
529 { ISD::SHL, MVT::v8i64, 1 },
530 { ISD::SRL, MVT::v8i64, 1 },
531
532 { ISD::SRA, MVT::v2i64, 1 },
533 { ISD::SRA, MVT::v4i64, 1 },
534 { ISD::SRA, MVT::v8i64, 1 },
535
536 { ISD::MUL, MVT::v32i8, 13 }, // extend/pmullw/trunc sequence.
537 { ISD::MUL, MVT::v16i8, 5 }, // extend/pmullw/trunc sequence.
538 { ISD::MUL, MVT::v16i32, 1 }, // pmulld (Skylake from agner.org)
539 { ISD::MUL, MVT::v8i32, 1 }, // pmulld (Skylake from agner.org)
540 { ISD::MUL, MVT::v4i32, 1 }, // pmulld (Skylake from agner.org)
541 { ISD::MUL, MVT::v8i64, 8 }, // 3*pmuludq/3*shift/2*add
542
543 { ISD::FADD, MVT::v8f64, 1 }, // Skylake from http://www.agner.org/
544 { ISD::FSUB, MVT::v8f64, 1 }, // Skylake from http://www.agner.org/
545 { ISD::FMUL, MVT::v8f64, 1 }, // Skylake from http://www.agner.org/
546
547 { ISD::FADD, MVT::v16f32, 1 }, // Skylake from http://www.agner.org/
548 { ISD::FSUB, MVT::v16f32, 1 }, // Skylake from http://www.agner.org/
549 { ISD::FMUL, MVT::v16f32, 1 }, // Skylake from http://www.agner.org/
550 };
551
552 if (ST->hasAVX512())
553 if (const auto *Entry = CostTableLookup(AVX512CostTable, ISD, LT.second))
554 return LT.first * Entry->Cost;
555
556 static const CostTblEntry AVX2ShiftCostTable[] = {
557 // Shifts on v4i64/v8i32 on AVX2 is legal even though we declare to
558 // customize them to detect the cases where shift amount is a scalar one.
559 { ISD::SHL, MVT::v4i32, 1 },
560 { ISD::SRL, MVT::v4i32, 1 },
561 { ISD::SRA, MVT::v4i32, 1 },
562 { ISD::SHL, MVT::v8i32, 1 },
563 { ISD::SRL, MVT::v8i32, 1 },
564 { ISD::SRA, MVT::v8i32, 1 },
565 { ISD::SHL, MVT::v2i64, 1 },
566 { ISD::SRL, MVT::v2i64, 1 },
567 { ISD::SHL, MVT::v4i64, 1 },
568 { ISD::SRL, MVT::v4i64, 1 },
569 };
570
571 // Look for AVX2 lowering tricks.
572 if (ST->hasAVX2()) {
573 if (ISD == ISD::SHL && LT.second == MVT::v16i16 &&
574 (Op2Info == TargetTransformInfo::OK_UniformConstantValue ||
575 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue))
576 // On AVX2, a packed v16i16 shift left by a constant build_vector
577 // is lowered into a vector multiply (vpmullw).
578 return getArithmeticInstrCost(Instruction::Mul, Ty, Op1Info, Op2Info,
579 TargetTransformInfo::OP_None,
580 TargetTransformInfo::OP_None);
581
582 if (const auto *Entry = CostTableLookup(AVX2ShiftCostTable, ISD, LT.second))
583 return LT.first * Entry->Cost;
584 }
585
586 static const CostTblEntry XOPShiftCostTable[] = {
587 // 128bit shifts take 1cy, but right shifts require negation beforehand.
588 { ISD::SHL, MVT::v16i8, 1 },
589 { ISD::SRL, MVT::v16i8, 2 },
590 { ISD::SRA, MVT::v16i8, 2 },
591 { ISD::SHL, MVT::v8i16, 1 },
592 { ISD::SRL, MVT::v8i16, 2 },
593 { ISD::SRA, MVT::v8i16, 2 },
594 { ISD::SHL, MVT::v4i32, 1 },
595 { ISD::SRL, MVT::v4i32, 2 },
596 { ISD::SRA, MVT::v4i32, 2 },
597 { ISD::SHL, MVT::v2i64, 1 },
598 { ISD::SRL, MVT::v2i64, 2 },
599 { ISD::SRA, MVT::v2i64, 2 },
600 // 256bit shifts require splitting if AVX2 didn't catch them above.
601 { ISD::SHL, MVT::v32i8, 2+2 },
602 { ISD::SRL, MVT::v32i8, 4+2 },
603 { ISD::SRA, MVT::v32i8, 4+2 },
604 { ISD::SHL, MVT::v16i16, 2+2 },
605 { ISD::SRL, MVT::v16i16, 4+2 },
606 { ISD::SRA, MVT::v16i16, 4+2 },
607 { ISD::SHL, MVT::v8i32, 2+2 },
608 { ISD::SRL, MVT::v8i32, 4+2 },
609 { ISD::SRA, MVT::v8i32, 4+2 },
610 { ISD::SHL, MVT::v4i64, 2+2 },
611 { ISD::SRL, MVT::v4i64, 4+2 },
612 { ISD::SRA, MVT::v4i64, 4+2 },
613 };
614
615 // Look for XOP lowering tricks.
616 if (ST->hasXOP()) {
617 // If the right shift is constant then we'll fold the negation so
618 // it's as cheap as a left shift.
619 int ShiftISD = ISD;
620 if ((ShiftISD == ISD::SRL || ShiftISD == ISD::SRA) &&
621 (Op2Info == TargetTransformInfo::OK_UniformConstantValue ||
622 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue))
623 ShiftISD = ISD::SHL;
624 if (const auto *Entry =
625 CostTableLookup(XOPShiftCostTable, ShiftISD, LT.second))
626 return LT.first * Entry->Cost;
627 }
628
629 static const CostTblEntry SSE2UniformShiftCostTable[] = {
630 // Uniform splats are cheaper for the following instructions.
631 { ISD::SHL, MVT::v16i16, 2+2 }, // 2*psllw + split.
632 { ISD::SHL, MVT::v8i32, 2+2 }, // 2*pslld + split.
633 { ISD::SHL, MVT::v4i64, 2+2 }, // 2*psllq + split.
634
635 { ISD::SRL, MVT::v16i16, 2+2 }, // 2*psrlw + split.
636 { ISD::SRL, MVT::v8i32, 2+2 }, // 2*psrld + split.
637 { ISD::SRL, MVT::v4i64, 2+2 }, // 2*psrlq + split.
638
639 { ISD::SRA, MVT::v16i16, 2+2 }, // 2*psraw + split.
640 { ISD::SRA, MVT::v8i32, 2+2 }, // 2*psrad + split.
641 { ISD::SRA, MVT::v2i64, 4 }, // 2*psrad + shuffle.
642 { ISD::SRA, MVT::v4i64, 8+2 }, // 2*(2*psrad + shuffle) + split.
643 };
644
645 if (ST->hasSSE2() &&
646 ((Op2Info == TargetTransformInfo::OK_UniformConstantValue) ||
647 (Op2Info == TargetTransformInfo::OK_UniformValue))) {
648
649 // Handle AVX2 uniform v4i64 ISD::SRA, it's not worth a table.
650 if (ISD == ISD::SRA && LT.second == MVT::v4i64 && ST->hasAVX2())
651 return LT.first * 4; // 2*psrad + shuffle.
652
653 if (const auto *Entry =
654 CostTableLookup(SSE2UniformShiftCostTable, ISD, LT.second))
655 return LT.first * Entry->Cost;
656 }
657
658 if (ISD == ISD::SHL &&
659 Op2Info == TargetTransformInfo::OK_NonUniformConstantValue) {
660 MVT VT = LT.second;
661 // Vector shift left by non uniform constant can be lowered
662 // into vector multiply.
663 if (((VT == MVT::v8i16 || VT == MVT::v4i32) && ST->hasSSE2()) ||
664 ((VT == MVT::v16i16 || VT == MVT::v8i32) && ST->hasAVX()))
665 ISD = ISD::MUL;
666 }
667
668 static const CostTblEntry AVX2CostTable[] = {
669 { ISD::SHL, MVT::v32i8, 11 }, // vpblendvb sequence.
670 { ISD::SHL, MVT::v16i16, 10 }, // extend/vpsrlvd/pack sequence.
671
672 { ISD::SRL, MVT::v32i8, 11 }, // vpblendvb sequence.
673 { ISD::SRL, MVT::v16i16, 10 }, // extend/vpsrlvd/pack sequence.
674
675 { ISD::SRA, MVT::v32i8, 24 }, // vpblendvb sequence.
676 { ISD::SRA, MVT::v16i16, 10 }, // extend/vpsravd/pack sequence.
677 { ISD::SRA, MVT::v2i64, 4 }, // srl/xor/sub sequence.
678 { ISD::SRA, MVT::v4i64, 4 }, // srl/xor/sub sequence.
679
680 { ISD::SUB, MVT::v32i8, 1 }, // psubb
681 { ISD::ADD, MVT::v32i8, 1 }, // paddb
682 { ISD::SUB, MVT::v16i16, 1 }, // psubw
683 { ISD::ADD, MVT::v16i16, 1 }, // paddw
684 { ISD::SUB, MVT::v8i32, 1 }, // psubd
685 { ISD::ADD, MVT::v8i32, 1 }, // paddd
686 { ISD::SUB, MVT::v4i64, 1 }, // psubq
687 { ISD::ADD, MVT::v4i64, 1 }, // paddq
688
689 { ISD::MUL, MVT::v32i8, 17 }, // extend/pmullw/trunc sequence.
690 { ISD::MUL, MVT::v16i8, 7 }, // extend/pmullw/trunc sequence.
691 { ISD::MUL, MVT::v16i16, 1 }, // pmullw
692 { ISD::MUL, MVT::v8i32, 2 }, // pmulld (Haswell from agner.org)
693 { ISD::MUL, MVT::v4i64, 8 }, // 3*pmuludq/3*shift/2*add
694
695 { ISD::FADD, MVT::v4f64, 1 }, // Haswell from http://www.agner.org/
696 { ISD::FADD, MVT::v8f32, 1 }, // Haswell from http://www.agner.org/
697 { ISD::FSUB, MVT::v4f64, 1 }, // Haswell from http://www.agner.org/
698 { ISD::FSUB, MVT::v8f32, 1 }, // Haswell from http://www.agner.org/
699 { ISD::FMUL, MVT::v4f64, 1 }, // Haswell from http://www.agner.org/
700 { ISD::FMUL, MVT::v8f32, 1 }, // Haswell from http://www.agner.org/
701
702 { ISD::FDIV, MVT::f32, 7 }, // Haswell from http://www.agner.org/
703 { ISD::FDIV, MVT::v4f32, 7 }, // Haswell from http://www.agner.org/
704 { ISD::FDIV, MVT::v8f32, 14 }, // Haswell from http://www.agner.org/
705 { ISD::FDIV, MVT::f64, 14 }, // Haswell from http://www.agner.org/
706 { ISD::FDIV, MVT::v2f64, 14 }, // Haswell from http://www.agner.org/
707 { ISD::FDIV, MVT::v4f64, 28 }, // Haswell from http://www.agner.org/
708 };
709
710 // Look for AVX2 lowering tricks for custom cases.
711 if (ST->hasAVX2())
712 if (const auto *Entry = CostTableLookup(AVX2CostTable, ISD, LT.second))
713 return LT.first * Entry->Cost;
714
715 static const CostTblEntry AVX1CostTable[] = {
716 // We don't have to scalarize unsupported ops. We can issue two half-sized
717 // operations and we only need to extract the upper YMM half.
718 // Two ops + 1 extract + 1 insert = 4.
719 { ISD::MUL, MVT::v16i16, 4 },
720 { ISD::MUL, MVT::v8i32, 4 },
721 { ISD::SUB, MVT::v32i8, 4 },
722 { ISD::ADD, MVT::v32i8, 4 },
723 { ISD::SUB, MVT::v16i16, 4 },
724 { ISD::ADD, MVT::v16i16, 4 },
725 { ISD::SUB, MVT::v8i32, 4 },
726 { ISD::ADD, MVT::v8i32, 4 },
727 { ISD::SUB, MVT::v4i64, 4 },
728 { ISD::ADD, MVT::v4i64, 4 },
729
730 // A v4i64 multiply is custom lowered as two split v2i64 vectors that then
731 // are lowered as a series of long multiplies(3), shifts(3) and adds(2)
732 // Because we believe v4i64 to be a legal type, we must also include the
733 // extract+insert in the cost table. Therefore, the cost here is 18
734 // instead of 8.
735 { ISD::MUL, MVT::v4i64, 18 },
736
737 { ISD::MUL, MVT::v32i8, 26 }, // extend/pmullw/trunc sequence.
738
739 { ISD::FDIV, MVT::f32, 14 }, // SNB from http://www.agner.org/
740 { ISD::FDIV, MVT::v4f32, 14 }, // SNB from http://www.agner.org/
741 { ISD::FDIV, MVT::v8f32, 28 }, // SNB from http://www.agner.org/
742 { ISD::FDIV, MVT::f64, 22 }, // SNB from http://www.agner.org/
743 { ISD::FDIV, MVT::v2f64, 22 }, // SNB from http://www.agner.org/
744 { ISD::FDIV, MVT::v4f64, 44 }, // SNB from http://www.agner.org/
745 };
746
747 if (ST->hasAVX())
748 if (const auto *Entry = CostTableLookup(AVX1CostTable, ISD, LT.second))
749 return LT.first * Entry->Cost;
750
751 static const CostTblEntry SSE42CostTable[] = {
752 { ISD::FADD, MVT::f64, 1 }, // Nehalem from http://www.agner.org/
753 { ISD::FADD, MVT::f32, 1 }, // Nehalem from http://www.agner.org/
754 { ISD::FADD, MVT::v2f64, 1 }, // Nehalem from http://www.agner.org/
755 { ISD::FADD, MVT::v4f32, 1 }, // Nehalem from http://www.agner.org/
756
757 { ISD::FSUB, MVT::f64, 1 }, // Nehalem from http://www.agner.org/
758 { ISD::FSUB, MVT::f32 , 1 }, // Nehalem from http://www.agner.org/
759 { ISD::FSUB, MVT::v2f64, 1 }, // Nehalem from http://www.agner.org/
760 { ISD::FSUB, MVT::v4f32, 1 }, // Nehalem from http://www.agner.org/
761
762 { ISD::FMUL, MVT::f64, 1 }, // Nehalem from http://www.agner.org/
763 { ISD::FMUL, MVT::f32, 1 }, // Nehalem from http://www.agner.org/
764 { ISD::FMUL, MVT::v2f64, 1 }, // Nehalem from http://www.agner.org/
765 { ISD::FMUL, MVT::v4f32, 1 }, // Nehalem from http://www.agner.org/
766
767 { ISD::FDIV, MVT::f32, 14 }, // Nehalem from http://www.agner.org/
768 { ISD::FDIV, MVT::v4f32, 14 }, // Nehalem from http://www.agner.org/
769 { ISD::FDIV, MVT::f64, 22 }, // Nehalem from http://www.agner.org/
770 { ISD::FDIV, MVT::v2f64, 22 }, // Nehalem from http://www.agner.org/
771 };
772
773 if (ST->hasSSE42())
774 if (const auto *Entry = CostTableLookup(SSE42CostTable, ISD, LT.second))
775 return LT.first * Entry->Cost;
776
777 static const CostTblEntry SSE41CostTable[] = {
778 { ISD::SHL, MVT::v16i8, 11 }, // pblendvb sequence.
779 { ISD::SHL, MVT::v32i8, 2*11+2 }, // pblendvb sequence + split.
780 { ISD::SHL, MVT::v8i16, 14 }, // pblendvb sequence.
781 { ISD::SHL, MVT::v16i16, 2*14+2 }, // pblendvb sequence + split.
782 { ISD::SHL, MVT::v4i32, 4 }, // pslld/paddd/cvttps2dq/pmulld
783 { ISD::SHL, MVT::v8i32, 2*4+2 }, // pslld/paddd/cvttps2dq/pmulld + split
784
785 { ISD::SRL, MVT::v16i8, 12 }, // pblendvb sequence.
786 { ISD::SRL, MVT::v32i8, 2*12+2 }, // pblendvb sequence + split.
787 { ISD::SRL, MVT::v8i16, 14 }, // pblendvb sequence.
788 { ISD::SRL, MVT::v16i16, 2*14+2 }, // pblendvb sequence + split.
789 { ISD::SRL, MVT::v4i32, 11 }, // Shift each lane + blend.
790 { ISD::SRL, MVT::v8i32, 2*11+2 }, // Shift each lane + blend + split.
791
792 { ISD::SRA, MVT::v16i8, 24 }, // pblendvb sequence.
793 { ISD::SRA, MVT::v32i8, 2*24+2 }, // pblendvb sequence + split.
794 { ISD::SRA, MVT::v8i16, 14 }, // pblendvb sequence.
795 { ISD::SRA, MVT::v16i16, 2*14+2 }, // pblendvb sequence + split.
796 { ISD::SRA, MVT::v4i32, 12 }, // Shift each lane + blend.
797 { ISD::SRA, MVT::v8i32, 2*12+2 }, // Shift each lane + blend + split.
798
799 { ISD::MUL, MVT::v4i32, 2 } // pmulld (Nehalem from agner.org)
800 };
801
802 if (ST->hasSSE41())
803 if (const auto *Entry = CostTableLookup(SSE41CostTable, ISD, LT.second))
804 return LT.first * Entry->Cost;
805
806 static const CostTblEntry SSE2CostTable[] = {
807 // We don't correctly identify costs of casts because they are marked as
808 // custom.
809 { ISD::SHL, MVT::v16i8, 26 }, // cmpgtb sequence.
810 { ISD::SHL, MVT::v8i16, 32 }, // cmpgtb sequence.
811 { ISD::SHL, MVT::v4i32, 2*5 }, // We optimized this using mul.
812 { ISD::SHL, MVT::v2i64, 4 }, // splat+shuffle sequence.
813 { ISD::SHL, MVT::v4i64, 2*4+2 }, // splat+shuffle sequence + split.
814
815 { ISD::SRL, MVT::v16i8, 26 }, // cmpgtb sequence.
816 { ISD::SRL, MVT::v8i16, 32 }, // cmpgtb sequence.
817 { ISD::SRL, MVT::v4i32, 16 }, // Shift each lane + blend.
818 { ISD::SRL, MVT::v2i64, 4 }, // splat+shuffle sequence.
819 { ISD::SRL, MVT::v4i64, 2*4+2 }, // splat+shuffle sequence + split.
820
821 { ISD::SRA, MVT::v16i8, 54 }, // unpacked cmpgtb sequence.
822 { ISD::SRA, MVT::v8i16, 32 }, // cmpgtb sequence.
823 { ISD::SRA, MVT::v4i32, 16 }, // Shift each lane + blend.
824 { ISD::SRA, MVT::v2i64, 12 }, // srl/xor/sub sequence.
825 { ISD::SRA, MVT::v4i64, 2*12+2 }, // srl/xor/sub sequence+split.
826
827 { ISD::MUL, MVT::v16i8, 12 }, // extend/pmullw/trunc sequence.
828 { ISD::MUL, MVT::v8i16, 1 }, // pmullw
829 { ISD::MUL, MVT::v4i32, 6 }, // 3*pmuludq/4*shuffle
830 { ISD::MUL, MVT::v2i64, 8 }, // 3*pmuludq/3*shift/2*add
831
832 { ISD::FDIV, MVT::f32, 23 }, // Pentium IV from http://www.agner.org/
833 { ISD::FDIV, MVT::v4f32, 39 }, // Pentium IV from http://www.agner.org/
834 { ISD::FDIV, MVT::f64, 38 }, // Pentium IV from http://www.agner.org/
835 { ISD::FDIV, MVT::v2f64, 69 }, // Pentium IV from http://www.agner.org/
836
837 { ISD::FADD, MVT::f32, 2 }, // Pentium IV from http://www.agner.org/
838 { ISD::FADD, MVT::f64, 2 }, // Pentium IV from http://www.agner.org/
839
840 { ISD::FSUB, MVT::f32, 2 }, // Pentium IV from http://www.agner.org/
841 { ISD::FSUB, MVT::f64, 2 }, // Pentium IV from http://www.agner.org/
842 };
843
844 if (ST->hasSSE2())
845 if (const auto *Entry = CostTableLookup(SSE2CostTable, ISD, LT.second))
846 return LT.first * Entry->Cost;
847
848 static const CostTblEntry SSE1CostTable[] = {
849 { ISD::FDIV, MVT::f32, 17 }, // Pentium III from http://www.agner.org/
850 { ISD::FDIV, MVT::v4f32, 34 }, // Pentium III from http://www.agner.org/
851
852 { ISD::FADD, MVT::f32, 1 }, // Pentium III from http://www.agner.org/
853 { ISD::FADD, MVT::v4f32, 2 }, // Pentium III from http://www.agner.org/
854
855 { ISD::FSUB, MVT::f32, 1 }, // Pentium III from http://www.agner.org/
856 { ISD::FSUB, MVT::v4f32, 2 }, // Pentium III from http://www.agner.org/
857
858 { ISD::ADD, MVT::i8, 1 }, // Pentium III from http://www.agner.org/
859 { ISD::ADD, MVT::i16, 1 }, // Pentium III from http://www.agner.org/
860 { ISD::ADD, MVT::i32, 1 }, // Pentium III from http://www.agner.org/
861
862 { ISD::SUB, MVT::i8, 1 }, // Pentium III from http://www.agner.org/
863 { ISD::SUB, MVT::i16, 1 }, // Pentium III from http://www.agner.org/
864 { ISD::SUB, MVT::i32, 1 }, // Pentium III from http://www.agner.org/
865 };
866
867 if (ST->hasSSE1())
868 if (const auto *Entry = CostTableLookup(SSE1CostTable, ISD, LT.second))
869 return LT.first * Entry->Cost;
870
871 // It is not a good idea to vectorize division. We have to scalarize it and
872 // in the process we will often end up having to spilling regular
873 // registers. The overhead of division is going to dominate most kernels
874 // anyways so try hard to prevent vectorization of division - it is
875 // generally a bad idea. Assume somewhat arbitrarily that we have to be able
876 // to hide "20 cycles" for each lane.
877 if (LT.second.isVector() && (ISD == ISD::SDIV || ISD == ISD::SREM ||
878 ISD == ISD::UDIV || ISD == ISD::UREM)) {
879 int ScalarCost = getArithmeticInstrCost(
880 Opcode, Ty->getScalarType(), Op1Info, Op2Info,
881 TargetTransformInfo::OP_None, TargetTransformInfo::OP_None);
882 return 20 * LT.first * LT.second.getVectorNumElements() * ScalarCost;
883 }
884
885 // Fallback to the default implementation.
886 return BaseT::getArithmeticInstrCost(Opcode, Ty, Op1Info, Op2Info);
887}
888
889int X86TTIImpl::getShuffleCost(TTI::ShuffleKind Kind, Type *Tp, int Index,
890 Type *SubTp) {
891 // 64-bit packed float vectors (v2f32) are widened to type v4f32.
892 // 64-bit packed integer vectors (v2i32) are widened to type v4i32.
893 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Tp);
894
895 // Treat Transpose as 2-op shuffles - there's no difference in lowering.
896 if (Kind == TTI::SK_Transpose)
897 Kind = TTI::SK_PermuteTwoSrc;
898
899 // For Broadcasts we are splatting the first element from the first input
900 // register, so only need to reference that input and all the output
901 // registers are the same.
902 if (Kind == TTI::SK_Broadcast)
903 LT.first = 1;
904
905 // Subvector extractions are free if they start at the beginning of a
906 // vector and cheap if the subvectors are aligned.
907 if (Kind == TTI::SK_ExtractSubvector && LT.second.isVector()) {
908 int NumElts = LT.second.getVectorNumElements();
909 if ((Index % NumElts) == 0)
910 return 0;
911 std::pair<int, MVT> SubLT = TLI->getTypeLegalizationCost(DL, SubTp);
912 if (SubLT.second.isVector()) {
913 int NumSubElts = SubLT.second.getVectorNumElements();
914 if ((Index % NumSubElts) == 0 && (NumElts % NumSubElts) == 0)
915 return SubLT.first;
916 // Handle some cases for widening legalization. For now we only handle
917 // cases where the original subvector was naturally aligned and evenly
918 // fit in its legalized subvector type.
919 // FIXME: Remove some of the alignment restrictions.
920 // FIXME: We can use permq for 64-bit or larger extracts from 256-bit
921 // vectors.
922 int OrigSubElts = SubTp->getVectorNumElements();
923 if (NumSubElts > OrigSubElts &&
924 (Index % OrigSubElts) == 0 && (NumSubElts % OrigSubElts) == 0 &&
925 LT.second.getVectorElementType() ==
926 SubLT.second.getVectorElementType() &&
927 LT.second.getVectorElementType().getSizeInBits() ==
928 Tp->getVectorElementType()->getPrimitiveSizeInBits()) {
929 assert(NumElts >= NumSubElts && NumElts > OrigSubElts &&((NumElts >= NumSubElts && NumElts > OrigSubElts
&& "Unexpected number of elements!") ? static_cast<
void> (0) : __assert_fail ("NumElts >= NumSubElts && NumElts > OrigSubElts && \"Unexpected number of elements!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Target/X86/X86TargetTransformInfo.cpp"
, 930, __PRETTY_FUNCTION__))
930 "Unexpected number of elements!")((NumElts >= NumSubElts && NumElts > OrigSubElts
&& "Unexpected number of elements!") ? static_cast<
void> (0) : __assert_fail ("NumElts >= NumSubElts && NumElts > OrigSubElts && \"Unexpected number of elements!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Target/X86/X86TargetTransformInfo.cpp"
, 930, __PRETTY_FUNCTION__))
;
931 Type *VecTy = VectorType::get(Tp->getVectorElementType(),
932 LT.second.getVectorNumElements());
933 Type *SubTy = VectorType::get(Tp->getVectorElementType(),
934 SubLT.second.getVectorNumElements());
935 int ExtractIndex = alignDown((Index % NumElts), NumSubElts);
936 int ExtractCost = getShuffleCost(TTI::SK_ExtractSubvector, VecTy,
937 ExtractIndex, SubTy);
938
939 // If the original size is 32-bits or more, we can use pshufd. Otherwise
940 // if we have SSSE3 we can use pshufb.
941 if (SubTp->getPrimitiveSizeInBits() >= 32 || ST->hasSSSE3())
942 return ExtractCost + 1; // pshufd or pshufb
943
944 assert(SubTp->getPrimitiveSizeInBits() == 16 &&((SubTp->getPrimitiveSizeInBits() == 16 && "Unexpected vector size"
) ? static_cast<void> (0) : __assert_fail ("SubTp->getPrimitiveSizeInBits() == 16 && \"Unexpected vector size\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Target/X86/X86TargetTransformInfo.cpp"
, 945, __PRETTY_FUNCTION__))
945 "Unexpected vector size")((SubTp->getPrimitiveSizeInBits() == 16 && "Unexpected vector size"
) ? static_cast<void> (0) : __assert_fail ("SubTp->getPrimitiveSizeInBits() == 16 && \"Unexpected vector size\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Target/X86/X86TargetTransformInfo.cpp"
, 945, __PRETTY_FUNCTION__))
;
946
947 return ExtractCost + 2; // worst case pshufhw + pshufd
948 }
949 }
950 }
951
952 // We are going to permute multiple sources and the result will be in multiple
953 // destinations. Providing an accurate cost only for splits where the element
954 // type remains the same.
955 if (Kind == TTI::SK_PermuteSingleSrc && LT.first != 1) {
956 MVT LegalVT = LT.second;
957 if (LegalVT.isVector() &&
958 LegalVT.getVectorElementType().getSizeInBits() ==
959 Tp->getVectorElementType()->getPrimitiveSizeInBits() &&
960 LegalVT.getVectorNumElements() < Tp->getVectorNumElements()) {
961
962 unsigned VecTySize = DL.getTypeStoreSize(Tp);
963 unsigned LegalVTSize = LegalVT.getStoreSize();
964 // Number of source vectors after legalization:
965 unsigned NumOfSrcs = (VecTySize + LegalVTSize - 1) / LegalVTSize;
966 // Number of destination vectors after legalization:
967 unsigned NumOfDests = LT.first;
968
969 Type *SingleOpTy = VectorType::get(Tp->getVectorElementType(),
970 LegalVT.getVectorNumElements());
971
972 unsigned NumOfShuffles = (NumOfSrcs - 1) * NumOfDests;
973 return NumOfShuffles *
974 getShuffleCost(TTI::SK_PermuteTwoSrc, SingleOpTy, 0, nullptr);
975 }
976
977 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
978 }
979
980 // For 2-input shuffles, we must account for splitting the 2 inputs into many.
981 if (Kind == TTI::SK_PermuteTwoSrc && LT.first != 1) {
982 // We assume that source and destination have the same vector type.
983 int NumOfDests = LT.first;
984 int NumOfShufflesPerDest = LT.first * 2 - 1;
985 LT.first = NumOfDests * NumOfShufflesPerDest;
986 }
987
988 static const CostTblEntry AVX512VBMIShuffleTbl[] = {
989 {TTI::SK_Reverse, MVT::v64i8, 1}, // vpermb
990 {TTI::SK_Reverse, MVT::v32i8, 1}, // vpermb
991
992 {TTI::SK_PermuteSingleSrc, MVT::v64i8, 1}, // vpermb
993 {TTI::SK_PermuteSingleSrc, MVT::v32i8, 1}, // vpermb
994
995 {TTI::SK_PermuteTwoSrc, MVT::v64i8, 1}, // vpermt2b
996 {TTI::SK_PermuteTwoSrc, MVT::v32i8, 1}, // vpermt2b
997 {TTI::SK_PermuteTwoSrc, MVT::v16i8, 1} // vpermt2b
998 };
999
1000 if (ST->hasVBMI())
1001 if (const auto *Entry =
1002 CostTableLookup(AVX512VBMIShuffleTbl, Kind, LT.second))
1003 return LT.first * Entry->Cost;
1004
1005 static const CostTblEntry AVX512BWShuffleTbl[] = {
1006 {TTI::SK_Broadcast, MVT::v32i16, 1}, // vpbroadcastw
1007 {TTI::SK_Broadcast, MVT::v64i8, 1}, // vpbroadcastb
1008
1009 {TTI::SK_Reverse, MVT::v32i16, 1}, // vpermw
1010 {TTI::SK_Reverse, MVT::v16i16, 1}, // vpermw
1011 {TTI::SK_Reverse, MVT::v64i8, 2}, // pshufb + vshufi64x2
1012
1013 {TTI::SK_PermuteSingleSrc, MVT::v32i16, 1}, // vpermw
1014 {TTI::SK_PermuteSingleSrc, MVT::v16i16, 1}, // vpermw
1015 {TTI::SK_PermuteSingleSrc, MVT::v8i16, 1}, // vpermw
1016 {TTI::SK_PermuteSingleSrc, MVT::v64i8, 8}, // extend to v32i16
1017 {TTI::SK_PermuteSingleSrc, MVT::v32i8, 3}, // vpermw + zext/trunc
1018
1019 {TTI::SK_PermuteTwoSrc, MVT::v32i16, 1}, // vpermt2w
1020 {TTI::SK_PermuteTwoSrc, MVT::v16i16, 1}, // vpermt2w
1021 {TTI::SK_PermuteTwoSrc, MVT::v8i16, 1}, // vpermt2w
1022 {TTI::SK_PermuteTwoSrc, MVT::v32i8, 3}, // zext + vpermt2w + trunc
1023 {TTI::SK_PermuteTwoSrc, MVT::v64i8, 19}, // 6 * v32i8 + 1
1024 {TTI::SK_PermuteTwoSrc, MVT::v16i8, 3} // zext + vpermt2w + trunc
1025 };
1026
1027 if (ST->hasBWI())
1028 if (const auto *Entry =
1029 CostTableLookup(AVX512BWShuffleTbl, Kind, LT.second))
1030 return LT.first * Entry->Cost;
1031
1032 static const CostTblEntry AVX512ShuffleTbl[] = {
1033 {TTI::SK_Broadcast, MVT::v8f64, 1}, // vbroadcastpd
1034 {TTI::SK_Broadcast, MVT::v16f32, 1}, // vbroadcastps
1035 {TTI::SK_Broadcast, MVT::v8i64, 1}, // vpbroadcastq
1036 {TTI::SK_Broadcast, MVT::v16i32, 1}, // vpbroadcastd
1037
1038 {TTI::SK_Reverse, MVT::v8f64, 1}, // vpermpd
1039 {TTI::SK_Reverse, MVT::v16f32, 1}, // vpermps
1040 {TTI::SK_Reverse, MVT::v8i64, 1}, // vpermq
1041 {TTI::SK_Reverse, MVT::v16i32, 1}, // vpermd
1042
1043 {TTI::SK_PermuteSingleSrc, MVT::v8f64, 1}, // vpermpd
1044 {TTI::SK_PermuteSingleSrc, MVT::v4f64, 1}, // vpermpd
1045 {TTI::SK_PermuteSingleSrc, MVT::v2f64, 1}, // vpermpd
1046 {TTI::SK_PermuteSingleSrc, MVT::v16f32, 1}, // vpermps
1047 {TTI::SK_PermuteSingleSrc, MVT::v8f32, 1}, // vpermps
1048 {TTI::SK_PermuteSingleSrc, MVT::v4f32, 1}, // vpermps
1049 {TTI::SK_PermuteSingleSrc, MVT::v8i64, 1}, // vpermq
1050 {TTI::SK_PermuteSingleSrc, MVT::v4i64, 1}, // vpermq
1051 {TTI::SK_PermuteSingleSrc, MVT::v2i64, 1}, // vpermq
1052 {TTI::SK_PermuteSingleSrc, MVT::v16i32, 1}, // vpermd
1053 {TTI::SK_PermuteSingleSrc, MVT::v8i32, 1}, // vpermd
1054 {TTI::SK_PermuteSingleSrc, MVT::v4i32, 1}, // vpermd
1055 {TTI::SK_PermuteSingleSrc, MVT::v16i8, 1}, // pshufb
1056
1057 {TTI::SK_PermuteTwoSrc, MVT::v8f64, 1}, // vpermt2pd
1058 {TTI::SK_PermuteTwoSrc, MVT::v16f32, 1}, // vpermt2ps
1059 {TTI::SK_PermuteTwoSrc, MVT::v8i64, 1}, // vpermt2q
1060 {TTI::SK_PermuteTwoSrc, MVT::v16i32, 1}, // vpermt2d
1061 {TTI::SK_PermuteTwoSrc, MVT::v4f64, 1}, // vpermt2pd
1062 {TTI::SK_PermuteTwoSrc, MVT::v8f32, 1}, // vpermt2ps
1063 {TTI::SK_PermuteTwoSrc, MVT::v4i64, 1}, // vpermt2q
1064 {TTI::SK_PermuteTwoSrc, MVT::v8i32, 1}, // vpermt2d
1065 {TTI::SK_PermuteTwoSrc, MVT::v2f64, 1}, // vpermt2pd
1066 {TTI::SK_PermuteTwoSrc, MVT::v4f32, 1}, // vpermt2ps
1067 {TTI::SK_PermuteTwoSrc, MVT::v2i64, 1}, // vpermt2q
1068 {TTI::SK_PermuteTwoSrc, MVT::v4i32, 1} // vpermt2d
1069 };
1070
1071 if (ST->hasAVX512())
1072 if (const auto *Entry = CostTableLookup(AVX512ShuffleTbl, Kind, LT.second))
1073 return LT.first * Entry->Cost;
1074
1075 static const CostTblEntry AVX2ShuffleTbl[] = {
1076 {TTI::SK_Broadcast, MVT::v4f64, 1}, // vbroadcastpd
1077 {TTI::SK_Broadcast, MVT::v8f32, 1}, // vbroadcastps
1078 {TTI::SK_Broadcast, MVT::v4i64, 1}, // vpbroadcastq
1079 {TTI::SK_Broadcast, MVT::v8i32, 1}, // vpbroadcastd
1080 {TTI::SK_Broadcast, MVT::v16i16, 1}, // vpbroadcastw
1081 {TTI::SK_Broadcast, MVT::v32i8, 1}, // vpbroadcastb
1082
1083 {TTI::SK_Reverse, MVT::v4f64, 1}, // vpermpd
1084 {TTI::SK_Reverse, MVT::v8f32, 1}, // vpermps
1085 {TTI::SK_Reverse, MVT::v4i64, 1}, // vpermq
1086 {TTI::SK_Reverse, MVT::v8i32, 1}, // vpermd
1087 {TTI::SK_Reverse, MVT::v16i16, 2}, // vperm2i128 + pshufb
1088 {TTI::SK_Reverse, MVT::v32i8, 2}, // vperm2i128 + pshufb
1089
1090 {TTI::SK_Select, MVT::v16i16, 1}, // vpblendvb
1091 {TTI::SK_Select, MVT::v32i8, 1}, // vpblendvb
1092
1093 {TTI::SK_PermuteSingleSrc, MVT::v4f64, 1}, // vpermpd
1094 {TTI::SK_PermuteSingleSrc, MVT::v8f32, 1}, // vpermps
1095 {TTI::SK_PermuteSingleSrc, MVT::v4i64, 1}, // vpermq
1096 {TTI::SK_PermuteSingleSrc, MVT::v8i32, 1}, // vpermd
1097 {TTI::SK_PermuteSingleSrc, MVT::v16i16, 4}, // vperm2i128 + 2*vpshufb
1098 // + vpblendvb
1099 {TTI::SK_PermuteSingleSrc, MVT::v32i8, 4}, // vperm2i128 + 2*vpshufb
1100 // + vpblendvb
1101
1102 {TTI::SK_PermuteTwoSrc, MVT::v4f64, 3}, // 2*vpermpd + vblendpd
1103 {TTI::SK_PermuteTwoSrc, MVT::v8f32, 3}, // 2*vpermps + vblendps
1104 {TTI::SK_PermuteTwoSrc, MVT::v4i64, 3}, // 2*vpermq + vpblendd
1105 {TTI::SK_PermuteTwoSrc, MVT::v8i32, 3}, // 2*vpermd + vpblendd
1106 {TTI::SK_PermuteTwoSrc, MVT::v16i16, 7}, // 2*vperm2i128 + 4*vpshufb
1107 // + vpblendvb
1108 {TTI::SK_PermuteTwoSrc, MVT::v32i8, 7}, // 2*vperm2i128 + 4*vpshufb
1109 // + vpblendvb
1110 };
1111
1112 if (ST->hasAVX2())
1113 if (const auto *Entry = CostTableLookup(AVX2ShuffleTbl, Kind, LT.second))
1114 return LT.first * Entry->Cost;
1115
1116 static const CostTblEntry XOPShuffleTbl[] = {
1117 {TTI::SK_PermuteSingleSrc, MVT::v4f64, 2}, // vperm2f128 + vpermil2pd
1118 {TTI::SK_PermuteSingleSrc, MVT::v8f32, 2}, // vperm2f128 + vpermil2ps
1119 {TTI::SK_PermuteSingleSrc, MVT::v4i64, 2}, // vperm2f128 + vpermil2pd
1120 {TTI::SK_PermuteSingleSrc, MVT::v8i32, 2}, // vperm2f128 + vpermil2ps
1121 {TTI::SK_PermuteSingleSrc, MVT::v16i16, 4}, // vextractf128 + 2*vpperm
1122 // + vinsertf128
1123 {TTI::SK_PermuteSingleSrc, MVT::v32i8, 4}, // vextractf128 + 2*vpperm
1124 // + vinsertf128
1125
1126 {TTI::SK_PermuteTwoSrc, MVT::v16i16, 9}, // 2*vextractf128 + 6*vpperm
1127 // + vinsertf128
1128 {TTI::SK_PermuteTwoSrc, MVT::v8i16, 1}, // vpperm
1129 {TTI::SK_PermuteTwoSrc, MVT::v32i8, 9}, // 2*vextractf128 + 6*vpperm
1130 // + vinsertf128
1131 {TTI::SK_PermuteTwoSrc, MVT::v16i8, 1}, // vpperm
1132 };
1133
1134 if (ST->hasXOP())
1135 if (const auto *Entry = CostTableLookup(XOPShuffleTbl, Kind, LT.second))
1136 return LT.first * Entry->Cost;
1137
1138 static const CostTblEntry AVX1ShuffleTbl[] = {
1139 {TTI::SK_Broadcast, MVT::v4f64, 2}, // vperm2f128 + vpermilpd
1140 {TTI::SK_Broadcast, MVT::v8f32, 2}, // vperm2f128 + vpermilps
1141 {TTI::SK_Broadcast, MVT::v4i64, 2}, // vperm2f128 + vpermilpd
1142 {TTI::SK_Broadcast, MVT::v8i32, 2}, // vperm2f128 + vpermilps
1143 {TTI::SK_Broadcast, MVT::v16i16, 3}, // vpshuflw + vpshufd + vinsertf128
1144 {TTI::SK_Broadcast, MVT::v32i8, 2}, // vpshufb + vinsertf128
1145
1146 {TTI::SK_Reverse, MVT::v4f64, 2}, // vperm2f128 + vpermilpd
1147 {TTI::SK_Reverse, MVT::v8f32, 2}, // vperm2f128 + vpermilps
1148 {TTI::SK_Reverse, MVT::v4i64, 2}, // vperm2f128 + vpermilpd
1149 {TTI::SK_Reverse, MVT::v8i32, 2}, // vperm2f128 + vpermilps
1150 {TTI::SK_Reverse, MVT::v16i16, 4}, // vextractf128 + 2*pshufb
1151 // + vinsertf128
1152 {TTI::SK_Reverse, MVT::v32i8, 4}, // vextractf128 + 2*pshufb
1153 // + vinsertf128
1154
1155 {TTI::SK_Select, MVT::v4i64, 1}, // vblendpd
1156 {TTI::SK_Select, MVT::v4f64, 1}, // vblendpd
1157 {TTI::SK_Select, MVT::v8i32, 1}, // vblendps
1158 {TTI::SK_Select, MVT::v8f32, 1}, // vblendps
1159 {TTI::SK_Select, MVT::v16i16, 3}, // vpand + vpandn + vpor
1160 {TTI::SK_Select, MVT::v32i8, 3}, // vpand + vpandn + vpor
1161
1162 {TTI::SK_PermuteSingleSrc, MVT::v4f64, 2}, // vperm2f128 + vshufpd
1163 {TTI::SK_PermuteSingleSrc, MVT::v4i64, 2}, // vperm2f128 + vshufpd
1164 {TTI::SK_PermuteSingleSrc, MVT::v8f32, 4}, // 2*vperm2f128 + 2*vshufps
1165 {TTI::SK_PermuteSingleSrc, MVT::v8i32, 4}, // 2*vperm2f128 + 2*vshufps
1166 {TTI::SK_PermuteSingleSrc, MVT::v16i16, 8}, // vextractf128 + 4*pshufb
1167 // + 2*por + vinsertf128
1168 {TTI::SK_PermuteSingleSrc, MVT::v32i8, 8}, // vextractf128 + 4*pshufb
1169 // + 2*por + vinsertf128
1170
1171 {TTI::SK_PermuteTwoSrc, MVT::v4f64, 3}, // 2*vperm2f128 + vshufpd
1172 {TTI::SK_PermuteTwoSrc, MVT::v4i64, 3}, // 2*vperm2f128 + vshufpd
1173 {TTI::SK_PermuteTwoSrc, MVT::v8f32, 4}, // 2*vperm2f128 + 2*vshufps
1174 {TTI::SK_PermuteTwoSrc, MVT::v8i32, 4}, // 2*vperm2f128 + 2*vshufps
1175 {TTI::SK_PermuteTwoSrc, MVT::v16i16, 15}, // 2*vextractf128 + 8*pshufb
1176 // + 4*por + vinsertf128
1177 {TTI::SK_PermuteTwoSrc, MVT::v32i8, 15}, // 2*vextractf128 + 8*pshufb
1178 // + 4*por + vinsertf128
1179 };
1180
1181 if (ST->hasAVX())
1182 if (const auto *Entry = CostTableLookup(AVX1ShuffleTbl, Kind, LT.second))
1183 return LT.first * Entry->Cost;
1184
1185 static const CostTblEntry SSE41ShuffleTbl[] = {
1186 {TTI::SK_Select, MVT::v2i64, 1}, // pblendw
1187 {TTI::SK_Select, MVT::v2f64, 1}, // movsd
1188 {TTI::SK_Select, MVT::v4i32, 1}, // pblendw
1189 {TTI::SK_Select, MVT::v4f32, 1}, // blendps
1190 {TTI::SK_Select, MVT::v8i16, 1}, // pblendw
1191 {TTI::SK_Select, MVT::v16i8, 1} // pblendvb
1192 };
1193
1194 if (ST->hasSSE41())
1195 if (const auto *Entry = CostTableLookup(SSE41ShuffleTbl, Kind, LT.second))
1196 return LT.first * Entry->Cost;
1197
1198 static const CostTblEntry SSSE3ShuffleTbl[] = {
1199 {TTI::SK_Broadcast, MVT::v8i16, 1}, // pshufb
1200 {TTI::SK_Broadcast, MVT::v16i8, 1}, // pshufb
1201
1202 {TTI::SK_Reverse, MVT::v8i16, 1}, // pshufb
1203 {TTI::SK_Reverse, MVT::v16i8, 1}, // pshufb
1204
1205 {TTI::SK_Select, MVT::v8i16, 3}, // 2*pshufb + por
1206 {TTI::SK_Select, MVT::v16i8, 3}, // 2*pshufb + por
1207
1208 {TTI::SK_PermuteSingleSrc, MVT::v8i16, 1}, // pshufb
1209 {TTI::SK_PermuteSingleSrc, MVT::v16i8, 1}, // pshufb
1210
1211 {TTI::SK_PermuteTwoSrc, MVT::v8i16, 3}, // 2*pshufb + por
1212 {TTI::SK_PermuteTwoSrc, MVT::v16i8, 3}, // 2*pshufb + por
1213 };
1214
1215 if (ST->hasSSSE3())
1216 if (const auto *Entry = CostTableLookup(SSSE3ShuffleTbl, Kind, LT.second))
1217 return LT.first * Entry->Cost;
1218
1219 static const CostTblEntry SSE2ShuffleTbl[] = {
1220 {TTI::SK_Broadcast, MVT::v2f64, 1}, // shufpd
1221 {TTI::SK_Broadcast, MVT::v2i64, 1}, // pshufd
1222 {TTI::SK_Broadcast, MVT::v4i32, 1}, // pshufd
1223 {TTI::SK_Broadcast, MVT::v8i16, 2}, // pshuflw + pshufd
1224 {TTI::SK_Broadcast, MVT::v16i8, 3}, // unpck + pshuflw + pshufd
1225
1226 {TTI::SK_Reverse, MVT::v2f64, 1}, // shufpd
1227 {TTI::SK_Reverse, MVT::v2i64, 1}, // pshufd
1228 {TTI::SK_Reverse, MVT::v4i32, 1}, // pshufd
1229 {TTI::SK_Reverse, MVT::v8i16, 3}, // pshuflw + pshufhw + pshufd
1230 {TTI::SK_Reverse, MVT::v16i8, 9}, // 2*pshuflw + 2*pshufhw
1231 // + 2*pshufd + 2*unpck + packus
1232
1233 {TTI::SK_Select, MVT::v2i64, 1}, // movsd
1234 {TTI::SK_Select, MVT::v2f64, 1}, // movsd
1235 {TTI::SK_Select, MVT::v4i32, 2}, // 2*shufps
1236 {TTI::SK_Select, MVT::v8i16, 3}, // pand + pandn + por
1237 {TTI::SK_Select, MVT::v16i8, 3}, // pand + pandn + por
1238
1239 {TTI::SK_PermuteSingleSrc, MVT::v2f64, 1}, // shufpd
1240 {TTI::SK_PermuteSingleSrc, MVT::v2i64, 1}, // pshufd
1241 {TTI::SK_PermuteSingleSrc, MVT::v4i32, 1}, // pshufd
1242 {TTI::SK_PermuteSingleSrc, MVT::v8i16, 5}, // 2*pshuflw + 2*pshufhw
1243 // + pshufd/unpck
1244 { TTI::SK_PermuteSingleSrc, MVT::v16i8, 10 }, // 2*pshuflw + 2*pshufhw
1245 // + 2*pshufd + 2*unpck + 2*packus
1246
1247 { TTI::SK_PermuteTwoSrc, MVT::v2f64, 1 }, // shufpd
1248 { TTI::SK_PermuteTwoSrc, MVT::v2i64, 1 }, // shufpd
1249 { TTI::SK_PermuteTwoSrc, MVT::v4i32, 2 }, // 2*{unpck,movsd,pshufd}
1250 { TTI::SK_PermuteTwoSrc, MVT::v8i16, 8 }, // blend+permute
1251 { TTI::SK_PermuteTwoSrc, MVT::v16i8, 13 }, // blend+permute
1252 };
1253
1254 if (ST->hasSSE2())
1255 if (const auto *Entry = CostTableLookup(SSE2ShuffleTbl, Kind, LT.second))
1256 return LT.first * Entry->Cost;
1257
1258 static const CostTblEntry SSE1ShuffleTbl[] = {
1259 { TTI::SK_Broadcast, MVT::v4f32, 1 }, // shufps
1260 { TTI::SK_Reverse, MVT::v4f32, 1 }, // shufps
1261 { TTI::SK_Select, MVT::v4f32, 2 }, // 2*shufps
1262 { TTI::SK_PermuteSingleSrc, MVT::v4f32, 1 }, // shufps
1263 { TTI::SK_PermuteTwoSrc, MVT::v4f32, 2 }, // 2*shufps
1264 };
1265
1266 if (ST->hasSSE1())
1267 if (const auto *Entry = CostTableLookup(SSE1ShuffleTbl, Kind, LT.second))
1268 return LT.first * Entry->Cost;
1269
1270 return BaseT::getShuffleCost(Kind, Tp, Index, SubTp);
1271}
1272
1273int X86TTIImpl::getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src,
1274 const Instruction *I) {
1275 int ISD = TLI->InstructionOpcodeToISD(Opcode);
1276 assert(ISD && "Invalid opcode")((ISD && "Invalid opcode") ? static_cast<void> (
0) : __assert_fail ("ISD && \"Invalid opcode\"", "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Target/X86/X86TargetTransformInfo.cpp"
, 1276, __PRETTY_FUNCTION__))
;
1277
1278 // FIXME: Need a better design of the cost table to handle non-simple types of
1279 // potential massive combinations (elem_num x src_type x dst_type).
1280
1281 static const TypeConversionCostTblEntry AVX512BWConversionTbl[] {
1282 { ISD::SIGN_EXTEND, MVT::v32i16, MVT::v32i8, 1 },
1283 { ISD::ZERO_EXTEND, MVT::v32i16, MVT::v32i8, 1 },
1284
1285 // Mask sign extend has an instruction.
1286 { ISD::SIGN_EXTEND, MVT::v8i16, MVT::v8i1, 1 },
1287 { ISD::SIGN_EXTEND, MVT::v16i8, MVT::v16i1, 1 },
1288 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i1, 1 },
1289 { ISD::SIGN_EXTEND, MVT::v32i8, MVT::v32i1, 1 },
1290 { ISD::SIGN_EXTEND, MVT::v32i16, MVT::v32i1, 1 },
1291 { ISD::SIGN_EXTEND, MVT::v64i8, MVT::v64i1, 1 },
1292
1293 // Mask zero extend is a load + broadcast.
1294 { ISD::ZERO_EXTEND, MVT::v8i16, MVT::v8i1, 2 },
1295 { ISD::ZERO_EXTEND, MVT::v16i8, MVT::v16i1, 2 },
1296 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i1, 2 },
1297 { ISD::ZERO_EXTEND, MVT::v32i8, MVT::v32i1, 2 },
1298 { ISD::ZERO_EXTEND, MVT::v32i16, MVT::v32i1, 2 },
1299 { ISD::ZERO_EXTEND, MVT::v64i8, MVT::v64i1, 2 },
1300 };
1301
1302 static const TypeConversionCostTblEntry AVX512DQConversionTbl[] = {
1303 { ISD::SINT_TO_FP, MVT::v2f32, MVT::v2i64, 1 },
1304 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i64, 1 },
1305 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i64, 1 },
1306 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i64, 1 },
1307 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i64, 1 },
1308 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i64, 1 },
1309
1310 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i64, 1 },
1311 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 1 },
1312 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i64, 1 },
1313 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i64, 1 },
1314 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i64, 1 },
1315 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i64, 1 },
1316
1317 { ISD::FP_TO_SINT, MVT::v2i64, MVT::v2f32, 1 },
1318 { ISD::FP_TO_SINT, MVT::v4i64, MVT::v4f32, 1 },
1319 { ISD::FP_TO_SINT, MVT::v8i64, MVT::v8f32, 1 },
1320 { ISD::FP_TO_SINT, MVT::v2i64, MVT::v2f64, 1 },
1321 { ISD::FP_TO_SINT, MVT::v4i64, MVT::v4f64, 1 },
1322 { ISD::FP_TO_SINT, MVT::v8i64, MVT::v8f64, 1 },
1323
1324 { ISD::FP_TO_UINT, MVT::v2i64, MVT::v2f32, 1 },
1325 { ISD::FP_TO_UINT, MVT::v4i64, MVT::v4f32, 1 },
1326 { ISD::FP_TO_UINT, MVT::v8i64, MVT::v8f32, 1 },
1327 { ISD::FP_TO_UINT, MVT::v2i64, MVT::v2f64, 1 },
1328 { ISD::FP_TO_UINT, MVT::v4i64, MVT::v4f64, 1 },
1329 { ISD::FP_TO_UINT, MVT::v8i64, MVT::v8f64, 1 },
1330 };
1331
1332 // TODO: For AVX512DQ + AVX512VL, we also have cheap casts for 128-bit and
1333 // 256-bit wide vectors.
1334
1335 static const TypeConversionCostTblEntry AVX512FConversionTbl[] = {
1336 { ISD::FP_EXTEND, MVT::v8f64, MVT::v8f32, 1 },
1337 { ISD::FP_EXTEND, MVT::v8f64, MVT::v16f32, 3 },
1338 { ISD::FP_ROUND, MVT::v8f32, MVT::v8f64, 1 },
1339
1340 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i32, 1 },
1341 { ISD::TRUNCATE, MVT::v16i16, MVT::v16i32, 1 },
1342 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i64, 1 },
1343 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 1 },
1344
1345 // v16i1 -> v16i32 - load + broadcast
1346 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i1, 2 },
1347 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i1, 2 },
1348 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i8, 1 },
1349 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i8, 1 },
1350 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i16, 1 },
1351 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i16, 1 },
1352 { ISD::SIGN_EXTEND, MVT::v8i64, MVT::v8i8, 1 },
1353 { ISD::ZERO_EXTEND, MVT::v8i64, MVT::v8i8, 1 },
1354 { ISD::SIGN_EXTEND, MVT::v8i64, MVT::v8i16, 1 },
1355 { ISD::ZERO_EXTEND, MVT::v8i64, MVT::v8i16, 1 },
1356 { ISD::SIGN_EXTEND, MVT::v8i64, MVT::v8i32, 1 },
1357 { ISD::ZERO_EXTEND, MVT::v8i64, MVT::v8i32, 1 },
1358
1359 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i1, 4 },
1360 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i1, 3 },
1361 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i8, 2 },
1362 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i8, 2 },
1363 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i16, 2 },
1364 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i16, 2 },
1365 { ISD::SINT_TO_FP, MVT::v16f32, MVT::v16i32, 1 },
1366 { ISD::SINT_TO_FP, MVT::v8f64, MVT::v8i32, 1 },
1367
1368 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i1, 4 },
1369 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i1, 3 },
1370 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i8, 2 },
1371 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i8, 2 },
1372 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i8, 2 },
1373 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i8, 2 },
1374 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i8, 2 },
1375 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i16, 5 },
1376 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i16, 2 },
1377 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i16, 2 },
1378 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i16, 2 },
1379 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i16, 2 },
1380 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i32, 2 },
1381 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i32, 1 },
1382 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 },
1383 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i32, 1 },
1384 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 1 },
1385 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i32, 1 },
1386 { ISD::UINT_TO_FP, MVT::v16f32, MVT::v16i32, 1 },
1387 { ISD::UINT_TO_FP, MVT::v2f32, MVT::v2i64, 5 },
1388 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i64, 26 },
1389 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 5 },
1390 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i64, 5 },
1391 { ISD::UINT_TO_FP, MVT::v8f64, MVT::v8i64, 5 },
1392
1393 { ISD::UINT_TO_FP, MVT::f32, MVT::i64, 1 },
1394 { ISD::UINT_TO_FP, MVT::f64, MVT::i64, 1 },
1395 { ISD::FP_TO_UINT, MVT::i64, MVT::f32, 1 },
1396 { ISD::FP_TO_UINT, MVT::i64, MVT::f64, 1 },
1397
1398 { ISD::FP_TO_UINT, MVT::v2i32, MVT::v2f32, 1 },
1399 { ISD::FP_TO_UINT, MVT::v4i32, MVT::v4f32, 1 },
1400 { ISD::FP_TO_UINT, MVT::v4i32, MVT::v4f64, 1 },
1401 { ISD::FP_TO_UINT, MVT::v8i32, MVT::v8f32, 1 },
1402 { ISD::FP_TO_UINT, MVT::v8i32, MVT::v8f64, 1 },
1403 { ISD::FP_TO_UINT, MVT::v8i16, MVT::v8f64, 2 },
1404 { ISD::FP_TO_UINT, MVT::v8i8, MVT::v8f64, 2 },
1405 { ISD::FP_TO_UINT, MVT::v16i32, MVT::v16f32, 1 },
1406 { ISD::FP_TO_UINT, MVT::v16i16, MVT::v16f32, 2 },
1407 { ISD::FP_TO_UINT, MVT::v16i8, MVT::v16f32, 2 },
1408 };
1409
1410 static const TypeConversionCostTblEntry AVX2ConversionTbl[] = {
1411 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i1, 3 },
1412 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i1, 3 },
1413 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 3 },
1414 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 3 },
1415 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 1 },
1416 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 1 },
1417 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 1 },
1418 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 1 },
1419 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 1 },
1420 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 1 },
1421 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 1 },
1422 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 1 },
1423 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
1424 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 1 },
1425 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
1426 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 1 },
1427
1428 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i64, 2 },
1429 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i64, 2 },
1430 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 2 },
1431 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 2 },
1432 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 2 },
1433 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 4 },
1434
1435 { ISD::FP_EXTEND, MVT::v8f64, MVT::v8f32, 3 },
1436 { ISD::FP_ROUND, MVT::v8f32, MVT::v8f64, 3 },
1437
1438 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 8 },
1439 };
1440
1441 static const TypeConversionCostTblEntry AVXConversionTbl[] = {
1442 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i1, 6 },
1443 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i1, 4 },
1444 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i1, 7 },
1445 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i1, 4 },
1446 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 4 },
1447 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 4 },
1448 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 4 },
1449 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 4 },
1450 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 4 },
1451 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 4 },
1452 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 4 },
1453 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
1454 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 4 },
1455 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 4 },
1456 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 4 },
1457 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 4 },
1458
1459 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i16, 4 },
1460 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 4 },
1461 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 5 },
1462 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i64, 4 },
1463 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i64, 4 },
1464 { ISD::TRUNCATE, MVT::v4i32, MVT::v4i64, 4 },
1465 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i64, 11 },
1466 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i64, 9 },
1467 { ISD::TRUNCATE, MVT::v8i32, MVT::v8i64, 9 },
1468 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i64, 11 },
1469
1470 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i1, 3 },
1471 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i1, 3 },
1472 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i1, 8 },
1473 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i8, 3 },
1474 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i8, 3 },
1475 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i8, 8 },
1476 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i16, 3 },
1477 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i16, 3 },
1478 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i16, 5 },
1479 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 1 },
1480 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i32, 1 },
1481 { ISD::SINT_TO_FP, MVT::v8f32, MVT::v8i32, 1 },
1482
1483 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i1, 7 },
1484 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i1, 7 },
1485 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i1, 6 },
1486 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i8, 2 },
1487 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i8, 2 },
1488 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i8, 5 },
1489 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i16, 2 },
1490 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i16, 2 },
1491 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i16, 5 },
1492 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i32, 6 },
1493 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 6 },
1494 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i32, 6 },
1495 { ISD::UINT_TO_FP, MVT::v8f32, MVT::v8i32, 9 },
1496 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 5 },
1497 { ISD::UINT_TO_FP, MVT::v4f64, MVT::v4i64, 6 },
1498 // The generic code to compute the scalar overhead is currently broken.
1499 // Workaround this limitation by estimating the scalarization overhead
1500 // here. We have roughly 10 instructions per scalar element.
1501 // Multiply that by the vector width.
1502 // FIXME: remove that when PR19268 is fixed.
1503 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i64, 13 },
1504 { ISD::SINT_TO_FP, MVT::v4f64, MVT::v4i64, 13 },
1505
1506 { ISD::FP_TO_SINT, MVT::v4i8, MVT::v4f32, 1 },
1507 { ISD::FP_TO_SINT, MVT::v8i8, MVT::v8f32, 7 },
1508 // This node is expanded into scalarized operations but BasicTTI is overly
1509 // optimistic estimating its cost. It computes 3 per element (one
1510 // vector-extract, one scalar conversion and one vector-insert). The
1511 // problem is that the inserts form a read-modify-write chain so latency
1512 // should be factored in too. Inflating the cost per element by 1.
1513 { ISD::FP_TO_UINT, MVT::v8i32, MVT::v8f32, 8*4 },
1514 { ISD::FP_TO_UINT, MVT::v4i32, MVT::v4f64, 4*4 },
1515
1516 { ISD::FP_EXTEND, MVT::v4f64, MVT::v4f32, 1 },
1517 { ISD::FP_ROUND, MVT::v4f32, MVT::v4f64, 1 },
1518 };
1519
1520 static const TypeConversionCostTblEntry SSE41ConversionTbl[] = {
1521 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 2 },
1522 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 2 },
1523 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 2 },
1524 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 2 },
1525 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 2 },
1526 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 2 },
1527
1528 { ISD::ZERO_EXTEND, MVT::v4i16, MVT::v4i8, 1 },
1529 { ISD::SIGN_EXTEND, MVT::v4i16, MVT::v4i8, 2 },
1530 { ISD::ZERO_EXTEND, MVT::v4i32, MVT::v4i8, 1 },
1531 { ISD::SIGN_EXTEND, MVT::v4i32, MVT::v4i8, 1 },
1532 { ISD::ZERO_EXTEND, MVT::v8i16, MVT::v8i8, 1 },
1533 { ISD::SIGN_EXTEND, MVT::v8i16, MVT::v8i8, 1 },
1534 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 2 },
1535 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 2 },
1536 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 2 },
1537 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 2 },
1538 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i8, 4 },
1539 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i8, 4 },
1540 { ISD::ZERO_EXTEND, MVT::v4i32, MVT::v4i16, 1 },
1541 { ISD::SIGN_EXTEND, MVT::v4i32, MVT::v4i16, 1 },
1542 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 2 },
1543 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 2 },
1544 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i16, 4 },
1545 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i16, 4 },
1546
1547 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i16, 2 },
1548 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i16, 1 },
1549 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i32, 1 },
1550 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i32, 1 },
1551 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 3 },
1552 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 3 },
1553 { ISD::TRUNCATE, MVT::v16i16, MVT::v16i32, 6 },
1554 { ISD::TRUNCATE, MVT::v2i8, MVT::v2i64, 1 }, // PSHUFB
1555
1556 { ISD::UINT_TO_FP, MVT::f32, MVT::i64, 4 },
1557 { ISD::UINT_TO_FP, MVT::f64, MVT::i64, 4 },
1558 };
1559
1560 static const TypeConversionCostTblEntry SSE2ConversionTbl[] = {
1561 // These are somewhat magic numbers justified by looking at the output of
1562 // Intel's IACA, running some kernels and making sure when we take
1563 // legalization into account the throughput will be overestimated.
1564 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 },
1565 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 },
1566 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 },
1567 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 },
1568 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v4i32, 5 },
1569 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v4i32, 2*10 },
1570 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i32, 2*10 },
1571 { ISD::SINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 },
1572 { ISD::SINT_TO_FP, MVT::v2f64, MVT::v2i64, 2*10 },
1573
1574 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v16i8, 16*10 },
1575 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v16i8, 8 },
1576 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v8i16, 15 },
1577 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v8i16, 8*10 },
1578 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v4i32, 4*10 },
1579 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v4i32, 8 },
1580 { ISD::UINT_TO_FP, MVT::v2f64, MVT::v2i64, 6 },
1581 { ISD::UINT_TO_FP, MVT::v4f32, MVT::v2i64, 15 },
1582
1583 { ISD::FP_TO_SINT, MVT::v4i16, MVT::v4f32, 2 },
1584 { ISD::FP_TO_SINT, MVT::v2i16, MVT::v2f64, 2 },
1585
1586 { ISD::FP_TO_SINT, MVT::v2i32, MVT::v2f64, 3 },
1587
1588 { ISD::UINT_TO_FP, MVT::f32, MVT::i64, 6 },
1589 { ISD::UINT_TO_FP, MVT::f64, MVT::i64, 6 },
1590
1591 { ISD::FP_TO_UINT, MVT::i64, MVT::f32, 4 },
1592 { ISD::FP_TO_UINT, MVT::i64, MVT::f64, 4 },
1593
1594 { ISD::ZERO_EXTEND, MVT::v4i16, MVT::v4i8, 1 },
1595 { ISD::SIGN_EXTEND, MVT::v4i16, MVT::v4i8, 6 },
1596 { ISD::ZERO_EXTEND, MVT::v4i32, MVT::v4i8, 2 },
1597 { ISD::SIGN_EXTEND, MVT::v4i32, MVT::v4i8, 3 },
1598 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i8, 4 },
1599 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i8, 8 },
1600 { ISD::ZERO_EXTEND, MVT::v8i16, MVT::v8i8, 1 },
1601 { ISD::SIGN_EXTEND, MVT::v8i16, MVT::v8i8, 2 },
1602 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i8, 6 },
1603 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i8, 6 },
1604 { ISD::ZERO_EXTEND, MVT::v16i16, MVT::v16i8, 3 },
1605 { ISD::SIGN_EXTEND, MVT::v16i16, MVT::v16i8, 4 },
1606 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i8, 9 },
1607 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i8, 12 },
1608 { ISD::ZERO_EXTEND, MVT::v4i32, MVT::v4i16, 1 },
1609 { ISD::SIGN_EXTEND, MVT::v4i32, MVT::v4i16, 2 },
1610 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i16, 3 },
1611 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i16, 10 },
1612 { ISD::ZERO_EXTEND, MVT::v8i32, MVT::v8i16, 3 },
1613 { ISD::SIGN_EXTEND, MVT::v8i32, MVT::v8i16, 4 },
1614 { ISD::ZERO_EXTEND, MVT::v16i32, MVT::v16i16, 6 },
1615 { ISD::SIGN_EXTEND, MVT::v16i32, MVT::v16i16, 8 },
1616 { ISD::ZERO_EXTEND, MVT::v4i64, MVT::v4i32, 3 },
1617 { ISD::SIGN_EXTEND, MVT::v4i64, MVT::v4i32, 5 },
1618
1619 { ISD::TRUNCATE, MVT::v2i8, MVT::v2i16, 2 }, // PAND+PACKUSWB
1620 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i16, 4 },
1621 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i16, 2 },
1622 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i16, 3 },
1623 { ISD::TRUNCATE, MVT::v2i8, MVT::v2i32, 3 }, // PAND+3*PACKUSWB
1624 { ISD::TRUNCATE, MVT::v2i16, MVT::v2i32, 1 },
1625 { ISD::TRUNCATE, MVT::v4i8, MVT::v4i32, 3 },
1626 { ISD::TRUNCATE, MVT::v4i16, MVT::v4i32, 3 },
1627 { ISD::TRUNCATE, MVT::v8i8, MVT::v8i32, 4 },
1628 { ISD::TRUNCATE, MVT::v16i8, MVT::v16i32, 7 },
1629 { ISD::TRUNCATE, MVT::v8i16, MVT::v8i32, 5 },
1630 { ISD::TRUNCATE, MVT::v16i16, MVT::v16i32, 10 },
1631 { ISD::TRUNCATE, MVT::v2i8, MVT::v2i64, 4 }, // PAND+3*PACKUSWB
1632 { ISD::TRUNCATE, MVT::v2i16, MVT::v2i64, 2 }, // PSHUFD+PSHUFLW
1633 { ISD::TRUNCATE, MVT::v2i32, MVT::v2i64, 1 }, // PSHUFD
1634 };
1635
1636 std::pair<int, MVT> LTSrc = TLI->getTypeLegalizationCost(DL, Src);
1637 std::pair<int, MVT> LTDest = TLI->getTypeLegalizationCost(DL, Dst);
1638
1639 if (ST->hasSSE2() && !ST->hasAVX()) {
1640 if (const auto *Entry = ConvertCostTableLookup(SSE2ConversionTbl, ISD,
1641 LTDest.second, LTSrc.second))
1642 return LTSrc.first * Entry->Cost;
1643 }
1644
1645 EVT SrcTy = TLI->getValueType(DL, Src);
1646 EVT DstTy = TLI->getValueType(DL, Dst);
1647
1648 // The function getSimpleVT only handles simple value types.
1649 if (!SrcTy.isSimple() || !DstTy.isSimple())
1650 return BaseT::getCastInstrCost(Opcode, Dst, Src);
1651
1652 MVT SimpleSrcTy = SrcTy.getSimpleVT();
1653 MVT SimpleDstTy = DstTy.getSimpleVT();
1654
1655 // Make sure that neither type is going to be split before using the
1656 // AVX512 tables. This handles -mprefer-vector-width=256
1657 // with -min-legal-vector-width<=256
1658 if (TLI->getTypeAction(SimpleSrcTy) != TargetLowering::TypeSplitVector &&
1659 TLI->getTypeAction(SimpleDstTy) != TargetLowering::TypeSplitVector) {
1660 if (ST->hasBWI())
1661 if (const auto *Entry = ConvertCostTableLookup(AVX512BWConversionTbl, ISD,
1662 SimpleDstTy, SimpleSrcTy))
1663 return Entry->Cost;
1664
1665 if (ST->hasDQI())
1666 if (const auto *Entry = ConvertCostTableLookup(AVX512DQConversionTbl, ISD,
1667 SimpleDstTy, SimpleSrcTy))
1668 return Entry->Cost;
1669
1670 if (ST->hasAVX512())
1671 if (const auto *Entry = ConvertCostTableLookup(AVX512FConversionTbl, ISD,
1672 SimpleDstTy, SimpleSrcTy))
1673 return Entry->Cost;
1674 }
1675
1676 if (ST->hasAVX2()) {
1677 if (const auto *Entry = ConvertCostTableLookup(AVX2ConversionTbl, ISD,
1678 SimpleDstTy, SimpleSrcTy))
1679 return Entry->Cost;
1680 }
1681
1682 if (ST->hasAVX()) {
1683 if (const auto *Entry = ConvertCostTableLookup(AVXConversionTbl, ISD,
1684 SimpleDstTy, SimpleSrcTy))
1685 return Entry->Cost;
1686 }
1687
1688 if (ST->hasSSE41()) {
1689 if (const auto *Entry = ConvertCostTableLookup(SSE41ConversionTbl, ISD,
1690 SimpleDstTy, SimpleSrcTy))
1691 return Entry->Cost;
1692 }
1693
1694 if (ST->hasSSE2()) {
1695 if (const auto *Entry = ConvertCostTableLookup(SSE2ConversionTbl, ISD,
1696 SimpleDstTy, SimpleSrcTy))
1697 return Entry->Cost;
1698 }
1699
1700 return BaseT::getCastInstrCost(Opcode, Dst, Src, I);
1701}
1702
1703int X86TTIImpl::getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy,
1704 const Instruction *I) {
1705 // Legalize the type.
1706 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, ValTy);
1707
1708 MVT MTy = LT.second;
1709
1710 int ISD = TLI->InstructionOpcodeToISD(Opcode);
1711 assert(ISD && "Invalid opcode")((ISD && "Invalid opcode") ? static_cast<void> (
0) : __assert_fail ("ISD && \"Invalid opcode\"", "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Target/X86/X86TargetTransformInfo.cpp"
, 1711, __PRETTY_FUNCTION__))
;
1712
1713 unsigned ExtraCost = 0;
1714 if (I && (Opcode == Instruction::ICmp || Opcode == Instruction::FCmp)) {
1715 // Some vector comparison predicates cost extra instructions.
1716 if (MTy.isVector() &&
1717 !((ST->hasXOP() && (!ST->hasAVX2() || MTy.is128BitVector())) ||
1718 (ST->hasAVX512() && 32 <= MTy.getScalarSizeInBits()) ||
1719 ST->hasBWI())) {
1720 switch (cast<CmpInst>(I)->getPredicate()) {
1721 case CmpInst::Predicate::ICMP_NE:
1722 // xor(cmpeq(x,y),-1)
1723 ExtraCost = 1;
1724 break;
1725 case CmpInst::Predicate::ICMP_SGE:
1726 case CmpInst::Predicate::ICMP_SLE:
1727 // xor(cmpgt(x,y),-1)
1728 ExtraCost = 1;
1729 break;
1730 case CmpInst::Predicate::ICMP_ULT:
1731 case CmpInst::Predicate::ICMP_UGT:
1732 // cmpgt(xor(x,signbit),xor(y,signbit))
1733 // xor(cmpeq(pmaxu(x,y),x),-1)
1734 ExtraCost = 2;
1735 break;
1736 case CmpInst::Predicate::ICMP_ULE:
1737 case CmpInst::Predicate::ICMP_UGE:
1738 if ((ST->hasSSE41() && MTy.getScalarSizeInBits() == 32) ||
1739 (ST->hasSSE2() && MTy.getScalarSizeInBits() < 32)) {
1740 // cmpeq(psubus(x,y),0)
1741 // cmpeq(pminu(x,y),x)
1742 ExtraCost = 1;
1743 } else {
1744 // xor(cmpgt(xor(x,signbit),xor(y,signbit)),-1)
1745 ExtraCost = 3;
1746 }
1747 break;
1748 default:
1749 break;
1750 }
1751 }
1752 }
1753
1754 static const CostTblEntry SLMCostTbl[] = {
1755 // slm pcmpeq/pcmpgt throughput is 2
1756 { ISD::SETCC, MVT::v2i64, 2 },
1757 };
1758
1759 static const CostTblEntry AVX512BWCostTbl[] = {
1760 { ISD::SETCC, MVT::v32i16, 1 },
1761 { ISD::SETCC, MVT::v64i8, 1 },
1762
1763 { ISD::SELECT, MVT::v32i16, 1 },
1764 { ISD::SELECT, MVT::v64i8, 1 },
1765 };
1766
1767 static const CostTblEntry AVX512CostTbl[] = {
1768 { ISD::SETCC, MVT::v8i64, 1 },
1769 { ISD::SETCC, MVT::v16i32, 1 },
1770 { ISD::SETCC, MVT::v8f64, 1 },
1771 { ISD::SETCC, MVT::v16f32, 1 },
1772
1773 { ISD::SELECT, MVT::v8i64, 1 },
1774 { ISD::SELECT, MVT::v16i32, 1 },
1775 { ISD::SELECT, MVT::v8f64, 1 },
1776 { ISD::SELECT, MVT::v16f32, 1 },
1777 };
1778
1779 static const CostTblEntry AVX2CostTbl[] = {
1780 { ISD::SETCC, MVT::v4i64, 1 },
1781 { ISD::SETCC, MVT::v8i32, 1 },
1782 { ISD::SETCC, MVT::v16i16, 1 },
1783 { ISD::SETCC, MVT::v32i8, 1 },
1784
1785 { ISD::SELECT, MVT::v4i64, 1 }, // pblendvb
1786 { ISD::SELECT, MVT::v8i32, 1 }, // pblendvb
1787 { ISD::SELECT, MVT::v16i16, 1 }, // pblendvb
1788 { ISD::SELECT, MVT::v32i8, 1 }, // pblendvb
1789 };
1790
1791 static const CostTblEntry AVX1CostTbl[] = {
1792 { ISD::SETCC, MVT::v4f64, 1 },
1793 { ISD::SETCC, MVT::v8f32, 1 },
1794 // AVX1 does not support 8-wide integer compare.
1795 { ISD::SETCC, MVT::v4i64, 4 },
1796 { ISD::SETCC, MVT::v8i32, 4 },
1797 { ISD::SETCC, MVT::v16i16, 4 },
1798 { ISD::SETCC, MVT::v32i8, 4 },
1799
1800 { ISD::SELECT, MVT::v4f64, 1 }, // vblendvpd
1801 { ISD::SELECT, MVT::v8f32, 1 }, // vblendvps
1802 { ISD::SELECT, MVT::v4i64, 1 }, // vblendvpd
1803 { ISD::SELECT, MVT::v8i32, 1 }, // vblendvps
1804 { ISD::SELECT, MVT::v16i16, 3 }, // vandps + vandnps + vorps
1805 { ISD::SELECT, MVT::v32i8, 3 }, // vandps + vandnps + vorps
1806 };
1807
1808 static const CostTblEntry SSE42CostTbl[] = {
1809 { ISD::SETCC, MVT::v2f64, 1 },
1810 { ISD::SETCC, MVT::v4f32, 1 },
1811 { ISD::SETCC, MVT::v2i64, 1 },
1812 };
1813
1814 static const CostTblEntry SSE41CostTbl[] = {
1815 { ISD::SELECT, MVT::v2f64, 1 }, // blendvpd
1816 { ISD::SELECT, MVT::v4f32, 1 }, // blendvps
1817 { ISD::SELECT, MVT::v2i64, 1 }, // pblendvb
1818 { ISD::SELECT, MVT::v4i32, 1 }, // pblendvb
1819 { ISD::SELECT, MVT::v8i16, 1 }, // pblendvb
1820 { ISD::SELECT, MVT::v16i8, 1 }, // pblendvb
1821 };
1822
1823 static const CostTblEntry SSE2CostTbl[] = {
1824 { ISD::SETCC, MVT::v2f64, 2 },
1825 { ISD::SETCC, MVT::f64, 1 },
1826 { ISD::SETCC, MVT::v2i64, 8 },
1827 { ISD::SETCC, MVT::v4i32, 1 },
1828 { ISD::SETCC, MVT::v8i16, 1 },
1829 { ISD::SETCC, MVT::v16i8, 1 },
1830
1831 { ISD::SELECT, MVT::v2f64, 3 }, // andpd + andnpd + orpd
1832 { ISD::SELECT, MVT::v2i64, 3 }, // pand + pandn + por
1833 { ISD::SELECT, MVT::v4i32, 3 }, // pand + pandn + por
1834 { ISD::SELECT, MVT::v8i16, 3 }, // pand + pandn + por
1835 { ISD::SELECT, MVT::v16i8, 3 }, // pand + pandn + por
1836 };
1837
1838 static const CostTblEntry SSE1CostTbl[] = {
1839 { ISD::SETCC, MVT::v4f32, 2 },
1840 { ISD::SETCC, MVT::f32, 1 },
1841
1842 { ISD::SELECT, MVT::v4f32, 3 }, // andps + andnps + orps
1843 };
1844
1845 if (ST->isSLM())
1846 if (const auto *Entry = CostTableLookup(SLMCostTbl, ISD, MTy))
1847 return LT.first * (ExtraCost + Entry->Cost);
1848
1849 if (ST->hasBWI())
1850 if (const auto *Entry = CostTableLookup(AVX512BWCostTbl, ISD, MTy))
1851 return LT.first * (ExtraCost + Entry->Cost);
1852
1853 if (ST->hasAVX512())
1854 if (const auto *Entry = CostTableLookup(AVX512CostTbl, ISD, MTy))
1855 return LT.first * (ExtraCost + Entry->Cost);
1856
1857 if (ST->hasAVX2())
1858 if (const auto *Entry = CostTableLookup(AVX2CostTbl, ISD, MTy))
1859 return LT.first * (ExtraCost + Entry->Cost);
1860
1861 if (ST->hasAVX())
1862 if (const auto *Entry = CostTableLookup(AVX1CostTbl, ISD, MTy))
1863 return LT.first * (ExtraCost + Entry->Cost);
1864
1865 if (ST->hasSSE42())
1866 if (const auto *Entry = CostTableLookup(SSE42CostTbl, ISD, MTy))
1867 return LT.first * (ExtraCost + Entry->Cost);
1868
1869 if (ST->hasSSE41())
1870 if (const auto *Entry = CostTableLookup(SSE41CostTbl, ISD, MTy))
1871 return LT.first * (ExtraCost + Entry->Cost);
1872
1873 if (ST->hasSSE2())
1874 if (const auto *Entry = CostTableLookup(SSE2CostTbl, ISD, MTy))
1875 return LT.first * (ExtraCost + Entry->Cost);
1876
1877 if (ST->hasSSE1())
1878 if (const auto *Entry = CostTableLookup(SSE1CostTbl, ISD, MTy))
1879 return LT.first * (ExtraCost + Entry->Cost);
1880
1881 return BaseT::getCmpSelInstrCost(Opcode, ValTy, CondTy, I);
1882}
1883
1884unsigned X86TTIImpl::getAtomicMemIntrinsicMaxElementSize() const { return 16; }
1885
1886int X86TTIImpl::getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy,
1887 ArrayRef<Type *> Tys, FastMathFlags FMF,
1888 unsigned ScalarizationCostPassed) {
1889 // Costs should match the codegen from:
1890 // BITREVERSE: llvm\test\CodeGen\X86\vector-bitreverse.ll
1891 // BSWAP: llvm\test\CodeGen\X86\bswap-vector.ll
1892 // CTLZ: llvm\test\CodeGen\X86\vector-lzcnt-*.ll
1893 // CTPOP: llvm\test\CodeGen\X86\vector-popcnt-*.ll
1894 // CTTZ: llvm\test\CodeGen\X86\vector-tzcnt-*.ll
1895 static const CostTblEntry AVX512CDCostTbl[] = {
1896 { ISD::CTLZ, MVT::v8i64, 1 },
1897 { ISD::CTLZ, MVT::v16i32, 1 },
1898 { ISD::CTLZ, MVT::v32i16, 8 },
1899 { ISD::CTLZ, MVT::v64i8, 20 },
1900 { ISD::CTLZ, MVT::v4i64, 1 },
1901 { ISD::CTLZ, MVT::v8i32, 1 },
1902 { ISD::CTLZ, MVT::v16i16, 4 },
1903 { ISD::CTLZ, MVT::v32i8, 10 },
1904 { ISD::CTLZ, MVT::v2i64, 1 },
1905 { ISD::CTLZ, MVT::v4i32, 1 },
1906 { ISD::CTLZ, MVT::v8i16, 4 },
1907 { ISD::CTLZ, MVT::v16i8, 4 },
1908 };
1909 static const CostTblEntry AVX512BWCostTbl[] = {
1910 { ISD::BITREVERSE, MVT::v8i64, 5 },
1911 { ISD::BITREVERSE, MVT::v16i32, 5 },
1912 { ISD::BITREVERSE, MVT::v32i16, 5 },
1913 { ISD::BITREVERSE, MVT::v64i8, 5 },
1914 { ISD::CTLZ, MVT::v8i64, 23 },
1915 { ISD::CTLZ, MVT::v16i32, 22 },
1916 { ISD::CTLZ, MVT::v32i16, 18 },
1917 { ISD::CTLZ, MVT::v64i8, 17 },
1918 { ISD::CTPOP, MVT::v8i64, 7 },
1919 { ISD::CTPOP, MVT::v16i32, 11 },
1920 { ISD::CTPOP, MVT::v32i16, 9 },
1921 { ISD::CTPOP, MVT::v64i8, 6 },
1922 { ISD::CTTZ, MVT::v8i64, 10 },
1923 { ISD::CTTZ, MVT::v16i32, 14 },
1924 { ISD::CTTZ, MVT::v32i16, 12 },
1925 { ISD::CTTZ, MVT::v64i8, 9 },
1926 { ISD::SADDSAT, MVT::v32i16, 1 },
1927 { ISD::SADDSAT, MVT::v64i8, 1 },
1928 { ISD::SSUBSAT, MVT::v32i16, 1 },
1929 { ISD::SSUBSAT, MVT::v64i8, 1 },
1930 { ISD::UADDSAT, MVT::v32i16, 1 },
1931 { ISD::UADDSAT, MVT::v64i8, 1 },
1932 { ISD::USUBSAT, MVT::v32i16, 1 },
1933 { ISD::USUBSAT, MVT::v64i8, 1 },
1934 };
1935 static const CostTblEntry AVX512CostTbl[] = {
1936 { ISD::BITREVERSE, MVT::v8i64, 36 },
1937 { ISD::BITREVERSE, MVT::v16i32, 24 },
1938 { ISD::CTLZ, MVT::v8i64, 29 },
1939 { ISD::CTLZ, MVT::v16i32, 35 },
1940 { ISD::CTPOP, MVT::v8i64, 16 },
1941 { ISD::CTPOP, MVT::v16i32, 24 },
1942 { ISD::CTTZ, MVT::v8i64, 20 },
1943 { ISD::CTTZ, MVT::v16i32, 28 },
1944 { ISD::USUBSAT, MVT::v16i32, 2 }, // pmaxud + psubd
1945 { ISD::USUBSAT, MVT::v2i64, 2 }, // pmaxuq + psubq
1946 { ISD::USUBSAT, MVT::v4i64, 2 }, // pmaxuq + psubq
1947 { ISD::USUBSAT, MVT::v8i64, 2 }, // pmaxuq + psubq
1948 { ISD::UADDSAT, MVT::v16i32, 3 }, // not + pminud + paddd
1949 { ISD::UADDSAT, MVT::v2i64, 3 }, // not + pminuq + paddq
1950 { ISD::UADDSAT, MVT::v4i64, 3 }, // not + pminuq + paddq
1951 { ISD::UADDSAT, MVT::v8i64, 3 }, // not + pminuq + paddq
1952 };
1953 static const CostTblEntry XOPCostTbl[] = {
1954 { ISD::BITREVERSE, MVT::v4i64, 4 },
1955 { ISD::BITREVERSE, MVT::v8i32, 4 },
1956 { ISD::BITREVERSE, MVT::v16i16, 4 },
1957 { ISD::BITREVERSE, MVT::v32i8, 4 },
1958 { ISD::BITREVERSE, MVT::v2i64, 1 },
1959 { ISD::BITREVERSE, MVT::v4i32, 1 },
1960 { ISD::BITREVERSE, MVT::v8i16, 1 },
1961 { ISD::BITREVERSE, MVT::v16i8, 1 },
1962 { ISD::BITREVERSE, MVT::i64, 3 },
1963 { ISD::BITREVERSE, MVT::i32, 3 },
1964 { ISD::BITREVERSE, MVT::i16, 3 },
1965 { ISD::BITREVERSE, MVT::i8, 3 }
1966 };
1967 static const CostTblEntry AVX2CostTbl[] = {
1968 { ISD::BITREVERSE, MVT::v4i64, 5 },
1969 { ISD::BITREVERSE, MVT::v8i32, 5 },
1970 { ISD::BITREVERSE, MVT::v16i16, 5 },
1971 { ISD::BITREVERSE, MVT::v32i8, 5 },
1972 { ISD::BSWAP, MVT::v4i64, 1 },
1973 { ISD::BSWAP, MVT::v8i32, 1 },
1974 { ISD::BSWAP, MVT::v16i16, 1 },
1975 { ISD::CTLZ, MVT::v4i64, 23 },
1976 { ISD::CTLZ, MVT::v8i32, 18 },
1977 { ISD::CTLZ, MVT::v16i16, 14 },
1978 { ISD::CTLZ, MVT::v32i8, 9 },
1979 { ISD::CTPOP, MVT::v4i64, 7 },
1980 { ISD::CTPOP, MVT::v8i32, 11 },
1981 { ISD::CTPOP, MVT::v16i16, 9 },
1982 { ISD::CTPOP, MVT::v32i8, 6 },
1983 { ISD::CTTZ, MVT::v4i64, 10 },
1984 { ISD::CTTZ, MVT::v8i32, 14 },
1985 { ISD::CTTZ, MVT::v16i16, 12 },
1986 { ISD::CTTZ, MVT::v32i8, 9 },
1987 { ISD::SADDSAT, MVT::v16i16, 1 },
1988 { ISD::SADDSAT, MVT::v32i8, 1 },
1989 { ISD::SSUBSAT, MVT::v16i16, 1 },
1990 { ISD::SSUBSAT, MVT::v32i8, 1 },
1991 { ISD::UADDSAT, MVT::v16i16, 1 },
1992 { ISD::UADDSAT, MVT::v32i8, 1 },
1993 { ISD::UADDSAT, MVT::v8i32, 3 }, // not + pminud + paddd
1994 { ISD::USUBSAT, MVT::v16i16, 1 },
1995 { ISD::USUBSAT, MVT::v32i8, 1 },
1996 { ISD::USUBSAT, MVT::v8i32, 2 }, // pmaxud + psubd
1997 { ISD::FSQRT, MVT::f32, 7 }, // Haswell from http://www.agner.org/
1998 { ISD::FSQRT, MVT::v4f32, 7 }, // Haswell from http://www.agner.org/
1999 { ISD::FSQRT, MVT::v8f32, 14 }, // Haswell from http://www.agner.org/
2000 { ISD::FSQRT, MVT::f64, 14 }, // Haswell from http://www.agner.org/
2001 { ISD::FSQRT, MVT::v2f64, 14 }, // Haswell from http://www.agner.org/
2002 { ISD::FSQRT, MVT::v4f64, 28 }, // Haswell from http://www.agner.org/
2003 };
2004 static const CostTblEntry AVX1CostTbl[] = {
2005 { ISD::BITREVERSE, MVT::v4i64, 12 }, // 2 x 128-bit Op + extract/insert
2006 { ISD::BITREVERSE, MVT::v8i32, 12 }, // 2 x 128-bit Op + extract/insert
2007 { ISD::BITREVERSE, MVT::v16i16, 12 }, // 2 x 128-bit Op + extract/insert
2008 { ISD::BITREVERSE, MVT::v32i8, 12 }, // 2 x 128-bit Op + extract/insert
2009 { ISD::BSWAP, MVT::v4i64, 4 },
2010 { ISD::BSWAP, MVT::v8i32, 4 },
2011 { ISD::BSWAP, MVT::v16i16, 4 },
2012 { ISD::CTLZ, MVT::v4i64, 48 }, // 2 x 128-bit Op + extract/insert
2013 { ISD::CTLZ, MVT::v8i32, 38 }, // 2 x 128-bit Op + extract/insert
2014 { ISD::CTLZ, MVT::v16i16, 30 }, // 2 x 128-bit Op + extract/insert
2015 { ISD::CTLZ, MVT::v32i8, 20 }, // 2 x 128-bit Op + extract/insert
2016 { ISD::CTPOP, MVT::v4i64, 16 }, // 2 x 128-bit Op + extract/insert
2017 { ISD::CTPOP, MVT::v8i32, 24 }, // 2 x 128-bit Op + extract/insert
2018 { ISD::CTPOP, MVT::v16i16, 20 }, // 2 x 128-bit Op + extract/insert
2019 { ISD::CTPOP, MVT::v32i8, 14 }, // 2 x 128-bit Op + extract/insert
2020 { ISD::CTTZ, MVT::v4i64, 22 }, // 2 x 128-bit Op + extract/insert
2021 { ISD::CTTZ, MVT::v8i32, 30 }, // 2 x 128-bit Op + extract/insert
2022 { ISD::CTTZ, MVT::v16i16, 26 }, // 2 x 128-bit Op + extract/insert
2023 { ISD::CTTZ, MVT::v32i8, 20 }, // 2 x 128-bit Op + extract/insert
2024 { ISD::SADDSAT, MVT::v16i16, 4 }, // 2 x 128-bit Op + extract/insert
2025 { ISD::SADDSAT, MVT::v32i8, 4 }, // 2 x 128-bit Op + extract/insert
2026 { ISD::SSUBSAT, MVT::v16i16, 4 }, // 2 x 128-bit Op + extract/insert
2027 { ISD::SSUBSAT, MVT::v32i8, 4 }, // 2 x 128-bit Op + extract/insert
2028 { ISD::UADDSAT, MVT::v16i16, 4 }, // 2 x 128-bit Op + extract/insert
2029 { ISD::UADDSAT, MVT::v32i8, 4 }, // 2 x 128-bit Op + extract/insert
2030 { ISD::UADDSAT, MVT::v8i32, 8 }, // 2 x 128-bit Op + extract/insert
2031 { ISD::USUBSAT, MVT::v16i16, 4 }, // 2 x 128-bit Op + extract/insert
2032 { ISD::USUBSAT, MVT::v32i8, 4 }, // 2 x 128-bit Op + extract/insert
2033 { ISD::USUBSAT, MVT::v8i32, 6 }, // 2 x 128-bit Op + extract/insert
2034 { ISD::FSQRT, MVT::f32, 14 }, // SNB from http://www.agner.org/
2035 { ISD::FSQRT, MVT::v4f32, 14 }, // SNB from http://www.agner.org/
2036 { ISD::FSQRT, MVT::v8f32, 28 }, // SNB from http://www.agner.org/
2037 { ISD::FSQRT, MVT::f64, 21 }, // SNB from http://www.agner.org/
2038 { ISD::FSQRT, MVT::v2f64, 21 }, // SNB from http://www.agner.org/
2039 { ISD::FSQRT, MVT::v4f64, 43 }, // SNB from http://www.agner.org/
2040 };
2041 static const CostTblEntry GLMCostTbl[] = {
2042 { ISD::FSQRT, MVT::f32, 19 }, // sqrtss
2043 { ISD::FSQRT, MVT::v4f32, 37 }, // sqrtps
2044 { ISD::FSQRT, MVT::f64, 34 }, // sqrtsd
2045 { ISD::FSQRT, MVT::v2f64, 67 }, // sqrtpd
2046 };
2047 static const CostTblEntry SLMCostTbl[] = {
2048 { ISD::FSQRT, MVT::f32, 20 }, // sqrtss
2049 { ISD::FSQRT, MVT::v4f32, 40 }, // sqrtps
2050 { ISD::FSQRT, MVT::f64, 35 }, // sqrtsd
2051 { ISD::FSQRT, MVT::v2f64, 70 }, // sqrtpd
2052 };
2053 static const CostTblEntry SSE42CostTbl[] = {
2054 { ISD::USUBSAT, MVT::v4i32, 2 }, // pmaxud + psubd
2055 { ISD::UADDSAT, MVT::v4i32, 3 }, // not + pminud + paddd
2056 { ISD::FSQRT, MVT::f32, 18 }, // Nehalem from http://www.agner.org/
2057 { ISD::FSQRT, MVT::v4f32, 18 }, // Nehalem from http://www.agner.org/
2058 };
2059 static const CostTblEntry SSSE3CostTbl[] = {
2060 { ISD::BITREVERSE, MVT::v2i64, 5 },
2061 { ISD::BITREVERSE, MVT::v4i32, 5 },
2062 { ISD::BITREVERSE, MVT::v8i16, 5 },
2063 { ISD::BITREVERSE, MVT::v16i8, 5 },
2064 { ISD::BSWAP, MVT::v2i64, 1 },
2065 { ISD::BSWAP, MVT::v4i32, 1 },
2066 { ISD::BSWAP, MVT::v8i16, 1 },
2067 { ISD::CTLZ, MVT::v2i64, 23 },
2068 { ISD::CTLZ, MVT::v4i32, 18 },
2069 { ISD::CTLZ, MVT::v8i16, 14 },
2070 { ISD::CTLZ, MVT::v16i8, 9 },
2071 { ISD::CTPOP, MVT::v2i64, 7 },
2072 { ISD::CTPOP, MVT::v4i32, 11 },
2073 { ISD::CTPOP, MVT::v8i16, 9 },
2074 { ISD::CTPOP, MVT::v16i8, 6 },
2075 { ISD::CTTZ, MVT::v2i64, 10 },
2076 { ISD::CTTZ, MVT::v4i32, 14 },
2077 { ISD::CTTZ, MVT::v8i16, 12 },
2078 { ISD::CTTZ, MVT::v16i8, 9 }
2079 };
2080 static const CostTblEntry SSE2CostTbl[] = {
2081 { ISD::BITREVERSE, MVT::v2i64, 29 },
2082 { ISD::BITREVERSE, MVT::v4i32, 27 },
2083 { ISD::BITREVERSE, MVT::v8i16, 27 },
2084 { ISD::BITREVERSE, MVT::v16i8, 20 },
2085 { ISD::BSWAP, MVT::v2i64, 7 },
2086 { ISD::BSWAP, MVT::v4i32, 7 },
2087 { ISD::BSWAP, MVT::v8i16, 7 },
2088 { ISD::CTLZ, MVT::v2i64, 25 },
2089 { ISD::CTLZ, MVT::v4i32, 26 },
2090 { ISD::CTLZ, MVT::v8i16, 20 },
2091 { ISD::CTLZ, MVT::v16i8, 17 },
2092 { ISD::CTPOP, MVT::v2i64, 12 },
2093 { ISD::CTPOP, MVT::v4i32, 15 },
2094 { ISD::CTPOP, MVT::v8i16, 13 },
2095 { ISD::CTPOP, MVT::v16i8, 10 },
2096 { ISD::CTTZ, MVT::v2i64, 14 },
2097 { ISD::CTTZ, MVT::v4i32, 18 },
2098 { ISD::CTTZ, MVT::v8i16, 16 },
2099 { ISD::CTTZ, MVT::v16i8, 13 },
2100 { ISD::SADDSAT, MVT::v8i16, 1 },
2101 { ISD::SADDSAT, MVT::v16i8, 1 },
2102 { ISD::SSUBSAT, MVT::v8i16, 1 },
2103 { ISD::SSUBSAT, MVT::v16i8, 1 },
2104 { ISD::UADDSAT, MVT::v8i16, 1 },
2105 { ISD::UADDSAT, MVT::v16i8, 1 },
2106 { ISD::USUBSAT, MVT::v8i16, 1 },
2107 { ISD::USUBSAT, MVT::v16i8, 1 },
2108 { ISD::FSQRT, MVT::f64, 32 }, // Nehalem from http://www.agner.org/
2109 { ISD::FSQRT, MVT::v2f64, 32 }, // Nehalem from http://www.agner.org/
2110 };
2111 static const CostTblEntry SSE1CostTbl[] = {
2112 { ISD::FSQRT, MVT::f32, 28 }, // Pentium III from http://www.agner.org/
2113 { ISD::FSQRT, MVT::v4f32, 56 }, // Pentium III from http://www.agner.org/
2114 };
2115 static const CostTblEntry LZCNT64CostTbl[] = { // 64-bit targets
2116 { ISD::CTLZ, MVT::i64, 1 },
2117 };
2118 static const CostTblEntry LZCNT32CostTbl[] = { // 32 or 64-bit targets
2119 { ISD::CTLZ, MVT::i32, 1 },
2120 { ISD::CTLZ, MVT::i16, 1 },
2121 { ISD::CTLZ, MVT::i8, 1 },
2122 };
2123 static const CostTblEntry POPCNT64CostTbl[] = { // 64-bit targets
2124 { ISD::CTPOP, MVT::i64, 1 },
2125 };
2126 static const CostTblEntry POPCNT32CostTbl[] = { // 32 or 64-bit targets
2127 { ISD::CTPOP, MVT::i32, 1 },
2128 { ISD::CTPOP, MVT::i16, 1 },
2129 { ISD::CTPOP, MVT::i8, 1 },
2130 };
2131 static const CostTblEntry X64CostTbl[] = { // 64-bit targets
2132 { ISD::BITREVERSE, MVT::i64, 14 },
2133 { ISD::CTLZ, MVT::i64, 4 }, // BSR+XOR or BSR+XOR+CMOV
2134 { ISD::CTPOP, MVT::i64, 10 },
2135 { ISD::SADDO, MVT::i64, 1 },
2136 { ISD::UADDO, MVT::i64, 1 },
2137 };
2138 static const CostTblEntry X86CostTbl[] = { // 32 or 64-bit targets
2139 { ISD::BITREVERSE, MVT::i32, 14 },
2140 { ISD::BITREVERSE, MVT::i16, 14 },
2141 { ISD::BITREVERSE, MVT::i8, 11 },
2142 { ISD::CTLZ, MVT::i32, 4 }, // BSR+XOR or BSR+XOR+CMOV
2143 { ISD::CTLZ, MVT::i16, 4 }, // BSR+XOR or BSR+XOR+CMOV
2144 { ISD::CTLZ, MVT::i8, 4 }, // BSR+XOR or BSR+XOR+CMOV
2145 { ISD::CTPOP, MVT::i32, 8 },
2146 { ISD::CTPOP, MVT::i16, 9 },
2147 { ISD::CTPOP, MVT::i8, 7 },
2148 { ISD::SADDO, MVT::i32, 1 },
2149 { ISD::SADDO, MVT::i16, 1 },
2150 { ISD::SADDO, MVT::i8, 1 },
2151 { ISD::UADDO, MVT::i32, 1 },
2152 { ISD::UADDO, MVT::i16, 1 },
2153 { ISD::UADDO, MVT::i8, 1 },
2154 };
2155
2156 Type *OpTy = RetTy;
2157 unsigned ISD = ISD::DELETED_NODE;
2158 switch (IID) {
2159 default:
2160 break;
2161 case Intrinsic::bitreverse:
2162 ISD = ISD::BITREVERSE;
2163 break;
2164 case Intrinsic::bswap:
2165 ISD = ISD::BSWAP;
2166 break;
2167 case Intrinsic::ctlz:
2168 ISD = ISD::CTLZ;
2169 break;
2170 case Intrinsic::ctpop:
2171 ISD = ISD::CTPOP;
2172 break;
2173 case Intrinsic::cttz:
2174 ISD = ISD::CTTZ;
2175 break;
2176 case Intrinsic::sadd_sat:
2177 ISD = ISD::SADDSAT;
2178 break;
2179 case Intrinsic::ssub_sat:
2180 ISD = ISD::SSUBSAT;
2181 break;
2182 case Intrinsic::uadd_sat:
2183 ISD = ISD::UADDSAT;
2184 break;
2185 case Intrinsic::usub_sat:
2186 ISD = ISD::USUBSAT;
2187 break;
2188 case Intrinsic::sqrt:
2189 ISD = ISD::FSQRT;
2190 break;
2191 case Intrinsic::sadd_with_overflow:
2192 case Intrinsic::ssub_with_overflow:
2193 // SSUBO has same costs so don't duplicate.
2194 ISD = ISD::SADDO;
2195 OpTy = RetTy->getContainedType(0);
2196 break;
2197 case Intrinsic::uadd_with_overflow:
2198 case Intrinsic::usub_with_overflow:
2199 // USUBO has same costs so don't duplicate.
2200 ISD = ISD::UADDO;
2201 OpTy = RetTy->getContainedType(0);
2202 break;
2203 }
2204
2205 if (ISD != ISD::DELETED_NODE) {
2206 // Legalize the type.
2207 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, OpTy);
2208 MVT MTy = LT.second;
2209
2210 // Attempt to lookup cost.
2211 if (ST->useGLMDivSqrtCosts())
2212 if (const auto *Entry = CostTableLookup(GLMCostTbl, ISD, MTy))
2213 return LT.first * Entry->Cost;
2214
2215 if (ST->isSLM())
2216 if (const auto *Entry = CostTableLookup(SLMCostTbl, ISD, MTy))
2217 return LT.first * Entry->Cost;
2218
2219 if (ST->hasCDI())
2220 if (const auto *Entry = CostTableLookup(AVX512CDCostTbl, ISD, MTy))
2221 return LT.first * Entry->Cost;
2222
2223 if (ST->hasBWI())
2224 if (const auto *Entry = CostTableLookup(AVX512BWCostTbl, ISD, MTy))
2225 return LT.first * Entry->Cost;
2226
2227 if (ST->hasAVX512())
2228 if (const auto *Entry = CostTableLookup(AVX512CostTbl, ISD, MTy))
2229 return LT.first * Entry->Cost;
2230
2231 if (ST->hasXOP())
2232 if (const auto *Entry = CostTableLookup(XOPCostTbl, ISD, MTy))
2233 return LT.first * Entry->Cost;
2234
2235 if (ST->hasAVX2())
2236 if (const auto *Entry = CostTableLookup(AVX2CostTbl, ISD, MTy))
2237 return LT.first * Entry->Cost;
2238
2239 if (ST->hasAVX())
2240 if (const auto *Entry = CostTableLookup(AVX1CostTbl, ISD, MTy))
2241 return LT.first * Entry->Cost;
2242
2243 if (ST->hasSSE42())
2244 if (const auto *Entry = CostTableLookup(SSE42CostTbl, ISD, MTy))
2245 return LT.first * Entry->Cost;
2246
2247 if (ST->hasSSSE3())
2248 if (const auto *Entry = CostTableLookup(SSSE3CostTbl, ISD, MTy))
2249 return LT.first * Entry->Cost;
2250
2251 if (ST->hasSSE2())
2252 if (const auto *Entry = CostTableLookup(SSE2CostTbl, ISD, MTy))
2253 return LT.first * Entry->Cost;
2254
2255 if (ST->hasSSE1())
2256 if (const auto *Entry = CostTableLookup(SSE1CostTbl, ISD, MTy))
2257 return LT.first * Entry->Cost;
2258
2259 if (ST->hasLZCNT()) {
2260 if (ST->is64Bit())
2261 if (const auto *Entry = CostTableLookup(LZCNT64CostTbl, ISD, MTy))
2262 return LT.first * Entry->Cost;
2263
2264 if (const auto *Entry = CostTableLookup(LZCNT32CostTbl, ISD, MTy))
2265 return LT.first * Entry->Cost;
2266 }
2267
2268 if (ST->hasPOPCNT()) {
2269 if (ST->is64Bit())
2270 if (const auto *Entry = CostTableLookup(POPCNT64CostTbl, ISD, MTy))
2271 return LT.first * Entry->Cost;
2272
2273 if (const auto *Entry = CostTableLookup(POPCNT32CostTbl, ISD, MTy))
2274 return LT.first * Entry->Cost;
2275 }
2276
2277 // TODO - add BMI (TZCNT) scalar handling
2278
2279 if (ST->is64Bit())
2280 if (const auto *Entry = CostTableLookup(X64CostTbl, ISD, MTy))
2281 return LT.first * Entry->Cost;
2282
2283 if (const auto *Entry = CostTableLookup(X86CostTbl, ISD, MTy))
2284 return LT.first * Entry->Cost;
2285 }
2286
2287 return BaseT::getIntrinsicInstrCost(IID, RetTy, Tys, FMF, ScalarizationCostPassed);
2288}
2289
2290int X86TTIImpl::getIntrinsicInstrCost(Intrinsic::ID IID, Type *RetTy,
2291 ArrayRef<Value *> Args, FastMathFlags FMF,
2292 unsigned VF) {
2293 static const CostTblEntry AVX512CostTbl[] = {
2294 { ISD::ROTL, MVT::v8i64, 1 },
2295 { ISD::ROTL, MVT::v4i64, 1 },
2296 { ISD::ROTL, MVT::v2i64, 1 },
2297 { ISD::ROTL, MVT::v16i32, 1 },
2298 { ISD::ROTL, MVT::v8i32, 1 },
2299 { ISD::ROTL, MVT::v4i32, 1 },
2300 { ISD::ROTR, MVT::v8i64, 1 },
2301 { ISD::ROTR, MVT::v4i64, 1 },
2302 { ISD::ROTR, MVT::v2i64, 1 },
2303 { ISD::ROTR, MVT::v16i32, 1 },
2304 { ISD::ROTR, MVT::v8i32, 1 },
2305 { ISD::ROTR, MVT::v4i32, 1 }
2306 };
2307 // XOP: ROTL = VPROT(X,Y), ROTR = VPROT(X,SUB(0,Y))
2308 static const CostTblEntry XOPCostTbl[] = {
2309 { ISD::ROTL, MVT::v4i64, 4 },
2310 { ISD::ROTL, MVT::v8i32, 4 },
2311 { ISD::ROTL, MVT::v16i16, 4 },
2312 { ISD::ROTL, MVT::v32i8, 4 },
2313 { ISD::ROTL, MVT::v2i64, 1 },
2314 { ISD::ROTL, MVT::v4i32, 1 },
2315 { ISD::ROTL, MVT::v8i16, 1 },
2316 { ISD::ROTL, MVT::v16i8, 1 },
2317 { ISD::ROTR, MVT::v4i64, 6 },
2318 { ISD::ROTR, MVT::v8i32, 6 },
2319 { ISD::ROTR, MVT::v16i16, 6 },
2320 { ISD::ROTR, MVT::v32i8, 6 },
2321 { ISD::ROTR, MVT::v2i64, 2 },
2322 { ISD::ROTR, MVT::v4i32, 2 },
2323 { ISD::ROTR, MVT::v8i16, 2 },
2324 { ISD::ROTR, MVT::v16i8, 2 }
2325 };
2326 static const CostTblEntry X64CostTbl[] = { // 64-bit targets
2327 { ISD::ROTL, MVT::i64, 1 },
2328 { ISD::ROTR, MVT::i64, 1 },
2329 { ISD::FSHL, MVT::i64, 4 }
2330 };
2331 static const CostTblEntry X86CostTbl[] = { // 32 or 64-bit targets
2332 { ISD::ROTL, MVT::i32, 1 },
2333 { ISD::ROTL, MVT::i16, 1 },
2334 { ISD::ROTL, MVT::i8, 1 },
2335 { ISD::ROTR, MVT::i32, 1 },
2336 { ISD::ROTR, MVT::i16, 1 },
2337 { ISD::ROTR, MVT::i8, 1 },
2338 { ISD::FSHL, MVT::i32, 4 },
2339 { ISD::FSHL, MVT::i16, 4 },
2340 { ISD::FSHL, MVT::i8, 4 }
2341 };
2342
2343 unsigned ISD = ISD::DELETED_NODE;
2344 switch (IID) {
2345 default:
2346 break;
2347 case Intrinsic::fshl:
2348 ISD = ISD::FSHL;
2349 if (Args[0] == Args[1])
2350 ISD = ISD::ROTL;
2351 break;
2352 case Intrinsic::fshr:
2353 // FSHR has same costs so don't duplicate.
2354 ISD = ISD::FSHL;
2355 if (Args[0] == Args[1])
2356 ISD = ISD::ROTR;
2357 break;
2358 }
2359
2360 if (ISD != ISD::DELETED_NODE) {
2361 // Legalize the type.
2362 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, RetTy);
2363 MVT MTy = LT.second;
2364
2365 // Attempt to lookup cost.
2366 if (ST->hasAVX512())
2367 if (const auto *Entry = CostTableLookup(AVX512CostTbl, ISD, MTy))
2368 return LT.first * Entry->Cost;
2369
2370 if (ST->hasXOP())
2371 if (const auto *Entry = CostTableLookup(XOPCostTbl, ISD, MTy))
2372 return LT.first * Entry->Cost;
2373
2374 if (ST->is64Bit())
2375 if (const auto *Entry = CostTableLookup(X64CostTbl, ISD, MTy))
2376 return LT.first * Entry->Cost;
2377
2378 if (const auto *Entry = CostTableLookup(X86CostTbl, ISD, MTy))
2379 return LT.first * Entry->Cost;
2380 }
2381
2382 return BaseT::getIntrinsicInstrCost(IID, RetTy, Args, FMF, VF);
2383}
2384
2385int X86TTIImpl::getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index) {
2386 static const CostTblEntry SLMCostTbl[] = {
2387 { ISD::EXTRACT_VECTOR_ELT, MVT::i8, 4 },
2388 { ISD::EXTRACT_VECTOR_ELT, MVT::i16, 4 },
2389 { ISD::EXTRACT_VECTOR_ELT, MVT::i32, 4 },
2390 { ISD::EXTRACT_VECTOR_ELT, MVT::i64, 7 }
2391 };
2392
2393 assert(Val->isVectorTy() && "This must be a vector type")((Val->isVectorTy() && "This must be a vector type"
) ? static_cast<void> (0) : __assert_fail ("Val->isVectorTy() && \"This must be a vector type\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Target/X86/X86TargetTransformInfo.cpp"
, 2393, __PRETTY_FUNCTION__))
;
2394
2395 Type *ScalarType = Val->getScalarType();
2396
2397 if (Index != -1U) {
2398 // Legalize the type.
2399 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Val);
2400
2401 // This type is legalized to a scalar type.
2402 if (!LT.second.isVector())
2403 return 0;
2404
2405 // The type may be split. Normalize the index to the new type.
2406 unsigned Width = LT.second.getVectorNumElements();
2407 Index = Index % Width;
2408
2409 if (Index == 0) {
2410 // Floating point scalars are already located in index #0.
2411 if (ScalarType->isFloatingPointTy())
2412 return 0;
2413
2414 // Assume movd/movq XMM <-> GPR is relatively cheap on all targets.
2415 if (ScalarType->isIntegerTy())
2416 return 1;
2417 }
2418
2419 int ISD = TLI->InstructionOpcodeToISD(Opcode);
2420 assert(ISD && "Unexpected vector opcode")((ISD && "Unexpected vector opcode") ? static_cast<
void> (0) : __assert_fail ("ISD && \"Unexpected vector opcode\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Target/X86/X86TargetTransformInfo.cpp"
, 2420, __PRETTY_FUNCTION__))
;
2421 MVT MScalarTy = LT.second.getScalarType();
2422 if (ST->isSLM())
2423 if (auto *Entry = CostTableLookup(SLMCostTbl, ISD, MScalarTy))
2424 return LT.first * Entry->Cost;
2425 }
2426
2427 // Add to the base cost if we know that the extracted element of a vector is
2428 // destined to be moved to and used in the integer register file.
2429 int RegisterFileMoveCost = 0;
2430 if (Opcode == Instruction::ExtractElement && ScalarType->isPointerTy())
2431 RegisterFileMoveCost = 1;
2432
2433 return BaseT::getVectorInstrCost(Opcode, Val, Index) + RegisterFileMoveCost;
2434}
2435
2436int X86TTIImpl::getMemoryOpCost(unsigned Opcode, Type *Src,
2437 MaybeAlign Alignment, unsigned AddressSpace,
2438 const Instruction *I) {
2439 // Handle non-power-of-two vectors such as <3 x float>
2440 if (VectorType *VTy = dyn_cast<VectorType>(Src)) {
2441 unsigned NumElem = VTy->getVectorNumElements();
2442
2443 // Handle a few common cases:
2444 // <3 x float>
2445 if (NumElem == 3 && VTy->getScalarSizeInBits() == 32)
2446 // Cost = 64 bit store + extract + 32 bit store.
2447 return 3;
2448
2449 // <3 x double>
2450 if (NumElem == 3 && VTy->getScalarSizeInBits() == 64)
2451 // Cost = 128 bit store + unpack + 64 bit store.
2452 return 3;
2453
2454 // Assume that all other non-power-of-two numbers are scalarized.
2455 if (!isPowerOf2_32(NumElem)) {
2456 int Cost = BaseT::getMemoryOpCost(Opcode, VTy->getScalarType(), Alignment,
2457 AddressSpace);
2458 int SplitCost = getScalarizationOverhead(Src, Opcode == Instruction::Load,
2459 Opcode == Instruction::Store);
2460 return NumElem * Cost + SplitCost;
2461 }
2462 }
2463
2464 // Legalize the type.
2465 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, Src);
2466 assert((Opcode == Instruction::Load || Opcode == Instruction::Store) &&(((Opcode == Instruction::Load || Opcode == Instruction::Store
) && "Invalid Opcode") ? static_cast<void> (0) :
__assert_fail ("(Opcode == Instruction::Load || Opcode == Instruction::Store) && \"Invalid Opcode\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Target/X86/X86TargetTransformInfo.cpp"
, 2467, __PRETTY_FUNCTION__))
2467 "Invalid Opcode")(((Opcode == Instruction::Load || Opcode == Instruction::Store
) && "Invalid Opcode") ? static_cast<void> (0) :
__assert_fail ("(Opcode == Instruction::Load || Opcode == Instruction::Store) && \"Invalid Opcode\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Target/X86/X86TargetTransformInfo.cpp"
, 2467, __PRETTY_FUNCTION__))
;
2468
2469 // Each load/store unit costs 1.
2470 int Cost = LT.first * 1;
2471
2472 // This isn't exactly right. We're using slow unaligned 32-byte accesses as a
2473 // proxy for a double-pumped AVX memory interface such as on Sandybridge.
2474 if (LT.second.getStoreSize() == 32 && ST->isUnalignedMem32Slow())
2475 Cost *= 2;
2476
2477 return Cost;
2478}
2479
2480int X86TTIImpl::getMaskedMemoryOpCost(unsigned Opcode, Type *SrcTy,
2481 unsigned Alignment,
2482 unsigned AddressSpace) {
2483 bool IsLoad = (Instruction::Load == Opcode);
2484 bool IsStore = (Instruction::Store == Opcode);
2485
2486 VectorType *SrcVTy = dyn_cast<VectorType>(SrcTy);
2487 if (!SrcVTy)
2488 // To calculate scalar take the regular cost, without mask
2489 return getMemoryOpCost(Opcode, SrcTy, MaybeAlign(Alignment), AddressSpace);
2490
2491 unsigned NumElem = SrcVTy->getVectorNumElements();
2492 VectorType *MaskTy =
2493 VectorType::get(Type::getInt8Ty(SrcVTy->getContext()), NumElem);
2494 if ((IsLoad && !isLegalMaskedLoad(SrcVTy, MaybeAlign(Alignment))) ||
2495 (IsStore && !isLegalMaskedStore(SrcVTy, MaybeAlign(Alignment))) ||
2496 !isPowerOf2_32(NumElem)) {
2497 // Scalarization
2498 int MaskSplitCost = getScalarizationOverhead(MaskTy, false, true);
2499 int ScalarCompareCost = getCmpSelInstrCost(
2500 Instruction::ICmp, Type::getInt8Ty(SrcVTy->getContext()), nullptr);
2501 int BranchCost = getCFInstrCost(Instruction::Br);
2502 int MaskCmpCost = NumElem * (BranchCost + ScalarCompareCost);
2503
2504 int ValueSplitCost = getScalarizationOverhead(SrcVTy, IsLoad, IsStore);
2505 int MemopCost =
2506 NumElem * BaseT::getMemoryOpCost(Opcode, SrcVTy->getScalarType(),
2507 MaybeAlign(Alignment), AddressSpace);
2508 return MemopCost + ValueSplitCost + MaskSplitCost + MaskCmpCost;
2509 }
2510
2511 // Legalize the type.
2512 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, SrcVTy);
2513 auto VT = TLI->getValueType(DL, SrcVTy);
2514 int Cost = 0;
2515 if (VT.isSimple() && LT.second != VT.getSimpleVT() &&
2516 LT.second.getVectorNumElements() == NumElem)
2517 // Promotion requires expand/truncate for data and a shuffle for mask.
2518 Cost += getShuffleCost(TTI::SK_PermuteTwoSrc, SrcVTy, 0, nullptr) +
2519 getShuffleCost(TTI::SK_PermuteTwoSrc, MaskTy, 0, nullptr);
2520
2521 else if (LT.second.getVectorNumElements() > NumElem) {
2522 VectorType *NewMaskTy = VectorType::get(MaskTy->getVectorElementType(),
2523 LT.second.getVectorNumElements());
2524 // Expanding requires fill mask with zeroes
2525 Cost += getShuffleCost(TTI::SK_InsertSubvector, NewMaskTy, 0, MaskTy);
2526 }
2527
2528 // Pre-AVX512 - each maskmov load costs 2 + store costs ~8.
2529 if (!ST->hasAVX512())
2530 return Cost + LT.first * (IsLoad ? 2 : 8);
2531
2532 // AVX-512 masked load/store is cheapper
2533 return Cost + LT.first;
2534}
2535
2536int X86TTIImpl::getAddressComputationCost(Type *Ty, ScalarEvolution *SE,
2537 const SCEV *Ptr) {
2538 // Address computations in vectorized code with non-consecutive addresses will
2539 // likely result in more instructions compared to scalar code where the
2540 // computation can more often be merged into the index mode. The resulting
2541 // extra micro-ops can significantly decrease throughput.
2542 const unsigned NumVectorInstToHideOverhead = 10;
2543
2544 // Cost modeling of Strided Access Computation is hidden by the indexing
2545 // modes of X86 regardless of the stride value. We dont believe that there
2546 // is a difference between constant strided access in gerenal and constant
2547 // strided value which is less than or equal to 64.
2548 // Even in the case of (loop invariant) stride whose value is not known at
2549 // compile time, the address computation will not incur more than one extra
2550 // ADD instruction.
2551 if (Ty->isVectorTy() && SE) {
2552 if (!BaseT::isStridedAccess(Ptr))
2553 return NumVectorInstToHideOverhead;
2554 if (!BaseT::getConstantStrideStep(SE, Ptr))
2555 return 1;
2556 }
2557
2558 return BaseT::getAddressComputationCost(Ty, SE, Ptr);
2559}
2560
2561int X86TTIImpl::getArithmeticReductionCost(unsigned Opcode, Type *ValTy,
2562 bool IsPairwise) {
2563 // We use the Intel Architecture Code Analyzer(IACA) to measure the throughput
2564 // and make it as the cost.
2565
2566 static const CostTblEntry SLMCostTblPairWise[] = {
2567 { ISD::FADD, MVT::v2f64, 3 },
2568 { ISD::ADD, MVT::v2i64, 5 },
2569 };
2570
2571 static const CostTblEntry SSE2CostTblPairWise[] = {
2572 { ISD::FADD, MVT::v2f64, 2 },
2573 { ISD::FADD, MVT::v4f32, 4 },
2574 { ISD::ADD, MVT::v2i64, 2 }, // The data reported by the IACA tool is "1.6".
2575 { ISD::ADD, MVT::v2i32, 2 }, // FIXME: chosen to be less than v4i32.
2576 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.5".
2577 { ISD::ADD, MVT::v2i16, 3 }, // FIXME: chosen to be less than v4i16
2578 { ISD::ADD, MVT::v4i16, 4 }, // FIXME: chosen to be less than v8i16
2579 { ISD::ADD, MVT::v8i16, 5 },
2580 { ISD::ADD, MVT::v2i8, 2 },
2581 { ISD::ADD, MVT::v4i8, 2 },
2582 { ISD::ADD, MVT::v8i8, 2 },
2583 { ISD::ADD, MVT::v16i8, 3 },
2584 };
2585
2586 static const CostTblEntry AVX1CostTblPairWise[] = {
2587 { ISD::FADD, MVT::v4f64, 5 },
2588 { ISD::FADD, MVT::v8f32, 7 },
2589 { ISD::ADD, MVT::v2i64, 1 }, // The data reported by the IACA tool is "1.5".
2590 { ISD::ADD, MVT::v4i64, 5 }, // The data reported by the IACA tool is "4.8".
2591 { ISD::ADD, MVT::v8i32, 5 },
2592 { ISD::ADD, MVT::v16i16, 6 },
2593 { ISD::ADD, MVT::v32i8, 4 },
2594 };
2595
2596 static const CostTblEntry SLMCostTblNoPairWise[] = {
2597 { ISD::FADD, MVT::v2f64, 3 },
2598 { ISD::ADD, MVT::v2i64, 5 },
2599 };
2600
2601 static const CostTblEntry SSE2CostTblNoPairWise[] = {
2602 { ISD::FADD, MVT::v2f64, 2 },
2603 { ISD::FADD, MVT::v4f32, 4 },
2604 { ISD::ADD, MVT::v2i64, 2 }, // The data reported by the IACA tool is "1.6".
2605 { ISD::ADD, MVT::v2i32, 2 }, // FIXME: chosen to be less than v4i32
2606 { ISD::ADD, MVT::v4i32, 3 }, // The data reported by the IACA tool is "3.3".
2607 { ISD::ADD, MVT::v2i16, 2 }, // The data reported by the IACA tool is "4.3".
2608 { ISD::ADD, MVT::v4i16, 3 }, // The data reported by the IACA tool is "4.3".
2609 { ISD::ADD, MVT::v8i16, 4 }, // The data reported by the IACA tool is "4.3".
2610 { ISD::ADD, MVT::v2i8, 2 },
2611 { ISD::ADD, MVT::v4i8, 2 },
2612 { ISD::ADD, MVT::v8i8, 2 },
2613 { ISD::ADD, MVT::v16i8, 3 },
2614 };
2615
2616 static const CostTblEntry AVX1CostTblNoPairWise[] = {
2617 { ISD::FADD, MVT::v4f64, 3 },
2618 { ISD::FADD, MVT::v4f32, 3 },
2619 { ISD::FADD, MVT::v8f32, 4 },
2620 { ISD::ADD, MVT::v2i64, 1 }, // The data reported by the IACA tool is "1.5".
2621 { ISD::ADD, MVT::v4i64, 3 },
2622 { ISD::ADD, MVT::v8i32, 5 },
2623 { ISD::ADD, MVT::v16i16, 5 },
2624 { ISD::ADD, MVT::v32i8, 4 },
2625 };
2626
2627 int ISD = TLI->InstructionOpcodeToISD(Opcode);
2628 assert(ISD && "Invalid opcode")((ISD && "Invalid opcode") ? static_cast<void> (
0) : __assert_fail ("ISD && \"Invalid opcode\"", "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Target/X86/X86TargetTransformInfo.cpp"
, 2628, __PRETTY_FUNCTION__))
;
2629
2630 // Before legalizing the type, give a chance to look up illegal narrow types
2631 // in the table.
2632 // FIXME: Is there a better way to do this?
2633 EVT VT = TLI->getValueType(DL, ValTy);
2634 if (VT.isSimple()) {
2635 MVT MTy = VT.getSimpleVT();
2636 if (IsPairwise) {
2637 if (ST->isSLM())
2638 if (const auto *Entry = CostTableLookup(SLMCostTblPairWise, ISD, MTy))
2639 return Entry->Cost;
2640
2641 if (ST->hasAVX())
2642 if (const auto *Entry = CostTableLookup(AVX1CostTblPairWise, ISD, MTy))
2643 return Entry->Cost;
2644
2645 if (ST->hasSSE2())
2646 if (const auto *Entry = CostTableLookup(SSE2CostTblPairWise, ISD, MTy))
2647 return Entry->Cost;
2648 } else {
2649 if (ST->isSLM())
2650 if (const auto *Entry = CostTableLookup(SLMCostTblNoPairWise, ISD, MTy))
2651 return Entry->Cost;
2652
2653 if (ST->hasAVX())
2654 if (const auto *Entry = CostTableLookup(AVX1CostTblNoPairWise, ISD, MTy))
2655 return Entry->Cost;
2656
2657 if (ST->hasSSE2())
2658 if (const auto *Entry = CostTableLookup(SSE2CostTblNoPairWise, ISD, MTy))
2659 return Entry->Cost;
2660 }
2661 }
2662
2663 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, ValTy);
2664
2665 MVT MTy = LT.second;
2666
2667 if (IsPairwise) {
2668 if (ST->isSLM())
2669 if (const auto *Entry = CostTableLookup(SLMCostTblPairWise, ISD, MTy))
2670 return LT.first * Entry->Cost;
2671
2672 if (ST->hasAVX())
2673 if (const auto *Entry = CostTableLookup(AVX1CostTblPairWise, ISD, MTy))
2674 return LT.first * Entry->Cost;
2675
2676 if (ST->hasSSE2())
2677 if (const auto *Entry = CostTableLookup(SSE2CostTblPairWise, ISD, MTy))
2678 return LT.first * Entry->Cost;
2679 } else {
2680 if (ST->isSLM())
2681 if (const auto *Entry = CostTableLookup(SLMCostTblNoPairWise, ISD, MTy))
2682 return LT.first * Entry->Cost;
2683
2684 if (ST->hasAVX())
2685 if (const auto *Entry = CostTableLookup(AVX1CostTblNoPairWise, ISD, MTy))
2686 return LT.first * Entry->Cost;
2687
2688 if (ST->hasSSE2())
2689 if (const auto *Entry = CostTableLookup(SSE2CostTblNoPairWise, ISD, MTy))
2690 return LT.first * Entry->Cost;
2691 }
2692
2693 // FIXME: These assume a naive kshift+binop lowering, which is probably
2694 // conservative in most cases.
2695 // FIXME: This doesn't cost large types like v128i1 correctly.
2696 static const CostTblEntry AVX512BoolReduction[] = {
2697 { ISD::AND, MVT::v2i1, 3 },
2698 { ISD::AND, MVT::v4i1, 5 },
2699 { ISD::AND, MVT::v8i1, 7 },
2700 { ISD::AND, MVT::v16i1, 9 },
2701 { ISD::AND, MVT::v32i1, 11 },
2702 { ISD::AND, MVT::v64i1, 13 },
2703 { ISD::OR, MVT::v2i1, 3 },
2704 { ISD::OR, MVT::v4i1, 5 },
2705 { ISD::OR, MVT::v8i1, 7 },
2706 { ISD::OR, MVT::v16i1, 9 },
2707 { ISD::OR, MVT::v32i1, 11 },
2708 { ISD::OR, MVT::v64i1, 13 },
2709 };
2710
2711 static const CostTblEntry AVX2BoolReduction[] = {
2712 { ISD::AND, MVT::v16i16, 2 }, // vpmovmskb + cmp
2713 { ISD::AND, MVT::v32i8, 2 }, // vpmovmskb + cmp
2714 { ISD::OR, MVT::v16i16, 2 }, // vpmovmskb + cmp
2715 { ISD::OR, MVT::v32i8, 2 }, // vpmovmskb + cmp
2716 };
2717
2718 static const CostTblEntry AVX1BoolReduction[] = {
2719 { ISD::AND, MVT::v4i64, 2 }, // vmovmskpd + cmp
2720 { ISD::AND, MVT::v8i32, 2 }, // vmovmskps + cmp
2721 { ISD::AND, MVT::v16i16, 4 }, // vextractf128 + vpand + vpmovmskb + cmp
2722 { ISD::AND, MVT::v32i8, 4 }, // vextractf128 + vpand + vpmovmskb + cmp
2723 { ISD::OR, MVT::v4i64, 2 }, // vmovmskpd + cmp
2724 { ISD::OR, MVT::v8i32, 2 }, // vmovmskps + cmp
2725 { ISD::OR, MVT::v16i16, 4 }, // vextractf128 + vpor + vpmovmskb + cmp
2726 { ISD::OR, MVT::v32i8, 4 }, // vextractf128 + vpor + vpmovmskb + cmp
2727 };
2728
2729 static const CostTblEntry SSE2BoolReduction[] = {
2730 { ISD::AND, MVT::v2i64, 2 }, // movmskpd + cmp
2731 { ISD::AND, MVT::v4i32, 2 }, // movmskps + cmp
2732 { ISD::AND, MVT::v8i16, 2 }, // pmovmskb + cmp
2733 { ISD::AND, MVT::v16i8, 2 }, // pmovmskb + cmp
2734 { ISD::OR, MVT::v2i64, 2 }, // movmskpd + cmp
2735 { ISD::OR, MVT::v4i32, 2 }, // movmskps + cmp
2736 { ISD::OR, MVT::v8i16, 2 }, // pmovmskb + cmp
2737 { ISD::OR, MVT::v16i8, 2 }, // pmovmskb + cmp
2738 };
2739
2740 // Handle bool allof/anyof patterns.
2741 if (!IsPairwise && ValTy->getVectorElementType()->isIntegerTy(1)) {
2742 if (ST->hasAVX512())
2743 if (const auto *Entry = CostTableLookup(AVX512BoolReduction, ISD, MTy))
2744 return LT.first * Entry->Cost;
2745 if (ST->hasAVX2())
2746 if (const auto *Entry = CostTableLookup(AVX2BoolReduction, ISD, MTy))
2747 return LT.first * Entry->Cost;
2748 if (ST->hasAVX())
2749 if (const auto *Entry = CostTableLookup(AVX1BoolReduction, ISD, MTy))
2750 return LT.first * Entry->Cost;
2751 if (ST->hasSSE2())
2752 if (const auto *Entry = CostTableLookup(SSE2BoolReduction, ISD, MTy))
2753 return LT.first * Entry->Cost;
2754 }
2755
2756 return BaseT::getArithmeticReductionCost(Opcode, ValTy, IsPairwise);
2757}
2758
2759int X86TTIImpl::getMinMaxReductionCost(Type *ValTy, Type *CondTy,
2760 bool IsPairwise, bool IsUnsigned) {
2761 std::pair<int, MVT> LT = TLI->getTypeLegalizationCost(DL, ValTy);
2762
2763 MVT MTy = LT.second;
2764
2765 int ISD;
2766 if (ValTy->isIntOrIntVectorTy()) {
2767 ISD = IsUnsigned ? ISD::UMIN : ISD::SMIN;
2768 } else {
2769 assert(ValTy->isFPOrFPVectorTy() &&((ValTy->isFPOrFPVectorTy() && "Expected float point or integer vector type."
) ? static_cast<void> (0) : __assert_fail ("ValTy->isFPOrFPVectorTy() && \"Expected float point or integer vector type.\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Target/X86/X86TargetTransformInfo.cpp"
, 2770, __PRETTY_FUNCTION__))
2770 "Expected float point or integer vector type.")((ValTy->isFPOrFPVectorTy() && "Expected float point or integer vector type."
) ? static_cast<void> (0) : __assert_fail ("ValTy->isFPOrFPVectorTy() && \"Expected float point or integer vector type.\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Target/X86/X86TargetTransformInfo.cpp"
, 2770, __PRETTY_FUNCTION__))
;
2771 ISD = ISD::FMINNUM;
2772 }
2773
2774 // We use the Intel Architecture Code Analyzer(IACA) to measure the throughput
2775 // and make it as the cost.
2776
2777 static const CostTblEntry SSE1CostTblPairWise[] = {
2778 {ISD::FMINNUM, MVT::v4f32, 4},
2779 };
2780
2781 static const CostTblEntry SSE2CostTblPairWise[] = {
2782 {ISD::FMINNUM, MVT::v2f64, 3},
2783 {ISD::SMIN, MVT::v2i64, 6},
2784 {ISD::UMIN, MVT::v2i64, 8},
2785 {ISD::SMIN, MVT::v4i32, 6},
2786 {ISD::UMIN, MVT::v4i32, 8},
2787 {ISD::SMIN, MVT::v8i16, 4},
2788 {ISD::UMIN, MVT::v8i16, 6},
2789 {ISD::SMIN, MVT::v16i8, 8},
2790 {ISD::UMIN, MVT::v16i8, 6},
2791 };
2792
2793 static const CostTblEntry SSE41CostTblPairWise[] = {
2794 {ISD::FMINNUM, MVT::v4f32, 2},
2795 {ISD::SMIN, MVT::v2i64, 9},
2796 {ISD::UMIN, MVT::v2i64,10},
2797 {ISD::SMIN, MVT::v4i32, 1}, // The data reported by the IACA is "1.5"
2798 {ISD::UMIN, MVT::v4i32, 2}, // The data reported by the IACA is "1.8"
2799 {ISD::SMIN, MVT::v8i16, 2},
2800 {ISD::UMIN, MVT::v8i16, 2},
2801 {ISD::SMIN, MVT::v16i8, 3},
2802 {ISD::UMIN, MVT::v16i8, 3},
2803 };
2804
2805 static const CostTblEntry SSE42CostTblPairWise[] = {
2806 {ISD::SMIN, MVT::v2i64, 7}, // The data reported by the IACA is "6.8"
2807 {ISD::UMIN, MVT::v2i64, 8}, // The data reported by the IACA is "8.6"
2808 };
2809
2810 static const CostTblEntry AVX1CostTblPairWise[] = {
2811 {ISD::FMINNUM, MVT::v4f32, 1},
2812 {ISD::FMINNUM, MVT::v4f64, 1},
2813 {ISD::FMINNUM, MVT::v8f32, 2},
2814 {ISD::SMIN, MVT::v2i64, 3},
2815 {ISD::UMIN, MVT::v2i64, 3},
2816 {ISD::SMIN, MVT::v4i32, 1},
2817 {ISD::UMIN, MVT::v4i32, 1},
2818 {ISD::SMIN, MVT::v8i16, 1},
2819 {ISD::UMIN, MVT::v8i16, 1},
2820 {ISD::SMIN, MVT::v16i8, 2},
2821 {ISD::UMIN, MVT::v16i8, 2},
2822 {ISD::SMIN, MVT::v4i64, 7},
2823 {ISD::UMIN, MVT::v4i64, 7},
2824 {ISD::SMIN, MVT::v8i32, 3},
2825 {ISD::UMIN, MVT::v8i32, 3},
2826 {ISD::SMIN, MVT::v16i16, 3},
2827 {ISD::UMIN, MVT::v16i16, 3},
2828 {ISD::SMIN, MVT::v32i8, 3},
2829 {ISD::UMIN, MVT::v32i8, 3},
2830 };
2831
2832 static const CostTblEntry AVX2CostTblPairWise[] = {
2833 {ISD::SMIN, MVT::v4i64, 2},
2834 {ISD::UMIN, MVT::v4i64, 2},
2835 {ISD::SMIN, MVT::v8i32, 1},
2836 {ISD::UMIN, MVT::v8i32, 1},
2837 {ISD::SMIN, MVT::v16i16, 1},
2838 {ISD::UMIN, MVT::v16i16, 1},
2839 {ISD::SMIN, MVT::v32i8, 2},
2840 {ISD::UMIN, MVT::v32i8, 2},
2841 };
2842
2843 static const CostTblEntry AVX512CostTblPairWise[] = {
2844 {ISD::FMINNUM, MVT::v8f64, 1},
2845 {ISD::FMINNUM, MVT::v16f32, 2},
2846 {ISD::SMIN, MVT::v8i64, 2},
2847 {ISD::UMIN, MVT::v8i64, 2},
2848 {ISD::SMIN, MVT::v16i32, 1},
2849 {ISD::UMIN, MVT::v16i32, 1},
2850 };
2851
2852 static const CostTblEntry SSE1CostTblNoPairWise[] = {
2853 {ISD::FMINNUM, MVT::v4f32, 4},
2854 };
2855
2856 static const CostTblEntry SSE2CostTblNoPairWise[] = {
2857 {ISD::FMINNUM, MVT::v2f64, 3},
2858 {ISD::SMIN, MVT::v2i64, 6},
2859 {ISD::UMIN, MVT::v2i64, 8},
2860 {ISD::SMIN, MVT::v4i32, 6},
2861 {ISD::UMIN, MVT::v4i32, 8},
2862 {ISD::SMIN, MVT::v8i16, 4},
2863 {ISD::UMIN, MVT::v8i16, 6},
2864 {ISD::SMIN, MVT::v16i8, 8},
2865 {ISD::UMIN, MVT::v16i8, 6},
2866 };
2867
2868 static const CostTblEntry SSE41CostTblNoPairWise[] = {
2869 {ISD::FMINNUM, MVT::v4f32, 3},
2870 {ISD::SMIN, MVT::v2i64, 9},
2871 {ISD::UMIN, MVT::v2i64,11},
2872 {ISD::SMIN, MVT::v4i32, 1}, // The data reported by the IACA is "1.5"
2873 {ISD::UMIN, MVT::v4i32, 2}, // The data reported by the IACA is "1.8"
2874 {ISD::SMIN, MVT::v8i16, 1}, // The data reported by the IACA is "1.5"
2875 {ISD::UMIN, MVT::v8i16, 2}, // The data reported by the IACA is "1.8"
2876 {ISD::SMIN, MVT::v16i8, 3},
2877 {ISD::UMIN, MVT::v16i8, 3},
2878 };
2879
2880 static const CostTblEntry SSE42CostTblNoPairWise[] = {
2881 {ISD::SMIN, MVT::v2i64, 7}, // The data reported by the IACA is "6.8"
2882 {ISD::UMIN, MVT::v2i64, 9}, // The data reported by the IACA is "8.6"
2883 };
2884
2885 static const CostTblEntry AVX1CostTblNoPairWise[] = {
2886 {ISD::FMINNUM, MVT::v4f32, 1},
2887 {ISD::FMINNUM, MVT::v4f64, 1},
2888 {ISD::FMINNUM, MVT::v8f32, 1},
2889 {ISD::SMIN, MVT::v2i64, 3},
2890 {ISD::UMIN, MVT::v2i64, 3},
2891 {ISD::SMIN, MVT::v4i32, 1},
2892 {ISD::UMIN, MVT::v4i32, 1},
2893 {ISD::SMIN, MVT::v8i16, 1},
2894 {ISD::UMIN, MVT::v8i16, 1},
2895 {ISD::SMIN, MVT::v16i8, 2},
2896 {ISD::UMIN, MVT::v16i8, 2},
2897 {ISD::SMIN, MVT::v4i64, 7},
2898 {ISD::UMIN, MVT::v4i64, 7},
2899 {ISD::SMIN, MVT::v8i32, 2},
2900 {ISD::UMIN, MVT::v8i32, 2},
2901 {ISD::SMIN, MVT::v16i16, 2},
2902 {ISD::UMIN, MVT::v16i16, 2},
2903 {ISD::SMIN, MVT::v32i8, 2},
2904 {ISD::UMIN, MVT::v32i8, 2},
2905 };
2906
2907 static const CostTblEntry AVX2CostTblNoPairWise[] = {
2908 {ISD::SMIN, MVT::v4i64, 1},
2909 {ISD::UMIN, MVT::v4i64, 1},
2910 {ISD::SMIN, MVT::v8i32, 1},
2911 {ISD::UMIN, MVT::v8i32, 1},
2912 {ISD::SMIN, MVT::v16i16, 1},
2913 {ISD::UMIN, MVT::v16i16, 1},
2914 {ISD::SMIN, MVT::v32i8, 1},
2915 {ISD::UMIN, MVT::v32i8, 1},
2916 };
2917
2918 static const CostTblEntry AVX512CostTblNoPairWise[] = {
2919 {ISD::FMINNUM, MVT::v8f64, 1},
2920 {ISD::FMINNUM, MVT::v16f32, 2},
2921 {ISD::SMIN, MVT::v8i64, 1},
2922 {ISD::UMIN, MVT::v8i64, 1},
2923 {ISD::SMIN, MVT::v16i32, 1},
2924 {ISD::UMIN, MVT::v16i32, 1},
2925 };
2926
2927 if (IsPairwise) {
2928 if (ST->hasAVX512())
2929 if (const auto *Entry = CostTableLookup(AVX512CostTblPairWise, ISD, MTy))
2930 return LT.first * Entry->Cost;
2931
2932 if (ST->hasAVX2())
2933 if (const auto *Entry = CostTableLookup(AVX2CostTblPairWise, ISD, MTy))
2934 return LT.first * Entry->Cost;
2935
2936 if (ST->hasAVX())
2937 if (const auto *Entry = CostTableLookup(AVX1CostTblPairWise, ISD, MTy))
2938 return LT.first * Entry->Cost;
2939
2940 if (ST->hasSSE42())
2941 if (const auto *Entry = CostTableLookup(SSE42CostTblPairWise, ISD, MTy))
2942 return LT.first * Entry->Cost;
2943
2944 if (ST->hasSSE41())
2945 if (const auto *Entry = CostTableLookup(SSE41CostTblPairWise, ISD, MTy))
2946 return LT.first * Entry->Cost;
2947
2948 if (ST->hasSSE2())
2949 if (const auto *Entry = CostTableLookup(SSE2CostTblPairWise, ISD, MTy))
2950 return LT.first * Entry->Cost;
2951
2952 if (ST->hasSSE1())
2953 if (const auto *Entry = CostTableLookup(SSE1CostTblPairWise, ISD, MTy))
2954 return LT.first * Entry->Cost;
2955 } else {
2956 if (ST->hasAVX512())
2957 if (const auto *Entry =
2958 CostTableLookup(AVX512CostTblNoPairWise, ISD, MTy))
2959 return LT.first * Entry->Cost;
2960
2961 if (ST->hasAVX2())
2962 if (const auto *Entry = CostTableLookup(AVX2CostTblNoPairWise, ISD, MTy))
2963 return LT.first * Entry->Cost;
2964
2965 if (ST->hasAVX())
2966 if (const auto *Entry = CostTableLookup(AVX1CostTblNoPairWise, ISD, MTy))
2967 return LT.first * Entry->Cost;
2968
2969 if (ST->hasSSE42())
2970 if (const auto *Entry = CostTableLookup(SSE42CostTblNoPairWise, ISD, MTy))
2971 return LT.first * Entry->Cost;
2972
2973 if (ST->hasSSE41())
2974 if (const auto *Entry = CostTableLookup(SSE41CostTblNoPairWise, ISD, MTy))
2975 return LT.first * Entry->Cost;
2976
2977 if (ST->hasSSE2())
2978 if (const auto *Entry = CostTableLookup(SSE2CostTblNoPairWise, ISD, MTy))
2979 return LT.first * Entry->Cost;
2980
2981 if (ST->hasSSE1())
2982 if (const auto *Entry = CostTableLookup(SSE1CostTblNoPairWise, ISD, MTy))
2983 return LT.first * Entry->Cost;
2984 }
2985
2986 return BaseT::getMinMaxReductionCost(ValTy, CondTy, IsPairwise, IsUnsigned);
2987}
2988
2989/// Calculate the cost of materializing a 64-bit value. This helper
2990/// method might only calculate a fraction of a larger immediate. Therefore it
2991/// is valid to return a cost of ZERO.
2992int X86TTIImpl::getIntImmCost(int64_t Val) {
2993 if (Val == 0)
2994 return TTI::TCC_Free;
2995
2996 if (isInt<32>(Val))
2997 return TTI::TCC_Basic;
2998
2999 return 2 * TTI::TCC_Basic;
3000}
3001
3002int X86TTIImpl::getIntImmCost(const APInt &Imm, Type *Ty) {
3003 assert(Ty->isIntegerTy())((Ty->isIntegerTy()) ? static_cast<void> (0) : __assert_fail
("Ty->isIntegerTy()", "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Target/X86/X86TargetTransformInfo.cpp"
, 3003, __PRETTY_FUNCTION__))
;
3004
3005 unsigned BitSize = Ty->getPrimitiveSizeInBits();
3006 if (BitSize == 0)
3007 return ~0U;
3008
3009 // Never hoist constants larger than 128bit, because this might lead to
3010 // incorrect code generation or assertions in codegen.
3011 // Fixme: Create a cost model for types larger than i128 once the codegen
3012 // issues have been fixed.
3013 if (BitSize > 128)
3014 return TTI::TCC_Free;
3015
3016 if (Imm == 0)
3017 return TTI::TCC_Free;
3018
3019 // Sign-extend all constants to a multiple of 64-bit.
3020 APInt ImmVal = Imm;
3021 if (BitSize % 64 != 0)
3022 ImmVal = Imm.sext(alignTo(BitSize, 64));
3023
3024 // Split the constant into 64-bit chunks and calculate the cost for each
3025 // chunk.
3026 int Cost = 0;
3027 for (unsigned ShiftVal = 0; ShiftVal < BitSize; ShiftVal += 64) {
3028 APInt Tmp = ImmVal.ashr(ShiftVal).sextOrTrunc(64);
3029 int64_t Val = Tmp.getSExtValue();
3030 Cost += getIntImmCost(Val);
3031 }
3032 // We need at least one instruction to materialize the constant.
3033 return std::max(1, Cost);
3034}
3035
3036int X86TTIImpl::getIntImmCostInst(unsigned Opcode, unsigned Idx, const APInt &Imm,
3037 Type *Ty) {
3038 assert(Ty->isIntegerTy())((Ty->isIntegerTy()) ? static_cast<void> (0) : __assert_fail
("Ty->isIntegerTy()", "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Target/X86/X86TargetTransformInfo.cpp"
, 3038, __PRETTY_FUNCTION__))
;
3039
3040 unsigned BitSize = Ty->getPrimitiveSizeInBits();
3041 // There is no cost model for constants with a bit size of 0. Return TCC_Free
3042 // here, so that constant hoisting will ignore this constant.
3043 if (BitSize == 0)
3044 return TTI::TCC_Free;
3045
3046 unsigned ImmIdx = ~0U;
3047 switch (Opcode) {
3048 default:
3049 return TTI::TCC_Free;
3050 case Instruction::GetElementPtr:
3051 // Always hoist the base address of a GetElementPtr. This prevents the
3052 // creation of new constants for every base constant that gets constant
3053 // folded with the offset.
3054 if (Idx == 0)
3055 return 2 * TTI::TCC_Basic;
3056 return TTI::TCC_Free;
3057 case Instruction::Store:
3058 ImmIdx = 0;
3059 break;
3060 case Instruction::ICmp:
3061 // This is an imperfect hack to prevent constant hoisting of
3062 // compares that might be trying to check if a 64-bit value fits in
3063 // 32-bits. The backend can optimize these cases using a right shift by 32.
3064 // Ideally we would check the compare predicate here. There also other
3065 // similar immediates the backend can use shifts for.
3066 if (Idx == 1 && Imm.getBitWidth() == 64) {
3067 uint64_t ImmVal = Imm.getZExtValue();
3068 if (ImmVal == 0x100000000ULL || ImmVal == 0xffffffff)
3069 return TTI::TCC_Free;
3070 }
3071 ImmIdx = 1;
3072 break;
3073 case Instruction::And:
3074 // We support 64-bit ANDs with immediates with 32-bits of leading zeroes
3075 // by using a 32-bit operation with implicit zero extension. Detect such
3076 // immediates here as the normal path expects bit 31 to be sign extended.
3077 if (Idx == 1 && Imm.getBitWidth() == 64 && isUInt<32>(Imm.getZExtValue()))
3078 return TTI::TCC_Free;
3079 ImmIdx = 1;
3080 break;
3081 case Instruction::Add:
3082 case Instruction::Sub:
3083 // For add/sub, we can use the opposite instruction for INT32_MIN.
3084 if (Idx == 1 && Imm.getBitWidth() == 64 && Imm.getZExtValue() == 0x80000000)
3085 return TTI::TCC_Free;
3086 ImmIdx = 1;
3087 break;
3088 case Instruction::UDiv:
3089 case Instruction::SDiv:
3090 case Instruction::URem:
3091 case Instruction::SRem:
3092 // Division by constant is typically expanded later into a different
3093 // instruction sequence. This completely changes the constants.
3094 // Report them as "free" to stop ConstantHoist from marking them as opaque.
3095 return TTI::TCC_Free;
3096 case Instruction::Mul:
3097 case Instruction::Or:
3098 case Instruction::Xor:
3099 ImmIdx = 1;
3100 break;
3101 // Always return TCC_Free for the shift value of a shift instruction.
3102 case Instruction::Shl:
3103 case Instruction::LShr:
3104 case Instruction::AShr:
3105 if (Idx == 1)
3106 return TTI::TCC_Free;
3107 break;
3108 case Instruction::Trunc:
3109 case Instruction::ZExt:
3110 case Instruction::SExt:
3111 case Instruction::IntToPtr:
3112 case Instruction::PtrToInt:
3113 case Instruction::BitCast:
3114 case Instruction::PHI:
3115 case Instruction::Call:
3116 case Instruction::Select:
3117 case Instruction::Ret:
3118 case Instruction::Load:
3119 break;
3120 }
3121
3122 if (Idx == ImmIdx) {
3123 int NumConstants = divideCeil(BitSize, 64);
3124 int Cost = X86TTIImpl::getIntImmCost(Imm, Ty);
3125 return (Cost <= NumConstants * TTI::TCC_Basic)
3126 ? static_cast<int>(TTI::TCC_Free)
3127 : Cost;
3128 }
3129
3130 return X86TTIImpl::getIntImmCost(Imm, Ty);
3131}
3132
3133int X86TTIImpl::getIntImmCostIntrin(Intrinsic::ID IID, unsigned Idx,
3134 const APInt &Imm, Type *Ty) {
3135 assert(Ty->isIntegerTy())((Ty->isIntegerTy()) ? static_cast<void> (0) : __assert_fail
("Ty->isIntegerTy()", "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Target/X86/X86TargetTransformInfo.cpp"
, 3135, __PRETTY_FUNCTION__))
;
3136
3137 unsigned BitSize = Ty->getPrimitiveSizeInBits();
3138 // There is no cost model for constants with a bit size of 0. Return TCC_Free
3139 // here, so that constant hoisting will ignore this constant.
3140 if (BitSize == 0)
3141 return TTI::TCC_Free;
3142
3143 switch (IID) {
3144 default:
3145 return TTI::TCC_Free;
3146 case Intrinsic::sadd_with_overflow:
3147 case Intrinsic::uadd_with_overflow:
3148 case Intrinsic::ssub_with_overflow:
3149 case Intrinsic::usub_with_overflow:
3150 case Intrinsic::smul_with_overflow:
3151 case Intrinsic::umul_with_overflow:
3152 if ((Idx == 1) && Imm.getBitWidth() <= 64 && isInt<32>(Imm.getSExtValue()))
3153 return TTI::TCC_Free;
3154 break;
3155 case Intrinsic::experimental_stackmap:
3156 if ((Idx < 2) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
3157 return TTI::TCC_Free;
3158 break;
3159 case Intrinsic::experimental_patchpoint_void:
3160 case Intrinsic::experimental_patchpoint_i64:
3161 if ((Idx < 4) || (Imm.getBitWidth() <= 64 && isInt<64>(Imm.getSExtValue())))
3162 return TTI::TCC_Free;
3163 break;
3164 }
3165 return X86TTIImpl::getIntImmCost(Imm, Ty);
3166}
3167
3168unsigned X86TTIImpl::getUserCost(const User *U,
3169 ArrayRef<const Value *> Operands) {
3170 if (isa<StoreInst>(U)) {
1
Assuming 'U' is not a 'StoreInst'
2
Taking false branch
3171 Value *Ptr = U->getOperand(1);
3172 // Store instruction with index and scale costs 2 Uops.
3173 // Check the preceding GEP to identify non-const indices.
3174 if (auto GEP = dyn_cast<GetElementPtrInst>(Ptr)) {
3175 if (!all_of(GEP->indices(), [](Value *V) { return isa<Constant>(V); }))
3176 return TTI::TCC_Basic * 2;
3177 }
3178 return TTI::TCC_Basic;
3179 }
3180 return BaseT::getUserCost(U, Operands);
3
Calling 'TargetTransformInfoImplCRTPBase::getUserCost'
3181}
3182
3183// Return an average cost of Gather / Scatter instruction, maybe improved later
3184int X86TTIImpl::getGSVectorCost(unsigned Opcode, Type *SrcVTy, Value *Ptr,
3185 unsigned Alignment, unsigned AddressSpace) {
3186
3187 assert(isa<VectorType>(SrcVTy) && "Unexpected type in getGSVectorCost")((isa<VectorType>(SrcVTy) && "Unexpected type in getGSVectorCost"
) ? static_cast<void> (0) : __assert_fail ("isa<VectorType>(SrcVTy) && \"Unexpected type in getGSVectorCost\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Target/X86/X86TargetTransformInfo.cpp"
, 3187, __PRETTY_FUNCTION__))
;
3188 unsigned VF = SrcVTy->getVectorNumElements();
3189
3190 // Try to reduce index size from 64 bit (default for GEP)
3191 // to 32. It is essential for VF 16. If the index can't be reduced to 32, the
3192 // operation will use 16 x 64 indices which do not fit in a zmm and needs
3193 // to split. Also check that the base pointer is the same for all lanes,
3194 // and that there's at most one variable index.
3195 auto getIndexSizeInBits = [](Value *Ptr, const DataLayout& DL) {
3196 unsigned IndexSize = DL.getPointerSizeInBits();
3197 GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr);
3198 if (IndexSize < 64 || !GEP)
3199 return IndexSize;
3200
3201 unsigned NumOfVarIndices = 0;
3202 Value *Ptrs = GEP->getPointerOperand();
3203 if (Ptrs->getType()->isVectorTy() && !getSplatValue(Ptrs))
3204 return IndexSize;
3205 for (unsigned i = 1; i < GEP->getNumOperands(); ++i) {
3206 if (isa<Constant>(GEP->getOperand(i)))
3207 continue;
3208 Type *IndxTy = GEP->getOperand(i)->getType();
3209 if (IndxTy->isVectorTy())
3210 IndxTy = IndxTy->getVectorElementType();
3211 if ((IndxTy->getPrimitiveSizeInBits() == 64 &&
3212 !isa<SExtInst>(GEP->getOperand(i))) ||
3213 ++NumOfVarIndices > 1)
3214 return IndexSize; // 64
3215 }
3216 return (unsigned)32;
3217 };
3218
3219
3220 // Trying to reduce IndexSize to 32 bits for vector 16.
3221 // By default the IndexSize is equal to pointer size.
3222 unsigned IndexSize = (ST->hasAVX512() && VF >= 16)
3223 ? getIndexSizeInBits(Ptr, DL)
3224 : DL.getPointerSizeInBits();
3225
3226 Type *IndexVTy = VectorType::get(IntegerType::get(SrcVTy->getContext(),
3227 IndexSize), VF);
3228 std::pair<int, MVT> IdxsLT = TLI->getTypeLegalizationCost(DL, IndexVTy);
3229 std::pair<int, MVT> SrcLT = TLI->getTypeLegalizationCost(DL, SrcVTy);
3230 int SplitFactor = std::max(IdxsLT.first, SrcLT.first);
3231 if (SplitFactor > 1) {
3232 // Handle splitting of vector of pointers
3233 Type *SplitSrcTy = VectorType::get(SrcVTy->getScalarType(), VF / SplitFactor);
3234 return SplitFactor * getGSVectorCost(Opcode, SplitSrcTy, Ptr, Alignment,
3235 AddressSpace);
3236 }
3237
3238 // The gather / scatter cost is given by Intel architects. It is a rough
3239 // number since we are looking at one instruction in a time.
3240 const int GSOverhead = (Opcode == Instruction::Load)
3241 ? ST->getGatherOverhead()
3242 : ST->getScatterOverhead();
3243 return GSOverhead + VF * getMemoryOpCost(Opcode, SrcVTy->getScalarType(),
3244 MaybeAlign(Alignment), AddressSpace);
3245}
3246
3247/// Return the cost of full scalarization of gather / scatter operation.
3248///
3249/// Opcode - Load or Store instruction.
3250/// SrcVTy - The type of the data vector that should be gathered or scattered.
3251/// VariableMask - The mask is non-constant at compile time.
3252/// Alignment - Alignment for one element.
3253/// AddressSpace - pointer[s] address space.
3254///
3255int X86TTIImpl::getGSScalarCost(unsigned Opcode, Type *SrcVTy,
3256 bool VariableMask, unsigned Alignment,
3257 unsigned AddressSpace) {
3258 unsigned VF = SrcVTy->getVectorNumElements();
3259
3260 int MaskUnpackCost = 0;
3261 if (VariableMask) {
3262 VectorType *MaskTy =
3263 VectorType::get(Type::getInt1Ty(SrcVTy->getContext()), VF);
3264 MaskUnpackCost = getScalarizationOverhead(MaskTy, false, true);
3265 int ScalarCompareCost =
3266 getCmpSelInstrCost(Instruction::ICmp, Type::getInt1Ty(SrcVTy->getContext()),
3267 nullptr);
3268 int BranchCost = getCFInstrCost(Instruction::Br);
3269 MaskUnpackCost += VF * (BranchCost + ScalarCompareCost);
3270 }
3271
3272 // The cost of the scalar loads/stores.
3273 int MemoryOpCost = VF * getMemoryOpCost(Opcode, SrcVTy->getScalarType(),
3274 MaybeAlign(Alignment), AddressSpace);
3275
3276 int InsertExtractCost = 0;
3277 if (Opcode == Instruction::Load)
3278 for (unsigned i = 0; i < VF; ++i)
3279 // Add the cost of inserting each scalar load into the vector
3280 InsertExtractCost +=
3281 getVectorInstrCost(Instruction::InsertElement, SrcVTy, i);
3282 else
3283 for (unsigned i = 0; i < VF; ++i)
3284 // Add the cost of extracting each element out of the data vector
3285 InsertExtractCost +=
3286 getVectorInstrCost(Instruction::ExtractElement, SrcVTy, i);
3287
3288 return MemoryOpCost + MaskUnpackCost + InsertExtractCost;
3289}
3290
3291/// Calculate the cost of Gather / Scatter operation
3292int X86TTIImpl::getGatherScatterOpCost(unsigned Opcode, Type *SrcVTy,
3293 Value *Ptr, bool VariableMask,
3294 unsigned Alignment) {
3295 assert(SrcVTy->isVectorTy() && "Unexpected data type for Gather/Scatter")((SrcVTy->isVectorTy() && "Unexpected data type for Gather/Scatter"
) ? static_cast<void> (0) : __assert_fail ("SrcVTy->isVectorTy() && \"Unexpected data type for Gather/Scatter\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Target/X86/X86TargetTransformInfo.cpp"
, 3295, __PRETTY_FUNCTION__))
;
3296 unsigned VF = SrcVTy->getVectorNumElements();
3297 PointerType *PtrTy = dyn_cast<PointerType>(Ptr->getType());
3298 if (!PtrTy && Ptr->getType()->isVectorTy())
3299 PtrTy = dyn_cast<PointerType>(Ptr->getType()->getVectorElementType());
3300 assert(PtrTy && "Unexpected type for Ptr argument")((PtrTy && "Unexpected type for Ptr argument") ? static_cast
<void> (0) : __assert_fail ("PtrTy && \"Unexpected type for Ptr argument\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Target/X86/X86TargetTransformInfo.cpp"
, 3300, __PRETTY_FUNCTION__))
;
3301 unsigned AddressSpace = PtrTy->getAddressSpace();
3302
3303 bool Scalarize = false;
3304 if ((Opcode == Instruction::Load &&
3305 !isLegalMaskedGather(SrcVTy, MaybeAlign(Alignment))) ||
3306 (Opcode == Instruction::Store &&
3307 !isLegalMaskedScatter(SrcVTy, MaybeAlign(Alignment))))
3308 Scalarize = true;
3309 // Gather / Scatter for vector 2 is not profitable on KNL / SKX
3310 // Vector-4 of gather/scatter instruction does not exist on KNL.
3311 // We can extend it to 8 elements, but zeroing upper bits of
3312 // the mask vector will add more instructions. Right now we give the scalar
3313 // cost of vector-4 for KNL. TODO: Check, maybe the gather/scatter instruction
3314 // is better in the VariableMask case.
3315 if (ST->hasAVX512() && (VF == 2 || (VF == 4 && !ST->hasVLX())))
3316 Scalarize = true;
3317
3318 if (Scalarize)
3319 return getGSScalarCost(Opcode, SrcVTy, VariableMask, Alignment,
3320 AddressSpace);
3321
3322 return getGSVectorCost(Opcode, SrcVTy, Ptr, Alignment, AddressSpace);
3323}
3324
3325bool X86TTIImpl::isLSRCostLess(TargetTransformInfo::LSRCost &C1,
3326 TargetTransformInfo::LSRCost &C2) {
3327 // X86 specific here are "instruction number 1st priority".
3328 return std::tie(C1.Insns, C1.NumRegs, C1.AddRecCost,
3329 C1.NumIVMuls, C1.NumBaseAdds,
3330 C1.ScaleCost, C1.ImmCost, C1.SetupCost) <
3331 std::tie(C2.Insns, C2.NumRegs, C2.AddRecCost,
3332 C2.NumIVMuls, C2.NumBaseAdds,
3333 C2.ScaleCost, C2.ImmCost, C2.SetupCost);
3334}
3335
3336bool X86TTIImpl::canMacroFuseCmp() {
3337 return ST->hasMacroFusion() || ST->hasBranchFusion();
3338}
3339
3340bool X86TTIImpl::isLegalMaskedLoad(Type *DataTy, MaybeAlign Alignment) {
3341 if (!ST->hasAVX())
3342 return false;
3343
3344 // The backend can't handle a single element vector.
3345 if (isa<VectorType>(DataTy) && DataTy->getVectorNumElements() == 1)
3346 return false;
3347 Type *ScalarTy = DataTy->getScalarType();
3348
3349 if (ScalarTy->isPointerTy())
3350 return true;
3351
3352 if (ScalarTy->isFloatTy() || ScalarTy->isDoubleTy())
3353 return true;
3354
3355 if (!ScalarTy->isIntegerTy())
3356 return false;
3357
3358 unsigned IntWidth = ScalarTy->getIntegerBitWidth();
3359 return IntWidth == 32 || IntWidth == 64 ||
3360 ((IntWidth == 8 || IntWidth == 16) && ST->hasBWI());
3361}
3362
3363bool X86TTIImpl::isLegalMaskedStore(Type *DataType, MaybeAlign Alignment) {
3364 return isLegalMaskedLoad(DataType, Alignment);
3365}
3366
3367bool X86TTIImpl::isLegalNTLoad(Type *DataType, Align Alignment) {
3368 unsigned DataSize = DL.getTypeStoreSize(DataType);
3369 // The only supported nontemporal loads are for aligned vectors of 16 or 32
3370 // bytes. Note that 32-byte nontemporal vector loads are supported by AVX2
3371 // (the equivalent stores only require AVX).
3372 if (Alignment >= DataSize && (DataSize == 16 || DataSize == 32))
3373 return DataSize == 16 ? ST->hasSSE1() : ST->hasAVX2();
3374
3375 return false;
3376}
3377
3378bool X86TTIImpl::isLegalNTStore(Type *DataType, Align Alignment) {
3379 unsigned DataSize = DL.getTypeStoreSize(DataType);
3380
3381 // SSE4A supports nontemporal stores of float and double at arbitrary
3382 // alignment.
3383 if (ST->hasSSE4A() && (DataType->isFloatTy() || DataType->isDoubleTy()))
3384 return true;
3385
3386 // Besides the SSE4A subtarget exception above, only aligned stores are
3387 // available nontemporaly on any other subtarget. And only stores with a size
3388 // of 4..32 bytes (powers of 2, only) are permitted.
3389 if (Alignment < DataSize || DataSize < 4 || DataSize > 32 ||
3390 !isPowerOf2_32(DataSize))
3391 return false;
3392
3393 // 32-byte vector nontemporal stores are supported by AVX (the equivalent
3394 // loads require AVX2).
3395 if (DataSize == 32)
3396 return ST->hasAVX();
3397 else if (DataSize == 16)
3398 return ST->hasSSE1();
3399 return true;
3400}
3401
3402bool X86TTIImpl::isLegalMaskedExpandLoad(Type *DataTy) {
3403 if (!isa<VectorType>(DataTy))
3404 return false;
3405
3406 if (!ST->hasAVX512())
3407 return false;
3408
3409 // The backend can't handle a single element vector.
3410 if (DataTy->getVectorNumElements() == 1)
3411 return false;
3412
3413 Type *ScalarTy = DataTy->getVectorElementType();
3414
3415 if (ScalarTy->isFloatTy() || ScalarTy->isDoubleTy())
3416 return true;
3417
3418 if (!ScalarTy->isIntegerTy())
3419 return false;
3420
3421 unsigned IntWidth = ScalarTy->getIntegerBitWidth();
3422 return IntWidth == 32 || IntWidth == 64 ||
3423 ((IntWidth == 8 || IntWidth == 16) && ST->hasVBMI2());
3424}
3425
3426bool X86TTIImpl::isLegalMaskedCompressStore(Type *DataTy) {
3427 return isLegalMaskedExpandLoad(DataTy);
3428}
3429
3430bool X86TTIImpl::isLegalMaskedGather(Type *DataTy, MaybeAlign Alignment) {
3431 // Some CPUs have better gather performance than others.
3432 // TODO: Remove the explicit ST->hasAVX512()?, That would mean we would only
3433 // enable gather with a -march.
3434 if (!(ST->hasAVX512() || (ST->hasFastGather() && ST->hasAVX2())))
3435 return false;
3436
3437 // This function is called now in two cases: from the Loop Vectorizer
3438 // and from the Scalarizer.
3439 // When the Loop Vectorizer asks about legality of the feature,
3440 // the vectorization factor is not calculated yet. The Loop Vectorizer
3441 // sends a scalar type and the decision is based on the width of the
3442 // scalar element.
3443 // Later on, the cost model will estimate usage this intrinsic based on
3444 // the vector type.
3445 // The Scalarizer asks again about legality. It sends a vector type.
3446 // In this case we can reject non-power-of-2 vectors.
3447 // We also reject single element vectors as the type legalizer can't
3448 // scalarize it.
3449 if (isa<VectorType>(DataTy)) {
3450 unsigned NumElts = DataTy->getVectorNumElements();
3451 if (NumElts == 1 || !isPowerOf2_32(NumElts))
3452 return false;
3453 }
3454 Type *ScalarTy = DataTy->getScalarType();
3455 if (ScalarTy->isPointerTy())
3456 return true;
3457
3458 if (ScalarTy->isFloatTy() || ScalarTy->isDoubleTy())
3459 return true;
3460
3461 if (!ScalarTy->isIntegerTy())
3462 return false;
3463
3464 unsigned IntWidth = ScalarTy->getIntegerBitWidth();
3465 return IntWidth == 32 || IntWidth == 64;
3466}
3467
3468bool X86TTIImpl::isLegalMaskedScatter(Type *DataType, MaybeAlign Alignment) {
3469 // AVX2 doesn't support scatter
3470 if (!ST->hasAVX512())
3471 return false;
3472 return isLegalMaskedGather(DataType, Alignment);
3473}
3474
3475bool X86TTIImpl::hasDivRemOp(Type *DataType, bool IsSigned) {
3476 EVT VT = TLI->getValueType(DL, DataType);
3477 return TLI->isOperationLegal(IsSigned ? ISD::SDIVREM : ISD::UDIVREM, VT);
3478}
3479
3480bool X86TTIImpl::isFCmpOrdCheaperThanFCmpZero(Type *Ty) {
3481 return false;
3482}
3483
3484bool X86TTIImpl::areInlineCompatible(const Function *Caller,
3485 const Function *Callee) const {
3486 const TargetMachine &TM = getTLI()->getTargetMachine();
3487
3488 // Work this as a subsetting of subtarget features.
3489 const FeatureBitset &CallerBits =
3490 TM.getSubtargetImpl(*Caller)->getFeatureBits();
3491 const FeatureBitset &CalleeBits =
3492 TM.getSubtargetImpl(*Callee)->getFeatureBits();
3493
3494 FeatureBitset RealCallerBits = CallerBits & ~InlineFeatureIgnoreList;
3495 FeatureBitset RealCalleeBits = CalleeBits & ~InlineFeatureIgnoreList;
3496 return (RealCallerBits & RealCalleeBits) == RealCalleeBits;
3497}
3498
3499bool X86TTIImpl::areFunctionArgsABICompatible(
3500 const Function *Caller, const Function *Callee,
3501 SmallPtrSetImpl<Argument *> &Args) const {
3502 if (!BaseT::areFunctionArgsABICompatible(Caller, Callee, Args))
3503 return false;
3504
3505 // If we get here, we know the target features match. If one function
3506 // considers 512-bit vectors legal and the other does not, consider them
3507 // incompatible.
3508 // FIXME Look at the arguments and only consider 512 bit or larger vectors?
3509 const TargetMachine &TM = getTLI()->getTargetMachine();
3510
3511 return TM.getSubtarget<X86Subtarget>(*Caller).useAVX512Regs() ==
3512 TM.getSubtarget<X86Subtarget>(*Callee).useAVX512Regs();
3513}
3514
3515X86TTIImpl::TTI::MemCmpExpansionOptions
3516X86TTIImpl::enableMemCmpExpansion(bool OptSize, bool IsZeroCmp) const {
3517 TTI::MemCmpExpansionOptions Options;
3518 Options.MaxNumLoads = TLI->getMaxExpandSizeMemcmp(OptSize);
3519 Options.NumLoadsPerBlock = 2;
3520 if (IsZeroCmp) {
3521 // Only enable vector loads for equality comparison. Right now the vector
3522 // version is not as fast for three way compare (see #33329).
3523 const unsigned PreferredWidth = ST->getPreferVectorWidth();
3524 if (PreferredWidth >= 512 && ST->hasAVX512()) Options.LoadSizes.push_back(64);
3525 if (PreferredWidth >= 256 && ST->hasAVX()) Options.LoadSizes.push_back(32);
3526 if (PreferredWidth >= 128 && ST->hasSSE2()) Options.LoadSizes.push_back(16);
3527 // All GPR and vector loads can be unaligned.
3528 Options.AllowOverlappingLoads = true;
3529 }
3530 if (ST->is64Bit()) {
3531 Options.LoadSizes.push_back(8);
3532 }
3533 Options.LoadSizes.push_back(4);
3534 Options.LoadSizes.push_back(2);
3535 Options.LoadSizes.push_back(1);
3536 return Options;
3537}
3538
3539bool X86TTIImpl::enableInterleavedAccessVectorization() {
3540 // TODO: We expect this to be beneficial regardless of arch,
3541 // but there are currently some unexplained performance artifacts on Atom.
3542 // As a temporary solution, disable on Atom.
3543 return !(ST->isAtom());
3544}
3545
3546// Get estimation for interleaved load/store operations for AVX2.
3547// \p Factor is the interleaved-access factor (stride) - number of
3548// (interleaved) elements in the group.
3549// \p Indices contains the indices for a strided load: when the
3550// interleaved load has gaps they indicate which elements are used.
3551// If Indices is empty (or if the number of indices is equal to the size
3552// of the interleaved-access as given in \p Factor) the access has no gaps.
3553//
3554// As opposed to AVX-512, AVX2 does not have generic shuffles that allow
3555// computing the cost using a generic formula as a function of generic
3556// shuffles. We therefore use a lookup table instead, filled according to
3557// the instruction sequences that codegen currently generates.
3558int X86TTIImpl::getInterleavedMemoryOpCostAVX2(unsigned Opcode, Type *VecTy,
3559 unsigned Factor,
3560 ArrayRef<unsigned> Indices,
3561 unsigned Alignment,
3562 unsigned AddressSpace,
3563 bool UseMaskForCond,
3564 bool UseMaskForGaps) {
3565
3566 if (UseMaskForCond || UseMaskForGaps)
3567 return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
3568 Alignment, AddressSpace,
3569 UseMaskForCond, UseMaskForGaps);
3570
3571 // We currently Support only fully-interleaved groups, with no gaps.
3572 // TODO: Support also strided loads (interleaved-groups with gaps).
3573 if (Indices.size() && Indices.size() != Factor)
3574 return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
3575 Alignment, AddressSpace);
3576
3577 // VecTy for interleave memop is <VF*Factor x Elt>.
3578 // So, for VF=4, Interleave Factor = 3, Element type = i32 we have
3579 // VecTy = <12 x i32>.
3580 MVT LegalVT = getTLI()->getTypeLegalizationCost(DL, VecTy).second;
3581
3582 // This function can be called with VecTy=<6xi128>, Factor=3, in which case
3583 // the VF=2, while v2i128 is an unsupported MVT vector type
3584 // (see MachineValueType.h::getVectorVT()).
3585 if (!LegalVT.isVector())
3586 return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
3587 Alignment, AddressSpace);
3588
3589 unsigned VF = VecTy->getVectorNumElements() / Factor;
3590 Type *ScalarTy = VecTy->getVectorElementType();
3591
3592 // Calculate the number of memory operations (NumOfMemOps), required
3593 // for load/store the VecTy.
3594 unsigned VecTySize = DL.getTypeStoreSize(VecTy);
3595 unsigned LegalVTSize = LegalVT.getStoreSize();
3596 unsigned NumOfMemOps = (VecTySize + LegalVTSize - 1) / LegalVTSize;
3597
3598 // Get the cost of one memory operation.
3599 Type *SingleMemOpTy = VectorType::get(VecTy->getVectorElementType(),
3600 LegalVT.getVectorNumElements());
3601 unsigned MemOpCost = getMemoryOpCost(Opcode, SingleMemOpTy,
3602 MaybeAlign(Alignment), AddressSpace);
3603
3604 VectorType *VT = VectorType::get(ScalarTy, VF);
3605 EVT ETy = TLI->getValueType(DL, VT);
3606 if (!ETy.isSimple())
3607 return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
3608 Alignment, AddressSpace);
3609
3610 // TODO: Complete for other data-types and strides.
3611 // Each combination of Stride, ElementTy and VF results in a different
3612 // sequence; The cost tables are therefore accessed with:
3613 // Factor (stride) and VectorType=VFxElemType.
3614 // The Cost accounts only for the shuffle sequence;
3615 // The cost of the loads/stores is accounted for separately.
3616 //
3617 static const CostTblEntry AVX2InterleavedLoadTbl[] = {
3618 { 2, MVT::v4i64, 6 }, //(load 8i64 and) deinterleave into 2 x 4i64
3619 { 2, MVT::v4f64, 6 }, //(load 8f64 and) deinterleave into 2 x 4f64
3620
3621 { 3, MVT::v2i8, 10 }, //(load 6i8 and) deinterleave into 3 x 2i8
3622 { 3, MVT::v4i8, 4 }, //(load 12i8 and) deinterleave into 3 x 4i8
3623 { 3, MVT::v8i8, 9 }, //(load 24i8 and) deinterleave into 3 x 8i8
3624 { 3, MVT::v16i8, 11}, //(load 48i8 and) deinterleave into 3 x 16i8
3625 { 3, MVT::v32i8, 13}, //(load 96i8 and) deinterleave into 3 x 32i8
3626 { 3, MVT::v8f32, 17 }, //(load 24f32 and)deinterleave into 3 x 8f32
3627
3628 { 4, MVT::v2i8, 12 }, //(load 8i8 and) deinterleave into 4 x 2i8
3629 { 4, MVT::v4i8, 4 }, //(load 16i8 and) deinterleave into 4 x 4i8
3630 { 4, MVT::v8i8, 20 }, //(load 32i8 and) deinterleave into 4 x 8i8
3631 { 4, MVT::v16i8, 39 }, //(load 64i8 and) deinterleave into 4 x 16i8
3632 { 4, MVT::v32i8, 80 }, //(load 128i8 and) deinterleave into 4 x 32i8
3633
3634 { 8, MVT::v8f32, 40 } //(load 64f32 and)deinterleave into 8 x 8f32
3635 };
3636
3637 static const CostTblEntry AVX2InterleavedStoreTbl[] = {
3638 { 2, MVT::v4i64, 6 }, //interleave into 2 x 4i64 into 8i64 (and store)
3639 { 2, MVT::v4f64, 6 }, //interleave into 2 x 4f64 into 8f64 (and store)
3640
3641 { 3, MVT::v2i8, 7 }, //interleave 3 x 2i8 into 6i8 (and store)
3642 { 3, MVT::v4i8, 8 }, //interleave 3 x 4i8 into 12i8 (and store)
3643 { 3, MVT::v8i8, 11 }, //interleave 3 x 8i8 into 24i8 (and store)
3644 { 3, MVT::v16i8, 11 }, //interleave 3 x 16i8 into 48i8 (and store)
3645 { 3, MVT::v32i8, 13 }, //interleave 3 x 32i8 into 96i8 (and store)
3646
3647 { 4, MVT::v2i8, 12 }, //interleave 4 x 2i8 into 8i8 (and store)
3648 { 4, MVT::v4i8, 9 }, //interleave 4 x 4i8 into 16i8 (and store)
3649 { 4, MVT::v8i8, 10 }, //interleave 4 x 8i8 into 32i8 (and store)
3650 { 4, MVT::v16i8, 10 }, //interleave 4 x 16i8 into 64i8 (and store)
3651 { 4, MVT::v32i8, 12 } //interleave 4 x 32i8 into 128i8 (and store)
3652 };
3653
3654 if (Opcode == Instruction::Load) {
3655 if (const auto *Entry =
3656 CostTableLookup(AVX2InterleavedLoadTbl, Factor, ETy.getSimpleVT()))
3657 return NumOfMemOps * MemOpCost + Entry->Cost;
3658 } else {
3659 assert(Opcode == Instruction::Store &&((Opcode == Instruction::Store && "Expected Store Instruction at this point"
) ? static_cast<void> (0) : __assert_fail ("Opcode == Instruction::Store && \"Expected Store Instruction at this point\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Target/X86/X86TargetTransformInfo.cpp"
, 3660, __PRETTY_FUNCTION__))
3660 "Expected Store Instruction at this point")((Opcode == Instruction::Store && "Expected Store Instruction at this point"
) ? static_cast<void> (0) : __assert_fail ("Opcode == Instruction::Store && \"Expected Store Instruction at this point\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Target/X86/X86TargetTransformInfo.cpp"
, 3660, __PRETTY_FUNCTION__))
;
3661 if (const auto *Entry =
3662 CostTableLookup(AVX2InterleavedStoreTbl, Factor, ETy.getSimpleVT()))
3663 return NumOfMemOps * MemOpCost + Entry->Cost;
3664 }
3665
3666 return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
3667 Alignment, AddressSpace);
3668}
3669
3670// Get estimation for interleaved load/store operations and strided load.
3671// \p Indices contains indices for strided load.
3672// \p Factor - the factor of interleaving.
3673// AVX-512 provides 3-src shuffles that significantly reduces the cost.
3674int X86TTIImpl::getInterleavedMemoryOpCostAVX512(unsigned Opcode, Type *VecTy,
3675 unsigned Factor,
3676 ArrayRef<unsigned> Indices,
3677 unsigned Alignment,
3678 unsigned AddressSpace,
3679 bool UseMaskForCond,
3680 bool UseMaskForGaps) {
3681
3682 if (UseMaskForCond || UseMaskForGaps)
3683 return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
3684 Alignment, AddressSpace,
3685 UseMaskForCond, UseMaskForGaps);
3686
3687 // VecTy for interleave memop is <VF*Factor x Elt>.
3688 // So, for VF=4, Interleave Factor = 3, Element type = i32 we have
3689 // VecTy = <12 x i32>.
3690
3691 // Calculate the number of memory operations (NumOfMemOps), required
3692 // for load/store the VecTy.
3693 MVT LegalVT = getTLI()->getTypeLegalizationCost(DL, VecTy).second;
3694 unsigned VecTySize = DL.getTypeStoreSize(VecTy);
3695 unsigned LegalVTSize = LegalVT.getStoreSize();
3696 unsigned NumOfMemOps = (VecTySize + LegalVTSize - 1) / LegalVTSize;
3697
3698 // Get the cost of one memory operation.
3699 Type *SingleMemOpTy = VectorType::get(VecTy->getVectorElementType(),
3700 LegalVT.getVectorNumElements());
3701 unsigned MemOpCost = getMemoryOpCost(Opcode, SingleMemOpTy,
3702 MaybeAlign(Alignment), AddressSpace);
3703
3704 unsigned VF = VecTy->getVectorNumElements() / Factor;
3705 MVT VT = MVT::getVectorVT(MVT::getVT(VecTy->getScalarType()), VF);
3706
3707 if (Opcode == Instruction::Load) {
3708 // The tables (AVX512InterleavedLoadTbl and AVX512InterleavedStoreTbl)
3709 // contain the cost of the optimized shuffle sequence that the
3710 // X86InterleavedAccess pass will generate.
3711 // The cost of loads and stores are computed separately from the table.
3712
3713 // X86InterleavedAccess support only the following interleaved-access group.
3714 static const CostTblEntry AVX512InterleavedLoadTbl[] = {
3715 {3, MVT::v16i8, 12}, //(load 48i8 and) deinterleave into 3 x 16i8
3716 {3, MVT::v32i8, 14}, //(load 96i8 and) deinterleave into 3 x 32i8
3717 {3, MVT::v64i8, 22}, //(load 96i8 and) deinterleave into 3 x 32i8
3718 };
3719
3720 if (const auto *Entry =
3721 CostTableLookup(AVX512InterleavedLoadTbl, Factor, VT))
3722 return NumOfMemOps * MemOpCost + Entry->Cost;
3723 //If an entry does not exist, fallback to the default implementation.
3724
3725 // Kind of shuffle depends on number of loaded values.
3726 // If we load the entire data in one register, we can use a 1-src shuffle.
3727 // Otherwise, we'll merge 2 sources in each operation.
3728 TTI::ShuffleKind ShuffleKind =
3729 (NumOfMemOps > 1) ? TTI::SK_PermuteTwoSrc : TTI::SK_PermuteSingleSrc;
3730
3731 unsigned ShuffleCost =
3732 getShuffleCost(ShuffleKind, SingleMemOpTy, 0, nullptr);
3733
3734 unsigned NumOfLoadsInInterleaveGrp =
3735 Indices.size() ? Indices.size() : Factor;
3736 Type *ResultTy = VectorType::get(VecTy->getVectorElementType(),
3737 VecTy->getVectorNumElements() / Factor);
3738 unsigned NumOfResults =
3739 getTLI()->getTypeLegalizationCost(DL, ResultTy).first *
3740 NumOfLoadsInInterleaveGrp;
3741
3742 // About a half of the loads may be folded in shuffles when we have only
3743 // one result. If we have more than one result, we do not fold loads at all.
3744 unsigned NumOfUnfoldedLoads =
3745 NumOfResults > 1 ? NumOfMemOps : NumOfMemOps / 2;
3746
3747 // Get a number of shuffle operations per result.
3748 unsigned NumOfShufflesPerResult =
3749 std::max((unsigned)1, (unsigned)(NumOfMemOps - 1));
3750
3751 // The SK_MergeTwoSrc shuffle clobbers one of src operands.
3752 // When we have more than one destination, we need additional instructions
3753 // to keep sources.
3754 unsigned NumOfMoves = 0;
3755 if (NumOfResults > 1 && ShuffleKind == TTI::SK_PermuteTwoSrc)
3756 NumOfMoves = NumOfResults * NumOfShufflesPerResult / 2;
3757
3758 int Cost = NumOfResults * NumOfShufflesPerResult * ShuffleCost +
3759 NumOfUnfoldedLoads * MemOpCost + NumOfMoves;
3760
3761 return Cost;
3762 }
3763
3764 // Store.
3765 assert(Opcode == Instruction::Store &&((Opcode == Instruction::Store && "Expected Store Instruction at this point"
) ? static_cast<void> (0) : __assert_fail ("Opcode == Instruction::Store && \"Expected Store Instruction at this point\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Target/X86/X86TargetTransformInfo.cpp"
, 3766, __PRETTY_FUNCTION__))
3766 "Expected Store Instruction at this point")((Opcode == Instruction::Store && "Expected Store Instruction at this point"
) ? static_cast<void> (0) : __assert_fail ("Opcode == Instruction::Store && \"Expected Store Instruction at this point\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/lib/Target/X86/X86TargetTransformInfo.cpp"
, 3766, __PRETTY_FUNCTION__))
;
3767 // X86InterleavedAccess support only the following interleaved-access group.
3768 static const CostTblEntry AVX512InterleavedStoreTbl[] = {
3769 {3, MVT::v16i8, 12}, // interleave 3 x 16i8 into 48i8 (and store)
3770 {3, MVT::v32i8, 14}, // interleave 3 x 32i8 into 96i8 (and store)
3771 {3, MVT::v64i8, 26}, // interleave 3 x 64i8 into 96i8 (and store)
3772
3773 {4, MVT::v8i8, 10}, // interleave 4 x 8i8 into 32i8 (and store)
3774 {4, MVT::v16i8, 11}, // interleave 4 x 16i8 into 64i8 (and store)
3775 {4, MVT::v32i8, 14}, // interleave 4 x 32i8 into 128i8 (and store)
3776 {4, MVT::v64i8, 24} // interleave 4 x 32i8 into 256i8 (and store)
3777 };
3778
3779 if (const auto *Entry =
3780 CostTableLookup(AVX512InterleavedStoreTbl, Factor, VT))
3781 return NumOfMemOps * MemOpCost + Entry->Cost;
3782 //If an entry does not exist, fallback to the default implementation.
3783
3784 // There is no strided stores meanwhile. And store can't be folded in
3785 // shuffle.
3786 unsigned NumOfSources = Factor; // The number of values to be merged.
3787 unsigned ShuffleCost =
3788 getShuffleCost(TTI::SK_PermuteTwoSrc, SingleMemOpTy, 0, nullptr);
3789 unsigned NumOfShufflesPerStore = NumOfSources - 1;
3790
3791 // The SK_MergeTwoSrc shuffle clobbers one of src operands.
3792 // We need additional instructions to keep sources.
3793 unsigned NumOfMoves = NumOfMemOps * NumOfShufflesPerStore / 2;
3794 int Cost = NumOfMemOps * (MemOpCost + NumOfShufflesPerStore * ShuffleCost) +
3795 NumOfMoves;
3796 return Cost;
3797}
3798
3799int X86TTIImpl::getInterleavedMemoryOpCost(unsigned Opcode, Type *VecTy,
3800 unsigned Factor,
3801 ArrayRef<unsigned> Indices,
3802 unsigned Alignment,
3803 unsigned AddressSpace,
3804 bool UseMaskForCond,
3805 bool UseMaskForGaps) {
3806 auto isSupportedOnAVX512 = [](Type *VecTy, bool HasBW) {
3807 Type *EltTy = VecTy->getVectorElementType();
3808 if (EltTy->isFloatTy() || EltTy->isDoubleTy() || EltTy->isIntegerTy(64) ||
3809 EltTy->isIntegerTy(32) || EltTy->isPointerTy())
3810 return true;
3811 if (EltTy->isIntegerTy(16) || EltTy->isIntegerTy(8))
3812 return HasBW;
3813 return false;
3814 };
3815 if (ST->hasAVX512() && isSupportedOnAVX512(VecTy, ST->hasBWI()))
3816 return getInterleavedMemoryOpCostAVX512(Opcode, VecTy, Factor, Indices,
3817 Alignment, AddressSpace,
3818 UseMaskForCond, UseMaskForGaps);
3819 if (ST->hasAVX2())
3820 return getInterleavedMemoryOpCostAVX2(Opcode, VecTy, Factor, Indices,
3821 Alignment, AddressSpace,
3822 UseMaskForCond, UseMaskForGaps);
3823
3824 return BaseT::getInterleavedMemoryOpCost(Opcode, VecTy, Factor, Indices,
3825 Alignment, AddressSpace,
3826 UseMaskForCond, UseMaskForGaps);
3827}

/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h

1//===- TargetTransformInfoImpl.h --------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8/// \file
9/// This file provides helpers for the implementation of
10/// a TargetTransformInfo-conforming class.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_ANALYSIS_TARGETTRANSFORMINFOIMPL_H
15#define LLVM_ANALYSIS_TARGETTRANSFORMINFOIMPL_H
16
17#include "llvm/Analysis/ScalarEvolutionExpressions.h"
18#include "llvm/Analysis/TargetTransformInfo.h"
19#include "llvm/Analysis/VectorUtils.h"
20#include "llvm/IR/CallSite.h"
21#include "llvm/IR/DataLayout.h"
22#include "llvm/IR/Function.h"
23#include "llvm/IR/GetElementPtrTypeIterator.h"
24#include "llvm/IR/Operator.h"
25#include "llvm/IR/Type.h"
26
27namespace llvm {
28
29/// Base class for use as a mix-in that aids implementing
30/// a TargetTransformInfo-compatible class.
31class TargetTransformInfoImplBase {
32protected:
33 typedef TargetTransformInfo TTI;
34
35 const DataLayout &DL;
36
37 explicit TargetTransformInfoImplBase(const DataLayout &DL) : DL(DL) {}
38
39public:
40 // Provide value semantics. MSVC requires that we spell all of these out.
41 TargetTransformInfoImplBase(const TargetTransformInfoImplBase &Arg)
42 : DL(Arg.DL) {}
43 TargetTransformInfoImplBase(TargetTransformInfoImplBase &&Arg) : DL(Arg.DL) {}
44
45 const DataLayout &getDataLayout() const { return DL; }
46
47 unsigned getOperationCost(unsigned Opcode, Type *Ty, Type *OpTy) {
48 switch (Opcode) {
49 default:
50 // By default, just classify everything as 'basic'.
51 return TTI::TCC_Basic;
52
53 case Instruction::GetElementPtr:
54 llvm_unreachable("Use getGEPCost for GEP operations!")::llvm::llvm_unreachable_internal("Use getGEPCost for GEP operations!"
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h"
, 54)
;
55
56 case Instruction::BitCast:
57 assert(OpTy && "Cast instructions must provide the operand type")((OpTy && "Cast instructions must provide the operand type"
) ? static_cast<void> (0) : __assert_fail ("OpTy && \"Cast instructions must provide the operand type\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h"
, 57, __PRETTY_FUNCTION__))
;
58 if (Ty == OpTy || (Ty->isPointerTy() && OpTy->isPointerTy()))
59 // Identity and pointer-to-pointer casts are free.
60 return TTI::TCC_Free;
61
62 // Otherwise, the default basic cost is used.
63 return TTI::TCC_Basic;
64
65 case Instruction::FDiv:
66 case Instruction::FRem:
67 case Instruction::SDiv:
68 case Instruction::SRem:
69 case Instruction::UDiv:
70 case Instruction::URem:
71 return TTI::TCC_Expensive;
72
73 case Instruction::IntToPtr: {
74 // An inttoptr cast is free so long as the input is a legal integer type
75 // which doesn't contain values outside the range of a pointer.
76 unsigned OpSize = OpTy->getScalarSizeInBits();
77 if (DL.isLegalInteger(OpSize) &&
78 OpSize <= DL.getPointerTypeSizeInBits(Ty))
79 return TTI::TCC_Free;
80
81 // Otherwise it's not a no-op.
82 return TTI::TCC_Basic;
83 }
84 case Instruction::PtrToInt: {
85 // A ptrtoint cast is free so long as the result is large enough to store
86 // the pointer, and a legal integer type.
87 unsigned DestSize = Ty->getScalarSizeInBits();
88 if (DL.isLegalInteger(DestSize) &&
89 DestSize >= DL.getPointerTypeSizeInBits(OpTy))
90 return TTI::TCC_Free;
91
92 // Otherwise it's not a no-op.
93 return TTI::TCC_Basic;
94 }
95 case Instruction::Trunc:
96 // trunc to a native type is free (assuming the target has compare and
97 // shift-right of the same width).
98 if (DL.isLegalInteger(DL.getTypeSizeInBits(Ty)))
99 return TTI::TCC_Free;
100
101 return TTI::TCC_Basic;
102 }
103 }
104
105 int getGEPCost(Type *PointeeType, const Value *Ptr,
106 ArrayRef<const Value *> Operands) {
107 // In the basic model, we just assume that all-constant GEPs will be folded
108 // into their uses via addressing modes.
109 for (unsigned Idx = 0, Size = Operands.size(); Idx != Size; ++Idx)
110 if (!isa<Constant>(Operands[Idx]))
111 return TTI::TCC_Basic;
112
113 return TTI::TCC_Free;
114 }
115
116 unsigned getEstimatedNumberOfCaseClusters(const SwitchInst &SI,
117 unsigned &JTSize,
118 ProfileSummaryInfo *PSI,
119 BlockFrequencyInfo *BFI) {
120 (void)PSI;
121 (void)BFI;
122 JTSize = 0;
123 return SI.getNumCases();
124 }
125
126 int getExtCost(const Instruction *I, const Value *Src) {
127 return TTI::TCC_Basic;
128 }
129
130 unsigned getCallCost(FunctionType *FTy, int NumArgs, const User *U) {
131 assert(FTy && "FunctionType must be provided to this routine.")((FTy && "FunctionType must be provided to this routine."
) ? static_cast<void> (0) : __assert_fail ("FTy && \"FunctionType must be provided to this routine.\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h"
, 131, __PRETTY_FUNCTION__))
;
132
133 // The target-independent implementation just measures the size of the
134 // function by approximating that each argument will take on average one
135 // instruction to prepare.
136
137 if (NumArgs < 0)
138 // Set the argument number to the number of explicit arguments in the
139 // function.
140 NumArgs = FTy->getNumParams();
141
142 return TTI::TCC_Basic * (NumArgs + 1);
143 }
144
145 unsigned getInliningThresholdMultiplier() { return 1; }
146
147 int getInlinerVectorBonusPercent() { return 150; }
148
149 unsigned getMemcpyCost(const Instruction *I) {
150 return TTI::TCC_Expensive;
151 }
152
153 bool hasBranchDivergence() { return false; }
154
155 bool isSourceOfDivergence(const Value *V) { return false; }
156
157 bool isAlwaysUniform(const Value *V) { return false; }
158
159 unsigned getFlatAddressSpace () {
160 return -1;
161 }
162
163 bool collectFlatAddressOperands(SmallVectorImpl<int> &OpIndexes,
164 Intrinsic::ID IID) const {
165 return false;
166 }
167
168 bool rewriteIntrinsicWithAddressSpace(IntrinsicInst *II,
169 Value *OldV, Value *NewV) const {
170 return false;
171 }
172
173 bool isLoweredToCall(const Function *F) {
174 assert(F && "A concrete function must be provided to this routine.")((F && "A concrete function must be provided to this routine."
) ? static_cast<void> (0) : __assert_fail ("F && \"A concrete function must be provided to this routine.\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h"
, 174, __PRETTY_FUNCTION__))
;
175
176 // FIXME: These should almost certainly not be handled here, and instead
177 // handled with the help of TLI or the target itself. This was largely
178 // ported from existing analysis heuristics here so that such refactorings
179 // can take place in the future.
180
181 if (F->isIntrinsic())
182 return false;
183
184 if (F->hasLocalLinkage() || !F->hasName())
185 return true;
186
187 StringRef Name = F->getName();
188
189 // These will all likely lower to a single selection DAG node.
190 if (Name == "copysign" || Name == "copysignf" || Name == "copysignl" ||
191 Name == "fabs" || Name == "fabsf" || Name == "fabsl" || Name == "sin" ||
192 Name == "fmin" || Name == "fminf" || Name == "fminl" ||
193 Name == "fmax" || Name == "fmaxf" || Name == "fmaxl" ||
194 Name == "sinf" || Name == "sinl" || Name == "cos" || Name == "cosf" ||
195 Name == "cosl" || Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl")
196 return false;
197
198 // These are all likely to be optimized into something smaller.
199 if (Name == "pow" || Name == "powf" || Name == "powl" || Name == "exp2" ||
200 Name == "exp2l" || Name == "exp2f" || Name == "floor" ||
201 Name == "floorf" || Name == "ceil" || Name == "round" ||
202 Name == "ffs" || Name == "ffsl" || Name == "abs" || Name == "labs" ||
203 Name == "llabs")
204 return false;
205
206 return true;
207 }
208
209 bool isHardwareLoopProfitable(Loop *L, ScalarEvolution &SE,
210 AssumptionCache &AC,
211 TargetLibraryInfo *LibInfo,
212 HardwareLoopInfo &HWLoopInfo) {
213 return false;
214 }
215
216 bool preferPredicateOverEpilogue(Loop *L, LoopInfo *LI, ScalarEvolution &SE,
217 AssumptionCache &AC, TargetLibraryInfo *TLI,
218 DominatorTree *DT,
219 const LoopAccessInfo *LAI) const {
220 return false;
221 }
222
223 void getUnrollingPreferences(Loop *, ScalarEvolution &,
224 TTI::UnrollingPreferences &) {}
225
226 bool isLegalAddImmediate(int64_t Imm) { return false; }
227
228 bool isLegalICmpImmediate(int64_t Imm) { return false; }
229
230 bool isLegalAddressingMode(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset,
231 bool HasBaseReg, int64_t Scale,
232 unsigned AddrSpace, Instruction *I = nullptr) {
233 // Guess that only reg and reg+reg addressing is allowed. This heuristic is
234 // taken from the implementation of LSR.
235 return !BaseGV && BaseOffset == 0 && (Scale == 0 || Scale == 1);
236 }
237
238 bool isLSRCostLess(TTI::LSRCost &C1, TTI::LSRCost &C2) {
239 return std::tie(C1.NumRegs, C1.AddRecCost, C1.NumIVMuls, C1.NumBaseAdds,
240 C1.ScaleCost, C1.ImmCost, C1.SetupCost) <
241 std::tie(C2.NumRegs, C2.AddRecCost, C2.NumIVMuls, C2.NumBaseAdds,
242 C2.ScaleCost, C2.ImmCost, C2.SetupCost);
243 }
244
245 bool canMacroFuseCmp() { return false; }
246
247 bool canSaveCmp(Loop *L, BranchInst **BI, ScalarEvolution *SE, LoopInfo *LI,
248 DominatorTree *DT, AssumptionCache *AC,
249 TargetLibraryInfo *LibInfo) {
250 return false;
251 }
252
253 bool shouldFavorPostInc() const { return false; }
254
255 bool shouldFavorBackedgeIndex(const Loop *L) const { return false; }
256
257 bool isLegalMaskedStore(Type *DataType, MaybeAlign Alignment) { return false; }
258
259 bool isLegalMaskedLoad(Type *DataType, MaybeAlign Alignment) { return false; }
260
261 bool isLegalNTStore(Type *DataType, Align Alignment) {
262 // By default, assume nontemporal memory stores are available for stores
263 // that are aligned and have a size that is a power of 2.
264 unsigned DataSize = DL.getTypeStoreSize(DataType);
265 return Alignment >= DataSize && isPowerOf2_32(DataSize);
266 }
267
268 bool isLegalNTLoad(Type *DataType, Align Alignment) {
269 // By default, assume nontemporal memory loads are available for loads that
270 // are aligned and have a size that is a power of 2.
271 unsigned DataSize = DL.getTypeStoreSize(DataType);
272 return Alignment >= DataSize && isPowerOf2_32(DataSize);
273 }
274
275 bool isLegalMaskedScatter(Type *DataType, MaybeAlign Alignment) {
276 return false;
277 }
278
279 bool isLegalMaskedGather(Type *DataType, MaybeAlign Alignment) {
280 return false;
281 }
282
283 bool isLegalMaskedCompressStore(Type *DataType) { return false; }
284
285 bool isLegalMaskedExpandLoad(Type *DataType) { return false; }
286
287 bool hasDivRemOp(Type *DataType, bool IsSigned) { return false; }
288
289 bool hasVolatileVariant(Instruction *I, unsigned AddrSpace) { return false; }
290
291 bool prefersVectorizedAddressing() { return true; }
292
293 int getScalingFactorCost(Type *Ty, GlobalValue *BaseGV, int64_t BaseOffset,
294 bool HasBaseReg, int64_t Scale, unsigned AddrSpace) {
295 // Guess that all legal addressing mode are free.
296 if (isLegalAddressingMode(Ty, BaseGV, BaseOffset, HasBaseReg,
297 Scale, AddrSpace))
298 return 0;
299 return -1;
300 }
301
302 bool LSRWithInstrQueries() { return false; }
303
304 bool isTruncateFree(Type *Ty1, Type *Ty2) { return false; }
305
306 bool isProfitableToHoist(Instruction *I) { return true; }
307
308 bool useAA() { return false; }
309
310 bool isTypeLegal(Type *Ty) { return false; }
311
312 bool shouldBuildLookupTables() { return true; }
313 bool shouldBuildLookupTablesForConstant(Constant *C) { return true; }
314
315 bool useColdCCForColdCall(Function &F) { return false; }
316
317 unsigned getScalarizationOverhead(Type *Ty, bool Insert, bool Extract) {
318 return 0;
319 }
320
321 unsigned getOperandsScalarizationOverhead(ArrayRef<const Value *> Args,
322 unsigned VF) { return 0; }
323
324 bool supportsEfficientVectorElementLoadStore() { return false; }
325
326 bool enableAggressiveInterleaving(bool LoopHasReductions) { return false; }
327
328 TTI::MemCmpExpansionOptions enableMemCmpExpansion(bool OptSize,
329 bool IsZeroCmp) const {
330 return {};
331 }
332
333 bool enableInterleavedAccessVectorization() { return false; }
334
335 bool enableMaskedInterleavedAccessVectorization() { return false; }
336
337 bool isFPVectorizationPotentiallyUnsafe() { return false; }
338
339 bool allowsMisalignedMemoryAccesses(LLVMContext &Context,
340 unsigned BitWidth,
341 unsigned AddressSpace,
342 unsigned Alignment,
343 bool *Fast) { return false; }
344
345 TTI::PopcntSupportKind getPopcntSupport(unsigned IntTyWidthInBit) {
346 return TTI::PSK_Software;
347 }
348
349 bool haveFastSqrt(Type *Ty) { return false; }
350
351 bool isFCmpOrdCheaperThanFCmpZero(Type *Ty) { return true; }
352
353 unsigned getFPOpCost(Type *Ty) { return TargetTransformInfo::TCC_Basic; }
354
355 int getIntImmCodeSizeCost(unsigned Opcode, unsigned Idx, const APInt &Imm,
356 Type *Ty) {
357 return 0;
358 }
359
360 unsigned getIntImmCost(const APInt &Imm, Type *Ty) { return TTI::TCC_Basic; }
361
362 unsigned getIntImmCostInst(unsigned Opcode, unsigned Idx, const APInt &Imm,
363 Type *Ty) {
364 return TTI::TCC_Free;
365 }
366
367 unsigned getIntImmCostIntrin(Intrinsic::ID IID, unsigned Idx,
368 const APInt &Imm, Type *Ty) {
369 return TTI::TCC_Free;
370 }
371
372 unsigned getNumberOfRegisters(unsigned ClassID) const { return 8; }
373
374 unsigned getRegisterClassForType(bool Vector, Type *Ty = nullptr) const {
375 return Vector ? 1 : 0;
376 };
377
378 const char* getRegisterClassName(unsigned ClassID) const {
379 switch (ClassID) {
380 default:
381 return "Generic::Unknown Register Class";
382 case 0: return "Generic::ScalarRC";
383 case 1: return "Generic::VectorRC";
384 }
385 }
386
387 unsigned getRegisterBitWidth(bool Vector) const { return 32; }
388
389 unsigned getMinVectorRegisterBitWidth() { return 128; }
390
391 bool shouldMaximizeVectorBandwidth(bool OptSize) const { return false; }
392
393 unsigned getMinimumVF(unsigned ElemWidth) const { return 0; }
394
395 bool
396 shouldConsiderAddressTypePromotion(const Instruction &I,
397 bool &AllowPromotionWithoutCommonHeader) {
398 AllowPromotionWithoutCommonHeader = false;
399 return false;
400 }
401
402 unsigned getCacheLineSize() const { return 0; }
403
404 llvm::Optional<unsigned> getCacheSize(TargetTransformInfo::CacheLevel Level) const {
405 switch (Level) {
406 case TargetTransformInfo::CacheLevel::L1D:
407 LLVM_FALLTHROUGH[[gnu::fallthrough]];
408 case TargetTransformInfo::CacheLevel::L2D:
409 return llvm::Optional<unsigned>();
410 }
411 llvm_unreachable("Unknown TargetTransformInfo::CacheLevel")::llvm::llvm_unreachable_internal("Unknown TargetTransformInfo::CacheLevel"
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h"
, 411)
;
412 }
413
414 llvm::Optional<unsigned> getCacheAssociativity(
415 TargetTransformInfo::CacheLevel Level) const {
416 switch (Level) {
417 case TargetTransformInfo::CacheLevel::L1D:
418 LLVM_FALLTHROUGH[[gnu::fallthrough]];
419 case TargetTransformInfo::CacheLevel::L2D:
420 return llvm::Optional<unsigned>();
421 }
422
423 llvm_unreachable("Unknown TargetTransformInfo::CacheLevel")::llvm::llvm_unreachable_internal("Unknown TargetTransformInfo::CacheLevel"
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h"
, 423)
;
424 }
425
426 unsigned getPrefetchDistance() const { return 0; }
427 unsigned getMinPrefetchStride() const { return 1; }
428 unsigned getMaxPrefetchIterationsAhead() const { return UINT_MAX(2147483647 *2U +1U); }
429
430 unsigned getMaxInterleaveFactor(unsigned VF) { return 1; }
431
432 unsigned getArithmeticInstrCost(unsigned Opcode, Type *Ty,
433 TTI::OperandValueKind Opd1Info,
434 TTI::OperandValueKind Opd2Info,
435 TTI::OperandValueProperties Opd1PropInfo,
436 TTI::OperandValueProperties Opd2PropInfo,
437 ArrayRef<const Value *> Args,
438 const Instruction *CxtI = nullptr) {
439 return 1;
440 }
441
442 unsigned getShuffleCost(TTI::ShuffleKind Kind, Type *Ty, int Index,
443 Type *SubTp) {
444 return 1;
445 }
446
447 unsigned getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src,
448 const Instruction *I) { return 1; }
449
450 unsigned getExtractWithExtendCost(unsigned Opcode, Type *Dst,
451 VectorType *VecTy, unsigned Index) {
452 return 1;
453 }
454
455 unsigned getCFInstrCost(unsigned Opcode) { return 1; }
456
457 unsigned getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy,
458 const Instruction *I) {
459 return 1;
460 }
461
462 unsigned getVectorInstrCost(unsigned Opcode, Type *Val, unsigned Index) {
463 return 1;
464 }
465
466 unsigned getMemoryOpCost(unsigned Opcode, Type *Src, MaybeAlign Alignment,
467 unsigned AddressSpace, const Instruction *I) {
468 return 1;
469 }
470
471 unsigned getMaskedMemoryOpCost(unsigned Opcode, Type *Src, unsigned Alignment,
472 unsigned AddressSpace) {
473 return 1;
474 }
475
476 unsigned getGatherScatterOpCost(unsigned Opcode, Type *DataTy, Value *Ptr,
477 bool VariableMask,
478 unsigned Alignment) {
479 return 1;
480 }
481
482 unsigned getInterleavedMemoryOpCost(unsigned Opcode, Type *VecTy,
483 unsigned Factor,
484 ArrayRef<unsigned> Indices,
485 unsigned Alignment, unsigned AddressSpace,
486 bool UseMaskForCond = false,
487 bool UseMaskForGaps = false) {
488 return 1;
489 }
490
491 unsigned getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
492 ArrayRef<Type *> Tys, FastMathFlags FMF,
493 unsigned ScalarizationCostPassed) {
494 return 1;
495 }
496 unsigned getIntrinsicInstrCost(Intrinsic::ID ID, Type *RetTy,
497 ArrayRef<Value *> Args, FastMathFlags FMF, unsigned VF) {
498 return 1;
499 }
500
501 unsigned getCallInstrCost(Function *F, Type *RetTy, ArrayRef<Type *> Tys) {
502 return 1;
503 }
504
505 unsigned getNumberOfParts(Type *Tp) { return 0; }
506
507 unsigned getAddressComputationCost(Type *Tp, ScalarEvolution *,
508 const SCEV *) {
509 return 0;
510 }
511
512 unsigned getArithmeticReductionCost(unsigned, Type *, bool) { return 1; }
513
514 unsigned getMinMaxReductionCost(Type *, Type *, bool, bool) { return 1; }
515
516 unsigned getCostOfKeepingLiveOverCall(ArrayRef<Type *> Tys) { return 0; }
517
518 bool getTgtMemIntrinsic(IntrinsicInst *Inst, MemIntrinsicInfo &Info) {
519 return false;
520 }
521
522 unsigned getAtomicMemIntrinsicMaxElementSize() const {
523 // Note for overrides: You must ensure for all element unordered-atomic
524 // memory intrinsics that all power-of-2 element sizes up to, and
525 // including, the return value of this method have a corresponding
526 // runtime lib call. These runtime lib call definitions can be found
527 // in RuntimeLibcalls.h
528 return 0;
529 }
530
531 Value *getOrCreateResultFromMemIntrinsic(IntrinsicInst *Inst,
532 Type *ExpectedType) {
533 return nullptr;
534 }
535
536 Type *getMemcpyLoopLoweringType(LLVMContext &Context, Value *Length,
537 unsigned SrcAlign, unsigned DestAlign) const {
538 return Type::getInt8Ty(Context);
539 }
540
541 void getMemcpyLoopResidualLoweringType(SmallVectorImpl<Type *> &OpsOut,
542 LLVMContext &Context,
543 unsigned RemainingBytes,
544 unsigned SrcAlign,
545 unsigned DestAlign) const {
546 for (unsigned i = 0; i != RemainingBytes; ++i)
547 OpsOut.push_back(Type::getInt8Ty(Context));
548 }
549
550 bool areInlineCompatible(const Function *Caller,
551 const Function *Callee) const {
552 return (Caller->getFnAttribute("target-cpu") ==
553 Callee->getFnAttribute("target-cpu")) &&
554 (Caller->getFnAttribute("target-features") ==
555 Callee->getFnAttribute("target-features"));
556 }
557
558 bool areFunctionArgsABICompatible(const Function *Caller, const Function *Callee,
559 SmallPtrSetImpl<Argument *> &Args) const {
560 return (Caller->getFnAttribute("target-cpu") ==
561 Callee->getFnAttribute("target-cpu")) &&
562 (Caller->getFnAttribute("target-features") ==
563 Callee->getFnAttribute("target-features"));
564 }
565
566 bool isIndexedLoadLegal(TTI::MemIndexedMode Mode, Type *Ty,
567 const DataLayout &DL) const {
568 return false;
569 }
570
571 bool isIndexedStoreLegal(TTI::MemIndexedMode Mode, Type *Ty,
572 const DataLayout &DL) const {
573 return false;
574 }
575
576 unsigned getLoadStoreVecRegBitWidth(unsigned AddrSpace) const { return 128; }
577
578 bool isLegalToVectorizeLoad(LoadInst *LI) const { return true; }
579
580 bool isLegalToVectorizeStore(StoreInst *SI) const { return true; }
581
582 bool isLegalToVectorizeLoadChain(unsigned ChainSizeInBytes,
583 unsigned Alignment,
584 unsigned AddrSpace) const {
585 return true;
586 }
587
588 bool isLegalToVectorizeStoreChain(unsigned ChainSizeInBytes,
589 unsigned Alignment,
590 unsigned AddrSpace) const {
591 return true;
592 }
593
594 unsigned getLoadVectorFactor(unsigned VF, unsigned LoadSize,
595 unsigned ChainSizeInBytes,
596 VectorType *VecTy) const {
597 return VF;
598 }
599
600 unsigned getStoreVectorFactor(unsigned VF, unsigned StoreSize,
601 unsigned ChainSizeInBytes,
602 VectorType *VecTy) const {
603 return VF;
604 }
605
606 bool useReductionIntrinsic(unsigned Opcode, Type *Ty,
607 TTI::ReductionFlags Flags) const {
608 return false;
609 }
610
611 bool shouldExpandReduction(const IntrinsicInst *II) const {
612 return true;
613 }
614
615 unsigned getGISelRematGlobalCost() const {
616 return 1;
617 }
618
619protected:
620 // Obtain the minimum required size to hold the value (without the sign)
621 // In case of a vector it returns the min required size for one element.
622 unsigned minRequiredElementSize(const Value* Val, bool &isSigned) {
623 if (isa<ConstantDataVector>(Val) || isa<ConstantVector>(Val)) {
624 const auto* VectorValue = cast<Constant>(Val);
625
626 // In case of a vector need to pick the max between the min
627 // required size for each element
628 auto *VT = cast<VectorType>(Val->getType());
629
630 // Assume unsigned elements
631 isSigned = false;
632
633 // The max required size is the total vector width divided by num
634 // of elements in the vector
635 unsigned MaxRequiredSize = VT->getBitWidth() / VT->getNumElements();
636
637 unsigned MinRequiredSize = 0;
638 for(unsigned i = 0, e = VT->getNumElements(); i < e; ++i) {
639 if (auto* IntElement =
640 dyn_cast<ConstantInt>(VectorValue->getAggregateElement(i))) {
641 bool signedElement = IntElement->getValue().isNegative();
642 // Get the element min required size.
643 unsigned ElementMinRequiredSize =
644 IntElement->getValue().getMinSignedBits() - 1;
645 // In case one element is signed then all the vector is signed.
646 isSigned |= signedElement;
647 // Save the max required bit size between all the elements.
648 MinRequiredSize = std::max(MinRequiredSize, ElementMinRequiredSize);
649 }
650 else {
651 // not an int constant element
652 return MaxRequiredSize;
653 }
654 }
655 return MinRequiredSize;
656 }
657
658 if (const auto* CI = dyn_cast<ConstantInt>(Val)) {
659 isSigned = CI->getValue().isNegative();
660 return CI->getValue().getMinSignedBits() - 1;
661 }
662
663 if (const auto* Cast = dyn_cast<SExtInst>(Val)) {
664 isSigned = true;
665 return Cast->getSrcTy()->getScalarSizeInBits() - 1;
666 }
667
668 if (const auto* Cast = dyn_cast<ZExtInst>(Val)) {
669 isSigned = false;
670 return Cast->getSrcTy()->getScalarSizeInBits();
671 }
672
673 isSigned = false;
674 return Val->getType()->getScalarSizeInBits();
675 }
676
677 bool isStridedAccess(const SCEV *Ptr) {
678 return Ptr && isa<SCEVAddRecExpr>(Ptr);
679 }
680
681 const SCEVConstant *getConstantStrideStep(ScalarEvolution *SE,
682 const SCEV *Ptr) {
683 if (!isStridedAccess(Ptr))
684 return nullptr;
685 const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ptr);
686 return dyn_cast<SCEVConstant>(AddRec->getStepRecurrence(*SE));
687 }
688
689 bool isConstantStridedAccessLessThan(ScalarEvolution *SE, const SCEV *Ptr,
690 int64_t MergeDistance) {
691 const SCEVConstant *Step = getConstantStrideStep(SE, Ptr);
692 if (!Step)
693 return false;
694 APInt StrideVal = Step->getAPInt();
695 if (StrideVal.getBitWidth() > 64)
696 return false;
697 // FIXME: Need to take absolute value for negative stride case.
698 return StrideVal.getSExtValue() < MergeDistance;
699 }
700};
701
702/// CRTP base class for use as a mix-in that aids implementing
703/// a TargetTransformInfo-compatible class.
704template <typename T>
705class TargetTransformInfoImplCRTPBase : public TargetTransformInfoImplBase {
706private:
707 typedef TargetTransformInfoImplBase BaseT;
708
709protected:
710 explicit TargetTransformInfoImplCRTPBase(const DataLayout &DL) : BaseT(DL) {}
711
712public:
713 using BaseT::getCallCost;
714
715 unsigned getCallCost(const Function *F, int NumArgs, const User *U) {
716 assert(F && "A concrete function must be provided to this routine.")((F && "A concrete function must be provided to this routine."
) ? static_cast<void> (0) : __assert_fail ("F && \"A concrete function must be provided to this routine.\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h"
, 716, __PRETTY_FUNCTION__))
;
717
718 if (NumArgs < 0)
719 // Set the argument number to the number of explicit arguments in the
720 // function.
721 NumArgs = F->arg_size();
722
723 if (Intrinsic::ID IID = F->getIntrinsicID()) {
724 FunctionType *FTy = F->getFunctionType();
725 SmallVector<Type *, 8> ParamTys(FTy->param_begin(), FTy->param_end());
726 return static_cast<T *>(this)
727 ->getIntrinsicCost(IID, FTy->getReturnType(), ParamTys, U);
728 }
729
730 if (!static_cast<T *>(this)->isLoweredToCall(F))
731 return TTI::TCC_Basic; // Give a basic cost if it will be lowered
732 // directly.
733
734 return static_cast<T *>(this)->getCallCost(F->getFunctionType(), NumArgs, U);
735 }
736
737 unsigned getCallCost(const Function *F, ArrayRef<const Value *> Arguments,
738 const User *U) {
739 // Simply delegate to generic handling of the call.
740 // FIXME: We should use instsimplify or something else to catch calls which
741 // will constant fold with these arguments.
742 return static_cast<T *>(this)->getCallCost(F, Arguments.size(), U);
743 }
744
745 using BaseT::getGEPCost;
746
747 int getGEPCost(Type *PointeeType, const Value *Ptr,
748 ArrayRef<const Value *> Operands) {
749 assert(PointeeType && Ptr && "can't get GEPCost of nullptr")((PointeeType && Ptr && "can't get GEPCost of nullptr"
) ? static_cast<void> (0) : __assert_fail ("PointeeType && Ptr && \"can't get GEPCost of nullptr\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h"
, 749, __PRETTY_FUNCTION__))
;
750 // TODO: will remove this when pointers have an opaque type.
751 assert(Ptr->getType()->getScalarType()->getPointerElementType() ==((Ptr->getType()->getScalarType()->getPointerElementType
() == PointeeType && "explicit pointee type doesn't match operand's pointee type"
) ? static_cast<void> (0) : __assert_fail ("Ptr->getType()->getScalarType()->getPointerElementType() == PointeeType && \"explicit pointee type doesn't match operand's pointee type\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h"
, 753, __PRETTY_FUNCTION__))
752 PointeeType &&((Ptr->getType()->getScalarType()->getPointerElementType
() == PointeeType && "explicit pointee type doesn't match operand's pointee type"
) ? static_cast<void> (0) : __assert_fail ("Ptr->getType()->getScalarType()->getPointerElementType() == PointeeType && \"explicit pointee type doesn't match operand's pointee type\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h"
, 753, __PRETTY_FUNCTION__))
753 "explicit pointee type doesn't match operand's pointee type")((Ptr->getType()->getScalarType()->getPointerElementType
() == PointeeType && "explicit pointee type doesn't match operand's pointee type"
) ? static_cast<void> (0) : __assert_fail ("Ptr->getType()->getScalarType()->getPointerElementType() == PointeeType && \"explicit pointee type doesn't match operand's pointee type\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h"
, 753, __PRETTY_FUNCTION__))
;
754 auto *BaseGV = dyn_cast<GlobalValue>(Ptr->stripPointerCasts());
755 bool HasBaseReg = (BaseGV == nullptr);
756
757 auto PtrSizeBits = DL.getPointerTypeSizeInBits(Ptr->getType());
758 APInt BaseOffset(PtrSizeBits, 0);
759 int64_t Scale = 0;
760
761 auto GTI = gep_type_begin(PointeeType, Operands);
762 Type *TargetType = nullptr;
763
764 // Handle the case where the GEP instruction has a single operand,
765 // the basis, therefore TargetType is a nullptr.
766 if (Operands.empty())
767 return !BaseGV ? TTI::TCC_Free : TTI::TCC_Basic;
768
769 for (auto I = Operands.begin(); I != Operands.end(); ++I, ++GTI) {
770 TargetType = GTI.getIndexedType();
771 // We assume that the cost of Scalar GEP with constant index and the
772 // cost of Vector GEP with splat constant index are the same.
773 const ConstantInt *ConstIdx = dyn_cast<ConstantInt>(*I);
774 if (!ConstIdx)
775 if (auto Splat = getSplatValue(*I))
776 ConstIdx = dyn_cast<ConstantInt>(Splat);
777 if (StructType *STy = GTI.getStructTypeOrNull()) {
778 // For structures the index is always splat or scalar constant
779 assert(ConstIdx && "Unexpected GEP index")((ConstIdx && "Unexpected GEP index") ? static_cast<
void> (0) : __assert_fail ("ConstIdx && \"Unexpected GEP index\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h"
, 779, __PRETTY_FUNCTION__))
;
780 uint64_t Field = ConstIdx->getZExtValue();
781 BaseOffset += DL.getStructLayout(STy)->getElementOffset(Field);
782 } else {
783 int64_t ElementSize = DL.getTypeAllocSize(GTI.getIndexedType());
784 if (ConstIdx) {
785 BaseOffset +=
786 ConstIdx->getValue().sextOrTrunc(PtrSizeBits) * ElementSize;
787 } else {
788 // Needs scale register.
789 if (Scale != 0)
790 // No addressing mode takes two scale registers.
791 return TTI::TCC_Basic;
792 Scale = ElementSize;
793 }
794 }
795 }
796
797 if (static_cast<T *>(this)->isLegalAddressingMode(
798 TargetType, const_cast<GlobalValue *>(BaseGV),
799 BaseOffset.sextOrTrunc(64).getSExtValue(), HasBaseReg, Scale,
800 Ptr->getType()->getPointerAddressSpace()))
801 return TTI::TCC_Free;
802 return TTI::TCC_Basic;
803 }
804
805 unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy,
806 ArrayRef<Type *> ParamTys, const User *U) {
807 switch (IID) {
808 default:
809 // Intrinsics rarely (if ever) have normal argument setup constraints.
810 // Model them as having a basic instruction cost.
811 return TTI::TCC_Basic;
812
813 // TODO: other libc intrinsics.
814 case Intrinsic::memcpy:
815 return static_cast<T *>(this)->getMemcpyCost(dyn_cast<Instruction>(U));
816
817 case Intrinsic::annotation:
818 case Intrinsic::assume:
819 case Intrinsic::sideeffect:
820 case Intrinsic::dbg_declare:
821 case Intrinsic::dbg_value:
822 case Intrinsic::dbg_label:
823 case Intrinsic::invariant_start:
824 case Intrinsic::invariant_end:
825 case Intrinsic::launder_invariant_group:
826 case Intrinsic::strip_invariant_group:
827 case Intrinsic::is_constant:
828 case Intrinsic::lifetime_start:
829 case Intrinsic::lifetime_end:
830 case Intrinsic::objectsize:
831 case Intrinsic::ptr_annotation:
832 case Intrinsic::var_annotation:
833 case Intrinsic::experimental_gc_result:
834 case Intrinsic::experimental_gc_relocate:
835 case Intrinsic::coro_alloc:
836 case Intrinsic::coro_begin:
837 case Intrinsic::coro_free:
838 case Intrinsic::coro_end:
839 case Intrinsic::coro_frame:
840 case Intrinsic::coro_size:
841 case Intrinsic::coro_suspend:
842 case Intrinsic::coro_param:
843 case Intrinsic::coro_subfn_addr:
844 // These intrinsics don't actually represent code after lowering.
845 return TTI::TCC_Free;
846 }
847 }
848
849 unsigned getIntrinsicCost(Intrinsic::ID IID, Type *RetTy,
850 ArrayRef<const Value *> Arguments, const User *U) {
851 // Delegate to the generic intrinsic handling code. This mostly provides an
852 // opportunity for targets to (for example) special case the cost of
853 // certain intrinsics based on constants used as arguments.
854 SmallVector<Type *, 8> ParamTys;
855 ParamTys.reserve(Arguments.size());
856 for (unsigned Idx = 0, Size = Arguments.size(); Idx != Size; ++Idx)
857 ParamTys.push_back(Arguments[Idx]->getType());
858 return static_cast<T *>(this)->getIntrinsicCost(IID, RetTy, ParamTys, U);
859 }
860
861 unsigned getUserCost(const User *U, ArrayRef<const Value *> Operands) {
862 if (isa<PHINode>(U))
4
Assuming 'U' is not a 'PHINode'
5
Taking false branch
863 return TTI::TCC_Free; // Model all PHI nodes as free.
864
865 if (isa<ExtractValueInst>(U))
6
Assuming 'U' is not a 'ExtractValueInst'
7
Taking false branch
866 return TTI::TCC_Free; // Model all ExtractValue nodes as free.
867
868 // Static alloca doesn't generate target instructions.
869 if (auto *A
8.1
'A' is null
8.1
'A' is null
8.1
'A' is null
8.1
'A' is null
8.1
'A' is null
8.1
'A' is null
8.1
'A' is null
= dyn_cast<AllocaInst>(U))
8
Assuming 'U' is not a 'AllocaInst'
9
Taking false branch
870 if (A->isStaticAlloca())
871 return TTI::TCC_Free;
872
873 if (const GEPOperator *GEP
10.1
'GEP' is null
10.1
'GEP' is null
10.1
'GEP' is null
10.1
'GEP' is null
10.1
'GEP' is null
10.1
'GEP' is null
10.1
'GEP' is null
= dyn_cast<GEPOperator>(U)) {
10
Assuming 'U' is not a 'GEPOperator'
11
Taking false branch
874 return static_cast<T *>(this)->getGEPCost(GEP->getSourceElementType(),
875 GEP->getPointerOperand(),
876 Operands.drop_front());
877 }
878
879 if (auto CS = ImmutableCallSite(U)) {
12
Calling 'CallSiteBase::operator bool'
26
Returning from 'CallSiteBase::operator bool'
27
Taking false branch
880 const Function *F = CS.getCalledFunction();
881 if (!F) {
882 // Just use the called value type.
883 Type *FTy = CS.getCalledValue()->getType()->getPointerElementType();
884 return static_cast<T *>(this)
885 ->getCallCost(cast<FunctionType>(FTy), CS.arg_size(), U);
886 }
887
888 SmallVector<const Value *, 8> Arguments(CS.arg_begin(), CS.arg_end());
889 return static_cast<T *>(this)->getCallCost(F, Arguments, U);
890 }
891
892 if (isa<SExtInst>(U) || isa<ZExtInst>(U) || isa<FPExtInst>(U))
28
Assuming 'U' is not a 'SExtInst'
29
Assuming 'U' is not a 'ZExtInst'
30
Assuming 'U' is not a 'FPExtInst'
31
Taking false branch
893 // The old behaviour of generally treating extensions of icmp to be free
894 // has been removed. A target that needs it should override getUserCost().
895 return static_cast<T *>(this)->getExtCost(cast<Instruction>(U),
896 Operands.back());
897
898 return static_cast<T *>(this)->getOperationCost(
40
Calling 'BasicTTIImplBase::getOperationCost'
899 Operator::getOpcode(U), U->getType(),
32
Calling 'Operator::getOpcode'
36
Returning from 'Operator::getOpcode'
900 U->getNumOperands() == 1 ? U->getOperand(0)->getType() : nullptr);
37
Assuming the condition is false
38
'?' condition is false
39
Passing null pointer value via 3rd parameter 'OpTy'
901 }
902
903 int getInstructionLatency(const Instruction *I) {
904 SmallVector<const Value *, 4> Operands(I->value_op_begin(),
905 I->value_op_end());
906 if (getUserCost(I, Operands) == TTI::TCC_Free)
907 return 0;
908
909 if (isa<LoadInst>(I))
910 return 4;
911
912 Type *DstTy = I->getType();
913
914 // Usually an intrinsic is a simple instruction.
915 // A real function call is much slower.
916 if (auto *CI = dyn_cast<CallInst>(I)) {
917 const Function *F = CI->getCalledFunction();
918 if (!F || static_cast<T *>(this)->isLoweredToCall(F))
919 return 40;
920 // Some intrinsics return a value and a flag, we use the value type
921 // to decide its latency.
922 if (StructType* StructTy = dyn_cast<StructType>(DstTy))
923 DstTy = StructTy->getElementType(0);
924 // Fall through to simple instructions.
925 }
926
927 if (VectorType *VectorTy = dyn_cast<VectorType>(DstTy))
928 DstTy = VectorTy->getElementType();
929 if (DstTy->isFloatingPointTy())
930 return 3;
931
932 return 1;
933 }
934};
935}
936
937#endif

/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/CallSite.h

1//===- CallSite.h - Abstract Call & Invoke instrs ---------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the CallSite class, which is a handy wrapper for code that
10// wants to treat Call, Invoke and CallBr instructions in a generic way. When
11// in non-mutation context (e.g. an analysis) ImmutableCallSite should be used.
12// Finally, when some degree of customization is necessary between these two
13// extremes, CallSiteBase<> can be supplied with fine-tuned parameters.
14//
15// NOTE: These classes are supposed to have "value semantics". So they should be
16// passed by value, not by reference; they should not be "new"ed or "delete"d.
17// They are efficiently copyable, assignable and constructable, with cost
18// equivalent to copying a pointer (notice that they have only a single data
19// member). The internal representation carries a flag which indicates which of
20// the three variants is enclosed. This allows for cheaper checks when various
21// accessors of CallSite are employed.
22//
23//===----------------------------------------------------------------------===//
24
25#ifndef LLVM_IR_CALLSITE_H
26#define LLVM_IR_CALLSITE_H
27
28#include "llvm/ADT/Optional.h"
29#include "llvm/ADT/PointerIntPair.h"
30#include "llvm/ADT/iterator_range.h"
31#include "llvm/IR/Attributes.h"
32#include "llvm/IR/CallingConv.h"
33#include "llvm/IR/Function.h"
34#include "llvm/IR/InstrTypes.h"
35#include "llvm/IR/Instruction.h"
36#include "llvm/IR/Instructions.h"
37#include "llvm/IR/Use.h"
38#include "llvm/IR/User.h"
39#include "llvm/IR/Value.h"
40#include "llvm/Support/Casting.h"
41#include <cassert>
42#include <cstdint>
43#include <iterator>
44
45namespace llvm {
46
47namespace Intrinsic {
48typedef unsigned ID;
49}
50
51template <typename FunTy = const Function, typename BBTy = const BasicBlock,
52 typename ValTy = const Value, typename UserTy = const User,
53 typename UseTy = const Use, typename InstrTy = const Instruction,
54 typename CallTy = const CallInst,
55 typename InvokeTy = const InvokeInst,
56 typename CallBrTy = const CallBrInst,
57 typename IterTy = User::const_op_iterator>
58class CallSiteBase {
59protected:
60 PointerIntPair<InstrTy *, 2, int> I;
61
62 CallSiteBase() = default;
63 CallSiteBase(CallTy *CI) : I(CI, 1) { assert(CI)((CI) ? static_cast<void> (0) : __assert_fail ("CI", "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/CallSite.h"
, 63, __PRETTY_FUNCTION__))
; }
64 CallSiteBase(InvokeTy *II) : I(II, 0) { assert(II)((II) ? static_cast<void> (0) : __assert_fail ("II", "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/CallSite.h"
, 64, __PRETTY_FUNCTION__))
; }
65 CallSiteBase(CallBrTy *CBI) : I(CBI, 2) { assert(CBI)((CBI) ? static_cast<void> (0) : __assert_fail ("CBI", "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/CallSite.h"
, 65, __PRETTY_FUNCTION__))
; }
66 explicit CallSiteBase(ValTy *II) { *this = get(II); }
67
68private:
69 /// This static method is like a constructor. It will create an appropriate
70 /// call site for a Call, Invoke or CallBr instruction, but it can also create
71 /// a null initialized CallSiteBase object for something which is NOT a call
72 /// site.
73 static CallSiteBase get(ValTy *V) {
74 if (InstrTy *II = dyn_cast<InstrTy>(V)) {
75 if (II->getOpcode() == Instruction::Call)
76 return CallSiteBase(static_cast<CallTy*>(II));
77 if (II->getOpcode() == Instruction::Invoke)
78 return CallSiteBase(static_cast<InvokeTy*>(II));
79 if (II->getOpcode() == Instruction::CallBr)
80 return CallSiteBase(static_cast<CallBrTy *>(II));
81 }
82 return CallSiteBase();
83 }
84
85public:
86 /// Return true if a CallInst is enclosed.
87 bool isCall() const { return I.getInt() == 1; }
88
89 /// Return true if a InvokeInst is enclosed. !I.getInt() may also signify a
90 /// NULL instruction pointer, so check that.
91 bool isInvoke() const { return getInstruction() && I.getInt() == 0; }
92
93 /// Return true if a CallBrInst is enclosed.
94 bool isCallBr() const { return I.getInt() == 2; }
95
96 InstrTy *getInstruction() const { return I.getPointer(); }
97 InstrTy *operator->() const { return I.getPointer(); }
98 explicit operator bool() const { return I.getPointer(); }
13
Calling 'PointerIntPair::getPointer'
24
Returning from 'PointerIntPair::getPointer'
25
Returning zero, which participates in a condition later
99
100 /// Get the basic block containing the call site.
101 BBTy* getParent() const { return getInstruction()->getParent(); }
102
103 /// Return the pointer to function that is being called.
104 ValTy *getCalledValue() const {
105 assert(getInstruction() && "Not a call, invoke or callbr instruction!")((getInstruction() && "Not a call, invoke or callbr instruction!"
) ? static_cast<void> (0) : __assert_fail ("getInstruction() && \"Not a call, invoke or callbr instruction!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/CallSite.h"
, 105, __PRETTY_FUNCTION__))
;
106 return *getCallee();
107 }
108
109 /// Return the function being called if this is a direct call, otherwise
110 /// return null (if it's an indirect call).
111 FunTy *getCalledFunction() const {
112 return dyn_cast<FunTy>(getCalledValue());
113 }
114
115 /// Return true if the callsite is an indirect call.
116 bool isIndirectCall() const {
117 const Value *V = getCalledValue();
118 if (!V)
119 return false;
120 if (isa<FunTy>(V) || isa<Constant>(V))
121 return false;
122 if (const CallBase *CB = dyn_cast<CallBase>(getInstruction()))
123 if (CB->isInlineAsm())
124 return false;
125 return true;
126 }
127
128 /// Set the callee to the specified value. Unlike the function of the same
129 /// name on CallBase, does not modify the type!
130 void setCalledFunction(Value *V) {
131 assert(getInstruction() && "Not a call, callbr, or invoke instruction!")((getInstruction() && "Not a call, callbr, or invoke instruction!"
) ? static_cast<void> (0) : __assert_fail ("getInstruction() && \"Not a call, callbr, or invoke instruction!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/CallSite.h"
, 131, __PRETTY_FUNCTION__))
;
132 assert(cast<PointerType>(V->getType())->getElementType() ==((cast<PointerType>(V->getType())->getElementType
() == cast<CallBase>(getInstruction())->getFunctionType
() && "New callee type does not match FunctionType on call"
) ? static_cast<void> (0) : __assert_fail ("cast<PointerType>(V->getType())->getElementType() == cast<CallBase>(getInstruction())->getFunctionType() && \"New callee type does not match FunctionType on call\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/CallSite.h"
, 134, __PRETTY_FUNCTION__))
133 cast<CallBase>(getInstruction())->getFunctionType() &&((cast<PointerType>(V->getType())->getElementType
() == cast<CallBase>(getInstruction())->getFunctionType
() && "New callee type does not match FunctionType on call"
) ? static_cast<void> (0) : __assert_fail ("cast<PointerType>(V->getType())->getElementType() == cast<CallBase>(getInstruction())->getFunctionType() && \"New callee type does not match FunctionType on call\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/CallSite.h"
, 134, __PRETTY_FUNCTION__))
134 "New callee type does not match FunctionType on call")((cast<PointerType>(V->getType())->getElementType
() == cast<CallBase>(getInstruction())->getFunctionType
() && "New callee type does not match FunctionType on call"
) ? static_cast<void> (0) : __assert_fail ("cast<PointerType>(V->getType())->getElementType() == cast<CallBase>(getInstruction())->getFunctionType() && \"New callee type does not match FunctionType on call\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/CallSite.h"
, 134, __PRETTY_FUNCTION__))
;
135 *getCallee() = V;
136 }
137
138 /// Return the intrinsic ID of the intrinsic called by this CallSite,
139 /// or Intrinsic::not_intrinsic if the called function is not an
140 /// intrinsic, or if this CallSite is an indirect call.
141 Intrinsic::ID getIntrinsicID() const {
142 if (auto *F = getCalledFunction())
143 return F->getIntrinsicID();
144 // Don't use Intrinsic::not_intrinsic, as it will require pulling
145 // Intrinsics.h into every header that uses CallSite.
146 return static_cast<Intrinsic::ID>(0);
147 }
148
149 /// Determine whether the passed iterator points to the callee operand's Use.
150 bool isCallee(Value::const_user_iterator UI) const {
151 return isCallee(&UI.getUse());
152 }
153
154 /// Determine whether this Use is the callee operand's Use.
155 bool isCallee(const Use *U) const { return getCallee() == U; }
156
157 /// Determine whether the passed iterator points to an argument operand.
158 bool isArgOperand(Value::const_user_iterator UI) const {
159 return isArgOperand(&UI.getUse());
160 }
161
162 /// Determine whether the passed use points to an argument operand.
163 bool isArgOperand(const Use *U) const {
164 assert(getInstruction() == U->getUser())((getInstruction() == U->getUser()) ? static_cast<void>
(0) : __assert_fail ("getInstruction() == U->getUser()", "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/CallSite.h"
, 164, __PRETTY_FUNCTION__))
;
165 return arg_begin() <= U && U < arg_end();
166 }
167
168 /// Determine whether the passed iterator points to a bundle operand.
169 bool isBundleOperand(Value::const_user_iterator UI) const {
170 return isBundleOperand(&UI.getUse());
171 }
172
173 /// Determine whether the passed use points to a bundle operand.
174 bool isBundleOperand(const Use *U) const {
175 assert(getInstruction() == U->getUser())((getInstruction() == U->getUser()) ? static_cast<void>
(0) : __assert_fail ("getInstruction() == U->getUser()", "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/CallSite.h"
, 175, __PRETTY_FUNCTION__))
;
176 if (!hasOperandBundles())
177 return false;
178 unsigned OperandNo = U - (*this)->op_begin();
179 return getBundleOperandsStartIndex() <= OperandNo &&
180 OperandNo < getBundleOperandsEndIndex();
181 }
182
183 /// Determine whether the passed iterator points to a data operand.
184 bool isDataOperand(Value::const_user_iterator UI) const {
185 return isDataOperand(&UI.getUse());
186 }
187
188 /// Determine whether the passed use points to a data operand.
189 bool isDataOperand(const Use *U) const {
190 return data_operands_begin() <= U && U < data_operands_end();
191 }
192
193 ValTy *getArgument(unsigned ArgNo) const {
194 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!")((arg_begin() + ArgNo < arg_end() && "Argument # out of range!"
) ? static_cast<void> (0) : __assert_fail ("arg_begin() + ArgNo < arg_end() && \"Argument # out of range!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/CallSite.h"
, 194, __PRETTY_FUNCTION__))
;
195 return *(arg_begin() + ArgNo);
196 }
197
198 void setArgument(unsigned ArgNo, Value* newVal) {
199 assert(getInstruction() && "Not a call, invoke or callbr instruction!")((getInstruction() && "Not a call, invoke or callbr instruction!"
) ? static_cast<void> (0) : __assert_fail ("getInstruction() && \"Not a call, invoke or callbr instruction!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/CallSite.h"
, 199, __PRETTY_FUNCTION__))
;
200 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!")((arg_begin() + ArgNo < arg_end() && "Argument # out of range!"
) ? static_cast<void> (0) : __assert_fail ("arg_begin() + ArgNo < arg_end() && \"Argument # out of range!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/CallSite.h"
, 200, __PRETTY_FUNCTION__))
;
201 getInstruction()->setOperand(ArgNo, newVal);
202 }
203
204 /// Given a value use iterator, returns the argument that corresponds to it.
205 /// Iterator must actually correspond to an argument.
206 unsigned getArgumentNo(Value::const_user_iterator I) const {
207 return getArgumentNo(&I.getUse());
208 }
209
210 /// Given a use for an argument, get the argument number that corresponds to
211 /// it.
212 unsigned getArgumentNo(const Use *U) const {
213 assert(getInstruction() && "Not a call, invoke or callbr instruction!")((getInstruction() && "Not a call, invoke or callbr instruction!"
) ? static_cast<void> (0) : __assert_fail ("getInstruction() && \"Not a call, invoke or callbr instruction!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/CallSite.h"
, 213, __PRETTY_FUNCTION__))
;
214 assert(isArgOperand(U) && "Argument # out of range!")((isArgOperand(U) && "Argument # out of range!") ? static_cast
<void> (0) : __assert_fail ("isArgOperand(U) && \"Argument # out of range!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/CallSite.h"
, 214, __PRETTY_FUNCTION__))
;
215 return U - arg_begin();
216 }
217
218 /// The type of iterator to use when looping over actual arguments at this
219 /// call site.
220 using arg_iterator = IterTy;
221
222 iterator_range<IterTy> args() const {
223 return make_range(arg_begin(), arg_end());
224 }
225 bool arg_empty() const { return arg_end() == arg_begin(); }
226 unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
227
228 /// Given a value use iterator, return the data operand corresponding to it.
229 /// Iterator must actually correspond to a data operand.
230 unsigned getDataOperandNo(Value::const_user_iterator UI) const {
231 return getDataOperandNo(&UI.getUse());
232 }
233
234 /// Given a use for a data operand, get the data operand number that
235 /// corresponds to it.
236 unsigned getDataOperandNo(const Use *U) const {
237 assert(getInstruction() && "Not a call, invoke or callbr instruction!")((getInstruction() && "Not a call, invoke or callbr instruction!"
) ? static_cast<void> (0) : __assert_fail ("getInstruction() && \"Not a call, invoke or callbr instruction!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/CallSite.h"
, 237, __PRETTY_FUNCTION__))
;
238 assert(isDataOperand(U) && "Data operand # out of range!")((isDataOperand(U) && "Data operand # out of range!")
? static_cast<void> (0) : __assert_fail ("isDataOperand(U) && \"Data operand # out of range!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/CallSite.h"
, 238, __PRETTY_FUNCTION__))
;
239 return U - data_operands_begin();
240 }
241
242 /// Type of iterator to use when looping over data operands at this call site
243 /// (see below).
244 using data_operand_iterator = IterTy;
245
246 /// data_operands_begin/data_operands_end - Return iterators iterating over
247 /// the call / invoke / callbr argument list and bundle operands. For invokes,
248 /// this is the set of instruction operands except the invoke target and the
249 /// two successor blocks; for calls this is the set of instruction operands
250 /// except the call target; for callbrs the number of labels to skip must be
251 /// determined first.
252
253 IterTy data_operands_begin() const {
254 assert(getInstruction() && "Not a call or invoke instruction!")((getInstruction() && "Not a call or invoke instruction!"
) ? static_cast<void> (0) : __assert_fail ("getInstruction() && \"Not a call or invoke instruction!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/CallSite.h"
, 254, __PRETTY_FUNCTION__))
;
255 return cast<CallBase>(getInstruction())->data_operands_begin();
256 }
257 IterTy data_operands_end() const {
258 assert(getInstruction() && "Not a call or invoke instruction!")((getInstruction() && "Not a call or invoke instruction!"
) ? static_cast<void> (0) : __assert_fail ("getInstruction() && \"Not a call or invoke instruction!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/CallSite.h"
, 258, __PRETTY_FUNCTION__))
;
259 return cast<CallBase>(getInstruction())->data_operands_end();
260 }
261 iterator_range<IterTy> data_ops() const {
262 return make_range(data_operands_begin(), data_operands_end());
263 }
264 bool data_operands_empty() const {
265 return data_operands_end() == data_operands_begin();
266 }
267 unsigned data_operands_size() const {
268 return std::distance(data_operands_begin(), data_operands_end());
269 }
270
271 /// Return the type of the instruction that generated this call site.
272 Type *getType() const { return (*this)->getType(); }
273
274 /// Return the caller function for this call site.
275 FunTy *getCaller() const { return (*this)->getParent()->getParent(); }
276
277 /// Tests if this call site must be tail call optimized. Only a CallInst can
278 /// be tail call optimized.
279 bool isMustTailCall() const {
280 return isCall() && cast<CallInst>(getInstruction())->isMustTailCall();
281 }
282
283 /// Tests if this call site is marked as a tail call.
284 bool isTailCall() const {
285 return isCall() && cast<CallInst>(getInstruction())->isTailCall();
286 }
287
288#define CALLSITE_DELEGATE_GETTER(METHOD) \
289 InstrTy *II = getInstruction(); \
290 return isCall() ? cast<CallInst>(II)->METHOD \
291 : isCallBr() ? cast<CallBrInst>(II)->METHOD \
292 : cast<InvokeInst>(II)->METHOD
293
294#define CALLSITE_DELEGATE_SETTER(METHOD) \
295 InstrTy *II = getInstruction(); \
296 if (isCall()) \
297 cast<CallInst>(II)->METHOD; \
298 else if (isCallBr()) \
299 cast<CallBrInst>(II)->METHOD; \
300 else \
301 cast<InvokeInst>(II)->METHOD
302
303 unsigned getNumArgOperands() const {
304 CALLSITE_DELEGATE_GETTER(getNumArgOperands());
305 }
306
307 ValTy *getArgOperand(unsigned i) const {
308 CALLSITE_DELEGATE_GETTER(getArgOperand(i));
309 }
310
311 ValTy *getReturnedArgOperand() const {
312 CALLSITE_DELEGATE_GETTER(getReturnedArgOperand());
313 }
314
315 bool isInlineAsm() const {
316 return cast<CallBase>(getInstruction())->isInlineAsm();
317 }
318
319 /// Get the calling convention of the call.
320 CallingConv::ID getCallingConv() const {
321 CALLSITE_DELEGATE_GETTER(getCallingConv());
322 }
323 /// Set the calling convention of the call.
324 void setCallingConv(CallingConv::ID CC) {
325 CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
326 }
327
328 FunctionType *getFunctionType() const {
329 CALLSITE_DELEGATE_GETTER(getFunctionType());
330 }
331
332 void mutateFunctionType(FunctionType *Ty) const {
333 CALLSITE_DELEGATE_SETTER(mutateFunctionType(Ty));
334 }
335
336 /// Get the parameter attributes of the call.
337 AttributeList getAttributes() const {
338 CALLSITE_DELEGATE_GETTER(getAttributes());
339 }
340 /// Set the parameter attributes of the call.
341 void setAttributes(AttributeList PAL) {
342 CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
343 }
344
345 void addAttribute(unsigned i, Attribute::AttrKind Kind) {
346 CALLSITE_DELEGATE_SETTER(addAttribute(i, Kind));
347 }
348
349 void addAttribute(unsigned i, Attribute Attr) {
350 CALLSITE_DELEGATE_SETTER(addAttribute(i, Attr));
351 }
352
353 void addParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
354 CALLSITE_DELEGATE_SETTER(addParamAttr(ArgNo, Kind));
355 }
356
357 void removeAttribute(unsigned i, Attribute::AttrKind Kind) {
358 CALLSITE_DELEGATE_SETTER(removeAttribute(i, Kind));
359 }
360
361 void removeAttribute(unsigned i, StringRef Kind) {
362 CALLSITE_DELEGATE_SETTER(removeAttribute(i, Kind));
363 }
364
365 void removeParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) {
366 CALLSITE_DELEGATE_SETTER(removeParamAttr(ArgNo, Kind));
367 }
368
369 /// Return true if this function has the given attribute.
370 bool hasFnAttr(Attribute::AttrKind Kind) const {
371 CALLSITE_DELEGATE_GETTER(hasFnAttr(Kind));
372 }
373
374 /// Return true if this function has the given attribute.
375 bool hasFnAttr(StringRef Kind) const {
376 CALLSITE_DELEGATE_GETTER(hasFnAttr(Kind));
377 }
378
379 /// Return true if this return value has the given attribute.
380 bool hasRetAttr(Attribute::AttrKind Kind) const {
381 CALLSITE_DELEGATE_GETTER(hasRetAttr(Kind));
382 }
383
384 /// Return true if the call or the callee has the given attribute.
385 bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
386 CALLSITE_DELEGATE_GETTER(paramHasAttr(ArgNo, Kind));
387 }
388
389 Attribute getAttribute(unsigned i, Attribute::AttrKind Kind) const {
390 CALLSITE_DELEGATE_GETTER(getAttribute(i, Kind));
391 }
392
393 Attribute getAttribute(unsigned i, StringRef Kind) const {
394 CALLSITE_DELEGATE_GETTER(getAttribute(i, Kind));
395 }
396
397 /// Return true if the data operand at index \p i directly or indirectly has
398 /// the attribute \p A.
399 ///
400 /// Normal call, invoke or callbr arguments have per operand attributes, as
401 /// specified in the attribute set attached to this instruction, while operand
402 /// bundle operands may have some attributes implied by the type of its
403 /// containing operand bundle.
404 bool dataOperandHasImpliedAttr(unsigned i, Attribute::AttrKind Kind) const {
405 CALLSITE_DELEGATE_GETTER(dataOperandHasImpliedAttr(i, Kind));
406 }
407
408 /// Extract the alignment of the return value.
409 unsigned getRetAlignment() const {
410 CALLSITE_DELEGATE_GETTER(getRetAlignment());
411 }
412
413 /// Extract the alignment for a call or parameter (0=unknown).
414 unsigned getParamAlignment(unsigned ArgNo) const {
415 CALLSITE_DELEGATE_GETTER(getParamAlignment(ArgNo));
416 }
417
418 /// Extract the byval type for a call or parameter (nullptr=unknown).
419 Type *getParamByValType(unsigned ArgNo) const {
420 CALLSITE_DELEGATE_GETTER(getParamByValType(ArgNo));
421 }
422
423 /// Extract the number of dereferenceable bytes for a call or parameter
424 /// (0=unknown).
425 uint64_t getDereferenceableBytes(unsigned i) const {
426 CALLSITE_DELEGATE_GETTER(getDereferenceableBytes(i));
427 }
428
429 /// Extract the number of dereferenceable_or_null bytes for a call or
430 /// parameter (0=unknown).
431 uint64_t getDereferenceableOrNullBytes(unsigned i) const {
432 CALLSITE_DELEGATE_GETTER(getDereferenceableOrNullBytes(i));
433 }
434
435 /// Determine if the return value is marked with NoAlias attribute.
436 bool returnDoesNotAlias() const {
437 CALLSITE_DELEGATE_GETTER(returnDoesNotAlias());
438 }
439
440 /// Return true if the call should not be treated as a call to a builtin.
441 bool isNoBuiltin() const {
442 CALLSITE_DELEGATE_GETTER(isNoBuiltin());
443 }
444
445 /// Return true if the call requires strict floating point semantics.
446 bool isStrictFP() const {
447 CALLSITE_DELEGATE_GETTER(isStrictFP());
448 }
449
450 /// Return true if the call should not be inlined.
451 bool isNoInline() const {
452 CALLSITE_DELEGATE_GETTER(isNoInline());
453 }
454 void setIsNoInline(bool Value = true) {
455 CALLSITE_DELEGATE_SETTER(setIsNoInline(Value));
456 }
457
458 /// Determine if the call does not access memory.
459 bool doesNotAccessMemory() const {
460 CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
461 }
462 void setDoesNotAccessMemory() {
463 CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory());
464 }
465
466 /// Determine if the call does not access or only reads memory.
467 bool onlyReadsMemory() const {
468 CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
469 }
470 void setOnlyReadsMemory() {
471 CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory());
472 }
473
474 /// Determine if the call does not access or only writes memory.
475 bool doesNotReadMemory() const {
476 CALLSITE_DELEGATE_GETTER(doesNotReadMemory());
477 }
478 void setDoesNotReadMemory() {
479 CALLSITE_DELEGATE_SETTER(setDoesNotReadMemory());
480 }
481
482 /// Determine if the call can access memmory only using pointers based
483 /// on its arguments.
484 bool onlyAccessesArgMemory() const {
485 CALLSITE_DELEGATE_GETTER(onlyAccessesArgMemory());
486 }
487 void setOnlyAccessesArgMemory() {
488 CALLSITE_DELEGATE_SETTER(setOnlyAccessesArgMemory());
489 }
490
491 /// Determine if the function may only access memory that is
492 /// inaccessible from the IR.
493 bool onlyAccessesInaccessibleMemory() const {
494 CALLSITE_DELEGATE_GETTER(onlyAccessesInaccessibleMemory());
495 }
496 void setOnlyAccessesInaccessibleMemory() {
497 CALLSITE_DELEGATE_SETTER(setOnlyAccessesInaccessibleMemory());
498 }
499
500 /// Determine if the function may only access memory that is
501 /// either inaccessible from the IR or pointed to by its arguments.
502 bool onlyAccessesInaccessibleMemOrArgMem() const {
503 CALLSITE_DELEGATE_GETTER(onlyAccessesInaccessibleMemOrArgMem());
504 }
505 void setOnlyAccessesInaccessibleMemOrArgMem() {
506 CALLSITE_DELEGATE_SETTER(setOnlyAccessesInaccessibleMemOrArgMem());
507 }
508
509 /// Determine if the call cannot return.
510 bool doesNotReturn() const {
511 CALLSITE_DELEGATE_GETTER(doesNotReturn());
512 }
513 void setDoesNotReturn() {
514 CALLSITE_DELEGATE_SETTER(setDoesNotReturn());
515 }
516
517 /// Determine if the call cannot unwind.
518 bool doesNotThrow() const {
519 CALLSITE_DELEGATE_GETTER(doesNotThrow());
520 }
521 void setDoesNotThrow() {
522 CALLSITE_DELEGATE_SETTER(setDoesNotThrow());
523 }
524
525 /// Determine if the call can be duplicated.
526 bool cannotDuplicate() const {
527 CALLSITE_DELEGATE_GETTER(cannotDuplicate());
528 }
529 void setCannotDuplicate() {
530 CALLSITE_DELEGATE_SETTER(setCannotDuplicate());
531 }
532
533 /// Determine if the call is convergent.
534 bool isConvergent() const {
535 CALLSITE_DELEGATE_GETTER(isConvergent());
536 }
537 void setConvergent() {
538 CALLSITE_DELEGATE_SETTER(setConvergent());
539 }
540 void setNotConvergent() {
541 CALLSITE_DELEGATE_SETTER(setNotConvergent());
542 }
543
544 unsigned getNumOperandBundles() const {
545 CALLSITE_DELEGATE_GETTER(getNumOperandBundles());
546 }
547
548 bool hasOperandBundles() const {
549 CALLSITE_DELEGATE_GETTER(hasOperandBundles());
550 }
551
552 unsigned getBundleOperandsStartIndex() const {
553 CALLSITE_DELEGATE_GETTER(getBundleOperandsStartIndex());
554 }
555
556 unsigned getBundleOperandsEndIndex() const {
557 CALLSITE_DELEGATE_GETTER(getBundleOperandsEndIndex());
558 }
559
560 unsigned getNumTotalBundleOperands() const {
561 CALLSITE_DELEGATE_GETTER(getNumTotalBundleOperands());
562 }
563
564 OperandBundleUse getOperandBundleAt(unsigned Index) const {
565 CALLSITE_DELEGATE_GETTER(getOperandBundleAt(Index));
566 }
567
568 Optional<OperandBundleUse> getOperandBundle(StringRef Name) const {
569 CALLSITE_DELEGATE_GETTER(getOperandBundle(Name));
570 }
571
572 Optional<OperandBundleUse> getOperandBundle(uint32_t ID) const {
573 CALLSITE_DELEGATE_GETTER(getOperandBundle(ID));
574 }
575
576 unsigned countOperandBundlesOfType(uint32_t ID) const {
577 CALLSITE_DELEGATE_GETTER(countOperandBundlesOfType(ID));
578 }
579
580 bool isBundleOperand(unsigned Idx) const {
581 CALLSITE_DELEGATE_GETTER(isBundleOperand(Idx));
582 }
583
584 IterTy arg_begin() const {
585 CALLSITE_DELEGATE_GETTER(arg_begin());
586 }
587
588 IterTy arg_end() const {
589 CALLSITE_DELEGATE_GETTER(arg_end());
590 }
591
592#undef CALLSITE_DELEGATE_GETTER
593#undef CALLSITE_DELEGATE_SETTER
594
595 void getOperandBundlesAsDefs(SmallVectorImpl<OperandBundleDef> &Defs) const {
596 // Since this is actually a getter that "looks like" a setter, don't use the
597 // above macros to avoid confusion.
598 cast<CallBase>(getInstruction())->getOperandBundlesAsDefs(Defs);
599 }
600
601 /// Determine whether this data operand is not captured.
602 bool doesNotCapture(unsigned OpNo) const {
603 return dataOperandHasImpliedAttr(OpNo + 1, Attribute::NoCapture);
604 }
605
606 /// Determine whether this argument is passed by value.
607 bool isByValArgument(unsigned ArgNo) const {
608 return paramHasAttr(ArgNo, Attribute::ByVal);
609 }
610
611 /// Determine whether this argument is passed in an alloca.
612 bool isInAllocaArgument(unsigned ArgNo) const {
613 return paramHasAttr(ArgNo, Attribute::InAlloca);
614 }
615
616 /// Determine whether this argument is passed by value or in an alloca.
617 bool isByValOrInAllocaArgument(unsigned ArgNo) const {
618 return paramHasAttr(ArgNo, Attribute::ByVal) ||
619 paramHasAttr(ArgNo, Attribute::InAlloca);
620 }
621
622 /// Determine if there are is an inalloca argument. Only the last argument can
623 /// have the inalloca attribute.
624 bool hasInAllocaArgument() const {
625 return !arg_empty() && paramHasAttr(arg_size() - 1, Attribute::InAlloca);
626 }
627
628 bool doesNotAccessMemory(unsigned OpNo) const {
629 return dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone);
630 }
631
632 bool onlyReadsMemory(unsigned OpNo) const {
633 return dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadOnly) ||
634 dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone);
635 }
636
637 bool doesNotReadMemory(unsigned OpNo) const {
638 return dataOperandHasImpliedAttr(OpNo + 1, Attribute::WriteOnly) ||
639 dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone);
640 }
641
642 /// Return true if the return value is known to be not null.
643 /// This may be because it has the nonnull attribute, or because at least
644 /// one byte is dereferenceable and the pointer is in addrspace(0).
645 bool isReturnNonNull() const {
646 if (hasRetAttr(Attribute::NonNull))
647 return true;
648 else if (getDereferenceableBytes(AttributeList::ReturnIndex) > 0 &&
649 !NullPointerIsDefined(getCaller(),
650 getType()->getPointerAddressSpace()))
651 return true;
652
653 return false;
654 }
655
656 /// Returns true if this CallSite passes the given Value* as an argument to
657 /// the called function.
658 bool hasArgument(const Value *Arg) const {
659 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E;
660 ++AI)
661 if (AI->get() == Arg)
662 return true;
663 return false;
664 }
665
666private:
667 IterTy getCallee() const {
668 return cast<CallBase>(getInstruction())->op_end() - 1;
669 }
670};
671
672class CallSite : public CallSiteBase<Function, BasicBlock, Value, User, Use,
673 Instruction, CallInst, InvokeInst,
674 CallBrInst, User::op_iterator> {
675public:
676 CallSite() = default;
677 CallSite(CallSiteBase B) : CallSiteBase(B) {}
678 CallSite(CallInst *CI) : CallSiteBase(CI) {}
679 CallSite(InvokeInst *II) : CallSiteBase(II) {}
680 CallSite(CallBrInst *CBI) : CallSiteBase(CBI) {}
681 explicit CallSite(Instruction *II) : CallSiteBase(II) {}
682 explicit CallSite(Value *V) : CallSiteBase(V) {}
683
684 bool operator==(const CallSite &CS) const { return I == CS.I; }
685 bool operator!=(const CallSite &CS) const { return I != CS.I; }
686 bool operator<(const CallSite &CS) const {
687 return getInstruction() < CS.getInstruction();
688 }
689
690private:
691 friend struct DenseMapInfo<CallSite>;
692
693 User::op_iterator getCallee() const;
694};
695
696/// Establish a view to a call site for examination.
697class ImmutableCallSite : public CallSiteBase<> {
698public:
699 ImmutableCallSite() = default;
700 ImmutableCallSite(const CallInst *CI) : CallSiteBase(CI) {}
701 ImmutableCallSite(const InvokeInst *II) : CallSiteBase(II) {}
702 ImmutableCallSite(const CallBrInst *CBI) : CallSiteBase(CBI) {}
703 explicit ImmutableCallSite(const Instruction *II) : CallSiteBase(II) {}
704 explicit ImmutableCallSite(const Value *V) : CallSiteBase(V) {}
705 ImmutableCallSite(CallSite CS) : CallSiteBase(CS.getInstruction()) {}
706};
707
708/// AbstractCallSite
709///
710/// An abstract call site is a wrapper that allows to treat direct,
711/// indirect, and callback calls the same. If an abstract call site
712/// represents a direct or indirect call site it behaves like a stripped
713/// down version of a normal call site object. The abstract call site can
714/// also represent a callback call, thus the fact that the initially
715/// called function (=broker) may invoke a third one (=callback callee).
716/// In this case, the abstract call site hides the middle man, hence the
717/// broker function. The result is a representation of the callback call,
718/// inside the broker, but in the context of the original call to the broker.
719///
720/// There are up to three functions involved when we talk about callback call
721/// sites. The caller (1), which invokes the broker function. The broker
722/// function (2), that will invoke the callee zero or more times. And finally
723/// the callee (3), which is the target of the callback call.
724///
725/// The abstract call site will handle the mapping from parameters to arguments
726/// depending on the semantic of the broker function. However, it is important
727/// to note that the mapping is often partial. Thus, some arguments of the
728/// call/invoke instruction are mapped to parameters of the callee while others
729/// are not.
730class AbstractCallSite {
731public:
732
733 /// The encoding of a callback with regards to the underlying instruction.
734 struct CallbackInfo {
735
736 /// For direct/indirect calls the parameter encoding is empty. If it is not,
737 /// the abstract call site represents a callback. In that case, the first
738 /// element of the encoding vector represents which argument of the call
739 /// site CS is the callback callee. The remaining elements map parameters
740 /// (identified by their position) to the arguments that will be passed
741 /// through (also identified by position but in the call site instruction).
742 ///
743 /// NOTE that we use LLVM argument numbers (starting at 0) and not
744 /// clang/source argument numbers (starting at 1). The -1 entries represent
745 /// unknown values that are passed to the callee.
746 using ParameterEncodingTy = SmallVector<int, 0>;
747 ParameterEncodingTy ParameterEncoding;
748
749 };
750
751private:
752
753 /// The underlying call site:
754 /// caller -> callee, if this is a direct or indirect call site
755 /// caller -> broker function, if this is a callback call site
756 CallSite CS;
757
758 /// The encoding of a callback with regards to the underlying instruction.
759 CallbackInfo CI;
760
761public:
762 /// Sole constructor for abstract call sites (ACS).
763 ///
764 /// An abstract call site can only be constructed through a llvm::Use because
765 /// each operand (=use) of an instruction could potentially be a different
766 /// abstract call site. Furthermore, even if the value of the llvm::Use is the
767 /// same, and the user is as well, the abstract call sites might not be.
768 ///
769 /// If a use is not associated with an abstract call site the constructed ACS
770 /// will evaluate to false if converted to a boolean.
771 ///
772 /// If the use is the callee use of a call or invoke instruction, the
773 /// constructed abstract call site will behave as a llvm::CallSite would.
774 ///
775 /// If the use is not a callee use of a call or invoke instruction, the
776 /// callback metadata is used to determine the argument <-> parameter mapping
777 /// as well as the callee of the abstract call site.
778 AbstractCallSite(const Use *U);
779
780 /// Add operand uses of \p ICS that represent callback uses into \p CBUses.
781 ///
782 /// All uses added to \p CBUses can be used to create abstract call sites for
783 /// which AbstractCallSite::isCallbackCall() will return true.
784 static void getCallbackUses(ImmutableCallSite ICS,
785 SmallVectorImpl<const Use *> &CBUses);
786
787 /// Conversion operator to conveniently check for a valid/initialized ACS.
788 explicit operator bool() const { return (bool)CS; }
789
790 /// Return the underlying instruction.
791 Instruction *getInstruction() const { return CS.getInstruction(); }
792
793 /// Return the call site abstraction for the underlying instruction.
794 CallSite getCallSite() const { return CS; }
795
796 /// Return true if this ACS represents a direct call.
797 bool isDirectCall() const {
798 return !isCallbackCall() && !CS.isIndirectCall();
799 }
800
801 /// Return true if this ACS represents an indirect call.
802 bool isIndirectCall() const {
803 return !isCallbackCall() && CS.isIndirectCall();
804 }
805
806 /// Return true if this ACS represents a callback call.
807 bool isCallbackCall() const {
808 // For a callback call site the callee is ALWAYS stored first in the
809 // transitive values vector. Thus, a non-empty vector indicates a callback.
810 return !CI.ParameterEncoding.empty();
811 }
812
813 /// Return true if @p UI is the use that defines the callee of this ACS.
814 bool isCallee(Value::const_user_iterator UI) const {
815 return isCallee(&UI.getUse());
816 }
817
818 /// Return true if @p U is the use that defines the callee of this ACS.
819 bool isCallee(const Use *U) const {
820 if (isDirectCall())
821 return CS.isCallee(U);
822
823 assert(!CI.ParameterEncoding.empty() &&((!CI.ParameterEncoding.empty() && "Callback without parameter encoding!"
) ? static_cast<void> (0) : __assert_fail ("!CI.ParameterEncoding.empty() && \"Callback without parameter encoding!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/CallSite.h"
, 824, __PRETTY_FUNCTION__))
824 "Callback without parameter encoding!")((!CI.ParameterEncoding.empty() && "Callback without parameter encoding!"
) ? static_cast<void> (0) : __assert_fail ("!CI.ParameterEncoding.empty() && \"Callback without parameter encoding!\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/CallSite.h"
, 824, __PRETTY_FUNCTION__))
;
825
826 return (int)CS.getArgumentNo(U) == CI.ParameterEncoding[0];
827 }
828
829 /// Return the number of parameters of the callee.
830 unsigned getNumArgOperands() const {
831 if (isDirectCall())
832 return CS.getNumArgOperands();
833 // Subtract 1 for the callee encoding.
834 return CI.ParameterEncoding.size() - 1;
835 }
836
837 /// Return the operand index of the underlying instruction associated with @p
838 /// Arg.
839 int getCallArgOperandNo(Argument &Arg) const {
840 return getCallArgOperandNo(Arg.getArgNo());
841 }
842
843 /// Return the operand index of the underlying instruction associated with
844 /// the function parameter number @p ArgNo or -1 if there is none.
845 int getCallArgOperandNo(unsigned ArgNo) const {
846 if (isDirectCall())
847 return ArgNo;
848 // Add 1 for the callee encoding.
849 return CI.ParameterEncoding[ArgNo + 1];
850 }
851
852 /// Return the operand of the underlying instruction associated with @p Arg.
853 Value *getCallArgOperand(Argument &Arg) const {
854 return getCallArgOperand(Arg.getArgNo());
855 }
856
857 /// Return the operand of the underlying instruction associated with the
858 /// function parameter number @p ArgNo or nullptr if there is none.
859 Value *getCallArgOperand(unsigned ArgNo) const {
860 if (isDirectCall())
861 return CS.getArgOperand(ArgNo);
862 // Add 1 for the callee encoding.
863 return CI.ParameterEncoding[ArgNo + 1] >= 0
864 ? CS.getArgOperand(CI.ParameterEncoding[ArgNo + 1])
865 : nullptr;
866 }
867
868 /// Return the operand index of the underlying instruction associated with the
869 /// callee of this ACS. Only valid for callback calls!
870 int getCallArgOperandNoForCallee() const {
871 assert(isCallbackCall())((isCallbackCall()) ? static_cast<void> (0) : __assert_fail
("isCallbackCall()", "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/CallSite.h"
, 871, __PRETTY_FUNCTION__))
;
872 assert(CI.ParameterEncoding.size() && CI.ParameterEncoding[0] >= 0)((CI.ParameterEncoding.size() && CI.ParameterEncoding
[0] >= 0) ? static_cast<void> (0) : __assert_fail ("CI.ParameterEncoding.size() && CI.ParameterEncoding[0] >= 0"
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/CallSite.h"
, 872, __PRETTY_FUNCTION__))
;
873 return CI.ParameterEncoding[0];
874 }
875
876 /// Return the use of the callee value in the underlying instruction. Only
877 /// valid for callback calls!
878 const Use &getCalleeUseForCallback() const {
879 int CalleeArgIdx = getCallArgOperandNoForCallee();
880 assert(CalleeArgIdx >= 0 &&((CalleeArgIdx >= 0 && unsigned(CalleeArgIdx) <
getInstruction()->getNumOperands()) ? static_cast<void
> (0) : __assert_fail ("CalleeArgIdx >= 0 && unsigned(CalleeArgIdx) < getInstruction()->getNumOperands()"
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/CallSite.h"
, 881, __PRETTY_FUNCTION__))
881 unsigned(CalleeArgIdx) < getInstruction()->getNumOperands())((CalleeArgIdx >= 0 && unsigned(CalleeArgIdx) <
getInstruction()->getNumOperands()) ? static_cast<void
> (0) : __assert_fail ("CalleeArgIdx >= 0 && unsigned(CalleeArgIdx) < getInstruction()->getNumOperands()"
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/IR/CallSite.h"
, 881, __PRETTY_FUNCTION__))
;
882 return getInstruction()->getOperandUse(CalleeArgIdx);
883 }
884
885 /// Return the pointer to function that is being called.
886 Value *getCalledValue() const {
887 if (isDirectCall())
888 return CS.getCalledValue();
889 return CS.getArgOperand(getCallArgOperandNoForCallee());
890 }
891
892 /// Return the function being called if this is a direct call, otherwise
893 /// return null (if it's an indirect call).
894 Function *getCalledFunction() const {
895 Value *V = getCalledValue();
896 return V ? dyn_cast<Function>(V->stripPointerCasts()) : nullptr;
897 }
898};
899
900template <> struct DenseMapInfo<CallSite> {
901 using BaseInfo = DenseMapInfo<decltype(CallSite::I)>;
902
903 static CallSite getEmptyKey() {
904 CallSite CS;
905 CS.I = BaseInfo::getEmptyKey();
906 return CS;
907 }
908
909 static CallSite getTombstoneKey() {
910 CallSite CS;
911 CS.I = BaseInfo::getTombstoneKey();
912 return CS;
913 }
914
915 static unsigned getHashValue(const CallSite &CS) {
916 return BaseInfo::getHashValue(CS.I);
917 }
918
919 static bool isEqual(const CallSite &LHS, const CallSite &RHS) {
920 return LHS == RHS;
921 }
922};
923
924} // end namespace llvm
925
926#endif // LLVM_IR_CALLSITE_H

/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/ADT/PointerIntPair.h

1//===- llvm/ADT/PointerIntPair.h - Pair for pointer and int -----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the PointerIntPair class.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_ADT_POINTERINTPAIR_H
14#define LLVM_ADT_POINTERINTPAIR_H
15
16#include "llvm/Support/Compiler.h"
17#include "llvm/Support/PointerLikeTypeTraits.h"
18#include "llvm/Support/type_traits.h"
19#include <cassert>
20#include <cstdint>
21#include <limits>
22
23namespace llvm {
24
25template <typename T> struct DenseMapInfo;
26template <typename PointerT, unsigned IntBits, typename PtrTraits>
27struct PointerIntPairInfo;
28
29/// PointerIntPair - This class implements a pair of a pointer and small
30/// integer. It is designed to represent this in the space required by one
31/// pointer by bitmangling the integer into the low part of the pointer. This
32/// can only be done for small integers: typically up to 3 bits, but it depends
33/// on the number of bits available according to PointerLikeTypeTraits for the
34/// type.
35///
36/// Note that PointerIntPair always puts the IntVal part in the highest bits
37/// possible. For example, PointerIntPair<void*, 1, bool> will put the bit for
38/// the bool into bit #2, not bit #0, which allows the low two bits to be used
39/// for something else. For example, this allows:
40/// PointerIntPair<PointerIntPair<void*, 1, bool>, 1, bool>
41/// ... and the two bools will land in different bits.
42template <typename PointerTy, unsigned IntBits, typename IntType = unsigned,
43 typename PtrTraits = PointerLikeTypeTraits<PointerTy>,
44 typename Info = PointerIntPairInfo<PointerTy, IntBits, PtrTraits>>
45class PointerIntPair {
46 // Used by MSVC visualizer and generally helpful for debugging/visualizing.
47 using InfoTy = Info;
48 intptr_t Value = 0;
49
50public:
51 constexpr PointerIntPair() = default;
52
53 PointerIntPair(PointerTy PtrVal, IntType IntVal) {
54 setPointerAndInt(PtrVal, IntVal);
55 }
56
57 explicit PointerIntPair(PointerTy PtrVal) { initWithPointer(PtrVal); }
58
59 PointerTy getPointer() const { return Info::getPointer(Value); }
14
Calling 'PointerIntPairInfo::getPointer'
22
Returning from 'PointerIntPairInfo::getPointer'
23
Returning null pointer, which participates in a condition later
60
61 IntType getInt() const { return (IntType)Info::getInt(Value); }
62
63 void setPointer(PointerTy PtrVal) LLVM_LVALUE_FUNCTION& {
64 Value = Info::updatePointer(Value, PtrVal);
65 }
66
67 void setInt(IntType IntVal) LLVM_LVALUE_FUNCTION& {
68 Value = Info::updateInt(Value, static_cast<intptr_t>(IntVal));
69 }
70
71 void initWithPointer(PointerTy PtrVal) LLVM_LVALUE_FUNCTION& {
72 Value = Info::updatePointer(0, PtrVal);
73 }
74
75 void setPointerAndInt(PointerTy PtrVal, IntType IntVal) LLVM_LVALUE_FUNCTION& {
76 Value = Info::updateInt(Info::updatePointer(0, PtrVal),
77 static_cast<intptr_t>(IntVal));
78 }
79
80 PointerTy const *getAddrOfPointer() const {
81 return const_cast<PointerIntPair *>(this)->getAddrOfPointer();
82 }
83
84 PointerTy *getAddrOfPointer() {
85 assert(Value == reinterpret_cast<intptr_t>(getPointer()) &&((Value == reinterpret_cast<intptr_t>(getPointer()) &&
"Can only return the address if IntBits is cleared and " "PtrTraits doesn't change the pointer"
) ? static_cast<void> (0) : __assert_fail ("Value == reinterpret_cast<intptr_t>(getPointer()) && \"Can only return the address if IntBits is cleared and \" \"PtrTraits doesn't change the pointer\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/ADT/PointerIntPair.h"
, 87, __PRETTY_FUNCTION__))
86 "Can only return the address if IntBits is cleared and "((Value == reinterpret_cast<intptr_t>(getPointer()) &&
"Can only return the address if IntBits is cleared and " "PtrTraits doesn't change the pointer"
) ? static_cast<void> (0) : __assert_fail ("Value == reinterpret_cast<intptr_t>(getPointer()) && \"Can only return the address if IntBits is cleared and \" \"PtrTraits doesn't change the pointer\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/ADT/PointerIntPair.h"
, 87, __PRETTY_FUNCTION__))
87 "PtrTraits doesn't change the pointer")((Value == reinterpret_cast<intptr_t>(getPointer()) &&
"Can only return the address if IntBits is cleared and " "PtrTraits doesn't change the pointer"
) ? static_cast<void> (0) : __assert_fail ("Value == reinterpret_cast<intptr_t>(getPointer()) && \"Can only return the address if IntBits is cleared and \" \"PtrTraits doesn't change the pointer\""
, "/build/llvm-toolchain-snapshot-10~++20200112100611+7fa5290d5bd/llvm/include/llvm/ADT/PointerIntPair.h"
, 87, __PRETTY_FUNCTION__))
;
88 return reinterpret_cast<PointerTy *>(&Value);
89 }
90
91 void *getOpaqueValue() const { return reinterpret_cast<void *>(Value); }
92
93 void setFromOpaqueValue(void *Val) LLVM_LVALUE_FUNCTION& {
94 Value = reinterpret_cast<intptr_t>(Val);
95 }
96
97 static PointerIntPair getFromOpaqueValue(void *V) {
98 PointerIntPair P;
99 P.setFromOpaqueValue(V);
100 return P;
101 }
102
103 // Allow PointerIntPairs to be created from const void * if and only if the
104 // pointer type could be created from a const void *.
105 static PointerIntPair getFromOpaqueValue(const void *V) {
106 (void)PtrTraits::getFromVoidPointer(V);
107 return getFromOpaqueValue(const_cast<void *>(V));
108 }
109
110 bool operator==(const PointerIntPair &RHS) const {
111 return Value == RHS.Value;
112 }
113
114 bool operator!=(const PointerIntPair &RHS) const {
115 return Value != RHS.Value;
116 }
117
118 bool operator<(const PointerIntPair &RHS) const { return Value < RHS.Value; }
119 bool operator>(const PointerIntPair &RHS) const { return Value > RHS.Value; }
120
121 bool operator<=(const PointerIntPair &RHS) const {
122 return Value <= RHS.Value;
123 }
124
125 bool operator>=(const PointerIntPair &RHS) const {
126 return Value >= RHS.Value;
127 }
128};
129
130// Specialize is_trivially_copyable to avoid limitation of llvm::is_trivially_copyable
131// when compiled with gcc 4.9.
132template <typename PointerTy, unsigned IntBits, typename IntType,
133 typename PtrTraits,
134 typename Info>
135struct is_trivially_copyable<PointerIntPair<PointerTy, IntBits, IntType, PtrTraits, Info>> : std::true_type {
136#ifdef HAVE_STD_IS_TRIVIALLY_COPYABLE
137 static_assert(std::is_trivially_copyable<PointerIntPair<PointerTy, IntBits, IntType, PtrTraits, Info>>::value,
138 "inconsistent behavior between llvm:: and std:: implementation of is_trivially_copyable");
139#endif
140};
141
142
143template <typename PointerT, unsigned IntBits, typename PtrTraits>
144struct PointerIntPairInfo {
145 static_assert(PtrTraits::NumLowBitsAvailable <
146 std::numeric_limits<uintptr_t>::digits,
147 "cannot use a pointer type that has all bits free");
148 static_assert(IntBits <= PtrTraits::NumLowBitsAvailable,
149 "PointerIntPair with integer size too large for pointer");
150 enum : uintptr_t {
151 /// PointerBitMask - The bits that come from the pointer.
152 PointerBitMask =
153 ~(uintptr_t)(((intptr_t)1 << PtrTraits::NumLowBitsAvailable) - 1),
154
155 /// IntShift - The number of low bits that we reserve for other uses, and
156 /// keep zero.
157 IntShift = (uintptr_t)PtrTraits::NumLowBitsAvailable - IntBits,
158
159 /// IntMask - This is the unshifted mask for valid bits of the int type.
160 IntMask = (uintptr_t)(((intptr_t)1 << IntBits) - 1),
161
162 // ShiftedIntMask - This is the bits for the integer shifted in place.
163 ShiftedIntMask = (uintptr_t)(IntMask << IntShift)
164 };
165
166 static PointerT getPointer(intptr_t Value) {
167 return PtrTraits::getFromVoidPointer(
15
Calling 'PointerLikeTypeTraits::getFromVoidPointer'
20
Returning from 'PointerLikeTypeTraits::getFromVoidPointer'
21
Returning null pointer, which participates in a condition later
168