File: | llvm/lib/Target/X86/X86ISelLowering.cpp |
Warning: | line 16384, column 21 The result of the '/' expression is undefined |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===// | |||
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 interfaces that X86 uses to lower LLVM code into a | |||
10 | // selection DAG. | |||
11 | // | |||
12 | //===----------------------------------------------------------------------===// | |||
13 | ||||
14 | #include "X86ISelLowering.h" | |||
15 | #include "MCTargetDesc/X86ShuffleDecode.h" | |||
16 | #include "X86.h" | |||
17 | #include "X86CallingConv.h" | |||
18 | #include "X86FrameLowering.h" | |||
19 | #include "X86InstrBuilder.h" | |||
20 | #include "X86IntrinsicsInfo.h" | |||
21 | #include "X86MachineFunctionInfo.h" | |||
22 | #include "X86TargetMachine.h" | |||
23 | #include "X86TargetObjectFile.h" | |||
24 | #include "llvm/ADT/SmallBitVector.h" | |||
25 | #include "llvm/ADT/SmallSet.h" | |||
26 | #include "llvm/ADT/Statistic.h" | |||
27 | #include "llvm/ADT/StringExtras.h" | |||
28 | #include "llvm/ADT/StringSwitch.h" | |||
29 | #include "llvm/Analysis/BlockFrequencyInfo.h" | |||
30 | #include "llvm/Analysis/EHPersonalities.h" | |||
31 | #include "llvm/Analysis/ObjCARCUtil.h" | |||
32 | #include "llvm/Analysis/ProfileSummaryInfo.h" | |||
33 | #include "llvm/Analysis/VectorUtils.h" | |||
34 | #include "llvm/CodeGen/IntrinsicLowering.h" | |||
35 | #include "llvm/CodeGen/MachineFrameInfo.h" | |||
36 | #include "llvm/CodeGen/MachineFunction.h" | |||
37 | #include "llvm/CodeGen/MachineInstrBuilder.h" | |||
38 | #include "llvm/CodeGen/MachineJumpTableInfo.h" | |||
39 | #include "llvm/CodeGen/MachineLoopInfo.h" | |||
40 | #include "llvm/CodeGen/MachineModuleInfo.h" | |||
41 | #include "llvm/CodeGen/MachineRegisterInfo.h" | |||
42 | #include "llvm/CodeGen/TargetLowering.h" | |||
43 | #include "llvm/CodeGen/WinEHFuncInfo.h" | |||
44 | #include "llvm/IR/CallingConv.h" | |||
45 | #include "llvm/IR/Constants.h" | |||
46 | #include "llvm/IR/DerivedTypes.h" | |||
47 | #include "llvm/IR/DiagnosticInfo.h" | |||
48 | #include "llvm/IR/Function.h" | |||
49 | #include "llvm/IR/GlobalAlias.h" | |||
50 | #include "llvm/IR/GlobalVariable.h" | |||
51 | #include "llvm/IR/IRBuilder.h" | |||
52 | #include "llvm/IR/Instructions.h" | |||
53 | #include "llvm/IR/Intrinsics.h" | |||
54 | #include "llvm/IR/PatternMatch.h" | |||
55 | #include "llvm/MC/MCAsmInfo.h" | |||
56 | #include "llvm/MC/MCContext.h" | |||
57 | #include "llvm/MC/MCExpr.h" | |||
58 | #include "llvm/MC/MCSymbol.h" | |||
59 | #include "llvm/Support/CommandLine.h" | |||
60 | #include "llvm/Support/Debug.h" | |||
61 | #include "llvm/Support/ErrorHandling.h" | |||
62 | #include "llvm/Support/KnownBits.h" | |||
63 | #include "llvm/Support/MathExtras.h" | |||
64 | #include "llvm/Target/TargetOptions.h" | |||
65 | #include <algorithm> | |||
66 | #include <bitset> | |||
67 | #include <cctype> | |||
68 | #include <numeric> | |||
69 | using namespace llvm; | |||
70 | ||||
71 | #define DEBUG_TYPE"x86-isel" "x86-isel" | |||
72 | ||||
73 | STATISTIC(NumTailCalls, "Number of tail calls")static llvm::Statistic NumTailCalls = {"x86-isel", "NumTailCalls" , "Number of tail calls"}; | |||
74 | ||||
75 | static cl::opt<int> ExperimentalPrefInnermostLoopAlignment( | |||
76 | "x86-experimental-pref-innermost-loop-alignment", cl::init(4), | |||
77 | cl::desc( | |||
78 | "Sets the preferable loop alignment for experiments (as log2 bytes) " | |||
79 | "for innermost loops only. If specified, this option overrides " | |||
80 | "alignment set by x86-experimental-pref-loop-alignment."), | |||
81 | cl::Hidden); | |||
82 | ||||
83 | static cl::opt<bool> MulConstantOptimization( | |||
84 | "mul-constant-optimization", cl::init(true), | |||
85 | cl::desc("Replace 'mul x, Const' with more effective instructions like " | |||
86 | "SHIFT, LEA, etc."), | |||
87 | cl::Hidden); | |||
88 | ||||
89 | static cl::opt<bool> ExperimentalUnorderedISEL( | |||
90 | "x86-experimental-unordered-atomic-isel", cl::init(false), | |||
91 | cl::desc("Use LoadSDNode and StoreSDNode instead of " | |||
92 | "AtomicSDNode for unordered atomic loads and " | |||
93 | "stores respectively."), | |||
94 | cl::Hidden); | |||
95 | ||||
96 | /// Call this when the user attempts to do something unsupported, like | |||
97 | /// returning a double without SSE2 enabled on x86_64. This is not fatal, unlike | |||
98 | /// report_fatal_error, so calling code should attempt to recover without | |||
99 | /// crashing. | |||
100 | static void errorUnsupported(SelectionDAG &DAG, const SDLoc &dl, | |||
101 | const char *Msg) { | |||
102 | MachineFunction &MF = DAG.getMachineFunction(); | |||
103 | DAG.getContext()->diagnose( | |||
104 | DiagnosticInfoUnsupported(MF.getFunction(), Msg, dl.getDebugLoc())); | |||
105 | } | |||
106 | ||||
107 | X86TargetLowering::X86TargetLowering(const X86TargetMachine &TM, | |||
108 | const X86Subtarget &STI) | |||
109 | : TargetLowering(TM), Subtarget(STI) { | |||
110 | bool UseX87 = !Subtarget.useSoftFloat() && Subtarget.hasX87(); | |||
111 | X86ScalarSSEf64 = Subtarget.hasSSE2(); | |||
112 | X86ScalarSSEf32 = Subtarget.hasSSE1(); | |||
113 | X86ScalarSSEf16 = Subtarget.hasFP16(); | |||
114 | MVT PtrVT = MVT::getIntegerVT(TM.getPointerSizeInBits(0)); | |||
115 | ||||
116 | // Set up the TargetLowering object. | |||
117 | ||||
118 | // X86 is weird. It always uses i8 for shift amounts and setcc results. | |||
119 | setBooleanContents(ZeroOrOneBooleanContent); | |||
120 | // X86-SSE is even stranger. It uses -1 or 0 for vector masks. | |||
121 | setBooleanVectorContents(ZeroOrNegativeOneBooleanContent); | |||
122 | ||||
123 | // For 64-bit, since we have so many registers, use the ILP scheduler. | |||
124 | // For 32-bit, use the register pressure specific scheduling. | |||
125 | // For Atom, always use ILP scheduling. | |||
126 | if (Subtarget.isAtom()) | |||
127 | setSchedulingPreference(Sched::ILP); | |||
128 | else if (Subtarget.is64Bit()) | |||
129 | setSchedulingPreference(Sched::ILP); | |||
130 | else | |||
131 | setSchedulingPreference(Sched::RegPressure); | |||
132 | const X86RegisterInfo *RegInfo = Subtarget.getRegisterInfo(); | |||
133 | setStackPointerRegisterToSaveRestore(RegInfo->getStackRegister()); | |||
134 | ||||
135 | // Bypass expensive divides and use cheaper ones. | |||
136 | if (TM.getOptLevel() >= CodeGenOpt::Default) { | |||
137 | if (Subtarget.hasSlowDivide32()) | |||
138 | addBypassSlowDiv(32, 8); | |||
139 | if (Subtarget.hasSlowDivide64() && Subtarget.is64Bit()) | |||
140 | addBypassSlowDiv(64, 32); | |||
141 | } | |||
142 | ||||
143 | // Setup Windows compiler runtime calls. | |||
144 | if (Subtarget.isTargetWindowsMSVC() || Subtarget.isTargetWindowsItanium()) { | |||
145 | static const struct { | |||
146 | const RTLIB::Libcall Op; | |||
147 | const char * const Name; | |||
148 | const CallingConv::ID CC; | |||
149 | } LibraryCalls[] = { | |||
150 | { RTLIB::SDIV_I64, "_alldiv", CallingConv::X86_StdCall }, | |||
151 | { RTLIB::UDIV_I64, "_aulldiv", CallingConv::X86_StdCall }, | |||
152 | { RTLIB::SREM_I64, "_allrem", CallingConv::X86_StdCall }, | |||
153 | { RTLIB::UREM_I64, "_aullrem", CallingConv::X86_StdCall }, | |||
154 | { RTLIB::MUL_I64, "_allmul", CallingConv::X86_StdCall }, | |||
155 | }; | |||
156 | ||||
157 | for (const auto &LC : LibraryCalls) { | |||
158 | setLibcallName(LC.Op, LC.Name); | |||
159 | setLibcallCallingConv(LC.Op, LC.CC); | |||
160 | } | |||
161 | } | |||
162 | ||||
163 | if (Subtarget.getTargetTriple().isOSMSVCRT()) { | |||
164 | // MSVCRT doesn't have powi; fall back to pow | |||
165 | setLibcallName(RTLIB::POWI_F32, nullptr); | |||
166 | setLibcallName(RTLIB::POWI_F64, nullptr); | |||
167 | } | |||
168 | ||||
169 | // If we don't have cmpxchg8b(meaing this is a 386/486), limit atomic size to | |||
170 | // 32 bits so the AtomicExpandPass will expand it so we don't need cmpxchg8b. | |||
171 | // FIXME: Should we be limiting the atomic size on other configs? Default is | |||
172 | // 1024. | |||
173 | if (!Subtarget.hasCmpxchg8b()) | |||
174 | setMaxAtomicSizeInBitsSupported(32); | |||
175 | ||||
176 | // Set up the register classes. | |||
177 | addRegisterClass(MVT::i8, &X86::GR8RegClass); | |||
178 | addRegisterClass(MVT::i16, &X86::GR16RegClass); | |||
179 | addRegisterClass(MVT::i32, &X86::GR32RegClass); | |||
180 | if (Subtarget.is64Bit()) | |||
181 | addRegisterClass(MVT::i64, &X86::GR64RegClass); | |||
182 | ||||
183 | for (MVT VT : MVT::integer_valuetypes()) | |||
184 | setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote); | |||
185 | ||||
186 | // We don't accept any truncstore of integer registers. | |||
187 | setTruncStoreAction(MVT::i64, MVT::i32, Expand); | |||
188 | setTruncStoreAction(MVT::i64, MVT::i16, Expand); | |||
189 | setTruncStoreAction(MVT::i64, MVT::i8 , Expand); | |||
190 | setTruncStoreAction(MVT::i32, MVT::i16, Expand); | |||
191 | setTruncStoreAction(MVT::i32, MVT::i8 , Expand); | |||
192 | setTruncStoreAction(MVT::i16, MVT::i8, Expand); | |||
193 | ||||
194 | setTruncStoreAction(MVT::f64, MVT::f32, Expand); | |||
195 | ||||
196 | // SETOEQ and SETUNE require checking two conditions. | |||
197 | for (auto VT : {MVT::f32, MVT::f64, MVT::f80}) { | |||
198 | setCondCodeAction(ISD::SETOEQ, VT, Expand); | |||
199 | setCondCodeAction(ISD::SETUNE, VT, Expand); | |||
200 | } | |||
201 | ||||
202 | // Integer absolute. | |||
203 | if (Subtarget.hasCMov()) { | |||
204 | setOperationAction(ISD::ABS , MVT::i16 , Custom); | |||
205 | setOperationAction(ISD::ABS , MVT::i32 , Custom); | |||
206 | if (Subtarget.is64Bit()) | |||
207 | setOperationAction(ISD::ABS , MVT::i64 , Custom); | |||
208 | } | |||
209 | ||||
210 | // Signed saturation subtraction. | |||
211 | setOperationAction(ISD::SSUBSAT , MVT::i8 , Custom); | |||
212 | setOperationAction(ISD::SSUBSAT , MVT::i16 , Custom); | |||
213 | setOperationAction(ISD::SSUBSAT , MVT::i32 , Custom); | |||
214 | if (Subtarget.is64Bit()) | |||
215 | setOperationAction(ISD::SSUBSAT , MVT::i64 , Custom); | |||
216 | ||||
217 | // Funnel shifts. | |||
218 | for (auto ShiftOp : {ISD::FSHL, ISD::FSHR}) { | |||
219 | // For slow shld targets we only lower for code size. | |||
220 | LegalizeAction ShiftDoubleAction = Subtarget.isSHLDSlow() ? Custom : Legal; | |||
221 | ||||
222 | setOperationAction(ShiftOp , MVT::i8 , Custom); | |||
223 | setOperationAction(ShiftOp , MVT::i16 , Custom); | |||
224 | setOperationAction(ShiftOp , MVT::i32 , ShiftDoubleAction); | |||
225 | if (Subtarget.is64Bit()) | |||
226 | setOperationAction(ShiftOp , MVT::i64 , ShiftDoubleAction); | |||
227 | } | |||
228 | ||||
229 | if (!Subtarget.useSoftFloat()) { | |||
230 | // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this | |||
231 | // operation. | |||
232 | setOperationAction(ISD::UINT_TO_FP, MVT::i8, Promote); | |||
233 | setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::i8, Promote); | |||
234 | setOperationAction(ISD::UINT_TO_FP, MVT::i16, Promote); | |||
235 | setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::i16, Promote); | |||
236 | // We have an algorithm for SSE2, and we turn this into a 64-bit | |||
237 | // FILD or VCVTUSI2SS/SD for other targets. | |||
238 | setOperationAction(ISD::UINT_TO_FP, MVT::i32, Custom); | |||
239 | setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::i32, Custom); | |||
240 | // We have an algorithm for SSE2->double, and we turn this into a | |||
241 | // 64-bit FILD followed by conditional FADD for other targets. | |||
242 | setOperationAction(ISD::UINT_TO_FP, MVT::i64, Custom); | |||
243 | setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::i64, Custom); | |||
244 | ||||
245 | // Promote i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have | |||
246 | // this operation. | |||
247 | setOperationAction(ISD::SINT_TO_FP, MVT::i8, Promote); | |||
248 | setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::i8, Promote); | |||
249 | // SSE has no i16 to fp conversion, only i32. We promote in the handler | |||
250 | // to allow f80 to use i16 and f64 to use i16 with sse1 only | |||
251 | setOperationAction(ISD::SINT_TO_FP, MVT::i16, Custom); | |||
252 | setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::i16, Custom); | |||
253 | // f32 and f64 cases are Legal with SSE1/SSE2, f80 case is not | |||
254 | setOperationAction(ISD::SINT_TO_FP, MVT::i32, Custom); | |||
255 | setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::i32, Custom); | |||
256 | // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64 | |||
257 | // are Legal, f80 is custom lowered. | |||
258 | setOperationAction(ISD::SINT_TO_FP, MVT::i64, Custom); | |||
259 | setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::i64, Custom); | |||
260 | ||||
261 | // Promote i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have | |||
262 | // this operation. | |||
263 | setOperationAction(ISD::FP_TO_SINT, MVT::i8, Promote); | |||
264 | // FIXME: This doesn't generate invalid exception when it should. PR44019. | |||
265 | setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::i8, Promote); | |||
266 | setOperationAction(ISD::FP_TO_SINT, MVT::i16, Custom); | |||
267 | setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::i16, Custom); | |||
268 | setOperationAction(ISD::FP_TO_SINT, MVT::i32, Custom); | |||
269 | setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::i32, Custom); | |||
270 | // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64 | |||
271 | // are Legal, f80 is custom lowered. | |||
272 | setOperationAction(ISD::FP_TO_SINT, MVT::i64, Custom); | |||
273 | setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::i64, Custom); | |||
274 | ||||
275 | // Handle FP_TO_UINT by promoting the destination to a larger signed | |||
276 | // conversion. | |||
277 | setOperationAction(ISD::FP_TO_UINT, MVT::i8, Promote); | |||
278 | // FIXME: This doesn't generate invalid exception when it should. PR44019. | |||
279 | setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::i8, Promote); | |||
280 | setOperationAction(ISD::FP_TO_UINT, MVT::i16, Promote); | |||
281 | // FIXME: This doesn't generate invalid exception when it should. PR44019. | |||
282 | setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::i16, Promote); | |||
283 | setOperationAction(ISD::FP_TO_UINT, MVT::i32, Custom); | |||
284 | setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::i32, Custom); | |||
285 | setOperationAction(ISD::FP_TO_UINT, MVT::i64, Custom); | |||
286 | setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::i64, Custom); | |||
287 | ||||
288 | setOperationAction(ISD::LRINT, MVT::f32, Custom); | |||
289 | setOperationAction(ISD::LRINT, MVT::f64, Custom); | |||
290 | setOperationAction(ISD::LLRINT, MVT::f32, Custom); | |||
291 | setOperationAction(ISD::LLRINT, MVT::f64, Custom); | |||
292 | ||||
293 | if (!Subtarget.is64Bit()) { | |||
294 | setOperationAction(ISD::LRINT, MVT::i64, Custom); | |||
295 | setOperationAction(ISD::LLRINT, MVT::i64, Custom); | |||
296 | } | |||
297 | } | |||
298 | ||||
299 | if (Subtarget.hasSSE2()) { | |||
300 | // Custom lowering for saturating float to int conversions. | |||
301 | // We handle promotion to larger result types manually. | |||
302 | for (MVT VT : { MVT::i8, MVT::i16, MVT::i32 }) { | |||
303 | setOperationAction(ISD::FP_TO_UINT_SAT, VT, Custom); | |||
304 | setOperationAction(ISD::FP_TO_SINT_SAT, VT, Custom); | |||
305 | } | |||
306 | if (Subtarget.is64Bit()) { | |||
307 | setOperationAction(ISD::FP_TO_UINT_SAT, MVT::i64, Custom); | |||
308 | setOperationAction(ISD::FP_TO_SINT_SAT, MVT::i64, Custom); | |||
309 | } | |||
310 | } | |||
311 | ||||
312 | // Handle address space casts between mixed sized pointers. | |||
313 | setOperationAction(ISD::ADDRSPACECAST, MVT::i32, Custom); | |||
314 | setOperationAction(ISD::ADDRSPACECAST, MVT::i64, Custom); | |||
315 | ||||
316 | // TODO: when we have SSE, these could be more efficient, by using movd/movq. | |||
317 | if (!X86ScalarSSEf64) { | |||
318 | setOperationAction(ISD::BITCAST , MVT::f32 , Expand); | |||
319 | setOperationAction(ISD::BITCAST , MVT::i32 , Expand); | |||
320 | if (Subtarget.is64Bit()) { | |||
321 | setOperationAction(ISD::BITCAST , MVT::f64 , Expand); | |||
322 | // Without SSE, i64->f64 goes through memory. | |||
323 | setOperationAction(ISD::BITCAST , MVT::i64 , Expand); | |||
324 | } | |||
325 | } else if (!Subtarget.is64Bit()) | |||
326 | setOperationAction(ISD::BITCAST , MVT::i64 , Custom); | |||
327 | ||||
328 | // Scalar integer divide and remainder are lowered to use operations that | |||
329 | // produce two results, to match the available instructions. This exposes | |||
330 | // the two-result form to trivial CSE, which is able to combine x/y and x%y | |||
331 | // into a single instruction. | |||
332 | // | |||
333 | // Scalar integer multiply-high is also lowered to use two-result | |||
334 | // operations, to match the available instructions. However, plain multiply | |||
335 | // (low) operations are left as Legal, as there are single-result | |||
336 | // instructions for this in x86. Using the two-result multiply instructions | |||
337 | // when both high and low results are needed must be arranged by dagcombine. | |||
338 | for (auto VT : { MVT::i8, MVT::i16, MVT::i32, MVT::i64 }) { | |||
339 | setOperationAction(ISD::MULHS, VT, Expand); | |||
340 | setOperationAction(ISD::MULHU, VT, Expand); | |||
341 | setOperationAction(ISD::SDIV, VT, Expand); | |||
342 | setOperationAction(ISD::UDIV, VT, Expand); | |||
343 | setOperationAction(ISD::SREM, VT, Expand); | |||
344 | setOperationAction(ISD::UREM, VT, Expand); | |||
345 | } | |||
346 | ||||
347 | setOperationAction(ISD::BR_JT , MVT::Other, Expand); | |||
348 | setOperationAction(ISD::BRCOND , MVT::Other, Custom); | |||
349 | for (auto VT : { MVT::f32, MVT::f64, MVT::f80, MVT::f128, | |||
350 | MVT::i8, MVT::i16, MVT::i32, MVT::i64 }) { | |||
351 | setOperationAction(ISD::BR_CC, VT, Expand); | |||
352 | setOperationAction(ISD::SELECT_CC, VT, Expand); | |||
353 | } | |||
354 | if (Subtarget.is64Bit()) | |||
355 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal); | |||
356 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal); | |||
357 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal); | |||
358 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand); | |||
359 | ||||
360 | setOperationAction(ISD::FREM , MVT::f32 , Expand); | |||
361 | setOperationAction(ISD::FREM , MVT::f64 , Expand); | |||
362 | setOperationAction(ISD::FREM , MVT::f80 , Expand); | |||
363 | setOperationAction(ISD::FREM , MVT::f128 , Expand); | |||
364 | ||||
365 | if (!Subtarget.useSoftFloat() && Subtarget.hasX87()) { | |||
366 | setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom); | |||
367 | setOperationAction(ISD::SET_ROUNDING , MVT::Other, Custom); | |||
368 | } | |||
369 | ||||
370 | // Promote the i8 variants and force them on up to i32 which has a shorter | |||
371 | // encoding. | |||
372 | setOperationPromotedToType(ISD::CTTZ , MVT::i8 , MVT::i32); | |||
373 | setOperationPromotedToType(ISD::CTTZ_ZERO_UNDEF, MVT::i8 , MVT::i32); | |||
374 | ||||
375 | if (Subtarget.hasBMI()) { | |||
376 | // Promote the i16 zero undef variant and force it on up to i32 when tzcnt | |||
377 | // is enabled. | |||
378 | setOperationPromotedToType(ISD::CTTZ_ZERO_UNDEF, MVT::i16, MVT::i32); | |||
379 | } else { | |||
380 | setOperationAction(ISD::CTTZ, MVT::i16, Custom); | |||
381 | setOperationAction(ISD::CTTZ , MVT::i32 , Custom); | |||
382 | setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16 , Legal); | |||
383 | setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32 , Legal); | |||
384 | if (Subtarget.is64Bit()) { | |||
385 | setOperationAction(ISD::CTTZ , MVT::i64 , Custom); | |||
386 | setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Legal); | |||
387 | } | |||
388 | } | |||
389 | ||||
390 | if (Subtarget.hasLZCNT()) { | |||
391 | // When promoting the i8 variants, force them to i32 for a shorter | |||
392 | // encoding. | |||
393 | setOperationPromotedToType(ISD::CTLZ , MVT::i8 , MVT::i32); | |||
394 | setOperationPromotedToType(ISD::CTLZ_ZERO_UNDEF, MVT::i8 , MVT::i32); | |||
395 | } else { | |||
396 | for (auto VT : {MVT::i8, MVT::i16, MVT::i32, MVT::i64}) { | |||
397 | if (VT == MVT::i64 && !Subtarget.is64Bit()) | |||
398 | continue; | |||
399 | setOperationAction(ISD::CTLZ , VT, Custom); | |||
400 | setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Custom); | |||
401 | } | |||
402 | } | |||
403 | ||||
404 | for (auto Op : {ISD::FP16_TO_FP, ISD::STRICT_FP16_TO_FP, ISD::FP_TO_FP16, | |||
405 | ISD::STRICT_FP_TO_FP16}) { | |||
406 | // Special handling for half-precision floating point conversions. | |||
407 | // If we don't have F16C support, then lower half float conversions | |||
408 | // into library calls. | |||
409 | setOperationAction( | |||
410 | Op, MVT::f32, | |||
411 | (!Subtarget.useSoftFloat() && Subtarget.hasF16C()) ? Custom : Expand); | |||
412 | // There's never any support for operations beyond MVT::f32. | |||
413 | setOperationAction(Op, MVT::f64, Expand); | |||
414 | setOperationAction(Op, MVT::f80, Expand); | |||
415 | setOperationAction(Op, MVT::f128, Expand); | |||
416 | } | |||
417 | ||||
418 | setLoadExtAction(ISD::EXTLOAD, MVT::f32, MVT::f16, Expand); | |||
419 | setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f16, Expand); | |||
420 | setLoadExtAction(ISD::EXTLOAD, MVT::f80, MVT::f16, Expand); | |||
421 | setLoadExtAction(ISD::EXTLOAD, MVT::f128, MVT::f16, Expand); | |||
422 | setTruncStoreAction(MVT::f32, MVT::f16, Expand); | |||
423 | setTruncStoreAction(MVT::f64, MVT::f16, Expand); | |||
424 | setTruncStoreAction(MVT::f80, MVT::f16, Expand); | |||
425 | setTruncStoreAction(MVT::f128, MVT::f16, Expand); | |||
426 | ||||
427 | setOperationAction(ISD::PARITY, MVT::i8, Custom); | |||
428 | setOperationAction(ISD::PARITY, MVT::i16, Custom); | |||
429 | setOperationAction(ISD::PARITY, MVT::i32, Custom); | |||
430 | if (Subtarget.is64Bit()) | |||
431 | setOperationAction(ISD::PARITY, MVT::i64, Custom); | |||
432 | if (Subtarget.hasPOPCNT()) { | |||
433 | setOperationPromotedToType(ISD::CTPOP, MVT::i8, MVT::i32); | |||
434 | } else { | |||
435 | setOperationAction(ISD::CTPOP , MVT::i8 , Expand); | |||
436 | setOperationAction(ISD::CTPOP , MVT::i16 , Expand); | |||
437 | setOperationAction(ISD::CTPOP , MVT::i32 , Expand); | |||
438 | if (Subtarget.is64Bit()) | |||
439 | setOperationAction(ISD::CTPOP , MVT::i64 , Expand); | |||
440 | else | |||
441 | setOperationAction(ISD::CTPOP , MVT::i64 , Custom); | |||
442 | } | |||
443 | ||||
444 | setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom); | |||
445 | ||||
446 | if (!Subtarget.hasMOVBE()) | |||
447 | setOperationAction(ISD::BSWAP , MVT::i16 , Expand); | |||
448 | ||||
449 | // X86 wants to expand cmov itself. | |||
450 | for (auto VT : { MVT::f32, MVT::f64, MVT::f80, MVT::f128 }) { | |||
451 | setOperationAction(ISD::SELECT, VT, Custom); | |||
452 | setOperationAction(ISD::SETCC, VT, Custom); | |||
453 | setOperationAction(ISD::STRICT_FSETCC, VT, Custom); | |||
454 | setOperationAction(ISD::STRICT_FSETCCS, VT, Custom); | |||
455 | } | |||
456 | for (auto VT : { MVT::i8, MVT::i16, MVT::i32, MVT::i64 }) { | |||
457 | if (VT == MVT::i64 && !Subtarget.is64Bit()) | |||
458 | continue; | |||
459 | setOperationAction(ISD::SELECT, VT, Custom); | |||
460 | setOperationAction(ISD::SETCC, VT, Custom); | |||
461 | } | |||
462 | ||||
463 | // Custom action for SELECT MMX and expand action for SELECT_CC MMX | |||
464 | setOperationAction(ISD::SELECT, MVT::x86mmx, Custom); | |||
465 | setOperationAction(ISD::SELECT_CC, MVT::x86mmx, Expand); | |||
466 | ||||
467 | setOperationAction(ISD::EH_RETURN , MVT::Other, Custom); | |||
468 | // NOTE: EH_SJLJ_SETJMP/_LONGJMP are not recommended, since | |||
469 | // LLVM/Clang supports zero-cost DWARF and SEH exception handling. | |||
470 | setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom); | |||
471 | setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom); | |||
472 | setOperationAction(ISD::EH_SJLJ_SETUP_DISPATCH, MVT::Other, Custom); | |||
473 | if (TM.Options.ExceptionModel == ExceptionHandling::SjLj) | |||
474 | setLibcallName(RTLIB::UNWIND_RESUME, "_Unwind_SjLj_Resume"); | |||
475 | ||||
476 | // Darwin ABI issue. | |||
477 | for (auto VT : { MVT::i32, MVT::i64 }) { | |||
478 | if (VT == MVT::i64 && !Subtarget.is64Bit()) | |||
479 | continue; | |||
480 | setOperationAction(ISD::ConstantPool , VT, Custom); | |||
481 | setOperationAction(ISD::JumpTable , VT, Custom); | |||
482 | setOperationAction(ISD::GlobalAddress , VT, Custom); | |||
483 | setOperationAction(ISD::GlobalTLSAddress, VT, Custom); | |||
484 | setOperationAction(ISD::ExternalSymbol , VT, Custom); | |||
485 | setOperationAction(ISD::BlockAddress , VT, Custom); | |||
486 | } | |||
487 | ||||
488 | // 64-bit shl, sra, srl (iff 32-bit x86) | |||
489 | for (auto VT : { MVT::i32, MVT::i64 }) { | |||
490 | if (VT == MVT::i64 && !Subtarget.is64Bit()) | |||
491 | continue; | |||
492 | setOperationAction(ISD::SHL_PARTS, VT, Custom); | |||
493 | setOperationAction(ISD::SRA_PARTS, VT, Custom); | |||
494 | setOperationAction(ISD::SRL_PARTS, VT, Custom); | |||
495 | } | |||
496 | ||||
497 | if (Subtarget.hasSSEPrefetch() || Subtarget.has3DNow()) | |||
498 | setOperationAction(ISD::PREFETCH , MVT::Other, Legal); | |||
499 | ||||
500 | setOperationAction(ISD::ATOMIC_FENCE , MVT::Other, Custom); | |||
501 | ||||
502 | // Expand certain atomics | |||
503 | for (auto VT : { MVT::i8, MVT::i16, MVT::i32, MVT::i64 }) { | |||
504 | setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, VT, Custom); | |||
505 | setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom); | |||
506 | setOperationAction(ISD::ATOMIC_LOAD_ADD, VT, Custom); | |||
507 | setOperationAction(ISD::ATOMIC_LOAD_OR, VT, Custom); | |||
508 | setOperationAction(ISD::ATOMIC_LOAD_XOR, VT, Custom); | |||
509 | setOperationAction(ISD::ATOMIC_LOAD_AND, VT, Custom); | |||
510 | setOperationAction(ISD::ATOMIC_STORE, VT, Custom); | |||
511 | } | |||
512 | ||||
513 | if (!Subtarget.is64Bit()) | |||
514 | setOperationAction(ISD::ATOMIC_LOAD, MVT::i64, Custom); | |||
515 | ||||
516 | if (Subtarget.hasCmpxchg16b()) { | |||
517 | setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i128, Custom); | |||
518 | } | |||
519 | ||||
520 | // FIXME - use subtarget debug flags | |||
521 | if (!Subtarget.isTargetDarwin() && !Subtarget.isTargetELF() && | |||
522 | !Subtarget.isTargetCygMing() && !Subtarget.isTargetWin64() && | |||
523 | TM.Options.ExceptionModel != ExceptionHandling::SjLj) { | |||
524 | setOperationAction(ISD::EH_LABEL, MVT::Other, Expand); | |||
525 | } | |||
526 | ||||
527 | setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom); | |||
528 | setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom); | |||
529 | ||||
530 | setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom); | |||
531 | setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom); | |||
532 | ||||
533 | setOperationAction(ISD::TRAP, MVT::Other, Legal); | |||
534 | setOperationAction(ISD::DEBUGTRAP, MVT::Other, Legal); | |||
535 | if (Subtarget.getTargetTriple().isPS4CPU()) | |||
536 | setOperationAction(ISD::UBSANTRAP, MVT::Other, Expand); | |||
537 | else | |||
538 | setOperationAction(ISD::UBSANTRAP, MVT::Other, Legal); | |||
539 | ||||
540 | // VASTART needs to be custom lowered to use the VarArgsFrameIndex | |||
541 | setOperationAction(ISD::VASTART , MVT::Other, Custom); | |||
542 | setOperationAction(ISD::VAEND , MVT::Other, Expand); | |||
543 | bool Is64Bit = Subtarget.is64Bit(); | |||
544 | setOperationAction(ISD::VAARG, MVT::Other, Is64Bit ? Custom : Expand); | |||
545 | setOperationAction(ISD::VACOPY, MVT::Other, Is64Bit ? Custom : Expand); | |||
546 | ||||
547 | setOperationAction(ISD::STACKSAVE, MVT::Other, Expand); | |||
548 | setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand); | |||
549 | ||||
550 | setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom); | |||
551 | ||||
552 | // GC_TRANSITION_START and GC_TRANSITION_END need custom lowering. | |||
553 | setOperationAction(ISD::GC_TRANSITION_START, MVT::Other, Custom); | |||
554 | setOperationAction(ISD::GC_TRANSITION_END, MVT::Other, Custom); | |||
555 | ||||
556 | if (!Subtarget.useSoftFloat() && X86ScalarSSEf64) { | |||
557 | // f32 and f64 use SSE. | |||
558 | // Set up the FP register classes. | |||
559 | addRegisterClass(MVT::f32, Subtarget.hasAVX512() ? &X86::FR32XRegClass | |||
560 | : &X86::FR32RegClass); | |||
561 | addRegisterClass(MVT::f64, Subtarget.hasAVX512() ? &X86::FR64XRegClass | |||
562 | : &X86::FR64RegClass); | |||
563 | ||||
564 | // Disable f32->f64 extload as we can only generate this in one instruction | |||
565 | // under optsize. So its easier to pattern match (fpext (load)) for that | |||
566 | // case instead of needing to emit 2 instructions for extload in the | |||
567 | // non-optsize case. | |||
568 | setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f32, Expand); | |||
569 | ||||
570 | for (auto VT : { MVT::f32, MVT::f64 }) { | |||
571 | // Use ANDPD to simulate FABS. | |||
572 | setOperationAction(ISD::FABS, VT, Custom); | |||
573 | ||||
574 | // Use XORP to simulate FNEG. | |||
575 | setOperationAction(ISD::FNEG, VT, Custom); | |||
576 | ||||
577 | // Use ANDPD and ORPD to simulate FCOPYSIGN. | |||
578 | setOperationAction(ISD::FCOPYSIGN, VT, Custom); | |||
579 | ||||
580 | // These might be better off as horizontal vector ops. | |||
581 | setOperationAction(ISD::FADD, VT, Custom); | |||
582 | setOperationAction(ISD::FSUB, VT, Custom); | |||
583 | ||||
584 | // We don't support sin/cos/fmod | |||
585 | setOperationAction(ISD::FSIN , VT, Expand); | |||
586 | setOperationAction(ISD::FCOS , VT, Expand); | |||
587 | setOperationAction(ISD::FSINCOS, VT, Expand); | |||
588 | } | |||
589 | ||||
590 | // Lower this to MOVMSK plus an AND. | |||
591 | setOperationAction(ISD::FGETSIGN, MVT::i64, Custom); | |||
592 | setOperationAction(ISD::FGETSIGN, MVT::i32, Custom); | |||
593 | ||||
594 | } else if (!Subtarget.useSoftFloat() && X86ScalarSSEf32 && | |||
595 | (UseX87 || Is64Bit)) { | |||
596 | // Use SSE for f32, x87 for f64. | |||
597 | // Set up the FP register classes. | |||
598 | addRegisterClass(MVT::f32, &X86::FR32RegClass); | |||
599 | if (UseX87) | |||
600 | addRegisterClass(MVT::f64, &X86::RFP64RegClass); | |||
601 | ||||
602 | // Use ANDPS to simulate FABS. | |||
603 | setOperationAction(ISD::FABS , MVT::f32, Custom); | |||
604 | ||||
605 | // Use XORP to simulate FNEG. | |||
606 | setOperationAction(ISD::FNEG , MVT::f32, Custom); | |||
607 | ||||
608 | if (UseX87) | |||
609 | setOperationAction(ISD::UNDEF, MVT::f64, Expand); | |||
610 | ||||
611 | // Use ANDPS and ORPS to simulate FCOPYSIGN. | |||
612 | if (UseX87) | |||
613 | setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand); | |||
614 | setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom); | |||
615 | ||||
616 | // We don't support sin/cos/fmod | |||
617 | setOperationAction(ISD::FSIN , MVT::f32, Expand); | |||
618 | setOperationAction(ISD::FCOS , MVT::f32, Expand); | |||
619 | setOperationAction(ISD::FSINCOS, MVT::f32, Expand); | |||
620 | ||||
621 | if (UseX87) { | |||
622 | // Always expand sin/cos functions even though x87 has an instruction. | |||
623 | setOperationAction(ISD::FSIN, MVT::f64, Expand); | |||
624 | setOperationAction(ISD::FCOS, MVT::f64, Expand); | |||
625 | setOperationAction(ISD::FSINCOS, MVT::f64, Expand); | |||
626 | } | |||
627 | } else if (UseX87) { | |||
628 | // f32 and f64 in x87. | |||
629 | // Set up the FP register classes. | |||
630 | addRegisterClass(MVT::f64, &X86::RFP64RegClass); | |||
631 | addRegisterClass(MVT::f32, &X86::RFP32RegClass); | |||
632 | ||||
633 | for (auto VT : { MVT::f32, MVT::f64 }) { | |||
634 | setOperationAction(ISD::UNDEF, VT, Expand); | |||
635 | setOperationAction(ISD::FCOPYSIGN, VT, Expand); | |||
636 | ||||
637 | // Always expand sin/cos functions even though x87 has an instruction. | |||
638 | setOperationAction(ISD::FSIN , VT, Expand); | |||
639 | setOperationAction(ISD::FCOS , VT, Expand); | |||
640 | setOperationAction(ISD::FSINCOS, VT, Expand); | |||
641 | } | |||
642 | } | |||
643 | ||||
644 | // Expand FP32 immediates into loads from the stack, save special cases. | |||
645 | if (isTypeLegal(MVT::f32)) { | |||
646 | if (UseX87 && (getRegClassFor(MVT::f32) == &X86::RFP32RegClass)) { | |||
647 | addLegalFPImmediate(APFloat(+0.0f)); // FLD0 | |||
648 | addLegalFPImmediate(APFloat(+1.0f)); // FLD1 | |||
649 | addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS | |||
650 | addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS | |||
651 | } else // SSE immediates. | |||
652 | addLegalFPImmediate(APFloat(+0.0f)); // xorps | |||
653 | } | |||
654 | // Expand FP64 immediates into loads from the stack, save special cases. | |||
655 | if (isTypeLegal(MVT::f64)) { | |||
656 | if (UseX87 && getRegClassFor(MVT::f64) == &X86::RFP64RegClass) { | |||
657 | addLegalFPImmediate(APFloat(+0.0)); // FLD0 | |||
658 | addLegalFPImmediate(APFloat(+1.0)); // FLD1 | |||
659 | addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS | |||
660 | addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS | |||
661 | } else // SSE immediates. | |||
662 | addLegalFPImmediate(APFloat(+0.0)); // xorpd | |||
663 | } | |||
664 | // Handle constrained floating-point operations of scalar. | |||
665 | setOperationAction(ISD::STRICT_FADD, MVT::f32, Legal); | |||
666 | setOperationAction(ISD::STRICT_FADD, MVT::f64, Legal); | |||
667 | setOperationAction(ISD::STRICT_FSUB, MVT::f32, Legal); | |||
668 | setOperationAction(ISD::STRICT_FSUB, MVT::f64, Legal); | |||
669 | setOperationAction(ISD::STRICT_FMUL, MVT::f32, Legal); | |||
670 | setOperationAction(ISD::STRICT_FMUL, MVT::f64, Legal); | |||
671 | setOperationAction(ISD::STRICT_FDIV, MVT::f32, Legal); | |||
672 | setOperationAction(ISD::STRICT_FDIV, MVT::f64, Legal); | |||
673 | setOperationAction(ISD::STRICT_FP_EXTEND, MVT::f64, Legal); | |||
674 | setOperationAction(ISD::STRICT_FP_ROUND, MVT::f32, Legal); | |||
675 | setOperationAction(ISD::STRICT_FP_ROUND, MVT::f64, Legal); | |||
676 | setOperationAction(ISD::STRICT_FSQRT, MVT::f32, Legal); | |||
677 | setOperationAction(ISD::STRICT_FSQRT, MVT::f64, Legal); | |||
678 | ||||
679 | // We don't support FMA. | |||
680 | setOperationAction(ISD::FMA, MVT::f64, Expand); | |||
681 | setOperationAction(ISD::FMA, MVT::f32, Expand); | |||
682 | ||||
683 | // f80 always uses X87. | |||
684 | if (UseX87) { | |||
685 | addRegisterClass(MVT::f80, &X86::RFP80RegClass); | |||
686 | setOperationAction(ISD::UNDEF, MVT::f80, Expand); | |||
687 | setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand); | |||
688 | { | |||
689 | APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended()); | |||
690 | addLegalFPImmediate(TmpFlt); // FLD0 | |||
691 | TmpFlt.changeSign(); | |||
692 | addLegalFPImmediate(TmpFlt); // FLD0/FCHS | |||
693 | ||||
694 | bool ignored; | |||
695 | APFloat TmpFlt2(+1.0); | |||
696 | TmpFlt2.convert(APFloat::x87DoubleExtended(), APFloat::rmNearestTiesToEven, | |||
697 | &ignored); | |||
698 | addLegalFPImmediate(TmpFlt2); // FLD1 | |||
699 | TmpFlt2.changeSign(); | |||
700 | addLegalFPImmediate(TmpFlt2); // FLD1/FCHS | |||
701 | } | |||
702 | ||||
703 | // Always expand sin/cos functions even though x87 has an instruction. | |||
704 | setOperationAction(ISD::FSIN , MVT::f80, Expand); | |||
705 | setOperationAction(ISD::FCOS , MVT::f80, Expand); | |||
706 | setOperationAction(ISD::FSINCOS, MVT::f80, Expand); | |||
707 | ||||
708 | setOperationAction(ISD::FFLOOR, MVT::f80, Expand); | |||
709 | setOperationAction(ISD::FCEIL, MVT::f80, Expand); | |||
710 | setOperationAction(ISD::FTRUNC, MVT::f80, Expand); | |||
711 | setOperationAction(ISD::FRINT, MVT::f80, Expand); | |||
712 | setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand); | |||
713 | setOperationAction(ISD::FMA, MVT::f80, Expand); | |||
714 | setOperationAction(ISD::LROUND, MVT::f80, Expand); | |||
715 | setOperationAction(ISD::LLROUND, MVT::f80, Expand); | |||
716 | setOperationAction(ISD::LRINT, MVT::f80, Custom); | |||
717 | setOperationAction(ISD::LLRINT, MVT::f80, Custom); | |||
718 | ||||
719 | // Handle constrained floating-point operations of scalar. | |||
720 | setOperationAction(ISD::STRICT_FADD , MVT::f80, Legal); | |||
721 | setOperationAction(ISD::STRICT_FSUB , MVT::f80, Legal); | |||
722 | setOperationAction(ISD::STRICT_FMUL , MVT::f80, Legal); | |||
723 | setOperationAction(ISD::STRICT_FDIV , MVT::f80, Legal); | |||
724 | setOperationAction(ISD::STRICT_FSQRT , MVT::f80, Legal); | |||
725 | setOperationAction(ISD::STRICT_FP_EXTEND, MVT::f80, Legal); | |||
726 | // FIXME: When the target is 64-bit, STRICT_FP_ROUND will be overwritten | |||
727 | // as Custom. | |||
728 | setOperationAction(ISD::STRICT_FP_ROUND, MVT::f80, Legal); | |||
729 | } | |||
730 | ||||
731 | // f128 uses xmm registers, but most operations require libcalls. | |||
732 | if (!Subtarget.useSoftFloat() && Subtarget.is64Bit() && Subtarget.hasSSE1()) { | |||
733 | addRegisterClass(MVT::f128, Subtarget.hasVLX() ? &X86::VR128XRegClass | |||
734 | : &X86::VR128RegClass); | |||
735 | ||||
736 | addLegalFPImmediate(APFloat::getZero(APFloat::IEEEquad())); // xorps | |||
737 | ||||
738 | setOperationAction(ISD::FADD, MVT::f128, LibCall); | |||
739 | setOperationAction(ISD::STRICT_FADD, MVT::f128, LibCall); | |||
740 | setOperationAction(ISD::FSUB, MVT::f128, LibCall); | |||
741 | setOperationAction(ISD::STRICT_FSUB, MVT::f128, LibCall); | |||
742 | setOperationAction(ISD::FDIV, MVT::f128, LibCall); | |||
743 | setOperationAction(ISD::STRICT_FDIV, MVT::f128, LibCall); | |||
744 | setOperationAction(ISD::FMUL, MVT::f128, LibCall); | |||
745 | setOperationAction(ISD::STRICT_FMUL, MVT::f128, LibCall); | |||
746 | setOperationAction(ISD::FMA, MVT::f128, LibCall); | |||
747 | setOperationAction(ISD::STRICT_FMA, MVT::f128, LibCall); | |||
748 | ||||
749 | setOperationAction(ISD::FABS, MVT::f128, Custom); | |||
750 | setOperationAction(ISD::FNEG, MVT::f128, Custom); | |||
751 | setOperationAction(ISD::FCOPYSIGN, MVT::f128, Custom); | |||
752 | ||||
753 | setOperationAction(ISD::FSIN, MVT::f128, LibCall); | |||
754 | setOperationAction(ISD::STRICT_FSIN, MVT::f128, LibCall); | |||
755 | setOperationAction(ISD::FCOS, MVT::f128, LibCall); | |||
756 | setOperationAction(ISD::STRICT_FCOS, MVT::f128, LibCall); | |||
757 | setOperationAction(ISD::FSINCOS, MVT::f128, LibCall); | |||
758 | // No STRICT_FSINCOS | |||
759 | setOperationAction(ISD::FSQRT, MVT::f128, LibCall); | |||
760 | setOperationAction(ISD::STRICT_FSQRT, MVT::f128, LibCall); | |||
761 | ||||
762 | setOperationAction(ISD::FP_EXTEND, MVT::f128, Custom); | |||
763 | setOperationAction(ISD::STRICT_FP_EXTEND, MVT::f128, Custom); | |||
764 | // We need to custom handle any FP_ROUND with an f128 input, but | |||
765 | // LegalizeDAG uses the result type to know when to run a custom handler. | |||
766 | // So we have to list all legal floating point result types here. | |||
767 | if (isTypeLegal(MVT::f32)) { | |||
768 | setOperationAction(ISD::FP_ROUND, MVT::f32, Custom); | |||
769 | setOperationAction(ISD::STRICT_FP_ROUND, MVT::f32, Custom); | |||
770 | } | |||
771 | if (isTypeLegal(MVT::f64)) { | |||
772 | setOperationAction(ISD::FP_ROUND, MVT::f64, Custom); | |||
773 | setOperationAction(ISD::STRICT_FP_ROUND, MVT::f64, Custom); | |||
774 | } | |||
775 | if (isTypeLegal(MVT::f80)) { | |||
776 | setOperationAction(ISD::FP_ROUND, MVT::f80, Custom); | |||
777 | setOperationAction(ISD::STRICT_FP_ROUND, MVT::f80, Custom); | |||
778 | } | |||
779 | ||||
780 | setOperationAction(ISD::SETCC, MVT::f128, Custom); | |||
781 | ||||
782 | setLoadExtAction(ISD::EXTLOAD, MVT::f128, MVT::f32, Expand); | |||
783 | setLoadExtAction(ISD::EXTLOAD, MVT::f128, MVT::f64, Expand); | |||
784 | setLoadExtAction(ISD::EXTLOAD, MVT::f128, MVT::f80, Expand); | |||
785 | setTruncStoreAction(MVT::f128, MVT::f32, Expand); | |||
786 | setTruncStoreAction(MVT::f128, MVT::f64, Expand); | |||
787 | setTruncStoreAction(MVT::f128, MVT::f80, Expand); | |||
788 | } | |||
789 | ||||
790 | // Always use a library call for pow. | |||
791 | setOperationAction(ISD::FPOW , MVT::f32 , Expand); | |||
792 | setOperationAction(ISD::FPOW , MVT::f64 , Expand); | |||
793 | setOperationAction(ISD::FPOW , MVT::f80 , Expand); | |||
794 | setOperationAction(ISD::FPOW , MVT::f128 , Expand); | |||
795 | ||||
796 | setOperationAction(ISD::FLOG, MVT::f80, Expand); | |||
797 | setOperationAction(ISD::FLOG2, MVT::f80, Expand); | |||
798 | setOperationAction(ISD::FLOG10, MVT::f80, Expand); | |||
799 | setOperationAction(ISD::FEXP, MVT::f80, Expand); | |||
800 | setOperationAction(ISD::FEXP2, MVT::f80, Expand); | |||
801 | setOperationAction(ISD::FMINNUM, MVT::f80, Expand); | |||
802 | setOperationAction(ISD::FMAXNUM, MVT::f80, Expand); | |||
803 | ||||
804 | // Some FP actions are always expanded for vector types. | |||
805 | for (auto VT : { MVT::v4f32, MVT::v8f32, MVT::v16f32, | |||
806 | MVT::v2f64, MVT::v4f64, MVT::v8f64 }) { | |||
807 | setOperationAction(ISD::FSIN, VT, Expand); | |||
808 | setOperationAction(ISD::FSINCOS, VT, Expand); | |||
809 | setOperationAction(ISD::FCOS, VT, Expand); | |||
810 | setOperationAction(ISD::FREM, VT, Expand); | |||
811 | setOperationAction(ISD::FCOPYSIGN, VT, Expand); | |||
812 | setOperationAction(ISD::FPOW, VT, Expand); | |||
813 | setOperationAction(ISD::FLOG, VT, Expand); | |||
814 | setOperationAction(ISD::FLOG2, VT, Expand); | |||
815 | setOperationAction(ISD::FLOG10, VT, Expand); | |||
816 | setOperationAction(ISD::FEXP, VT, Expand); | |||
817 | setOperationAction(ISD::FEXP2, VT, Expand); | |||
818 | } | |||
819 | ||||
820 | // First set operation action for all vector types to either promote | |||
821 | // (for widening) or expand (for scalarization). Then we will selectively | |||
822 | // turn on ones that can be effectively codegen'd. | |||
823 | for (MVT VT : MVT::fixedlen_vector_valuetypes()) { | |||
824 | setOperationAction(ISD::SDIV, VT, Expand); | |||
825 | setOperationAction(ISD::UDIV, VT, Expand); | |||
826 | setOperationAction(ISD::SREM, VT, Expand); | |||
827 | setOperationAction(ISD::UREM, VT, Expand); | |||
828 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT,Expand); | |||
829 | setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand); | |||
830 | setOperationAction(ISD::EXTRACT_SUBVECTOR, VT,Expand); | |||
831 | setOperationAction(ISD::INSERT_SUBVECTOR, VT,Expand); | |||
832 | setOperationAction(ISD::FMA, VT, Expand); | |||
833 | setOperationAction(ISD::FFLOOR, VT, Expand); | |||
834 | setOperationAction(ISD::FCEIL, VT, Expand); | |||
835 | setOperationAction(ISD::FTRUNC, VT, Expand); | |||
836 | setOperationAction(ISD::FRINT, VT, Expand); | |||
837 | setOperationAction(ISD::FNEARBYINT, VT, Expand); | |||
838 | setOperationAction(ISD::SMUL_LOHI, VT, Expand); | |||
839 | setOperationAction(ISD::MULHS, VT, Expand); | |||
840 | setOperationAction(ISD::UMUL_LOHI, VT, Expand); | |||
841 | setOperationAction(ISD::MULHU, VT, Expand); | |||
842 | setOperationAction(ISD::SDIVREM, VT, Expand); | |||
843 | setOperationAction(ISD::UDIVREM, VT, Expand); | |||
844 | setOperationAction(ISD::CTPOP, VT, Expand); | |||
845 | setOperationAction(ISD::CTTZ, VT, Expand); | |||
846 | setOperationAction(ISD::CTLZ, VT, Expand); | |||
847 | setOperationAction(ISD::ROTL, VT, Expand); | |||
848 | setOperationAction(ISD::ROTR, VT, Expand); | |||
849 | setOperationAction(ISD::BSWAP, VT, Expand); | |||
850 | setOperationAction(ISD::SETCC, VT, Expand); | |||
851 | setOperationAction(ISD::FP_TO_UINT, VT, Expand); | |||
852 | setOperationAction(ISD::FP_TO_SINT, VT, Expand); | |||
853 | setOperationAction(ISD::UINT_TO_FP, VT, Expand); | |||
854 | setOperationAction(ISD::SINT_TO_FP, VT, Expand); | |||
855 | setOperationAction(ISD::SIGN_EXTEND_INREG, VT,Expand); | |||
856 | setOperationAction(ISD::TRUNCATE, VT, Expand); | |||
857 | setOperationAction(ISD::SIGN_EXTEND, VT, Expand); | |||
858 | setOperationAction(ISD::ZERO_EXTEND, VT, Expand); | |||
859 | setOperationAction(ISD::ANY_EXTEND, VT, Expand); | |||
860 | setOperationAction(ISD::SELECT_CC, VT, Expand); | |||
861 | for (MVT InnerVT : MVT::fixedlen_vector_valuetypes()) { | |||
862 | setTruncStoreAction(InnerVT, VT, Expand); | |||
863 | ||||
864 | setLoadExtAction(ISD::SEXTLOAD, InnerVT, VT, Expand); | |||
865 | setLoadExtAction(ISD::ZEXTLOAD, InnerVT, VT, Expand); | |||
866 | ||||
867 | // N.b. ISD::EXTLOAD legality is basically ignored except for i1-like | |||
868 | // types, we have to deal with them whether we ask for Expansion or not. | |||
869 | // Setting Expand causes its own optimisation problems though, so leave | |||
870 | // them legal. | |||
871 | if (VT.getVectorElementType() == MVT::i1) | |||
872 | setLoadExtAction(ISD::EXTLOAD, InnerVT, VT, Expand); | |||
873 | ||||
874 | // EXTLOAD for MVT::f16 vectors is not legal because f16 vectors are | |||
875 | // split/scalarized right now. | |||
876 | if (VT.getVectorElementType() == MVT::f16) | |||
877 | setLoadExtAction(ISD::EXTLOAD, InnerVT, VT, Expand); | |||
878 | } | |||
879 | } | |||
880 | ||||
881 | // FIXME: In order to prevent SSE instructions being expanded to MMX ones | |||
882 | // with -msoft-float, disable use of MMX as well. | |||
883 | if (!Subtarget.useSoftFloat() && Subtarget.hasMMX()) { | |||
884 | addRegisterClass(MVT::x86mmx, &X86::VR64RegClass); | |||
885 | // No operations on x86mmx supported, everything uses intrinsics. | |||
886 | } | |||
887 | ||||
888 | if (!Subtarget.useSoftFloat() && Subtarget.hasSSE1()) { | |||
889 | addRegisterClass(MVT::v4f32, Subtarget.hasVLX() ? &X86::VR128XRegClass | |||
890 | : &X86::VR128RegClass); | |||
891 | ||||
892 | setOperationAction(ISD::FNEG, MVT::v4f32, Custom); | |||
893 | setOperationAction(ISD::FABS, MVT::v4f32, Custom); | |||
894 | setOperationAction(ISD::FCOPYSIGN, MVT::v4f32, Custom); | |||
895 | setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom); | |||
896 | setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom); | |||
897 | setOperationAction(ISD::VSELECT, MVT::v4f32, Custom); | |||
898 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom); | |||
899 | setOperationAction(ISD::SELECT, MVT::v4f32, Custom); | |||
900 | ||||
901 | setOperationAction(ISD::LOAD, MVT::v2f32, Custom); | |||
902 | setOperationAction(ISD::STORE, MVT::v2f32, Custom); | |||
903 | ||||
904 | setOperationAction(ISD::STRICT_FADD, MVT::v4f32, Legal); | |||
905 | setOperationAction(ISD::STRICT_FSUB, MVT::v4f32, Legal); | |||
906 | setOperationAction(ISD::STRICT_FMUL, MVT::v4f32, Legal); | |||
907 | setOperationAction(ISD::STRICT_FDIV, MVT::v4f32, Legal); | |||
908 | setOperationAction(ISD::STRICT_FSQRT, MVT::v4f32, Legal); | |||
909 | } | |||
910 | ||||
911 | if (!Subtarget.useSoftFloat() && Subtarget.hasSSE2()) { | |||
912 | addRegisterClass(MVT::v2f64, Subtarget.hasVLX() ? &X86::VR128XRegClass | |||
913 | : &X86::VR128RegClass); | |||
914 | ||||
915 | // FIXME: Unfortunately, -soft-float and -no-implicit-float mean XMM | |||
916 | // registers cannot be used even for integer operations. | |||
917 | addRegisterClass(MVT::v16i8, Subtarget.hasVLX() ? &X86::VR128XRegClass | |||
918 | : &X86::VR128RegClass); | |||
919 | addRegisterClass(MVT::v8i16, Subtarget.hasVLX() ? &X86::VR128XRegClass | |||
920 | : &X86::VR128RegClass); | |||
921 | addRegisterClass(MVT::v4i32, Subtarget.hasVLX() ? &X86::VR128XRegClass | |||
922 | : &X86::VR128RegClass); | |||
923 | addRegisterClass(MVT::v2i64, Subtarget.hasVLX() ? &X86::VR128XRegClass | |||
924 | : &X86::VR128RegClass); | |||
925 | ||||
926 | for (auto VT : { MVT::v2i8, MVT::v4i8, MVT::v8i8, | |||
927 | MVT::v2i16, MVT::v4i16, MVT::v2i32 }) { | |||
928 | setOperationAction(ISD::SDIV, VT, Custom); | |||
929 | setOperationAction(ISD::SREM, VT, Custom); | |||
930 | setOperationAction(ISD::UDIV, VT, Custom); | |||
931 | setOperationAction(ISD::UREM, VT, Custom); | |||
932 | } | |||
933 | ||||
934 | setOperationAction(ISD::MUL, MVT::v2i8, Custom); | |||
935 | setOperationAction(ISD::MUL, MVT::v4i8, Custom); | |||
936 | setOperationAction(ISD::MUL, MVT::v8i8, Custom); | |||
937 | ||||
938 | setOperationAction(ISD::MUL, MVT::v16i8, Custom); | |||
939 | setOperationAction(ISD::MUL, MVT::v4i32, Custom); | |||
940 | setOperationAction(ISD::MUL, MVT::v2i64, Custom); | |||
941 | setOperationAction(ISD::MULHU, MVT::v4i32, Custom); | |||
942 | setOperationAction(ISD::MULHS, MVT::v4i32, Custom); | |||
943 | setOperationAction(ISD::MULHU, MVT::v16i8, Custom); | |||
944 | setOperationAction(ISD::MULHS, MVT::v16i8, Custom); | |||
945 | setOperationAction(ISD::MULHU, MVT::v8i16, Legal); | |||
946 | setOperationAction(ISD::MULHS, MVT::v8i16, Legal); | |||
947 | setOperationAction(ISD::MUL, MVT::v8i16, Legal); | |||
948 | ||||
949 | setOperationAction(ISD::SMULO, MVT::v16i8, Custom); | |||
950 | setOperationAction(ISD::UMULO, MVT::v16i8, Custom); | |||
951 | ||||
952 | setOperationAction(ISD::FNEG, MVT::v2f64, Custom); | |||
953 | setOperationAction(ISD::FABS, MVT::v2f64, Custom); | |||
954 | setOperationAction(ISD::FCOPYSIGN, MVT::v2f64, Custom); | |||
955 | ||||
956 | for (auto VT : { MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v2i64 }) { | |||
957 | setOperationAction(ISD::SMAX, VT, VT == MVT::v8i16 ? Legal : Custom); | |||
958 | setOperationAction(ISD::SMIN, VT, VT == MVT::v8i16 ? Legal : Custom); | |||
959 | setOperationAction(ISD::UMAX, VT, VT == MVT::v16i8 ? Legal : Custom); | |||
960 | setOperationAction(ISD::UMIN, VT, VT == MVT::v16i8 ? Legal : Custom); | |||
961 | } | |||
962 | ||||
963 | setOperationAction(ISD::UADDSAT, MVT::v16i8, Legal); | |||
964 | setOperationAction(ISD::SADDSAT, MVT::v16i8, Legal); | |||
965 | setOperationAction(ISD::USUBSAT, MVT::v16i8, Legal); | |||
966 | setOperationAction(ISD::SSUBSAT, MVT::v16i8, Legal); | |||
967 | setOperationAction(ISD::UADDSAT, MVT::v8i16, Legal); | |||
968 | setOperationAction(ISD::SADDSAT, MVT::v8i16, Legal); | |||
969 | setOperationAction(ISD::USUBSAT, MVT::v8i16, Legal); | |||
970 | setOperationAction(ISD::SSUBSAT, MVT::v8i16, Legal); | |||
971 | setOperationAction(ISD::USUBSAT, MVT::v4i32, Custom); | |||
972 | setOperationAction(ISD::USUBSAT, MVT::v2i64, Custom); | |||
973 | ||||
974 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom); | |||
975 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom); | |||
976 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom); | |||
977 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom); | |||
978 | ||||
979 | for (auto VT : { MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v2i64 }) { | |||
980 | setOperationAction(ISD::SETCC, VT, Custom); | |||
981 | setOperationAction(ISD::STRICT_FSETCC, VT, Custom); | |||
982 | setOperationAction(ISD::STRICT_FSETCCS, VT, Custom); | |||
983 | setOperationAction(ISD::CTPOP, VT, Custom); | |||
984 | setOperationAction(ISD::ABS, VT, Custom); | |||
985 | ||||
986 | // The condition codes aren't legal in SSE/AVX and under AVX512 we use | |||
987 | // setcc all the way to isel and prefer SETGT in some isel patterns. | |||
988 | setCondCodeAction(ISD::SETLT, VT, Custom); | |||
989 | setCondCodeAction(ISD::SETLE, VT, Custom); | |||
990 | } | |||
991 | ||||
992 | for (auto VT : { MVT::v16i8, MVT::v8i16, MVT::v4i32 }) { | |||
993 | setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom); | |||
994 | setOperationAction(ISD::BUILD_VECTOR, VT, Custom); | |||
995 | setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom); | |||
996 | setOperationAction(ISD::VSELECT, VT, Custom); | |||
997 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom); | |||
998 | } | |||
999 | ||||
1000 | for (auto VT : { MVT::v2f64, MVT::v2i64 }) { | |||
1001 | setOperationAction(ISD::BUILD_VECTOR, VT, Custom); | |||
1002 | setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom); | |||
1003 | setOperationAction(ISD::VSELECT, VT, Custom); | |||
1004 | ||||
1005 | if (VT == MVT::v2i64 && !Subtarget.is64Bit()) | |||
1006 | continue; | |||
1007 | ||||
1008 | setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom); | |||
1009 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom); | |||
1010 | } | |||
1011 | ||||
1012 | // Custom lower v2i64 and v2f64 selects. | |||
1013 | setOperationAction(ISD::SELECT, MVT::v2f64, Custom); | |||
1014 | setOperationAction(ISD::SELECT, MVT::v2i64, Custom); | |||
1015 | setOperationAction(ISD::SELECT, MVT::v4i32, Custom); | |||
1016 | setOperationAction(ISD::SELECT, MVT::v8i16, Custom); | |||
1017 | setOperationAction(ISD::SELECT, MVT::v16i8, Custom); | |||
1018 | ||||
1019 | setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal); | |||
1020 | setOperationAction(ISD::FP_TO_UINT, MVT::v4i32, Custom); | |||
1021 | setOperationAction(ISD::FP_TO_SINT, MVT::v2i32, Custom); | |||
1022 | setOperationAction(ISD::FP_TO_UINT, MVT::v2i32, Custom); | |||
1023 | setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::v4i32, Legal); | |||
1024 | setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::v2i32, Custom); | |||
1025 | ||||
1026 | // Custom legalize these to avoid over promotion or custom promotion. | |||
1027 | for (auto VT : {MVT::v2i8, MVT::v4i8, MVT::v8i8, MVT::v2i16, MVT::v4i16}) { | |||
1028 | setOperationAction(ISD::FP_TO_SINT, VT, Custom); | |||
1029 | setOperationAction(ISD::FP_TO_UINT, VT, Custom); | |||
1030 | setOperationAction(ISD::STRICT_FP_TO_SINT, VT, Custom); | |||
1031 | setOperationAction(ISD::STRICT_FP_TO_UINT, VT, Custom); | |||
1032 | } | |||
1033 | ||||
1034 | setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal); | |||
1035 | setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::v4i32, Legal); | |||
1036 | setOperationAction(ISD::SINT_TO_FP, MVT::v2i32, Custom); | |||
1037 | setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::v2i32, Custom); | |||
1038 | ||||
1039 | setOperationAction(ISD::UINT_TO_FP, MVT::v2i32, Custom); | |||
1040 | setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::v2i32, Custom); | |||
1041 | ||||
1042 | setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Custom); | |||
1043 | setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::v4i32, Custom); | |||
1044 | ||||
1045 | // Fast v2f32 UINT_TO_FP( v2i32 ) custom conversion. | |||
1046 | setOperationAction(ISD::SINT_TO_FP, MVT::v2f32, Custom); | |||
1047 | setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::v2f32, Custom); | |||
1048 | setOperationAction(ISD::UINT_TO_FP, MVT::v2f32, Custom); | |||
1049 | setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::v2f32, Custom); | |||
1050 | ||||
1051 | setOperationAction(ISD::FP_EXTEND, MVT::v2f32, Custom); | |||
1052 | setOperationAction(ISD::STRICT_FP_EXTEND, MVT::v2f32, Custom); | |||
1053 | setOperationAction(ISD::FP_ROUND, MVT::v2f32, Custom); | |||
1054 | setOperationAction(ISD::STRICT_FP_ROUND, MVT::v2f32, Custom); | |||
1055 | ||||
1056 | // We want to legalize this to an f64 load rather than an i64 load on | |||
1057 | // 64-bit targets and two 32-bit loads on a 32-bit target. Similar for | |||
1058 | // store. | |||
1059 | setOperationAction(ISD::LOAD, MVT::v2i32, Custom); | |||
1060 | setOperationAction(ISD::LOAD, MVT::v4i16, Custom); | |||
1061 | setOperationAction(ISD::LOAD, MVT::v8i8, Custom); | |||
1062 | setOperationAction(ISD::STORE, MVT::v2i32, Custom); | |||
1063 | setOperationAction(ISD::STORE, MVT::v4i16, Custom); | |||
1064 | setOperationAction(ISD::STORE, MVT::v8i8, Custom); | |||
1065 | ||||
1066 | setOperationAction(ISD::BITCAST, MVT::v2i32, Custom); | |||
1067 | setOperationAction(ISD::BITCAST, MVT::v4i16, Custom); | |||
1068 | setOperationAction(ISD::BITCAST, MVT::v8i8, Custom); | |||
1069 | if (!Subtarget.hasAVX512()) | |||
1070 | setOperationAction(ISD::BITCAST, MVT::v16i1, Custom); | |||
1071 | ||||
1072 | setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, MVT::v2i64, Custom); | |||
1073 | setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, MVT::v4i32, Custom); | |||
1074 | setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, MVT::v8i16, Custom); | |||
1075 | ||||
1076 | setOperationAction(ISD::SIGN_EXTEND, MVT::v4i64, Custom); | |||
1077 | ||||
1078 | setOperationAction(ISD::TRUNCATE, MVT::v2i8, Custom); | |||
1079 | setOperationAction(ISD::TRUNCATE, MVT::v2i16, Custom); | |||
1080 | setOperationAction(ISD::TRUNCATE, MVT::v2i32, Custom); | |||
1081 | setOperationAction(ISD::TRUNCATE, MVT::v4i8, Custom); | |||
1082 | setOperationAction(ISD::TRUNCATE, MVT::v4i16, Custom); | |||
1083 | setOperationAction(ISD::TRUNCATE, MVT::v8i8, Custom); | |||
1084 | ||||
1085 | // In the customized shift lowering, the legal v4i32/v2i64 cases | |||
1086 | // in AVX2 will be recognized. | |||
1087 | for (auto VT : { MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v2i64 }) { | |||
1088 | setOperationAction(ISD::SRL, VT, Custom); | |||
1089 | setOperationAction(ISD::SHL, VT, Custom); | |||
1090 | setOperationAction(ISD::SRA, VT, Custom); | |||
1091 | } | |||
1092 | ||||
1093 | setOperationAction(ISD::ROTL, MVT::v4i32, Custom); | |||
1094 | setOperationAction(ISD::ROTL, MVT::v8i16, Custom); | |||
1095 | ||||
1096 | // With 512-bit registers or AVX512VL+BW, expanding (and promoting the | |||
1097 | // shifts) is better. | |||
1098 | if (!Subtarget.useAVX512Regs() && | |||
1099 | !(Subtarget.hasBWI() && Subtarget.hasVLX())) | |||
1100 | setOperationAction(ISD::ROTL, MVT::v16i8, Custom); | |||
1101 | ||||
1102 | setOperationAction(ISD::STRICT_FSQRT, MVT::v2f64, Legal); | |||
1103 | setOperationAction(ISD::STRICT_FADD, MVT::v2f64, Legal); | |||
1104 | setOperationAction(ISD::STRICT_FSUB, MVT::v2f64, Legal); | |||
1105 | setOperationAction(ISD::STRICT_FMUL, MVT::v2f64, Legal); | |||
1106 | setOperationAction(ISD::STRICT_FDIV, MVT::v2f64, Legal); | |||
1107 | } | |||
1108 | ||||
1109 | if (!Subtarget.useSoftFloat() && Subtarget.hasSSSE3()) { | |||
1110 | setOperationAction(ISD::ABS, MVT::v16i8, Legal); | |||
1111 | setOperationAction(ISD::ABS, MVT::v8i16, Legal); | |||
1112 | setOperationAction(ISD::ABS, MVT::v4i32, Legal); | |||
1113 | setOperationAction(ISD::BITREVERSE, MVT::v16i8, Custom); | |||
1114 | setOperationAction(ISD::CTLZ, MVT::v16i8, Custom); | |||
1115 | setOperationAction(ISD::CTLZ, MVT::v8i16, Custom); | |||
1116 | setOperationAction(ISD::CTLZ, MVT::v4i32, Custom); | |||
1117 | setOperationAction(ISD::CTLZ, MVT::v2i64, Custom); | |||
1118 | ||||
1119 | // These might be better off as horizontal vector ops. | |||
1120 | setOperationAction(ISD::ADD, MVT::i16, Custom); | |||
1121 | setOperationAction(ISD::ADD, MVT::i32, Custom); | |||
1122 | setOperationAction(ISD::SUB, MVT::i16, Custom); | |||
1123 | setOperationAction(ISD::SUB, MVT::i32, Custom); | |||
1124 | } | |||
1125 | ||||
1126 | if (!Subtarget.useSoftFloat() && Subtarget.hasSSE41()) { | |||
1127 | for (MVT RoundedTy : {MVT::f32, MVT::f64, MVT::v4f32, MVT::v2f64}) { | |||
1128 | setOperationAction(ISD::FFLOOR, RoundedTy, Legal); | |||
1129 | setOperationAction(ISD::STRICT_FFLOOR, RoundedTy, Legal); | |||
1130 | setOperationAction(ISD::FCEIL, RoundedTy, Legal); | |||
1131 | setOperationAction(ISD::STRICT_FCEIL, RoundedTy, Legal); | |||
1132 | setOperationAction(ISD::FTRUNC, RoundedTy, Legal); | |||
1133 | setOperationAction(ISD::STRICT_FTRUNC, RoundedTy, Legal); | |||
1134 | setOperationAction(ISD::FRINT, RoundedTy, Legal); | |||
1135 | setOperationAction(ISD::STRICT_FRINT, RoundedTy, Legal); | |||
1136 | setOperationAction(ISD::FNEARBYINT, RoundedTy, Legal); | |||
1137 | setOperationAction(ISD::STRICT_FNEARBYINT, RoundedTy, Legal); | |||
1138 | setOperationAction(ISD::FROUNDEVEN, RoundedTy, Legal); | |||
1139 | setOperationAction(ISD::STRICT_FROUNDEVEN, RoundedTy, Legal); | |||
1140 | ||||
1141 | setOperationAction(ISD::FROUND, RoundedTy, Custom); | |||
1142 | } | |||
1143 | ||||
1144 | setOperationAction(ISD::SMAX, MVT::v16i8, Legal); | |||
1145 | setOperationAction(ISD::SMAX, MVT::v4i32, Legal); | |||
1146 | setOperationAction(ISD::UMAX, MVT::v8i16, Legal); | |||
1147 | setOperationAction(ISD::UMAX, MVT::v4i32, Legal); | |||
1148 | setOperationAction(ISD::SMIN, MVT::v16i8, Legal); | |||
1149 | setOperationAction(ISD::SMIN, MVT::v4i32, Legal); | |||
1150 | setOperationAction(ISD::UMIN, MVT::v8i16, Legal); | |||
1151 | setOperationAction(ISD::UMIN, MVT::v4i32, Legal); | |||
1152 | ||||
1153 | setOperationAction(ISD::UADDSAT, MVT::v4i32, Custom); | |||
1154 | setOperationAction(ISD::SADDSAT, MVT::v2i64, Custom); | |||
1155 | setOperationAction(ISD::SSUBSAT, MVT::v2i64, Custom); | |||
1156 | ||||
1157 | // FIXME: Do we need to handle scalar-to-vector here? | |||
1158 | setOperationAction(ISD::MUL, MVT::v4i32, Legal); | |||
1159 | ||||
1160 | // We directly match byte blends in the backend as they match the VSELECT | |||
1161 | // condition form. | |||
1162 | setOperationAction(ISD::VSELECT, MVT::v16i8, Legal); | |||
1163 | ||||
1164 | // SSE41 brings specific instructions for doing vector sign extend even in | |||
1165 | // cases where we don't have SRA. | |||
1166 | for (auto VT : { MVT::v8i16, MVT::v4i32, MVT::v2i64 }) { | |||
1167 | setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, VT, Legal); | |||
1168 | setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, VT, Legal); | |||
1169 | } | |||
1170 | ||||
1171 | // SSE41 also has vector sign/zero extending loads, PMOV[SZ]X | |||
1172 | for (auto LoadExtOp : { ISD::SEXTLOAD, ISD::ZEXTLOAD }) { | |||
1173 | setLoadExtAction(LoadExtOp, MVT::v8i16, MVT::v8i8, Legal); | |||
1174 | setLoadExtAction(LoadExtOp, MVT::v4i32, MVT::v4i8, Legal); | |||
1175 | setLoadExtAction(LoadExtOp, MVT::v2i64, MVT::v2i8, Legal); | |||
1176 | setLoadExtAction(LoadExtOp, MVT::v4i32, MVT::v4i16, Legal); | |||
1177 | setLoadExtAction(LoadExtOp, MVT::v2i64, MVT::v2i16, Legal); | |||
1178 | setLoadExtAction(LoadExtOp, MVT::v2i64, MVT::v2i32, Legal); | |||
1179 | } | |||
1180 | ||||
1181 | if (Subtarget.is64Bit() && !Subtarget.hasAVX512()) { | |||
1182 | // We need to scalarize v4i64->v432 uint_to_fp using cvtsi2ss, but we can | |||
1183 | // do the pre and post work in the vector domain. | |||
1184 | setOperationAction(ISD::UINT_TO_FP, MVT::v4i64, Custom); | |||
1185 | setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::v4i64, Custom); | |||
1186 | // We need to mark SINT_TO_FP as Custom even though we want to expand it | |||
1187 | // so that DAG combine doesn't try to turn it into uint_to_fp. | |||
1188 | setOperationAction(ISD::SINT_TO_FP, MVT::v4i64, Custom); | |||
1189 | setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::v4i64, Custom); | |||
1190 | } | |||
1191 | } | |||
1192 | ||||
1193 | if (!Subtarget.useSoftFloat() && Subtarget.hasSSE42()) { | |||
1194 | setOperationAction(ISD::UADDSAT, MVT::v2i64, Custom); | |||
1195 | } | |||
1196 | ||||
1197 | if (!Subtarget.useSoftFloat() && Subtarget.hasXOP()) { | |||
1198 | for (auto VT : { MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v2i64, | |||
1199 | MVT::v32i8, MVT::v16i16, MVT::v8i32, MVT::v4i64 }) | |||
1200 | setOperationAction(ISD::ROTL, VT, Custom); | |||
1201 | ||||
1202 | // XOP can efficiently perform BITREVERSE with VPPERM. | |||
1203 | for (auto VT : { MVT::i8, MVT::i16, MVT::i32, MVT::i64 }) | |||
1204 | setOperationAction(ISD::BITREVERSE, VT, Custom); | |||
1205 | ||||
1206 | for (auto VT : { MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v2i64, | |||
1207 | MVT::v32i8, MVT::v16i16, MVT::v8i32, MVT::v4i64 }) | |||
1208 | setOperationAction(ISD::BITREVERSE, VT, Custom); | |||
1209 | } | |||
1210 | ||||
1211 | if (!Subtarget.useSoftFloat() && Subtarget.hasAVX()) { | |||
1212 | bool HasInt256 = Subtarget.hasInt256(); | |||
1213 | ||||
1214 | addRegisterClass(MVT::v32i8, Subtarget.hasVLX() ? &X86::VR256XRegClass | |||
1215 | : &X86::VR256RegClass); | |||
1216 | addRegisterClass(MVT::v16i16, Subtarget.hasVLX() ? &X86::VR256XRegClass | |||
1217 | : &X86::VR256RegClass); | |||
1218 | addRegisterClass(MVT::v8i32, Subtarget.hasVLX() ? &X86::VR256XRegClass | |||
1219 | : &X86::VR256RegClass); | |||
1220 | addRegisterClass(MVT::v8f32, Subtarget.hasVLX() ? &X86::VR256XRegClass | |||
1221 | : &X86::VR256RegClass); | |||
1222 | addRegisterClass(MVT::v4i64, Subtarget.hasVLX() ? &X86::VR256XRegClass | |||
1223 | : &X86::VR256RegClass); | |||
1224 | addRegisterClass(MVT::v4f64, Subtarget.hasVLX() ? &X86::VR256XRegClass | |||
1225 | : &X86::VR256RegClass); | |||
1226 | ||||
1227 | for (auto VT : { MVT::v8f32, MVT::v4f64 }) { | |||
1228 | setOperationAction(ISD::FFLOOR, VT, Legal); | |||
1229 | setOperationAction(ISD::STRICT_FFLOOR, VT, Legal); | |||
1230 | setOperationAction(ISD::FCEIL, VT, Legal); | |||
1231 | setOperationAction(ISD::STRICT_FCEIL, VT, Legal); | |||
1232 | setOperationAction(ISD::FTRUNC, VT, Legal); | |||
1233 | setOperationAction(ISD::STRICT_FTRUNC, VT, Legal); | |||
1234 | setOperationAction(ISD::FRINT, VT, Legal); | |||
1235 | setOperationAction(ISD::STRICT_FRINT, VT, Legal); | |||
1236 | setOperationAction(ISD::FNEARBYINT, VT, Legal); | |||
1237 | setOperationAction(ISD::STRICT_FNEARBYINT, VT, Legal); | |||
1238 | setOperationAction(ISD::FROUNDEVEN, VT, Legal); | |||
1239 | setOperationAction(ISD::STRICT_FROUNDEVEN, VT, Legal); | |||
1240 | ||||
1241 | setOperationAction(ISD::FROUND, VT, Custom); | |||
1242 | ||||
1243 | setOperationAction(ISD::FNEG, VT, Custom); | |||
1244 | setOperationAction(ISD::FABS, VT, Custom); | |||
1245 | setOperationAction(ISD::FCOPYSIGN, VT, Custom); | |||
1246 | } | |||
1247 | ||||
1248 | // (fp_to_int:v8i16 (v8f32 ..)) requires the result type to be promoted | |||
1249 | // even though v8i16 is a legal type. | |||
1250 | setOperationPromotedToType(ISD::FP_TO_SINT, MVT::v8i16, MVT::v8i32); | |||
1251 | setOperationPromotedToType(ISD::FP_TO_UINT, MVT::v8i16, MVT::v8i32); | |||
1252 | setOperationPromotedToType(ISD::STRICT_FP_TO_SINT, MVT::v8i16, MVT::v8i32); | |||
1253 | setOperationPromotedToType(ISD::STRICT_FP_TO_UINT, MVT::v8i16, MVT::v8i32); | |||
1254 | setOperationAction(ISD::FP_TO_SINT, MVT::v8i32, Legal); | |||
1255 | setOperationAction(ISD::FP_TO_UINT, MVT::v8i32, Custom); | |||
1256 | setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::v8i32, Legal); | |||
1257 | ||||
1258 | setOperationAction(ISD::SINT_TO_FP, MVT::v8i32, Legal); | |||
1259 | setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::v8i32, Legal); | |||
1260 | ||||
1261 | setOperationAction(ISD::STRICT_FP_ROUND, MVT::v4f32, Legal); | |||
1262 | setOperationAction(ISD::STRICT_FADD, MVT::v8f32, Legal); | |||
1263 | setOperationAction(ISD::STRICT_FADD, MVT::v4f64, Legal); | |||
1264 | setOperationAction(ISD::STRICT_FSUB, MVT::v8f32, Legal); | |||
1265 | setOperationAction(ISD::STRICT_FSUB, MVT::v4f64, Legal); | |||
1266 | setOperationAction(ISD::STRICT_FMUL, MVT::v8f32, Legal); | |||
1267 | setOperationAction(ISD::STRICT_FMUL, MVT::v4f64, Legal); | |||
1268 | setOperationAction(ISD::STRICT_FDIV, MVT::v8f32, Legal); | |||
1269 | setOperationAction(ISD::STRICT_FDIV, MVT::v4f64, Legal); | |||
1270 | setOperationAction(ISD::STRICT_FP_EXTEND, MVT::v4f64, Legal); | |||
1271 | setOperationAction(ISD::STRICT_FSQRT, MVT::v8f32, Legal); | |||
1272 | setOperationAction(ISD::STRICT_FSQRT, MVT::v4f64, Legal); | |||
1273 | ||||
1274 | if (!Subtarget.hasAVX512()) | |||
1275 | setOperationAction(ISD::BITCAST, MVT::v32i1, Custom); | |||
1276 | ||||
1277 | // In the customized shift lowering, the legal v8i32/v4i64 cases | |||
1278 | // in AVX2 will be recognized. | |||
1279 | for (auto VT : { MVT::v32i8, MVT::v16i16, MVT::v8i32, MVT::v4i64 }) { | |||
1280 | setOperationAction(ISD::SRL, VT, Custom); | |||
1281 | setOperationAction(ISD::SHL, VT, Custom); | |||
1282 | setOperationAction(ISD::SRA, VT, Custom); | |||
1283 | } | |||
1284 | ||||
1285 | // These types need custom splitting if their input is a 128-bit vector. | |||
1286 | setOperationAction(ISD::SIGN_EXTEND, MVT::v8i64, Custom); | |||
1287 | setOperationAction(ISD::SIGN_EXTEND, MVT::v16i32, Custom); | |||
1288 | setOperationAction(ISD::ZERO_EXTEND, MVT::v8i64, Custom); | |||
1289 | setOperationAction(ISD::ZERO_EXTEND, MVT::v16i32, Custom); | |||
1290 | ||||
1291 | setOperationAction(ISD::ROTL, MVT::v8i32, Custom); | |||
1292 | setOperationAction(ISD::ROTL, MVT::v16i16, Custom); | |||
1293 | ||||
1294 | // With BWI, expanding (and promoting the shifts) is the better. | |||
1295 | if (!Subtarget.useBWIRegs()) | |||
1296 | setOperationAction(ISD::ROTL, MVT::v32i8, Custom); | |||
1297 | ||||
1298 | setOperationAction(ISD::SELECT, MVT::v4f64, Custom); | |||
1299 | setOperationAction(ISD::SELECT, MVT::v4i64, Custom); | |||
1300 | setOperationAction(ISD::SELECT, MVT::v8i32, Custom); | |||
1301 | setOperationAction(ISD::SELECT, MVT::v16i16, Custom); | |||
1302 | setOperationAction(ISD::SELECT, MVT::v32i8, Custom); | |||
1303 | setOperationAction(ISD::SELECT, MVT::v8f32, Custom); | |||
1304 | ||||
1305 | for (auto VT : { MVT::v16i16, MVT::v8i32, MVT::v4i64 }) { | |||
1306 | setOperationAction(ISD::SIGN_EXTEND, VT, Custom); | |||
1307 | setOperationAction(ISD::ZERO_EXTEND, VT, Custom); | |||
1308 | setOperationAction(ISD::ANY_EXTEND, VT, Custom); | |||
1309 | } | |||
1310 | ||||
1311 | setOperationAction(ISD::TRUNCATE, MVT::v16i8, Custom); | |||
1312 | setOperationAction(ISD::TRUNCATE, MVT::v8i16, Custom); | |||
1313 | setOperationAction(ISD::TRUNCATE, MVT::v4i32, Custom); | |||
1314 | setOperationAction(ISD::BITREVERSE, MVT::v32i8, Custom); | |||
1315 | ||||
1316 | for (auto VT : { MVT::v32i8, MVT::v16i16, MVT::v8i32, MVT::v4i64 }) { | |||
1317 | setOperationAction(ISD::SETCC, VT, Custom); | |||
1318 | setOperationAction(ISD::STRICT_FSETCC, VT, Custom); | |||
1319 | setOperationAction(ISD::STRICT_FSETCCS, VT, Custom); | |||
1320 | setOperationAction(ISD::CTPOP, VT, Custom); | |||
1321 | setOperationAction(ISD::CTLZ, VT, Custom); | |||
1322 | ||||
1323 | // The condition codes aren't legal in SSE/AVX and under AVX512 we use | |||
1324 | // setcc all the way to isel and prefer SETGT in some isel patterns. | |||
1325 | setCondCodeAction(ISD::SETLT, VT, Custom); | |||
1326 | setCondCodeAction(ISD::SETLE, VT, Custom); | |||
1327 | } | |||
1328 | ||||
1329 | if (Subtarget.hasAnyFMA()) { | |||
1330 | for (auto VT : { MVT::f32, MVT::f64, MVT::v4f32, MVT::v8f32, | |||
1331 | MVT::v2f64, MVT::v4f64 }) { | |||
1332 | setOperationAction(ISD::FMA, VT, Legal); | |||
1333 | setOperationAction(ISD::STRICT_FMA, VT, Legal); | |||
1334 | } | |||
1335 | } | |||
1336 | ||||
1337 | for (auto VT : { MVT::v32i8, MVT::v16i16, MVT::v8i32, MVT::v4i64 }) { | |||
1338 | setOperationAction(ISD::ADD, VT, HasInt256 ? Legal : Custom); | |||
1339 | setOperationAction(ISD::SUB, VT, HasInt256 ? Legal : Custom); | |||
1340 | } | |||
1341 | ||||
1342 | setOperationAction(ISD::MUL, MVT::v4i64, Custom); | |||
1343 | setOperationAction(ISD::MUL, MVT::v8i32, HasInt256 ? Legal : Custom); | |||
1344 | setOperationAction(ISD::MUL, MVT::v16i16, HasInt256 ? Legal : Custom); | |||
1345 | setOperationAction(ISD::MUL, MVT::v32i8, Custom); | |||
1346 | ||||
1347 | setOperationAction(ISD::MULHU, MVT::v8i32, Custom); | |||
1348 | setOperationAction(ISD::MULHS, MVT::v8i32, Custom); | |||
1349 | setOperationAction(ISD::MULHU, MVT::v16i16, HasInt256 ? Legal : Custom); | |||
1350 | setOperationAction(ISD::MULHS, MVT::v16i16, HasInt256 ? Legal : Custom); | |||
1351 | setOperationAction(ISD::MULHU, MVT::v32i8, Custom); | |||
1352 | setOperationAction(ISD::MULHS, MVT::v32i8, Custom); | |||
1353 | ||||
1354 | setOperationAction(ISD::SMULO, MVT::v32i8, Custom); | |||
1355 | setOperationAction(ISD::UMULO, MVT::v32i8, Custom); | |||
1356 | ||||
1357 | setOperationAction(ISD::ABS, MVT::v4i64, Custom); | |||
1358 | setOperationAction(ISD::SMAX, MVT::v4i64, Custom); | |||
1359 | setOperationAction(ISD::UMAX, MVT::v4i64, Custom); | |||
1360 | setOperationAction(ISD::SMIN, MVT::v4i64, Custom); | |||
1361 | setOperationAction(ISD::UMIN, MVT::v4i64, Custom); | |||
1362 | ||||
1363 | setOperationAction(ISD::UADDSAT, MVT::v32i8, HasInt256 ? Legal : Custom); | |||
1364 | setOperationAction(ISD::SADDSAT, MVT::v32i8, HasInt256 ? Legal : Custom); | |||
1365 | setOperationAction(ISD::USUBSAT, MVT::v32i8, HasInt256 ? Legal : Custom); | |||
1366 | setOperationAction(ISD::SSUBSAT, MVT::v32i8, HasInt256 ? Legal : Custom); | |||
1367 | setOperationAction(ISD::UADDSAT, MVT::v16i16, HasInt256 ? Legal : Custom); | |||
1368 | setOperationAction(ISD::SADDSAT, MVT::v16i16, HasInt256 ? Legal : Custom); | |||
1369 | setOperationAction(ISD::USUBSAT, MVT::v16i16, HasInt256 ? Legal : Custom); | |||
1370 | setOperationAction(ISD::SSUBSAT, MVT::v16i16, HasInt256 ? Legal : Custom); | |||
1371 | setOperationAction(ISD::UADDSAT, MVT::v8i32, Custom); | |||
1372 | setOperationAction(ISD::USUBSAT, MVT::v8i32, Custom); | |||
1373 | setOperationAction(ISD::UADDSAT, MVT::v4i64, Custom); | |||
1374 | setOperationAction(ISD::USUBSAT, MVT::v4i64, Custom); | |||
1375 | ||||
1376 | for (auto VT : { MVT::v32i8, MVT::v16i16, MVT::v8i32 }) { | |||
1377 | setOperationAction(ISD::ABS, VT, HasInt256 ? Legal : Custom); | |||
1378 | setOperationAction(ISD::SMAX, VT, HasInt256 ? Legal : Custom); | |||
1379 | setOperationAction(ISD::UMAX, VT, HasInt256 ? Legal : Custom); | |||
1380 | setOperationAction(ISD::SMIN, VT, HasInt256 ? Legal : Custom); | |||
1381 | setOperationAction(ISD::UMIN, VT, HasInt256 ? Legal : Custom); | |||
1382 | } | |||
1383 | ||||
1384 | for (auto VT : {MVT::v16i16, MVT::v8i32, MVT::v4i64}) { | |||
1385 | setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, VT, Custom); | |||
1386 | setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, VT, Custom); | |||
1387 | } | |||
1388 | ||||
1389 | if (HasInt256) { | |||
1390 | // The custom lowering for UINT_TO_FP for v8i32 becomes interesting | |||
1391 | // when we have a 256bit-wide blend with immediate. | |||
1392 | setOperationAction(ISD::UINT_TO_FP, MVT::v8i32, Custom); | |||
1393 | setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::v8i32, Custom); | |||
1394 | ||||
1395 | // AVX2 also has wider vector sign/zero extending loads, VPMOV[SZ]X | |||
1396 | for (auto LoadExtOp : { ISD::SEXTLOAD, ISD::ZEXTLOAD }) { | |||
1397 | setLoadExtAction(LoadExtOp, MVT::v16i16, MVT::v16i8, Legal); | |||
1398 | setLoadExtAction(LoadExtOp, MVT::v8i32, MVT::v8i8, Legal); | |||
1399 | setLoadExtAction(LoadExtOp, MVT::v4i64, MVT::v4i8, Legal); | |||
1400 | setLoadExtAction(LoadExtOp, MVT::v8i32, MVT::v8i16, Legal); | |||
1401 | setLoadExtAction(LoadExtOp, MVT::v4i64, MVT::v4i16, Legal); | |||
1402 | setLoadExtAction(LoadExtOp, MVT::v4i64, MVT::v4i32, Legal); | |||
1403 | } | |||
1404 | } | |||
1405 | ||||
1406 | for (auto VT : { MVT::v4i32, MVT::v8i32, MVT::v2i64, MVT::v4i64, | |||
1407 | MVT::v4f32, MVT::v8f32, MVT::v2f64, MVT::v4f64 }) { | |||
1408 | setOperationAction(ISD::MLOAD, VT, Subtarget.hasVLX() ? Legal : Custom); | |||
1409 | setOperationAction(ISD::MSTORE, VT, Legal); | |||
1410 | } | |||
1411 | ||||
1412 | // Extract subvector is special because the value type | |||
1413 | // (result) is 128-bit but the source is 256-bit wide. | |||
1414 | for (auto VT : { MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v2i64, | |||
1415 | MVT::v4f32, MVT::v2f64 }) { | |||
1416 | setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal); | |||
1417 | } | |||
1418 | ||||
1419 | // Custom lower several nodes for 256-bit types. | |||
1420 | for (MVT VT : { MVT::v32i8, MVT::v16i16, MVT::v8i32, MVT::v4i64, | |||
1421 | MVT::v8f32, MVT::v4f64 }) { | |||
1422 | setOperationAction(ISD::BUILD_VECTOR, VT, Custom); | |||
1423 | setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom); | |||
1424 | setOperationAction(ISD::VSELECT, VT, Custom); | |||
1425 | setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom); | |||
1426 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom); | |||
1427 | setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom); | |||
1428 | setOperationAction(ISD::INSERT_SUBVECTOR, VT, Legal); | |||
1429 | setOperationAction(ISD::CONCAT_VECTORS, VT, Custom); | |||
1430 | setOperationAction(ISD::STORE, VT, Custom); | |||
1431 | } | |||
1432 | ||||
1433 | if (HasInt256) { | |||
1434 | setOperationAction(ISD::VSELECT, MVT::v32i8, Legal); | |||
1435 | ||||
1436 | // Custom legalize 2x32 to get a little better code. | |||
1437 | setOperationAction(ISD::MGATHER, MVT::v2f32, Custom); | |||
1438 | setOperationAction(ISD::MGATHER, MVT::v2i32, Custom); | |||
1439 | ||||
1440 | for (auto VT : { MVT::v4i32, MVT::v8i32, MVT::v2i64, MVT::v4i64, | |||
1441 | MVT::v4f32, MVT::v8f32, MVT::v2f64, MVT::v4f64 }) | |||
1442 | setOperationAction(ISD::MGATHER, VT, Custom); | |||
1443 | } | |||
1444 | } | |||
1445 | ||||
1446 | // This block controls legalization of the mask vector sizes that are | |||
1447 | // available with AVX512. 512-bit vectors are in a separate block controlled | |||
1448 | // by useAVX512Regs. | |||
1449 | if (!Subtarget.useSoftFloat() && Subtarget.hasAVX512()) { | |||
1450 | addRegisterClass(MVT::v1i1, &X86::VK1RegClass); | |||
1451 | addRegisterClass(MVT::v2i1, &X86::VK2RegClass); | |||
1452 | addRegisterClass(MVT::v4i1, &X86::VK4RegClass); | |||
1453 | addRegisterClass(MVT::v8i1, &X86::VK8RegClass); | |||
1454 | addRegisterClass(MVT::v16i1, &X86::VK16RegClass); | |||
1455 | ||||
1456 | setOperationAction(ISD::SELECT, MVT::v1i1, Custom); | |||
1457 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v1i1, Custom); | |||
1458 | setOperationAction(ISD::BUILD_VECTOR, MVT::v1i1, Custom); | |||
1459 | ||||
1460 | setOperationPromotedToType(ISD::FP_TO_SINT, MVT::v8i1, MVT::v8i32); | |||
1461 | setOperationPromotedToType(ISD::FP_TO_UINT, MVT::v8i1, MVT::v8i32); | |||
1462 | setOperationPromotedToType(ISD::FP_TO_SINT, MVT::v4i1, MVT::v4i32); | |||
1463 | setOperationPromotedToType(ISD::FP_TO_UINT, MVT::v4i1, MVT::v4i32); | |||
1464 | setOperationPromotedToType(ISD::STRICT_FP_TO_SINT, MVT::v8i1, MVT::v8i32); | |||
1465 | setOperationPromotedToType(ISD::STRICT_FP_TO_UINT, MVT::v8i1, MVT::v8i32); | |||
1466 | setOperationPromotedToType(ISD::STRICT_FP_TO_SINT, MVT::v4i1, MVT::v4i32); | |||
1467 | setOperationPromotedToType(ISD::STRICT_FP_TO_UINT, MVT::v4i1, MVT::v4i32); | |||
1468 | setOperationAction(ISD::FP_TO_SINT, MVT::v2i1, Custom); | |||
1469 | setOperationAction(ISD::FP_TO_UINT, MVT::v2i1, Custom); | |||
1470 | setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::v2i1, Custom); | |||
1471 | setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::v2i1, Custom); | |||
1472 | ||||
1473 | // There is no byte sized k-register load or store without AVX512DQ. | |||
1474 | if (!Subtarget.hasDQI()) { | |||
1475 | setOperationAction(ISD::LOAD, MVT::v1i1, Custom); | |||
1476 | setOperationAction(ISD::LOAD, MVT::v2i1, Custom); | |||
1477 | setOperationAction(ISD::LOAD, MVT::v4i1, Custom); | |||
1478 | setOperationAction(ISD::LOAD, MVT::v8i1, Custom); | |||
1479 | ||||
1480 | setOperationAction(ISD::STORE, MVT::v1i1, Custom); | |||
1481 | setOperationAction(ISD::STORE, MVT::v2i1, Custom); | |||
1482 | setOperationAction(ISD::STORE, MVT::v4i1, Custom); | |||
1483 | setOperationAction(ISD::STORE, MVT::v8i1, Custom); | |||
1484 | } | |||
1485 | ||||
1486 | // Extends of v16i1/v8i1/v4i1/v2i1 to 128-bit vectors. | |||
1487 | for (auto VT : { MVT::v16i8, MVT::v8i16, MVT::v4i32, MVT::v2i64 }) { | |||
1488 | setOperationAction(ISD::SIGN_EXTEND, VT, Custom); | |||
1489 | setOperationAction(ISD::ZERO_EXTEND, VT, Custom); | |||
1490 | setOperationAction(ISD::ANY_EXTEND, VT, Custom); | |||
1491 | } | |||
1492 | ||||
1493 | for (auto VT : { MVT::v1i1, MVT::v2i1, MVT::v4i1, MVT::v8i1, MVT::v16i1 }) | |||
1494 | setOperationAction(ISD::VSELECT, VT, Expand); | |||
1495 | ||||
1496 | for (auto VT : { MVT::v2i1, MVT::v4i1, MVT::v8i1, MVT::v16i1 }) { | |||
1497 | setOperationAction(ISD::SETCC, VT, Custom); | |||
1498 | setOperationAction(ISD::STRICT_FSETCC, VT, Custom); | |||
1499 | setOperationAction(ISD::STRICT_FSETCCS, VT, Custom); | |||
1500 | setOperationAction(ISD::SELECT, VT, Custom); | |||
1501 | setOperationAction(ISD::TRUNCATE, VT, Custom); | |||
1502 | ||||
1503 | setOperationAction(ISD::BUILD_VECTOR, VT, Custom); | |||
1504 | setOperationAction(ISD::CONCAT_VECTORS, VT, Custom); | |||
1505 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom); | |||
1506 | setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom); | |||
1507 | setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom); | |||
1508 | setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom); | |||
1509 | } | |||
1510 | ||||
1511 | for (auto VT : { MVT::v1i1, MVT::v2i1, MVT::v4i1, MVT::v8i1 }) | |||
1512 | setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom); | |||
1513 | } | |||
1514 | ||||
1515 | // This block controls legalization for 512-bit operations with 32/64 bit | |||
1516 | // elements. 512-bits can be disabled based on prefer-vector-width and | |||
1517 | // required-vector-width function attributes. | |||
1518 | if (!Subtarget.useSoftFloat() && Subtarget.useAVX512Regs()) { | |||
1519 | bool HasBWI = Subtarget.hasBWI(); | |||
1520 | ||||
1521 | addRegisterClass(MVT::v16i32, &X86::VR512RegClass); | |||
1522 | addRegisterClass(MVT::v16f32, &X86::VR512RegClass); | |||
1523 | addRegisterClass(MVT::v8i64, &X86::VR512RegClass); | |||
1524 | addRegisterClass(MVT::v8f64, &X86::VR512RegClass); | |||
1525 | addRegisterClass(MVT::v32i16, &X86::VR512RegClass); | |||
1526 | addRegisterClass(MVT::v64i8, &X86::VR512RegClass); | |||
1527 | ||||
1528 | for (auto ExtType : {ISD::ZEXTLOAD, ISD::SEXTLOAD}) { | |||
1529 | setLoadExtAction(ExtType, MVT::v16i32, MVT::v16i8, Legal); | |||
1530 | setLoadExtAction(ExtType, MVT::v16i32, MVT::v16i16, Legal); | |||
1531 | setLoadExtAction(ExtType, MVT::v8i64, MVT::v8i8, Legal); | |||
1532 | setLoadExtAction(ExtType, MVT::v8i64, MVT::v8i16, Legal); | |||
1533 | setLoadExtAction(ExtType, MVT::v8i64, MVT::v8i32, Legal); | |||
1534 | if (HasBWI) | |||
1535 | setLoadExtAction(ExtType, MVT::v32i16, MVT::v32i8, Legal); | |||
1536 | } | |||
1537 | ||||
1538 | for (MVT VT : { MVT::v16f32, MVT::v8f64 }) { | |||
1539 | setOperationAction(ISD::FNEG, VT, Custom); | |||
1540 | setOperationAction(ISD::FABS, VT, Custom); | |||
1541 | setOperationAction(ISD::FMA, VT, Legal); | |||
1542 | setOperationAction(ISD::STRICT_FMA, VT, Legal); | |||
1543 | setOperationAction(ISD::FCOPYSIGN, VT, Custom); | |||
1544 | } | |||
1545 | ||||
1546 | for (MVT VT : { MVT::v16i1, MVT::v16i8, MVT::v16i16 }) { | |||
1547 | setOperationPromotedToType(ISD::FP_TO_SINT , VT, MVT::v16i32); | |||
1548 | setOperationPromotedToType(ISD::FP_TO_UINT , VT, MVT::v16i32); | |||
1549 | setOperationPromotedToType(ISD::STRICT_FP_TO_SINT, VT, MVT::v16i32); | |||
1550 | setOperationPromotedToType(ISD::STRICT_FP_TO_UINT, VT, MVT::v16i32); | |||
1551 | } | |||
1552 | setOperationAction(ISD::FP_TO_SINT, MVT::v16i32, Legal); | |||
1553 | setOperationAction(ISD::FP_TO_UINT, MVT::v16i32, Legal); | |||
1554 | setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::v16i32, Legal); | |||
1555 | setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::v16i32, Legal); | |||
1556 | setOperationAction(ISD::SINT_TO_FP, MVT::v16i32, Legal); | |||
1557 | setOperationAction(ISD::UINT_TO_FP, MVT::v16i32, Legal); | |||
1558 | setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::v16i32, Legal); | |||
1559 | setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::v16i32, Legal); | |||
1560 | ||||
1561 | setOperationAction(ISD::STRICT_FADD, MVT::v16f32, Legal); | |||
1562 | setOperationAction(ISD::STRICT_FADD, MVT::v8f64, Legal); | |||
1563 | setOperationAction(ISD::STRICT_FSUB, MVT::v16f32, Legal); | |||
1564 | setOperationAction(ISD::STRICT_FSUB, MVT::v8f64, Legal); | |||
1565 | setOperationAction(ISD::STRICT_FMUL, MVT::v16f32, Legal); | |||
1566 | setOperationAction(ISD::STRICT_FMUL, MVT::v8f64, Legal); | |||
1567 | setOperationAction(ISD::STRICT_FDIV, MVT::v16f32, Legal); | |||
1568 | setOperationAction(ISD::STRICT_FDIV, MVT::v8f64, Legal); | |||
1569 | setOperationAction(ISD::STRICT_FSQRT, MVT::v16f32, Legal); | |||
1570 | setOperationAction(ISD::STRICT_FSQRT, MVT::v8f64, Legal); | |||
1571 | setOperationAction(ISD::STRICT_FP_EXTEND, MVT::v8f64, Legal); | |||
1572 | setOperationAction(ISD::STRICT_FP_ROUND, MVT::v8f32, Legal); | |||
1573 | ||||
1574 | setTruncStoreAction(MVT::v8i64, MVT::v8i8, Legal); | |||
1575 | setTruncStoreAction(MVT::v8i64, MVT::v8i16, Legal); | |||
1576 | setTruncStoreAction(MVT::v8i64, MVT::v8i32, Legal); | |||
1577 | setTruncStoreAction(MVT::v16i32, MVT::v16i8, Legal); | |||
1578 | setTruncStoreAction(MVT::v16i32, MVT::v16i16, Legal); | |||
1579 | if (HasBWI) | |||
1580 | setTruncStoreAction(MVT::v32i16, MVT::v32i8, Legal); | |||
1581 | ||||
1582 | // With 512-bit vectors and no VLX, we prefer to widen MLOAD/MSTORE | |||
1583 | // to 512-bit rather than use the AVX2 instructions so that we can use | |||
1584 | // k-masks. | |||
1585 | if (!Subtarget.hasVLX()) { | |||
1586 | for (auto VT : {MVT::v4i32, MVT::v8i32, MVT::v2i64, MVT::v4i64, | |||
1587 | MVT::v4f32, MVT::v8f32, MVT::v2f64, MVT::v4f64}) { | |||
1588 | setOperationAction(ISD::MLOAD, VT, Custom); | |||
1589 | setOperationAction(ISD::MSTORE, VT, Custom); | |||
1590 | } | |||
1591 | } | |||
1592 | ||||
1593 | setOperationAction(ISD::TRUNCATE, MVT::v8i32, Legal); | |||
1594 | setOperationAction(ISD::TRUNCATE, MVT::v16i16, Legal); | |||
1595 | setOperationAction(ISD::TRUNCATE, MVT::v32i8, HasBWI ? Legal : Custom); | |||
1596 | setOperationAction(ISD::TRUNCATE, MVT::v16i64, Custom); | |||
1597 | setOperationAction(ISD::ZERO_EXTEND, MVT::v32i16, Custom); | |||
1598 | setOperationAction(ISD::ZERO_EXTEND, MVT::v16i32, Custom); | |||
1599 | setOperationAction(ISD::ZERO_EXTEND, MVT::v8i64, Custom); | |||
1600 | setOperationAction(ISD::ANY_EXTEND, MVT::v32i16, Custom); | |||
1601 | setOperationAction(ISD::ANY_EXTEND, MVT::v16i32, Custom); | |||
1602 | setOperationAction(ISD::ANY_EXTEND, MVT::v8i64, Custom); | |||
1603 | setOperationAction(ISD::SIGN_EXTEND, MVT::v32i16, Custom); | |||
1604 | setOperationAction(ISD::SIGN_EXTEND, MVT::v16i32, Custom); | |||
1605 | setOperationAction(ISD::SIGN_EXTEND, MVT::v8i64, Custom); | |||
1606 | ||||
1607 | if (HasBWI) { | |||
1608 | // Extends from v64i1 masks to 512-bit vectors. | |||
1609 | setOperationAction(ISD::SIGN_EXTEND, MVT::v64i8, Custom); | |||
1610 | setOperationAction(ISD::ZERO_EXTEND, MVT::v64i8, Custom); | |||
1611 | setOperationAction(ISD::ANY_EXTEND, MVT::v64i8, Custom); | |||
1612 | } | |||
1613 | ||||
1614 | for (auto VT : { MVT::v16f32, MVT::v8f64 }) { | |||
1615 | setOperationAction(ISD::FFLOOR, VT, Legal); | |||
1616 | setOperationAction(ISD::STRICT_FFLOOR, VT, Legal); | |||
1617 | setOperationAction(ISD::FCEIL, VT, Legal); | |||
1618 | setOperationAction(ISD::STRICT_FCEIL, VT, Legal); | |||
1619 | setOperationAction(ISD::FTRUNC, VT, Legal); | |||
1620 | setOperationAction(ISD::STRICT_FTRUNC, VT, Legal); | |||
1621 | setOperationAction(ISD::FRINT, VT, Legal); | |||
1622 | setOperationAction(ISD::STRICT_FRINT, VT, Legal); | |||
1623 | setOperationAction(ISD::FNEARBYINT, VT, Legal); | |||
1624 | setOperationAction(ISD::STRICT_FNEARBYINT, VT, Legal); | |||
1625 | setOperationAction(ISD::FROUNDEVEN, VT, Legal); | |||
1626 | setOperationAction(ISD::STRICT_FROUNDEVEN, VT, Legal); | |||
1627 | ||||
1628 | setOperationAction(ISD::FROUND, VT, Custom); | |||
1629 | } | |||
1630 | ||||
1631 | for (auto VT : {MVT::v32i16, MVT::v16i32, MVT::v8i64}) { | |||
1632 | setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, VT, Custom); | |||
1633 | setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, VT, Custom); | |||
1634 | } | |||
1635 | ||||
1636 | setOperationAction(ISD::ADD, MVT::v32i16, HasBWI ? Legal : Custom); | |||
1637 | setOperationAction(ISD::SUB, MVT::v32i16, HasBWI ? Legal : Custom); | |||
1638 | setOperationAction(ISD::ADD, MVT::v64i8, HasBWI ? Legal : Custom); | |||
1639 | setOperationAction(ISD::SUB, MVT::v64i8, HasBWI ? Legal : Custom); | |||
1640 | ||||
1641 | setOperationAction(ISD::MUL, MVT::v8i64, Custom); | |||
1642 | setOperationAction(ISD::MUL, MVT::v16i32, Legal); | |||
1643 | setOperationAction(ISD::MUL, MVT::v32i16, HasBWI ? Legal : Custom); | |||
1644 | setOperationAction(ISD::MUL, MVT::v64i8, Custom); | |||
1645 | ||||
1646 | setOperationAction(ISD::MULHU, MVT::v16i32, Custom); | |||
1647 | setOperationAction(ISD::MULHS, MVT::v16i32, Custom); | |||
1648 | setOperationAction(ISD::MULHS, MVT::v32i16, HasBWI ? Legal : Custom); | |||
1649 | setOperationAction(ISD::MULHU, MVT::v32i16, HasBWI ? Legal : Custom); | |||
1650 | setOperationAction(ISD::MULHS, MVT::v64i8, Custom); | |||
1651 | setOperationAction(ISD::MULHU, MVT::v64i8, Custom); | |||
1652 | ||||
1653 | setOperationAction(ISD::SMULO, MVT::v64i8, Custom); | |||
1654 | setOperationAction(ISD::UMULO, MVT::v64i8, Custom); | |||
1655 | ||||
1656 | setOperationAction(ISD::BITREVERSE, MVT::v64i8, Custom); | |||
1657 | ||||
1658 | for (auto VT : { MVT::v64i8, MVT::v32i16, MVT::v16i32, MVT::v8i64 }) { | |||
1659 | setOperationAction(ISD::SRL, VT, Custom); | |||
1660 | setOperationAction(ISD::SHL, VT, Custom); | |||
1661 | setOperationAction(ISD::SRA, VT, Custom); | |||
1662 | setOperationAction(ISD::SETCC, VT, Custom); | |||
1663 | ||||
1664 | // The condition codes aren't legal in SSE/AVX and under AVX512 we use | |||
1665 | // setcc all the way to isel and prefer SETGT in some isel patterns. | |||
1666 | setCondCodeAction(ISD::SETLT, VT, Custom); | |||
1667 | setCondCodeAction(ISD::SETLE, VT, Custom); | |||
1668 | } | |||
1669 | for (auto VT : { MVT::v16i32, MVT::v8i64 }) { | |||
1670 | setOperationAction(ISD::SMAX, VT, Legal); | |||
1671 | setOperationAction(ISD::UMAX, VT, Legal); | |||
1672 | setOperationAction(ISD::SMIN, VT, Legal); | |||
1673 | setOperationAction(ISD::UMIN, VT, Legal); | |||
1674 | setOperationAction(ISD::ABS, VT, Legal); | |||
1675 | setOperationAction(ISD::CTPOP, VT, Custom); | |||
1676 | setOperationAction(ISD::ROTL, VT, Custom); | |||
1677 | setOperationAction(ISD::ROTR, VT, Custom); | |||
1678 | setOperationAction(ISD::STRICT_FSETCC, VT, Custom); | |||
1679 | setOperationAction(ISD::STRICT_FSETCCS, VT, Custom); | |||
1680 | } | |||
1681 | ||||
1682 | for (auto VT : { MVT::v64i8, MVT::v32i16 }) { | |||
1683 | setOperationAction(ISD::ABS, VT, HasBWI ? Legal : Custom); | |||
1684 | setOperationAction(ISD::CTPOP, VT, Subtarget.hasBITALG() ? Legal : Custom); | |||
1685 | setOperationAction(ISD::CTLZ, VT, Custom); | |||
1686 | setOperationAction(ISD::SMAX, VT, HasBWI ? Legal : Custom); | |||
1687 | setOperationAction(ISD::UMAX, VT, HasBWI ? Legal : Custom); | |||
1688 | setOperationAction(ISD::SMIN, VT, HasBWI ? Legal : Custom); | |||
1689 | setOperationAction(ISD::UMIN, VT, HasBWI ? Legal : Custom); | |||
1690 | setOperationAction(ISD::UADDSAT, VT, HasBWI ? Legal : Custom); | |||
1691 | setOperationAction(ISD::SADDSAT, VT, HasBWI ? Legal : Custom); | |||
1692 | setOperationAction(ISD::USUBSAT, VT, HasBWI ? Legal : Custom); | |||
1693 | setOperationAction(ISD::SSUBSAT, VT, HasBWI ? Legal : Custom); | |||
1694 | } | |||
1695 | ||||
1696 | if (Subtarget.hasDQI()) { | |||
1697 | setOperationAction(ISD::SINT_TO_FP, MVT::v8i64, Legal); | |||
1698 | setOperationAction(ISD::UINT_TO_FP, MVT::v8i64, Legal); | |||
1699 | setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::v8i64, Legal); | |||
1700 | setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::v8i64, Legal); | |||
1701 | setOperationAction(ISD::FP_TO_SINT, MVT::v8i64, Legal); | |||
1702 | setOperationAction(ISD::FP_TO_UINT, MVT::v8i64, Legal); | |||
1703 | setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::v8i64, Legal); | |||
1704 | setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::v8i64, Legal); | |||
1705 | ||||
1706 | setOperationAction(ISD::MUL, MVT::v8i64, Legal); | |||
1707 | } | |||
1708 | ||||
1709 | if (Subtarget.hasCDI()) { | |||
1710 | // NonVLX sub-targets extend 128/256 vectors to use the 512 version. | |||
1711 | for (auto VT : { MVT::v16i32, MVT::v8i64} ) { | |||
1712 | setOperationAction(ISD::CTLZ, VT, Legal); | |||
1713 | } | |||
1714 | } // Subtarget.hasCDI() | |||
1715 | ||||
1716 | if (Subtarget.hasVPOPCNTDQ()) { | |||
1717 | for (auto VT : { MVT::v16i32, MVT::v8i64 }) | |||
1718 | setOperationAction(ISD::CTPOP, VT, Legal); | |||
1719 | } | |||
1720 | ||||
1721 | // Extract subvector is special because the value type | |||
1722 | // (result) is 256-bit but the source is 512-bit wide. | |||
1723 | // 128-bit was made Legal under AVX1. | |||
1724 | for (auto VT : { MVT::v32i8, MVT::v16i16, MVT::v8i32, MVT::v4i64, | |||
1725 | MVT::v8f32, MVT::v4f64 }) | |||
1726 | setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal); | |||
1727 | ||||
1728 | for (auto VT : { MVT::v64i8, MVT::v32i16, MVT::v16i32, MVT::v8i64, | |||
1729 | MVT::v16f32, MVT::v8f64 }) { | |||
1730 | setOperationAction(ISD::CONCAT_VECTORS, VT, Custom); | |||
1731 | setOperationAction(ISD::INSERT_SUBVECTOR, VT, Legal); | |||
1732 | setOperationAction(ISD::SELECT, VT, Custom); | |||
1733 | setOperationAction(ISD::VSELECT, VT, Custom); | |||
1734 | setOperationAction(ISD::BUILD_VECTOR, VT, Custom); | |||
1735 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom); | |||
1736 | setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom); | |||
1737 | setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom); | |||
1738 | setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom); | |||
1739 | } | |||
1740 | ||||
1741 | for (auto VT : { MVT::v16i32, MVT::v8i64, MVT::v16f32, MVT::v8f64 }) { | |||
1742 | setOperationAction(ISD::MLOAD, VT, Legal); | |||
1743 | setOperationAction(ISD::MSTORE, VT, Legal); | |||
1744 | setOperationAction(ISD::MGATHER, VT, Custom); | |||
1745 | setOperationAction(ISD::MSCATTER, VT, Custom); | |||
1746 | } | |||
1747 | if (HasBWI) { | |||
1748 | for (auto VT : { MVT::v64i8, MVT::v32i16 }) { | |||
1749 | setOperationAction(ISD::MLOAD, VT, Legal); | |||
1750 | setOperationAction(ISD::MSTORE, VT, Legal); | |||
1751 | } | |||
1752 | } else { | |||
1753 | setOperationAction(ISD::STORE, MVT::v32i16, Custom); | |||
1754 | setOperationAction(ISD::STORE, MVT::v64i8, Custom); | |||
1755 | } | |||
1756 | ||||
1757 | if (Subtarget.hasVBMI2()) { | |||
1758 | for (auto VT : { MVT::v8i16, MVT::v4i32, MVT::v2i64, | |||
1759 | MVT::v16i16, MVT::v8i32, MVT::v4i64, | |||
1760 | MVT::v32i16, MVT::v16i32, MVT::v8i64 }) { | |||
1761 | setOperationAction(ISD::FSHL, VT, Custom); | |||
1762 | setOperationAction(ISD::FSHR, VT, Custom); | |||
1763 | } | |||
1764 | ||||
1765 | setOperationAction(ISD::ROTL, MVT::v32i16, Custom); | |||
1766 | setOperationAction(ISD::ROTR, MVT::v8i16, Custom); | |||
1767 | setOperationAction(ISD::ROTR, MVT::v16i16, Custom); | |||
1768 | setOperationAction(ISD::ROTR, MVT::v32i16, Custom); | |||
1769 | } | |||
1770 | }// useAVX512Regs | |||
1771 | ||||
1772 | // This block controls legalization for operations that don't have | |||
1773 | // pre-AVX512 equivalents. Without VLX we use 512-bit operations for | |||
1774 | // narrower widths. | |||
1775 | if (!Subtarget.useSoftFloat() && Subtarget.hasAVX512()) { | |||
1776 | // These operations are handled on non-VLX by artificially widening in | |||
1777 | // isel patterns. | |||
1778 | ||||
1779 | setOperationAction(ISD::FP_TO_UINT, MVT::v8i32, | |||
1780 | Subtarget.hasVLX() ? Legal : Custom); | |||
1781 | setOperationAction(ISD::FP_TO_UINT, MVT::v4i32, | |||
1782 | Subtarget.hasVLX() ? Legal : Custom); | |||
1783 | setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::v8i32, | |||
1784 | Subtarget.hasVLX() ? Legal : Custom); | |||
1785 | setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::v4i32, | |||
1786 | Subtarget.hasVLX() ? Legal : Custom); | |||
1787 | setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::v2i32, Custom); | |||
1788 | setOperationAction(ISD::UINT_TO_FP, MVT::v8i32, | |||
1789 | Subtarget.hasVLX() ? Legal : Custom); | |||
1790 | setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, | |||
1791 | Subtarget.hasVLX() ? Legal : Custom); | |||
1792 | setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::v8i32, | |||
1793 | Subtarget.hasVLX() ? Legal : Custom); | |||
1794 | setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::v4i32, | |||
1795 | Subtarget.hasVLX() ? Legal : Custom); | |||
1796 | ||||
1797 | if (Subtarget.hasDQI()) { | |||
1798 | // Fast v2f32 SINT_TO_FP( v2i64 ) custom conversion. | |||
1799 | // v2f32 UINT_TO_FP is already custom under SSE2. | |||
1800 | assert(isOperationCustom(ISD::UINT_TO_FP, MVT::v2f32) &&(static_cast <bool> (isOperationCustom(ISD::UINT_TO_FP, MVT::v2f32) && isOperationCustom(ISD::STRICT_UINT_TO_FP , MVT::v2f32) && "Unexpected operation action!") ? void (0) : __assert_fail ("isOperationCustom(ISD::UINT_TO_FP, MVT::v2f32) && isOperationCustom(ISD::STRICT_UINT_TO_FP, MVT::v2f32) && \"Unexpected operation action!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 1802, __extension__ __PRETTY_FUNCTION__)) | |||
1801 | isOperationCustom(ISD::STRICT_UINT_TO_FP, MVT::v2f32) &&(static_cast <bool> (isOperationCustom(ISD::UINT_TO_FP, MVT::v2f32) && isOperationCustom(ISD::STRICT_UINT_TO_FP , MVT::v2f32) && "Unexpected operation action!") ? void (0) : __assert_fail ("isOperationCustom(ISD::UINT_TO_FP, MVT::v2f32) && isOperationCustom(ISD::STRICT_UINT_TO_FP, MVT::v2f32) && \"Unexpected operation action!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 1802, __extension__ __PRETTY_FUNCTION__)) | |||
1802 | "Unexpected operation action!")(static_cast <bool> (isOperationCustom(ISD::UINT_TO_FP, MVT::v2f32) && isOperationCustom(ISD::STRICT_UINT_TO_FP , MVT::v2f32) && "Unexpected operation action!") ? void (0) : __assert_fail ("isOperationCustom(ISD::UINT_TO_FP, MVT::v2f32) && isOperationCustom(ISD::STRICT_UINT_TO_FP, MVT::v2f32) && \"Unexpected operation action!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 1802, __extension__ __PRETTY_FUNCTION__)); | |||
1803 | // v2i64 FP_TO_S/UINT(v2f32) custom conversion. | |||
1804 | setOperationAction(ISD::FP_TO_SINT, MVT::v2f32, Custom); | |||
1805 | setOperationAction(ISD::FP_TO_UINT, MVT::v2f32, Custom); | |||
1806 | setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::v2f32, Custom); | |||
1807 | setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::v2f32, Custom); | |||
1808 | } | |||
1809 | ||||
1810 | for (auto VT : { MVT::v2i64, MVT::v4i64 }) { | |||
1811 | setOperationAction(ISD::SMAX, VT, Legal); | |||
1812 | setOperationAction(ISD::UMAX, VT, Legal); | |||
1813 | setOperationAction(ISD::SMIN, VT, Legal); | |||
1814 | setOperationAction(ISD::UMIN, VT, Legal); | |||
1815 | setOperationAction(ISD::ABS, VT, Legal); | |||
1816 | } | |||
1817 | ||||
1818 | for (auto VT : { MVT::v4i32, MVT::v8i32, MVT::v2i64, MVT::v4i64 }) { | |||
1819 | setOperationAction(ISD::ROTL, VT, Custom); | |||
1820 | setOperationAction(ISD::ROTR, VT, Custom); | |||
1821 | } | |||
1822 | ||||
1823 | // Custom legalize 2x32 to get a little better code. | |||
1824 | setOperationAction(ISD::MSCATTER, MVT::v2f32, Custom); | |||
1825 | setOperationAction(ISD::MSCATTER, MVT::v2i32, Custom); | |||
1826 | ||||
1827 | for (auto VT : { MVT::v4i32, MVT::v8i32, MVT::v2i64, MVT::v4i64, | |||
1828 | MVT::v4f32, MVT::v8f32, MVT::v2f64, MVT::v4f64 }) | |||
1829 | setOperationAction(ISD::MSCATTER, VT, Custom); | |||
1830 | ||||
1831 | if (Subtarget.hasDQI()) { | |||
1832 | for (auto VT : { MVT::v2i64, MVT::v4i64 }) { | |||
1833 | setOperationAction(ISD::SINT_TO_FP, VT, | |||
1834 | Subtarget.hasVLX() ? Legal : Custom); | |||
1835 | setOperationAction(ISD::UINT_TO_FP, VT, | |||
1836 | Subtarget.hasVLX() ? Legal : Custom); | |||
1837 | setOperationAction(ISD::STRICT_SINT_TO_FP, VT, | |||
1838 | Subtarget.hasVLX() ? Legal : Custom); | |||
1839 | setOperationAction(ISD::STRICT_UINT_TO_FP, VT, | |||
1840 | Subtarget.hasVLX() ? Legal : Custom); | |||
1841 | setOperationAction(ISD::FP_TO_SINT, VT, | |||
1842 | Subtarget.hasVLX() ? Legal : Custom); | |||
1843 | setOperationAction(ISD::FP_TO_UINT, VT, | |||
1844 | Subtarget.hasVLX() ? Legal : Custom); | |||
1845 | setOperationAction(ISD::STRICT_FP_TO_SINT, VT, | |||
1846 | Subtarget.hasVLX() ? Legal : Custom); | |||
1847 | setOperationAction(ISD::STRICT_FP_TO_UINT, VT, | |||
1848 | Subtarget.hasVLX() ? Legal : Custom); | |||
1849 | setOperationAction(ISD::MUL, VT, Legal); | |||
1850 | } | |||
1851 | } | |||
1852 | ||||
1853 | if (Subtarget.hasCDI()) { | |||
1854 | for (auto VT : { MVT::v4i32, MVT::v8i32, MVT::v2i64, MVT::v4i64 }) { | |||
1855 | setOperationAction(ISD::CTLZ, VT, Legal); | |||
1856 | } | |||
1857 | } // Subtarget.hasCDI() | |||
1858 | ||||
1859 | if (Subtarget.hasVPOPCNTDQ()) { | |||
1860 | for (auto VT : { MVT::v4i32, MVT::v8i32, MVT::v2i64, MVT::v4i64 }) | |||
1861 | setOperationAction(ISD::CTPOP, VT, Legal); | |||
1862 | } | |||
1863 | } | |||
1864 | ||||
1865 | // This block control legalization of v32i1/v64i1 which are available with | |||
1866 | // AVX512BW. 512-bit v32i16 and v64i8 vector legalization is controlled with | |||
1867 | // useBWIRegs. | |||
1868 | if (!Subtarget.useSoftFloat() && Subtarget.hasBWI()) { | |||
1869 | addRegisterClass(MVT::v32i1, &X86::VK32RegClass); | |||
1870 | addRegisterClass(MVT::v64i1, &X86::VK64RegClass); | |||
1871 | ||||
1872 | for (auto VT : { MVT::v32i1, MVT::v64i1 }) { | |||
1873 | setOperationAction(ISD::VSELECT, VT, Expand); | |||
1874 | setOperationAction(ISD::TRUNCATE, VT, Custom); | |||
1875 | setOperationAction(ISD::SETCC, VT, Custom); | |||
1876 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom); | |||
1877 | setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Custom); | |||
1878 | setOperationAction(ISD::SELECT, VT, Custom); | |||
1879 | setOperationAction(ISD::BUILD_VECTOR, VT, Custom); | |||
1880 | setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom); | |||
1881 | setOperationAction(ISD::CONCAT_VECTORS, VT, Custom); | |||
1882 | setOperationAction(ISD::INSERT_SUBVECTOR, VT, Custom); | |||
1883 | } | |||
1884 | ||||
1885 | for (auto VT : { MVT::v16i1, MVT::v32i1 }) | |||
1886 | setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom); | |||
1887 | ||||
1888 | // Extends from v32i1 masks to 256-bit vectors. | |||
1889 | setOperationAction(ISD::SIGN_EXTEND, MVT::v32i8, Custom); | |||
1890 | setOperationAction(ISD::ZERO_EXTEND, MVT::v32i8, Custom); | |||
1891 | setOperationAction(ISD::ANY_EXTEND, MVT::v32i8, Custom); | |||
1892 | ||||
1893 | for (auto VT : { MVT::v32i8, MVT::v16i8, MVT::v16i16, MVT::v8i16 }) { | |||
1894 | setOperationAction(ISD::MLOAD, VT, Subtarget.hasVLX() ? Legal : Custom); | |||
1895 | setOperationAction(ISD::MSTORE, VT, Subtarget.hasVLX() ? Legal : Custom); | |||
1896 | } | |||
1897 | ||||
1898 | // These operations are handled on non-VLX by artificially widening in | |||
1899 | // isel patterns. | |||
1900 | // TODO: Custom widen in lowering on non-VLX and drop the isel patterns? | |||
1901 | ||||
1902 | if (Subtarget.hasBITALG()) { | |||
1903 | for (auto VT : { MVT::v16i8, MVT::v32i8, MVT::v8i16, MVT::v16i16 }) | |||
1904 | setOperationAction(ISD::CTPOP, VT, Legal); | |||
1905 | } | |||
1906 | } | |||
1907 | ||||
1908 | if (!Subtarget.useSoftFloat() && Subtarget.hasFP16()) { | |||
1909 | auto setGroup = [&] (MVT VT) { | |||
1910 | setOperationAction(ISD::FADD, VT, Legal); | |||
1911 | setOperationAction(ISD::STRICT_FADD, VT, Legal); | |||
1912 | setOperationAction(ISD::FSUB, VT, Legal); | |||
1913 | setOperationAction(ISD::STRICT_FSUB, VT, Legal); | |||
1914 | setOperationAction(ISD::FMUL, VT, Legal); | |||
1915 | setOperationAction(ISD::STRICT_FMUL, VT, Legal); | |||
1916 | setOperationAction(ISD::FDIV, VT, Legal); | |||
1917 | setOperationAction(ISD::STRICT_FDIV, VT, Legal); | |||
1918 | setOperationAction(ISD::FSQRT, VT, Legal); | |||
1919 | setOperationAction(ISD::STRICT_FSQRT, VT, Legal); | |||
1920 | ||||
1921 | setOperationAction(ISD::FFLOOR, VT, Legal); | |||
1922 | setOperationAction(ISD::STRICT_FFLOOR, VT, Legal); | |||
1923 | setOperationAction(ISD::FCEIL, VT, Legal); | |||
1924 | setOperationAction(ISD::STRICT_FCEIL, VT, Legal); | |||
1925 | setOperationAction(ISD::FTRUNC, VT, Legal); | |||
1926 | setOperationAction(ISD::STRICT_FTRUNC, VT, Legal); | |||
1927 | setOperationAction(ISD::FRINT, VT, Legal); | |||
1928 | setOperationAction(ISD::STRICT_FRINT, VT, Legal); | |||
1929 | setOperationAction(ISD::FNEARBYINT, VT, Legal); | |||
1930 | setOperationAction(ISD::STRICT_FNEARBYINT, VT, Legal); | |||
1931 | ||||
1932 | setOperationAction(ISD::LOAD, VT, Legal); | |||
1933 | setOperationAction(ISD::STORE, VT, Legal); | |||
1934 | ||||
1935 | setOperationAction(ISD::FMA, VT, Legal); | |||
1936 | setOperationAction(ISD::STRICT_FMA, VT, Legal); | |||
1937 | setOperationAction(ISD::VSELECT, VT, Legal); | |||
1938 | setOperationAction(ISD::BUILD_VECTOR, VT, Custom); | |||
1939 | setOperationAction(ISD::SELECT, VT, Custom); | |||
1940 | ||||
1941 | setOperationAction(ISD::FNEG, VT, Custom); | |||
1942 | setOperationAction(ISD::FABS, VT, Custom); | |||
1943 | setOperationAction(ISD::FCOPYSIGN, VT, Custom); | |||
1944 | setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom); | |||
1945 | setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom); | |||
1946 | }; | |||
1947 | ||||
1948 | // AVX512_FP16 scalar operations | |||
1949 | setGroup(MVT::f16); | |||
1950 | addRegisterClass(MVT::f16, &X86::FR16XRegClass); | |||
1951 | setOperationAction(ISD::SELECT_CC, MVT::f16, Expand); | |||
1952 | setOperationAction(ISD::BR_CC, MVT::f16, Expand); | |||
1953 | setOperationAction(ISD::SETCC, MVT::f16, Custom); | |||
1954 | setOperationAction(ISD::STRICT_FSETCC, MVT::f16, Custom); | |||
1955 | setOperationAction(ISD::STRICT_FSETCCS, MVT::f16, Custom); | |||
1956 | setOperationAction(ISD::FROUND, MVT::f16, Custom); | |||
1957 | setOperationAction(ISD::STRICT_FROUND, MVT::f16, Custom); | |||
1958 | setOperationAction(ISD::FROUNDEVEN, MVT::f16, Legal); | |||
1959 | setOperationAction(ISD::STRICT_FROUNDEVEN, MVT::f16, Legal); | |||
1960 | setOperationAction(ISD::FP_ROUND, MVT::f16, Custom); | |||
1961 | setOperationAction(ISD::STRICT_FP_ROUND, MVT::f16, Custom); | |||
1962 | setOperationAction(ISD::STRICT_FP_EXTEND, MVT::f32, Legal); | |||
1963 | if (isTypeLegal(MVT::f80)) { | |||
1964 | setOperationAction(ISD::FP_EXTEND, MVT::f80, Custom); | |||
1965 | setOperationAction(ISD::STRICT_FP_EXTEND, MVT::f80, Custom); | |||
1966 | } | |||
1967 | ||||
1968 | setCondCodeAction(ISD::SETOEQ, MVT::f16, Expand); | |||
1969 | setCondCodeAction(ISD::SETUNE, MVT::f16, Expand); | |||
1970 | ||||
1971 | if (Subtarget.useAVX512Regs()) { | |||
1972 | setGroup(MVT::v32f16); | |||
1973 | addRegisterClass(MVT::v32f16, &X86::VR512RegClass); | |||
1974 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32f16, Custom); | |||
1975 | setOperationAction(ISD::SINT_TO_FP, MVT::v32i16, Legal); | |||
1976 | setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::v32i16, Legal); | |||
1977 | setOperationAction(ISD::UINT_TO_FP, MVT::v32i16, Legal); | |||
1978 | setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::v32i16, Legal); | |||
1979 | setOperationAction(ISD::STRICT_FP_ROUND, MVT::v16f16, Legal); | |||
1980 | setOperationAction(ISD::STRICT_FP_EXTEND, MVT::v16f32, Legal); | |||
1981 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v32f16, Custom); | |||
1982 | ||||
1983 | setOperationAction(ISD::FP_TO_SINT, MVT::v32i16, Custom); | |||
1984 | setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::v32i16, Custom); | |||
1985 | setOperationAction(ISD::FP_TO_UINT, MVT::v32i16, Custom); | |||
1986 | setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::v32i16, Custom); | |||
1987 | setOperationPromotedToType(ISD::FP_TO_SINT, MVT::v32i8, MVT::v32i16); | |||
1988 | setOperationPromotedToType(ISD::STRICT_FP_TO_SINT, MVT::v32i8, | |||
1989 | MVT::v32i16); | |||
1990 | setOperationPromotedToType(ISD::FP_TO_UINT, MVT::v32i8, MVT::v32i16); | |||
1991 | setOperationPromotedToType(ISD::STRICT_FP_TO_UINT, MVT::v32i8, | |||
1992 | MVT::v32i16); | |||
1993 | setOperationPromotedToType(ISD::FP_TO_SINT, MVT::v32i1, MVT::v32i16); | |||
1994 | setOperationPromotedToType(ISD::STRICT_FP_TO_SINT, MVT::v32i1, | |||
1995 | MVT::v32i16); | |||
1996 | setOperationPromotedToType(ISD::FP_TO_UINT, MVT::v32i1, MVT::v32i16); | |||
1997 | setOperationPromotedToType(ISD::STRICT_FP_TO_UINT, MVT::v32i1, | |||
1998 | MVT::v32i16); | |||
1999 | ||||
2000 | setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v16f16, Legal); | |||
2001 | setOperationAction(ISD::INSERT_SUBVECTOR, MVT::v32f16, Legal); | |||
2002 | setOperationAction(ISD::CONCAT_VECTORS, MVT::v32f16, Custom); | |||
2003 | ||||
2004 | setLoadExtAction(ISD::EXTLOAD, MVT::v8f64, MVT::v8f16, Legal); | |||
2005 | setLoadExtAction(ISD::EXTLOAD, MVT::v16f32, MVT::v16f16, Legal); | |||
2006 | ||||
2007 | setOperationAction(ISD::STRICT_FSETCC, MVT::v32i1, Custom); | |||
2008 | setOperationAction(ISD::STRICT_FSETCCS, MVT::v32i1, Custom); | |||
2009 | } | |||
2010 | ||||
2011 | if (Subtarget.hasVLX()) { | |||
2012 | addRegisterClass(MVT::v8f16, &X86::VR128XRegClass); | |||
2013 | addRegisterClass(MVT::v16f16, &X86::VR256XRegClass); | |||
2014 | setGroup(MVT::v8f16); | |||
2015 | setGroup(MVT::v16f16); | |||
2016 | ||||
2017 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8f16, Legal); | |||
2018 | setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16f16, Custom); | |||
2019 | setOperationAction(ISD::SINT_TO_FP, MVT::v16i16, Legal); | |||
2020 | setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::v16i16, Legal); | |||
2021 | setOperationAction(ISD::SINT_TO_FP, MVT::v8i16, Legal); | |||
2022 | setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::v8i16, Legal); | |||
2023 | setOperationAction(ISD::UINT_TO_FP, MVT::v16i16, Legal); | |||
2024 | setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::v16i16, Legal); | |||
2025 | setOperationAction(ISD::UINT_TO_FP, MVT::v8i16, Legal); | |||
2026 | setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::v8i16, Legal); | |||
2027 | ||||
2028 | setOperationAction(ISD::FP_TO_SINT, MVT::v8i16, Custom); | |||
2029 | setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::v8i16, Custom); | |||
2030 | setOperationAction(ISD::FP_TO_UINT, MVT::v8i16, Custom); | |||
2031 | setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::v8i16, Custom); | |||
2032 | setOperationAction(ISD::STRICT_FP_ROUND, MVT::v8f16, Legal); | |||
2033 | setOperationAction(ISD::STRICT_FP_EXTEND, MVT::v8f32, Legal); | |||
2034 | ||||
2035 | // INSERT_VECTOR_ELT v8f16 extended to VECTOR_SHUFFLE | |||
2036 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f16, Custom); | |||
2037 | setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16f16, Custom); | |||
2038 | ||||
2039 | setOperationAction(ISD::EXTRACT_SUBVECTOR, MVT::v8f16, Legal); | |||
2040 | setOperationAction(ISD::INSERT_SUBVECTOR, MVT::v16f16, Legal); | |||
2041 | setOperationAction(ISD::CONCAT_VECTORS, MVT::v16f16, Custom); | |||
2042 | ||||
2043 | setLoadExtAction(ISD::EXTLOAD, MVT::v4f64, MVT::v4f16, Legal); | |||
2044 | setLoadExtAction(ISD::EXTLOAD, MVT::v2f64, MVT::v2f16, Legal); | |||
2045 | setLoadExtAction(ISD::EXTLOAD, MVT::v8f32, MVT::v8f16, Legal); | |||
2046 | setLoadExtAction(ISD::EXTLOAD, MVT::v4f32, MVT::v4f16, Legal); | |||
2047 | ||||
2048 | // Need to custom widen these to prevent scalarization. | |||
2049 | setOperationAction(ISD::LOAD, MVT::v4f16, Custom); | |||
2050 | setOperationAction(ISD::STORE, MVT::v4f16, Custom); | |||
2051 | } | |||
2052 | ||||
2053 | // Support fp16 0 immediate | |||
2054 | addLegalFPImmediate(APFloat::getZero(APFloat::IEEEhalf())); | |||
2055 | } | |||
2056 | ||||
2057 | if (!Subtarget.useSoftFloat() && Subtarget.hasVLX()) { | |||
2058 | setTruncStoreAction(MVT::v4i64, MVT::v4i8, Legal); | |||
2059 | setTruncStoreAction(MVT::v4i64, MVT::v4i16, Legal); | |||
2060 | setTruncStoreAction(MVT::v4i64, MVT::v4i32, Legal); | |||
2061 | setTruncStoreAction(MVT::v8i32, MVT::v8i8, Legal); | |||
2062 | setTruncStoreAction(MVT::v8i32, MVT::v8i16, Legal); | |||
2063 | ||||
2064 | setTruncStoreAction(MVT::v2i64, MVT::v2i8, Legal); | |||
2065 | setTruncStoreAction(MVT::v2i64, MVT::v2i16, Legal); | |||
2066 | setTruncStoreAction(MVT::v2i64, MVT::v2i32, Legal); | |||
2067 | setTruncStoreAction(MVT::v4i32, MVT::v4i8, Legal); | |||
2068 | setTruncStoreAction(MVT::v4i32, MVT::v4i16, Legal); | |||
2069 | ||||
2070 | if (Subtarget.hasBWI()) { | |||
2071 | setTruncStoreAction(MVT::v16i16, MVT::v16i8, Legal); | |||
2072 | setTruncStoreAction(MVT::v8i16, MVT::v8i8, Legal); | |||
2073 | } | |||
2074 | ||||
2075 | if (Subtarget.hasFP16()) { | |||
2076 | // vcvttph2[u]dq v4f16 -> v4i32/64, v2f16 -> v2i32/64 | |||
2077 | setOperationAction(ISD::FP_TO_SINT, MVT::v2f16, Custom); | |||
2078 | setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::v2f16, Custom); | |||
2079 | setOperationAction(ISD::FP_TO_UINT, MVT::v2f16, Custom); | |||
2080 | setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::v2f16, Custom); | |||
2081 | setOperationAction(ISD::FP_TO_SINT, MVT::v4f16, Custom); | |||
2082 | setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::v4f16, Custom); | |||
2083 | setOperationAction(ISD::FP_TO_UINT, MVT::v4f16, Custom); | |||
2084 | setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::v4f16, Custom); | |||
2085 | // vcvt[u]dq2ph v4i32/64 -> v4f16, v2i32/64 -> v2f16 | |||
2086 | setOperationAction(ISD::SINT_TO_FP, MVT::v2f16, Custom); | |||
2087 | setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::v2f16, Custom); | |||
2088 | setOperationAction(ISD::UINT_TO_FP, MVT::v2f16, Custom); | |||
2089 | setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::v2f16, Custom); | |||
2090 | setOperationAction(ISD::SINT_TO_FP, MVT::v4f16, Custom); | |||
2091 | setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::v4f16, Custom); | |||
2092 | setOperationAction(ISD::UINT_TO_FP, MVT::v4f16, Custom); | |||
2093 | setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::v4f16, Custom); | |||
2094 | // vcvtps2phx v4f32 -> v4f16, v2f32 -> v2f16 | |||
2095 | setOperationAction(ISD::FP_ROUND, MVT::v2f16, Custom); | |||
2096 | setOperationAction(ISD::STRICT_FP_ROUND, MVT::v2f16, Custom); | |||
2097 | setOperationAction(ISD::FP_ROUND, MVT::v4f16, Custom); | |||
2098 | setOperationAction(ISD::STRICT_FP_ROUND, MVT::v4f16, Custom); | |||
2099 | // vcvtph2psx v4f16 -> v4f32, v2f16 -> v2f32 | |||
2100 | setOperationAction(ISD::FP_EXTEND, MVT::v2f16, Custom); | |||
2101 | setOperationAction(ISD::STRICT_FP_EXTEND, MVT::v2f16, Custom); | |||
2102 | setOperationAction(ISD::FP_EXTEND, MVT::v4f16, Custom); | |||
2103 | setOperationAction(ISD::STRICT_FP_EXTEND, MVT::v4f16, Custom); | |||
2104 | } | |||
2105 | ||||
2106 | setOperationAction(ISD::TRUNCATE, MVT::v16i32, Custom); | |||
2107 | setOperationAction(ISD::TRUNCATE, MVT::v8i64, Custom); | |||
2108 | setOperationAction(ISD::TRUNCATE, MVT::v16i64, Custom); | |||
2109 | } | |||
2110 | ||||
2111 | if (Subtarget.hasAMXTILE()) { | |||
2112 | addRegisterClass(MVT::x86amx, &X86::TILERegClass); | |||
2113 | } | |||
2114 | ||||
2115 | // We want to custom lower some of our intrinsics. | |||
2116 | setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); | |||
2117 | setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom); | |||
2118 | setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom); | |||
2119 | if (!Subtarget.is64Bit()) { | |||
2120 | setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i64, Custom); | |||
2121 | } | |||
2122 | ||||
2123 | // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't | |||
2124 | // handle type legalization for these operations here. | |||
2125 | // | |||
2126 | // FIXME: We really should do custom legalization for addition and | |||
2127 | // subtraction on x86-32 once PR3203 is fixed. We really can't do much better | |||
2128 | // than generic legalization for 64-bit multiplication-with-overflow, though. | |||
2129 | for (auto VT : { MVT::i8, MVT::i16, MVT::i32, MVT::i64 }) { | |||
2130 | if (VT == MVT::i64 && !Subtarget.is64Bit()) | |||
2131 | continue; | |||
2132 | // Add/Sub/Mul with overflow operations are custom lowered. | |||
2133 | setOperationAction(ISD::SADDO, VT, Custom); | |||
2134 | setOperationAction(ISD::UADDO, VT, Custom); | |||
2135 | setOperationAction(ISD::SSUBO, VT, Custom); | |||
2136 | setOperationAction(ISD::USUBO, VT, Custom); | |||
2137 | setOperationAction(ISD::SMULO, VT, Custom); | |||
2138 | setOperationAction(ISD::UMULO, VT, Custom); | |||
2139 | ||||
2140 | // Support carry in as value rather than glue. | |||
2141 | setOperationAction(ISD::ADDCARRY, VT, Custom); | |||
2142 | setOperationAction(ISD::SUBCARRY, VT, Custom); | |||
2143 | setOperationAction(ISD::SETCCCARRY, VT, Custom); | |||
2144 | setOperationAction(ISD::SADDO_CARRY, VT, Custom); | |||
2145 | setOperationAction(ISD::SSUBO_CARRY, VT, Custom); | |||
2146 | } | |||
2147 | ||||
2148 | if (!Subtarget.is64Bit()) { | |||
2149 | // These libcalls are not available in 32-bit. | |||
2150 | setLibcallName(RTLIB::SHL_I128, nullptr); | |||
2151 | setLibcallName(RTLIB::SRL_I128, nullptr); | |||
2152 | setLibcallName(RTLIB::SRA_I128, nullptr); | |||
2153 | setLibcallName(RTLIB::MUL_I128, nullptr); | |||
2154 | // The MULO libcall is not part of libgcc, only compiler-rt. | |||
2155 | setLibcallName(RTLIB::MULO_I64, nullptr); | |||
2156 | } | |||
2157 | // The MULO libcall is not part of libgcc, only compiler-rt. | |||
2158 | setLibcallName(RTLIB::MULO_I128, nullptr); | |||
2159 | ||||
2160 | // Combine sin / cos into _sincos_stret if it is available. | |||
2161 | if (getLibcallName(RTLIB::SINCOS_STRET_F32) != nullptr && | |||
2162 | getLibcallName(RTLIB::SINCOS_STRET_F64) != nullptr) { | |||
2163 | setOperationAction(ISD::FSINCOS, MVT::f64, Custom); | |||
2164 | setOperationAction(ISD::FSINCOS, MVT::f32, Custom); | |||
2165 | } | |||
2166 | ||||
2167 | if (Subtarget.isTargetWin64()) { | |||
2168 | setOperationAction(ISD::SDIV, MVT::i128, Custom); | |||
2169 | setOperationAction(ISD::UDIV, MVT::i128, Custom); | |||
2170 | setOperationAction(ISD::SREM, MVT::i128, Custom); | |||
2171 | setOperationAction(ISD::UREM, MVT::i128, Custom); | |||
2172 | setOperationAction(ISD::FP_TO_SINT, MVT::i128, Custom); | |||
2173 | setOperationAction(ISD::FP_TO_UINT, MVT::i128, Custom); | |||
2174 | setOperationAction(ISD::SINT_TO_FP, MVT::i128, Custom); | |||
2175 | setOperationAction(ISD::UINT_TO_FP, MVT::i128, Custom); | |||
2176 | setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::i128, Custom); | |||
2177 | setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::i128, Custom); | |||
2178 | setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::i128, Custom); | |||
2179 | setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::i128, Custom); | |||
2180 | } | |||
2181 | ||||
2182 | // On 32 bit MSVC, `fmodf(f32)` is not defined - only `fmod(f64)` | |||
2183 | // is. We should promote the value to 64-bits to solve this. | |||
2184 | // This is what the CRT headers do - `fmodf` is an inline header | |||
2185 | // function casting to f64 and calling `fmod`. | |||
2186 | if (Subtarget.is32Bit() && | |||
2187 | (Subtarget.isTargetWindowsMSVC() || Subtarget.isTargetWindowsItanium())) | |||
2188 | for (ISD::NodeType Op : | |||
2189 | {ISD::FCEIL, ISD::STRICT_FCEIL, | |||
2190 | ISD::FCOS, ISD::STRICT_FCOS, | |||
2191 | ISD::FEXP, ISD::STRICT_FEXP, | |||
2192 | ISD::FFLOOR, ISD::STRICT_FFLOOR, | |||
2193 | ISD::FREM, ISD::STRICT_FREM, | |||
2194 | ISD::FLOG, ISD::STRICT_FLOG, | |||
2195 | ISD::FLOG10, ISD::STRICT_FLOG10, | |||
2196 | ISD::FPOW, ISD::STRICT_FPOW, | |||
2197 | ISD::FSIN, ISD::STRICT_FSIN}) | |||
2198 | if (isOperationExpand(Op, MVT::f32)) | |||
2199 | setOperationAction(Op, MVT::f32, Promote); | |||
2200 | ||||
2201 | // We have target-specific dag combine patterns for the following nodes: | |||
2202 | setTargetDAGCombine(ISD::VECTOR_SHUFFLE); | |||
2203 | setTargetDAGCombine(ISD::SCALAR_TO_VECTOR); | |||
2204 | setTargetDAGCombine(ISD::INSERT_VECTOR_ELT); | |||
2205 | setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT); | |||
2206 | setTargetDAGCombine(ISD::CONCAT_VECTORS); | |||
2207 | setTargetDAGCombine(ISD::INSERT_SUBVECTOR); | |||
2208 | setTargetDAGCombine(ISD::EXTRACT_SUBVECTOR); | |||
2209 | setTargetDAGCombine(ISD::BITCAST); | |||
2210 | setTargetDAGCombine(ISD::VSELECT); | |||
2211 | setTargetDAGCombine(ISD::SELECT); | |||
2212 | setTargetDAGCombine(ISD::SHL); | |||
2213 | setTargetDAGCombine(ISD::SRA); | |||
2214 | setTargetDAGCombine(ISD::SRL); | |||
2215 | setTargetDAGCombine(ISD::OR); | |||
2216 | setTargetDAGCombine(ISD::AND); | |||
2217 | setTargetDAGCombine(ISD::ADD); | |||
2218 | setTargetDAGCombine(ISD::FADD); | |||
2219 | setTargetDAGCombine(ISD::FSUB); | |||
2220 | setTargetDAGCombine(ISD::FNEG); | |||
2221 | setTargetDAGCombine(ISD::FMA); | |||
2222 | setTargetDAGCombine(ISD::STRICT_FMA); | |||
2223 | setTargetDAGCombine(ISD::FMINNUM); | |||
2224 | setTargetDAGCombine(ISD::FMAXNUM); | |||
2225 | setTargetDAGCombine(ISD::SUB); | |||
2226 | setTargetDAGCombine(ISD::LOAD); | |||
2227 | setTargetDAGCombine(ISD::MLOAD); | |||
2228 | setTargetDAGCombine(ISD::STORE); | |||
2229 | setTargetDAGCombine(ISD::MSTORE); | |||
2230 | setTargetDAGCombine(ISD::TRUNCATE); | |||
2231 | setTargetDAGCombine(ISD::ZERO_EXTEND); | |||
2232 | setTargetDAGCombine(ISD::ANY_EXTEND); | |||
2233 | setTargetDAGCombine(ISD::SIGN_EXTEND); | |||
2234 | setTargetDAGCombine(ISD::SIGN_EXTEND_INREG); | |||
2235 | setTargetDAGCombine(ISD::ANY_EXTEND_VECTOR_INREG); | |||
2236 | setTargetDAGCombine(ISD::SIGN_EXTEND_VECTOR_INREG); | |||
2237 | setTargetDAGCombine(ISD::ZERO_EXTEND_VECTOR_INREG); | |||
2238 | setTargetDAGCombine(ISD::SINT_TO_FP); | |||
2239 | setTargetDAGCombine(ISD::UINT_TO_FP); | |||
2240 | setTargetDAGCombine(ISD::STRICT_SINT_TO_FP); | |||
2241 | setTargetDAGCombine(ISD::STRICT_UINT_TO_FP); | |||
2242 | setTargetDAGCombine(ISD::SETCC); | |||
2243 | setTargetDAGCombine(ISD::MUL); | |||
2244 | setTargetDAGCombine(ISD::XOR); | |||
2245 | setTargetDAGCombine(ISD::MSCATTER); | |||
2246 | setTargetDAGCombine(ISD::MGATHER); | |||
2247 | setTargetDAGCombine(ISD::FP16_TO_FP); | |||
2248 | setTargetDAGCombine(ISD::FP_EXTEND); | |||
2249 | setTargetDAGCombine(ISD::STRICT_FP_EXTEND); | |||
2250 | setTargetDAGCombine(ISD::FP_ROUND); | |||
2251 | ||||
2252 | computeRegisterProperties(Subtarget.getRegisterInfo()); | |||
2253 | ||||
2254 | MaxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores | |||
2255 | MaxStoresPerMemsetOptSize = 8; | |||
2256 | MaxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores | |||
2257 | MaxStoresPerMemcpyOptSize = 4; | |||
2258 | MaxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores | |||
2259 | MaxStoresPerMemmoveOptSize = 4; | |||
2260 | ||||
2261 | // TODO: These control memcmp expansion in CGP and could be raised higher, but | |||
2262 | // that needs to benchmarked and balanced with the potential use of vector | |||
2263 | // load/store types (PR33329, PR33914). | |||
2264 | MaxLoadsPerMemcmp = 2; | |||
2265 | MaxLoadsPerMemcmpOptSize = 2; | |||
2266 | ||||
2267 | // Default loop alignment, which can be overridden by -align-loops. | |||
2268 | setPrefLoopAlignment(Align(16)); | |||
2269 | ||||
2270 | // An out-of-order CPU can speculatively execute past a predictable branch, | |||
2271 | // but a conditional move could be stalled by an expensive earlier operation. | |||
2272 | PredictableSelectIsExpensive = Subtarget.getSchedModel().isOutOfOrder(); | |||
2273 | EnableExtLdPromotion = true; | |||
2274 | setPrefFunctionAlignment(Align(16)); | |||
2275 | ||||
2276 | verifyIntrinsicTables(); | |||
2277 | ||||
2278 | // Default to having -disable-strictnode-mutation on | |||
2279 | IsStrictFPEnabled = true; | |||
2280 | } | |||
2281 | ||||
2282 | // This has so far only been implemented for 64-bit MachO. | |||
2283 | bool X86TargetLowering::useLoadStackGuardNode() const { | |||
2284 | return Subtarget.isTargetMachO() && Subtarget.is64Bit(); | |||
2285 | } | |||
2286 | ||||
2287 | bool X86TargetLowering::useStackGuardXorFP() const { | |||
2288 | // Currently only MSVC CRTs XOR the frame pointer into the stack guard value. | |||
2289 | return Subtarget.getTargetTriple().isOSMSVCRT() && !Subtarget.isTargetMachO(); | |||
2290 | } | |||
2291 | ||||
2292 | SDValue X86TargetLowering::emitStackGuardXorFP(SelectionDAG &DAG, SDValue Val, | |||
2293 | const SDLoc &DL) const { | |||
2294 | EVT PtrTy = getPointerTy(DAG.getDataLayout()); | |||
2295 | unsigned XorOp = Subtarget.is64Bit() ? X86::XOR64_FP : X86::XOR32_FP; | |||
2296 | MachineSDNode *Node = DAG.getMachineNode(XorOp, DL, PtrTy, Val); | |||
2297 | return SDValue(Node, 0); | |||
2298 | } | |||
2299 | ||||
2300 | TargetLoweringBase::LegalizeTypeAction | |||
2301 | X86TargetLowering::getPreferredVectorAction(MVT VT) const { | |||
2302 | if ((VT == MVT::v32i1 || VT == MVT::v64i1) && Subtarget.hasAVX512() && | |||
2303 | !Subtarget.hasBWI()) | |||
2304 | return TypeSplitVector; | |||
2305 | ||||
2306 | if (!VT.isScalableVector() && VT.getVectorNumElements() != 1 && | |||
2307 | VT.getVectorElementType() != MVT::i1) | |||
2308 | return TypeWidenVector; | |||
2309 | ||||
2310 | return TargetLoweringBase::getPreferredVectorAction(VT); | |||
2311 | } | |||
2312 | ||||
2313 | static std::pair<MVT, unsigned> | |||
2314 | handleMaskRegisterForCallingConv(unsigned NumElts, CallingConv::ID CC, | |||
2315 | const X86Subtarget &Subtarget) { | |||
2316 | // v2i1/v4i1/v8i1/v16i1 all pass in xmm registers unless the calling | |||
2317 | // convention is one that uses k registers. | |||
2318 | if (NumElts == 2) | |||
2319 | return {MVT::v2i64, 1}; | |||
2320 | if (NumElts == 4) | |||
2321 | return {MVT::v4i32, 1}; | |||
2322 | if (NumElts == 8 && CC != CallingConv::X86_RegCall && | |||
2323 | CC != CallingConv::Intel_OCL_BI) | |||
2324 | return {MVT::v8i16, 1}; | |||
2325 | if (NumElts == 16 && CC != CallingConv::X86_RegCall && | |||
2326 | CC != CallingConv::Intel_OCL_BI) | |||
2327 | return {MVT::v16i8, 1}; | |||
2328 | // v32i1 passes in ymm unless we have BWI and the calling convention is | |||
2329 | // regcall. | |||
2330 | if (NumElts == 32 && (!Subtarget.hasBWI() || CC != CallingConv::X86_RegCall)) | |||
2331 | return {MVT::v32i8, 1}; | |||
2332 | // Split v64i1 vectors if we don't have v64i8 available. | |||
2333 | if (NumElts == 64 && Subtarget.hasBWI() && CC != CallingConv::X86_RegCall) { | |||
2334 | if (Subtarget.useAVX512Regs()) | |||
2335 | return {MVT::v64i8, 1}; | |||
2336 | return {MVT::v32i8, 2}; | |||
2337 | } | |||
2338 | ||||
2339 | // Break wide or odd vXi1 vectors into scalars to match avx2 behavior. | |||
2340 | if (!isPowerOf2_32(NumElts) || (NumElts == 64 && !Subtarget.hasBWI()) || | |||
2341 | NumElts > 64) | |||
2342 | return {MVT::i8, NumElts}; | |||
2343 | ||||
2344 | return {MVT::INVALID_SIMPLE_VALUE_TYPE, 0}; | |||
2345 | } | |||
2346 | ||||
2347 | MVT X86TargetLowering::getRegisterTypeForCallingConv(LLVMContext &Context, | |||
2348 | CallingConv::ID CC, | |||
2349 | EVT VT) const { | |||
2350 | if (VT.isVector() && VT.getVectorElementType() == MVT::i1 && | |||
2351 | Subtarget.hasAVX512()) { | |||
2352 | unsigned NumElts = VT.getVectorNumElements(); | |||
2353 | ||||
2354 | MVT RegisterVT; | |||
2355 | unsigned NumRegisters; | |||
2356 | std::tie(RegisterVT, NumRegisters) = | |||
2357 | handleMaskRegisterForCallingConv(NumElts, CC, Subtarget); | |||
2358 | if (RegisterVT != MVT::INVALID_SIMPLE_VALUE_TYPE) | |||
2359 | return RegisterVT; | |||
2360 | } | |||
2361 | ||||
2362 | // v3f16 will be widen to v4f16. But we don't assign register class for v4f16. | |||
2363 | // So its default register type is f16. We override the type to v8f16 here. | |||
2364 | if (VT == MVT::v3f16 && Subtarget.hasFP16()) | |||
2365 | return MVT::v8f16; | |||
2366 | ||||
2367 | return TargetLowering::getRegisterTypeForCallingConv(Context, CC, VT); | |||
2368 | } | |||
2369 | ||||
2370 | unsigned X86TargetLowering::getNumRegistersForCallingConv(LLVMContext &Context, | |||
2371 | CallingConv::ID CC, | |||
2372 | EVT VT) const { | |||
2373 | if (VT.isVector() && VT.getVectorElementType() == MVT::i1 && | |||
2374 | Subtarget.hasAVX512()) { | |||
2375 | unsigned NumElts = VT.getVectorNumElements(); | |||
2376 | ||||
2377 | MVT RegisterVT; | |||
2378 | unsigned NumRegisters; | |||
2379 | std::tie(RegisterVT, NumRegisters) = | |||
2380 | handleMaskRegisterForCallingConv(NumElts, CC, Subtarget); | |||
2381 | if (RegisterVT != MVT::INVALID_SIMPLE_VALUE_TYPE) | |||
2382 | return NumRegisters; | |||
2383 | } | |||
2384 | ||||
2385 | // v3f16 will be widen to v4f16. But we don't assign register class for v4f16. | |||
2386 | // So its default register number is 3. We override the number to 1 here. | |||
2387 | if (VT == MVT::v3f16 && Subtarget.hasFP16()) | |||
2388 | return 1; | |||
2389 | ||||
2390 | return TargetLowering::getNumRegistersForCallingConv(Context, CC, VT); | |||
2391 | } | |||
2392 | ||||
2393 | unsigned X86TargetLowering::getVectorTypeBreakdownForCallingConv( | |||
2394 | LLVMContext &Context, CallingConv::ID CC, EVT VT, EVT &IntermediateVT, | |||
2395 | unsigned &NumIntermediates, MVT &RegisterVT) const { | |||
2396 | // Break wide or odd vXi1 vectors into scalars to match avx2 behavior. | |||
2397 | if (VT.isVector() && VT.getVectorElementType() == MVT::i1 && | |||
2398 | Subtarget.hasAVX512() && | |||
2399 | (!isPowerOf2_32(VT.getVectorNumElements()) || | |||
2400 | (VT.getVectorNumElements() == 64 && !Subtarget.hasBWI()) || | |||
2401 | VT.getVectorNumElements() > 64)) { | |||
2402 | RegisterVT = MVT::i8; | |||
2403 | IntermediateVT = MVT::i1; | |||
2404 | NumIntermediates = VT.getVectorNumElements(); | |||
2405 | return NumIntermediates; | |||
2406 | } | |||
2407 | ||||
2408 | // Split v64i1 vectors if we don't have v64i8 available. | |||
2409 | if (VT == MVT::v64i1 && Subtarget.hasBWI() && !Subtarget.useAVX512Regs() && | |||
2410 | CC != CallingConv::X86_RegCall) { | |||
2411 | RegisterVT = MVT::v32i8; | |||
2412 | IntermediateVT = MVT::v32i1; | |||
2413 | NumIntermediates = 2; | |||
2414 | return 2; | |||
2415 | } | |||
2416 | ||||
2417 | return TargetLowering::getVectorTypeBreakdownForCallingConv(Context, CC, VT, IntermediateVT, | |||
2418 | NumIntermediates, RegisterVT); | |||
2419 | } | |||
2420 | ||||
2421 | EVT X86TargetLowering::getSetCCResultType(const DataLayout &DL, | |||
2422 | LLVMContext& Context, | |||
2423 | EVT VT) const { | |||
2424 | if (!VT.isVector()) | |||
2425 | return MVT::i8; | |||
2426 | ||||
2427 | if (Subtarget.hasAVX512()) { | |||
2428 | // Figure out what this type will be legalized to. | |||
2429 | EVT LegalVT = VT; | |||
2430 | while (getTypeAction(Context, LegalVT) != TypeLegal) | |||
2431 | LegalVT = getTypeToTransformTo(Context, LegalVT); | |||
2432 | ||||
2433 | // If we got a 512-bit vector then we'll definitely have a vXi1 compare. | |||
2434 | if (LegalVT.getSimpleVT().is512BitVector()) | |||
2435 | return EVT::getVectorVT(Context, MVT::i1, VT.getVectorElementCount()); | |||
2436 | ||||
2437 | if (LegalVT.getSimpleVT().isVector() && Subtarget.hasVLX()) { | |||
2438 | // If we legalized to less than a 512-bit vector, then we will use a vXi1 | |||
2439 | // compare for vXi32/vXi64 for sure. If we have BWI we will also support | |||
2440 | // vXi16/vXi8. | |||
2441 | MVT EltVT = LegalVT.getSimpleVT().getVectorElementType(); | |||
2442 | if (Subtarget.hasBWI() || EltVT.getSizeInBits() >= 32) | |||
2443 | return EVT::getVectorVT(Context, MVT::i1, VT.getVectorElementCount()); | |||
2444 | } | |||
2445 | } | |||
2446 | ||||
2447 | return VT.changeVectorElementTypeToInteger(); | |||
2448 | } | |||
2449 | ||||
2450 | /// Helper for getByValTypeAlignment to determine | |||
2451 | /// the desired ByVal argument alignment. | |||
2452 | static void getMaxByValAlign(Type *Ty, Align &MaxAlign) { | |||
2453 | if (MaxAlign == 16) | |||
2454 | return; | |||
2455 | if (VectorType *VTy = dyn_cast<VectorType>(Ty)) { | |||
2456 | if (VTy->getPrimitiveSizeInBits().getFixedSize() == 128) | |||
2457 | MaxAlign = Align(16); | |||
2458 | } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) { | |||
2459 | Align EltAlign; | |||
2460 | getMaxByValAlign(ATy->getElementType(), EltAlign); | |||
2461 | if (EltAlign > MaxAlign) | |||
2462 | MaxAlign = EltAlign; | |||
2463 | } else if (StructType *STy = dyn_cast<StructType>(Ty)) { | |||
2464 | for (auto *EltTy : STy->elements()) { | |||
2465 | Align EltAlign; | |||
2466 | getMaxByValAlign(EltTy, EltAlign); | |||
2467 | if (EltAlign > MaxAlign) | |||
2468 | MaxAlign = EltAlign; | |||
2469 | if (MaxAlign == 16) | |||
2470 | break; | |||
2471 | } | |||
2472 | } | |||
2473 | } | |||
2474 | ||||
2475 | /// Return the desired alignment for ByVal aggregate | |||
2476 | /// function arguments in the caller parameter area. For X86, aggregates | |||
2477 | /// that contain SSE vectors are placed at 16-byte boundaries while the rest | |||
2478 | /// are at 4-byte boundaries. | |||
2479 | uint64_t X86TargetLowering::getByValTypeAlignment(Type *Ty, | |||
2480 | const DataLayout &DL) const { | |||
2481 | if (Subtarget.is64Bit()) { | |||
2482 | // Max of 8 and alignment of type. | |||
2483 | Align TyAlign = DL.getABITypeAlign(Ty); | |||
2484 | if (TyAlign > 8) | |||
2485 | return TyAlign.value(); | |||
2486 | return 8; | |||
2487 | } | |||
2488 | ||||
2489 | Align Alignment(4); | |||
2490 | if (Subtarget.hasSSE1()) | |||
2491 | getMaxByValAlign(Ty, Alignment); | |||
2492 | return Alignment.value(); | |||
2493 | } | |||
2494 | ||||
2495 | /// It returns EVT::Other if the type should be determined using generic | |||
2496 | /// target-independent logic. | |||
2497 | /// For vector ops we check that the overall size isn't larger than our | |||
2498 | /// preferred vector width. | |||
2499 | EVT X86TargetLowering::getOptimalMemOpType( | |||
2500 | const MemOp &Op, const AttributeList &FuncAttributes) const { | |||
2501 | if (!FuncAttributes.hasFnAttr(Attribute::NoImplicitFloat)) { | |||
2502 | if (Op.size() >= 16 && | |||
2503 | (!Subtarget.isUnalignedMem16Slow() || Op.isAligned(Align(16)))) { | |||
2504 | // FIXME: Check if unaligned 64-byte accesses are slow. | |||
2505 | if (Op.size() >= 64 && Subtarget.hasAVX512() && | |||
2506 | (Subtarget.getPreferVectorWidth() >= 512)) { | |||
2507 | return Subtarget.hasBWI() ? MVT::v64i8 : MVT::v16i32; | |||
2508 | } | |||
2509 | // FIXME: Check if unaligned 32-byte accesses are slow. | |||
2510 | if (Op.size() >= 32 && Subtarget.hasAVX() && | |||
2511 | (Subtarget.getPreferVectorWidth() >= 256)) { | |||
2512 | // Although this isn't a well-supported type for AVX1, we'll let | |||
2513 | // legalization and shuffle lowering produce the optimal codegen. If we | |||
2514 | // choose an optimal type with a vector element larger than a byte, | |||
2515 | // getMemsetStores() may create an intermediate splat (using an integer | |||
2516 | // multiply) before we splat as a vector. | |||
2517 | return MVT::v32i8; | |||
2518 | } | |||
2519 | if (Subtarget.hasSSE2() && (Subtarget.getPreferVectorWidth() >= 128)) | |||
2520 | return MVT::v16i8; | |||
2521 | // TODO: Can SSE1 handle a byte vector? | |||
2522 | // If we have SSE1 registers we should be able to use them. | |||
2523 | if (Subtarget.hasSSE1() && (Subtarget.is64Bit() || Subtarget.hasX87()) && | |||
2524 | (Subtarget.getPreferVectorWidth() >= 128)) | |||
2525 | return MVT::v4f32; | |||
2526 | } else if (((Op.isMemcpy() && !Op.isMemcpyStrSrc()) || Op.isZeroMemset()) && | |||
2527 | Op.size() >= 8 && !Subtarget.is64Bit() && Subtarget.hasSSE2()) { | |||
2528 | // Do not use f64 to lower memcpy if source is string constant. It's | |||
2529 | // better to use i32 to avoid the loads. | |||
2530 | // Also, do not use f64 to lower memset unless this is a memset of zeros. | |||
2531 | // The gymnastics of splatting a byte value into an XMM register and then | |||
2532 | // only using 8-byte stores (because this is a CPU with slow unaligned | |||
2533 | // 16-byte accesses) makes that a loser. | |||
2534 | return MVT::f64; | |||
2535 | } | |||
2536 | } | |||
2537 | // This is a compromise. If we reach here, unaligned accesses may be slow on | |||
2538 | // this target. However, creating smaller, aligned accesses could be even | |||
2539 | // slower and would certainly be a lot more code. | |||
2540 | if (Subtarget.is64Bit() && Op.size() >= 8) | |||
2541 | return MVT::i64; | |||
2542 | return MVT::i32; | |||
2543 | } | |||
2544 | ||||
2545 | bool X86TargetLowering::isSafeMemOpType(MVT VT) const { | |||
2546 | if (VT == MVT::f32) | |||
2547 | return X86ScalarSSEf32; | |||
2548 | if (VT == MVT::f64) | |||
2549 | return X86ScalarSSEf64; | |||
2550 | return true; | |||
2551 | } | |||
2552 | ||||
2553 | bool X86TargetLowering::allowsMisalignedMemoryAccesses( | |||
2554 | EVT VT, unsigned, Align Alignment, MachineMemOperand::Flags Flags, | |||
2555 | bool *Fast) const { | |||
2556 | if (Fast) { | |||
2557 | switch (VT.getSizeInBits()) { | |||
2558 | default: | |||
2559 | // 8-byte and under are always assumed to be fast. | |||
2560 | *Fast = true; | |||
2561 | break; | |||
2562 | case 128: | |||
2563 | *Fast = !Subtarget.isUnalignedMem16Slow(); | |||
2564 | break; | |||
2565 | case 256: | |||
2566 | *Fast = !Subtarget.isUnalignedMem32Slow(); | |||
2567 | break; | |||
2568 | // TODO: What about AVX-512 (512-bit) accesses? | |||
2569 | } | |||
2570 | } | |||
2571 | // NonTemporal vector memory ops must be aligned. | |||
2572 | if (!!(Flags & MachineMemOperand::MONonTemporal) && VT.isVector()) { | |||
2573 | // NT loads can only be vector aligned, so if its less aligned than the | |||
2574 | // minimum vector size (which we can split the vector down to), we might as | |||
2575 | // well use a regular unaligned vector load. | |||
2576 | // We don't have any NT loads pre-SSE41. | |||
2577 | if (!!(Flags & MachineMemOperand::MOLoad)) | |||
2578 | return (Alignment < 16 || !Subtarget.hasSSE41()); | |||
2579 | return false; | |||
2580 | } | |||
2581 | // Misaligned accesses of any size are always allowed. | |||
2582 | return true; | |||
2583 | } | |||
2584 | ||||
2585 | /// Return the entry encoding for a jump table in the | |||
2586 | /// current function. The returned value is a member of the | |||
2587 | /// MachineJumpTableInfo::JTEntryKind enum. | |||
2588 | unsigned X86TargetLowering::getJumpTableEncoding() const { | |||
2589 | // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF | |||
2590 | // symbol. | |||
2591 | if (isPositionIndependent() && Subtarget.isPICStyleGOT()) | |||
2592 | return MachineJumpTableInfo::EK_Custom32; | |||
2593 | ||||
2594 | // Otherwise, use the normal jump table encoding heuristics. | |||
2595 | return TargetLowering::getJumpTableEncoding(); | |||
2596 | } | |||
2597 | ||||
2598 | bool X86TargetLowering::useSoftFloat() const { | |||
2599 | return Subtarget.useSoftFloat(); | |||
2600 | } | |||
2601 | ||||
2602 | void X86TargetLowering::markLibCallAttributes(MachineFunction *MF, unsigned CC, | |||
2603 | ArgListTy &Args) const { | |||
2604 | ||||
2605 | // Only relabel X86-32 for C / Stdcall CCs. | |||
2606 | if (Subtarget.is64Bit()) | |||
2607 | return; | |||
2608 | if (CC != CallingConv::C && CC != CallingConv::X86_StdCall) | |||
2609 | return; | |||
2610 | unsigned ParamRegs = 0; | |||
2611 | if (auto *M = MF->getFunction().getParent()) | |||
2612 | ParamRegs = M->getNumberRegisterParameters(); | |||
2613 | ||||
2614 | // Mark the first N int arguments as having reg | |||
2615 | for (auto &Arg : Args) { | |||
2616 | Type *T = Arg.Ty; | |||
2617 | if (T->isIntOrPtrTy()) | |||
2618 | if (MF->getDataLayout().getTypeAllocSize(T) <= 8) { | |||
2619 | unsigned numRegs = 1; | |||
2620 | if (MF->getDataLayout().getTypeAllocSize(T) > 4) | |||
2621 | numRegs = 2; | |||
2622 | if (ParamRegs < numRegs) | |||
2623 | return; | |||
2624 | ParamRegs -= numRegs; | |||
2625 | Arg.IsInReg = true; | |||
2626 | } | |||
2627 | } | |||
2628 | } | |||
2629 | ||||
2630 | const MCExpr * | |||
2631 | X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI, | |||
2632 | const MachineBasicBlock *MBB, | |||
2633 | unsigned uid,MCContext &Ctx) const{ | |||
2634 | assert(isPositionIndependent() && Subtarget.isPICStyleGOT())(static_cast <bool> (isPositionIndependent() && Subtarget.isPICStyleGOT()) ? void (0) : __assert_fail ("isPositionIndependent() && Subtarget.isPICStyleGOT()" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 2634, __extension__ __PRETTY_FUNCTION__)); | |||
2635 | // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF | |||
2636 | // entries. | |||
2637 | return MCSymbolRefExpr::create(MBB->getSymbol(), | |||
2638 | MCSymbolRefExpr::VK_GOTOFF, Ctx); | |||
2639 | } | |||
2640 | ||||
2641 | /// Returns relocation base for the given PIC jumptable. | |||
2642 | SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table, | |||
2643 | SelectionDAG &DAG) const { | |||
2644 | if (!Subtarget.is64Bit()) | |||
2645 | // This doesn't have SDLoc associated with it, but is not really the | |||
2646 | // same as a Register. | |||
2647 | return DAG.getNode(X86ISD::GlobalBaseReg, SDLoc(), | |||
2648 | getPointerTy(DAG.getDataLayout())); | |||
2649 | return Table; | |||
2650 | } | |||
2651 | ||||
2652 | /// This returns the relocation base for the given PIC jumptable, | |||
2653 | /// the same as getPICJumpTableRelocBase, but as an MCExpr. | |||
2654 | const MCExpr *X86TargetLowering:: | |||
2655 | getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI, | |||
2656 | MCContext &Ctx) const { | |||
2657 | // X86-64 uses RIP relative addressing based on the jump table label. | |||
2658 | if (Subtarget.isPICStyleRIPRel()) | |||
2659 | return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx); | |||
2660 | ||||
2661 | // Otherwise, the reference is relative to the PIC base. | |||
2662 | return MCSymbolRefExpr::create(MF->getPICBaseSymbol(), Ctx); | |||
2663 | } | |||
2664 | ||||
2665 | std::pair<const TargetRegisterClass *, uint8_t> | |||
2666 | X86TargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI, | |||
2667 | MVT VT) const { | |||
2668 | const TargetRegisterClass *RRC = nullptr; | |||
2669 | uint8_t Cost = 1; | |||
2670 | switch (VT.SimpleTy) { | |||
2671 | default: | |||
2672 | return TargetLowering::findRepresentativeClass(TRI, VT); | |||
2673 | case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64: | |||
2674 | RRC = Subtarget.is64Bit() ? &X86::GR64RegClass : &X86::GR32RegClass; | |||
2675 | break; | |||
2676 | case MVT::x86mmx: | |||
2677 | RRC = &X86::VR64RegClass; | |||
2678 | break; | |||
2679 | case MVT::f32: case MVT::f64: | |||
2680 | case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64: | |||
2681 | case MVT::v4f32: case MVT::v2f64: | |||
2682 | case MVT::v32i8: case MVT::v16i16: case MVT::v8i32: case MVT::v4i64: | |||
2683 | case MVT::v8f32: case MVT::v4f64: | |||
2684 | case MVT::v64i8: case MVT::v32i16: case MVT::v16i32: case MVT::v8i64: | |||
2685 | case MVT::v16f32: case MVT::v8f64: | |||
2686 | RRC = &X86::VR128XRegClass; | |||
2687 | break; | |||
2688 | } | |||
2689 | return std::make_pair(RRC, Cost); | |||
2690 | } | |||
2691 | ||||
2692 | unsigned X86TargetLowering::getAddressSpace() const { | |||
2693 | if (Subtarget.is64Bit()) | |||
2694 | return (getTargetMachine().getCodeModel() == CodeModel::Kernel) ? 256 : 257; | |||
2695 | return 256; | |||
2696 | } | |||
2697 | ||||
2698 | static bool hasStackGuardSlotTLS(const Triple &TargetTriple) { | |||
2699 | return TargetTriple.isOSGlibc() || TargetTriple.isOSFuchsia() || | |||
2700 | (TargetTriple.isAndroid() && !TargetTriple.isAndroidVersionLT(17)); | |||
2701 | } | |||
2702 | ||||
2703 | static Constant* SegmentOffset(IRBuilderBase &IRB, | |||
2704 | int Offset, unsigned AddressSpace) { | |||
2705 | return ConstantExpr::getIntToPtr( | |||
2706 | ConstantInt::get(Type::getInt32Ty(IRB.getContext()), Offset), | |||
2707 | Type::getInt8PtrTy(IRB.getContext())->getPointerTo(AddressSpace)); | |||
2708 | } | |||
2709 | ||||
2710 | Value *X86TargetLowering::getIRStackGuard(IRBuilderBase &IRB) const { | |||
2711 | // glibc, bionic, and Fuchsia have a special slot for the stack guard in | |||
2712 | // tcbhead_t; use it instead of the usual global variable (see | |||
2713 | // sysdeps/{i386,x86_64}/nptl/tls.h) | |||
2714 | if (hasStackGuardSlotTLS(Subtarget.getTargetTriple())) { | |||
2715 | if (Subtarget.isTargetFuchsia()) { | |||
2716 | // <zircon/tls.h> defines ZX_TLS_STACK_GUARD_OFFSET with this value. | |||
2717 | return SegmentOffset(IRB, 0x10, getAddressSpace()); | |||
2718 | } else { | |||
2719 | unsigned AddressSpace = getAddressSpace(); | |||
2720 | Module *M = IRB.GetInsertBlock()->getParent()->getParent(); | |||
2721 | // Specially, some users may customize the base reg and offset. | |||
2722 | int Offset = M->getStackProtectorGuardOffset(); | |||
2723 | // If we don't set -stack-protector-guard-offset value: | |||
2724 | // %fs:0x28, unless we're using a Kernel code model, in which case | |||
2725 | // it's %gs:0x28. gs:0x14 on i386. | |||
2726 | if (Offset == INT_MAX2147483647) | |||
2727 | Offset = (Subtarget.is64Bit()) ? 0x28 : 0x14; | |||
2728 | ||||
2729 | StringRef GuardReg = M->getStackProtectorGuardReg(); | |||
2730 | if (GuardReg == "fs") | |||
2731 | AddressSpace = X86AS::FS; | |||
2732 | else if (GuardReg == "gs") | |||
2733 | AddressSpace = X86AS::GS; | |||
2734 | return SegmentOffset(IRB, Offset, AddressSpace); | |||
2735 | } | |||
2736 | } | |||
2737 | return TargetLowering::getIRStackGuard(IRB); | |||
2738 | } | |||
2739 | ||||
2740 | void X86TargetLowering::insertSSPDeclarations(Module &M) const { | |||
2741 | // MSVC CRT provides functionalities for stack protection. | |||
2742 | if (Subtarget.getTargetTriple().isWindowsMSVCEnvironment() || | |||
2743 | Subtarget.getTargetTriple().isWindowsItaniumEnvironment()) { | |||
2744 | // MSVC CRT has a global variable holding security cookie. | |||
2745 | M.getOrInsertGlobal("__security_cookie", | |||
2746 | Type::getInt8PtrTy(M.getContext())); | |||
2747 | ||||
2748 | // MSVC CRT has a function to validate security cookie. | |||
2749 | FunctionCallee SecurityCheckCookie = M.getOrInsertFunction( | |||
2750 | "__security_check_cookie", Type::getVoidTy(M.getContext()), | |||
2751 | Type::getInt8PtrTy(M.getContext())); | |||
2752 | if (Function *F = dyn_cast<Function>(SecurityCheckCookie.getCallee())) { | |||
2753 | F->setCallingConv(CallingConv::X86_FastCall); | |||
2754 | F->addParamAttr(0, Attribute::AttrKind::InReg); | |||
2755 | } | |||
2756 | return; | |||
2757 | } | |||
2758 | ||||
2759 | StringRef GuardMode = M.getStackProtectorGuard(); | |||
2760 | ||||
2761 | // glibc, bionic, and Fuchsia have a special slot for the stack guard. | |||
2762 | if ((GuardMode == "tls" || GuardMode.empty()) && | |||
2763 | hasStackGuardSlotTLS(Subtarget.getTargetTriple())) | |||
2764 | return; | |||
2765 | TargetLowering::insertSSPDeclarations(M); | |||
2766 | } | |||
2767 | ||||
2768 | Value *X86TargetLowering::getSDagStackGuard(const Module &M) const { | |||
2769 | // MSVC CRT has a global variable holding security cookie. | |||
2770 | if (Subtarget.getTargetTriple().isWindowsMSVCEnvironment() || | |||
2771 | Subtarget.getTargetTriple().isWindowsItaniumEnvironment()) { | |||
2772 | return M.getGlobalVariable("__security_cookie"); | |||
2773 | } | |||
2774 | return TargetLowering::getSDagStackGuard(M); | |||
2775 | } | |||
2776 | ||||
2777 | Function *X86TargetLowering::getSSPStackGuardCheck(const Module &M) const { | |||
2778 | // MSVC CRT has a function to validate security cookie. | |||
2779 | if (Subtarget.getTargetTriple().isWindowsMSVCEnvironment() || | |||
2780 | Subtarget.getTargetTriple().isWindowsItaniumEnvironment()) { | |||
2781 | return M.getFunction("__security_check_cookie"); | |||
2782 | } | |||
2783 | return TargetLowering::getSSPStackGuardCheck(M); | |||
2784 | } | |||
2785 | ||||
2786 | Value * | |||
2787 | X86TargetLowering::getSafeStackPointerLocation(IRBuilderBase &IRB) const { | |||
2788 | if (Subtarget.getTargetTriple().isOSContiki()) | |||
2789 | return getDefaultSafeStackPointerLocation(IRB, false); | |||
2790 | ||||
2791 | // Android provides a fixed TLS slot for the SafeStack pointer. See the | |||
2792 | // definition of TLS_SLOT_SAFESTACK in | |||
2793 | // https://android.googlesource.com/platform/bionic/+/master/libc/private/bionic_tls.h | |||
2794 | if (Subtarget.isTargetAndroid()) { | |||
2795 | // %fs:0x48, unless we're using a Kernel code model, in which case it's %gs: | |||
2796 | // %gs:0x24 on i386 | |||
2797 | int Offset = (Subtarget.is64Bit()) ? 0x48 : 0x24; | |||
2798 | return SegmentOffset(IRB, Offset, getAddressSpace()); | |||
2799 | } | |||
2800 | ||||
2801 | // Fuchsia is similar. | |||
2802 | if (Subtarget.isTargetFuchsia()) { | |||
2803 | // <zircon/tls.h> defines ZX_TLS_UNSAFE_SP_OFFSET with this value. | |||
2804 | return SegmentOffset(IRB, 0x18, getAddressSpace()); | |||
2805 | } | |||
2806 | ||||
2807 | return TargetLowering::getSafeStackPointerLocation(IRB); | |||
2808 | } | |||
2809 | ||||
2810 | //===----------------------------------------------------------------------===// | |||
2811 | // Return Value Calling Convention Implementation | |||
2812 | //===----------------------------------------------------------------------===// | |||
2813 | ||||
2814 | bool X86TargetLowering::CanLowerReturn( | |||
2815 | CallingConv::ID CallConv, MachineFunction &MF, bool isVarArg, | |||
2816 | const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context) const { | |||
2817 | SmallVector<CCValAssign, 16> RVLocs; | |||
2818 | CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context); | |||
2819 | return CCInfo.CheckReturn(Outs, RetCC_X86); | |||
2820 | } | |||
2821 | ||||
2822 | const MCPhysReg *X86TargetLowering::getScratchRegisters(CallingConv::ID) const { | |||
2823 | static const MCPhysReg ScratchRegs[] = { X86::R11, 0 }; | |||
2824 | return ScratchRegs; | |||
2825 | } | |||
2826 | ||||
2827 | /// Lowers masks values (v*i1) to the local register values | |||
2828 | /// \returns DAG node after lowering to register type | |||
2829 | static SDValue lowerMasksToReg(const SDValue &ValArg, const EVT &ValLoc, | |||
2830 | const SDLoc &Dl, SelectionDAG &DAG) { | |||
2831 | EVT ValVT = ValArg.getValueType(); | |||
2832 | ||||
2833 | if (ValVT == MVT::v1i1) | |||
2834 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, Dl, ValLoc, ValArg, | |||
2835 | DAG.getIntPtrConstant(0, Dl)); | |||
2836 | ||||
2837 | if ((ValVT == MVT::v8i1 && (ValLoc == MVT::i8 || ValLoc == MVT::i32)) || | |||
2838 | (ValVT == MVT::v16i1 && (ValLoc == MVT::i16 || ValLoc == MVT::i32))) { | |||
2839 | // Two stage lowering might be required | |||
2840 | // bitcast: v8i1 -> i8 / v16i1 -> i16 | |||
2841 | // anyextend: i8 -> i32 / i16 -> i32 | |||
2842 | EVT TempValLoc = ValVT == MVT::v8i1 ? MVT::i8 : MVT::i16; | |||
2843 | SDValue ValToCopy = DAG.getBitcast(TempValLoc, ValArg); | |||
2844 | if (ValLoc == MVT::i32) | |||
2845 | ValToCopy = DAG.getNode(ISD::ANY_EXTEND, Dl, ValLoc, ValToCopy); | |||
2846 | return ValToCopy; | |||
2847 | } | |||
2848 | ||||
2849 | if ((ValVT == MVT::v32i1 && ValLoc == MVT::i32) || | |||
2850 | (ValVT == MVT::v64i1 && ValLoc == MVT::i64)) { | |||
2851 | // One stage lowering is required | |||
2852 | // bitcast: v32i1 -> i32 / v64i1 -> i64 | |||
2853 | return DAG.getBitcast(ValLoc, ValArg); | |||
2854 | } | |||
2855 | ||||
2856 | return DAG.getNode(ISD::ANY_EXTEND, Dl, ValLoc, ValArg); | |||
2857 | } | |||
2858 | ||||
2859 | /// Breaks v64i1 value into two registers and adds the new node to the DAG | |||
2860 | static void Passv64i1ArgInRegs( | |||
2861 | const SDLoc &Dl, SelectionDAG &DAG, SDValue &Arg, | |||
2862 | SmallVectorImpl<std::pair<Register, SDValue>> &RegsToPass, CCValAssign &VA, | |||
2863 | CCValAssign &NextVA, const X86Subtarget &Subtarget) { | |||
2864 | assert(Subtarget.hasBWI() && "Expected AVX512BW target!")(static_cast <bool> (Subtarget.hasBWI() && "Expected AVX512BW target!" ) ? void (0) : __assert_fail ("Subtarget.hasBWI() && \"Expected AVX512BW target!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 2864, __extension__ __PRETTY_FUNCTION__)); | |||
2865 | assert(Subtarget.is32Bit() && "Expecting 32 bit target")(static_cast <bool> (Subtarget.is32Bit() && "Expecting 32 bit target" ) ? void (0) : __assert_fail ("Subtarget.is32Bit() && \"Expecting 32 bit target\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 2865, __extension__ __PRETTY_FUNCTION__)); | |||
2866 | assert(Arg.getValueType() == MVT::i64 && "Expecting 64 bit value")(static_cast <bool> (Arg.getValueType() == MVT::i64 && "Expecting 64 bit value") ? void (0) : __assert_fail ("Arg.getValueType() == MVT::i64 && \"Expecting 64 bit value\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 2866, __extension__ __PRETTY_FUNCTION__)); | |||
2867 | assert(VA.isRegLoc() && NextVA.isRegLoc() &&(static_cast <bool> (VA.isRegLoc() && NextVA.isRegLoc () && "The value should reside in two registers") ? void (0) : __assert_fail ("VA.isRegLoc() && NextVA.isRegLoc() && \"The value should reside in two registers\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 2868, __extension__ __PRETTY_FUNCTION__)) | |||
2868 | "The value should reside in two registers")(static_cast <bool> (VA.isRegLoc() && NextVA.isRegLoc () && "The value should reside in two registers") ? void (0) : __assert_fail ("VA.isRegLoc() && NextVA.isRegLoc() && \"The value should reside in two registers\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 2868, __extension__ __PRETTY_FUNCTION__)); | |||
2869 | ||||
2870 | // Before splitting the value we cast it to i64 | |||
2871 | Arg = DAG.getBitcast(MVT::i64, Arg); | |||
2872 | ||||
2873 | // Splitting the value into two i32 types | |||
2874 | SDValue Lo, Hi; | |||
2875 | Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, Dl, MVT::i32, Arg, | |||
2876 | DAG.getConstant(0, Dl, MVT::i32)); | |||
2877 | Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, Dl, MVT::i32, Arg, | |||
2878 | DAG.getConstant(1, Dl, MVT::i32)); | |||
2879 | ||||
2880 | // Attach the two i32 types into corresponding registers | |||
2881 | RegsToPass.push_back(std::make_pair(VA.getLocReg(), Lo)); | |||
2882 | RegsToPass.push_back(std::make_pair(NextVA.getLocReg(), Hi)); | |||
2883 | } | |||
2884 | ||||
2885 | SDValue | |||
2886 | X86TargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv, | |||
2887 | bool isVarArg, | |||
2888 | const SmallVectorImpl<ISD::OutputArg> &Outs, | |||
2889 | const SmallVectorImpl<SDValue> &OutVals, | |||
2890 | const SDLoc &dl, SelectionDAG &DAG) const { | |||
2891 | MachineFunction &MF = DAG.getMachineFunction(); | |||
2892 | X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>(); | |||
2893 | ||||
2894 | // In some cases we need to disable registers from the default CSR list. | |||
2895 | // For example, when they are used for argument passing. | |||
2896 | bool ShouldDisableCalleeSavedRegister = | |||
2897 | CallConv == CallingConv::X86_RegCall || | |||
2898 | MF.getFunction().hasFnAttribute("no_caller_saved_registers"); | |||
2899 | ||||
2900 | if (CallConv == CallingConv::X86_INTR && !Outs.empty()) | |||
2901 | report_fatal_error("X86 interrupts may not return any value"); | |||
2902 | ||||
2903 | SmallVector<CCValAssign, 16> RVLocs; | |||
2904 | CCState CCInfo(CallConv, isVarArg, MF, RVLocs, *DAG.getContext()); | |||
2905 | CCInfo.AnalyzeReturn(Outs, RetCC_X86); | |||
2906 | ||||
2907 | SmallVector<std::pair<Register, SDValue>, 4> RetVals; | |||
2908 | for (unsigned I = 0, OutsIndex = 0, E = RVLocs.size(); I != E; | |||
2909 | ++I, ++OutsIndex) { | |||
2910 | CCValAssign &VA = RVLocs[I]; | |||
2911 | assert(VA.isRegLoc() && "Can only return in registers!")(static_cast <bool> (VA.isRegLoc() && "Can only return in registers!" ) ? void (0) : __assert_fail ("VA.isRegLoc() && \"Can only return in registers!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 2911, __extension__ __PRETTY_FUNCTION__)); | |||
2912 | ||||
2913 | // Add the register to the CalleeSaveDisableRegs list. | |||
2914 | if (ShouldDisableCalleeSavedRegister) | |||
2915 | MF.getRegInfo().disableCalleeSavedRegister(VA.getLocReg()); | |||
2916 | ||||
2917 | SDValue ValToCopy = OutVals[OutsIndex]; | |||
2918 | EVT ValVT = ValToCopy.getValueType(); | |||
2919 | ||||
2920 | // Promote values to the appropriate types. | |||
2921 | if (VA.getLocInfo() == CCValAssign::SExt) | |||
2922 | ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy); | |||
2923 | else if (VA.getLocInfo() == CCValAssign::ZExt) | |||
2924 | ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy); | |||
2925 | else if (VA.getLocInfo() == CCValAssign::AExt) { | |||
2926 | if (ValVT.isVector() && ValVT.getVectorElementType() == MVT::i1) | |||
2927 | ValToCopy = lowerMasksToReg(ValToCopy, VA.getLocVT(), dl, DAG); | |||
2928 | else | |||
2929 | ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy); | |||
2930 | } | |||
2931 | else if (VA.getLocInfo() == CCValAssign::BCvt) | |||
2932 | ValToCopy = DAG.getBitcast(VA.getLocVT(), ValToCopy); | |||
2933 | ||||
2934 | assert(VA.getLocInfo() != CCValAssign::FPExt &&(static_cast <bool> (VA.getLocInfo() != CCValAssign::FPExt && "Unexpected FP-extend for return value.") ? void ( 0) : __assert_fail ("VA.getLocInfo() != CCValAssign::FPExt && \"Unexpected FP-extend for return value.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 2935, __extension__ __PRETTY_FUNCTION__)) | |||
2935 | "Unexpected FP-extend for return value.")(static_cast <bool> (VA.getLocInfo() != CCValAssign::FPExt && "Unexpected FP-extend for return value.") ? void ( 0) : __assert_fail ("VA.getLocInfo() != CCValAssign::FPExt && \"Unexpected FP-extend for return value.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 2935, __extension__ __PRETTY_FUNCTION__)); | |||
2936 | ||||
2937 | // Report an error if we have attempted to return a value via an XMM | |||
2938 | // register and SSE was disabled. | |||
2939 | if (!Subtarget.hasSSE1() && X86::FR32XRegClass.contains(VA.getLocReg())) { | |||
2940 | errorUnsupported(DAG, dl, "SSE register return with SSE disabled"); | |||
2941 | VA.convertToReg(X86::FP0); // Set reg to FP0, avoid hitting asserts. | |||
2942 | } else if (!Subtarget.hasSSE2() && | |||
2943 | X86::FR64XRegClass.contains(VA.getLocReg()) && | |||
2944 | ValVT == MVT::f64) { | |||
2945 | // When returning a double via an XMM register, report an error if SSE2 is | |||
2946 | // not enabled. | |||
2947 | errorUnsupported(DAG, dl, "SSE2 register return with SSE2 disabled"); | |||
2948 | VA.convertToReg(X86::FP0); // Set reg to FP0, avoid hitting asserts. | |||
2949 | } | |||
2950 | ||||
2951 | // Returns in ST0/ST1 are handled specially: these are pushed as operands to | |||
2952 | // the RET instruction and handled by the FP Stackifier. | |||
2953 | if (VA.getLocReg() == X86::FP0 || | |||
2954 | VA.getLocReg() == X86::FP1) { | |||
2955 | // If this is a copy from an xmm register to ST(0), use an FPExtend to | |||
2956 | // change the value to the FP stack register class. | |||
2957 | if (isScalarFPTypeInSSEReg(VA.getValVT())) | |||
2958 | ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy); | |||
2959 | RetVals.push_back(std::make_pair(VA.getLocReg(), ValToCopy)); | |||
2960 | // Don't emit a copytoreg. | |||
2961 | continue; | |||
2962 | } | |||
2963 | ||||
2964 | // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64 | |||
2965 | // which is returned in RAX / RDX. | |||
2966 | if (Subtarget.is64Bit()) { | |||
2967 | if (ValVT == MVT::x86mmx) { | |||
2968 | if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) { | |||
2969 | ValToCopy = DAG.getBitcast(MVT::i64, ValToCopy); | |||
2970 | ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, | |||
2971 | ValToCopy); | |||
2972 | // If we don't have SSE2 available, convert to v4f32 so the generated | |||
2973 | // register is legal. | |||
2974 | if (!Subtarget.hasSSE2()) | |||
2975 | ValToCopy = DAG.getBitcast(MVT::v4f32, ValToCopy); | |||
2976 | } | |||
2977 | } | |||
2978 | } | |||
2979 | ||||
2980 | if (VA.needsCustom()) { | |||
2981 | assert(VA.getValVT() == MVT::v64i1 &&(static_cast <bool> (VA.getValVT() == MVT::v64i1 && "Currently the only custom case is when we split v64i1 to 2 regs" ) ? void (0) : __assert_fail ("VA.getValVT() == MVT::v64i1 && \"Currently the only custom case is when we split v64i1 to 2 regs\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 2982, __extension__ __PRETTY_FUNCTION__)) | |||
2982 | "Currently the only custom case is when we split v64i1 to 2 regs")(static_cast <bool> (VA.getValVT() == MVT::v64i1 && "Currently the only custom case is when we split v64i1 to 2 regs" ) ? void (0) : __assert_fail ("VA.getValVT() == MVT::v64i1 && \"Currently the only custom case is when we split v64i1 to 2 regs\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 2982, __extension__ __PRETTY_FUNCTION__)); | |||
2983 | ||||
2984 | Passv64i1ArgInRegs(dl, DAG, ValToCopy, RetVals, VA, RVLocs[++I], | |||
2985 | Subtarget); | |||
2986 | ||||
2987 | // Add the second register to the CalleeSaveDisableRegs list. | |||
2988 | if (ShouldDisableCalleeSavedRegister) | |||
2989 | MF.getRegInfo().disableCalleeSavedRegister(RVLocs[I].getLocReg()); | |||
2990 | } else { | |||
2991 | RetVals.push_back(std::make_pair(VA.getLocReg(), ValToCopy)); | |||
2992 | } | |||
2993 | } | |||
2994 | ||||
2995 | SDValue Flag; | |||
2996 | SmallVector<SDValue, 6> RetOps; | |||
2997 | RetOps.push_back(Chain); // Operand #0 = Chain (updated below) | |||
2998 | // Operand #1 = Bytes To Pop | |||
2999 | RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(), dl, | |||
3000 | MVT::i32)); | |||
3001 | ||||
3002 | // Copy the result values into the output registers. | |||
3003 | for (auto &RetVal : RetVals) { | |||
3004 | if (RetVal.first == X86::FP0 || RetVal.first == X86::FP1) { | |||
3005 | RetOps.push_back(RetVal.second); | |||
3006 | continue; // Don't emit a copytoreg. | |||
3007 | } | |||
3008 | ||||
3009 | Chain = DAG.getCopyToReg(Chain, dl, RetVal.first, RetVal.second, Flag); | |||
3010 | Flag = Chain.getValue(1); | |||
3011 | RetOps.push_back( | |||
3012 | DAG.getRegister(RetVal.first, RetVal.second.getValueType())); | |||
3013 | } | |||
3014 | ||||
3015 | // Swift calling convention does not require we copy the sret argument | |||
3016 | // into %rax/%eax for the return, and SRetReturnReg is not set for Swift. | |||
3017 | ||||
3018 | // All x86 ABIs require that for returning structs by value we copy | |||
3019 | // the sret argument into %rax/%eax (depending on ABI) for the return. | |||
3020 | // We saved the argument into a virtual register in the entry block, | |||
3021 | // so now we copy the value out and into %rax/%eax. | |||
3022 | // | |||
3023 | // Checking Function.hasStructRetAttr() here is insufficient because the IR | |||
3024 | // may not have an explicit sret argument. If FuncInfo.CanLowerReturn is | |||
3025 | // false, then an sret argument may be implicitly inserted in the SelDAG. In | |||
3026 | // either case FuncInfo->setSRetReturnReg() will have been called. | |||
3027 | if (Register SRetReg = FuncInfo->getSRetReturnReg()) { | |||
3028 | // When we have both sret and another return value, we should use the | |||
3029 | // original Chain stored in RetOps[0], instead of the current Chain updated | |||
3030 | // in the above loop. If we only have sret, RetOps[0] equals to Chain. | |||
3031 | ||||
3032 | // For the case of sret and another return value, we have | |||
3033 | // Chain_0 at the function entry | |||
3034 | // Chain_1 = getCopyToReg(Chain_0) in the above loop | |||
3035 | // If we use Chain_1 in getCopyFromReg, we will have | |||
3036 | // Val = getCopyFromReg(Chain_1) | |||
3037 | // Chain_2 = getCopyToReg(Chain_1, Val) from below | |||
3038 | ||||
3039 | // getCopyToReg(Chain_0) will be glued together with | |||
3040 | // getCopyToReg(Chain_1, Val) into Unit A, getCopyFromReg(Chain_1) will be | |||
3041 | // in Unit B, and we will have cyclic dependency between Unit A and Unit B: | |||
3042 | // Data dependency from Unit B to Unit A due to usage of Val in | |||
3043 | // getCopyToReg(Chain_1, Val) | |||
3044 | // Chain dependency from Unit A to Unit B | |||
3045 | ||||
3046 | // So here, we use RetOps[0] (i.e Chain_0) for getCopyFromReg. | |||
3047 | SDValue Val = DAG.getCopyFromReg(RetOps[0], dl, SRetReg, | |||
3048 | getPointerTy(MF.getDataLayout())); | |||
3049 | ||||
3050 | Register RetValReg | |||
3051 | = (Subtarget.is64Bit() && !Subtarget.isTarget64BitILP32()) ? | |||
3052 | X86::RAX : X86::EAX; | |||
3053 | Chain = DAG.getCopyToReg(Chain, dl, RetValReg, Val, Flag); | |||
3054 | Flag = Chain.getValue(1); | |||
3055 | ||||
3056 | // RAX/EAX now acts like a return value. | |||
3057 | RetOps.push_back( | |||
3058 | DAG.getRegister(RetValReg, getPointerTy(DAG.getDataLayout()))); | |||
3059 | ||||
3060 | // Add the returned register to the CalleeSaveDisableRegs list. | |||
3061 | if (ShouldDisableCalleeSavedRegister) | |||
3062 | MF.getRegInfo().disableCalleeSavedRegister(RetValReg); | |||
3063 | } | |||
3064 | ||||
3065 | const X86RegisterInfo *TRI = Subtarget.getRegisterInfo(); | |||
3066 | const MCPhysReg *I = | |||
3067 | TRI->getCalleeSavedRegsViaCopy(&DAG.getMachineFunction()); | |||
3068 | if (I) { | |||
3069 | for (; *I; ++I) { | |||
3070 | if (X86::GR64RegClass.contains(*I)) | |||
3071 | RetOps.push_back(DAG.getRegister(*I, MVT::i64)); | |||
3072 | else | |||
3073 | llvm_unreachable("Unexpected register class in CSRsViaCopy!")::llvm::llvm_unreachable_internal("Unexpected register class in CSRsViaCopy!" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 3073); | |||
3074 | } | |||
3075 | } | |||
3076 | ||||
3077 | RetOps[0] = Chain; // Update chain. | |||
3078 | ||||
3079 | // Add the flag if we have it. | |||
3080 | if (Flag.getNode()) | |||
3081 | RetOps.push_back(Flag); | |||
3082 | ||||
3083 | X86ISD::NodeType opcode = X86ISD::RET_FLAG; | |||
3084 | if (CallConv == CallingConv::X86_INTR) | |||
3085 | opcode = X86ISD::IRET; | |||
3086 | return DAG.getNode(opcode, dl, MVT::Other, RetOps); | |||
3087 | } | |||
3088 | ||||
3089 | bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const { | |||
3090 | if (N->getNumValues() != 1 || !N->hasNUsesOfValue(1, 0)) | |||
3091 | return false; | |||
3092 | ||||
3093 | SDValue TCChain = Chain; | |||
3094 | SDNode *Copy = *N->use_begin(); | |||
3095 | if (Copy->getOpcode() == ISD::CopyToReg) { | |||
3096 | // If the copy has a glue operand, we conservatively assume it isn't safe to | |||
3097 | // perform a tail call. | |||
3098 | if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue) | |||
3099 | return false; | |||
3100 | TCChain = Copy->getOperand(0); | |||
3101 | } else if (Copy->getOpcode() != ISD::FP_EXTEND) | |||
3102 | return false; | |||
3103 | ||||
3104 | bool HasRet = false; | |||
3105 | for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end(); | |||
3106 | UI != UE; ++UI) { | |||
3107 | if (UI->getOpcode() != X86ISD::RET_FLAG) | |||
3108 | return false; | |||
3109 | // If we are returning more than one value, we can definitely | |||
3110 | // not make a tail call see PR19530 | |||
3111 | if (UI->getNumOperands() > 4) | |||
3112 | return false; | |||
3113 | if (UI->getNumOperands() == 4 && | |||
3114 | UI->getOperand(UI->getNumOperands()-1).getValueType() != MVT::Glue) | |||
3115 | return false; | |||
3116 | HasRet = true; | |||
3117 | } | |||
3118 | ||||
3119 | if (!HasRet) | |||
3120 | return false; | |||
3121 | ||||
3122 | Chain = TCChain; | |||
3123 | return true; | |||
3124 | } | |||
3125 | ||||
3126 | EVT X86TargetLowering::getTypeForExtReturn(LLVMContext &Context, EVT VT, | |||
3127 | ISD::NodeType ExtendKind) const { | |||
3128 | MVT ReturnMVT = MVT::i32; | |||
3129 | ||||
3130 | bool Darwin = Subtarget.getTargetTriple().isOSDarwin(); | |||
3131 | if (VT == MVT::i1 || (!Darwin && (VT == MVT::i8 || VT == MVT::i16))) { | |||
3132 | // The ABI does not require i1, i8 or i16 to be extended. | |||
3133 | // | |||
3134 | // On Darwin, there is code in the wild relying on Clang's old behaviour of | |||
3135 | // always extending i8/i16 return values, so keep doing that for now. | |||
3136 | // (PR26665). | |||
3137 | ReturnMVT = MVT::i8; | |||
3138 | } | |||
3139 | ||||
3140 | EVT MinVT = getRegisterType(Context, ReturnMVT); | |||
3141 | return VT.bitsLT(MinVT) ? MinVT : VT; | |||
3142 | } | |||
3143 | ||||
3144 | /// Reads two 32 bit registers and creates a 64 bit mask value. | |||
3145 | /// \param VA The current 32 bit value that need to be assigned. | |||
3146 | /// \param NextVA The next 32 bit value that need to be assigned. | |||
3147 | /// \param Root The parent DAG node. | |||
3148 | /// \param [in,out] InFlag Represents SDvalue in the parent DAG node for | |||
3149 | /// glue purposes. In the case the DAG is already using | |||
3150 | /// physical register instead of virtual, we should glue | |||
3151 | /// our new SDValue to InFlag SDvalue. | |||
3152 | /// \return a new SDvalue of size 64bit. | |||
3153 | static SDValue getv64i1Argument(CCValAssign &VA, CCValAssign &NextVA, | |||
3154 | SDValue &Root, SelectionDAG &DAG, | |||
3155 | const SDLoc &Dl, const X86Subtarget &Subtarget, | |||
3156 | SDValue *InFlag = nullptr) { | |||
3157 | assert((Subtarget.hasBWI()) && "Expected AVX512BW target!")(static_cast <bool> ((Subtarget.hasBWI()) && "Expected AVX512BW target!" ) ? void (0) : __assert_fail ("(Subtarget.hasBWI()) && \"Expected AVX512BW target!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 3157, __extension__ __PRETTY_FUNCTION__)); | |||
3158 | assert(Subtarget.is32Bit() && "Expecting 32 bit target")(static_cast <bool> (Subtarget.is32Bit() && "Expecting 32 bit target" ) ? void (0) : __assert_fail ("Subtarget.is32Bit() && \"Expecting 32 bit target\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 3158, __extension__ __PRETTY_FUNCTION__)); | |||
3159 | assert(VA.getValVT() == MVT::v64i1 &&(static_cast <bool> (VA.getValVT() == MVT::v64i1 && "Expecting first location of 64 bit width type") ? void (0) : __assert_fail ("VA.getValVT() == MVT::v64i1 && \"Expecting first location of 64 bit width type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 3160, __extension__ __PRETTY_FUNCTION__)) | |||
3160 | "Expecting first location of 64 bit width type")(static_cast <bool> (VA.getValVT() == MVT::v64i1 && "Expecting first location of 64 bit width type") ? void (0) : __assert_fail ("VA.getValVT() == MVT::v64i1 && \"Expecting first location of 64 bit width type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 3160, __extension__ __PRETTY_FUNCTION__)); | |||
3161 | assert(NextVA.getValVT() == VA.getValVT() &&(static_cast <bool> (NextVA.getValVT() == VA.getValVT() && "The locations should have the same type") ? void (0) : __assert_fail ("NextVA.getValVT() == VA.getValVT() && \"The locations should have the same type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 3162, __extension__ __PRETTY_FUNCTION__)) | |||
3162 | "The locations should have the same type")(static_cast <bool> (NextVA.getValVT() == VA.getValVT() && "The locations should have the same type") ? void (0) : __assert_fail ("NextVA.getValVT() == VA.getValVT() && \"The locations should have the same type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 3162, __extension__ __PRETTY_FUNCTION__)); | |||
3163 | assert(VA.isRegLoc() && NextVA.isRegLoc() &&(static_cast <bool> (VA.isRegLoc() && NextVA.isRegLoc () && "The values should reside in two registers") ? void (0) : __assert_fail ("VA.isRegLoc() && NextVA.isRegLoc() && \"The values should reside in two registers\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 3164, __extension__ __PRETTY_FUNCTION__)) | |||
3164 | "The values should reside in two registers")(static_cast <bool> (VA.isRegLoc() && NextVA.isRegLoc () && "The values should reside in two registers") ? void (0) : __assert_fail ("VA.isRegLoc() && NextVA.isRegLoc() && \"The values should reside in two registers\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 3164, __extension__ __PRETTY_FUNCTION__)); | |||
3165 | ||||
3166 | SDValue Lo, Hi; | |||
3167 | SDValue ArgValueLo, ArgValueHi; | |||
3168 | ||||
3169 | MachineFunction &MF = DAG.getMachineFunction(); | |||
3170 | const TargetRegisterClass *RC = &X86::GR32RegClass; | |||
3171 | ||||
3172 | // Read a 32 bit value from the registers. | |||
3173 | if (nullptr == InFlag) { | |||
3174 | // When no physical register is present, | |||
3175 | // create an intermediate virtual register. | |||
3176 | Register Reg = MF.addLiveIn(VA.getLocReg(), RC); | |||
3177 | ArgValueLo = DAG.getCopyFromReg(Root, Dl, Reg, MVT::i32); | |||
3178 | Reg = MF.addLiveIn(NextVA.getLocReg(), RC); | |||
3179 | ArgValueHi = DAG.getCopyFromReg(Root, Dl, Reg, MVT::i32); | |||
3180 | } else { | |||
3181 | // When a physical register is available read the value from it and glue | |||
3182 | // the reads together. | |||
3183 | ArgValueLo = | |||
3184 | DAG.getCopyFromReg(Root, Dl, VA.getLocReg(), MVT::i32, *InFlag); | |||
3185 | *InFlag = ArgValueLo.getValue(2); | |||
3186 | ArgValueHi = | |||
3187 | DAG.getCopyFromReg(Root, Dl, NextVA.getLocReg(), MVT::i32, *InFlag); | |||
3188 | *InFlag = ArgValueHi.getValue(2); | |||
3189 | } | |||
3190 | ||||
3191 | // Convert the i32 type into v32i1 type. | |||
3192 | Lo = DAG.getBitcast(MVT::v32i1, ArgValueLo); | |||
3193 | ||||
3194 | // Convert the i32 type into v32i1 type. | |||
3195 | Hi = DAG.getBitcast(MVT::v32i1, ArgValueHi); | |||
3196 | ||||
3197 | // Concatenate the two values together. | |||
3198 | return DAG.getNode(ISD::CONCAT_VECTORS, Dl, MVT::v64i1, Lo, Hi); | |||
3199 | } | |||
3200 | ||||
3201 | /// The function will lower a register of various sizes (8/16/32/64) | |||
3202 | /// to a mask value of the expected size (v8i1/v16i1/v32i1/v64i1) | |||
3203 | /// \returns a DAG node contains the operand after lowering to mask type. | |||
3204 | static SDValue lowerRegToMasks(const SDValue &ValArg, const EVT &ValVT, | |||
3205 | const EVT &ValLoc, const SDLoc &Dl, | |||
3206 | SelectionDAG &DAG) { | |||
3207 | SDValue ValReturned = ValArg; | |||
3208 | ||||
3209 | if (ValVT == MVT::v1i1) | |||
3210 | return DAG.getNode(ISD::SCALAR_TO_VECTOR, Dl, MVT::v1i1, ValReturned); | |||
3211 | ||||
3212 | if (ValVT == MVT::v64i1) { | |||
3213 | // In 32 bit machine, this case is handled by getv64i1Argument | |||
3214 | assert(ValLoc == MVT::i64 && "Expecting only i64 locations")(static_cast <bool> (ValLoc == MVT::i64 && "Expecting only i64 locations" ) ? void (0) : __assert_fail ("ValLoc == MVT::i64 && \"Expecting only i64 locations\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 3214, __extension__ __PRETTY_FUNCTION__)); | |||
3215 | // In 64 bit machine, There is no need to truncate the value only bitcast | |||
3216 | } else { | |||
3217 | MVT maskLen; | |||
3218 | switch (ValVT.getSimpleVT().SimpleTy) { | |||
3219 | case MVT::v8i1: | |||
3220 | maskLen = MVT::i8; | |||
3221 | break; | |||
3222 | case MVT::v16i1: | |||
3223 | maskLen = MVT::i16; | |||
3224 | break; | |||
3225 | case MVT::v32i1: | |||
3226 | maskLen = MVT::i32; | |||
3227 | break; | |||
3228 | default: | |||
3229 | llvm_unreachable("Expecting a vector of i1 types")::llvm::llvm_unreachable_internal("Expecting a vector of i1 types" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 3229); | |||
3230 | } | |||
3231 | ||||
3232 | ValReturned = DAG.getNode(ISD::TRUNCATE, Dl, maskLen, ValReturned); | |||
3233 | } | |||
3234 | return DAG.getBitcast(ValVT, ValReturned); | |||
3235 | } | |||
3236 | ||||
3237 | /// Lower the result values of a call into the | |||
3238 | /// appropriate copies out of appropriate physical registers. | |||
3239 | /// | |||
3240 | SDValue X86TargetLowering::LowerCallResult( | |||
3241 | SDValue Chain, SDValue InFlag, CallingConv::ID CallConv, bool isVarArg, | |||
3242 | const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl, | |||
3243 | SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals, | |||
3244 | uint32_t *RegMask) const { | |||
3245 | ||||
3246 | const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo(); | |||
3247 | // Assign locations to each value returned by this call. | |||
3248 | SmallVector<CCValAssign, 16> RVLocs; | |||
3249 | CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs, | |||
3250 | *DAG.getContext()); | |||
3251 | CCInfo.AnalyzeCallResult(Ins, RetCC_X86); | |||
3252 | ||||
3253 | // Copy all of the result registers out of their specified physreg. | |||
3254 | for (unsigned I = 0, InsIndex = 0, E = RVLocs.size(); I != E; | |||
3255 | ++I, ++InsIndex) { | |||
3256 | CCValAssign &VA = RVLocs[I]; | |||
3257 | EVT CopyVT = VA.getLocVT(); | |||
3258 | ||||
3259 | // In some calling conventions we need to remove the used registers | |||
3260 | // from the register mask. | |||
3261 | if (RegMask) { | |||
3262 | for (MCSubRegIterator SubRegs(VA.getLocReg(), TRI, /*IncludeSelf=*/true); | |||
3263 | SubRegs.isValid(); ++SubRegs) | |||
3264 | RegMask[*SubRegs / 32] &= ~(1u << (*SubRegs % 32)); | |||
3265 | } | |||
3266 | ||||
3267 | // Report an error if there was an attempt to return FP values via XMM | |||
3268 | // registers. | |||
3269 | if (!Subtarget.hasSSE1() && X86::FR32XRegClass.contains(VA.getLocReg())) { | |||
3270 | errorUnsupported(DAG, dl, "SSE register return with SSE disabled"); | |||
3271 | if (VA.getLocReg() == X86::XMM1) | |||
3272 | VA.convertToReg(X86::FP1); // Set reg to FP1, avoid hitting asserts. | |||
3273 | else | |||
3274 | VA.convertToReg(X86::FP0); // Set reg to FP0, avoid hitting asserts. | |||
3275 | } else if (!Subtarget.hasSSE2() && | |||
3276 | X86::FR64XRegClass.contains(VA.getLocReg()) && | |||
3277 | CopyVT == MVT::f64) { | |||
3278 | errorUnsupported(DAG, dl, "SSE2 register return with SSE2 disabled"); | |||
3279 | if (VA.getLocReg() == X86::XMM1) | |||
3280 | VA.convertToReg(X86::FP1); // Set reg to FP1, avoid hitting asserts. | |||
3281 | else | |||
3282 | VA.convertToReg(X86::FP0); // Set reg to FP0, avoid hitting asserts. | |||
3283 | } | |||
3284 | ||||
3285 | // If we prefer to use the value in xmm registers, copy it out as f80 and | |||
3286 | // use a truncate to move it from fp stack reg to xmm reg. | |||
3287 | bool RoundAfterCopy = false; | |||
3288 | if ((VA.getLocReg() == X86::FP0 || VA.getLocReg() == X86::FP1) && | |||
3289 | isScalarFPTypeInSSEReg(VA.getValVT())) { | |||
3290 | if (!Subtarget.hasX87()) | |||
3291 | report_fatal_error("X87 register return with X87 disabled"); | |||
3292 | CopyVT = MVT::f80; | |||
3293 | RoundAfterCopy = (CopyVT != VA.getLocVT()); | |||
3294 | } | |||
3295 | ||||
3296 | SDValue Val; | |||
3297 | if (VA.needsCustom()) { | |||
3298 | assert(VA.getValVT() == MVT::v64i1 &&(static_cast <bool> (VA.getValVT() == MVT::v64i1 && "Currently the only custom case is when we split v64i1 to 2 regs" ) ? void (0) : __assert_fail ("VA.getValVT() == MVT::v64i1 && \"Currently the only custom case is when we split v64i1 to 2 regs\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 3299, __extension__ __PRETTY_FUNCTION__)) | |||
3299 | "Currently the only custom case is when we split v64i1 to 2 regs")(static_cast <bool> (VA.getValVT() == MVT::v64i1 && "Currently the only custom case is when we split v64i1 to 2 regs" ) ? void (0) : __assert_fail ("VA.getValVT() == MVT::v64i1 && \"Currently the only custom case is when we split v64i1 to 2 regs\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 3299, __extension__ __PRETTY_FUNCTION__)); | |||
3300 | Val = | |||
3301 | getv64i1Argument(VA, RVLocs[++I], Chain, DAG, dl, Subtarget, &InFlag); | |||
3302 | } else { | |||
3303 | Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(), CopyVT, InFlag) | |||
3304 | .getValue(1); | |||
3305 | Val = Chain.getValue(0); | |||
3306 | InFlag = Chain.getValue(2); | |||
3307 | } | |||
3308 | ||||
3309 | if (RoundAfterCopy) | |||
3310 | Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val, | |||
3311 | // This truncation won't change the value. | |||
3312 | DAG.getIntPtrConstant(1, dl)); | |||
3313 | ||||
3314 | if (VA.isExtInLoc()) { | |||
3315 | if (VA.getValVT().isVector() && | |||
3316 | VA.getValVT().getScalarType() == MVT::i1 && | |||
3317 | ((VA.getLocVT() == MVT::i64) || (VA.getLocVT() == MVT::i32) || | |||
3318 | (VA.getLocVT() == MVT::i16) || (VA.getLocVT() == MVT::i8))) { | |||
3319 | // promoting a mask type (v*i1) into a register of type i64/i32/i16/i8 | |||
3320 | Val = lowerRegToMasks(Val, VA.getValVT(), VA.getLocVT(), dl, DAG); | |||
3321 | } else | |||
3322 | Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val); | |||
3323 | } | |||
3324 | ||||
3325 | if (VA.getLocInfo() == CCValAssign::BCvt) | |||
3326 | Val = DAG.getBitcast(VA.getValVT(), Val); | |||
3327 | ||||
3328 | InVals.push_back(Val); | |||
3329 | } | |||
3330 | ||||
3331 | return Chain; | |||
3332 | } | |||
3333 | ||||
3334 | //===----------------------------------------------------------------------===// | |||
3335 | // C & StdCall & Fast Calling Convention implementation | |||
3336 | //===----------------------------------------------------------------------===// | |||
3337 | // StdCall calling convention seems to be standard for many Windows' API | |||
3338 | // routines and around. It differs from C calling convention just a little: | |||
3339 | // callee should clean up the stack, not caller. Symbols should be also | |||
3340 | // decorated in some fancy way :) It doesn't support any vector arguments. | |||
3341 | // For info on fast calling convention see Fast Calling Convention (tail call) | |||
3342 | // implementation LowerX86_32FastCCCallTo. | |||
3343 | ||||
3344 | /// Determines whether Args, either a set of outgoing arguments to a call, or a | |||
3345 | /// set of incoming args of a call, contains an sret pointer that the callee | |||
3346 | /// pops | |||
3347 | template <typename T> | |||
3348 | static bool hasCalleePopSRet(const SmallVectorImpl<T> &Args, | |||
3349 | const X86Subtarget &Subtarget) { | |||
3350 | // Not C++20 (yet), so no concepts available. | |||
3351 | static_assert(std::is_same<T, ISD::OutputArg>::value || | |||
3352 | std::is_same<T, ISD::InputArg>::value, | |||
3353 | "requires ISD::OutputArg or ISD::InputArg"); | |||
3354 | ||||
3355 | // Only 32-bit pops the sret. It's a 64-bit world these days, so early-out | |||
3356 | // for most compilations. | |||
3357 | if (!Subtarget.is32Bit()) | |||
3358 | return false; | |||
3359 | ||||
3360 | if (Args.empty()) | |||
3361 | return false; | |||
3362 | ||||
3363 | // Most calls do not have an sret argument, check the arg next. | |||
3364 | const ISD::ArgFlagsTy &Flags = Args[0].Flags; | |||
3365 | if (!Flags.isSRet() || Flags.isInReg()) | |||
3366 | return false; | |||
3367 | ||||
3368 | // The MSVCabi does not pop the sret. | |||
3369 | if (Subtarget.getTargetTriple().isOSMSVCRT()) | |||
3370 | return false; | |||
3371 | ||||
3372 | // MCUs don't pop the sret | |||
3373 | if (Subtarget.isTargetMCU()) | |||
3374 | return false; | |||
3375 | ||||
3376 | // Callee pops argument | |||
3377 | return true; | |||
3378 | } | |||
3379 | ||||
3380 | /// Make a copy of an aggregate at address specified by "Src" to address | |||
3381 | /// "Dst" with size and alignment information specified by the specific | |||
3382 | /// parameter attribute. The copy will be passed as a byval function parameter. | |||
3383 | static SDValue CreateCopyOfByValArgument(SDValue Src, SDValue Dst, | |||
3384 | SDValue Chain, ISD::ArgFlagsTy Flags, | |||
3385 | SelectionDAG &DAG, const SDLoc &dl) { | |||
3386 | SDValue SizeNode = DAG.getIntPtrConstant(Flags.getByValSize(), dl); | |||
3387 | ||||
3388 | return DAG.getMemcpy( | |||
3389 | Chain, dl, Dst, Src, SizeNode, Flags.getNonZeroByValAlign(), | |||
3390 | /*isVolatile*/ false, /*AlwaysInline=*/true, | |||
3391 | /*isTailCall*/ false, MachinePointerInfo(), MachinePointerInfo()); | |||
3392 | } | |||
3393 | ||||
3394 | /// Return true if the calling convention is one that we can guarantee TCO for. | |||
3395 | static bool canGuaranteeTCO(CallingConv::ID CC) { | |||
3396 | return (CC == CallingConv::Fast || CC == CallingConv::GHC || | |||
3397 | CC == CallingConv::X86_RegCall || CC == CallingConv::HiPE || | |||
3398 | CC == CallingConv::HHVM || CC == CallingConv::Tail || | |||
3399 | CC == CallingConv::SwiftTail); | |||
3400 | } | |||
3401 | ||||
3402 | /// Return true if we might ever do TCO for calls with this calling convention. | |||
3403 | static bool mayTailCallThisCC(CallingConv::ID CC) { | |||
3404 | switch (CC) { | |||
3405 | // C calling conventions: | |||
3406 | case CallingConv::C: | |||
3407 | case CallingConv::Win64: | |||
3408 | case CallingConv::X86_64_SysV: | |||
3409 | // Callee pop conventions: | |||
3410 | case CallingConv::X86_ThisCall: | |||
3411 | case CallingConv::X86_StdCall: | |||
3412 | case CallingConv::X86_VectorCall: | |||
3413 | case CallingConv::X86_FastCall: | |||
3414 | // Swift: | |||
3415 | case CallingConv::Swift: | |||
3416 | return true; | |||
3417 | default: | |||
3418 | return canGuaranteeTCO(CC); | |||
3419 | } | |||
3420 | } | |||
3421 | ||||
3422 | /// Return true if the function is being made into a tailcall target by | |||
3423 | /// changing its ABI. | |||
3424 | static bool shouldGuaranteeTCO(CallingConv::ID CC, bool GuaranteedTailCallOpt) { | |||
3425 | return (GuaranteedTailCallOpt && canGuaranteeTCO(CC)) || | |||
3426 | CC == CallingConv::Tail || CC == CallingConv::SwiftTail; | |||
3427 | } | |||
3428 | ||||
3429 | bool X86TargetLowering::mayBeEmittedAsTailCall(const CallInst *CI) const { | |||
3430 | if (!CI->isTailCall()) | |||
3431 | return false; | |||
3432 | ||||
3433 | CallingConv::ID CalleeCC = CI->getCallingConv(); | |||
3434 | if (!mayTailCallThisCC(CalleeCC)) | |||
3435 | return false; | |||
3436 | ||||
3437 | return true; | |||
3438 | } | |||
3439 | ||||
3440 | SDValue | |||
3441 | X86TargetLowering::LowerMemArgument(SDValue Chain, CallingConv::ID CallConv, | |||
3442 | const SmallVectorImpl<ISD::InputArg> &Ins, | |||
3443 | const SDLoc &dl, SelectionDAG &DAG, | |||
3444 | const CCValAssign &VA, | |||
3445 | MachineFrameInfo &MFI, unsigned i) const { | |||
3446 | // Create the nodes corresponding to a load from this parameter slot. | |||
3447 | ISD::ArgFlagsTy Flags = Ins[i].Flags; | |||
3448 | bool AlwaysUseMutable = shouldGuaranteeTCO( | |||
3449 | CallConv, DAG.getTarget().Options.GuaranteedTailCallOpt); | |||
3450 | bool isImmutable = !AlwaysUseMutable && !Flags.isByVal(); | |||
3451 | EVT ValVT; | |||
3452 | MVT PtrVT = getPointerTy(DAG.getDataLayout()); | |||
3453 | ||||
3454 | // If value is passed by pointer we have address passed instead of the value | |||
3455 | // itself. No need to extend if the mask value and location share the same | |||
3456 | // absolute size. | |||
3457 | bool ExtendedInMem = | |||
3458 | VA.isExtInLoc() && VA.getValVT().getScalarType() == MVT::i1 && | |||
3459 | VA.getValVT().getSizeInBits() != VA.getLocVT().getSizeInBits(); | |||
3460 | ||||
3461 | if (VA.getLocInfo() == CCValAssign::Indirect || ExtendedInMem) | |||
3462 | ValVT = VA.getLocVT(); | |||
3463 | else | |||
3464 | ValVT = VA.getValVT(); | |||
3465 | ||||
3466 | // FIXME: For now, all byval parameter objects are marked mutable. This can be | |||
3467 | // changed with more analysis. | |||
3468 | // In case of tail call optimization mark all arguments mutable. Since they | |||
3469 | // could be overwritten by lowering of arguments in case of a tail call. | |||
3470 | if (Flags.isByVal()) { | |||
3471 | unsigned Bytes = Flags.getByValSize(); | |||
3472 | if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects. | |||
3473 | ||||
3474 | // FIXME: For now, all byval parameter objects are marked as aliasing. This | |||
3475 | // can be improved with deeper analysis. | |||
3476 | int FI = MFI.CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable, | |||
3477 | /*isAliased=*/true); | |||
3478 | return DAG.getFrameIndex(FI, PtrVT); | |||
3479 | } | |||
3480 | ||||
3481 | EVT ArgVT = Ins[i].ArgVT; | |||
3482 | ||||
3483 | // If this is a vector that has been split into multiple parts, and the | |||
3484 | // scalar size of the parts don't match the vector element size, then we can't | |||
3485 | // elide the copy. The parts will have padding between them instead of being | |||
3486 | // packed like a vector. | |||
3487 | bool ScalarizedAndExtendedVector = | |||
3488 | ArgVT.isVector() && !VA.getLocVT().isVector() && | |||
3489 | VA.getLocVT().getSizeInBits() != ArgVT.getScalarSizeInBits(); | |||
3490 | ||||
3491 | // This is an argument in memory. We might be able to perform copy elision. | |||
3492 | // If the argument is passed directly in memory without any extension, then we | |||
3493 | // can perform copy elision. Large vector types, for example, may be passed | |||
3494 | // indirectly by pointer. | |||
3495 | if (Flags.isCopyElisionCandidate() && | |||
3496 | VA.getLocInfo() != CCValAssign::Indirect && !ExtendedInMem && | |||
3497 | !ScalarizedAndExtendedVector) { | |||
3498 | SDValue PartAddr; | |||
3499 | if (Ins[i].PartOffset == 0) { | |||
3500 | // If this is a one-part value or the first part of a multi-part value, | |||
3501 | // create a stack object for the entire argument value type and return a | |||
3502 | // load from our portion of it. This assumes that if the first part of an | |||
3503 | // argument is in memory, the rest will also be in memory. | |||
3504 | int FI = MFI.CreateFixedObject(ArgVT.getStoreSize(), VA.getLocMemOffset(), | |||
3505 | /*IsImmutable=*/false); | |||
3506 | PartAddr = DAG.getFrameIndex(FI, PtrVT); | |||
3507 | return DAG.getLoad( | |||
3508 | ValVT, dl, Chain, PartAddr, | |||
3509 | MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI)); | |||
3510 | } else { | |||
3511 | // This is not the first piece of an argument in memory. See if there is | |||
3512 | // already a fixed stack object including this offset. If so, assume it | |||
3513 | // was created by the PartOffset == 0 branch above and create a load from | |||
3514 | // the appropriate offset into it. | |||
3515 | int64_t PartBegin = VA.getLocMemOffset(); | |||
3516 | int64_t PartEnd = PartBegin + ValVT.getSizeInBits() / 8; | |||
3517 | int FI = MFI.getObjectIndexBegin(); | |||
3518 | for (; MFI.isFixedObjectIndex(FI); ++FI) { | |||
3519 | int64_t ObjBegin = MFI.getObjectOffset(FI); | |||
3520 | int64_t ObjEnd = ObjBegin + MFI.getObjectSize(FI); | |||
3521 | if (ObjBegin <= PartBegin && PartEnd <= ObjEnd) | |||
3522 | break; | |||
3523 | } | |||
3524 | if (MFI.isFixedObjectIndex(FI)) { | |||
3525 | SDValue Addr = | |||
3526 | DAG.getNode(ISD::ADD, dl, PtrVT, DAG.getFrameIndex(FI, PtrVT), | |||
3527 | DAG.getIntPtrConstant(Ins[i].PartOffset, dl)); | |||
3528 | return DAG.getLoad( | |||
3529 | ValVT, dl, Chain, Addr, | |||
3530 | MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI, | |||
3531 | Ins[i].PartOffset)); | |||
3532 | } | |||
3533 | } | |||
3534 | } | |||
3535 | ||||
3536 | int FI = MFI.CreateFixedObject(ValVT.getSizeInBits() / 8, | |||
3537 | VA.getLocMemOffset(), isImmutable); | |||
3538 | ||||
3539 | // Set SExt or ZExt flag. | |||
3540 | if (VA.getLocInfo() == CCValAssign::ZExt) { | |||
3541 | MFI.setObjectZExt(FI, true); | |||
3542 | } else if (VA.getLocInfo() == CCValAssign::SExt) { | |||
3543 | MFI.setObjectSExt(FI, true); | |||
3544 | } | |||
3545 | ||||
3546 | SDValue FIN = DAG.getFrameIndex(FI, PtrVT); | |||
3547 | SDValue Val = DAG.getLoad( | |||
3548 | ValVT, dl, Chain, FIN, | |||
3549 | MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI)); | |||
3550 | return ExtendedInMem | |||
3551 | ? (VA.getValVT().isVector() | |||
3552 | ? DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VA.getValVT(), Val) | |||
3553 | : DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val)) | |||
3554 | : Val; | |||
3555 | } | |||
3556 | ||||
3557 | // FIXME: Get this from tablegen. | |||
3558 | static ArrayRef<MCPhysReg> get64BitArgumentGPRs(CallingConv::ID CallConv, | |||
3559 | const X86Subtarget &Subtarget) { | |||
3560 | assert(Subtarget.is64Bit())(static_cast <bool> (Subtarget.is64Bit()) ? void (0) : __assert_fail ("Subtarget.is64Bit()", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 3560, __extension__ __PRETTY_FUNCTION__)); | |||
3561 | ||||
3562 | if (Subtarget.isCallingConvWin64(CallConv)) { | |||
3563 | static const MCPhysReg GPR64ArgRegsWin64[] = { | |||
3564 | X86::RCX, X86::RDX, X86::R8, X86::R9 | |||
3565 | }; | |||
3566 | return makeArrayRef(std::begin(GPR64ArgRegsWin64), std::end(GPR64ArgRegsWin64)); | |||
3567 | } | |||
3568 | ||||
3569 | static const MCPhysReg GPR64ArgRegs64Bit[] = { | |||
3570 | X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9 | |||
3571 | }; | |||
3572 | return makeArrayRef(std::begin(GPR64ArgRegs64Bit), std::end(GPR64ArgRegs64Bit)); | |||
3573 | } | |||
3574 | ||||
3575 | // FIXME: Get this from tablegen. | |||
3576 | static ArrayRef<MCPhysReg> get64BitArgumentXMMs(MachineFunction &MF, | |||
3577 | CallingConv::ID CallConv, | |||
3578 | const X86Subtarget &Subtarget) { | |||
3579 | assert(Subtarget.is64Bit())(static_cast <bool> (Subtarget.is64Bit()) ? void (0) : __assert_fail ("Subtarget.is64Bit()", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 3579, __extension__ __PRETTY_FUNCTION__)); | |||
3580 | if (Subtarget.isCallingConvWin64(CallConv)) { | |||
3581 | // The XMM registers which might contain var arg parameters are shadowed | |||
3582 | // in their paired GPR. So we only need to save the GPR to their home | |||
3583 | // slots. | |||
3584 | // TODO: __vectorcall will change this. | |||
3585 | return None; | |||
3586 | } | |||
3587 | ||||
3588 | bool isSoftFloat = Subtarget.useSoftFloat(); | |||
3589 | if (isSoftFloat || !Subtarget.hasSSE1()) | |||
3590 | // Kernel mode asks for SSE to be disabled, so there are no XMM argument | |||
3591 | // registers. | |||
3592 | return None; | |||
3593 | ||||
3594 | static const MCPhysReg XMMArgRegs64Bit[] = { | |||
3595 | X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3, | |||
3596 | X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7 | |||
3597 | }; | |||
3598 | return makeArrayRef(std::begin(XMMArgRegs64Bit), std::end(XMMArgRegs64Bit)); | |||
3599 | } | |||
3600 | ||||
3601 | #ifndef NDEBUG | |||
3602 | static bool isSortedByValueNo(ArrayRef<CCValAssign> ArgLocs) { | |||
3603 | return llvm::is_sorted( | |||
3604 | ArgLocs, [](const CCValAssign &A, const CCValAssign &B) -> bool { | |||
3605 | return A.getValNo() < B.getValNo(); | |||
3606 | }); | |||
3607 | } | |||
3608 | #endif | |||
3609 | ||||
3610 | namespace { | |||
3611 | /// This is a helper class for lowering variable arguments parameters. | |||
3612 | class VarArgsLoweringHelper { | |||
3613 | public: | |||
3614 | VarArgsLoweringHelper(X86MachineFunctionInfo *FuncInfo, const SDLoc &Loc, | |||
3615 | SelectionDAG &DAG, const X86Subtarget &Subtarget, | |||
3616 | CallingConv::ID CallConv, CCState &CCInfo) | |||
3617 | : FuncInfo(FuncInfo), DL(Loc), DAG(DAG), Subtarget(Subtarget), | |||
3618 | TheMachineFunction(DAG.getMachineFunction()), | |||
3619 | TheFunction(TheMachineFunction.getFunction()), | |||
3620 | FrameInfo(TheMachineFunction.getFrameInfo()), | |||
3621 | FrameLowering(*Subtarget.getFrameLowering()), | |||
3622 | TargLowering(DAG.getTargetLoweringInfo()), CallConv(CallConv), | |||
3623 | CCInfo(CCInfo) {} | |||
3624 | ||||
3625 | // Lower variable arguments parameters. | |||
3626 | void lowerVarArgsParameters(SDValue &Chain, unsigned StackSize); | |||
3627 | ||||
3628 | private: | |||
3629 | void createVarArgAreaAndStoreRegisters(SDValue &Chain, unsigned StackSize); | |||
3630 | ||||
3631 | void forwardMustTailParameters(SDValue &Chain); | |||
3632 | ||||
3633 | bool is64Bit() const { return Subtarget.is64Bit(); } | |||
3634 | bool isWin64() const { return Subtarget.isCallingConvWin64(CallConv); } | |||
3635 | ||||
3636 | X86MachineFunctionInfo *FuncInfo; | |||
3637 | const SDLoc &DL; | |||
3638 | SelectionDAG &DAG; | |||
3639 | const X86Subtarget &Subtarget; | |||
3640 | MachineFunction &TheMachineFunction; | |||
3641 | const Function &TheFunction; | |||
3642 | MachineFrameInfo &FrameInfo; | |||
3643 | const TargetFrameLowering &FrameLowering; | |||
3644 | const TargetLowering &TargLowering; | |||
3645 | CallingConv::ID CallConv; | |||
3646 | CCState &CCInfo; | |||
3647 | }; | |||
3648 | } // namespace | |||
3649 | ||||
3650 | void VarArgsLoweringHelper::createVarArgAreaAndStoreRegisters( | |||
3651 | SDValue &Chain, unsigned StackSize) { | |||
3652 | // If the function takes variable number of arguments, make a frame index for | |||
3653 | // the start of the first vararg value... for expansion of llvm.va_start. We | |||
3654 | // can skip this if there are no va_start calls. | |||
3655 | if (is64Bit() || (CallConv != CallingConv::X86_FastCall && | |||
3656 | CallConv != CallingConv::X86_ThisCall)) { | |||
3657 | FuncInfo->setVarArgsFrameIndex( | |||
3658 | FrameInfo.CreateFixedObject(1, StackSize, true)); | |||
3659 | } | |||
3660 | ||||
3661 | // 64-bit calling conventions support varargs and register parameters, so we | |||
3662 | // have to do extra work to spill them in the prologue. | |||
3663 | if (is64Bit()) { | |||
3664 | // Find the first unallocated argument registers. | |||
3665 | ArrayRef<MCPhysReg> ArgGPRs = get64BitArgumentGPRs(CallConv, Subtarget); | |||
3666 | ArrayRef<MCPhysReg> ArgXMMs = | |||
3667 | get64BitArgumentXMMs(TheMachineFunction, CallConv, Subtarget); | |||
3668 | unsigned NumIntRegs = CCInfo.getFirstUnallocated(ArgGPRs); | |||
3669 | unsigned NumXMMRegs = CCInfo.getFirstUnallocated(ArgXMMs); | |||
3670 | ||||
3671 | assert(!(NumXMMRegs && !Subtarget.hasSSE1()) &&(static_cast <bool> (!(NumXMMRegs && !Subtarget .hasSSE1()) && "SSE register cannot be used when SSE is disabled!" ) ? void (0) : __assert_fail ("!(NumXMMRegs && !Subtarget.hasSSE1()) && \"SSE register cannot be used when SSE is disabled!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 3672, __extension__ __PRETTY_FUNCTION__)) | |||
3672 | "SSE register cannot be used when SSE is disabled!")(static_cast <bool> (!(NumXMMRegs && !Subtarget .hasSSE1()) && "SSE register cannot be used when SSE is disabled!" ) ? void (0) : __assert_fail ("!(NumXMMRegs && !Subtarget.hasSSE1()) && \"SSE register cannot be used when SSE is disabled!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 3672, __extension__ __PRETTY_FUNCTION__)); | |||
3673 | ||||
3674 | if (isWin64()) { | |||
3675 | // Get to the caller-allocated home save location. Add 8 to account | |||
3676 | // for the return address. | |||
3677 | int HomeOffset = FrameLowering.getOffsetOfLocalArea() + 8; | |||
3678 | FuncInfo->setRegSaveFrameIndex( | |||
3679 | FrameInfo.CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false)); | |||
3680 | // Fixup to set vararg frame on shadow area (4 x i64). | |||
3681 | if (NumIntRegs < 4) | |||
3682 | FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex()); | |||
3683 | } else { | |||
3684 | // For X86-64, if there are vararg parameters that are passed via | |||
3685 | // registers, then we must store them to their spots on the stack so | |||
3686 | // they may be loaded by dereferencing the result of va_next. | |||
3687 | FuncInfo->setVarArgsGPOffset(NumIntRegs * 8); | |||
3688 | FuncInfo->setVarArgsFPOffset(ArgGPRs.size() * 8 + NumXMMRegs * 16); | |||
3689 | FuncInfo->setRegSaveFrameIndex(FrameInfo.CreateStackObject( | |||
3690 | ArgGPRs.size() * 8 + ArgXMMs.size() * 16, Align(16), false)); | |||
3691 | } | |||
3692 | ||||
3693 | SmallVector<SDValue, 6> | |||
3694 | LiveGPRs; // list of SDValue for GPR registers keeping live input value | |||
3695 | SmallVector<SDValue, 8> LiveXMMRegs; // list of SDValue for XMM registers | |||
3696 | // keeping live input value | |||
3697 | SDValue ALVal; // if applicable keeps SDValue for %al register | |||
3698 | ||||
3699 | // Gather all the live in physical registers. | |||
3700 | for (MCPhysReg Reg : ArgGPRs.slice(NumIntRegs)) { | |||
3701 | Register GPR = TheMachineFunction.addLiveIn(Reg, &X86::GR64RegClass); | |||
3702 | LiveGPRs.push_back(DAG.getCopyFromReg(Chain, DL, GPR, MVT::i64)); | |||
3703 | } | |||
3704 | const auto &AvailableXmms = ArgXMMs.slice(NumXMMRegs); | |||
3705 | if (!AvailableXmms.empty()) { | |||
3706 | Register AL = TheMachineFunction.addLiveIn(X86::AL, &X86::GR8RegClass); | |||
3707 | ALVal = DAG.getCopyFromReg(Chain, DL, AL, MVT::i8); | |||
3708 | for (MCPhysReg Reg : AvailableXmms) { | |||
3709 | // FastRegisterAllocator spills virtual registers at basic | |||
3710 | // block boundary. That leads to usages of xmm registers | |||
3711 | // outside of check for %al. Pass physical registers to | |||
3712 | // VASTART_SAVE_XMM_REGS to avoid unneccessary spilling. | |||
3713 | TheMachineFunction.getRegInfo().addLiveIn(Reg); | |||
3714 | LiveXMMRegs.push_back(DAG.getRegister(Reg, MVT::v4f32)); | |||
3715 | } | |||
3716 | } | |||
3717 | ||||
3718 | // Store the integer parameter registers. | |||
3719 | SmallVector<SDValue, 8> MemOps; | |||
3720 | SDValue RSFIN = | |||
3721 | DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), | |||
3722 | TargLowering.getPointerTy(DAG.getDataLayout())); | |||
3723 | unsigned Offset = FuncInfo->getVarArgsGPOffset(); | |||
3724 | for (SDValue Val : LiveGPRs) { | |||
3725 | SDValue FIN = DAG.getNode(ISD::ADD, DL, | |||
3726 | TargLowering.getPointerTy(DAG.getDataLayout()), | |||
3727 | RSFIN, DAG.getIntPtrConstant(Offset, DL)); | |||
3728 | SDValue Store = | |||
3729 | DAG.getStore(Val.getValue(1), DL, Val, FIN, | |||
3730 | MachinePointerInfo::getFixedStack( | |||
3731 | DAG.getMachineFunction(), | |||
3732 | FuncInfo->getRegSaveFrameIndex(), Offset)); | |||
3733 | MemOps.push_back(Store); | |||
3734 | Offset += 8; | |||
3735 | } | |||
3736 | ||||
3737 | // Now store the XMM (fp + vector) parameter registers. | |||
3738 | if (!LiveXMMRegs.empty()) { | |||
3739 | SmallVector<SDValue, 12> SaveXMMOps; | |||
3740 | SaveXMMOps.push_back(Chain); | |||
3741 | SaveXMMOps.push_back(ALVal); | |||
3742 | SaveXMMOps.push_back( | |||
3743 | DAG.getTargetConstant(FuncInfo->getRegSaveFrameIndex(), DL, MVT::i32)); | |||
3744 | SaveXMMOps.push_back( | |||
3745 | DAG.getTargetConstant(FuncInfo->getVarArgsFPOffset(), DL, MVT::i32)); | |||
3746 | llvm::append_range(SaveXMMOps, LiveXMMRegs); | |||
3747 | MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, DL, | |||
3748 | MVT::Other, SaveXMMOps)); | |||
3749 | } | |||
3750 | ||||
3751 | if (!MemOps.empty()) | |||
3752 | Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps); | |||
3753 | } | |||
3754 | } | |||
3755 | ||||
3756 | void VarArgsLoweringHelper::forwardMustTailParameters(SDValue &Chain) { | |||
3757 | // Find the largest legal vector type. | |||
3758 | MVT VecVT = MVT::Other; | |||
3759 | // FIXME: Only some x86_32 calling conventions support AVX512. | |||
3760 | if (Subtarget.useAVX512Regs() && | |||
3761 | (is64Bit() || (CallConv == CallingConv::X86_VectorCall || | |||
3762 | CallConv == CallingConv::Intel_OCL_BI))) | |||
3763 | VecVT = MVT::v16f32; | |||
3764 | else if (Subtarget.hasAVX()) | |||
3765 | VecVT = MVT::v8f32; | |||
3766 | else if (Subtarget.hasSSE2()) | |||
3767 | VecVT = MVT::v4f32; | |||
3768 | ||||
3769 | // We forward some GPRs and some vector types. | |||
3770 | SmallVector<MVT, 2> RegParmTypes; | |||
3771 | MVT IntVT = is64Bit() ? MVT::i64 : MVT::i32; | |||
3772 | RegParmTypes.push_back(IntVT); | |||
3773 | if (VecVT != MVT::Other) | |||
3774 | RegParmTypes.push_back(VecVT); | |||
3775 | ||||
3776 | // Compute the set of forwarded registers. The rest are scratch. | |||
3777 | SmallVectorImpl<ForwardedRegister> &Forwards = | |||
3778 | FuncInfo->getForwardedMustTailRegParms(); | |||
3779 | CCInfo.analyzeMustTailForwardedRegisters(Forwards, RegParmTypes, CC_X86); | |||
3780 | ||||
3781 | // Forward AL for SysV x86_64 targets, since it is used for varargs. | |||
3782 | if (is64Bit() && !isWin64() && !CCInfo.isAllocated(X86::AL)) { | |||
3783 | Register ALVReg = TheMachineFunction.addLiveIn(X86::AL, &X86::GR8RegClass); | |||
3784 | Forwards.push_back(ForwardedRegister(ALVReg, X86::AL, MVT::i8)); | |||
3785 | } | |||
3786 | ||||
3787 | // Copy all forwards from physical to virtual registers. | |||
3788 | for (ForwardedRegister &FR : Forwards) { | |||
3789 | // FIXME: Can we use a less constrained schedule? | |||
3790 | SDValue RegVal = DAG.getCopyFromReg(Chain, DL, FR.VReg, FR.VT); | |||
3791 | FR.VReg = TheMachineFunction.getRegInfo().createVirtualRegister( | |||
3792 | TargLowering.getRegClassFor(FR.VT)); | |||
3793 | Chain = DAG.getCopyToReg(Chain, DL, FR.VReg, RegVal); | |||
3794 | } | |||
3795 | } | |||
3796 | ||||
3797 | void VarArgsLoweringHelper::lowerVarArgsParameters(SDValue &Chain, | |||
3798 | unsigned StackSize) { | |||
3799 | // Set FrameIndex to the 0xAAAAAAA value to mark unset state. | |||
3800 | // If necessary, it would be set into the correct value later. | |||
3801 | FuncInfo->setVarArgsFrameIndex(0xAAAAAAA); | |||
3802 | FuncInfo->setRegSaveFrameIndex(0xAAAAAAA); | |||
3803 | ||||
3804 | if (FrameInfo.hasVAStart()) | |||
3805 | createVarArgAreaAndStoreRegisters(Chain, StackSize); | |||
3806 | ||||
3807 | if (FrameInfo.hasMustTailInVarArgFunc()) | |||
3808 | forwardMustTailParameters(Chain); | |||
3809 | } | |||
3810 | ||||
3811 | SDValue X86TargetLowering::LowerFormalArguments( | |||
3812 | SDValue Chain, CallingConv::ID CallConv, bool IsVarArg, | |||
3813 | const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &dl, | |||
3814 | SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const { | |||
3815 | MachineFunction &MF = DAG.getMachineFunction(); | |||
3816 | X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>(); | |||
3817 | ||||
3818 | const Function &F = MF.getFunction(); | |||
3819 | if (F.hasExternalLinkage() && Subtarget.isTargetCygMing() && | |||
3820 | F.getName() == "main") | |||
3821 | FuncInfo->setForceFramePointer(true); | |||
3822 | ||||
3823 | MachineFrameInfo &MFI = MF.getFrameInfo(); | |||
3824 | bool Is64Bit = Subtarget.is64Bit(); | |||
3825 | bool IsWin64 = Subtarget.isCallingConvWin64(CallConv); | |||
3826 | ||||
3827 | assert((static_cast <bool> (!(IsVarArg && canGuaranteeTCO (CallConv)) && "Var args not supported with calling conv' regcall, fastcc, ghc or hipe" ) ? void (0) : __assert_fail ("!(IsVarArg && canGuaranteeTCO(CallConv)) && \"Var args not supported with calling conv' regcall, fastcc, ghc or hipe\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 3829, __extension__ __PRETTY_FUNCTION__)) | |||
3828 | !(IsVarArg && canGuaranteeTCO(CallConv)) &&(static_cast <bool> (!(IsVarArg && canGuaranteeTCO (CallConv)) && "Var args not supported with calling conv' regcall, fastcc, ghc or hipe" ) ? void (0) : __assert_fail ("!(IsVarArg && canGuaranteeTCO(CallConv)) && \"Var args not supported with calling conv' regcall, fastcc, ghc or hipe\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 3829, __extension__ __PRETTY_FUNCTION__)) | |||
3829 | "Var args not supported with calling conv' regcall, fastcc, ghc or hipe")(static_cast <bool> (!(IsVarArg && canGuaranteeTCO (CallConv)) && "Var args not supported with calling conv' regcall, fastcc, ghc or hipe" ) ? void (0) : __assert_fail ("!(IsVarArg && canGuaranteeTCO(CallConv)) && \"Var args not supported with calling conv' regcall, fastcc, ghc or hipe\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 3829, __extension__ __PRETTY_FUNCTION__)); | |||
3830 | ||||
3831 | // Assign locations to all of the incoming arguments. | |||
3832 | SmallVector<CCValAssign, 16> ArgLocs; | |||
3833 | CCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext()); | |||
3834 | ||||
3835 | // Allocate shadow area for Win64. | |||
3836 | if (IsWin64) | |||
3837 | CCInfo.AllocateStack(32, Align(8)); | |||
3838 | ||||
3839 | CCInfo.AnalyzeArguments(Ins, CC_X86); | |||
3840 | ||||
3841 | // In vectorcall calling convention a second pass is required for the HVA | |||
3842 | // types. | |||
3843 | if (CallingConv::X86_VectorCall == CallConv) { | |||
3844 | CCInfo.AnalyzeArgumentsSecondPass(Ins, CC_X86); | |||
3845 | } | |||
3846 | ||||
3847 | // The next loop assumes that the locations are in the same order of the | |||
3848 | // input arguments. | |||
3849 | assert(isSortedByValueNo(ArgLocs) &&(static_cast <bool> (isSortedByValueNo(ArgLocs) && "Argument Location list must be sorted before lowering") ? void (0) : __assert_fail ("isSortedByValueNo(ArgLocs) && \"Argument Location list must be sorted before lowering\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 3850, __extension__ __PRETTY_FUNCTION__)) | |||
3850 | "Argument Location list must be sorted before lowering")(static_cast <bool> (isSortedByValueNo(ArgLocs) && "Argument Location list must be sorted before lowering") ? void (0) : __assert_fail ("isSortedByValueNo(ArgLocs) && \"Argument Location list must be sorted before lowering\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 3850, __extension__ __PRETTY_FUNCTION__)); | |||
3851 | ||||
3852 | SDValue ArgValue; | |||
3853 | for (unsigned I = 0, InsIndex = 0, E = ArgLocs.size(); I != E; | |||
3854 | ++I, ++InsIndex) { | |||
3855 | assert(InsIndex < Ins.size() && "Invalid Ins index")(static_cast <bool> (InsIndex < Ins.size() && "Invalid Ins index") ? void (0) : __assert_fail ("InsIndex < Ins.size() && \"Invalid Ins index\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 3855, __extension__ __PRETTY_FUNCTION__)); | |||
3856 | CCValAssign &VA = ArgLocs[I]; | |||
3857 | ||||
3858 | if (VA.isRegLoc()) { | |||
3859 | EVT RegVT = VA.getLocVT(); | |||
3860 | if (VA.needsCustom()) { | |||
3861 | assert((static_cast <bool> (VA.getValVT() == MVT::v64i1 && "Currently the only custom case is when we split v64i1 to 2 regs" ) ? void (0) : __assert_fail ("VA.getValVT() == MVT::v64i1 && \"Currently the only custom case is when we split v64i1 to 2 regs\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 3863, __extension__ __PRETTY_FUNCTION__)) | |||
3862 | VA.getValVT() == MVT::v64i1 &&(static_cast <bool> (VA.getValVT() == MVT::v64i1 && "Currently the only custom case is when we split v64i1 to 2 regs" ) ? void (0) : __assert_fail ("VA.getValVT() == MVT::v64i1 && \"Currently the only custom case is when we split v64i1 to 2 regs\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 3863, __extension__ __PRETTY_FUNCTION__)) | |||
3863 | "Currently the only custom case is when we split v64i1 to 2 regs")(static_cast <bool> (VA.getValVT() == MVT::v64i1 && "Currently the only custom case is when we split v64i1 to 2 regs" ) ? void (0) : __assert_fail ("VA.getValVT() == MVT::v64i1 && \"Currently the only custom case is when we split v64i1 to 2 regs\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 3863, __extension__ __PRETTY_FUNCTION__)); | |||
3864 | ||||
3865 | // v64i1 values, in regcall calling convention, that are | |||
3866 | // compiled to 32 bit arch, are split up into two registers. | |||
3867 | ArgValue = | |||
3868 | getv64i1Argument(VA, ArgLocs[++I], Chain, DAG, dl, Subtarget); | |||
3869 | } else { | |||
3870 | const TargetRegisterClass *RC; | |||
3871 | if (RegVT == MVT::i8) | |||
3872 | RC = &X86::GR8RegClass; | |||
3873 | else if (RegVT == MVT::i16) | |||
3874 | RC = &X86::GR16RegClass; | |||
3875 | else if (RegVT == MVT::i32) | |||
3876 | RC = &X86::GR32RegClass; | |||
3877 | else if (Is64Bit && RegVT == MVT::i64) | |||
3878 | RC = &X86::GR64RegClass; | |||
3879 | else if (RegVT == MVT::f16) | |||
3880 | RC = &X86::FR16XRegClass; | |||
3881 | else if (RegVT == MVT::f32) | |||
3882 | RC = Subtarget.hasAVX512() ? &X86::FR32XRegClass : &X86::FR32RegClass; | |||
3883 | else if (RegVT == MVT::f64) | |||
3884 | RC = Subtarget.hasAVX512() ? &X86::FR64XRegClass : &X86::FR64RegClass; | |||
3885 | else if (RegVT == MVT::f80) | |||
3886 | RC = &X86::RFP80RegClass; | |||
3887 | else if (RegVT == MVT::f128) | |||
3888 | RC = &X86::VR128RegClass; | |||
3889 | else if (RegVT.is512BitVector()) | |||
3890 | RC = &X86::VR512RegClass; | |||
3891 | else if (RegVT.is256BitVector()) | |||
3892 | RC = Subtarget.hasVLX() ? &X86::VR256XRegClass : &X86::VR256RegClass; | |||
3893 | else if (RegVT.is128BitVector()) | |||
3894 | RC = Subtarget.hasVLX() ? &X86::VR128XRegClass : &X86::VR128RegClass; | |||
3895 | else if (RegVT == MVT::x86mmx) | |||
3896 | RC = &X86::VR64RegClass; | |||
3897 | else if (RegVT == MVT::v1i1) | |||
3898 | RC = &X86::VK1RegClass; | |||
3899 | else if (RegVT == MVT::v8i1) | |||
3900 | RC = &X86::VK8RegClass; | |||
3901 | else if (RegVT == MVT::v16i1) | |||
3902 | RC = &X86::VK16RegClass; | |||
3903 | else if (RegVT == MVT::v32i1) | |||
3904 | RC = &X86::VK32RegClass; | |||
3905 | else if (RegVT == MVT::v64i1) | |||
3906 | RC = &X86::VK64RegClass; | |||
3907 | else | |||
3908 | llvm_unreachable("Unknown argument type!")::llvm::llvm_unreachable_internal("Unknown argument type!", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 3908); | |||
3909 | ||||
3910 | Register Reg = MF.addLiveIn(VA.getLocReg(), RC); | |||
3911 | ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT); | |||
3912 | } | |||
3913 | ||||
3914 | // If this is an 8 or 16-bit value, it is really passed promoted to 32 | |||
3915 | // bits. Insert an assert[sz]ext to capture this, then truncate to the | |||
3916 | // right size. | |||
3917 | if (VA.getLocInfo() == CCValAssign::SExt) | |||
3918 | ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue, | |||
3919 | DAG.getValueType(VA.getValVT())); | |||
3920 | else if (VA.getLocInfo() == CCValAssign::ZExt) | |||
3921 | ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue, | |||
3922 | DAG.getValueType(VA.getValVT())); | |||
3923 | else if (VA.getLocInfo() == CCValAssign::BCvt) | |||
3924 | ArgValue = DAG.getBitcast(VA.getValVT(), ArgValue); | |||
3925 | ||||
3926 | if (VA.isExtInLoc()) { | |||
3927 | // Handle MMX values passed in XMM regs. | |||
3928 | if (RegVT.isVector() && VA.getValVT().getScalarType() != MVT::i1) | |||
3929 | ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(), ArgValue); | |||
3930 | else if (VA.getValVT().isVector() && | |||
3931 | VA.getValVT().getScalarType() == MVT::i1 && | |||
3932 | ((VA.getLocVT() == MVT::i64) || (VA.getLocVT() == MVT::i32) || | |||
3933 | (VA.getLocVT() == MVT::i16) || (VA.getLocVT() == MVT::i8))) { | |||
3934 | // Promoting a mask type (v*i1) into a register of type i64/i32/i16/i8 | |||
3935 | ArgValue = lowerRegToMasks(ArgValue, VA.getValVT(), RegVT, dl, DAG); | |||
3936 | } else | |||
3937 | ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue); | |||
3938 | } | |||
3939 | } else { | |||
3940 | assert(VA.isMemLoc())(static_cast <bool> (VA.isMemLoc()) ? void (0) : __assert_fail ("VA.isMemLoc()", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 3940, __extension__ __PRETTY_FUNCTION__)); | |||
3941 | ArgValue = | |||
3942 | LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, InsIndex); | |||
3943 | } | |||
3944 | ||||
3945 | // If value is passed via pointer - do a load. | |||
3946 | if (VA.getLocInfo() == CCValAssign::Indirect && !Ins[I].Flags.isByVal()) | |||
3947 | ArgValue = | |||
3948 | DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue, MachinePointerInfo()); | |||
3949 | ||||
3950 | InVals.push_back(ArgValue); | |||
3951 | } | |||
3952 | ||||
3953 | for (unsigned I = 0, E = Ins.size(); I != E; ++I) { | |||
3954 | if (Ins[I].Flags.isSwiftAsync()) { | |||
3955 | auto X86FI = MF.getInfo<X86MachineFunctionInfo>(); | |||
3956 | if (Subtarget.is64Bit()) | |||
3957 | X86FI->setHasSwiftAsyncContext(true); | |||
3958 | else { | |||
3959 | int FI = MF.getFrameInfo().CreateStackObject(4, Align(4), false); | |||
3960 | X86FI->setSwiftAsyncContextFrameIdx(FI); | |||
3961 | SDValue St = DAG.getStore(DAG.getEntryNode(), dl, InVals[I], | |||
3962 | DAG.getFrameIndex(FI, MVT::i32), | |||
3963 | MachinePointerInfo::getFixedStack(MF, FI)); | |||
3964 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, St, Chain); | |||
3965 | } | |||
3966 | } | |||
3967 | ||||
3968 | // Swift calling convention does not require we copy the sret argument | |||
3969 | // into %rax/%eax for the return. We don't set SRetReturnReg for Swift. | |||
3970 | if (CallConv == CallingConv::Swift || CallConv == CallingConv::SwiftTail) | |||
3971 | continue; | |||
3972 | ||||
3973 | // All x86 ABIs require that for returning structs by value we copy the | |||
3974 | // sret argument into %rax/%eax (depending on ABI) for the return. Save | |||
3975 | // the argument into a virtual register so that we can access it from the | |||
3976 | // return points. | |||
3977 | if (Ins[I].Flags.isSRet()) { | |||
3978 | assert(!FuncInfo->getSRetReturnReg() &&(static_cast <bool> (!FuncInfo->getSRetReturnReg() && "SRet return has already been set") ? void (0) : __assert_fail ("!FuncInfo->getSRetReturnReg() && \"SRet return has already been set\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 3979, __extension__ __PRETTY_FUNCTION__)) | |||
3979 | "SRet return has already been set")(static_cast <bool> (!FuncInfo->getSRetReturnReg() && "SRet return has already been set") ? void (0) : __assert_fail ("!FuncInfo->getSRetReturnReg() && \"SRet return has already been set\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 3979, __extension__ __PRETTY_FUNCTION__)); | |||
3980 | MVT PtrTy = getPointerTy(DAG.getDataLayout()); | |||
3981 | Register Reg = | |||
3982 | MF.getRegInfo().createVirtualRegister(getRegClassFor(PtrTy)); | |||
3983 | FuncInfo->setSRetReturnReg(Reg); | |||
3984 | SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[I]); | |||
3985 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain); | |||
3986 | break; | |||
3987 | } | |||
3988 | } | |||
3989 | ||||
3990 | unsigned StackSize = CCInfo.getNextStackOffset(); | |||
3991 | // Align stack specially for tail calls. | |||
3992 | if (shouldGuaranteeTCO(CallConv, | |||
3993 | MF.getTarget().Options.GuaranteedTailCallOpt)) | |||
3994 | StackSize = GetAlignedArgumentStackSize(StackSize, DAG); | |||
3995 | ||||
3996 | if (IsVarArg) | |||
3997 | VarArgsLoweringHelper(FuncInfo, dl, DAG, Subtarget, CallConv, CCInfo) | |||
3998 | .lowerVarArgsParameters(Chain, StackSize); | |||
3999 | ||||
4000 | // Some CCs need callee pop. | |||
4001 | if (X86::isCalleePop(CallConv, Is64Bit, IsVarArg, | |||
4002 | MF.getTarget().Options.GuaranteedTailCallOpt)) { | |||
4003 | FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything. | |||
4004 | } else if (CallConv == CallingConv::X86_INTR && Ins.size() == 2) { | |||
4005 | // X86 interrupts must pop the error code (and the alignment padding) if | |||
4006 | // present. | |||
4007 | FuncInfo->setBytesToPopOnReturn(Is64Bit ? 16 : 4); | |||
4008 | } else { | |||
4009 | FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing. | |||
4010 | // If this is an sret function, the return should pop the hidden pointer. | |||
4011 | if (!canGuaranteeTCO(CallConv) && hasCalleePopSRet(Ins, Subtarget)) | |||
4012 | FuncInfo->setBytesToPopOnReturn(4); | |||
4013 | } | |||
4014 | ||||
4015 | if (!Is64Bit) { | |||
4016 | // RegSaveFrameIndex is X86-64 only. | |||
4017 | FuncInfo->setRegSaveFrameIndex(0xAAAAAAA); | |||
4018 | } | |||
4019 | ||||
4020 | FuncInfo->setArgumentStackSize(StackSize); | |||
4021 | ||||
4022 | if (WinEHFuncInfo *EHInfo = MF.getWinEHFuncInfo()) { | |||
4023 | EHPersonality Personality = classifyEHPersonality(F.getPersonalityFn()); | |||
4024 | if (Personality == EHPersonality::CoreCLR) { | |||
4025 | assert(Is64Bit)(static_cast <bool> (Is64Bit) ? void (0) : __assert_fail ("Is64Bit", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 4025, __extension__ __PRETTY_FUNCTION__)); | |||
4026 | // TODO: Add a mechanism to frame lowering that will allow us to indicate | |||
4027 | // that we'd prefer this slot be allocated towards the bottom of the frame | |||
4028 | // (i.e. near the stack pointer after allocating the frame). Every | |||
4029 | // funclet needs a copy of this slot in its (mostly empty) frame, and the | |||
4030 | // offset from the bottom of this and each funclet's frame must be the | |||
4031 | // same, so the size of funclets' (mostly empty) frames is dictated by | |||
4032 | // how far this slot is from the bottom (since they allocate just enough | |||
4033 | // space to accommodate holding this slot at the correct offset). | |||
4034 | int PSPSymFI = MFI.CreateStackObject(8, Align(8), /*isSpillSlot=*/false); | |||
4035 | EHInfo->PSPSymFrameIdx = PSPSymFI; | |||
4036 | } | |||
4037 | } | |||
4038 | ||||
4039 | if (CallConv == CallingConv::X86_RegCall || | |||
4040 | F.hasFnAttribute("no_caller_saved_registers")) { | |||
4041 | MachineRegisterInfo &MRI = MF.getRegInfo(); | |||
4042 | for (std::pair<Register, Register> Pair : MRI.liveins()) | |||
4043 | MRI.disableCalleeSavedRegister(Pair.first); | |||
4044 | } | |||
4045 | ||||
4046 | return Chain; | |||
4047 | } | |||
4048 | ||||
4049 | SDValue X86TargetLowering::LowerMemOpCallTo(SDValue Chain, SDValue StackPtr, | |||
4050 | SDValue Arg, const SDLoc &dl, | |||
4051 | SelectionDAG &DAG, | |||
4052 | const CCValAssign &VA, | |||
4053 | ISD::ArgFlagsTy Flags, | |||
4054 | bool isByVal) const { | |||
4055 | unsigned LocMemOffset = VA.getLocMemOffset(); | |||
4056 | SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl); | |||
4057 | PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(DAG.getDataLayout()), | |||
4058 | StackPtr, PtrOff); | |||
4059 | if (isByVal) | |||
4060 | return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl); | |||
4061 | ||||
4062 | return DAG.getStore( | |||
4063 | Chain, dl, Arg, PtrOff, | |||
4064 | MachinePointerInfo::getStack(DAG.getMachineFunction(), LocMemOffset)); | |||
4065 | } | |||
4066 | ||||
4067 | /// Emit a load of return address if tail call | |||
4068 | /// optimization is performed and it is required. | |||
4069 | SDValue X86TargetLowering::EmitTailCallLoadRetAddr( | |||
4070 | SelectionDAG &DAG, SDValue &OutRetAddr, SDValue Chain, bool IsTailCall, | |||
4071 | bool Is64Bit, int FPDiff, const SDLoc &dl) const { | |||
4072 | // Adjust the Return address stack slot. | |||
4073 | EVT VT = getPointerTy(DAG.getDataLayout()); | |||
4074 | OutRetAddr = getReturnAddressFrameIndex(DAG); | |||
4075 | ||||
4076 | // Load the "old" Return address. | |||
4077 | OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo()); | |||
4078 | return SDValue(OutRetAddr.getNode(), 1); | |||
4079 | } | |||
4080 | ||||
4081 | /// Emit a store of the return address if tail call | |||
4082 | /// optimization is performed and it is required (FPDiff!=0). | |||
4083 | static SDValue EmitTailCallStoreRetAddr(SelectionDAG &DAG, MachineFunction &MF, | |||
4084 | SDValue Chain, SDValue RetAddrFrIdx, | |||
4085 | EVT PtrVT, unsigned SlotSize, | |||
4086 | int FPDiff, const SDLoc &dl) { | |||
4087 | // Store the return address to the appropriate stack slot. | |||
4088 | if (!FPDiff) return Chain; | |||
4089 | // Calculate the new stack slot for the return address. | |||
4090 | int NewReturnAddrFI = | |||
4091 | MF.getFrameInfo().CreateFixedObject(SlotSize, (int64_t)FPDiff - SlotSize, | |||
4092 | false); | |||
4093 | SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, PtrVT); | |||
4094 | Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx, | |||
4095 | MachinePointerInfo::getFixedStack( | |||
4096 | DAG.getMachineFunction(), NewReturnAddrFI)); | |||
4097 | return Chain; | |||
4098 | } | |||
4099 | ||||
4100 | /// Returns a vector_shuffle mask for an movs{s|d}, movd | |||
4101 | /// operation of specified width. | |||
4102 | static SDValue getMOVL(SelectionDAG &DAG, const SDLoc &dl, MVT VT, SDValue V1, | |||
4103 | SDValue V2) { | |||
4104 | unsigned NumElems = VT.getVectorNumElements(); | |||
4105 | SmallVector<int, 8> Mask; | |||
4106 | Mask.push_back(NumElems); | |||
4107 | for (unsigned i = 1; i != NumElems; ++i) | |||
4108 | Mask.push_back(i); | |||
4109 | return DAG.getVectorShuffle(VT, dl, V1, V2, Mask); | |||
4110 | } | |||
4111 | ||||
4112 | SDValue | |||
4113 | X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI, | |||
4114 | SmallVectorImpl<SDValue> &InVals) const { | |||
4115 | SelectionDAG &DAG = CLI.DAG; | |||
4116 | SDLoc &dl = CLI.DL; | |||
4117 | SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs; | |||
4118 | SmallVectorImpl<SDValue> &OutVals = CLI.OutVals; | |||
4119 | SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins; | |||
4120 | SDValue Chain = CLI.Chain; | |||
4121 | SDValue Callee = CLI.Callee; | |||
4122 | CallingConv::ID CallConv = CLI.CallConv; | |||
4123 | bool &isTailCall = CLI.IsTailCall; | |||
4124 | bool isVarArg = CLI.IsVarArg; | |||
4125 | const auto *CB = CLI.CB; | |||
4126 | ||||
4127 | MachineFunction &MF = DAG.getMachineFunction(); | |||
4128 | bool Is64Bit = Subtarget.is64Bit(); | |||
4129 | bool IsWin64 = Subtarget.isCallingConvWin64(CallConv); | |||
4130 | bool IsSibcall = false; | |||
4131 | bool IsGuaranteeTCO = MF.getTarget().Options.GuaranteedTailCallOpt || | |||
4132 | CallConv == CallingConv::Tail || CallConv == CallingConv::SwiftTail; | |||
4133 | bool IsCalleePopSRet = !IsGuaranteeTCO && hasCalleePopSRet(Outs, Subtarget); | |||
4134 | X86MachineFunctionInfo *X86Info = MF.getInfo<X86MachineFunctionInfo>(); | |||
4135 | bool HasNCSR = (CB && isa<CallInst>(CB) && | |||
4136 | CB->hasFnAttr("no_caller_saved_registers")); | |||
4137 | bool HasNoCfCheck = (CB && CB->doesNoCfCheck()); | |||
4138 | bool IsIndirectCall = (CB && isa<CallInst>(CB) && CB->isIndirectCall()); | |||
4139 | const Module *M = MF.getMMI().getModule(); | |||
4140 | Metadata *IsCFProtectionSupported = M->getModuleFlag("cf-protection-branch"); | |||
4141 | ||||
4142 | MachineFunction::CallSiteInfo CSInfo; | |||
4143 | if (CallConv == CallingConv::X86_INTR) | |||
4144 | report_fatal_error("X86 interrupts may not be called directly"); | |||
4145 | ||||
4146 | bool IsMustTail = CLI.CB && CLI.CB->isMustTailCall(); | |||
4147 | if (Subtarget.isPICStyleGOT() && !IsGuaranteeTCO && !IsMustTail) { | |||
4148 | // If we are using a GOT, disable tail calls to external symbols with | |||
4149 | // default visibility. Tail calling such a symbol requires using a GOT | |||
4150 | // relocation, which forces early binding of the symbol. This breaks code | |||
4151 | // that require lazy function symbol resolution. Using musttail or | |||
4152 | // GuaranteedTailCallOpt will override this. | |||
4153 | GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee); | |||
4154 | if (!G || (!G->getGlobal()->hasLocalLinkage() && | |||
4155 | G->getGlobal()->hasDefaultVisibility())) | |||
4156 | isTailCall = false; | |||
4157 | } | |||
4158 | ||||
4159 | if (isTailCall && !IsMustTail) { | |||
4160 | // Check if it's really possible to do a tail call. | |||
4161 | isTailCall = IsEligibleForTailCallOptimization( | |||
4162 | Callee, CallConv, IsCalleePopSRet, isVarArg, CLI.RetTy, Outs, OutVals, | |||
4163 | Ins, DAG); | |||
4164 | ||||
4165 | // Sibcalls are automatically detected tailcalls which do not require | |||
4166 | // ABI changes. | |||
4167 | if (!IsGuaranteeTCO && isTailCall) | |||
4168 | IsSibcall = true; | |||
4169 | ||||
4170 | if (isTailCall) | |||
4171 | ++NumTailCalls; | |||
4172 | } | |||
4173 | ||||
4174 | if (IsMustTail && !isTailCall) | |||
4175 | report_fatal_error("failed to perform tail call elimination on a call " | |||
4176 | "site marked musttail"); | |||
4177 | ||||
4178 | assert(!(isVarArg && canGuaranteeTCO(CallConv)) &&(static_cast <bool> (!(isVarArg && canGuaranteeTCO (CallConv)) && "Var args not supported with calling convention fastcc, ghc or hipe" ) ? void (0) : __assert_fail ("!(isVarArg && canGuaranteeTCO(CallConv)) && \"Var args not supported with calling convention fastcc, ghc or hipe\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 4179, __extension__ __PRETTY_FUNCTION__)) | |||
4179 | "Var args not supported with calling convention fastcc, ghc or hipe")(static_cast <bool> (!(isVarArg && canGuaranteeTCO (CallConv)) && "Var args not supported with calling convention fastcc, ghc or hipe" ) ? void (0) : __assert_fail ("!(isVarArg && canGuaranteeTCO(CallConv)) && \"Var args not supported with calling convention fastcc, ghc or hipe\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 4179, __extension__ __PRETTY_FUNCTION__)); | |||
4180 | ||||
4181 | // Analyze operands of the call, assigning locations to each operand. | |||
4182 | SmallVector<CCValAssign, 16> ArgLocs; | |||
4183 | CCState CCInfo(CallConv, isVarArg, MF, ArgLocs, *DAG.getContext()); | |||
4184 | ||||
4185 | // Allocate shadow area for Win64. | |||
4186 | if (IsWin64) | |||
4187 | CCInfo.AllocateStack(32, Align(8)); | |||
4188 | ||||
4189 | CCInfo.AnalyzeArguments(Outs, CC_X86); | |||
4190 | ||||
4191 | // In vectorcall calling convention a second pass is required for the HVA | |||
4192 | // types. | |||
4193 | if (CallingConv::X86_VectorCall == CallConv) { | |||
4194 | CCInfo.AnalyzeArgumentsSecondPass(Outs, CC_X86); | |||
4195 | } | |||
4196 | ||||
4197 | // Get a count of how many bytes are to be pushed on the stack. | |||
4198 | unsigned NumBytes = CCInfo.getAlignedCallFrameSize(); | |||
4199 | if (IsSibcall) | |||
4200 | // This is a sibcall. The memory operands are available in caller's | |||
4201 | // own caller's stack. | |||
4202 | NumBytes = 0; | |||
4203 | else if (IsGuaranteeTCO && canGuaranteeTCO(CallConv)) | |||
4204 | NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG); | |||
4205 | ||||
4206 | int FPDiff = 0; | |||
4207 | if (isTailCall && | |||
4208 | shouldGuaranteeTCO(CallConv, | |||
4209 | MF.getTarget().Options.GuaranteedTailCallOpt)) { | |||
4210 | // Lower arguments at fp - stackoffset + fpdiff. | |||
4211 | unsigned NumBytesCallerPushed = X86Info->getBytesToPopOnReturn(); | |||
4212 | ||||
4213 | FPDiff = NumBytesCallerPushed - NumBytes; | |||
4214 | ||||
4215 | // Set the delta of movement of the returnaddr stackslot. | |||
4216 | // But only set if delta is greater than previous delta. | |||
4217 | if (FPDiff < X86Info->getTCReturnAddrDelta()) | |||
4218 | X86Info->setTCReturnAddrDelta(FPDiff); | |||
4219 | } | |||
4220 | ||||
4221 | unsigned NumBytesToPush = NumBytes; | |||
4222 | unsigned NumBytesToPop = NumBytes; | |||
4223 | ||||
4224 | // If we have an inalloca argument, all stack space has already been allocated | |||
4225 | // for us and be right at the top of the stack. We don't support multiple | |||
4226 | // arguments passed in memory when using inalloca. | |||
4227 | if (!Outs.empty() && Outs.back().Flags.isInAlloca()) { | |||
4228 | NumBytesToPush = 0; | |||
4229 | if (!ArgLocs.back().isMemLoc()) | |||
4230 | report_fatal_error("cannot use inalloca attribute on a register " | |||
4231 | "parameter"); | |||
4232 | if (ArgLocs.back().getLocMemOffset() != 0) | |||
4233 | report_fatal_error("any parameter with the inalloca attribute must be " | |||
4234 | "the only memory argument"); | |||
4235 | } else if (CLI.IsPreallocated) { | |||
4236 | assert(ArgLocs.back().isMemLoc() &&(static_cast <bool> (ArgLocs.back().isMemLoc() && "cannot use preallocated attribute on a register " "parameter" ) ? void (0) : __assert_fail ("ArgLocs.back().isMemLoc() && \"cannot use preallocated attribute on a register \" \"parameter\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 4238, __extension__ __PRETTY_FUNCTION__)) | |||
4237 | "cannot use preallocated attribute on a register "(static_cast <bool> (ArgLocs.back().isMemLoc() && "cannot use preallocated attribute on a register " "parameter" ) ? void (0) : __assert_fail ("ArgLocs.back().isMemLoc() && \"cannot use preallocated attribute on a register \" \"parameter\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 4238, __extension__ __PRETTY_FUNCTION__)) | |||
4238 | "parameter")(static_cast <bool> (ArgLocs.back().isMemLoc() && "cannot use preallocated attribute on a register " "parameter" ) ? void (0) : __assert_fail ("ArgLocs.back().isMemLoc() && \"cannot use preallocated attribute on a register \" \"parameter\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 4238, __extension__ __PRETTY_FUNCTION__)); | |||
4239 | SmallVector<size_t, 4> PreallocatedOffsets; | |||
4240 | for (size_t i = 0; i < CLI.OutVals.size(); ++i) { | |||
4241 | if (CLI.CB->paramHasAttr(i, Attribute::Preallocated)) { | |||
4242 | PreallocatedOffsets.push_back(ArgLocs[i].getLocMemOffset()); | |||
4243 | } | |||
4244 | } | |||
4245 | auto *MFI = DAG.getMachineFunction().getInfo<X86MachineFunctionInfo>(); | |||
4246 | size_t PreallocatedId = MFI->getPreallocatedIdForCallSite(CLI.CB); | |||
4247 | MFI->setPreallocatedStackSize(PreallocatedId, NumBytes); | |||
4248 | MFI->setPreallocatedArgOffsets(PreallocatedId, PreallocatedOffsets); | |||
4249 | NumBytesToPush = 0; | |||
4250 | } | |||
4251 | ||||
4252 | if (!IsSibcall && !IsMustTail) | |||
4253 | Chain = DAG.getCALLSEQ_START(Chain, NumBytesToPush, | |||
4254 | NumBytes - NumBytesToPush, dl); | |||
4255 | ||||
4256 | SDValue RetAddrFrIdx; | |||
4257 | // Load return address for tail calls. | |||
4258 | if (isTailCall && FPDiff) | |||
4259 | Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall, | |||
4260 | Is64Bit, FPDiff, dl); | |||
4261 | ||||
4262 | SmallVector<std::pair<Register, SDValue>, 8> RegsToPass; | |||
4263 | SmallVector<SDValue, 8> MemOpChains; | |||
4264 | SDValue StackPtr; | |||
4265 | ||||
4266 | // The next loop assumes that the locations are in the same order of the | |||
4267 | // input arguments. | |||
4268 | assert(isSortedByValueNo(ArgLocs) &&(static_cast <bool> (isSortedByValueNo(ArgLocs) && "Argument Location list must be sorted before lowering") ? void (0) : __assert_fail ("isSortedByValueNo(ArgLocs) && \"Argument Location list must be sorted before lowering\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 4269, __extension__ __PRETTY_FUNCTION__)) | |||
4269 | "Argument Location list must be sorted before lowering")(static_cast <bool> (isSortedByValueNo(ArgLocs) && "Argument Location list must be sorted before lowering") ? void (0) : __assert_fail ("isSortedByValueNo(ArgLocs) && \"Argument Location list must be sorted before lowering\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 4269, __extension__ __PRETTY_FUNCTION__)); | |||
4270 | ||||
4271 | // Walk the register/memloc assignments, inserting copies/loads. In the case | |||
4272 | // of tail call optimization arguments are handle later. | |||
4273 | const X86RegisterInfo *RegInfo = Subtarget.getRegisterInfo(); | |||
4274 | for (unsigned I = 0, OutIndex = 0, E = ArgLocs.size(); I != E; | |||
4275 | ++I, ++OutIndex) { | |||
4276 | assert(OutIndex < Outs.size() && "Invalid Out index")(static_cast <bool> (OutIndex < Outs.size() && "Invalid Out index") ? void (0) : __assert_fail ("OutIndex < Outs.size() && \"Invalid Out index\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 4276, __extension__ __PRETTY_FUNCTION__)); | |||
4277 | // Skip inalloca/preallocated arguments, they have already been written. | |||
4278 | ISD::ArgFlagsTy Flags = Outs[OutIndex].Flags; | |||
4279 | if (Flags.isInAlloca() || Flags.isPreallocated()) | |||
4280 | continue; | |||
4281 | ||||
4282 | CCValAssign &VA = ArgLocs[I]; | |||
4283 | EVT RegVT = VA.getLocVT(); | |||
4284 | SDValue Arg = OutVals[OutIndex]; | |||
4285 | bool isByVal = Flags.isByVal(); | |||
4286 | ||||
4287 | // Promote the value if needed. | |||
4288 | switch (VA.getLocInfo()) { | |||
4289 | default: llvm_unreachable("Unknown loc info!")::llvm::llvm_unreachable_internal("Unknown loc info!", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 4289); | |||
4290 | case CCValAssign::Full: break; | |||
4291 | case CCValAssign::SExt: | |||
4292 | Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg); | |||
4293 | break; | |||
4294 | case CCValAssign::ZExt: | |||
4295 | Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg); | |||
4296 | break; | |||
4297 | case CCValAssign::AExt: | |||
4298 | if (Arg.getValueType().isVector() && | |||
4299 | Arg.getValueType().getVectorElementType() == MVT::i1) | |||
4300 | Arg = lowerMasksToReg(Arg, RegVT, dl, DAG); | |||
4301 | else if (RegVT.is128BitVector()) { | |||
4302 | // Special case: passing MMX values in XMM registers. | |||
4303 | Arg = DAG.getBitcast(MVT::i64, Arg); | |||
4304 | Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg); | |||
4305 | Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg); | |||
4306 | } else | |||
4307 | Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg); | |||
4308 | break; | |||
4309 | case CCValAssign::BCvt: | |||
4310 | Arg = DAG.getBitcast(RegVT, Arg); | |||
4311 | break; | |||
4312 | case CCValAssign::Indirect: { | |||
4313 | if (isByVal) { | |||
4314 | // Memcpy the argument to a temporary stack slot to prevent | |||
4315 | // the caller from seeing any modifications the callee may make | |||
4316 | // as guaranteed by the `byval` attribute. | |||
4317 | int FrameIdx = MF.getFrameInfo().CreateStackObject( | |||
4318 | Flags.getByValSize(), | |||
4319 | std::max(Align(16), Flags.getNonZeroByValAlign()), false); | |||
4320 | SDValue StackSlot = | |||
4321 | DAG.getFrameIndex(FrameIdx, getPointerTy(DAG.getDataLayout())); | |||
4322 | Chain = | |||
4323 | CreateCopyOfByValArgument(Arg, StackSlot, Chain, Flags, DAG, dl); | |||
4324 | // From now on treat this as a regular pointer | |||
4325 | Arg = StackSlot; | |||
4326 | isByVal = false; | |||
4327 | } else { | |||
4328 | // Store the argument. | |||
4329 | SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT()); | |||
4330 | int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex(); | |||
4331 | Chain = DAG.getStore( | |||
4332 | Chain, dl, Arg, SpillSlot, | |||
4333 | MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI)); | |||
4334 | Arg = SpillSlot; | |||
4335 | } | |||
4336 | break; | |||
4337 | } | |||
4338 | } | |||
4339 | ||||
4340 | if (VA.needsCustom()) { | |||
4341 | assert(VA.getValVT() == MVT::v64i1 &&(static_cast <bool> (VA.getValVT() == MVT::v64i1 && "Currently the only custom case is when we split v64i1 to 2 regs" ) ? void (0) : __assert_fail ("VA.getValVT() == MVT::v64i1 && \"Currently the only custom case is when we split v64i1 to 2 regs\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 4342, __extension__ __PRETTY_FUNCTION__)) | |||
4342 | "Currently the only custom case is when we split v64i1 to 2 regs")(static_cast <bool> (VA.getValVT() == MVT::v64i1 && "Currently the only custom case is when we split v64i1 to 2 regs" ) ? void (0) : __assert_fail ("VA.getValVT() == MVT::v64i1 && \"Currently the only custom case is when we split v64i1 to 2 regs\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 4342, __extension__ __PRETTY_FUNCTION__)); | |||
4343 | // Split v64i1 value into two registers | |||
4344 | Passv64i1ArgInRegs(dl, DAG, Arg, RegsToPass, VA, ArgLocs[++I], Subtarget); | |||
4345 | } else if (VA.isRegLoc()) { | |||
4346 | RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg)); | |||
4347 | const TargetOptions &Options = DAG.getTarget().Options; | |||
4348 | if (Options.EmitCallSiteInfo) | |||
4349 | CSInfo.emplace_back(VA.getLocReg(), I); | |||
4350 | if (isVarArg && IsWin64) { | |||
4351 | // Win64 ABI requires argument XMM reg to be copied to the corresponding | |||
4352 | // shadow reg if callee is a varargs function. | |||
4353 | Register ShadowReg; | |||
4354 | switch (VA.getLocReg()) { | |||
4355 | case X86::XMM0: ShadowReg = X86::RCX; break; | |||
4356 | case X86::XMM1: ShadowReg = X86::RDX; break; | |||
4357 | case X86::XMM2: ShadowReg = X86::R8; break; | |||
4358 | case X86::XMM3: ShadowReg = X86::R9; break; | |||
4359 | } | |||
4360 | if (ShadowReg) | |||
4361 | RegsToPass.push_back(std::make_pair(ShadowReg, Arg)); | |||
4362 | } | |||
4363 | } else if (!IsSibcall && (!isTailCall || isByVal)) { | |||
4364 | assert(VA.isMemLoc())(static_cast <bool> (VA.isMemLoc()) ? void (0) : __assert_fail ("VA.isMemLoc()", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 4364, __extension__ __PRETTY_FUNCTION__)); | |||
4365 | if (!StackPtr.getNode()) | |||
4366 | StackPtr = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(), | |||
4367 | getPointerTy(DAG.getDataLayout())); | |||
4368 | MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg, | |||
4369 | dl, DAG, VA, Flags, isByVal)); | |||
4370 | } | |||
4371 | } | |||
4372 | ||||
4373 | if (!MemOpChains.empty()) | |||
4374 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains); | |||
4375 | ||||
4376 | if (Subtarget.isPICStyleGOT()) { | |||
4377 | // ELF / PIC requires GOT in the EBX register before function calls via PLT | |||
4378 | // GOT pointer (except regcall). | |||
4379 | if (!isTailCall) { | |||
4380 | // Indirect call with RegCall calling convertion may use up all the | |||
4381 | // general registers, so it is not suitable to bind EBX reister for | |||
4382 | // GOT address, just let register allocator handle it. | |||
4383 | if (CallConv != CallingConv::X86_RegCall) | |||
4384 | RegsToPass.push_back(std::make_pair( | |||
4385 | Register(X86::EBX), DAG.getNode(X86ISD::GlobalBaseReg, SDLoc(), | |||
4386 | getPointerTy(DAG.getDataLayout())))); | |||
4387 | } else { | |||
4388 | // If we are tail calling and generating PIC/GOT style code load the | |||
4389 | // address of the callee into ECX. The value in ecx is used as target of | |||
4390 | // the tail jump. This is done to circumvent the ebx/callee-saved problem | |||
4391 | // for tail calls on PIC/GOT architectures. Normally we would just put the | |||
4392 | // address of GOT into ebx and then call target@PLT. But for tail calls | |||
4393 | // ebx would be restored (since ebx is callee saved) before jumping to the | |||
4394 | // target@PLT. | |||
4395 | ||||
4396 | // Note: The actual moving to ECX is done further down. | |||
4397 | GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee); | |||
4398 | if (G && !G->getGlobal()->hasLocalLinkage() && | |||
4399 | G->getGlobal()->hasDefaultVisibility()) | |||
4400 | Callee = LowerGlobalAddress(Callee, DAG); | |||
4401 | else if (isa<ExternalSymbolSDNode>(Callee)) | |||
4402 | Callee = LowerExternalSymbol(Callee, DAG); | |||
4403 | } | |||
4404 | } | |||
4405 | ||||
4406 | if (Is64Bit && isVarArg && !IsWin64 && !IsMustTail) { | |||
4407 | // From AMD64 ABI document: | |||
4408 | // For calls that may call functions that use varargs or stdargs | |||
4409 | // (prototype-less calls or calls to functions containing ellipsis (...) in | |||
4410 | // the declaration) %al is used as hidden argument to specify the number | |||
4411 | // of SSE registers used. The contents of %al do not need to match exactly | |||
4412 | // the number of registers, but must be an ubound on the number of SSE | |||
4413 | // registers used and is in the range 0 - 8 inclusive. | |||
4414 | ||||
4415 | // Count the number of XMM registers allocated. | |||
4416 | static const MCPhysReg XMMArgRegs[] = { | |||
4417 | X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3, | |||
4418 | X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7 | |||
4419 | }; | |||
4420 | unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs); | |||
4421 | assert((Subtarget.hasSSE1() || !NumXMMRegs)(static_cast <bool> ((Subtarget.hasSSE1() || !NumXMMRegs ) && "SSE registers cannot be used when SSE is disabled" ) ? void (0) : __assert_fail ("(Subtarget.hasSSE1() || !NumXMMRegs) && \"SSE registers cannot be used when SSE is disabled\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 4422, __extension__ __PRETTY_FUNCTION__)) | |||
4422 | && "SSE registers cannot be used when SSE is disabled")(static_cast <bool> ((Subtarget.hasSSE1() || !NumXMMRegs ) && "SSE registers cannot be used when SSE is disabled" ) ? void (0) : __assert_fail ("(Subtarget.hasSSE1() || !NumXMMRegs) && \"SSE registers cannot be used when SSE is disabled\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 4422, __extension__ __PRETTY_FUNCTION__)); | |||
4423 | RegsToPass.push_back(std::make_pair(Register(X86::AL), | |||
4424 | DAG.getConstant(NumXMMRegs, dl, | |||
4425 | MVT::i8))); | |||
4426 | } | |||
4427 | ||||
4428 | if (isVarArg && IsMustTail) { | |||
4429 | const auto &Forwards = X86Info->getForwardedMustTailRegParms(); | |||
4430 | for (const auto &F : Forwards) { | |||
4431 | SDValue Val = DAG.getCopyFromReg(Chain, dl, F.VReg, F.VT); | |||
4432 | RegsToPass.push_back(std::make_pair(F.PReg, Val)); | |||
4433 | } | |||
4434 | } | |||
4435 | ||||
4436 | // For tail calls lower the arguments to the 'real' stack slots. Sibcalls | |||
4437 | // don't need this because the eligibility check rejects calls that require | |||
4438 | // shuffling arguments passed in memory. | |||
4439 | if (!IsSibcall && isTailCall) { | |||
4440 | // Force all the incoming stack arguments to be loaded from the stack | |||
4441 | // before any new outgoing arguments are stored to the stack, because the | |||
4442 | // outgoing stack slots may alias the incoming argument stack slots, and | |||
4443 | // the alias isn't otherwise explicit. This is slightly more conservative | |||
4444 | // than necessary, because it means that each store effectively depends | |||
4445 | // on every argument instead of just those arguments it would clobber. | |||
4446 | SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain); | |||
4447 | ||||
4448 | SmallVector<SDValue, 8> MemOpChains2; | |||
4449 | SDValue FIN; | |||
4450 | int FI = 0; | |||
4451 | for (unsigned I = 0, OutsIndex = 0, E = ArgLocs.size(); I != E; | |||
4452 | ++I, ++OutsIndex) { | |||
4453 | CCValAssign &VA = ArgLocs[I]; | |||
4454 | ||||
4455 | if (VA.isRegLoc()) { | |||
4456 | if (VA.needsCustom()) { | |||
4457 | assert((CallConv == CallingConv::X86_RegCall) &&(static_cast <bool> ((CallConv == CallingConv::X86_RegCall ) && "Expecting custom case only in regcall calling convention" ) ? void (0) : __assert_fail ("(CallConv == CallingConv::X86_RegCall) && \"Expecting custom case only in regcall calling convention\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 4458, __extension__ __PRETTY_FUNCTION__)) | |||
4458 | "Expecting custom case only in regcall calling convention")(static_cast <bool> ((CallConv == CallingConv::X86_RegCall ) && "Expecting custom case only in regcall calling convention" ) ? void (0) : __assert_fail ("(CallConv == CallingConv::X86_RegCall) && \"Expecting custom case only in regcall calling convention\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 4458, __extension__ __PRETTY_FUNCTION__)); | |||
4459 | // This means that we are in special case where one argument was | |||
4460 | // passed through two register locations - Skip the next location | |||
4461 | ++I; | |||
4462 | } | |||
4463 | ||||
4464 | continue; | |||
4465 | } | |||
4466 | ||||
4467 | assert(VA.isMemLoc())(static_cast <bool> (VA.isMemLoc()) ? void (0) : __assert_fail ("VA.isMemLoc()", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 4467, __extension__ __PRETTY_FUNCTION__)); | |||
4468 | SDValue Arg = OutVals[OutsIndex]; | |||
4469 | ISD::ArgFlagsTy Flags = Outs[OutsIndex].Flags; | |||
4470 | // Skip inalloca/preallocated arguments. They don't require any work. | |||
4471 | if (Flags.isInAlloca() || Flags.isPreallocated()) | |||
4472 | continue; | |||
4473 | // Create frame index. | |||
4474 | int32_t Offset = VA.getLocMemOffset()+FPDiff; | |||
4475 | uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8; | |||
4476 | FI = MF.getFrameInfo().CreateFixedObject(OpSize, Offset, true); | |||
4477 | FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout())); | |||
4478 | ||||
4479 | if (Flags.isByVal()) { | |||
4480 | // Copy relative to framepointer. | |||
4481 | SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset(), dl); | |||
4482 | if (!StackPtr.getNode()) | |||
4483 | StackPtr = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(), | |||
4484 | getPointerTy(DAG.getDataLayout())); | |||
4485 | Source = DAG.getNode(ISD::ADD, dl, getPointerTy(DAG.getDataLayout()), | |||
4486 | StackPtr, Source); | |||
4487 | ||||
4488 | MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, | |||
4489 | ArgChain, | |||
4490 | Flags, DAG, dl)); | |||
4491 | } else { | |||
4492 | // Store relative to framepointer. | |||
4493 | MemOpChains2.push_back(DAG.getStore( | |||
4494 | ArgChain, dl, Arg, FIN, | |||
4495 | MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI))); | |||
4496 | } | |||
4497 | } | |||
4498 | ||||
4499 | if (!MemOpChains2.empty()) | |||
4500 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains2); | |||
4501 | ||||
4502 | // Store the return address to the appropriate stack slot. | |||
4503 | Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, | |||
4504 | getPointerTy(DAG.getDataLayout()), | |||
4505 | RegInfo->getSlotSize(), FPDiff, dl); | |||
4506 | } | |||
4507 | ||||
4508 | // Build a sequence of copy-to-reg nodes chained together with token chain | |||
4509 | // and flag operands which copy the outgoing args into registers. | |||
4510 | SDValue InFlag; | |||
4511 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) { | |||
4512 | Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first, | |||
4513 | RegsToPass[i].second, InFlag); | |||
4514 | InFlag = Chain.getValue(1); | |||
4515 | } | |||
4516 | ||||
4517 | if (DAG.getTarget().getCodeModel() == CodeModel::Large) { | |||
4518 | assert(Is64Bit && "Large code model is only legal in 64-bit mode.")(static_cast <bool> (Is64Bit && "Large code model is only legal in 64-bit mode." ) ? void (0) : __assert_fail ("Is64Bit && \"Large code model is only legal in 64-bit mode.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 4518, __extension__ __PRETTY_FUNCTION__)); | |||
4519 | // In the 64-bit large code model, we have to make all calls | |||
4520 | // through a register, since the call instruction's 32-bit | |||
4521 | // pc-relative offset may not be large enough to hold the whole | |||
4522 | // address. | |||
4523 | } else if (Callee->getOpcode() == ISD::GlobalAddress || | |||
4524 | Callee->getOpcode() == ISD::ExternalSymbol) { | |||
4525 | // Lower direct calls to global addresses and external symbols. Setting | |||
4526 | // ForCall to true here has the effect of removing WrapperRIP when possible | |||
4527 | // to allow direct calls to be selected without first materializing the | |||
4528 | // address into a register. | |||
4529 | Callee = LowerGlobalOrExternal(Callee, DAG, /*ForCall=*/true); | |||
4530 | } else if (Subtarget.isTarget64BitILP32() && | |||
4531 | Callee->getValueType(0) == MVT::i32) { | |||
4532 | // Zero-extend the 32-bit Callee address into a 64-bit according to x32 ABI | |||
4533 | Callee = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, Callee); | |||
4534 | } | |||
4535 | ||||
4536 | // Returns a chain & a flag for retval copy to use. | |||
4537 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); | |||
4538 | SmallVector<SDValue, 8> Ops; | |||
4539 | ||||
4540 | if (!IsSibcall && isTailCall && !IsMustTail) { | |||
4541 | Chain = DAG.getCALLSEQ_END(Chain, | |||
4542 | DAG.getIntPtrConstant(NumBytesToPop, dl, true), | |||
4543 | DAG.getIntPtrConstant(0, dl, true), InFlag, dl); | |||
4544 | InFlag = Chain.getValue(1); | |||
4545 | } | |||
4546 | ||||
4547 | Ops.push_back(Chain); | |||
4548 | Ops.push_back(Callee); | |||
4549 | ||||
4550 | if (isTailCall) | |||
4551 | Ops.push_back(DAG.getTargetConstant(FPDiff, dl, MVT::i32)); | |||
4552 | ||||
4553 | // Add argument registers to the end of the list so that they are known live | |||
4554 | // into the call. | |||
4555 | for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) | |||
4556 | Ops.push_back(DAG.getRegister(RegsToPass[i].first, | |||
4557 | RegsToPass[i].second.getValueType())); | |||
4558 | ||||
4559 | // Add a register mask operand representing the call-preserved registers. | |||
4560 | const uint32_t *Mask = [&]() { | |||
4561 | auto AdaptedCC = CallConv; | |||
4562 | // If HasNCSR is asserted (attribute NoCallerSavedRegisters exists), | |||
4563 | // use X86_INTR calling convention because it has the same CSR mask | |||
4564 | // (same preserved registers). | |||
4565 | if (HasNCSR) | |||
4566 | AdaptedCC = (CallingConv::ID)CallingConv::X86_INTR; | |||
4567 | // If NoCalleeSavedRegisters is requested, than use GHC since it happens | |||
4568 | // to use the CSR_NoRegs_RegMask. | |||
4569 | if (CB && CB->hasFnAttr("no_callee_saved_registers")) | |||
4570 | AdaptedCC = (CallingConv::ID)CallingConv::GHC; | |||
4571 | return RegInfo->getCallPreservedMask(MF, AdaptedCC); | |||
4572 | }(); | |||
4573 | assert(Mask && "Missing call preserved mask for calling convention")(static_cast <bool> (Mask && "Missing call preserved mask for calling convention" ) ? void (0) : __assert_fail ("Mask && \"Missing call preserved mask for calling convention\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 4573, __extension__ __PRETTY_FUNCTION__)); | |||
4574 | ||||
4575 | // If this is an invoke in a 32-bit function using a funclet-based | |||
4576 | // personality, assume the function clobbers all registers. If an exception | |||
4577 | // is thrown, the runtime will not restore CSRs. | |||
4578 | // FIXME: Model this more precisely so that we can register allocate across | |||
4579 | // the normal edge and spill and fill across the exceptional edge. | |||
4580 | if (!Is64Bit && CLI.CB && isa<InvokeInst>(CLI.CB)) { | |||
4581 | const Function &CallerFn = MF.getFunction(); | |||
4582 | EHPersonality Pers = | |||
4583 | CallerFn.hasPersonalityFn() | |||
4584 | ? classifyEHPersonality(CallerFn.getPersonalityFn()) | |||
4585 | : EHPersonality::Unknown; | |||
4586 | if (isFuncletEHPersonality(Pers)) | |||
4587 | Mask = RegInfo->getNoPreservedMask(); | |||
4588 | } | |||
4589 | ||||
4590 | // Define a new register mask from the existing mask. | |||
4591 | uint32_t *RegMask = nullptr; | |||
4592 | ||||
4593 | // In some calling conventions we need to remove the used physical registers | |||
4594 | // from the reg mask. | |||
4595 | if (CallConv == CallingConv::X86_RegCall || HasNCSR) { | |||
4596 | const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo(); | |||
4597 | ||||
4598 | // Allocate a new Reg Mask and copy Mask. | |||
4599 | RegMask = MF.allocateRegMask(); | |||
4600 | unsigned RegMaskSize = MachineOperand::getRegMaskSize(TRI->getNumRegs()); | |||
4601 | memcpy(RegMask, Mask, sizeof(RegMask[0]) * RegMaskSize); | |||
4602 | ||||
4603 | // Make sure all sub registers of the argument registers are reset | |||
4604 | // in the RegMask. | |||
4605 | for (auto const &RegPair : RegsToPass) | |||
4606 | for (MCSubRegIterator SubRegs(RegPair.first, TRI, /*IncludeSelf=*/true); | |||
4607 | SubRegs.isValid(); ++SubRegs) | |||
4608 | RegMask[*SubRegs / 32] &= ~(1u << (*SubRegs % 32)); | |||
4609 | ||||
4610 | // Create the RegMask Operand according to our updated mask. | |||
4611 | Ops.push_back(DAG.getRegisterMask(RegMask)); | |||
4612 | } else { | |||
4613 | // Create the RegMask Operand according to the static mask. | |||
4614 | Ops.push_back(DAG.getRegisterMask(Mask)); | |||
4615 | } | |||
4616 | ||||
4617 | if (InFlag.getNode()) | |||
4618 | Ops.push_back(InFlag); | |||
4619 | ||||
4620 | if (isTailCall) { | |||
4621 | // We used to do: | |||
4622 | //// If this is the first return lowered for this function, add the regs | |||
4623 | //// to the liveout set for the function. | |||
4624 | // This isn't right, although it's probably harmless on x86; liveouts | |||
4625 | // should be computed from returns not tail calls. Consider a void | |||
4626 | // function making a tail call to a function returning int. | |||
4627 | MF.getFrameInfo().setHasTailCall(); | |||
4628 | SDValue Ret = DAG.getNode(X86ISD::TC_RETURN, dl, NodeTys, Ops); | |||
4629 | DAG.addCallSiteInfo(Ret.getNode(), std::move(CSInfo)); | |||
4630 | return Ret; | |||
4631 | } | |||
4632 | ||||
4633 | if (HasNoCfCheck && IsCFProtectionSupported && IsIndirectCall) { | |||
4634 | Chain = DAG.getNode(X86ISD::NT_CALL, dl, NodeTys, Ops); | |||
4635 | } else if (CLI.CB && objcarc::hasAttachedCallOpBundle(CLI.CB)) { | |||
4636 | // Calls with a "clang.arc.attachedcall" bundle are special. They should be | |||
4637 | // expanded to the call, directly followed by a special marker sequence and | |||
4638 | // a call to a ObjC library function. Use the CALL_RVMARKER to do that. | |||
4639 | assert(!isTailCall &&(static_cast <bool> (!isTailCall && "tail calls cannot be marked with clang.arc.attachedcall" ) ? void (0) : __assert_fail ("!isTailCall && \"tail calls cannot be marked with clang.arc.attachedcall\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 4640, __extension__ __PRETTY_FUNCTION__)) | |||
4640 | "tail calls cannot be marked with clang.arc.attachedcall")(static_cast <bool> (!isTailCall && "tail calls cannot be marked with clang.arc.attachedcall" ) ? void (0) : __assert_fail ("!isTailCall && \"tail calls cannot be marked with clang.arc.attachedcall\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 4640, __extension__ __PRETTY_FUNCTION__)); | |||
4641 | assert(Is64Bit && "clang.arc.attachedcall is only supported in 64bit mode")(static_cast <bool> (Is64Bit && "clang.arc.attachedcall is only supported in 64bit mode" ) ? void (0) : __assert_fail ("Is64Bit && \"clang.arc.attachedcall is only supported in 64bit mode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 4641, __extension__ __PRETTY_FUNCTION__)); | |||
4642 | ||||
4643 | // Add a target global address for the retainRV/claimRV runtime function | |||
4644 | // just before the call target. | |||
4645 | Function *ARCFn = *objcarc::getAttachedARCFunction(CLI.CB); | |||
4646 | auto PtrVT = getPointerTy(DAG.getDataLayout()); | |||
4647 | auto GA = DAG.getTargetGlobalAddress(ARCFn, dl, PtrVT); | |||
4648 | Ops.insert(Ops.begin() + 1, GA); | |||
4649 | Chain = DAG.getNode(X86ISD::CALL_RVMARKER, dl, NodeTys, Ops); | |||
4650 | } else { | |||
4651 | Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops); | |||
4652 | } | |||
4653 | ||||
4654 | InFlag = Chain.getValue(1); | |||
4655 | DAG.addNoMergeSiteInfo(Chain.getNode(), CLI.NoMerge); | |||
4656 | DAG.addCallSiteInfo(Chain.getNode(), std::move(CSInfo)); | |||
4657 | ||||
4658 | // Save heapallocsite metadata. | |||
4659 | if (CLI.CB) | |||
4660 | if (MDNode *HeapAlloc = CLI.CB->getMetadata("heapallocsite")) | |||
4661 | DAG.addHeapAllocSite(Chain.getNode(), HeapAlloc); | |||
4662 | ||||
4663 | // Create the CALLSEQ_END node. | |||
4664 | unsigned NumBytesForCalleeToPop = 0; // Callee pops nothing. | |||
4665 | if (X86::isCalleePop(CallConv, Is64Bit, isVarArg, | |||
4666 | DAG.getTarget().Options.GuaranteedTailCallOpt)) | |||
4667 | NumBytesForCalleeToPop = NumBytes; // Callee pops everything | |||
4668 | else if (!canGuaranteeTCO(CallConv) && IsCalleePopSRet) | |||
4669 | // If this call passes a struct-return pointer, the callee | |||
4670 | // pops that struct pointer. | |||
4671 | NumBytesForCalleeToPop = 4; | |||
4672 | ||||
4673 | // Returns a flag for retval copy to use. | |||
4674 | if (!IsSibcall) { | |||
4675 | Chain = DAG.getCALLSEQ_END(Chain, | |||
4676 | DAG.getIntPtrConstant(NumBytesToPop, dl, true), | |||
4677 | DAG.getIntPtrConstant(NumBytesForCalleeToPop, dl, | |||
4678 | true), | |||
4679 | InFlag, dl); | |||
4680 | InFlag = Chain.getValue(1); | |||
4681 | } | |||
4682 | ||||
4683 | // Handle result values, copying them out of physregs into vregs that we | |||
4684 | // return. | |||
4685 | return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl, DAG, | |||
4686 | InVals, RegMask); | |||
4687 | } | |||
4688 | ||||
4689 | //===----------------------------------------------------------------------===// | |||
4690 | // Fast Calling Convention (tail call) implementation | |||
4691 | //===----------------------------------------------------------------------===// | |||
4692 | ||||
4693 | // Like std call, callee cleans arguments, convention except that ECX is | |||
4694 | // reserved for storing the tail called function address. Only 2 registers are | |||
4695 | // free for argument passing (inreg). Tail call optimization is performed | |||
4696 | // provided: | |||
4697 | // * tailcallopt is enabled | |||
4698 | // * caller/callee are fastcc | |||
4699 | // On X86_64 architecture with GOT-style position independent code only local | |||
4700 | // (within module) calls are supported at the moment. | |||
4701 | // To keep the stack aligned according to platform abi the function | |||
4702 | // GetAlignedArgumentStackSize ensures that argument delta is always multiples | |||
4703 | // of stack alignment. (Dynamic linkers need this - Darwin's dyld for example) | |||
4704 | // If a tail called function callee has more arguments than the caller the | |||
4705 | // caller needs to make sure that there is room to move the RETADDR to. This is | |||
4706 | // achieved by reserving an area the size of the argument delta right after the | |||
4707 | // original RETADDR, but before the saved framepointer or the spilled registers | |||
4708 | // e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4) | |||
4709 | // stack layout: | |||
4710 | // arg1 | |||
4711 | // arg2 | |||
4712 | // RETADDR | |||
4713 | // [ new RETADDR | |||
4714 | // move area ] | |||
4715 | // (possible EBP) | |||
4716 | // ESI | |||
4717 | // EDI | |||
4718 | // local1 .. | |||
4719 | ||||
4720 | /// Make the stack size align e.g 16n + 12 aligned for a 16-byte align | |||
4721 | /// requirement. | |||
4722 | unsigned | |||
4723 | X86TargetLowering::GetAlignedArgumentStackSize(const unsigned StackSize, | |||
4724 | SelectionDAG &DAG) const { | |||
4725 | const Align StackAlignment = Subtarget.getFrameLowering()->getStackAlign(); | |||
4726 | const uint64_t SlotSize = Subtarget.getRegisterInfo()->getSlotSize(); | |||
4727 | assert(StackSize % SlotSize == 0 &&(static_cast <bool> (StackSize % SlotSize == 0 && "StackSize must be a multiple of SlotSize") ? void (0) : __assert_fail ("StackSize % SlotSize == 0 && \"StackSize must be a multiple of SlotSize\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 4728, __extension__ __PRETTY_FUNCTION__)) | |||
4728 | "StackSize must be a multiple of SlotSize")(static_cast <bool> (StackSize % SlotSize == 0 && "StackSize must be a multiple of SlotSize") ? void (0) : __assert_fail ("StackSize % SlotSize == 0 && \"StackSize must be a multiple of SlotSize\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 4728, __extension__ __PRETTY_FUNCTION__)); | |||
4729 | return alignTo(StackSize + SlotSize, StackAlignment) - SlotSize; | |||
4730 | } | |||
4731 | ||||
4732 | /// Return true if the given stack call argument is already available in the | |||
4733 | /// same position (relatively) of the caller's incoming argument stack. | |||
4734 | static | |||
4735 | bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags, | |||
4736 | MachineFrameInfo &MFI, const MachineRegisterInfo *MRI, | |||
4737 | const X86InstrInfo *TII, const CCValAssign &VA) { | |||
4738 | unsigned Bytes = Arg.getValueSizeInBits() / 8; | |||
4739 | ||||
4740 | for (;;) { | |||
4741 | // Look through nodes that don't alter the bits of the incoming value. | |||
4742 | unsigned Op = Arg.getOpcode(); | |||
4743 | if (Op == ISD::ZERO_EXTEND || Op == ISD::ANY_EXTEND || Op == ISD::BITCAST) { | |||
4744 | Arg = Arg.getOperand(0); | |||
4745 | continue; | |||
4746 | } | |||
4747 | if (Op == ISD::TRUNCATE) { | |||
4748 | const SDValue &TruncInput = Arg.getOperand(0); | |||
4749 | if (TruncInput.getOpcode() == ISD::AssertZext && | |||
4750 | cast<VTSDNode>(TruncInput.getOperand(1))->getVT() == | |||
4751 | Arg.getValueType()) { | |||
4752 | Arg = TruncInput.getOperand(0); | |||
4753 | continue; | |||
4754 | } | |||
4755 | } | |||
4756 | break; | |||
4757 | } | |||
4758 | ||||
4759 | int FI = INT_MAX2147483647; | |||
4760 | if (Arg.getOpcode() == ISD::CopyFromReg) { | |||
4761 | Register VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg(); | |||
4762 | if (!VR.isVirtual()) | |||
4763 | return false; | |||
4764 | MachineInstr *Def = MRI->getVRegDef(VR); | |||
4765 | if (!Def) | |||
4766 | return false; | |||
4767 | if (!Flags.isByVal()) { | |||
4768 | if (!TII->isLoadFromStackSlot(*Def, FI)) | |||
4769 | return false; | |||
4770 | } else { | |||
4771 | unsigned Opcode = Def->getOpcode(); | |||
4772 | if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r || | |||
4773 | Opcode == X86::LEA64_32r) && | |||
4774 | Def->getOperand(1).isFI()) { | |||
4775 | FI = Def->getOperand(1).getIndex(); | |||
4776 | Bytes = Flags.getByValSize(); | |||
4777 | } else | |||
4778 | return false; | |||
4779 | } | |||
4780 | } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) { | |||
4781 | if (Flags.isByVal()) | |||
4782 | // ByVal argument is passed in as a pointer but it's now being | |||
4783 | // dereferenced. e.g. | |||
4784 | // define @foo(%struct.X* %A) { | |||
4785 | // tail call @bar(%struct.X* byval %A) | |||
4786 | // } | |||
4787 | return false; | |||
4788 | SDValue Ptr = Ld->getBasePtr(); | |||
4789 | FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr); | |||
4790 | if (!FINode) | |||
4791 | return false; | |||
4792 | FI = FINode->getIndex(); | |||
4793 | } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) { | |||
4794 | FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg); | |||
4795 | FI = FINode->getIndex(); | |||
4796 | Bytes = Flags.getByValSize(); | |||
4797 | } else | |||
4798 | return false; | |||
4799 | ||||
4800 | assert(FI != INT_MAX)(static_cast <bool> (FI != 2147483647) ? void (0) : __assert_fail ("FI != INT_MAX", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 4800, __extension__ __PRETTY_FUNCTION__)); | |||
4801 | if (!MFI.isFixedObjectIndex(FI)) | |||
4802 | return false; | |||
4803 | ||||
4804 | if (Offset != MFI.getObjectOffset(FI)) | |||
4805 | return false; | |||
4806 | ||||
4807 | // If this is not byval, check that the argument stack object is immutable. | |||
4808 | // inalloca and argument copy elision can create mutable argument stack | |||
4809 | // objects. Byval objects can be mutated, but a byval call intends to pass the | |||
4810 | // mutated memory. | |||
4811 | if (!Flags.isByVal() && !MFI.isImmutableObjectIndex(FI)) | |||
4812 | return false; | |||
4813 | ||||
4814 | if (VA.getLocVT().getFixedSizeInBits() > | |||
4815 | Arg.getValueSizeInBits().getFixedSize()) { | |||
4816 | // If the argument location is wider than the argument type, check that any | |||
4817 | // extension flags match. | |||
4818 | if (Flags.isZExt() != MFI.isObjectZExt(FI) || | |||
4819 | Flags.isSExt() != MFI.isObjectSExt(FI)) { | |||
4820 | return false; | |||
4821 | } | |||
4822 | } | |||
4823 | ||||
4824 | return Bytes == MFI.getObjectSize(FI); | |||
4825 | } | |||
4826 | ||||
4827 | /// Check whether the call is eligible for tail call optimization. Targets | |||
4828 | /// that want to do tail call optimization should implement this function. | |||
4829 | bool X86TargetLowering::IsEligibleForTailCallOptimization( | |||
4830 | SDValue Callee, CallingConv::ID CalleeCC, bool IsCalleePopSRet, | |||
4831 | bool isVarArg, Type *RetTy, const SmallVectorImpl<ISD::OutputArg> &Outs, | |||
4832 | const SmallVectorImpl<SDValue> &OutVals, | |||
4833 | const SmallVectorImpl<ISD::InputArg> &Ins, SelectionDAG &DAG) const { | |||
4834 | if (!mayTailCallThisCC(CalleeCC)) | |||
4835 | return false; | |||
4836 | ||||
4837 | // If -tailcallopt is specified, make fastcc functions tail-callable. | |||
4838 | MachineFunction &MF = DAG.getMachineFunction(); | |||
4839 | const Function &CallerF = MF.getFunction(); | |||
4840 | ||||
4841 | // If the function return type is x86_fp80 and the callee return type is not, | |||
4842 | // then the FP_EXTEND of the call result is not a nop. It's not safe to | |||
4843 | // perform a tailcall optimization here. | |||
4844 | if (CallerF.getReturnType()->isX86_FP80Ty() && !RetTy->isX86_FP80Ty()) | |||
4845 | return false; | |||
4846 | ||||
4847 | CallingConv::ID CallerCC = CallerF.getCallingConv(); | |||
4848 | bool CCMatch = CallerCC == CalleeCC; | |||
4849 | bool IsCalleeWin64 = Subtarget.isCallingConvWin64(CalleeCC); | |||
4850 | bool IsCallerWin64 = Subtarget.isCallingConvWin64(CallerCC); | |||
4851 | bool IsGuaranteeTCO = DAG.getTarget().Options.GuaranteedTailCallOpt || | |||
4852 | CalleeCC == CallingConv::Tail || CalleeCC == CallingConv::SwiftTail; | |||
4853 | ||||
4854 | // Win64 functions have extra shadow space for argument homing. Don't do the | |||
4855 | // sibcall if the caller and callee have mismatched expectations for this | |||
4856 | // space. | |||
4857 | if (IsCalleeWin64 != IsCallerWin64) | |||
4858 | return false; | |||
4859 | ||||
4860 | if (IsGuaranteeTCO) { | |||
4861 | if (canGuaranteeTCO(CalleeCC) && CCMatch) | |||
4862 | return true; | |||
4863 | return false; | |||
4864 | } | |||
4865 | ||||
4866 | // Look for obvious safe cases to perform tail call optimization that do not | |||
4867 | // require ABI changes. This is what gcc calls sibcall. | |||
4868 | ||||
4869 | // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to | |||
4870 | // emit a special epilogue. | |||
4871 | const X86RegisterInfo *RegInfo = Subtarget.getRegisterInfo(); | |||
4872 | if (RegInfo->hasStackRealignment(MF)) | |||
4873 | return false; | |||
4874 | ||||
4875 | // Also avoid sibcall optimization if we're an sret return fn and the callee | |||
4876 | // is incompatible. See comment in LowerReturn about why hasStructRetAttr is | |||
4877 | // insufficient. | |||
4878 | if (MF.getInfo<X86MachineFunctionInfo>()->getSRetReturnReg()) { | |||
4879 | // For a compatible tail call the callee must return our sret pointer. So it | |||
4880 | // needs to be (a) an sret function itself and (b) we pass our sret as its | |||
4881 | // sret. Condition #b is harder to determine. | |||
4882 | return false; | |||
4883 | } else if (IsCalleePopSRet) | |||
4884 | // The callee pops an sret, so we cannot tail-call, as our caller doesn't | |||
4885 | // expect that. | |||
4886 | return false; | |||
4887 | ||||
4888 | // Do not sibcall optimize vararg calls unless all arguments are passed via | |||
4889 | // registers. | |||
4890 | LLVMContext &C = *DAG.getContext(); | |||
4891 | if (isVarArg && !Outs.empty()) { | |||
4892 | // Optimizing for varargs on Win64 is unlikely to be safe without | |||
4893 | // additional testing. | |||
4894 | if (IsCalleeWin64 || IsCallerWin64) | |||
4895 | return false; | |||
4896 | ||||
4897 | SmallVector<CCValAssign, 16> ArgLocs; | |||
4898 | CCState CCInfo(CalleeCC, isVarArg, MF, ArgLocs, C); | |||
4899 | ||||
4900 | CCInfo.AnalyzeCallOperands(Outs, CC_X86); | |||
4901 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) | |||
4902 | if (!ArgLocs[i].isRegLoc()) | |||
4903 | return false; | |||
4904 | } | |||
4905 | ||||
4906 | // If the call result is in ST0 / ST1, it needs to be popped off the x87 | |||
4907 | // stack. Therefore, if it's not used by the call it is not safe to optimize | |||
4908 | // this into a sibcall. | |||
4909 | bool Unused = false; | |||
4910 | for (unsigned i = 0, e = Ins.size(); i != e; ++i) { | |||
4911 | if (!Ins[i].Used) { | |||
4912 | Unused = true; | |||
4913 | break; | |||
4914 | } | |||
4915 | } | |||
4916 | if (Unused) { | |||
4917 | SmallVector<CCValAssign, 16> RVLocs; | |||
4918 | CCState CCInfo(CalleeCC, false, MF, RVLocs, C); | |||
4919 | CCInfo.AnalyzeCallResult(Ins, RetCC_X86); | |||
4920 | for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) { | |||
4921 | CCValAssign &VA = RVLocs[i]; | |||
4922 | if (VA.getLocReg() == X86::FP0 || VA.getLocReg() == X86::FP1) | |||
4923 | return false; | |||
4924 | } | |||
4925 | } | |||
4926 | ||||
4927 | // Check that the call results are passed in the same way. | |||
4928 | if (!CCState::resultsCompatible(CalleeCC, CallerCC, MF, C, Ins, | |||
4929 | RetCC_X86, RetCC_X86)) | |||
4930 | return false; | |||
4931 | // The callee has to preserve all registers the caller needs to preserve. | |||
4932 | const X86RegisterInfo *TRI = Subtarget.getRegisterInfo(); | |||
4933 | const uint32_t *CallerPreserved = TRI->getCallPreservedMask(MF, CallerCC); | |||
4934 | if (!CCMatch) { | |||
4935 | const uint32_t *CalleePreserved = TRI->getCallPreservedMask(MF, CalleeCC); | |||
4936 | if (!TRI->regmaskSubsetEqual(CallerPreserved, CalleePreserved)) | |||
4937 | return false; | |||
4938 | } | |||
4939 | ||||
4940 | unsigned StackArgsSize = 0; | |||
4941 | ||||
4942 | // If the callee takes no arguments then go on to check the results of the | |||
4943 | // call. | |||
4944 | if (!Outs.empty()) { | |||
4945 | // Check if stack adjustment is needed. For now, do not do this if any | |||
4946 | // argument is passed on the stack. | |||
4947 | SmallVector<CCValAssign, 16> ArgLocs; | |||
4948 | CCState CCInfo(CalleeCC, isVarArg, MF, ArgLocs, C); | |||
4949 | ||||
4950 | // Allocate shadow area for Win64 | |||
4951 | if (IsCalleeWin64) | |||
4952 | CCInfo.AllocateStack(32, Align(8)); | |||
4953 | ||||
4954 | CCInfo.AnalyzeCallOperands(Outs, CC_X86); | |||
4955 | StackArgsSize = CCInfo.getNextStackOffset(); | |||
4956 | ||||
4957 | if (CCInfo.getNextStackOffset()) { | |||
4958 | // Check if the arguments are already laid out in the right way as | |||
4959 | // the caller's fixed stack objects. | |||
4960 | MachineFrameInfo &MFI = MF.getFrameInfo(); | |||
4961 | const MachineRegisterInfo *MRI = &MF.getRegInfo(); | |||
4962 | const X86InstrInfo *TII = Subtarget.getInstrInfo(); | |||
4963 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { | |||
4964 | CCValAssign &VA = ArgLocs[i]; | |||
4965 | SDValue Arg = OutVals[i]; | |||
4966 | ISD::ArgFlagsTy Flags = Outs[i].Flags; | |||
4967 | if (VA.getLocInfo() == CCValAssign::Indirect) | |||
4968 | return false; | |||
4969 | if (!VA.isRegLoc()) { | |||
4970 | if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags, | |||
4971 | MFI, MRI, TII, VA)) | |||
4972 | return false; | |||
4973 | } | |||
4974 | } | |||
4975 | } | |||
4976 | ||||
4977 | bool PositionIndependent = isPositionIndependent(); | |||
4978 | // If the tailcall address may be in a register, then make sure it's | |||
4979 | // possible to register allocate for it. In 32-bit, the call address can | |||
4980 | // only target EAX, EDX, or ECX since the tail call must be scheduled after | |||
4981 | // callee-saved registers are restored. These happen to be the same | |||
4982 | // registers used to pass 'inreg' arguments so watch out for those. | |||
4983 | if (!Subtarget.is64Bit() && ((!isa<GlobalAddressSDNode>(Callee) && | |||
4984 | !isa<ExternalSymbolSDNode>(Callee)) || | |||
4985 | PositionIndependent)) { | |||
4986 | unsigned NumInRegs = 0; | |||
4987 | // In PIC we need an extra register to formulate the address computation | |||
4988 | // for the callee. | |||
4989 | unsigned MaxInRegs = PositionIndependent ? 2 : 3; | |||
4990 | ||||
4991 | for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) { | |||
4992 | CCValAssign &VA = ArgLocs[i]; | |||
4993 | if (!VA.isRegLoc()) | |||
4994 | continue; | |||
4995 | Register Reg = VA.getLocReg(); | |||
4996 | switch (Reg) { | |||
4997 | default: break; | |||
4998 | case X86::EAX: case X86::EDX: case X86::ECX: | |||
4999 | if (++NumInRegs == MaxInRegs) | |||
5000 | return false; | |||
5001 | break; | |||
5002 | } | |||
5003 | } | |||
5004 | } | |||
5005 | ||||
5006 | const MachineRegisterInfo &MRI = MF.getRegInfo(); | |||
5007 | if (!parametersInCSRMatch(MRI, CallerPreserved, ArgLocs, OutVals)) | |||
5008 | return false; | |||
5009 | } | |||
5010 | ||||
5011 | bool CalleeWillPop = | |||
5012 | X86::isCalleePop(CalleeCC, Subtarget.is64Bit(), isVarArg, | |||
5013 | MF.getTarget().Options.GuaranteedTailCallOpt); | |||
5014 | ||||
5015 | if (unsigned BytesToPop = | |||
5016 | MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn()) { | |||
5017 | // If we have bytes to pop, the callee must pop them. | |||
5018 | bool CalleePopMatches = CalleeWillPop && BytesToPop == StackArgsSize; | |||
5019 | if (!CalleePopMatches) | |||
5020 | return false; | |||
5021 | } else if (CalleeWillPop && StackArgsSize > 0) { | |||
5022 | // If we don't have bytes to pop, make sure the callee doesn't pop any. | |||
5023 | return false; | |||
5024 | } | |||
5025 | ||||
5026 | return true; | |||
5027 | } | |||
5028 | ||||
5029 | FastISel * | |||
5030 | X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo, | |||
5031 | const TargetLibraryInfo *libInfo) const { | |||
5032 | return X86::createFastISel(funcInfo, libInfo); | |||
5033 | } | |||
5034 | ||||
5035 | //===----------------------------------------------------------------------===// | |||
5036 | // Other Lowering Hooks | |||
5037 | //===----------------------------------------------------------------------===// | |||
5038 | ||||
5039 | static bool MayFoldLoad(SDValue Op, bool AssumeSingleUse = false) { | |||
5040 | return (AssumeSingleUse || Op.hasOneUse()) && ISD::isNormalLoad(Op.getNode()); | |||
5041 | } | |||
5042 | ||||
5043 | static bool MayFoldLoadIntoBroadcastFromMem(SDValue Op, MVT EltVT, | |||
5044 | bool AssumeSingleUse = false) { | |||
5045 | if (!MayFoldLoad(Op, AssumeSingleUse)) | |||
5046 | return false; | |||
5047 | ||||
5048 | // We can not replace a wide volatile load with a broadcast-from-memory, | |||
5049 | // because that would narrow the load, which isn't legal for volatiles. | |||
5050 | const LoadSDNode *Ld = dyn_cast<LoadSDNode>(Op.getNode()); | |||
5051 | return !Ld->isVolatile() || | |||
5052 | Ld->getValueSizeInBits(0) == EltVT.getScalarSizeInBits(); | |||
5053 | } | |||
5054 | ||||
5055 | static bool MayFoldIntoStore(SDValue Op) { | |||
5056 | return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin()); | |||
5057 | } | |||
5058 | ||||
5059 | static bool MayFoldIntoZeroExtend(SDValue Op) { | |||
5060 | if (Op.hasOneUse()) { | |||
5061 | unsigned Opcode = Op.getNode()->use_begin()->getOpcode(); | |||
5062 | return (ISD::ZERO_EXTEND == Opcode); | |||
5063 | } | |||
5064 | return false; | |||
5065 | } | |||
5066 | ||||
5067 | static bool isTargetShuffle(unsigned Opcode) { | |||
5068 | switch(Opcode) { | |||
5069 | default: return false; | |||
5070 | case X86ISD::BLENDI: | |||
5071 | case X86ISD::PSHUFB: | |||
5072 | case X86ISD::PSHUFD: | |||
5073 | case X86ISD::PSHUFHW: | |||
5074 | case X86ISD::PSHUFLW: | |||
5075 | case X86ISD::SHUFP: | |||
5076 | case X86ISD::INSERTPS: | |||
5077 | case X86ISD::EXTRQI: | |||
5078 | case X86ISD::INSERTQI: | |||
5079 | case X86ISD::VALIGN: | |||
5080 | case X86ISD::PALIGNR: | |||
5081 | case X86ISD::VSHLDQ: | |||
5082 | case X86ISD::VSRLDQ: | |||
5083 | case X86ISD::MOVLHPS: | |||
5084 | case X86ISD::MOVHLPS: | |||
5085 | case X86ISD::MOVSHDUP: | |||
5086 | case X86ISD::MOVSLDUP: | |||
5087 | case X86ISD::MOVDDUP: | |||
5088 | case X86ISD::MOVSS: | |||
5089 | case X86ISD::MOVSD: | |||
5090 | case X86ISD::MOVSH: | |||
5091 | case X86ISD::UNPCKL: | |||
5092 | case X86ISD::UNPCKH: | |||
5093 | case X86ISD::VBROADCAST: | |||
5094 | case X86ISD::VPERMILPI: | |||
5095 | case X86ISD::VPERMILPV: | |||
5096 | case X86ISD::VPERM2X128: | |||
5097 | case X86ISD::SHUF128: | |||
5098 | case X86ISD::VPERMIL2: | |||
5099 | case X86ISD::VPERMI: | |||
5100 | case X86ISD::VPPERM: | |||
5101 | case X86ISD::VPERMV: | |||
5102 | case X86ISD::VPERMV3: | |||
5103 | case X86ISD::VZEXT_MOVL: | |||
5104 | return true; | |||
5105 | } | |||
5106 | } | |||
5107 | ||||
5108 | static bool isTargetShuffleVariableMask(unsigned Opcode) { | |||
5109 | switch (Opcode) { | |||
5110 | default: return false; | |||
5111 | // Target Shuffles. | |||
5112 | case X86ISD::PSHUFB: | |||
5113 | case X86ISD::VPERMILPV: | |||
5114 | case X86ISD::VPERMIL2: | |||
5115 | case X86ISD::VPPERM: | |||
5116 | case X86ISD::VPERMV: | |||
5117 | case X86ISD::VPERMV3: | |||
5118 | return true; | |||
5119 | // 'Faux' Target Shuffles. | |||
5120 | case ISD::OR: | |||
5121 | case ISD::AND: | |||
5122 | case X86ISD::ANDNP: | |||
5123 | return true; | |||
5124 | } | |||
5125 | } | |||
5126 | ||||
5127 | static bool isTargetShuffleSplat(SDValue Op) { | |||
5128 | unsigned Opcode = Op.getOpcode(); | |||
5129 | if (Opcode == ISD::EXTRACT_SUBVECTOR) | |||
5130 | return isTargetShuffleSplat(Op.getOperand(0)); | |||
5131 | return Opcode == X86ISD::VBROADCAST || Opcode == X86ISD::VBROADCAST_LOAD; | |||
5132 | } | |||
5133 | ||||
5134 | SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const { | |||
5135 | MachineFunction &MF = DAG.getMachineFunction(); | |||
5136 | const X86RegisterInfo *RegInfo = Subtarget.getRegisterInfo(); | |||
5137 | X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>(); | |||
5138 | int ReturnAddrIndex = FuncInfo->getRAIndex(); | |||
5139 | ||||
5140 | if (ReturnAddrIndex == 0) { | |||
5141 | // Set up a frame object for the return address. | |||
5142 | unsigned SlotSize = RegInfo->getSlotSize(); | |||
5143 | ReturnAddrIndex = MF.getFrameInfo().CreateFixedObject(SlotSize, | |||
5144 | -(int64_t)SlotSize, | |||
5145 | false); | |||
5146 | FuncInfo->setRAIndex(ReturnAddrIndex); | |||
5147 | } | |||
5148 | ||||
5149 | return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy(DAG.getDataLayout())); | |||
5150 | } | |||
5151 | ||||
5152 | bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M, | |||
5153 | bool hasSymbolicDisplacement) { | |||
5154 | // Offset should fit into 32 bit immediate field. | |||
5155 | if (!isInt<32>(Offset)) | |||
5156 | return false; | |||
5157 | ||||
5158 | // If we don't have a symbolic displacement - we don't have any extra | |||
5159 | // restrictions. | |||
5160 | if (!hasSymbolicDisplacement) | |||
5161 | return true; | |||
5162 | ||||
5163 | // FIXME: Some tweaks might be needed for medium code model. | |||
5164 | if (M != CodeModel::Small && M != CodeModel::Kernel) | |||
5165 | return false; | |||
5166 | ||||
5167 | // For small code model we assume that latest object is 16MB before end of 31 | |||
5168 | // bits boundary. We may also accept pretty large negative constants knowing | |||
5169 | // that all objects are in the positive half of address space. | |||
5170 | if (M == CodeModel::Small && Offset < 16*1024*1024) | |||
5171 | return true; | |||
5172 | ||||
5173 | // For kernel code model we know that all object resist in the negative half | |||
5174 | // of 32bits address space. We may not accept negative offsets, since they may | |||
5175 | // be just off and we may accept pretty large positive ones. | |||
5176 | if (M == CodeModel::Kernel && Offset >= 0) | |||
5177 | return true; | |||
5178 | ||||
5179 | return false; | |||
5180 | } | |||
5181 | ||||
5182 | /// Determines whether the callee is required to pop its own arguments. | |||
5183 | /// Callee pop is necessary to support tail calls. | |||
5184 | bool X86::isCalleePop(CallingConv::ID CallingConv, | |||
5185 | bool is64Bit, bool IsVarArg, bool GuaranteeTCO) { | |||
5186 | // If GuaranteeTCO is true, we force some calls to be callee pop so that we | |||
5187 | // can guarantee TCO. | |||
5188 | if (!IsVarArg && shouldGuaranteeTCO(CallingConv, GuaranteeTCO)) | |||
5189 | return true; | |||
5190 | ||||
5191 | switch (CallingConv) { | |||
5192 | default: | |||
5193 | return false; | |||
5194 | case CallingConv::X86_StdCall: | |||
5195 | case CallingConv::X86_FastCall: | |||
5196 | case CallingConv::X86_ThisCall: | |||
5197 | case CallingConv::X86_VectorCall: | |||
5198 | return !is64Bit; | |||
5199 | } | |||
5200 | } | |||
5201 | ||||
5202 | /// Return true if the condition is an signed comparison operation. | |||
5203 | static bool isX86CCSigned(unsigned X86CC) { | |||
5204 | switch (X86CC) { | |||
5205 | default: | |||
5206 | llvm_unreachable("Invalid integer condition!")::llvm::llvm_unreachable_internal("Invalid integer condition!" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 5206); | |||
5207 | case X86::COND_E: | |||
5208 | case X86::COND_NE: | |||
5209 | case X86::COND_B: | |||
5210 | case X86::COND_A: | |||
5211 | case X86::COND_BE: | |||
5212 | case X86::COND_AE: | |||
5213 | return false; | |||
5214 | case X86::COND_G: | |||
5215 | case X86::COND_GE: | |||
5216 | case X86::COND_L: | |||
5217 | case X86::COND_LE: | |||
5218 | return true; | |||
5219 | } | |||
5220 | } | |||
5221 | ||||
5222 | static X86::CondCode TranslateIntegerX86CC(ISD::CondCode SetCCOpcode) { | |||
5223 | switch (SetCCOpcode) { | |||
5224 | default: llvm_unreachable("Invalid integer condition!")::llvm::llvm_unreachable_internal("Invalid integer condition!" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 5224); | |||
5225 | case ISD::SETEQ: return X86::COND_E; | |||
5226 | case ISD::SETGT: return X86::COND_G; | |||
5227 | case ISD::SETGE: return X86::COND_GE; | |||
5228 | case ISD::SETLT: return X86::COND_L; | |||
5229 | case ISD::SETLE: return X86::COND_LE; | |||
5230 | case ISD::SETNE: return X86::COND_NE; | |||
5231 | case ISD::SETULT: return X86::COND_B; | |||
5232 | case ISD::SETUGT: return X86::COND_A; | |||
5233 | case ISD::SETULE: return X86::COND_BE; | |||
5234 | case ISD::SETUGE: return X86::COND_AE; | |||
5235 | } | |||
5236 | } | |||
5237 | ||||
5238 | /// Do a one-to-one translation of a ISD::CondCode to the X86-specific | |||
5239 | /// condition code, returning the condition code and the LHS/RHS of the | |||
5240 | /// comparison to make. | |||
5241 | static X86::CondCode TranslateX86CC(ISD::CondCode SetCCOpcode, const SDLoc &DL, | |||
5242 | bool isFP, SDValue &LHS, SDValue &RHS, | |||
5243 | SelectionDAG &DAG) { | |||
5244 | if (!isFP) { | |||
5245 | if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) { | |||
5246 | if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnes()) { | |||
5247 | // X > -1 -> X == 0, jump !sign. | |||
5248 | RHS = DAG.getConstant(0, DL, RHS.getValueType()); | |||
5249 | return X86::COND_NS; | |||
5250 | } | |||
5251 | if (SetCCOpcode == ISD::SETLT && RHSC->isZero()) { | |||
5252 | // X < 0 -> X == 0, jump on sign. | |||
5253 | return X86::COND_S; | |||
5254 | } | |||
5255 | if (SetCCOpcode == ISD::SETGE && RHSC->isZero()) { | |||
5256 | // X >= 0 -> X == 0, jump on !sign. | |||
5257 | return X86::COND_NS; | |||
5258 | } | |||
5259 | if (SetCCOpcode == ISD::SETLT && RHSC->isOne()) { | |||
5260 | // X < 1 -> X <= 0 | |||
5261 | RHS = DAG.getConstant(0, DL, RHS.getValueType()); | |||
5262 | return X86::COND_LE; | |||
5263 | } | |||
5264 | } | |||
5265 | ||||
5266 | return TranslateIntegerX86CC(SetCCOpcode); | |||
5267 | } | |||
5268 | ||||
5269 | // First determine if it is required or is profitable to flip the operands. | |||
5270 | ||||
5271 | // If LHS is a foldable load, but RHS is not, flip the condition. | |||
5272 | if (ISD::isNON_EXTLoad(LHS.getNode()) && | |||
5273 | !ISD::isNON_EXTLoad(RHS.getNode())) { | |||
5274 | SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode); | |||
5275 | std::swap(LHS, RHS); | |||
5276 | } | |||
5277 | ||||
5278 | switch (SetCCOpcode) { | |||
5279 | default: break; | |||
5280 | case ISD::SETOLT: | |||
5281 | case ISD::SETOLE: | |||
5282 | case ISD::SETUGT: | |||
5283 | case ISD::SETUGE: | |||
5284 | std::swap(LHS, RHS); | |||
5285 | break; | |||
5286 | } | |||
5287 | ||||
5288 | // On a floating point condition, the flags are set as follows: | |||
5289 | // ZF PF CF op | |||
5290 | // 0 | 0 | 0 | X > Y | |||
5291 | // 0 | 0 | 1 | X < Y | |||
5292 | // 1 | 0 | 0 | X == Y | |||
5293 | // 1 | 1 | 1 | unordered | |||
5294 | switch (SetCCOpcode) { | |||
5295 | default: llvm_unreachable("Condcode should be pre-legalized away")::llvm::llvm_unreachable_internal("Condcode should be pre-legalized away" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 5295); | |||
5296 | case ISD::SETUEQ: | |||
5297 | case ISD::SETEQ: return X86::COND_E; | |||
5298 | case ISD::SETOLT: // flipped | |||
5299 | case ISD::SETOGT: | |||
5300 | case ISD::SETGT: return X86::COND_A; | |||
5301 | case ISD::SETOLE: // flipped | |||
5302 | case ISD::SETOGE: | |||
5303 | case ISD::SETGE: return X86::COND_AE; | |||
5304 | case ISD::SETUGT: // flipped | |||
5305 | case ISD::SETULT: | |||
5306 | case ISD::SETLT: return X86::COND_B; | |||
5307 | case ISD::SETUGE: // flipped | |||
5308 | case ISD::SETULE: | |||
5309 | case ISD::SETLE: return X86::COND_BE; | |||
5310 | case ISD::SETONE: | |||
5311 | case ISD::SETNE: return X86::COND_NE; | |||
5312 | case ISD::SETUO: return X86::COND_P; | |||
5313 | case ISD::SETO: return X86::COND_NP; | |||
5314 | case ISD::SETOEQ: | |||
5315 | case ISD::SETUNE: return X86::COND_INVALID; | |||
5316 | } | |||
5317 | } | |||
5318 | ||||
5319 | /// Is there a floating point cmov for the specific X86 condition code? | |||
5320 | /// Current x86 isa includes the following FP cmov instructions: | |||
5321 | /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu. | |||
5322 | static bool hasFPCMov(unsigned X86CC) { | |||
5323 | switch (X86CC) { | |||
5324 | default: | |||
5325 | return false; | |||
5326 | case X86::COND_B: | |||
5327 | case X86::COND_BE: | |||
5328 | case X86::COND_E: | |||
5329 | case X86::COND_P: | |||
5330 | case X86::COND_A: | |||
5331 | case X86::COND_AE: | |||
5332 | case X86::COND_NE: | |||
5333 | case X86::COND_NP: | |||
5334 | return true; | |||
5335 | } | |||
5336 | } | |||
5337 | ||||
5338 | ||||
5339 | bool X86TargetLowering::getTgtMemIntrinsic(IntrinsicInfo &Info, | |||
5340 | const CallInst &I, | |||
5341 | MachineFunction &MF, | |||
5342 | unsigned Intrinsic) const { | |||
5343 | Info.flags = MachineMemOperand::MONone; | |||
5344 | Info.offset = 0; | |||
5345 | ||||
5346 | const IntrinsicData* IntrData = getIntrinsicWithChain(Intrinsic); | |||
5347 | if (!IntrData) { | |||
5348 | switch (Intrinsic) { | |||
5349 | case Intrinsic::x86_aesenc128kl: | |||
5350 | case Intrinsic::x86_aesdec128kl: | |||
5351 | Info.opc = ISD::INTRINSIC_W_CHAIN; | |||
5352 | Info.ptrVal = I.getArgOperand(1); | |||
5353 | Info.memVT = EVT::getIntegerVT(I.getType()->getContext(), 48); | |||
5354 | Info.align = Align(1); | |||
5355 | Info.flags |= MachineMemOperand::MOLoad; | |||
5356 | return true; | |||
5357 | case Intrinsic::x86_aesenc256kl: | |||
5358 | case Intrinsic::x86_aesdec256kl: | |||
5359 | Info.opc = ISD::INTRINSIC_W_CHAIN; | |||
5360 | Info.ptrVal = I.getArgOperand(1); | |||
5361 | Info.memVT = EVT::getIntegerVT(I.getType()->getContext(), 64); | |||
5362 | Info.align = Align(1); | |||
5363 | Info.flags |= MachineMemOperand::MOLoad; | |||
5364 | return true; | |||
5365 | case Intrinsic::x86_aesencwide128kl: | |||
5366 | case Intrinsic::x86_aesdecwide128kl: | |||
5367 | Info.opc = ISD::INTRINSIC_W_CHAIN; | |||
5368 | Info.ptrVal = I.getArgOperand(0); | |||
5369 | Info.memVT = EVT::getIntegerVT(I.getType()->getContext(), 48); | |||
5370 | Info.align = Align(1); | |||
5371 | Info.flags |= MachineMemOperand::MOLoad; | |||
5372 | return true; | |||
5373 | case Intrinsic::x86_aesencwide256kl: | |||
5374 | case Intrinsic::x86_aesdecwide256kl: | |||
5375 | Info.opc = ISD::INTRINSIC_W_CHAIN; | |||
5376 | Info.ptrVal = I.getArgOperand(0); | |||
5377 | Info.memVT = EVT::getIntegerVT(I.getType()->getContext(), 64); | |||
5378 | Info.align = Align(1); | |||
5379 | Info.flags |= MachineMemOperand::MOLoad; | |||
5380 | return true; | |||
5381 | } | |||
5382 | return false; | |||
5383 | } | |||
5384 | ||||
5385 | switch (IntrData->Type) { | |||
5386 | case TRUNCATE_TO_MEM_VI8: | |||
5387 | case TRUNCATE_TO_MEM_VI16: | |||
5388 | case TRUNCATE_TO_MEM_VI32: { | |||
5389 | Info.opc = ISD::INTRINSIC_VOID; | |||
5390 | Info.ptrVal = I.getArgOperand(0); | |||
5391 | MVT VT = MVT::getVT(I.getArgOperand(1)->getType()); | |||
5392 | MVT ScalarVT = MVT::INVALID_SIMPLE_VALUE_TYPE; | |||
5393 | if (IntrData->Type == TRUNCATE_TO_MEM_VI8) | |||
5394 | ScalarVT = MVT::i8; | |||
5395 | else if (IntrData->Type == TRUNCATE_TO_MEM_VI16) | |||
5396 | ScalarVT = MVT::i16; | |||
5397 | else if (IntrData->Type == TRUNCATE_TO_MEM_VI32) | |||
5398 | ScalarVT = MVT::i32; | |||
5399 | ||||
5400 | Info.memVT = MVT::getVectorVT(ScalarVT, VT.getVectorNumElements()); | |||
5401 | Info.align = Align(1); | |||
5402 | Info.flags |= MachineMemOperand::MOStore; | |||
5403 | break; | |||
5404 | } | |||
5405 | case GATHER: | |||
5406 | case GATHER_AVX2: { | |||
5407 | Info.opc = ISD::INTRINSIC_W_CHAIN; | |||
5408 | Info.ptrVal = nullptr; | |||
5409 | MVT DataVT = MVT::getVT(I.getType()); | |||
5410 | MVT IndexVT = MVT::getVT(I.getArgOperand(2)->getType()); | |||
5411 | unsigned NumElts = std::min(DataVT.getVectorNumElements(), | |||
5412 | IndexVT.getVectorNumElements()); | |||
5413 | Info.memVT = MVT::getVectorVT(DataVT.getVectorElementType(), NumElts); | |||
5414 | Info.align = Align(1); | |||
5415 | Info.flags |= MachineMemOperand::MOLoad; | |||
5416 | break; | |||
5417 | } | |||
5418 | case SCATTER: { | |||
5419 | Info.opc = ISD::INTRINSIC_VOID; | |||
5420 | Info.ptrVal = nullptr; | |||
5421 | MVT DataVT = MVT::getVT(I.getArgOperand(3)->getType()); | |||
5422 | MVT IndexVT = MVT::getVT(I.getArgOperand(2)->getType()); | |||
5423 | unsigned NumElts = std::min(DataVT.getVectorNumElements(), | |||
5424 | IndexVT.getVectorNumElements()); | |||
5425 | Info.memVT = MVT::getVectorVT(DataVT.getVectorElementType(), NumElts); | |||
5426 | Info.align = Align(1); | |||
5427 | Info.flags |= MachineMemOperand::MOStore; | |||
5428 | break; | |||
5429 | } | |||
5430 | default: | |||
5431 | return false; | |||
5432 | } | |||
5433 | ||||
5434 | return true; | |||
5435 | } | |||
5436 | ||||
5437 | /// Returns true if the target can instruction select the | |||
5438 | /// specified FP immediate natively. If false, the legalizer will | |||
5439 | /// materialize the FP immediate as a load from a constant pool. | |||
5440 | bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT, | |||
5441 | bool ForCodeSize) const { | |||
5442 | for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) { | |||
5443 | if (Imm.bitwiseIsEqual(LegalFPImmediates[i])) | |||
5444 | return true; | |||
5445 | } | |||
5446 | return false; | |||
5447 | } | |||
5448 | ||||
5449 | bool X86TargetLowering::shouldReduceLoadWidth(SDNode *Load, | |||
5450 | ISD::LoadExtType ExtTy, | |||
5451 | EVT NewVT) const { | |||
5452 | assert(cast<LoadSDNode>(Load)->isSimple() && "illegal to narrow")(static_cast <bool> (cast<LoadSDNode>(Load)->isSimple () && "illegal to narrow") ? void (0) : __assert_fail ("cast<LoadSDNode>(Load)->isSimple() && \"illegal to narrow\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 5452, __extension__ __PRETTY_FUNCTION__)); | |||
5453 | ||||
5454 | // "ELF Handling for Thread-Local Storage" specifies that R_X86_64_GOTTPOFF | |||
5455 | // relocation target a movq or addq instruction: don't let the load shrink. | |||
5456 | SDValue BasePtr = cast<LoadSDNode>(Load)->getBasePtr(); | |||
5457 | if (BasePtr.getOpcode() == X86ISD::WrapperRIP) | |||
5458 | if (const auto *GA = dyn_cast<GlobalAddressSDNode>(BasePtr.getOperand(0))) | |||
5459 | return GA->getTargetFlags() != X86II::MO_GOTTPOFF; | |||
5460 | ||||
5461 | // If this is an (1) AVX vector load with (2) multiple uses and (3) all of | |||
5462 | // those uses are extracted directly into a store, then the extract + store | |||
5463 | // can be store-folded. Therefore, it's probably not worth splitting the load. | |||
5464 | EVT VT = Load->getValueType(0); | |||
5465 | if ((VT.is256BitVector() || VT.is512BitVector()) && !Load->hasOneUse()) { | |||
5466 | for (auto UI = Load->use_begin(), UE = Load->use_end(); UI != UE; ++UI) { | |||
5467 | // Skip uses of the chain value. Result 0 of the node is the load value. | |||
5468 | if (UI.getUse().getResNo() != 0) | |||
5469 | continue; | |||
5470 | ||||
5471 | // If this use is not an extract + store, it's probably worth splitting. | |||
5472 | if (UI->getOpcode() != ISD::EXTRACT_SUBVECTOR || !UI->hasOneUse() || | |||
5473 | UI->use_begin()->getOpcode() != ISD::STORE) | |||
5474 | return true; | |||
5475 | } | |||
5476 | // All non-chain uses are extract + store. | |||
5477 | return false; | |||
5478 | } | |||
5479 | ||||
5480 | return true; | |||
5481 | } | |||
5482 | ||||
5483 | /// Returns true if it is beneficial to convert a load of a constant | |||
5484 | /// to just the constant itself. | |||
5485 | bool X86TargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm, | |||
5486 | Type *Ty) const { | |||
5487 | assert(Ty->isIntegerTy())(static_cast <bool> (Ty->isIntegerTy()) ? void (0) : __assert_fail ("Ty->isIntegerTy()", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 5487, __extension__ __PRETTY_FUNCTION__)); | |||
5488 | ||||
5489 | unsigned BitSize = Ty->getPrimitiveSizeInBits(); | |||
5490 | if (BitSize == 0 || BitSize > 64) | |||
5491 | return false; | |||
5492 | return true; | |||
5493 | } | |||
5494 | ||||
5495 | bool X86TargetLowering::reduceSelectOfFPConstantLoads(EVT CmpOpVT) const { | |||
5496 | // If we are using XMM registers in the ABI and the condition of the select is | |||
5497 | // a floating-point compare and we have blendv or conditional move, then it is | |||
5498 | // cheaper to select instead of doing a cross-register move and creating a | |||
5499 | // load that depends on the compare result. | |||
5500 | bool IsFPSetCC = CmpOpVT.isFloatingPoint() && CmpOpVT != MVT::f128; | |||
5501 | return !IsFPSetCC || !Subtarget.isTarget64BitLP64() || !Subtarget.hasAVX(); | |||
5502 | } | |||
5503 | ||||
5504 | bool X86TargetLowering::convertSelectOfConstantsToMath(EVT VT) const { | |||
5505 | // TODO: It might be a win to ease or lift this restriction, but the generic | |||
5506 | // folds in DAGCombiner conflict with vector folds for an AVX512 target. | |||
5507 | if (VT.isVector() && Subtarget.hasAVX512()) | |||
5508 | return false; | |||
5509 | ||||
5510 | return true; | |||
5511 | } | |||
5512 | ||||
5513 | bool X86TargetLowering::decomposeMulByConstant(LLVMContext &Context, EVT VT, | |||
5514 | SDValue C) const { | |||
5515 | // TODO: We handle scalars using custom code, but generic combining could make | |||
5516 | // that unnecessary. | |||
5517 | APInt MulC; | |||
5518 | if (!ISD::isConstantSplatVector(C.getNode(), MulC)) | |||
5519 | return false; | |||
5520 | ||||
5521 | // Find the type this will be legalized too. Otherwise we might prematurely | |||
5522 | // convert this to shl+add/sub and then still have to type legalize those ops. | |||
5523 | // Another choice would be to defer the decision for illegal types until | |||
5524 | // after type legalization. But constant splat vectors of i64 can't make it | |||
5525 | // through type legalization on 32-bit targets so we would need to special | |||
5526 | // case vXi64. | |||
5527 | while (getTypeAction(Context, VT) != TypeLegal) | |||
5528 | VT = getTypeToTransformTo(Context, VT); | |||
5529 | ||||
5530 | // If vector multiply is legal, assume that's faster than shl + add/sub. | |||
5531 | // Multiply is a complex op with higher latency and lower throughput in | |||
5532 | // most implementations, sub-vXi32 vector multiplies are always fast, | |||
5533 | // vXi32 mustn't have a SlowMULLD implementation, and anything larger (vXi64) | |||
5534 | // is always going to be slow. | |||
5535 | unsigned EltSizeInBits = VT.getScalarSizeInBits(); | |||
5536 | if (isOperationLegal(ISD::MUL, VT) && EltSizeInBits <= 32 && | |||
5537 | (EltSizeInBits != 32 || !Subtarget.isPMULLDSlow())) | |||
5538 | return false; | |||
5539 | ||||
5540 | // shl+add, shl+sub, shl+add+neg | |||
5541 | return (MulC + 1).isPowerOf2() || (MulC - 1).isPowerOf2() || | |||
5542 | (1 - MulC).isPowerOf2() || (-(MulC + 1)).isPowerOf2(); | |||
5543 | } | |||
5544 | ||||
5545 | bool X86TargetLowering::isExtractSubvectorCheap(EVT ResVT, EVT SrcVT, | |||
5546 | unsigned Index) const { | |||
5547 | if (!isOperationLegalOrCustom(ISD::EXTRACT_SUBVECTOR, ResVT)) | |||
5548 | return false; | |||
5549 | ||||
5550 | // Mask vectors support all subregister combinations and operations that | |||
5551 | // extract half of vector. | |||
5552 | if (ResVT.getVectorElementType() == MVT::i1) | |||
5553 | return Index == 0 || ((ResVT.getSizeInBits() == SrcVT.getSizeInBits()*2) && | |||
5554 | (Index == ResVT.getVectorNumElements())); | |||
5555 | ||||
5556 | return (Index % ResVT.getVectorNumElements()) == 0; | |||
5557 | } | |||
5558 | ||||
5559 | bool X86TargetLowering::shouldScalarizeBinop(SDValue VecOp) const { | |||
5560 | unsigned Opc = VecOp.getOpcode(); | |||
5561 | ||||
5562 | // Assume target opcodes can't be scalarized. | |||
5563 | // TODO - do we have any exceptions? | |||
5564 | if (Opc >= ISD::BUILTIN_OP_END) | |||
5565 | return false; | |||
5566 | ||||
5567 | // If the vector op is not supported, try to convert to scalar. | |||
5568 | EVT VecVT = VecOp.getValueType(); | |||
5569 | if (!isOperationLegalOrCustomOrPromote(Opc, VecVT)) | |||
5570 | return true; | |||
5571 | ||||
5572 | // If the vector op is supported, but the scalar op is not, the transform may | |||
5573 | // not be worthwhile. | |||
5574 | EVT ScalarVT = VecVT.getScalarType(); | |||
5575 | return isOperationLegalOrCustomOrPromote(Opc, ScalarVT); | |||
5576 | } | |||
5577 | ||||
5578 | bool X86TargetLowering::shouldFormOverflowOp(unsigned Opcode, EVT VT, | |||
5579 | bool) const { | |||
5580 | // TODO: Allow vectors? | |||
5581 | if (VT.isVector()) | |||
5582 | return false; | |||
5583 | return VT.isSimple() || !isOperationExpand(Opcode, VT); | |||
5584 | } | |||
5585 | ||||
5586 | bool X86TargetLowering::isCheapToSpeculateCttz() const { | |||
5587 | // Speculate cttz only if we can directly use TZCNT. | |||
5588 | return Subtarget.hasBMI(); | |||
5589 | } | |||
5590 | ||||
5591 | bool X86TargetLowering::isCheapToSpeculateCtlz() const { | |||
5592 | // Speculate ctlz only if we can directly use LZCNT. | |||
5593 | return Subtarget.hasLZCNT(); | |||
5594 | } | |||
5595 | ||||
5596 | bool X86TargetLowering::isLoadBitCastBeneficial(EVT LoadVT, EVT BitcastVT, | |||
5597 | const SelectionDAG &DAG, | |||
5598 | const MachineMemOperand &MMO) const { | |||
5599 | if (!Subtarget.hasAVX512() && !LoadVT.isVector() && BitcastVT.isVector() && | |||
5600 | BitcastVT.getVectorElementType() == MVT::i1) | |||
5601 | return false; | |||
5602 | ||||
5603 | if (!Subtarget.hasDQI() && BitcastVT == MVT::v8i1 && LoadVT == MVT::i8) | |||
5604 | return false; | |||
5605 | ||||
5606 | // If both types are legal vectors, it's always ok to convert them. | |||
5607 | if (LoadVT.isVector() && BitcastVT.isVector() && | |||
5608 | isTypeLegal(LoadVT) && isTypeLegal(BitcastVT)) | |||
5609 | return true; | |||
5610 | ||||
5611 | return TargetLowering::isLoadBitCastBeneficial(LoadVT, BitcastVT, DAG, MMO); | |||
5612 | } | |||
5613 | ||||
5614 | bool X86TargetLowering::canMergeStoresTo(unsigned AddressSpace, EVT MemVT, | |||
5615 | const MachineFunction &MF) const { | |||
5616 | // Do not merge to float value size (128 bytes) if no implicit | |||
5617 | // float attribute is set. | |||
5618 | bool NoFloat = MF.getFunction().hasFnAttribute(Attribute::NoImplicitFloat); | |||
5619 | ||||
5620 | if (NoFloat) { | |||
5621 | unsigned MaxIntSize = Subtarget.is64Bit() ? 64 : 32; | |||
5622 | return (MemVT.getSizeInBits() <= MaxIntSize); | |||
5623 | } | |||
5624 | // Make sure we don't merge greater than our preferred vector | |||
5625 | // width. | |||
5626 | if (MemVT.getSizeInBits() > Subtarget.getPreferVectorWidth()) | |||
5627 | return false; | |||
5628 | ||||
5629 | return true; | |||
5630 | } | |||
5631 | ||||
5632 | bool X86TargetLowering::isCtlzFast() const { | |||
5633 | return Subtarget.hasFastLZCNT(); | |||
5634 | } | |||
5635 | ||||
5636 | bool X86TargetLowering::isMaskAndCmp0FoldingBeneficial( | |||
5637 | const Instruction &AndI) const { | |||
5638 | return true; | |||
5639 | } | |||
5640 | ||||
5641 | bool X86TargetLowering::hasAndNotCompare(SDValue Y) const { | |||
5642 | EVT VT = Y.getValueType(); | |||
5643 | ||||
5644 | if (VT.isVector()) | |||
5645 | return false; | |||
5646 | ||||
5647 | if (!Subtarget.hasBMI()) | |||
5648 | return false; | |||
5649 | ||||
5650 | // There are only 32-bit and 64-bit forms for 'andn'. | |||
5651 | if (VT != MVT::i32 && VT != MVT::i64) | |||
5652 | return false; | |||
5653 | ||||
5654 | return !isa<ConstantSDNode>(Y); | |||
5655 | } | |||
5656 | ||||
5657 | bool X86TargetLowering::hasAndNot(SDValue Y) const { | |||
5658 | EVT VT = Y.getValueType(); | |||
5659 | ||||
5660 | if (!VT.isVector()) | |||
5661 | return hasAndNotCompare(Y); | |||
5662 | ||||
5663 | // Vector. | |||
5664 | ||||
5665 | if (!Subtarget.hasSSE1() || VT.getSizeInBits() < 128) | |||
5666 | return false; | |||
5667 | ||||
5668 | if (VT == MVT::v4i32) | |||
5669 | return true; | |||
5670 | ||||
5671 | return Subtarget.hasSSE2(); | |||
5672 | } | |||
5673 | ||||
5674 | bool X86TargetLowering::hasBitTest(SDValue X, SDValue Y) const { | |||
5675 | return X.getValueType().isScalarInteger(); // 'bt' | |||
5676 | } | |||
5677 | ||||
5678 | bool X86TargetLowering:: | |||
5679 | shouldProduceAndByConstByHoistingConstFromShiftsLHSOfAnd( | |||
5680 | SDValue X, ConstantSDNode *XC, ConstantSDNode *CC, SDValue Y, | |||
5681 | unsigned OldShiftOpcode, unsigned NewShiftOpcode, | |||
5682 | SelectionDAG &DAG) const { | |||
5683 | // Does baseline recommend not to perform the fold by default? | |||
5684 | if (!TargetLowering::shouldProduceAndByConstByHoistingConstFromShiftsLHSOfAnd( | |||
5685 | X, XC, CC, Y, OldShiftOpcode, NewShiftOpcode, DAG)) | |||
5686 | return false; | |||
5687 | // For scalars this transform is always beneficial. | |||
5688 | if (X.getValueType().isScalarInteger()) | |||
5689 | return true; | |||
5690 | // If all the shift amounts are identical, then transform is beneficial even | |||
5691 | // with rudimentary SSE2 shifts. | |||
5692 | if (DAG.isSplatValue(Y, /*AllowUndefs=*/true)) | |||
5693 | return true; | |||
5694 | // If we have AVX2 with it's powerful shift operations, then it's also good. | |||
5695 | if (Subtarget.hasAVX2()) | |||
5696 | return true; | |||
5697 | // Pre-AVX2 vector codegen for this pattern is best for variant with 'shl'. | |||
5698 | return NewShiftOpcode == ISD::SHL; | |||
5699 | } | |||
5700 | ||||
5701 | bool X86TargetLowering::shouldFoldConstantShiftPairToMask( | |||
5702 | const SDNode *N, CombineLevel Level) const { | |||
5703 | assert(((N->getOpcode() == ISD::SHL &&(static_cast <bool> (((N->getOpcode() == ISD::SHL && N->getOperand(0).getOpcode() == ISD::SRL) || (N->getOpcode () == ISD::SRL && N->getOperand(0).getOpcode() == ISD ::SHL)) && "Expected shift-shift mask") ? void (0) : __assert_fail ("((N->getOpcode() == ISD::SHL && N->getOperand(0).getOpcode() == ISD::SRL) || (N->getOpcode() == ISD::SRL && N->getOperand(0).getOpcode() == ISD::SHL)) && \"Expected shift-shift mask\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 5707, __extension__ __PRETTY_FUNCTION__)) | |||
5704 | N->getOperand(0).getOpcode() == ISD::SRL) ||(static_cast <bool> (((N->getOpcode() == ISD::SHL && N->getOperand(0).getOpcode() == ISD::SRL) || (N->getOpcode () == ISD::SRL && N->getOperand(0).getOpcode() == ISD ::SHL)) && "Expected shift-shift mask") ? void (0) : __assert_fail ("((N->getOpcode() == ISD::SHL && N->getOperand(0).getOpcode() == ISD::SRL) || (N->getOpcode() == ISD::SRL && N->getOperand(0).getOpcode() == ISD::SHL)) && \"Expected shift-shift mask\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 5707, __extension__ __PRETTY_FUNCTION__)) | |||
5705 | (N->getOpcode() == ISD::SRL &&(static_cast <bool> (((N->getOpcode() == ISD::SHL && N->getOperand(0).getOpcode() == ISD::SRL) || (N->getOpcode () == ISD::SRL && N->getOperand(0).getOpcode() == ISD ::SHL)) && "Expected shift-shift mask") ? void (0) : __assert_fail ("((N->getOpcode() == ISD::SHL && N->getOperand(0).getOpcode() == ISD::SRL) || (N->getOpcode() == ISD::SRL && N->getOperand(0).getOpcode() == ISD::SHL)) && \"Expected shift-shift mask\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 5707, __extension__ __PRETTY_FUNCTION__)) | |||
5706 | N->getOperand(0).getOpcode() == ISD::SHL)) &&(static_cast <bool> (((N->getOpcode() == ISD::SHL && N->getOperand(0).getOpcode() == ISD::SRL) || (N->getOpcode () == ISD::SRL && N->getOperand(0).getOpcode() == ISD ::SHL)) && "Expected shift-shift mask") ? void (0) : __assert_fail ("((N->getOpcode() == ISD::SHL && N->getOperand(0).getOpcode() == ISD::SRL) || (N->getOpcode() == ISD::SRL && N->getOperand(0).getOpcode() == ISD::SHL)) && \"Expected shift-shift mask\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 5707, __extension__ __PRETTY_FUNCTION__)) | |||
5707 | "Expected shift-shift mask")(static_cast <bool> (((N->getOpcode() == ISD::SHL && N->getOperand(0).getOpcode() == ISD::SRL) || (N->getOpcode () == ISD::SRL && N->getOperand(0).getOpcode() == ISD ::SHL)) && "Expected shift-shift mask") ? void (0) : __assert_fail ("((N->getOpcode() == ISD::SHL && N->getOperand(0).getOpcode() == ISD::SRL) || (N->getOpcode() == ISD::SRL && N->getOperand(0).getOpcode() == ISD::SHL)) && \"Expected shift-shift mask\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 5707, __extension__ __PRETTY_FUNCTION__)); | |||
5708 | EVT VT = N->getValueType(0); | |||
5709 | if ((Subtarget.hasFastVectorShiftMasks() && VT.isVector()) || | |||
5710 | (Subtarget.hasFastScalarShiftMasks() && !VT.isVector())) { | |||
5711 | // Only fold if the shift values are equal - so it folds to AND. | |||
5712 | // TODO - we should fold if either is a non-uniform vector but we don't do | |||
5713 | // the fold for non-splats yet. | |||
5714 | return N->getOperand(1) == N->getOperand(0).getOperand(1); | |||
5715 | } | |||
5716 | return TargetLoweringBase::shouldFoldConstantShiftPairToMask(N, Level); | |||
5717 | } | |||
5718 | ||||
5719 | bool X86TargetLowering::shouldFoldMaskToVariableShiftPair(SDValue Y) const { | |||
5720 | EVT VT = Y.getValueType(); | |||
5721 | ||||
5722 | // For vectors, we don't have a preference, but we probably want a mask. | |||
5723 | if (VT.isVector()) | |||
5724 | return false; | |||
5725 | ||||
5726 | // 64-bit shifts on 32-bit targets produce really bad bloated code. | |||
5727 | if (VT == MVT::i64 && !Subtarget.is64Bit()) | |||
5728 | return false; | |||
5729 | ||||
5730 | return true; | |||
5731 | } | |||
5732 | ||||
5733 | bool X86TargetLowering::shouldExpandShift(SelectionDAG &DAG, | |||
5734 | SDNode *N) const { | |||
5735 | if (DAG.getMachineFunction().getFunction().hasMinSize() && | |||
5736 | !Subtarget.isOSWindows()) | |||
5737 | return false; | |||
5738 | return true; | |||
5739 | } | |||
5740 | ||||
5741 | bool X86TargetLowering::shouldSplatInsEltVarIndex(EVT VT) const { | |||
5742 | // Any legal vector type can be splatted more efficiently than | |||
5743 | // loading/spilling from memory. | |||
5744 | return isTypeLegal(VT); | |||
5745 | } | |||
5746 | ||||
5747 | MVT X86TargetLowering::hasFastEqualityCompare(unsigned NumBits) const { | |||
5748 | MVT VT = MVT::getIntegerVT(NumBits); | |||
5749 | if (isTypeLegal(VT)) | |||
5750 | return VT; | |||
5751 | ||||
5752 | // PMOVMSKB can handle this. | |||
5753 | if (NumBits == 128 && isTypeLegal(MVT::v16i8)) | |||
5754 | return MVT::v16i8; | |||
5755 | ||||
5756 | // VPMOVMSKB can handle this. | |||
5757 | if (NumBits == 256 && isTypeLegal(MVT::v32i8)) | |||
5758 | return MVT::v32i8; | |||
5759 | ||||
5760 | // TODO: Allow 64-bit type for 32-bit target. | |||
5761 | // TODO: 512-bit types should be allowed, but make sure that those | |||
5762 | // cases are handled in combineVectorSizedSetCCEquality(). | |||
5763 | ||||
5764 | return MVT::INVALID_SIMPLE_VALUE_TYPE; | |||
5765 | } | |||
5766 | ||||
5767 | /// Val is the undef sentinel value or equal to the specified value. | |||
5768 | static bool isUndefOrEqual(int Val, int CmpVal) { | |||
5769 | return ((Val == SM_SentinelUndef) || (Val == CmpVal)); | |||
5770 | } | |||
5771 | ||||
5772 | /// Return true if every element in Mask is the undef sentinel value or equal to | |||
5773 | /// the specified value.. | |||
5774 | static bool isUndefOrEqual(ArrayRef<int> Mask, int CmpVal) { | |||
5775 | return llvm::all_of(Mask, [CmpVal](int M) { | |||
5776 | return (M == SM_SentinelUndef) || (M == CmpVal); | |||
5777 | }); | |||
5778 | } | |||
5779 | ||||
5780 | /// Val is either the undef or zero sentinel value. | |||
5781 | static bool isUndefOrZero(int Val) { | |||
5782 | return ((Val == SM_SentinelUndef) || (Val == SM_SentinelZero)); | |||
5783 | } | |||
5784 | ||||
5785 | /// Return true if every element in Mask, beginning from position Pos and ending | |||
5786 | /// in Pos+Size is the undef sentinel value. | |||
5787 | static bool isUndefInRange(ArrayRef<int> Mask, unsigned Pos, unsigned Size) { | |||
5788 | return llvm::all_of(Mask.slice(Pos, Size), | |||
5789 | [](int M) { return M == SM_SentinelUndef; }); | |||
5790 | } | |||
5791 | ||||
5792 | /// Return true if the mask creates a vector whose lower half is undefined. | |||
5793 | static bool isUndefLowerHalf(ArrayRef<int> Mask) { | |||
5794 | unsigned NumElts = Mask.size(); | |||
5795 | return isUndefInRange(Mask, 0, NumElts / 2); | |||
5796 | } | |||
5797 | ||||
5798 | /// Return true if the mask creates a vector whose upper half is undefined. | |||
5799 | static bool isUndefUpperHalf(ArrayRef<int> Mask) { | |||
5800 | unsigned NumElts = Mask.size(); | |||
5801 | return isUndefInRange(Mask, NumElts / 2, NumElts / 2); | |||
5802 | } | |||
5803 | ||||
5804 | /// Return true if Val falls within the specified range (L, H]. | |||
5805 | static bool isInRange(int Val, int Low, int Hi) { | |||
5806 | return (Val >= Low && Val < Hi); | |||
5807 | } | |||
5808 | ||||
5809 | /// Return true if the value of any element in Mask falls within the specified | |||
5810 | /// range (L, H]. | |||
5811 | static bool isAnyInRange(ArrayRef<int> Mask, int Low, int Hi) { | |||
5812 | return llvm::any_of(Mask, [Low, Hi](int M) { return isInRange(M, Low, Hi); }); | |||
5813 | } | |||
5814 | ||||
5815 | /// Return true if the value of any element in Mask is the zero sentinel value. | |||
5816 | static bool isAnyZero(ArrayRef<int> Mask) { | |||
5817 | return llvm::any_of(Mask, [](int M) { return M == SM_SentinelZero; }); | |||
5818 | } | |||
5819 | ||||
5820 | /// Return true if the value of any element in Mask is the zero or undef | |||
5821 | /// sentinel values. | |||
5822 | static bool isAnyZeroOrUndef(ArrayRef<int> Mask) { | |||
5823 | return llvm::any_of(Mask, [](int M) { | |||
5824 | return M == SM_SentinelZero || M == SM_SentinelUndef; | |||
5825 | }); | |||
5826 | } | |||
5827 | ||||
5828 | /// Return true if Val is undef or if its value falls within the | |||
5829 | /// specified range (L, H]. | |||
5830 | static bool isUndefOrInRange(int Val, int Low, int Hi) { | |||
5831 | return (Val == SM_SentinelUndef) || isInRange(Val, Low, Hi); | |||
5832 | } | |||
5833 | ||||
5834 | /// Return true if every element in Mask is undef or if its value | |||
5835 | /// falls within the specified range (L, H]. | |||
5836 | static bool isUndefOrInRange(ArrayRef<int> Mask, int Low, int Hi) { | |||
5837 | return llvm::all_of( | |||
5838 | Mask, [Low, Hi](int M) { return isUndefOrInRange(M, Low, Hi); }); | |||
5839 | } | |||
5840 | ||||
5841 | /// Return true if Val is undef, zero or if its value falls within the | |||
5842 | /// specified range (L, H]. | |||
5843 | static bool isUndefOrZeroOrInRange(int Val, int Low, int Hi) { | |||
5844 | return isUndefOrZero(Val) || isInRange(Val, Low, Hi); | |||
5845 | } | |||
5846 | ||||
5847 | /// Return true if every element in Mask is undef, zero or if its value | |||
5848 | /// falls within the specified range (L, H]. | |||
5849 | static bool isUndefOrZeroOrInRange(ArrayRef<int> Mask, int Low, int Hi) { | |||
5850 | return llvm::all_of( | |||
5851 | Mask, [Low, Hi](int M) { return isUndefOrZeroOrInRange(M, Low, Hi); }); | |||
5852 | } | |||
5853 | ||||
5854 | /// Return true if every element in Mask, beginning | |||
5855 | /// from position Pos and ending in Pos + Size, falls within the specified | |||
5856 | /// sequence (Low, Low + Step, ..., Low + (Size - 1) * Step) or is undef. | |||
5857 | static bool isSequentialOrUndefInRange(ArrayRef<int> Mask, unsigned Pos, | |||
5858 | unsigned Size, int Low, int Step = 1) { | |||
5859 | for (unsigned i = Pos, e = Pos + Size; i != e; ++i, Low += Step) | |||
5860 | if (!isUndefOrEqual(Mask[i], Low)) | |||
5861 | return false; | |||
5862 | return true; | |||
5863 | } | |||
5864 | ||||
5865 | /// Return true if every element in Mask, beginning | |||
5866 | /// from position Pos and ending in Pos+Size, falls within the specified | |||
5867 | /// sequential range (Low, Low+Size], or is undef or is zero. | |||
5868 | static bool isSequentialOrUndefOrZeroInRange(ArrayRef<int> Mask, unsigned Pos, | |||
5869 | unsigned Size, int Low, | |||
5870 | int Step = 1) { | |||
5871 | for (unsigned i = Pos, e = Pos + Size; i != e; ++i, Low += Step) | |||
5872 | if (!isUndefOrZero(Mask[i]) && Mask[i] != Low) | |||
5873 | return false; | |||
5874 | return true; | |||
5875 | } | |||
5876 | ||||
5877 | /// Return true if every element in Mask, beginning | |||
5878 | /// from position Pos and ending in Pos+Size is undef or is zero. | |||
5879 | static bool isUndefOrZeroInRange(ArrayRef<int> Mask, unsigned Pos, | |||
5880 | unsigned Size) { | |||
5881 | return llvm::all_of(Mask.slice(Pos, Size), | |||
5882 | [](int M) { return isUndefOrZero(M); }); | |||
5883 | } | |||
5884 | ||||
5885 | /// Helper function to test whether a shuffle mask could be | |||
5886 | /// simplified by widening the elements being shuffled. | |||
5887 | /// | |||
5888 | /// Appends the mask for wider elements in WidenedMask if valid. Otherwise | |||
5889 | /// leaves it in an unspecified state. | |||
5890 | /// | |||
5891 | /// NOTE: This must handle normal vector shuffle masks and *target* vector | |||
5892 | /// shuffle masks. The latter have the special property of a '-2' representing | |||
5893 | /// a zero-ed lane of a vector. | |||
5894 | static bool canWidenShuffleElements(ArrayRef<int> Mask, | |||
5895 | SmallVectorImpl<int> &WidenedMask) { | |||
5896 | WidenedMask.assign(Mask.size() / 2, 0); | |||
5897 | for (int i = 0, Size = Mask.size(); i < Size; i += 2) { | |||
5898 | int M0 = Mask[i]; | |||
5899 | int M1 = Mask[i + 1]; | |||
5900 | ||||
5901 | // If both elements are undef, its trivial. | |||
5902 | if (M0 == SM_SentinelUndef && M1 == SM_SentinelUndef) { | |||
5903 | WidenedMask[i / 2] = SM_SentinelUndef; | |||
5904 | continue; | |||
5905 | } | |||
5906 | ||||
5907 | // Check for an undef mask and a mask value properly aligned to fit with | |||
5908 | // a pair of values. If we find such a case, use the non-undef mask's value. | |||
5909 | if (M0 == SM_SentinelUndef && M1 >= 0 && (M1 % 2) == 1) { | |||
5910 | WidenedMask[i / 2] = M1 / 2; | |||
5911 | continue; | |||
5912 | } | |||
5913 | if (M1 == SM_SentinelUndef && M0 >= 0 && (M0 % 2) == 0) { | |||
5914 | WidenedMask[i / 2] = M0 / 2; | |||
5915 | continue; | |||
5916 | } | |||
5917 | ||||
5918 | // When zeroing, we need to spread the zeroing across both lanes to widen. | |||
5919 | if (M0 == SM_SentinelZero || M1 == SM_SentinelZero) { | |||
5920 | if ((M0 == SM_SentinelZero || M0 == SM_SentinelUndef) && | |||
5921 | (M1 == SM_SentinelZero || M1 == SM_SentinelUndef)) { | |||
5922 | WidenedMask[i / 2] = SM_SentinelZero; | |||
5923 | continue; | |||
5924 | } | |||
5925 | return false; | |||
5926 | } | |||
5927 | ||||
5928 | // Finally check if the two mask values are adjacent and aligned with | |||
5929 | // a pair. | |||
5930 | if (M0 != SM_SentinelUndef && (M0 % 2) == 0 && (M0 + 1) == M1) { | |||
5931 | WidenedMask[i / 2] = M0 / 2; | |||
5932 | continue; | |||
5933 | } | |||
5934 | ||||
5935 | // Otherwise we can't safely widen the elements used in this shuffle. | |||
5936 | return false; | |||
5937 | } | |||
5938 | assert(WidenedMask.size() == Mask.size() / 2 &&(static_cast <bool> (WidenedMask.size() == Mask.size() / 2 && "Incorrect size of mask after widening the elements!" ) ? void (0) : __assert_fail ("WidenedMask.size() == Mask.size() / 2 && \"Incorrect size of mask after widening the elements!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 5939, __extension__ __PRETTY_FUNCTION__)) | |||
5939 | "Incorrect size of mask after widening the elements!")(static_cast <bool> (WidenedMask.size() == Mask.size() / 2 && "Incorrect size of mask after widening the elements!" ) ? void (0) : __assert_fail ("WidenedMask.size() == Mask.size() / 2 && \"Incorrect size of mask after widening the elements!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 5939, __extension__ __PRETTY_FUNCTION__)); | |||
5940 | ||||
5941 | return true; | |||
5942 | } | |||
5943 | ||||
5944 | static bool canWidenShuffleElements(ArrayRef<int> Mask, | |||
5945 | const APInt &Zeroable, | |||
5946 | bool V2IsZero, | |||
5947 | SmallVectorImpl<int> &WidenedMask) { | |||
5948 | // Create an alternative mask with info about zeroable elements. | |||
5949 | // Here we do not set undef elements as zeroable. | |||
5950 | SmallVector<int, 64> ZeroableMask(Mask.begin(), Mask.end()); | |||
5951 | if (V2IsZero) { | |||
5952 | assert(!Zeroable.isZero() && "V2's non-undef elements are used?!")(static_cast <bool> (!Zeroable.isZero() && "V2's non-undef elements are used?!" ) ? void (0) : __assert_fail ("!Zeroable.isZero() && \"V2's non-undef elements are used?!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 5952, __extension__ __PRETTY_FUNCTION__)); | |||
5953 | for (int i = 0, Size = Mask.size(); i != Size; ++i) | |||
5954 | if (Mask[i] != SM_SentinelUndef && Zeroable[i]) | |||
5955 | ZeroableMask[i] = SM_SentinelZero; | |||
5956 | } | |||
5957 | return canWidenShuffleElements(ZeroableMask, WidenedMask); | |||
5958 | } | |||
5959 | ||||
5960 | static bool canWidenShuffleElements(ArrayRef<int> Mask) { | |||
5961 | SmallVector<int, 32> WidenedMask; | |||
5962 | return canWidenShuffleElements(Mask, WidenedMask); | |||
5963 | } | |||
5964 | ||||
5965 | // Attempt to narrow/widen shuffle mask until it matches the target number of | |||
5966 | // elements. | |||
5967 | static bool scaleShuffleElements(ArrayRef<int> Mask, unsigned NumDstElts, | |||
5968 | SmallVectorImpl<int> &ScaledMask) { | |||
5969 | unsigned NumSrcElts = Mask.size(); | |||
5970 | assert(((NumSrcElts % NumDstElts) == 0 || (NumDstElts % NumSrcElts) == 0) &&(static_cast <bool> (((NumSrcElts % NumDstElts) == 0 || (NumDstElts % NumSrcElts) == 0) && "Illegal shuffle scale factor" ) ? void (0) : __assert_fail ("((NumSrcElts % NumDstElts) == 0 || (NumDstElts % NumSrcElts) == 0) && \"Illegal shuffle scale factor\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 5971, __extension__ __PRETTY_FUNCTION__)) | |||
5971 | "Illegal shuffle scale factor")(static_cast <bool> (((NumSrcElts % NumDstElts) == 0 || (NumDstElts % NumSrcElts) == 0) && "Illegal shuffle scale factor" ) ? void (0) : __assert_fail ("((NumSrcElts % NumDstElts) == 0 || (NumDstElts % NumSrcElts) == 0) && \"Illegal shuffle scale factor\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 5971, __extension__ __PRETTY_FUNCTION__)); | |||
5972 | ||||
5973 | // Narrowing is guaranteed to work. | |||
5974 | if (NumDstElts >= NumSrcElts) { | |||
5975 | int Scale = NumDstElts / NumSrcElts; | |||
5976 | llvm::narrowShuffleMaskElts(Scale, Mask, ScaledMask); | |||
5977 | return true; | |||
5978 | } | |||
5979 | ||||
5980 | // We have to repeat the widening until we reach the target size, but we can | |||
5981 | // split out the first widening as it sets up ScaledMask for us. | |||
5982 | if (canWidenShuffleElements(Mask, ScaledMask)) { | |||
5983 | while (ScaledMask.size() > NumDstElts) { | |||
5984 | SmallVector<int, 16> WidenedMask; | |||
5985 | if (!canWidenShuffleElements(ScaledMask, WidenedMask)) | |||
5986 | return false; | |||
5987 | ScaledMask = std::move(WidenedMask); | |||
5988 | } | |||
5989 | return true; | |||
5990 | } | |||
5991 | ||||
5992 | return false; | |||
5993 | } | |||
5994 | ||||
5995 | /// Returns true if Elt is a constant zero or a floating point constant +0.0. | |||
5996 | bool X86::isZeroNode(SDValue Elt) { | |||
5997 | return isNullConstant(Elt) || isNullFPConstant(Elt); | |||
5998 | } | |||
5999 | ||||
6000 | // Build a vector of constants. | |||
6001 | // Use an UNDEF node if MaskElt == -1. | |||
6002 | // Split 64-bit constants in the 32-bit mode. | |||
6003 | static SDValue getConstVector(ArrayRef<int> Values, MVT VT, SelectionDAG &DAG, | |||
6004 | const SDLoc &dl, bool IsMask = false) { | |||
6005 | ||||
6006 | SmallVector<SDValue, 32> Ops; | |||
6007 | bool Split = false; | |||
6008 | ||||
6009 | MVT ConstVecVT = VT; | |||
6010 | unsigned NumElts = VT.getVectorNumElements(); | |||
6011 | bool In64BitMode = DAG.getTargetLoweringInfo().isTypeLegal(MVT::i64); | |||
6012 | if (!In64BitMode && VT.getVectorElementType() == MVT::i64) { | |||
6013 | ConstVecVT = MVT::getVectorVT(MVT::i32, NumElts * 2); | |||
6014 | Split = true; | |||
6015 | } | |||
6016 | ||||
6017 | MVT EltVT = ConstVecVT.getVectorElementType(); | |||
6018 | for (unsigned i = 0; i < NumElts; ++i) { | |||
6019 | bool IsUndef = Values[i] < 0 && IsMask; | |||
6020 | SDValue OpNode = IsUndef ? DAG.getUNDEF(EltVT) : | |||
6021 | DAG.getConstant(Values[i], dl, EltVT); | |||
6022 | Ops.push_back(OpNode); | |||
6023 | if (Split) | |||
6024 | Ops.push_back(IsUndef ? DAG.getUNDEF(EltVT) : | |||
6025 | DAG.getConstant(0, dl, EltVT)); | |||
6026 | } | |||
6027 | SDValue ConstsNode = DAG.getBuildVector(ConstVecVT, dl, Ops); | |||
6028 | if (Split) | |||
6029 | ConstsNode = DAG.getBitcast(VT, ConstsNode); | |||
6030 | return ConstsNode; | |||
6031 | } | |||
6032 | ||||
6033 | static SDValue getConstVector(ArrayRef<APInt> Bits, APInt &Undefs, | |||
6034 | MVT VT, SelectionDAG &DAG, const SDLoc &dl) { | |||
6035 | assert(Bits.size() == Undefs.getBitWidth() &&(static_cast <bool> (Bits.size() == Undefs.getBitWidth( ) && "Unequal constant and undef arrays") ? void (0) : __assert_fail ("Bits.size() == Undefs.getBitWidth() && \"Unequal constant and undef arrays\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6036, __extension__ __PRETTY_FUNCTION__)) | |||
6036 | "Unequal constant and undef arrays")(static_cast <bool> (Bits.size() == Undefs.getBitWidth( ) && "Unequal constant and undef arrays") ? void (0) : __assert_fail ("Bits.size() == Undefs.getBitWidth() && \"Unequal constant and undef arrays\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6036, __extension__ __PRETTY_FUNCTION__)); | |||
6037 | SmallVector<SDValue, 32> Ops; | |||
6038 | bool Split = false; | |||
6039 | ||||
6040 | MVT ConstVecVT = VT; | |||
6041 | unsigned NumElts = VT.getVectorNumElements(); | |||
6042 | bool In64BitMode = DAG.getTargetLoweringInfo().isTypeLegal(MVT::i64); | |||
6043 | if (!In64BitMode && VT.getVectorElementType() == MVT::i64) { | |||
6044 | ConstVecVT = MVT::getVectorVT(MVT::i32, NumElts * 2); | |||
6045 | Split = true; | |||
6046 | } | |||
6047 | ||||
6048 | MVT EltVT = ConstVecVT.getVectorElementType(); | |||
6049 | for (unsigned i = 0, e = Bits.size(); i != e; ++i) { | |||
6050 | if (Undefs[i]) { | |||
6051 | Ops.append(Split ? 2 : 1, DAG.getUNDEF(EltVT)); | |||
6052 | continue; | |||
6053 | } | |||
6054 | const APInt &V = Bits[i]; | |||
6055 | assert(V.getBitWidth() == VT.getScalarSizeInBits() && "Unexpected sizes")(static_cast <bool> (V.getBitWidth() == VT.getScalarSizeInBits () && "Unexpected sizes") ? void (0) : __assert_fail ( "V.getBitWidth() == VT.getScalarSizeInBits() && \"Unexpected sizes\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6055, __extension__ __PRETTY_FUNCTION__)); | |||
6056 | if (Split) { | |||
6057 | Ops.push_back(DAG.getConstant(V.trunc(32), dl, EltVT)); | |||
6058 | Ops.push_back(DAG.getConstant(V.lshr(32).trunc(32), dl, EltVT)); | |||
6059 | } else if (EltVT == MVT::f32) { | |||
6060 | APFloat FV(APFloat::IEEEsingle(), V); | |||
6061 | Ops.push_back(DAG.getConstantFP(FV, dl, EltVT)); | |||
6062 | } else if (EltVT == MVT::f64) { | |||
6063 | APFloat FV(APFloat::IEEEdouble(), V); | |||
6064 | Ops.push_back(DAG.getConstantFP(FV, dl, EltVT)); | |||
6065 | } else { | |||
6066 | Ops.push_back(DAG.getConstant(V, dl, EltVT)); | |||
6067 | } | |||
6068 | } | |||
6069 | ||||
6070 | SDValue ConstsNode = DAG.getBuildVector(ConstVecVT, dl, Ops); | |||
6071 | return DAG.getBitcast(VT, ConstsNode); | |||
6072 | } | |||
6073 | ||||
6074 | /// Returns a vector of specified type with all zero elements. | |||
6075 | static SDValue getZeroVector(MVT VT, const X86Subtarget &Subtarget, | |||
6076 | SelectionDAG &DAG, const SDLoc &dl) { | |||
6077 | assert((VT.is128BitVector() || VT.is256BitVector() || VT.is512BitVector() ||(static_cast <bool> ((VT.is128BitVector() || VT.is256BitVector () || VT.is512BitVector() || VT.getVectorElementType() == MVT ::i1) && "Unexpected vector type") ? void (0) : __assert_fail ("(VT.is128BitVector() || VT.is256BitVector() || VT.is512BitVector() || VT.getVectorElementType() == MVT::i1) && \"Unexpected vector type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6079, __extension__ __PRETTY_FUNCTION__)) | |||
6078 | VT.getVectorElementType() == MVT::i1) &&(static_cast <bool> ((VT.is128BitVector() || VT.is256BitVector () || VT.is512BitVector() || VT.getVectorElementType() == MVT ::i1) && "Unexpected vector type") ? void (0) : __assert_fail ("(VT.is128BitVector() || VT.is256BitVector() || VT.is512BitVector() || VT.getVectorElementType() == MVT::i1) && \"Unexpected vector type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6079, __extension__ __PRETTY_FUNCTION__)) | |||
6079 | "Unexpected vector type")(static_cast <bool> ((VT.is128BitVector() || VT.is256BitVector () || VT.is512BitVector() || VT.getVectorElementType() == MVT ::i1) && "Unexpected vector type") ? void (0) : __assert_fail ("(VT.is128BitVector() || VT.is256BitVector() || VT.is512BitVector() || VT.getVectorElementType() == MVT::i1) && \"Unexpected vector type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6079, __extension__ __PRETTY_FUNCTION__)); | |||
6080 | ||||
6081 | // Try to build SSE/AVX zero vectors as <N x i32> bitcasted to their dest | |||
6082 | // type. This ensures they get CSE'd. But if the integer type is not | |||
6083 | // available, use a floating-point +0.0 instead. | |||
6084 | SDValue Vec; | |||
6085 | if (!Subtarget.hasSSE2() && VT.is128BitVector()) { | |||
6086 | Vec = DAG.getConstantFP(+0.0, dl, MVT::v4f32); | |||
6087 | } else if (VT.isFloatingPoint()) { | |||
6088 | Vec = DAG.getConstantFP(+0.0, dl, VT); | |||
6089 | } else if (VT.getVectorElementType() == MVT::i1) { | |||
6090 | assert((Subtarget.hasBWI() || VT.getVectorNumElements() <= 16) &&(static_cast <bool> ((Subtarget.hasBWI() || VT.getVectorNumElements () <= 16) && "Unexpected vector type") ? void (0) : __assert_fail ("(Subtarget.hasBWI() || VT.getVectorNumElements() <= 16) && \"Unexpected vector type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6091, __extension__ __PRETTY_FUNCTION__)) | |||
6091 | "Unexpected vector type")(static_cast <bool> ((Subtarget.hasBWI() || VT.getVectorNumElements () <= 16) && "Unexpected vector type") ? void (0) : __assert_fail ("(Subtarget.hasBWI() || VT.getVectorNumElements() <= 16) && \"Unexpected vector type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6091, __extension__ __PRETTY_FUNCTION__)); | |||
6092 | Vec = DAG.getConstant(0, dl, VT); | |||
6093 | } else { | |||
6094 | unsigned Num32BitElts = VT.getSizeInBits() / 32; | |||
6095 | Vec = DAG.getConstant(0, dl, MVT::getVectorVT(MVT::i32, Num32BitElts)); | |||
6096 | } | |||
6097 | return DAG.getBitcast(VT, Vec); | |||
6098 | } | |||
6099 | ||||
6100 | static SDValue extractSubVector(SDValue Vec, unsigned IdxVal, SelectionDAG &DAG, | |||
6101 | const SDLoc &dl, unsigned vectorWidth) { | |||
6102 | EVT VT = Vec.getValueType(); | |||
6103 | EVT ElVT = VT.getVectorElementType(); | |||
6104 | unsigned Factor = VT.getSizeInBits() / vectorWidth; | |||
6105 | EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT, | |||
6106 | VT.getVectorNumElements() / Factor); | |||
6107 | ||||
6108 | // Extract the relevant vectorWidth bits. Generate an EXTRACT_SUBVECTOR | |||
6109 | unsigned ElemsPerChunk = vectorWidth / ElVT.getSizeInBits(); | |||
6110 | assert(isPowerOf2_32(ElemsPerChunk) && "Elements per chunk not power of 2")(static_cast <bool> (isPowerOf2_32(ElemsPerChunk) && "Elements per chunk not power of 2") ? void (0) : __assert_fail ("isPowerOf2_32(ElemsPerChunk) && \"Elements per chunk not power of 2\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6110, __extension__ __PRETTY_FUNCTION__)); | |||
6111 | ||||
6112 | // This is the index of the first element of the vectorWidth-bit chunk | |||
6113 | // we want. Since ElemsPerChunk is a power of 2 just need to clear bits. | |||
6114 | IdxVal &= ~(ElemsPerChunk - 1); | |||
6115 | ||||
6116 | // If the input is a buildvector just emit a smaller one. | |||
6117 | if (Vec.getOpcode() == ISD::BUILD_VECTOR) | |||
6118 | return DAG.getBuildVector(ResultVT, dl, | |||
6119 | Vec->ops().slice(IdxVal, ElemsPerChunk)); | |||
6120 | ||||
6121 | SDValue VecIdx = DAG.getIntPtrConstant(IdxVal, dl); | |||
6122 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec, VecIdx); | |||
6123 | } | |||
6124 | ||||
6125 | /// Generate a DAG to grab 128-bits from a vector > 128 bits. This | |||
6126 | /// sets things up to match to an AVX VEXTRACTF128 / VEXTRACTI128 | |||
6127 | /// or AVX-512 VEXTRACTF32x4 / VEXTRACTI32x4 | |||
6128 | /// instructions or a simple subregister reference. Idx is an index in the | |||
6129 | /// 128 bits we want. It need not be aligned to a 128-bit boundary. That makes | |||
6130 | /// lowering EXTRACT_VECTOR_ELT operations easier. | |||
6131 | static SDValue extract128BitVector(SDValue Vec, unsigned IdxVal, | |||
6132 | SelectionDAG &DAG, const SDLoc &dl) { | |||
6133 | assert((Vec.getValueType().is256BitVector() ||(static_cast <bool> ((Vec.getValueType().is256BitVector () || Vec.getValueType().is512BitVector()) && "Unexpected vector size!" ) ? void (0) : __assert_fail ("(Vec.getValueType().is256BitVector() || Vec.getValueType().is512BitVector()) && \"Unexpected vector size!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6134, __extension__ __PRETTY_FUNCTION__)) | |||
6134 | Vec.getValueType().is512BitVector()) && "Unexpected vector size!")(static_cast <bool> ((Vec.getValueType().is256BitVector () || Vec.getValueType().is512BitVector()) && "Unexpected vector size!" ) ? void (0) : __assert_fail ("(Vec.getValueType().is256BitVector() || Vec.getValueType().is512BitVector()) && \"Unexpected vector size!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6134, __extension__ __PRETTY_FUNCTION__)); | |||
6135 | return extractSubVector(Vec, IdxVal, DAG, dl, 128); | |||
6136 | } | |||
6137 | ||||
6138 | /// Generate a DAG to grab 256-bits from a 512-bit vector. | |||
6139 | static SDValue extract256BitVector(SDValue Vec, unsigned IdxVal, | |||
6140 | SelectionDAG &DAG, const SDLoc &dl) { | |||
6141 | assert(Vec.getValueType().is512BitVector() && "Unexpected vector size!")(static_cast <bool> (Vec.getValueType().is512BitVector( ) && "Unexpected vector size!") ? void (0) : __assert_fail ("Vec.getValueType().is512BitVector() && \"Unexpected vector size!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6141, __extension__ __PRETTY_FUNCTION__)); | |||
6142 | return extractSubVector(Vec, IdxVal, DAG, dl, 256); | |||
6143 | } | |||
6144 | ||||
6145 | static SDValue insertSubVector(SDValue Result, SDValue Vec, unsigned IdxVal, | |||
6146 | SelectionDAG &DAG, const SDLoc &dl, | |||
6147 | unsigned vectorWidth) { | |||
6148 | assert((vectorWidth == 128 || vectorWidth == 256) &&(static_cast <bool> ((vectorWidth == 128 || vectorWidth == 256) && "Unsupported vector width") ? void (0) : __assert_fail ("(vectorWidth == 128 || vectorWidth == 256) && \"Unsupported vector width\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6149, __extension__ __PRETTY_FUNCTION__)) | |||
6149 | "Unsupported vector width")(static_cast <bool> ((vectorWidth == 128 || vectorWidth == 256) && "Unsupported vector width") ? void (0) : __assert_fail ("(vectorWidth == 128 || vectorWidth == 256) && \"Unsupported vector width\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6149, __extension__ __PRETTY_FUNCTION__)); | |||
6150 | // Inserting UNDEF is Result | |||
6151 | if (Vec.isUndef()) | |||
6152 | return Result; | |||
6153 | EVT VT = Vec.getValueType(); | |||
6154 | EVT ElVT = VT.getVectorElementType(); | |||
6155 | EVT ResultVT = Result.getValueType(); | |||
6156 | ||||
6157 | // Insert the relevant vectorWidth bits. | |||
6158 | unsigned ElemsPerChunk = vectorWidth/ElVT.getSizeInBits(); | |||
6159 | assert(isPowerOf2_32(ElemsPerChunk) && "Elements per chunk not power of 2")(static_cast <bool> (isPowerOf2_32(ElemsPerChunk) && "Elements per chunk not power of 2") ? void (0) : __assert_fail ("isPowerOf2_32(ElemsPerChunk) && \"Elements per chunk not power of 2\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6159, __extension__ __PRETTY_FUNCTION__)); | |||
6160 | ||||
6161 | // This is the index of the first element of the vectorWidth-bit chunk | |||
6162 | // we want. Since ElemsPerChunk is a power of 2 just need to clear bits. | |||
6163 | IdxVal &= ~(ElemsPerChunk - 1); | |||
6164 | ||||
6165 | SDValue VecIdx = DAG.getIntPtrConstant(IdxVal, dl); | |||
6166 | return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec, VecIdx); | |||
6167 | } | |||
6168 | ||||
6169 | /// Generate a DAG to put 128-bits into a vector > 128 bits. This | |||
6170 | /// sets things up to match to an AVX VINSERTF128/VINSERTI128 or | |||
6171 | /// AVX-512 VINSERTF32x4/VINSERTI32x4 instructions or a | |||
6172 | /// simple superregister reference. Idx is an index in the 128 bits | |||
6173 | /// we want. It need not be aligned to a 128-bit boundary. That makes | |||
6174 | /// lowering INSERT_VECTOR_ELT operations easier. | |||
6175 | static SDValue insert128BitVector(SDValue Result, SDValue Vec, unsigned IdxVal, | |||
6176 | SelectionDAG &DAG, const SDLoc &dl) { | |||
6177 | assert(Vec.getValueType().is128BitVector() && "Unexpected vector size!")(static_cast <bool> (Vec.getValueType().is128BitVector( ) && "Unexpected vector size!") ? void (0) : __assert_fail ("Vec.getValueType().is128BitVector() && \"Unexpected vector size!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6177, __extension__ __PRETTY_FUNCTION__)); | |||
6178 | return insertSubVector(Result, Vec, IdxVal, DAG, dl, 128); | |||
6179 | } | |||
6180 | ||||
6181 | /// Widen a vector to a larger size with the same scalar type, with the new | |||
6182 | /// elements either zero or undef. | |||
6183 | static SDValue widenSubVector(MVT VT, SDValue Vec, bool ZeroNewElements, | |||
6184 | const X86Subtarget &Subtarget, SelectionDAG &DAG, | |||
6185 | const SDLoc &dl) { | |||
6186 | assert(Vec.getValueSizeInBits().getFixedSize() < VT.getFixedSizeInBits() &&(static_cast <bool> (Vec.getValueSizeInBits().getFixedSize () < VT.getFixedSizeInBits() && Vec.getValueType() .getScalarType() == VT.getScalarType() && "Unsupported vector widening type" ) ? void (0) : __assert_fail ("Vec.getValueSizeInBits().getFixedSize() < VT.getFixedSizeInBits() && Vec.getValueType().getScalarType() == VT.getScalarType() && \"Unsupported vector widening type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6188, __extension__ __PRETTY_FUNCTION__)) | |||
6187 | Vec.getValueType().getScalarType() == VT.getScalarType() &&(static_cast <bool> (Vec.getValueSizeInBits().getFixedSize () < VT.getFixedSizeInBits() && Vec.getValueType() .getScalarType() == VT.getScalarType() && "Unsupported vector widening type" ) ? void (0) : __assert_fail ("Vec.getValueSizeInBits().getFixedSize() < VT.getFixedSizeInBits() && Vec.getValueType().getScalarType() == VT.getScalarType() && \"Unsupported vector widening type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6188, __extension__ __PRETTY_FUNCTION__)) | |||
6188 | "Unsupported vector widening type")(static_cast <bool> (Vec.getValueSizeInBits().getFixedSize () < VT.getFixedSizeInBits() && Vec.getValueType() .getScalarType() == VT.getScalarType() && "Unsupported vector widening type" ) ? void (0) : __assert_fail ("Vec.getValueSizeInBits().getFixedSize() < VT.getFixedSizeInBits() && Vec.getValueType().getScalarType() == VT.getScalarType() && \"Unsupported vector widening type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6188, __extension__ __PRETTY_FUNCTION__)); | |||
6189 | SDValue Res = ZeroNewElements ? getZeroVector(VT, Subtarget, DAG, dl) | |||
6190 | : DAG.getUNDEF(VT); | |||
6191 | return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, VT, Res, Vec, | |||
6192 | DAG.getIntPtrConstant(0, dl)); | |||
6193 | } | |||
6194 | ||||
6195 | /// Widen a vector to a larger size with the same scalar type, with the new | |||
6196 | /// elements either zero or undef. | |||
6197 | static SDValue widenSubVector(SDValue Vec, bool ZeroNewElements, | |||
6198 | const X86Subtarget &Subtarget, SelectionDAG &DAG, | |||
6199 | const SDLoc &dl, unsigned WideSizeInBits) { | |||
6200 | assert(Vec.getValueSizeInBits() < WideSizeInBits &&(static_cast <bool> (Vec.getValueSizeInBits() < WideSizeInBits && (WideSizeInBits % Vec.getScalarValueSizeInBits()) == 0 && "Unsupported vector widening type") ? void ( 0) : __assert_fail ("Vec.getValueSizeInBits() < WideSizeInBits && (WideSizeInBits % Vec.getScalarValueSizeInBits()) == 0 && \"Unsupported vector widening type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6202, __extension__ __PRETTY_FUNCTION__)) | |||
6201 | (WideSizeInBits % Vec.getScalarValueSizeInBits()) == 0 &&(static_cast <bool> (Vec.getValueSizeInBits() < WideSizeInBits && (WideSizeInBits % Vec.getScalarValueSizeInBits()) == 0 && "Unsupported vector widening type") ? void ( 0) : __assert_fail ("Vec.getValueSizeInBits() < WideSizeInBits && (WideSizeInBits % Vec.getScalarValueSizeInBits()) == 0 && \"Unsupported vector widening type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6202, __extension__ __PRETTY_FUNCTION__)) | |||
6202 | "Unsupported vector widening type")(static_cast <bool> (Vec.getValueSizeInBits() < WideSizeInBits && (WideSizeInBits % Vec.getScalarValueSizeInBits()) == 0 && "Unsupported vector widening type") ? void ( 0) : __assert_fail ("Vec.getValueSizeInBits() < WideSizeInBits && (WideSizeInBits % Vec.getScalarValueSizeInBits()) == 0 && \"Unsupported vector widening type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6202, __extension__ __PRETTY_FUNCTION__)); | |||
6203 | unsigned WideNumElts = WideSizeInBits / Vec.getScalarValueSizeInBits(); | |||
6204 | MVT SVT = Vec.getSimpleValueType().getScalarType(); | |||
6205 | MVT VT = MVT::getVectorVT(SVT, WideNumElts); | |||
6206 | return widenSubVector(VT, Vec, ZeroNewElements, Subtarget, DAG, dl); | |||
6207 | } | |||
6208 | ||||
6209 | // Helper function to collect subvector ops that are concatenated together, | |||
6210 | // either by ISD::CONCAT_VECTORS or a ISD::INSERT_SUBVECTOR series. | |||
6211 | // The subvectors in Ops are guaranteed to be the same type. | |||
6212 | static bool collectConcatOps(SDNode *N, SmallVectorImpl<SDValue> &Ops) { | |||
6213 | assert(Ops.empty() && "Expected an empty ops vector")(static_cast <bool> (Ops.empty() && "Expected an empty ops vector" ) ? void (0) : __assert_fail ("Ops.empty() && \"Expected an empty ops vector\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6213, __extension__ __PRETTY_FUNCTION__)); | |||
6214 | ||||
6215 | if (N->getOpcode() == ISD::CONCAT_VECTORS) { | |||
6216 | Ops.append(N->op_begin(), N->op_end()); | |||
6217 | return true; | |||
6218 | } | |||
6219 | ||||
6220 | if (N->getOpcode() == ISD::INSERT_SUBVECTOR) { | |||
6221 | SDValue Src = N->getOperand(0); | |||
6222 | SDValue Sub = N->getOperand(1); | |||
6223 | const APInt &Idx = N->getConstantOperandAPInt(2); | |||
6224 | EVT VT = Src.getValueType(); | |||
6225 | EVT SubVT = Sub.getValueType(); | |||
6226 | ||||
6227 | // TODO - Handle more general insert_subvector chains. | |||
6228 | if (VT.getSizeInBits() == (SubVT.getSizeInBits() * 2) && | |||
6229 | Idx == (VT.getVectorNumElements() / 2)) { | |||
6230 | // insert_subvector(insert_subvector(undef, x, lo), y, hi) | |||
6231 | if (Src.getOpcode() == ISD::INSERT_SUBVECTOR && | |||
6232 | Src.getOperand(1).getValueType() == SubVT && | |||
6233 | isNullConstant(Src.getOperand(2))) { | |||
6234 | Ops.push_back(Src.getOperand(1)); | |||
6235 | Ops.push_back(Sub); | |||
6236 | return true; | |||
6237 | } | |||
6238 | // insert_subvector(x, extract_subvector(x, lo), hi) | |||
6239 | if (Sub.getOpcode() == ISD::EXTRACT_SUBVECTOR && | |||
6240 | Sub.getOperand(0) == Src && isNullConstant(Sub.getOperand(1))) { | |||
6241 | Ops.append(2, Sub); | |||
6242 | return true; | |||
6243 | } | |||
6244 | } | |||
6245 | } | |||
6246 | ||||
6247 | return false; | |||
6248 | } | |||
6249 | ||||
6250 | static std::pair<SDValue, SDValue> splitVector(SDValue Op, SelectionDAG &DAG, | |||
6251 | const SDLoc &dl) { | |||
6252 | EVT VT = Op.getValueType(); | |||
6253 | unsigned NumElems = VT.getVectorNumElements(); | |||
6254 | unsigned SizeInBits = VT.getSizeInBits(); | |||
6255 | assert((NumElems % 2) == 0 && (SizeInBits % 2) == 0 &&(static_cast <bool> ((NumElems % 2) == 0 && (SizeInBits % 2) == 0 && "Can't split odd sized vector") ? void ( 0) : __assert_fail ("(NumElems % 2) == 0 && (SizeInBits % 2) == 0 && \"Can't split odd sized vector\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6256, __extension__ __PRETTY_FUNCTION__)) | |||
6256 | "Can't split odd sized vector")(static_cast <bool> ((NumElems % 2) == 0 && (SizeInBits % 2) == 0 && "Can't split odd sized vector") ? void ( 0) : __assert_fail ("(NumElems % 2) == 0 && (SizeInBits % 2) == 0 && \"Can't split odd sized vector\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6256, __extension__ __PRETTY_FUNCTION__)); | |||
6257 | ||||
6258 | SDValue Lo = extractSubVector(Op, 0, DAG, dl, SizeInBits / 2); | |||
6259 | SDValue Hi = extractSubVector(Op, NumElems / 2, DAG, dl, SizeInBits / 2); | |||
6260 | return std::make_pair(Lo, Hi); | |||
6261 | } | |||
6262 | ||||
6263 | // Split an unary integer op into 2 half sized ops. | |||
6264 | static SDValue splitVectorIntUnary(SDValue Op, SelectionDAG &DAG) { | |||
6265 | EVT VT = Op.getValueType(); | |||
6266 | ||||
6267 | // Make sure we only try to split 256/512-bit types to avoid creating | |||
6268 | // narrow vectors. | |||
6269 | assert((Op.getOperand(0).getValueType().is256BitVector() ||(static_cast <bool> ((Op.getOperand(0).getValueType().is256BitVector () || Op.getOperand(0).getValueType().is512BitVector()) && (VT.is256BitVector() || VT.is512BitVector()) && "Unsupported VT!" ) ? void (0) : __assert_fail ("(Op.getOperand(0).getValueType().is256BitVector() || Op.getOperand(0).getValueType().is512BitVector()) && (VT.is256BitVector() || VT.is512BitVector()) && \"Unsupported VT!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6271, __extension__ __PRETTY_FUNCTION__)) | |||
6270 | Op.getOperand(0).getValueType().is512BitVector()) &&(static_cast <bool> ((Op.getOperand(0).getValueType().is256BitVector () || Op.getOperand(0).getValueType().is512BitVector()) && (VT.is256BitVector() || VT.is512BitVector()) && "Unsupported VT!" ) ? void (0) : __assert_fail ("(Op.getOperand(0).getValueType().is256BitVector() || Op.getOperand(0).getValueType().is512BitVector()) && (VT.is256BitVector() || VT.is512BitVector()) && \"Unsupported VT!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6271, __extension__ __PRETTY_FUNCTION__)) | |||
6271 | (VT.is256BitVector() || VT.is512BitVector()) && "Unsupported VT!")(static_cast <bool> ((Op.getOperand(0).getValueType().is256BitVector () || Op.getOperand(0).getValueType().is512BitVector()) && (VT.is256BitVector() || VT.is512BitVector()) && "Unsupported VT!" ) ? void (0) : __assert_fail ("(Op.getOperand(0).getValueType().is256BitVector() || Op.getOperand(0).getValueType().is512BitVector()) && (VT.is256BitVector() || VT.is512BitVector()) && \"Unsupported VT!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6271, __extension__ __PRETTY_FUNCTION__)); | |||
6272 | assert(Op.getOperand(0).getValueType().getVectorNumElements() ==(static_cast <bool> (Op.getOperand(0).getValueType().getVectorNumElements () == VT.getVectorNumElements() && "Unexpected VTs!") ? void (0) : __assert_fail ("Op.getOperand(0).getValueType().getVectorNumElements() == VT.getVectorNumElements() && \"Unexpected VTs!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6274, __extension__ __PRETTY_FUNCTION__)) | |||
6273 | VT.getVectorNumElements() &&(static_cast <bool> (Op.getOperand(0).getValueType().getVectorNumElements () == VT.getVectorNumElements() && "Unexpected VTs!") ? void (0) : __assert_fail ("Op.getOperand(0).getValueType().getVectorNumElements() == VT.getVectorNumElements() && \"Unexpected VTs!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6274, __extension__ __PRETTY_FUNCTION__)) | |||
6274 | "Unexpected VTs!")(static_cast <bool> (Op.getOperand(0).getValueType().getVectorNumElements () == VT.getVectorNumElements() && "Unexpected VTs!") ? void (0) : __assert_fail ("Op.getOperand(0).getValueType().getVectorNumElements() == VT.getVectorNumElements() && \"Unexpected VTs!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6274, __extension__ __PRETTY_FUNCTION__)); | |||
6275 | ||||
6276 | SDLoc dl(Op); | |||
6277 | ||||
6278 | // Extract the Lo/Hi vectors | |||
6279 | SDValue Lo, Hi; | |||
6280 | std::tie(Lo, Hi) = splitVector(Op.getOperand(0), DAG, dl); | |||
6281 | ||||
6282 | EVT LoVT, HiVT; | |||
6283 | std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VT); | |||
6284 | return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, | |||
6285 | DAG.getNode(Op.getOpcode(), dl, LoVT, Lo), | |||
6286 | DAG.getNode(Op.getOpcode(), dl, HiVT, Hi)); | |||
6287 | } | |||
6288 | ||||
6289 | /// Break a binary integer operation into 2 half sized ops and then | |||
6290 | /// concatenate the result back. | |||
6291 | static SDValue splitVectorIntBinary(SDValue Op, SelectionDAG &DAG) { | |||
6292 | EVT VT = Op.getValueType(); | |||
6293 | ||||
6294 | // Sanity check that all the types match. | |||
6295 | assert(Op.getOperand(0).getValueType() == VT &&(static_cast <bool> (Op.getOperand(0).getValueType() == VT && Op.getOperand(1).getValueType() == VT && "Unexpected VTs!") ? void (0) : __assert_fail ("Op.getOperand(0).getValueType() == VT && Op.getOperand(1).getValueType() == VT && \"Unexpected VTs!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6296, __extension__ __PRETTY_FUNCTION__)) | |||
6296 | Op.getOperand(1).getValueType() == VT && "Unexpected VTs!")(static_cast <bool> (Op.getOperand(0).getValueType() == VT && Op.getOperand(1).getValueType() == VT && "Unexpected VTs!") ? void (0) : __assert_fail ("Op.getOperand(0).getValueType() == VT && Op.getOperand(1).getValueType() == VT && \"Unexpected VTs!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6296, __extension__ __PRETTY_FUNCTION__)); | |||
6297 | assert((VT.is256BitVector() || VT.is512BitVector()) && "Unsupported VT!")(static_cast <bool> ((VT.is256BitVector() || VT.is512BitVector ()) && "Unsupported VT!") ? void (0) : __assert_fail ( "(VT.is256BitVector() || VT.is512BitVector()) && \"Unsupported VT!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6297, __extension__ __PRETTY_FUNCTION__)); | |||
6298 | ||||
6299 | SDLoc dl(Op); | |||
6300 | ||||
6301 | // Extract the LHS Lo/Hi vectors | |||
6302 | SDValue LHS1, LHS2; | |||
6303 | std::tie(LHS1, LHS2) = splitVector(Op.getOperand(0), DAG, dl); | |||
6304 | ||||
6305 | // Extract the RHS Lo/Hi vectors | |||
6306 | SDValue RHS1, RHS2; | |||
6307 | std::tie(RHS1, RHS2) = splitVector(Op.getOperand(1), DAG, dl); | |||
6308 | ||||
6309 | EVT LoVT, HiVT; | |||
6310 | std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VT); | |||
6311 | return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, | |||
6312 | DAG.getNode(Op.getOpcode(), dl, LoVT, LHS1, RHS1), | |||
6313 | DAG.getNode(Op.getOpcode(), dl, HiVT, LHS2, RHS2)); | |||
6314 | } | |||
6315 | ||||
6316 | // Helper for splitting operands of an operation to legal target size and | |||
6317 | // apply a function on each part. | |||
6318 | // Useful for operations that are available on SSE2 in 128-bit, on AVX2 in | |||
6319 | // 256-bit and on AVX512BW in 512-bit. The argument VT is the type used for | |||
6320 | // deciding if/how to split Ops. Ops elements do *not* have to be of type VT. | |||
6321 | // The argument Builder is a function that will be applied on each split part: | |||
6322 | // SDValue Builder(SelectionDAG&G, SDLoc, ArrayRef<SDValue>) | |||
6323 | template <typename F> | |||
6324 | SDValue SplitOpsAndApply(SelectionDAG &DAG, const X86Subtarget &Subtarget, | |||
6325 | const SDLoc &DL, EVT VT, ArrayRef<SDValue> Ops, | |||
6326 | F Builder, bool CheckBWI = true) { | |||
6327 | assert(Subtarget.hasSSE2() && "Target assumed to support at least SSE2")(static_cast <bool> (Subtarget.hasSSE2() && "Target assumed to support at least SSE2" ) ? void (0) : __assert_fail ("Subtarget.hasSSE2() && \"Target assumed to support at least SSE2\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6327, __extension__ __PRETTY_FUNCTION__)); | |||
6328 | unsigned NumSubs = 1; | |||
6329 | if ((CheckBWI && Subtarget.useBWIRegs()) || | |||
6330 | (!CheckBWI && Subtarget.useAVX512Regs())) { | |||
6331 | if (VT.getSizeInBits() > 512) { | |||
6332 | NumSubs = VT.getSizeInBits() / 512; | |||
6333 | assert((VT.getSizeInBits() % 512) == 0 && "Illegal vector size")(static_cast <bool> ((VT.getSizeInBits() % 512) == 0 && "Illegal vector size") ? void (0) : __assert_fail ("(VT.getSizeInBits() % 512) == 0 && \"Illegal vector size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6333, __extension__ __PRETTY_FUNCTION__)); | |||
6334 | } | |||
6335 | } else if (Subtarget.hasAVX2()) { | |||
6336 | if (VT.getSizeInBits() > 256) { | |||
6337 | NumSubs = VT.getSizeInBits() / 256; | |||
6338 | assert((VT.getSizeInBits() % 256) == 0 && "Illegal vector size")(static_cast <bool> ((VT.getSizeInBits() % 256) == 0 && "Illegal vector size") ? void (0) : __assert_fail ("(VT.getSizeInBits() % 256) == 0 && \"Illegal vector size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6338, __extension__ __PRETTY_FUNCTION__)); | |||
6339 | } | |||
6340 | } else { | |||
6341 | if (VT.getSizeInBits() > 128) { | |||
6342 | NumSubs = VT.getSizeInBits() / 128; | |||
6343 | assert((VT.getSizeInBits() % 128) == 0 && "Illegal vector size")(static_cast <bool> ((VT.getSizeInBits() % 128) == 0 && "Illegal vector size") ? void (0) : __assert_fail ("(VT.getSizeInBits() % 128) == 0 && \"Illegal vector size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6343, __extension__ __PRETTY_FUNCTION__)); | |||
6344 | } | |||
6345 | } | |||
6346 | ||||
6347 | if (NumSubs == 1) | |||
6348 | return Builder(DAG, DL, Ops); | |||
6349 | ||||
6350 | SmallVector<SDValue, 4> Subs; | |||
6351 | for (unsigned i = 0; i != NumSubs; ++i) { | |||
6352 | SmallVector<SDValue, 2> SubOps; | |||
6353 | for (SDValue Op : Ops) { | |||
6354 | EVT OpVT = Op.getValueType(); | |||
6355 | unsigned NumSubElts = OpVT.getVectorNumElements() / NumSubs; | |||
6356 | unsigned SizeSub = OpVT.getSizeInBits() / NumSubs; | |||
6357 | SubOps.push_back(extractSubVector(Op, i * NumSubElts, DAG, DL, SizeSub)); | |||
6358 | } | |||
6359 | Subs.push_back(Builder(DAG, DL, SubOps)); | |||
6360 | } | |||
6361 | return DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, Subs); | |||
6362 | } | |||
6363 | ||||
6364 | /// Insert i1-subvector to i1-vector. | |||
6365 | static SDValue insert1BitVector(SDValue Op, SelectionDAG &DAG, | |||
6366 | const X86Subtarget &Subtarget) { | |||
6367 | ||||
6368 | SDLoc dl(Op); | |||
6369 | SDValue Vec = Op.getOperand(0); | |||
6370 | SDValue SubVec = Op.getOperand(1); | |||
6371 | SDValue Idx = Op.getOperand(2); | |||
6372 | unsigned IdxVal = Op.getConstantOperandVal(2); | |||
6373 | ||||
6374 | // Inserting undef is a nop. We can just return the original vector. | |||
6375 | if (SubVec.isUndef()) | |||
6376 | return Vec; | |||
6377 | ||||
6378 | if (IdxVal == 0 && Vec.isUndef()) // the operation is legal | |||
6379 | return Op; | |||
6380 | ||||
6381 | MVT OpVT = Op.getSimpleValueType(); | |||
6382 | unsigned NumElems = OpVT.getVectorNumElements(); | |||
6383 | SDValue ZeroIdx = DAG.getIntPtrConstant(0, dl); | |||
6384 | ||||
6385 | // Extend to natively supported kshift. | |||
6386 | MVT WideOpVT = OpVT; | |||
6387 | if ((!Subtarget.hasDQI() && NumElems == 8) || NumElems < 8) | |||
6388 | WideOpVT = Subtarget.hasDQI() ? MVT::v8i1 : MVT::v16i1; | |||
6389 | ||||
6390 | // Inserting into the lsbs of a zero vector is legal. ISel will insert shifts | |||
6391 | // if necessary. | |||
6392 | if (IdxVal == 0 && ISD::isBuildVectorAllZeros(Vec.getNode())) { | |||
6393 | // May need to promote to a legal type. | |||
6394 | Op = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, WideOpVT, | |||
6395 | DAG.getConstant(0, dl, WideOpVT), | |||
6396 | SubVec, Idx); | |||
6397 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, OpVT, Op, ZeroIdx); | |||
6398 | } | |||
6399 | ||||
6400 | MVT SubVecVT = SubVec.getSimpleValueType(); | |||
6401 | unsigned SubVecNumElems = SubVecVT.getVectorNumElements(); | |||
6402 | assert(IdxVal + SubVecNumElems <= NumElems &&(static_cast <bool> (IdxVal + SubVecNumElems <= NumElems && IdxVal % SubVecVT.getSizeInBits() == 0 && "Unexpected index value in INSERT_SUBVECTOR") ? void (0) : __assert_fail ("IdxVal + SubVecNumElems <= NumElems && IdxVal % SubVecVT.getSizeInBits() == 0 && \"Unexpected index value in INSERT_SUBVECTOR\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6404, __extension__ __PRETTY_FUNCTION__)) | |||
6403 | IdxVal % SubVecVT.getSizeInBits() == 0 &&(static_cast <bool> (IdxVal + SubVecNumElems <= NumElems && IdxVal % SubVecVT.getSizeInBits() == 0 && "Unexpected index value in INSERT_SUBVECTOR") ? void (0) : __assert_fail ("IdxVal + SubVecNumElems <= NumElems && IdxVal % SubVecVT.getSizeInBits() == 0 && \"Unexpected index value in INSERT_SUBVECTOR\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6404, __extension__ __PRETTY_FUNCTION__)) | |||
6404 | "Unexpected index value in INSERT_SUBVECTOR")(static_cast <bool> (IdxVal + SubVecNumElems <= NumElems && IdxVal % SubVecVT.getSizeInBits() == 0 && "Unexpected index value in INSERT_SUBVECTOR") ? void (0) : __assert_fail ("IdxVal + SubVecNumElems <= NumElems && IdxVal % SubVecVT.getSizeInBits() == 0 && \"Unexpected index value in INSERT_SUBVECTOR\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6404, __extension__ __PRETTY_FUNCTION__)); | |||
6405 | ||||
6406 | SDValue Undef = DAG.getUNDEF(WideOpVT); | |||
6407 | ||||
6408 | if (IdxVal == 0) { | |||
6409 | // Zero lower bits of the Vec | |||
6410 | SDValue ShiftBits = DAG.getTargetConstant(SubVecNumElems, dl, MVT::i8); | |||
6411 | Vec = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, WideOpVT, Undef, Vec, | |||
6412 | ZeroIdx); | |||
6413 | Vec = DAG.getNode(X86ISD::KSHIFTR, dl, WideOpVT, Vec, ShiftBits); | |||
6414 | Vec = DAG.getNode(X86ISD::KSHIFTL, dl, WideOpVT, Vec, ShiftBits); | |||
6415 | // Merge them together, SubVec should be zero extended. | |||
6416 | SubVec = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, WideOpVT, | |||
6417 | DAG.getConstant(0, dl, WideOpVT), | |||
6418 | SubVec, ZeroIdx); | |||
6419 | Op = DAG.getNode(ISD::OR, dl, WideOpVT, Vec, SubVec); | |||
6420 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, OpVT, Op, ZeroIdx); | |||
6421 | } | |||
6422 | ||||
6423 | SubVec = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, WideOpVT, | |||
6424 | Undef, SubVec, ZeroIdx); | |||
6425 | ||||
6426 | if (Vec.isUndef()) { | |||
6427 | assert(IdxVal != 0 && "Unexpected index")(static_cast <bool> (IdxVal != 0 && "Unexpected index" ) ? void (0) : __assert_fail ("IdxVal != 0 && \"Unexpected index\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6427, __extension__ __PRETTY_FUNCTION__)); | |||
6428 | SubVec = DAG.getNode(X86ISD::KSHIFTL, dl, WideOpVT, SubVec, | |||
6429 | DAG.getTargetConstant(IdxVal, dl, MVT::i8)); | |||
6430 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, OpVT, SubVec, ZeroIdx); | |||
6431 | } | |||
6432 | ||||
6433 | if (ISD::isBuildVectorAllZeros(Vec.getNode())) { | |||
6434 | assert(IdxVal != 0 && "Unexpected index")(static_cast <bool> (IdxVal != 0 && "Unexpected index" ) ? void (0) : __assert_fail ("IdxVal != 0 && \"Unexpected index\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6434, __extension__ __PRETTY_FUNCTION__)); | |||
6435 | // If upper elements of Vec are known undef, then just shift into place. | |||
6436 | if (llvm::all_of(Vec->ops().slice(IdxVal + SubVecNumElems), | |||
6437 | [](SDValue V) { return V.isUndef(); })) { | |||
6438 | SubVec = DAG.getNode(X86ISD::KSHIFTL, dl, WideOpVT, SubVec, | |||
6439 | DAG.getTargetConstant(IdxVal, dl, MVT::i8)); | |||
6440 | } else { | |||
6441 | NumElems = WideOpVT.getVectorNumElements(); | |||
6442 | unsigned ShiftLeft = NumElems - SubVecNumElems; | |||
6443 | unsigned ShiftRight = NumElems - SubVecNumElems - IdxVal; | |||
6444 | SubVec = DAG.getNode(X86ISD::KSHIFTL, dl, WideOpVT, SubVec, | |||
6445 | DAG.getTargetConstant(ShiftLeft, dl, MVT::i8)); | |||
6446 | if (ShiftRight != 0) | |||
6447 | SubVec = DAG.getNode(X86ISD::KSHIFTR, dl, WideOpVT, SubVec, | |||
6448 | DAG.getTargetConstant(ShiftRight, dl, MVT::i8)); | |||
6449 | } | |||
6450 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, OpVT, SubVec, ZeroIdx); | |||
6451 | } | |||
6452 | ||||
6453 | // Simple case when we put subvector in the upper part | |||
6454 | if (IdxVal + SubVecNumElems == NumElems) { | |||
6455 | SubVec = DAG.getNode(X86ISD::KSHIFTL, dl, WideOpVT, SubVec, | |||
6456 | DAG.getTargetConstant(IdxVal, dl, MVT::i8)); | |||
6457 | if (SubVecNumElems * 2 == NumElems) { | |||
6458 | // Special case, use legal zero extending insert_subvector. This allows | |||
6459 | // isel to optimize when bits are known zero. | |||
6460 | Vec = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, SubVecVT, Vec, ZeroIdx); | |||
6461 | Vec = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, WideOpVT, | |||
6462 | DAG.getConstant(0, dl, WideOpVT), | |||
6463 | Vec, ZeroIdx); | |||
6464 | } else { | |||
6465 | // Otherwise use explicit shifts to zero the bits. | |||
6466 | Vec = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, WideOpVT, | |||
6467 | Undef, Vec, ZeroIdx); | |||
6468 | NumElems = WideOpVT.getVectorNumElements(); | |||
6469 | SDValue ShiftBits = DAG.getTargetConstant(NumElems - IdxVal, dl, MVT::i8); | |||
6470 | Vec = DAG.getNode(X86ISD::KSHIFTL, dl, WideOpVT, Vec, ShiftBits); | |||
6471 | Vec = DAG.getNode(X86ISD::KSHIFTR, dl, WideOpVT, Vec, ShiftBits); | |||
6472 | } | |||
6473 | Op = DAG.getNode(ISD::OR, dl, WideOpVT, Vec, SubVec); | |||
6474 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, OpVT, Op, ZeroIdx); | |||
6475 | } | |||
6476 | ||||
6477 | // Inserting into the middle is more complicated. | |||
6478 | ||||
6479 | NumElems = WideOpVT.getVectorNumElements(); | |||
6480 | ||||
6481 | // Widen the vector if needed. | |||
6482 | Vec = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, WideOpVT, Undef, Vec, ZeroIdx); | |||
6483 | ||||
6484 | unsigned ShiftLeft = NumElems - SubVecNumElems; | |||
6485 | unsigned ShiftRight = NumElems - SubVecNumElems - IdxVal; | |||
6486 | ||||
6487 | // Do an optimization for the the most frequently used types. | |||
6488 | if (WideOpVT != MVT::v64i1 || Subtarget.is64Bit()) { | |||
6489 | APInt Mask0 = APInt::getBitsSet(NumElems, IdxVal, IdxVal + SubVecNumElems); | |||
6490 | Mask0.flipAllBits(); | |||
6491 | SDValue CMask0 = DAG.getConstant(Mask0, dl, MVT::getIntegerVT(NumElems)); | |||
6492 | SDValue VMask0 = DAG.getNode(ISD::BITCAST, dl, WideOpVT, CMask0); | |||
6493 | Vec = DAG.getNode(ISD::AND, dl, WideOpVT, Vec, VMask0); | |||
6494 | SubVec = DAG.getNode(X86ISD::KSHIFTL, dl, WideOpVT, SubVec, | |||
6495 | DAG.getTargetConstant(ShiftLeft, dl, MVT::i8)); | |||
6496 | SubVec = DAG.getNode(X86ISD::KSHIFTR, dl, WideOpVT, SubVec, | |||
6497 | DAG.getTargetConstant(ShiftRight, dl, MVT::i8)); | |||
6498 | Op = DAG.getNode(ISD::OR, dl, WideOpVT, Vec, SubVec); | |||
6499 | ||||
6500 | // Reduce to original width if needed. | |||
6501 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, OpVT, Op, ZeroIdx); | |||
6502 | } | |||
6503 | ||||
6504 | // Clear the upper bits of the subvector and move it to its insert position. | |||
6505 | SubVec = DAG.getNode(X86ISD::KSHIFTL, dl, WideOpVT, SubVec, | |||
6506 | DAG.getTargetConstant(ShiftLeft, dl, MVT::i8)); | |||
6507 | SubVec = DAG.getNode(X86ISD::KSHIFTR, dl, WideOpVT, SubVec, | |||
6508 | DAG.getTargetConstant(ShiftRight, dl, MVT::i8)); | |||
6509 | ||||
6510 | // Isolate the bits below the insertion point. | |||
6511 | unsigned LowShift = NumElems - IdxVal; | |||
6512 | SDValue Low = DAG.getNode(X86ISD::KSHIFTL, dl, WideOpVT, Vec, | |||
6513 | DAG.getTargetConstant(LowShift, dl, MVT::i8)); | |||
6514 | Low = DAG.getNode(X86ISD::KSHIFTR, dl, WideOpVT, Low, | |||
6515 | DAG.getTargetConstant(LowShift, dl, MVT::i8)); | |||
6516 | ||||
6517 | // Isolate the bits after the last inserted bit. | |||
6518 | unsigned HighShift = IdxVal + SubVecNumElems; | |||
6519 | SDValue High = DAG.getNode(X86ISD::KSHIFTR, dl, WideOpVT, Vec, | |||
6520 | DAG.getTargetConstant(HighShift, dl, MVT::i8)); | |||
6521 | High = DAG.getNode(X86ISD::KSHIFTL, dl, WideOpVT, High, | |||
6522 | DAG.getTargetConstant(HighShift, dl, MVT::i8)); | |||
6523 | ||||
6524 | // Now OR all 3 pieces together. | |||
6525 | Vec = DAG.getNode(ISD::OR, dl, WideOpVT, Low, High); | |||
6526 | SubVec = DAG.getNode(ISD::OR, dl, WideOpVT, SubVec, Vec); | |||
6527 | ||||
6528 | // Reduce to original width if needed. | |||
6529 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, OpVT, SubVec, ZeroIdx); | |||
6530 | } | |||
6531 | ||||
6532 | static SDValue concatSubVectors(SDValue V1, SDValue V2, SelectionDAG &DAG, | |||
6533 | const SDLoc &dl) { | |||
6534 | assert(V1.getValueType() == V2.getValueType() && "subvector type mismatch")(static_cast <bool> (V1.getValueType() == V2.getValueType () && "subvector type mismatch") ? void (0) : __assert_fail ("V1.getValueType() == V2.getValueType() && \"subvector type mismatch\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6534, __extension__ __PRETTY_FUNCTION__)); | |||
6535 | EVT SubVT = V1.getValueType(); | |||
6536 | EVT SubSVT = SubVT.getScalarType(); | |||
6537 | unsigned SubNumElts = SubVT.getVectorNumElements(); | |||
6538 | unsigned SubVectorWidth = SubVT.getSizeInBits(); | |||
6539 | EVT VT = EVT::getVectorVT(*DAG.getContext(), SubSVT, 2 * SubNumElts); | |||
6540 | SDValue V = insertSubVector(DAG.getUNDEF(VT), V1, 0, DAG, dl, SubVectorWidth); | |||
6541 | return insertSubVector(V, V2, SubNumElts, DAG, dl, SubVectorWidth); | |||
6542 | } | |||
6543 | ||||
6544 | /// Returns a vector of specified type with all bits set. | |||
6545 | /// Always build ones vectors as <4 x i32>, <8 x i32> or <16 x i32>. | |||
6546 | /// Then bitcast to their original type, ensuring they get CSE'd. | |||
6547 | static SDValue getOnesVector(EVT VT, SelectionDAG &DAG, const SDLoc &dl) { | |||
6548 | assert((VT.is128BitVector() || VT.is256BitVector() || VT.is512BitVector()) &&(static_cast <bool> ((VT.is128BitVector() || VT.is256BitVector () || VT.is512BitVector()) && "Expected a 128/256/512-bit vector type" ) ? void (0) : __assert_fail ("(VT.is128BitVector() || VT.is256BitVector() || VT.is512BitVector()) && \"Expected a 128/256/512-bit vector type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6549, __extension__ __PRETTY_FUNCTION__)) | |||
6549 | "Expected a 128/256/512-bit vector type")(static_cast <bool> ((VT.is128BitVector() || VT.is256BitVector () || VT.is512BitVector()) && "Expected a 128/256/512-bit vector type" ) ? void (0) : __assert_fail ("(VT.is128BitVector() || VT.is256BitVector() || VT.is512BitVector()) && \"Expected a 128/256/512-bit vector type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6549, __extension__ __PRETTY_FUNCTION__)); | |||
6550 | ||||
6551 | APInt Ones = APInt::getAllOnes(32); | |||
6552 | unsigned NumElts = VT.getSizeInBits() / 32; | |||
6553 | SDValue Vec = DAG.getConstant(Ones, dl, MVT::getVectorVT(MVT::i32, NumElts)); | |||
6554 | return DAG.getBitcast(VT, Vec); | |||
6555 | } | |||
6556 | ||||
6557 | // Convert *_EXTEND_VECTOR_INREG to *_EXTEND opcode. | |||
6558 | static unsigned getOpcode_EXTEND(unsigned Opcode) { | |||
6559 | switch (Opcode) { | |||
6560 | case ISD::ANY_EXTEND: | |||
6561 | case ISD::ANY_EXTEND_VECTOR_INREG: | |||
6562 | return ISD::ANY_EXTEND; | |||
6563 | case ISD::ZERO_EXTEND: | |||
6564 | case ISD::ZERO_EXTEND_VECTOR_INREG: | |||
6565 | return ISD::ZERO_EXTEND; | |||
6566 | case ISD::SIGN_EXTEND: | |||
6567 | case ISD::SIGN_EXTEND_VECTOR_INREG: | |||
6568 | return ISD::SIGN_EXTEND; | |||
6569 | } | |||
6570 | llvm_unreachable("Unknown opcode")::llvm::llvm_unreachable_internal("Unknown opcode", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6570); | |||
6571 | } | |||
6572 | ||||
6573 | // Convert *_EXTEND to *_EXTEND_VECTOR_INREG opcode. | |||
6574 | static unsigned getOpcode_EXTEND_VECTOR_INREG(unsigned Opcode) { | |||
6575 | switch (Opcode) { | |||
6576 | case ISD::ANY_EXTEND: | |||
6577 | case ISD::ANY_EXTEND_VECTOR_INREG: | |||
6578 | return ISD::ANY_EXTEND_VECTOR_INREG; | |||
6579 | case ISD::ZERO_EXTEND: | |||
6580 | case ISD::ZERO_EXTEND_VECTOR_INREG: | |||
6581 | return ISD::ZERO_EXTEND_VECTOR_INREG; | |||
6582 | case ISD::SIGN_EXTEND: | |||
6583 | case ISD::SIGN_EXTEND_VECTOR_INREG: | |||
6584 | return ISD::SIGN_EXTEND_VECTOR_INREG; | |||
6585 | } | |||
6586 | llvm_unreachable("Unknown opcode")::llvm::llvm_unreachable_internal("Unknown opcode", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6586); | |||
6587 | } | |||
6588 | ||||
6589 | static SDValue getEXTEND_VECTOR_INREG(unsigned Opcode, const SDLoc &DL, EVT VT, | |||
6590 | SDValue In, SelectionDAG &DAG) { | |||
6591 | EVT InVT = In.getValueType(); | |||
6592 | assert(VT.isVector() && InVT.isVector() && "Expected vector VTs.")(static_cast <bool> (VT.isVector() && InVT.isVector () && "Expected vector VTs.") ? void (0) : __assert_fail ("VT.isVector() && InVT.isVector() && \"Expected vector VTs.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6592, __extension__ __PRETTY_FUNCTION__)); | |||
6593 | assert((ISD::ANY_EXTEND == Opcode || ISD::SIGN_EXTEND == Opcode ||(static_cast <bool> ((ISD::ANY_EXTEND == Opcode || ISD:: SIGN_EXTEND == Opcode || ISD::ZERO_EXTEND == Opcode) && "Unknown extension opcode") ? void (0) : __assert_fail ("(ISD::ANY_EXTEND == Opcode || ISD::SIGN_EXTEND == Opcode || ISD::ZERO_EXTEND == Opcode) && \"Unknown extension opcode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6595, __extension__ __PRETTY_FUNCTION__)) | |||
6594 | ISD::ZERO_EXTEND == Opcode) &&(static_cast <bool> ((ISD::ANY_EXTEND == Opcode || ISD:: SIGN_EXTEND == Opcode || ISD::ZERO_EXTEND == Opcode) && "Unknown extension opcode") ? void (0) : __assert_fail ("(ISD::ANY_EXTEND == Opcode || ISD::SIGN_EXTEND == Opcode || ISD::ZERO_EXTEND == Opcode) && \"Unknown extension opcode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6595, __extension__ __PRETTY_FUNCTION__)) | |||
6595 | "Unknown extension opcode")(static_cast <bool> ((ISD::ANY_EXTEND == Opcode || ISD:: SIGN_EXTEND == Opcode || ISD::ZERO_EXTEND == Opcode) && "Unknown extension opcode") ? void (0) : __assert_fail ("(ISD::ANY_EXTEND == Opcode || ISD::SIGN_EXTEND == Opcode || ISD::ZERO_EXTEND == Opcode) && \"Unknown extension opcode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6595, __extension__ __PRETTY_FUNCTION__)); | |||
6596 | ||||
6597 | // For 256-bit vectors, we only need the lower (128-bit) input half. | |||
6598 | // For 512-bit vectors, we only need the lower input half or quarter. | |||
6599 | if (InVT.getSizeInBits() > 128) { | |||
6600 | assert(VT.getSizeInBits() == InVT.getSizeInBits() &&(static_cast <bool> (VT.getSizeInBits() == InVT.getSizeInBits () && "Expected VTs to be the same size!") ? void (0) : __assert_fail ("VT.getSizeInBits() == InVT.getSizeInBits() && \"Expected VTs to be the same size!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6601, __extension__ __PRETTY_FUNCTION__)) | |||
6601 | "Expected VTs to be the same size!")(static_cast <bool> (VT.getSizeInBits() == InVT.getSizeInBits () && "Expected VTs to be the same size!") ? void (0) : __assert_fail ("VT.getSizeInBits() == InVT.getSizeInBits() && \"Expected VTs to be the same size!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6601, __extension__ __PRETTY_FUNCTION__)); | |||
6602 | unsigned Scale = VT.getScalarSizeInBits() / InVT.getScalarSizeInBits(); | |||
6603 | In = extractSubVector(In, 0, DAG, DL, | |||
6604 | std::max(128U, (unsigned)VT.getSizeInBits() / Scale)); | |||
6605 | InVT = In.getValueType(); | |||
6606 | } | |||
6607 | ||||
6608 | if (VT.getVectorNumElements() != InVT.getVectorNumElements()) | |||
6609 | Opcode = getOpcode_EXTEND_VECTOR_INREG(Opcode); | |||
6610 | ||||
6611 | return DAG.getNode(Opcode, DL, VT, In); | |||
6612 | } | |||
6613 | ||||
6614 | // Match (xor X, -1) -> X. | |||
6615 | // Match extract_subvector(xor X, -1) -> extract_subvector(X). | |||
6616 | // Match concat_vectors(xor X, -1, xor Y, -1) -> concat_vectors(X, Y). | |||
6617 | static SDValue IsNOT(SDValue V, SelectionDAG &DAG) { | |||
6618 | V = peekThroughBitcasts(V); | |||
6619 | if (V.getOpcode() == ISD::XOR && | |||
6620 | ISD::isBuildVectorAllOnes(V.getOperand(1).getNode())) | |||
6621 | return V.getOperand(0); | |||
6622 | if (V.getOpcode() == ISD::EXTRACT_SUBVECTOR && | |||
6623 | (isNullConstant(V.getOperand(1)) || V.getOperand(0).hasOneUse())) { | |||
6624 | if (SDValue Not = IsNOT(V.getOperand(0), DAG)) { | |||
6625 | Not = DAG.getBitcast(V.getOperand(0).getValueType(), Not); | |||
6626 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(Not), V.getValueType(), | |||
6627 | Not, V.getOperand(1)); | |||
6628 | } | |||
6629 | } | |||
6630 | SmallVector<SDValue, 2> CatOps; | |||
6631 | if (collectConcatOps(V.getNode(), CatOps)) { | |||
6632 | for (SDValue &CatOp : CatOps) { | |||
6633 | SDValue NotCat = IsNOT(CatOp, DAG); | |||
6634 | if (!NotCat) return SDValue(); | |||
6635 | CatOp = DAG.getBitcast(CatOp.getValueType(), NotCat); | |||
6636 | } | |||
6637 | return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(V), V.getValueType(), CatOps); | |||
6638 | } | |||
6639 | return SDValue(); | |||
6640 | } | |||
6641 | ||||
6642 | void llvm::createUnpackShuffleMask(EVT VT, SmallVectorImpl<int> &Mask, | |||
6643 | bool Lo, bool Unary) { | |||
6644 | assert(VT.getScalarType().isSimple() && (VT.getSizeInBits() % 128) == 0 &&(static_cast <bool> (VT.getScalarType().isSimple() && (VT.getSizeInBits() % 128) == 0 && "Illegal vector type to unpack" ) ? void (0) : __assert_fail ("VT.getScalarType().isSimple() && (VT.getSizeInBits() % 128) == 0 && \"Illegal vector type to unpack\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6645, __extension__ __PRETTY_FUNCTION__)) | |||
6645 | "Illegal vector type to unpack")(static_cast <bool> (VT.getScalarType().isSimple() && (VT.getSizeInBits() % 128) == 0 && "Illegal vector type to unpack" ) ? void (0) : __assert_fail ("VT.getScalarType().isSimple() && (VT.getSizeInBits() % 128) == 0 && \"Illegal vector type to unpack\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6645, __extension__ __PRETTY_FUNCTION__)); | |||
6646 | assert(Mask.empty() && "Expected an empty shuffle mask vector")(static_cast <bool> (Mask.empty() && "Expected an empty shuffle mask vector" ) ? void (0) : __assert_fail ("Mask.empty() && \"Expected an empty shuffle mask vector\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6646, __extension__ __PRETTY_FUNCTION__)); | |||
6647 | int NumElts = VT.getVectorNumElements(); | |||
6648 | int NumEltsInLane = 128 / VT.getScalarSizeInBits(); | |||
6649 | for (int i = 0; i < NumElts; ++i) { | |||
6650 | unsigned LaneStart = (i / NumEltsInLane) * NumEltsInLane; | |||
6651 | int Pos = (i % NumEltsInLane) / 2 + LaneStart; | |||
6652 | Pos += (Unary ? 0 : NumElts * (i % 2)); | |||
6653 | Pos += (Lo ? 0 : NumEltsInLane / 2); | |||
6654 | Mask.push_back(Pos); | |||
6655 | } | |||
6656 | } | |||
6657 | ||||
6658 | /// Similar to unpacklo/unpackhi, but without the 128-bit lane limitation | |||
6659 | /// imposed by AVX and specific to the unary pattern. Example: | |||
6660 | /// v8iX Lo --> <0, 0, 1, 1, 2, 2, 3, 3> | |||
6661 | /// v8iX Hi --> <4, 4, 5, 5, 6, 6, 7, 7> | |||
6662 | void llvm::createSplat2ShuffleMask(MVT VT, SmallVectorImpl<int> &Mask, | |||
6663 | bool Lo) { | |||
6664 | assert(Mask.empty() && "Expected an empty shuffle mask vector")(static_cast <bool> (Mask.empty() && "Expected an empty shuffle mask vector" ) ? void (0) : __assert_fail ("Mask.empty() && \"Expected an empty shuffle mask vector\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6664, __extension__ __PRETTY_FUNCTION__)); | |||
6665 | int NumElts = VT.getVectorNumElements(); | |||
6666 | for (int i = 0; i < NumElts; ++i) { | |||
6667 | int Pos = i / 2; | |||
6668 | Pos += (Lo ? 0 : NumElts / 2); | |||
6669 | Mask.push_back(Pos); | |||
6670 | } | |||
6671 | } | |||
6672 | ||||
6673 | /// Returns a vector_shuffle node for an unpackl operation. | |||
6674 | static SDValue getUnpackl(SelectionDAG &DAG, const SDLoc &dl, EVT VT, | |||
6675 | SDValue V1, SDValue V2) { | |||
6676 | SmallVector<int, 8> Mask; | |||
6677 | createUnpackShuffleMask(VT, Mask, /* Lo = */ true, /* Unary = */ false); | |||
6678 | return DAG.getVectorShuffle(VT, dl, V1, V2, Mask); | |||
6679 | } | |||
6680 | ||||
6681 | /// Returns a vector_shuffle node for an unpackh operation. | |||
6682 | static SDValue getUnpackh(SelectionDAG &DAG, const SDLoc &dl, EVT VT, | |||
6683 | SDValue V1, SDValue V2) { | |||
6684 | SmallVector<int, 8> Mask; | |||
6685 | createUnpackShuffleMask(VT, Mask, /* Lo = */ false, /* Unary = */ false); | |||
6686 | return DAG.getVectorShuffle(VT, dl, V1, V2, Mask); | |||
6687 | } | |||
6688 | ||||
6689 | /// Return a vector_shuffle of the specified vector of zero or undef vector. | |||
6690 | /// This produces a shuffle where the low element of V2 is swizzled into the | |||
6691 | /// zero/undef vector, landing at element Idx. | |||
6692 | /// This produces a shuffle mask like 4,1,2,3 (idx=0) or 0,1,2,4 (idx=3). | |||
6693 | static SDValue getShuffleVectorZeroOrUndef(SDValue V2, int Idx, | |||
6694 | bool IsZero, | |||
6695 | const X86Subtarget &Subtarget, | |||
6696 | SelectionDAG &DAG) { | |||
6697 | MVT VT = V2.getSimpleValueType(); | |||
6698 | SDValue V1 = IsZero | |||
6699 | ? getZeroVector(VT, Subtarget, DAG, SDLoc(V2)) : DAG.getUNDEF(VT); | |||
6700 | int NumElems = VT.getVectorNumElements(); | |||
6701 | SmallVector<int, 16> MaskVec(NumElems); | |||
6702 | for (int i = 0; i != NumElems; ++i) | |||
6703 | // If this is the insertion idx, put the low elt of V2 here. | |||
6704 | MaskVec[i] = (i == Idx) ? NumElems : i; | |||
6705 | return DAG.getVectorShuffle(VT, SDLoc(V2), V1, V2, MaskVec); | |||
6706 | } | |||
6707 | ||||
6708 | static const Constant *getTargetConstantFromBasePtr(SDValue Ptr) { | |||
6709 | if (Ptr.getOpcode() == X86ISD::Wrapper || | |||
6710 | Ptr.getOpcode() == X86ISD::WrapperRIP) | |||
6711 | Ptr = Ptr.getOperand(0); | |||
6712 | ||||
6713 | auto *CNode = dyn_cast<ConstantPoolSDNode>(Ptr); | |||
6714 | if (!CNode || CNode->isMachineConstantPoolEntry() || CNode->getOffset() != 0) | |||
6715 | return nullptr; | |||
6716 | ||||
6717 | return CNode->getConstVal(); | |||
6718 | } | |||
6719 | ||||
6720 | static const Constant *getTargetConstantFromNode(LoadSDNode *Load) { | |||
6721 | if (!Load || !ISD::isNormalLoad(Load)) | |||
6722 | return nullptr; | |||
6723 | return getTargetConstantFromBasePtr(Load->getBasePtr()); | |||
6724 | } | |||
6725 | ||||
6726 | static const Constant *getTargetConstantFromNode(SDValue Op) { | |||
6727 | Op = peekThroughBitcasts(Op); | |||
6728 | return getTargetConstantFromNode(dyn_cast<LoadSDNode>(Op)); | |||
6729 | } | |||
6730 | ||||
6731 | const Constant * | |||
6732 | X86TargetLowering::getTargetConstantFromLoad(LoadSDNode *LD) const { | |||
6733 | assert(LD && "Unexpected null LoadSDNode")(static_cast <bool> (LD && "Unexpected null LoadSDNode" ) ? void (0) : __assert_fail ("LD && \"Unexpected null LoadSDNode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6733, __extension__ __PRETTY_FUNCTION__)); | |||
6734 | return getTargetConstantFromNode(LD); | |||
6735 | } | |||
6736 | ||||
6737 | // Extract raw constant bits from constant pools. | |||
6738 | static bool getTargetConstantBitsFromNode(SDValue Op, unsigned EltSizeInBits, | |||
6739 | APInt &UndefElts, | |||
6740 | SmallVectorImpl<APInt> &EltBits, | |||
6741 | bool AllowWholeUndefs = true, | |||
6742 | bool AllowPartialUndefs = true) { | |||
6743 | assert(EltBits.empty() && "Expected an empty EltBits vector")(static_cast <bool> (EltBits.empty() && "Expected an empty EltBits vector" ) ? void (0) : __assert_fail ("EltBits.empty() && \"Expected an empty EltBits vector\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6743, __extension__ __PRETTY_FUNCTION__)); | |||
6744 | ||||
6745 | Op = peekThroughBitcasts(Op); | |||
6746 | ||||
6747 | EVT VT = Op.getValueType(); | |||
6748 | unsigned SizeInBits = VT.getSizeInBits(); | |||
6749 | assert((SizeInBits % EltSizeInBits) == 0 && "Can't split constant!")(static_cast <bool> ((SizeInBits % EltSizeInBits) == 0 && "Can't split constant!") ? void (0) : __assert_fail ("(SizeInBits % EltSizeInBits) == 0 && \"Can't split constant!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6749, __extension__ __PRETTY_FUNCTION__)); | |||
6750 | unsigned NumElts = SizeInBits / EltSizeInBits; | |||
6751 | ||||
6752 | // Bitcast a source array of element bits to the target size. | |||
6753 | auto CastBitData = [&](APInt &UndefSrcElts, ArrayRef<APInt> SrcEltBits) { | |||
6754 | unsigned NumSrcElts = UndefSrcElts.getBitWidth(); | |||
6755 | unsigned SrcEltSizeInBits = SrcEltBits[0].getBitWidth(); | |||
6756 | assert((NumSrcElts * SrcEltSizeInBits) == SizeInBits &&(static_cast <bool> ((NumSrcElts * SrcEltSizeInBits) == SizeInBits && "Constant bit sizes don't match") ? void (0) : __assert_fail ("(NumSrcElts * SrcEltSizeInBits) == SizeInBits && \"Constant bit sizes don't match\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6757, __extension__ __PRETTY_FUNCTION__)) | |||
6757 | "Constant bit sizes don't match")(static_cast <bool> ((NumSrcElts * SrcEltSizeInBits) == SizeInBits && "Constant bit sizes don't match") ? void (0) : __assert_fail ("(NumSrcElts * SrcEltSizeInBits) == SizeInBits && \"Constant bit sizes don't match\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 6757, __extension__ __PRETTY_FUNCTION__)); | |||
6758 | ||||
6759 | // Don't split if we don't allow undef bits. | |||
6760 | bool AllowUndefs = AllowWholeUndefs || AllowPartialUndefs; | |||
6761 | if (UndefSrcElts.getBoolValue() && !AllowUndefs) | |||
6762 | return false; | |||
6763 | ||||
6764 | // If we're already the right size, don't bother bitcasting. | |||
6765 | if (NumSrcElts == NumElts) { | |||
6766 | UndefElts = UndefSrcElts; | |||
6767 | EltBits.assign(SrcEltBits.begin(), SrcEltBits.end()); | |||
6768 | return true; | |||
6769 | } | |||
6770 | ||||
6771 | // Extract all the undef/constant element data and pack into single bitsets. | |||
6772 | APInt UndefBits(SizeInBits, 0); | |||
6773 | APInt MaskBits(SizeInBits, 0); | |||
6774 | ||||
6775 | for (unsigned i = 0; i != NumSrcElts; ++i) { | |||
6776 | unsigned BitOffset = i * SrcEltSizeInBits; | |||
6777 | if (UndefSrcElts[i]) | |||
6778 | UndefBits.setBits(BitOffset, BitOffset + SrcEltSizeInBits); | |||
6779 | MaskBits.insertBits(SrcEltBits[i], BitOffset); | |||
6780 | } | |||
6781 | ||||
6782 | // Split the undef/constant single bitset data into the target elements. | |||
6783 | UndefElts = APInt(NumElts, 0); | |||
6784 | EltBits.resize(NumElts, APInt(EltSizeInBits, 0)); | |||
6785 | ||||
6786 | for (unsigned i = 0; i != NumElts; ++i) { | |||
6787 | unsigned BitOffset = i * EltSizeInBits; | |||
6788 | APInt UndefEltBits = UndefBits.extractBits(EltSizeInBits, BitOffset); | |||
6789 | ||||
6790 | // Only treat an element as UNDEF if all bits are UNDEF. | |||
6791 | if (UndefEltBits.isAllOnes()) { | |||
6792 | if (!AllowWholeUndefs) | |||
6793 | return false; | |||
6794 | UndefElts.setBit(i); | |||
6795 | continue; | |||
6796 | } | |||
6797 | ||||
6798 | // If only some bits are UNDEF then treat them as zero (or bail if not | |||
6799 | // supported). | |||
6800 | if (UndefEltBits.getBoolValue() && !AllowPartialUndefs) | |||
6801 | return false; | |||
6802 | ||||
6803 | EltBits[i] = MaskBits.extractBits(EltSizeInBits, BitOffset); | |||
6804 | } | |||
6805 | return true; | |||
6806 | }; | |||
6807 | ||||
6808 | // Collect constant bits and insert into mask/undef bit masks. | |||
6809 | auto CollectConstantBits = [](const Constant *Cst, APInt &Mask, APInt &Undefs, | |||
6810 | unsigned UndefBitIndex) { | |||
6811 | if (!Cst) | |||
6812 | return false; | |||
6813 | if (isa<UndefValue>(Cst)) { | |||
6814 | Undefs.setBit(UndefBitIndex); | |||
6815 | return true; | |||
6816 | } | |||
6817 | if (auto *CInt = dyn_cast<ConstantInt>(Cst)) { | |||
6818 | Mask = CInt->getValue(); | |||
6819 | return true; | |||
6820 | } | |||
6821 | if (auto *CFP = dyn_cast<ConstantFP>(Cst)) { | |||
6822 | Mask = CFP->getValueAPF().bitcastToAPInt(); | |||
6823 | return true; | |||
6824 | } | |||
6825 | return false; | |||
6826 | }; | |||
6827 | ||||
6828 | // Handle UNDEFs. | |||
6829 | if (Op.isUndef()) { | |||
6830 | APInt UndefSrcElts = APInt::getAllOnes(NumElts); | |||
6831 | SmallVector<APInt, 64> SrcEltBits(NumElts, APInt(EltSizeInBits, 0)); | |||
6832 | return CastBitData(UndefSrcElts, SrcEltBits); | |||
6833 | } | |||
6834 | ||||
6835 | // Extract scalar constant bits. | |||
6836 | if (auto *Cst = dyn_cast<ConstantSDNode>(Op)) { | |||
6837 | APInt UndefSrcElts = APInt::getZero(1); | |||
6838 | SmallVector<APInt, 64> SrcEltBits(1, Cst->getAPIntValue()); | |||
6839 | return CastBitData(UndefSrcElts, SrcEltBits); | |||
6840 | } | |||
6841 | if (auto *Cst = dyn_cast<ConstantFPSDNode>(Op)) { | |||
6842 | APInt UndefSrcElts = APInt::getZero(1); | |||
6843 | APInt RawBits = Cst->getValueAPF().bitcastToAPInt(); | |||
6844 | SmallVector<APInt, 64> SrcEltBits(1, RawBits); | |||
6845 | return CastBitData(UndefSrcElts, SrcEltBits); | |||
6846 | } | |||
6847 | ||||
6848 | // Extract constant bits from build vector. | |||
6849 | if (ISD::isBuildVectorOfConstantSDNodes(Op.getNode())) { | |||
6850 | unsigned SrcEltSizeInBits = VT.getScalarSizeInBits(); | |||
6851 | unsigned NumSrcElts = SizeInBits / SrcEltSizeInBits; | |||
6852 | ||||
6853 | APInt UndefSrcElts(NumSrcElts, 0); | |||
6854 | SmallVector<APInt, 64> SrcEltBits(NumSrcElts, APInt(SrcEltSizeInBits, 0)); | |||
6855 | for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) { | |||
6856 | const SDValue &Src = Op.getOperand(i); | |||
6857 | if (Src.isUndef()) { | |||
6858 | UndefSrcElts.setBit(i); | |||
6859 | continue; | |||
6860 | } | |||
6861 | auto *Cst = cast<ConstantSDNode>(Src); | |||
6862 | SrcEltBits[i] = Cst->getAPIntValue().zextOrTrunc(SrcEltSizeInBits); | |||
6863 | } | |||
6864 | return CastBitData(UndefSrcElts, SrcEltBits); | |||
6865 | } | |||
6866 | if (ISD::isBuildVectorOfConstantFPSDNodes(Op.getNode())) { | |||
6867 | unsigned SrcEltSizeInBits = VT.getScalarSizeInBits(); | |||
6868 | unsigned NumSrcElts = SizeInBits / SrcEltSizeInBits; | |||
6869 | ||||
6870 | APInt UndefSrcElts(NumSrcElts, 0); | |||
6871 | SmallVector<APInt, 64> SrcEltBits(NumSrcElts, APInt(SrcEltSizeInBits, 0)); | |||
6872 | for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) { | |||
6873 | const SDValue &Src = Op.getOperand(i); | |||
6874 | if (Src.isUndef()) { | |||
6875 | UndefSrcElts.setBit(i); | |||
6876 | continue; | |||
6877 | } | |||
6878 | auto *Cst = cast<ConstantFPSDNode>(Src); | |||
6879 | APInt RawBits = Cst->getValueAPF().bitcastToAPInt(); | |||
6880 | SrcEltBits[i] = RawBits.zextOrTrunc(SrcEltSizeInBits); | |||
6881 | } | |||
6882 | return CastBitData(UndefSrcElts, SrcEltBits); | |||
6883 | } | |||
6884 | ||||
6885 | // Extract constant bits from constant pool vector. | |||
6886 | if (auto *Cst = getTargetConstantFromNode(Op)) { | |||
6887 | Type *CstTy = Cst->getType(); | |||
6888 | unsigned CstSizeInBits = CstTy->getPrimitiveSizeInBits(); | |||
6889 | if (!CstTy->isVectorTy() || (CstSizeInBits % SizeInBits) != 0) | |||
6890 | return false; | |||
6891 | ||||
6892 | unsigned SrcEltSizeInBits = CstTy->getScalarSizeInBits(); | |||
6893 | unsigned NumSrcElts = SizeInBits / SrcEltSizeInBits; | |||
6894 | ||||
6895 | APInt UndefSrcElts(NumSrcElts, 0); | |||
6896 | SmallVector<APInt, 64> SrcEltBits(NumSrcElts, APInt(SrcEltSizeInBits, 0)); | |||
6897 | for (unsigned i = 0; i != NumSrcElts; ++i) | |||
6898 | if (!CollectConstantBits(Cst->getAggregateElement(i), SrcEltBits[i], | |||
6899 | UndefSrcElts, i)) | |||
6900 | return false; | |||
6901 | ||||
6902 | return CastBitData(UndefSrcElts, SrcEltBits); | |||
6903 | } | |||
6904 | ||||
6905 | // Extract constant bits from a broadcasted constant pool scalar. | |||
6906 | if (Op.getOpcode() == X86ISD::VBROADCAST_LOAD && | |||
6907 | EltSizeInBits <= VT.getScalarSizeInBits()) { | |||
6908 | auto *MemIntr = cast<MemIntrinsicSDNode>(Op); | |||
6909 | if (MemIntr->getMemoryVT().getScalarSizeInBits() != VT.getScalarSizeInBits()) | |||
6910 | return false; | |||
6911 | ||||
6912 | SDValue Ptr = MemIntr->getBasePtr(); | |||
6913 | if (const Constant *C = getTargetConstantFromBasePtr(Ptr)) { | |||
6914 | unsigned SrcEltSizeInBits = C->getType()->getScalarSizeInBits(); | |||
6915 | unsigned NumSrcElts = SizeInBits / SrcEltSizeInBits; | |||
6916 | ||||
6917 | APInt UndefSrcElts(NumSrcElts, 0); | |||
6918 | SmallVector<APInt, 64> SrcEltBits(1, APInt(SrcEltSizeInBits, 0)); | |||
6919 | if (CollectConstantBits(C, SrcEltBits[0], UndefSrcElts, 0)) { | |||
6920 | if (UndefSrcElts[0]) | |||
6921 | UndefSrcElts.setBits(0, NumSrcElts); | |||
6922 | SrcEltBits.append(NumSrcElts - 1, SrcEltBits[0]); | |||
6923 | return CastBitData(UndefSrcElts, SrcEltBits); | |||
6924 | } | |||
6925 | } | |||
6926 | } | |||
6927 | ||||
6928 | // Extract constant bits from a subvector broadcast. | |||
6929 | if (Op.getOpcode() == X86ISD::SUBV_BROADCAST_LOAD) { | |||
6930 | auto *MemIntr = cast<MemIntrinsicSDNode>(Op); | |||
6931 | SDValue Ptr = MemIntr->getBasePtr(); | |||
6932 | // The source constant may be larger than the subvector broadcast, | |||
6933 | // ensure we extract the correct subvector constants. | |||
6934 | if (const Constant *Cst = getTargetConstantFromBasePtr(Ptr)) { | |||
6935 | Type *CstTy = Cst->getType(); | |||
6936 | unsigned CstSizeInBits = CstTy->getPrimitiveSizeInBits(); | |||
6937 | unsigned SubVecSizeInBits = MemIntr->getMemoryVT().getStoreSizeInBits(); | |||
6938 | if (!CstTy->isVectorTy() || (CstSizeInBits % SubVecSizeInBits) != 0 || | |||
6939 | (SizeInBits % SubVecSizeInBits) != 0) | |||
6940 | return false; | |||
6941 | unsigned CstEltSizeInBits = CstTy->getScalarSizeInBits(); | |||
6942 | unsigned NumSubElts = SubVecSizeInBits / CstEltSizeInBits; | |||
6943 | unsigned NumSubVecs = SizeInBits / SubVecSizeInBits; | |||
6944 | APInt UndefSubElts(NumSubElts, 0); | |||
6945 | SmallVector<APInt, 64> SubEltBits(NumSubElts * NumSubVecs, | |||
6946 | APInt(CstEltSizeInBits, 0)); | |||
6947 | for (unsigned i = 0; i != NumSubElts; ++i) { | |||
6948 | if (!CollectConstantBits(Cst->getAggregateElement(i), SubEltBits[i], | |||
6949 | UndefSubElts, i)) | |||
6950 | return false; | |||
6951 | for (unsigned j = 1; j != NumSubVecs; ++j) | |||
6952 | SubEltBits[i + (j * NumSubElts)] = SubEltBits[i]; | |||
6953 | } | |||
6954 | UndefSubElts = APInt::getSplat(NumSubVecs * UndefSubElts.getBitWidth(), | |||
6955 | UndefSubElts); | |||
6956 | return CastBitData(UndefSubElts, SubEltBits); | |||
6957 | } | |||
6958 | } | |||
6959 | ||||
6960 | // Extract a rematerialized scalar constant insertion. | |||
6961 | if (Op.getOpcode() == X86ISD::VZEXT_MOVL && | |||
6962 | Op.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR && | |||
6963 | isa<ConstantSDNode>(Op.getOperand(0).getOperand(0))) { | |||
6964 | unsigned SrcEltSizeInBits = VT.getScalarSizeInBits(); | |||
6965 | unsigned NumSrcElts = SizeInBits / SrcEltSizeInBits; | |||
6966 | ||||
6967 | APInt UndefSrcElts(NumSrcElts, 0); | |||
6968 | SmallVector<APInt, 64> SrcEltBits; | |||
6969 | auto *CN = cast<ConstantSDNode>(Op.getOperand(0).getOperand(0)); | |||
6970 | SrcEltBits.push_back(CN->getAPIntValue().zextOrTrunc(SrcEltSizeInBits)); | |||
6971 | SrcEltBits.append(NumSrcElts - 1, APInt(SrcEltSizeInBits, 0)); | |||
6972 | return CastBitData(UndefSrcElts, SrcEltBits); | |||
6973 | } | |||
6974 | ||||
6975 | // Insert constant bits from a base and sub vector sources. | |||
6976 | if (Op.getOpcode() == ISD::INSERT_SUBVECTOR) { | |||
6977 | // If bitcasts to larger elements we might lose track of undefs - don't | |||
6978 | // allow any to be safe. | |||
6979 | unsigned SrcEltSizeInBits = VT.getScalarSizeInBits(); | |||
6980 | bool AllowUndefs = EltSizeInBits >= SrcEltSizeInBits; | |||
6981 | ||||
6982 | APInt UndefSrcElts, UndefSubElts; | |||
6983 | SmallVector<APInt, 32> EltSrcBits, EltSubBits; | |||
6984 | if (getTargetConstantBitsFromNode(Op.getOperand(1), SrcEltSizeInBits, | |||
6985 | UndefSubElts, EltSubBits, | |||
6986 | AllowWholeUndefs && AllowUndefs, | |||
6987 | AllowPartialUndefs && AllowUndefs) && | |||
6988 | getTargetConstantBitsFromNode(Op.getOperand(0), SrcEltSizeInBits, | |||
6989 | UndefSrcElts, EltSrcBits, | |||
6990 | AllowWholeUndefs && AllowUndefs, | |||
6991 | AllowPartialUndefs && AllowUndefs)) { | |||
6992 | unsigned BaseIdx = Op.getConstantOperandVal(2); | |||
6993 | UndefSrcElts.insertBits(UndefSubElts, BaseIdx); | |||
6994 | for (unsigned i = 0, e = EltSubBits.size(); i != e; ++i) | |||
6995 | EltSrcBits[BaseIdx + i] = EltSubBits[i]; | |||
6996 | return CastBitData(UndefSrcElts, EltSrcBits); | |||
6997 | } | |||
6998 | } | |||
6999 | ||||
7000 | // Extract constant bits from a subvector's source. | |||
7001 | if (Op.getOpcode() == ISD::EXTRACT_SUBVECTOR) { | |||
7002 | // TODO - support extract_subvector through bitcasts. | |||
7003 | if (EltSizeInBits != VT.getScalarSizeInBits()) | |||
7004 | return false; | |||
7005 | ||||
7006 | if (getTargetConstantBitsFromNode(Op.getOperand(0), EltSizeInBits, | |||
7007 | UndefElts, EltBits, AllowWholeUndefs, | |||
7008 | AllowPartialUndefs)) { | |||
7009 | EVT SrcVT = Op.getOperand(0).getValueType(); | |||
7010 | unsigned NumSrcElts = SrcVT.getVectorNumElements(); | |||
7011 | unsigned NumSubElts = VT.getVectorNumElements(); | |||
7012 | unsigned BaseIdx = Op.getConstantOperandVal(1); | |||
7013 | UndefElts = UndefElts.extractBits(NumSubElts, BaseIdx); | |||
7014 | if ((BaseIdx + NumSubElts) != NumSrcElts) | |||
7015 | EltBits.erase(EltBits.begin() + BaseIdx + NumSubElts, EltBits.end()); | |||
7016 | if (BaseIdx != 0) | |||
7017 | EltBits.erase(EltBits.begin(), EltBits.begin() + BaseIdx); | |||
7018 | return true; | |||
7019 | } | |||
7020 | } | |||
7021 | ||||
7022 | // Extract constant bits from shuffle node sources. | |||
7023 | if (auto *SVN = dyn_cast<ShuffleVectorSDNode>(Op)) { | |||
7024 | // TODO - support shuffle through bitcasts. | |||
7025 | if (EltSizeInBits != VT.getScalarSizeInBits()) | |||
7026 | return false; | |||
7027 | ||||
7028 | ArrayRef<int> Mask = SVN->getMask(); | |||
7029 | if ((!AllowWholeUndefs || !AllowPartialUndefs) && | |||
7030 | llvm::any_of(Mask, [](int M) { return M < 0; })) | |||
7031 | return false; | |||
7032 | ||||
7033 | APInt UndefElts0, UndefElts1; | |||
7034 | SmallVector<APInt, 32> EltBits0, EltBits1; | |||
7035 | if (isAnyInRange(Mask, 0, NumElts) && | |||
7036 | !getTargetConstantBitsFromNode(Op.getOperand(0), EltSizeInBits, | |||
7037 | UndefElts0, EltBits0, AllowWholeUndefs, | |||
7038 | AllowPartialUndefs)) | |||
7039 | return false; | |||
7040 | if (isAnyInRange(Mask, NumElts, 2 * NumElts) && | |||
7041 | !getTargetConstantBitsFromNode(Op.getOperand(1), EltSizeInBits, | |||
7042 | UndefElts1, EltBits1, AllowWholeUndefs, | |||
7043 | AllowPartialUndefs)) | |||
7044 | return false; | |||
7045 | ||||
7046 | UndefElts = APInt::getZero(NumElts); | |||
7047 | for (int i = 0; i != (int)NumElts; ++i) { | |||
7048 | int M = Mask[i]; | |||
7049 | if (M < 0) { | |||
7050 | UndefElts.setBit(i); | |||
7051 | EltBits.push_back(APInt::getZero(EltSizeInBits)); | |||
7052 | } else if (M < (int)NumElts) { | |||
7053 | if (UndefElts0[M]) | |||
7054 | UndefElts.setBit(i); | |||
7055 | EltBits.push_back(EltBits0[M]); | |||
7056 | } else { | |||
7057 | if (UndefElts1[M - NumElts]) | |||
7058 | UndefElts.setBit(i); | |||
7059 | EltBits.push_back(EltBits1[M - NumElts]); | |||
7060 | } | |||
7061 | } | |||
7062 | return true; | |||
7063 | } | |||
7064 | ||||
7065 | return false; | |||
7066 | } | |||
7067 | ||||
7068 | namespace llvm { | |||
7069 | namespace X86 { | |||
7070 | bool isConstantSplat(SDValue Op, APInt &SplatVal, bool AllowPartialUndefs) { | |||
7071 | APInt UndefElts; | |||
7072 | SmallVector<APInt, 16> EltBits; | |||
7073 | if (getTargetConstantBitsFromNode(Op, Op.getScalarValueSizeInBits(), | |||
7074 | UndefElts, EltBits, true, | |||
7075 | AllowPartialUndefs)) { | |||
7076 | int SplatIndex = -1; | |||
7077 | for (int i = 0, e = EltBits.size(); i != e; ++i) { | |||
7078 | if (UndefElts[i]) | |||
7079 | continue; | |||
7080 | if (0 <= SplatIndex && EltBits[i] != EltBits[SplatIndex]) { | |||
7081 | SplatIndex = -1; | |||
7082 | break; | |||
7083 | } | |||
7084 | SplatIndex = i; | |||
7085 | } | |||
7086 | if (0 <= SplatIndex) { | |||
7087 | SplatVal = EltBits[SplatIndex]; | |||
7088 | return true; | |||
7089 | } | |||
7090 | } | |||
7091 | ||||
7092 | return false; | |||
7093 | } | |||
7094 | } // namespace X86 | |||
7095 | } // namespace llvm | |||
7096 | ||||
7097 | static bool getTargetShuffleMaskIndices(SDValue MaskNode, | |||
7098 | unsigned MaskEltSizeInBits, | |||
7099 | SmallVectorImpl<uint64_t> &RawMask, | |||
7100 | APInt &UndefElts) { | |||
7101 | // Extract the raw target constant bits. | |||
7102 | SmallVector<APInt, 64> EltBits; | |||
7103 | if (!getTargetConstantBitsFromNode(MaskNode, MaskEltSizeInBits, UndefElts, | |||
7104 | EltBits, /* AllowWholeUndefs */ true, | |||
7105 | /* AllowPartialUndefs */ false)) | |||
7106 | return false; | |||
7107 | ||||
7108 | // Insert the extracted elements into the mask. | |||
7109 | for (const APInt &Elt : EltBits) | |||
7110 | RawMask.push_back(Elt.getZExtValue()); | |||
7111 | ||||
7112 | return true; | |||
7113 | } | |||
7114 | ||||
7115 | /// Create a shuffle mask that matches the PACKSS/PACKUS truncation. | |||
7116 | /// A multi-stage pack shuffle mask is created by specifying NumStages > 1. | |||
7117 | /// Note: This ignores saturation, so inputs must be checked first. | |||
7118 | static void createPackShuffleMask(MVT VT, SmallVectorImpl<int> &Mask, | |||
7119 | bool Unary, unsigned NumStages = 1) { | |||
7120 | assert(Mask.empty() && "Expected an empty shuffle mask vector")(static_cast <bool> (Mask.empty() && "Expected an empty shuffle mask vector" ) ? void (0) : __assert_fail ("Mask.empty() && \"Expected an empty shuffle mask vector\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7120, __extension__ __PRETTY_FUNCTION__)); | |||
7121 | unsigned NumElts = VT.getVectorNumElements(); | |||
7122 | unsigned NumLanes = VT.getSizeInBits() / 128; | |||
7123 | unsigned NumEltsPerLane = 128 / VT.getScalarSizeInBits(); | |||
7124 | unsigned Offset = Unary ? 0 : NumElts; | |||
7125 | unsigned Repetitions = 1u << (NumStages - 1); | |||
7126 | unsigned Increment = 1u << NumStages; | |||
7127 | assert((NumEltsPerLane >> NumStages) > 0 && "Illegal packing compaction")(static_cast <bool> ((NumEltsPerLane >> NumStages ) > 0 && "Illegal packing compaction") ? void (0) : __assert_fail ("(NumEltsPerLane >> NumStages) > 0 && \"Illegal packing compaction\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7127, __extension__ __PRETTY_FUNCTION__)); | |||
7128 | ||||
7129 | for (unsigned Lane = 0; Lane != NumLanes; ++Lane) { | |||
7130 | for (unsigned Stage = 0; Stage != Repetitions; ++Stage) { | |||
7131 | for (unsigned Elt = 0; Elt != NumEltsPerLane; Elt += Increment) | |||
7132 | Mask.push_back(Elt + (Lane * NumEltsPerLane)); | |||
7133 | for (unsigned Elt = 0; Elt != NumEltsPerLane; Elt += Increment) | |||
7134 | Mask.push_back(Elt + (Lane * NumEltsPerLane) + Offset); | |||
7135 | } | |||
7136 | } | |||
7137 | } | |||
7138 | ||||
7139 | // Split the demanded elts of a PACKSS/PACKUS node between its operands. | |||
7140 | static void getPackDemandedElts(EVT VT, const APInt &DemandedElts, | |||
7141 | APInt &DemandedLHS, APInt &DemandedRHS) { | |||
7142 | int NumLanes = VT.getSizeInBits() / 128; | |||
7143 | int NumElts = DemandedElts.getBitWidth(); | |||
7144 | int NumInnerElts = NumElts / 2; | |||
7145 | int NumEltsPerLane = NumElts / NumLanes; | |||
7146 | int NumInnerEltsPerLane = NumInnerElts / NumLanes; | |||
7147 | ||||
7148 | DemandedLHS = APInt::getZero(NumInnerElts); | |||
7149 | DemandedRHS = APInt::getZero(NumInnerElts); | |||
7150 | ||||
7151 | // Map DemandedElts to the packed operands. | |||
7152 | for (int Lane = 0; Lane != NumLanes; ++Lane) { | |||
7153 | for (int Elt = 0; Elt != NumInnerEltsPerLane; ++Elt) { | |||
7154 | int OuterIdx = (Lane * NumEltsPerLane) + Elt; | |||
7155 | int InnerIdx = (Lane * NumInnerEltsPerLane) + Elt; | |||
7156 | if (DemandedElts[OuterIdx]) | |||
7157 | DemandedLHS.setBit(InnerIdx); | |||
7158 | if (DemandedElts[OuterIdx + NumInnerEltsPerLane]) | |||
7159 | DemandedRHS.setBit(InnerIdx); | |||
7160 | } | |||
7161 | } | |||
7162 | } | |||
7163 | ||||
7164 | // Split the demanded elts of a HADD/HSUB node between its operands. | |||
7165 | static void getHorizDemandedElts(EVT VT, const APInt &DemandedElts, | |||
7166 | APInt &DemandedLHS, APInt &DemandedRHS) { | |||
7167 | int NumLanes = VT.getSizeInBits() / 128; | |||
7168 | int NumElts = DemandedElts.getBitWidth(); | |||
7169 | int NumEltsPerLane = NumElts / NumLanes; | |||
7170 | int HalfEltsPerLane = NumEltsPerLane / 2; | |||
7171 | ||||
7172 | DemandedLHS = APInt::getZero(NumElts); | |||
7173 | DemandedRHS = APInt::getZero(NumElts); | |||
7174 | ||||
7175 | // Map DemandedElts to the horizontal operands. | |||
7176 | for (int Idx = 0; Idx != NumElts; ++Idx) { | |||
7177 | if (!DemandedElts[Idx]) | |||
7178 | continue; | |||
7179 | int LaneIdx = (Idx / NumEltsPerLane) * NumEltsPerLane; | |||
7180 | int LocalIdx = Idx % NumEltsPerLane; | |||
7181 | if (LocalIdx < HalfEltsPerLane) { | |||
7182 | DemandedLHS.setBit(LaneIdx + 2 * LocalIdx + 0); | |||
7183 | DemandedLHS.setBit(LaneIdx + 2 * LocalIdx + 1); | |||
7184 | } else { | |||
7185 | LocalIdx -= HalfEltsPerLane; | |||
7186 | DemandedRHS.setBit(LaneIdx + 2 * LocalIdx + 0); | |||
7187 | DemandedRHS.setBit(LaneIdx + 2 * LocalIdx + 1); | |||
7188 | } | |||
7189 | } | |||
7190 | } | |||
7191 | ||||
7192 | /// Calculates the shuffle mask corresponding to the target-specific opcode. | |||
7193 | /// If the mask could be calculated, returns it in \p Mask, returns the shuffle | |||
7194 | /// operands in \p Ops, and returns true. | |||
7195 | /// Sets \p IsUnary to true if only one source is used. Note that this will set | |||
7196 | /// IsUnary for shuffles which use a single input multiple times, and in those | |||
7197 | /// cases it will adjust the mask to only have indices within that single input. | |||
7198 | /// It is an error to call this with non-empty Mask/Ops vectors. | |||
7199 | static bool getTargetShuffleMask(SDNode *N, MVT VT, bool AllowSentinelZero, | |||
7200 | SmallVectorImpl<SDValue> &Ops, | |||
7201 | SmallVectorImpl<int> &Mask, bool &IsUnary) { | |||
7202 | unsigned NumElems = VT.getVectorNumElements(); | |||
7203 | unsigned MaskEltSize = VT.getScalarSizeInBits(); | |||
7204 | SmallVector<uint64_t, 32> RawMask; | |||
7205 | APInt RawUndefs; | |||
7206 | uint64_t ImmN; | |||
7207 | ||||
7208 | assert(Mask.empty() && "getTargetShuffleMask expects an empty Mask vector")(static_cast <bool> (Mask.empty() && "getTargetShuffleMask expects an empty Mask vector" ) ? void (0) : __assert_fail ("Mask.empty() && \"getTargetShuffleMask expects an empty Mask vector\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7208, __extension__ __PRETTY_FUNCTION__)); | |||
7209 | assert(Ops.empty() && "getTargetShuffleMask expects an empty Ops vector")(static_cast <bool> (Ops.empty() && "getTargetShuffleMask expects an empty Ops vector" ) ? void (0) : __assert_fail ("Ops.empty() && \"getTargetShuffleMask expects an empty Ops vector\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7209, __extension__ __PRETTY_FUNCTION__)); | |||
7210 | ||||
7211 | IsUnary = false; | |||
7212 | bool IsFakeUnary = false; | |||
7213 | switch (N->getOpcode()) { | |||
7214 | case X86ISD::BLENDI: | |||
7215 | assert(N->getOperand(0).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(0).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(0).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7215, __extension__ __PRETTY_FUNCTION__)); | |||
7216 | assert(N->getOperand(1).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(1).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(1).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7216, __extension__ __PRETTY_FUNCTION__)); | |||
7217 | ImmN = N->getConstantOperandVal(N->getNumOperands() - 1); | |||
7218 | DecodeBLENDMask(NumElems, ImmN, Mask); | |||
7219 | IsUnary = IsFakeUnary = N->getOperand(0) == N->getOperand(1); | |||
7220 | break; | |||
7221 | case X86ISD::SHUFP: | |||
7222 | assert(N->getOperand(0).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(0).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(0).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7222, __extension__ __PRETTY_FUNCTION__)); | |||
7223 | assert(N->getOperand(1).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(1).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(1).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7223, __extension__ __PRETTY_FUNCTION__)); | |||
7224 | ImmN = N->getConstantOperandVal(N->getNumOperands() - 1); | |||
7225 | DecodeSHUFPMask(NumElems, MaskEltSize, ImmN, Mask); | |||
7226 | IsUnary = IsFakeUnary = N->getOperand(0) == N->getOperand(1); | |||
7227 | break; | |||
7228 | case X86ISD::INSERTPS: | |||
7229 | assert(N->getOperand(0).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(0).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(0).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7229, __extension__ __PRETTY_FUNCTION__)); | |||
7230 | assert(N->getOperand(1).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(1).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(1).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7230, __extension__ __PRETTY_FUNCTION__)); | |||
7231 | ImmN = N->getConstantOperandVal(N->getNumOperands() - 1); | |||
7232 | DecodeINSERTPSMask(ImmN, Mask); | |||
7233 | IsUnary = IsFakeUnary = N->getOperand(0) == N->getOperand(1); | |||
7234 | break; | |||
7235 | case X86ISD::EXTRQI: | |||
7236 | assert(N->getOperand(0).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(0).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(0).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7236, __extension__ __PRETTY_FUNCTION__)); | |||
7237 | if (isa<ConstantSDNode>(N->getOperand(1)) && | |||
7238 | isa<ConstantSDNode>(N->getOperand(2))) { | |||
7239 | int BitLen = N->getConstantOperandVal(1); | |||
7240 | int BitIdx = N->getConstantOperandVal(2); | |||
7241 | DecodeEXTRQIMask(NumElems, MaskEltSize, BitLen, BitIdx, Mask); | |||
7242 | IsUnary = true; | |||
7243 | } | |||
7244 | break; | |||
7245 | case X86ISD::INSERTQI: | |||
7246 | assert(N->getOperand(0).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(0).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(0).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7246, __extension__ __PRETTY_FUNCTION__)); | |||
7247 | assert(N->getOperand(1).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(1).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(1).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7247, __extension__ __PRETTY_FUNCTION__)); | |||
7248 | if (isa<ConstantSDNode>(N->getOperand(2)) && | |||
7249 | isa<ConstantSDNode>(N->getOperand(3))) { | |||
7250 | int BitLen = N->getConstantOperandVal(2); | |||
7251 | int BitIdx = N->getConstantOperandVal(3); | |||
7252 | DecodeINSERTQIMask(NumElems, MaskEltSize, BitLen, BitIdx, Mask); | |||
7253 | IsUnary = IsFakeUnary = N->getOperand(0) == N->getOperand(1); | |||
7254 | } | |||
7255 | break; | |||
7256 | case X86ISD::UNPCKH: | |||
7257 | assert(N->getOperand(0).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(0).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(0).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7257, __extension__ __PRETTY_FUNCTION__)); | |||
7258 | assert(N->getOperand(1).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(1).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(1).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7258, __extension__ __PRETTY_FUNCTION__)); | |||
7259 | DecodeUNPCKHMask(NumElems, MaskEltSize, Mask); | |||
7260 | IsUnary = IsFakeUnary = N->getOperand(0) == N->getOperand(1); | |||
7261 | break; | |||
7262 | case X86ISD::UNPCKL: | |||
7263 | assert(N->getOperand(0).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(0).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(0).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7263, __extension__ __PRETTY_FUNCTION__)); | |||
7264 | assert(N->getOperand(1).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(1).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(1).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7264, __extension__ __PRETTY_FUNCTION__)); | |||
7265 | DecodeUNPCKLMask(NumElems, MaskEltSize, Mask); | |||
7266 | IsUnary = IsFakeUnary = N->getOperand(0) == N->getOperand(1); | |||
7267 | break; | |||
7268 | case X86ISD::MOVHLPS: | |||
7269 | assert(N->getOperand(0).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(0).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(0).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7269, __extension__ __PRETTY_FUNCTION__)); | |||
7270 | assert(N->getOperand(1).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(1).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(1).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7270, __extension__ __PRETTY_FUNCTION__)); | |||
7271 | DecodeMOVHLPSMask(NumElems, Mask); | |||
7272 | IsUnary = IsFakeUnary = N->getOperand(0) == N->getOperand(1); | |||
7273 | break; | |||
7274 | case X86ISD::MOVLHPS: | |||
7275 | assert(N->getOperand(0).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(0).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(0).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7275, __extension__ __PRETTY_FUNCTION__)); | |||
7276 | assert(N->getOperand(1).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(1).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(1).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7276, __extension__ __PRETTY_FUNCTION__)); | |||
7277 | DecodeMOVLHPSMask(NumElems, Mask); | |||
7278 | IsUnary = IsFakeUnary = N->getOperand(0) == N->getOperand(1); | |||
7279 | break; | |||
7280 | case X86ISD::VALIGN: | |||
7281 | assert((VT.getScalarType() == MVT::i32 || VT.getScalarType() == MVT::i64) &&(static_cast <bool> ((VT.getScalarType() == MVT::i32 || VT.getScalarType() == MVT::i64) && "Only 32-bit and 64-bit elements are supported!" ) ? void (0) : __assert_fail ("(VT.getScalarType() == MVT::i32 || VT.getScalarType() == MVT::i64) && \"Only 32-bit and 64-bit elements are supported!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7282, __extension__ __PRETTY_FUNCTION__)) | |||
7282 | "Only 32-bit and 64-bit elements are supported!")(static_cast <bool> ((VT.getScalarType() == MVT::i32 || VT.getScalarType() == MVT::i64) && "Only 32-bit and 64-bit elements are supported!" ) ? void (0) : __assert_fail ("(VT.getScalarType() == MVT::i32 || VT.getScalarType() == MVT::i64) && \"Only 32-bit and 64-bit elements are supported!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7282, __extension__ __PRETTY_FUNCTION__)); | |||
7283 | assert(N->getOperand(0).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(0).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(0).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7283, __extension__ __PRETTY_FUNCTION__)); | |||
7284 | assert(N->getOperand(1).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(1).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(1).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7284, __extension__ __PRETTY_FUNCTION__)); | |||
7285 | ImmN = N->getConstantOperandVal(N->getNumOperands() - 1); | |||
7286 | DecodeVALIGNMask(NumElems, ImmN, Mask); | |||
7287 | IsUnary = IsFakeUnary = N->getOperand(0) == N->getOperand(1); | |||
7288 | Ops.push_back(N->getOperand(1)); | |||
7289 | Ops.push_back(N->getOperand(0)); | |||
7290 | break; | |||
7291 | case X86ISD::PALIGNR: | |||
7292 | assert(VT.getScalarType() == MVT::i8 && "Byte vector expected")(static_cast <bool> (VT.getScalarType() == MVT::i8 && "Byte vector expected") ? void (0) : __assert_fail ("VT.getScalarType() == MVT::i8 && \"Byte vector expected\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7292, __extension__ __PRETTY_FUNCTION__)); | |||
7293 | assert(N->getOperand(0).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(0).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(0).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7293, __extension__ __PRETTY_FUNCTION__)); | |||
7294 | assert(N->getOperand(1).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(1).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(1).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7294, __extension__ __PRETTY_FUNCTION__)); | |||
7295 | ImmN = N->getConstantOperandVal(N->getNumOperands() - 1); | |||
7296 | DecodePALIGNRMask(NumElems, ImmN, Mask); | |||
7297 | IsUnary = IsFakeUnary = N->getOperand(0) == N->getOperand(1); | |||
7298 | Ops.push_back(N->getOperand(1)); | |||
7299 | Ops.push_back(N->getOperand(0)); | |||
7300 | break; | |||
7301 | case X86ISD::VSHLDQ: | |||
7302 | assert(VT.getScalarType() == MVT::i8 && "Byte vector expected")(static_cast <bool> (VT.getScalarType() == MVT::i8 && "Byte vector expected") ? void (0) : __assert_fail ("VT.getScalarType() == MVT::i8 && \"Byte vector expected\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7302, __extension__ __PRETTY_FUNCTION__)); | |||
7303 | assert(N->getOperand(0).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(0).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(0).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7303, __extension__ __PRETTY_FUNCTION__)); | |||
7304 | ImmN = N->getConstantOperandVal(N->getNumOperands() - 1); | |||
7305 | DecodePSLLDQMask(NumElems, ImmN, Mask); | |||
7306 | IsUnary = true; | |||
7307 | break; | |||
7308 | case X86ISD::VSRLDQ: | |||
7309 | assert(VT.getScalarType() == MVT::i8 && "Byte vector expected")(static_cast <bool> (VT.getScalarType() == MVT::i8 && "Byte vector expected") ? void (0) : __assert_fail ("VT.getScalarType() == MVT::i8 && \"Byte vector expected\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7309, __extension__ __PRETTY_FUNCTION__)); | |||
7310 | assert(N->getOperand(0).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(0).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(0).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7310, __extension__ __PRETTY_FUNCTION__)); | |||
7311 | ImmN = N->getConstantOperandVal(N->getNumOperands() - 1); | |||
7312 | DecodePSRLDQMask(NumElems, ImmN, Mask); | |||
7313 | IsUnary = true; | |||
7314 | break; | |||
7315 | case X86ISD::PSHUFD: | |||
7316 | case X86ISD::VPERMILPI: | |||
7317 | assert(N->getOperand(0).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(0).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(0).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7317, __extension__ __PRETTY_FUNCTION__)); | |||
7318 | ImmN = N->getConstantOperandVal(N->getNumOperands() - 1); | |||
7319 | DecodePSHUFMask(NumElems, MaskEltSize, ImmN, Mask); | |||
7320 | IsUnary = true; | |||
7321 | break; | |||
7322 | case X86ISD::PSHUFHW: | |||
7323 | assert(N->getOperand(0).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(0).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(0).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7323, __extension__ __PRETTY_FUNCTION__)); | |||
7324 | ImmN = N->getConstantOperandVal(N->getNumOperands() - 1); | |||
7325 | DecodePSHUFHWMask(NumElems, ImmN, Mask); | |||
7326 | IsUnary = true; | |||
7327 | break; | |||
7328 | case X86ISD::PSHUFLW: | |||
7329 | assert(N->getOperand(0).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(0).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(0).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7329, __extension__ __PRETTY_FUNCTION__)); | |||
7330 | ImmN = N->getConstantOperandVal(N->getNumOperands() - 1); | |||
7331 | DecodePSHUFLWMask(NumElems, ImmN, Mask); | |||
7332 | IsUnary = true; | |||
7333 | break; | |||
7334 | case X86ISD::VZEXT_MOVL: | |||
7335 | assert(N->getOperand(0).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(0).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(0).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7335, __extension__ __PRETTY_FUNCTION__)); | |||
7336 | DecodeZeroMoveLowMask(NumElems, Mask); | |||
7337 | IsUnary = true; | |||
7338 | break; | |||
7339 | case X86ISD::VBROADCAST: | |||
7340 | // We only decode broadcasts of same-sized vectors, peeking through to | |||
7341 | // extracted subvectors is likely to cause hasOneUse issues with | |||
7342 | // SimplifyDemandedBits etc. | |||
7343 | if (N->getOperand(0).getValueType() == VT) { | |||
7344 | DecodeVectorBroadcast(NumElems, Mask); | |||
7345 | IsUnary = true; | |||
7346 | break; | |||
7347 | } | |||
7348 | return false; | |||
7349 | case X86ISD::VPERMILPV: { | |||
7350 | assert(N->getOperand(0).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(0).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(0).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7350, __extension__ __PRETTY_FUNCTION__)); | |||
7351 | IsUnary = true; | |||
7352 | SDValue MaskNode = N->getOperand(1); | |||
7353 | if (getTargetShuffleMaskIndices(MaskNode, MaskEltSize, RawMask, | |||
7354 | RawUndefs)) { | |||
7355 | DecodeVPERMILPMask(NumElems, MaskEltSize, RawMask, RawUndefs, Mask); | |||
7356 | break; | |||
7357 | } | |||
7358 | return false; | |||
7359 | } | |||
7360 | case X86ISD::PSHUFB: { | |||
7361 | assert(VT.getScalarType() == MVT::i8 && "Byte vector expected")(static_cast <bool> (VT.getScalarType() == MVT::i8 && "Byte vector expected") ? void (0) : __assert_fail ("VT.getScalarType() == MVT::i8 && \"Byte vector expected\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7361, __extension__ __PRETTY_FUNCTION__)); | |||
7362 | assert(N->getOperand(0).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(0).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(0).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7362, __extension__ __PRETTY_FUNCTION__)); | |||
7363 | assert(N->getOperand(1).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(1).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(1).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7363, __extension__ __PRETTY_FUNCTION__)); | |||
7364 | IsUnary = true; | |||
7365 | SDValue MaskNode = N->getOperand(1); | |||
7366 | if (getTargetShuffleMaskIndices(MaskNode, 8, RawMask, RawUndefs)) { | |||
7367 | DecodePSHUFBMask(RawMask, RawUndefs, Mask); | |||
7368 | break; | |||
7369 | } | |||
7370 | return false; | |||
7371 | } | |||
7372 | case X86ISD::VPERMI: | |||
7373 | assert(N->getOperand(0).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(0).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(0).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7373, __extension__ __PRETTY_FUNCTION__)); | |||
7374 | ImmN = N->getConstantOperandVal(N->getNumOperands() - 1); | |||
7375 | DecodeVPERMMask(NumElems, ImmN, Mask); | |||
7376 | IsUnary = true; | |||
7377 | break; | |||
7378 | case X86ISD::MOVSS: | |||
7379 | case X86ISD::MOVSD: | |||
7380 | case X86ISD::MOVSH: | |||
7381 | assert(N->getOperand(0).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(0).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(0).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7381, __extension__ __PRETTY_FUNCTION__)); | |||
7382 | assert(N->getOperand(1).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(1).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(1).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7382, __extension__ __PRETTY_FUNCTION__)); | |||
7383 | DecodeScalarMoveMask(NumElems, /* IsLoad */ false, Mask); | |||
7384 | break; | |||
7385 | case X86ISD::VPERM2X128: | |||
7386 | assert(N->getOperand(0).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(0).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(0).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7386, __extension__ __PRETTY_FUNCTION__)); | |||
7387 | assert(N->getOperand(1).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(1).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(1).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7387, __extension__ __PRETTY_FUNCTION__)); | |||
7388 | ImmN = N->getConstantOperandVal(N->getNumOperands() - 1); | |||
7389 | DecodeVPERM2X128Mask(NumElems, ImmN, Mask); | |||
7390 | IsUnary = IsFakeUnary = N->getOperand(0) == N->getOperand(1); | |||
7391 | break; | |||
7392 | case X86ISD::SHUF128: | |||
7393 | assert(N->getOperand(0).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(0).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(0).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7393, __extension__ __PRETTY_FUNCTION__)); | |||
7394 | assert(N->getOperand(1).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(1).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(1).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7394, __extension__ __PRETTY_FUNCTION__)); | |||
7395 | ImmN = N->getConstantOperandVal(N->getNumOperands() - 1); | |||
7396 | decodeVSHUF64x2FamilyMask(NumElems, MaskEltSize, ImmN, Mask); | |||
7397 | IsUnary = IsFakeUnary = N->getOperand(0) == N->getOperand(1); | |||
7398 | break; | |||
7399 | case X86ISD::MOVSLDUP: | |||
7400 | assert(N->getOperand(0).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(0).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(0).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7400, __extension__ __PRETTY_FUNCTION__)); | |||
7401 | DecodeMOVSLDUPMask(NumElems, Mask); | |||
7402 | IsUnary = true; | |||
7403 | break; | |||
7404 | case X86ISD::MOVSHDUP: | |||
7405 | assert(N->getOperand(0).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(0).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(0).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7405, __extension__ __PRETTY_FUNCTION__)); | |||
7406 | DecodeMOVSHDUPMask(NumElems, Mask); | |||
7407 | IsUnary = true; | |||
7408 | break; | |||
7409 | case X86ISD::MOVDDUP: | |||
7410 | assert(N->getOperand(0).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(0).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(0).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7410, __extension__ __PRETTY_FUNCTION__)); | |||
7411 | DecodeMOVDDUPMask(NumElems, Mask); | |||
7412 | IsUnary = true; | |||
7413 | break; | |||
7414 | case X86ISD::VPERMIL2: { | |||
7415 | assert(N->getOperand(0).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(0).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(0).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7415, __extension__ __PRETTY_FUNCTION__)); | |||
7416 | assert(N->getOperand(1).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(1).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(1).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7416, __extension__ __PRETTY_FUNCTION__)); | |||
7417 | IsUnary = IsFakeUnary = N->getOperand(0) == N->getOperand(1); | |||
7418 | SDValue MaskNode = N->getOperand(2); | |||
7419 | SDValue CtrlNode = N->getOperand(3); | |||
7420 | if (ConstantSDNode *CtrlOp = dyn_cast<ConstantSDNode>(CtrlNode)) { | |||
7421 | unsigned CtrlImm = CtrlOp->getZExtValue(); | |||
7422 | if (getTargetShuffleMaskIndices(MaskNode, MaskEltSize, RawMask, | |||
7423 | RawUndefs)) { | |||
7424 | DecodeVPERMIL2PMask(NumElems, MaskEltSize, CtrlImm, RawMask, RawUndefs, | |||
7425 | Mask); | |||
7426 | break; | |||
7427 | } | |||
7428 | } | |||
7429 | return false; | |||
7430 | } | |||
7431 | case X86ISD::VPPERM: { | |||
7432 | assert(N->getOperand(0).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(0).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(0).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7432, __extension__ __PRETTY_FUNCTION__)); | |||
7433 | assert(N->getOperand(1).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(1).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(1).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7433, __extension__ __PRETTY_FUNCTION__)); | |||
7434 | IsUnary = IsFakeUnary = N->getOperand(0) == N->getOperand(1); | |||
7435 | SDValue MaskNode = N->getOperand(2); | |||
7436 | if (getTargetShuffleMaskIndices(MaskNode, 8, RawMask, RawUndefs)) { | |||
7437 | DecodeVPPERMMask(RawMask, RawUndefs, Mask); | |||
7438 | break; | |||
7439 | } | |||
7440 | return false; | |||
7441 | } | |||
7442 | case X86ISD::VPERMV: { | |||
7443 | assert(N->getOperand(1).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(1).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(1).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7443, __extension__ __PRETTY_FUNCTION__)); | |||
7444 | IsUnary = true; | |||
7445 | // Unlike most shuffle nodes, VPERMV's mask operand is operand 0. | |||
7446 | Ops.push_back(N->getOperand(1)); | |||
7447 | SDValue MaskNode = N->getOperand(0); | |||
7448 | if (getTargetShuffleMaskIndices(MaskNode, MaskEltSize, RawMask, | |||
7449 | RawUndefs)) { | |||
7450 | DecodeVPERMVMask(RawMask, RawUndefs, Mask); | |||
7451 | break; | |||
7452 | } | |||
7453 | return false; | |||
7454 | } | |||
7455 | case X86ISD::VPERMV3: { | |||
7456 | assert(N->getOperand(0).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(0).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(0).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7456, __extension__ __PRETTY_FUNCTION__)); | |||
7457 | assert(N->getOperand(2).getValueType() == VT && "Unexpected value type")(static_cast <bool> (N->getOperand(2).getValueType() == VT && "Unexpected value type") ? void (0) : __assert_fail ("N->getOperand(2).getValueType() == VT && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7457, __extension__ __PRETTY_FUNCTION__)); | |||
7458 | IsUnary = IsFakeUnary = N->getOperand(0) == N->getOperand(2); | |||
7459 | // Unlike most shuffle nodes, VPERMV3's mask operand is the middle one. | |||
7460 | Ops.push_back(N->getOperand(0)); | |||
7461 | Ops.push_back(N->getOperand(2)); | |||
7462 | SDValue MaskNode = N->getOperand(1); | |||
7463 | if (getTargetShuffleMaskIndices(MaskNode, MaskEltSize, RawMask, | |||
7464 | RawUndefs)) { | |||
7465 | DecodeVPERMV3Mask(RawMask, RawUndefs, Mask); | |||
7466 | break; | |||
7467 | } | |||
7468 | return false; | |||
7469 | } | |||
7470 | default: llvm_unreachable("unknown target shuffle node")::llvm::llvm_unreachable_internal("unknown target shuffle node" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7470); | |||
7471 | } | |||
7472 | ||||
7473 | // Empty mask indicates the decode failed. | |||
7474 | if (Mask.empty()) | |||
7475 | return false; | |||
7476 | ||||
7477 | // Check if we're getting a shuffle mask with zero'd elements. | |||
7478 | if (!AllowSentinelZero && isAnyZero(Mask)) | |||
7479 | return false; | |||
7480 | ||||
7481 | // If we have a fake unary shuffle, the shuffle mask is spread across two | |||
7482 | // inputs that are actually the same node. Re-map the mask to always point | |||
7483 | // into the first input. | |||
7484 | if (IsFakeUnary) | |||
7485 | for (int &M : Mask) | |||
7486 | if (M >= (int)Mask.size()) | |||
7487 | M -= Mask.size(); | |||
7488 | ||||
7489 | // If we didn't already add operands in the opcode-specific code, default to | |||
7490 | // adding 1 or 2 operands starting at 0. | |||
7491 | if (Ops.empty()) { | |||
7492 | Ops.push_back(N->getOperand(0)); | |||
7493 | if (!IsUnary || IsFakeUnary) | |||
7494 | Ops.push_back(N->getOperand(1)); | |||
7495 | } | |||
7496 | ||||
7497 | return true; | |||
7498 | } | |||
7499 | ||||
7500 | // Wrapper for getTargetShuffleMask with InUnary; | |||
7501 | static bool getTargetShuffleMask(SDNode *N, MVT VT, bool AllowSentinelZero, | |||
7502 | SmallVectorImpl<SDValue> &Ops, | |||
7503 | SmallVectorImpl<int> &Mask) { | |||
7504 | bool IsUnary; | |||
7505 | return getTargetShuffleMask(N, VT, AllowSentinelZero, Ops, Mask, IsUnary); | |||
7506 | } | |||
7507 | ||||
7508 | /// Compute whether each element of a shuffle is zeroable. | |||
7509 | /// | |||
7510 | /// A "zeroable" vector shuffle element is one which can be lowered to zero. | |||
7511 | /// Either it is an undef element in the shuffle mask, the element of the input | |||
7512 | /// referenced is undef, or the element of the input referenced is known to be | |||
7513 | /// zero. Many x86 shuffles can zero lanes cheaply and we often want to handle | |||
7514 | /// as many lanes with this technique as possible to simplify the remaining | |||
7515 | /// shuffle. | |||
7516 | static void computeZeroableShuffleElements(ArrayRef<int> Mask, | |||
7517 | SDValue V1, SDValue V2, | |||
7518 | APInt &KnownUndef, APInt &KnownZero) { | |||
7519 | int Size = Mask.size(); | |||
7520 | KnownUndef = KnownZero = APInt::getZero(Size); | |||
7521 | ||||
7522 | V1 = peekThroughBitcasts(V1); | |||
7523 | V2 = peekThroughBitcasts(V2); | |||
7524 | ||||
7525 | bool V1IsZero = ISD::isBuildVectorAllZeros(V1.getNode()); | |||
7526 | bool V2IsZero = ISD::isBuildVectorAllZeros(V2.getNode()); | |||
7527 | ||||
7528 | int VectorSizeInBits = V1.getValueSizeInBits(); | |||
7529 | int ScalarSizeInBits = VectorSizeInBits / Size; | |||
7530 | assert(!(VectorSizeInBits % ScalarSizeInBits) && "Illegal shuffle mask size")(static_cast <bool> (!(VectorSizeInBits % ScalarSizeInBits ) && "Illegal shuffle mask size") ? void (0) : __assert_fail ("!(VectorSizeInBits % ScalarSizeInBits) && \"Illegal shuffle mask size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7530, __extension__ __PRETTY_FUNCTION__)); | |||
7531 | ||||
7532 | for (int i = 0; i < Size; ++i) { | |||
7533 | int M = Mask[i]; | |||
7534 | // Handle the easy cases. | |||
7535 | if (M < 0) { | |||
7536 | KnownUndef.setBit(i); | |||
7537 | continue; | |||
7538 | } | |||
7539 | if ((M >= 0 && M < Size && V1IsZero) || (M >= Size && V2IsZero)) { | |||
7540 | KnownZero.setBit(i); | |||
7541 | continue; | |||
7542 | } | |||
7543 | ||||
7544 | // Determine shuffle input and normalize the mask. | |||
7545 | SDValue V = M < Size ? V1 : V2; | |||
7546 | M %= Size; | |||
7547 | ||||
7548 | // Currently we can only search BUILD_VECTOR for UNDEF/ZERO elements. | |||
7549 | if (V.getOpcode() != ISD::BUILD_VECTOR) | |||
7550 | continue; | |||
7551 | ||||
7552 | // If the BUILD_VECTOR has fewer elements then the bitcasted portion of | |||
7553 | // the (larger) source element must be UNDEF/ZERO. | |||
7554 | if ((Size % V.getNumOperands()) == 0) { | |||
7555 | int Scale = Size / V->getNumOperands(); | |||
7556 | SDValue Op = V.getOperand(M / Scale); | |||
7557 | if (Op.isUndef()) | |||
7558 | KnownUndef.setBit(i); | |||
7559 | if (X86::isZeroNode(Op)) | |||
7560 | KnownZero.setBit(i); | |||
7561 | else if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Op)) { | |||
7562 | APInt Val = Cst->getAPIntValue(); | |||
7563 | Val = Val.extractBits(ScalarSizeInBits, (M % Scale) * ScalarSizeInBits); | |||
7564 | if (Val == 0) | |||
7565 | KnownZero.setBit(i); | |||
7566 | } else if (ConstantFPSDNode *Cst = dyn_cast<ConstantFPSDNode>(Op)) { | |||
7567 | APInt Val = Cst->getValueAPF().bitcastToAPInt(); | |||
7568 | Val = Val.extractBits(ScalarSizeInBits, (M % Scale) * ScalarSizeInBits); | |||
7569 | if (Val == 0) | |||
7570 | KnownZero.setBit(i); | |||
7571 | } | |||
7572 | continue; | |||
7573 | } | |||
7574 | ||||
7575 | // If the BUILD_VECTOR has more elements then all the (smaller) source | |||
7576 | // elements must be UNDEF or ZERO. | |||
7577 | if ((V.getNumOperands() % Size) == 0) { | |||
7578 | int Scale = V->getNumOperands() / Size; | |||
7579 | bool AllUndef = true; | |||
7580 | bool AllZero = true; | |||
7581 | for (int j = 0; j < Scale; ++j) { | |||
7582 | SDValue Op = V.getOperand((M * Scale) + j); | |||
7583 | AllUndef &= Op.isUndef(); | |||
7584 | AllZero &= X86::isZeroNode(Op); | |||
7585 | } | |||
7586 | if (AllUndef) | |||
7587 | KnownUndef.setBit(i); | |||
7588 | if (AllZero) | |||
7589 | KnownZero.setBit(i); | |||
7590 | continue; | |||
7591 | } | |||
7592 | } | |||
7593 | } | |||
7594 | ||||
7595 | /// Decode a target shuffle mask and inputs and see if any values are | |||
7596 | /// known to be undef or zero from their inputs. | |||
7597 | /// Returns true if the target shuffle mask was decoded. | |||
7598 | /// FIXME: Merge this with computeZeroableShuffleElements? | |||
7599 | static bool getTargetShuffleAndZeroables(SDValue N, SmallVectorImpl<int> &Mask, | |||
7600 | SmallVectorImpl<SDValue> &Ops, | |||
7601 | APInt &KnownUndef, APInt &KnownZero) { | |||
7602 | bool IsUnary; | |||
7603 | if (!isTargetShuffle(N.getOpcode())) | |||
7604 | return false; | |||
7605 | ||||
7606 | MVT VT = N.getSimpleValueType(); | |||
7607 | if (!getTargetShuffleMask(N.getNode(), VT, true, Ops, Mask, IsUnary)) | |||
7608 | return false; | |||
7609 | ||||
7610 | int Size = Mask.size(); | |||
7611 | SDValue V1 = Ops[0]; | |||
7612 | SDValue V2 = IsUnary ? V1 : Ops[1]; | |||
7613 | KnownUndef = KnownZero = APInt::getZero(Size); | |||
7614 | ||||
7615 | V1 = peekThroughBitcasts(V1); | |||
7616 | V2 = peekThroughBitcasts(V2); | |||
7617 | ||||
7618 | assert((VT.getSizeInBits() % Size) == 0 &&(static_cast <bool> ((VT.getSizeInBits() % Size) == 0 && "Illegal split of shuffle value type") ? void (0) : __assert_fail ("(VT.getSizeInBits() % Size) == 0 && \"Illegal split of shuffle value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7619, __extension__ __PRETTY_FUNCTION__)) | |||
7619 | "Illegal split of shuffle value type")(static_cast <bool> ((VT.getSizeInBits() % Size) == 0 && "Illegal split of shuffle value type") ? void (0) : __assert_fail ("(VT.getSizeInBits() % Size) == 0 && \"Illegal split of shuffle value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7619, __extension__ __PRETTY_FUNCTION__)); | |||
7620 | unsigned EltSizeInBits = VT.getSizeInBits() / Size; | |||
7621 | ||||
7622 | // Extract known constant input data. | |||
7623 | APInt UndefSrcElts[2]; | |||
7624 | SmallVector<APInt, 32> SrcEltBits[2]; | |||
7625 | bool IsSrcConstant[2] = { | |||
7626 | getTargetConstantBitsFromNode(V1, EltSizeInBits, UndefSrcElts[0], | |||
7627 | SrcEltBits[0], true, false), | |||
7628 | getTargetConstantBitsFromNode(V2, EltSizeInBits, UndefSrcElts[1], | |||
7629 | SrcEltBits[1], true, false)}; | |||
7630 | ||||
7631 | for (int i = 0; i < Size; ++i) { | |||
7632 | int M = Mask[i]; | |||
7633 | ||||
7634 | // Already decoded as SM_SentinelZero / SM_SentinelUndef. | |||
7635 | if (M < 0) { | |||
7636 | assert(isUndefOrZero(M) && "Unknown shuffle sentinel value!")(static_cast <bool> (isUndefOrZero(M) && "Unknown shuffle sentinel value!" ) ? void (0) : __assert_fail ("isUndefOrZero(M) && \"Unknown shuffle sentinel value!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7636, __extension__ __PRETTY_FUNCTION__)); | |||
7637 | if (SM_SentinelUndef == M) | |||
7638 | KnownUndef.setBit(i); | |||
7639 | if (SM_SentinelZero == M) | |||
7640 | KnownZero.setBit(i); | |||
7641 | continue; | |||
7642 | } | |||
7643 | ||||
7644 | // Determine shuffle input and normalize the mask. | |||
7645 | unsigned SrcIdx = M / Size; | |||
7646 | SDValue V = M < Size ? V1 : V2; | |||
7647 | M %= Size; | |||
7648 | ||||
7649 | // We are referencing an UNDEF input. | |||
7650 | if (V.isUndef()) { | |||
7651 | KnownUndef.setBit(i); | |||
7652 | continue; | |||
7653 | } | |||
7654 | ||||
7655 | // SCALAR_TO_VECTOR - only the first element is defined, and the rest UNDEF. | |||
7656 | // TODO: We currently only set UNDEF for integer types - floats use the same | |||
7657 | // registers as vectors and many of the scalar folded loads rely on the | |||
7658 | // SCALAR_TO_VECTOR pattern. | |||
7659 | if (V.getOpcode() == ISD::SCALAR_TO_VECTOR && | |||
7660 | (Size % V.getValueType().getVectorNumElements()) == 0) { | |||
7661 | int Scale = Size / V.getValueType().getVectorNumElements(); | |||
7662 | int Idx = M / Scale; | |||
7663 | if (Idx != 0 && !VT.isFloatingPoint()) | |||
7664 | KnownUndef.setBit(i); | |||
7665 | else if (Idx == 0 && X86::isZeroNode(V.getOperand(0))) | |||
7666 | KnownZero.setBit(i); | |||
7667 | continue; | |||
7668 | } | |||
7669 | ||||
7670 | // INSERT_SUBVECTOR - to widen vectors we often insert them into UNDEF | |||
7671 | // base vectors. | |||
7672 | if (V.getOpcode() == ISD::INSERT_SUBVECTOR) { | |||
7673 | SDValue Vec = V.getOperand(0); | |||
7674 | int NumVecElts = Vec.getValueType().getVectorNumElements(); | |||
7675 | if (Vec.isUndef() && Size == NumVecElts) { | |||
7676 | int Idx = V.getConstantOperandVal(2); | |||
7677 | int NumSubElts = V.getOperand(1).getValueType().getVectorNumElements(); | |||
7678 | if (M < Idx || (Idx + NumSubElts) <= M) | |||
7679 | KnownUndef.setBit(i); | |||
7680 | } | |||
7681 | continue; | |||
7682 | } | |||
7683 | ||||
7684 | // Attempt to extract from the source's constant bits. | |||
7685 | if (IsSrcConstant[SrcIdx]) { | |||
7686 | if (UndefSrcElts[SrcIdx][M]) | |||
7687 | KnownUndef.setBit(i); | |||
7688 | else if (SrcEltBits[SrcIdx][M] == 0) | |||
7689 | KnownZero.setBit(i); | |||
7690 | } | |||
7691 | } | |||
7692 | ||||
7693 | assert(VT.getVectorNumElements() == (unsigned)Size &&(static_cast <bool> (VT.getVectorNumElements() == (unsigned )Size && "Different mask size from vector size!") ? void (0) : __assert_fail ("VT.getVectorNumElements() == (unsigned)Size && \"Different mask size from vector size!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7694, __extension__ __PRETTY_FUNCTION__)) | |||
7694 | "Different mask size from vector size!")(static_cast <bool> (VT.getVectorNumElements() == (unsigned )Size && "Different mask size from vector size!") ? void (0) : __assert_fail ("VT.getVectorNumElements() == (unsigned)Size && \"Different mask size from vector size!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7694, __extension__ __PRETTY_FUNCTION__)); | |||
7695 | return true; | |||
7696 | } | |||
7697 | ||||
7698 | // Replace target shuffle mask elements with known undef/zero sentinels. | |||
7699 | static void resolveTargetShuffleFromZeroables(SmallVectorImpl<int> &Mask, | |||
7700 | const APInt &KnownUndef, | |||
7701 | const APInt &KnownZero, | |||
7702 | bool ResolveKnownZeros= true) { | |||
7703 | unsigned NumElts = Mask.size(); | |||
7704 | assert(KnownUndef.getBitWidth() == NumElts &&(static_cast <bool> (KnownUndef.getBitWidth() == NumElts && KnownZero.getBitWidth() == NumElts && "Shuffle mask size mismatch" ) ? void (0) : __assert_fail ("KnownUndef.getBitWidth() == NumElts && KnownZero.getBitWidth() == NumElts && \"Shuffle mask size mismatch\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7705, __extension__ __PRETTY_FUNCTION__)) | |||
7705 | KnownZero.getBitWidth() == NumElts && "Shuffle mask size mismatch")(static_cast <bool> (KnownUndef.getBitWidth() == NumElts && KnownZero.getBitWidth() == NumElts && "Shuffle mask size mismatch" ) ? void (0) : __assert_fail ("KnownUndef.getBitWidth() == NumElts && KnownZero.getBitWidth() == NumElts && \"Shuffle mask size mismatch\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7705, __extension__ __PRETTY_FUNCTION__)); | |||
7706 | ||||
7707 | for (unsigned i = 0; i != NumElts; ++i) { | |||
7708 | if (KnownUndef[i]) | |||
7709 | Mask[i] = SM_SentinelUndef; | |||
7710 | else if (ResolveKnownZeros && KnownZero[i]) | |||
7711 | Mask[i] = SM_SentinelZero; | |||
7712 | } | |||
7713 | } | |||
7714 | ||||
7715 | // Extract target shuffle mask sentinel elements to known undef/zero bitmasks. | |||
7716 | static void resolveZeroablesFromTargetShuffle(const SmallVectorImpl<int> &Mask, | |||
7717 | APInt &KnownUndef, | |||
7718 | APInt &KnownZero) { | |||
7719 | unsigned NumElts = Mask.size(); | |||
7720 | KnownUndef = KnownZero = APInt::getZero(NumElts); | |||
7721 | ||||
7722 | for (unsigned i = 0; i != NumElts; ++i) { | |||
7723 | int M = Mask[i]; | |||
7724 | if (SM_SentinelUndef == M) | |||
7725 | KnownUndef.setBit(i); | |||
7726 | if (SM_SentinelZero == M) | |||
7727 | KnownZero.setBit(i); | |||
7728 | } | |||
7729 | } | |||
7730 | ||||
7731 | // Forward declaration (for getFauxShuffleMask recursive check). | |||
7732 | // TODO: Use DemandedElts variant. | |||
7733 | static bool getTargetShuffleInputs(SDValue Op, SmallVectorImpl<SDValue> &Inputs, | |||
7734 | SmallVectorImpl<int> &Mask, | |||
7735 | const SelectionDAG &DAG, unsigned Depth, | |||
7736 | bool ResolveKnownElts); | |||
7737 | ||||
7738 | // Attempt to decode ops that could be represented as a shuffle mask. | |||
7739 | // The decoded shuffle mask may contain a different number of elements to the | |||
7740 | // destination value type. | |||
7741 | static bool getFauxShuffleMask(SDValue N, const APInt &DemandedElts, | |||
7742 | SmallVectorImpl<int> &Mask, | |||
7743 | SmallVectorImpl<SDValue> &Ops, | |||
7744 | const SelectionDAG &DAG, unsigned Depth, | |||
7745 | bool ResolveKnownElts) { | |||
7746 | Mask.clear(); | |||
7747 | Ops.clear(); | |||
7748 | ||||
7749 | MVT VT = N.getSimpleValueType(); | |||
7750 | unsigned NumElts = VT.getVectorNumElements(); | |||
7751 | unsigned NumSizeInBits = VT.getSizeInBits(); | |||
7752 | unsigned NumBitsPerElt = VT.getScalarSizeInBits(); | |||
7753 | if ((NumBitsPerElt % 8) != 0 || (NumSizeInBits % 8) != 0) | |||
7754 | return false; | |||
7755 | assert(NumElts == DemandedElts.getBitWidth() && "Unexpected vector size")(static_cast <bool> (NumElts == DemandedElts.getBitWidth () && "Unexpected vector size") ? void (0) : __assert_fail ("NumElts == DemandedElts.getBitWidth() && \"Unexpected vector size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7755, __extension__ __PRETTY_FUNCTION__)); | |||
7756 | unsigned NumSizeInBytes = NumSizeInBits / 8; | |||
7757 | unsigned NumBytesPerElt = NumBitsPerElt / 8; | |||
7758 | ||||
7759 | unsigned Opcode = N.getOpcode(); | |||
7760 | switch (Opcode) { | |||
7761 | case ISD::VECTOR_SHUFFLE: { | |||
7762 | // Don't treat ISD::VECTOR_SHUFFLE as a target shuffle so decode it here. | |||
7763 | ArrayRef<int> ShuffleMask = cast<ShuffleVectorSDNode>(N)->getMask(); | |||
7764 | if (isUndefOrInRange(ShuffleMask, 0, 2 * NumElts)) { | |||
7765 | Mask.append(ShuffleMask.begin(), ShuffleMask.end()); | |||
7766 | Ops.push_back(N.getOperand(0)); | |||
7767 | Ops.push_back(N.getOperand(1)); | |||
7768 | return true; | |||
7769 | } | |||
7770 | return false; | |||
7771 | } | |||
7772 | case ISD::AND: | |||
7773 | case X86ISD::ANDNP: { | |||
7774 | // Attempt to decode as a per-byte mask. | |||
7775 | APInt UndefElts; | |||
7776 | SmallVector<APInt, 32> EltBits; | |||
7777 | SDValue N0 = N.getOperand(0); | |||
7778 | SDValue N1 = N.getOperand(1); | |||
7779 | bool IsAndN = (X86ISD::ANDNP == Opcode); | |||
7780 | uint64_t ZeroMask = IsAndN ? 255 : 0; | |||
7781 | if (!getTargetConstantBitsFromNode(IsAndN ? N0 : N1, 8, UndefElts, EltBits)) | |||
7782 | return false; | |||
7783 | for (int i = 0, e = (int)EltBits.size(); i != e; ++i) { | |||
7784 | if (UndefElts[i]) { | |||
7785 | Mask.push_back(SM_SentinelUndef); | |||
7786 | continue; | |||
7787 | } | |||
7788 | const APInt &ByteBits = EltBits[i]; | |||
7789 | if (ByteBits != 0 && ByteBits != 255) | |||
7790 | return false; | |||
7791 | Mask.push_back(ByteBits == ZeroMask ? SM_SentinelZero : i); | |||
7792 | } | |||
7793 | Ops.push_back(IsAndN ? N1 : N0); | |||
7794 | return true; | |||
7795 | } | |||
7796 | case ISD::OR: { | |||
7797 | // Handle OR(SHUFFLE,SHUFFLE) case where one source is zero and the other | |||
7798 | // is a valid shuffle index. | |||
7799 | SDValue N0 = peekThroughBitcasts(N.getOperand(0)); | |||
7800 | SDValue N1 = peekThroughBitcasts(N.getOperand(1)); | |||
7801 | if (!N0.getValueType().isVector() || !N1.getValueType().isVector()) | |||
7802 | return false; | |||
7803 | SmallVector<int, 64> SrcMask0, SrcMask1; | |||
7804 | SmallVector<SDValue, 2> SrcInputs0, SrcInputs1; | |||
7805 | if (!getTargetShuffleInputs(N0, SrcInputs0, SrcMask0, DAG, Depth + 1, | |||
7806 | true) || | |||
7807 | !getTargetShuffleInputs(N1, SrcInputs1, SrcMask1, DAG, Depth + 1, | |||
7808 | true)) | |||
7809 | return false; | |||
7810 | ||||
7811 | size_t MaskSize = std::max(SrcMask0.size(), SrcMask1.size()); | |||
7812 | SmallVector<int, 64> Mask0, Mask1; | |||
7813 | narrowShuffleMaskElts(MaskSize / SrcMask0.size(), SrcMask0, Mask0); | |||
7814 | narrowShuffleMaskElts(MaskSize / SrcMask1.size(), SrcMask1, Mask1); | |||
7815 | for (int i = 0; i != (int)MaskSize; ++i) { | |||
7816 | // NOTE: Don't handle SM_SentinelUndef, as we can end up in infinite | |||
7817 | // loops converting between OR and BLEND shuffles due to | |||
7818 | // canWidenShuffleElements merging away undef elements, meaning we | |||
7819 | // fail to recognise the OR as the undef element isn't known zero. | |||
7820 | if (Mask0[i] == SM_SentinelZero && Mask1[i] == SM_SentinelZero) | |||
7821 | Mask.push_back(SM_SentinelZero); | |||
7822 | else if (Mask1[i] == SM_SentinelZero) | |||
7823 | Mask.push_back(i); | |||
7824 | else if (Mask0[i] == SM_SentinelZero) | |||
7825 | Mask.push_back(i + MaskSize); | |||
7826 | else | |||
7827 | return false; | |||
7828 | } | |||
7829 | Ops.push_back(N0); | |||
7830 | Ops.push_back(N1); | |||
7831 | return true; | |||
7832 | } | |||
7833 | case ISD::INSERT_SUBVECTOR: { | |||
7834 | SDValue Src = N.getOperand(0); | |||
7835 | SDValue Sub = N.getOperand(1); | |||
7836 | EVT SubVT = Sub.getValueType(); | |||
7837 | unsigned NumSubElts = SubVT.getVectorNumElements(); | |||
7838 | if (!N->isOnlyUserOf(Sub.getNode())) | |||
7839 | return false; | |||
7840 | uint64_t InsertIdx = N.getConstantOperandVal(2); | |||
7841 | // Handle INSERT_SUBVECTOR(SRC0, EXTRACT_SUBVECTOR(SRC1)). | |||
7842 | if (Sub.getOpcode() == ISD::EXTRACT_SUBVECTOR && | |||
7843 | Sub.getOperand(0).getValueType() == VT) { | |||
7844 | uint64_t ExtractIdx = Sub.getConstantOperandVal(1); | |||
7845 | for (int i = 0; i != (int)NumElts; ++i) | |||
7846 | Mask.push_back(i); | |||
7847 | for (int i = 0; i != (int)NumSubElts; ++i) | |||
7848 | Mask[InsertIdx + i] = NumElts + ExtractIdx + i; | |||
7849 | Ops.push_back(Src); | |||
7850 | Ops.push_back(Sub.getOperand(0)); | |||
7851 | return true; | |||
7852 | } | |||
7853 | // Handle INSERT_SUBVECTOR(SRC0, SHUFFLE(SRC1)). | |||
7854 | SmallVector<int, 64> SubMask; | |||
7855 | SmallVector<SDValue, 2> SubInputs; | |||
7856 | if (!getTargetShuffleInputs(peekThroughOneUseBitcasts(Sub), SubInputs, | |||
7857 | SubMask, DAG, Depth + 1, ResolveKnownElts)) | |||
7858 | return false; | |||
7859 | ||||
7860 | // Subvector shuffle inputs must not be larger than the subvector. | |||
7861 | if (llvm::any_of(SubInputs, [SubVT](SDValue SubInput) { | |||
7862 | return SubVT.getFixedSizeInBits() < | |||
7863 | SubInput.getValueSizeInBits().getFixedSize(); | |||
7864 | })) | |||
7865 | return false; | |||
7866 | ||||
7867 | if (SubMask.size() != NumSubElts) { | |||
7868 | assert(((SubMask.size() % NumSubElts) == 0 ||(static_cast <bool> (((SubMask.size() % NumSubElts) == 0 || (NumSubElts % SubMask.size()) == 0) && "Illegal submask scale" ) ? void (0) : __assert_fail ("((SubMask.size() % NumSubElts) == 0 || (NumSubElts % SubMask.size()) == 0) && \"Illegal submask scale\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7869, __extension__ __PRETTY_FUNCTION__)) | |||
7869 | (NumSubElts % SubMask.size()) == 0) && "Illegal submask scale")(static_cast <bool> (((SubMask.size() % NumSubElts) == 0 || (NumSubElts % SubMask.size()) == 0) && "Illegal submask scale" ) ? void (0) : __assert_fail ("((SubMask.size() % NumSubElts) == 0 || (NumSubElts % SubMask.size()) == 0) && \"Illegal submask scale\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7869, __extension__ __PRETTY_FUNCTION__)); | |||
7870 | if ((NumSubElts % SubMask.size()) == 0) { | |||
7871 | int Scale = NumSubElts / SubMask.size(); | |||
7872 | SmallVector<int,64> ScaledSubMask; | |||
7873 | narrowShuffleMaskElts(Scale, SubMask, ScaledSubMask); | |||
7874 | SubMask = ScaledSubMask; | |||
7875 | } else { | |||
7876 | int Scale = SubMask.size() / NumSubElts; | |||
7877 | NumSubElts = SubMask.size(); | |||
7878 | NumElts *= Scale; | |||
7879 | InsertIdx *= Scale; | |||
7880 | } | |||
7881 | } | |||
7882 | Ops.push_back(Src); | |||
7883 | Ops.append(SubInputs.begin(), SubInputs.end()); | |||
7884 | if (ISD::isBuildVectorAllZeros(Src.getNode())) | |||
7885 | Mask.append(NumElts, SM_SentinelZero); | |||
7886 | else | |||
7887 | for (int i = 0; i != (int)NumElts; ++i) | |||
7888 | Mask.push_back(i); | |||
7889 | for (int i = 0; i != (int)NumSubElts; ++i) { | |||
7890 | int M = SubMask[i]; | |||
7891 | if (0 <= M) { | |||
7892 | int InputIdx = M / NumSubElts; | |||
7893 | M = (NumElts * (1 + InputIdx)) + (M % NumSubElts); | |||
7894 | } | |||
7895 | Mask[i + InsertIdx] = M; | |||
7896 | } | |||
7897 | return true; | |||
7898 | } | |||
7899 | case X86ISD::PINSRB: | |||
7900 | case X86ISD::PINSRW: | |||
7901 | case ISD::SCALAR_TO_VECTOR: | |||
7902 | case ISD::INSERT_VECTOR_ELT: { | |||
7903 | // Match against a insert_vector_elt/scalar_to_vector of an extract from a | |||
7904 | // vector, for matching src/dst vector types. | |||
7905 | SDValue Scl = N.getOperand(Opcode == ISD::SCALAR_TO_VECTOR ? 0 : 1); | |||
7906 | ||||
7907 | unsigned DstIdx = 0; | |||
7908 | if (Opcode != ISD::SCALAR_TO_VECTOR) { | |||
7909 | // Check we have an in-range constant insertion index. | |||
7910 | if (!isa<ConstantSDNode>(N.getOperand(2)) || | |||
7911 | N.getConstantOperandAPInt(2).uge(NumElts)) | |||
7912 | return false; | |||
7913 | DstIdx = N.getConstantOperandVal(2); | |||
7914 | ||||
7915 | // Attempt to recognise an INSERT*(VEC, 0, DstIdx) shuffle pattern. | |||
7916 | if (X86::isZeroNode(Scl)) { | |||
7917 | Ops.push_back(N.getOperand(0)); | |||
7918 | for (unsigned i = 0; i != NumElts; ++i) | |||
7919 | Mask.push_back(i == DstIdx ? SM_SentinelZero : (int)i); | |||
7920 | return true; | |||
7921 | } | |||
7922 | } | |||
7923 | ||||
7924 | // Peek through trunc/aext/zext. | |||
7925 | // TODO: aext shouldn't require SM_SentinelZero padding. | |||
7926 | // TODO: handle shift of scalars. | |||
7927 | unsigned MinBitsPerElt = Scl.getScalarValueSizeInBits(); | |||
7928 | while (Scl.getOpcode() == ISD::TRUNCATE || | |||
7929 | Scl.getOpcode() == ISD::ANY_EXTEND || | |||
7930 | Scl.getOpcode() == ISD::ZERO_EXTEND) { | |||
7931 | Scl = Scl.getOperand(0); | |||
7932 | MinBitsPerElt = | |||
7933 | std::min<unsigned>(MinBitsPerElt, Scl.getScalarValueSizeInBits()); | |||
7934 | } | |||
7935 | if ((MinBitsPerElt % 8) != 0) | |||
7936 | return false; | |||
7937 | ||||
7938 | // Attempt to find the source vector the scalar was extracted from. | |||
7939 | SDValue SrcExtract; | |||
7940 | if ((Scl.getOpcode() == ISD::EXTRACT_VECTOR_ELT || | |||
7941 | Scl.getOpcode() == X86ISD::PEXTRW || | |||
7942 | Scl.getOpcode() == X86ISD::PEXTRB) && | |||
7943 | Scl.getOperand(0).getValueSizeInBits() == NumSizeInBits) { | |||
7944 | SrcExtract = Scl; | |||
7945 | } | |||
7946 | if (!SrcExtract || !isa<ConstantSDNode>(SrcExtract.getOperand(1))) | |||
7947 | return false; | |||
7948 | ||||
7949 | SDValue SrcVec = SrcExtract.getOperand(0); | |||
7950 | EVT SrcVT = SrcVec.getValueType(); | |||
7951 | if (!SrcVT.getScalarType().isByteSized()) | |||
7952 | return false; | |||
7953 | unsigned SrcIdx = SrcExtract.getConstantOperandVal(1); | |||
7954 | unsigned SrcByte = SrcIdx * (SrcVT.getScalarSizeInBits() / 8); | |||
7955 | unsigned DstByte = DstIdx * NumBytesPerElt; | |||
7956 | MinBitsPerElt = | |||
7957 | std::min<unsigned>(MinBitsPerElt, SrcVT.getScalarSizeInBits()); | |||
7958 | ||||
7959 | // Create 'identity' byte level shuffle mask and then add inserted bytes. | |||
7960 | if (Opcode == ISD::SCALAR_TO_VECTOR) { | |||
7961 | Ops.push_back(SrcVec); | |||
7962 | Mask.append(NumSizeInBytes, SM_SentinelUndef); | |||
7963 | } else { | |||
7964 | Ops.push_back(SrcVec); | |||
7965 | Ops.push_back(N.getOperand(0)); | |||
7966 | for (int i = 0; i != (int)NumSizeInBytes; ++i) | |||
7967 | Mask.push_back(NumSizeInBytes + i); | |||
7968 | } | |||
7969 | ||||
7970 | unsigned MinBytesPerElts = MinBitsPerElt / 8; | |||
7971 | MinBytesPerElts = std::min(MinBytesPerElts, NumBytesPerElt); | |||
7972 | for (unsigned i = 0; i != MinBytesPerElts; ++i) | |||
7973 | Mask[DstByte + i] = SrcByte + i; | |||
7974 | for (unsigned i = MinBytesPerElts; i < NumBytesPerElt; ++i) | |||
7975 | Mask[DstByte + i] = SM_SentinelZero; | |||
7976 | return true; | |||
7977 | } | |||
7978 | case X86ISD::PACKSS: | |||
7979 | case X86ISD::PACKUS: { | |||
7980 | SDValue N0 = N.getOperand(0); | |||
7981 | SDValue N1 = N.getOperand(1); | |||
7982 | assert(N0.getValueType().getVectorNumElements() == (NumElts / 2) &&(static_cast <bool> (N0.getValueType().getVectorNumElements () == (NumElts / 2) && N1.getValueType().getVectorNumElements () == (NumElts / 2) && "Unexpected input value type") ? void (0) : __assert_fail ("N0.getValueType().getVectorNumElements() == (NumElts / 2) && N1.getValueType().getVectorNumElements() == (NumElts / 2) && \"Unexpected input value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7984, __extension__ __PRETTY_FUNCTION__)) | |||
7983 | N1.getValueType().getVectorNumElements() == (NumElts / 2) &&(static_cast <bool> (N0.getValueType().getVectorNumElements () == (NumElts / 2) && N1.getValueType().getVectorNumElements () == (NumElts / 2) && "Unexpected input value type") ? void (0) : __assert_fail ("N0.getValueType().getVectorNumElements() == (NumElts / 2) && N1.getValueType().getVectorNumElements() == (NumElts / 2) && \"Unexpected input value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7984, __extension__ __PRETTY_FUNCTION__)) | |||
7984 | "Unexpected input value type")(static_cast <bool> (N0.getValueType().getVectorNumElements () == (NumElts / 2) && N1.getValueType().getVectorNumElements () == (NumElts / 2) && "Unexpected input value type") ? void (0) : __assert_fail ("N0.getValueType().getVectorNumElements() == (NumElts / 2) && N1.getValueType().getVectorNumElements() == (NumElts / 2) && \"Unexpected input value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 7984, __extension__ __PRETTY_FUNCTION__)); | |||
7985 | ||||
7986 | APInt EltsLHS, EltsRHS; | |||
7987 | getPackDemandedElts(VT, DemandedElts, EltsLHS, EltsRHS); | |||
7988 | ||||
7989 | // If we know input saturation won't happen (or we don't care for particular | |||
7990 | // lanes), we can treat this as a truncation shuffle. | |||
7991 | bool Offset0 = false, Offset1 = false; | |||
7992 | if (Opcode == X86ISD::PACKSS) { | |||
7993 | if ((!(N0.isUndef() || EltsLHS.isZero()) && | |||
7994 | DAG.ComputeNumSignBits(N0, EltsLHS, Depth + 1) <= NumBitsPerElt) || | |||
7995 | (!(N1.isUndef() || EltsRHS.isZero()) && | |||
7996 | DAG.ComputeNumSignBits(N1, EltsRHS, Depth + 1) <= NumBitsPerElt)) | |||
7997 | return false; | |||
7998 | // We can't easily fold ASHR into a shuffle, but if it was feeding a | |||
7999 | // PACKSS then it was likely being used for sign-extension for a | |||
8000 | // truncation, so just peek through and adjust the mask accordingly. | |||
8001 | if (N0.getOpcode() == X86ISD::VSRAI && N->isOnlyUserOf(N0.getNode()) && | |||
8002 | N0.getConstantOperandAPInt(1) == NumBitsPerElt) { | |||
8003 | Offset0 = true; | |||
8004 | N0 = N0.getOperand(0); | |||
8005 | } | |||
8006 | if (N1.getOpcode() == X86ISD::VSRAI && N->isOnlyUserOf(N1.getNode()) && | |||
8007 | N1.getConstantOperandAPInt(1) == NumBitsPerElt) { | |||
8008 | Offset1 = true; | |||
8009 | N1 = N1.getOperand(0); | |||
8010 | } | |||
8011 | } else { | |||
8012 | APInt ZeroMask = APInt::getHighBitsSet(2 * NumBitsPerElt, NumBitsPerElt); | |||
8013 | if ((!(N0.isUndef() || EltsLHS.isZero()) && | |||
8014 | !DAG.MaskedValueIsZero(N0, ZeroMask, EltsLHS, Depth + 1)) || | |||
8015 | (!(N1.isUndef() || EltsRHS.isZero()) && | |||
8016 | !DAG.MaskedValueIsZero(N1, ZeroMask, EltsRHS, Depth + 1))) | |||
8017 | return false; | |||
8018 | } | |||
8019 | ||||
8020 | bool IsUnary = (N0 == N1); | |||
8021 | ||||
8022 | Ops.push_back(N0); | |||
8023 | if (!IsUnary) | |||
8024 | Ops.push_back(N1); | |||
8025 | ||||
8026 | createPackShuffleMask(VT, Mask, IsUnary); | |||
8027 | ||||
8028 | if (Offset0 || Offset1) { | |||
8029 | for (int &M : Mask) | |||
8030 | if ((Offset0 && isInRange(M, 0, NumElts)) || | |||
8031 | (Offset1 && isInRange(M, NumElts, 2 * NumElts))) | |||
8032 | ++M; | |||
8033 | } | |||
8034 | return true; | |||
8035 | } | |||
8036 | case X86ISD::VTRUNC: { | |||
8037 | SDValue Src = N.getOperand(0); | |||
8038 | EVT SrcVT = Src.getValueType(); | |||
8039 | // Truncated source must be a simple vector. | |||
8040 | if (!SrcVT.isSimple() || (SrcVT.getSizeInBits() % 128) != 0 || | |||
8041 | (SrcVT.getScalarSizeInBits() % 8) != 0) | |||
8042 | return false; | |||
8043 | unsigned NumSrcElts = SrcVT.getVectorNumElements(); | |||
8044 | unsigned NumBitsPerSrcElt = SrcVT.getScalarSizeInBits(); | |||
8045 | unsigned Scale = NumBitsPerSrcElt / NumBitsPerElt; | |||
8046 | assert((NumBitsPerSrcElt % NumBitsPerElt) == 0 && "Illegal truncation")(static_cast <bool> ((NumBitsPerSrcElt % NumBitsPerElt) == 0 && "Illegal truncation") ? void (0) : __assert_fail ("(NumBitsPerSrcElt % NumBitsPerElt) == 0 && \"Illegal truncation\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 8046, __extension__ __PRETTY_FUNCTION__)); | |||
8047 | for (unsigned i = 0; i != NumSrcElts; ++i) | |||
8048 | Mask.push_back(i * Scale); | |||
8049 | Mask.append(NumElts - NumSrcElts, SM_SentinelZero); | |||
8050 | Ops.push_back(Src); | |||
8051 | return true; | |||
8052 | } | |||
8053 | case X86ISD::VSHLI: | |||
8054 | case X86ISD::VSRLI: { | |||
8055 | uint64_t ShiftVal = N.getConstantOperandVal(1); | |||
8056 | // Out of range bit shifts are guaranteed to be zero. | |||
8057 | if (NumBitsPerElt <= ShiftVal) { | |||
8058 | Mask.append(NumElts, SM_SentinelZero); | |||
8059 | return true; | |||
8060 | } | |||
8061 | ||||
8062 | // We can only decode 'whole byte' bit shifts as shuffles. | |||
8063 | if ((ShiftVal % 8) != 0) | |||
8064 | break; | |||
8065 | ||||
8066 | uint64_t ByteShift = ShiftVal / 8; | |||
8067 | Ops.push_back(N.getOperand(0)); | |||
8068 | ||||
8069 | // Clear mask to all zeros and insert the shifted byte indices. | |||
8070 | Mask.append(NumSizeInBytes, SM_SentinelZero); | |||
8071 | ||||
8072 | if (X86ISD::VSHLI == Opcode) { | |||
8073 | for (unsigned i = 0; i != NumSizeInBytes; i += NumBytesPerElt) | |||
8074 | for (unsigned j = ByteShift; j != NumBytesPerElt; ++j) | |||
8075 | Mask[i + j] = i + j - ByteShift; | |||
8076 | } else { | |||
8077 | for (unsigned i = 0; i != NumSizeInBytes; i += NumBytesPerElt) | |||
8078 | for (unsigned j = ByteShift; j != NumBytesPerElt; ++j) | |||
8079 | Mask[i + j - ByteShift] = i + j; | |||
8080 | } | |||
8081 | return true; | |||
8082 | } | |||
8083 | case X86ISD::VROTLI: | |||
8084 | case X86ISD::VROTRI: { | |||
8085 | // We can only decode 'whole byte' bit rotates as shuffles. | |||
8086 | uint64_t RotateVal = N.getConstantOperandAPInt(1).urem(NumBitsPerElt); | |||
8087 | if ((RotateVal % 8) != 0) | |||
8088 | return false; | |||
8089 | Ops.push_back(N.getOperand(0)); | |||
8090 | int Offset = RotateVal / 8; | |||
8091 | Offset = (X86ISD::VROTLI == Opcode ? NumBytesPerElt - Offset : Offset); | |||
8092 | for (int i = 0; i != (int)NumElts; ++i) { | |||
8093 | int BaseIdx = i * NumBytesPerElt; | |||
8094 | for (int j = 0; j != (int)NumBytesPerElt; ++j) { | |||
8095 | Mask.push_back(BaseIdx + ((Offset + j) % NumBytesPerElt)); | |||
8096 | } | |||
8097 | } | |||
8098 | return true; | |||
8099 | } | |||
8100 | case X86ISD::VBROADCAST: { | |||
8101 | SDValue Src = N.getOperand(0); | |||
8102 | if (!Src.getSimpleValueType().isVector()) { | |||
8103 | if (Src.getOpcode() != ISD::EXTRACT_VECTOR_ELT || | |||
8104 | !isNullConstant(Src.getOperand(1)) || | |||
8105 | Src.getOperand(0).getValueType().getScalarType() != | |||
8106 | VT.getScalarType()) | |||
8107 | return false; | |||
8108 | Src = Src.getOperand(0); | |||
8109 | } | |||
8110 | Ops.push_back(Src); | |||
8111 | Mask.append(NumElts, 0); | |||
8112 | return true; | |||
8113 | } | |||
8114 | case ISD::ZERO_EXTEND: | |||
8115 | case ISD::ANY_EXTEND: | |||
8116 | case ISD::ZERO_EXTEND_VECTOR_INREG: | |||
8117 | case ISD::ANY_EXTEND_VECTOR_INREG: { | |||
8118 | SDValue Src = N.getOperand(0); | |||
8119 | EVT SrcVT = Src.getValueType(); | |||
8120 | ||||
8121 | // Extended source must be a simple vector. | |||
8122 | if (!SrcVT.isSimple() || (SrcVT.getSizeInBits() % 128) != 0 || | |||
8123 | (SrcVT.getScalarSizeInBits() % 8) != 0) | |||
8124 | return false; | |||
8125 | ||||
8126 | bool IsAnyExtend = | |||
8127 | (ISD::ANY_EXTEND == Opcode || ISD::ANY_EXTEND_VECTOR_INREG == Opcode); | |||
8128 | DecodeZeroExtendMask(SrcVT.getScalarSizeInBits(), NumBitsPerElt, NumElts, | |||
8129 | IsAnyExtend, Mask); | |||
8130 | Ops.push_back(Src); | |||
8131 | return true; | |||
8132 | } | |||
8133 | } | |||
8134 | ||||
8135 | return false; | |||
8136 | } | |||
8137 | ||||
8138 | /// Removes unused/repeated shuffle source inputs and adjusts the shuffle mask. | |||
8139 | static void resolveTargetShuffleInputsAndMask(SmallVectorImpl<SDValue> &Inputs, | |||
8140 | SmallVectorImpl<int> &Mask) { | |||
8141 | int MaskWidth = Mask.size(); | |||
8142 | SmallVector<SDValue, 16> UsedInputs; | |||
8143 | for (int i = 0, e = Inputs.size(); i < e; ++i) { | |||
8144 | int lo = UsedInputs.size() * MaskWidth; | |||
8145 | int hi = lo + MaskWidth; | |||
8146 | ||||
8147 | // Strip UNDEF input usage. | |||
8148 | if (Inputs[i].isUndef()) | |||
8149 | for (int &M : Mask) | |||
8150 | if ((lo <= M) && (M < hi)) | |||
8151 | M = SM_SentinelUndef; | |||
8152 | ||||
8153 | // Check for unused inputs. | |||
8154 | if (none_of(Mask, [lo, hi](int i) { return (lo <= i) && (i < hi); })) { | |||
8155 | for (int &M : Mask) | |||
8156 | if (lo <= M) | |||
8157 | M -= MaskWidth; | |||
8158 | continue; | |||
8159 | } | |||
8160 | ||||
8161 | // Check for repeated inputs. | |||
8162 | bool IsRepeat = false; | |||
8163 | for (int j = 0, ue = UsedInputs.size(); j != ue; ++j) { | |||
8164 | if (UsedInputs[j] != Inputs[i]) | |||
8165 | continue; | |||
8166 | for (int &M : Mask) | |||
8167 | if (lo <= M) | |||
8168 | M = (M < hi) ? ((M - lo) + (j * MaskWidth)) : (M - MaskWidth); | |||
8169 | IsRepeat = true; | |||
8170 | break; | |||
8171 | } | |||
8172 | if (IsRepeat) | |||
8173 | continue; | |||
8174 | ||||
8175 | UsedInputs.push_back(Inputs[i]); | |||
8176 | } | |||
8177 | Inputs = UsedInputs; | |||
8178 | } | |||
8179 | ||||
8180 | /// Calls getTargetShuffleAndZeroables to resolve a target shuffle mask's inputs | |||
8181 | /// and then sets the SM_SentinelUndef and SM_SentinelZero values. | |||
8182 | /// Returns true if the target shuffle mask was decoded. | |||
8183 | static bool getTargetShuffleInputs(SDValue Op, const APInt &DemandedElts, | |||
8184 | SmallVectorImpl<SDValue> &Inputs, | |||
8185 | SmallVectorImpl<int> &Mask, | |||
8186 | APInt &KnownUndef, APInt &KnownZero, | |||
8187 | const SelectionDAG &DAG, unsigned Depth, | |||
8188 | bool ResolveKnownElts) { | |||
8189 | EVT VT = Op.getValueType(); | |||
8190 | if (!VT.isSimple() || !VT.isVector()) | |||
8191 | return false; | |||
8192 | ||||
8193 | if (getTargetShuffleAndZeroables(Op, Mask, Inputs, KnownUndef, KnownZero)) { | |||
8194 | if (ResolveKnownElts) | |||
8195 | resolveTargetShuffleFromZeroables(Mask, KnownUndef, KnownZero); | |||
8196 | return true; | |||
8197 | } | |||
8198 | if (getFauxShuffleMask(Op, DemandedElts, Mask, Inputs, DAG, Depth, | |||
8199 | ResolveKnownElts)) { | |||
8200 | resolveZeroablesFromTargetShuffle(Mask, KnownUndef, KnownZero); | |||
8201 | return true; | |||
8202 | } | |||
8203 | return false; | |||
8204 | } | |||
8205 | ||||
8206 | static bool getTargetShuffleInputs(SDValue Op, SmallVectorImpl<SDValue> &Inputs, | |||
8207 | SmallVectorImpl<int> &Mask, | |||
8208 | const SelectionDAG &DAG, unsigned Depth = 0, | |||
8209 | bool ResolveKnownElts = true) { | |||
8210 | EVT VT = Op.getValueType(); | |||
8211 | if (!VT.isSimple() || !VT.isVector()) | |||
8212 | return false; | |||
8213 | ||||
8214 | APInt KnownUndef, KnownZero; | |||
8215 | unsigned NumElts = Op.getValueType().getVectorNumElements(); | |||
8216 | APInt DemandedElts = APInt::getAllOnes(NumElts); | |||
8217 | return getTargetShuffleInputs(Op, DemandedElts, Inputs, Mask, KnownUndef, | |||
8218 | KnownZero, DAG, Depth, ResolveKnownElts); | |||
8219 | } | |||
8220 | ||||
8221 | // Attempt to create a scalar/subvector broadcast from the base MemSDNode. | |||
8222 | static SDValue getBROADCAST_LOAD(unsigned Opcode, const SDLoc &DL, EVT VT, | |||
8223 | EVT MemVT, MemSDNode *Mem, unsigned Offset, | |||
8224 | SelectionDAG &DAG) { | |||
8225 | assert((Opcode == X86ISD::VBROADCAST_LOAD ||(static_cast <bool> ((Opcode == X86ISD::VBROADCAST_LOAD || Opcode == X86ISD::SUBV_BROADCAST_LOAD) && "Unknown broadcast load type" ) ? void (0) : __assert_fail ("(Opcode == X86ISD::VBROADCAST_LOAD || Opcode == X86ISD::SUBV_BROADCAST_LOAD) && \"Unknown broadcast load type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 8227, __extension__ __PRETTY_FUNCTION__)) | |||
8226 | Opcode == X86ISD::SUBV_BROADCAST_LOAD) &&(static_cast <bool> ((Opcode == X86ISD::VBROADCAST_LOAD || Opcode == X86ISD::SUBV_BROADCAST_LOAD) && "Unknown broadcast load type" ) ? void (0) : __assert_fail ("(Opcode == X86ISD::VBROADCAST_LOAD || Opcode == X86ISD::SUBV_BROADCAST_LOAD) && \"Unknown broadcast load type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 8227, __extension__ __PRETTY_FUNCTION__)) | |||
8227 | "Unknown broadcast load type")(static_cast <bool> ((Opcode == X86ISD::VBROADCAST_LOAD || Opcode == X86ISD::SUBV_BROADCAST_LOAD) && "Unknown broadcast load type" ) ? void (0) : __assert_fail ("(Opcode == X86ISD::VBROADCAST_LOAD || Opcode == X86ISD::SUBV_BROADCAST_LOAD) && \"Unknown broadcast load type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 8227, __extension__ __PRETTY_FUNCTION__)); | |||
8228 | ||||
8229 | // Ensure this is a simple (non-atomic, non-voltile), temporal read memop. | |||
8230 | if (!Mem || !Mem->readMem() || !Mem->isSimple() || Mem->isNonTemporal()) | |||
8231 | return SDValue(); | |||
8232 | ||||
8233 | SDValue Ptr = | |||
8234 | DAG.getMemBasePlusOffset(Mem->getBasePtr(), TypeSize::Fixed(Offset), DL); | |||
8235 | SDVTList Tys = DAG.getVTList(VT, MVT::Other); | |||
8236 | SDValue Ops[] = {Mem->getChain(), Ptr}; | |||
8237 | SDValue BcstLd = DAG.getMemIntrinsicNode( | |||
8238 | Opcode, DL, Tys, Ops, MemVT, | |||
8239 | DAG.getMachineFunction().getMachineMemOperand( | |||
8240 | Mem->getMemOperand(), Offset, MemVT.getStoreSize())); | |||
8241 | DAG.makeEquivalentMemoryOrdering(SDValue(Mem, 1), BcstLd.getValue(1)); | |||
8242 | return BcstLd; | |||
8243 | } | |||
8244 | ||||
8245 | /// Returns the scalar element that will make up the i'th | |||
8246 | /// element of the result of the vector shuffle. | |||
8247 | static SDValue getShuffleScalarElt(SDValue Op, unsigned Index, | |||
8248 | SelectionDAG &DAG, unsigned Depth) { | |||
8249 | if (Depth >= SelectionDAG::MaxRecursionDepth) | |||
8250 | return SDValue(); // Limit search depth. | |||
8251 | ||||
8252 | EVT VT = Op.getValueType(); | |||
8253 | unsigned Opcode = Op.getOpcode(); | |||
8254 | unsigned NumElems = VT.getVectorNumElements(); | |||
8255 | ||||
8256 | // Recurse into ISD::VECTOR_SHUFFLE node to find scalars. | |||
8257 | if (auto *SV = dyn_cast<ShuffleVectorSDNode>(Op)) { | |||
8258 | int Elt = SV->getMaskElt(Index); | |||
8259 | ||||
8260 | if (Elt < 0) | |||
8261 | return DAG.getUNDEF(VT.getVectorElementType()); | |||
8262 | ||||
8263 | SDValue Src = (Elt < (int)NumElems) ? SV->getOperand(0) : SV->getOperand(1); | |||
8264 | return getShuffleScalarElt(Src, Elt % NumElems, DAG, Depth + 1); | |||
8265 | } | |||
8266 | ||||
8267 | // Recurse into target specific vector shuffles to find scalars. | |||
8268 | if (isTargetShuffle(Opcode)) { | |||
8269 | MVT ShufVT = VT.getSimpleVT(); | |||
8270 | MVT ShufSVT = ShufVT.getVectorElementType(); | |||
8271 | int NumElems = (int)ShufVT.getVectorNumElements(); | |||
8272 | SmallVector<int, 16> ShuffleMask; | |||
8273 | SmallVector<SDValue, 16> ShuffleOps; | |||
8274 | if (!getTargetShuffleMask(Op.getNode(), ShufVT, true, ShuffleOps, | |||
8275 | ShuffleMask)) | |||
8276 | return SDValue(); | |||
8277 | ||||
8278 | int Elt = ShuffleMask[Index]; | |||
8279 | if (Elt == SM_SentinelZero) | |||
8280 | return ShufSVT.isInteger() ? DAG.getConstant(0, SDLoc(Op), ShufSVT) | |||
8281 | : DAG.getConstantFP(+0.0, SDLoc(Op), ShufSVT); | |||
8282 | if (Elt == SM_SentinelUndef) | |||
8283 | return DAG.getUNDEF(ShufSVT); | |||
8284 | ||||
8285 | assert(0 <= Elt && Elt < (2 * NumElems) && "Shuffle index out of range")(static_cast <bool> (0 <= Elt && Elt < (2 * NumElems) && "Shuffle index out of range") ? void ( 0) : __assert_fail ("0 <= Elt && Elt < (2 * NumElems) && \"Shuffle index out of range\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 8285, __extension__ __PRETTY_FUNCTION__)); | |||
8286 | SDValue Src = (Elt < NumElems) ? ShuffleOps[0] : ShuffleOps[1]; | |||
8287 | return getShuffleScalarElt(Src, Elt % NumElems, DAG, Depth + 1); | |||
8288 | } | |||
8289 | ||||
8290 | // Recurse into insert_subvector base/sub vector to find scalars. | |||
8291 | if (Opcode == ISD::INSERT_SUBVECTOR) { | |||
8292 | SDValue Vec = Op.getOperand(0); | |||
8293 | SDValue Sub = Op.getOperand(1); | |||
8294 | uint64_t SubIdx = Op.getConstantOperandVal(2); | |||
8295 | unsigned NumSubElts = Sub.getValueType().getVectorNumElements(); | |||
8296 | ||||
8297 | if (SubIdx <= Index && Index < (SubIdx + NumSubElts)) | |||
8298 | return getShuffleScalarElt(Sub, Index - SubIdx, DAG, Depth + 1); | |||
8299 | return getShuffleScalarElt(Vec, Index, DAG, Depth + 1); | |||
8300 | } | |||
8301 | ||||
8302 | // Recurse into concat_vectors sub vector to find scalars. | |||
8303 | if (Opcode == ISD::CONCAT_VECTORS) { | |||
8304 | EVT SubVT = Op.getOperand(0).getValueType(); | |||
8305 | unsigned NumSubElts = SubVT.getVectorNumElements(); | |||
8306 | uint64_t SubIdx = Index / NumSubElts; | |||
8307 | uint64_t SubElt = Index % NumSubElts; | |||
8308 | return getShuffleScalarElt(Op.getOperand(SubIdx), SubElt, DAG, Depth + 1); | |||
8309 | } | |||
8310 | ||||
8311 | // Recurse into extract_subvector src vector to find scalars. | |||
8312 | if (Opcode == ISD::EXTRACT_SUBVECTOR) { | |||
8313 | SDValue Src = Op.getOperand(0); | |||
8314 | uint64_t SrcIdx = Op.getConstantOperandVal(1); | |||
8315 | return getShuffleScalarElt(Src, Index + SrcIdx, DAG, Depth + 1); | |||
8316 | } | |||
8317 | ||||
8318 | // We only peek through bitcasts of the same vector width. | |||
8319 | if (Opcode == ISD::BITCAST) { | |||
8320 | SDValue Src = Op.getOperand(0); | |||
8321 | EVT SrcVT = Src.getValueType(); | |||
8322 | if (SrcVT.isVector() && SrcVT.getVectorNumElements() == NumElems) | |||
8323 | return getShuffleScalarElt(Src, Index, DAG, Depth + 1); | |||
8324 | return SDValue(); | |||
8325 | } | |||
8326 | ||||
8327 | // Actual nodes that may contain scalar elements | |||
8328 | ||||
8329 | // For insert_vector_elt - either return the index matching scalar or recurse | |||
8330 | // into the base vector. | |||
8331 | if (Opcode == ISD::INSERT_VECTOR_ELT && | |||
8332 | isa<ConstantSDNode>(Op.getOperand(2))) { | |||
8333 | if (Op.getConstantOperandAPInt(2) == Index) | |||
8334 | return Op.getOperand(1); | |||
8335 | return getShuffleScalarElt(Op.getOperand(0), Index, DAG, Depth + 1); | |||
8336 | } | |||
8337 | ||||
8338 | if (Opcode == ISD::SCALAR_TO_VECTOR) | |||
8339 | return (Index == 0) ? Op.getOperand(0) | |||
8340 | : DAG.getUNDEF(VT.getVectorElementType()); | |||
8341 | ||||
8342 | if (Opcode == ISD::BUILD_VECTOR) | |||
8343 | return Op.getOperand(Index); | |||
8344 | ||||
8345 | return SDValue(); | |||
8346 | } | |||
8347 | ||||
8348 | // Use PINSRB/PINSRW/PINSRD to create a build vector. | |||
8349 | static SDValue LowerBuildVectorAsInsert(SDValue Op, const APInt &NonZeroMask, | |||
8350 | unsigned NumNonZero, unsigned NumZero, | |||
8351 | SelectionDAG &DAG, | |||
8352 | const X86Subtarget &Subtarget) { | |||
8353 | MVT VT = Op.getSimpleValueType(); | |||
8354 | unsigned NumElts = VT.getVectorNumElements(); | |||
8355 | assert(((VT == MVT::v8i16 && Subtarget.hasSSE2()) ||(static_cast <bool> (((VT == MVT::v8i16 && Subtarget .hasSSE2()) || ((VT == MVT::v16i8 || VT == MVT::v4i32) && Subtarget.hasSSE41())) && "Illegal vector insertion" ) ? void (0) : __assert_fail ("((VT == MVT::v8i16 && Subtarget.hasSSE2()) || ((VT == MVT::v16i8 || VT == MVT::v4i32) && Subtarget.hasSSE41())) && \"Illegal vector insertion\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 8357, __extension__ __PRETTY_FUNCTION__)) | |||
8356 | ((VT == MVT::v16i8 || VT == MVT::v4i32) && Subtarget.hasSSE41())) &&(static_cast <bool> (((VT == MVT::v8i16 && Subtarget .hasSSE2()) || ((VT == MVT::v16i8 || VT == MVT::v4i32) && Subtarget.hasSSE41())) && "Illegal vector insertion" ) ? void (0) : __assert_fail ("((VT == MVT::v8i16 && Subtarget.hasSSE2()) || ((VT == MVT::v16i8 || VT == MVT::v4i32) && Subtarget.hasSSE41())) && \"Illegal vector insertion\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 8357, __extension__ __PRETTY_FUNCTION__)) | |||
8357 | "Illegal vector insertion")(static_cast <bool> (((VT == MVT::v8i16 && Subtarget .hasSSE2()) || ((VT == MVT::v16i8 || VT == MVT::v4i32) && Subtarget.hasSSE41())) && "Illegal vector insertion" ) ? void (0) : __assert_fail ("((VT == MVT::v8i16 && Subtarget.hasSSE2()) || ((VT == MVT::v16i8 || VT == MVT::v4i32) && Subtarget.hasSSE41())) && \"Illegal vector insertion\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 8357, __extension__ __PRETTY_FUNCTION__)); | |||
8358 | ||||
8359 | SDLoc dl(Op); | |||
8360 | SDValue V; | |||
8361 | bool First = true; | |||
8362 | ||||
8363 | for (unsigned i = 0; i < NumElts; ++i) { | |||
8364 | bool IsNonZero = NonZeroMask[i]; | |||
8365 | if (!IsNonZero) | |||
8366 | continue; | |||
8367 | ||||
8368 | // If the build vector contains zeros or our first insertion is not the | |||
8369 | // first index then insert into zero vector to break any register | |||
8370 | // dependency else use SCALAR_TO_VECTOR. | |||
8371 | if (First) { | |||
8372 | First = false; | |||
8373 | if (NumZero || 0 != i) | |||
8374 | V = getZeroVector(VT, Subtarget, DAG, dl); | |||
8375 | else { | |||
8376 | assert(0 == i && "Expected insertion into zero-index")(static_cast <bool> (0 == i && "Expected insertion into zero-index" ) ? void (0) : __assert_fail ("0 == i && \"Expected insertion into zero-index\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 8376, __extension__ __PRETTY_FUNCTION__)); | |||
8377 | V = DAG.getAnyExtOrTrunc(Op.getOperand(i), dl, MVT::i32); | |||
8378 | V = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, V); | |||
8379 | V = DAG.getBitcast(VT, V); | |||
8380 | continue; | |||
8381 | } | |||
8382 | } | |||
8383 | V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V, Op.getOperand(i), | |||
8384 | DAG.getIntPtrConstant(i, dl)); | |||
8385 | } | |||
8386 | ||||
8387 | return V; | |||
8388 | } | |||
8389 | ||||
8390 | /// Custom lower build_vector of v16i8. | |||
8391 | static SDValue LowerBuildVectorv16i8(SDValue Op, const APInt &NonZeroMask, | |||
8392 | unsigned NumNonZero, unsigned NumZero, | |||
8393 | SelectionDAG &DAG, | |||
8394 | const X86Subtarget &Subtarget) { | |||
8395 | if (NumNonZero > 8 && !Subtarget.hasSSE41()) | |||
8396 | return SDValue(); | |||
8397 | ||||
8398 | // SSE4.1 - use PINSRB to insert each byte directly. | |||
8399 | if (Subtarget.hasSSE41()) | |||
8400 | return LowerBuildVectorAsInsert(Op, NonZeroMask, NumNonZero, NumZero, DAG, | |||
8401 | Subtarget); | |||
8402 | ||||
8403 | SDLoc dl(Op); | |||
8404 | SDValue V; | |||
8405 | ||||
8406 | // Pre-SSE4.1 - merge byte pairs and insert with PINSRW. | |||
8407 | for (unsigned i = 0; i < 16; i += 2) { | |||
8408 | bool ThisIsNonZero = NonZeroMask[i]; | |||
8409 | bool NextIsNonZero = NonZeroMask[i + 1]; | |||
8410 | if (!ThisIsNonZero && !NextIsNonZero) | |||
8411 | continue; | |||
8412 | ||||
8413 | // FIXME: Investigate combining the first 4 bytes as a i32 instead. | |||
8414 | SDValue Elt; | |||
8415 | if (ThisIsNonZero) { | |||
8416 | if (NumZero || NextIsNonZero) | |||
8417 | Elt = DAG.getZExtOrTrunc(Op.getOperand(i), dl, MVT::i32); | |||
8418 | else | |||
8419 | Elt = DAG.getAnyExtOrTrunc(Op.getOperand(i), dl, MVT::i32); | |||
8420 | } | |||
8421 | ||||
8422 | if (NextIsNonZero) { | |||
8423 | SDValue NextElt = Op.getOperand(i + 1); | |||
8424 | if (i == 0 && NumZero) | |||
8425 | NextElt = DAG.getZExtOrTrunc(NextElt, dl, MVT::i32); | |||
8426 | else | |||
8427 | NextElt = DAG.getAnyExtOrTrunc(NextElt, dl, MVT::i32); | |||
8428 | NextElt = DAG.getNode(ISD::SHL, dl, MVT::i32, NextElt, | |||
8429 | DAG.getConstant(8, dl, MVT::i8)); | |||
8430 | if (ThisIsNonZero) | |||
8431 | Elt = DAG.getNode(ISD::OR, dl, MVT::i32, NextElt, Elt); | |||
8432 | else | |||
8433 | Elt = NextElt; | |||
8434 | } | |||
8435 | ||||
8436 | // If our first insertion is not the first index or zeros are needed, then | |||
8437 | // insert into zero vector. Otherwise, use SCALAR_TO_VECTOR (leaves high | |||
8438 | // elements undefined). | |||
8439 | if (!V) { | |||
8440 | if (i != 0 || NumZero) | |||
8441 | V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl); | |||
8442 | else { | |||
8443 | V = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Elt); | |||
8444 | V = DAG.getBitcast(MVT::v8i16, V); | |||
8445 | continue; | |||
8446 | } | |||
8447 | } | |||
8448 | Elt = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Elt); | |||
8449 | V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, Elt, | |||
8450 | DAG.getIntPtrConstant(i / 2, dl)); | |||
8451 | } | |||
8452 | ||||
8453 | return DAG.getBitcast(MVT::v16i8, V); | |||
8454 | } | |||
8455 | ||||
8456 | /// Custom lower build_vector of v8i16. | |||
8457 | static SDValue LowerBuildVectorv8i16(SDValue Op, const APInt &NonZeroMask, | |||
8458 | unsigned NumNonZero, unsigned NumZero, | |||
8459 | SelectionDAG &DAG, | |||
8460 | const X86Subtarget &Subtarget) { | |||
8461 | if (NumNonZero > 4 && !Subtarget.hasSSE41()) | |||
8462 | return SDValue(); | |||
8463 | ||||
8464 | // Use PINSRW to insert each byte directly. | |||
8465 | return LowerBuildVectorAsInsert(Op, NonZeroMask, NumNonZero, NumZero, DAG, | |||
8466 | Subtarget); | |||
8467 | } | |||
8468 | ||||
8469 | /// Custom lower build_vector of v4i32 or v4f32. | |||
8470 | static SDValue LowerBuildVectorv4x32(SDValue Op, SelectionDAG &DAG, | |||
8471 | const X86Subtarget &Subtarget) { | |||
8472 | // If this is a splat of a pair of elements, use MOVDDUP (unless the target | |||
8473 | // has XOP; in that case defer lowering to potentially use VPERMIL2PS). | |||
8474 | // Because we're creating a less complicated build vector here, we may enable | |||
8475 | // further folding of the MOVDDUP via shuffle transforms. | |||
8476 | if (Subtarget.hasSSE3() && !Subtarget.hasXOP() && | |||
8477 | Op.getOperand(0) == Op.getOperand(2) && | |||
8478 | Op.getOperand(1) == Op.getOperand(3) && | |||
8479 | Op.getOperand(0) != Op.getOperand(1)) { | |||
8480 | SDLoc DL(Op); | |||
8481 | MVT VT = Op.getSimpleValueType(); | |||
8482 | MVT EltVT = VT.getVectorElementType(); | |||
8483 | // Create a new build vector with the first 2 elements followed by undef | |||
8484 | // padding, bitcast to v2f64, duplicate, and bitcast back. | |||
8485 | SDValue Ops[4] = { Op.getOperand(0), Op.getOperand(1), | |||
8486 | DAG.getUNDEF(EltVT), DAG.getUNDEF(EltVT) }; | |||
8487 | SDValue NewBV = DAG.getBitcast(MVT::v2f64, DAG.getBuildVector(VT, DL, Ops)); | |||
8488 | SDValue Dup = DAG.getNode(X86ISD::MOVDDUP, DL, MVT::v2f64, NewBV); | |||
8489 | return DAG.getBitcast(VT, Dup); | |||
8490 | } | |||
8491 | ||||
8492 | // Find all zeroable elements. | |||
8493 | std::bitset<4> Zeroable, Undefs; | |||
8494 | for (int i = 0; i < 4; ++i) { | |||
8495 | SDValue Elt = Op.getOperand(i); | |||
8496 | Undefs[i] = Elt.isUndef(); | |||
8497 | Zeroable[i] = (Elt.isUndef() || X86::isZeroNode(Elt)); | |||
8498 | } | |||
8499 | assert(Zeroable.size() - Zeroable.count() > 1 &&(static_cast <bool> (Zeroable.size() - Zeroable.count() > 1 && "We expect at least two non-zero elements!" ) ? void (0) : __assert_fail ("Zeroable.size() - Zeroable.count() > 1 && \"We expect at least two non-zero elements!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 8500, __extension__ __PRETTY_FUNCTION__)) | |||
8500 | "We expect at least two non-zero elements!")(static_cast <bool> (Zeroable.size() - Zeroable.count() > 1 && "We expect at least two non-zero elements!" ) ? void (0) : __assert_fail ("Zeroable.size() - Zeroable.count() > 1 && \"We expect at least two non-zero elements!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 8500, __extension__ __PRETTY_FUNCTION__)); | |||
8501 | ||||
8502 | // We only know how to deal with build_vector nodes where elements are either | |||
8503 | // zeroable or extract_vector_elt with constant index. | |||
8504 | SDValue FirstNonZero; | |||
8505 | unsigned FirstNonZeroIdx; | |||
8506 | for (unsigned i = 0; i < 4; ++i) { | |||
8507 | if (Zeroable[i]) | |||
8508 | continue; | |||
8509 | SDValue Elt = Op.getOperand(i); | |||
8510 | if (Elt.getOpcode() != ISD::EXTRACT_VECTOR_ELT || | |||
8511 | !isa<ConstantSDNode>(Elt.getOperand(1))) | |||
8512 | return SDValue(); | |||
8513 | // Make sure that this node is extracting from a 128-bit vector. | |||
8514 | MVT VT = Elt.getOperand(0).getSimpleValueType(); | |||
8515 | if (!VT.is128BitVector()) | |||
8516 | return SDValue(); | |||
8517 | if (!FirstNonZero.getNode()) { | |||
8518 | FirstNonZero = Elt; | |||
8519 | FirstNonZeroIdx = i; | |||
8520 | } | |||
8521 | } | |||
8522 | ||||
8523 | assert(FirstNonZero.getNode() && "Unexpected build vector of all zeros!")(static_cast <bool> (FirstNonZero.getNode() && "Unexpected build vector of all zeros!" ) ? void (0) : __assert_fail ("FirstNonZero.getNode() && \"Unexpected build vector of all zeros!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 8523, __extension__ __PRETTY_FUNCTION__)); | |||
8524 | SDValue V1 = FirstNonZero.getOperand(0); | |||
8525 | MVT VT = V1.getSimpleValueType(); | |||
8526 | ||||
8527 | // See if this build_vector can be lowered as a blend with zero. | |||
8528 | SDValue Elt; | |||
8529 | unsigned EltMaskIdx, EltIdx; | |||
8530 | int Mask[4]; | |||
8531 | for (EltIdx = 0; EltIdx < 4; ++EltIdx) { | |||
8532 | if (Zeroable[EltIdx]) { | |||
8533 | // The zero vector will be on the right hand side. | |||
8534 | Mask[EltIdx] = EltIdx+4; | |||
8535 | continue; | |||
8536 | } | |||
8537 | ||||
8538 | Elt = Op->getOperand(EltIdx); | |||
8539 | // By construction, Elt is a EXTRACT_VECTOR_ELT with constant index. | |||
8540 | EltMaskIdx = Elt.getConstantOperandVal(1); | |||
8541 | if (Elt.getOperand(0) != V1 || EltMaskIdx != EltIdx) | |||
8542 | break; | |||
8543 | Mask[EltIdx] = EltIdx; | |||
8544 | } | |||
8545 | ||||
8546 | if (EltIdx == 4) { | |||
8547 | // Let the shuffle legalizer deal with blend operations. | |||
8548 | SDValue VZeroOrUndef = (Zeroable == Undefs) | |||
8549 | ? DAG.getUNDEF(VT) | |||
8550 | : getZeroVector(VT, Subtarget, DAG, SDLoc(Op)); | |||
8551 | if (V1.getSimpleValueType() != VT) | |||
8552 | V1 = DAG.getBitcast(VT, V1); | |||
8553 | return DAG.getVectorShuffle(VT, SDLoc(V1), V1, VZeroOrUndef, Mask); | |||
8554 | } | |||
8555 | ||||
8556 | // See if we can lower this build_vector to a INSERTPS. | |||
8557 | if (!Subtarget.hasSSE41()) | |||
8558 | return SDValue(); | |||
8559 | ||||
8560 | SDValue V2 = Elt.getOperand(0); | |||
8561 | if (Elt == FirstNonZero && EltIdx == FirstNonZeroIdx) | |||
8562 | V1 = SDValue(); | |||
8563 | ||||
8564 | bool CanFold = true; | |||
8565 | for (unsigned i = EltIdx + 1; i < 4 && CanFold; ++i) { | |||
8566 | if (Zeroable[i]) | |||
8567 | continue; | |||
8568 | ||||
8569 | SDValue Current = Op->getOperand(i); | |||
8570 | SDValue SrcVector = Current->getOperand(0); | |||
8571 | if (!V1.getNode()) | |||
8572 | V1 = SrcVector; | |||
8573 | CanFold = (SrcVector == V1) && (Current.getConstantOperandAPInt(1) == i); | |||
8574 | } | |||
8575 | ||||
8576 | if (!CanFold) | |||
8577 | return SDValue(); | |||
8578 | ||||
8579 | assert(V1.getNode() && "Expected at least two non-zero elements!")(static_cast <bool> (V1.getNode() && "Expected at least two non-zero elements!" ) ? void (0) : __assert_fail ("V1.getNode() && \"Expected at least two non-zero elements!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 8579, __extension__ __PRETTY_FUNCTION__)); | |||
8580 | if (V1.getSimpleValueType() != MVT::v4f32) | |||
8581 | V1 = DAG.getBitcast(MVT::v4f32, V1); | |||
8582 | if (V2.getSimpleValueType() != MVT::v4f32) | |||
8583 | V2 = DAG.getBitcast(MVT::v4f32, V2); | |||
8584 | ||||
8585 | // Ok, we can emit an INSERTPS instruction. | |||
8586 | unsigned ZMask = Zeroable.to_ulong(); | |||
8587 | ||||
8588 | unsigned InsertPSMask = EltMaskIdx << 6 | EltIdx << 4 | ZMask; | |||
8589 | assert((InsertPSMask & ~0xFFu) == 0 && "Invalid mask!")(static_cast <bool> ((InsertPSMask & ~0xFFu) == 0 && "Invalid mask!") ? void (0) : __assert_fail ("(InsertPSMask & ~0xFFu) == 0 && \"Invalid mask!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 8589, __extension__ __PRETTY_FUNCTION__)); | |||
8590 | SDLoc DL(Op); | |||
8591 | SDValue Result = DAG.getNode(X86ISD::INSERTPS, DL, MVT::v4f32, V1, V2, | |||
8592 | DAG.getIntPtrConstant(InsertPSMask, DL, true)); | |||
8593 | return DAG.getBitcast(VT, Result); | |||
8594 | } | |||
8595 | ||||
8596 | /// Return a vector logical shift node. | |||
8597 | static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp, unsigned NumBits, | |||
8598 | SelectionDAG &DAG, const TargetLowering &TLI, | |||
8599 | const SDLoc &dl) { | |||
8600 | assert(VT.is128BitVector() && "Unknown type for VShift")(static_cast <bool> (VT.is128BitVector() && "Unknown type for VShift" ) ? void (0) : __assert_fail ("VT.is128BitVector() && \"Unknown type for VShift\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 8600, __extension__ __PRETTY_FUNCTION__)); | |||
8601 | MVT ShVT = MVT::v16i8; | |||
8602 | unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ; | |||
8603 | SrcOp = DAG.getBitcast(ShVT, SrcOp); | |||
8604 | assert(NumBits % 8 == 0 && "Only support byte sized shifts")(static_cast <bool> (NumBits % 8 == 0 && "Only support byte sized shifts" ) ? void (0) : __assert_fail ("NumBits % 8 == 0 && \"Only support byte sized shifts\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 8604, __extension__ __PRETTY_FUNCTION__)); | |||
8605 | SDValue ShiftVal = DAG.getTargetConstant(NumBits / 8, dl, MVT::i8); | |||
8606 | return DAG.getBitcast(VT, DAG.getNode(Opc, dl, ShVT, SrcOp, ShiftVal)); | |||
8607 | } | |||
8608 | ||||
8609 | static SDValue LowerAsSplatVectorLoad(SDValue SrcOp, MVT VT, const SDLoc &dl, | |||
8610 | SelectionDAG &DAG) { | |||
8611 | ||||
8612 | // Check if the scalar load can be widened into a vector load. And if | |||
8613 | // the address is "base + cst" see if the cst can be "absorbed" into | |||
8614 | // the shuffle mask. | |||
8615 | if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) { | |||
8616 | SDValue Ptr = LD->getBasePtr(); | |||
8617 | if (!ISD::isNormalLoad(LD) || !LD->isSimple()) | |||
8618 | return SDValue(); | |||
8619 | EVT PVT = LD->getValueType(0); | |||
8620 | if (PVT != MVT::i32 && PVT != MVT::f32) | |||
8621 | return SDValue(); | |||
8622 | ||||
8623 | int FI = -1; | |||
8624 | int64_t Offset = 0; | |||
8625 | if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) { | |||
8626 | FI = FINode->getIndex(); | |||
8627 | Offset = 0; | |||
8628 | } else if (DAG.isBaseWithConstantOffset(Ptr) && | |||
8629 | isa<FrameIndexSDNode>(Ptr.getOperand(0))) { | |||
8630 | FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex(); | |||
8631 | Offset = Ptr.getConstantOperandVal(1); | |||
8632 | Ptr = Ptr.getOperand(0); | |||
8633 | } else { | |||
8634 | return SDValue(); | |||
8635 | } | |||
8636 | ||||
8637 | // FIXME: 256-bit vector instructions don't require a strict alignment, | |||
8638 | // improve this code to support it better. | |||
8639 | Align RequiredAlign(VT.getSizeInBits() / 8); | |||
8640 | SDValue Chain = LD->getChain(); | |||
8641 | // Make sure the stack object alignment is at least 16 or 32. | |||
8642 | MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); | |||
8643 | MaybeAlign InferredAlign = DAG.InferPtrAlign(Ptr); | |||
8644 | if (!InferredAlign || *InferredAlign < RequiredAlign) { | |||
8645 | if (MFI.isFixedObjectIndex(FI)) { | |||
8646 | // Can't change the alignment. FIXME: It's possible to compute | |||
8647 | // the exact stack offset and reference FI + adjust offset instead. | |||
8648 | // If someone *really* cares about this. That's the way to implement it. | |||
8649 | return SDValue(); | |||
8650 | } else { | |||
8651 | MFI.setObjectAlignment(FI, RequiredAlign); | |||
8652 | } | |||
8653 | } | |||
8654 | ||||
8655 | // (Offset % 16 or 32) must be multiple of 4. Then address is then | |||
8656 | // Ptr + (Offset & ~15). | |||
8657 | if (Offset < 0) | |||
8658 | return SDValue(); | |||
8659 | if ((Offset % RequiredAlign.value()) & 3) | |||
8660 | return SDValue(); | |||
8661 | int64_t StartOffset = Offset & ~int64_t(RequiredAlign.value() - 1); | |||
8662 | if (StartOffset) { | |||
8663 | SDLoc DL(Ptr); | |||
8664 | Ptr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr, | |||
8665 | DAG.getConstant(StartOffset, DL, Ptr.getValueType())); | |||
8666 | } | |||
8667 | ||||
8668 | int EltNo = (Offset - StartOffset) >> 2; | |||
8669 | unsigned NumElems = VT.getVectorNumElements(); | |||
8670 | ||||
8671 | EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems); | |||
8672 | SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr, | |||
8673 | LD->getPointerInfo().getWithOffset(StartOffset)); | |||
8674 | ||||
8675 | SmallVector<int, 8> Mask(NumElems, EltNo); | |||
8676 | ||||
8677 | return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), Mask); | |||
8678 | } | |||
8679 | ||||
8680 | return SDValue(); | |||
8681 | } | |||
8682 | ||||
8683 | // Recurse to find a LoadSDNode source and the accumulated ByteOffest. | |||
8684 | static bool findEltLoadSrc(SDValue Elt, LoadSDNode *&Ld, int64_t &ByteOffset) { | |||
8685 | if (ISD::isNON_EXTLoad(Elt.getNode())) { | |||
8686 | auto *BaseLd = cast<LoadSDNode>(Elt); | |||
8687 | if (!BaseLd->isSimple()) | |||
8688 | return false; | |||
8689 | Ld = BaseLd; | |||
8690 | ByteOffset = 0; | |||
8691 | return true; | |||
8692 | } | |||
8693 | ||||
8694 | switch (Elt.getOpcode()) { | |||
8695 | case ISD::BITCAST: | |||
8696 | case ISD::TRUNCATE: | |||
8697 | case ISD::SCALAR_TO_VECTOR: | |||
8698 | return findEltLoadSrc(Elt.getOperand(0), Ld, ByteOffset); | |||
8699 | case ISD::SRL: | |||
8700 | if (auto *IdxC = dyn_cast<ConstantSDNode>(Elt.getOperand(1))) { | |||
8701 | uint64_t Idx = IdxC->getZExtValue(); | |||
8702 | if ((Idx % 8) == 0 && findEltLoadSrc(Elt.getOperand(0), Ld, ByteOffset)) { | |||
8703 | ByteOffset += Idx / 8; | |||
8704 | return true; | |||
8705 | } | |||
8706 | } | |||
8707 | break; | |||
8708 | case ISD::EXTRACT_VECTOR_ELT: | |||
8709 | if (auto *IdxC = dyn_cast<ConstantSDNode>(Elt.getOperand(1))) { | |||
8710 | SDValue Src = Elt.getOperand(0); | |||
8711 | unsigned SrcSizeInBits = Src.getScalarValueSizeInBits(); | |||
8712 | unsigned DstSizeInBits = Elt.getScalarValueSizeInBits(); | |||
8713 | if (DstSizeInBits == SrcSizeInBits && (SrcSizeInBits % 8) == 0 && | |||
8714 | findEltLoadSrc(Src, Ld, ByteOffset)) { | |||
8715 | uint64_t Idx = IdxC->getZExtValue(); | |||
8716 | ByteOffset += Idx * (SrcSizeInBits / 8); | |||
8717 | return true; | |||
8718 | } | |||
8719 | } | |||
8720 | break; | |||
8721 | } | |||
8722 | ||||
8723 | return false; | |||
8724 | } | |||
8725 | ||||
8726 | /// Given the initializing elements 'Elts' of a vector of type 'VT', see if the | |||
8727 | /// elements can be replaced by a single large load which has the same value as | |||
8728 | /// a build_vector or insert_subvector whose loaded operands are 'Elts'. | |||
8729 | /// | |||
8730 | /// Example: <load i32 *a, load i32 *a+4, zero, undef> -> zextload a | |||
8731 | static SDValue EltsFromConsecutiveLoads(EVT VT, ArrayRef<SDValue> Elts, | |||
8732 | const SDLoc &DL, SelectionDAG &DAG, | |||
8733 | const X86Subtarget &Subtarget, | |||
8734 | bool IsAfterLegalize) { | |||
8735 | if ((VT.getScalarSizeInBits() % 8) != 0) | |||
8736 | return SDValue(); | |||
8737 | ||||
8738 | unsigned NumElems = Elts.size(); | |||
8739 | ||||
8740 | int LastLoadedElt = -1; | |||
8741 | APInt LoadMask = APInt::getZero(NumElems); | |||
8742 | APInt ZeroMask = APInt::getZero(NumElems); | |||
8743 | APInt UndefMask = APInt::getZero(NumElems); | |||
8744 | ||||
8745 | SmallVector<LoadSDNode*, 8> Loads(NumElems, nullptr); | |||
8746 | SmallVector<int64_t, 8> ByteOffsets(NumElems, 0); | |||
8747 | ||||
8748 | // For each element in the initializer, see if we've found a load, zero or an | |||
8749 | // undef. | |||
8750 | for (unsigned i = 0; i < NumElems; ++i) { | |||
8751 | SDValue Elt = peekThroughBitcasts(Elts[i]); | |||
8752 | if (!Elt.getNode()) | |||
8753 | return SDValue(); | |||
8754 | if (Elt.isUndef()) { | |||
8755 | UndefMask.setBit(i); | |||
8756 | continue; | |||
8757 | } | |||
8758 | if (X86::isZeroNode(Elt) || ISD::isBuildVectorAllZeros(Elt.getNode())) { | |||
8759 | ZeroMask.setBit(i); | |||
8760 | continue; | |||
8761 | } | |||
8762 | ||||
8763 | // Each loaded element must be the correct fractional portion of the | |||
8764 | // requested vector load. | |||
8765 | unsigned EltSizeInBits = Elt.getValueSizeInBits(); | |||
8766 | if ((NumElems * EltSizeInBits) != VT.getSizeInBits()) | |||
8767 | return SDValue(); | |||
8768 | ||||
8769 | if (!findEltLoadSrc(Elt, Loads[i], ByteOffsets[i]) || ByteOffsets[i] < 0) | |||
8770 | return SDValue(); | |||
8771 | unsigned LoadSizeInBits = Loads[i]->getValueSizeInBits(0); | |||
8772 | if (((ByteOffsets[i] * 8) + EltSizeInBits) > LoadSizeInBits) | |||
8773 | return SDValue(); | |||
8774 | ||||
8775 | LoadMask.setBit(i); | |||
8776 | LastLoadedElt = i; | |||
8777 | } | |||
8778 | assert((ZeroMask.countPopulation() + UndefMask.countPopulation() +(static_cast <bool> ((ZeroMask.countPopulation() + UndefMask .countPopulation() + LoadMask.countPopulation()) == NumElems && "Incomplete element masks") ? void (0) : __assert_fail ("(ZeroMask.countPopulation() + UndefMask.countPopulation() + LoadMask.countPopulation()) == NumElems && \"Incomplete element masks\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 8780, __extension__ __PRETTY_FUNCTION__)) | |||
8779 | LoadMask.countPopulation()) == NumElems &&(static_cast <bool> ((ZeroMask.countPopulation() + UndefMask .countPopulation() + LoadMask.countPopulation()) == NumElems && "Incomplete element masks") ? void (0) : __assert_fail ("(ZeroMask.countPopulation() + UndefMask.countPopulation() + LoadMask.countPopulation()) == NumElems && \"Incomplete element masks\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 8780, __extension__ __PRETTY_FUNCTION__)) | |||
8780 | "Incomplete element masks")(static_cast <bool> ((ZeroMask.countPopulation() + UndefMask .countPopulation() + LoadMask.countPopulation()) == NumElems && "Incomplete element masks") ? void (0) : __assert_fail ("(ZeroMask.countPopulation() + UndefMask.countPopulation() + LoadMask.countPopulation()) == NumElems && \"Incomplete element masks\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 8780, __extension__ __PRETTY_FUNCTION__)); | |||
8781 | ||||
8782 | // Handle Special Cases - all undef or undef/zero. | |||
8783 | if (UndefMask.countPopulation() == NumElems) | |||
8784 | return DAG.getUNDEF(VT); | |||
8785 | if ((ZeroMask.countPopulation() + UndefMask.countPopulation()) == NumElems) | |||
8786 | return VT.isInteger() ? DAG.getConstant(0, DL, VT) | |||
8787 | : DAG.getConstantFP(0.0, DL, VT); | |||
8788 | ||||
8789 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
8790 | int FirstLoadedElt = LoadMask.countTrailingZeros(); | |||
8791 | SDValue EltBase = peekThroughBitcasts(Elts[FirstLoadedElt]); | |||
8792 | EVT EltBaseVT = EltBase.getValueType(); | |||
8793 | assert(EltBaseVT.getSizeInBits() == EltBaseVT.getStoreSizeInBits() &&(static_cast <bool> (EltBaseVT.getSizeInBits() == EltBaseVT .getStoreSizeInBits() && "Register/Memory size mismatch" ) ? void (0) : __assert_fail ("EltBaseVT.getSizeInBits() == EltBaseVT.getStoreSizeInBits() && \"Register/Memory size mismatch\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 8794, __extension__ __PRETTY_FUNCTION__)) | |||
8794 | "Register/Memory size mismatch")(static_cast <bool> (EltBaseVT.getSizeInBits() == EltBaseVT .getStoreSizeInBits() && "Register/Memory size mismatch" ) ? void (0) : __assert_fail ("EltBaseVT.getSizeInBits() == EltBaseVT.getStoreSizeInBits() && \"Register/Memory size mismatch\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 8794, __extension__ __PRETTY_FUNCTION__)); | |||
8795 | LoadSDNode *LDBase = Loads[FirstLoadedElt]; | |||
8796 | assert(LDBase && "Did not find base load for merging consecutive loads")(static_cast <bool> (LDBase && "Did not find base load for merging consecutive loads" ) ? void (0) : __assert_fail ("LDBase && \"Did not find base load for merging consecutive loads\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 8796, __extension__ __PRETTY_FUNCTION__)); | |||
8797 | unsigned BaseSizeInBits = EltBaseVT.getStoreSizeInBits(); | |||
8798 | unsigned BaseSizeInBytes = BaseSizeInBits / 8; | |||
8799 | int NumLoadedElts = (1 + LastLoadedElt - FirstLoadedElt); | |||
8800 | int LoadSizeInBits = NumLoadedElts * BaseSizeInBits; | |||
8801 | assert((BaseSizeInBits % 8) == 0 && "Sub-byte element loads detected")(static_cast <bool> ((BaseSizeInBits % 8) == 0 && "Sub-byte element loads detected") ? void (0) : __assert_fail ("(BaseSizeInBits % 8) == 0 && \"Sub-byte element loads detected\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 8801, __extension__ __PRETTY_FUNCTION__)); | |||
8802 | ||||
8803 | // TODO: Support offsetting the base load. | |||
8804 | if (ByteOffsets[FirstLoadedElt] != 0) | |||
8805 | return SDValue(); | |||
8806 | ||||
8807 | // Check to see if the element's load is consecutive to the base load | |||
8808 | // or offset from a previous (already checked) load. | |||
8809 | auto CheckConsecutiveLoad = [&](LoadSDNode *Base, int EltIdx) { | |||
8810 | LoadSDNode *Ld = Loads[EltIdx]; | |||
8811 | int64_t ByteOffset = ByteOffsets[EltIdx]; | |||
8812 | if (ByteOffset && (ByteOffset % BaseSizeInBytes) == 0) { | |||
8813 | int64_t BaseIdx = EltIdx - (ByteOffset / BaseSizeInBytes); | |||
8814 | return (0 <= BaseIdx && BaseIdx < (int)NumElems && LoadMask[BaseIdx] && | |||
8815 | Loads[BaseIdx] == Ld && ByteOffsets[BaseIdx] == 0); | |||
8816 | } | |||
8817 | return DAG.areNonVolatileConsecutiveLoads(Ld, Base, BaseSizeInBytes, | |||
8818 | EltIdx - FirstLoadedElt); | |||
8819 | }; | |||
8820 | ||||
8821 | // Consecutive loads can contain UNDEFS but not ZERO elements. | |||
8822 | // Consecutive loads with UNDEFs and ZEROs elements require a | |||
8823 | // an additional shuffle stage to clear the ZERO elements. | |||
8824 | bool IsConsecutiveLoad = true; | |||
8825 | bool IsConsecutiveLoadWithZeros = true; | |||
8826 | for (int i = FirstLoadedElt + 1; i <= LastLoadedElt; ++i) { | |||
8827 | if (LoadMask[i]) { | |||
8828 | if (!CheckConsecutiveLoad(LDBase, i)) { | |||
8829 | IsConsecutiveLoad = false; | |||
8830 | IsConsecutiveLoadWithZeros = false; | |||
8831 | break; | |||
8832 | } | |||
8833 | } else if (ZeroMask[i]) { | |||
8834 | IsConsecutiveLoad = false; | |||
8835 | } | |||
8836 | } | |||
8837 | ||||
8838 | auto CreateLoad = [&DAG, &DL, &Loads](EVT VT, LoadSDNode *LDBase) { | |||
8839 | auto MMOFlags = LDBase->getMemOperand()->getFlags(); | |||
8840 | assert(LDBase->isSimple() &&(static_cast <bool> (LDBase->isSimple() && "Cannot merge volatile or atomic loads." ) ? void (0) : __assert_fail ("LDBase->isSimple() && \"Cannot merge volatile or atomic loads.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 8841, __extension__ __PRETTY_FUNCTION__)) | |||
8841 | "Cannot merge volatile or atomic loads.")(static_cast <bool> (LDBase->isSimple() && "Cannot merge volatile or atomic loads." ) ? void (0) : __assert_fail ("LDBase->isSimple() && \"Cannot merge volatile or atomic loads.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 8841, __extension__ __PRETTY_FUNCTION__)); | |||
8842 | SDValue NewLd = | |||
8843 | DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(), | |||
8844 | LDBase->getPointerInfo(), LDBase->getOriginalAlign(), | |||
8845 | MMOFlags); | |||
8846 | for (auto *LD : Loads) | |||
8847 | if (LD) | |||
8848 | DAG.makeEquivalentMemoryOrdering(LD, NewLd); | |||
8849 | return NewLd; | |||
8850 | }; | |||
8851 | ||||
8852 | // Check if the base load is entirely dereferenceable. | |||
8853 | bool IsDereferenceable = LDBase->getPointerInfo().isDereferenceable( | |||
8854 | VT.getSizeInBits() / 8, *DAG.getContext(), DAG.getDataLayout()); | |||
8855 | ||||
8856 | // LOAD - all consecutive load/undefs (must start/end with a load or be | |||
8857 | // entirely dereferenceable). If we have found an entire vector of loads and | |||
8858 | // undefs, then return a large load of the entire vector width starting at the | |||
8859 | // base pointer. If the vector contains zeros, then attempt to shuffle those | |||
8860 | // elements. | |||
8861 | if (FirstLoadedElt == 0 && | |||
8862 | (NumLoadedElts == (int)NumElems || IsDereferenceable) && | |||
8863 | (IsConsecutiveLoad || IsConsecutiveLoadWithZeros)) { | |||
8864 | if (IsAfterLegalize && !TLI.isOperationLegal(ISD::LOAD, VT)) | |||
8865 | return SDValue(); | |||
8866 | ||||
8867 | // Don't create 256-bit non-temporal aligned loads without AVX2 as these | |||
8868 | // will lower to regular temporal loads and use the cache. | |||
8869 | if (LDBase->isNonTemporal() && LDBase->getAlignment() >= 32 && | |||
8870 | VT.is256BitVector() && !Subtarget.hasInt256()) | |||
8871 | return SDValue(); | |||
8872 | ||||
8873 | if (NumElems == 1) | |||
8874 | return DAG.getBitcast(VT, Elts[FirstLoadedElt]); | |||
8875 | ||||
8876 | if (!ZeroMask) | |||
8877 | return CreateLoad(VT, LDBase); | |||
8878 | ||||
8879 | // IsConsecutiveLoadWithZeros - we need to create a shuffle of the loaded | |||
8880 | // vector and a zero vector to clear out the zero elements. | |||
8881 | if (!IsAfterLegalize && VT.isVector()) { | |||
8882 | unsigned NumMaskElts = VT.getVectorNumElements(); | |||
8883 | if ((NumMaskElts % NumElems) == 0) { | |||
8884 | unsigned Scale = NumMaskElts / NumElems; | |||
8885 | SmallVector<int, 4> ClearMask(NumMaskElts, -1); | |||
8886 | for (unsigned i = 0; i < NumElems; ++i) { | |||
8887 | if (UndefMask[i]) | |||
8888 | continue; | |||
8889 | int Offset = ZeroMask[i] ? NumMaskElts : 0; | |||
8890 | for (unsigned j = 0; j != Scale; ++j) | |||
8891 | ClearMask[(i * Scale) + j] = (i * Scale) + j + Offset; | |||
8892 | } | |||
8893 | SDValue V = CreateLoad(VT, LDBase); | |||
8894 | SDValue Z = VT.isInteger() ? DAG.getConstant(0, DL, VT) | |||
8895 | : DAG.getConstantFP(0.0, DL, VT); | |||
8896 | return DAG.getVectorShuffle(VT, DL, V, Z, ClearMask); | |||
8897 | } | |||
8898 | } | |||
8899 | } | |||
8900 | ||||
8901 | // If the upper half of a ymm/zmm load is undef then just load the lower half. | |||
8902 | if (VT.is256BitVector() || VT.is512BitVector()) { | |||
8903 | unsigned HalfNumElems = NumElems / 2; | |||
8904 | if (UndefMask.extractBits(HalfNumElems, HalfNumElems).isAllOnes()) { | |||
8905 | EVT HalfVT = | |||
8906 | EVT::getVectorVT(*DAG.getContext(), VT.getScalarType(), HalfNumElems); | |||
8907 | SDValue HalfLD = | |||
8908 | EltsFromConsecutiveLoads(HalfVT, Elts.drop_back(HalfNumElems), DL, | |||
8909 | DAG, Subtarget, IsAfterLegalize); | |||
8910 | if (HalfLD) | |||
8911 | return DAG.getNode(ISD::INSERT_SUBVECTOR, DL, VT, DAG.getUNDEF(VT), | |||
8912 | HalfLD, DAG.getIntPtrConstant(0, DL)); | |||
8913 | } | |||
8914 | } | |||
8915 | ||||
8916 | // VZEXT_LOAD - consecutive 32/64-bit load/undefs followed by zeros/undefs. | |||
8917 | if (IsConsecutiveLoad && FirstLoadedElt == 0 && | |||
8918 | ((LoadSizeInBits == 16 && Subtarget.hasFP16()) || LoadSizeInBits == 32 || | |||
8919 | LoadSizeInBits == 64) && | |||
8920 | ((VT.is128BitVector() || VT.is256BitVector() || VT.is512BitVector()))) { | |||
8921 | MVT VecSVT = VT.isFloatingPoint() ? MVT::getFloatingPointVT(LoadSizeInBits) | |||
8922 | : MVT::getIntegerVT(LoadSizeInBits); | |||
8923 | MVT VecVT = MVT::getVectorVT(VecSVT, VT.getSizeInBits() / LoadSizeInBits); | |||
8924 | // Allow v4f32 on SSE1 only targets. | |||
8925 | // FIXME: Add more isel patterns so we can just use VT directly. | |||
8926 | if (!Subtarget.hasSSE2() && VT == MVT::v4f32) | |||
8927 | VecVT = MVT::v4f32; | |||
8928 | if (TLI.isTypeLegal(VecVT)) { | |||
8929 | SDVTList Tys = DAG.getVTList(VecVT, MVT::Other); | |||
8930 | SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() }; | |||
8931 | SDValue ResNode = DAG.getMemIntrinsicNode( | |||
8932 | X86ISD::VZEXT_LOAD, DL, Tys, Ops, VecSVT, LDBase->getPointerInfo(), | |||
8933 | LDBase->getOriginalAlign(), MachineMemOperand::MOLoad); | |||
8934 | for (auto *LD : Loads) | |||
8935 | if (LD) | |||
8936 | DAG.makeEquivalentMemoryOrdering(LD, ResNode); | |||
8937 | return DAG.getBitcast(VT, ResNode); | |||
8938 | } | |||
8939 | } | |||
8940 | ||||
8941 | // BROADCAST - match the smallest possible repetition pattern, load that | |||
8942 | // scalar/subvector element and then broadcast to the entire vector. | |||
8943 | if (ZeroMask.isZero() && isPowerOf2_32(NumElems) && Subtarget.hasAVX() && | |||
8944 | (VT.is128BitVector() || VT.is256BitVector() || VT.is512BitVector())) { | |||
8945 | for (unsigned SubElems = 1; SubElems < NumElems; SubElems *= 2) { | |||
8946 | unsigned RepeatSize = SubElems * BaseSizeInBits; | |||
8947 | unsigned ScalarSize = std::min(RepeatSize, 64u); | |||
8948 | if (!Subtarget.hasAVX2() && ScalarSize < 32) | |||
8949 | continue; | |||
8950 | ||||
8951 | // Don't attempt a 1:N subvector broadcast - it should be caught by | |||
8952 | // combineConcatVectorOps, else will cause infinite loops. | |||
8953 | if (RepeatSize > ScalarSize && SubElems == 1) | |||
8954 | continue; | |||
8955 | ||||
8956 | bool Match = true; | |||
8957 | SmallVector<SDValue, 8> RepeatedLoads(SubElems, DAG.getUNDEF(EltBaseVT)); | |||
8958 | for (unsigned i = 0; i != NumElems && Match; ++i) { | |||
8959 | if (!LoadMask[i]) | |||
8960 | continue; | |||
8961 | SDValue Elt = peekThroughBitcasts(Elts[i]); | |||
8962 | if (RepeatedLoads[i % SubElems].isUndef()) | |||
8963 | RepeatedLoads[i % SubElems] = Elt; | |||
8964 | else | |||
8965 | Match &= (RepeatedLoads[i % SubElems] == Elt); | |||
8966 | } | |||
8967 | ||||
8968 | // We must have loads at both ends of the repetition. | |||
8969 | Match &= !RepeatedLoads.front().isUndef(); | |||
8970 | Match &= !RepeatedLoads.back().isUndef(); | |||
8971 | if (!Match) | |||
8972 | continue; | |||
8973 | ||||
8974 | EVT RepeatVT = | |||
8975 | VT.isInteger() && (RepeatSize != 64 || TLI.isTypeLegal(MVT::i64)) | |||
8976 | ? EVT::getIntegerVT(*DAG.getContext(), ScalarSize) | |||
8977 | : EVT::getFloatingPointVT(ScalarSize); | |||
8978 | if (RepeatSize > ScalarSize) | |||
8979 | RepeatVT = EVT::getVectorVT(*DAG.getContext(), RepeatVT, | |||
8980 | RepeatSize / ScalarSize); | |||
8981 | EVT BroadcastVT = | |||
8982 | EVT::getVectorVT(*DAG.getContext(), RepeatVT.getScalarType(), | |||
8983 | VT.getSizeInBits() / ScalarSize); | |||
8984 | if (TLI.isTypeLegal(BroadcastVT)) { | |||
8985 | if (SDValue RepeatLoad = EltsFromConsecutiveLoads( | |||
8986 | RepeatVT, RepeatedLoads, DL, DAG, Subtarget, IsAfterLegalize)) { | |||
8987 | SDValue Broadcast = RepeatLoad; | |||
8988 | if (RepeatSize > ScalarSize) { | |||
8989 | while (Broadcast.getValueSizeInBits() < VT.getSizeInBits()) | |||
8990 | Broadcast = concatSubVectors(Broadcast, Broadcast, DAG, DL); | |||
8991 | } else { | |||
8992 | if (!Subtarget.hasAVX2() && | |||
8993 | !MayFoldLoadIntoBroadcastFromMem( | |||
8994 | RepeatLoad, RepeatVT.getScalarType().getSimpleVT(), | |||
8995 | /*AssumeSingleUse=*/true)) | |||
8996 | return SDValue(); | |||
8997 | Broadcast = | |||
8998 | DAG.getNode(X86ISD::VBROADCAST, DL, BroadcastVT, RepeatLoad); | |||
8999 | } | |||
9000 | return DAG.getBitcast(VT, Broadcast); | |||
9001 | } | |||
9002 | } | |||
9003 | } | |||
9004 | } | |||
9005 | ||||
9006 | return SDValue(); | |||
9007 | } | |||
9008 | ||||
9009 | // Combine a vector ops (shuffles etc.) that is equal to build_vector load1, | |||
9010 | // load2, load3, load4, <0, 1, 2, 3> into a vector load if the load addresses | |||
9011 | // are consecutive, non-overlapping, and in the right order. | |||
9012 | static SDValue combineToConsecutiveLoads(EVT VT, SDValue Op, const SDLoc &DL, | |||
9013 | SelectionDAG &DAG, | |||
9014 | const X86Subtarget &Subtarget, | |||
9015 | bool IsAfterLegalize) { | |||
9016 | SmallVector<SDValue, 64> Elts; | |||
9017 | for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) { | |||
9018 | if (SDValue Elt = getShuffleScalarElt(Op, i, DAG, 0)) { | |||
9019 | Elts.push_back(Elt); | |||
9020 | continue; | |||
9021 | } | |||
9022 | return SDValue(); | |||
9023 | } | |||
9024 | assert(Elts.size() == VT.getVectorNumElements())(static_cast <bool> (Elts.size() == VT.getVectorNumElements ()) ? void (0) : __assert_fail ("Elts.size() == VT.getVectorNumElements()" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 9024, __extension__ __PRETTY_FUNCTION__)); | |||
9025 | return EltsFromConsecutiveLoads(VT, Elts, DL, DAG, Subtarget, | |||
9026 | IsAfterLegalize); | |||
9027 | } | |||
9028 | ||||
9029 | static Constant *getConstantVector(MVT VT, const APInt &SplatValue, | |||
9030 | unsigned SplatBitSize, LLVMContext &C) { | |||
9031 | unsigned ScalarSize = VT.getScalarSizeInBits(); | |||
9032 | unsigned NumElm = SplatBitSize / ScalarSize; | |||
9033 | ||||
9034 | SmallVector<Constant *, 32> ConstantVec; | |||
9035 | for (unsigned i = 0; i < NumElm; i++) { | |||
9036 | APInt Val = SplatValue.extractBits(ScalarSize, ScalarSize * i); | |||
9037 | Constant *Const; | |||
9038 | if (VT.isFloatingPoint()) { | |||
9039 | if (ScalarSize == 16) { | |||
9040 | Const = ConstantFP::get(C, APFloat(APFloat::IEEEhalf(), Val)); | |||
9041 | } else if (ScalarSize == 32) { | |||
9042 | Const = ConstantFP::get(C, APFloat(APFloat::IEEEsingle(), Val)); | |||
9043 | } else { | |||
9044 | assert(ScalarSize == 64 && "Unsupported floating point scalar size")(static_cast <bool> (ScalarSize == 64 && "Unsupported floating point scalar size" ) ? void (0) : __assert_fail ("ScalarSize == 64 && \"Unsupported floating point scalar size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 9044, __extension__ __PRETTY_FUNCTION__)); | |||
9045 | Const = ConstantFP::get(C, APFloat(APFloat::IEEEdouble(), Val)); | |||
9046 | } | |||
9047 | } else | |||
9048 | Const = Constant::getIntegerValue(Type::getIntNTy(C, ScalarSize), Val); | |||
9049 | ConstantVec.push_back(Const); | |||
9050 | } | |||
9051 | return ConstantVector::get(ArrayRef<Constant *>(ConstantVec)); | |||
9052 | } | |||
9053 | ||||
9054 | static bool isFoldableUseOfShuffle(SDNode *N) { | |||
9055 | for (auto *U : N->uses()) { | |||
9056 | unsigned Opc = U->getOpcode(); | |||
9057 | // VPERMV/VPERMV3 shuffles can never fold their index operands. | |||
9058 | if (Opc == X86ISD::VPERMV && U->getOperand(0).getNode() == N) | |||
9059 | return false; | |||
9060 | if (Opc == X86ISD::VPERMV3 && U->getOperand(1).getNode() == N) | |||
9061 | return false; | |||
9062 | if (isTargetShuffle(Opc)) | |||
9063 | return true; | |||
9064 | if (Opc == ISD::BITCAST) // Ignore bitcasts | |||
9065 | return isFoldableUseOfShuffle(U); | |||
9066 | if (N->hasOneUse()) | |||
9067 | return true; | |||
9068 | } | |||
9069 | return false; | |||
9070 | } | |||
9071 | ||||
9072 | /// Attempt to use the vbroadcast instruction to generate a splat value | |||
9073 | /// from a splat BUILD_VECTOR which uses: | |||
9074 | /// a. A single scalar load, or a constant. | |||
9075 | /// b. Repeated pattern of constants (e.g. <0,1,0,1> or <0,1,2,3,0,1,2,3>). | |||
9076 | /// | |||
9077 | /// The VBROADCAST node is returned when a pattern is found, | |||
9078 | /// or SDValue() otherwise. | |||
9079 | static SDValue lowerBuildVectorAsBroadcast(BuildVectorSDNode *BVOp, | |||
9080 | const X86Subtarget &Subtarget, | |||
9081 | SelectionDAG &DAG) { | |||
9082 | // VBROADCAST requires AVX. | |||
9083 | // TODO: Splats could be generated for non-AVX CPUs using SSE | |||
9084 | // instructions, but there's less potential gain for only 128-bit vectors. | |||
9085 | if (!Subtarget.hasAVX()) | |||
9086 | return SDValue(); | |||
9087 | ||||
9088 | MVT VT = BVOp->getSimpleValueType(0); | |||
9089 | unsigned NumElts = VT.getVectorNumElements(); | |||
9090 | SDLoc dl(BVOp); | |||
9091 | ||||
9092 | assert((VT.is128BitVector() || VT.is256BitVector() || VT.is512BitVector()) &&(static_cast <bool> ((VT.is128BitVector() || VT.is256BitVector () || VT.is512BitVector()) && "Unsupported vector type for broadcast." ) ? void (0) : __assert_fail ("(VT.is128BitVector() || VT.is256BitVector() || VT.is512BitVector()) && \"Unsupported vector type for broadcast.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 9093, __extension__ __PRETTY_FUNCTION__)) | |||
9093 | "Unsupported vector type for broadcast.")(static_cast <bool> ((VT.is128BitVector() || VT.is256BitVector () || VT.is512BitVector()) && "Unsupported vector type for broadcast." ) ? void (0) : __assert_fail ("(VT.is128BitVector() || VT.is256BitVector() || VT.is512BitVector()) && \"Unsupported vector type for broadcast.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 9093, __extension__ __PRETTY_FUNCTION__)); | |||
9094 | ||||
9095 | // See if the build vector is a repeating sequence of scalars (inc. splat). | |||
9096 | SDValue Ld; | |||
9097 | BitVector UndefElements; | |||
9098 | SmallVector<SDValue, 16> Sequence; | |||
9099 | if (BVOp->getRepeatedSequence(Sequence, &UndefElements)) { | |||
9100 | assert((NumElts % Sequence.size()) == 0 && "Sequence doesn't fit.")(static_cast <bool> ((NumElts % Sequence.size()) == 0 && "Sequence doesn't fit.") ? void (0) : __assert_fail ("(NumElts % Sequence.size()) == 0 && \"Sequence doesn't fit.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 9100, __extension__ __PRETTY_FUNCTION__)); | |||
9101 | if (Sequence.size() == 1) | |||
9102 | Ld = Sequence[0]; | |||
9103 | } | |||
9104 | ||||
9105 | // Attempt to use VBROADCASTM | |||
9106 | // From this pattern: | |||
9107 | // a. t0 = (zext_i64 (bitcast_i8 v2i1 X)) | |||
9108 | // b. t1 = (build_vector t0 t0) | |||
9109 | // | |||
9110 | // Create (VBROADCASTM v2i1 X) | |||
9111 | if (!Sequence.empty() && Subtarget.hasCDI()) { | |||
9112 | // If not a splat, are the upper sequence values zeroable? | |||
9113 | unsigned SeqLen = Sequence.size(); | |||
9114 | bool UpperZeroOrUndef = | |||
9115 | SeqLen == 1 || | |||
9116 | llvm::all_of(makeArrayRef(Sequence).drop_front(), [](SDValue V) { | |||
9117 | return !V || V.isUndef() || isNullConstant(V); | |||
9118 | }); | |||
9119 | SDValue Op0 = Sequence[0]; | |||
9120 | if (UpperZeroOrUndef && ((Op0.getOpcode() == ISD::BITCAST) || | |||
9121 | (Op0.getOpcode() == ISD::ZERO_EXTEND && | |||
9122 | Op0.getOperand(0).getOpcode() == ISD::BITCAST))) { | |||
9123 | SDValue BOperand = Op0.getOpcode() == ISD::BITCAST | |||
9124 | ? Op0.getOperand(0) | |||
9125 | : Op0.getOperand(0).getOperand(0); | |||
9126 | MVT MaskVT = BOperand.getSimpleValueType(); | |||
9127 | MVT EltType = MVT::getIntegerVT(VT.getScalarSizeInBits() * SeqLen); | |||
9128 | if ((EltType == MVT::i64 && MaskVT == MVT::v8i1) || // for broadcastmb2q | |||
9129 | (EltType == MVT::i32 && MaskVT == MVT::v16i1)) { // for broadcastmw2d | |||
9130 | MVT BcstVT = MVT::getVectorVT(EltType, NumElts / SeqLen); | |||
9131 | if (!VT.is512BitVector() && !Subtarget.hasVLX()) { | |||
9132 | unsigned Scale = 512 / VT.getSizeInBits(); | |||
9133 | BcstVT = MVT::getVectorVT(EltType, Scale * (NumElts / SeqLen)); | |||
9134 | } | |||
9135 | SDValue Bcst = DAG.getNode(X86ISD::VBROADCASTM, dl, BcstVT, BOperand); | |||
9136 | if (BcstVT.getSizeInBits() != VT.getSizeInBits()) | |||
9137 | Bcst = extractSubVector(Bcst, 0, DAG, dl, VT.getSizeInBits()); | |||
9138 | return DAG.getBitcast(VT, Bcst); | |||
9139 | } | |||
9140 | } | |||
9141 | } | |||
9142 | ||||
9143 | unsigned NumUndefElts = UndefElements.count(); | |||
9144 | if (!Ld || (NumElts - NumUndefElts) <= 1) { | |||
9145 | APInt SplatValue, Undef; | |||
9146 | unsigned SplatBitSize; | |||
9147 | bool HasUndef; | |||
9148 | // Check if this is a repeated constant pattern suitable for broadcasting. | |||
9149 | if (BVOp->isConstantSplat(SplatValue, Undef, SplatBitSize, HasUndef) && | |||
9150 | SplatBitSize > VT.getScalarSizeInBits() && | |||
9151 | SplatBitSize < VT.getSizeInBits()) { | |||
9152 | // Avoid replacing with broadcast when it's a use of a shuffle | |||
9153 | // instruction to preserve the present custom lowering of shuffles. | |||
9154 | if (isFoldableUseOfShuffle(BVOp)) | |||
9155 | return SDValue(); | |||
9156 | // replace BUILD_VECTOR with broadcast of the repeated constants. | |||
9157 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
9158 | LLVMContext *Ctx = DAG.getContext(); | |||
9159 | MVT PVT = TLI.getPointerTy(DAG.getDataLayout()); | |||
9160 | if (Subtarget.hasAVX()) { | |||
9161 | if (SplatBitSize == 32 || SplatBitSize == 64 || | |||
9162 | (SplatBitSize < 32 && Subtarget.hasAVX2())) { | |||
9163 | // Splatted value can fit in one INTEGER constant in constant pool. | |||
9164 | // Load the constant and broadcast it. | |||
9165 | MVT CVT = MVT::getIntegerVT(SplatBitSize); | |||
9166 | Type *ScalarTy = Type::getIntNTy(*Ctx, SplatBitSize); | |||
9167 | Constant *C = Constant::getIntegerValue(ScalarTy, SplatValue); | |||
9168 | SDValue CP = DAG.getConstantPool(C, PVT); | |||
9169 | unsigned Repeat = VT.getSizeInBits() / SplatBitSize; | |||
9170 | ||||
9171 | Align Alignment = cast<ConstantPoolSDNode>(CP)->getAlign(); | |||
9172 | SDVTList Tys = | |||
9173 | DAG.getVTList(MVT::getVectorVT(CVT, Repeat), MVT::Other); | |||
9174 | SDValue Ops[] = {DAG.getEntryNode(), CP}; | |||
9175 | MachinePointerInfo MPI = | |||
9176 | MachinePointerInfo::getConstantPool(DAG.getMachineFunction()); | |||
9177 | SDValue Brdcst = DAG.getMemIntrinsicNode( | |||
9178 | X86ISD::VBROADCAST_LOAD, dl, Tys, Ops, CVT, MPI, Alignment, | |||
9179 | MachineMemOperand::MOLoad); | |||
9180 | return DAG.getBitcast(VT, Brdcst); | |||
9181 | } | |||
9182 | if (SplatBitSize > 64) { | |||
9183 | // Load the vector of constants and broadcast it. | |||
9184 | Constant *VecC = getConstantVector(VT, SplatValue, SplatBitSize, | |||
9185 | *Ctx); | |||
9186 | SDValue VCP = DAG.getConstantPool(VecC, PVT); | |||
9187 | unsigned NumElm = SplatBitSize / VT.getScalarSizeInBits(); | |||
9188 | MVT VVT = MVT::getVectorVT(VT.getScalarType(), NumElm); | |||
9189 | Align Alignment = cast<ConstantPoolSDNode>(VCP)->getAlign(); | |||
9190 | SDVTList Tys = DAG.getVTList(VT, MVT::Other); | |||
9191 | SDValue Ops[] = {DAG.getEntryNode(), VCP}; | |||
9192 | MachinePointerInfo MPI = | |||
9193 | MachinePointerInfo::getConstantPool(DAG.getMachineFunction()); | |||
9194 | return DAG.getMemIntrinsicNode( | |||
9195 | X86ISD::SUBV_BROADCAST_LOAD, dl, Tys, Ops, VVT, MPI, Alignment, | |||
9196 | MachineMemOperand::MOLoad); | |||
9197 | } | |||
9198 | } | |||
9199 | } | |||
9200 | ||||
9201 | // If we are moving a scalar into a vector (Ld must be set and all elements | |||
9202 | // but 1 are undef) and that operation is not obviously supported by | |||
9203 | // vmovd/vmovq/vmovss/vmovsd, then keep trying to form a broadcast. | |||
9204 | // That's better than general shuffling and may eliminate a load to GPR and | |||
9205 | // move from scalar to vector register. | |||
9206 | if (!Ld || NumElts - NumUndefElts != 1) | |||
9207 | return SDValue(); | |||
9208 | unsigned ScalarSize = Ld.getValueSizeInBits(); | |||
9209 | if (!(UndefElements[0] || (ScalarSize != 32 && ScalarSize != 64))) | |||
9210 | return SDValue(); | |||
9211 | } | |||
9212 | ||||
9213 | bool ConstSplatVal = | |||
9214 | (Ld.getOpcode() == ISD::Constant || Ld.getOpcode() == ISD::ConstantFP); | |||
9215 | bool IsLoad = ISD::isNormalLoad(Ld.getNode()); | |||
9216 | ||||
9217 | // TODO: Handle broadcasts of non-constant sequences. | |||
9218 | ||||
9219 | // Make sure that all of the users of a non-constant load are from the | |||
9220 | // BUILD_VECTOR node. | |||
9221 | // FIXME: Is the use count needed for non-constant, non-load case? | |||
9222 | if (!ConstSplatVal && !IsLoad && !BVOp->isOnlyUserOf(Ld.getNode())) | |||
9223 | return SDValue(); | |||
9224 | ||||
9225 | unsigned ScalarSize = Ld.getValueSizeInBits(); | |||
9226 | bool IsGE256 = (VT.getSizeInBits() >= 256); | |||
9227 | ||||
9228 | // When optimizing for size, generate up to 5 extra bytes for a broadcast | |||
9229 | // instruction to save 8 or more bytes of constant pool data. | |||
9230 | // TODO: If multiple splats are generated to load the same constant, | |||
9231 | // it may be detrimental to overall size. There needs to be a way to detect | |||
9232 | // that condition to know if this is truly a size win. | |||
9233 | bool OptForSize = DAG.shouldOptForSize(); | |||
9234 | ||||
9235 | // Handle broadcasting a single constant scalar from the constant pool | |||
9236 | // into a vector. | |||
9237 | // On Sandybridge (no AVX2), it is still better to load a constant vector | |||
9238 | // from the constant pool and not to broadcast it from a scalar. | |||
9239 | // But override that restriction when optimizing for size. | |||
9240 | // TODO: Check if splatting is recommended for other AVX-capable CPUs. | |||
9241 | if (ConstSplatVal && (Subtarget.hasAVX2() || OptForSize)) { | |||
9242 | EVT CVT = Ld.getValueType(); | |||
9243 | assert(!CVT.isVector() && "Must not broadcast a vector type")(static_cast <bool> (!CVT.isVector() && "Must not broadcast a vector type" ) ? void (0) : __assert_fail ("!CVT.isVector() && \"Must not broadcast a vector type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 9243, __extension__ __PRETTY_FUNCTION__)); | |||
9244 | ||||
9245 | // Splat f32, i32, v4f64, v4i64 in all cases with AVX2. | |||
9246 | // For size optimization, also splat v2f64 and v2i64, and for size opt | |||
9247 | // with AVX2, also splat i8 and i16. | |||
9248 | // With pattern matching, the VBROADCAST node may become a VMOVDDUP. | |||
9249 | if (ScalarSize == 32 || (IsGE256 && ScalarSize == 64) || | |||
9250 | (ScalarSize == 16 && Subtarget.hasFP16() && CVT.isFloatingPoint()) || | |||
9251 | (OptForSize && (ScalarSize == 64 || Subtarget.hasAVX2()))) { | |||
9252 | const Constant *C = nullptr; | |||
9253 | if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld)) | |||
9254 | C = CI->getConstantIntValue(); | |||
9255 | else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld)) | |||
9256 | C = CF->getConstantFPValue(); | |||
9257 | ||||
9258 | assert(C && "Invalid constant type")(static_cast <bool> (C && "Invalid constant type" ) ? void (0) : __assert_fail ("C && \"Invalid constant type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 9258, __extension__ __PRETTY_FUNCTION__)); | |||
9259 | ||||
9260 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
9261 | SDValue CP = | |||
9262 | DAG.getConstantPool(C, TLI.getPointerTy(DAG.getDataLayout())); | |||
9263 | Align Alignment = cast<ConstantPoolSDNode>(CP)->getAlign(); | |||
9264 | ||||
9265 | SDVTList Tys = DAG.getVTList(VT, MVT::Other); | |||
9266 | SDValue Ops[] = {DAG.getEntryNode(), CP}; | |||
9267 | MachinePointerInfo MPI = | |||
9268 | MachinePointerInfo::getConstantPool(DAG.getMachineFunction()); | |||
9269 | return DAG.getMemIntrinsicNode(X86ISD::VBROADCAST_LOAD, dl, Tys, Ops, CVT, | |||
9270 | MPI, Alignment, MachineMemOperand::MOLoad); | |||
9271 | } | |||
9272 | } | |||
9273 | ||||
9274 | // Handle AVX2 in-register broadcasts. | |||
9275 | if (!IsLoad && Subtarget.hasInt256() && | |||
9276 | (ScalarSize == 32 || (IsGE256 && ScalarSize == 64))) | |||
9277 | return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld); | |||
9278 | ||||
9279 | // The scalar source must be a normal load. | |||
9280 | if (!IsLoad) | |||
9281 | return SDValue(); | |||
9282 | ||||
9283 | // Make sure the non-chain result is only used by this build vector. | |||
9284 | if (!Ld->hasNUsesOfValue(NumElts - NumUndefElts, 0)) | |||
9285 | return SDValue(); | |||
9286 | ||||
9287 | if (ScalarSize == 32 || (IsGE256 && ScalarSize == 64) || | |||
9288 | (Subtarget.hasVLX() && ScalarSize == 64)) { | |||
9289 | auto *LN = cast<LoadSDNode>(Ld); | |||
9290 | SDVTList Tys = DAG.getVTList(VT, MVT::Other); | |||
9291 | SDValue Ops[] = {LN->getChain(), LN->getBasePtr()}; | |||
9292 | SDValue BCast = | |||
9293 | DAG.getMemIntrinsicNode(X86ISD::VBROADCAST_LOAD, dl, Tys, Ops, | |||
9294 | LN->getMemoryVT(), LN->getMemOperand()); | |||
9295 | DAG.ReplaceAllUsesOfValueWith(SDValue(LN, 1), BCast.getValue(1)); | |||
9296 | return BCast; | |||
9297 | } | |||
9298 | ||||
9299 | // The integer check is needed for the 64-bit into 128-bit so it doesn't match | |||
9300 | // double since there is no vbroadcastsd xmm | |||
9301 | if (Subtarget.hasInt256() && Ld.getValueType().isInteger() && | |||
9302 | (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)) { | |||
9303 | auto *LN = cast<LoadSDNode>(Ld); | |||
9304 | SDVTList Tys = DAG.getVTList(VT, MVT::Other); | |||
9305 | SDValue Ops[] = {LN->getChain(), LN->getBasePtr()}; | |||
9306 | SDValue BCast = | |||
9307 | DAG.getMemIntrinsicNode(X86ISD::VBROADCAST_LOAD, dl, Tys, Ops, | |||
9308 | LN->getMemoryVT(), LN->getMemOperand()); | |||
9309 | DAG.ReplaceAllUsesOfValueWith(SDValue(LN, 1), BCast.getValue(1)); | |||
9310 | return BCast; | |||
9311 | } | |||
9312 | ||||
9313 | if (ScalarSize == 16 && Subtarget.hasFP16() && IsGE256) | |||
9314 | return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld); | |||
9315 | ||||
9316 | // Unsupported broadcast. | |||
9317 | return SDValue(); | |||
9318 | } | |||
9319 | ||||
9320 | /// For an EXTRACT_VECTOR_ELT with a constant index return the real | |||
9321 | /// underlying vector and index. | |||
9322 | /// | |||
9323 | /// Modifies \p ExtractedFromVec to the real vector and returns the real | |||
9324 | /// index. | |||
9325 | static int getUnderlyingExtractedFromVec(SDValue &ExtractedFromVec, | |||
9326 | SDValue ExtIdx) { | |||
9327 | int Idx = cast<ConstantSDNode>(ExtIdx)->getZExtValue(); | |||
9328 | if (!isa<ShuffleVectorSDNode>(ExtractedFromVec)) | |||
9329 | return Idx; | |||
9330 | ||||
9331 | // For 256-bit vectors, LowerEXTRACT_VECTOR_ELT_SSE4 may have already | |||
9332 | // lowered this: | |||
9333 | // (extract_vector_elt (v8f32 %1), Constant<6>) | |||
9334 | // to: | |||
9335 | // (extract_vector_elt (vector_shuffle<2,u,u,u> | |||
9336 | // (extract_subvector (v8f32 %0), Constant<4>), | |||
9337 | // undef) | |||
9338 | // Constant<0>) | |||
9339 | // In this case the vector is the extract_subvector expression and the index | |||
9340 | // is 2, as specified by the shuffle. | |||
9341 | ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(ExtractedFromVec); | |||
9342 | SDValue ShuffleVec = SVOp->getOperand(0); | |||
9343 | MVT ShuffleVecVT = ShuffleVec.getSimpleValueType(); | |||
9344 | assert(ShuffleVecVT.getVectorElementType() ==(static_cast <bool> (ShuffleVecVT.getVectorElementType( ) == ExtractedFromVec.getSimpleValueType().getVectorElementType ()) ? void (0) : __assert_fail ("ShuffleVecVT.getVectorElementType() == ExtractedFromVec.getSimpleValueType().getVectorElementType()" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 9345, __extension__ __PRETTY_FUNCTION__)) | |||
9345 | ExtractedFromVec.getSimpleValueType().getVectorElementType())(static_cast <bool> (ShuffleVecVT.getVectorElementType( ) == ExtractedFromVec.getSimpleValueType().getVectorElementType ()) ? void (0) : __assert_fail ("ShuffleVecVT.getVectorElementType() == ExtractedFromVec.getSimpleValueType().getVectorElementType()" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 9345, __extension__ __PRETTY_FUNCTION__)); | |||
9346 | ||||
9347 | int ShuffleIdx = SVOp->getMaskElt(Idx); | |||
9348 | if (isUndefOrInRange(ShuffleIdx, 0, ShuffleVecVT.getVectorNumElements())) { | |||
9349 | ExtractedFromVec = ShuffleVec; | |||
9350 | return ShuffleIdx; | |||
9351 | } | |||
9352 | return Idx; | |||
9353 | } | |||
9354 | ||||
9355 | static SDValue buildFromShuffleMostly(SDValue Op, SelectionDAG &DAG) { | |||
9356 | MVT VT = Op.getSimpleValueType(); | |||
9357 | ||||
9358 | // Skip if insert_vec_elt is not supported. | |||
9359 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
9360 | if (!TLI.isOperationLegalOrCustom(ISD::INSERT_VECTOR_ELT, VT)) | |||
9361 | return SDValue(); | |||
9362 | ||||
9363 | SDLoc DL(Op); | |||
9364 | unsigned NumElems = Op.getNumOperands(); | |||
9365 | ||||
9366 | SDValue VecIn1; | |||
9367 | SDValue VecIn2; | |||
9368 | SmallVector<unsigned, 4> InsertIndices; | |||
9369 | SmallVector<int, 8> Mask(NumElems, -1); | |||
9370 | ||||
9371 | for (unsigned i = 0; i != NumElems; ++i) { | |||
9372 | unsigned Opc = Op.getOperand(i).getOpcode(); | |||
9373 | ||||
9374 | if (Opc == ISD::UNDEF) | |||
9375 | continue; | |||
9376 | ||||
9377 | if (Opc != ISD::EXTRACT_VECTOR_ELT) { | |||
9378 | // Quit if more than 1 elements need inserting. | |||
9379 | if (InsertIndices.size() > 1) | |||
9380 | return SDValue(); | |||
9381 | ||||
9382 | InsertIndices.push_back(i); | |||
9383 | continue; | |||
9384 | } | |||
9385 | ||||
9386 | SDValue ExtractedFromVec = Op.getOperand(i).getOperand(0); | |||
9387 | SDValue ExtIdx = Op.getOperand(i).getOperand(1); | |||
9388 | ||||
9389 | // Quit if non-constant index. | |||
9390 | if (!isa<ConstantSDNode>(ExtIdx)) | |||
9391 | return SDValue(); | |||
9392 | int Idx = getUnderlyingExtractedFromVec(ExtractedFromVec, ExtIdx); | |||
9393 | ||||
9394 | // Quit if extracted from vector of different type. | |||
9395 | if (ExtractedFromVec.getValueType() != VT) | |||
9396 | return SDValue(); | |||
9397 | ||||
9398 | if (!VecIn1.getNode()) | |||
9399 | VecIn1 = ExtractedFromVec; | |||
9400 | else if (VecIn1 != ExtractedFromVec) { | |||
9401 | if (!VecIn2.getNode()) | |||
9402 | VecIn2 = ExtractedFromVec; | |||
9403 | else if (VecIn2 != ExtractedFromVec) | |||
9404 | // Quit if more than 2 vectors to shuffle | |||
9405 | return SDValue(); | |||
9406 | } | |||
9407 | ||||
9408 | if (ExtractedFromVec == VecIn1) | |||
9409 | Mask[i] = Idx; | |||
9410 | else if (ExtractedFromVec == VecIn2) | |||
9411 | Mask[i] = Idx + NumElems; | |||
9412 | } | |||
9413 | ||||
9414 | if (!VecIn1.getNode()) | |||
9415 | return SDValue(); | |||
9416 | ||||
9417 | VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT); | |||
9418 | SDValue NV = DAG.getVectorShuffle(VT, DL, VecIn1, VecIn2, Mask); | |||
9419 | ||||
9420 | for (unsigned Idx : InsertIndices) | |||
9421 | NV = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, NV, Op.getOperand(Idx), | |||
9422 | DAG.getIntPtrConstant(Idx, DL)); | |||
9423 | ||||
9424 | return NV; | |||
9425 | } | |||
9426 | ||||
9427 | // Lower BUILD_VECTOR operation for v8i1 and v16i1 types. | |||
9428 | static SDValue LowerBUILD_VECTORvXi1(SDValue Op, SelectionDAG &DAG, | |||
9429 | const X86Subtarget &Subtarget) { | |||
9430 | ||||
9431 | MVT VT = Op.getSimpleValueType(); | |||
9432 | assert((VT.getVectorElementType() == MVT::i1) &&(static_cast <bool> ((VT.getVectorElementType() == MVT:: i1) && "Unexpected type in LowerBUILD_VECTORvXi1!") ? void (0) : __assert_fail ("(VT.getVectorElementType() == MVT::i1) && \"Unexpected type in LowerBUILD_VECTORvXi1!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 9433, __extension__ __PRETTY_FUNCTION__)) | |||
9433 | "Unexpected type in LowerBUILD_VECTORvXi1!")(static_cast <bool> ((VT.getVectorElementType() == MVT:: i1) && "Unexpected type in LowerBUILD_VECTORvXi1!") ? void (0) : __assert_fail ("(VT.getVectorElementType() == MVT::i1) && \"Unexpected type in LowerBUILD_VECTORvXi1!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 9433, __extension__ __PRETTY_FUNCTION__)); | |||
9434 | ||||
9435 | SDLoc dl(Op); | |||
9436 | if (ISD::isBuildVectorAllZeros(Op.getNode()) || | |||
9437 | ISD::isBuildVectorAllOnes(Op.getNode())) | |||
9438 | return Op; | |||
9439 | ||||
9440 | uint64_t Immediate = 0; | |||
9441 | SmallVector<unsigned, 16> NonConstIdx; | |||
9442 | bool IsSplat = true; | |||
9443 | bool HasConstElts = false; | |||
9444 | int SplatIdx = -1; | |||
9445 | for (unsigned idx = 0, e = Op.getNumOperands(); idx < e; ++idx) { | |||
9446 | SDValue In = Op.getOperand(idx); | |||
9447 | if (In.isUndef()) | |||
9448 | continue; | |||
9449 | if (auto *InC = dyn_cast<ConstantSDNode>(In)) { | |||
9450 | Immediate |= (InC->getZExtValue() & 0x1) << idx; | |||
9451 | HasConstElts = true; | |||
9452 | } else { | |||
9453 | NonConstIdx.push_back(idx); | |||
9454 | } | |||
9455 | if (SplatIdx < 0) | |||
9456 | SplatIdx = idx; | |||
9457 | else if (In != Op.getOperand(SplatIdx)) | |||
9458 | IsSplat = false; | |||
9459 | } | |||
9460 | ||||
9461 | // for splat use " (select i1 splat_elt, all-ones, all-zeroes)" | |||
9462 | if (IsSplat) { | |||
9463 | // The build_vector allows the scalar element to be larger than the vector | |||
9464 | // element type. We need to mask it to use as a condition unless we know | |||
9465 | // the upper bits are zero. | |||
9466 | // FIXME: Use computeKnownBits instead of checking specific opcode? | |||
9467 | SDValue Cond = Op.getOperand(SplatIdx); | |||
9468 | assert(Cond.getValueType() == MVT::i8 && "Unexpected VT!")(static_cast <bool> (Cond.getValueType() == MVT::i8 && "Unexpected VT!") ? void (0) : __assert_fail ("Cond.getValueType() == MVT::i8 && \"Unexpected VT!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 9468, __extension__ __PRETTY_FUNCTION__)); | |||
9469 | if (Cond.getOpcode() != ISD::SETCC) | |||
9470 | Cond = DAG.getNode(ISD::AND, dl, MVT::i8, Cond, | |||
9471 | DAG.getConstant(1, dl, MVT::i8)); | |||
9472 | ||||
9473 | // Perform the select in the scalar domain so we can use cmov. | |||
9474 | if (VT == MVT::v64i1 && !Subtarget.is64Bit()) { | |||
9475 | SDValue Select = DAG.getSelect(dl, MVT::i32, Cond, | |||
9476 | DAG.getAllOnesConstant(dl, MVT::i32), | |||
9477 | DAG.getConstant(0, dl, MVT::i32)); | |||
9478 | Select = DAG.getBitcast(MVT::v32i1, Select); | |||
9479 | return DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v64i1, Select, Select); | |||
9480 | } else { | |||
9481 | MVT ImmVT = MVT::getIntegerVT(std::max((unsigned)VT.getSizeInBits(), 8U)); | |||
9482 | SDValue Select = DAG.getSelect(dl, ImmVT, Cond, | |||
9483 | DAG.getAllOnesConstant(dl, ImmVT), | |||
9484 | DAG.getConstant(0, dl, ImmVT)); | |||
9485 | MVT VecVT = VT.getSizeInBits() >= 8 ? VT : MVT::v8i1; | |||
9486 | Select = DAG.getBitcast(VecVT, Select); | |||
9487 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, Select, | |||
9488 | DAG.getIntPtrConstant(0, dl)); | |||
9489 | } | |||
9490 | } | |||
9491 | ||||
9492 | // insert elements one by one | |||
9493 | SDValue DstVec; | |||
9494 | if (HasConstElts) { | |||
9495 | if (VT == MVT::v64i1 && !Subtarget.is64Bit()) { | |||
9496 | SDValue ImmL = DAG.getConstant(Lo_32(Immediate), dl, MVT::i32); | |||
9497 | SDValue ImmH = DAG.getConstant(Hi_32(Immediate), dl, MVT::i32); | |||
9498 | ImmL = DAG.getBitcast(MVT::v32i1, ImmL); | |||
9499 | ImmH = DAG.getBitcast(MVT::v32i1, ImmH); | |||
9500 | DstVec = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v64i1, ImmL, ImmH); | |||
9501 | } else { | |||
9502 | MVT ImmVT = MVT::getIntegerVT(std::max((unsigned)VT.getSizeInBits(), 8U)); | |||
9503 | SDValue Imm = DAG.getConstant(Immediate, dl, ImmVT); | |||
9504 | MVT VecVT = VT.getSizeInBits() >= 8 ? VT : MVT::v8i1; | |||
9505 | DstVec = DAG.getBitcast(VecVT, Imm); | |||
9506 | DstVec = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, DstVec, | |||
9507 | DAG.getIntPtrConstant(0, dl)); | |||
9508 | } | |||
9509 | } else | |||
9510 | DstVec = DAG.getUNDEF(VT); | |||
9511 | ||||
9512 | for (unsigned i = 0, e = NonConstIdx.size(); i != e; ++i) { | |||
9513 | unsigned InsertIdx = NonConstIdx[i]; | |||
9514 | DstVec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DstVec, | |||
9515 | Op.getOperand(InsertIdx), | |||
9516 | DAG.getIntPtrConstant(InsertIdx, dl)); | |||
9517 | } | |||
9518 | return DstVec; | |||
9519 | } | |||
9520 | ||||
9521 | LLVM_ATTRIBUTE_UNUSED__attribute__((__unused__)) static bool isHorizOp(unsigned Opcode) { | |||
9522 | switch (Opcode) { | |||
9523 | case X86ISD::PACKSS: | |||
9524 | case X86ISD::PACKUS: | |||
9525 | case X86ISD::FHADD: | |||
9526 | case X86ISD::FHSUB: | |||
9527 | case X86ISD::HADD: | |||
9528 | case X86ISD::HSUB: | |||
9529 | return true; | |||
9530 | } | |||
9531 | return false; | |||
9532 | } | |||
9533 | ||||
9534 | /// This is a helper function of LowerToHorizontalOp(). | |||
9535 | /// This function checks that the build_vector \p N in input implements a | |||
9536 | /// 128-bit partial horizontal operation on a 256-bit vector, but that operation | |||
9537 | /// may not match the layout of an x86 256-bit horizontal instruction. | |||
9538 | /// In other words, if this returns true, then some extraction/insertion will | |||
9539 | /// be required to produce a valid horizontal instruction. | |||
9540 | /// | |||
9541 | /// Parameter \p Opcode defines the kind of horizontal operation to match. | |||
9542 | /// For example, if \p Opcode is equal to ISD::ADD, then this function | |||
9543 | /// checks if \p N implements a horizontal arithmetic add; if instead \p Opcode | |||
9544 | /// is equal to ISD::SUB, then this function checks if this is a horizontal | |||
9545 | /// arithmetic sub. | |||
9546 | /// | |||
9547 | /// This function only analyzes elements of \p N whose indices are | |||
9548 | /// in range [BaseIdx, LastIdx). | |||
9549 | /// | |||
9550 | /// TODO: This function was originally used to match both real and fake partial | |||
9551 | /// horizontal operations, but the index-matching logic is incorrect for that. | |||
9552 | /// See the corrected implementation in isHopBuildVector(). Can we reduce this | |||
9553 | /// code because it is only used for partial h-op matching now? | |||
9554 | static bool isHorizontalBinOpPart(const BuildVectorSDNode *N, unsigned Opcode, | |||
9555 | SelectionDAG &DAG, | |||
9556 | unsigned BaseIdx, unsigned LastIdx, | |||
9557 | SDValue &V0, SDValue &V1) { | |||
9558 | EVT VT = N->getValueType(0); | |||
9559 | assert(VT.is256BitVector() && "Only use for matching partial 256-bit h-ops")(static_cast <bool> (VT.is256BitVector() && "Only use for matching partial 256-bit h-ops" ) ? void (0) : __assert_fail ("VT.is256BitVector() && \"Only use for matching partial 256-bit h-ops\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 9559, __extension__ __PRETTY_FUNCTION__)); | |||
9560 | assert(BaseIdx * 2 <= LastIdx && "Invalid Indices in input!")(static_cast <bool> (BaseIdx * 2 <= LastIdx && "Invalid Indices in input!") ? void (0) : __assert_fail ("BaseIdx * 2 <= LastIdx && \"Invalid Indices in input!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 9560, __extension__ __PRETTY_FUNCTION__)); | |||
9561 | assert(VT.isVector() && VT.getVectorNumElements() >= LastIdx &&(static_cast <bool> (VT.isVector() && VT.getVectorNumElements () >= LastIdx && "Invalid Vector in input!") ? void (0) : __assert_fail ("VT.isVector() && VT.getVectorNumElements() >= LastIdx && \"Invalid Vector in input!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 9562, __extension__ __PRETTY_FUNCTION__)) | |||
9562 | "Invalid Vector in input!")(static_cast <bool> (VT.isVector() && VT.getVectorNumElements () >= LastIdx && "Invalid Vector in input!") ? void (0) : __assert_fail ("VT.isVector() && VT.getVectorNumElements() >= LastIdx && \"Invalid Vector in input!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 9562, __extension__ __PRETTY_FUNCTION__)); | |||
9563 | ||||
9564 | bool IsCommutable = (Opcode == ISD::ADD || Opcode == ISD::FADD); | |||
9565 | bool CanFold = true; | |||
9566 | unsigned ExpectedVExtractIdx = BaseIdx; | |||
9567 | unsigned NumElts = LastIdx - BaseIdx; | |||
9568 | V0 = DAG.getUNDEF(VT); | |||
9569 | V1 = DAG.getUNDEF(VT); | |||
9570 | ||||
9571 | // Check if N implements a horizontal binop. | |||
9572 | for (unsigned i = 0, e = NumElts; i != e && CanFold; ++i) { | |||
9573 | SDValue Op = N->getOperand(i + BaseIdx); | |||
9574 | ||||
9575 | // Skip UNDEFs. | |||
9576 | if (Op->isUndef()) { | |||
9577 | // Update the expected vector extract index. | |||
9578 | if (i * 2 == NumElts) | |||
9579 | ExpectedVExtractIdx = BaseIdx; | |||
9580 | ExpectedVExtractIdx += 2; | |||
9581 | continue; | |||
9582 | } | |||
9583 | ||||
9584 | CanFold = Op->getOpcode() == Opcode && Op->hasOneUse(); | |||
9585 | ||||
9586 | if (!CanFold) | |||
9587 | break; | |||
9588 | ||||
9589 | SDValue Op0 = Op.getOperand(0); | |||
9590 | SDValue Op1 = Op.getOperand(1); | |||
9591 | ||||
9592 | // Try to match the following pattern: | |||
9593 | // (BINOP (extract_vector_elt A, I), (extract_vector_elt A, I+1)) | |||
9594 | CanFold = (Op0.getOpcode() == ISD::EXTRACT_VECTOR_ELT && | |||
9595 | Op1.getOpcode() == ISD::EXTRACT_VECTOR_ELT && | |||
9596 | Op0.getOperand(0) == Op1.getOperand(0) && | |||
9597 | isa<ConstantSDNode>(Op0.getOperand(1)) && | |||
9598 | isa<ConstantSDNode>(Op1.getOperand(1))); | |||
9599 | if (!CanFold) | |||
9600 | break; | |||
9601 | ||||
9602 | unsigned I0 = Op0.getConstantOperandVal(1); | |||
9603 | unsigned I1 = Op1.getConstantOperandVal(1); | |||
9604 | ||||
9605 | if (i * 2 < NumElts) { | |||
9606 | if (V0.isUndef()) { | |||
9607 | V0 = Op0.getOperand(0); | |||
9608 | if (V0.getValueType() != VT) | |||
9609 | return false; | |||
9610 | } | |||
9611 | } else { | |||
9612 | if (V1.isUndef()) { | |||
9613 | V1 = Op0.getOperand(0); | |||
9614 | if (V1.getValueType() != VT) | |||
9615 | return false; | |||
9616 | } | |||
9617 | if (i * 2 == NumElts) | |||
9618 | ExpectedVExtractIdx = BaseIdx; | |||
9619 | } | |||
9620 | ||||
9621 | SDValue Expected = (i * 2 < NumElts) ? V0 : V1; | |||
9622 | if (I0 == ExpectedVExtractIdx) | |||
9623 | CanFold = I1 == I0 + 1 && Op0.getOperand(0) == Expected; | |||
9624 | else if (IsCommutable && I1 == ExpectedVExtractIdx) { | |||
9625 | // Try to match the following dag sequence: | |||
9626 | // (BINOP (extract_vector_elt A, I+1), (extract_vector_elt A, I)) | |||
9627 | CanFold = I0 == I1 + 1 && Op1.getOperand(0) == Expected; | |||
9628 | } else | |||
9629 | CanFold = false; | |||
9630 | ||||
9631 | ExpectedVExtractIdx += 2; | |||
9632 | } | |||
9633 | ||||
9634 | return CanFold; | |||
9635 | } | |||
9636 | ||||
9637 | /// Emit a sequence of two 128-bit horizontal add/sub followed by | |||
9638 | /// a concat_vector. | |||
9639 | /// | |||
9640 | /// This is a helper function of LowerToHorizontalOp(). | |||
9641 | /// This function expects two 256-bit vectors called V0 and V1. | |||
9642 | /// At first, each vector is split into two separate 128-bit vectors. | |||
9643 | /// Then, the resulting 128-bit vectors are used to implement two | |||
9644 | /// horizontal binary operations. | |||
9645 | /// | |||
9646 | /// The kind of horizontal binary operation is defined by \p X86Opcode. | |||
9647 | /// | |||
9648 | /// \p Mode specifies how the 128-bit parts of V0 and V1 are passed in input to | |||
9649 | /// the two new horizontal binop. | |||
9650 | /// When Mode is set, the first horizontal binop dag node would take as input | |||
9651 | /// the lower 128-bit of V0 and the upper 128-bit of V0. The second | |||
9652 | /// horizontal binop dag node would take as input the lower 128-bit of V1 | |||
9653 | /// and the upper 128-bit of V1. | |||
9654 | /// Example: | |||
9655 | /// HADD V0_LO, V0_HI | |||
9656 | /// HADD V1_LO, V1_HI | |||
9657 | /// | |||
9658 | /// Otherwise, the first horizontal binop dag node takes as input the lower | |||
9659 | /// 128-bit of V0 and the lower 128-bit of V1, and the second horizontal binop | |||
9660 | /// dag node takes the upper 128-bit of V0 and the upper 128-bit of V1. | |||
9661 | /// Example: | |||
9662 | /// HADD V0_LO, V1_LO | |||
9663 | /// HADD V0_HI, V1_HI | |||
9664 | /// | |||
9665 | /// If \p isUndefLO is set, then the algorithm propagates UNDEF to the lower | |||
9666 | /// 128-bits of the result. If \p isUndefHI is set, then UNDEF is propagated to | |||
9667 | /// the upper 128-bits of the result. | |||
9668 | static SDValue ExpandHorizontalBinOp(const SDValue &V0, const SDValue &V1, | |||
9669 | const SDLoc &DL, SelectionDAG &DAG, | |||
9670 | unsigned X86Opcode, bool Mode, | |||
9671 | bool isUndefLO, bool isUndefHI) { | |||
9672 | MVT VT = V0.getSimpleValueType(); | |||
9673 | assert(VT.is256BitVector() && VT == V1.getSimpleValueType() &&(static_cast <bool> (VT.is256BitVector() && VT == V1.getSimpleValueType() && "Invalid nodes in input!" ) ? void (0) : __assert_fail ("VT.is256BitVector() && VT == V1.getSimpleValueType() && \"Invalid nodes in input!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 9674, __extension__ __PRETTY_FUNCTION__)) | |||
9674 | "Invalid nodes in input!")(static_cast <bool> (VT.is256BitVector() && VT == V1.getSimpleValueType() && "Invalid nodes in input!" ) ? void (0) : __assert_fail ("VT.is256BitVector() && VT == V1.getSimpleValueType() && \"Invalid nodes in input!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 9674, __extension__ __PRETTY_FUNCTION__)); | |||
9675 | ||||
9676 | unsigned NumElts = VT.getVectorNumElements(); | |||
9677 | SDValue V0_LO = extract128BitVector(V0, 0, DAG, DL); | |||
9678 | SDValue V0_HI = extract128BitVector(V0, NumElts/2, DAG, DL); | |||
9679 | SDValue V1_LO = extract128BitVector(V1, 0, DAG, DL); | |||
9680 | SDValue V1_HI = extract128BitVector(V1, NumElts/2, DAG, DL); | |||
9681 | MVT NewVT = V0_LO.getSimpleValueType(); | |||
9682 | ||||
9683 | SDValue LO = DAG.getUNDEF(NewVT); | |||
9684 | SDValue HI = DAG.getUNDEF(NewVT); | |||
9685 | ||||
9686 | if (Mode) { | |||
9687 | // Don't emit a horizontal binop if the result is expected to be UNDEF. | |||
9688 | if (!isUndefLO && !V0->isUndef()) | |||
9689 | LO = DAG.getNode(X86Opcode, DL, NewVT, V0_LO, V0_HI); | |||
9690 | if (!isUndefHI && !V1->isUndef()) | |||
9691 | HI = DAG.getNode(X86Opcode, DL, NewVT, V1_LO, V1_HI); | |||
9692 | } else { | |||
9693 | // Don't emit a horizontal binop if the result is expected to be UNDEF. | |||
9694 | if (!isUndefLO && (!V0_LO->isUndef() || !V1_LO->isUndef())) | |||
9695 | LO = DAG.getNode(X86Opcode, DL, NewVT, V0_LO, V1_LO); | |||
9696 | ||||
9697 | if (!isUndefHI && (!V0_HI->isUndef() || !V1_HI->isUndef())) | |||
9698 | HI = DAG.getNode(X86Opcode, DL, NewVT, V0_HI, V1_HI); | |||
9699 | } | |||
9700 | ||||
9701 | return DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, LO, HI); | |||
9702 | } | |||
9703 | ||||
9704 | /// Returns true iff \p BV builds a vector with the result equivalent to | |||
9705 | /// the result of ADDSUB/SUBADD operation. | |||
9706 | /// If true is returned then the operands of ADDSUB = Opnd0 +- Opnd1 | |||
9707 | /// (SUBADD = Opnd0 -+ Opnd1) operation are written to the parameters | |||
9708 | /// \p Opnd0 and \p Opnd1. | |||
9709 | static bool isAddSubOrSubAdd(const BuildVectorSDNode *BV, | |||
9710 | const X86Subtarget &Subtarget, SelectionDAG &DAG, | |||
9711 | SDValue &Opnd0, SDValue &Opnd1, | |||
9712 | unsigned &NumExtracts, | |||
9713 | bool &IsSubAdd) { | |||
9714 | ||||
9715 | MVT VT = BV->getSimpleValueType(0); | |||
9716 | if (!Subtarget.hasSSE3() || !VT.isFloatingPoint()) | |||
9717 | return false; | |||
9718 | ||||
9719 | unsigned NumElts = VT.getVectorNumElements(); | |||
9720 | SDValue InVec0 = DAG.getUNDEF(VT); | |||
9721 | SDValue InVec1 = DAG.getUNDEF(VT); | |||
9722 | ||||
9723 | NumExtracts = 0; | |||
9724 | ||||
9725 | // Odd-numbered elements in the input build vector are obtained from | |||
9726 | // adding/subtracting two integer/float elements. | |||
9727 | // Even-numbered elements in the input build vector are obtained from | |||
9728 | // subtracting/adding two integer/float elements. | |||
9729 | unsigned Opc[2] = {0, 0}; | |||
9730 | for (unsigned i = 0, e = NumElts; i != e; ++i) { | |||
9731 | SDValue Op = BV->getOperand(i); | |||
9732 | ||||
9733 | // Skip 'undef' values. | |||
9734 | unsigned Opcode = Op.getOpcode(); | |||
9735 | if (Opcode == ISD::UNDEF) | |||
9736 | continue; | |||
9737 | ||||
9738 | // Early exit if we found an unexpected opcode. | |||
9739 | if (Opcode != ISD::FADD && Opcode != ISD::FSUB) | |||
9740 | return false; | |||
9741 | ||||
9742 | SDValue Op0 = Op.getOperand(0); | |||
9743 | SDValue Op1 = Op.getOperand(1); | |||
9744 | ||||
9745 | // Try to match the following pattern: | |||
9746 | // (BINOP (extract_vector_elt A, i), (extract_vector_elt B, i)) | |||
9747 | // Early exit if we cannot match that sequence. | |||
9748 | if (Op0.getOpcode() != ISD::EXTRACT_VECTOR_ELT || | |||
9749 | Op1.getOpcode() != ISD::EXTRACT_VECTOR_ELT || | |||
9750 | !isa<ConstantSDNode>(Op0.getOperand(1)) || | |||
9751 | Op0.getOperand(1) != Op1.getOperand(1)) | |||
9752 | return false; | |||
9753 | ||||
9754 | unsigned I0 = Op0.getConstantOperandVal(1); | |||
9755 | if (I0 != i) | |||
9756 | return false; | |||
9757 | ||||
9758 | // We found a valid add/sub node, make sure its the same opcode as previous | |||
9759 | // elements for this parity. | |||
9760 | if (Opc[i % 2] != 0 && Opc[i % 2] != Opcode) | |||
9761 | return false; | |||
9762 | Opc[i % 2] = Opcode; | |||
9763 | ||||
9764 | // Update InVec0 and InVec1. | |||
9765 | if (InVec0.isUndef()) { | |||
9766 | InVec0 = Op0.getOperand(0); | |||
9767 | if (InVec0.getSimpleValueType() != VT) | |||
9768 | return false; | |||
9769 | } | |||
9770 | if (InVec1.isUndef()) { | |||
9771 | InVec1 = Op1.getOperand(0); | |||
9772 | if (InVec1.getSimpleValueType() != VT) | |||
9773 | return false; | |||
9774 | } | |||
9775 | ||||
9776 | // Make sure that operands in input to each add/sub node always | |||
9777 | // come from a same pair of vectors. | |||
9778 | if (InVec0 != Op0.getOperand(0)) { | |||
9779 | if (Opcode == ISD::FSUB) | |||
9780 | return false; | |||
9781 | ||||
9782 | // FADD is commutable. Try to commute the operands | |||
9783 | // and then test again. | |||
9784 | std::swap(Op0, Op1); | |||
9785 | if (InVec0 != Op0.getOperand(0)) | |||
9786 | return false; | |||
9787 | } | |||
9788 | ||||
9789 | if (InVec1 != Op1.getOperand(0)) | |||
9790 | return false; | |||
9791 | ||||
9792 | // Increment the number of extractions done. | |||
9793 | ++NumExtracts; | |||
9794 | } | |||
9795 | ||||
9796 | // Ensure we have found an opcode for both parities and that they are | |||
9797 | // different. Don't try to fold this build_vector into an ADDSUB/SUBADD if the | |||
9798 | // inputs are undef. | |||
9799 | if (!Opc[0] || !Opc[1] || Opc[0] == Opc[1] || | |||
9800 | InVec0.isUndef() || InVec1.isUndef()) | |||
9801 | return false; | |||
9802 | ||||
9803 | IsSubAdd = Opc[0] == ISD::FADD; | |||
9804 | ||||
9805 | Opnd0 = InVec0; | |||
9806 | Opnd1 = InVec1; | |||
9807 | return true; | |||
9808 | } | |||
9809 | ||||
9810 | /// Returns true if is possible to fold MUL and an idiom that has already been | |||
9811 | /// recognized as ADDSUB/SUBADD(\p Opnd0, \p Opnd1) into | |||
9812 | /// FMADDSUB/FMSUBADD(x, y, \p Opnd1). If (and only if) true is returned, the | |||
9813 | /// operands of FMADDSUB/FMSUBADD are written to parameters \p Opnd0, \p Opnd1, \p Opnd2. | |||
9814 | /// | |||
9815 | /// Prior to calling this function it should be known that there is some | |||
9816 | /// SDNode that potentially can be replaced with an X86ISD::ADDSUB operation | |||
9817 | /// using \p Opnd0 and \p Opnd1 as operands. Also, this method is called | |||
9818 | /// before replacement of such SDNode with ADDSUB operation. Thus the number | |||
9819 | /// of \p Opnd0 uses is expected to be equal to 2. | |||
9820 | /// For example, this function may be called for the following IR: | |||
9821 | /// %AB = fmul fast <2 x double> %A, %B | |||
9822 | /// %Sub = fsub fast <2 x double> %AB, %C | |||
9823 | /// %Add = fadd fast <2 x double> %AB, %C | |||
9824 | /// %Addsub = shufflevector <2 x double> %Sub, <2 x double> %Add, | |||
9825 | /// <2 x i32> <i32 0, i32 3> | |||
9826 | /// There is a def for %Addsub here, which potentially can be replaced by | |||
9827 | /// X86ISD::ADDSUB operation: | |||
9828 | /// %Addsub = X86ISD::ADDSUB %AB, %C | |||
9829 | /// and such ADDSUB can further be replaced with FMADDSUB: | |||
9830 | /// %Addsub = FMADDSUB %A, %B, %C. | |||
9831 | /// | |||
9832 | /// The main reason why this method is called before the replacement of the | |||
9833 | /// recognized ADDSUB idiom with ADDSUB operation is that such replacement | |||
9834 | /// is illegal sometimes. E.g. 512-bit ADDSUB is not available, while 512-bit | |||
9835 | /// FMADDSUB is. | |||
9836 | static bool isFMAddSubOrFMSubAdd(const X86Subtarget &Subtarget, | |||
9837 | SelectionDAG &DAG, | |||
9838 | SDValue &Opnd0, SDValue &Opnd1, SDValue &Opnd2, | |||
9839 | unsigned ExpectedUses) { | |||
9840 | if (Opnd0.getOpcode() != ISD::FMUL || | |||
9841 | !Opnd0->hasNUsesOfValue(ExpectedUses, 0) || !Subtarget.hasAnyFMA()) | |||
9842 | return false; | |||
9843 | ||||
9844 | // FIXME: These checks must match the similar ones in | |||
9845 | // DAGCombiner::visitFADDForFMACombine. It would be good to have one | |||
9846 | // function that would answer if it is Ok to fuse MUL + ADD to FMADD | |||
9847 | // or MUL + ADDSUB to FMADDSUB. | |||
9848 | const TargetOptions &Options = DAG.getTarget().Options; | |||
9849 | bool AllowFusion = | |||
9850 | (Options.AllowFPOpFusion == FPOpFusion::Fast || Options.UnsafeFPMath); | |||
9851 | if (!AllowFusion) | |||
9852 | return false; | |||
9853 | ||||
9854 | Opnd2 = Opnd1; | |||
9855 | Opnd1 = Opnd0.getOperand(1); | |||
9856 | Opnd0 = Opnd0.getOperand(0); | |||
9857 | ||||
9858 | return true; | |||
9859 | } | |||
9860 | ||||
9861 | /// Try to fold a build_vector that performs an 'addsub' or 'fmaddsub' or | |||
9862 | /// 'fsubadd' operation accordingly to X86ISD::ADDSUB or X86ISD::FMADDSUB or | |||
9863 | /// X86ISD::FMSUBADD node. | |||
9864 | static SDValue lowerToAddSubOrFMAddSub(const BuildVectorSDNode *BV, | |||
9865 | const X86Subtarget &Subtarget, | |||
9866 | SelectionDAG &DAG) { | |||
9867 | SDValue Opnd0, Opnd1; | |||
9868 | unsigned NumExtracts; | |||
9869 | bool IsSubAdd; | |||
9870 | if (!isAddSubOrSubAdd(BV, Subtarget, DAG, Opnd0, Opnd1, NumExtracts, | |||
9871 | IsSubAdd)) | |||
9872 | return SDValue(); | |||
9873 | ||||
9874 | MVT VT = BV->getSimpleValueType(0); | |||
9875 | SDLoc DL(BV); | |||
9876 | ||||
9877 | // Try to generate X86ISD::FMADDSUB node here. | |||
9878 | SDValue Opnd2; | |||
9879 | if (isFMAddSubOrFMSubAdd(Subtarget, DAG, Opnd0, Opnd1, Opnd2, NumExtracts)) { | |||
9880 | unsigned Opc = IsSubAdd ? X86ISD::FMSUBADD : X86ISD::FMADDSUB; | |||
9881 | return DAG.getNode(Opc, DL, VT, Opnd0, Opnd1, Opnd2); | |||
9882 | } | |||
9883 | ||||
9884 | // We only support ADDSUB. | |||
9885 | if (IsSubAdd) | |||
9886 | return SDValue(); | |||
9887 | ||||
9888 | // Do not generate X86ISD::ADDSUB node for 512-bit types even though | |||
9889 | // the ADDSUB idiom has been successfully recognized. There are no known | |||
9890 | // X86 targets with 512-bit ADDSUB instructions! | |||
9891 | // 512-bit ADDSUB idiom recognition was needed only as part of FMADDSUB idiom | |||
9892 | // recognition. | |||
9893 | if (VT.is512BitVector()) | |||
9894 | return SDValue(); | |||
9895 | ||||
9896 | return DAG.getNode(X86ISD::ADDSUB, DL, VT, Opnd0, Opnd1); | |||
9897 | } | |||
9898 | ||||
9899 | static bool isHopBuildVector(const BuildVectorSDNode *BV, SelectionDAG &DAG, | |||
9900 | unsigned &HOpcode, SDValue &V0, SDValue &V1) { | |||
9901 | // Initialize outputs to known values. | |||
9902 | MVT VT = BV->getSimpleValueType(0); | |||
9903 | HOpcode = ISD::DELETED_NODE; | |||
9904 | V0 = DAG.getUNDEF(VT); | |||
9905 | V1 = DAG.getUNDEF(VT); | |||
9906 | ||||
9907 | // x86 256-bit horizontal ops are defined in a non-obvious way. Each 128-bit | |||
9908 | // half of the result is calculated independently from the 128-bit halves of | |||
9909 | // the inputs, so that makes the index-checking logic below more complicated. | |||
9910 | unsigned NumElts = VT.getVectorNumElements(); | |||
9911 | unsigned GenericOpcode = ISD::DELETED_NODE; | |||
9912 | unsigned Num128BitChunks = VT.is256BitVector() ? 2 : 1; | |||
9913 | unsigned NumEltsIn128Bits = NumElts / Num128BitChunks; | |||
9914 | unsigned NumEltsIn64Bits = NumEltsIn128Bits / 2; | |||
9915 | for (unsigned i = 0; i != Num128BitChunks; ++i) { | |||
9916 | for (unsigned j = 0; j != NumEltsIn128Bits; ++j) { | |||
9917 | // Ignore undef elements. | |||
9918 | SDValue Op = BV->getOperand(i * NumEltsIn128Bits + j); | |||
9919 | if (Op.isUndef()) | |||
9920 | continue; | |||
9921 | ||||
9922 | // If there's an opcode mismatch, we're done. | |||
9923 | if (HOpcode != ISD::DELETED_NODE && Op.getOpcode() != GenericOpcode) | |||
9924 | return false; | |||
9925 | ||||
9926 | // Initialize horizontal opcode. | |||
9927 | if (HOpcode == ISD::DELETED_NODE) { | |||
9928 | GenericOpcode = Op.getOpcode(); | |||
9929 | switch (GenericOpcode) { | |||
9930 | case ISD::ADD: HOpcode = X86ISD::HADD; break; | |||
9931 | case ISD::SUB: HOpcode = X86ISD::HSUB; break; | |||
9932 | case ISD::FADD: HOpcode = X86ISD::FHADD; break; | |||
9933 | case ISD::FSUB: HOpcode = X86ISD::FHSUB; break; | |||
9934 | default: return false; | |||
9935 | } | |||
9936 | } | |||
9937 | ||||
9938 | SDValue Op0 = Op.getOperand(0); | |||
9939 | SDValue Op1 = Op.getOperand(1); | |||
9940 | if (Op0.getOpcode() != ISD::EXTRACT_VECTOR_ELT || | |||
9941 | Op1.getOpcode() != ISD::EXTRACT_VECTOR_ELT || | |||
9942 | Op0.getOperand(0) != Op1.getOperand(0) || | |||
9943 | !isa<ConstantSDNode>(Op0.getOperand(1)) || | |||
9944 | !isa<ConstantSDNode>(Op1.getOperand(1)) || !Op.hasOneUse()) | |||
9945 | return false; | |||
9946 | ||||
9947 | // The source vector is chosen based on which 64-bit half of the | |||
9948 | // destination vector is being calculated. | |||
9949 | if (j < NumEltsIn64Bits) { | |||
9950 | if (V0.isUndef()) | |||
9951 | V0 = Op0.getOperand(0); | |||
9952 | } else { | |||
9953 | if (V1.isUndef()) | |||
9954 | V1 = Op0.getOperand(0); | |||
9955 | } | |||
9956 | ||||
9957 | SDValue SourceVec = (j < NumEltsIn64Bits) ? V0 : V1; | |||
9958 | if (SourceVec != Op0.getOperand(0)) | |||
9959 | return false; | |||
9960 | ||||
9961 | // op (extract_vector_elt A, I), (extract_vector_elt A, I+1) | |||
9962 | unsigned ExtIndex0 = Op0.getConstantOperandVal(1); | |||
9963 | unsigned ExtIndex1 = Op1.getConstantOperandVal(1); | |||
9964 | unsigned ExpectedIndex = i * NumEltsIn128Bits + | |||
9965 | (j % NumEltsIn64Bits) * 2; | |||
9966 | if (ExpectedIndex == ExtIndex0 && ExtIndex1 == ExtIndex0 + 1) | |||
9967 | continue; | |||
9968 | ||||
9969 | // If this is not a commutative op, this does not match. | |||
9970 | if (GenericOpcode != ISD::ADD && GenericOpcode != ISD::FADD) | |||
9971 | return false; | |||
9972 | ||||
9973 | // Addition is commutative, so try swapping the extract indexes. | |||
9974 | // op (extract_vector_elt A, I+1), (extract_vector_elt A, I) | |||
9975 | if (ExpectedIndex == ExtIndex1 && ExtIndex0 == ExtIndex1 + 1) | |||
9976 | continue; | |||
9977 | ||||
9978 | // Extract indexes do not match horizontal requirement. | |||
9979 | return false; | |||
9980 | } | |||
9981 | } | |||
9982 | // We matched. Opcode and operands are returned by reference as arguments. | |||
9983 | return true; | |||
9984 | } | |||
9985 | ||||
9986 | static SDValue getHopForBuildVector(const BuildVectorSDNode *BV, | |||
9987 | SelectionDAG &DAG, unsigned HOpcode, | |||
9988 | SDValue V0, SDValue V1) { | |||
9989 | // If either input vector is not the same size as the build vector, | |||
9990 | // extract/insert the low bits to the correct size. | |||
9991 | // This is free (examples: zmm --> xmm, xmm --> ymm). | |||
9992 | MVT VT = BV->getSimpleValueType(0); | |||
9993 | unsigned Width = VT.getSizeInBits(); | |||
9994 | if (V0.getValueSizeInBits() > Width) | |||
9995 | V0 = extractSubVector(V0, 0, DAG, SDLoc(BV), Width); | |||
9996 | else if (V0.getValueSizeInBits() < Width) | |||
9997 | V0 = insertSubVector(DAG.getUNDEF(VT), V0, 0, DAG, SDLoc(BV), Width); | |||
9998 | ||||
9999 | if (V1.getValueSizeInBits() > Width) | |||
10000 | V1 = extractSubVector(V1, 0, DAG, SDLoc(BV), Width); | |||
10001 | else if (V1.getValueSizeInBits() < Width) | |||
10002 | V1 = insertSubVector(DAG.getUNDEF(VT), V1, 0, DAG, SDLoc(BV), Width); | |||
10003 | ||||
10004 | unsigned NumElts = VT.getVectorNumElements(); | |||
10005 | APInt DemandedElts = APInt::getAllOnes(NumElts); | |||
10006 | for (unsigned i = 0; i != NumElts; ++i) | |||
10007 | if (BV->getOperand(i).isUndef()) | |||
10008 | DemandedElts.clearBit(i); | |||
10009 | ||||
10010 | // If we don't need the upper xmm, then perform as a xmm hop. | |||
10011 | unsigned HalfNumElts = NumElts / 2; | |||
10012 | if (VT.is256BitVector() && DemandedElts.lshr(HalfNumElts) == 0) { | |||
10013 | MVT HalfVT = VT.getHalfNumVectorElementsVT(); | |||
10014 | V0 = extractSubVector(V0, 0, DAG, SDLoc(BV), 128); | |||
10015 | V1 = extractSubVector(V1, 0, DAG, SDLoc(BV), 128); | |||
10016 | SDValue Half = DAG.getNode(HOpcode, SDLoc(BV), HalfVT, V0, V1); | |||
10017 | return insertSubVector(DAG.getUNDEF(VT), Half, 0, DAG, SDLoc(BV), 256); | |||
10018 | } | |||
10019 | ||||
10020 | return DAG.getNode(HOpcode, SDLoc(BV), VT, V0, V1); | |||
10021 | } | |||
10022 | ||||
10023 | /// Lower BUILD_VECTOR to a horizontal add/sub operation if possible. | |||
10024 | static SDValue LowerToHorizontalOp(const BuildVectorSDNode *BV, | |||
10025 | const X86Subtarget &Subtarget, | |||
10026 | SelectionDAG &DAG) { | |||
10027 | // We need at least 2 non-undef elements to make this worthwhile by default. | |||
10028 | unsigned NumNonUndefs = | |||
10029 | count_if(BV->op_values(), [](SDValue V) { return !V.isUndef(); }); | |||
10030 | if (NumNonUndefs < 2) | |||
10031 | return SDValue(); | |||
10032 | ||||
10033 | // There are 4 sets of horizontal math operations distinguished by type: | |||
10034 | // int/FP at 128-bit/256-bit. Each type was introduced with a different | |||
10035 | // subtarget feature. Try to match those "native" patterns first. | |||
10036 | MVT VT = BV->getSimpleValueType(0); | |||
10037 | if (((VT == MVT::v4f32 || VT == MVT::v2f64) && Subtarget.hasSSE3()) || | |||
10038 | ((VT == MVT::v8i16 || VT == MVT::v4i32) && Subtarget.hasSSSE3()) || | |||
10039 | ((VT == MVT::v8f32 || VT == MVT::v4f64) && Subtarget.hasAVX()) || | |||
10040 | ((VT == MVT::v16i16 || VT == MVT::v8i32) && Subtarget.hasAVX2())) { | |||
10041 | unsigned HOpcode; | |||
10042 | SDValue V0, V1; | |||
10043 | if (isHopBuildVector(BV, DAG, HOpcode, V0, V1)) | |||
10044 | return getHopForBuildVector(BV, DAG, HOpcode, V0, V1); | |||
10045 | } | |||
10046 | ||||
10047 | // Try harder to match 256-bit ops by using extract/concat. | |||
10048 | if (!Subtarget.hasAVX() || !VT.is256BitVector()) | |||
10049 | return SDValue(); | |||
10050 | ||||
10051 | // Count the number of UNDEF operands in the build_vector in input. | |||
10052 | unsigned NumElts = VT.getVectorNumElements(); | |||
10053 | unsigned Half = NumElts / 2; | |||
10054 | unsigned NumUndefsLO = 0; | |||
10055 | unsigned NumUndefsHI = 0; | |||
10056 | for (unsigned i = 0, e = Half; i != e; ++i) | |||
10057 | if (BV->getOperand(i)->isUndef()) | |||
10058 | NumUndefsLO++; | |||
10059 | ||||
10060 | for (unsigned i = Half, e = NumElts; i != e; ++i) | |||
10061 | if (BV->getOperand(i)->isUndef()) | |||
10062 | NumUndefsHI++; | |||
10063 | ||||
10064 | SDLoc DL(BV); | |||
10065 | SDValue InVec0, InVec1; | |||
10066 | if (VT == MVT::v8i32 || VT == MVT::v16i16) { | |||
10067 | SDValue InVec2, InVec3; | |||
10068 | unsigned X86Opcode; | |||
10069 | bool CanFold = true; | |||
10070 | ||||
10071 | if (isHorizontalBinOpPart(BV, ISD::ADD, DAG, 0, Half, InVec0, InVec1) && | |||
10072 | isHorizontalBinOpPart(BV, ISD::ADD, DAG, Half, NumElts, InVec2, | |||
10073 | InVec3) && | |||
10074 | ((InVec0.isUndef() || InVec2.isUndef()) || InVec0 == InVec2) && | |||
10075 | ((InVec1.isUndef() || InVec3.isUndef()) || InVec1 == InVec3)) | |||
10076 | X86Opcode = X86ISD::HADD; | |||
10077 | else if (isHorizontalBinOpPart(BV, ISD::SUB, DAG, 0, Half, InVec0, | |||
10078 | InVec1) && | |||
10079 | isHorizontalBinOpPart(BV, ISD::SUB, DAG, Half, NumElts, InVec2, | |||
10080 | InVec3) && | |||
10081 | ((InVec0.isUndef() || InVec2.isUndef()) || InVec0 == InVec2) && | |||
10082 | ((InVec1.isUndef() || InVec3.isUndef()) || InVec1 == InVec3)) | |||
10083 | X86Opcode = X86ISD::HSUB; | |||
10084 | else | |||
10085 | CanFold = false; | |||
10086 | ||||
10087 | if (CanFold) { | |||
10088 | // Do not try to expand this build_vector into a pair of horizontal | |||
10089 | // add/sub if we can emit a pair of scalar add/sub. | |||
10090 | if (NumUndefsLO + 1 == Half || NumUndefsHI + 1 == Half) | |||
10091 | return SDValue(); | |||
10092 | ||||
10093 | // Convert this build_vector into a pair of horizontal binops followed by | |||
10094 | // a concat vector. We must adjust the outputs from the partial horizontal | |||
10095 | // matching calls above to account for undefined vector halves. | |||
10096 | SDValue V0 = InVec0.isUndef() ? InVec2 : InVec0; | |||
10097 | SDValue V1 = InVec1.isUndef() ? InVec3 : InVec1; | |||
10098 | assert((!V0.isUndef() || !V1.isUndef()) && "Horizontal-op of undefs?")(static_cast <bool> ((!V0.isUndef() || !V1.isUndef()) && "Horizontal-op of undefs?") ? void (0) : __assert_fail ("(!V0.isUndef() || !V1.isUndef()) && \"Horizontal-op of undefs?\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 10098, __extension__ __PRETTY_FUNCTION__)); | |||
10099 | bool isUndefLO = NumUndefsLO == Half; | |||
10100 | bool isUndefHI = NumUndefsHI == Half; | |||
10101 | return ExpandHorizontalBinOp(V0, V1, DL, DAG, X86Opcode, false, isUndefLO, | |||
10102 | isUndefHI); | |||
10103 | } | |||
10104 | } | |||
10105 | ||||
10106 | if (VT == MVT::v8f32 || VT == MVT::v4f64 || VT == MVT::v8i32 || | |||
10107 | VT == MVT::v16i16) { | |||
10108 | unsigned X86Opcode; | |||
10109 | if (isHorizontalBinOpPart(BV, ISD::ADD, DAG, 0, NumElts, InVec0, InVec1)) | |||
10110 | X86Opcode = X86ISD::HADD; | |||
10111 | else if (isHorizontalBinOpPart(BV, ISD::SUB, DAG, 0, NumElts, InVec0, | |||
10112 | InVec1)) | |||
10113 | X86Opcode = X86ISD::HSUB; | |||
10114 | else if (isHorizontalBinOpPart(BV, ISD::FADD, DAG, 0, NumElts, InVec0, | |||
10115 | InVec1)) | |||
10116 | X86Opcode = X86ISD::FHADD; | |||
10117 | else if (isHorizontalBinOpPart(BV, ISD::FSUB, DAG, 0, NumElts, InVec0, | |||
10118 | InVec1)) | |||
10119 | X86Opcode = X86ISD::FHSUB; | |||
10120 | else | |||
10121 | return SDValue(); | |||
10122 | ||||
10123 | // Don't try to expand this build_vector into a pair of horizontal add/sub | |||
10124 | // if we can simply emit a pair of scalar add/sub. | |||
10125 | if (NumUndefsLO + 1 == Half || NumUndefsHI + 1 == Half) | |||
10126 | return SDValue(); | |||
10127 | ||||
10128 | // Convert this build_vector into two horizontal add/sub followed by | |||
10129 | // a concat vector. | |||
10130 | bool isUndefLO = NumUndefsLO == Half; | |||
10131 | bool isUndefHI = NumUndefsHI == Half; | |||
10132 | return ExpandHorizontalBinOp(InVec0, InVec1, DL, DAG, X86Opcode, true, | |||
10133 | isUndefLO, isUndefHI); | |||
10134 | } | |||
10135 | ||||
10136 | return SDValue(); | |||
10137 | } | |||
10138 | ||||
10139 | static SDValue LowerShift(SDValue Op, const X86Subtarget &Subtarget, | |||
10140 | SelectionDAG &DAG); | |||
10141 | ||||
10142 | /// If a BUILD_VECTOR's source elements all apply the same bit operation and | |||
10143 | /// one of their operands is constant, lower to a pair of BUILD_VECTOR and | |||
10144 | /// just apply the bit to the vectors. | |||
10145 | /// NOTE: Its not in our interest to start make a general purpose vectorizer | |||
10146 | /// from this, but enough scalar bit operations are created from the later | |||
10147 | /// legalization + scalarization stages to need basic support. | |||
10148 | static SDValue lowerBuildVectorToBitOp(BuildVectorSDNode *Op, | |||
10149 | const X86Subtarget &Subtarget, | |||
10150 | SelectionDAG &DAG) { | |||
10151 | SDLoc DL(Op); | |||
10152 | MVT VT = Op->getSimpleValueType(0); | |||
10153 | unsigned NumElems = VT.getVectorNumElements(); | |||
10154 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
10155 | ||||
10156 | // Check that all elements have the same opcode. | |||
10157 | // TODO: Should we allow UNDEFS and if so how many? | |||
10158 | unsigned Opcode = Op->getOperand(0).getOpcode(); | |||
10159 | for (unsigned i = 1; i < NumElems; ++i) | |||
10160 | if (Opcode != Op->getOperand(i).getOpcode()) | |||
10161 | return SDValue(); | |||
10162 | ||||
10163 | // TODO: We may be able to add support for other Ops (ADD/SUB + shifts). | |||
10164 | bool IsShift = false; | |||
10165 | switch (Opcode) { | |||
10166 | default: | |||
10167 | return SDValue(); | |||
10168 | case ISD::SHL: | |||
10169 | case ISD::SRL: | |||
10170 | case ISD::SRA: | |||
10171 | IsShift = true; | |||
10172 | break; | |||
10173 | case ISD::AND: | |||
10174 | case ISD::XOR: | |||
10175 | case ISD::OR: | |||
10176 | // Don't do this if the buildvector is a splat - we'd replace one | |||
10177 | // constant with an entire vector. | |||
10178 | if (Op->getSplatValue()) | |||
10179 | return SDValue(); | |||
10180 | if (!TLI.isOperationLegalOrPromote(Opcode, VT)) | |||
10181 | return SDValue(); | |||
10182 | break; | |||
10183 | } | |||
10184 | ||||
10185 | SmallVector<SDValue, 4> LHSElts, RHSElts; | |||
10186 | for (SDValue Elt : Op->ops()) { | |||
10187 | SDValue LHS = Elt.getOperand(0); | |||
10188 | SDValue RHS = Elt.getOperand(1); | |||
10189 | ||||
10190 | // We expect the canonicalized RHS operand to be the constant. | |||
10191 | if (!isa<ConstantSDNode>(RHS)) | |||
10192 | return SDValue(); | |||
10193 | ||||
10194 | // Extend shift amounts. | |||
10195 | if (RHS.getValueSizeInBits() != VT.getScalarSizeInBits()) { | |||
10196 | if (!IsShift) | |||
10197 | return SDValue(); | |||
10198 | RHS = DAG.getZExtOrTrunc(RHS, DL, VT.getScalarType()); | |||
10199 | } | |||
10200 | ||||
10201 | LHSElts.push_back(LHS); | |||
10202 | RHSElts.push_back(RHS); | |||
10203 | } | |||
10204 | ||||
10205 | // Limit to shifts by uniform immediates. | |||
10206 | // TODO: Only accept vXi8/vXi64 special cases? | |||
10207 | // TODO: Permit non-uniform XOP/AVX2/MULLO cases? | |||
10208 | if (IsShift && any_of(RHSElts, [&](SDValue V) { return RHSElts[0] != V; })) | |||
10209 | return SDValue(); | |||
10210 | ||||
10211 | SDValue LHS = DAG.getBuildVector(VT, DL, LHSElts); | |||
10212 | SDValue RHS = DAG.getBuildVector(VT, DL, RHSElts); | |||
10213 | SDValue Res = DAG.getNode(Opcode, DL, VT, LHS, RHS); | |||
10214 | ||||
10215 | if (!IsShift) | |||
10216 | return Res; | |||
10217 | ||||
10218 | // Immediately lower the shift to ensure the constant build vector doesn't | |||
10219 | // get converted to a constant pool before the shift is lowered. | |||
10220 | return LowerShift(Res, Subtarget, DAG); | |||
10221 | } | |||
10222 | ||||
10223 | /// Create a vector constant without a load. SSE/AVX provide the bare minimum | |||
10224 | /// functionality to do this, so it's all zeros, all ones, or some derivation | |||
10225 | /// that is cheap to calculate. | |||
10226 | static SDValue materializeVectorConstant(SDValue Op, SelectionDAG &DAG, | |||
10227 | const X86Subtarget &Subtarget) { | |||
10228 | SDLoc DL(Op); | |||
10229 | MVT VT = Op.getSimpleValueType(); | |||
10230 | ||||
10231 | // Vectors containing all zeros can be matched by pxor and xorps. | |||
10232 | if (ISD::isBuildVectorAllZeros(Op.getNode())) | |||
10233 | return Op; | |||
10234 | ||||
10235 | // Vectors containing all ones can be matched by pcmpeqd on 128-bit width | |||
10236 | // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use | |||
10237 | // vpcmpeqd on 256-bit vectors. | |||
10238 | if (Subtarget.hasSSE2() && ISD::isBuildVectorAllOnes(Op.getNode())) { | |||
10239 | if (VT == MVT::v4i32 || VT == MVT::v8i32 || VT == MVT::v16i32) | |||
10240 | return Op; | |||
10241 | ||||
10242 | return getOnesVector(VT, DAG, DL); | |||
10243 | } | |||
10244 | ||||
10245 | return SDValue(); | |||
10246 | } | |||
10247 | ||||
10248 | /// Look for opportunities to create a VPERMV/VPERMILPV/PSHUFB variable permute | |||
10249 | /// from a vector of source values and a vector of extraction indices. | |||
10250 | /// The vectors might be manipulated to match the type of the permute op. | |||
10251 | static SDValue createVariablePermute(MVT VT, SDValue SrcVec, SDValue IndicesVec, | |||
10252 | SDLoc &DL, SelectionDAG &DAG, | |||
10253 | const X86Subtarget &Subtarget) { | |||
10254 | MVT ShuffleVT = VT; | |||
10255 | EVT IndicesVT = EVT(VT).changeVectorElementTypeToInteger(); | |||
10256 | unsigned NumElts = VT.getVectorNumElements(); | |||
10257 | unsigned SizeInBits = VT.getSizeInBits(); | |||
10258 | ||||
10259 | // Adjust IndicesVec to match VT size. | |||
10260 | assert(IndicesVec.getValueType().getVectorNumElements() >= NumElts &&(static_cast <bool> (IndicesVec.getValueType().getVectorNumElements () >= NumElts && "Illegal variable permute mask size" ) ? void (0) : __assert_fail ("IndicesVec.getValueType().getVectorNumElements() >= NumElts && \"Illegal variable permute mask size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 10261, __extension__ __PRETTY_FUNCTION__)) | |||
10261 | "Illegal variable permute mask size")(static_cast <bool> (IndicesVec.getValueType().getVectorNumElements () >= NumElts && "Illegal variable permute mask size" ) ? void (0) : __assert_fail ("IndicesVec.getValueType().getVectorNumElements() >= NumElts && \"Illegal variable permute mask size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 10261, __extension__ __PRETTY_FUNCTION__)); | |||
10262 | if (IndicesVec.getValueType().getVectorNumElements() > NumElts) { | |||
10263 | // Narrow/widen the indices vector to the correct size. | |||
10264 | if (IndicesVec.getValueSizeInBits() > SizeInBits) | |||
10265 | IndicesVec = extractSubVector(IndicesVec, 0, DAG, SDLoc(IndicesVec), | |||
10266 | NumElts * VT.getScalarSizeInBits()); | |||
10267 | else if (IndicesVec.getValueSizeInBits() < SizeInBits) | |||
10268 | IndicesVec = widenSubVector(IndicesVec, false, Subtarget, DAG, | |||
10269 | SDLoc(IndicesVec), SizeInBits); | |||
10270 | // Zero-extend the index elements within the vector. | |||
10271 | if (IndicesVec.getValueType().getVectorNumElements() > NumElts) | |||
10272 | IndicesVec = DAG.getNode(ISD::ZERO_EXTEND_VECTOR_INREG, SDLoc(IndicesVec), | |||
10273 | IndicesVT, IndicesVec); | |||
10274 | } | |||
10275 | IndicesVec = DAG.getZExtOrTrunc(IndicesVec, SDLoc(IndicesVec), IndicesVT); | |||
10276 | ||||
10277 | // Handle SrcVec that don't match VT type. | |||
10278 | if (SrcVec.getValueSizeInBits() != SizeInBits) { | |||
10279 | if ((SrcVec.getValueSizeInBits() % SizeInBits) == 0) { | |||
10280 | // Handle larger SrcVec by treating it as a larger permute. | |||
10281 | unsigned Scale = SrcVec.getValueSizeInBits() / SizeInBits; | |||
10282 | VT = MVT::getVectorVT(VT.getScalarType(), Scale * NumElts); | |||
10283 | IndicesVT = EVT(VT).changeVectorElementTypeToInteger(); | |||
10284 | IndicesVec = widenSubVector(IndicesVT.getSimpleVT(), IndicesVec, false, | |||
10285 | Subtarget, DAG, SDLoc(IndicesVec)); | |||
10286 | SDValue NewSrcVec = | |||
10287 | createVariablePermute(VT, SrcVec, IndicesVec, DL, DAG, Subtarget); | |||
10288 | if (NewSrcVec) | |||
10289 | return extractSubVector(NewSrcVec, 0, DAG, DL, SizeInBits); | |||
10290 | return SDValue(); | |||
10291 | } else if (SrcVec.getValueSizeInBits() < SizeInBits) { | |||
10292 | // Widen smaller SrcVec to match VT. | |||
10293 | SrcVec = widenSubVector(VT, SrcVec, false, Subtarget, DAG, SDLoc(SrcVec)); | |||
10294 | } else | |||
10295 | return SDValue(); | |||
10296 | } | |||
10297 | ||||
10298 | auto ScaleIndices = [&DAG](SDValue Idx, uint64_t Scale) { | |||
10299 | assert(isPowerOf2_64(Scale) && "Illegal variable permute shuffle scale")(static_cast <bool> (isPowerOf2_64(Scale) && "Illegal variable permute shuffle scale" ) ? void (0) : __assert_fail ("isPowerOf2_64(Scale) && \"Illegal variable permute shuffle scale\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 10299, __extension__ __PRETTY_FUNCTION__)); | |||
10300 | EVT SrcVT = Idx.getValueType(); | |||
10301 | unsigned NumDstBits = SrcVT.getScalarSizeInBits() / Scale; | |||
10302 | uint64_t IndexScale = 0; | |||
10303 | uint64_t IndexOffset = 0; | |||
10304 | ||||
10305 | // If we're scaling a smaller permute op, then we need to repeat the | |||
10306 | // indices, scaling and offsetting them as well. | |||
10307 | // e.g. v4i32 -> v16i8 (Scale = 4) | |||
10308 | // IndexScale = v4i32 Splat(4 << 24 | 4 << 16 | 4 << 8 | 4) | |||
10309 | // IndexOffset = v4i32 Splat(3 << 24 | 2 << 16 | 1 << 8 | 0) | |||
10310 | for (uint64_t i = 0; i != Scale; ++i) { | |||
10311 | IndexScale |= Scale << (i * NumDstBits); | |||
10312 | IndexOffset |= i << (i * NumDstBits); | |||
10313 | } | |||
10314 | ||||
10315 | Idx = DAG.getNode(ISD::MUL, SDLoc(Idx), SrcVT, Idx, | |||
10316 | DAG.getConstant(IndexScale, SDLoc(Idx), SrcVT)); | |||
10317 | Idx = DAG.getNode(ISD::ADD, SDLoc(Idx), SrcVT, Idx, | |||
10318 | DAG.getConstant(IndexOffset, SDLoc(Idx), SrcVT)); | |||
10319 | return Idx; | |||
10320 | }; | |||
10321 | ||||
10322 | unsigned Opcode = 0; | |||
10323 | switch (VT.SimpleTy) { | |||
10324 | default: | |||
10325 | break; | |||
10326 | case MVT::v16i8: | |||
10327 | if (Subtarget.hasSSSE3()) | |||
10328 | Opcode = X86ISD::PSHUFB; | |||
10329 | break; | |||
10330 | case MVT::v8i16: | |||
10331 | if (Subtarget.hasVLX() && Subtarget.hasBWI()) | |||
10332 | Opcode = X86ISD::VPERMV; | |||
10333 | else if (Subtarget.hasSSSE3()) { | |||
10334 | Opcode = X86ISD::PSHUFB; | |||
10335 | ShuffleVT = MVT::v16i8; | |||
10336 | } | |||
10337 | break; | |||
10338 | case MVT::v4f32: | |||
10339 | case MVT::v4i32: | |||
10340 | if (Subtarget.hasAVX()) { | |||
10341 | Opcode = X86ISD::VPERMILPV; | |||
10342 | ShuffleVT = MVT::v4f32; | |||
10343 | } else if (Subtarget.hasSSSE3()) { | |||
10344 | Opcode = X86ISD::PSHUFB; | |||
10345 | ShuffleVT = MVT::v16i8; | |||
10346 | } | |||
10347 | break; | |||
10348 | case MVT::v2f64: | |||
10349 | case MVT::v2i64: | |||
10350 | if (Subtarget.hasAVX()) { | |||
10351 | // VPERMILPD selects using bit#1 of the index vector, so scale IndicesVec. | |||
10352 | IndicesVec = DAG.getNode(ISD::ADD, DL, IndicesVT, IndicesVec, IndicesVec); | |||
10353 | Opcode = X86ISD::VPERMILPV; | |||
10354 | ShuffleVT = MVT::v2f64; | |||
10355 | } else if (Subtarget.hasSSE41()) { | |||
10356 | // SSE41 can compare v2i64 - select between indices 0 and 1. | |||
10357 | return DAG.getSelectCC( | |||
10358 | DL, IndicesVec, | |||
10359 | getZeroVector(IndicesVT.getSimpleVT(), Subtarget, DAG, DL), | |||
10360 | DAG.getVectorShuffle(VT, DL, SrcVec, SrcVec, {0, 0}), | |||
10361 | DAG.getVectorShuffle(VT, DL, SrcVec, SrcVec, {1, 1}), | |||
10362 | ISD::CondCode::SETEQ); | |||
10363 | } | |||
10364 | break; | |||
10365 | case MVT::v32i8: | |||
10366 | if (Subtarget.hasVLX() && Subtarget.hasVBMI()) | |||
10367 | Opcode = X86ISD::VPERMV; | |||
10368 | else if (Subtarget.hasXOP()) { | |||
10369 | SDValue LoSrc = extract128BitVector(SrcVec, 0, DAG, DL); | |||
10370 | SDValue HiSrc = extract128BitVector(SrcVec, 16, DAG, DL); | |||
10371 | SDValue LoIdx = extract128BitVector(IndicesVec, 0, DAG, DL); | |||
10372 | SDValue HiIdx = extract128BitVector(IndicesVec, 16, DAG, DL); | |||
10373 | return DAG.getNode( | |||
10374 | ISD::CONCAT_VECTORS, DL, VT, | |||
10375 | DAG.getNode(X86ISD::VPPERM, DL, MVT::v16i8, LoSrc, HiSrc, LoIdx), | |||
10376 | DAG.getNode(X86ISD::VPPERM, DL, MVT::v16i8, LoSrc, HiSrc, HiIdx)); | |||
10377 | } else if (Subtarget.hasAVX()) { | |||
10378 | SDValue Lo = extract128BitVector(SrcVec, 0, DAG, DL); | |||
10379 | SDValue Hi = extract128BitVector(SrcVec, 16, DAG, DL); | |||
10380 | SDValue LoLo = DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, Lo, Lo); | |||
10381 | SDValue HiHi = DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, Hi, Hi); | |||
10382 | auto PSHUFBBuilder = [](SelectionDAG &DAG, const SDLoc &DL, | |||
10383 | ArrayRef<SDValue> Ops) { | |||
10384 | // Permute Lo and Hi and then select based on index range. | |||
10385 | // This works as SHUFB uses bits[3:0] to permute elements and we don't | |||
10386 | // care about the bit[7] as its just an index vector. | |||
10387 | SDValue Idx = Ops[2]; | |||
10388 | EVT VT = Idx.getValueType(); | |||
10389 | return DAG.getSelectCC(DL, Idx, DAG.getConstant(15, DL, VT), | |||
10390 | DAG.getNode(X86ISD::PSHUFB, DL, VT, Ops[1], Idx), | |||
10391 | DAG.getNode(X86ISD::PSHUFB, DL, VT, Ops[0], Idx), | |||
10392 | ISD::CondCode::SETGT); | |||
10393 | }; | |||
10394 | SDValue Ops[] = {LoLo, HiHi, IndicesVec}; | |||
10395 | return SplitOpsAndApply(DAG, Subtarget, DL, MVT::v32i8, Ops, | |||
10396 | PSHUFBBuilder); | |||
10397 | } | |||
10398 | break; | |||
10399 | case MVT::v16i16: | |||
10400 | if (Subtarget.hasVLX() && Subtarget.hasBWI()) | |||
10401 | Opcode = X86ISD::VPERMV; | |||
10402 | else if (Subtarget.hasAVX()) { | |||
10403 | // Scale to v32i8 and perform as v32i8. | |||
10404 | IndicesVec = ScaleIndices(IndicesVec, 2); | |||
10405 | return DAG.getBitcast( | |||
10406 | VT, createVariablePermute( | |||
10407 | MVT::v32i8, DAG.getBitcast(MVT::v32i8, SrcVec), | |||
10408 | DAG.getBitcast(MVT::v32i8, IndicesVec), DL, DAG, Subtarget)); | |||
10409 | } | |||
10410 | break; | |||
10411 | case MVT::v8f32: | |||
10412 | case MVT::v8i32: | |||
10413 | if (Subtarget.hasAVX2()) | |||
10414 | Opcode = X86ISD::VPERMV; | |||
10415 | else if (Subtarget.hasAVX()) { | |||
10416 | SrcVec = DAG.getBitcast(MVT::v8f32, SrcVec); | |||
10417 | SDValue LoLo = DAG.getVectorShuffle(MVT::v8f32, DL, SrcVec, SrcVec, | |||
10418 | {0, 1, 2, 3, 0, 1, 2, 3}); | |||
10419 | SDValue HiHi = DAG.getVectorShuffle(MVT::v8f32, DL, SrcVec, SrcVec, | |||
10420 | {4, 5, 6, 7, 4, 5, 6, 7}); | |||
10421 | if (Subtarget.hasXOP()) | |||
10422 | return DAG.getBitcast( | |||
10423 | VT, DAG.getNode(X86ISD::VPERMIL2, DL, MVT::v8f32, LoLo, HiHi, | |||
10424 | IndicesVec, DAG.getTargetConstant(0, DL, MVT::i8))); | |||
10425 | // Permute Lo and Hi and then select based on index range. | |||
10426 | // This works as VPERMILPS only uses index bits[0:1] to permute elements. | |||
10427 | SDValue Res = DAG.getSelectCC( | |||
10428 | DL, IndicesVec, DAG.getConstant(3, DL, MVT::v8i32), | |||
10429 | DAG.getNode(X86ISD::VPERMILPV, DL, MVT::v8f32, HiHi, IndicesVec), | |||
10430 | DAG.getNode(X86ISD::VPERMILPV, DL, MVT::v8f32, LoLo, IndicesVec), | |||
10431 | ISD::CondCode::SETGT); | |||
10432 | return DAG.getBitcast(VT, Res); | |||
10433 | } | |||
10434 | break; | |||
10435 | case MVT::v4i64: | |||
10436 | case MVT::v4f64: | |||
10437 | if (Subtarget.hasAVX512()) { | |||
10438 | if (!Subtarget.hasVLX()) { | |||
10439 | MVT WidenSrcVT = MVT::getVectorVT(VT.getScalarType(), 8); | |||
10440 | SrcVec = widenSubVector(WidenSrcVT, SrcVec, false, Subtarget, DAG, | |||
10441 | SDLoc(SrcVec)); | |||
10442 | IndicesVec = widenSubVector(MVT::v8i64, IndicesVec, false, Subtarget, | |||
10443 | DAG, SDLoc(IndicesVec)); | |||
10444 | SDValue Res = createVariablePermute(WidenSrcVT, SrcVec, IndicesVec, DL, | |||
10445 | DAG, Subtarget); | |||
10446 | return extract256BitVector(Res, 0, DAG, DL); | |||
10447 | } | |||
10448 | Opcode = X86ISD::VPERMV; | |||
10449 | } else if (Subtarget.hasAVX()) { | |||
10450 | SrcVec = DAG.getBitcast(MVT::v4f64, SrcVec); | |||
10451 | SDValue LoLo = | |||
10452 | DAG.getVectorShuffle(MVT::v4f64, DL, SrcVec, SrcVec, {0, 1, 0, 1}); | |||
10453 | SDValue HiHi = | |||
10454 | DAG.getVectorShuffle(MVT::v4f64, DL, SrcVec, SrcVec, {2, 3, 2, 3}); | |||
10455 | // VPERMIL2PD selects with bit#1 of the index vector, so scale IndicesVec. | |||
10456 | IndicesVec = DAG.getNode(ISD::ADD, DL, IndicesVT, IndicesVec, IndicesVec); | |||
10457 | if (Subtarget.hasXOP()) | |||
10458 | return DAG.getBitcast( | |||
10459 | VT, DAG.getNode(X86ISD::VPERMIL2, DL, MVT::v4f64, LoLo, HiHi, | |||
10460 | IndicesVec, DAG.getTargetConstant(0, DL, MVT::i8))); | |||
10461 | // Permute Lo and Hi and then select based on index range. | |||
10462 | // This works as VPERMILPD only uses index bit[1] to permute elements. | |||
10463 | SDValue Res = DAG.getSelectCC( | |||
10464 | DL, IndicesVec, DAG.getConstant(2, DL, MVT::v4i64), | |||
10465 | DAG.getNode(X86ISD::VPERMILPV, DL, MVT::v4f64, HiHi, IndicesVec), | |||
10466 | DAG.getNode(X86ISD::VPERMILPV, DL, MVT::v4f64, LoLo, IndicesVec), | |||
10467 | ISD::CondCode::SETGT); | |||
10468 | return DAG.getBitcast(VT, Res); | |||
10469 | } | |||
10470 | break; | |||
10471 | case MVT::v64i8: | |||
10472 | if (Subtarget.hasVBMI()) | |||
10473 | Opcode = X86ISD::VPERMV; | |||
10474 | break; | |||
10475 | case MVT::v32i16: | |||
10476 | if (Subtarget.hasBWI()) | |||
10477 | Opcode = X86ISD::VPERMV; | |||
10478 | break; | |||
10479 | case MVT::v16f32: | |||
10480 | case MVT::v16i32: | |||
10481 | case MVT::v8f64: | |||
10482 | case MVT::v8i64: | |||
10483 | if (Subtarget.hasAVX512()) | |||
10484 | Opcode = X86ISD::VPERMV; | |||
10485 | break; | |||
10486 | } | |||
10487 | if (!Opcode) | |||
10488 | return SDValue(); | |||
10489 | ||||
10490 | assert((VT.getSizeInBits() == ShuffleVT.getSizeInBits()) &&(static_cast <bool> ((VT.getSizeInBits() == ShuffleVT.getSizeInBits ()) && (VT.getScalarSizeInBits() % ShuffleVT.getScalarSizeInBits ()) == 0 && "Illegal variable permute shuffle type") ? void (0) : __assert_fail ("(VT.getSizeInBits() == ShuffleVT.getSizeInBits()) && (VT.getScalarSizeInBits() % ShuffleVT.getScalarSizeInBits()) == 0 && \"Illegal variable permute shuffle type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 10492, __extension__ __PRETTY_FUNCTION__)) | |||
10491 | (VT.getScalarSizeInBits() % ShuffleVT.getScalarSizeInBits()) == 0 &&(static_cast <bool> ((VT.getSizeInBits() == ShuffleVT.getSizeInBits ()) && (VT.getScalarSizeInBits() % ShuffleVT.getScalarSizeInBits ()) == 0 && "Illegal variable permute shuffle type") ? void (0) : __assert_fail ("(VT.getSizeInBits() == ShuffleVT.getSizeInBits()) && (VT.getScalarSizeInBits() % ShuffleVT.getScalarSizeInBits()) == 0 && \"Illegal variable permute shuffle type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 10492, __extension__ __PRETTY_FUNCTION__)) | |||
10492 | "Illegal variable permute shuffle type")(static_cast <bool> ((VT.getSizeInBits() == ShuffleVT.getSizeInBits ()) && (VT.getScalarSizeInBits() % ShuffleVT.getScalarSizeInBits ()) == 0 && "Illegal variable permute shuffle type") ? void (0) : __assert_fail ("(VT.getSizeInBits() == ShuffleVT.getSizeInBits()) && (VT.getScalarSizeInBits() % ShuffleVT.getScalarSizeInBits()) == 0 && \"Illegal variable permute shuffle type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 10492, __extension__ __PRETTY_FUNCTION__)); | |||
10493 | ||||
10494 | uint64_t Scale = VT.getScalarSizeInBits() / ShuffleVT.getScalarSizeInBits(); | |||
10495 | if (Scale > 1) | |||
10496 | IndicesVec = ScaleIndices(IndicesVec, Scale); | |||
10497 | ||||
10498 | EVT ShuffleIdxVT = EVT(ShuffleVT).changeVectorElementTypeToInteger(); | |||
10499 | IndicesVec = DAG.getBitcast(ShuffleIdxVT, IndicesVec); | |||
10500 | ||||
10501 | SrcVec = DAG.getBitcast(ShuffleVT, SrcVec); | |||
10502 | SDValue Res = Opcode == X86ISD::VPERMV | |||
10503 | ? DAG.getNode(Opcode, DL, ShuffleVT, IndicesVec, SrcVec) | |||
10504 | : DAG.getNode(Opcode, DL, ShuffleVT, SrcVec, IndicesVec); | |||
10505 | return DAG.getBitcast(VT, Res); | |||
10506 | } | |||
10507 | ||||
10508 | // Tries to lower a BUILD_VECTOR composed of extract-extract chains that can be | |||
10509 | // reasoned to be a permutation of a vector by indices in a non-constant vector. | |||
10510 | // (build_vector (extract_elt V, (extract_elt I, 0)), | |||
10511 | // (extract_elt V, (extract_elt I, 1)), | |||
10512 | // ... | |||
10513 | // -> | |||
10514 | // (vpermv I, V) | |||
10515 | // | |||
10516 | // TODO: Handle undefs | |||
10517 | // TODO: Utilize pshufb and zero mask blending to support more efficient | |||
10518 | // construction of vectors with constant-0 elements. | |||
10519 | static SDValue | |||
10520 | LowerBUILD_VECTORAsVariablePermute(SDValue V, SelectionDAG &DAG, | |||
10521 | const X86Subtarget &Subtarget) { | |||
10522 | SDValue SrcVec, IndicesVec; | |||
10523 | // Check for a match of the permute source vector and permute index elements. | |||
10524 | // This is done by checking that the i-th build_vector operand is of the form: | |||
10525 | // (extract_elt SrcVec, (extract_elt IndicesVec, i)). | |||
10526 | for (unsigned Idx = 0, E = V.getNumOperands(); Idx != E; ++Idx) { | |||
10527 | SDValue Op = V.getOperand(Idx); | |||
10528 | if (Op.getOpcode() != ISD::EXTRACT_VECTOR_ELT) | |||
10529 | return SDValue(); | |||
10530 | ||||
10531 | // If this is the first extract encountered in V, set the source vector, | |||
10532 | // otherwise verify the extract is from the previously defined source | |||
10533 | // vector. | |||
10534 | if (!SrcVec) | |||
10535 | SrcVec = Op.getOperand(0); | |||
10536 | else if (SrcVec != Op.getOperand(0)) | |||
10537 | return SDValue(); | |||
10538 | SDValue ExtractedIndex = Op->getOperand(1); | |||
10539 | // Peek through extends. | |||
10540 | if (ExtractedIndex.getOpcode() == ISD::ZERO_EXTEND || | |||
10541 | ExtractedIndex.getOpcode() == ISD::SIGN_EXTEND) | |||
10542 | ExtractedIndex = ExtractedIndex.getOperand(0); | |||
10543 | if (ExtractedIndex.getOpcode() != ISD::EXTRACT_VECTOR_ELT) | |||
10544 | return SDValue(); | |||
10545 | ||||
10546 | // If this is the first extract from the index vector candidate, set the | |||
10547 | // indices vector, otherwise verify the extract is from the previously | |||
10548 | // defined indices vector. | |||
10549 | if (!IndicesVec) | |||
10550 | IndicesVec = ExtractedIndex.getOperand(0); | |||
10551 | else if (IndicesVec != ExtractedIndex.getOperand(0)) | |||
10552 | return SDValue(); | |||
10553 | ||||
10554 | auto *PermIdx = dyn_cast<ConstantSDNode>(ExtractedIndex.getOperand(1)); | |||
10555 | if (!PermIdx || PermIdx->getAPIntValue() != Idx) | |||
10556 | return SDValue(); | |||
10557 | } | |||
10558 | ||||
10559 | SDLoc DL(V); | |||
10560 | MVT VT = V.getSimpleValueType(); | |||
10561 | return createVariablePermute(VT, SrcVec, IndicesVec, DL, DAG, Subtarget); | |||
10562 | } | |||
10563 | ||||
10564 | SDValue | |||
10565 | X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const { | |||
10566 | SDLoc dl(Op); | |||
10567 | ||||
10568 | MVT VT = Op.getSimpleValueType(); | |||
10569 | MVT EltVT = VT.getVectorElementType(); | |||
10570 | unsigned NumElems = Op.getNumOperands(); | |||
10571 | ||||
10572 | // Generate vectors for predicate vectors. | |||
10573 | if (VT.getVectorElementType() == MVT::i1 && Subtarget.hasAVX512()) | |||
10574 | return LowerBUILD_VECTORvXi1(Op, DAG, Subtarget); | |||
10575 | ||||
10576 | if (SDValue VectorConstant = materializeVectorConstant(Op, DAG, Subtarget)) | |||
10577 | return VectorConstant; | |||
10578 | ||||
10579 | unsigned EVTBits = EltVT.getSizeInBits(); | |||
10580 | APInt UndefMask = APInt::getZero(NumElems); | |||
10581 | APInt ZeroMask = APInt::getZero(NumElems); | |||
10582 | APInt NonZeroMask = APInt::getZero(NumElems); | |||
10583 | bool IsAllConstants = true; | |||
10584 | SmallSet<SDValue, 8> Values; | |||
10585 | unsigned NumConstants = NumElems; | |||
10586 | for (unsigned i = 0; i < NumElems; ++i) { | |||
10587 | SDValue Elt = Op.getOperand(i); | |||
10588 | if (Elt.isUndef()) { | |||
10589 | UndefMask.setBit(i); | |||
10590 | continue; | |||
10591 | } | |||
10592 | Values.insert(Elt); | |||
10593 | if (!isa<ConstantSDNode>(Elt) && !isa<ConstantFPSDNode>(Elt)) { | |||
10594 | IsAllConstants = false; | |||
10595 | NumConstants--; | |||
10596 | } | |||
10597 | if (X86::isZeroNode(Elt)) { | |||
10598 | ZeroMask.setBit(i); | |||
10599 | } else { | |||
10600 | NonZeroMask.setBit(i); | |||
10601 | } | |||
10602 | } | |||
10603 | ||||
10604 | // All undef vector. Return an UNDEF. All zero vectors were handled above. | |||
10605 | if (NonZeroMask == 0) { | |||
10606 | assert(UndefMask.isAllOnes() && "Fully undef mask expected")(static_cast <bool> (UndefMask.isAllOnes() && "Fully undef mask expected" ) ? void (0) : __assert_fail ("UndefMask.isAllOnes() && \"Fully undef mask expected\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 10606, __extension__ __PRETTY_FUNCTION__)); | |||
10607 | return DAG.getUNDEF(VT); | |||
10608 | } | |||
10609 | ||||
10610 | BuildVectorSDNode *BV = cast<BuildVectorSDNode>(Op.getNode()); | |||
10611 | ||||
10612 | // If the upper elts of a ymm/zmm are undef/zero then we might be better off | |||
10613 | // lowering to a smaller build vector and padding with undef/zero. | |||
10614 | if ((VT.is256BitVector() || VT.is512BitVector()) && | |||
10615 | !isFoldableUseOfShuffle(BV)) { | |||
10616 | unsigned UpperElems = NumElems / 2; | |||
10617 | APInt UndefOrZeroMask = UndefMask | ZeroMask; | |||
10618 | unsigned NumUpperUndefsOrZeros = UndefOrZeroMask.countLeadingOnes(); | |||
10619 | if (NumUpperUndefsOrZeros >= UpperElems) { | |||
10620 | if (VT.is512BitVector() && | |||
10621 | NumUpperUndefsOrZeros >= (NumElems - (NumElems / 4))) | |||
10622 | UpperElems = NumElems - (NumElems / 4); | |||
10623 | bool UndefUpper = UndefMask.countLeadingOnes() >= UpperElems; | |||
10624 | MVT LowerVT = MVT::getVectorVT(EltVT, NumElems - UpperElems); | |||
10625 | SDValue NewBV = | |||
10626 | DAG.getBuildVector(LowerVT, dl, Op->ops().drop_back(UpperElems)); | |||
10627 | return widenSubVector(VT, NewBV, !UndefUpper, Subtarget, DAG, dl); | |||
10628 | } | |||
10629 | } | |||
10630 | ||||
10631 | if (SDValue AddSub = lowerToAddSubOrFMAddSub(BV, Subtarget, DAG)) | |||
10632 | return AddSub; | |||
10633 | if (SDValue HorizontalOp = LowerToHorizontalOp(BV, Subtarget, DAG)) | |||
10634 | return HorizontalOp; | |||
10635 | if (SDValue Broadcast = lowerBuildVectorAsBroadcast(BV, Subtarget, DAG)) | |||
10636 | return Broadcast; | |||
10637 | if (SDValue BitOp = lowerBuildVectorToBitOp(BV, Subtarget, DAG)) | |||
10638 | return BitOp; | |||
10639 | ||||
10640 | unsigned NumZero = ZeroMask.countPopulation(); | |||
10641 | unsigned NumNonZero = NonZeroMask.countPopulation(); | |||
10642 | ||||
10643 | // If we are inserting one variable into a vector of non-zero constants, try | |||
10644 | // to avoid loading each constant element as a scalar. Load the constants as a | |||
10645 | // vector and then insert the variable scalar element. If insertion is not | |||
10646 | // supported, fall back to a shuffle to get the scalar blended with the | |||
10647 | // constants. Insertion into a zero vector is handled as a special-case | |||
10648 | // somewhere below here. | |||
10649 | if (NumConstants == NumElems - 1 && NumNonZero != 1 && | |||
10650 | (isOperationLegalOrCustom(ISD::INSERT_VECTOR_ELT, VT) || | |||
10651 | isOperationLegalOrCustom(ISD::VECTOR_SHUFFLE, VT))) { | |||
10652 | // Create an all-constant vector. The variable element in the old | |||
10653 | // build vector is replaced by undef in the constant vector. Save the | |||
10654 | // variable scalar element and its index for use in the insertelement. | |||
10655 | LLVMContext &Context = *DAG.getContext(); | |||
10656 | Type *EltType = Op.getValueType().getScalarType().getTypeForEVT(Context); | |||
10657 | SmallVector<Constant *, 16> ConstVecOps(NumElems, UndefValue::get(EltType)); | |||
10658 | SDValue VarElt; | |||
10659 | SDValue InsIndex; | |||
10660 | for (unsigned i = 0; i != NumElems; ++i) { | |||
10661 | SDValue Elt = Op.getOperand(i); | |||
10662 | if (auto *C = dyn_cast<ConstantSDNode>(Elt)) | |||
10663 | ConstVecOps[i] = ConstantInt::get(Context, C->getAPIntValue()); | |||
10664 | else if (auto *C = dyn_cast<ConstantFPSDNode>(Elt)) | |||
10665 | ConstVecOps[i] = ConstantFP::get(Context, C->getValueAPF()); | |||
10666 | else if (!Elt.isUndef()) { | |||
10667 | assert(!VarElt.getNode() && !InsIndex.getNode() &&(static_cast <bool> (!VarElt.getNode() && !InsIndex .getNode() && "Expected one variable element in this vector" ) ? void (0) : __assert_fail ("!VarElt.getNode() && !InsIndex.getNode() && \"Expected one variable element in this vector\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 10668, __extension__ __PRETTY_FUNCTION__)) | |||
10668 | "Expected one variable element in this vector")(static_cast <bool> (!VarElt.getNode() && !InsIndex .getNode() && "Expected one variable element in this vector" ) ? void (0) : __assert_fail ("!VarElt.getNode() && !InsIndex.getNode() && \"Expected one variable element in this vector\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 10668, __extension__ __PRETTY_FUNCTION__)); | |||
10669 | VarElt = Elt; | |||
10670 | InsIndex = DAG.getVectorIdxConstant(i, dl); | |||
10671 | } | |||
10672 | } | |||
10673 | Constant *CV = ConstantVector::get(ConstVecOps); | |||
10674 | SDValue DAGConstVec = DAG.getConstantPool(CV, VT); | |||
10675 | ||||
10676 | // The constants we just created may not be legal (eg, floating point). We | |||
10677 | // must lower the vector right here because we can not guarantee that we'll | |||
10678 | // legalize it before loading it. This is also why we could not just create | |||
10679 | // a new build vector here. If the build vector contains illegal constants, | |||
10680 | // it could get split back up into a series of insert elements. | |||
10681 | // TODO: Improve this by using shorter loads with broadcast/VZEXT_LOAD. | |||
10682 | SDValue LegalDAGConstVec = LowerConstantPool(DAGConstVec, DAG); | |||
10683 | MachineFunction &MF = DAG.getMachineFunction(); | |||
10684 | MachinePointerInfo MPI = MachinePointerInfo::getConstantPool(MF); | |||
10685 | SDValue Ld = DAG.getLoad(VT, dl, DAG.getEntryNode(), LegalDAGConstVec, MPI); | |||
10686 | unsigned InsertC = cast<ConstantSDNode>(InsIndex)->getZExtValue(); | |||
10687 | unsigned NumEltsInLow128Bits = 128 / VT.getScalarSizeInBits(); | |||
10688 | if (InsertC < NumEltsInLow128Bits) | |||
10689 | return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Ld, VarElt, InsIndex); | |||
10690 | ||||
10691 | // There's no good way to insert into the high elements of a >128-bit | |||
10692 | // vector, so use shuffles to avoid an extract/insert sequence. | |||
10693 | assert(VT.getSizeInBits() > 128 && "Invalid insertion index?")(static_cast <bool> (VT.getSizeInBits() > 128 && "Invalid insertion index?") ? void (0) : __assert_fail ("VT.getSizeInBits() > 128 && \"Invalid insertion index?\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 10693, __extension__ __PRETTY_FUNCTION__)); | |||
10694 | assert(Subtarget.hasAVX() && "Must have AVX with >16-byte vector")(static_cast <bool> (Subtarget.hasAVX() && "Must have AVX with >16-byte vector" ) ? void (0) : __assert_fail ("Subtarget.hasAVX() && \"Must have AVX with >16-byte vector\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 10694, __extension__ __PRETTY_FUNCTION__)); | |||
10695 | SmallVector<int, 8> ShuffleMask; | |||
10696 | unsigned NumElts = VT.getVectorNumElements(); | |||
10697 | for (unsigned i = 0; i != NumElts; ++i) | |||
10698 | ShuffleMask.push_back(i == InsertC ? NumElts : i); | |||
10699 | SDValue S2V = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, VarElt); | |||
10700 | return DAG.getVectorShuffle(VT, dl, Ld, S2V, ShuffleMask); | |||
10701 | } | |||
10702 | ||||
10703 | // Special case for single non-zero, non-undef, element. | |||
10704 | if (NumNonZero == 1) { | |||
10705 | unsigned Idx = NonZeroMask.countTrailingZeros(); | |||
10706 | SDValue Item = Op.getOperand(Idx); | |||
10707 | ||||
10708 | // If we have a constant or non-constant insertion into the low element of | |||
10709 | // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into | |||
10710 | // the rest of the elements. This will be matched as movd/movq/movss/movsd | |||
10711 | // depending on what the source datatype is. | |||
10712 | if (Idx == 0) { | |||
10713 | if (NumZero == 0) | |||
10714 | return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item); | |||
10715 | ||||
10716 | if (EltVT == MVT::i32 || EltVT == MVT::f16 || EltVT == MVT::f32 || | |||
10717 | EltVT == MVT::f64 || (EltVT == MVT::i64 && Subtarget.is64Bit()) || | |||
10718 | (EltVT == MVT::i16 && Subtarget.hasFP16())) { | |||
10719 | assert((VT.is128BitVector() || VT.is256BitVector() ||(static_cast <bool> ((VT.is128BitVector() || VT.is256BitVector () || VT.is512BitVector()) && "Expected an SSE value type!" ) ? void (0) : __assert_fail ("(VT.is128BitVector() || VT.is256BitVector() || VT.is512BitVector()) && \"Expected an SSE value type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 10721, __extension__ __PRETTY_FUNCTION__)) | |||
10720 | VT.is512BitVector()) &&(static_cast <bool> ((VT.is128BitVector() || VT.is256BitVector () || VT.is512BitVector()) && "Expected an SSE value type!" ) ? void (0) : __assert_fail ("(VT.is128BitVector() || VT.is256BitVector() || VT.is512BitVector()) && \"Expected an SSE value type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 10721, __extension__ __PRETTY_FUNCTION__)) | |||
10721 | "Expected an SSE value type!")(static_cast <bool> ((VT.is128BitVector() || VT.is256BitVector () || VT.is512BitVector()) && "Expected an SSE value type!" ) ? void (0) : __assert_fail ("(VT.is128BitVector() || VT.is256BitVector() || VT.is512BitVector()) && \"Expected an SSE value type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 10721, __extension__ __PRETTY_FUNCTION__)); | |||
10722 | Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item); | |||
10723 | // Turn it into a MOVL (i.e. movsh, movss, movsd, movw or movd) to a | |||
10724 | // zero vector. | |||
10725 | return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG); | |||
10726 | } | |||
10727 | ||||
10728 | // We can't directly insert an i8 or i16 into a vector, so zero extend | |||
10729 | // it to i32 first. | |||
10730 | if (EltVT == MVT::i16 || EltVT == MVT::i8) { | |||
10731 | Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item); | |||
10732 | MVT ShufVT = MVT::getVectorVT(MVT::i32, VT.getSizeInBits() / 32); | |||
10733 | Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, ShufVT, Item); | |||
10734 | Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG); | |||
10735 | return DAG.getBitcast(VT, Item); | |||
10736 | } | |||
10737 | } | |||
10738 | ||||
10739 | // Is it a vector logical left shift? | |||
10740 | if (NumElems == 2 && Idx == 1 && | |||
10741 | X86::isZeroNode(Op.getOperand(0)) && | |||
10742 | !X86::isZeroNode(Op.getOperand(1))) { | |||
10743 | unsigned NumBits = VT.getSizeInBits(); | |||
10744 | return getVShift(true, VT, | |||
10745 | DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, | |||
10746 | VT, Op.getOperand(1)), | |||
10747 | NumBits/2, DAG, *this, dl); | |||
10748 | } | |||
10749 | ||||
10750 | if (IsAllConstants) // Otherwise, it's better to do a constpool load. | |||
10751 | return SDValue(); | |||
10752 | ||||
10753 | // Otherwise, if this is a vector with i32 or f32 elements, and the element | |||
10754 | // is a non-constant being inserted into an element other than the low one, | |||
10755 | // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka | |||
10756 | // movd/movss) to move this into the low element, then shuffle it into | |||
10757 | // place. | |||
10758 | if (EVTBits == 32) { | |||
10759 | Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item); | |||
10760 | return getShuffleVectorZeroOrUndef(Item, Idx, NumZero > 0, Subtarget, DAG); | |||
10761 | } | |||
10762 | } | |||
10763 | ||||
10764 | // Splat is obviously ok. Let legalizer expand it to a shuffle. | |||
10765 | if (Values.size() == 1) { | |||
10766 | if (EVTBits == 32) { | |||
10767 | // Instead of a shuffle like this: | |||
10768 | // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0> | |||
10769 | // Check if it's possible to issue this instead. | |||
10770 | // shuffle (vload ptr)), undef, <1, 1, 1, 1> | |||
10771 | unsigned Idx = NonZeroMask.countTrailingZeros(); | |||
10772 | SDValue Item = Op.getOperand(Idx); | |||
10773 | if (Op.getNode()->isOnlyUserOf(Item.getNode())) | |||
10774 | return LowerAsSplatVectorLoad(Item, VT, dl, DAG); | |||
10775 | } | |||
10776 | return SDValue(); | |||
10777 | } | |||
10778 | ||||
10779 | // A vector full of immediates; various special cases are already | |||
10780 | // handled, so this is best done with a single constant-pool load. | |||
10781 | if (IsAllConstants) | |||
10782 | return SDValue(); | |||
10783 | ||||
10784 | if (SDValue V = LowerBUILD_VECTORAsVariablePermute(Op, DAG, Subtarget)) | |||
10785 | return V; | |||
10786 | ||||
10787 | // See if we can use a vector load to get all of the elements. | |||
10788 | { | |||
10789 | SmallVector<SDValue, 64> Ops(Op->op_begin(), Op->op_begin() + NumElems); | |||
10790 | if (SDValue LD = | |||
10791 | EltsFromConsecutiveLoads(VT, Ops, dl, DAG, Subtarget, false)) | |||
10792 | return LD; | |||
10793 | } | |||
10794 | ||||
10795 | // If this is a splat of pairs of 32-bit elements, we can use a narrower | |||
10796 | // build_vector and broadcast it. | |||
10797 | // TODO: We could probably generalize this more. | |||
10798 | if (Subtarget.hasAVX2() && EVTBits == 32 && Values.size() == 2) { | |||
10799 | SDValue Ops[4] = { Op.getOperand(0), Op.getOperand(1), | |||
10800 | DAG.getUNDEF(EltVT), DAG.getUNDEF(EltVT) }; | |||
10801 | auto CanSplat = [](SDValue Op, unsigned NumElems, ArrayRef<SDValue> Ops) { | |||
10802 | // Make sure all the even/odd operands match. | |||
10803 | for (unsigned i = 2; i != NumElems; ++i) | |||
10804 | if (Ops[i % 2] != Op.getOperand(i)) | |||
10805 | return false; | |||
10806 | return true; | |||
10807 | }; | |||
10808 | if (CanSplat(Op, NumElems, Ops)) { | |||
10809 | MVT WideEltVT = VT.isFloatingPoint() ? MVT::f64 : MVT::i64; | |||
10810 | MVT NarrowVT = MVT::getVectorVT(EltVT, 4); | |||
10811 | // Create a new build vector and cast to v2i64/v2f64. | |||
10812 | SDValue NewBV = DAG.getBitcast(MVT::getVectorVT(WideEltVT, 2), | |||
10813 | DAG.getBuildVector(NarrowVT, dl, Ops)); | |||
10814 | // Broadcast from v2i64/v2f64 and cast to final VT. | |||
10815 | MVT BcastVT = MVT::getVectorVT(WideEltVT, NumElems / 2); | |||
10816 | return DAG.getBitcast(VT, DAG.getNode(X86ISD::VBROADCAST, dl, BcastVT, | |||
10817 | NewBV)); | |||
10818 | } | |||
10819 | } | |||
10820 | ||||
10821 | // For AVX-length vectors, build the individual 128-bit pieces and use | |||
10822 | // shuffles to put them in place. | |||
10823 | if (VT.getSizeInBits() > 128) { | |||
10824 | MVT HVT = MVT::getVectorVT(EltVT, NumElems / 2); | |||
10825 | ||||
10826 | // Build both the lower and upper subvector. | |||
10827 | SDValue Lower = | |||
10828 | DAG.getBuildVector(HVT, dl, Op->ops().slice(0, NumElems / 2)); | |||
10829 | SDValue Upper = DAG.getBuildVector( | |||
10830 | HVT, dl, Op->ops().slice(NumElems / 2, NumElems /2)); | |||
10831 | ||||
10832 | // Recreate the wider vector with the lower and upper part. | |||
10833 | return concatSubVectors(Lower, Upper, DAG, dl); | |||
10834 | } | |||
10835 | ||||
10836 | // Let legalizer expand 2-wide build_vectors. | |||
10837 | if (EVTBits == 64) { | |||
10838 | if (NumNonZero == 1) { | |||
10839 | // One half is zero or undef. | |||
10840 | unsigned Idx = NonZeroMask.countTrailingZeros(); | |||
10841 | SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, | |||
10842 | Op.getOperand(Idx)); | |||
10843 | return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG); | |||
10844 | } | |||
10845 | return SDValue(); | |||
10846 | } | |||
10847 | ||||
10848 | // If element VT is < 32 bits, convert it to inserts into a zero vector. | |||
10849 | if (EVTBits == 8 && NumElems == 16) | |||
10850 | if (SDValue V = LowerBuildVectorv16i8(Op, NonZeroMask, NumNonZero, NumZero, | |||
10851 | DAG, Subtarget)) | |||
10852 | return V; | |||
10853 | ||||
10854 | if (EltVT == MVT::i16 && NumElems == 8) | |||
10855 | if (SDValue V = LowerBuildVectorv8i16(Op, NonZeroMask, NumNonZero, NumZero, | |||
10856 | DAG, Subtarget)) | |||
10857 | return V; | |||
10858 | ||||
10859 | // If element VT is == 32 bits and has 4 elems, try to generate an INSERTPS | |||
10860 | if (EVTBits == 32 && NumElems == 4) | |||
10861 | if (SDValue V = LowerBuildVectorv4x32(Op, DAG, Subtarget)) | |||
10862 | return V; | |||
10863 | ||||
10864 | // If element VT is == 32 bits, turn it into a number of shuffles. | |||
10865 | if (NumElems == 4 && NumZero > 0) { | |||
10866 | SmallVector<SDValue, 8> Ops(NumElems); | |||
10867 | for (unsigned i = 0; i < 4; ++i) { | |||
10868 | bool isZero = !NonZeroMask[i]; | |||
10869 | if (isZero) | |||
10870 | Ops[i] = getZeroVector(VT, Subtarget, DAG, dl); | |||
10871 | else | |||
10872 | Ops[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i)); | |||
10873 | } | |||
10874 | ||||
10875 | for (unsigned i = 0; i < 2; ++i) { | |||
10876 | switch (NonZeroMask.extractBitsAsZExtValue(2, i * 2)) { | |||
10877 | default: llvm_unreachable("Unexpected NonZero count")::llvm::llvm_unreachable_internal("Unexpected NonZero count", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 10877); | |||
10878 | case 0: | |||
10879 | Ops[i] = Ops[i*2]; // Must be a zero vector. | |||
10880 | break; | |||
10881 | case 1: | |||
10882 | Ops[i] = getMOVL(DAG, dl, VT, Ops[i*2+1], Ops[i*2]); | |||
10883 | break; | |||
10884 | case 2: | |||
10885 | Ops[i] = getMOVL(DAG, dl, VT, Ops[i*2], Ops[i*2+1]); | |||
10886 | break; | |||
10887 | case 3: | |||
10888 | Ops[i] = getUnpackl(DAG, dl, VT, Ops[i*2], Ops[i*2+1]); | |||
10889 | break; | |||
10890 | } | |||
10891 | } | |||
10892 | ||||
10893 | bool Reverse1 = NonZeroMask.extractBitsAsZExtValue(2, 0) == 2; | |||
10894 | bool Reverse2 = NonZeroMask.extractBitsAsZExtValue(2, 2) == 2; | |||
10895 | int MaskVec[] = { | |||
10896 | Reverse1 ? 1 : 0, | |||
10897 | Reverse1 ? 0 : 1, | |||
10898 | static_cast<int>(Reverse2 ? NumElems+1 : NumElems), | |||
10899 | static_cast<int>(Reverse2 ? NumElems : NumElems+1) | |||
10900 | }; | |||
10901 | return DAG.getVectorShuffle(VT, dl, Ops[0], Ops[1], MaskVec); | |||
10902 | } | |||
10903 | ||||
10904 | assert(Values.size() > 1 && "Expected non-undef and non-splat vector")(static_cast <bool> (Values.size() > 1 && "Expected non-undef and non-splat vector" ) ? void (0) : __assert_fail ("Values.size() > 1 && \"Expected non-undef and non-splat vector\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 10904, __extension__ __PRETTY_FUNCTION__)); | |||
10905 | ||||
10906 | // Check for a build vector from mostly shuffle plus few inserting. | |||
10907 | if (SDValue Sh = buildFromShuffleMostly(Op, DAG)) | |||
10908 | return Sh; | |||
10909 | ||||
10910 | // For SSE 4.1, use insertps to put the high elements into the low element. | |||
10911 | if (Subtarget.hasSSE41() && EltVT != MVT::f16) { | |||
10912 | SDValue Result; | |||
10913 | if (!Op.getOperand(0).isUndef()) | |||
10914 | Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0)); | |||
10915 | else | |||
10916 | Result = DAG.getUNDEF(VT); | |||
10917 | ||||
10918 | for (unsigned i = 1; i < NumElems; ++i) { | |||
10919 | if (Op.getOperand(i).isUndef()) continue; | |||
10920 | Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result, | |||
10921 | Op.getOperand(i), DAG.getIntPtrConstant(i, dl)); | |||
10922 | } | |||
10923 | return Result; | |||
10924 | } | |||
10925 | ||||
10926 | // Otherwise, expand into a number of unpckl*, start by extending each of | |||
10927 | // our (non-undef) elements to the full vector width with the element in the | |||
10928 | // bottom slot of the vector (which generates no code for SSE). | |||
10929 | SmallVector<SDValue, 8> Ops(NumElems); | |||
10930 | for (unsigned i = 0; i < NumElems; ++i) { | |||
10931 | if (!Op.getOperand(i).isUndef()) | |||
10932 | Ops[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i)); | |||
10933 | else | |||
10934 | Ops[i] = DAG.getUNDEF(VT); | |||
10935 | } | |||
10936 | ||||
10937 | // Next, we iteratively mix elements, e.g. for v4f32: | |||
10938 | // Step 1: unpcklps 0, 1 ==> X: <?, ?, 1, 0> | |||
10939 | // : unpcklps 2, 3 ==> Y: <?, ?, 3, 2> | |||
10940 | // Step 2: unpcklpd X, Y ==> <3, 2, 1, 0> | |||
10941 | for (unsigned Scale = 1; Scale < NumElems; Scale *= 2) { | |||
10942 | // Generate scaled UNPCKL shuffle mask. | |||
10943 | SmallVector<int, 16> Mask; | |||
10944 | for(unsigned i = 0; i != Scale; ++i) | |||
10945 | Mask.push_back(i); | |||
10946 | for (unsigned i = 0; i != Scale; ++i) | |||
10947 | Mask.push_back(NumElems+i); | |||
10948 | Mask.append(NumElems - Mask.size(), SM_SentinelUndef); | |||
10949 | ||||
10950 | for (unsigned i = 0, e = NumElems / (2 * Scale); i != e; ++i) | |||
10951 | Ops[i] = DAG.getVectorShuffle(VT, dl, Ops[2*i], Ops[(2*i)+1], Mask); | |||
10952 | } | |||
10953 | return Ops[0]; | |||
10954 | } | |||
10955 | ||||
10956 | // 256-bit AVX can use the vinsertf128 instruction | |||
10957 | // to create 256-bit vectors from two other 128-bit ones. | |||
10958 | // TODO: Detect subvector broadcast here instead of DAG combine? | |||
10959 | static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG, | |||
10960 | const X86Subtarget &Subtarget) { | |||
10961 | SDLoc dl(Op); | |||
10962 | MVT ResVT = Op.getSimpleValueType(); | |||
10963 | ||||
10964 | assert((ResVT.is256BitVector() ||(static_cast <bool> ((ResVT.is256BitVector() || ResVT.is512BitVector ()) && "Value type must be 256-/512-bit wide") ? void (0) : __assert_fail ("(ResVT.is256BitVector() || ResVT.is512BitVector()) && \"Value type must be 256-/512-bit wide\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 10965, __extension__ __PRETTY_FUNCTION__)) | |||
10965 | ResVT.is512BitVector()) && "Value type must be 256-/512-bit wide")(static_cast <bool> ((ResVT.is256BitVector() || ResVT.is512BitVector ()) && "Value type must be 256-/512-bit wide") ? void (0) : __assert_fail ("(ResVT.is256BitVector() || ResVT.is512BitVector()) && \"Value type must be 256-/512-bit wide\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 10965, __extension__ __PRETTY_FUNCTION__)); | |||
10966 | ||||
10967 | unsigned NumOperands = Op.getNumOperands(); | |||
10968 | unsigned NumZero = 0; | |||
10969 | unsigned NumNonZero = 0; | |||
10970 | unsigned NonZeros = 0; | |||
10971 | for (unsigned i = 0; i != NumOperands; ++i) { | |||
10972 | SDValue SubVec = Op.getOperand(i); | |||
10973 | if (SubVec.isUndef()) | |||
10974 | continue; | |||
10975 | if (ISD::isBuildVectorAllZeros(SubVec.getNode())) | |||
10976 | ++NumZero; | |||
10977 | else { | |||
10978 | assert(i < sizeof(NonZeros) * CHAR_BIT)(static_cast <bool> (i < sizeof(NonZeros) * 8) ? void (0) : __assert_fail ("i < sizeof(NonZeros) * CHAR_BIT", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 10978, __extension__ __PRETTY_FUNCTION__)); // Ensure the shift is in range. | |||
10979 | NonZeros |= 1 << i; | |||
10980 | ++NumNonZero; | |||
10981 | } | |||
10982 | } | |||
10983 | ||||
10984 | // If we have more than 2 non-zeros, build each half separately. | |||
10985 | if (NumNonZero > 2) { | |||
10986 | MVT HalfVT = ResVT.getHalfNumVectorElementsVT(); | |||
10987 | ArrayRef<SDUse> Ops = Op->ops(); | |||
10988 | SDValue Lo = DAG.getNode(ISD::CONCAT_VECTORS, dl, HalfVT, | |||
10989 | Ops.slice(0, NumOperands/2)); | |||
10990 | SDValue Hi = DAG.getNode(ISD::CONCAT_VECTORS, dl, HalfVT, | |||
10991 | Ops.slice(NumOperands/2)); | |||
10992 | return DAG.getNode(ISD::CONCAT_VECTORS, dl, ResVT, Lo, Hi); | |||
10993 | } | |||
10994 | ||||
10995 | // Otherwise, build it up through insert_subvectors. | |||
10996 | SDValue Vec = NumZero ? getZeroVector(ResVT, Subtarget, DAG, dl) | |||
10997 | : DAG.getUNDEF(ResVT); | |||
10998 | ||||
10999 | MVT SubVT = Op.getOperand(0).getSimpleValueType(); | |||
11000 | unsigned NumSubElems = SubVT.getVectorNumElements(); | |||
11001 | for (unsigned i = 0; i != NumOperands; ++i) { | |||
11002 | if ((NonZeros & (1 << i)) == 0) | |||
11003 | continue; | |||
11004 | ||||
11005 | Vec = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResVT, Vec, | |||
11006 | Op.getOperand(i), | |||
11007 | DAG.getIntPtrConstant(i * NumSubElems, dl)); | |||
11008 | } | |||
11009 | ||||
11010 | return Vec; | |||
11011 | } | |||
11012 | ||||
11013 | // Returns true if the given node is a type promotion (by concatenating i1 | |||
11014 | // zeros) of the result of a node that already zeros all upper bits of | |||
11015 | // k-register. | |||
11016 | // TODO: Merge this with LowerAVXCONCAT_VECTORS? | |||
11017 | static SDValue LowerCONCAT_VECTORSvXi1(SDValue Op, | |||
11018 | const X86Subtarget &Subtarget, | |||
11019 | SelectionDAG & DAG) { | |||
11020 | SDLoc dl(Op); | |||
11021 | MVT ResVT = Op.getSimpleValueType(); | |||
11022 | unsigned NumOperands = Op.getNumOperands(); | |||
11023 | ||||
11024 | assert(NumOperands > 1 && isPowerOf2_32(NumOperands) &&(static_cast <bool> (NumOperands > 1 && isPowerOf2_32 (NumOperands) && "Unexpected number of operands in CONCAT_VECTORS" ) ? void (0) : __assert_fail ("NumOperands > 1 && isPowerOf2_32(NumOperands) && \"Unexpected number of operands in CONCAT_VECTORS\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11025, __extension__ __PRETTY_FUNCTION__)) | |||
11025 | "Unexpected number of operands in CONCAT_VECTORS")(static_cast <bool> (NumOperands > 1 && isPowerOf2_32 (NumOperands) && "Unexpected number of operands in CONCAT_VECTORS" ) ? void (0) : __assert_fail ("NumOperands > 1 && isPowerOf2_32(NumOperands) && \"Unexpected number of operands in CONCAT_VECTORS\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11025, __extension__ __PRETTY_FUNCTION__)); | |||
11026 | ||||
11027 | uint64_t Zeros = 0; | |||
11028 | uint64_t NonZeros = 0; | |||
11029 | for (unsigned i = 0; i != NumOperands; ++i) { | |||
11030 | SDValue SubVec = Op.getOperand(i); | |||
11031 | if (SubVec.isUndef()) | |||
11032 | continue; | |||
11033 | assert(i < sizeof(NonZeros) * CHAR_BIT)(static_cast <bool> (i < sizeof(NonZeros) * 8) ? void (0) : __assert_fail ("i < sizeof(NonZeros) * CHAR_BIT", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11033, __extension__ __PRETTY_FUNCTION__)); // Ensure the shift is in range. | |||
11034 | if (ISD::isBuildVectorAllZeros(SubVec.getNode())) | |||
11035 | Zeros |= (uint64_t)1 << i; | |||
11036 | else | |||
11037 | NonZeros |= (uint64_t)1 << i; | |||
11038 | } | |||
11039 | ||||
11040 | unsigned NumElems = ResVT.getVectorNumElements(); | |||
11041 | ||||
11042 | // If we are inserting non-zero vector and there are zeros in LSBs and undef | |||
11043 | // in the MSBs we need to emit a KSHIFTL. The generic lowering to | |||
11044 | // insert_subvector will give us two kshifts. | |||
11045 | if (isPowerOf2_64(NonZeros) && Zeros != 0 && NonZeros > Zeros && | |||
11046 | Log2_64(NonZeros) != NumOperands - 1) { | |||
11047 | MVT ShiftVT = ResVT; | |||
11048 | if ((!Subtarget.hasDQI() && NumElems == 8) || NumElems < 8) | |||
11049 | ShiftVT = Subtarget.hasDQI() ? MVT::v8i1 : MVT::v16i1; | |||
11050 | unsigned Idx = Log2_64(NonZeros); | |||
11051 | SDValue SubVec = Op.getOperand(Idx); | |||
11052 | unsigned SubVecNumElts = SubVec.getSimpleValueType().getVectorNumElements(); | |||
11053 | SubVec = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ShiftVT, | |||
11054 | DAG.getUNDEF(ShiftVT), SubVec, | |||
11055 | DAG.getIntPtrConstant(0, dl)); | |||
11056 | Op = DAG.getNode(X86ISD::KSHIFTL, dl, ShiftVT, SubVec, | |||
11057 | DAG.getTargetConstant(Idx * SubVecNumElts, dl, MVT::i8)); | |||
11058 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResVT, Op, | |||
11059 | DAG.getIntPtrConstant(0, dl)); | |||
11060 | } | |||
11061 | ||||
11062 | // If there are zero or one non-zeros we can handle this very simply. | |||
11063 | if (NonZeros == 0 || isPowerOf2_64(NonZeros)) { | |||
11064 | SDValue Vec = Zeros ? DAG.getConstant(0, dl, ResVT) : DAG.getUNDEF(ResVT); | |||
11065 | if (!NonZeros) | |||
11066 | return Vec; | |||
11067 | unsigned Idx = Log2_64(NonZeros); | |||
11068 | SDValue SubVec = Op.getOperand(Idx); | |||
11069 | unsigned SubVecNumElts = SubVec.getSimpleValueType().getVectorNumElements(); | |||
11070 | return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResVT, Vec, SubVec, | |||
11071 | DAG.getIntPtrConstant(Idx * SubVecNumElts, dl)); | |||
11072 | } | |||
11073 | ||||
11074 | if (NumOperands > 2) { | |||
11075 | MVT HalfVT = ResVT.getHalfNumVectorElementsVT(); | |||
11076 | ArrayRef<SDUse> Ops = Op->ops(); | |||
11077 | SDValue Lo = DAG.getNode(ISD::CONCAT_VECTORS, dl, HalfVT, | |||
11078 | Ops.slice(0, NumOperands/2)); | |||
11079 | SDValue Hi = DAG.getNode(ISD::CONCAT_VECTORS, dl, HalfVT, | |||
11080 | Ops.slice(NumOperands/2)); | |||
11081 | return DAG.getNode(ISD::CONCAT_VECTORS, dl, ResVT, Lo, Hi); | |||
11082 | } | |||
11083 | ||||
11084 | assert(countPopulation(NonZeros) == 2 && "Simple cases not handled?")(static_cast <bool> (countPopulation(NonZeros) == 2 && "Simple cases not handled?") ? void (0) : __assert_fail ("countPopulation(NonZeros) == 2 && \"Simple cases not handled?\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11084, __extension__ __PRETTY_FUNCTION__)); | |||
11085 | ||||
11086 | if (ResVT.getVectorNumElements() >= 16) | |||
11087 | return Op; // The operation is legal with KUNPCK | |||
11088 | ||||
11089 | SDValue Vec = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResVT, | |||
11090 | DAG.getUNDEF(ResVT), Op.getOperand(0), | |||
11091 | DAG.getIntPtrConstant(0, dl)); | |||
11092 | return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResVT, Vec, Op.getOperand(1), | |||
11093 | DAG.getIntPtrConstant(NumElems/2, dl)); | |||
11094 | } | |||
11095 | ||||
11096 | static SDValue LowerCONCAT_VECTORS(SDValue Op, | |||
11097 | const X86Subtarget &Subtarget, | |||
11098 | SelectionDAG &DAG) { | |||
11099 | MVT VT = Op.getSimpleValueType(); | |||
11100 | if (VT.getVectorElementType() == MVT::i1) | |||
11101 | return LowerCONCAT_VECTORSvXi1(Op, Subtarget, DAG); | |||
11102 | ||||
11103 | assert((VT.is256BitVector() && Op.getNumOperands() == 2) ||(static_cast <bool> ((VT.is256BitVector() && Op .getNumOperands() == 2) || (VT.is512BitVector() && (Op .getNumOperands() == 2 || Op.getNumOperands() == 4))) ? void ( 0) : __assert_fail ("(VT.is256BitVector() && Op.getNumOperands() == 2) || (VT.is512BitVector() && (Op.getNumOperands() == 2 || Op.getNumOperands() == 4))" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11105, __extension__ __PRETTY_FUNCTION__)) | |||
11104 | (VT.is512BitVector() && (Op.getNumOperands() == 2 ||(static_cast <bool> ((VT.is256BitVector() && Op .getNumOperands() == 2) || (VT.is512BitVector() && (Op .getNumOperands() == 2 || Op.getNumOperands() == 4))) ? void ( 0) : __assert_fail ("(VT.is256BitVector() && Op.getNumOperands() == 2) || (VT.is512BitVector() && (Op.getNumOperands() == 2 || Op.getNumOperands() == 4))" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11105, __extension__ __PRETTY_FUNCTION__)) | |||
11105 | Op.getNumOperands() == 4)))(static_cast <bool> ((VT.is256BitVector() && Op .getNumOperands() == 2) || (VT.is512BitVector() && (Op .getNumOperands() == 2 || Op.getNumOperands() == 4))) ? void ( 0) : __assert_fail ("(VT.is256BitVector() && Op.getNumOperands() == 2) || (VT.is512BitVector() && (Op.getNumOperands() == 2 || Op.getNumOperands() == 4))" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11105, __extension__ __PRETTY_FUNCTION__)); | |||
11106 | ||||
11107 | // AVX can use the vinsertf128 instruction to create 256-bit vectors | |||
11108 | // from two other 128-bit ones. | |||
11109 | ||||
11110 | // 512-bit vector may contain 2 256-bit vectors or 4 128-bit vectors | |||
11111 | return LowerAVXCONCAT_VECTORS(Op, DAG, Subtarget); | |||
11112 | } | |||
11113 | ||||
11114 | //===----------------------------------------------------------------------===// | |||
11115 | // Vector shuffle lowering | |||
11116 | // | |||
11117 | // This is an experimental code path for lowering vector shuffles on x86. It is | |||
11118 | // designed to handle arbitrary vector shuffles and blends, gracefully | |||
11119 | // degrading performance as necessary. It works hard to recognize idiomatic | |||
11120 | // shuffles and lower them to optimal instruction patterns without leaving | |||
11121 | // a framework that allows reasonably efficient handling of all vector shuffle | |||
11122 | // patterns. | |||
11123 | //===----------------------------------------------------------------------===// | |||
11124 | ||||
11125 | /// Tiny helper function to identify a no-op mask. | |||
11126 | /// | |||
11127 | /// This is a somewhat boring predicate function. It checks whether the mask | |||
11128 | /// array input, which is assumed to be a single-input shuffle mask of the kind | |||
11129 | /// used by the X86 shuffle instructions (not a fully general | |||
11130 | /// ShuffleVectorSDNode mask) requires any shuffles to occur. Both undef and an | |||
11131 | /// in-place shuffle are 'no-op's. | |||
11132 | static bool isNoopShuffleMask(ArrayRef<int> Mask) { | |||
11133 | for (int i = 0, Size = Mask.size(); i < Size; ++i) { | |||
11134 | assert(Mask[i] >= -1 && "Out of bound mask element!")(static_cast <bool> (Mask[i] >= -1 && "Out of bound mask element!" ) ? void (0) : __assert_fail ("Mask[i] >= -1 && \"Out of bound mask element!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11134, __extension__ __PRETTY_FUNCTION__)); | |||
11135 | if (Mask[i] >= 0 && Mask[i] != i) | |||
11136 | return false; | |||
11137 | } | |||
11138 | return true; | |||
11139 | } | |||
11140 | ||||
11141 | /// Test whether there are elements crossing LaneSizeInBits lanes in this | |||
11142 | /// shuffle mask. | |||
11143 | /// | |||
11144 | /// X86 divides up its shuffles into in-lane and cross-lane shuffle operations | |||
11145 | /// and we routinely test for these. | |||
11146 | static bool isLaneCrossingShuffleMask(unsigned LaneSizeInBits, | |||
11147 | unsigned ScalarSizeInBits, | |||
11148 | ArrayRef<int> Mask) { | |||
11149 | assert(LaneSizeInBits && ScalarSizeInBits &&(static_cast <bool> (LaneSizeInBits && ScalarSizeInBits && (LaneSizeInBits % ScalarSizeInBits) == 0 && "Illegal shuffle lane size") ? void (0) : __assert_fail ("LaneSizeInBits && ScalarSizeInBits && (LaneSizeInBits % ScalarSizeInBits) == 0 && \"Illegal shuffle lane size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11151, __extension__ __PRETTY_FUNCTION__)) | |||
11150 | (LaneSizeInBits % ScalarSizeInBits) == 0 &&(static_cast <bool> (LaneSizeInBits && ScalarSizeInBits && (LaneSizeInBits % ScalarSizeInBits) == 0 && "Illegal shuffle lane size") ? void (0) : __assert_fail ("LaneSizeInBits && ScalarSizeInBits && (LaneSizeInBits % ScalarSizeInBits) == 0 && \"Illegal shuffle lane size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11151, __extension__ __PRETTY_FUNCTION__)) | |||
11151 | "Illegal shuffle lane size")(static_cast <bool> (LaneSizeInBits && ScalarSizeInBits && (LaneSizeInBits % ScalarSizeInBits) == 0 && "Illegal shuffle lane size") ? void (0) : __assert_fail ("LaneSizeInBits && ScalarSizeInBits && (LaneSizeInBits % ScalarSizeInBits) == 0 && \"Illegal shuffle lane size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11151, __extension__ __PRETTY_FUNCTION__)); | |||
11152 | int LaneSize = LaneSizeInBits / ScalarSizeInBits; | |||
11153 | int Size = Mask.size(); | |||
11154 | for (int i = 0; i < Size; ++i) | |||
11155 | if (Mask[i] >= 0 && (Mask[i] % Size) / LaneSize != i / LaneSize) | |||
11156 | return true; | |||
11157 | return false; | |||
11158 | } | |||
11159 | ||||
11160 | /// Test whether there are elements crossing 128-bit lanes in this | |||
11161 | /// shuffle mask. | |||
11162 | static bool is128BitLaneCrossingShuffleMask(MVT VT, ArrayRef<int> Mask) { | |||
11163 | return isLaneCrossingShuffleMask(128, VT.getScalarSizeInBits(), Mask); | |||
11164 | } | |||
11165 | ||||
11166 | /// Test whether elements in each LaneSizeInBits lane in this shuffle mask come | |||
11167 | /// from multiple lanes - this is different to isLaneCrossingShuffleMask to | |||
11168 | /// better support 'repeated mask + lane permute' style shuffles. | |||
11169 | static bool isMultiLaneShuffleMask(unsigned LaneSizeInBits, | |||
11170 | unsigned ScalarSizeInBits, | |||
11171 | ArrayRef<int> Mask) { | |||
11172 | assert(LaneSizeInBits && ScalarSizeInBits &&(static_cast <bool> (LaneSizeInBits && ScalarSizeInBits && (LaneSizeInBits % ScalarSizeInBits) == 0 && "Illegal shuffle lane size") ? void (0) : __assert_fail ("LaneSizeInBits && ScalarSizeInBits && (LaneSizeInBits % ScalarSizeInBits) == 0 && \"Illegal shuffle lane size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11174, __extension__ __PRETTY_FUNCTION__)) | |||
11173 | (LaneSizeInBits % ScalarSizeInBits) == 0 &&(static_cast <bool> (LaneSizeInBits && ScalarSizeInBits && (LaneSizeInBits % ScalarSizeInBits) == 0 && "Illegal shuffle lane size") ? void (0) : __assert_fail ("LaneSizeInBits && ScalarSizeInBits && (LaneSizeInBits % ScalarSizeInBits) == 0 && \"Illegal shuffle lane size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11174, __extension__ __PRETTY_FUNCTION__)) | |||
11174 | "Illegal shuffle lane size")(static_cast <bool> (LaneSizeInBits && ScalarSizeInBits && (LaneSizeInBits % ScalarSizeInBits) == 0 && "Illegal shuffle lane size") ? void (0) : __assert_fail ("LaneSizeInBits && ScalarSizeInBits && (LaneSizeInBits % ScalarSizeInBits) == 0 && \"Illegal shuffle lane size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11174, __extension__ __PRETTY_FUNCTION__)); | |||
11175 | int NumElts = Mask.size(); | |||
11176 | int NumEltsPerLane = LaneSizeInBits / ScalarSizeInBits; | |||
11177 | int NumLanes = NumElts / NumEltsPerLane; | |||
11178 | if (NumLanes > 1) { | |||
11179 | for (int i = 0; i != NumLanes; ++i) { | |||
11180 | int SrcLane = -1; | |||
11181 | for (int j = 0; j != NumEltsPerLane; ++j) { | |||
11182 | int M = Mask[(i * NumEltsPerLane) + j]; | |||
11183 | if (M < 0) | |||
11184 | continue; | |||
11185 | int Lane = (M % NumElts) / NumEltsPerLane; | |||
11186 | if (SrcLane >= 0 && SrcLane != Lane) | |||
11187 | return true; | |||
11188 | SrcLane = Lane; | |||
11189 | } | |||
11190 | } | |||
11191 | } | |||
11192 | return false; | |||
11193 | } | |||
11194 | ||||
11195 | /// Test whether a shuffle mask is equivalent within each sub-lane. | |||
11196 | /// | |||
11197 | /// This checks a shuffle mask to see if it is performing the same | |||
11198 | /// lane-relative shuffle in each sub-lane. This trivially implies | |||
11199 | /// that it is also not lane-crossing. It may however involve a blend from the | |||
11200 | /// same lane of a second vector. | |||
11201 | /// | |||
11202 | /// The specific repeated shuffle mask is populated in \p RepeatedMask, as it is | |||
11203 | /// non-trivial to compute in the face of undef lanes. The representation is | |||
11204 | /// suitable for use with existing 128-bit shuffles as entries from the second | |||
11205 | /// vector have been remapped to [LaneSize, 2*LaneSize). | |||
11206 | static bool isRepeatedShuffleMask(unsigned LaneSizeInBits, MVT VT, | |||
11207 | ArrayRef<int> Mask, | |||
11208 | SmallVectorImpl<int> &RepeatedMask) { | |||
11209 | auto LaneSize = LaneSizeInBits / VT.getScalarSizeInBits(); | |||
11210 | RepeatedMask.assign(LaneSize, -1); | |||
11211 | int Size = Mask.size(); | |||
11212 | for (int i = 0; i < Size; ++i) { | |||
11213 | assert(Mask[i] == SM_SentinelUndef || Mask[i] >= 0)(static_cast <bool> (Mask[i] == SM_SentinelUndef || Mask [i] >= 0) ? void (0) : __assert_fail ("Mask[i] == SM_SentinelUndef || Mask[i] >= 0" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11213, __extension__ __PRETTY_FUNCTION__)); | |||
11214 | if (Mask[i] < 0) | |||
11215 | continue; | |||
11216 | if ((Mask[i] % Size) / LaneSize != i / LaneSize) | |||
11217 | // This entry crosses lanes, so there is no way to model this shuffle. | |||
11218 | return false; | |||
11219 | ||||
11220 | // Ok, handle the in-lane shuffles by detecting if and when they repeat. | |||
11221 | // Adjust second vector indices to start at LaneSize instead of Size. | |||
11222 | int LocalM = Mask[i] < Size ? Mask[i] % LaneSize | |||
11223 | : Mask[i] % LaneSize + LaneSize; | |||
11224 | if (RepeatedMask[i % LaneSize] < 0) | |||
11225 | // This is the first non-undef entry in this slot of a 128-bit lane. | |||
11226 | RepeatedMask[i % LaneSize] = LocalM; | |||
11227 | else if (RepeatedMask[i % LaneSize] != LocalM) | |||
11228 | // Found a mismatch with the repeated mask. | |||
11229 | return false; | |||
11230 | } | |||
11231 | return true; | |||
11232 | } | |||
11233 | ||||
11234 | /// Test whether a shuffle mask is equivalent within each 128-bit lane. | |||
11235 | static bool | |||
11236 | is128BitLaneRepeatedShuffleMask(MVT VT, ArrayRef<int> Mask, | |||
11237 | SmallVectorImpl<int> &RepeatedMask) { | |||
11238 | return isRepeatedShuffleMask(128, VT, Mask, RepeatedMask); | |||
11239 | } | |||
11240 | ||||
11241 | static bool | |||
11242 | is128BitLaneRepeatedShuffleMask(MVT VT, ArrayRef<int> Mask) { | |||
11243 | SmallVector<int, 32> RepeatedMask; | |||
11244 | return isRepeatedShuffleMask(128, VT, Mask, RepeatedMask); | |||
11245 | } | |||
11246 | ||||
11247 | /// Test whether a shuffle mask is equivalent within each 256-bit lane. | |||
11248 | static bool | |||
11249 | is256BitLaneRepeatedShuffleMask(MVT VT, ArrayRef<int> Mask, | |||
11250 | SmallVectorImpl<int> &RepeatedMask) { | |||
11251 | return isRepeatedShuffleMask(256, VT, Mask, RepeatedMask); | |||
11252 | } | |||
11253 | ||||
11254 | /// Test whether a target shuffle mask is equivalent within each sub-lane. | |||
11255 | /// Unlike isRepeatedShuffleMask we must respect SM_SentinelZero. | |||
11256 | static bool isRepeatedTargetShuffleMask(unsigned LaneSizeInBits, | |||
11257 | unsigned EltSizeInBits, | |||
11258 | ArrayRef<int> Mask, | |||
11259 | SmallVectorImpl<int> &RepeatedMask) { | |||
11260 | int LaneSize = LaneSizeInBits / EltSizeInBits; | |||
11261 | RepeatedMask.assign(LaneSize, SM_SentinelUndef); | |||
11262 | int Size = Mask.size(); | |||
11263 | for (int i = 0; i < Size; ++i) { | |||
11264 | assert(isUndefOrZero(Mask[i]) || (Mask[i] >= 0))(static_cast <bool> (isUndefOrZero(Mask[i]) || (Mask[i] >= 0)) ? void (0) : __assert_fail ("isUndefOrZero(Mask[i]) || (Mask[i] >= 0)" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11264, __extension__ __PRETTY_FUNCTION__)); | |||
11265 | if (Mask[i] == SM_SentinelUndef) | |||
11266 | continue; | |||
11267 | if (Mask[i] == SM_SentinelZero) { | |||
11268 | if (!isUndefOrZero(RepeatedMask[i % LaneSize])) | |||
11269 | return false; | |||
11270 | RepeatedMask[i % LaneSize] = SM_SentinelZero; | |||
11271 | continue; | |||
11272 | } | |||
11273 | if ((Mask[i] % Size) / LaneSize != i / LaneSize) | |||
11274 | // This entry crosses lanes, so there is no way to model this shuffle. | |||
11275 | return false; | |||
11276 | ||||
11277 | // Handle the in-lane shuffles by detecting if and when they repeat. Adjust | |||
11278 | // later vector indices to start at multiples of LaneSize instead of Size. | |||
11279 | int LaneM = Mask[i] / Size; | |||
11280 | int LocalM = (Mask[i] % LaneSize) + (LaneM * LaneSize); | |||
11281 | if (RepeatedMask[i % LaneSize] == SM_SentinelUndef) | |||
11282 | // This is the first non-undef entry in this slot of a 128-bit lane. | |||
11283 | RepeatedMask[i % LaneSize] = LocalM; | |||
11284 | else if (RepeatedMask[i % LaneSize] != LocalM) | |||
11285 | // Found a mismatch with the repeated mask. | |||
11286 | return false; | |||
11287 | } | |||
11288 | return true; | |||
11289 | } | |||
11290 | ||||
11291 | /// Test whether a target shuffle mask is equivalent within each sub-lane. | |||
11292 | /// Unlike isRepeatedShuffleMask we must respect SM_SentinelZero. | |||
11293 | static bool isRepeatedTargetShuffleMask(unsigned LaneSizeInBits, MVT VT, | |||
11294 | ArrayRef<int> Mask, | |||
11295 | SmallVectorImpl<int> &RepeatedMask) { | |||
11296 | return isRepeatedTargetShuffleMask(LaneSizeInBits, VT.getScalarSizeInBits(), | |||
11297 | Mask, RepeatedMask); | |||
11298 | } | |||
11299 | ||||
11300 | /// Checks whether the vector elements referenced by two shuffle masks are | |||
11301 | /// equivalent. | |||
11302 | static bool IsElementEquivalent(int MaskSize, SDValue Op, SDValue ExpectedOp, | |||
11303 | int Idx, int ExpectedIdx) { | |||
11304 | assert(0 <= Idx && Idx < MaskSize && 0 <= ExpectedIdx &&(static_cast <bool> (0 <= Idx && Idx < MaskSize && 0 <= ExpectedIdx && ExpectedIdx < MaskSize && "Out of range element index") ? void (0) : __assert_fail ("0 <= Idx && Idx < MaskSize && 0 <= ExpectedIdx && ExpectedIdx < MaskSize && \"Out of range element index\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11305, __extension__ __PRETTY_FUNCTION__)) | |||
11305 | ExpectedIdx < MaskSize && "Out of range element index")(static_cast <bool> (0 <= Idx && Idx < MaskSize && 0 <= ExpectedIdx && ExpectedIdx < MaskSize && "Out of range element index") ? void (0) : __assert_fail ("0 <= Idx && Idx < MaskSize && 0 <= ExpectedIdx && ExpectedIdx < MaskSize && \"Out of range element index\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11305, __extension__ __PRETTY_FUNCTION__)); | |||
11306 | if (!Op || !ExpectedOp || Op.getOpcode() != ExpectedOp.getOpcode()) | |||
11307 | return false; | |||
11308 | ||||
11309 | switch (Op.getOpcode()) { | |||
11310 | case ISD::BUILD_VECTOR: | |||
11311 | // If the values are build vectors, we can look through them to find | |||
11312 | // equivalent inputs that make the shuffles equivalent. | |||
11313 | // TODO: Handle MaskSize != Op.getNumOperands()? | |||
11314 | if (MaskSize == (int)Op.getNumOperands() && | |||
11315 | MaskSize == (int)ExpectedOp.getNumOperands()) | |||
11316 | return Op.getOperand(Idx) == ExpectedOp.getOperand(ExpectedIdx); | |||
11317 | break; | |||
11318 | case X86ISD::VBROADCAST: | |||
11319 | case X86ISD::VBROADCAST_LOAD: | |||
11320 | // TODO: Handle MaskSize != Op.getValueType().getVectorNumElements()? | |||
11321 | return (Op == ExpectedOp && | |||
11322 | (int)Op.getValueType().getVectorNumElements() == MaskSize); | |||
11323 | case X86ISD::HADD: | |||
11324 | case X86ISD::HSUB: | |||
11325 | case X86ISD::FHADD: | |||
11326 | case X86ISD::FHSUB: | |||
11327 | case X86ISD::PACKSS: | |||
11328 | case X86ISD::PACKUS: | |||
11329 | // HOP(X,X) can refer to the elt from the lower/upper half of a lane. | |||
11330 | // TODO: Handle MaskSize != NumElts? | |||
11331 | // TODO: Handle HOP(X,Y) vs HOP(Y,X) equivalence cases. | |||
11332 | if (Op == ExpectedOp && Op.getOperand(0) == Op.getOperand(1)) { | |||
11333 | MVT VT = Op.getSimpleValueType(); | |||
11334 | int NumElts = VT.getVectorNumElements(); | |||
11335 | if (MaskSize == NumElts) { | |||
11336 | int NumLanes = VT.getSizeInBits() / 128; | |||
11337 | int NumEltsPerLane = NumElts / NumLanes; | |||
11338 | int NumHalfEltsPerLane = NumEltsPerLane / 2; | |||
11339 | bool SameLane = | |||
11340 | (Idx / NumEltsPerLane) == (ExpectedIdx / NumEltsPerLane); | |||
11341 | bool SameElt = | |||
11342 | (Idx % NumHalfEltsPerLane) == (ExpectedIdx % NumHalfEltsPerLane); | |||
11343 | return SameLane && SameElt; | |||
11344 | } | |||
11345 | } | |||
11346 | break; | |||
11347 | } | |||
11348 | ||||
11349 | return false; | |||
11350 | } | |||
11351 | ||||
11352 | /// Checks whether a shuffle mask is equivalent to an explicit list of | |||
11353 | /// arguments. | |||
11354 | /// | |||
11355 | /// This is a fast way to test a shuffle mask against a fixed pattern: | |||
11356 | /// | |||
11357 | /// if (isShuffleEquivalent(Mask, 3, 2, {1, 0})) { ... } | |||
11358 | /// | |||
11359 | /// It returns true if the mask is exactly as wide as the argument list, and | |||
11360 | /// each element of the mask is either -1 (signifying undef) or the value given | |||
11361 | /// in the argument. | |||
11362 | static bool isShuffleEquivalent(ArrayRef<int> Mask, ArrayRef<int> ExpectedMask, | |||
11363 | SDValue V1 = SDValue(), | |||
11364 | SDValue V2 = SDValue()) { | |||
11365 | int Size = Mask.size(); | |||
11366 | if (Size != (int)ExpectedMask.size()) | |||
11367 | return false; | |||
11368 | ||||
11369 | for (int i = 0; i < Size; ++i) { | |||
11370 | assert(Mask[i] >= -1 && "Out of bound mask element!")(static_cast <bool> (Mask[i] >= -1 && "Out of bound mask element!" ) ? void (0) : __assert_fail ("Mask[i] >= -1 && \"Out of bound mask element!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11370, __extension__ __PRETTY_FUNCTION__)); | |||
11371 | int MaskIdx = Mask[i]; | |||
11372 | int ExpectedIdx = ExpectedMask[i]; | |||
11373 | if (0 <= MaskIdx && MaskIdx != ExpectedIdx) { | |||
11374 | SDValue MaskV = MaskIdx < Size ? V1 : V2; | |||
11375 | SDValue ExpectedV = ExpectedIdx < Size ? V1 : V2; | |||
11376 | MaskIdx = MaskIdx < Size ? MaskIdx : (MaskIdx - Size); | |||
11377 | ExpectedIdx = ExpectedIdx < Size ? ExpectedIdx : (ExpectedIdx - Size); | |||
11378 | if (!IsElementEquivalent(Size, MaskV, ExpectedV, MaskIdx, ExpectedIdx)) | |||
11379 | return false; | |||
11380 | } | |||
11381 | } | |||
11382 | return true; | |||
11383 | } | |||
11384 | ||||
11385 | /// Checks whether a target shuffle mask is equivalent to an explicit pattern. | |||
11386 | /// | |||
11387 | /// The masks must be exactly the same width. | |||
11388 | /// | |||
11389 | /// If an element in Mask matches SM_SentinelUndef (-1) then the corresponding | |||
11390 | /// value in ExpectedMask is always accepted. Otherwise the indices must match. | |||
11391 | /// | |||
11392 | /// SM_SentinelZero is accepted as a valid negative index but must match in | |||
11393 | /// both. | |||
11394 | static bool isTargetShuffleEquivalent(MVT VT, ArrayRef<int> Mask, | |||
11395 | ArrayRef<int> ExpectedMask, | |||
11396 | SDValue V1 = SDValue(), | |||
11397 | SDValue V2 = SDValue()) { | |||
11398 | int Size = Mask.size(); | |||
11399 | if (Size != (int)ExpectedMask.size()) | |||
11400 | return false; | |||
11401 | assert(isUndefOrZeroOrInRange(ExpectedMask, 0, 2 * Size) &&(static_cast <bool> (isUndefOrZeroOrInRange(ExpectedMask , 0, 2 * Size) && "Illegal target shuffle mask") ? void (0) : __assert_fail ("isUndefOrZeroOrInRange(ExpectedMask, 0, 2 * Size) && \"Illegal target shuffle mask\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11402, __extension__ __PRETTY_FUNCTION__)) | |||
11402 | "Illegal target shuffle mask")(static_cast <bool> (isUndefOrZeroOrInRange(ExpectedMask , 0, 2 * Size) && "Illegal target shuffle mask") ? void (0) : __assert_fail ("isUndefOrZeroOrInRange(ExpectedMask, 0, 2 * Size) && \"Illegal target shuffle mask\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11402, __extension__ __PRETTY_FUNCTION__)); | |||
11403 | ||||
11404 | // Check for out-of-range target shuffle mask indices. | |||
11405 | if (!isUndefOrZeroOrInRange(Mask, 0, 2 * Size)) | |||
11406 | return false; | |||
11407 | ||||
11408 | // Don't use V1/V2 if they're not the same size as the shuffle mask type. | |||
11409 | if (V1 && V1.getValueSizeInBits() != VT.getSizeInBits()) | |||
11410 | V1 = SDValue(); | |||
11411 | if (V2 && V2.getValueSizeInBits() != VT.getSizeInBits()) | |||
11412 | V2 = SDValue(); | |||
11413 | ||||
11414 | for (int i = 0; i < Size; ++i) { | |||
11415 | int MaskIdx = Mask[i]; | |||
11416 | int ExpectedIdx = ExpectedMask[i]; | |||
11417 | if (MaskIdx == SM_SentinelUndef || MaskIdx == ExpectedIdx) | |||
11418 | continue; | |||
11419 | if (0 <= MaskIdx && 0 <= ExpectedIdx) { | |||
11420 | SDValue MaskV = MaskIdx < Size ? V1 : V2; | |||
11421 | SDValue ExpectedV = ExpectedIdx < Size ? V1 : V2; | |||
11422 | MaskIdx = MaskIdx < Size ? MaskIdx : (MaskIdx - Size); | |||
11423 | ExpectedIdx = ExpectedIdx < Size ? ExpectedIdx : (ExpectedIdx - Size); | |||
11424 | if (IsElementEquivalent(Size, MaskV, ExpectedV, MaskIdx, ExpectedIdx)) | |||
11425 | continue; | |||
11426 | } | |||
11427 | // TODO - handle SM_Sentinel equivalences. | |||
11428 | return false; | |||
11429 | } | |||
11430 | return true; | |||
11431 | } | |||
11432 | ||||
11433 | // Attempt to create a shuffle mask from a VSELECT condition mask. | |||
11434 | static bool createShuffleMaskFromVSELECT(SmallVectorImpl<int> &Mask, | |||
11435 | SDValue Cond) { | |||
11436 | EVT CondVT = Cond.getValueType(); | |||
11437 | unsigned EltSizeInBits = CondVT.getScalarSizeInBits(); | |||
11438 | unsigned NumElts = CondVT.getVectorNumElements(); | |||
11439 | ||||
11440 | APInt UndefElts; | |||
11441 | SmallVector<APInt, 32> EltBits; | |||
11442 | if (!getTargetConstantBitsFromNode(Cond, EltSizeInBits, UndefElts, EltBits, | |||
11443 | true, false)) | |||
11444 | return false; | |||
11445 | ||||
11446 | Mask.resize(NumElts, SM_SentinelUndef); | |||
11447 | ||||
11448 | for (int i = 0; i != (int)NumElts; ++i) { | |||
11449 | Mask[i] = i; | |||
11450 | // Arbitrarily choose from the 2nd operand if the select condition element | |||
11451 | // is undef. | |||
11452 | // TODO: Can we do better by matching patterns such as even/odd? | |||
11453 | if (UndefElts[i] || EltBits[i].isZero()) | |||
11454 | Mask[i] += NumElts; | |||
11455 | } | |||
11456 | ||||
11457 | return true; | |||
11458 | } | |||
11459 | ||||
11460 | // Check if the shuffle mask is suitable for the AVX vpunpcklwd or vpunpckhwd | |||
11461 | // instructions. | |||
11462 | static bool isUnpackWdShuffleMask(ArrayRef<int> Mask, MVT VT) { | |||
11463 | if (VT != MVT::v8i32 && VT != MVT::v8f32) | |||
11464 | return false; | |||
11465 | ||||
11466 | SmallVector<int, 8> Unpcklwd; | |||
11467 | createUnpackShuffleMask(MVT::v8i16, Unpcklwd, /* Lo = */ true, | |||
11468 | /* Unary = */ false); | |||
11469 | SmallVector<int, 8> Unpckhwd; | |||
11470 | createUnpackShuffleMask(MVT::v8i16, Unpckhwd, /* Lo = */ false, | |||
11471 | /* Unary = */ false); | |||
11472 | bool IsUnpackwdMask = (isTargetShuffleEquivalent(VT, Mask, Unpcklwd) || | |||
11473 | isTargetShuffleEquivalent(VT, Mask, Unpckhwd)); | |||
11474 | return IsUnpackwdMask; | |||
11475 | } | |||
11476 | ||||
11477 | static bool is128BitUnpackShuffleMask(ArrayRef<int> Mask) { | |||
11478 | // Create 128-bit vector type based on mask size. | |||
11479 | MVT EltVT = MVT::getIntegerVT(128 / Mask.size()); | |||
11480 | MVT VT = MVT::getVectorVT(EltVT, Mask.size()); | |||
11481 | ||||
11482 | // We can't assume a canonical shuffle mask, so try the commuted version too. | |||
11483 | SmallVector<int, 4> CommutedMask(Mask.begin(), Mask.end()); | |||
11484 | ShuffleVectorSDNode::commuteMask(CommutedMask); | |||
11485 | ||||
11486 | // Match any of unary/binary or low/high. | |||
11487 | for (unsigned i = 0; i != 4; ++i) { | |||
11488 | SmallVector<int, 16> UnpackMask; | |||
11489 | createUnpackShuffleMask(VT, UnpackMask, (i >> 1) % 2, i % 2); | |||
11490 | if (isTargetShuffleEquivalent(VT, Mask, UnpackMask) || | |||
11491 | isTargetShuffleEquivalent(VT, CommutedMask, UnpackMask)) | |||
11492 | return true; | |||
11493 | } | |||
11494 | return false; | |||
11495 | } | |||
11496 | ||||
11497 | /// Return true if a shuffle mask chooses elements identically in its top and | |||
11498 | /// bottom halves. For example, any splat mask has the same top and bottom | |||
11499 | /// halves. If an element is undefined in only one half of the mask, the halves | |||
11500 | /// are not considered identical. | |||
11501 | static bool hasIdenticalHalvesShuffleMask(ArrayRef<int> Mask) { | |||
11502 | assert(Mask.size() % 2 == 0 && "Expecting even number of elements in mask")(static_cast <bool> (Mask.size() % 2 == 0 && "Expecting even number of elements in mask" ) ? void (0) : __assert_fail ("Mask.size() % 2 == 0 && \"Expecting even number of elements in mask\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11502, __extension__ __PRETTY_FUNCTION__)); | |||
11503 | unsigned HalfSize = Mask.size() / 2; | |||
11504 | for (unsigned i = 0; i != HalfSize; ++i) { | |||
11505 | if (Mask[i] != Mask[i + HalfSize]) | |||
11506 | return false; | |||
11507 | } | |||
11508 | return true; | |||
11509 | } | |||
11510 | ||||
11511 | /// Get a 4-lane 8-bit shuffle immediate for a mask. | |||
11512 | /// | |||
11513 | /// This helper function produces an 8-bit shuffle immediate corresponding to | |||
11514 | /// the ubiquitous shuffle encoding scheme used in x86 instructions for | |||
11515 | /// shuffling 4 lanes. It can be used with most of the PSHUF instructions for | |||
11516 | /// example. | |||
11517 | /// | |||
11518 | /// NB: We rely heavily on "undef" masks preserving the input lane. | |||
11519 | static unsigned getV4X86ShuffleImm(ArrayRef<int> Mask) { | |||
11520 | assert(Mask.size() == 4 && "Only 4-lane shuffle masks")(static_cast <bool> (Mask.size() == 4 && "Only 4-lane shuffle masks" ) ? void (0) : __assert_fail ("Mask.size() == 4 && \"Only 4-lane shuffle masks\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11520, __extension__ __PRETTY_FUNCTION__)); | |||
11521 | assert(Mask[0] >= -1 && Mask[0] < 4 && "Out of bound mask element!")(static_cast <bool> (Mask[0] >= -1 && Mask[0 ] < 4 && "Out of bound mask element!") ? void (0) : __assert_fail ("Mask[0] >= -1 && Mask[0] < 4 && \"Out of bound mask element!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11521, __extension__ __PRETTY_FUNCTION__)); | |||
11522 | assert(Mask[1] >= -1 && Mask[1] < 4 && "Out of bound mask element!")(static_cast <bool> (Mask[1] >= -1 && Mask[1 ] < 4 && "Out of bound mask element!") ? void (0) : __assert_fail ("Mask[1] >= -1 && Mask[1] < 4 && \"Out of bound mask element!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11522, __extension__ __PRETTY_FUNCTION__)); | |||
11523 | assert(Mask[2] >= -1 && Mask[2] < 4 && "Out of bound mask element!")(static_cast <bool> (Mask[2] >= -1 && Mask[2 ] < 4 && "Out of bound mask element!") ? void (0) : __assert_fail ("Mask[2] >= -1 && Mask[2] < 4 && \"Out of bound mask element!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11523, __extension__ __PRETTY_FUNCTION__)); | |||
11524 | assert(Mask[3] >= -1 && Mask[3] < 4 && "Out of bound mask element!")(static_cast <bool> (Mask[3] >= -1 && Mask[3 ] < 4 && "Out of bound mask element!") ? void (0) : __assert_fail ("Mask[3] >= -1 && Mask[3] < 4 && \"Out of bound mask element!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11524, __extension__ __PRETTY_FUNCTION__)); | |||
11525 | ||||
11526 | // If the mask only uses one non-undef element, then fully 'splat' it to | |||
11527 | // improve later broadcast matching. | |||
11528 | int FirstIndex = find_if(Mask, [](int M) { return M >= 0; }) - Mask.begin(); | |||
11529 | assert(0 <= FirstIndex && FirstIndex < 4 && "All undef shuffle mask")(static_cast <bool> (0 <= FirstIndex && FirstIndex < 4 && "All undef shuffle mask") ? void (0) : __assert_fail ("0 <= FirstIndex && FirstIndex < 4 && \"All undef shuffle mask\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11529, __extension__ __PRETTY_FUNCTION__)); | |||
11530 | ||||
11531 | int FirstElt = Mask[FirstIndex]; | |||
11532 | if (all_of(Mask, [FirstElt](int M) { return M < 0 || M == FirstElt; })) | |||
11533 | return (FirstElt << 6) | (FirstElt << 4) | (FirstElt << 2) | FirstElt; | |||
11534 | ||||
11535 | unsigned Imm = 0; | |||
11536 | Imm |= (Mask[0] < 0 ? 0 : Mask[0]) << 0; | |||
11537 | Imm |= (Mask[1] < 0 ? 1 : Mask[1]) << 2; | |||
11538 | Imm |= (Mask[2] < 0 ? 2 : Mask[2]) << 4; | |||
11539 | Imm |= (Mask[3] < 0 ? 3 : Mask[3]) << 6; | |||
11540 | return Imm; | |||
11541 | } | |||
11542 | ||||
11543 | static SDValue getV4X86ShuffleImm8ForMask(ArrayRef<int> Mask, const SDLoc &DL, | |||
11544 | SelectionDAG &DAG) { | |||
11545 | return DAG.getTargetConstant(getV4X86ShuffleImm(Mask), DL, MVT::i8); | |||
11546 | } | |||
11547 | ||||
11548 | // The Shuffle result is as follow: | |||
11549 | // 0*a[0]0*a[1]...0*a[n] , n >=0 where a[] elements in a ascending order. | |||
11550 | // Each Zeroable's element correspond to a particular Mask's element. | |||
11551 | // As described in computeZeroableShuffleElements function. | |||
11552 | // | |||
11553 | // The function looks for a sub-mask that the nonzero elements are in | |||
11554 | // increasing order. If such sub-mask exist. The function returns true. | |||
11555 | static bool isNonZeroElementsInOrder(const APInt &Zeroable, | |||
11556 | ArrayRef<int> Mask, const EVT &VectorType, | |||
11557 | bool &IsZeroSideLeft) { | |||
11558 | int NextElement = -1; | |||
11559 | // Check if the Mask's nonzero elements are in increasing order. | |||
11560 | for (int i = 0, e = Mask.size(); i < e; i++) { | |||
11561 | // Checks if the mask's zeros elements are built from only zeros. | |||
11562 | assert(Mask[i] >= -1 && "Out of bound mask element!")(static_cast <bool> (Mask[i] >= -1 && "Out of bound mask element!" ) ? void (0) : __assert_fail ("Mask[i] >= -1 && \"Out of bound mask element!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11562, __extension__ __PRETTY_FUNCTION__)); | |||
11563 | if (Mask[i] < 0) | |||
11564 | return false; | |||
11565 | if (Zeroable[i]) | |||
11566 | continue; | |||
11567 | // Find the lowest non zero element | |||
11568 | if (NextElement < 0) { | |||
11569 | NextElement = Mask[i] != 0 ? VectorType.getVectorNumElements() : 0; | |||
11570 | IsZeroSideLeft = NextElement != 0; | |||
11571 | } | |||
11572 | // Exit if the mask's non zero elements are not in increasing order. | |||
11573 | if (NextElement != Mask[i]) | |||
11574 | return false; | |||
11575 | NextElement++; | |||
11576 | } | |||
11577 | return true; | |||
11578 | } | |||
11579 | ||||
11580 | /// Try to lower a shuffle with a single PSHUFB of V1 or V2. | |||
11581 | static SDValue lowerShuffleWithPSHUFB(const SDLoc &DL, MVT VT, | |||
11582 | ArrayRef<int> Mask, SDValue V1, | |||
11583 | SDValue V2, const APInt &Zeroable, | |||
11584 | const X86Subtarget &Subtarget, | |||
11585 | SelectionDAG &DAG) { | |||
11586 | int Size = Mask.size(); | |||
11587 | int LaneSize = 128 / VT.getScalarSizeInBits(); | |||
11588 | const int NumBytes = VT.getSizeInBits() / 8; | |||
11589 | const int NumEltBytes = VT.getScalarSizeInBits() / 8; | |||
11590 | ||||
11591 | assert((Subtarget.hasSSSE3() && VT.is128BitVector()) ||(static_cast <bool> ((Subtarget.hasSSSE3() && VT .is128BitVector()) || (Subtarget.hasAVX2() && VT.is256BitVector ()) || (Subtarget.hasBWI() && VT.is512BitVector())) ? void (0) : __assert_fail ("(Subtarget.hasSSSE3() && VT.is128BitVector()) || (Subtarget.hasAVX2() && VT.is256BitVector()) || (Subtarget.hasBWI() && VT.is512BitVector())" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11593, __extension__ __PRETTY_FUNCTION__)) | |||
11592 | (Subtarget.hasAVX2() && VT.is256BitVector()) ||(static_cast <bool> ((Subtarget.hasSSSE3() && VT .is128BitVector()) || (Subtarget.hasAVX2() && VT.is256BitVector ()) || (Subtarget.hasBWI() && VT.is512BitVector())) ? void (0) : __assert_fail ("(Subtarget.hasSSSE3() && VT.is128BitVector()) || (Subtarget.hasAVX2() && VT.is256BitVector()) || (Subtarget.hasBWI() && VT.is512BitVector())" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11593, __extension__ __PRETTY_FUNCTION__)) | |||
11593 | (Subtarget.hasBWI() && VT.is512BitVector()))(static_cast <bool> ((Subtarget.hasSSSE3() && VT .is128BitVector()) || (Subtarget.hasAVX2() && VT.is256BitVector ()) || (Subtarget.hasBWI() && VT.is512BitVector())) ? void (0) : __assert_fail ("(Subtarget.hasSSSE3() && VT.is128BitVector()) || (Subtarget.hasAVX2() && VT.is256BitVector()) || (Subtarget.hasBWI() && VT.is512BitVector())" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11593, __extension__ __PRETTY_FUNCTION__)); | |||
11594 | ||||
11595 | SmallVector<SDValue, 64> PSHUFBMask(NumBytes); | |||
11596 | // Sign bit set in i8 mask means zero element. | |||
11597 | SDValue ZeroMask = DAG.getConstant(0x80, DL, MVT::i8); | |||
11598 | ||||
11599 | SDValue V; | |||
11600 | for (int i = 0; i < NumBytes; ++i) { | |||
11601 | int M = Mask[i / NumEltBytes]; | |||
11602 | if (M < 0) { | |||
11603 | PSHUFBMask[i] = DAG.getUNDEF(MVT::i8); | |||
11604 | continue; | |||
11605 | } | |||
11606 | if (Zeroable[i / NumEltBytes]) { | |||
11607 | PSHUFBMask[i] = ZeroMask; | |||
11608 | continue; | |||
11609 | } | |||
11610 | ||||
11611 | // We can only use a single input of V1 or V2. | |||
11612 | SDValue SrcV = (M >= Size ? V2 : V1); | |||
11613 | if (V && V != SrcV) | |||
11614 | return SDValue(); | |||
11615 | V = SrcV; | |||
11616 | M %= Size; | |||
11617 | ||||
11618 | // PSHUFB can't cross lanes, ensure this doesn't happen. | |||
11619 | if ((M / LaneSize) != ((i / NumEltBytes) / LaneSize)) | |||
11620 | return SDValue(); | |||
11621 | ||||
11622 | M = M % LaneSize; | |||
11623 | M = M * NumEltBytes + (i % NumEltBytes); | |||
11624 | PSHUFBMask[i] = DAG.getConstant(M, DL, MVT::i8); | |||
11625 | } | |||
11626 | assert(V && "Failed to find a source input")(static_cast <bool> (V && "Failed to find a source input" ) ? void (0) : __assert_fail ("V && \"Failed to find a source input\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11626, __extension__ __PRETTY_FUNCTION__)); | |||
11627 | ||||
11628 | MVT I8VT = MVT::getVectorVT(MVT::i8, NumBytes); | |||
11629 | return DAG.getBitcast( | |||
11630 | VT, DAG.getNode(X86ISD::PSHUFB, DL, I8VT, DAG.getBitcast(I8VT, V), | |||
11631 | DAG.getBuildVector(I8VT, DL, PSHUFBMask))); | |||
11632 | } | |||
11633 | ||||
11634 | static SDValue getMaskNode(SDValue Mask, MVT MaskVT, | |||
11635 | const X86Subtarget &Subtarget, SelectionDAG &DAG, | |||
11636 | const SDLoc &dl); | |||
11637 | ||||
11638 | // X86 has dedicated shuffle that can be lowered to VEXPAND | |||
11639 | static SDValue lowerShuffleToEXPAND(const SDLoc &DL, MVT VT, | |||
11640 | const APInt &Zeroable, | |||
11641 | ArrayRef<int> Mask, SDValue &V1, | |||
11642 | SDValue &V2, SelectionDAG &DAG, | |||
11643 | const X86Subtarget &Subtarget) { | |||
11644 | bool IsLeftZeroSide = true; | |||
11645 | if (!isNonZeroElementsInOrder(Zeroable, Mask, V1.getValueType(), | |||
11646 | IsLeftZeroSide)) | |||
11647 | return SDValue(); | |||
11648 | unsigned VEXPANDMask = (~Zeroable).getZExtValue(); | |||
11649 | MVT IntegerType = | |||
11650 | MVT::getIntegerVT(std::max((int)VT.getVectorNumElements(), 8)); | |||
11651 | SDValue MaskNode = DAG.getConstant(VEXPANDMask, DL, IntegerType); | |||
11652 | unsigned NumElts = VT.getVectorNumElements(); | |||
11653 | assert((NumElts == 4 || NumElts == 8 || NumElts == 16) &&(static_cast <bool> ((NumElts == 4 || NumElts == 8 || NumElts == 16) && "Unexpected number of vector elements") ? void (0) : __assert_fail ("(NumElts == 4 || NumElts == 8 || NumElts == 16) && \"Unexpected number of vector elements\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11654, __extension__ __PRETTY_FUNCTION__)) | |||
11654 | "Unexpected number of vector elements")(static_cast <bool> ((NumElts == 4 || NumElts == 8 || NumElts == 16) && "Unexpected number of vector elements") ? void (0) : __assert_fail ("(NumElts == 4 || NumElts == 8 || NumElts == 16) && \"Unexpected number of vector elements\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11654, __extension__ __PRETTY_FUNCTION__)); | |||
11655 | SDValue VMask = getMaskNode(MaskNode, MVT::getVectorVT(MVT::i1, NumElts), | |||
11656 | Subtarget, DAG, DL); | |||
11657 | SDValue ZeroVector = getZeroVector(VT, Subtarget, DAG, DL); | |||
11658 | SDValue ExpandedVector = IsLeftZeroSide ? V2 : V1; | |||
11659 | return DAG.getNode(X86ISD::EXPAND, DL, VT, ExpandedVector, ZeroVector, VMask); | |||
11660 | } | |||
11661 | ||||
11662 | static bool matchShuffleWithUNPCK(MVT VT, SDValue &V1, SDValue &V2, | |||
11663 | unsigned &UnpackOpcode, bool IsUnary, | |||
11664 | ArrayRef<int> TargetMask, const SDLoc &DL, | |||
11665 | SelectionDAG &DAG, | |||
11666 | const X86Subtarget &Subtarget) { | |||
11667 | int NumElts = VT.getVectorNumElements(); | |||
11668 | ||||
11669 | bool Undef1 = true, Undef2 = true, Zero1 = true, Zero2 = true; | |||
11670 | for (int i = 0; i != NumElts; i += 2) { | |||
11671 | int M1 = TargetMask[i + 0]; | |||
11672 | int M2 = TargetMask[i + 1]; | |||
11673 | Undef1 &= (SM_SentinelUndef == M1); | |||
11674 | Undef2 &= (SM_SentinelUndef == M2); | |||
11675 | Zero1 &= isUndefOrZero(M1); | |||
11676 | Zero2 &= isUndefOrZero(M2); | |||
11677 | } | |||
11678 | assert(!((Undef1 || Zero1) && (Undef2 || Zero2)) &&(static_cast <bool> (!((Undef1 || Zero1) && (Undef2 || Zero2)) && "Zeroable shuffle detected") ? void (0 ) : __assert_fail ("!((Undef1 || Zero1) && (Undef2 || Zero2)) && \"Zeroable shuffle detected\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11679, __extension__ __PRETTY_FUNCTION__)) | |||
11679 | "Zeroable shuffle detected")(static_cast <bool> (!((Undef1 || Zero1) && (Undef2 || Zero2)) && "Zeroable shuffle detected") ? void (0 ) : __assert_fail ("!((Undef1 || Zero1) && (Undef2 || Zero2)) && \"Zeroable shuffle detected\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11679, __extension__ __PRETTY_FUNCTION__)); | |||
11680 | ||||
11681 | // Attempt to match the target mask against the unpack lo/hi mask patterns. | |||
11682 | SmallVector<int, 64> Unpckl, Unpckh; | |||
11683 | createUnpackShuffleMask(VT, Unpckl, /* Lo = */ true, IsUnary); | |||
11684 | if (isTargetShuffleEquivalent(VT, TargetMask, Unpckl, V1, | |||
11685 | (IsUnary ? V1 : V2))) { | |||
11686 | UnpackOpcode = X86ISD::UNPCKL; | |||
11687 | V2 = (Undef2 ? DAG.getUNDEF(VT) : (IsUnary ? V1 : V2)); | |||
11688 | V1 = (Undef1 ? DAG.getUNDEF(VT) : V1); | |||
11689 | return true; | |||
11690 | } | |||
11691 | ||||
11692 | createUnpackShuffleMask(VT, Unpckh, /* Lo = */ false, IsUnary); | |||
11693 | if (isTargetShuffleEquivalent(VT, TargetMask, Unpckh, V1, | |||
11694 | (IsUnary ? V1 : V2))) { | |||
11695 | UnpackOpcode = X86ISD::UNPCKH; | |||
11696 | V2 = (Undef2 ? DAG.getUNDEF(VT) : (IsUnary ? V1 : V2)); | |||
11697 | V1 = (Undef1 ? DAG.getUNDEF(VT) : V1); | |||
11698 | return true; | |||
11699 | } | |||
11700 | ||||
11701 | // If an unary shuffle, attempt to match as an unpack lo/hi with zero. | |||
11702 | if (IsUnary && (Zero1 || Zero2)) { | |||
11703 | // Don't bother if we can blend instead. | |||
11704 | if ((Subtarget.hasSSE41() || VT == MVT::v2i64 || VT == MVT::v2f64) && | |||
11705 | isSequentialOrUndefOrZeroInRange(TargetMask, 0, NumElts, 0)) | |||
11706 | return false; | |||
11707 | ||||
11708 | bool MatchLo = true, MatchHi = true; | |||
11709 | for (int i = 0; (i != NumElts) && (MatchLo || MatchHi); ++i) { | |||
11710 | int M = TargetMask[i]; | |||
11711 | ||||
11712 | // Ignore if the input is known to be zero or the index is undef. | |||
11713 | if ((((i & 1) == 0) && Zero1) || (((i & 1) == 1) && Zero2) || | |||
11714 | (M == SM_SentinelUndef)) | |||
11715 | continue; | |||
11716 | ||||
11717 | MatchLo &= (M == Unpckl[i]); | |||
11718 | MatchHi &= (M == Unpckh[i]); | |||
11719 | } | |||
11720 | ||||
11721 | if (MatchLo || MatchHi) { | |||
11722 | UnpackOpcode = MatchLo ? X86ISD::UNPCKL : X86ISD::UNPCKH; | |||
11723 | V2 = Zero2 ? getZeroVector(VT, Subtarget, DAG, DL) : V1; | |||
11724 | V1 = Zero1 ? getZeroVector(VT, Subtarget, DAG, DL) : V1; | |||
11725 | return true; | |||
11726 | } | |||
11727 | } | |||
11728 | ||||
11729 | // If a binary shuffle, commute and try again. | |||
11730 | if (!IsUnary) { | |||
11731 | ShuffleVectorSDNode::commuteMask(Unpckl); | |||
11732 | if (isTargetShuffleEquivalent(VT, TargetMask, Unpckl)) { | |||
11733 | UnpackOpcode = X86ISD::UNPCKL; | |||
11734 | std::swap(V1, V2); | |||
11735 | return true; | |||
11736 | } | |||
11737 | ||||
11738 | ShuffleVectorSDNode::commuteMask(Unpckh); | |||
11739 | if (isTargetShuffleEquivalent(VT, TargetMask, Unpckh)) { | |||
11740 | UnpackOpcode = X86ISD::UNPCKH; | |||
11741 | std::swap(V1, V2); | |||
11742 | return true; | |||
11743 | } | |||
11744 | } | |||
11745 | ||||
11746 | return false; | |||
11747 | } | |||
11748 | ||||
11749 | // X86 has dedicated unpack instructions that can handle specific blend | |||
11750 | // operations: UNPCKH and UNPCKL. | |||
11751 | static SDValue lowerShuffleWithUNPCK(const SDLoc &DL, MVT VT, | |||
11752 | ArrayRef<int> Mask, SDValue V1, SDValue V2, | |||
11753 | SelectionDAG &DAG) { | |||
11754 | SmallVector<int, 8> Unpckl; | |||
11755 | createUnpackShuffleMask(VT, Unpckl, /* Lo = */ true, /* Unary = */ false); | |||
11756 | if (isShuffleEquivalent(Mask, Unpckl, V1, V2)) | |||
11757 | return DAG.getNode(X86ISD::UNPCKL, DL, VT, V1, V2); | |||
11758 | ||||
11759 | SmallVector<int, 8> Unpckh; | |||
11760 | createUnpackShuffleMask(VT, Unpckh, /* Lo = */ false, /* Unary = */ false); | |||
11761 | if (isShuffleEquivalent(Mask, Unpckh, V1, V2)) | |||
11762 | return DAG.getNode(X86ISD::UNPCKH, DL, VT, V1, V2); | |||
11763 | ||||
11764 | // Commute and try again. | |||
11765 | ShuffleVectorSDNode::commuteMask(Unpckl); | |||
11766 | if (isShuffleEquivalent(Mask, Unpckl, V1, V2)) | |||
11767 | return DAG.getNode(X86ISD::UNPCKL, DL, VT, V2, V1); | |||
11768 | ||||
11769 | ShuffleVectorSDNode::commuteMask(Unpckh); | |||
11770 | if (isShuffleEquivalent(Mask, Unpckh, V1, V2)) | |||
11771 | return DAG.getNode(X86ISD::UNPCKH, DL, VT, V2, V1); | |||
11772 | ||||
11773 | return SDValue(); | |||
11774 | } | |||
11775 | ||||
11776 | /// Check if the mask can be mapped to a preliminary shuffle (vperm 64-bit) | |||
11777 | /// followed by unpack 256-bit. | |||
11778 | static SDValue lowerShuffleWithUNPCK256(const SDLoc &DL, MVT VT, | |||
11779 | ArrayRef<int> Mask, SDValue V1, | |||
11780 | SDValue V2, SelectionDAG &DAG) { | |||
11781 | SmallVector<int, 32> Unpckl, Unpckh; | |||
11782 | createSplat2ShuffleMask(VT, Unpckl, /* Lo */ true); | |||
11783 | createSplat2ShuffleMask(VT, Unpckh, /* Lo */ false); | |||
11784 | ||||
11785 | unsigned UnpackOpcode; | |||
11786 | if (isShuffleEquivalent(Mask, Unpckl, V1, V2)) | |||
11787 | UnpackOpcode = X86ISD::UNPCKL; | |||
11788 | else if (isShuffleEquivalent(Mask, Unpckh, V1, V2)) | |||
11789 | UnpackOpcode = X86ISD::UNPCKH; | |||
11790 | else | |||
11791 | return SDValue(); | |||
11792 | ||||
11793 | // This is a "natural" unpack operation (rather than the 128-bit sectored | |||
11794 | // operation implemented by AVX). We need to rearrange 64-bit chunks of the | |||
11795 | // input in order to use the x86 instruction. | |||
11796 | V1 = DAG.getVectorShuffle(MVT::v4f64, DL, DAG.getBitcast(MVT::v4f64, V1), | |||
11797 | DAG.getUNDEF(MVT::v4f64), {0, 2, 1, 3}); | |||
11798 | V1 = DAG.getBitcast(VT, V1); | |||
11799 | return DAG.getNode(UnpackOpcode, DL, VT, V1, V1); | |||
11800 | } | |||
11801 | ||||
11802 | // Check if the mask can be mapped to a TRUNCATE or VTRUNC, truncating the | |||
11803 | // source into the lower elements and zeroing the upper elements. | |||
11804 | static bool matchShuffleAsVTRUNC(MVT &SrcVT, MVT &DstVT, MVT VT, | |||
11805 | ArrayRef<int> Mask, const APInt &Zeroable, | |||
11806 | const X86Subtarget &Subtarget) { | |||
11807 | if (!VT.is512BitVector() && !Subtarget.hasVLX()) | |||
11808 | return false; | |||
11809 | ||||
11810 | unsigned NumElts = Mask.size(); | |||
11811 | unsigned EltSizeInBits = VT.getScalarSizeInBits(); | |||
11812 | unsigned MaxScale = 64 / EltSizeInBits; | |||
11813 | ||||
11814 | for (unsigned Scale = 2; Scale <= MaxScale; Scale += Scale) { | |||
11815 | unsigned SrcEltBits = EltSizeInBits * Scale; | |||
11816 | if (SrcEltBits < 32 && !Subtarget.hasBWI()) | |||
11817 | continue; | |||
11818 | unsigned NumSrcElts = NumElts / Scale; | |||
11819 | if (!isSequentialOrUndefInRange(Mask, 0, NumSrcElts, 0, Scale)) | |||
11820 | continue; | |||
11821 | unsigned UpperElts = NumElts - NumSrcElts; | |||
11822 | if (!Zeroable.extractBits(UpperElts, NumSrcElts).isAllOnes()) | |||
11823 | continue; | |||
11824 | SrcVT = MVT::getIntegerVT(EltSizeInBits * Scale); | |||
11825 | SrcVT = MVT::getVectorVT(SrcVT, NumSrcElts); | |||
11826 | DstVT = MVT::getIntegerVT(EltSizeInBits); | |||
11827 | if ((NumSrcElts * EltSizeInBits) >= 128) { | |||
11828 | // ISD::TRUNCATE | |||
11829 | DstVT = MVT::getVectorVT(DstVT, NumSrcElts); | |||
11830 | } else { | |||
11831 | // X86ISD::VTRUNC | |||
11832 | DstVT = MVT::getVectorVT(DstVT, 128 / EltSizeInBits); | |||
11833 | } | |||
11834 | return true; | |||
11835 | } | |||
11836 | ||||
11837 | return false; | |||
11838 | } | |||
11839 | ||||
11840 | // Helper to create TRUNCATE/VTRUNC nodes, optionally with zero/undef upper | |||
11841 | // element padding to the final DstVT. | |||
11842 | static SDValue getAVX512TruncNode(const SDLoc &DL, MVT DstVT, SDValue Src, | |||
11843 | const X86Subtarget &Subtarget, | |||
11844 | SelectionDAG &DAG, bool ZeroUppers) { | |||
11845 | MVT SrcVT = Src.getSimpleValueType(); | |||
11846 | MVT DstSVT = DstVT.getScalarType(); | |||
11847 | unsigned NumDstElts = DstVT.getVectorNumElements(); | |||
11848 | unsigned NumSrcElts = SrcVT.getVectorNumElements(); | |||
11849 | unsigned DstEltSizeInBits = DstVT.getScalarSizeInBits(); | |||
11850 | ||||
11851 | if (!DAG.getTargetLoweringInfo().isTypeLegal(SrcVT)) | |||
11852 | return SDValue(); | |||
11853 | ||||
11854 | // Perform a direct ISD::TRUNCATE if possible. | |||
11855 | if (NumSrcElts == NumDstElts) | |||
11856 | return DAG.getNode(ISD::TRUNCATE, DL, DstVT, Src); | |||
11857 | ||||
11858 | if (NumSrcElts > NumDstElts) { | |||
11859 | MVT TruncVT = MVT::getVectorVT(DstSVT, NumSrcElts); | |||
11860 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, TruncVT, Src); | |||
11861 | return extractSubVector(Trunc, 0, DAG, DL, DstVT.getSizeInBits()); | |||
11862 | } | |||
11863 | ||||
11864 | if ((NumSrcElts * DstEltSizeInBits) >= 128) { | |||
11865 | MVT TruncVT = MVT::getVectorVT(DstSVT, NumSrcElts); | |||
11866 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, TruncVT, Src); | |||
11867 | return widenSubVector(Trunc, ZeroUppers, Subtarget, DAG, DL, | |||
11868 | DstVT.getSizeInBits()); | |||
11869 | } | |||
11870 | ||||
11871 | // Non-VLX targets must truncate from a 512-bit type, so we need to | |||
11872 | // widen, truncate and then possibly extract the original subvector. | |||
11873 | if (!Subtarget.hasVLX() && !SrcVT.is512BitVector()) { | |||
11874 | SDValue NewSrc = widenSubVector(Src, ZeroUppers, Subtarget, DAG, DL, 512); | |||
11875 | return getAVX512TruncNode(DL, DstVT, NewSrc, Subtarget, DAG, ZeroUppers); | |||
11876 | } | |||
11877 | ||||
11878 | // Fallback to a X86ISD::VTRUNC, padding if necessary. | |||
11879 | MVT TruncVT = MVT::getVectorVT(DstSVT, 128 / DstEltSizeInBits); | |||
11880 | SDValue Trunc = DAG.getNode(X86ISD::VTRUNC, DL, TruncVT, Src); | |||
11881 | if (DstVT != TruncVT) | |||
11882 | Trunc = widenSubVector(Trunc, ZeroUppers, Subtarget, DAG, DL, | |||
11883 | DstVT.getSizeInBits()); | |||
11884 | return Trunc; | |||
11885 | } | |||
11886 | ||||
11887 | // Try to lower trunc+vector_shuffle to a vpmovdb or a vpmovdw instruction. | |||
11888 | // | |||
11889 | // An example is the following: | |||
11890 | // | |||
11891 | // t0: ch = EntryToken | |||
11892 | // t2: v4i64,ch = CopyFromReg t0, Register:v4i64 %0 | |||
11893 | // t25: v4i32 = truncate t2 | |||
11894 | // t41: v8i16 = bitcast t25 | |||
11895 | // t21: v8i16 = BUILD_VECTOR undef:i16, undef:i16, undef:i16, undef:i16, | |||
11896 | // Constant:i16<0>, Constant:i16<0>, Constant:i16<0>, Constant:i16<0> | |||
11897 | // t51: v8i16 = vector_shuffle<0,2,4,6,12,13,14,15> t41, t21 | |||
11898 | // t18: v2i64 = bitcast t51 | |||
11899 | // | |||
11900 | // One can just use a single vpmovdw instruction, without avx512vl we need to | |||
11901 | // use the zmm variant and extract the lower subvector, padding with zeroes. | |||
11902 | // TODO: Merge with lowerShuffleAsVTRUNC. | |||
11903 | static SDValue lowerShuffleWithVPMOV(const SDLoc &DL, MVT VT, SDValue V1, | |||
11904 | SDValue V2, ArrayRef<int> Mask, | |||
11905 | const APInt &Zeroable, | |||
11906 | const X86Subtarget &Subtarget, | |||
11907 | SelectionDAG &DAG) { | |||
11908 | assert((VT == MVT::v16i8 || VT == MVT::v8i16) && "Unexpected VTRUNC type")(static_cast <bool> ((VT == MVT::v16i8 || VT == MVT::v8i16 ) && "Unexpected VTRUNC type") ? void (0) : __assert_fail ("(VT == MVT::v16i8 || VT == MVT::v8i16) && \"Unexpected VTRUNC type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11908, __extension__ __PRETTY_FUNCTION__)); | |||
11909 | if (!Subtarget.hasAVX512()) | |||
11910 | return SDValue(); | |||
11911 | ||||
11912 | unsigned NumElts = VT.getVectorNumElements(); | |||
11913 | unsigned EltSizeInBits = VT.getScalarSizeInBits(); | |||
11914 | unsigned MaxScale = 64 / EltSizeInBits; | |||
11915 | for (unsigned Scale = 2; Scale <= MaxScale; Scale += Scale) { | |||
11916 | unsigned NumSrcElts = NumElts / Scale; | |||
11917 | unsigned UpperElts = NumElts - NumSrcElts; | |||
11918 | if (!isSequentialOrUndefInRange(Mask, 0, NumSrcElts, 0, Scale) || | |||
11919 | !Zeroable.extractBits(UpperElts, NumSrcElts).isAllOnes()) | |||
11920 | continue; | |||
11921 | ||||
11922 | SDValue Src = V1; | |||
11923 | if (!Src.hasOneUse()) | |||
11924 | return SDValue(); | |||
11925 | ||||
11926 | Src = peekThroughOneUseBitcasts(Src); | |||
11927 | if (Src.getOpcode() != ISD::TRUNCATE || | |||
11928 | Src.getScalarValueSizeInBits() != (EltSizeInBits * Scale)) | |||
11929 | return SDValue(); | |||
11930 | Src = Src.getOperand(0); | |||
11931 | ||||
11932 | // VPMOVWB is only available with avx512bw. | |||
11933 | MVT SrcVT = Src.getSimpleValueType(); | |||
11934 | if (SrcVT.getVectorElementType() == MVT::i16 && VT == MVT::v16i8 && | |||
11935 | !Subtarget.hasBWI()) | |||
11936 | return SDValue(); | |||
11937 | ||||
11938 | bool UndefUppers = isUndefInRange(Mask, NumSrcElts, UpperElts); | |||
11939 | return getAVX512TruncNode(DL, VT, Src, Subtarget, DAG, !UndefUppers); | |||
11940 | } | |||
11941 | ||||
11942 | return SDValue(); | |||
11943 | } | |||
11944 | ||||
11945 | // Attempt to match binary shuffle patterns as a truncate. | |||
11946 | static SDValue lowerShuffleAsVTRUNC(const SDLoc &DL, MVT VT, SDValue V1, | |||
11947 | SDValue V2, ArrayRef<int> Mask, | |||
11948 | const APInt &Zeroable, | |||
11949 | const X86Subtarget &Subtarget, | |||
11950 | SelectionDAG &DAG) { | |||
11951 | assert((VT.is128BitVector() || VT.is256BitVector()) &&(static_cast <bool> ((VT.is128BitVector() || VT.is256BitVector ()) && "Unexpected VTRUNC type") ? void (0) : __assert_fail ("(VT.is128BitVector() || VT.is256BitVector()) && \"Unexpected VTRUNC type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11952, __extension__ __PRETTY_FUNCTION__)) | |||
11952 | "Unexpected VTRUNC type")(static_cast <bool> ((VT.is128BitVector() || VT.is256BitVector ()) && "Unexpected VTRUNC type") ? void (0) : __assert_fail ("(VT.is128BitVector() || VT.is256BitVector()) && \"Unexpected VTRUNC type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 11952, __extension__ __PRETTY_FUNCTION__)); | |||
11953 | if (!Subtarget.hasAVX512()) | |||
11954 | return SDValue(); | |||
11955 | ||||
11956 | unsigned NumElts = VT.getVectorNumElements(); | |||
11957 | unsigned EltSizeInBits = VT.getScalarSizeInBits(); | |||
11958 | unsigned MaxScale = 64 / EltSizeInBits; | |||
11959 | for (unsigned Scale = 2; Scale <= MaxScale; Scale += Scale) { | |||
11960 | // TODO: Support non-BWI VPMOVWB truncations? | |||
11961 | unsigned SrcEltBits = EltSizeInBits * Scale; | |||
11962 | if (SrcEltBits < 32 && !Subtarget.hasBWI()) | |||
11963 | continue; | |||
11964 | ||||
11965 | // Match shuffle <0,Scale,2*Scale,..,undef_or_zero,undef_or_zero,...> | |||
11966 | // Bail if the V2 elements are undef. | |||
11967 | unsigned NumHalfSrcElts = NumElts / Scale; | |||
11968 | unsigned NumSrcElts = 2 * NumHalfSrcElts; | |||
11969 | if (!isSequentialOrUndefInRange(Mask, 0, NumSrcElts, 0, Scale) || | |||
11970 | isUndefInRange(Mask, NumHalfSrcElts, NumHalfSrcElts)) | |||
11971 | continue; | |||
11972 | ||||
11973 | // The elements beyond the truncation must be undef/zero. | |||
11974 | unsigned UpperElts = NumElts - NumSrcElts; | |||
11975 | if (UpperElts > 0 && | |||
11976 | !Zeroable.extractBits(UpperElts, NumSrcElts).isAllOnes()) | |||
11977 | continue; | |||
11978 | bool UndefUppers = | |||
11979 | UpperElts > 0 && isUndefInRange(Mask, NumSrcElts, UpperElts); | |||
11980 | ||||
11981 | // As we're using both sources then we need to concat them together | |||
11982 | // and truncate from the double-sized src. | |||
11983 | MVT ConcatVT = MVT::getVectorVT(VT.getScalarType(), NumElts * 2); | |||
11984 | SDValue Src = DAG.getNode(ISD::CONCAT_VECTORS, DL, ConcatVT, V1, V2); | |||
11985 | ||||
11986 | MVT SrcSVT = MVT::getIntegerVT(SrcEltBits); | |||
11987 | MVT SrcVT = MVT::getVectorVT(SrcSVT, NumSrcElts); | |||
11988 | Src = DAG.getBitcast(SrcVT, Src); | |||
11989 | return getAVX512TruncNode(DL, VT, Src, Subtarget, DAG, !UndefUppers); | |||
11990 | } | |||
11991 | ||||
11992 | return SDValue(); | |||
11993 | } | |||
11994 | ||||
11995 | /// Check whether a compaction lowering can be done by dropping even | |||
11996 | /// elements and compute how many times even elements must be dropped. | |||
11997 | /// | |||
11998 | /// This handles shuffles which take every Nth element where N is a power of | |||
11999 | /// two. Example shuffle masks: | |||
12000 | /// | |||
12001 | /// N = 1: 0, 2, 4, 6, 8, 10, 12, 14, 0, 2, 4, 6, 8, 10, 12, 14 | |||
12002 | /// N = 1: 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30 | |||
12003 | /// N = 2: 0, 4, 8, 12, 0, 4, 8, 12, 0, 4, 8, 12, 0, 4, 8, 12 | |||
12004 | /// N = 2: 0, 4, 8, 12, 16, 20, 24, 28, 0, 4, 8, 12, 16, 20, 24, 28 | |||
12005 | /// N = 3: 0, 8, 0, 8, 0, 8, 0, 8, 0, 8, 0, 8, 0, 8, 0, 8 | |||
12006 | /// N = 3: 0, 8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24 | |||
12007 | /// | |||
12008 | /// Any of these lanes can of course be undef. | |||
12009 | /// | |||
12010 | /// This routine only supports N <= 3. | |||
12011 | /// FIXME: Evaluate whether either AVX or AVX-512 have any opportunities here | |||
12012 | /// for larger N. | |||
12013 | /// | |||
12014 | /// \returns N above, or the number of times even elements must be dropped if | |||
12015 | /// there is such a number. Otherwise returns zero. | |||
12016 | static int canLowerByDroppingEvenElements(ArrayRef<int> Mask, | |||
12017 | bool IsSingleInput) { | |||
12018 | // The modulus for the shuffle vector entries is based on whether this is | |||
12019 | // a single input or not. | |||
12020 | int ShuffleModulus = Mask.size() * (IsSingleInput ? 1 : 2); | |||
12021 | assert(isPowerOf2_32((uint32_t)ShuffleModulus) &&(static_cast <bool> (isPowerOf2_32((uint32_t)ShuffleModulus ) && "We should only be called with masks with a power-of-2 size!" ) ? void (0) : __assert_fail ("isPowerOf2_32((uint32_t)ShuffleModulus) && \"We should only be called with masks with a power-of-2 size!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 12022, __extension__ __PRETTY_FUNCTION__)) | |||
12022 | "We should only be called with masks with a power-of-2 size!")(static_cast <bool> (isPowerOf2_32((uint32_t)ShuffleModulus ) && "We should only be called with masks with a power-of-2 size!" ) ? void (0) : __assert_fail ("isPowerOf2_32((uint32_t)ShuffleModulus) && \"We should only be called with masks with a power-of-2 size!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 12022, __extension__ __PRETTY_FUNCTION__)); | |||
12023 | ||||
12024 | uint64_t ModMask = (uint64_t)ShuffleModulus - 1; | |||
12025 | ||||
12026 | // We track whether the input is viable for all power-of-2 strides 2^1, 2^2, | |||
12027 | // and 2^3 simultaneously. This is because we may have ambiguity with | |||
12028 | // partially undef inputs. | |||
12029 | bool ViableForN[3] = {true, true, true}; | |||
12030 | ||||
12031 | for (int i = 0, e = Mask.size(); i < e; ++i) { | |||
12032 | // Ignore undef lanes, we'll optimistically collapse them to the pattern we | |||
12033 | // want. | |||
12034 | if (Mask[i] < 0) | |||
12035 | continue; | |||
12036 | ||||
12037 | bool IsAnyViable = false; | |||
12038 | for (unsigned j = 0; j != array_lengthof(ViableForN); ++j) | |||
12039 | if (ViableForN[j]) { | |||
12040 | uint64_t N = j + 1; | |||
12041 | ||||
12042 | // The shuffle mask must be equal to (i * 2^N) % M. | |||
12043 | if ((uint64_t)Mask[i] == (((uint64_t)i << N) & ModMask)) | |||
12044 | IsAnyViable = true; | |||
12045 | else | |||
12046 | ViableForN[j] = false; | |||
12047 | } | |||
12048 | // Early exit if we exhaust the possible powers of two. | |||
12049 | if (!IsAnyViable) | |||
12050 | break; | |||
12051 | } | |||
12052 | ||||
12053 | for (unsigned j = 0; j != array_lengthof(ViableForN); ++j) | |||
12054 | if (ViableForN[j]) | |||
12055 | return j + 1; | |||
12056 | ||||
12057 | // Return 0 as there is no viable power of two. | |||
12058 | return 0; | |||
12059 | } | |||
12060 | ||||
12061 | // X86 has dedicated pack instructions that can handle specific truncation | |||
12062 | // operations: PACKSS and PACKUS. | |||
12063 | // Checks for compaction shuffle masks if MaxStages > 1. | |||
12064 | // TODO: Add support for matching multiple PACKSS/PACKUS stages. | |||
12065 | static bool matchShuffleWithPACK(MVT VT, MVT &SrcVT, SDValue &V1, SDValue &V2, | |||
12066 | unsigned &PackOpcode, ArrayRef<int> TargetMask, | |||
12067 | const SelectionDAG &DAG, | |||
12068 | const X86Subtarget &Subtarget, | |||
12069 | unsigned MaxStages = 1) { | |||
12070 | unsigned NumElts = VT.getVectorNumElements(); | |||
12071 | unsigned BitSize = VT.getScalarSizeInBits(); | |||
12072 | assert(0 < MaxStages && MaxStages <= 3 && (BitSize << MaxStages) <= 64 &&(static_cast <bool> (0 < MaxStages && MaxStages <= 3 && (BitSize << MaxStages) <= 64 && "Illegal maximum compaction") ? void (0) : __assert_fail ("0 < MaxStages && MaxStages <= 3 && (BitSize << MaxStages) <= 64 && \"Illegal maximum compaction\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 12073, __extension__ __PRETTY_FUNCTION__)) | |||
12073 | "Illegal maximum compaction")(static_cast <bool> (0 < MaxStages && MaxStages <= 3 && (BitSize << MaxStages) <= 64 && "Illegal maximum compaction") ? void (0) : __assert_fail ("0 < MaxStages && MaxStages <= 3 && (BitSize << MaxStages) <= 64 && \"Illegal maximum compaction\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 12073, __extension__ __PRETTY_FUNCTION__)); | |||
12074 | ||||
12075 | auto MatchPACK = [&](SDValue N1, SDValue N2, MVT PackVT) { | |||
12076 | unsigned NumSrcBits = PackVT.getScalarSizeInBits(); | |||
12077 | unsigned NumPackedBits = NumSrcBits - BitSize; | |||
12078 | N1 = peekThroughBitcasts(N1); | |||
12079 | N2 = peekThroughBitcasts(N2); | |||
12080 | unsigned NumBits1 = N1.getScalarValueSizeInBits(); | |||
12081 | unsigned NumBits2 = N2.getScalarValueSizeInBits(); | |||
12082 | bool IsZero1 = llvm::isNullOrNullSplat(N1, /*AllowUndefs*/ false); | |||
12083 | bool IsZero2 = llvm::isNullOrNullSplat(N2, /*AllowUndefs*/ false); | |||
12084 | if ((!N1.isUndef() && !IsZero1 && NumBits1 != NumSrcBits) || | |||
12085 | (!N2.isUndef() && !IsZero2 && NumBits2 != NumSrcBits)) | |||
12086 | return false; | |||
12087 | if (Subtarget.hasSSE41() || BitSize == 8) { | |||
12088 | APInt ZeroMask = APInt::getHighBitsSet(NumSrcBits, NumPackedBits); | |||
12089 | if ((N1.isUndef() || IsZero1 || DAG.MaskedValueIsZero(N1, ZeroMask)) && | |||
12090 | (N2.isUndef() || IsZero2 || DAG.MaskedValueIsZero(N2, ZeroMask))) { | |||
12091 | V1 = N1; | |||
12092 | V2 = N2; | |||
12093 | SrcVT = PackVT; | |||
12094 | PackOpcode = X86ISD::PACKUS; | |||
12095 | return true; | |||
12096 | } | |||
12097 | } | |||
12098 | bool IsAllOnes1 = llvm::isAllOnesOrAllOnesSplat(N1, /*AllowUndefs*/ false); | |||
12099 | bool IsAllOnes2 = llvm::isAllOnesOrAllOnesSplat(N2, /*AllowUndefs*/ false); | |||
12100 | if ((N1.isUndef() || IsZero1 || IsAllOnes1 || | |||
12101 | DAG.ComputeNumSignBits(N1) > NumPackedBits) && | |||
12102 | (N2.isUndef() || IsZero2 || IsAllOnes2 || | |||
12103 | DAG.ComputeNumSignBits(N2) > NumPackedBits)) { | |||
12104 | V1 = N1; | |||
12105 | V2 = N2; | |||
12106 | SrcVT = PackVT; | |||
12107 | PackOpcode = X86ISD::PACKSS; | |||
12108 | return true; | |||
12109 | } | |||
12110 | return false; | |||
12111 | }; | |||
12112 | ||||
12113 | // Attempt to match against wider and wider compaction patterns. | |||
12114 | for (unsigned NumStages = 1; NumStages <= MaxStages; ++NumStages) { | |||
12115 | MVT PackSVT = MVT::getIntegerVT(BitSize << NumStages); | |||
12116 | MVT PackVT = MVT::getVectorVT(PackSVT, NumElts >> NumStages); | |||
12117 | ||||
12118 | // Try binary shuffle. | |||
12119 | SmallVector<int, 32> BinaryMask; | |||
12120 | createPackShuffleMask(VT, BinaryMask, false, NumStages); | |||
12121 | if (isTargetShuffleEquivalent(VT, TargetMask, BinaryMask, V1, V2)) | |||
12122 | if (MatchPACK(V1, V2, PackVT)) | |||
12123 | return true; | |||
12124 | ||||
12125 | // Try unary shuffle. | |||
12126 | SmallVector<int, 32> UnaryMask; | |||
12127 | createPackShuffleMask(VT, UnaryMask, true, NumStages); | |||
12128 | if (isTargetShuffleEquivalent(VT, TargetMask, UnaryMask, V1)) | |||
12129 | if (MatchPACK(V1, V1, PackVT)) | |||
12130 | return true; | |||
12131 | } | |||
12132 | ||||
12133 | return false; | |||
12134 | } | |||
12135 | ||||
12136 | static SDValue lowerShuffleWithPACK(const SDLoc &DL, MVT VT, ArrayRef<int> Mask, | |||
12137 | SDValue V1, SDValue V2, SelectionDAG &DAG, | |||
12138 | const X86Subtarget &Subtarget) { | |||
12139 | MVT PackVT; | |||
12140 | unsigned PackOpcode; | |||
12141 | unsigned SizeBits = VT.getSizeInBits(); | |||
12142 | unsigned EltBits = VT.getScalarSizeInBits(); | |||
12143 | unsigned MaxStages = Log2_32(64 / EltBits); | |||
12144 | if (!matchShuffleWithPACK(VT, PackVT, V1, V2, PackOpcode, Mask, DAG, | |||
12145 | Subtarget, MaxStages)) | |||
12146 | return SDValue(); | |||
12147 | ||||
12148 | unsigned CurrentEltBits = PackVT.getScalarSizeInBits(); | |||
12149 | unsigned NumStages = Log2_32(CurrentEltBits / EltBits); | |||
12150 | ||||
12151 | // Don't lower multi-stage packs on AVX512, truncation is better. | |||
12152 | if (NumStages != 1 && SizeBits == 128 && Subtarget.hasVLX()) | |||
12153 | return SDValue(); | |||
12154 | ||||
12155 | // Pack to the largest type possible: | |||
12156 | // vXi64/vXi32 -> PACK*SDW and vXi16 -> PACK*SWB. | |||
12157 | unsigned MaxPackBits = 16; | |||
12158 | if (CurrentEltBits > 16 && | |||
12159 | (PackOpcode == X86ISD::PACKSS || Subtarget.hasSSE41())) | |||
12160 | MaxPackBits = 32; | |||
12161 | ||||
12162 | // Repeatedly pack down to the target size. | |||
12163 | SDValue Res; | |||
12164 | for (unsigned i = 0; i != NumStages; ++i) { | |||
12165 | unsigned SrcEltBits = std::min(MaxPackBits, CurrentEltBits); | |||
12166 | unsigned NumSrcElts = SizeBits / SrcEltBits; | |||
12167 | MVT SrcSVT = MVT::getIntegerVT(SrcEltBits); | |||
12168 | MVT DstSVT = MVT::getIntegerVT(SrcEltBits / 2); | |||
12169 | MVT SrcVT = MVT::getVectorVT(SrcSVT, NumSrcElts); | |||
12170 | MVT DstVT = MVT::getVectorVT(DstSVT, NumSrcElts * 2); | |||
12171 | Res = DAG.getNode(PackOpcode, DL, DstVT, DAG.getBitcast(SrcVT, V1), | |||
12172 | DAG.getBitcast(SrcVT, V2)); | |||
12173 | V1 = V2 = Res; | |||
12174 | CurrentEltBits /= 2; | |||
12175 | } | |||
12176 | assert(Res && Res.getValueType() == VT &&(static_cast <bool> (Res && Res.getValueType() == VT && "Failed to lower compaction shuffle") ? void ( 0) : __assert_fail ("Res && Res.getValueType() == VT && \"Failed to lower compaction shuffle\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 12177, __extension__ __PRETTY_FUNCTION__)) | |||
12177 | "Failed to lower compaction shuffle")(static_cast <bool> (Res && Res.getValueType() == VT && "Failed to lower compaction shuffle") ? void ( 0) : __assert_fail ("Res && Res.getValueType() == VT && \"Failed to lower compaction shuffle\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 12177, __extension__ __PRETTY_FUNCTION__)); | |||
12178 | return Res; | |||
12179 | } | |||
12180 | ||||
12181 | /// Try to emit a bitmask instruction for a shuffle. | |||
12182 | /// | |||
12183 | /// This handles cases where we can model a blend exactly as a bitmask due to | |||
12184 | /// one of the inputs being zeroable. | |||
12185 | static SDValue lowerShuffleAsBitMask(const SDLoc &DL, MVT VT, SDValue V1, | |||
12186 | SDValue V2, ArrayRef<int> Mask, | |||
12187 | const APInt &Zeroable, | |||
12188 | const X86Subtarget &Subtarget, | |||
12189 | SelectionDAG &DAG) { | |||
12190 | MVT MaskVT = VT; | |||
12191 | MVT EltVT = VT.getVectorElementType(); | |||
12192 | SDValue Zero, AllOnes; | |||
12193 | // Use f64 if i64 isn't legal. | |||
12194 | if (EltVT == MVT::i64 && !Subtarget.is64Bit()) { | |||
12195 | EltVT = MVT::f64; | |||
12196 | MaskVT = MVT::getVectorVT(EltVT, Mask.size()); | |||
12197 | } | |||
12198 | ||||
12199 | MVT LogicVT = VT; | |||
12200 | if (EltVT == MVT::f32 || EltVT == MVT::f64) { | |||
12201 | Zero = DAG.getConstantFP(0.0, DL, EltVT); | |||
12202 | APFloat AllOnesValue = | |||
12203 | APFloat::getAllOnesValue(SelectionDAG::EVTToAPFloatSemantics(EltVT)); | |||
12204 | AllOnes = DAG.getConstantFP(AllOnesValue, DL, EltVT); | |||
12205 | LogicVT = | |||
12206 | MVT::getVectorVT(EltVT == MVT::f64 ? MVT::i64 : MVT::i32, Mask.size()); | |||
12207 | } else { | |||
12208 | Zero = DAG.getConstant(0, DL, EltVT); | |||
12209 | AllOnes = DAG.getAllOnesConstant(DL, EltVT); | |||
12210 | } | |||
12211 | ||||
12212 | SmallVector<SDValue, 16> VMaskOps(Mask.size(), Zero); | |||
12213 | SDValue V; | |||
12214 | for (int i = 0, Size = Mask.size(); i < Size; ++i) { | |||
12215 | if (Zeroable[i]) | |||
12216 | continue; | |||
12217 | if (Mask[i] % Size != i) | |||
12218 | return SDValue(); // Not a blend. | |||
12219 | if (!V) | |||
12220 | V = Mask[i] < Size ? V1 : V2; | |||
12221 | else if (V != (Mask[i] < Size ? V1 : V2)) | |||
12222 | return SDValue(); // Can only let one input through the mask. | |||
12223 | ||||
12224 | VMaskOps[i] = AllOnes; | |||
12225 | } | |||
12226 | if (!V) | |||
12227 | return SDValue(); // No non-zeroable elements! | |||
12228 | ||||
12229 | SDValue VMask = DAG.getBuildVector(MaskVT, DL, VMaskOps); | |||
12230 | VMask = DAG.getBitcast(LogicVT, VMask); | |||
12231 | V = DAG.getBitcast(LogicVT, V); | |||
12232 | SDValue And = DAG.getNode(ISD::AND, DL, LogicVT, V, VMask); | |||
12233 | return DAG.getBitcast(VT, And); | |||
12234 | } | |||
12235 | ||||
12236 | /// Try to emit a blend instruction for a shuffle using bit math. | |||
12237 | /// | |||
12238 | /// This is used as a fallback approach when first class blend instructions are | |||
12239 | /// unavailable. Currently it is only suitable for integer vectors, but could | |||
12240 | /// be generalized for floating point vectors if desirable. | |||
12241 | static SDValue lowerShuffleAsBitBlend(const SDLoc &DL, MVT VT, SDValue V1, | |||
12242 | SDValue V2, ArrayRef<int> Mask, | |||
12243 | SelectionDAG &DAG) { | |||
12244 | assert(VT.isInteger() && "Only supports integer vector types!")(static_cast <bool> (VT.isInteger() && "Only supports integer vector types!" ) ? void (0) : __assert_fail ("VT.isInteger() && \"Only supports integer vector types!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 12244, __extension__ __PRETTY_FUNCTION__)); | |||
12245 | MVT EltVT = VT.getVectorElementType(); | |||
12246 | SDValue Zero = DAG.getConstant(0, DL, EltVT); | |||
12247 | SDValue AllOnes = DAG.getAllOnesConstant(DL, EltVT); | |||
12248 | SmallVector<SDValue, 16> MaskOps; | |||
12249 | for (int i = 0, Size = Mask.size(); i < Size; ++i) { | |||
12250 | if (Mask[i] >= 0 && Mask[i] != i && Mask[i] != i + Size) | |||
12251 | return SDValue(); // Shuffled input! | |||
12252 | MaskOps.push_back(Mask[i] < Size ? AllOnes : Zero); | |||
12253 | } | |||
12254 | ||||
12255 | SDValue V1Mask = DAG.getBuildVector(VT, DL, MaskOps); | |||
12256 | V1 = DAG.getNode(ISD::AND, DL, VT, V1, V1Mask); | |||
12257 | V2 = DAG.getNode(X86ISD::ANDNP, DL, VT, V1Mask, V2); | |||
12258 | return DAG.getNode(ISD::OR, DL, VT, V1, V2); | |||
12259 | } | |||
12260 | ||||
12261 | static SDValue getVectorMaskingNode(SDValue Op, SDValue Mask, | |||
12262 | SDValue PreservedSrc, | |||
12263 | const X86Subtarget &Subtarget, | |||
12264 | SelectionDAG &DAG); | |||
12265 | ||||
12266 | static bool matchShuffleAsBlend(SDValue V1, SDValue V2, | |||
12267 | MutableArrayRef<int> Mask, | |||
12268 | const APInt &Zeroable, bool &ForceV1Zero, | |||
12269 | bool &ForceV2Zero, uint64_t &BlendMask) { | |||
12270 | bool V1IsZeroOrUndef = | |||
12271 | V1.isUndef() || ISD::isBuildVectorAllZeros(V1.getNode()); | |||
12272 | bool V2IsZeroOrUndef = | |||
12273 | V2.isUndef() || ISD::isBuildVectorAllZeros(V2.getNode()); | |||
12274 | ||||
12275 | BlendMask = 0; | |||
12276 | ForceV1Zero = false, ForceV2Zero = false; | |||
12277 | assert(Mask.size() <= 64 && "Shuffle mask too big for blend mask")(static_cast <bool> (Mask.size() <= 64 && "Shuffle mask too big for blend mask" ) ? void (0) : __assert_fail ("Mask.size() <= 64 && \"Shuffle mask too big for blend mask\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 12277, __extension__ __PRETTY_FUNCTION__)); | |||
12278 | ||||
12279 | // Attempt to generate the binary blend mask. If an input is zero then | |||
12280 | // we can use any lane. | |||
12281 | for (int i = 0, Size = Mask.size(); i < Size; ++i) { | |||
12282 | int M = Mask[i]; | |||
12283 | if (M == SM_SentinelUndef) | |||
12284 | continue; | |||
12285 | if (M == i || | |||
12286 | (0 <= M && M < Size && IsElementEquivalent(Size, V1, V1, M, i))) { | |||
12287 | Mask[i] = i; | |||
12288 | continue; | |||
12289 | } | |||
12290 | if (M == (i + Size) || | |||
12291 | (Size <= M && IsElementEquivalent(Size, V2, V2, M - Size, i))) { | |||
12292 | BlendMask |= 1ull << i; | |||
12293 | Mask[i] = i + Size; | |||
12294 | continue; | |||
12295 | } | |||
12296 | if (Zeroable[i]) { | |||
12297 | if (V1IsZeroOrUndef) { | |||
12298 | ForceV1Zero = true; | |||
12299 | Mask[i] = i; | |||
12300 | continue; | |||
12301 | } | |||
12302 | if (V2IsZeroOrUndef) { | |||
12303 | ForceV2Zero = true; | |||
12304 | BlendMask |= 1ull << i; | |||
12305 | Mask[i] = i + Size; | |||
12306 | continue; | |||
12307 | } | |||
12308 | } | |||
12309 | return false; | |||
12310 | } | |||
12311 | return true; | |||
12312 | } | |||
12313 | ||||
12314 | static uint64_t scaleVectorShuffleBlendMask(uint64_t BlendMask, int Size, | |||
12315 | int Scale) { | |||
12316 | uint64_t ScaledMask = 0; | |||
12317 | for (int i = 0; i != Size; ++i) | |||
12318 | if (BlendMask & (1ull << i)) | |||
12319 | ScaledMask |= ((1ull << Scale) - 1) << (i * Scale); | |||
12320 | return ScaledMask; | |||
12321 | } | |||
12322 | ||||
12323 | /// Try to emit a blend instruction for a shuffle. | |||
12324 | /// | |||
12325 | /// This doesn't do any checks for the availability of instructions for blending | |||
12326 | /// these values. It relies on the availability of the X86ISD::BLENDI pattern to | |||
12327 | /// be matched in the backend with the type given. What it does check for is | |||
12328 | /// that the shuffle mask is a blend, or convertible into a blend with zero. | |||
12329 | static SDValue lowerShuffleAsBlend(const SDLoc &DL, MVT VT, SDValue V1, | |||
12330 | SDValue V2, ArrayRef<int> Original, | |||
12331 | const APInt &Zeroable, | |||
12332 | const X86Subtarget &Subtarget, | |||
12333 | SelectionDAG &DAG) { | |||
12334 | uint64_t BlendMask = 0; | |||
12335 | bool ForceV1Zero = false, ForceV2Zero = false; | |||
12336 | SmallVector<int, 64> Mask(Original.begin(), Original.end()); | |||
12337 | if (!matchShuffleAsBlend(V1, V2, Mask, Zeroable, ForceV1Zero, ForceV2Zero, | |||
12338 | BlendMask)) | |||
12339 | return SDValue(); | |||
12340 | ||||
12341 | // Create a REAL zero vector - ISD::isBuildVectorAllZeros allows UNDEFs. | |||
12342 | if (ForceV1Zero) | |||
12343 | V1 = getZeroVector(VT, Subtarget, DAG, DL); | |||
12344 | if (ForceV2Zero) | |||
12345 | V2 = getZeroVector(VT, Subtarget, DAG, DL); | |||
12346 | ||||
12347 | switch (VT.SimpleTy) { | |||
12348 | case MVT::v4i64: | |||
12349 | case MVT::v8i32: | |||
12350 | assert(Subtarget.hasAVX2() && "256-bit integer blends require AVX2!")(static_cast <bool> (Subtarget.hasAVX2() && "256-bit integer blends require AVX2!" ) ? void (0) : __assert_fail ("Subtarget.hasAVX2() && \"256-bit integer blends require AVX2!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 12350, __extension__ __PRETTY_FUNCTION__)); | |||
12351 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
12352 | case MVT::v4f64: | |||
12353 | case MVT::v8f32: | |||
12354 | assert(Subtarget.hasAVX() && "256-bit float blends require AVX!")(static_cast <bool> (Subtarget.hasAVX() && "256-bit float blends require AVX!" ) ? void (0) : __assert_fail ("Subtarget.hasAVX() && \"256-bit float blends require AVX!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 12354, __extension__ __PRETTY_FUNCTION__)); | |||
12355 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
12356 | case MVT::v2f64: | |||
12357 | case MVT::v2i64: | |||
12358 | case MVT::v4f32: | |||
12359 | case MVT::v4i32: | |||
12360 | case MVT::v8i16: | |||
12361 | assert(Subtarget.hasSSE41() && "128-bit blends require SSE41!")(static_cast <bool> (Subtarget.hasSSE41() && "128-bit blends require SSE41!" ) ? void (0) : __assert_fail ("Subtarget.hasSSE41() && \"128-bit blends require SSE41!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 12361, __extension__ __PRETTY_FUNCTION__)); | |||
12362 | return DAG.getNode(X86ISD::BLENDI, DL, VT, V1, V2, | |||
12363 | DAG.getTargetConstant(BlendMask, DL, MVT::i8)); | |||
12364 | case MVT::v16i16: { | |||
12365 | assert(Subtarget.hasAVX2() && "v16i16 blends require AVX2!")(static_cast <bool> (Subtarget.hasAVX2() && "v16i16 blends require AVX2!" ) ? void (0) : __assert_fail ("Subtarget.hasAVX2() && \"v16i16 blends require AVX2!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 12365, __extension__ __PRETTY_FUNCTION__)); | |||
12366 | SmallVector<int, 8> RepeatedMask; | |||
12367 | if (is128BitLaneRepeatedShuffleMask(MVT::v16i16, Mask, RepeatedMask)) { | |||
12368 | // We can lower these with PBLENDW which is mirrored across 128-bit lanes. | |||
12369 | assert(RepeatedMask.size() == 8 && "Repeated mask size doesn't match!")(static_cast <bool> (RepeatedMask.size() == 8 && "Repeated mask size doesn't match!") ? void (0) : __assert_fail ("RepeatedMask.size() == 8 && \"Repeated mask size doesn't match!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 12369, __extension__ __PRETTY_FUNCTION__)); | |||
12370 | BlendMask = 0; | |||
12371 | for (int i = 0; i < 8; ++i) | |||
12372 | if (RepeatedMask[i] >= 8) | |||
12373 | BlendMask |= 1ull << i; | |||
12374 | return DAG.getNode(X86ISD::BLENDI, DL, MVT::v16i16, V1, V2, | |||
12375 | DAG.getTargetConstant(BlendMask, DL, MVT::i8)); | |||
12376 | } | |||
12377 | // Use PBLENDW for lower/upper lanes and then blend lanes. | |||
12378 | // TODO - we should allow 2 PBLENDW here and leave shuffle combine to | |||
12379 | // merge to VSELECT where useful. | |||
12380 | uint64_t LoMask = BlendMask & 0xFF; | |||
12381 | uint64_t HiMask = (BlendMask >> 8) & 0xFF; | |||
12382 | if (LoMask == 0 || LoMask == 255 || HiMask == 0 || HiMask == 255) { | |||
12383 | SDValue Lo = DAG.getNode(X86ISD::BLENDI, DL, MVT::v16i16, V1, V2, | |||
12384 | DAG.getTargetConstant(LoMask, DL, MVT::i8)); | |||
12385 | SDValue Hi = DAG.getNode(X86ISD::BLENDI, DL, MVT::v16i16, V1, V2, | |||
12386 | DAG.getTargetConstant(HiMask, DL, MVT::i8)); | |||
12387 | return DAG.getVectorShuffle( | |||
12388 | MVT::v16i16, DL, Lo, Hi, | |||
12389 | {0, 1, 2, 3, 4, 5, 6, 7, 24, 25, 26, 27, 28, 29, 30, 31}); | |||
12390 | } | |||
12391 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
12392 | } | |||
12393 | case MVT::v32i8: | |||
12394 | assert(Subtarget.hasAVX2() && "256-bit byte-blends require AVX2!")(static_cast <bool> (Subtarget.hasAVX2() && "256-bit byte-blends require AVX2!" ) ? void (0) : __assert_fail ("Subtarget.hasAVX2() && \"256-bit byte-blends require AVX2!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 12394, __extension__ __PRETTY_FUNCTION__)); | |||
12395 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
12396 | case MVT::v16i8: { | |||
12397 | assert(Subtarget.hasSSE41() && "128-bit byte-blends require SSE41!")(static_cast <bool> (Subtarget.hasSSE41() && "128-bit byte-blends require SSE41!" ) ? void (0) : __assert_fail ("Subtarget.hasSSE41() && \"128-bit byte-blends require SSE41!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 12397, __extension__ __PRETTY_FUNCTION__)); | |||
12398 | ||||
12399 | // Attempt to lower to a bitmask if we can. VPAND is faster than VPBLENDVB. | |||
12400 | if (SDValue Masked = lowerShuffleAsBitMask(DL, VT, V1, V2, Mask, Zeroable, | |||
12401 | Subtarget, DAG)) | |||
12402 | return Masked; | |||
12403 | ||||
12404 | if (Subtarget.hasBWI() && Subtarget.hasVLX()) { | |||
12405 | MVT IntegerType = | |||
12406 | MVT::getIntegerVT(std::max((int)VT.getVectorNumElements(), 8)); | |||
12407 | SDValue MaskNode = DAG.getConstant(BlendMask, DL, IntegerType); | |||
12408 | return getVectorMaskingNode(V2, MaskNode, V1, Subtarget, DAG); | |||
12409 | } | |||
12410 | ||||
12411 | // If we have VPTERNLOG, we can use that as a bit blend. | |||
12412 | if (Subtarget.hasVLX()) | |||
12413 | if (SDValue BitBlend = | |||
12414 | lowerShuffleAsBitBlend(DL, VT, V1, V2, Mask, DAG)) | |||
12415 | return BitBlend; | |||
12416 | ||||
12417 | // Scale the blend by the number of bytes per element. | |||
12418 | int Scale = VT.getScalarSizeInBits() / 8; | |||
12419 | ||||
12420 | // This form of blend is always done on bytes. Compute the byte vector | |||
12421 | // type. | |||
12422 | MVT BlendVT = MVT::getVectorVT(MVT::i8, VT.getSizeInBits() / 8); | |||
12423 | ||||
12424 | // x86 allows load folding with blendvb from the 2nd source operand. But | |||
12425 | // we are still using LLVM select here (see comment below), so that's V1. | |||
12426 | // If V2 can be load-folded and V1 cannot be load-folded, then commute to | |||
12427 | // allow that load-folding possibility. | |||
12428 | if (!ISD::isNormalLoad(V1.getNode()) && ISD::isNormalLoad(V2.getNode())) { | |||
12429 | ShuffleVectorSDNode::commuteMask(Mask); | |||
12430 | std::swap(V1, V2); | |||
12431 | } | |||
12432 | ||||
12433 | // Compute the VSELECT mask. Note that VSELECT is really confusing in the | |||
12434 | // mix of LLVM's code generator and the x86 backend. We tell the code | |||
12435 | // generator that boolean values in the elements of an x86 vector register | |||
12436 | // are -1 for true and 0 for false. We then use the LLVM semantics of 'true' | |||
12437 | // mapping a select to operand #1, and 'false' mapping to operand #2. The | |||
12438 | // reality in x86 is that vector masks (pre-AVX-512) use only the high bit | |||
12439 | // of the element (the remaining are ignored) and 0 in that high bit would | |||
12440 | // mean operand #1 while 1 in the high bit would mean operand #2. So while | |||
12441 | // the LLVM model for boolean values in vector elements gets the relevant | |||
12442 | // bit set, it is set backwards and over constrained relative to x86's | |||
12443 | // actual model. | |||
12444 | SmallVector<SDValue, 32> VSELECTMask; | |||
12445 | for (int i = 0, Size = Mask.size(); i < Size; ++i) | |||
12446 | for (int j = 0; j < Scale; ++j) | |||
12447 | VSELECTMask.push_back( | |||
12448 | Mask[i] < 0 ? DAG.getUNDEF(MVT::i8) | |||
12449 | : DAG.getConstant(Mask[i] < Size ? -1 : 0, DL, | |||
12450 | MVT::i8)); | |||
12451 | ||||
12452 | V1 = DAG.getBitcast(BlendVT, V1); | |||
12453 | V2 = DAG.getBitcast(BlendVT, V2); | |||
12454 | return DAG.getBitcast( | |||
12455 | VT, | |||
12456 | DAG.getSelect(DL, BlendVT, DAG.getBuildVector(BlendVT, DL, VSELECTMask), | |||
12457 | V1, V2)); | |||
12458 | } | |||
12459 | case MVT::v16f32: | |||
12460 | case MVT::v8f64: | |||
12461 | case MVT::v8i64: | |||
12462 | case MVT::v16i32: | |||
12463 | case MVT::v32i16: | |||
12464 | case MVT::v64i8: { | |||
12465 | // Attempt to lower to a bitmask if we can. Only if not optimizing for size. | |||
12466 | bool OptForSize = DAG.shouldOptForSize(); | |||
12467 | if (!OptForSize) { | |||
12468 | if (SDValue Masked = lowerShuffleAsBitMask(DL, VT, V1, V2, Mask, Zeroable, | |||
12469 | Subtarget, DAG)) | |||
12470 | return Masked; | |||
12471 | } | |||
12472 | ||||
12473 | // Otherwise load an immediate into a GPR, cast to k-register, and use a | |||
12474 | // masked move. | |||
12475 | MVT IntegerType = | |||
12476 | MVT::getIntegerVT(std::max((int)VT.getVectorNumElements(), 8)); | |||
12477 | SDValue MaskNode = DAG.getConstant(BlendMask, DL, IntegerType); | |||
12478 | return getVectorMaskingNode(V2, MaskNode, V1, Subtarget, DAG); | |||
12479 | } | |||
12480 | default: | |||
12481 | llvm_unreachable("Not a supported integer vector type!")::llvm::llvm_unreachable_internal("Not a supported integer vector type!" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 12481); | |||
12482 | } | |||
12483 | } | |||
12484 | ||||
12485 | /// Try to lower as a blend of elements from two inputs followed by | |||
12486 | /// a single-input permutation. | |||
12487 | /// | |||
12488 | /// This matches the pattern where we can blend elements from two inputs and | |||
12489 | /// then reduce the shuffle to a single-input permutation. | |||
12490 | static SDValue lowerShuffleAsBlendAndPermute(const SDLoc &DL, MVT VT, | |||
12491 | SDValue V1, SDValue V2, | |||
12492 | ArrayRef<int> Mask, | |||
12493 | SelectionDAG &DAG, | |||
12494 | bool ImmBlends = false) { | |||
12495 | // We build up the blend mask while checking whether a blend is a viable way | |||
12496 | // to reduce the shuffle. | |||
12497 | SmallVector<int, 32> BlendMask(Mask.size(), -1); | |||
12498 | SmallVector<int, 32> PermuteMask(Mask.size(), -1); | |||
12499 | ||||
12500 | for (int i = 0, Size = Mask.size(); i < Size; ++i) { | |||
12501 | if (Mask[i] < 0) | |||
12502 | continue; | |||
12503 | ||||
12504 | assert(Mask[i] < Size * 2 && "Shuffle input is out of bounds.")(static_cast <bool> (Mask[i] < Size * 2 && "Shuffle input is out of bounds." ) ? void (0) : __assert_fail ("Mask[i] < Size * 2 && \"Shuffle input is out of bounds.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 12504, __extension__ __PRETTY_FUNCTION__)); | |||
12505 | ||||
12506 | if (BlendMask[Mask[i] % Size] < 0) | |||
12507 | BlendMask[Mask[i] % Size] = Mask[i]; | |||
12508 | else if (BlendMask[Mask[i] % Size] != Mask[i]) | |||
12509 | return SDValue(); // Can't blend in the needed input! | |||
12510 | ||||
12511 | PermuteMask[i] = Mask[i] % Size; | |||
12512 | } | |||
12513 | ||||
12514 | // If only immediate blends, then bail if the blend mask can't be widened to | |||
12515 | // i16. | |||
12516 | unsigned EltSize = VT.getScalarSizeInBits(); | |||
12517 | if (ImmBlends && EltSize == 8 && !canWidenShuffleElements(BlendMask)) | |||
12518 | return SDValue(); | |||
12519 | ||||
12520 | SDValue V = DAG.getVectorShuffle(VT, DL, V1, V2, BlendMask); | |||
12521 | return DAG.getVectorShuffle(VT, DL, V, DAG.getUNDEF(VT), PermuteMask); | |||
12522 | } | |||
12523 | ||||
12524 | /// Try to lower as an unpack of elements from two inputs followed by | |||
12525 | /// a single-input permutation. | |||
12526 | /// | |||
12527 | /// This matches the pattern where we can unpack elements from two inputs and | |||
12528 | /// then reduce the shuffle to a single-input (wider) permutation. | |||
12529 | static SDValue lowerShuffleAsUNPCKAndPermute(const SDLoc &DL, MVT VT, | |||
12530 | SDValue V1, SDValue V2, | |||
12531 | ArrayRef<int> Mask, | |||
12532 | SelectionDAG &DAG) { | |||
12533 | int NumElts = Mask.size(); | |||
12534 | int NumLanes = VT.getSizeInBits() / 128; | |||
12535 | int NumLaneElts = NumElts / NumLanes; | |||
12536 | int NumHalfLaneElts = NumLaneElts / 2; | |||
12537 | ||||
12538 | bool MatchLo = true, MatchHi = true; | |||
12539 | SDValue Ops[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT)}; | |||
12540 | ||||
12541 | // Determine UNPCKL/UNPCKH type and operand order. | |||
12542 | for (int Lane = 0; Lane != NumElts; Lane += NumLaneElts) { | |||
12543 | for (int Elt = 0; Elt != NumLaneElts; ++Elt) { | |||
12544 | int M = Mask[Lane + Elt]; | |||
12545 | if (M < 0) | |||
12546 | continue; | |||
12547 | ||||
12548 | SDValue &Op = Ops[Elt & 1]; | |||
12549 | if (M < NumElts && (Op.isUndef() || Op == V1)) | |||
12550 | Op = V1; | |||
12551 | else if (NumElts <= M && (Op.isUndef() || Op == V2)) | |||
12552 | Op = V2; | |||
12553 | else | |||
12554 | return SDValue(); | |||
12555 | ||||
12556 | int Lo = Lane, Mid = Lane + NumHalfLaneElts, Hi = Lane + NumLaneElts; | |||
12557 | MatchLo &= isUndefOrInRange(M, Lo, Mid) || | |||
12558 | isUndefOrInRange(M, NumElts + Lo, NumElts + Mid); | |||
12559 | MatchHi &= isUndefOrInRange(M, Mid, Hi) || | |||
12560 | isUndefOrInRange(M, NumElts + Mid, NumElts + Hi); | |||
12561 | if (!MatchLo && !MatchHi) | |||
12562 | return SDValue(); | |||
12563 | } | |||
12564 | } | |||
12565 | assert((MatchLo ^ MatchHi) && "Failed to match UNPCKLO/UNPCKHI")(static_cast <bool> ((MatchLo ^ MatchHi) && "Failed to match UNPCKLO/UNPCKHI" ) ? void (0) : __assert_fail ("(MatchLo ^ MatchHi) && \"Failed to match UNPCKLO/UNPCKHI\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 12565, __extension__ __PRETTY_FUNCTION__)); | |||
12566 | ||||
12567 | // Now check that each pair of elts come from the same unpack pair | |||
12568 | // and set the permute mask based on each pair. | |||
12569 | // TODO - Investigate cases where we permute individual elements. | |||
12570 | SmallVector<int, 32> PermuteMask(NumElts, -1); | |||
12571 | for (int Lane = 0; Lane != NumElts; Lane += NumLaneElts) { | |||
12572 | for (int Elt = 0; Elt != NumLaneElts; Elt += 2) { | |||
12573 | int M0 = Mask[Lane + Elt + 0]; | |||
12574 | int M1 = Mask[Lane + Elt + 1]; | |||
12575 | if (0 <= M0 && 0 <= M1 && | |||
12576 | (M0 % NumHalfLaneElts) != (M1 % NumHalfLaneElts)) | |||
12577 | return SDValue(); | |||
12578 | if (0 <= M0) | |||
12579 | PermuteMask[Lane + Elt + 0] = Lane + (2 * (M0 % NumHalfLaneElts)); | |||
12580 | if (0 <= M1) | |||
12581 | PermuteMask[Lane + Elt + 1] = Lane + (2 * (M1 % NumHalfLaneElts)) + 1; | |||
12582 | } | |||
12583 | } | |||
12584 | ||||
12585 | unsigned UnpckOp = MatchLo ? X86ISD::UNPCKL : X86ISD::UNPCKH; | |||
12586 | SDValue Unpck = DAG.getNode(UnpckOp, DL, VT, Ops); | |||
12587 | return DAG.getVectorShuffle(VT, DL, Unpck, DAG.getUNDEF(VT), PermuteMask); | |||
12588 | } | |||
12589 | ||||
12590 | /// Helper to form a PALIGNR-based rotate+permute, merging 2 inputs and then | |||
12591 | /// permuting the elements of the result in place. | |||
12592 | static SDValue lowerShuffleAsByteRotateAndPermute( | |||
12593 | const SDLoc &DL, MVT VT, SDValue V1, SDValue V2, ArrayRef<int> Mask, | |||
12594 | const X86Subtarget &Subtarget, SelectionDAG &DAG) { | |||
12595 | if ((VT.is128BitVector() && !Subtarget.hasSSSE3()) || | |||
12596 | (VT.is256BitVector() && !Subtarget.hasAVX2()) || | |||
12597 | (VT.is512BitVector() && !Subtarget.hasBWI())) | |||
12598 | return SDValue(); | |||
12599 | ||||
12600 | // We don't currently support lane crossing permutes. | |||
12601 | if (is128BitLaneCrossingShuffleMask(VT, Mask)) | |||
12602 | return SDValue(); | |||
12603 | ||||
12604 | int Scale = VT.getScalarSizeInBits() / 8; | |||
12605 | int NumLanes = VT.getSizeInBits() / 128; | |||
12606 | int NumElts = VT.getVectorNumElements(); | |||
12607 | int NumEltsPerLane = NumElts / NumLanes; | |||
12608 | ||||
12609 | // Determine range of mask elts. | |||
12610 | bool Blend1 = true; | |||
12611 | bool Blend2 = true; | |||
12612 | std::pair<int, int> Range1 = std::make_pair(INT_MAX2147483647, INT_MIN(-2147483647 -1)); | |||
12613 | std::pair<int, int> Range2 = std::make_pair(INT_MAX2147483647, INT_MIN(-2147483647 -1)); | |||
12614 | for (int Lane = 0; Lane != NumElts; Lane += NumEltsPerLane) { | |||
12615 | for (int Elt = 0; Elt != NumEltsPerLane; ++Elt) { | |||
12616 | int M = Mask[Lane + Elt]; | |||
12617 | if (M < 0) | |||
12618 | continue; | |||
12619 | if (M < NumElts) { | |||
12620 | Blend1 &= (M == (Lane + Elt)); | |||
12621 | assert(Lane <= M && M < (Lane + NumEltsPerLane) && "Out of range mask")(static_cast <bool> (Lane <= M && M < (Lane + NumEltsPerLane) && "Out of range mask") ? void (0) : __assert_fail ("Lane <= M && M < (Lane + NumEltsPerLane) && \"Out of range mask\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 12621, __extension__ __PRETTY_FUNCTION__)); | |||
12622 | M = M % NumEltsPerLane; | |||
12623 | Range1.first = std::min(Range1.first, M); | |||
12624 | Range1.second = std::max(Range1.second, M); | |||
12625 | } else { | |||
12626 | M -= NumElts; | |||
12627 | Blend2 &= (M == (Lane + Elt)); | |||
12628 | assert(Lane <= M && M < (Lane + NumEltsPerLane) && "Out of range mask")(static_cast <bool> (Lane <= M && M < (Lane + NumEltsPerLane) && "Out of range mask") ? void (0) : __assert_fail ("Lane <= M && M < (Lane + NumEltsPerLane) && \"Out of range mask\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 12628, __extension__ __PRETTY_FUNCTION__)); | |||
12629 | M = M % NumEltsPerLane; | |||
12630 | Range2.first = std::min(Range2.first, M); | |||
12631 | Range2.second = std::max(Range2.second, M); | |||
12632 | } | |||
12633 | } | |||
12634 | } | |||
12635 | ||||
12636 | // Bail if we don't need both elements. | |||
12637 | // TODO - it might be worth doing this for unary shuffles if the permute | |||
12638 | // can be widened. | |||
12639 | if (!(0 <= Range1.first && Range1.second < NumEltsPerLane) || | |||
12640 | !(0 <= Range2.first && Range2.second < NumEltsPerLane)) | |||
12641 | return SDValue(); | |||
12642 | ||||
12643 | if (VT.getSizeInBits() > 128 && (Blend1 || Blend2)) | |||
12644 | return SDValue(); | |||
12645 | ||||
12646 | // Rotate the 2 ops so we can access both ranges, then permute the result. | |||
12647 | auto RotateAndPermute = [&](SDValue Lo, SDValue Hi, int RotAmt, int Ofs) { | |||
12648 | MVT ByteVT = MVT::getVectorVT(MVT::i8, VT.getSizeInBits() / 8); | |||
12649 | SDValue Rotate = DAG.getBitcast( | |||
12650 | VT, DAG.getNode(X86ISD::PALIGNR, DL, ByteVT, DAG.getBitcast(ByteVT, Hi), | |||
12651 | DAG.getBitcast(ByteVT, Lo), | |||
12652 | DAG.getTargetConstant(Scale * RotAmt, DL, MVT::i8))); | |||
12653 | SmallVector<int, 64> PermMask(NumElts, SM_SentinelUndef); | |||
12654 | for (int Lane = 0; Lane != NumElts; Lane += NumEltsPerLane) { | |||
12655 | for (int Elt = 0; Elt != NumEltsPerLane; ++Elt) { | |||
12656 | int M = Mask[Lane + Elt]; | |||
12657 | if (M < 0) | |||
12658 | continue; | |||
12659 | if (M < NumElts) | |||
12660 | PermMask[Lane + Elt] = Lane + ((M + Ofs - RotAmt) % NumEltsPerLane); | |||
12661 | else | |||
12662 | PermMask[Lane + Elt] = Lane + ((M - Ofs - RotAmt) % NumEltsPerLane); | |||
12663 | } | |||
12664 | } | |||
12665 | return DAG.getVectorShuffle(VT, DL, Rotate, DAG.getUNDEF(VT), PermMask); | |||
12666 | }; | |||
12667 | ||||
12668 | // Check if the ranges are small enough to rotate from either direction. | |||
12669 | if (Range2.second < Range1.first) | |||
12670 | return RotateAndPermute(V1, V2, Range1.first, 0); | |||
12671 | if (Range1.second < Range2.first) | |||
12672 | return RotateAndPermute(V2, V1, Range2.first, NumElts); | |||
12673 | return SDValue(); | |||
12674 | } | |||
12675 | ||||
12676 | static bool isBroadcastShuffleMask(ArrayRef<int> Mask) { | |||
12677 | return isUndefOrEqual(Mask, 0); | |||
12678 | } | |||
12679 | ||||
12680 | static bool isNoopOrBroadcastShuffleMask(ArrayRef<int> Mask) { | |||
12681 | return isNoopShuffleMask(Mask) || isBroadcastShuffleMask(Mask); | |||
12682 | } | |||
12683 | ||||
12684 | /// Generic routine to decompose a shuffle and blend into independent | |||
12685 | /// blends and permutes. | |||
12686 | /// | |||
12687 | /// This matches the extremely common pattern for handling combined | |||
12688 | /// shuffle+blend operations on newer X86 ISAs where we have very fast blend | |||
12689 | /// operations. It will try to pick the best arrangement of shuffles and | |||
12690 | /// blends. For vXi8/vXi16 shuffles we may use unpack instead of blend. | |||
12691 | static SDValue lowerShuffleAsDecomposedShuffleMerge( | |||
12692 | const SDLoc &DL, MVT VT, SDValue V1, SDValue V2, ArrayRef<int> Mask, | |||
12693 | const X86Subtarget &Subtarget, SelectionDAG &DAG) { | |||
12694 | int NumElts = Mask.size(); | |||
12695 | int NumLanes = VT.getSizeInBits() / 128; | |||
12696 | int NumEltsPerLane = NumElts / NumLanes; | |||
12697 | ||||
12698 | // Shuffle the input elements into the desired positions in V1 and V2 and | |||
12699 | // unpack/blend them together. | |||
12700 | bool IsAlternating = true; | |||
12701 | SmallVector<int, 32> V1Mask(NumElts, -1); | |||
12702 | SmallVector<int, 32> V2Mask(NumElts, -1); | |||
12703 | SmallVector<int, 32> FinalMask(NumElts, -1); | |||
12704 | for (int i = 0; i < NumElts; ++i) { | |||
12705 | int M = Mask[i]; | |||
12706 | if (M >= 0 && M < NumElts) { | |||
12707 | V1Mask[i] = M; | |||
12708 | FinalMask[i] = i; | |||
12709 | IsAlternating &= (i & 1) == 0; | |||
12710 | } else if (M >= NumElts) { | |||
12711 | V2Mask[i] = M - NumElts; | |||
12712 | FinalMask[i] = i + NumElts; | |||
12713 | IsAlternating &= (i & 1) == 1; | |||
12714 | } | |||
12715 | } | |||
12716 | ||||
12717 | // If we effectively only demand the 0'th element of \p Input, and not only | |||
12718 | // as 0'th element, then broadcast said input, | |||
12719 | // and change \p InputMask to be a no-op (identity) mask. | |||
12720 | auto canonicalizeBroadcastableInput = [DL, VT, &Subtarget, | |||
12721 | &DAG](SDValue &Input, | |||
12722 | MutableArrayRef<int> InputMask) { | |||
12723 | unsigned EltSizeInBits = Input.getScalarValueSizeInBits(); | |||
12724 | if (!Subtarget.hasAVX2() && | |||
12725 | (!Subtarget.hasAVX() || EltSizeInBits < 32 || !MayFoldLoad(Input))) | |||
12726 | return; | |||
12727 | if (isNoopShuffleMask(InputMask)) | |||
12728 | return; | |||
12729 | assert(isBroadcastShuffleMask(InputMask) &&(static_cast <bool> (isBroadcastShuffleMask(InputMask) && "Expected to demand only the 0'th element.") ? void (0) : __assert_fail ("isBroadcastShuffleMask(InputMask) && \"Expected to demand only the 0'th element.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 12730, __extension__ __PRETTY_FUNCTION__)) | |||
12730 | "Expected to demand only the 0'th element.")(static_cast <bool> (isBroadcastShuffleMask(InputMask) && "Expected to demand only the 0'th element.") ? void (0) : __assert_fail ("isBroadcastShuffleMask(InputMask) && \"Expected to demand only the 0'th element.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 12730, __extension__ __PRETTY_FUNCTION__)); | |||
12731 | Input = DAG.getNode(X86ISD::VBROADCAST, DL, VT, Input); | |||
12732 | for (auto I : enumerate(InputMask)) { | |||
12733 | int &InputMaskElt = I.value(); | |||
12734 | if (InputMaskElt >= 0) | |||
12735 | InputMaskElt = I.index(); | |||
12736 | } | |||
12737 | }; | |||
12738 | ||||
12739 | // Currently, we may need to produce one shuffle per input, and blend results. | |||
12740 | // It is possible that the shuffle for one of the inputs is already a no-op. | |||
12741 | // See if we can simplify non-no-op shuffles into broadcasts, | |||
12742 | // which we consider to be strictly better than an arbitrary shuffle. | |||
12743 | if (isNoopOrBroadcastShuffleMask(V1Mask) && | |||
12744 | isNoopOrBroadcastShuffleMask(V2Mask)) { | |||
12745 | canonicalizeBroadcastableInput(V1, V1Mask); | |||
12746 | canonicalizeBroadcastableInput(V2, V2Mask); | |||
12747 | } | |||
12748 | ||||
12749 | // Try to lower with the simpler initial blend/unpack/rotate strategies unless | |||
12750 | // one of the input shuffles would be a no-op. We prefer to shuffle inputs as | |||
12751 | // the shuffle may be able to fold with a load or other benefit. However, when | |||
12752 | // we'll have to do 2x as many shuffles in order to achieve this, a 2-input | |||
12753 | // pre-shuffle first is a better strategy. | |||
12754 | if (!isNoopShuffleMask(V1Mask) && !isNoopShuffleMask(V2Mask)) { | |||
12755 | // Only prefer immediate blends to unpack/rotate. | |||
12756 | if (SDValue BlendPerm = lowerShuffleAsBlendAndPermute(DL, VT, V1, V2, Mask, | |||
12757 | DAG, true)) | |||
12758 | return BlendPerm; | |||
12759 | if (SDValue UnpackPerm = lowerShuffleAsUNPCKAndPermute(DL, VT, V1, V2, Mask, | |||
12760 | DAG)) | |||
12761 | return UnpackPerm; | |||
12762 | if (SDValue RotatePerm = lowerShuffleAsByteRotateAndPermute( | |||
12763 | DL, VT, V1, V2, Mask, Subtarget, DAG)) | |||
12764 | return RotatePerm; | |||
12765 | // Unpack/rotate failed - try again with variable blends. | |||
12766 | if (SDValue BlendPerm = lowerShuffleAsBlendAndPermute(DL, VT, V1, V2, Mask, | |||
12767 | DAG)) | |||
12768 | return BlendPerm; | |||
12769 | } | |||
12770 | ||||
12771 | // If the final mask is an alternating blend of vXi8/vXi16, convert to an | |||
12772 | // UNPCKL(SHUFFLE, SHUFFLE) pattern. | |||
12773 | // TODO: It doesn't have to be alternating - but each lane mustn't have more | |||
12774 | // than half the elements coming from each source. | |||
12775 | if (IsAlternating && VT.getScalarSizeInBits() < 32) { | |||
12776 | V1Mask.assign(NumElts, -1); | |||
12777 | V2Mask.assign(NumElts, -1); | |||
12778 | FinalMask.assign(NumElts, -1); | |||
12779 | for (int i = 0; i != NumElts; i += NumEltsPerLane) | |||
12780 | for (int j = 0; j != NumEltsPerLane; ++j) { | |||
12781 | int M = Mask[i + j]; | |||
12782 | if (M >= 0 && M < NumElts) { | |||
12783 | V1Mask[i + (j / 2)] = M; | |||
12784 | FinalMask[i + j] = i + (j / 2); | |||
12785 | } else if (M >= NumElts) { | |||
12786 | V2Mask[i + (j / 2)] = M - NumElts; | |||
12787 | FinalMask[i + j] = i + (j / 2) + NumElts; | |||
12788 | } | |||
12789 | } | |||
12790 | } | |||
12791 | ||||
12792 | V1 = DAG.getVectorShuffle(VT, DL, V1, DAG.getUNDEF(VT), V1Mask); | |||
12793 | V2 = DAG.getVectorShuffle(VT, DL, V2, DAG.getUNDEF(VT), V2Mask); | |||
12794 | return DAG.getVectorShuffle(VT, DL, V1, V2, FinalMask); | |||
12795 | } | |||
12796 | ||||
12797 | /// Try to lower a vector shuffle as a bit rotation. | |||
12798 | /// | |||
12799 | /// Look for a repeated rotation pattern in each sub group. | |||
12800 | /// Returns a ISD::ROTL element rotation amount or -1 if failed. | |||
12801 | static int matchShuffleAsBitRotate(ArrayRef<int> Mask, int NumSubElts) { | |||
12802 | int NumElts = Mask.size(); | |||
12803 | assert((NumElts % NumSubElts) == 0 && "Illegal shuffle mask")(static_cast <bool> ((NumElts % NumSubElts) == 0 && "Illegal shuffle mask") ? void (0) : __assert_fail ("(NumElts % NumSubElts) == 0 && \"Illegal shuffle mask\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 12803, __extension__ __PRETTY_FUNCTION__)); | |||
12804 | ||||
12805 | int RotateAmt = -1; | |||
12806 | for (int i = 0; i != NumElts; i += NumSubElts) { | |||
12807 | for (int j = 0; j != NumSubElts; ++j) { | |||
12808 | int M = Mask[i + j]; | |||
12809 | if (M < 0) | |||
12810 | continue; | |||
12811 | if (!isInRange(M, i, i + NumSubElts)) | |||
12812 | return -1; | |||
12813 | int Offset = (NumSubElts - (M - (i + j))) % NumSubElts; | |||
12814 | if (0 <= RotateAmt && Offset != RotateAmt) | |||
12815 | return -1; | |||
12816 | RotateAmt = Offset; | |||
12817 | } | |||
12818 | } | |||
12819 | return RotateAmt; | |||
12820 | } | |||
12821 | ||||
12822 | static int matchShuffleAsBitRotate(MVT &RotateVT, int EltSizeInBits, | |||
12823 | const X86Subtarget &Subtarget, | |||
12824 | ArrayRef<int> Mask) { | |||
12825 | assert(!isNoopShuffleMask(Mask) && "We shouldn't lower no-op shuffles!")(static_cast <bool> (!isNoopShuffleMask(Mask) && "We shouldn't lower no-op shuffles!") ? void (0) : __assert_fail ("!isNoopShuffleMask(Mask) && \"We shouldn't lower no-op shuffles!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 12825, __extension__ __PRETTY_FUNCTION__)); | |||
12826 | assert(EltSizeInBits < 64 && "Can't rotate 64-bit integers")(static_cast <bool> (EltSizeInBits < 64 && "Can't rotate 64-bit integers" ) ? void (0) : __assert_fail ("EltSizeInBits < 64 && \"Can't rotate 64-bit integers\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 12826, __extension__ __PRETTY_FUNCTION__)); | |||
12827 | ||||
12828 | // AVX512 only has vXi32/vXi64 rotates, so limit the rotation sub group size. | |||
12829 | int MinSubElts = Subtarget.hasAVX512() ? std::max(32 / EltSizeInBits, 2) : 2; | |||
12830 | int MaxSubElts = 64 / EltSizeInBits; | |||
12831 | for (int NumSubElts = MinSubElts; NumSubElts <= MaxSubElts; NumSubElts *= 2) { | |||
12832 | int RotateAmt = matchShuffleAsBitRotate(Mask, NumSubElts); | |||
12833 | if (RotateAmt < 0) | |||
12834 | continue; | |||
12835 | ||||
12836 | int NumElts = Mask.size(); | |||
12837 | MVT RotateSVT = MVT::getIntegerVT(EltSizeInBits * NumSubElts); | |||
12838 | RotateVT = MVT::getVectorVT(RotateSVT, NumElts / NumSubElts); | |||
12839 | return RotateAmt * EltSizeInBits; | |||
12840 | } | |||
12841 | ||||
12842 | return -1; | |||
12843 | } | |||
12844 | ||||
12845 | /// Lower shuffle using X86ISD::VROTLI rotations. | |||
12846 | static SDValue lowerShuffleAsBitRotate(const SDLoc &DL, MVT VT, SDValue V1, | |||
12847 | ArrayRef<int> Mask, | |||
12848 | const X86Subtarget &Subtarget, | |||
12849 | SelectionDAG &DAG) { | |||
12850 | // Only XOP + AVX512 targets have bit rotation instructions. | |||
12851 | // If we at least have SSSE3 (PSHUFB) then we shouldn't attempt to use this. | |||
12852 | bool IsLegal = | |||
12853 | (VT.is128BitVector() && Subtarget.hasXOP()) || Subtarget.hasAVX512(); | |||
12854 | if (!IsLegal && Subtarget.hasSSE3()) | |||
12855 | return SDValue(); | |||
12856 | ||||
12857 | MVT RotateVT; | |||
12858 | int RotateAmt = matchShuffleAsBitRotate(RotateVT, VT.getScalarSizeInBits(), | |||
12859 | Subtarget, Mask); | |||
12860 | if (RotateAmt < 0) | |||
12861 | return SDValue(); | |||
12862 | ||||
12863 | // For pre-SSSE3 targets, if we are shuffling vXi8 elts then ISD::ROTL, | |||
12864 | // expanded to OR(SRL,SHL), will be more efficient, but if they can | |||
12865 | // widen to vXi16 or more then existing lowering should will be better. | |||
12866 | if (!IsLegal) { | |||
12867 | if ((RotateAmt % 16) == 0) | |||
12868 | return SDValue(); | |||
12869 | // TODO: Use getTargetVShiftByConstNode. | |||
12870 | unsigned ShlAmt = RotateAmt; | |||
12871 | unsigned SrlAmt = RotateVT.getScalarSizeInBits() - RotateAmt; | |||
12872 | V1 = DAG.getBitcast(RotateVT, V1); | |||
12873 | SDValue SHL = DAG.getNode(X86ISD::VSHLI, DL, RotateVT, V1, | |||
12874 | DAG.getTargetConstant(ShlAmt, DL, MVT::i8)); | |||
12875 | SDValue SRL = DAG.getNode(X86ISD::VSRLI, DL, RotateVT, V1, | |||
12876 | DAG.getTargetConstant(SrlAmt, DL, MVT::i8)); | |||
12877 | SDValue Rot = DAG.getNode(ISD::OR, DL, RotateVT, SHL, SRL); | |||
12878 | return DAG.getBitcast(VT, Rot); | |||
12879 | } | |||
12880 | ||||
12881 | SDValue Rot = | |||
12882 | DAG.getNode(X86ISD::VROTLI, DL, RotateVT, DAG.getBitcast(RotateVT, V1), | |||
12883 | DAG.getTargetConstant(RotateAmt, DL, MVT::i8)); | |||
12884 | return DAG.getBitcast(VT, Rot); | |||
12885 | } | |||
12886 | ||||
12887 | /// Try to match a vector shuffle as an element rotation. | |||
12888 | /// | |||
12889 | /// This is used for support PALIGNR for SSSE3 or VALIGND/Q for AVX512. | |||
12890 | static int matchShuffleAsElementRotate(SDValue &V1, SDValue &V2, | |||
12891 | ArrayRef<int> Mask) { | |||
12892 | int NumElts = Mask.size(); | |||
12893 | ||||
12894 | // We need to detect various ways of spelling a rotation: | |||
12895 | // [11, 12, 13, 14, 15, 0, 1, 2] | |||
12896 | // [-1, 12, 13, 14, -1, -1, 1, -1] | |||
12897 | // [-1, -1, -1, -1, -1, -1, 1, 2] | |||
12898 | // [ 3, 4, 5, 6, 7, 8, 9, 10] | |||
12899 | // [-1, 4, 5, 6, -1, -1, 9, -1] | |||
12900 | // [-1, 4, 5, 6, -1, -1, -1, -1] | |||
12901 | int Rotation = 0; | |||
12902 | SDValue Lo, Hi; | |||
12903 | for (int i = 0; i < NumElts; ++i) { | |||
12904 | int M = Mask[i]; | |||
12905 | assert((M == SM_SentinelUndef || (0 <= M && M < (2*NumElts))) &&(static_cast <bool> ((M == SM_SentinelUndef || (0 <= M && M < (2*NumElts))) && "Unexpected mask index." ) ? void (0) : __assert_fail ("(M == SM_SentinelUndef || (0 <= M && M < (2*NumElts))) && \"Unexpected mask index.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 12906, __extension__ __PRETTY_FUNCTION__)) | |||
12906 | "Unexpected mask index.")(static_cast <bool> ((M == SM_SentinelUndef || (0 <= M && M < (2*NumElts))) && "Unexpected mask index." ) ? void (0) : __assert_fail ("(M == SM_SentinelUndef || (0 <= M && M < (2*NumElts))) && \"Unexpected mask index.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 12906, __extension__ __PRETTY_FUNCTION__)); | |||
12907 | if (M < 0) | |||
12908 | continue; | |||
12909 | ||||
12910 | // Determine where a rotated vector would have started. | |||
12911 | int StartIdx = i - (M % NumElts); | |||
12912 | if (StartIdx == 0) | |||
12913 | // The identity rotation isn't interesting, stop. | |||
12914 | return -1; | |||
12915 | ||||
12916 | // If we found the tail of a vector the rotation must be the missing | |||
12917 | // front. If we found the head of a vector, it must be how much of the | |||
12918 | // head. | |||
12919 | int CandidateRotation = StartIdx < 0 ? -StartIdx : NumElts - StartIdx; | |||
12920 | ||||
12921 | if (Rotation == 0) | |||
12922 | Rotation = CandidateRotation; | |||
12923 | else if (Rotation != CandidateRotation) | |||
12924 | // The rotations don't match, so we can't match this mask. | |||
12925 | return -1; | |||
12926 | ||||
12927 | // Compute which value this mask is pointing at. | |||
12928 | SDValue MaskV = M < NumElts ? V1 : V2; | |||
12929 | ||||
12930 | // Compute which of the two target values this index should be assigned | |||
12931 | // to. This reflects whether the high elements are remaining or the low | |||
12932 | // elements are remaining. | |||
12933 | SDValue &TargetV = StartIdx < 0 ? Hi : Lo; | |||
12934 | ||||
12935 | // Either set up this value if we've not encountered it before, or check | |||
12936 | // that it remains consistent. | |||
12937 | if (!TargetV) | |||
12938 | TargetV = MaskV; | |||
12939 | else if (TargetV != MaskV) | |||
12940 | // This may be a rotation, but it pulls from the inputs in some | |||
12941 | // unsupported interleaving. | |||
12942 | return -1; | |||
12943 | } | |||
12944 | ||||
12945 | // Check that we successfully analyzed the mask, and normalize the results. | |||
12946 | assert(Rotation != 0 && "Failed to locate a viable rotation!")(static_cast <bool> (Rotation != 0 && "Failed to locate a viable rotation!" ) ? void (0) : __assert_fail ("Rotation != 0 && \"Failed to locate a viable rotation!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 12946, __extension__ __PRETTY_FUNCTION__)); | |||
12947 | assert((Lo || Hi) && "Failed to find a rotated input vector!")(static_cast <bool> ((Lo || Hi) && "Failed to find a rotated input vector!" ) ? void (0) : __assert_fail ("(Lo || Hi) && \"Failed to find a rotated input vector!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 12947, __extension__ __PRETTY_FUNCTION__)); | |||
12948 | if (!Lo) | |||
12949 | Lo = Hi; | |||
12950 | else if (!Hi) | |||
12951 | Hi = Lo; | |||
12952 | ||||
12953 | V1 = Lo; | |||
12954 | V2 = Hi; | |||
12955 | ||||
12956 | return Rotation; | |||
12957 | } | |||
12958 | ||||
12959 | /// Try to lower a vector shuffle as a byte rotation. | |||
12960 | /// | |||
12961 | /// SSSE3 has a generic PALIGNR instruction in x86 that will do an arbitrary | |||
12962 | /// byte-rotation of the concatenation of two vectors; pre-SSSE3 can use | |||
12963 | /// a PSRLDQ/PSLLDQ/POR pattern to get a similar effect. This routine will | |||
12964 | /// try to generically lower a vector shuffle through such an pattern. It | |||
12965 | /// does not check for the profitability of lowering either as PALIGNR or | |||
12966 | /// PSRLDQ/PSLLDQ/POR, only whether the mask is valid to lower in that form. | |||
12967 | /// This matches shuffle vectors that look like: | |||
12968 | /// | |||
12969 | /// v8i16 [11, 12, 13, 14, 15, 0, 1, 2] | |||
12970 | /// | |||
12971 | /// Essentially it concatenates V1 and V2, shifts right by some number of | |||
12972 | /// elements, and takes the low elements as the result. Note that while this is | |||
12973 | /// specified as a *right shift* because x86 is little-endian, it is a *left | |||
12974 | /// rotate* of the vector lanes. | |||
12975 | static int matchShuffleAsByteRotate(MVT VT, SDValue &V1, SDValue &V2, | |||
12976 | ArrayRef<int> Mask) { | |||
12977 | // Don't accept any shuffles with zero elements. | |||
12978 | if (isAnyZero(Mask)) | |||
12979 | return -1; | |||
12980 | ||||
12981 | // PALIGNR works on 128-bit lanes. | |||
12982 | SmallVector<int, 16> RepeatedMask; | |||
12983 | if (!is128BitLaneRepeatedShuffleMask(VT, Mask, RepeatedMask)) | |||
12984 | return -1; | |||
12985 | ||||
12986 | int Rotation = matchShuffleAsElementRotate(V1, V2, RepeatedMask); | |||
12987 | if (Rotation <= 0) | |||
12988 | return -1; | |||
12989 | ||||
12990 | // PALIGNR rotates bytes, so we need to scale the | |||
12991 | // rotation based on how many bytes are in the vector lane. | |||
12992 | int NumElts = RepeatedMask.size(); | |||
12993 | int Scale = 16 / NumElts; | |||
12994 | return Rotation * Scale; | |||
12995 | } | |||
12996 | ||||
12997 | static SDValue lowerShuffleAsByteRotate(const SDLoc &DL, MVT VT, SDValue V1, | |||
12998 | SDValue V2, ArrayRef<int> Mask, | |||
12999 | const X86Subtarget &Subtarget, | |||
13000 | SelectionDAG &DAG) { | |||
13001 | assert(!isNoopShuffleMask(Mask) && "We shouldn't lower no-op shuffles!")(static_cast <bool> (!isNoopShuffleMask(Mask) && "We shouldn't lower no-op shuffles!") ? void (0) : __assert_fail ("!isNoopShuffleMask(Mask) && \"We shouldn't lower no-op shuffles!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13001, __extension__ __PRETTY_FUNCTION__)); | |||
13002 | ||||
13003 | SDValue Lo = V1, Hi = V2; | |||
13004 | int ByteRotation = matchShuffleAsByteRotate(VT, Lo, Hi, Mask); | |||
13005 | if (ByteRotation <= 0) | |||
13006 | return SDValue(); | |||
13007 | ||||
13008 | // Cast the inputs to i8 vector of correct length to match PALIGNR or | |||
13009 | // PSLLDQ/PSRLDQ. | |||
13010 | MVT ByteVT = MVT::getVectorVT(MVT::i8, VT.getSizeInBits() / 8); | |||
13011 | Lo = DAG.getBitcast(ByteVT, Lo); | |||
13012 | Hi = DAG.getBitcast(ByteVT, Hi); | |||
13013 | ||||
13014 | // SSSE3 targets can use the palignr instruction. | |||
13015 | if (Subtarget.hasSSSE3()) { | |||
13016 | assert((!VT.is512BitVector() || Subtarget.hasBWI()) &&(static_cast <bool> ((!VT.is512BitVector() || Subtarget .hasBWI()) && "512-bit PALIGNR requires BWI instructions" ) ? void (0) : __assert_fail ("(!VT.is512BitVector() || Subtarget.hasBWI()) && \"512-bit PALIGNR requires BWI instructions\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13017, __extension__ __PRETTY_FUNCTION__)) | |||
13017 | "512-bit PALIGNR requires BWI instructions")(static_cast <bool> ((!VT.is512BitVector() || Subtarget .hasBWI()) && "512-bit PALIGNR requires BWI instructions" ) ? void (0) : __assert_fail ("(!VT.is512BitVector() || Subtarget.hasBWI()) && \"512-bit PALIGNR requires BWI instructions\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13017, __extension__ __PRETTY_FUNCTION__)); | |||
13018 | return DAG.getBitcast( | |||
13019 | VT, DAG.getNode(X86ISD::PALIGNR, DL, ByteVT, Lo, Hi, | |||
13020 | DAG.getTargetConstant(ByteRotation, DL, MVT::i8))); | |||
13021 | } | |||
13022 | ||||
13023 | assert(VT.is128BitVector() &&(static_cast <bool> (VT.is128BitVector() && "Rotate-based lowering only supports 128-bit lowering!" ) ? void (0) : __assert_fail ("VT.is128BitVector() && \"Rotate-based lowering only supports 128-bit lowering!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13024, __extension__ __PRETTY_FUNCTION__)) | |||
13024 | "Rotate-based lowering only supports 128-bit lowering!")(static_cast <bool> (VT.is128BitVector() && "Rotate-based lowering only supports 128-bit lowering!" ) ? void (0) : __assert_fail ("VT.is128BitVector() && \"Rotate-based lowering only supports 128-bit lowering!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13024, __extension__ __PRETTY_FUNCTION__)); | |||
13025 | assert(Mask.size() <= 16 &&(static_cast <bool> (Mask.size() <= 16 && "Can shuffle at most 16 bytes in a 128-bit vector!" ) ? void (0) : __assert_fail ("Mask.size() <= 16 && \"Can shuffle at most 16 bytes in a 128-bit vector!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13026, __extension__ __PRETTY_FUNCTION__)) | |||
13026 | "Can shuffle at most 16 bytes in a 128-bit vector!")(static_cast <bool> (Mask.size() <= 16 && "Can shuffle at most 16 bytes in a 128-bit vector!" ) ? void (0) : __assert_fail ("Mask.size() <= 16 && \"Can shuffle at most 16 bytes in a 128-bit vector!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13026, __extension__ __PRETTY_FUNCTION__)); | |||
13027 | assert(ByteVT == MVT::v16i8 &&(static_cast <bool> (ByteVT == MVT::v16i8 && "SSE2 rotate lowering only needed for v16i8!" ) ? void (0) : __assert_fail ("ByteVT == MVT::v16i8 && \"SSE2 rotate lowering only needed for v16i8!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13028, __extension__ __PRETTY_FUNCTION__)) | |||
13028 | "SSE2 rotate lowering only needed for v16i8!")(static_cast <bool> (ByteVT == MVT::v16i8 && "SSE2 rotate lowering only needed for v16i8!" ) ? void (0) : __assert_fail ("ByteVT == MVT::v16i8 && \"SSE2 rotate lowering only needed for v16i8!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13028, __extension__ __PRETTY_FUNCTION__)); | |||
13029 | ||||
13030 | // Default SSE2 implementation | |||
13031 | int LoByteShift = 16 - ByteRotation; | |||
13032 | int HiByteShift = ByteRotation; | |||
13033 | ||||
13034 | SDValue LoShift = | |||
13035 | DAG.getNode(X86ISD::VSHLDQ, DL, MVT::v16i8, Lo, | |||
13036 | DAG.getTargetConstant(LoByteShift, DL, MVT::i8)); | |||
13037 | SDValue HiShift = | |||
13038 | DAG.getNode(X86ISD::VSRLDQ, DL, MVT::v16i8, Hi, | |||
13039 | DAG.getTargetConstant(HiByteShift, DL, MVT::i8)); | |||
13040 | return DAG.getBitcast(VT, | |||
13041 | DAG.getNode(ISD::OR, DL, MVT::v16i8, LoShift, HiShift)); | |||
13042 | } | |||
13043 | ||||
13044 | /// Try to lower a vector shuffle as a dword/qword rotation. | |||
13045 | /// | |||
13046 | /// AVX512 has a VALIGND/VALIGNQ instructions that will do an arbitrary | |||
13047 | /// rotation of the concatenation of two vectors; This routine will | |||
13048 | /// try to generically lower a vector shuffle through such an pattern. | |||
13049 | /// | |||
13050 | /// Essentially it concatenates V1 and V2, shifts right by some number of | |||
13051 | /// elements, and takes the low elements as the result. Note that while this is | |||
13052 | /// specified as a *right shift* because x86 is little-endian, it is a *left | |||
13053 | /// rotate* of the vector lanes. | |||
13054 | static SDValue lowerShuffleAsVALIGN(const SDLoc &DL, MVT VT, SDValue V1, | |||
13055 | SDValue V2, ArrayRef<int> Mask, | |||
13056 | const X86Subtarget &Subtarget, | |||
13057 | SelectionDAG &DAG) { | |||
13058 | assert((VT.getScalarType() == MVT::i32 || VT.getScalarType() == MVT::i64) &&(static_cast <bool> ((VT.getScalarType() == MVT::i32 || VT.getScalarType() == MVT::i64) && "Only 32-bit and 64-bit elements are supported!" ) ? void (0) : __assert_fail ("(VT.getScalarType() == MVT::i32 || VT.getScalarType() == MVT::i64) && \"Only 32-bit and 64-bit elements are supported!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13059, __extension__ __PRETTY_FUNCTION__)) | |||
13059 | "Only 32-bit and 64-bit elements are supported!")(static_cast <bool> ((VT.getScalarType() == MVT::i32 || VT.getScalarType() == MVT::i64) && "Only 32-bit and 64-bit elements are supported!" ) ? void (0) : __assert_fail ("(VT.getScalarType() == MVT::i32 || VT.getScalarType() == MVT::i64) && \"Only 32-bit and 64-bit elements are supported!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13059, __extension__ __PRETTY_FUNCTION__)); | |||
13060 | ||||
13061 | // 128/256-bit vectors are only supported with VLX. | |||
13062 | assert((Subtarget.hasVLX() || (!VT.is128BitVector() && !VT.is256BitVector()))(static_cast <bool> ((Subtarget.hasVLX() || (!VT.is128BitVector () && !VT.is256BitVector())) && "VLX required for 128/256-bit vectors" ) ? void (0) : __assert_fail ("(Subtarget.hasVLX() || (!VT.is128BitVector() && !VT.is256BitVector())) && \"VLX required for 128/256-bit vectors\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13063, __extension__ __PRETTY_FUNCTION__)) | |||
13063 | && "VLX required for 128/256-bit vectors")(static_cast <bool> ((Subtarget.hasVLX() || (!VT.is128BitVector () && !VT.is256BitVector())) && "VLX required for 128/256-bit vectors" ) ? void (0) : __assert_fail ("(Subtarget.hasVLX() || (!VT.is128BitVector() && !VT.is256BitVector())) && \"VLX required for 128/256-bit vectors\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13063, __extension__ __PRETTY_FUNCTION__)); | |||
13064 | ||||
13065 | SDValue Lo = V1, Hi = V2; | |||
13066 | int Rotation = matchShuffleAsElementRotate(Lo, Hi, Mask); | |||
13067 | if (Rotation <= 0) | |||
13068 | return SDValue(); | |||
13069 | ||||
13070 | return DAG.getNode(X86ISD::VALIGN, DL, VT, Lo, Hi, | |||
13071 | DAG.getTargetConstant(Rotation, DL, MVT::i8)); | |||
13072 | } | |||
13073 | ||||
13074 | /// Try to lower a vector shuffle as a byte shift sequence. | |||
13075 | static SDValue lowerShuffleAsByteShiftMask(const SDLoc &DL, MVT VT, SDValue V1, | |||
13076 | SDValue V2, ArrayRef<int> Mask, | |||
13077 | const APInt &Zeroable, | |||
13078 | const X86Subtarget &Subtarget, | |||
13079 | SelectionDAG &DAG) { | |||
13080 | assert(!isNoopShuffleMask(Mask) && "We shouldn't lower no-op shuffles!")(static_cast <bool> (!isNoopShuffleMask(Mask) && "We shouldn't lower no-op shuffles!") ? void (0) : __assert_fail ("!isNoopShuffleMask(Mask) && \"We shouldn't lower no-op shuffles!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13080, __extension__ __PRETTY_FUNCTION__)); | |||
13081 | assert(VT.is128BitVector() && "Only 128-bit vectors supported")(static_cast <bool> (VT.is128BitVector() && "Only 128-bit vectors supported" ) ? void (0) : __assert_fail ("VT.is128BitVector() && \"Only 128-bit vectors supported\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13081, __extension__ __PRETTY_FUNCTION__)); | |||
13082 | ||||
13083 | // We need a shuffle that has zeros at one/both ends and a sequential | |||
13084 | // shuffle from one source within. | |||
13085 | unsigned ZeroLo = Zeroable.countTrailingOnes(); | |||
13086 | unsigned ZeroHi = Zeroable.countLeadingOnes(); | |||
13087 | if (!ZeroLo && !ZeroHi) | |||
13088 | return SDValue(); | |||
13089 | ||||
13090 | unsigned NumElts = Mask.size(); | |||
13091 | unsigned Len = NumElts - (ZeroLo + ZeroHi); | |||
13092 | if (!isSequentialOrUndefInRange(Mask, ZeroLo, Len, Mask[ZeroLo])) | |||
13093 | return SDValue(); | |||
13094 | ||||
13095 | unsigned Scale = VT.getScalarSizeInBits() / 8; | |||
13096 | ArrayRef<int> StubMask = Mask.slice(ZeroLo, Len); | |||
13097 | if (!isUndefOrInRange(StubMask, 0, NumElts) && | |||
13098 | !isUndefOrInRange(StubMask, NumElts, 2 * NumElts)) | |||
13099 | return SDValue(); | |||
13100 | ||||
13101 | SDValue Res = Mask[ZeroLo] < (int)NumElts ? V1 : V2; | |||
13102 | Res = DAG.getBitcast(MVT::v16i8, Res); | |||
13103 | ||||
13104 | // Use VSHLDQ/VSRLDQ ops to zero the ends of a vector and leave an | |||
13105 | // inner sequential set of elements, possibly offset: | |||
13106 | // 01234567 --> zzzzzz01 --> 1zzzzzzz | |||
13107 | // 01234567 --> 4567zzzz --> zzzzz456 | |||
13108 | // 01234567 --> z0123456 --> 3456zzzz --> zz3456zz | |||
13109 | if (ZeroLo == 0) { | |||
13110 | unsigned Shift = (NumElts - 1) - (Mask[ZeroLo + Len - 1] % NumElts); | |||
13111 | Res = DAG.getNode(X86ISD::VSHLDQ, DL, MVT::v16i8, Res, | |||
13112 | DAG.getTargetConstant(Scale * Shift, DL, MVT::i8)); | |||
13113 | Res = DAG.getNode(X86ISD::VSRLDQ, DL, MVT::v16i8, Res, | |||
13114 | DAG.getTargetConstant(Scale * ZeroHi, DL, MVT::i8)); | |||
13115 | } else if (ZeroHi == 0) { | |||
13116 | unsigned Shift = Mask[ZeroLo] % NumElts; | |||
13117 | Res = DAG.getNode(X86ISD::VSRLDQ, DL, MVT::v16i8, Res, | |||
13118 | DAG.getTargetConstant(Scale * Shift, DL, MVT::i8)); | |||
13119 | Res = DAG.getNode(X86ISD::VSHLDQ, DL, MVT::v16i8, Res, | |||
13120 | DAG.getTargetConstant(Scale * ZeroLo, DL, MVT::i8)); | |||
13121 | } else if (!Subtarget.hasSSSE3()) { | |||
13122 | // If we don't have PSHUFB then its worth avoiding an AND constant mask | |||
13123 | // by performing 3 byte shifts. Shuffle combining can kick in above that. | |||
13124 | // TODO: There may be some cases where VSH{LR}DQ+PAND is still better. | |||
13125 | unsigned Shift = (NumElts - 1) - (Mask[ZeroLo + Len - 1] % NumElts); | |||
13126 | Res = DAG.getNode(X86ISD::VSHLDQ, DL, MVT::v16i8, Res, | |||
13127 | DAG.getTargetConstant(Scale * Shift, DL, MVT::i8)); | |||
13128 | Shift += Mask[ZeroLo] % NumElts; | |||
13129 | Res = DAG.getNode(X86ISD::VSRLDQ, DL, MVT::v16i8, Res, | |||
13130 | DAG.getTargetConstant(Scale * Shift, DL, MVT::i8)); | |||
13131 | Res = DAG.getNode(X86ISD::VSHLDQ, DL, MVT::v16i8, Res, | |||
13132 | DAG.getTargetConstant(Scale * ZeroLo, DL, MVT::i8)); | |||
13133 | } else | |||
13134 | return SDValue(); | |||
13135 | ||||
13136 | return DAG.getBitcast(VT, Res); | |||
13137 | } | |||
13138 | ||||
13139 | /// Try to lower a vector shuffle as a bit shift (shifts in zeros). | |||
13140 | /// | |||
13141 | /// Attempts to match a shuffle mask against the PSLL(W/D/Q/DQ) and | |||
13142 | /// PSRL(W/D/Q/DQ) SSE2 and AVX2 logical bit-shift instructions. The function | |||
13143 | /// matches elements from one of the input vectors shuffled to the left or | |||
13144 | /// right with zeroable elements 'shifted in'. It handles both the strictly | |||
13145 | /// bit-wise element shifts and the byte shift across an entire 128-bit double | |||
13146 | /// quad word lane. | |||
13147 | /// | |||
13148 | /// PSHL : (little-endian) left bit shift. | |||
13149 | /// [ zz, 0, zz, 2 ] | |||
13150 | /// [ -1, 4, zz, -1 ] | |||
13151 | /// PSRL : (little-endian) right bit shift. | |||
13152 | /// [ 1, zz, 3, zz] | |||
13153 | /// [ -1, -1, 7, zz] | |||
13154 | /// PSLLDQ : (little-endian) left byte shift | |||
13155 | /// [ zz, 0, 1, 2, 3, 4, 5, 6] | |||
13156 | /// [ zz, zz, -1, -1, 2, 3, 4, -1] | |||
13157 | /// [ zz, zz, zz, zz, zz, zz, -1, 1] | |||
13158 | /// PSRLDQ : (little-endian) right byte shift | |||
13159 | /// [ 5, 6, 7, zz, zz, zz, zz, zz] | |||
13160 | /// [ -1, 5, 6, 7, zz, zz, zz, zz] | |||
13161 | /// [ 1, 2, -1, -1, -1, -1, zz, zz] | |||
13162 | static int matchShuffleAsShift(MVT &ShiftVT, unsigned &Opcode, | |||
13163 | unsigned ScalarSizeInBits, ArrayRef<int> Mask, | |||
13164 | int MaskOffset, const APInt &Zeroable, | |||
13165 | const X86Subtarget &Subtarget) { | |||
13166 | int Size = Mask.size(); | |||
13167 | unsigned SizeInBits = Size * ScalarSizeInBits; | |||
13168 | ||||
13169 | auto CheckZeros = [&](int Shift, int Scale, bool Left) { | |||
13170 | for (int i = 0; i < Size; i += Scale) | |||
13171 | for (int j = 0; j < Shift; ++j) | |||
13172 | if (!Zeroable[i + j + (Left ? 0 : (Scale - Shift))]) | |||
13173 | return false; | |||
13174 | ||||
13175 | return true; | |||
13176 | }; | |||
13177 | ||||
13178 | auto MatchShift = [&](int Shift, int Scale, bool Left) { | |||
13179 | for (int i = 0; i != Size; i += Scale) { | |||
13180 | unsigned Pos = Left ? i + Shift : i; | |||
13181 | unsigned Low = Left ? i : i + Shift; | |||
13182 | unsigned Len = Scale - Shift; | |||
13183 | if (!isSequentialOrUndefInRange(Mask, Pos, Len, Low + MaskOffset)) | |||
13184 | return -1; | |||
13185 | } | |||
13186 | ||||
13187 | int ShiftEltBits = ScalarSizeInBits * Scale; | |||
13188 | bool ByteShift = ShiftEltBits > 64; | |||
13189 | Opcode = Left ? (ByteShift ? X86ISD::VSHLDQ : X86ISD::VSHLI) | |||
13190 | : (ByteShift ? X86ISD::VSRLDQ : X86ISD::VSRLI); | |||
13191 | int ShiftAmt = Shift * ScalarSizeInBits / (ByteShift ? 8 : 1); | |||
13192 | ||||
13193 | // Normalize the scale for byte shifts to still produce an i64 element | |||
13194 | // type. | |||
13195 | Scale = ByteShift ? Scale / 2 : Scale; | |||
13196 | ||||
13197 | // We need to round trip through the appropriate type for the shift. | |||
13198 | MVT ShiftSVT = MVT::getIntegerVT(ScalarSizeInBits * Scale); | |||
13199 | ShiftVT = ByteShift ? MVT::getVectorVT(MVT::i8, SizeInBits / 8) | |||
13200 | : MVT::getVectorVT(ShiftSVT, Size / Scale); | |||
13201 | return (int)ShiftAmt; | |||
13202 | }; | |||
13203 | ||||
13204 | // SSE/AVX supports logical shifts up to 64-bit integers - so we can just | |||
13205 | // keep doubling the size of the integer elements up to that. We can | |||
13206 | // then shift the elements of the integer vector by whole multiples of | |||
13207 | // their width within the elements of the larger integer vector. Test each | |||
13208 | // multiple to see if we can find a match with the moved element indices | |||
13209 | // and that the shifted in elements are all zeroable. | |||
13210 | unsigned MaxWidth = ((SizeInBits == 512) && !Subtarget.hasBWI() ? 64 : 128); | |||
13211 | for (int Scale = 2; Scale * ScalarSizeInBits <= MaxWidth; Scale *= 2) | |||
13212 | for (int Shift = 1; Shift != Scale; ++Shift) | |||
13213 | for (bool Left : {true, false}) | |||
13214 | if (CheckZeros(Shift, Scale, Left)) { | |||
13215 | int ShiftAmt = MatchShift(Shift, Scale, Left); | |||
13216 | if (0 < ShiftAmt) | |||
13217 | return ShiftAmt; | |||
13218 | } | |||
13219 | ||||
13220 | // no match | |||
13221 | return -1; | |||
13222 | } | |||
13223 | ||||
13224 | static SDValue lowerShuffleAsShift(const SDLoc &DL, MVT VT, SDValue V1, | |||
13225 | SDValue V2, ArrayRef<int> Mask, | |||
13226 | const APInt &Zeroable, | |||
13227 | const X86Subtarget &Subtarget, | |||
13228 | SelectionDAG &DAG) { | |||
13229 | int Size = Mask.size(); | |||
13230 | assert(Size == (int)VT.getVectorNumElements() && "Unexpected mask size")(static_cast <bool> (Size == (int)VT.getVectorNumElements () && "Unexpected mask size") ? void (0) : __assert_fail ("Size == (int)VT.getVectorNumElements() && \"Unexpected mask size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13230, __extension__ __PRETTY_FUNCTION__)); | |||
13231 | ||||
13232 | MVT ShiftVT; | |||
13233 | SDValue V = V1; | |||
13234 | unsigned Opcode; | |||
13235 | ||||
13236 | // Try to match shuffle against V1 shift. | |||
13237 | int ShiftAmt = matchShuffleAsShift(ShiftVT, Opcode, VT.getScalarSizeInBits(), | |||
13238 | Mask, 0, Zeroable, Subtarget); | |||
13239 | ||||
13240 | // If V1 failed, try to match shuffle against V2 shift. | |||
13241 | if (ShiftAmt < 0) { | |||
13242 | ShiftAmt = matchShuffleAsShift(ShiftVT, Opcode, VT.getScalarSizeInBits(), | |||
13243 | Mask, Size, Zeroable, Subtarget); | |||
13244 | V = V2; | |||
13245 | } | |||
13246 | ||||
13247 | if (ShiftAmt < 0) | |||
13248 | return SDValue(); | |||
13249 | ||||
13250 | assert(DAG.getTargetLoweringInfo().isTypeLegal(ShiftVT) &&(static_cast <bool> (DAG.getTargetLoweringInfo().isTypeLegal (ShiftVT) && "Illegal integer vector type") ? void (0 ) : __assert_fail ("DAG.getTargetLoweringInfo().isTypeLegal(ShiftVT) && \"Illegal integer vector type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13251, __extension__ __PRETTY_FUNCTION__)) | |||
13251 | "Illegal integer vector type")(static_cast <bool> (DAG.getTargetLoweringInfo().isTypeLegal (ShiftVT) && "Illegal integer vector type") ? void (0 ) : __assert_fail ("DAG.getTargetLoweringInfo().isTypeLegal(ShiftVT) && \"Illegal integer vector type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13251, __extension__ __PRETTY_FUNCTION__)); | |||
13252 | V = DAG.getBitcast(ShiftVT, V); | |||
13253 | V = DAG.getNode(Opcode, DL, ShiftVT, V, | |||
13254 | DAG.getTargetConstant(ShiftAmt, DL, MVT::i8)); | |||
13255 | return DAG.getBitcast(VT, V); | |||
13256 | } | |||
13257 | ||||
13258 | // EXTRQ: Extract Len elements from lower half of source, starting at Idx. | |||
13259 | // Remainder of lower half result is zero and upper half is all undef. | |||
13260 | static bool matchShuffleAsEXTRQ(MVT VT, SDValue &V1, SDValue &V2, | |||
13261 | ArrayRef<int> Mask, uint64_t &BitLen, | |||
13262 | uint64_t &BitIdx, const APInt &Zeroable) { | |||
13263 | int Size = Mask.size(); | |||
13264 | int HalfSize = Size / 2; | |||
13265 | assert(Size == (int)VT.getVectorNumElements() && "Unexpected mask size")(static_cast <bool> (Size == (int)VT.getVectorNumElements () && "Unexpected mask size") ? void (0) : __assert_fail ("Size == (int)VT.getVectorNumElements() && \"Unexpected mask size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13265, __extension__ __PRETTY_FUNCTION__)); | |||
13266 | assert(!Zeroable.isAllOnes() && "Fully zeroable shuffle mask")(static_cast <bool> (!Zeroable.isAllOnes() && "Fully zeroable shuffle mask" ) ? void (0) : __assert_fail ("!Zeroable.isAllOnes() && \"Fully zeroable shuffle mask\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13266, __extension__ __PRETTY_FUNCTION__)); | |||
13267 | ||||
13268 | // Upper half must be undefined. | |||
13269 | if (!isUndefUpperHalf(Mask)) | |||
13270 | return false; | |||
13271 | ||||
13272 | // Determine the extraction length from the part of the | |||
13273 | // lower half that isn't zeroable. | |||
13274 | int Len = HalfSize; | |||
13275 | for (; Len > 0; --Len) | |||
13276 | if (!Zeroable[Len - 1]) | |||
13277 | break; | |||
13278 | assert(Len > 0 && "Zeroable shuffle mask")(static_cast <bool> (Len > 0 && "Zeroable shuffle mask" ) ? void (0) : __assert_fail ("Len > 0 && \"Zeroable shuffle mask\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13278, __extension__ __PRETTY_FUNCTION__)); | |||
13279 | ||||
13280 | // Attempt to match first Len sequential elements from the lower half. | |||
13281 | SDValue Src; | |||
13282 | int Idx = -1; | |||
13283 | for (int i = 0; i != Len; ++i) { | |||
13284 | int M = Mask[i]; | |||
13285 | if (M == SM_SentinelUndef) | |||
13286 | continue; | |||
13287 | SDValue &V = (M < Size ? V1 : V2); | |||
13288 | M = M % Size; | |||
13289 | ||||
13290 | // The extracted elements must start at a valid index and all mask | |||
13291 | // elements must be in the lower half. | |||
13292 | if (i > M || M >= HalfSize) | |||
13293 | return false; | |||
13294 | ||||
13295 | if (Idx < 0 || (Src == V && Idx == (M - i))) { | |||
13296 | Src = V; | |||
13297 | Idx = M - i; | |||
13298 | continue; | |||
13299 | } | |||
13300 | return false; | |||
13301 | } | |||
13302 | ||||
13303 | if (!Src || Idx < 0) | |||
13304 | return false; | |||
13305 | ||||
13306 | assert((Idx + Len) <= HalfSize && "Illegal extraction mask")(static_cast <bool> ((Idx + Len) <= HalfSize && "Illegal extraction mask") ? void (0) : __assert_fail ("(Idx + Len) <= HalfSize && \"Illegal extraction mask\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13306, __extension__ __PRETTY_FUNCTION__)); | |||
13307 | BitLen = (Len * VT.getScalarSizeInBits()) & 0x3f; | |||
13308 | BitIdx = (Idx * VT.getScalarSizeInBits()) & 0x3f; | |||
13309 | V1 = Src; | |||
13310 | return true; | |||
13311 | } | |||
13312 | ||||
13313 | // INSERTQ: Extract lowest Len elements from lower half of second source and | |||
13314 | // insert over first source, starting at Idx. | |||
13315 | // { A[0], .., A[Idx-1], B[0], .., B[Len-1], A[Idx+Len], .., UNDEF, ... } | |||
13316 | static bool matchShuffleAsINSERTQ(MVT VT, SDValue &V1, SDValue &V2, | |||
13317 | ArrayRef<int> Mask, uint64_t &BitLen, | |||
13318 | uint64_t &BitIdx) { | |||
13319 | int Size = Mask.size(); | |||
13320 | int HalfSize = Size / 2; | |||
13321 | assert(Size == (int)VT.getVectorNumElements() && "Unexpected mask size")(static_cast <bool> (Size == (int)VT.getVectorNumElements () && "Unexpected mask size") ? void (0) : __assert_fail ("Size == (int)VT.getVectorNumElements() && \"Unexpected mask size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13321, __extension__ __PRETTY_FUNCTION__)); | |||
13322 | ||||
13323 | // Upper half must be undefined. | |||
13324 | if (!isUndefUpperHalf(Mask)) | |||
13325 | return false; | |||
13326 | ||||
13327 | for (int Idx = 0; Idx != HalfSize; ++Idx) { | |||
13328 | SDValue Base; | |||
13329 | ||||
13330 | // Attempt to match first source from mask before insertion point. | |||
13331 | if (isUndefInRange(Mask, 0, Idx)) { | |||
13332 | /* EMPTY */ | |||
13333 | } else if (isSequentialOrUndefInRange(Mask, 0, Idx, 0)) { | |||
13334 | Base = V1; | |||
13335 | } else if (isSequentialOrUndefInRange(Mask, 0, Idx, Size)) { | |||
13336 | Base = V2; | |||
13337 | } else { | |||
13338 | continue; | |||
13339 | } | |||
13340 | ||||
13341 | // Extend the extraction length looking to match both the insertion of | |||
13342 | // the second source and the remaining elements of the first. | |||
13343 | for (int Hi = Idx + 1; Hi <= HalfSize; ++Hi) { | |||
13344 | SDValue Insert; | |||
13345 | int Len = Hi - Idx; | |||
13346 | ||||
13347 | // Match insertion. | |||
13348 | if (isSequentialOrUndefInRange(Mask, Idx, Len, 0)) { | |||
13349 | Insert = V1; | |||
13350 | } else if (isSequentialOrUndefInRange(Mask, Idx, Len, Size)) { | |||
13351 | Insert = V2; | |||
13352 | } else { | |||
13353 | continue; | |||
13354 | } | |||
13355 | ||||
13356 | // Match the remaining elements of the lower half. | |||
13357 | if (isUndefInRange(Mask, Hi, HalfSize - Hi)) { | |||
13358 | /* EMPTY */ | |||
13359 | } else if ((!Base || (Base == V1)) && | |||
13360 | isSequentialOrUndefInRange(Mask, Hi, HalfSize - Hi, Hi)) { | |||
13361 | Base = V1; | |||
13362 | } else if ((!Base || (Base == V2)) && | |||
13363 | isSequentialOrUndefInRange(Mask, Hi, HalfSize - Hi, | |||
13364 | Size + Hi)) { | |||
13365 | Base = V2; | |||
13366 | } else { | |||
13367 | continue; | |||
13368 | } | |||
13369 | ||||
13370 | BitLen = (Len * VT.getScalarSizeInBits()) & 0x3f; | |||
13371 | BitIdx = (Idx * VT.getScalarSizeInBits()) & 0x3f; | |||
13372 | V1 = Base; | |||
13373 | V2 = Insert; | |||
13374 | return true; | |||
13375 | } | |||
13376 | } | |||
13377 | ||||
13378 | return false; | |||
13379 | } | |||
13380 | ||||
13381 | /// Try to lower a vector shuffle using SSE4a EXTRQ/INSERTQ. | |||
13382 | static SDValue lowerShuffleWithSSE4A(const SDLoc &DL, MVT VT, SDValue V1, | |||
13383 | SDValue V2, ArrayRef<int> Mask, | |||
13384 | const APInt &Zeroable, SelectionDAG &DAG) { | |||
13385 | uint64_t BitLen, BitIdx; | |||
13386 | if (matchShuffleAsEXTRQ(VT, V1, V2, Mask, BitLen, BitIdx, Zeroable)) | |||
13387 | return DAG.getNode(X86ISD::EXTRQI, DL, VT, V1, | |||
13388 | DAG.getTargetConstant(BitLen, DL, MVT::i8), | |||
13389 | DAG.getTargetConstant(BitIdx, DL, MVT::i8)); | |||
13390 | ||||
13391 | if (matchShuffleAsINSERTQ(VT, V1, V2, Mask, BitLen, BitIdx)) | |||
13392 | return DAG.getNode(X86ISD::INSERTQI, DL, VT, V1 ? V1 : DAG.getUNDEF(VT), | |||
13393 | V2 ? V2 : DAG.getUNDEF(VT), | |||
13394 | DAG.getTargetConstant(BitLen, DL, MVT::i8), | |||
13395 | DAG.getTargetConstant(BitIdx, DL, MVT::i8)); | |||
13396 | ||||
13397 | return SDValue(); | |||
13398 | } | |||
13399 | ||||
13400 | /// Lower a vector shuffle as a zero or any extension. | |||
13401 | /// | |||
13402 | /// Given a specific number of elements, element bit width, and extension | |||
13403 | /// stride, produce either a zero or any extension based on the available | |||
13404 | /// features of the subtarget. The extended elements are consecutive and | |||
13405 | /// begin and can start from an offsetted element index in the input; to | |||
13406 | /// avoid excess shuffling the offset must either being in the bottom lane | |||
13407 | /// or at the start of a higher lane. All extended elements must be from | |||
13408 | /// the same lane. | |||
13409 | static SDValue lowerShuffleAsSpecificZeroOrAnyExtend( | |||
13410 | const SDLoc &DL, MVT VT, int Scale, int Offset, bool AnyExt, SDValue InputV, | |||
13411 | ArrayRef<int> Mask, const X86Subtarget &Subtarget, SelectionDAG &DAG) { | |||
13412 | assert(Scale > 1 && "Need a scale to extend.")(static_cast <bool> (Scale > 1 && "Need a scale to extend." ) ? void (0) : __assert_fail ("Scale > 1 && \"Need a scale to extend.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13412, __extension__ __PRETTY_FUNCTION__)); | |||
13413 | int EltBits = VT.getScalarSizeInBits(); | |||
13414 | int NumElements = VT.getVectorNumElements(); | |||
13415 | int NumEltsPerLane = 128 / EltBits; | |||
13416 | int OffsetLane = Offset / NumEltsPerLane; | |||
13417 | assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&(static_cast <bool> ((EltBits == 8 || EltBits == 16 || EltBits == 32) && "Only 8, 16, and 32 bit elements can be extended." ) ? void (0) : __assert_fail ("(EltBits == 8 || EltBits == 16 || EltBits == 32) && \"Only 8, 16, and 32 bit elements can be extended.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13418, __extension__ __PRETTY_FUNCTION__)) | |||
13418 | "Only 8, 16, and 32 bit elements can be extended.")(static_cast <bool> ((EltBits == 8 || EltBits == 16 || EltBits == 32) && "Only 8, 16, and 32 bit elements can be extended." ) ? void (0) : __assert_fail ("(EltBits == 8 || EltBits == 16 || EltBits == 32) && \"Only 8, 16, and 32 bit elements can be extended.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13418, __extension__ __PRETTY_FUNCTION__)); | |||
13419 | assert(Scale * EltBits <= 64 && "Cannot zero extend past 64 bits.")(static_cast <bool> (Scale * EltBits <= 64 && "Cannot zero extend past 64 bits.") ? void (0) : __assert_fail ("Scale * EltBits <= 64 && \"Cannot zero extend past 64 bits.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13419, __extension__ __PRETTY_FUNCTION__)); | |||
13420 | assert(0 <= Offset && "Extension offset must be positive.")(static_cast <bool> (0 <= Offset && "Extension offset must be positive." ) ? void (0) : __assert_fail ("0 <= Offset && \"Extension offset must be positive.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13420, __extension__ __PRETTY_FUNCTION__)); | |||
13421 | assert((Offset < NumEltsPerLane || Offset % NumEltsPerLane == 0) &&(static_cast <bool> ((Offset < NumEltsPerLane || Offset % NumEltsPerLane == 0) && "Extension offset must be in the first lane or start an upper lane." ) ? void (0) : __assert_fail ("(Offset < NumEltsPerLane || Offset % NumEltsPerLane == 0) && \"Extension offset must be in the first lane or start an upper lane.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13422, __extension__ __PRETTY_FUNCTION__)) | |||
13422 | "Extension offset must be in the first lane or start an upper lane.")(static_cast <bool> ((Offset < NumEltsPerLane || Offset % NumEltsPerLane == 0) && "Extension offset must be in the first lane or start an upper lane." ) ? void (0) : __assert_fail ("(Offset < NumEltsPerLane || Offset % NumEltsPerLane == 0) && \"Extension offset must be in the first lane or start an upper lane.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13422, __extension__ __PRETTY_FUNCTION__)); | |||
13423 | ||||
13424 | // Check that an index is in same lane as the base offset. | |||
13425 | auto SafeOffset = [&](int Idx) { | |||
13426 | return OffsetLane == (Idx / NumEltsPerLane); | |||
13427 | }; | |||
13428 | ||||
13429 | // Shift along an input so that the offset base moves to the first element. | |||
13430 | auto ShuffleOffset = [&](SDValue V) { | |||
13431 | if (!Offset) | |||
13432 | return V; | |||
13433 | ||||
13434 | SmallVector<int, 8> ShMask((unsigned)NumElements, -1); | |||
13435 | for (int i = 0; i * Scale < NumElements; ++i) { | |||
13436 | int SrcIdx = i + Offset; | |||
13437 | ShMask[i] = SafeOffset(SrcIdx) ? SrcIdx : -1; | |||
13438 | } | |||
13439 | return DAG.getVectorShuffle(VT, DL, V, DAG.getUNDEF(VT), ShMask); | |||
13440 | }; | |||
13441 | ||||
13442 | // Found a valid a/zext mask! Try various lowering strategies based on the | |||
13443 | // input type and available ISA extensions. | |||
13444 | if (Subtarget.hasSSE41()) { | |||
13445 | // Not worth offsetting 128-bit vectors if scale == 2, a pattern using | |||
13446 | // PUNPCK will catch this in a later shuffle match. | |||
13447 | if (Offset && Scale == 2 && VT.is128BitVector()) | |||
13448 | return SDValue(); | |||
13449 | MVT ExtVT = MVT::getVectorVT(MVT::getIntegerVT(EltBits * Scale), | |||
13450 | NumElements / Scale); | |||
13451 | InputV = ShuffleOffset(InputV); | |||
13452 | InputV = getEXTEND_VECTOR_INREG(AnyExt ? ISD::ANY_EXTEND : ISD::ZERO_EXTEND, | |||
13453 | DL, ExtVT, InputV, DAG); | |||
13454 | return DAG.getBitcast(VT, InputV); | |||
13455 | } | |||
13456 | ||||
13457 | assert(VT.is128BitVector() && "Only 128-bit vectors can be extended.")(static_cast <bool> (VT.is128BitVector() && "Only 128-bit vectors can be extended." ) ? void (0) : __assert_fail ("VT.is128BitVector() && \"Only 128-bit vectors can be extended.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13457, __extension__ __PRETTY_FUNCTION__)); | |||
13458 | ||||
13459 | // For any extends we can cheat for larger element sizes and use shuffle | |||
13460 | // instructions that can fold with a load and/or copy. | |||
13461 | if (AnyExt && EltBits == 32) { | |||
13462 | int PSHUFDMask[4] = {Offset, -1, SafeOffset(Offset + 1) ? Offset + 1 : -1, | |||
13463 | -1}; | |||
13464 | return DAG.getBitcast( | |||
13465 | VT, DAG.getNode(X86ISD::PSHUFD, DL, MVT::v4i32, | |||
13466 | DAG.getBitcast(MVT::v4i32, InputV), | |||
13467 | getV4X86ShuffleImm8ForMask(PSHUFDMask, DL, DAG))); | |||
13468 | } | |||
13469 | if (AnyExt && EltBits == 16 && Scale > 2) { | |||
13470 | int PSHUFDMask[4] = {Offset / 2, -1, | |||
13471 | SafeOffset(Offset + 1) ? (Offset + 1) / 2 : -1, -1}; | |||
13472 | InputV = DAG.getNode(X86ISD::PSHUFD, DL, MVT::v4i32, | |||
13473 | DAG.getBitcast(MVT::v4i32, InputV), | |||
13474 | getV4X86ShuffleImm8ForMask(PSHUFDMask, DL, DAG)); | |||
13475 | int PSHUFWMask[4] = {1, -1, -1, -1}; | |||
13476 | unsigned OddEvenOp = (Offset & 1) ? X86ISD::PSHUFLW : X86ISD::PSHUFHW; | |||
13477 | return DAG.getBitcast( | |||
13478 | VT, DAG.getNode(OddEvenOp, DL, MVT::v8i16, | |||
13479 | DAG.getBitcast(MVT::v8i16, InputV), | |||
13480 | getV4X86ShuffleImm8ForMask(PSHUFWMask, DL, DAG))); | |||
13481 | } | |||
13482 | ||||
13483 | // The SSE4A EXTRQ instruction can efficiently extend the first 2 lanes | |||
13484 | // to 64-bits. | |||
13485 | if ((Scale * EltBits) == 64 && EltBits < 32 && Subtarget.hasSSE4A()) { | |||
13486 | assert(NumElements == (int)Mask.size() && "Unexpected shuffle mask size!")(static_cast <bool> (NumElements == (int)Mask.size() && "Unexpected shuffle mask size!") ? void (0) : __assert_fail ( "NumElements == (int)Mask.size() && \"Unexpected shuffle mask size!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13486, __extension__ __PRETTY_FUNCTION__)); | |||
13487 | assert(VT.is128BitVector() && "Unexpected vector width!")(static_cast <bool> (VT.is128BitVector() && "Unexpected vector width!" ) ? void (0) : __assert_fail ("VT.is128BitVector() && \"Unexpected vector width!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13487, __extension__ __PRETTY_FUNCTION__)); | |||
13488 | ||||
13489 | int LoIdx = Offset * EltBits; | |||
13490 | SDValue Lo = DAG.getBitcast( | |||
13491 | MVT::v2i64, DAG.getNode(X86ISD::EXTRQI, DL, VT, InputV, | |||
13492 | DAG.getTargetConstant(EltBits, DL, MVT::i8), | |||
13493 | DAG.getTargetConstant(LoIdx, DL, MVT::i8))); | |||
13494 | ||||
13495 | if (isUndefUpperHalf(Mask) || !SafeOffset(Offset + 1)) | |||
13496 | return DAG.getBitcast(VT, Lo); | |||
13497 | ||||
13498 | int HiIdx = (Offset + 1) * EltBits; | |||
13499 | SDValue Hi = DAG.getBitcast( | |||
13500 | MVT::v2i64, DAG.getNode(X86ISD::EXTRQI, DL, VT, InputV, | |||
13501 | DAG.getTargetConstant(EltBits, DL, MVT::i8), | |||
13502 | DAG.getTargetConstant(HiIdx, DL, MVT::i8))); | |||
13503 | return DAG.getBitcast(VT, | |||
13504 | DAG.getNode(X86ISD::UNPCKL, DL, MVT::v2i64, Lo, Hi)); | |||
13505 | } | |||
13506 | ||||
13507 | // If this would require more than 2 unpack instructions to expand, use | |||
13508 | // pshufb when available. We can only use more than 2 unpack instructions | |||
13509 | // when zero extending i8 elements which also makes it easier to use pshufb. | |||
13510 | if (Scale > 4 && EltBits == 8 && Subtarget.hasSSSE3()) { | |||
13511 | assert(NumElements == 16 && "Unexpected byte vector width!")(static_cast <bool> (NumElements == 16 && "Unexpected byte vector width!" ) ? void (0) : __assert_fail ("NumElements == 16 && \"Unexpected byte vector width!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13511, __extension__ __PRETTY_FUNCTION__)); | |||
13512 | SDValue PSHUFBMask[16]; | |||
13513 | for (int i = 0; i < 16; ++i) { | |||
13514 | int Idx = Offset + (i / Scale); | |||
13515 | if ((i % Scale == 0 && SafeOffset(Idx))) { | |||
13516 | PSHUFBMask[i] = DAG.getConstant(Idx, DL, MVT::i8); | |||
13517 | continue; | |||
13518 | } | |||
13519 | PSHUFBMask[i] = | |||
13520 | AnyExt ? DAG.getUNDEF(MVT::i8) : DAG.getConstant(0x80, DL, MVT::i8); | |||
13521 | } | |||
13522 | InputV = DAG.getBitcast(MVT::v16i8, InputV); | |||
13523 | return DAG.getBitcast( | |||
13524 | VT, DAG.getNode(X86ISD::PSHUFB, DL, MVT::v16i8, InputV, | |||
13525 | DAG.getBuildVector(MVT::v16i8, DL, PSHUFBMask))); | |||
13526 | } | |||
13527 | ||||
13528 | // If we are extending from an offset, ensure we start on a boundary that | |||
13529 | // we can unpack from. | |||
13530 | int AlignToUnpack = Offset % (NumElements / Scale); | |||
13531 | if (AlignToUnpack) { | |||
13532 | SmallVector<int, 8> ShMask((unsigned)NumElements, -1); | |||
13533 | for (int i = AlignToUnpack; i < NumElements; ++i) | |||
13534 | ShMask[i - AlignToUnpack] = i; | |||
13535 | InputV = DAG.getVectorShuffle(VT, DL, InputV, DAG.getUNDEF(VT), ShMask); | |||
13536 | Offset -= AlignToUnpack; | |||
13537 | } | |||
13538 | ||||
13539 | // Otherwise emit a sequence of unpacks. | |||
13540 | do { | |||
13541 | unsigned UnpackLoHi = X86ISD::UNPCKL; | |||
13542 | if (Offset >= (NumElements / 2)) { | |||
13543 | UnpackLoHi = X86ISD::UNPCKH; | |||
13544 | Offset -= (NumElements / 2); | |||
13545 | } | |||
13546 | ||||
13547 | MVT InputVT = MVT::getVectorVT(MVT::getIntegerVT(EltBits), NumElements); | |||
13548 | SDValue Ext = AnyExt ? DAG.getUNDEF(InputVT) | |||
13549 | : getZeroVector(InputVT, Subtarget, DAG, DL); | |||
13550 | InputV = DAG.getBitcast(InputVT, InputV); | |||
13551 | InputV = DAG.getNode(UnpackLoHi, DL, InputVT, InputV, Ext); | |||
13552 | Scale /= 2; | |||
13553 | EltBits *= 2; | |||
13554 | NumElements /= 2; | |||
13555 | } while (Scale > 1); | |||
13556 | return DAG.getBitcast(VT, InputV); | |||
13557 | } | |||
13558 | ||||
13559 | /// Try to lower a vector shuffle as a zero extension on any microarch. | |||
13560 | /// | |||
13561 | /// This routine will try to do everything in its power to cleverly lower | |||
13562 | /// a shuffle which happens to match the pattern of a zero extend. It doesn't | |||
13563 | /// check for the profitability of this lowering, it tries to aggressively | |||
13564 | /// match this pattern. It will use all of the micro-architectural details it | |||
13565 | /// can to emit an efficient lowering. It handles both blends with all-zero | |||
13566 | /// inputs to explicitly zero-extend and undef-lanes (sometimes undef due to | |||
13567 | /// masking out later). | |||
13568 | /// | |||
13569 | /// The reason we have dedicated lowering for zext-style shuffles is that they | |||
13570 | /// are both incredibly common and often quite performance sensitive. | |||
13571 | static SDValue lowerShuffleAsZeroOrAnyExtend( | |||
13572 | const SDLoc &DL, MVT VT, SDValue V1, SDValue V2, ArrayRef<int> Mask, | |||
13573 | const APInt &Zeroable, const X86Subtarget &Subtarget, | |||
13574 | SelectionDAG &DAG) { | |||
13575 | int Bits = VT.getSizeInBits(); | |||
13576 | int NumLanes = Bits / 128; | |||
13577 | int NumElements = VT.getVectorNumElements(); | |||
13578 | int NumEltsPerLane = NumElements / NumLanes; | |||
13579 | assert(VT.getScalarSizeInBits() <= 32 &&(static_cast <bool> (VT.getScalarSizeInBits() <= 32 && "Exceeds 32-bit integer zero extension limit") ? void (0) : __assert_fail ("VT.getScalarSizeInBits() <= 32 && \"Exceeds 32-bit integer zero extension limit\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13580, __extension__ __PRETTY_FUNCTION__)) | |||
13580 | "Exceeds 32-bit integer zero extension limit")(static_cast <bool> (VT.getScalarSizeInBits() <= 32 && "Exceeds 32-bit integer zero extension limit") ? void (0) : __assert_fail ("VT.getScalarSizeInBits() <= 32 && \"Exceeds 32-bit integer zero extension limit\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13580, __extension__ __PRETTY_FUNCTION__)); | |||
13581 | assert((int)Mask.size() == NumElements && "Unexpected shuffle mask size")(static_cast <bool> ((int)Mask.size() == NumElements && "Unexpected shuffle mask size") ? void (0) : __assert_fail ( "(int)Mask.size() == NumElements && \"Unexpected shuffle mask size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13581, __extension__ __PRETTY_FUNCTION__)); | |||
13582 | ||||
13583 | // Define a helper function to check a particular ext-scale and lower to it if | |||
13584 | // valid. | |||
13585 | auto Lower = [&](int Scale) -> SDValue { | |||
13586 | SDValue InputV; | |||
13587 | bool AnyExt = true; | |||
13588 | int Offset = 0; | |||
13589 | int Matches = 0; | |||
13590 | for (int i = 0; i < NumElements; ++i) { | |||
13591 | int M = Mask[i]; | |||
13592 | if (M < 0) | |||
13593 | continue; // Valid anywhere but doesn't tell us anything. | |||
13594 | if (i % Scale != 0) { | |||
13595 | // Each of the extended elements need to be zeroable. | |||
13596 | if (!Zeroable[i]) | |||
13597 | return SDValue(); | |||
13598 | ||||
13599 | // We no longer are in the anyext case. | |||
13600 | AnyExt = false; | |||
13601 | continue; | |||
13602 | } | |||
13603 | ||||
13604 | // Each of the base elements needs to be consecutive indices into the | |||
13605 | // same input vector. | |||
13606 | SDValue V = M < NumElements ? V1 : V2; | |||
13607 | M = M % NumElements; | |||
13608 | if (!InputV) { | |||
13609 | InputV = V; | |||
13610 | Offset = M - (i / Scale); | |||
13611 | } else if (InputV != V) | |||
13612 | return SDValue(); // Flip-flopping inputs. | |||
13613 | ||||
13614 | // Offset must start in the lowest 128-bit lane or at the start of an | |||
13615 | // upper lane. | |||
13616 | // FIXME: Is it ever worth allowing a negative base offset? | |||
13617 | if (!((0 <= Offset && Offset < NumEltsPerLane) || | |||
13618 | (Offset % NumEltsPerLane) == 0)) | |||
13619 | return SDValue(); | |||
13620 | ||||
13621 | // If we are offsetting, all referenced entries must come from the same | |||
13622 | // lane. | |||
13623 | if (Offset && (Offset / NumEltsPerLane) != (M / NumEltsPerLane)) | |||
13624 | return SDValue(); | |||
13625 | ||||
13626 | if ((M % NumElements) != (Offset + (i / Scale))) | |||
13627 | return SDValue(); // Non-consecutive strided elements. | |||
13628 | Matches++; | |||
13629 | } | |||
13630 | ||||
13631 | // If we fail to find an input, we have a zero-shuffle which should always | |||
13632 | // have already been handled. | |||
13633 | // FIXME: Maybe handle this here in case during blending we end up with one? | |||
13634 | if (!InputV) | |||
13635 | return SDValue(); | |||
13636 | ||||
13637 | // If we are offsetting, don't extend if we only match a single input, we | |||
13638 | // can always do better by using a basic PSHUF or PUNPCK. | |||
13639 | if (Offset != 0 && Matches < 2) | |||
13640 | return SDValue(); | |||
13641 | ||||
13642 | return lowerShuffleAsSpecificZeroOrAnyExtend(DL, VT, Scale, Offset, AnyExt, | |||
13643 | InputV, Mask, Subtarget, DAG); | |||
13644 | }; | |||
13645 | ||||
13646 | // The widest scale possible for extending is to a 64-bit integer. | |||
13647 | assert(Bits % 64 == 0 &&(static_cast <bool> (Bits % 64 == 0 && "The number of bits in a vector must be divisible by 64 on x86!" ) ? void (0) : __assert_fail ("Bits % 64 == 0 && \"The number of bits in a vector must be divisible by 64 on x86!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13648, __extension__ __PRETTY_FUNCTION__)) | |||
13648 | "The number of bits in a vector must be divisible by 64 on x86!")(static_cast <bool> (Bits % 64 == 0 && "The number of bits in a vector must be divisible by 64 on x86!" ) ? void (0) : __assert_fail ("Bits % 64 == 0 && \"The number of bits in a vector must be divisible by 64 on x86!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13648, __extension__ __PRETTY_FUNCTION__)); | |||
13649 | int NumExtElements = Bits / 64; | |||
13650 | ||||
13651 | // Each iteration, try extending the elements half as much, but into twice as | |||
13652 | // many elements. | |||
13653 | for (; NumExtElements < NumElements; NumExtElements *= 2) { | |||
13654 | assert(NumElements % NumExtElements == 0 &&(static_cast <bool> (NumElements % NumExtElements == 0 && "The input vector size must be divisible by the extended size." ) ? void (0) : __assert_fail ("NumElements % NumExtElements == 0 && \"The input vector size must be divisible by the extended size.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13655, __extension__ __PRETTY_FUNCTION__)) | |||
13655 | "The input vector size must be divisible by the extended size.")(static_cast <bool> (NumElements % NumExtElements == 0 && "The input vector size must be divisible by the extended size." ) ? void (0) : __assert_fail ("NumElements % NumExtElements == 0 && \"The input vector size must be divisible by the extended size.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13655, __extension__ __PRETTY_FUNCTION__)); | |||
13656 | if (SDValue V = Lower(NumElements / NumExtElements)) | |||
13657 | return V; | |||
13658 | } | |||
13659 | ||||
13660 | // General extends failed, but 128-bit vectors may be able to use MOVQ. | |||
13661 | if (Bits != 128) | |||
13662 | return SDValue(); | |||
13663 | ||||
13664 | // Returns one of the source operands if the shuffle can be reduced to a | |||
13665 | // MOVQ, copying the lower 64-bits and zero-extending to the upper 64-bits. | |||
13666 | auto CanZExtLowHalf = [&]() { | |||
13667 | for (int i = NumElements / 2; i != NumElements; ++i) | |||
13668 | if (!Zeroable[i]) | |||
13669 | return SDValue(); | |||
13670 | if (isSequentialOrUndefInRange(Mask, 0, NumElements / 2, 0)) | |||
13671 | return V1; | |||
13672 | if (isSequentialOrUndefInRange(Mask, 0, NumElements / 2, NumElements)) | |||
13673 | return V2; | |||
13674 | return SDValue(); | |||
13675 | }; | |||
13676 | ||||
13677 | if (SDValue V = CanZExtLowHalf()) { | |||
13678 | V = DAG.getBitcast(MVT::v2i64, V); | |||
13679 | V = DAG.getNode(X86ISD::VZEXT_MOVL, DL, MVT::v2i64, V); | |||
13680 | return DAG.getBitcast(VT, V); | |||
13681 | } | |||
13682 | ||||
13683 | // No viable ext lowering found. | |||
13684 | return SDValue(); | |||
13685 | } | |||
13686 | ||||
13687 | /// Try to get a scalar value for a specific element of a vector. | |||
13688 | /// | |||
13689 | /// Looks through BUILD_VECTOR and SCALAR_TO_VECTOR nodes to find a scalar. | |||
13690 | static SDValue getScalarValueForVectorElement(SDValue V, int Idx, | |||
13691 | SelectionDAG &DAG) { | |||
13692 | MVT VT = V.getSimpleValueType(); | |||
13693 | MVT EltVT = VT.getVectorElementType(); | |||
13694 | V = peekThroughBitcasts(V); | |||
13695 | ||||
13696 | // If the bitcasts shift the element size, we can't extract an equivalent | |||
13697 | // element from it. | |||
13698 | MVT NewVT = V.getSimpleValueType(); | |||
13699 | if (!NewVT.isVector() || NewVT.getScalarSizeInBits() != VT.getScalarSizeInBits()) | |||
13700 | return SDValue(); | |||
13701 | ||||
13702 | if (V.getOpcode() == ISD::BUILD_VECTOR || | |||
13703 | (Idx == 0 && V.getOpcode() == ISD::SCALAR_TO_VECTOR)) { | |||
13704 | // Ensure the scalar operand is the same size as the destination. | |||
13705 | // FIXME: Add support for scalar truncation where possible. | |||
13706 | SDValue S = V.getOperand(Idx); | |||
13707 | if (EltVT.getSizeInBits() == S.getSimpleValueType().getSizeInBits()) | |||
13708 | return DAG.getBitcast(EltVT, S); | |||
13709 | } | |||
13710 | ||||
13711 | return SDValue(); | |||
13712 | } | |||
13713 | ||||
13714 | /// Helper to test for a load that can be folded with x86 shuffles. | |||
13715 | /// | |||
13716 | /// This is particularly important because the set of instructions varies | |||
13717 | /// significantly based on whether the operand is a load or not. | |||
13718 | static bool isShuffleFoldableLoad(SDValue V) { | |||
13719 | V = peekThroughBitcasts(V); | |||
13720 | return ISD::isNON_EXTLoad(V.getNode()); | |||
13721 | } | |||
13722 | ||||
13723 | /// Try to lower insertion of a single element into a zero vector. | |||
13724 | /// | |||
13725 | /// This is a common pattern that we have especially efficient patterns to lower | |||
13726 | /// across all subtarget feature sets. | |||
13727 | static SDValue lowerShuffleAsElementInsertion( | |||
13728 | const SDLoc &DL, MVT VT, SDValue V1, SDValue V2, ArrayRef<int> Mask, | |||
13729 | const APInt &Zeroable, const X86Subtarget &Subtarget, | |||
13730 | SelectionDAG &DAG) { | |||
13731 | MVT ExtVT = VT; | |||
13732 | MVT EltVT = VT.getVectorElementType(); | |||
13733 | ||||
13734 | int V2Index = | |||
13735 | find_if(Mask, [&Mask](int M) { return M >= (int)Mask.size(); }) - | |||
13736 | Mask.begin(); | |||
13737 | bool IsV1Zeroable = true; | |||
13738 | for (int i = 0, Size = Mask.size(); i < Size; ++i) | |||
13739 | if (i != V2Index && !Zeroable[i]) { | |||
13740 | IsV1Zeroable = false; | |||
13741 | break; | |||
13742 | } | |||
13743 | ||||
13744 | // Check for a single input from a SCALAR_TO_VECTOR node. | |||
13745 | // FIXME: All of this should be canonicalized into INSERT_VECTOR_ELT and | |||
13746 | // all the smarts here sunk into that routine. However, the current | |||
13747 | // lowering of BUILD_VECTOR makes that nearly impossible until the old | |||
13748 | // vector shuffle lowering is dead. | |||
13749 | SDValue V2S = getScalarValueForVectorElement(V2, Mask[V2Index] - Mask.size(), | |||
13750 | DAG); | |||
13751 | if (V2S && DAG.getTargetLoweringInfo().isTypeLegal(V2S.getValueType())) { | |||
13752 | // We need to zext the scalar if it is smaller than an i32. | |||
13753 | V2S = DAG.getBitcast(EltVT, V2S); | |||
13754 | if (EltVT == MVT::i8 || (EltVT == MVT::i16 && !Subtarget.hasFP16())) { | |||
13755 | // Using zext to expand a narrow element won't work for non-zero | |||
13756 | // insertions. | |||
13757 | if (!IsV1Zeroable) | |||
13758 | return SDValue(); | |||
13759 | ||||
13760 | // Zero-extend directly to i32. | |||
13761 | ExtVT = MVT::getVectorVT(MVT::i32, ExtVT.getSizeInBits() / 32); | |||
13762 | V2S = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, V2S); | |||
13763 | } | |||
13764 | V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, ExtVT, V2S); | |||
13765 | } else if (Mask[V2Index] != (int)Mask.size() || EltVT == MVT::i8 || | |||
13766 | EltVT == MVT::i16) { | |||
13767 | // Either not inserting from the low element of the input or the input | |||
13768 | // element size is too small to use VZEXT_MOVL to clear the high bits. | |||
13769 | return SDValue(); | |||
13770 | } | |||
13771 | ||||
13772 | if (!IsV1Zeroable) { | |||
13773 | // If V1 can't be treated as a zero vector we have fewer options to lower | |||
13774 | // this. We can't support integer vectors or non-zero targets cheaply, and | |||
13775 | // the V1 elements can't be permuted in any way. | |||
13776 | assert(VT == ExtVT && "Cannot change extended type when non-zeroable!")(static_cast <bool> (VT == ExtVT && "Cannot change extended type when non-zeroable!" ) ? void (0) : __assert_fail ("VT == ExtVT && \"Cannot change extended type when non-zeroable!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13776, __extension__ __PRETTY_FUNCTION__)); | |||
13777 | if (!VT.isFloatingPoint() || V2Index != 0) | |||
13778 | return SDValue(); | |||
13779 | SmallVector<int, 8> V1Mask(Mask.begin(), Mask.end()); | |||
13780 | V1Mask[V2Index] = -1; | |||
13781 | if (!isNoopShuffleMask(V1Mask)) | |||
13782 | return SDValue(); | |||
13783 | if (!VT.is128BitVector()) | |||
13784 | return SDValue(); | |||
13785 | ||||
13786 | // Otherwise, use MOVSD, MOVSS or MOVSH. | |||
13787 | unsigned MovOpc = 0; | |||
13788 | if (EltVT == MVT::f16) | |||
13789 | MovOpc = X86ISD::MOVSH; | |||
13790 | else if (EltVT == MVT::f32) | |||
13791 | MovOpc = X86ISD::MOVSS; | |||
13792 | else if (EltVT == MVT::f64) | |||
13793 | MovOpc = X86ISD::MOVSD; | |||
13794 | else | |||
13795 | llvm_unreachable("Unsupported floating point element type to handle!")::llvm::llvm_unreachable_internal("Unsupported floating point element type to handle!" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13795); | |||
13796 | return DAG.getNode(MovOpc, DL, ExtVT, V1, V2); | |||
13797 | } | |||
13798 | ||||
13799 | // This lowering only works for the low element with floating point vectors. | |||
13800 | if (VT.isFloatingPoint() && V2Index != 0) | |||
13801 | return SDValue(); | |||
13802 | ||||
13803 | V2 = DAG.getNode(X86ISD::VZEXT_MOVL, DL, ExtVT, V2); | |||
13804 | if (ExtVT != VT) | |||
13805 | V2 = DAG.getBitcast(VT, V2); | |||
13806 | ||||
13807 | if (V2Index != 0) { | |||
13808 | // If we have 4 or fewer lanes we can cheaply shuffle the element into | |||
13809 | // the desired position. Otherwise it is more efficient to do a vector | |||
13810 | // shift left. We know that we can do a vector shift left because all | |||
13811 | // the inputs are zero. | |||
13812 | if (VT.isFloatingPoint() || VT.getVectorNumElements() <= 4) { | |||
13813 | SmallVector<int, 4> V2Shuffle(Mask.size(), 1); | |||
13814 | V2Shuffle[V2Index] = 0; | |||
13815 | V2 = DAG.getVectorShuffle(VT, DL, V2, DAG.getUNDEF(VT), V2Shuffle); | |||
13816 | } else { | |||
13817 | V2 = DAG.getBitcast(MVT::v16i8, V2); | |||
13818 | V2 = DAG.getNode(X86ISD::VSHLDQ, DL, MVT::v16i8, V2, | |||
13819 | DAG.getTargetConstant( | |||
13820 | V2Index * EltVT.getSizeInBits() / 8, DL, MVT::i8)); | |||
13821 | V2 = DAG.getBitcast(VT, V2); | |||
13822 | } | |||
13823 | } | |||
13824 | return V2; | |||
13825 | } | |||
13826 | ||||
13827 | /// Try to lower broadcast of a single - truncated - integer element, | |||
13828 | /// coming from a scalar_to_vector/build_vector node \p V0 with larger elements. | |||
13829 | /// | |||
13830 | /// This assumes we have AVX2. | |||
13831 | static SDValue lowerShuffleAsTruncBroadcast(const SDLoc &DL, MVT VT, SDValue V0, | |||
13832 | int BroadcastIdx, | |||
13833 | const X86Subtarget &Subtarget, | |||
13834 | SelectionDAG &DAG) { | |||
13835 | assert(Subtarget.hasAVX2() &&(static_cast <bool> (Subtarget.hasAVX2() && "We can only lower integer broadcasts with AVX2!" ) ? void (0) : __assert_fail ("Subtarget.hasAVX2() && \"We can only lower integer broadcasts with AVX2!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13836, __extension__ __PRETTY_FUNCTION__)) | |||
13836 | "We can only lower integer broadcasts with AVX2!")(static_cast <bool> (Subtarget.hasAVX2() && "We can only lower integer broadcasts with AVX2!" ) ? void (0) : __assert_fail ("Subtarget.hasAVX2() && \"We can only lower integer broadcasts with AVX2!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13836, __extension__ __PRETTY_FUNCTION__)); | |||
13837 | ||||
13838 | MVT EltVT = VT.getVectorElementType(); | |||
13839 | MVT V0VT = V0.getSimpleValueType(); | |||
13840 | ||||
13841 | assert(VT.isInteger() && "Unexpected non-integer trunc broadcast!")(static_cast <bool> (VT.isInteger() && "Unexpected non-integer trunc broadcast!" ) ? void (0) : __assert_fail ("VT.isInteger() && \"Unexpected non-integer trunc broadcast!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13841, __extension__ __PRETTY_FUNCTION__)); | |||
13842 | assert(V0VT.isVector() && "Unexpected non-vector vector-sized value!")(static_cast <bool> (V0VT.isVector() && "Unexpected non-vector vector-sized value!" ) ? void (0) : __assert_fail ("V0VT.isVector() && \"Unexpected non-vector vector-sized value!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13842, __extension__ __PRETTY_FUNCTION__)); | |||
13843 | ||||
13844 | MVT V0EltVT = V0VT.getVectorElementType(); | |||
13845 | if (!V0EltVT.isInteger()) | |||
13846 | return SDValue(); | |||
13847 | ||||
13848 | const unsigned EltSize = EltVT.getSizeInBits(); | |||
13849 | const unsigned V0EltSize = V0EltVT.getSizeInBits(); | |||
13850 | ||||
13851 | // This is only a truncation if the original element type is larger. | |||
13852 | if (V0EltSize <= EltSize) | |||
13853 | return SDValue(); | |||
13854 | ||||
13855 | assert(((V0EltSize % EltSize) == 0) &&(static_cast <bool> (((V0EltSize % EltSize) == 0) && "Scalar type sizes must all be powers of 2 on x86!") ? void ( 0) : __assert_fail ("((V0EltSize % EltSize) == 0) && \"Scalar type sizes must all be powers of 2 on x86!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13856, __extension__ __PRETTY_FUNCTION__)) | |||
13856 | "Scalar type sizes must all be powers of 2 on x86!")(static_cast <bool> (((V0EltSize % EltSize) == 0) && "Scalar type sizes must all be powers of 2 on x86!") ? void ( 0) : __assert_fail ("((V0EltSize % EltSize) == 0) && \"Scalar type sizes must all be powers of 2 on x86!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13856, __extension__ __PRETTY_FUNCTION__)); | |||
13857 | ||||
13858 | const unsigned V0Opc = V0.getOpcode(); | |||
13859 | const unsigned Scale = V0EltSize / EltSize; | |||
13860 | const unsigned V0BroadcastIdx = BroadcastIdx / Scale; | |||
13861 | ||||
13862 | if ((V0Opc != ISD::SCALAR_TO_VECTOR || V0BroadcastIdx != 0) && | |||
13863 | V0Opc != ISD::BUILD_VECTOR) | |||
13864 | return SDValue(); | |||
13865 | ||||
13866 | SDValue Scalar = V0.getOperand(V0BroadcastIdx); | |||
13867 | ||||
13868 | // If we're extracting non-least-significant bits, shift so we can truncate. | |||
13869 | // Hopefully, we can fold away the trunc/srl/load into the broadcast. | |||
13870 | // Even if we can't (and !isShuffleFoldableLoad(Scalar)), prefer | |||
13871 | // vpbroadcast+vmovd+shr to vpshufb(m)+vmovd. | |||
13872 | if (const int OffsetIdx = BroadcastIdx % Scale) | |||
13873 | Scalar = DAG.getNode(ISD::SRL, DL, Scalar.getValueType(), Scalar, | |||
13874 | DAG.getConstant(OffsetIdx * EltSize, DL, MVT::i8)); | |||
13875 | ||||
13876 | return DAG.getNode(X86ISD::VBROADCAST, DL, VT, | |||
13877 | DAG.getNode(ISD::TRUNCATE, DL, EltVT, Scalar)); | |||
13878 | } | |||
13879 | ||||
13880 | /// Test whether this can be lowered with a single SHUFPS instruction. | |||
13881 | /// | |||
13882 | /// This is used to disable more specialized lowerings when the shufps lowering | |||
13883 | /// will happen to be efficient. | |||
13884 | static bool isSingleSHUFPSMask(ArrayRef<int> Mask) { | |||
13885 | // This routine only handles 128-bit shufps. | |||
13886 | assert(Mask.size() == 4 && "Unsupported mask size!")(static_cast <bool> (Mask.size() == 4 && "Unsupported mask size!" ) ? void (0) : __assert_fail ("Mask.size() == 4 && \"Unsupported mask size!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13886, __extension__ __PRETTY_FUNCTION__)); | |||
13887 | assert(Mask[0] >= -1 && Mask[0] < 8 && "Out of bound mask element!")(static_cast <bool> (Mask[0] >= -1 && Mask[0 ] < 8 && "Out of bound mask element!") ? void (0) : __assert_fail ("Mask[0] >= -1 && Mask[0] < 8 && \"Out of bound mask element!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13887, __extension__ __PRETTY_FUNCTION__)); | |||
13888 | assert(Mask[1] >= -1 && Mask[1] < 8 && "Out of bound mask element!")(static_cast <bool> (Mask[1] >= -1 && Mask[1 ] < 8 && "Out of bound mask element!") ? void (0) : __assert_fail ("Mask[1] >= -1 && Mask[1] < 8 && \"Out of bound mask element!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13888, __extension__ __PRETTY_FUNCTION__)); | |||
13889 | assert(Mask[2] >= -1 && Mask[2] < 8 && "Out of bound mask element!")(static_cast <bool> (Mask[2] >= -1 && Mask[2 ] < 8 && "Out of bound mask element!") ? void (0) : __assert_fail ("Mask[2] >= -1 && Mask[2] < 8 && \"Out of bound mask element!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13889, __extension__ __PRETTY_FUNCTION__)); | |||
13890 | assert(Mask[3] >= -1 && Mask[3] < 8 && "Out of bound mask element!")(static_cast <bool> (Mask[3] >= -1 && Mask[3 ] < 8 && "Out of bound mask element!") ? void (0) : __assert_fail ("Mask[3] >= -1 && Mask[3] < 8 && \"Out of bound mask element!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13890, __extension__ __PRETTY_FUNCTION__)); | |||
13891 | ||||
13892 | // To lower with a single SHUFPS we need to have the low half and high half | |||
13893 | // each requiring a single input. | |||
13894 | if (Mask[0] >= 0 && Mask[1] >= 0 && (Mask[0] < 4) != (Mask[1] < 4)) | |||
13895 | return false; | |||
13896 | if (Mask[2] >= 0 && Mask[3] >= 0 && (Mask[2] < 4) != (Mask[3] < 4)) | |||
13897 | return false; | |||
13898 | ||||
13899 | return true; | |||
13900 | } | |||
13901 | ||||
13902 | /// If we are extracting two 128-bit halves of a vector and shuffling the | |||
13903 | /// result, match that to a 256-bit AVX2 vperm* instruction to avoid a | |||
13904 | /// multi-shuffle lowering. | |||
13905 | static SDValue lowerShuffleOfExtractsAsVperm(const SDLoc &DL, SDValue N0, | |||
13906 | SDValue N1, ArrayRef<int> Mask, | |||
13907 | SelectionDAG &DAG) { | |||
13908 | MVT VT = N0.getSimpleValueType(); | |||
13909 | assert((VT.is128BitVector() &&(static_cast <bool> ((VT.is128BitVector() && (VT .getScalarSizeInBits() == 32 || VT.getScalarSizeInBits() == 64 )) && "VPERM* family of shuffles requires 32-bit or 64-bit elements" ) ? void (0) : __assert_fail ("(VT.is128BitVector() && (VT.getScalarSizeInBits() == 32 || VT.getScalarSizeInBits() == 64)) && \"VPERM* family of shuffles requires 32-bit or 64-bit elements\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13911, __extension__ __PRETTY_FUNCTION__)) | |||
13910 | (VT.getScalarSizeInBits() == 32 || VT.getScalarSizeInBits() == 64)) &&(static_cast <bool> ((VT.is128BitVector() && (VT .getScalarSizeInBits() == 32 || VT.getScalarSizeInBits() == 64 )) && "VPERM* family of shuffles requires 32-bit or 64-bit elements" ) ? void (0) : __assert_fail ("(VT.is128BitVector() && (VT.getScalarSizeInBits() == 32 || VT.getScalarSizeInBits() == 64)) && \"VPERM* family of shuffles requires 32-bit or 64-bit elements\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13911, __extension__ __PRETTY_FUNCTION__)) | |||
13911 | "VPERM* family of shuffles requires 32-bit or 64-bit elements")(static_cast <bool> ((VT.is128BitVector() && (VT .getScalarSizeInBits() == 32 || VT.getScalarSizeInBits() == 64 )) && "VPERM* family of shuffles requires 32-bit or 64-bit elements" ) ? void (0) : __assert_fail ("(VT.is128BitVector() && (VT.getScalarSizeInBits() == 32 || VT.getScalarSizeInBits() == 64)) && \"VPERM* family of shuffles requires 32-bit or 64-bit elements\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13911, __extension__ __PRETTY_FUNCTION__)); | |||
13912 | ||||
13913 | // Check that both sources are extracts of the same source vector. | |||
13914 | if (!N0.hasOneUse() || !N1.hasOneUse() || | |||
13915 | N0.getOpcode() != ISD::EXTRACT_SUBVECTOR || | |||
13916 | N1.getOpcode() != ISD::EXTRACT_SUBVECTOR || | |||
13917 | N0.getOperand(0) != N1.getOperand(0)) | |||
13918 | return SDValue(); | |||
13919 | ||||
13920 | SDValue WideVec = N0.getOperand(0); | |||
13921 | MVT WideVT = WideVec.getSimpleValueType(); | |||
13922 | if (!WideVT.is256BitVector()) | |||
13923 | return SDValue(); | |||
13924 | ||||
13925 | // Match extracts of each half of the wide source vector. Commute the shuffle | |||
13926 | // if the extract of the low half is N1. | |||
13927 | unsigned NumElts = VT.getVectorNumElements(); | |||
13928 | SmallVector<int, 4> NewMask(Mask.begin(), Mask.end()); | |||
13929 | const APInt &ExtIndex0 = N0.getConstantOperandAPInt(1); | |||
13930 | const APInt &ExtIndex1 = N1.getConstantOperandAPInt(1); | |||
13931 | if (ExtIndex1 == 0 && ExtIndex0 == NumElts) | |||
13932 | ShuffleVectorSDNode::commuteMask(NewMask); | |||
13933 | else if (ExtIndex0 != 0 || ExtIndex1 != NumElts) | |||
13934 | return SDValue(); | |||
13935 | ||||
13936 | // Final bailout: if the mask is simple, we are better off using an extract | |||
13937 | // and a simple narrow shuffle. Prefer extract+unpack(h/l)ps to vpermps | |||
13938 | // because that avoids a constant load from memory. | |||
13939 | if (NumElts == 4 && | |||
13940 | (isSingleSHUFPSMask(NewMask) || is128BitUnpackShuffleMask(NewMask))) | |||
13941 | return SDValue(); | |||
13942 | ||||
13943 | // Extend the shuffle mask with undef elements. | |||
13944 | NewMask.append(NumElts, -1); | |||
13945 | ||||
13946 | // shuf (extract X, 0), (extract X, 4), M --> extract (shuf X, undef, M'), 0 | |||
13947 | SDValue Shuf = DAG.getVectorShuffle(WideVT, DL, WideVec, DAG.getUNDEF(WideVT), | |||
13948 | NewMask); | |||
13949 | // This is free: ymm -> xmm. | |||
13950 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Shuf, | |||
13951 | DAG.getIntPtrConstant(0, DL)); | |||
13952 | } | |||
13953 | ||||
13954 | /// Try to lower broadcast of a single element. | |||
13955 | /// | |||
13956 | /// For convenience, this code also bundles all of the subtarget feature set | |||
13957 | /// filtering. While a little annoying to re-dispatch on type here, there isn't | |||
13958 | /// a convenient way to factor it out. | |||
13959 | static SDValue lowerShuffleAsBroadcast(const SDLoc &DL, MVT VT, SDValue V1, | |||
13960 | SDValue V2, ArrayRef<int> Mask, | |||
13961 | const X86Subtarget &Subtarget, | |||
13962 | SelectionDAG &DAG) { | |||
13963 | if (!((Subtarget.hasSSE3() && VT == MVT::v2f64) || | |||
13964 | (Subtarget.hasAVX() && VT.isFloatingPoint()) || | |||
13965 | (Subtarget.hasAVX2() && VT.isInteger()))) | |||
13966 | return SDValue(); | |||
13967 | ||||
13968 | // With MOVDDUP (v2f64) we can broadcast from a register or a load, otherwise | |||
13969 | // we can only broadcast from a register with AVX2. | |||
13970 | unsigned NumEltBits = VT.getScalarSizeInBits(); | |||
13971 | unsigned Opcode = (VT == MVT::v2f64 && !Subtarget.hasAVX2()) | |||
13972 | ? X86ISD::MOVDDUP | |||
13973 | : X86ISD::VBROADCAST; | |||
13974 | bool BroadcastFromReg = (Opcode == X86ISD::MOVDDUP) || Subtarget.hasAVX2(); | |||
13975 | ||||
13976 | // Check that the mask is a broadcast. | |||
13977 | int BroadcastIdx = getSplatIndex(Mask); | |||
13978 | if (BroadcastIdx < 0) | |||
13979 | return SDValue(); | |||
13980 | assert(BroadcastIdx < (int)Mask.size() && "We only expect to be called with "(static_cast <bool> (BroadcastIdx < (int)Mask.size() && "We only expect to be called with " "a sorted mask where the broadcast " "comes from V1.") ? void (0) : __assert_fail ("BroadcastIdx < (int)Mask.size() && \"We only expect to be called with \" \"a sorted mask where the broadcast \" \"comes from V1.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13982, __extension__ __PRETTY_FUNCTION__)) | |||
13981 | "a sorted mask where the broadcast "(static_cast <bool> (BroadcastIdx < (int)Mask.size() && "We only expect to be called with " "a sorted mask where the broadcast " "comes from V1.") ? void (0) : __assert_fail ("BroadcastIdx < (int)Mask.size() && \"We only expect to be called with \" \"a sorted mask where the broadcast \" \"comes from V1.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13982, __extension__ __PRETTY_FUNCTION__)) | |||
13982 | "comes from V1.")(static_cast <bool> (BroadcastIdx < (int)Mask.size() && "We only expect to be called with " "a sorted mask where the broadcast " "comes from V1.") ? void (0) : __assert_fail ("BroadcastIdx < (int)Mask.size() && \"We only expect to be called with \" \"a sorted mask where the broadcast \" \"comes from V1.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 13982, __extension__ __PRETTY_FUNCTION__)); | |||
13983 | ||||
13984 | // Go up the chain of (vector) values to find a scalar load that we can | |||
13985 | // combine with the broadcast. | |||
13986 | // TODO: Combine this logic with findEltLoadSrc() used by | |||
13987 | // EltsFromConsecutiveLoads(). | |||
13988 | int BitOffset = BroadcastIdx * NumEltBits; | |||
13989 | SDValue V = V1; | |||
13990 | for (;;) { | |||
13991 | switch (V.getOpcode()) { | |||
13992 | case ISD::BITCAST: { | |||
13993 | V = V.getOperand(0); | |||
13994 | continue; | |||
13995 | } | |||
13996 | case ISD::CONCAT_VECTORS: { | |||
13997 | int OpBitWidth = V.getOperand(0).getValueSizeInBits(); | |||
13998 | int OpIdx = BitOffset / OpBitWidth; | |||
13999 | V = V.getOperand(OpIdx); | |||
14000 | BitOffset %= OpBitWidth; | |||
14001 | continue; | |||
14002 | } | |||
14003 | case ISD::EXTRACT_SUBVECTOR: { | |||
14004 | // The extraction index adds to the existing offset. | |||
14005 | unsigned EltBitWidth = V.getScalarValueSizeInBits(); | |||
14006 | unsigned Idx = V.getConstantOperandVal(1); | |||
14007 | unsigned BeginOffset = Idx * EltBitWidth; | |||
14008 | BitOffset += BeginOffset; | |||
14009 | V = V.getOperand(0); | |||
14010 | continue; | |||
14011 | } | |||
14012 | case ISD::INSERT_SUBVECTOR: { | |||
14013 | SDValue VOuter = V.getOperand(0), VInner = V.getOperand(1); | |||
14014 | int EltBitWidth = VOuter.getScalarValueSizeInBits(); | |||
14015 | int Idx = (int)V.getConstantOperandVal(2); | |||
14016 | int NumSubElts = (int)VInner.getSimpleValueType().getVectorNumElements(); | |||
14017 | int BeginOffset = Idx * EltBitWidth; | |||
14018 | int EndOffset = BeginOffset + NumSubElts * EltBitWidth; | |||
14019 | if (BeginOffset <= BitOffset && BitOffset < EndOffset) { | |||
14020 | BitOffset -= BeginOffset; | |||
14021 | V = VInner; | |||
14022 | } else { | |||
14023 | V = VOuter; | |||
14024 | } | |||
14025 | continue; | |||
14026 | } | |||
14027 | } | |||
14028 | break; | |||
14029 | } | |||
14030 | assert((BitOffset % NumEltBits) == 0 && "Illegal bit-offset")(static_cast <bool> ((BitOffset % NumEltBits) == 0 && "Illegal bit-offset") ? void (0) : __assert_fail ("(BitOffset % NumEltBits) == 0 && \"Illegal bit-offset\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14030, __extension__ __PRETTY_FUNCTION__)); | |||
14031 | BroadcastIdx = BitOffset / NumEltBits; | |||
14032 | ||||
14033 | // Do we need to bitcast the source to retrieve the original broadcast index? | |||
14034 | bool BitCastSrc = V.getScalarValueSizeInBits() != NumEltBits; | |||
14035 | ||||
14036 | // Check if this is a broadcast of a scalar. We special case lowering | |||
14037 | // for scalars so that we can more effectively fold with loads. | |||
14038 | // If the original value has a larger element type than the shuffle, the | |||
14039 | // broadcast element is in essence truncated. Make that explicit to ease | |||
14040 | // folding. | |||
14041 | if (BitCastSrc && VT.isInteger()) | |||
14042 | if (SDValue TruncBroadcast = lowerShuffleAsTruncBroadcast( | |||
14043 | DL, VT, V, BroadcastIdx, Subtarget, DAG)) | |||
14044 | return TruncBroadcast; | |||
14045 | ||||
14046 | // Also check the simpler case, where we can directly reuse the scalar. | |||
14047 | if (!BitCastSrc && | |||
14048 | ((V.getOpcode() == ISD::BUILD_VECTOR && V.hasOneUse()) || | |||
14049 | (V.getOpcode() == ISD::SCALAR_TO_VECTOR && BroadcastIdx == 0))) { | |||
14050 | V = V.getOperand(BroadcastIdx); | |||
14051 | ||||
14052 | // If we can't broadcast from a register, check that the input is a load. | |||
14053 | if (!BroadcastFromReg && !isShuffleFoldableLoad(V)) | |||
14054 | return SDValue(); | |||
14055 | } else if (ISD::isNormalLoad(V.getNode()) && | |||
14056 | cast<LoadSDNode>(V)->isSimple()) { | |||
14057 | // We do not check for one-use of the vector load because a broadcast load | |||
14058 | // is expected to be a win for code size, register pressure, and possibly | |||
14059 | // uops even if the original vector load is not eliminated. | |||
14060 | ||||
14061 | // Reduce the vector load and shuffle to a broadcasted scalar load. | |||
14062 | LoadSDNode *Ld = cast<LoadSDNode>(V); | |||
14063 | SDValue BaseAddr = Ld->getOperand(1); | |||
14064 | MVT SVT = VT.getScalarType(); | |||
14065 | unsigned Offset = BroadcastIdx * SVT.getStoreSize(); | |||
14066 | assert((int)(Offset * 8) == BitOffset && "Unexpected bit-offset")(static_cast <bool> ((int)(Offset * 8) == BitOffset && "Unexpected bit-offset") ? void (0) : __assert_fail ("(int)(Offset * 8) == BitOffset && \"Unexpected bit-offset\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14066, __extension__ __PRETTY_FUNCTION__)); | |||
14067 | SDValue NewAddr = | |||
14068 | DAG.getMemBasePlusOffset(BaseAddr, TypeSize::Fixed(Offset), DL); | |||
14069 | ||||
14070 | // Directly form VBROADCAST_LOAD if we're using VBROADCAST opcode rather | |||
14071 | // than MOVDDUP. | |||
14072 | // FIXME: Should we add VBROADCAST_LOAD isel patterns for pre-AVX? | |||
14073 | if (Opcode == X86ISD::VBROADCAST) { | |||
14074 | SDVTList Tys = DAG.getVTList(VT, MVT::Other); | |||
14075 | SDValue Ops[] = {Ld->getChain(), NewAddr}; | |||
14076 | V = DAG.getMemIntrinsicNode( | |||
14077 | X86ISD::VBROADCAST_LOAD, DL, Tys, Ops, SVT, | |||
14078 | DAG.getMachineFunction().getMachineMemOperand( | |||
14079 | Ld->getMemOperand(), Offset, SVT.getStoreSize())); | |||
14080 | DAG.makeEquivalentMemoryOrdering(Ld, V); | |||
14081 | return DAG.getBitcast(VT, V); | |||
14082 | } | |||
14083 | assert(SVT == MVT::f64 && "Unexpected VT!")(static_cast <bool> (SVT == MVT::f64 && "Unexpected VT!" ) ? void (0) : __assert_fail ("SVT == MVT::f64 && \"Unexpected VT!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14083, __extension__ __PRETTY_FUNCTION__)); | |||
14084 | V = DAG.getLoad(SVT, DL, Ld->getChain(), NewAddr, | |||
14085 | DAG.getMachineFunction().getMachineMemOperand( | |||
14086 | Ld->getMemOperand(), Offset, SVT.getStoreSize())); | |||
14087 | DAG.makeEquivalentMemoryOrdering(Ld, V); | |||
14088 | } else if (!BroadcastFromReg) { | |||
14089 | // We can't broadcast from a vector register. | |||
14090 | return SDValue(); | |||
14091 | } else if (BitOffset != 0) { | |||
14092 | // We can only broadcast from the zero-element of a vector register, | |||
14093 | // but it can be advantageous to broadcast from the zero-element of a | |||
14094 | // subvector. | |||
14095 | if (!VT.is256BitVector() && !VT.is512BitVector()) | |||
14096 | return SDValue(); | |||
14097 | ||||
14098 | // VPERMQ/VPERMPD can perform the cross-lane shuffle directly. | |||
14099 | if (VT == MVT::v4f64 || VT == MVT::v4i64) | |||
14100 | return SDValue(); | |||
14101 | ||||
14102 | // Only broadcast the zero-element of a 128-bit subvector. | |||
14103 | if ((BitOffset % 128) != 0) | |||
14104 | return SDValue(); | |||
14105 | ||||
14106 | assert((BitOffset % V.getScalarValueSizeInBits()) == 0 &&(static_cast <bool> ((BitOffset % V.getScalarValueSizeInBits ()) == 0 && "Unexpected bit-offset") ? void (0) : __assert_fail ("(BitOffset % V.getScalarValueSizeInBits()) == 0 && \"Unexpected bit-offset\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14107, __extension__ __PRETTY_FUNCTION__)) | |||
14107 | "Unexpected bit-offset")(static_cast <bool> ((BitOffset % V.getScalarValueSizeInBits ()) == 0 && "Unexpected bit-offset") ? void (0) : __assert_fail ("(BitOffset % V.getScalarValueSizeInBits()) == 0 && \"Unexpected bit-offset\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14107, __extension__ __PRETTY_FUNCTION__)); | |||
14108 | assert((V.getValueSizeInBits() == 256 || V.getValueSizeInBits() == 512) &&(static_cast <bool> ((V.getValueSizeInBits() == 256 || V .getValueSizeInBits() == 512) && "Unexpected vector size" ) ? void (0) : __assert_fail ("(V.getValueSizeInBits() == 256 || V.getValueSizeInBits() == 512) && \"Unexpected vector size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14109, __extension__ __PRETTY_FUNCTION__)) | |||
14109 | "Unexpected vector size")(static_cast <bool> ((V.getValueSizeInBits() == 256 || V .getValueSizeInBits() == 512) && "Unexpected vector size" ) ? void (0) : __assert_fail ("(V.getValueSizeInBits() == 256 || V.getValueSizeInBits() == 512) && \"Unexpected vector size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14109, __extension__ __PRETTY_FUNCTION__)); | |||
14110 | unsigned ExtractIdx = BitOffset / V.getScalarValueSizeInBits(); | |||
14111 | V = extract128BitVector(V, ExtractIdx, DAG, DL); | |||
14112 | } | |||
14113 | ||||
14114 | // On AVX we can use VBROADCAST directly for scalar sources. | |||
14115 | if (Opcode == X86ISD::MOVDDUP && !V.getValueType().isVector()) { | |||
14116 | V = DAG.getBitcast(MVT::f64, V); | |||
14117 | if (Subtarget.hasAVX()) { | |||
14118 | V = DAG.getNode(X86ISD::VBROADCAST, DL, MVT::v2f64, V); | |||
14119 | return DAG.getBitcast(VT, V); | |||
14120 | } | |||
14121 | V = DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, MVT::v2f64, V); | |||
14122 | } | |||
14123 | ||||
14124 | // If this is a scalar, do the broadcast on this type and bitcast. | |||
14125 | if (!V.getValueType().isVector()) { | |||
14126 | assert(V.getScalarValueSizeInBits() == NumEltBits &&(static_cast <bool> (V.getScalarValueSizeInBits() == NumEltBits && "Unexpected scalar size") ? void (0) : __assert_fail ("V.getScalarValueSizeInBits() == NumEltBits && \"Unexpected scalar size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14127, __extension__ __PRETTY_FUNCTION__)) | |||
14127 | "Unexpected scalar size")(static_cast <bool> (V.getScalarValueSizeInBits() == NumEltBits && "Unexpected scalar size") ? void (0) : __assert_fail ("V.getScalarValueSizeInBits() == NumEltBits && \"Unexpected scalar size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14127, __extension__ __PRETTY_FUNCTION__)); | |||
14128 | MVT BroadcastVT = MVT::getVectorVT(V.getSimpleValueType(), | |||
14129 | VT.getVectorNumElements()); | |||
14130 | return DAG.getBitcast(VT, DAG.getNode(Opcode, DL, BroadcastVT, V)); | |||
14131 | } | |||
14132 | ||||
14133 | // We only support broadcasting from 128-bit vectors to minimize the | |||
14134 | // number of patterns we need to deal with in isel. So extract down to | |||
14135 | // 128-bits, removing as many bitcasts as possible. | |||
14136 | if (V.getValueSizeInBits() > 128) | |||
14137 | V = extract128BitVector(peekThroughBitcasts(V), 0, DAG, DL); | |||
14138 | ||||
14139 | // Otherwise cast V to a vector with the same element type as VT, but | |||
14140 | // possibly narrower than VT. Then perform the broadcast. | |||
14141 | unsigned NumSrcElts = V.getValueSizeInBits() / NumEltBits; | |||
14142 | MVT CastVT = MVT::getVectorVT(VT.getVectorElementType(), NumSrcElts); | |||
14143 | return DAG.getNode(Opcode, DL, VT, DAG.getBitcast(CastVT, V)); | |||
14144 | } | |||
14145 | ||||
14146 | // Check for whether we can use INSERTPS to perform the shuffle. We only use | |||
14147 | // INSERTPS when the V1 elements are already in the correct locations | |||
14148 | // because otherwise we can just always use two SHUFPS instructions which | |||
14149 | // are much smaller to encode than a SHUFPS and an INSERTPS. We can also | |||
14150 | // perform INSERTPS if a single V1 element is out of place and all V2 | |||
14151 | // elements are zeroable. | |||
14152 | static bool matchShuffleAsInsertPS(SDValue &V1, SDValue &V2, | |||
14153 | unsigned &InsertPSMask, | |||
14154 | const APInt &Zeroable, | |||
14155 | ArrayRef<int> Mask, SelectionDAG &DAG) { | |||
14156 | assert(V1.getSimpleValueType().is128BitVector() && "Bad operand type!")(static_cast <bool> (V1.getSimpleValueType().is128BitVector () && "Bad operand type!") ? void (0) : __assert_fail ("V1.getSimpleValueType().is128BitVector() && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14156, __extension__ __PRETTY_FUNCTION__)); | |||
14157 | assert(V2.getSimpleValueType().is128BitVector() && "Bad operand type!")(static_cast <bool> (V2.getSimpleValueType().is128BitVector () && "Bad operand type!") ? void (0) : __assert_fail ("V2.getSimpleValueType().is128BitVector() && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14157, __extension__ __PRETTY_FUNCTION__)); | |||
14158 | assert(Mask.size() == 4 && "Unexpected mask size for v4 shuffle!")(static_cast <bool> (Mask.size() == 4 && "Unexpected mask size for v4 shuffle!" ) ? void (0) : __assert_fail ("Mask.size() == 4 && \"Unexpected mask size for v4 shuffle!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14158, __extension__ __PRETTY_FUNCTION__)); | |||
14159 | ||||
14160 | // Attempt to match INSERTPS with one element from VA or VB being | |||
14161 | // inserted into VA (or undef). If successful, V1, V2 and InsertPSMask | |||
14162 | // are updated. | |||
14163 | auto matchAsInsertPS = [&](SDValue VA, SDValue VB, | |||
14164 | ArrayRef<int> CandidateMask) { | |||
14165 | unsigned ZMask = 0; | |||
14166 | int VADstIndex = -1; | |||
14167 | int VBDstIndex = -1; | |||
14168 | bool VAUsedInPlace = false; | |||
14169 | ||||
14170 | for (int i = 0; i < 4; ++i) { | |||
14171 | // Synthesize a zero mask from the zeroable elements (includes undefs). | |||
14172 | if (Zeroable[i]) { | |||
14173 | ZMask |= 1 << i; | |||
14174 | continue; | |||
14175 | } | |||
14176 | ||||
14177 | // Flag if we use any VA inputs in place. | |||
14178 | if (i == CandidateMask[i]) { | |||
14179 | VAUsedInPlace = true; | |||
14180 | continue; | |||
14181 | } | |||
14182 | ||||
14183 | // We can only insert a single non-zeroable element. | |||
14184 | if (VADstIndex >= 0 || VBDstIndex >= 0) | |||
14185 | return false; | |||
14186 | ||||
14187 | if (CandidateMask[i] < 4) { | |||
14188 | // VA input out of place for insertion. | |||
14189 | VADstIndex = i; | |||
14190 | } else { | |||
14191 | // VB input for insertion. | |||
14192 | VBDstIndex = i; | |||
14193 | } | |||
14194 | } | |||
14195 | ||||
14196 | // Don't bother if we have no (non-zeroable) element for insertion. | |||
14197 | if (VADstIndex < 0 && VBDstIndex < 0) | |||
14198 | return false; | |||
14199 | ||||
14200 | // Determine element insertion src/dst indices. The src index is from the | |||
14201 | // start of the inserted vector, not the start of the concatenated vector. | |||
14202 | unsigned VBSrcIndex = 0; | |||
14203 | if (VADstIndex >= 0) { | |||
14204 | // If we have a VA input out of place, we use VA as the V2 element | |||
14205 | // insertion and don't use the original V2 at all. | |||
14206 | VBSrcIndex = CandidateMask[VADstIndex]; | |||
14207 | VBDstIndex = VADstIndex; | |||
14208 | VB = VA; | |||
14209 | } else { | |||
14210 | VBSrcIndex = CandidateMask[VBDstIndex] - 4; | |||
14211 | } | |||
14212 | ||||
14213 | // If no V1 inputs are used in place, then the result is created only from | |||
14214 | // the zero mask and the V2 insertion - so remove V1 dependency. | |||
14215 | if (!VAUsedInPlace) | |||
14216 | VA = DAG.getUNDEF(MVT::v4f32); | |||
14217 | ||||
14218 | // Update V1, V2 and InsertPSMask accordingly. | |||
14219 | V1 = VA; | |||
14220 | V2 = VB; | |||
14221 | ||||
14222 | // Insert the V2 element into the desired position. | |||
14223 | InsertPSMask = VBSrcIndex << 6 | VBDstIndex << 4 | ZMask; | |||
14224 | assert((InsertPSMask & ~0xFFu) == 0 && "Invalid mask!")(static_cast <bool> ((InsertPSMask & ~0xFFu) == 0 && "Invalid mask!") ? void (0) : __assert_fail ("(InsertPSMask & ~0xFFu) == 0 && \"Invalid mask!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14224, __extension__ __PRETTY_FUNCTION__)); | |||
14225 | return true; | |||
14226 | }; | |||
14227 | ||||
14228 | if (matchAsInsertPS(V1, V2, Mask)) | |||
14229 | return true; | |||
14230 | ||||
14231 | // Commute and try again. | |||
14232 | SmallVector<int, 4> CommutedMask(Mask.begin(), Mask.end()); | |||
14233 | ShuffleVectorSDNode::commuteMask(CommutedMask); | |||
14234 | if (matchAsInsertPS(V2, V1, CommutedMask)) | |||
14235 | return true; | |||
14236 | ||||
14237 | return false; | |||
14238 | } | |||
14239 | ||||
14240 | static SDValue lowerShuffleAsInsertPS(const SDLoc &DL, SDValue V1, SDValue V2, | |||
14241 | ArrayRef<int> Mask, const APInt &Zeroable, | |||
14242 | SelectionDAG &DAG) { | |||
14243 | assert(V1.getSimpleValueType() == MVT::v4f32 && "Bad operand type!")(static_cast <bool> (V1.getSimpleValueType() == MVT::v4f32 && "Bad operand type!") ? void (0) : __assert_fail ( "V1.getSimpleValueType() == MVT::v4f32 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14243, __extension__ __PRETTY_FUNCTION__)); | |||
14244 | assert(V2.getSimpleValueType() == MVT::v4f32 && "Bad operand type!")(static_cast <bool> (V2.getSimpleValueType() == MVT::v4f32 && "Bad operand type!") ? void (0) : __assert_fail ( "V2.getSimpleValueType() == MVT::v4f32 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14244, __extension__ __PRETTY_FUNCTION__)); | |||
14245 | ||||
14246 | // Attempt to match the insertps pattern. | |||
14247 | unsigned InsertPSMask = 0; | |||
14248 | if (!matchShuffleAsInsertPS(V1, V2, InsertPSMask, Zeroable, Mask, DAG)) | |||
14249 | return SDValue(); | |||
14250 | ||||
14251 | // Insert the V2 element into the desired position. | |||
14252 | return DAG.getNode(X86ISD::INSERTPS, DL, MVT::v4f32, V1, V2, | |||
14253 | DAG.getTargetConstant(InsertPSMask, DL, MVT::i8)); | |||
14254 | } | |||
14255 | ||||
14256 | /// Try to lower a shuffle as a permute of the inputs followed by an | |||
14257 | /// UNPCK instruction. | |||
14258 | /// | |||
14259 | /// This specifically targets cases where we end up with alternating between | |||
14260 | /// the two inputs, and so can permute them into something that feeds a single | |||
14261 | /// UNPCK instruction. Note that this routine only targets integer vectors | |||
14262 | /// because for floating point vectors we have a generalized SHUFPS lowering | |||
14263 | /// strategy that handles everything that doesn't *exactly* match an unpack, | |||
14264 | /// making this clever lowering unnecessary. | |||
14265 | static SDValue lowerShuffleAsPermuteAndUnpack( | |||
14266 | const SDLoc &DL, MVT VT, SDValue V1, SDValue V2, ArrayRef<int> Mask, | |||
14267 | const X86Subtarget &Subtarget, SelectionDAG &DAG) { | |||
14268 | assert(!VT.isFloatingPoint() &&(static_cast <bool> (!VT.isFloatingPoint() && "This routine only supports integer vectors." ) ? void (0) : __assert_fail ("!VT.isFloatingPoint() && \"This routine only supports integer vectors.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14269, __extension__ __PRETTY_FUNCTION__)) | |||
14269 | "This routine only supports integer vectors.")(static_cast <bool> (!VT.isFloatingPoint() && "This routine only supports integer vectors." ) ? void (0) : __assert_fail ("!VT.isFloatingPoint() && \"This routine only supports integer vectors.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14269, __extension__ __PRETTY_FUNCTION__)); | |||
14270 | assert(VT.is128BitVector() &&(static_cast <bool> (VT.is128BitVector() && "This routine only works on 128-bit vectors." ) ? void (0) : __assert_fail ("VT.is128BitVector() && \"This routine only works on 128-bit vectors.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14271, __extension__ __PRETTY_FUNCTION__)) | |||
14271 | "This routine only works on 128-bit vectors.")(static_cast <bool> (VT.is128BitVector() && "This routine only works on 128-bit vectors." ) ? void (0) : __assert_fail ("VT.is128BitVector() && \"This routine only works on 128-bit vectors.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14271, __extension__ __PRETTY_FUNCTION__)); | |||
14272 | assert(!V2.isUndef() &&(static_cast <bool> (!V2.isUndef() && "This routine should only be used when blending two inputs." ) ? void (0) : __assert_fail ("!V2.isUndef() && \"This routine should only be used when blending two inputs.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14273, __extension__ __PRETTY_FUNCTION__)) | |||
14273 | "This routine should only be used when blending two inputs.")(static_cast <bool> (!V2.isUndef() && "This routine should only be used when blending two inputs." ) ? void (0) : __assert_fail ("!V2.isUndef() && \"This routine should only be used when blending two inputs.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14273, __extension__ __PRETTY_FUNCTION__)); | |||
14274 | assert(Mask.size() >= 2 && "Single element masks are invalid.")(static_cast <bool> (Mask.size() >= 2 && "Single element masks are invalid." ) ? void (0) : __assert_fail ("Mask.size() >= 2 && \"Single element masks are invalid.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14274, __extension__ __PRETTY_FUNCTION__)); | |||
14275 | ||||
14276 | int Size = Mask.size(); | |||
14277 | ||||
14278 | int NumLoInputs = | |||
14279 | count_if(Mask, [Size](int M) { return M >= 0 && M % Size < Size / 2; }); | |||
14280 | int NumHiInputs = | |||
14281 | count_if(Mask, [Size](int M) { return M % Size >= Size / 2; }); | |||
14282 | ||||
14283 | bool UnpackLo = NumLoInputs >= NumHiInputs; | |||
14284 | ||||
14285 | auto TryUnpack = [&](int ScalarSize, int Scale) { | |||
14286 | SmallVector<int, 16> V1Mask((unsigned)Size, -1); | |||
14287 | SmallVector<int, 16> V2Mask((unsigned)Size, -1); | |||
14288 | ||||
14289 | for (int i = 0; i < Size; ++i) { | |||
14290 | if (Mask[i] < 0) | |||
14291 | continue; | |||
14292 | ||||
14293 | // Each element of the unpack contains Scale elements from this mask. | |||
14294 | int UnpackIdx = i / Scale; | |||
14295 | ||||
14296 | // We only handle the case where V1 feeds the first slots of the unpack. | |||
14297 | // We rely on canonicalization to ensure this is the case. | |||
14298 | if ((UnpackIdx % 2 == 0) != (Mask[i] < Size)) | |||
14299 | return SDValue(); | |||
14300 | ||||
14301 | // Setup the mask for this input. The indexing is tricky as we have to | |||
14302 | // handle the unpack stride. | |||
14303 | SmallVectorImpl<int> &VMask = (UnpackIdx % 2 == 0) ? V1Mask : V2Mask; | |||
14304 | VMask[(UnpackIdx / 2) * Scale + i % Scale + (UnpackLo ? 0 : Size / 2)] = | |||
14305 | Mask[i] % Size; | |||
14306 | } | |||
14307 | ||||
14308 | // If we will have to shuffle both inputs to use the unpack, check whether | |||
14309 | // we can just unpack first and shuffle the result. If so, skip this unpack. | |||
14310 | if ((NumLoInputs == 0 || NumHiInputs == 0) && !isNoopShuffleMask(V1Mask) && | |||
14311 | !isNoopShuffleMask(V2Mask)) | |||
14312 | return SDValue(); | |||
14313 | ||||
14314 | // Shuffle the inputs into place. | |||
14315 | V1 = DAG.getVectorShuffle(VT, DL, V1, DAG.getUNDEF(VT), V1Mask); | |||
14316 | V2 = DAG.getVectorShuffle(VT, DL, V2, DAG.getUNDEF(VT), V2Mask); | |||
14317 | ||||
14318 | // Cast the inputs to the type we will use to unpack them. | |||
14319 | MVT UnpackVT = MVT::getVectorVT(MVT::getIntegerVT(ScalarSize), Size / Scale); | |||
14320 | V1 = DAG.getBitcast(UnpackVT, V1); | |||
14321 | V2 = DAG.getBitcast(UnpackVT, V2); | |||
14322 | ||||
14323 | // Unpack the inputs and cast the result back to the desired type. | |||
14324 | return DAG.getBitcast( | |||
14325 | VT, DAG.getNode(UnpackLo ? X86ISD::UNPCKL : X86ISD::UNPCKH, DL, | |||
14326 | UnpackVT, V1, V2)); | |||
14327 | }; | |||
14328 | ||||
14329 | // We try each unpack from the largest to the smallest to try and find one | |||
14330 | // that fits this mask. | |||
14331 | int OrigScalarSize = VT.getScalarSizeInBits(); | |||
14332 | for (int ScalarSize = 64; ScalarSize >= OrigScalarSize; ScalarSize /= 2) | |||
14333 | if (SDValue Unpack = TryUnpack(ScalarSize, ScalarSize / OrigScalarSize)) | |||
14334 | return Unpack; | |||
14335 | ||||
14336 | // If we're shuffling with a zero vector then we're better off not doing | |||
14337 | // VECTOR_SHUFFLE(UNPCK()) as we lose track of those zero elements. | |||
14338 | if (ISD::isBuildVectorAllZeros(V1.getNode()) || | |||
14339 | ISD::isBuildVectorAllZeros(V2.getNode())) | |||
14340 | return SDValue(); | |||
14341 | ||||
14342 | // If none of the unpack-rooted lowerings worked (or were profitable) try an | |||
14343 | // initial unpack. | |||
14344 | if (NumLoInputs == 0 || NumHiInputs == 0) { | |||
14345 | assert((NumLoInputs > 0 || NumHiInputs > 0) &&(static_cast <bool> ((NumLoInputs > 0 || NumHiInputs > 0) && "We have to have *some* inputs!") ? void ( 0) : __assert_fail ("(NumLoInputs > 0 || NumHiInputs > 0) && \"We have to have *some* inputs!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14346, __extension__ __PRETTY_FUNCTION__)) | |||
14346 | "We have to have *some* inputs!")(static_cast <bool> ((NumLoInputs > 0 || NumHiInputs > 0) && "We have to have *some* inputs!") ? void ( 0) : __assert_fail ("(NumLoInputs > 0 || NumHiInputs > 0) && \"We have to have *some* inputs!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14346, __extension__ __PRETTY_FUNCTION__)); | |||
14347 | int HalfOffset = NumLoInputs == 0 ? Size / 2 : 0; | |||
14348 | ||||
14349 | // FIXME: We could consider the total complexity of the permute of each | |||
14350 | // possible unpacking. Or at the least we should consider how many | |||
14351 | // half-crossings are created. | |||
14352 | // FIXME: We could consider commuting the unpacks. | |||
14353 | ||||
14354 | SmallVector<int, 32> PermMask((unsigned)Size, -1); | |||
14355 | for (int i = 0; i < Size; ++i) { | |||
14356 | if (Mask[i] < 0) | |||
14357 | continue; | |||
14358 | ||||
14359 | assert(Mask[i] % Size >= HalfOffset && "Found input from wrong half!")(static_cast <bool> (Mask[i] % Size >= HalfOffset && "Found input from wrong half!") ? void (0) : __assert_fail ( "Mask[i] % Size >= HalfOffset && \"Found input from wrong half!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14359, __extension__ __PRETTY_FUNCTION__)); | |||
14360 | ||||
14361 | PermMask[i] = | |||
14362 | 2 * ((Mask[i] % Size) - HalfOffset) + (Mask[i] < Size ? 0 : 1); | |||
14363 | } | |||
14364 | return DAG.getVectorShuffle( | |||
14365 | VT, DL, DAG.getNode(NumLoInputs == 0 ? X86ISD::UNPCKH : X86ISD::UNPCKL, | |||
14366 | DL, VT, V1, V2), | |||
14367 | DAG.getUNDEF(VT), PermMask); | |||
14368 | } | |||
14369 | ||||
14370 | return SDValue(); | |||
14371 | } | |||
14372 | ||||
14373 | /// Handle lowering of 2-lane 64-bit floating point shuffles. | |||
14374 | /// | |||
14375 | /// This is the basis function for the 2-lane 64-bit shuffles as we have full | |||
14376 | /// support for floating point shuffles but not integer shuffles. These | |||
14377 | /// instructions will incur a domain crossing penalty on some chips though so | |||
14378 | /// it is better to avoid lowering through this for integer vectors where | |||
14379 | /// possible. | |||
14380 | static SDValue lowerV2F64Shuffle(const SDLoc &DL, ArrayRef<int> Mask, | |||
14381 | const APInt &Zeroable, SDValue V1, SDValue V2, | |||
14382 | const X86Subtarget &Subtarget, | |||
14383 | SelectionDAG &DAG) { | |||
14384 | assert(V1.getSimpleValueType() == MVT::v2f64 && "Bad operand type!")(static_cast <bool> (V1.getSimpleValueType() == MVT::v2f64 && "Bad operand type!") ? void (0) : __assert_fail ( "V1.getSimpleValueType() == MVT::v2f64 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14384, __extension__ __PRETTY_FUNCTION__)); | |||
14385 | assert(V2.getSimpleValueType() == MVT::v2f64 && "Bad operand type!")(static_cast <bool> (V2.getSimpleValueType() == MVT::v2f64 && "Bad operand type!") ? void (0) : __assert_fail ( "V2.getSimpleValueType() == MVT::v2f64 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14385, __extension__ __PRETTY_FUNCTION__)); | |||
14386 | assert(Mask.size() == 2 && "Unexpected mask size for v2 shuffle!")(static_cast <bool> (Mask.size() == 2 && "Unexpected mask size for v2 shuffle!" ) ? void (0) : __assert_fail ("Mask.size() == 2 && \"Unexpected mask size for v2 shuffle!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14386, __extension__ __PRETTY_FUNCTION__)); | |||
14387 | ||||
14388 | if (V2.isUndef()) { | |||
14389 | // Check for being able to broadcast a single element. | |||
14390 | if (SDValue Broadcast = lowerShuffleAsBroadcast(DL, MVT::v2f64, V1, V2, | |||
14391 | Mask, Subtarget, DAG)) | |||
14392 | return Broadcast; | |||
14393 | ||||
14394 | // Straight shuffle of a single input vector. Simulate this by using the | |||
14395 | // single input as both of the "inputs" to this instruction.. | |||
14396 | unsigned SHUFPDMask = (Mask[0] == 1) | ((Mask[1] == 1) << 1); | |||
14397 | ||||
14398 | if (Subtarget.hasAVX()) { | |||
14399 | // If we have AVX, we can use VPERMILPS which will allow folding a load | |||
14400 | // into the shuffle. | |||
14401 | return DAG.getNode(X86ISD::VPERMILPI, DL, MVT::v2f64, V1, | |||
14402 | DAG.getTargetConstant(SHUFPDMask, DL, MVT::i8)); | |||
14403 | } | |||
14404 | ||||
14405 | return DAG.getNode( | |||
14406 | X86ISD::SHUFP, DL, MVT::v2f64, | |||
14407 | Mask[0] == SM_SentinelUndef ? DAG.getUNDEF(MVT::v2f64) : V1, | |||
14408 | Mask[1] == SM_SentinelUndef ? DAG.getUNDEF(MVT::v2f64) : V1, | |||
14409 | DAG.getTargetConstant(SHUFPDMask, DL, MVT::i8)); | |||
14410 | } | |||
14411 | assert(Mask[0] >= 0 && "No undef lanes in multi-input v2 shuffles!")(static_cast <bool> (Mask[0] >= 0 && "No undef lanes in multi-input v2 shuffles!" ) ? void (0) : __assert_fail ("Mask[0] >= 0 && \"No undef lanes in multi-input v2 shuffles!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14411, __extension__ __PRETTY_FUNCTION__)); | |||
14412 | assert(Mask[1] >= 0 && "No undef lanes in multi-input v2 shuffles!")(static_cast <bool> (Mask[1] >= 0 && "No undef lanes in multi-input v2 shuffles!" ) ? void (0) : __assert_fail ("Mask[1] >= 0 && \"No undef lanes in multi-input v2 shuffles!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14412, __extension__ __PRETTY_FUNCTION__)); | |||
14413 | assert(Mask[0] < 2 && "We sort V1 to be the first input.")(static_cast <bool> (Mask[0] < 2 && "We sort V1 to be the first input." ) ? void (0) : __assert_fail ("Mask[0] < 2 && \"We sort V1 to be the first input.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14413, __extension__ __PRETTY_FUNCTION__)); | |||
14414 | assert(Mask[1] >= 2 && "We sort V2 to be the second input.")(static_cast <bool> (Mask[1] >= 2 && "We sort V2 to be the second input." ) ? void (0) : __assert_fail ("Mask[1] >= 2 && \"We sort V2 to be the second input.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14414, __extension__ __PRETTY_FUNCTION__)); | |||
14415 | ||||
14416 | if (Subtarget.hasAVX2()) | |||
14417 | if (SDValue Extract = lowerShuffleOfExtractsAsVperm(DL, V1, V2, Mask, DAG)) | |||
14418 | return Extract; | |||
14419 | ||||
14420 | // When loading a scalar and then shuffling it into a vector we can often do | |||
14421 | // the insertion cheaply. | |||
14422 | if (SDValue Insertion = lowerShuffleAsElementInsertion( | |||
14423 | DL, MVT::v2f64, V1, V2, Mask, Zeroable, Subtarget, DAG)) | |||
14424 | return Insertion; | |||
14425 | // Try inverting the insertion since for v2 masks it is easy to do and we | |||
14426 | // can't reliably sort the mask one way or the other. | |||
14427 | int InverseMask[2] = {Mask[0] < 0 ? -1 : (Mask[0] ^ 2), | |||
14428 | Mask[1] < 0 ? -1 : (Mask[1] ^ 2)}; | |||
14429 | if (SDValue Insertion = lowerShuffleAsElementInsertion( | |||
14430 | DL, MVT::v2f64, V2, V1, InverseMask, Zeroable, Subtarget, DAG)) | |||
14431 | return Insertion; | |||
14432 | ||||
14433 | // Try to use one of the special instruction patterns to handle two common | |||
14434 | // blend patterns if a zero-blend above didn't work. | |||
14435 | if (isShuffleEquivalent(Mask, {0, 3}, V1, V2) || | |||
14436 | isShuffleEquivalent(Mask, {1, 3}, V1, V2)) | |||
14437 | if (SDValue V1S = getScalarValueForVectorElement(V1, Mask[0], DAG)) | |||
14438 | // We can either use a special instruction to load over the low double or | |||
14439 | // to move just the low double. | |||
14440 | return DAG.getNode( | |||
14441 | X86ISD::MOVSD, DL, MVT::v2f64, V2, | |||
14442 | DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, MVT::v2f64, V1S)); | |||
14443 | ||||
14444 | if (Subtarget.hasSSE41()) | |||
14445 | if (SDValue Blend = lowerShuffleAsBlend(DL, MVT::v2f64, V1, V2, Mask, | |||
14446 | Zeroable, Subtarget, DAG)) | |||
14447 | return Blend; | |||
14448 | ||||
14449 | // Use dedicated unpack instructions for masks that match their pattern. | |||
14450 | if (SDValue V = lowerShuffleWithUNPCK(DL, MVT::v2f64, Mask, V1, V2, DAG)) | |||
14451 | return V; | |||
14452 | ||||
14453 | unsigned SHUFPDMask = (Mask[0] == 1) | (((Mask[1] - 2) == 1) << 1); | |||
14454 | return DAG.getNode(X86ISD::SHUFP, DL, MVT::v2f64, V1, V2, | |||
14455 | DAG.getTargetConstant(SHUFPDMask, DL, MVT::i8)); | |||
14456 | } | |||
14457 | ||||
14458 | /// Handle lowering of 2-lane 64-bit integer shuffles. | |||
14459 | /// | |||
14460 | /// Tries to lower a 2-lane 64-bit shuffle using shuffle operations provided by | |||
14461 | /// the integer unit to minimize domain crossing penalties. However, for blends | |||
14462 | /// it falls back to the floating point shuffle operation with appropriate bit | |||
14463 | /// casting. | |||
14464 | static SDValue lowerV2I64Shuffle(const SDLoc &DL, ArrayRef<int> Mask, | |||
14465 | const APInt &Zeroable, SDValue V1, SDValue V2, | |||
14466 | const X86Subtarget &Subtarget, | |||
14467 | SelectionDAG &DAG) { | |||
14468 | assert(V1.getSimpleValueType() == MVT::v2i64 && "Bad operand type!")(static_cast <bool> (V1.getSimpleValueType() == MVT::v2i64 && "Bad operand type!") ? void (0) : __assert_fail ( "V1.getSimpleValueType() == MVT::v2i64 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14468, __extension__ __PRETTY_FUNCTION__)); | |||
14469 | assert(V2.getSimpleValueType() == MVT::v2i64 && "Bad operand type!")(static_cast <bool> (V2.getSimpleValueType() == MVT::v2i64 && "Bad operand type!") ? void (0) : __assert_fail ( "V2.getSimpleValueType() == MVT::v2i64 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14469, __extension__ __PRETTY_FUNCTION__)); | |||
14470 | assert(Mask.size() == 2 && "Unexpected mask size for v2 shuffle!")(static_cast <bool> (Mask.size() == 2 && "Unexpected mask size for v2 shuffle!" ) ? void (0) : __assert_fail ("Mask.size() == 2 && \"Unexpected mask size for v2 shuffle!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14470, __extension__ __PRETTY_FUNCTION__)); | |||
14471 | ||||
14472 | if (V2.isUndef()) { | |||
14473 | // Check for being able to broadcast a single element. | |||
14474 | if (SDValue Broadcast = lowerShuffleAsBroadcast(DL, MVT::v2i64, V1, V2, | |||
14475 | Mask, Subtarget, DAG)) | |||
14476 | return Broadcast; | |||
14477 | ||||
14478 | // Straight shuffle of a single input vector. For everything from SSE2 | |||
14479 | // onward this has a single fast instruction with no scary immediates. | |||
14480 | // We have to map the mask as it is actually a v4i32 shuffle instruction. | |||
14481 | V1 = DAG.getBitcast(MVT::v4i32, V1); | |||
14482 | int WidenedMask[4] = {Mask[0] < 0 ? -1 : (Mask[0] * 2), | |||
14483 | Mask[0] < 0 ? -1 : ((Mask[0] * 2) + 1), | |||
14484 | Mask[1] < 0 ? -1 : (Mask[1] * 2), | |||
14485 | Mask[1] < 0 ? -1 : ((Mask[1] * 2) + 1)}; | |||
14486 | return DAG.getBitcast( | |||
14487 | MVT::v2i64, | |||
14488 | DAG.getNode(X86ISD::PSHUFD, DL, MVT::v4i32, V1, | |||
14489 | getV4X86ShuffleImm8ForMask(WidenedMask, DL, DAG))); | |||
14490 | } | |||
14491 | assert(Mask[0] != -1 && "No undef lanes in multi-input v2 shuffles!")(static_cast <bool> (Mask[0] != -1 && "No undef lanes in multi-input v2 shuffles!" ) ? void (0) : __assert_fail ("Mask[0] != -1 && \"No undef lanes in multi-input v2 shuffles!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14491, __extension__ __PRETTY_FUNCTION__)); | |||
14492 | assert(Mask[1] != -1 && "No undef lanes in multi-input v2 shuffles!")(static_cast <bool> (Mask[1] != -1 && "No undef lanes in multi-input v2 shuffles!" ) ? void (0) : __assert_fail ("Mask[1] != -1 && \"No undef lanes in multi-input v2 shuffles!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14492, __extension__ __PRETTY_FUNCTION__)); | |||
14493 | assert(Mask[0] < 2 && "We sort V1 to be the first input.")(static_cast <bool> (Mask[0] < 2 && "We sort V1 to be the first input." ) ? void (0) : __assert_fail ("Mask[0] < 2 && \"We sort V1 to be the first input.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14493, __extension__ __PRETTY_FUNCTION__)); | |||
14494 | assert(Mask[1] >= 2 && "We sort V2 to be the second input.")(static_cast <bool> (Mask[1] >= 2 && "We sort V2 to be the second input." ) ? void (0) : __assert_fail ("Mask[1] >= 2 && \"We sort V2 to be the second input.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14494, __extension__ __PRETTY_FUNCTION__)); | |||
14495 | ||||
14496 | if (Subtarget.hasAVX2()) | |||
14497 | if (SDValue Extract = lowerShuffleOfExtractsAsVperm(DL, V1, V2, Mask, DAG)) | |||
14498 | return Extract; | |||
14499 | ||||
14500 | // Try to use shift instructions. | |||
14501 | if (SDValue Shift = lowerShuffleAsShift(DL, MVT::v2i64, V1, V2, Mask, | |||
14502 | Zeroable, Subtarget, DAG)) | |||
14503 | return Shift; | |||
14504 | ||||
14505 | // When loading a scalar and then shuffling it into a vector we can often do | |||
14506 | // the insertion cheaply. | |||
14507 | if (SDValue Insertion = lowerShuffleAsElementInsertion( | |||
14508 | DL, MVT::v2i64, V1, V2, Mask, Zeroable, Subtarget, DAG)) | |||
14509 | return Insertion; | |||
14510 | // Try inverting the insertion since for v2 masks it is easy to do and we | |||
14511 | // can't reliably sort the mask one way or the other. | |||
14512 | int InverseMask[2] = {Mask[0] ^ 2, Mask[1] ^ 2}; | |||
14513 | if (SDValue Insertion = lowerShuffleAsElementInsertion( | |||
14514 | DL, MVT::v2i64, V2, V1, InverseMask, Zeroable, Subtarget, DAG)) | |||
14515 | return Insertion; | |||
14516 | ||||
14517 | // We have different paths for blend lowering, but they all must use the | |||
14518 | // *exact* same predicate. | |||
14519 | bool IsBlendSupported = Subtarget.hasSSE41(); | |||
14520 | if (IsBlendSupported) | |||
14521 | if (SDValue Blend = lowerShuffleAsBlend(DL, MVT::v2i64, V1, V2, Mask, | |||
14522 | Zeroable, Subtarget, DAG)) | |||
14523 | return Blend; | |||
14524 | ||||
14525 | // Use dedicated unpack instructions for masks that match their pattern. | |||
14526 | if (SDValue V = lowerShuffleWithUNPCK(DL, MVT::v2i64, Mask, V1, V2, DAG)) | |||
14527 | return V; | |||
14528 | ||||
14529 | // Try to use byte rotation instructions. | |||
14530 | // Its more profitable for pre-SSSE3 to use shuffles/unpacks. | |||
14531 | if (Subtarget.hasSSSE3()) { | |||
14532 | if (Subtarget.hasVLX()) | |||
14533 | if (SDValue Rotate = lowerShuffleAsVALIGN(DL, MVT::v2i64, V1, V2, Mask, | |||
14534 | Subtarget, DAG)) | |||
14535 | return Rotate; | |||
14536 | ||||
14537 | if (SDValue Rotate = lowerShuffleAsByteRotate(DL, MVT::v2i64, V1, V2, Mask, | |||
14538 | Subtarget, DAG)) | |||
14539 | return Rotate; | |||
14540 | } | |||
14541 | ||||
14542 | // If we have direct support for blends, we should lower by decomposing into | |||
14543 | // a permute. That will be faster than the domain cross. | |||
14544 | if (IsBlendSupported) | |||
14545 | return lowerShuffleAsDecomposedShuffleMerge(DL, MVT::v2i64, V1, V2, Mask, | |||
14546 | Subtarget, DAG); | |||
14547 | ||||
14548 | // We implement this with SHUFPD which is pretty lame because it will likely | |||
14549 | // incur 2 cycles of stall for integer vectors on Nehalem and older chips. | |||
14550 | // However, all the alternatives are still more cycles and newer chips don't | |||
14551 | // have this problem. It would be really nice if x86 had better shuffles here. | |||
14552 | V1 = DAG.getBitcast(MVT::v2f64, V1); | |||
14553 | V2 = DAG.getBitcast(MVT::v2f64, V2); | |||
14554 | return DAG.getBitcast(MVT::v2i64, | |||
14555 | DAG.getVectorShuffle(MVT::v2f64, DL, V1, V2, Mask)); | |||
14556 | } | |||
14557 | ||||
14558 | /// Lower a vector shuffle using the SHUFPS instruction. | |||
14559 | /// | |||
14560 | /// This is a helper routine dedicated to lowering vector shuffles using SHUFPS. | |||
14561 | /// It makes no assumptions about whether this is the *best* lowering, it simply | |||
14562 | /// uses it. | |||
14563 | static SDValue lowerShuffleWithSHUFPS(const SDLoc &DL, MVT VT, | |||
14564 | ArrayRef<int> Mask, SDValue V1, | |||
14565 | SDValue V2, SelectionDAG &DAG) { | |||
14566 | SDValue LowV = V1, HighV = V2; | |||
14567 | SmallVector<int, 4> NewMask(Mask.begin(), Mask.end()); | |||
14568 | int NumV2Elements = count_if(Mask, [](int M) { return M >= 4; }); | |||
14569 | ||||
14570 | if (NumV2Elements == 1) { | |||
14571 | int V2Index = find_if(Mask, [](int M) { return M >= 4; }) - Mask.begin(); | |||
14572 | ||||
14573 | // Compute the index adjacent to V2Index and in the same half by toggling | |||
14574 | // the low bit. | |||
14575 | int V2AdjIndex = V2Index ^ 1; | |||
14576 | ||||
14577 | if (Mask[V2AdjIndex] < 0) { | |||
14578 | // Handles all the cases where we have a single V2 element and an undef. | |||
14579 | // This will only ever happen in the high lanes because we commute the | |||
14580 | // vector otherwise. | |||
14581 | if (V2Index < 2) | |||
14582 | std::swap(LowV, HighV); | |||
14583 | NewMask[V2Index] -= 4; | |||
14584 | } else { | |||
14585 | // Handle the case where the V2 element ends up adjacent to a V1 element. | |||
14586 | // To make this work, blend them together as the first step. | |||
14587 | int V1Index = V2AdjIndex; | |||
14588 | int BlendMask[4] = {Mask[V2Index] - 4, 0, Mask[V1Index], 0}; | |||
14589 | V2 = DAG.getNode(X86ISD::SHUFP, DL, VT, V2, V1, | |||
14590 | getV4X86ShuffleImm8ForMask(BlendMask, DL, DAG)); | |||
14591 | ||||
14592 | // Now proceed to reconstruct the final blend as we have the necessary | |||
14593 | // high or low half formed. | |||
14594 | if (V2Index < 2) { | |||
14595 | LowV = V2; | |||
14596 | HighV = V1; | |||
14597 | } else { | |||
14598 | HighV = V2; | |||
14599 | } | |||
14600 | NewMask[V1Index] = 2; // We put the V1 element in V2[2]. | |||
14601 | NewMask[V2Index] = 0; // We shifted the V2 element into V2[0]. | |||
14602 | } | |||
14603 | } else if (NumV2Elements == 2) { | |||
14604 | if (Mask[0] < 4 && Mask[1] < 4) { | |||
14605 | // Handle the easy case where we have V1 in the low lanes and V2 in the | |||
14606 | // high lanes. | |||
14607 | NewMask[2] -= 4; | |||
14608 | NewMask[3] -= 4; | |||
14609 | } else if (Mask[2] < 4 && Mask[3] < 4) { | |||
14610 | // We also handle the reversed case because this utility may get called | |||
14611 | // when we detect a SHUFPS pattern but can't easily commute the shuffle to | |||
14612 | // arrange things in the right direction. | |||
14613 | NewMask[0] -= 4; | |||
14614 | NewMask[1] -= 4; | |||
14615 | HighV = V1; | |||
14616 | LowV = V2; | |||
14617 | } else { | |||
14618 | // We have a mixture of V1 and V2 in both low and high lanes. Rather than | |||
14619 | // trying to place elements directly, just blend them and set up the final | |||
14620 | // shuffle to place them. | |||
14621 | ||||
14622 | // The first two blend mask elements are for V1, the second two are for | |||
14623 | // V2. | |||
14624 | int BlendMask[4] = {Mask[0] < 4 ? Mask[0] : Mask[1], | |||
14625 | Mask[2] < 4 ? Mask[2] : Mask[3], | |||
14626 | (Mask[0] >= 4 ? Mask[0] : Mask[1]) - 4, | |||
14627 | (Mask[2] >= 4 ? Mask[2] : Mask[3]) - 4}; | |||
14628 | V1 = DAG.getNode(X86ISD::SHUFP, DL, VT, V1, V2, | |||
14629 | getV4X86ShuffleImm8ForMask(BlendMask, DL, DAG)); | |||
14630 | ||||
14631 | // Now we do a normal shuffle of V1 by giving V1 as both operands to | |||
14632 | // a blend. | |||
14633 | LowV = HighV = V1; | |||
14634 | NewMask[0] = Mask[0] < 4 ? 0 : 2; | |||
14635 | NewMask[1] = Mask[0] < 4 ? 2 : 0; | |||
14636 | NewMask[2] = Mask[2] < 4 ? 1 : 3; | |||
14637 | NewMask[3] = Mask[2] < 4 ? 3 : 1; | |||
14638 | } | |||
14639 | } else if (NumV2Elements == 3) { | |||
14640 | // Ideally canonicalizeShuffleMaskWithCommute should have caught this, but | |||
14641 | // we can get here due to other paths (e.g repeated mask matching) that we | |||
14642 | // don't want to do another round of lowerVECTOR_SHUFFLE. | |||
14643 | ShuffleVectorSDNode::commuteMask(NewMask); | |||
14644 | return lowerShuffleWithSHUFPS(DL, VT, NewMask, V2, V1, DAG); | |||
14645 | } | |||
14646 | return DAG.getNode(X86ISD::SHUFP, DL, VT, LowV, HighV, | |||
14647 | getV4X86ShuffleImm8ForMask(NewMask, DL, DAG)); | |||
14648 | } | |||
14649 | ||||
14650 | /// Lower 4-lane 32-bit floating point shuffles. | |||
14651 | /// | |||
14652 | /// Uses instructions exclusively from the floating point unit to minimize | |||
14653 | /// domain crossing penalties, as these are sufficient to implement all v4f32 | |||
14654 | /// shuffles. | |||
14655 | static SDValue lowerV4F32Shuffle(const SDLoc &DL, ArrayRef<int> Mask, | |||
14656 | const APInt &Zeroable, SDValue V1, SDValue V2, | |||
14657 | const X86Subtarget &Subtarget, | |||
14658 | SelectionDAG &DAG) { | |||
14659 | assert(V1.getSimpleValueType() == MVT::v4f32 && "Bad operand type!")(static_cast <bool> (V1.getSimpleValueType() == MVT::v4f32 && "Bad operand type!") ? void (0) : __assert_fail ( "V1.getSimpleValueType() == MVT::v4f32 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14659, __extension__ __PRETTY_FUNCTION__)); | |||
14660 | assert(V2.getSimpleValueType() == MVT::v4f32 && "Bad operand type!")(static_cast <bool> (V2.getSimpleValueType() == MVT::v4f32 && "Bad operand type!") ? void (0) : __assert_fail ( "V2.getSimpleValueType() == MVT::v4f32 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14660, __extension__ __PRETTY_FUNCTION__)); | |||
14661 | assert(Mask.size() == 4 && "Unexpected mask size for v4 shuffle!")(static_cast <bool> (Mask.size() == 4 && "Unexpected mask size for v4 shuffle!" ) ? void (0) : __assert_fail ("Mask.size() == 4 && \"Unexpected mask size for v4 shuffle!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14661, __extension__ __PRETTY_FUNCTION__)); | |||
14662 | ||||
14663 | int NumV2Elements = count_if(Mask, [](int M) { return M >= 4; }); | |||
14664 | ||||
14665 | if (NumV2Elements == 0) { | |||
14666 | // Check for being able to broadcast a single element. | |||
14667 | if (SDValue Broadcast = lowerShuffleAsBroadcast(DL, MVT::v4f32, V1, V2, | |||
14668 | Mask, Subtarget, DAG)) | |||
14669 | return Broadcast; | |||
14670 | ||||
14671 | // Use even/odd duplicate instructions for masks that match their pattern. | |||
14672 | if (Subtarget.hasSSE3()) { | |||
14673 | if (isShuffleEquivalent(Mask, {0, 0, 2, 2}, V1, V2)) | |||
14674 | return DAG.getNode(X86ISD::MOVSLDUP, DL, MVT::v4f32, V1); | |||
14675 | if (isShuffleEquivalent(Mask, {1, 1, 3, 3}, V1, V2)) | |||
14676 | return DAG.getNode(X86ISD::MOVSHDUP, DL, MVT::v4f32, V1); | |||
14677 | } | |||
14678 | ||||
14679 | if (Subtarget.hasAVX()) { | |||
14680 | // If we have AVX, we can use VPERMILPS which will allow folding a load | |||
14681 | // into the shuffle. | |||
14682 | return DAG.getNode(X86ISD::VPERMILPI, DL, MVT::v4f32, V1, | |||
14683 | getV4X86ShuffleImm8ForMask(Mask, DL, DAG)); | |||
14684 | } | |||
14685 | ||||
14686 | // Use MOVLHPS/MOVHLPS to simulate unary shuffles. These are only valid | |||
14687 | // in SSE1 because otherwise they are widened to v2f64 and never get here. | |||
14688 | if (!Subtarget.hasSSE2()) { | |||
14689 | if (isShuffleEquivalent(Mask, {0, 1, 0, 1}, V1, V2)) | |||
14690 | return DAG.getNode(X86ISD::MOVLHPS, DL, MVT::v4f32, V1, V1); | |||
14691 | if (isShuffleEquivalent(Mask, {2, 3, 2, 3}, V1, V2)) | |||
14692 | return DAG.getNode(X86ISD::MOVHLPS, DL, MVT::v4f32, V1, V1); | |||
14693 | } | |||
14694 | ||||
14695 | // Otherwise, use a straight shuffle of a single input vector. We pass the | |||
14696 | // input vector to both operands to simulate this with a SHUFPS. | |||
14697 | return DAG.getNode(X86ISD::SHUFP, DL, MVT::v4f32, V1, V1, | |||
14698 | getV4X86ShuffleImm8ForMask(Mask, DL, DAG)); | |||
14699 | } | |||
14700 | ||||
14701 | if (Subtarget.hasAVX2()) | |||
14702 | if (SDValue Extract = lowerShuffleOfExtractsAsVperm(DL, V1, V2, Mask, DAG)) | |||
14703 | return Extract; | |||
14704 | ||||
14705 | // There are special ways we can lower some single-element blends. However, we | |||
14706 | // have custom ways we can lower more complex single-element blends below that | |||
14707 | // we defer to if both this and BLENDPS fail to match, so restrict this to | |||
14708 | // when the V2 input is targeting element 0 of the mask -- that is the fast | |||
14709 | // case here. | |||
14710 | if (NumV2Elements == 1 && Mask[0] >= 4) | |||
14711 | if (SDValue V = lowerShuffleAsElementInsertion( | |||
14712 | DL, MVT::v4f32, V1, V2, Mask, Zeroable, Subtarget, DAG)) | |||
14713 | return V; | |||
14714 | ||||
14715 | if (Subtarget.hasSSE41()) { | |||
14716 | if (SDValue Blend = lowerShuffleAsBlend(DL, MVT::v4f32, V1, V2, Mask, | |||
14717 | Zeroable, Subtarget, DAG)) | |||
14718 | return Blend; | |||
14719 | ||||
14720 | // Use INSERTPS if we can complete the shuffle efficiently. | |||
14721 | if (SDValue V = lowerShuffleAsInsertPS(DL, V1, V2, Mask, Zeroable, DAG)) | |||
14722 | return V; | |||
14723 | ||||
14724 | if (!isSingleSHUFPSMask(Mask)) | |||
14725 | if (SDValue BlendPerm = lowerShuffleAsBlendAndPermute(DL, MVT::v4f32, V1, | |||
14726 | V2, Mask, DAG)) | |||
14727 | return BlendPerm; | |||
14728 | } | |||
14729 | ||||
14730 | // Use low/high mov instructions. These are only valid in SSE1 because | |||
14731 | // otherwise they are widened to v2f64 and never get here. | |||
14732 | if (!Subtarget.hasSSE2()) { | |||
14733 | if (isShuffleEquivalent(Mask, {0, 1, 4, 5}, V1, V2)) | |||
14734 | return DAG.getNode(X86ISD::MOVLHPS, DL, MVT::v4f32, V1, V2); | |||
14735 | if (isShuffleEquivalent(Mask, {2, 3, 6, 7}, V1, V2)) | |||
14736 | return DAG.getNode(X86ISD::MOVHLPS, DL, MVT::v4f32, V2, V1); | |||
14737 | } | |||
14738 | ||||
14739 | // Use dedicated unpack instructions for masks that match their pattern. | |||
14740 | if (SDValue V = lowerShuffleWithUNPCK(DL, MVT::v4f32, Mask, V1, V2, DAG)) | |||
14741 | return V; | |||
14742 | ||||
14743 | // Otherwise fall back to a SHUFPS lowering strategy. | |||
14744 | return lowerShuffleWithSHUFPS(DL, MVT::v4f32, Mask, V1, V2, DAG); | |||
14745 | } | |||
14746 | ||||
14747 | /// Lower 4-lane i32 vector shuffles. | |||
14748 | /// | |||
14749 | /// We try to handle these with integer-domain shuffles where we can, but for | |||
14750 | /// blends we use the floating point domain blend instructions. | |||
14751 | static SDValue lowerV4I32Shuffle(const SDLoc &DL, ArrayRef<int> Mask, | |||
14752 | const APInt &Zeroable, SDValue V1, SDValue V2, | |||
14753 | const X86Subtarget &Subtarget, | |||
14754 | SelectionDAG &DAG) { | |||
14755 | assert(V1.getSimpleValueType() == MVT::v4i32 && "Bad operand type!")(static_cast <bool> (V1.getSimpleValueType() == MVT::v4i32 && "Bad operand type!") ? void (0) : __assert_fail ( "V1.getSimpleValueType() == MVT::v4i32 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14755, __extension__ __PRETTY_FUNCTION__)); | |||
14756 | assert(V2.getSimpleValueType() == MVT::v4i32 && "Bad operand type!")(static_cast <bool> (V2.getSimpleValueType() == MVT::v4i32 && "Bad operand type!") ? void (0) : __assert_fail ( "V2.getSimpleValueType() == MVT::v4i32 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14756, __extension__ __PRETTY_FUNCTION__)); | |||
14757 | assert(Mask.size() == 4 && "Unexpected mask size for v4 shuffle!")(static_cast <bool> (Mask.size() == 4 && "Unexpected mask size for v4 shuffle!" ) ? void (0) : __assert_fail ("Mask.size() == 4 && \"Unexpected mask size for v4 shuffle!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14757, __extension__ __PRETTY_FUNCTION__)); | |||
14758 | ||||
14759 | // Whenever we can lower this as a zext, that instruction is strictly faster | |||
14760 | // than any alternative. It also allows us to fold memory operands into the | |||
14761 | // shuffle in many cases. | |||
14762 | if (SDValue ZExt = lowerShuffleAsZeroOrAnyExtend(DL, MVT::v4i32, V1, V2, Mask, | |||
14763 | Zeroable, Subtarget, DAG)) | |||
14764 | return ZExt; | |||
14765 | ||||
14766 | int NumV2Elements = count_if(Mask, [](int M) { return M >= 4; }); | |||
14767 | ||||
14768 | if (NumV2Elements == 0) { | |||
14769 | // Try to use broadcast unless the mask only has one non-undef element. | |||
14770 | if (count_if(Mask, [](int M) { return M >= 0 && M < 4; }) > 1) { | |||
14771 | if (SDValue Broadcast = lowerShuffleAsBroadcast(DL, MVT::v4i32, V1, V2, | |||
14772 | Mask, Subtarget, DAG)) | |||
14773 | return Broadcast; | |||
14774 | } | |||
14775 | ||||
14776 | // Straight shuffle of a single input vector. For everything from SSE2 | |||
14777 | // onward this has a single fast instruction with no scary immediates. | |||
14778 | // We coerce the shuffle pattern to be compatible with UNPCK instructions | |||
14779 | // but we aren't actually going to use the UNPCK instruction because doing | |||
14780 | // so prevents folding a load into this instruction or making a copy. | |||
14781 | const int UnpackLoMask[] = {0, 0, 1, 1}; | |||
14782 | const int UnpackHiMask[] = {2, 2, 3, 3}; | |||
14783 | if (isShuffleEquivalent(Mask, {0, 0, 1, 1}, V1, V2)) | |||
14784 | Mask = UnpackLoMask; | |||
14785 | else if (isShuffleEquivalent(Mask, {2, 2, 3, 3}, V1, V2)) | |||
14786 | Mask = UnpackHiMask; | |||
14787 | ||||
14788 | return DAG.getNode(X86ISD::PSHUFD, DL, MVT::v4i32, V1, | |||
14789 | getV4X86ShuffleImm8ForMask(Mask, DL, DAG)); | |||
14790 | } | |||
14791 | ||||
14792 | if (Subtarget.hasAVX2()) | |||
14793 | if (SDValue Extract = lowerShuffleOfExtractsAsVperm(DL, V1, V2, Mask, DAG)) | |||
14794 | return Extract; | |||
14795 | ||||
14796 | // Try to use shift instructions. | |||
14797 | if (SDValue Shift = lowerShuffleAsShift(DL, MVT::v4i32, V1, V2, Mask, | |||
14798 | Zeroable, Subtarget, DAG)) | |||
14799 | return Shift; | |||
14800 | ||||
14801 | // There are special ways we can lower some single-element blends. | |||
14802 | if (NumV2Elements == 1) | |||
14803 | if (SDValue V = lowerShuffleAsElementInsertion( | |||
14804 | DL, MVT::v4i32, V1, V2, Mask, Zeroable, Subtarget, DAG)) | |||
14805 | return V; | |||
14806 | ||||
14807 | // We have different paths for blend lowering, but they all must use the | |||
14808 | // *exact* same predicate. | |||
14809 | bool IsBlendSupported = Subtarget.hasSSE41(); | |||
14810 | if (IsBlendSupported) | |||
14811 | if (SDValue Blend = lowerShuffleAsBlend(DL, MVT::v4i32, V1, V2, Mask, | |||
14812 | Zeroable, Subtarget, DAG)) | |||
14813 | return Blend; | |||
14814 | ||||
14815 | if (SDValue Masked = lowerShuffleAsBitMask(DL, MVT::v4i32, V1, V2, Mask, | |||
14816 | Zeroable, Subtarget, DAG)) | |||
14817 | return Masked; | |||
14818 | ||||
14819 | // Use dedicated unpack instructions for masks that match their pattern. | |||
14820 | if (SDValue V = lowerShuffleWithUNPCK(DL, MVT::v4i32, Mask, V1, V2, DAG)) | |||
14821 | return V; | |||
14822 | ||||
14823 | // Try to use byte rotation instructions. | |||
14824 | // Its more profitable for pre-SSSE3 to use shuffles/unpacks. | |||
14825 | if (Subtarget.hasSSSE3()) { | |||
14826 | if (Subtarget.hasVLX()) | |||
14827 | if (SDValue Rotate = lowerShuffleAsVALIGN(DL, MVT::v4i32, V1, V2, Mask, | |||
14828 | Subtarget, DAG)) | |||
14829 | return Rotate; | |||
14830 | ||||
14831 | if (SDValue Rotate = lowerShuffleAsByteRotate(DL, MVT::v4i32, V1, V2, Mask, | |||
14832 | Subtarget, DAG)) | |||
14833 | return Rotate; | |||
14834 | } | |||
14835 | ||||
14836 | // Assume that a single SHUFPS is faster than an alternative sequence of | |||
14837 | // multiple instructions (even if the CPU has a domain penalty). | |||
14838 | // If some CPU is harmed by the domain switch, we can fix it in a later pass. | |||
14839 | if (!isSingleSHUFPSMask(Mask)) { | |||
14840 | // If we have direct support for blends, we should lower by decomposing into | |||
14841 | // a permute. That will be faster than the domain cross. | |||
14842 | if (IsBlendSupported) | |||
14843 | return lowerShuffleAsDecomposedShuffleMerge(DL, MVT::v4i32, V1, V2, Mask, | |||
14844 | Subtarget, DAG); | |||
14845 | ||||
14846 | // Try to lower by permuting the inputs into an unpack instruction. | |||
14847 | if (SDValue Unpack = lowerShuffleAsPermuteAndUnpack(DL, MVT::v4i32, V1, V2, | |||
14848 | Mask, Subtarget, DAG)) | |||
14849 | return Unpack; | |||
14850 | } | |||
14851 | ||||
14852 | // We implement this with SHUFPS because it can blend from two vectors. | |||
14853 | // Because we're going to eventually use SHUFPS, we use SHUFPS even to build | |||
14854 | // up the inputs, bypassing domain shift penalties that we would incur if we | |||
14855 | // directly used PSHUFD on Nehalem and older. For newer chips, this isn't | |||
14856 | // relevant. | |||
14857 | SDValue CastV1 = DAG.getBitcast(MVT::v4f32, V1); | |||
14858 | SDValue CastV2 = DAG.getBitcast(MVT::v4f32, V2); | |||
14859 | SDValue ShufPS = DAG.getVectorShuffle(MVT::v4f32, DL, CastV1, CastV2, Mask); | |||
14860 | return DAG.getBitcast(MVT::v4i32, ShufPS); | |||
14861 | } | |||
14862 | ||||
14863 | /// Lowering of single-input v8i16 shuffles is the cornerstone of SSE2 | |||
14864 | /// shuffle lowering, and the most complex part. | |||
14865 | /// | |||
14866 | /// The lowering strategy is to try to form pairs of input lanes which are | |||
14867 | /// targeted at the same half of the final vector, and then use a dword shuffle | |||
14868 | /// to place them onto the right half, and finally unpack the paired lanes into | |||
14869 | /// their final position. | |||
14870 | /// | |||
14871 | /// The exact breakdown of how to form these dword pairs and align them on the | |||
14872 | /// correct sides is really tricky. See the comments within the function for | |||
14873 | /// more of the details. | |||
14874 | /// | |||
14875 | /// This code also handles repeated 128-bit lanes of v8i16 shuffles, but each | |||
14876 | /// lane must shuffle the *exact* same way. In fact, you must pass a v8 Mask to | |||
14877 | /// this routine for it to work correctly. To shuffle a 256-bit or 512-bit i16 | |||
14878 | /// vector, form the analogous 128-bit 8-element Mask. | |||
14879 | static SDValue lowerV8I16GeneralSingleInputShuffle( | |||
14880 | const SDLoc &DL, MVT VT, SDValue V, MutableArrayRef<int> Mask, | |||
14881 | const X86Subtarget &Subtarget, SelectionDAG &DAG) { | |||
14882 | assert(VT.getVectorElementType() == MVT::i16 && "Bad input type!")(static_cast <bool> (VT.getVectorElementType() == MVT:: i16 && "Bad input type!") ? void (0) : __assert_fail ( "VT.getVectorElementType() == MVT::i16 && \"Bad input type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14882, __extension__ __PRETTY_FUNCTION__)); | |||
14883 | MVT PSHUFDVT = MVT::getVectorVT(MVT::i32, VT.getVectorNumElements() / 2); | |||
14884 | ||||
14885 | assert(Mask.size() == 8 && "Shuffle mask length doesn't match!")(static_cast <bool> (Mask.size() == 8 && "Shuffle mask length doesn't match!" ) ? void (0) : __assert_fail ("Mask.size() == 8 && \"Shuffle mask length doesn't match!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 14885, __extension__ __PRETTY_FUNCTION__)); | |||
14886 | MutableArrayRef<int> LoMask = Mask.slice(0, 4); | |||
14887 | MutableArrayRef<int> HiMask = Mask.slice(4, 4); | |||
14888 | ||||
14889 | // Attempt to directly match PSHUFLW or PSHUFHW. | |||
14890 | if (isUndefOrInRange(LoMask, 0, 4) && | |||
14891 | isSequentialOrUndefInRange(HiMask, 0, 4, 4)) { | |||
14892 | return DAG.getNode(X86ISD::PSHUFLW, DL, VT, V, | |||
14893 | getV4X86ShuffleImm8ForMask(LoMask, DL, DAG)); | |||
14894 | } | |||
14895 | if (isUndefOrInRange(HiMask, 4, 8) && | |||
14896 | isSequentialOrUndefInRange(LoMask, 0, 4, 0)) { | |||
14897 | for (int i = 0; i != 4; ++i) | |||
14898 | HiMask[i] = (HiMask[i] < 0 ? HiMask[i] : (HiMask[i] - 4)); | |||
14899 | return DAG.getNode(X86ISD::PSHUFHW, DL, VT, V, | |||
14900 | getV4X86ShuffleImm8ForMask(HiMask, DL, DAG)); | |||
14901 | } | |||
14902 | ||||
14903 | SmallVector<int, 4> LoInputs; | |||
14904 | copy_if(LoMask, std::back_inserter(LoInputs), [](int M) { return M >= 0; }); | |||
14905 | array_pod_sort(LoInputs.begin(), LoInputs.end()); | |||
14906 | LoInputs.erase(std::unique(LoInputs.begin(), LoInputs.end()), LoInputs.end()); | |||
14907 | SmallVector<int, 4> HiInputs; | |||
14908 | copy_if(HiMask, std::back_inserter(HiInputs), [](int M) { return M >= 0; }); | |||
14909 | array_pod_sort(HiInputs.begin(), HiInputs.end()); | |||
14910 | HiInputs.erase(std::unique(HiInputs.begin(), HiInputs.end()), HiInputs.end()); | |||
14911 | int NumLToL = llvm::lower_bound(LoInputs, 4) - LoInputs.begin(); | |||
14912 | int NumHToL = LoInputs.size() - NumLToL; | |||
14913 | int NumLToH = llvm::lower_bound(HiInputs, 4) - HiInputs.begin(); | |||
14914 | int NumHToH = HiInputs.size() - NumLToH; | |||
14915 | MutableArrayRef<int> LToLInputs(LoInputs.data(), NumLToL); | |||
14916 | MutableArrayRef<int> LToHInputs(HiInputs.data(), NumLToH); | |||
14917 | MutableArrayRef<int> HToLInputs(LoInputs.data() + NumLToL, NumHToL); | |||
14918 | MutableArrayRef<int> HToHInputs(HiInputs.data() + NumLToH, NumHToH); | |||
14919 | ||||
14920 | // If we are shuffling values from one half - check how many different DWORD | |||
14921 | // pairs we need to create. If only 1 or 2 then we can perform this as a | |||
14922 | // PSHUFLW/PSHUFHW + PSHUFD instead of the PSHUFD+PSHUFLW+PSHUFHW chain below. | |||
14923 | auto ShuffleDWordPairs = [&](ArrayRef<int> PSHUFHalfMask, | |||
14924 | ArrayRef<int> PSHUFDMask, unsigned ShufWOp) { | |||
14925 | V = DAG.getNode(ShufWOp, DL, VT, V, | |||
14926 | getV4X86ShuffleImm8ForMask(PSHUFHalfMask, DL, DAG)); | |||
14927 | V = DAG.getBitcast(PSHUFDVT, V); | |||
14928 | V = DAG.getNode(X86ISD::PSHUFD, DL, PSHUFDVT, V, | |||
14929 | getV4X86ShuffleImm8ForMask(PSHUFDMask, DL, DAG)); | |||
14930 | return DAG.getBitcast(VT, V); | |||
14931 | }; | |||
14932 | ||||
14933 | if ((NumHToL + NumHToH) == 0 || (NumLToL + NumLToH) == 0) { | |||
14934 | int PSHUFDMask[4] = { -1, -1, -1, -1 }; | |||
14935 | SmallVector<std::pair<int, int>, 4> DWordPairs; | |||
14936 | int DOffset = ((NumHToL + NumHToH) == 0 ? 0 : 2); | |||
14937 | ||||
14938 | // Collect the different DWORD pairs. | |||
14939 | for (int DWord = 0; DWord != 4; ++DWord) { | |||
14940 | int M0 = Mask[2 * DWord + 0]; | |||
14941 | int M1 = Mask[2 * DWord + 1]; | |||
14942 | M0 = (M0 >= 0 ? M0 % 4 : M0); | |||
14943 | M1 = (M1 >= 0 ? M1 % 4 : M1); | |||
14944 | if (M0 < 0 && M1 < 0) | |||
14945 | continue; | |||
14946 | ||||
14947 | bool Match = false; | |||
14948 | for (int j = 0, e = DWordPairs.size(); j < e; ++j) { | |||
14949 | auto &DWordPair = DWordPairs[j]; | |||
14950 | if ((M0 < 0 || isUndefOrEqual(DWordPair.first, M0)) && | |||
14951 | (M1 < 0 || isUndefOrEqual(DWordPair.second, M1))) { | |||
14952 | DWordPair.first = (M0 >= 0 ? M0 : DWordPair.first); | |||
14953 | DWordPair.second = (M1 >= 0 ? M1 : DWordPair.second); | |||
14954 | PSHUFDMask[DWord] = DOffset + j; | |||
14955 | Match = true; | |||
14956 | break; | |||
14957 | } | |||
14958 | } | |||
14959 | if (!Match) { | |||
14960 | PSHUFDMask[DWord] = DOffset + DWordPairs.size(); | |||
14961 | DWordPairs.push_back(std::make_pair(M0, M1)); | |||
14962 | } | |||
14963 | } | |||
14964 | ||||
14965 | if (DWordPairs.size() <= 2) { | |||
14966 | DWordPairs.resize(2, std::make_pair(-1, -1)); | |||
14967 | int PSHUFHalfMask[4] = {DWordPairs[0].first, DWordPairs[0].second, | |||
14968 | DWordPairs[1].first, DWordPairs[1].second}; | |||
14969 | if ((NumHToL + NumHToH) == 0) | |||
14970 | return ShuffleDWordPairs(PSHUFHalfMask, PSHUFDMask, X86ISD::PSHUFLW); | |||
14971 | if ((NumLToL + NumLToH) == 0) | |||
14972 | return ShuffleDWordPairs(PSHUFHalfMask, PSHUFDMask, X86ISD::PSHUFHW); | |||
14973 | } | |||
14974 | } | |||
14975 | ||||
14976 | // Simplify the 1-into-3 and 3-into-1 cases with a single pshufd. For all | |||
14977 | // such inputs we can swap two of the dwords across the half mark and end up | |||
14978 | // with <=2 inputs to each half in each half. Once there, we can fall through | |||
14979 | // to the generic code below. For example: | |||
14980 | // | |||
14981 | // Input: [a, b, c, d, e, f, g, h] -PSHUFD[0,2,1,3]-> [a, b, e, f, c, d, g, h] | |||
14982 | // Mask: [0, 1, 2, 7, 4, 5, 6, 3] -----------------> [0, 1, 4, 7, 2, 3, 6, 5] | |||
14983 | // | |||
14984 | // However in some very rare cases we have a 1-into-3 or 3-into-1 on one half | |||
14985 | // and an existing 2-into-2 on the other half. In this case we may have to | |||
14986 | // pre-shuffle the 2-into-2 half to avoid turning it into a 3-into-1 or | |||
14987 | // 1-into-3 which could cause us to cycle endlessly fixing each side in turn. | |||
14988 | // Fortunately, we don't have to handle anything but a 2-into-2 pattern | |||
14989 | // because any other situation (including a 3-into-1 or 1-into-3 in the other | |||
14990 | // half than the one we target for fixing) will be fixed when we re-enter this | |||
14991 | // path. We will also combine away any sequence of PSHUFD instructions that | |||
14992 | // result into a single instruction. Here is an example of the tricky case: | |||
14993 | // | |||
14994 | // Input: [a, b, c, d, e, f, g, h] -PSHUFD[0,2,1,3]-> [a, b, e, f, c, d, g, h] | |||
14995 | // Mask: [3, 7, 1, 0, 2, 7, 3, 5] -THIS-IS-BAD!!!!-> [5, 7, 1, 0, 4, 7, 5, 3] | |||
14996 | // | |||
14997 | // This now has a 1-into-3 in the high half! Instead, we do two shuffles: | |||
14998 | // | |||
14999 | // Input: [a, b, c, d, e, f, g, h] PSHUFHW[0,2,1,3]-> [a, b, c, d, e, g, f, h] | |||
15000 | // Mask: [3, 7, 1, 0, 2, 7, 3, 5] -----------------> [3, 7, 1, 0, 2, 7, 3, 6] | |||
15001 | // | |||
15002 | // Input: [a, b, c, d, e, g, f, h] -PSHUFD[0,2,1,3]-> [a, b, e, g, c, d, f, h] | |||
15003 | // Mask: [3, 7, 1, 0, 2, 7, 3, 6] -----------------> [5, 7, 1, 0, 4, 7, 5, 6] | |||
15004 | // | |||
15005 | // The result is fine to be handled by the generic logic. | |||
15006 | auto balanceSides = [&](ArrayRef<int> AToAInputs, ArrayRef<int> BToAInputs, | |||
15007 | ArrayRef<int> BToBInputs, ArrayRef<int> AToBInputs, | |||
15008 | int AOffset, int BOffset) { | |||
15009 | assert((AToAInputs.size() == 3 || AToAInputs.size() == 1) &&(static_cast <bool> ((AToAInputs.size() == 3 || AToAInputs .size() == 1) && "Must call this with A having 3 or 1 inputs from the A half." ) ? void (0) : __assert_fail ("(AToAInputs.size() == 3 || AToAInputs.size() == 1) && \"Must call this with A having 3 or 1 inputs from the A half.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15010, __extension__ __PRETTY_FUNCTION__)) | |||
15010 | "Must call this with A having 3 or 1 inputs from the A half.")(static_cast <bool> ((AToAInputs.size() == 3 || AToAInputs .size() == 1) && "Must call this with A having 3 or 1 inputs from the A half." ) ? void (0) : __assert_fail ("(AToAInputs.size() == 3 || AToAInputs.size() == 1) && \"Must call this with A having 3 or 1 inputs from the A half.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15010, __extension__ __PRETTY_FUNCTION__)); | |||
15011 | assert((BToAInputs.size() == 1 || BToAInputs.size() == 3) &&(static_cast <bool> ((BToAInputs.size() == 1 || BToAInputs .size() == 3) && "Must call this with B having 1 or 3 inputs from the B half." ) ? void (0) : __assert_fail ("(BToAInputs.size() == 1 || BToAInputs.size() == 3) && \"Must call this with B having 1 or 3 inputs from the B half.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15012, __extension__ __PRETTY_FUNCTION__)) | |||
15012 | "Must call this with B having 1 or 3 inputs from the B half.")(static_cast <bool> ((BToAInputs.size() == 1 || BToAInputs .size() == 3) && "Must call this with B having 1 or 3 inputs from the B half." ) ? void (0) : __assert_fail ("(BToAInputs.size() == 1 || BToAInputs.size() == 3) && \"Must call this with B having 1 or 3 inputs from the B half.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15012, __extension__ __PRETTY_FUNCTION__)); | |||
15013 | assert(AToAInputs.size() + BToAInputs.size() == 4 &&(static_cast <bool> (AToAInputs.size() + BToAInputs.size () == 4 && "Must call this with either 3:1 or 1:3 inputs (summing to 4)." ) ? void (0) : __assert_fail ("AToAInputs.size() + BToAInputs.size() == 4 && \"Must call this with either 3:1 or 1:3 inputs (summing to 4).\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15014, __extension__ __PRETTY_FUNCTION__)) | |||
15014 | "Must call this with either 3:1 or 1:3 inputs (summing to 4).")(static_cast <bool> (AToAInputs.size() + BToAInputs.size () == 4 && "Must call this with either 3:1 or 1:3 inputs (summing to 4)." ) ? void (0) : __assert_fail ("AToAInputs.size() + BToAInputs.size() == 4 && \"Must call this with either 3:1 or 1:3 inputs (summing to 4).\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15014, __extension__ __PRETTY_FUNCTION__)); | |||
15015 | ||||
15016 | bool ThreeAInputs = AToAInputs.size() == 3; | |||
15017 | ||||
15018 | // Compute the index of dword with only one word among the three inputs in | |||
15019 | // a half by taking the sum of the half with three inputs and subtracting | |||
15020 | // the sum of the actual three inputs. The difference is the remaining | |||
15021 | // slot. | |||
15022 | int ADWord = 0, BDWord = 0; | |||
15023 | int &TripleDWord = ThreeAInputs ? ADWord : BDWord; | |||
15024 | int &OneInputDWord = ThreeAInputs ? BDWord : ADWord; | |||
15025 | int TripleInputOffset = ThreeAInputs ? AOffset : BOffset; | |||
15026 | ArrayRef<int> TripleInputs = ThreeAInputs ? AToAInputs : BToAInputs; | |||
15027 | int OneInput = ThreeAInputs ? BToAInputs[0] : AToAInputs[0]; | |||
15028 | int TripleInputSum = 0 + 1 + 2 + 3 + (4 * TripleInputOffset); | |||
15029 | int TripleNonInputIdx = | |||
15030 | TripleInputSum - std::accumulate(TripleInputs.begin(), TripleInputs.end(), 0); | |||
15031 | TripleDWord = TripleNonInputIdx / 2; | |||
15032 | ||||
15033 | // We use xor with one to compute the adjacent DWord to whichever one the | |||
15034 | // OneInput is in. | |||
15035 | OneInputDWord = (OneInput / 2) ^ 1; | |||
15036 | ||||
15037 | // Check for one tricky case: We're fixing a 3<-1 or a 1<-3 shuffle for AToA | |||
15038 | // and BToA inputs. If there is also such a problem with the BToB and AToB | |||
15039 | // inputs, we don't try to fix it necessarily -- we'll recurse and see it in | |||
15040 | // the next pass. However, if we have a 2<-2 in the BToB and AToB inputs, it | |||
15041 | // is essential that we don't *create* a 3<-1 as then we might oscillate. | |||
15042 | if (BToBInputs.size() == 2 && AToBInputs.size() == 2) { | |||
15043 | // Compute how many inputs will be flipped by swapping these DWords. We | |||
15044 | // need | |||
15045 | // to balance this to ensure we don't form a 3-1 shuffle in the other | |||
15046 | // half. | |||
15047 | int NumFlippedAToBInputs = | |||
15048 | std::count(AToBInputs.begin(), AToBInputs.end(), 2 * ADWord) + | |||
15049 | std::count(AToBInputs.begin(), AToBInputs.end(), 2 * ADWord + 1); | |||
15050 | int NumFlippedBToBInputs = | |||
15051 | std::count(BToBInputs.begin(), BToBInputs.end(), 2 * BDWord) + | |||
15052 | std::count(BToBInputs.begin(), BToBInputs.end(), 2 * BDWord + 1); | |||
15053 | if ((NumFlippedAToBInputs == 1 && | |||
15054 | (NumFlippedBToBInputs == 0 || NumFlippedBToBInputs == 2)) || | |||
15055 | (NumFlippedBToBInputs == 1 && | |||
15056 | (NumFlippedAToBInputs == 0 || NumFlippedAToBInputs == 2))) { | |||
15057 | // We choose whether to fix the A half or B half based on whether that | |||
15058 | // half has zero flipped inputs. At zero, we may not be able to fix it | |||
15059 | // with that half. We also bias towards fixing the B half because that | |||
15060 | // will more commonly be the high half, and we have to bias one way. | |||
15061 | auto FixFlippedInputs = [&V, &DL, &Mask, &DAG](int PinnedIdx, int DWord, | |||
15062 | ArrayRef<int> Inputs) { | |||
15063 | int FixIdx = PinnedIdx ^ 1; // The adjacent slot to the pinned slot. | |||
15064 | bool IsFixIdxInput = is_contained(Inputs, PinnedIdx ^ 1); | |||
15065 | // Determine whether the free index is in the flipped dword or the | |||
15066 | // unflipped dword based on where the pinned index is. We use this bit | |||
15067 | // in an xor to conditionally select the adjacent dword. | |||
15068 | int FixFreeIdx = 2 * (DWord ^ (PinnedIdx / 2 == DWord)); | |||
15069 | bool IsFixFreeIdxInput = is_contained(Inputs, FixFreeIdx); | |||
15070 | if (IsFixIdxInput == IsFixFreeIdxInput) | |||
15071 | FixFreeIdx += 1; | |||
15072 | IsFixFreeIdxInput = is_contained(Inputs, FixFreeIdx); | |||
15073 | assert(IsFixIdxInput != IsFixFreeIdxInput &&(static_cast <bool> (IsFixIdxInput != IsFixFreeIdxInput && "We need to be changing the number of flipped inputs!" ) ? void (0) : __assert_fail ("IsFixIdxInput != IsFixFreeIdxInput && \"We need to be changing the number of flipped inputs!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15074, __extension__ __PRETTY_FUNCTION__)) | |||
15074 | "We need to be changing the number of flipped inputs!")(static_cast <bool> (IsFixIdxInput != IsFixFreeIdxInput && "We need to be changing the number of flipped inputs!" ) ? void (0) : __assert_fail ("IsFixIdxInput != IsFixFreeIdxInput && \"We need to be changing the number of flipped inputs!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15074, __extension__ __PRETTY_FUNCTION__)); | |||
15075 | int PSHUFHalfMask[] = {0, 1, 2, 3}; | |||
15076 | std::swap(PSHUFHalfMask[FixFreeIdx % 4], PSHUFHalfMask[FixIdx % 4]); | |||
15077 | V = DAG.getNode( | |||
15078 | FixIdx < 4 ? X86ISD::PSHUFLW : X86ISD::PSHUFHW, DL, | |||
15079 | MVT::getVectorVT(MVT::i16, V.getValueSizeInBits() / 16), V, | |||
15080 | getV4X86ShuffleImm8ForMask(PSHUFHalfMask, DL, DAG)); | |||
15081 | ||||
15082 | for (int &M : Mask) | |||
15083 | if (M >= 0 && M == FixIdx) | |||
15084 | M = FixFreeIdx; | |||
15085 | else if (M >= 0 && M == FixFreeIdx) | |||
15086 | M = FixIdx; | |||
15087 | }; | |||
15088 | if (NumFlippedBToBInputs != 0) { | |||
15089 | int BPinnedIdx = | |||
15090 | BToAInputs.size() == 3 ? TripleNonInputIdx : OneInput; | |||
15091 | FixFlippedInputs(BPinnedIdx, BDWord, BToBInputs); | |||
15092 | } else { | |||
15093 | assert(NumFlippedAToBInputs != 0 && "Impossible given predicates!")(static_cast <bool> (NumFlippedAToBInputs != 0 && "Impossible given predicates!") ? void (0) : __assert_fail ( "NumFlippedAToBInputs != 0 && \"Impossible given predicates!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15093, __extension__ __PRETTY_FUNCTION__)); | |||
15094 | int APinnedIdx = ThreeAInputs ? TripleNonInputIdx : OneInput; | |||
15095 | FixFlippedInputs(APinnedIdx, ADWord, AToBInputs); | |||
15096 | } | |||
15097 | } | |||
15098 | } | |||
15099 | ||||
15100 | int PSHUFDMask[] = {0, 1, 2, 3}; | |||
15101 | PSHUFDMask[ADWord] = BDWord; | |||
15102 | PSHUFDMask[BDWord] = ADWord; | |||
15103 | V = DAG.getBitcast( | |||
15104 | VT, | |||
15105 | DAG.getNode(X86ISD::PSHUFD, DL, PSHUFDVT, DAG.getBitcast(PSHUFDVT, V), | |||
15106 | getV4X86ShuffleImm8ForMask(PSHUFDMask, DL, DAG))); | |||
15107 | ||||
15108 | // Adjust the mask to match the new locations of A and B. | |||
15109 | for (int &M : Mask) | |||
15110 | if (M >= 0 && M/2 == ADWord) | |||
15111 | M = 2 * BDWord + M % 2; | |||
15112 | else if (M >= 0 && M/2 == BDWord) | |||
15113 | M = 2 * ADWord + M % 2; | |||
15114 | ||||
15115 | // Recurse back into this routine to re-compute state now that this isn't | |||
15116 | // a 3 and 1 problem. | |||
15117 | return lowerV8I16GeneralSingleInputShuffle(DL, VT, V, Mask, Subtarget, DAG); | |||
15118 | }; | |||
15119 | if ((NumLToL == 3 && NumHToL == 1) || (NumLToL == 1 && NumHToL == 3)) | |||
15120 | return balanceSides(LToLInputs, HToLInputs, HToHInputs, LToHInputs, 0, 4); | |||
15121 | if ((NumHToH == 3 && NumLToH == 1) || (NumHToH == 1 && NumLToH == 3)) | |||
15122 | return balanceSides(HToHInputs, LToHInputs, LToLInputs, HToLInputs, 4, 0); | |||
15123 | ||||
15124 | // At this point there are at most two inputs to the low and high halves from | |||
15125 | // each half. That means the inputs can always be grouped into dwords and | |||
15126 | // those dwords can then be moved to the correct half with a dword shuffle. | |||
15127 | // We use at most one low and one high word shuffle to collect these paired | |||
15128 | // inputs into dwords, and finally a dword shuffle to place them. | |||
15129 | int PSHUFLMask[4] = {-1, -1, -1, -1}; | |||
15130 | int PSHUFHMask[4] = {-1, -1, -1, -1}; | |||
15131 | int PSHUFDMask[4] = {-1, -1, -1, -1}; | |||
15132 | ||||
15133 | // First fix the masks for all the inputs that are staying in their | |||
15134 | // original halves. This will then dictate the targets of the cross-half | |||
15135 | // shuffles. | |||
15136 | auto fixInPlaceInputs = | |||
15137 | [&PSHUFDMask](ArrayRef<int> InPlaceInputs, ArrayRef<int> IncomingInputs, | |||
15138 | MutableArrayRef<int> SourceHalfMask, | |||
15139 | MutableArrayRef<int> HalfMask, int HalfOffset) { | |||
15140 | if (InPlaceInputs.empty()) | |||
15141 | return; | |||
15142 | if (InPlaceInputs.size() == 1) { | |||
15143 | SourceHalfMask[InPlaceInputs[0] - HalfOffset] = | |||
15144 | InPlaceInputs[0] - HalfOffset; | |||
15145 | PSHUFDMask[InPlaceInputs[0] / 2] = InPlaceInputs[0] / 2; | |||
15146 | return; | |||
15147 | } | |||
15148 | if (IncomingInputs.empty()) { | |||
15149 | // Just fix all of the in place inputs. | |||
15150 | for (int Input : InPlaceInputs) { | |||
15151 | SourceHalfMask[Input - HalfOffset] = Input - HalfOffset; | |||
15152 | PSHUFDMask[Input / 2] = Input / 2; | |||
15153 | } | |||
15154 | return; | |||
15155 | } | |||
15156 | ||||
15157 | assert(InPlaceInputs.size() == 2 && "Cannot handle 3 or 4 inputs!")(static_cast <bool> (InPlaceInputs.size() == 2 && "Cannot handle 3 or 4 inputs!") ? void (0) : __assert_fail ( "InPlaceInputs.size() == 2 && \"Cannot handle 3 or 4 inputs!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15157, __extension__ __PRETTY_FUNCTION__)); | |||
15158 | SourceHalfMask[InPlaceInputs[0] - HalfOffset] = | |||
15159 | InPlaceInputs[0] - HalfOffset; | |||
15160 | // Put the second input next to the first so that they are packed into | |||
15161 | // a dword. We find the adjacent index by toggling the low bit. | |||
15162 | int AdjIndex = InPlaceInputs[0] ^ 1; | |||
15163 | SourceHalfMask[AdjIndex - HalfOffset] = InPlaceInputs[1] - HalfOffset; | |||
15164 | std::replace(HalfMask.begin(), HalfMask.end(), InPlaceInputs[1], AdjIndex); | |||
15165 | PSHUFDMask[AdjIndex / 2] = AdjIndex / 2; | |||
15166 | }; | |||
15167 | fixInPlaceInputs(LToLInputs, HToLInputs, PSHUFLMask, LoMask, 0); | |||
15168 | fixInPlaceInputs(HToHInputs, LToHInputs, PSHUFHMask, HiMask, 4); | |||
15169 | ||||
15170 | // Now gather the cross-half inputs and place them into a free dword of | |||
15171 | // their target half. | |||
15172 | // FIXME: This operation could almost certainly be simplified dramatically to | |||
15173 | // look more like the 3-1 fixing operation. | |||
15174 | auto moveInputsToRightHalf = [&PSHUFDMask]( | |||
15175 | MutableArrayRef<int> IncomingInputs, ArrayRef<int> ExistingInputs, | |||
15176 | MutableArrayRef<int> SourceHalfMask, MutableArrayRef<int> HalfMask, | |||
15177 | MutableArrayRef<int> FinalSourceHalfMask, int SourceOffset, | |||
15178 | int DestOffset) { | |||
15179 | auto isWordClobbered = [](ArrayRef<int> SourceHalfMask, int Word) { | |||
15180 | return SourceHalfMask[Word] >= 0 && SourceHalfMask[Word] != Word; | |||
15181 | }; | |||
15182 | auto isDWordClobbered = [&isWordClobbered](ArrayRef<int> SourceHalfMask, | |||
15183 | int Word) { | |||
15184 | int LowWord = Word & ~1; | |||
15185 | int HighWord = Word | 1; | |||
15186 | return isWordClobbered(SourceHalfMask, LowWord) || | |||
15187 | isWordClobbered(SourceHalfMask, HighWord); | |||
15188 | }; | |||
15189 | ||||
15190 | if (IncomingInputs.empty()) | |||
15191 | return; | |||
15192 | ||||
15193 | if (ExistingInputs.empty()) { | |||
15194 | // Map any dwords with inputs from them into the right half. | |||
15195 | for (int Input : IncomingInputs) { | |||
15196 | // If the source half mask maps over the inputs, turn those into | |||
15197 | // swaps and use the swapped lane. | |||
15198 | if (isWordClobbered(SourceHalfMask, Input - SourceOffset)) { | |||
15199 | if (SourceHalfMask[SourceHalfMask[Input - SourceOffset]] < 0) { | |||
15200 | SourceHalfMask[SourceHalfMask[Input - SourceOffset]] = | |||
15201 | Input - SourceOffset; | |||
15202 | // We have to swap the uses in our half mask in one sweep. | |||
15203 | for (int &M : HalfMask) | |||
15204 | if (M == SourceHalfMask[Input - SourceOffset] + SourceOffset) | |||
15205 | M = Input; | |||
15206 | else if (M == Input) | |||
15207 | M = SourceHalfMask[Input - SourceOffset] + SourceOffset; | |||
15208 | } else { | |||
15209 | assert(SourceHalfMask[SourceHalfMask[Input - SourceOffset]] ==(static_cast <bool> (SourceHalfMask[SourceHalfMask[Input - SourceOffset]] == Input - SourceOffset && "Previous placement doesn't match!" ) ? void (0) : __assert_fail ("SourceHalfMask[SourceHalfMask[Input - SourceOffset]] == Input - SourceOffset && \"Previous placement doesn't match!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15211, __extension__ __PRETTY_FUNCTION__)) | |||
15210 | Input - SourceOffset &&(static_cast <bool> (SourceHalfMask[SourceHalfMask[Input - SourceOffset]] == Input - SourceOffset && "Previous placement doesn't match!" ) ? void (0) : __assert_fail ("SourceHalfMask[SourceHalfMask[Input - SourceOffset]] == Input - SourceOffset && \"Previous placement doesn't match!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15211, __extension__ __PRETTY_FUNCTION__)) | |||
15211 | "Previous placement doesn't match!")(static_cast <bool> (SourceHalfMask[SourceHalfMask[Input - SourceOffset]] == Input - SourceOffset && "Previous placement doesn't match!" ) ? void (0) : __assert_fail ("SourceHalfMask[SourceHalfMask[Input - SourceOffset]] == Input - SourceOffset && \"Previous placement doesn't match!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15211, __extension__ __PRETTY_FUNCTION__)); | |||
15212 | } | |||
15213 | // Note that this correctly re-maps both when we do a swap and when | |||
15214 | // we observe the other side of the swap above. We rely on that to | |||
15215 | // avoid swapping the members of the input list directly. | |||
15216 | Input = SourceHalfMask[Input - SourceOffset] + SourceOffset; | |||
15217 | } | |||
15218 | ||||
15219 | // Map the input's dword into the correct half. | |||
15220 | if (PSHUFDMask[(Input - SourceOffset + DestOffset) / 2] < 0) | |||
15221 | PSHUFDMask[(Input - SourceOffset + DestOffset) / 2] = Input / 2; | |||
15222 | else | |||
15223 | assert(PSHUFDMask[(Input - SourceOffset + DestOffset) / 2] ==(static_cast <bool> (PSHUFDMask[(Input - SourceOffset + DestOffset) / 2] == Input / 2 && "Previous placement doesn't match!" ) ? void (0) : __assert_fail ("PSHUFDMask[(Input - SourceOffset + DestOffset) / 2] == Input / 2 && \"Previous placement doesn't match!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15225, __extension__ __PRETTY_FUNCTION__)) | |||
15224 | Input / 2 &&(static_cast <bool> (PSHUFDMask[(Input - SourceOffset + DestOffset) / 2] == Input / 2 && "Previous placement doesn't match!" ) ? void (0) : __assert_fail ("PSHUFDMask[(Input - SourceOffset + DestOffset) / 2] == Input / 2 && \"Previous placement doesn't match!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15225, __extension__ __PRETTY_FUNCTION__)) | |||
15225 | "Previous placement doesn't match!")(static_cast <bool> (PSHUFDMask[(Input - SourceOffset + DestOffset) / 2] == Input / 2 && "Previous placement doesn't match!" ) ? void (0) : __assert_fail ("PSHUFDMask[(Input - SourceOffset + DestOffset) / 2] == Input / 2 && \"Previous placement doesn't match!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15225, __extension__ __PRETTY_FUNCTION__)); | |||
15226 | } | |||
15227 | ||||
15228 | // And just directly shift any other-half mask elements to be same-half | |||
15229 | // as we will have mirrored the dword containing the element into the | |||
15230 | // same position within that half. | |||
15231 | for (int &M : HalfMask) | |||
15232 | if (M >= SourceOffset && M < SourceOffset + 4) { | |||
15233 | M = M - SourceOffset + DestOffset; | |||
15234 | assert(M >= 0 && "This should never wrap below zero!")(static_cast <bool> (M >= 0 && "This should never wrap below zero!" ) ? void (0) : __assert_fail ("M >= 0 && \"This should never wrap below zero!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15234, __extension__ __PRETTY_FUNCTION__)); | |||
15235 | } | |||
15236 | return; | |||
15237 | } | |||
15238 | ||||
15239 | // Ensure we have the input in a viable dword of its current half. This | |||
15240 | // is particularly tricky because the original position may be clobbered | |||
15241 | // by inputs being moved and *staying* in that half. | |||
15242 | if (IncomingInputs.size() == 1) { | |||
15243 | if (isWordClobbered(SourceHalfMask, IncomingInputs[0] - SourceOffset)) { | |||
15244 | int InputFixed = find(SourceHalfMask, -1) - std::begin(SourceHalfMask) + | |||
15245 | SourceOffset; | |||
15246 | SourceHalfMask[InputFixed - SourceOffset] = | |||
15247 | IncomingInputs[0] - SourceOffset; | |||
15248 | std::replace(HalfMask.begin(), HalfMask.end(), IncomingInputs[0], | |||
15249 | InputFixed); | |||
15250 | IncomingInputs[0] = InputFixed; | |||
15251 | } | |||
15252 | } else if (IncomingInputs.size() == 2) { | |||
15253 | if (IncomingInputs[0] / 2 != IncomingInputs[1] / 2 || | |||
15254 | isDWordClobbered(SourceHalfMask, IncomingInputs[0] - SourceOffset)) { | |||
15255 | // We have two non-adjacent or clobbered inputs we need to extract from | |||
15256 | // the source half. To do this, we need to map them into some adjacent | |||
15257 | // dword slot in the source mask. | |||
15258 | int InputsFixed[2] = {IncomingInputs[0] - SourceOffset, | |||
15259 | IncomingInputs[1] - SourceOffset}; | |||
15260 | ||||
15261 | // If there is a free slot in the source half mask adjacent to one of | |||
15262 | // the inputs, place the other input in it. We use (Index XOR 1) to | |||
15263 | // compute an adjacent index. | |||
15264 | if (!isWordClobbered(SourceHalfMask, InputsFixed[0]) && | |||
15265 | SourceHalfMask[InputsFixed[0] ^ 1] < 0) { | |||
15266 | SourceHalfMask[InputsFixed[0]] = InputsFixed[0]; | |||
15267 | SourceHalfMask[InputsFixed[0] ^ 1] = InputsFixed[1]; | |||
15268 | InputsFixed[1] = InputsFixed[0] ^ 1; | |||
15269 | } else if (!isWordClobbered(SourceHalfMask, InputsFixed[1]) && | |||
15270 | SourceHalfMask[InputsFixed[1] ^ 1] < 0) { | |||
15271 | SourceHalfMask[InputsFixed[1]] = InputsFixed[1]; | |||
15272 | SourceHalfMask[InputsFixed[1] ^ 1] = InputsFixed[0]; | |||
15273 | InputsFixed[0] = InputsFixed[1] ^ 1; | |||
15274 | } else if (SourceHalfMask[2 * ((InputsFixed[0] / 2) ^ 1)] < 0 && | |||
15275 | SourceHalfMask[2 * ((InputsFixed[0] / 2) ^ 1) + 1] < 0) { | |||
15276 | // The two inputs are in the same DWord but it is clobbered and the | |||
15277 | // adjacent DWord isn't used at all. Move both inputs to the free | |||
15278 | // slot. | |||
15279 | SourceHalfMask[2 * ((InputsFixed[0] / 2) ^ 1)] = InputsFixed[0]; | |||
15280 | SourceHalfMask[2 * ((InputsFixed[0] / 2) ^ 1) + 1] = InputsFixed[1]; | |||
15281 | InputsFixed[0] = 2 * ((InputsFixed[0] / 2) ^ 1); | |||
15282 | InputsFixed[1] = 2 * ((InputsFixed[0] / 2) ^ 1) + 1; | |||
15283 | } else { | |||
15284 | // The only way we hit this point is if there is no clobbering | |||
15285 | // (because there are no off-half inputs to this half) and there is no | |||
15286 | // free slot adjacent to one of the inputs. In this case, we have to | |||
15287 | // swap an input with a non-input. | |||
15288 | for (int i = 0; i < 4; ++i) | |||
15289 | assert((SourceHalfMask[i] < 0 || SourceHalfMask[i] == i) &&(static_cast <bool> ((SourceHalfMask[i] < 0 || SourceHalfMask [i] == i) && "We can't handle any clobbers here!") ? void (0) : __assert_fail ("(SourceHalfMask[i] < 0 || SourceHalfMask[i] == i) && \"We can't handle any clobbers here!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15290, __extension__ __PRETTY_FUNCTION__)) | |||
15290 | "We can't handle any clobbers here!")(static_cast <bool> ((SourceHalfMask[i] < 0 || SourceHalfMask [i] == i) && "We can't handle any clobbers here!") ? void (0) : __assert_fail ("(SourceHalfMask[i] < 0 || SourceHalfMask[i] == i) && \"We can't handle any clobbers here!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15290, __extension__ __PRETTY_FUNCTION__)); | |||
15291 | assert(InputsFixed[1] != (InputsFixed[0] ^ 1) &&(static_cast <bool> (InputsFixed[1] != (InputsFixed[0] ^ 1) && "Cannot have adjacent inputs here!") ? void (0 ) : __assert_fail ("InputsFixed[1] != (InputsFixed[0] ^ 1) && \"Cannot have adjacent inputs here!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15292, __extension__ __PRETTY_FUNCTION__)) | |||
15292 | "Cannot have adjacent inputs here!")(static_cast <bool> (InputsFixed[1] != (InputsFixed[0] ^ 1) && "Cannot have adjacent inputs here!") ? void (0 ) : __assert_fail ("InputsFixed[1] != (InputsFixed[0] ^ 1) && \"Cannot have adjacent inputs here!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15292, __extension__ __PRETTY_FUNCTION__)); | |||
15293 | ||||
15294 | SourceHalfMask[InputsFixed[0] ^ 1] = InputsFixed[1]; | |||
15295 | SourceHalfMask[InputsFixed[1]] = InputsFixed[0] ^ 1; | |||
15296 | ||||
15297 | // We also have to update the final source mask in this case because | |||
15298 | // it may need to undo the above swap. | |||
15299 | for (int &M : FinalSourceHalfMask) | |||
15300 | if (M == (InputsFixed[0] ^ 1) + SourceOffset) | |||
15301 | M = InputsFixed[1] + SourceOffset; | |||
15302 | else if (M == InputsFixed[1] + SourceOffset) | |||
15303 | M = (InputsFixed[0] ^ 1) + SourceOffset; | |||
15304 | ||||
15305 | InputsFixed[1] = InputsFixed[0] ^ 1; | |||
15306 | } | |||
15307 | ||||
15308 | // Point everything at the fixed inputs. | |||
15309 | for (int &M : HalfMask) | |||
15310 | if (M == IncomingInputs[0]) | |||
15311 | M = InputsFixed[0] + SourceOffset; | |||
15312 | else if (M == IncomingInputs[1]) | |||
15313 | M = InputsFixed[1] + SourceOffset; | |||
15314 | ||||
15315 | IncomingInputs[0] = InputsFixed[0] + SourceOffset; | |||
15316 | IncomingInputs[1] = InputsFixed[1] + SourceOffset; | |||
15317 | } | |||
15318 | } else { | |||
15319 | llvm_unreachable("Unhandled input size!")::llvm::llvm_unreachable_internal("Unhandled input size!", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15319); | |||
15320 | } | |||
15321 | ||||
15322 | // Now hoist the DWord down to the right half. | |||
15323 | int FreeDWord = (PSHUFDMask[DestOffset / 2] < 0 ? 0 : 1) + DestOffset / 2; | |||
15324 | assert(PSHUFDMask[FreeDWord] < 0 && "DWord not free")(static_cast <bool> (PSHUFDMask[FreeDWord] < 0 && "DWord not free") ? void (0) : __assert_fail ("PSHUFDMask[FreeDWord] < 0 && \"DWord not free\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15324, __extension__ __PRETTY_FUNCTION__)); | |||
15325 | PSHUFDMask[FreeDWord] = IncomingInputs[0] / 2; | |||
15326 | for (int &M : HalfMask) | |||
15327 | for (int Input : IncomingInputs) | |||
15328 | if (M == Input) | |||
15329 | M = FreeDWord * 2 + Input % 2; | |||
15330 | }; | |||
15331 | moveInputsToRightHalf(HToLInputs, LToLInputs, PSHUFHMask, LoMask, HiMask, | |||
15332 | /*SourceOffset*/ 4, /*DestOffset*/ 0); | |||
15333 | moveInputsToRightHalf(LToHInputs, HToHInputs, PSHUFLMask, HiMask, LoMask, | |||
15334 | /*SourceOffset*/ 0, /*DestOffset*/ 4); | |||
15335 | ||||
15336 | // Now enact all the shuffles we've computed to move the inputs into their | |||
15337 | // target half. | |||
15338 | if (!isNoopShuffleMask(PSHUFLMask)) | |||
15339 | V = DAG.getNode(X86ISD::PSHUFLW, DL, VT, V, | |||
15340 | getV4X86ShuffleImm8ForMask(PSHUFLMask, DL, DAG)); | |||
15341 | if (!isNoopShuffleMask(PSHUFHMask)) | |||
15342 | V = DAG.getNode(X86ISD::PSHUFHW, DL, VT, V, | |||
15343 | getV4X86ShuffleImm8ForMask(PSHUFHMask, DL, DAG)); | |||
15344 | if (!isNoopShuffleMask(PSHUFDMask)) | |||
15345 | V = DAG.getBitcast( | |||
15346 | VT, | |||
15347 | DAG.getNode(X86ISD::PSHUFD, DL, PSHUFDVT, DAG.getBitcast(PSHUFDVT, V), | |||
15348 | getV4X86ShuffleImm8ForMask(PSHUFDMask, DL, DAG))); | |||
15349 | ||||
15350 | // At this point, each half should contain all its inputs, and we can then | |||
15351 | // just shuffle them into their final position. | |||
15352 | assert(count_if(LoMask, [](int M) { return M >= 4; }) == 0 &&(static_cast <bool> (count_if(LoMask, [](int M) { return M >= 4; }) == 0 && "Failed to lift all the high half inputs to the low mask!" ) ? void (0) : __assert_fail ("count_if(LoMask, [](int M) { return M >= 4; }) == 0 && \"Failed to lift all the high half inputs to the low mask!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15353, __extension__ __PRETTY_FUNCTION__)) | |||
15353 | "Failed to lift all the high half inputs to the low mask!")(static_cast <bool> (count_if(LoMask, [](int M) { return M >= 4; }) == 0 && "Failed to lift all the high half inputs to the low mask!" ) ? void (0) : __assert_fail ("count_if(LoMask, [](int M) { return M >= 4; }) == 0 && \"Failed to lift all the high half inputs to the low mask!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15353, __extension__ __PRETTY_FUNCTION__)); | |||
15354 | assert(count_if(HiMask, [](int M) { return M >= 0 && M < 4; }) == 0 &&(static_cast <bool> (count_if(HiMask, [](int M) { return M >= 0 && M < 4; }) == 0 && "Failed to lift all the low half inputs to the high mask!" ) ? void (0) : __assert_fail ("count_if(HiMask, [](int M) { return M >= 0 && M < 4; }) == 0 && \"Failed to lift all the low half inputs to the high mask!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15355, __extension__ __PRETTY_FUNCTION__)) | |||
15355 | "Failed to lift all the low half inputs to the high mask!")(static_cast <bool> (count_if(HiMask, [](int M) { return M >= 0 && M < 4; }) == 0 && "Failed to lift all the low half inputs to the high mask!" ) ? void (0) : __assert_fail ("count_if(HiMask, [](int M) { return M >= 0 && M < 4; }) == 0 && \"Failed to lift all the low half inputs to the high mask!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15355, __extension__ __PRETTY_FUNCTION__)); | |||
15356 | ||||
15357 | // Do a half shuffle for the low mask. | |||
15358 | if (!isNoopShuffleMask(LoMask)) | |||
15359 | V = DAG.getNode(X86ISD::PSHUFLW, DL, VT, V, | |||
15360 | getV4X86ShuffleImm8ForMask(LoMask, DL, DAG)); | |||
15361 | ||||
15362 | // Do a half shuffle with the high mask after shifting its values down. | |||
15363 | for (int &M : HiMask) | |||
15364 | if (M >= 0) | |||
15365 | M -= 4; | |||
15366 | if (!isNoopShuffleMask(HiMask)) | |||
15367 | V = DAG.getNode(X86ISD::PSHUFHW, DL, VT, V, | |||
15368 | getV4X86ShuffleImm8ForMask(HiMask, DL, DAG)); | |||
15369 | ||||
15370 | return V; | |||
15371 | } | |||
15372 | ||||
15373 | /// Helper to form a PSHUFB-based shuffle+blend, opportunistically avoiding the | |||
15374 | /// blend if only one input is used. | |||
15375 | static SDValue lowerShuffleAsBlendOfPSHUFBs( | |||
15376 | const SDLoc &DL, MVT VT, SDValue V1, SDValue V2, ArrayRef<int> Mask, | |||
15377 | const APInt &Zeroable, SelectionDAG &DAG, bool &V1InUse, bool &V2InUse) { | |||
15378 | assert(!is128BitLaneCrossingShuffleMask(VT, Mask) &&(static_cast <bool> (!is128BitLaneCrossingShuffleMask(VT , Mask) && "Lane crossing shuffle masks not supported" ) ? void (0) : __assert_fail ("!is128BitLaneCrossingShuffleMask(VT, Mask) && \"Lane crossing shuffle masks not supported\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15379, __extension__ __PRETTY_FUNCTION__)) | |||
15379 | "Lane crossing shuffle masks not supported")(static_cast <bool> (!is128BitLaneCrossingShuffleMask(VT , Mask) && "Lane crossing shuffle masks not supported" ) ? void (0) : __assert_fail ("!is128BitLaneCrossingShuffleMask(VT, Mask) && \"Lane crossing shuffle masks not supported\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15379, __extension__ __PRETTY_FUNCTION__)); | |||
15380 | ||||
15381 | int NumBytes = VT.getSizeInBits() / 8; | |||
15382 | int Size = Mask.size(); | |||
15383 | int Scale = NumBytes / Size; | |||
15384 | ||||
15385 | SmallVector<SDValue, 64> V1Mask(NumBytes, DAG.getUNDEF(MVT::i8)); | |||
15386 | SmallVector<SDValue, 64> V2Mask(NumBytes, DAG.getUNDEF(MVT::i8)); | |||
15387 | V1InUse = false; | |||
15388 | V2InUse = false; | |||
15389 | ||||
15390 | for (int i = 0; i < NumBytes; ++i) { | |||
15391 | int M = Mask[i / Scale]; | |||
15392 | if (M < 0) | |||
15393 | continue; | |||
15394 | ||||
15395 | const int ZeroMask = 0x80; | |||
15396 | int V1Idx = M < Size ? M * Scale + i % Scale : ZeroMask; | |||
15397 | int V2Idx = M < Size ? ZeroMask : (M - Size) * Scale + i % Scale; | |||
15398 | if (Zeroable[i / Scale]) | |||
15399 | V1Idx = V2Idx = ZeroMask; | |||
15400 | ||||
15401 | V1Mask[i] = DAG.getConstant(V1Idx, DL, MVT::i8); | |||
15402 | V2Mask[i] = DAG.getConstant(V2Idx, DL, MVT::i8); | |||
15403 | V1InUse |= (ZeroMask != V1Idx); | |||
15404 | V2InUse |= (ZeroMask != V2Idx); | |||
15405 | } | |||
15406 | ||||
15407 | MVT ShufVT = MVT::getVectorVT(MVT::i8, NumBytes); | |||
15408 | if (V1InUse) | |||
15409 | V1 = DAG.getNode(X86ISD::PSHUFB, DL, ShufVT, DAG.getBitcast(ShufVT, V1), | |||
15410 | DAG.getBuildVector(ShufVT, DL, V1Mask)); | |||
15411 | if (V2InUse) | |||
15412 | V2 = DAG.getNode(X86ISD::PSHUFB, DL, ShufVT, DAG.getBitcast(ShufVT, V2), | |||
15413 | DAG.getBuildVector(ShufVT, DL, V2Mask)); | |||
15414 | ||||
15415 | // If we need shuffled inputs from both, blend the two. | |||
15416 | SDValue V; | |||
15417 | if (V1InUse && V2InUse) | |||
15418 | V = DAG.getNode(ISD::OR, DL, ShufVT, V1, V2); | |||
15419 | else | |||
15420 | V = V1InUse ? V1 : V2; | |||
15421 | ||||
15422 | // Cast the result back to the correct type. | |||
15423 | return DAG.getBitcast(VT, V); | |||
15424 | } | |||
15425 | ||||
15426 | /// Generic lowering of 8-lane i16 shuffles. | |||
15427 | /// | |||
15428 | /// This handles both single-input shuffles and combined shuffle/blends with | |||
15429 | /// two inputs. The single input shuffles are immediately delegated to | |||
15430 | /// a dedicated lowering routine. | |||
15431 | /// | |||
15432 | /// The blends are lowered in one of three fundamental ways. If there are few | |||
15433 | /// enough inputs, it delegates to a basic UNPCK-based strategy. If the shuffle | |||
15434 | /// of the input is significantly cheaper when lowered as an interleaving of | |||
15435 | /// the two inputs, try to interleave them. Otherwise, blend the low and high | |||
15436 | /// halves of the inputs separately (making them have relatively few inputs) | |||
15437 | /// and then concatenate them. | |||
15438 | static SDValue lowerV8I16Shuffle(const SDLoc &DL, ArrayRef<int> Mask, | |||
15439 | const APInt &Zeroable, SDValue V1, SDValue V2, | |||
15440 | const X86Subtarget &Subtarget, | |||
15441 | SelectionDAG &DAG) { | |||
15442 | assert(V1.getSimpleValueType() == MVT::v8i16 && "Bad operand type!")(static_cast <bool> (V1.getSimpleValueType() == MVT::v8i16 && "Bad operand type!") ? void (0) : __assert_fail ( "V1.getSimpleValueType() == MVT::v8i16 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15442, __extension__ __PRETTY_FUNCTION__)); | |||
15443 | assert(V2.getSimpleValueType() == MVT::v8i16 && "Bad operand type!")(static_cast <bool> (V2.getSimpleValueType() == MVT::v8i16 && "Bad operand type!") ? void (0) : __assert_fail ( "V2.getSimpleValueType() == MVT::v8i16 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15443, __extension__ __PRETTY_FUNCTION__)); | |||
15444 | assert(Mask.size() == 8 && "Unexpected mask size for v8 shuffle!")(static_cast <bool> (Mask.size() == 8 && "Unexpected mask size for v8 shuffle!" ) ? void (0) : __assert_fail ("Mask.size() == 8 && \"Unexpected mask size for v8 shuffle!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15444, __extension__ __PRETTY_FUNCTION__)); | |||
15445 | ||||
15446 | // Whenever we can lower this as a zext, that instruction is strictly faster | |||
15447 | // than any alternative. | |||
15448 | if (SDValue ZExt = lowerShuffleAsZeroOrAnyExtend(DL, MVT::v8i16, V1, V2, Mask, | |||
15449 | Zeroable, Subtarget, DAG)) | |||
15450 | return ZExt; | |||
15451 | ||||
15452 | // Try to use lower using a truncation. | |||
15453 | if (SDValue V = lowerShuffleWithVPMOV(DL, MVT::v8i16, V1, V2, Mask, Zeroable, | |||
15454 | Subtarget, DAG)) | |||
15455 | return V; | |||
15456 | ||||
15457 | int NumV2Inputs = count_if(Mask, [](int M) { return M >= 8; }); | |||
15458 | ||||
15459 | if (NumV2Inputs == 0) { | |||
15460 | // Try to use shift instructions. | |||
15461 | if (SDValue Shift = lowerShuffleAsShift(DL, MVT::v8i16, V1, V1, Mask, | |||
15462 | Zeroable, Subtarget, DAG)) | |||
15463 | return Shift; | |||
15464 | ||||
15465 | // Check for being able to broadcast a single element. | |||
15466 | if (SDValue Broadcast = lowerShuffleAsBroadcast(DL, MVT::v8i16, V1, V2, | |||
15467 | Mask, Subtarget, DAG)) | |||
15468 | return Broadcast; | |||
15469 | ||||
15470 | // Try to use bit rotation instructions. | |||
15471 | if (SDValue Rotate = lowerShuffleAsBitRotate(DL, MVT::v8i16, V1, Mask, | |||
15472 | Subtarget, DAG)) | |||
15473 | return Rotate; | |||
15474 | ||||
15475 | // Use dedicated unpack instructions for masks that match their pattern. | |||
15476 | if (SDValue V = lowerShuffleWithUNPCK(DL, MVT::v8i16, Mask, V1, V2, DAG)) | |||
15477 | return V; | |||
15478 | ||||
15479 | // Use dedicated pack instructions for masks that match their pattern. | |||
15480 | if (SDValue V = lowerShuffleWithPACK(DL, MVT::v8i16, Mask, V1, V2, DAG, | |||
15481 | Subtarget)) | |||
15482 | return V; | |||
15483 | ||||
15484 | // Try to use byte rotation instructions. | |||
15485 | if (SDValue Rotate = lowerShuffleAsByteRotate(DL, MVT::v8i16, V1, V1, Mask, | |||
15486 | Subtarget, DAG)) | |||
15487 | return Rotate; | |||
15488 | ||||
15489 | // Make a copy of the mask so it can be modified. | |||
15490 | SmallVector<int, 8> MutableMask(Mask.begin(), Mask.end()); | |||
15491 | return lowerV8I16GeneralSingleInputShuffle(DL, MVT::v8i16, V1, MutableMask, | |||
15492 | Subtarget, DAG); | |||
15493 | } | |||
15494 | ||||
15495 | assert(llvm::any_of(Mask, [](int M) { return M >= 0 && M < 8; }) &&(static_cast <bool> (llvm::any_of(Mask, [](int M) { return M >= 0 && M < 8; }) && "All single-input shuffles should be canonicalized to be V1-input " "shuffles.") ? void (0) : __assert_fail ("llvm::any_of(Mask, [](int M) { return M >= 0 && M < 8; }) && \"All single-input shuffles should be canonicalized to be V1-input \" \"shuffles.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15497, __extension__ __PRETTY_FUNCTION__)) | |||
15496 | "All single-input shuffles should be canonicalized to be V1-input "(static_cast <bool> (llvm::any_of(Mask, [](int M) { return M >= 0 && M < 8; }) && "All single-input shuffles should be canonicalized to be V1-input " "shuffles.") ? void (0) : __assert_fail ("llvm::any_of(Mask, [](int M) { return M >= 0 && M < 8; }) && \"All single-input shuffles should be canonicalized to be V1-input \" \"shuffles.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15497, __extension__ __PRETTY_FUNCTION__)) | |||
15497 | "shuffles.")(static_cast <bool> (llvm::any_of(Mask, [](int M) { return M >= 0 && M < 8; }) && "All single-input shuffles should be canonicalized to be V1-input " "shuffles.") ? void (0) : __assert_fail ("llvm::any_of(Mask, [](int M) { return M >= 0 && M < 8; }) && \"All single-input shuffles should be canonicalized to be V1-input \" \"shuffles.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15497, __extension__ __PRETTY_FUNCTION__)); | |||
15498 | ||||
15499 | // Try to use shift instructions. | |||
15500 | if (SDValue Shift = lowerShuffleAsShift(DL, MVT::v8i16, V1, V2, Mask, | |||
15501 | Zeroable, Subtarget, DAG)) | |||
15502 | return Shift; | |||
15503 | ||||
15504 | // See if we can use SSE4A Extraction / Insertion. | |||
15505 | if (Subtarget.hasSSE4A()) | |||
15506 | if (SDValue V = lowerShuffleWithSSE4A(DL, MVT::v8i16, V1, V2, Mask, | |||
15507 | Zeroable, DAG)) | |||
15508 | return V; | |||
15509 | ||||
15510 | // There are special ways we can lower some single-element blends. | |||
15511 | if (NumV2Inputs == 1) | |||
15512 | if (SDValue V = lowerShuffleAsElementInsertion( | |||
15513 | DL, MVT::v8i16, V1, V2, Mask, Zeroable, Subtarget, DAG)) | |||
15514 | return V; | |||
15515 | ||||
15516 | // We have different paths for blend lowering, but they all must use the | |||
15517 | // *exact* same predicate. | |||
15518 | bool IsBlendSupported = Subtarget.hasSSE41(); | |||
15519 | if (IsBlendSupported) | |||
15520 | if (SDValue Blend = lowerShuffleAsBlend(DL, MVT::v8i16, V1, V2, Mask, | |||
15521 | Zeroable, Subtarget, DAG)) | |||
15522 | return Blend; | |||
15523 | ||||
15524 | if (SDValue Masked = lowerShuffleAsBitMask(DL, MVT::v8i16, V1, V2, Mask, | |||
15525 | Zeroable, Subtarget, DAG)) | |||
15526 | return Masked; | |||
15527 | ||||
15528 | // Use dedicated unpack instructions for masks that match their pattern. | |||
15529 | if (SDValue V = lowerShuffleWithUNPCK(DL, MVT::v8i16, Mask, V1, V2, DAG)) | |||
15530 | return V; | |||
15531 | ||||
15532 | // Use dedicated pack instructions for masks that match their pattern. | |||
15533 | if (SDValue V = lowerShuffleWithPACK(DL, MVT::v8i16, Mask, V1, V2, DAG, | |||
15534 | Subtarget)) | |||
15535 | return V; | |||
15536 | ||||
15537 | // Try to use lower using a truncation. | |||
15538 | if (SDValue V = lowerShuffleAsVTRUNC(DL, MVT::v8i16, V1, V2, Mask, Zeroable, | |||
15539 | Subtarget, DAG)) | |||
15540 | return V; | |||
15541 | ||||
15542 | // Try to use byte rotation instructions. | |||
15543 | if (SDValue Rotate = lowerShuffleAsByteRotate(DL, MVT::v8i16, V1, V2, Mask, | |||
15544 | Subtarget, DAG)) | |||
15545 | return Rotate; | |||
15546 | ||||
15547 | if (SDValue BitBlend = | |||
15548 | lowerShuffleAsBitBlend(DL, MVT::v8i16, V1, V2, Mask, DAG)) | |||
15549 | return BitBlend; | |||
15550 | ||||
15551 | // Try to use byte shift instructions to mask. | |||
15552 | if (SDValue V = lowerShuffleAsByteShiftMask(DL, MVT::v8i16, V1, V2, Mask, | |||
15553 | Zeroable, Subtarget, DAG)) | |||
15554 | return V; | |||
15555 | ||||
15556 | // Attempt to lower using compaction, SSE41 is necessary for PACKUSDW. | |||
15557 | // We could use SIGN_EXTEND_INREG+PACKSSDW for older targets but this seems to | |||
15558 | // be slower than a PSHUFLW+PSHUFHW+PSHUFD chain. | |||
15559 | int NumEvenDrops = canLowerByDroppingEvenElements(Mask, false); | |||
15560 | if ((NumEvenDrops == 1 || NumEvenDrops == 2) && Subtarget.hasSSE41() && | |||
15561 | !Subtarget.hasVLX()) { | |||
15562 | SmallVector<SDValue, 8> DWordClearOps(4, DAG.getConstant(0, DL, MVT::i32)); | |||
15563 | for (unsigned i = 0; i != 4; i += 1 << (NumEvenDrops - 1)) | |||
15564 | DWordClearOps[i] = DAG.getConstant(0xFFFF, DL, MVT::i32); | |||
15565 | SDValue DWordClearMask = DAG.getBuildVector(MVT::v4i32, DL, DWordClearOps); | |||
15566 | V1 = DAG.getNode(ISD::AND, DL, MVT::v4i32, DAG.getBitcast(MVT::v4i32, V1), | |||
15567 | DWordClearMask); | |||
15568 | V2 = DAG.getNode(ISD::AND, DL, MVT::v4i32, DAG.getBitcast(MVT::v4i32, V2), | |||
15569 | DWordClearMask); | |||
15570 | // Now pack things back together. | |||
15571 | SDValue Result = DAG.getNode(X86ISD::PACKUS, DL, MVT::v8i16, V1, V2); | |||
15572 | if (NumEvenDrops == 2) { | |||
15573 | Result = DAG.getBitcast(MVT::v4i32, Result); | |||
15574 | Result = DAG.getNode(X86ISD::PACKUS, DL, MVT::v8i16, Result, Result); | |||
15575 | } | |||
15576 | return Result; | |||
15577 | } | |||
15578 | ||||
15579 | // Try to lower by permuting the inputs into an unpack instruction. | |||
15580 | if (SDValue Unpack = lowerShuffleAsPermuteAndUnpack(DL, MVT::v8i16, V1, V2, | |||
15581 | Mask, Subtarget, DAG)) | |||
15582 | return Unpack; | |||
15583 | ||||
15584 | // If we can't directly blend but can use PSHUFB, that will be better as it | |||
15585 | // can both shuffle and set up the inefficient blend. | |||
15586 | if (!IsBlendSupported && Subtarget.hasSSSE3()) { | |||
15587 | bool V1InUse, V2InUse; | |||
15588 | return lowerShuffleAsBlendOfPSHUFBs(DL, MVT::v8i16, V1, V2, Mask, | |||
15589 | Zeroable, DAG, V1InUse, V2InUse); | |||
15590 | } | |||
15591 | ||||
15592 | // We can always bit-blend if we have to so the fallback strategy is to | |||
15593 | // decompose into single-input permutes and blends/unpacks. | |||
15594 | return lowerShuffleAsDecomposedShuffleMerge(DL, MVT::v8i16, V1, V2, | |||
15595 | Mask, Subtarget, DAG); | |||
15596 | } | |||
15597 | ||||
15598 | /// Lower 8-lane 16-bit floating point shuffles. | |||
15599 | static SDValue lowerV8F16Shuffle(const SDLoc &DL, ArrayRef<int> Mask, | |||
15600 | const APInt &Zeroable, SDValue V1, SDValue V2, | |||
15601 | const X86Subtarget &Subtarget, | |||
15602 | SelectionDAG &DAG) { | |||
15603 | assert(V1.getSimpleValueType() == MVT::v8f16 && "Bad operand type!")(static_cast <bool> (V1.getSimpleValueType() == MVT::v8f16 && "Bad operand type!") ? void (0) : __assert_fail ( "V1.getSimpleValueType() == MVT::v8f16 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15603, __extension__ __PRETTY_FUNCTION__)); | |||
15604 | assert(V2.getSimpleValueType() == MVT::v8f16 && "Bad operand type!")(static_cast <bool> (V2.getSimpleValueType() == MVT::v8f16 && "Bad operand type!") ? void (0) : __assert_fail ( "V2.getSimpleValueType() == MVT::v8f16 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15604, __extension__ __PRETTY_FUNCTION__)); | |||
15605 | assert(Mask.size() == 8 && "Unexpected mask size for v8 shuffle!")(static_cast <bool> (Mask.size() == 8 && "Unexpected mask size for v8 shuffle!" ) ? void (0) : __assert_fail ("Mask.size() == 8 && \"Unexpected mask size for v8 shuffle!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15605, __extension__ __PRETTY_FUNCTION__)); | |||
15606 | int NumV2Elements = count_if(Mask, [](int M) { return M >= 8; }); | |||
15607 | ||||
15608 | if (NumV2Elements == 0) { | |||
15609 | // Check for being able to broadcast a single element. | |||
15610 | if (SDValue Broadcast = lowerShuffleAsBroadcast(DL, MVT::v8f16, V1, V2, | |||
15611 | Mask, Subtarget, DAG)) | |||
15612 | return Broadcast; | |||
15613 | } | |||
15614 | if (NumV2Elements == 1 && Mask[0] >= 8) | |||
15615 | if (SDValue V = lowerShuffleAsElementInsertion(DL, MVT::v8f16, V1, V2, Mask, | |||
15616 | Zeroable, Subtarget, DAG)) | |||
15617 | return V; | |||
15618 | ||||
15619 | V1 = DAG.getBitcast(MVT::v8i16, V1); | |||
15620 | V2 = DAG.getBitcast(MVT::v8i16, V2); | |||
15621 | return DAG.getBitcast(MVT::v8f16, | |||
15622 | DAG.getVectorShuffle(MVT::v8i16, DL, V1, V2, Mask)); | |||
15623 | } | |||
15624 | ||||
15625 | // Lowers unary/binary shuffle as VPERMV/VPERMV3, for non-VLX targets, | |||
15626 | // sub-512-bit shuffles are padded to 512-bits for the shuffle and then | |||
15627 | // the active subvector is extracted. | |||
15628 | static SDValue lowerShuffleWithPERMV(const SDLoc &DL, MVT VT, | |||
15629 | ArrayRef<int> Mask, SDValue V1, SDValue V2, | |||
15630 | const X86Subtarget &Subtarget, | |||
15631 | SelectionDAG &DAG) { | |||
15632 | MVT MaskVT = VT.changeTypeToInteger(); | |||
15633 | SDValue MaskNode; | |||
15634 | MVT ShuffleVT = VT; | |||
15635 | if (!VT.is512BitVector() && !Subtarget.hasVLX()) { | |||
15636 | V1 = widenSubVector(V1, false, Subtarget, DAG, DL, 512); | |||
15637 | V2 = widenSubVector(V2, false, Subtarget, DAG, DL, 512); | |||
15638 | ShuffleVT = V1.getSimpleValueType(); | |||
15639 | ||||
15640 | // Adjust mask to correct indices for the second input. | |||
15641 | int NumElts = VT.getVectorNumElements(); | |||
15642 | unsigned Scale = 512 / VT.getSizeInBits(); | |||
15643 | SmallVector<int, 32> AdjustedMask(Mask.begin(), Mask.end()); | |||
15644 | for (int &M : AdjustedMask) | |||
15645 | if (NumElts <= M) | |||
15646 | M += (Scale - 1) * NumElts; | |||
15647 | MaskNode = getConstVector(AdjustedMask, MaskVT, DAG, DL, true); | |||
15648 | MaskNode = widenSubVector(MaskNode, false, Subtarget, DAG, DL, 512); | |||
15649 | } else { | |||
15650 | MaskNode = getConstVector(Mask, MaskVT, DAG, DL, true); | |||
15651 | } | |||
15652 | ||||
15653 | SDValue Result; | |||
15654 | if (V2.isUndef()) | |||
15655 | Result = DAG.getNode(X86ISD::VPERMV, DL, ShuffleVT, MaskNode, V1); | |||
15656 | else | |||
15657 | Result = DAG.getNode(X86ISD::VPERMV3, DL, ShuffleVT, V1, MaskNode, V2); | |||
15658 | ||||
15659 | if (VT != ShuffleVT) | |||
15660 | Result = extractSubVector(Result, 0, DAG, DL, VT.getSizeInBits()); | |||
15661 | ||||
15662 | return Result; | |||
15663 | } | |||
15664 | ||||
15665 | /// Generic lowering of v16i8 shuffles. | |||
15666 | /// | |||
15667 | /// This is a hybrid strategy to lower v16i8 vectors. It first attempts to | |||
15668 | /// detect any complexity reducing interleaving. If that doesn't help, it uses | |||
15669 | /// UNPCK to spread the i8 elements across two i16-element vectors, and uses | |||
15670 | /// the existing lowering for v8i16 blends on each half, finally PACK-ing them | |||
15671 | /// back together. | |||
15672 | static SDValue lowerV16I8Shuffle(const SDLoc &DL, ArrayRef<int> Mask, | |||
15673 | const APInt &Zeroable, SDValue V1, SDValue V2, | |||
15674 | const X86Subtarget &Subtarget, | |||
15675 | SelectionDAG &DAG) { | |||
15676 | assert(V1.getSimpleValueType() == MVT::v16i8 && "Bad operand type!")(static_cast <bool> (V1.getSimpleValueType() == MVT::v16i8 && "Bad operand type!") ? void (0) : __assert_fail ( "V1.getSimpleValueType() == MVT::v16i8 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15676, __extension__ __PRETTY_FUNCTION__)); | |||
15677 | assert(V2.getSimpleValueType() == MVT::v16i8 && "Bad operand type!")(static_cast <bool> (V2.getSimpleValueType() == MVT::v16i8 && "Bad operand type!") ? void (0) : __assert_fail ( "V2.getSimpleValueType() == MVT::v16i8 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15677, __extension__ __PRETTY_FUNCTION__)); | |||
15678 | assert(Mask.size() == 16 && "Unexpected mask size for v16 shuffle!")(static_cast <bool> (Mask.size() == 16 && "Unexpected mask size for v16 shuffle!" ) ? void (0) : __assert_fail ("Mask.size() == 16 && \"Unexpected mask size for v16 shuffle!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15678, __extension__ __PRETTY_FUNCTION__)); | |||
15679 | ||||
15680 | // Try to use shift instructions. | |||
15681 | if (SDValue Shift = lowerShuffleAsShift(DL, MVT::v16i8, V1, V2, Mask, | |||
15682 | Zeroable, Subtarget, DAG)) | |||
15683 | return Shift; | |||
15684 | ||||
15685 | // Try to use byte rotation instructions. | |||
15686 | if (SDValue Rotate = lowerShuffleAsByteRotate(DL, MVT::v16i8, V1, V2, Mask, | |||
15687 | Subtarget, DAG)) | |||
15688 | return Rotate; | |||
15689 | ||||
15690 | // Use dedicated pack instructions for masks that match their pattern. | |||
15691 | if (SDValue V = lowerShuffleWithPACK(DL, MVT::v16i8, Mask, V1, V2, DAG, | |||
15692 | Subtarget)) | |||
15693 | return V; | |||
15694 | ||||
15695 | // Try to use a zext lowering. | |||
15696 | if (SDValue ZExt = lowerShuffleAsZeroOrAnyExtend(DL, MVT::v16i8, V1, V2, Mask, | |||
15697 | Zeroable, Subtarget, DAG)) | |||
15698 | return ZExt; | |||
15699 | ||||
15700 | // Try to use lower using a truncation. | |||
15701 | if (SDValue V = lowerShuffleWithVPMOV(DL, MVT::v16i8, V1, V2, Mask, Zeroable, | |||
15702 | Subtarget, DAG)) | |||
15703 | return V; | |||
15704 | ||||
15705 | if (SDValue V = lowerShuffleAsVTRUNC(DL, MVT::v16i8, V1, V2, Mask, Zeroable, | |||
15706 | Subtarget, DAG)) | |||
15707 | return V; | |||
15708 | ||||
15709 | // See if we can use SSE4A Extraction / Insertion. | |||
15710 | if (Subtarget.hasSSE4A()) | |||
15711 | if (SDValue V = lowerShuffleWithSSE4A(DL, MVT::v16i8, V1, V2, Mask, | |||
15712 | Zeroable, DAG)) | |||
15713 | return V; | |||
15714 | ||||
15715 | int NumV2Elements = count_if(Mask, [](int M) { return M >= 16; }); | |||
15716 | ||||
15717 | // For single-input shuffles, there are some nicer lowering tricks we can use. | |||
15718 | if (NumV2Elements == 0) { | |||
15719 | // Check for being able to broadcast a single element. | |||
15720 | if (SDValue Broadcast = lowerShuffleAsBroadcast(DL, MVT::v16i8, V1, V2, | |||
15721 | Mask, Subtarget, DAG)) | |||
15722 | return Broadcast; | |||
15723 | ||||
15724 | // Try to use bit rotation instructions. | |||
15725 | if (SDValue Rotate = lowerShuffleAsBitRotate(DL, MVT::v16i8, V1, Mask, | |||
15726 | Subtarget, DAG)) | |||
15727 | return Rotate; | |||
15728 | ||||
15729 | if (SDValue V = lowerShuffleWithUNPCK(DL, MVT::v16i8, Mask, V1, V2, DAG)) | |||
15730 | return V; | |||
15731 | ||||
15732 | // Check whether we can widen this to an i16 shuffle by duplicating bytes. | |||
15733 | // Notably, this handles splat and partial-splat shuffles more efficiently. | |||
15734 | // However, it only makes sense if the pre-duplication shuffle simplifies | |||
15735 | // things significantly. Currently, this means we need to be able to | |||
15736 | // express the pre-duplication shuffle as an i16 shuffle. | |||
15737 | // | |||
15738 | // FIXME: We should check for other patterns which can be widened into an | |||
15739 | // i16 shuffle as well. | |||
15740 | auto canWidenViaDuplication = [](ArrayRef<int> Mask) { | |||
15741 | for (int i = 0; i < 16; i += 2) | |||
15742 | if (Mask[i] >= 0 && Mask[i + 1] >= 0 && Mask[i] != Mask[i + 1]) | |||
15743 | return false; | |||
15744 | ||||
15745 | return true; | |||
15746 | }; | |||
15747 | auto tryToWidenViaDuplication = [&]() -> SDValue { | |||
15748 | if (!canWidenViaDuplication(Mask)) | |||
15749 | return SDValue(); | |||
15750 | SmallVector<int, 4> LoInputs; | |||
15751 | copy_if(Mask, std::back_inserter(LoInputs), | |||
15752 | [](int M) { return M >= 0 && M < 8; }); | |||
15753 | array_pod_sort(LoInputs.begin(), LoInputs.end()); | |||
15754 | LoInputs.erase(std::unique(LoInputs.begin(), LoInputs.end()), | |||
15755 | LoInputs.end()); | |||
15756 | SmallVector<int, 4> HiInputs; | |||
15757 | copy_if(Mask, std::back_inserter(HiInputs), [](int M) { return M >= 8; }); | |||
15758 | array_pod_sort(HiInputs.begin(), HiInputs.end()); | |||
15759 | HiInputs.erase(std::unique(HiInputs.begin(), HiInputs.end()), | |||
15760 | HiInputs.end()); | |||
15761 | ||||
15762 | bool TargetLo = LoInputs.size() >= HiInputs.size(); | |||
15763 | ArrayRef<int> InPlaceInputs = TargetLo ? LoInputs : HiInputs; | |||
15764 | ArrayRef<int> MovingInputs = TargetLo ? HiInputs : LoInputs; | |||
15765 | ||||
15766 | int PreDupI16Shuffle[] = {-1, -1, -1, -1, -1, -1, -1, -1}; | |||
15767 | SmallDenseMap<int, int, 8> LaneMap; | |||
15768 | for (int I : InPlaceInputs) { | |||
15769 | PreDupI16Shuffle[I/2] = I/2; | |||
15770 | LaneMap[I] = I; | |||
15771 | } | |||
15772 | int j = TargetLo ? 0 : 4, je = j + 4; | |||
15773 | for (int i = 0, ie = MovingInputs.size(); i < ie; ++i) { | |||
15774 | // Check if j is already a shuffle of this input. This happens when | |||
15775 | // there are two adjacent bytes after we move the low one. | |||
15776 | if (PreDupI16Shuffle[j] != MovingInputs[i] / 2) { | |||
15777 | // If we haven't yet mapped the input, search for a slot into which | |||
15778 | // we can map it. | |||
15779 | while (j < je && PreDupI16Shuffle[j] >= 0) | |||
15780 | ++j; | |||
15781 | ||||
15782 | if (j == je) | |||
15783 | // We can't place the inputs into a single half with a simple i16 shuffle, so bail. | |||
15784 | return SDValue(); | |||
15785 | ||||
15786 | // Map this input with the i16 shuffle. | |||
15787 | PreDupI16Shuffle[j] = MovingInputs[i] / 2; | |||
15788 | } | |||
15789 | ||||
15790 | // Update the lane map based on the mapping we ended up with. | |||
15791 | LaneMap[MovingInputs[i]] = 2 * j + MovingInputs[i] % 2; | |||
15792 | } | |||
15793 | V1 = DAG.getBitcast( | |||
15794 | MVT::v16i8, | |||
15795 | DAG.getVectorShuffle(MVT::v8i16, DL, DAG.getBitcast(MVT::v8i16, V1), | |||
15796 | DAG.getUNDEF(MVT::v8i16), PreDupI16Shuffle)); | |||
15797 | ||||
15798 | // Unpack the bytes to form the i16s that will be shuffled into place. | |||
15799 | bool EvenInUse = false, OddInUse = false; | |||
15800 | for (int i = 0; i < 16; i += 2) { | |||
15801 | EvenInUse |= (Mask[i + 0] >= 0); | |||
15802 | OddInUse |= (Mask[i + 1] >= 0); | |||
15803 | if (EvenInUse && OddInUse) | |||
15804 | break; | |||
15805 | } | |||
15806 | V1 = DAG.getNode(TargetLo ? X86ISD::UNPCKL : X86ISD::UNPCKH, DL, | |||
15807 | MVT::v16i8, EvenInUse ? V1 : DAG.getUNDEF(MVT::v16i8), | |||
15808 | OddInUse ? V1 : DAG.getUNDEF(MVT::v16i8)); | |||
15809 | ||||
15810 | int PostDupI16Shuffle[8] = {-1, -1, -1, -1, -1, -1, -1, -1}; | |||
15811 | for (int i = 0; i < 16; ++i) | |||
15812 | if (Mask[i] >= 0) { | |||
15813 | int MappedMask = LaneMap[Mask[i]] - (TargetLo ? 0 : 8); | |||
15814 | assert(MappedMask < 8 && "Invalid v8 shuffle mask!")(static_cast <bool> (MappedMask < 8 && "Invalid v8 shuffle mask!" ) ? void (0) : __assert_fail ("MappedMask < 8 && \"Invalid v8 shuffle mask!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15814, __extension__ __PRETTY_FUNCTION__)); | |||
15815 | if (PostDupI16Shuffle[i / 2] < 0) | |||
15816 | PostDupI16Shuffle[i / 2] = MappedMask; | |||
15817 | else | |||
15818 | assert(PostDupI16Shuffle[i / 2] == MappedMask &&(static_cast <bool> (PostDupI16Shuffle[i / 2] == MappedMask && "Conflicting entries in the original shuffle!") ? void (0) : __assert_fail ("PostDupI16Shuffle[i / 2] == MappedMask && \"Conflicting entries in the original shuffle!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15819, __extension__ __PRETTY_FUNCTION__)) | |||
15819 | "Conflicting entries in the original shuffle!")(static_cast <bool> (PostDupI16Shuffle[i / 2] == MappedMask && "Conflicting entries in the original shuffle!") ? void (0) : __assert_fail ("PostDupI16Shuffle[i / 2] == MappedMask && \"Conflicting entries in the original shuffle!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15819, __extension__ __PRETTY_FUNCTION__)); | |||
15820 | } | |||
15821 | return DAG.getBitcast( | |||
15822 | MVT::v16i8, | |||
15823 | DAG.getVectorShuffle(MVT::v8i16, DL, DAG.getBitcast(MVT::v8i16, V1), | |||
15824 | DAG.getUNDEF(MVT::v8i16), PostDupI16Shuffle)); | |||
15825 | }; | |||
15826 | if (SDValue V = tryToWidenViaDuplication()) | |||
15827 | return V; | |||
15828 | } | |||
15829 | ||||
15830 | if (SDValue Masked = lowerShuffleAsBitMask(DL, MVT::v16i8, V1, V2, Mask, | |||
15831 | Zeroable, Subtarget, DAG)) | |||
15832 | return Masked; | |||
15833 | ||||
15834 | // Use dedicated unpack instructions for masks that match their pattern. | |||
15835 | if (SDValue V = lowerShuffleWithUNPCK(DL, MVT::v16i8, Mask, V1, V2, DAG)) | |||
15836 | return V; | |||
15837 | ||||
15838 | // Try to use byte shift instructions to mask. | |||
15839 | if (SDValue V = lowerShuffleAsByteShiftMask(DL, MVT::v16i8, V1, V2, Mask, | |||
15840 | Zeroable, Subtarget, DAG)) | |||
15841 | return V; | |||
15842 | ||||
15843 | // Check for compaction patterns. | |||
15844 | bool IsSingleInput = V2.isUndef(); | |||
15845 | int NumEvenDrops = canLowerByDroppingEvenElements(Mask, IsSingleInput); | |||
15846 | ||||
15847 | // Check for SSSE3 which lets us lower all v16i8 shuffles much more directly | |||
15848 | // with PSHUFB. It is important to do this before we attempt to generate any | |||
15849 | // blends but after all of the single-input lowerings. If the single input | |||
15850 | // lowerings can find an instruction sequence that is faster than a PSHUFB, we | |||
15851 | // want to preserve that and we can DAG combine any longer sequences into | |||
15852 | // a PSHUFB in the end. But once we start blending from multiple inputs, | |||
15853 | // the complexity of DAG combining bad patterns back into PSHUFB is too high, | |||
15854 | // and there are *very* few patterns that would actually be faster than the | |||
15855 | // PSHUFB approach because of its ability to zero lanes. | |||
15856 | // | |||
15857 | // If the mask is a binary compaction, we can more efficiently perform this | |||
15858 | // as a PACKUS(AND(),AND()) - which is quicker than UNPACK(PSHUFB(),PSHUFB()). | |||
15859 | // | |||
15860 | // FIXME: The only exceptions to the above are blends which are exact | |||
15861 | // interleavings with direct instructions supporting them. We currently don't | |||
15862 | // handle those well here. | |||
15863 | if (Subtarget.hasSSSE3() && (IsSingleInput || NumEvenDrops != 1)) { | |||
15864 | bool V1InUse = false; | |||
15865 | bool V2InUse = false; | |||
15866 | ||||
15867 | SDValue PSHUFB = lowerShuffleAsBlendOfPSHUFBs( | |||
15868 | DL, MVT::v16i8, V1, V2, Mask, Zeroable, DAG, V1InUse, V2InUse); | |||
15869 | ||||
15870 | // If both V1 and V2 are in use and we can use a direct blend or an unpack, | |||
15871 | // do so. This avoids using them to handle blends-with-zero which is | |||
15872 | // important as a single pshufb is significantly faster for that. | |||
15873 | if (V1InUse && V2InUse) { | |||
15874 | if (Subtarget.hasSSE41()) | |||
15875 | if (SDValue Blend = lowerShuffleAsBlend(DL, MVT::v16i8, V1, V2, Mask, | |||
15876 | Zeroable, Subtarget, DAG)) | |||
15877 | return Blend; | |||
15878 | ||||
15879 | // We can use an unpack to do the blending rather than an or in some | |||
15880 | // cases. Even though the or may be (very minorly) more efficient, we | |||
15881 | // preference this lowering because there are common cases where part of | |||
15882 | // the complexity of the shuffles goes away when we do the final blend as | |||
15883 | // an unpack. | |||
15884 | // FIXME: It might be worth trying to detect if the unpack-feeding | |||
15885 | // shuffles will both be pshufb, in which case we shouldn't bother with | |||
15886 | // this. | |||
15887 | if (SDValue Unpack = lowerShuffleAsPermuteAndUnpack( | |||
15888 | DL, MVT::v16i8, V1, V2, Mask, Subtarget, DAG)) | |||
15889 | return Unpack; | |||
15890 | ||||
15891 | // AVX512VBMI can lower to VPERMB (non-VLX will pad to v64i8). | |||
15892 | if (Subtarget.hasVBMI()) | |||
15893 | return lowerShuffleWithPERMV(DL, MVT::v16i8, Mask, V1, V2, Subtarget, | |||
15894 | DAG); | |||
15895 | ||||
15896 | // If we have XOP we can use one VPPERM instead of multiple PSHUFBs. | |||
15897 | if (Subtarget.hasXOP()) { | |||
15898 | SDValue MaskNode = getConstVector(Mask, MVT::v16i8, DAG, DL, true); | |||
15899 | return DAG.getNode(X86ISD::VPPERM, DL, MVT::v16i8, V1, V2, MaskNode); | |||
15900 | } | |||
15901 | ||||
15902 | // Use PALIGNR+Permute if possible - permute might become PSHUFB but the | |||
15903 | // PALIGNR will be cheaper than the second PSHUFB+OR. | |||
15904 | if (SDValue V = lowerShuffleAsByteRotateAndPermute( | |||
15905 | DL, MVT::v16i8, V1, V2, Mask, Subtarget, DAG)) | |||
15906 | return V; | |||
15907 | } | |||
15908 | ||||
15909 | return PSHUFB; | |||
15910 | } | |||
15911 | ||||
15912 | // There are special ways we can lower some single-element blends. | |||
15913 | if (NumV2Elements == 1) | |||
15914 | if (SDValue V = lowerShuffleAsElementInsertion( | |||
15915 | DL, MVT::v16i8, V1, V2, Mask, Zeroable, Subtarget, DAG)) | |||
15916 | return V; | |||
15917 | ||||
15918 | if (SDValue Blend = lowerShuffleAsBitBlend(DL, MVT::v16i8, V1, V2, Mask, DAG)) | |||
15919 | return Blend; | |||
15920 | ||||
15921 | // Check whether a compaction lowering can be done. This handles shuffles | |||
15922 | // which take every Nth element for some even N. See the helper function for | |||
15923 | // details. | |||
15924 | // | |||
15925 | // We special case these as they can be particularly efficiently handled with | |||
15926 | // the PACKUSB instruction on x86 and they show up in common patterns of | |||
15927 | // rearranging bytes to truncate wide elements. | |||
15928 | if (NumEvenDrops) { | |||
15929 | // NumEvenDrops is the power of two stride of the elements. Another way of | |||
15930 | // thinking about it is that we need to drop the even elements this many | |||
15931 | // times to get the original input. | |||
15932 | ||||
15933 | // First we need to zero all the dropped bytes. | |||
15934 | assert(NumEvenDrops <= 3 &&(static_cast <bool> (NumEvenDrops <= 3 && "No support for dropping even elements more than 3 times." ) ? void (0) : __assert_fail ("NumEvenDrops <= 3 && \"No support for dropping even elements more than 3 times.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15935, __extension__ __PRETTY_FUNCTION__)) | |||
15935 | "No support for dropping even elements more than 3 times.")(static_cast <bool> (NumEvenDrops <= 3 && "No support for dropping even elements more than 3 times." ) ? void (0) : __assert_fail ("NumEvenDrops <= 3 && \"No support for dropping even elements more than 3 times.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 15935, __extension__ __PRETTY_FUNCTION__)); | |||
15936 | SmallVector<SDValue, 8> WordClearOps(8, DAG.getConstant(0, DL, MVT::i16)); | |||
15937 | for (unsigned i = 0; i != 8; i += 1 << (NumEvenDrops - 1)) | |||
15938 | WordClearOps[i] = DAG.getConstant(0xFF, DL, MVT::i16); | |||
15939 | SDValue WordClearMask = DAG.getBuildVector(MVT::v8i16, DL, WordClearOps); | |||
15940 | V1 = DAG.getNode(ISD::AND, DL, MVT::v8i16, DAG.getBitcast(MVT::v8i16, V1), | |||
15941 | WordClearMask); | |||
15942 | if (!IsSingleInput) | |||
15943 | V2 = DAG.getNode(ISD::AND, DL, MVT::v8i16, DAG.getBitcast(MVT::v8i16, V2), | |||
15944 | WordClearMask); | |||
15945 | ||||
15946 | // Now pack things back together. | |||
15947 | SDValue Result = DAG.getNode(X86ISD::PACKUS, DL, MVT::v16i8, V1, | |||
15948 | IsSingleInput ? V1 : V2); | |||
15949 | for (int i = 1; i < NumEvenDrops; ++i) { | |||
15950 | Result = DAG.getBitcast(MVT::v8i16, Result); | |||
15951 | Result = DAG.getNode(X86ISD::PACKUS, DL, MVT::v16i8, Result, Result); | |||
15952 | } | |||
15953 | return Result; | |||
15954 | } | |||
15955 | ||||
15956 | // Handle multi-input cases by blending/unpacking single-input shuffles. | |||
15957 | if (NumV2Elements > 0) | |||
15958 | return lowerShuffleAsDecomposedShuffleMerge(DL, MVT::v16i8, V1, V2, Mask, | |||
15959 | Subtarget, DAG); | |||
15960 | ||||
15961 | // The fallback path for single-input shuffles widens this into two v8i16 | |||
15962 | // vectors with unpacks, shuffles those, and then pulls them back together | |||
15963 | // with a pack. | |||
15964 | SDValue V = V1; | |||
15965 | ||||
15966 | std::array<int, 8> LoBlendMask = {{-1, -1, -1, -1, -1, -1, -1, -1}}; | |||
15967 | std::array<int, 8> HiBlendMask = {{-1, -1, -1, -1, -1, -1, -1, -1}}; | |||
15968 | for (int i = 0; i < 16; ++i) | |||
15969 | if (Mask[i] >= 0) | |||
15970 | (i < 8 ? LoBlendMask[i] : HiBlendMask[i % 8]) = Mask[i]; | |||
15971 | ||||
15972 | SDValue VLoHalf, VHiHalf; | |||
15973 | // Check if any of the odd lanes in the v16i8 are used. If not, we can mask | |||
15974 | // them out and avoid using UNPCK{L,H} to extract the elements of V as | |||
15975 | // i16s. | |||
15976 | if (none_of(LoBlendMask, [](int M) { return M >= 0 && M % 2 == 1; }) && | |||
15977 | none_of(HiBlendMask, [](int M) { return M >= 0 && M % 2 == 1; })) { | |||
15978 | // Use a mask to drop the high bytes. | |||
15979 | VLoHalf = DAG.getBitcast(MVT::v8i16, V); | |||
15980 | VLoHalf = DAG.getNode(ISD::AND, DL, MVT::v8i16, VLoHalf, | |||
15981 | DAG.getConstant(0x00FF, DL, MVT::v8i16)); | |||
15982 | ||||
15983 | // This will be a single vector shuffle instead of a blend so nuke VHiHalf. | |||
15984 | VHiHalf = DAG.getUNDEF(MVT::v8i16); | |||
15985 | ||||
15986 | // Squash the masks to point directly into VLoHalf. | |||
15987 | for (int &M : LoBlendMask) | |||
15988 | if (M >= 0) | |||
15989 | M /= 2; | |||
15990 | for (int &M : HiBlendMask) | |||
15991 | if (M >= 0) | |||
15992 | M /= 2; | |||
15993 | } else { | |||
15994 | // Otherwise just unpack the low half of V into VLoHalf and the high half into | |||
15995 | // VHiHalf so that we can blend them as i16s. | |||
15996 | SDValue Zero = getZeroVector(MVT::v16i8, Subtarget, DAG, DL); | |||
15997 | ||||
15998 | VLoHalf = DAG.getBitcast( | |||
15999 | MVT::v8i16, DAG.getNode(X86ISD::UNPCKL, DL, MVT::v16i8, V, Zero)); | |||
16000 | VHiHalf = DAG.getBitcast( | |||
16001 | MVT::v8i16, DAG.getNode(X86ISD::UNPCKH, DL, MVT::v16i8, V, Zero)); | |||
16002 | } | |||
16003 | ||||
16004 | SDValue LoV = DAG.getVectorShuffle(MVT::v8i16, DL, VLoHalf, VHiHalf, LoBlendMask); | |||
16005 | SDValue HiV = DAG.getVectorShuffle(MVT::v8i16, DL, VLoHalf, VHiHalf, HiBlendMask); | |||
16006 | ||||
16007 | return DAG.getNode(X86ISD::PACKUS, DL, MVT::v16i8, LoV, HiV); | |||
16008 | } | |||
16009 | ||||
16010 | /// Dispatching routine to lower various 128-bit x86 vector shuffles. | |||
16011 | /// | |||
16012 | /// This routine breaks down the specific type of 128-bit shuffle and | |||
16013 | /// dispatches to the lowering routines accordingly. | |||
16014 | static SDValue lower128BitShuffle(const SDLoc &DL, ArrayRef<int> Mask, | |||
16015 | MVT VT, SDValue V1, SDValue V2, | |||
16016 | const APInt &Zeroable, | |||
16017 | const X86Subtarget &Subtarget, | |||
16018 | SelectionDAG &DAG) { | |||
16019 | switch (VT.SimpleTy) { | |||
16020 | case MVT::v2i64: | |||
16021 | return lowerV2I64Shuffle(DL, Mask, Zeroable, V1, V2, Subtarget, DAG); | |||
16022 | case MVT::v2f64: | |||
16023 | return lowerV2F64Shuffle(DL, Mask, Zeroable, V1, V2, Subtarget, DAG); | |||
16024 | case MVT::v4i32: | |||
16025 | return lowerV4I32Shuffle(DL, Mask, Zeroable, V1, V2, Subtarget, DAG); | |||
16026 | case MVT::v4f32: | |||
16027 | return lowerV4F32Shuffle(DL, Mask, Zeroable, V1, V2, Subtarget, DAG); | |||
16028 | case MVT::v8i16: | |||
16029 | return lowerV8I16Shuffle(DL, Mask, Zeroable, V1, V2, Subtarget, DAG); | |||
16030 | case MVT::v8f16: | |||
16031 | return lowerV8F16Shuffle(DL, Mask, Zeroable, V1, V2, Subtarget, DAG); | |||
16032 | case MVT::v16i8: | |||
16033 | return lowerV16I8Shuffle(DL, Mask, Zeroable, V1, V2, Subtarget, DAG); | |||
16034 | ||||
16035 | default: | |||
16036 | llvm_unreachable("Unimplemented!")::llvm::llvm_unreachable_internal("Unimplemented!", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 16036); | |||
16037 | } | |||
16038 | } | |||
16039 | ||||
16040 | /// Generic routine to split vector shuffle into half-sized shuffles. | |||
16041 | /// | |||
16042 | /// This routine just extracts two subvectors, shuffles them independently, and | |||
16043 | /// then concatenates them back together. This should work effectively with all | |||
16044 | /// AVX vector shuffle types. | |||
16045 | static SDValue splitAndLowerShuffle(const SDLoc &DL, MVT VT, SDValue V1, | |||
16046 | SDValue V2, ArrayRef<int> Mask, | |||
16047 | SelectionDAG &DAG) { | |||
16048 | assert(VT.getSizeInBits() >= 256 &&(static_cast <bool> (VT.getSizeInBits() >= 256 && "Only for 256-bit or wider vector shuffles!") ? void (0) : __assert_fail ("VT.getSizeInBits() >= 256 && \"Only for 256-bit or wider vector shuffles!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 16049, __extension__ __PRETTY_FUNCTION__)) | |||
16049 | "Only for 256-bit or wider vector shuffles!")(static_cast <bool> (VT.getSizeInBits() >= 256 && "Only for 256-bit or wider vector shuffles!") ? void (0) : __assert_fail ("VT.getSizeInBits() >= 256 && \"Only for 256-bit or wider vector shuffles!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 16049, __extension__ __PRETTY_FUNCTION__)); | |||
16050 | assert(V1.getSimpleValueType() == VT && "Bad operand type!")(static_cast <bool> (V1.getSimpleValueType() == VT && "Bad operand type!") ? void (0) : __assert_fail ("V1.getSimpleValueType() == VT && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 16050, __extension__ __PRETTY_FUNCTION__)); | |||
16051 | assert(V2.getSimpleValueType() == VT && "Bad operand type!")(static_cast <bool> (V2.getSimpleValueType() == VT && "Bad operand type!") ? void (0) : __assert_fail ("V2.getSimpleValueType() == VT && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 16051, __extension__ __PRETTY_FUNCTION__)); | |||
16052 | ||||
16053 | ArrayRef<int> LoMask = Mask.slice(0, Mask.size() / 2); | |||
16054 | ArrayRef<int> HiMask = Mask.slice(Mask.size() / 2); | |||
16055 | ||||
16056 | int NumElements = VT.getVectorNumElements(); | |||
16057 | int SplitNumElements = NumElements / 2; | |||
16058 | MVT ScalarVT = VT.getVectorElementType(); | |||
16059 | MVT SplitVT = MVT::getVectorVT(ScalarVT, SplitNumElements); | |||
16060 | ||||
16061 | // Use splitVector/extractSubVector so that split build-vectors just build two | |||
16062 | // narrower build vectors. This helps shuffling with splats and zeros. | |||
16063 | auto SplitVector = [&](SDValue V) { | |||
16064 | SDValue LoV, HiV; | |||
16065 | std::tie(LoV, HiV) = splitVector(peekThroughBitcasts(V), DAG, DL); | |||
16066 | return std::make_pair(DAG.getBitcast(SplitVT, LoV), | |||
16067 | DAG.getBitcast(SplitVT, HiV)); | |||
16068 | }; | |||
16069 | ||||
16070 | SDValue LoV1, HiV1, LoV2, HiV2; | |||
16071 | std::tie(LoV1, HiV1) = SplitVector(V1); | |||
16072 | std::tie(LoV2, HiV2) = SplitVector(V2); | |||
16073 | ||||
16074 | // Now create two 4-way blends of these half-width vectors. | |||
16075 | auto HalfBlend = [&](ArrayRef<int> HalfMask) { | |||
16076 | bool UseLoV1 = false, UseHiV1 = false, UseLoV2 = false, UseHiV2 = false; | |||
16077 | SmallVector<int, 32> V1BlendMask((unsigned)SplitNumElements, -1); | |||
16078 | SmallVector<int, 32> V2BlendMask((unsigned)SplitNumElements, -1); | |||
16079 | SmallVector<int, 32> BlendMask((unsigned)SplitNumElements, -1); | |||
16080 | for (int i = 0; i < SplitNumElements; ++i) { | |||
16081 | int M = HalfMask[i]; | |||
16082 | if (M >= NumElements) { | |||
16083 | if (M >= NumElements + SplitNumElements) | |||
16084 | UseHiV2 = true; | |||
16085 | else | |||
16086 | UseLoV2 = true; | |||
16087 | V2BlendMask[i] = M - NumElements; | |||
16088 | BlendMask[i] = SplitNumElements + i; | |||
16089 | } else if (M >= 0) { | |||
16090 | if (M >= SplitNumElements) | |||
16091 | UseHiV1 = true; | |||
16092 | else | |||
16093 | UseLoV1 = true; | |||
16094 | V1BlendMask[i] = M; | |||
16095 | BlendMask[i] = i; | |||
16096 | } | |||
16097 | } | |||
16098 | ||||
16099 | // Because the lowering happens after all combining takes place, we need to | |||
16100 | // manually combine these blend masks as much as possible so that we create | |||
16101 | // a minimal number of high-level vector shuffle nodes. | |||
16102 | ||||
16103 | // First try just blending the halves of V1 or V2. | |||
16104 | if (!UseLoV1 && !UseHiV1 && !UseLoV2 && !UseHiV2) | |||
16105 | return DAG.getUNDEF(SplitVT); | |||
16106 | if (!UseLoV2 && !UseHiV2) | |||
16107 | return DAG.getVectorShuffle(SplitVT, DL, LoV1, HiV1, V1BlendMask); | |||
16108 | if (!UseLoV1 && !UseHiV1) | |||
16109 | return DAG.getVectorShuffle(SplitVT, DL, LoV2, HiV2, V2BlendMask); | |||
16110 | ||||
16111 | SDValue V1Blend, V2Blend; | |||
16112 | if (UseLoV1 && UseHiV1) { | |||
16113 | V1Blend = | |||
16114 | DAG.getVectorShuffle(SplitVT, DL, LoV1, HiV1, V1BlendMask); | |||
16115 | } else { | |||
16116 | // We only use half of V1 so map the usage down into the final blend mask. | |||
16117 | V1Blend = UseLoV1 ? LoV1 : HiV1; | |||
16118 | for (int i = 0; i < SplitNumElements; ++i) | |||
16119 | if (BlendMask[i] >= 0 && BlendMask[i] < SplitNumElements) | |||
16120 | BlendMask[i] = V1BlendMask[i] - (UseLoV1 ? 0 : SplitNumElements); | |||
16121 | } | |||
16122 | if (UseLoV2 && UseHiV2) { | |||
16123 | V2Blend = | |||
16124 | DAG.getVectorShuffle(SplitVT, DL, LoV2, HiV2, V2BlendMask); | |||
16125 | } else { | |||
16126 | // We only use half of V2 so map the usage down into the final blend mask. | |||
16127 | V2Blend = UseLoV2 ? LoV2 : HiV2; | |||
16128 | for (int i = 0; i < SplitNumElements; ++i) | |||
16129 | if (BlendMask[i] >= SplitNumElements) | |||
16130 | BlendMask[i] = V2BlendMask[i] + (UseLoV2 ? SplitNumElements : 0); | |||
16131 | } | |||
16132 | return DAG.getVectorShuffle(SplitVT, DL, V1Blend, V2Blend, BlendMask); | |||
16133 | }; | |||
16134 | SDValue Lo = HalfBlend(LoMask); | |||
16135 | SDValue Hi = HalfBlend(HiMask); | |||
16136 | return DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, Lo, Hi); | |||
16137 | } | |||
16138 | ||||
16139 | /// Either split a vector in halves or decompose the shuffles and the | |||
16140 | /// blend/unpack. | |||
16141 | /// | |||
16142 | /// This is provided as a good fallback for many lowerings of non-single-input | |||
16143 | /// shuffles with more than one 128-bit lane. In those cases, we want to select | |||
16144 | /// between splitting the shuffle into 128-bit components and stitching those | |||
16145 | /// back together vs. extracting the single-input shuffles and blending those | |||
16146 | /// results. | |||
16147 | static SDValue lowerShuffleAsSplitOrBlend(const SDLoc &DL, MVT VT, SDValue V1, | |||
16148 | SDValue V2, ArrayRef<int> Mask, | |||
16149 | const X86Subtarget &Subtarget, | |||
16150 | SelectionDAG &DAG) { | |||
16151 | assert(!V2.isUndef() && "This routine must not be used to lower single-input "(static_cast <bool> (!V2.isUndef() && "This routine must not be used to lower single-input " "shuffles as it could then recurse on itself.") ? void (0) : __assert_fail ("!V2.isUndef() && \"This routine must not be used to lower single-input \" \"shuffles as it could then recurse on itself.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 16152, __extension__ __PRETTY_FUNCTION__)) | |||
16152 | "shuffles as it could then recurse on itself.")(static_cast <bool> (!V2.isUndef() && "This routine must not be used to lower single-input " "shuffles as it could then recurse on itself.") ? void (0) : __assert_fail ("!V2.isUndef() && \"This routine must not be used to lower single-input \" \"shuffles as it could then recurse on itself.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 16152, __extension__ __PRETTY_FUNCTION__)); | |||
16153 | int Size = Mask.size(); | |||
16154 | ||||
16155 | // If this can be modeled as a broadcast of two elements followed by a blend, | |||
16156 | // prefer that lowering. This is especially important because broadcasts can | |||
16157 | // often fold with memory operands. | |||
16158 | auto DoBothBroadcast = [&] { | |||
16159 | int V1BroadcastIdx = -1, V2BroadcastIdx = -1; | |||
16160 | for (int M : Mask) | |||
16161 | if (M >= Size) { | |||
16162 | if (V2BroadcastIdx < 0) | |||
16163 | V2BroadcastIdx = M - Size; | |||
16164 | else if (M - Size != V2BroadcastIdx) | |||
16165 | return false; | |||
16166 | } else if (M >= 0) { | |||
16167 | if (V1BroadcastIdx < 0) | |||
16168 | V1BroadcastIdx = M; | |||
16169 | else if (M != V1BroadcastIdx) | |||
16170 | return false; | |||
16171 | } | |||
16172 | return true; | |||
16173 | }; | |||
16174 | if (DoBothBroadcast()) | |||
16175 | return lowerShuffleAsDecomposedShuffleMerge(DL, VT, V1, V2, Mask, Subtarget, | |||
16176 | DAG); | |||
16177 | ||||
16178 | // If the inputs all stem from a single 128-bit lane of each input, then we | |||
16179 | // split them rather than blending because the split will decompose to | |||
16180 | // unusually few instructions. | |||
16181 | int LaneCount = VT.getSizeInBits() / 128; | |||
16182 | int LaneSize = Size / LaneCount; | |||
16183 | SmallBitVector LaneInputs[2]; | |||
16184 | LaneInputs[0].resize(LaneCount, false); | |||
16185 | LaneInputs[1].resize(LaneCount, false); | |||
16186 | for (int i = 0; i < Size; ++i) | |||
16187 | if (Mask[i] >= 0) | |||
16188 | LaneInputs[Mask[i] / Size][(Mask[i] % Size) / LaneSize] = true; | |||
16189 | if (LaneInputs[0].count() <= 1 && LaneInputs[1].count() <= 1) | |||
16190 | return splitAndLowerShuffle(DL, VT, V1, V2, Mask, DAG); | |||
16191 | ||||
16192 | // Otherwise, just fall back to decomposed shuffles and a blend/unpack. This | |||
16193 | // requires that the decomposed single-input shuffles don't end up here. | |||
16194 | return lowerShuffleAsDecomposedShuffleMerge(DL, VT, V1, V2, Mask, Subtarget, | |||
16195 | DAG); | |||
16196 | } | |||
16197 | ||||
16198 | // Lower as SHUFPD(VPERM2F128(V1, V2), VPERM2F128(V1, V2)). | |||
16199 | // TODO: Extend to support v8f32 (+ 512-bit shuffles). | |||
16200 | static SDValue lowerShuffleAsLanePermuteAndSHUFP(const SDLoc &DL, MVT VT, | |||
16201 | SDValue V1, SDValue V2, | |||
16202 | ArrayRef<int> Mask, | |||
16203 | SelectionDAG &DAG) { | |||
16204 | assert(VT == MVT::v4f64 && "Only for v4f64 shuffles")(static_cast <bool> (VT == MVT::v4f64 && "Only for v4f64 shuffles" ) ? void (0) : __assert_fail ("VT == MVT::v4f64 && \"Only for v4f64 shuffles\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 16204, __extension__ __PRETTY_FUNCTION__)); | |||
16205 | ||||
16206 | int LHSMask[4] = {-1, -1, -1, -1}; | |||
16207 | int RHSMask[4] = {-1, -1, -1, -1}; | |||
16208 | unsigned SHUFPMask = 0; | |||
16209 | ||||
16210 | // As SHUFPD uses a single LHS/RHS element per lane, we can always | |||
16211 | // perform the shuffle once the lanes have been shuffled in place. | |||
16212 | for (int i = 0; i != 4; ++i) { | |||
16213 | int M = Mask[i]; | |||
16214 | if (M < 0) | |||
16215 | continue; | |||
16216 | int LaneBase = i & ~1; | |||
16217 | auto &LaneMask = (i & 1) ? RHSMask : LHSMask; | |||
16218 | LaneMask[LaneBase + (M & 1)] = M; | |||
16219 | SHUFPMask |= (M & 1) << i; | |||
16220 | } | |||
16221 | ||||
16222 | SDValue LHS = DAG.getVectorShuffle(VT, DL, V1, V2, LHSMask); | |||
16223 | SDValue RHS = DAG.getVectorShuffle(VT, DL, V1, V2, RHSMask); | |||
16224 | return DAG.getNode(X86ISD::SHUFP, DL, VT, LHS, RHS, | |||
16225 | DAG.getTargetConstant(SHUFPMask, DL, MVT::i8)); | |||
16226 | } | |||
16227 | ||||
16228 | /// Lower a vector shuffle crossing multiple 128-bit lanes as | |||
16229 | /// a lane permutation followed by a per-lane permutation. | |||
16230 | /// | |||
16231 | /// This is mainly for cases where we can have non-repeating permutes | |||
16232 | /// in each lane. | |||
16233 | /// | |||
16234 | /// TODO: This is very similar to lowerShuffleAsLanePermuteAndRepeatedMask, | |||
16235 | /// we should investigate merging them. | |||
16236 | static SDValue lowerShuffleAsLanePermuteAndPermute( | |||
16237 | const SDLoc &DL, MVT VT, SDValue V1, SDValue V2, ArrayRef<int> Mask, | |||
16238 | SelectionDAG &DAG, const X86Subtarget &Subtarget) { | |||
16239 | int NumElts = VT.getVectorNumElements(); | |||
16240 | int NumLanes = VT.getSizeInBits() / 128; | |||
16241 | int NumEltsPerLane = NumElts / NumLanes; | |||
16242 | bool CanUseSublanes = Subtarget.hasAVX2() && V2.isUndef(); | |||
16243 | ||||
16244 | /// Attempts to find a sublane permute with the given size | |||
16245 | /// that gets all elements into their target lanes. | |||
16246 | /// | |||
16247 | /// If successful, fills CrossLaneMask and InLaneMask and returns true. | |||
16248 | /// If unsuccessful, returns false and may overwrite InLaneMask. | |||
16249 | auto getSublanePermute = [&](int NumSublanes) -> SDValue { | |||
16250 | int NumSublanesPerLane = NumSublanes / NumLanes; | |||
16251 | int NumEltsPerSublane = NumElts / NumSublanes; | |||
16252 | ||||
16253 | SmallVector<int, 16> CrossLaneMask; | |||
16254 | SmallVector<int, 16> InLaneMask(NumElts, SM_SentinelUndef); | |||
16255 | // CrossLaneMask but one entry == one sublane. | |||
16256 | SmallVector<int, 16> CrossLaneMaskLarge(NumSublanes, SM_SentinelUndef); | |||
16257 | ||||
16258 | for (int i = 0; i != NumElts; ++i) { | |||
16259 | int M = Mask[i]; | |||
16260 | if (M < 0) | |||
16261 | continue; | |||
16262 | ||||
16263 | int SrcSublane = M / NumEltsPerSublane; | |||
16264 | int DstLane = i / NumEltsPerLane; | |||
16265 | ||||
16266 | // We only need to get the elements into the right lane, not sublane. | |||
16267 | // So search all sublanes that make up the destination lane. | |||
16268 | bool Found = false; | |||
16269 | int DstSubStart = DstLane * NumSublanesPerLane; | |||
16270 | int DstSubEnd = DstSubStart + NumSublanesPerLane; | |||
16271 | for (int DstSublane = DstSubStart; DstSublane < DstSubEnd; ++DstSublane) { | |||
16272 | if (!isUndefOrEqual(CrossLaneMaskLarge[DstSublane], SrcSublane)) | |||
16273 | continue; | |||
16274 | ||||
16275 | Found = true; | |||
16276 | CrossLaneMaskLarge[DstSublane] = SrcSublane; | |||
16277 | int DstSublaneOffset = DstSublane * NumEltsPerSublane; | |||
16278 | InLaneMask[i] = DstSublaneOffset + M % NumEltsPerSublane; | |||
16279 | break; | |||
16280 | } | |||
16281 | if (!Found) | |||
16282 | return SDValue(); | |||
16283 | } | |||
16284 | ||||
16285 | // Fill CrossLaneMask using CrossLaneMaskLarge. | |||
16286 | narrowShuffleMaskElts(NumEltsPerSublane, CrossLaneMaskLarge, CrossLaneMask); | |||
16287 | ||||
16288 | if (!CanUseSublanes) { | |||
16289 | // If we're only shuffling a single lowest lane and the rest are identity | |||
16290 | // then don't bother. | |||
16291 | // TODO - isShuffleMaskInputInPlace could be extended to something like | |||
16292 | // this. | |||
16293 | int NumIdentityLanes = 0; | |||
16294 | bool OnlyShuffleLowestLane = true; | |||
16295 | for (int i = 0; i != NumLanes; ++i) { | |||
16296 | int LaneOffset = i * NumEltsPerLane; | |||
16297 | if (isSequentialOrUndefInRange(InLaneMask, LaneOffset, NumEltsPerLane, | |||
16298 | i * NumEltsPerLane)) | |||
16299 | NumIdentityLanes++; | |||
16300 | else if (CrossLaneMask[LaneOffset] != 0) | |||
16301 | OnlyShuffleLowestLane = false; | |||
16302 | } | |||
16303 | if (OnlyShuffleLowestLane && NumIdentityLanes == (NumLanes - 1)) | |||
16304 | return SDValue(); | |||
16305 | } | |||
16306 | ||||
16307 | SDValue CrossLane = DAG.getVectorShuffle(VT, DL, V1, V2, CrossLaneMask); | |||
16308 | return DAG.getVectorShuffle(VT, DL, CrossLane, DAG.getUNDEF(VT), | |||
16309 | InLaneMask); | |||
16310 | }; | |||
16311 | ||||
16312 | // First attempt a solution with full lanes. | |||
16313 | if (SDValue V = getSublanePermute(/*NumSublanes=*/NumLanes)) | |||
16314 | return V; | |||
16315 | ||||
16316 | // The rest of the solutions use sublanes. | |||
16317 | if (!CanUseSublanes) | |||
16318 | return SDValue(); | |||
16319 | ||||
16320 | // Then attempt a solution with 64-bit sublanes (vpermq). | |||
16321 | if (SDValue V = getSublanePermute(/*NumSublanes=*/NumLanes * 2)) | |||
16322 | return V; | |||
16323 | ||||
16324 | // If that doesn't work and we have fast variable cross-lane shuffle, | |||
16325 | // attempt 32-bit sublanes (vpermd). | |||
16326 | if (!Subtarget.hasFastVariableCrossLaneShuffle()) | |||
16327 | return SDValue(); | |||
16328 | ||||
16329 | return getSublanePermute(/*NumSublanes=*/NumLanes * 4); | |||
16330 | } | |||
16331 | ||||
16332 | /// Lower a vector shuffle crossing multiple 128-bit lanes by shuffling one | |||
16333 | /// source with a lane permutation. | |||
16334 | /// | |||
16335 | /// This lowering strategy results in four instructions in the worst case for a | |||
16336 | /// single-input cross lane shuffle which is lower than any other fully general | |||
16337 | /// cross-lane shuffle strategy I'm aware of. Special cases for each particular | |||
16338 | /// shuffle pattern should be handled prior to trying this lowering. | |||
16339 | static SDValue lowerShuffleAsLanePermuteAndShuffle( | |||
16340 | const SDLoc &DL, MVT VT, SDValue V1, SDValue V2, ArrayRef<int> Mask, | |||
16341 | SelectionDAG &DAG, const X86Subtarget &Subtarget) { | |||
16342 | // FIXME: This should probably be generalized for 512-bit vectors as well. | |||
16343 | assert(VT.is256BitVector() && "Only for 256-bit vector shuffles!")(static_cast <bool> (VT.is256BitVector() && "Only for 256-bit vector shuffles!" ) ? void (0) : __assert_fail ("VT.is256BitVector() && \"Only for 256-bit vector shuffles!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 16343, __extension__ __PRETTY_FUNCTION__)); | |||
16344 | int Size = Mask.size(); | |||
16345 | int LaneSize = Size / 2; | |||
16346 | ||||
16347 | // Fold to SHUFPD(VPERM2F128(V1, V2), VPERM2F128(V1, V2)). | |||
16348 | // Only do this if the elements aren't all from the lower lane, | |||
16349 | // otherwise we're (probably) better off doing a split. | |||
16350 | if (VT == MVT::v4f64 && | |||
16351 | !all_of(Mask, [LaneSize](int M) { return M < LaneSize; })) | |||
16352 | if (SDValue V = | |||
16353 | lowerShuffleAsLanePermuteAndSHUFP(DL, VT, V1, V2, Mask, DAG)) | |||
16354 | return V; | |||
16355 | ||||
16356 | // If there are only inputs from one 128-bit lane, splitting will in fact be | |||
16357 | // less expensive. The flags track whether the given lane contains an element | |||
16358 | // that crosses to another lane. | |||
16359 | if (!Subtarget.hasAVX2()) { | |||
16360 | bool LaneCrossing[2] = {false, false}; | |||
16361 | for (int i = 0; i < Size; ++i) | |||
16362 | if (Mask[i] >= 0 && ((Mask[i] % Size) / LaneSize) != (i / LaneSize)) | |||
16363 | LaneCrossing[(Mask[i] % Size) / LaneSize] = true; | |||
16364 | if (!LaneCrossing[0] || !LaneCrossing[1]) | |||
16365 | return splitAndLowerShuffle(DL, VT, V1, V2, Mask, DAG); | |||
16366 | } else { | |||
16367 | bool LaneUsed[2] = {false, false}; | |||
16368 | for (int i = 0; i < Size; ++i) | |||
16369 | if (Mask[i] >= 0) | |||
16370 | LaneUsed[(Mask[i] % Size) / LaneSize] = true; | |||
16371 | if (!LaneUsed[0] || !LaneUsed[1]) | |||
16372 | return splitAndLowerShuffle(DL, VT, V1, V2, Mask, DAG); | |||
16373 | } | |||
16374 | ||||
16375 | // TODO - we could support shuffling V2 in the Flipped input. | |||
16376 | assert(V2.isUndef() &&(static_cast <bool> (V2.isUndef() && "This last part of this routine only works on single input shuffles" ) ? void (0) : __assert_fail ("V2.isUndef() && \"This last part of this routine only works on single input shuffles\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 16377, __extension__ __PRETTY_FUNCTION__)) | |||
16377 | "This last part of this routine only works on single input shuffles")(static_cast <bool> (V2.isUndef() && "This last part of this routine only works on single input shuffles" ) ? void (0) : __assert_fail ("V2.isUndef() && \"This last part of this routine only works on single input shuffles\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 16377, __extension__ __PRETTY_FUNCTION__)); | |||
16378 | ||||
16379 | SmallVector<int, 32> InLaneMask(Mask.begin(), Mask.end()); | |||
16380 | for (int i = 0; i < Size; ++i) { | |||
16381 | int &M = InLaneMask[i]; | |||
16382 | if (M < 0) | |||
16383 | continue; | |||
16384 | if (((M % Size) / LaneSize) != (i / LaneSize)) | |||
| ||||
16385 | M = (M % LaneSize) + ((i / LaneSize) * LaneSize) + Size; | |||
16386 | } | |||
16387 | assert(!is128BitLaneCrossingShuffleMask(VT, InLaneMask) &&(static_cast <bool> (!is128BitLaneCrossingShuffleMask(VT , InLaneMask) && "In-lane shuffle mask expected") ? void (0) : __assert_fail ("!is128BitLaneCrossingShuffleMask(VT, InLaneMask) && \"In-lane shuffle mask expected\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 16388, __extension__ __PRETTY_FUNCTION__)) | |||
16388 | "In-lane shuffle mask expected")(static_cast <bool> (!is128BitLaneCrossingShuffleMask(VT , InLaneMask) && "In-lane shuffle mask expected") ? void (0) : __assert_fail ("!is128BitLaneCrossingShuffleMask(VT, InLaneMask) && \"In-lane shuffle mask expected\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 16388, __extension__ __PRETTY_FUNCTION__)); | |||
16389 | ||||
16390 | // Flip the lanes, and shuffle the results which should now be in-lane. | |||
16391 | MVT PVT = VT.isFloatingPoint() ? MVT::v4f64 : MVT::v4i64; | |||
16392 | SDValue Flipped = DAG.getBitcast(PVT, V1); | |||
16393 | Flipped = | |||
16394 | DAG.getVectorShuffle(PVT, DL, Flipped, DAG.getUNDEF(PVT), {2, 3, 0, 1}); | |||
16395 | Flipped = DAG.getBitcast(VT, Flipped); | |||
16396 | return DAG.getVectorShuffle(VT, DL, V1, Flipped, InLaneMask); | |||
16397 | } | |||
16398 | ||||
16399 | /// Handle lowering 2-lane 128-bit shuffles. | |||
16400 | static SDValue lowerV2X128Shuffle(const SDLoc &DL, MVT VT, SDValue V1, | |||
16401 | SDValue V2, ArrayRef<int> Mask, | |||
16402 | const APInt &Zeroable, | |||
16403 | const X86Subtarget &Subtarget, | |||
16404 | SelectionDAG &DAG) { | |||
16405 | if (V2.isUndef()) { | |||
16406 | // Attempt to match VBROADCAST*128 subvector broadcast load. | |||
16407 | bool SplatLo = isShuffleEquivalent(Mask, {0, 1, 0, 1}, V1); | |||
16408 | bool SplatHi = isShuffleEquivalent(Mask, {2, 3, 2, 3}, V1); | |||
16409 | if ((SplatLo || SplatHi) && !Subtarget.hasAVX512() && V1.hasOneUse() && | |||
16410 | MayFoldLoad(peekThroughOneUseBitcasts(V1))) { | |||
16411 | auto *Ld = cast<LoadSDNode>(peekThroughOneUseBitcasts(V1)); | |||
16412 | if (!Ld->isNonTemporal()) { | |||
16413 | MVT MemVT = VT.getHalfNumVectorElementsVT(); | |||
16414 | unsigned Ofs = SplatLo ? 0 : MemVT.getStoreSize(); | |||
16415 | SDVTList Tys = DAG.getVTList(VT, MVT::Other); | |||
16416 | SDValue Ptr = DAG.getMemBasePlusOffset(Ld->getBasePtr(), | |||
16417 | TypeSize::Fixed(Ofs), DL); | |||
16418 | SDValue Ops[] = {Ld->getChain(), Ptr}; | |||
16419 | SDValue BcastLd = DAG.getMemIntrinsicNode( | |||
16420 | X86ISD::SUBV_BROADCAST_LOAD, DL, Tys, Ops, MemVT, | |||
16421 | DAG.getMachineFunction().getMachineMemOperand( | |||
16422 | Ld->getMemOperand(), Ofs, MemVT.getStoreSize())); | |||
16423 | DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), BcastLd.getValue(1)); | |||
16424 | return BcastLd; | |||
16425 | } | |||
16426 | } | |||
16427 | ||||
16428 | // With AVX2, use VPERMQ/VPERMPD for unary shuffles to allow memory folding. | |||
16429 | if (Subtarget.hasAVX2()) | |||
16430 | return SDValue(); | |||
16431 | } | |||
16432 | ||||
16433 | bool V2IsZero = !V2.isUndef() && ISD::isBuildVectorAllZeros(V2.getNode()); | |||
16434 | ||||
16435 | SmallVector<int, 4> WidenedMask; | |||
16436 | if (!canWidenShuffleElements(Mask, Zeroable, V2IsZero, WidenedMask)) | |||
16437 | return SDValue(); | |||
16438 | ||||
16439 | bool IsLowZero = (Zeroable & 0x3) == 0x3; | |||
16440 | bool IsHighZero = (Zeroable & 0xc) == 0xc; | |||
16441 | ||||
16442 | // Try to use an insert into a zero vector. | |||
16443 | if (WidenedMask[0] == 0 && IsHighZero) { | |||
16444 | MVT SubVT = MVT::getVectorVT(VT.getVectorElementType(), 2); | |||
16445 | SDValue LoV = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, SubVT, V1, | |||
16446 | DAG.getIntPtrConstant(0, DL)); | |||
16447 | return DAG.getNode(ISD::INSERT_SUBVECTOR, DL, VT, | |||
16448 | getZeroVector(VT, Subtarget, DAG, DL), LoV, | |||
16449 | DAG.getIntPtrConstant(0, DL)); | |||
16450 | } | |||
16451 | ||||
16452 | // TODO: If minimizing size and one of the inputs is a zero vector and the | |||
16453 | // the zero vector has only one use, we could use a VPERM2X128 to save the | |||
16454 | // instruction bytes needed to explicitly generate the zero vector. | |||
16455 | ||||
16456 | // Blends are faster and handle all the non-lane-crossing cases. | |||
16457 | if (SDValue Blend = lowerShuffleAsBlend(DL, VT, V1, V2, Mask, Zeroable, | |||
16458 | Subtarget, DAG)) | |||
16459 | return Blend; | |||
16460 | ||||
16461 | // If either input operand is a zero vector, use VPERM2X128 because its mask | |||
16462 | // allows us to replace the zero input with an implicit zero. | |||
16463 | if (!IsLowZero && !IsHighZero) { | |||
16464 | // Check for patterns which can be matched with a single insert of a 128-bit | |||
16465 | // subvector. | |||
16466 | bool OnlyUsesV1 = isShuffleEquivalent(Mask, {0, 1, 0, 1}, V1, V2); | |||
16467 | if (OnlyUsesV1 || isShuffleEquivalent(Mask, {0, 1, 4, 5}, V1, V2)) { | |||
16468 | ||||
16469 | // With AVX1, use vperm2f128 (below) to allow load folding. Otherwise, | |||
16470 | // this will likely become vinsertf128 which can't fold a 256-bit memop. | |||
16471 | if (!isa<LoadSDNode>(peekThroughBitcasts(V1))) { | |||
16472 | MVT SubVT = MVT::getVectorVT(VT.getVectorElementType(), 2); | |||
16473 | SDValue SubVec = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, SubVT, | |||
16474 | OnlyUsesV1 ? V1 : V2, | |||
16475 | DAG.getIntPtrConstant(0, DL)); | |||
16476 | return DAG.getNode(ISD::INSERT_SUBVECTOR, DL, VT, V1, SubVec, | |||
16477 | DAG.getIntPtrConstant(2, DL)); | |||
16478 | } | |||
16479 | } | |||
16480 | ||||
16481 | // Try to use SHUF128 if possible. | |||
16482 | if (Subtarget.hasVLX()) { | |||
16483 | if (WidenedMask[0] < 2 && WidenedMask[1] >= 2) { | |||
16484 | unsigned PermMask = ((WidenedMask[0] % 2) << 0) | | |||
16485 | ((WidenedMask[1] % 2) << 1); | |||
16486 | return DAG.getNode(X86ISD::SHUF128, DL, VT, V1, V2, | |||
16487 | DAG.getTargetConstant(PermMask, DL, MVT::i8)); | |||
16488 | } | |||
16489 | } | |||
16490 | } | |||
16491 | ||||
16492 | // Otherwise form a 128-bit permutation. After accounting for undefs, | |||
16493 | // convert the 64-bit shuffle mask selection values into 128-bit | |||
16494 | // selection bits by dividing the indexes by 2 and shifting into positions | |||
16495 | // defined by a vperm2*128 instruction's immediate control byte. | |||
16496 | ||||
16497 | // The immediate permute control byte looks like this: | |||
16498 | // [1:0] - select 128 bits from sources for low half of destination | |||
16499 | // [2] - ignore | |||
16500 | // [3] - zero low half of destination | |||
16501 | // [5:4] - select 128 bits from sources for high half of destination | |||
16502 | // [6] - ignore | |||
16503 | // [7] - zero high half of destination | |||
16504 | ||||
16505 | assert((WidenedMask[0] >= 0 || IsLowZero) &&(static_cast <bool> ((WidenedMask[0] >= 0 || IsLowZero ) && (WidenedMask[1] >= 0 || IsHighZero) && "Undef half?") ? void (0) : __assert_fail ("(WidenedMask[0] >= 0 || IsLowZero) && (WidenedMask[1] >= 0 || IsHighZero) && \"Undef half?\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 16506, __extension__ __PRETTY_FUNCTION__)) | |||
16506 | (WidenedMask[1] >= 0 || IsHighZero) && "Undef half?")(static_cast <bool> ((WidenedMask[0] >= 0 || IsLowZero ) && (WidenedMask[1] >= 0 || IsHighZero) && "Undef half?") ? void (0) : __assert_fail ("(WidenedMask[0] >= 0 || IsLowZero) && (WidenedMask[1] >= 0 || IsHighZero) && \"Undef half?\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 16506, __extension__ __PRETTY_FUNCTION__)); | |||
16507 | ||||
16508 | unsigned PermMask = 0; | |||
16509 | PermMask |= IsLowZero ? 0x08 : (WidenedMask[0] << 0); | |||
16510 | PermMask |= IsHighZero ? 0x80 : (WidenedMask[1] << 4); | |||
16511 | ||||
16512 | // Check the immediate mask and replace unused sources with undef. | |||
16513 | if ((PermMask & 0x0a) != 0x00 && (PermMask & 0xa0) != 0x00) | |||
16514 | V1 = DAG.getUNDEF(VT); | |||
16515 | if ((PermMask & 0x0a) != 0x02 && (PermMask & 0xa0) != 0x20) | |||
16516 | V2 = DAG.getUNDEF(VT); | |||
16517 | ||||
16518 | return DAG.getNode(X86ISD::VPERM2X128, DL, VT, V1, V2, | |||
16519 | DAG.getTargetConstant(PermMask, DL, MVT::i8)); | |||
16520 | } | |||
16521 | ||||
16522 | /// Lower a vector shuffle by first fixing the 128-bit lanes and then | |||
16523 | /// shuffling each lane. | |||
16524 | /// | |||
16525 | /// This attempts to create a repeated lane shuffle where each lane uses one | |||
16526 | /// or two of the lanes of the inputs. The lanes of the input vectors are | |||
16527 | /// shuffled in one or two independent shuffles to get the lanes into the | |||
16528 | /// position needed by the final shuffle. | |||
16529 | static SDValue lowerShuffleAsLanePermuteAndRepeatedMask( | |||
16530 | const SDLoc &DL, MVT VT, SDValue V1, SDValue V2, ArrayRef<int> Mask, | |||
16531 | const X86Subtarget &Subtarget, SelectionDAG &DAG) { | |||
16532 | assert(!V2.isUndef() && "This is only useful with multiple inputs.")(static_cast <bool> (!V2.isUndef() && "This is only useful with multiple inputs." ) ? void (0) : __assert_fail ("!V2.isUndef() && \"This is only useful with multiple inputs.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 16532, __extension__ __PRETTY_FUNCTION__)); | |||
16533 | ||||
16534 | if (is128BitLaneRepeatedShuffleMask(VT, Mask)) | |||
16535 | return SDValue(); | |||
16536 | ||||
16537 | int NumElts = Mask.size(); | |||
16538 | int NumLanes = VT.getSizeInBits() / 128; | |||
16539 | int NumLaneElts = 128 / VT.getScalarSizeInBits(); | |||
16540 | SmallVector<int, 16> RepeatMask(NumLaneElts, -1); | |||
16541 | SmallVector<std::array<int, 2>, 2> LaneSrcs(NumLanes, {{-1, -1}}); | |||
16542 | ||||
16543 | // First pass will try to fill in the RepeatMask from lanes that need two | |||
16544 | // sources. | |||
16545 | for (int Lane = 0; Lane != NumLanes; ++Lane) { | |||
16546 | int Srcs[2] = {-1, -1}; | |||
16547 | SmallVector<int, 16> InLaneMask(NumLaneElts, -1); | |||
16548 | for (int i = 0; i != NumLaneElts; ++i) { | |||
16549 | int M = Mask[(Lane * NumLaneElts) + i]; | |||
16550 | if (M < 0) | |||
16551 | continue; | |||
16552 | // Determine which of the possible input lanes (NumLanes from each source) | |||
16553 | // this element comes from. Assign that as one of the sources for this | |||
16554 | // lane. We can assign up to 2 sources for this lane. If we run out | |||
16555 | // sources we can't do anything. | |||
16556 | int LaneSrc = M / NumLaneElts; | |||
16557 | int Src; | |||
16558 | if (Srcs[0] < 0 || Srcs[0] == LaneSrc) | |||
16559 | Src = 0; | |||
16560 | else if (Srcs[1] < 0 || Srcs[1] == LaneSrc) | |||
16561 | Src = 1; | |||
16562 | else | |||
16563 | return SDValue(); | |||
16564 | ||||
16565 | Srcs[Src] = LaneSrc; | |||
16566 | InLaneMask[i] = (M % NumLaneElts) + Src * NumElts; | |||
16567 | } | |||
16568 | ||||
16569 | // If this lane has two sources, see if it fits with the repeat mask so far. | |||
16570 | if (Srcs[1] < 0) | |||
16571 | continue; | |||
16572 | ||||
16573 | LaneSrcs[Lane][0] = Srcs[0]; | |||
16574 | LaneSrcs[Lane][1] = Srcs[1]; | |||
16575 | ||||
16576 | auto MatchMasks = [](ArrayRef<int> M1, ArrayRef<int> M2) { | |||
16577 | assert(M1.size() == M2.size() && "Unexpected mask size")(static_cast <bool> (M1.size() == M2.size() && "Unexpected mask size" ) ? void (0) : __assert_fail ("M1.size() == M2.size() && \"Unexpected mask size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 16577, __extension__ __PRETTY_FUNCTION__)); | |||
16578 | for (int i = 0, e = M1.size(); i != e; ++i) | |||
16579 | if (M1[i] >= 0 && M2[i] >= 0 && M1[i] != M2[i]) | |||
16580 | return false; | |||
16581 | return true; | |||
16582 | }; | |||
16583 | ||||
16584 | auto MergeMasks = [](ArrayRef<int> Mask, MutableArrayRef<int> MergedMask) { | |||
16585 | assert(Mask.size() == MergedMask.size() && "Unexpected mask size")(static_cast <bool> (Mask.size() == MergedMask.size() && "Unexpected mask size") ? void (0) : __assert_fail ("Mask.size() == MergedMask.size() && \"Unexpected mask size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 16585, __extension__ __PRETTY_FUNCTION__)); | |||
16586 | for (int i = 0, e = MergedMask.size(); i != e; ++i) { | |||
16587 | int M = Mask[i]; | |||
16588 | if (M < 0) | |||
16589 | continue; | |||
16590 | assert((MergedMask[i] < 0 || MergedMask[i] == M) &&(static_cast <bool> ((MergedMask[i] < 0 || MergedMask [i] == M) && "Unexpected mask element") ? void (0) : __assert_fail ("(MergedMask[i] < 0 || MergedMask[i] == M) && \"Unexpected mask element\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 16591, __extension__ __PRETTY_FUNCTION__)) | |||
16591 | "Unexpected mask element")(static_cast <bool> ((MergedMask[i] < 0 || MergedMask [i] == M) && "Unexpected mask element") ? void (0) : __assert_fail ("(MergedMask[i] < 0 || MergedMask[i] == M) && \"Unexpected mask element\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 16591, __extension__ __PRETTY_FUNCTION__)); | |||
16592 | MergedMask[i] = M; | |||
16593 | } | |||
16594 | }; | |||
16595 | ||||
16596 | if (MatchMasks(InLaneMask, RepeatMask)) { | |||
16597 | // Merge this lane mask into the final repeat mask. | |||
16598 | MergeMasks(InLaneMask, RepeatMask); | |||
16599 | continue; | |||
16600 | } | |||
16601 | ||||
16602 | // Didn't find a match. Swap the operands and try again. | |||
16603 | std::swap(LaneSrcs[Lane][0], LaneSrcs[Lane][1]); | |||
16604 | ShuffleVectorSDNode::commuteMask(InLaneMask); | |||
16605 | ||||
16606 | if (MatchMasks(InLaneMask, RepeatMask)) { | |||
16607 | // Merge this lane mask into the final repeat mask. | |||
16608 | MergeMasks(InLaneMask, RepeatMask); | |||
16609 | continue; | |||
16610 | } | |||
16611 | ||||
16612 | // Couldn't find a match with the operands in either order. | |||
16613 | return SDValue(); | |||
16614 | } | |||
16615 | ||||
16616 | // Now handle any lanes with only one source. | |||
16617 | for (int Lane = 0; Lane != NumLanes; ++Lane) { | |||
16618 | // If this lane has already been processed, skip it. | |||
16619 | if (LaneSrcs[Lane][0] >= 0) | |||
16620 | continue; | |||
16621 | ||||
16622 | for (int i = 0; i != NumLaneElts; ++i) { | |||
16623 | int M = Mask[(Lane * NumLaneElts) + i]; | |||
16624 | if (M < 0) | |||
16625 | continue; | |||
16626 | ||||
16627 | // If RepeatMask isn't defined yet we can define it ourself. | |||
16628 | if (RepeatMask[i] < 0) | |||
16629 | RepeatMask[i] = M % NumLaneElts; | |||
16630 | ||||
16631 | if (RepeatMask[i] < NumElts) { | |||
16632 | if (RepeatMask[i] != M % NumLaneElts) | |||
16633 | return SDValue(); | |||
16634 | LaneSrcs[Lane][0] = M / NumLaneElts; | |||
16635 | } else { | |||
16636 | if (RepeatMask[i] != ((M % NumLaneElts) + NumElts)) | |||
16637 | return SDValue(); | |||
16638 | LaneSrcs[Lane][1] = M / NumLaneElts; | |||
16639 | } | |||
16640 | } | |||
16641 | ||||
16642 | if (LaneSrcs[Lane][0] < 0 && LaneSrcs[Lane][1] < 0) | |||
16643 | return SDValue(); | |||
16644 | } | |||
16645 | ||||
16646 | SmallVector<int, 16> NewMask(NumElts, -1); | |||
16647 | for (int Lane = 0; Lane != NumLanes; ++Lane) { | |||
16648 | int Src = LaneSrcs[Lane][0]; | |||
16649 | for (int i = 0; i != NumLaneElts; ++i) { | |||
16650 | int M = -1; | |||
16651 | if (Src >= 0) | |||
16652 | M = Src * NumLaneElts + i; | |||
16653 | NewMask[Lane * NumLaneElts + i] = M; | |||
16654 | } | |||
16655 | } | |||
16656 | SDValue NewV1 = DAG.getVectorShuffle(VT, DL, V1, V2, NewMask); | |||
16657 | // Ensure we didn't get back the shuffle we started with. | |||
16658 | // FIXME: This is a hack to make up for some splat handling code in | |||
16659 | // getVectorShuffle. | |||
16660 | if (isa<ShuffleVectorSDNode>(NewV1) && | |||
16661 | cast<ShuffleVectorSDNode>(NewV1)->getMask() == Mask) | |||
16662 | return SDValue(); | |||
16663 | ||||
16664 | for (int Lane = 0; Lane != NumLanes; ++Lane) { | |||
16665 | int Src = LaneSrcs[Lane][1]; | |||
16666 | for (int i = 0; i != NumLaneElts; ++i) { | |||
16667 | int M = -1; | |||
16668 | if (Src >= 0) | |||
16669 | M = Src * NumLaneElts + i; | |||
16670 | NewMask[Lane * NumLaneElts + i] = M; | |||
16671 | } | |||
16672 | } | |||
16673 | SDValue NewV2 = DAG.getVectorShuffle(VT, DL, V1, V2, NewMask); | |||
16674 | // Ensure we didn't get back the shuffle we started with. | |||
16675 | // FIXME: This is a hack to make up for some splat handling code in | |||
16676 | // getVectorShuffle. | |||
16677 | if (isa<ShuffleVectorSDNode>(NewV2) && | |||
16678 | cast<ShuffleVectorSDNode>(NewV2)->getMask() == Mask) | |||
16679 | return SDValue(); | |||
16680 | ||||
16681 | for (int i = 0; i != NumElts; ++i) { | |||
16682 | NewMask[i] = RepeatMask[i % NumLaneElts]; | |||
16683 | if (NewMask[i] < 0) | |||
16684 | continue; | |||
16685 | ||||
16686 | NewMask[i] += (i / NumLaneElts) * NumLaneElts; | |||
16687 | } | |||
16688 | return DAG.getVectorShuffle(VT, DL, NewV1, NewV2, NewMask); | |||
16689 | } | |||
16690 | ||||
16691 | /// If the input shuffle mask results in a vector that is undefined in all upper | |||
16692 | /// or lower half elements and that mask accesses only 2 halves of the | |||
16693 | /// shuffle's operands, return true. A mask of half the width with mask indexes | |||
16694 | /// adjusted to access the extracted halves of the original shuffle operands is | |||
16695 | /// returned in HalfMask. HalfIdx1 and HalfIdx2 return whether the upper or | |||
16696 | /// lower half of each input operand is accessed. | |||
16697 | static bool | |||
16698 | getHalfShuffleMask(ArrayRef<int> Mask, MutableArrayRef<int> HalfMask, | |||
16699 | int &HalfIdx1, int &HalfIdx2) { | |||
16700 | assert((Mask.size() == HalfMask.size() * 2) &&(static_cast <bool> ((Mask.size() == HalfMask.size() * 2 ) && "Expected input mask to be twice as long as output" ) ? void (0) : __assert_fail ("(Mask.size() == HalfMask.size() * 2) && \"Expected input mask to be twice as long as output\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 16701, __extension__ __PRETTY_FUNCTION__)) | |||
16701 | "Expected input mask to be twice as long as output")(static_cast <bool> ((Mask.size() == HalfMask.size() * 2 ) && "Expected input mask to be twice as long as output" ) ? void (0) : __assert_fail ("(Mask.size() == HalfMask.size() * 2) && \"Expected input mask to be twice as long as output\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 16701, __extension__ __PRETTY_FUNCTION__)); | |||
16702 | ||||
16703 | // Exactly one half of the result must be undef to allow narrowing. | |||
16704 | bool UndefLower = isUndefLowerHalf(Mask); | |||
16705 | bool UndefUpper = isUndefUpperHalf(Mask); | |||
16706 | if (UndefLower == UndefUpper) | |||
16707 | return false; | |||
16708 | ||||
16709 | unsigned HalfNumElts = HalfMask.size(); | |||
16710 | unsigned MaskIndexOffset = UndefLower ? HalfNumElts : 0; | |||
16711 | HalfIdx1 = -1; | |||
16712 | HalfIdx2 = -1; | |||
16713 | for (unsigned i = 0; i != HalfNumElts; ++i) { | |||
16714 | int M = Mask[i + MaskIndexOffset]; | |||
16715 | if (M < 0) { | |||
16716 | HalfMask[i] = M; | |||
16717 | continue; | |||
16718 | } | |||
16719 | ||||
16720 | // Determine which of the 4 half vectors this element is from. | |||
16721 | // i.e. 0 = Lower V1, 1 = Upper V1, 2 = Lower V2, 3 = Upper V2. | |||
16722 | int HalfIdx = M / HalfNumElts; | |||
16723 | ||||
16724 | // Determine the element index into its half vector source. | |||
16725 | int HalfElt = M % HalfNumElts; | |||
16726 | ||||
16727 | // We can shuffle with up to 2 half vectors, set the new 'half' | |||
16728 | // shuffle mask accordingly. | |||
16729 | if (HalfIdx1 < 0 || HalfIdx1 == HalfIdx) { | |||
16730 | HalfMask[i] = HalfElt; | |||
16731 | HalfIdx1 = HalfIdx; | |||
16732 | continue; | |||
16733 | } | |||
16734 | if (HalfIdx2 < 0 || HalfIdx2 == HalfIdx) { | |||
16735 | HalfMask[i] = HalfElt + HalfNumElts; | |||
16736 | HalfIdx2 = HalfIdx; | |||
16737 | continue; | |||
16738 | } | |||
16739 | ||||
16740 | // Too many half vectors referenced. | |||
16741 | return false; | |||
16742 | } | |||
16743 | ||||
16744 | return true; | |||
16745 | } | |||
16746 | ||||
16747 | /// Given the output values from getHalfShuffleMask(), create a half width | |||
16748 | /// shuffle of extracted vectors followed by an insert back to full width. | |||
16749 | static SDValue getShuffleHalfVectors(const SDLoc &DL, SDValue V1, SDValue V2, | |||
16750 | ArrayRef<int> HalfMask, int HalfIdx1, | |||
16751 | int HalfIdx2, bool UndefLower, | |||
16752 | SelectionDAG &DAG, bool UseConcat = false) { | |||
16753 | assert(V1.getValueType() == V2.getValueType() && "Different sized vectors?")(static_cast <bool> (V1.getValueType() == V2.getValueType () && "Different sized vectors?") ? void (0) : __assert_fail ("V1.getValueType() == V2.getValueType() && \"Different sized vectors?\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 16753, __extension__ __PRETTY_FUNCTION__)); | |||
16754 | assert(V1.getValueType().isSimple() && "Expecting only simple types")(static_cast <bool> (V1.getValueType().isSimple() && "Expecting only simple types") ? void (0) : __assert_fail ("V1.getValueType().isSimple() && \"Expecting only simple types\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 16754, __extension__ __PRETTY_FUNCTION__)); | |||
16755 | ||||
16756 | MVT VT = V1.getSimpleValueType(); | |||
16757 | MVT HalfVT = VT.getHalfNumVectorElementsVT(); | |||
16758 | unsigned HalfNumElts = HalfVT.getVectorNumElements(); | |||
16759 | ||||
16760 | auto getHalfVector = [&](int HalfIdx) { | |||
16761 | if (HalfIdx < 0) | |||
16762 | return DAG.getUNDEF(HalfVT); | |||
16763 | SDValue V = (HalfIdx < 2 ? V1 : V2); | |||
16764 | HalfIdx = (HalfIdx % 2) * HalfNumElts; | |||
16765 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, HalfVT, V, | |||
16766 | DAG.getIntPtrConstant(HalfIdx, DL)); | |||
16767 | }; | |||
16768 | ||||
16769 | // ins undef, (shuf (ext V1, HalfIdx1), (ext V2, HalfIdx2), HalfMask), Offset | |||
16770 | SDValue Half1 = getHalfVector(HalfIdx1); | |||
16771 | SDValue Half2 = getHalfVector(HalfIdx2); | |||
16772 | SDValue V = DAG.getVectorShuffle(HalfVT, DL, Half1, Half2, HalfMask); | |||
16773 | if (UseConcat) { | |||
16774 | SDValue Op0 = V; | |||
16775 | SDValue Op1 = DAG.getUNDEF(HalfVT); | |||
16776 | if (UndefLower) | |||
16777 | std::swap(Op0, Op1); | |||
16778 | return DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, Op0, Op1); | |||
16779 | } | |||
16780 | ||||
16781 | unsigned Offset = UndefLower ? HalfNumElts : 0; | |||
16782 | return DAG.getNode(ISD::INSERT_SUBVECTOR, DL, VT, DAG.getUNDEF(VT), V, | |||
16783 | DAG.getIntPtrConstant(Offset, DL)); | |||
16784 | } | |||
16785 | ||||
16786 | /// Lower shuffles where an entire half of a 256 or 512-bit vector is UNDEF. | |||
16787 | /// This allows for fast cases such as subvector extraction/insertion | |||
16788 | /// or shuffling smaller vector types which can lower more efficiently. | |||
16789 | static SDValue lowerShuffleWithUndefHalf(const SDLoc &DL, MVT VT, SDValue V1, | |||
16790 | SDValue V2, ArrayRef<int> Mask, | |||
16791 | const X86Subtarget &Subtarget, | |||
16792 | SelectionDAG &DAG) { | |||
16793 | assert((VT.is256BitVector() || VT.is512BitVector()) &&(static_cast <bool> ((VT.is256BitVector() || VT.is512BitVector ()) && "Expected 256-bit or 512-bit vector") ? void ( 0) : __assert_fail ("(VT.is256BitVector() || VT.is512BitVector()) && \"Expected 256-bit or 512-bit vector\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 16794, __extension__ __PRETTY_FUNCTION__)) | |||
16794 | "Expected 256-bit or 512-bit vector")(static_cast <bool> ((VT.is256BitVector() || VT.is512BitVector ()) && "Expected 256-bit or 512-bit vector") ? void ( 0) : __assert_fail ("(VT.is256BitVector() || VT.is512BitVector()) && \"Expected 256-bit or 512-bit vector\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 16794, __extension__ __PRETTY_FUNCTION__)); | |||
16795 | ||||
16796 | bool UndefLower = isUndefLowerHalf(Mask); | |||
16797 | if (!UndefLower && !isUndefUpperHalf(Mask)) | |||
16798 | return SDValue(); | |||
16799 | ||||
16800 | assert((!UndefLower || !isUndefUpperHalf(Mask)) &&(static_cast <bool> ((!UndefLower || !isUndefUpperHalf( Mask)) && "Completely undef shuffle mask should have been simplified already" ) ? void (0) : __assert_fail ("(!UndefLower || !isUndefUpperHalf(Mask)) && \"Completely undef shuffle mask should have been simplified already\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 16801, __extension__ __PRETTY_FUNCTION__)) | |||
16801 | "Completely undef shuffle mask should have been simplified already")(static_cast <bool> ((!UndefLower || !isUndefUpperHalf( Mask)) && "Completely undef shuffle mask should have been simplified already" ) ? void (0) : __assert_fail ("(!UndefLower || !isUndefUpperHalf(Mask)) && \"Completely undef shuffle mask should have been simplified already\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 16801, __extension__ __PRETTY_FUNCTION__)); | |||
16802 | ||||
16803 | // Upper half is undef and lower half is whole upper subvector. | |||
16804 | // e.g. vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u> | |||
16805 | MVT HalfVT = VT.getHalfNumVectorElementsVT(); | |||
16806 | unsigned HalfNumElts = HalfVT.getVectorNumElements(); | |||
16807 | if (!UndefLower && | |||
16808 | isSequentialOrUndefInRange(Mask, 0, HalfNumElts, HalfNumElts)) { | |||
16809 | SDValue Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, HalfVT, V1, | |||
16810 | DAG.getIntPtrConstant(HalfNumElts, DL)); | |||
16811 | return DAG.getNode(ISD::INSERT_SUBVECTOR, DL, VT, DAG.getUNDEF(VT), Hi, | |||
16812 | DAG.getIntPtrConstant(0, DL)); | |||
16813 | } | |||
16814 | ||||
16815 | // Lower half is undef and upper half is whole lower subvector. | |||
16816 | // e.g. vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1> | |||
16817 | if (UndefLower && | |||
16818 | isSequentialOrUndefInRange(Mask, HalfNumElts, HalfNumElts, 0)) { | |||
16819 | SDValue Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, HalfVT, V1, | |||
16820 | DAG.getIntPtrConstant(0, DL)); | |||
16821 | return DAG.getNode(ISD::INSERT_SUBVECTOR, DL, VT, DAG.getUNDEF(VT), Hi, | |||
16822 | DAG.getIntPtrConstant(HalfNumElts, DL)); | |||
16823 | } | |||
16824 | ||||
16825 | int HalfIdx1, HalfIdx2; | |||
16826 | SmallVector<int, 8> HalfMask(HalfNumElts); | |||
16827 | if (!getHalfShuffleMask(Mask, HalfMask, HalfIdx1, HalfIdx2)) | |||
16828 | return SDValue(); | |||
16829 | ||||
16830 | assert(HalfMask.size() == HalfNumElts && "Unexpected shuffle mask length")(static_cast <bool> (HalfMask.size() == HalfNumElts && "Unexpected shuffle mask length") ? void (0) : __assert_fail ("HalfMask.size() == HalfNumElts && \"Unexpected shuffle mask length\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 16830, __extension__ __PRETTY_FUNCTION__)); | |||
16831 | ||||
16832 | // Only shuffle the halves of the inputs when useful. | |||
16833 | unsigned NumLowerHalves = | |||
16834 | (HalfIdx1 == 0 || HalfIdx1 == 2) + (HalfIdx2 == 0 || HalfIdx2 == 2); | |||
16835 | unsigned NumUpperHalves = | |||
16836 | (HalfIdx1 == 1 || HalfIdx1 == 3) + (HalfIdx2 == 1 || HalfIdx2 == 3); | |||
16837 | assert(NumLowerHalves + NumUpperHalves <= 2 && "Only 1 or 2 halves allowed")(static_cast <bool> (NumLowerHalves + NumUpperHalves <= 2 && "Only 1 or 2 halves allowed") ? void (0) : __assert_fail ("NumLowerHalves + NumUpperHalves <= 2 && \"Only 1 or 2 halves allowed\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 16837, __extension__ __PRETTY_FUNCTION__)); | |||
16838 | ||||
16839 | // Determine the larger pattern of undef/halves, then decide if it's worth | |||
16840 | // splitting the shuffle based on subtarget capabilities and types. | |||
16841 | unsigned EltWidth = VT.getVectorElementType().getSizeInBits(); | |||
16842 | if (!UndefLower) { | |||
16843 | // XXXXuuuu: no insert is needed. | |||
16844 | // Always extract lowers when setting lower - these are all free subreg ops. | |||
16845 | if (NumUpperHalves == 0) | |||
16846 | return getShuffleHalfVectors(DL, V1, V2, HalfMask, HalfIdx1, HalfIdx2, | |||
16847 | UndefLower, DAG); | |||
16848 | ||||
16849 | if (NumUpperHalves == 1) { | |||
16850 | // AVX2 has efficient 32/64-bit element cross-lane shuffles. | |||
16851 | if (Subtarget.hasAVX2()) { | |||
16852 | // extract128 + vunpckhps/vshufps, is better than vblend + vpermps. | |||
16853 | if (EltWidth == 32 && NumLowerHalves && HalfVT.is128BitVector() && | |||
16854 | !is128BitUnpackShuffleMask(HalfMask) && | |||
16855 | (!isSingleSHUFPSMask(HalfMask) || | |||
16856 | Subtarget.hasFastVariableCrossLaneShuffle())) | |||
16857 | return SDValue(); | |||
16858 | // If this is a unary shuffle (assume that the 2nd operand is | |||
16859 | // canonicalized to undef), then we can use vpermpd. Otherwise, we | |||
16860 | // are better off extracting the upper half of 1 operand and using a | |||
16861 | // narrow shuffle. | |||
16862 | if (EltWidth == 64 && V2.isUndef()) | |||
16863 | return SDValue(); | |||
16864 | } | |||
16865 | // AVX512 has efficient cross-lane shuffles for all legal 512-bit types. | |||
16866 | if (Subtarget.hasAVX512() && VT.is512BitVector()) | |||
16867 | return SDValue(); | |||
16868 | // Extract + narrow shuffle is better than the wide alternative. | |||
16869 | return getShuffleHalfVectors(DL, V1, V2, HalfMask, HalfIdx1, HalfIdx2, | |||
16870 | UndefLower, DAG); | |||
16871 | } | |||
16872 | ||||
16873 | // Don't extract both uppers, instead shuffle and then extract. | |||
16874 | assert(NumUpperHalves == 2 && "Half vector count went wrong")(static_cast <bool> (NumUpperHalves == 2 && "Half vector count went wrong" ) ? void (0) : __assert_fail ("NumUpperHalves == 2 && \"Half vector count went wrong\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 16874, __extension__ __PRETTY_FUNCTION__)); | |||
16875 | return SDValue(); | |||
16876 | } | |||
16877 | ||||
16878 | // UndefLower - uuuuXXXX: an insert to high half is required if we split this. | |||
16879 | if (NumUpperHalves == 0) { | |||
16880 | // AVX2 has efficient 64-bit element cross-lane shuffles. | |||
16881 | // TODO: Refine to account for unary shuffle, splat, and other masks? | |||
16882 | if (Subtarget.hasAVX2() && EltWidth == 64) | |||
16883 | return SDValue(); | |||
16884 | // AVX512 has efficient cross-lane shuffles for all legal 512-bit types. | |||
16885 | if (Subtarget.hasAVX512() && VT.is512BitVector()) | |||
16886 | return SDValue(); | |||
16887 | // Narrow shuffle + insert is better than the wide alternative. | |||
16888 | return getShuffleHalfVectors(DL, V1, V2, HalfMask, HalfIdx1, HalfIdx2, | |||
16889 | UndefLower, DAG); | |||
16890 | } | |||
16891 | ||||
16892 | // NumUpperHalves != 0: don't bother with extract, shuffle, and then insert. | |||
16893 | return SDValue(); | |||
16894 | } | |||
16895 | ||||
16896 | /// Test whether the specified input (0 or 1) is in-place blended by the | |||
16897 | /// given mask. | |||
16898 | /// | |||
16899 | /// This returns true if the elements from a particular input are already in the | |||
16900 | /// slot required by the given mask and require no permutation. | |||
16901 | static bool isShuffleMaskInputInPlace(int Input, ArrayRef<int> Mask) { | |||
16902 | assert((Input == 0 || Input == 1) && "Only two inputs to shuffles.")(static_cast <bool> ((Input == 0 || Input == 1) && "Only two inputs to shuffles.") ? void (0) : __assert_fail ( "(Input == 0 || Input == 1) && \"Only two inputs to shuffles.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 16902, __extension__ __PRETTY_FUNCTION__)); | |||
16903 | int Size = Mask.size(); | |||
16904 | for (int i = 0; i < Size; ++i) | |||
16905 | if (Mask[i] >= 0 && Mask[i] / Size == Input && Mask[i] % Size != i) | |||
16906 | return false; | |||
16907 | ||||
16908 | return true; | |||
16909 | } | |||
16910 | ||||
16911 | /// Handle case where shuffle sources are coming from the same 128-bit lane and | |||
16912 | /// every lane can be represented as the same repeating mask - allowing us to | |||
16913 | /// shuffle the sources with the repeating shuffle and then permute the result | |||
16914 | /// to the destination lanes. | |||
16915 | static SDValue lowerShuffleAsRepeatedMaskAndLanePermute( | |||
16916 | const SDLoc &DL, MVT VT, SDValue V1, SDValue V2, ArrayRef<int> Mask, | |||
16917 | const X86Subtarget &Subtarget, SelectionDAG &DAG) { | |||
16918 | int NumElts = VT.getVectorNumElements(); | |||
16919 | int NumLanes = VT.getSizeInBits() / 128; | |||
16920 | int NumLaneElts = NumElts / NumLanes; | |||
16921 | ||||
16922 | // On AVX2 we may be able to just shuffle the lowest elements and then | |||
16923 | // broadcast the result. | |||
16924 | if (Subtarget.hasAVX2()) { | |||
16925 | for (unsigned BroadcastSize : {16, 32, 64}) { | |||
16926 | if (BroadcastSize <= VT.getScalarSizeInBits()) | |||
16927 | continue; | |||
16928 | int NumBroadcastElts = BroadcastSize / VT.getScalarSizeInBits(); | |||
16929 | ||||
16930 | // Attempt to match a repeating pattern every NumBroadcastElts, | |||
16931 | // accounting for UNDEFs but only references the lowest 128-bit | |||
16932 | // lane of the inputs. | |||
16933 | auto FindRepeatingBroadcastMask = [&](SmallVectorImpl<int> &RepeatMask) { | |||
16934 | for (int i = 0; i != NumElts; i += NumBroadcastElts) | |||
16935 | for (int j = 0; j != NumBroadcastElts; ++j) { | |||
16936 | int M = Mask[i + j]; | |||
16937 | if (M < 0) | |||
16938 | continue; | |||
16939 | int &R = RepeatMask[j]; | |||
16940 | if (0 != ((M % NumElts) / NumLaneElts)) | |||
16941 | return false; | |||
16942 | if (0 <= R && R != M) | |||
16943 | return false; | |||
16944 | R = M; | |||
16945 | } | |||
16946 | return true; | |||
16947 | }; | |||
16948 | ||||
16949 | SmallVector<int, 8> RepeatMask((unsigned)NumElts, -1); | |||
16950 | if (!FindRepeatingBroadcastMask(RepeatMask)) | |||
16951 | continue; | |||
16952 | ||||
16953 | // Shuffle the (lowest) repeated elements in place for broadcast. | |||
16954 | SDValue RepeatShuf = DAG.getVectorShuffle(VT, DL, V1, V2, RepeatMask); | |||
16955 | ||||
16956 | // Shuffle the actual broadcast. | |||
16957 | SmallVector<int, 8> BroadcastMask((unsigned)NumElts, -1); | |||
16958 | for (int i = 0; i != NumElts; i += NumBroadcastElts) | |||
16959 | for (int j = 0; j != NumBroadcastElts; ++j) | |||
16960 | BroadcastMask[i + j] = j; | |||
16961 | return DAG.getVectorShuffle(VT, DL, RepeatShuf, DAG.getUNDEF(VT), | |||
16962 | BroadcastMask); | |||
16963 | } | |||
16964 | } | |||
16965 | ||||
16966 | // Bail if the shuffle mask doesn't cross 128-bit lanes. | |||
16967 | if (!is128BitLaneCrossingShuffleMask(VT, Mask)) | |||
16968 | return SDValue(); | |||
16969 | ||||
16970 | // Bail if we already have a repeated lane shuffle mask. | |||
16971 | SmallVector<int, 8> RepeatedShuffleMask; | |||
16972 | if (is128BitLaneRepeatedShuffleMask(VT, Mask, RepeatedShuffleMask)) | |||
16973 | return SDValue(); | |||
16974 | ||||
16975 | // On AVX2 targets we can permute 256-bit vectors as 64-bit sub-lanes | |||
16976 | // (with PERMQ/PERMPD), otherwise we can only permute whole 128-bit lanes. | |||
16977 | int SubLaneScale = Subtarget.hasAVX2() && VT.is256BitVector() ? 2 : 1; | |||
16978 | int NumSubLanes = NumLanes * SubLaneScale; | |||
16979 | int NumSubLaneElts = NumLaneElts / SubLaneScale; | |||
16980 | ||||
16981 | // Check that all the sources are coming from the same lane and see if we can | |||
16982 | // form a repeating shuffle mask (local to each sub-lane). At the same time, | |||
16983 | // determine the source sub-lane for each destination sub-lane. | |||
16984 | int TopSrcSubLane = -1; | |||
16985 | SmallVector<int, 8> Dst2SrcSubLanes((unsigned)NumSubLanes, -1); | |||
16986 | SmallVector<int, 8> RepeatedSubLaneMasks[2] = { | |||
16987 | SmallVector<int, 8>((unsigned)NumSubLaneElts, SM_SentinelUndef), | |||
16988 | SmallVector<int, 8>((unsigned)NumSubLaneElts, SM_SentinelUndef)}; | |||
16989 | ||||
16990 | for (int DstSubLane = 0; DstSubLane != NumSubLanes; ++DstSubLane) { | |||
16991 | // Extract the sub-lane mask, check that it all comes from the same lane | |||
16992 | // and normalize the mask entries to come from the first lane. | |||
16993 | int SrcLane = -1; | |||
16994 | SmallVector<int, 8> SubLaneMask((unsigned)NumSubLaneElts, -1); | |||
16995 | for (int Elt = 0; Elt != NumSubLaneElts; ++Elt) { | |||
16996 | int M = Mask[(DstSubLane * NumSubLaneElts) + Elt]; | |||
16997 | if (M < 0) | |||
16998 | continue; | |||
16999 | int Lane = (M % NumElts) / NumLaneElts; | |||
17000 | if ((0 <= SrcLane) && (SrcLane != Lane)) | |||
17001 | return SDValue(); | |||
17002 | SrcLane = Lane; | |||
17003 | int LocalM = (M % NumLaneElts) + (M < NumElts ? 0 : NumElts); | |||
17004 | SubLaneMask[Elt] = LocalM; | |||
17005 | } | |||
17006 | ||||
17007 | // Whole sub-lane is UNDEF. | |||
17008 | if (SrcLane < 0) | |||
17009 | continue; | |||
17010 | ||||
17011 | // Attempt to match against the candidate repeated sub-lane masks. | |||
17012 | for (int SubLane = 0; SubLane != SubLaneScale; ++SubLane) { | |||
17013 | auto MatchMasks = [NumSubLaneElts](ArrayRef<int> M1, ArrayRef<int> M2) { | |||
17014 | for (int i = 0; i != NumSubLaneElts; ++i) { | |||
17015 | if (M1[i] < 0 || M2[i] < 0) | |||
17016 | continue; | |||
17017 | if (M1[i] != M2[i]) | |||
17018 | return false; | |||
17019 | } | |||
17020 | return true; | |||
17021 | }; | |||
17022 | ||||
17023 | auto &RepeatedSubLaneMask = RepeatedSubLaneMasks[SubLane]; | |||
17024 | if (!MatchMasks(SubLaneMask, RepeatedSubLaneMask)) | |||
17025 | continue; | |||
17026 | ||||
17027 | // Merge the sub-lane mask into the matching repeated sub-lane mask. | |||
17028 | for (int i = 0; i != NumSubLaneElts; ++i) { | |||
17029 | int M = SubLaneMask[i]; | |||
17030 | if (M < 0) | |||
17031 | continue; | |||
17032 | assert((RepeatedSubLaneMask[i] < 0 || RepeatedSubLaneMask[i] == M) &&(static_cast <bool> ((RepeatedSubLaneMask[i] < 0 || RepeatedSubLaneMask [i] == M) && "Unexpected mask element") ? void (0) : __assert_fail ("(RepeatedSubLaneMask[i] < 0 || RepeatedSubLaneMask[i] == M) && \"Unexpected mask element\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17033, __extension__ __PRETTY_FUNCTION__)) | |||
17033 | "Unexpected mask element")(static_cast <bool> ((RepeatedSubLaneMask[i] < 0 || RepeatedSubLaneMask [i] == M) && "Unexpected mask element") ? void (0) : __assert_fail ("(RepeatedSubLaneMask[i] < 0 || RepeatedSubLaneMask[i] == M) && \"Unexpected mask element\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17033, __extension__ __PRETTY_FUNCTION__)); | |||
17034 | RepeatedSubLaneMask[i] = M; | |||
17035 | } | |||
17036 | ||||
17037 | // Track the top most source sub-lane - by setting the remaining to UNDEF | |||
17038 | // we can greatly simplify shuffle matching. | |||
17039 | int SrcSubLane = (SrcLane * SubLaneScale) + SubLane; | |||
17040 | TopSrcSubLane = std::max(TopSrcSubLane, SrcSubLane); | |||
17041 | Dst2SrcSubLanes[DstSubLane] = SrcSubLane; | |||
17042 | break; | |||
17043 | } | |||
17044 | ||||
17045 | // Bail if we failed to find a matching repeated sub-lane mask. | |||
17046 | if (Dst2SrcSubLanes[DstSubLane] < 0) | |||
17047 | return SDValue(); | |||
17048 | } | |||
17049 | assert(0 <= TopSrcSubLane && TopSrcSubLane < NumSubLanes &&(static_cast <bool> (0 <= TopSrcSubLane && TopSrcSubLane < NumSubLanes && "Unexpected source lane") ? void (0) : __assert_fail ("0 <= TopSrcSubLane && TopSrcSubLane < NumSubLanes && \"Unexpected source lane\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17050, __extension__ __PRETTY_FUNCTION__)) | |||
17050 | "Unexpected source lane")(static_cast <bool> (0 <= TopSrcSubLane && TopSrcSubLane < NumSubLanes && "Unexpected source lane") ? void (0) : __assert_fail ("0 <= TopSrcSubLane && TopSrcSubLane < NumSubLanes && \"Unexpected source lane\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17050, __extension__ __PRETTY_FUNCTION__)); | |||
17051 | ||||
17052 | // Create a repeating shuffle mask for the entire vector. | |||
17053 | SmallVector<int, 8> RepeatedMask((unsigned)NumElts, -1); | |||
17054 | for (int SubLane = 0; SubLane <= TopSrcSubLane; ++SubLane) { | |||
17055 | int Lane = SubLane / SubLaneScale; | |||
17056 | auto &RepeatedSubLaneMask = RepeatedSubLaneMasks[SubLane % SubLaneScale]; | |||
17057 | for (int Elt = 0; Elt != NumSubLaneElts; ++Elt) { | |||
17058 | int M = RepeatedSubLaneMask[Elt]; | |||
17059 | if (M < 0) | |||
17060 | continue; | |||
17061 | int Idx = (SubLane * NumSubLaneElts) + Elt; | |||
17062 | RepeatedMask[Idx] = M + (Lane * NumLaneElts); | |||
17063 | } | |||
17064 | } | |||
17065 | SDValue RepeatedShuffle = DAG.getVectorShuffle(VT, DL, V1, V2, RepeatedMask); | |||
17066 | ||||
17067 | // Shuffle each source sub-lane to its destination. | |||
17068 | SmallVector<int, 8> SubLaneMask((unsigned)NumElts, -1); | |||
17069 | for (int i = 0; i != NumElts; i += NumSubLaneElts) { | |||
17070 | int SrcSubLane = Dst2SrcSubLanes[i / NumSubLaneElts]; | |||
17071 | if (SrcSubLane < 0) | |||
17072 | continue; | |||
17073 | for (int j = 0; j != NumSubLaneElts; ++j) | |||
17074 | SubLaneMask[i + j] = j + (SrcSubLane * NumSubLaneElts); | |||
17075 | } | |||
17076 | ||||
17077 | return DAG.getVectorShuffle(VT, DL, RepeatedShuffle, DAG.getUNDEF(VT), | |||
17078 | SubLaneMask); | |||
17079 | } | |||
17080 | ||||
17081 | static bool matchShuffleWithSHUFPD(MVT VT, SDValue &V1, SDValue &V2, | |||
17082 | bool &ForceV1Zero, bool &ForceV2Zero, | |||
17083 | unsigned &ShuffleImm, ArrayRef<int> Mask, | |||
17084 | const APInt &Zeroable) { | |||
17085 | int NumElts = VT.getVectorNumElements(); | |||
17086 | assert(VT.getScalarSizeInBits() == 64 &&(static_cast <bool> (VT.getScalarSizeInBits() == 64 && (NumElts == 2 || NumElts == 4 || NumElts == 8) && "Unexpected data type for VSHUFPD" ) ? void (0) : __assert_fail ("VT.getScalarSizeInBits() == 64 && (NumElts == 2 || NumElts == 4 || NumElts == 8) && \"Unexpected data type for VSHUFPD\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17088, __extension__ __PRETTY_FUNCTION__)) | |||
17087 | (NumElts == 2 || NumElts == 4 || NumElts == 8) &&(static_cast <bool> (VT.getScalarSizeInBits() == 64 && (NumElts == 2 || NumElts == 4 || NumElts == 8) && "Unexpected data type for VSHUFPD" ) ? void (0) : __assert_fail ("VT.getScalarSizeInBits() == 64 && (NumElts == 2 || NumElts == 4 || NumElts == 8) && \"Unexpected data type for VSHUFPD\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17088, __extension__ __PRETTY_FUNCTION__)) | |||
17088 | "Unexpected data type for VSHUFPD")(static_cast <bool> (VT.getScalarSizeInBits() == 64 && (NumElts == 2 || NumElts == 4 || NumElts == 8) && "Unexpected data type for VSHUFPD" ) ? void (0) : __assert_fail ("VT.getScalarSizeInBits() == 64 && (NumElts == 2 || NumElts == 4 || NumElts == 8) && \"Unexpected data type for VSHUFPD\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17088, __extension__ __PRETTY_FUNCTION__)); | |||
17089 | assert(isUndefOrZeroOrInRange(Mask, 0, 2 * NumElts) &&(static_cast <bool> (isUndefOrZeroOrInRange(Mask, 0, 2 * NumElts) && "Illegal shuffle mask") ? void (0) : __assert_fail ("isUndefOrZeroOrInRange(Mask, 0, 2 * NumElts) && \"Illegal shuffle mask\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17090, __extension__ __PRETTY_FUNCTION__)) | |||
17090 | "Illegal shuffle mask")(static_cast <bool> (isUndefOrZeroOrInRange(Mask, 0, 2 * NumElts) && "Illegal shuffle mask") ? void (0) : __assert_fail ("isUndefOrZeroOrInRange(Mask, 0, 2 * NumElts) && \"Illegal shuffle mask\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17090, __extension__ __PRETTY_FUNCTION__)); | |||
17091 | ||||
17092 | bool ZeroLane[2] = { true, true }; | |||
17093 | for (int i = 0; i < NumElts; ++i) | |||
17094 | ZeroLane[i & 1] &= Zeroable[i]; | |||
17095 | ||||
17096 | // Mask for V8F64: 0/1, 8/9, 2/3, 10/11, 4/5, .. | |||
17097 | // Mask for V4F64; 0/1, 4/5, 2/3, 6/7.. | |||
17098 | ShuffleImm = 0; | |||
17099 | bool ShufpdMask = true; | |||
17100 | bool CommutableMask = true; | |||
17101 | for (int i = 0; i < NumElts; ++i) { | |||
17102 | if (Mask[i] == SM_SentinelUndef || ZeroLane[i & 1]) | |||
17103 | continue; | |||
17104 | if (Mask[i] < 0) | |||
17105 | return false; | |||
17106 | int Val = (i & 6) + NumElts * (i & 1); | |||
17107 | int CommutVal = (i & 0xe) + NumElts * ((i & 1) ^ 1); | |||
17108 | if (Mask[i] < Val || Mask[i] > Val + 1) | |||
17109 | ShufpdMask = false; | |||
17110 | if (Mask[i] < CommutVal || Mask[i] > CommutVal + 1) | |||
17111 | CommutableMask = false; | |||
17112 | ShuffleImm |= (Mask[i] % 2) << i; | |||
17113 | } | |||
17114 | ||||
17115 | if (!ShufpdMask && !CommutableMask) | |||
17116 | return false; | |||
17117 | ||||
17118 | if (!ShufpdMask && CommutableMask) | |||
17119 | std::swap(V1, V2); | |||
17120 | ||||
17121 | ForceV1Zero = ZeroLane[0]; | |||
17122 | ForceV2Zero = ZeroLane[1]; | |||
17123 | return true; | |||
17124 | } | |||
17125 | ||||
17126 | static SDValue lowerShuffleWithSHUFPD(const SDLoc &DL, MVT VT, SDValue V1, | |||
17127 | SDValue V2, ArrayRef<int> Mask, | |||
17128 | const APInt &Zeroable, | |||
17129 | const X86Subtarget &Subtarget, | |||
17130 | SelectionDAG &DAG) { | |||
17131 | assert((VT == MVT::v2f64 || VT == MVT::v4f64 || VT == MVT::v8f64) &&(static_cast <bool> ((VT == MVT::v2f64 || VT == MVT::v4f64 || VT == MVT::v8f64) && "Unexpected data type for VSHUFPD" ) ? void (0) : __assert_fail ("(VT == MVT::v2f64 || VT == MVT::v4f64 || VT == MVT::v8f64) && \"Unexpected data type for VSHUFPD\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17132, __extension__ __PRETTY_FUNCTION__)) | |||
17132 | "Unexpected data type for VSHUFPD")(static_cast <bool> ((VT == MVT::v2f64 || VT == MVT::v4f64 || VT == MVT::v8f64) && "Unexpected data type for VSHUFPD" ) ? void (0) : __assert_fail ("(VT == MVT::v2f64 || VT == MVT::v4f64 || VT == MVT::v8f64) && \"Unexpected data type for VSHUFPD\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17132, __extension__ __PRETTY_FUNCTION__)); | |||
17133 | ||||
17134 | unsigned Immediate = 0; | |||
17135 | bool ForceV1Zero = false, ForceV2Zero = false; | |||
17136 | if (!matchShuffleWithSHUFPD(VT, V1, V2, ForceV1Zero, ForceV2Zero, Immediate, | |||
17137 | Mask, Zeroable)) | |||
17138 | return SDValue(); | |||
17139 | ||||
17140 | // Create a REAL zero vector - ISD::isBuildVectorAllZeros allows UNDEFs. | |||
17141 | if (ForceV1Zero) | |||
17142 | V1 = getZeroVector(VT, Subtarget, DAG, DL); | |||
17143 | if (ForceV2Zero) | |||
17144 | V2 = getZeroVector(VT, Subtarget, DAG, DL); | |||
17145 | ||||
17146 | return DAG.getNode(X86ISD::SHUFP, DL, VT, V1, V2, | |||
17147 | DAG.getTargetConstant(Immediate, DL, MVT::i8)); | |||
17148 | } | |||
17149 | ||||
17150 | // Look for {0, 8, 16, 24, 32, 40, 48, 56 } in the first 8 elements. Followed | |||
17151 | // by zeroable elements in the remaining 24 elements. Turn this into two | |||
17152 | // vmovqb instructions shuffled together. | |||
17153 | static SDValue lowerShuffleAsVTRUNCAndUnpack(const SDLoc &DL, MVT VT, | |||
17154 | SDValue V1, SDValue V2, | |||
17155 | ArrayRef<int> Mask, | |||
17156 | const APInt &Zeroable, | |||
17157 | SelectionDAG &DAG) { | |||
17158 | assert(VT == MVT::v32i8 && "Unexpected type!")(static_cast <bool> (VT == MVT::v32i8 && "Unexpected type!" ) ? void (0) : __assert_fail ("VT == MVT::v32i8 && \"Unexpected type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17158, __extension__ __PRETTY_FUNCTION__)); | |||
17159 | ||||
17160 | // The first 8 indices should be every 8th element. | |||
17161 | if (!isSequentialOrUndefInRange(Mask, 0, 8, 0, 8)) | |||
17162 | return SDValue(); | |||
17163 | ||||
17164 | // Remaining elements need to be zeroable. | |||
17165 | if (Zeroable.countLeadingOnes() < (Mask.size() - 8)) | |||
17166 | return SDValue(); | |||
17167 | ||||
17168 | V1 = DAG.getBitcast(MVT::v4i64, V1); | |||
17169 | V2 = DAG.getBitcast(MVT::v4i64, V2); | |||
17170 | ||||
17171 | V1 = DAG.getNode(X86ISD::VTRUNC, DL, MVT::v16i8, V1); | |||
17172 | V2 = DAG.getNode(X86ISD::VTRUNC, DL, MVT::v16i8, V2); | |||
17173 | ||||
17174 | // The VTRUNCs will put 0s in the upper 12 bytes. Use them to put zeroes in | |||
17175 | // the upper bits of the result using an unpckldq. | |||
17176 | SDValue Unpack = DAG.getVectorShuffle(MVT::v16i8, DL, V1, V2, | |||
17177 | { 0, 1, 2, 3, 16, 17, 18, 19, | |||
17178 | 4, 5, 6, 7, 20, 21, 22, 23 }); | |||
17179 | // Insert the unpckldq into a zero vector to widen to v32i8. | |||
17180 | return DAG.getNode(ISD::INSERT_SUBVECTOR, DL, MVT::v32i8, | |||
17181 | DAG.getConstant(0, DL, MVT::v32i8), Unpack, | |||
17182 | DAG.getIntPtrConstant(0, DL)); | |||
17183 | } | |||
17184 | ||||
17185 | ||||
17186 | /// Handle lowering of 4-lane 64-bit floating point shuffles. | |||
17187 | /// | |||
17188 | /// Also ends up handling lowering of 4-lane 64-bit integer shuffles when AVX2 | |||
17189 | /// isn't available. | |||
17190 | static SDValue lowerV4F64Shuffle(const SDLoc &DL, ArrayRef<int> Mask, | |||
17191 | const APInt &Zeroable, SDValue V1, SDValue V2, | |||
17192 | const X86Subtarget &Subtarget, | |||
17193 | SelectionDAG &DAG) { | |||
17194 | assert(V1.getSimpleValueType() == MVT::v4f64 && "Bad operand type!")(static_cast <bool> (V1.getSimpleValueType() == MVT::v4f64 && "Bad operand type!") ? void (0) : __assert_fail ( "V1.getSimpleValueType() == MVT::v4f64 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17194, __extension__ __PRETTY_FUNCTION__)); | |||
17195 | assert(V2.getSimpleValueType() == MVT::v4f64 && "Bad operand type!")(static_cast <bool> (V2.getSimpleValueType() == MVT::v4f64 && "Bad operand type!") ? void (0) : __assert_fail ( "V2.getSimpleValueType() == MVT::v4f64 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17195, __extension__ __PRETTY_FUNCTION__)); | |||
17196 | assert(Mask.size() == 4 && "Unexpected mask size for v4 shuffle!")(static_cast <bool> (Mask.size() == 4 && "Unexpected mask size for v4 shuffle!" ) ? void (0) : __assert_fail ("Mask.size() == 4 && \"Unexpected mask size for v4 shuffle!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17196, __extension__ __PRETTY_FUNCTION__)); | |||
17197 | ||||
17198 | if (SDValue V = lowerV2X128Shuffle(DL, MVT::v4f64, V1, V2, Mask, Zeroable, | |||
17199 | Subtarget, DAG)) | |||
17200 | return V; | |||
17201 | ||||
17202 | if (V2.isUndef()) { | |||
17203 | // Check for being able to broadcast a single element. | |||
17204 | if (SDValue Broadcast = lowerShuffleAsBroadcast(DL, MVT::v4f64, V1, V2, | |||
17205 | Mask, Subtarget, DAG)) | |||
17206 | return Broadcast; | |||
17207 | ||||
17208 | // Use low duplicate instructions for masks that match their pattern. | |||
17209 | if (isShuffleEquivalent(Mask, {0, 0, 2, 2}, V1, V2)) | |||
17210 | return DAG.getNode(X86ISD::MOVDDUP, DL, MVT::v4f64, V1); | |||
17211 | ||||
17212 | if (!is128BitLaneCrossingShuffleMask(MVT::v4f64, Mask)) { | |||
17213 | // Non-half-crossing single input shuffles can be lowered with an | |||
17214 | // interleaved permutation. | |||
17215 | unsigned VPERMILPMask = (Mask[0] == 1) | ((Mask[1] == 1) << 1) | | |||
17216 | ((Mask[2] == 3) << 2) | ((Mask[3] == 3) << 3); | |||
17217 | return DAG.getNode(X86ISD::VPERMILPI, DL, MVT::v4f64, V1, | |||
17218 | DAG.getTargetConstant(VPERMILPMask, DL, MVT::i8)); | |||
17219 | } | |||
17220 | ||||
17221 | // With AVX2 we have direct support for this permutation. | |||
17222 | if (Subtarget.hasAVX2()) | |||
17223 | return DAG.getNode(X86ISD::VPERMI, DL, MVT::v4f64, V1, | |||
17224 | getV4X86ShuffleImm8ForMask(Mask, DL, DAG)); | |||
17225 | ||||
17226 | // Try to create an in-lane repeating shuffle mask and then shuffle the | |||
17227 | // results into the target lanes. | |||
17228 | if (SDValue V = lowerShuffleAsRepeatedMaskAndLanePermute( | |||
17229 | DL, MVT::v4f64, V1, V2, Mask, Subtarget, DAG)) | |||
17230 | return V; | |||
17231 | ||||
17232 | // Try to permute the lanes and then use a per-lane permute. | |||
17233 | if (SDValue V = lowerShuffleAsLanePermuteAndPermute(DL, MVT::v4f64, V1, V2, | |||
17234 | Mask, DAG, Subtarget)) | |||
17235 | return V; | |||
17236 | ||||
17237 | // Otherwise, fall back. | |||
17238 | return lowerShuffleAsLanePermuteAndShuffle(DL, MVT::v4f64, V1, V2, Mask, | |||
17239 | DAG, Subtarget); | |||
17240 | } | |||
17241 | ||||
17242 | // Use dedicated unpack instructions for masks that match their pattern. | |||
17243 | if (SDValue V = lowerShuffleWithUNPCK(DL, MVT::v4f64, Mask, V1, V2, DAG)) | |||
17244 | return V; | |||
17245 | ||||
17246 | if (SDValue Blend = lowerShuffleAsBlend(DL, MVT::v4f64, V1, V2, Mask, | |||
17247 | Zeroable, Subtarget, DAG)) | |||
17248 | return Blend; | |||
17249 | ||||
17250 | // Check if the blend happens to exactly fit that of SHUFPD. | |||
17251 | if (SDValue Op = lowerShuffleWithSHUFPD(DL, MVT::v4f64, V1, V2, Mask, | |||
17252 | Zeroable, Subtarget, DAG)) | |||
17253 | return Op; | |||
17254 | ||||
17255 | // If we have lane crossing shuffles AND they don't all come from the lower | |||
17256 | // lane elements, lower to SHUFPD(VPERM2F128(V1, V2), VPERM2F128(V1, V2)). | |||
17257 | // TODO: Handle BUILD_VECTOR sources which getVectorShuffle currently | |||
17258 | // canonicalize to a blend of splat which isn't necessary for this combine. | |||
17259 | if (is128BitLaneCrossingShuffleMask(MVT::v4f64, Mask) && | |||
17260 | !all_of(Mask, [](int M) { return M < 2 || (4 <= M && M < 6); }) && | |||
17261 | (V1.getOpcode() != ISD::BUILD_VECTOR) && | |||
17262 | (V2.getOpcode() != ISD::BUILD_VECTOR)) | |||
17263 | if (SDValue Op = lowerShuffleAsLanePermuteAndSHUFP(DL, MVT::v4f64, V1, V2, | |||
17264 | Mask, DAG)) | |||
17265 | return Op; | |||
17266 | ||||
17267 | // If we have one input in place, then we can permute the other input and | |||
17268 | // blend the result. | |||
17269 | if (isShuffleMaskInputInPlace(0, Mask) || isShuffleMaskInputInPlace(1, Mask)) | |||
17270 | return lowerShuffleAsDecomposedShuffleMerge(DL, MVT::v4f64, V1, V2, Mask, | |||
17271 | Subtarget, DAG); | |||
17272 | ||||
17273 | // Try to create an in-lane repeating shuffle mask and then shuffle the | |||
17274 | // results into the target lanes. | |||
17275 | if (SDValue V = lowerShuffleAsRepeatedMaskAndLanePermute( | |||
17276 | DL, MVT::v4f64, V1, V2, Mask, Subtarget, DAG)) | |||
17277 | return V; | |||
17278 | ||||
17279 | // Try to simplify this by merging 128-bit lanes to enable a lane-based | |||
17280 | // shuffle. However, if we have AVX2 and either inputs are already in place, | |||
17281 | // we will be able to shuffle even across lanes the other input in a single | |||
17282 | // instruction so skip this pattern. | |||
17283 | if (!(Subtarget.hasAVX2() && (isShuffleMaskInputInPlace(0, Mask) || | |||
17284 | isShuffleMaskInputInPlace(1, Mask)))) | |||
17285 | if (SDValue V = lowerShuffleAsLanePermuteAndRepeatedMask( | |||
17286 | DL, MVT::v4f64, V1, V2, Mask, Subtarget, DAG)) | |||
17287 | return V; | |||
17288 | ||||
17289 | // If we have VLX support, we can use VEXPAND. | |||
17290 | if (Subtarget.hasVLX()) | |||
17291 | if (SDValue V = lowerShuffleToEXPAND(DL, MVT::v4f64, Zeroable, Mask, V1, V2, | |||
17292 | DAG, Subtarget)) | |||
17293 | return V; | |||
17294 | ||||
17295 | // If we have AVX2 then we always want to lower with a blend because an v4 we | |||
17296 | // can fully permute the elements. | |||
17297 | if (Subtarget.hasAVX2()) | |||
17298 | return lowerShuffleAsDecomposedShuffleMerge(DL, MVT::v4f64, V1, V2, Mask, | |||
17299 | Subtarget, DAG); | |||
17300 | ||||
17301 | // Otherwise fall back on generic lowering. | |||
17302 | return lowerShuffleAsSplitOrBlend(DL, MVT::v4f64, V1, V2, Mask, | |||
17303 | Subtarget, DAG); | |||
17304 | } | |||
17305 | ||||
17306 | /// Handle lowering of 4-lane 64-bit integer shuffles. | |||
17307 | /// | |||
17308 | /// This routine is only called when we have AVX2 and thus a reasonable | |||
17309 | /// instruction set for v4i64 shuffling.. | |||
17310 | static SDValue lowerV4I64Shuffle(const SDLoc &DL, ArrayRef<int> Mask, | |||
17311 | const APInt &Zeroable, SDValue V1, SDValue V2, | |||
17312 | const X86Subtarget &Subtarget, | |||
17313 | SelectionDAG &DAG) { | |||
17314 | assert(V1.getSimpleValueType() == MVT::v4i64 && "Bad operand type!")(static_cast <bool> (V1.getSimpleValueType() == MVT::v4i64 && "Bad operand type!") ? void (0) : __assert_fail ( "V1.getSimpleValueType() == MVT::v4i64 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17314, __extension__ __PRETTY_FUNCTION__)); | |||
17315 | assert(V2.getSimpleValueType() == MVT::v4i64 && "Bad operand type!")(static_cast <bool> (V2.getSimpleValueType() == MVT::v4i64 && "Bad operand type!") ? void (0) : __assert_fail ( "V2.getSimpleValueType() == MVT::v4i64 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17315, __extension__ __PRETTY_FUNCTION__)); | |||
17316 | assert(Mask.size() == 4 && "Unexpected mask size for v4 shuffle!")(static_cast <bool> (Mask.size() == 4 && "Unexpected mask size for v4 shuffle!" ) ? void (0) : __assert_fail ("Mask.size() == 4 && \"Unexpected mask size for v4 shuffle!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17316, __extension__ __PRETTY_FUNCTION__)); | |||
17317 | assert(Subtarget.hasAVX2() && "We can only lower v4i64 with AVX2!")(static_cast <bool> (Subtarget.hasAVX2() && "We can only lower v4i64 with AVX2!" ) ? void (0) : __assert_fail ("Subtarget.hasAVX2() && \"We can only lower v4i64 with AVX2!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17317, __extension__ __PRETTY_FUNCTION__)); | |||
17318 | ||||
17319 | if (SDValue V = lowerV2X128Shuffle(DL, MVT::v4i64, V1, V2, Mask, Zeroable, | |||
17320 | Subtarget, DAG)) | |||
17321 | return V; | |||
17322 | ||||
17323 | if (SDValue Blend = lowerShuffleAsBlend(DL, MVT::v4i64, V1, V2, Mask, | |||
17324 | Zeroable, Subtarget, DAG)) | |||
17325 | return Blend; | |||
17326 | ||||
17327 | // Check for being able to broadcast a single element. | |||
17328 | if (SDValue Broadcast = lowerShuffleAsBroadcast(DL, MVT::v4i64, V1, V2, Mask, | |||
17329 | Subtarget, DAG)) | |||
17330 | return Broadcast; | |||
17331 | ||||
17332 | if (V2.isUndef()) { | |||
17333 | // When the shuffle is mirrored between the 128-bit lanes of the unit, we | |||
17334 | // can use lower latency instructions that will operate on both lanes. | |||
17335 | SmallVector<int, 2> RepeatedMask; | |||
17336 | if (is128BitLaneRepeatedShuffleMask(MVT::v4i64, Mask, RepeatedMask)) { | |||
17337 | SmallVector<int, 4> PSHUFDMask; | |||
17338 | narrowShuffleMaskElts(2, RepeatedMask, PSHUFDMask); | |||
17339 | return DAG.getBitcast( | |||
17340 | MVT::v4i64, | |||
17341 | DAG.getNode(X86ISD::PSHUFD, DL, MVT::v8i32, | |||
17342 | DAG.getBitcast(MVT::v8i32, V1), | |||
17343 | getV4X86ShuffleImm8ForMask(PSHUFDMask, DL, DAG))); | |||
17344 | } | |||
17345 | ||||
17346 | // AVX2 provides a direct instruction for permuting a single input across | |||
17347 | // lanes. | |||
17348 | return DAG.getNode(X86ISD::VPERMI, DL, MVT::v4i64, V1, | |||
17349 | getV4X86ShuffleImm8ForMask(Mask, DL, DAG)); | |||
17350 | } | |||
17351 | ||||
17352 | // Try to use shift instructions. | |||
17353 | if (SDValue Shift = lowerShuffleAsShift(DL, MVT::v4i64, V1, V2, Mask, | |||
17354 | Zeroable, Subtarget, DAG)) | |||
17355 | return Shift; | |||
17356 | ||||
17357 | // If we have VLX support, we can use VALIGN or VEXPAND. | |||
17358 | if (Subtarget.hasVLX()) { | |||
17359 | if (SDValue Rotate = lowerShuffleAsVALIGN(DL, MVT::v4i64, V1, V2, Mask, | |||
17360 | Subtarget, DAG)) | |||
17361 | return Rotate; | |||
17362 | ||||
17363 | if (SDValue V = lowerShuffleToEXPAND(DL, MVT::v4i64, Zeroable, Mask, V1, V2, | |||
17364 | DAG, Subtarget)) | |||
17365 | return V; | |||
17366 | } | |||
17367 | ||||
17368 | // Try to use PALIGNR. | |||
17369 | if (SDValue Rotate = lowerShuffleAsByteRotate(DL, MVT::v4i64, V1, V2, Mask, | |||
17370 | Subtarget, DAG)) | |||
17371 | return Rotate; | |||
17372 | ||||
17373 | // Use dedicated unpack instructions for masks that match their pattern. | |||
17374 | if (SDValue V = lowerShuffleWithUNPCK(DL, MVT::v4i64, Mask, V1, V2, DAG)) | |||
17375 | return V; | |||
17376 | ||||
17377 | // If we have one input in place, then we can permute the other input and | |||
17378 | // blend the result. | |||
17379 | if (isShuffleMaskInputInPlace(0, Mask) || isShuffleMaskInputInPlace(1, Mask)) | |||
17380 | return lowerShuffleAsDecomposedShuffleMerge(DL, MVT::v4i64, V1, V2, Mask, | |||
17381 | Subtarget, DAG); | |||
17382 | ||||
17383 | // Try to create an in-lane repeating shuffle mask and then shuffle the | |||
17384 | // results into the target lanes. | |||
17385 | if (SDValue V = lowerShuffleAsRepeatedMaskAndLanePermute( | |||
17386 | DL, MVT::v4i64, V1, V2, Mask, Subtarget, DAG)) | |||
17387 | return V; | |||
17388 | ||||
17389 | // Try to simplify this by merging 128-bit lanes to enable a lane-based | |||
17390 | // shuffle. However, if we have AVX2 and either inputs are already in place, | |||
17391 | // we will be able to shuffle even across lanes the other input in a single | |||
17392 | // instruction so skip this pattern. | |||
17393 | if (!isShuffleMaskInputInPlace(0, Mask) && | |||
17394 | !isShuffleMaskInputInPlace(1, Mask)) | |||
17395 | if (SDValue Result = lowerShuffleAsLanePermuteAndRepeatedMask( | |||
17396 | DL, MVT::v4i64, V1, V2, Mask, Subtarget, DAG)) | |||
17397 | return Result; | |||
17398 | ||||
17399 | // Otherwise fall back on generic blend lowering. | |||
17400 | return lowerShuffleAsDecomposedShuffleMerge(DL, MVT::v4i64, V1, V2, Mask, | |||
17401 | Subtarget, DAG); | |||
17402 | } | |||
17403 | ||||
17404 | /// Handle lowering of 8-lane 32-bit floating point shuffles. | |||
17405 | /// | |||
17406 | /// Also ends up handling lowering of 8-lane 32-bit integer shuffles when AVX2 | |||
17407 | /// isn't available. | |||
17408 | static SDValue lowerV8F32Shuffle(const SDLoc &DL, ArrayRef<int> Mask, | |||
17409 | const APInt &Zeroable, SDValue V1, SDValue V2, | |||
17410 | const X86Subtarget &Subtarget, | |||
17411 | SelectionDAG &DAG) { | |||
17412 | assert(V1.getSimpleValueType() == MVT::v8f32 && "Bad operand type!")(static_cast <bool> (V1.getSimpleValueType() == MVT::v8f32 && "Bad operand type!") ? void (0) : __assert_fail ( "V1.getSimpleValueType() == MVT::v8f32 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17412, __extension__ __PRETTY_FUNCTION__)); | |||
| ||||
17413 | assert(V2.getSimpleValueType() == MVT::v8f32 && "Bad operand type!")(static_cast <bool> (V2.getSimpleValueType() == MVT::v8f32 && "Bad operand type!") ? void (0) : __assert_fail ( "V2.getSimpleValueType() == MVT::v8f32 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17413, __extension__ __PRETTY_FUNCTION__)); | |||
17414 | assert(Mask.size() == 8 && "Unexpected mask size for v8 shuffle!")(static_cast <bool> (Mask.size() == 8 && "Unexpected mask size for v8 shuffle!" ) ? void (0) : __assert_fail ("Mask.size() == 8 && \"Unexpected mask size for v8 shuffle!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17414, __extension__ __PRETTY_FUNCTION__)); | |||
17415 | ||||
17416 | if (SDValue Blend = lowerShuffleAsBlend(DL, MVT::v8f32, V1, V2, Mask, | |||
17417 | Zeroable, Subtarget, DAG)) | |||
17418 | return Blend; | |||
17419 | ||||
17420 | // Check for being able to broadcast a single element. | |||
17421 | if (SDValue Broadcast = lowerShuffleAsBroadcast(DL, MVT::v8f32, V1, V2, Mask, | |||
17422 | Subtarget, DAG)) | |||
17423 | return Broadcast; | |||
17424 | ||||
17425 | // If the shuffle mask is repeated in each 128-bit lane, we have many more | |||
17426 | // options to efficiently lower the shuffle. | |||
17427 | SmallVector<int, 4> RepeatedMask; | |||
17428 | if (is128BitLaneRepeatedShuffleMask(MVT::v8f32, Mask, RepeatedMask)) { | |||
17429 | assert(RepeatedMask.size() == 4 &&(static_cast <bool> (RepeatedMask.size() == 4 && "Repeated masks must be half the mask width!") ? void (0) : __assert_fail ("RepeatedMask.size() == 4 && \"Repeated masks must be half the mask width!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17430, __extension__ __PRETTY_FUNCTION__)) | |||
17430 | "Repeated masks must be half the mask width!")(static_cast <bool> (RepeatedMask.size() == 4 && "Repeated masks must be half the mask width!") ? void (0) : __assert_fail ("RepeatedMask.size() == 4 && \"Repeated masks must be half the mask width!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17430, __extension__ __PRETTY_FUNCTION__)); | |||
17431 | ||||
17432 | // Use even/odd duplicate instructions for masks that match their pattern. | |||
17433 | if (isShuffleEquivalent(RepeatedMask, {0, 0, 2, 2}, V1, V2)) | |||
17434 | return DAG.getNode(X86ISD::MOVSLDUP, DL, MVT::v8f32, V1); | |||
17435 | if (isShuffleEquivalent(RepeatedMask, {1, 1, 3, 3}, V1, V2)) | |||
17436 | return DAG.getNode(X86ISD::MOVSHDUP, DL, MVT::v8f32, V1); | |||
17437 | ||||
17438 | if (V2.isUndef()) | |||
17439 | return DAG.getNode(X86ISD::VPERMILPI, DL, MVT::v8f32, V1, | |||
17440 | getV4X86ShuffleImm8ForMask(RepeatedMask, DL, DAG)); | |||
17441 | ||||
17442 | // Use dedicated unpack instructions for masks that match their pattern. | |||
17443 | if (SDValue V = lowerShuffleWithUNPCK(DL, MVT::v8f32, Mask, V1, V2, DAG)) | |||
17444 | return V; | |||
17445 | ||||
17446 | // Otherwise, fall back to a SHUFPS sequence. Here it is important that we | |||
17447 | // have already handled any direct blends. | |||
17448 | return lowerShuffleWithSHUFPS(DL, MVT::v8f32, RepeatedMask, V1, V2, DAG); | |||
17449 | } | |||
17450 | ||||
17451 | // Try to create an in-lane repeating shuffle mask and then shuffle the | |||
17452 | // results into the target lanes. | |||
17453 | if (SDValue V = lowerShuffleAsRepeatedMaskAndLanePermute( | |||
17454 | DL, MVT::v8f32, V1, V2, Mask, Subtarget, DAG)) | |||
17455 | return V; | |||
17456 | ||||
17457 | // If we have a single input shuffle with different shuffle patterns in the | |||
17458 | // two 128-bit lanes use the variable mask to VPERMILPS. | |||
17459 | if (V2.isUndef()) { | |||
17460 | if (!is128BitLaneCrossingShuffleMask(MVT::v8f32, Mask)) { | |||
17461 | SDValue VPermMask = getConstVector(Mask, MVT::v8i32, DAG, DL, true); | |||
17462 | return DAG.getNode(X86ISD::VPERMILPV, DL, MVT::v8f32, V1, VPermMask); | |||
17463 | } | |||
17464 | if (Subtarget.hasAVX2()) { | |||
17465 | SDValue VPermMask = getConstVector(Mask, MVT::v8i32, DAG, DL, true); | |||
17466 | return DAG.getNode(X86ISD::VPERMV, DL, MVT::v8f32, VPermMask, V1); | |||
17467 | } | |||
17468 | // Otherwise, fall back. | |||
17469 | return lowerShuffleAsLanePermuteAndShuffle(DL, MVT::v8f32, V1, V2, Mask, | |||
17470 | DAG, Subtarget); | |||
17471 | } | |||
17472 | ||||
17473 | // Try to simplify this by merging 128-bit lanes to enable a lane-based | |||
17474 | // shuffle. | |||
17475 | if (SDValue Result = lowerShuffleAsLanePermuteAndRepeatedMask( | |||
17476 | DL, MVT::v8f32, V1, V2, Mask, Subtarget, DAG)) | |||
17477 | return Result; | |||
17478 | ||||
17479 | // If we have VLX support, we can use VEXPAND. | |||
17480 | if (Subtarget.hasVLX()) | |||
17481 | if (SDValue V = lowerShuffleToEXPAND(DL, MVT::v8f32, Zeroable, Mask, V1, V2, | |||
17482 | DAG, Subtarget)) | |||
17483 | return V; | |||
17484 | ||||
17485 | // For non-AVX512 if the Mask is of 16bit elements in lane then try to split | |||
17486 | // since after split we get a more efficient code using vpunpcklwd and | |||
17487 | // vpunpckhwd instrs than vblend. | |||
17488 | if (!Subtarget.hasAVX512() && isUnpackWdShuffleMask(Mask, MVT::v8f32)) | |||
17489 | return lowerShuffleAsSplitOrBlend(DL, MVT::v8f32, V1, V2, Mask, Subtarget, | |||
17490 | DAG); | |||
17491 | ||||
17492 | // If we have AVX2 then we always want to lower with a blend because at v8 we | |||
17493 | // can fully permute the elements. | |||
17494 | if (Subtarget.hasAVX2()) | |||
17495 | return lowerShuffleAsDecomposedShuffleMerge(DL, MVT::v8f32, V1, V2, Mask, | |||
17496 | Subtarget, DAG); | |||
17497 | ||||
17498 | // Otherwise fall back on generic lowering. | |||
17499 | return lowerShuffleAsSplitOrBlend(DL, MVT::v8f32, V1, V2, Mask, | |||
17500 | Subtarget, DAG); | |||
17501 | } | |||
17502 | ||||
17503 | /// Handle lowering of 8-lane 32-bit integer shuffles. | |||
17504 | /// | |||
17505 | /// This routine is only called when we have AVX2 and thus a reasonable | |||
17506 | /// instruction set for v8i32 shuffling.. | |||
17507 | static SDValue lowerV8I32Shuffle(const SDLoc &DL, ArrayRef<int> Mask, | |||
17508 | const APInt &Zeroable, SDValue V1, SDValue V2, | |||
17509 | const X86Subtarget &Subtarget, | |||
17510 | SelectionDAG &DAG) { | |||
17511 | assert(V1.getSimpleValueType() == MVT::v8i32 && "Bad operand type!")(static_cast <bool> (V1.getSimpleValueType() == MVT::v8i32 && "Bad operand type!") ? void (0) : __assert_fail ( "V1.getSimpleValueType() == MVT::v8i32 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17511, __extension__ __PRETTY_FUNCTION__)); | |||
17512 | assert(V2.getSimpleValueType() == MVT::v8i32 && "Bad operand type!")(static_cast <bool> (V2.getSimpleValueType() == MVT::v8i32 && "Bad operand type!") ? void (0) : __assert_fail ( "V2.getSimpleValueType() == MVT::v8i32 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17512, __extension__ __PRETTY_FUNCTION__)); | |||
17513 | assert(Mask.size() == 8 && "Unexpected mask size for v8 shuffle!")(static_cast <bool> (Mask.size() == 8 && "Unexpected mask size for v8 shuffle!" ) ? void (0) : __assert_fail ("Mask.size() == 8 && \"Unexpected mask size for v8 shuffle!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17513, __extension__ __PRETTY_FUNCTION__)); | |||
17514 | assert(Subtarget.hasAVX2() && "We can only lower v8i32 with AVX2!")(static_cast <bool> (Subtarget.hasAVX2() && "We can only lower v8i32 with AVX2!" ) ? void (0) : __assert_fail ("Subtarget.hasAVX2() && \"We can only lower v8i32 with AVX2!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17514, __extension__ __PRETTY_FUNCTION__)); | |||
17515 | ||||
17516 | // Whenever we can lower this as a zext, that instruction is strictly faster | |||
17517 | // than any alternative. It also allows us to fold memory operands into the | |||
17518 | // shuffle in many cases. | |||
17519 | if (SDValue ZExt = lowerShuffleAsZeroOrAnyExtend(DL, MVT::v8i32, V1, V2, Mask, | |||
17520 | Zeroable, Subtarget, DAG)) | |||
17521 | return ZExt; | |||
17522 | ||||
17523 | // For non-AVX512 if the Mask is of 16bit elements in lane then try to split | |||
17524 | // since after split we get a more efficient code than vblend by using | |||
17525 | // vpunpcklwd and vpunpckhwd instrs. | |||
17526 | if (isUnpackWdShuffleMask(Mask, MVT::v8i32) && !V2.isUndef() && | |||
17527 | !Subtarget.hasAVX512()) | |||
17528 | return lowerShuffleAsSplitOrBlend(DL, MVT::v8i32, V1, V2, Mask, Subtarget, | |||
17529 | DAG); | |||
17530 | ||||
17531 | if (SDValue Blend = lowerShuffleAsBlend(DL, MVT::v8i32, V1, V2, Mask, | |||
17532 | Zeroable, Subtarget, DAG)) | |||
17533 | return Blend; | |||
17534 | ||||
17535 | // Check for being able to broadcast a single element. | |||
17536 | if (SDValue Broadcast = lowerShuffleAsBroadcast(DL, MVT::v8i32, V1, V2, Mask, | |||
17537 | Subtarget, DAG)) | |||
17538 | return Broadcast; | |||
17539 | ||||
17540 | // If the shuffle mask is repeated in each 128-bit lane we can use more | |||
17541 | // efficient instructions that mirror the shuffles across the two 128-bit | |||
17542 | // lanes. | |||
17543 | SmallVector<int, 4> RepeatedMask; | |||
17544 | bool Is128BitLaneRepeatedShuffle = | |||
17545 | is128BitLaneRepeatedShuffleMask(MVT::v8i32, Mask, RepeatedMask); | |||
17546 | if (Is128BitLaneRepeatedShuffle) { | |||
17547 | assert(RepeatedMask.size() == 4 && "Unexpected repeated mask size!")(static_cast <bool> (RepeatedMask.size() == 4 && "Unexpected repeated mask size!") ? void (0) : __assert_fail ("RepeatedMask.size() == 4 && \"Unexpected repeated mask size!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17547, __extension__ __PRETTY_FUNCTION__)); | |||
17548 | if (V2.isUndef()) | |||
17549 | return DAG.getNode(X86ISD::PSHUFD, DL, MVT::v8i32, V1, | |||
17550 | getV4X86ShuffleImm8ForMask(RepeatedMask, DL, DAG)); | |||
17551 | ||||
17552 | // Use dedicated unpack instructions for masks that match their pattern. | |||
17553 | if (SDValue V = lowerShuffleWithUNPCK(DL, MVT::v8i32, Mask, V1, V2, DAG)) | |||
17554 | return V; | |||
17555 | } | |||
17556 | ||||
17557 | // Try to use shift instructions. | |||
17558 | if (SDValue Shift = lowerShuffleAsShift(DL, MVT::v8i32, V1, V2, Mask, | |||
17559 | Zeroable, Subtarget, DAG)) | |||
17560 | return Shift; | |||
17561 | ||||
17562 | // If we have VLX support, we can use VALIGN or EXPAND. | |||
17563 | if (Subtarget.hasVLX()) { | |||
17564 | if (SDValue Rotate = lowerShuffleAsVALIGN(DL, MVT::v8i32, V1, V2, Mask, | |||
17565 | Subtarget, DAG)) | |||
17566 | return Rotate; | |||
17567 | ||||
17568 | if (SDValue V = lowerShuffleToEXPAND(DL, MVT::v8i32, Zeroable, Mask, V1, V2, | |||
17569 | DAG, Subtarget)) | |||
17570 | return V; | |||
17571 | } | |||
17572 | ||||
17573 | // Try to use byte rotation instructions. | |||
17574 | if (SDValue Rotate = lowerShuffleAsByteRotate(DL, MVT::v8i32, V1, V2, Mask, | |||
17575 | Subtarget, DAG)) | |||
17576 | return Rotate; | |||
17577 | ||||
17578 | // Try to create an in-lane repeating shuffle mask and then shuffle the | |||
17579 | // results into the target lanes. | |||
17580 | if (SDValue V = lowerShuffleAsRepeatedMaskAndLanePermute( | |||
17581 | DL, MVT::v8i32, V1, V2, Mask, Subtarget, DAG)) | |||
17582 | return V; | |||
17583 | ||||
17584 | if (V2.isUndef()) { | |||
17585 | // Try to produce a fixed cross-128-bit lane permute followed by unpack | |||
17586 | // because that should be faster than the variable permute alternatives. | |||
17587 | if (SDValue V = lowerShuffleWithUNPCK256(DL, MVT::v8i32, Mask, V1, V2, DAG)) | |||
17588 | return V; | |||
17589 | ||||
17590 | // If the shuffle patterns aren't repeated but it's a single input, directly | |||
17591 | // generate a cross-lane VPERMD instruction. | |||
17592 | SDValue VPermMask = getConstVector(Mask, MVT::v8i32, DAG, DL, true); | |||
17593 | return DAG.getNode(X86ISD::VPERMV, DL, MVT::v8i32, VPermMask, V1); | |||
17594 | } | |||
17595 | ||||
17596 | // Assume that a single SHUFPS is faster than an alternative sequence of | |||
17597 | // multiple instructions (even if the CPU has a domain penalty). | |||
17598 | // If some CPU is harmed by the domain switch, we can fix it in a later pass. | |||
17599 | if (Is128BitLaneRepeatedShuffle && isSingleSHUFPSMask(RepeatedMask)) { | |||
17600 | SDValue CastV1 = DAG.getBitcast(MVT::v8f32, V1); | |||
17601 | SDValue CastV2 = DAG.getBitcast(MVT::v8f32, V2); | |||
17602 | SDValue ShufPS = lowerShuffleWithSHUFPS(DL, MVT::v8f32, RepeatedMask, | |||
17603 | CastV1, CastV2, DAG); | |||
17604 | return DAG.getBitcast(MVT::v8i32, ShufPS); | |||
17605 | } | |||
17606 | ||||
17607 | // Try to simplify this by merging 128-bit lanes to enable a lane-based | |||
17608 | // shuffle. | |||
17609 | if (SDValue Result = lowerShuffleAsLanePermuteAndRepeatedMask( | |||
17610 | DL, MVT::v8i32, V1, V2, Mask, Subtarget, DAG)) | |||
17611 | return Result; | |||
17612 | ||||
17613 | // Otherwise fall back on generic blend lowering. | |||
17614 | return lowerShuffleAsDecomposedShuffleMerge(DL, MVT::v8i32, V1, V2, Mask, | |||
17615 | Subtarget, DAG); | |||
17616 | } | |||
17617 | ||||
17618 | /// Handle lowering of 16-lane 16-bit integer shuffles. | |||
17619 | /// | |||
17620 | /// This routine is only called when we have AVX2 and thus a reasonable | |||
17621 | /// instruction set for v16i16 shuffling.. | |||
17622 | static SDValue lowerV16I16Shuffle(const SDLoc &DL, ArrayRef<int> Mask, | |||
17623 | const APInt &Zeroable, SDValue V1, SDValue V2, | |||
17624 | const X86Subtarget &Subtarget, | |||
17625 | SelectionDAG &DAG) { | |||
17626 | assert(V1.getSimpleValueType() == MVT::v16i16 && "Bad operand type!")(static_cast <bool> (V1.getSimpleValueType() == MVT::v16i16 && "Bad operand type!") ? void (0) : __assert_fail ( "V1.getSimpleValueType() == MVT::v16i16 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17626, __extension__ __PRETTY_FUNCTION__)); | |||
17627 | assert(V2.getSimpleValueType() == MVT::v16i16 && "Bad operand type!")(static_cast <bool> (V2.getSimpleValueType() == MVT::v16i16 && "Bad operand type!") ? void (0) : __assert_fail ( "V2.getSimpleValueType() == MVT::v16i16 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17627, __extension__ __PRETTY_FUNCTION__)); | |||
17628 | assert(Mask.size() == 16 && "Unexpected mask size for v16 shuffle!")(static_cast <bool> (Mask.size() == 16 && "Unexpected mask size for v16 shuffle!" ) ? void (0) : __assert_fail ("Mask.size() == 16 && \"Unexpected mask size for v16 shuffle!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17628, __extension__ __PRETTY_FUNCTION__)); | |||
17629 | assert(Subtarget.hasAVX2() && "We can only lower v16i16 with AVX2!")(static_cast <bool> (Subtarget.hasAVX2() && "We can only lower v16i16 with AVX2!" ) ? void (0) : __assert_fail ("Subtarget.hasAVX2() && \"We can only lower v16i16 with AVX2!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17629, __extension__ __PRETTY_FUNCTION__)); | |||
17630 | ||||
17631 | // Whenever we can lower this as a zext, that instruction is strictly faster | |||
17632 | // than any alternative. It also allows us to fold memory operands into the | |||
17633 | // shuffle in many cases. | |||
17634 | if (SDValue ZExt = lowerShuffleAsZeroOrAnyExtend( | |||
17635 | DL, MVT::v16i16, V1, V2, Mask, Zeroable, Subtarget, DAG)) | |||
17636 | return ZExt; | |||
17637 | ||||
17638 | // Check for being able to broadcast a single element. | |||
17639 | if (SDValue Broadcast = lowerShuffleAsBroadcast(DL, MVT::v16i16, V1, V2, Mask, | |||
17640 | Subtarget, DAG)) | |||
17641 | return Broadcast; | |||
17642 | ||||
17643 | if (SDValue Blend = lowerShuffleAsBlend(DL, MVT::v16i16, V1, V2, Mask, | |||
17644 | Zeroable, Subtarget, DAG)) | |||
17645 | return Blend; | |||
17646 | ||||
17647 | // Use dedicated unpack instructions for masks that match their pattern. | |||
17648 | if (SDValue V = lowerShuffleWithUNPCK(DL, MVT::v16i16, Mask, V1, V2, DAG)) | |||
17649 | return V; | |||
17650 | ||||
17651 | // Use dedicated pack instructions for masks that match their pattern. | |||
17652 | if (SDValue V = lowerShuffleWithPACK(DL, MVT::v16i16, Mask, V1, V2, DAG, | |||
17653 | Subtarget)) | |||
17654 | return V; | |||
17655 | ||||
17656 | // Try to use lower using a truncation. | |||
17657 | if (SDValue V = lowerShuffleAsVTRUNC(DL, MVT::v16i16, V1, V2, Mask, Zeroable, | |||
17658 | Subtarget, DAG)) | |||
17659 | return V; | |||
17660 | ||||
17661 | // Try to use shift instructions. | |||
17662 | if (SDValue Shift = lowerShuffleAsShift(DL, MVT::v16i16, V1, V2, Mask, | |||
17663 | Zeroable, Subtarget, DAG)) | |||
17664 | return Shift; | |||
17665 | ||||
17666 | // Try to use byte rotation instructions. | |||
17667 | if (SDValue Rotate = lowerShuffleAsByteRotate(DL, MVT::v16i16, V1, V2, Mask, | |||
17668 | Subtarget, DAG)) | |||
17669 | return Rotate; | |||
17670 | ||||
17671 | // Try to create an in-lane repeating shuffle mask and then shuffle the | |||
17672 | // results into the target lanes. | |||
17673 | if (SDValue V = lowerShuffleAsRepeatedMaskAndLanePermute( | |||
17674 | DL, MVT::v16i16, V1, V2, Mask, Subtarget, DAG)) | |||
17675 | return V; | |||
17676 | ||||
17677 | if (V2.isUndef()) { | |||
17678 | // Try to use bit rotation instructions. | |||
17679 | if (SDValue Rotate = | |||
17680 | lowerShuffleAsBitRotate(DL, MVT::v16i16, V1, Mask, Subtarget, DAG)) | |||
17681 | return Rotate; | |||
17682 | ||||
17683 | // Try to produce a fixed cross-128-bit lane permute followed by unpack | |||
17684 | // because that should be faster than the variable permute alternatives. | |||
17685 | if (SDValue V = lowerShuffleWithUNPCK256(DL, MVT::v16i16, Mask, V1, V2, DAG)) | |||
17686 | return V; | |||
17687 | ||||
17688 | // There are no generalized cross-lane shuffle operations available on i16 | |||
17689 | // element types. | |||
17690 | if (is128BitLaneCrossingShuffleMask(MVT::v16i16, Mask)) { | |||
17691 | if (SDValue V = lowerShuffleAsLanePermuteAndPermute( | |||
17692 | DL, MVT::v16i16, V1, V2, Mask, DAG, Subtarget)) | |||
17693 | return V; | |||
17694 | ||||
17695 | return lowerShuffleAsLanePermuteAndShuffle(DL, MVT::v16i16, V1, V2, Mask, | |||
17696 | DAG, Subtarget); | |||
17697 | } | |||
17698 | ||||
17699 | SmallVector<int, 8> RepeatedMask; | |||
17700 | if (is128BitLaneRepeatedShuffleMask(MVT::v16i16, Mask, RepeatedMask)) { | |||
17701 | // As this is a single-input shuffle, the repeated mask should be | |||
17702 | // a strictly valid v8i16 mask that we can pass through to the v8i16 | |||
17703 | // lowering to handle even the v16 case. | |||
17704 | return lowerV8I16GeneralSingleInputShuffle( | |||
17705 | DL, MVT::v16i16, V1, RepeatedMask, Subtarget, DAG); | |||
17706 | } | |||
17707 | } | |||
17708 | ||||
17709 | if (SDValue PSHUFB = lowerShuffleWithPSHUFB(DL, MVT::v16i16, Mask, V1, V2, | |||
17710 | Zeroable, Subtarget, DAG)) | |||
17711 | return PSHUFB; | |||
17712 | ||||
17713 | // AVX512BW can lower to VPERMW (non-VLX will pad to v32i16). | |||
17714 | if (Subtarget.hasBWI()) | |||
17715 | return lowerShuffleWithPERMV(DL, MVT::v16i16, Mask, V1, V2, Subtarget, DAG); | |||
17716 | ||||
17717 | // Try to simplify this by merging 128-bit lanes to enable a lane-based | |||
17718 | // shuffle. | |||
17719 | if (SDValue Result = lowerShuffleAsLanePermuteAndRepeatedMask( | |||
17720 | DL, MVT::v16i16, V1, V2, Mask, Subtarget, DAG)) | |||
17721 | return Result; | |||
17722 | ||||
17723 | // Try to permute the lanes and then use a per-lane permute. | |||
17724 | if (SDValue V = lowerShuffleAsLanePermuteAndPermute( | |||
17725 | DL, MVT::v16i16, V1, V2, Mask, DAG, Subtarget)) | |||
17726 | return V; | |||
17727 | ||||
17728 | // Otherwise fall back on generic lowering. | |||
17729 | return lowerShuffleAsSplitOrBlend(DL, MVT::v16i16, V1, V2, Mask, | |||
17730 | Subtarget, DAG); | |||
17731 | } | |||
17732 | ||||
17733 | /// Handle lowering of 32-lane 8-bit integer shuffles. | |||
17734 | /// | |||
17735 | /// This routine is only called when we have AVX2 and thus a reasonable | |||
17736 | /// instruction set for v32i8 shuffling.. | |||
17737 | static SDValue lowerV32I8Shuffle(const SDLoc &DL, ArrayRef<int> Mask, | |||
17738 | const APInt &Zeroable, SDValue V1, SDValue V2, | |||
17739 | const X86Subtarget &Subtarget, | |||
17740 | SelectionDAG &DAG) { | |||
17741 | assert(V1.getSimpleValueType() == MVT::v32i8 && "Bad operand type!")(static_cast <bool> (V1.getSimpleValueType() == MVT::v32i8 && "Bad operand type!") ? void (0) : __assert_fail ( "V1.getSimpleValueType() == MVT::v32i8 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17741, __extension__ __PRETTY_FUNCTION__)); | |||
17742 | assert(V2.getSimpleValueType() == MVT::v32i8 && "Bad operand type!")(static_cast <bool> (V2.getSimpleValueType() == MVT::v32i8 && "Bad operand type!") ? void (0) : __assert_fail ( "V2.getSimpleValueType() == MVT::v32i8 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17742, __extension__ __PRETTY_FUNCTION__)); | |||
17743 | assert(Mask.size() == 32 && "Unexpected mask size for v32 shuffle!")(static_cast <bool> (Mask.size() == 32 && "Unexpected mask size for v32 shuffle!" ) ? void (0) : __assert_fail ("Mask.size() == 32 && \"Unexpected mask size for v32 shuffle!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17743, __extension__ __PRETTY_FUNCTION__)); | |||
17744 | assert(Subtarget.hasAVX2() && "We can only lower v32i8 with AVX2!")(static_cast <bool> (Subtarget.hasAVX2() && "We can only lower v32i8 with AVX2!" ) ? void (0) : __assert_fail ("Subtarget.hasAVX2() && \"We can only lower v32i8 with AVX2!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17744, __extension__ __PRETTY_FUNCTION__)); | |||
17745 | ||||
17746 | // Whenever we can lower this as a zext, that instruction is strictly faster | |||
17747 | // than any alternative. It also allows us to fold memory operands into the | |||
17748 | // shuffle in many cases. | |||
17749 | if (SDValue ZExt = lowerShuffleAsZeroOrAnyExtend(DL, MVT::v32i8, V1, V2, Mask, | |||
17750 | Zeroable, Subtarget, DAG)) | |||
17751 | return ZExt; | |||
17752 | ||||
17753 | // Check for being able to broadcast a single element. | |||
17754 | if (SDValue Broadcast = lowerShuffleAsBroadcast(DL, MVT::v32i8, V1, V2, Mask, | |||
17755 | Subtarget, DAG)) | |||
17756 | return Broadcast; | |||
17757 | ||||
17758 | if (SDValue Blend = lowerShuffleAsBlend(DL, MVT::v32i8, V1, V2, Mask, | |||
17759 | Zeroable, Subtarget, DAG)) | |||
17760 | return Blend; | |||
17761 | ||||
17762 | // Use dedicated unpack instructions for masks that match their pattern. | |||
17763 | if (SDValue V = lowerShuffleWithUNPCK(DL, MVT::v32i8, Mask, V1, V2, DAG)) | |||
17764 | return V; | |||
17765 | ||||
17766 | // Use dedicated pack instructions for masks that match their pattern. | |||
17767 | if (SDValue V = lowerShuffleWithPACK(DL, MVT::v32i8, Mask, V1, V2, DAG, | |||
17768 | Subtarget)) | |||
17769 | return V; | |||
17770 | ||||
17771 | // Try to use lower using a truncation. | |||
17772 | if (SDValue V = lowerShuffleAsVTRUNC(DL, MVT::v32i8, V1, V2, Mask, Zeroable, | |||
17773 | Subtarget, DAG)) | |||
17774 | return V; | |||
17775 | ||||
17776 | // Try to use shift instructions. | |||
17777 | if (SDValue Shift = lowerShuffleAsShift(DL, MVT::v32i8, V1, V2, Mask, | |||
17778 | Zeroable, Subtarget, DAG)) | |||
17779 | return Shift; | |||
17780 | ||||
17781 | // Try to use byte rotation instructions. | |||
17782 | if (SDValue Rotate = lowerShuffleAsByteRotate(DL, MVT::v32i8, V1, V2, Mask, | |||
17783 | Subtarget, DAG)) | |||
17784 | return Rotate; | |||
17785 | ||||
17786 | // Try to use bit rotation instructions. | |||
17787 | if (V2.isUndef()) | |||
17788 | if (SDValue Rotate = | |||
17789 | lowerShuffleAsBitRotate(DL, MVT::v32i8, V1, Mask, Subtarget, DAG)) | |||
17790 | return Rotate; | |||
17791 | ||||
17792 | // Try to create an in-lane repeating shuffle mask and then shuffle the | |||
17793 | // results into the target lanes. | |||
17794 | if (SDValue V = lowerShuffleAsRepeatedMaskAndLanePermute( | |||
17795 | DL, MVT::v32i8, V1, V2, Mask, Subtarget, DAG)) | |||
17796 | return V; | |||
17797 | ||||
17798 | // There are no generalized cross-lane shuffle operations available on i8 | |||
17799 | // element types. | |||
17800 | if (V2.isUndef() && is128BitLaneCrossingShuffleMask(MVT::v32i8, Mask)) { | |||
17801 | // Try to produce a fixed cross-128-bit lane permute followed by unpack | |||
17802 | // because that should be faster than the variable permute alternatives. | |||
17803 | if (SDValue V = lowerShuffleWithUNPCK256(DL, MVT::v32i8, Mask, V1, V2, DAG)) | |||
17804 | return V; | |||
17805 | ||||
17806 | if (SDValue V = lowerShuffleAsLanePermuteAndPermute( | |||
17807 | DL, MVT::v32i8, V1, V2, Mask, DAG, Subtarget)) | |||
17808 | return V; | |||
17809 | ||||
17810 | return lowerShuffleAsLanePermuteAndShuffle(DL, MVT::v32i8, V1, V2, Mask, | |||
17811 | DAG, Subtarget); | |||
17812 | } | |||
17813 | ||||
17814 | if (SDValue PSHUFB = lowerShuffleWithPSHUFB(DL, MVT::v32i8, Mask, V1, V2, | |||
17815 | Zeroable, Subtarget, DAG)) | |||
17816 | return PSHUFB; | |||
17817 | ||||
17818 | // AVX512VBMI can lower to VPERMB (non-VLX will pad to v64i8). | |||
17819 | if (Subtarget.hasVBMI()) | |||
17820 | return lowerShuffleWithPERMV(DL, MVT::v32i8, Mask, V1, V2, Subtarget, DAG); | |||
17821 | ||||
17822 | // Try to simplify this by merging 128-bit lanes to enable a lane-based | |||
17823 | // shuffle. | |||
17824 | if (SDValue Result = lowerShuffleAsLanePermuteAndRepeatedMask( | |||
17825 | DL, MVT::v32i8, V1, V2, Mask, Subtarget, DAG)) | |||
17826 | return Result; | |||
17827 | ||||
17828 | // Try to permute the lanes and then use a per-lane permute. | |||
17829 | if (SDValue V = lowerShuffleAsLanePermuteAndPermute( | |||
17830 | DL, MVT::v32i8, V1, V2, Mask, DAG, Subtarget)) | |||
17831 | return V; | |||
17832 | ||||
17833 | // Look for {0, 8, 16, 24, 32, 40, 48, 56 } in the first 8 elements. Followed | |||
17834 | // by zeroable elements in the remaining 24 elements. Turn this into two | |||
17835 | // vmovqb instructions shuffled together. | |||
17836 | if (Subtarget.hasVLX()) | |||
17837 | if (SDValue V = lowerShuffleAsVTRUNCAndUnpack(DL, MVT::v32i8, V1, V2, | |||
17838 | Mask, Zeroable, DAG)) | |||
17839 | return V; | |||
17840 | ||||
17841 | // Otherwise fall back on generic lowering. | |||
17842 | return lowerShuffleAsSplitOrBlend(DL, MVT::v32i8, V1, V2, Mask, | |||
17843 | Subtarget, DAG); | |||
17844 | } | |||
17845 | ||||
17846 | /// High-level routine to lower various 256-bit x86 vector shuffles. | |||
17847 | /// | |||
17848 | /// This routine either breaks down the specific type of a 256-bit x86 vector | |||
17849 | /// shuffle or splits it into two 128-bit shuffles and fuses the results back | |||
17850 | /// together based on the available instructions. | |||
17851 | static SDValue lower256BitShuffle(const SDLoc &DL, ArrayRef<int> Mask, MVT VT, | |||
17852 | SDValue V1, SDValue V2, const APInt &Zeroable, | |||
17853 | const X86Subtarget &Subtarget, | |||
17854 | SelectionDAG &DAG) { | |||
17855 | // If we have a single input to the zero element, insert that into V1 if we | |||
17856 | // can do so cheaply. | |||
17857 | int NumElts = VT.getVectorNumElements(); | |||
17858 | int NumV2Elements = count_if(Mask, [NumElts](int M) { return M >= NumElts; }); | |||
17859 | ||||
17860 | if (NumV2Elements == 1 && Mask[0] >= NumElts) | |||
17861 | if (SDValue Insertion = lowerShuffleAsElementInsertion( | |||
17862 | DL, VT, V1, V2, Mask, Zeroable, Subtarget, DAG)) | |||
17863 | return Insertion; | |||
17864 | ||||
17865 | // Handle special cases where the lower or upper half is UNDEF. | |||
17866 | if (SDValue V = | |||
17867 | lowerShuffleWithUndefHalf(DL, VT, V1, V2, Mask, Subtarget, DAG)) | |||
17868 | return V; | |||
17869 | ||||
17870 | // There is a really nice hard cut-over between AVX1 and AVX2 that means we | |||
17871 | // can check for those subtargets here and avoid much of the subtarget | |||
17872 | // querying in the per-vector-type lowering routines. With AVX1 we have | |||
17873 | // essentially *zero* ability to manipulate a 256-bit vector with integer | |||
17874 | // types. Since we'll use floating point types there eventually, just | |||
17875 | // immediately cast everything to a float and operate entirely in that domain. | |||
17876 | if (VT.isInteger() && !Subtarget.hasAVX2()) { | |||
17877 | int ElementBits = VT.getScalarSizeInBits(); | |||
17878 | if (ElementBits < 32) { | |||
17879 | // No floating point type available, if we can't use the bit operations | |||
17880 | // for masking/blending then decompose into 128-bit vectors. | |||
17881 | if (SDValue V = lowerShuffleAsBitMask(DL, VT, V1, V2, Mask, Zeroable, | |||
17882 | Subtarget, DAG)) | |||
17883 | return V; | |||
17884 | if (SDValue V = lowerShuffleAsBitBlend(DL, VT, V1, V2, Mask, DAG)) | |||
17885 | return V; | |||
17886 | return splitAndLowerShuffle(DL, VT, V1, V2, Mask, DAG); | |||
17887 | } | |||
17888 | ||||
17889 | MVT FpVT = MVT::getVectorVT(MVT::getFloatingPointVT(ElementBits), | |||
17890 | VT.getVectorNumElements()); | |||
17891 | V1 = DAG.getBitcast(FpVT, V1); | |||
17892 | V2 = DAG.getBitcast(FpVT, V2); | |||
17893 | return DAG.getBitcast(VT, DAG.getVectorShuffle(FpVT, DL, V1, V2, Mask)); | |||
17894 | } | |||
17895 | ||||
17896 | if (VT == MVT::v16f16) { | |||
17897 | V1 = DAG.getBitcast(MVT::v16i16, V1); | |||
17898 | V2 = DAG.getBitcast(MVT::v16i16, V2); | |||
17899 | return DAG.getBitcast(MVT::v16f16, | |||
17900 | DAG.getVectorShuffle(MVT::v16i16, DL, V1, V2, Mask)); | |||
17901 | } | |||
17902 | ||||
17903 | switch (VT.SimpleTy) { | |||
17904 | case MVT::v4f64: | |||
17905 | return lowerV4F64Shuffle(DL, Mask, Zeroable, V1, V2, Subtarget, DAG); | |||
17906 | case MVT::v4i64: | |||
17907 | return lowerV4I64Shuffle(DL, Mask, Zeroable, V1, V2, Subtarget, DAG); | |||
17908 | case MVT::v8f32: | |||
17909 | return lowerV8F32Shuffle(DL, Mask, Zeroable, V1, V2, Subtarget, DAG); | |||
17910 | case MVT::v8i32: | |||
17911 | return lowerV8I32Shuffle(DL, Mask, Zeroable, V1, V2, Subtarget, DAG); | |||
17912 | case MVT::v16i16: | |||
17913 | return lowerV16I16Shuffle(DL, Mask, Zeroable, V1, V2, Subtarget, DAG); | |||
17914 | case MVT::v32i8: | |||
17915 | return lowerV32I8Shuffle(DL, Mask, Zeroable, V1, V2, Subtarget, DAG); | |||
17916 | ||||
17917 | default: | |||
17918 | llvm_unreachable("Not a valid 256-bit x86 vector type!")::llvm::llvm_unreachable_internal("Not a valid 256-bit x86 vector type!" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17918); | |||
17919 | } | |||
17920 | } | |||
17921 | ||||
17922 | /// Try to lower a vector shuffle as a 128-bit shuffles. | |||
17923 | static SDValue lowerV4X128Shuffle(const SDLoc &DL, MVT VT, ArrayRef<int> Mask, | |||
17924 | const APInt &Zeroable, SDValue V1, SDValue V2, | |||
17925 | const X86Subtarget &Subtarget, | |||
17926 | SelectionDAG &DAG) { | |||
17927 | assert(VT.getScalarSizeInBits() == 64 &&(static_cast <bool> (VT.getScalarSizeInBits() == 64 && "Unexpected element type size for 128bit shuffle.") ? void ( 0) : __assert_fail ("VT.getScalarSizeInBits() == 64 && \"Unexpected element type size for 128bit shuffle.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17928, __extension__ __PRETTY_FUNCTION__)) | |||
17928 | "Unexpected element type size for 128bit shuffle.")(static_cast <bool> (VT.getScalarSizeInBits() == 64 && "Unexpected element type size for 128bit shuffle.") ? void ( 0) : __assert_fail ("VT.getScalarSizeInBits() == 64 && \"Unexpected element type size for 128bit shuffle.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17928, __extension__ __PRETTY_FUNCTION__)); | |||
17929 | ||||
17930 | // To handle 256 bit vector requires VLX and most probably | |||
17931 | // function lowerV2X128VectorShuffle() is better solution. | |||
17932 | assert(VT.is512BitVector() && "Unexpected vector size for 512bit shuffle.")(static_cast <bool> (VT.is512BitVector() && "Unexpected vector size for 512bit shuffle." ) ? void (0) : __assert_fail ("VT.is512BitVector() && \"Unexpected vector size for 512bit shuffle.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17932, __extension__ __PRETTY_FUNCTION__)); | |||
17933 | ||||
17934 | // TODO - use Zeroable like we do for lowerV2X128VectorShuffle? | |||
17935 | SmallVector<int, 4> Widened128Mask; | |||
17936 | if (!canWidenShuffleElements(Mask, Widened128Mask)) | |||
17937 | return SDValue(); | |||
17938 | assert(Widened128Mask.size() == 4 && "Shuffle widening mismatch")(static_cast <bool> (Widened128Mask.size() == 4 && "Shuffle widening mismatch") ? void (0) : __assert_fail ("Widened128Mask.size() == 4 && \"Shuffle widening mismatch\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17938, __extension__ __PRETTY_FUNCTION__)); | |||
17939 | ||||
17940 | // Try to use an insert into a zero vector. | |||
17941 | if (Widened128Mask[0] == 0 && (Zeroable & 0xf0) == 0xf0 && | |||
17942 | (Widened128Mask[1] == 1 || (Zeroable & 0x0c) == 0x0c)) { | |||
17943 | unsigned NumElts = ((Zeroable & 0x0c) == 0x0c) ? 2 : 4; | |||
17944 | MVT SubVT = MVT::getVectorVT(VT.getVectorElementType(), NumElts); | |||
17945 | SDValue LoV = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, SubVT, V1, | |||
17946 | DAG.getIntPtrConstant(0, DL)); | |||
17947 | return DAG.getNode(ISD::INSERT_SUBVECTOR, DL, VT, | |||
17948 | getZeroVector(VT, Subtarget, DAG, DL), LoV, | |||
17949 | DAG.getIntPtrConstant(0, DL)); | |||
17950 | } | |||
17951 | ||||
17952 | // Check for patterns which can be matched with a single insert of a 256-bit | |||
17953 | // subvector. | |||
17954 | bool OnlyUsesV1 = isShuffleEquivalent(Mask, {0, 1, 2, 3, 0, 1, 2, 3}, V1, V2); | |||
17955 | if (OnlyUsesV1 || | |||
17956 | isShuffleEquivalent(Mask, {0, 1, 2, 3, 8, 9, 10, 11}, V1, V2)) { | |||
17957 | MVT SubVT = MVT::getVectorVT(VT.getVectorElementType(), 4); | |||
17958 | SDValue SubVec = | |||
17959 | DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, SubVT, OnlyUsesV1 ? V1 : V2, | |||
17960 | DAG.getIntPtrConstant(0, DL)); | |||
17961 | return DAG.getNode(ISD::INSERT_SUBVECTOR, DL, VT, V1, SubVec, | |||
17962 | DAG.getIntPtrConstant(4, DL)); | |||
17963 | } | |||
17964 | ||||
17965 | // See if this is an insertion of the lower 128-bits of V2 into V1. | |||
17966 | bool IsInsert = true; | |||
17967 | int V2Index = -1; | |||
17968 | for (int i = 0; i < 4; ++i) { | |||
17969 | assert(Widened128Mask[i] >= -1 && "Illegal shuffle sentinel value")(static_cast <bool> (Widened128Mask[i] >= -1 && "Illegal shuffle sentinel value") ? void (0) : __assert_fail ("Widened128Mask[i] >= -1 && \"Illegal shuffle sentinel value\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 17969, __extension__ __PRETTY_FUNCTION__)); | |||
17970 | if (Widened128Mask[i] < 0) | |||
17971 | continue; | |||
17972 | ||||
17973 | // Make sure all V1 subvectors are in place. | |||
17974 | if (Widened128Mask[i] < 4) { | |||
17975 | if (Widened128Mask[i] != i) { | |||
17976 | IsInsert = false; | |||
17977 | break; | |||
17978 | } | |||
17979 | } else { | |||
17980 | // Make sure we only have a single V2 index and its the lowest 128-bits. | |||
17981 | if (V2Index >= 0 || Widened128Mask[i] != 4) { | |||
17982 | IsInsert = false; | |||
17983 | break; | |||
17984 | } | |||
17985 | V2Index = i; | |||
17986 | } | |||
17987 | } | |||
17988 | if (IsInsert && V2Index >= 0) { | |||
17989 | MVT SubVT = MVT::getVectorVT(VT.getVectorElementType(), 2); | |||
17990 | SDValue Subvec = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, SubVT, V2, | |||
17991 | DAG.getIntPtrConstant(0, DL)); | |||
17992 | return insert128BitVector(V1, Subvec, V2Index * 2, DAG, DL); | |||
17993 | } | |||
17994 | ||||
17995 | // See if we can widen to a 256-bit lane shuffle, we're going to lose 128-lane | |||
17996 | // UNDEF info by lowering to X86ISD::SHUF128 anyway, so by widening where | |||
17997 | // possible we at least ensure the lanes stay sequential to help later | |||
17998 | // combines. | |||
17999 | SmallVector<int, 2> Widened256Mask; | |||
18000 | if (canWidenShuffleElements(Widened128Mask, Widened256Mask)) { | |||
18001 | Widened128Mask.clear(); | |||
18002 | narrowShuffleMaskElts(2, Widened256Mask, Widened128Mask); | |||
18003 | } | |||
18004 | ||||
18005 | // Try to lower to vshuf64x2/vshuf32x4. | |||
18006 | SDValue Ops[2] = {DAG.getUNDEF(VT), DAG.getUNDEF(VT)}; | |||
18007 | unsigned PermMask = 0; | |||
18008 | // Insure elements came from the same Op. | |||
18009 | for (int i = 0; i < 4; ++i) { | |||
18010 | assert(Widened128Mask[i] >= -1 && "Illegal shuffle sentinel value")(static_cast <bool> (Widened128Mask[i] >= -1 && "Illegal shuffle sentinel value") ? void (0) : __assert_fail ("Widened128Mask[i] >= -1 && \"Illegal shuffle sentinel value\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18010, __extension__ __PRETTY_FUNCTION__)); | |||
18011 | if (Widened128Mask[i] < 0) | |||
18012 | continue; | |||
18013 | ||||
18014 | SDValue Op = Widened128Mask[i] >= 4 ? V2 : V1; | |||
18015 | unsigned OpIndex = i / 2; | |||
18016 | if (Ops[OpIndex].isUndef()) | |||
18017 | Ops[OpIndex] = Op; | |||
18018 | else if (Ops[OpIndex] != Op) | |||
18019 | return SDValue(); | |||
18020 | ||||
18021 | // Convert the 128-bit shuffle mask selection values into 128-bit selection | |||
18022 | // bits defined by a vshuf64x2 instruction's immediate control byte. | |||
18023 | PermMask |= (Widened128Mask[i] % 4) << (i * 2); | |||
18024 | } | |||
18025 | ||||
18026 | return DAG.getNode(X86ISD::SHUF128, DL, VT, Ops[0], Ops[1], | |||
18027 | DAG.getTargetConstant(PermMask, DL, MVT::i8)); | |||
18028 | } | |||
18029 | ||||
18030 | /// Handle lowering of 8-lane 64-bit floating point shuffles. | |||
18031 | static SDValue lowerV8F64Shuffle(const SDLoc &DL, ArrayRef<int> Mask, | |||
18032 | const APInt &Zeroable, SDValue V1, SDValue V2, | |||
18033 | const X86Subtarget &Subtarget, | |||
18034 | SelectionDAG &DAG) { | |||
18035 | assert(V1.getSimpleValueType() == MVT::v8f64 && "Bad operand type!")(static_cast <bool> (V1.getSimpleValueType() == MVT::v8f64 && "Bad operand type!") ? void (0) : __assert_fail ( "V1.getSimpleValueType() == MVT::v8f64 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18035, __extension__ __PRETTY_FUNCTION__)); | |||
18036 | assert(V2.getSimpleValueType() == MVT::v8f64 && "Bad operand type!")(static_cast <bool> (V2.getSimpleValueType() == MVT::v8f64 && "Bad operand type!") ? void (0) : __assert_fail ( "V2.getSimpleValueType() == MVT::v8f64 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18036, __extension__ __PRETTY_FUNCTION__)); | |||
18037 | assert(Mask.size() == 8 && "Unexpected mask size for v8 shuffle!")(static_cast <bool> (Mask.size() == 8 && "Unexpected mask size for v8 shuffle!" ) ? void (0) : __assert_fail ("Mask.size() == 8 && \"Unexpected mask size for v8 shuffle!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18037, __extension__ __PRETTY_FUNCTION__)); | |||
18038 | ||||
18039 | if (V2.isUndef()) { | |||
18040 | // Use low duplicate instructions for masks that match their pattern. | |||
18041 | if (isShuffleEquivalent(Mask, {0, 0, 2, 2, 4, 4, 6, 6}, V1, V2)) | |||
18042 | return DAG.getNode(X86ISD::MOVDDUP, DL, MVT::v8f64, V1); | |||
18043 | ||||
18044 | if (!is128BitLaneCrossingShuffleMask(MVT::v8f64, Mask)) { | |||
18045 | // Non-half-crossing single input shuffles can be lowered with an | |||
18046 | // interleaved permutation. | |||
18047 | unsigned VPERMILPMask = (Mask[0] == 1) | ((Mask[1] == 1) << 1) | | |||
18048 | ((Mask[2] == 3) << 2) | ((Mask[3] == 3) << 3) | | |||
18049 | ((Mask[4] == 5) << 4) | ((Mask[5] == 5) << 5) | | |||
18050 | ((Mask[6] == 7) << 6) | ((Mask[7] == 7) << 7); | |||
18051 | return DAG.getNode(X86ISD::VPERMILPI, DL, MVT::v8f64, V1, | |||
18052 | DAG.getTargetConstant(VPERMILPMask, DL, MVT::i8)); | |||
18053 | } | |||
18054 | ||||
18055 | SmallVector<int, 4> RepeatedMask; | |||
18056 | if (is256BitLaneRepeatedShuffleMask(MVT::v8f64, Mask, RepeatedMask)) | |||
18057 | return DAG.getNode(X86ISD::VPERMI, DL, MVT::v8f64, V1, | |||
18058 | getV4X86ShuffleImm8ForMask(RepeatedMask, DL, DAG)); | |||
18059 | } | |||
18060 | ||||
18061 | if (SDValue Shuf128 = lowerV4X128Shuffle(DL, MVT::v8f64, Mask, Zeroable, V1, | |||
18062 | V2, Subtarget, DAG)) | |||
18063 | return Shuf128; | |||
18064 | ||||
18065 | if (SDValue Unpck = lowerShuffleWithUNPCK(DL, MVT::v8f64, Mask, V1, V2, DAG)) | |||
18066 | return Unpck; | |||
18067 | ||||
18068 | // Check if the blend happens to exactly fit that of SHUFPD. | |||
18069 | if (SDValue Op = lowerShuffleWithSHUFPD(DL, MVT::v8f64, V1, V2, Mask, | |||
18070 | Zeroable, Subtarget, DAG)) | |||
18071 | return Op; | |||
18072 | ||||
18073 | if (SDValue V = lowerShuffleToEXPAND(DL, MVT::v8f64, Zeroable, Mask, V1, V2, | |||
18074 | DAG, Subtarget)) | |||
18075 | return V; | |||
18076 | ||||
18077 | if (SDValue Blend = lowerShuffleAsBlend(DL, MVT::v8f64, V1, V2, Mask, | |||
18078 | Zeroable, Subtarget, DAG)) | |||
18079 | return Blend; | |||
18080 | ||||
18081 | return lowerShuffleWithPERMV(DL, MVT::v8f64, Mask, V1, V2, Subtarget, DAG); | |||
18082 | } | |||
18083 | ||||
18084 | /// Handle lowering of 16-lane 32-bit floating point shuffles. | |||
18085 | static SDValue lowerV16F32Shuffle(const SDLoc &DL, ArrayRef<int> Mask, | |||
18086 | const APInt &Zeroable, SDValue V1, SDValue V2, | |||
18087 | const X86Subtarget &Subtarget, | |||
18088 | SelectionDAG &DAG) { | |||
18089 | assert(V1.getSimpleValueType() == MVT::v16f32 && "Bad operand type!")(static_cast <bool> (V1.getSimpleValueType() == MVT::v16f32 && "Bad operand type!") ? void (0) : __assert_fail ( "V1.getSimpleValueType() == MVT::v16f32 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18089, __extension__ __PRETTY_FUNCTION__)); | |||
18090 | assert(V2.getSimpleValueType() == MVT::v16f32 && "Bad operand type!")(static_cast <bool> (V2.getSimpleValueType() == MVT::v16f32 && "Bad operand type!") ? void (0) : __assert_fail ( "V2.getSimpleValueType() == MVT::v16f32 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18090, __extension__ __PRETTY_FUNCTION__)); | |||
18091 | assert(Mask.size() == 16 && "Unexpected mask size for v16 shuffle!")(static_cast <bool> (Mask.size() == 16 && "Unexpected mask size for v16 shuffle!" ) ? void (0) : __assert_fail ("Mask.size() == 16 && \"Unexpected mask size for v16 shuffle!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18091, __extension__ __PRETTY_FUNCTION__)); | |||
18092 | ||||
18093 | // If the shuffle mask is repeated in each 128-bit lane, we have many more | |||
18094 | // options to efficiently lower the shuffle. | |||
18095 | SmallVector<int, 4> RepeatedMask; | |||
18096 | if (is128BitLaneRepeatedShuffleMask(MVT::v16f32, Mask, RepeatedMask)) { | |||
18097 | assert(RepeatedMask.size() == 4 && "Unexpected repeated mask size!")(static_cast <bool> (RepeatedMask.size() == 4 && "Unexpected repeated mask size!") ? void (0) : __assert_fail ("RepeatedMask.size() == 4 && \"Unexpected repeated mask size!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18097, __extension__ __PRETTY_FUNCTION__)); | |||
18098 | ||||
18099 | // Use even/odd duplicate instructions for masks that match their pattern. | |||
18100 | if (isShuffleEquivalent(RepeatedMask, {0, 0, 2, 2}, V1, V2)) | |||
18101 | return DAG.getNode(X86ISD::MOVSLDUP, DL, MVT::v16f32, V1); | |||
18102 | if (isShuffleEquivalent(RepeatedMask, {1, 1, 3, 3}, V1, V2)) | |||
18103 | return DAG.getNode(X86ISD::MOVSHDUP, DL, MVT::v16f32, V1); | |||
18104 | ||||
18105 | if (V2.isUndef()) | |||
18106 | return DAG.getNode(X86ISD::VPERMILPI, DL, MVT::v16f32, V1, | |||
18107 | getV4X86ShuffleImm8ForMask(RepeatedMask, DL, DAG)); | |||
18108 | ||||
18109 | // Use dedicated unpack instructions for masks that match their pattern. | |||
18110 | if (SDValue V = lowerShuffleWithUNPCK(DL, MVT::v16f32, Mask, V1, V2, DAG)) | |||
18111 | return V; | |||
18112 | ||||
18113 | if (SDValue Blend = lowerShuffleAsBlend(DL, MVT::v16f32, V1, V2, Mask, | |||
18114 | Zeroable, Subtarget, DAG)) | |||
18115 | return Blend; | |||
18116 | ||||
18117 | // Otherwise, fall back to a SHUFPS sequence. | |||
18118 | return lowerShuffleWithSHUFPS(DL, MVT::v16f32, RepeatedMask, V1, V2, DAG); | |||
18119 | } | |||
18120 | ||||
18121 | // Try to create an in-lane repeating shuffle mask and then shuffle the | |||
18122 | // results into the target lanes. | |||
18123 | if (SDValue V = lowerShuffleAsRepeatedMaskAndLanePermute( | |||
18124 | DL, MVT::v16f32, V1, V2, Mask, Subtarget, DAG)) | |||
18125 | return V; | |||
18126 | ||||
18127 | // If we have a single input shuffle with different shuffle patterns in the | |||
18128 | // 128-bit lanes and don't lane cross, use variable mask VPERMILPS. | |||
18129 | if (V2.isUndef() && | |||
18130 | !is128BitLaneCrossingShuffleMask(MVT::v16f32, Mask)) { | |||
18131 | SDValue VPermMask = getConstVector(Mask, MVT::v16i32, DAG, DL, true); | |||
18132 | return DAG.getNode(X86ISD::VPERMILPV, DL, MVT::v16f32, V1, VPermMask); | |||
18133 | } | |||
18134 | ||||
18135 | // If we have AVX512F support, we can use VEXPAND. | |||
18136 | if (SDValue V = lowerShuffleToEXPAND(DL, MVT::v16f32, Zeroable, Mask, | |||
18137 | V1, V2, DAG, Subtarget)) | |||
18138 | return V; | |||
18139 | ||||
18140 | return lowerShuffleWithPERMV(DL, MVT::v16f32, Mask, V1, V2, Subtarget, DAG); | |||
18141 | } | |||
18142 | ||||
18143 | /// Handle lowering of 8-lane 64-bit integer shuffles. | |||
18144 | static SDValue lowerV8I64Shuffle(const SDLoc &DL, ArrayRef<int> Mask, | |||
18145 | const APInt &Zeroable, SDValue V1, SDValue V2, | |||
18146 | const X86Subtarget &Subtarget, | |||
18147 | SelectionDAG &DAG) { | |||
18148 | assert(V1.getSimpleValueType() == MVT::v8i64 && "Bad operand type!")(static_cast <bool> (V1.getSimpleValueType() == MVT::v8i64 && "Bad operand type!") ? void (0) : __assert_fail ( "V1.getSimpleValueType() == MVT::v8i64 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18148, __extension__ __PRETTY_FUNCTION__)); | |||
18149 | assert(V2.getSimpleValueType() == MVT::v8i64 && "Bad operand type!")(static_cast <bool> (V2.getSimpleValueType() == MVT::v8i64 && "Bad operand type!") ? void (0) : __assert_fail ( "V2.getSimpleValueType() == MVT::v8i64 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18149, __extension__ __PRETTY_FUNCTION__)); | |||
18150 | assert(Mask.size() == 8 && "Unexpected mask size for v8 shuffle!")(static_cast <bool> (Mask.size() == 8 && "Unexpected mask size for v8 shuffle!" ) ? void (0) : __assert_fail ("Mask.size() == 8 && \"Unexpected mask size for v8 shuffle!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18150, __extension__ __PRETTY_FUNCTION__)); | |||
18151 | ||||
18152 | if (V2.isUndef()) { | |||
18153 | // When the shuffle is mirrored between the 128-bit lanes of the unit, we | |||
18154 | // can use lower latency instructions that will operate on all four | |||
18155 | // 128-bit lanes. | |||
18156 | SmallVector<int, 2> Repeated128Mask; | |||
18157 | if (is128BitLaneRepeatedShuffleMask(MVT::v8i64, Mask, Repeated128Mask)) { | |||
18158 | SmallVector<int, 4> PSHUFDMask; | |||
18159 | narrowShuffleMaskElts(2, Repeated128Mask, PSHUFDMask); | |||
18160 | return DAG.getBitcast( | |||
18161 | MVT::v8i64, | |||
18162 | DAG.getNode(X86ISD::PSHUFD, DL, MVT::v16i32, | |||
18163 | DAG.getBitcast(MVT::v16i32, V1), | |||
18164 | getV4X86ShuffleImm8ForMask(PSHUFDMask, DL, DAG))); | |||
18165 | } | |||
18166 | ||||
18167 | SmallVector<int, 4> Repeated256Mask; | |||
18168 | if (is256BitLaneRepeatedShuffleMask(MVT::v8i64, Mask, Repeated256Mask)) | |||
18169 | return DAG.getNode(X86ISD::VPERMI, DL, MVT::v8i64, V1, | |||
18170 | getV4X86ShuffleImm8ForMask(Repeated256Mask, DL, DAG)); | |||
18171 | } | |||
18172 | ||||
18173 | if (SDValue Shuf128 = lowerV4X128Shuffle(DL, MVT::v8i64, Mask, Zeroable, V1, | |||
18174 | V2, Subtarget, DAG)) | |||
18175 | return Shuf128; | |||
18176 | ||||
18177 | // Try to use shift instructions. | |||
18178 | if (SDValue Shift = lowerShuffleAsShift(DL, MVT::v8i64, V1, V2, Mask, | |||
18179 | Zeroable, Subtarget, DAG)) | |||
18180 | return Shift; | |||
18181 | ||||
18182 | // Try to use VALIGN. | |||
18183 | if (SDValue Rotate = lowerShuffleAsVALIGN(DL, MVT::v8i64, V1, V2, Mask, | |||
18184 | Subtarget, DAG)) | |||
18185 | return Rotate; | |||
18186 | ||||
18187 | // Try to use PALIGNR. | |||
18188 | if (Subtarget.hasBWI()) | |||
18189 | if (SDValue Rotate = lowerShuffleAsByteRotate(DL, MVT::v8i64, V1, V2, Mask, | |||
18190 | Subtarget, DAG)) | |||
18191 | return Rotate; | |||
18192 | ||||
18193 | if (SDValue Unpck = lowerShuffleWithUNPCK(DL, MVT::v8i64, Mask, V1, V2, DAG)) | |||
18194 | return Unpck; | |||
18195 | ||||
18196 | // If we have AVX512F support, we can use VEXPAND. | |||
18197 | if (SDValue V = lowerShuffleToEXPAND(DL, MVT::v8i64, Zeroable, Mask, V1, V2, | |||
18198 | DAG, Subtarget)) | |||
18199 | return V; | |||
18200 | ||||
18201 | if (SDValue Blend = lowerShuffleAsBlend(DL, MVT::v8i64, V1, V2, Mask, | |||
18202 | Zeroable, Subtarget, DAG)) | |||
18203 | return Blend; | |||
18204 | ||||
18205 | return lowerShuffleWithPERMV(DL, MVT::v8i64, Mask, V1, V2, Subtarget, DAG); | |||
18206 | } | |||
18207 | ||||
18208 | /// Handle lowering of 16-lane 32-bit integer shuffles. | |||
18209 | static SDValue lowerV16I32Shuffle(const SDLoc &DL, ArrayRef<int> Mask, | |||
18210 | const APInt &Zeroable, SDValue V1, SDValue V2, | |||
18211 | const X86Subtarget &Subtarget, | |||
18212 | SelectionDAG &DAG) { | |||
18213 | assert(V1.getSimpleValueType() == MVT::v16i32 && "Bad operand type!")(static_cast <bool> (V1.getSimpleValueType() == MVT::v16i32 && "Bad operand type!") ? void (0) : __assert_fail ( "V1.getSimpleValueType() == MVT::v16i32 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18213, __extension__ __PRETTY_FUNCTION__)); | |||
18214 | assert(V2.getSimpleValueType() == MVT::v16i32 && "Bad operand type!")(static_cast <bool> (V2.getSimpleValueType() == MVT::v16i32 && "Bad operand type!") ? void (0) : __assert_fail ( "V2.getSimpleValueType() == MVT::v16i32 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18214, __extension__ __PRETTY_FUNCTION__)); | |||
18215 | assert(Mask.size() == 16 && "Unexpected mask size for v16 shuffle!")(static_cast <bool> (Mask.size() == 16 && "Unexpected mask size for v16 shuffle!" ) ? void (0) : __assert_fail ("Mask.size() == 16 && \"Unexpected mask size for v16 shuffle!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18215, __extension__ __PRETTY_FUNCTION__)); | |||
18216 | ||||
18217 | // Whenever we can lower this as a zext, that instruction is strictly faster | |||
18218 | // than any alternative. It also allows us to fold memory operands into the | |||
18219 | // shuffle in many cases. | |||
18220 | if (SDValue ZExt = lowerShuffleAsZeroOrAnyExtend( | |||
18221 | DL, MVT::v16i32, V1, V2, Mask, Zeroable, Subtarget, DAG)) | |||
18222 | return ZExt; | |||
18223 | ||||
18224 | // If the shuffle mask is repeated in each 128-bit lane we can use more | |||
18225 | // efficient instructions that mirror the shuffles across the four 128-bit | |||
18226 | // lanes. | |||
18227 | SmallVector<int, 4> RepeatedMask; | |||
18228 | bool Is128BitLaneRepeatedShuffle = | |||
18229 | is128BitLaneRepeatedShuffleMask(MVT::v16i32, Mask, RepeatedMask); | |||
18230 | if (Is128BitLaneRepeatedShuffle) { | |||
18231 | assert(RepeatedMask.size() == 4 && "Unexpected repeated mask size!")(static_cast <bool> (RepeatedMask.size() == 4 && "Unexpected repeated mask size!") ? void (0) : __assert_fail ("RepeatedMask.size() == 4 && \"Unexpected repeated mask size!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18231, __extension__ __PRETTY_FUNCTION__)); | |||
18232 | if (V2.isUndef()) | |||
18233 | return DAG.getNode(X86ISD::PSHUFD, DL, MVT::v16i32, V1, | |||
18234 | getV4X86ShuffleImm8ForMask(RepeatedMask, DL, DAG)); | |||
18235 | ||||
18236 | // Use dedicated unpack instructions for masks that match their pattern. | |||
18237 | if (SDValue V = lowerShuffleWithUNPCK(DL, MVT::v16i32, Mask, V1, V2, DAG)) | |||
18238 | return V; | |||
18239 | } | |||
18240 | ||||
18241 | // Try to use shift instructions. | |||
18242 | if (SDValue Shift = lowerShuffleAsShift(DL, MVT::v16i32, V1, V2, Mask, | |||
18243 | Zeroable, Subtarget, DAG)) | |||
18244 | return Shift; | |||
18245 | ||||
18246 | // Try to use VALIGN. | |||
18247 | if (SDValue Rotate = lowerShuffleAsVALIGN(DL, MVT::v16i32, V1, V2, Mask, | |||
18248 | Subtarget, DAG)) | |||
18249 | return Rotate; | |||
18250 | ||||
18251 | // Try to use byte rotation instructions. | |||
18252 | if (Subtarget.hasBWI()) | |||
18253 | if (SDValue Rotate = lowerShuffleAsByteRotate(DL, MVT::v16i32, V1, V2, Mask, | |||
18254 | Subtarget, DAG)) | |||
18255 | return Rotate; | |||
18256 | ||||
18257 | // Assume that a single SHUFPS is faster than using a permv shuffle. | |||
18258 | // If some CPU is harmed by the domain switch, we can fix it in a later pass. | |||
18259 | if (Is128BitLaneRepeatedShuffle && isSingleSHUFPSMask(RepeatedMask)) { | |||
18260 | SDValue CastV1 = DAG.getBitcast(MVT::v16f32, V1); | |||
18261 | SDValue CastV2 = DAG.getBitcast(MVT::v16f32, V2); | |||
18262 | SDValue ShufPS = lowerShuffleWithSHUFPS(DL, MVT::v16f32, RepeatedMask, | |||
18263 | CastV1, CastV2, DAG); | |||
18264 | return DAG.getBitcast(MVT::v16i32, ShufPS); | |||
18265 | } | |||
18266 | ||||
18267 | // Try to create an in-lane repeating shuffle mask and then shuffle the | |||
18268 | // results into the target lanes. | |||
18269 | if (SDValue V = lowerShuffleAsRepeatedMaskAndLanePermute( | |||
18270 | DL, MVT::v16i32, V1, V2, Mask, Subtarget, DAG)) | |||
18271 | return V; | |||
18272 | ||||
18273 | // If we have AVX512F support, we can use VEXPAND. | |||
18274 | if (SDValue V = lowerShuffleToEXPAND(DL, MVT::v16i32, Zeroable, Mask, V1, V2, | |||
18275 | DAG, Subtarget)) | |||
18276 | return V; | |||
18277 | ||||
18278 | if (SDValue Blend = lowerShuffleAsBlend(DL, MVT::v16i32, V1, V2, Mask, | |||
18279 | Zeroable, Subtarget, DAG)) | |||
18280 | return Blend; | |||
18281 | ||||
18282 | return lowerShuffleWithPERMV(DL, MVT::v16i32, Mask, V1, V2, Subtarget, DAG); | |||
18283 | } | |||
18284 | ||||
18285 | /// Handle lowering of 32-lane 16-bit integer shuffles. | |||
18286 | static SDValue lowerV32I16Shuffle(const SDLoc &DL, ArrayRef<int> Mask, | |||
18287 | const APInt &Zeroable, SDValue V1, SDValue V2, | |||
18288 | const X86Subtarget &Subtarget, | |||
18289 | SelectionDAG &DAG) { | |||
18290 | assert(V1.getSimpleValueType() == MVT::v32i16 && "Bad operand type!")(static_cast <bool> (V1.getSimpleValueType() == MVT::v32i16 && "Bad operand type!") ? void (0) : __assert_fail ( "V1.getSimpleValueType() == MVT::v32i16 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18290, __extension__ __PRETTY_FUNCTION__)); | |||
18291 | assert(V2.getSimpleValueType() == MVT::v32i16 && "Bad operand type!")(static_cast <bool> (V2.getSimpleValueType() == MVT::v32i16 && "Bad operand type!") ? void (0) : __assert_fail ( "V2.getSimpleValueType() == MVT::v32i16 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18291, __extension__ __PRETTY_FUNCTION__)); | |||
18292 | assert(Mask.size() == 32 && "Unexpected mask size for v32 shuffle!")(static_cast <bool> (Mask.size() == 32 && "Unexpected mask size for v32 shuffle!" ) ? void (0) : __assert_fail ("Mask.size() == 32 && \"Unexpected mask size for v32 shuffle!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18292, __extension__ __PRETTY_FUNCTION__)); | |||
18293 | assert(Subtarget.hasBWI() && "We can only lower v32i16 with AVX-512-BWI!")(static_cast <bool> (Subtarget.hasBWI() && "We can only lower v32i16 with AVX-512-BWI!" ) ? void (0) : __assert_fail ("Subtarget.hasBWI() && \"We can only lower v32i16 with AVX-512-BWI!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18293, __extension__ __PRETTY_FUNCTION__)); | |||
18294 | ||||
18295 | // Whenever we can lower this as a zext, that instruction is strictly faster | |||
18296 | // than any alternative. It also allows us to fold memory operands into the | |||
18297 | // shuffle in many cases. | |||
18298 | if (SDValue ZExt = lowerShuffleAsZeroOrAnyExtend( | |||
18299 | DL, MVT::v32i16, V1, V2, Mask, Zeroable, Subtarget, DAG)) | |||
18300 | return ZExt; | |||
18301 | ||||
18302 | // Use dedicated unpack instructions for masks that match their pattern. | |||
18303 | if (SDValue V = lowerShuffleWithUNPCK(DL, MVT::v32i16, Mask, V1, V2, DAG)) | |||
18304 | return V; | |||
18305 | ||||
18306 | // Use dedicated pack instructions for masks that match their pattern. | |||
18307 | if (SDValue V = | |||
18308 | lowerShuffleWithPACK(DL, MVT::v32i16, Mask, V1, V2, DAG, Subtarget)) | |||
18309 | return V; | |||
18310 | ||||
18311 | // Try to use shift instructions. | |||
18312 | if (SDValue Shift = lowerShuffleAsShift(DL, MVT::v32i16, V1, V2, Mask, | |||
18313 | Zeroable, Subtarget, DAG)) | |||
18314 | return Shift; | |||
18315 | ||||
18316 | // Try to use byte rotation instructions. | |||
18317 | if (SDValue Rotate = lowerShuffleAsByteRotate(DL, MVT::v32i16, V1, V2, Mask, | |||
18318 | Subtarget, DAG)) | |||
18319 | return Rotate; | |||
18320 | ||||
18321 | if (V2.isUndef()) { | |||
18322 | // Try to use bit rotation instructions. | |||
18323 | if (SDValue Rotate = | |||
18324 | lowerShuffleAsBitRotate(DL, MVT::v32i16, V1, Mask, Subtarget, DAG)) | |||
18325 | return Rotate; | |||
18326 | ||||
18327 | SmallVector<int, 8> RepeatedMask; | |||
18328 | if (is128BitLaneRepeatedShuffleMask(MVT::v32i16, Mask, RepeatedMask)) { | |||
18329 | // As this is a single-input shuffle, the repeated mask should be | |||
18330 | // a strictly valid v8i16 mask that we can pass through to the v8i16 | |||
18331 | // lowering to handle even the v32 case. | |||
18332 | return lowerV8I16GeneralSingleInputShuffle(DL, MVT::v32i16, V1, | |||
18333 | RepeatedMask, Subtarget, DAG); | |||
18334 | } | |||
18335 | } | |||
18336 | ||||
18337 | if (SDValue Blend = lowerShuffleAsBlend(DL, MVT::v32i16, V1, V2, Mask, | |||
18338 | Zeroable, Subtarget, DAG)) | |||
18339 | return Blend; | |||
18340 | ||||
18341 | if (SDValue PSHUFB = lowerShuffleWithPSHUFB(DL, MVT::v32i16, Mask, V1, V2, | |||
18342 | Zeroable, Subtarget, DAG)) | |||
18343 | return PSHUFB; | |||
18344 | ||||
18345 | return lowerShuffleWithPERMV(DL, MVT::v32i16, Mask, V1, V2, Subtarget, DAG); | |||
18346 | } | |||
18347 | ||||
18348 | /// Handle lowering of 64-lane 8-bit integer shuffles. | |||
18349 | static SDValue lowerV64I8Shuffle(const SDLoc &DL, ArrayRef<int> Mask, | |||
18350 | const APInt &Zeroable, SDValue V1, SDValue V2, | |||
18351 | const X86Subtarget &Subtarget, | |||
18352 | SelectionDAG &DAG) { | |||
18353 | assert(V1.getSimpleValueType() == MVT::v64i8 && "Bad operand type!")(static_cast <bool> (V1.getSimpleValueType() == MVT::v64i8 && "Bad operand type!") ? void (0) : __assert_fail ( "V1.getSimpleValueType() == MVT::v64i8 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18353, __extension__ __PRETTY_FUNCTION__)); | |||
18354 | assert(V2.getSimpleValueType() == MVT::v64i8 && "Bad operand type!")(static_cast <bool> (V2.getSimpleValueType() == MVT::v64i8 && "Bad operand type!") ? void (0) : __assert_fail ( "V2.getSimpleValueType() == MVT::v64i8 && \"Bad operand type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18354, __extension__ __PRETTY_FUNCTION__)); | |||
18355 | assert(Mask.size() == 64 && "Unexpected mask size for v64 shuffle!")(static_cast <bool> (Mask.size() == 64 && "Unexpected mask size for v64 shuffle!" ) ? void (0) : __assert_fail ("Mask.size() == 64 && \"Unexpected mask size for v64 shuffle!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18355, __extension__ __PRETTY_FUNCTION__)); | |||
18356 | assert(Subtarget.hasBWI() && "We can only lower v64i8 with AVX-512-BWI!")(static_cast <bool> (Subtarget.hasBWI() && "We can only lower v64i8 with AVX-512-BWI!" ) ? void (0) : __assert_fail ("Subtarget.hasBWI() && \"We can only lower v64i8 with AVX-512-BWI!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18356, __extension__ __PRETTY_FUNCTION__)); | |||
18357 | ||||
18358 | // Whenever we can lower this as a zext, that instruction is strictly faster | |||
18359 | // than any alternative. It also allows us to fold memory operands into the | |||
18360 | // shuffle in many cases. | |||
18361 | if (SDValue ZExt = lowerShuffleAsZeroOrAnyExtend( | |||
18362 | DL, MVT::v64i8, V1, V2, Mask, Zeroable, Subtarget, DAG)) | |||
18363 | return ZExt; | |||
18364 | ||||
18365 | // Use dedicated unpack instructions for masks that match their pattern. | |||
18366 | if (SDValue V = lowerShuffleWithUNPCK(DL, MVT::v64i8, Mask, V1, V2, DAG)) | |||
18367 | return V; | |||
18368 | ||||
18369 | // Use dedicated pack instructions for masks that match their pattern. | |||
18370 | if (SDValue V = lowerShuffleWithPACK(DL, MVT::v64i8, Mask, V1, V2, DAG, | |||
18371 | Subtarget)) | |||
18372 | return V; | |||
18373 | ||||
18374 | // Try to use shift instructions. | |||
18375 | if (SDValue Shift = lowerShuffleAsShift(DL, MVT::v64i8, V1, V2, Mask, | |||
18376 | Zeroable, Subtarget, DAG)) | |||
18377 | return Shift; | |||
18378 | ||||
18379 | // Try to use byte rotation instructions. | |||
18380 | if (SDValue Rotate = lowerShuffleAsByteRotate(DL, MVT::v64i8, V1, V2, Mask, | |||
18381 | Subtarget, DAG)) | |||
18382 | return Rotate; | |||
18383 | ||||
18384 | // Try to use bit rotation instructions. | |||
18385 | if (V2.isUndef()) | |||
18386 | if (SDValue Rotate = | |||
18387 | lowerShuffleAsBitRotate(DL, MVT::v64i8, V1, Mask, Subtarget, DAG)) | |||
18388 | return Rotate; | |||
18389 | ||||
18390 | // Lower as AND if possible. | |||
18391 | if (SDValue Masked = lowerShuffleAsBitMask(DL, MVT::v64i8, V1, V2, Mask, | |||
18392 | Zeroable, Subtarget, DAG)) | |||
18393 | return Masked; | |||
18394 | ||||
18395 | if (SDValue PSHUFB = lowerShuffleWithPSHUFB(DL, MVT::v64i8, Mask, V1, V2, | |||
18396 | Zeroable, Subtarget, DAG)) | |||
18397 | return PSHUFB; | |||
18398 | ||||
18399 | // VBMI can use VPERMV/VPERMV3 byte shuffles. | |||
18400 | if (Subtarget.hasVBMI()) | |||
18401 | return lowerShuffleWithPERMV(DL, MVT::v64i8, Mask, V1, V2, Subtarget, DAG); | |||
18402 | ||||
18403 | // Try to create an in-lane repeating shuffle mask and then shuffle the | |||
18404 | // results into the target lanes. | |||
18405 | if (SDValue V = lowerShuffleAsRepeatedMaskAndLanePermute( | |||
18406 | DL, MVT::v64i8, V1, V2, Mask, Subtarget, DAG)) | |||
18407 | return V; | |||
18408 | ||||
18409 | if (SDValue Blend = lowerShuffleAsBlend(DL, MVT::v64i8, V1, V2, Mask, | |||
18410 | Zeroable, Subtarget, DAG)) | |||
18411 | return Blend; | |||
18412 | ||||
18413 | // Try to simplify this by merging 128-bit lanes to enable a lane-based | |||
18414 | // shuffle. | |||
18415 | if (!V2.isUndef()) | |||
18416 | if (SDValue Result = lowerShuffleAsLanePermuteAndRepeatedMask( | |||
18417 | DL, MVT::v64i8, V1, V2, Mask, Subtarget, DAG)) | |||
18418 | return Result; | |||
18419 | ||||
18420 | // FIXME: Implement direct support for this type! | |||
18421 | return splitAndLowerShuffle(DL, MVT::v64i8, V1, V2, Mask, DAG); | |||
18422 | } | |||
18423 | ||||
18424 | /// High-level routine to lower various 512-bit x86 vector shuffles. | |||
18425 | /// | |||
18426 | /// This routine either breaks down the specific type of a 512-bit x86 vector | |||
18427 | /// shuffle or splits it into two 256-bit shuffles and fuses the results back | |||
18428 | /// together based on the available instructions. | |||
18429 | static SDValue lower512BitShuffle(const SDLoc &DL, ArrayRef<int> Mask, | |||
18430 | MVT VT, SDValue V1, SDValue V2, | |||
18431 | const APInt &Zeroable, | |||
18432 | const X86Subtarget &Subtarget, | |||
18433 | SelectionDAG &DAG) { | |||
18434 | assert(Subtarget.hasAVX512() &&(static_cast <bool> (Subtarget.hasAVX512() && "Cannot lower 512-bit vectors w/ basic ISA!" ) ? void (0) : __assert_fail ("Subtarget.hasAVX512() && \"Cannot lower 512-bit vectors w/ basic ISA!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18435, __extension__ __PRETTY_FUNCTION__)) | |||
18435 | "Cannot lower 512-bit vectors w/ basic ISA!")(static_cast <bool> (Subtarget.hasAVX512() && "Cannot lower 512-bit vectors w/ basic ISA!" ) ? void (0) : __assert_fail ("Subtarget.hasAVX512() && \"Cannot lower 512-bit vectors w/ basic ISA!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18435, __extension__ __PRETTY_FUNCTION__)); | |||
18436 | ||||
18437 | // If we have a single input to the zero element, insert that into V1 if we | |||
18438 | // can do so cheaply. | |||
18439 | int NumElts = Mask.size(); | |||
18440 | int NumV2Elements = count_if(Mask, [NumElts](int M) { return M >= NumElts; }); | |||
18441 | ||||
18442 | if (NumV2Elements == 1 && Mask[0] >= NumElts) | |||
18443 | if (SDValue Insertion = lowerShuffleAsElementInsertion( | |||
18444 | DL, VT, V1, V2, Mask, Zeroable, Subtarget, DAG)) | |||
18445 | return Insertion; | |||
18446 | ||||
18447 | // Handle special cases where the lower or upper half is UNDEF. | |||
18448 | if (SDValue V = | |||
18449 | lowerShuffleWithUndefHalf(DL, VT, V1, V2, Mask, Subtarget, DAG)) | |||
18450 | return V; | |||
18451 | ||||
18452 | // Check for being able to broadcast a single element. | |||
18453 | if (SDValue Broadcast = lowerShuffleAsBroadcast(DL, VT, V1, V2, Mask, | |||
18454 | Subtarget, DAG)) | |||
18455 | return Broadcast; | |||
18456 | ||||
18457 | if ((VT == MVT::v32i16 || VT == MVT::v64i8) && !Subtarget.hasBWI()) { | |||
18458 | // Try using bit ops for masking and blending before falling back to | |||
18459 | // splitting. | |||
18460 | if (SDValue V = lowerShuffleAsBitMask(DL, VT, V1, V2, Mask, Zeroable, | |||
18461 | Subtarget, DAG)) | |||
18462 | return V; | |||
18463 | if (SDValue V = lowerShuffleAsBitBlend(DL, VT, V1, V2, Mask, DAG)) | |||
18464 | return V; | |||
18465 | ||||
18466 | return splitAndLowerShuffle(DL, VT, V1, V2, Mask, DAG); | |||
18467 | } | |||
18468 | ||||
18469 | if (VT == MVT::v32f16) { | |||
18470 | V1 = DAG.getBitcast(MVT::v32i16, V1); | |||
18471 | V2 = DAG.getBitcast(MVT::v32i16, V2); | |||
18472 | return DAG.getBitcast(MVT::v32f16, | |||
18473 | DAG.getVectorShuffle(MVT::v32i16, DL, V1, V2, Mask)); | |||
18474 | } | |||
18475 | ||||
18476 | // Dispatch to each element type for lowering. If we don't have support for | |||
18477 | // specific element type shuffles at 512 bits, immediately split them and | |||
18478 | // lower them. Each lowering routine of a given type is allowed to assume that | |||
18479 | // the requisite ISA extensions for that element type are available. | |||
18480 | switch (VT.SimpleTy) { | |||
18481 | case MVT::v8f64: | |||
18482 | return lowerV8F64Shuffle(DL, Mask, Zeroable, V1, V2, Subtarget, DAG); | |||
18483 | case MVT::v16f32: | |||
18484 | return lowerV16F32Shuffle(DL, Mask, Zeroable, V1, V2, Subtarget, DAG); | |||
18485 | case MVT::v8i64: | |||
18486 | return lowerV8I64Shuffle(DL, Mask, Zeroable, V1, V2, Subtarget, DAG); | |||
18487 | case MVT::v16i32: | |||
18488 | return lowerV16I32Shuffle(DL, Mask, Zeroable, V1, V2, Subtarget, DAG); | |||
18489 | case MVT::v32i16: | |||
18490 | return lowerV32I16Shuffle(DL, Mask, Zeroable, V1, V2, Subtarget, DAG); | |||
18491 | case MVT::v64i8: | |||
18492 | return lowerV64I8Shuffle(DL, Mask, Zeroable, V1, V2, Subtarget, DAG); | |||
18493 | ||||
18494 | default: | |||
18495 | llvm_unreachable("Not a valid 512-bit x86 vector type!")::llvm::llvm_unreachable_internal("Not a valid 512-bit x86 vector type!" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18495); | |||
18496 | } | |||
18497 | } | |||
18498 | ||||
18499 | static SDValue lower1BitShuffleAsKSHIFTR(const SDLoc &DL, ArrayRef<int> Mask, | |||
18500 | MVT VT, SDValue V1, SDValue V2, | |||
18501 | const X86Subtarget &Subtarget, | |||
18502 | SelectionDAG &DAG) { | |||
18503 | // Shuffle should be unary. | |||
18504 | if (!V2.isUndef()) | |||
18505 | return SDValue(); | |||
18506 | ||||
18507 | int ShiftAmt = -1; | |||
18508 | int NumElts = Mask.size(); | |||
18509 | for (int i = 0; i != NumElts; ++i) { | |||
18510 | int M = Mask[i]; | |||
18511 | assert((M == SM_SentinelUndef || (0 <= M && M < NumElts)) &&(static_cast <bool> ((M == SM_SentinelUndef || (0 <= M && M < NumElts)) && "Unexpected mask index." ) ? void (0) : __assert_fail ("(M == SM_SentinelUndef || (0 <= M && M < NumElts)) && \"Unexpected mask index.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18512, __extension__ __PRETTY_FUNCTION__)) | |||
18512 | "Unexpected mask index.")(static_cast <bool> ((M == SM_SentinelUndef || (0 <= M && M < NumElts)) && "Unexpected mask index." ) ? void (0) : __assert_fail ("(M == SM_SentinelUndef || (0 <= M && M < NumElts)) && \"Unexpected mask index.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18512, __extension__ __PRETTY_FUNCTION__)); | |||
18513 | if (M < 0) | |||
18514 | continue; | |||
18515 | ||||
18516 | // The first non-undef element determines our shift amount. | |||
18517 | if (ShiftAmt < 0) { | |||
18518 | ShiftAmt = M - i; | |||
18519 | // Need to be shifting right. | |||
18520 | if (ShiftAmt <= 0) | |||
18521 | return SDValue(); | |||
18522 | } | |||
18523 | // All non-undef elements must shift by the same amount. | |||
18524 | if (ShiftAmt != M - i) | |||
18525 | return SDValue(); | |||
18526 | } | |||
18527 | assert(ShiftAmt >= 0 && "All undef?")(static_cast <bool> (ShiftAmt >= 0 && "All undef?" ) ? void (0) : __assert_fail ("ShiftAmt >= 0 && \"All undef?\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18527, __extension__ __PRETTY_FUNCTION__)); | |||
18528 | ||||
18529 | // Great we found a shift right. | |||
18530 | MVT WideVT = VT; | |||
18531 | if ((!Subtarget.hasDQI() && NumElts == 8) || NumElts < 8) | |||
18532 | WideVT = Subtarget.hasDQI() ? MVT::v8i1 : MVT::v16i1; | |||
18533 | SDValue Res = DAG.getNode(ISD::INSERT_SUBVECTOR, DL, WideVT, | |||
18534 | DAG.getUNDEF(WideVT), V1, | |||
18535 | DAG.getIntPtrConstant(0, DL)); | |||
18536 | Res = DAG.getNode(X86ISD::KSHIFTR, DL, WideVT, Res, | |||
18537 | DAG.getTargetConstant(ShiftAmt, DL, MVT::i8)); | |||
18538 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Res, | |||
18539 | DAG.getIntPtrConstant(0, DL)); | |||
18540 | } | |||
18541 | ||||
18542 | // Determine if this shuffle can be implemented with a KSHIFT instruction. | |||
18543 | // Returns the shift amount if possible or -1 if not. This is a simplified | |||
18544 | // version of matchShuffleAsShift. | |||
18545 | static int match1BitShuffleAsKSHIFT(unsigned &Opcode, ArrayRef<int> Mask, | |||
18546 | int MaskOffset, const APInt &Zeroable) { | |||
18547 | int Size = Mask.size(); | |||
18548 | ||||
18549 | auto CheckZeros = [&](int Shift, bool Left) { | |||
18550 | for (int j = 0; j < Shift; ++j) | |||
18551 | if (!Zeroable[j + (Left ? 0 : (Size - Shift))]) | |||
18552 | return false; | |||
18553 | ||||
18554 | return true; | |||
18555 | }; | |||
18556 | ||||
18557 | auto MatchShift = [&](int Shift, bool Left) { | |||
18558 | unsigned Pos = Left ? Shift : 0; | |||
18559 | unsigned Low = Left ? 0 : Shift; | |||
18560 | unsigned Len = Size - Shift; | |||
18561 | return isSequentialOrUndefInRange(Mask, Pos, Len, Low + MaskOffset); | |||
18562 | }; | |||
18563 | ||||
18564 | for (int Shift = 1; Shift != Size; ++Shift) | |||
18565 | for (bool Left : {true, false}) | |||
18566 | if (CheckZeros(Shift, Left) && MatchShift(Shift, Left)) { | |||
18567 | Opcode = Left ? X86ISD::KSHIFTL : X86ISD::KSHIFTR; | |||
18568 | return Shift; | |||
18569 | } | |||
18570 | ||||
18571 | return -1; | |||
18572 | } | |||
18573 | ||||
18574 | ||||
18575 | // Lower vXi1 vector shuffles. | |||
18576 | // There is no a dedicated instruction on AVX-512 that shuffles the masks. | |||
18577 | // The only way to shuffle bits is to sign-extend the mask vector to SIMD | |||
18578 | // vector, shuffle and then truncate it back. | |||
18579 | static SDValue lower1BitShuffle(const SDLoc &DL, ArrayRef<int> Mask, | |||
18580 | MVT VT, SDValue V1, SDValue V2, | |||
18581 | const APInt &Zeroable, | |||
18582 | const X86Subtarget &Subtarget, | |||
18583 | SelectionDAG &DAG) { | |||
18584 | assert(Subtarget.hasAVX512() &&(static_cast <bool> (Subtarget.hasAVX512() && "Cannot lower 512-bit vectors w/o basic ISA!" ) ? void (0) : __assert_fail ("Subtarget.hasAVX512() && \"Cannot lower 512-bit vectors w/o basic ISA!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18585, __extension__ __PRETTY_FUNCTION__)) | |||
18585 | "Cannot lower 512-bit vectors w/o basic ISA!")(static_cast <bool> (Subtarget.hasAVX512() && "Cannot lower 512-bit vectors w/o basic ISA!" ) ? void (0) : __assert_fail ("Subtarget.hasAVX512() && \"Cannot lower 512-bit vectors w/o basic ISA!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18585, __extension__ __PRETTY_FUNCTION__)); | |||
18586 | ||||
18587 | int NumElts = Mask.size(); | |||
18588 | ||||
18589 | // Try to recognize shuffles that are just padding a subvector with zeros. | |||
18590 | int SubvecElts = 0; | |||
18591 | int Src = -1; | |||
18592 | for (int i = 0; i != NumElts; ++i) { | |||
18593 | if (Mask[i] >= 0) { | |||
18594 | // Grab the source from the first valid mask. All subsequent elements need | |||
18595 | // to use this same source. | |||
18596 | if (Src < 0) | |||
18597 | Src = Mask[i] / NumElts; | |||
18598 | if (Src != (Mask[i] / NumElts) || (Mask[i] % NumElts) != i) | |||
18599 | break; | |||
18600 | } | |||
18601 | ||||
18602 | ++SubvecElts; | |||
18603 | } | |||
18604 | assert(SubvecElts != NumElts && "Identity shuffle?")(static_cast <bool> (SubvecElts != NumElts && "Identity shuffle?" ) ? void (0) : __assert_fail ("SubvecElts != NumElts && \"Identity shuffle?\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18604, __extension__ __PRETTY_FUNCTION__)); | |||
18605 | ||||
18606 | // Clip to a power 2. | |||
18607 | SubvecElts = PowerOf2Floor(SubvecElts); | |||
18608 | ||||
18609 | // Make sure the number of zeroable bits in the top at least covers the bits | |||
18610 | // not covered by the subvector. | |||
18611 | if ((int)Zeroable.countLeadingOnes() >= (NumElts - SubvecElts)) { | |||
18612 | assert(Src >= 0 && "Expected a source!")(static_cast <bool> (Src >= 0 && "Expected a source!" ) ? void (0) : __assert_fail ("Src >= 0 && \"Expected a source!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18612, __extension__ __PRETTY_FUNCTION__)); | |||
18613 | MVT ExtractVT = MVT::getVectorVT(MVT::i1, SubvecElts); | |||
18614 | SDValue Extract = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, ExtractVT, | |||
18615 | Src == 0 ? V1 : V2, | |||
18616 | DAG.getIntPtrConstant(0, DL)); | |||
18617 | return DAG.getNode(ISD::INSERT_SUBVECTOR, DL, VT, | |||
18618 | DAG.getConstant(0, DL, VT), | |||
18619 | Extract, DAG.getIntPtrConstant(0, DL)); | |||
18620 | } | |||
18621 | ||||
18622 | // Try a simple shift right with undef elements. Later we'll try with zeros. | |||
18623 | if (SDValue Shift = lower1BitShuffleAsKSHIFTR(DL, Mask, VT, V1, V2, Subtarget, | |||
18624 | DAG)) | |||
18625 | return Shift; | |||
18626 | ||||
18627 | // Try to match KSHIFTs. | |||
18628 | unsigned Offset = 0; | |||
18629 | for (SDValue V : { V1, V2 }) { | |||
18630 | unsigned Opcode; | |||
18631 | int ShiftAmt = match1BitShuffleAsKSHIFT(Opcode, Mask, Offset, Zeroable); | |||
18632 | if (ShiftAmt >= 0) { | |||
18633 | MVT WideVT = VT; | |||
18634 | if ((!Subtarget.hasDQI() && NumElts == 8) || NumElts < 8) | |||
18635 | WideVT = Subtarget.hasDQI() ? MVT::v8i1 : MVT::v16i1; | |||
18636 | SDValue Res = DAG.getNode(ISD::INSERT_SUBVECTOR, DL, WideVT, | |||
18637 | DAG.getUNDEF(WideVT), V, | |||
18638 | DAG.getIntPtrConstant(0, DL)); | |||
18639 | // Widened right shifts need two shifts to ensure we shift in zeroes. | |||
18640 | if (Opcode == X86ISD::KSHIFTR && WideVT != VT) { | |||
18641 | int WideElts = WideVT.getVectorNumElements(); | |||
18642 | // Shift left to put the original vector in the MSBs of the new size. | |||
18643 | Res = DAG.getNode(X86ISD::KSHIFTL, DL, WideVT, Res, | |||
18644 | DAG.getTargetConstant(WideElts - NumElts, DL, MVT::i8)); | |||
18645 | // Increase the shift amount to account for the left shift. | |||
18646 | ShiftAmt += WideElts - NumElts; | |||
18647 | } | |||
18648 | ||||
18649 | Res = DAG.getNode(Opcode, DL, WideVT, Res, | |||
18650 | DAG.getTargetConstant(ShiftAmt, DL, MVT::i8)); | |||
18651 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Res, | |||
18652 | DAG.getIntPtrConstant(0, DL)); | |||
18653 | } | |||
18654 | Offset += NumElts; // Increment for next iteration. | |||
18655 | } | |||
18656 | ||||
18657 | ||||
18658 | ||||
18659 | MVT ExtVT; | |||
18660 | switch (VT.SimpleTy) { | |||
18661 | default: | |||
18662 | llvm_unreachable("Expected a vector of i1 elements")::llvm::llvm_unreachable_internal("Expected a vector of i1 elements" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18662); | |||
18663 | case MVT::v2i1: | |||
18664 | ExtVT = MVT::v2i64; | |||
18665 | break; | |||
18666 | case MVT::v4i1: | |||
18667 | ExtVT = MVT::v4i32; | |||
18668 | break; | |||
18669 | case MVT::v8i1: | |||
18670 | // Take 512-bit type, more shuffles on KNL. If we have VLX use a 256-bit | |||
18671 | // shuffle. | |||
18672 | ExtVT = Subtarget.hasVLX() ? MVT::v8i32 : MVT::v8i64; | |||
18673 | break; | |||
18674 | case MVT::v16i1: | |||
18675 | // Take 512-bit type, unless we are avoiding 512-bit types and have the | |||
18676 | // 256-bit operation available. | |||
18677 | ExtVT = Subtarget.canExtendTo512DQ() ? MVT::v16i32 : MVT::v16i16; | |||
18678 | break; | |||
18679 | case MVT::v32i1: | |||
18680 | // Take 512-bit type, unless we are avoiding 512-bit types and have the | |||
18681 | // 256-bit operation available. | |||
18682 | assert(Subtarget.hasBWI() && "Expected AVX512BW support")(static_cast <bool> (Subtarget.hasBWI() && "Expected AVX512BW support" ) ? void (0) : __assert_fail ("Subtarget.hasBWI() && \"Expected AVX512BW support\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18682, __extension__ __PRETTY_FUNCTION__)); | |||
18683 | ExtVT = Subtarget.canExtendTo512BW() ? MVT::v32i16 : MVT::v32i8; | |||
18684 | break; | |||
18685 | case MVT::v64i1: | |||
18686 | // Fall back to scalarization. FIXME: We can do better if the shuffle | |||
18687 | // can be partitioned cleanly. | |||
18688 | if (!Subtarget.useBWIRegs()) | |||
18689 | return SDValue(); | |||
18690 | ExtVT = MVT::v64i8; | |||
18691 | break; | |||
18692 | } | |||
18693 | ||||
18694 | V1 = DAG.getNode(ISD::SIGN_EXTEND, DL, ExtVT, V1); | |||
18695 | V2 = DAG.getNode(ISD::SIGN_EXTEND, DL, ExtVT, V2); | |||
18696 | ||||
18697 | SDValue Shuffle = DAG.getVectorShuffle(ExtVT, DL, V1, V2, Mask); | |||
18698 | // i1 was sign extended we can use X86ISD::CVT2MASK. | |||
18699 | int NumElems = VT.getVectorNumElements(); | |||
18700 | if ((Subtarget.hasBWI() && (NumElems >= 32)) || | |||
18701 | (Subtarget.hasDQI() && (NumElems < 32))) | |||
18702 | return DAG.getSetCC(DL, VT, DAG.getConstant(0, DL, ExtVT), | |||
18703 | Shuffle, ISD::SETGT); | |||
18704 | ||||
18705 | return DAG.getNode(ISD::TRUNCATE, DL, VT, Shuffle); | |||
18706 | } | |||
18707 | ||||
18708 | /// Helper function that returns true if the shuffle mask should be | |||
18709 | /// commuted to improve canonicalization. | |||
18710 | static bool canonicalizeShuffleMaskWithCommute(ArrayRef<int> Mask) { | |||
18711 | int NumElements = Mask.size(); | |||
18712 | ||||
18713 | int NumV1Elements = 0, NumV2Elements = 0; | |||
18714 | for (int M : Mask) | |||
18715 | if (M < 0) | |||
18716 | continue; | |||
18717 | else if (M < NumElements) | |||
18718 | ++NumV1Elements; | |||
18719 | else | |||
18720 | ++NumV2Elements; | |||
18721 | ||||
18722 | // Commute the shuffle as needed such that more elements come from V1 than | |||
18723 | // V2. This allows us to match the shuffle pattern strictly on how many | |||
18724 | // elements come from V1 without handling the symmetric cases. | |||
18725 | if (NumV2Elements > NumV1Elements) | |||
18726 | return true; | |||
18727 | ||||
18728 | assert(NumV1Elements > 0 && "No V1 indices")(static_cast <bool> (NumV1Elements > 0 && "No V1 indices" ) ? void (0) : __assert_fail ("NumV1Elements > 0 && \"No V1 indices\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18728, __extension__ __PRETTY_FUNCTION__)); | |||
18729 | ||||
18730 | if (NumV2Elements == 0) | |||
18731 | return false; | |||
18732 | ||||
18733 | // When the number of V1 and V2 elements are the same, try to minimize the | |||
18734 | // number of uses of V2 in the low half of the vector. When that is tied, | |||
18735 | // ensure that the sum of indices for V1 is equal to or lower than the sum | |||
18736 | // indices for V2. When those are equal, try to ensure that the number of odd | |||
18737 | // indices for V1 is lower than the number of odd indices for V2. | |||
18738 | if (NumV1Elements == NumV2Elements) { | |||
18739 | int LowV1Elements = 0, LowV2Elements = 0; | |||
18740 | for (int M : Mask.slice(0, NumElements / 2)) | |||
18741 | if (M >= NumElements) | |||
18742 | ++LowV2Elements; | |||
18743 | else if (M >= 0) | |||
18744 | ++LowV1Elements; | |||
18745 | if (LowV2Elements > LowV1Elements) | |||
18746 | return true; | |||
18747 | if (LowV2Elements == LowV1Elements) { | |||
18748 | int SumV1Indices = 0, SumV2Indices = 0; | |||
18749 | for (int i = 0, Size = Mask.size(); i < Size; ++i) | |||
18750 | if (Mask[i] >= NumElements) | |||
18751 | SumV2Indices += i; | |||
18752 | else if (Mask[i] >= 0) | |||
18753 | SumV1Indices += i; | |||
18754 | if (SumV2Indices < SumV1Indices) | |||
18755 | return true; | |||
18756 | if (SumV2Indices == SumV1Indices) { | |||
18757 | int NumV1OddIndices = 0, NumV2OddIndices = 0; | |||
18758 | for (int i = 0, Size = Mask.size(); i < Size; ++i) | |||
18759 | if (Mask[i] >= NumElements) | |||
18760 | NumV2OddIndices += i % 2; | |||
18761 | else if (Mask[i] >= 0) | |||
18762 | NumV1OddIndices += i % 2; | |||
18763 | if (NumV2OddIndices < NumV1OddIndices) | |||
18764 | return true; | |||
18765 | } | |||
18766 | } | |||
18767 | } | |||
18768 | ||||
18769 | return false; | |||
18770 | } | |||
18771 | ||||
18772 | // Forward declaration. | |||
18773 | static SDValue canonicalizeShuffleMaskWithHorizOp( | |||
18774 | MutableArrayRef<SDValue> Ops, MutableArrayRef<int> Mask, | |||
18775 | unsigned RootSizeInBits, const SDLoc &DL, SelectionDAG &DAG, | |||
18776 | const X86Subtarget &Subtarget); | |||
18777 | ||||
18778 | /// Top-level lowering for x86 vector shuffles. | |||
18779 | /// | |||
18780 | /// This handles decomposition, canonicalization, and lowering of all x86 | |||
18781 | /// vector shuffles. Most of the specific lowering strategies are encapsulated | |||
18782 | /// above in helper routines. The canonicalization attempts to widen shuffles | |||
18783 | /// to involve fewer lanes of wider elements, consolidate symmetric patterns | |||
18784 | /// s.t. only one of the two inputs needs to be tested, etc. | |||
18785 | static SDValue lowerVECTOR_SHUFFLE(SDValue Op, const X86Subtarget &Subtarget, | |||
18786 | SelectionDAG &DAG) { | |||
18787 | ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op); | |||
18788 | ArrayRef<int> OrigMask = SVOp->getMask(); | |||
18789 | SDValue V1 = Op.getOperand(0); | |||
18790 | SDValue V2 = Op.getOperand(1); | |||
18791 | MVT VT = Op.getSimpleValueType(); | |||
18792 | int NumElements = VT.getVectorNumElements(); | |||
18793 | SDLoc DL(Op); | |||
18794 | bool Is1BitVector = (VT.getVectorElementType() == MVT::i1); | |||
18795 | ||||
18796 | assert((VT.getSizeInBits() != 64 || Is1BitVector) &&(static_cast <bool> ((VT.getSizeInBits() != 64 || Is1BitVector ) && "Can't lower MMX shuffles") ? void (0) : __assert_fail ("(VT.getSizeInBits() != 64 || Is1BitVector) && \"Can't lower MMX shuffles\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18797, __extension__ __PRETTY_FUNCTION__)) | |||
18797 | "Can't lower MMX shuffles")(static_cast <bool> ((VT.getSizeInBits() != 64 || Is1BitVector ) && "Can't lower MMX shuffles") ? void (0) : __assert_fail ("(VT.getSizeInBits() != 64 || Is1BitVector) && \"Can't lower MMX shuffles\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18797, __extension__ __PRETTY_FUNCTION__)); | |||
18798 | ||||
18799 | bool V1IsUndef = V1.isUndef(); | |||
18800 | bool V2IsUndef = V2.isUndef(); | |||
18801 | if (V1IsUndef && V2IsUndef) | |||
18802 | return DAG.getUNDEF(VT); | |||
18803 | ||||
18804 | // When we create a shuffle node we put the UNDEF node to second operand, | |||
18805 | // but in some cases the first operand may be transformed to UNDEF. | |||
18806 | // In this case we should just commute the node. | |||
18807 | if (V1IsUndef) | |||
18808 | return DAG.getCommutedVectorShuffle(*SVOp); | |||
18809 | ||||
18810 | // Check for non-undef masks pointing at an undef vector and make the masks | |||
18811 | // undef as well. This makes it easier to match the shuffle based solely on | |||
18812 | // the mask. | |||
18813 | if (V2IsUndef && | |||
18814 | any_of(OrigMask, [NumElements](int M) { return M >= NumElements; })) { | |||
18815 | SmallVector<int, 8> NewMask(OrigMask.begin(), OrigMask.end()); | |||
18816 | for (int &M : NewMask) | |||
18817 | if (M >= NumElements) | |||
18818 | M = -1; | |||
18819 | return DAG.getVectorShuffle(VT, DL, V1, V2, NewMask); | |||
18820 | } | |||
18821 | ||||
18822 | // Check for illegal shuffle mask element index values. | |||
18823 | int MaskUpperLimit = OrigMask.size() * (V2IsUndef ? 1 : 2); | |||
18824 | (void)MaskUpperLimit; | |||
18825 | assert(llvm::all_of(OrigMask,(static_cast <bool> (llvm::all_of(OrigMask, [&](int M) { return -1 <= M && M < MaskUpperLimit; }) && "Out of bounds shuffle index") ? void (0) : __assert_fail ("llvm::all_of(OrigMask, [&](int M) { return -1 <= M && M < MaskUpperLimit; }) && \"Out of bounds shuffle index\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18827, __extension__ __PRETTY_FUNCTION__)) | |||
18826 | [&](int M) { return -1 <= M && M < MaskUpperLimit; }) &&(static_cast <bool> (llvm::all_of(OrigMask, [&](int M) { return -1 <= M && M < MaskUpperLimit; }) && "Out of bounds shuffle index") ? void (0) : __assert_fail ("llvm::all_of(OrigMask, [&](int M) { return -1 <= M && M < MaskUpperLimit; }) && \"Out of bounds shuffle index\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18827, __extension__ __PRETTY_FUNCTION__)) | |||
18827 | "Out of bounds shuffle index")(static_cast <bool> (llvm::all_of(OrigMask, [&](int M) { return -1 <= M && M < MaskUpperLimit; }) && "Out of bounds shuffle index") ? void (0) : __assert_fail ("llvm::all_of(OrigMask, [&](int M) { return -1 <= M && M < MaskUpperLimit; }) && \"Out of bounds shuffle index\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18827, __extension__ __PRETTY_FUNCTION__)); | |||
18828 | ||||
18829 | // We actually see shuffles that are entirely re-arrangements of a set of | |||
18830 | // zero inputs. This mostly happens while decomposing complex shuffles into | |||
18831 | // simple ones. Directly lower these as a buildvector of zeros. | |||
18832 | APInt KnownUndef, KnownZero; | |||
18833 | computeZeroableShuffleElements(OrigMask, V1, V2, KnownUndef, KnownZero); | |||
18834 | ||||
18835 | APInt Zeroable = KnownUndef | KnownZero; | |||
18836 | if (Zeroable.isAllOnes()) | |||
18837 | return getZeroVector(VT, Subtarget, DAG, DL); | |||
18838 | ||||
18839 | bool V2IsZero = !V2IsUndef && ISD::isBuildVectorAllZeros(V2.getNode()); | |||
18840 | ||||
18841 | // Try to collapse shuffles into using a vector type with fewer elements but | |||
18842 | // wider element types. We cap this to not form integers or floating point | |||
18843 | // elements wider than 64 bits. It does not seem beneficial to form i128 | |||
18844 | // integers to handle flipping the low and high halves of AVX 256-bit vectors. | |||
18845 | SmallVector<int, 16> WidenedMask; | |||
18846 | if (VT.getScalarSizeInBits() < 64 && !Is1BitVector && | |||
18847 | canWidenShuffleElements(OrigMask, Zeroable, V2IsZero, WidenedMask)) { | |||
18848 | // Shuffle mask widening should not interfere with a broadcast opportunity | |||
18849 | // by obfuscating the operands with bitcasts. | |||
18850 | // TODO: Avoid lowering directly from this top-level function: make this | |||
18851 | // a query (canLowerAsBroadcast) and defer lowering to the type-based calls. | |||
18852 | if (SDValue Broadcast = lowerShuffleAsBroadcast(DL, VT, V1, V2, OrigMask, | |||
18853 | Subtarget, DAG)) | |||
18854 | return Broadcast; | |||
18855 | ||||
18856 | MVT NewEltVT = VT.isFloatingPoint() | |||
18857 | ? MVT::getFloatingPointVT(VT.getScalarSizeInBits() * 2) | |||
18858 | : MVT::getIntegerVT(VT.getScalarSizeInBits() * 2); | |||
18859 | int NewNumElts = NumElements / 2; | |||
18860 | MVT NewVT = MVT::getVectorVT(NewEltVT, NewNumElts); | |||
18861 | // Make sure that the new vector type is legal. For example, v2f64 isn't | |||
18862 | // legal on SSE1. | |||
18863 | if (DAG.getTargetLoweringInfo().isTypeLegal(NewVT)) { | |||
18864 | if (V2IsZero) { | |||
18865 | // Modify the new Mask to take all zeros from the all-zero vector. | |||
18866 | // Choose indices that are blend-friendly. | |||
18867 | bool UsedZeroVector = false; | |||
18868 | assert(is_contained(WidenedMask, SM_SentinelZero) &&(static_cast <bool> (is_contained(WidenedMask, SM_SentinelZero ) && "V2's non-undef elements are used?!") ? void (0) : __assert_fail ("is_contained(WidenedMask, SM_SentinelZero) && \"V2's non-undef elements are used?!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18869, __extension__ __PRETTY_FUNCTION__)) | |||
18869 | "V2's non-undef elements are used?!")(static_cast <bool> (is_contained(WidenedMask, SM_SentinelZero ) && "V2's non-undef elements are used?!") ? void (0) : __assert_fail ("is_contained(WidenedMask, SM_SentinelZero) && \"V2's non-undef elements are used?!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18869, __extension__ __PRETTY_FUNCTION__)); | |||
18870 | for (int i = 0; i != NewNumElts; ++i) | |||
18871 | if (WidenedMask[i] == SM_SentinelZero) { | |||
18872 | WidenedMask[i] = i + NewNumElts; | |||
18873 | UsedZeroVector = true; | |||
18874 | } | |||
18875 | // Ensure all elements of V2 are zero - isBuildVectorAllZeros permits | |||
18876 | // some elements to be undef. | |||
18877 | if (UsedZeroVector) | |||
18878 | V2 = getZeroVector(NewVT, Subtarget, DAG, DL); | |||
18879 | } | |||
18880 | V1 = DAG.getBitcast(NewVT, V1); | |||
18881 | V2 = DAG.getBitcast(NewVT, V2); | |||
18882 | return DAG.getBitcast( | |||
18883 | VT, DAG.getVectorShuffle(NewVT, DL, V1, V2, WidenedMask)); | |||
18884 | } | |||
18885 | } | |||
18886 | ||||
18887 | SmallVector<SDValue> Ops = {V1, V2}; | |||
18888 | SmallVector<int> Mask(OrigMask.begin(), OrigMask.end()); | |||
18889 | ||||
18890 | // Canonicalize the shuffle with any horizontal ops inputs. | |||
18891 | // NOTE: This may update Ops and Mask. | |||
18892 | if (SDValue HOp = canonicalizeShuffleMaskWithHorizOp( | |||
18893 | Ops, Mask, VT.getSizeInBits(), DL, DAG, Subtarget)) | |||
18894 | return DAG.getBitcast(VT, HOp); | |||
18895 | ||||
18896 | V1 = DAG.getBitcast(VT, Ops[0]); | |||
18897 | V2 = DAG.getBitcast(VT, Ops[1]); | |||
18898 | assert(NumElements == (int)Mask.size() &&(static_cast <bool> (NumElements == (int)Mask.size() && "canonicalizeShuffleMaskWithHorizOp " "shouldn't alter the shuffle mask size" ) ? void (0) : __assert_fail ("NumElements == (int)Mask.size() && \"canonicalizeShuffleMaskWithHorizOp \" \"shouldn't alter the shuffle mask size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18900, __extension__ __PRETTY_FUNCTION__)) | |||
18899 | "canonicalizeShuffleMaskWithHorizOp "(static_cast <bool> (NumElements == (int)Mask.size() && "canonicalizeShuffleMaskWithHorizOp " "shouldn't alter the shuffle mask size" ) ? void (0) : __assert_fail ("NumElements == (int)Mask.size() && \"canonicalizeShuffleMaskWithHorizOp \" \"shouldn't alter the shuffle mask size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18900, __extension__ __PRETTY_FUNCTION__)) | |||
18900 | "shouldn't alter the shuffle mask size")(static_cast <bool> (NumElements == (int)Mask.size() && "canonicalizeShuffleMaskWithHorizOp " "shouldn't alter the shuffle mask size" ) ? void (0) : __assert_fail ("NumElements == (int)Mask.size() && \"canonicalizeShuffleMaskWithHorizOp \" \"shouldn't alter the shuffle mask size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18900, __extension__ __PRETTY_FUNCTION__)); | |||
18901 | ||||
18902 | // Commute the shuffle if it will improve canonicalization. | |||
18903 | if (canonicalizeShuffleMaskWithCommute(Mask)) { | |||
18904 | ShuffleVectorSDNode::commuteMask(Mask); | |||
18905 | std::swap(V1, V2); | |||
18906 | } | |||
18907 | ||||
18908 | // For each vector width, delegate to a specialized lowering routine. | |||
18909 | if (VT.is128BitVector()) | |||
18910 | return lower128BitShuffle(DL, Mask, VT, V1, V2, Zeroable, Subtarget, DAG); | |||
18911 | ||||
18912 | if (VT.is256BitVector()) | |||
18913 | return lower256BitShuffle(DL, Mask, VT, V1, V2, Zeroable, Subtarget, DAG); | |||
18914 | ||||
18915 | if (VT.is512BitVector()) | |||
18916 | return lower512BitShuffle(DL, Mask, VT, V1, V2, Zeroable, Subtarget, DAG); | |||
18917 | ||||
18918 | if (Is1BitVector) | |||
18919 | return lower1BitShuffle(DL, Mask, VT, V1, V2, Zeroable, Subtarget, DAG); | |||
18920 | ||||
18921 | llvm_unreachable("Unimplemented!")::llvm::llvm_unreachable_internal("Unimplemented!", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 18921); | |||
18922 | } | |||
18923 | ||||
18924 | /// Try to lower a VSELECT instruction to a vector shuffle. | |||
18925 | static SDValue lowerVSELECTtoVectorShuffle(SDValue Op, | |||
18926 | const X86Subtarget &Subtarget, | |||
18927 | SelectionDAG &DAG) { | |||
18928 | SDValue Cond = Op.getOperand(0); | |||
18929 | SDValue LHS = Op.getOperand(1); | |||
18930 | SDValue RHS = Op.getOperand(2); | |||
18931 | MVT VT = Op.getSimpleValueType(); | |||
18932 | ||||
18933 | // Only non-legal VSELECTs reach this lowering, convert those into generic | |||
18934 | // shuffles and re-use the shuffle lowering path for blends. | |||
18935 | if (ISD::isBuildVectorOfConstantSDNodes(Cond.getNode())) { | |||
18936 | SmallVector<int, 32> Mask; | |||
18937 | if (createShuffleMaskFromVSELECT(Mask, Cond)) | |||
18938 | return DAG.getVectorShuffle(VT, SDLoc(Op), LHS, RHS, Mask); | |||
18939 | } | |||
18940 | ||||
18941 | return SDValue(); | |||
18942 | } | |||
18943 | ||||
18944 | SDValue X86TargetLowering::LowerVSELECT(SDValue Op, SelectionDAG &DAG) const { | |||
18945 | SDValue Cond = Op.getOperand(0); | |||
18946 | SDValue LHS = Op.getOperand(1); | |||
18947 | SDValue RHS = Op.getOperand(2); | |||
18948 | ||||
18949 | // A vselect where all conditions and data are constants can be optimized into | |||
18950 | // a single vector load by SelectionDAGLegalize::ExpandBUILD_VECTOR(). | |||
18951 | if (ISD::isBuildVectorOfConstantSDNodes(Cond.getNode()) && | |||
18952 | ISD::isBuildVectorOfConstantSDNodes(LHS.getNode()) && | |||
18953 | ISD::isBuildVectorOfConstantSDNodes(RHS.getNode())) | |||
18954 | return SDValue(); | |||
18955 | ||||
18956 | // Try to lower this to a blend-style vector shuffle. This can handle all | |||
18957 | // constant condition cases. | |||
18958 | if (SDValue BlendOp = lowerVSELECTtoVectorShuffle(Op, Subtarget, DAG)) | |||
18959 | return BlendOp; | |||
18960 | ||||
18961 | // If this VSELECT has a vector if i1 as a mask, it will be directly matched | |||
18962 | // with patterns on the mask registers on AVX-512. | |||
18963 | MVT CondVT = Cond.getSimpleValueType(); | |||
18964 | unsigned CondEltSize = Cond.getScalarValueSizeInBits(); | |||
18965 | if (CondEltSize == 1) | |||
18966 | return Op; | |||
18967 | ||||
18968 | // Variable blends are only legal from SSE4.1 onward. | |||
18969 | if (!Subtarget.hasSSE41()) | |||
18970 | return SDValue(); | |||
18971 | ||||
18972 | SDLoc dl(Op); | |||
18973 | MVT VT = Op.getSimpleValueType(); | |||
18974 | unsigned EltSize = VT.getScalarSizeInBits(); | |||
18975 | unsigned NumElts = VT.getVectorNumElements(); | |||
18976 | ||||
18977 | // Expand v32i16/v64i8 without BWI. | |||
18978 | if ((VT == MVT::v32i16 || VT == MVT::v64i8) && !Subtarget.hasBWI()) | |||
18979 | return SDValue(); | |||
18980 | ||||
18981 | // If the VSELECT is on a 512-bit type, we have to convert a non-i1 condition | |||
18982 | // into an i1 condition so that we can use the mask-based 512-bit blend | |||
18983 | // instructions. | |||
18984 | if (VT.getSizeInBits() == 512) { | |||
18985 | // Build a mask by testing the condition against zero. | |||
18986 | MVT MaskVT = MVT::getVectorVT(MVT::i1, NumElts); | |||
18987 | SDValue Mask = DAG.getSetCC(dl, MaskVT, Cond, | |||
18988 | DAG.getConstant(0, dl, CondVT), | |||
18989 | ISD::SETNE); | |||
18990 | // Now return a new VSELECT using the mask. | |||
18991 | return DAG.getSelect(dl, VT, Mask, LHS, RHS); | |||
18992 | } | |||
18993 | ||||
18994 | // SEXT/TRUNC cases where the mask doesn't match the destination size. | |||
18995 | if (CondEltSize != EltSize) { | |||
18996 | // If we don't have a sign splat, rely on the expansion. | |||
18997 | if (CondEltSize != DAG.ComputeNumSignBits(Cond)) | |||
18998 | return SDValue(); | |||
18999 | ||||
19000 | MVT NewCondSVT = MVT::getIntegerVT(EltSize); | |||
19001 | MVT NewCondVT = MVT::getVectorVT(NewCondSVT, NumElts); | |||
19002 | Cond = DAG.getSExtOrTrunc(Cond, dl, NewCondVT); | |||
19003 | return DAG.getNode(ISD::VSELECT, dl, VT, Cond, LHS, RHS); | |||
19004 | } | |||
19005 | ||||
19006 | // Only some types will be legal on some subtargets. If we can emit a legal | |||
19007 | // VSELECT-matching blend, return Op, and but if we need to expand, return | |||
19008 | // a null value. | |||
19009 | switch (VT.SimpleTy) { | |||
19010 | default: | |||
19011 | // Most of the vector types have blends past SSE4.1. | |||
19012 | return Op; | |||
19013 | ||||
19014 | case MVT::v32i8: | |||
19015 | // The byte blends for AVX vectors were introduced only in AVX2. | |||
19016 | if (Subtarget.hasAVX2()) | |||
19017 | return Op; | |||
19018 | ||||
19019 | return SDValue(); | |||
19020 | ||||
19021 | case MVT::v8i16: | |||
19022 | case MVT::v16i16: { | |||
19023 | // Bitcast everything to the vXi8 type and use a vXi8 vselect. | |||
19024 | MVT CastVT = MVT::getVectorVT(MVT::i8, NumElts * 2); | |||
19025 | Cond = DAG.getBitcast(CastVT, Cond); | |||
19026 | LHS = DAG.getBitcast(CastVT, LHS); | |||
19027 | RHS = DAG.getBitcast(CastVT, RHS); | |||
19028 | SDValue Select = DAG.getNode(ISD::VSELECT, dl, CastVT, Cond, LHS, RHS); | |||
19029 | return DAG.getBitcast(VT, Select); | |||
19030 | } | |||
19031 | } | |||
19032 | } | |||
19033 | ||||
19034 | static SDValue LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG) { | |||
19035 | MVT VT = Op.getSimpleValueType(); | |||
19036 | SDValue Vec = Op.getOperand(0); | |||
19037 | SDValue Idx = Op.getOperand(1); | |||
19038 | assert(isa<ConstantSDNode>(Idx) && "Constant index expected")(static_cast <bool> (isa<ConstantSDNode>(Idx) && "Constant index expected") ? void (0) : __assert_fail ("isa<ConstantSDNode>(Idx) && \"Constant index expected\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 19038, __extension__ __PRETTY_FUNCTION__)); | |||
19039 | SDLoc dl(Op); | |||
19040 | ||||
19041 | if (!Vec.getSimpleValueType().is128BitVector()) | |||
19042 | return SDValue(); | |||
19043 | ||||
19044 | if (VT.getSizeInBits() == 8) { | |||
19045 | // If IdxVal is 0, it's cheaper to do a move instead of a pextrb, unless | |||
19046 | // we're going to zero extend the register or fold the store. | |||
19047 | if (llvm::isNullConstant(Idx) && !MayFoldIntoZeroExtend(Op) && | |||
19048 | !MayFoldIntoStore(Op)) | |||
19049 | return DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, | |||
19050 | DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32, | |||
19051 | DAG.getBitcast(MVT::v4i32, Vec), Idx)); | |||
19052 | ||||
19053 | unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue(); | |||
19054 | SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32, Vec, | |||
19055 | DAG.getTargetConstant(IdxVal, dl, MVT::i8)); | |||
19056 | return DAG.getNode(ISD::TRUNCATE, dl, VT, Extract); | |||
19057 | } | |||
19058 | ||||
19059 | if (VT == MVT::f32) { | |||
19060 | // EXTRACTPS outputs to a GPR32 register which will require a movd to copy | |||
19061 | // the result back to FR32 register. It's only worth matching if the | |||
19062 | // result has a single use which is a store or a bitcast to i32. And in | |||
19063 | // the case of a store, it's not worth it if the index is a constant 0, | |||
19064 | // because a MOVSSmr can be used instead, which is smaller and faster. | |||
19065 | if (!Op.hasOneUse()) | |||
19066 | return SDValue(); | |||
19067 | SDNode *User = *Op.getNode()->use_begin(); | |||
19068 | if ((User->getOpcode() != ISD::STORE || isNullConstant(Idx)) && | |||
19069 | (User->getOpcode() != ISD::BITCAST || | |||
19070 | User->getValueType(0) != MVT::i32)) | |||
19071 | return SDValue(); | |||
19072 | SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32, | |||
19073 | DAG.getBitcast(MVT::v4i32, Vec), Idx); | |||
19074 | return DAG.getBitcast(MVT::f32, Extract); | |||
19075 | } | |||
19076 | ||||
19077 | if (VT == MVT::i32 || VT == MVT::i64) | |||
19078 | return Op; | |||
19079 | ||||
19080 | return SDValue(); | |||
19081 | } | |||
19082 | ||||
19083 | /// Extract one bit from mask vector, like v16i1 or v8i1. | |||
19084 | /// AVX-512 feature. | |||
19085 | static SDValue ExtractBitFromMaskVector(SDValue Op, SelectionDAG &DAG, | |||
19086 | const X86Subtarget &Subtarget) { | |||
19087 | SDValue Vec = Op.getOperand(0); | |||
19088 | SDLoc dl(Vec); | |||
19089 | MVT VecVT = Vec.getSimpleValueType(); | |||
19090 | SDValue Idx = Op.getOperand(1); | |||
19091 | auto* IdxC = dyn_cast<ConstantSDNode>(Idx); | |||
19092 | MVT EltVT = Op.getSimpleValueType(); | |||
19093 | ||||
19094 | assert((VecVT.getVectorNumElements() <= 16 || Subtarget.hasBWI()) &&(static_cast <bool> ((VecVT.getVectorNumElements() <= 16 || Subtarget.hasBWI()) && "Unexpected vector type in ExtractBitFromMaskVector" ) ? void (0) : __assert_fail ("(VecVT.getVectorNumElements() <= 16 || Subtarget.hasBWI()) && \"Unexpected vector type in ExtractBitFromMaskVector\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 19095, __extension__ __PRETTY_FUNCTION__)) | |||
19095 | "Unexpected vector type in ExtractBitFromMaskVector")(static_cast <bool> ((VecVT.getVectorNumElements() <= 16 || Subtarget.hasBWI()) && "Unexpected vector type in ExtractBitFromMaskVector" ) ? void (0) : __assert_fail ("(VecVT.getVectorNumElements() <= 16 || Subtarget.hasBWI()) && \"Unexpected vector type in ExtractBitFromMaskVector\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 19095, __extension__ __PRETTY_FUNCTION__)); | |||
19096 | ||||
19097 | // variable index can't be handled in mask registers, | |||
19098 | // extend vector to VR512/128 | |||
19099 | if (!IdxC) { | |||
19100 | unsigned NumElts = VecVT.getVectorNumElements(); | |||
19101 | // Extending v8i1/v16i1 to 512-bit get better performance on KNL | |||
19102 | // than extending to 128/256bit. | |||
19103 | MVT ExtEltVT = (NumElts <= 8) ? MVT::getIntegerVT(128 / NumElts) : MVT::i8; | |||
19104 | MVT ExtVecVT = MVT::getVectorVT(ExtEltVT, NumElts); | |||
19105 | SDValue Ext = DAG.getNode(ISD::SIGN_EXTEND, dl, ExtVecVT, Vec); | |||
19106 | SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, ExtEltVT, Ext, Idx); | |||
19107 | return DAG.getNode(ISD::TRUNCATE, dl, EltVT, Elt); | |||
19108 | } | |||
19109 | ||||
19110 | unsigned IdxVal = IdxC->getZExtValue(); | |||
19111 | if (IdxVal == 0) // the operation is legal | |||
19112 | return Op; | |||
19113 | ||||
19114 | // Extend to natively supported kshift. | |||
19115 | unsigned NumElems = VecVT.getVectorNumElements(); | |||
19116 | MVT WideVecVT = VecVT; | |||
19117 | if ((!Subtarget.hasDQI() && NumElems == 8) || NumElems < 8) { | |||
19118 | WideVecVT = Subtarget.hasDQI() ? MVT::v8i1 : MVT::v16i1; | |||
19119 | Vec = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, WideVecVT, | |||
19120 | DAG.getUNDEF(WideVecVT), Vec, | |||
19121 | DAG.getIntPtrConstant(0, dl)); | |||
19122 | } | |||
19123 | ||||
19124 | // Use kshiftr instruction to move to the lower element. | |||
19125 | Vec = DAG.getNode(X86ISD::KSHIFTR, dl, WideVecVT, Vec, | |||
19126 | DAG.getTargetConstant(IdxVal, dl, MVT::i8)); | |||
19127 | ||||
19128 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec, | |||
19129 | DAG.getIntPtrConstant(0, dl)); | |||
19130 | } | |||
19131 | ||||
19132 | SDValue | |||
19133 | X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, | |||
19134 | SelectionDAG &DAG) const { | |||
19135 | SDLoc dl(Op); | |||
19136 | SDValue Vec = Op.getOperand(0); | |||
19137 | MVT VecVT = Vec.getSimpleValueType(); | |||
19138 | SDValue Idx = Op.getOperand(1); | |||
19139 | auto* IdxC = dyn_cast<ConstantSDNode>(Idx); | |||
19140 | ||||
19141 | if (VecVT.getVectorElementType() == MVT::i1) | |||
19142 | return ExtractBitFromMaskVector(Op, DAG, Subtarget); | |||
19143 | ||||
19144 | if (!IdxC) { | |||
19145 | // Its more profitable to go through memory (1 cycles throughput) | |||
19146 | // than using VMOVD + VPERMV/PSHUFB sequence ( 2/3 cycles throughput) | |||
19147 | // IACA tool was used to get performance estimation | |||
19148 | // (https://software.intel.com/en-us/articles/intel-architecture-code-analyzer) | |||
19149 | // | |||
19150 | // example : extractelement <16 x i8> %a, i32 %i | |||
19151 | // | |||
19152 | // Block Throughput: 3.00 Cycles | |||
19153 | // Throughput Bottleneck: Port5 | |||
19154 | // | |||
19155 | // | Num Of | Ports pressure in cycles | | | |||
19156 | // | Uops | 0 - DV | 5 | 6 | 7 | | | |||
19157 | // --------------------------------------------- | |||
19158 | // | 1 | | 1.0 | | | CP | vmovd xmm1, edi | |||
19159 | // | 1 | | 1.0 | | | CP | vpshufb xmm0, xmm0, xmm1 | |||
19160 | // | 2 | 1.0 | 1.0 | | | CP | vpextrb eax, xmm0, 0x0 | |||
19161 | // Total Num Of Uops: 4 | |||
19162 | // | |||
19163 | // | |||
19164 | // Block Throughput: 1.00 Cycles | |||
19165 | // Throughput Bottleneck: PORT2_AGU, PORT3_AGU, Port4 | |||
19166 | // | |||
19167 | // | | Ports pressure in cycles | | | |||
19168 | // |Uops| 1 | 2 - D |3 - D | 4 | 5 | | | |||
19169 | // --------------------------------------------------------- | |||
19170 | // |2^ | | 0.5 | 0.5 |1.0| |CP| vmovaps xmmword ptr [rsp-0x18], xmm0 | |||
19171 | // |1 |0.5| | | |0.5| | lea rax, ptr [rsp-0x18] | |||
19172 | // |1 | |0.5, 0.5|0.5, 0.5| | |CP| mov al, byte ptr [rdi+rax*1] | |||
19173 | // Total Num Of Uops: 4 | |||
19174 | ||||
19175 | return SDValue(); | |||
19176 | } | |||
19177 | ||||
19178 | unsigned IdxVal = IdxC->getZExtValue(); | |||
19179 | ||||
19180 | // If this is a 256-bit vector result, first extract the 128-bit vector and | |||
19181 | // then extract the element from the 128-bit vector. | |||
19182 | if (VecVT.is256BitVector() || VecVT.is512BitVector()) { | |||
19183 | // Get the 128-bit vector. | |||
19184 | Vec = extract128BitVector(Vec, IdxVal, DAG, dl); | |||
19185 | MVT EltVT = VecVT.getVectorElementType(); | |||
19186 | ||||
19187 | unsigned ElemsPerChunk = 128 / EltVT.getSizeInBits(); | |||
19188 | assert(isPowerOf2_32(ElemsPerChunk) && "Elements per chunk not power of 2")(static_cast <bool> (isPowerOf2_32(ElemsPerChunk) && "Elements per chunk not power of 2") ? void (0) : __assert_fail ("isPowerOf2_32(ElemsPerChunk) && \"Elements per chunk not power of 2\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 19188, __extension__ __PRETTY_FUNCTION__)); | |||
19189 | ||||
19190 | // Find IdxVal modulo ElemsPerChunk. Since ElemsPerChunk is a power of 2 | |||
19191 | // this can be done with a mask. | |||
19192 | IdxVal &= ElemsPerChunk - 1; | |||
19193 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec, | |||
19194 | DAG.getIntPtrConstant(IdxVal, dl)); | |||
19195 | } | |||
19196 | ||||
19197 | assert(VecVT.is128BitVector() && "Unexpected vector length")(static_cast <bool> (VecVT.is128BitVector() && "Unexpected vector length" ) ? void (0) : __assert_fail ("VecVT.is128BitVector() && \"Unexpected vector length\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 19197, __extension__ __PRETTY_FUNCTION__)); | |||
19198 | ||||
19199 | MVT VT = Op.getSimpleValueType(); | |||
19200 | ||||
19201 | if (VT == MVT::i16) { | |||
19202 | // If IdxVal is 0, it's cheaper to do a move instead of a pextrw, unless | |||
19203 | // we're going to zero extend the register or fold the store (SSE41 only). | |||
19204 | if (IdxVal == 0 && !MayFoldIntoZeroExtend(Op) && | |||
19205 | !(Subtarget.hasSSE41() && MayFoldIntoStore(Op))) { | |||
19206 | if (Subtarget.hasFP16()) | |||
19207 | return Op; | |||
19208 | ||||
19209 | return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, | |||
19210 | DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32, | |||
19211 | DAG.getBitcast(MVT::v4i32, Vec), Idx)); | |||
19212 | } | |||
19213 | ||||
19214 | SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32, Vec, | |||
19215 | DAG.getTargetConstant(IdxVal, dl, MVT::i8)); | |||
19216 | return DAG.getNode(ISD::TRUNCATE, dl, VT, Extract); | |||
19217 | } | |||
19218 | ||||
19219 | if (Subtarget.hasSSE41()) | |||
19220 | if (SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG)) | |||
19221 | return Res; | |||
19222 | ||||
19223 | // TODO: We only extract a single element from v16i8, we can probably afford | |||
19224 | // to be more aggressive here before using the default approach of spilling to | |||
19225 | // stack. | |||
19226 | if (VT.getSizeInBits() == 8 && Op->isOnlyUserOf(Vec.getNode())) { | |||
19227 | // Extract either the lowest i32 or any i16, and extract the sub-byte. | |||
19228 | int DWordIdx = IdxVal / 4; | |||
19229 | if (DWordIdx == 0) { | |||
19230 | SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32, | |||
19231 | DAG.getBitcast(MVT::v4i32, Vec), | |||
19232 | DAG.getIntPtrConstant(DWordIdx, dl)); | |||
19233 | int ShiftVal = (IdxVal % 4) * 8; | |||
19234 | if (ShiftVal != 0) | |||
19235 | Res = DAG.getNode(ISD::SRL, dl, MVT::i32, Res, | |||
19236 | DAG.getConstant(ShiftVal, dl, MVT::i8)); | |||
19237 | return DAG.getNode(ISD::TRUNCATE, dl, VT, Res); | |||
19238 | } | |||
19239 | ||||
19240 | int WordIdx = IdxVal / 2; | |||
19241 | SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, | |||
19242 | DAG.getBitcast(MVT::v8i16, Vec), | |||
19243 | DAG.getIntPtrConstant(WordIdx, dl)); | |||
19244 | int ShiftVal = (IdxVal % 2) * 8; | |||
19245 | if (ShiftVal != 0) | |||
19246 | Res = DAG.getNode(ISD::SRL, dl, MVT::i16, Res, | |||
19247 | DAG.getConstant(ShiftVal, dl, MVT::i8)); | |||
19248 | return DAG.getNode(ISD::TRUNCATE, dl, VT, Res); | |||
19249 | } | |||
19250 | ||||
19251 | if (VT == MVT::f16 || VT.getSizeInBits() == 32) { | |||
19252 | if (IdxVal == 0) | |||
19253 | return Op; | |||
19254 | ||||
19255 | // Shuffle the element to the lowest element, then movss or movsh. | |||
19256 | SmallVector<int, 8> Mask(VecVT.getVectorNumElements(), -1); | |||
19257 | Mask[0] = static_cast<int>(IdxVal); | |||
19258 | Vec = DAG.getVectorShuffle(VecVT, dl, Vec, DAG.getUNDEF(VecVT), Mask); | |||
19259 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec, | |||
19260 | DAG.getIntPtrConstant(0, dl)); | |||
19261 | } | |||
19262 | ||||
19263 | if (VT.getSizeInBits() == 64) { | |||
19264 | // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b | |||
19265 | // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught | |||
19266 | // to match extract_elt for f64. | |||
19267 | if (IdxVal == 0) | |||
19268 | return Op; | |||
19269 | ||||
19270 | // UNPCKHPD the element to the lowest double word, then movsd. | |||
19271 | // Note if the lower 64 bits of the result of the UNPCKHPD is then stored | |||
19272 | // to a f64mem, the whole operation is folded into a single MOVHPDmr. | |||
19273 | int Mask[2] = { 1, -1 }; | |||
19274 | Vec = DAG.getVectorShuffle(VecVT, dl, Vec, DAG.getUNDEF(VecVT), Mask); | |||
19275 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec, | |||
19276 | DAG.getIntPtrConstant(0, dl)); | |||
19277 | } | |||
19278 | ||||
19279 | return SDValue(); | |||
19280 | } | |||
19281 | ||||
19282 | /// Insert one bit to mask vector, like v16i1 or v8i1. | |||
19283 | /// AVX-512 feature. | |||
19284 | static SDValue InsertBitToMaskVector(SDValue Op, SelectionDAG &DAG, | |||
19285 | const X86Subtarget &Subtarget) { | |||
19286 | SDLoc dl(Op); | |||
19287 | SDValue Vec = Op.getOperand(0); | |||
19288 | SDValue Elt = Op.getOperand(1); | |||
19289 | SDValue Idx = Op.getOperand(2); | |||
19290 | MVT VecVT = Vec.getSimpleValueType(); | |||
19291 | ||||
19292 | if (!isa<ConstantSDNode>(Idx)) { | |||
19293 | // Non constant index. Extend source and destination, | |||
19294 | // insert element and then truncate the result. | |||
19295 | unsigned NumElts = VecVT.getVectorNumElements(); | |||
19296 | MVT ExtEltVT = (NumElts <= 8) ? MVT::getIntegerVT(128 / NumElts) : MVT::i8; | |||
19297 | MVT ExtVecVT = MVT::getVectorVT(ExtEltVT, NumElts); | |||
19298 | SDValue ExtOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ExtVecVT, | |||
19299 | DAG.getNode(ISD::SIGN_EXTEND, dl, ExtVecVT, Vec), | |||
19300 | DAG.getNode(ISD::SIGN_EXTEND, dl, ExtEltVT, Elt), Idx); | |||
19301 | return DAG.getNode(ISD::TRUNCATE, dl, VecVT, ExtOp); | |||
19302 | } | |||
19303 | ||||
19304 | // Copy into a k-register, extract to v1i1 and insert_subvector. | |||
19305 | SDValue EltInVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i1, Elt); | |||
19306 | return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, VecVT, Vec, EltInVec, Idx); | |||
19307 | } | |||
19308 | ||||
19309 | SDValue X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, | |||
19310 | SelectionDAG &DAG) const { | |||
19311 | MVT VT = Op.getSimpleValueType(); | |||
19312 | MVT EltVT = VT.getVectorElementType(); | |||
19313 | unsigned NumElts = VT.getVectorNumElements(); | |||
19314 | unsigned EltSizeInBits = EltVT.getScalarSizeInBits(); | |||
19315 | ||||
19316 | if (EltVT == MVT::i1) | |||
19317 | return InsertBitToMaskVector(Op, DAG, Subtarget); | |||
19318 | ||||
19319 | SDLoc dl(Op); | |||
19320 | SDValue N0 = Op.getOperand(0); | |||
19321 | SDValue N1 = Op.getOperand(1); | |||
19322 | SDValue N2 = Op.getOperand(2); | |||
19323 | auto *N2C = dyn_cast<ConstantSDNode>(N2); | |||
19324 | ||||
19325 | if (!N2C) { | |||
19326 | // Variable insertion indices, usually we're better off spilling to stack, | |||
19327 | // but AVX512 can use a variable compare+select by comparing against all | |||
19328 | // possible vector indices, and FP insertion has less gpr->simd traffic. | |||
19329 | if (!(Subtarget.hasBWI() || | |||
19330 | (Subtarget.hasAVX512() && EltSizeInBits >= 32) || | |||
19331 | (Subtarget.hasSSE41() && VT.isFloatingPoint()))) | |||
19332 | return SDValue(); | |||
19333 | ||||
19334 | MVT IdxSVT = MVT::getIntegerVT(EltSizeInBits); | |||
19335 | MVT IdxVT = MVT::getVectorVT(IdxSVT, NumElts); | |||
19336 | if (!isTypeLegal(IdxSVT) || !isTypeLegal(IdxVT)) | |||
19337 | return SDValue(); | |||
19338 | ||||
19339 | SDValue IdxExt = DAG.getZExtOrTrunc(N2, dl, IdxSVT); | |||
19340 | SDValue IdxSplat = DAG.getSplatBuildVector(IdxVT, dl, IdxExt); | |||
19341 | SDValue EltSplat = DAG.getSplatBuildVector(VT, dl, N1); | |||
19342 | ||||
19343 | SmallVector<SDValue, 16> RawIndices; | |||
19344 | for (unsigned I = 0; I != NumElts; ++I) | |||
19345 | RawIndices.push_back(DAG.getConstant(I, dl, IdxSVT)); | |||
19346 | SDValue Indices = DAG.getBuildVector(IdxVT, dl, RawIndices); | |||
19347 | ||||
19348 | // inselt N0, N1, N2 --> select (SplatN2 == {0,1,2...}) ? SplatN1 : N0. | |||
19349 | return DAG.getSelectCC(dl, IdxSplat, Indices, EltSplat, N0, | |||
19350 | ISD::CondCode::SETEQ); | |||
19351 | } | |||
19352 | ||||
19353 | if (N2C->getAPIntValue().uge(NumElts)) | |||
19354 | return SDValue(); | |||
19355 | uint64_t IdxVal = N2C->getZExtValue(); | |||
19356 | ||||
19357 | bool IsZeroElt = X86::isZeroNode(N1); | |||
19358 | bool IsAllOnesElt = VT.isInteger() && llvm::isAllOnesConstant(N1); | |||
19359 | ||||
19360 | if (IsZeroElt || IsAllOnesElt) { | |||
19361 | // Lower insertion of i8 -1 as an 'OR' blend. | |||
19362 | // We don't deal with i8 0 since it appears to be handled elsewhere. | |||
19363 | if (IsAllOnesElt && EltSizeInBits == 8 && !Subtarget.hasSSE41()) { | |||
19364 | SDValue ZeroCst = DAG.getConstant(0, dl, VT.getScalarType()); | |||
19365 | SDValue OnesCst = DAG.getAllOnesConstant(dl, VT.getScalarType()); | |||
19366 | SmallVector<SDValue, 8> CstVectorElts(NumElts, ZeroCst); | |||
19367 | CstVectorElts[IdxVal] = OnesCst; | |||
19368 | SDValue CstVector = DAG.getBuildVector(VT, dl, CstVectorElts); | |||
19369 | return DAG.getNode(ISD::OR, dl, VT, N0, CstVector); | |||
19370 | } | |||
19371 | // See if we can do this more efficiently with a blend shuffle with a | |||
19372 | // rematerializable vector. | |||
19373 | if (Subtarget.hasSSE41() && | |||
19374 | (EltSizeInBits >= 16 || (IsZeroElt && !VT.is128BitVector()))) { | |||
19375 | SmallVector<int, 8> BlendMask; | |||
19376 | for (unsigned i = 0; i != NumElts; ++i) | |||
19377 | BlendMask.push_back(i == IdxVal ? i + NumElts : i); | |||
19378 | SDValue CstVector = IsZeroElt ? getZeroVector(VT, Subtarget, DAG, dl) | |||
19379 | : getOnesVector(VT, DAG, dl); | |||
19380 | return DAG.getVectorShuffle(VT, dl, N0, CstVector, BlendMask); | |||
19381 | } | |||
19382 | } | |||
19383 | ||||
19384 | // If the vector is wider than 128 bits, extract the 128-bit subvector, insert | |||
19385 | // into that, and then insert the subvector back into the result. | |||
19386 | if (VT.is256BitVector() || VT.is512BitVector()) { | |||
19387 | // With a 256-bit vector, we can insert into the zero element efficiently | |||
19388 | // using a blend if we have AVX or AVX2 and the right data type. | |||
19389 | if (VT.is256BitVector() && IdxVal == 0) { | |||
19390 | // TODO: It is worthwhile to cast integer to floating point and back | |||
19391 | // and incur a domain crossing penalty if that's what we'll end up | |||
19392 | // doing anyway after extracting to a 128-bit vector. | |||
19393 | if ((Subtarget.hasAVX() && (EltVT == MVT::f64 || EltVT == MVT::f32)) || | |||
19394 | (Subtarget.hasAVX2() && EltVT == MVT::i32)) { | |||
19395 | SDValue N1Vec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, N1); | |||
19396 | return DAG.getNode(X86ISD::BLENDI, dl, VT, N0, N1Vec, | |||
19397 | DAG.getTargetConstant(1, dl, MVT::i8)); | |||
19398 | } | |||
19399 | } | |||
19400 | ||||
19401 | unsigned NumEltsIn128 = 128 / EltSizeInBits; | |||
19402 | assert(isPowerOf2_32(NumEltsIn128) &&(static_cast <bool> (isPowerOf2_32(NumEltsIn128) && "Vectors will always have power-of-two number of elements.") ? void (0) : __assert_fail ("isPowerOf2_32(NumEltsIn128) && \"Vectors will always have power-of-two number of elements.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 19403, __extension__ __PRETTY_FUNCTION__)) | |||
19403 | "Vectors will always have power-of-two number of elements.")(static_cast <bool> (isPowerOf2_32(NumEltsIn128) && "Vectors will always have power-of-two number of elements.") ? void (0) : __assert_fail ("isPowerOf2_32(NumEltsIn128) && \"Vectors will always have power-of-two number of elements.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 19403, __extension__ __PRETTY_FUNCTION__)); | |||
19404 | ||||
19405 | // If we are not inserting into the low 128-bit vector chunk, | |||
19406 | // then prefer the broadcast+blend sequence. | |||
19407 | // FIXME: relax the profitability check iff all N1 uses are insertions. | |||
19408 | if (!VT.is128BitVector() && IdxVal >= NumEltsIn128 && | |||
19409 | ((Subtarget.hasAVX2() && EltSizeInBits != 8) || | |||
19410 | (Subtarget.hasAVX() && (EltSizeInBits >= 32) && MayFoldLoad(N1)))) { | |||
19411 | SDValue N1SplatVec = DAG.getSplatBuildVector(VT, dl, N1); | |||
19412 | SmallVector<int, 8> BlendMask; | |||
19413 | for (unsigned i = 0; i != NumElts; ++i) | |||
19414 | BlendMask.push_back(i == IdxVal ? i + NumElts : i); | |||
19415 | return DAG.getVectorShuffle(VT, dl, N0, N1SplatVec, BlendMask); | |||
19416 | } | |||
19417 | ||||
19418 | // Get the desired 128-bit vector chunk. | |||
19419 | SDValue V = extract128BitVector(N0, IdxVal, DAG, dl); | |||
19420 | ||||
19421 | // Insert the element into the desired chunk. | |||
19422 | // Since NumEltsIn128 is a power of 2 we can use mask instead of modulo. | |||
19423 | unsigned IdxIn128 = IdxVal & (NumEltsIn128 - 1); | |||
19424 | ||||
19425 | V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1, | |||
19426 | DAG.getIntPtrConstant(IdxIn128, dl)); | |||
19427 | ||||
19428 | // Insert the changed part back into the bigger vector | |||
19429 | return insert128BitVector(N0, V, IdxVal, DAG, dl); | |||
19430 | } | |||
19431 | assert(VT.is128BitVector() && "Only 128-bit vector types should be left!")(static_cast <bool> (VT.is128BitVector() && "Only 128-bit vector types should be left!" ) ? void (0) : __assert_fail ("VT.is128BitVector() && \"Only 128-bit vector types should be left!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 19431, __extension__ __PRETTY_FUNCTION__)); | |||
19432 | ||||
19433 | // This will be just movw/movd/movq/movsh/movss/movsd. | |||
19434 | if (IdxVal == 0 && ISD::isBuildVectorAllZeros(N0.getNode())) { | |||
19435 | if (EltVT == MVT::i32 || EltVT == MVT::f32 || EltVT == MVT::f64 || | |||
19436 | EltVT == MVT::f16 || EltVT == MVT::i64) { | |||
19437 | N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, N1); | |||
19438 | return getShuffleVectorZeroOrUndef(N1, 0, true, Subtarget, DAG); | |||
19439 | } | |||
19440 | ||||
19441 | // We can't directly insert an i8 or i16 into a vector, so zero extend | |||
19442 | // it to i32 first. | |||
19443 | if (EltVT == MVT::i16 || EltVT == MVT::i8) { | |||
19444 | N1 = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, N1); | |||
19445 | MVT ShufVT = MVT::getVectorVT(MVT::i32, VT.getSizeInBits() / 32); | |||
19446 | N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, ShufVT, N1); | |||
19447 | N1 = getShuffleVectorZeroOrUndef(N1, 0, true, Subtarget, DAG); | |||
19448 | return DAG.getBitcast(VT, N1); | |||
19449 | } | |||
19450 | } | |||
19451 | ||||
19452 | // Transform it so it match pinsr{b,w} which expects a GR32 as its second | |||
19453 | // argument. SSE41 required for pinsrb. | |||
19454 | if (VT == MVT::v8i16 || (VT == MVT::v16i8 && Subtarget.hasSSE41())) { | |||
19455 | unsigned Opc; | |||
19456 | if (VT == MVT::v8i16) { | |||
19457 | assert(Subtarget.hasSSE2() && "SSE2 required for PINSRW")(static_cast <bool> (Subtarget.hasSSE2() && "SSE2 required for PINSRW" ) ? void (0) : __assert_fail ("Subtarget.hasSSE2() && \"SSE2 required for PINSRW\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 19457, __extension__ __PRETTY_FUNCTION__)); | |||
19458 | Opc = X86ISD::PINSRW; | |||
19459 | } else { | |||
19460 | assert(VT == MVT::v16i8 && "PINSRB requires v16i8 vector")(static_cast <bool> (VT == MVT::v16i8 && "PINSRB requires v16i8 vector" ) ? void (0) : __assert_fail ("VT == MVT::v16i8 && \"PINSRB requires v16i8 vector\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 19460, __extension__ __PRETTY_FUNCTION__)); | |||
19461 | assert(Subtarget.hasSSE41() && "SSE41 required for PINSRB")(static_cast <bool> (Subtarget.hasSSE41() && "SSE41 required for PINSRB" ) ? void (0) : __assert_fail ("Subtarget.hasSSE41() && \"SSE41 required for PINSRB\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 19461, __extension__ __PRETTY_FUNCTION__)); | |||
19462 | Opc = X86ISD::PINSRB; | |||
19463 | } | |||
19464 | ||||
19465 | assert(N1.getValueType() != MVT::i32 && "Unexpected VT")(static_cast <bool> (N1.getValueType() != MVT::i32 && "Unexpected VT") ? void (0) : __assert_fail ("N1.getValueType() != MVT::i32 && \"Unexpected VT\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 19465, __extension__ __PRETTY_FUNCTION__)); | |||
19466 | N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1); | |||
19467 | N2 = DAG.getTargetConstant(IdxVal, dl, MVT::i8); | |||
19468 | return DAG.getNode(Opc, dl, VT, N0, N1, N2); | |||
19469 | } | |||
19470 | ||||
19471 | if (Subtarget.hasSSE41()) { | |||
19472 | if (EltVT == MVT::f32) { | |||
19473 | // Bits [7:6] of the constant are the source select. This will always be | |||
19474 | // zero here. The DAG Combiner may combine an extract_elt index into | |||
19475 | // these bits. For example (insert (extract, 3), 2) could be matched by | |||
19476 | // putting the '3' into bits [7:6] of X86ISD::INSERTPS. | |||
19477 | // Bits [5:4] of the constant are the destination select. This is the | |||
19478 | // value of the incoming immediate. | |||
19479 | // Bits [3:0] of the constant are the zero mask. The DAG Combiner may | |||
19480 | // combine either bitwise AND or insert of float 0.0 to set these bits. | |||
19481 | ||||
19482 | bool MinSize = DAG.getMachineFunction().getFunction().hasMinSize(); | |||
19483 | if (IdxVal == 0 && (!MinSize || !MayFoldLoad(N1))) { | |||
19484 | // If this is an insertion of 32-bits into the low 32-bits of | |||
19485 | // a vector, we prefer to generate a blend with immediate rather | |||
19486 | // than an insertps. Blends are simpler operations in hardware and so | |||
19487 | // will always have equal or better performance than insertps. | |||
19488 | // But if optimizing for size and there's a load folding opportunity, | |||
19489 | // generate insertps because blendps does not have a 32-bit memory | |||
19490 | // operand form. | |||
19491 | N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1); | |||
19492 | return DAG.getNode(X86ISD::BLENDI, dl, VT, N0, N1, | |||
19493 | DAG.getTargetConstant(1, dl, MVT::i8)); | |||
19494 | } | |||
19495 | // Create this as a scalar to vector.. | |||
19496 | N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1); | |||
19497 | return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, | |||
19498 | DAG.getTargetConstant(IdxVal << 4, dl, MVT::i8)); | |||
19499 | } | |||
19500 | ||||
19501 | // PINSR* works with constant index. | |||
19502 | if (EltVT == MVT::i32 || EltVT == MVT::i64) | |||
19503 | return Op; | |||
19504 | } | |||
19505 | ||||
19506 | return SDValue(); | |||
19507 | } | |||
19508 | ||||
19509 | static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, const X86Subtarget &Subtarget, | |||
19510 | SelectionDAG &DAG) { | |||
19511 | SDLoc dl(Op); | |||
19512 | MVT OpVT = Op.getSimpleValueType(); | |||
19513 | ||||
19514 | // It's always cheaper to replace a xor+movd with xorps and simplifies further | |||
19515 | // combines. | |||
19516 | if (X86::isZeroNode(Op.getOperand(0))) | |||
19517 | return getZeroVector(OpVT, Subtarget, DAG, dl); | |||
19518 | ||||
19519 | // If this is a 256-bit vector result, first insert into a 128-bit | |||
19520 | // vector and then insert into the 256-bit vector. | |||
19521 | if (!OpVT.is128BitVector()) { | |||
19522 | // Insert into a 128-bit vector. | |||
19523 | unsigned SizeFactor = OpVT.getSizeInBits() / 128; | |||
19524 | MVT VT128 = MVT::getVectorVT(OpVT.getVectorElementType(), | |||
19525 | OpVT.getVectorNumElements() / SizeFactor); | |||
19526 | ||||
19527 | Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0)); | |||
19528 | ||||
19529 | // Insert the 128-bit vector. | |||
19530 | return insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl); | |||
19531 | } | |||
19532 | assert(OpVT.is128BitVector() && OpVT.isInteger() && OpVT != MVT::v2i64 &&(static_cast <bool> (OpVT.is128BitVector() && OpVT .isInteger() && OpVT != MVT::v2i64 && "Expected an SSE type!" ) ? void (0) : __assert_fail ("OpVT.is128BitVector() && OpVT.isInteger() && OpVT != MVT::v2i64 && \"Expected an SSE type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 19533, __extension__ __PRETTY_FUNCTION__)) | |||
19533 | "Expected an SSE type!")(static_cast <bool> (OpVT.is128BitVector() && OpVT .isInteger() && OpVT != MVT::v2i64 && "Expected an SSE type!" ) ? void (0) : __assert_fail ("OpVT.is128BitVector() && OpVT.isInteger() && OpVT != MVT::v2i64 && \"Expected an SSE type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 19533, __extension__ __PRETTY_FUNCTION__)); | |||
19534 | ||||
19535 | // Pass through a v4i32 or V8i16 SCALAR_TO_VECTOR as that's what we use in | |||
19536 | // tblgen. | |||
19537 | if (OpVT == MVT::v4i32 || (OpVT == MVT::v8i16 && Subtarget.hasFP16())) | |||
19538 | return Op; | |||
19539 | ||||
19540 | SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0)); | |||
19541 | return DAG.getBitcast( | |||
19542 | OpVT, DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, AnyExt)); | |||
19543 | } | |||
19544 | ||||
19545 | // Lower a node with an INSERT_SUBVECTOR opcode. This may result in a | |||
19546 | // simple superregister reference or explicit instructions to insert | |||
19547 | // the upper bits of a vector. | |||
19548 | static SDValue LowerINSERT_SUBVECTOR(SDValue Op, const X86Subtarget &Subtarget, | |||
19549 | SelectionDAG &DAG) { | |||
19550 | assert(Op.getSimpleValueType().getVectorElementType() == MVT::i1)(static_cast <bool> (Op.getSimpleValueType().getVectorElementType () == MVT::i1) ? void (0) : __assert_fail ("Op.getSimpleValueType().getVectorElementType() == MVT::i1" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 19550, __extension__ __PRETTY_FUNCTION__)); | |||
19551 | ||||
19552 | return insert1BitVector(Op, DAG, Subtarget); | |||
19553 | } | |||
19554 | ||||
19555 | static SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, const X86Subtarget &Subtarget, | |||
19556 | SelectionDAG &DAG) { | |||
19557 | assert(Op.getSimpleValueType().getVectorElementType() == MVT::i1 &&(static_cast <bool> (Op.getSimpleValueType().getVectorElementType () == MVT::i1 && "Only vXi1 extract_subvectors need custom lowering" ) ? void (0) : __assert_fail ("Op.getSimpleValueType().getVectorElementType() == MVT::i1 && \"Only vXi1 extract_subvectors need custom lowering\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 19558, __extension__ __PRETTY_FUNCTION__)) | |||
19558 | "Only vXi1 extract_subvectors need custom lowering")(static_cast <bool> (Op.getSimpleValueType().getVectorElementType () == MVT::i1 && "Only vXi1 extract_subvectors need custom lowering" ) ? void (0) : __assert_fail ("Op.getSimpleValueType().getVectorElementType() == MVT::i1 && \"Only vXi1 extract_subvectors need custom lowering\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 19558, __extension__ __PRETTY_FUNCTION__)); | |||
19559 | ||||
19560 | SDLoc dl(Op); | |||
19561 | SDValue Vec = Op.getOperand(0); | |||
19562 | uint64_t IdxVal = Op.getConstantOperandVal(1); | |||
19563 | ||||
19564 | if (IdxVal == 0) // the operation is legal | |||
19565 | return Op; | |||
19566 | ||||
19567 | MVT VecVT = Vec.getSimpleValueType(); | |||
19568 | unsigned NumElems = VecVT.getVectorNumElements(); | |||
19569 | ||||
19570 | // Extend to natively supported kshift. | |||
19571 | MVT WideVecVT = VecVT; | |||
19572 | if ((!Subtarget.hasDQI() && NumElems == 8) || NumElems < 8) { | |||
19573 | WideVecVT = Subtarget.hasDQI() ? MVT::v8i1 : MVT::v16i1; | |||
19574 | Vec = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, WideVecVT, | |||
19575 | DAG.getUNDEF(WideVecVT), Vec, | |||
19576 | DAG.getIntPtrConstant(0, dl)); | |||
19577 | } | |||
19578 | ||||
19579 | // Shift to the LSB. | |||
19580 | Vec = DAG.getNode(X86ISD::KSHIFTR, dl, WideVecVT, Vec, | |||
19581 | DAG.getTargetConstant(IdxVal, dl, MVT::i8)); | |||
19582 | ||||
19583 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, Op.getValueType(), Vec, | |||
19584 | DAG.getIntPtrConstant(0, dl)); | |||
19585 | } | |||
19586 | ||||
19587 | // Returns the appropriate wrapper opcode for a global reference. | |||
19588 | unsigned X86TargetLowering::getGlobalWrapperKind( | |||
19589 | const GlobalValue *GV, const unsigned char OpFlags) const { | |||
19590 | // References to absolute symbols are never PC-relative. | |||
19591 | if (GV && GV->isAbsoluteSymbolRef()) | |||
19592 | return X86ISD::Wrapper; | |||
19593 | ||||
19594 | CodeModel::Model M = getTargetMachine().getCodeModel(); | |||
19595 | if (Subtarget.isPICStyleRIPRel() && | |||
19596 | (M == CodeModel::Small || M == CodeModel::Kernel)) | |||
19597 | return X86ISD::WrapperRIP; | |||
19598 | ||||
19599 | // GOTPCREL references must always use RIP. | |||
19600 | if (OpFlags == X86II::MO_GOTPCREL) | |||
19601 | return X86ISD::WrapperRIP; | |||
19602 | ||||
19603 | return X86ISD::Wrapper; | |||
19604 | } | |||
19605 | ||||
19606 | // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as | |||
19607 | // their target counterpart wrapped in the X86ISD::Wrapper node. Suppose N is | |||
19608 | // one of the above mentioned nodes. It has to be wrapped because otherwise | |||
19609 | // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only | |||
19610 | // be used to form addressing mode. These wrapped nodes will be selected | |||
19611 | // into MOV32ri. | |||
19612 | SDValue | |||
19613 | X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const { | |||
19614 | ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op); | |||
19615 | ||||
19616 | // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the | |||
19617 | // global base reg. | |||
19618 | unsigned char OpFlag = Subtarget.classifyLocalReference(nullptr); | |||
19619 | ||||
19620 | auto PtrVT = getPointerTy(DAG.getDataLayout()); | |||
19621 | SDValue Result = DAG.getTargetConstantPool( | |||
19622 | CP->getConstVal(), PtrVT, CP->getAlign(), CP->getOffset(), OpFlag); | |||
19623 | SDLoc DL(CP); | |||
19624 | Result = DAG.getNode(getGlobalWrapperKind(), DL, PtrVT, Result); | |||
19625 | // With PIC, the address is actually $g + Offset. | |||
19626 | if (OpFlag) { | |||
19627 | Result = | |||
19628 | DAG.getNode(ISD::ADD, DL, PtrVT, | |||
19629 | DAG.getNode(X86ISD::GlobalBaseReg, SDLoc(), PtrVT), Result); | |||
19630 | } | |||
19631 | ||||
19632 | return Result; | |||
19633 | } | |||
19634 | ||||
19635 | SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const { | |||
19636 | JumpTableSDNode *JT = cast<JumpTableSDNode>(Op); | |||
19637 | ||||
19638 | // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the | |||
19639 | // global base reg. | |||
19640 | unsigned char OpFlag = Subtarget.classifyLocalReference(nullptr); | |||
19641 | ||||
19642 | auto PtrVT = getPointerTy(DAG.getDataLayout()); | |||
19643 | SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag); | |||
19644 | SDLoc DL(JT); | |||
19645 | Result = DAG.getNode(getGlobalWrapperKind(), DL, PtrVT, Result); | |||
19646 | ||||
19647 | // With PIC, the address is actually $g + Offset. | |||
19648 | if (OpFlag) | |||
19649 | Result = | |||
19650 | DAG.getNode(ISD::ADD, DL, PtrVT, | |||
19651 | DAG.getNode(X86ISD::GlobalBaseReg, SDLoc(), PtrVT), Result); | |||
19652 | ||||
19653 | return Result; | |||
19654 | } | |||
19655 | ||||
19656 | SDValue X86TargetLowering::LowerExternalSymbol(SDValue Op, | |||
19657 | SelectionDAG &DAG) const { | |||
19658 | return LowerGlobalOrExternal(Op, DAG, /*ForCall=*/false); | |||
19659 | } | |||
19660 | ||||
19661 | SDValue | |||
19662 | X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const { | |||
19663 | // Create the TargetBlockAddressAddress node. | |||
19664 | unsigned char OpFlags = | |||
19665 | Subtarget.classifyBlockAddressReference(); | |||
19666 | const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress(); | |||
19667 | int64_t Offset = cast<BlockAddressSDNode>(Op)->getOffset(); | |||
19668 | SDLoc dl(Op); | |||
19669 | auto PtrVT = getPointerTy(DAG.getDataLayout()); | |||
19670 | SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset, OpFlags); | |||
19671 | Result = DAG.getNode(getGlobalWrapperKind(), dl, PtrVT, Result); | |||
19672 | ||||
19673 | // With PIC, the address is actually $g + Offset. | |||
19674 | if (isGlobalRelativeToPICBase(OpFlags)) { | |||
19675 | Result = DAG.getNode(ISD::ADD, dl, PtrVT, | |||
19676 | DAG.getNode(X86ISD::GlobalBaseReg, dl, PtrVT), Result); | |||
19677 | } | |||
19678 | ||||
19679 | return Result; | |||
19680 | } | |||
19681 | ||||
19682 | /// Creates target global address or external symbol nodes for calls or | |||
19683 | /// other uses. | |||
19684 | SDValue X86TargetLowering::LowerGlobalOrExternal(SDValue Op, SelectionDAG &DAG, | |||
19685 | bool ForCall) const { | |||
19686 | // Unpack the global address or external symbol. | |||
19687 | const SDLoc &dl = SDLoc(Op); | |||
19688 | const GlobalValue *GV = nullptr; | |||
19689 | int64_t Offset = 0; | |||
19690 | const char *ExternalSym = nullptr; | |||
19691 | if (const auto *G = dyn_cast<GlobalAddressSDNode>(Op)) { | |||
19692 | GV = G->getGlobal(); | |||
19693 | Offset = G->getOffset(); | |||
19694 | } else { | |||
19695 | const auto *ES = cast<ExternalSymbolSDNode>(Op); | |||
19696 | ExternalSym = ES->getSymbol(); | |||
19697 | } | |||
19698 | ||||
19699 | // Calculate some flags for address lowering. | |||
19700 | const Module &Mod = *DAG.getMachineFunction().getFunction().getParent(); | |||
19701 | unsigned char OpFlags; | |||
19702 | if (ForCall) | |||
19703 | OpFlags = Subtarget.classifyGlobalFunctionReference(GV, Mod); | |||
19704 | else | |||
19705 | OpFlags = Subtarget.classifyGlobalReference(GV, Mod); | |||
19706 | bool HasPICReg = isGlobalRelativeToPICBase(OpFlags); | |||
19707 | bool NeedsLoad = isGlobalStubReference(OpFlags); | |||
19708 | ||||
19709 | CodeModel::Model M = DAG.getTarget().getCodeModel(); | |||
19710 | auto PtrVT = getPointerTy(DAG.getDataLayout()); | |||
19711 | SDValue Result; | |||
19712 | ||||
19713 | if (GV) { | |||
19714 | // Create a target global address if this is a global. If possible, fold the | |||
19715 | // offset into the global address reference. Otherwise, ADD it on later. | |||
19716 | // Suppress the folding if Offset is negative: movl foo-1, %eax is not | |||
19717 | // allowed because if the address of foo is 0, the ELF R_X86_64_32 | |||
19718 | // relocation will compute to a negative value, which is invalid. | |||
19719 | int64_t GlobalOffset = 0; | |||
19720 | if (OpFlags == X86II::MO_NO_FLAG && Offset >= 0 && | |||
19721 | X86::isOffsetSuitableForCodeModel(Offset, M, true)) { | |||
19722 | std::swap(GlobalOffset, Offset); | |||
19723 | } | |||
19724 | Result = DAG.getTargetGlobalAddress(GV, dl, PtrVT, GlobalOffset, OpFlags); | |||
19725 | } else { | |||
19726 | // If this is not a global address, this must be an external symbol. | |||
19727 | Result = DAG.getTargetExternalSymbol(ExternalSym, PtrVT, OpFlags); | |||
19728 | } | |||
19729 | ||||
19730 | // If this is a direct call, avoid the wrapper if we don't need to do any | |||
19731 | // loads or adds. This allows SDAG ISel to match direct calls. | |||
19732 | if (ForCall && !NeedsLoad && !HasPICReg && Offset == 0) | |||
19733 | return Result; | |||
19734 | ||||
19735 | Result = DAG.getNode(getGlobalWrapperKind(GV, OpFlags), dl, PtrVT, Result); | |||
19736 | ||||
19737 | // With PIC, the address is actually $g + Offset. | |||
19738 | if (HasPICReg) { | |||
19739 | Result = DAG.getNode(ISD::ADD, dl, PtrVT, | |||
19740 | DAG.getNode(X86ISD::GlobalBaseReg, dl, PtrVT), Result); | |||
19741 | } | |||
19742 | ||||
19743 | // For globals that require a load from a stub to get the address, emit the | |||
19744 | // load. | |||
19745 | if (NeedsLoad) | |||
19746 | Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result, | |||
19747 | MachinePointerInfo::getGOT(DAG.getMachineFunction())); | |||
19748 | ||||
19749 | // If there was a non-zero offset that we didn't fold, create an explicit | |||
19750 | // addition for it. | |||
19751 | if (Offset != 0) | |||
19752 | Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result, | |||
19753 | DAG.getConstant(Offset, dl, PtrVT)); | |||
19754 | ||||
19755 | return Result; | |||
19756 | } | |||
19757 | ||||
19758 | SDValue | |||
19759 | X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const { | |||
19760 | return LowerGlobalOrExternal(Op, DAG, /*ForCall=*/false); | |||
19761 | } | |||
19762 | ||||
19763 | static SDValue | |||
19764 | GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA, | |||
19765 | SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg, | |||
19766 | unsigned char OperandFlags, bool LocalDynamic = false) { | |||
19767 | MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); | |||
19768 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); | |||
19769 | SDLoc dl(GA); | |||
19770 | SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, | |||
19771 | GA->getValueType(0), | |||
19772 | GA->getOffset(), | |||
19773 | OperandFlags); | |||
19774 | ||||
19775 | X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR | |||
19776 | : X86ISD::TLSADDR; | |||
19777 | ||||
19778 | if (InFlag) { | |||
19779 | SDValue Ops[] = { Chain, TGA, *InFlag }; | |||
19780 | Chain = DAG.getNode(CallType, dl, NodeTys, Ops); | |||
19781 | } else { | |||
19782 | SDValue Ops[] = { Chain, TGA }; | |||
19783 | Chain = DAG.getNode(CallType, dl, NodeTys, Ops); | |||
19784 | } | |||
19785 | ||||
19786 | // TLSADDR will be codegen'ed as call. Inform MFI that function has calls. | |||
19787 | MFI.setAdjustsStack(true); | |||
19788 | MFI.setHasCalls(true); | |||
19789 | ||||
19790 | SDValue Flag = Chain.getValue(1); | |||
19791 | return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag); | |||
19792 | } | |||
19793 | ||||
19794 | // Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit | |||
19795 | static SDValue | |||
19796 | LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG, | |||
19797 | const EVT PtrVT) { | |||
19798 | SDValue InFlag; | |||
19799 | SDLoc dl(GA); // ? function entry point might be better | |||
19800 | SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX, | |||
19801 | DAG.getNode(X86ISD::GlobalBaseReg, | |||
19802 | SDLoc(), PtrVT), InFlag); | |||
19803 | InFlag = Chain.getValue(1); | |||
19804 | ||||
19805 | return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD); | |||
19806 | } | |||
19807 | ||||
19808 | // Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit LP64 | |||
19809 | static SDValue | |||
19810 | LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG, | |||
19811 | const EVT PtrVT) { | |||
19812 | return GetTLSADDR(DAG, DAG.getEntryNode(), GA, nullptr, PtrVT, | |||
19813 | X86::RAX, X86II::MO_TLSGD); | |||
19814 | } | |||
19815 | ||||
19816 | // Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit ILP32 | |||
19817 | static SDValue | |||
19818 | LowerToTLSGeneralDynamicModelX32(GlobalAddressSDNode *GA, SelectionDAG &DAG, | |||
19819 | const EVT PtrVT) { | |||
19820 | return GetTLSADDR(DAG, DAG.getEntryNode(), GA, nullptr, PtrVT, | |||
19821 | X86::EAX, X86II::MO_TLSGD); | |||
19822 | } | |||
19823 | ||||
19824 | static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA, | |||
19825 | SelectionDAG &DAG, const EVT PtrVT, | |||
19826 | bool Is64Bit, bool Is64BitLP64) { | |||
19827 | SDLoc dl(GA); | |||
19828 | ||||
19829 | // Get the start address of the TLS block for this module. | |||
19830 | X86MachineFunctionInfo *MFI = DAG.getMachineFunction() | |||
19831 | .getInfo<X86MachineFunctionInfo>(); | |||
19832 | MFI->incNumLocalDynamicTLSAccesses(); | |||
19833 | ||||
19834 | SDValue Base; | |||
19835 | if (Is64Bit) { | |||
19836 | unsigned ReturnReg = Is64BitLP64 ? X86::RAX : X86::EAX; | |||
19837 | Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, nullptr, PtrVT, ReturnReg, | |||
19838 | X86II::MO_TLSLD, /*LocalDynamic=*/true); | |||
19839 | } else { | |||
19840 | SDValue InFlag; | |||
19841 | SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX, | |||
19842 | DAG.getNode(X86ISD::GlobalBaseReg, SDLoc(), PtrVT), InFlag); | |||
19843 | InFlag = Chain.getValue(1); | |||
19844 | Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, | |||
19845 | X86II::MO_TLSLDM, /*LocalDynamic=*/true); | |||
19846 | } | |||
19847 | ||||
19848 | // Note: the CleanupLocalDynamicTLSPass will remove redundant computations | |||
19849 | // of Base. | |||
19850 | ||||
19851 | // Build x@dtpoff. | |||
19852 | unsigned char OperandFlags = X86II::MO_DTPOFF; | |||
19853 | unsigned WrapperKind = X86ISD::Wrapper; | |||
19854 | SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, | |||
19855 | GA->getValueType(0), | |||
19856 | GA->getOffset(), OperandFlags); | |||
19857 | SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA); | |||
19858 | ||||
19859 | // Add x@dtpoff with the base. | |||
19860 | return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base); | |||
19861 | } | |||
19862 | ||||
19863 | // Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model. | |||
19864 | static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG, | |||
19865 | const EVT PtrVT, TLSModel::Model model, | |||
19866 | bool is64Bit, bool isPIC) { | |||
19867 | SDLoc dl(GA); | |||
19868 | ||||
19869 | // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit). | |||
19870 | Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(), | |||
19871 | is64Bit ? 257 : 256)); | |||
19872 | ||||
19873 | SDValue ThreadPointer = | |||
19874 | DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), DAG.getIntPtrConstant(0, dl), | |||
19875 | MachinePointerInfo(Ptr)); | |||
19876 | ||||
19877 | unsigned char OperandFlags = 0; | |||
19878 | // Most TLS accesses are not RIP relative, even on x86-64. One exception is | |||
19879 | // initialexec. | |||
19880 | unsigned WrapperKind = X86ISD::Wrapper; | |||
19881 | if (model == TLSModel::LocalExec) { | |||
19882 | OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF; | |||
19883 | } else if (model == TLSModel::InitialExec) { | |||
19884 | if (is64Bit) { | |||
19885 | OperandFlags = X86II::MO_GOTTPOFF; | |||
19886 | WrapperKind = X86ISD::WrapperRIP; | |||
19887 | } else { | |||
19888 | OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF; | |||
19889 | } | |||
19890 | } else { | |||
19891 | llvm_unreachable("Unexpected model")::llvm::llvm_unreachable_internal("Unexpected model", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 19891); | |||
19892 | } | |||
19893 | ||||
19894 | // emit "addl x@ntpoff,%eax" (local exec) | |||
19895 | // or "addl x@indntpoff,%eax" (initial exec) | |||
19896 | // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic) | |||
19897 | SDValue TGA = | |||
19898 | DAG.getTargetGlobalAddress(GA->getGlobal(), dl, GA->getValueType(0), | |||
19899 | GA->getOffset(), OperandFlags); | |||
19900 | SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA); | |||
19901 | ||||
19902 | if (model == TLSModel::InitialExec) { | |||
19903 | if (isPIC && !is64Bit) { | |||
19904 | Offset = DAG.getNode(ISD::ADD, dl, PtrVT, | |||
19905 | DAG.getNode(X86ISD::GlobalBaseReg, SDLoc(), PtrVT), | |||
19906 | Offset); | |||
19907 | } | |||
19908 | ||||
19909 | Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset, | |||
19910 | MachinePointerInfo::getGOT(DAG.getMachineFunction())); | |||
19911 | } | |||
19912 | ||||
19913 | // The address of the thread local variable is the add of the thread | |||
19914 | // pointer with the offset of the variable. | |||
19915 | return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset); | |||
19916 | } | |||
19917 | ||||
19918 | SDValue | |||
19919 | X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const { | |||
19920 | ||||
19921 | GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op); | |||
19922 | ||||
19923 | if (DAG.getTarget().useEmulatedTLS()) | |||
19924 | return LowerToTLSEmulatedModel(GA, DAG); | |||
19925 | ||||
19926 | const GlobalValue *GV = GA->getGlobal(); | |||
19927 | auto PtrVT = getPointerTy(DAG.getDataLayout()); | |||
19928 | bool PositionIndependent = isPositionIndependent(); | |||
19929 | ||||
19930 | if (Subtarget.isTargetELF()) { | |||
19931 | TLSModel::Model model = DAG.getTarget().getTLSModel(GV); | |||
19932 | switch (model) { | |||
19933 | case TLSModel::GeneralDynamic: | |||
19934 | if (Subtarget.is64Bit()) { | |||
19935 | if (Subtarget.isTarget64BitLP64()) | |||
19936 | return LowerToTLSGeneralDynamicModel64(GA, DAG, PtrVT); | |||
19937 | return LowerToTLSGeneralDynamicModelX32(GA, DAG, PtrVT); | |||
19938 | } | |||
19939 | return LowerToTLSGeneralDynamicModel32(GA, DAG, PtrVT); | |||
19940 | case TLSModel::LocalDynamic: | |||
19941 | return LowerToTLSLocalDynamicModel(GA, DAG, PtrVT, Subtarget.is64Bit(), | |||
19942 | Subtarget.isTarget64BitLP64()); | |||
19943 | case TLSModel::InitialExec: | |||
19944 | case TLSModel::LocalExec: | |||
19945 | return LowerToTLSExecModel(GA, DAG, PtrVT, model, Subtarget.is64Bit(), | |||
19946 | PositionIndependent); | |||
19947 | } | |||
19948 | llvm_unreachable("Unknown TLS model.")::llvm::llvm_unreachable_internal("Unknown TLS model.", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 19948); | |||
19949 | } | |||
19950 | ||||
19951 | if (Subtarget.isTargetDarwin()) { | |||
19952 | // Darwin only has one model of TLS. Lower to that. | |||
19953 | unsigned char OpFlag = 0; | |||
19954 | unsigned WrapperKind = Subtarget.isPICStyleRIPRel() ? | |||
19955 | X86ISD::WrapperRIP : X86ISD::Wrapper; | |||
19956 | ||||
19957 | // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the | |||
19958 | // global base reg. | |||
19959 | bool PIC32 = PositionIndependent && !Subtarget.is64Bit(); | |||
19960 | if (PIC32) | |||
19961 | OpFlag = X86II::MO_TLVP_PIC_BASE; | |||
19962 | else | |||
19963 | OpFlag = X86II::MO_TLVP; | |||
19964 | SDLoc DL(Op); | |||
19965 | SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL, | |||
19966 | GA->getValueType(0), | |||
19967 | GA->getOffset(), OpFlag); | |||
19968 | SDValue Offset = DAG.getNode(WrapperKind, DL, PtrVT, Result); | |||
19969 | ||||
19970 | // With PIC32, the address is actually $g + Offset. | |||
19971 | if (PIC32) | |||
19972 | Offset = DAG.getNode(ISD::ADD, DL, PtrVT, | |||
19973 | DAG.getNode(X86ISD::GlobalBaseReg, SDLoc(), PtrVT), | |||
19974 | Offset); | |||
19975 | ||||
19976 | // Lowering the machine isd will make sure everything is in the right | |||
19977 | // location. | |||
19978 | SDValue Chain = DAG.getEntryNode(); | |||
19979 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); | |||
19980 | Chain = DAG.getCALLSEQ_START(Chain, 0, 0, DL); | |||
19981 | SDValue Args[] = { Chain, Offset }; | |||
19982 | Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args); | |||
19983 | Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, DL, true), | |||
19984 | DAG.getIntPtrConstant(0, DL, true), | |||
19985 | Chain.getValue(1), DL); | |||
19986 | ||||
19987 | // TLSCALL will be codegen'ed as call. Inform MFI that function has calls. | |||
19988 | MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); | |||
19989 | MFI.setAdjustsStack(true); | |||
19990 | ||||
19991 | // And our return value (tls address) is in the standard call return value | |||
19992 | // location. | |||
19993 | unsigned Reg = Subtarget.is64Bit() ? X86::RAX : X86::EAX; | |||
19994 | return DAG.getCopyFromReg(Chain, DL, Reg, PtrVT, Chain.getValue(1)); | |||
19995 | } | |||
19996 | ||||
19997 | if (Subtarget.isOSWindows()) { | |||
19998 | // Just use the implicit TLS architecture | |||
19999 | // Need to generate something similar to: | |||
20000 | // mov rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage | |||
20001 | // ; from TEB | |||
20002 | // mov ecx, dword [rel _tls_index]: Load index (from C runtime) | |||
20003 | // mov rcx, qword [rdx+rcx*8] | |||
20004 | // mov eax, .tls$:tlsvar | |||
20005 | // [rax+rcx] contains the address | |||
20006 | // Windows 64bit: gs:0x58 | |||
20007 | // Windows 32bit: fs:__tls_array | |||
20008 | ||||
20009 | SDLoc dl(GA); | |||
20010 | SDValue Chain = DAG.getEntryNode(); | |||
20011 | ||||
20012 | // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or | |||
20013 | // %gs:0x58 (64-bit). On MinGW, __tls_array is not available, so directly | |||
20014 | // use its literal value of 0x2C. | |||
20015 | Value *Ptr = Constant::getNullValue(Subtarget.is64Bit() | |||
20016 | ? Type::getInt8PtrTy(*DAG.getContext(), | |||
20017 | 256) | |||
20018 | : Type::getInt32PtrTy(*DAG.getContext(), | |||
20019 | 257)); | |||
20020 | ||||
20021 | SDValue TlsArray = Subtarget.is64Bit() | |||
20022 | ? DAG.getIntPtrConstant(0x58, dl) | |||
20023 | : (Subtarget.isTargetWindowsGNU() | |||
20024 | ? DAG.getIntPtrConstant(0x2C, dl) | |||
20025 | : DAG.getExternalSymbol("_tls_array", PtrVT)); | |||
20026 | ||||
20027 | SDValue ThreadPointer = | |||
20028 | DAG.getLoad(PtrVT, dl, Chain, TlsArray, MachinePointerInfo(Ptr)); | |||
20029 | ||||
20030 | SDValue res; | |||
20031 | if (GV->getThreadLocalMode() == GlobalVariable::LocalExecTLSModel) { | |||
20032 | res = ThreadPointer; | |||
20033 | } else { | |||
20034 | // Load the _tls_index variable | |||
20035 | SDValue IDX = DAG.getExternalSymbol("_tls_index", PtrVT); | |||
20036 | if (Subtarget.is64Bit()) | |||
20037 | IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, PtrVT, Chain, IDX, | |||
20038 | MachinePointerInfo(), MVT::i32); | |||
20039 | else | |||
20040 | IDX = DAG.getLoad(PtrVT, dl, Chain, IDX, MachinePointerInfo()); | |||
20041 | ||||
20042 | const DataLayout &DL = DAG.getDataLayout(); | |||
20043 | SDValue Scale = | |||
20044 | DAG.getConstant(Log2_64_Ceil(DL.getPointerSize()), dl, MVT::i8); | |||
20045 | IDX = DAG.getNode(ISD::SHL, dl, PtrVT, IDX, Scale); | |||
20046 | ||||
20047 | res = DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, IDX); | |||
20048 | } | |||
20049 | ||||
20050 | res = DAG.getLoad(PtrVT, dl, Chain, res, MachinePointerInfo()); | |||
20051 | ||||
20052 | // Get the offset of start of .tls section | |||
20053 | SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl, | |||
20054 | GA->getValueType(0), | |||
20055 | GA->getOffset(), X86II::MO_SECREL); | |||
20056 | SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, PtrVT, TGA); | |||
20057 | ||||
20058 | // The address of the thread local variable is the add of the thread | |||
20059 | // pointer with the offset of the variable. | |||
20060 | return DAG.getNode(ISD::ADD, dl, PtrVT, res, Offset); | |||
20061 | } | |||
20062 | ||||
20063 | llvm_unreachable("TLS not implemented for this target.")::llvm::llvm_unreachable_internal("TLS not implemented for this target." , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20063); | |||
20064 | } | |||
20065 | ||||
20066 | /// Lower SRA_PARTS and friends, which return two i32 values | |||
20067 | /// and take a 2 x i32 value to shift plus a shift amount. | |||
20068 | /// TODO: Can this be moved to general expansion code? | |||
20069 | static SDValue LowerShiftParts(SDValue Op, SelectionDAG &DAG) { | |||
20070 | SDValue Lo, Hi; | |||
20071 | DAG.getTargetLoweringInfo().expandShiftParts(Op.getNode(), Lo, Hi, DAG); | |||
20072 | return DAG.getMergeValues({Lo, Hi}, SDLoc(Op)); | |||
20073 | } | |||
20074 | ||||
20075 | static SDValue LowerFunnelShift(SDValue Op, const X86Subtarget &Subtarget, | |||
20076 | SelectionDAG &DAG) { | |||
20077 | MVT VT = Op.getSimpleValueType(); | |||
20078 | assert((Op.getOpcode() == ISD::FSHL || Op.getOpcode() == ISD::FSHR) &&(static_cast <bool> ((Op.getOpcode() == ISD::FSHL || Op .getOpcode() == ISD::FSHR) && "Unexpected funnel shift opcode!" ) ? void (0) : __assert_fail ("(Op.getOpcode() == ISD::FSHL || Op.getOpcode() == ISD::FSHR) && \"Unexpected funnel shift opcode!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20079, __extension__ __PRETTY_FUNCTION__)) | |||
20079 | "Unexpected funnel shift opcode!")(static_cast <bool> ((Op.getOpcode() == ISD::FSHL || Op .getOpcode() == ISD::FSHR) && "Unexpected funnel shift opcode!" ) ? void (0) : __assert_fail ("(Op.getOpcode() == ISD::FSHL || Op.getOpcode() == ISD::FSHR) && \"Unexpected funnel shift opcode!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20079, __extension__ __PRETTY_FUNCTION__)); | |||
20080 | ||||
20081 | SDLoc DL(Op); | |||
20082 | SDValue Op0 = Op.getOperand(0); | |||
20083 | SDValue Op1 = Op.getOperand(1); | |||
20084 | SDValue Amt = Op.getOperand(2); | |||
20085 | ||||
20086 | bool IsFSHR = Op.getOpcode() == ISD::FSHR; | |||
20087 | ||||
20088 | if (VT.isVector()) { | |||
20089 | assert(Subtarget.hasVBMI2() && "Expected VBMI2")(static_cast <bool> (Subtarget.hasVBMI2() && "Expected VBMI2" ) ? void (0) : __assert_fail ("Subtarget.hasVBMI2() && \"Expected VBMI2\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20089, __extension__ __PRETTY_FUNCTION__)); | |||
20090 | ||||
20091 | if (IsFSHR) | |||
20092 | std::swap(Op0, Op1); | |||
20093 | ||||
20094 | // With AVX512, but not VLX we need to widen to get a 512-bit result type. | |||
20095 | if (!Subtarget.hasVLX() && !VT.is512BitVector()) { | |||
20096 | Op0 = widenSubVector(Op0, false, Subtarget, DAG, DL, 512); | |||
20097 | Op1 = widenSubVector(Op1, false, Subtarget, DAG, DL, 512); | |||
20098 | } | |||
20099 | ||||
20100 | SDValue Funnel; | |||
20101 | APInt APIntShiftAmt; | |||
20102 | MVT ResultVT = Op0.getSimpleValueType(); | |||
20103 | if (X86::isConstantSplat(Amt, APIntShiftAmt)) { | |||
20104 | uint64_t ShiftAmt = APIntShiftAmt.urem(VT.getScalarSizeInBits()); | |||
20105 | Funnel = | |||
20106 | DAG.getNode(IsFSHR ? X86ISD::VSHRD : X86ISD::VSHLD, DL, ResultVT, Op0, | |||
20107 | Op1, DAG.getTargetConstant(ShiftAmt, DL, MVT::i8)); | |||
20108 | } else { | |||
20109 | if (!Subtarget.hasVLX() && !VT.is512BitVector()) | |||
20110 | Amt = widenSubVector(Amt, false, Subtarget, DAG, DL, 512); | |||
20111 | Funnel = DAG.getNode(IsFSHR ? X86ISD::VSHRDV : X86ISD::VSHLDV, DL, | |||
20112 | ResultVT, Op0, Op1, Amt); | |||
20113 | } | |||
20114 | if (!Subtarget.hasVLX() && !VT.is512BitVector()) | |||
20115 | Funnel = extractSubVector(Funnel, 0, DAG, DL, VT.getSizeInBits()); | |||
20116 | return Funnel; | |||
20117 | } | |||
20118 | assert((static_cast <bool> ((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32 || VT == MVT::i64) && "Unexpected funnel shift type!" ) ? void (0) : __assert_fail ("(VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32 || VT == MVT::i64) && \"Unexpected funnel shift type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20120, __extension__ __PRETTY_FUNCTION__)) | |||
20119 | (VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32 || VT == MVT::i64) &&(static_cast <bool> ((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32 || VT == MVT::i64) && "Unexpected funnel shift type!" ) ? void (0) : __assert_fail ("(VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32 || VT == MVT::i64) && \"Unexpected funnel shift type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20120, __extension__ __PRETTY_FUNCTION__)) | |||
20120 | "Unexpected funnel shift type!")(static_cast <bool> ((VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32 || VT == MVT::i64) && "Unexpected funnel shift type!" ) ? void (0) : __assert_fail ("(VT == MVT::i8 || VT == MVT::i16 || VT == MVT::i32 || VT == MVT::i64) && \"Unexpected funnel shift type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20120, __extension__ __PRETTY_FUNCTION__)); | |||
20121 | ||||
20122 | // Expand slow SHLD/SHRD cases if we are not optimizing for size. | |||
20123 | bool OptForSize = DAG.shouldOptForSize(); | |||
20124 | bool ExpandFunnel = !OptForSize && Subtarget.isSHLDSlow(); | |||
20125 | ||||
20126 | // fshl(x,y,z) -> (((aext(x) << bw) | zext(y)) << (z & (bw-1))) >> bw. | |||
20127 | // fshr(x,y,z) -> (((aext(x) << bw) | zext(y)) >> (z & (bw-1))). | |||
20128 | if ((VT == MVT::i8 || (ExpandFunnel && VT == MVT::i16)) && | |||
20129 | !isa<ConstantSDNode>(Amt)) { | |||
20130 | unsigned EltSizeInBits = VT.getScalarSizeInBits(); | |||
20131 | SDValue Mask = DAG.getConstant(EltSizeInBits - 1, DL, Amt.getValueType()); | |||
20132 | SDValue HiShift = DAG.getConstant(EltSizeInBits, DL, Amt.getValueType()); | |||
20133 | Op0 = DAG.getAnyExtOrTrunc(Op0, DL, MVT::i32); | |||
20134 | Op1 = DAG.getZExtOrTrunc(Op1, DL, MVT::i32); | |||
20135 | Amt = DAG.getNode(ISD::AND, DL, Amt.getValueType(), Amt, Mask); | |||
20136 | SDValue Res = DAG.getNode(ISD::SHL, DL, MVT::i32, Op0, HiShift); | |||
20137 | Res = DAG.getNode(ISD::OR, DL, MVT::i32, Res, Op1); | |||
20138 | if (IsFSHR) { | |||
20139 | Res = DAG.getNode(ISD::SRL, DL, MVT::i32, Res, Amt); | |||
20140 | } else { | |||
20141 | Res = DAG.getNode(ISD::SHL, DL, MVT::i32, Res, Amt); | |||
20142 | Res = DAG.getNode(ISD::SRL, DL, MVT::i32, Res, HiShift); | |||
20143 | } | |||
20144 | return DAG.getZExtOrTrunc(Res, DL, VT); | |||
20145 | } | |||
20146 | ||||
20147 | if (VT == MVT::i8 || ExpandFunnel) | |||
20148 | return SDValue(); | |||
20149 | ||||
20150 | // i16 needs to modulo the shift amount, but i32/i64 have implicit modulo. | |||
20151 | if (VT == MVT::i16) { | |||
20152 | Amt = DAG.getNode(ISD::AND, DL, Amt.getValueType(), Amt, | |||
20153 | DAG.getConstant(15, DL, Amt.getValueType())); | |||
20154 | unsigned FSHOp = (IsFSHR ? X86ISD::FSHR : X86ISD::FSHL); | |||
20155 | return DAG.getNode(FSHOp, DL, VT, Op0, Op1, Amt); | |||
20156 | } | |||
20157 | ||||
20158 | return Op; | |||
20159 | } | |||
20160 | ||||
20161 | // Try to use a packed vector operation to handle i64 on 32-bit targets when | |||
20162 | // AVX512DQ is enabled. | |||
20163 | static SDValue LowerI64IntToFP_AVX512DQ(SDValue Op, SelectionDAG &DAG, | |||
20164 | const X86Subtarget &Subtarget) { | |||
20165 | assert((Op.getOpcode() == ISD::SINT_TO_FP ||(static_cast <bool> ((Op.getOpcode() == ISD::SINT_TO_FP || Op.getOpcode() == ISD::STRICT_SINT_TO_FP || Op.getOpcode( ) == ISD::STRICT_UINT_TO_FP || Op.getOpcode() == ISD::UINT_TO_FP ) && "Unexpected opcode!") ? void (0) : __assert_fail ("(Op.getOpcode() == ISD::SINT_TO_FP || Op.getOpcode() == ISD::STRICT_SINT_TO_FP || Op.getOpcode() == ISD::STRICT_UINT_TO_FP || Op.getOpcode() == ISD::UINT_TO_FP) && \"Unexpected opcode!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20169, __extension__ __PRETTY_FUNCTION__)) | |||
20166 | Op.getOpcode() == ISD::STRICT_SINT_TO_FP ||(static_cast <bool> ((Op.getOpcode() == ISD::SINT_TO_FP || Op.getOpcode() == ISD::STRICT_SINT_TO_FP || Op.getOpcode( ) == ISD::STRICT_UINT_TO_FP || Op.getOpcode() == ISD::UINT_TO_FP ) && "Unexpected opcode!") ? void (0) : __assert_fail ("(Op.getOpcode() == ISD::SINT_TO_FP || Op.getOpcode() == ISD::STRICT_SINT_TO_FP || Op.getOpcode() == ISD::STRICT_UINT_TO_FP || Op.getOpcode() == ISD::UINT_TO_FP) && \"Unexpected opcode!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20169, __extension__ __PRETTY_FUNCTION__)) | |||
20167 | Op.getOpcode() == ISD::STRICT_UINT_TO_FP ||(static_cast <bool> ((Op.getOpcode() == ISD::SINT_TO_FP || Op.getOpcode() == ISD::STRICT_SINT_TO_FP || Op.getOpcode( ) == ISD::STRICT_UINT_TO_FP || Op.getOpcode() == ISD::UINT_TO_FP ) && "Unexpected opcode!") ? void (0) : __assert_fail ("(Op.getOpcode() == ISD::SINT_TO_FP || Op.getOpcode() == ISD::STRICT_SINT_TO_FP || Op.getOpcode() == ISD::STRICT_UINT_TO_FP || Op.getOpcode() == ISD::UINT_TO_FP) && \"Unexpected opcode!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20169, __extension__ __PRETTY_FUNCTION__)) | |||
20168 | Op.getOpcode() == ISD::UINT_TO_FP) &&(static_cast <bool> ((Op.getOpcode() == ISD::SINT_TO_FP || Op.getOpcode() == ISD::STRICT_SINT_TO_FP || Op.getOpcode( ) == ISD::STRICT_UINT_TO_FP || Op.getOpcode() == ISD::UINT_TO_FP ) && "Unexpected opcode!") ? void (0) : __assert_fail ("(Op.getOpcode() == ISD::SINT_TO_FP || Op.getOpcode() == ISD::STRICT_SINT_TO_FP || Op.getOpcode() == ISD::STRICT_UINT_TO_FP || Op.getOpcode() == ISD::UINT_TO_FP) && \"Unexpected opcode!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20169, __extension__ __PRETTY_FUNCTION__)) | |||
20169 | "Unexpected opcode!")(static_cast <bool> ((Op.getOpcode() == ISD::SINT_TO_FP || Op.getOpcode() == ISD::STRICT_SINT_TO_FP || Op.getOpcode( ) == ISD::STRICT_UINT_TO_FP || Op.getOpcode() == ISD::UINT_TO_FP ) && "Unexpected opcode!") ? void (0) : __assert_fail ("(Op.getOpcode() == ISD::SINT_TO_FP || Op.getOpcode() == ISD::STRICT_SINT_TO_FP || Op.getOpcode() == ISD::STRICT_UINT_TO_FP || Op.getOpcode() == ISD::UINT_TO_FP) && \"Unexpected opcode!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20169, __extension__ __PRETTY_FUNCTION__)); | |||
20170 | bool IsStrict = Op->isStrictFPOpcode(); | |||
20171 | unsigned OpNo = IsStrict ? 1 : 0; | |||
20172 | SDValue Src = Op.getOperand(OpNo); | |||
20173 | MVT SrcVT = Src.getSimpleValueType(); | |||
20174 | MVT VT = Op.getSimpleValueType(); | |||
20175 | ||||
20176 | if (!Subtarget.hasDQI() || SrcVT != MVT::i64 || Subtarget.is64Bit() || | |||
20177 | (VT != MVT::f32 && VT != MVT::f64)) | |||
20178 | return SDValue(); | |||
20179 | ||||
20180 | // Pack the i64 into a vector, do the operation and extract. | |||
20181 | ||||
20182 | // Using 256-bit to ensure result is 128-bits for f32 case. | |||
20183 | unsigned NumElts = Subtarget.hasVLX() ? 4 : 8; | |||
20184 | MVT VecInVT = MVT::getVectorVT(MVT::i64, NumElts); | |||
20185 | MVT VecVT = MVT::getVectorVT(VT, NumElts); | |||
20186 | ||||
20187 | SDLoc dl(Op); | |||
20188 | SDValue InVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecInVT, Src); | |||
20189 | if (IsStrict) { | |||
20190 | SDValue CvtVec = DAG.getNode(Op.getOpcode(), dl, {VecVT, MVT::Other}, | |||
20191 | {Op.getOperand(0), InVec}); | |||
20192 | SDValue Chain = CvtVec.getValue(1); | |||
20193 | SDValue Value = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, CvtVec, | |||
20194 | DAG.getIntPtrConstant(0, dl)); | |||
20195 | return DAG.getMergeValues({Value, Chain}, dl); | |||
20196 | } | |||
20197 | ||||
20198 | SDValue CvtVec = DAG.getNode(Op.getOpcode(), dl, VecVT, InVec); | |||
20199 | ||||
20200 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, CvtVec, | |||
20201 | DAG.getIntPtrConstant(0, dl)); | |||
20202 | } | |||
20203 | ||||
20204 | // Try to use a packed vector operation to handle i64 on 32-bit targets. | |||
20205 | static SDValue LowerI64IntToFP16(SDValue Op, SelectionDAG &DAG, | |||
20206 | const X86Subtarget &Subtarget) { | |||
20207 | assert((Op.getOpcode() == ISD::SINT_TO_FP ||(static_cast <bool> ((Op.getOpcode() == ISD::SINT_TO_FP || Op.getOpcode() == ISD::STRICT_SINT_TO_FP || Op.getOpcode( ) == ISD::STRICT_UINT_TO_FP || Op.getOpcode() == ISD::UINT_TO_FP ) && "Unexpected opcode!") ? void (0) : __assert_fail ("(Op.getOpcode() == ISD::SINT_TO_FP || Op.getOpcode() == ISD::STRICT_SINT_TO_FP || Op.getOpcode() == ISD::STRICT_UINT_TO_FP || Op.getOpcode() == ISD::UINT_TO_FP) && \"Unexpected opcode!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20211, __extension__ __PRETTY_FUNCTION__)) | |||
20208 | Op.getOpcode() == ISD::STRICT_SINT_TO_FP ||(static_cast <bool> ((Op.getOpcode() == ISD::SINT_TO_FP || Op.getOpcode() == ISD::STRICT_SINT_TO_FP || Op.getOpcode( ) == ISD::STRICT_UINT_TO_FP || Op.getOpcode() == ISD::UINT_TO_FP ) && "Unexpected opcode!") ? void (0) : __assert_fail ("(Op.getOpcode() == ISD::SINT_TO_FP || Op.getOpcode() == ISD::STRICT_SINT_TO_FP || Op.getOpcode() == ISD::STRICT_UINT_TO_FP || Op.getOpcode() == ISD::UINT_TO_FP) && \"Unexpected opcode!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20211, __extension__ __PRETTY_FUNCTION__)) | |||
20209 | Op.getOpcode() == ISD::STRICT_UINT_TO_FP ||(static_cast <bool> ((Op.getOpcode() == ISD::SINT_TO_FP || Op.getOpcode() == ISD::STRICT_SINT_TO_FP || Op.getOpcode( ) == ISD::STRICT_UINT_TO_FP || Op.getOpcode() == ISD::UINT_TO_FP ) && "Unexpected opcode!") ? void (0) : __assert_fail ("(Op.getOpcode() == ISD::SINT_TO_FP || Op.getOpcode() == ISD::STRICT_SINT_TO_FP || Op.getOpcode() == ISD::STRICT_UINT_TO_FP || Op.getOpcode() == ISD::UINT_TO_FP) && \"Unexpected opcode!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20211, __extension__ __PRETTY_FUNCTION__)) | |||
20210 | Op.getOpcode() == ISD::UINT_TO_FP) &&(static_cast <bool> ((Op.getOpcode() == ISD::SINT_TO_FP || Op.getOpcode() == ISD::STRICT_SINT_TO_FP || Op.getOpcode( ) == ISD::STRICT_UINT_TO_FP || Op.getOpcode() == ISD::UINT_TO_FP ) && "Unexpected opcode!") ? void (0) : __assert_fail ("(Op.getOpcode() == ISD::SINT_TO_FP || Op.getOpcode() == ISD::STRICT_SINT_TO_FP || Op.getOpcode() == ISD::STRICT_UINT_TO_FP || Op.getOpcode() == ISD::UINT_TO_FP) && \"Unexpected opcode!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20211, __extension__ __PRETTY_FUNCTION__)) | |||
20211 | "Unexpected opcode!")(static_cast <bool> ((Op.getOpcode() == ISD::SINT_TO_FP || Op.getOpcode() == ISD::STRICT_SINT_TO_FP || Op.getOpcode( ) == ISD::STRICT_UINT_TO_FP || Op.getOpcode() == ISD::UINT_TO_FP ) && "Unexpected opcode!") ? void (0) : __assert_fail ("(Op.getOpcode() == ISD::SINT_TO_FP || Op.getOpcode() == ISD::STRICT_SINT_TO_FP || Op.getOpcode() == ISD::STRICT_UINT_TO_FP || Op.getOpcode() == ISD::UINT_TO_FP) && \"Unexpected opcode!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20211, __extension__ __PRETTY_FUNCTION__)); | |||
20212 | bool IsStrict = Op->isStrictFPOpcode(); | |||
20213 | SDValue Src = Op.getOperand(IsStrict ? 1 : 0); | |||
20214 | MVT SrcVT = Src.getSimpleValueType(); | |||
20215 | MVT VT = Op.getSimpleValueType(); | |||
20216 | ||||
20217 | if (SrcVT != MVT::i64 || Subtarget.is64Bit() || VT != MVT::f16) | |||
20218 | return SDValue(); | |||
20219 | ||||
20220 | // Pack the i64 into a vector, do the operation and extract. | |||
20221 | ||||
20222 | assert(Subtarget.hasFP16() && "Expected FP16")(static_cast <bool> (Subtarget.hasFP16() && "Expected FP16" ) ? void (0) : __assert_fail ("Subtarget.hasFP16() && \"Expected FP16\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20222, __extension__ __PRETTY_FUNCTION__)); | |||
20223 | ||||
20224 | SDLoc dl(Op); | |||
20225 | SDValue InVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Src); | |||
20226 | if (IsStrict) { | |||
20227 | SDValue CvtVec = DAG.getNode(Op.getOpcode(), dl, {MVT::v2f16, MVT::Other}, | |||
20228 | {Op.getOperand(0), InVec}); | |||
20229 | SDValue Chain = CvtVec.getValue(1); | |||
20230 | SDValue Value = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, CvtVec, | |||
20231 | DAG.getIntPtrConstant(0, dl)); | |||
20232 | return DAG.getMergeValues({Value, Chain}, dl); | |||
20233 | } | |||
20234 | ||||
20235 | SDValue CvtVec = DAG.getNode(Op.getOpcode(), dl, MVT::v2f16, InVec); | |||
20236 | ||||
20237 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, CvtVec, | |||
20238 | DAG.getIntPtrConstant(0, dl)); | |||
20239 | } | |||
20240 | ||||
20241 | static bool useVectorCast(unsigned Opcode, MVT FromVT, MVT ToVT, | |||
20242 | const X86Subtarget &Subtarget) { | |||
20243 | switch (Opcode) { | |||
20244 | case ISD::SINT_TO_FP: | |||
20245 | // TODO: Handle wider types with AVX/AVX512. | |||
20246 | if (!Subtarget.hasSSE2() || FromVT != MVT::v4i32) | |||
20247 | return false; | |||
20248 | // CVTDQ2PS or (V)CVTDQ2PD | |||
20249 | return ToVT == MVT::v4f32 || (Subtarget.hasAVX() && ToVT == MVT::v4f64); | |||
20250 | ||||
20251 | case ISD::UINT_TO_FP: | |||
20252 | // TODO: Handle wider types and i64 elements. | |||
20253 | if (!Subtarget.hasAVX512() || FromVT != MVT::v4i32) | |||
20254 | return false; | |||
20255 | // VCVTUDQ2PS or VCVTUDQ2PD | |||
20256 | return ToVT == MVT::v4f32 || ToVT == MVT::v4f64; | |||
20257 | ||||
20258 | default: | |||
20259 | return false; | |||
20260 | } | |||
20261 | } | |||
20262 | ||||
20263 | /// Given a scalar cast operation that is extracted from a vector, try to | |||
20264 | /// vectorize the cast op followed by extraction. This will avoid an expensive | |||
20265 | /// round-trip between XMM and GPR. | |||
20266 | static SDValue vectorizeExtractedCast(SDValue Cast, SelectionDAG &DAG, | |||
20267 | const X86Subtarget &Subtarget) { | |||
20268 | // TODO: This could be enhanced to handle smaller integer types by peeking | |||
20269 | // through an extend. | |||
20270 | SDValue Extract = Cast.getOperand(0); | |||
20271 | MVT DestVT = Cast.getSimpleValueType(); | |||
20272 | if (Extract.getOpcode() != ISD::EXTRACT_VECTOR_ELT || | |||
20273 | !isa<ConstantSDNode>(Extract.getOperand(1))) | |||
20274 | return SDValue(); | |||
20275 | ||||
20276 | // See if we have a 128-bit vector cast op for this type of cast. | |||
20277 | SDValue VecOp = Extract.getOperand(0); | |||
20278 | MVT FromVT = VecOp.getSimpleValueType(); | |||
20279 | unsigned NumEltsInXMM = 128 / FromVT.getScalarSizeInBits(); | |||
20280 | MVT Vec128VT = MVT::getVectorVT(FromVT.getScalarType(), NumEltsInXMM); | |||
20281 | MVT ToVT = MVT::getVectorVT(DestVT, NumEltsInXMM); | |||
20282 | if (!useVectorCast(Cast.getOpcode(), Vec128VT, ToVT, Subtarget)) | |||
20283 | return SDValue(); | |||
20284 | ||||
20285 | // If we are extracting from a non-zero element, first shuffle the source | |||
20286 | // vector to allow extracting from element zero. | |||
20287 | SDLoc DL(Cast); | |||
20288 | if (!isNullConstant(Extract.getOperand(1))) { | |||
20289 | SmallVector<int, 16> Mask(FromVT.getVectorNumElements(), -1); | |||
20290 | Mask[0] = Extract.getConstantOperandVal(1); | |||
20291 | VecOp = DAG.getVectorShuffle(FromVT, DL, VecOp, DAG.getUNDEF(FromVT), Mask); | |||
20292 | } | |||
20293 | // If the source vector is wider than 128-bits, extract the low part. Do not | |||
20294 | // create an unnecessarily wide vector cast op. | |||
20295 | if (FromVT != Vec128VT) | |||
20296 | VecOp = extract128BitVector(VecOp, 0, DAG, DL); | |||
20297 | ||||
20298 | // cast (extelt V, 0) --> extelt (cast (extract_subv V)), 0 | |||
20299 | // cast (extelt V, C) --> extelt (cast (extract_subv (shuffle V, [C...]))), 0 | |||
20300 | SDValue VCast = DAG.getNode(Cast.getOpcode(), DL, ToVT, VecOp); | |||
20301 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, DestVT, VCast, | |||
20302 | DAG.getIntPtrConstant(0, DL)); | |||
20303 | } | |||
20304 | ||||
20305 | /// Given a scalar cast to FP with a cast to integer operand (almost an ftrunc), | |||
20306 | /// try to vectorize the cast ops. This will avoid an expensive round-trip | |||
20307 | /// between XMM and GPR. | |||
20308 | static SDValue lowerFPToIntToFP(SDValue CastToFP, SelectionDAG &DAG, | |||
20309 | const X86Subtarget &Subtarget) { | |||
20310 | // TODO: Allow FP_TO_UINT. | |||
20311 | SDValue CastToInt = CastToFP.getOperand(0); | |||
20312 | MVT VT = CastToFP.getSimpleValueType(); | |||
20313 | if (CastToInt.getOpcode() != ISD::FP_TO_SINT || VT.isVector()) | |||
20314 | return SDValue(); | |||
20315 | ||||
20316 | MVT IntVT = CastToInt.getSimpleValueType(); | |||
20317 | SDValue X = CastToInt.getOperand(0); | |||
20318 | MVT SrcVT = X.getSimpleValueType(); | |||
20319 | if (SrcVT != MVT::f32 && SrcVT != MVT::f64) | |||
20320 | return SDValue(); | |||
20321 | ||||
20322 | // See if we have 128-bit vector cast instructions for this type of cast. | |||
20323 | // We need cvttps2dq/cvttpd2dq and cvtdq2ps/cvtdq2pd. | |||
20324 | if (!Subtarget.hasSSE2() || (VT != MVT::f32 && VT != MVT::f64) || | |||
20325 | IntVT != MVT::i32) | |||
20326 | return SDValue(); | |||
20327 | ||||
20328 | unsigned SrcSize = SrcVT.getSizeInBits(); | |||
20329 | unsigned IntSize = IntVT.getSizeInBits(); | |||
20330 | unsigned VTSize = VT.getSizeInBits(); | |||
20331 | MVT VecSrcVT = MVT::getVectorVT(SrcVT, 128 / SrcSize); | |||
20332 | MVT VecIntVT = MVT::getVectorVT(IntVT, 128 / IntSize); | |||
20333 | MVT VecVT = MVT::getVectorVT(VT, 128 / VTSize); | |||
20334 | ||||
20335 | // We need target-specific opcodes if this is v2f64 -> v4i32 -> v2f64. | |||
20336 | unsigned ToIntOpcode = | |||
20337 | SrcSize != IntSize ? X86ISD::CVTTP2SI : (unsigned)ISD::FP_TO_SINT; | |||
20338 | unsigned ToFPOpcode = | |||
20339 | IntSize != VTSize ? X86ISD::CVTSI2P : (unsigned)ISD::SINT_TO_FP; | |||
20340 | ||||
20341 | // sint_to_fp (fp_to_sint X) --> extelt (sint_to_fp (fp_to_sint (s2v X))), 0 | |||
20342 | // | |||
20343 | // We are not defining the high elements (for example, zero them) because | |||
20344 | // that could nullify any performance advantage that we hoped to gain from | |||
20345 | // this vector op hack. We do not expect any adverse effects (like denorm | |||
20346 | // penalties) with cast ops. | |||
20347 | SDLoc DL(CastToFP); | |||
20348 | SDValue ZeroIdx = DAG.getIntPtrConstant(0, DL); | |||
20349 | SDValue VecX = DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VecSrcVT, X); | |||
20350 | SDValue VCastToInt = DAG.getNode(ToIntOpcode, DL, VecIntVT, VecX); | |||
20351 | SDValue VCastToFP = DAG.getNode(ToFPOpcode, DL, VecVT, VCastToInt); | |||
20352 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, VCastToFP, ZeroIdx); | |||
20353 | } | |||
20354 | ||||
20355 | static SDValue lowerINT_TO_FP_vXi64(SDValue Op, SelectionDAG &DAG, | |||
20356 | const X86Subtarget &Subtarget) { | |||
20357 | SDLoc DL(Op); | |||
20358 | bool IsStrict = Op->isStrictFPOpcode(); | |||
20359 | MVT VT = Op->getSimpleValueType(0); | |||
20360 | SDValue Src = Op->getOperand(IsStrict ? 1 : 0); | |||
20361 | ||||
20362 | if (Subtarget.hasDQI()) { | |||
20363 | assert(!Subtarget.hasVLX() && "Unexpected features")(static_cast <bool> (!Subtarget.hasVLX() && "Unexpected features" ) ? void (0) : __assert_fail ("!Subtarget.hasVLX() && \"Unexpected features\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20363, __extension__ __PRETTY_FUNCTION__)); | |||
20364 | ||||
20365 | assert((Src.getSimpleValueType() == MVT::v2i64 ||(static_cast <bool> ((Src.getSimpleValueType() == MVT:: v2i64 || Src.getSimpleValueType() == MVT::v4i64) && "Unsupported custom type" ) ? void (0) : __assert_fail ("(Src.getSimpleValueType() == MVT::v2i64 || Src.getSimpleValueType() == MVT::v4i64) && \"Unsupported custom type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20367, __extension__ __PRETTY_FUNCTION__)) | |||
20366 | Src.getSimpleValueType() == MVT::v4i64) &&(static_cast <bool> ((Src.getSimpleValueType() == MVT:: v2i64 || Src.getSimpleValueType() == MVT::v4i64) && "Unsupported custom type" ) ? void (0) : __assert_fail ("(Src.getSimpleValueType() == MVT::v2i64 || Src.getSimpleValueType() == MVT::v4i64) && \"Unsupported custom type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20367, __extension__ __PRETTY_FUNCTION__)) | |||
20367 | "Unsupported custom type")(static_cast <bool> ((Src.getSimpleValueType() == MVT:: v2i64 || Src.getSimpleValueType() == MVT::v4i64) && "Unsupported custom type" ) ? void (0) : __assert_fail ("(Src.getSimpleValueType() == MVT::v2i64 || Src.getSimpleValueType() == MVT::v4i64) && \"Unsupported custom type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20367, __extension__ __PRETTY_FUNCTION__)); | |||
20368 | ||||
20369 | // With AVX512DQ, but not VLX we need to widen to get a 512-bit result type. | |||
20370 | assert((VT == MVT::v4f32 || VT == MVT::v2f64 || VT == MVT::v4f64) &&(static_cast <bool> ((VT == MVT::v4f32 || VT == MVT::v2f64 || VT == MVT::v4f64) && "Unexpected VT!") ? void (0) : __assert_fail ("(VT == MVT::v4f32 || VT == MVT::v2f64 || VT == MVT::v4f64) && \"Unexpected VT!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20371, __extension__ __PRETTY_FUNCTION__)) | |||
20371 | "Unexpected VT!")(static_cast <bool> ((VT == MVT::v4f32 || VT == MVT::v2f64 || VT == MVT::v4f64) && "Unexpected VT!") ? void (0) : __assert_fail ("(VT == MVT::v4f32 || VT == MVT::v2f64 || VT == MVT::v4f64) && \"Unexpected VT!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20371, __extension__ __PRETTY_FUNCTION__)); | |||
20372 | MVT WideVT = VT == MVT::v4f32 ? MVT::v8f32 : MVT::v8f64; | |||
20373 | ||||
20374 | // Need to concat with zero vector for strict fp to avoid spurious | |||
20375 | // exceptions. | |||
20376 | SDValue Tmp = IsStrict ? DAG.getConstant(0, DL, MVT::v8i64) | |||
20377 | : DAG.getUNDEF(MVT::v8i64); | |||
20378 | Src = DAG.getNode(ISD::INSERT_SUBVECTOR, DL, MVT::v8i64, Tmp, Src, | |||
20379 | DAG.getIntPtrConstant(0, DL)); | |||
20380 | SDValue Res, Chain; | |||
20381 | if (IsStrict) { | |||
20382 | Res = DAG.getNode(Op.getOpcode(), DL, {WideVT, MVT::Other}, | |||
20383 | {Op->getOperand(0), Src}); | |||
20384 | Chain = Res.getValue(1); | |||
20385 | } else { | |||
20386 | Res = DAG.getNode(Op.getOpcode(), DL, WideVT, Src); | |||
20387 | } | |||
20388 | ||||
20389 | Res = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Res, | |||
20390 | DAG.getIntPtrConstant(0, DL)); | |||
20391 | ||||
20392 | if (IsStrict) | |||
20393 | return DAG.getMergeValues({Res, Chain}, DL); | |||
20394 | return Res; | |||
20395 | } | |||
20396 | ||||
20397 | bool IsSigned = Op->getOpcode() == ISD::SINT_TO_FP || | |||
20398 | Op->getOpcode() == ISD::STRICT_SINT_TO_FP; | |||
20399 | if (VT != MVT::v4f32 || IsSigned) | |||
20400 | return SDValue(); | |||
20401 | ||||
20402 | SDValue Zero = DAG.getConstant(0, DL, MVT::v4i64); | |||
20403 | SDValue One = DAG.getConstant(1, DL, MVT::v4i64); | |||
20404 | SDValue Sign = DAG.getNode(ISD::OR, DL, MVT::v4i64, | |||
20405 | DAG.getNode(ISD::SRL, DL, MVT::v4i64, Src, One), | |||
20406 | DAG.getNode(ISD::AND, DL, MVT::v4i64, Src, One)); | |||
20407 | SDValue IsNeg = DAG.getSetCC(DL, MVT::v4i64, Src, Zero, ISD::SETLT); | |||
20408 | SDValue SignSrc = DAG.getSelect(DL, MVT::v4i64, IsNeg, Sign, Src); | |||
20409 | SmallVector<SDValue, 4> SignCvts(4); | |||
20410 | SmallVector<SDValue, 4> Chains(4); | |||
20411 | for (int i = 0; i != 4; ++i) { | |||
20412 | SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i64, SignSrc, | |||
20413 | DAG.getIntPtrConstant(i, DL)); | |||
20414 | if (IsStrict) { | |||
20415 | SignCvts[i] = | |||
20416 | DAG.getNode(ISD::STRICT_SINT_TO_FP, DL, {MVT::f32, MVT::Other}, | |||
20417 | {Op.getOperand(0), Elt}); | |||
20418 | Chains[i] = SignCvts[i].getValue(1); | |||
20419 | } else { | |||
20420 | SignCvts[i] = DAG.getNode(ISD::SINT_TO_FP, DL, MVT::f32, Elt); | |||
20421 | } | |||
20422 | } | |||
20423 | SDValue SignCvt = DAG.getBuildVector(VT, DL, SignCvts); | |||
20424 | ||||
20425 | SDValue Slow, Chain; | |||
20426 | if (IsStrict) { | |||
20427 | Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains); | |||
20428 | Slow = DAG.getNode(ISD::STRICT_FADD, DL, {MVT::v4f32, MVT::Other}, | |||
20429 | {Chain, SignCvt, SignCvt}); | |||
20430 | Chain = Slow.getValue(1); | |||
20431 | } else { | |||
20432 | Slow = DAG.getNode(ISD::FADD, DL, MVT::v4f32, SignCvt, SignCvt); | |||
20433 | } | |||
20434 | ||||
20435 | IsNeg = DAG.getNode(ISD::TRUNCATE, DL, MVT::v4i32, IsNeg); | |||
20436 | SDValue Cvt = DAG.getSelect(DL, MVT::v4f32, IsNeg, Slow, SignCvt); | |||
20437 | ||||
20438 | if (IsStrict) | |||
20439 | return DAG.getMergeValues({Cvt, Chain}, DL); | |||
20440 | ||||
20441 | return Cvt; | |||
20442 | } | |||
20443 | ||||
20444 | SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, | |||
20445 | SelectionDAG &DAG) const { | |||
20446 | bool IsStrict = Op->isStrictFPOpcode(); | |||
20447 | unsigned OpNo = IsStrict ? 1 : 0; | |||
20448 | SDValue Src = Op.getOperand(OpNo); | |||
20449 | SDValue Chain = IsStrict ? Op->getOperand(0) : DAG.getEntryNode(); | |||
20450 | MVT SrcVT = Src.getSimpleValueType(); | |||
20451 | MVT VT = Op.getSimpleValueType(); | |||
20452 | SDLoc dl(Op); | |||
20453 | ||||
20454 | if (Subtarget.isTargetWin64() && SrcVT == MVT::i128) | |||
20455 | return LowerWin64_INT128_TO_FP(Op, DAG); | |||
20456 | ||||
20457 | if (SDValue Extract = vectorizeExtractedCast(Op, DAG, Subtarget)) | |||
20458 | return Extract; | |||
20459 | ||||
20460 | if (SDValue R = lowerFPToIntToFP(Op, DAG, Subtarget)) | |||
20461 | return R; | |||
20462 | ||||
20463 | if (SrcVT.isVector()) { | |||
20464 | if (SrcVT == MVT::v2i32 && VT == MVT::v2f64) { | |||
20465 | // Note: Since v2f64 is a legal type. We don't need to zero extend the | |||
20466 | // source for strict FP. | |||
20467 | if (IsStrict) | |||
20468 | return DAG.getNode( | |||
20469 | X86ISD::STRICT_CVTSI2P, dl, {VT, MVT::Other}, | |||
20470 | {Chain, DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v4i32, Src, | |||
20471 | DAG.getUNDEF(SrcVT))}); | |||
20472 | return DAG.getNode(X86ISD::CVTSI2P, dl, VT, | |||
20473 | DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v4i32, Src, | |||
20474 | DAG.getUNDEF(SrcVT))); | |||
20475 | } | |||
20476 | if (SrcVT == MVT::v2i64 || SrcVT == MVT::v4i64) | |||
20477 | return lowerINT_TO_FP_vXi64(Op, DAG, Subtarget); | |||
20478 | ||||
20479 | return SDValue(); | |||
20480 | } | |||
20481 | ||||
20482 | assert(SrcVT <= MVT::i64 && SrcVT >= MVT::i16 &&(static_cast <bool> (SrcVT <= MVT::i64 && SrcVT >= MVT::i16 && "Unknown SINT_TO_FP to lower!") ? void (0) : __assert_fail ("SrcVT <= MVT::i64 && SrcVT >= MVT::i16 && \"Unknown SINT_TO_FP to lower!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20483, __extension__ __PRETTY_FUNCTION__)) | |||
20483 | "Unknown SINT_TO_FP to lower!")(static_cast <bool> (SrcVT <= MVT::i64 && SrcVT >= MVT::i16 && "Unknown SINT_TO_FP to lower!") ? void (0) : __assert_fail ("SrcVT <= MVT::i64 && SrcVT >= MVT::i16 && \"Unknown SINT_TO_FP to lower!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20483, __extension__ __PRETTY_FUNCTION__)); | |||
20484 | ||||
20485 | bool UseSSEReg = isScalarFPTypeInSSEReg(VT); | |||
20486 | ||||
20487 | // These are really Legal; return the operand so the caller accepts it as | |||
20488 | // Legal. | |||
20489 | if (SrcVT == MVT::i32 && UseSSEReg) | |||
20490 | return Op; | |||
20491 | if (SrcVT == MVT::i64 && UseSSEReg && Subtarget.is64Bit()) | |||
20492 | return Op; | |||
20493 | ||||
20494 | if (SDValue V = LowerI64IntToFP_AVX512DQ(Op, DAG, Subtarget)) | |||
20495 | return V; | |||
20496 | if (SDValue V = LowerI64IntToFP16(Op, DAG, Subtarget)) | |||
20497 | return V; | |||
20498 | ||||
20499 | // SSE doesn't have an i16 conversion so we need to promote. | |||
20500 | if (SrcVT == MVT::i16 && (UseSSEReg || VT == MVT::f128)) { | |||
20501 | SDValue Ext = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i32, Src); | |||
20502 | if (IsStrict) | |||
20503 | return DAG.getNode(ISD::STRICT_SINT_TO_FP, dl, {VT, MVT::Other}, | |||
20504 | {Chain, Ext}); | |||
20505 | ||||
20506 | return DAG.getNode(ISD::SINT_TO_FP, dl, VT, Ext); | |||
20507 | } | |||
20508 | ||||
20509 | if (VT == MVT::f128) | |||
20510 | return SDValue(); | |||
20511 | ||||
20512 | SDValue ValueToStore = Src; | |||
20513 | if (SrcVT == MVT::i64 && Subtarget.hasSSE2() && !Subtarget.is64Bit()) | |||
20514 | // Bitcasting to f64 here allows us to do a single 64-bit store from | |||
20515 | // an SSE register, avoiding the store forwarding penalty that would come | |||
20516 | // with two 32-bit stores. | |||
20517 | ValueToStore = DAG.getBitcast(MVT::f64, ValueToStore); | |||
20518 | ||||
20519 | unsigned Size = SrcVT.getStoreSize(); | |||
20520 | Align Alignment(Size); | |||
20521 | MachineFunction &MF = DAG.getMachineFunction(); | |||
20522 | auto PtrVT = getPointerTy(MF.getDataLayout()); | |||
20523 | int SSFI = MF.getFrameInfo().CreateStackObject(Size, Alignment, false); | |||
20524 | MachinePointerInfo MPI = | |||
20525 | MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SSFI); | |||
20526 | SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT); | |||
20527 | Chain = DAG.getStore(Chain, dl, ValueToStore, StackSlot, MPI, Alignment); | |||
20528 | std::pair<SDValue, SDValue> Tmp = | |||
20529 | BuildFILD(VT, SrcVT, dl, Chain, StackSlot, MPI, Alignment, DAG); | |||
20530 | ||||
20531 | if (IsStrict) | |||
20532 | return DAG.getMergeValues({Tmp.first, Tmp.second}, dl); | |||
20533 | ||||
20534 | return Tmp.first; | |||
20535 | } | |||
20536 | ||||
20537 | std::pair<SDValue, SDValue> X86TargetLowering::BuildFILD( | |||
20538 | EVT DstVT, EVT SrcVT, const SDLoc &DL, SDValue Chain, SDValue Pointer, | |||
20539 | MachinePointerInfo PtrInfo, Align Alignment, SelectionDAG &DAG) const { | |||
20540 | // Build the FILD | |||
20541 | SDVTList Tys; | |||
20542 | bool useSSE = isScalarFPTypeInSSEReg(DstVT); | |||
20543 | if (useSSE) | |||
20544 | Tys = DAG.getVTList(MVT::f80, MVT::Other); | |||
20545 | else | |||
20546 | Tys = DAG.getVTList(DstVT, MVT::Other); | |||
20547 | ||||
20548 | SDValue FILDOps[] = {Chain, Pointer}; | |||
20549 | SDValue Result = | |||
20550 | DAG.getMemIntrinsicNode(X86ISD::FILD, DL, Tys, FILDOps, SrcVT, PtrInfo, | |||
20551 | Alignment, MachineMemOperand::MOLoad); | |||
20552 | Chain = Result.getValue(1); | |||
20553 | ||||
20554 | if (useSSE) { | |||
20555 | MachineFunction &MF = DAG.getMachineFunction(); | |||
20556 | unsigned SSFISize = DstVT.getStoreSize(); | |||
20557 | int SSFI = | |||
20558 | MF.getFrameInfo().CreateStackObject(SSFISize, Align(SSFISize), false); | |||
20559 | auto PtrVT = getPointerTy(MF.getDataLayout()); | |||
20560 | SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT); | |||
20561 | Tys = DAG.getVTList(MVT::Other); | |||
20562 | SDValue FSTOps[] = {Chain, Result, StackSlot}; | |||
20563 | MachineMemOperand *StoreMMO = DAG.getMachineFunction().getMachineMemOperand( | |||
20564 | MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SSFI), | |||
20565 | MachineMemOperand::MOStore, SSFISize, Align(SSFISize)); | |||
20566 | ||||
20567 | Chain = | |||
20568 | DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys, FSTOps, DstVT, StoreMMO); | |||
20569 | Result = DAG.getLoad( | |||
20570 | DstVT, DL, Chain, StackSlot, | |||
20571 | MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SSFI)); | |||
20572 | Chain = Result.getValue(1); | |||
20573 | } | |||
20574 | ||||
20575 | return { Result, Chain }; | |||
20576 | } | |||
20577 | ||||
20578 | /// Horizontal vector math instructions may be slower than normal math with | |||
20579 | /// shuffles. Limit horizontal op codegen based on size/speed trade-offs, uarch | |||
20580 | /// implementation, and likely shuffle complexity of the alternate sequence. | |||
20581 | static bool shouldUseHorizontalOp(bool IsSingleSource, SelectionDAG &DAG, | |||
20582 | const X86Subtarget &Subtarget) { | |||
20583 | bool IsOptimizingSize = DAG.shouldOptForSize(); | |||
20584 | bool HasFastHOps = Subtarget.hasFastHorizontalOps(); | |||
20585 | return !IsSingleSource || IsOptimizingSize || HasFastHOps; | |||
20586 | } | |||
20587 | ||||
20588 | /// 64-bit unsigned integer to double expansion. | |||
20589 | static SDValue LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG, | |||
20590 | const X86Subtarget &Subtarget) { | |||
20591 | // We can't use this algorithm for strict fp. It produces -0.0 instead of +0.0 | |||
20592 | // when converting 0 when rounding toward negative infinity. Caller will | |||
20593 | // fall back to Expand for when i64 or is legal or use FILD in 32-bit mode. | |||
20594 | assert(!Op->isStrictFPOpcode() && "Expected non-strict uint_to_fp!")(static_cast <bool> (!Op->isStrictFPOpcode() && "Expected non-strict uint_to_fp!") ? void (0) : __assert_fail ("!Op->isStrictFPOpcode() && \"Expected non-strict uint_to_fp!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20594, __extension__ __PRETTY_FUNCTION__)); | |||
20595 | // This algorithm is not obvious. Here it is what we're trying to output: | |||
20596 | /* | |||
20597 | movq %rax, %xmm0 | |||
20598 | punpckldq (c0), %xmm0 // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U } | |||
20599 | subpd (c1), %xmm0 // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 } | |||
20600 | #ifdef __SSE3__ | |||
20601 | haddpd %xmm0, %xmm0 | |||
20602 | #else | |||
20603 | pshufd $0x4e, %xmm0, %xmm1 | |||
20604 | addpd %xmm1, %xmm0 | |||
20605 | #endif | |||
20606 | */ | |||
20607 | ||||
20608 | SDLoc dl(Op); | |||
20609 | LLVMContext *Context = DAG.getContext(); | |||
20610 | ||||
20611 | // Build some magic constants. | |||
20612 | static const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 }; | |||
20613 | Constant *C0 = ConstantDataVector::get(*Context, CV0); | |||
20614 | auto PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout()); | |||
20615 | SDValue CPIdx0 = DAG.getConstantPool(C0, PtrVT, Align(16)); | |||
20616 | ||||
20617 | SmallVector<Constant*,2> CV1; | |||
20618 | CV1.push_back( | |||
20619 | ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble(), | |||
20620 | APInt(64, 0x4330000000000000ULL)))); | |||
20621 | CV1.push_back( | |||
20622 | ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble(), | |||
20623 | APInt(64, 0x4530000000000000ULL)))); | |||
20624 | Constant *C1 = ConstantVector::get(CV1); | |||
20625 | SDValue CPIdx1 = DAG.getConstantPool(C1, PtrVT, Align(16)); | |||
20626 | ||||
20627 | // Load the 64-bit value into an XMM register. | |||
20628 | SDValue XR1 = | |||
20629 | DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Op.getOperand(0)); | |||
20630 | SDValue CLod0 = DAG.getLoad( | |||
20631 | MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0, | |||
20632 | MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), Align(16)); | |||
20633 | SDValue Unpck1 = | |||
20634 | getUnpackl(DAG, dl, MVT::v4i32, DAG.getBitcast(MVT::v4i32, XR1), CLod0); | |||
20635 | ||||
20636 | SDValue CLod1 = DAG.getLoad( | |||
20637 | MVT::v2f64, dl, CLod0.getValue(1), CPIdx1, | |||
20638 | MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), Align(16)); | |||
20639 | SDValue XR2F = DAG.getBitcast(MVT::v2f64, Unpck1); | |||
20640 | // TODO: Are there any fast-math-flags to propagate here? | |||
20641 | SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1); | |||
20642 | SDValue Result; | |||
20643 | ||||
20644 | if (Subtarget.hasSSE3() && | |||
20645 | shouldUseHorizontalOp(true, DAG, Subtarget)) { | |||
20646 | Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub); | |||
20647 | } else { | |||
20648 | SDValue Shuffle = DAG.getVectorShuffle(MVT::v2f64, dl, Sub, Sub, {1,-1}); | |||
20649 | Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuffle, Sub); | |||
20650 | } | |||
20651 | Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result, | |||
20652 | DAG.getIntPtrConstant(0, dl)); | |||
20653 | return Result; | |||
20654 | } | |||
20655 | ||||
20656 | /// 32-bit unsigned integer to float expansion. | |||
20657 | static SDValue LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG, | |||
20658 | const X86Subtarget &Subtarget) { | |||
20659 | unsigned OpNo = Op.getNode()->isStrictFPOpcode() ? 1 : 0; | |||
20660 | SDLoc dl(Op); | |||
20661 | // FP constant to bias correct the final result. | |||
20662 | SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL), dl, | |||
20663 | MVT::f64); | |||
20664 | ||||
20665 | // Load the 32-bit value into an XMM register. | |||
20666 | SDValue Load = | |||
20667 | DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Op.getOperand(OpNo)); | |||
20668 | ||||
20669 | // Zero out the upper parts of the register. | |||
20670 | Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG); | |||
20671 | ||||
20672 | // Or the load with the bias. | |||
20673 | SDValue Or = DAG.getNode( | |||
20674 | ISD::OR, dl, MVT::v2i64, | |||
20675 | DAG.getBitcast(MVT::v2i64, Load), | |||
20676 | DAG.getBitcast(MVT::v2i64, | |||
20677 | DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, Bias))); | |||
20678 | Or = | |||
20679 | DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, | |||
20680 | DAG.getBitcast(MVT::v2f64, Or), DAG.getIntPtrConstant(0, dl)); | |||
20681 | ||||
20682 | if (Op.getNode()->isStrictFPOpcode()) { | |||
20683 | // Subtract the bias. | |||
20684 | // TODO: Are there any fast-math-flags to propagate here? | |||
20685 | SDValue Chain = Op.getOperand(0); | |||
20686 | SDValue Sub = DAG.getNode(ISD::STRICT_FSUB, dl, {MVT::f64, MVT::Other}, | |||
20687 | {Chain, Or, Bias}); | |||
20688 | ||||
20689 | if (Op.getValueType() == Sub.getValueType()) | |||
20690 | return Sub; | |||
20691 | ||||
20692 | // Handle final rounding. | |||
20693 | std::pair<SDValue, SDValue> ResultPair = DAG.getStrictFPExtendOrRound( | |||
20694 | Sub, Sub.getValue(1), dl, Op.getSimpleValueType()); | |||
20695 | ||||
20696 | return DAG.getMergeValues({ResultPair.first, ResultPair.second}, dl); | |||
20697 | } | |||
20698 | ||||
20699 | // Subtract the bias. | |||
20700 | // TODO: Are there any fast-math-flags to propagate here? | |||
20701 | SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias); | |||
20702 | ||||
20703 | // Handle final rounding. | |||
20704 | return DAG.getFPExtendOrRound(Sub, dl, Op.getSimpleValueType()); | |||
20705 | } | |||
20706 | ||||
20707 | static SDValue lowerUINT_TO_FP_v2i32(SDValue Op, SelectionDAG &DAG, | |||
20708 | const X86Subtarget &Subtarget, | |||
20709 | const SDLoc &DL) { | |||
20710 | if (Op.getSimpleValueType() != MVT::v2f64) | |||
20711 | return SDValue(); | |||
20712 | ||||
20713 | bool IsStrict = Op->isStrictFPOpcode(); | |||
20714 | ||||
20715 | SDValue N0 = Op.getOperand(IsStrict ? 1 : 0); | |||
20716 | assert(N0.getSimpleValueType() == MVT::v2i32 && "Unexpected input type")(static_cast <bool> (N0.getSimpleValueType() == MVT::v2i32 && "Unexpected input type") ? void (0) : __assert_fail ("N0.getSimpleValueType() == MVT::v2i32 && \"Unexpected input type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20716, __extension__ __PRETTY_FUNCTION__)); | |||
20717 | ||||
20718 | if (Subtarget.hasAVX512()) { | |||
20719 | if (!Subtarget.hasVLX()) { | |||
20720 | // Let generic type legalization widen this. | |||
20721 | if (!IsStrict) | |||
20722 | return SDValue(); | |||
20723 | // Otherwise pad the integer input with 0s and widen the operation. | |||
20724 | N0 = DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v4i32, N0, | |||
20725 | DAG.getConstant(0, DL, MVT::v2i32)); | |||
20726 | SDValue Res = DAG.getNode(Op->getOpcode(), DL, {MVT::v4f64, MVT::Other}, | |||
20727 | {Op.getOperand(0), N0}); | |||
20728 | SDValue Chain = Res.getValue(1); | |||
20729 | Res = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2f64, Res, | |||
20730 | DAG.getIntPtrConstant(0, DL)); | |||
20731 | return DAG.getMergeValues({Res, Chain}, DL); | |||
20732 | } | |||
20733 | ||||
20734 | // Legalize to v4i32 type. | |||
20735 | N0 = DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v4i32, N0, | |||
20736 | DAG.getUNDEF(MVT::v2i32)); | |||
20737 | if (IsStrict) | |||
20738 | return DAG.getNode(X86ISD::STRICT_CVTUI2P, DL, {MVT::v2f64, MVT::Other}, | |||
20739 | {Op.getOperand(0), N0}); | |||
20740 | return DAG.getNode(X86ISD::CVTUI2P, DL, MVT::v2f64, N0); | |||
20741 | } | |||
20742 | ||||
20743 | // Zero extend to 2i64, OR with the floating point representation of 2^52. | |||
20744 | // This gives us the floating point equivalent of 2^52 + the i32 integer | |||
20745 | // since double has 52-bits of mantissa. Then subtract 2^52 in floating | |||
20746 | // point leaving just our i32 integers in double format. | |||
20747 | SDValue ZExtIn = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v2i64, N0); | |||
20748 | SDValue VBias = | |||
20749 | DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL), DL, MVT::v2f64); | |||
20750 | SDValue Or = DAG.getNode(ISD::OR, DL, MVT::v2i64, ZExtIn, | |||
20751 | DAG.getBitcast(MVT::v2i64, VBias)); | |||
20752 | Or = DAG.getBitcast(MVT::v2f64, Or); | |||
20753 | ||||
20754 | if (IsStrict) | |||
20755 | return DAG.getNode(ISD::STRICT_FSUB, DL, {MVT::v2f64, MVT::Other}, | |||
20756 | {Op.getOperand(0), Or, VBias}); | |||
20757 | return DAG.getNode(ISD::FSUB, DL, MVT::v2f64, Or, VBias); | |||
20758 | } | |||
20759 | ||||
20760 | static SDValue lowerUINT_TO_FP_vXi32(SDValue Op, SelectionDAG &DAG, | |||
20761 | const X86Subtarget &Subtarget) { | |||
20762 | SDLoc DL(Op); | |||
20763 | bool IsStrict = Op->isStrictFPOpcode(); | |||
20764 | SDValue V = Op->getOperand(IsStrict ? 1 : 0); | |||
20765 | MVT VecIntVT = V.getSimpleValueType(); | |||
20766 | assert((VecIntVT == MVT::v4i32 || VecIntVT == MVT::v8i32) &&(static_cast <bool> ((VecIntVT == MVT::v4i32 || VecIntVT == MVT::v8i32) && "Unsupported custom type") ? void ( 0) : __assert_fail ("(VecIntVT == MVT::v4i32 || VecIntVT == MVT::v8i32) && \"Unsupported custom type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20767, __extension__ __PRETTY_FUNCTION__)) | |||
20767 | "Unsupported custom type")(static_cast <bool> ((VecIntVT == MVT::v4i32 || VecIntVT == MVT::v8i32) && "Unsupported custom type") ? void ( 0) : __assert_fail ("(VecIntVT == MVT::v4i32 || VecIntVT == MVT::v8i32) && \"Unsupported custom type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20767, __extension__ __PRETTY_FUNCTION__)); | |||
20768 | ||||
20769 | if (Subtarget.hasAVX512()) { | |||
20770 | // With AVX512, but not VLX we need to widen to get a 512-bit result type. | |||
20771 | assert(!Subtarget.hasVLX() && "Unexpected features")(static_cast <bool> (!Subtarget.hasVLX() && "Unexpected features" ) ? void (0) : __assert_fail ("!Subtarget.hasVLX() && \"Unexpected features\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20771, __extension__ __PRETTY_FUNCTION__)); | |||
20772 | MVT VT = Op->getSimpleValueType(0); | |||
20773 | ||||
20774 | // v8i32->v8f64 is legal with AVX512 so just return it. | |||
20775 | if (VT == MVT::v8f64) | |||
20776 | return Op; | |||
20777 | ||||
20778 | assert((VT == MVT::v4f32 || VT == MVT::v8f32 || VT == MVT::v4f64) &&(static_cast <bool> ((VT == MVT::v4f32 || VT == MVT::v8f32 || VT == MVT::v4f64) && "Unexpected VT!") ? void (0) : __assert_fail ("(VT == MVT::v4f32 || VT == MVT::v8f32 || VT == MVT::v4f64) && \"Unexpected VT!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20779, __extension__ __PRETTY_FUNCTION__)) | |||
20779 | "Unexpected VT!")(static_cast <bool> ((VT == MVT::v4f32 || VT == MVT::v8f32 || VT == MVT::v4f64) && "Unexpected VT!") ? void (0) : __assert_fail ("(VT == MVT::v4f32 || VT == MVT::v8f32 || VT == MVT::v4f64) && \"Unexpected VT!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20779, __extension__ __PRETTY_FUNCTION__)); | |||
20780 | MVT WideVT = VT == MVT::v4f64 ? MVT::v8f64 : MVT::v16f32; | |||
20781 | MVT WideIntVT = VT == MVT::v4f64 ? MVT::v8i32 : MVT::v16i32; | |||
20782 | // Need to concat with zero vector for strict fp to avoid spurious | |||
20783 | // exceptions. | |||
20784 | SDValue Tmp = | |||
20785 | IsStrict ? DAG.getConstant(0, DL, WideIntVT) : DAG.getUNDEF(WideIntVT); | |||
20786 | V = DAG.getNode(ISD::INSERT_SUBVECTOR, DL, WideIntVT, Tmp, V, | |||
20787 | DAG.getIntPtrConstant(0, DL)); | |||
20788 | SDValue Res, Chain; | |||
20789 | if (IsStrict) { | |||
20790 | Res = DAG.getNode(ISD::STRICT_UINT_TO_FP, DL, {WideVT, MVT::Other}, | |||
20791 | {Op->getOperand(0), V}); | |||
20792 | Chain = Res.getValue(1); | |||
20793 | } else { | |||
20794 | Res = DAG.getNode(ISD::UINT_TO_FP, DL, WideVT, V); | |||
20795 | } | |||
20796 | ||||
20797 | Res = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Res, | |||
20798 | DAG.getIntPtrConstant(0, DL)); | |||
20799 | ||||
20800 | if (IsStrict) | |||
20801 | return DAG.getMergeValues({Res, Chain}, DL); | |||
20802 | return Res; | |||
20803 | } | |||
20804 | ||||
20805 | if (Subtarget.hasAVX() && VecIntVT == MVT::v4i32 && | |||
20806 | Op->getSimpleValueType(0) == MVT::v4f64) { | |||
20807 | SDValue ZExtIn = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::v4i64, V); | |||
20808 | Constant *Bias = ConstantFP::get( | |||
20809 | *DAG.getContext(), | |||
20810 | APFloat(APFloat::IEEEdouble(), APInt(64, 0x4330000000000000ULL))); | |||
20811 | auto PtrVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout()); | |||
20812 | SDValue CPIdx = DAG.getConstantPool(Bias, PtrVT, Align(8)); | |||
20813 | SDVTList Tys = DAG.getVTList(MVT::v4f64, MVT::Other); | |||
20814 | SDValue Ops[] = {DAG.getEntryNode(), CPIdx}; | |||
20815 | SDValue VBias = DAG.getMemIntrinsicNode( | |||
20816 | X86ISD::VBROADCAST_LOAD, DL, Tys, Ops, MVT::f64, | |||
20817 | MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), Align(8), | |||
20818 | MachineMemOperand::MOLoad); | |||
20819 | ||||
20820 | SDValue Or = DAG.getNode(ISD::OR, DL, MVT::v4i64, ZExtIn, | |||
20821 | DAG.getBitcast(MVT::v4i64, VBias)); | |||
20822 | Or = DAG.getBitcast(MVT::v4f64, Or); | |||
20823 | ||||
20824 | if (IsStrict) | |||
20825 | return DAG.getNode(ISD::STRICT_FSUB, DL, {MVT::v4f64, MVT::Other}, | |||
20826 | {Op.getOperand(0), Or, VBias}); | |||
20827 | return DAG.getNode(ISD::FSUB, DL, MVT::v4f64, Or, VBias); | |||
20828 | } | |||
20829 | ||||
20830 | // The algorithm is the following: | |||
20831 | // #ifdef __SSE4_1__ | |||
20832 | // uint4 lo = _mm_blend_epi16( v, (uint4) 0x4b000000, 0xaa); | |||
20833 | // uint4 hi = _mm_blend_epi16( _mm_srli_epi32(v,16), | |||
20834 | // (uint4) 0x53000000, 0xaa); | |||
20835 | // #else | |||
20836 | // uint4 lo = (v & (uint4) 0xffff) | (uint4) 0x4b000000; | |||
20837 | // uint4 hi = (v >> 16) | (uint4) 0x53000000; | |||
20838 | // #endif | |||
20839 | // float4 fhi = (float4) hi - (0x1.0p39f + 0x1.0p23f); | |||
20840 | // return (float4) lo + fhi; | |||
20841 | ||||
20842 | bool Is128 = VecIntVT == MVT::v4i32; | |||
20843 | MVT VecFloatVT = Is128 ? MVT::v4f32 : MVT::v8f32; | |||
20844 | // If we convert to something else than the supported type, e.g., to v4f64, | |||
20845 | // abort early. | |||
20846 | if (VecFloatVT != Op->getSimpleValueType(0)) | |||
20847 | return SDValue(); | |||
20848 | ||||
20849 | // In the #idef/#else code, we have in common: | |||
20850 | // - The vector of constants: | |||
20851 | // -- 0x4b000000 | |||
20852 | // -- 0x53000000 | |||
20853 | // - A shift: | |||
20854 | // -- v >> 16 | |||
20855 | ||||
20856 | // Create the splat vector for 0x4b000000. | |||
20857 | SDValue VecCstLow = DAG.getConstant(0x4b000000, DL, VecIntVT); | |||
20858 | // Create the splat vector for 0x53000000. | |||
20859 | SDValue VecCstHigh = DAG.getConstant(0x53000000, DL, VecIntVT); | |||
20860 | ||||
20861 | // Create the right shift. | |||
20862 | SDValue VecCstShift = DAG.getConstant(16, DL, VecIntVT); | |||
20863 | SDValue HighShift = DAG.getNode(ISD::SRL, DL, VecIntVT, V, VecCstShift); | |||
20864 | ||||
20865 | SDValue Low, High; | |||
20866 | if (Subtarget.hasSSE41()) { | |||
20867 | MVT VecI16VT = Is128 ? MVT::v8i16 : MVT::v16i16; | |||
20868 | // uint4 lo = _mm_blend_epi16( v, (uint4) 0x4b000000, 0xaa); | |||
20869 | SDValue VecCstLowBitcast = DAG.getBitcast(VecI16VT, VecCstLow); | |||
20870 | SDValue VecBitcast = DAG.getBitcast(VecI16VT, V); | |||
20871 | // Low will be bitcasted right away, so do not bother bitcasting back to its | |||
20872 | // original type. | |||
20873 | Low = DAG.getNode(X86ISD::BLENDI, DL, VecI16VT, VecBitcast, | |||
20874 | VecCstLowBitcast, DAG.getTargetConstant(0xaa, DL, MVT::i8)); | |||
20875 | // uint4 hi = _mm_blend_epi16( _mm_srli_epi32(v,16), | |||
20876 | // (uint4) 0x53000000, 0xaa); | |||
20877 | SDValue VecCstHighBitcast = DAG.getBitcast(VecI16VT, VecCstHigh); | |||
20878 | SDValue VecShiftBitcast = DAG.getBitcast(VecI16VT, HighShift); | |||
20879 | // High will be bitcasted right away, so do not bother bitcasting back to | |||
20880 | // its original type. | |||
20881 | High = DAG.getNode(X86ISD::BLENDI, DL, VecI16VT, VecShiftBitcast, | |||
20882 | VecCstHighBitcast, DAG.getTargetConstant(0xaa, DL, MVT::i8)); | |||
20883 | } else { | |||
20884 | SDValue VecCstMask = DAG.getConstant(0xffff, DL, VecIntVT); | |||
20885 | // uint4 lo = (v & (uint4) 0xffff) | (uint4) 0x4b000000; | |||
20886 | SDValue LowAnd = DAG.getNode(ISD::AND, DL, VecIntVT, V, VecCstMask); | |||
20887 | Low = DAG.getNode(ISD::OR, DL, VecIntVT, LowAnd, VecCstLow); | |||
20888 | ||||
20889 | // uint4 hi = (v >> 16) | (uint4) 0x53000000; | |||
20890 | High = DAG.getNode(ISD::OR, DL, VecIntVT, HighShift, VecCstHigh); | |||
20891 | } | |||
20892 | ||||
20893 | // Create the vector constant for (0x1.0p39f + 0x1.0p23f). | |||
20894 | SDValue VecCstFSub = DAG.getConstantFP( | |||
20895 | APFloat(APFloat::IEEEsingle(), APInt(32, 0x53000080)), DL, VecFloatVT); | |||
20896 | ||||
20897 | // float4 fhi = (float4) hi - (0x1.0p39f + 0x1.0p23f); | |||
20898 | // NOTE: By using fsub of a positive constant instead of fadd of a negative | |||
20899 | // constant, we avoid reassociation in MachineCombiner when unsafe-fp-math is | |||
20900 | // enabled. See PR24512. | |||
20901 | SDValue HighBitcast = DAG.getBitcast(VecFloatVT, High); | |||
20902 | // TODO: Are there any fast-math-flags to propagate here? | |||
20903 | // (float4) lo; | |||
20904 | SDValue LowBitcast = DAG.getBitcast(VecFloatVT, Low); | |||
20905 | // return (float4) lo + fhi; | |||
20906 | if (IsStrict) { | |||
20907 | SDValue FHigh = DAG.getNode(ISD::STRICT_FSUB, DL, {VecFloatVT, MVT::Other}, | |||
20908 | {Op.getOperand(0), HighBitcast, VecCstFSub}); | |||
20909 | return DAG.getNode(ISD::STRICT_FADD, DL, {VecFloatVT, MVT::Other}, | |||
20910 | {FHigh.getValue(1), LowBitcast, FHigh}); | |||
20911 | } | |||
20912 | ||||
20913 | SDValue FHigh = | |||
20914 | DAG.getNode(ISD::FSUB, DL, VecFloatVT, HighBitcast, VecCstFSub); | |||
20915 | return DAG.getNode(ISD::FADD, DL, VecFloatVT, LowBitcast, FHigh); | |||
20916 | } | |||
20917 | ||||
20918 | static SDValue lowerUINT_TO_FP_vec(SDValue Op, SelectionDAG &DAG, | |||
20919 | const X86Subtarget &Subtarget) { | |||
20920 | unsigned OpNo = Op.getNode()->isStrictFPOpcode() ? 1 : 0; | |||
20921 | SDValue N0 = Op.getOperand(OpNo); | |||
20922 | MVT SrcVT = N0.getSimpleValueType(); | |||
20923 | SDLoc dl(Op); | |||
20924 | ||||
20925 | switch (SrcVT.SimpleTy) { | |||
20926 | default: | |||
20927 | llvm_unreachable("Custom UINT_TO_FP is not supported!")::llvm::llvm_unreachable_internal("Custom UINT_TO_FP is not supported!" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 20927); | |||
20928 | case MVT::v2i32: | |||
20929 | return lowerUINT_TO_FP_v2i32(Op, DAG, Subtarget, dl); | |||
20930 | case MVT::v4i32: | |||
20931 | case MVT::v8i32: | |||
20932 | return lowerUINT_TO_FP_vXi32(Op, DAG, Subtarget); | |||
20933 | case MVT::v2i64: | |||
20934 | case MVT::v4i64: | |||
20935 | return lowerINT_TO_FP_vXi64(Op, DAG, Subtarget); | |||
20936 | } | |||
20937 | } | |||
20938 | ||||
20939 | SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, | |||
20940 | SelectionDAG &DAG) const { | |||
20941 | bool IsStrict = Op->isStrictFPOpcode(); | |||
20942 | unsigned OpNo = IsStrict ? 1 : 0; | |||
20943 | SDValue Src = Op.getOperand(OpNo); | |||
20944 | SDLoc dl(Op); | |||
20945 | auto PtrVT = getPointerTy(DAG.getDataLayout()); | |||
20946 | MVT SrcVT = Src.getSimpleValueType(); | |||
20947 | MVT DstVT = Op->getSimpleValueType(0); | |||
20948 | SDValue Chain = IsStrict ? Op.getOperand(0) : DAG.getEntryNode(); | |||
20949 | ||||
20950 | if (DstVT == MVT::f128) | |||
20951 | return SDValue(); | |||
20952 | ||||
20953 | if (DstVT.isVector()) | |||
20954 | return lowerUINT_TO_FP_vec(Op, DAG, Subtarget); | |||
20955 | ||||
20956 | if (Subtarget.isTargetWin64() && SrcVT == MVT::i128) | |||
20957 | return LowerWin64_INT128_TO_FP(Op, DAG); | |||
20958 | ||||
20959 | if (SDValue Extract = vectorizeExtractedCast(Op, DAG, Subtarget)) | |||
20960 | return Extract; | |||
20961 | ||||
20962 | if (Subtarget.hasAVX512() && isScalarFPTypeInSSEReg(DstVT) && | |||
20963 | (SrcVT == MVT::i32 || (SrcVT == MVT::i64 && Subtarget.is64Bit()))) { | |||
20964 | // Conversions from unsigned i32 to f32/f64 are legal, | |||
20965 | // using VCVTUSI2SS/SD. Same for i64 in 64-bit mode. | |||
20966 | return Op; | |||
20967 | } | |||
20968 | ||||
20969 | // Promote i32 to i64 and use a signed conversion on 64-bit targets. | |||
20970 | if (SrcVT == MVT::i32 && Subtarget.is64Bit()) { | |||
20971 | Src = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, Src); | |||
20972 | if (IsStrict) | |||
20973 | return DAG.getNode(ISD::STRICT_SINT_TO_FP, dl, {DstVT, MVT::Other}, | |||
20974 | {Chain, Src}); | |||
20975 | return DAG.getNode(ISD::SINT_TO_FP, dl, DstVT, Src); | |||
20976 | } | |||
20977 | ||||
20978 | if (SDValue V = LowerI64IntToFP_AVX512DQ(Op, DAG, Subtarget)) | |||
20979 | return V; | |||
20980 | if (SDValue V = LowerI64IntToFP16(Op, DAG, Subtarget)) | |||
20981 | return V; | |||
20982 | ||||
20983 | // The transform for i64->f64 isn't correct for 0 when rounding to negative | |||
20984 | // infinity. It produces -0.0, so disable under strictfp. | |||
20985 | if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64 && !IsStrict) | |||
20986 | return LowerUINT_TO_FP_i64(Op, DAG, Subtarget); | |||
20987 | if (SrcVT == MVT::i32 && X86ScalarSSEf64 && DstVT != MVT::f80) | |||
20988 | return LowerUINT_TO_FP_i32(Op, DAG, Subtarget); | |||
20989 | if (Subtarget.is64Bit() && SrcVT == MVT::i64 && | |||
20990 | (DstVT == MVT::f32 || DstVT == MVT::f64)) | |||
20991 | return SDValue(); | |||
20992 | ||||
20993 | // Make a 64-bit buffer, and use it to build an FILD. | |||
20994 | SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64, 8); | |||
20995 | int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex(); | |||
20996 | Align SlotAlign(8); | |||
20997 | MachinePointerInfo MPI = | |||
20998 | MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SSFI); | |||
20999 | if (SrcVT == MVT::i32) { | |||
21000 | SDValue OffsetSlot = | |||
21001 | DAG.getMemBasePlusOffset(StackSlot, TypeSize::Fixed(4), dl); | |||
21002 | SDValue Store1 = DAG.getStore(Chain, dl, Src, StackSlot, MPI, SlotAlign); | |||
21003 | SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, dl, MVT::i32), | |||
21004 | OffsetSlot, MPI.getWithOffset(4), SlotAlign); | |||
21005 | std::pair<SDValue, SDValue> Tmp = | |||
21006 | BuildFILD(DstVT, MVT::i64, dl, Store2, StackSlot, MPI, SlotAlign, DAG); | |||
21007 | if (IsStrict) | |||
21008 | return DAG.getMergeValues({Tmp.first, Tmp.second}, dl); | |||
21009 | ||||
21010 | return Tmp.first; | |||
21011 | } | |||
21012 | ||||
21013 | assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP")(static_cast <bool> (SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP" ) ? void (0) : __assert_fail ("SrcVT == MVT::i64 && \"Unexpected type in UINT_TO_FP\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21013, __extension__ __PRETTY_FUNCTION__)); | |||
21014 | SDValue ValueToStore = Src; | |||
21015 | if (isScalarFPTypeInSSEReg(Op.getValueType()) && !Subtarget.is64Bit()) { | |||
21016 | // Bitcasting to f64 here allows us to do a single 64-bit store from | |||
21017 | // an SSE register, avoiding the store forwarding penalty that would come | |||
21018 | // with two 32-bit stores. | |||
21019 | ValueToStore = DAG.getBitcast(MVT::f64, ValueToStore); | |||
21020 | } | |||
21021 | SDValue Store = | |||
21022 | DAG.getStore(Chain, dl, ValueToStore, StackSlot, MPI, SlotAlign); | |||
21023 | // For i64 source, we need to add the appropriate power of 2 if the input | |||
21024 | // was negative. We must be careful to do the computation in x87 extended | |||
21025 | // precision, not in SSE. | |||
21026 | SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other); | |||
21027 | SDValue Ops[] = { Store, StackSlot }; | |||
21028 | SDValue Fild = | |||
21029 | DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops, MVT::i64, MPI, | |||
21030 | SlotAlign, MachineMemOperand::MOLoad); | |||
21031 | Chain = Fild.getValue(1); | |||
21032 | ||||
21033 | ||||
21034 | // Check whether the sign bit is set. | |||
21035 | SDValue SignSet = DAG.getSetCC( | |||
21036 | dl, getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::i64), | |||
21037 | Op.getOperand(OpNo), DAG.getConstant(0, dl, MVT::i64), ISD::SETLT); | |||
21038 | ||||
21039 | // Build a 64 bit pair (FF, 0) in the constant pool, with FF in the hi bits. | |||
21040 | APInt FF(64, 0x5F80000000000000ULL); | |||
21041 | SDValue FudgePtr = DAG.getConstantPool( | |||
21042 | ConstantInt::get(*DAG.getContext(), FF), PtrVT); | |||
21043 | Align CPAlignment = cast<ConstantPoolSDNode>(FudgePtr)->getAlign(); | |||
21044 | ||||
21045 | // Get a pointer to FF if the sign bit was set, or to 0 otherwise. | |||
21046 | SDValue Zero = DAG.getIntPtrConstant(0, dl); | |||
21047 | SDValue Four = DAG.getIntPtrConstant(4, dl); | |||
21048 | SDValue Offset = DAG.getSelect(dl, Zero.getValueType(), SignSet, Four, Zero); | |||
21049 | FudgePtr = DAG.getNode(ISD::ADD, dl, PtrVT, FudgePtr, Offset); | |||
21050 | ||||
21051 | // Load the value out, extending it from f32 to f80. | |||
21052 | SDValue Fudge = DAG.getExtLoad( | |||
21053 | ISD::EXTLOAD, dl, MVT::f80, Chain, FudgePtr, | |||
21054 | MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), MVT::f32, | |||
21055 | CPAlignment); | |||
21056 | Chain = Fudge.getValue(1); | |||
21057 | // Extend everything to 80 bits to force it to be done on x87. | |||
21058 | // TODO: Are there any fast-math-flags to propagate here? | |||
21059 | if (IsStrict) { | |||
21060 | SDValue Add = DAG.getNode(ISD::STRICT_FADD, dl, {MVT::f80, MVT::Other}, | |||
21061 | {Chain, Fild, Fudge}); | |||
21062 | // STRICT_FP_ROUND can't handle equal types. | |||
21063 | if (DstVT == MVT::f80) | |||
21064 | return Add; | |||
21065 | return DAG.getNode(ISD::STRICT_FP_ROUND, dl, {DstVT, MVT::Other}, | |||
21066 | {Add.getValue(1), Add, DAG.getIntPtrConstant(0, dl)}); | |||
21067 | } | |||
21068 | SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge); | |||
21069 | return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add, | |||
21070 | DAG.getIntPtrConstant(0, dl)); | |||
21071 | } | |||
21072 | ||||
21073 | // If the given FP_TO_SINT (IsSigned) or FP_TO_UINT (!IsSigned) operation | |||
21074 | // is legal, or has an fp128 or f16 source (which needs to be promoted to f32), | |||
21075 | // just return an SDValue(). | |||
21076 | // Otherwise it is assumed to be a conversion from one of f32, f64 or f80 | |||
21077 | // to i16, i32 or i64, and we lower it to a legal sequence and return the | |||
21078 | // result. | |||
21079 | SDValue | |||
21080 | X86TargetLowering::FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, | |||
21081 | bool IsSigned, SDValue &Chain) const { | |||
21082 | bool IsStrict = Op->isStrictFPOpcode(); | |||
21083 | SDLoc DL(Op); | |||
21084 | ||||
21085 | EVT DstTy = Op.getValueType(); | |||
21086 | SDValue Value = Op.getOperand(IsStrict ? 1 : 0); | |||
21087 | EVT TheVT = Value.getValueType(); | |||
21088 | auto PtrVT = getPointerTy(DAG.getDataLayout()); | |||
21089 | ||||
21090 | if (TheVT != MVT::f32 && TheVT != MVT::f64 && TheVT != MVT::f80) { | |||
21091 | // f16 must be promoted before using the lowering in this routine. | |||
21092 | // fp128 does not use this lowering. | |||
21093 | return SDValue(); | |||
21094 | } | |||
21095 | ||||
21096 | // If using FIST to compute an unsigned i64, we'll need some fixup | |||
21097 | // to handle values above the maximum signed i64. A FIST is always | |||
21098 | // used for the 32-bit subtarget, but also for f80 on a 64-bit target. | |||
21099 | bool UnsignedFixup = !IsSigned && DstTy == MVT::i64; | |||
21100 | ||||
21101 | // FIXME: This does not generate an invalid exception if the input does not | |||
21102 | // fit in i32. PR44019 | |||
21103 | if (!IsSigned && DstTy != MVT::i64) { | |||
21104 | // Replace the fp-to-uint32 operation with an fp-to-sint64 FIST. | |||
21105 | // The low 32 bits of the fist result will have the correct uint32 result. | |||
21106 | assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT")(static_cast <bool> (DstTy == MVT::i32 && "Unexpected FP_TO_UINT" ) ? void (0) : __assert_fail ("DstTy == MVT::i32 && \"Unexpected FP_TO_UINT\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21106, __extension__ __PRETTY_FUNCTION__)); | |||
21107 | DstTy = MVT::i64; | |||
21108 | } | |||
21109 | ||||
21110 | assert(DstTy.getSimpleVT() <= MVT::i64 &&(static_cast <bool> (DstTy.getSimpleVT() <= MVT::i64 && DstTy.getSimpleVT() >= MVT::i16 && "Unknown FP_TO_INT to lower!" ) ? void (0) : __assert_fail ("DstTy.getSimpleVT() <= MVT::i64 && DstTy.getSimpleVT() >= MVT::i16 && \"Unknown FP_TO_INT to lower!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21112, __extension__ __PRETTY_FUNCTION__)) | |||
21111 | DstTy.getSimpleVT() >= MVT::i16 &&(static_cast <bool> (DstTy.getSimpleVT() <= MVT::i64 && DstTy.getSimpleVT() >= MVT::i16 && "Unknown FP_TO_INT to lower!" ) ? void (0) : __assert_fail ("DstTy.getSimpleVT() <= MVT::i64 && DstTy.getSimpleVT() >= MVT::i16 && \"Unknown FP_TO_INT to lower!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21112, __extension__ __PRETTY_FUNCTION__)) | |||
21112 | "Unknown FP_TO_INT to lower!")(static_cast <bool> (DstTy.getSimpleVT() <= MVT::i64 && DstTy.getSimpleVT() >= MVT::i16 && "Unknown FP_TO_INT to lower!" ) ? void (0) : __assert_fail ("DstTy.getSimpleVT() <= MVT::i64 && DstTy.getSimpleVT() >= MVT::i16 && \"Unknown FP_TO_INT to lower!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21112, __extension__ __PRETTY_FUNCTION__)); | |||
21113 | ||||
21114 | // We lower FP->int64 into FISTP64 followed by a load from a temporary | |||
21115 | // stack slot. | |||
21116 | MachineFunction &MF = DAG.getMachineFunction(); | |||
21117 | unsigned MemSize = DstTy.getStoreSize(); | |||
21118 | int SSFI = | |||
21119 | MF.getFrameInfo().CreateStackObject(MemSize, Align(MemSize), false); | |||
21120 | SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT); | |||
21121 | ||||
21122 | Chain = IsStrict ? Op.getOperand(0) : DAG.getEntryNode(); | |||
21123 | ||||
21124 | SDValue Adjust; // 0x0 or 0x80000000, for result sign bit adjustment. | |||
21125 | ||||
21126 | if (UnsignedFixup) { | |||
21127 | // | |||
21128 | // Conversion to unsigned i64 is implemented with a select, | |||
21129 | // depending on whether the source value fits in the range | |||
21130 | // of a signed i64. Let Thresh be the FP equivalent of | |||
21131 | // 0x8000000000000000ULL. | |||
21132 | // | |||
21133 | // Adjust = (Value >= Thresh) ? 0x80000000 : 0; | |||
21134 | // FltOfs = (Value >= Thresh) ? 0x80000000 : 0; | |||
21135 | // FistSrc = (Value - FltOfs); | |||
21136 | // Fist-to-mem64 FistSrc | |||
21137 | // Add 0 or 0x800...0ULL to the 64-bit result, which is equivalent | |||
21138 | // to XOR'ing the high 32 bits with Adjust. | |||
21139 | // | |||
21140 | // Being a power of 2, Thresh is exactly representable in all FP formats. | |||
21141 | // For X87 we'd like to use the smallest FP type for this constant, but | |||
21142 | // for DAG type consistency we have to match the FP operand type. | |||
21143 | ||||
21144 | APFloat Thresh(APFloat::IEEEsingle(), APInt(32, 0x5f000000)); | |||
21145 | LLVM_ATTRIBUTE_UNUSED__attribute__((__unused__)) APFloat::opStatus Status = APFloat::opOK; | |||
21146 | bool LosesInfo = false; | |||
21147 | if (TheVT == MVT::f64) | |||
21148 | // The rounding mode is irrelevant as the conversion should be exact. | |||
21149 | Status = Thresh.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, | |||
21150 | &LosesInfo); | |||
21151 | else if (TheVT == MVT::f80) | |||
21152 | Status = Thresh.convert(APFloat::x87DoubleExtended(), | |||
21153 | APFloat::rmNearestTiesToEven, &LosesInfo); | |||
21154 | ||||
21155 | assert(Status == APFloat::opOK && !LosesInfo &&(static_cast <bool> (Status == APFloat::opOK && !LosesInfo && "FP conversion should have been exact" ) ? void (0) : __assert_fail ("Status == APFloat::opOK && !LosesInfo && \"FP conversion should have been exact\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21156, __extension__ __PRETTY_FUNCTION__)) | |||
21156 | "FP conversion should have been exact")(static_cast <bool> (Status == APFloat::opOK && !LosesInfo && "FP conversion should have been exact" ) ? void (0) : __assert_fail ("Status == APFloat::opOK && !LosesInfo && \"FP conversion should have been exact\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21156, __extension__ __PRETTY_FUNCTION__)); | |||
21157 | ||||
21158 | SDValue ThreshVal = DAG.getConstantFP(Thresh, DL, TheVT); | |||
21159 | ||||
21160 | EVT ResVT = getSetCCResultType(DAG.getDataLayout(), | |||
21161 | *DAG.getContext(), TheVT); | |||
21162 | SDValue Cmp; | |||
21163 | if (IsStrict) { | |||
21164 | Cmp = DAG.getSetCC(DL, ResVT, Value, ThreshVal, ISD::SETGE, Chain, | |||
21165 | /*IsSignaling*/ true); | |||
21166 | Chain = Cmp.getValue(1); | |||
21167 | } else { | |||
21168 | Cmp = DAG.getSetCC(DL, ResVT, Value, ThreshVal, ISD::SETGE); | |||
21169 | } | |||
21170 | ||||
21171 | // Our preferred lowering of | |||
21172 | // | |||
21173 | // (Value >= Thresh) ? 0x8000000000000000ULL : 0 | |||
21174 | // | |||
21175 | // is | |||
21176 | // | |||
21177 | // (Value >= Thresh) << 63 | |||
21178 | // | |||
21179 | // but since we can get here after LegalOperations, DAGCombine might do the | |||
21180 | // wrong thing if we create a select. So, directly create the preferred | |||
21181 | // version. | |||
21182 | SDValue Zext = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, Cmp); | |||
21183 | SDValue Const63 = DAG.getConstant(63, DL, MVT::i8); | |||
21184 | Adjust = DAG.getNode(ISD::SHL, DL, MVT::i64, Zext, Const63); | |||
21185 | ||||
21186 | SDValue FltOfs = DAG.getSelect(DL, TheVT, Cmp, ThreshVal, | |||
21187 | DAG.getConstantFP(0.0, DL, TheVT)); | |||
21188 | ||||
21189 | if (IsStrict) { | |||
21190 | Value = DAG.getNode(ISD::STRICT_FSUB, DL, { TheVT, MVT::Other}, | |||
21191 | { Chain, Value, FltOfs }); | |||
21192 | Chain = Value.getValue(1); | |||
21193 | } else | |||
21194 | Value = DAG.getNode(ISD::FSUB, DL, TheVT, Value, FltOfs); | |||
21195 | } | |||
21196 | ||||
21197 | MachinePointerInfo MPI = MachinePointerInfo::getFixedStack(MF, SSFI); | |||
21198 | ||||
21199 | // FIXME This causes a redundant load/store if the SSE-class value is already | |||
21200 | // in memory, such as if it is on the callstack. | |||
21201 | if (isScalarFPTypeInSSEReg(TheVT)) { | |||
21202 | assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!")(static_cast <bool> (DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!" ) ? void (0) : __assert_fail ("DstTy == MVT::i64 && \"Invalid FP_TO_SINT to lower!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21202, __extension__ __PRETTY_FUNCTION__)); | |||
21203 | Chain = DAG.getStore(Chain, DL, Value, StackSlot, MPI); | |||
21204 | SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other); | |||
21205 | SDValue Ops[] = { Chain, StackSlot }; | |||
21206 | ||||
21207 | unsigned FLDSize = TheVT.getStoreSize(); | |||
21208 | assert(FLDSize <= MemSize && "Stack slot not big enough")(static_cast <bool> (FLDSize <= MemSize && "Stack slot not big enough" ) ? void (0) : __assert_fail ("FLDSize <= MemSize && \"Stack slot not big enough\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21208, __extension__ __PRETTY_FUNCTION__)); | |||
21209 | MachineMemOperand *MMO = MF.getMachineMemOperand( | |||
21210 | MPI, MachineMemOperand::MOLoad, FLDSize, Align(FLDSize)); | |||
21211 | Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, TheVT, MMO); | |||
21212 | Chain = Value.getValue(1); | |||
21213 | } | |||
21214 | ||||
21215 | // Build the FP_TO_INT*_IN_MEM | |||
21216 | MachineMemOperand *MMO = MF.getMachineMemOperand( | |||
21217 | MPI, MachineMemOperand::MOStore, MemSize, Align(MemSize)); | |||
21218 | SDValue Ops[] = { Chain, Value, StackSlot }; | |||
21219 | SDValue FIST = DAG.getMemIntrinsicNode(X86ISD::FP_TO_INT_IN_MEM, DL, | |||
21220 | DAG.getVTList(MVT::Other), | |||
21221 | Ops, DstTy, MMO); | |||
21222 | ||||
21223 | SDValue Res = DAG.getLoad(Op.getValueType(), SDLoc(Op), FIST, StackSlot, MPI); | |||
21224 | Chain = Res.getValue(1); | |||
21225 | ||||
21226 | // If we need an unsigned fixup, XOR the result with adjust. | |||
21227 | if (UnsignedFixup) | |||
21228 | Res = DAG.getNode(ISD::XOR, DL, MVT::i64, Res, Adjust); | |||
21229 | ||||
21230 | return Res; | |||
21231 | } | |||
21232 | ||||
21233 | static SDValue LowerAVXExtend(SDValue Op, SelectionDAG &DAG, | |||
21234 | const X86Subtarget &Subtarget) { | |||
21235 | MVT VT = Op.getSimpleValueType(); | |||
21236 | SDValue In = Op.getOperand(0); | |||
21237 | MVT InVT = In.getSimpleValueType(); | |||
21238 | SDLoc dl(Op); | |||
21239 | unsigned Opc = Op.getOpcode(); | |||
21240 | ||||
21241 | assert(VT.isVector() && InVT.isVector() && "Expected vector type")(static_cast <bool> (VT.isVector() && InVT.isVector () && "Expected vector type") ? void (0) : __assert_fail ("VT.isVector() && InVT.isVector() && \"Expected vector type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21241, __extension__ __PRETTY_FUNCTION__)); | |||
21242 | assert((Opc == ISD::ANY_EXTEND || Opc == ISD::ZERO_EXTEND) &&(static_cast <bool> ((Opc == ISD::ANY_EXTEND || Opc == ISD ::ZERO_EXTEND) && "Unexpected extension opcode") ? void (0) : __assert_fail ("(Opc == ISD::ANY_EXTEND || Opc == ISD::ZERO_EXTEND) && \"Unexpected extension opcode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21243, __extension__ __PRETTY_FUNCTION__)) | |||
21243 | "Unexpected extension opcode")(static_cast <bool> ((Opc == ISD::ANY_EXTEND || Opc == ISD ::ZERO_EXTEND) && "Unexpected extension opcode") ? void (0) : __assert_fail ("(Opc == ISD::ANY_EXTEND || Opc == ISD::ZERO_EXTEND) && \"Unexpected extension opcode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21243, __extension__ __PRETTY_FUNCTION__)); | |||
21244 | assert(VT.getVectorNumElements() == InVT.getVectorNumElements() &&(static_cast <bool> (VT.getVectorNumElements() == InVT. getVectorNumElements() && "Expected same number of elements" ) ? void (0) : __assert_fail ("VT.getVectorNumElements() == InVT.getVectorNumElements() && \"Expected same number of elements\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21245, __extension__ __PRETTY_FUNCTION__)) | |||
21245 | "Expected same number of elements")(static_cast <bool> (VT.getVectorNumElements() == InVT. getVectorNumElements() && "Expected same number of elements" ) ? void (0) : __assert_fail ("VT.getVectorNumElements() == InVT.getVectorNumElements() && \"Expected same number of elements\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21245, __extension__ __PRETTY_FUNCTION__)); | |||
21246 | assert((VT.getVectorElementType() == MVT::i16 ||(static_cast <bool> ((VT.getVectorElementType() == MVT:: i16 || VT.getVectorElementType() == MVT::i32 || VT.getVectorElementType () == MVT::i64) && "Unexpected element type") ? void ( 0) : __assert_fail ("(VT.getVectorElementType() == MVT::i16 || VT.getVectorElementType() == MVT::i32 || VT.getVectorElementType() == MVT::i64) && \"Unexpected element type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21249, __extension__ __PRETTY_FUNCTION__)) | |||
21247 | VT.getVectorElementType() == MVT::i32 ||(static_cast <bool> ((VT.getVectorElementType() == MVT:: i16 || VT.getVectorElementType() == MVT::i32 || VT.getVectorElementType () == MVT::i64) && "Unexpected element type") ? void ( 0) : __assert_fail ("(VT.getVectorElementType() == MVT::i16 || VT.getVectorElementType() == MVT::i32 || VT.getVectorElementType() == MVT::i64) && \"Unexpected element type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21249, __extension__ __PRETTY_FUNCTION__)) | |||
21248 | VT.getVectorElementType() == MVT::i64) &&(static_cast <bool> ((VT.getVectorElementType() == MVT:: i16 || VT.getVectorElementType() == MVT::i32 || VT.getVectorElementType () == MVT::i64) && "Unexpected element type") ? void ( 0) : __assert_fail ("(VT.getVectorElementType() == MVT::i16 || VT.getVectorElementType() == MVT::i32 || VT.getVectorElementType() == MVT::i64) && \"Unexpected element type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21249, __extension__ __PRETTY_FUNCTION__)) | |||
21249 | "Unexpected element type")(static_cast <bool> ((VT.getVectorElementType() == MVT:: i16 || VT.getVectorElementType() == MVT::i32 || VT.getVectorElementType () == MVT::i64) && "Unexpected element type") ? void ( 0) : __assert_fail ("(VT.getVectorElementType() == MVT::i16 || VT.getVectorElementType() == MVT::i32 || VT.getVectorElementType() == MVT::i64) && \"Unexpected element type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21249, __extension__ __PRETTY_FUNCTION__)); | |||
21250 | assert((InVT.getVectorElementType() == MVT::i8 ||(static_cast <bool> ((InVT.getVectorElementType() == MVT ::i8 || InVT.getVectorElementType() == MVT::i16 || InVT.getVectorElementType () == MVT::i32) && "Unexpected element type") ? void ( 0) : __assert_fail ("(InVT.getVectorElementType() == MVT::i8 || InVT.getVectorElementType() == MVT::i16 || InVT.getVectorElementType() == MVT::i32) && \"Unexpected element type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21253, __extension__ __PRETTY_FUNCTION__)) | |||
21251 | InVT.getVectorElementType() == MVT::i16 ||(static_cast <bool> ((InVT.getVectorElementType() == MVT ::i8 || InVT.getVectorElementType() == MVT::i16 || InVT.getVectorElementType () == MVT::i32) && "Unexpected element type") ? void ( 0) : __assert_fail ("(InVT.getVectorElementType() == MVT::i8 || InVT.getVectorElementType() == MVT::i16 || InVT.getVectorElementType() == MVT::i32) && \"Unexpected element type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21253, __extension__ __PRETTY_FUNCTION__)) | |||
21252 | InVT.getVectorElementType() == MVT::i32) &&(static_cast <bool> ((InVT.getVectorElementType() == MVT ::i8 || InVT.getVectorElementType() == MVT::i16 || InVT.getVectorElementType () == MVT::i32) && "Unexpected element type") ? void ( 0) : __assert_fail ("(InVT.getVectorElementType() == MVT::i8 || InVT.getVectorElementType() == MVT::i16 || InVT.getVectorElementType() == MVT::i32) && \"Unexpected element type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21253, __extension__ __PRETTY_FUNCTION__)) | |||
21253 | "Unexpected element type")(static_cast <bool> ((InVT.getVectorElementType() == MVT ::i8 || InVT.getVectorElementType() == MVT::i16 || InVT.getVectorElementType () == MVT::i32) && "Unexpected element type") ? void ( 0) : __assert_fail ("(InVT.getVectorElementType() == MVT::i8 || InVT.getVectorElementType() == MVT::i16 || InVT.getVectorElementType() == MVT::i32) && \"Unexpected element type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21253, __extension__ __PRETTY_FUNCTION__)); | |||
21254 | ||||
21255 | unsigned ExtendInVecOpc = getOpcode_EXTEND_VECTOR_INREG(Opc); | |||
21256 | ||||
21257 | if (VT == MVT::v32i16 && !Subtarget.hasBWI()) { | |||
21258 | assert(InVT == MVT::v32i8 && "Unexpected VT!")(static_cast <bool> (InVT == MVT::v32i8 && "Unexpected VT!" ) ? void (0) : __assert_fail ("InVT == MVT::v32i8 && \"Unexpected VT!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21258, __extension__ __PRETTY_FUNCTION__)); | |||
21259 | return splitVectorIntUnary(Op, DAG); | |||
21260 | } | |||
21261 | ||||
21262 | if (Subtarget.hasInt256()) | |||
21263 | return Op; | |||
21264 | ||||
21265 | // Optimize vectors in AVX mode: | |||
21266 | // | |||
21267 | // v8i16 -> v8i32 | |||
21268 | // Use vpmovzwd for 4 lower elements v8i16 -> v4i32. | |||
21269 | // Use vpunpckhwd for 4 upper elements v8i16 -> v4i32. | |||
21270 | // Concat upper and lower parts. | |||
21271 | // | |||
21272 | // v4i32 -> v4i64 | |||
21273 | // Use vpmovzdq for 4 lower elements v4i32 -> v2i64. | |||
21274 | // Use vpunpckhdq for 4 upper elements v4i32 -> v2i64. | |||
21275 | // Concat upper and lower parts. | |||
21276 | // | |||
21277 | MVT HalfVT = VT.getHalfNumVectorElementsVT(); | |||
21278 | SDValue OpLo = DAG.getNode(ExtendInVecOpc, dl, HalfVT, In); | |||
21279 | ||||
21280 | // Short-circuit if we can determine that each 128-bit half is the same value. | |||
21281 | // Otherwise, this is difficult to match and optimize. | |||
21282 | if (auto *Shuf = dyn_cast<ShuffleVectorSDNode>(In)) | |||
21283 | if (hasIdenticalHalvesShuffleMask(Shuf->getMask())) | |||
21284 | return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpLo); | |||
21285 | ||||
21286 | SDValue ZeroVec = DAG.getConstant(0, dl, InVT); | |||
21287 | SDValue Undef = DAG.getUNDEF(InVT); | |||
21288 | bool NeedZero = Opc == ISD::ZERO_EXTEND; | |||
21289 | SDValue OpHi = getUnpackh(DAG, dl, InVT, In, NeedZero ? ZeroVec : Undef); | |||
21290 | OpHi = DAG.getBitcast(HalfVT, OpHi); | |||
21291 | ||||
21292 | return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi); | |||
21293 | } | |||
21294 | ||||
21295 | // Helper to split and extend a v16i1 mask to v16i8 or v16i16. | |||
21296 | static SDValue SplitAndExtendv16i1(unsigned ExtOpc, MVT VT, SDValue In, | |||
21297 | const SDLoc &dl, SelectionDAG &DAG) { | |||
21298 | assert((VT == MVT::v16i8 || VT == MVT::v16i16) && "Unexpected VT.")(static_cast <bool> ((VT == MVT::v16i8 || VT == MVT::v16i16 ) && "Unexpected VT.") ? void (0) : __assert_fail ("(VT == MVT::v16i8 || VT == MVT::v16i16) && \"Unexpected VT.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21298, __extension__ __PRETTY_FUNCTION__)); | |||
21299 | SDValue Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v8i1, In, | |||
21300 | DAG.getIntPtrConstant(0, dl)); | |||
21301 | SDValue Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v8i1, In, | |||
21302 | DAG.getIntPtrConstant(8, dl)); | |||
21303 | Lo = DAG.getNode(ExtOpc, dl, MVT::v8i16, Lo); | |||
21304 | Hi = DAG.getNode(ExtOpc, dl, MVT::v8i16, Hi); | |||
21305 | SDValue Res = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v16i16, Lo, Hi); | |||
21306 | return DAG.getNode(ISD::TRUNCATE, dl, VT, Res); | |||
21307 | } | |||
21308 | ||||
21309 | static SDValue LowerZERO_EXTEND_Mask(SDValue Op, | |||
21310 | const X86Subtarget &Subtarget, | |||
21311 | SelectionDAG &DAG) { | |||
21312 | MVT VT = Op->getSimpleValueType(0); | |||
21313 | SDValue In = Op->getOperand(0); | |||
21314 | MVT InVT = In.getSimpleValueType(); | |||
21315 | assert(InVT.getVectorElementType() == MVT::i1 && "Unexpected input type!")(static_cast <bool> (InVT.getVectorElementType() == MVT ::i1 && "Unexpected input type!") ? void (0) : __assert_fail ("InVT.getVectorElementType() == MVT::i1 && \"Unexpected input type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21315, __extension__ __PRETTY_FUNCTION__)); | |||
21316 | SDLoc DL(Op); | |||
21317 | unsigned NumElts = VT.getVectorNumElements(); | |||
21318 | ||||
21319 | // For all vectors, but vXi8 we can just emit a sign_extend and a shift. This | |||
21320 | // avoids a constant pool load. | |||
21321 | if (VT.getVectorElementType() != MVT::i8) { | |||
21322 | SDValue Extend = DAG.getNode(ISD::SIGN_EXTEND, DL, VT, In); | |||
21323 | return DAG.getNode(ISD::SRL, DL, VT, Extend, | |||
21324 | DAG.getConstant(VT.getScalarSizeInBits() - 1, DL, VT)); | |||
21325 | } | |||
21326 | ||||
21327 | // Extend VT if BWI is not supported. | |||
21328 | MVT ExtVT = VT; | |||
21329 | if (!Subtarget.hasBWI()) { | |||
21330 | // If v16i32 is to be avoided, we'll need to split and concatenate. | |||
21331 | if (NumElts == 16 && !Subtarget.canExtendTo512DQ()) | |||
21332 | return SplitAndExtendv16i1(ISD::ZERO_EXTEND, VT, In, DL, DAG); | |||
21333 | ||||
21334 | ExtVT = MVT::getVectorVT(MVT::i32, NumElts); | |||
21335 | } | |||
21336 | ||||
21337 | // Widen to 512-bits if VLX is not supported. | |||
21338 | MVT WideVT = ExtVT; | |||
21339 | if (!ExtVT.is512BitVector() && !Subtarget.hasVLX()) { | |||
21340 | NumElts *= 512 / ExtVT.getSizeInBits(); | |||
21341 | InVT = MVT::getVectorVT(MVT::i1, NumElts); | |||
21342 | In = DAG.getNode(ISD::INSERT_SUBVECTOR, DL, InVT, DAG.getUNDEF(InVT), | |||
21343 | In, DAG.getIntPtrConstant(0, DL)); | |||
21344 | WideVT = MVT::getVectorVT(ExtVT.getVectorElementType(), | |||
21345 | NumElts); | |||
21346 | } | |||
21347 | ||||
21348 | SDValue One = DAG.getConstant(1, DL, WideVT); | |||
21349 | SDValue Zero = DAG.getConstant(0, DL, WideVT); | |||
21350 | ||||
21351 | SDValue SelectedVal = DAG.getSelect(DL, WideVT, In, One, Zero); | |||
21352 | ||||
21353 | // Truncate if we had to extend above. | |||
21354 | if (VT != ExtVT) { | |||
21355 | WideVT = MVT::getVectorVT(MVT::i8, NumElts); | |||
21356 | SelectedVal = DAG.getNode(ISD::TRUNCATE, DL, WideVT, SelectedVal); | |||
21357 | } | |||
21358 | ||||
21359 | // Extract back to 128/256-bit if we widened. | |||
21360 | if (WideVT != VT) | |||
21361 | SelectedVal = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, SelectedVal, | |||
21362 | DAG.getIntPtrConstant(0, DL)); | |||
21363 | ||||
21364 | return SelectedVal; | |||
21365 | } | |||
21366 | ||||
21367 | static SDValue LowerZERO_EXTEND(SDValue Op, const X86Subtarget &Subtarget, | |||
21368 | SelectionDAG &DAG) { | |||
21369 | SDValue In = Op.getOperand(0); | |||
21370 | MVT SVT = In.getSimpleValueType(); | |||
21371 | ||||
21372 | if (SVT.getVectorElementType() == MVT::i1) | |||
21373 | return LowerZERO_EXTEND_Mask(Op, Subtarget, DAG); | |||
21374 | ||||
21375 | assert(Subtarget.hasAVX() && "Expected AVX support")(static_cast <bool> (Subtarget.hasAVX() && "Expected AVX support" ) ? void (0) : __assert_fail ("Subtarget.hasAVX() && \"Expected AVX support\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21375, __extension__ __PRETTY_FUNCTION__)); | |||
21376 | return LowerAVXExtend(Op, DAG, Subtarget); | |||
21377 | } | |||
21378 | ||||
21379 | /// Helper to recursively truncate vector elements in half with PACKSS/PACKUS. | |||
21380 | /// It makes use of the fact that vectors with enough leading sign/zero bits | |||
21381 | /// prevent the PACKSS/PACKUS from saturating the results. | |||
21382 | /// AVX2 (Int256) sub-targets require extra shuffling as the PACK*S operates | |||
21383 | /// within each 128-bit lane. | |||
21384 | static SDValue truncateVectorWithPACK(unsigned Opcode, EVT DstVT, SDValue In, | |||
21385 | const SDLoc &DL, SelectionDAG &DAG, | |||
21386 | const X86Subtarget &Subtarget) { | |||
21387 | assert((Opcode == X86ISD::PACKSS || Opcode == X86ISD::PACKUS) &&(static_cast <bool> ((Opcode == X86ISD::PACKSS || Opcode == X86ISD::PACKUS) && "Unexpected PACK opcode") ? void (0) : __assert_fail ("(Opcode == X86ISD::PACKSS || Opcode == X86ISD::PACKUS) && \"Unexpected PACK opcode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21388, __extension__ __PRETTY_FUNCTION__)) | |||
21388 | "Unexpected PACK opcode")(static_cast <bool> ((Opcode == X86ISD::PACKSS || Opcode == X86ISD::PACKUS) && "Unexpected PACK opcode") ? void (0) : __assert_fail ("(Opcode == X86ISD::PACKSS || Opcode == X86ISD::PACKUS) && \"Unexpected PACK opcode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21388, __extension__ __PRETTY_FUNCTION__)); | |||
21389 | assert(DstVT.isVector() && "VT not a vector?")(static_cast <bool> (DstVT.isVector() && "VT not a vector?" ) ? void (0) : __assert_fail ("DstVT.isVector() && \"VT not a vector?\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21389, __extension__ __PRETTY_FUNCTION__)); | |||
21390 | ||||
21391 | // Requires SSE2 for PACKSS (SSE41 PACKUSDW is handled below). | |||
21392 | if (!Subtarget.hasSSE2()) | |||
21393 | return SDValue(); | |||
21394 | ||||
21395 | EVT SrcVT = In.getValueType(); | |||
21396 | ||||
21397 | // No truncation required, we might get here due to recursive calls. | |||
21398 | if (SrcVT == DstVT) | |||
21399 | return In; | |||
21400 | ||||
21401 | // We only support vector truncation to 64bits or greater from a | |||
21402 | // 128bits or greater source. | |||
21403 | unsigned DstSizeInBits = DstVT.getSizeInBits(); | |||
21404 | unsigned SrcSizeInBits = SrcVT.getSizeInBits(); | |||
21405 | if ((DstSizeInBits % 64) != 0 || (SrcSizeInBits % 128) != 0) | |||
21406 | return SDValue(); | |||
21407 | ||||
21408 | unsigned NumElems = SrcVT.getVectorNumElements(); | |||
21409 | if (!isPowerOf2_32(NumElems)) | |||
21410 | return SDValue(); | |||
21411 | ||||
21412 | LLVMContext &Ctx = *DAG.getContext(); | |||
21413 | assert(DstVT.getVectorNumElements() == NumElems && "Illegal truncation")(static_cast <bool> (DstVT.getVectorNumElements() == NumElems && "Illegal truncation") ? void (0) : __assert_fail ( "DstVT.getVectorNumElements() == NumElems && \"Illegal truncation\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21413, __extension__ __PRETTY_FUNCTION__)); | |||
21414 | assert(SrcSizeInBits > DstSizeInBits && "Illegal truncation")(static_cast <bool> (SrcSizeInBits > DstSizeInBits && "Illegal truncation") ? void (0) : __assert_fail ("SrcSizeInBits > DstSizeInBits && \"Illegal truncation\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21414, __extension__ __PRETTY_FUNCTION__)); | |||
21415 | ||||
21416 | EVT PackedSVT = EVT::getIntegerVT(Ctx, SrcVT.getScalarSizeInBits() / 2); | |||
21417 | ||||
21418 | // Pack to the largest type possible: | |||
21419 | // vXi64/vXi32 -> PACK*SDW and vXi16 -> PACK*SWB. | |||
21420 | EVT InVT = MVT::i16, OutVT = MVT::i8; | |||
21421 | if (SrcVT.getScalarSizeInBits() > 16 && | |||
21422 | (Opcode == X86ISD::PACKSS || Subtarget.hasSSE41())) { | |||
21423 | InVT = MVT::i32; | |||
21424 | OutVT = MVT::i16; | |||
21425 | } | |||
21426 | ||||
21427 | // 128bit -> 64bit truncate - PACK 128-bit src in the lower subvector. | |||
21428 | if (SrcVT.is128BitVector()) { | |||
21429 | InVT = EVT::getVectorVT(Ctx, InVT, 128 / InVT.getSizeInBits()); | |||
21430 | OutVT = EVT::getVectorVT(Ctx, OutVT, 128 / OutVT.getSizeInBits()); | |||
21431 | In = DAG.getBitcast(InVT, In); | |||
21432 | SDValue Res = DAG.getNode(Opcode, DL, OutVT, In, DAG.getUNDEF(InVT)); | |||
21433 | Res = extractSubVector(Res, 0, DAG, DL, 64); | |||
21434 | return DAG.getBitcast(DstVT, Res); | |||
21435 | } | |||
21436 | ||||
21437 | // Split lower/upper subvectors. | |||
21438 | SDValue Lo, Hi; | |||
21439 | std::tie(Lo, Hi) = splitVector(In, DAG, DL); | |||
21440 | ||||
21441 | unsigned SubSizeInBits = SrcSizeInBits / 2; | |||
21442 | InVT = EVT::getVectorVT(Ctx, InVT, SubSizeInBits / InVT.getSizeInBits()); | |||
21443 | OutVT = EVT::getVectorVT(Ctx, OutVT, SubSizeInBits / OutVT.getSizeInBits()); | |||
21444 | ||||
21445 | // 256bit -> 128bit truncate - PACK lower/upper 128-bit subvectors. | |||
21446 | if (SrcVT.is256BitVector() && DstVT.is128BitVector()) { | |||
21447 | Lo = DAG.getBitcast(InVT, Lo); | |||
21448 | Hi = DAG.getBitcast(InVT, Hi); | |||
21449 | SDValue Res = DAG.getNode(Opcode, DL, OutVT, Lo, Hi); | |||
21450 | return DAG.getBitcast(DstVT, Res); | |||
21451 | } | |||
21452 | ||||
21453 | // AVX2: 512bit -> 256bit truncate - PACK lower/upper 256-bit subvectors. | |||
21454 | // AVX2: 512bit -> 128bit truncate - PACK(PACK, PACK). | |||
21455 | if (SrcVT.is512BitVector() && Subtarget.hasInt256()) { | |||
21456 | Lo = DAG.getBitcast(InVT, Lo); | |||
21457 | Hi = DAG.getBitcast(InVT, Hi); | |||
21458 | SDValue Res = DAG.getNode(Opcode, DL, OutVT, Lo, Hi); | |||
21459 | ||||
21460 | // 256-bit PACK(ARG0, ARG1) leaves us with ((LO0,LO1),(HI0,HI1)), | |||
21461 | // so we need to shuffle to get ((LO0,HI0),(LO1,HI1)). | |||
21462 | // Scale shuffle mask to avoid bitcasts and help ComputeNumSignBits. | |||
21463 | SmallVector<int, 64> Mask; | |||
21464 | int Scale = 64 / OutVT.getScalarSizeInBits(); | |||
21465 | narrowShuffleMaskElts(Scale, { 0, 2, 1, 3 }, Mask); | |||
21466 | Res = DAG.getVectorShuffle(OutVT, DL, Res, Res, Mask); | |||
21467 | ||||
21468 | if (DstVT.is256BitVector()) | |||
21469 | return DAG.getBitcast(DstVT, Res); | |||
21470 | ||||
21471 | // If 512bit -> 128bit truncate another stage. | |||
21472 | EVT PackedVT = EVT::getVectorVT(Ctx, PackedSVT, NumElems); | |||
21473 | Res = DAG.getBitcast(PackedVT, Res); | |||
21474 | return truncateVectorWithPACK(Opcode, DstVT, Res, DL, DAG, Subtarget); | |||
21475 | } | |||
21476 | ||||
21477 | // Recursively pack lower/upper subvectors, concat result and pack again. | |||
21478 | assert(SrcSizeInBits >= 256 && "Expected 256-bit vector or greater")(static_cast <bool> (SrcSizeInBits >= 256 && "Expected 256-bit vector or greater") ? void (0) : __assert_fail ("SrcSizeInBits >= 256 && \"Expected 256-bit vector or greater\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21478, __extension__ __PRETTY_FUNCTION__)); | |||
21479 | EVT PackedVT = EVT::getVectorVT(Ctx, PackedSVT, NumElems / 2); | |||
21480 | Lo = truncateVectorWithPACK(Opcode, PackedVT, Lo, DL, DAG, Subtarget); | |||
21481 | Hi = truncateVectorWithPACK(Opcode, PackedVT, Hi, DL, DAG, Subtarget); | |||
21482 | ||||
21483 | PackedVT = EVT::getVectorVT(Ctx, PackedSVT, NumElems); | |||
21484 | SDValue Res = DAG.getNode(ISD::CONCAT_VECTORS, DL, PackedVT, Lo, Hi); | |||
21485 | return truncateVectorWithPACK(Opcode, DstVT, Res, DL, DAG, Subtarget); | |||
21486 | } | |||
21487 | ||||
21488 | static SDValue LowerTruncateVecI1(SDValue Op, SelectionDAG &DAG, | |||
21489 | const X86Subtarget &Subtarget) { | |||
21490 | ||||
21491 | SDLoc DL(Op); | |||
21492 | MVT VT = Op.getSimpleValueType(); | |||
21493 | SDValue In = Op.getOperand(0); | |||
21494 | MVT InVT = In.getSimpleValueType(); | |||
21495 | ||||
21496 | assert(VT.getVectorElementType() == MVT::i1 && "Unexpected vector type.")(static_cast <bool> (VT.getVectorElementType() == MVT:: i1 && "Unexpected vector type.") ? void (0) : __assert_fail ("VT.getVectorElementType() == MVT::i1 && \"Unexpected vector type.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21496, __extension__ __PRETTY_FUNCTION__)); | |||
21497 | ||||
21498 | // Shift LSB to MSB and use VPMOVB/W2M or TESTD/Q. | |||
21499 | unsigned ShiftInx = InVT.getScalarSizeInBits() - 1; | |||
21500 | if (InVT.getScalarSizeInBits() <= 16) { | |||
21501 | if (Subtarget.hasBWI()) { | |||
21502 | // legal, will go to VPMOVB2M, VPMOVW2M | |||
21503 | if (DAG.ComputeNumSignBits(In) < InVT.getScalarSizeInBits()) { | |||
21504 | // We need to shift to get the lsb into sign position. | |||
21505 | // Shift packed bytes not supported natively, bitcast to word | |||
21506 | MVT ExtVT = MVT::getVectorVT(MVT::i16, InVT.getSizeInBits()/16); | |||
21507 | In = DAG.getNode(ISD::SHL, DL, ExtVT, | |||
21508 | DAG.getBitcast(ExtVT, In), | |||
21509 | DAG.getConstant(ShiftInx, DL, ExtVT)); | |||
21510 | In = DAG.getBitcast(InVT, In); | |||
21511 | } | |||
21512 | return DAG.getSetCC(DL, VT, DAG.getConstant(0, DL, InVT), | |||
21513 | In, ISD::SETGT); | |||
21514 | } | |||
21515 | // Use TESTD/Q, extended vector to packed dword/qword. | |||
21516 | assert((InVT.is256BitVector() || InVT.is128BitVector()) &&(static_cast <bool> ((InVT.is256BitVector() || InVT.is128BitVector ()) && "Unexpected vector type.") ? void (0) : __assert_fail ("(InVT.is256BitVector() || InVT.is128BitVector()) && \"Unexpected vector type.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21517, __extension__ __PRETTY_FUNCTION__)) | |||
21517 | "Unexpected vector type.")(static_cast <bool> ((InVT.is256BitVector() || InVT.is128BitVector ()) && "Unexpected vector type.") ? void (0) : __assert_fail ("(InVT.is256BitVector() || InVT.is128BitVector()) && \"Unexpected vector type.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21517, __extension__ __PRETTY_FUNCTION__)); | |||
21518 | unsigned NumElts = InVT.getVectorNumElements(); | |||
21519 | assert((NumElts == 8 || NumElts == 16) && "Unexpected number of elements")(static_cast <bool> ((NumElts == 8 || NumElts == 16) && "Unexpected number of elements") ? void (0) : __assert_fail ( "(NumElts == 8 || NumElts == 16) && \"Unexpected number of elements\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21519, __extension__ __PRETTY_FUNCTION__)); | |||
21520 | // We need to change to a wider element type that we have support for. | |||
21521 | // For 8 element vectors this is easy, we either extend to v8i32 or v8i64. | |||
21522 | // For 16 element vectors we extend to v16i32 unless we are explicitly | |||
21523 | // trying to avoid 512-bit vectors. If we are avoiding 512-bit vectors | |||
21524 | // we need to split into two 8 element vectors which we can extend to v8i32, | |||
21525 | // truncate and concat the results. There's an additional complication if | |||
21526 | // the original type is v16i8. In that case we can't split the v16i8 | |||
21527 | // directly, so we need to shuffle high elements to low and use | |||
21528 | // sign_extend_vector_inreg. | |||
21529 | if (NumElts == 16 && !Subtarget.canExtendTo512DQ()) { | |||
21530 | SDValue Lo, Hi; | |||
21531 | if (InVT == MVT::v16i8) { | |||
21532 | Lo = DAG.getNode(ISD::SIGN_EXTEND_VECTOR_INREG, DL, MVT::v8i32, In); | |||
21533 | Hi = DAG.getVectorShuffle( | |||
21534 | InVT, DL, In, In, | |||
21535 | {8, 9, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1}); | |||
21536 | Hi = DAG.getNode(ISD::SIGN_EXTEND_VECTOR_INREG, DL, MVT::v8i32, Hi); | |||
21537 | } else { | |||
21538 | assert(InVT == MVT::v16i16 && "Unexpected VT!")(static_cast <bool> (InVT == MVT::v16i16 && "Unexpected VT!" ) ? void (0) : __assert_fail ("InVT == MVT::v16i16 && \"Unexpected VT!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21538, __extension__ __PRETTY_FUNCTION__)); | |||
21539 | Lo = extract128BitVector(In, 0, DAG, DL); | |||
21540 | Hi = extract128BitVector(In, 8, DAG, DL); | |||
21541 | } | |||
21542 | // We're split now, just emit two truncates and a concat. The two | |||
21543 | // truncates will trigger legalization to come back to this function. | |||
21544 | Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::v8i1, Lo); | |||
21545 | Hi = DAG.getNode(ISD::TRUNCATE, DL, MVT::v8i1, Hi); | |||
21546 | return DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, Lo, Hi); | |||
21547 | } | |||
21548 | // We either have 8 elements or we're allowed to use 512-bit vectors. | |||
21549 | // If we have VLX, we want to use the narrowest vector that can get the | |||
21550 | // job done so we use vXi32. | |||
21551 | MVT EltVT = Subtarget.hasVLX() ? MVT::i32 : MVT::getIntegerVT(512/NumElts); | |||
21552 | MVT ExtVT = MVT::getVectorVT(EltVT, NumElts); | |||
21553 | In = DAG.getNode(ISD::SIGN_EXTEND, DL, ExtVT, In); | |||
21554 | InVT = ExtVT; | |||
21555 | ShiftInx = InVT.getScalarSizeInBits() - 1; | |||
21556 | } | |||
21557 | ||||
21558 | if (DAG.ComputeNumSignBits(In) < InVT.getScalarSizeInBits()) { | |||
21559 | // We need to shift to get the lsb into sign position. | |||
21560 | In = DAG.getNode(ISD::SHL, DL, InVT, In, | |||
21561 | DAG.getConstant(ShiftInx, DL, InVT)); | |||
21562 | } | |||
21563 | // If we have DQI, emit a pattern that will be iseled as vpmovq2m/vpmovd2m. | |||
21564 | if (Subtarget.hasDQI()) | |||
21565 | return DAG.getSetCC(DL, VT, DAG.getConstant(0, DL, InVT), In, ISD::SETGT); | |||
21566 | return DAG.getSetCC(DL, VT, In, DAG.getConstant(0, DL, InVT), ISD::SETNE); | |||
21567 | } | |||
21568 | ||||
21569 | SDValue X86TargetLowering::LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const { | |||
21570 | SDLoc DL(Op); | |||
21571 | MVT VT = Op.getSimpleValueType(); | |||
21572 | SDValue In = Op.getOperand(0); | |||
21573 | MVT InVT = In.getSimpleValueType(); | |||
21574 | unsigned InNumEltBits = InVT.getScalarSizeInBits(); | |||
21575 | ||||
21576 | assert(VT.getVectorNumElements() == InVT.getVectorNumElements() &&(static_cast <bool> (VT.getVectorNumElements() == InVT. getVectorNumElements() && "Invalid TRUNCATE operation" ) ? void (0) : __assert_fail ("VT.getVectorNumElements() == InVT.getVectorNumElements() && \"Invalid TRUNCATE operation\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21577, __extension__ __PRETTY_FUNCTION__)) | |||
21577 | "Invalid TRUNCATE operation")(static_cast <bool> (VT.getVectorNumElements() == InVT. getVectorNumElements() && "Invalid TRUNCATE operation" ) ? void (0) : __assert_fail ("VT.getVectorNumElements() == InVT.getVectorNumElements() && \"Invalid TRUNCATE operation\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21577, __extension__ __PRETTY_FUNCTION__)); | |||
21578 | ||||
21579 | // If we're called by the type legalizer, handle a few cases. | |||
21580 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
21581 | if (!TLI.isTypeLegal(InVT)) { | |||
21582 | if ((InVT == MVT::v8i64 || InVT == MVT::v16i32 || InVT == MVT::v16i64) && | |||
21583 | VT.is128BitVector()) { | |||
21584 | assert((InVT == MVT::v16i64 || Subtarget.hasVLX()) &&(static_cast <bool> ((InVT == MVT::v16i64 || Subtarget. hasVLX()) && "Unexpected subtarget!") ? void (0) : __assert_fail ("(InVT == MVT::v16i64 || Subtarget.hasVLX()) && \"Unexpected subtarget!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21585, __extension__ __PRETTY_FUNCTION__)) | |||
21585 | "Unexpected subtarget!")(static_cast <bool> ((InVT == MVT::v16i64 || Subtarget. hasVLX()) && "Unexpected subtarget!") ? void (0) : __assert_fail ("(InVT == MVT::v16i64 || Subtarget.hasVLX()) && \"Unexpected subtarget!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21585, __extension__ __PRETTY_FUNCTION__)); | |||
21586 | // The default behavior is to truncate one step, concatenate, and then | |||
21587 | // truncate the remainder. We'd rather produce two 64-bit results and | |||
21588 | // concatenate those. | |||
21589 | SDValue Lo, Hi; | |||
21590 | std::tie(Lo, Hi) = DAG.SplitVector(In, DL); | |||
21591 | ||||
21592 | EVT LoVT, HiVT; | |||
21593 | std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VT); | |||
21594 | ||||
21595 | Lo = DAG.getNode(ISD::TRUNCATE, DL, LoVT, Lo); | |||
21596 | Hi = DAG.getNode(ISD::TRUNCATE, DL, HiVT, Hi); | |||
21597 | return DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, Lo, Hi); | |||
21598 | } | |||
21599 | ||||
21600 | // Otherwise let default legalization handle it. | |||
21601 | return SDValue(); | |||
21602 | } | |||
21603 | ||||
21604 | if (VT.getVectorElementType() == MVT::i1) | |||
21605 | return LowerTruncateVecI1(Op, DAG, Subtarget); | |||
21606 | ||||
21607 | // vpmovqb/w/d, vpmovdb/w, vpmovwb | |||
21608 | if (Subtarget.hasAVX512()) { | |||
21609 | if (InVT == MVT::v32i16 && !Subtarget.hasBWI()) { | |||
21610 | assert(VT == MVT::v32i8 && "Unexpected VT!")(static_cast <bool> (VT == MVT::v32i8 && "Unexpected VT!" ) ? void (0) : __assert_fail ("VT == MVT::v32i8 && \"Unexpected VT!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21610, __extension__ __PRETTY_FUNCTION__)); | |||
21611 | return splitVectorIntUnary(Op, DAG); | |||
21612 | } | |||
21613 | ||||
21614 | // word to byte only under BWI. Otherwise we have to promoted to v16i32 | |||
21615 | // and then truncate that. But we should only do that if we haven't been | |||
21616 | // asked to avoid 512-bit vectors. The actual promotion to v16i32 will be | |||
21617 | // handled by isel patterns. | |||
21618 | if (InVT != MVT::v16i16 || Subtarget.hasBWI() || | |||
21619 | Subtarget.canExtendTo512DQ()) | |||
21620 | return Op; | |||
21621 | } | |||
21622 | ||||
21623 | unsigned NumPackedSignBits = std::min<unsigned>(VT.getScalarSizeInBits(), 16); | |||
21624 | unsigned NumPackedZeroBits = Subtarget.hasSSE41() ? NumPackedSignBits : 8; | |||
21625 | ||||
21626 | // Truncate with PACKUS if we are truncating a vector with leading zero bits | |||
21627 | // that extend all the way to the packed/truncated value. | |||
21628 | // Pre-SSE41 we can only use PACKUSWB. | |||
21629 | KnownBits Known = DAG.computeKnownBits(In); | |||
21630 | if ((InNumEltBits - NumPackedZeroBits) <= Known.countMinLeadingZeros()) | |||
21631 | if (SDValue V = | |||
21632 | truncateVectorWithPACK(X86ISD::PACKUS, VT, In, DL, DAG, Subtarget)) | |||
21633 | return V; | |||
21634 | ||||
21635 | // Truncate with PACKSS if we are truncating a vector with sign-bits that | |||
21636 | // extend all the way to the packed/truncated value. | |||
21637 | if ((InNumEltBits - NumPackedSignBits) < DAG.ComputeNumSignBits(In)) | |||
21638 | if (SDValue V = | |||
21639 | truncateVectorWithPACK(X86ISD::PACKSS, VT, In, DL, DAG, Subtarget)) | |||
21640 | return V; | |||
21641 | ||||
21642 | // Handle truncation of V256 to V128 using shuffles. | |||
21643 | assert(VT.is128BitVector() && InVT.is256BitVector() && "Unexpected types!")(static_cast <bool> (VT.is128BitVector() && InVT .is256BitVector() && "Unexpected types!") ? void (0) : __assert_fail ("VT.is128BitVector() && InVT.is256BitVector() && \"Unexpected types!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21643, __extension__ __PRETTY_FUNCTION__)); | |||
21644 | ||||
21645 | if ((VT == MVT::v4i32) && (InVT == MVT::v4i64)) { | |||
21646 | In = DAG.getBitcast(MVT::v8i32, In); | |||
21647 | ||||
21648 | // On AVX2, v4i64 -> v4i32 becomes VPERMD. | |||
21649 | if (Subtarget.hasInt256()) { | |||
21650 | static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1}; | |||
21651 | In = DAG.getVectorShuffle(MVT::v8i32, DL, In, In, ShufMask); | |||
21652 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, In, | |||
21653 | DAG.getIntPtrConstant(0, DL)); | |||
21654 | } | |||
21655 | ||||
21656 | SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In, | |||
21657 | DAG.getIntPtrConstant(0, DL)); | |||
21658 | SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In, | |||
21659 | DAG.getIntPtrConstant(4, DL)); | |||
21660 | static const int ShufMask[] = {0, 2, 4, 6}; | |||
21661 | return DAG.getVectorShuffle(VT, DL, OpLo, OpHi, ShufMask); | |||
21662 | } | |||
21663 | ||||
21664 | if ((VT == MVT::v8i16) && (InVT == MVT::v8i32)) { | |||
21665 | In = DAG.getBitcast(MVT::v32i8, In); | |||
21666 | ||||
21667 | // On AVX2, v8i32 -> v8i16 becomes PSHUFB. | |||
21668 | if (Subtarget.hasInt256()) { | |||
21669 | // The PSHUFB mask: | |||
21670 | static const int ShufMask1[] = { 0, 1, 4, 5, 8, 9, 12, 13, | |||
21671 | -1, -1, -1, -1, -1, -1, -1, -1, | |||
21672 | 16, 17, 20, 21, 24, 25, 28, 29, | |||
21673 | -1, -1, -1, -1, -1, -1, -1, -1 }; | |||
21674 | In = DAG.getVectorShuffle(MVT::v32i8, DL, In, In, ShufMask1); | |||
21675 | In = DAG.getBitcast(MVT::v4i64, In); | |||
21676 | ||||
21677 | static const int ShufMask2[] = {0, 2, -1, -1}; | |||
21678 | In = DAG.getVectorShuffle(MVT::v4i64, DL, In, In, ShufMask2); | |||
21679 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i16, | |||
21680 | DAG.getBitcast(MVT::v16i16, In), | |||
21681 | DAG.getIntPtrConstant(0, DL)); | |||
21682 | } | |||
21683 | ||||
21684 | SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v16i8, In, | |||
21685 | DAG.getIntPtrConstant(0, DL)); | |||
21686 | SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v16i8, In, | |||
21687 | DAG.getIntPtrConstant(16, DL)); | |||
21688 | ||||
21689 | // The PSHUFB mask: | |||
21690 | static const int ShufMask1[] = {0, 1, 4, 5, 8, 9, 12, 13, | |||
21691 | -1, -1, -1, -1, -1, -1, -1, -1}; | |||
21692 | ||||
21693 | OpLo = DAG.getVectorShuffle(MVT::v16i8, DL, OpLo, OpLo, ShufMask1); | |||
21694 | OpHi = DAG.getVectorShuffle(MVT::v16i8, DL, OpHi, OpHi, ShufMask1); | |||
21695 | ||||
21696 | OpLo = DAG.getBitcast(MVT::v4i32, OpLo); | |||
21697 | OpHi = DAG.getBitcast(MVT::v4i32, OpHi); | |||
21698 | ||||
21699 | // The MOVLHPS Mask: | |||
21700 | static const int ShufMask2[] = {0, 1, 4, 5}; | |||
21701 | SDValue res = DAG.getVectorShuffle(MVT::v4i32, DL, OpLo, OpHi, ShufMask2); | |||
21702 | return DAG.getBitcast(MVT::v8i16, res); | |||
21703 | } | |||
21704 | ||||
21705 | if (VT == MVT::v16i8 && InVT == MVT::v16i16) { | |||
21706 | // Use an AND to zero uppper bits for PACKUS. | |||
21707 | In = DAG.getNode(ISD::AND, DL, InVT, In, DAG.getConstant(255, DL, InVT)); | |||
21708 | ||||
21709 | SDValue InLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i16, In, | |||
21710 | DAG.getIntPtrConstant(0, DL)); | |||
21711 | SDValue InHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v8i16, In, | |||
21712 | DAG.getIntPtrConstant(8, DL)); | |||
21713 | return DAG.getNode(X86ISD::PACKUS, DL, VT, InLo, InHi); | |||
21714 | } | |||
21715 | ||||
21716 | llvm_unreachable("All 256->128 cases should have been handled above!")::llvm::llvm_unreachable_internal("All 256->128 cases should have been handled above!" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21716); | |||
21717 | } | |||
21718 | ||||
21719 | // We can leverage the specific way the "cvttps2dq/cvttpd2dq" instruction | |||
21720 | // behaves on out of range inputs to generate optimized conversions. | |||
21721 | static SDValue expandFP_TO_UINT_SSE(MVT VT, SDValue Src, const SDLoc &dl, | |||
21722 | SelectionDAG &DAG, | |||
21723 | const X86Subtarget &Subtarget) { | |||
21724 | MVT SrcVT = Src.getSimpleValueType(); | |||
21725 | unsigned DstBits = VT.getScalarSizeInBits(); | |||
21726 | assert(DstBits == 32 && "expandFP_TO_UINT_SSE - only vXi32 supported")(static_cast <bool> (DstBits == 32 && "expandFP_TO_UINT_SSE - only vXi32 supported" ) ? void (0) : __assert_fail ("DstBits == 32 && \"expandFP_TO_UINT_SSE - only vXi32 supported\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21726, __extension__ __PRETTY_FUNCTION__)); | |||
21727 | ||||
21728 | // Calculate the converted result for values in the range 0 to | |||
21729 | // 2^31-1 ("Small") and from 2^31 to 2^32-1 ("Big"). | |||
21730 | SDValue Small = DAG.getNode(X86ISD::CVTTP2SI, dl, VT, Src); | |||
21731 | SDValue Big = | |||
21732 | DAG.getNode(X86ISD::CVTTP2SI, dl, VT, | |||
21733 | DAG.getNode(ISD::FSUB, dl, SrcVT, Src, | |||
21734 | DAG.getConstantFP(2147483648.0f, dl, SrcVT))); | |||
21735 | ||||
21736 | // The "CVTTP2SI" instruction conveniently sets the sign bit if | |||
21737 | // and only if the value was out of range. So we can use that | |||
21738 | // as our indicator that we rather use "Big" instead of "Small". | |||
21739 | // | |||
21740 | // Use "Small" if "IsOverflown" has all bits cleared | |||
21741 | // and "0x80000000 | Big" if all bits in "IsOverflown" are set. | |||
21742 | ||||
21743 | // AVX1 can't use the signsplat masking for 256-bit vectors - we have to | |||
21744 | // use the slightly slower blendv select instead. | |||
21745 | if (VT == MVT::v8i32 && !Subtarget.hasAVX2()) { | |||
21746 | SDValue Overflow = DAG.getNode(ISD::OR, dl, VT, Small, Big); | |||
21747 | return DAG.getNode(X86ISD::BLENDV, dl, VT, Small, Overflow, Small); | |||
21748 | } | |||
21749 | ||||
21750 | SDValue IsOverflown = | |||
21751 | DAG.getNode(X86ISD::VSRAI, dl, VT, Small, | |||
21752 | DAG.getTargetConstant(DstBits - 1, dl, MVT::i8)); | |||
21753 | return DAG.getNode(ISD::OR, dl, VT, Small, | |||
21754 | DAG.getNode(ISD::AND, dl, VT, Big, IsOverflown)); | |||
21755 | } | |||
21756 | ||||
21757 | SDValue X86TargetLowering::LowerFP_TO_INT(SDValue Op, SelectionDAG &DAG) const { | |||
21758 | bool IsStrict = Op->isStrictFPOpcode(); | |||
21759 | bool IsSigned = Op.getOpcode() == ISD::FP_TO_SINT || | |||
21760 | Op.getOpcode() == ISD::STRICT_FP_TO_SINT; | |||
21761 | MVT VT = Op->getSimpleValueType(0); | |||
21762 | SDValue Src = Op.getOperand(IsStrict ? 1 : 0); | |||
21763 | SDValue Chain = IsStrict ? Op->getOperand(0) : SDValue(); | |||
21764 | MVT SrcVT = Src.getSimpleValueType(); | |||
21765 | SDLoc dl(Op); | |||
21766 | ||||
21767 | SDValue Res; | |||
21768 | if (VT.isVector()) { | |||
21769 | if (VT == MVT::v2i1 && SrcVT == MVT::v2f64) { | |||
21770 | MVT ResVT = MVT::v4i32; | |||
21771 | MVT TruncVT = MVT::v4i1; | |||
21772 | unsigned Opc; | |||
21773 | if (IsStrict) | |||
21774 | Opc = IsSigned ? X86ISD::STRICT_CVTTP2SI : X86ISD::STRICT_CVTTP2UI; | |||
21775 | else | |||
21776 | Opc = IsSigned ? X86ISD::CVTTP2SI : X86ISD::CVTTP2UI; | |||
21777 | ||||
21778 | if (!IsSigned && !Subtarget.hasVLX()) { | |||
21779 | assert(Subtarget.useAVX512Regs() && "Unexpected features!")(static_cast <bool> (Subtarget.useAVX512Regs() && "Unexpected features!") ? void (0) : __assert_fail ("Subtarget.useAVX512Regs() && \"Unexpected features!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21779, __extension__ __PRETTY_FUNCTION__)); | |||
21780 | // Widen to 512-bits. | |||
21781 | ResVT = MVT::v8i32; | |||
21782 | TruncVT = MVT::v8i1; | |||
21783 | Opc = Op.getOpcode(); | |||
21784 | // Need to concat with zero vector for strict fp to avoid spurious | |||
21785 | // exceptions. | |||
21786 | // TODO: Should we just do this for non-strict as well? | |||
21787 | SDValue Tmp = IsStrict ? DAG.getConstantFP(0.0, dl, MVT::v8f64) | |||
21788 | : DAG.getUNDEF(MVT::v8f64); | |||
21789 | Src = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, MVT::v8f64, Tmp, Src, | |||
21790 | DAG.getIntPtrConstant(0, dl)); | |||
21791 | } | |||
21792 | if (IsStrict) { | |||
21793 | Res = DAG.getNode(Opc, dl, {ResVT, MVT::Other}, {Chain, Src}); | |||
21794 | Chain = Res.getValue(1); | |||
21795 | } else { | |||
21796 | Res = DAG.getNode(Opc, dl, ResVT, Src); | |||
21797 | } | |||
21798 | ||||
21799 | Res = DAG.getNode(ISD::TRUNCATE, dl, TruncVT, Res); | |||
21800 | Res = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i1, Res, | |||
21801 | DAG.getIntPtrConstant(0, dl)); | |||
21802 | if (IsStrict) | |||
21803 | return DAG.getMergeValues({Res, Chain}, dl); | |||
21804 | return Res; | |||
21805 | } | |||
21806 | ||||
21807 | if (Subtarget.hasFP16() && SrcVT.getVectorElementType() == MVT::f16) { | |||
21808 | if (VT == MVT::v8i16 || VT == MVT::v16i16 || VT == MVT::v32i16) | |||
21809 | return Op; | |||
21810 | ||||
21811 | MVT ResVT = VT; | |||
21812 | MVT EleVT = VT.getVectorElementType(); | |||
21813 | if (EleVT != MVT::i64) | |||
21814 | ResVT = EleVT == MVT::i32 ? MVT::v4i32 : MVT::v8i16; | |||
21815 | ||||
21816 | if (SrcVT != MVT::v8f16) { | |||
21817 | SDValue Tmp = | |||
21818 | IsStrict ? DAG.getConstantFP(0.0, dl, SrcVT) : DAG.getUNDEF(SrcVT); | |||
21819 | SmallVector<SDValue, 4> Ops(SrcVT == MVT::v2f16 ? 4 : 2, Tmp); | |||
21820 | Ops[0] = Src; | |||
21821 | Src = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8f16, Ops); | |||
21822 | } | |||
21823 | ||||
21824 | if (IsStrict) { | |||
21825 | Res = DAG.getNode(IsSigned ? X86ISD::STRICT_CVTTP2SI | |||
21826 | : X86ISD::STRICT_CVTTP2UI, | |||
21827 | dl, {ResVT, MVT::Other}, {Chain, Src}); | |||
21828 | Chain = Res.getValue(1); | |||
21829 | } else { | |||
21830 | Res = DAG.getNode(IsSigned ? X86ISD::CVTTP2SI : X86ISD::CVTTP2UI, dl, | |||
21831 | ResVT, Src); | |||
21832 | } | |||
21833 | ||||
21834 | // TODO: Need to add exception check code for strict FP. | |||
21835 | if (EleVT.getSizeInBits() < 16) { | |||
21836 | ResVT = MVT::getVectorVT(EleVT, 8); | |||
21837 | Res = DAG.getNode(ISD::TRUNCATE, dl, ResVT, Res); | |||
21838 | } | |||
21839 | ||||
21840 | if (ResVT != VT) | |||
21841 | Res = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, Res, | |||
21842 | DAG.getIntPtrConstant(0, dl)); | |||
21843 | ||||
21844 | if (IsStrict) | |||
21845 | return DAG.getMergeValues({Res, Chain}, dl); | |||
21846 | return Res; | |||
21847 | } | |||
21848 | ||||
21849 | if (VT == MVT::v8i16 && (SrcVT == MVT::v8f32 || SrcVT == MVT::v8f64)) { | |||
21850 | if (IsStrict) { | |||
21851 | Res = DAG.getNode(IsSigned ? ISD::STRICT_FP_TO_SINT | |||
21852 | : ISD::STRICT_FP_TO_UINT, | |||
21853 | dl, {MVT::v8i32, MVT::Other}, {Chain, Src}); | |||
21854 | Chain = Res.getValue(1); | |||
21855 | } else { | |||
21856 | Res = DAG.getNode(IsSigned ? ISD::FP_TO_SINT : ISD::FP_TO_UINT, dl, | |||
21857 | MVT::v8i32, Src); | |||
21858 | } | |||
21859 | ||||
21860 | // TODO: Need to add exception check code for strict FP. | |||
21861 | Res = DAG.getNode(ISD::TRUNCATE, dl, MVT::v8i16, Res); | |||
21862 | ||||
21863 | if (IsStrict) | |||
21864 | return DAG.getMergeValues({Res, Chain}, dl); | |||
21865 | return Res; | |||
21866 | } | |||
21867 | ||||
21868 | // v8f64->v8i32 is legal, but we need v8i32 to be custom for v8f32. | |||
21869 | if (VT == MVT::v8i32 && SrcVT == MVT::v8f64) { | |||
21870 | assert(!IsSigned && "Expected unsigned conversion!")(static_cast <bool> (!IsSigned && "Expected unsigned conversion!" ) ? void (0) : __assert_fail ("!IsSigned && \"Expected unsigned conversion!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21870, __extension__ __PRETTY_FUNCTION__)); | |||
21871 | assert(Subtarget.useAVX512Regs() && "Requires avx512f")(static_cast <bool> (Subtarget.useAVX512Regs() && "Requires avx512f") ? void (0) : __assert_fail ("Subtarget.useAVX512Regs() && \"Requires avx512f\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21871, __extension__ __PRETTY_FUNCTION__)); | |||
21872 | return Op; | |||
21873 | } | |||
21874 | ||||
21875 | // Widen vXi32 fp_to_uint with avx512f to 512-bit source. | |||
21876 | if ((VT == MVT::v4i32 || VT == MVT::v8i32) && | |||
21877 | (SrcVT == MVT::v4f64 || SrcVT == MVT::v4f32 || SrcVT == MVT::v8f32) && | |||
21878 | Subtarget.useAVX512Regs()) { | |||
21879 | assert(!IsSigned && "Expected unsigned conversion!")(static_cast <bool> (!IsSigned && "Expected unsigned conversion!" ) ? void (0) : __assert_fail ("!IsSigned && \"Expected unsigned conversion!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21879, __extension__ __PRETTY_FUNCTION__)); | |||
21880 | assert(!Subtarget.hasVLX() && "Unexpected features!")(static_cast <bool> (!Subtarget.hasVLX() && "Unexpected features!" ) ? void (0) : __assert_fail ("!Subtarget.hasVLX() && \"Unexpected features!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21880, __extension__ __PRETTY_FUNCTION__)); | |||
21881 | MVT WideVT = SrcVT == MVT::v4f64 ? MVT::v8f64 : MVT::v16f32; | |||
21882 | MVT ResVT = SrcVT == MVT::v4f64 ? MVT::v8i32 : MVT::v16i32; | |||
21883 | // Need to concat with zero vector for strict fp to avoid spurious | |||
21884 | // exceptions. | |||
21885 | // TODO: Should we just do this for non-strict as well? | |||
21886 | SDValue Tmp = | |||
21887 | IsStrict ? DAG.getConstantFP(0.0, dl, WideVT) : DAG.getUNDEF(WideVT); | |||
21888 | Src = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, WideVT, Tmp, Src, | |||
21889 | DAG.getIntPtrConstant(0, dl)); | |||
21890 | ||||
21891 | if (IsStrict) { | |||
21892 | Res = DAG.getNode(ISD::STRICT_FP_TO_UINT, dl, {ResVT, MVT::Other}, | |||
21893 | {Chain, Src}); | |||
21894 | Chain = Res.getValue(1); | |||
21895 | } else { | |||
21896 | Res = DAG.getNode(ISD::FP_TO_UINT, dl, ResVT, Src); | |||
21897 | } | |||
21898 | ||||
21899 | Res = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, Res, | |||
21900 | DAG.getIntPtrConstant(0, dl)); | |||
21901 | ||||
21902 | if (IsStrict) | |||
21903 | return DAG.getMergeValues({Res, Chain}, dl); | |||
21904 | return Res; | |||
21905 | } | |||
21906 | ||||
21907 | // Widen vXi64 fp_to_uint/fp_to_sint with avx512dq to 512-bit source. | |||
21908 | if ((VT == MVT::v2i64 || VT == MVT::v4i64) && | |||
21909 | (SrcVT == MVT::v2f64 || SrcVT == MVT::v4f64 || SrcVT == MVT::v4f32) && | |||
21910 | Subtarget.useAVX512Regs() && Subtarget.hasDQI()) { | |||
21911 | assert(!Subtarget.hasVLX() && "Unexpected features!")(static_cast <bool> (!Subtarget.hasVLX() && "Unexpected features!" ) ? void (0) : __assert_fail ("!Subtarget.hasVLX() && \"Unexpected features!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21911, __extension__ __PRETTY_FUNCTION__)); | |||
21912 | MVT WideVT = SrcVT == MVT::v4f32 ? MVT::v8f32 : MVT::v8f64; | |||
21913 | // Need to concat with zero vector for strict fp to avoid spurious | |||
21914 | // exceptions. | |||
21915 | // TODO: Should we just do this for non-strict as well? | |||
21916 | SDValue Tmp = | |||
21917 | IsStrict ? DAG.getConstantFP(0.0, dl, WideVT) : DAG.getUNDEF(WideVT); | |||
21918 | Src = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, WideVT, Tmp, Src, | |||
21919 | DAG.getIntPtrConstant(0, dl)); | |||
21920 | ||||
21921 | if (IsStrict) { | |||
21922 | Res = DAG.getNode(Op.getOpcode(), dl, {MVT::v8i64, MVT::Other}, | |||
21923 | {Chain, Src}); | |||
21924 | Chain = Res.getValue(1); | |||
21925 | } else { | |||
21926 | Res = DAG.getNode(Op.getOpcode(), dl, MVT::v8i64, Src); | |||
21927 | } | |||
21928 | ||||
21929 | Res = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, Res, | |||
21930 | DAG.getIntPtrConstant(0, dl)); | |||
21931 | ||||
21932 | if (IsStrict) | |||
21933 | return DAG.getMergeValues({Res, Chain}, dl); | |||
21934 | return Res; | |||
21935 | } | |||
21936 | ||||
21937 | if (VT == MVT::v2i64 && SrcVT == MVT::v2f32) { | |||
21938 | if (!Subtarget.hasVLX()) { | |||
21939 | // Non-strict nodes without VLX can we widened to v4f32->v4i64 by type | |||
21940 | // legalizer and then widened again by vector op legalization. | |||
21941 | if (!IsStrict) | |||
21942 | return SDValue(); | |||
21943 | ||||
21944 | SDValue Zero = DAG.getConstantFP(0.0, dl, MVT::v2f32); | |||
21945 | SDValue Tmp = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8f32, | |||
21946 | {Src, Zero, Zero, Zero}); | |||
21947 | Tmp = DAG.getNode(Op.getOpcode(), dl, {MVT::v8i64, MVT::Other}, | |||
21948 | {Chain, Tmp}); | |||
21949 | SDValue Chain = Tmp.getValue(1); | |||
21950 | Tmp = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i64, Tmp, | |||
21951 | DAG.getIntPtrConstant(0, dl)); | |||
21952 | return DAG.getMergeValues({Tmp, Chain}, dl); | |||
21953 | } | |||
21954 | ||||
21955 | assert(Subtarget.hasDQI() && Subtarget.hasVLX() && "Requires AVX512DQVL")(static_cast <bool> (Subtarget.hasDQI() && Subtarget .hasVLX() && "Requires AVX512DQVL") ? void (0) : __assert_fail ("Subtarget.hasDQI() && Subtarget.hasVLX() && \"Requires AVX512DQVL\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21955, __extension__ __PRETTY_FUNCTION__)); | |||
21956 | SDValue Tmp = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v4f32, Src, | |||
21957 | DAG.getUNDEF(MVT::v2f32)); | |||
21958 | if (IsStrict) { | |||
21959 | unsigned Opc = IsSigned ? X86ISD::STRICT_CVTTP2SI | |||
21960 | : X86ISD::STRICT_CVTTP2UI; | |||
21961 | return DAG.getNode(Opc, dl, {VT, MVT::Other}, {Op->getOperand(0), Tmp}); | |||
21962 | } | |||
21963 | unsigned Opc = IsSigned ? X86ISD::CVTTP2SI : X86ISD::CVTTP2UI; | |||
21964 | return DAG.getNode(Opc, dl, VT, Tmp); | |||
21965 | } | |||
21966 | ||||
21967 | // Generate optimized instructions for pre AVX512 unsigned conversions from | |||
21968 | // vXf32 to vXi32. | |||
21969 | if ((VT == MVT::v4i32 && SrcVT == MVT::v4f32) || | |||
21970 | (VT == MVT::v4i32 && SrcVT == MVT::v4f64) || | |||
21971 | (VT == MVT::v8i32 && SrcVT == MVT::v8f32)) { | |||
21972 | assert(!IsSigned && "Expected unsigned conversion!")(static_cast <bool> (!IsSigned && "Expected unsigned conversion!" ) ? void (0) : __assert_fail ("!IsSigned && \"Expected unsigned conversion!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21972, __extension__ __PRETTY_FUNCTION__)); | |||
21973 | return expandFP_TO_UINT_SSE(VT, Src, dl, DAG, Subtarget); | |||
21974 | } | |||
21975 | ||||
21976 | return SDValue(); | |||
21977 | } | |||
21978 | ||||
21979 | assert(!VT.isVector())(static_cast <bool> (!VT.isVector()) ? void (0) : __assert_fail ("!VT.isVector()", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 21979, __extension__ __PRETTY_FUNCTION__)); | |||
21980 | ||||
21981 | bool UseSSEReg = isScalarFPTypeInSSEReg(SrcVT); | |||
21982 | ||||
21983 | if (!IsSigned && UseSSEReg) { | |||
21984 | // Conversions from f32/f64 with AVX512 should be legal. | |||
21985 | if (Subtarget.hasAVX512()) | |||
21986 | return Op; | |||
21987 | ||||
21988 | // We can leverage the specific way the "cvttss2si/cvttsd2si" instruction | |||
21989 | // behaves on out of range inputs to generate optimized conversions. | |||
21990 | if (!IsStrict && ((VT == MVT::i32 && !Subtarget.is64Bit()) || | |||
21991 | (VT == MVT::i64 && Subtarget.is64Bit()))) { | |||
21992 | unsigned DstBits = VT.getScalarSizeInBits(); | |||
21993 | APInt UIntLimit = APInt::getSignMask(DstBits); | |||
21994 | SDValue FloatOffset = DAG.getNode(ISD::UINT_TO_FP, dl, SrcVT, | |||
21995 | DAG.getConstant(UIntLimit, dl, VT)); | |||
21996 | MVT SrcVecVT = MVT::getVectorVT(SrcVT, 128 / SrcVT.getScalarSizeInBits()); | |||
21997 | ||||
21998 | // Calculate the converted result for values in the range: | |||
21999 | // (i32) 0 to 2^31-1 ("Small") and from 2^31 to 2^32-1 ("Big"). | |||
22000 | // (i64) 0 to 2^63-1 ("Small") and from 2^63 to 2^64-1 ("Big"). | |||
22001 | SDValue Small = | |||
22002 | DAG.getNode(X86ISD::CVTTS2SI, dl, VT, | |||
22003 | DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, SrcVecVT, Src)); | |||
22004 | SDValue Big = DAG.getNode( | |||
22005 | X86ISD::CVTTS2SI, dl, VT, | |||
22006 | DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, SrcVecVT, | |||
22007 | DAG.getNode(ISD::FSUB, dl, SrcVT, Src, FloatOffset))); | |||
22008 | ||||
22009 | // The "CVTTS2SI" instruction conveniently sets the sign bit if | |||
22010 | // and only if the value was out of range. So we can use that | |||
22011 | // as our indicator that we rather use "Big" instead of "Small". | |||
22012 | // | |||
22013 | // Use "Small" if "IsOverflown" has all bits cleared | |||
22014 | // and "0x80000000 | Big" if all bits in "IsOverflown" are set. | |||
22015 | SDValue IsOverflown = DAG.getNode( | |||
22016 | ISD::SRA, dl, VT, Small, DAG.getConstant(DstBits - 1, dl, MVT::i8)); | |||
22017 | return DAG.getNode(ISD::OR, dl, VT, Small, | |||
22018 | DAG.getNode(ISD::AND, dl, VT, Big, IsOverflown)); | |||
22019 | } | |||
22020 | ||||
22021 | // Use default expansion for i64. | |||
22022 | if (VT == MVT::i64) | |||
22023 | return SDValue(); | |||
22024 | ||||
22025 | assert(VT == MVT::i32 && "Unexpected VT!")(static_cast <bool> (VT == MVT::i32 && "Unexpected VT!" ) ? void (0) : __assert_fail ("VT == MVT::i32 && \"Unexpected VT!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22025, __extension__ __PRETTY_FUNCTION__)); | |||
22026 | ||||
22027 | // Promote i32 to i64 and use a signed operation on 64-bit targets. | |||
22028 | // FIXME: This does not generate an invalid exception if the input does not | |||
22029 | // fit in i32. PR44019 | |||
22030 | if (Subtarget.is64Bit()) { | |||
22031 | if (IsStrict) { | |||
22032 | Res = DAG.getNode(ISD::STRICT_FP_TO_SINT, dl, {MVT::i64, MVT::Other}, | |||
22033 | {Chain, Src}); | |||
22034 | Chain = Res.getValue(1); | |||
22035 | } else | |||
22036 | Res = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i64, Src); | |||
22037 | ||||
22038 | Res = DAG.getNode(ISD::TRUNCATE, dl, VT, Res); | |||
22039 | if (IsStrict) | |||
22040 | return DAG.getMergeValues({Res, Chain}, dl); | |||
22041 | return Res; | |||
22042 | } | |||
22043 | ||||
22044 | // Use default expansion for SSE1/2 targets without SSE3. With SSE3 we can | |||
22045 | // use fisttp which will be handled later. | |||
22046 | if (!Subtarget.hasSSE3()) | |||
22047 | return SDValue(); | |||
22048 | } | |||
22049 | ||||
22050 | // Promote i16 to i32 if we can use a SSE operation or the type is f128. | |||
22051 | // FIXME: This does not generate an invalid exception if the input does not | |||
22052 | // fit in i16. PR44019 | |||
22053 | if (VT == MVT::i16 && (UseSSEReg || SrcVT == MVT::f128)) { | |||
22054 | assert(IsSigned && "Expected i16 FP_TO_UINT to have been promoted!")(static_cast <bool> (IsSigned && "Expected i16 FP_TO_UINT to have been promoted!" ) ? void (0) : __assert_fail ("IsSigned && \"Expected i16 FP_TO_UINT to have been promoted!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22054, __extension__ __PRETTY_FUNCTION__)); | |||
22055 | if (IsStrict) { | |||
22056 | Res = DAG.getNode(ISD::STRICT_FP_TO_SINT, dl, {MVT::i32, MVT::Other}, | |||
22057 | {Chain, Src}); | |||
22058 | Chain = Res.getValue(1); | |||
22059 | } else | |||
22060 | Res = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Src); | |||
22061 | ||||
22062 | Res = DAG.getNode(ISD::TRUNCATE, dl, VT, Res); | |||
22063 | if (IsStrict) | |||
22064 | return DAG.getMergeValues({Res, Chain}, dl); | |||
22065 | return Res; | |||
22066 | } | |||
22067 | ||||
22068 | // If this is a FP_TO_SINT using SSEReg we're done. | |||
22069 | if (UseSSEReg && IsSigned) | |||
22070 | return Op; | |||
22071 | ||||
22072 | // fp128 needs to use a libcall. | |||
22073 | if (SrcVT == MVT::f128) { | |||
22074 | RTLIB::Libcall LC; | |||
22075 | if (IsSigned) | |||
22076 | LC = RTLIB::getFPTOSINT(SrcVT, VT); | |||
22077 | else | |||
22078 | LC = RTLIB::getFPTOUINT(SrcVT, VT); | |||
22079 | ||||
22080 | MakeLibCallOptions CallOptions; | |||
22081 | std::pair<SDValue, SDValue> Tmp = makeLibCall(DAG, LC, VT, Src, CallOptions, | |||
22082 | SDLoc(Op), Chain); | |||
22083 | ||||
22084 | if (IsStrict) | |||
22085 | return DAG.getMergeValues({ Tmp.first, Tmp.second }, dl); | |||
22086 | ||||
22087 | return Tmp.first; | |||
22088 | } | |||
22089 | ||||
22090 | // Fall back to X87. | |||
22091 | if (SDValue V = FP_TO_INTHelper(Op, DAG, IsSigned, Chain)) { | |||
22092 | if (IsStrict) | |||
22093 | return DAG.getMergeValues({V, Chain}, dl); | |||
22094 | return V; | |||
22095 | } | |||
22096 | ||||
22097 | llvm_unreachable("Expected FP_TO_INTHelper to handle all remaining cases.")::llvm::llvm_unreachable_internal("Expected FP_TO_INTHelper to handle all remaining cases." , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22097); | |||
22098 | } | |||
22099 | ||||
22100 | SDValue X86TargetLowering::LowerLRINT_LLRINT(SDValue Op, | |||
22101 | SelectionDAG &DAG) const { | |||
22102 | SDValue Src = Op.getOperand(0); | |||
22103 | MVT SrcVT = Src.getSimpleValueType(); | |||
22104 | ||||
22105 | // If the source is in an SSE register, the node is Legal. | |||
22106 | if (isScalarFPTypeInSSEReg(SrcVT)) | |||
22107 | return Op; | |||
22108 | ||||
22109 | return LRINT_LLRINTHelper(Op.getNode(), DAG); | |||
22110 | } | |||
22111 | ||||
22112 | SDValue X86TargetLowering::LRINT_LLRINTHelper(SDNode *N, | |||
22113 | SelectionDAG &DAG) const { | |||
22114 | EVT DstVT = N->getValueType(0); | |||
22115 | SDValue Src = N->getOperand(0); | |||
22116 | EVT SrcVT = Src.getValueType(); | |||
22117 | ||||
22118 | if (SrcVT != MVT::f32 && SrcVT != MVT::f64 && SrcVT != MVT::f80) { | |||
22119 | // f16 must be promoted before using the lowering in this routine. | |||
22120 | // fp128 does not use this lowering. | |||
22121 | return SDValue(); | |||
22122 | } | |||
22123 | ||||
22124 | SDLoc DL(N); | |||
22125 | SDValue Chain = DAG.getEntryNode(); | |||
22126 | ||||
22127 | bool UseSSE = isScalarFPTypeInSSEReg(SrcVT); | |||
22128 | ||||
22129 | // If we're converting from SSE, the stack slot needs to hold both types. | |||
22130 | // Otherwise it only needs to hold the DstVT. | |||
22131 | EVT OtherVT = UseSSE ? SrcVT : DstVT; | |||
22132 | SDValue StackPtr = DAG.CreateStackTemporary(DstVT, OtherVT); | |||
22133 | int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex(); | |||
22134 | MachinePointerInfo MPI = | |||
22135 | MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI); | |||
22136 | ||||
22137 | if (UseSSE) { | |||
22138 | assert(DstVT == MVT::i64 && "Invalid LRINT/LLRINT to lower!")(static_cast <bool> (DstVT == MVT::i64 && "Invalid LRINT/LLRINT to lower!" ) ? void (0) : __assert_fail ("DstVT == MVT::i64 && \"Invalid LRINT/LLRINT to lower!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22138, __extension__ __PRETTY_FUNCTION__)); | |||
22139 | Chain = DAG.getStore(Chain, DL, Src, StackPtr, MPI); | |||
22140 | SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other); | |||
22141 | SDValue Ops[] = { Chain, StackPtr }; | |||
22142 | ||||
22143 | Src = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, SrcVT, MPI, | |||
22144 | /*Align*/ None, MachineMemOperand::MOLoad); | |||
22145 | Chain = Src.getValue(1); | |||
22146 | } | |||
22147 | ||||
22148 | SDValue StoreOps[] = { Chain, Src, StackPtr }; | |||
22149 | Chain = DAG.getMemIntrinsicNode(X86ISD::FIST, DL, DAG.getVTList(MVT::Other), | |||
22150 | StoreOps, DstVT, MPI, /*Align*/ None, | |||
22151 | MachineMemOperand::MOStore); | |||
22152 | ||||
22153 | return DAG.getLoad(DstVT, DL, Chain, StackPtr, MPI); | |||
22154 | } | |||
22155 | ||||
22156 | SDValue | |||
22157 | X86TargetLowering::LowerFP_TO_INT_SAT(SDValue Op, SelectionDAG &DAG) const { | |||
22158 | // This is based on the TargetLowering::expandFP_TO_INT_SAT implementation, | |||
22159 | // but making use of X86 specifics to produce better instruction sequences. | |||
22160 | SDNode *Node = Op.getNode(); | |||
22161 | bool IsSigned = Node->getOpcode() == ISD::FP_TO_SINT_SAT; | |||
22162 | unsigned FpToIntOpcode = IsSigned ? ISD::FP_TO_SINT : ISD::FP_TO_UINT; | |||
22163 | SDLoc dl(SDValue(Node, 0)); | |||
22164 | SDValue Src = Node->getOperand(0); | |||
22165 | ||||
22166 | // There are three types involved here: SrcVT is the source floating point | |||
22167 | // type, DstVT is the type of the result, and TmpVT is the result of the | |||
22168 | // intermediate FP_TO_*INT operation we'll use (which may be a promotion of | |||
22169 | // DstVT). | |||
22170 | EVT SrcVT = Src.getValueType(); | |||
22171 | EVT DstVT = Node->getValueType(0); | |||
22172 | EVT TmpVT = DstVT; | |||
22173 | ||||
22174 | // This code is only for floats and doubles. Fall back to generic code for | |||
22175 | // anything else. | |||
22176 | if (!isScalarFPTypeInSSEReg(SrcVT)) | |||
22177 | return SDValue(); | |||
22178 | ||||
22179 | EVT SatVT = cast<VTSDNode>(Node->getOperand(1))->getVT(); | |||
22180 | unsigned SatWidth = SatVT.getScalarSizeInBits(); | |||
22181 | unsigned DstWidth = DstVT.getScalarSizeInBits(); | |||
22182 | unsigned TmpWidth = TmpVT.getScalarSizeInBits(); | |||
22183 | assert(SatWidth <= DstWidth && SatWidth <= TmpWidth &&(static_cast <bool> (SatWidth <= DstWidth && SatWidth <= TmpWidth && "Expected saturation width smaller than result width" ) ? void (0) : __assert_fail ("SatWidth <= DstWidth && SatWidth <= TmpWidth && \"Expected saturation width smaller than result width\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22184, __extension__ __PRETTY_FUNCTION__)) | |||
22184 | "Expected saturation width smaller than result width")(static_cast <bool> (SatWidth <= DstWidth && SatWidth <= TmpWidth && "Expected saturation width smaller than result width" ) ? void (0) : __assert_fail ("SatWidth <= DstWidth && SatWidth <= TmpWidth && \"Expected saturation width smaller than result width\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22184, __extension__ __PRETTY_FUNCTION__)); | |||
22185 | ||||
22186 | // Promote result of FP_TO_*INT to at least 32 bits. | |||
22187 | if (TmpWidth < 32) { | |||
22188 | TmpVT = MVT::i32; | |||
22189 | TmpWidth = 32; | |||
22190 | } | |||
22191 | ||||
22192 | // Promote conversions to unsigned 32-bit to 64-bit, because it will allow | |||
22193 | // us to use a native signed conversion instead. | |||
22194 | if (SatWidth == 32 && !IsSigned && Subtarget.is64Bit()) { | |||
22195 | TmpVT = MVT::i64; | |||
22196 | TmpWidth = 64; | |||
22197 | } | |||
22198 | ||||
22199 | // If the saturation width is smaller than the size of the temporary result, | |||
22200 | // we can always use signed conversion, which is native. | |||
22201 | if (SatWidth < TmpWidth) | |||
22202 | FpToIntOpcode = ISD::FP_TO_SINT; | |||
22203 | ||||
22204 | // Determine minimum and maximum integer values and their corresponding | |||
22205 | // floating-point values. | |||
22206 | APInt MinInt, MaxInt; | |||
22207 | if (IsSigned) { | |||
22208 | MinInt = APInt::getSignedMinValue(SatWidth).sextOrSelf(DstWidth); | |||
22209 | MaxInt = APInt::getSignedMaxValue(SatWidth).sextOrSelf(DstWidth); | |||
22210 | } else { | |||
22211 | MinInt = APInt::getMinValue(SatWidth).zextOrSelf(DstWidth); | |||
22212 | MaxInt = APInt::getMaxValue(SatWidth).zextOrSelf(DstWidth); | |||
22213 | } | |||
22214 | ||||
22215 | APFloat MinFloat(DAG.EVTToAPFloatSemantics(SrcVT)); | |||
22216 | APFloat MaxFloat(DAG.EVTToAPFloatSemantics(SrcVT)); | |||
22217 | ||||
22218 | APFloat::opStatus MinStatus = MinFloat.convertFromAPInt( | |||
22219 | MinInt, IsSigned, APFloat::rmTowardZero); | |||
22220 | APFloat::opStatus MaxStatus = MaxFloat.convertFromAPInt( | |||
22221 | MaxInt, IsSigned, APFloat::rmTowardZero); | |||
22222 | bool AreExactFloatBounds = !(MinStatus & APFloat::opStatus::opInexact) | |||
22223 | && !(MaxStatus & APFloat::opStatus::opInexact); | |||
22224 | ||||
22225 | SDValue MinFloatNode = DAG.getConstantFP(MinFloat, dl, SrcVT); | |||
22226 | SDValue MaxFloatNode = DAG.getConstantFP(MaxFloat, dl, SrcVT); | |||
22227 | ||||
22228 | // If the integer bounds are exactly representable as floats, emit a | |||
22229 | // min+max+fptoi sequence. Otherwise use comparisons and selects. | |||
22230 | if (AreExactFloatBounds) { | |||
22231 | if (DstVT != TmpVT) { | |||
22232 | // Clamp by MinFloat from below. If Src is NaN, propagate NaN. | |||
22233 | SDValue MinClamped = DAG.getNode( | |||
22234 | X86ISD::FMAX, dl, SrcVT, MinFloatNode, Src); | |||
22235 | // Clamp by MaxFloat from above. If Src is NaN, propagate NaN. | |||
22236 | SDValue BothClamped = DAG.getNode( | |||
22237 | X86ISD::FMIN, dl, SrcVT, MaxFloatNode, MinClamped); | |||
22238 | // Convert clamped value to integer. | |||
22239 | SDValue FpToInt = DAG.getNode(FpToIntOpcode, dl, TmpVT, BothClamped); | |||
22240 | ||||
22241 | // NaN will become INDVAL, with the top bit set and the rest zero. | |||
22242 | // Truncation will discard the top bit, resulting in zero. | |||
22243 | return DAG.getNode(ISD::TRUNCATE, dl, DstVT, FpToInt); | |||
22244 | } | |||
22245 | ||||
22246 | // Clamp by MinFloat from below. If Src is NaN, the result is MinFloat. | |||
22247 | SDValue MinClamped = DAG.getNode( | |||
22248 | X86ISD::FMAX, dl, SrcVT, Src, MinFloatNode); | |||
22249 | // Clamp by MaxFloat from above. NaN cannot occur. | |||
22250 | SDValue BothClamped = DAG.getNode( | |||
22251 | X86ISD::FMINC, dl, SrcVT, MinClamped, MaxFloatNode); | |||
22252 | // Convert clamped value to integer. | |||
22253 | SDValue FpToInt = DAG.getNode(FpToIntOpcode, dl, DstVT, BothClamped); | |||
22254 | ||||
22255 | if (!IsSigned) { | |||
22256 | // In the unsigned case we're done, because we mapped NaN to MinFloat, | |||
22257 | // which is zero. | |||
22258 | return FpToInt; | |||
22259 | } | |||
22260 | ||||
22261 | // Otherwise, select zero if Src is NaN. | |||
22262 | SDValue ZeroInt = DAG.getConstant(0, dl, DstVT); | |||
22263 | return DAG.getSelectCC( | |||
22264 | dl, Src, Src, ZeroInt, FpToInt, ISD::CondCode::SETUO); | |||
22265 | } | |||
22266 | ||||
22267 | SDValue MinIntNode = DAG.getConstant(MinInt, dl, DstVT); | |||
22268 | SDValue MaxIntNode = DAG.getConstant(MaxInt, dl, DstVT); | |||
22269 | ||||
22270 | // Result of direct conversion, which may be selected away. | |||
22271 | SDValue FpToInt = DAG.getNode(FpToIntOpcode, dl, TmpVT, Src); | |||
22272 | ||||
22273 | if (DstVT != TmpVT) { | |||
22274 | // NaN will become INDVAL, with the top bit set and the rest zero. | |||
22275 | // Truncation will discard the top bit, resulting in zero. | |||
22276 | FpToInt = DAG.getNode(ISD::TRUNCATE, dl, DstVT, FpToInt); | |||
22277 | } | |||
22278 | ||||
22279 | SDValue Select = FpToInt; | |||
22280 | // For signed conversions where we saturate to the same size as the | |||
22281 | // result type of the fptoi instructions, INDVAL coincides with integer | |||
22282 | // minimum, so we don't need to explicitly check it. | |||
22283 | if (!IsSigned || SatWidth != TmpVT.getScalarSizeInBits()) { | |||
22284 | // If Src ULT MinFloat, select MinInt. In particular, this also selects | |||
22285 | // MinInt if Src is NaN. | |||
22286 | Select = DAG.getSelectCC( | |||
22287 | dl, Src, MinFloatNode, MinIntNode, Select, ISD::CondCode::SETULT); | |||
22288 | } | |||
22289 | ||||
22290 | // If Src OGT MaxFloat, select MaxInt. | |||
22291 | Select = DAG.getSelectCC( | |||
22292 | dl, Src, MaxFloatNode, MaxIntNode, Select, ISD::CondCode::SETOGT); | |||
22293 | ||||
22294 | // In the unsigned case we are done, because we mapped NaN to MinInt, which | |||
22295 | // is already zero. The promoted case was already handled above. | |||
22296 | if (!IsSigned || DstVT != TmpVT) { | |||
22297 | return Select; | |||
22298 | } | |||
22299 | ||||
22300 | // Otherwise, select 0 if Src is NaN. | |||
22301 | SDValue ZeroInt = DAG.getConstant(0, dl, DstVT); | |||
22302 | return DAG.getSelectCC( | |||
22303 | dl, Src, Src, ZeroInt, Select, ISD::CondCode::SETUO); | |||
22304 | } | |||
22305 | ||||
22306 | SDValue X86TargetLowering::LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) const { | |||
22307 | bool IsStrict = Op->isStrictFPOpcode(); | |||
22308 | ||||
22309 | SDLoc DL(Op); | |||
22310 | MVT VT = Op.getSimpleValueType(); | |||
22311 | SDValue In = Op.getOperand(IsStrict ? 1 : 0); | |||
22312 | MVT SVT = In.getSimpleValueType(); | |||
22313 | ||||
22314 | if (VT == MVT::f128) | |||
22315 | return SDValue(); | |||
22316 | ||||
22317 | if (VT == MVT::f80) { | |||
22318 | if (SVT == MVT::f16) { | |||
22319 | assert(Subtarget.hasFP16() && "Unexpected features!")(static_cast <bool> (Subtarget.hasFP16() && "Unexpected features!" ) ? void (0) : __assert_fail ("Subtarget.hasFP16() && \"Unexpected features!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22319, __extension__ __PRETTY_FUNCTION__)); | |||
22320 | RTLIB::Libcall LC = RTLIB::getFPEXT(SVT, VT); | |||
22321 | MakeLibCallOptions CallOptions; | |||
22322 | std::pair<SDValue, SDValue> Tmp = | |||
22323 | makeLibCall(DAG, LC, VT, In, CallOptions, DL, | |||
22324 | IsStrict ? Op.getOperand(0) : SDValue()); | |||
22325 | if (IsStrict) | |||
22326 | return DAG.getMergeValues({Tmp.first, Tmp.second}, DL); | |||
22327 | else | |||
22328 | return Tmp.first; | |||
22329 | } | |||
22330 | return Op; | |||
22331 | } | |||
22332 | ||||
22333 | if (SVT.getVectorElementType() == MVT::f16) { | |||
22334 | assert(Subtarget.hasFP16() && Subtarget.hasVLX() && "Unexpected features!")(static_cast <bool> (Subtarget.hasFP16() && Subtarget .hasVLX() && "Unexpected features!") ? void (0) : __assert_fail ("Subtarget.hasFP16() && Subtarget.hasVLX() && \"Unexpected features!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22334, __extension__ __PRETTY_FUNCTION__)); | |||
22335 | if (SVT == MVT::v2f16) | |||
22336 | In = DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v4f16, In, | |||
22337 | DAG.getUNDEF(MVT::v2f16)); | |||
22338 | SDValue Res = DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v8f16, In, | |||
22339 | DAG.getUNDEF(MVT::v4f16)); | |||
22340 | if (IsStrict) | |||
22341 | return DAG.getNode(X86ISD::STRICT_VFPEXT, DL, {VT, MVT::Other}, | |||
22342 | {Op->getOperand(0), Res}); | |||
22343 | return DAG.getNode(X86ISD::VFPEXT, DL, VT, Res); | |||
22344 | } | |||
22345 | ||||
22346 | assert(SVT == MVT::v2f32 && "Only customize MVT::v2f32 type legalization!")(static_cast <bool> (SVT == MVT::v2f32 && "Only customize MVT::v2f32 type legalization!" ) ? void (0) : __assert_fail ("SVT == MVT::v2f32 && \"Only customize MVT::v2f32 type legalization!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22346, __extension__ __PRETTY_FUNCTION__)); | |||
22347 | ||||
22348 | SDValue Res = | |||
22349 | DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v4f32, In, DAG.getUNDEF(SVT)); | |||
22350 | if (IsStrict) | |||
22351 | return DAG.getNode(X86ISD::STRICT_VFPEXT, DL, {VT, MVT::Other}, | |||
22352 | {Op->getOperand(0), Res}); | |||
22353 | return DAG.getNode(X86ISD::VFPEXT, DL, VT, Res); | |||
22354 | } | |||
22355 | ||||
22356 | SDValue X86TargetLowering::LowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const { | |||
22357 | bool IsStrict = Op->isStrictFPOpcode(); | |||
22358 | SDValue In = Op.getOperand(IsStrict ? 1 : 0); | |||
22359 | MVT VT = Op.getSimpleValueType(); | |||
22360 | MVT SVT = In.getSimpleValueType(); | |||
22361 | ||||
22362 | // It's legal except when f128 is involved or we're converting f80->f16. | |||
22363 | if (SVT != MVT::f128 && !(VT == MVT::f16 && SVT == MVT::f80)) | |||
22364 | return Op; | |||
22365 | ||||
22366 | return SDValue(); | |||
22367 | } | |||
22368 | ||||
22369 | static SDValue LowerFP16_TO_FP(SDValue Op, SelectionDAG &DAG) { | |||
22370 | bool IsStrict = Op->isStrictFPOpcode(); | |||
22371 | SDValue Src = Op.getOperand(IsStrict ? 1 : 0); | |||
22372 | assert(Src.getValueType() == MVT::i16 && Op.getValueType() == MVT::f32 &&(static_cast <bool> (Src.getValueType() == MVT::i16 && Op.getValueType() == MVT::f32 && "Unexpected VT!") ? void (0) : __assert_fail ("Src.getValueType() == MVT::i16 && Op.getValueType() == MVT::f32 && \"Unexpected VT!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22373, __extension__ __PRETTY_FUNCTION__)) | |||
22373 | "Unexpected VT!")(static_cast <bool> (Src.getValueType() == MVT::i16 && Op.getValueType() == MVT::f32 && "Unexpected VT!") ? void (0) : __assert_fail ("Src.getValueType() == MVT::i16 && Op.getValueType() == MVT::f32 && \"Unexpected VT!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22373, __extension__ __PRETTY_FUNCTION__)); | |||
22374 | ||||
22375 | SDLoc dl(Op); | |||
22376 | SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, | |||
22377 | DAG.getConstant(0, dl, MVT::v8i16), Src, | |||
22378 | DAG.getIntPtrConstant(0, dl)); | |||
22379 | ||||
22380 | SDValue Chain; | |||
22381 | if (IsStrict) { | |||
22382 | Res = DAG.getNode(X86ISD::STRICT_CVTPH2PS, dl, {MVT::v4f32, MVT::Other}, | |||
22383 | {Op.getOperand(0), Res}); | |||
22384 | Chain = Res.getValue(1); | |||
22385 | } else { | |||
22386 | Res = DAG.getNode(X86ISD::CVTPH2PS, dl, MVT::v4f32, Res); | |||
22387 | } | |||
22388 | ||||
22389 | Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res, | |||
22390 | DAG.getIntPtrConstant(0, dl)); | |||
22391 | ||||
22392 | if (IsStrict) | |||
22393 | return DAG.getMergeValues({Res, Chain}, dl); | |||
22394 | ||||
22395 | return Res; | |||
22396 | } | |||
22397 | ||||
22398 | static SDValue LowerFP_TO_FP16(SDValue Op, SelectionDAG &DAG) { | |||
22399 | bool IsStrict = Op->isStrictFPOpcode(); | |||
22400 | SDValue Src = Op.getOperand(IsStrict ? 1 : 0); | |||
22401 | assert(Src.getValueType() == MVT::f32 && Op.getValueType() == MVT::i16 &&(static_cast <bool> (Src.getValueType() == MVT::f32 && Op.getValueType() == MVT::i16 && "Unexpected VT!") ? void (0) : __assert_fail ("Src.getValueType() == MVT::f32 && Op.getValueType() == MVT::i16 && \"Unexpected VT!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22402, __extension__ __PRETTY_FUNCTION__)) | |||
22402 | "Unexpected VT!")(static_cast <bool> (Src.getValueType() == MVT::f32 && Op.getValueType() == MVT::i16 && "Unexpected VT!") ? void (0) : __assert_fail ("Src.getValueType() == MVT::f32 && Op.getValueType() == MVT::i16 && \"Unexpected VT!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22402, __extension__ __PRETTY_FUNCTION__)); | |||
22403 | ||||
22404 | SDLoc dl(Op); | |||
22405 | SDValue Res, Chain; | |||
22406 | if (IsStrict) { | |||
22407 | Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v4f32, | |||
22408 | DAG.getConstantFP(0, dl, MVT::v4f32), Src, | |||
22409 | DAG.getIntPtrConstant(0, dl)); | |||
22410 | Res = DAG.getNode( | |||
22411 | X86ISD::STRICT_CVTPS2PH, dl, {MVT::v8i16, MVT::Other}, | |||
22412 | {Op.getOperand(0), Res, DAG.getTargetConstant(4, dl, MVT::i32)}); | |||
22413 | Chain = Res.getValue(1); | |||
22414 | } else { | |||
22415 | // FIXME: Should we use zeros for upper elements for non-strict? | |||
22416 | Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, Src); | |||
22417 | Res = DAG.getNode(X86ISD::CVTPS2PH, dl, MVT::v8i16, Res, | |||
22418 | DAG.getTargetConstant(4, dl, MVT::i32)); | |||
22419 | } | |||
22420 | ||||
22421 | Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Res, | |||
22422 | DAG.getIntPtrConstant(0, dl)); | |||
22423 | ||||
22424 | if (IsStrict) | |||
22425 | return DAG.getMergeValues({Res, Chain}, dl); | |||
22426 | ||||
22427 | return Res; | |||
22428 | } | |||
22429 | ||||
22430 | /// Depending on uarch and/or optimizing for size, we might prefer to use a | |||
22431 | /// vector operation in place of the typical scalar operation. | |||
22432 | static SDValue lowerAddSubToHorizontalOp(SDValue Op, SelectionDAG &DAG, | |||
22433 | const X86Subtarget &Subtarget) { | |||
22434 | // If both operands have other uses, this is probably not profitable. | |||
22435 | SDValue LHS = Op.getOperand(0); | |||
22436 | SDValue RHS = Op.getOperand(1); | |||
22437 | if (!LHS.hasOneUse() && !RHS.hasOneUse()) | |||
22438 | return Op; | |||
22439 | ||||
22440 | // FP horizontal add/sub were added with SSE3. Integer with SSSE3. | |||
22441 | bool IsFP = Op.getSimpleValueType().isFloatingPoint(); | |||
22442 | if (IsFP && !Subtarget.hasSSE3()) | |||
22443 | return Op; | |||
22444 | if (!IsFP && !Subtarget.hasSSSE3()) | |||
22445 | return Op; | |||
22446 | ||||
22447 | // Extract from a common vector. | |||
22448 | if (LHS.getOpcode() != ISD::EXTRACT_VECTOR_ELT || | |||
22449 | RHS.getOpcode() != ISD::EXTRACT_VECTOR_ELT || | |||
22450 | LHS.getOperand(0) != RHS.getOperand(0) || | |||
22451 | !isa<ConstantSDNode>(LHS.getOperand(1)) || | |||
22452 | !isa<ConstantSDNode>(RHS.getOperand(1)) || | |||
22453 | !shouldUseHorizontalOp(true, DAG, Subtarget)) | |||
22454 | return Op; | |||
22455 | ||||
22456 | // Allow commuted 'hadd' ops. | |||
22457 | // TODO: Allow commuted (f)sub by negating the result of (F)HSUB? | |||
22458 | unsigned HOpcode; | |||
22459 | switch (Op.getOpcode()) { | |||
22460 | case ISD::ADD: HOpcode = X86ISD::HADD; break; | |||
22461 | case ISD::SUB: HOpcode = X86ISD::HSUB; break; | |||
22462 | case ISD::FADD: HOpcode = X86ISD::FHADD; break; | |||
22463 | case ISD::FSUB: HOpcode = X86ISD::FHSUB; break; | |||
22464 | default: | |||
22465 | llvm_unreachable("Trying to lower unsupported opcode to horizontal op")::llvm::llvm_unreachable_internal("Trying to lower unsupported opcode to horizontal op" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22465); | |||
22466 | } | |||
22467 | unsigned LExtIndex = LHS.getConstantOperandVal(1); | |||
22468 | unsigned RExtIndex = RHS.getConstantOperandVal(1); | |||
22469 | if ((LExtIndex & 1) == 1 && (RExtIndex & 1) == 0 && | |||
22470 | (HOpcode == X86ISD::HADD || HOpcode == X86ISD::FHADD)) | |||
22471 | std::swap(LExtIndex, RExtIndex); | |||
22472 | ||||
22473 | if ((LExtIndex & 1) != 0 || RExtIndex != (LExtIndex + 1)) | |||
22474 | return Op; | |||
22475 | ||||
22476 | SDValue X = LHS.getOperand(0); | |||
22477 | EVT VecVT = X.getValueType(); | |||
22478 | unsigned BitWidth = VecVT.getSizeInBits(); | |||
22479 | unsigned NumLanes = BitWidth / 128; | |||
22480 | unsigned NumEltsPerLane = VecVT.getVectorNumElements() / NumLanes; | |||
22481 | assert((BitWidth == 128 || BitWidth == 256 || BitWidth == 512) &&(static_cast <bool> ((BitWidth == 128 || BitWidth == 256 || BitWidth == 512) && "Not expecting illegal vector widths here" ) ? void (0) : __assert_fail ("(BitWidth == 128 || BitWidth == 256 || BitWidth == 512) && \"Not expecting illegal vector widths here\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22482, __extension__ __PRETTY_FUNCTION__)) | |||
22482 | "Not expecting illegal vector widths here")(static_cast <bool> ((BitWidth == 128 || BitWidth == 256 || BitWidth == 512) && "Not expecting illegal vector widths here" ) ? void (0) : __assert_fail ("(BitWidth == 128 || BitWidth == 256 || BitWidth == 512) && \"Not expecting illegal vector widths here\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22482, __extension__ __PRETTY_FUNCTION__)); | |||
22483 | ||||
22484 | // Creating a 256-bit horizontal op would be wasteful, and there is no 512-bit | |||
22485 | // equivalent, so extract the 256/512-bit source op to 128-bit if we can. | |||
22486 | SDLoc DL(Op); | |||
22487 | if (BitWidth == 256 || BitWidth == 512) { | |||
22488 | unsigned LaneIdx = LExtIndex / NumEltsPerLane; | |||
22489 | X = extract128BitVector(X, LaneIdx * NumEltsPerLane, DAG, DL); | |||
22490 | LExtIndex %= NumEltsPerLane; | |||
22491 | } | |||
22492 | ||||
22493 | // add (extractelt (X, 0), extractelt (X, 1)) --> extractelt (hadd X, X), 0 | |||
22494 | // add (extractelt (X, 1), extractelt (X, 0)) --> extractelt (hadd X, X), 0 | |||
22495 | // add (extractelt (X, 2), extractelt (X, 3)) --> extractelt (hadd X, X), 1 | |||
22496 | // sub (extractelt (X, 0), extractelt (X, 1)) --> extractelt (hsub X, X), 0 | |||
22497 | SDValue HOp = DAG.getNode(HOpcode, DL, X.getValueType(), X, X); | |||
22498 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, Op.getSimpleValueType(), HOp, | |||
22499 | DAG.getIntPtrConstant(LExtIndex / 2, DL)); | |||
22500 | } | |||
22501 | ||||
22502 | /// Depending on uarch and/or optimizing for size, we might prefer to use a | |||
22503 | /// vector operation in place of the typical scalar operation. | |||
22504 | SDValue X86TargetLowering::lowerFaddFsub(SDValue Op, SelectionDAG &DAG) const { | |||
22505 | assert((Op.getValueType() == MVT::f32 || Op.getValueType() == MVT::f64) &&(static_cast <bool> ((Op.getValueType() == MVT::f32 || Op .getValueType() == MVT::f64) && "Only expecting float/double" ) ? void (0) : __assert_fail ("(Op.getValueType() == MVT::f32 || Op.getValueType() == MVT::f64) && \"Only expecting float/double\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22506, __extension__ __PRETTY_FUNCTION__)) | |||
22506 | "Only expecting float/double")(static_cast <bool> ((Op.getValueType() == MVT::f32 || Op .getValueType() == MVT::f64) && "Only expecting float/double" ) ? void (0) : __assert_fail ("(Op.getValueType() == MVT::f32 || Op.getValueType() == MVT::f64) && \"Only expecting float/double\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22506, __extension__ __PRETTY_FUNCTION__)); | |||
22507 | return lowerAddSubToHorizontalOp(Op, DAG, Subtarget); | |||
22508 | } | |||
22509 | ||||
22510 | /// ISD::FROUND is defined to round to nearest with ties rounding away from 0. | |||
22511 | /// This mode isn't supported in hardware on X86. But as long as we aren't | |||
22512 | /// compiling with trapping math, we can emulate this with | |||
22513 | /// floor(X + copysign(nextafter(0.5, 0.0), X)). | |||
22514 | static SDValue LowerFROUND(SDValue Op, SelectionDAG &DAG) { | |||
22515 | if (Op.getOpcode() == ISD::STRICT_FROUND && | |||
22516 | Op.getSimpleValueType() == MVT::f16) | |||
22517 | report_fatal_error("For now cannot emit strict round(fp16) at backend for " | |||
22518 | "lacking library support."); | |||
22519 | SDValue N0 = Op.getOperand(0); | |||
22520 | SDLoc dl(Op); | |||
22521 | MVT VT = Op.getSimpleValueType(); | |||
22522 | ||||
22523 | // N0 += copysign(nextafter(0.5, 0.0), N0) | |||
22524 | const fltSemantics &Sem = SelectionDAG::EVTToAPFloatSemantics(VT); | |||
22525 | bool Ignored; | |||
22526 | APFloat Point5Pred = APFloat(0.5f); | |||
22527 | Point5Pred.convert(Sem, APFloat::rmNearestTiesToEven, &Ignored); | |||
22528 | Point5Pred.next(/*nextDown*/true); | |||
22529 | ||||
22530 | SDValue Adder = DAG.getNode(ISD::FCOPYSIGN, dl, VT, | |||
22531 | DAG.getConstantFP(Point5Pred, dl, VT), N0); | |||
22532 | N0 = DAG.getNode(ISD::FADD, dl, VT, N0, Adder); | |||
22533 | ||||
22534 | // Truncate the result to remove fraction. | |||
22535 | return DAG.getNode(ISD::FTRUNC, dl, VT, N0); | |||
22536 | } | |||
22537 | ||||
22538 | /// The only differences between FABS and FNEG are the mask and the logic op. | |||
22539 | /// FNEG also has a folding opportunity for FNEG(FABS(x)). | |||
22540 | static SDValue LowerFABSorFNEG(SDValue Op, SelectionDAG &DAG) { | |||
22541 | assert((Op.getOpcode() == ISD::FABS || Op.getOpcode() == ISD::FNEG) &&(static_cast <bool> ((Op.getOpcode() == ISD::FABS || Op .getOpcode() == ISD::FNEG) && "Wrong opcode for lowering FABS or FNEG." ) ? void (0) : __assert_fail ("(Op.getOpcode() == ISD::FABS || Op.getOpcode() == ISD::FNEG) && \"Wrong opcode for lowering FABS or FNEG.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22542, __extension__ __PRETTY_FUNCTION__)) | |||
22542 | "Wrong opcode for lowering FABS or FNEG.")(static_cast <bool> ((Op.getOpcode() == ISD::FABS || Op .getOpcode() == ISD::FNEG) && "Wrong opcode for lowering FABS or FNEG." ) ? void (0) : __assert_fail ("(Op.getOpcode() == ISD::FABS || Op.getOpcode() == ISD::FNEG) && \"Wrong opcode for lowering FABS or FNEG.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22542, __extension__ __PRETTY_FUNCTION__)); | |||
22543 | ||||
22544 | bool IsFABS = (Op.getOpcode() == ISD::FABS); | |||
22545 | ||||
22546 | // If this is a FABS and it has an FNEG user, bail out to fold the combination | |||
22547 | // into an FNABS. We'll lower the FABS after that if it is still in use. | |||
22548 | if (IsFABS) | |||
22549 | for (SDNode *User : Op->uses()) | |||
22550 | if (User->getOpcode() == ISD::FNEG) | |||
22551 | return Op; | |||
22552 | ||||
22553 | SDLoc dl(Op); | |||
22554 | MVT VT = Op.getSimpleValueType(); | |||
22555 | ||||
22556 | bool IsF128 = (VT == MVT::f128); | |||
22557 | assert(VT.isFloatingPoint() && VT != MVT::f80 &&(static_cast <bool> (VT.isFloatingPoint() && VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal (VT) && "Unexpected type in LowerFABSorFNEG") ? void ( 0) : __assert_fail ("VT.isFloatingPoint() && VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) && \"Unexpected type in LowerFABSorFNEG\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22559, __extension__ __PRETTY_FUNCTION__)) | |||
22558 | DAG.getTargetLoweringInfo().isTypeLegal(VT) &&(static_cast <bool> (VT.isFloatingPoint() && VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal (VT) && "Unexpected type in LowerFABSorFNEG") ? void ( 0) : __assert_fail ("VT.isFloatingPoint() && VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) && \"Unexpected type in LowerFABSorFNEG\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22559, __extension__ __PRETTY_FUNCTION__)) | |||
22559 | "Unexpected type in LowerFABSorFNEG")(static_cast <bool> (VT.isFloatingPoint() && VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal (VT) && "Unexpected type in LowerFABSorFNEG") ? void ( 0) : __assert_fail ("VT.isFloatingPoint() && VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) && \"Unexpected type in LowerFABSorFNEG\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22559, __extension__ __PRETTY_FUNCTION__)); | |||
22560 | ||||
22561 | // FIXME: Use function attribute "OptimizeForSize" and/or CodeGenOpt::Level to | |||
22562 | // decide if we should generate a 16-byte constant mask when we only need 4 or | |||
22563 | // 8 bytes for the scalar case. | |||
22564 | ||||
22565 | // There are no scalar bitwise logical SSE/AVX instructions, so we | |||
22566 | // generate a 16-byte vector constant and logic op even for the scalar case. | |||
22567 | // Using a 16-byte mask allows folding the load of the mask with | |||
22568 | // the logic op, so it can save (~4 bytes) on code size. | |||
22569 | bool IsFakeVector = !VT.isVector() && !IsF128; | |||
22570 | MVT LogicVT = VT; | |||
22571 | if (IsFakeVector) | |||
22572 | LogicVT = (VT == MVT::f64) ? MVT::v2f64 | |||
22573 | : (VT == MVT::f32) ? MVT::v4f32 | |||
22574 | : MVT::v8f16; | |||
22575 | ||||
22576 | unsigned EltBits = VT.getScalarSizeInBits(); | |||
22577 | // For FABS, mask is 0x7f...; for FNEG, mask is 0x80... | |||
22578 | APInt MaskElt = IsFABS ? APInt::getSignedMaxValue(EltBits) : | |||
22579 | APInt::getSignMask(EltBits); | |||
22580 | const fltSemantics &Sem = SelectionDAG::EVTToAPFloatSemantics(VT); | |||
22581 | SDValue Mask = DAG.getConstantFP(APFloat(Sem, MaskElt), dl, LogicVT); | |||
22582 | ||||
22583 | SDValue Op0 = Op.getOperand(0); | |||
22584 | bool IsFNABS = !IsFABS && (Op0.getOpcode() == ISD::FABS); | |||
22585 | unsigned LogicOp = IsFABS ? X86ISD::FAND : | |||
22586 | IsFNABS ? X86ISD::FOR : | |||
22587 | X86ISD::FXOR; | |||
22588 | SDValue Operand = IsFNABS ? Op0.getOperand(0) : Op0; | |||
22589 | ||||
22590 | if (VT.isVector() || IsF128) | |||
22591 | return DAG.getNode(LogicOp, dl, LogicVT, Operand, Mask); | |||
22592 | ||||
22593 | // For the scalar case extend to a 128-bit vector, perform the logic op, | |||
22594 | // and extract the scalar result back out. | |||
22595 | Operand = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LogicVT, Operand); | |||
22596 | SDValue LogicNode = DAG.getNode(LogicOp, dl, LogicVT, Operand, Mask); | |||
22597 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, LogicNode, | |||
22598 | DAG.getIntPtrConstant(0, dl)); | |||
22599 | } | |||
22600 | ||||
22601 | static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) { | |||
22602 | SDValue Mag = Op.getOperand(0); | |||
22603 | SDValue Sign = Op.getOperand(1); | |||
22604 | SDLoc dl(Op); | |||
22605 | ||||
22606 | // If the sign operand is smaller, extend it first. | |||
22607 | MVT VT = Op.getSimpleValueType(); | |||
22608 | if (Sign.getSimpleValueType().bitsLT(VT)) | |||
22609 | Sign = DAG.getNode(ISD::FP_EXTEND, dl, VT, Sign); | |||
22610 | ||||
22611 | // And if it is bigger, shrink it first. | |||
22612 | if (Sign.getSimpleValueType().bitsGT(VT)) | |||
22613 | Sign = | |||
22614 | DAG.getNode(ISD::FP_ROUND, dl, VT, Sign, DAG.getIntPtrConstant(0, dl)); | |||
22615 | ||||
22616 | // At this point the operands and the result should have the same | |||
22617 | // type, and that won't be f80 since that is not custom lowered. | |||
22618 | bool IsF128 = (VT == MVT::f128); | |||
22619 | assert(VT.isFloatingPoint() && VT != MVT::f80 &&(static_cast <bool> (VT.isFloatingPoint() && VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal (VT) && "Unexpected type in LowerFCOPYSIGN") ? void ( 0) : __assert_fail ("VT.isFloatingPoint() && VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) && \"Unexpected type in LowerFCOPYSIGN\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22621, __extension__ __PRETTY_FUNCTION__)) | |||
22620 | DAG.getTargetLoweringInfo().isTypeLegal(VT) &&(static_cast <bool> (VT.isFloatingPoint() && VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal (VT) && "Unexpected type in LowerFCOPYSIGN") ? void ( 0) : __assert_fail ("VT.isFloatingPoint() && VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) && \"Unexpected type in LowerFCOPYSIGN\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22621, __extension__ __PRETTY_FUNCTION__)) | |||
22621 | "Unexpected type in LowerFCOPYSIGN")(static_cast <bool> (VT.isFloatingPoint() && VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal (VT) && "Unexpected type in LowerFCOPYSIGN") ? void ( 0) : __assert_fail ("VT.isFloatingPoint() && VT != MVT::f80 && DAG.getTargetLoweringInfo().isTypeLegal(VT) && \"Unexpected type in LowerFCOPYSIGN\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22621, __extension__ __PRETTY_FUNCTION__)); | |||
22622 | ||||
22623 | const fltSemantics &Sem = SelectionDAG::EVTToAPFloatSemantics(VT); | |||
22624 | ||||
22625 | // Perform all scalar logic operations as 16-byte vectors because there are no | |||
22626 | // scalar FP logic instructions in SSE. | |||
22627 | // TODO: This isn't necessary. If we used scalar types, we might avoid some | |||
22628 | // unnecessary splats, but we might miss load folding opportunities. Should | |||
22629 | // this decision be based on OptimizeForSize? | |||
22630 | bool IsFakeVector = !VT.isVector() && !IsF128; | |||
22631 | MVT LogicVT = VT; | |||
22632 | if (IsFakeVector) | |||
22633 | LogicVT = (VT == MVT::f64) ? MVT::v2f64 | |||
22634 | : (VT == MVT::f32) ? MVT::v4f32 | |||
22635 | : MVT::v8f16; | |||
22636 | ||||
22637 | // The mask constants are automatically splatted for vector types. | |||
22638 | unsigned EltSizeInBits = VT.getScalarSizeInBits(); | |||
22639 | SDValue SignMask = DAG.getConstantFP( | |||
22640 | APFloat(Sem, APInt::getSignMask(EltSizeInBits)), dl, LogicVT); | |||
22641 | SDValue MagMask = DAG.getConstantFP( | |||
22642 | APFloat(Sem, APInt::getSignedMaxValue(EltSizeInBits)), dl, LogicVT); | |||
22643 | ||||
22644 | // First, clear all bits but the sign bit from the second operand (sign). | |||
22645 | if (IsFakeVector) | |||
22646 | Sign = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LogicVT, Sign); | |||
22647 | SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, LogicVT, Sign, SignMask); | |||
22648 | ||||
22649 | // Next, clear the sign bit from the first operand (magnitude). | |||
22650 | // TODO: If we had general constant folding for FP logic ops, this check | |||
22651 | // wouldn't be necessary. | |||
22652 | SDValue MagBits; | |||
22653 | if (ConstantFPSDNode *Op0CN = isConstOrConstSplatFP(Mag)) { | |||
22654 | APFloat APF = Op0CN->getValueAPF(); | |||
22655 | APF.clearSign(); | |||
22656 | MagBits = DAG.getConstantFP(APF, dl, LogicVT); | |||
22657 | } else { | |||
22658 | // If the magnitude operand wasn't a constant, we need to AND out the sign. | |||
22659 | if (IsFakeVector) | |||
22660 | Mag = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LogicVT, Mag); | |||
22661 | MagBits = DAG.getNode(X86ISD::FAND, dl, LogicVT, Mag, MagMask); | |||
22662 | } | |||
22663 | ||||
22664 | // OR the magnitude value with the sign bit. | |||
22665 | SDValue Or = DAG.getNode(X86ISD::FOR, dl, LogicVT, MagBits, SignBit); | |||
22666 | return !IsFakeVector ? Or : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Or, | |||
22667 | DAG.getIntPtrConstant(0, dl)); | |||
22668 | } | |||
22669 | ||||
22670 | static SDValue LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) { | |||
22671 | SDValue N0 = Op.getOperand(0); | |||
22672 | SDLoc dl(Op); | |||
22673 | MVT VT = Op.getSimpleValueType(); | |||
22674 | ||||
22675 | MVT OpVT = N0.getSimpleValueType(); | |||
22676 | assert((OpVT == MVT::f32 || OpVT == MVT::f64) &&(static_cast <bool> ((OpVT == MVT::f32 || OpVT == MVT:: f64) && "Unexpected type for FGETSIGN") ? void (0) : __assert_fail ("(OpVT == MVT::f32 || OpVT == MVT::f64) && \"Unexpected type for FGETSIGN\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22677, __extension__ __PRETTY_FUNCTION__)) | |||
22677 | "Unexpected type for FGETSIGN")(static_cast <bool> ((OpVT == MVT::f32 || OpVT == MVT:: f64) && "Unexpected type for FGETSIGN") ? void (0) : __assert_fail ("(OpVT == MVT::f32 || OpVT == MVT::f64) && \"Unexpected type for FGETSIGN\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22677, __extension__ __PRETTY_FUNCTION__)); | |||
22678 | ||||
22679 | // Lower ISD::FGETSIGN to (AND (X86ISD::MOVMSK ...) 1). | |||
22680 | MVT VecVT = (OpVT == MVT::f32 ? MVT::v4f32 : MVT::v2f64); | |||
22681 | SDValue Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, N0); | |||
22682 | Res = DAG.getNode(X86ISD::MOVMSK, dl, MVT::i32, Res); | |||
22683 | Res = DAG.getZExtOrTrunc(Res, dl, VT); | |||
22684 | Res = DAG.getNode(ISD::AND, dl, VT, Res, DAG.getConstant(1, dl, VT)); | |||
22685 | return Res; | |||
22686 | } | |||
22687 | ||||
22688 | /// Helper for creating a X86ISD::SETCC node. | |||
22689 | static SDValue getSETCC(X86::CondCode Cond, SDValue EFLAGS, const SDLoc &dl, | |||
22690 | SelectionDAG &DAG) { | |||
22691 | return DAG.getNode(X86ISD::SETCC, dl, MVT::i8, | |||
22692 | DAG.getTargetConstant(Cond, dl, MVT::i8), EFLAGS); | |||
22693 | } | |||
22694 | ||||
22695 | /// Helper for matching OR(EXTRACTELT(X,0),OR(EXTRACTELT(X,1),...)) | |||
22696 | /// style scalarized (associative) reduction patterns. Partial reductions | |||
22697 | /// are supported when the pointer SrcMask is non-null. | |||
22698 | /// TODO - move this to SelectionDAG? | |||
22699 | static bool matchScalarReduction(SDValue Op, ISD::NodeType BinOp, | |||
22700 | SmallVectorImpl<SDValue> &SrcOps, | |||
22701 | SmallVectorImpl<APInt> *SrcMask = nullptr) { | |||
22702 | SmallVector<SDValue, 8> Opnds; | |||
22703 | DenseMap<SDValue, APInt> SrcOpMap; | |||
22704 | EVT VT = MVT::Other; | |||
22705 | ||||
22706 | // Recognize a special case where a vector is casted into wide integer to | |||
22707 | // test all 0s. | |||
22708 | assert(Op.getOpcode() == unsigned(BinOp) &&(static_cast <bool> (Op.getOpcode() == unsigned(BinOp) && "Unexpected bit reduction opcode") ? void (0) : __assert_fail ("Op.getOpcode() == unsigned(BinOp) && \"Unexpected bit reduction opcode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22709, __extension__ __PRETTY_FUNCTION__)) | |||
22709 | "Unexpected bit reduction opcode")(static_cast <bool> (Op.getOpcode() == unsigned(BinOp) && "Unexpected bit reduction opcode") ? void (0) : __assert_fail ("Op.getOpcode() == unsigned(BinOp) && \"Unexpected bit reduction opcode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22709, __extension__ __PRETTY_FUNCTION__)); | |||
22710 | Opnds.push_back(Op.getOperand(0)); | |||
22711 | Opnds.push_back(Op.getOperand(1)); | |||
22712 | ||||
22713 | for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) { | |||
22714 | SmallVectorImpl<SDValue>::const_iterator I = Opnds.begin() + Slot; | |||
22715 | // BFS traverse all BinOp operands. | |||
22716 | if (I->getOpcode() == unsigned(BinOp)) { | |||
22717 | Opnds.push_back(I->getOperand(0)); | |||
22718 | Opnds.push_back(I->getOperand(1)); | |||
22719 | // Re-evaluate the number of nodes to be traversed. | |||
22720 | e += 2; // 2 more nodes (LHS and RHS) are pushed. | |||
22721 | continue; | |||
22722 | } | |||
22723 | ||||
22724 | // Quit if a non-EXTRACT_VECTOR_ELT | |||
22725 | if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT) | |||
22726 | return false; | |||
22727 | ||||
22728 | // Quit if without a constant index. | |||
22729 | auto *Idx = dyn_cast<ConstantSDNode>(I->getOperand(1)); | |||
22730 | if (!Idx) | |||
22731 | return false; | |||
22732 | ||||
22733 | SDValue Src = I->getOperand(0); | |||
22734 | DenseMap<SDValue, APInt>::iterator M = SrcOpMap.find(Src); | |||
22735 | if (M == SrcOpMap.end()) { | |||
22736 | VT = Src.getValueType(); | |||
22737 | // Quit if not the same type. | |||
22738 | if (!SrcOpMap.empty() && VT != SrcOpMap.begin()->first.getValueType()) | |||
22739 | return false; | |||
22740 | unsigned NumElts = VT.getVectorNumElements(); | |||
22741 | APInt EltCount = APInt::getZero(NumElts); | |||
22742 | M = SrcOpMap.insert(std::make_pair(Src, EltCount)).first; | |||
22743 | SrcOps.push_back(Src); | |||
22744 | } | |||
22745 | ||||
22746 | // Quit if element already used. | |||
22747 | unsigned CIdx = Idx->getZExtValue(); | |||
22748 | if (M->second[CIdx]) | |||
22749 | return false; | |||
22750 | M->second.setBit(CIdx); | |||
22751 | } | |||
22752 | ||||
22753 | if (SrcMask) { | |||
22754 | // Collect the source partial masks. | |||
22755 | for (SDValue &SrcOp : SrcOps) | |||
22756 | SrcMask->push_back(SrcOpMap[SrcOp]); | |||
22757 | } else { | |||
22758 | // Quit if not all elements are used. | |||
22759 | for (const auto &I : SrcOpMap) | |||
22760 | if (!I.second.isAllOnes()) | |||
22761 | return false; | |||
22762 | } | |||
22763 | ||||
22764 | return true; | |||
22765 | } | |||
22766 | ||||
22767 | // Helper function for comparing all bits of a vector against zero. | |||
22768 | static SDValue LowerVectorAllZero(const SDLoc &DL, SDValue V, ISD::CondCode CC, | |||
22769 | const APInt &Mask, | |||
22770 | const X86Subtarget &Subtarget, | |||
22771 | SelectionDAG &DAG, X86::CondCode &X86CC) { | |||
22772 | EVT VT = V.getValueType(); | |||
22773 | unsigned ScalarSize = VT.getScalarSizeInBits(); | |||
22774 | if (Mask.getBitWidth() != ScalarSize) { | |||
22775 | assert(ScalarSize == 1 && "Element Mask vs Vector bitwidth mismatch")(static_cast <bool> (ScalarSize == 1 && "Element Mask vs Vector bitwidth mismatch" ) ? void (0) : __assert_fail ("ScalarSize == 1 && \"Element Mask vs Vector bitwidth mismatch\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22775, __extension__ __PRETTY_FUNCTION__)); | |||
22776 | return SDValue(); | |||
22777 | } | |||
22778 | ||||
22779 | assert((CC == ISD::SETEQ || CC == ISD::SETNE) && "Unsupported ISD::CondCode")(static_cast <bool> ((CC == ISD::SETEQ || CC == ISD::SETNE ) && "Unsupported ISD::CondCode") ? void (0) : __assert_fail ("(CC == ISD::SETEQ || CC == ISD::SETNE) && \"Unsupported ISD::CondCode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22779, __extension__ __PRETTY_FUNCTION__)); | |||
22780 | X86CC = (CC == ISD::SETEQ ? X86::COND_E : X86::COND_NE); | |||
22781 | ||||
22782 | auto MaskBits = [&](SDValue Src) { | |||
22783 | if (Mask.isAllOnes()) | |||
22784 | return Src; | |||
22785 | EVT SrcVT = Src.getValueType(); | |||
22786 | SDValue MaskValue = DAG.getConstant(Mask, DL, SrcVT); | |||
22787 | return DAG.getNode(ISD::AND, DL, SrcVT, Src, MaskValue); | |||
22788 | }; | |||
22789 | ||||
22790 | // For sub-128-bit vector, cast to (legal) integer and compare with zero. | |||
22791 | if (VT.getSizeInBits() < 128) { | |||
22792 | EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), VT.getSizeInBits()); | |||
22793 | if (!DAG.getTargetLoweringInfo().isTypeLegal(IntVT)) | |||
22794 | return SDValue(); | |||
22795 | return DAG.getNode(X86ISD::CMP, DL, MVT::i32, | |||
22796 | DAG.getBitcast(IntVT, MaskBits(V)), | |||
22797 | DAG.getConstant(0, DL, IntVT)); | |||
22798 | } | |||
22799 | ||||
22800 | // Quit if not splittable to 128/256-bit vector. | |||
22801 | if (!isPowerOf2_32(VT.getSizeInBits())) | |||
22802 | return SDValue(); | |||
22803 | ||||
22804 | // Split down to 128/256-bit vector. | |||
22805 | unsigned TestSize = Subtarget.hasAVX() ? 256 : 128; | |||
22806 | while (VT.getSizeInBits() > TestSize) { | |||
22807 | auto Split = DAG.SplitVector(V, DL); | |||
22808 | VT = Split.first.getValueType(); | |||
22809 | V = DAG.getNode(ISD::OR, DL, VT, Split.first, Split.second); | |||
22810 | } | |||
22811 | ||||
22812 | bool UsePTEST = Subtarget.hasSSE41(); | |||
22813 | if (UsePTEST) { | |||
22814 | MVT TestVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64; | |||
22815 | V = DAG.getBitcast(TestVT, MaskBits(V)); | |||
22816 | return DAG.getNode(X86ISD::PTEST, DL, MVT::i32, V, V); | |||
22817 | } | |||
22818 | ||||
22819 | // Without PTEST, a masked v2i64 or-reduction is not faster than | |||
22820 | // scalarization. | |||
22821 | if (!Mask.isAllOnes() && VT.getScalarSizeInBits() > 32) | |||
22822 | return SDValue(); | |||
22823 | ||||
22824 | V = DAG.getBitcast(MVT::v16i8, MaskBits(V)); | |||
22825 | V = DAG.getNode(X86ISD::PCMPEQ, DL, MVT::v16i8, V, | |||
22826 | getZeroVector(MVT::v16i8, Subtarget, DAG, DL)); | |||
22827 | V = DAG.getNode(X86ISD::MOVMSK, DL, MVT::i32, V); | |||
22828 | return DAG.getNode(X86ISD::CMP, DL, MVT::i32, V, | |||
22829 | DAG.getConstant(0xFFFF, DL, MVT::i32)); | |||
22830 | } | |||
22831 | ||||
22832 | // Check whether an OR'd reduction tree is PTEST-able, or if we can fallback to | |||
22833 | // CMP(MOVMSK(PCMPEQB(X,0))). | |||
22834 | static SDValue MatchVectorAllZeroTest(SDValue Op, ISD::CondCode CC, | |||
22835 | const SDLoc &DL, | |||
22836 | const X86Subtarget &Subtarget, | |||
22837 | SelectionDAG &DAG, SDValue &X86CC) { | |||
22838 | assert((CC == ISD::SETEQ || CC == ISD::SETNE) && "Unsupported ISD::CondCode")(static_cast <bool> ((CC == ISD::SETEQ || CC == ISD::SETNE ) && "Unsupported ISD::CondCode") ? void (0) : __assert_fail ("(CC == ISD::SETEQ || CC == ISD::SETNE) && \"Unsupported ISD::CondCode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22838, __extension__ __PRETTY_FUNCTION__)); | |||
22839 | ||||
22840 | if (!Subtarget.hasSSE2() || !Op->hasOneUse()) | |||
22841 | return SDValue(); | |||
22842 | ||||
22843 | // Check whether we're masking/truncating an OR-reduction result, in which | |||
22844 | // case track the masked bits. | |||
22845 | APInt Mask = APInt::getAllOnes(Op.getScalarValueSizeInBits()); | |||
22846 | switch (Op.getOpcode()) { | |||
22847 | case ISD::TRUNCATE: { | |||
22848 | SDValue Src = Op.getOperand(0); | |||
22849 | Mask = APInt::getLowBitsSet(Src.getScalarValueSizeInBits(), | |||
22850 | Op.getScalarValueSizeInBits()); | |||
22851 | Op = Src; | |||
22852 | break; | |||
22853 | } | |||
22854 | case ISD::AND: { | |||
22855 | if (auto *Cst = dyn_cast<ConstantSDNode>(Op.getOperand(1))) { | |||
22856 | Mask = Cst->getAPIntValue(); | |||
22857 | Op = Op.getOperand(0); | |||
22858 | } | |||
22859 | break; | |||
22860 | } | |||
22861 | } | |||
22862 | ||||
22863 | SmallVector<SDValue, 8> VecIns; | |||
22864 | if (Op.getOpcode() == ISD::OR && matchScalarReduction(Op, ISD::OR, VecIns)) { | |||
22865 | EVT VT = VecIns[0].getValueType(); | |||
22866 | assert(llvm::all_of(VecIns,(static_cast <bool> (llvm::all_of(VecIns, [VT](SDValue V ) { return VT == V.getValueType(); }) && "Reduction source vector mismatch" ) ? void (0) : __assert_fail ("llvm::all_of(VecIns, [VT](SDValue V) { return VT == V.getValueType(); }) && \"Reduction source vector mismatch\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22868, __extension__ __PRETTY_FUNCTION__)) | |||
22867 | [VT](SDValue V) { return VT == V.getValueType(); }) &&(static_cast <bool> (llvm::all_of(VecIns, [VT](SDValue V ) { return VT == V.getValueType(); }) && "Reduction source vector mismatch" ) ? void (0) : __assert_fail ("llvm::all_of(VecIns, [VT](SDValue V) { return VT == V.getValueType(); }) && \"Reduction source vector mismatch\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22868, __extension__ __PRETTY_FUNCTION__)) | |||
22868 | "Reduction source vector mismatch")(static_cast <bool> (llvm::all_of(VecIns, [VT](SDValue V ) { return VT == V.getValueType(); }) && "Reduction source vector mismatch" ) ? void (0) : __assert_fail ("llvm::all_of(VecIns, [VT](SDValue V) { return VT == V.getValueType(); }) && \"Reduction source vector mismatch\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 22868, __extension__ __PRETTY_FUNCTION__)); | |||
22869 | ||||
22870 | // Quit if less than 128-bits or not splittable to 128/256-bit vector. | |||
22871 | if (VT.getSizeInBits() < 128 || !isPowerOf2_32(VT.getSizeInBits())) | |||
22872 | return SDValue(); | |||
22873 | ||||
22874 | // If more than one full vector is evaluated, OR them first before PTEST. | |||
22875 | for (unsigned Slot = 0, e = VecIns.size(); e - Slot > 1; | |||
22876 | Slot += 2, e += 1) { | |||
22877 | // Each iteration will OR 2 nodes and append the result until there is | |||
22878 | // only 1 node left, i.e. the final OR'd value of all vectors. | |||
22879 | SDValue LHS = VecIns[Slot]; | |||
22880 | SDValue RHS = VecIns[Slot + 1]; | |||
22881 | VecIns.push_back(DAG.getNode(ISD::OR, DL, VT, LHS, RHS)); | |||
22882 | } | |||
22883 | ||||
22884 | X86::CondCode CCode; | |||
22885 | if (SDValue V = LowerVectorAllZero(DL, VecIns.back(), CC, Mask, Subtarget, | |||
22886 | DAG, CCode)) { | |||
22887 | X86CC = DAG.getTargetConstant(CCode, DL, MVT::i8); | |||
22888 | return V; | |||
22889 | } | |||
22890 | } | |||
22891 | ||||
22892 | if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT) { | |||
22893 | ISD::NodeType BinOp; | |||
22894 | if (SDValue Match = | |||
22895 | DAG.matchBinOpReduction(Op.getNode(), BinOp, {ISD::OR})) { | |||
22896 | X86::CondCode CCode; | |||
22897 | if (SDValue V = | |||
22898 | LowerVectorAllZero(DL, Match, CC, Mask, Subtarget, DAG, CCode)) { | |||
22899 | X86CC = DAG.getTargetConstant(CCode, DL, MVT::i8); | |||
22900 | return V; | |||
22901 | } | |||
22902 | } | |||
22903 | } | |||
22904 | ||||
22905 | return SDValue(); | |||
22906 | } | |||
22907 | ||||
22908 | /// return true if \c Op has a use that doesn't just read flags. | |||
22909 | static bool hasNonFlagsUse(SDValue Op) { | |||
22910 | for (SDNode::use_iterator UI = Op->use_begin(), UE = Op->use_end(); UI != UE; | |||
22911 | ++UI) { | |||
22912 | SDNode *User = *UI; | |||
22913 | unsigned UOpNo = UI.getOperandNo(); | |||
22914 | if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) { | |||
22915 | // Look pass truncate. | |||
22916 | UOpNo = User->use_begin().getOperandNo(); | |||
22917 | User = *User->use_begin(); | |||
22918 | } | |||
22919 | ||||
22920 | if (User->getOpcode() != ISD::BRCOND && User->getOpcode() != ISD::SETCC && | |||
22921 | !(User->getOpcode() == ISD::SELECT && UOpNo == 0)) | |||
22922 | return true; | |||
22923 | } | |||
22924 | return false; | |||
22925 | } | |||
22926 | ||||
22927 | // Transform to an x86-specific ALU node with flags if there is a chance of | |||
22928 | // using an RMW op or only the flags are used. Otherwise, leave | |||
22929 | // the node alone and emit a 'cmp' or 'test' instruction. | |||
22930 | static bool isProfitableToUseFlagOp(SDValue Op) { | |||
22931 | for (SDNode *U : Op->uses()) | |||
22932 | if (U->getOpcode() != ISD::CopyToReg && | |||
22933 | U->getOpcode() != ISD::SETCC && | |||
22934 | U->getOpcode() != ISD::STORE) | |||
22935 | return false; | |||
22936 | ||||
22937 | return true; | |||
22938 | } | |||
22939 | ||||
22940 | /// Emit nodes that will be selected as "test Op0,Op0", or something | |||
22941 | /// equivalent. | |||
22942 | static SDValue EmitTest(SDValue Op, unsigned X86CC, const SDLoc &dl, | |||
22943 | SelectionDAG &DAG, const X86Subtarget &Subtarget) { | |||
22944 | // CF and OF aren't always set the way we want. Determine which | |||
22945 | // of these we need. | |||
22946 | bool NeedCF = false; | |||
22947 | bool NeedOF = false; | |||
22948 | switch (X86CC) { | |||
22949 | default: break; | |||
22950 | case X86::COND_A: case X86::COND_AE: | |||
22951 | case X86::COND_B: case X86::COND_BE: | |||
22952 | NeedCF = true; | |||
22953 | break; | |||
22954 | case X86::COND_G: case X86::COND_GE: | |||
22955 | case X86::COND_L: case X86::COND_LE: | |||
22956 | case X86::COND_O: case X86::COND_NO: { | |||
22957 | // Check if we really need to set the | |||
22958 | // Overflow flag. If NoSignedWrap is present | |||
22959 | // that is not actually needed. | |||
22960 | switch (Op->getOpcode()) { | |||
22961 | case ISD::ADD: | |||
22962 | case ISD::SUB: | |||
22963 | case ISD::MUL: | |||
22964 | case ISD::SHL: | |||
22965 | if (Op.getNode()->getFlags().hasNoSignedWrap()) | |||
22966 | break; | |||
22967 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
22968 | default: | |||
22969 | NeedOF = true; | |||
22970 | break; | |||
22971 | } | |||
22972 | break; | |||
22973 | } | |||
22974 | } | |||
22975 | // See if we can use the EFLAGS value from the operand instead of | |||
22976 | // doing a separate TEST. TEST always sets OF and CF to 0, so unless | |||
22977 | // we prove that the arithmetic won't overflow, we can't use OF or CF. | |||
22978 | if (Op.getResNo() != 0 || NeedOF || NeedCF) { | |||
22979 | // Emit a CMP with 0, which is the TEST pattern. | |||
22980 | return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op, | |||
22981 | DAG.getConstant(0, dl, Op.getValueType())); | |||
22982 | } | |||
22983 | unsigned Opcode = 0; | |||
22984 | unsigned NumOperands = 0; | |||
22985 | ||||
22986 | SDValue ArithOp = Op; | |||
22987 | ||||
22988 | // NOTICE: In the code below we use ArithOp to hold the arithmetic operation | |||
22989 | // which may be the result of a CAST. We use the variable 'Op', which is the | |||
22990 | // non-casted variable when we check for possible users. | |||
22991 | switch (ArithOp.getOpcode()) { | |||
22992 | case ISD::AND: | |||
22993 | // If the primary 'and' result isn't used, don't bother using X86ISD::AND, | |||
22994 | // because a TEST instruction will be better. | |||
22995 | if (!hasNonFlagsUse(Op)) | |||
22996 | break; | |||
22997 | ||||
22998 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
22999 | case ISD::ADD: | |||
23000 | case ISD::SUB: | |||
23001 | case ISD::OR: | |||
23002 | case ISD::XOR: | |||
23003 | if (!isProfitableToUseFlagOp(Op)) | |||
23004 | break; | |||
23005 | ||||
23006 | // Otherwise use a regular EFLAGS-setting instruction. | |||
23007 | switch (ArithOp.getOpcode()) { | |||
23008 | default: llvm_unreachable("unexpected operator!")::llvm::llvm_unreachable_internal("unexpected operator!", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 23008); | |||
23009 | case ISD::ADD: Opcode = X86ISD::ADD; break; | |||
23010 | case ISD::SUB: Opcode = X86ISD::SUB; break; | |||
23011 | case ISD::XOR: Opcode = X86ISD::XOR; break; | |||
23012 | case ISD::AND: Opcode = X86ISD::AND; break; | |||
23013 | case ISD::OR: Opcode = X86ISD::OR; break; | |||
23014 | } | |||
23015 | ||||
23016 | NumOperands = 2; | |||
23017 | break; | |||
23018 | case X86ISD::ADD: | |||
23019 | case X86ISD::SUB: | |||
23020 | case X86ISD::OR: | |||
23021 | case X86ISD::XOR: | |||
23022 | case X86ISD::AND: | |||
23023 | return SDValue(Op.getNode(), 1); | |||
23024 | case ISD::SSUBO: | |||
23025 | case ISD::USUBO: { | |||
23026 | // /USUBO/SSUBO will become a X86ISD::SUB and we can use its Z flag. | |||
23027 | SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32); | |||
23028 | return DAG.getNode(X86ISD::SUB, dl, VTs, Op->getOperand(0), | |||
23029 | Op->getOperand(1)).getValue(1); | |||
23030 | } | |||
23031 | default: | |||
23032 | break; | |||
23033 | } | |||
23034 | ||||
23035 | if (Opcode == 0) { | |||
23036 | // Emit a CMP with 0, which is the TEST pattern. | |||
23037 | return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op, | |||
23038 | DAG.getConstant(0, dl, Op.getValueType())); | |||
23039 | } | |||
23040 | SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32); | |||
23041 | SmallVector<SDValue, 4> Ops(Op->op_begin(), Op->op_begin() + NumOperands); | |||
23042 | ||||
23043 | SDValue New = DAG.getNode(Opcode, dl, VTs, Ops); | |||
23044 | DAG.ReplaceAllUsesOfValueWith(SDValue(Op.getNode(), 0), New); | |||
23045 | return SDValue(New.getNode(), 1); | |||
23046 | } | |||
23047 | ||||
23048 | /// Emit nodes that will be selected as "cmp Op0,Op1", or something | |||
23049 | /// equivalent. | |||
23050 | static SDValue EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC, | |||
23051 | const SDLoc &dl, SelectionDAG &DAG, | |||
23052 | const X86Subtarget &Subtarget) { | |||
23053 | if (isNullConstant(Op1)) | |||
23054 | return EmitTest(Op0, X86CC, dl, DAG, Subtarget); | |||
23055 | ||||
23056 | EVT CmpVT = Op0.getValueType(); | |||
23057 | ||||
23058 | assert((CmpVT == MVT::i8 || CmpVT == MVT::i16 ||(static_cast <bool> ((CmpVT == MVT::i8 || CmpVT == MVT:: i16 || CmpVT == MVT::i32 || CmpVT == MVT::i64) && "Unexpected VT!" ) ? void (0) : __assert_fail ("(CmpVT == MVT::i8 || CmpVT == MVT::i16 || CmpVT == MVT::i32 || CmpVT == MVT::i64) && \"Unexpected VT!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 23059, __extension__ __PRETTY_FUNCTION__)) | |||
23059 | CmpVT == MVT::i32 || CmpVT == MVT::i64) && "Unexpected VT!")(static_cast <bool> ((CmpVT == MVT::i8 || CmpVT == MVT:: i16 || CmpVT == MVT::i32 || CmpVT == MVT::i64) && "Unexpected VT!" ) ? void (0) : __assert_fail ("(CmpVT == MVT::i8 || CmpVT == MVT::i16 || CmpVT == MVT::i32 || CmpVT == MVT::i64) && \"Unexpected VT!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 23059, __extension__ __PRETTY_FUNCTION__)); | |||
23060 | ||||
23061 | // Only promote the compare up to I32 if it is a 16 bit operation | |||
23062 | // with an immediate. 16 bit immediates are to be avoided. | |||
23063 | if (CmpVT == MVT::i16 && !Subtarget.isAtom() && | |||
23064 | !DAG.getMachineFunction().getFunction().hasMinSize()) { | |||
23065 | ConstantSDNode *COp0 = dyn_cast<ConstantSDNode>(Op0); | |||
23066 | ConstantSDNode *COp1 = dyn_cast<ConstantSDNode>(Op1); | |||
23067 | // Don't do this if the immediate can fit in 8-bits. | |||
23068 | if ((COp0 && !COp0->getAPIntValue().isSignedIntN(8)) || | |||
23069 | (COp1 && !COp1->getAPIntValue().isSignedIntN(8))) { | |||
23070 | unsigned ExtendOp = | |||
23071 | isX86CCSigned(X86CC) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; | |||
23072 | if (X86CC == X86::COND_E || X86CC == X86::COND_NE) { | |||
23073 | // For equality comparisons try to use SIGN_EXTEND if the input was | |||
23074 | // truncate from something with enough sign bits. | |||
23075 | if (Op0.getOpcode() == ISD::TRUNCATE) { | |||
23076 | SDValue In = Op0.getOperand(0); | |||
23077 | unsigned EffBits = | |||
23078 | In.getScalarValueSizeInBits() - DAG.ComputeNumSignBits(In) + 1; | |||
23079 | if (EffBits <= 16) | |||
23080 | ExtendOp = ISD::SIGN_EXTEND; | |||
23081 | } else if (Op1.getOpcode() == ISD::TRUNCATE) { | |||
23082 | SDValue In = Op1.getOperand(0); | |||
23083 | unsigned EffBits = | |||
23084 | In.getScalarValueSizeInBits() - DAG.ComputeNumSignBits(In) + 1; | |||
23085 | if (EffBits <= 16) | |||
23086 | ExtendOp = ISD::SIGN_EXTEND; | |||
23087 | } | |||
23088 | } | |||
23089 | ||||
23090 | CmpVT = MVT::i32; | |||
23091 | Op0 = DAG.getNode(ExtendOp, dl, CmpVT, Op0); | |||
23092 | Op1 = DAG.getNode(ExtendOp, dl, CmpVT, Op1); | |||
23093 | } | |||
23094 | } | |||
23095 | ||||
23096 | // Try to shrink i64 compares if the input has enough zero bits. | |||
23097 | // FIXME: Do this for non-constant compares for constant on LHS? | |||
23098 | if (CmpVT == MVT::i64 && isa<ConstantSDNode>(Op1) && !isX86CCSigned(X86CC) && | |||
23099 | Op0.hasOneUse() && // Hacky way to not break CSE opportunities with sub. | |||
23100 | cast<ConstantSDNode>(Op1)->getAPIntValue().getActiveBits() <= 32 && | |||
23101 | DAG.MaskedValueIsZero(Op0, APInt::getHighBitsSet(64, 32))) { | |||
23102 | CmpVT = MVT::i32; | |||
23103 | Op0 = DAG.getNode(ISD::TRUNCATE, dl, CmpVT, Op0); | |||
23104 | Op1 = DAG.getNode(ISD::TRUNCATE, dl, CmpVT, Op1); | |||
23105 | } | |||
23106 | ||||
23107 | // 0-x == y --> x+y == 0 | |||
23108 | // 0-x != y --> x+y != 0 | |||
23109 | if (Op0.getOpcode() == ISD::SUB && isNullConstant(Op0.getOperand(0)) && | |||
23110 | Op0.hasOneUse() && (X86CC == X86::COND_E || X86CC == X86::COND_NE)) { | |||
23111 | SDVTList VTs = DAG.getVTList(CmpVT, MVT::i32); | |||
23112 | SDValue Add = DAG.getNode(X86ISD::ADD, dl, VTs, Op0.getOperand(1), Op1); | |||
23113 | return Add.getValue(1); | |||
23114 | } | |||
23115 | ||||
23116 | // x == 0-y --> x+y == 0 | |||
23117 | // x != 0-y --> x+y != 0 | |||
23118 | if (Op1.getOpcode() == ISD::SUB && isNullConstant(Op1.getOperand(0)) && | |||
23119 | Op1.hasOneUse() && (X86CC == X86::COND_E || X86CC == X86::COND_NE)) { | |||
23120 | SDVTList VTs = DAG.getVTList(CmpVT, MVT::i32); | |||
23121 | SDValue Add = DAG.getNode(X86ISD::ADD, dl, VTs, Op0, Op1.getOperand(1)); | |||
23122 | return Add.getValue(1); | |||
23123 | } | |||
23124 | ||||
23125 | // Use SUB instead of CMP to enable CSE between SUB and CMP. | |||
23126 | SDVTList VTs = DAG.getVTList(CmpVT, MVT::i32); | |||
23127 | SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs, Op0, Op1); | |||
23128 | return Sub.getValue(1); | |||
23129 | } | |||
23130 | ||||
23131 | /// Check if replacement of SQRT with RSQRT should be disabled. | |||
23132 | bool X86TargetLowering::isFsqrtCheap(SDValue Op, SelectionDAG &DAG) const { | |||
23133 | EVT VT = Op.getValueType(); | |||
23134 | ||||
23135 | // We never want to use both SQRT and RSQRT instructions for the same input. | |||
23136 | if (DAG.getNodeIfExists(X86ISD::FRSQRT, DAG.getVTList(VT), Op)) | |||
23137 | return false; | |||
23138 | ||||
23139 | if (VT.isVector()) | |||
23140 | return Subtarget.hasFastVectorFSQRT(); | |||
23141 | return Subtarget.hasFastScalarFSQRT(); | |||
23142 | } | |||
23143 | ||||
23144 | /// The minimum architected relative accuracy is 2^-12. We need one | |||
23145 | /// Newton-Raphson step to have a good float result (24 bits of precision). | |||
23146 | SDValue X86TargetLowering::getSqrtEstimate(SDValue Op, | |||
23147 | SelectionDAG &DAG, int Enabled, | |||
23148 | int &RefinementSteps, | |||
23149 | bool &UseOneConstNR, | |||
23150 | bool Reciprocal) const { | |||
23151 | SDLoc DL(Op); | |||
23152 | EVT VT = Op.getValueType(); | |||
23153 | ||||
23154 | // SSE1 has rsqrtss and rsqrtps. AVX adds a 256-bit variant for rsqrtps. | |||
23155 | // It is likely not profitable to do this for f64 because a double-precision | |||
23156 | // rsqrt estimate with refinement on x86 prior to FMA requires at least 16 | |||
23157 | // instructions: convert to single, rsqrtss, convert back to double, refine | |||
23158 | // (3 steps = at least 13 insts). If an 'rsqrtsd' variant was added to the ISA | |||
23159 | // along with FMA, this could be a throughput win. | |||
23160 | // TODO: SQRT requires SSE2 to prevent the introduction of an illegal v4i32 | |||
23161 | // after legalize types. | |||
23162 | if ((VT == MVT::f32 && Subtarget.hasSSE1()) || | |||
23163 | (VT == MVT::v4f32 && Subtarget.hasSSE1() && Reciprocal) || | |||
23164 | (VT == MVT::v4f32 && Subtarget.hasSSE2() && !Reciprocal) || | |||
23165 | (VT == MVT::v8f32 && Subtarget.hasAVX()) || | |||
23166 | (VT == MVT::v16f32 && Subtarget.useAVX512Regs())) { | |||
23167 | if (RefinementSteps == ReciprocalEstimate::Unspecified) | |||
23168 | RefinementSteps = 1; | |||
23169 | ||||
23170 | UseOneConstNR = false; | |||
23171 | // There is no FSQRT for 512-bits, but there is RSQRT14. | |||
23172 | unsigned Opcode = VT == MVT::v16f32 ? X86ISD::RSQRT14 : X86ISD::FRSQRT; | |||
23173 | return DAG.getNode(Opcode, DL, VT, Op); | |||
23174 | } | |||
23175 | ||||
23176 | if (VT.getScalarType() == MVT::f16 && isTypeLegal(VT) && | |||
23177 | Subtarget.hasFP16()) { | |||
23178 | if (RefinementSteps == ReciprocalEstimate::Unspecified) | |||
23179 | RefinementSteps = 0; | |||
23180 | ||||
23181 | if (VT == MVT::f16) { | |||
23182 | SDValue Zero = DAG.getIntPtrConstant(0, DL); | |||
23183 | SDValue Undef = DAG.getUNDEF(MVT::v8f16); | |||
23184 | Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, MVT::v8f16, Op); | |||
23185 | Op = DAG.getNode(X86ISD::RSQRT14S, DL, MVT::v8f16, Undef, Op); | |||
23186 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f16, Op, Zero); | |||
23187 | } | |||
23188 | ||||
23189 | return DAG.getNode(X86ISD::RSQRT14, DL, VT, Op); | |||
23190 | } | |||
23191 | return SDValue(); | |||
23192 | } | |||
23193 | ||||
23194 | /// The minimum architected relative accuracy is 2^-12. We need one | |||
23195 | /// Newton-Raphson step to have a good float result (24 bits of precision). | |||
23196 | SDValue X86TargetLowering::getRecipEstimate(SDValue Op, SelectionDAG &DAG, | |||
23197 | int Enabled, | |||
23198 | int &RefinementSteps) const { | |||
23199 | SDLoc DL(Op); | |||
23200 | EVT VT = Op.getValueType(); | |||
23201 | ||||
23202 | // SSE1 has rcpss and rcpps. AVX adds a 256-bit variant for rcpps. | |||
23203 | // It is likely not profitable to do this for f64 because a double-precision | |||
23204 | // reciprocal estimate with refinement on x86 prior to FMA requires | |||
23205 | // 15 instructions: convert to single, rcpss, convert back to double, refine | |||
23206 | // (3 steps = 12 insts). If an 'rcpsd' variant was added to the ISA | |||
23207 | // along with FMA, this could be a throughput win. | |||
23208 | ||||
23209 | if ((VT == MVT::f32 && Subtarget.hasSSE1()) || | |||
23210 | (VT == MVT::v4f32 && Subtarget.hasSSE1()) || | |||
23211 | (VT == MVT::v8f32 && Subtarget.hasAVX()) || | |||
23212 | (VT == MVT::v16f32 && Subtarget.useAVX512Regs())) { | |||
23213 | // Enable estimate codegen with 1 refinement step for vector division. | |||
23214 | // Scalar division estimates are disabled because they break too much | |||
23215 | // real-world code. These defaults are intended to match GCC behavior. | |||
23216 | if (VT == MVT::f32 && Enabled == ReciprocalEstimate::Unspecified) | |||
23217 | return SDValue(); | |||
23218 | ||||
23219 | if (RefinementSteps == ReciprocalEstimate::Unspecified) | |||
23220 | RefinementSteps = 1; | |||
23221 | ||||
23222 | // There is no FSQRT for 512-bits, but there is RCP14. | |||
23223 | unsigned Opcode = VT == MVT::v16f32 ? X86ISD::RCP14 : X86ISD::FRCP; | |||
23224 | return DAG.getNode(Opcode, DL, VT, Op); | |||
23225 | } | |||
23226 | ||||
23227 | if (VT.getScalarType() == MVT::f16 && isTypeLegal(VT) && | |||
23228 | Subtarget.hasFP16()) { | |||
23229 | if (RefinementSteps == ReciprocalEstimate::Unspecified) | |||
23230 | RefinementSteps = 0; | |||
23231 | ||||
23232 | if (VT == MVT::f16) { | |||
23233 | SDValue Zero = DAG.getIntPtrConstant(0, DL); | |||
23234 | SDValue Undef = DAG.getUNDEF(MVT::v8f16); | |||
23235 | Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, MVT::v8f16, Op); | |||
23236 | Op = DAG.getNode(X86ISD::RCP14S, DL, MVT::v8f16, Undef, Op); | |||
23237 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f16, Op, Zero); | |||
23238 | } | |||
23239 | ||||
23240 | return DAG.getNode(X86ISD::RCP14, DL, VT, Op); | |||
23241 | } | |||
23242 | return SDValue(); | |||
23243 | } | |||
23244 | ||||
23245 | /// If we have at least two divisions that use the same divisor, convert to | |||
23246 | /// multiplication by a reciprocal. This may need to be adjusted for a given | |||
23247 | /// CPU if a division's cost is not at least twice the cost of a multiplication. | |||
23248 | /// This is because we still need one division to calculate the reciprocal and | |||
23249 | /// then we need two multiplies by that reciprocal as replacements for the | |||
23250 | /// original divisions. | |||
23251 | unsigned X86TargetLowering::combineRepeatedFPDivisors() const { | |||
23252 | return 2; | |||
23253 | } | |||
23254 | ||||
23255 | SDValue | |||
23256 | X86TargetLowering::BuildSDIVPow2(SDNode *N, const APInt &Divisor, | |||
23257 | SelectionDAG &DAG, | |||
23258 | SmallVectorImpl<SDNode *> &Created) const { | |||
23259 | AttributeList Attr = DAG.getMachineFunction().getFunction().getAttributes(); | |||
23260 | if (isIntDivCheap(N->getValueType(0), Attr)) | |||
23261 | return SDValue(N,0); // Lower SDIV as SDIV | |||
23262 | ||||
23263 | assert((Divisor.isPowerOf2() || (-Divisor).isPowerOf2()) &&(static_cast <bool> ((Divisor.isPowerOf2() || (-Divisor ).isPowerOf2()) && "Unexpected divisor!") ? void (0) : __assert_fail ("(Divisor.isPowerOf2() || (-Divisor).isPowerOf2()) && \"Unexpected divisor!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 23264, __extension__ __PRETTY_FUNCTION__)) | |||
23264 | "Unexpected divisor!")(static_cast <bool> ((Divisor.isPowerOf2() || (-Divisor ).isPowerOf2()) && "Unexpected divisor!") ? void (0) : __assert_fail ("(Divisor.isPowerOf2() || (-Divisor).isPowerOf2()) && \"Unexpected divisor!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 23264, __extension__ __PRETTY_FUNCTION__)); | |||
23265 | ||||
23266 | // Only perform this transform if CMOV is supported otherwise the select | |||
23267 | // below will become a branch. | |||
23268 | if (!Subtarget.hasCMov()) | |||
23269 | return SDValue(); | |||
23270 | ||||
23271 | // fold (sdiv X, pow2) | |||
23272 | EVT VT = N->getValueType(0); | |||
23273 | // FIXME: Support i8. | |||
23274 | if (VT != MVT::i16 && VT != MVT::i32 && | |||
23275 | !(Subtarget.is64Bit() && VT == MVT::i64)) | |||
23276 | return SDValue(); | |||
23277 | ||||
23278 | unsigned Lg2 = Divisor.countTrailingZeros(); | |||
23279 | ||||
23280 | // If the divisor is 2 or -2, the default expansion is better. | |||
23281 | if (Lg2 == 1) | |||
23282 | return SDValue(); | |||
23283 | ||||
23284 | SDLoc DL(N); | |||
23285 | SDValue N0 = N->getOperand(0); | |||
23286 | SDValue Zero = DAG.getConstant(0, DL, VT); | |||
23287 | APInt Lg2Mask = APInt::getLowBitsSet(VT.getSizeInBits(), Lg2); | |||
23288 | SDValue Pow2MinusOne = DAG.getConstant(Lg2Mask, DL, VT); | |||
23289 | ||||
23290 | // If N0 is negative, we need to add (Pow2 - 1) to it before shifting right. | |||
23291 | SDValue Cmp = DAG.getSetCC(DL, MVT::i8, N0, Zero, ISD::SETLT); | |||
23292 | SDValue Add = DAG.getNode(ISD::ADD, DL, VT, N0, Pow2MinusOne); | |||
23293 | SDValue CMov = DAG.getNode(ISD::SELECT, DL, VT, Cmp, Add, N0); | |||
23294 | ||||
23295 | Created.push_back(Cmp.getNode()); | |||
23296 | Created.push_back(Add.getNode()); | |||
23297 | Created.push_back(CMov.getNode()); | |||
23298 | ||||
23299 | // Divide by pow2. | |||
23300 | SDValue SRA = | |||
23301 | DAG.getNode(ISD::SRA, DL, VT, CMov, DAG.getConstant(Lg2, DL, MVT::i8)); | |||
23302 | ||||
23303 | // If we're dividing by a positive value, we're done. Otherwise, we must | |||
23304 | // negate the result. | |||
23305 | if (Divisor.isNonNegative()) | |||
23306 | return SRA; | |||
23307 | ||||
23308 | Created.push_back(SRA.getNode()); | |||
23309 | return DAG.getNode(ISD::SUB, DL, VT, Zero, SRA); | |||
23310 | } | |||
23311 | ||||
23312 | /// Result of 'and' is compared against zero. Change to a BT node if possible. | |||
23313 | /// Returns the BT node and the condition code needed to use it. | |||
23314 | static SDValue LowerAndToBT(SDValue And, ISD::CondCode CC, | |||
23315 | const SDLoc &dl, SelectionDAG &DAG, | |||
23316 | SDValue &X86CC) { | |||
23317 | assert(And.getOpcode() == ISD::AND && "Expected AND node!")(static_cast <bool> (And.getOpcode() == ISD::AND && "Expected AND node!") ? void (0) : __assert_fail ("And.getOpcode() == ISD::AND && \"Expected AND node!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 23317, __extension__ __PRETTY_FUNCTION__)); | |||
23318 | SDValue Op0 = And.getOperand(0); | |||
23319 | SDValue Op1 = And.getOperand(1); | |||
23320 | if (Op0.getOpcode() == ISD::TRUNCATE) | |||
23321 | Op0 = Op0.getOperand(0); | |||
23322 | if (Op1.getOpcode() == ISD::TRUNCATE) | |||
23323 | Op1 = Op1.getOperand(0); | |||
23324 | ||||
23325 | SDValue Src, BitNo; | |||
23326 | if (Op1.getOpcode() == ISD::SHL) | |||
23327 | std::swap(Op0, Op1); | |||
23328 | if (Op0.getOpcode() == ISD::SHL) { | |||
23329 | if (isOneConstant(Op0.getOperand(0))) { | |||
23330 | // If we looked past a truncate, check that it's only truncating away | |||
23331 | // known zeros. | |||
23332 | unsigned BitWidth = Op0.getValueSizeInBits(); | |||
23333 | unsigned AndBitWidth = And.getValueSizeInBits(); | |||
23334 | if (BitWidth > AndBitWidth) { | |||
23335 | KnownBits Known = DAG.computeKnownBits(Op0); | |||
23336 | if (Known.countMinLeadingZeros() < BitWidth - AndBitWidth) | |||
23337 | return SDValue(); | |||
23338 | } | |||
23339 | Src = Op1; | |||
23340 | BitNo = Op0.getOperand(1); | |||
23341 | } | |||
23342 | } else if (Op1.getOpcode() == ISD::Constant) { | |||
23343 | ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1); | |||
23344 | uint64_t AndRHSVal = AndRHS->getZExtValue(); | |||
23345 | SDValue AndLHS = Op0; | |||
23346 | ||||
23347 | if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) { | |||
23348 | Src = AndLHS.getOperand(0); | |||
23349 | BitNo = AndLHS.getOperand(1); | |||
23350 | } else { | |||
23351 | // Use BT if the immediate can't be encoded in a TEST instruction or we | |||
23352 | // are optimizing for size and the immedaite won't fit in a byte. | |||
23353 | bool OptForSize = DAG.shouldOptForSize(); | |||
23354 | if ((!isUInt<32>(AndRHSVal) || (OptForSize && !isUInt<8>(AndRHSVal))) && | |||
23355 | isPowerOf2_64(AndRHSVal)) { | |||
23356 | Src = AndLHS; | |||
23357 | BitNo = DAG.getConstant(Log2_64_Ceil(AndRHSVal), dl, | |||
23358 | Src.getValueType()); | |||
23359 | } | |||
23360 | } | |||
23361 | } | |||
23362 | ||||
23363 | // No patterns found, give up. | |||
23364 | if (!Src.getNode()) | |||
23365 | return SDValue(); | |||
23366 | ||||
23367 | // If Src is i8, promote it to i32 with any_extend. There is no i8 BT | |||
23368 | // instruction. Since the shift amount is in-range-or-undefined, we know | |||
23369 | // that doing a bittest on the i32 value is ok. We extend to i32 because | |||
23370 | // the encoding for the i16 version is larger than the i32 version. | |||
23371 | // Also promote i16 to i32 for performance / code size reason. | |||
23372 | if (Src.getValueType() == MVT::i8 || Src.getValueType() == MVT::i16) | |||
23373 | Src = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Src); | |||
23374 | ||||
23375 | // See if we can use the 32-bit instruction instead of the 64-bit one for a | |||
23376 | // shorter encoding. Since the former takes the modulo 32 of BitNo and the | |||
23377 | // latter takes the modulo 64, this is only valid if the 5th bit of BitNo is | |||
23378 | // known to be zero. | |||
23379 | if (Src.getValueType() == MVT::i64 && | |||
23380 | DAG.MaskedValueIsZero(BitNo, APInt(BitNo.getValueSizeInBits(), 32))) | |||
23381 | Src = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src); | |||
23382 | ||||
23383 | // If the operand types disagree, extend the shift amount to match. Since | |||
23384 | // BT ignores high bits (like shifts) we can use anyextend. | |||
23385 | if (Src.getValueType() != BitNo.getValueType()) | |||
23386 | BitNo = DAG.getNode(ISD::ANY_EXTEND, dl, Src.getValueType(), BitNo); | |||
23387 | ||||
23388 | X86CC = DAG.getTargetConstant(CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B, | |||
23389 | dl, MVT::i8); | |||
23390 | return DAG.getNode(X86ISD::BT, dl, MVT::i32, Src, BitNo); | |||
23391 | } | |||
23392 | ||||
23393 | /// Turns an ISD::CondCode into a value suitable for SSE floating-point mask | |||
23394 | /// CMPs. | |||
23395 | static unsigned translateX86FSETCC(ISD::CondCode SetCCOpcode, SDValue &Op0, | |||
23396 | SDValue &Op1, bool &IsAlwaysSignaling) { | |||
23397 | unsigned SSECC; | |||
23398 | bool Swap = false; | |||
23399 | ||||
23400 | // SSE Condition code mapping: | |||
23401 | // 0 - EQ | |||
23402 | // 1 - LT | |||
23403 | // 2 - LE | |||
23404 | // 3 - UNORD | |||
23405 | // 4 - NEQ | |||
23406 | // 5 - NLT | |||
23407 | // 6 - NLE | |||
23408 | // 7 - ORD | |||
23409 | switch (SetCCOpcode) { | |||
23410 | default: llvm_unreachable("Unexpected SETCC condition")::llvm::llvm_unreachable_internal("Unexpected SETCC condition" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 23410); | |||
23411 | case ISD::SETOEQ: | |||
23412 | case ISD::SETEQ: SSECC = 0; break; | |||
23413 | case ISD::SETOGT: | |||
23414 | case ISD::SETGT: Swap = true; LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
23415 | case ISD::SETLT: | |||
23416 | case ISD::SETOLT: SSECC = 1; break; | |||
23417 | case ISD::SETOGE: | |||
23418 | case ISD::SETGE: Swap = true; LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
23419 | case ISD::SETLE: | |||
23420 | case ISD::SETOLE: SSECC = 2; break; | |||
23421 | case ISD::SETUO: SSECC = 3; break; | |||
23422 | case ISD::SETUNE: | |||
23423 | case ISD::SETNE: SSECC = 4; break; | |||
23424 | case ISD::SETULE: Swap = true; LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
23425 | case ISD::SETUGE: SSECC = 5; break; | |||
23426 | case ISD::SETULT: Swap = true; LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
23427 | case ISD::SETUGT: SSECC = 6; break; | |||
23428 | case ISD::SETO: SSECC = 7; break; | |||
23429 | case ISD::SETUEQ: SSECC = 8; break; | |||
23430 | case ISD::SETONE: SSECC = 12; break; | |||
23431 | } | |||
23432 | if (Swap) | |||
23433 | std::swap(Op0, Op1); | |||
23434 | ||||
23435 | switch (SetCCOpcode) { | |||
23436 | default: | |||
23437 | IsAlwaysSignaling = true; | |||
23438 | break; | |||
23439 | case ISD::SETEQ: | |||
23440 | case ISD::SETOEQ: | |||
23441 | case ISD::SETUEQ: | |||
23442 | case ISD::SETNE: | |||
23443 | case ISD::SETONE: | |||
23444 | case ISD::SETUNE: | |||
23445 | case ISD::SETO: | |||
23446 | case ISD::SETUO: | |||
23447 | IsAlwaysSignaling = false; | |||
23448 | break; | |||
23449 | } | |||
23450 | ||||
23451 | return SSECC; | |||
23452 | } | |||
23453 | ||||
23454 | /// Break a VSETCC 256-bit integer VSETCC into two new 128 ones and then | |||
23455 | /// concatenate the result back. | |||
23456 | static SDValue splitIntVSETCC(EVT VT, SDValue LHS, SDValue RHS, | |||
23457 | ISD::CondCode Cond, SelectionDAG &DAG, | |||
23458 | const SDLoc &dl) { | |||
23459 | assert(VT.isInteger() && VT == LHS.getValueType() &&(static_cast <bool> (VT.isInteger() && VT == LHS .getValueType() && VT == RHS.getValueType() && "Unsupported VTs!") ? void (0) : __assert_fail ("VT.isInteger() && VT == LHS.getValueType() && VT == RHS.getValueType() && \"Unsupported VTs!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 23460, __extension__ __PRETTY_FUNCTION__)) | |||
23460 | VT == RHS.getValueType() && "Unsupported VTs!")(static_cast <bool> (VT.isInteger() && VT == LHS .getValueType() && VT == RHS.getValueType() && "Unsupported VTs!") ? void (0) : __assert_fail ("VT.isInteger() && VT == LHS.getValueType() && VT == RHS.getValueType() && \"Unsupported VTs!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 23460, __extension__ __PRETTY_FUNCTION__)); | |||
23461 | ||||
23462 | SDValue CC = DAG.getCondCode(Cond); | |||
23463 | ||||
23464 | // Extract the LHS Lo/Hi vectors | |||
23465 | SDValue LHS1, LHS2; | |||
23466 | std::tie(LHS1, LHS2) = splitVector(LHS, DAG, dl); | |||
23467 | ||||
23468 | // Extract the RHS Lo/Hi vectors | |||
23469 | SDValue RHS1, RHS2; | |||
23470 | std::tie(RHS1, RHS2) = splitVector(RHS, DAG, dl); | |||
23471 | ||||
23472 | // Issue the operation on the smaller types and concatenate the result back | |||
23473 | EVT LoVT, HiVT; | |||
23474 | std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VT); | |||
23475 | return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, | |||
23476 | DAG.getNode(ISD::SETCC, dl, LoVT, LHS1, RHS1, CC), | |||
23477 | DAG.getNode(ISD::SETCC, dl, HiVT, LHS2, RHS2, CC)); | |||
23478 | } | |||
23479 | ||||
23480 | static SDValue LowerIntVSETCC_AVX512(SDValue Op, SelectionDAG &DAG) { | |||
23481 | ||||
23482 | SDValue Op0 = Op.getOperand(0); | |||
23483 | SDValue Op1 = Op.getOperand(1); | |||
23484 | SDValue CC = Op.getOperand(2); | |||
23485 | MVT VT = Op.getSimpleValueType(); | |||
23486 | SDLoc dl(Op); | |||
23487 | ||||
23488 | assert(VT.getVectorElementType() == MVT::i1 &&(static_cast <bool> (VT.getVectorElementType() == MVT:: i1 && "Cannot set masked compare for this operation") ? void (0) : __assert_fail ("VT.getVectorElementType() == MVT::i1 && \"Cannot set masked compare for this operation\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 23489, __extension__ __PRETTY_FUNCTION__)) | |||
23489 | "Cannot set masked compare for this operation")(static_cast <bool> (VT.getVectorElementType() == MVT:: i1 && "Cannot set masked compare for this operation") ? void (0) : __assert_fail ("VT.getVectorElementType() == MVT::i1 && \"Cannot set masked compare for this operation\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 23489, __extension__ __PRETTY_FUNCTION__)); | |||
23490 | ||||
23491 | ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get(); | |||
23492 | ||||
23493 | // Prefer SETGT over SETLT. | |||
23494 | if (SetCCOpcode == ISD::SETLT) { | |||
23495 | SetCCOpcode = ISD::getSetCCSwappedOperands(SetCCOpcode); | |||
23496 | std::swap(Op0, Op1); | |||
23497 | } | |||
23498 | ||||
23499 | return DAG.getSetCC(dl, VT, Op0, Op1, SetCCOpcode); | |||
23500 | } | |||
23501 | ||||
23502 | /// Given a buildvector constant, return a new vector constant with each element | |||
23503 | /// incremented or decremented. If incrementing or decrementing would result in | |||
23504 | /// unsigned overflow or underflow or this is not a simple vector constant, | |||
23505 | /// return an empty value. | |||
23506 | static SDValue incDecVectorConstant(SDValue V, SelectionDAG &DAG, bool IsInc) { | |||
23507 | auto *BV = dyn_cast<BuildVectorSDNode>(V.getNode()); | |||
23508 | if (!BV) | |||
23509 | return SDValue(); | |||
23510 | ||||
23511 | MVT VT = V.getSimpleValueType(); | |||
23512 | MVT EltVT = VT.getVectorElementType(); | |||
23513 | unsigned NumElts = VT.getVectorNumElements(); | |||
23514 | SmallVector<SDValue, 8> NewVecC; | |||
23515 | SDLoc DL(V); | |||
23516 | for (unsigned i = 0; i < NumElts; ++i) { | |||
23517 | auto *Elt = dyn_cast<ConstantSDNode>(BV->getOperand(i)); | |||
23518 | if (!Elt || Elt->isOpaque() || Elt->getSimpleValueType(0) != EltVT) | |||
23519 | return SDValue(); | |||
23520 | ||||
23521 | // Avoid overflow/underflow. | |||
23522 | const APInt &EltC = Elt->getAPIntValue(); | |||
23523 | if ((IsInc && EltC.isMaxValue()) || (!IsInc && EltC.isZero())) | |||
23524 | return SDValue(); | |||
23525 | ||||
23526 | NewVecC.push_back(DAG.getConstant(EltC + (IsInc ? 1 : -1), DL, EltVT)); | |||
23527 | } | |||
23528 | ||||
23529 | return DAG.getBuildVector(VT, DL, NewVecC); | |||
23530 | } | |||
23531 | ||||
23532 | /// As another special case, use PSUBUS[BW] when it's profitable. E.g. for | |||
23533 | /// Op0 u<= Op1: | |||
23534 | /// t = psubus Op0, Op1 | |||
23535 | /// pcmpeq t, <0..0> | |||
23536 | static SDValue LowerVSETCCWithSUBUS(SDValue Op0, SDValue Op1, MVT VT, | |||
23537 | ISD::CondCode Cond, const SDLoc &dl, | |||
23538 | const X86Subtarget &Subtarget, | |||
23539 | SelectionDAG &DAG) { | |||
23540 | if (!Subtarget.hasSSE2()) | |||
23541 | return SDValue(); | |||
23542 | ||||
23543 | MVT VET = VT.getVectorElementType(); | |||
23544 | if (VET != MVT::i8 && VET != MVT::i16) | |||
23545 | return SDValue(); | |||
23546 | ||||
23547 | switch (Cond) { | |||
23548 | default: | |||
23549 | return SDValue(); | |||
23550 | case ISD::SETULT: { | |||
23551 | // If the comparison is against a constant we can turn this into a | |||
23552 | // setule. With psubus, setule does not require a swap. This is | |||
23553 | // beneficial because the constant in the register is no longer | |||
23554 | // destructed as the destination so it can be hoisted out of a loop. | |||
23555 | // Only do this pre-AVX since vpcmp* is no longer destructive. | |||
23556 | if (Subtarget.hasAVX()) | |||
23557 | return SDValue(); | |||
23558 | SDValue ULEOp1 = incDecVectorConstant(Op1, DAG, /*IsInc*/false); | |||
23559 | if (!ULEOp1) | |||
23560 | return SDValue(); | |||
23561 | Op1 = ULEOp1; | |||
23562 | break; | |||
23563 | } | |||
23564 | case ISD::SETUGT: { | |||
23565 | // If the comparison is against a constant, we can turn this into a setuge. | |||
23566 | // This is beneficial because materializing a constant 0 for the PCMPEQ is | |||
23567 | // probably cheaper than XOR+PCMPGT using 2 different vector constants: | |||
23568 | // cmpgt (xor X, SignMaskC) CmpC --> cmpeq (usubsat (CmpC+1), X), 0 | |||
23569 | SDValue UGEOp1 = incDecVectorConstant(Op1, DAG, /*IsInc*/true); | |||
23570 | if (!UGEOp1) | |||
23571 | return SDValue(); | |||
23572 | Op1 = Op0; | |||
23573 | Op0 = UGEOp1; | |||
23574 | break; | |||
23575 | } | |||
23576 | // Psubus is better than flip-sign because it requires no inversion. | |||
23577 | case ISD::SETUGE: | |||
23578 | std::swap(Op0, Op1); | |||
23579 | break; | |||
23580 | case ISD::SETULE: | |||
23581 | break; | |||
23582 | } | |||
23583 | ||||
23584 | SDValue Result = DAG.getNode(ISD::USUBSAT, dl, VT, Op0, Op1); | |||
23585 | return DAG.getNode(X86ISD::PCMPEQ, dl, VT, Result, | |||
23586 | DAG.getConstant(0, dl, VT)); | |||
23587 | } | |||
23588 | ||||
23589 | static SDValue LowerVSETCC(SDValue Op, const X86Subtarget &Subtarget, | |||
23590 | SelectionDAG &DAG) { | |||
23591 | bool IsStrict = Op.getOpcode() == ISD::STRICT_FSETCC || | |||
23592 | Op.getOpcode() == ISD::STRICT_FSETCCS; | |||
23593 | SDValue Op0 = Op.getOperand(IsStrict ? 1 : 0); | |||
23594 | SDValue Op1 = Op.getOperand(IsStrict ? 2 : 1); | |||
23595 | SDValue CC = Op.getOperand(IsStrict ? 3 : 2); | |||
23596 | MVT VT = Op->getSimpleValueType(0); | |||
23597 | ISD::CondCode Cond = cast<CondCodeSDNode>(CC)->get(); | |||
23598 | bool isFP = Op1.getSimpleValueType().isFloatingPoint(); | |||
23599 | SDLoc dl(Op); | |||
23600 | ||||
23601 | if (isFP) { | |||
23602 | #ifndef NDEBUG | |||
23603 | MVT EltVT = Op0.getSimpleValueType().getVectorElementType(); | |||
23604 | assert(EltVT == MVT::f16 || EltVT == MVT::f32 || EltVT == MVT::f64)(static_cast <bool> (EltVT == MVT::f16 || EltVT == MVT:: f32 || EltVT == MVT::f64) ? void (0) : __assert_fail ("EltVT == MVT::f16 || EltVT == MVT::f32 || EltVT == MVT::f64" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 23604, __extension__ __PRETTY_FUNCTION__)); | |||
23605 | #endif | |||
23606 | ||||
23607 | bool IsSignaling = Op.getOpcode() == ISD::STRICT_FSETCCS; | |||
23608 | SDValue Chain = IsStrict ? Op.getOperand(0) : SDValue(); | |||
23609 | ||||
23610 | // If we have a strict compare with a vXi1 result and the input is 128/256 | |||
23611 | // bits we can't use a masked compare unless we have VLX. If we use a wider | |||
23612 | // compare like we do for non-strict, we might trigger spurious exceptions | |||
23613 | // from the upper elements. Instead emit a AVX compare and convert to mask. | |||
23614 | unsigned Opc; | |||
23615 | if (Subtarget.hasAVX512() && VT.getVectorElementType() == MVT::i1 && | |||
23616 | (!IsStrict || Subtarget.hasVLX() || | |||
23617 | Op0.getSimpleValueType().is512BitVector())) { | |||
23618 | #ifndef NDEBUG | |||
23619 | unsigned Num = VT.getVectorNumElements(); | |||
23620 | assert(Num <= 16 || (Num == 32 && EltVT == MVT::f16))(static_cast <bool> (Num <= 16 || (Num == 32 && EltVT == MVT::f16)) ? void (0) : __assert_fail ("Num <= 16 || (Num == 32 && EltVT == MVT::f16)" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 23620, __extension__ __PRETTY_FUNCTION__)); | |||
23621 | #endif | |||
23622 | Opc = IsStrict ? X86ISD::STRICT_CMPM : X86ISD::CMPM; | |||
23623 | } else { | |||
23624 | Opc = IsStrict ? X86ISD::STRICT_CMPP : X86ISD::CMPP; | |||
23625 | // The SSE/AVX packed FP comparison nodes are defined with a | |||
23626 | // floating-point vector result that matches the operand type. This allows | |||
23627 | // them to work with an SSE1 target (integer vector types are not legal). | |||
23628 | VT = Op0.getSimpleValueType(); | |||
23629 | } | |||
23630 | ||||
23631 | SDValue Cmp; | |||
23632 | bool IsAlwaysSignaling; | |||
23633 | unsigned SSECC = translateX86FSETCC(Cond, Op0, Op1, IsAlwaysSignaling); | |||
23634 | if (!Subtarget.hasAVX()) { | |||
23635 | // TODO: We could use following steps to handle a quiet compare with | |||
23636 | // signaling encodings. | |||
23637 | // 1. Get ordered masks from a quiet ISD::SETO | |||
23638 | // 2. Use the masks to mask potential unordered elements in operand A, B | |||
23639 | // 3. Get the compare results of masked A, B | |||
23640 | // 4. Calculating final result using the mask and result from 3 | |||
23641 | // But currently, we just fall back to scalar operations. | |||
23642 | if (IsStrict && IsAlwaysSignaling && !IsSignaling) | |||
23643 | return SDValue(); | |||
23644 | ||||
23645 | // Insert an extra signaling instruction to raise exception. | |||
23646 | if (IsStrict && !IsAlwaysSignaling && IsSignaling) { | |||
23647 | SDValue SignalCmp = DAG.getNode( | |||
23648 | Opc, dl, {VT, MVT::Other}, | |||
23649 | {Chain, Op0, Op1, DAG.getTargetConstant(1, dl, MVT::i8)}); // LT_OS | |||
23650 | // FIXME: It seems we need to update the flags of all new strict nodes. | |||
23651 | // Otherwise, mayRaiseFPException in MI will return false due to | |||
23652 | // NoFPExcept = false by default. However, I didn't find it in other | |||
23653 | // patches. | |||
23654 | SignalCmp->setFlags(Op->getFlags()); | |||
23655 | Chain = SignalCmp.getValue(1); | |||
23656 | } | |||
23657 | ||||
23658 | // In the two cases not handled by SSE compare predicates (SETUEQ/SETONE), | |||
23659 | // emit two comparisons and a logic op to tie them together. | |||
23660 | if (SSECC >= 8) { | |||
23661 | // LLVM predicate is SETUEQ or SETONE. | |||
23662 | unsigned CC0, CC1; | |||
23663 | unsigned CombineOpc; | |||
23664 | if (Cond == ISD::SETUEQ) { | |||
23665 | CC0 = 3; // UNORD | |||
23666 | CC1 = 0; // EQ | |||
23667 | CombineOpc = X86ISD::FOR; | |||
23668 | } else { | |||
23669 | assert(Cond == ISD::SETONE)(static_cast <bool> (Cond == ISD::SETONE) ? void (0) : __assert_fail ("Cond == ISD::SETONE", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 23669, __extension__ __PRETTY_FUNCTION__)); | |||
23670 | CC0 = 7; // ORD | |||
23671 | CC1 = 4; // NEQ | |||
23672 | CombineOpc = X86ISD::FAND; | |||
23673 | } | |||
23674 | ||||
23675 | SDValue Cmp0, Cmp1; | |||
23676 | if (IsStrict) { | |||
23677 | Cmp0 = DAG.getNode( | |||
23678 | Opc, dl, {VT, MVT::Other}, | |||
23679 | {Chain, Op0, Op1, DAG.getTargetConstant(CC0, dl, MVT::i8)}); | |||
23680 | Cmp1 = DAG.getNode( | |||
23681 | Opc, dl, {VT, MVT::Other}, | |||
23682 | {Chain, Op0, Op1, DAG.getTargetConstant(CC1, dl, MVT::i8)}); | |||
23683 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Cmp0.getValue(1), | |||
23684 | Cmp1.getValue(1)); | |||
23685 | } else { | |||
23686 | Cmp0 = DAG.getNode( | |||
23687 | Opc, dl, VT, Op0, Op1, DAG.getTargetConstant(CC0, dl, MVT::i8)); | |||
23688 | Cmp1 = DAG.getNode( | |||
23689 | Opc, dl, VT, Op0, Op1, DAG.getTargetConstant(CC1, dl, MVT::i8)); | |||
23690 | } | |||
23691 | Cmp = DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1); | |||
23692 | } else { | |||
23693 | if (IsStrict) { | |||
23694 | Cmp = DAG.getNode( | |||
23695 | Opc, dl, {VT, MVT::Other}, | |||
23696 | {Chain, Op0, Op1, DAG.getTargetConstant(SSECC, dl, MVT::i8)}); | |||
23697 | Chain = Cmp.getValue(1); | |||
23698 | } else | |||
23699 | Cmp = DAG.getNode( | |||
23700 | Opc, dl, VT, Op0, Op1, DAG.getTargetConstant(SSECC, dl, MVT::i8)); | |||
23701 | } | |||
23702 | } else { | |||
23703 | // Handle all other FP comparisons here. | |||
23704 | if (IsStrict) { | |||
23705 | // Make a flip on already signaling CCs before setting bit 4 of AVX CC. | |||
23706 | SSECC |= (IsAlwaysSignaling ^ IsSignaling) << 4; | |||
23707 | Cmp = DAG.getNode( | |||
23708 | Opc, dl, {VT, MVT::Other}, | |||
23709 | {Chain, Op0, Op1, DAG.getTargetConstant(SSECC, dl, MVT::i8)}); | |||
23710 | Chain = Cmp.getValue(1); | |||
23711 | } else | |||
23712 | Cmp = DAG.getNode( | |||
23713 | Opc, dl, VT, Op0, Op1, DAG.getTargetConstant(SSECC, dl, MVT::i8)); | |||
23714 | } | |||
23715 | ||||
23716 | if (VT.getFixedSizeInBits() > | |||
23717 | Op.getSimpleValueType().getFixedSizeInBits()) { | |||
23718 | // We emitted a compare with an XMM/YMM result. Finish converting to a | |||
23719 | // mask register using a vptestm. | |||
23720 | EVT CastVT = EVT(VT).changeVectorElementTypeToInteger(); | |||
23721 | Cmp = DAG.getBitcast(CastVT, Cmp); | |||
23722 | Cmp = DAG.getSetCC(dl, Op.getSimpleValueType(), Cmp, | |||
23723 | DAG.getConstant(0, dl, CastVT), ISD::SETNE); | |||
23724 | } else { | |||
23725 | // If this is SSE/AVX CMPP, bitcast the result back to integer to match | |||
23726 | // the result type of SETCC. The bitcast is expected to be optimized | |||
23727 | // away during combining/isel. | |||
23728 | Cmp = DAG.getBitcast(Op.getSimpleValueType(), Cmp); | |||
23729 | } | |||
23730 | ||||
23731 | if (IsStrict) | |||
23732 | return DAG.getMergeValues({Cmp, Chain}, dl); | |||
23733 | ||||
23734 | return Cmp; | |||
23735 | } | |||
23736 | ||||
23737 | assert(!IsStrict && "Strict SETCC only handles FP operands.")(static_cast <bool> (!IsStrict && "Strict SETCC only handles FP operands." ) ? void (0) : __assert_fail ("!IsStrict && \"Strict SETCC only handles FP operands.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 23737, __extension__ __PRETTY_FUNCTION__)); | |||
23738 | ||||
23739 | MVT VTOp0 = Op0.getSimpleValueType(); | |||
23740 | (void)VTOp0; | |||
23741 | assert(VTOp0 == Op1.getSimpleValueType() &&(static_cast <bool> (VTOp0 == Op1.getSimpleValueType() && "Expected operands with same type!") ? void (0) : __assert_fail ("VTOp0 == Op1.getSimpleValueType() && \"Expected operands with same type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 23742, __extension__ __PRETTY_FUNCTION__)) | |||
23742 | "Expected operands with same type!")(static_cast <bool> (VTOp0 == Op1.getSimpleValueType() && "Expected operands with same type!") ? void (0) : __assert_fail ("VTOp0 == Op1.getSimpleValueType() && \"Expected operands with same type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 23742, __extension__ __PRETTY_FUNCTION__)); | |||
23743 | assert(VT.getVectorNumElements() == VTOp0.getVectorNumElements() &&(static_cast <bool> (VT.getVectorNumElements() == VTOp0 .getVectorNumElements() && "Invalid number of packed elements for source and destination!" ) ? void (0) : __assert_fail ("VT.getVectorNumElements() == VTOp0.getVectorNumElements() && \"Invalid number of packed elements for source and destination!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 23744, __extension__ __PRETTY_FUNCTION__)) | |||
23744 | "Invalid number of packed elements for source and destination!")(static_cast <bool> (VT.getVectorNumElements() == VTOp0 .getVectorNumElements() && "Invalid number of packed elements for source and destination!" ) ? void (0) : __assert_fail ("VT.getVectorNumElements() == VTOp0.getVectorNumElements() && \"Invalid number of packed elements for source and destination!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 23744, __extension__ __PRETTY_FUNCTION__)); | |||
23745 | ||||
23746 | // The non-AVX512 code below works under the assumption that source and | |||
23747 | // destination types are the same. | |||
23748 | assert((Subtarget.hasAVX512() || (VT == VTOp0)) &&(static_cast <bool> ((Subtarget.hasAVX512() || (VT == VTOp0 )) && "Value types for source and destination must be the same!" ) ? void (0) : __assert_fail ("(Subtarget.hasAVX512() || (VT == VTOp0)) && \"Value types for source and destination must be the same!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 23749, __extension__ __PRETTY_FUNCTION__)) | |||
23749 | "Value types for source and destination must be the same!")(static_cast <bool> ((Subtarget.hasAVX512() || (VT == VTOp0 )) && "Value types for source and destination must be the same!" ) ? void (0) : __assert_fail ("(Subtarget.hasAVX512() || (VT == VTOp0)) && \"Value types for source and destination must be the same!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 23749, __extension__ __PRETTY_FUNCTION__)); | |||
23750 | ||||
23751 | // The result is boolean, but operands are int/float | |||
23752 | if (VT.getVectorElementType() == MVT::i1) { | |||
23753 | // In AVX-512 architecture setcc returns mask with i1 elements, | |||
23754 | // But there is no compare instruction for i8 and i16 elements in KNL. | |||
23755 | assert((VTOp0.getScalarSizeInBits() >= 32 || Subtarget.hasBWI()) &&(static_cast <bool> ((VTOp0.getScalarSizeInBits() >= 32 || Subtarget.hasBWI()) && "Unexpected operand type" ) ? void (0) : __assert_fail ("(VTOp0.getScalarSizeInBits() >= 32 || Subtarget.hasBWI()) && \"Unexpected operand type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 23756, __extension__ __PRETTY_FUNCTION__)) | |||
23756 | "Unexpected operand type")(static_cast <bool> ((VTOp0.getScalarSizeInBits() >= 32 || Subtarget.hasBWI()) && "Unexpected operand type" ) ? void (0) : __assert_fail ("(VTOp0.getScalarSizeInBits() >= 32 || Subtarget.hasBWI()) && \"Unexpected operand type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 23756, __extension__ __PRETTY_FUNCTION__)); | |||
23757 | return LowerIntVSETCC_AVX512(Op, DAG); | |||
23758 | } | |||
23759 | ||||
23760 | // Lower using XOP integer comparisons. | |||
23761 | if (VT.is128BitVector() && Subtarget.hasXOP()) { | |||
23762 | // Translate compare code to XOP PCOM compare mode. | |||
23763 | unsigned CmpMode = 0; | |||
23764 | switch (Cond) { | |||
23765 | default: llvm_unreachable("Unexpected SETCC condition")::llvm::llvm_unreachable_internal("Unexpected SETCC condition" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 23765); | |||
23766 | case ISD::SETULT: | |||
23767 | case ISD::SETLT: CmpMode = 0x00; break; | |||
23768 | case ISD::SETULE: | |||
23769 | case ISD::SETLE: CmpMode = 0x01; break; | |||
23770 | case ISD::SETUGT: | |||
23771 | case ISD::SETGT: CmpMode = 0x02; break; | |||
23772 | case ISD::SETUGE: | |||
23773 | case ISD::SETGE: CmpMode = 0x03; break; | |||
23774 | case ISD::SETEQ: CmpMode = 0x04; break; | |||
23775 | case ISD::SETNE: CmpMode = 0x05; break; | |||
23776 | } | |||
23777 | ||||
23778 | // Are we comparing unsigned or signed integers? | |||
23779 | unsigned Opc = | |||
23780 | ISD::isUnsignedIntSetCC(Cond) ? X86ISD::VPCOMU : X86ISD::VPCOM; | |||
23781 | ||||
23782 | return DAG.getNode(Opc, dl, VT, Op0, Op1, | |||
23783 | DAG.getTargetConstant(CmpMode, dl, MVT::i8)); | |||
23784 | } | |||
23785 | ||||
23786 | // (X & Y) != 0 --> (X & Y) == Y iff Y is power-of-2. | |||
23787 | // Revert part of the simplifySetCCWithAnd combine, to avoid an invert. | |||
23788 | if (Cond == ISD::SETNE && ISD::isBuildVectorAllZeros(Op1.getNode())) { | |||
23789 | SDValue BC0 = peekThroughBitcasts(Op0); | |||
23790 | if (BC0.getOpcode() == ISD::AND) { | |||
23791 | APInt UndefElts; | |||
23792 | SmallVector<APInt, 64> EltBits; | |||
23793 | if (getTargetConstantBitsFromNode(BC0.getOperand(1), | |||
23794 | VT.getScalarSizeInBits(), UndefElts, | |||
23795 | EltBits, false, false)) { | |||
23796 | if (llvm::all_of(EltBits, [](APInt &V) { return V.isPowerOf2(); })) { | |||
23797 | Cond = ISD::SETEQ; | |||
23798 | Op1 = DAG.getBitcast(VT, BC0.getOperand(1)); | |||
23799 | } | |||
23800 | } | |||
23801 | } | |||
23802 | } | |||
23803 | ||||
23804 | // ICMP_EQ(AND(X,C),C) -> SRA(SHL(X,LOG2(C)),BW-1) iff C is power-of-2. | |||
23805 | if (Cond == ISD::SETEQ && Op0.getOpcode() == ISD::AND && | |||
23806 | Op0.getOperand(1) == Op1 && Op0.hasOneUse()) { | |||
23807 | ConstantSDNode *C1 = isConstOrConstSplat(Op1); | |||
23808 | if (C1 && C1->getAPIntValue().isPowerOf2()) { | |||
23809 | unsigned BitWidth = VT.getScalarSizeInBits(); | |||
23810 | unsigned ShiftAmt = BitWidth - C1->getAPIntValue().logBase2() - 1; | |||
23811 | ||||
23812 | SDValue Result = Op0.getOperand(0); | |||
23813 | Result = DAG.getNode(ISD::SHL, dl, VT, Result, | |||
23814 | DAG.getConstant(ShiftAmt, dl, VT)); | |||
23815 | Result = DAG.getNode(ISD::SRA, dl, VT, Result, | |||
23816 | DAG.getConstant(BitWidth - 1, dl, VT)); | |||
23817 | return Result; | |||
23818 | } | |||
23819 | } | |||
23820 | ||||
23821 | // Break 256-bit integer vector compare into smaller ones. | |||
23822 | if (VT.is256BitVector() && !Subtarget.hasInt256()) | |||
23823 | return splitIntVSETCC(VT, Op0, Op1, Cond, DAG, dl); | |||
23824 | ||||
23825 | if (VT == MVT::v32i16 || VT == MVT::v64i8) { | |||
23826 | assert(!Subtarget.hasBWI() && "Unexpected VT with AVX512BW!")(static_cast <bool> (!Subtarget.hasBWI() && "Unexpected VT with AVX512BW!" ) ? void (0) : __assert_fail ("!Subtarget.hasBWI() && \"Unexpected VT with AVX512BW!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 23826, __extension__ __PRETTY_FUNCTION__)); | |||
23827 | return splitIntVSETCC(VT, Op0, Op1, Cond, DAG, dl); | |||
23828 | } | |||
23829 | ||||
23830 | // If we have a limit constant, try to form PCMPGT (signed cmp) to avoid | |||
23831 | // not-of-PCMPEQ: | |||
23832 | // X != INT_MIN --> X >s INT_MIN | |||
23833 | // X != INT_MAX --> X <s INT_MAX --> INT_MAX >s X | |||
23834 | // +X != 0 --> +X >s 0 | |||
23835 | APInt ConstValue; | |||
23836 | if (Cond == ISD::SETNE && | |||
23837 | ISD::isConstantSplatVector(Op1.getNode(), ConstValue)) { | |||
23838 | if (ConstValue.isMinSignedValue()) | |||
23839 | Cond = ISD::SETGT; | |||
23840 | else if (ConstValue.isMaxSignedValue()) | |||
23841 | Cond = ISD::SETLT; | |||
23842 | else if (ConstValue.isZero() && DAG.SignBitIsZero(Op0)) | |||
23843 | Cond = ISD::SETGT; | |||
23844 | } | |||
23845 | ||||
23846 | // If both operands are known non-negative, then an unsigned compare is the | |||
23847 | // same as a signed compare and there's no need to flip signbits. | |||
23848 | // TODO: We could check for more general simplifications here since we're | |||
23849 | // computing known bits. | |||
23850 | bool FlipSigns = ISD::isUnsignedIntSetCC(Cond) && | |||
23851 | !(DAG.SignBitIsZero(Op0) && DAG.SignBitIsZero(Op1)); | |||
23852 | ||||
23853 | // Special case: Use min/max operations for unsigned compares. | |||
23854 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
23855 | if (ISD::isUnsignedIntSetCC(Cond) && | |||
23856 | (FlipSigns || ISD::isTrueWhenEqual(Cond)) && | |||
23857 | TLI.isOperationLegal(ISD::UMIN, VT)) { | |||
23858 | // If we have a constant operand, increment/decrement it and change the | |||
23859 | // condition to avoid an invert. | |||
23860 | if (Cond == ISD::SETUGT) { | |||
23861 | // X > C --> X >= (C+1) --> X == umax(X, C+1) | |||
23862 | if (SDValue UGTOp1 = incDecVectorConstant(Op1, DAG, /*IsInc*/true)) { | |||
23863 | Op1 = UGTOp1; | |||
23864 | Cond = ISD::SETUGE; | |||
23865 | } | |||
23866 | } | |||
23867 | if (Cond == ISD::SETULT) { | |||
23868 | // X < C --> X <= (C-1) --> X == umin(X, C-1) | |||
23869 | if (SDValue ULTOp1 = incDecVectorConstant(Op1, DAG, /*IsInc*/false)) { | |||
23870 | Op1 = ULTOp1; | |||
23871 | Cond = ISD::SETULE; | |||
23872 | } | |||
23873 | } | |||
23874 | bool Invert = false; | |||
23875 | unsigned Opc; | |||
23876 | switch (Cond) { | |||
23877 | default: llvm_unreachable("Unexpected condition code")::llvm::llvm_unreachable_internal("Unexpected condition code" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 23877); | |||
23878 | case ISD::SETUGT: Invert = true; LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
23879 | case ISD::SETULE: Opc = ISD::UMIN; break; | |||
23880 | case ISD::SETULT: Invert = true; LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
23881 | case ISD::SETUGE: Opc = ISD::UMAX; break; | |||
23882 | } | |||
23883 | ||||
23884 | SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1); | |||
23885 | Result = DAG.getNode(X86ISD::PCMPEQ, dl, VT, Op0, Result); | |||
23886 | ||||
23887 | // If the logical-not of the result is required, perform that now. | |||
23888 | if (Invert) | |||
23889 | Result = DAG.getNOT(dl, Result, VT); | |||
23890 | ||||
23891 | return Result; | |||
23892 | } | |||
23893 | ||||
23894 | // Try to use SUBUS and PCMPEQ. | |||
23895 | if (FlipSigns) | |||
23896 | if (SDValue V = | |||
23897 | LowerVSETCCWithSUBUS(Op0, Op1, VT, Cond, dl, Subtarget, DAG)) | |||
23898 | return V; | |||
23899 | ||||
23900 | // We are handling one of the integer comparisons here. Since SSE only has | |||
23901 | // GT and EQ comparisons for integer, swapping operands and multiple | |||
23902 | // operations may be required for some comparisons. | |||
23903 | unsigned Opc = (Cond == ISD::SETEQ || Cond == ISD::SETNE) ? X86ISD::PCMPEQ | |||
23904 | : X86ISD::PCMPGT; | |||
23905 | bool Swap = Cond == ISD::SETLT || Cond == ISD::SETULT || | |||
23906 | Cond == ISD::SETGE || Cond == ISD::SETUGE; | |||
23907 | bool Invert = Cond == ISD::SETNE || | |||
23908 | (Cond != ISD::SETEQ && ISD::isTrueWhenEqual(Cond)); | |||
23909 | ||||
23910 | if (Swap) | |||
23911 | std::swap(Op0, Op1); | |||
23912 | ||||
23913 | // Check that the operation in question is available (most are plain SSE2, | |||
23914 | // but PCMPGTQ and PCMPEQQ have different requirements). | |||
23915 | if (VT == MVT::v2i64) { | |||
23916 | if (Opc == X86ISD::PCMPGT && !Subtarget.hasSSE42()) { | |||
23917 | assert(Subtarget.hasSSE2() && "Don't know how to lower!")(static_cast <bool> (Subtarget.hasSSE2() && "Don't know how to lower!" ) ? void (0) : __assert_fail ("Subtarget.hasSSE2() && \"Don't know how to lower!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 23917, __extension__ __PRETTY_FUNCTION__)); | |||
23918 | ||||
23919 | // Special case for sign bit test. We can use a v4i32 PCMPGT and shuffle | |||
23920 | // the odd elements over the even elements. | |||
23921 | if (!FlipSigns && !Invert && ISD::isBuildVectorAllZeros(Op0.getNode())) { | |||
23922 | Op0 = DAG.getConstant(0, dl, MVT::v4i32); | |||
23923 | Op1 = DAG.getBitcast(MVT::v4i32, Op1); | |||
23924 | ||||
23925 | SDValue GT = DAG.getNode(X86ISD::PCMPGT, dl, MVT::v4i32, Op0, Op1); | |||
23926 | static const int MaskHi[] = { 1, 1, 3, 3 }; | |||
23927 | SDValue Result = DAG.getVectorShuffle(MVT::v4i32, dl, GT, GT, MaskHi); | |||
23928 | ||||
23929 | return DAG.getBitcast(VT, Result); | |||
23930 | } | |||
23931 | ||||
23932 | if (!FlipSigns && !Invert && ISD::isBuildVectorAllOnes(Op1.getNode())) { | |||
23933 | Op0 = DAG.getBitcast(MVT::v4i32, Op0); | |||
23934 | Op1 = DAG.getConstant(-1, dl, MVT::v4i32); | |||
23935 | ||||
23936 | SDValue GT = DAG.getNode(X86ISD::PCMPGT, dl, MVT::v4i32, Op0, Op1); | |||
23937 | static const int MaskHi[] = { 1, 1, 3, 3 }; | |||
23938 | SDValue Result = DAG.getVectorShuffle(MVT::v4i32, dl, GT, GT, MaskHi); | |||
23939 | ||||
23940 | return DAG.getBitcast(VT, Result); | |||
23941 | } | |||
23942 | ||||
23943 | // Since SSE has no unsigned integer comparisons, we need to flip the sign | |||
23944 | // bits of the inputs before performing those operations. The lower | |||
23945 | // compare is always unsigned. | |||
23946 | SDValue SB; | |||
23947 | if (FlipSigns) { | |||
23948 | SB = DAG.getConstant(0x8000000080000000ULL, dl, MVT::v2i64); | |||
23949 | } else { | |||
23950 | SB = DAG.getConstant(0x0000000080000000ULL, dl, MVT::v2i64); | |||
23951 | } | |||
23952 | Op0 = DAG.getNode(ISD::XOR, dl, MVT::v2i64, Op0, SB); | |||
23953 | Op1 = DAG.getNode(ISD::XOR, dl, MVT::v2i64, Op1, SB); | |||
23954 | ||||
23955 | // Cast everything to the right type. | |||
23956 | Op0 = DAG.getBitcast(MVT::v4i32, Op0); | |||
23957 | Op1 = DAG.getBitcast(MVT::v4i32, Op1); | |||
23958 | ||||
23959 | // Emulate PCMPGTQ with (hi1 > hi2) | ((hi1 == hi2) & (lo1 > lo2)) | |||
23960 | SDValue GT = DAG.getNode(X86ISD::PCMPGT, dl, MVT::v4i32, Op0, Op1); | |||
23961 | SDValue EQ = DAG.getNode(X86ISD::PCMPEQ, dl, MVT::v4i32, Op0, Op1); | |||
23962 | ||||
23963 | // Create masks for only the low parts/high parts of the 64 bit integers. | |||
23964 | static const int MaskHi[] = { 1, 1, 3, 3 }; | |||
23965 | static const int MaskLo[] = { 0, 0, 2, 2 }; | |||
23966 | SDValue EQHi = DAG.getVectorShuffle(MVT::v4i32, dl, EQ, EQ, MaskHi); | |||
23967 | SDValue GTLo = DAG.getVectorShuffle(MVT::v4i32, dl, GT, GT, MaskLo); | |||
23968 | SDValue GTHi = DAG.getVectorShuffle(MVT::v4i32, dl, GT, GT, MaskHi); | |||
23969 | ||||
23970 | SDValue Result = DAG.getNode(ISD::AND, dl, MVT::v4i32, EQHi, GTLo); | |||
23971 | Result = DAG.getNode(ISD::OR, dl, MVT::v4i32, Result, GTHi); | |||
23972 | ||||
23973 | if (Invert) | |||
23974 | Result = DAG.getNOT(dl, Result, MVT::v4i32); | |||
23975 | ||||
23976 | return DAG.getBitcast(VT, Result); | |||
23977 | } | |||
23978 | ||||
23979 | if (Opc == X86ISD::PCMPEQ && !Subtarget.hasSSE41()) { | |||
23980 | // If pcmpeqq is missing but pcmpeqd is available synthesize pcmpeqq with | |||
23981 | // pcmpeqd + pshufd + pand. | |||
23982 | assert(Subtarget.hasSSE2() && !FlipSigns && "Don't know how to lower!")(static_cast <bool> (Subtarget.hasSSE2() && !FlipSigns && "Don't know how to lower!") ? void (0) : __assert_fail ("Subtarget.hasSSE2() && !FlipSigns && \"Don't know how to lower!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 23982, __extension__ __PRETTY_FUNCTION__)); | |||
23983 | ||||
23984 | // First cast everything to the right type. | |||
23985 | Op0 = DAG.getBitcast(MVT::v4i32, Op0); | |||
23986 | Op1 = DAG.getBitcast(MVT::v4i32, Op1); | |||
23987 | ||||
23988 | // Do the compare. | |||
23989 | SDValue Result = DAG.getNode(Opc, dl, MVT::v4i32, Op0, Op1); | |||
23990 | ||||
23991 | // Make sure the lower and upper halves are both all-ones. | |||
23992 | static const int Mask[] = { 1, 0, 3, 2 }; | |||
23993 | SDValue Shuf = DAG.getVectorShuffle(MVT::v4i32, dl, Result, Result, Mask); | |||
23994 | Result = DAG.getNode(ISD::AND, dl, MVT::v4i32, Result, Shuf); | |||
23995 | ||||
23996 | if (Invert) | |||
23997 | Result = DAG.getNOT(dl, Result, MVT::v4i32); | |||
23998 | ||||
23999 | return DAG.getBitcast(VT, Result); | |||
24000 | } | |||
24001 | } | |||
24002 | ||||
24003 | // Since SSE has no unsigned integer comparisons, we need to flip the sign | |||
24004 | // bits of the inputs before performing those operations. | |||
24005 | if (FlipSigns) { | |||
24006 | MVT EltVT = VT.getVectorElementType(); | |||
24007 | SDValue SM = DAG.getConstant(APInt::getSignMask(EltVT.getSizeInBits()), dl, | |||
24008 | VT); | |||
24009 | Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SM); | |||
24010 | Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SM); | |||
24011 | } | |||
24012 | ||||
24013 | SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1); | |||
24014 | ||||
24015 | // If the logical-not of the result is required, perform that now. | |||
24016 | if (Invert) | |||
24017 | Result = DAG.getNOT(dl, Result, VT); | |||
24018 | ||||
24019 | return Result; | |||
24020 | } | |||
24021 | ||||
24022 | // Try to select this as a KORTEST+SETCC or KTEST+SETCC if possible. | |||
24023 | static SDValue EmitAVX512Test(SDValue Op0, SDValue Op1, ISD::CondCode CC, | |||
24024 | const SDLoc &dl, SelectionDAG &DAG, | |||
24025 | const X86Subtarget &Subtarget, | |||
24026 | SDValue &X86CC) { | |||
24027 | // Only support equality comparisons. | |||
24028 | if (CC != ISD::SETEQ && CC != ISD::SETNE) | |||
24029 | return SDValue(); | |||
24030 | ||||
24031 | // Must be a bitcast from vXi1. | |||
24032 | if (Op0.getOpcode() != ISD::BITCAST) | |||
24033 | return SDValue(); | |||
24034 | ||||
24035 | Op0 = Op0.getOperand(0); | |||
24036 | MVT VT = Op0.getSimpleValueType(); | |||
24037 | if (!(Subtarget.hasAVX512() && VT == MVT::v16i1) && | |||
24038 | !(Subtarget.hasDQI() && VT == MVT::v8i1) && | |||
24039 | !(Subtarget.hasBWI() && (VT == MVT::v32i1 || VT == MVT::v64i1))) | |||
24040 | return SDValue(); | |||
24041 | ||||
24042 | X86::CondCode X86Cond; | |||
24043 | if (isNullConstant(Op1)) { | |||
24044 | X86Cond = CC == ISD::SETEQ ? X86::COND_E : X86::COND_NE; | |||
24045 | } else if (isAllOnesConstant(Op1)) { | |||
24046 | // C flag is set for all ones. | |||
24047 | X86Cond = CC == ISD::SETEQ ? X86::COND_B : X86::COND_AE; | |||
24048 | } else | |||
24049 | return SDValue(); | |||
24050 | ||||
24051 | // If the input is an AND, we can combine it's operands into the KTEST. | |||
24052 | bool KTestable = false; | |||
24053 | if (Subtarget.hasDQI() && (VT == MVT::v8i1 || VT == MVT::v16i1)) | |||
24054 | KTestable = true; | |||
24055 | if (Subtarget.hasBWI() && (VT == MVT::v32i1 || VT == MVT::v64i1)) | |||
24056 | KTestable = true; | |||
24057 | if (!isNullConstant(Op1)) | |||
24058 | KTestable = false; | |||
24059 | if (KTestable && Op0.getOpcode() == ISD::AND && Op0.hasOneUse()) { | |||
24060 | SDValue LHS = Op0.getOperand(0); | |||
24061 | SDValue RHS = Op0.getOperand(1); | |||
24062 | X86CC = DAG.getTargetConstant(X86Cond, dl, MVT::i8); | |||
24063 | return DAG.getNode(X86ISD::KTEST, dl, MVT::i32, LHS, RHS); | |||
24064 | } | |||
24065 | ||||
24066 | // If the input is an OR, we can combine it's operands into the KORTEST. | |||
24067 | SDValue LHS = Op0; | |||
24068 | SDValue RHS = Op0; | |||
24069 | if (Op0.getOpcode() == ISD::OR && Op0.hasOneUse()) { | |||
24070 | LHS = Op0.getOperand(0); | |||
24071 | RHS = Op0.getOperand(1); | |||
24072 | } | |||
24073 | ||||
24074 | X86CC = DAG.getTargetConstant(X86Cond, dl, MVT::i8); | |||
24075 | return DAG.getNode(X86ISD::KORTEST, dl, MVT::i32, LHS, RHS); | |||
24076 | } | |||
24077 | ||||
24078 | /// Emit flags for the given setcc condition and operands. Also returns the | |||
24079 | /// corresponding X86 condition code constant in X86CC. | |||
24080 | SDValue X86TargetLowering::emitFlagsForSetcc(SDValue Op0, SDValue Op1, | |||
24081 | ISD::CondCode CC, const SDLoc &dl, | |||
24082 | SelectionDAG &DAG, | |||
24083 | SDValue &X86CC) const { | |||
24084 | // Optimize to BT if possible. | |||
24085 | // Lower (X & (1 << N)) == 0 to BT(X, N). | |||
24086 | // Lower ((X >>u N) & 1) != 0 to BT(X, N). | |||
24087 | // Lower ((X >>s N) & 1) != 0 to BT(X, N). | |||
24088 | if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() && isNullConstant(Op1) && | |||
24089 | (CC == ISD::SETEQ || CC == ISD::SETNE)) { | |||
24090 | if (SDValue BT = LowerAndToBT(Op0, CC, dl, DAG, X86CC)) | |||
24091 | return BT; | |||
24092 | } | |||
24093 | ||||
24094 | // Try to use PTEST/PMOVMSKB for a tree ORs equality compared with 0. | |||
24095 | // TODO: We could do AND tree with all 1s as well by using the C flag. | |||
24096 | if (isNullConstant(Op1) && (CC == ISD::SETEQ || CC == ISD::SETNE)) | |||
24097 | if (SDValue CmpZ = | |||
24098 | MatchVectorAllZeroTest(Op0, CC, dl, Subtarget, DAG, X86CC)) | |||
24099 | return CmpZ; | |||
24100 | ||||
24101 | // Try to lower using KORTEST or KTEST. | |||
24102 | if (SDValue Test = EmitAVX512Test(Op0, Op1, CC, dl, DAG, Subtarget, X86CC)) | |||
24103 | return Test; | |||
24104 | ||||
24105 | // Look for X == 0, X == 1, X != 0, or X != 1. We can simplify some forms of | |||
24106 | // these. | |||
24107 | if ((isOneConstant(Op1) || isNullConstant(Op1)) && | |||
24108 | (CC == ISD::SETEQ || CC == ISD::SETNE)) { | |||
24109 | // If the input is a setcc, then reuse the input setcc or use a new one with | |||
24110 | // the inverted condition. | |||
24111 | if (Op0.getOpcode() == X86ISD::SETCC) { | |||
24112 | bool Invert = (CC == ISD::SETNE) ^ isNullConstant(Op1); | |||
24113 | ||||
24114 | X86CC = Op0.getOperand(0); | |||
24115 | if (Invert) { | |||
24116 | X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0); | |||
24117 | CCode = X86::GetOppositeBranchCondition(CCode); | |||
24118 | X86CC = DAG.getTargetConstant(CCode, dl, MVT::i8); | |||
24119 | } | |||
24120 | ||||
24121 | return Op0.getOperand(1); | |||
24122 | } | |||
24123 | } | |||
24124 | ||||
24125 | // Try to use the carry flag from the add in place of an separate CMP for: | |||
24126 | // (seteq (add X, -1), -1). Similar for setne. | |||
24127 | if (isAllOnesConstant(Op1) && Op0.getOpcode() == ISD::ADD && | |||
24128 | Op0.getOperand(1) == Op1 && (CC == ISD::SETEQ || CC == ISD::SETNE)) { | |||
24129 | if (isProfitableToUseFlagOp(Op0)) { | |||
24130 | SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32); | |||
24131 | ||||
24132 | SDValue New = DAG.getNode(X86ISD::ADD, dl, VTs, Op0.getOperand(0), | |||
24133 | Op0.getOperand(1)); | |||
24134 | DAG.ReplaceAllUsesOfValueWith(SDValue(Op0.getNode(), 0), New); | |||
24135 | X86::CondCode CCode = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B; | |||
24136 | X86CC = DAG.getTargetConstant(CCode, dl, MVT::i8); | |||
24137 | return SDValue(New.getNode(), 1); | |||
24138 | } | |||
24139 | } | |||
24140 | ||||
24141 | X86::CondCode CondCode = | |||
24142 | TranslateX86CC(CC, dl, /*IsFP*/ false, Op0, Op1, DAG); | |||
24143 | assert(CondCode != X86::COND_INVALID && "Unexpected condition code!")(static_cast <bool> (CondCode != X86::COND_INVALID && "Unexpected condition code!") ? void (0) : __assert_fail ("CondCode != X86::COND_INVALID && \"Unexpected condition code!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24143, __extension__ __PRETTY_FUNCTION__)); | |||
24144 | ||||
24145 | SDValue EFLAGS = EmitCmp(Op0, Op1, CondCode, dl, DAG, Subtarget); | |||
24146 | X86CC = DAG.getTargetConstant(CondCode, dl, MVT::i8); | |||
24147 | return EFLAGS; | |||
24148 | } | |||
24149 | ||||
24150 | SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const { | |||
24151 | ||||
24152 | bool IsStrict = Op.getOpcode() == ISD::STRICT_FSETCC || | |||
24153 | Op.getOpcode() == ISD::STRICT_FSETCCS; | |||
24154 | MVT VT = Op->getSimpleValueType(0); | |||
24155 | ||||
24156 | if (VT.isVector()) return LowerVSETCC(Op, Subtarget, DAG); | |||
24157 | ||||
24158 | assert(VT == MVT::i8 && "SetCC type must be 8-bit integer")(static_cast <bool> (VT == MVT::i8 && "SetCC type must be 8-bit integer" ) ? void (0) : __assert_fail ("VT == MVT::i8 && \"SetCC type must be 8-bit integer\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24158, __extension__ __PRETTY_FUNCTION__)); | |||
24159 | SDValue Chain = IsStrict ? Op.getOperand(0) : SDValue(); | |||
24160 | SDValue Op0 = Op.getOperand(IsStrict ? 1 : 0); | |||
24161 | SDValue Op1 = Op.getOperand(IsStrict ? 2 : 1); | |||
24162 | SDLoc dl(Op); | |||
24163 | ISD::CondCode CC = | |||
24164 | cast<CondCodeSDNode>(Op.getOperand(IsStrict ? 3 : 2))->get(); | |||
24165 | ||||
24166 | // Handle f128 first, since one possible outcome is a normal integer | |||
24167 | // comparison which gets handled by emitFlagsForSetcc. | |||
24168 | if (Op0.getValueType() == MVT::f128) { | |||
24169 | softenSetCCOperands(DAG, MVT::f128, Op0, Op1, CC, dl, Op0, Op1, Chain, | |||
24170 | Op.getOpcode() == ISD::STRICT_FSETCCS); | |||
24171 | ||||
24172 | // If softenSetCCOperands returned a scalar, use it. | |||
24173 | if (!Op1.getNode()) { | |||
24174 | assert(Op0.getValueType() == Op.getValueType() &&(static_cast <bool> (Op0.getValueType() == Op.getValueType () && "Unexpected setcc expansion!") ? void (0) : __assert_fail ("Op0.getValueType() == Op.getValueType() && \"Unexpected setcc expansion!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24175, __extension__ __PRETTY_FUNCTION__)) | |||
24175 | "Unexpected setcc expansion!")(static_cast <bool> (Op0.getValueType() == Op.getValueType () && "Unexpected setcc expansion!") ? void (0) : __assert_fail ("Op0.getValueType() == Op.getValueType() && \"Unexpected setcc expansion!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24175, __extension__ __PRETTY_FUNCTION__)); | |||
24176 | if (IsStrict) | |||
24177 | return DAG.getMergeValues({Op0, Chain}, dl); | |||
24178 | return Op0; | |||
24179 | } | |||
24180 | } | |||
24181 | ||||
24182 | if (Op0.getSimpleValueType().isInteger()) { | |||
24183 | // Attempt to canonicalize SGT/UGT -> SGE/UGE compares with constant which | |||
24184 | // reduces the number of EFLAGs bit reads (the GE conditions don't read ZF), | |||
24185 | // this may translate to less uops depending on uarch implementation. The | |||
24186 | // equivalent for SLE/ULE -> SLT/ULT isn't likely to happen as we already | |||
24187 | // canonicalize to that CondCode. | |||
24188 | // NOTE: Only do this if incrementing the constant doesn't increase the bit | |||
24189 | // encoding size - so it must either already be a i8 or i32 immediate, or it | |||
24190 | // shrinks down to that. We don't do this for any i64's to avoid additional | |||
24191 | // constant materializations. | |||
24192 | // TODO: Can we move this to TranslateX86CC to handle jumps/branches too? | |||
24193 | if (auto *Op1C = dyn_cast<ConstantSDNode>(Op1)) { | |||
24194 | const APInt &Op1Val = Op1C->getAPIntValue(); | |||
24195 | if (!Op1Val.isZero()) { | |||
24196 | // Ensure the constant+1 doesn't overflow. | |||
24197 | if ((CC == ISD::CondCode::SETGT && !Op1Val.isMaxSignedValue()) || | |||
24198 | (CC == ISD::CondCode::SETUGT && !Op1Val.isMaxValue())) { | |||
24199 | APInt Op1ValPlusOne = Op1Val + 1; | |||
24200 | if (Op1ValPlusOne.isSignedIntN(32) && | |||
24201 | (!Op1Val.isSignedIntN(8) || Op1ValPlusOne.isSignedIntN(8))) { | |||
24202 | Op1 = DAG.getConstant(Op1ValPlusOne, dl, Op0.getValueType()); | |||
24203 | CC = CC == ISD::CondCode::SETGT ? ISD::CondCode::SETGE | |||
24204 | : ISD::CondCode::SETUGE; | |||
24205 | } | |||
24206 | } | |||
24207 | } | |||
24208 | } | |||
24209 | ||||
24210 | SDValue X86CC; | |||
24211 | SDValue EFLAGS = emitFlagsForSetcc(Op0, Op1, CC, dl, DAG, X86CC); | |||
24212 | SDValue Res = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, X86CC, EFLAGS); | |||
24213 | return IsStrict ? DAG.getMergeValues({Res, Chain}, dl) : Res; | |||
24214 | } | |||
24215 | ||||
24216 | // Handle floating point. | |||
24217 | X86::CondCode CondCode = TranslateX86CC(CC, dl, /*IsFP*/ true, Op0, Op1, DAG); | |||
24218 | if (CondCode == X86::COND_INVALID) | |||
24219 | return SDValue(); | |||
24220 | ||||
24221 | SDValue EFLAGS; | |||
24222 | if (IsStrict) { | |||
24223 | bool IsSignaling = Op.getOpcode() == ISD::STRICT_FSETCCS; | |||
24224 | EFLAGS = | |||
24225 | DAG.getNode(IsSignaling ? X86ISD::STRICT_FCMPS : X86ISD::STRICT_FCMP, | |||
24226 | dl, {MVT::i32, MVT::Other}, {Chain, Op0, Op1}); | |||
24227 | Chain = EFLAGS.getValue(1); | |||
24228 | } else { | |||
24229 | EFLAGS = DAG.getNode(X86ISD::FCMP, dl, MVT::i32, Op0, Op1); | |||
24230 | } | |||
24231 | ||||
24232 | SDValue X86CC = DAG.getTargetConstant(CondCode, dl, MVT::i8); | |||
24233 | SDValue Res = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, X86CC, EFLAGS); | |||
24234 | return IsStrict ? DAG.getMergeValues({Res, Chain}, dl) : Res; | |||
24235 | } | |||
24236 | ||||
24237 | SDValue X86TargetLowering::LowerSETCCCARRY(SDValue Op, SelectionDAG &DAG) const { | |||
24238 | SDValue LHS = Op.getOperand(0); | |||
24239 | SDValue RHS = Op.getOperand(1); | |||
24240 | SDValue Carry = Op.getOperand(2); | |||
24241 | SDValue Cond = Op.getOperand(3); | |||
24242 | SDLoc DL(Op); | |||
24243 | ||||
24244 | assert(LHS.getSimpleValueType().isInteger() && "SETCCCARRY is integer only.")(static_cast <bool> (LHS.getSimpleValueType().isInteger () && "SETCCCARRY is integer only.") ? void (0) : __assert_fail ("LHS.getSimpleValueType().isInteger() && \"SETCCCARRY is integer only.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24244, __extension__ __PRETTY_FUNCTION__)); | |||
24245 | X86::CondCode CC = TranslateIntegerX86CC(cast<CondCodeSDNode>(Cond)->get()); | |||
24246 | ||||
24247 | // Recreate the carry if needed. | |||
24248 | EVT CarryVT = Carry.getValueType(); | |||
24249 | Carry = DAG.getNode(X86ISD::ADD, DL, DAG.getVTList(CarryVT, MVT::i32), | |||
24250 | Carry, DAG.getAllOnesConstant(DL, CarryVT)); | |||
24251 | ||||
24252 | SDVTList VTs = DAG.getVTList(LHS.getValueType(), MVT::i32); | |||
24253 | SDValue Cmp = DAG.getNode(X86ISD::SBB, DL, VTs, LHS, RHS, Carry.getValue(1)); | |||
24254 | return getSETCC(CC, Cmp.getValue(1), DL, DAG); | |||
24255 | } | |||
24256 | ||||
24257 | // This function returns three things: the arithmetic computation itself | |||
24258 | // (Value), an EFLAGS result (Overflow), and a condition code (Cond). The | |||
24259 | // flag and the condition code define the case in which the arithmetic | |||
24260 | // computation overflows. | |||
24261 | static std::pair<SDValue, SDValue> | |||
24262 | getX86XALUOOp(X86::CondCode &Cond, SDValue Op, SelectionDAG &DAG) { | |||
24263 | assert(Op.getResNo() == 0 && "Unexpected result number!")(static_cast <bool> (Op.getResNo() == 0 && "Unexpected result number!" ) ? void (0) : __assert_fail ("Op.getResNo() == 0 && \"Unexpected result number!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24263, __extension__ __PRETTY_FUNCTION__)); | |||
24264 | SDValue Value, Overflow; | |||
24265 | SDValue LHS = Op.getOperand(0); | |||
24266 | SDValue RHS = Op.getOperand(1); | |||
24267 | unsigned BaseOp = 0; | |||
24268 | SDLoc DL(Op); | |||
24269 | switch (Op.getOpcode()) { | |||
24270 | default: llvm_unreachable("Unknown ovf instruction!")::llvm::llvm_unreachable_internal("Unknown ovf instruction!", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24270); | |||
24271 | case ISD::SADDO: | |||
24272 | BaseOp = X86ISD::ADD; | |||
24273 | Cond = X86::COND_O; | |||
24274 | break; | |||
24275 | case ISD::UADDO: | |||
24276 | BaseOp = X86ISD::ADD; | |||
24277 | Cond = isOneConstant(RHS) ? X86::COND_E : X86::COND_B; | |||
24278 | break; | |||
24279 | case ISD::SSUBO: | |||
24280 | BaseOp = X86ISD::SUB; | |||
24281 | Cond = X86::COND_O; | |||
24282 | break; | |||
24283 | case ISD::USUBO: | |||
24284 | BaseOp = X86ISD::SUB; | |||
24285 | Cond = X86::COND_B; | |||
24286 | break; | |||
24287 | case ISD::SMULO: | |||
24288 | BaseOp = X86ISD::SMUL; | |||
24289 | Cond = X86::COND_O; | |||
24290 | break; | |||
24291 | case ISD::UMULO: | |||
24292 | BaseOp = X86ISD::UMUL; | |||
24293 | Cond = X86::COND_O; | |||
24294 | break; | |||
24295 | } | |||
24296 | ||||
24297 | if (BaseOp) { | |||
24298 | // Also sets EFLAGS. | |||
24299 | SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32); | |||
24300 | Value = DAG.getNode(BaseOp, DL, VTs, LHS, RHS); | |||
24301 | Overflow = Value.getValue(1); | |||
24302 | } | |||
24303 | ||||
24304 | return std::make_pair(Value, Overflow); | |||
24305 | } | |||
24306 | ||||
24307 | static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) { | |||
24308 | // Lower the "add/sub/mul with overflow" instruction into a regular ins plus | |||
24309 | // a "setcc" instruction that checks the overflow flag. The "brcond" lowering | |||
24310 | // looks for this combo and may remove the "setcc" instruction if the "setcc" | |||
24311 | // has only one use. | |||
24312 | SDLoc DL(Op); | |||
24313 | X86::CondCode Cond; | |||
24314 | SDValue Value, Overflow; | |||
24315 | std::tie(Value, Overflow) = getX86XALUOOp(Cond, Op, DAG); | |||
24316 | ||||
24317 | SDValue SetCC = getSETCC(Cond, Overflow, DL, DAG); | |||
24318 | assert(Op->getValueType(1) == MVT::i8 && "Unexpected VT!")(static_cast <bool> (Op->getValueType(1) == MVT::i8 && "Unexpected VT!") ? void (0) : __assert_fail ("Op->getValueType(1) == MVT::i8 && \"Unexpected VT!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24318, __extension__ __PRETTY_FUNCTION__)); | |||
24319 | return DAG.getNode(ISD::MERGE_VALUES, DL, Op->getVTList(), Value, SetCC); | |||
24320 | } | |||
24321 | ||||
24322 | /// Return true if opcode is a X86 logical comparison. | |||
24323 | static bool isX86LogicalCmp(SDValue Op) { | |||
24324 | unsigned Opc = Op.getOpcode(); | |||
24325 | if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI || | |||
24326 | Opc == X86ISD::FCMP) | |||
24327 | return true; | |||
24328 | if (Op.getResNo() == 1 && | |||
24329 | (Opc == X86ISD::ADD || Opc == X86ISD::SUB || Opc == X86ISD::ADC || | |||
24330 | Opc == X86ISD::SBB || Opc == X86ISD::SMUL || Opc == X86ISD::UMUL || | |||
24331 | Opc == X86ISD::OR || Opc == X86ISD::XOR || Opc == X86ISD::AND)) | |||
24332 | return true; | |||
24333 | ||||
24334 | return false; | |||
24335 | } | |||
24336 | ||||
24337 | static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) { | |||
24338 | if (V.getOpcode() != ISD::TRUNCATE) | |||
24339 | return false; | |||
24340 | ||||
24341 | SDValue VOp0 = V.getOperand(0); | |||
24342 | unsigned InBits = VOp0.getValueSizeInBits(); | |||
24343 | unsigned Bits = V.getValueSizeInBits(); | |||
24344 | return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits)); | |||
24345 | } | |||
24346 | ||||
24347 | SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const { | |||
24348 | bool AddTest = true; | |||
24349 | SDValue Cond = Op.getOperand(0); | |||
24350 | SDValue Op1 = Op.getOperand(1); | |||
24351 | SDValue Op2 = Op.getOperand(2); | |||
24352 | SDLoc DL(Op); | |||
24353 | MVT VT = Op1.getSimpleValueType(); | |||
24354 | SDValue CC; | |||
24355 | ||||
24356 | // Lower FP selects into a CMP/AND/ANDN/OR sequence when the necessary SSE ops | |||
24357 | // are available or VBLENDV if AVX is available. | |||
24358 | // Otherwise FP cmovs get lowered into a less efficient branch sequence later. | |||
24359 | if (Cond.getOpcode() == ISD::SETCC && isScalarFPTypeInSSEReg(VT) && | |||
24360 | VT == Cond.getOperand(0).getSimpleValueType() && Cond->hasOneUse()) { | |||
24361 | SDValue CondOp0 = Cond.getOperand(0), CondOp1 = Cond.getOperand(1); | |||
24362 | bool IsAlwaysSignaling; | |||
24363 | unsigned SSECC = | |||
24364 | translateX86FSETCC(cast<CondCodeSDNode>(Cond.getOperand(2))->get(), | |||
24365 | CondOp0, CondOp1, IsAlwaysSignaling); | |||
24366 | ||||
24367 | if (Subtarget.hasAVX512()) { | |||
24368 | SDValue Cmp = | |||
24369 | DAG.getNode(X86ISD::FSETCCM, DL, MVT::v1i1, CondOp0, CondOp1, | |||
24370 | DAG.getTargetConstant(SSECC, DL, MVT::i8)); | |||
24371 | assert(!VT.isVector() && "Not a scalar type?")(static_cast <bool> (!VT.isVector() && "Not a scalar type?" ) ? void (0) : __assert_fail ("!VT.isVector() && \"Not a scalar type?\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24371, __extension__ __PRETTY_FUNCTION__)); | |||
24372 | return DAG.getNode(X86ISD::SELECTS, DL, VT, Cmp, Op1, Op2); | |||
24373 | } | |||
24374 | ||||
24375 | if (SSECC < 8 || Subtarget.hasAVX()) { | |||
24376 | SDValue Cmp = DAG.getNode(X86ISD::FSETCC, DL, VT, CondOp0, CondOp1, | |||
24377 | DAG.getTargetConstant(SSECC, DL, MVT::i8)); | |||
24378 | ||||
24379 | // If we have AVX, we can use a variable vector select (VBLENDV) instead | |||
24380 | // of 3 logic instructions for size savings and potentially speed. | |||
24381 | // Unfortunately, there is no scalar form of VBLENDV. | |||
24382 | ||||
24383 | // If either operand is a +0.0 constant, don't try this. We can expect to | |||
24384 | // optimize away at least one of the logic instructions later in that | |||
24385 | // case, so that sequence would be faster than a variable blend. | |||
24386 | ||||
24387 | // BLENDV was introduced with SSE 4.1, but the 2 register form implicitly | |||
24388 | // uses XMM0 as the selection register. That may need just as many | |||
24389 | // instructions as the AND/ANDN/OR sequence due to register moves, so | |||
24390 | // don't bother. | |||
24391 | if (Subtarget.hasAVX() && !isNullFPConstant(Op1) && | |||
24392 | !isNullFPConstant(Op2)) { | |||
24393 | // Convert to vectors, do a VSELECT, and convert back to scalar. | |||
24394 | // All of the conversions should be optimized away. | |||
24395 | MVT VecVT = VT == MVT::f32 ? MVT::v4f32 : MVT::v2f64; | |||
24396 | SDValue VOp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VecVT, Op1); | |||
24397 | SDValue VOp2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VecVT, Op2); | |||
24398 | SDValue VCmp = DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VecVT, Cmp); | |||
24399 | ||||
24400 | MVT VCmpVT = VT == MVT::f32 ? MVT::v4i32 : MVT::v2i64; | |||
24401 | VCmp = DAG.getBitcast(VCmpVT, VCmp); | |||
24402 | ||||
24403 | SDValue VSel = DAG.getSelect(DL, VecVT, VCmp, VOp1, VOp2); | |||
24404 | ||||
24405 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, | |||
24406 | VSel, DAG.getIntPtrConstant(0, DL)); | |||
24407 | } | |||
24408 | SDValue AndN = DAG.getNode(X86ISD::FANDN, DL, VT, Cmp, Op2); | |||
24409 | SDValue And = DAG.getNode(X86ISD::FAND, DL, VT, Cmp, Op1); | |||
24410 | return DAG.getNode(X86ISD::FOR, DL, VT, AndN, And); | |||
24411 | } | |||
24412 | } | |||
24413 | ||||
24414 | // AVX512 fallback is to lower selects of scalar floats to masked moves. | |||
24415 | if (isScalarFPTypeInSSEReg(VT) && Subtarget.hasAVX512()) { | |||
24416 | SDValue Cmp = DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, MVT::v1i1, Cond); | |||
24417 | return DAG.getNode(X86ISD::SELECTS, DL, VT, Cmp, Op1, Op2); | |||
24418 | } | |||
24419 | ||||
24420 | if (Cond.getOpcode() == ISD::SETCC) { | |||
24421 | if (SDValue NewCond = LowerSETCC(Cond, DAG)) { | |||
24422 | Cond = NewCond; | |||
24423 | // If the condition was updated, it's possible that the operands of the | |||
24424 | // select were also updated (for example, EmitTest has a RAUW). Refresh | |||
24425 | // the local references to the select operands in case they got stale. | |||
24426 | Op1 = Op.getOperand(1); | |||
24427 | Op2 = Op.getOperand(2); | |||
24428 | } | |||
24429 | } | |||
24430 | ||||
24431 | // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y | |||
24432 | // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y | |||
24433 | // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y | |||
24434 | // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y | |||
24435 | // (select (and (x , 0x1) == 0), y, (z ^ y) ) -> (-(and (x , 0x1)) & z ) ^ y | |||
24436 | // (select (and (x , 0x1) == 0), y, (z | y) ) -> (-(and (x , 0x1)) & z ) | y | |||
24437 | if (Cond.getOpcode() == X86ISD::SETCC && | |||
24438 | Cond.getOperand(1).getOpcode() == X86ISD::CMP && | |||
24439 | isNullConstant(Cond.getOperand(1).getOperand(1))) { | |||
24440 | SDValue Cmp = Cond.getOperand(1); | |||
24441 | SDValue CmpOp0 = Cmp.getOperand(0); | |||
24442 | unsigned CondCode = Cond.getConstantOperandVal(0); | |||
24443 | ||||
24444 | // Special handling for __builtin_ffs(X) - 1 pattern which looks like | |||
24445 | // (select (seteq X, 0), -1, (cttz_zero_undef X)). Disable the special | |||
24446 | // handle to keep the CMP with 0. This should be removed by | |||
24447 | // optimizeCompareInst by using the flags from the BSR/TZCNT used for the | |||
24448 | // cttz_zero_undef. | |||
24449 | auto MatchFFSMinus1 = [&](SDValue Op1, SDValue Op2) { | |||
24450 | return (Op1.getOpcode() == ISD::CTTZ_ZERO_UNDEF && Op1.hasOneUse() && | |||
24451 | Op1.getOperand(0) == CmpOp0 && isAllOnesConstant(Op2)); | |||
24452 | }; | |||
24453 | if (Subtarget.hasCMov() && (VT == MVT::i32 || VT == MVT::i64) && | |||
24454 | ((CondCode == X86::COND_NE && MatchFFSMinus1(Op1, Op2)) || | |||
24455 | (CondCode == X86::COND_E && MatchFFSMinus1(Op2, Op1)))) { | |||
24456 | // Keep Cmp. | |||
24457 | } else if ((isAllOnesConstant(Op1) || isAllOnesConstant(Op2)) && | |||
24458 | (CondCode == X86::COND_E || CondCode == X86::COND_NE)) { | |||
24459 | SDValue Y = isAllOnesConstant(Op2) ? Op1 : Op2; | |||
24460 | ||||
24461 | SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32); | |||
24462 | SDVTList CmpVTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32); | |||
24463 | ||||
24464 | // Apply further optimizations for special cases | |||
24465 | // (select (x != 0), -1, 0) -> neg & sbb | |||
24466 | // (select (x == 0), 0, -1) -> neg & sbb | |||
24467 | if (isNullConstant(Y) && | |||
24468 | (isAllOnesConstant(Op1) == (CondCode == X86::COND_NE))) { | |||
24469 | SDValue Zero = DAG.getConstant(0, DL, CmpOp0.getValueType()); | |||
24470 | SDValue Neg = DAG.getNode(X86ISD::SUB, DL, CmpVTs, Zero, CmpOp0); | |||
24471 | Zero = DAG.getConstant(0, DL, Op.getValueType()); | |||
24472 | return DAG.getNode(X86ISD::SBB, DL, VTs, Zero, Zero, Neg.getValue(1)); | |||
24473 | } | |||
24474 | ||||
24475 | Cmp = DAG.getNode(X86ISD::SUB, DL, CmpVTs, | |||
24476 | CmpOp0, DAG.getConstant(1, DL, CmpOp0.getValueType())); | |||
24477 | ||||
24478 | SDValue Zero = DAG.getConstant(0, DL, Op.getValueType()); | |||
24479 | SDValue Res = // Res = 0 or -1. | |||
24480 | DAG.getNode(X86ISD::SBB, DL, VTs, Zero, Zero, Cmp.getValue(1)); | |||
24481 | ||||
24482 | if (isAllOnesConstant(Op1) != (CondCode == X86::COND_E)) | |||
24483 | Res = DAG.getNOT(DL, Res, Res.getValueType()); | |||
24484 | ||||
24485 | return DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y); | |||
24486 | } else if (!Subtarget.hasCMov() && CondCode == X86::COND_E && | |||
24487 | Cmp.getOperand(0).getOpcode() == ISD::AND && | |||
24488 | isOneConstant(Cmp.getOperand(0).getOperand(1))) { | |||
24489 | SDValue Src1, Src2; | |||
24490 | // true if Op2 is XOR or OR operator and one of its operands | |||
24491 | // is equal to Op1 | |||
24492 | // ( a , a op b) || ( b , a op b) | |||
24493 | auto isOrXorPattern = [&]() { | |||
24494 | if ((Op2.getOpcode() == ISD::XOR || Op2.getOpcode() == ISD::OR) && | |||
24495 | (Op2.getOperand(0) == Op1 || Op2.getOperand(1) == Op1)) { | |||
24496 | Src1 = | |||
24497 | Op2.getOperand(0) == Op1 ? Op2.getOperand(1) : Op2.getOperand(0); | |||
24498 | Src2 = Op1; | |||
24499 | return true; | |||
24500 | } | |||
24501 | return false; | |||
24502 | }; | |||
24503 | ||||
24504 | if (isOrXorPattern()) { | |||
24505 | SDValue Neg; | |||
24506 | unsigned int CmpSz = CmpOp0.getSimpleValueType().getSizeInBits(); | |||
24507 | // we need mask of all zeros or ones with same size of the other | |||
24508 | // operands. | |||
24509 | if (CmpSz > VT.getSizeInBits()) | |||
24510 | Neg = DAG.getNode(ISD::TRUNCATE, DL, VT, CmpOp0); | |||
24511 | else if (CmpSz < VT.getSizeInBits()) | |||
24512 | Neg = DAG.getNode(ISD::AND, DL, VT, | |||
24513 | DAG.getNode(ISD::ANY_EXTEND, DL, VT, CmpOp0.getOperand(0)), | |||
24514 | DAG.getConstant(1, DL, VT)); | |||
24515 | else | |||
24516 | Neg = CmpOp0; | |||
24517 | SDValue Mask = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), | |||
24518 | Neg); // -(and (x, 0x1)) | |||
24519 | SDValue And = DAG.getNode(ISD::AND, DL, VT, Mask, Src1); // Mask & z | |||
24520 | return DAG.getNode(Op2.getOpcode(), DL, VT, And, Src2); // And Op y | |||
24521 | } | |||
24522 | } | |||
24523 | } | |||
24524 | ||||
24525 | // Look past (and (setcc_carry (cmp ...)), 1). | |||
24526 | if (Cond.getOpcode() == ISD::AND && | |||
24527 | Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY && | |||
24528 | isOneConstant(Cond.getOperand(1))) | |||
24529 | Cond = Cond.getOperand(0); | |||
24530 | ||||
24531 | // If condition flag is set by a X86ISD::CMP, then use it as the condition | |||
24532 | // setting operand in place of the X86ISD::SETCC. | |||
24533 | unsigned CondOpcode = Cond.getOpcode(); | |||
24534 | if (CondOpcode == X86ISD::SETCC || | |||
24535 | CondOpcode == X86ISD::SETCC_CARRY) { | |||
24536 | CC = Cond.getOperand(0); | |||
24537 | ||||
24538 | SDValue Cmp = Cond.getOperand(1); | |||
24539 | bool IllegalFPCMov = false; | |||
24540 | if (VT.isFloatingPoint() && !VT.isVector() && | |||
24541 | !isScalarFPTypeInSSEReg(VT) && Subtarget.hasCMov()) // FPStack? | |||
24542 | IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue()); | |||
24543 | ||||
24544 | if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) || | |||
24545 | Cmp.getOpcode() == X86ISD::BT) { // FIXME | |||
24546 | Cond = Cmp; | |||
24547 | AddTest = false; | |||
24548 | } | |||
24549 | } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO || | |||
24550 | CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO || | |||
24551 | CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) { | |||
24552 | SDValue Value; | |||
24553 | X86::CondCode X86Cond; | |||
24554 | std::tie(Value, Cond) = getX86XALUOOp(X86Cond, Cond.getValue(0), DAG); | |||
24555 | ||||
24556 | CC = DAG.getTargetConstant(X86Cond, DL, MVT::i8); | |||
24557 | AddTest = false; | |||
24558 | } | |||
24559 | ||||
24560 | if (AddTest) { | |||
24561 | // Look past the truncate if the high bits are known zero. | |||
24562 | if (isTruncWithZeroHighBitsInput(Cond, DAG)) | |||
24563 | Cond = Cond.getOperand(0); | |||
24564 | ||||
24565 | // We know the result of AND is compared against zero. Try to match | |||
24566 | // it to BT. | |||
24567 | if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) { | |||
24568 | SDValue BTCC; | |||
24569 | if (SDValue BT = LowerAndToBT(Cond, ISD::SETNE, DL, DAG, BTCC)) { | |||
24570 | CC = BTCC; | |||
24571 | Cond = BT; | |||
24572 | AddTest = false; | |||
24573 | } | |||
24574 | } | |||
24575 | } | |||
24576 | ||||
24577 | if (AddTest) { | |||
24578 | CC = DAG.getTargetConstant(X86::COND_NE, DL, MVT::i8); | |||
24579 | Cond = EmitTest(Cond, X86::COND_NE, DL, DAG, Subtarget); | |||
24580 | } | |||
24581 | ||||
24582 | // a < b ? -1 : 0 -> RES = ~setcc_carry | |||
24583 | // a < b ? 0 : -1 -> RES = setcc_carry | |||
24584 | // a >= b ? -1 : 0 -> RES = setcc_carry | |||
24585 | // a >= b ? 0 : -1 -> RES = ~setcc_carry | |||
24586 | if (Cond.getOpcode() == X86ISD::SUB) { | |||
24587 | unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue(); | |||
24588 | ||||
24589 | if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) && | |||
24590 | (isAllOnesConstant(Op1) || isAllOnesConstant(Op2)) && | |||
24591 | (isNullConstant(Op1) || isNullConstant(Op2))) { | |||
24592 | SDValue Res = | |||
24593 | DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(), | |||
24594 | DAG.getTargetConstant(X86::COND_B, DL, MVT::i8), Cond); | |||
24595 | if (isAllOnesConstant(Op1) != (CondCode == X86::COND_B)) | |||
24596 | return DAG.getNOT(DL, Res, Res.getValueType()); | |||
24597 | return Res; | |||
24598 | } | |||
24599 | } | |||
24600 | ||||
24601 | // X86 doesn't have an i8 cmov. If both operands are the result of a truncate | |||
24602 | // widen the cmov and push the truncate through. This avoids introducing a new | |||
24603 | // branch during isel and doesn't add any extensions. | |||
24604 | if (Op.getValueType() == MVT::i8 && | |||
24605 | Op1.getOpcode() == ISD::TRUNCATE && Op2.getOpcode() == ISD::TRUNCATE) { | |||
24606 | SDValue T1 = Op1.getOperand(0), T2 = Op2.getOperand(0); | |||
24607 | if (T1.getValueType() == T2.getValueType() && | |||
24608 | // Exclude CopyFromReg to avoid partial register stalls. | |||
24609 | T1.getOpcode() != ISD::CopyFromReg && T2.getOpcode()!=ISD::CopyFromReg){ | |||
24610 | SDValue Cmov = DAG.getNode(X86ISD::CMOV, DL, T1.getValueType(), T2, T1, | |||
24611 | CC, Cond); | |||
24612 | return DAG.getNode(ISD::TRUNCATE, DL, Op.getValueType(), Cmov); | |||
24613 | } | |||
24614 | } | |||
24615 | ||||
24616 | // Or finally, promote i8 cmovs if we have CMOV, | |||
24617 | // or i16 cmovs if it won't prevent folding a load. | |||
24618 | // FIXME: we should not limit promotion of i8 case to only when the CMOV is | |||
24619 | // legal, but EmitLoweredSelect() can not deal with these extensions | |||
24620 | // being inserted between two CMOV's. (in i16 case too TBN) | |||
24621 | // https://bugs.llvm.org/show_bug.cgi?id=40974 | |||
24622 | if ((Op.getValueType() == MVT::i8 && Subtarget.hasCMov()) || | |||
24623 | (Op.getValueType() == MVT::i16 && !MayFoldLoad(Op1) && | |||
24624 | !MayFoldLoad(Op2))) { | |||
24625 | Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, Op1); | |||
24626 | Op2 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, Op2); | |||
24627 | SDValue Ops[] = { Op2, Op1, CC, Cond }; | |||
24628 | SDValue Cmov = DAG.getNode(X86ISD::CMOV, DL, MVT::i32, Ops); | |||
24629 | return DAG.getNode(ISD::TRUNCATE, DL, Op.getValueType(), Cmov); | |||
24630 | } | |||
24631 | ||||
24632 | // X86ISD::CMOV means set the result (which is operand 1) to the RHS if | |||
24633 | // condition is true. | |||
24634 | SDValue Ops[] = { Op2, Op1, CC, Cond }; | |||
24635 | return DAG.getNode(X86ISD::CMOV, DL, Op.getValueType(), Ops); | |||
24636 | } | |||
24637 | ||||
24638 | static SDValue LowerSIGN_EXTEND_Mask(SDValue Op, | |||
24639 | const X86Subtarget &Subtarget, | |||
24640 | SelectionDAG &DAG) { | |||
24641 | MVT VT = Op->getSimpleValueType(0); | |||
24642 | SDValue In = Op->getOperand(0); | |||
24643 | MVT InVT = In.getSimpleValueType(); | |||
24644 | assert(InVT.getVectorElementType() == MVT::i1 && "Unexpected input type!")(static_cast <bool> (InVT.getVectorElementType() == MVT ::i1 && "Unexpected input type!") ? void (0) : __assert_fail ("InVT.getVectorElementType() == MVT::i1 && \"Unexpected input type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24644, __extension__ __PRETTY_FUNCTION__)); | |||
24645 | MVT VTElt = VT.getVectorElementType(); | |||
24646 | SDLoc dl(Op); | |||
24647 | ||||
24648 | unsigned NumElts = VT.getVectorNumElements(); | |||
24649 | ||||
24650 | // Extend VT if the scalar type is i8/i16 and BWI is not supported. | |||
24651 | MVT ExtVT = VT; | |||
24652 | if (!Subtarget.hasBWI() && VTElt.getSizeInBits() <= 16) { | |||
24653 | // If v16i32 is to be avoided, we'll need to split and concatenate. | |||
24654 | if (NumElts == 16 && !Subtarget.canExtendTo512DQ()) | |||
24655 | return SplitAndExtendv16i1(Op.getOpcode(), VT, In, dl, DAG); | |||
24656 | ||||
24657 | ExtVT = MVT::getVectorVT(MVT::i32, NumElts); | |||
24658 | } | |||
24659 | ||||
24660 | // Widen to 512-bits if VLX is not supported. | |||
24661 | MVT WideVT = ExtVT; | |||
24662 | if (!ExtVT.is512BitVector() && !Subtarget.hasVLX()) { | |||
24663 | NumElts *= 512 / ExtVT.getSizeInBits(); | |||
24664 | InVT = MVT::getVectorVT(MVT::i1, NumElts); | |||
24665 | In = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, InVT, DAG.getUNDEF(InVT), | |||
24666 | In, DAG.getIntPtrConstant(0, dl)); | |||
24667 | WideVT = MVT::getVectorVT(ExtVT.getVectorElementType(), NumElts); | |||
24668 | } | |||
24669 | ||||
24670 | SDValue V; | |||
24671 | MVT WideEltVT = WideVT.getVectorElementType(); | |||
24672 | if ((Subtarget.hasDQI() && WideEltVT.getSizeInBits() >= 32) || | |||
24673 | (Subtarget.hasBWI() && WideEltVT.getSizeInBits() <= 16)) { | |||
24674 | V = DAG.getNode(Op.getOpcode(), dl, WideVT, In); | |||
24675 | } else { | |||
24676 | SDValue NegOne = DAG.getConstant(-1, dl, WideVT); | |||
24677 | SDValue Zero = DAG.getConstant(0, dl, WideVT); | |||
24678 | V = DAG.getSelect(dl, WideVT, In, NegOne, Zero); | |||
24679 | } | |||
24680 | ||||
24681 | // Truncate if we had to extend i16/i8 above. | |||
24682 | if (VT != ExtVT) { | |||
24683 | WideVT = MVT::getVectorVT(VTElt, NumElts); | |||
24684 | V = DAG.getNode(ISD::TRUNCATE, dl, WideVT, V); | |||
24685 | } | |||
24686 | ||||
24687 | // Extract back to 128/256-bit if we widened. | |||
24688 | if (WideVT != VT) | |||
24689 | V = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, V, | |||
24690 | DAG.getIntPtrConstant(0, dl)); | |||
24691 | ||||
24692 | return V; | |||
24693 | } | |||
24694 | ||||
24695 | static SDValue LowerANY_EXTEND(SDValue Op, const X86Subtarget &Subtarget, | |||
24696 | SelectionDAG &DAG) { | |||
24697 | SDValue In = Op->getOperand(0); | |||
24698 | MVT InVT = In.getSimpleValueType(); | |||
24699 | ||||
24700 | if (InVT.getVectorElementType() == MVT::i1) | |||
24701 | return LowerSIGN_EXTEND_Mask(Op, Subtarget, DAG); | |||
24702 | ||||
24703 | assert(Subtarget.hasAVX() && "Expected AVX support")(static_cast <bool> (Subtarget.hasAVX() && "Expected AVX support" ) ? void (0) : __assert_fail ("Subtarget.hasAVX() && \"Expected AVX support\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24703, __extension__ __PRETTY_FUNCTION__)); | |||
24704 | return LowerAVXExtend(Op, DAG, Subtarget); | |||
24705 | } | |||
24706 | ||||
24707 | // Lowering for SIGN_EXTEND_VECTOR_INREG and ZERO_EXTEND_VECTOR_INREG. | |||
24708 | // For sign extend this needs to handle all vector sizes and SSE4.1 and | |||
24709 | // non-SSE4.1 targets. For zero extend this should only handle inputs of | |||
24710 | // MVT::v64i8 when BWI is not supported, but AVX512 is. | |||
24711 | static SDValue LowerEXTEND_VECTOR_INREG(SDValue Op, | |||
24712 | const X86Subtarget &Subtarget, | |||
24713 | SelectionDAG &DAG) { | |||
24714 | SDValue In = Op->getOperand(0); | |||
24715 | MVT VT = Op->getSimpleValueType(0); | |||
24716 | MVT InVT = In.getSimpleValueType(); | |||
24717 | ||||
24718 | MVT SVT = VT.getVectorElementType(); | |||
24719 | MVT InSVT = InVT.getVectorElementType(); | |||
24720 | assert(SVT.getFixedSizeInBits() > InSVT.getFixedSizeInBits())(static_cast <bool> (SVT.getFixedSizeInBits() > InSVT .getFixedSizeInBits()) ? void (0) : __assert_fail ("SVT.getFixedSizeInBits() > InSVT.getFixedSizeInBits()" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24720, __extension__ __PRETTY_FUNCTION__)); | |||
24721 | ||||
24722 | if (SVT != MVT::i64 && SVT != MVT::i32 && SVT != MVT::i16) | |||
24723 | return SDValue(); | |||
24724 | if (InSVT != MVT::i32 && InSVT != MVT::i16 && InSVT != MVT::i8) | |||
24725 | return SDValue(); | |||
24726 | if (!(VT.is128BitVector() && Subtarget.hasSSE2()) && | |||
24727 | !(VT.is256BitVector() && Subtarget.hasAVX()) && | |||
24728 | !(VT.is512BitVector() && Subtarget.hasAVX512())) | |||
24729 | return SDValue(); | |||
24730 | ||||
24731 | SDLoc dl(Op); | |||
24732 | unsigned Opc = Op.getOpcode(); | |||
24733 | unsigned NumElts = VT.getVectorNumElements(); | |||
24734 | ||||
24735 | // For 256-bit vectors, we only need the lower (128-bit) half of the input. | |||
24736 | // For 512-bit vectors, we need 128-bits or 256-bits. | |||
24737 | if (InVT.getSizeInBits() > 128) { | |||
24738 | // Input needs to be at least the same number of elements as output, and | |||
24739 | // at least 128-bits. | |||
24740 | int InSize = InSVT.getSizeInBits() * NumElts; | |||
24741 | In = extractSubVector(In, 0, DAG, dl, std::max(InSize, 128)); | |||
24742 | InVT = In.getSimpleValueType(); | |||
24743 | } | |||
24744 | ||||
24745 | // SSE41 targets can use the pmov[sz]x* instructions directly for 128-bit results, | |||
24746 | // so are legal and shouldn't occur here. AVX2/AVX512 pmovsx* instructions still | |||
24747 | // need to be handled here for 256/512-bit results. | |||
24748 | if (Subtarget.hasInt256()) { | |||
24749 | assert(VT.getSizeInBits() > 128 && "Unexpected 128-bit vector extension")(static_cast <bool> (VT.getSizeInBits() > 128 && "Unexpected 128-bit vector extension") ? void (0) : __assert_fail ("VT.getSizeInBits() > 128 && \"Unexpected 128-bit vector extension\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24749, __extension__ __PRETTY_FUNCTION__)); | |||
24750 | ||||
24751 | if (InVT.getVectorNumElements() != NumElts) | |||
24752 | return DAG.getNode(Op.getOpcode(), dl, VT, In); | |||
24753 | ||||
24754 | // FIXME: Apparently we create inreg operations that could be regular | |||
24755 | // extends. | |||
24756 | unsigned ExtOpc = | |||
24757 | Opc == ISD::SIGN_EXTEND_VECTOR_INREG ? ISD::SIGN_EXTEND | |||
24758 | : ISD::ZERO_EXTEND; | |||
24759 | return DAG.getNode(ExtOpc, dl, VT, In); | |||
24760 | } | |||
24761 | ||||
24762 | // pre-AVX2 256-bit extensions need to be split into 128-bit instructions. | |||
24763 | if (Subtarget.hasAVX()) { | |||
24764 | assert(VT.is256BitVector() && "256-bit vector expected")(static_cast <bool> (VT.is256BitVector() && "256-bit vector expected" ) ? void (0) : __assert_fail ("VT.is256BitVector() && \"256-bit vector expected\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24764, __extension__ __PRETTY_FUNCTION__)); | |||
24765 | MVT HalfVT = VT.getHalfNumVectorElementsVT(); | |||
24766 | int HalfNumElts = HalfVT.getVectorNumElements(); | |||
24767 | ||||
24768 | unsigned NumSrcElts = InVT.getVectorNumElements(); | |||
24769 | SmallVector<int, 16> HiMask(NumSrcElts, SM_SentinelUndef); | |||
24770 | for (int i = 0; i != HalfNumElts; ++i) | |||
24771 | HiMask[i] = HalfNumElts + i; | |||
24772 | ||||
24773 | SDValue Lo = DAG.getNode(Opc, dl, HalfVT, In); | |||
24774 | SDValue Hi = DAG.getVectorShuffle(InVT, dl, In, DAG.getUNDEF(InVT), HiMask); | |||
24775 | Hi = DAG.getNode(Opc, dl, HalfVT, Hi); | |||
24776 | return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Lo, Hi); | |||
24777 | } | |||
24778 | ||||
24779 | // We should only get here for sign extend. | |||
24780 | assert(Opc == ISD::SIGN_EXTEND_VECTOR_INREG && "Unexpected opcode!")(static_cast <bool> (Opc == ISD::SIGN_EXTEND_VECTOR_INREG && "Unexpected opcode!") ? void (0) : __assert_fail ( "Opc == ISD::SIGN_EXTEND_VECTOR_INREG && \"Unexpected opcode!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24780, __extension__ __PRETTY_FUNCTION__)); | |||
24781 | assert(VT.is128BitVector() && InVT.is128BitVector() && "Unexpected VTs")(static_cast <bool> (VT.is128BitVector() && InVT .is128BitVector() && "Unexpected VTs") ? void (0) : __assert_fail ("VT.is128BitVector() && InVT.is128BitVector() && \"Unexpected VTs\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24781, __extension__ __PRETTY_FUNCTION__)); | |||
24782 | ||||
24783 | // pre-SSE41 targets unpack lower lanes and then sign-extend using SRAI. | |||
24784 | SDValue Curr = In; | |||
24785 | SDValue SignExt = Curr; | |||
24786 | ||||
24787 | // As SRAI is only available on i16/i32 types, we expand only up to i32 | |||
24788 | // and handle i64 separately. | |||
24789 | if (InVT != MVT::v4i32) { | |||
24790 | MVT DestVT = VT == MVT::v2i64 ? MVT::v4i32 : VT; | |||
24791 | ||||
24792 | unsigned DestWidth = DestVT.getScalarSizeInBits(); | |||
24793 | unsigned Scale = DestWidth / InSVT.getSizeInBits(); | |||
24794 | ||||
24795 | unsigned InNumElts = InVT.getVectorNumElements(); | |||
24796 | unsigned DestElts = DestVT.getVectorNumElements(); | |||
24797 | ||||
24798 | // Build a shuffle mask that takes each input element and places it in the | |||
24799 | // MSBs of the new element size. | |||
24800 | SmallVector<int, 16> Mask(InNumElts, SM_SentinelUndef); | |||
24801 | for (unsigned i = 0; i != DestElts; ++i) | |||
24802 | Mask[i * Scale + (Scale - 1)] = i; | |||
24803 | ||||
24804 | Curr = DAG.getVectorShuffle(InVT, dl, In, In, Mask); | |||
24805 | Curr = DAG.getBitcast(DestVT, Curr); | |||
24806 | ||||
24807 | unsigned SignExtShift = DestWidth - InSVT.getSizeInBits(); | |||
24808 | SignExt = DAG.getNode(X86ISD::VSRAI, dl, DestVT, Curr, | |||
24809 | DAG.getTargetConstant(SignExtShift, dl, MVT::i8)); | |||
24810 | } | |||
24811 | ||||
24812 | if (VT == MVT::v2i64) { | |||
24813 | assert(Curr.getValueType() == MVT::v4i32 && "Unexpected input VT")(static_cast <bool> (Curr.getValueType() == MVT::v4i32 && "Unexpected input VT") ? void (0) : __assert_fail ("Curr.getValueType() == MVT::v4i32 && \"Unexpected input VT\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24813, __extension__ __PRETTY_FUNCTION__)); | |||
24814 | SDValue Zero = DAG.getConstant(0, dl, MVT::v4i32); | |||
24815 | SDValue Sign = DAG.getSetCC(dl, MVT::v4i32, Zero, Curr, ISD::SETGT); | |||
24816 | SignExt = DAG.getVectorShuffle(MVT::v4i32, dl, SignExt, Sign, {0, 4, 1, 5}); | |||
24817 | SignExt = DAG.getBitcast(VT, SignExt); | |||
24818 | } | |||
24819 | ||||
24820 | return SignExt; | |||
24821 | } | |||
24822 | ||||
24823 | static SDValue LowerSIGN_EXTEND(SDValue Op, const X86Subtarget &Subtarget, | |||
24824 | SelectionDAG &DAG) { | |||
24825 | MVT VT = Op->getSimpleValueType(0); | |||
24826 | SDValue In = Op->getOperand(0); | |||
24827 | MVT InVT = In.getSimpleValueType(); | |||
24828 | SDLoc dl(Op); | |||
24829 | ||||
24830 | if (InVT.getVectorElementType() == MVT::i1) | |||
24831 | return LowerSIGN_EXTEND_Mask(Op, Subtarget, DAG); | |||
24832 | ||||
24833 | assert(VT.isVector() && InVT.isVector() && "Expected vector type")(static_cast <bool> (VT.isVector() && InVT.isVector () && "Expected vector type") ? void (0) : __assert_fail ("VT.isVector() && InVT.isVector() && \"Expected vector type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24833, __extension__ __PRETTY_FUNCTION__)); | |||
24834 | assert(VT.getVectorNumElements() == InVT.getVectorNumElements() &&(static_cast <bool> (VT.getVectorNumElements() == InVT. getVectorNumElements() && "Expected same number of elements" ) ? void (0) : __assert_fail ("VT.getVectorNumElements() == InVT.getVectorNumElements() && \"Expected same number of elements\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24835, __extension__ __PRETTY_FUNCTION__)) | |||
24835 | "Expected same number of elements")(static_cast <bool> (VT.getVectorNumElements() == InVT. getVectorNumElements() && "Expected same number of elements" ) ? void (0) : __assert_fail ("VT.getVectorNumElements() == InVT.getVectorNumElements() && \"Expected same number of elements\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24835, __extension__ __PRETTY_FUNCTION__)); | |||
24836 | assert((VT.getVectorElementType() == MVT::i16 ||(static_cast <bool> ((VT.getVectorElementType() == MVT:: i16 || VT.getVectorElementType() == MVT::i32 || VT.getVectorElementType () == MVT::i64) && "Unexpected element type") ? void ( 0) : __assert_fail ("(VT.getVectorElementType() == MVT::i16 || VT.getVectorElementType() == MVT::i32 || VT.getVectorElementType() == MVT::i64) && \"Unexpected element type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24839, __extension__ __PRETTY_FUNCTION__)) | |||
24837 | VT.getVectorElementType() == MVT::i32 ||(static_cast <bool> ((VT.getVectorElementType() == MVT:: i16 || VT.getVectorElementType() == MVT::i32 || VT.getVectorElementType () == MVT::i64) && "Unexpected element type") ? void ( 0) : __assert_fail ("(VT.getVectorElementType() == MVT::i16 || VT.getVectorElementType() == MVT::i32 || VT.getVectorElementType() == MVT::i64) && \"Unexpected element type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24839, __extension__ __PRETTY_FUNCTION__)) | |||
24838 | VT.getVectorElementType() == MVT::i64) &&(static_cast <bool> ((VT.getVectorElementType() == MVT:: i16 || VT.getVectorElementType() == MVT::i32 || VT.getVectorElementType () == MVT::i64) && "Unexpected element type") ? void ( 0) : __assert_fail ("(VT.getVectorElementType() == MVT::i16 || VT.getVectorElementType() == MVT::i32 || VT.getVectorElementType() == MVT::i64) && \"Unexpected element type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24839, __extension__ __PRETTY_FUNCTION__)) | |||
24839 | "Unexpected element type")(static_cast <bool> ((VT.getVectorElementType() == MVT:: i16 || VT.getVectorElementType() == MVT::i32 || VT.getVectorElementType () == MVT::i64) && "Unexpected element type") ? void ( 0) : __assert_fail ("(VT.getVectorElementType() == MVT::i16 || VT.getVectorElementType() == MVT::i32 || VT.getVectorElementType() == MVT::i64) && \"Unexpected element type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24839, __extension__ __PRETTY_FUNCTION__)); | |||
24840 | assert((InVT.getVectorElementType() == MVT::i8 ||(static_cast <bool> ((InVT.getVectorElementType() == MVT ::i8 || InVT.getVectorElementType() == MVT::i16 || InVT.getVectorElementType () == MVT::i32) && "Unexpected element type") ? void ( 0) : __assert_fail ("(InVT.getVectorElementType() == MVT::i8 || InVT.getVectorElementType() == MVT::i16 || InVT.getVectorElementType() == MVT::i32) && \"Unexpected element type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24843, __extension__ __PRETTY_FUNCTION__)) | |||
24841 | InVT.getVectorElementType() == MVT::i16 ||(static_cast <bool> ((InVT.getVectorElementType() == MVT ::i8 || InVT.getVectorElementType() == MVT::i16 || InVT.getVectorElementType () == MVT::i32) && "Unexpected element type") ? void ( 0) : __assert_fail ("(InVT.getVectorElementType() == MVT::i8 || InVT.getVectorElementType() == MVT::i16 || InVT.getVectorElementType() == MVT::i32) && \"Unexpected element type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24843, __extension__ __PRETTY_FUNCTION__)) | |||
24842 | InVT.getVectorElementType() == MVT::i32) &&(static_cast <bool> ((InVT.getVectorElementType() == MVT ::i8 || InVT.getVectorElementType() == MVT::i16 || InVT.getVectorElementType () == MVT::i32) && "Unexpected element type") ? void ( 0) : __assert_fail ("(InVT.getVectorElementType() == MVT::i8 || InVT.getVectorElementType() == MVT::i16 || InVT.getVectorElementType() == MVT::i32) && \"Unexpected element type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24843, __extension__ __PRETTY_FUNCTION__)) | |||
24843 | "Unexpected element type")(static_cast <bool> ((InVT.getVectorElementType() == MVT ::i8 || InVT.getVectorElementType() == MVT::i16 || InVT.getVectorElementType () == MVT::i32) && "Unexpected element type") ? void ( 0) : __assert_fail ("(InVT.getVectorElementType() == MVT::i8 || InVT.getVectorElementType() == MVT::i16 || InVT.getVectorElementType() == MVT::i32) && \"Unexpected element type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24843, __extension__ __PRETTY_FUNCTION__)); | |||
24844 | ||||
24845 | if (VT == MVT::v32i16 && !Subtarget.hasBWI()) { | |||
24846 | assert(InVT == MVT::v32i8 && "Unexpected VT!")(static_cast <bool> (InVT == MVT::v32i8 && "Unexpected VT!" ) ? void (0) : __assert_fail ("InVT == MVT::v32i8 && \"Unexpected VT!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24846, __extension__ __PRETTY_FUNCTION__)); | |||
24847 | return splitVectorIntUnary(Op, DAG); | |||
24848 | } | |||
24849 | ||||
24850 | if (Subtarget.hasInt256()) | |||
24851 | return Op; | |||
24852 | ||||
24853 | // Optimize vectors in AVX mode | |||
24854 | // Sign extend v8i16 to v8i32 and | |||
24855 | // v4i32 to v4i64 | |||
24856 | // | |||
24857 | // Divide input vector into two parts | |||
24858 | // for v4i32 the high shuffle mask will be {2, 3, -1, -1} | |||
24859 | // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32 | |||
24860 | // concat the vectors to original VT | |||
24861 | MVT HalfVT = VT.getHalfNumVectorElementsVT(); | |||
24862 | SDValue OpLo = DAG.getNode(ISD::SIGN_EXTEND_VECTOR_INREG, dl, HalfVT, In); | |||
24863 | ||||
24864 | unsigned NumElems = InVT.getVectorNumElements(); | |||
24865 | SmallVector<int,8> ShufMask(NumElems, -1); | |||
24866 | for (unsigned i = 0; i != NumElems/2; ++i) | |||
24867 | ShufMask[i] = i + NumElems/2; | |||
24868 | ||||
24869 | SDValue OpHi = DAG.getVectorShuffle(InVT, dl, In, In, ShufMask); | |||
24870 | OpHi = DAG.getNode(ISD::SIGN_EXTEND_VECTOR_INREG, dl, HalfVT, OpHi); | |||
24871 | ||||
24872 | return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi); | |||
24873 | } | |||
24874 | ||||
24875 | /// Change a vector store into a pair of half-size vector stores. | |||
24876 | static SDValue splitVectorStore(StoreSDNode *Store, SelectionDAG &DAG) { | |||
24877 | SDValue StoredVal = Store->getValue(); | |||
24878 | assert((StoredVal.getValueType().is256BitVector() ||(static_cast <bool> ((StoredVal.getValueType().is256BitVector () || StoredVal.getValueType().is512BitVector()) && "Expecting 256/512-bit op" ) ? void (0) : __assert_fail ("(StoredVal.getValueType().is256BitVector() || StoredVal.getValueType().is512BitVector()) && \"Expecting 256/512-bit op\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24880, __extension__ __PRETTY_FUNCTION__)) | |||
24879 | StoredVal.getValueType().is512BitVector()) &&(static_cast <bool> ((StoredVal.getValueType().is256BitVector () || StoredVal.getValueType().is512BitVector()) && "Expecting 256/512-bit op" ) ? void (0) : __assert_fail ("(StoredVal.getValueType().is256BitVector() || StoredVal.getValueType().is512BitVector()) && \"Expecting 256/512-bit op\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24880, __extension__ __PRETTY_FUNCTION__)) | |||
24880 | "Expecting 256/512-bit op")(static_cast <bool> ((StoredVal.getValueType().is256BitVector () || StoredVal.getValueType().is512BitVector()) && "Expecting 256/512-bit op" ) ? void (0) : __assert_fail ("(StoredVal.getValueType().is256BitVector() || StoredVal.getValueType().is512BitVector()) && \"Expecting 256/512-bit op\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24880, __extension__ __PRETTY_FUNCTION__)); | |||
24881 | ||||
24882 | // Splitting volatile memory ops is not allowed unless the operation was not | |||
24883 | // legal to begin with. Assume the input store is legal (this transform is | |||
24884 | // only used for targets with AVX). Note: It is possible that we have an | |||
24885 | // illegal type like v2i128, and so we could allow splitting a volatile store | |||
24886 | // in that case if that is important. | |||
24887 | if (!Store->isSimple()) | |||
24888 | return SDValue(); | |||
24889 | ||||
24890 | SDLoc DL(Store); | |||
24891 | SDValue Value0, Value1; | |||
24892 | std::tie(Value0, Value1) = splitVector(StoredVal, DAG, DL); | |||
24893 | unsigned HalfOffset = Value0.getValueType().getStoreSize(); | |||
24894 | SDValue Ptr0 = Store->getBasePtr(); | |||
24895 | SDValue Ptr1 = | |||
24896 | DAG.getMemBasePlusOffset(Ptr0, TypeSize::Fixed(HalfOffset), DL); | |||
24897 | SDValue Ch0 = | |||
24898 | DAG.getStore(Store->getChain(), DL, Value0, Ptr0, Store->getPointerInfo(), | |||
24899 | Store->getOriginalAlign(), | |||
24900 | Store->getMemOperand()->getFlags()); | |||
24901 | SDValue Ch1 = DAG.getStore(Store->getChain(), DL, Value1, Ptr1, | |||
24902 | Store->getPointerInfo().getWithOffset(HalfOffset), | |||
24903 | Store->getOriginalAlign(), | |||
24904 | Store->getMemOperand()->getFlags()); | |||
24905 | return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Ch0, Ch1); | |||
24906 | } | |||
24907 | ||||
24908 | /// Scalarize a vector store, bitcasting to TargetVT to determine the scalar | |||
24909 | /// type. | |||
24910 | static SDValue scalarizeVectorStore(StoreSDNode *Store, MVT StoreVT, | |||
24911 | SelectionDAG &DAG) { | |||
24912 | SDValue StoredVal = Store->getValue(); | |||
24913 | assert(StoreVT.is128BitVector() &&(static_cast <bool> (StoreVT.is128BitVector() && StoredVal.getValueType().is128BitVector() && "Expecting 128-bit op" ) ? void (0) : __assert_fail ("StoreVT.is128BitVector() && StoredVal.getValueType().is128BitVector() && \"Expecting 128-bit op\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24914, __extension__ __PRETTY_FUNCTION__)) | |||
24914 | StoredVal.getValueType().is128BitVector() && "Expecting 128-bit op")(static_cast <bool> (StoreVT.is128BitVector() && StoredVal.getValueType().is128BitVector() && "Expecting 128-bit op" ) ? void (0) : __assert_fail ("StoreVT.is128BitVector() && StoredVal.getValueType().is128BitVector() && \"Expecting 128-bit op\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24914, __extension__ __PRETTY_FUNCTION__)); | |||
24915 | StoredVal = DAG.getBitcast(StoreVT, StoredVal); | |||
24916 | ||||
24917 | // Splitting volatile memory ops is not allowed unless the operation was not | |||
24918 | // legal to begin with. We are assuming the input op is legal (this transform | |||
24919 | // is only used for targets with AVX). | |||
24920 | if (!Store->isSimple()) | |||
24921 | return SDValue(); | |||
24922 | ||||
24923 | MVT StoreSVT = StoreVT.getScalarType(); | |||
24924 | unsigned NumElems = StoreVT.getVectorNumElements(); | |||
24925 | unsigned ScalarSize = StoreSVT.getStoreSize(); | |||
24926 | ||||
24927 | SDLoc DL(Store); | |||
24928 | SmallVector<SDValue, 4> Stores; | |||
24929 | for (unsigned i = 0; i != NumElems; ++i) { | |||
24930 | unsigned Offset = i * ScalarSize; | |||
24931 | SDValue Ptr = DAG.getMemBasePlusOffset(Store->getBasePtr(), | |||
24932 | TypeSize::Fixed(Offset), DL); | |||
24933 | SDValue Scl = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, StoreSVT, StoredVal, | |||
24934 | DAG.getIntPtrConstant(i, DL)); | |||
24935 | SDValue Ch = DAG.getStore(Store->getChain(), DL, Scl, Ptr, | |||
24936 | Store->getPointerInfo().getWithOffset(Offset), | |||
24937 | Store->getOriginalAlign(), | |||
24938 | Store->getMemOperand()->getFlags()); | |||
24939 | Stores.push_back(Ch); | |||
24940 | } | |||
24941 | return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Stores); | |||
24942 | } | |||
24943 | ||||
24944 | static SDValue LowerStore(SDValue Op, const X86Subtarget &Subtarget, | |||
24945 | SelectionDAG &DAG) { | |||
24946 | StoreSDNode *St = cast<StoreSDNode>(Op.getNode()); | |||
24947 | SDLoc dl(St); | |||
24948 | SDValue StoredVal = St->getValue(); | |||
24949 | ||||
24950 | // Without AVX512DQ, we need to use a scalar type for v2i1/v4i1/v8i1 stores. | |||
24951 | if (StoredVal.getValueType().isVector() && | |||
24952 | StoredVal.getValueType().getVectorElementType() == MVT::i1) { | |||
24953 | unsigned NumElts = StoredVal.getValueType().getVectorNumElements(); | |||
24954 | assert(NumElts <= 8 && "Unexpected VT")(static_cast <bool> (NumElts <= 8 && "Unexpected VT" ) ? void (0) : __assert_fail ("NumElts <= 8 && \"Unexpected VT\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24954, __extension__ __PRETTY_FUNCTION__)); | |||
24955 | assert(!St->isTruncatingStore() && "Expected non-truncating store")(static_cast <bool> (!St->isTruncatingStore() && "Expected non-truncating store") ? void (0) : __assert_fail ( "!St->isTruncatingStore() && \"Expected non-truncating store\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24955, __extension__ __PRETTY_FUNCTION__)); | |||
24956 | assert(Subtarget.hasAVX512() && !Subtarget.hasDQI() &&(static_cast <bool> (Subtarget.hasAVX512() && ! Subtarget.hasDQI() && "Expected AVX512F without AVX512DQI" ) ? void (0) : __assert_fail ("Subtarget.hasAVX512() && !Subtarget.hasDQI() && \"Expected AVX512F without AVX512DQI\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24957, __extension__ __PRETTY_FUNCTION__)) | |||
24957 | "Expected AVX512F without AVX512DQI")(static_cast <bool> (Subtarget.hasAVX512() && ! Subtarget.hasDQI() && "Expected AVX512F without AVX512DQI" ) ? void (0) : __assert_fail ("Subtarget.hasAVX512() && !Subtarget.hasDQI() && \"Expected AVX512F without AVX512DQI\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24957, __extension__ __PRETTY_FUNCTION__)); | |||
24958 | ||||
24959 | // We must pad with zeros to ensure we store zeroes to any unused bits. | |||
24960 | StoredVal = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, MVT::v16i1, | |||
24961 | DAG.getUNDEF(MVT::v16i1), StoredVal, | |||
24962 | DAG.getIntPtrConstant(0, dl)); | |||
24963 | StoredVal = DAG.getBitcast(MVT::i16, StoredVal); | |||
24964 | StoredVal = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, StoredVal); | |||
24965 | // Make sure we store zeros in the extra bits. | |||
24966 | if (NumElts < 8) | |||
24967 | StoredVal = DAG.getZeroExtendInReg( | |||
24968 | StoredVal, dl, EVT::getIntegerVT(*DAG.getContext(), NumElts)); | |||
24969 | ||||
24970 | return DAG.getStore(St->getChain(), dl, StoredVal, St->getBasePtr(), | |||
24971 | St->getPointerInfo(), St->getOriginalAlign(), | |||
24972 | St->getMemOperand()->getFlags()); | |||
24973 | } | |||
24974 | ||||
24975 | if (St->isTruncatingStore()) | |||
24976 | return SDValue(); | |||
24977 | ||||
24978 | // If this is a 256-bit store of concatenated ops, we are better off splitting | |||
24979 | // that store into two 128-bit stores. This avoids spurious use of 256-bit ops | |||
24980 | // and each half can execute independently. Some cores would split the op into | |||
24981 | // halves anyway, so the concat (vinsertf128) is purely an extra op. | |||
24982 | MVT StoreVT = StoredVal.getSimpleValueType(); | |||
24983 | if (StoreVT.is256BitVector() || | |||
24984 | ((StoreVT == MVT::v32i16 || StoreVT == MVT::v64i8) && | |||
24985 | !Subtarget.hasBWI())) { | |||
24986 | SmallVector<SDValue, 4> CatOps; | |||
24987 | if (StoredVal.hasOneUse() && collectConcatOps(StoredVal.getNode(), CatOps)) | |||
24988 | return splitVectorStore(St, DAG); | |||
24989 | return SDValue(); | |||
24990 | } | |||
24991 | ||||
24992 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
24993 | assert(StoreVT.isVector() && StoreVT.getSizeInBits() == 64 &&(static_cast <bool> (StoreVT.isVector() && StoreVT .getSizeInBits() == 64 && "Unexpected VT") ? void (0) : __assert_fail ("StoreVT.isVector() && StoreVT.getSizeInBits() == 64 && \"Unexpected VT\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24994, __extension__ __PRETTY_FUNCTION__)) | |||
24994 | "Unexpected VT")(static_cast <bool> (StoreVT.isVector() && StoreVT .getSizeInBits() == 64 && "Unexpected VT") ? void (0) : __assert_fail ("StoreVT.isVector() && StoreVT.getSizeInBits() == 64 && \"Unexpected VT\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24994, __extension__ __PRETTY_FUNCTION__)); | |||
24995 | assert(TLI.getTypeAction(*DAG.getContext(), StoreVT) ==(static_cast <bool> (TLI.getTypeAction(*DAG.getContext( ), StoreVT) == TargetLowering::TypeWidenVector && "Unexpected type action!" ) ? void (0) : __assert_fail ("TLI.getTypeAction(*DAG.getContext(), StoreVT) == TargetLowering::TypeWidenVector && \"Unexpected type action!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24996, __extension__ __PRETTY_FUNCTION__)) | |||
24996 | TargetLowering::TypeWidenVector && "Unexpected type action!")(static_cast <bool> (TLI.getTypeAction(*DAG.getContext( ), StoreVT) == TargetLowering::TypeWidenVector && "Unexpected type action!" ) ? void (0) : __assert_fail ("TLI.getTypeAction(*DAG.getContext(), StoreVT) == TargetLowering::TypeWidenVector && \"Unexpected type action!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 24996, __extension__ __PRETTY_FUNCTION__)); | |||
24997 | ||||
24998 | EVT WideVT = TLI.getTypeToTransformTo(*DAG.getContext(), StoreVT); | |||
24999 | StoredVal = DAG.getNode(ISD::CONCAT_VECTORS, dl, WideVT, StoredVal, | |||
25000 | DAG.getUNDEF(StoreVT)); | |||
25001 | ||||
25002 | if (Subtarget.hasSSE2()) { | |||
25003 | // Widen the vector, cast to a v2x64 type, extract the single 64-bit element | |||
25004 | // and store it. | |||
25005 | MVT StVT = Subtarget.is64Bit() && StoreVT.isInteger() ? MVT::i64 : MVT::f64; | |||
25006 | MVT CastVT = MVT::getVectorVT(StVT, 2); | |||
25007 | StoredVal = DAG.getBitcast(CastVT, StoredVal); | |||
25008 | StoredVal = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, StVT, StoredVal, | |||
25009 | DAG.getIntPtrConstant(0, dl)); | |||
25010 | ||||
25011 | return DAG.getStore(St->getChain(), dl, StoredVal, St->getBasePtr(), | |||
25012 | St->getPointerInfo(), St->getOriginalAlign(), | |||
25013 | St->getMemOperand()->getFlags()); | |||
25014 | } | |||
25015 | assert(Subtarget.hasSSE1() && "Expected SSE")(static_cast <bool> (Subtarget.hasSSE1() && "Expected SSE" ) ? void (0) : __assert_fail ("Subtarget.hasSSE1() && \"Expected SSE\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25015, __extension__ __PRETTY_FUNCTION__)); | |||
25016 | SDVTList Tys = DAG.getVTList(MVT::Other); | |||
25017 | SDValue Ops[] = {St->getChain(), StoredVal, St->getBasePtr()}; | |||
25018 | return DAG.getMemIntrinsicNode(X86ISD::VEXTRACT_STORE, dl, Tys, Ops, MVT::i64, | |||
25019 | St->getMemOperand()); | |||
25020 | } | |||
25021 | ||||
25022 | // Lower vector extended loads using a shuffle. If SSSE3 is not available we | |||
25023 | // may emit an illegal shuffle but the expansion is still better than scalar | |||
25024 | // code. We generate sext/sext_invec for SEXTLOADs if it's available, otherwise | |||
25025 | // we'll emit a shuffle and a arithmetic shift. | |||
25026 | // FIXME: Is the expansion actually better than scalar code? It doesn't seem so. | |||
25027 | // TODO: It is possible to support ZExt by zeroing the undef values during | |||
25028 | // the shuffle phase or after the shuffle. | |||
25029 | static SDValue LowerLoad(SDValue Op, const X86Subtarget &Subtarget, | |||
25030 | SelectionDAG &DAG) { | |||
25031 | MVT RegVT = Op.getSimpleValueType(); | |||
25032 | assert(RegVT.isVector() && "We only custom lower vector loads.")(static_cast <bool> (RegVT.isVector() && "We only custom lower vector loads." ) ? void (0) : __assert_fail ("RegVT.isVector() && \"We only custom lower vector loads.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25032, __extension__ __PRETTY_FUNCTION__)); | |||
25033 | assert(RegVT.isInteger() &&(static_cast <bool> (RegVT.isInteger() && "We only custom lower integer vector loads." ) ? void (0) : __assert_fail ("RegVT.isInteger() && \"We only custom lower integer vector loads.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25034, __extension__ __PRETTY_FUNCTION__)) | |||
25034 | "We only custom lower integer vector loads.")(static_cast <bool> (RegVT.isInteger() && "We only custom lower integer vector loads." ) ? void (0) : __assert_fail ("RegVT.isInteger() && \"We only custom lower integer vector loads.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25034, __extension__ __PRETTY_FUNCTION__)); | |||
25035 | ||||
25036 | LoadSDNode *Ld = cast<LoadSDNode>(Op.getNode()); | |||
25037 | SDLoc dl(Ld); | |||
25038 | ||||
25039 | // Without AVX512DQ, we need to use a scalar type for v2i1/v4i1/v8i1 loads. | |||
25040 | if (RegVT.getVectorElementType() == MVT::i1) { | |||
25041 | assert(EVT(RegVT) == Ld->getMemoryVT() && "Expected non-extending load")(static_cast <bool> (EVT(RegVT) == Ld->getMemoryVT() && "Expected non-extending load") ? void (0) : __assert_fail ("EVT(RegVT) == Ld->getMemoryVT() && \"Expected non-extending load\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25041, __extension__ __PRETTY_FUNCTION__)); | |||
25042 | assert(RegVT.getVectorNumElements() <= 8 && "Unexpected VT")(static_cast <bool> (RegVT.getVectorNumElements() <= 8 && "Unexpected VT") ? void (0) : __assert_fail ("RegVT.getVectorNumElements() <= 8 && \"Unexpected VT\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25042, __extension__ __PRETTY_FUNCTION__)); | |||
25043 | assert(Subtarget.hasAVX512() && !Subtarget.hasDQI() &&(static_cast <bool> (Subtarget.hasAVX512() && ! Subtarget.hasDQI() && "Expected AVX512F without AVX512DQI" ) ? void (0) : __assert_fail ("Subtarget.hasAVX512() && !Subtarget.hasDQI() && \"Expected AVX512F without AVX512DQI\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25044, __extension__ __PRETTY_FUNCTION__)) | |||
25044 | "Expected AVX512F without AVX512DQI")(static_cast <bool> (Subtarget.hasAVX512() && ! Subtarget.hasDQI() && "Expected AVX512F without AVX512DQI" ) ? void (0) : __assert_fail ("Subtarget.hasAVX512() && !Subtarget.hasDQI() && \"Expected AVX512F without AVX512DQI\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25044, __extension__ __PRETTY_FUNCTION__)); | |||
25045 | ||||
25046 | SDValue NewLd = DAG.getLoad(MVT::i8, dl, Ld->getChain(), Ld->getBasePtr(), | |||
25047 | Ld->getPointerInfo(), Ld->getOriginalAlign(), | |||
25048 | Ld->getMemOperand()->getFlags()); | |||
25049 | ||||
25050 | // Replace chain users with the new chain. | |||
25051 | assert(NewLd->getNumValues() == 2 && "Loads must carry a chain!")(static_cast <bool> (NewLd->getNumValues() == 2 && "Loads must carry a chain!") ? void (0) : __assert_fail ("NewLd->getNumValues() == 2 && \"Loads must carry a chain!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25051, __extension__ __PRETTY_FUNCTION__)); | |||
25052 | ||||
25053 | SDValue Val = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i16, NewLd); | |||
25054 | Val = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, RegVT, | |||
25055 | DAG.getBitcast(MVT::v16i1, Val), | |||
25056 | DAG.getIntPtrConstant(0, dl)); | |||
25057 | return DAG.getMergeValues({Val, NewLd.getValue(1)}, dl); | |||
25058 | } | |||
25059 | ||||
25060 | return SDValue(); | |||
25061 | } | |||
25062 | ||||
25063 | /// Return true if node is an ISD::AND or ISD::OR of two X86ISD::SETCC nodes | |||
25064 | /// each of which has no other use apart from the AND / OR. | |||
25065 | static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) { | |||
25066 | Opc = Op.getOpcode(); | |||
25067 | if (Opc != ISD::OR && Opc != ISD::AND) | |||
25068 | return false; | |||
25069 | return (Op.getOperand(0).getOpcode() == X86ISD::SETCC && | |||
25070 | Op.getOperand(0).hasOneUse() && | |||
25071 | Op.getOperand(1).getOpcode() == X86ISD::SETCC && | |||
25072 | Op.getOperand(1).hasOneUse()); | |||
25073 | } | |||
25074 | ||||
25075 | SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const { | |||
25076 | SDValue Chain = Op.getOperand(0); | |||
25077 | SDValue Cond = Op.getOperand(1); | |||
25078 | SDValue Dest = Op.getOperand(2); | |||
25079 | SDLoc dl(Op); | |||
25080 | ||||
25081 | if (Cond.getOpcode() == ISD::SETCC && | |||
25082 | Cond.getOperand(0).getValueType() != MVT::f128) { | |||
25083 | SDValue LHS = Cond.getOperand(0); | |||
25084 | SDValue RHS = Cond.getOperand(1); | |||
25085 | ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get(); | |||
25086 | ||||
25087 | // Special case for | |||
25088 | // setcc([su]{add,sub,mul}o == 0) | |||
25089 | // setcc([su]{add,sub,mul}o != 1) | |||
25090 | if (ISD::isOverflowIntrOpRes(LHS) && | |||
25091 | (CC == ISD::SETEQ || CC == ISD::SETNE) && | |||
25092 | (isNullConstant(RHS) || isOneConstant(RHS))) { | |||
25093 | SDValue Value, Overflow; | |||
25094 | X86::CondCode X86Cond; | |||
25095 | std::tie(Value, Overflow) = getX86XALUOOp(X86Cond, LHS.getValue(0), DAG); | |||
25096 | ||||
25097 | if ((CC == ISD::SETEQ) == isNullConstant(RHS)) | |||
25098 | X86Cond = X86::GetOppositeBranchCondition(X86Cond); | |||
25099 | ||||
25100 | SDValue CCVal = DAG.getTargetConstant(X86Cond, dl, MVT::i8); | |||
25101 | return DAG.getNode(X86ISD::BRCOND, dl, MVT::Other, Chain, Dest, CCVal, | |||
25102 | Overflow); | |||
25103 | } | |||
25104 | ||||
25105 | if (LHS.getSimpleValueType().isInteger()) { | |||
25106 | SDValue CCVal; | |||
25107 | SDValue EFLAGS = emitFlagsForSetcc(LHS, RHS, CC, SDLoc(Cond), DAG, CCVal); | |||
25108 | return DAG.getNode(X86ISD::BRCOND, dl, MVT::Other, Chain, Dest, CCVal, | |||
25109 | EFLAGS); | |||
25110 | } | |||
25111 | ||||
25112 | if (CC == ISD::SETOEQ) { | |||
25113 | // For FCMP_OEQ, we can emit | |||
25114 | // two branches instead of an explicit AND instruction with a | |||
25115 | // separate test. However, we only do this if this block doesn't | |||
25116 | // have a fall-through edge, because this requires an explicit | |||
25117 | // jmp when the condition is false. | |||
25118 | if (Op.getNode()->hasOneUse()) { | |||
25119 | SDNode *User = *Op.getNode()->use_begin(); | |||
25120 | // Look for an unconditional branch following this conditional branch. | |||
25121 | // We need this because we need to reverse the successors in order | |||
25122 | // to implement FCMP_OEQ. | |||
25123 | if (User->getOpcode() == ISD::BR) { | |||
25124 | SDValue FalseBB = User->getOperand(1); | |||
25125 | SDNode *NewBR = | |||
25126 | DAG.UpdateNodeOperands(User, User->getOperand(0), Dest); | |||
25127 | assert(NewBR == User)(static_cast <bool> (NewBR == User) ? void (0) : __assert_fail ("NewBR == User", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25127, __extension__ __PRETTY_FUNCTION__)); | |||
25128 | (void)NewBR; | |||
25129 | Dest = FalseBB; | |||
25130 | ||||
25131 | SDValue Cmp = | |||
25132 | DAG.getNode(X86ISD::FCMP, SDLoc(Cond), MVT::i32, LHS, RHS); | |||
25133 | SDValue CCVal = DAG.getTargetConstant(X86::COND_NE, dl, MVT::i8); | |||
25134 | Chain = DAG.getNode(X86ISD::BRCOND, dl, MVT::Other, Chain, Dest, | |||
25135 | CCVal, Cmp); | |||
25136 | CCVal = DAG.getTargetConstant(X86::COND_P, dl, MVT::i8); | |||
25137 | return DAG.getNode(X86ISD::BRCOND, dl, MVT::Other, Chain, Dest, CCVal, | |||
25138 | Cmp); | |||
25139 | } | |||
25140 | } | |||
25141 | } else if (CC == ISD::SETUNE) { | |||
25142 | // For FCMP_UNE, we can emit | |||
25143 | // two branches instead of an explicit OR instruction with a | |||
25144 | // separate test. | |||
25145 | SDValue Cmp = DAG.getNode(X86ISD::FCMP, SDLoc(Cond), MVT::i32, LHS, RHS); | |||
25146 | SDValue CCVal = DAG.getTargetConstant(X86::COND_NE, dl, MVT::i8); | |||
25147 | Chain = | |||
25148 | DAG.getNode(X86ISD::BRCOND, dl, MVT::Other, Chain, Dest, CCVal, Cmp); | |||
25149 | CCVal = DAG.getTargetConstant(X86::COND_P, dl, MVT::i8); | |||
25150 | return DAG.getNode(X86ISD::BRCOND, dl, MVT::Other, Chain, Dest, CCVal, | |||
25151 | Cmp); | |||
25152 | } else { | |||
25153 | X86::CondCode X86Cond = | |||
25154 | TranslateX86CC(CC, dl, /*IsFP*/ true, LHS, RHS, DAG); | |||
25155 | SDValue Cmp = DAG.getNode(X86ISD::FCMP, SDLoc(Cond), MVT::i32, LHS, RHS); | |||
25156 | SDValue CCVal = DAG.getTargetConstant(X86Cond, dl, MVT::i8); | |||
25157 | return DAG.getNode(X86ISD::BRCOND, dl, MVT::Other, Chain, Dest, CCVal, | |||
25158 | Cmp); | |||
25159 | } | |||
25160 | } | |||
25161 | ||||
25162 | if (ISD::isOverflowIntrOpRes(Cond)) { | |||
25163 | SDValue Value, Overflow; | |||
25164 | X86::CondCode X86Cond; | |||
25165 | std::tie(Value, Overflow) = getX86XALUOOp(X86Cond, Cond.getValue(0), DAG); | |||
25166 | ||||
25167 | SDValue CCVal = DAG.getTargetConstant(X86Cond, dl, MVT::i8); | |||
25168 | return DAG.getNode(X86ISD::BRCOND, dl, MVT::Other, Chain, Dest, CCVal, | |||
25169 | Overflow); | |||
25170 | } | |||
25171 | ||||
25172 | // Look past the truncate if the high bits are known zero. | |||
25173 | if (isTruncWithZeroHighBitsInput(Cond, DAG)) | |||
25174 | Cond = Cond.getOperand(0); | |||
25175 | ||||
25176 | EVT CondVT = Cond.getValueType(); | |||
25177 | ||||
25178 | // Add an AND with 1 if we don't already have one. | |||
25179 | if (!(Cond.getOpcode() == ISD::AND && isOneConstant(Cond.getOperand(1)))) | |||
25180 | Cond = | |||
25181 | DAG.getNode(ISD::AND, dl, CondVT, Cond, DAG.getConstant(1, dl, CondVT)); | |||
25182 | ||||
25183 | SDValue LHS = Cond; | |||
25184 | SDValue RHS = DAG.getConstant(0, dl, CondVT); | |||
25185 | ||||
25186 | SDValue CCVal; | |||
25187 | SDValue EFLAGS = emitFlagsForSetcc(LHS, RHS, ISD::SETNE, dl, DAG, CCVal); | |||
25188 | return DAG.getNode(X86ISD::BRCOND, dl, MVT::Other, Chain, Dest, CCVal, | |||
25189 | EFLAGS); | |||
25190 | } | |||
25191 | ||||
25192 | // Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets. | |||
25193 | // Calls to _alloca are needed to probe the stack when allocating more than 4k | |||
25194 | // bytes in one go. Touching the stack at 4K increments is necessary to ensure | |||
25195 | // that the guard pages used by the OS virtual memory manager are allocated in | |||
25196 | // correct sequence. | |||
25197 | SDValue | |||
25198 | X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op, | |||
25199 | SelectionDAG &DAG) const { | |||
25200 | MachineFunction &MF = DAG.getMachineFunction(); | |||
25201 | bool SplitStack = MF.shouldSplitStack(); | |||
25202 | bool EmitStackProbeCall = hasStackProbeSymbol(MF); | |||
25203 | bool Lower = (Subtarget.isOSWindows() && !Subtarget.isTargetMachO()) || | |||
25204 | SplitStack || EmitStackProbeCall; | |||
25205 | SDLoc dl(Op); | |||
25206 | ||||
25207 | // Get the inputs. | |||
25208 | SDNode *Node = Op.getNode(); | |||
25209 | SDValue Chain = Op.getOperand(0); | |||
25210 | SDValue Size = Op.getOperand(1); | |||
25211 | MaybeAlign Alignment(Op.getConstantOperandVal(2)); | |||
25212 | EVT VT = Node->getValueType(0); | |||
25213 | ||||
25214 | // Chain the dynamic stack allocation so that it doesn't modify the stack | |||
25215 | // pointer when other instructions are using the stack. | |||
25216 | Chain = DAG.getCALLSEQ_START(Chain, 0, 0, dl); | |||
25217 | ||||
25218 | bool Is64Bit = Subtarget.is64Bit(); | |||
25219 | MVT SPTy = getPointerTy(DAG.getDataLayout()); | |||
25220 | ||||
25221 | SDValue Result; | |||
25222 | if (!Lower) { | |||
25223 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
25224 | Register SPReg = TLI.getStackPointerRegisterToSaveRestore(); | |||
25225 | assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"(static_cast <bool> (SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and" " not tell us which reg is the stack pointer!") ? void (0) : __assert_fail ("SPReg && \"Target cannot require DYNAMIC_STACKALLOC expansion and\" \" not tell us which reg is the stack pointer!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25226, __extension__ __PRETTY_FUNCTION__)) | |||
25226 | " not tell us which reg is the stack pointer!")(static_cast <bool> (SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and" " not tell us which reg is the stack pointer!") ? void (0) : __assert_fail ("SPReg && \"Target cannot require DYNAMIC_STACKALLOC expansion and\" \" not tell us which reg is the stack pointer!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25226, __extension__ __PRETTY_FUNCTION__)); | |||
25227 | ||||
25228 | const TargetFrameLowering &TFI = *Subtarget.getFrameLowering(); | |||
25229 | const Align StackAlign = TFI.getStackAlign(); | |||
25230 | if (hasInlineStackProbe(MF)) { | |||
25231 | MachineRegisterInfo &MRI = MF.getRegInfo(); | |||
25232 | ||||
25233 | const TargetRegisterClass *AddrRegClass = getRegClassFor(SPTy); | |||
25234 | Register Vreg = MRI.createVirtualRegister(AddrRegClass); | |||
25235 | Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size); | |||
25236 | Result = DAG.getNode(X86ISD::PROBED_ALLOCA, dl, SPTy, Chain, | |||
25237 | DAG.getRegister(Vreg, SPTy)); | |||
25238 | } else { | |||
25239 | SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT); | |||
25240 | Chain = SP.getValue(1); | |||
25241 | Result = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value | |||
25242 | } | |||
25243 | if (Alignment && *Alignment > StackAlign) | |||
25244 | Result = | |||
25245 | DAG.getNode(ISD::AND, dl, VT, Result, | |||
25246 | DAG.getConstant(~(Alignment->value() - 1ULL), dl, VT)); | |||
25247 | Chain = DAG.getCopyToReg(Chain, dl, SPReg, Result); // Output chain | |||
25248 | } else if (SplitStack) { | |||
25249 | MachineRegisterInfo &MRI = MF.getRegInfo(); | |||
25250 | ||||
25251 | if (Is64Bit) { | |||
25252 | // The 64 bit implementation of segmented stacks needs to clobber both r10 | |||
25253 | // r11. This makes it impossible to use it along with nested parameters. | |||
25254 | const Function &F = MF.getFunction(); | |||
25255 | for (const auto &A : F.args()) { | |||
25256 | if (A.hasNestAttr()) | |||
25257 | report_fatal_error("Cannot use segmented stacks with functions that " | |||
25258 | "have nested arguments."); | |||
25259 | } | |||
25260 | } | |||
25261 | ||||
25262 | const TargetRegisterClass *AddrRegClass = getRegClassFor(SPTy); | |||
25263 | Register Vreg = MRI.createVirtualRegister(AddrRegClass); | |||
25264 | Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size); | |||
25265 | Result = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain, | |||
25266 | DAG.getRegister(Vreg, SPTy)); | |||
25267 | } else { | |||
25268 | SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue); | |||
25269 | Chain = DAG.getNode(X86ISD::DYN_ALLOCA, dl, NodeTys, Chain, Size); | |||
25270 | MF.getInfo<X86MachineFunctionInfo>()->setHasDynAlloca(true); | |||
25271 | ||||
25272 | const X86RegisterInfo *RegInfo = Subtarget.getRegisterInfo(); | |||
25273 | Register SPReg = RegInfo->getStackRegister(); | |||
25274 | SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, SPTy); | |||
25275 | Chain = SP.getValue(1); | |||
25276 | ||||
25277 | if (Alignment) { | |||
25278 | SP = DAG.getNode(ISD::AND, dl, VT, SP.getValue(0), | |||
25279 | DAG.getConstant(~(Alignment->value() - 1ULL), dl, VT)); | |||
25280 | Chain = DAG.getCopyToReg(Chain, dl, SPReg, SP); | |||
25281 | } | |||
25282 | ||||
25283 | Result = SP; | |||
25284 | } | |||
25285 | ||||
25286 | Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, dl, true), | |||
25287 | DAG.getIntPtrConstant(0, dl, true), SDValue(), dl); | |||
25288 | ||||
25289 | SDValue Ops[2] = {Result, Chain}; | |||
25290 | return DAG.getMergeValues(Ops, dl); | |||
25291 | } | |||
25292 | ||||
25293 | SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const { | |||
25294 | MachineFunction &MF = DAG.getMachineFunction(); | |||
25295 | auto PtrVT = getPointerTy(MF.getDataLayout()); | |||
25296 | X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>(); | |||
25297 | ||||
25298 | const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); | |||
25299 | SDLoc DL(Op); | |||
25300 | ||||
25301 | if (!Subtarget.is64Bit() || | |||
25302 | Subtarget.isCallingConvWin64(MF.getFunction().getCallingConv())) { | |||
25303 | // vastart just stores the address of the VarArgsFrameIndex slot into the | |||
25304 | // memory location argument. | |||
25305 | SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT); | |||
25306 | return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1), | |||
25307 | MachinePointerInfo(SV)); | |||
25308 | } | |||
25309 | ||||
25310 | // __va_list_tag: | |||
25311 | // gp_offset (0 - 6 * 8) | |||
25312 | // fp_offset (48 - 48 + 8 * 16) | |||
25313 | // overflow_arg_area (point to parameters coming in memory). | |||
25314 | // reg_save_area | |||
25315 | SmallVector<SDValue, 8> MemOps; | |||
25316 | SDValue FIN = Op.getOperand(1); | |||
25317 | // Store gp_offset | |||
25318 | SDValue Store = DAG.getStore( | |||
25319 | Op.getOperand(0), DL, | |||
25320 | DAG.getConstant(FuncInfo->getVarArgsGPOffset(), DL, MVT::i32), FIN, | |||
25321 | MachinePointerInfo(SV)); | |||
25322 | MemOps.push_back(Store); | |||
25323 | ||||
25324 | // Store fp_offset | |||
25325 | FIN = DAG.getMemBasePlusOffset(FIN, TypeSize::Fixed(4), DL); | |||
25326 | Store = DAG.getStore( | |||
25327 | Op.getOperand(0), DL, | |||
25328 | DAG.getConstant(FuncInfo->getVarArgsFPOffset(), DL, MVT::i32), FIN, | |||
25329 | MachinePointerInfo(SV, 4)); | |||
25330 | MemOps.push_back(Store); | |||
25331 | ||||
25332 | // Store ptr to overflow_arg_area | |||
25333 | FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN, DAG.getIntPtrConstant(4, DL)); | |||
25334 | SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT); | |||
25335 | Store = | |||
25336 | DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN, MachinePointerInfo(SV, 8)); | |||
25337 | MemOps.push_back(Store); | |||
25338 | ||||
25339 | // Store ptr to reg_save_area. | |||
25340 | FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN, DAG.getIntPtrConstant( | |||
25341 | Subtarget.isTarget64BitLP64() ? 8 : 4, DL)); | |||
25342 | SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), PtrVT); | |||
25343 | Store = DAG.getStore( | |||
25344 | Op.getOperand(0), DL, RSFIN, FIN, | |||
25345 | MachinePointerInfo(SV, Subtarget.isTarget64BitLP64() ? 16 : 12)); | |||
25346 | MemOps.push_back(Store); | |||
25347 | return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps); | |||
25348 | } | |||
25349 | ||||
25350 | SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const { | |||
25351 | assert(Subtarget.is64Bit() &&(static_cast <bool> (Subtarget.is64Bit() && "LowerVAARG only handles 64-bit va_arg!" ) ? void (0) : __assert_fail ("Subtarget.is64Bit() && \"LowerVAARG only handles 64-bit va_arg!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25352, __extension__ __PRETTY_FUNCTION__)) | |||
25352 | "LowerVAARG only handles 64-bit va_arg!")(static_cast <bool> (Subtarget.is64Bit() && "LowerVAARG only handles 64-bit va_arg!" ) ? void (0) : __assert_fail ("Subtarget.is64Bit() && \"LowerVAARG only handles 64-bit va_arg!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25352, __extension__ __PRETTY_FUNCTION__)); | |||
25353 | assert(Op.getNumOperands() == 4)(static_cast <bool> (Op.getNumOperands() == 4) ? void ( 0) : __assert_fail ("Op.getNumOperands() == 4", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25353, __extension__ __PRETTY_FUNCTION__)); | |||
25354 | ||||
25355 | MachineFunction &MF = DAG.getMachineFunction(); | |||
25356 | if (Subtarget.isCallingConvWin64(MF.getFunction().getCallingConv())) | |||
25357 | // The Win64 ABI uses char* instead of a structure. | |||
25358 | return DAG.expandVAArg(Op.getNode()); | |||
25359 | ||||
25360 | SDValue Chain = Op.getOperand(0); | |||
25361 | SDValue SrcPtr = Op.getOperand(1); | |||
25362 | const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue(); | |||
25363 | unsigned Align = Op.getConstantOperandVal(3); | |||
25364 | SDLoc dl(Op); | |||
25365 | ||||
25366 | EVT ArgVT = Op.getNode()->getValueType(0); | |||
25367 | Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); | |||
25368 | uint32_t ArgSize = DAG.getDataLayout().getTypeAllocSize(ArgTy); | |||
25369 | uint8_t ArgMode; | |||
25370 | ||||
25371 | // Decide which area this value should be read from. | |||
25372 | // TODO: Implement the AMD64 ABI in its entirety. This simple | |||
25373 | // selection mechanism works only for the basic types. | |||
25374 | assert(ArgVT != MVT::f80 && "va_arg for f80 not yet implemented")(static_cast <bool> (ArgVT != MVT::f80 && "va_arg for f80 not yet implemented" ) ? void (0) : __assert_fail ("ArgVT != MVT::f80 && \"va_arg for f80 not yet implemented\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25374, __extension__ __PRETTY_FUNCTION__)); | |||
25375 | if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) { | |||
25376 | ArgMode = 2; // Argument passed in XMM register. Use fp_offset. | |||
25377 | } else { | |||
25378 | assert(ArgVT.isInteger() && ArgSize <= 32 /*bytes*/ &&(static_cast <bool> (ArgVT.isInteger() && ArgSize <= 32 && "Unhandled argument type in LowerVAARG") ? void (0) : __assert_fail ("ArgVT.isInteger() && ArgSize <= 32 && \"Unhandled argument type in LowerVAARG\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25379, __extension__ __PRETTY_FUNCTION__)) | |||
25379 | "Unhandled argument type in LowerVAARG")(static_cast <bool> (ArgVT.isInteger() && ArgSize <= 32 && "Unhandled argument type in LowerVAARG") ? void (0) : __assert_fail ("ArgVT.isInteger() && ArgSize <= 32 && \"Unhandled argument type in LowerVAARG\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25379, __extension__ __PRETTY_FUNCTION__)); | |||
25380 | ArgMode = 1; // Argument passed in GPR64 register(s). Use gp_offset. | |||
25381 | } | |||
25382 | ||||
25383 | if (ArgMode == 2) { | |||
25384 | // Sanity Check: Make sure using fp_offset makes sense. | |||
25385 | assert(!Subtarget.useSoftFloat() &&(static_cast <bool> (!Subtarget.useSoftFloat() && !(MF.getFunction().hasFnAttribute(Attribute::NoImplicitFloat )) && Subtarget.hasSSE1()) ? void (0) : __assert_fail ("!Subtarget.useSoftFloat() && !(MF.getFunction().hasFnAttribute(Attribute::NoImplicitFloat)) && Subtarget.hasSSE1()" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25387, __extension__ __PRETTY_FUNCTION__)) | |||
25386 | !(MF.getFunction().hasFnAttribute(Attribute::NoImplicitFloat)) &&(static_cast <bool> (!Subtarget.useSoftFloat() && !(MF.getFunction().hasFnAttribute(Attribute::NoImplicitFloat )) && Subtarget.hasSSE1()) ? void (0) : __assert_fail ("!Subtarget.useSoftFloat() && !(MF.getFunction().hasFnAttribute(Attribute::NoImplicitFloat)) && Subtarget.hasSSE1()" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25387, __extension__ __PRETTY_FUNCTION__)) | |||
25387 | Subtarget.hasSSE1())(static_cast <bool> (!Subtarget.useSoftFloat() && !(MF.getFunction().hasFnAttribute(Attribute::NoImplicitFloat )) && Subtarget.hasSSE1()) ? void (0) : __assert_fail ("!Subtarget.useSoftFloat() && !(MF.getFunction().hasFnAttribute(Attribute::NoImplicitFloat)) && Subtarget.hasSSE1()" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25387, __extension__ __PRETTY_FUNCTION__)); | |||
25388 | } | |||
25389 | ||||
25390 | // Insert VAARG node into the DAG | |||
25391 | // VAARG returns two values: Variable Argument Address, Chain | |||
25392 | SDValue InstOps[] = {Chain, SrcPtr, | |||
25393 | DAG.getTargetConstant(ArgSize, dl, MVT::i32), | |||
25394 | DAG.getTargetConstant(ArgMode, dl, MVT::i8), | |||
25395 | DAG.getTargetConstant(Align, dl, MVT::i32)}; | |||
25396 | SDVTList VTs = DAG.getVTList(getPointerTy(DAG.getDataLayout()), MVT::Other); | |||
25397 | SDValue VAARG = DAG.getMemIntrinsicNode( | |||
25398 | Subtarget.isTarget64BitLP64() ? X86ISD::VAARG_64 : X86ISD::VAARG_X32, dl, | |||
25399 | VTs, InstOps, MVT::i64, MachinePointerInfo(SV), | |||
25400 | /*Alignment=*/None, | |||
25401 | MachineMemOperand::MOLoad | MachineMemOperand::MOStore); | |||
25402 | Chain = VAARG.getValue(1); | |||
25403 | ||||
25404 | // Load the next argument and return it | |||
25405 | return DAG.getLoad(ArgVT, dl, Chain, VAARG, MachinePointerInfo()); | |||
25406 | } | |||
25407 | ||||
25408 | static SDValue LowerVACOPY(SDValue Op, const X86Subtarget &Subtarget, | |||
25409 | SelectionDAG &DAG) { | |||
25410 | // X86-64 va_list is a struct { i32, i32, i8*, i8* }, except on Windows, | |||
25411 | // where a va_list is still an i8*. | |||
25412 | assert(Subtarget.is64Bit() && "This code only handles 64-bit va_copy!")(static_cast <bool> (Subtarget.is64Bit() && "This code only handles 64-bit va_copy!" ) ? void (0) : __assert_fail ("Subtarget.is64Bit() && \"This code only handles 64-bit va_copy!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25412, __extension__ __PRETTY_FUNCTION__)); | |||
25413 | if (Subtarget.isCallingConvWin64( | |||
25414 | DAG.getMachineFunction().getFunction().getCallingConv())) | |||
25415 | // Probably a Win64 va_copy. | |||
25416 | return DAG.expandVACopy(Op.getNode()); | |||
25417 | ||||
25418 | SDValue Chain = Op.getOperand(0); | |||
25419 | SDValue DstPtr = Op.getOperand(1); | |||
25420 | SDValue SrcPtr = Op.getOperand(2); | |||
25421 | const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue(); | |||
25422 | const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue(); | |||
25423 | SDLoc DL(Op); | |||
25424 | ||||
25425 | return DAG.getMemcpy( | |||
25426 | Chain, DL, DstPtr, SrcPtr, | |||
25427 | DAG.getIntPtrConstant(Subtarget.isTarget64BitLP64() ? 24 : 16, DL), | |||
25428 | Align(Subtarget.isTarget64BitLP64() ? 8 : 4), /*isVolatile*/ false, false, | |||
25429 | false, MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV)); | |||
25430 | } | |||
25431 | ||||
25432 | // Helper to get immediate/variable SSE shift opcode from other shift opcodes. | |||
25433 | static unsigned getTargetVShiftUniformOpcode(unsigned Opc, bool IsVariable) { | |||
25434 | switch (Opc) { | |||
25435 | case ISD::SHL: | |||
25436 | case X86ISD::VSHL: | |||
25437 | case X86ISD::VSHLI: | |||
25438 | return IsVariable ? X86ISD::VSHL : X86ISD::VSHLI; | |||
25439 | case ISD::SRL: | |||
25440 | case X86ISD::VSRL: | |||
25441 | case X86ISD::VSRLI: | |||
25442 | return IsVariable ? X86ISD::VSRL : X86ISD::VSRLI; | |||
25443 | case ISD::SRA: | |||
25444 | case X86ISD::VSRA: | |||
25445 | case X86ISD::VSRAI: | |||
25446 | return IsVariable ? X86ISD::VSRA : X86ISD::VSRAI; | |||
25447 | } | |||
25448 | llvm_unreachable("Unknown target vector shift node")::llvm::llvm_unreachable_internal("Unknown target vector shift node" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25448); | |||
25449 | } | |||
25450 | ||||
25451 | /// Handle vector element shifts where the shift amount is a constant. | |||
25452 | /// Takes immediate version of shift as input. | |||
25453 | static SDValue getTargetVShiftByConstNode(unsigned Opc, const SDLoc &dl, MVT VT, | |||
25454 | SDValue SrcOp, uint64_t ShiftAmt, | |||
25455 | SelectionDAG &DAG) { | |||
25456 | MVT ElementType = VT.getVectorElementType(); | |||
25457 | ||||
25458 | // Bitcast the source vector to the output type, this is mainly necessary for | |||
25459 | // vXi8/vXi64 shifts. | |||
25460 | if (VT != SrcOp.getSimpleValueType()) | |||
25461 | SrcOp = DAG.getBitcast(VT, SrcOp); | |||
25462 | ||||
25463 | // Fold this packed shift into its first operand if ShiftAmt is 0. | |||
25464 | if (ShiftAmt == 0) | |||
25465 | return SrcOp; | |||
25466 | ||||
25467 | // Check for ShiftAmt >= element width | |||
25468 | if (ShiftAmt >= ElementType.getSizeInBits()) { | |||
25469 | if (Opc == X86ISD::VSRAI) | |||
25470 | ShiftAmt = ElementType.getSizeInBits() - 1; | |||
25471 | else | |||
25472 | return DAG.getConstant(0, dl, VT); | |||
25473 | } | |||
25474 | ||||
25475 | assert((Opc == X86ISD::VSHLI || Opc == X86ISD::VSRLI || Opc == X86ISD::VSRAI)(static_cast <bool> ((Opc == X86ISD::VSHLI || Opc == X86ISD ::VSRLI || Opc == X86ISD::VSRAI) && "Unknown target vector shift-by-constant node" ) ? void (0) : __assert_fail ("(Opc == X86ISD::VSHLI || Opc == X86ISD::VSRLI || Opc == X86ISD::VSRAI) && \"Unknown target vector shift-by-constant node\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25476, __extension__ __PRETTY_FUNCTION__)) | |||
25476 | && "Unknown target vector shift-by-constant node")(static_cast <bool> ((Opc == X86ISD::VSHLI || Opc == X86ISD ::VSRLI || Opc == X86ISD::VSRAI) && "Unknown target vector shift-by-constant node" ) ? void (0) : __assert_fail ("(Opc == X86ISD::VSHLI || Opc == X86ISD::VSRLI || Opc == X86ISD::VSRAI) && \"Unknown target vector shift-by-constant node\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25476, __extension__ __PRETTY_FUNCTION__)); | |||
25477 | ||||
25478 | // Fold this packed vector shift into a build vector if SrcOp is a | |||
25479 | // vector of Constants or UNDEFs. | |||
25480 | if (ISD::isBuildVectorOfConstantSDNodes(SrcOp.getNode())) { | |||
25481 | SmallVector<SDValue, 8> Elts; | |||
25482 | unsigned NumElts = SrcOp->getNumOperands(); | |||
25483 | ||||
25484 | switch (Opc) { | |||
25485 | default: llvm_unreachable("Unknown opcode!")::llvm::llvm_unreachable_internal("Unknown opcode!", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25485); | |||
25486 | case X86ISD::VSHLI: | |||
25487 | for (unsigned i = 0; i != NumElts; ++i) { | |||
25488 | SDValue CurrentOp = SrcOp->getOperand(i); | |||
25489 | if (CurrentOp->isUndef()) { | |||
25490 | // Must produce 0s in the correct bits. | |||
25491 | Elts.push_back(DAG.getConstant(0, dl, ElementType)); | |||
25492 | continue; | |||
25493 | } | |||
25494 | auto *ND = cast<ConstantSDNode>(CurrentOp); | |||
25495 | const APInt &C = ND->getAPIntValue(); | |||
25496 | Elts.push_back(DAG.getConstant(C.shl(ShiftAmt), dl, ElementType)); | |||
25497 | } | |||
25498 | break; | |||
25499 | case X86ISD::VSRLI: | |||
25500 | for (unsigned i = 0; i != NumElts; ++i) { | |||
25501 | SDValue CurrentOp = SrcOp->getOperand(i); | |||
25502 | if (CurrentOp->isUndef()) { | |||
25503 | // Must produce 0s in the correct bits. | |||
25504 | Elts.push_back(DAG.getConstant(0, dl, ElementType)); | |||
25505 | continue; | |||
25506 | } | |||
25507 | auto *ND = cast<ConstantSDNode>(CurrentOp); | |||
25508 | const APInt &C = ND->getAPIntValue(); | |||
25509 | Elts.push_back(DAG.getConstant(C.lshr(ShiftAmt), dl, ElementType)); | |||
25510 | } | |||
25511 | break; | |||
25512 | case X86ISD::VSRAI: | |||
25513 | for (unsigned i = 0; i != NumElts; ++i) { | |||
25514 | SDValue CurrentOp = SrcOp->getOperand(i); | |||
25515 | if (CurrentOp->isUndef()) { | |||
25516 | // All shifted in bits must be the same so use 0. | |||
25517 | Elts.push_back(DAG.getConstant(0, dl, ElementType)); | |||
25518 | continue; | |||
25519 | } | |||
25520 | auto *ND = cast<ConstantSDNode>(CurrentOp); | |||
25521 | const APInt &C = ND->getAPIntValue(); | |||
25522 | Elts.push_back(DAG.getConstant(C.ashr(ShiftAmt), dl, ElementType)); | |||
25523 | } | |||
25524 | break; | |||
25525 | } | |||
25526 | ||||
25527 | return DAG.getBuildVector(VT, dl, Elts); | |||
25528 | } | |||
25529 | ||||
25530 | return DAG.getNode(Opc, dl, VT, SrcOp, | |||
25531 | DAG.getTargetConstant(ShiftAmt, dl, MVT::i8)); | |||
25532 | } | |||
25533 | ||||
25534 | /// Handle vector element shifts where the shift amount may or may not be a | |||
25535 | /// constant. Takes immediate version of shift as input. | |||
25536 | static SDValue getTargetVShiftNode(unsigned Opc, const SDLoc &dl, MVT VT, | |||
25537 | SDValue SrcOp, SDValue ShAmt, | |||
25538 | const X86Subtarget &Subtarget, | |||
25539 | SelectionDAG &DAG) { | |||
25540 | MVT SVT = ShAmt.getSimpleValueType(); | |||
25541 | assert((SVT == MVT::i32 || SVT == MVT::i64) && "Unexpected value type!")(static_cast <bool> ((SVT == MVT::i32 || SVT == MVT::i64 ) && "Unexpected value type!") ? void (0) : __assert_fail ("(SVT == MVT::i32 || SVT == MVT::i64) && \"Unexpected value type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25541, __extension__ __PRETTY_FUNCTION__)); | |||
25542 | ||||
25543 | // Catch shift-by-constant. | |||
25544 | if (ConstantSDNode *CShAmt = dyn_cast<ConstantSDNode>(ShAmt)) | |||
25545 | return getTargetVShiftByConstNode(Opc, dl, VT, SrcOp, | |||
25546 | CShAmt->getZExtValue(), DAG); | |||
25547 | ||||
25548 | // Change opcode to non-immediate version. | |||
25549 | Opc = getTargetVShiftUniformOpcode(Opc, true); | |||
25550 | ||||
25551 | // Need to build a vector containing shift amount. | |||
25552 | // SSE/AVX packed shifts only use the lower 64-bit of the shift count. | |||
25553 | // +====================+============+=======================================+ | |||
25554 | // | ShAmt is | HasSSE4.1? | Construct ShAmt vector as | | |||
25555 | // +====================+============+=======================================+ | |||
25556 | // | i64 | Yes, No | Use ShAmt as lowest elt | | |||
25557 | // | i32 | Yes | zero-extend in-reg | | |||
25558 | // | (i32 zext(i16/i8)) | Yes | zero-extend in-reg | | |||
25559 | // | (i32 zext(i16/i8)) | No | byte-shift-in-reg | | |||
25560 | // | i16/i32 | No | v4i32 build_vector(ShAmt, 0, ud, ud)) | | |||
25561 | // +====================+============+=======================================+ | |||
25562 | ||||
25563 | if (SVT == MVT::i64) | |||
25564 | ShAmt = DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(ShAmt), MVT::v2i64, ShAmt); | |||
25565 | else if (ShAmt.getOpcode() == ISD::ZERO_EXTEND && | |||
25566 | ShAmt.getOperand(0).getOpcode() == ISD::EXTRACT_VECTOR_ELT && | |||
25567 | (ShAmt.getOperand(0).getSimpleValueType() == MVT::i16 || | |||
25568 | ShAmt.getOperand(0).getSimpleValueType() == MVT::i8)) { | |||
25569 | ShAmt = ShAmt.getOperand(0); | |||
25570 | MVT AmtTy = ShAmt.getSimpleValueType() == MVT::i8 ? MVT::v16i8 : MVT::v8i16; | |||
25571 | ShAmt = DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(ShAmt), AmtTy, ShAmt); | |||
25572 | if (Subtarget.hasSSE41()) | |||
25573 | ShAmt = DAG.getNode(ISD::ZERO_EXTEND_VECTOR_INREG, SDLoc(ShAmt), | |||
25574 | MVT::v2i64, ShAmt); | |||
25575 | else { | |||
25576 | SDValue ByteShift = DAG.getTargetConstant( | |||
25577 | (128 - AmtTy.getScalarSizeInBits()) / 8, SDLoc(ShAmt), MVT::i8); | |||
25578 | ShAmt = DAG.getBitcast(MVT::v16i8, ShAmt); | |||
25579 | ShAmt = DAG.getNode(X86ISD::VSHLDQ, SDLoc(ShAmt), MVT::v16i8, ShAmt, | |||
25580 | ByteShift); | |||
25581 | ShAmt = DAG.getNode(X86ISD::VSRLDQ, SDLoc(ShAmt), MVT::v16i8, ShAmt, | |||
25582 | ByteShift); | |||
25583 | } | |||
25584 | } else if (Subtarget.hasSSE41() && | |||
25585 | ShAmt.getOpcode() == ISD::EXTRACT_VECTOR_ELT) { | |||
25586 | ShAmt = DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(ShAmt), MVT::v4i32, ShAmt); | |||
25587 | ShAmt = DAG.getNode(ISD::ZERO_EXTEND_VECTOR_INREG, SDLoc(ShAmt), | |||
25588 | MVT::v2i64, ShAmt); | |||
25589 | } else { | |||
25590 | SDValue ShOps[4] = {ShAmt, DAG.getConstant(0, dl, SVT), DAG.getUNDEF(SVT), | |||
25591 | DAG.getUNDEF(SVT)}; | |||
25592 | ShAmt = DAG.getBuildVector(MVT::v4i32, dl, ShOps); | |||
25593 | } | |||
25594 | ||||
25595 | // The return type has to be a 128-bit type with the same element | |||
25596 | // type as the input type. | |||
25597 | MVT EltVT = VT.getVectorElementType(); | |||
25598 | MVT ShVT = MVT::getVectorVT(EltVT, 128 / EltVT.getSizeInBits()); | |||
25599 | ||||
25600 | ShAmt = DAG.getBitcast(ShVT, ShAmt); | |||
25601 | return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt); | |||
25602 | } | |||
25603 | ||||
25604 | /// Return Mask with the necessary casting or extending | |||
25605 | /// for \p Mask according to \p MaskVT when lowering masking intrinsics | |||
25606 | static SDValue getMaskNode(SDValue Mask, MVT MaskVT, | |||
25607 | const X86Subtarget &Subtarget, SelectionDAG &DAG, | |||
25608 | const SDLoc &dl) { | |||
25609 | ||||
25610 | if (isAllOnesConstant(Mask)) | |||
25611 | return DAG.getConstant(1, dl, MaskVT); | |||
25612 | if (X86::isZeroNode(Mask)) | |||
25613 | return DAG.getConstant(0, dl, MaskVT); | |||
25614 | ||||
25615 | assert(MaskVT.bitsLE(Mask.getSimpleValueType()) && "Unexpected mask size!")(static_cast <bool> (MaskVT.bitsLE(Mask.getSimpleValueType ()) && "Unexpected mask size!") ? void (0) : __assert_fail ("MaskVT.bitsLE(Mask.getSimpleValueType()) && \"Unexpected mask size!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25615, __extension__ __PRETTY_FUNCTION__)); | |||
25616 | ||||
25617 | if (Mask.getSimpleValueType() == MVT::i64 && Subtarget.is32Bit()) { | |||
25618 | assert(MaskVT == MVT::v64i1 && "Expected v64i1 mask!")(static_cast <bool> (MaskVT == MVT::v64i1 && "Expected v64i1 mask!" ) ? void (0) : __assert_fail ("MaskVT == MVT::v64i1 && \"Expected v64i1 mask!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25618, __extension__ __PRETTY_FUNCTION__)); | |||
25619 | assert(Subtarget.hasBWI() && "Expected AVX512BW target!")(static_cast <bool> (Subtarget.hasBWI() && "Expected AVX512BW target!" ) ? void (0) : __assert_fail ("Subtarget.hasBWI() && \"Expected AVX512BW target!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25619, __extension__ __PRETTY_FUNCTION__)); | |||
25620 | // In case 32bit mode, bitcast i64 is illegal, extend/split it. | |||
25621 | SDValue Lo, Hi; | |||
25622 | Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Mask, | |||
25623 | DAG.getConstant(0, dl, MVT::i32)); | |||
25624 | Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Mask, | |||
25625 | DAG.getConstant(1, dl, MVT::i32)); | |||
25626 | ||||
25627 | Lo = DAG.getBitcast(MVT::v32i1, Lo); | |||
25628 | Hi = DAG.getBitcast(MVT::v32i1, Hi); | |||
25629 | ||||
25630 | return DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v64i1, Lo, Hi); | |||
25631 | } else { | |||
25632 | MVT BitcastVT = MVT::getVectorVT(MVT::i1, | |||
25633 | Mask.getSimpleValueType().getSizeInBits()); | |||
25634 | // In case when MaskVT equals v2i1 or v4i1, low 2 or 4 elements | |||
25635 | // are extracted by EXTRACT_SUBVECTOR. | |||
25636 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MaskVT, | |||
25637 | DAG.getBitcast(BitcastVT, Mask), | |||
25638 | DAG.getIntPtrConstant(0, dl)); | |||
25639 | } | |||
25640 | } | |||
25641 | ||||
25642 | /// Return (and \p Op, \p Mask) for compare instructions or | |||
25643 | /// (vselect \p Mask, \p Op, \p PreservedSrc) for others along with the | |||
25644 | /// necessary casting or extending for \p Mask when lowering masking intrinsics | |||
25645 | static SDValue getVectorMaskingNode(SDValue Op, SDValue Mask, | |||
25646 | SDValue PreservedSrc, | |||
25647 | const X86Subtarget &Subtarget, | |||
25648 | SelectionDAG &DAG) { | |||
25649 | MVT VT = Op.getSimpleValueType(); | |||
25650 | MVT MaskVT = MVT::getVectorVT(MVT::i1, VT.getVectorNumElements()); | |||
25651 | unsigned OpcodeSelect = ISD::VSELECT; | |||
25652 | SDLoc dl(Op); | |||
25653 | ||||
25654 | if (isAllOnesConstant(Mask)) | |||
25655 | return Op; | |||
25656 | ||||
25657 | SDValue VMask = getMaskNode(Mask, MaskVT, Subtarget, DAG, dl); | |||
25658 | ||||
25659 | if (PreservedSrc.isUndef()) | |||
25660 | PreservedSrc = getZeroVector(VT, Subtarget, DAG, dl); | |||
25661 | return DAG.getNode(OpcodeSelect, dl, VT, VMask, Op, PreservedSrc); | |||
25662 | } | |||
25663 | ||||
25664 | /// Creates an SDNode for a predicated scalar operation. | |||
25665 | /// \returns (X86vselect \p Mask, \p Op, \p PreservedSrc). | |||
25666 | /// The mask is coming as MVT::i8 and it should be transformed | |||
25667 | /// to MVT::v1i1 while lowering masking intrinsics. | |||
25668 | /// The main difference between ScalarMaskingNode and VectorMaskingNode is using | |||
25669 | /// "X86select" instead of "vselect". We just can't create the "vselect" node | |||
25670 | /// for a scalar instruction. | |||
25671 | static SDValue getScalarMaskingNode(SDValue Op, SDValue Mask, | |||
25672 | SDValue PreservedSrc, | |||
25673 | const X86Subtarget &Subtarget, | |||
25674 | SelectionDAG &DAG) { | |||
25675 | ||||
25676 | if (auto *MaskConst = dyn_cast<ConstantSDNode>(Mask)) | |||
25677 | if (MaskConst->getZExtValue() & 0x1) | |||
25678 | return Op; | |||
25679 | ||||
25680 | MVT VT = Op.getSimpleValueType(); | |||
25681 | SDLoc dl(Op); | |||
25682 | ||||
25683 | assert(Mask.getValueType() == MVT::i8 && "Unexpect type")(static_cast <bool> (Mask.getValueType() == MVT::i8 && "Unexpect type") ? void (0) : __assert_fail ("Mask.getValueType() == MVT::i8 && \"Unexpect type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25683, __extension__ __PRETTY_FUNCTION__)); | |||
25684 | SDValue IMask = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v1i1, | |||
25685 | DAG.getBitcast(MVT::v8i1, Mask), | |||
25686 | DAG.getIntPtrConstant(0, dl)); | |||
25687 | if (Op.getOpcode() == X86ISD::FSETCCM || | |||
25688 | Op.getOpcode() == X86ISD::FSETCCM_SAE || | |||
25689 | Op.getOpcode() == X86ISD::VFPCLASSS) | |||
25690 | return DAG.getNode(ISD::AND, dl, VT, Op, IMask); | |||
25691 | ||||
25692 | if (PreservedSrc.isUndef()) | |||
25693 | PreservedSrc = getZeroVector(VT, Subtarget, DAG, dl); | |||
25694 | return DAG.getNode(X86ISD::SELECTS, dl, VT, IMask, Op, PreservedSrc); | |||
25695 | } | |||
25696 | ||||
25697 | static int getSEHRegistrationNodeSize(const Function *Fn) { | |||
25698 | if (!Fn->hasPersonalityFn()) | |||
25699 | report_fatal_error( | |||
25700 | "querying registration node size for function without personality"); | |||
25701 | // The RegNodeSize is 6 32-bit words for SEH and 4 for C++ EH. See | |||
25702 | // WinEHStatePass for the full struct definition. | |||
25703 | switch (classifyEHPersonality(Fn->getPersonalityFn())) { | |||
25704 | case EHPersonality::MSVC_X86SEH: return 24; | |||
25705 | case EHPersonality::MSVC_CXX: return 16; | |||
25706 | default: break; | |||
25707 | } | |||
25708 | report_fatal_error( | |||
25709 | "can only recover FP for 32-bit MSVC EH personality functions"); | |||
25710 | } | |||
25711 | ||||
25712 | /// When the MSVC runtime transfers control to us, either to an outlined | |||
25713 | /// function or when returning to a parent frame after catching an exception, we | |||
25714 | /// recover the parent frame pointer by doing arithmetic on the incoming EBP. | |||
25715 | /// Here's the math: | |||
25716 | /// RegNodeBase = EntryEBP - RegNodeSize | |||
25717 | /// ParentFP = RegNodeBase - ParentFrameOffset | |||
25718 | /// Subtracting RegNodeSize takes us to the offset of the registration node, and | |||
25719 | /// subtracting the offset (negative on x86) takes us back to the parent FP. | |||
25720 | static SDValue recoverFramePointer(SelectionDAG &DAG, const Function *Fn, | |||
25721 | SDValue EntryEBP) { | |||
25722 | MachineFunction &MF = DAG.getMachineFunction(); | |||
25723 | SDLoc dl; | |||
25724 | ||||
25725 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
25726 | MVT PtrVT = TLI.getPointerTy(DAG.getDataLayout()); | |||
25727 | ||||
25728 | // It's possible that the parent function no longer has a personality function | |||
25729 | // if the exceptional code was optimized away, in which case we just return | |||
25730 | // the incoming EBP. | |||
25731 | if (!Fn->hasPersonalityFn()) | |||
25732 | return EntryEBP; | |||
25733 | ||||
25734 | // Get an MCSymbol that will ultimately resolve to the frame offset of the EH | |||
25735 | // registration, or the .set_setframe offset. | |||
25736 | MCSymbol *OffsetSym = | |||
25737 | MF.getMMI().getContext().getOrCreateParentFrameOffsetSymbol( | |||
25738 | GlobalValue::dropLLVMManglingEscape(Fn->getName())); | |||
25739 | SDValue OffsetSymVal = DAG.getMCSymbol(OffsetSym, PtrVT); | |||
25740 | SDValue ParentFrameOffset = | |||
25741 | DAG.getNode(ISD::LOCAL_RECOVER, dl, PtrVT, OffsetSymVal); | |||
25742 | ||||
25743 | // Return EntryEBP + ParentFrameOffset for x64. This adjusts from RSP after | |||
25744 | // prologue to RBP in the parent function. | |||
25745 | const X86Subtarget &Subtarget = | |||
25746 | static_cast<const X86Subtarget &>(DAG.getSubtarget()); | |||
25747 | if (Subtarget.is64Bit()) | |||
25748 | return DAG.getNode(ISD::ADD, dl, PtrVT, EntryEBP, ParentFrameOffset); | |||
25749 | ||||
25750 | int RegNodeSize = getSEHRegistrationNodeSize(Fn); | |||
25751 | // RegNodeBase = EntryEBP - RegNodeSize | |||
25752 | // ParentFP = RegNodeBase - ParentFrameOffset | |||
25753 | SDValue RegNodeBase = DAG.getNode(ISD::SUB, dl, PtrVT, EntryEBP, | |||
25754 | DAG.getConstant(RegNodeSize, dl, PtrVT)); | |||
25755 | return DAG.getNode(ISD::SUB, dl, PtrVT, RegNodeBase, ParentFrameOffset); | |||
25756 | } | |||
25757 | ||||
25758 | SDValue X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, | |||
25759 | SelectionDAG &DAG) const { | |||
25760 | // Helper to detect if the operand is CUR_DIRECTION rounding mode. | |||
25761 | auto isRoundModeCurDirection = [](SDValue Rnd) { | |||
25762 | if (auto *C = dyn_cast<ConstantSDNode>(Rnd)) | |||
25763 | return C->getAPIntValue() == X86::STATIC_ROUNDING::CUR_DIRECTION; | |||
25764 | ||||
25765 | return false; | |||
25766 | }; | |||
25767 | auto isRoundModeSAE = [](SDValue Rnd) { | |||
25768 | if (auto *C = dyn_cast<ConstantSDNode>(Rnd)) { | |||
25769 | unsigned RC = C->getZExtValue(); | |||
25770 | if (RC & X86::STATIC_ROUNDING::NO_EXC) { | |||
25771 | // Clear the NO_EXC bit and check remaining bits. | |||
25772 | RC ^= X86::STATIC_ROUNDING::NO_EXC; | |||
25773 | // As a convenience we allow no other bits or explicitly | |||
25774 | // current direction. | |||
25775 | return RC == 0 || RC == X86::STATIC_ROUNDING::CUR_DIRECTION; | |||
25776 | } | |||
25777 | } | |||
25778 | ||||
25779 | return false; | |||
25780 | }; | |||
25781 | auto isRoundModeSAEToX = [](SDValue Rnd, unsigned &RC) { | |||
25782 | if (auto *C = dyn_cast<ConstantSDNode>(Rnd)) { | |||
25783 | RC = C->getZExtValue(); | |||
25784 | if (RC & X86::STATIC_ROUNDING::NO_EXC) { | |||
25785 | // Clear the NO_EXC bit and check remaining bits. | |||
25786 | RC ^= X86::STATIC_ROUNDING::NO_EXC; | |||
25787 | return RC == X86::STATIC_ROUNDING::TO_NEAREST_INT || | |||
25788 | RC == X86::STATIC_ROUNDING::TO_NEG_INF || | |||
25789 | RC == X86::STATIC_ROUNDING::TO_POS_INF || | |||
25790 | RC == X86::STATIC_ROUNDING::TO_ZERO; | |||
25791 | } | |||
25792 | } | |||
25793 | ||||
25794 | return false; | |||
25795 | }; | |||
25796 | ||||
25797 | SDLoc dl(Op); | |||
25798 | unsigned IntNo = Op.getConstantOperandVal(0); | |||
25799 | MVT VT = Op.getSimpleValueType(); | |||
25800 | const IntrinsicData* IntrData = getIntrinsicWithoutChain(IntNo); | |||
25801 | ||||
25802 | // Propagate flags from original node to transformed node(s). | |||
25803 | SelectionDAG::FlagInserter FlagsInserter(DAG, Op->getFlags()); | |||
25804 | ||||
25805 | if (IntrData) { | |||
25806 | switch(IntrData->Type) { | |||
25807 | case INTR_TYPE_1OP: { | |||
25808 | // We specify 2 possible opcodes for intrinsics with rounding modes. | |||
25809 | // First, we check if the intrinsic may have non-default rounding mode, | |||
25810 | // (IntrData->Opc1 != 0), then we check the rounding mode operand. | |||
25811 | unsigned IntrWithRoundingModeOpcode = IntrData->Opc1; | |||
25812 | if (IntrWithRoundingModeOpcode != 0) { | |||
25813 | SDValue Rnd = Op.getOperand(2); | |||
25814 | unsigned RC = 0; | |||
25815 | if (isRoundModeSAEToX(Rnd, RC)) | |||
25816 | return DAG.getNode(IntrWithRoundingModeOpcode, dl, Op.getValueType(), | |||
25817 | Op.getOperand(1), | |||
25818 | DAG.getTargetConstant(RC, dl, MVT::i32)); | |||
25819 | if (!isRoundModeCurDirection(Rnd)) | |||
25820 | return SDValue(); | |||
25821 | } | |||
25822 | return DAG.getNode(IntrData->Opc0, dl, Op.getValueType(), | |||
25823 | Op.getOperand(1)); | |||
25824 | } | |||
25825 | case INTR_TYPE_1OP_SAE: { | |||
25826 | SDValue Sae = Op.getOperand(2); | |||
25827 | ||||
25828 | unsigned Opc; | |||
25829 | if (isRoundModeCurDirection(Sae)) | |||
25830 | Opc = IntrData->Opc0; | |||
25831 | else if (isRoundModeSAE(Sae)) | |||
25832 | Opc = IntrData->Opc1; | |||
25833 | else | |||
25834 | return SDValue(); | |||
25835 | ||||
25836 | return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1)); | |||
25837 | } | |||
25838 | case INTR_TYPE_2OP: { | |||
25839 | SDValue Src2 = Op.getOperand(2); | |||
25840 | ||||
25841 | // We specify 2 possible opcodes for intrinsics with rounding modes. | |||
25842 | // First, we check if the intrinsic may have non-default rounding mode, | |||
25843 | // (IntrData->Opc1 != 0), then we check the rounding mode operand. | |||
25844 | unsigned IntrWithRoundingModeOpcode = IntrData->Opc1; | |||
25845 | if (IntrWithRoundingModeOpcode != 0) { | |||
25846 | SDValue Rnd = Op.getOperand(3); | |||
25847 | unsigned RC = 0; | |||
25848 | if (isRoundModeSAEToX(Rnd, RC)) | |||
25849 | return DAG.getNode(IntrWithRoundingModeOpcode, dl, Op.getValueType(), | |||
25850 | Op.getOperand(1), Src2, | |||
25851 | DAG.getTargetConstant(RC, dl, MVT::i32)); | |||
25852 | if (!isRoundModeCurDirection(Rnd)) | |||
25853 | return SDValue(); | |||
25854 | } | |||
25855 | ||||
25856 | return DAG.getNode(IntrData->Opc0, dl, Op.getValueType(), | |||
25857 | Op.getOperand(1), Src2); | |||
25858 | } | |||
25859 | case INTR_TYPE_2OP_SAE: { | |||
25860 | SDValue Sae = Op.getOperand(3); | |||
25861 | ||||
25862 | unsigned Opc; | |||
25863 | if (isRoundModeCurDirection(Sae)) | |||
25864 | Opc = IntrData->Opc0; | |||
25865 | else if (isRoundModeSAE(Sae)) | |||
25866 | Opc = IntrData->Opc1; | |||
25867 | else | |||
25868 | return SDValue(); | |||
25869 | ||||
25870 | return DAG.getNode(Opc, dl, Op.getValueType(), Op.getOperand(1), | |||
25871 | Op.getOperand(2)); | |||
25872 | } | |||
25873 | case INTR_TYPE_3OP: | |||
25874 | case INTR_TYPE_3OP_IMM8: { | |||
25875 | SDValue Src1 = Op.getOperand(1); | |||
25876 | SDValue Src2 = Op.getOperand(2); | |||
25877 | SDValue Src3 = Op.getOperand(3); | |||
25878 | ||||
25879 | if (IntrData->Type == INTR_TYPE_3OP_IMM8 && | |||
25880 | Src3.getValueType() != MVT::i8) { | |||
25881 | Src3 = DAG.getTargetConstant( | |||
25882 | cast<ConstantSDNode>(Src3)->getZExtValue() & 0xff, dl, MVT::i8); | |||
25883 | } | |||
25884 | ||||
25885 | // We specify 2 possible opcodes for intrinsics with rounding modes. | |||
25886 | // First, we check if the intrinsic may have non-default rounding mode, | |||
25887 | // (IntrData->Opc1 != 0), then we check the rounding mode operand. | |||
25888 | unsigned IntrWithRoundingModeOpcode = IntrData->Opc1; | |||
25889 | if (IntrWithRoundingModeOpcode != 0) { | |||
25890 | SDValue Rnd = Op.getOperand(4); | |||
25891 | unsigned RC = 0; | |||
25892 | if (isRoundModeSAEToX(Rnd, RC)) | |||
25893 | return DAG.getNode(IntrWithRoundingModeOpcode, dl, Op.getValueType(), | |||
25894 | Src1, Src2, Src3, | |||
25895 | DAG.getTargetConstant(RC, dl, MVT::i32)); | |||
25896 | if (!isRoundModeCurDirection(Rnd)) | |||
25897 | return SDValue(); | |||
25898 | } | |||
25899 | ||||
25900 | return DAG.getNode(IntrData->Opc0, dl, Op.getValueType(), | |||
25901 | {Src1, Src2, Src3}); | |||
25902 | } | |||
25903 | case INTR_TYPE_4OP_IMM8: { | |||
25904 | assert(Op.getOperand(4)->getOpcode() == ISD::TargetConstant)(static_cast <bool> (Op.getOperand(4)->getOpcode() == ISD::TargetConstant) ? void (0) : __assert_fail ("Op.getOperand(4)->getOpcode() == ISD::TargetConstant" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25904, __extension__ __PRETTY_FUNCTION__)); | |||
25905 | SDValue Src4 = Op.getOperand(4); | |||
25906 | if (Src4.getValueType() != MVT::i8) { | |||
25907 | Src4 = DAG.getTargetConstant( | |||
25908 | cast<ConstantSDNode>(Src4)->getZExtValue() & 0xff, dl, MVT::i8); | |||
25909 | } | |||
25910 | ||||
25911 | return DAG.getNode(IntrData->Opc0, dl, Op.getValueType(), | |||
25912 | Op.getOperand(1), Op.getOperand(2), Op.getOperand(3), | |||
25913 | Src4); | |||
25914 | } | |||
25915 | case INTR_TYPE_1OP_MASK: { | |||
25916 | SDValue Src = Op.getOperand(1); | |||
25917 | SDValue PassThru = Op.getOperand(2); | |||
25918 | SDValue Mask = Op.getOperand(3); | |||
25919 | // We add rounding mode to the Node when | |||
25920 | // - RC Opcode is specified and | |||
25921 | // - RC is not "current direction". | |||
25922 | unsigned IntrWithRoundingModeOpcode = IntrData->Opc1; | |||
25923 | if (IntrWithRoundingModeOpcode != 0) { | |||
25924 | SDValue Rnd = Op.getOperand(4); | |||
25925 | unsigned RC = 0; | |||
25926 | if (isRoundModeSAEToX(Rnd, RC)) | |||
25927 | return getVectorMaskingNode( | |||
25928 | DAG.getNode(IntrWithRoundingModeOpcode, dl, Op.getValueType(), | |||
25929 | Src, DAG.getTargetConstant(RC, dl, MVT::i32)), | |||
25930 | Mask, PassThru, Subtarget, DAG); | |||
25931 | if (!isRoundModeCurDirection(Rnd)) | |||
25932 | return SDValue(); | |||
25933 | } | |||
25934 | return getVectorMaskingNode( | |||
25935 | DAG.getNode(IntrData->Opc0, dl, VT, Src), Mask, PassThru, | |||
25936 | Subtarget, DAG); | |||
25937 | } | |||
25938 | case INTR_TYPE_1OP_MASK_SAE: { | |||
25939 | SDValue Src = Op.getOperand(1); | |||
25940 | SDValue PassThru = Op.getOperand(2); | |||
25941 | SDValue Mask = Op.getOperand(3); | |||
25942 | SDValue Rnd = Op.getOperand(4); | |||
25943 | ||||
25944 | unsigned Opc; | |||
25945 | if (isRoundModeCurDirection(Rnd)) | |||
25946 | Opc = IntrData->Opc0; | |||
25947 | else if (isRoundModeSAE(Rnd)) | |||
25948 | Opc = IntrData->Opc1; | |||
25949 | else | |||
25950 | return SDValue(); | |||
25951 | ||||
25952 | return getVectorMaskingNode(DAG.getNode(Opc, dl, VT, Src), Mask, PassThru, | |||
25953 | Subtarget, DAG); | |||
25954 | } | |||
25955 | case INTR_TYPE_SCALAR_MASK: { | |||
25956 | SDValue Src1 = Op.getOperand(1); | |||
25957 | SDValue Src2 = Op.getOperand(2); | |||
25958 | SDValue passThru = Op.getOperand(3); | |||
25959 | SDValue Mask = Op.getOperand(4); | |||
25960 | unsigned IntrWithRoundingModeOpcode = IntrData->Opc1; | |||
25961 | // There are 2 kinds of intrinsics in this group: | |||
25962 | // (1) With suppress-all-exceptions (sae) or rounding mode- 6 operands | |||
25963 | // (2) With rounding mode and sae - 7 operands. | |||
25964 | bool HasRounding = IntrWithRoundingModeOpcode != 0; | |||
25965 | if (Op.getNumOperands() == (5U + HasRounding)) { | |||
25966 | if (HasRounding) { | |||
25967 | SDValue Rnd = Op.getOperand(5); | |||
25968 | unsigned RC = 0; | |||
25969 | if (isRoundModeSAEToX(Rnd, RC)) | |||
25970 | return getScalarMaskingNode( | |||
25971 | DAG.getNode(IntrWithRoundingModeOpcode, dl, VT, Src1, Src2, | |||
25972 | DAG.getTargetConstant(RC, dl, MVT::i32)), | |||
25973 | Mask, passThru, Subtarget, DAG); | |||
25974 | if (!isRoundModeCurDirection(Rnd)) | |||
25975 | return SDValue(); | |||
25976 | } | |||
25977 | return getScalarMaskingNode(DAG.getNode(IntrData->Opc0, dl, VT, Src1, | |||
25978 | Src2), | |||
25979 | Mask, passThru, Subtarget, DAG); | |||
25980 | } | |||
25981 | ||||
25982 | assert(Op.getNumOperands() == (6U + HasRounding) &&(static_cast <bool> (Op.getNumOperands() == (6U + HasRounding ) && "Unexpected intrinsic form") ? void (0) : __assert_fail ("Op.getNumOperands() == (6U + HasRounding) && \"Unexpected intrinsic form\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25983, __extension__ __PRETTY_FUNCTION__)) | |||
25983 | "Unexpected intrinsic form")(static_cast <bool> (Op.getNumOperands() == (6U + HasRounding ) && "Unexpected intrinsic form") ? void (0) : __assert_fail ("Op.getNumOperands() == (6U + HasRounding) && \"Unexpected intrinsic form\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 25983, __extension__ __PRETTY_FUNCTION__)); | |||
25984 | SDValue RoundingMode = Op.getOperand(5); | |||
25985 | unsigned Opc = IntrData->Opc0; | |||
25986 | if (HasRounding) { | |||
25987 | SDValue Sae = Op.getOperand(6); | |||
25988 | if (isRoundModeSAE(Sae)) | |||
25989 | Opc = IntrWithRoundingModeOpcode; | |||
25990 | else if (!isRoundModeCurDirection(Sae)) | |||
25991 | return SDValue(); | |||
25992 | } | |||
25993 | return getScalarMaskingNode(DAG.getNode(Opc, dl, VT, Src1, | |||
25994 | Src2, RoundingMode), | |||
25995 | Mask, passThru, Subtarget, DAG); | |||
25996 | } | |||
25997 | case INTR_TYPE_SCALAR_MASK_RND: { | |||
25998 | SDValue Src1 = Op.getOperand(1); | |||
25999 | SDValue Src2 = Op.getOperand(2); | |||
26000 | SDValue passThru = Op.getOperand(3); | |||
26001 | SDValue Mask = Op.getOperand(4); | |||
26002 | SDValue Rnd = Op.getOperand(5); | |||
26003 | ||||
26004 | SDValue NewOp; | |||
26005 | unsigned RC = 0; | |||
26006 | if (isRoundModeCurDirection(Rnd)) | |||
26007 | NewOp = DAG.getNode(IntrData->Opc0, dl, VT, Src1, Src2); | |||
26008 | else if (isRoundModeSAEToX(Rnd, RC)) | |||
26009 | NewOp = DAG.getNode(IntrData->Opc1, dl, VT, Src1, Src2, | |||
26010 | DAG.getTargetConstant(RC, dl, MVT::i32)); | |||
26011 | else | |||
26012 | return SDValue(); | |||
26013 | ||||
26014 | return getScalarMaskingNode(NewOp, Mask, passThru, Subtarget, DAG); | |||
26015 | } | |||
26016 | case INTR_TYPE_SCALAR_MASK_SAE: { | |||
26017 | SDValue Src1 = Op.getOperand(1); | |||
26018 | SDValue Src2 = Op.getOperand(2); | |||
26019 | SDValue passThru = Op.getOperand(3); | |||
26020 | SDValue Mask = Op.getOperand(4); | |||
26021 | SDValue Sae = Op.getOperand(5); | |||
26022 | unsigned Opc; | |||
26023 | if (isRoundModeCurDirection(Sae)) | |||
26024 | Opc = IntrData->Opc0; | |||
26025 | else if (isRoundModeSAE(Sae)) | |||
26026 | Opc = IntrData->Opc1; | |||
26027 | else | |||
26028 | return SDValue(); | |||
26029 | ||||
26030 | return getScalarMaskingNode(DAG.getNode(Opc, dl, VT, Src1, Src2), | |||
26031 | Mask, passThru, Subtarget, DAG); | |||
26032 | } | |||
26033 | case INTR_TYPE_2OP_MASK: { | |||
26034 | SDValue Src1 = Op.getOperand(1); | |||
26035 | SDValue Src2 = Op.getOperand(2); | |||
26036 | SDValue PassThru = Op.getOperand(3); | |||
26037 | SDValue Mask = Op.getOperand(4); | |||
26038 | SDValue NewOp; | |||
26039 | if (IntrData->Opc1 != 0) { | |||
26040 | SDValue Rnd = Op.getOperand(5); | |||
26041 | unsigned RC = 0; | |||
26042 | if (isRoundModeSAEToX(Rnd, RC)) | |||
26043 | NewOp = DAG.getNode(IntrData->Opc1, dl, VT, Src1, Src2, | |||
26044 | DAG.getTargetConstant(RC, dl, MVT::i32)); | |||
26045 | else if (!isRoundModeCurDirection(Rnd)) | |||
26046 | return SDValue(); | |||
26047 | } | |||
26048 | if (!NewOp) | |||
26049 | NewOp = DAG.getNode(IntrData->Opc0, dl, VT, Src1, Src2); | |||
26050 | return getVectorMaskingNode(NewOp, Mask, PassThru, Subtarget, DAG); | |||
26051 | } | |||
26052 | case INTR_TYPE_2OP_MASK_SAE: { | |||
26053 | SDValue Src1 = Op.getOperand(1); | |||
26054 | SDValue Src2 = Op.getOperand(2); | |||
26055 | SDValue PassThru = Op.getOperand(3); | |||
26056 | SDValue Mask = Op.getOperand(4); | |||
26057 | ||||
26058 | unsigned Opc = IntrData->Opc0; | |||
26059 | if (IntrData->Opc1 != 0) { | |||
26060 | SDValue Sae = Op.getOperand(5); | |||
26061 | if (isRoundModeSAE(Sae)) | |||
26062 | Opc = IntrData->Opc1; | |||
26063 | else if (!isRoundModeCurDirection(Sae)) | |||
26064 | return SDValue(); | |||
26065 | } | |||
26066 | ||||
26067 | return getVectorMaskingNode(DAG.getNode(Opc, dl, VT, Src1, Src2), | |||
26068 | Mask, PassThru, Subtarget, DAG); | |||
26069 | } | |||
26070 | case INTR_TYPE_3OP_SCALAR_MASK_SAE: { | |||
26071 | SDValue Src1 = Op.getOperand(1); | |||
26072 | SDValue Src2 = Op.getOperand(2); | |||
26073 | SDValue Src3 = Op.getOperand(3); | |||
26074 | SDValue PassThru = Op.getOperand(4); | |||
26075 | SDValue Mask = Op.getOperand(5); | |||
26076 | SDValue Sae = Op.getOperand(6); | |||
26077 | unsigned Opc; | |||
26078 | if (isRoundModeCurDirection(Sae)) | |||
26079 | Opc = IntrData->Opc0; | |||
26080 | else if (isRoundModeSAE(Sae)) | |||
26081 | Opc = IntrData->Opc1; | |||
26082 | else | |||
26083 | return SDValue(); | |||
26084 | ||||
26085 | return getScalarMaskingNode(DAG.getNode(Opc, dl, VT, Src1, Src2, Src3), | |||
26086 | Mask, PassThru, Subtarget, DAG); | |||
26087 | } | |||
26088 | case INTR_TYPE_3OP_MASK_SAE: { | |||
26089 | SDValue Src1 = Op.getOperand(1); | |||
26090 | SDValue Src2 = Op.getOperand(2); | |||
26091 | SDValue Src3 = Op.getOperand(3); | |||
26092 | SDValue PassThru = Op.getOperand(4); | |||
26093 | SDValue Mask = Op.getOperand(5); | |||
26094 | ||||
26095 | unsigned Opc = IntrData->Opc0; | |||
26096 | if (IntrData->Opc1 != 0) { | |||
26097 | SDValue Sae = Op.getOperand(6); | |||
26098 | if (isRoundModeSAE(Sae)) | |||
26099 | Opc = IntrData->Opc1; | |||
26100 | else if (!isRoundModeCurDirection(Sae)) | |||
26101 | return SDValue(); | |||
26102 | } | |||
26103 | return getVectorMaskingNode(DAG.getNode(Opc, dl, VT, Src1, Src2, Src3), | |||
26104 | Mask, PassThru, Subtarget, DAG); | |||
26105 | } | |||
26106 | case BLENDV: { | |||
26107 | SDValue Src1 = Op.getOperand(1); | |||
26108 | SDValue Src2 = Op.getOperand(2); | |||
26109 | SDValue Src3 = Op.getOperand(3); | |||
26110 | ||||
26111 | EVT MaskVT = Src3.getValueType().changeVectorElementTypeToInteger(); | |||
26112 | Src3 = DAG.getBitcast(MaskVT, Src3); | |||
26113 | ||||
26114 | // Reverse the operands to match VSELECT order. | |||
26115 | return DAG.getNode(IntrData->Opc0, dl, VT, Src3, Src2, Src1); | |||
26116 | } | |||
26117 | case VPERM_2OP : { | |||
26118 | SDValue Src1 = Op.getOperand(1); | |||
26119 | SDValue Src2 = Op.getOperand(2); | |||
26120 | ||||
26121 | // Swap Src1 and Src2 in the node creation | |||
26122 | return DAG.getNode(IntrData->Opc0, dl, VT,Src2, Src1); | |||
26123 | } | |||
26124 | case CFMA_OP_MASKZ: | |||
26125 | case CFMA_OP_MASK: { | |||
26126 | SDValue Src1 = Op.getOperand(1); | |||
26127 | SDValue Src2 = Op.getOperand(2); | |||
26128 | SDValue Src3 = Op.getOperand(3); | |||
26129 | SDValue Mask = Op.getOperand(4); | |||
26130 | MVT VT = Op.getSimpleValueType(); | |||
26131 | ||||
26132 | SDValue PassThru = Src3; | |||
26133 | if (IntrData->Type == CFMA_OP_MASKZ) | |||
26134 | PassThru = getZeroVector(VT, Subtarget, DAG, dl); | |||
26135 | ||||
26136 | // We add rounding mode to the Node when | |||
26137 | // - RC Opcode is specified and | |||
26138 | // - RC is not "current direction". | |||
26139 | SDValue NewOp; | |||
26140 | if (IntrData->Opc1 != 0) { | |||
26141 | SDValue Rnd = Op.getOperand(5); | |||
26142 | unsigned RC = 0; | |||
26143 | if (isRoundModeSAEToX(Rnd, RC)) | |||
26144 | NewOp = DAG.getNode(IntrData->Opc1, dl, VT, Src1, Src2, Src3, | |||
26145 | DAG.getTargetConstant(RC, dl, MVT::i32)); | |||
26146 | else if (!isRoundModeCurDirection(Rnd)) | |||
26147 | return SDValue(); | |||
26148 | } | |||
26149 | if (!NewOp) | |||
26150 | NewOp = DAG.getNode(IntrData->Opc0, dl, VT, Src1, Src2, Src3); | |||
26151 | return getVectorMaskingNode(NewOp, Mask, PassThru, Subtarget, DAG); | |||
26152 | } | |||
26153 | case IFMA_OP: | |||
26154 | // NOTE: We need to swizzle the operands to pass the multiply operands | |||
26155 | // first. | |||
26156 | return DAG.getNode(IntrData->Opc0, dl, Op.getValueType(), | |||
26157 | Op.getOperand(2), Op.getOperand(3), Op.getOperand(1)); | |||
26158 | case FPCLASSS: { | |||
26159 | SDValue Src1 = Op.getOperand(1); | |||
26160 | SDValue Imm = Op.getOperand(2); | |||
26161 | SDValue Mask = Op.getOperand(3); | |||
26162 | SDValue FPclass = DAG.getNode(IntrData->Opc0, dl, MVT::v1i1, Src1, Imm); | |||
26163 | SDValue FPclassMask = getScalarMaskingNode(FPclass, Mask, SDValue(), | |||
26164 | Subtarget, DAG); | |||
26165 | // Need to fill with zeros to ensure the bitcast will produce zeroes | |||
26166 | // for the upper bits. An EXTRACT_ELEMENT here wouldn't guarantee that. | |||
26167 | SDValue Ins = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, MVT::v8i1, | |||
26168 | DAG.getConstant(0, dl, MVT::v8i1), | |||
26169 | FPclassMask, DAG.getIntPtrConstant(0, dl)); | |||
26170 | return DAG.getBitcast(MVT::i8, Ins); | |||
26171 | } | |||
26172 | ||||
26173 | case CMP_MASK_CC: { | |||
26174 | MVT MaskVT = Op.getSimpleValueType(); | |||
26175 | SDValue CC = Op.getOperand(3); | |||
26176 | SDValue Mask = Op.getOperand(4); | |||
26177 | // We specify 2 possible opcodes for intrinsics with rounding modes. | |||
26178 | // First, we check if the intrinsic may have non-default rounding mode, | |||
26179 | // (IntrData->Opc1 != 0), then we check the rounding mode operand. | |||
26180 | if (IntrData->Opc1 != 0) { | |||
26181 | SDValue Sae = Op.getOperand(5); | |||
26182 | if (isRoundModeSAE(Sae)) | |||
26183 | return DAG.getNode(IntrData->Opc1, dl, MaskVT, Op.getOperand(1), | |||
26184 | Op.getOperand(2), CC, Mask, Sae); | |||
26185 | if (!isRoundModeCurDirection(Sae)) | |||
26186 | return SDValue(); | |||
26187 | } | |||
26188 | //default rounding mode | |||
26189 | return DAG.getNode(IntrData->Opc0, dl, MaskVT, | |||
26190 | {Op.getOperand(1), Op.getOperand(2), CC, Mask}); | |||
26191 | } | |||
26192 | case CMP_MASK_SCALAR_CC: { | |||
26193 | SDValue Src1 = Op.getOperand(1); | |||
26194 | SDValue Src2 = Op.getOperand(2); | |||
26195 | SDValue CC = Op.getOperand(3); | |||
26196 | SDValue Mask = Op.getOperand(4); | |||
26197 | ||||
26198 | SDValue Cmp; | |||
26199 | if (IntrData->Opc1 != 0) { | |||
26200 | SDValue Sae = Op.getOperand(5); | |||
26201 | if (isRoundModeSAE(Sae)) | |||
26202 | Cmp = DAG.getNode(IntrData->Opc1, dl, MVT::v1i1, Src1, Src2, CC, Sae); | |||
26203 | else if (!isRoundModeCurDirection(Sae)) | |||
26204 | return SDValue(); | |||
26205 | } | |||
26206 | //default rounding mode | |||
26207 | if (!Cmp.getNode()) | |||
26208 | Cmp = DAG.getNode(IntrData->Opc0, dl, MVT::v1i1, Src1, Src2, CC); | |||
26209 | ||||
26210 | SDValue CmpMask = getScalarMaskingNode(Cmp, Mask, SDValue(), | |||
26211 | Subtarget, DAG); | |||
26212 | // Need to fill with zeros to ensure the bitcast will produce zeroes | |||
26213 | // for the upper bits. An EXTRACT_ELEMENT here wouldn't guarantee that. | |||
26214 | SDValue Ins = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, MVT::v8i1, | |||
26215 | DAG.getConstant(0, dl, MVT::v8i1), | |||
26216 | CmpMask, DAG.getIntPtrConstant(0, dl)); | |||
26217 | return DAG.getBitcast(MVT::i8, Ins); | |||
26218 | } | |||
26219 | case COMI: { // Comparison intrinsics | |||
26220 | ISD::CondCode CC = (ISD::CondCode)IntrData->Opc1; | |||
26221 | SDValue LHS = Op.getOperand(1); | |||
26222 | SDValue RHS = Op.getOperand(2); | |||
26223 | // Some conditions require the operands to be swapped. | |||
26224 | if (CC == ISD::SETLT || CC == ISD::SETLE) | |||
26225 | std::swap(LHS, RHS); | |||
26226 | ||||
26227 | SDValue Comi = DAG.getNode(IntrData->Opc0, dl, MVT::i32, LHS, RHS); | |||
26228 | SDValue SetCC; | |||
26229 | switch (CC) { | |||
26230 | case ISD::SETEQ: { // (ZF = 0 and PF = 0) | |||
26231 | SetCC = getSETCC(X86::COND_E, Comi, dl, DAG); | |||
26232 | SDValue SetNP = getSETCC(X86::COND_NP, Comi, dl, DAG); | |||
26233 | SetCC = DAG.getNode(ISD::AND, dl, MVT::i8, SetCC, SetNP); | |||
26234 | break; | |||
26235 | } | |||
26236 | case ISD::SETNE: { // (ZF = 1 or PF = 1) | |||
26237 | SetCC = getSETCC(X86::COND_NE, Comi, dl, DAG); | |||
26238 | SDValue SetP = getSETCC(X86::COND_P, Comi, dl, DAG); | |||
26239 | SetCC = DAG.getNode(ISD::OR, dl, MVT::i8, SetCC, SetP); | |||
26240 | break; | |||
26241 | } | |||
26242 | case ISD::SETGT: // (CF = 0 and ZF = 0) | |||
26243 | case ISD::SETLT: { // Condition opposite to GT. Operands swapped above. | |||
26244 | SetCC = getSETCC(X86::COND_A, Comi, dl, DAG); | |||
26245 | break; | |||
26246 | } | |||
26247 | case ISD::SETGE: // CF = 0 | |||
26248 | case ISD::SETLE: // Condition opposite to GE. Operands swapped above. | |||
26249 | SetCC = getSETCC(X86::COND_AE, Comi, dl, DAG); | |||
26250 | break; | |||
26251 | default: | |||
26252 | llvm_unreachable("Unexpected illegal condition!")::llvm::llvm_unreachable_internal("Unexpected illegal condition!" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 26252); | |||
26253 | } | |||
26254 | return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC); | |||
26255 | } | |||
26256 | case COMI_RM: { // Comparison intrinsics with Sae | |||
26257 | SDValue LHS = Op.getOperand(1); | |||
26258 | SDValue RHS = Op.getOperand(2); | |||
26259 | unsigned CondVal = Op.getConstantOperandVal(3); | |||
26260 | SDValue Sae = Op.getOperand(4); | |||
26261 | ||||
26262 | SDValue FCmp; | |||
26263 | if (isRoundModeCurDirection(Sae)) | |||
26264 | FCmp = DAG.getNode(X86ISD::FSETCCM, dl, MVT::v1i1, LHS, RHS, | |||
26265 | DAG.getTargetConstant(CondVal, dl, MVT::i8)); | |||
26266 | else if (isRoundModeSAE(Sae)) | |||
26267 | FCmp = DAG.getNode(X86ISD::FSETCCM_SAE, dl, MVT::v1i1, LHS, RHS, | |||
26268 | DAG.getTargetConstant(CondVal, dl, MVT::i8), Sae); | |||
26269 | else | |||
26270 | return SDValue(); | |||
26271 | // Need to fill with zeros to ensure the bitcast will produce zeroes | |||
26272 | // for the upper bits. An EXTRACT_ELEMENT here wouldn't guarantee that. | |||
26273 | SDValue Ins = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, MVT::v16i1, | |||
26274 | DAG.getConstant(0, dl, MVT::v16i1), | |||
26275 | FCmp, DAG.getIntPtrConstant(0, dl)); | |||
26276 | return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, | |||
26277 | DAG.getBitcast(MVT::i16, Ins)); | |||
26278 | } | |||
26279 | case VSHIFT: | |||
26280 | return getTargetVShiftNode(IntrData->Opc0, dl, Op.getSimpleValueType(), | |||
26281 | Op.getOperand(1), Op.getOperand(2), Subtarget, | |||
26282 | DAG); | |||
26283 | case COMPRESS_EXPAND_IN_REG: { | |||
26284 | SDValue Mask = Op.getOperand(3); | |||
26285 | SDValue DataToCompress = Op.getOperand(1); | |||
26286 | SDValue PassThru = Op.getOperand(2); | |||
26287 | if (ISD::isBuildVectorAllOnes(Mask.getNode())) // return data as is | |||
26288 | return Op.getOperand(1); | |||
26289 | ||||
26290 | // Avoid false dependency. | |||
26291 | if (PassThru.isUndef()) | |||
26292 | PassThru = DAG.getConstant(0, dl, VT); | |||
26293 | ||||
26294 | return DAG.getNode(IntrData->Opc0, dl, VT, DataToCompress, PassThru, | |||
26295 | Mask); | |||
26296 | } | |||
26297 | case FIXUPIMM: | |||
26298 | case FIXUPIMM_MASKZ: { | |||
26299 | SDValue Src1 = Op.getOperand(1); | |||
26300 | SDValue Src2 = Op.getOperand(2); | |||
26301 | SDValue Src3 = Op.getOperand(3); | |||
26302 | SDValue Imm = Op.getOperand(4); | |||
26303 | SDValue Mask = Op.getOperand(5); | |||
26304 | SDValue Passthru = (IntrData->Type == FIXUPIMM) | |||
26305 | ? Src1 | |||
26306 | : getZeroVector(VT, Subtarget, DAG, dl); | |||
26307 | ||||
26308 | unsigned Opc = IntrData->Opc0; | |||
26309 | if (IntrData->Opc1 != 0) { | |||
26310 | SDValue Sae = Op.getOperand(6); | |||
26311 | if (isRoundModeSAE(Sae)) | |||
26312 | Opc = IntrData->Opc1; | |||
26313 | else if (!isRoundModeCurDirection(Sae)) | |||
26314 | return SDValue(); | |||
26315 | } | |||
26316 | ||||
26317 | SDValue FixupImm = DAG.getNode(Opc, dl, VT, Src1, Src2, Src3, Imm); | |||
26318 | ||||
26319 | if (Opc == X86ISD::VFIXUPIMM || Opc == X86ISD::VFIXUPIMM_SAE) | |||
26320 | return getVectorMaskingNode(FixupImm, Mask, Passthru, Subtarget, DAG); | |||
26321 | ||||
26322 | return getScalarMaskingNode(FixupImm, Mask, Passthru, Subtarget, DAG); | |||
26323 | } | |||
26324 | case ROUNDP: { | |||
26325 | assert(IntrData->Opc0 == X86ISD::VRNDSCALE && "Unexpected opcode")(static_cast <bool> (IntrData->Opc0 == X86ISD::VRNDSCALE && "Unexpected opcode") ? void (0) : __assert_fail ( "IntrData->Opc0 == X86ISD::VRNDSCALE && \"Unexpected opcode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 26325, __extension__ __PRETTY_FUNCTION__)); | |||
26326 | // Clear the upper bits of the rounding immediate so that the legacy | |||
26327 | // intrinsic can't trigger the scaling behavior of VRNDSCALE. | |||
26328 | auto Round = cast<ConstantSDNode>(Op.getOperand(2)); | |||
26329 | SDValue RoundingMode = | |||
26330 | DAG.getTargetConstant(Round->getZExtValue() & 0xf, dl, MVT::i32); | |||
26331 | return DAG.getNode(IntrData->Opc0, dl, Op.getValueType(), | |||
26332 | Op.getOperand(1), RoundingMode); | |||
26333 | } | |||
26334 | case ROUNDS: { | |||
26335 | assert(IntrData->Opc0 == X86ISD::VRNDSCALES && "Unexpected opcode")(static_cast <bool> (IntrData->Opc0 == X86ISD::VRNDSCALES && "Unexpected opcode") ? void (0) : __assert_fail ( "IntrData->Opc0 == X86ISD::VRNDSCALES && \"Unexpected opcode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 26335, __extension__ __PRETTY_FUNCTION__)); | |||
26336 | // Clear the upper bits of the rounding immediate so that the legacy | |||
26337 | // intrinsic can't trigger the scaling behavior of VRNDSCALE. | |||
26338 | auto Round = cast<ConstantSDNode>(Op.getOperand(3)); | |||
26339 | SDValue RoundingMode = | |||
26340 | DAG.getTargetConstant(Round->getZExtValue() & 0xf, dl, MVT::i32); | |||
26341 | return DAG.getNode(IntrData->Opc0, dl, Op.getValueType(), | |||
26342 | Op.getOperand(1), Op.getOperand(2), RoundingMode); | |||
26343 | } | |||
26344 | case BEXTRI: { | |||
26345 | assert(IntrData->Opc0 == X86ISD::BEXTRI && "Unexpected opcode")(static_cast <bool> (IntrData->Opc0 == X86ISD::BEXTRI && "Unexpected opcode") ? void (0) : __assert_fail ( "IntrData->Opc0 == X86ISD::BEXTRI && \"Unexpected opcode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 26345, __extension__ __PRETTY_FUNCTION__)); | |||
26346 | ||||
26347 | uint64_t Imm = Op.getConstantOperandVal(2); | |||
26348 | SDValue Control = DAG.getTargetConstant(Imm & 0xffff, dl, | |||
26349 | Op.getValueType()); | |||
26350 | return DAG.getNode(IntrData->Opc0, dl, Op.getValueType(), | |||
26351 | Op.getOperand(1), Control); | |||
26352 | } | |||
26353 | // ADC/ADCX/SBB | |||
26354 | case ADX: { | |||
26355 | SDVTList CFVTs = DAG.getVTList(Op->getValueType(0), MVT::i32); | |||
26356 | SDVTList VTs = DAG.getVTList(Op.getOperand(2).getValueType(), MVT::i32); | |||
26357 | ||||
26358 | SDValue Res; | |||
26359 | // If the carry in is zero, then we should just use ADD/SUB instead of | |||
26360 | // ADC/SBB. | |||
26361 | if (isNullConstant(Op.getOperand(1))) { | |||
26362 | Res = DAG.getNode(IntrData->Opc1, dl, VTs, Op.getOperand(2), | |||
26363 | Op.getOperand(3)); | |||
26364 | } else { | |||
26365 | SDValue GenCF = DAG.getNode(X86ISD::ADD, dl, CFVTs, Op.getOperand(1), | |||
26366 | DAG.getConstant(-1, dl, MVT::i8)); | |||
26367 | Res = DAG.getNode(IntrData->Opc0, dl, VTs, Op.getOperand(2), | |||
26368 | Op.getOperand(3), GenCF.getValue(1)); | |||
26369 | } | |||
26370 | SDValue SetCC = getSETCC(X86::COND_B, Res.getValue(1), dl, DAG); | |||
26371 | SDValue Results[] = { SetCC, Res }; | |||
26372 | return DAG.getMergeValues(Results, dl); | |||
26373 | } | |||
26374 | case CVTPD2PS_MASK: | |||
26375 | case CVTPD2DQ_MASK: | |||
26376 | case CVTQQ2PS_MASK: | |||
26377 | case TRUNCATE_TO_REG: { | |||
26378 | SDValue Src = Op.getOperand(1); | |||
26379 | SDValue PassThru = Op.getOperand(2); | |||
26380 | SDValue Mask = Op.getOperand(3); | |||
26381 | ||||
26382 | if (isAllOnesConstant(Mask)) | |||
26383 | return DAG.getNode(IntrData->Opc0, dl, Op.getValueType(), Src); | |||
26384 | ||||
26385 | MVT SrcVT = Src.getSimpleValueType(); | |||
26386 | MVT MaskVT = MVT::getVectorVT(MVT::i1, SrcVT.getVectorNumElements()); | |||
26387 | Mask = getMaskNode(Mask, MaskVT, Subtarget, DAG, dl); | |||
26388 | return DAG.getNode(IntrData->Opc1, dl, Op.getValueType(), | |||
26389 | {Src, PassThru, Mask}); | |||
26390 | } | |||
26391 | case CVTPS2PH_MASK: { | |||
26392 | SDValue Src = Op.getOperand(1); | |||
26393 | SDValue Rnd = Op.getOperand(2); | |||
26394 | SDValue PassThru = Op.getOperand(3); | |||
26395 | SDValue Mask = Op.getOperand(4); | |||
26396 | ||||
26397 | if (isAllOnesConstant(Mask)) | |||
26398 | return DAG.getNode(IntrData->Opc0, dl, Op.getValueType(), Src, Rnd); | |||
26399 | ||||
26400 | MVT SrcVT = Src.getSimpleValueType(); | |||
26401 | MVT MaskVT = MVT::getVectorVT(MVT::i1, SrcVT.getVectorNumElements()); | |||
26402 | Mask = getMaskNode(Mask, MaskVT, Subtarget, DAG, dl); | |||
26403 | return DAG.getNode(IntrData->Opc1, dl, Op.getValueType(), Src, Rnd, | |||
26404 | PassThru, Mask); | |||
26405 | ||||
26406 | } | |||
26407 | case CVTNEPS2BF16_MASK: { | |||
26408 | SDValue Src = Op.getOperand(1); | |||
26409 | SDValue PassThru = Op.getOperand(2); | |||
26410 | SDValue Mask = Op.getOperand(3); | |||
26411 | ||||
26412 | if (ISD::isBuildVectorAllOnes(Mask.getNode())) | |||
26413 | return DAG.getNode(IntrData->Opc0, dl, Op.getValueType(), Src); | |||
26414 | ||||
26415 | // Break false dependency. | |||
26416 | if (PassThru.isUndef()) | |||
26417 | PassThru = DAG.getConstant(0, dl, PassThru.getValueType()); | |||
26418 | ||||
26419 | return DAG.getNode(IntrData->Opc1, dl, Op.getValueType(), Src, PassThru, | |||
26420 | Mask); | |||
26421 | } | |||
26422 | default: | |||
26423 | break; | |||
26424 | } | |||
26425 | } | |||
26426 | ||||
26427 | switch (IntNo) { | |||
26428 | default: return SDValue(); // Don't custom lower most intrinsics. | |||
26429 | ||||
26430 | // ptest and testp intrinsics. The intrinsic these come from are designed to | |||
26431 | // return an integer value, not just an instruction so lower it to the ptest | |||
26432 | // or testp pattern and a setcc for the result. | |||
26433 | case Intrinsic::x86_avx512_ktestc_b: | |||
26434 | case Intrinsic::x86_avx512_ktestc_w: | |||
26435 | case Intrinsic::x86_avx512_ktestc_d: | |||
26436 | case Intrinsic::x86_avx512_ktestc_q: | |||
26437 | case Intrinsic::x86_avx512_ktestz_b: | |||
26438 | case Intrinsic::x86_avx512_ktestz_w: | |||
26439 | case Intrinsic::x86_avx512_ktestz_d: | |||
26440 | case Intrinsic::x86_avx512_ktestz_q: | |||
26441 | case Intrinsic::x86_sse41_ptestz: | |||
26442 | case Intrinsic::x86_sse41_ptestc: | |||
26443 | case Intrinsic::x86_sse41_ptestnzc: | |||
26444 | case Intrinsic::x86_avx_ptestz_256: | |||
26445 | case Intrinsic::x86_avx_ptestc_256: | |||
26446 | case Intrinsic::x86_avx_ptestnzc_256: | |||
26447 | case Intrinsic::x86_avx_vtestz_ps: | |||
26448 | case Intrinsic::x86_avx_vtestc_ps: | |||
26449 | case Intrinsic::x86_avx_vtestnzc_ps: | |||
26450 | case Intrinsic::x86_avx_vtestz_pd: | |||
26451 | case Intrinsic::x86_avx_vtestc_pd: | |||
26452 | case Intrinsic::x86_avx_vtestnzc_pd: | |||
26453 | case Intrinsic::x86_avx_vtestz_ps_256: | |||
26454 | case Intrinsic::x86_avx_vtestc_ps_256: | |||
26455 | case Intrinsic::x86_avx_vtestnzc_ps_256: | |||
26456 | case Intrinsic::x86_avx_vtestz_pd_256: | |||
26457 | case Intrinsic::x86_avx_vtestc_pd_256: | |||
26458 | case Intrinsic::x86_avx_vtestnzc_pd_256: { | |||
26459 | unsigned TestOpc = X86ISD::PTEST; | |||
26460 | X86::CondCode X86CC; | |||
26461 | switch (IntNo) { | |||
26462 | default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.")::llvm::llvm_unreachable_internal("Bad fallthrough in Intrinsic lowering." , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 26462); | |||
26463 | case Intrinsic::x86_avx512_ktestc_b: | |||
26464 | case Intrinsic::x86_avx512_ktestc_w: | |||
26465 | case Intrinsic::x86_avx512_ktestc_d: | |||
26466 | case Intrinsic::x86_avx512_ktestc_q: | |||
26467 | // CF = 1 | |||
26468 | TestOpc = X86ISD::KTEST; | |||
26469 | X86CC = X86::COND_B; | |||
26470 | break; | |||
26471 | case Intrinsic::x86_avx512_ktestz_b: | |||
26472 | case Intrinsic::x86_avx512_ktestz_w: | |||
26473 | case Intrinsic::x86_avx512_ktestz_d: | |||
26474 | case Intrinsic::x86_avx512_ktestz_q: | |||
26475 | TestOpc = X86ISD::KTEST; | |||
26476 | X86CC = X86::COND_E; | |||
26477 | break; | |||
26478 | case Intrinsic::x86_avx_vtestz_ps: | |||
26479 | case Intrinsic::x86_avx_vtestz_pd: | |||
26480 | case Intrinsic::x86_avx_vtestz_ps_256: | |||
26481 | case Intrinsic::x86_avx_vtestz_pd_256: | |||
26482 | TestOpc = X86ISD::TESTP; | |||
26483 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
26484 | case Intrinsic::x86_sse41_ptestz: | |||
26485 | case Intrinsic::x86_avx_ptestz_256: | |||
26486 | // ZF = 1 | |||
26487 | X86CC = X86::COND_E; | |||
26488 | break; | |||
26489 | case Intrinsic::x86_avx_vtestc_ps: | |||
26490 | case Intrinsic::x86_avx_vtestc_pd: | |||
26491 | case Intrinsic::x86_avx_vtestc_ps_256: | |||
26492 | case Intrinsic::x86_avx_vtestc_pd_256: | |||
26493 | TestOpc = X86ISD::TESTP; | |||
26494 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
26495 | case Intrinsic::x86_sse41_ptestc: | |||
26496 | case Intrinsic::x86_avx_ptestc_256: | |||
26497 | // CF = 1 | |||
26498 | X86CC = X86::COND_B; | |||
26499 | break; | |||
26500 | case Intrinsic::x86_avx_vtestnzc_ps: | |||
26501 | case Intrinsic::x86_avx_vtestnzc_pd: | |||
26502 | case Intrinsic::x86_avx_vtestnzc_ps_256: | |||
26503 | case Intrinsic::x86_avx_vtestnzc_pd_256: | |||
26504 | TestOpc = X86ISD::TESTP; | |||
26505 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
26506 | case Intrinsic::x86_sse41_ptestnzc: | |||
26507 | case Intrinsic::x86_avx_ptestnzc_256: | |||
26508 | // ZF and CF = 0 | |||
26509 | X86CC = X86::COND_A; | |||
26510 | break; | |||
26511 | } | |||
26512 | ||||
26513 | SDValue LHS = Op.getOperand(1); | |||
26514 | SDValue RHS = Op.getOperand(2); | |||
26515 | SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS); | |||
26516 | SDValue SetCC = getSETCC(X86CC, Test, dl, DAG); | |||
26517 | return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC); | |||
26518 | } | |||
26519 | ||||
26520 | case Intrinsic::x86_sse42_pcmpistria128: | |||
26521 | case Intrinsic::x86_sse42_pcmpestria128: | |||
26522 | case Intrinsic::x86_sse42_pcmpistric128: | |||
26523 | case Intrinsic::x86_sse42_pcmpestric128: | |||
26524 | case Intrinsic::x86_sse42_pcmpistrio128: | |||
26525 | case Intrinsic::x86_sse42_pcmpestrio128: | |||
26526 | case Intrinsic::x86_sse42_pcmpistris128: | |||
26527 | case Intrinsic::x86_sse42_pcmpestris128: | |||
26528 | case Intrinsic::x86_sse42_pcmpistriz128: | |||
26529 | case Intrinsic::x86_sse42_pcmpestriz128: { | |||
26530 | unsigned Opcode; | |||
26531 | X86::CondCode X86CC; | |||
26532 | switch (IntNo) { | |||
26533 | default: llvm_unreachable("Impossible intrinsic")::llvm::llvm_unreachable_internal("Impossible intrinsic", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 26533); // Can't reach here. | |||
26534 | case Intrinsic::x86_sse42_pcmpistria128: | |||
26535 | Opcode = X86ISD::PCMPISTR; | |||
26536 | X86CC = X86::COND_A; | |||
26537 | break; | |||
26538 | case Intrinsic::x86_sse42_pcmpestria128: | |||
26539 | Opcode = X86ISD::PCMPESTR; | |||
26540 | X86CC = X86::COND_A; | |||
26541 | break; | |||
26542 | case Intrinsic::x86_sse42_pcmpistric128: | |||
26543 | Opcode = X86ISD::PCMPISTR; | |||
26544 | X86CC = X86::COND_B; | |||
26545 | break; | |||
26546 | case Intrinsic::x86_sse42_pcmpestric128: | |||
26547 | Opcode = X86ISD::PCMPESTR; | |||
26548 | X86CC = X86::COND_B; | |||
26549 | break; | |||
26550 | case Intrinsic::x86_sse42_pcmpistrio128: | |||
26551 | Opcode = X86ISD::PCMPISTR; | |||
26552 | X86CC = X86::COND_O; | |||
26553 | break; | |||
26554 | case Intrinsic::x86_sse42_pcmpestrio128: | |||
26555 | Opcode = X86ISD::PCMPESTR; | |||
26556 | X86CC = X86::COND_O; | |||
26557 | break; | |||
26558 | case Intrinsic::x86_sse42_pcmpistris128: | |||
26559 | Opcode = X86ISD::PCMPISTR; | |||
26560 | X86CC = X86::COND_S; | |||
26561 | break; | |||
26562 | case Intrinsic::x86_sse42_pcmpestris128: | |||
26563 | Opcode = X86ISD::PCMPESTR; | |||
26564 | X86CC = X86::COND_S; | |||
26565 | break; | |||
26566 | case Intrinsic::x86_sse42_pcmpistriz128: | |||
26567 | Opcode = X86ISD::PCMPISTR; | |||
26568 | X86CC = X86::COND_E; | |||
26569 | break; | |||
26570 | case Intrinsic::x86_sse42_pcmpestriz128: | |||
26571 | Opcode = X86ISD::PCMPESTR; | |||
26572 | X86CC = X86::COND_E; | |||
26573 | break; | |||
26574 | } | |||
26575 | SmallVector<SDValue, 5> NewOps(Op->op_begin()+1, Op->op_end()); | |||
26576 | SDVTList VTs = DAG.getVTList(MVT::i32, MVT::v16i8, MVT::i32); | |||
26577 | SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps).getValue(2); | |||
26578 | SDValue SetCC = getSETCC(X86CC, PCMP, dl, DAG); | |||
26579 | return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC); | |||
26580 | } | |||
26581 | ||||
26582 | case Intrinsic::x86_sse42_pcmpistri128: | |||
26583 | case Intrinsic::x86_sse42_pcmpestri128: { | |||
26584 | unsigned Opcode; | |||
26585 | if (IntNo == Intrinsic::x86_sse42_pcmpistri128) | |||
26586 | Opcode = X86ISD::PCMPISTR; | |||
26587 | else | |||
26588 | Opcode = X86ISD::PCMPESTR; | |||
26589 | ||||
26590 | SmallVector<SDValue, 5> NewOps(Op->op_begin()+1, Op->op_end()); | |||
26591 | SDVTList VTs = DAG.getVTList(MVT::i32, MVT::v16i8, MVT::i32); | |||
26592 | return DAG.getNode(Opcode, dl, VTs, NewOps); | |||
26593 | } | |||
26594 | ||||
26595 | case Intrinsic::x86_sse42_pcmpistrm128: | |||
26596 | case Intrinsic::x86_sse42_pcmpestrm128: { | |||
26597 | unsigned Opcode; | |||
26598 | if (IntNo == Intrinsic::x86_sse42_pcmpistrm128) | |||
26599 | Opcode = X86ISD::PCMPISTR; | |||
26600 | else | |||
26601 | Opcode = X86ISD::PCMPESTR; | |||
26602 | ||||
26603 | SmallVector<SDValue, 5> NewOps(Op->op_begin()+1, Op->op_end()); | |||
26604 | SDVTList VTs = DAG.getVTList(MVT::i32, MVT::v16i8, MVT::i32); | |||
26605 | return DAG.getNode(Opcode, dl, VTs, NewOps).getValue(1); | |||
26606 | } | |||
26607 | ||||
26608 | case Intrinsic::eh_sjlj_lsda: { | |||
26609 | MachineFunction &MF = DAG.getMachineFunction(); | |||
26610 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
26611 | MVT PtrVT = TLI.getPointerTy(DAG.getDataLayout()); | |||
26612 | auto &Context = MF.getMMI().getContext(); | |||
26613 | MCSymbol *S = Context.getOrCreateSymbol(Twine("GCC_except_table") + | |||
26614 | Twine(MF.getFunctionNumber())); | |||
26615 | return DAG.getNode(getGlobalWrapperKind(), dl, VT, | |||
26616 | DAG.getMCSymbol(S, PtrVT)); | |||
26617 | } | |||
26618 | ||||
26619 | case Intrinsic::x86_seh_lsda: { | |||
26620 | // Compute the symbol for the LSDA. We know it'll get emitted later. | |||
26621 | MachineFunction &MF = DAG.getMachineFunction(); | |||
26622 | SDValue Op1 = Op.getOperand(1); | |||
26623 | auto *Fn = cast<Function>(cast<GlobalAddressSDNode>(Op1)->getGlobal()); | |||
26624 | MCSymbol *LSDASym = MF.getMMI().getContext().getOrCreateLSDASymbol( | |||
26625 | GlobalValue::dropLLVMManglingEscape(Fn->getName())); | |||
26626 | ||||
26627 | // Generate a simple absolute symbol reference. This intrinsic is only | |||
26628 | // supported on 32-bit Windows, which isn't PIC. | |||
26629 | SDValue Result = DAG.getMCSymbol(LSDASym, VT); | |||
26630 | return DAG.getNode(X86ISD::Wrapper, dl, VT, Result); | |||
26631 | } | |||
26632 | ||||
26633 | case Intrinsic::eh_recoverfp: { | |||
26634 | SDValue FnOp = Op.getOperand(1); | |||
26635 | SDValue IncomingFPOp = Op.getOperand(2); | |||
26636 | GlobalAddressSDNode *GSD = dyn_cast<GlobalAddressSDNode>(FnOp); | |||
26637 | auto *Fn = dyn_cast_or_null<Function>(GSD ? GSD->getGlobal() : nullptr); | |||
26638 | if (!Fn) | |||
26639 | report_fatal_error( | |||
26640 | "llvm.eh.recoverfp must take a function as the first argument"); | |||
26641 | return recoverFramePointer(DAG, Fn, IncomingFPOp); | |||
26642 | } | |||
26643 | ||||
26644 | case Intrinsic::localaddress: { | |||
26645 | // Returns one of the stack, base, or frame pointer registers, depending on | |||
26646 | // which is used to reference local variables. | |||
26647 | MachineFunction &MF = DAG.getMachineFunction(); | |||
26648 | const X86RegisterInfo *RegInfo = Subtarget.getRegisterInfo(); | |||
26649 | unsigned Reg; | |||
26650 | if (RegInfo->hasBasePointer(MF)) | |||
26651 | Reg = RegInfo->getBaseRegister(); | |||
26652 | else { // Handles the SP or FP case. | |||
26653 | bool CantUseFP = RegInfo->hasStackRealignment(MF); | |||
26654 | if (CantUseFP) | |||
26655 | Reg = RegInfo->getPtrSizedStackRegister(MF); | |||
26656 | else | |||
26657 | Reg = RegInfo->getPtrSizedFrameRegister(MF); | |||
26658 | } | |||
26659 | return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT); | |||
26660 | } | |||
26661 | case Intrinsic::swift_async_context_addr: { | |||
26662 | auto &MF = DAG.getMachineFunction(); | |||
26663 | auto X86FI = MF.getInfo<X86MachineFunctionInfo>(); | |||
26664 | if (Subtarget.is64Bit()) { | |||
26665 | MF.getFrameInfo().setFrameAddressIsTaken(true); | |||
26666 | X86FI->setHasSwiftAsyncContext(true); | |||
26667 | return SDValue( | |||
26668 | DAG.getMachineNode( | |||
26669 | X86::SUB64ri8, dl, MVT::i64, | |||
26670 | DAG.getCopyFromReg(DAG.getEntryNode(), dl, X86::RBP, MVT::i64), | |||
26671 | DAG.getTargetConstant(8, dl, MVT::i32)), | |||
26672 | 0); | |||
26673 | } else { | |||
26674 | // 32-bit so no special extended frame, create or reuse an existing stack | |||
26675 | // slot. | |||
26676 | if (!X86FI->getSwiftAsyncContextFrameIdx()) | |||
26677 | X86FI->setSwiftAsyncContextFrameIdx( | |||
26678 | MF.getFrameInfo().CreateStackObject(4, Align(4), false)); | |||
26679 | return DAG.getFrameIndex(*X86FI->getSwiftAsyncContextFrameIdx(), MVT::i32); | |||
26680 | } | |||
26681 | } | |||
26682 | case Intrinsic::x86_avx512_vp2intersect_q_512: | |||
26683 | case Intrinsic::x86_avx512_vp2intersect_q_256: | |||
26684 | case Intrinsic::x86_avx512_vp2intersect_q_128: | |||
26685 | case Intrinsic::x86_avx512_vp2intersect_d_512: | |||
26686 | case Intrinsic::x86_avx512_vp2intersect_d_256: | |||
26687 | case Intrinsic::x86_avx512_vp2intersect_d_128: { | |||
26688 | MVT MaskVT = Op.getSimpleValueType(); | |||
26689 | ||||
26690 | SDVTList VTs = DAG.getVTList(MVT::Untyped, MVT::Other); | |||
26691 | SDLoc DL(Op); | |||
26692 | ||||
26693 | SDValue Operation = | |||
26694 | DAG.getNode(X86ISD::VP2INTERSECT, DL, VTs, | |||
26695 | Op->getOperand(1), Op->getOperand(2)); | |||
26696 | ||||
26697 | SDValue Result0 = DAG.getTargetExtractSubreg(X86::sub_mask_0, DL, | |||
26698 | MaskVT, Operation); | |||
26699 | SDValue Result1 = DAG.getTargetExtractSubreg(X86::sub_mask_1, DL, | |||
26700 | MaskVT, Operation); | |||
26701 | return DAG.getMergeValues({Result0, Result1}, DL); | |||
26702 | } | |||
26703 | case Intrinsic::x86_mmx_pslli_w: | |||
26704 | case Intrinsic::x86_mmx_pslli_d: | |||
26705 | case Intrinsic::x86_mmx_pslli_q: | |||
26706 | case Intrinsic::x86_mmx_psrli_w: | |||
26707 | case Intrinsic::x86_mmx_psrli_d: | |||
26708 | case Intrinsic::x86_mmx_psrli_q: | |||
26709 | case Intrinsic::x86_mmx_psrai_w: | |||
26710 | case Intrinsic::x86_mmx_psrai_d: { | |||
26711 | SDLoc DL(Op); | |||
26712 | SDValue ShAmt = Op.getOperand(2); | |||
26713 | // If the argument is a constant, convert it to a target constant. | |||
26714 | if (auto *C = dyn_cast<ConstantSDNode>(ShAmt)) { | |||
26715 | // Clamp out of bounds shift amounts since they will otherwise be masked | |||
26716 | // to 8-bits which may make it no longer out of bounds. | |||
26717 | unsigned ShiftAmount = C->getAPIntValue().getLimitedValue(255); | |||
26718 | if (ShiftAmount == 0) | |||
26719 | return Op.getOperand(1); | |||
26720 | ||||
26721 | return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, Op.getValueType(), | |||
26722 | Op.getOperand(0), Op.getOperand(1), | |||
26723 | DAG.getTargetConstant(ShiftAmount, DL, MVT::i32)); | |||
26724 | } | |||
26725 | ||||
26726 | unsigned NewIntrinsic; | |||
26727 | switch (IntNo) { | |||
26728 | default: llvm_unreachable("Impossible intrinsic")::llvm::llvm_unreachable_internal("Impossible intrinsic", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 26728); // Can't reach here. | |||
26729 | case Intrinsic::x86_mmx_pslli_w: | |||
26730 | NewIntrinsic = Intrinsic::x86_mmx_psll_w; | |||
26731 | break; | |||
26732 | case Intrinsic::x86_mmx_pslli_d: | |||
26733 | NewIntrinsic = Intrinsic::x86_mmx_psll_d; | |||
26734 | break; | |||
26735 | case Intrinsic::x86_mmx_pslli_q: | |||
26736 | NewIntrinsic = Intrinsic::x86_mmx_psll_q; | |||
26737 | break; | |||
26738 | case Intrinsic::x86_mmx_psrli_w: | |||
26739 | NewIntrinsic = Intrinsic::x86_mmx_psrl_w; | |||
26740 | break; | |||
26741 | case Intrinsic::x86_mmx_psrli_d: | |||
26742 | NewIntrinsic = Intrinsic::x86_mmx_psrl_d; | |||
26743 | break; | |||
26744 | case Intrinsic::x86_mmx_psrli_q: | |||
26745 | NewIntrinsic = Intrinsic::x86_mmx_psrl_q; | |||
26746 | break; | |||
26747 | case Intrinsic::x86_mmx_psrai_w: | |||
26748 | NewIntrinsic = Intrinsic::x86_mmx_psra_w; | |||
26749 | break; | |||
26750 | case Intrinsic::x86_mmx_psrai_d: | |||
26751 | NewIntrinsic = Intrinsic::x86_mmx_psra_d; | |||
26752 | break; | |||
26753 | } | |||
26754 | ||||
26755 | // The vector shift intrinsics with scalars uses 32b shift amounts but | |||
26756 | // the sse2/mmx shift instructions reads 64 bits. Copy the 32 bits to an | |||
26757 | // MMX register. | |||
26758 | ShAmt = DAG.getNode(X86ISD::MMX_MOVW2D, DL, MVT::x86mmx, ShAmt); | |||
26759 | return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, Op.getValueType(), | |||
26760 | DAG.getTargetConstant(NewIntrinsic, DL, | |||
26761 | getPointerTy(DAG.getDataLayout())), | |||
26762 | Op.getOperand(1), ShAmt); | |||
26763 | } | |||
26764 | case Intrinsic::thread_pointer: { | |||
26765 | if (Subtarget.isTargetELF()) { | |||
26766 | SDLoc dl(Op); | |||
26767 | EVT PtrVT = getPointerTy(DAG.getDataLayout()); | |||
26768 | // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit). | |||
26769 | Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy( | |||
26770 | *DAG.getContext(), Subtarget.is64Bit() ? X86AS::FS : X86AS::GS)); | |||
26771 | return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), | |||
26772 | DAG.getIntPtrConstant(0, dl), MachinePointerInfo(Ptr)); | |||
26773 | } | |||
26774 | report_fatal_error( | |||
26775 | "Target OS doesn't support __builtin_thread_pointer() yet."); | |||
26776 | } | |||
26777 | } | |||
26778 | } | |||
26779 | ||||
26780 | static SDValue getAVX2GatherNode(unsigned Opc, SDValue Op, SelectionDAG &DAG, | |||
26781 | SDValue Src, SDValue Mask, SDValue Base, | |||
26782 | SDValue Index, SDValue ScaleOp, SDValue Chain, | |||
26783 | const X86Subtarget &Subtarget) { | |||
26784 | SDLoc dl(Op); | |||
26785 | auto *C = dyn_cast<ConstantSDNode>(ScaleOp); | |||
26786 | // Scale must be constant. | |||
26787 | if (!C) | |||
26788 | return SDValue(); | |||
26789 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
26790 | SDValue Scale = DAG.getTargetConstant(C->getZExtValue(), dl, | |||
26791 | TLI.getPointerTy(DAG.getDataLayout())); | |||
26792 | EVT MaskVT = Mask.getValueType().changeVectorElementTypeToInteger(); | |||
26793 | SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Other); | |||
26794 | // If source is undef or we know it won't be used, use a zero vector | |||
26795 | // to break register dependency. | |||
26796 | // TODO: use undef instead and let BreakFalseDeps deal with it? | |||
26797 | if (Src.isUndef() || ISD::isBuildVectorAllOnes(Mask.getNode())) | |||
26798 | Src = getZeroVector(Op.getSimpleValueType(), Subtarget, DAG, dl); | |||
26799 | ||||
26800 | // Cast mask to an integer type. | |||
26801 | Mask = DAG.getBitcast(MaskVT, Mask); | |||
26802 | ||||
26803 | MemIntrinsicSDNode *MemIntr = cast<MemIntrinsicSDNode>(Op); | |||
26804 | ||||
26805 | SDValue Ops[] = {Chain, Src, Mask, Base, Index, Scale }; | |||
26806 | SDValue Res = | |||
26807 | DAG.getMemIntrinsicNode(X86ISD::MGATHER, dl, VTs, Ops, | |||
26808 | MemIntr->getMemoryVT(), MemIntr->getMemOperand()); | |||
26809 | return DAG.getMergeValues({Res, Res.getValue(1)}, dl); | |||
26810 | } | |||
26811 | ||||
26812 | static SDValue getGatherNode(SDValue Op, SelectionDAG &DAG, | |||
26813 | SDValue Src, SDValue Mask, SDValue Base, | |||
26814 | SDValue Index, SDValue ScaleOp, SDValue Chain, | |||
26815 | const X86Subtarget &Subtarget) { | |||
26816 | MVT VT = Op.getSimpleValueType(); | |||
26817 | SDLoc dl(Op); | |||
26818 | auto *C = dyn_cast<ConstantSDNode>(ScaleOp); | |||
26819 | // Scale must be constant. | |||
26820 | if (!C) | |||
26821 | return SDValue(); | |||
26822 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
26823 | SDValue Scale = DAG.getTargetConstant(C->getZExtValue(), dl, | |||
26824 | TLI.getPointerTy(DAG.getDataLayout())); | |||
26825 | unsigned MinElts = std::min(Index.getSimpleValueType().getVectorNumElements(), | |||
26826 | VT.getVectorNumElements()); | |||
26827 | MVT MaskVT = MVT::getVectorVT(MVT::i1, MinElts); | |||
26828 | ||||
26829 | // We support two versions of the gather intrinsics. One with scalar mask and | |||
26830 | // one with vXi1 mask. Convert scalar to vXi1 if necessary. | |||
26831 | if (Mask.getValueType() != MaskVT) | |||
26832 | Mask = getMaskNode(Mask, MaskVT, Subtarget, DAG, dl); | |||
26833 | ||||
26834 | SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Other); | |||
26835 | // If source is undef or we know it won't be used, use a zero vector | |||
26836 | // to break register dependency. | |||
26837 | // TODO: use undef instead and let BreakFalseDeps deal with it? | |||
26838 | if (Src.isUndef() || ISD::isBuildVectorAllOnes(Mask.getNode())) | |||
26839 | Src = getZeroVector(Op.getSimpleValueType(), Subtarget, DAG, dl); | |||
26840 | ||||
26841 | MemIntrinsicSDNode *MemIntr = cast<MemIntrinsicSDNode>(Op); | |||
26842 | ||||
26843 | SDValue Ops[] = {Chain, Src, Mask, Base, Index, Scale }; | |||
26844 | SDValue Res = | |||
26845 | DAG.getMemIntrinsicNode(X86ISD::MGATHER, dl, VTs, Ops, | |||
26846 | MemIntr->getMemoryVT(), MemIntr->getMemOperand()); | |||
26847 | return DAG.getMergeValues({Res, Res.getValue(1)}, dl); | |||
26848 | } | |||
26849 | ||||
26850 | static SDValue getScatterNode(unsigned Opc, SDValue Op, SelectionDAG &DAG, | |||
26851 | SDValue Src, SDValue Mask, SDValue Base, | |||
26852 | SDValue Index, SDValue ScaleOp, SDValue Chain, | |||
26853 | const X86Subtarget &Subtarget) { | |||
26854 | SDLoc dl(Op); | |||
26855 | auto *C = dyn_cast<ConstantSDNode>(ScaleOp); | |||
26856 | // Scale must be constant. | |||
26857 | if (!C) | |||
26858 | return SDValue(); | |||
26859 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
26860 | SDValue Scale = DAG.getTargetConstant(C->getZExtValue(), dl, | |||
26861 | TLI.getPointerTy(DAG.getDataLayout())); | |||
26862 | unsigned MinElts = std::min(Index.getSimpleValueType().getVectorNumElements(), | |||
26863 | Src.getSimpleValueType().getVectorNumElements()); | |||
26864 | MVT MaskVT = MVT::getVectorVT(MVT::i1, MinElts); | |||
26865 | ||||
26866 | // We support two versions of the scatter intrinsics. One with scalar mask and | |||
26867 | // one with vXi1 mask. Convert scalar to vXi1 if necessary. | |||
26868 | if (Mask.getValueType() != MaskVT) | |||
26869 | Mask = getMaskNode(Mask, MaskVT, Subtarget, DAG, dl); | |||
26870 | ||||
26871 | MemIntrinsicSDNode *MemIntr = cast<MemIntrinsicSDNode>(Op); | |||
26872 | ||||
26873 | SDVTList VTs = DAG.getVTList(MVT::Other); | |||
26874 | SDValue Ops[] = {Chain, Src, Mask, Base, Index, Scale}; | |||
26875 | SDValue Res = | |||
26876 | DAG.getMemIntrinsicNode(X86ISD::MSCATTER, dl, VTs, Ops, | |||
26877 | MemIntr->getMemoryVT(), MemIntr->getMemOperand()); | |||
26878 | return Res; | |||
26879 | } | |||
26880 | ||||
26881 | static SDValue getPrefetchNode(unsigned Opc, SDValue Op, SelectionDAG &DAG, | |||
26882 | SDValue Mask, SDValue Base, SDValue Index, | |||
26883 | SDValue ScaleOp, SDValue Chain, | |||
26884 | const X86Subtarget &Subtarget) { | |||
26885 | SDLoc dl(Op); | |||
26886 | auto *C = dyn_cast<ConstantSDNode>(ScaleOp); | |||
26887 | // Scale must be constant. | |||
26888 | if (!C) | |||
26889 | return SDValue(); | |||
26890 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
26891 | SDValue Scale = DAG.getTargetConstant(C->getZExtValue(), dl, | |||
26892 | TLI.getPointerTy(DAG.getDataLayout())); | |||
26893 | SDValue Disp = DAG.getTargetConstant(0, dl, MVT::i32); | |||
26894 | SDValue Segment = DAG.getRegister(0, MVT::i32); | |||
26895 | MVT MaskVT = | |||
26896 | MVT::getVectorVT(MVT::i1, Index.getSimpleValueType().getVectorNumElements()); | |||
26897 | SDValue VMask = getMaskNode(Mask, MaskVT, Subtarget, DAG, dl); | |||
26898 | SDValue Ops[] = {VMask, Base, Scale, Index, Disp, Segment, Chain}; | |||
26899 | SDNode *Res = DAG.getMachineNode(Opc, dl, MVT::Other, Ops); | |||
26900 | return SDValue(Res, 0); | |||
26901 | } | |||
26902 | ||||
26903 | /// Handles the lowering of builtin intrinsics with chain that return their | |||
26904 | /// value into registers EDX:EAX. | |||
26905 | /// If operand ScrReg is a valid register identifier, then operand 2 of N is | |||
26906 | /// copied to SrcReg. The assumption is that SrcReg is an implicit input to | |||
26907 | /// TargetOpcode. | |||
26908 | /// Returns a Glue value which can be used to add extra copy-from-reg if the | |||
26909 | /// expanded intrinsics implicitly defines extra registers (i.e. not just | |||
26910 | /// EDX:EAX). | |||
26911 | static SDValue expandIntrinsicWChainHelper(SDNode *N, const SDLoc &DL, | |||
26912 | SelectionDAG &DAG, | |||
26913 | unsigned TargetOpcode, | |||
26914 | unsigned SrcReg, | |||
26915 | const X86Subtarget &Subtarget, | |||
26916 | SmallVectorImpl<SDValue> &Results) { | |||
26917 | SDValue Chain = N->getOperand(0); | |||
26918 | SDValue Glue; | |||
26919 | ||||
26920 | if (SrcReg) { | |||
26921 | assert(N->getNumOperands() == 3 && "Unexpected number of operands!")(static_cast <bool> (N->getNumOperands() == 3 && "Unexpected number of operands!") ? void (0) : __assert_fail ("N->getNumOperands() == 3 && \"Unexpected number of operands!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 26921, __extension__ __PRETTY_FUNCTION__)); | |||
26922 | Chain = DAG.getCopyToReg(Chain, DL, SrcReg, N->getOperand(2), Glue); | |||
26923 | Glue = Chain.getValue(1); | |||
26924 | } | |||
26925 | ||||
26926 | SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue); | |||
26927 | SDValue N1Ops[] = {Chain, Glue}; | |||
26928 | SDNode *N1 = DAG.getMachineNode( | |||
26929 | TargetOpcode, DL, Tys, ArrayRef<SDValue>(N1Ops, Glue.getNode() ? 2 : 1)); | |||
26930 | Chain = SDValue(N1, 0); | |||
26931 | ||||
26932 | // Reads the content of XCR and returns it in registers EDX:EAX. | |||
26933 | SDValue LO, HI; | |||
26934 | if (Subtarget.is64Bit()) { | |||
26935 | LO = DAG.getCopyFromReg(Chain, DL, X86::RAX, MVT::i64, SDValue(N1, 1)); | |||
26936 | HI = DAG.getCopyFromReg(LO.getValue(1), DL, X86::RDX, MVT::i64, | |||
26937 | LO.getValue(2)); | |||
26938 | } else { | |||
26939 | LO = DAG.getCopyFromReg(Chain, DL, X86::EAX, MVT::i32, SDValue(N1, 1)); | |||
26940 | HI = DAG.getCopyFromReg(LO.getValue(1), DL, X86::EDX, MVT::i32, | |||
26941 | LO.getValue(2)); | |||
26942 | } | |||
26943 | Chain = HI.getValue(1); | |||
26944 | Glue = HI.getValue(2); | |||
26945 | ||||
26946 | if (Subtarget.is64Bit()) { | |||
26947 | // Merge the two 32-bit values into a 64-bit one. | |||
26948 | SDValue Tmp = DAG.getNode(ISD::SHL, DL, MVT::i64, HI, | |||
26949 | DAG.getConstant(32, DL, MVT::i8)); | |||
26950 | Results.push_back(DAG.getNode(ISD::OR, DL, MVT::i64, LO, Tmp)); | |||
26951 | Results.push_back(Chain); | |||
26952 | return Glue; | |||
26953 | } | |||
26954 | ||||
26955 | // Use a buildpair to merge the two 32-bit values into a 64-bit one. | |||
26956 | SDValue Ops[] = { LO, HI }; | |||
26957 | SDValue Pair = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops); | |||
26958 | Results.push_back(Pair); | |||
26959 | Results.push_back(Chain); | |||
26960 | return Glue; | |||
26961 | } | |||
26962 | ||||
26963 | /// Handles the lowering of builtin intrinsics that read the time stamp counter | |||
26964 | /// (x86_rdtsc and x86_rdtscp). This function is also used to custom lower | |||
26965 | /// READCYCLECOUNTER nodes. | |||
26966 | static void getReadTimeStampCounter(SDNode *N, const SDLoc &DL, unsigned Opcode, | |||
26967 | SelectionDAG &DAG, | |||
26968 | const X86Subtarget &Subtarget, | |||
26969 | SmallVectorImpl<SDValue> &Results) { | |||
26970 | // The processor's time-stamp counter (a 64-bit MSR) is stored into the | |||
26971 | // EDX:EAX registers. EDX is loaded with the high-order 32 bits of the MSR | |||
26972 | // and the EAX register is loaded with the low-order 32 bits. | |||
26973 | SDValue Glue = expandIntrinsicWChainHelper(N, DL, DAG, Opcode, | |||
26974 | /* NoRegister */0, Subtarget, | |||
26975 | Results); | |||
26976 | if (Opcode != X86::RDTSCP) | |||
26977 | return; | |||
26978 | ||||
26979 | SDValue Chain = Results[1]; | |||
26980 | // Instruction RDTSCP loads the IA32:TSC_AUX_MSR (address C000_0103H) into | |||
26981 | // the ECX register. Add 'ecx' explicitly to the chain. | |||
26982 | SDValue ecx = DAG.getCopyFromReg(Chain, DL, X86::ECX, MVT::i32, Glue); | |||
26983 | Results[1] = ecx; | |||
26984 | Results.push_back(ecx.getValue(1)); | |||
26985 | } | |||
26986 | ||||
26987 | static SDValue LowerREADCYCLECOUNTER(SDValue Op, const X86Subtarget &Subtarget, | |||
26988 | SelectionDAG &DAG) { | |||
26989 | SmallVector<SDValue, 3> Results; | |||
26990 | SDLoc DL(Op); | |||
26991 | getReadTimeStampCounter(Op.getNode(), DL, X86::RDTSC, DAG, Subtarget, | |||
26992 | Results); | |||
26993 | return DAG.getMergeValues(Results, DL); | |||
26994 | } | |||
26995 | ||||
26996 | static SDValue MarkEHRegistrationNode(SDValue Op, SelectionDAG &DAG) { | |||
26997 | MachineFunction &MF = DAG.getMachineFunction(); | |||
26998 | SDValue Chain = Op.getOperand(0); | |||
26999 | SDValue RegNode = Op.getOperand(2); | |||
27000 | WinEHFuncInfo *EHInfo = MF.getWinEHFuncInfo(); | |||
27001 | if (!EHInfo) | |||
27002 | report_fatal_error("EH registrations only live in functions using WinEH"); | |||
27003 | ||||
27004 | // Cast the operand to an alloca, and remember the frame index. | |||
27005 | auto *FINode = dyn_cast<FrameIndexSDNode>(RegNode); | |||
27006 | if (!FINode) | |||
27007 | report_fatal_error("llvm.x86.seh.ehregnode expects a static alloca"); | |||
27008 | EHInfo->EHRegNodeFrameIndex = FINode->getIndex(); | |||
27009 | ||||
27010 | // Return the chain operand without making any DAG nodes. | |||
27011 | return Chain; | |||
27012 | } | |||
27013 | ||||
27014 | static SDValue MarkEHGuard(SDValue Op, SelectionDAG &DAG) { | |||
27015 | MachineFunction &MF = DAG.getMachineFunction(); | |||
27016 | SDValue Chain = Op.getOperand(0); | |||
27017 | SDValue EHGuard = Op.getOperand(2); | |||
27018 | WinEHFuncInfo *EHInfo = MF.getWinEHFuncInfo(); | |||
27019 | if (!EHInfo) | |||
27020 | report_fatal_error("EHGuard only live in functions using WinEH"); | |||
27021 | ||||
27022 | // Cast the operand to an alloca, and remember the frame index. | |||
27023 | auto *FINode = dyn_cast<FrameIndexSDNode>(EHGuard); | |||
27024 | if (!FINode) | |||
27025 | report_fatal_error("llvm.x86.seh.ehguard expects a static alloca"); | |||
27026 | EHInfo->EHGuardFrameIndex = FINode->getIndex(); | |||
27027 | ||||
27028 | // Return the chain operand without making any DAG nodes. | |||
27029 | return Chain; | |||
27030 | } | |||
27031 | ||||
27032 | /// Emit Truncating Store with signed or unsigned saturation. | |||
27033 | static SDValue | |||
27034 | EmitTruncSStore(bool SignedSat, SDValue Chain, const SDLoc &Dl, SDValue Val, | |||
27035 | SDValue Ptr, EVT MemVT, MachineMemOperand *MMO, | |||
27036 | SelectionDAG &DAG) { | |||
27037 | SDVTList VTs = DAG.getVTList(MVT::Other); | |||
27038 | SDValue Undef = DAG.getUNDEF(Ptr.getValueType()); | |||
27039 | SDValue Ops[] = { Chain, Val, Ptr, Undef }; | |||
27040 | unsigned Opc = SignedSat ? X86ISD::VTRUNCSTORES : X86ISD::VTRUNCSTOREUS; | |||
27041 | return DAG.getMemIntrinsicNode(Opc, Dl, VTs, Ops, MemVT, MMO); | |||
27042 | } | |||
27043 | ||||
27044 | /// Emit Masked Truncating Store with signed or unsigned saturation. | |||
27045 | static SDValue | |||
27046 | EmitMaskedTruncSStore(bool SignedSat, SDValue Chain, const SDLoc &Dl, | |||
27047 | SDValue Val, SDValue Ptr, SDValue Mask, EVT MemVT, | |||
27048 | MachineMemOperand *MMO, SelectionDAG &DAG) { | |||
27049 | SDVTList VTs = DAG.getVTList(MVT::Other); | |||
27050 | SDValue Ops[] = { Chain, Val, Ptr, Mask }; | |||
27051 | unsigned Opc = SignedSat ? X86ISD::VMTRUNCSTORES : X86ISD::VMTRUNCSTOREUS; | |||
27052 | return DAG.getMemIntrinsicNode(Opc, Dl, VTs, Ops, MemVT, MMO); | |||
27053 | } | |||
27054 | ||||
27055 | static SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, const X86Subtarget &Subtarget, | |||
27056 | SelectionDAG &DAG) { | |||
27057 | unsigned IntNo = Op.getConstantOperandVal(1); | |||
27058 | const IntrinsicData *IntrData = getIntrinsicWithChain(IntNo); | |||
27059 | if (!IntrData) { | |||
27060 | switch (IntNo) { | |||
27061 | case llvm::Intrinsic::x86_seh_ehregnode: | |||
27062 | return MarkEHRegistrationNode(Op, DAG); | |||
27063 | case llvm::Intrinsic::x86_seh_ehguard: | |||
27064 | return MarkEHGuard(Op, DAG); | |||
27065 | case llvm::Intrinsic::x86_rdpkru: { | |||
27066 | SDLoc dl(Op); | |||
27067 | SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other); | |||
27068 | // Create a RDPKRU node and pass 0 to the ECX parameter. | |||
27069 | return DAG.getNode(X86ISD::RDPKRU, dl, VTs, Op.getOperand(0), | |||
27070 | DAG.getConstant(0, dl, MVT::i32)); | |||
27071 | } | |||
27072 | case llvm::Intrinsic::x86_wrpkru: { | |||
27073 | SDLoc dl(Op); | |||
27074 | // Create a WRPKRU node, pass the input to the EAX parameter, and pass 0 | |||
27075 | // to the EDX and ECX parameters. | |||
27076 | return DAG.getNode(X86ISD::WRPKRU, dl, MVT::Other, | |||
27077 | Op.getOperand(0), Op.getOperand(2), | |||
27078 | DAG.getConstant(0, dl, MVT::i32), | |||
27079 | DAG.getConstant(0, dl, MVT::i32)); | |||
27080 | } | |||
27081 | case llvm::Intrinsic::asan_check_memaccess: { | |||
27082 | // Mark this as adjustsStack because it will be lowered to a call. | |||
27083 | DAG.getMachineFunction().getFrameInfo().setAdjustsStack(true); | |||
27084 | // Don't do anything here, we will expand these intrinsics out later. | |||
27085 | return Op; | |||
27086 | } | |||
27087 | case llvm::Intrinsic::x86_flags_read_u32: | |||
27088 | case llvm::Intrinsic::x86_flags_read_u64: | |||
27089 | case llvm::Intrinsic::x86_flags_write_u32: | |||
27090 | case llvm::Intrinsic::x86_flags_write_u64: { | |||
27091 | // We need a frame pointer because this will get lowered to a PUSH/POP | |||
27092 | // sequence. | |||
27093 | MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); | |||
27094 | MFI.setHasCopyImplyingStackAdjustment(true); | |||
27095 | // Don't do anything here, we will expand these intrinsics out later | |||
27096 | // during FinalizeISel in EmitInstrWithCustomInserter. | |||
27097 | return Op; | |||
27098 | } | |||
27099 | case Intrinsic::x86_lwpins32: | |||
27100 | case Intrinsic::x86_lwpins64: | |||
27101 | case Intrinsic::x86_umwait: | |||
27102 | case Intrinsic::x86_tpause: { | |||
27103 | SDLoc dl(Op); | |||
27104 | SDValue Chain = Op->getOperand(0); | |||
27105 | SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other); | |||
27106 | unsigned Opcode; | |||
27107 | ||||
27108 | switch (IntNo) { | |||
27109 | default: llvm_unreachable("Impossible intrinsic")::llvm::llvm_unreachable_internal("Impossible intrinsic", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 27109); | |||
27110 | case Intrinsic::x86_umwait: | |||
27111 | Opcode = X86ISD::UMWAIT; | |||
27112 | break; | |||
27113 | case Intrinsic::x86_tpause: | |||
27114 | Opcode = X86ISD::TPAUSE; | |||
27115 | break; | |||
27116 | case Intrinsic::x86_lwpins32: | |||
27117 | case Intrinsic::x86_lwpins64: | |||
27118 | Opcode = X86ISD::LWPINS; | |||
27119 | break; | |||
27120 | } | |||
27121 | ||||
27122 | SDValue Operation = | |||
27123 | DAG.getNode(Opcode, dl, VTs, Chain, Op->getOperand(2), | |||
27124 | Op->getOperand(3), Op->getOperand(4)); | |||
27125 | SDValue SetCC = getSETCC(X86::COND_B, Operation.getValue(0), dl, DAG); | |||
27126 | return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), SetCC, | |||
27127 | Operation.getValue(1)); | |||
27128 | } | |||
27129 | case Intrinsic::x86_enqcmd: | |||
27130 | case Intrinsic::x86_enqcmds: { | |||
27131 | SDLoc dl(Op); | |||
27132 | SDValue Chain = Op.getOperand(0); | |||
27133 | SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other); | |||
27134 | unsigned Opcode; | |||
27135 | switch (IntNo) { | |||
27136 | default: llvm_unreachable("Impossible intrinsic!")::llvm::llvm_unreachable_internal("Impossible intrinsic!", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 27136); | |||
27137 | case Intrinsic::x86_enqcmd: | |||
27138 | Opcode = X86ISD::ENQCMD; | |||
27139 | break; | |||
27140 | case Intrinsic::x86_enqcmds: | |||
27141 | Opcode = X86ISD::ENQCMDS; | |||
27142 | break; | |||
27143 | } | |||
27144 | SDValue Operation = DAG.getNode(Opcode, dl, VTs, Chain, Op.getOperand(2), | |||
27145 | Op.getOperand(3)); | |||
27146 | SDValue SetCC = getSETCC(X86::COND_E, Operation.getValue(0), dl, DAG); | |||
27147 | return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), SetCC, | |||
27148 | Operation.getValue(1)); | |||
27149 | } | |||
27150 | case Intrinsic::x86_aesenc128kl: | |||
27151 | case Intrinsic::x86_aesdec128kl: | |||
27152 | case Intrinsic::x86_aesenc256kl: | |||
27153 | case Intrinsic::x86_aesdec256kl: { | |||
27154 | SDLoc DL(Op); | |||
27155 | SDVTList VTs = DAG.getVTList(MVT::v2i64, MVT::i32, MVT::Other); | |||
27156 | SDValue Chain = Op.getOperand(0); | |||
27157 | unsigned Opcode; | |||
27158 | ||||
27159 | switch (IntNo) { | |||
27160 | default: llvm_unreachable("Impossible intrinsic")::llvm::llvm_unreachable_internal("Impossible intrinsic", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 27160); | |||
27161 | case Intrinsic::x86_aesenc128kl: | |||
27162 | Opcode = X86ISD::AESENC128KL; | |||
27163 | break; | |||
27164 | case Intrinsic::x86_aesdec128kl: | |||
27165 | Opcode = X86ISD::AESDEC128KL; | |||
27166 | break; | |||
27167 | case Intrinsic::x86_aesenc256kl: | |||
27168 | Opcode = X86ISD::AESENC256KL; | |||
27169 | break; | |||
27170 | case Intrinsic::x86_aesdec256kl: | |||
27171 | Opcode = X86ISD::AESDEC256KL; | |||
27172 | break; | |||
27173 | } | |||
27174 | ||||
27175 | MemIntrinsicSDNode *MemIntr = cast<MemIntrinsicSDNode>(Op); | |||
27176 | MachineMemOperand *MMO = MemIntr->getMemOperand(); | |||
27177 | EVT MemVT = MemIntr->getMemoryVT(); | |||
27178 | SDValue Operation = DAG.getMemIntrinsicNode( | |||
27179 | Opcode, DL, VTs, {Chain, Op.getOperand(2), Op.getOperand(3)}, MemVT, | |||
27180 | MMO); | |||
27181 | SDValue ZF = getSETCC(X86::COND_E, Operation.getValue(1), DL, DAG); | |||
27182 | ||||
27183 | return DAG.getNode(ISD::MERGE_VALUES, DL, Op->getVTList(), | |||
27184 | {ZF, Operation.getValue(0), Operation.getValue(2)}); | |||
27185 | } | |||
27186 | case Intrinsic::x86_aesencwide128kl: | |||
27187 | case Intrinsic::x86_aesdecwide128kl: | |||
27188 | case Intrinsic::x86_aesencwide256kl: | |||
27189 | case Intrinsic::x86_aesdecwide256kl: { | |||
27190 | SDLoc DL(Op); | |||
27191 | SDVTList VTs = DAG.getVTList( | |||
27192 | {MVT::i32, MVT::v2i64, MVT::v2i64, MVT::v2i64, MVT::v2i64, MVT::v2i64, | |||
27193 | MVT::v2i64, MVT::v2i64, MVT::v2i64, MVT::Other}); | |||
27194 | SDValue Chain = Op.getOperand(0); | |||
27195 | unsigned Opcode; | |||
27196 | ||||
27197 | switch (IntNo) { | |||
27198 | default: llvm_unreachable("Impossible intrinsic")::llvm::llvm_unreachable_internal("Impossible intrinsic", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 27198); | |||
27199 | case Intrinsic::x86_aesencwide128kl: | |||
27200 | Opcode = X86ISD::AESENCWIDE128KL; | |||
27201 | break; | |||
27202 | case Intrinsic::x86_aesdecwide128kl: | |||
27203 | Opcode = X86ISD::AESDECWIDE128KL; | |||
27204 | break; | |||
27205 | case Intrinsic::x86_aesencwide256kl: | |||
27206 | Opcode = X86ISD::AESENCWIDE256KL; | |||
27207 | break; | |||
27208 | case Intrinsic::x86_aesdecwide256kl: | |||
27209 | Opcode = X86ISD::AESDECWIDE256KL; | |||
27210 | break; | |||
27211 | } | |||
27212 | ||||
27213 | MemIntrinsicSDNode *MemIntr = cast<MemIntrinsicSDNode>(Op); | |||
27214 | MachineMemOperand *MMO = MemIntr->getMemOperand(); | |||
27215 | EVT MemVT = MemIntr->getMemoryVT(); | |||
27216 | SDValue Operation = DAG.getMemIntrinsicNode( | |||
27217 | Opcode, DL, VTs, | |||
27218 | {Chain, Op.getOperand(2), Op.getOperand(3), Op.getOperand(4), | |||
27219 | Op.getOperand(5), Op.getOperand(6), Op.getOperand(7), | |||
27220 | Op.getOperand(8), Op.getOperand(9), Op.getOperand(10)}, | |||
27221 | MemVT, MMO); | |||
27222 | SDValue ZF = getSETCC(X86::COND_E, Operation.getValue(0), DL, DAG); | |||
27223 | ||||
27224 | return DAG.getNode(ISD::MERGE_VALUES, DL, Op->getVTList(), | |||
27225 | {ZF, Operation.getValue(1), Operation.getValue(2), | |||
27226 | Operation.getValue(3), Operation.getValue(4), | |||
27227 | Operation.getValue(5), Operation.getValue(6), | |||
27228 | Operation.getValue(7), Operation.getValue(8), | |||
27229 | Operation.getValue(9)}); | |||
27230 | } | |||
27231 | case Intrinsic::x86_testui: { | |||
27232 | SDLoc dl(Op); | |||
27233 | SDValue Chain = Op.getOperand(0); | |||
27234 | SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other); | |||
27235 | SDValue Operation = DAG.getNode(X86ISD::TESTUI, dl, VTs, Chain); | |||
27236 | SDValue SetCC = getSETCC(X86::COND_B, Operation.getValue(0), dl, DAG); | |||
27237 | return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), SetCC, | |||
27238 | Operation.getValue(1)); | |||
27239 | } | |||
27240 | } | |||
27241 | return SDValue(); | |||
27242 | } | |||
27243 | ||||
27244 | SDLoc dl(Op); | |||
27245 | switch(IntrData->Type) { | |||
27246 | default: llvm_unreachable("Unknown Intrinsic Type")::llvm::llvm_unreachable_internal("Unknown Intrinsic Type", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 27246); | |||
27247 | case RDSEED: | |||
27248 | case RDRAND: { | |||
27249 | // Emit the node with the right value type. | |||
27250 | SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::i32, MVT::Other); | |||
27251 | SDValue Result = DAG.getNode(IntrData->Opc0, dl, VTs, Op.getOperand(0)); | |||
27252 | ||||
27253 | // If the value returned by RDRAND/RDSEED was valid (CF=1), return 1. | |||
27254 | // Otherwise return the value from Rand, which is always 0, casted to i32. | |||
27255 | SDValue Ops[] = {DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)), | |||
27256 | DAG.getConstant(1, dl, Op->getValueType(1)), | |||
27257 | DAG.getTargetConstant(X86::COND_B, dl, MVT::i8), | |||
27258 | SDValue(Result.getNode(), 1)}; | |||
27259 | SDValue isValid = DAG.getNode(X86ISD::CMOV, dl, Op->getValueType(1), Ops); | |||
27260 | ||||
27261 | // Return { result, isValid, chain }. | |||
27262 | return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid, | |||
27263 | SDValue(Result.getNode(), 2)); | |||
27264 | } | |||
27265 | case GATHER_AVX2: { | |||
27266 | SDValue Chain = Op.getOperand(0); | |||
27267 | SDValue Src = Op.getOperand(2); | |||
27268 | SDValue Base = Op.getOperand(3); | |||
27269 | SDValue Index = Op.getOperand(4); | |||
27270 | SDValue Mask = Op.getOperand(5); | |||
27271 | SDValue Scale = Op.getOperand(6); | |||
27272 | return getAVX2GatherNode(IntrData->Opc0, Op, DAG, Src, Mask, Base, Index, | |||
27273 | Scale, Chain, Subtarget); | |||
27274 | } | |||
27275 | case GATHER: { | |||
27276 | //gather(v1, mask, index, base, scale); | |||
27277 | SDValue Chain = Op.getOperand(0); | |||
27278 | SDValue Src = Op.getOperand(2); | |||
27279 | SDValue Base = Op.getOperand(3); | |||
27280 | SDValue Index = Op.getOperand(4); | |||
27281 | SDValue Mask = Op.getOperand(5); | |||
27282 | SDValue Scale = Op.getOperand(6); | |||
27283 | return getGatherNode(Op, DAG, Src, Mask, Base, Index, Scale, | |||
27284 | Chain, Subtarget); | |||
27285 | } | |||
27286 | case SCATTER: { | |||
27287 | //scatter(base, mask, index, v1, scale); | |||
27288 | SDValue Chain = Op.getOperand(0); | |||
27289 | SDValue Base = Op.getOperand(2); | |||
27290 | SDValue Mask = Op.getOperand(3); | |||
27291 | SDValue Index = Op.getOperand(4); | |||
27292 | SDValue Src = Op.getOperand(5); | |||
27293 | SDValue Scale = Op.getOperand(6); | |||
27294 | return getScatterNode(IntrData->Opc0, Op, DAG, Src, Mask, Base, Index, | |||
27295 | Scale, Chain, Subtarget); | |||
27296 | } | |||
27297 | case PREFETCH: { | |||
27298 | const APInt &HintVal = Op.getConstantOperandAPInt(6); | |||
27299 | assert((HintVal == 2 || HintVal == 3) &&(static_cast <bool> ((HintVal == 2 || HintVal == 3) && "Wrong prefetch hint in intrinsic: should be 2 or 3") ? void (0) : __assert_fail ("(HintVal == 2 || HintVal == 3) && \"Wrong prefetch hint in intrinsic: should be 2 or 3\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 27300, __extension__ __PRETTY_FUNCTION__)) | |||
27300 | "Wrong prefetch hint in intrinsic: should be 2 or 3")(static_cast <bool> ((HintVal == 2 || HintVal == 3) && "Wrong prefetch hint in intrinsic: should be 2 or 3") ? void (0) : __assert_fail ("(HintVal == 2 || HintVal == 3) && \"Wrong prefetch hint in intrinsic: should be 2 or 3\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 27300, __extension__ __PRETTY_FUNCTION__)); | |||
27301 | unsigned Opcode = (HintVal == 2 ? IntrData->Opc1 : IntrData->Opc0); | |||
27302 | SDValue Chain = Op.getOperand(0); | |||
27303 | SDValue Mask = Op.getOperand(2); | |||
27304 | SDValue Index = Op.getOperand(3); | |||
27305 | SDValue Base = Op.getOperand(4); | |||
27306 | SDValue Scale = Op.getOperand(5); | |||
27307 | return getPrefetchNode(Opcode, Op, DAG, Mask, Base, Index, Scale, Chain, | |||
27308 | Subtarget); | |||
27309 | } | |||
27310 | // Read Time Stamp Counter (RDTSC) and Processor ID (RDTSCP). | |||
27311 | case RDTSC: { | |||
27312 | SmallVector<SDValue, 2> Results; | |||
27313 | getReadTimeStampCounter(Op.getNode(), dl, IntrData->Opc0, DAG, Subtarget, | |||
27314 | Results); | |||
27315 | return DAG.getMergeValues(Results, dl); | |||
27316 | } | |||
27317 | // Read Performance Monitoring Counters. | |||
27318 | case RDPMC: | |||
27319 | // GetExtended Control Register. | |||
27320 | case XGETBV: { | |||
27321 | SmallVector<SDValue, 2> Results; | |||
27322 | ||||
27323 | // RDPMC uses ECX to select the index of the performance counter to read. | |||
27324 | // XGETBV uses ECX to select the index of the XCR register to return. | |||
27325 | // The result is stored into registers EDX:EAX. | |||
27326 | expandIntrinsicWChainHelper(Op.getNode(), dl, DAG, IntrData->Opc0, X86::ECX, | |||
27327 | Subtarget, Results); | |||
27328 | return DAG.getMergeValues(Results, dl); | |||
27329 | } | |||
27330 | // XTEST intrinsics. | |||
27331 | case XTEST: { | |||
27332 | SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Other); | |||
27333 | SDValue InTrans = DAG.getNode(IntrData->Opc0, dl, VTs, Op.getOperand(0)); | |||
27334 | ||||
27335 | SDValue SetCC = getSETCC(X86::COND_NE, InTrans, dl, DAG); | |||
27336 | SDValue Ret = DAG.getNode(ISD::ZERO_EXTEND, dl, Op->getValueType(0), SetCC); | |||
27337 | return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), | |||
27338 | Ret, SDValue(InTrans.getNode(), 1)); | |||
27339 | } | |||
27340 | case TRUNCATE_TO_MEM_VI8: | |||
27341 | case TRUNCATE_TO_MEM_VI16: | |||
27342 | case TRUNCATE_TO_MEM_VI32: { | |||
27343 | SDValue Mask = Op.getOperand(4); | |||
27344 | SDValue DataToTruncate = Op.getOperand(3); | |||
27345 | SDValue Addr = Op.getOperand(2); | |||
27346 | SDValue Chain = Op.getOperand(0); | |||
27347 | ||||
27348 | MemIntrinsicSDNode *MemIntr = dyn_cast<MemIntrinsicSDNode>(Op); | |||
27349 | assert(MemIntr && "Expected MemIntrinsicSDNode!")(static_cast <bool> (MemIntr && "Expected MemIntrinsicSDNode!" ) ? void (0) : __assert_fail ("MemIntr && \"Expected MemIntrinsicSDNode!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 27349, __extension__ __PRETTY_FUNCTION__)); | |||
27350 | ||||
27351 | EVT MemVT = MemIntr->getMemoryVT(); | |||
27352 | ||||
27353 | uint16_t TruncationOp = IntrData->Opc0; | |||
27354 | switch (TruncationOp) { | |||
27355 | case X86ISD::VTRUNC: { | |||
27356 | if (isAllOnesConstant(Mask)) // return just a truncate store | |||
27357 | return DAG.getTruncStore(Chain, dl, DataToTruncate, Addr, MemVT, | |||
27358 | MemIntr->getMemOperand()); | |||
27359 | ||||
27360 | MVT MaskVT = MVT::getVectorVT(MVT::i1, MemVT.getVectorNumElements()); | |||
27361 | SDValue VMask = getMaskNode(Mask, MaskVT, Subtarget, DAG, dl); | |||
27362 | SDValue Offset = DAG.getUNDEF(VMask.getValueType()); | |||
27363 | ||||
27364 | return DAG.getMaskedStore(Chain, dl, DataToTruncate, Addr, Offset, VMask, | |||
27365 | MemVT, MemIntr->getMemOperand(), ISD::UNINDEXED, | |||
27366 | true /* truncating */); | |||
27367 | } | |||
27368 | case X86ISD::VTRUNCUS: | |||
27369 | case X86ISD::VTRUNCS: { | |||
27370 | bool IsSigned = (TruncationOp == X86ISD::VTRUNCS); | |||
27371 | if (isAllOnesConstant(Mask)) | |||
27372 | return EmitTruncSStore(IsSigned, Chain, dl, DataToTruncate, Addr, MemVT, | |||
27373 | MemIntr->getMemOperand(), DAG); | |||
27374 | ||||
27375 | MVT MaskVT = MVT::getVectorVT(MVT::i1, MemVT.getVectorNumElements()); | |||
27376 | SDValue VMask = getMaskNode(Mask, MaskVT, Subtarget, DAG, dl); | |||
27377 | ||||
27378 | return EmitMaskedTruncSStore(IsSigned, Chain, dl, DataToTruncate, Addr, | |||
27379 | VMask, MemVT, MemIntr->getMemOperand(), DAG); | |||
27380 | } | |||
27381 | default: | |||
27382 | llvm_unreachable("Unsupported truncstore intrinsic")::llvm::llvm_unreachable_internal("Unsupported truncstore intrinsic" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 27382); | |||
27383 | } | |||
27384 | } | |||
27385 | } | |||
27386 | } | |||
27387 | ||||
27388 | SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, | |||
27389 | SelectionDAG &DAG) const { | |||
27390 | MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); | |||
27391 | MFI.setReturnAddressIsTaken(true); | |||
27392 | ||||
27393 | if (verifyReturnAddressArgumentIsConstant(Op, DAG)) | |||
27394 | return SDValue(); | |||
27395 | ||||
27396 | unsigned Depth = Op.getConstantOperandVal(0); | |||
27397 | SDLoc dl(Op); | |||
27398 | EVT PtrVT = getPointerTy(DAG.getDataLayout()); | |||
27399 | ||||
27400 | if (Depth > 0) { | |||
27401 | SDValue FrameAddr = LowerFRAMEADDR(Op, DAG); | |||
27402 | const X86RegisterInfo *RegInfo = Subtarget.getRegisterInfo(); | |||
27403 | SDValue Offset = DAG.getConstant(RegInfo->getSlotSize(), dl, PtrVT); | |||
27404 | return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), | |||
27405 | DAG.getNode(ISD::ADD, dl, PtrVT, FrameAddr, Offset), | |||
27406 | MachinePointerInfo()); | |||
27407 | } | |||
27408 | ||||
27409 | // Just load the return address. | |||
27410 | SDValue RetAddrFI = getReturnAddressFrameIndex(DAG); | |||
27411 | return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), RetAddrFI, | |||
27412 | MachinePointerInfo()); | |||
27413 | } | |||
27414 | ||||
27415 | SDValue X86TargetLowering::LowerADDROFRETURNADDR(SDValue Op, | |||
27416 | SelectionDAG &DAG) const { | |||
27417 | DAG.getMachineFunction().getFrameInfo().setReturnAddressIsTaken(true); | |||
27418 | return getReturnAddressFrameIndex(DAG); | |||
27419 | } | |||
27420 | ||||
27421 | SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const { | |||
27422 | MachineFunction &MF = DAG.getMachineFunction(); | |||
27423 | MachineFrameInfo &MFI = MF.getFrameInfo(); | |||
27424 | X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>(); | |||
27425 | const X86RegisterInfo *RegInfo = Subtarget.getRegisterInfo(); | |||
27426 | EVT VT = Op.getValueType(); | |||
27427 | ||||
27428 | MFI.setFrameAddressIsTaken(true); | |||
27429 | ||||
27430 | if (MF.getTarget().getMCAsmInfo()->usesWindowsCFI()) { | |||
27431 | // Depth > 0 makes no sense on targets which use Windows unwind codes. It | |||
27432 | // is not possible to crawl up the stack without looking at the unwind codes | |||
27433 | // simultaneously. | |||
27434 | int FrameAddrIndex = FuncInfo->getFAIndex(); | |||
27435 | if (!FrameAddrIndex) { | |||
27436 | // Set up a frame object for the return address. | |||
27437 | unsigned SlotSize = RegInfo->getSlotSize(); | |||
27438 | FrameAddrIndex = MF.getFrameInfo().CreateFixedObject( | |||
27439 | SlotSize, /*SPOffset=*/0, /*IsImmutable=*/false); | |||
27440 | FuncInfo->setFAIndex(FrameAddrIndex); | |||
27441 | } | |||
27442 | return DAG.getFrameIndex(FrameAddrIndex, VT); | |||
27443 | } | |||
27444 | ||||
27445 | unsigned FrameReg = | |||
27446 | RegInfo->getPtrSizedFrameRegister(DAG.getMachineFunction()); | |||
27447 | SDLoc dl(Op); // FIXME probably not meaningful | |||
27448 | unsigned Depth = Op.getConstantOperandVal(0); | |||
27449 | assert(((FrameReg == X86::RBP && VT == MVT::i64) ||(static_cast <bool> (((FrameReg == X86::RBP && VT == MVT::i64) || (FrameReg == X86::EBP && VT == MVT:: i32)) && "Invalid Frame Register!") ? void (0) : __assert_fail ("((FrameReg == X86::RBP && VT == MVT::i64) || (FrameReg == X86::EBP && VT == MVT::i32)) && \"Invalid Frame Register!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 27451, __extension__ __PRETTY_FUNCTION__)) | |||
27450 | (FrameReg == X86::EBP && VT == MVT::i32)) &&(static_cast <bool> (((FrameReg == X86::RBP && VT == MVT::i64) || (FrameReg == X86::EBP && VT == MVT:: i32)) && "Invalid Frame Register!") ? void (0) : __assert_fail ("((FrameReg == X86::RBP && VT == MVT::i64) || (FrameReg == X86::EBP && VT == MVT::i32)) && \"Invalid Frame Register!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 27451, __extension__ __PRETTY_FUNCTION__)) | |||
27451 | "Invalid Frame Register!")(static_cast <bool> (((FrameReg == X86::RBP && VT == MVT::i64) || (FrameReg == X86::EBP && VT == MVT:: i32)) && "Invalid Frame Register!") ? void (0) : __assert_fail ("((FrameReg == X86::RBP && VT == MVT::i64) || (FrameReg == X86::EBP && VT == MVT::i32)) && \"Invalid Frame Register!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 27451, __extension__ __PRETTY_FUNCTION__)); | |||
27452 | SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT); | |||
27453 | while (Depth--) | |||
27454 | FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, | |||
27455 | MachinePointerInfo()); | |||
27456 | return FrameAddr; | |||
27457 | } | |||
27458 | ||||
27459 | // FIXME? Maybe this could be a TableGen attribute on some registers and | |||
27460 | // this table could be generated automatically from RegInfo. | |||
27461 | Register X86TargetLowering::getRegisterByName(const char* RegName, LLT VT, | |||
27462 | const MachineFunction &MF) const { | |||
27463 | const TargetFrameLowering &TFI = *Subtarget.getFrameLowering(); | |||
27464 | ||||
27465 | Register Reg = StringSwitch<unsigned>(RegName) | |||
27466 | .Case("esp", X86::ESP) | |||
27467 | .Case("rsp", X86::RSP) | |||
27468 | .Case("ebp", X86::EBP) | |||
27469 | .Case("rbp", X86::RBP) | |||
27470 | .Default(0); | |||
27471 | ||||
27472 | if (Reg == X86::EBP || Reg == X86::RBP) { | |||
27473 | if (!TFI.hasFP(MF)) | |||
27474 | report_fatal_error("register " + StringRef(RegName) + | |||
27475 | " is allocatable: function has no frame pointer"); | |||
27476 | #ifndef NDEBUG | |||
27477 | else { | |||
27478 | const X86RegisterInfo *RegInfo = Subtarget.getRegisterInfo(); | |||
27479 | Register FrameReg = RegInfo->getPtrSizedFrameRegister(MF); | |||
27480 | assert((FrameReg == X86::EBP || FrameReg == X86::RBP) &&(static_cast <bool> ((FrameReg == X86::EBP || FrameReg == X86::RBP) && "Invalid Frame Register!") ? void (0) : __assert_fail ("(FrameReg == X86::EBP || FrameReg == X86::RBP) && \"Invalid Frame Register!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 27481, __extension__ __PRETTY_FUNCTION__)) | |||
27481 | "Invalid Frame Register!")(static_cast <bool> ((FrameReg == X86::EBP || FrameReg == X86::RBP) && "Invalid Frame Register!") ? void (0) : __assert_fail ("(FrameReg == X86::EBP || FrameReg == X86::RBP) && \"Invalid Frame Register!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 27481, __extension__ __PRETTY_FUNCTION__)); | |||
27482 | } | |||
27483 | #endif | |||
27484 | } | |||
27485 | ||||
27486 | if (Reg) | |||
27487 | return Reg; | |||
27488 | ||||
27489 | report_fatal_error("Invalid register name global variable"); | |||
27490 | } | |||
27491 | ||||
27492 | SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op, | |||
27493 | SelectionDAG &DAG) const { | |||
27494 | const X86RegisterInfo *RegInfo = Subtarget.getRegisterInfo(); | |||
27495 | return DAG.getIntPtrConstant(2 * RegInfo->getSlotSize(), SDLoc(Op)); | |||
27496 | } | |||
27497 | ||||
27498 | Register X86TargetLowering::getExceptionPointerRegister( | |||
27499 | const Constant *PersonalityFn) const { | |||
27500 | if (classifyEHPersonality(PersonalityFn) == EHPersonality::CoreCLR) | |||
27501 | return Subtarget.isTarget64BitLP64() ? X86::RDX : X86::EDX; | |||
27502 | ||||
27503 | return Subtarget.isTarget64BitLP64() ? X86::RAX : X86::EAX; | |||
27504 | } | |||
27505 | ||||
27506 | Register X86TargetLowering::getExceptionSelectorRegister( | |||
27507 | const Constant *PersonalityFn) const { | |||
27508 | // Funclet personalities don't use selectors (the runtime does the selection). | |||
27509 | if (isFuncletEHPersonality(classifyEHPersonality(PersonalityFn))) | |||
27510 | return X86::NoRegister; | |||
27511 | return Subtarget.isTarget64BitLP64() ? X86::RDX : X86::EDX; | |||
27512 | } | |||
27513 | ||||
27514 | bool X86TargetLowering::needsFixedCatchObjects() const { | |||
27515 | return Subtarget.isTargetWin64(); | |||
27516 | } | |||
27517 | ||||
27518 | SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const { | |||
27519 | SDValue Chain = Op.getOperand(0); | |||
27520 | SDValue Offset = Op.getOperand(1); | |||
27521 | SDValue Handler = Op.getOperand(2); | |||
27522 | SDLoc dl (Op); | |||
27523 | ||||
27524 | EVT PtrVT = getPointerTy(DAG.getDataLayout()); | |||
27525 | const X86RegisterInfo *RegInfo = Subtarget.getRegisterInfo(); | |||
27526 | Register FrameReg = RegInfo->getFrameRegister(DAG.getMachineFunction()); | |||
27527 | assert(((FrameReg == X86::RBP && PtrVT == MVT::i64) ||(static_cast <bool> (((FrameReg == X86::RBP && PtrVT == MVT::i64) || (FrameReg == X86::EBP && PtrVT == MVT ::i32)) && "Invalid Frame Register!") ? void (0) : __assert_fail ("((FrameReg == X86::RBP && PtrVT == MVT::i64) || (FrameReg == X86::EBP && PtrVT == MVT::i32)) && \"Invalid Frame Register!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 27529, __extension__ __PRETTY_FUNCTION__)) | |||
27528 | (FrameReg == X86::EBP && PtrVT == MVT::i32)) &&(static_cast <bool> (((FrameReg == X86::RBP && PtrVT == MVT::i64) || (FrameReg == X86::EBP && PtrVT == MVT ::i32)) && "Invalid Frame Register!") ? void (0) : __assert_fail ("((FrameReg == X86::RBP && PtrVT == MVT::i64) || (FrameReg == X86::EBP && PtrVT == MVT::i32)) && \"Invalid Frame Register!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 27529, __extension__ __PRETTY_FUNCTION__)) | |||
27529 | "Invalid Frame Register!")(static_cast <bool> (((FrameReg == X86::RBP && PtrVT == MVT::i64) || (FrameReg == X86::EBP && PtrVT == MVT ::i32)) && "Invalid Frame Register!") ? void (0) : __assert_fail ("((FrameReg == X86::RBP && PtrVT == MVT::i64) || (FrameReg == X86::EBP && PtrVT == MVT::i32)) && \"Invalid Frame Register!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 27529, __extension__ __PRETTY_FUNCTION__)); | |||
27530 | SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, PtrVT); | |||
27531 | Register StoreAddrReg = (PtrVT == MVT::i64) ? X86::RCX : X86::ECX; | |||
27532 | ||||
27533 | SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, PtrVT, Frame, | |||
27534 | DAG.getIntPtrConstant(RegInfo->getSlotSize(), | |||
27535 | dl)); | |||
27536 | StoreAddr = DAG.getNode(ISD::ADD, dl, PtrVT, StoreAddr, Offset); | |||
27537 | Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo()); | |||
27538 | Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr); | |||
27539 | ||||
27540 | return DAG.getNode(X86ISD::EH_RETURN, dl, MVT::Other, Chain, | |||
27541 | DAG.getRegister(StoreAddrReg, PtrVT)); | |||
27542 | } | |||
27543 | ||||
27544 | SDValue X86TargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op, | |||
27545 | SelectionDAG &DAG) const { | |||
27546 | SDLoc DL(Op); | |||
27547 | // If the subtarget is not 64bit, we may need the global base reg | |||
27548 | // after isel expand pseudo, i.e., after CGBR pass ran. | |||
27549 | // Therefore, ask for the GlobalBaseReg now, so that the pass | |||
27550 | // inserts the code for us in case we need it. | |||
27551 | // Otherwise, we will end up in a situation where we will | |||
27552 | // reference a virtual register that is not defined! | |||
27553 | if (!Subtarget.is64Bit()) { | |||
27554 | const X86InstrInfo *TII = Subtarget.getInstrInfo(); | |||
27555 | (void)TII->getGlobalBaseReg(&DAG.getMachineFunction()); | |||
27556 | } | |||
27557 | return DAG.getNode(X86ISD::EH_SJLJ_SETJMP, DL, | |||
27558 | DAG.getVTList(MVT::i32, MVT::Other), | |||
27559 | Op.getOperand(0), Op.getOperand(1)); | |||
27560 | } | |||
27561 | ||||
27562 | SDValue X86TargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op, | |||
27563 | SelectionDAG &DAG) const { | |||
27564 | SDLoc DL(Op); | |||
27565 | return DAG.getNode(X86ISD::EH_SJLJ_LONGJMP, DL, MVT::Other, | |||
27566 | Op.getOperand(0), Op.getOperand(1)); | |||
27567 | } | |||
27568 | ||||
27569 | SDValue X86TargetLowering::lowerEH_SJLJ_SETUP_DISPATCH(SDValue Op, | |||
27570 | SelectionDAG &DAG) const { | |||
27571 | SDLoc DL(Op); | |||
27572 | return DAG.getNode(X86ISD::EH_SJLJ_SETUP_DISPATCH, DL, MVT::Other, | |||
27573 | Op.getOperand(0)); | |||
27574 | } | |||
27575 | ||||
27576 | static SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) { | |||
27577 | return Op.getOperand(0); | |||
27578 | } | |||
27579 | ||||
27580 | SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op, | |||
27581 | SelectionDAG &DAG) const { | |||
27582 | SDValue Root = Op.getOperand(0); | |||
27583 | SDValue Trmp = Op.getOperand(1); // trampoline | |||
27584 | SDValue FPtr = Op.getOperand(2); // nested function | |||
27585 | SDValue Nest = Op.getOperand(3); // 'nest' parameter value | |||
27586 | SDLoc dl (Op); | |||
27587 | ||||
27588 | const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue(); | |||
27589 | const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo(); | |||
27590 | ||||
27591 | if (Subtarget.is64Bit()) { | |||
27592 | SDValue OutChains[6]; | |||
27593 | ||||
27594 | // Large code-model. | |||
27595 | const unsigned char JMP64r = 0xFF; // 64-bit jmp through register opcode. | |||
27596 | const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode. | |||
27597 | ||||
27598 | const unsigned char N86R10 = TRI->getEncodingValue(X86::R10) & 0x7; | |||
27599 | const unsigned char N86R11 = TRI->getEncodingValue(X86::R11) & 0x7; | |||
27600 | ||||
27601 | const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix | |||
27602 | ||||
27603 | // Load the pointer to the nested function into R11. | |||
27604 | unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11 | |||
27605 | SDValue Addr = Trmp; | |||
27606 | OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, dl, MVT::i16), | |||
27607 | Addr, MachinePointerInfo(TrmpAddr)); | |||
27608 | ||||
27609 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp, | |||
27610 | DAG.getConstant(2, dl, MVT::i64)); | |||
27611 | OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, | |||
27612 | MachinePointerInfo(TrmpAddr, 2), Align(2)); | |||
27613 | ||||
27614 | // Load the 'nest' parameter value into R10. | |||
27615 | // R10 is specified in X86CallingConv.td | |||
27616 | OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10 | |||
27617 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp, | |||
27618 | DAG.getConstant(10, dl, MVT::i64)); | |||
27619 | OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, dl, MVT::i16), | |||
27620 | Addr, MachinePointerInfo(TrmpAddr, 10)); | |||
27621 | ||||
27622 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp, | |||
27623 | DAG.getConstant(12, dl, MVT::i64)); | |||
27624 | OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, | |||
27625 | MachinePointerInfo(TrmpAddr, 12), Align(2)); | |||
27626 | ||||
27627 | // Jump to the nested function. | |||
27628 | OpCode = (JMP64r << 8) | REX_WB; // jmpq *... | |||
27629 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp, | |||
27630 | DAG.getConstant(20, dl, MVT::i64)); | |||
27631 | OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, dl, MVT::i16), | |||
27632 | Addr, MachinePointerInfo(TrmpAddr, 20)); | |||
27633 | ||||
27634 | unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11 | |||
27635 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp, | |||
27636 | DAG.getConstant(22, dl, MVT::i64)); | |||
27637 | OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, dl, MVT::i8), | |||
27638 | Addr, MachinePointerInfo(TrmpAddr, 22)); | |||
27639 | ||||
27640 | return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains); | |||
27641 | } else { | |||
27642 | const Function *Func = | |||
27643 | cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue()); | |||
27644 | CallingConv::ID CC = Func->getCallingConv(); | |||
27645 | unsigned NestReg; | |||
27646 | ||||
27647 | switch (CC) { | |||
27648 | default: | |||
27649 | llvm_unreachable("Unsupported calling convention")::llvm::llvm_unreachable_internal("Unsupported calling convention" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 27649); | |||
27650 | case CallingConv::C: | |||
27651 | case CallingConv::X86_StdCall: { | |||
27652 | // Pass 'nest' parameter in ECX. | |||
27653 | // Must be kept in sync with X86CallingConv.td | |||
27654 | NestReg = X86::ECX; | |||
27655 | ||||
27656 | // Check that ECX wasn't needed by an 'inreg' parameter. | |||
27657 | FunctionType *FTy = Func->getFunctionType(); | |||
27658 | const AttributeList &Attrs = Func->getAttributes(); | |||
27659 | ||||
27660 | if (!Attrs.isEmpty() && !Func->isVarArg()) { | |||
27661 | unsigned InRegCount = 0; | |||
27662 | unsigned Idx = 0; | |||
27663 | ||||
27664 | for (FunctionType::param_iterator I = FTy->param_begin(), | |||
27665 | E = FTy->param_end(); I != E; ++I, ++Idx) | |||
27666 | if (Attrs.hasParamAttr(Idx, Attribute::InReg)) { | |||
27667 | const DataLayout &DL = DAG.getDataLayout(); | |||
27668 | // FIXME: should only count parameters that are lowered to integers. | |||
27669 | InRegCount += (DL.getTypeSizeInBits(*I) + 31) / 32; | |||
27670 | } | |||
27671 | ||||
27672 | if (InRegCount > 2) { | |||
27673 | report_fatal_error("Nest register in use - reduce number of inreg" | |||
27674 | " parameters!"); | |||
27675 | } | |||
27676 | } | |||
27677 | break; | |||
27678 | } | |||
27679 | case CallingConv::X86_FastCall: | |||
27680 | case CallingConv::X86_ThisCall: | |||
27681 | case CallingConv::Fast: | |||
27682 | case CallingConv::Tail: | |||
27683 | case CallingConv::SwiftTail: | |||
27684 | // Pass 'nest' parameter in EAX. | |||
27685 | // Must be kept in sync with X86CallingConv.td | |||
27686 | NestReg = X86::EAX; | |||
27687 | break; | |||
27688 | } | |||
27689 | ||||
27690 | SDValue OutChains[4]; | |||
27691 | SDValue Addr, Disp; | |||
27692 | ||||
27693 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp, | |||
27694 | DAG.getConstant(10, dl, MVT::i32)); | |||
27695 | Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr); | |||
27696 | ||||
27697 | // This is storing the opcode for MOV32ri. | |||
27698 | const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte. | |||
27699 | const unsigned char N86Reg = TRI->getEncodingValue(NestReg) & 0x7; | |||
27700 | OutChains[0] = | |||
27701 | DAG.getStore(Root, dl, DAG.getConstant(MOV32ri | N86Reg, dl, MVT::i8), | |||
27702 | Trmp, MachinePointerInfo(TrmpAddr)); | |||
27703 | ||||
27704 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp, | |||
27705 | DAG.getConstant(1, dl, MVT::i32)); | |||
27706 | OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, | |||
27707 | MachinePointerInfo(TrmpAddr, 1), Align(1)); | |||
27708 | ||||
27709 | const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode. | |||
27710 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp, | |||
27711 | DAG.getConstant(5, dl, MVT::i32)); | |||
27712 | OutChains[2] = | |||
27713 | DAG.getStore(Root, dl, DAG.getConstant(JMP, dl, MVT::i8), Addr, | |||
27714 | MachinePointerInfo(TrmpAddr, 5), Align(1)); | |||
27715 | ||||
27716 | Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp, | |||
27717 | DAG.getConstant(6, dl, MVT::i32)); | |||
27718 | OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, | |||
27719 | MachinePointerInfo(TrmpAddr, 6), Align(1)); | |||
27720 | ||||
27721 | return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains); | |||
27722 | } | |||
27723 | } | |||
27724 | ||||
27725 | SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, | |||
27726 | SelectionDAG &DAG) const { | |||
27727 | /* | |||
27728 | The rounding mode is in bits 11:10 of FPSR, and has the following | |||
27729 | settings: | |||
27730 | 00 Round to nearest | |||
27731 | 01 Round to -inf | |||
27732 | 10 Round to +inf | |||
27733 | 11 Round to 0 | |||
27734 | ||||
27735 | FLT_ROUNDS, on the other hand, expects the following: | |||
27736 | -1 Undefined | |||
27737 | 0 Round to 0 | |||
27738 | 1 Round to nearest | |||
27739 | 2 Round to +inf | |||
27740 | 3 Round to -inf | |||
27741 | ||||
27742 | To perform the conversion, we use a packed lookup table of the four 2-bit | |||
27743 | values that we can index by FPSP[11:10] | |||
27744 | 0x2d --> (0b00,10,11,01) --> (0,2,3,1) >> FPSR[11:10] | |||
27745 | ||||
27746 | (0x2d >> ((FPSR & 0xc00) >> 9)) & 3 | |||
27747 | */ | |||
27748 | ||||
27749 | MachineFunction &MF = DAG.getMachineFunction(); | |||
27750 | MVT VT = Op.getSimpleValueType(); | |||
27751 | SDLoc DL(Op); | |||
27752 | ||||
27753 | // Save FP Control Word to stack slot | |||
27754 | int SSFI = MF.getFrameInfo().CreateStackObject(2, Align(2), false); | |||
27755 | SDValue StackSlot = | |||
27756 | DAG.getFrameIndex(SSFI, getPointerTy(DAG.getDataLayout())); | |||
27757 | ||||
27758 | MachinePointerInfo MPI = MachinePointerInfo::getFixedStack(MF, SSFI); | |||
27759 | ||||
27760 | SDValue Chain = Op.getOperand(0); | |||
27761 | SDValue Ops[] = {Chain, StackSlot}; | |||
27762 | Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL, | |||
27763 | DAG.getVTList(MVT::Other), Ops, MVT::i16, MPI, | |||
27764 | Align(2), MachineMemOperand::MOStore); | |||
27765 | ||||
27766 | // Load FP Control Word from stack slot | |||
27767 | SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot, MPI, Align(2)); | |||
27768 | Chain = CWD.getValue(1); | |||
27769 | ||||
27770 | // Mask and turn the control bits into a shift for the lookup table. | |||
27771 | SDValue Shift = | |||
27772 | DAG.getNode(ISD::SRL, DL, MVT::i16, | |||
27773 | DAG.getNode(ISD::AND, DL, MVT::i16, | |||
27774 | CWD, DAG.getConstant(0xc00, DL, MVT::i16)), | |||
27775 | DAG.getConstant(9, DL, MVT::i8)); | |||
27776 | Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, Shift); | |||
27777 | ||||
27778 | SDValue LUT = DAG.getConstant(0x2d, DL, MVT::i32); | |||
27779 | SDValue RetVal = | |||
27780 | DAG.getNode(ISD::AND, DL, MVT::i32, | |||
27781 | DAG.getNode(ISD::SRL, DL, MVT::i32, LUT, Shift), | |||
27782 | DAG.getConstant(3, DL, MVT::i32)); | |||
27783 | ||||
27784 | RetVal = DAG.getZExtOrTrunc(RetVal, DL, VT); | |||
27785 | ||||
27786 | return DAG.getMergeValues({RetVal, Chain}, DL); | |||
27787 | } | |||
27788 | ||||
27789 | SDValue X86TargetLowering::LowerSET_ROUNDING(SDValue Op, | |||
27790 | SelectionDAG &DAG) const { | |||
27791 | MachineFunction &MF = DAG.getMachineFunction(); | |||
27792 | SDLoc DL(Op); | |||
27793 | SDValue Chain = Op.getNode()->getOperand(0); | |||
27794 | ||||
27795 | // FP control word may be set only from data in memory. So we need to allocate | |||
27796 | // stack space to save/load FP control word. | |||
27797 | int OldCWFrameIdx = MF.getFrameInfo().CreateStackObject(4, Align(4), false); | |||
27798 | SDValue StackSlot = | |||
27799 | DAG.getFrameIndex(OldCWFrameIdx, getPointerTy(DAG.getDataLayout())); | |||
27800 | MachinePointerInfo MPI = MachinePointerInfo::getFixedStack(MF, OldCWFrameIdx); | |||
27801 | MachineMemOperand *MMO = | |||
27802 | MF.getMachineMemOperand(MPI, MachineMemOperand::MOStore, 2, Align(2)); | |||
27803 | ||||
27804 | // Store FP control word into memory. | |||
27805 | SDValue Ops[] = {Chain, StackSlot}; | |||
27806 | Chain = DAG.getMemIntrinsicNode( | |||
27807 | X86ISD::FNSTCW16m, DL, DAG.getVTList(MVT::Other), Ops, MVT::i16, MMO); | |||
27808 | ||||
27809 | // Load FP Control Word from stack slot and clear RM field (bits 11:10). | |||
27810 | SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot, MPI); | |||
27811 | Chain = CWD.getValue(1); | |||
27812 | CWD = DAG.getNode(ISD::AND, DL, MVT::i16, CWD.getValue(0), | |||
27813 | DAG.getConstant(0xf3ff, DL, MVT::i16)); | |||
27814 | ||||
27815 | // Calculate new rounding mode. | |||
27816 | SDValue NewRM = Op.getNode()->getOperand(1); | |||
27817 | SDValue RMBits; | |||
27818 | if (auto *CVal = dyn_cast<ConstantSDNode>(NewRM)) { | |||
27819 | uint64_t RM = CVal->getZExtValue(); | |||
27820 | int FieldVal; | |||
27821 | switch (static_cast<RoundingMode>(RM)) { | |||
27822 | case RoundingMode::NearestTiesToEven: FieldVal = X86::rmToNearest; break; | |||
27823 | case RoundingMode::TowardNegative: FieldVal = X86::rmDownward; break; | |||
27824 | case RoundingMode::TowardPositive: FieldVal = X86::rmUpward; break; | |||
27825 | case RoundingMode::TowardZero: FieldVal = X86::rmTowardZero; break; | |||
27826 | default: | |||
27827 | llvm_unreachable("rounding mode is not supported by X86 hardware")::llvm::llvm_unreachable_internal("rounding mode is not supported by X86 hardware" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 27827); | |||
27828 | } | |||
27829 | RMBits = DAG.getConstant(FieldVal, DL, MVT::i16); | |||
27830 | } else { | |||
27831 | // Need to convert argument into bits of control word: | |||
27832 | // 0 Round to 0 -> 11 | |||
27833 | // 1 Round to nearest -> 00 | |||
27834 | // 2 Round to +inf -> 10 | |||
27835 | // 3 Round to -inf -> 01 | |||
27836 | // The 2-bit value needs then to be shifted so that it occupies bits 11:10. | |||
27837 | // To make the conversion, put all these values into a value 0xc9 and shift | |||
27838 | // it left depending on the rounding mode: | |||
27839 | // (0xc9 << 4) & 0xc00 = X86::rmTowardZero | |||
27840 | // (0xc9 << 6) & 0xc00 = X86::rmToNearest | |||
27841 | // ... | |||
27842 | // (0xc9 << (2 * NewRM + 4)) & 0xc00 | |||
27843 | SDValue ShiftValue = | |||
27844 | DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, | |||
27845 | DAG.getNode(ISD::ADD, DL, MVT::i32, | |||
27846 | DAG.getNode(ISD::SHL, DL, MVT::i32, NewRM, | |||
27847 | DAG.getConstant(1, DL, MVT::i8)), | |||
27848 | DAG.getConstant(4, DL, MVT::i32))); | |||
27849 | SDValue Shifted = | |||
27850 | DAG.getNode(ISD::SHL, DL, MVT::i16, DAG.getConstant(0xc9, DL, MVT::i16), | |||
27851 | ShiftValue); | |||
27852 | RMBits = DAG.getNode(ISD::AND, DL, MVT::i16, Shifted, | |||
27853 | DAG.getConstant(0xc00, DL, MVT::i16)); | |||
27854 | } | |||
27855 | ||||
27856 | // Update rounding mode bits and store the new FP Control Word into stack. | |||
27857 | CWD = DAG.getNode(ISD::OR, DL, MVT::i16, CWD, RMBits); | |||
27858 | Chain = DAG.getStore(Chain, DL, CWD, StackSlot, MPI, /* Alignment = */ 2); | |||
27859 | ||||
27860 | // Load FP control word from the slot. | |||
27861 | SDValue OpsLD[] = {Chain, StackSlot}; | |||
27862 | MachineMemOperand *MMOL = | |||
27863 | MF.getMachineMemOperand(MPI, MachineMemOperand::MOLoad, 2, Align(2)); | |||
27864 | Chain = DAG.getMemIntrinsicNode( | |||
27865 | X86ISD::FLDCW16m, DL, DAG.getVTList(MVT::Other), OpsLD, MVT::i16, MMOL); | |||
27866 | ||||
27867 | // If target supports SSE, set MXCSR as well. Rounding mode is encoded in the | |||
27868 | // same way but in bits 14:13. | |||
27869 | if (Subtarget.hasSSE1()) { | |||
27870 | // Store MXCSR into memory. | |||
27871 | Chain = DAG.getNode( | |||
27872 | ISD::INTRINSIC_VOID, DL, DAG.getVTList(MVT::Other), Chain, | |||
27873 | DAG.getTargetConstant(Intrinsic::x86_sse_stmxcsr, DL, MVT::i32), | |||
27874 | StackSlot); | |||
27875 | ||||
27876 | // Load MXCSR from stack slot and clear RM field (bits 14:13). | |||
27877 | SDValue CWD = DAG.getLoad(MVT::i32, DL, Chain, StackSlot, MPI); | |||
27878 | Chain = CWD.getValue(1); | |||
27879 | CWD = DAG.getNode(ISD::AND, DL, MVT::i32, CWD.getValue(0), | |||
27880 | DAG.getConstant(0xffff9fff, DL, MVT::i32)); | |||
27881 | ||||
27882 | // Shift X87 RM bits from 11:10 to 14:13. | |||
27883 | RMBits = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, RMBits); | |||
27884 | RMBits = DAG.getNode(ISD::SHL, DL, MVT::i32, RMBits, | |||
27885 | DAG.getConstant(3, DL, MVT::i8)); | |||
27886 | ||||
27887 | // Update rounding mode bits and store the new FP Control Word into stack. | |||
27888 | CWD = DAG.getNode(ISD::OR, DL, MVT::i32, CWD, RMBits); | |||
27889 | Chain = DAG.getStore(Chain, DL, CWD, StackSlot, MPI, /* Alignment = */ 4); | |||
27890 | ||||
27891 | // Load MXCSR from the slot. | |||
27892 | Chain = DAG.getNode( | |||
27893 | ISD::INTRINSIC_VOID, DL, DAG.getVTList(MVT::Other), Chain, | |||
27894 | DAG.getTargetConstant(Intrinsic::x86_sse_ldmxcsr, DL, MVT::i32), | |||
27895 | StackSlot); | |||
27896 | } | |||
27897 | ||||
27898 | return Chain; | |||
27899 | } | |||
27900 | ||||
27901 | /// Lower a vector CTLZ using native supported vector CTLZ instruction. | |||
27902 | // | |||
27903 | // i8/i16 vector implemented using dword LZCNT vector instruction | |||
27904 | // ( sub(trunc(lzcnt(zext32(x)))) ). In case zext32(x) is illegal, | |||
27905 | // split the vector, perform operation on it's Lo a Hi part and | |||
27906 | // concatenate the results. | |||
27907 | static SDValue LowerVectorCTLZ_AVX512CDI(SDValue Op, SelectionDAG &DAG, | |||
27908 | const X86Subtarget &Subtarget) { | |||
27909 | assert(Op.getOpcode() == ISD::CTLZ)(static_cast <bool> (Op.getOpcode() == ISD::CTLZ) ? void (0) : __assert_fail ("Op.getOpcode() == ISD::CTLZ", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 27909, __extension__ __PRETTY_FUNCTION__)); | |||
27910 | SDLoc dl(Op); | |||
27911 | MVT VT = Op.getSimpleValueType(); | |||
27912 | MVT EltVT = VT.getVectorElementType(); | |||
27913 | unsigned NumElems = VT.getVectorNumElements(); | |||
27914 | ||||
27915 | assert((EltVT == MVT::i8 || EltVT == MVT::i16) &&(static_cast <bool> ((EltVT == MVT::i8 || EltVT == MVT:: i16) && "Unsupported element type") ? void (0) : __assert_fail ("(EltVT == MVT::i8 || EltVT == MVT::i16) && \"Unsupported element type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 27916, __extension__ __PRETTY_FUNCTION__)) | |||
27916 | "Unsupported element type")(static_cast <bool> ((EltVT == MVT::i8 || EltVT == MVT:: i16) && "Unsupported element type") ? void (0) : __assert_fail ("(EltVT == MVT::i8 || EltVT == MVT::i16) && \"Unsupported element type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 27916, __extension__ __PRETTY_FUNCTION__)); | |||
27917 | ||||
27918 | // Split vector, it's Lo and Hi parts will be handled in next iteration. | |||
27919 | if (NumElems > 16 || | |||
27920 | (NumElems == 16 && !Subtarget.canExtendTo512DQ())) | |||
27921 | return splitVectorIntUnary(Op, DAG); | |||
27922 | ||||
27923 | MVT NewVT = MVT::getVectorVT(MVT::i32, NumElems); | |||
27924 | assert((NewVT.is256BitVector() || NewVT.is512BitVector()) &&(static_cast <bool> ((NewVT.is256BitVector() || NewVT.is512BitVector ()) && "Unsupported value type for operation") ? void (0) : __assert_fail ("(NewVT.is256BitVector() || NewVT.is512BitVector()) && \"Unsupported value type for operation\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 27925, __extension__ __PRETTY_FUNCTION__)) | |||
27925 | "Unsupported value type for operation")(static_cast <bool> ((NewVT.is256BitVector() || NewVT.is512BitVector ()) && "Unsupported value type for operation") ? void (0) : __assert_fail ("(NewVT.is256BitVector() || NewVT.is512BitVector()) && \"Unsupported value type for operation\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 27925, __extension__ __PRETTY_FUNCTION__)); | |||
27926 | ||||
27927 | // Use native supported vector instruction vplzcntd. | |||
27928 | Op = DAG.getNode(ISD::ZERO_EXTEND, dl, NewVT, Op.getOperand(0)); | |||
27929 | SDValue CtlzNode = DAG.getNode(ISD::CTLZ, dl, NewVT, Op); | |||
27930 | SDValue TruncNode = DAG.getNode(ISD::TRUNCATE, dl, VT, CtlzNode); | |||
27931 | SDValue Delta = DAG.getConstant(32 - EltVT.getSizeInBits(), dl, VT); | |||
27932 | ||||
27933 | return DAG.getNode(ISD::SUB, dl, VT, TruncNode, Delta); | |||
27934 | } | |||
27935 | ||||
27936 | // Lower CTLZ using a PSHUFB lookup table implementation. | |||
27937 | static SDValue LowerVectorCTLZInRegLUT(SDValue Op, const SDLoc &DL, | |||
27938 | const X86Subtarget &Subtarget, | |||
27939 | SelectionDAG &DAG) { | |||
27940 | MVT VT = Op.getSimpleValueType(); | |||
27941 | int NumElts = VT.getVectorNumElements(); | |||
27942 | int NumBytes = NumElts * (VT.getScalarSizeInBits() / 8); | |||
27943 | MVT CurrVT = MVT::getVectorVT(MVT::i8, NumBytes); | |||
27944 | ||||
27945 | // Per-nibble leading zero PSHUFB lookup table. | |||
27946 | const int LUT[16] = {/* 0 */ 4, /* 1 */ 3, /* 2 */ 2, /* 3 */ 2, | |||
27947 | /* 4 */ 1, /* 5 */ 1, /* 6 */ 1, /* 7 */ 1, | |||
27948 | /* 8 */ 0, /* 9 */ 0, /* a */ 0, /* b */ 0, | |||
27949 | /* c */ 0, /* d */ 0, /* e */ 0, /* f */ 0}; | |||
27950 | ||||
27951 | SmallVector<SDValue, 64> LUTVec; | |||
27952 | for (int i = 0; i < NumBytes; ++i) | |||
27953 | LUTVec.push_back(DAG.getConstant(LUT[i % 16], DL, MVT::i8)); | |||
27954 | SDValue InRegLUT = DAG.getBuildVector(CurrVT, DL, LUTVec); | |||
27955 | ||||
27956 | // Begin by bitcasting the input to byte vector, then split those bytes | |||
27957 | // into lo/hi nibbles and use the PSHUFB LUT to perform CLTZ on each of them. | |||
27958 | // If the hi input nibble is zero then we add both results together, otherwise | |||
27959 | // we just take the hi result (by masking the lo result to zero before the | |||
27960 | // add). | |||
27961 | SDValue Op0 = DAG.getBitcast(CurrVT, Op.getOperand(0)); | |||
27962 | SDValue Zero = DAG.getConstant(0, DL, CurrVT); | |||
27963 | ||||
27964 | SDValue NibbleShift = DAG.getConstant(0x4, DL, CurrVT); | |||
27965 | SDValue Lo = Op0; | |||
27966 | SDValue Hi = DAG.getNode(ISD::SRL, DL, CurrVT, Op0, NibbleShift); | |||
27967 | SDValue HiZ; | |||
27968 | if (CurrVT.is512BitVector()) { | |||
27969 | MVT MaskVT = MVT::getVectorVT(MVT::i1, CurrVT.getVectorNumElements()); | |||
27970 | HiZ = DAG.getSetCC(DL, MaskVT, Hi, Zero, ISD::SETEQ); | |||
27971 | HiZ = DAG.getNode(ISD::SIGN_EXTEND, DL, CurrVT, HiZ); | |||
27972 | } else { | |||
27973 | HiZ = DAG.getSetCC(DL, CurrVT, Hi, Zero, ISD::SETEQ); | |||
27974 | } | |||
27975 | ||||
27976 | Lo = DAG.getNode(X86ISD::PSHUFB, DL, CurrVT, InRegLUT, Lo); | |||
27977 | Hi = DAG.getNode(X86ISD::PSHUFB, DL, CurrVT, InRegLUT, Hi); | |||
27978 | Lo = DAG.getNode(ISD::AND, DL, CurrVT, Lo, HiZ); | |||
27979 | SDValue Res = DAG.getNode(ISD::ADD, DL, CurrVT, Lo, Hi); | |||
27980 | ||||
27981 | // Merge result back from vXi8 back to VT, working on the lo/hi halves | |||
27982 | // of the current vector width in the same way we did for the nibbles. | |||
27983 | // If the upper half of the input element is zero then add the halves' | |||
27984 | // leading zero counts together, otherwise just use the upper half's. | |||
27985 | // Double the width of the result until we are at target width. | |||
27986 | while (CurrVT != VT) { | |||
27987 | int CurrScalarSizeInBits = CurrVT.getScalarSizeInBits(); | |||
27988 | int CurrNumElts = CurrVT.getVectorNumElements(); | |||
27989 | MVT NextSVT = MVT::getIntegerVT(CurrScalarSizeInBits * 2); | |||
27990 | MVT NextVT = MVT::getVectorVT(NextSVT, CurrNumElts / 2); | |||
27991 | SDValue Shift = DAG.getConstant(CurrScalarSizeInBits, DL, NextVT); | |||
27992 | ||||
27993 | // Check if the upper half of the input element is zero. | |||
27994 | if (CurrVT.is512BitVector()) { | |||
27995 | MVT MaskVT = MVT::getVectorVT(MVT::i1, CurrVT.getVectorNumElements()); | |||
27996 | HiZ = DAG.getSetCC(DL, MaskVT, DAG.getBitcast(CurrVT, Op0), | |||
27997 | DAG.getBitcast(CurrVT, Zero), ISD::SETEQ); | |||
27998 | HiZ = DAG.getNode(ISD::SIGN_EXTEND, DL, CurrVT, HiZ); | |||
27999 | } else { | |||
28000 | HiZ = DAG.getSetCC(DL, CurrVT, DAG.getBitcast(CurrVT, Op0), | |||
28001 | DAG.getBitcast(CurrVT, Zero), ISD::SETEQ); | |||
28002 | } | |||
28003 | HiZ = DAG.getBitcast(NextVT, HiZ); | |||
28004 | ||||
28005 | // Move the upper/lower halves to the lower bits as we'll be extending to | |||
28006 | // NextVT. Mask the lower result to zero if HiZ is true and add the results | |||
28007 | // together. | |||
28008 | SDValue ResNext = Res = DAG.getBitcast(NextVT, Res); | |||
28009 | SDValue R0 = DAG.getNode(ISD::SRL, DL, NextVT, ResNext, Shift); | |||
28010 | SDValue R1 = DAG.getNode(ISD::SRL, DL, NextVT, HiZ, Shift); | |||
28011 | R1 = DAG.getNode(ISD::AND, DL, NextVT, ResNext, R1); | |||
28012 | Res = DAG.getNode(ISD::ADD, DL, NextVT, R0, R1); | |||
28013 | CurrVT = NextVT; | |||
28014 | } | |||
28015 | ||||
28016 | return Res; | |||
28017 | } | |||
28018 | ||||
28019 | static SDValue LowerVectorCTLZ(SDValue Op, const SDLoc &DL, | |||
28020 | const X86Subtarget &Subtarget, | |||
28021 | SelectionDAG &DAG) { | |||
28022 | MVT VT = Op.getSimpleValueType(); | |||
28023 | ||||
28024 | if (Subtarget.hasCDI() && | |||
28025 | // vXi8 vectors need to be promoted to 512-bits for vXi32. | |||
28026 | (Subtarget.canExtendTo512DQ() || VT.getVectorElementType() != MVT::i8)) | |||
28027 | return LowerVectorCTLZ_AVX512CDI(Op, DAG, Subtarget); | |||
28028 | ||||
28029 | // Decompose 256-bit ops into smaller 128-bit ops. | |||
28030 | if (VT.is256BitVector() && !Subtarget.hasInt256()) | |||
28031 | return splitVectorIntUnary(Op, DAG); | |||
28032 | ||||
28033 | // Decompose 512-bit ops into smaller 256-bit ops. | |||
28034 | if (VT.is512BitVector() && !Subtarget.hasBWI()) | |||
28035 | return splitVectorIntUnary(Op, DAG); | |||
28036 | ||||
28037 | assert(Subtarget.hasSSSE3() && "Expected SSSE3 support for PSHUFB")(static_cast <bool> (Subtarget.hasSSSE3() && "Expected SSSE3 support for PSHUFB" ) ? void (0) : __assert_fail ("Subtarget.hasSSSE3() && \"Expected SSSE3 support for PSHUFB\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28037, __extension__ __PRETTY_FUNCTION__)); | |||
28038 | return LowerVectorCTLZInRegLUT(Op, DL, Subtarget, DAG); | |||
28039 | } | |||
28040 | ||||
28041 | static SDValue LowerCTLZ(SDValue Op, const X86Subtarget &Subtarget, | |||
28042 | SelectionDAG &DAG) { | |||
28043 | MVT VT = Op.getSimpleValueType(); | |||
28044 | MVT OpVT = VT; | |||
28045 | unsigned NumBits = VT.getSizeInBits(); | |||
28046 | SDLoc dl(Op); | |||
28047 | unsigned Opc = Op.getOpcode(); | |||
28048 | ||||
28049 | if (VT.isVector()) | |||
28050 | return LowerVectorCTLZ(Op, dl, Subtarget, DAG); | |||
28051 | ||||
28052 | Op = Op.getOperand(0); | |||
28053 | if (VT == MVT::i8) { | |||
28054 | // Zero extend to i32 since there is not an i8 bsr. | |||
28055 | OpVT = MVT::i32; | |||
28056 | Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op); | |||
28057 | } | |||
28058 | ||||
28059 | // Issue a bsr (scan bits in reverse) which also sets EFLAGS. | |||
28060 | SDVTList VTs = DAG.getVTList(OpVT, MVT::i32); | |||
28061 | Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op); | |||
28062 | ||||
28063 | if (Opc == ISD::CTLZ) { | |||
28064 | // If src is zero (i.e. bsr sets ZF), returns NumBits. | |||
28065 | SDValue Ops[] = {Op, DAG.getConstant(NumBits + NumBits - 1, dl, OpVT), | |||
28066 | DAG.getTargetConstant(X86::COND_E, dl, MVT::i8), | |||
28067 | Op.getValue(1)}; | |||
28068 | Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops); | |||
28069 | } | |||
28070 | ||||
28071 | // Finally xor with NumBits-1. | |||
28072 | Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, | |||
28073 | DAG.getConstant(NumBits - 1, dl, OpVT)); | |||
28074 | ||||
28075 | if (VT == MVT::i8) | |||
28076 | Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op); | |||
28077 | return Op; | |||
28078 | } | |||
28079 | ||||
28080 | static SDValue LowerCTTZ(SDValue Op, const X86Subtarget &Subtarget, | |||
28081 | SelectionDAG &DAG) { | |||
28082 | MVT VT = Op.getSimpleValueType(); | |||
28083 | unsigned NumBits = VT.getScalarSizeInBits(); | |||
28084 | SDValue N0 = Op.getOperand(0); | |||
28085 | SDLoc dl(Op); | |||
28086 | ||||
28087 | assert(!VT.isVector() && Op.getOpcode() == ISD::CTTZ &&(static_cast <bool> (!VT.isVector() && Op.getOpcode () == ISD::CTTZ && "Only scalar CTTZ requires custom lowering" ) ? void (0) : __assert_fail ("!VT.isVector() && Op.getOpcode() == ISD::CTTZ && \"Only scalar CTTZ requires custom lowering\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28088, __extension__ __PRETTY_FUNCTION__)) | |||
28088 | "Only scalar CTTZ requires custom lowering")(static_cast <bool> (!VT.isVector() && Op.getOpcode () == ISD::CTTZ && "Only scalar CTTZ requires custom lowering" ) ? void (0) : __assert_fail ("!VT.isVector() && Op.getOpcode() == ISD::CTTZ && \"Only scalar CTTZ requires custom lowering\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28088, __extension__ __PRETTY_FUNCTION__)); | |||
28089 | ||||
28090 | // Issue a bsf (scan bits forward) which also sets EFLAGS. | |||
28091 | SDVTList VTs = DAG.getVTList(VT, MVT::i32); | |||
28092 | Op = DAG.getNode(X86ISD::BSF, dl, VTs, N0); | |||
28093 | ||||
28094 | // If src is zero (i.e. bsf sets ZF), returns NumBits. | |||
28095 | SDValue Ops[] = {Op, DAG.getConstant(NumBits, dl, VT), | |||
28096 | DAG.getTargetConstant(X86::COND_E, dl, MVT::i8), | |||
28097 | Op.getValue(1)}; | |||
28098 | return DAG.getNode(X86ISD::CMOV, dl, VT, Ops); | |||
28099 | } | |||
28100 | ||||
28101 | static SDValue lowerAddSub(SDValue Op, SelectionDAG &DAG, | |||
28102 | const X86Subtarget &Subtarget) { | |||
28103 | MVT VT = Op.getSimpleValueType(); | |||
28104 | if (VT == MVT::i16 || VT == MVT::i32) | |||
28105 | return lowerAddSubToHorizontalOp(Op, DAG, Subtarget); | |||
28106 | ||||
28107 | if (VT == MVT::v32i16 || VT == MVT::v64i8) | |||
28108 | return splitVectorIntBinary(Op, DAG); | |||
28109 | ||||
28110 | assert(Op.getSimpleValueType().is256BitVector() &&(static_cast <bool> (Op.getSimpleValueType().is256BitVector () && Op.getSimpleValueType().isInteger() && "Only handle AVX 256-bit vector integer operation" ) ? void (0) : __assert_fail ("Op.getSimpleValueType().is256BitVector() && Op.getSimpleValueType().isInteger() && \"Only handle AVX 256-bit vector integer operation\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28112, __extension__ __PRETTY_FUNCTION__)) | |||
28111 | Op.getSimpleValueType().isInteger() &&(static_cast <bool> (Op.getSimpleValueType().is256BitVector () && Op.getSimpleValueType().isInteger() && "Only handle AVX 256-bit vector integer operation" ) ? void (0) : __assert_fail ("Op.getSimpleValueType().is256BitVector() && Op.getSimpleValueType().isInteger() && \"Only handle AVX 256-bit vector integer operation\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28112, __extension__ __PRETTY_FUNCTION__)) | |||
28112 | "Only handle AVX 256-bit vector integer operation")(static_cast <bool> (Op.getSimpleValueType().is256BitVector () && Op.getSimpleValueType().isInteger() && "Only handle AVX 256-bit vector integer operation" ) ? void (0) : __assert_fail ("Op.getSimpleValueType().is256BitVector() && Op.getSimpleValueType().isInteger() && \"Only handle AVX 256-bit vector integer operation\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28112, __extension__ __PRETTY_FUNCTION__)); | |||
28113 | return splitVectorIntBinary(Op, DAG); | |||
28114 | } | |||
28115 | ||||
28116 | static SDValue LowerADDSAT_SUBSAT(SDValue Op, SelectionDAG &DAG, | |||
28117 | const X86Subtarget &Subtarget) { | |||
28118 | MVT VT = Op.getSimpleValueType(); | |||
28119 | SDValue X = Op.getOperand(0), Y = Op.getOperand(1); | |||
28120 | unsigned Opcode = Op.getOpcode(); | |||
28121 | SDLoc DL(Op); | |||
28122 | ||||
28123 | if (VT == MVT::v32i16 || VT == MVT::v64i8 || | |||
28124 | (VT.is256BitVector() && !Subtarget.hasInt256())) { | |||
28125 | assert(Op.getSimpleValueType().isInteger() &&(static_cast <bool> (Op.getSimpleValueType().isInteger( ) && "Only handle AVX vector integer operation") ? void (0) : __assert_fail ("Op.getSimpleValueType().isInteger() && \"Only handle AVX vector integer operation\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28126, __extension__ __PRETTY_FUNCTION__)) | |||
28126 | "Only handle AVX vector integer operation")(static_cast <bool> (Op.getSimpleValueType().isInteger( ) && "Only handle AVX vector integer operation") ? void (0) : __assert_fail ("Op.getSimpleValueType().isInteger() && \"Only handle AVX vector integer operation\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28126, __extension__ __PRETTY_FUNCTION__)); | |||
28127 | return splitVectorIntBinary(Op, DAG); | |||
28128 | } | |||
28129 | ||||
28130 | // Avoid the generic expansion with min/max if we don't have pminu*/pmaxu*. | |||
28131 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
28132 | EVT SetCCResultType = | |||
28133 | TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT); | |||
28134 | ||||
28135 | if (Opcode == ISD::USUBSAT && !TLI.isOperationLegal(ISD::UMAX, VT)) { | |||
28136 | // usubsat X, Y --> (X >u Y) ? X - Y : 0 | |||
28137 | SDValue Sub = DAG.getNode(ISD::SUB, DL, VT, X, Y); | |||
28138 | SDValue Cmp = DAG.getSetCC(DL, SetCCResultType, X, Y, ISD::SETUGT); | |||
28139 | // TODO: Move this to DAGCombiner? | |||
28140 | if (SetCCResultType == VT && | |||
28141 | DAG.ComputeNumSignBits(Cmp) == VT.getScalarSizeInBits()) | |||
28142 | return DAG.getNode(ISD::AND, DL, VT, Cmp, Sub); | |||
28143 | return DAG.getSelect(DL, VT, Cmp, Sub, DAG.getConstant(0, DL, VT)); | |||
28144 | } | |||
28145 | ||||
28146 | if ((Opcode == ISD::SADDSAT || Opcode == ISD::SSUBSAT) && | |||
28147 | (!VT.isVector() || VT == MVT::v2i64)) { | |||
28148 | unsigned BitWidth = VT.getScalarSizeInBits(); | |||
28149 | APInt MinVal = APInt::getSignedMinValue(BitWidth); | |||
28150 | APInt MaxVal = APInt::getSignedMaxValue(BitWidth); | |||
28151 | SDValue Zero = DAG.getConstant(0, DL, VT); | |||
28152 | SDValue Result = | |||
28153 | DAG.getNode(Opcode == ISD::SADDSAT ? ISD::SADDO : ISD::SSUBO, DL, | |||
28154 | DAG.getVTList(VT, SetCCResultType), X, Y); | |||
28155 | SDValue SumDiff = Result.getValue(0); | |||
28156 | SDValue Overflow = Result.getValue(1); | |||
28157 | SDValue SatMin = DAG.getConstant(MinVal, DL, VT); | |||
28158 | SDValue SatMax = DAG.getConstant(MaxVal, DL, VT); | |||
28159 | SDValue SumNeg = | |||
28160 | DAG.getSetCC(DL, SetCCResultType, SumDiff, Zero, ISD::SETLT); | |||
28161 | Result = DAG.getSelect(DL, VT, SumNeg, SatMax, SatMin); | |||
28162 | return DAG.getSelect(DL, VT, Overflow, Result, SumDiff); | |||
28163 | } | |||
28164 | ||||
28165 | // Use default expansion. | |||
28166 | return SDValue(); | |||
28167 | } | |||
28168 | ||||
28169 | static SDValue LowerABS(SDValue Op, const X86Subtarget &Subtarget, | |||
28170 | SelectionDAG &DAG) { | |||
28171 | MVT VT = Op.getSimpleValueType(); | |||
28172 | if (VT == MVT::i16 || VT == MVT::i32 || VT == MVT::i64) { | |||
28173 | // Since X86 does not have CMOV for 8-bit integer, we don't convert | |||
28174 | // 8-bit integer abs to NEG and CMOV. | |||
28175 | SDLoc DL(Op); | |||
28176 | SDValue N0 = Op.getOperand(0); | |||
28177 | SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32), | |||
28178 | DAG.getConstant(0, DL, VT), N0); | |||
28179 | SDValue Ops[] = {N0, Neg, DAG.getTargetConstant(X86::COND_NS, DL, MVT::i8), | |||
28180 | SDValue(Neg.getNode(), 1)}; | |||
28181 | return DAG.getNode(X86ISD::CMOV, DL, VT, Ops); | |||
28182 | } | |||
28183 | ||||
28184 | // ABS(vXi64 X) --> VPBLENDVPD(X, 0-X, X). | |||
28185 | if ((VT == MVT::v2i64 || VT == MVT::v4i64) && Subtarget.hasSSE41()) { | |||
28186 | SDLoc DL(Op); | |||
28187 | SDValue Src = Op.getOperand(0); | |||
28188 | SDValue Sub = | |||
28189 | DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), Src); | |||
28190 | return DAG.getNode(X86ISD::BLENDV, DL, VT, Src, Sub, Src); | |||
28191 | } | |||
28192 | ||||
28193 | if (VT.is256BitVector() && !Subtarget.hasInt256()) { | |||
28194 | assert(VT.isInteger() &&(static_cast <bool> (VT.isInteger() && "Only handle AVX 256-bit vector integer operation" ) ? void (0) : __assert_fail ("VT.isInteger() && \"Only handle AVX 256-bit vector integer operation\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28195, __extension__ __PRETTY_FUNCTION__)) | |||
28195 | "Only handle AVX 256-bit vector integer operation")(static_cast <bool> (VT.isInteger() && "Only handle AVX 256-bit vector integer operation" ) ? void (0) : __assert_fail ("VT.isInteger() && \"Only handle AVX 256-bit vector integer operation\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28195, __extension__ __PRETTY_FUNCTION__)); | |||
28196 | return splitVectorIntUnary(Op, DAG); | |||
28197 | } | |||
28198 | ||||
28199 | if ((VT == MVT::v32i16 || VT == MVT::v64i8) && !Subtarget.hasBWI()) | |||
28200 | return splitVectorIntUnary(Op, DAG); | |||
28201 | ||||
28202 | // Default to expand. | |||
28203 | return SDValue(); | |||
28204 | } | |||
28205 | ||||
28206 | static SDValue LowerMINMAX(SDValue Op, SelectionDAG &DAG) { | |||
28207 | MVT VT = Op.getSimpleValueType(); | |||
28208 | ||||
28209 | // For AVX1 cases, split to use legal ops (everything but v4i64). | |||
28210 | if (VT.getScalarType() != MVT::i64 && VT.is256BitVector()) | |||
28211 | return splitVectorIntBinary(Op, DAG); | |||
28212 | ||||
28213 | if (VT == MVT::v32i16 || VT == MVT::v64i8) | |||
28214 | return splitVectorIntBinary(Op, DAG); | |||
28215 | ||||
28216 | // Default to expand. | |||
28217 | return SDValue(); | |||
28218 | } | |||
28219 | ||||
28220 | static SDValue LowerMUL(SDValue Op, const X86Subtarget &Subtarget, | |||
28221 | SelectionDAG &DAG) { | |||
28222 | SDLoc dl(Op); | |||
28223 | MVT VT = Op.getSimpleValueType(); | |||
28224 | ||||
28225 | // Decompose 256-bit ops into 128-bit ops. | |||
28226 | if (VT.is256BitVector() && !Subtarget.hasInt256()) | |||
28227 | return splitVectorIntBinary(Op, DAG); | |||
28228 | ||||
28229 | if ((VT == MVT::v32i16 || VT == MVT::v64i8) && !Subtarget.hasBWI()) | |||
28230 | return splitVectorIntBinary(Op, DAG); | |||
28231 | ||||
28232 | SDValue A = Op.getOperand(0); | |||
28233 | SDValue B = Op.getOperand(1); | |||
28234 | ||||
28235 | // Lower v16i8/v32i8/v64i8 mul as sign-extension to v8i16/v16i16/v32i16 | |||
28236 | // vector pairs, multiply and truncate. | |||
28237 | if (VT == MVT::v16i8 || VT == MVT::v32i8 || VT == MVT::v64i8) { | |||
28238 | unsigned NumElts = VT.getVectorNumElements(); | |||
28239 | ||||
28240 | if ((VT == MVT::v16i8 && Subtarget.hasInt256()) || | |||
28241 | (VT == MVT::v32i8 && Subtarget.canExtendTo512BW())) { | |||
28242 | MVT ExVT = MVT::getVectorVT(MVT::i16, VT.getVectorNumElements()); | |||
28243 | return DAG.getNode( | |||
28244 | ISD::TRUNCATE, dl, VT, | |||
28245 | DAG.getNode(ISD::MUL, dl, ExVT, | |||
28246 | DAG.getNode(ISD::ANY_EXTEND, dl, ExVT, A), | |||
28247 | DAG.getNode(ISD::ANY_EXTEND, dl, ExVT, B))); | |||
28248 | } | |||
28249 | ||||
28250 | MVT ExVT = MVT::getVectorVT(MVT::i16, NumElts / 2); | |||
28251 | ||||
28252 | // Extract the lo/hi parts to any extend to i16. | |||
28253 | // We're going to mask off the low byte of each result element of the | |||
28254 | // pmullw, so it doesn't matter what's in the high byte of each 16-bit | |||
28255 | // element. | |||
28256 | SDValue Undef = DAG.getUNDEF(VT); | |||
28257 | SDValue ALo = DAG.getBitcast(ExVT, getUnpackl(DAG, dl, VT, A, Undef)); | |||
28258 | SDValue AHi = DAG.getBitcast(ExVT, getUnpackh(DAG, dl, VT, A, Undef)); | |||
28259 | ||||
28260 | SDValue BLo, BHi; | |||
28261 | if (ISD::isBuildVectorOfConstantSDNodes(B.getNode())) { | |||
28262 | // If the RHS is a constant, manually unpackl/unpackh. | |||
28263 | SmallVector<SDValue, 16> LoOps, HiOps; | |||
28264 | for (unsigned i = 0; i != NumElts; i += 16) { | |||
28265 | for (unsigned j = 0; j != 8; ++j) { | |||
28266 | LoOps.push_back(DAG.getAnyExtOrTrunc(B.getOperand(i + j), dl, | |||
28267 | MVT::i16)); | |||
28268 | HiOps.push_back(DAG.getAnyExtOrTrunc(B.getOperand(i + j + 8), dl, | |||
28269 | MVT::i16)); | |||
28270 | } | |||
28271 | } | |||
28272 | ||||
28273 | BLo = DAG.getBuildVector(ExVT, dl, LoOps); | |||
28274 | BHi = DAG.getBuildVector(ExVT, dl, HiOps); | |||
28275 | } else { | |||
28276 | BLo = DAG.getBitcast(ExVT, getUnpackl(DAG, dl, VT, B, Undef)); | |||
28277 | BHi = DAG.getBitcast(ExVT, getUnpackh(DAG, dl, VT, B, Undef)); | |||
28278 | } | |||
28279 | ||||
28280 | // Multiply, mask the lower 8bits of the lo/hi results and pack. | |||
28281 | SDValue RLo = DAG.getNode(ISD::MUL, dl, ExVT, ALo, BLo); | |||
28282 | SDValue RHi = DAG.getNode(ISD::MUL, dl, ExVT, AHi, BHi); | |||
28283 | RLo = DAG.getNode(ISD::AND, dl, ExVT, RLo, DAG.getConstant(255, dl, ExVT)); | |||
28284 | RHi = DAG.getNode(ISD::AND, dl, ExVT, RHi, DAG.getConstant(255, dl, ExVT)); | |||
28285 | return DAG.getNode(X86ISD::PACKUS, dl, VT, RLo, RHi); | |||
28286 | } | |||
28287 | ||||
28288 | // Lower v4i32 mul as 2x shuffle, 2x pmuludq, 2x shuffle. | |||
28289 | if (VT == MVT::v4i32) { | |||
28290 | assert(Subtarget.hasSSE2() && !Subtarget.hasSSE41() &&(static_cast <bool> (Subtarget.hasSSE2() && !Subtarget .hasSSE41() && "Should not custom lower when pmulld is available!" ) ? void (0) : __assert_fail ("Subtarget.hasSSE2() && !Subtarget.hasSSE41() && \"Should not custom lower when pmulld is available!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28291, __extension__ __PRETTY_FUNCTION__)) | |||
28291 | "Should not custom lower when pmulld is available!")(static_cast <bool> (Subtarget.hasSSE2() && !Subtarget .hasSSE41() && "Should not custom lower when pmulld is available!" ) ? void (0) : __assert_fail ("Subtarget.hasSSE2() && !Subtarget.hasSSE41() && \"Should not custom lower when pmulld is available!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28291, __extension__ __PRETTY_FUNCTION__)); | |||
28292 | ||||
28293 | // Extract the odd parts. | |||
28294 | static const int UnpackMask[] = { 1, -1, 3, -1 }; | |||
28295 | SDValue Aodds = DAG.getVectorShuffle(VT, dl, A, A, UnpackMask); | |||
28296 | SDValue Bodds = DAG.getVectorShuffle(VT, dl, B, B, UnpackMask); | |||
28297 | ||||
28298 | // Multiply the even parts. | |||
28299 | SDValue Evens = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, | |||
28300 | DAG.getBitcast(MVT::v2i64, A), | |||
28301 | DAG.getBitcast(MVT::v2i64, B)); | |||
28302 | // Now multiply odd parts. | |||
28303 | SDValue Odds = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, | |||
28304 | DAG.getBitcast(MVT::v2i64, Aodds), | |||
28305 | DAG.getBitcast(MVT::v2i64, Bodds)); | |||
28306 | ||||
28307 | Evens = DAG.getBitcast(VT, Evens); | |||
28308 | Odds = DAG.getBitcast(VT, Odds); | |||
28309 | ||||
28310 | // Merge the two vectors back together with a shuffle. This expands into 2 | |||
28311 | // shuffles. | |||
28312 | static const int ShufMask[] = { 0, 4, 2, 6 }; | |||
28313 | return DAG.getVectorShuffle(VT, dl, Evens, Odds, ShufMask); | |||
28314 | } | |||
28315 | ||||
28316 | assert((VT == MVT::v2i64 || VT == MVT::v4i64 || VT == MVT::v8i64) &&(static_cast <bool> ((VT == MVT::v2i64 || VT == MVT::v4i64 || VT == MVT::v8i64) && "Only know how to lower V2I64/V4I64/V8I64 multiply" ) ? void (0) : __assert_fail ("(VT == MVT::v2i64 || VT == MVT::v4i64 || VT == MVT::v8i64) && \"Only know how to lower V2I64/V4I64/V8I64 multiply\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28317, __extension__ __PRETTY_FUNCTION__)) | |||
28317 | "Only know how to lower V2I64/V4I64/V8I64 multiply")(static_cast <bool> ((VT == MVT::v2i64 || VT == MVT::v4i64 || VT == MVT::v8i64) && "Only know how to lower V2I64/V4I64/V8I64 multiply" ) ? void (0) : __assert_fail ("(VT == MVT::v2i64 || VT == MVT::v4i64 || VT == MVT::v8i64) && \"Only know how to lower V2I64/V4I64/V8I64 multiply\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28317, __extension__ __PRETTY_FUNCTION__)); | |||
28318 | assert(!Subtarget.hasDQI() && "DQI should use MULLQ")(static_cast <bool> (!Subtarget.hasDQI() && "DQI should use MULLQ" ) ? void (0) : __assert_fail ("!Subtarget.hasDQI() && \"DQI should use MULLQ\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28318, __extension__ __PRETTY_FUNCTION__)); | |||
28319 | ||||
28320 | // Ahi = psrlqi(a, 32); | |||
28321 | // Bhi = psrlqi(b, 32); | |||
28322 | // | |||
28323 | // AloBlo = pmuludq(a, b); | |||
28324 | // AloBhi = pmuludq(a, Bhi); | |||
28325 | // AhiBlo = pmuludq(Ahi, b); | |||
28326 | // | |||
28327 | // Hi = psllqi(AloBhi + AhiBlo, 32); | |||
28328 | // return AloBlo + Hi; | |||
28329 | KnownBits AKnown = DAG.computeKnownBits(A); | |||
28330 | KnownBits BKnown = DAG.computeKnownBits(B); | |||
28331 | ||||
28332 | APInt LowerBitsMask = APInt::getLowBitsSet(64, 32); | |||
28333 | bool ALoIsZero = LowerBitsMask.isSubsetOf(AKnown.Zero); | |||
28334 | bool BLoIsZero = LowerBitsMask.isSubsetOf(BKnown.Zero); | |||
28335 | ||||
28336 | APInt UpperBitsMask = APInt::getHighBitsSet(64, 32); | |||
28337 | bool AHiIsZero = UpperBitsMask.isSubsetOf(AKnown.Zero); | |||
28338 | bool BHiIsZero = UpperBitsMask.isSubsetOf(BKnown.Zero); | |||
28339 | ||||
28340 | SDValue Zero = DAG.getConstant(0, dl, VT); | |||
28341 | ||||
28342 | // Only multiply lo/hi halves that aren't known to be zero. | |||
28343 | SDValue AloBlo = Zero; | |||
28344 | if (!ALoIsZero && !BLoIsZero) | |||
28345 | AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B); | |||
28346 | ||||
28347 | SDValue AloBhi = Zero; | |||
28348 | if (!ALoIsZero && !BHiIsZero) { | |||
28349 | SDValue Bhi = getTargetVShiftByConstNode(X86ISD::VSRLI, dl, VT, B, 32, DAG); | |||
28350 | AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi); | |||
28351 | } | |||
28352 | ||||
28353 | SDValue AhiBlo = Zero; | |||
28354 | if (!AHiIsZero && !BLoIsZero) { | |||
28355 | SDValue Ahi = getTargetVShiftByConstNode(X86ISD::VSRLI, dl, VT, A, 32, DAG); | |||
28356 | AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B); | |||
28357 | } | |||
28358 | ||||
28359 | SDValue Hi = DAG.getNode(ISD::ADD, dl, VT, AloBhi, AhiBlo); | |||
28360 | Hi = getTargetVShiftByConstNode(X86ISD::VSHLI, dl, VT, Hi, 32, DAG); | |||
28361 | ||||
28362 | return DAG.getNode(ISD::ADD, dl, VT, AloBlo, Hi); | |||
28363 | } | |||
28364 | ||||
28365 | static SDValue LowervXi8MulWithUNPCK(SDValue A, SDValue B, const SDLoc &dl, | |||
28366 | MVT VT, bool IsSigned, | |||
28367 | const X86Subtarget &Subtarget, | |||
28368 | SelectionDAG &DAG, | |||
28369 | SDValue *Low = nullptr) { | |||
28370 | unsigned NumElts = VT.getVectorNumElements(); | |||
28371 | ||||
28372 | // For vXi8 we will unpack the low and high half of each 128 bit lane to widen | |||
28373 | // to a vXi16 type. Do the multiplies, shift the results and pack the half | |||
28374 | // lane results back together. | |||
28375 | ||||
28376 | // We'll take different approaches for signed and unsigned. | |||
28377 | // For unsigned we'll use punpcklbw/punpckhbw to put zero extend the bytes | |||
28378 | // and use pmullw to calculate the full 16-bit product. | |||
28379 | // For signed we'll use punpcklbw/punpckbw to extend the bytes to words and | |||
28380 | // shift them left into the upper byte of each word. This allows us to use | |||
28381 | // pmulhw to calculate the full 16-bit product. This trick means we don't | |||
28382 | // need to sign extend the bytes to use pmullw. | |||
28383 | ||||
28384 | MVT ExVT = MVT::getVectorVT(MVT::i16, NumElts / 2); | |||
28385 | SDValue Zero = DAG.getConstant(0, dl, VT); | |||
28386 | ||||
28387 | SDValue ALo, AHi; | |||
28388 | if (IsSigned) { | |||
28389 | ALo = DAG.getBitcast(ExVT, getUnpackl(DAG, dl, VT, Zero, A)); | |||
28390 | AHi = DAG.getBitcast(ExVT, getUnpackh(DAG, dl, VT, Zero, A)); | |||
28391 | } else { | |||
28392 | ALo = DAG.getBitcast(ExVT, getUnpackl(DAG, dl, VT, A, Zero)); | |||
28393 | AHi = DAG.getBitcast(ExVT, getUnpackh(DAG, dl, VT, A, Zero)); | |||
28394 | } | |||
28395 | ||||
28396 | SDValue BLo, BHi; | |||
28397 | if (ISD::isBuildVectorOfConstantSDNodes(B.getNode())) { | |||
28398 | // If the RHS is a constant, manually unpackl/unpackh and extend. | |||
28399 | SmallVector<SDValue, 16> LoOps, HiOps; | |||
28400 | for (unsigned i = 0; i != NumElts; i += 16) { | |||
28401 | for (unsigned j = 0; j != 8; ++j) { | |||
28402 | SDValue LoOp = B.getOperand(i + j); | |||
28403 | SDValue HiOp = B.getOperand(i + j + 8); | |||
28404 | ||||
28405 | if (IsSigned) { | |||
28406 | LoOp = DAG.getAnyExtOrTrunc(LoOp, dl, MVT::i16); | |||
28407 | HiOp = DAG.getAnyExtOrTrunc(HiOp, dl, MVT::i16); | |||
28408 | LoOp = DAG.getNode(ISD::SHL, dl, MVT::i16, LoOp, | |||
28409 | DAG.getConstant(8, dl, MVT::i16)); | |||
28410 | HiOp = DAG.getNode(ISD::SHL, dl, MVT::i16, HiOp, | |||
28411 | DAG.getConstant(8, dl, MVT::i16)); | |||
28412 | } else { | |||
28413 | LoOp = DAG.getZExtOrTrunc(LoOp, dl, MVT::i16); | |||
28414 | HiOp = DAG.getZExtOrTrunc(HiOp, dl, MVT::i16); | |||
28415 | } | |||
28416 | ||||
28417 | LoOps.push_back(LoOp); | |||
28418 | HiOps.push_back(HiOp); | |||
28419 | } | |||
28420 | } | |||
28421 | ||||
28422 | BLo = DAG.getBuildVector(ExVT, dl, LoOps); | |||
28423 | BHi = DAG.getBuildVector(ExVT, dl, HiOps); | |||
28424 | } else if (IsSigned) { | |||
28425 | BLo = DAG.getBitcast(ExVT, getUnpackl(DAG, dl, VT, Zero, B)); | |||
28426 | BHi = DAG.getBitcast(ExVT, getUnpackh(DAG, dl, VT, Zero, B)); | |||
28427 | } else { | |||
28428 | BLo = DAG.getBitcast(ExVT, getUnpackl(DAG, dl, VT, B, Zero)); | |||
28429 | BHi = DAG.getBitcast(ExVT, getUnpackh(DAG, dl, VT, B, Zero)); | |||
28430 | } | |||
28431 | ||||
28432 | // Multiply, lshr the upper 8bits to the lower 8bits of the lo/hi results and | |||
28433 | // pack back to vXi8. | |||
28434 | unsigned MulOpc = IsSigned ? ISD::MULHS : ISD::MUL; | |||
28435 | SDValue RLo = DAG.getNode(MulOpc, dl, ExVT, ALo, BLo); | |||
28436 | SDValue RHi = DAG.getNode(MulOpc, dl, ExVT, AHi, BHi); | |||
28437 | ||||
28438 | if (Low) { | |||
28439 | // Mask the lower bits and pack the results to rejoin the halves. | |||
28440 | SDValue Mask = DAG.getConstant(255, dl, ExVT); | |||
28441 | SDValue LLo = DAG.getNode(ISD::AND, dl, ExVT, RLo, Mask); | |||
28442 | SDValue LHi = DAG.getNode(ISD::AND, dl, ExVT, RHi, Mask); | |||
28443 | *Low = DAG.getNode(X86ISD::PACKUS, dl, VT, LLo, LHi); | |||
28444 | } | |||
28445 | ||||
28446 | RLo = getTargetVShiftByConstNode(X86ISD::VSRLI, dl, ExVT, RLo, 8, DAG); | |||
28447 | RHi = getTargetVShiftByConstNode(X86ISD::VSRLI, dl, ExVT, RHi, 8, DAG); | |||
28448 | ||||
28449 | // Bitcast back to VT and then pack all the even elements from Lo and Hi. | |||
28450 | return DAG.getNode(X86ISD::PACKUS, dl, VT, RLo, RHi); | |||
28451 | } | |||
28452 | ||||
28453 | static SDValue LowerMULH(SDValue Op, const X86Subtarget &Subtarget, | |||
28454 | SelectionDAG &DAG) { | |||
28455 | SDLoc dl(Op); | |||
28456 | MVT VT = Op.getSimpleValueType(); | |||
28457 | bool IsSigned = Op->getOpcode() == ISD::MULHS; | |||
28458 | unsigned NumElts = VT.getVectorNumElements(); | |||
28459 | SDValue A = Op.getOperand(0); | |||
28460 | SDValue B = Op.getOperand(1); | |||
28461 | ||||
28462 | // Decompose 256-bit ops into 128-bit ops. | |||
28463 | if (VT.is256BitVector() && !Subtarget.hasInt256()) | |||
28464 | return splitVectorIntBinary(Op, DAG); | |||
28465 | ||||
28466 | if ((VT == MVT::v32i16 || VT == MVT::v64i8) && !Subtarget.hasBWI()) | |||
28467 | return splitVectorIntBinary(Op, DAG); | |||
28468 | ||||
28469 | if (VT == MVT::v4i32 || VT == MVT::v8i32 || VT == MVT::v16i32) { | |||
28470 | assert((VT == MVT::v4i32 && Subtarget.hasSSE2()) ||(static_cast <bool> ((VT == MVT::v4i32 && Subtarget .hasSSE2()) || (VT == MVT::v8i32 && Subtarget.hasInt256 ()) || (VT == MVT::v16i32 && Subtarget.hasAVX512())) ? void (0) : __assert_fail ("(VT == MVT::v4i32 && Subtarget.hasSSE2()) || (VT == MVT::v8i32 && Subtarget.hasInt256()) || (VT == MVT::v16i32 && Subtarget.hasAVX512())" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28472, __extension__ __PRETTY_FUNCTION__)) | |||
28471 | (VT == MVT::v8i32 && Subtarget.hasInt256()) ||(static_cast <bool> ((VT == MVT::v4i32 && Subtarget .hasSSE2()) || (VT == MVT::v8i32 && Subtarget.hasInt256 ()) || (VT == MVT::v16i32 && Subtarget.hasAVX512())) ? void (0) : __assert_fail ("(VT == MVT::v4i32 && Subtarget.hasSSE2()) || (VT == MVT::v8i32 && Subtarget.hasInt256()) || (VT == MVT::v16i32 && Subtarget.hasAVX512())" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28472, __extension__ __PRETTY_FUNCTION__)) | |||
28472 | (VT == MVT::v16i32 && Subtarget.hasAVX512()))(static_cast <bool> ((VT == MVT::v4i32 && Subtarget .hasSSE2()) || (VT == MVT::v8i32 && Subtarget.hasInt256 ()) || (VT == MVT::v16i32 && Subtarget.hasAVX512())) ? void (0) : __assert_fail ("(VT == MVT::v4i32 && Subtarget.hasSSE2()) || (VT == MVT::v8i32 && Subtarget.hasInt256()) || (VT == MVT::v16i32 && Subtarget.hasAVX512())" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28472, __extension__ __PRETTY_FUNCTION__)); | |||
28473 | ||||
28474 | // PMULxD operations multiply each even value (starting at 0) of LHS with | |||
28475 | // the related value of RHS and produce a widen result. | |||
28476 | // E.g., PMULUDQ <4 x i32> <a|b|c|d>, <4 x i32> <e|f|g|h> | |||
28477 | // => <2 x i64> <ae|cg> | |||
28478 | // | |||
28479 | // In other word, to have all the results, we need to perform two PMULxD: | |||
28480 | // 1. one with the even values. | |||
28481 | // 2. one with the odd values. | |||
28482 | // To achieve #2, with need to place the odd values at an even position. | |||
28483 | // | |||
28484 | // Place the odd value at an even position (basically, shift all values 1 | |||
28485 | // step to the left): | |||
28486 | const int Mask[] = {1, -1, 3, -1, 5, -1, 7, -1, | |||
28487 | 9, -1, 11, -1, 13, -1, 15, -1}; | |||
28488 | // <a|b|c|d> => <b|undef|d|undef> | |||
28489 | SDValue Odd0 = DAG.getVectorShuffle(VT, dl, A, A, | |||
28490 | makeArrayRef(&Mask[0], NumElts)); | |||
28491 | // <e|f|g|h> => <f|undef|h|undef> | |||
28492 | SDValue Odd1 = DAG.getVectorShuffle(VT, dl, B, B, | |||
28493 | makeArrayRef(&Mask[0], NumElts)); | |||
28494 | ||||
28495 | // Emit two multiplies, one for the lower 2 ints and one for the higher 2 | |||
28496 | // ints. | |||
28497 | MVT MulVT = MVT::getVectorVT(MVT::i64, NumElts / 2); | |||
28498 | unsigned Opcode = | |||
28499 | (IsSigned && Subtarget.hasSSE41()) ? X86ISD::PMULDQ : X86ISD::PMULUDQ; | |||
28500 | // PMULUDQ <4 x i32> <a|b|c|d>, <4 x i32> <e|f|g|h> | |||
28501 | // => <2 x i64> <ae|cg> | |||
28502 | SDValue Mul1 = DAG.getBitcast(VT, DAG.getNode(Opcode, dl, MulVT, | |||
28503 | DAG.getBitcast(MulVT, A), | |||
28504 | DAG.getBitcast(MulVT, B))); | |||
28505 | // PMULUDQ <4 x i32> <b|undef|d|undef>, <4 x i32> <f|undef|h|undef> | |||
28506 | // => <2 x i64> <bf|dh> | |||
28507 | SDValue Mul2 = DAG.getBitcast(VT, DAG.getNode(Opcode, dl, MulVT, | |||
28508 | DAG.getBitcast(MulVT, Odd0), | |||
28509 | DAG.getBitcast(MulVT, Odd1))); | |||
28510 | ||||
28511 | // Shuffle it back into the right order. | |||
28512 | SmallVector<int, 16> ShufMask(NumElts); | |||
28513 | for (int i = 0; i != (int)NumElts; ++i) | |||
28514 | ShufMask[i] = (i / 2) * 2 + ((i % 2) * NumElts) + 1; | |||
28515 | ||||
28516 | SDValue Res = DAG.getVectorShuffle(VT, dl, Mul1, Mul2, ShufMask); | |||
28517 | ||||
28518 | // If we have a signed multiply but no PMULDQ fix up the result of an | |||
28519 | // unsigned multiply. | |||
28520 | if (IsSigned && !Subtarget.hasSSE41()) { | |||
28521 | SDValue Zero = DAG.getConstant(0, dl, VT); | |||
28522 | SDValue T1 = DAG.getNode(ISD::AND, dl, VT, | |||
28523 | DAG.getSetCC(dl, VT, Zero, A, ISD::SETGT), B); | |||
28524 | SDValue T2 = DAG.getNode(ISD::AND, dl, VT, | |||
28525 | DAG.getSetCC(dl, VT, Zero, B, ISD::SETGT), A); | |||
28526 | ||||
28527 | SDValue Fixup = DAG.getNode(ISD::ADD, dl, VT, T1, T2); | |||
28528 | Res = DAG.getNode(ISD::SUB, dl, VT, Res, Fixup); | |||
28529 | } | |||
28530 | ||||
28531 | return Res; | |||
28532 | } | |||
28533 | ||||
28534 | // Only i8 vectors should need custom lowering after this. | |||
28535 | assert((VT == MVT::v16i8 || (VT == MVT::v32i8 && Subtarget.hasInt256()) ||(static_cast <bool> ((VT == MVT::v16i8 || (VT == MVT::v32i8 && Subtarget.hasInt256()) || (VT == MVT::v64i8 && Subtarget.hasBWI())) && "Unsupported vector type") ? void (0) : __assert_fail ("(VT == MVT::v16i8 || (VT == MVT::v32i8 && Subtarget.hasInt256()) || (VT == MVT::v64i8 && Subtarget.hasBWI())) && \"Unsupported vector type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28537, __extension__ __PRETTY_FUNCTION__)) | |||
28536 | (VT == MVT::v64i8 && Subtarget.hasBWI())) &&(static_cast <bool> ((VT == MVT::v16i8 || (VT == MVT::v32i8 && Subtarget.hasInt256()) || (VT == MVT::v64i8 && Subtarget.hasBWI())) && "Unsupported vector type") ? void (0) : __assert_fail ("(VT == MVT::v16i8 || (VT == MVT::v32i8 && Subtarget.hasInt256()) || (VT == MVT::v64i8 && Subtarget.hasBWI())) && \"Unsupported vector type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28537, __extension__ __PRETTY_FUNCTION__)) | |||
28537 | "Unsupported vector type")(static_cast <bool> ((VT == MVT::v16i8 || (VT == MVT::v32i8 && Subtarget.hasInt256()) || (VT == MVT::v64i8 && Subtarget.hasBWI())) && "Unsupported vector type") ? void (0) : __assert_fail ("(VT == MVT::v16i8 || (VT == MVT::v32i8 && Subtarget.hasInt256()) || (VT == MVT::v64i8 && Subtarget.hasBWI())) && \"Unsupported vector type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28537, __extension__ __PRETTY_FUNCTION__)); | |||
28538 | ||||
28539 | // Lower v16i8/v32i8 as extension to v8i16/v16i16 vector pairs, multiply, | |||
28540 | // logical shift down the upper half and pack back to i8. | |||
28541 | ||||
28542 | // With SSE41 we can use sign/zero extend, but for pre-SSE41 we unpack | |||
28543 | // and then ashr/lshr the upper bits down to the lower bits before multiply. | |||
28544 | ||||
28545 | if ((VT == MVT::v16i8 && Subtarget.hasInt256()) || | |||
28546 | (VT == MVT::v32i8 && Subtarget.canExtendTo512BW())) { | |||
28547 | MVT ExVT = MVT::getVectorVT(MVT::i16, NumElts); | |||
28548 | unsigned ExAVX = IsSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; | |||
28549 | SDValue ExA = DAG.getNode(ExAVX, dl, ExVT, A); | |||
28550 | SDValue ExB = DAG.getNode(ExAVX, dl, ExVT, B); | |||
28551 | SDValue Mul = DAG.getNode(ISD::MUL, dl, ExVT, ExA, ExB); | |||
28552 | Mul = getTargetVShiftByConstNode(X86ISD::VSRLI, dl, ExVT, Mul, 8, DAG); | |||
28553 | return DAG.getNode(ISD::TRUNCATE, dl, VT, Mul); | |||
28554 | } | |||
28555 | ||||
28556 | return LowervXi8MulWithUNPCK(A, B, dl, VT, IsSigned, Subtarget, DAG); | |||
28557 | } | |||
28558 | ||||
28559 | // Custom lowering for SMULO/UMULO. | |||
28560 | static SDValue LowerMULO(SDValue Op, const X86Subtarget &Subtarget, | |||
28561 | SelectionDAG &DAG) { | |||
28562 | MVT VT = Op.getSimpleValueType(); | |||
28563 | ||||
28564 | // Scalars defer to LowerXALUO. | |||
28565 | if (!VT.isVector()) | |||
28566 | return LowerXALUO(Op, DAG); | |||
28567 | ||||
28568 | SDLoc dl(Op); | |||
28569 | bool IsSigned = Op->getOpcode() == ISD::SMULO; | |||
28570 | SDValue A = Op.getOperand(0); | |||
28571 | SDValue B = Op.getOperand(1); | |||
28572 | EVT OvfVT = Op->getValueType(1); | |||
28573 | ||||
28574 | if ((VT == MVT::v32i8 && !Subtarget.hasInt256()) || | |||
28575 | (VT == MVT::v64i8 && !Subtarget.hasBWI())) { | |||
28576 | // Extract the LHS Lo/Hi vectors | |||
28577 | SDValue LHSLo, LHSHi; | |||
28578 | std::tie(LHSLo, LHSHi) = splitVector(A, DAG, dl); | |||
28579 | ||||
28580 | // Extract the RHS Lo/Hi vectors | |||
28581 | SDValue RHSLo, RHSHi; | |||
28582 | std::tie(RHSLo, RHSHi) = splitVector(B, DAG, dl); | |||
28583 | ||||
28584 | EVT LoOvfVT, HiOvfVT; | |||
28585 | std::tie(LoOvfVT, HiOvfVT) = DAG.GetSplitDestVTs(OvfVT); | |||
28586 | SDVTList LoVTs = DAG.getVTList(LHSLo.getValueType(), LoOvfVT); | |||
28587 | SDVTList HiVTs = DAG.getVTList(LHSHi.getValueType(), HiOvfVT); | |||
28588 | ||||
28589 | // Issue the split operations. | |||
28590 | SDValue Lo = DAG.getNode(Op.getOpcode(), dl, LoVTs, LHSLo, RHSLo); | |||
28591 | SDValue Hi = DAG.getNode(Op.getOpcode(), dl, HiVTs, LHSHi, RHSHi); | |||
28592 | ||||
28593 | // Join the separate data results and the overflow results. | |||
28594 | SDValue Res = DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Lo, Hi); | |||
28595 | SDValue Ovf = DAG.getNode(ISD::CONCAT_VECTORS, dl, OvfVT, Lo.getValue(1), | |||
28596 | Hi.getValue(1)); | |||
28597 | ||||
28598 | return DAG.getMergeValues({Res, Ovf}, dl); | |||
28599 | } | |||
28600 | ||||
28601 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
28602 | EVT SetccVT = | |||
28603 | TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT); | |||
28604 | ||||
28605 | if ((VT == MVT::v16i8 && Subtarget.hasInt256()) || | |||
28606 | (VT == MVT::v32i8 && Subtarget.canExtendTo512BW())) { | |||
28607 | unsigned NumElts = VT.getVectorNumElements(); | |||
28608 | MVT ExVT = MVT::getVectorVT(MVT::i16, NumElts); | |||
28609 | unsigned ExAVX = IsSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; | |||
28610 | SDValue ExA = DAG.getNode(ExAVX, dl, ExVT, A); | |||
28611 | SDValue ExB = DAG.getNode(ExAVX, dl, ExVT, B); | |||
28612 | SDValue Mul = DAG.getNode(ISD::MUL, dl, ExVT, ExA, ExB); | |||
28613 | ||||
28614 | SDValue Low = DAG.getNode(ISD::TRUNCATE, dl, VT, Mul); | |||
28615 | ||||
28616 | SDValue Ovf; | |||
28617 | if (IsSigned) { | |||
28618 | SDValue High, LowSign; | |||
28619 | if (OvfVT.getVectorElementType() == MVT::i1 && | |||
28620 | (Subtarget.hasBWI() || Subtarget.canExtendTo512DQ())) { | |||
28621 | // Rather the truncating try to do the compare on vXi16 or vXi32. | |||
28622 | // Shift the high down filling with sign bits. | |||
28623 | High = getTargetVShiftByConstNode(X86ISD::VSRAI, dl, ExVT, Mul, 8, DAG); | |||
28624 | // Fill all 16 bits with the sign bit from the low. | |||
28625 | LowSign = | |||
28626 | getTargetVShiftByConstNode(X86ISD::VSHLI, dl, ExVT, Mul, 8, DAG); | |||
28627 | LowSign = getTargetVShiftByConstNode(X86ISD::VSRAI, dl, ExVT, LowSign, | |||
28628 | 15, DAG); | |||
28629 | SetccVT = OvfVT; | |||
28630 | if (!Subtarget.hasBWI()) { | |||
28631 | // We can't do a vXi16 compare so sign extend to v16i32. | |||
28632 | High = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v16i32, High); | |||
28633 | LowSign = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v16i32, LowSign); | |||
28634 | } | |||
28635 | } else { | |||
28636 | // Otherwise do the compare at vXi8. | |||
28637 | High = getTargetVShiftByConstNode(X86ISD::VSRLI, dl, ExVT, Mul, 8, DAG); | |||
28638 | High = DAG.getNode(ISD::TRUNCATE, dl, VT, High); | |||
28639 | LowSign = | |||
28640 | DAG.getNode(ISD::SRA, dl, VT, Low, DAG.getConstant(7, dl, VT)); | |||
28641 | } | |||
28642 | ||||
28643 | Ovf = DAG.getSetCC(dl, SetccVT, LowSign, High, ISD::SETNE); | |||
28644 | } else { | |||
28645 | SDValue High = | |||
28646 | getTargetVShiftByConstNode(X86ISD::VSRLI, dl, ExVT, Mul, 8, DAG); | |||
28647 | if (OvfVT.getVectorElementType() == MVT::i1 && | |||
28648 | (Subtarget.hasBWI() || Subtarget.canExtendTo512DQ())) { | |||
28649 | // Rather the truncating try to do the compare on vXi16 or vXi32. | |||
28650 | SetccVT = OvfVT; | |||
28651 | if (!Subtarget.hasBWI()) { | |||
28652 | // We can't do a vXi16 compare so sign extend to v16i32. | |||
28653 | High = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v16i32, High); | |||
28654 | } | |||
28655 | } else { | |||
28656 | // Otherwise do the compare at vXi8. | |||
28657 | High = DAG.getNode(ISD::TRUNCATE, dl, VT, High); | |||
28658 | } | |||
28659 | ||||
28660 | Ovf = | |||
28661 | DAG.getSetCC(dl, SetccVT, High, | |||
28662 | DAG.getConstant(0, dl, High.getValueType()), ISD::SETNE); | |||
28663 | } | |||
28664 | ||||
28665 | Ovf = DAG.getSExtOrTrunc(Ovf, dl, OvfVT); | |||
28666 | ||||
28667 | return DAG.getMergeValues({Low, Ovf}, dl); | |||
28668 | } | |||
28669 | ||||
28670 | SDValue Low; | |||
28671 | SDValue High = | |||
28672 | LowervXi8MulWithUNPCK(A, B, dl, VT, IsSigned, Subtarget, DAG, &Low); | |||
28673 | ||||
28674 | SDValue Ovf; | |||
28675 | if (IsSigned) { | |||
28676 | // SMULO overflows if the high bits don't match the sign of the low. | |||
28677 | SDValue LowSign = | |||
28678 | DAG.getNode(ISD::SRA, dl, VT, Low, DAG.getConstant(7, dl, VT)); | |||
28679 | Ovf = DAG.getSetCC(dl, SetccVT, LowSign, High, ISD::SETNE); | |||
28680 | } else { | |||
28681 | // UMULO overflows if the high bits are non-zero. | |||
28682 | Ovf = | |||
28683 | DAG.getSetCC(dl, SetccVT, High, DAG.getConstant(0, dl, VT), ISD::SETNE); | |||
28684 | } | |||
28685 | ||||
28686 | Ovf = DAG.getSExtOrTrunc(Ovf, dl, OvfVT); | |||
28687 | ||||
28688 | return DAG.getMergeValues({Low, Ovf}, dl); | |||
28689 | } | |||
28690 | ||||
28691 | SDValue X86TargetLowering::LowerWin64_i128OP(SDValue Op, SelectionDAG &DAG) const { | |||
28692 | assert(Subtarget.isTargetWin64() && "Unexpected target")(static_cast <bool> (Subtarget.isTargetWin64() && "Unexpected target") ? void (0) : __assert_fail ("Subtarget.isTargetWin64() && \"Unexpected target\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28692, __extension__ __PRETTY_FUNCTION__)); | |||
28693 | EVT VT = Op.getValueType(); | |||
28694 | assert(VT.isInteger() && VT.getSizeInBits() == 128 &&(static_cast <bool> (VT.isInteger() && VT.getSizeInBits () == 128 && "Unexpected return type for lowering") ? void (0) : __assert_fail ("VT.isInteger() && VT.getSizeInBits() == 128 && \"Unexpected return type for lowering\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28695, __extension__ __PRETTY_FUNCTION__)) | |||
28695 | "Unexpected return type for lowering")(static_cast <bool> (VT.isInteger() && VT.getSizeInBits () == 128 && "Unexpected return type for lowering") ? void (0) : __assert_fail ("VT.isInteger() && VT.getSizeInBits() == 128 && \"Unexpected return type for lowering\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28695, __extension__ __PRETTY_FUNCTION__)); | |||
28696 | ||||
28697 | RTLIB::Libcall LC; | |||
28698 | bool isSigned; | |||
28699 | switch (Op->getOpcode()) { | |||
28700 | default: llvm_unreachable("Unexpected request for libcall!")::llvm::llvm_unreachable_internal("Unexpected request for libcall!" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28700); | |||
28701 | case ISD::SDIV: isSigned = true; LC = RTLIB::SDIV_I128; break; | |||
28702 | case ISD::UDIV: isSigned = false; LC = RTLIB::UDIV_I128; break; | |||
28703 | case ISD::SREM: isSigned = true; LC = RTLIB::SREM_I128; break; | |||
28704 | case ISD::UREM: isSigned = false; LC = RTLIB::UREM_I128; break; | |||
28705 | } | |||
28706 | ||||
28707 | SDLoc dl(Op); | |||
28708 | SDValue InChain = DAG.getEntryNode(); | |||
28709 | ||||
28710 | TargetLowering::ArgListTy Args; | |||
28711 | TargetLowering::ArgListEntry Entry; | |||
28712 | for (unsigned i = 0, e = Op->getNumOperands(); i != e; ++i) { | |||
28713 | EVT ArgVT = Op->getOperand(i).getValueType(); | |||
28714 | assert(ArgVT.isInteger() && ArgVT.getSizeInBits() == 128 &&(static_cast <bool> (ArgVT.isInteger() && ArgVT .getSizeInBits() == 128 && "Unexpected argument type for lowering" ) ? void (0) : __assert_fail ("ArgVT.isInteger() && ArgVT.getSizeInBits() == 128 && \"Unexpected argument type for lowering\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28715, __extension__ __PRETTY_FUNCTION__)) | |||
28715 | "Unexpected argument type for lowering")(static_cast <bool> (ArgVT.isInteger() && ArgVT .getSizeInBits() == 128 && "Unexpected argument type for lowering" ) ? void (0) : __assert_fail ("ArgVT.isInteger() && ArgVT.getSizeInBits() == 128 && \"Unexpected argument type for lowering\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28715, __extension__ __PRETTY_FUNCTION__)); | |||
28716 | SDValue StackPtr = DAG.CreateStackTemporary(ArgVT, 16); | |||
28717 | int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex(); | |||
28718 | MachinePointerInfo MPI = | |||
28719 | MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI); | |||
28720 | Entry.Node = StackPtr; | |||
28721 | InChain = | |||
28722 | DAG.getStore(InChain, dl, Op->getOperand(i), StackPtr, MPI, Align(16)); | |||
28723 | Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); | |||
28724 | Entry.Ty = PointerType::get(ArgTy,0); | |||
28725 | Entry.IsSExt = false; | |||
28726 | Entry.IsZExt = false; | |||
28727 | Args.push_back(Entry); | |||
28728 | } | |||
28729 | ||||
28730 | SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC), | |||
28731 | getPointerTy(DAG.getDataLayout())); | |||
28732 | ||||
28733 | TargetLowering::CallLoweringInfo CLI(DAG); | |||
28734 | CLI.setDebugLoc(dl) | |||
28735 | .setChain(InChain) | |||
28736 | .setLibCallee( | |||
28737 | getLibcallCallingConv(LC), | |||
28738 | static_cast<EVT>(MVT::v2i64).getTypeForEVT(*DAG.getContext()), Callee, | |||
28739 | std::move(Args)) | |||
28740 | .setInRegister() | |||
28741 | .setSExtResult(isSigned) | |||
28742 | .setZExtResult(!isSigned); | |||
28743 | ||||
28744 | std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI); | |||
28745 | return DAG.getBitcast(VT, CallInfo.first); | |||
28746 | } | |||
28747 | ||||
28748 | SDValue X86TargetLowering::LowerWin64_FP_TO_INT128(SDValue Op, | |||
28749 | SelectionDAG &DAG, | |||
28750 | SDValue &Chain) const { | |||
28751 | assert(Subtarget.isTargetWin64() && "Unexpected target")(static_cast <bool> (Subtarget.isTargetWin64() && "Unexpected target") ? void (0) : __assert_fail ("Subtarget.isTargetWin64() && \"Unexpected target\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28751, __extension__ __PRETTY_FUNCTION__)); | |||
28752 | EVT VT = Op.getValueType(); | |||
28753 | bool IsStrict = Op->isStrictFPOpcode(); | |||
28754 | ||||
28755 | SDValue Arg = Op.getOperand(IsStrict ? 1 : 0); | |||
28756 | EVT ArgVT = Arg.getValueType(); | |||
28757 | ||||
28758 | assert(VT.isInteger() && VT.getSizeInBits() == 128 &&(static_cast <bool> (VT.isInteger() && VT.getSizeInBits () == 128 && "Unexpected return type for lowering") ? void (0) : __assert_fail ("VT.isInteger() && VT.getSizeInBits() == 128 && \"Unexpected return type for lowering\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28759, __extension__ __PRETTY_FUNCTION__)) | |||
28759 | "Unexpected return type for lowering")(static_cast <bool> (VT.isInteger() && VT.getSizeInBits () == 128 && "Unexpected return type for lowering") ? void (0) : __assert_fail ("VT.isInteger() && VT.getSizeInBits() == 128 && \"Unexpected return type for lowering\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28759, __extension__ __PRETTY_FUNCTION__)); | |||
28760 | ||||
28761 | RTLIB::Libcall LC; | |||
28762 | if (Op->getOpcode() == ISD::FP_TO_SINT || | |||
28763 | Op->getOpcode() == ISD::STRICT_FP_TO_SINT) | |||
28764 | LC = RTLIB::getFPTOSINT(ArgVT, VT); | |||
28765 | else | |||
28766 | LC = RTLIB::getFPTOUINT(ArgVT, VT); | |||
28767 | assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected request for libcall!")(static_cast <bool> (LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected request for libcall!") ? void (0) : __assert_fail ("LC != RTLIB::UNKNOWN_LIBCALL && \"Unexpected request for libcall!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28767, __extension__ __PRETTY_FUNCTION__)); | |||
28768 | ||||
28769 | SDLoc dl(Op); | |||
28770 | MakeLibCallOptions CallOptions; | |||
28771 | Chain = IsStrict ? Op.getOperand(0) : DAG.getEntryNode(); | |||
28772 | ||||
28773 | SDValue Result; | |||
28774 | // Expect the i128 argument returned as a v2i64 in xmm0, cast back to the | |||
28775 | // expected VT (i128). | |||
28776 | std::tie(Result, Chain) = | |||
28777 | makeLibCall(DAG, LC, MVT::v2i64, Arg, CallOptions, dl, Chain); | |||
28778 | Result = DAG.getBitcast(VT, Result); | |||
28779 | return Result; | |||
28780 | } | |||
28781 | ||||
28782 | SDValue X86TargetLowering::LowerWin64_INT128_TO_FP(SDValue Op, | |||
28783 | SelectionDAG &DAG) const { | |||
28784 | assert(Subtarget.isTargetWin64() && "Unexpected target")(static_cast <bool> (Subtarget.isTargetWin64() && "Unexpected target") ? void (0) : __assert_fail ("Subtarget.isTargetWin64() && \"Unexpected target\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28784, __extension__ __PRETTY_FUNCTION__)); | |||
28785 | EVT VT = Op.getValueType(); | |||
28786 | bool IsStrict = Op->isStrictFPOpcode(); | |||
28787 | ||||
28788 | SDValue Arg = Op.getOperand(IsStrict ? 1 : 0); | |||
28789 | EVT ArgVT = Arg.getValueType(); | |||
28790 | ||||
28791 | assert(ArgVT.isInteger() && ArgVT.getSizeInBits() == 128 &&(static_cast <bool> (ArgVT.isInteger() && ArgVT .getSizeInBits() == 128 && "Unexpected argument type for lowering" ) ? void (0) : __assert_fail ("ArgVT.isInteger() && ArgVT.getSizeInBits() == 128 && \"Unexpected argument type for lowering\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28792, __extension__ __PRETTY_FUNCTION__)) | |||
28792 | "Unexpected argument type for lowering")(static_cast <bool> (ArgVT.isInteger() && ArgVT .getSizeInBits() == 128 && "Unexpected argument type for lowering" ) ? void (0) : __assert_fail ("ArgVT.isInteger() && ArgVT.getSizeInBits() == 128 && \"Unexpected argument type for lowering\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28792, __extension__ __PRETTY_FUNCTION__)); | |||
28793 | ||||
28794 | RTLIB::Libcall LC; | |||
28795 | if (Op->getOpcode() == ISD::SINT_TO_FP || | |||
28796 | Op->getOpcode() == ISD::STRICT_SINT_TO_FP) | |||
28797 | LC = RTLIB::getSINTTOFP(ArgVT, VT); | |||
28798 | else | |||
28799 | LC = RTLIB::getUINTTOFP(ArgVT, VT); | |||
28800 | assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected request for libcall!")(static_cast <bool> (LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected request for libcall!") ? void (0) : __assert_fail ("LC != RTLIB::UNKNOWN_LIBCALL && \"Unexpected request for libcall!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28800, __extension__ __PRETTY_FUNCTION__)); | |||
28801 | ||||
28802 | SDLoc dl(Op); | |||
28803 | MakeLibCallOptions CallOptions; | |||
28804 | SDValue Chain = IsStrict ? Op.getOperand(0) : DAG.getEntryNode(); | |||
28805 | ||||
28806 | // Pass the i128 argument as an indirect argument on the stack. | |||
28807 | SDValue StackPtr = DAG.CreateStackTemporary(ArgVT, 16); | |||
28808 | int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex(); | |||
28809 | MachinePointerInfo MPI = | |||
28810 | MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI); | |||
28811 | Chain = DAG.getStore(Chain, dl, Arg, StackPtr, MPI, Align(16)); | |||
28812 | ||||
28813 | SDValue Result; | |||
28814 | std::tie(Result, Chain) = | |||
28815 | makeLibCall(DAG, LC, VT, StackPtr, CallOptions, dl, Chain); | |||
28816 | return IsStrict ? DAG.getMergeValues({Result, Chain}, dl) : Result; | |||
28817 | } | |||
28818 | ||||
28819 | // Return true if the required (according to Opcode) shift-imm form is natively | |||
28820 | // supported by the Subtarget | |||
28821 | static bool SupportedVectorShiftWithImm(MVT VT, const X86Subtarget &Subtarget, | |||
28822 | unsigned Opcode) { | |||
28823 | if (VT.getScalarSizeInBits() < 16) | |||
28824 | return false; | |||
28825 | ||||
28826 | if (VT.is512BitVector() && Subtarget.hasAVX512() && | |||
28827 | (VT.getScalarSizeInBits() > 16 || Subtarget.hasBWI())) | |||
28828 | return true; | |||
28829 | ||||
28830 | bool LShift = (VT.is128BitVector() && Subtarget.hasSSE2()) || | |||
28831 | (VT.is256BitVector() && Subtarget.hasInt256()); | |||
28832 | ||||
28833 | bool AShift = LShift && (Subtarget.hasAVX512() || | |||
28834 | (VT != MVT::v2i64 && VT != MVT::v4i64)); | |||
28835 | return (Opcode == ISD::SRA) ? AShift : LShift; | |||
28836 | } | |||
28837 | ||||
28838 | // The shift amount is a variable, but it is the same for all vector lanes. | |||
28839 | // These instructions are defined together with shift-immediate. | |||
28840 | static | |||
28841 | bool SupportedVectorShiftWithBaseAmnt(MVT VT, const X86Subtarget &Subtarget, | |||
28842 | unsigned Opcode) { | |||
28843 | return SupportedVectorShiftWithImm(VT, Subtarget, Opcode); | |||
28844 | } | |||
28845 | ||||
28846 | // Return true if the required (according to Opcode) variable-shift form is | |||
28847 | // natively supported by the Subtarget | |||
28848 | static bool SupportedVectorVarShift(MVT VT, const X86Subtarget &Subtarget, | |||
28849 | unsigned Opcode) { | |||
28850 | ||||
28851 | if (!Subtarget.hasInt256() || VT.getScalarSizeInBits() < 16) | |||
28852 | return false; | |||
28853 | ||||
28854 | // vXi16 supported only on AVX-512, BWI | |||
28855 | if (VT.getScalarSizeInBits() == 16 && !Subtarget.hasBWI()) | |||
28856 | return false; | |||
28857 | ||||
28858 | if (Subtarget.hasAVX512()) | |||
28859 | return true; | |||
28860 | ||||
28861 | bool LShift = VT.is128BitVector() || VT.is256BitVector(); | |||
28862 | bool AShift = LShift && VT != MVT::v2i64 && VT != MVT::v4i64; | |||
28863 | return (Opcode == ISD::SRA) ? AShift : LShift; | |||
28864 | } | |||
28865 | ||||
28866 | static SDValue LowerScalarImmediateShift(SDValue Op, SelectionDAG &DAG, | |||
28867 | const X86Subtarget &Subtarget) { | |||
28868 | MVT VT = Op.getSimpleValueType(); | |||
28869 | SDLoc dl(Op); | |||
28870 | SDValue R = Op.getOperand(0); | |||
28871 | SDValue Amt = Op.getOperand(1); | |||
28872 | unsigned X86Opc = getTargetVShiftUniformOpcode(Op.getOpcode(), false); | |||
28873 | ||||
28874 | auto ArithmeticShiftRight64 = [&](uint64_t ShiftAmt) { | |||
28875 | assert((VT == MVT::v2i64 || VT == MVT::v4i64) && "Unexpected SRA type")(static_cast <bool> ((VT == MVT::v2i64 || VT == MVT::v4i64 ) && "Unexpected SRA type") ? void (0) : __assert_fail ("(VT == MVT::v2i64 || VT == MVT::v4i64) && \"Unexpected SRA type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28875, __extension__ __PRETTY_FUNCTION__)); | |||
28876 | MVT ExVT = MVT::getVectorVT(MVT::i32, VT.getVectorNumElements() * 2); | |||
28877 | SDValue Ex = DAG.getBitcast(ExVT, R); | |||
28878 | ||||
28879 | // ashr(R, 63) === cmp_slt(R, 0) | |||
28880 | if (ShiftAmt == 63 && Subtarget.hasSSE42()) { | |||
28881 | assert((VT != MVT::v4i64 || Subtarget.hasInt256()) &&(static_cast <bool> ((VT != MVT::v4i64 || Subtarget.hasInt256 ()) && "Unsupported PCMPGT op") ? void (0) : __assert_fail ("(VT != MVT::v4i64 || Subtarget.hasInt256()) && \"Unsupported PCMPGT op\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28882, __extension__ __PRETTY_FUNCTION__)) | |||
28882 | "Unsupported PCMPGT op")(static_cast <bool> ((VT != MVT::v4i64 || Subtarget.hasInt256 ()) && "Unsupported PCMPGT op") ? void (0) : __assert_fail ("(VT != MVT::v4i64 || Subtarget.hasInt256()) && \"Unsupported PCMPGT op\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28882, __extension__ __PRETTY_FUNCTION__)); | |||
28883 | return DAG.getNode(X86ISD::PCMPGT, dl, VT, DAG.getConstant(0, dl, VT), R); | |||
28884 | } | |||
28885 | ||||
28886 | if (ShiftAmt >= 32) { | |||
28887 | // Splat sign to upper i32 dst, and SRA upper i32 src to lower i32. | |||
28888 | SDValue Upper = | |||
28889 | getTargetVShiftByConstNode(X86ISD::VSRAI, dl, ExVT, Ex, 31, DAG); | |||
28890 | SDValue Lower = getTargetVShiftByConstNode(X86ISD::VSRAI, dl, ExVT, Ex, | |||
28891 | ShiftAmt - 32, DAG); | |||
28892 | if (VT == MVT::v2i64) | |||
28893 | Ex = DAG.getVectorShuffle(ExVT, dl, Upper, Lower, {5, 1, 7, 3}); | |||
28894 | if (VT == MVT::v4i64) | |||
28895 | Ex = DAG.getVectorShuffle(ExVT, dl, Upper, Lower, | |||
28896 | {9, 1, 11, 3, 13, 5, 15, 7}); | |||
28897 | } else { | |||
28898 | // SRA upper i32, SRL whole i64 and select lower i32. | |||
28899 | SDValue Upper = getTargetVShiftByConstNode(X86ISD::VSRAI, dl, ExVT, Ex, | |||
28900 | ShiftAmt, DAG); | |||
28901 | SDValue Lower = | |||
28902 | getTargetVShiftByConstNode(X86ISD::VSRLI, dl, VT, R, ShiftAmt, DAG); | |||
28903 | Lower = DAG.getBitcast(ExVT, Lower); | |||
28904 | if (VT == MVT::v2i64) | |||
28905 | Ex = DAG.getVectorShuffle(ExVT, dl, Upper, Lower, {4, 1, 6, 3}); | |||
28906 | if (VT == MVT::v4i64) | |||
28907 | Ex = DAG.getVectorShuffle(ExVT, dl, Upper, Lower, | |||
28908 | {8, 1, 10, 3, 12, 5, 14, 7}); | |||
28909 | } | |||
28910 | return DAG.getBitcast(VT, Ex); | |||
28911 | }; | |||
28912 | ||||
28913 | // Optimize shl/srl/sra with constant shift amount. | |||
28914 | APInt APIntShiftAmt; | |||
28915 | if (!X86::isConstantSplat(Amt, APIntShiftAmt)) | |||
28916 | return SDValue(); | |||
28917 | ||||
28918 | // If the shift amount is out of range, return undef. | |||
28919 | if (APIntShiftAmt.uge(VT.getScalarSizeInBits())) | |||
28920 | return DAG.getUNDEF(VT); | |||
28921 | ||||
28922 | uint64_t ShiftAmt = APIntShiftAmt.getZExtValue(); | |||
28923 | ||||
28924 | if (SupportedVectorShiftWithImm(VT, Subtarget, Op.getOpcode())) | |||
28925 | return getTargetVShiftByConstNode(X86Opc, dl, VT, R, ShiftAmt, DAG); | |||
28926 | ||||
28927 | // i64 SRA needs to be performed as partial shifts. | |||
28928 | if (((!Subtarget.hasXOP() && VT == MVT::v2i64) || | |||
28929 | (Subtarget.hasInt256() && VT == MVT::v4i64)) && | |||
28930 | Op.getOpcode() == ISD::SRA) | |||
28931 | return ArithmeticShiftRight64(ShiftAmt); | |||
28932 | ||||
28933 | if (VT == MVT::v16i8 || (Subtarget.hasInt256() && VT == MVT::v32i8) || | |||
28934 | (Subtarget.hasBWI() && VT == MVT::v64i8)) { | |||
28935 | unsigned NumElts = VT.getVectorNumElements(); | |||
28936 | MVT ShiftVT = MVT::getVectorVT(MVT::i16, NumElts / 2); | |||
28937 | ||||
28938 | // Simple i8 add case | |||
28939 | if (Op.getOpcode() == ISD::SHL && ShiftAmt == 1) { | |||
28940 | // R may be undef at run-time, but (shl R, 1) must be an even number (LSB | |||
28941 | // must be 0). (add undef, undef) however can be any value. To make this | |||
28942 | // safe, we must freeze R to ensure that register allocation uses the same | |||
28943 | // register for an undefined value. This ensures that the result will | |||
28944 | // still be even and preserves the original semantics. | |||
28945 | R = DAG.getNode(ISD::FREEZE, dl, VT, R); | |||
28946 | return DAG.getNode(ISD::ADD, dl, VT, R, R); | |||
28947 | } | |||
28948 | ||||
28949 | // ashr(R, 7) === cmp_slt(R, 0) | |||
28950 | if (Op.getOpcode() == ISD::SRA && ShiftAmt == 7) { | |||
28951 | SDValue Zeros = DAG.getConstant(0, dl, VT); | |||
28952 | if (VT.is512BitVector()) { | |||
28953 | assert(VT == MVT::v64i8 && "Unexpected element type!")(static_cast <bool> (VT == MVT::v64i8 && "Unexpected element type!" ) ? void (0) : __assert_fail ("VT == MVT::v64i8 && \"Unexpected element type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28953, __extension__ __PRETTY_FUNCTION__)); | |||
28954 | SDValue CMP = DAG.getSetCC(dl, MVT::v64i1, Zeros, R, ISD::SETGT); | |||
28955 | return DAG.getNode(ISD::SIGN_EXTEND, dl, VT, CMP); | |||
28956 | } | |||
28957 | return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R); | |||
28958 | } | |||
28959 | ||||
28960 | // XOP can shift v16i8 directly instead of as shift v8i16 + mask. | |||
28961 | if (VT == MVT::v16i8 && Subtarget.hasXOP()) | |||
28962 | return SDValue(); | |||
28963 | ||||
28964 | if (Op.getOpcode() == ISD::SHL) { | |||
28965 | // Make a large shift. | |||
28966 | SDValue SHL = getTargetVShiftByConstNode(X86ISD::VSHLI, dl, ShiftVT, R, | |||
28967 | ShiftAmt, DAG); | |||
28968 | SHL = DAG.getBitcast(VT, SHL); | |||
28969 | // Zero out the rightmost bits. | |||
28970 | APInt Mask = APInt::getHighBitsSet(8, 8 - ShiftAmt); | |||
28971 | return DAG.getNode(ISD::AND, dl, VT, SHL, DAG.getConstant(Mask, dl, VT)); | |||
28972 | } | |||
28973 | if (Op.getOpcode() == ISD::SRL) { | |||
28974 | // Make a large shift. | |||
28975 | SDValue SRL = getTargetVShiftByConstNode(X86ISD::VSRLI, dl, ShiftVT, R, | |||
28976 | ShiftAmt, DAG); | |||
28977 | SRL = DAG.getBitcast(VT, SRL); | |||
28978 | // Zero out the leftmost bits. | |||
28979 | APInt Mask = APInt::getLowBitsSet(8, 8 - ShiftAmt); | |||
28980 | return DAG.getNode(ISD::AND, dl, VT, SRL, DAG.getConstant(Mask, dl, VT)); | |||
28981 | } | |||
28982 | if (Op.getOpcode() == ISD::SRA) { | |||
28983 | // ashr(R, Amt) === sub(xor(lshr(R, Amt), Mask), Mask) | |||
28984 | SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt); | |||
28985 | ||||
28986 | SDValue Mask = DAG.getConstant(128 >> ShiftAmt, dl, VT); | |||
28987 | Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask); | |||
28988 | Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask); | |||
28989 | return Res; | |||
28990 | } | |||
28991 | llvm_unreachable("Unknown shift opcode.")::llvm::llvm_unreachable_internal("Unknown shift opcode.", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 28991); | |||
28992 | } | |||
28993 | ||||
28994 | return SDValue(); | |||
28995 | } | |||
28996 | ||||
28997 | static SDValue LowerScalarVariableShift(SDValue Op, SelectionDAG &DAG, | |||
28998 | const X86Subtarget &Subtarget) { | |||
28999 | MVT VT = Op.getSimpleValueType(); | |||
29000 | SDLoc dl(Op); | |||
29001 | SDValue R = Op.getOperand(0); | |||
29002 | SDValue Amt = Op.getOperand(1); | |||
29003 | unsigned Opcode = Op.getOpcode(); | |||
29004 | unsigned X86OpcI = getTargetVShiftUniformOpcode(Opcode, false); | |||
29005 | unsigned X86OpcV = getTargetVShiftUniformOpcode(Opcode, true); | |||
29006 | ||||
29007 | if (SDValue BaseShAmt = DAG.getSplatValue(Amt)) { | |||
29008 | if (SupportedVectorShiftWithBaseAmnt(VT, Subtarget, Opcode)) { | |||
29009 | MVT EltVT = VT.getVectorElementType(); | |||
29010 | assert(EltVT.bitsLE(MVT::i64) && "Unexpected element type!")(static_cast <bool> (EltVT.bitsLE(MVT::i64) && "Unexpected element type!" ) ? void (0) : __assert_fail ("EltVT.bitsLE(MVT::i64) && \"Unexpected element type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 29010, __extension__ __PRETTY_FUNCTION__)); | |||
29011 | if (EltVT != MVT::i64 && EltVT.bitsGT(MVT::i32)) | |||
29012 | BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, BaseShAmt); | |||
29013 | else if (EltVT.bitsLT(MVT::i32)) | |||
29014 | BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, BaseShAmt); | |||
29015 | ||||
29016 | return getTargetVShiftNode(X86OpcI, dl, VT, R, BaseShAmt, Subtarget, DAG); | |||
29017 | } | |||
29018 | ||||
29019 | // vXi8 shifts - shift as v8i16 + mask result. | |||
29020 | if (((VT == MVT::v16i8 && !Subtarget.canExtendTo512DQ()) || | |||
29021 | (VT == MVT::v32i8 && !Subtarget.canExtendTo512BW()) || | |||
29022 | VT == MVT::v64i8) && | |||
29023 | !Subtarget.hasXOP()) { | |||
29024 | unsigned NumElts = VT.getVectorNumElements(); | |||
29025 | MVT ExtVT = MVT::getVectorVT(MVT::i16, NumElts / 2); | |||
29026 | if (SupportedVectorShiftWithBaseAmnt(ExtVT, Subtarget, Opcode)) { | |||
29027 | unsigned LogicalOp = (Opcode == ISD::SHL ? ISD::SHL : ISD::SRL); | |||
29028 | unsigned LogicalX86Op = getTargetVShiftUniformOpcode(LogicalOp, false); | |||
29029 | BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, BaseShAmt); | |||
29030 | ||||
29031 | // Create the mask using vXi16 shifts. For shift-rights we need to move | |||
29032 | // the upper byte down before splatting the vXi8 mask. | |||
29033 | SDValue BitMask = DAG.getConstant(-1, dl, ExtVT); | |||
29034 | BitMask = getTargetVShiftNode(LogicalX86Op, dl, ExtVT, BitMask, | |||
29035 | BaseShAmt, Subtarget, DAG); | |||
29036 | if (Opcode != ISD::SHL) | |||
29037 | BitMask = getTargetVShiftByConstNode(LogicalX86Op, dl, ExtVT, BitMask, | |||
29038 | 8, DAG); | |||
29039 | BitMask = DAG.getBitcast(VT, BitMask); | |||
29040 | BitMask = DAG.getVectorShuffle(VT, dl, BitMask, BitMask, | |||
29041 | SmallVector<int, 64>(NumElts, 0)); | |||
29042 | ||||
29043 | SDValue Res = getTargetVShiftNode(LogicalX86Op, dl, ExtVT, | |||
29044 | DAG.getBitcast(ExtVT, R), BaseShAmt, | |||
29045 | Subtarget, DAG); | |||
29046 | Res = DAG.getBitcast(VT, Res); | |||
29047 | Res = DAG.getNode(ISD::AND, dl, VT, Res, BitMask); | |||
29048 | ||||
29049 | if (Opcode == ISD::SRA) { | |||
29050 | // ashr(R, Amt) === sub(xor(lshr(R, Amt), SignMask), SignMask) | |||
29051 | // SignMask = lshr(SignBit, Amt) - safe to do this with PSRLW. | |||
29052 | SDValue SignMask = DAG.getConstant(0x8080, dl, ExtVT); | |||
29053 | SignMask = getTargetVShiftNode(LogicalX86Op, dl, ExtVT, SignMask, | |||
29054 | BaseShAmt, Subtarget, DAG); | |||
29055 | SignMask = DAG.getBitcast(VT, SignMask); | |||
29056 | Res = DAG.getNode(ISD::XOR, dl, VT, Res, SignMask); | |||
29057 | Res = DAG.getNode(ISD::SUB, dl, VT, Res, SignMask); | |||
29058 | } | |||
29059 | return Res; | |||
29060 | } | |||
29061 | } | |||
29062 | } | |||
29063 | ||||
29064 | // Check cases (mainly 32-bit) where i64 is expanded into high and low parts. | |||
29065 | if (VT == MVT::v2i64 && Amt.getOpcode() == ISD::BITCAST && | |||
29066 | Amt.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) { | |||
29067 | Amt = Amt.getOperand(0); | |||
29068 | unsigned Ratio = 64 / Amt.getScalarValueSizeInBits(); | |||
29069 | std::vector<SDValue> Vals(Ratio); | |||
29070 | for (unsigned i = 0; i != Ratio; ++i) | |||
29071 | Vals[i] = Amt.getOperand(i); | |||
29072 | for (unsigned i = Ratio, e = Amt.getNumOperands(); i != e; i += Ratio) { | |||
29073 | for (unsigned j = 0; j != Ratio; ++j) | |||
29074 | if (Vals[j] != Amt.getOperand(i + j)) | |||
29075 | return SDValue(); | |||
29076 | } | |||
29077 | ||||
29078 | if (SupportedVectorShiftWithBaseAmnt(VT, Subtarget, Op.getOpcode())) | |||
29079 | return DAG.getNode(X86OpcV, dl, VT, R, Op.getOperand(1)); | |||
29080 | } | |||
29081 | return SDValue(); | |||
29082 | } | |||
29083 | ||||
29084 | // Convert a shift/rotate left amount to a multiplication scale factor. | |||
29085 | static SDValue convertShiftLeftToScale(SDValue Amt, const SDLoc &dl, | |||
29086 | const X86Subtarget &Subtarget, | |||
29087 | SelectionDAG &DAG) { | |||
29088 | MVT VT = Amt.getSimpleValueType(); | |||
29089 | if (!(VT == MVT::v8i16 || VT == MVT::v4i32 || | |||
29090 | (Subtarget.hasInt256() && VT == MVT::v16i16) || | |||
29091 | (Subtarget.hasVBMI2() && VT == MVT::v32i16) || | |||
29092 | (!Subtarget.hasAVX512() && VT == MVT::v16i8))) | |||
29093 | return SDValue(); | |||
29094 | ||||
29095 | if (ISD::isBuildVectorOfConstantSDNodes(Amt.getNode())) { | |||
29096 | SmallVector<SDValue, 8> Elts; | |||
29097 | MVT SVT = VT.getVectorElementType(); | |||
29098 | unsigned SVTBits = SVT.getSizeInBits(); | |||
29099 | APInt One(SVTBits, 1); | |||
29100 | unsigned NumElems = VT.getVectorNumElements(); | |||
29101 | ||||
29102 | for (unsigned i = 0; i != NumElems; ++i) { | |||
29103 | SDValue Op = Amt->getOperand(i); | |||
29104 | if (Op->isUndef()) { | |||
29105 | Elts.push_back(Op); | |||
29106 | continue; | |||
29107 | } | |||
29108 | ||||
29109 | ConstantSDNode *ND = cast<ConstantSDNode>(Op); | |||
29110 | APInt C(SVTBits, ND->getZExtValue()); | |||
29111 | uint64_t ShAmt = C.getZExtValue(); | |||
29112 | if (ShAmt >= SVTBits) { | |||
29113 | Elts.push_back(DAG.getUNDEF(SVT)); | |||
29114 | continue; | |||
29115 | } | |||
29116 | Elts.push_back(DAG.getConstant(One.shl(ShAmt), dl, SVT)); | |||
29117 | } | |||
29118 | return DAG.getBuildVector(VT, dl, Elts); | |||
29119 | } | |||
29120 | ||||
29121 | // If the target doesn't support variable shifts, use either FP conversion | |||
29122 | // or integer multiplication to avoid shifting each element individually. | |||
29123 | if (VT == MVT::v4i32) { | |||
29124 | Amt = DAG.getNode(ISD::SHL, dl, VT, Amt, DAG.getConstant(23, dl, VT)); | |||
29125 | Amt = DAG.getNode(ISD::ADD, dl, VT, Amt, | |||
29126 | DAG.getConstant(0x3f800000U, dl, VT)); | |||
29127 | Amt = DAG.getBitcast(MVT::v4f32, Amt); | |||
29128 | return DAG.getNode(ISD::FP_TO_SINT, dl, VT, Amt); | |||
29129 | } | |||
29130 | ||||
29131 | // AVX2 can more effectively perform this as a zext/trunc to/from v8i32. | |||
29132 | if (VT == MVT::v8i16 && !Subtarget.hasAVX2()) { | |||
29133 | SDValue Z = DAG.getConstant(0, dl, VT); | |||
29134 | SDValue Lo = DAG.getBitcast(MVT::v4i32, getUnpackl(DAG, dl, VT, Amt, Z)); | |||
29135 | SDValue Hi = DAG.getBitcast(MVT::v4i32, getUnpackh(DAG, dl, VT, Amt, Z)); | |||
29136 | Lo = convertShiftLeftToScale(Lo, dl, Subtarget, DAG); | |||
29137 | Hi = convertShiftLeftToScale(Hi, dl, Subtarget, DAG); | |||
29138 | if (Subtarget.hasSSE41()) | |||
29139 | return DAG.getNode(X86ISD::PACKUS, dl, VT, Lo, Hi); | |||
29140 | ||||
29141 | return DAG.getVectorShuffle(VT, dl, DAG.getBitcast(VT, Lo), | |||
29142 | DAG.getBitcast(VT, Hi), | |||
29143 | {0, 2, 4, 6, 8, 10, 12, 14}); | |||
29144 | } | |||
29145 | ||||
29146 | return SDValue(); | |||
29147 | } | |||
29148 | ||||
29149 | static SDValue LowerShift(SDValue Op, const X86Subtarget &Subtarget, | |||
29150 | SelectionDAG &DAG) { | |||
29151 | MVT VT = Op.getSimpleValueType(); | |||
29152 | SDLoc dl(Op); | |||
29153 | SDValue R = Op.getOperand(0); | |||
29154 | SDValue Amt = Op.getOperand(1); | |||
29155 | unsigned EltSizeInBits = VT.getScalarSizeInBits(); | |||
29156 | bool ConstantAmt = ISD::isBuildVectorOfConstantSDNodes(Amt.getNode()); | |||
29157 | ||||
29158 | unsigned Opc = Op.getOpcode(); | |||
29159 | unsigned X86OpcV = getTargetVShiftUniformOpcode(Opc, true); | |||
29160 | unsigned X86OpcI = getTargetVShiftUniformOpcode(Opc, false); | |||
29161 | ||||
29162 | assert(VT.isVector() && "Custom lowering only for vector shifts!")(static_cast <bool> (VT.isVector() && "Custom lowering only for vector shifts!" ) ? void (0) : __assert_fail ("VT.isVector() && \"Custom lowering only for vector shifts!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 29162, __extension__ __PRETTY_FUNCTION__)); | |||
29163 | assert(Subtarget.hasSSE2() && "Only custom lower when we have SSE2!")(static_cast <bool> (Subtarget.hasSSE2() && "Only custom lower when we have SSE2!" ) ? void (0) : __assert_fail ("Subtarget.hasSSE2() && \"Only custom lower when we have SSE2!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 29163, __extension__ __PRETTY_FUNCTION__)); | |||
29164 | ||||
29165 | if (SDValue V = LowerScalarImmediateShift(Op, DAG, Subtarget)) | |||
29166 | return V; | |||
29167 | ||||
29168 | if (SDValue V = LowerScalarVariableShift(Op, DAG, Subtarget)) | |||
29169 | return V; | |||
29170 | ||||
29171 | if (SupportedVectorVarShift(VT, Subtarget, Opc)) | |||
29172 | return Op; | |||
29173 | ||||
29174 | // XOP has 128-bit variable logical/arithmetic shifts. | |||
29175 | // +ve/-ve Amt = shift left/right. | |||
29176 | if (Subtarget.hasXOP() && (VT == MVT::v2i64 || VT == MVT::v4i32 || | |||
29177 | VT == MVT::v8i16 || VT == MVT::v16i8)) { | |||
29178 | if (Opc == ISD::SRL || Opc == ISD::SRA) { | |||
29179 | SDValue Zero = DAG.getConstant(0, dl, VT); | |||
29180 | Amt = DAG.getNode(ISD::SUB, dl, VT, Zero, Amt); | |||
29181 | } | |||
29182 | if (Opc == ISD::SHL || Opc == ISD::SRL) | |||
29183 | return DAG.getNode(X86ISD::VPSHL, dl, VT, R, Amt); | |||
29184 | if (Opc == ISD::SRA) | |||
29185 | return DAG.getNode(X86ISD::VPSHA, dl, VT, R, Amt); | |||
29186 | } | |||
29187 | ||||
29188 | // 2i64 vector logical shifts can efficiently avoid scalarization - do the | |||
29189 | // shifts per-lane and then shuffle the partial results back together. | |||
29190 | if (VT == MVT::v2i64 && Opc != ISD::SRA) { | |||
29191 | // Splat the shift amounts so the scalar shifts above will catch it. | |||
29192 | SDValue Amt0 = DAG.getVectorShuffle(VT, dl, Amt, Amt, {0, 0}); | |||
29193 | SDValue Amt1 = DAG.getVectorShuffle(VT, dl, Amt, Amt, {1, 1}); | |||
29194 | SDValue R0 = DAG.getNode(Opc, dl, VT, R, Amt0); | |||
29195 | SDValue R1 = DAG.getNode(Opc, dl, VT, R, Amt1); | |||
29196 | return DAG.getVectorShuffle(VT, dl, R0, R1, {0, 3}); | |||
29197 | } | |||
29198 | ||||
29199 | // i64 vector arithmetic shift can be emulated with the transform: | |||
29200 | // M = lshr(SIGN_MASK, Amt) | |||
29201 | // ashr(R, Amt) === sub(xor(lshr(R, Amt), M), M) | |||
29202 | if ((VT == MVT::v2i64 || (VT == MVT::v4i64 && Subtarget.hasInt256())) && | |||
29203 | Opc == ISD::SRA) { | |||
29204 | SDValue S = DAG.getConstant(APInt::getSignMask(64), dl, VT); | |||
29205 | SDValue M = DAG.getNode(ISD::SRL, dl, VT, S, Amt); | |||
29206 | R = DAG.getNode(ISD::SRL, dl, VT, R, Amt); | |||
29207 | R = DAG.getNode(ISD::XOR, dl, VT, R, M); | |||
29208 | R = DAG.getNode(ISD::SUB, dl, VT, R, M); | |||
29209 | return R; | |||
29210 | } | |||
29211 | ||||
29212 | // If possible, lower this shift as a sequence of two shifts by | |||
29213 | // constant plus a BLENDing shuffle instead of scalarizing it. | |||
29214 | // Example: | |||
29215 | // (v4i32 (srl A, (build_vector < X, Y, Y, Y>))) | |||
29216 | // | |||
29217 | // Could be rewritten as: | |||
29218 | // (v4i32 (MOVSS (srl A, <Y,Y,Y,Y>), (srl A, <X,X,X,X>))) | |||
29219 | // | |||
29220 | // The advantage is that the two shifts from the example would be | |||
29221 | // lowered as X86ISD::VSRLI nodes in parallel before blending. | |||
29222 | if (ConstantAmt && (VT == MVT::v8i16 || VT == MVT::v4i32 || | |||
29223 | (VT == MVT::v16i16 && Subtarget.hasInt256()))) { | |||
29224 | SDValue Amt1, Amt2; | |||
29225 | unsigned NumElts = VT.getVectorNumElements(); | |||
29226 | SmallVector<int, 8> ShuffleMask; | |||
29227 | for (unsigned i = 0; i != NumElts; ++i) { | |||
29228 | SDValue A = Amt->getOperand(i); | |||
29229 | if (A.isUndef()) { | |||
29230 | ShuffleMask.push_back(SM_SentinelUndef); | |||
29231 | continue; | |||
29232 | } | |||
29233 | if (!Amt1 || Amt1 == A) { | |||
29234 | ShuffleMask.push_back(i); | |||
29235 | Amt1 = A; | |||
29236 | continue; | |||
29237 | } | |||
29238 | if (!Amt2 || Amt2 == A) { | |||
29239 | ShuffleMask.push_back(i + NumElts); | |||
29240 | Amt2 = A; | |||
29241 | continue; | |||
29242 | } | |||
29243 | break; | |||
29244 | } | |||
29245 | ||||
29246 | // Only perform this blend if we can perform it without loading a mask. | |||
29247 | if (ShuffleMask.size() == NumElts && Amt1 && Amt2 && | |||
29248 | (VT != MVT::v16i16 || | |||
29249 | is128BitLaneRepeatedShuffleMask(VT, ShuffleMask)) && | |||
29250 | (VT == MVT::v4i32 || Subtarget.hasSSE41() || Opc != ISD::SHL || | |||
29251 | canWidenShuffleElements(ShuffleMask))) { | |||
29252 | auto *Cst1 = dyn_cast<ConstantSDNode>(Amt1); | |||
29253 | auto *Cst2 = dyn_cast<ConstantSDNode>(Amt2); | |||
29254 | if (Cst1 && Cst2 && Cst1->getAPIntValue().ult(EltSizeInBits) && | |||
29255 | Cst2->getAPIntValue().ult(EltSizeInBits)) { | |||
29256 | SDValue Shift1 = getTargetVShiftByConstNode(X86OpcI, dl, VT, R, | |||
29257 | Cst1->getZExtValue(), DAG); | |||
29258 | SDValue Shift2 = getTargetVShiftByConstNode(X86OpcI, dl, VT, R, | |||
29259 | Cst2->getZExtValue(), DAG); | |||
29260 | return DAG.getVectorShuffle(VT, dl, Shift1, Shift2, ShuffleMask); | |||
29261 | } | |||
29262 | } | |||
29263 | } | |||
29264 | ||||
29265 | // If possible, lower this packed shift into a vector multiply instead of | |||
29266 | // expanding it into a sequence of scalar shifts. | |||
29267 | if (Opc == ISD::SHL) | |||
29268 | if (SDValue Scale = convertShiftLeftToScale(Amt, dl, Subtarget, DAG)) | |||
29269 | return DAG.getNode(ISD::MUL, dl, VT, R, Scale); | |||
29270 | ||||
29271 | // Constant ISD::SRL can be performed efficiently on vXi16 vectors as we | |||
29272 | // can replace with ISD::MULHU, creating scale factor from (NumEltBits - Amt). | |||
29273 | if (Opc == ISD::SRL && ConstantAmt && | |||
29274 | (VT == MVT::v8i16 || (VT == MVT::v16i16 && Subtarget.hasInt256()))) { | |||
29275 | SDValue EltBits = DAG.getConstant(EltSizeInBits, dl, VT); | |||
29276 | SDValue RAmt = DAG.getNode(ISD::SUB, dl, VT, EltBits, Amt); | |||
29277 | if (SDValue Scale = convertShiftLeftToScale(RAmt, dl, Subtarget, DAG)) { | |||
29278 | SDValue Zero = DAG.getConstant(0, dl, VT); | |||
29279 | SDValue ZAmt = DAG.getSetCC(dl, VT, Amt, Zero, ISD::SETEQ); | |||
29280 | SDValue Res = DAG.getNode(ISD::MULHU, dl, VT, R, Scale); | |||
29281 | return DAG.getSelect(dl, VT, ZAmt, R, Res); | |||
29282 | } | |||
29283 | } | |||
29284 | ||||
29285 | // Constant ISD::SRA can be performed efficiently on vXi16 vectors as we | |||
29286 | // can replace with ISD::MULHS, creating scale factor from (NumEltBits - Amt). | |||
29287 | // TODO: Special case handling for shift by 0/1, really we can afford either | |||
29288 | // of these cases in pre-SSE41/XOP/AVX512 but not both. | |||
29289 | if (Opc == ISD::SRA && ConstantAmt && | |||
29290 | (VT == MVT::v8i16 || (VT == MVT::v16i16 && Subtarget.hasInt256())) && | |||
29291 | ((Subtarget.hasSSE41() && !Subtarget.hasXOP() && | |||
29292 | !Subtarget.hasAVX512()) || | |||
29293 | DAG.isKnownNeverZero(Amt))) { | |||
29294 | SDValue EltBits = DAG.getConstant(EltSizeInBits, dl, VT); | |||
29295 | SDValue RAmt = DAG.getNode(ISD::SUB, dl, VT, EltBits, Amt); | |||
29296 | if (SDValue Scale = convertShiftLeftToScale(RAmt, dl, Subtarget, DAG)) { | |||
29297 | SDValue Amt0 = | |||
29298 | DAG.getSetCC(dl, VT, Amt, DAG.getConstant(0, dl, VT), ISD::SETEQ); | |||
29299 | SDValue Amt1 = | |||
29300 | DAG.getSetCC(dl, VT, Amt, DAG.getConstant(1, dl, VT), ISD::SETEQ); | |||
29301 | SDValue Sra1 = | |||
29302 | getTargetVShiftByConstNode(X86ISD::VSRAI, dl, VT, R, 1, DAG); | |||
29303 | SDValue Res = DAG.getNode(ISD::MULHS, dl, VT, R, Scale); | |||
29304 | Res = DAG.getSelect(dl, VT, Amt0, R, Res); | |||
29305 | return DAG.getSelect(dl, VT, Amt1, Sra1, Res); | |||
29306 | } | |||
29307 | } | |||
29308 | ||||
29309 | // v4i32 Non Uniform Shifts. | |||
29310 | // If the shift amount is constant we can shift each lane using the SSE2 | |||
29311 | // immediate shifts, else we need to zero-extend each lane to the lower i64 | |||
29312 | // and shift using the SSE2 variable shifts. | |||
29313 | // The separate results can then be blended together. | |||
29314 | if (VT == MVT::v4i32) { | |||
29315 | SDValue Amt0, Amt1, Amt2, Amt3; | |||
29316 | if (ConstantAmt) { | |||
29317 | Amt0 = DAG.getVectorShuffle(VT, dl, Amt, DAG.getUNDEF(VT), {0, 0, 0, 0}); | |||
29318 | Amt1 = DAG.getVectorShuffle(VT, dl, Amt, DAG.getUNDEF(VT), {1, 1, 1, 1}); | |||
29319 | Amt2 = DAG.getVectorShuffle(VT, dl, Amt, DAG.getUNDEF(VT), {2, 2, 2, 2}); | |||
29320 | Amt3 = DAG.getVectorShuffle(VT, dl, Amt, DAG.getUNDEF(VT), {3, 3, 3, 3}); | |||
29321 | } else { | |||
29322 | // The SSE2 shifts use the lower i64 as the same shift amount for | |||
29323 | // all lanes and the upper i64 is ignored. On AVX we're better off | |||
29324 | // just zero-extending, but for SSE just duplicating the top 16-bits is | |||
29325 | // cheaper and has the same effect for out of range values. | |||
29326 | if (Subtarget.hasAVX()) { | |||
29327 | SDValue Z = DAG.getConstant(0, dl, VT); | |||
29328 | Amt0 = DAG.getVectorShuffle(VT, dl, Amt, Z, {0, 4, -1, -1}); | |||
29329 | Amt1 = DAG.getVectorShuffle(VT, dl, Amt, Z, {1, 5, -1, -1}); | |||
29330 | Amt2 = DAG.getVectorShuffle(VT, dl, Amt, Z, {2, 6, -1, -1}); | |||
29331 | Amt3 = DAG.getVectorShuffle(VT, dl, Amt, Z, {3, 7, -1, -1}); | |||
29332 | } else { | |||
29333 | SDValue Amt01 = DAG.getBitcast(MVT::v8i16, Amt); | |||
29334 | SDValue Amt23 = DAG.getVectorShuffle(MVT::v8i16, dl, Amt01, Amt01, | |||
29335 | {4, 5, 6, 7, -1, -1, -1, -1}); | |||
29336 | Amt0 = DAG.getVectorShuffle(MVT::v8i16, dl, Amt01, Amt01, | |||
29337 | {0, 1, 1, 1, -1, -1, -1, -1}); | |||
29338 | Amt1 = DAG.getVectorShuffle(MVT::v8i16, dl, Amt01, Amt01, | |||
29339 | {2, 3, 3, 3, -1, -1, -1, -1}); | |||
29340 | Amt2 = DAG.getVectorShuffle(MVT::v8i16, dl, Amt23, Amt23, | |||
29341 | {0, 1, 1, 1, -1, -1, -1, -1}); | |||
29342 | Amt3 = DAG.getVectorShuffle(MVT::v8i16, dl, Amt23, Amt23, | |||
29343 | {2, 3, 3, 3, -1, -1, -1, -1}); | |||
29344 | } | |||
29345 | } | |||
29346 | ||||
29347 | unsigned ShOpc = ConstantAmt ? Opc : X86OpcV; | |||
29348 | SDValue R0 = DAG.getNode(ShOpc, dl, VT, R, DAG.getBitcast(VT, Amt0)); | |||
29349 | SDValue R1 = DAG.getNode(ShOpc, dl, VT, R, DAG.getBitcast(VT, Amt1)); | |||
29350 | SDValue R2 = DAG.getNode(ShOpc, dl, VT, R, DAG.getBitcast(VT, Amt2)); | |||
29351 | SDValue R3 = DAG.getNode(ShOpc, dl, VT, R, DAG.getBitcast(VT, Amt3)); | |||
29352 | ||||
29353 | // Merge the shifted lane results optimally with/without PBLENDW. | |||
29354 | // TODO - ideally shuffle combining would handle this. | |||
29355 | if (Subtarget.hasSSE41()) { | |||
29356 | SDValue R02 = DAG.getVectorShuffle(VT, dl, R0, R2, {0, -1, 6, -1}); | |||
29357 | SDValue R13 = DAG.getVectorShuffle(VT, dl, R1, R3, {-1, 1, -1, 7}); | |||
29358 | return DAG.getVectorShuffle(VT, dl, R02, R13, {0, 5, 2, 7}); | |||
29359 | } | |||
29360 | SDValue R01 = DAG.getVectorShuffle(VT, dl, R0, R1, {0, -1, -1, 5}); | |||
29361 | SDValue R23 = DAG.getVectorShuffle(VT, dl, R2, R3, {2, -1, -1, 7}); | |||
29362 | return DAG.getVectorShuffle(VT, dl, R01, R23, {0, 3, 4, 7}); | |||
29363 | } | |||
29364 | ||||
29365 | // It's worth extending once and using the vXi16/vXi32 shifts for smaller | |||
29366 | // types, but without AVX512 the extra overheads to get from vXi8 to vXi32 | |||
29367 | // make the existing SSE solution better. | |||
29368 | // NOTE: We honor prefered vector width before promoting to 512-bits. | |||
29369 | if ((Subtarget.hasInt256() && VT == MVT::v8i16) || | |||
29370 | (Subtarget.canExtendTo512DQ() && VT == MVT::v16i16) || | |||
29371 | (Subtarget.canExtendTo512DQ() && VT == MVT::v16i8) || | |||
29372 | (Subtarget.canExtendTo512BW() && VT == MVT::v32i8) || | |||
29373 | (Subtarget.hasBWI() && Subtarget.hasVLX() && VT == MVT::v16i8)) { | |||
29374 | assert((!Subtarget.hasBWI() || VT == MVT::v32i8 || VT == MVT::v16i8) &&(static_cast <bool> ((!Subtarget.hasBWI() || VT == MVT:: v32i8 || VT == MVT::v16i8) && "Unexpected vector type" ) ? void (0) : __assert_fail ("(!Subtarget.hasBWI() || VT == MVT::v32i8 || VT == MVT::v16i8) && \"Unexpected vector type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 29375, __extension__ __PRETTY_FUNCTION__)) | |||
29375 | "Unexpected vector type")(static_cast <bool> ((!Subtarget.hasBWI() || VT == MVT:: v32i8 || VT == MVT::v16i8) && "Unexpected vector type" ) ? void (0) : __assert_fail ("(!Subtarget.hasBWI() || VT == MVT::v32i8 || VT == MVT::v16i8) && \"Unexpected vector type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 29375, __extension__ __PRETTY_FUNCTION__)); | |||
29376 | MVT EvtSVT = Subtarget.hasBWI() ? MVT::i16 : MVT::i32; | |||
29377 | MVT ExtVT = MVT::getVectorVT(EvtSVT, VT.getVectorNumElements()); | |||
29378 | unsigned ExtOpc = Opc == ISD::SRA ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; | |||
29379 | R = DAG.getNode(ExtOpc, dl, ExtVT, R); | |||
29380 | Amt = DAG.getNode(ISD::ZERO_EXTEND, dl, ExtVT, Amt); | |||
29381 | return DAG.getNode(ISD::TRUNCATE, dl, VT, | |||
29382 | DAG.getNode(Opc, dl, ExtVT, R, Amt)); | |||
29383 | } | |||
29384 | ||||
29385 | // Constant ISD::SRA/SRL can be performed efficiently on vXi8 vectors as we | |||
29386 | // extend to vXi16 to perform a MUL scale effectively as a MUL_LOHI. | |||
29387 | if (ConstantAmt && (Opc == ISD::SRA || Opc == ISD::SRL) && | |||
29388 | (VT == MVT::v16i8 || (VT == MVT::v32i8 && Subtarget.hasInt256()) || | |||
29389 | (VT == MVT::v64i8 && Subtarget.hasBWI())) && | |||
29390 | !Subtarget.hasXOP()) { | |||
29391 | int NumElts = VT.getVectorNumElements(); | |||
29392 | SDValue Cst8 = DAG.getTargetConstant(8, dl, MVT::i8); | |||
29393 | ||||
29394 | // Extend constant shift amount to vXi16 (it doesn't matter if the type | |||
29395 | // isn't legal). | |||
29396 | MVT ExVT = MVT::getVectorVT(MVT::i16, NumElts); | |||
29397 | Amt = DAG.getZExtOrTrunc(Amt, dl, ExVT); | |||
29398 | Amt = DAG.getNode(ISD::SUB, dl, ExVT, DAG.getConstant(8, dl, ExVT), Amt); | |||
29399 | Amt = DAG.getNode(ISD::SHL, dl, ExVT, DAG.getConstant(1, dl, ExVT), Amt); | |||
29400 | assert(ISD::isBuildVectorOfConstantSDNodes(Amt.getNode()) &&(static_cast <bool> (ISD::isBuildVectorOfConstantSDNodes (Amt.getNode()) && "Constant build vector expected") ? void (0) : __assert_fail ("ISD::isBuildVectorOfConstantSDNodes(Amt.getNode()) && \"Constant build vector expected\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 29401, __extension__ __PRETTY_FUNCTION__)) | |||
29401 | "Constant build vector expected")(static_cast <bool> (ISD::isBuildVectorOfConstantSDNodes (Amt.getNode()) && "Constant build vector expected") ? void (0) : __assert_fail ("ISD::isBuildVectorOfConstantSDNodes(Amt.getNode()) && \"Constant build vector expected\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 29401, __extension__ __PRETTY_FUNCTION__)); | |||
29402 | ||||
29403 | if (VT == MVT::v16i8 && Subtarget.hasInt256()) { | |||
29404 | R = Opc == ISD::SRA ? DAG.getSExtOrTrunc(R, dl, ExVT) | |||
29405 | : DAG.getZExtOrTrunc(R, dl, ExVT); | |||
29406 | R = DAG.getNode(ISD::MUL, dl, ExVT, R, Amt); | |||
29407 | R = DAG.getNode(X86ISD::VSRLI, dl, ExVT, R, Cst8); | |||
29408 | return DAG.getZExtOrTrunc(R, dl, VT); | |||
29409 | } | |||
29410 | ||||
29411 | SmallVector<SDValue, 16> LoAmt, HiAmt; | |||
29412 | for (int i = 0; i != NumElts; i += 16) { | |||
29413 | for (int j = 0; j != 8; ++j) { | |||
29414 | LoAmt.push_back(Amt.getOperand(i + j)); | |||
29415 | HiAmt.push_back(Amt.getOperand(i + j + 8)); | |||
29416 | } | |||
29417 | } | |||
29418 | ||||
29419 | MVT VT16 = MVT::getVectorVT(MVT::i16, NumElts / 2); | |||
29420 | SDValue LoA = DAG.getBuildVector(VT16, dl, LoAmt); | |||
29421 | SDValue HiA = DAG.getBuildVector(VT16, dl, HiAmt); | |||
29422 | ||||
29423 | SDValue LoR = DAG.getBitcast(VT16, getUnpackl(DAG, dl, VT, R, R)); | |||
29424 | SDValue HiR = DAG.getBitcast(VT16, getUnpackh(DAG, dl, VT, R, R)); | |||
29425 | LoR = DAG.getNode(X86OpcI, dl, VT16, LoR, Cst8); | |||
29426 | HiR = DAG.getNode(X86OpcI, dl, VT16, HiR, Cst8); | |||
29427 | LoR = DAG.getNode(ISD::MUL, dl, VT16, LoR, LoA); | |||
29428 | HiR = DAG.getNode(ISD::MUL, dl, VT16, HiR, HiA); | |||
29429 | LoR = DAG.getNode(X86ISD::VSRLI, dl, VT16, LoR, Cst8); | |||
29430 | HiR = DAG.getNode(X86ISD::VSRLI, dl, VT16, HiR, Cst8); | |||
29431 | return DAG.getNode(X86ISD::PACKUS, dl, VT, LoR, HiR); | |||
29432 | } | |||
29433 | ||||
29434 | if (VT == MVT::v16i8 || | |||
29435 | (VT == MVT::v32i8 && Subtarget.hasInt256() && !Subtarget.hasXOP()) || | |||
29436 | (VT == MVT::v64i8 && Subtarget.hasBWI())) { | |||
29437 | MVT ExtVT = MVT::getVectorVT(MVT::i16, VT.getVectorNumElements() / 2); | |||
29438 | ||||
29439 | auto SignBitSelect = [&](MVT SelVT, SDValue Sel, SDValue V0, SDValue V1) { | |||
29440 | if (VT.is512BitVector()) { | |||
29441 | // On AVX512BW targets we make use of the fact that VSELECT lowers | |||
29442 | // to a masked blend which selects bytes based just on the sign bit | |||
29443 | // extracted to a mask. | |||
29444 | MVT MaskVT = MVT::getVectorVT(MVT::i1, VT.getVectorNumElements()); | |||
29445 | V0 = DAG.getBitcast(VT, V0); | |||
29446 | V1 = DAG.getBitcast(VT, V1); | |||
29447 | Sel = DAG.getBitcast(VT, Sel); | |||
29448 | Sel = DAG.getSetCC(dl, MaskVT, DAG.getConstant(0, dl, VT), Sel, | |||
29449 | ISD::SETGT); | |||
29450 | return DAG.getBitcast(SelVT, DAG.getSelect(dl, VT, Sel, V0, V1)); | |||
29451 | } else if (Subtarget.hasSSE41()) { | |||
29452 | // On SSE41 targets we can use PBLENDVB which selects bytes based just | |||
29453 | // on the sign bit. | |||
29454 | V0 = DAG.getBitcast(VT, V0); | |||
29455 | V1 = DAG.getBitcast(VT, V1); | |||
29456 | Sel = DAG.getBitcast(VT, Sel); | |||
29457 | return DAG.getBitcast(SelVT, | |||
29458 | DAG.getNode(X86ISD::BLENDV, dl, VT, Sel, V0, V1)); | |||
29459 | } | |||
29460 | // On pre-SSE41 targets we test for the sign bit by comparing to | |||
29461 | // zero - a negative value will set all bits of the lanes to true | |||
29462 | // and VSELECT uses that in its OR(AND(V0,C),AND(V1,~C)) lowering. | |||
29463 | SDValue Z = DAG.getConstant(0, dl, SelVT); | |||
29464 | SDValue C = DAG.getNode(X86ISD::PCMPGT, dl, SelVT, Z, Sel); | |||
29465 | return DAG.getSelect(dl, SelVT, C, V0, V1); | |||
29466 | }; | |||
29467 | ||||
29468 | // Turn 'a' into a mask suitable for VSELECT: a = a << 5; | |||
29469 | // We can safely do this using i16 shifts as we're only interested in | |||
29470 | // the 3 lower bits of each byte. | |||
29471 | Amt = DAG.getBitcast(ExtVT, Amt); | |||
29472 | Amt = getTargetVShiftByConstNode(X86ISD::VSHLI, dl, ExtVT, Amt, 5, DAG); | |||
29473 | Amt = DAG.getBitcast(VT, Amt); | |||
29474 | ||||
29475 | if (Opc == ISD::SHL || Opc == ISD::SRL) { | |||
29476 | // r = VSELECT(r, shift(r, 4), a); | |||
29477 | SDValue M = DAG.getNode(Opc, dl, VT, R, DAG.getConstant(4, dl, VT)); | |||
29478 | R = SignBitSelect(VT, Amt, M, R); | |||
29479 | ||||
29480 | // a += a | |||
29481 | Amt = DAG.getNode(ISD::ADD, dl, VT, Amt, Amt); | |||
29482 | ||||
29483 | // r = VSELECT(r, shift(r, 2), a); | |||
29484 | M = DAG.getNode(Opc, dl, VT, R, DAG.getConstant(2, dl, VT)); | |||
29485 | R = SignBitSelect(VT, Amt, M, R); | |||
29486 | ||||
29487 | // a += a | |||
29488 | Amt = DAG.getNode(ISD::ADD, dl, VT, Amt, Amt); | |||
29489 | ||||
29490 | // return VSELECT(r, shift(r, 1), a); | |||
29491 | M = DAG.getNode(Opc, dl, VT, R, DAG.getConstant(1, dl, VT)); | |||
29492 | R = SignBitSelect(VT, Amt, M, R); | |||
29493 | return R; | |||
29494 | } | |||
29495 | ||||
29496 | if (Opc == ISD::SRA) { | |||
29497 | // For SRA we need to unpack each byte to the higher byte of a i16 vector | |||
29498 | // so we can correctly sign extend. We don't care what happens to the | |||
29499 | // lower byte. | |||
29500 | SDValue ALo = getUnpackl(DAG, dl, VT, DAG.getUNDEF(VT), Amt); | |||
29501 | SDValue AHi = getUnpackh(DAG, dl, VT, DAG.getUNDEF(VT), Amt); | |||
29502 | SDValue RLo = getUnpackl(DAG, dl, VT, DAG.getUNDEF(VT), R); | |||
29503 | SDValue RHi = getUnpackh(DAG, dl, VT, DAG.getUNDEF(VT), R); | |||
29504 | ALo = DAG.getBitcast(ExtVT, ALo); | |||
29505 | AHi = DAG.getBitcast(ExtVT, AHi); | |||
29506 | RLo = DAG.getBitcast(ExtVT, RLo); | |||
29507 | RHi = DAG.getBitcast(ExtVT, RHi); | |||
29508 | ||||
29509 | // r = VSELECT(r, shift(r, 4), a); | |||
29510 | SDValue MLo = getTargetVShiftByConstNode(X86OpcI, dl, ExtVT, RLo, 4, DAG); | |||
29511 | SDValue MHi = getTargetVShiftByConstNode(X86OpcI, dl, ExtVT, RHi, 4, DAG); | |||
29512 | RLo = SignBitSelect(ExtVT, ALo, MLo, RLo); | |||
29513 | RHi = SignBitSelect(ExtVT, AHi, MHi, RHi); | |||
29514 | ||||
29515 | // a += a | |||
29516 | ALo = DAG.getNode(ISD::ADD, dl, ExtVT, ALo, ALo); | |||
29517 | AHi = DAG.getNode(ISD::ADD, dl, ExtVT, AHi, AHi); | |||
29518 | ||||
29519 | // r = VSELECT(r, shift(r, 2), a); | |||
29520 | MLo = getTargetVShiftByConstNode(X86OpcI, dl, ExtVT, RLo, 2, DAG); | |||
29521 | MHi = getTargetVShiftByConstNode(X86OpcI, dl, ExtVT, RHi, 2, DAG); | |||
29522 | RLo = SignBitSelect(ExtVT, ALo, MLo, RLo); | |||
29523 | RHi = SignBitSelect(ExtVT, AHi, MHi, RHi); | |||
29524 | ||||
29525 | // a += a | |||
29526 | ALo = DAG.getNode(ISD::ADD, dl, ExtVT, ALo, ALo); | |||
29527 | AHi = DAG.getNode(ISD::ADD, dl, ExtVT, AHi, AHi); | |||
29528 | ||||
29529 | // r = VSELECT(r, shift(r, 1), a); | |||
29530 | MLo = getTargetVShiftByConstNode(X86OpcI, dl, ExtVT, RLo, 1, DAG); | |||
29531 | MHi = getTargetVShiftByConstNode(X86OpcI, dl, ExtVT, RHi, 1, DAG); | |||
29532 | RLo = SignBitSelect(ExtVT, ALo, MLo, RLo); | |||
29533 | RHi = SignBitSelect(ExtVT, AHi, MHi, RHi); | |||
29534 | ||||
29535 | // Logical shift the result back to the lower byte, leaving a zero upper | |||
29536 | // byte meaning that we can safely pack with PACKUSWB. | |||
29537 | RLo = getTargetVShiftByConstNode(X86ISD::VSRLI, dl, ExtVT, RLo, 8, DAG); | |||
29538 | RHi = getTargetVShiftByConstNode(X86ISD::VSRLI, dl, ExtVT, RHi, 8, DAG); | |||
29539 | return DAG.getNode(X86ISD::PACKUS, dl, VT, RLo, RHi); | |||
29540 | } | |||
29541 | } | |||
29542 | ||||
29543 | if (Subtarget.hasInt256() && !Subtarget.hasXOP() && VT == MVT::v16i16) { | |||
29544 | MVT ExtVT = MVT::v8i32; | |||
29545 | SDValue Z = DAG.getConstant(0, dl, VT); | |||
29546 | SDValue ALo = getUnpackl(DAG, dl, VT, Amt, Z); | |||
29547 | SDValue AHi = getUnpackh(DAG, dl, VT, Amt, Z); | |||
29548 | SDValue RLo = getUnpackl(DAG, dl, VT, Z, R); | |||
29549 | SDValue RHi = getUnpackh(DAG, dl, VT, Z, R); | |||
29550 | ALo = DAG.getBitcast(ExtVT, ALo); | |||
29551 | AHi = DAG.getBitcast(ExtVT, AHi); | |||
29552 | RLo = DAG.getBitcast(ExtVT, RLo); | |||
29553 | RHi = DAG.getBitcast(ExtVT, RHi); | |||
29554 | SDValue Lo = DAG.getNode(Opc, dl, ExtVT, RLo, ALo); | |||
29555 | SDValue Hi = DAG.getNode(Opc, dl, ExtVT, RHi, AHi); | |||
29556 | Lo = getTargetVShiftByConstNode(X86ISD::VSRLI, dl, ExtVT, Lo, 16, DAG); | |||
29557 | Hi = getTargetVShiftByConstNode(X86ISD::VSRLI, dl, ExtVT, Hi, 16, DAG); | |||
29558 | return DAG.getNode(X86ISD::PACKUS, dl, VT, Lo, Hi); | |||
29559 | } | |||
29560 | ||||
29561 | if (VT == MVT::v8i16) { | |||
29562 | // If we have a constant shift amount, the non-SSE41 path is best as | |||
29563 | // avoiding bitcasts make it easier to constant fold and reduce to PBLENDW. | |||
29564 | bool UseSSE41 = Subtarget.hasSSE41() && | |||
29565 | !ISD::isBuildVectorOfConstantSDNodes(Amt.getNode()); | |||
29566 | ||||
29567 | auto SignBitSelect = [&](SDValue Sel, SDValue V0, SDValue V1) { | |||
29568 | // On SSE41 targets we can use PBLENDVB which selects bytes based just on | |||
29569 | // the sign bit. | |||
29570 | if (UseSSE41) { | |||
29571 | MVT ExtVT = MVT::getVectorVT(MVT::i8, VT.getVectorNumElements() * 2); | |||
29572 | V0 = DAG.getBitcast(ExtVT, V0); | |||
29573 | V1 = DAG.getBitcast(ExtVT, V1); | |||
29574 | Sel = DAG.getBitcast(ExtVT, Sel); | |||
29575 | return DAG.getBitcast( | |||
29576 | VT, DAG.getNode(X86ISD::BLENDV, dl, ExtVT, Sel, V0, V1)); | |||
29577 | } | |||
29578 | // On pre-SSE41 targets we splat the sign bit - a negative value will | |||
29579 | // set all bits of the lanes to true and VSELECT uses that in | |||
29580 | // its OR(AND(V0,C),AND(V1,~C)) lowering. | |||
29581 | SDValue C = | |||
29582 | getTargetVShiftByConstNode(X86ISD::VSRAI, dl, VT, Sel, 15, DAG); | |||
29583 | return DAG.getSelect(dl, VT, C, V0, V1); | |||
29584 | }; | |||
29585 | ||||
29586 | // Turn 'a' into a mask suitable for VSELECT: a = a << 12; | |||
29587 | if (UseSSE41) { | |||
29588 | // On SSE41 targets we need to replicate the shift mask in both | |||
29589 | // bytes for PBLENDVB. | |||
29590 | Amt = DAG.getNode( | |||
29591 | ISD::OR, dl, VT, | |||
29592 | getTargetVShiftByConstNode(X86ISD::VSHLI, dl, VT, Amt, 4, DAG), | |||
29593 | getTargetVShiftByConstNode(X86ISD::VSHLI, dl, VT, Amt, 12, DAG)); | |||
29594 | } else { | |||
29595 | Amt = getTargetVShiftByConstNode(X86ISD::VSHLI, dl, VT, Amt, 12, DAG); | |||
29596 | } | |||
29597 | ||||
29598 | // r = VSELECT(r, shift(r, 8), a); | |||
29599 | SDValue M = getTargetVShiftByConstNode(X86OpcI, dl, VT, R, 8, DAG); | |||
29600 | R = SignBitSelect(Amt, M, R); | |||
29601 | ||||
29602 | // a += a | |||
29603 | Amt = DAG.getNode(ISD::ADD, dl, VT, Amt, Amt); | |||
29604 | ||||
29605 | // r = VSELECT(r, shift(r, 4), a); | |||
29606 | M = getTargetVShiftByConstNode(X86OpcI, dl, VT, R, 4, DAG); | |||
29607 | R = SignBitSelect(Amt, M, R); | |||
29608 | ||||
29609 | // a += a | |||
29610 | Amt = DAG.getNode(ISD::ADD, dl, VT, Amt, Amt); | |||
29611 | ||||
29612 | // r = VSELECT(r, shift(r, 2), a); | |||
29613 | M = getTargetVShiftByConstNode(X86OpcI, dl, VT, R, 2, DAG); | |||
29614 | R = SignBitSelect(Amt, M, R); | |||
29615 | ||||
29616 | // a += a | |||
29617 | Amt = DAG.getNode(ISD::ADD, dl, VT, Amt, Amt); | |||
29618 | ||||
29619 | // return VSELECT(r, shift(r, 1), a); | |||
29620 | M = getTargetVShiftByConstNode(X86OpcI, dl, VT, R, 1, DAG); | |||
29621 | R = SignBitSelect(Amt, M, R); | |||
29622 | return R; | |||
29623 | } | |||
29624 | ||||
29625 | // Decompose 256-bit shifts into 128-bit shifts. | |||
29626 | if (VT.is256BitVector()) | |||
29627 | return splitVectorIntBinary(Op, DAG); | |||
29628 | ||||
29629 | if (VT == MVT::v32i16 || VT == MVT::v64i8) | |||
29630 | return splitVectorIntBinary(Op, DAG); | |||
29631 | ||||
29632 | return SDValue(); | |||
29633 | } | |||
29634 | ||||
29635 | static SDValue LowerRotate(SDValue Op, const X86Subtarget &Subtarget, | |||
29636 | SelectionDAG &DAG) { | |||
29637 | MVT VT = Op.getSimpleValueType(); | |||
29638 | assert(VT.isVector() && "Custom lowering only for vector rotates!")(static_cast <bool> (VT.isVector() && "Custom lowering only for vector rotates!" ) ? void (0) : __assert_fail ("VT.isVector() && \"Custom lowering only for vector rotates!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 29638, __extension__ __PRETTY_FUNCTION__)); | |||
29639 | ||||
29640 | SDLoc DL(Op); | |||
29641 | SDValue R = Op.getOperand(0); | |||
29642 | SDValue Amt = Op.getOperand(1); | |||
29643 | unsigned Opcode = Op.getOpcode(); | |||
29644 | unsigned EltSizeInBits = VT.getScalarSizeInBits(); | |||
29645 | int NumElts = VT.getVectorNumElements(); | |||
29646 | ||||
29647 | // Check for constant splat rotation amount. | |||
29648 | APInt CstSplatValue; | |||
29649 | bool IsCstSplat = X86::isConstantSplat(Amt, CstSplatValue); | |||
29650 | ||||
29651 | // Check for splat rotate by zero. | |||
29652 | if (IsCstSplat && CstSplatValue.urem(EltSizeInBits) == 0) | |||
29653 | return R; | |||
29654 | ||||
29655 | // AVX512 implicitly uses modulo rotation amounts. | |||
29656 | if (Subtarget.hasAVX512() && 32 <= EltSizeInBits) { | |||
29657 | // Attempt to rotate by immediate. | |||
29658 | if (IsCstSplat) { | |||
29659 | unsigned RotOpc = (Opcode == ISD::ROTL ? X86ISD::VROTLI : X86ISD::VROTRI); | |||
29660 | uint64_t RotAmt = CstSplatValue.urem(EltSizeInBits); | |||
29661 | return DAG.getNode(RotOpc, DL, VT, R, | |||
29662 | DAG.getTargetConstant(RotAmt, DL, MVT::i8)); | |||
29663 | } | |||
29664 | ||||
29665 | // Else, fall-back on VPROLV/VPRORV. | |||
29666 | return Op; | |||
29667 | } | |||
29668 | ||||
29669 | // AVX512 VBMI2 vXi16 - lower to funnel shifts. | |||
29670 | if (Subtarget.hasVBMI2() && 16 == EltSizeInBits) { | |||
29671 | unsigned FunnelOpc = (Opcode == ISD::ROTL ? ISD::FSHL : ISD::FSHR); | |||
29672 | return DAG.getNode(FunnelOpc, DL, VT, R, R, Amt); | |||
29673 | } | |||
29674 | ||||
29675 | assert((Opcode == ISD::ROTL) && "Only ROTL supported")(static_cast <bool> ((Opcode == ISD::ROTL) && "Only ROTL supported" ) ? void (0) : __assert_fail ("(Opcode == ISD::ROTL) && \"Only ROTL supported\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 29675, __extension__ __PRETTY_FUNCTION__)); | |||
29676 | ||||
29677 | // XOP has 128-bit vector variable + immediate rotates. | |||
29678 | // +ve/-ve Amt = rotate left/right - just need to handle ISD::ROTL. | |||
29679 | // XOP implicitly uses modulo rotation amounts. | |||
29680 | if (Subtarget.hasXOP()) { | |||
29681 | if (VT.is256BitVector()) | |||
29682 | return splitVectorIntBinary(Op, DAG); | |||
29683 | assert(VT.is128BitVector() && "Only rotate 128-bit vectors!")(static_cast <bool> (VT.is128BitVector() && "Only rotate 128-bit vectors!" ) ? void (0) : __assert_fail ("VT.is128BitVector() && \"Only rotate 128-bit vectors!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 29683, __extension__ __PRETTY_FUNCTION__)); | |||
29684 | ||||
29685 | // Attempt to rotate by immediate. | |||
29686 | if (IsCstSplat) { | |||
29687 | uint64_t RotAmt = CstSplatValue.urem(EltSizeInBits); | |||
29688 | return DAG.getNode(X86ISD::VROTLI, DL, VT, R, | |||
29689 | DAG.getTargetConstant(RotAmt, DL, MVT::i8)); | |||
29690 | } | |||
29691 | ||||
29692 | // Use general rotate by variable (per-element). | |||
29693 | return Op; | |||
29694 | } | |||
29695 | ||||
29696 | // Split 256-bit integers on pre-AVX2 targets. | |||
29697 | if (VT.is256BitVector() && !Subtarget.hasAVX2()) | |||
29698 | return splitVectorIntBinary(Op, DAG); | |||
29699 | ||||
29700 | assert((VT == MVT::v4i32 || VT == MVT::v8i16 || VT == MVT::v16i8 ||(static_cast <bool> ((VT == MVT::v4i32 || VT == MVT::v8i16 || VT == MVT::v16i8 || ((VT == MVT::v8i32 || VT == MVT::v16i16 || VT == MVT::v32i8 || VT == MVT::v32i16) && Subtarget .hasAVX2())) && "Only vXi32/vXi16/vXi8 vector rotates supported" ) ? void (0) : __assert_fail ("(VT == MVT::v4i32 || VT == MVT::v8i16 || VT == MVT::v16i8 || ((VT == MVT::v8i32 || VT == MVT::v16i16 || VT == MVT::v32i8 || VT == MVT::v32i16) && Subtarget.hasAVX2())) && \"Only vXi32/vXi16/vXi8 vector rotates supported\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 29704, __extension__ __PRETTY_FUNCTION__)) | |||
29701 | ((VT == MVT::v8i32 || VT == MVT::v16i16 || VT == MVT::v32i8 ||(static_cast <bool> ((VT == MVT::v4i32 || VT == MVT::v8i16 || VT == MVT::v16i8 || ((VT == MVT::v8i32 || VT == MVT::v16i16 || VT == MVT::v32i8 || VT == MVT::v32i16) && Subtarget .hasAVX2())) && "Only vXi32/vXi16/vXi8 vector rotates supported" ) ? void (0) : __assert_fail ("(VT == MVT::v4i32 || VT == MVT::v8i16 || VT == MVT::v16i8 || ((VT == MVT::v8i32 || VT == MVT::v16i16 || VT == MVT::v32i8 || VT == MVT::v32i16) && Subtarget.hasAVX2())) && \"Only vXi32/vXi16/vXi8 vector rotates supported\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 29704, __extension__ __PRETTY_FUNCTION__)) | |||
29702 | VT == MVT::v32i16) &&(static_cast <bool> ((VT == MVT::v4i32 || VT == MVT::v8i16 || VT == MVT::v16i8 || ((VT == MVT::v8i32 || VT == MVT::v16i16 || VT == MVT::v32i8 || VT == MVT::v32i16) && Subtarget .hasAVX2())) && "Only vXi32/vXi16/vXi8 vector rotates supported" ) ? void (0) : __assert_fail ("(VT == MVT::v4i32 || VT == MVT::v8i16 || VT == MVT::v16i8 || ((VT == MVT::v8i32 || VT == MVT::v16i16 || VT == MVT::v32i8 || VT == MVT::v32i16) && Subtarget.hasAVX2())) && \"Only vXi32/vXi16/vXi8 vector rotates supported\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 29704, __extension__ __PRETTY_FUNCTION__)) | |||
29703 | Subtarget.hasAVX2())) &&(static_cast <bool> ((VT == MVT::v4i32 || VT == MVT::v8i16 || VT == MVT::v16i8 || ((VT == MVT::v8i32 || VT == MVT::v16i16 || VT == MVT::v32i8 || VT == MVT::v32i16) && Subtarget .hasAVX2())) && "Only vXi32/vXi16/vXi8 vector rotates supported" ) ? void (0) : __assert_fail ("(VT == MVT::v4i32 || VT == MVT::v8i16 || VT == MVT::v16i8 || ((VT == MVT::v8i32 || VT == MVT::v16i16 || VT == MVT::v32i8 || VT == MVT::v32i16) && Subtarget.hasAVX2())) && \"Only vXi32/vXi16/vXi8 vector rotates supported\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 29704, __extension__ __PRETTY_FUNCTION__)) | |||
29704 | "Only vXi32/vXi16/vXi8 vector rotates supported")(static_cast <bool> ((VT == MVT::v4i32 || VT == MVT::v8i16 || VT == MVT::v16i8 || ((VT == MVT::v8i32 || VT == MVT::v16i16 || VT == MVT::v32i8 || VT == MVT::v32i16) && Subtarget .hasAVX2())) && "Only vXi32/vXi16/vXi8 vector rotates supported" ) ? void (0) : __assert_fail ("(VT == MVT::v4i32 || VT == MVT::v8i16 || VT == MVT::v16i8 || ((VT == MVT::v8i32 || VT == MVT::v16i16 || VT == MVT::v32i8 || VT == MVT::v32i16) && Subtarget.hasAVX2())) && \"Only vXi32/vXi16/vXi8 vector rotates supported\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 29704, __extension__ __PRETTY_FUNCTION__)); | |||
29705 | ||||
29706 | // Rotate by an uniform constant - expand back to shifts. | |||
29707 | if (IsCstSplat) | |||
29708 | return SDValue(); | |||
29709 | ||||
29710 | bool IsSplatAmt = DAG.isSplatValue(Amt); | |||
29711 | ||||
29712 | // v16i8/v32i8: Split rotation into rot4/rot2/rot1 stages and select by | |||
29713 | // the amount bit. | |||
29714 | if (EltSizeInBits == 8 && !IsSplatAmt) { | |||
29715 | if (ISD::isBuildVectorOfConstantSDNodes(Amt.getNode())) | |||
29716 | return SDValue(); | |||
29717 | ||||
29718 | // We don't need ModuloAmt here as we just peek at individual bits. | |||
29719 | MVT ExtVT = MVT::getVectorVT(MVT::i16, NumElts / 2); | |||
29720 | ||||
29721 | auto SignBitSelect = [&](MVT SelVT, SDValue Sel, SDValue V0, SDValue V1) { | |||
29722 | if (Subtarget.hasSSE41()) { | |||
29723 | // On SSE41 targets we can use PBLENDVB which selects bytes based just | |||
29724 | // on the sign bit. | |||
29725 | V0 = DAG.getBitcast(VT, V0); | |||
29726 | V1 = DAG.getBitcast(VT, V1); | |||
29727 | Sel = DAG.getBitcast(VT, Sel); | |||
29728 | return DAG.getBitcast(SelVT, | |||
29729 | DAG.getNode(X86ISD::BLENDV, DL, VT, Sel, V0, V1)); | |||
29730 | } | |||
29731 | // On pre-SSE41 targets we test for the sign bit by comparing to | |||
29732 | // zero - a negative value will set all bits of the lanes to true | |||
29733 | // and VSELECT uses that in its OR(AND(V0,C),AND(V1,~C)) lowering. | |||
29734 | SDValue Z = DAG.getConstant(0, DL, SelVT); | |||
29735 | SDValue C = DAG.getNode(X86ISD::PCMPGT, DL, SelVT, Z, Sel); | |||
29736 | return DAG.getSelect(DL, SelVT, C, V0, V1); | |||
29737 | }; | |||
29738 | ||||
29739 | // Turn 'a' into a mask suitable for VSELECT: a = a << 5; | |||
29740 | // We can safely do this using i16 shifts as we're only interested in | |||
29741 | // the 3 lower bits of each byte. | |||
29742 | Amt = DAG.getBitcast(ExtVT, Amt); | |||
29743 | Amt = DAG.getNode(ISD::SHL, DL, ExtVT, Amt, DAG.getConstant(5, DL, ExtVT)); | |||
29744 | Amt = DAG.getBitcast(VT, Amt); | |||
29745 | ||||
29746 | // r = VSELECT(r, rot(r, 4), a); | |||
29747 | SDValue M; | |||
29748 | M = DAG.getNode( | |||
29749 | ISD::OR, DL, VT, | |||
29750 | DAG.getNode(ISD::SHL, DL, VT, R, DAG.getConstant(4, DL, VT)), | |||
29751 | DAG.getNode(ISD::SRL, DL, VT, R, DAG.getConstant(4, DL, VT))); | |||
29752 | R = SignBitSelect(VT, Amt, M, R); | |||
29753 | ||||
29754 | // a += a | |||
29755 | Amt = DAG.getNode(ISD::ADD, DL, VT, Amt, Amt); | |||
29756 | ||||
29757 | // r = VSELECT(r, rot(r, 2), a); | |||
29758 | M = DAG.getNode( | |||
29759 | ISD::OR, DL, VT, | |||
29760 | DAG.getNode(ISD::SHL, DL, VT, R, DAG.getConstant(2, DL, VT)), | |||
29761 | DAG.getNode(ISD::SRL, DL, VT, R, DAG.getConstant(6, DL, VT))); | |||
29762 | R = SignBitSelect(VT, Amt, M, R); | |||
29763 | ||||
29764 | // a += a | |||
29765 | Amt = DAG.getNode(ISD::ADD, DL, VT, Amt, Amt); | |||
29766 | ||||
29767 | // return VSELECT(r, rot(r, 1), a); | |||
29768 | M = DAG.getNode( | |||
29769 | ISD::OR, DL, VT, | |||
29770 | DAG.getNode(ISD::SHL, DL, VT, R, DAG.getConstant(1, DL, VT)), | |||
29771 | DAG.getNode(ISD::SRL, DL, VT, R, DAG.getConstant(7, DL, VT))); | |||
29772 | return SignBitSelect(VT, Amt, M, R); | |||
29773 | } | |||
29774 | ||||
29775 | // ISD::ROT* uses modulo rotate amounts. | |||
29776 | if (SDValue BaseRotAmt = DAG.getSplatValue(Amt)) { | |||
29777 | // If the amount is a splat, perform the modulo BEFORE the splat, | |||
29778 | // this helps LowerScalarVariableShift to remove the splat later. | |||
29779 | Amt = DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, BaseRotAmt); | |||
29780 | Amt = DAG.getNode(ISD::AND, DL, VT, Amt, | |||
29781 | DAG.getConstant(EltSizeInBits - 1, DL, VT)); | |||
29782 | Amt = DAG.getVectorShuffle(VT, DL, Amt, DAG.getUNDEF(VT), | |||
29783 | SmallVector<int>(NumElts, 0)); | |||
29784 | } else { | |||
29785 | Amt = DAG.getNode(ISD::AND, DL, VT, Amt, | |||
29786 | DAG.getConstant(EltSizeInBits - 1, DL, VT)); | |||
29787 | } | |||
29788 | ||||
29789 | bool ConstantAmt = ISD::isBuildVectorOfConstantSDNodes(Amt.getNode()); | |||
29790 | bool LegalVarShifts = SupportedVectorVarShift(VT, Subtarget, ISD::SHL) && | |||
29791 | SupportedVectorVarShift(VT, Subtarget, ISD::SRL); | |||
29792 | ||||
29793 | // Fallback for splats + all supported variable shifts. | |||
29794 | // Fallback for non-constants AVX2 vXi16 as well. | |||
29795 | if (IsSplatAmt || LegalVarShifts || (Subtarget.hasAVX2() && !ConstantAmt)) { | |||
29796 | SDValue AmtR = DAG.getConstant(EltSizeInBits, DL, VT); | |||
29797 | AmtR = DAG.getNode(ISD::SUB, DL, VT, AmtR, Amt); | |||
29798 | SDValue SHL = DAG.getNode(ISD::SHL, DL, VT, R, Amt); | |||
29799 | SDValue SRL = DAG.getNode(ISD::SRL, DL, VT, R, AmtR); | |||
29800 | return DAG.getNode(ISD::OR, DL, VT, SHL, SRL); | |||
29801 | } | |||
29802 | ||||
29803 | // As with shifts, convert the rotation amount to a multiplication factor. | |||
29804 | SDValue Scale = convertShiftLeftToScale(Amt, DL, Subtarget, DAG); | |||
29805 | assert(Scale && "Failed to convert ROTL amount to scale")(static_cast <bool> (Scale && "Failed to convert ROTL amount to scale" ) ? void (0) : __assert_fail ("Scale && \"Failed to convert ROTL amount to scale\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 29805, __extension__ __PRETTY_FUNCTION__)); | |||
29806 | ||||
29807 | // v8i16/v16i16: perform unsigned multiply hi/lo and OR the results. | |||
29808 | if (EltSizeInBits == 16) { | |||
29809 | SDValue Lo = DAG.getNode(ISD::MUL, DL, VT, R, Scale); | |||
29810 | SDValue Hi = DAG.getNode(ISD::MULHU, DL, VT, R, Scale); | |||
29811 | return DAG.getNode(ISD::OR, DL, VT, Lo, Hi); | |||
29812 | } | |||
29813 | ||||
29814 | // v4i32: make use of the PMULUDQ instruction to multiply 2 lanes of v4i32 | |||
29815 | // to v2i64 results at a time. The upper 32-bits contain the wrapped bits | |||
29816 | // that can then be OR'd with the lower 32-bits. | |||
29817 | assert(VT == MVT::v4i32 && "Only v4i32 vector rotate expected")(static_cast <bool> (VT == MVT::v4i32 && "Only v4i32 vector rotate expected" ) ? void (0) : __assert_fail ("VT == MVT::v4i32 && \"Only v4i32 vector rotate expected\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 29817, __extension__ __PRETTY_FUNCTION__)); | |||
29818 | static const int OddMask[] = {1, -1, 3, -1}; | |||
29819 | SDValue R13 = DAG.getVectorShuffle(VT, DL, R, R, OddMask); | |||
29820 | SDValue Scale13 = DAG.getVectorShuffle(VT, DL, Scale, Scale, OddMask); | |||
29821 | ||||
29822 | SDValue Res02 = DAG.getNode(X86ISD::PMULUDQ, DL, MVT::v2i64, | |||
29823 | DAG.getBitcast(MVT::v2i64, R), | |||
29824 | DAG.getBitcast(MVT::v2i64, Scale)); | |||
29825 | SDValue Res13 = DAG.getNode(X86ISD::PMULUDQ, DL, MVT::v2i64, | |||
29826 | DAG.getBitcast(MVT::v2i64, R13), | |||
29827 | DAG.getBitcast(MVT::v2i64, Scale13)); | |||
29828 | Res02 = DAG.getBitcast(VT, Res02); | |||
29829 | Res13 = DAG.getBitcast(VT, Res13); | |||
29830 | ||||
29831 | return DAG.getNode(ISD::OR, DL, VT, | |||
29832 | DAG.getVectorShuffle(VT, DL, Res02, Res13, {0, 4, 2, 6}), | |||
29833 | DAG.getVectorShuffle(VT, DL, Res02, Res13, {1, 5, 3, 7})); | |||
29834 | } | |||
29835 | ||||
29836 | /// Returns true if the operand type is exactly twice the native width, and | |||
29837 | /// the corresponding cmpxchg8b or cmpxchg16b instruction is available. | |||
29838 | /// Used to know whether to use cmpxchg8/16b when expanding atomic operations | |||
29839 | /// (otherwise we leave them alone to become __sync_fetch_and_... calls). | |||
29840 | bool X86TargetLowering::needsCmpXchgNb(Type *MemType) const { | |||
29841 | unsigned OpWidth = MemType->getPrimitiveSizeInBits(); | |||
29842 | ||||
29843 | if (OpWidth == 64) | |||
29844 | return Subtarget.hasCmpxchg8b() && !Subtarget.is64Bit(); | |||
29845 | if (OpWidth == 128) | |||
29846 | return Subtarget.hasCmpxchg16b(); | |||
29847 | ||||
29848 | return false; | |||
29849 | } | |||
29850 | ||||
29851 | bool X86TargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const { | |||
29852 | Type *MemType = SI->getValueOperand()->getType(); | |||
29853 | ||||
29854 | bool NoImplicitFloatOps = | |||
29855 | SI->getFunction()->hasFnAttribute(Attribute::NoImplicitFloat); | |||
29856 | if (MemType->getPrimitiveSizeInBits() == 64 && !Subtarget.is64Bit() && | |||
29857 | !Subtarget.useSoftFloat() && !NoImplicitFloatOps && | |||
29858 | (Subtarget.hasSSE1() || Subtarget.hasX87())) | |||
29859 | return false; | |||
29860 | ||||
29861 | return needsCmpXchgNb(MemType); | |||
29862 | } | |||
29863 | ||||
29864 | // Note: this turns large loads into lock cmpxchg8b/16b. | |||
29865 | // TODO: In 32-bit mode, use MOVLPS when SSE1 is available? | |||
29866 | TargetLowering::AtomicExpansionKind | |||
29867 | X86TargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const { | |||
29868 | Type *MemType = LI->getType(); | |||
29869 | ||||
29870 | // If this a 64 bit atomic load on a 32-bit target and SSE2 is enabled, we | |||
29871 | // can use movq to do the load. If we have X87 we can load into an 80-bit | |||
29872 | // X87 register and store it to a stack temporary. | |||
29873 | bool NoImplicitFloatOps = | |||
29874 | LI->getFunction()->hasFnAttribute(Attribute::NoImplicitFloat); | |||
29875 | if (MemType->getPrimitiveSizeInBits() == 64 && !Subtarget.is64Bit() && | |||
29876 | !Subtarget.useSoftFloat() && !NoImplicitFloatOps && | |||
29877 | (Subtarget.hasSSE1() || Subtarget.hasX87())) | |||
29878 | return AtomicExpansionKind::None; | |||
29879 | ||||
29880 | return needsCmpXchgNb(MemType) ? AtomicExpansionKind::CmpXChg | |||
29881 | : AtomicExpansionKind::None; | |||
29882 | } | |||
29883 | ||||
29884 | TargetLowering::AtomicExpansionKind | |||
29885 | X86TargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const { | |||
29886 | unsigned NativeWidth = Subtarget.is64Bit() ? 64 : 32; | |||
29887 | Type *MemType = AI->getType(); | |||
29888 | ||||
29889 | // If the operand is too big, we must see if cmpxchg8/16b is available | |||
29890 | // and default to library calls otherwise. | |||
29891 | if (MemType->getPrimitiveSizeInBits() > NativeWidth) { | |||
29892 | return needsCmpXchgNb(MemType) ? AtomicExpansionKind::CmpXChg | |||
29893 | : AtomicExpansionKind::None; | |||
29894 | } | |||
29895 | ||||
29896 | AtomicRMWInst::BinOp Op = AI->getOperation(); | |||
29897 | switch (Op) { | |||
29898 | default: | |||
29899 | llvm_unreachable("Unknown atomic operation")::llvm::llvm_unreachable_internal("Unknown atomic operation", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 29899); | |||
29900 | case AtomicRMWInst::Xchg: | |||
29901 | case AtomicRMWInst::Add: | |||
29902 | case AtomicRMWInst::Sub: | |||
29903 | // It's better to use xadd, xsub or xchg for these in all cases. | |||
29904 | return AtomicExpansionKind::None; | |||
29905 | case AtomicRMWInst::Or: | |||
29906 | case AtomicRMWInst::And: | |||
29907 | case AtomicRMWInst::Xor: | |||
29908 | // If the atomicrmw's result isn't actually used, we can just add a "lock" | |||
29909 | // prefix to a normal instruction for these operations. | |||
29910 | return !AI->use_empty() ? AtomicExpansionKind::CmpXChg | |||
29911 | : AtomicExpansionKind::None; | |||
29912 | case AtomicRMWInst::Nand: | |||
29913 | case AtomicRMWInst::Max: | |||
29914 | case AtomicRMWInst::Min: | |||
29915 | case AtomicRMWInst::UMax: | |||
29916 | case AtomicRMWInst::UMin: | |||
29917 | case AtomicRMWInst::FAdd: | |||
29918 | case AtomicRMWInst::FSub: | |||
29919 | // These always require a non-trivial set of data operations on x86. We must | |||
29920 | // use a cmpxchg loop. | |||
29921 | return AtomicExpansionKind::CmpXChg; | |||
29922 | } | |||
29923 | } | |||
29924 | ||||
29925 | LoadInst * | |||
29926 | X86TargetLowering::lowerIdempotentRMWIntoFencedLoad(AtomicRMWInst *AI) const { | |||
29927 | unsigned NativeWidth = Subtarget.is64Bit() ? 64 : 32; | |||
29928 | Type *MemType = AI->getType(); | |||
29929 | // Accesses larger than the native width are turned into cmpxchg/libcalls, so | |||
29930 | // there is no benefit in turning such RMWs into loads, and it is actually | |||
29931 | // harmful as it introduces a mfence. | |||
29932 | if (MemType->getPrimitiveSizeInBits() > NativeWidth) | |||
29933 | return nullptr; | |||
29934 | ||||
29935 | // If this is a canonical idempotent atomicrmw w/no uses, we have a better | |||
29936 | // lowering available in lowerAtomicArith. | |||
29937 | // TODO: push more cases through this path. | |||
29938 | if (auto *C = dyn_cast<ConstantInt>(AI->getValOperand())) | |||
29939 | if (AI->getOperation() == AtomicRMWInst::Or && C->isZero() && | |||
29940 | AI->use_empty()) | |||
29941 | return nullptr; | |||
29942 | ||||
29943 | IRBuilder<> Builder(AI); | |||
29944 | Module *M = Builder.GetInsertBlock()->getParent()->getParent(); | |||
29945 | auto SSID = AI->getSyncScopeID(); | |||
29946 | // We must restrict the ordering to avoid generating loads with Release or | |||
29947 | // ReleaseAcquire orderings. | |||
29948 | auto Order = AtomicCmpXchgInst::getStrongestFailureOrdering(AI->getOrdering()); | |||
29949 | ||||
29950 | // Before the load we need a fence. Here is an example lifted from | |||
29951 | // http://www.hpl.hp.com/techreports/2012/HPL-2012-68.pdf showing why a fence | |||
29952 | // is required: | |||
29953 | // Thread 0: | |||
29954 | // x.store(1, relaxed); | |||
29955 | // r1 = y.fetch_add(0, release); | |||
29956 | // Thread 1: | |||
29957 | // y.fetch_add(42, acquire); | |||
29958 | // r2 = x.load(relaxed); | |||
29959 | // r1 = r2 = 0 is impossible, but becomes possible if the idempotent rmw is | |||
29960 | // lowered to just a load without a fence. A mfence flushes the store buffer, | |||
29961 | // making the optimization clearly correct. | |||
29962 | // FIXME: it is required if isReleaseOrStronger(Order) but it is not clear | |||
29963 | // otherwise, we might be able to be more aggressive on relaxed idempotent | |||
29964 | // rmw. In practice, they do not look useful, so we don't try to be | |||
29965 | // especially clever. | |||
29966 | if (SSID == SyncScope::SingleThread) | |||
29967 | // FIXME: we could just insert an X86ISD::MEMBARRIER here, except we are at | |||
29968 | // the IR level, so we must wrap it in an intrinsic. | |||
29969 | return nullptr; | |||
29970 | ||||
29971 | if (!Subtarget.hasMFence()) | |||
29972 | // FIXME: it might make sense to use a locked operation here but on a | |||
29973 | // different cache-line to prevent cache-line bouncing. In practice it | |||
29974 | // is probably a small win, and x86 processors without mfence are rare | |||
29975 | // enough that we do not bother. | |||
29976 | return nullptr; | |||
29977 | ||||
29978 | Function *MFence = | |||
29979 | llvm::Intrinsic::getDeclaration(M, Intrinsic::x86_sse2_mfence); | |||
29980 | Builder.CreateCall(MFence, {}); | |||
29981 | ||||
29982 | // Finally we can emit the atomic load. | |||
29983 | LoadInst *Loaded = Builder.CreateAlignedLoad( | |||
29984 | AI->getType(), AI->getPointerOperand(), AI->getAlign()); | |||
29985 | Loaded->setAtomic(Order, SSID); | |||
29986 | AI->replaceAllUsesWith(Loaded); | |||
29987 | AI->eraseFromParent(); | |||
29988 | return Loaded; | |||
29989 | } | |||
29990 | ||||
29991 | bool X86TargetLowering::lowerAtomicStoreAsStoreSDNode(const StoreInst &SI) const { | |||
29992 | if (!SI.isUnordered()) | |||
29993 | return false; | |||
29994 | return ExperimentalUnorderedISEL; | |||
29995 | } | |||
29996 | bool X86TargetLowering::lowerAtomicLoadAsLoadSDNode(const LoadInst &LI) const { | |||
29997 | if (!LI.isUnordered()) | |||
29998 | return false; | |||
29999 | return ExperimentalUnorderedISEL; | |||
30000 | } | |||
30001 | ||||
30002 | ||||
30003 | /// Emit a locked operation on a stack location which does not change any | |||
30004 | /// memory location, but does involve a lock prefix. Location is chosen to be | |||
30005 | /// a) very likely accessed only by a single thread to minimize cache traffic, | |||
30006 | /// and b) definitely dereferenceable. Returns the new Chain result. | |||
30007 | static SDValue emitLockedStackOp(SelectionDAG &DAG, | |||
30008 | const X86Subtarget &Subtarget, SDValue Chain, | |||
30009 | const SDLoc &DL) { | |||
30010 | // Implementation notes: | |||
30011 | // 1) LOCK prefix creates a full read/write reordering barrier for memory | |||
30012 | // operations issued by the current processor. As such, the location | |||
30013 | // referenced is not relevant for the ordering properties of the instruction. | |||
30014 | // See: Intel® 64 and IA-32 ArchitecturesSoftware Developer’s Manual, | |||
30015 | // 8.2.3.9 Loads and Stores Are Not Reordered with Locked Instructions | |||
30016 | // 2) Using an immediate operand appears to be the best encoding choice | |||
30017 | // here since it doesn't require an extra register. | |||
30018 | // 3) OR appears to be very slightly faster than ADD. (Though, the difference | |||
30019 | // is small enough it might just be measurement noise.) | |||
30020 | // 4) When choosing offsets, there are several contributing factors: | |||
30021 | // a) If there's no redzone, we default to TOS. (We could allocate a cache | |||
30022 | // line aligned stack object to improve this case.) | |||
30023 | // b) To minimize our chances of introducing a false dependence, we prefer | |||
30024 | // to offset the stack usage from TOS slightly. | |||
30025 | // c) To minimize concerns about cross thread stack usage - in particular, | |||
30026 | // the idiomatic MyThreadPool.run([&StackVars]() {...}) pattern which | |||
30027 | // captures state in the TOS frame and accesses it from many threads - | |||
30028 | // we want to use an offset such that the offset is in a distinct cache | |||
30029 | // line from the TOS frame. | |||
30030 | // | |||
30031 | // For a general discussion of the tradeoffs and benchmark results, see: | |||
30032 | // https://shipilev.net/blog/2014/on-the-fence-with-dependencies/ | |||
30033 | ||||
30034 | auto &MF = DAG.getMachineFunction(); | |||
30035 | auto &TFL = *Subtarget.getFrameLowering(); | |||
30036 | const unsigned SPOffset = TFL.has128ByteRedZone(MF) ? -64 : 0; | |||
30037 | ||||
30038 | if (Subtarget.is64Bit()) { | |||
30039 | SDValue Zero = DAG.getTargetConstant(0, DL, MVT::i32); | |||
30040 | SDValue Ops[] = { | |||
30041 | DAG.getRegister(X86::RSP, MVT::i64), // Base | |||
30042 | DAG.getTargetConstant(1, DL, MVT::i8), // Scale | |||
30043 | DAG.getRegister(0, MVT::i64), // Index | |||
30044 | DAG.getTargetConstant(SPOffset, DL, MVT::i32), // Disp | |||
30045 | DAG.getRegister(0, MVT::i16), // Segment. | |||
30046 | Zero, | |||
30047 | Chain}; | |||
30048 | SDNode *Res = DAG.getMachineNode(X86::OR32mi8Locked, DL, MVT::i32, | |||
30049 | MVT::Other, Ops); | |||
30050 | return SDValue(Res, 1); | |||
30051 | } | |||
30052 | ||||
30053 | SDValue Zero = DAG.getTargetConstant(0, DL, MVT::i32); | |||
30054 | SDValue Ops[] = { | |||
30055 | DAG.getRegister(X86::ESP, MVT::i32), // Base | |||
30056 | DAG.getTargetConstant(1, DL, MVT::i8), // Scale | |||
30057 | DAG.getRegister(0, MVT::i32), // Index | |||
30058 | DAG.getTargetConstant(SPOffset, DL, MVT::i32), // Disp | |||
30059 | DAG.getRegister(0, MVT::i16), // Segment. | |||
30060 | Zero, | |||
30061 | Chain | |||
30062 | }; | |||
30063 | SDNode *Res = DAG.getMachineNode(X86::OR32mi8Locked, DL, MVT::i32, | |||
30064 | MVT::Other, Ops); | |||
30065 | return SDValue(Res, 1); | |||
30066 | } | |||
30067 | ||||
30068 | static SDValue LowerATOMIC_FENCE(SDValue Op, const X86Subtarget &Subtarget, | |||
30069 | SelectionDAG &DAG) { | |||
30070 | SDLoc dl(Op); | |||
30071 | AtomicOrdering FenceOrdering = | |||
30072 | static_cast<AtomicOrdering>(Op.getConstantOperandVal(1)); | |||
30073 | SyncScope::ID FenceSSID = | |||
30074 | static_cast<SyncScope::ID>(Op.getConstantOperandVal(2)); | |||
30075 | ||||
30076 | // The only fence that needs an instruction is a sequentially-consistent | |||
30077 | // cross-thread fence. | |||
30078 | if (FenceOrdering == AtomicOrdering::SequentiallyConsistent && | |||
30079 | FenceSSID == SyncScope::System) { | |||
30080 | if (Subtarget.hasMFence()) | |||
30081 | return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0)); | |||
30082 | ||||
30083 | SDValue Chain = Op.getOperand(0); | |||
30084 | return emitLockedStackOp(DAG, Subtarget, Chain, dl); | |||
30085 | } | |||
30086 | ||||
30087 | // MEMBARRIER is a compiler barrier; it codegens to a no-op. | |||
30088 | return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0)); | |||
30089 | } | |||
30090 | ||||
30091 | static SDValue LowerCMP_SWAP(SDValue Op, const X86Subtarget &Subtarget, | |||
30092 | SelectionDAG &DAG) { | |||
30093 | MVT T = Op.getSimpleValueType(); | |||
30094 | SDLoc DL(Op); | |||
30095 | unsigned Reg = 0; | |||
30096 | unsigned size = 0; | |||
30097 | switch(T.SimpleTy) { | |||
30098 | default: llvm_unreachable("Invalid value type!")::llvm::llvm_unreachable_internal("Invalid value type!", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30098); | |||
30099 | case MVT::i8: Reg = X86::AL; size = 1; break; | |||
30100 | case MVT::i16: Reg = X86::AX; size = 2; break; | |||
30101 | case MVT::i32: Reg = X86::EAX; size = 4; break; | |||
30102 | case MVT::i64: | |||
30103 | assert(Subtarget.is64Bit() && "Node not type legal!")(static_cast <bool> (Subtarget.is64Bit() && "Node not type legal!" ) ? void (0) : __assert_fail ("Subtarget.is64Bit() && \"Node not type legal!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30103, __extension__ __PRETTY_FUNCTION__)); | |||
30104 | Reg = X86::RAX; size = 8; | |||
30105 | break; | |||
30106 | } | |||
30107 | SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg, | |||
30108 | Op.getOperand(2), SDValue()); | |||
30109 | SDValue Ops[] = { cpIn.getValue(0), | |||
30110 | Op.getOperand(1), | |||
30111 | Op.getOperand(3), | |||
30112 | DAG.getTargetConstant(size, DL, MVT::i8), | |||
30113 | cpIn.getValue(1) }; | |||
30114 | SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue); | |||
30115 | MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand(); | |||
30116 | SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys, | |||
30117 | Ops, T, MMO); | |||
30118 | ||||
30119 | SDValue cpOut = | |||
30120 | DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1)); | |||
30121 | SDValue EFLAGS = DAG.getCopyFromReg(cpOut.getValue(1), DL, X86::EFLAGS, | |||
30122 | MVT::i32, cpOut.getValue(2)); | |||
30123 | SDValue Success = getSETCC(X86::COND_E, EFLAGS, DL, DAG); | |||
30124 | ||||
30125 | return DAG.getNode(ISD::MERGE_VALUES, DL, Op->getVTList(), | |||
30126 | cpOut, Success, EFLAGS.getValue(1)); | |||
30127 | } | |||
30128 | ||||
30129 | // Create MOVMSKB, taking into account whether we need to split for AVX1. | |||
30130 | static SDValue getPMOVMSKB(const SDLoc &DL, SDValue V, SelectionDAG &DAG, | |||
30131 | const X86Subtarget &Subtarget) { | |||
30132 | MVT InVT = V.getSimpleValueType(); | |||
30133 | ||||
30134 | if (InVT == MVT::v64i8) { | |||
30135 | SDValue Lo, Hi; | |||
30136 | std::tie(Lo, Hi) = DAG.SplitVector(V, DL); | |||
30137 | Lo = getPMOVMSKB(DL, Lo, DAG, Subtarget); | |||
30138 | Hi = getPMOVMSKB(DL, Hi, DAG, Subtarget); | |||
30139 | Lo = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, Lo); | |||
30140 | Hi = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Hi); | |||
30141 | Hi = DAG.getNode(ISD::SHL, DL, MVT::i64, Hi, | |||
30142 | DAG.getConstant(32, DL, MVT::i8)); | |||
30143 | return DAG.getNode(ISD::OR, DL, MVT::i64, Lo, Hi); | |||
30144 | } | |||
30145 | if (InVT == MVT::v32i8 && !Subtarget.hasInt256()) { | |||
30146 | SDValue Lo, Hi; | |||
30147 | std::tie(Lo, Hi) = DAG.SplitVector(V, DL); | |||
30148 | Lo = DAG.getNode(X86ISD::MOVMSK, DL, MVT::i32, Lo); | |||
30149 | Hi = DAG.getNode(X86ISD::MOVMSK, DL, MVT::i32, Hi); | |||
30150 | Hi = DAG.getNode(ISD::SHL, DL, MVT::i32, Hi, | |||
30151 | DAG.getConstant(16, DL, MVT::i8)); | |||
30152 | return DAG.getNode(ISD::OR, DL, MVT::i32, Lo, Hi); | |||
30153 | } | |||
30154 | ||||
30155 | return DAG.getNode(X86ISD::MOVMSK, DL, MVT::i32, V); | |||
30156 | } | |||
30157 | ||||
30158 | static SDValue LowerBITCAST(SDValue Op, const X86Subtarget &Subtarget, | |||
30159 | SelectionDAG &DAG) { | |||
30160 | SDValue Src = Op.getOperand(0); | |||
30161 | MVT SrcVT = Src.getSimpleValueType(); | |||
30162 | MVT DstVT = Op.getSimpleValueType(); | |||
30163 | ||||
30164 | // Legalize (v64i1 (bitcast i64 (X))) by splitting the i64, bitcasting each | |||
30165 | // half to v32i1 and concatenating the result. | |||
30166 | if (SrcVT == MVT::i64 && DstVT == MVT::v64i1) { | |||
30167 | assert(!Subtarget.is64Bit() && "Expected 32-bit mode")(static_cast <bool> (!Subtarget.is64Bit() && "Expected 32-bit mode" ) ? void (0) : __assert_fail ("!Subtarget.is64Bit() && \"Expected 32-bit mode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30167, __extension__ __PRETTY_FUNCTION__)); | |||
30168 | assert(Subtarget.hasBWI() && "Expected BWI target")(static_cast <bool> (Subtarget.hasBWI() && "Expected BWI target" ) ? void (0) : __assert_fail ("Subtarget.hasBWI() && \"Expected BWI target\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30168, __extension__ __PRETTY_FUNCTION__)); | |||
30169 | SDLoc dl(Op); | |||
30170 | SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Src, | |||
30171 | DAG.getIntPtrConstant(0, dl)); | |||
30172 | Lo = DAG.getBitcast(MVT::v32i1, Lo); | |||
30173 | SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Src, | |||
30174 | DAG.getIntPtrConstant(1, dl)); | |||
30175 | Hi = DAG.getBitcast(MVT::v32i1, Hi); | |||
30176 | return DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v64i1, Lo, Hi); | |||
30177 | } | |||
30178 | ||||
30179 | // Use MOVMSK for vector to scalar conversion to prevent scalarization. | |||
30180 | if ((SrcVT == MVT::v16i1 || SrcVT == MVT::v32i1) && DstVT.isScalarInteger()) { | |||
30181 | assert(!Subtarget.hasAVX512() && "Should use K-registers with AVX512")(static_cast <bool> (!Subtarget.hasAVX512() && "Should use K-registers with AVX512" ) ? void (0) : __assert_fail ("!Subtarget.hasAVX512() && \"Should use K-registers with AVX512\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30181, __extension__ __PRETTY_FUNCTION__)); | |||
30182 | MVT SExtVT = SrcVT == MVT::v16i1 ? MVT::v16i8 : MVT::v32i8; | |||
30183 | SDLoc DL(Op); | |||
30184 | SDValue V = DAG.getSExtOrTrunc(Src, DL, SExtVT); | |||
30185 | V = getPMOVMSKB(DL, V, DAG, Subtarget); | |||
30186 | return DAG.getZExtOrTrunc(V, DL, DstVT); | |||
30187 | } | |||
30188 | ||||
30189 | assert((SrcVT == MVT::v2i32 || SrcVT == MVT::v4i16 || SrcVT == MVT::v8i8 ||(static_cast <bool> ((SrcVT == MVT::v2i32 || SrcVT == MVT ::v4i16 || SrcVT == MVT::v8i8 || SrcVT == MVT::i64) && "Unexpected VT!") ? void (0) : __assert_fail ("(SrcVT == MVT::v2i32 || SrcVT == MVT::v4i16 || SrcVT == MVT::v8i8 || SrcVT == MVT::i64) && \"Unexpected VT!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30190, __extension__ __PRETTY_FUNCTION__)) | |||
30190 | SrcVT == MVT::i64) && "Unexpected VT!")(static_cast <bool> ((SrcVT == MVT::v2i32 || SrcVT == MVT ::v4i16 || SrcVT == MVT::v8i8 || SrcVT == MVT::i64) && "Unexpected VT!") ? void (0) : __assert_fail ("(SrcVT == MVT::v2i32 || SrcVT == MVT::v4i16 || SrcVT == MVT::v8i8 || SrcVT == MVT::i64) && \"Unexpected VT!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30190, __extension__ __PRETTY_FUNCTION__)); | |||
30191 | ||||
30192 | assert(Subtarget.hasSSE2() && "Requires at least SSE2!")(static_cast <bool> (Subtarget.hasSSE2() && "Requires at least SSE2!" ) ? void (0) : __assert_fail ("Subtarget.hasSSE2() && \"Requires at least SSE2!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30192, __extension__ __PRETTY_FUNCTION__)); | |||
30193 | if (!(DstVT == MVT::f64 && SrcVT == MVT::i64) && | |||
30194 | !(DstVT == MVT::x86mmx && SrcVT.isVector())) | |||
30195 | // This conversion needs to be expanded. | |||
30196 | return SDValue(); | |||
30197 | ||||
30198 | SDLoc dl(Op); | |||
30199 | if (SrcVT.isVector()) { | |||
30200 | // Widen the vector in input in the case of MVT::v2i32. | |||
30201 | // Example: from MVT::v2i32 to MVT::v4i32. | |||
30202 | MVT NewVT = MVT::getVectorVT(SrcVT.getVectorElementType(), | |||
30203 | SrcVT.getVectorNumElements() * 2); | |||
30204 | Src = DAG.getNode(ISD::CONCAT_VECTORS, dl, NewVT, Src, | |||
30205 | DAG.getUNDEF(SrcVT)); | |||
30206 | } else { | |||
30207 | assert(SrcVT == MVT::i64 && !Subtarget.is64Bit() &&(static_cast <bool> (SrcVT == MVT::i64 && !Subtarget .is64Bit() && "Unexpected source type in LowerBITCAST" ) ? void (0) : __assert_fail ("SrcVT == MVT::i64 && !Subtarget.is64Bit() && \"Unexpected source type in LowerBITCAST\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30208, __extension__ __PRETTY_FUNCTION__)) | |||
30208 | "Unexpected source type in LowerBITCAST")(static_cast <bool> (SrcVT == MVT::i64 && !Subtarget .is64Bit() && "Unexpected source type in LowerBITCAST" ) ? void (0) : __assert_fail ("SrcVT == MVT::i64 && !Subtarget.is64Bit() && \"Unexpected source type in LowerBITCAST\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30208, __extension__ __PRETTY_FUNCTION__)); | |||
30209 | Src = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Src); | |||
30210 | } | |||
30211 | ||||
30212 | MVT V2X64VT = DstVT == MVT::f64 ? MVT::v2f64 : MVT::v2i64; | |||
30213 | Src = DAG.getNode(ISD::BITCAST, dl, V2X64VT, Src); | |||
30214 | ||||
30215 | if (DstVT == MVT::x86mmx) | |||
30216 | return DAG.getNode(X86ISD::MOVDQ2Q, dl, DstVT, Src); | |||
30217 | ||||
30218 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, DstVT, Src, | |||
30219 | DAG.getIntPtrConstant(0, dl)); | |||
30220 | } | |||
30221 | ||||
30222 | /// Compute the horizontal sum of bytes in V for the elements of VT. | |||
30223 | /// | |||
30224 | /// Requires V to be a byte vector and VT to be an integer vector type with | |||
30225 | /// wider elements than V's type. The width of the elements of VT determines | |||
30226 | /// how many bytes of V are summed horizontally to produce each element of the | |||
30227 | /// result. | |||
30228 | static SDValue LowerHorizontalByteSum(SDValue V, MVT VT, | |||
30229 | const X86Subtarget &Subtarget, | |||
30230 | SelectionDAG &DAG) { | |||
30231 | SDLoc DL(V); | |||
30232 | MVT ByteVecVT = V.getSimpleValueType(); | |||
30233 | MVT EltVT = VT.getVectorElementType(); | |||
30234 | assert(ByteVecVT.getVectorElementType() == MVT::i8 &&(static_cast <bool> (ByteVecVT.getVectorElementType() == MVT::i8 && "Expected value to have byte element type." ) ? void (0) : __assert_fail ("ByteVecVT.getVectorElementType() == MVT::i8 && \"Expected value to have byte element type.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30235, __extension__ __PRETTY_FUNCTION__)) | |||
30235 | "Expected value to have byte element type.")(static_cast <bool> (ByteVecVT.getVectorElementType() == MVT::i8 && "Expected value to have byte element type." ) ? void (0) : __assert_fail ("ByteVecVT.getVectorElementType() == MVT::i8 && \"Expected value to have byte element type.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30235, __extension__ __PRETTY_FUNCTION__)); | |||
30236 | assert(EltVT != MVT::i8 &&(static_cast <bool> (EltVT != MVT::i8 && "Horizontal byte sum only makes sense for wider elements!" ) ? void (0) : __assert_fail ("EltVT != MVT::i8 && \"Horizontal byte sum only makes sense for wider elements!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30237, __extension__ __PRETTY_FUNCTION__)) | |||
30237 | "Horizontal byte sum only makes sense for wider elements!")(static_cast <bool> (EltVT != MVT::i8 && "Horizontal byte sum only makes sense for wider elements!" ) ? void (0) : __assert_fail ("EltVT != MVT::i8 && \"Horizontal byte sum only makes sense for wider elements!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30237, __extension__ __PRETTY_FUNCTION__)); | |||
30238 | unsigned VecSize = VT.getSizeInBits(); | |||
30239 | assert(ByteVecVT.getSizeInBits() == VecSize && "Cannot change vector size!")(static_cast <bool> (ByteVecVT.getSizeInBits() == VecSize && "Cannot change vector size!") ? void (0) : __assert_fail ("ByteVecVT.getSizeInBits() == VecSize && \"Cannot change vector size!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30239, __extension__ __PRETTY_FUNCTION__)); | |||
30240 | ||||
30241 | // PSADBW instruction horizontally add all bytes and leave the result in i64 | |||
30242 | // chunks, thus directly computes the pop count for v2i64 and v4i64. | |||
30243 | if (EltVT == MVT::i64) { | |||
30244 | SDValue Zeros = DAG.getConstant(0, DL, ByteVecVT); | |||
30245 | MVT SadVecVT = MVT::getVectorVT(MVT::i64, VecSize / 64); | |||
30246 | V = DAG.getNode(X86ISD::PSADBW, DL, SadVecVT, V, Zeros); | |||
30247 | return DAG.getBitcast(VT, V); | |||
30248 | } | |||
30249 | ||||
30250 | if (EltVT == MVT::i32) { | |||
30251 | // We unpack the low half and high half into i32s interleaved with zeros so | |||
30252 | // that we can use PSADBW to horizontally sum them. The most useful part of | |||
30253 | // this is that it lines up the results of two PSADBW instructions to be | |||
30254 | // two v2i64 vectors which concatenated are the 4 population counts. We can | |||
30255 | // then use PACKUSWB to shrink and concatenate them into a v4i32 again. | |||
30256 | SDValue Zeros = DAG.getConstant(0, DL, VT); | |||
30257 | SDValue V32 = DAG.getBitcast(VT, V); | |||
30258 | SDValue Low = getUnpackl(DAG, DL, VT, V32, Zeros); | |||
30259 | SDValue High = getUnpackh(DAG, DL, VT, V32, Zeros); | |||
30260 | ||||
30261 | // Do the horizontal sums into two v2i64s. | |||
30262 | Zeros = DAG.getConstant(0, DL, ByteVecVT); | |||
30263 | MVT SadVecVT = MVT::getVectorVT(MVT::i64, VecSize / 64); | |||
30264 | Low = DAG.getNode(X86ISD::PSADBW, DL, SadVecVT, | |||
30265 | DAG.getBitcast(ByteVecVT, Low), Zeros); | |||
30266 | High = DAG.getNode(X86ISD::PSADBW, DL, SadVecVT, | |||
30267 | DAG.getBitcast(ByteVecVT, High), Zeros); | |||
30268 | ||||
30269 | // Merge them together. | |||
30270 | MVT ShortVecVT = MVT::getVectorVT(MVT::i16, VecSize / 16); | |||
30271 | V = DAG.getNode(X86ISD::PACKUS, DL, ByteVecVT, | |||
30272 | DAG.getBitcast(ShortVecVT, Low), | |||
30273 | DAG.getBitcast(ShortVecVT, High)); | |||
30274 | ||||
30275 | return DAG.getBitcast(VT, V); | |||
30276 | } | |||
30277 | ||||
30278 | // The only element type left is i16. | |||
30279 | assert(EltVT == MVT::i16 && "Unknown how to handle type")(static_cast <bool> (EltVT == MVT::i16 && "Unknown how to handle type" ) ? void (0) : __assert_fail ("EltVT == MVT::i16 && \"Unknown how to handle type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30279, __extension__ __PRETTY_FUNCTION__)); | |||
30280 | ||||
30281 | // To obtain pop count for each i16 element starting from the pop count for | |||
30282 | // i8 elements, shift the i16s left by 8, sum as i8s, and then shift as i16s | |||
30283 | // right by 8. It is important to shift as i16s as i8 vector shift isn't | |||
30284 | // directly supported. | |||
30285 | SDValue ShifterV = DAG.getConstant(8, DL, VT); | |||
30286 | SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, DAG.getBitcast(VT, V), ShifterV); | |||
30287 | V = DAG.getNode(ISD::ADD, DL, ByteVecVT, DAG.getBitcast(ByteVecVT, Shl), | |||
30288 | DAG.getBitcast(ByteVecVT, V)); | |||
30289 | return DAG.getNode(ISD::SRL, DL, VT, DAG.getBitcast(VT, V), ShifterV); | |||
30290 | } | |||
30291 | ||||
30292 | static SDValue LowerVectorCTPOPInRegLUT(SDValue Op, const SDLoc &DL, | |||
30293 | const X86Subtarget &Subtarget, | |||
30294 | SelectionDAG &DAG) { | |||
30295 | MVT VT = Op.getSimpleValueType(); | |||
30296 | MVT EltVT = VT.getVectorElementType(); | |||
30297 | int NumElts = VT.getVectorNumElements(); | |||
30298 | (void)EltVT; | |||
30299 | assert(EltVT == MVT::i8 && "Only vXi8 vector CTPOP lowering supported.")(static_cast <bool> (EltVT == MVT::i8 && "Only vXi8 vector CTPOP lowering supported." ) ? void (0) : __assert_fail ("EltVT == MVT::i8 && \"Only vXi8 vector CTPOP lowering supported.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30299, __extension__ __PRETTY_FUNCTION__)); | |||
30300 | ||||
30301 | // Implement a lookup table in register by using an algorithm based on: | |||
30302 | // http://wm.ite.pl/articles/sse-popcount.html | |||
30303 | // | |||
30304 | // The general idea is that every lower byte nibble in the input vector is an | |||
30305 | // index into a in-register pre-computed pop count table. We then split up the | |||
30306 | // input vector in two new ones: (1) a vector with only the shifted-right | |||
30307 | // higher nibbles for each byte and (2) a vector with the lower nibbles (and | |||
30308 | // masked out higher ones) for each byte. PSHUFB is used separately with both | |||
30309 | // to index the in-register table. Next, both are added and the result is a | |||
30310 | // i8 vector where each element contains the pop count for input byte. | |||
30311 | const int LUT[16] = {/* 0 */ 0, /* 1 */ 1, /* 2 */ 1, /* 3 */ 2, | |||
30312 | /* 4 */ 1, /* 5 */ 2, /* 6 */ 2, /* 7 */ 3, | |||
30313 | /* 8 */ 1, /* 9 */ 2, /* a */ 2, /* b */ 3, | |||
30314 | /* c */ 2, /* d */ 3, /* e */ 3, /* f */ 4}; | |||
30315 | ||||
30316 | SmallVector<SDValue, 64> LUTVec; | |||
30317 | for (int i = 0; i < NumElts; ++i) | |||
30318 | LUTVec.push_back(DAG.getConstant(LUT[i % 16], DL, MVT::i8)); | |||
30319 | SDValue InRegLUT = DAG.getBuildVector(VT, DL, LUTVec); | |||
30320 | SDValue M0F = DAG.getConstant(0x0F, DL, VT); | |||
30321 | ||||
30322 | // High nibbles | |||
30323 | SDValue FourV = DAG.getConstant(4, DL, VT); | |||
30324 | SDValue HiNibbles = DAG.getNode(ISD::SRL, DL, VT, Op, FourV); | |||
30325 | ||||
30326 | // Low nibbles | |||
30327 | SDValue LoNibbles = DAG.getNode(ISD::AND, DL, VT, Op, M0F); | |||
30328 | ||||
30329 | // The input vector is used as the shuffle mask that index elements into the | |||
30330 | // LUT. After counting low and high nibbles, add the vector to obtain the | |||
30331 | // final pop count per i8 element. | |||
30332 | SDValue HiPopCnt = DAG.getNode(X86ISD::PSHUFB, DL, VT, InRegLUT, HiNibbles); | |||
30333 | SDValue LoPopCnt = DAG.getNode(X86ISD::PSHUFB, DL, VT, InRegLUT, LoNibbles); | |||
30334 | return DAG.getNode(ISD::ADD, DL, VT, HiPopCnt, LoPopCnt); | |||
30335 | } | |||
30336 | ||||
30337 | // Please ensure that any codegen change from LowerVectorCTPOP is reflected in | |||
30338 | // updated cost models in X86TTIImpl::getIntrinsicInstrCost. | |||
30339 | static SDValue LowerVectorCTPOP(SDValue Op, const X86Subtarget &Subtarget, | |||
30340 | SelectionDAG &DAG) { | |||
30341 | MVT VT = Op.getSimpleValueType(); | |||
30342 | assert((VT.is512BitVector() || VT.is256BitVector() || VT.is128BitVector()) &&(static_cast <bool> ((VT.is512BitVector() || VT.is256BitVector () || VT.is128BitVector()) && "Unknown CTPOP type to handle" ) ? void (0) : __assert_fail ("(VT.is512BitVector() || VT.is256BitVector() || VT.is128BitVector()) && \"Unknown CTPOP type to handle\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30343, __extension__ __PRETTY_FUNCTION__)) | |||
30343 | "Unknown CTPOP type to handle")(static_cast <bool> ((VT.is512BitVector() || VT.is256BitVector () || VT.is128BitVector()) && "Unknown CTPOP type to handle" ) ? void (0) : __assert_fail ("(VT.is512BitVector() || VT.is256BitVector() || VT.is128BitVector()) && \"Unknown CTPOP type to handle\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30343, __extension__ __PRETTY_FUNCTION__)); | |||
30344 | SDLoc DL(Op.getNode()); | |||
30345 | SDValue Op0 = Op.getOperand(0); | |||
30346 | ||||
30347 | // TRUNC(CTPOP(ZEXT(X))) to make use of vXi32/vXi64 VPOPCNT instructions. | |||
30348 | if (Subtarget.hasVPOPCNTDQ()) { | |||
30349 | unsigned NumElems = VT.getVectorNumElements(); | |||
30350 | assert((VT.getVectorElementType() == MVT::i8 ||(static_cast <bool> ((VT.getVectorElementType() == MVT:: i8 || VT.getVectorElementType() == MVT::i16) && "Unexpected type" ) ? void (0) : __assert_fail ("(VT.getVectorElementType() == MVT::i8 || VT.getVectorElementType() == MVT::i16) && \"Unexpected type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30351, __extension__ __PRETTY_FUNCTION__)) | |||
30351 | VT.getVectorElementType() == MVT::i16) && "Unexpected type")(static_cast <bool> ((VT.getVectorElementType() == MVT:: i8 || VT.getVectorElementType() == MVT::i16) && "Unexpected type" ) ? void (0) : __assert_fail ("(VT.getVectorElementType() == MVT::i8 || VT.getVectorElementType() == MVT::i16) && \"Unexpected type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30351, __extension__ __PRETTY_FUNCTION__)); | |||
30352 | if (NumElems < 16 || (NumElems == 16 && Subtarget.canExtendTo512DQ())) { | |||
30353 | MVT NewVT = MVT::getVectorVT(MVT::i32, NumElems); | |||
30354 | Op = DAG.getNode(ISD::ZERO_EXTEND, DL, NewVT, Op0); | |||
30355 | Op = DAG.getNode(ISD::CTPOP, DL, NewVT, Op); | |||
30356 | return DAG.getNode(ISD::TRUNCATE, DL, VT, Op); | |||
30357 | } | |||
30358 | } | |||
30359 | ||||
30360 | // Decompose 256-bit ops into smaller 128-bit ops. | |||
30361 | if (VT.is256BitVector() && !Subtarget.hasInt256()) | |||
30362 | return splitVectorIntUnary(Op, DAG); | |||
30363 | ||||
30364 | // Decompose 512-bit ops into smaller 256-bit ops. | |||
30365 | if (VT.is512BitVector() && !Subtarget.hasBWI()) | |||
30366 | return splitVectorIntUnary(Op, DAG); | |||
30367 | ||||
30368 | // For element types greater than i8, do vXi8 pop counts and a bytesum. | |||
30369 | if (VT.getScalarType() != MVT::i8) { | |||
30370 | MVT ByteVT = MVT::getVectorVT(MVT::i8, VT.getSizeInBits() / 8); | |||
30371 | SDValue ByteOp = DAG.getBitcast(ByteVT, Op0); | |||
30372 | SDValue PopCnt8 = DAG.getNode(ISD::CTPOP, DL, ByteVT, ByteOp); | |||
30373 | return LowerHorizontalByteSum(PopCnt8, VT, Subtarget, DAG); | |||
30374 | } | |||
30375 | ||||
30376 | // We can't use the fast LUT approach, so fall back on LegalizeDAG. | |||
30377 | if (!Subtarget.hasSSSE3()) | |||
30378 | return SDValue(); | |||
30379 | ||||
30380 | return LowerVectorCTPOPInRegLUT(Op0, DL, Subtarget, DAG); | |||
30381 | } | |||
30382 | ||||
30383 | static SDValue LowerCTPOP(SDValue Op, const X86Subtarget &Subtarget, | |||
30384 | SelectionDAG &DAG) { | |||
30385 | assert(Op.getSimpleValueType().isVector() &&(static_cast <bool> (Op.getSimpleValueType().isVector() && "We only do custom lowering for vector population count." ) ? void (0) : __assert_fail ("Op.getSimpleValueType().isVector() && \"We only do custom lowering for vector population count.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30386, __extension__ __PRETTY_FUNCTION__)) | |||
30386 | "We only do custom lowering for vector population count.")(static_cast <bool> (Op.getSimpleValueType().isVector() && "We only do custom lowering for vector population count." ) ? void (0) : __assert_fail ("Op.getSimpleValueType().isVector() && \"We only do custom lowering for vector population count.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30386, __extension__ __PRETTY_FUNCTION__)); | |||
30387 | return LowerVectorCTPOP(Op, Subtarget, DAG); | |||
30388 | } | |||
30389 | ||||
30390 | static SDValue LowerBITREVERSE_XOP(SDValue Op, SelectionDAG &DAG) { | |||
30391 | MVT VT = Op.getSimpleValueType(); | |||
30392 | SDValue In = Op.getOperand(0); | |||
30393 | SDLoc DL(Op); | |||
30394 | ||||
30395 | // For scalars, its still beneficial to transfer to/from the SIMD unit to | |||
30396 | // perform the BITREVERSE. | |||
30397 | if (!VT.isVector()) { | |||
30398 | MVT VecVT = MVT::getVectorVT(VT, 128 / VT.getSizeInBits()); | |||
30399 | SDValue Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VecVT, In); | |||
30400 | Res = DAG.getNode(ISD::BITREVERSE, DL, VecVT, Res); | |||
30401 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, Res, | |||
30402 | DAG.getIntPtrConstant(0, DL)); | |||
30403 | } | |||
30404 | ||||
30405 | int NumElts = VT.getVectorNumElements(); | |||
30406 | int ScalarSizeInBytes = VT.getScalarSizeInBits() / 8; | |||
30407 | ||||
30408 | // Decompose 256-bit ops into smaller 128-bit ops. | |||
30409 | if (VT.is256BitVector()) | |||
30410 | return splitVectorIntUnary(Op, DAG); | |||
30411 | ||||
30412 | assert(VT.is128BitVector() &&(static_cast <bool> (VT.is128BitVector() && "Only 128-bit vector bitreverse lowering supported." ) ? void (0) : __assert_fail ("VT.is128BitVector() && \"Only 128-bit vector bitreverse lowering supported.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30413, __extension__ __PRETTY_FUNCTION__)) | |||
30413 | "Only 128-bit vector bitreverse lowering supported.")(static_cast <bool> (VT.is128BitVector() && "Only 128-bit vector bitreverse lowering supported." ) ? void (0) : __assert_fail ("VT.is128BitVector() && \"Only 128-bit vector bitreverse lowering supported.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30413, __extension__ __PRETTY_FUNCTION__)); | |||
30414 | ||||
30415 | // VPPERM reverses the bits of a byte with the permute Op (2 << 5), and we | |||
30416 | // perform the BSWAP in the shuffle. | |||
30417 | // Its best to shuffle using the second operand as this will implicitly allow | |||
30418 | // memory folding for multiple vectors. | |||
30419 | SmallVector<SDValue, 16> MaskElts; | |||
30420 | for (int i = 0; i != NumElts; ++i) { | |||
30421 | for (int j = ScalarSizeInBytes - 1; j >= 0; --j) { | |||
30422 | int SourceByte = 16 + (i * ScalarSizeInBytes) + j; | |||
30423 | int PermuteByte = SourceByte | (2 << 5); | |||
30424 | MaskElts.push_back(DAG.getConstant(PermuteByte, DL, MVT::i8)); | |||
30425 | } | |||
30426 | } | |||
30427 | ||||
30428 | SDValue Mask = DAG.getBuildVector(MVT::v16i8, DL, MaskElts); | |||
30429 | SDValue Res = DAG.getBitcast(MVT::v16i8, In); | |||
30430 | Res = DAG.getNode(X86ISD::VPPERM, DL, MVT::v16i8, DAG.getUNDEF(MVT::v16i8), | |||
30431 | Res, Mask); | |||
30432 | return DAG.getBitcast(VT, Res); | |||
30433 | } | |||
30434 | ||||
30435 | static SDValue LowerBITREVERSE(SDValue Op, const X86Subtarget &Subtarget, | |||
30436 | SelectionDAG &DAG) { | |||
30437 | MVT VT = Op.getSimpleValueType(); | |||
30438 | ||||
30439 | if (Subtarget.hasXOP() && !VT.is512BitVector()) | |||
30440 | return LowerBITREVERSE_XOP(Op, DAG); | |||
30441 | ||||
30442 | assert(Subtarget.hasSSSE3() && "SSSE3 required for BITREVERSE")(static_cast <bool> (Subtarget.hasSSSE3() && "SSSE3 required for BITREVERSE" ) ? void (0) : __assert_fail ("Subtarget.hasSSSE3() && \"SSSE3 required for BITREVERSE\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30442, __extension__ __PRETTY_FUNCTION__)); | |||
30443 | ||||
30444 | SDValue In = Op.getOperand(0); | |||
30445 | SDLoc DL(Op); | |||
30446 | ||||
30447 | assert(VT.getScalarType() == MVT::i8 &&(static_cast <bool> (VT.getScalarType() == MVT::i8 && "Only byte vector BITREVERSE supported") ? void (0) : __assert_fail ("VT.getScalarType() == MVT::i8 && \"Only byte vector BITREVERSE supported\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30448, __extension__ __PRETTY_FUNCTION__)) | |||
30448 | "Only byte vector BITREVERSE supported")(static_cast <bool> (VT.getScalarType() == MVT::i8 && "Only byte vector BITREVERSE supported") ? void (0) : __assert_fail ("VT.getScalarType() == MVT::i8 && \"Only byte vector BITREVERSE supported\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30448, __extension__ __PRETTY_FUNCTION__)); | |||
30449 | ||||
30450 | // Split v64i8 without BWI so that we can still use the PSHUFB lowering. | |||
30451 | if (VT == MVT::v64i8 && !Subtarget.hasBWI()) | |||
30452 | return splitVectorIntUnary(Op, DAG); | |||
30453 | ||||
30454 | // Decompose 256-bit ops into smaller 128-bit ops on pre-AVX2. | |||
30455 | if (VT == MVT::v32i8 && !Subtarget.hasInt256()) | |||
30456 | return splitVectorIntUnary(Op, DAG); | |||
30457 | ||||
30458 | unsigned NumElts = VT.getVectorNumElements(); | |||
30459 | ||||
30460 | // If we have GFNI, we can use GF2P8AFFINEQB to reverse the bits. | |||
30461 | if (Subtarget.hasGFNI()) { | |||
30462 | MVT MatrixVT = MVT::getVectorVT(MVT::i64, NumElts / 8); | |||
30463 | SDValue Matrix = DAG.getConstant(0x8040201008040201ULL, DL, MatrixVT); | |||
30464 | Matrix = DAG.getBitcast(VT, Matrix); | |||
30465 | return DAG.getNode(X86ISD::GF2P8AFFINEQB, DL, VT, In, Matrix, | |||
30466 | DAG.getTargetConstant(0, DL, MVT::i8)); | |||
30467 | } | |||
30468 | ||||
30469 | // Perform BITREVERSE using PSHUFB lookups. Each byte is split into | |||
30470 | // two nibbles and a PSHUFB lookup to find the bitreverse of each | |||
30471 | // 0-15 value (moved to the other nibble). | |||
30472 | SDValue NibbleMask = DAG.getConstant(0xF, DL, VT); | |||
30473 | SDValue Lo = DAG.getNode(ISD::AND, DL, VT, In, NibbleMask); | |||
30474 | SDValue Hi = DAG.getNode(ISD::SRL, DL, VT, In, DAG.getConstant(4, DL, VT)); | |||
30475 | ||||
30476 | const int LoLUT[16] = { | |||
30477 | /* 0 */ 0x00, /* 1 */ 0x80, /* 2 */ 0x40, /* 3 */ 0xC0, | |||
30478 | /* 4 */ 0x20, /* 5 */ 0xA0, /* 6 */ 0x60, /* 7 */ 0xE0, | |||
30479 | /* 8 */ 0x10, /* 9 */ 0x90, /* a */ 0x50, /* b */ 0xD0, | |||
30480 | /* c */ 0x30, /* d */ 0xB0, /* e */ 0x70, /* f */ 0xF0}; | |||
30481 | const int HiLUT[16] = { | |||
30482 | /* 0 */ 0x00, /* 1 */ 0x08, /* 2 */ 0x04, /* 3 */ 0x0C, | |||
30483 | /* 4 */ 0x02, /* 5 */ 0x0A, /* 6 */ 0x06, /* 7 */ 0x0E, | |||
30484 | /* 8 */ 0x01, /* 9 */ 0x09, /* a */ 0x05, /* b */ 0x0D, | |||
30485 | /* c */ 0x03, /* d */ 0x0B, /* e */ 0x07, /* f */ 0x0F}; | |||
30486 | ||||
30487 | SmallVector<SDValue, 16> LoMaskElts, HiMaskElts; | |||
30488 | for (unsigned i = 0; i < NumElts; ++i) { | |||
30489 | LoMaskElts.push_back(DAG.getConstant(LoLUT[i % 16], DL, MVT::i8)); | |||
30490 | HiMaskElts.push_back(DAG.getConstant(HiLUT[i % 16], DL, MVT::i8)); | |||
30491 | } | |||
30492 | ||||
30493 | SDValue LoMask = DAG.getBuildVector(VT, DL, LoMaskElts); | |||
30494 | SDValue HiMask = DAG.getBuildVector(VT, DL, HiMaskElts); | |||
30495 | Lo = DAG.getNode(X86ISD::PSHUFB, DL, VT, LoMask, Lo); | |||
30496 | Hi = DAG.getNode(X86ISD::PSHUFB, DL, VT, HiMask, Hi); | |||
30497 | return DAG.getNode(ISD::OR, DL, VT, Lo, Hi); | |||
30498 | } | |||
30499 | ||||
30500 | static SDValue LowerPARITY(SDValue Op, const X86Subtarget &Subtarget, | |||
30501 | SelectionDAG &DAG) { | |||
30502 | SDLoc DL(Op); | |||
30503 | SDValue X = Op.getOperand(0); | |||
30504 | MVT VT = Op.getSimpleValueType(); | |||
30505 | ||||
30506 | // Special case. If the input fits in 8-bits we can use a single 8-bit TEST. | |||
30507 | if (VT == MVT::i8 || | |||
30508 | DAG.MaskedValueIsZero(X, APInt::getBitsSetFrom(VT.getSizeInBits(), 8))) { | |||
30509 | X = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, X); | |||
30510 | SDValue Flags = DAG.getNode(X86ISD::CMP, DL, MVT::i32, X, | |||
30511 | DAG.getConstant(0, DL, MVT::i8)); | |||
30512 | // Copy the inverse of the parity flag into a register with setcc. | |||
30513 | SDValue Setnp = getSETCC(X86::COND_NP, Flags, DL, DAG); | |||
30514 | // Extend to the original type. | |||
30515 | return DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Setnp); | |||
30516 | } | |||
30517 | ||||
30518 | // If we have POPCNT, use the default expansion. | |||
30519 | if (Subtarget.hasPOPCNT()) | |||
30520 | return SDValue(); | |||
30521 | ||||
30522 | if (VT == MVT::i64) { | |||
30523 | // Xor the high and low 16-bits together using a 32-bit operation. | |||
30524 | SDValue Hi = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, | |||
30525 | DAG.getNode(ISD::SRL, DL, MVT::i64, X, | |||
30526 | DAG.getConstant(32, DL, MVT::i8))); | |||
30527 | SDValue Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, X); | |||
30528 | X = DAG.getNode(ISD::XOR, DL, MVT::i32, Lo, Hi); | |||
30529 | } | |||
30530 | ||||
30531 | if (VT != MVT::i16) { | |||
30532 | // Xor the high and low 16-bits together using a 32-bit operation. | |||
30533 | SDValue Hi16 = DAG.getNode(ISD::SRL, DL, MVT::i32, X, | |||
30534 | DAG.getConstant(16, DL, MVT::i8)); | |||
30535 | X = DAG.getNode(ISD::XOR, DL, MVT::i32, X, Hi16); | |||
30536 | } else { | |||
30537 | // If the input is 16-bits, we need to extend to use an i32 shift below. | |||
30538 | X = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, X); | |||
30539 | } | |||
30540 | ||||
30541 | // Finally xor the low 2 bytes together and use a 8-bit flag setting xor. | |||
30542 | // This should allow an h-reg to be used to save a shift. | |||
30543 | SDValue Hi = DAG.getNode( | |||
30544 | ISD::TRUNCATE, DL, MVT::i8, | |||
30545 | DAG.getNode(ISD::SRL, DL, MVT::i32, X, DAG.getConstant(8, DL, MVT::i8))); | |||
30546 | SDValue Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, X); | |||
30547 | SDVTList VTs = DAG.getVTList(MVT::i8, MVT::i32); | |||
30548 | SDValue Flags = DAG.getNode(X86ISD::XOR, DL, VTs, Lo, Hi).getValue(1); | |||
30549 | ||||
30550 | // Copy the inverse of the parity flag into a register with setcc. | |||
30551 | SDValue Setnp = getSETCC(X86::COND_NP, Flags, DL, DAG); | |||
30552 | // Extend to the original type. | |||
30553 | return DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Setnp); | |||
30554 | } | |||
30555 | ||||
30556 | static SDValue lowerAtomicArithWithLOCK(SDValue N, SelectionDAG &DAG, | |||
30557 | const X86Subtarget &Subtarget) { | |||
30558 | unsigned NewOpc = 0; | |||
30559 | switch (N->getOpcode()) { | |||
30560 | case ISD::ATOMIC_LOAD_ADD: | |||
30561 | NewOpc = X86ISD::LADD; | |||
30562 | break; | |||
30563 | case ISD::ATOMIC_LOAD_SUB: | |||
30564 | NewOpc = X86ISD::LSUB; | |||
30565 | break; | |||
30566 | case ISD::ATOMIC_LOAD_OR: | |||
30567 | NewOpc = X86ISD::LOR; | |||
30568 | break; | |||
30569 | case ISD::ATOMIC_LOAD_XOR: | |||
30570 | NewOpc = X86ISD::LXOR; | |||
30571 | break; | |||
30572 | case ISD::ATOMIC_LOAD_AND: | |||
30573 | NewOpc = X86ISD::LAND; | |||
30574 | break; | |||
30575 | default: | |||
30576 | llvm_unreachable("Unknown ATOMIC_LOAD_ opcode")::llvm::llvm_unreachable_internal("Unknown ATOMIC_LOAD_ opcode" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30576); | |||
30577 | } | |||
30578 | ||||
30579 | MachineMemOperand *MMO = cast<MemSDNode>(N)->getMemOperand(); | |||
30580 | ||||
30581 | return DAG.getMemIntrinsicNode( | |||
30582 | NewOpc, SDLoc(N), DAG.getVTList(MVT::i32, MVT::Other), | |||
30583 | {N->getOperand(0), N->getOperand(1), N->getOperand(2)}, | |||
30584 | /*MemVT=*/N->getSimpleValueType(0), MMO); | |||
30585 | } | |||
30586 | ||||
30587 | /// Lower atomic_load_ops into LOCK-prefixed operations. | |||
30588 | static SDValue lowerAtomicArith(SDValue N, SelectionDAG &DAG, | |||
30589 | const X86Subtarget &Subtarget) { | |||
30590 | AtomicSDNode *AN = cast<AtomicSDNode>(N.getNode()); | |||
30591 | SDValue Chain = N->getOperand(0); | |||
30592 | SDValue LHS = N->getOperand(1); | |||
30593 | SDValue RHS = N->getOperand(2); | |||
30594 | unsigned Opc = N->getOpcode(); | |||
30595 | MVT VT = N->getSimpleValueType(0); | |||
30596 | SDLoc DL(N); | |||
30597 | ||||
30598 | // We can lower atomic_load_add into LXADD. However, any other atomicrmw op | |||
30599 | // can only be lowered when the result is unused. They should have already | |||
30600 | // been transformed into a cmpxchg loop in AtomicExpand. | |||
30601 | if (N->hasAnyUseOfValue(0)) { | |||
30602 | // Handle (atomic_load_sub p, v) as (atomic_load_add p, -v), to be able to | |||
30603 | // select LXADD if LOCK_SUB can't be selected. | |||
30604 | if (Opc == ISD::ATOMIC_LOAD_SUB) { | |||
30605 | RHS = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), RHS); | |||
30606 | return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, VT, Chain, LHS, | |||
30607 | RHS, AN->getMemOperand()); | |||
30608 | } | |||
30609 | assert(Opc == ISD::ATOMIC_LOAD_ADD &&(static_cast <bool> (Opc == ISD::ATOMIC_LOAD_ADD && "Used AtomicRMW ops other than Add should have been expanded!" ) ? void (0) : __assert_fail ("Opc == ISD::ATOMIC_LOAD_ADD && \"Used AtomicRMW ops other than Add should have been expanded!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30610, __extension__ __PRETTY_FUNCTION__)) | |||
30610 | "Used AtomicRMW ops other than Add should have been expanded!")(static_cast <bool> (Opc == ISD::ATOMIC_LOAD_ADD && "Used AtomicRMW ops other than Add should have been expanded!" ) ? void (0) : __assert_fail ("Opc == ISD::ATOMIC_LOAD_ADD && \"Used AtomicRMW ops other than Add should have been expanded!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30610, __extension__ __PRETTY_FUNCTION__)); | |||
30611 | return N; | |||
30612 | } | |||
30613 | ||||
30614 | // Specialized lowering for the canonical form of an idemptotent atomicrmw. | |||
30615 | // The core idea here is that since the memory location isn't actually | |||
30616 | // changing, all we need is a lowering for the *ordering* impacts of the | |||
30617 | // atomicrmw. As such, we can chose a different operation and memory | |||
30618 | // location to minimize impact on other code. | |||
30619 | if (Opc == ISD::ATOMIC_LOAD_OR && isNullConstant(RHS)) { | |||
30620 | // On X86, the only ordering which actually requires an instruction is | |||
30621 | // seq_cst which isn't SingleThread, everything just needs to be preserved | |||
30622 | // during codegen and then dropped. Note that we expect (but don't assume), | |||
30623 | // that orderings other than seq_cst and acq_rel have been canonicalized to | |||
30624 | // a store or load. | |||
30625 | if (AN->getSuccessOrdering() == AtomicOrdering::SequentiallyConsistent && | |||
30626 | AN->getSyncScopeID() == SyncScope::System) { | |||
30627 | // Prefer a locked operation against a stack location to minimize cache | |||
30628 | // traffic. This assumes that stack locations are very likely to be | |||
30629 | // accessed only by the owning thread. | |||
30630 | SDValue NewChain = emitLockedStackOp(DAG, Subtarget, Chain, DL); | |||
30631 | assert(!N->hasAnyUseOfValue(0))(static_cast <bool> (!N->hasAnyUseOfValue(0)) ? void (0) : __assert_fail ("!N->hasAnyUseOfValue(0)", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30631, __extension__ __PRETTY_FUNCTION__)); | |||
30632 | // NOTE: The getUNDEF is needed to give something for the unused result 0. | |||
30633 | return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), | |||
30634 | DAG.getUNDEF(VT), NewChain); | |||
30635 | } | |||
30636 | // MEMBARRIER is a compiler barrier; it codegens to a no-op. | |||
30637 | SDValue NewChain = DAG.getNode(X86ISD::MEMBARRIER, DL, MVT::Other, Chain); | |||
30638 | assert(!N->hasAnyUseOfValue(0))(static_cast <bool> (!N->hasAnyUseOfValue(0)) ? void (0) : __assert_fail ("!N->hasAnyUseOfValue(0)", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30638, __extension__ __PRETTY_FUNCTION__)); | |||
30639 | // NOTE: The getUNDEF is needed to give something for the unused result 0. | |||
30640 | return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), | |||
30641 | DAG.getUNDEF(VT), NewChain); | |||
30642 | } | |||
30643 | ||||
30644 | SDValue LockOp = lowerAtomicArithWithLOCK(N, DAG, Subtarget); | |||
30645 | // RAUW the chain, but don't worry about the result, as it's unused. | |||
30646 | assert(!N->hasAnyUseOfValue(0))(static_cast <bool> (!N->hasAnyUseOfValue(0)) ? void (0) : __assert_fail ("!N->hasAnyUseOfValue(0)", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30646, __extension__ __PRETTY_FUNCTION__)); | |||
30647 | // NOTE: The getUNDEF is needed to give something for the unused result 0. | |||
30648 | return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), | |||
30649 | DAG.getUNDEF(VT), LockOp.getValue(1)); | |||
30650 | } | |||
30651 | ||||
30652 | static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG, | |||
30653 | const X86Subtarget &Subtarget) { | |||
30654 | auto *Node = cast<AtomicSDNode>(Op.getNode()); | |||
30655 | SDLoc dl(Node); | |||
30656 | EVT VT = Node->getMemoryVT(); | |||
30657 | ||||
30658 | bool IsSeqCst = | |||
30659 | Node->getSuccessOrdering() == AtomicOrdering::SequentiallyConsistent; | |||
30660 | bool IsTypeLegal = DAG.getTargetLoweringInfo().isTypeLegal(VT); | |||
30661 | ||||
30662 | // If this store is not sequentially consistent and the type is legal | |||
30663 | // we can just keep it. | |||
30664 | if (!IsSeqCst && IsTypeLegal) | |||
30665 | return Op; | |||
30666 | ||||
30667 | if (VT == MVT::i64 && !IsTypeLegal) { | |||
30668 | // For illegal i64 atomic_stores, we can try to use MOVQ or MOVLPS if SSE | |||
30669 | // is enabled. | |||
30670 | bool NoImplicitFloatOps = | |||
30671 | DAG.getMachineFunction().getFunction().hasFnAttribute( | |||
30672 | Attribute::NoImplicitFloat); | |||
30673 | if (!Subtarget.useSoftFloat() && !NoImplicitFloatOps) { | |||
30674 | SDValue Chain; | |||
30675 | if (Subtarget.hasSSE1()) { | |||
30676 | SDValue SclToVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, | |||
30677 | Node->getOperand(2)); | |||
30678 | MVT StVT = Subtarget.hasSSE2() ? MVT::v2i64 : MVT::v4f32; | |||
30679 | SclToVec = DAG.getBitcast(StVT, SclToVec); | |||
30680 | SDVTList Tys = DAG.getVTList(MVT::Other); | |||
30681 | SDValue Ops[] = {Node->getChain(), SclToVec, Node->getBasePtr()}; | |||
30682 | Chain = DAG.getMemIntrinsicNode(X86ISD::VEXTRACT_STORE, dl, Tys, Ops, | |||
30683 | MVT::i64, Node->getMemOperand()); | |||
30684 | } else if (Subtarget.hasX87()) { | |||
30685 | // First load this into an 80-bit X87 register using a stack temporary. | |||
30686 | // This will put the whole integer into the significand. | |||
30687 | SDValue StackPtr = DAG.CreateStackTemporary(MVT::i64); | |||
30688 | int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex(); | |||
30689 | MachinePointerInfo MPI = | |||
30690 | MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI); | |||
30691 | Chain = | |||
30692 | DAG.getStore(Node->getChain(), dl, Node->getOperand(2), StackPtr, | |||
30693 | MPI, MaybeAlign(), MachineMemOperand::MOStore); | |||
30694 | SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other); | |||
30695 | SDValue LdOps[] = {Chain, StackPtr}; | |||
30696 | SDValue Value = | |||
30697 | DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, LdOps, MVT::i64, MPI, | |||
30698 | /*Align*/ None, MachineMemOperand::MOLoad); | |||
30699 | Chain = Value.getValue(1); | |||
30700 | ||||
30701 | // Now use an FIST to do the atomic store. | |||
30702 | SDValue StoreOps[] = {Chain, Value, Node->getBasePtr()}; | |||
30703 | Chain = | |||
30704 | DAG.getMemIntrinsicNode(X86ISD::FIST, dl, DAG.getVTList(MVT::Other), | |||
30705 | StoreOps, MVT::i64, Node->getMemOperand()); | |||
30706 | } | |||
30707 | ||||
30708 | if (Chain) { | |||
30709 | // If this is a sequentially consistent store, also emit an appropriate | |||
30710 | // barrier. | |||
30711 | if (IsSeqCst) | |||
30712 | Chain = emitLockedStackOp(DAG, Subtarget, Chain, dl); | |||
30713 | ||||
30714 | return Chain; | |||
30715 | } | |||
30716 | } | |||
30717 | } | |||
30718 | ||||
30719 | // Convert seq_cst store -> xchg | |||
30720 | // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b) | |||
30721 | // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment. | |||
30722 | SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl, | |||
30723 | Node->getMemoryVT(), | |||
30724 | Node->getOperand(0), | |||
30725 | Node->getOperand(1), Node->getOperand(2), | |||
30726 | Node->getMemOperand()); | |||
30727 | return Swap.getValue(1); | |||
30728 | } | |||
30729 | ||||
30730 | static SDValue LowerADDSUBCARRY(SDValue Op, SelectionDAG &DAG) { | |||
30731 | SDNode *N = Op.getNode(); | |||
30732 | MVT VT = N->getSimpleValueType(0); | |||
30733 | unsigned Opc = Op.getOpcode(); | |||
30734 | ||||
30735 | // Let legalize expand this if it isn't a legal type yet. | |||
30736 | if (!DAG.getTargetLoweringInfo().isTypeLegal(VT)) | |||
30737 | return SDValue(); | |||
30738 | ||||
30739 | SDVTList VTs = DAG.getVTList(VT, MVT::i32); | |||
30740 | SDLoc DL(N); | |||
30741 | ||||
30742 | // Set the carry flag. | |||
30743 | SDValue Carry = Op.getOperand(2); | |||
30744 | EVT CarryVT = Carry.getValueType(); | |||
30745 | Carry = DAG.getNode(X86ISD::ADD, DL, DAG.getVTList(CarryVT, MVT::i32), | |||
30746 | Carry, DAG.getAllOnesConstant(DL, CarryVT)); | |||
30747 | ||||
30748 | bool IsAdd = Opc == ISD::ADDCARRY || Opc == ISD::SADDO_CARRY; | |||
30749 | SDValue Sum = DAG.getNode(IsAdd ? X86ISD::ADC : X86ISD::SBB, DL, VTs, | |||
30750 | Op.getOperand(0), Op.getOperand(1), | |||
30751 | Carry.getValue(1)); | |||
30752 | ||||
30753 | bool IsSigned = Opc == ISD::SADDO_CARRY || Opc == ISD::SSUBO_CARRY; | |||
30754 | SDValue SetCC = getSETCC(IsSigned ? X86::COND_O : X86::COND_B, | |||
30755 | Sum.getValue(1), DL, DAG); | |||
30756 | if (N->getValueType(1) == MVT::i1) | |||
30757 | SetCC = DAG.getNode(ISD::TRUNCATE, DL, MVT::i1, SetCC); | |||
30758 | ||||
30759 | return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC); | |||
30760 | } | |||
30761 | ||||
30762 | static SDValue LowerFSINCOS(SDValue Op, const X86Subtarget &Subtarget, | |||
30763 | SelectionDAG &DAG) { | |||
30764 | assert(Subtarget.isTargetDarwin() && Subtarget.is64Bit())(static_cast <bool> (Subtarget.isTargetDarwin() && Subtarget.is64Bit()) ? void (0) : __assert_fail ("Subtarget.isTargetDarwin() && Subtarget.is64Bit()" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30764, __extension__ __PRETTY_FUNCTION__)); | |||
30765 | ||||
30766 | // For MacOSX, we want to call an alternative entry point: __sincos_stret, | |||
30767 | // which returns the values as { float, float } (in XMM0) or | |||
30768 | // { double, double } (which is returned in XMM0, XMM1). | |||
30769 | SDLoc dl(Op); | |||
30770 | SDValue Arg = Op.getOperand(0); | |||
30771 | EVT ArgVT = Arg.getValueType(); | |||
30772 | Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext()); | |||
30773 | ||||
30774 | TargetLowering::ArgListTy Args; | |||
30775 | TargetLowering::ArgListEntry Entry; | |||
30776 | ||||
30777 | Entry.Node = Arg; | |||
30778 | Entry.Ty = ArgTy; | |||
30779 | Entry.IsSExt = false; | |||
30780 | Entry.IsZExt = false; | |||
30781 | Args.push_back(Entry); | |||
30782 | ||||
30783 | bool isF64 = ArgVT == MVT::f64; | |||
30784 | // Only optimize x86_64 for now. i386 is a bit messy. For f32, | |||
30785 | // the small struct {f32, f32} is returned in (eax, edx). For f64, | |||
30786 | // the results are returned via SRet in memory. | |||
30787 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
30788 | RTLIB::Libcall LC = isF64 ? RTLIB::SINCOS_STRET_F64 : RTLIB::SINCOS_STRET_F32; | |||
30789 | const char *LibcallName = TLI.getLibcallName(LC); | |||
30790 | SDValue Callee = | |||
30791 | DAG.getExternalSymbol(LibcallName, TLI.getPointerTy(DAG.getDataLayout())); | |||
30792 | ||||
30793 | Type *RetTy = isF64 ? (Type *)StructType::get(ArgTy, ArgTy) | |||
30794 | : (Type *)FixedVectorType::get(ArgTy, 4); | |||
30795 | ||||
30796 | TargetLowering::CallLoweringInfo CLI(DAG); | |||
30797 | CLI.setDebugLoc(dl) | |||
30798 | .setChain(DAG.getEntryNode()) | |||
30799 | .setLibCallee(CallingConv::C, RetTy, Callee, std::move(Args)); | |||
30800 | ||||
30801 | std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI); | |||
30802 | ||||
30803 | if (isF64) | |||
30804 | // Returned in xmm0 and xmm1. | |||
30805 | return CallResult.first; | |||
30806 | ||||
30807 | // Returned in bits 0:31 and 32:64 xmm0. | |||
30808 | SDValue SinVal = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, ArgVT, | |||
30809 | CallResult.first, DAG.getIntPtrConstant(0, dl)); | |||
30810 | SDValue CosVal = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, ArgVT, | |||
30811 | CallResult.first, DAG.getIntPtrConstant(1, dl)); | |||
30812 | SDVTList Tys = DAG.getVTList(ArgVT, ArgVT); | |||
30813 | return DAG.getNode(ISD::MERGE_VALUES, dl, Tys, SinVal, CosVal); | |||
30814 | } | |||
30815 | ||||
30816 | /// Widen a vector input to a vector of NVT. The | |||
30817 | /// input vector must have the same element type as NVT. | |||
30818 | static SDValue ExtendToType(SDValue InOp, MVT NVT, SelectionDAG &DAG, | |||
30819 | bool FillWithZeroes = false) { | |||
30820 | // Check if InOp already has the right width. | |||
30821 | MVT InVT = InOp.getSimpleValueType(); | |||
30822 | if (InVT == NVT) | |||
30823 | return InOp; | |||
30824 | ||||
30825 | if (InOp.isUndef()) | |||
30826 | return DAG.getUNDEF(NVT); | |||
30827 | ||||
30828 | assert(InVT.getVectorElementType() == NVT.getVectorElementType() &&(static_cast <bool> (InVT.getVectorElementType() == NVT .getVectorElementType() && "input and widen element type must match" ) ? void (0) : __assert_fail ("InVT.getVectorElementType() == NVT.getVectorElementType() && \"input and widen element type must match\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30829, __extension__ __PRETTY_FUNCTION__)) | |||
30829 | "input and widen element type must match")(static_cast <bool> (InVT.getVectorElementType() == NVT .getVectorElementType() && "input and widen element type must match" ) ? void (0) : __assert_fail ("InVT.getVectorElementType() == NVT.getVectorElementType() && \"input and widen element type must match\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30829, __extension__ __PRETTY_FUNCTION__)); | |||
30830 | ||||
30831 | unsigned InNumElts = InVT.getVectorNumElements(); | |||
30832 | unsigned WidenNumElts = NVT.getVectorNumElements(); | |||
30833 | assert(WidenNumElts > InNumElts && WidenNumElts % InNumElts == 0 &&(static_cast <bool> (WidenNumElts > InNumElts && WidenNumElts % InNumElts == 0 && "Unexpected request for vector widening" ) ? void (0) : __assert_fail ("WidenNumElts > InNumElts && WidenNumElts % InNumElts == 0 && \"Unexpected request for vector widening\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30834, __extension__ __PRETTY_FUNCTION__)) | |||
30834 | "Unexpected request for vector widening")(static_cast <bool> (WidenNumElts > InNumElts && WidenNumElts % InNumElts == 0 && "Unexpected request for vector widening" ) ? void (0) : __assert_fail ("WidenNumElts > InNumElts && WidenNumElts % InNumElts == 0 && \"Unexpected request for vector widening\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30834, __extension__ __PRETTY_FUNCTION__)); | |||
30835 | ||||
30836 | SDLoc dl(InOp); | |||
30837 | if (InOp.getOpcode() == ISD::CONCAT_VECTORS && | |||
30838 | InOp.getNumOperands() == 2) { | |||
30839 | SDValue N1 = InOp.getOperand(1); | |||
30840 | if ((ISD::isBuildVectorAllZeros(N1.getNode()) && FillWithZeroes) || | |||
30841 | N1.isUndef()) { | |||
30842 | InOp = InOp.getOperand(0); | |||
30843 | InVT = InOp.getSimpleValueType(); | |||
30844 | InNumElts = InVT.getVectorNumElements(); | |||
30845 | } | |||
30846 | } | |||
30847 | if (ISD::isBuildVectorOfConstantSDNodes(InOp.getNode()) || | |||
30848 | ISD::isBuildVectorOfConstantFPSDNodes(InOp.getNode())) { | |||
30849 | SmallVector<SDValue, 16> Ops; | |||
30850 | for (unsigned i = 0; i < InNumElts; ++i) | |||
30851 | Ops.push_back(InOp.getOperand(i)); | |||
30852 | ||||
30853 | EVT EltVT = InOp.getOperand(0).getValueType(); | |||
30854 | ||||
30855 | SDValue FillVal = FillWithZeroes ? DAG.getConstant(0, dl, EltVT) : | |||
30856 | DAG.getUNDEF(EltVT); | |||
30857 | for (unsigned i = 0; i < WidenNumElts - InNumElts; ++i) | |||
30858 | Ops.push_back(FillVal); | |||
30859 | return DAG.getBuildVector(NVT, dl, Ops); | |||
30860 | } | |||
30861 | SDValue FillVal = FillWithZeroes ? DAG.getConstant(0, dl, NVT) : | |||
30862 | DAG.getUNDEF(NVT); | |||
30863 | return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, NVT, FillVal, | |||
30864 | InOp, DAG.getIntPtrConstant(0, dl)); | |||
30865 | } | |||
30866 | ||||
30867 | static SDValue LowerMSCATTER(SDValue Op, const X86Subtarget &Subtarget, | |||
30868 | SelectionDAG &DAG) { | |||
30869 | assert(Subtarget.hasAVX512() &&(static_cast <bool> (Subtarget.hasAVX512() && "MGATHER/MSCATTER are supported on AVX-512 arch only" ) ? void (0) : __assert_fail ("Subtarget.hasAVX512() && \"MGATHER/MSCATTER are supported on AVX-512 arch only\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30870, __extension__ __PRETTY_FUNCTION__)) | |||
30870 | "MGATHER/MSCATTER are supported on AVX-512 arch only")(static_cast <bool> (Subtarget.hasAVX512() && "MGATHER/MSCATTER are supported on AVX-512 arch only" ) ? void (0) : __assert_fail ("Subtarget.hasAVX512() && \"MGATHER/MSCATTER are supported on AVX-512 arch only\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30870, __extension__ __PRETTY_FUNCTION__)); | |||
30871 | ||||
30872 | MaskedScatterSDNode *N = cast<MaskedScatterSDNode>(Op.getNode()); | |||
30873 | SDValue Src = N->getValue(); | |||
30874 | MVT VT = Src.getSimpleValueType(); | |||
30875 | assert(VT.getScalarSizeInBits() >= 32 && "Unsupported scatter op")(static_cast <bool> (VT.getScalarSizeInBits() >= 32 && "Unsupported scatter op") ? void (0) : __assert_fail ("VT.getScalarSizeInBits() >= 32 && \"Unsupported scatter op\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30875, __extension__ __PRETTY_FUNCTION__)); | |||
30876 | SDLoc dl(Op); | |||
30877 | ||||
30878 | SDValue Scale = N->getScale(); | |||
30879 | SDValue Index = N->getIndex(); | |||
30880 | SDValue Mask = N->getMask(); | |||
30881 | SDValue Chain = N->getChain(); | |||
30882 | SDValue BasePtr = N->getBasePtr(); | |||
30883 | ||||
30884 | if (VT == MVT::v2f32 || VT == MVT::v2i32) { | |||
30885 | assert(Mask.getValueType() == MVT::v2i1 && "Unexpected mask type")(static_cast <bool> (Mask.getValueType() == MVT::v2i1 && "Unexpected mask type") ? void (0) : __assert_fail ("Mask.getValueType() == MVT::v2i1 && \"Unexpected mask type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30885, __extension__ __PRETTY_FUNCTION__)); | |||
30886 | // If the index is v2i64 and we have VLX we can use xmm for data and index. | |||
30887 | if (Index.getValueType() == MVT::v2i64 && Subtarget.hasVLX()) { | |||
30888 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
30889 | EVT WideVT = TLI.getTypeToTransformTo(*DAG.getContext(), VT); | |||
30890 | Src = DAG.getNode(ISD::CONCAT_VECTORS, dl, WideVT, Src, DAG.getUNDEF(VT)); | |||
30891 | SDVTList VTs = DAG.getVTList(MVT::Other); | |||
30892 | SDValue Ops[] = {Chain, Src, Mask, BasePtr, Index, Scale}; | |||
30893 | return DAG.getMemIntrinsicNode(X86ISD::MSCATTER, dl, VTs, Ops, | |||
30894 | N->getMemoryVT(), N->getMemOperand()); | |||
30895 | } | |||
30896 | return SDValue(); | |||
30897 | } | |||
30898 | ||||
30899 | MVT IndexVT = Index.getSimpleValueType(); | |||
30900 | ||||
30901 | // If the index is v2i32, we're being called by type legalization and we | |||
30902 | // should just let the default handling take care of it. | |||
30903 | if (IndexVT == MVT::v2i32) | |||
30904 | return SDValue(); | |||
30905 | ||||
30906 | // If we don't have VLX and neither the passthru or index is 512-bits, we | |||
30907 | // need to widen until one is. | |||
30908 | if (!Subtarget.hasVLX() && !VT.is512BitVector() && | |||
30909 | !Index.getSimpleValueType().is512BitVector()) { | |||
30910 | // Determine how much we need to widen by to get a 512-bit type. | |||
30911 | unsigned Factor = std::min(512/VT.getSizeInBits(), | |||
30912 | 512/IndexVT.getSizeInBits()); | |||
30913 | unsigned NumElts = VT.getVectorNumElements() * Factor; | |||
30914 | ||||
30915 | VT = MVT::getVectorVT(VT.getVectorElementType(), NumElts); | |||
30916 | IndexVT = MVT::getVectorVT(IndexVT.getVectorElementType(), NumElts); | |||
30917 | MVT MaskVT = MVT::getVectorVT(MVT::i1, NumElts); | |||
30918 | ||||
30919 | Src = ExtendToType(Src, VT, DAG); | |||
30920 | Index = ExtendToType(Index, IndexVT, DAG); | |||
30921 | Mask = ExtendToType(Mask, MaskVT, DAG, true); | |||
30922 | } | |||
30923 | ||||
30924 | SDVTList VTs = DAG.getVTList(MVT::Other); | |||
30925 | SDValue Ops[] = {Chain, Src, Mask, BasePtr, Index, Scale}; | |||
30926 | return DAG.getMemIntrinsicNode(X86ISD::MSCATTER, dl, VTs, Ops, | |||
30927 | N->getMemoryVT(), N->getMemOperand()); | |||
30928 | } | |||
30929 | ||||
30930 | static SDValue LowerMLOAD(SDValue Op, const X86Subtarget &Subtarget, | |||
30931 | SelectionDAG &DAG) { | |||
30932 | ||||
30933 | MaskedLoadSDNode *N = cast<MaskedLoadSDNode>(Op.getNode()); | |||
30934 | MVT VT = Op.getSimpleValueType(); | |||
30935 | MVT ScalarVT = VT.getScalarType(); | |||
30936 | SDValue Mask = N->getMask(); | |||
30937 | MVT MaskVT = Mask.getSimpleValueType(); | |||
30938 | SDValue PassThru = N->getPassThru(); | |||
30939 | SDLoc dl(Op); | |||
30940 | ||||
30941 | // Handle AVX masked loads which don't support passthru other than 0. | |||
30942 | if (MaskVT.getVectorElementType() != MVT::i1) { | |||
30943 | // We also allow undef in the isel pattern. | |||
30944 | if (PassThru.isUndef() || ISD::isBuildVectorAllZeros(PassThru.getNode())) | |||
30945 | return Op; | |||
30946 | ||||
30947 | SDValue NewLoad = DAG.getMaskedLoad( | |||
30948 | VT, dl, N->getChain(), N->getBasePtr(), N->getOffset(), Mask, | |||
30949 | getZeroVector(VT, Subtarget, DAG, dl), N->getMemoryVT(), | |||
30950 | N->getMemOperand(), N->getAddressingMode(), N->getExtensionType(), | |||
30951 | N->isExpandingLoad()); | |||
30952 | // Emit a blend. | |||
30953 | SDValue Select = DAG.getNode(ISD::VSELECT, dl, VT, Mask, NewLoad, PassThru); | |||
30954 | return DAG.getMergeValues({ Select, NewLoad.getValue(1) }, dl); | |||
30955 | } | |||
30956 | ||||
30957 | assert((!N->isExpandingLoad() || Subtarget.hasAVX512()) &&(static_cast <bool> ((!N->isExpandingLoad() || Subtarget .hasAVX512()) && "Expanding masked load is supported on AVX-512 target only!" ) ? void (0) : __assert_fail ("(!N->isExpandingLoad() || Subtarget.hasAVX512()) && \"Expanding masked load is supported on AVX-512 target only!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30958, __extension__ __PRETTY_FUNCTION__)) | |||
30958 | "Expanding masked load is supported on AVX-512 target only!")(static_cast <bool> ((!N->isExpandingLoad() || Subtarget .hasAVX512()) && "Expanding masked load is supported on AVX-512 target only!" ) ? void (0) : __assert_fail ("(!N->isExpandingLoad() || Subtarget.hasAVX512()) && \"Expanding masked load is supported on AVX-512 target only!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30958, __extension__ __PRETTY_FUNCTION__)); | |||
30959 | ||||
30960 | assert((!N->isExpandingLoad() || ScalarVT.getSizeInBits() >= 32) &&(static_cast <bool> ((!N->isExpandingLoad() || ScalarVT .getSizeInBits() >= 32) && "Expanding masked load is supported for 32 and 64-bit types only!" ) ? void (0) : __assert_fail ("(!N->isExpandingLoad() || ScalarVT.getSizeInBits() >= 32) && \"Expanding masked load is supported for 32 and 64-bit types only!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30961, __extension__ __PRETTY_FUNCTION__)) | |||
30961 | "Expanding masked load is supported for 32 and 64-bit types only!")(static_cast <bool> ((!N->isExpandingLoad() || ScalarVT .getSizeInBits() >= 32) && "Expanding masked load is supported for 32 and 64-bit types only!" ) ? void (0) : __assert_fail ("(!N->isExpandingLoad() || ScalarVT.getSizeInBits() >= 32) && \"Expanding masked load is supported for 32 and 64-bit types only!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30961, __extension__ __PRETTY_FUNCTION__)); | |||
30962 | ||||
30963 | assert(Subtarget.hasAVX512() && !Subtarget.hasVLX() && !VT.is512BitVector() &&(static_cast <bool> (Subtarget.hasAVX512() && ! Subtarget.hasVLX() && !VT.is512BitVector() && "Cannot lower masked load op.") ? void (0) : __assert_fail ( "Subtarget.hasAVX512() && !Subtarget.hasVLX() && !VT.is512BitVector() && \"Cannot lower masked load op.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30964, __extension__ __PRETTY_FUNCTION__)) | |||
30964 | "Cannot lower masked load op.")(static_cast <bool> (Subtarget.hasAVX512() && ! Subtarget.hasVLX() && !VT.is512BitVector() && "Cannot lower masked load op.") ? void (0) : __assert_fail ( "Subtarget.hasAVX512() && !Subtarget.hasVLX() && !VT.is512BitVector() && \"Cannot lower masked load op.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30964, __extension__ __PRETTY_FUNCTION__)); | |||
30965 | ||||
30966 | assert((ScalarVT.getSizeInBits() >= 32 ||(static_cast <bool> ((ScalarVT.getSizeInBits() >= 32 || (Subtarget.hasBWI() && (ScalarVT == MVT::i8 || ScalarVT == MVT::i16))) && "Unsupported masked load op.") ? void (0) : __assert_fail ("(ScalarVT.getSizeInBits() >= 32 || (Subtarget.hasBWI() && (ScalarVT == MVT::i8 || ScalarVT == MVT::i16))) && \"Unsupported masked load op.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30969, __extension__ __PRETTY_FUNCTION__)) | |||
30967 | (Subtarget.hasBWI() &&(static_cast <bool> ((ScalarVT.getSizeInBits() >= 32 || (Subtarget.hasBWI() && (ScalarVT == MVT::i8 || ScalarVT == MVT::i16))) && "Unsupported masked load op.") ? void (0) : __assert_fail ("(ScalarVT.getSizeInBits() >= 32 || (Subtarget.hasBWI() && (ScalarVT == MVT::i8 || ScalarVT == MVT::i16))) && \"Unsupported masked load op.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30969, __extension__ __PRETTY_FUNCTION__)) | |||
30968 | (ScalarVT == MVT::i8 || ScalarVT == MVT::i16))) &&(static_cast <bool> ((ScalarVT.getSizeInBits() >= 32 || (Subtarget.hasBWI() && (ScalarVT == MVT::i8 || ScalarVT == MVT::i16))) && "Unsupported masked load op.") ? void (0) : __assert_fail ("(ScalarVT.getSizeInBits() >= 32 || (Subtarget.hasBWI() && (ScalarVT == MVT::i8 || ScalarVT == MVT::i16))) && \"Unsupported masked load op.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30969, __extension__ __PRETTY_FUNCTION__)) | |||
30969 | "Unsupported masked load op.")(static_cast <bool> ((ScalarVT.getSizeInBits() >= 32 || (Subtarget.hasBWI() && (ScalarVT == MVT::i8 || ScalarVT == MVT::i16))) && "Unsupported masked load op.") ? void (0) : __assert_fail ("(ScalarVT.getSizeInBits() >= 32 || (Subtarget.hasBWI() && (ScalarVT == MVT::i8 || ScalarVT == MVT::i16))) && \"Unsupported masked load op.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30969, __extension__ __PRETTY_FUNCTION__)); | |||
30970 | ||||
30971 | // This operation is legal for targets with VLX, but without | |||
30972 | // VLX the vector should be widened to 512 bit | |||
30973 | unsigned NumEltsInWideVec = 512 / VT.getScalarSizeInBits(); | |||
30974 | MVT WideDataVT = MVT::getVectorVT(ScalarVT, NumEltsInWideVec); | |||
30975 | PassThru = ExtendToType(PassThru, WideDataVT, DAG); | |||
30976 | ||||
30977 | // Mask element has to be i1. | |||
30978 | assert(Mask.getSimpleValueType().getScalarType() == MVT::i1 &&(static_cast <bool> (Mask.getSimpleValueType().getScalarType () == MVT::i1 && "Unexpected mask type") ? void (0) : __assert_fail ("Mask.getSimpleValueType().getScalarType() == MVT::i1 && \"Unexpected mask type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30979, __extension__ __PRETTY_FUNCTION__)) | |||
30979 | "Unexpected mask type")(static_cast <bool> (Mask.getSimpleValueType().getScalarType () == MVT::i1 && "Unexpected mask type") ? void (0) : __assert_fail ("Mask.getSimpleValueType().getScalarType() == MVT::i1 && \"Unexpected mask type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 30979, __extension__ __PRETTY_FUNCTION__)); | |||
30980 | ||||
30981 | MVT WideMaskVT = MVT::getVectorVT(MVT::i1, NumEltsInWideVec); | |||
30982 | ||||
30983 | Mask = ExtendToType(Mask, WideMaskVT, DAG, true); | |||
30984 | SDValue NewLoad = DAG.getMaskedLoad( | |||
30985 | WideDataVT, dl, N->getChain(), N->getBasePtr(), N->getOffset(), Mask, | |||
30986 | PassThru, N->getMemoryVT(), N->getMemOperand(), N->getAddressingMode(), | |||
30987 | N->getExtensionType(), N->isExpandingLoad()); | |||
30988 | ||||
30989 | SDValue Extract = | |||
30990 | DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, NewLoad.getValue(0), | |||
30991 | DAG.getIntPtrConstant(0, dl)); | |||
30992 | SDValue RetOps[] = {Extract, NewLoad.getValue(1)}; | |||
30993 | return DAG.getMergeValues(RetOps, dl); | |||
30994 | } | |||
30995 | ||||
30996 | static SDValue LowerMSTORE(SDValue Op, const X86Subtarget &Subtarget, | |||
30997 | SelectionDAG &DAG) { | |||
30998 | MaskedStoreSDNode *N = cast<MaskedStoreSDNode>(Op.getNode()); | |||
30999 | SDValue DataToStore = N->getValue(); | |||
31000 | MVT VT = DataToStore.getSimpleValueType(); | |||
31001 | MVT ScalarVT = VT.getScalarType(); | |||
31002 | SDValue Mask = N->getMask(); | |||
31003 | SDLoc dl(Op); | |||
31004 | ||||
31005 | assert((!N->isCompressingStore() || Subtarget.hasAVX512()) &&(static_cast <bool> ((!N->isCompressingStore() || Subtarget .hasAVX512()) && "Expanding masked load is supported on AVX-512 target only!" ) ? void (0) : __assert_fail ("(!N->isCompressingStore() || Subtarget.hasAVX512()) && \"Expanding masked load is supported on AVX-512 target only!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31006, __extension__ __PRETTY_FUNCTION__)) | |||
31006 | "Expanding masked load is supported on AVX-512 target only!")(static_cast <bool> ((!N->isCompressingStore() || Subtarget .hasAVX512()) && "Expanding masked load is supported on AVX-512 target only!" ) ? void (0) : __assert_fail ("(!N->isCompressingStore() || Subtarget.hasAVX512()) && \"Expanding masked load is supported on AVX-512 target only!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31006, __extension__ __PRETTY_FUNCTION__)); | |||
31007 | ||||
31008 | assert((!N->isCompressingStore() || ScalarVT.getSizeInBits() >= 32) &&(static_cast <bool> ((!N->isCompressingStore() || ScalarVT .getSizeInBits() >= 32) && "Expanding masked load is supported for 32 and 64-bit types only!" ) ? void (0) : __assert_fail ("(!N->isCompressingStore() || ScalarVT.getSizeInBits() >= 32) && \"Expanding masked load is supported for 32 and 64-bit types only!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31009, __extension__ __PRETTY_FUNCTION__)) | |||
31009 | "Expanding masked load is supported for 32 and 64-bit types only!")(static_cast <bool> ((!N->isCompressingStore() || ScalarVT .getSizeInBits() >= 32) && "Expanding masked load is supported for 32 and 64-bit types only!" ) ? void (0) : __assert_fail ("(!N->isCompressingStore() || ScalarVT.getSizeInBits() >= 32) && \"Expanding masked load is supported for 32 and 64-bit types only!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31009, __extension__ __PRETTY_FUNCTION__)); | |||
31010 | ||||
31011 | assert(Subtarget.hasAVX512() && !Subtarget.hasVLX() && !VT.is512BitVector() &&(static_cast <bool> (Subtarget.hasAVX512() && ! Subtarget.hasVLX() && !VT.is512BitVector() && "Cannot lower masked store op.") ? void (0) : __assert_fail ( "Subtarget.hasAVX512() && !Subtarget.hasVLX() && !VT.is512BitVector() && \"Cannot lower masked store op.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31012, __extension__ __PRETTY_FUNCTION__)) | |||
31012 | "Cannot lower masked store op.")(static_cast <bool> (Subtarget.hasAVX512() && ! Subtarget.hasVLX() && !VT.is512BitVector() && "Cannot lower masked store op.") ? void (0) : __assert_fail ( "Subtarget.hasAVX512() && !Subtarget.hasVLX() && !VT.is512BitVector() && \"Cannot lower masked store op.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31012, __extension__ __PRETTY_FUNCTION__)); | |||
31013 | ||||
31014 | assert((ScalarVT.getSizeInBits() >= 32 ||(static_cast <bool> ((ScalarVT.getSizeInBits() >= 32 || (Subtarget.hasBWI() && (ScalarVT == MVT::i8 || ScalarVT == MVT::i16))) && "Unsupported masked store op.") ? void (0) : __assert_fail ("(ScalarVT.getSizeInBits() >= 32 || (Subtarget.hasBWI() && (ScalarVT == MVT::i8 || ScalarVT == MVT::i16))) && \"Unsupported masked store op.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31017, __extension__ __PRETTY_FUNCTION__)) | |||
31015 | (Subtarget.hasBWI() &&(static_cast <bool> ((ScalarVT.getSizeInBits() >= 32 || (Subtarget.hasBWI() && (ScalarVT == MVT::i8 || ScalarVT == MVT::i16))) && "Unsupported masked store op.") ? void (0) : __assert_fail ("(ScalarVT.getSizeInBits() >= 32 || (Subtarget.hasBWI() && (ScalarVT == MVT::i8 || ScalarVT == MVT::i16))) && \"Unsupported masked store op.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31017, __extension__ __PRETTY_FUNCTION__)) | |||
31016 | (ScalarVT == MVT::i8 || ScalarVT == MVT::i16))) &&(static_cast <bool> ((ScalarVT.getSizeInBits() >= 32 || (Subtarget.hasBWI() && (ScalarVT == MVT::i8 || ScalarVT == MVT::i16))) && "Unsupported masked store op.") ? void (0) : __assert_fail ("(ScalarVT.getSizeInBits() >= 32 || (Subtarget.hasBWI() && (ScalarVT == MVT::i8 || ScalarVT == MVT::i16))) && \"Unsupported masked store op.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31017, __extension__ __PRETTY_FUNCTION__)) | |||
31017 | "Unsupported masked store op.")(static_cast <bool> ((ScalarVT.getSizeInBits() >= 32 || (Subtarget.hasBWI() && (ScalarVT == MVT::i8 || ScalarVT == MVT::i16))) && "Unsupported masked store op.") ? void (0) : __assert_fail ("(ScalarVT.getSizeInBits() >= 32 || (Subtarget.hasBWI() && (ScalarVT == MVT::i8 || ScalarVT == MVT::i16))) && \"Unsupported masked store op.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31017, __extension__ __PRETTY_FUNCTION__)); | |||
31018 | ||||
31019 | // This operation is legal for targets with VLX, but without | |||
31020 | // VLX the vector should be widened to 512 bit | |||
31021 | unsigned NumEltsInWideVec = 512/VT.getScalarSizeInBits(); | |||
31022 | MVT WideDataVT = MVT::getVectorVT(ScalarVT, NumEltsInWideVec); | |||
31023 | ||||
31024 | // Mask element has to be i1. | |||
31025 | assert(Mask.getSimpleValueType().getScalarType() == MVT::i1 &&(static_cast <bool> (Mask.getSimpleValueType().getScalarType () == MVT::i1 && "Unexpected mask type") ? void (0) : __assert_fail ("Mask.getSimpleValueType().getScalarType() == MVT::i1 && \"Unexpected mask type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31026, __extension__ __PRETTY_FUNCTION__)) | |||
31026 | "Unexpected mask type")(static_cast <bool> (Mask.getSimpleValueType().getScalarType () == MVT::i1 && "Unexpected mask type") ? void (0) : __assert_fail ("Mask.getSimpleValueType().getScalarType() == MVT::i1 && \"Unexpected mask type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31026, __extension__ __PRETTY_FUNCTION__)); | |||
31027 | ||||
31028 | MVT WideMaskVT = MVT::getVectorVT(MVT::i1, NumEltsInWideVec); | |||
31029 | ||||
31030 | DataToStore = ExtendToType(DataToStore, WideDataVT, DAG); | |||
31031 | Mask = ExtendToType(Mask, WideMaskVT, DAG, true); | |||
31032 | return DAG.getMaskedStore(N->getChain(), dl, DataToStore, N->getBasePtr(), | |||
31033 | N->getOffset(), Mask, N->getMemoryVT(), | |||
31034 | N->getMemOperand(), N->getAddressingMode(), | |||
31035 | N->isTruncatingStore(), N->isCompressingStore()); | |||
31036 | } | |||
31037 | ||||
31038 | static SDValue LowerMGATHER(SDValue Op, const X86Subtarget &Subtarget, | |||
31039 | SelectionDAG &DAG) { | |||
31040 | assert(Subtarget.hasAVX2() &&(static_cast <bool> (Subtarget.hasAVX2() && "MGATHER/MSCATTER are supported on AVX-512/AVX-2 arch only" ) ? void (0) : __assert_fail ("Subtarget.hasAVX2() && \"MGATHER/MSCATTER are supported on AVX-512/AVX-2 arch only\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31041, __extension__ __PRETTY_FUNCTION__)) | |||
31041 | "MGATHER/MSCATTER are supported on AVX-512/AVX-2 arch only")(static_cast <bool> (Subtarget.hasAVX2() && "MGATHER/MSCATTER are supported on AVX-512/AVX-2 arch only" ) ? void (0) : __assert_fail ("Subtarget.hasAVX2() && \"MGATHER/MSCATTER are supported on AVX-512/AVX-2 arch only\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31041, __extension__ __PRETTY_FUNCTION__)); | |||
31042 | ||||
31043 | MaskedGatherSDNode *N = cast<MaskedGatherSDNode>(Op.getNode()); | |||
31044 | SDLoc dl(Op); | |||
31045 | MVT VT = Op.getSimpleValueType(); | |||
31046 | SDValue Index = N->getIndex(); | |||
31047 | SDValue Mask = N->getMask(); | |||
31048 | SDValue PassThru = N->getPassThru(); | |||
31049 | MVT IndexVT = Index.getSimpleValueType(); | |||
31050 | ||||
31051 | assert(VT.getScalarSizeInBits() >= 32 && "Unsupported gather op")(static_cast <bool> (VT.getScalarSizeInBits() >= 32 && "Unsupported gather op") ? void (0) : __assert_fail ("VT.getScalarSizeInBits() >= 32 && \"Unsupported gather op\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31051, __extension__ __PRETTY_FUNCTION__)); | |||
31052 | ||||
31053 | // If the index is v2i32, we're being called by type legalization. | |||
31054 | if (IndexVT == MVT::v2i32) | |||
31055 | return SDValue(); | |||
31056 | ||||
31057 | // If we don't have VLX and neither the passthru or index is 512-bits, we | |||
31058 | // need to widen until one is. | |||
31059 | MVT OrigVT = VT; | |||
31060 | if (Subtarget.hasAVX512() && !Subtarget.hasVLX() && !VT.is512BitVector() && | |||
31061 | !IndexVT.is512BitVector()) { | |||
31062 | // Determine how much we need to widen by to get a 512-bit type. | |||
31063 | unsigned Factor = std::min(512/VT.getSizeInBits(), | |||
31064 | 512/IndexVT.getSizeInBits()); | |||
31065 | ||||
31066 | unsigned NumElts = VT.getVectorNumElements() * Factor; | |||
31067 | ||||
31068 | VT = MVT::getVectorVT(VT.getVectorElementType(), NumElts); | |||
31069 | IndexVT = MVT::getVectorVT(IndexVT.getVectorElementType(), NumElts); | |||
31070 | MVT MaskVT = MVT::getVectorVT(MVT::i1, NumElts); | |||
31071 | ||||
31072 | PassThru = ExtendToType(PassThru, VT, DAG); | |||
31073 | Index = ExtendToType(Index, IndexVT, DAG); | |||
31074 | Mask = ExtendToType(Mask, MaskVT, DAG, true); | |||
31075 | } | |||
31076 | ||||
31077 | SDValue Ops[] = { N->getChain(), PassThru, Mask, N->getBasePtr(), Index, | |||
31078 | N->getScale() }; | |||
31079 | SDValue NewGather = DAG.getMemIntrinsicNode( | |||
31080 | X86ISD::MGATHER, dl, DAG.getVTList(VT, MVT::Other), Ops, N->getMemoryVT(), | |||
31081 | N->getMemOperand()); | |||
31082 | SDValue Extract = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, OrigVT, | |||
31083 | NewGather, DAG.getIntPtrConstant(0, dl)); | |||
31084 | return DAG.getMergeValues({Extract, NewGather.getValue(1)}, dl); | |||
31085 | } | |||
31086 | ||||
31087 | static SDValue LowerADDRSPACECAST(SDValue Op, SelectionDAG &DAG) { | |||
31088 | SDLoc dl(Op); | |||
31089 | SDValue Src = Op.getOperand(0); | |||
31090 | MVT DstVT = Op.getSimpleValueType(); | |||
31091 | ||||
31092 | AddrSpaceCastSDNode *N = cast<AddrSpaceCastSDNode>(Op.getNode()); | |||
31093 | unsigned SrcAS = N->getSrcAddressSpace(); | |||
31094 | ||||
31095 | assert(SrcAS != N->getDestAddressSpace() &&(static_cast <bool> (SrcAS != N->getDestAddressSpace () && "addrspacecast must be between different address spaces" ) ? void (0) : __assert_fail ("SrcAS != N->getDestAddressSpace() && \"addrspacecast must be between different address spaces\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31096, __extension__ __PRETTY_FUNCTION__)) | |||
31096 | "addrspacecast must be between different address spaces")(static_cast <bool> (SrcAS != N->getDestAddressSpace () && "addrspacecast must be between different address spaces" ) ? void (0) : __assert_fail ("SrcAS != N->getDestAddressSpace() && \"addrspacecast must be between different address spaces\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31096, __extension__ __PRETTY_FUNCTION__)); | |||
31097 | ||||
31098 | if (SrcAS == X86AS::PTR32_UPTR && DstVT == MVT::i64) { | |||
31099 | Op = DAG.getNode(ISD::ZERO_EXTEND, dl, DstVT, Src); | |||
31100 | } else if (DstVT == MVT::i64) { | |||
31101 | Op = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Src); | |||
31102 | } else if (DstVT == MVT::i32) { | |||
31103 | Op = DAG.getNode(ISD::TRUNCATE, dl, DstVT, Src); | |||
31104 | } else { | |||
31105 | report_fatal_error("Bad address space in addrspacecast"); | |||
31106 | } | |||
31107 | return Op; | |||
31108 | } | |||
31109 | ||||
31110 | SDValue X86TargetLowering::LowerGC_TRANSITION(SDValue Op, | |||
31111 | SelectionDAG &DAG) const { | |||
31112 | // TODO: Eventually, the lowering of these nodes should be informed by or | |||
31113 | // deferred to the GC strategy for the function in which they appear. For | |||
31114 | // now, however, they must be lowered to something. Since they are logically | |||
31115 | // no-ops in the case of a null GC strategy (or a GC strategy which does not | |||
31116 | // require special handling for these nodes), lower them as literal NOOPs for | |||
31117 | // the time being. | |||
31118 | SmallVector<SDValue, 2> Ops; | |||
31119 | ||||
31120 | Ops.push_back(Op.getOperand(0)); | |||
31121 | if (Op->getGluedNode()) | |||
31122 | Ops.push_back(Op->getOperand(Op->getNumOperands() - 1)); | |||
31123 | ||||
31124 | SDLoc OpDL(Op); | |||
31125 | SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue); | |||
31126 | SDValue NOOP(DAG.getMachineNode(X86::NOOP, SDLoc(Op), VTs, Ops), 0); | |||
31127 | ||||
31128 | return NOOP; | |||
31129 | } | |||
31130 | ||||
31131 | // Custom split CVTPS2PH with wide types. | |||
31132 | static SDValue LowerCVTPS2PH(SDValue Op, SelectionDAG &DAG) { | |||
31133 | SDLoc dl(Op); | |||
31134 | EVT VT = Op.getValueType(); | |||
31135 | SDValue Lo, Hi; | |||
31136 | std::tie(Lo, Hi) = DAG.SplitVectorOperand(Op.getNode(), 0); | |||
31137 | EVT LoVT, HiVT; | |||
31138 | std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VT); | |||
31139 | SDValue RC = Op.getOperand(1); | |||
31140 | Lo = DAG.getNode(X86ISD::CVTPS2PH, dl, LoVT, Lo, RC); | |||
31141 | Hi = DAG.getNode(X86ISD::CVTPS2PH, dl, HiVT, Hi, RC); | |||
31142 | return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Lo, Hi); | |||
31143 | } | |||
31144 | ||||
31145 | /// Provide custom lowering hooks for some operations. | |||
31146 | SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const { | |||
31147 | switch (Op.getOpcode()) { | |||
31148 | default: llvm_unreachable("Should not custom lower this!")::llvm::llvm_unreachable_internal("Should not custom lower this!" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31148); | |||
31149 | case ISD::ATOMIC_FENCE: return LowerATOMIC_FENCE(Op, Subtarget, DAG); | |||
31150 | case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: | |||
31151 | return LowerCMP_SWAP(Op, Subtarget, DAG); | |||
31152 | case ISD::CTPOP: return LowerCTPOP(Op, Subtarget, DAG); | |||
31153 | case ISD::ATOMIC_LOAD_ADD: | |||
31154 | case ISD::ATOMIC_LOAD_SUB: | |||
31155 | case ISD::ATOMIC_LOAD_OR: | |||
31156 | case ISD::ATOMIC_LOAD_XOR: | |||
31157 | case ISD::ATOMIC_LOAD_AND: return lowerAtomicArith(Op, DAG, Subtarget); | |||
31158 | case ISD::ATOMIC_STORE: return LowerATOMIC_STORE(Op, DAG, Subtarget); | |||
31159 | case ISD::BITREVERSE: return LowerBITREVERSE(Op, Subtarget, DAG); | |||
31160 | case ISD::PARITY: return LowerPARITY(Op, Subtarget, DAG); | |||
31161 | case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG); | |||
31162 | case ISD::CONCAT_VECTORS: return LowerCONCAT_VECTORS(Op, Subtarget, DAG); | |||
31163 | case ISD::VECTOR_SHUFFLE: return lowerVECTOR_SHUFFLE(Op, Subtarget, DAG); | |||
31164 | case ISD::VSELECT: return LowerVSELECT(Op, DAG); | |||
31165 | case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG); | |||
31166 | case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG); | |||
31167 | case ISD::INSERT_SUBVECTOR: return LowerINSERT_SUBVECTOR(Op, Subtarget,DAG); | |||
31168 | case ISD::EXTRACT_SUBVECTOR: return LowerEXTRACT_SUBVECTOR(Op,Subtarget,DAG); | |||
31169 | case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, Subtarget,DAG); | |||
31170 | case ISD::ConstantPool: return LowerConstantPool(Op, DAG); | |||
31171 | case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG); | |||
31172 | case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG); | |||
31173 | case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG); | |||
31174 | case ISD::BlockAddress: return LowerBlockAddress(Op, DAG); | |||
31175 | case ISD::SHL_PARTS: | |||
31176 | case ISD::SRA_PARTS: | |||
31177 | case ISD::SRL_PARTS: return LowerShiftParts(Op, DAG); | |||
31178 | case ISD::FSHL: | |||
31179 | case ISD::FSHR: return LowerFunnelShift(Op, Subtarget, DAG); | |||
31180 | case ISD::STRICT_SINT_TO_FP: | |||
31181 | case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG); | |||
31182 | case ISD::STRICT_UINT_TO_FP: | |||
31183 | case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG); | |||
31184 | case ISD::TRUNCATE: return LowerTRUNCATE(Op, DAG); | |||
31185 | case ISD::ZERO_EXTEND: return LowerZERO_EXTEND(Op, Subtarget, DAG); | |||
31186 | case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, Subtarget, DAG); | |||
31187 | case ISD::ANY_EXTEND: return LowerANY_EXTEND(Op, Subtarget, DAG); | |||
31188 | case ISD::ZERO_EXTEND_VECTOR_INREG: | |||
31189 | case ISD::SIGN_EXTEND_VECTOR_INREG: | |||
31190 | return LowerEXTEND_VECTOR_INREG(Op, Subtarget, DAG); | |||
31191 | case ISD::FP_TO_SINT: | |||
31192 | case ISD::STRICT_FP_TO_SINT: | |||
31193 | case ISD::FP_TO_UINT: | |||
31194 | case ISD::STRICT_FP_TO_UINT: return LowerFP_TO_INT(Op, DAG); | |||
31195 | case ISD::FP_TO_SINT_SAT: | |||
31196 | case ISD::FP_TO_UINT_SAT: return LowerFP_TO_INT_SAT(Op, DAG); | |||
31197 | case ISD::FP_EXTEND: | |||
31198 | case ISD::STRICT_FP_EXTEND: return LowerFP_EXTEND(Op, DAG); | |||
31199 | case ISD::FP_ROUND: | |||
31200 | case ISD::STRICT_FP_ROUND: return LowerFP_ROUND(Op, DAG); | |||
31201 | case ISD::FP16_TO_FP: | |||
31202 | case ISD::STRICT_FP16_TO_FP: return LowerFP16_TO_FP(Op, DAG); | |||
31203 | case ISD::FP_TO_FP16: | |||
31204 | case ISD::STRICT_FP_TO_FP16: return LowerFP_TO_FP16(Op, DAG); | |||
31205 | case ISD::LOAD: return LowerLoad(Op, Subtarget, DAG); | |||
31206 | case ISD::STORE: return LowerStore(Op, Subtarget, DAG); | |||
31207 | case ISD::FADD: | |||
31208 | case ISD::FSUB: return lowerFaddFsub(Op, DAG); | |||
31209 | case ISD::STRICT_FROUND: | |||
31210 | case ISD::FROUND: return LowerFROUND(Op, DAG); | |||
31211 | case ISD::FABS: | |||
31212 | case ISD::FNEG: return LowerFABSorFNEG(Op, DAG); | |||
31213 | case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG); | |||
31214 | case ISD::FGETSIGN: return LowerFGETSIGN(Op, DAG); | |||
31215 | case ISD::LRINT: | |||
31216 | case ISD::LLRINT: return LowerLRINT_LLRINT(Op, DAG); | |||
31217 | case ISD::SETCC: | |||
31218 | case ISD::STRICT_FSETCC: | |||
31219 | case ISD::STRICT_FSETCCS: return LowerSETCC(Op, DAG); | |||
31220 | case ISD::SETCCCARRY: return LowerSETCCCARRY(Op, DAG); | |||
31221 | case ISD::SELECT: return LowerSELECT(Op, DAG); | |||
31222 | case ISD::BRCOND: return LowerBRCOND(Op, DAG); | |||
31223 | case ISD::JumpTable: return LowerJumpTable(Op, DAG); | |||
31224 | case ISD::VASTART: return LowerVASTART(Op, DAG); | |||
31225 | case ISD::VAARG: return LowerVAARG(Op, DAG); | |||
31226 | case ISD::VACOPY: return LowerVACOPY(Op, Subtarget, DAG); | |||
31227 | case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG); | |||
31228 | case ISD::INTRINSIC_VOID: | |||
31229 | case ISD::INTRINSIC_W_CHAIN: return LowerINTRINSIC_W_CHAIN(Op, Subtarget, DAG); | |||
31230 | case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG); | |||
31231 | case ISD::ADDROFRETURNADDR: return LowerADDROFRETURNADDR(Op, DAG); | |||
31232 | case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG); | |||
31233 | case ISD::FRAME_TO_ARGS_OFFSET: | |||
31234 | return LowerFRAME_TO_ARGS_OFFSET(Op, DAG); | |||
31235 | case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG); | |||
31236 | case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG); | |||
31237 | case ISD::EH_SJLJ_SETJMP: return lowerEH_SJLJ_SETJMP(Op, DAG); | |||
31238 | case ISD::EH_SJLJ_LONGJMP: return lowerEH_SJLJ_LONGJMP(Op, DAG); | |||
31239 | case ISD::EH_SJLJ_SETUP_DISPATCH: | |||
31240 | return lowerEH_SJLJ_SETUP_DISPATCH(Op, DAG); | |||
31241 | case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG); | |||
31242 | case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG); | |||
31243 | case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG); | |||
31244 | case ISD::SET_ROUNDING: return LowerSET_ROUNDING(Op, DAG); | |||
31245 | case ISD::CTLZ: | |||
31246 | case ISD::CTLZ_ZERO_UNDEF: return LowerCTLZ(Op, Subtarget, DAG); | |||
31247 | case ISD::CTTZ: | |||
31248 | case ISD::CTTZ_ZERO_UNDEF: return LowerCTTZ(Op, Subtarget, DAG); | |||
31249 | case ISD::MUL: return LowerMUL(Op, Subtarget, DAG); | |||
31250 | case ISD::MULHS: | |||
31251 | case ISD::MULHU: return LowerMULH(Op, Subtarget, DAG); | |||
31252 | case ISD::ROTL: | |||
31253 | case ISD::ROTR: return LowerRotate(Op, Subtarget, DAG); | |||
31254 | case ISD::SRA: | |||
31255 | case ISD::SRL: | |||
31256 | case ISD::SHL: return LowerShift(Op, Subtarget, DAG); | |||
31257 | case ISD::SADDO: | |||
31258 | case ISD::UADDO: | |||
31259 | case ISD::SSUBO: | |||
31260 | case ISD::USUBO: return LowerXALUO(Op, DAG); | |||
31261 | case ISD::SMULO: | |||
31262 | case ISD::UMULO: return LowerMULO(Op, Subtarget, DAG); | |||
31263 | case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, Subtarget,DAG); | |||
31264 | case ISD::BITCAST: return LowerBITCAST(Op, Subtarget, DAG); | |||
31265 | case ISD::SADDO_CARRY: | |||
31266 | case ISD::SSUBO_CARRY: | |||
31267 | case ISD::ADDCARRY: | |||
31268 | case ISD::SUBCARRY: return LowerADDSUBCARRY(Op, DAG); | |||
31269 | case ISD::ADD: | |||
31270 | case ISD::SUB: return lowerAddSub(Op, DAG, Subtarget); | |||
31271 | case ISD::UADDSAT: | |||
31272 | case ISD::SADDSAT: | |||
31273 | case ISD::USUBSAT: | |||
31274 | case ISD::SSUBSAT: return LowerADDSAT_SUBSAT(Op, DAG, Subtarget); | |||
31275 | case ISD::SMAX: | |||
31276 | case ISD::SMIN: | |||
31277 | case ISD::UMAX: | |||
31278 | case ISD::UMIN: return LowerMINMAX(Op, DAG); | |||
31279 | case ISD::ABS: return LowerABS(Op, Subtarget, DAG); | |||
31280 | case ISD::FSINCOS: return LowerFSINCOS(Op, Subtarget, DAG); | |||
31281 | case ISD::MLOAD: return LowerMLOAD(Op, Subtarget, DAG); | |||
31282 | case ISD::MSTORE: return LowerMSTORE(Op, Subtarget, DAG); | |||
31283 | case ISD::MGATHER: return LowerMGATHER(Op, Subtarget, DAG); | |||
31284 | case ISD::MSCATTER: return LowerMSCATTER(Op, Subtarget, DAG); | |||
31285 | case ISD::GC_TRANSITION_START: | |||
31286 | case ISD::GC_TRANSITION_END: return LowerGC_TRANSITION(Op, DAG); | |||
31287 | case ISD::ADDRSPACECAST: return LowerADDRSPACECAST(Op, DAG); | |||
31288 | case X86ISD::CVTPS2PH: return LowerCVTPS2PH(Op, DAG); | |||
31289 | } | |||
31290 | } | |||
31291 | ||||
31292 | /// Replace a node with an illegal result type with a new node built out of | |||
31293 | /// custom code. | |||
31294 | void X86TargetLowering::ReplaceNodeResults(SDNode *N, | |||
31295 | SmallVectorImpl<SDValue>&Results, | |||
31296 | SelectionDAG &DAG) const { | |||
31297 | SDLoc dl(N); | |||
31298 | switch (N->getOpcode()) { | |||
31299 | default: | |||
31300 | #ifndef NDEBUG | |||
31301 | dbgs() << "ReplaceNodeResults: "; | |||
31302 | N->dump(&DAG); | |||
31303 | #endif | |||
31304 | llvm_unreachable("Do not know how to custom type legalize this operation!")::llvm::llvm_unreachable_internal("Do not know how to custom type legalize this operation!" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31304); | |||
31305 | case X86ISD::CVTPH2PS: { | |||
31306 | EVT VT = N->getValueType(0); | |||
31307 | SDValue Lo, Hi; | |||
31308 | std::tie(Lo, Hi) = DAG.SplitVectorOperand(N, 0); | |||
31309 | EVT LoVT, HiVT; | |||
31310 | std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VT); | |||
31311 | Lo = DAG.getNode(X86ISD::CVTPH2PS, dl, LoVT, Lo); | |||
31312 | Hi = DAG.getNode(X86ISD::CVTPH2PS, dl, HiVT, Hi); | |||
31313 | SDValue Res = DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Lo, Hi); | |||
31314 | Results.push_back(Res); | |||
31315 | return; | |||
31316 | } | |||
31317 | case X86ISD::STRICT_CVTPH2PS: { | |||
31318 | EVT VT = N->getValueType(0); | |||
31319 | SDValue Lo, Hi; | |||
31320 | std::tie(Lo, Hi) = DAG.SplitVectorOperand(N, 1); | |||
31321 | EVT LoVT, HiVT; | |||
31322 | std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(VT); | |||
31323 | Lo = DAG.getNode(X86ISD::STRICT_CVTPH2PS, dl, {LoVT, MVT::Other}, | |||
31324 | {N->getOperand(0), Lo}); | |||
31325 | Hi = DAG.getNode(X86ISD::STRICT_CVTPH2PS, dl, {HiVT, MVT::Other}, | |||
31326 | {N->getOperand(0), Hi}); | |||
31327 | SDValue Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, | |||
31328 | Lo.getValue(1), Hi.getValue(1)); | |||
31329 | SDValue Res = DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Lo, Hi); | |||
31330 | Results.push_back(Res); | |||
31331 | Results.push_back(Chain); | |||
31332 | return; | |||
31333 | } | |||
31334 | case X86ISD::CVTPS2PH: | |||
31335 | Results.push_back(LowerCVTPS2PH(SDValue(N, 0), DAG)); | |||
31336 | return; | |||
31337 | case ISD::CTPOP: { | |||
31338 | assert(N->getValueType(0) == MVT::i64 && "Unexpected VT!")(static_cast <bool> (N->getValueType(0) == MVT::i64 && "Unexpected VT!") ? void (0) : __assert_fail ("N->getValueType(0) == MVT::i64 && \"Unexpected VT!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31338, __extension__ __PRETTY_FUNCTION__)); | |||
31339 | // Use a v2i64 if possible. | |||
31340 | bool NoImplicitFloatOps = | |||
31341 | DAG.getMachineFunction().getFunction().hasFnAttribute( | |||
31342 | Attribute::NoImplicitFloat); | |||
31343 | if (isTypeLegal(MVT::v2i64) && !NoImplicitFloatOps) { | |||
31344 | SDValue Wide = | |||
31345 | DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, N->getOperand(0)); | |||
31346 | Wide = DAG.getNode(ISD::CTPOP, dl, MVT::v2i64, Wide); | |||
31347 | // Bit count should fit in 32-bits, extract it as that and then zero | |||
31348 | // extend to i64. Otherwise we end up extracting bits 63:32 separately. | |||
31349 | Wide = DAG.getNode(ISD::BITCAST, dl, MVT::v4i32, Wide); | |||
31350 | Wide = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32, Wide, | |||
31351 | DAG.getIntPtrConstant(0, dl)); | |||
31352 | Wide = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, Wide); | |||
31353 | Results.push_back(Wide); | |||
31354 | } | |||
31355 | return; | |||
31356 | } | |||
31357 | case ISD::MUL: { | |||
31358 | EVT VT = N->getValueType(0); | |||
31359 | assert(getTypeAction(*DAG.getContext(), VT) == TypeWidenVector &&(static_cast <bool> (getTypeAction(*DAG.getContext(), VT ) == TypeWidenVector && VT.getVectorElementType() == MVT ::i8 && "Unexpected VT!") ? void (0) : __assert_fail ( "getTypeAction(*DAG.getContext(), VT) == TypeWidenVector && VT.getVectorElementType() == MVT::i8 && \"Unexpected VT!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31360, __extension__ __PRETTY_FUNCTION__)) | |||
31360 | VT.getVectorElementType() == MVT::i8 && "Unexpected VT!")(static_cast <bool> (getTypeAction(*DAG.getContext(), VT ) == TypeWidenVector && VT.getVectorElementType() == MVT ::i8 && "Unexpected VT!") ? void (0) : __assert_fail ( "getTypeAction(*DAG.getContext(), VT) == TypeWidenVector && VT.getVectorElementType() == MVT::i8 && \"Unexpected VT!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31360, __extension__ __PRETTY_FUNCTION__)); | |||
31361 | // Pre-promote these to vXi16 to avoid op legalization thinking all 16 | |||
31362 | // elements are needed. | |||
31363 | MVT MulVT = MVT::getVectorVT(MVT::i16, VT.getVectorNumElements()); | |||
31364 | SDValue Op0 = DAG.getNode(ISD::ANY_EXTEND, dl, MulVT, N->getOperand(0)); | |||
31365 | SDValue Op1 = DAG.getNode(ISD::ANY_EXTEND, dl, MulVT, N->getOperand(1)); | |||
31366 | SDValue Res = DAG.getNode(ISD::MUL, dl, MulVT, Op0, Op1); | |||
31367 | Res = DAG.getNode(ISD::TRUNCATE, dl, VT, Res); | |||
31368 | unsigned NumConcats = 16 / VT.getVectorNumElements(); | |||
31369 | SmallVector<SDValue, 8> ConcatOps(NumConcats, DAG.getUNDEF(VT)); | |||
31370 | ConcatOps[0] = Res; | |||
31371 | Res = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v16i8, ConcatOps); | |||
31372 | Results.push_back(Res); | |||
31373 | return; | |||
31374 | } | |||
31375 | case X86ISD::VPMADDWD: | |||
31376 | case X86ISD::AVG: { | |||
31377 | // Legalize types for X86ISD::AVG/VPMADDWD by widening. | |||
31378 | assert(Subtarget.hasSSE2() && "Requires at least SSE2!")(static_cast <bool> (Subtarget.hasSSE2() && "Requires at least SSE2!" ) ? void (0) : __assert_fail ("Subtarget.hasSSE2() && \"Requires at least SSE2!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31378, __extension__ __PRETTY_FUNCTION__)); | |||
31379 | ||||
31380 | EVT VT = N->getValueType(0); | |||
31381 | EVT InVT = N->getOperand(0).getValueType(); | |||
31382 | assert(VT.getSizeInBits() < 128 && 128 % VT.getSizeInBits() == 0 &&(static_cast <bool> (VT.getSizeInBits() < 128 && 128 % VT.getSizeInBits() == 0 && "Expected a VT that divides into 128 bits." ) ? void (0) : __assert_fail ("VT.getSizeInBits() < 128 && 128 % VT.getSizeInBits() == 0 && \"Expected a VT that divides into 128 bits.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31383, __extension__ __PRETTY_FUNCTION__)) | |||
31383 | "Expected a VT that divides into 128 bits.")(static_cast <bool> (VT.getSizeInBits() < 128 && 128 % VT.getSizeInBits() == 0 && "Expected a VT that divides into 128 bits." ) ? void (0) : __assert_fail ("VT.getSizeInBits() < 128 && 128 % VT.getSizeInBits() == 0 && \"Expected a VT that divides into 128 bits.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31383, __extension__ __PRETTY_FUNCTION__)); | |||
31384 | assert(getTypeAction(*DAG.getContext(), VT) == TypeWidenVector &&(static_cast <bool> (getTypeAction(*DAG.getContext(), VT ) == TypeWidenVector && "Unexpected type action!") ? void (0) : __assert_fail ("getTypeAction(*DAG.getContext(), VT) == TypeWidenVector && \"Unexpected type action!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31385, __extension__ __PRETTY_FUNCTION__)) | |||
31385 | "Unexpected type action!")(static_cast <bool> (getTypeAction(*DAG.getContext(), VT ) == TypeWidenVector && "Unexpected type action!") ? void (0) : __assert_fail ("getTypeAction(*DAG.getContext(), VT) == TypeWidenVector && \"Unexpected type action!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31385, __extension__ __PRETTY_FUNCTION__)); | |||
31386 | unsigned NumConcat = 128 / InVT.getSizeInBits(); | |||
31387 | ||||
31388 | EVT InWideVT = EVT::getVectorVT(*DAG.getContext(), | |||
31389 | InVT.getVectorElementType(), | |||
31390 | NumConcat * InVT.getVectorNumElements()); | |||
31391 | EVT WideVT = EVT::getVectorVT(*DAG.getContext(), | |||
31392 | VT.getVectorElementType(), | |||
31393 | NumConcat * VT.getVectorNumElements()); | |||
31394 | ||||
31395 | SmallVector<SDValue, 16> Ops(NumConcat, DAG.getUNDEF(InVT)); | |||
31396 | Ops[0] = N->getOperand(0); | |||
31397 | SDValue InVec0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, InWideVT, Ops); | |||
31398 | Ops[0] = N->getOperand(1); | |||
31399 | SDValue InVec1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, InWideVT, Ops); | |||
31400 | ||||
31401 | SDValue Res = DAG.getNode(N->getOpcode(), dl, WideVT, InVec0, InVec1); | |||
31402 | Results.push_back(Res); | |||
31403 | return; | |||
31404 | } | |||
31405 | // We might have generated v2f32 FMIN/FMAX operations. Widen them to v4f32. | |||
31406 | case X86ISD::FMINC: | |||
31407 | case X86ISD::FMIN: | |||
31408 | case X86ISD::FMAXC: | |||
31409 | case X86ISD::FMAX: { | |||
31410 | EVT VT = N->getValueType(0); | |||
31411 | assert(VT == MVT::v2f32 && "Unexpected type (!= v2f32) on FMIN/FMAX.")(static_cast <bool> (VT == MVT::v2f32 && "Unexpected type (!= v2f32) on FMIN/FMAX." ) ? void (0) : __assert_fail ("VT == MVT::v2f32 && \"Unexpected type (!= v2f32) on FMIN/FMAX.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31411, __extension__ __PRETTY_FUNCTION__)); | |||
31412 | SDValue UNDEF = DAG.getUNDEF(VT); | |||
31413 | SDValue LHS = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v4f32, | |||
31414 | N->getOperand(0), UNDEF); | |||
31415 | SDValue RHS = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v4f32, | |||
31416 | N->getOperand(1), UNDEF); | |||
31417 | Results.push_back(DAG.getNode(N->getOpcode(), dl, MVT::v4f32, LHS, RHS)); | |||
31418 | return; | |||
31419 | } | |||
31420 | case ISD::SDIV: | |||
31421 | case ISD::UDIV: | |||
31422 | case ISD::SREM: | |||
31423 | case ISD::UREM: { | |||
31424 | EVT VT = N->getValueType(0); | |||
31425 | if (VT.isVector()) { | |||
31426 | assert(getTypeAction(*DAG.getContext(), VT) == TypeWidenVector &&(static_cast <bool> (getTypeAction(*DAG.getContext(), VT ) == TypeWidenVector && "Unexpected type action!") ? void (0) : __assert_fail ("getTypeAction(*DAG.getContext(), VT) == TypeWidenVector && \"Unexpected type action!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31427, __extension__ __PRETTY_FUNCTION__)) | |||
31427 | "Unexpected type action!")(static_cast <bool> (getTypeAction(*DAG.getContext(), VT ) == TypeWidenVector && "Unexpected type action!") ? void (0) : __assert_fail ("getTypeAction(*DAG.getContext(), VT) == TypeWidenVector && \"Unexpected type action!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31427, __extension__ __PRETTY_FUNCTION__)); | |||
31428 | // If this RHS is a constant splat vector we can widen this and let | |||
31429 | // division/remainder by constant optimize it. | |||
31430 | // TODO: Can we do something for non-splat? | |||
31431 | APInt SplatVal; | |||
31432 | if (ISD::isConstantSplatVector(N->getOperand(1).getNode(), SplatVal)) { | |||
31433 | unsigned NumConcats = 128 / VT.getSizeInBits(); | |||
31434 | SmallVector<SDValue, 8> Ops0(NumConcats, DAG.getUNDEF(VT)); | |||
31435 | Ops0[0] = N->getOperand(0); | |||
31436 | EVT ResVT = getTypeToTransformTo(*DAG.getContext(), VT); | |||
31437 | SDValue N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, ResVT, Ops0); | |||
31438 | SDValue N1 = DAG.getConstant(SplatVal, dl, ResVT); | |||
31439 | SDValue Res = DAG.getNode(N->getOpcode(), dl, ResVT, N0, N1); | |||
31440 | Results.push_back(Res); | |||
31441 | } | |||
31442 | return; | |||
31443 | } | |||
31444 | ||||
31445 | SDValue V = LowerWin64_i128OP(SDValue(N,0), DAG); | |||
31446 | Results.push_back(V); | |||
31447 | return; | |||
31448 | } | |||
31449 | case ISD::TRUNCATE: { | |||
31450 | MVT VT = N->getSimpleValueType(0); | |||
31451 | if (getTypeAction(*DAG.getContext(), VT) != TypeWidenVector) | |||
31452 | return; | |||
31453 | ||||
31454 | // The generic legalizer will try to widen the input type to the same | |||
31455 | // number of elements as the widened result type. But this isn't always | |||
31456 | // the best thing so do some custom legalization to avoid some cases. | |||
31457 | MVT WidenVT = getTypeToTransformTo(*DAG.getContext(), VT).getSimpleVT(); | |||
31458 | SDValue In = N->getOperand(0); | |||
31459 | EVT InVT = In.getValueType(); | |||
31460 | ||||
31461 | unsigned InBits = InVT.getSizeInBits(); | |||
31462 | if (128 % InBits == 0) { | |||
31463 | // 128 bit and smaller inputs should avoid truncate all together and | |||
31464 | // just use a build_vector that will become a shuffle. | |||
31465 | // TODO: Widen and use a shuffle directly? | |||
31466 | MVT InEltVT = InVT.getSimpleVT().getVectorElementType(); | |||
31467 | EVT EltVT = VT.getVectorElementType(); | |||
31468 | unsigned WidenNumElts = WidenVT.getVectorNumElements(); | |||
31469 | SmallVector<SDValue, 16> Ops(WidenNumElts, DAG.getUNDEF(EltVT)); | |||
31470 | // Use the original element count so we don't do more scalar opts than | |||
31471 | // necessary. | |||
31472 | unsigned MinElts = VT.getVectorNumElements(); | |||
31473 | for (unsigned i=0; i < MinElts; ++i) { | |||
31474 | SDValue Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, InEltVT, In, | |||
31475 | DAG.getIntPtrConstant(i, dl)); | |||
31476 | Ops[i] = DAG.getNode(ISD::TRUNCATE, dl, EltVT, Val); | |||
31477 | } | |||
31478 | Results.push_back(DAG.getBuildVector(WidenVT, dl, Ops)); | |||
31479 | return; | |||
31480 | } | |||
31481 | // With AVX512 there are some cases that can use a target specific | |||
31482 | // truncate node to go from 256/512 to less than 128 with zeros in the | |||
31483 | // upper elements of the 128 bit result. | |||
31484 | if (Subtarget.hasAVX512() && isTypeLegal(InVT)) { | |||
31485 | // We can use VTRUNC directly if for 256 bits with VLX or for any 512. | |||
31486 | if ((InBits == 256 && Subtarget.hasVLX()) || InBits == 512) { | |||
31487 | Results.push_back(DAG.getNode(X86ISD::VTRUNC, dl, WidenVT, In)); | |||
31488 | return; | |||
31489 | } | |||
31490 | // There's one case we can widen to 512 bits and use VTRUNC. | |||
31491 | if (InVT == MVT::v4i64 && VT == MVT::v4i8 && isTypeLegal(MVT::v8i64)) { | |||
31492 | In = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i64, In, | |||
31493 | DAG.getUNDEF(MVT::v4i64)); | |||
31494 | Results.push_back(DAG.getNode(X86ISD::VTRUNC, dl, WidenVT, In)); | |||
31495 | return; | |||
31496 | } | |||
31497 | } | |||
31498 | if (Subtarget.hasVLX() && InVT == MVT::v8i64 && VT == MVT::v8i8 && | |||
31499 | getTypeAction(*DAG.getContext(), InVT) == TypeSplitVector && | |||
31500 | isTypeLegal(MVT::v4i64)) { | |||
31501 | // Input needs to be split and output needs to widened. Let's use two | |||
31502 | // VTRUNCs, and shuffle their results together into the wider type. | |||
31503 | SDValue Lo, Hi; | |||
31504 | std::tie(Lo, Hi) = DAG.SplitVector(In, dl); | |||
31505 | ||||
31506 | Lo = DAG.getNode(X86ISD::VTRUNC, dl, MVT::v16i8, Lo); | |||
31507 | Hi = DAG.getNode(X86ISD::VTRUNC, dl, MVT::v16i8, Hi); | |||
31508 | SDValue Res = DAG.getVectorShuffle(MVT::v16i8, dl, Lo, Hi, | |||
31509 | { 0, 1, 2, 3, 16, 17, 18, 19, | |||
31510 | -1, -1, -1, -1, -1, -1, -1, -1 }); | |||
31511 | Results.push_back(Res); | |||
31512 | return; | |||
31513 | } | |||
31514 | ||||
31515 | return; | |||
31516 | } | |||
31517 | case ISD::ANY_EXTEND: | |||
31518 | // Right now, only MVT::v8i8 has Custom action for an illegal type. | |||
31519 | // It's intended to custom handle the input type. | |||
31520 | assert(N->getValueType(0) == MVT::v8i8 &&(static_cast <bool> (N->getValueType(0) == MVT::v8i8 && "Do not know how to legalize this Node") ? void ( 0) : __assert_fail ("N->getValueType(0) == MVT::v8i8 && \"Do not know how to legalize this Node\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31521, __extension__ __PRETTY_FUNCTION__)) | |||
31521 | "Do not know how to legalize this Node")(static_cast <bool> (N->getValueType(0) == MVT::v8i8 && "Do not know how to legalize this Node") ? void ( 0) : __assert_fail ("N->getValueType(0) == MVT::v8i8 && \"Do not know how to legalize this Node\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31521, __extension__ __PRETTY_FUNCTION__)); | |||
31522 | return; | |||
31523 | case ISD::SIGN_EXTEND: | |||
31524 | case ISD::ZERO_EXTEND: { | |||
31525 | EVT VT = N->getValueType(0); | |||
31526 | SDValue In = N->getOperand(0); | |||
31527 | EVT InVT = In.getValueType(); | |||
31528 | if (!Subtarget.hasSSE41() && VT == MVT::v4i64 && | |||
31529 | (InVT == MVT::v4i16 || InVT == MVT::v4i8)){ | |||
31530 | assert(getTypeAction(*DAG.getContext(), InVT) == TypeWidenVector &&(static_cast <bool> (getTypeAction(*DAG.getContext(), InVT ) == TypeWidenVector && "Unexpected type action!") ? void (0) : __assert_fail ("getTypeAction(*DAG.getContext(), InVT) == TypeWidenVector && \"Unexpected type action!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31531, __extension__ __PRETTY_FUNCTION__)) | |||
31531 | "Unexpected type action!")(static_cast <bool> (getTypeAction(*DAG.getContext(), InVT ) == TypeWidenVector && "Unexpected type action!") ? void (0) : __assert_fail ("getTypeAction(*DAG.getContext(), InVT) == TypeWidenVector && \"Unexpected type action!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31531, __extension__ __PRETTY_FUNCTION__)); | |||
31532 | assert(N->getOpcode() == ISD::SIGN_EXTEND && "Unexpected opcode")(static_cast <bool> (N->getOpcode() == ISD::SIGN_EXTEND && "Unexpected opcode") ? void (0) : __assert_fail ( "N->getOpcode() == ISD::SIGN_EXTEND && \"Unexpected opcode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31532, __extension__ __PRETTY_FUNCTION__)); | |||
31533 | // Custom split this so we can extend i8/i16->i32 invec. This is better | |||
31534 | // since sign_extend_inreg i8/i16->i64 requires an extend to i32 using | |||
31535 | // sra. Then extending from i32 to i64 using pcmpgt. By custom splitting | |||
31536 | // we allow the sra from the extend to i32 to be shared by the split. | |||
31537 | In = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, In); | |||
31538 | ||||
31539 | // Fill a vector with sign bits for each element. | |||
31540 | SDValue Zero = DAG.getConstant(0, dl, MVT::v4i32); | |||
31541 | SDValue SignBits = DAG.getSetCC(dl, MVT::v4i32, Zero, In, ISD::SETGT); | |||
31542 | ||||
31543 | // Create an unpackl and unpackh to interleave the sign bits then bitcast | |||
31544 | // to v2i64. | |||
31545 | SDValue Lo = DAG.getVectorShuffle(MVT::v4i32, dl, In, SignBits, | |||
31546 | {0, 4, 1, 5}); | |||
31547 | Lo = DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Lo); | |||
31548 | SDValue Hi = DAG.getVectorShuffle(MVT::v4i32, dl, In, SignBits, | |||
31549 | {2, 6, 3, 7}); | |||
31550 | Hi = DAG.getNode(ISD::BITCAST, dl, MVT::v2i64, Hi); | |||
31551 | ||||
31552 | SDValue Res = DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Lo, Hi); | |||
31553 | Results.push_back(Res); | |||
31554 | return; | |||
31555 | } | |||
31556 | ||||
31557 | if (VT == MVT::v16i32 || VT == MVT::v8i64) { | |||
31558 | if (!InVT.is128BitVector()) { | |||
31559 | // Not a 128 bit vector, but maybe type legalization will promote | |||
31560 | // it to 128 bits. | |||
31561 | if (getTypeAction(*DAG.getContext(), InVT) != TypePromoteInteger) | |||
31562 | return; | |||
31563 | InVT = getTypeToTransformTo(*DAG.getContext(), InVT); | |||
31564 | if (!InVT.is128BitVector()) | |||
31565 | return; | |||
31566 | ||||
31567 | // Promote the input to 128 bits. Type legalization will turn this into | |||
31568 | // zext_inreg/sext_inreg. | |||
31569 | In = DAG.getNode(N->getOpcode(), dl, InVT, In); | |||
31570 | } | |||
31571 | ||||
31572 | // Perform custom splitting instead of the two stage extend we would get | |||
31573 | // by default. | |||
31574 | EVT LoVT, HiVT; | |||
31575 | std::tie(LoVT, HiVT) = DAG.GetSplitDestVTs(N->getValueType(0)); | |||
31576 | assert(isTypeLegal(LoVT) && "Split VT not legal?")(static_cast <bool> (isTypeLegal(LoVT) && "Split VT not legal?" ) ? void (0) : __assert_fail ("isTypeLegal(LoVT) && \"Split VT not legal?\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31576, __extension__ __PRETTY_FUNCTION__)); | |||
31577 | ||||
31578 | SDValue Lo = getEXTEND_VECTOR_INREG(N->getOpcode(), dl, LoVT, In, DAG); | |||
31579 | ||||
31580 | // We need to shift the input over by half the number of elements. | |||
31581 | unsigned NumElts = InVT.getVectorNumElements(); | |||
31582 | unsigned HalfNumElts = NumElts / 2; | |||
31583 | SmallVector<int, 16> ShufMask(NumElts, SM_SentinelUndef); | |||
31584 | for (unsigned i = 0; i != HalfNumElts; ++i) | |||
31585 | ShufMask[i] = i + HalfNumElts; | |||
31586 | ||||
31587 | SDValue Hi = DAG.getVectorShuffle(InVT, dl, In, In, ShufMask); | |||
31588 | Hi = getEXTEND_VECTOR_INREG(N->getOpcode(), dl, HiVT, Hi, DAG); | |||
31589 | ||||
31590 | SDValue Res = DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Lo, Hi); | |||
31591 | Results.push_back(Res); | |||
31592 | } | |||
31593 | return; | |||
31594 | } | |||
31595 | case ISD::FP_TO_SINT: | |||
31596 | case ISD::STRICT_FP_TO_SINT: | |||
31597 | case ISD::FP_TO_UINT: | |||
31598 | case ISD::STRICT_FP_TO_UINT: { | |||
31599 | bool IsStrict = N->isStrictFPOpcode(); | |||
31600 | bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT || | |||
31601 | N->getOpcode() == ISD::STRICT_FP_TO_SINT; | |||
31602 | EVT VT = N->getValueType(0); | |||
31603 | SDValue Src = N->getOperand(IsStrict ? 1 : 0); | |||
31604 | EVT SrcVT = Src.getValueType(); | |||
31605 | ||||
31606 | if (VT.isVector() && Subtarget.hasFP16() && | |||
31607 | SrcVT.getVectorElementType() == MVT::f16) { | |||
31608 | EVT EleVT = VT.getVectorElementType(); | |||
31609 | EVT ResVT = EleVT == MVT::i32 ? MVT::v4i32 : MVT::v8i16; | |||
31610 | ||||
31611 | if (SrcVT != MVT::v8f16) { | |||
31612 | SDValue Tmp = | |||
31613 | IsStrict ? DAG.getConstantFP(0.0, dl, SrcVT) : DAG.getUNDEF(SrcVT); | |||
31614 | SmallVector<SDValue, 4> Ops(SrcVT == MVT::v2f16 ? 4 : 2, Tmp); | |||
31615 | Ops[0] = Src; | |||
31616 | Src = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8f16, Ops); | |||
31617 | } | |||
31618 | ||||
31619 | SDValue Res, Chain; | |||
31620 | if (IsStrict) { | |||
31621 | unsigned Opc = | |||
31622 | IsSigned ? X86ISD::STRICT_CVTTP2SI : X86ISD::STRICT_CVTTP2UI; | |||
31623 | Res = | |||
31624 | DAG.getNode(Opc, dl, {ResVT, MVT::Other}, {N->getOperand(0), Src}); | |||
31625 | Chain = Res.getValue(1); | |||
31626 | } else { | |||
31627 | unsigned Opc = IsSigned ? X86ISD::CVTTP2SI : X86ISD::CVTTP2UI; | |||
31628 | Res = DAG.getNode(Opc, dl, ResVT, Src); | |||
31629 | } | |||
31630 | ||||
31631 | // TODO: Need to add exception check code for strict FP. | |||
31632 | if (EleVT.getSizeInBits() < 16) { | |||
31633 | MVT TmpVT = MVT::getVectorVT(EleVT.getSimpleVT(), 8); | |||
31634 | Res = DAG.getNode(ISD::TRUNCATE, dl, TmpVT, Res); | |||
31635 | ||||
31636 | // Now widen to 128 bits. | |||
31637 | unsigned NumConcats = 128 / TmpVT.getSizeInBits(); | |||
31638 | MVT ConcatVT = MVT::getVectorVT(EleVT.getSimpleVT(), 8 * NumConcats); | |||
31639 | SmallVector<SDValue, 8> ConcatOps(NumConcats, DAG.getUNDEF(TmpVT)); | |||
31640 | ConcatOps[0] = Res; | |||
31641 | Res = DAG.getNode(ISD::CONCAT_VECTORS, dl, ConcatVT, ConcatOps); | |||
31642 | } | |||
31643 | ||||
31644 | Results.push_back(Res); | |||
31645 | if (IsStrict) | |||
31646 | Results.push_back(Chain); | |||
31647 | ||||
31648 | return; | |||
31649 | } | |||
31650 | ||||
31651 | if (VT.isVector() && VT.getScalarSizeInBits() < 32) { | |||
31652 | assert(getTypeAction(*DAG.getContext(), VT) == TypeWidenVector &&(static_cast <bool> (getTypeAction(*DAG.getContext(), VT ) == TypeWidenVector && "Unexpected type action!") ? void (0) : __assert_fail ("getTypeAction(*DAG.getContext(), VT) == TypeWidenVector && \"Unexpected type action!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31653, __extension__ __PRETTY_FUNCTION__)) | |||
31653 | "Unexpected type action!")(static_cast <bool> (getTypeAction(*DAG.getContext(), VT ) == TypeWidenVector && "Unexpected type action!") ? void (0) : __assert_fail ("getTypeAction(*DAG.getContext(), VT) == TypeWidenVector && \"Unexpected type action!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31653, __extension__ __PRETTY_FUNCTION__)); | |||
31654 | ||||
31655 | // Try to create a 128 bit vector, but don't exceed a 32 bit element. | |||
31656 | unsigned NewEltWidth = std::min(128 / VT.getVectorNumElements(), 32U); | |||
31657 | MVT PromoteVT = MVT::getVectorVT(MVT::getIntegerVT(NewEltWidth), | |||
31658 | VT.getVectorNumElements()); | |||
31659 | SDValue Res; | |||
31660 | SDValue Chain; | |||
31661 | if (IsStrict) { | |||
31662 | Res = DAG.getNode(ISD::STRICT_FP_TO_SINT, dl, {PromoteVT, MVT::Other}, | |||
31663 | {N->getOperand(0), Src}); | |||
31664 | Chain = Res.getValue(1); | |||
31665 | } else | |||
31666 | Res = DAG.getNode(ISD::FP_TO_SINT, dl, PromoteVT, Src); | |||
31667 | ||||
31668 | // Preserve what we know about the size of the original result. If the | |||
31669 | // result is v2i32, we have to manually widen the assert. | |||
31670 | if (PromoteVT == MVT::v2i32) | |||
31671 | Res = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v4i32, Res, | |||
31672 | DAG.getUNDEF(MVT::v2i32)); | |||
31673 | ||||
31674 | Res = DAG.getNode(!IsSigned ? ISD::AssertZext : ISD::AssertSext, dl, | |||
31675 | Res.getValueType(), Res, | |||
31676 | DAG.getValueType(VT.getVectorElementType())); | |||
31677 | ||||
31678 | if (PromoteVT == MVT::v2i32) | |||
31679 | Res = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2i32, Res, | |||
31680 | DAG.getIntPtrConstant(0, dl)); | |||
31681 | ||||
31682 | // Truncate back to the original width. | |||
31683 | Res = DAG.getNode(ISD::TRUNCATE, dl, VT, Res); | |||
31684 | ||||
31685 | // Now widen to 128 bits. | |||
31686 | unsigned NumConcats = 128 / VT.getSizeInBits(); | |||
31687 | MVT ConcatVT = MVT::getVectorVT(VT.getSimpleVT().getVectorElementType(), | |||
31688 | VT.getVectorNumElements() * NumConcats); | |||
31689 | SmallVector<SDValue, 8> ConcatOps(NumConcats, DAG.getUNDEF(VT)); | |||
31690 | ConcatOps[0] = Res; | |||
31691 | Res = DAG.getNode(ISD::CONCAT_VECTORS, dl, ConcatVT, ConcatOps); | |||
31692 | Results.push_back(Res); | |||
31693 | if (IsStrict) | |||
31694 | Results.push_back(Chain); | |||
31695 | return; | |||
31696 | } | |||
31697 | ||||
31698 | ||||
31699 | if (VT == MVT::v2i32) { | |||
31700 | assert((!IsStrict || IsSigned || Subtarget.hasAVX512()) &&(static_cast <bool> ((!IsStrict || IsSigned || Subtarget .hasAVX512()) && "Strict unsigned conversion requires AVX512" ) ? void (0) : __assert_fail ("(!IsStrict || IsSigned || Subtarget.hasAVX512()) && \"Strict unsigned conversion requires AVX512\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31701, __extension__ __PRETTY_FUNCTION__)) | |||
31701 | "Strict unsigned conversion requires AVX512")(static_cast <bool> ((!IsStrict || IsSigned || Subtarget .hasAVX512()) && "Strict unsigned conversion requires AVX512" ) ? void (0) : __assert_fail ("(!IsStrict || IsSigned || Subtarget.hasAVX512()) && \"Strict unsigned conversion requires AVX512\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31701, __extension__ __PRETTY_FUNCTION__)); | |||
31702 | assert(Subtarget.hasSSE2() && "Requires at least SSE2!")(static_cast <bool> (Subtarget.hasSSE2() && "Requires at least SSE2!" ) ? void (0) : __assert_fail ("Subtarget.hasSSE2() && \"Requires at least SSE2!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31702, __extension__ __PRETTY_FUNCTION__)); | |||
31703 | assert(getTypeAction(*DAG.getContext(), VT) == TypeWidenVector &&(static_cast <bool> (getTypeAction(*DAG.getContext(), VT ) == TypeWidenVector && "Unexpected type action!") ? void (0) : __assert_fail ("getTypeAction(*DAG.getContext(), VT) == TypeWidenVector && \"Unexpected type action!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31704, __extension__ __PRETTY_FUNCTION__)) | |||
31704 | "Unexpected type action!")(static_cast <bool> (getTypeAction(*DAG.getContext(), VT ) == TypeWidenVector && "Unexpected type action!") ? void (0) : __assert_fail ("getTypeAction(*DAG.getContext(), VT) == TypeWidenVector && \"Unexpected type action!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31704, __extension__ __PRETTY_FUNCTION__)); | |||
31705 | if (Src.getValueType() == MVT::v2f64) { | |||
31706 | if (!IsSigned && !Subtarget.hasAVX512()) { | |||
31707 | SDValue Res = | |||
31708 | expandFP_TO_UINT_SSE(MVT::v4i32, Src, dl, DAG, Subtarget); | |||
31709 | Results.push_back(Res); | |||
31710 | return; | |||
31711 | } | |||
31712 | ||||
31713 | unsigned Opc; | |||
31714 | if (IsStrict) | |||
31715 | Opc = IsSigned ? X86ISD::STRICT_CVTTP2SI : X86ISD::STRICT_CVTTP2UI; | |||
31716 | else | |||
31717 | Opc = IsSigned ? X86ISD::CVTTP2SI : X86ISD::CVTTP2UI; | |||
31718 | ||||
31719 | // If we have VLX we can emit a target specific FP_TO_UINT node,. | |||
31720 | if (!IsSigned && !Subtarget.hasVLX()) { | |||
31721 | // Otherwise we can defer to the generic legalizer which will widen | |||
31722 | // the input as well. This will be further widened during op | |||
31723 | // legalization to v8i32<-v8f64. | |||
31724 | // For strict nodes we'll need to widen ourselves. | |||
31725 | // FIXME: Fix the type legalizer to safely widen strict nodes? | |||
31726 | if (!IsStrict) | |||
31727 | return; | |||
31728 | Src = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v4f64, Src, | |||
31729 | DAG.getConstantFP(0.0, dl, MVT::v2f64)); | |||
31730 | Opc = N->getOpcode(); | |||
31731 | } | |||
31732 | SDValue Res; | |||
31733 | SDValue Chain; | |||
31734 | if (IsStrict) { | |||
31735 | Res = DAG.getNode(Opc, dl, {MVT::v4i32, MVT::Other}, | |||
31736 | {N->getOperand(0), Src}); | |||
31737 | Chain = Res.getValue(1); | |||
31738 | } else { | |||
31739 | Res = DAG.getNode(Opc, dl, MVT::v4i32, Src); | |||
31740 | } | |||
31741 | Results.push_back(Res); | |||
31742 | if (IsStrict) | |||
31743 | Results.push_back(Chain); | |||
31744 | return; | |||
31745 | } | |||
31746 | ||||
31747 | // Custom widen strict v2f32->v2i32 by padding with zeros. | |||
31748 | // FIXME: Should generic type legalizer do this? | |||
31749 | if (Src.getValueType() == MVT::v2f32 && IsStrict) { | |||
31750 | Src = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v4f32, Src, | |||
31751 | DAG.getConstantFP(0.0, dl, MVT::v2f32)); | |||
31752 | SDValue Res = DAG.getNode(N->getOpcode(), dl, {MVT::v4i32, MVT::Other}, | |||
31753 | {N->getOperand(0), Src}); | |||
31754 | Results.push_back(Res); | |||
31755 | Results.push_back(Res.getValue(1)); | |||
31756 | return; | |||
31757 | } | |||
31758 | ||||
31759 | // The FP_TO_INTHelper below only handles f32/f64/f80 scalar inputs, | |||
31760 | // so early out here. | |||
31761 | return; | |||
31762 | } | |||
31763 | ||||
31764 | assert(!VT.isVector() && "Vectors should have been handled above!")(static_cast <bool> (!VT.isVector() && "Vectors should have been handled above!" ) ? void (0) : __assert_fail ("!VT.isVector() && \"Vectors should have been handled above!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31764, __extension__ __PRETTY_FUNCTION__)); | |||
31765 | ||||
31766 | if ((Subtarget.hasDQI() && VT == MVT::i64 && | |||
31767 | (SrcVT == MVT::f32 || SrcVT == MVT::f64)) || | |||
31768 | (Subtarget.hasFP16() && SrcVT == MVT::f16)) { | |||
31769 | assert(!Subtarget.is64Bit() && "i64 should be legal")(static_cast <bool> (!Subtarget.is64Bit() && "i64 should be legal" ) ? void (0) : __assert_fail ("!Subtarget.is64Bit() && \"i64 should be legal\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31769, __extension__ __PRETTY_FUNCTION__)); | |||
31770 | unsigned NumElts = Subtarget.hasVLX() ? 2 : 8; | |||
31771 | // If we use a 128-bit result we might need to use a target specific node. | |||
31772 | unsigned SrcElts = | |||
31773 | std::max(NumElts, 128U / (unsigned)SrcVT.getSizeInBits()); | |||
31774 | MVT VecVT = MVT::getVectorVT(MVT::i64, NumElts); | |||
31775 | MVT VecInVT = MVT::getVectorVT(SrcVT.getSimpleVT(), SrcElts); | |||
31776 | unsigned Opc = N->getOpcode(); | |||
31777 | if (NumElts != SrcElts) { | |||
31778 | if (IsStrict) | |||
31779 | Opc = IsSigned ? X86ISD::STRICT_CVTTP2SI : X86ISD::STRICT_CVTTP2UI; | |||
31780 | else | |||
31781 | Opc = IsSigned ? X86ISD::CVTTP2SI : X86ISD::CVTTP2UI; | |||
31782 | } | |||
31783 | ||||
31784 | SDValue ZeroIdx = DAG.getIntPtrConstant(0, dl); | |||
31785 | SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VecInVT, | |||
31786 | DAG.getConstantFP(0.0, dl, VecInVT), Src, | |||
31787 | ZeroIdx); | |||
31788 | SDValue Chain; | |||
31789 | if (IsStrict) { | |||
31790 | SDVTList Tys = DAG.getVTList(VecVT, MVT::Other); | |||
31791 | Res = DAG.getNode(Opc, SDLoc(N), Tys, N->getOperand(0), Res); | |||
31792 | Chain = Res.getValue(1); | |||
31793 | } else | |||
31794 | Res = DAG.getNode(Opc, SDLoc(N), VecVT, Res); | |||
31795 | Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Res, ZeroIdx); | |||
31796 | Results.push_back(Res); | |||
31797 | if (IsStrict) | |||
31798 | Results.push_back(Chain); | |||
31799 | return; | |||
31800 | } | |||
31801 | ||||
31802 | if (VT == MVT::i128 && Subtarget.isTargetWin64()) { | |||
31803 | SDValue Chain; | |||
31804 | SDValue V = LowerWin64_FP_TO_INT128(SDValue(N, 0), DAG, Chain); | |||
31805 | Results.push_back(V); | |||
31806 | if (IsStrict) | |||
31807 | Results.push_back(Chain); | |||
31808 | return; | |||
31809 | } | |||
31810 | ||||
31811 | SDValue Chain; | |||
31812 | if (SDValue V = FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, Chain)) { | |||
31813 | Results.push_back(V); | |||
31814 | if (IsStrict) | |||
31815 | Results.push_back(Chain); | |||
31816 | } | |||
31817 | return; | |||
31818 | } | |||
31819 | case ISD::LRINT: | |||
31820 | case ISD::LLRINT: { | |||
31821 | if (SDValue V = LRINT_LLRINTHelper(N, DAG)) | |||
31822 | Results.push_back(V); | |||
31823 | return; | |||
31824 | } | |||
31825 | ||||
31826 | case ISD::SINT_TO_FP: | |||
31827 | case ISD::STRICT_SINT_TO_FP: | |||
31828 | case ISD::UINT_TO_FP: | |||
31829 | case ISD::STRICT_UINT_TO_FP: { | |||
31830 | bool IsStrict = N->isStrictFPOpcode(); | |||
31831 | bool IsSigned = N->getOpcode() == ISD::SINT_TO_FP || | |||
31832 | N->getOpcode() == ISD::STRICT_SINT_TO_FP; | |||
31833 | EVT VT = N->getValueType(0); | |||
31834 | SDValue Src = N->getOperand(IsStrict ? 1 : 0); | |||
31835 | if (VT.getVectorElementType() == MVT::f16 && Subtarget.hasFP16() && | |||
31836 | Subtarget.hasVLX()) { | |||
31837 | if (Src.getValueType().getVectorElementType() == MVT::i16) | |||
31838 | return; | |||
31839 | ||||
31840 | if (VT == MVT::v2f16 && Src.getValueType() == MVT::v2i32) | |||
31841 | Src = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v4i32, Src, | |||
31842 | IsStrict ? DAG.getConstant(0, dl, MVT::v2i32) | |||
31843 | : DAG.getUNDEF(MVT::v2i32)); | |||
31844 | if (IsStrict) { | |||
31845 | unsigned Opc = | |||
31846 | IsSigned ? X86ISD::STRICT_CVTSI2P : X86ISD::STRICT_CVTUI2P; | |||
31847 | SDValue Res = DAG.getNode(Opc, dl, {MVT::v8f16, MVT::Other}, | |||
31848 | {N->getOperand(0), Src}); | |||
31849 | Results.push_back(Res); | |||
31850 | Results.push_back(Res.getValue(1)); | |||
31851 | } else { | |||
31852 | unsigned Opc = IsSigned ? X86ISD::CVTSI2P : X86ISD::CVTUI2P; | |||
31853 | Results.push_back(DAG.getNode(Opc, dl, MVT::v8f16, Src)); | |||
31854 | } | |||
31855 | return; | |||
31856 | } | |||
31857 | if (VT != MVT::v2f32) | |||
31858 | return; | |||
31859 | EVT SrcVT = Src.getValueType(); | |||
31860 | if (Subtarget.hasDQI() && Subtarget.hasVLX() && SrcVT == MVT::v2i64) { | |||
31861 | if (IsStrict) { | |||
31862 | unsigned Opc = IsSigned ? X86ISD::STRICT_CVTSI2P | |||
31863 | : X86ISD::STRICT_CVTUI2P; | |||
31864 | SDValue Res = DAG.getNode(Opc, dl, {MVT::v4f32, MVT::Other}, | |||
31865 | {N->getOperand(0), Src}); | |||
31866 | Results.push_back(Res); | |||
31867 | Results.push_back(Res.getValue(1)); | |||
31868 | } else { | |||
31869 | unsigned Opc = IsSigned ? X86ISD::CVTSI2P : X86ISD::CVTUI2P; | |||
31870 | Results.push_back(DAG.getNode(Opc, dl, MVT::v4f32, Src)); | |||
31871 | } | |||
31872 | return; | |||
31873 | } | |||
31874 | if (SrcVT == MVT::v2i64 && !IsSigned && Subtarget.is64Bit() && | |||
31875 | Subtarget.hasSSE41() && !Subtarget.hasAVX512()) { | |||
31876 | SDValue Zero = DAG.getConstant(0, dl, SrcVT); | |||
31877 | SDValue One = DAG.getConstant(1, dl, SrcVT); | |||
31878 | SDValue Sign = DAG.getNode(ISD::OR, dl, SrcVT, | |||
31879 | DAG.getNode(ISD::SRL, dl, SrcVT, Src, One), | |||
31880 | DAG.getNode(ISD::AND, dl, SrcVT, Src, One)); | |||
31881 | SDValue IsNeg = DAG.getSetCC(dl, MVT::v2i64, Src, Zero, ISD::SETLT); | |||
31882 | SDValue SignSrc = DAG.getSelect(dl, SrcVT, IsNeg, Sign, Src); | |||
31883 | SmallVector<SDValue, 4> SignCvts(4, DAG.getConstantFP(0.0, dl, MVT::f32)); | |||
31884 | for (int i = 0; i != 2; ++i) { | |||
31885 | SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64, | |||
31886 | SignSrc, DAG.getIntPtrConstant(i, dl)); | |||
31887 | if (IsStrict) | |||
31888 | SignCvts[i] = | |||
31889 | DAG.getNode(ISD::STRICT_SINT_TO_FP, dl, {MVT::f32, MVT::Other}, | |||
31890 | {N->getOperand(0), Elt}); | |||
31891 | else | |||
31892 | SignCvts[i] = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, Elt); | |||
31893 | }; | |||
31894 | SDValue SignCvt = DAG.getBuildVector(MVT::v4f32, dl, SignCvts); | |||
31895 | SDValue Slow, Chain; | |||
31896 | if (IsStrict) { | |||
31897 | Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, | |||
31898 | SignCvts[0].getValue(1), SignCvts[1].getValue(1)); | |||
31899 | Slow = DAG.getNode(ISD::STRICT_FADD, dl, {MVT::v4f32, MVT::Other}, | |||
31900 | {Chain, SignCvt, SignCvt}); | |||
31901 | Chain = Slow.getValue(1); | |||
31902 | } else { | |||
31903 | Slow = DAG.getNode(ISD::FADD, dl, MVT::v4f32, SignCvt, SignCvt); | |||
31904 | } | |||
31905 | IsNeg = DAG.getBitcast(MVT::v4i32, IsNeg); | |||
31906 | IsNeg = | |||
31907 | DAG.getVectorShuffle(MVT::v4i32, dl, IsNeg, IsNeg, {1, 3, -1, -1}); | |||
31908 | SDValue Cvt = DAG.getSelect(dl, MVT::v4f32, IsNeg, Slow, SignCvt); | |||
31909 | Results.push_back(Cvt); | |||
31910 | if (IsStrict) | |||
31911 | Results.push_back(Chain); | |||
31912 | return; | |||
31913 | } | |||
31914 | ||||
31915 | if (SrcVT != MVT::v2i32) | |||
31916 | return; | |||
31917 | ||||
31918 | if (IsSigned || Subtarget.hasAVX512()) { | |||
31919 | if (!IsStrict) | |||
31920 | return; | |||
31921 | ||||
31922 | // Custom widen strict v2i32->v2f32 to avoid scalarization. | |||
31923 | // FIXME: Should generic type legalizer do this? | |||
31924 | Src = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v4i32, Src, | |||
31925 | DAG.getConstant(0, dl, MVT::v2i32)); | |||
31926 | SDValue Res = DAG.getNode(N->getOpcode(), dl, {MVT::v4f32, MVT::Other}, | |||
31927 | {N->getOperand(0), Src}); | |||
31928 | Results.push_back(Res); | |||
31929 | Results.push_back(Res.getValue(1)); | |||
31930 | return; | |||
31931 | } | |||
31932 | ||||
31933 | assert(Subtarget.hasSSE2() && "Requires at least SSE2!")(static_cast <bool> (Subtarget.hasSSE2() && "Requires at least SSE2!" ) ? void (0) : __assert_fail ("Subtarget.hasSSE2() && \"Requires at least SSE2!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31933, __extension__ __PRETTY_FUNCTION__)); | |||
31934 | SDValue ZExtIn = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v2i64, Src); | |||
31935 | SDValue VBias = | |||
31936 | DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL), dl, MVT::v2f64); | |||
31937 | SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64, ZExtIn, | |||
31938 | DAG.getBitcast(MVT::v2i64, VBias)); | |||
31939 | Or = DAG.getBitcast(MVT::v2f64, Or); | |||
31940 | if (IsStrict) { | |||
31941 | SDValue Sub = DAG.getNode(ISD::STRICT_FSUB, dl, {MVT::v2f64, MVT::Other}, | |||
31942 | {N->getOperand(0), Or, VBias}); | |||
31943 | SDValue Res = DAG.getNode(X86ISD::STRICT_VFPROUND, dl, | |||
31944 | {MVT::v4f32, MVT::Other}, | |||
31945 | {Sub.getValue(1), Sub}); | |||
31946 | Results.push_back(Res); | |||
31947 | Results.push_back(Res.getValue(1)); | |||
31948 | } else { | |||
31949 | // TODO: Are there any fast-math-flags to propagate here? | |||
31950 | SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, Or, VBias); | |||
31951 | Results.push_back(DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, Sub)); | |||
31952 | } | |||
31953 | return; | |||
31954 | } | |||
31955 | case ISD::STRICT_FP_ROUND: | |||
31956 | case ISD::FP_ROUND: { | |||
31957 | bool IsStrict = N->isStrictFPOpcode(); | |||
31958 | SDValue Src = N->getOperand(IsStrict ? 1 : 0); | |||
31959 | EVT VT = N->getValueType(0); | |||
31960 | EVT NewVT = VT.getVectorElementType() == MVT::f16 ? MVT::v8f16 : MVT::v4f32; | |||
31961 | if (VT == MVT::v2f16 && Src.getValueType() == MVT::v2f32) { | |||
31962 | SDValue Ext = IsStrict ? DAG.getConstantFP(0.0, dl, MVT::v2f32) | |||
31963 | : DAG.getUNDEF(MVT::v2f32); | |||
31964 | Src = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v4f32, Src, Ext); | |||
31965 | } | |||
31966 | if (!isTypeLegal(Src.getValueType())) | |||
31967 | return; | |||
31968 | SDValue V; | |||
31969 | if (IsStrict) | |||
31970 | V = DAG.getNode(X86ISD::STRICT_VFPROUND, dl, {NewVT, MVT::Other}, | |||
31971 | {N->getOperand(0), Src}); | |||
31972 | else | |||
31973 | V = DAG.getNode(X86ISD::VFPROUND, dl, NewVT, Src); | |||
31974 | Results.push_back(V); | |||
31975 | if (IsStrict) | |||
31976 | Results.push_back(V.getValue(1)); | |||
31977 | return; | |||
31978 | } | |||
31979 | case ISD::FP_EXTEND: | |||
31980 | case ISD::STRICT_FP_EXTEND: { | |||
31981 | // Right now, only MVT::v2f32 has OperationAction for FP_EXTEND. | |||
31982 | // No other ValueType for FP_EXTEND should reach this point. | |||
31983 | assert(N->getValueType(0) == MVT::v2f32 &&(static_cast <bool> (N->getValueType(0) == MVT::v2f32 && "Do not know how to legalize this Node") ? void ( 0) : __assert_fail ("N->getValueType(0) == MVT::v2f32 && \"Do not know how to legalize this Node\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31984, __extension__ __PRETTY_FUNCTION__)) | |||
31984 | "Do not know how to legalize this Node")(static_cast <bool> (N->getValueType(0) == MVT::v2f32 && "Do not know how to legalize this Node") ? void ( 0) : __assert_fail ("N->getValueType(0) == MVT::v2f32 && \"Do not know how to legalize this Node\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 31984, __extension__ __PRETTY_FUNCTION__)); | |||
31985 | if (!Subtarget.hasFP16() || !Subtarget.hasVLX()) | |||
31986 | return; | |||
31987 | bool IsStrict = N->isStrictFPOpcode(); | |||
31988 | SDValue Src = N->getOperand(IsStrict ? 1 : 0); | |||
31989 | SDValue Ext = IsStrict ? DAG.getConstantFP(0.0, dl, MVT::v2f16) | |||
31990 | : DAG.getUNDEF(MVT::v2f16); | |||
31991 | SDValue V = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v4f16, Src, Ext); | |||
31992 | if (IsStrict) | |||
31993 | V = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {MVT::v4f32, MVT::Other}, | |||
31994 | {N->getOperand(0), V}); | |||
31995 | else | |||
31996 | V = DAG.getNode(ISD::FP_EXTEND, dl, MVT::v4f32, V); | |||
31997 | Results.push_back(V); | |||
31998 | if (IsStrict) | |||
31999 | Results.push_back(V.getValue(1)); | |||
32000 | return; | |||
32001 | } | |||
32002 | case ISD::INTRINSIC_W_CHAIN: { | |||
32003 | unsigned IntNo = N->getConstantOperandVal(1); | |||
32004 | switch (IntNo) { | |||
32005 | default : llvm_unreachable("Do not know how to custom type "::llvm::llvm_unreachable_internal("Do not know how to custom type " "legalize this intrinsic operation!", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 32006) | |||
32006 | "legalize this intrinsic operation!")::llvm::llvm_unreachable_internal("Do not know how to custom type " "legalize this intrinsic operation!", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 32006); | |||
32007 | case Intrinsic::x86_rdtsc: | |||
32008 | return getReadTimeStampCounter(N, dl, X86::RDTSC, DAG, Subtarget, | |||
32009 | Results); | |||
32010 | case Intrinsic::x86_rdtscp: | |||
32011 | return getReadTimeStampCounter(N, dl, X86::RDTSCP, DAG, Subtarget, | |||
32012 | Results); | |||
32013 | case Intrinsic::x86_rdpmc: | |||
32014 | expandIntrinsicWChainHelper(N, dl, DAG, X86::RDPMC, X86::ECX, Subtarget, | |||
32015 | Results); | |||
32016 | return; | |||
32017 | case Intrinsic::x86_xgetbv: | |||
32018 | expandIntrinsicWChainHelper(N, dl, DAG, X86::XGETBV, X86::ECX, Subtarget, | |||
32019 | Results); | |||
32020 | return; | |||
32021 | } | |||
32022 | } | |||
32023 | case ISD::READCYCLECOUNTER: { | |||
32024 | return getReadTimeStampCounter(N, dl, X86::RDTSC, DAG, Subtarget, Results); | |||
32025 | } | |||
32026 | case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: { | |||
32027 | EVT T = N->getValueType(0); | |||
32028 | assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair")(static_cast <bool> ((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair") ? void (0) : __assert_fail ( "(T == MVT::i64 || T == MVT::i128) && \"can only expand cmpxchg pair\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 32028, __extension__ __PRETTY_FUNCTION__)); | |||
32029 | bool Regs64bit = T == MVT::i128; | |||
32030 | assert((!Regs64bit || Subtarget.hasCmpxchg16b()) &&(static_cast <bool> ((!Regs64bit || Subtarget.hasCmpxchg16b ()) && "64-bit ATOMIC_CMP_SWAP_WITH_SUCCESS requires CMPXCHG16B" ) ? void (0) : __assert_fail ("(!Regs64bit || Subtarget.hasCmpxchg16b()) && \"64-bit ATOMIC_CMP_SWAP_WITH_SUCCESS requires CMPXCHG16B\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 32031, __extension__ __PRETTY_FUNCTION__)) | |||
32031 | "64-bit ATOMIC_CMP_SWAP_WITH_SUCCESS requires CMPXCHG16B")(static_cast <bool> ((!Regs64bit || Subtarget.hasCmpxchg16b ()) && "64-bit ATOMIC_CMP_SWAP_WITH_SUCCESS requires CMPXCHG16B" ) ? void (0) : __assert_fail ("(!Regs64bit || Subtarget.hasCmpxchg16b()) && \"64-bit ATOMIC_CMP_SWAP_WITH_SUCCESS requires CMPXCHG16B\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 32031, __extension__ __PRETTY_FUNCTION__)); | |||
32032 | MVT HalfT = Regs64bit ? MVT::i64 : MVT::i32; | |||
32033 | SDValue cpInL, cpInH; | |||
32034 | cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2), | |||
32035 | DAG.getConstant(0, dl, HalfT)); | |||
32036 | cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2), | |||
32037 | DAG.getConstant(1, dl, HalfT)); | |||
32038 | cpInL = DAG.getCopyToReg(N->getOperand(0), dl, | |||
32039 | Regs64bit ? X86::RAX : X86::EAX, | |||
32040 | cpInL, SDValue()); | |||
32041 | cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, | |||
32042 | Regs64bit ? X86::RDX : X86::EDX, | |||
32043 | cpInH, cpInL.getValue(1)); | |||
32044 | SDValue swapInL, swapInH; | |||
32045 | swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3), | |||
32046 | DAG.getConstant(0, dl, HalfT)); | |||
32047 | swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3), | |||
32048 | DAG.getConstant(1, dl, HalfT)); | |||
32049 | swapInH = | |||
32050 | DAG.getCopyToReg(cpInH.getValue(0), dl, Regs64bit ? X86::RCX : X86::ECX, | |||
32051 | swapInH, cpInH.getValue(1)); | |||
32052 | ||||
32053 | // In 64-bit mode we might need the base pointer in RBX, but we can't know | |||
32054 | // until later. So we keep the RBX input in a vreg and use a custom | |||
32055 | // inserter. | |||
32056 | // Since RBX will be a reserved register the register allocator will not | |||
32057 | // make sure its value will be properly saved and restored around this | |||
32058 | // live-range. | |||
32059 | SDValue Result; | |||
32060 | SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue); | |||
32061 | MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand(); | |||
32062 | if (Regs64bit) { | |||
32063 | SDValue Ops[] = {swapInH.getValue(0), N->getOperand(1), swapInL, | |||
32064 | swapInH.getValue(1)}; | |||
32065 | Result = | |||
32066 | DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG16_DAG, dl, Tys, Ops, T, MMO); | |||
32067 | } else { | |||
32068 | swapInL = DAG.getCopyToReg(swapInH.getValue(0), dl, X86::EBX, swapInL, | |||
32069 | swapInH.getValue(1)); | |||
32070 | SDValue Ops[] = {swapInL.getValue(0), N->getOperand(1), | |||
32071 | swapInL.getValue(1)}; | |||
32072 | Result = | |||
32073 | DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, T, MMO); | |||
32074 | } | |||
32075 | ||||
32076 | SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, | |||
32077 | Regs64bit ? X86::RAX : X86::EAX, | |||
32078 | HalfT, Result.getValue(1)); | |||
32079 | SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, | |||
32080 | Regs64bit ? X86::RDX : X86::EDX, | |||
32081 | HalfT, cpOutL.getValue(2)); | |||
32082 | SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)}; | |||
32083 | ||||
32084 | SDValue EFLAGS = DAG.getCopyFromReg(cpOutH.getValue(1), dl, X86::EFLAGS, | |||
32085 | MVT::i32, cpOutH.getValue(2)); | |||
32086 | SDValue Success = getSETCC(X86::COND_E, EFLAGS, dl, DAG); | |||
32087 | Success = DAG.getZExtOrTrunc(Success, dl, N->getValueType(1)); | |||
32088 | ||||
32089 | Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF)); | |||
32090 | Results.push_back(Success); | |||
32091 | Results.push_back(EFLAGS.getValue(1)); | |||
32092 | return; | |||
32093 | } | |||
32094 | case ISD::ATOMIC_LOAD: { | |||
32095 | assert(N->getValueType(0) == MVT::i64 && "Unexpected VT!")(static_cast <bool> (N->getValueType(0) == MVT::i64 && "Unexpected VT!") ? void (0) : __assert_fail ("N->getValueType(0) == MVT::i64 && \"Unexpected VT!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 32095, __extension__ __PRETTY_FUNCTION__)); | |||
32096 | bool NoImplicitFloatOps = | |||
32097 | DAG.getMachineFunction().getFunction().hasFnAttribute( | |||
32098 | Attribute::NoImplicitFloat); | |||
32099 | if (!Subtarget.useSoftFloat() && !NoImplicitFloatOps) { | |||
32100 | auto *Node = cast<AtomicSDNode>(N); | |||
32101 | if (Subtarget.hasSSE1()) { | |||
32102 | // Use a VZEXT_LOAD which will be selected as MOVQ or XORPS+MOVLPS. | |||
32103 | // Then extract the lower 64-bits. | |||
32104 | MVT LdVT = Subtarget.hasSSE2() ? MVT::v2i64 : MVT::v4f32; | |||
32105 | SDVTList Tys = DAG.getVTList(LdVT, MVT::Other); | |||
32106 | SDValue Ops[] = { Node->getChain(), Node->getBasePtr() }; | |||
32107 | SDValue Ld = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, | |||
32108 | MVT::i64, Node->getMemOperand()); | |||
32109 | if (Subtarget.hasSSE2()) { | |||
32110 | SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64, Ld, | |||
32111 | DAG.getIntPtrConstant(0, dl)); | |||
32112 | Results.push_back(Res); | |||
32113 | Results.push_back(Ld.getValue(1)); | |||
32114 | return; | |||
32115 | } | |||
32116 | // We use an alternative sequence for SSE1 that extracts as v2f32 and | |||
32117 | // then casts to i64. This avoids a 128-bit stack temporary being | |||
32118 | // created by type legalization if we were to cast v4f32->v2i64. | |||
32119 | SDValue Res = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2f32, Ld, | |||
32120 | DAG.getIntPtrConstant(0, dl)); | |||
32121 | Res = DAG.getBitcast(MVT::i64, Res); | |||
32122 | Results.push_back(Res); | |||
32123 | Results.push_back(Ld.getValue(1)); | |||
32124 | return; | |||
32125 | } | |||
32126 | if (Subtarget.hasX87()) { | |||
32127 | // First load this into an 80-bit X87 register. This will put the whole | |||
32128 | // integer into the significand. | |||
32129 | SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other); | |||
32130 | SDValue Ops[] = { Node->getChain(), Node->getBasePtr() }; | |||
32131 | SDValue Result = DAG.getMemIntrinsicNode(X86ISD::FILD, | |||
32132 | dl, Tys, Ops, MVT::i64, | |||
32133 | Node->getMemOperand()); | |||
32134 | SDValue Chain = Result.getValue(1); | |||
32135 | ||||
32136 | // Now store the X87 register to a stack temporary and convert to i64. | |||
32137 | // This store is not atomic and doesn't need to be. | |||
32138 | // FIXME: We don't need a stack temporary if the result of the load | |||
32139 | // is already being stored. We could just directly store there. | |||
32140 | SDValue StackPtr = DAG.CreateStackTemporary(MVT::i64); | |||
32141 | int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex(); | |||
32142 | MachinePointerInfo MPI = | |||
32143 | MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SPFI); | |||
32144 | SDValue StoreOps[] = { Chain, Result, StackPtr }; | |||
32145 | Chain = DAG.getMemIntrinsicNode( | |||
32146 | X86ISD::FIST, dl, DAG.getVTList(MVT::Other), StoreOps, MVT::i64, | |||
32147 | MPI, None /*Align*/, MachineMemOperand::MOStore); | |||
32148 | ||||
32149 | // Finally load the value back from the stack temporary and return it. | |||
32150 | // This load is not atomic and doesn't need to be. | |||
32151 | // This load will be further type legalized. | |||
32152 | Result = DAG.getLoad(MVT::i64, dl, Chain, StackPtr, MPI); | |||
32153 | Results.push_back(Result); | |||
32154 | Results.push_back(Result.getValue(1)); | |||
32155 | return; | |||
32156 | } | |||
32157 | } | |||
32158 | // TODO: Use MOVLPS when SSE1 is available? | |||
32159 | // Delegate to generic TypeLegalization. Situations we can really handle | |||
32160 | // should have already been dealt with by AtomicExpandPass.cpp. | |||
32161 | break; | |||
32162 | } | |||
32163 | case ISD::ATOMIC_SWAP: | |||
32164 | case ISD::ATOMIC_LOAD_ADD: | |||
32165 | case ISD::ATOMIC_LOAD_SUB: | |||
32166 | case ISD::ATOMIC_LOAD_AND: | |||
32167 | case ISD::ATOMIC_LOAD_OR: | |||
32168 | case ISD::ATOMIC_LOAD_XOR: | |||
32169 | case ISD::ATOMIC_LOAD_NAND: | |||
32170 | case ISD::ATOMIC_LOAD_MIN: | |||
32171 | case ISD::ATOMIC_LOAD_MAX: | |||
32172 | case ISD::ATOMIC_LOAD_UMIN: | |||
32173 | case ISD::ATOMIC_LOAD_UMAX: | |||
32174 | // Delegate to generic TypeLegalization. Situations we can really handle | |||
32175 | // should have already been dealt with by AtomicExpandPass.cpp. | |||
32176 | break; | |||
32177 | ||||
32178 | case ISD::BITCAST: { | |||
32179 | assert(Subtarget.hasSSE2() && "Requires at least SSE2!")(static_cast <bool> (Subtarget.hasSSE2() && "Requires at least SSE2!" ) ? void (0) : __assert_fail ("Subtarget.hasSSE2() && \"Requires at least SSE2!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 32179, __extension__ __PRETTY_FUNCTION__)); | |||
32180 | EVT DstVT = N->getValueType(0); | |||
32181 | EVT SrcVT = N->getOperand(0).getValueType(); | |||
32182 | ||||
32183 | // If this is a bitcast from a v64i1 k-register to a i64 on a 32-bit target | |||
32184 | // we can split using the k-register rather than memory. | |||
32185 | if (SrcVT == MVT::v64i1 && DstVT == MVT::i64 && Subtarget.hasBWI()) { | |||
32186 | assert(!Subtarget.is64Bit() && "Expected 32-bit mode")(static_cast <bool> (!Subtarget.is64Bit() && "Expected 32-bit mode" ) ? void (0) : __assert_fail ("!Subtarget.is64Bit() && \"Expected 32-bit mode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 32186, __extension__ __PRETTY_FUNCTION__)); | |||
32187 | SDValue Lo, Hi; | |||
32188 | std::tie(Lo, Hi) = DAG.SplitVectorOperand(N, 0); | |||
32189 | Lo = DAG.getBitcast(MVT::i32, Lo); | |||
32190 | Hi = DAG.getBitcast(MVT::i32, Hi); | |||
32191 | SDValue Res = DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Lo, Hi); | |||
32192 | Results.push_back(Res); | |||
32193 | return; | |||
32194 | } | |||
32195 | ||||
32196 | if (DstVT.isVector() && SrcVT == MVT::x86mmx) { | |||
32197 | // FIXME: Use v4f32 for SSE1? | |||
32198 | assert(Subtarget.hasSSE2() && "Requires SSE2")(static_cast <bool> (Subtarget.hasSSE2() && "Requires SSE2" ) ? void (0) : __assert_fail ("Subtarget.hasSSE2() && \"Requires SSE2\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 32198, __extension__ __PRETTY_FUNCTION__)); | |||
32199 | assert(getTypeAction(*DAG.getContext(), DstVT) == TypeWidenVector &&(static_cast <bool> (getTypeAction(*DAG.getContext(), DstVT ) == TypeWidenVector && "Unexpected type action!") ? void (0) : __assert_fail ("getTypeAction(*DAG.getContext(), DstVT) == TypeWidenVector && \"Unexpected type action!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 32200, __extension__ __PRETTY_FUNCTION__)) | |||
32200 | "Unexpected type action!")(static_cast <bool> (getTypeAction(*DAG.getContext(), DstVT ) == TypeWidenVector && "Unexpected type action!") ? void (0) : __assert_fail ("getTypeAction(*DAG.getContext(), DstVT) == TypeWidenVector && \"Unexpected type action!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 32200, __extension__ __PRETTY_FUNCTION__)); | |||
32201 | EVT WideVT = getTypeToTransformTo(*DAG.getContext(), DstVT); | |||
32202 | SDValue Res = DAG.getNode(X86ISD::MOVQ2DQ, dl, MVT::v2i64, | |||
32203 | N->getOperand(0)); | |||
32204 | Res = DAG.getBitcast(WideVT, Res); | |||
32205 | Results.push_back(Res); | |||
32206 | return; | |||
32207 | } | |||
32208 | ||||
32209 | return; | |||
32210 | } | |||
32211 | case ISD::MGATHER: { | |||
32212 | EVT VT = N->getValueType(0); | |||
32213 | if ((VT == MVT::v2f32 || VT == MVT::v2i32) && | |||
32214 | (Subtarget.hasVLX() || !Subtarget.hasAVX512())) { | |||
32215 | auto *Gather = cast<MaskedGatherSDNode>(N); | |||
32216 | SDValue Index = Gather->getIndex(); | |||
32217 | if (Index.getValueType() != MVT::v2i64) | |||
32218 | return; | |||
32219 | assert(getTypeAction(*DAG.getContext(), VT) == TypeWidenVector &&(static_cast <bool> (getTypeAction(*DAG.getContext(), VT ) == TypeWidenVector && "Unexpected type action!") ? void (0) : __assert_fail ("getTypeAction(*DAG.getContext(), VT) == TypeWidenVector && \"Unexpected type action!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 32220, __extension__ __PRETTY_FUNCTION__)) | |||
32220 | "Unexpected type action!")(static_cast <bool> (getTypeAction(*DAG.getContext(), VT ) == TypeWidenVector && "Unexpected type action!") ? void (0) : __assert_fail ("getTypeAction(*DAG.getContext(), VT) == TypeWidenVector && \"Unexpected type action!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 32220, __extension__ __PRETTY_FUNCTION__)); | |||
32221 | EVT WideVT = getTypeToTransformTo(*DAG.getContext(), VT); | |||
32222 | SDValue Mask = Gather->getMask(); | |||
32223 | assert(Mask.getValueType() == MVT::v2i1 && "Unexpected mask type")(static_cast <bool> (Mask.getValueType() == MVT::v2i1 && "Unexpected mask type") ? void (0) : __assert_fail ("Mask.getValueType() == MVT::v2i1 && \"Unexpected mask type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 32223, __extension__ __PRETTY_FUNCTION__)); | |||
32224 | SDValue PassThru = DAG.getNode(ISD::CONCAT_VECTORS, dl, WideVT, | |||
32225 | Gather->getPassThru(), | |||
32226 | DAG.getUNDEF(VT)); | |||
32227 | if (!Subtarget.hasVLX()) { | |||
32228 | // We need to widen the mask, but the instruction will only use 2 | |||
32229 | // of its elements. So we can use undef. | |||
32230 | Mask = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v4i1, Mask, | |||
32231 | DAG.getUNDEF(MVT::v2i1)); | |||
32232 | Mask = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i32, Mask); | |||
32233 | } | |||
32234 | SDValue Ops[] = { Gather->getChain(), PassThru, Mask, | |||
32235 | Gather->getBasePtr(), Index, Gather->getScale() }; | |||
32236 | SDValue Res = DAG.getMemIntrinsicNode( | |||
32237 | X86ISD::MGATHER, dl, DAG.getVTList(WideVT, MVT::Other), Ops, | |||
32238 | Gather->getMemoryVT(), Gather->getMemOperand()); | |||
32239 | Results.push_back(Res); | |||
32240 | Results.push_back(Res.getValue(1)); | |||
32241 | return; | |||
32242 | } | |||
32243 | return; | |||
32244 | } | |||
32245 | case ISD::LOAD: { | |||
32246 | // Use an f64/i64 load and a scalar_to_vector for v2f32/v2i32 loads. This | |||
32247 | // avoids scalarizing in 32-bit mode. In 64-bit mode this avoids a int->fp | |||
32248 | // cast since type legalization will try to use an i64 load. | |||
32249 | MVT VT = N->getSimpleValueType(0); | |||
32250 | assert(VT.isVector() && VT.getSizeInBits() == 64 && "Unexpected VT")(static_cast <bool> (VT.isVector() && VT.getSizeInBits () == 64 && "Unexpected VT") ? void (0) : __assert_fail ("VT.isVector() && VT.getSizeInBits() == 64 && \"Unexpected VT\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 32250, __extension__ __PRETTY_FUNCTION__)); | |||
32251 | assert(getTypeAction(*DAG.getContext(), VT) == TypeWidenVector &&(static_cast <bool> (getTypeAction(*DAG.getContext(), VT ) == TypeWidenVector && "Unexpected type action!") ? void (0) : __assert_fail ("getTypeAction(*DAG.getContext(), VT) == TypeWidenVector && \"Unexpected type action!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 32252, __extension__ __PRETTY_FUNCTION__)) | |||
32252 | "Unexpected type action!")(static_cast <bool> (getTypeAction(*DAG.getContext(), VT ) == TypeWidenVector && "Unexpected type action!") ? void (0) : __assert_fail ("getTypeAction(*DAG.getContext(), VT) == TypeWidenVector && \"Unexpected type action!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 32252, __extension__ __PRETTY_FUNCTION__)); | |||
32253 | if (!ISD::isNON_EXTLoad(N)) | |||
32254 | return; | |||
32255 | auto *Ld = cast<LoadSDNode>(N); | |||
32256 | if (Subtarget.hasSSE2()) { | |||
32257 | MVT LdVT = Subtarget.is64Bit() && VT.isInteger() ? MVT::i64 : MVT::f64; | |||
32258 | SDValue Res = DAG.getLoad(LdVT, dl, Ld->getChain(), Ld->getBasePtr(), | |||
32259 | Ld->getPointerInfo(), Ld->getOriginalAlign(), | |||
32260 | Ld->getMemOperand()->getFlags()); | |||
32261 | SDValue Chain = Res.getValue(1); | |||
32262 | MVT VecVT = MVT::getVectorVT(LdVT, 2); | |||
32263 | Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Res); | |||
32264 | EVT WideVT = getTypeToTransformTo(*DAG.getContext(), VT); | |||
32265 | Res = DAG.getBitcast(WideVT, Res); | |||
32266 | Results.push_back(Res); | |||
32267 | Results.push_back(Chain); | |||
32268 | return; | |||
32269 | } | |||
32270 | assert(Subtarget.hasSSE1() && "Expected SSE")(static_cast <bool> (Subtarget.hasSSE1() && "Expected SSE" ) ? void (0) : __assert_fail ("Subtarget.hasSSE1() && \"Expected SSE\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 32270, __extension__ __PRETTY_FUNCTION__)); | |||
32271 | SDVTList Tys = DAG.getVTList(MVT::v4f32, MVT::Other); | |||
32272 | SDValue Ops[] = {Ld->getChain(), Ld->getBasePtr()}; | |||
32273 | SDValue Res = DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, | |||
32274 | MVT::i64, Ld->getMemOperand()); | |||
32275 | Results.push_back(Res); | |||
32276 | Results.push_back(Res.getValue(1)); | |||
32277 | return; | |||
32278 | } | |||
32279 | case ISD::ADDRSPACECAST: { | |||
32280 | SDValue V = LowerADDRSPACECAST(SDValue(N,0), DAG); | |||
32281 | Results.push_back(V); | |||
32282 | return; | |||
32283 | } | |||
32284 | case ISD::BITREVERSE: | |||
32285 | assert(N->getValueType(0) == MVT::i64 && "Unexpected VT!")(static_cast <bool> (N->getValueType(0) == MVT::i64 && "Unexpected VT!") ? void (0) : __assert_fail ("N->getValueType(0) == MVT::i64 && \"Unexpected VT!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 32285, __extension__ __PRETTY_FUNCTION__)); | |||
32286 | assert(Subtarget.hasXOP() && "Expected XOP")(static_cast <bool> (Subtarget.hasXOP() && "Expected XOP" ) ? void (0) : __assert_fail ("Subtarget.hasXOP() && \"Expected XOP\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 32286, __extension__ __PRETTY_FUNCTION__)); | |||
32287 | // We can use VPPERM by copying to a vector register and back. We'll need | |||
32288 | // to move the scalar in two i32 pieces. | |||
32289 | Results.push_back(LowerBITREVERSE(SDValue(N, 0), Subtarget, DAG)); | |||
32290 | return; | |||
32291 | } | |||
32292 | } | |||
32293 | ||||
32294 | const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const { | |||
32295 | switch ((X86ISD::NodeType)Opcode) { | |||
32296 | case X86ISD::FIRST_NUMBER: break; | |||
32297 | #define NODE_NAME_CASE(NODE) case X86ISD::NODE: return "X86ISD::" #NODE; | |||
32298 | NODE_NAME_CASE(BSF) | |||
32299 | NODE_NAME_CASE(BSR) | |||
32300 | NODE_NAME_CASE(FSHL) | |||
32301 | NODE_NAME_CASE(FSHR) | |||
32302 | NODE_NAME_CASE(FAND) | |||
32303 | NODE_NAME_CASE(FANDN) | |||
32304 | NODE_NAME_CASE(FOR) | |||
32305 | NODE_NAME_CASE(FXOR) | |||
32306 | NODE_NAME_CASE(FILD) | |||
32307 | NODE_NAME_CASE(FIST) | |||
32308 | NODE_NAME_CASE(FP_TO_INT_IN_MEM) | |||
32309 | NODE_NAME_CASE(FLD) | |||
32310 | NODE_NAME_CASE(FST) | |||
32311 | NODE_NAME_CASE(CALL) | |||
32312 | NODE_NAME_CASE(CALL_RVMARKER) | |||
32313 | NODE_NAME_CASE(BT) | |||
32314 | NODE_NAME_CASE(CMP) | |||
32315 | NODE_NAME_CASE(FCMP) | |||
32316 | NODE_NAME_CASE(STRICT_FCMP) | |||
32317 | NODE_NAME_CASE(STRICT_FCMPS) | |||
32318 | NODE_NAME_CASE(COMI) | |||
32319 | NODE_NAME_CASE(UCOMI) | |||
32320 | NODE_NAME_CASE(CMPM) | |||
32321 | NODE_NAME_CASE(CMPMM) | |||
32322 | NODE_NAME_CASE(STRICT_CMPM) | |||
32323 | NODE_NAME_CASE(CMPMM_SAE) | |||
32324 | NODE_NAME_CASE(SETCC) | |||
32325 | NODE_NAME_CASE(SETCC_CARRY) | |||
32326 | NODE_NAME_CASE(FSETCC) | |||
32327 | NODE_NAME_CASE(FSETCCM) | |||
32328 | NODE_NAME_CASE(FSETCCM_SAE) | |||
32329 | NODE_NAME_CASE(CMOV) | |||
32330 | NODE_NAME_CASE(BRCOND) | |||
32331 | NODE_NAME_CASE(RET_FLAG) | |||
32332 | NODE_NAME_CASE(IRET) | |||
32333 | NODE_NAME_CASE(REP_STOS) | |||
32334 | NODE_NAME_CASE(REP_MOVS) | |||
32335 | NODE_NAME_CASE(GlobalBaseReg) | |||
32336 | NODE_NAME_CASE(Wrapper) | |||
32337 | NODE_NAME_CASE(WrapperRIP) | |||
32338 | NODE_NAME_CASE(MOVQ2DQ) | |||
32339 | NODE_NAME_CASE(MOVDQ2Q) | |||
32340 | NODE_NAME_CASE(MMX_MOVD2W) | |||
32341 | NODE_NAME_CASE(MMX_MOVW2D) | |||
32342 | NODE_NAME_CASE(PEXTRB) | |||
32343 | NODE_NAME_CASE(PEXTRW) | |||
32344 | NODE_NAME_CASE(INSERTPS) | |||
32345 | NODE_NAME_CASE(PINSRB) | |||
32346 | NODE_NAME_CASE(PINSRW) | |||
32347 | NODE_NAME_CASE(PSHUFB) | |||
32348 | NODE_NAME_CASE(ANDNP) | |||
32349 | NODE_NAME_CASE(BLENDI) | |||
32350 | NODE_NAME_CASE(BLENDV) | |||
32351 | NODE_NAME_CASE(HADD) | |||
32352 | NODE_NAME_CASE(HSUB) | |||
32353 | NODE_NAME_CASE(FHADD) | |||
32354 | NODE_NAME_CASE(FHSUB) | |||
32355 | NODE_NAME_CASE(CONFLICT) | |||
32356 | NODE_NAME_CASE(FMAX) | |||
32357 | NODE_NAME_CASE(FMAXS) | |||
32358 | NODE_NAME_CASE(FMAX_SAE) | |||
32359 | NODE_NAME_CASE(FMAXS_SAE) | |||
32360 | NODE_NAME_CASE(FMIN) | |||
32361 | NODE_NAME_CASE(FMINS) | |||
32362 | NODE_NAME_CASE(FMIN_SAE) | |||
32363 | NODE_NAME_CASE(FMINS_SAE) | |||
32364 | NODE_NAME_CASE(FMAXC) | |||
32365 | NODE_NAME_CASE(FMINC) | |||
32366 | NODE_NAME_CASE(FRSQRT) | |||
32367 | NODE_NAME_CASE(FRCP) | |||
32368 | NODE_NAME_CASE(EXTRQI) | |||
32369 | NODE_NAME_CASE(INSERTQI) | |||
32370 | NODE_NAME_CASE(TLSADDR) | |||
32371 | NODE_NAME_CASE(TLSBASEADDR) | |||
32372 | NODE_NAME_CASE(TLSCALL) | |||
32373 | NODE_NAME_CASE(EH_SJLJ_SETJMP) | |||
32374 | NODE_NAME_CASE(EH_SJLJ_LONGJMP) | |||
32375 | NODE_NAME_CASE(EH_SJLJ_SETUP_DISPATCH) | |||
32376 | NODE_NAME_CASE(EH_RETURN) | |||
32377 | NODE_NAME_CASE(TC_RETURN) | |||
32378 | NODE_NAME_CASE(FNSTCW16m) | |||
32379 | NODE_NAME_CASE(FLDCW16m) | |||
32380 | NODE_NAME_CASE(LCMPXCHG_DAG) | |||
32381 | NODE_NAME_CASE(LCMPXCHG8_DAG) | |||
32382 | NODE_NAME_CASE(LCMPXCHG16_DAG) | |||
32383 | NODE_NAME_CASE(LCMPXCHG16_SAVE_RBX_DAG) | |||
32384 | NODE_NAME_CASE(LADD) | |||
32385 | NODE_NAME_CASE(LSUB) | |||
32386 | NODE_NAME_CASE(LOR) | |||
32387 | NODE_NAME_CASE(LXOR) | |||
32388 | NODE_NAME_CASE(LAND) | |||
32389 | NODE_NAME_CASE(VZEXT_MOVL) | |||
32390 | NODE_NAME_CASE(VZEXT_LOAD) | |||
32391 | NODE_NAME_CASE(VEXTRACT_STORE) | |||
32392 | NODE_NAME_CASE(VTRUNC) | |||
32393 | NODE_NAME_CASE(VTRUNCS) | |||
32394 | NODE_NAME_CASE(VTRUNCUS) | |||
32395 | NODE_NAME_CASE(VMTRUNC) | |||
32396 | NODE_NAME_CASE(VMTRUNCS) | |||
32397 | NODE_NAME_CASE(VMTRUNCUS) | |||
32398 | NODE_NAME_CASE(VTRUNCSTORES) | |||
32399 | NODE_NAME_CASE(VTRUNCSTOREUS) | |||
32400 | NODE_NAME_CASE(VMTRUNCSTORES) | |||
32401 | NODE_NAME_CASE(VMTRUNCSTOREUS) | |||
32402 | NODE_NAME_CASE(VFPEXT) | |||
32403 | NODE_NAME_CASE(STRICT_VFPEXT) | |||
32404 | NODE_NAME_CASE(VFPEXT_SAE) | |||
32405 | NODE_NAME_CASE(VFPEXTS) | |||
32406 | NODE_NAME_CASE(VFPEXTS_SAE) | |||
32407 | NODE_NAME_CASE(VFPROUND) | |||
32408 | NODE_NAME_CASE(STRICT_VFPROUND) | |||
32409 | NODE_NAME_CASE(VMFPROUND) | |||
32410 | NODE_NAME_CASE(VFPROUND_RND) | |||
32411 | NODE_NAME_CASE(VFPROUNDS) | |||
32412 | NODE_NAME_CASE(VFPROUNDS_RND) | |||
32413 | NODE_NAME_CASE(VSHLDQ) | |||
32414 | NODE_NAME_CASE(VSRLDQ) | |||
32415 | NODE_NAME_CASE(VSHL) | |||
32416 | NODE_NAME_CASE(VSRL) | |||
32417 | NODE_NAME_CASE(VSRA) | |||
32418 | NODE_NAME_CASE(VSHLI) | |||
32419 | NODE_NAME_CASE(VSRLI) | |||
32420 | NODE_NAME_CASE(VSRAI) | |||
32421 | NODE_NAME_CASE(VSHLV) | |||
32422 | NODE_NAME_CASE(VSRLV) | |||
32423 | NODE_NAME_CASE(VSRAV) | |||
32424 | NODE_NAME_CASE(VROTLI) | |||
32425 | NODE_NAME_CASE(VROTRI) | |||
32426 | NODE_NAME_CASE(VPPERM) | |||
32427 | NODE_NAME_CASE(CMPP) | |||
32428 | NODE_NAME_CASE(STRICT_CMPP) | |||
32429 | NODE_NAME_CASE(PCMPEQ) | |||
32430 | NODE_NAME_CASE(PCMPGT) | |||
32431 | NODE_NAME_CASE(PHMINPOS) | |||
32432 | NODE_NAME_CASE(ADD) | |||
32433 | NODE_NAME_CASE(SUB) | |||
32434 | NODE_NAME_CASE(ADC) | |||
32435 | NODE_NAME_CASE(SBB) | |||
32436 | NODE_NAME_CASE(SMUL) | |||
32437 | NODE_NAME_CASE(UMUL) | |||
32438 | NODE_NAME_CASE(OR) | |||
32439 | NODE_NAME_CASE(XOR) | |||
32440 | NODE_NAME_CASE(AND) | |||
32441 | NODE_NAME_CASE(BEXTR) | |||
32442 | NODE_NAME_CASE(BEXTRI) | |||
32443 | NODE_NAME_CASE(BZHI) | |||
32444 | NODE_NAME_CASE(PDEP) | |||
32445 | NODE_NAME_CASE(PEXT) | |||
32446 | NODE_NAME_CASE(MUL_IMM) | |||
32447 | NODE_NAME_CASE(MOVMSK) | |||
32448 | NODE_NAME_CASE(PTEST) | |||
32449 | NODE_NAME_CASE(TESTP) | |||
32450 | NODE_NAME_CASE(KORTEST) | |||
32451 | NODE_NAME_CASE(KTEST) | |||
32452 | NODE_NAME_CASE(KADD) | |||
32453 | NODE_NAME_CASE(KSHIFTL) | |||
32454 | NODE_NAME_CASE(KSHIFTR) | |||
32455 | NODE_NAME_CASE(PACKSS) | |||
32456 | NODE_NAME_CASE(PACKUS) | |||
32457 | NODE_NAME_CASE(PALIGNR) | |||
32458 | NODE_NAME_CASE(VALIGN) | |||
32459 | NODE_NAME_CASE(VSHLD) | |||
32460 | NODE_NAME_CASE(VSHRD) | |||
32461 | NODE_NAME_CASE(VSHLDV) | |||
32462 | NODE_NAME_CASE(VSHRDV) | |||
32463 | NODE_NAME_CASE(PSHUFD) | |||
32464 | NODE_NAME_CASE(PSHUFHW) | |||
32465 | NODE_NAME_CASE(PSHUFLW) | |||
32466 | NODE_NAME_CASE(SHUFP) | |||
32467 | NODE_NAME_CASE(SHUF128) | |||
32468 | NODE_NAME_CASE(MOVLHPS) | |||
32469 | NODE_NAME_CASE(MOVHLPS) | |||
32470 | NODE_NAME_CASE(MOVDDUP) | |||
32471 | NODE_NAME_CASE(MOVSHDUP) | |||
32472 | NODE_NAME_CASE(MOVSLDUP) | |||
32473 | NODE_NAME_CASE(MOVSD) | |||
32474 | NODE_NAME_CASE(MOVSS) | |||
32475 | NODE_NAME_CASE(MOVSH) | |||
32476 | NODE_NAME_CASE(UNPCKL) | |||
32477 | NODE_NAME_CASE(UNPCKH) | |||
32478 | NODE_NAME_CASE(VBROADCAST) | |||
32479 | NODE_NAME_CASE(VBROADCAST_LOAD) | |||
32480 | NODE_NAME_CASE(VBROADCASTM) | |||
32481 | NODE_NAME_CASE(SUBV_BROADCAST_LOAD) | |||
32482 | NODE_NAME_CASE(VPERMILPV) | |||
32483 | NODE_NAME_CASE(VPERMILPI) | |||
32484 | NODE_NAME_CASE(VPERM2X128) | |||
32485 | NODE_NAME_CASE(VPERMV) | |||
32486 | NODE_NAME_CASE(VPERMV3) | |||
32487 | NODE_NAME_CASE(VPERMI) | |||
32488 | NODE_NAME_CASE(VPTERNLOG) | |||
32489 | NODE_NAME_CASE(VFIXUPIMM) | |||
32490 | NODE_NAME_CASE(VFIXUPIMM_SAE) | |||
32491 | NODE_NAME_CASE(VFIXUPIMMS) | |||
32492 | NODE_NAME_CASE(VFIXUPIMMS_SAE) | |||
32493 | NODE_NAME_CASE(VRANGE) | |||
32494 | NODE_NAME_CASE(VRANGE_SAE) | |||
32495 | NODE_NAME_CASE(VRANGES) | |||
32496 | NODE_NAME_CASE(VRANGES_SAE) | |||
32497 | NODE_NAME_CASE(PMULUDQ) | |||
32498 | NODE_NAME_CASE(PMULDQ) | |||
32499 | NODE_NAME_CASE(PSADBW) | |||
32500 | NODE_NAME_CASE(DBPSADBW) | |||
32501 | NODE_NAME_CASE(VASTART_SAVE_XMM_REGS) | |||
32502 | NODE_NAME_CASE(VAARG_64) | |||
32503 | NODE_NAME_CASE(VAARG_X32) | |||
32504 | NODE_NAME_CASE(DYN_ALLOCA) | |||
32505 | NODE_NAME_CASE(MEMBARRIER) | |||
32506 | NODE_NAME_CASE(MFENCE) | |||
32507 | NODE_NAME_CASE(SEG_ALLOCA) | |||
32508 | NODE_NAME_CASE(PROBED_ALLOCA) | |||
32509 | NODE_NAME_CASE(RDRAND) | |||
32510 | NODE_NAME_CASE(RDSEED) | |||
32511 | NODE_NAME_CASE(RDPKRU) | |||
32512 | NODE_NAME_CASE(WRPKRU) | |||
32513 | NODE_NAME_CASE(VPMADDUBSW) | |||
32514 | NODE_NAME_CASE(VPMADDWD) | |||
32515 | NODE_NAME_CASE(VPSHA) | |||
32516 | NODE_NAME_CASE(VPSHL) | |||
32517 | NODE_NAME_CASE(VPCOM) | |||
32518 | NODE_NAME_CASE(VPCOMU) | |||
32519 | NODE_NAME_CASE(VPERMIL2) | |||
32520 | NODE_NAME_CASE(FMSUB) | |||
32521 | NODE_NAME_CASE(STRICT_FMSUB) | |||
32522 | NODE_NAME_CASE(FNMADD) | |||
32523 | NODE_NAME_CASE(STRICT_FNMADD) | |||
32524 | NODE_NAME_CASE(FNMSUB) | |||
32525 | NODE_NAME_CASE(STRICT_FNMSUB) | |||
32526 | NODE_NAME_CASE(FMADDSUB) | |||
32527 | NODE_NAME_CASE(FMSUBADD) | |||
32528 | NODE_NAME_CASE(FMADD_RND) | |||
32529 | NODE_NAME_CASE(FNMADD_RND) | |||
32530 | NODE_NAME_CASE(FMSUB_RND) | |||
32531 | NODE_NAME_CASE(FNMSUB_RND) | |||
32532 | NODE_NAME_CASE(FMADDSUB_RND) | |||
32533 | NODE_NAME_CASE(FMSUBADD_RND) | |||
32534 | NODE_NAME_CASE(VFMADDC) | |||
32535 | NODE_NAME_CASE(VFMADDC_RND) | |||
32536 | NODE_NAME_CASE(VFCMADDC) | |||
32537 | NODE_NAME_CASE(VFCMADDC_RND) | |||
32538 | NODE_NAME_CASE(VFMULC) | |||
32539 | NODE_NAME_CASE(VFMULC_RND) | |||
32540 | NODE_NAME_CASE(VFCMULC) | |||
32541 | NODE_NAME_CASE(VFCMULC_RND) | |||
32542 | NODE_NAME_CASE(VFMULCSH) | |||
32543 | NODE_NAME_CASE(VFMULCSH_RND) | |||
32544 | NODE_NAME_CASE(VFCMULCSH) | |||
32545 | NODE_NAME_CASE(VFCMULCSH_RND) | |||
32546 | NODE_NAME_CASE(VFMADDCSH) | |||
32547 | NODE_NAME_CASE(VFMADDCSH_RND) | |||
32548 | NODE_NAME_CASE(VFCMADDCSH) | |||
32549 | NODE_NAME_CASE(VFCMADDCSH_RND) | |||
32550 | NODE_NAME_CASE(VPMADD52H) | |||
32551 | NODE_NAME_CASE(VPMADD52L) | |||
32552 | NODE_NAME_CASE(VRNDSCALE) | |||
32553 | NODE_NAME_CASE(STRICT_VRNDSCALE) | |||
32554 | NODE_NAME_CASE(VRNDSCALE_SAE) | |||
32555 | NODE_NAME_CASE(VRNDSCALES) | |||
32556 | NODE_NAME_CASE(VRNDSCALES_SAE) | |||
32557 | NODE_NAME_CASE(VREDUCE) | |||
32558 | NODE_NAME_CASE(VREDUCE_SAE) | |||
32559 | NODE_NAME_CASE(VREDUCES) | |||
32560 | NODE_NAME_CASE(VREDUCES_SAE) | |||
32561 | NODE_NAME_CASE(VGETMANT) | |||
32562 | NODE_NAME_CASE(VGETMANT_SAE) | |||
32563 | NODE_NAME_CASE(VGETMANTS) | |||
32564 | NODE_NAME_CASE(VGETMANTS_SAE) | |||
32565 | NODE_NAME_CASE(PCMPESTR) | |||
32566 | NODE_NAME_CASE(PCMPISTR) | |||
32567 | NODE_NAME_CASE(XTEST) | |||
32568 | NODE_NAME_CASE(COMPRESS) | |||
32569 | NODE_NAME_CASE(EXPAND) | |||
32570 | NODE_NAME_CASE(SELECTS) | |||
32571 | NODE_NAME_CASE(ADDSUB) | |||
32572 | NODE_NAME_CASE(RCP14) | |||
32573 | NODE_NAME_CASE(RCP14S) | |||
32574 | NODE_NAME_CASE(RCP28) | |||
32575 | NODE_NAME_CASE(RCP28_SAE) | |||
32576 | NODE_NAME_CASE(RCP28S) | |||
32577 | NODE_NAME_CASE(RCP28S_SAE) | |||
32578 | NODE_NAME_CASE(EXP2) | |||
32579 | NODE_NAME_CASE(EXP2_SAE) | |||
32580 | NODE_NAME_CASE(RSQRT14) | |||
32581 | NODE_NAME_CASE(RSQRT14S) | |||
32582 | NODE_NAME_CASE(RSQRT28) | |||
32583 | NODE_NAME_CASE(RSQRT28_SAE) | |||
32584 | NODE_NAME_CASE(RSQRT28S) | |||
32585 | NODE_NAME_CASE(RSQRT28S_SAE) | |||
32586 | NODE_NAME_CASE(FADD_RND) | |||
32587 | NODE_NAME_CASE(FADDS) | |||
32588 | NODE_NAME_CASE(FADDS_RND) | |||
32589 | NODE_NAME_CASE(FSUB_RND) | |||
32590 | NODE_NAME_CASE(FSUBS) | |||
32591 | NODE_NAME_CASE(FSUBS_RND) | |||
32592 | NODE_NAME_CASE(FMUL_RND) | |||
32593 | NODE_NAME_CASE(FMULS) | |||
32594 | NODE_NAME_CASE(FMULS_RND) | |||
32595 | NODE_NAME_CASE(FDIV_RND) | |||
32596 | NODE_NAME_CASE(FDIVS) | |||
32597 | NODE_NAME_CASE(FDIVS_RND) | |||
32598 | NODE_NAME_CASE(FSQRT_RND) | |||
32599 | NODE_NAME_CASE(FSQRTS) | |||
32600 | NODE_NAME_CASE(FSQRTS_RND) | |||
32601 | NODE_NAME_CASE(FGETEXP) | |||
32602 | NODE_NAME_CASE(FGETEXP_SAE) | |||
32603 | NODE_NAME_CASE(FGETEXPS) | |||
32604 | NODE_NAME_CASE(FGETEXPS_SAE) | |||
32605 | NODE_NAME_CASE(SCALEF) | |||
32606 | NODE_NAME_CASE(SCALEF_RND) | |||
32607 | NODE_NAME_CASE(SCALEFS) | |||
32608 | NODE_NAME_CASE(SCALEFS_RND) | |||
32609 | NODE_NAME_CASE(AVG) | |||
32610 | NODE_NAME_CASE(MULHRS) | |||
32611 | NODE_NAME_CASE(SINT_TO_FP_RND) | |||
32612 | NODE_NAME_CASE(UINT_TO_FP_RND) | |||
32613 | NODE_NAME_CASE(CVTTP2SI) | |||
32614 | NODE_NAME_CASE(CVTTP2UI) | |||
32615 | NODE_NAME_CASE(STRICT_CVTTP2SI) | |||
32616 | NODE_NAME_CASE(STRICT_CVTTP2UI) | |||
32617 | NODE_NAME_CASE(MCVTTP2SI) | |||
32618 | NODE_NAME_CASE(MCVTTP2UI) | |||
32619 | NODE_NAME_CASE(CVTTP2SI_SAE) | |||
32620 | NODE_NAME_CASE(CVTTP2UI_SAE) | |||
32621 | NODE_NAME_CASE(CVTTS2SI) | |||
32622 | NODE_NAME_CASE(CVTTS2UI) | |||
32623 | NODE_NAME_CASE(CVTTS2SI_SAE) | |||
32624 | NODE_NAME_CASE(CVTTS2UI_SAE) | |||
32625 | NODE_NAME_CASE(CVTSI2P) | |||
32626 | NODE_NAME_CASE(CVTUI2P) | |||
32627 | NODE_NAME_CASE(STRICT_CVTSI2P) | |||
32628 | NODE_NAME_CASE(STRICT_CVTUI2P) | |||
32629 | NODE_NAME_CASE(MCVTSI2P) | |||
32630 | NODE_NAME_CASE(MCVTUI2P) | |||
32631 | NODE_NAME_CASE(VFPCLASS) | |||
32632 | NODE_NAME_CASE(VFPCLASSS) | |||
32633 | NODE_NAME_CASE(MULTISHIFT) | |||
32634 | NODE_NAME_CASE(SCALAR_SINT_TO_FP) | |||
32635 | NODE_NAME_CASE(SCALAR_SINT_TO_FP_RND) | |||
32636 | NODE_NAME_CASE(SCALAR_UINT_TO_FP) | |||
32637 | NODE_NAME_CASE(SCALAR_UINT_TO_FP_RND) | |||
32638 | NODE_NAME_CASE(CVTPS2PH) | |||
32639 | NODE_NAME_CASE(STRICT_CVTPS2PH) | |||
32640 | NODE_NAME_CASE(MCVTPS2PH) | |||
32641 | NODE_NAME_CASE(CVTPH2PS) | |||
32642 | NODE_NAME_CASE(STRICT_CVTPH2PS) | |||
32643 | NODE_NAME_CASE(CVTPH2PS_SAE) | |||
32644 | NODE_NAME_CASE(CVTP2SI) | |||
32645 | NODE_NAME_CASE(CVTP2UI) | |||
32646 | NODE_NAME_CASE(MCVTP2SI) | |||
32647 | NODE_NAME_CASE(MCVTP2UI) | |||
32648 | NODE_NAME_CASE(CVTP2SI_RND) | |||
32649 | NODE_NAME_CASE(CVTP2UI_RND) | |||
32650 | NODE_NAME_CASE(CVTS2SI) | |||
32651 | NODE_NAME_CASE(CVTS2UI) | |||
32652 | NODE_NAME_CASE(CVTS2SI_RND) | |||
32653 | NODE_NAME_CASE(CVTS2UI_RND) | |||
32654 | NODE_NAME_CASE(CVTNE2PS2BF16) | |||
32655 | NODE_NAME_CASE(CVTNEPS2BF16) | |||
32656 | NODE_NAME_CASE(MCVTNEPS2BF16) | |||
32657 | NODE_NAME_CASE(DPBF16PS) | |||
32658 | NODE_NAME_CASE(LWPINS) | |||
32659 | NODE_NAME_CASE(MGATHER) | |||
32660 | NODE_NAME_CASE(MSCATTER) | |||
32661 | NODE_NAME_CASE(VPDPBUSD) | |||
32662 | NODE_NAME_CASE(VPDPBUSDS) | |||
32663 | NODE_NAME_CASE(VPDPWSSD) | |||
32664 | NODE_NAME_CASE(VPDPWSSDS) | |||
32665 | NODE_NAME_CASE(VPSHUFBITQMB) | |||
32666 | NODE_NAME_CASE(GF2P8MULB) | |||
32667 | NODE_NAME_CASE(GF2P8AFFINEQB) | |||
32668 | NODE_NAME_CASE(GF2P8AFFINEINVQB) | |||
32669 | NODE_NAME_CASE(NT_CALL) | |||
32670 | NODE_NAME_CASE(NT_BRIND) | |||
32671 | NODE_NAME_CASE(UMWAIT) | |||
32672 | NODE_NAME_CASE(TPAUSE) | |||
32673 | NODE_NAME_CASE(ENQCMD) | |||
32674 | NODE_NAME_CASE(ENQCMDS) | |||
32675 | NODE_NAME_CASE(VP2INTERSECT) | |||
32676 | NODE_NAME_CASE(AESENC128KL) | |||
32677 | NODE_NAME_CASE(AESDEC128KL) | |||
32678 | NODE_NAME_CASE(AESENC256KL) | |||
32679 | NODE_NAME_CASE(AESDEC256KL) | |||
32680 | NODE_NAME_CASE(AESENCWIDE128KL) | |||
32681 | NODE_NAME_CASE(AESDECWIDE128KL) | |||
32682 | NODE_NAME_CASE(AESENCWIDE256KL) | |||
32683 | NODE_NAME_CASE(AESDECWIDE256KL) | |||
32684 | NODE_NAME_CASE(TESTUI) | |||
32685 | } | |||
32686 | return nullptr; | |||
32687 | #undef NODE_NAME_CASE | |||
32688 | } | |||
32689 | ||||
32690 | /// Return true if the addressing mode represented by AM is legal for this | |||
32691 | /// target, for a load/store of the specified type. | |||
32692 | bool X86TargetLowering::isLegalAddressingMode(const DataLayout &DL, | |||
32693 | const AddrMode &AM, Type *Ty, | |||
32694 | unsigned AS, | |||
32695 | Instruction *I) const { | |||
32696 | // X86 supports extremely general addressing modes. | |||
32697 | CodeModel::Model M = getTargetMachine().getCodeModel(); | |||
32698 | ||||
32699 | // X86 allows a sign-extended 32-bit immediate field as a displacement. | |||
32700 | if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != nullptr)) | |||
32701 | return false; | |||
32702 | ||||
32703 | if (AM.BaseGV) { | |||
32704 | unsigned GVFlags = Subtarget.classifyGlobalReference(AM.BaseGV); | |||
32705 | ||||
32706 | // If a reference to this global requires an extra load, we can't fold it. | |||
32707 | if (isGlobalStubReference(GVFlags)) | |||
32708 | return false; | |||
32709 | ||||
32710 | // If BaseGV requires a register for the PIC base, we cannot also have a | |||
32711 | // BaseReg specified. | |||
32712 | if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags)) | |||
32713 | return false; | |||
32714 | ||||
32715 | // If lower 4G is not available, then we must use rip-relative addressing. | |||
32716 | if ((M != CodeModel::Small || isPositionIndependent()) && | |||
32717 | Subtarget.is64Bit() && (AM.BaseOffs || AM.Scale > 1)) | |||
32718 | return false; | |||
32719 | } | |||
32720 | ||||
32721 | switch (AM.Scale) { | |||
32722 | case 0: | |||
32723 | case 1: | |||
32724 | case 2: | |||
32725 | case 4: | |||
32726 | case 8: | |||
32727 | // These scales always work. | |||
32728 | break; | |||
32729 | case 3: | |||
32730 | case 5: | |||
32731 | case 9: | |||
32732 | // These scales are formed with basereg+scalereg. Only accept if there is | |||
32733 | // no basereg yet. | |||
32734 | if (AM.HasBaseReg) | |||
32735 | return false; | |||
32736 | break; | |||
32737 | default: // Other stuff never works. | |||
32738 | return false; | |||
32739 | } | |||
32740 | ||||
32741 | return true; | |||
32742 | } | |||
32743 | ||||
32744 | bool X86TargetLowering::isVectorShiftByScalarCheap(Type *Ty) const { | |||
32745 | unsigned Bits = Ty->getScalarSizeInBits(); | |||
32746 | ||||
32747 | // 8-bit shifts are always expensive, but versions with a scalar amount aren't | |||
32748 | // particularly cheaper than those without. | |||
32749 | if (Bits == 8) | |||
32750 | return false; | |||
32751 | ||||
32752 | // XOP has v16i8/v8i16/v4i32/v2i64 variable vector shifts. | |||
32753 | // Splitting for v32i8/v16i16 on XOP+AVX2 targets is still preferred. | |||
32754 | if (Subtarget.hasXOP() && | |||
32755 | (Bits == 8 || Bits == 16 || Bits == 32 || Bits == 64)) | |||
32756 | return false; | |||
32757 | ||||
32758 | // AVX2 has vpsllv[dq] instructions (and other shifts) that make variable | |||
32759 | // shifts just as cheap as scalar ones. | |||
32760 | if (Subtarget.hasAVX2() && (Bits == 32 || Bits == 64)) | |||
32761 | return false; | |||
32762 | ||||
32763 | // AVX512BW has shifts such as vpsllvw. | |||
32764 | if (Subtarget.hasBWI() && Bits == 16) | |||
32765 | return false; | |||
32766 | ||||
32767 | // Otherwise, it's significantly cheaper to shift by a scalar amount than by a | |||
32768 | // fully general vector. | |||
32769 | return true; | |||
32770 | } | |||
32771 | ||||
32772 | bool X86TargetLowering::isBinOp(unsigned Opcode) const { | |||
32773 | switch (Opcode) { | |||
32774 | // These are non-commutative binops. | |||
32775 | // TODO: Add more X86ISD opcodes once we have test coverage. | |||
32776 | case X86ISD::ANDNP: | |||
32777 | case X86ISD::PCMPGT: | |||
32778 | case X86ISD::FMAX: | |||
32779 | case X86ISD::FMIN: | |||
32780 | case X86ISD::FANDN: | |||
32781 | return true; | |||
32782 | } | |||
32783 | ||||
32784 | return TargetLoweringBase::isBinOp(Opcode); | |||
32785 | } | |||
32786 | ||||
32787 | bool X86TargetLowering::isCommutativeBinOp(unsigned Opcode) const { | |||
32788 | switch (Opcode) { | |||
32789 | // TODO: Add more X86ISD opcodes once we have test coverage. | |||
32790 | case X86ISD::AVG: | |||
32791 | case X86ISD::PCMPEQ: | |||
32792 | case X86ISD::PMULDQ: | |||
32793 | case X86ISD::PMULUDQ: | |||
32794 | case X86ISD::FMAXC: | |||
32795 | case X86ISD::FMINC: | |||
32796 | case X86ISD::FAND: | |||
32797 | case X86ISD::FOR: | |||
32798 | case X86ISD::FXOR: | |||
32799 | return true; | |||
32800 | } | |||
32801 | ||||
32802 | return TargetLoweringBase::isCommutativeBinOp(Opcode); | |||
32803 | } | |||
32804 | ||||
32805 | bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const { | |||
32806 | if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy()) | |||
32807 | return false; | |||
32808 | unsigned NumBits1 = Ty1->getPrimitiveSizeInBits(); | |||
32809 | unsigned NumBits2 = Ty2->getPrimitiveSizeInBits(); | |||
32810 | return NumBits1 > NumBits2; | |||
32811 | } | |||
32812 | ||||
32813 | bool X86TargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const { | |||
32814 | if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy()) | |||
32815 | return false; | |||
32816 | ||||
32817 | if (!isTypeLegal(EVT::getEVT(Ty1))) | |||
32818 | return false; | |||
32819 | ||||
32820 | assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop")(static_cast <bool> (Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop") ? void (0) : __assert_fail ("Ty1->getPrimitiveSizeInBits() <= 64 && \"i128 is probably not a noop\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 32820, __extension__ __PRETTY_FUNCTION__)); | |||
32821 | ||||
32822 | // Assuming the caller doesn't have a zeroext or signext return parameter, | |||
32823 | // truncation all the way down to i1 is valid. | |||
32824 | return true; | |||
32825 | } | |||
32826 | ||||
32827 | bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const { | |||
32828 | return isInt<32>(Imm); | |||
32829 | } | |||
32830 | ||||
32831 | bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const { | |||
32832 | // Can also use sub to handle negated immediates. | |||
32833 | return isInt<32>(Imm); | |||
32834 | } | |||
32835 | ||||
32836 | bool X86TargetLowering::isLegalStoreImmediate(int64_t Imm) const { | |||
32837 | return isInt<32>(Imm); | |||
32838 | } | |||
32839 | ||||
32840 | bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const { | |||
32841 | if (!VT1.isScalarInteger() || !VT2.isScalarInteger()) | |||
32842 | return false; | |||
32843 | unsigned NumBits1 = VT1.getSizeInBits(); | |||
32844 | unsigned NumBits2 = VT2.getSizeInBits(); | |||
32845 | return NumBits1 > NumBits2; | |||
32846 | } | |||
32847 | ||||
32848 | bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const { | |||
32849 | // x86-64 implicitly zero-extends 32-bit results in 64-bit registers. | |||
32850 | return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget.is64Bit(); | |||
32851 | } | |||
32852 | ||||
32853 | bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const { | |||
32854 | // x86-64 implicitly zero-extends 32-bit results in 64-bit registers. | |||
32855 | return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget.is64Bit(); | |||
32856 | } | |||
32857 | ||||
32858 | bool X86TargetLowering::isZExtFree(SDValue Val, EVT VT2) const { | |||
32859 | EVT VT1 = Val.getValueType(); | |||
32860 | if (isZExtFree(VT1, VT2)) | |||
32861 | return true; | |||
32862 | ||||
32863 | if (Val.getOpcode() != ISD::LOAD) | |||
32864 | return false; | |||
32865 | ||||
32866 | if (!VT1.isSimple() || !VT1.isInteger() || | |||
32867 | !VT2.isSimple() || !VT2.isInteger()) | |||
32868 | return false; | |||
32869 | ||||
32870 | switch (VT1.getSimpleVT().SimpleTy) { | |||
32871 | default: break; | |||
32872 | case MVT::i8: | |||
32873 | case MVT::i16: | |||
32874 | case MVT::i32: | |||
32875 | // X86 has 8, 16, and 32-bit zero-extending loads. | |||
32876 | return true; | |||
32877 | } | |||
32878 | ||||
32879 | return false; | |||
32880 | } | |||
32881 | ||||
32882 | bool X86TargetLowering::shouldSinkOperands(Instruction *I, | |||
32883 | SmallVectorImpl<Use *> &Ops) const { | |||
32884 | using namespace llvm::PatternMatch; | |||
32885 | ||||
32886 | FixedVectorType *VTy = dyn_cast<FixedVectorType>(I->getType()); | |||
32887 | if (!VTy) | |||
32888 | return false; | |||
32889 | ||||
32890 | if (I->getOpcode() == Instruction::Mul && | |||
32891 | VTy->getElementType()->isIntegerTy(64)) { | |||
32892 | for (auto &Op : I->operands()) { | |||
32893 | // Make sure we are not already sinking this operand | |||
32894 | if (any_of(Ops, [&](Use *U) { return U->get() == Op; })) | |||
32895 | continue; | |||
32896 | ||||
32897 | // Look for PMULDQ pattern where the input is a sext_inreg from vXi32 or | |||
32898 | // the PMULUDQ pattern where the input is a zext_inreg from vXi32. | |||
32899 | if (Subtarget.hasSSE41() && | |||
32900 | match(Op.get(), m_AShr(m_Shl(m_Value(), m_SpecificInt(32)), | |||
32901 | m_SpecificInt(32)))) { | |||
32902 | Ops.push_back(&cast<Instruction>(Op)->getOperandUse(0)); | |||
32903 | Ops.push_back(&Op); | |||
32904 | } else if (Subtarget.hasSSE2() && | |||
32905 | match(Op.get(), | |||
32906 | m_And(m_Value(), m_SpecificInt(UINT64_C(0xffffffff)0xffffffffUL)))) { | |||
32907 | Ops.push_back(&Op); | |||
32908 | } | |||
32909 | } | |||
32910 | ||||
32911 | return !Ops.empty(); | |||
32912 | } | |||
32913 | ||||
32914 | // A uniform shift amount in a vector shift or funnel shift may be much | |||
32915 | // cheaper than a generic variable vector shift, so make that pattern visible | |||
32916 | // to SDAG by sinking the shuffle instruction next to the shift. | |||
32917 | int ShiftAmountOpNum = -1; | |||
32918 | if (I->isShift()) | |||
32919 | ShiftAmountOpNum = 1; | |||
32920 | else if (auto *II = dyn_cast<IntrinsicInst>(I)) { | |||
32921 | if (II->getIntrinsicID() == Intrinsic::fshl || | |||
32922 | II->getIntrinsicID() == Intrinsic::fshr) | |||
32923 | ShiftAmountOpNum = 2; | |||
32924 | } | |||
32925 | ||||
32926 | if (ShiftAmountOpNum == -1) | |||
32927 | return false; | |||
32928 | ||||
32929 | auto *Shuf = dyn_cast<ShuffleVectorInst>(I->getOperand(ShiftAmountOpNum)); | |||
32930 | if (Shuf && getSplatIndex(Shuf->getShuffleMask()) >= 0 && | |||
32931 | isVectorShiftByScalarCheap(I->getType())) { | |||
32932 | Ops.push_back(&I->getOperandUse(ShiftAmountOpNum)); | |||
32933 | return true; | |||
32934 | } | |||
32935 | ||||
32936 | return false; | |||
32937 | } | |||
32938 | ||||
32939 | bool X86TargetLowering::shouldConvertPhiType(Type *From, Type *To) const { | |||
32940 | if (!Subtarget.is64Bit()) | |||
32941 | return false; | |||
32942 | return TargetLowering::shouldConvertPhiType(From, To); | |||
32943 | } | |||
32944 | ||||
32945 | bool X86TargetLowering::isVectorLoadExtDesirable(SDValue ExtVal) const { | |||
32946 | if (isa<MaskedLoadSDNode>(ExtVal.getOperand(0))) | |||
32947 | return false; | |||
32948 | ||||
32949 | EVT SrcVT = ExtVal.getOperand(0).getValueType(); | |||
32950 | ||||
32951 | // There is no extending load for vXi1. | |||
32952 | if (SrcVT.getScalarType() == MVT::i1) | |||
32953 | return false; | |||
32954 | ||||
32955 | return true; | |||
32956 | } | |||
32957 | ||||
32958 | bool X86TargetLowering::isFMAFasterThanFMulAndFAdd(const MachineFunction &MF, | |||
32959 | EVT VT) const { | |||
32960 | if (!Subtarget.hasAnyFMA()) | |||
32961 | return false; | |||
32962 | ||||
32963 | VT = VT.getScalarType(); | |||
32964 | ||||
32965 | if (!VT.isSimple()) | |||
32966 | return false; | |||
32967 | ||||
32968 | switch (VT.getSimpleVT().SimpleTy) { | |||
32969 | case MVT::f16: | |||
32970 | return Subtarget.hasFP16(); | |||
32971 | case MVT::f32: | |||
32972 | case MVT::f64: | |||
32973 | return true; | |||
32974 | default: | |||
32975 | break; | |||
32976 | } | |||
32977 | ||||
32978 | return false; | |||
32979 | } | |||
32980 | ||||
32981 | bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const { | |||
32982 | // i16 instructions are longer (0x66 prefix) and potentially slower. | |||
32983 | return !(VT1 == MVT::i32 && VT2 == MVT::i16); | |||
32984 | } | |||
32985 | ||||
32986 | /// Targets can use this to indicate that they only support *some* | |||
32987 | /// VECTOR_SHUFFLE operations, those with specific masks. | |||
32988 | /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values | |||
32989 | /// are assumed to be legal. | |||
32990 | bool X86TargetLowering::isShuffleMaskLegal(ArrayRef<int> Mask, EVT VT) const { | |||
32991 | if (!VT.isSimple()) | |||
32992 | return false; | |||
32993 | ||||
32994 | // Not for i1 vectors | |||
32995 | if (VT.getSimpleVT().getScalarType() == MVT::i1) | |||
32996 | return false; | |||
32997 | ||||
32998 | // Very little shuffling can be done for 64-bit vectors right now. | |||
32999 | if (VT.getSimpleVT().getSizeInBits() == 64) | |||
33000 | return false; | |||
33001 | ||||
33002 | // We only care that the types being shuffled are legal. The lowering can | |||
33003 | // handle any possible shuffle mask that results. | |||
33004 | return isTypeLegal(VT.getSimpleVT()); | |||
33005 | } | |||
33006 | ||||
33007 | bool X86TargetLowering::isVectorClearMaskLegal(ArrayRef<int> Mask, | |||
33008 | EVT VT) const { | |||
33009 | // Don't convert an 'and' into a shuffle that we don't directly support. | |||
33010 | // vpblendw and vpshufb for 256-bit vectors are not available on AVX1. | |||
33011 | if (!Subtarget.hasAVX2()) | |||
33012 | if (VT == MVT::v32i8 || VT == MVT::v16i16) | |||
33013 | return false; | |||
33014 | ||||
33015 | // Just delegate to the generic legality, clear masks aren't special. | |||
33016 | return isShuffleMaskLegal(Mask, VT); | |||
33017 | } | |||
33018 | ||||
33019 | bool X86TargetLowering::areJTsAllowed(const Function *Fn) const { | |||
33020 | // If the subtarget is using thunks, we need to not generate jump tables. | |||
33021 | if (Subtarget.useIndirectThunkBranches()) | |||
33022 | return false; | |||
33023 | ||||
33024 | // Otherwise, fallback on the generic logic. | |||
33025 | return TargetLowering::areJTsAllowed(Fn); | |||
33026 | } | |||
33027 | ||||
33028 | //===----------------------------------------------------------------------===// | |||
33029 | // X86 Scheduler Hooks | |||
33030 | //===----------------------------------------------------------------------===// | |||
33031 | ||||
33032 | // Returns true if EFLAG is consumed after this iterator in the rest of the | |||
33033 | // basic block or any successors of the basic block. | |||
33034 | static bool isEFLAGSLiveAfter(MachineBasicBlock::iterator Itr, | |||
33035 | MachineBasicBlock *BB) { | |||
33036 | // Scan forward through BB for a use/def of EFLAGS. | |||
33037 | for (MachineBasicBlock::iterator miI = std::next(Itr), miE = BB->end(); | |||
33038 | miI != miE; ++miI) { | |||
33039 | const MachineInstr& mi = *miI; | |||
33040 | if (mi.readsRegister(X86::EFLAGS)) | |||
33041 | return true; | |||
33042 | // If we found a def, we can stop searching. | |||
33043 | if (mi.definesRegister(X86::EFLAGS)) | |||
33044 | return false; | |||
33045 | } | |||
33046 | ||||
33047 | // If we hit the end of the block, check whether EFLAGS is live into a | |||
33048 | // successor. | |||
33049 | for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(), | |||
33050 | sEnd = BB->succ_end(); | |||
33051 | sItr != sEnd; ++sItr) { | |||
33052 | MachineBasicBlock* succ = *sItr; | |||
33053 | if (succ->isLiveIn(X86::EFLAGS)) | |||
33054 | return true; | |||
33055 | } | |||
33056 | ||||
33057 | return false; | |||
33058 | } | |||
33059 | ||||
33060 | /// Utility function to emit xbegin specifying the start of an RTM region. | |||
33061 | static MachineBasicBlock *emitXBegin(MachineInstr &MI, MachineBasicBlock *MBB, | |||
33062 | const TargetInstrInfo *TII) { | |||
33063 | const DebugLoc &DL = MI.getDebugLoc(); | |||
33064 | ||||
33065 | const BasicBlock *BB = MBB->getBasicBlock(); | |||
33066 | MachineFunction::iterator I = ++MBB->getIterator(); | |||
33067 | ||||
33068 | // For the v = xbegin(), we generate | |||
33069 | // | |||
33070 | // thisMBB: | |||
33071 | // xbegin sinkMBB | |||
33072 | // | |||
33073 | // mainMBB: | |||
33074 | // s0 = -1 | |||
33075 | // | |||
33076 | // fallBB: | |||
33077 | // eax = # XABORT_DEF | |||
33078 | // s1 = eax | |||
33079 | // | |||
33080 | // sinkMBB: | |||
33081 | // v = phi(s0/mainBB, s1/fallBB) | |||
33082 | ||||
33083 | MachineBasicBlock *thisMBB = MBB; | |||
33084 | MachineFunction *MF = MBB->getParent(); | |||
33085 | MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB); | |||
33086 | MachineBasicBlock *fallMBB = MF->CreateMachineBasicBlock(BB); | |||
33087 | MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB); | |||
33088 | MF->insert(I, mainMBB); | |||
33089 | MF->insert(I, fallMBB); | |||
33090 | MF->insert(I, sinkMBB); | |||
33091 | ||||
33092 | if (isEFLAGSLiveAfter(MI, MBB)) { | |||
33093 | mainMBB->addLiveIn(X86::EFLAGS); | |||
33094 | fallMBB->addLiveIn(X86::EFLAGS); | |||
33095 | sinkMBB->addLiveIn(X86::EFLAGS); | |||
33096 | } | |||
33097 | ||||
33098 | // Transfer the remainder of BB and its successor edges to sinkMBB. | |||
33099 | sinkMBB->splice(sinkMBB->begin(), MBB, | |||
33100 | std::next(MachineBasicBlock::iterator(MI)), MBB->end()); | |||
33101 | sinkMBB->transferSuccessorsAndUpdatePHIs(MBB); | |||
33102 | ||||
33103 | MachineRegisterInfo &MRI = MF->getRegInfo(); | |||
33104 | Register DstReg = MI.getOperand(0).getReg(); | |||
33105 | const TargetRegisterClass *RC = MRI.getRegClass(DstReg); | |||
33106 | Register mainDstReg = MRI.createVirtualRegister(RC); | |||
33107 | Register fallDstReg = MRI.createVirtualRegister(RC); | |||
33108 | ||||
33109 | // thisMBB: | |||
33110 | // xbegin fallMBB | |||
33111 | // # fallthrough to mainMBB | |||
33112 | // # abortion to fallMBB | |||
33113 | BuildMI(thisMBB, DL, TII->get(X86::XBEGIN_4)).addMBB(fallMBB); | |||
33114 | thisMBB->addSuccessor(mainMBB); | |||
33115 | thisMBB->addSuccessor(fallMBB); | |||
33116 | ||||
33117 | // mainMBB: | |||
33118 | // mainDstReg := -1 | |||
33119 | BuildMI(mainMBB, DL, TII->get(X86::MOV32ri), mainDstReg).addImm(-1); | |||
33120 | BuildMI(mainMBB, DL, TII->get(X86::JMP_1)).addMBB(sinkMBB); | |||
33121 | mainMBB->addSuccessor(sinkMBB); | |||
33122 | ||||
33123 | // fallMBB: | |||
33124 | // ; pseudo instruction to model hardware's definition from XABORT | |||
33125 | // EAX := XABORT_DEF | |||
33126 | // fallDstReg := EAX | |||
33127 | BuildMI(fallMBB, DL, TII->get(X86::XABORT_DEF)); | |||
33128 | BuildMI(fallMBB, DL, TII->get(TargetOpcode::COPY), fallDstReg) | |||
33129 | .addReg(X86::EAX); | |||
33130 | fallMBB->addSuccessor(sinkMBB); | |||
33131 | ||||
33132 | // sinkMBB: | |||
33133 | // DstReg := phi(mainDstReg/mainBB, fallDstReg/fallBB) | |||
33134 | BuildMI(*sinkMBB, sinkMBB->begin(), DL, TII->get(X86::PHI), DstReg) | |||
33135 | .addReg(mainDstReg).addMBB(mainMBB) | |||
33136 | .addReg(fallDstReg).addMBB(fallMBB); | |||
33137 | ||||
33138 | MI.eraseFromParent(); | |||
33139 | return sinkMBB; | |||
33140 | } | |||
33141 | ||||
33142 | MachineBasicBlock * | |||
33143 | X86TargetLowering::EmitVAARGWithCustomInserter(MachineInstr &MI, | |||
33144 | MachineBasicBlock *MBB) const { | |||
33145 | // Emit va_arg instruction on X86-64. | |||
33146 | ||||
33147 | // Operands to this pseudo-instruction: | |||
33148 | // 0 ) Output : destination address (reg) | |||
33149 | // 1-5) Input : va_list address (addr, i64mem) | |||
33150 | // 6 ) ArgSize : Size (in bytes) of vararg type | |||
33151 | // 7 ) ArgMode : 0=overflow only, 1=use gp_offset, 2=use fp_offset | |||
33152 | // 8 ) Align : Alignment of type | |||
33153 | // 9 ) EFLAGS (implicit-def) | |||
33154 | ||||
33155 | assert(MI.getNumOperands() == 10 && "VAARG should have 10 operands!")(static_cast <bool> (MI.getNumOperands() == 10 && "VAARG should have 10 operands!") ? void (0) : __assert_fail ("MI.getNumOperands() == 10 && \"VAARG should have 10 operands!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 33155, __extension__ __PRETTY_FUNCTION__)); | |||
33156 | static_assert(X86::AddrNumOperands == 5, "VAARG assumes 5 address operands"); | |||
33157 | ||||
33158 | Register DestReg = MI.getOperand(0).getReg(); | |||
33159 | MachineOperand &Base = MI.getOperand(1); | |||
33160 | MachineOperand &Scale = MI.getOperand(2); | |||
33161 | MachineOperand &Index = MI.getOperand(3); | |||
33162 | MachineOperand &Disp = MI.getOperand(4); | |||
33163 | MachineOperand &Segment = MI.getOperand(5); | |||
33164 | unsigned ArgSize = MI.getOperand(6).getImm(); | |||
33165 | unsigned ArgMode = MI.getOperand(7).getImm(); | |||
33166 | Align Alignment = Align(MI.getOperand(8).getImm()); | |||
33167 | ||||
33168 | MachineFunction *MF = MBB->getParent(); | |||
33169 | ||||
33170 | // Memory Reference | |||
33171 | assert(MI.hasOneMemOperand() && "Expected VAARG to have one memoperand")(static_cast <bool> (MI.hasOneMemOperand() && "Expected VAARG to have one memoperand" ) ? void (0) : __assert_fail ("MI.hasOneMemOperand() && \"Expected VAARG to have one memoperand\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 33171, __extension__ __PRETTY_FUNCTION__)); | |||
33172 | ||||
33173 | MachineMemOperand *OldMMO = MI.memoperands().front(); | |||
33174 | ||||
33175 | // Clone the MMO into two separate MMOs for loading and storing | |||
33176 | MachineMemOperand *LoadOnlyMMO = MF->getMachineMemOperand( | |||
33177 | OldMMO, OldMMO->getFlags() & ~MachineMemOperand::MOStore); | |||
33178 | MachineMemOperand *StoreOnlyMMO = MF->getMachineMemOperand( | |||
33179 | OldMMO, OldMMO->getFlags() & ~MachineMemOperand::MOLoad); | |||
33180 | ||||
33181 | // Machine Information | |||
33182 | const TargetInstrInfo *TII = Subtarget.getInstrInfo(); | |||
33183 | MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo(); | |||
33184 | const TargetRegisterClass *AddrRegClass = | |||
33185 | getRegClassFor(getPointerTy(MBB->getParent()->getDataLayout())); | |||
33186 | const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32); | |||
33187 | const DebugLoc &DL = MI.getDebugLoc(); | |||
33188 | ||||
33189 | // struct va_list { | |||
33190 | // i32 gp_offset | |||
33191 | // i32 fp_offset | |||
33192 | // i64 overflow_area (address) | |||
33193 | // i64 reg_save_area (address) | |||
33194 | // } | |||
33195 | // sizeof(va_list) = 24 | |||
33196 | // alignment(va_list) = 8 | |||
33197 | ||||
33198 | unsigned TotalNumIntRegs = 6; | |||
33199 | unsigned TotalNumXMMRegs = 8; | |||
33200 | bool UseGPOffset = (ArgMode == 1); | |||
33201 | bool UseFPOffset = (ArgMode == 2); | |||
33202 | unsigned MaxOffset = TotalNumIntRegs * 8 + | |||
33203 | (UseFPOffset ? TotalNumXMMRegs * 16 : 0); | |||
33204 | ||||
33205 | /* Align ArgSize to a multiple of 8 */ | |||
33206 | unsigned ArgSizeA8 = (ArgSize + 7) & ~7; | |||
33207 | bool NeedsAlign = (Alignment > 8); | |||
33208 | ||||
33209 | MachineBasicBlock *thisMBB = MBB; | |||
33210 | MachineBasicBlock *overflowMBB; | |||
33211 | MachineBasicBlock *offsetMBB; | |||
33212 | MachineBasicBlock *endMBB; | |||
33213 | ||||
33214 | unsigned OffsetDestReg = 0; // Argument address computed by offsetMBB | |||
33215 | unsigned OverflowDestReg = 0; // Argument address computed by overflowMBB | |||
33216 | unsigned OffsetReg = 0; | |||
33217 | ||||
33218 | if (!UseGPOffset && !UseFPOffset) { | |||
33219 | // If we only pull from the overflow region, we don't create a branch. | |||
33220 | // We don't need to alter control flow. | |||
33221 | OffsetDestReg = 0; // unused | |||
33222 | OverflowDestReg = DestReg; | |||
33223 | ||||
33224 | offsetMBB = nullptr; | |||
33225 | overflowMBB = thisMBB; | |||
33226 | endMBB = thisMBB; | |||
33227 | } else { | |||
33228 | // First emit code to check if gp_offset (or fp_offset) is below the bound. | |||
33229 | // If so, pull the argument from reg_save_area. (branch to offsetMBB) | |||
33230 | // If not, pull from overflow_area. (branch to overflowMBB) | |||
33231 | // | |||
33232 | // thisMBB | |||
33233 | // | . | |||
33234 | // | . | |||
33235 | // offsetMBB overflowMBB | |||
33236 | // | . | |||
33237 | // | . | |||
33238 | // endMBB | |||
33239 | ||||
33240 | // Registers for the PHI in endMBB | |||
33241 | OffsetDestReg = MRI.createVirtualRegister(AddrRegClass); | |||
33242 | OverflowDestReg = MRI.createVirtualRegister(AddrRegClass); | |||
33243 | ||||
33244 | const BasicBlock *LLVM_BB = MBB->getBasicBlock(); | |||
33245 | overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB); | |||
33246 | offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB); | |||
33247 | endMBB = MF->CreateMachineBasicBlock(LLVM_BB); | |||
33248 | ||||
33249 | MachineFunction::iterator MBBIter = ++MBB->getIterator(); | |||
33250 | ||||
33251 | // Insert the new basic blocks | |||
33252 | MF->insert(MBBIter, offsetMBB); | |||
33253 | MF->insert(MBBIter, overflowMBB); | |||
33254 | MF->insert(MBBIter, endMBB); | |||
33255 | ||||
33256 | // Transfer the remainder of MBB and its successor edges to endMBB. | |||
33257 | endMBB->splice(endMBB->begin(), thisMBB, | |||
33258 | std::next(MachineBasicBlock::iterator(MI)), thisMBB->end()); | |||
33259 | endMBB->transferSuccessorsAndUpdatePHIs(thisMBB); | |||
33260 | ||||
33261 | // Make offsetMBB and overflowMBB successors of thisMBB | |||
33262 | thisMBB->addSuccessor(offsetMBB); | |||
33263 | thisMBB->addSuccessor(overflowMBB); | |||
33264 | ||||
33265 | // endMBB is a successor of both offsetMBB and overflowMBB | |||
33266 | offsetMBB->addSuccessor(endMBB); | |||
33267 | overflowMBB->addSuccessor(endMBB); | |||
33268 | ||||
33269 | // Load the offset value into a register | |||
33270 | OffsetReg = MRI.createVirtualRegister(OffsetRegClass); | |||
33271 | BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg) | |||
33272 | .add(Base) | |||
33273 | .add(Scale) | |||
33274 | .add(Index) | |||
33275 | .addDisp(Disp, UseFPOffset ? 4 : 0) | |||
33276 | .add(Segment) | |||
33277 | .setMemRefs(LoadOnlyMMO); | |||
33278 | ||||
33279 | // Check if there is enough room left to pull this argument. | |||
33280 | BuildMI(thisMBB, DL, TII->get(X86::CMP32ri)) | |||
33281 | .addReg(OffsetReg) | |||
33282 | .addImm(MaxOffset + 8 - ArgSizeA8); | |||
33283 | ||||
33284 | // Branch to "overflowMBB" if offset >= max | |||
33285 | // Fall through to "offsetMBB" otherwise | |||
33286 | BuildMI(thisMBB, DL, TII->get(X86::JCC_1)) | |||
33287 | .addMBB(overflowMBB).addImm(X86::COND_AE); | |||
33288 | } | |||
33289 | ||||
33290 | // In offsetMBB, emit code to use the reg_save_area. | |||
33291 | if (offsetMBB) { | |||
33292 | assert(OffsetReg != 0)(static_cast <bool> (OffsetReg != 0) ? void (0) : __assert_fail ("OffsetReg != 0", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 33292, __extension__ __PRETTY_FUNCTION__)); | |||
33293 | ||||
33294 | // Read the reg_save_area address. | |||
33295 | Register RegSaveReg = MRI.createVirtualRegister(AddrRegClass); | |||
33296 | BuildMI( | |||
33297 | offsetMBB, DL, | |||
33298 | TII->get(Subtarget.isTarget64BitLP64() ? X86::MOV64rm : X86::MOV32rm), | |||
33299 | RegSaveReg) | |||
33300 | .add(Base) | |||
33301 | .add(Scale) | |||
33302 | .add(Index) | |||
33303 | .addDisp(Disp, Subtarget.isTarget64BitLP64() ? 16 : 12) | |||
33304 | .add(Segment) | |||
33305 | .setMemRefs(LoadOnlyMMO); | |||
33306 | ||||
33307 | if (Subtarget.isTarget64BitLP64()) { | |||
33308 | // Zero-extend the offset | |||
33309 | Register OffsetReg64 = MRI.createVirtualRegister(AddrRegClass); | |||
33310 | BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64) | |||
33311 | .addImm(0) | |||
33312 | .addReg(OffsetReg) | |||
33313 | .addImm(X86::sub_32bit); | |||
33314 | ||||
33315 | // Add the offset to the reg_save_area to get the final address. | |||
33316 | BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg) | |||
33317 | .addReg(OffsetReg64) | |||
33318 | .addReg(RegSaveReg); | |||
33319 | } else { | |||
33320 | // Add the offset to the reg_save_area to get the final address. | |||
33321 | BuildMI(offsetMBB, DL, TII->get(X86::ADD32rr), OffsetDestReg) | |||
33322 | .addReg(OffsetReg) | |||
33323 | .addReg(RegSaveReg); | |||
33324 | } | |||
33325 | ||||
33326 | // Compute the offset for the next argument | |||
33327 | Register NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass); | |||
33328 | BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg) | |||
33329 | .addReg(OffsetReg) | |||
33330 | .addImm(UseFPOffset ? 16 : 8); | |||
33331 | ||||
33332 | // Store it back into the va_list. | |||
33333 | BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr)) | |||
33334 | .add(Base) | |||
33335 | .add(Scale) | |||
33336 | .add(Index) | |||
33337 | .addDisp(Disp, UseFPOffset ? 4 : 0) | |||
33338 | .add(Segment) | |||
33339 | .addReg(NextOffsetReg) | |||
33340 | .setMemRefs(StoreOnlyMMO); | |||
33341 | ||||
33342 | // Jump to endMBB | |||
33343 | BuildMI(offsetMBB, DL, TII->get(X86::JMP_1)) | |||
33344 | .addMBB(endMBB); | |||
33345 | } | |||
33346 | ||||
33347 | // | |||
33348 | // Emit code to use overflow area | |||
33349 | // | |||
33350 | ||||
33351 | // Load the overflow_area address into a register. | |||
33352 | Register OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass); | |||
33353 | BuildMI(overflowMBB, DL, | |||
33354 | TII->get(Subtarget.isTarget64BitLP64() ? X86::MOV64rm : X86::MOV32rm), | |||
33355 | OverflowAddrReg) | |||
33356 | .add(Base) | |||
33357 | .add(Scale) | |||
33358 | .add(Index) | |||
33359 | .addDisp(Disp, 8) | |||
33360 | .add(Segment) | |||
33361 | .setMemRefs(LoadOnlyMMO); | |||
33362 | ||||
33363 | // If we need to align it, do so. Otherwise, just copy the address | |||
33364 | // to OverflowDestReg. | |||
33365 | if (NeedsAlign) { | |||
33366 | // Align the overflow address | |||
33367 | Register TmpReg = MRI.createVirtualRegister(AddrRegClass); | |||
33368 | ||||
33369 | // aligned_addr = (addr + (align-1)) & ~(align-1) | |||
33370 | BuildMI( | |||
33371 | overflowMBB, DL, | |||
33372 | TII->get(Subtarget.isTarget64BitLP64() ? X86::ADD64ri32 : X86::ADD32ri), | |||
33373 | TmpReg) | |||
33374 | .addReg(OverflowAddrReg) | |||
33375 | .addImm(Alignment.value() - 1); | |||
33376 | ||||
33377 | BuildMI( | |||
33378 | overflowMBB, DL, | |||
33379 | TII->get(Subtarget.isTarget64BitLP64() ? X86::AND64ri32 : X86::AND32ri), | |||
33380 | OverflowDestReg) | |||
33381 | .addReg(TmpReg) | |||
33382 | .addImm(~(uint64_t)(Alignment.value() - 1)); | |||
33383 | } else { | |||
33384 | BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg) | |||
33385 | .addReg(OverflowAddrReg); | |||
33386 | } | |||
33387 | ||||
33388 | // Compute the next overflow address after this argument. | |||
33389 | // (the overflow address should be kept 8-byte aligned) | |||
33390 | Register NextAddrReg = MRI.createVirtualRegister(AddrRegClass); | |||
33391 | BuildMI( | |||
33392 | overflowMBB, DL, | |||
33393 | TII->get(Subtarget.isTarget64BitLP64() ? X86::ADD64ri32 : X86::ADD32ri), | |||
33394 | NextAddrReg) | |||
33395 | .addReg(OverflowDestReg) | |||
33396 | .addImm(ArgSizeA8); | |||
33397 | ||||
33398 | // Store the new overflow address. | |||
33399 | BuildMI(overflowMBB, DL, | |||
33400 | TII->get(Subtarget.isTarget64BitLP64() ? X86::MOV64mr : X86::MOV32mr)) | |||
33401 | .add(Base) | |||
33402 | .add(Scale) | |||
33403 | .add(Index) | |||
33404 | .addDisp(Disp, 8) | |||
33405 | .add(Segment) | |||
33406 | .addReg(NextAddrReg) | |||
33407 | .setMemRefs(StoreOnlyMMO); | |||
33408 | ||||
33409 | // If we branched, emit the PHI to the front of endMBB. | |||
33410 | if (offsetMBB) { | |||
33411 | BuildMI(*endMBB, endMBB->begin(), DL, | |||
33412 | TII->get(X86::PHI), DestReg) | |||
33413 | .addReg(OffsetDestReg).addMBB(offsetMBB) | |||
33414 | .addReg(OverflowDestReg).addMBB(overflowMBB); | |||
33415 | } | |||
33416 | ||||
33417 | // Erase the pseudo instruction | |||
33418 | MI.eraseFromParent(); | |||
33419 | ||||
33420 | return endMBB; | |||
33421 | } | |||
33422 | ||||
33423 | // The EFLAGS operand of SelectItr might be missing a kill marker | |||
33424 | // because there were multiple uses of EFLAGS, and ISel didn't know | |||
33425 | // which to mark. Figure out whether SelectItr should have had a | |||
33426 | // kill marker, and set it if it should. Returns the correct kill | |||
33427 | // marker value. | |||
33428 | static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr, | |||
33429 | MachineBasicBlock* BB, | |||
33430 | const TargetRegisterInfo* TRI) { | |||
33431 | if (isEFLAGSLiveAfter(SelectItr, BB)) | |||
33432 | return false; | |||
33433 | ||||
33434 | // We found a def, or hit the end of the basic block and EFLAGS wasn't live | |||
33435 | // out. SelectMI should have a kill flag on EFLAGS. | |||
33436 | SelectItr->addRegisterKilled(X86::EFLAGS, TRI); | |||
33437 | return true; | |||
33438 | } | |||
33439 | ||||
33440 | // Return true if it is OK for this CMOV pseudo-opcode to be cascaded | |||
33441 | // together with other CMOV pseudo-opcodes into a single basic-block with | |||
33442 | // conditional jump around it. | |||
33443 | static bool isCMOVPseudo(MachineInstr &MI) { | |||
33444 | switch (MI.getOpcode()) { | |||
33445 | case X86::CMOV_FR16X: | |||
33446 | case X86::CMOV_FR32: | |||
33447 | case X86::CMOV_FR32X: | |||
33448 | case X86::CMOV_FR64: | |||
33449 | case X86::CMOV_FR64X: | |||
33450 | case X86::CMOV_GR8: | |||
33451 | case X86::CMOV_GR16: | |||
33452 | case X86::CMOV_GR32: | |||
33453 | case X86::CMOV_RFP32: | |||
33454 | case X86::CMOV_RFP64: | |||
33455 | case X86::CMOV_RFP80: | |||
33456 | case X86::CMOV_VR64: | |||
33457 | case X86::CMOV_VR128: | |||
33458 | case X86::CMOV_VR128X: | |||
33459 | case X86::CMOV_VR256: | |||
33460 | case X86::CMOV_VR256X: | |||
33461 | case X86::CMOV_VR512: | |||
33462 | case X86::CMOV_VK1: | |||
33463 | case X86::CMOV_VK2: | |||
33464 | case X86::CMOV_VK4: | |||
33465 | case X86::CMOV_VK8: | |||
33466 | case X86::CMOV_VK16: | |||
33467 | case X86::CMOV_VK32: | |||
33468 | case X86::CMOV_VK64: | |||
33469 | return true; | |||
33470 | ||||
33471 | default: | |||
33472 | return false; | |||
33473 | } | |||
33474 | } | |||
33475 | ||||
33476 | // Helper function, which inserts PHI functions into SinkMBB: | |||
33477 | // %Result(i) = phi [ %FalseValue(i), FalseMBB ], [ %TrueValue(i), TrueMBB ], | |||
33478 | // where %FalseValue(i) and %TrueValue(i) are taken from the consequent CMOVs | |||
33479 | // in [MIItBegin, MIItEnd) range. It returns the last MachineInstrBuilder for | |||
33480 | // the last PHI function inserted. | |||
33481 | static MachineInstrBuilder createPHIsForCMOVsInSinkBB( | |||
33482 | MachineBasicBlock::iterator MIItBegin, MachineBasicBlock::iterator MIItEnd, | |||
33483 | MachineBasicBlock *TrueMBB, MachineBasicBlock *FalseMBB, | |||
33484 | MachineBasicBlock *SinkMBB) { | |||
33485 | MachineFunction *MF = TrueMBB->getParent(); | |||
33486 | const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo(); | |||
33487 | const DebugLoc &DL = MIItBegin->getDebugLoc(); | |||
33488 | ||||
33489 | X86::CondCode CC = X86::CondCode(MIItBegin->getOperand(3).getImm()); | |||
33490 | X86::CondCode OppCC = X86::GetOppositeBranchCondition(CC); | |||
33491 | ||||
33492 | MachineBasicBlock::iterator SinkInsertionPoint = SinkMBB->begin(); | |||
33493 | ||||
33494 | // As we are creating the PHIs, we have to be careful if there is more than | |||
33495 | // one. Later CMOVs may reference the results of earlier CMOVs, but later | |||
33496 | // PHIs have to reference the individual true/false inputs from earlier PHIs. | |||
33497 | // That also means that PHI construction must work forward from earlier to | |||
33498 | // later, and that the code must maintain a mapping from earlier PHI's | |||
33499 | // destination registers, and the registers that went into the PHI. | |||
33500 | DenseMap<unsigned, std::pair<unsigned, unsigned>> RegRewriteTable; | |||
33501 | MachineInstrBuilder MIB; | |||
33502 | ||||
33503 | for (MachineBasicBlock::iterator MIIt = MIItBegin; MIIt != MIItEnd; ++MIIt) { | |||
33504 | Register DestReg = MIIt->getOperand(0).getReg(); | |||
33505 | Register Op1Reg = MIIt->getOperand(1).getReg(); | |||
33506 | Register Op2Reg = MIIt->getOperand(2).getReg(); | |||
33507 | ||||
33508 | // If this CMOV we are generating is the opposite condition from | |||
33509 | // the jump we generated, then we have to swap the operands for the | |||
33510 | // PHI that is going to be generated. | |||
33511 | if (MIIt->getOperand(3).getImm() == OppCC) | |||
33512 | std::swap(Op1Reg, Op2Reg); | |||
33513 | ||||
33514 | if (RegRewriteTable.find(Op1Reg) != RegRewriteTable.end()) | |||
33515 | Op1Reg = RegRewriteTable[Op1Reg].first; | |||
33516 | ||||
33517 | if (RegRewriteTable.find(Op2Reg) != RegRewriteTable.end()) | |||
33518 | Op2Reg = RegRewriteTable[Op2Reg].second; | |||
33519 | ||||
33520 | MIB = BuildMI(*SinkMBB, SinkInsertionPoint, DL, TII->get(X86::PHI), DestReg) | |||
33521 | .addReg(Op1Reg) | |||
33522 | .addMBB(FalseMBB) | |||
33523 | .addReg(Op2Reg) | |||
33524 | .addMBB(TrueMBB); | |||
33525 | ||||
33526 | // Add this PHI to the rewrite table. | |||
33527 | RegRewriteTable[DestReg] = std::make_pair(Op1Reg, Op2Reg); | |||
33528 | } | |||
33529 | ||||
33530 | return MIB; | |||
33531 | } | |||
33532 | ||||
33533 | // Lower cascaded selects in form of (SecondCmov (FirstCMOV F, T, cc1), T, cc2). | |||
33534 | MachineBasicBlock * | |||
33535 | X86TargetLowering::EmitLoweredCascadedSelect(MachineInstr &FirstCMOV, | |||
33536 | MachineInstr &SecondCascadedCMOV, | |||
33537 | MachineBasicBlock *ThisMBB) const { | |||
33538 | const TargetInstrInfo *TII = Subtarget.getInstrInfo(); | |||
33539 | const DebugLoc &DL = FirstCMOV.getDebugLoc(); | |||
33540 | ||||
33541 | // We lower cascaded CMOVs such as | |||
33542 | // | |||
33543 | // (SecondCascadedCMOV (FirstCMOV F, T, cc1), T, cc2) | |||
33544 | // | |||
33545 | // to two successive branches. | |||
33546 | // | |||
33547 | // Without this, we would add a PHI between the two jumps, which ends up | |||
33548 | // creating a few copies all around. For instance, for | |||
33549 | // | |||
33550 | // (sitofp (zext (fcmp une))) | |||
33551 | // | |||
33552 | // we would generate: | |||
33553 | // | |||
33554 | // ucomiss %xmm1, %xmm0 | |||
33555 | // movss <1.0f>, %xmm0 | |||
33556 | // movaps %xmm0, %xmm1 | |||
33557 | // jne .LBB5_2 | |||
33558 | // xorps %xmm1, %xmm1 | |||
33559 | // .LBB5_2: | |||
33560 | // jp .LBB5_4 | |||
33561 | // movaps %xmm1, %xmm0 | |||
33562 | // .LBB5_4: | |||
33563 | // retq | |||
33564 | // | |||
33565 | // because this custom-inserter would have generated: | |||
33566 | // | |||
33567 | // A | |||
33568 | // | \ | |||
33569 | // | B | |||
33570 | // | / | |||
33571 | // C | |||
33572 | // | \ | |||
33573 | // | D | |||
33574 | // | / | |||
33575 | // E | |||
33576 | // | |||
33577 | // A: X = ...; Y = ... | |||
33578 | // B: empty | |||
33579 | // C: Z = PHI [X, A], [Y, B] | |||
33580 | // D: empty | |||
33581 | // E: PHI [X, C], [Z, D] | |||
33582 | // | |||
33583 | // If we lower both CMOVs in a single step, we can instead generate: | |||
33584 | // | |||
33585 | // A | |||
33586 | // | \ | |||
33587 | // | C | |||
33588 | // | /| | |||
33589 | // |/ | | |||
33590 | // | | | |||
33591 | // | D | |||
33592 | // | / | |||
33593 | // E | |||
33594 | // | |||
33595 | // A: X = ...; Y = ... | |||
33596 | // D: empty | |||
33597 | // E: PHI [X, A], [X, C], [Y, D] | |||
33598 | // | |||
33599 | // Which, in our sitofp/fcmp example, gives us something like: | |||
33600 | // | |||
33601 | // ucomiss %xmm1, %xmm0 | |||
33602 | // movss <1.0f>, %xmm0 | |||
33603 | // jne .LBB5_4 | |||
33604 | // jp .LBB5_4 | |||
33605 | // xorps %xmm0, %xmm0 | |||
33606 | // .LBB5_4: | |||
33607 | // retq | |||
33608 | // | |||
33609 | ||||
33610 | // We lower cascaded CMOV into two successive branches to the same block. | |||
33611 | // EFLAGS is used by both, so mark it as live in the second. | |||
33612 | const BasicBlock *LLVM_BB = ThisMBB->getBasicBlock(); | |||
33613 | MachineFunction *F = ThisMBB->getParent(); | |||
33614 | MachineBasicBlock *FirstInsertedMBB = F->CreateMachineBasicBlock(LLVM_BB); | |||
33615 | MachineBasicBlock *SecondInsertedMBB = F->CreateMachineBasicBlock(LLVM_BB); | |||
33616 | MachineBasicBlock *SinkMBB = F->CreateMachineBasicBlock(LLVM_BB); | |||
33617 | ||||
33618 | MachineFunction::iterator It = ++ThisMBB->getIterator(); | |||
33619 | F->insert(It, FirstInsertedMBB); | |||
33620 | F->insert(It, SecondInsertedMBB); | |||
33621 | F->insert(It, SinkMBB); | |||
33622 | ||||
33623 | // For a cascaded CMOV, we lower it to two successive branches to | |||
33624 | // the same block (SinkMBB). EFLAGS is used by both, so mark it as live in | |||
33625 | // the FirstInsertedMBB. | |||
33626 | FirstInsertedMBB->addLiveIn(X86::EFLAGS); | |||
33627 | ||||
33628 | // If the EFLAGS register isn't dead in the terminator, then claim that it's | |||
33629 | // live into the sink and copy blocks. | |||
33630 | const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo(); | |||
33631 | if (!SecondCascadedCMOV.killsRegister(X86::EFLAGS) && | |||
33632 | !checkAndUpdateEFLAGSKill(SecondCascadedCMOV, ThisMBB, TRI)) { | |||
33633 | SecondInsertedMBB->addLiveIn(X86::EFLAGS); | |||
33634 | SinkMBB->addLiveIn(X86::EFLAGS); | |||
33635 | } | |||
33636 | ||||
33637 | // Transfer the remainder of ThisMBB and its successor edges to SinkMBB. | |||
33638 | SinkMBB->splice(SinkMBB->begin(), ThisMBB, | |||
33639 | std::next(MachineBasicBlock::iterator(FirstCMOV)), | |||
33640 | ThisMBB->end()); | |||
33641 | SinkMBB->transferSuccessorsAndUpdatePHIs(ThisMBB); | |||
33642 | ||||
33643 | // Fallthrough block for ThisMBB. | |||
33644 | ThisMBB->addSuccessor(FirstInsertedMBB); | |||
33645 | // The true block target of the first branch is always SinkMBB. | |||
33646 | ThisMBB->addSuccessor(SinkMBB); | |||
33647 | // Fallthrough block for FirstInsertedMBB. | |||
33648 | FirstInsertedMBB->addSuccessor(SecondInsertedMBB); | |||
33649 | // The true block for the branch of FirstInsertedMBB. | |||
33650 | FirstInsertedMBB->addSuccessor(SinkMBB); | |||
33651 | // This is fallthrough. | |||
33652 | SecondInsertedMBB->addSuccessor(SinkMBB); | |||
33653 | ||||
33654 | // Create the conditional branch instructions. | |||
33655 | X86::CondCode FirstCC = X86::CondCode(FirstCMOV.getOperand(3).getImm()); | |||
33656 | BuildMI(ThisMBB, DL, TII->get(X86::JCC_1)).addMBB(SinkMBB).addImm(FirstCC); | |||
33657 | ||||
33658 | X86::CondCode SecondCC = | |||
33659 | X86::CondCode(SecondCascadedCMOV.getOperand(3).getImm()); | |||
33660 | BuildMI(FirstInsertedMBB, DL, TII->get(X86::JCC_1)).addMBB(SinkMBB).addImm(SecondCC); | |||
33661 | ||||
33662 | // SinkMBB: | |||
33663 | // %Result = phi [ %FalseValue, SecondInsertedMBB ], [ %TrueValue, ThisMBB ] | |||
33664 | Register DestReg = FirstCMOV.getOperand(0).getReg(); | |||
33665 | Register Op1Reg = FirstCMOV.getOperand(1).getReg(); | |||
33666 | Register Op2Reg = FirstCMOV.getOperand(2).getReg(); | |||
33667 | MachineInstrBuilder MIB = | |||
33668 | BuildMI(*SinkMBB, SinkMBB->begin(), DL, TII->get(X86::PHI), DestReg) | |||
33669 | .addReg(Op1Reg) | |||
33670 | .addMBB(SecondInsertedMBB) | |||
33671 | .addReg(Op2Reg) | |||
33672 | .addMBB(ThisMBB); | |||
33673 | ||||
33674 | // The second SecondInsertedMBB provides the same incoming value as the | |||
33675 | // FirstInsertedMBB (the True operand of the SELECT_CC/CMOV nodes). | |||
33676 | MIB.addReg(FirstCMOV.getOperand(2).getReg()).addMBB(FirstInsertedMBB); | |||
33677 | // Copy the PHI result to the register defined by the second CMOV. | |||
33678 | BuildMI(*SinkMBB, std::next(MachineBasicBlock::iterator(MIB.getInstr())), DL, | |||
33679 | TII->get(TargetOpcode::COPY), | |||
33680 | SecondCascadedCMOV.getOperand(0).getReg()) | |||
33681 | .addReg(FirstCMOV.getOperand(0).getReg()); | |||
33682 | ||||
33683 | // Now remove the CMOVs. | |||
33684 | FirstCMOV.eraseFromParent(); | |||
33685 | SecondCascadedCMOV.eraseFromParent(); | |||
33686 | ||||
33687 | return SinkMBB; | |||
33688 | } | |||
33689 | ||||
33690 | MachineBasicBlock * | |||
33691 | X86TargetLowering::EmitLoweredSelect(MachineInstr &MI, | |||
33692 | MachineBasicBlock *ThisMBB) const { | |||
33693 | const TargetInstrInfo *TII = Subtarget.getInstrInfo(); | |||
33694 | const DebugLoc &DL = MI.getDebugLoc(); | |||
33695 | ||||
33696 | // To "insert" a SELECT_CC instruction, we actually have to insert the | |||
33697 | // diamond control-flow pattern. The incoming instruction knows the | |||
33698 | // destination vreg to set, the condition code register to branch on, the | |||
33699 | // true/false values to select between and a branch opcode to use. | |||
33700 | ||||
33701 | // ThisMBB: | |||
33702 | // ... | |||
33703 | // TrueVal = ... | |||
33704 | // cmpTY ccX, r1, r2 | |||
33705 | // bCC copy1MBB | |||
33706 | // fallthrough --> FalseMBB | |||
33707 | ||||
33708 | // This code lowers all pseudo-CMOV instructions. Generally it lowers these | |||
33709 | // as described above, by inserting a BB, and then making a PHI at the join | |||
33710 | // point to select the true and false operands of the CMOV in the PHI. | |||
33711 | // | |||
33712 | // The code also handles two different cases of multiple CMOV opcodes | |||
33713 | // in a row. | |||
33714 | // | |||
33715 | // Case 1: | |||
33716 | // In this case, there are multiple CMOVs in a row, all which are based on | |||
33717 | // the same condition setting (or the exact opposite condition setting). | |||
33718 | // In this case we can lower all the CMOVs using a single inserted BB, and | |||
33719 | // then make a number of PHIs at the join point to model the CMOVs. The only | |||
33720 | // trickiness here, is that in a case like: | |||
33721 | // | |||
33722 | // t2 = CMOV cond1 t1, f1 | |||
33723 | // t3 = CMOV cond1 t2, f2 | |||
33724 | // | |||
33725 | // when rewriting this into PHIs, we have to perform some renaming on the | |||
33726 | // temps since you cannot have a PHI operand refer to a PHI result earlier | |||
33727 | // in the same block. The "simple" but wrong lowering would be: | |||
33728 | // | |||
33729 | // t2 = PHI t1(BB1), f1(BB2) | |||
33730 | // t3 = PHI t2(BB1), f2(BB2) | |||
33731 | // | |||
33732 | // but clearly t2 is not defined in BB1, so that is incorrect. The proper | |||
33733 | // renaming is to note that on the path through BB1, t2 is really just a | |||
33734 | // copy of t1, and do that renaming, properly generating: | |||
33735 | // | |||
33736 | // t2 = PHI t1(BB1), f1(BB2) | |||
33737 | // t3 = PHI t1(BB1), f2(BB2) | |||
33738 | // | |||
33739 | // Case 2: | |||
33740 | // CMOV ((CMOV F, T, cc1), T, cc2) is checked here and handled by a separate | |||
33741 | // function - EmitLoweredCascadedSelect. | |||
33742 | ||||
33743 | X86::CondCode CC = X86::CondCode(MI.getOperand(3).getImm()); | |||
33744 | X86::CondCode OppCC = X86::GetOppositeBranchCondition(CC); | |||
33745 | MachineInstr *LastCMOV = &MI; | |||
33746 | MachineBasicBlock::iterator NextMIIt = MachineBasicBlock::iterator(MI); | |||
33747 | ||||
33748 | // Check for case 1, where there are multiple CMOVs with the same condition | |||
33749 | // first. Of the two cases of multiple CMOV lowerings, case 1 reduces the | |||
33750 | // number of jumps the most. | |||
33751 | ||||
33752 | if (isCMOVPseudo(MI)) { | |||
33753 | // See if we have a string of CMOVS with the same condition. Skip over | |||
33754 | // intervening debug insts. | |||
33755 | while (NextMIIt != ThisMBB->end() && isCMOVPseudo(*NextMIIt) && | |||
33756 | (NextMIIt->getOperand(3).getImm() == CC || | |||
33757 | NextMIIt->getOperand(3).getImm() == OppCC)) { | |||
33758 | LastCMOV = &*NextMIIt; | |||
33759 | NextMIIt = next_nodbg(NextMIIt, ThisMBB->end()); | |||
33760 | } | |||
33761 | } | |||
33762 | ||||
33763 | // This checks for case 2, but only do this if we didn't already find | |||
33764 | // case 1, as indicated by LastCMOV == MI. | |||
33765 | if (LastCMOV == &MI && NextMIIt != ThisMBB->end() && | |||
33766 | NextMIIt->getOpcode() == MI.getOpcode() && | |||
33767 | NextMIIt->getOperand(2).getReg() == MI.getOperand(2).getReg() && | |||
33768 | NextMIIt->getOperand(1).getReg() == MI.getOperand(0).getReg() && | |||
33769 | NextMIIt->getOperand(1).isKill()) { | |||
33770 | return EmitLoweredCascadedSelect(MI, *NextMIIt, ThisMBB); | |||
33771 | } | |||
33772 | ||||
33773 | const BasicBlock *LLVM_BB = ThisMBB->getBasicBlock(); | |||
33774 | MachineFunction *F = ThisMBB->getParent(); | |||
33775 | MachineBasicBlock *FalseMBB = F->CreateMachineBasicBlock(LLVM_BB); | |||
33776 | MachineBasicBlock *SinkMBB = F->CreateMachineBasicBlock(LLVM_BB); | |||
33777 | ||||
33778 | MachineFunction::iterator It = ++ThisMBB->getIterator(); | |||
33779 | F->insert(It, FalseMBB); | |||
33780 | F->insert(It, SinkMBB); | |||
33781 | ||||
33782 | // If the EFLAGS register isn't dead in the terminator, then claim that it's | |||
33783 | // live into the sink and copy blocks. | |||
33784 | const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo(); | |||
33785 | if (!LastCMOV->killsRegister(X86::EFLAGS) && | |||
33786 | !checkAndUpdateEFLAGSKill(LastCMOV, ThisMBB, TRI)) { | |||
33787 | FalseMBB->addLiveIn(X86::EFLAGS); | |||
33788 | SinkMBB->addLiveIn(X86::EFLAGS); | |||
33789 | } | |||
33790 | ||||
33791 | // Transfer any debug instructions inside the CMOV sequence to the sunk block. | |||
33792 | auto DbgEnd = MachineBasicBlock::iterator(LastCMOV); | |||
33793 | auto DbgIt = MachineBasicBlock::iterator(MI); | |||
33794 | while (DbgIt != DbgEnd) { | |||
33795 | auto Next = std::next(DbgIt); | |||
33796 | if (DbgIt->isDebugInstr()) | |||
33797 | SinkMBB->push_back(DbgIt->removeFromParent()); | |||
33798 | DbgIt = Next; | |||
33799 | } | |||
33800 | ||||
33801 | // Transfer the remainder of ThisMBB and its successor edges to SinkMBB. | |||
33802 | SinkMBB->splice(SinkMBB->end(), ThisMBB, | |||
33803 | std::next(MachineBasicBlock::iterator(LastCMOV)), | |||
33804 | ThisMBB->end()); | |||
33805 | SinkMBB->transferSuccessorsAndUpdatePHIs(ThisMBB); | |||
33806 | ||||
33807 | // Fallthrough block for ThisMBB. | |||
33808 | ThisMBB->addSuccessor(FalseMBB); | |||
33809 | // The true block target of the first (or only) branch is always a SinkMBB. | |||
33810 | ThisMBB->addSuccessor(SinkMBB); | |||
33811 | // Fallthrough block for FalseMBB. | |||
33812 | FalseMBB->addSuccessor(SinkMBB); | |||
33813 | ||||
33814 | // Create the conditional branch instruction. | |||
33815 | BuildMI(ThisMBB, DL, TII->get(X86::JCC_1)).addMBB(SinkMBB).addImm(CC); | |||
33816 | ||||
33817 | // SinkMBB: | |||
33818 | // %Result = phi [ %FalseValue, FalseMBB ], [ %TrueValue, ThisMBB ] | |||
33819 | // ... | |||
33820 | MachineBasicBlock::iterator MIItBegin = MachineBasicBlock::iterator(MI); | |||
33821 | MachineBasicBlock::iterator MIItEnd = | |||
33822 | std::next(MachineBasicBlock::iterator(LastCMOV)); | |||
33823 | createPHIsForCMOVsInSinkBB(MIItBegin, MIItEnd, ThisMBB, FalseMBB, SinkMBB); | |||
33824 | ||||
33825 | // Now remove the CMOV(s). | |||
33826 | ThisMBB->erase(MIItBegin, MIItEnd); | |||
33827 | ||||
33828 | return SinkMBB; | |||
33829 | } | |||
33830 | ||||
33831 | static unsigned getSUBriOpcode(bool IsLP64, int64_t Imm) { | |||
33832 | if (IsLP64) { | |||
33833 | if (isInt<8>(Imm)) | |||
33834 | return X86::SUB64ri8; | |||
33835 | return X86::SUB64ri32; | |||
33836 | } else { | |||
33837 | if (isInt<8>(Imm)) | |||
33838 | return X86::SUB32ri8; | |||
33839 | return X86::SUB32ri; | |||
33840 | } | |||
33841 | } | |||
33842 | ||||
33843 | MachineBasicBlock * | |||
33844 | X86TargetLowering::EmitLoweredProbedAlloca(MachineInstr &MI, | |||
33845 | MachineBasicBlock *MBB) const { | |||
33846 | MachineFunction *MF = MBB->getParent(); | |||
33847 | const TargetInstrInfo *TII = Subtarget.getInstrInfo(); | |||
33848 | const X86FrameLowering &TFI = *Subtarget.getFrameLowering(); | |||
33849 | const DebugLoc &DL = MI.getDebugLoc(); | |||
33850 | const BasicBlock *LLVM_BB = MBB->getBasicBlock(); | |||
33851 | ||||
33852 | const unsigned ProbeSize = getStackProbeSize(*MF); | |||
33853 | ||||
33854 | MachineRegisterInfo &MRI = MF->getRegInfo(); | |||
33855 | MachineBasicBlock *testMBB = MF->CreateMachineBasicBlock(LLVM_BB); | |||
33856 | MachineBasicBlock *tailMBB = MF->CreateMachineBasicBlock(LLVM_BB); | |||
33857 | MachineBasicBlock *blockMBB = MF->CreateMachineBasicBlock(LLVM_BB); | |||
33858 | ||||
33859 | MachineFunction::iterator MBBIter = ++MBB->getIterator(); | |||
33860 | MF->insert(MBBIter, testMBB); | |||
33861 | MF->insert(MBBIter, blockMBB); | |||
33862 | MF->insert(MBBIter, tailMBB); | |||
33863 | ||||
33864 | Register sizeVReg = MI.getOperand(1).getReg(); | |||
33865 | ||||
33866 | Register physSPReg = TFI.Uses64BitFramePtr ? X86::RSP : X86::ESP; | |||
33867 | ||||
33868 | Register TmpStackPtr = MRI.createVirtualRegister( | |||
33869 | TFI.Uses64BitFramePtr ? &X86::GR64RegClass : &X86::GR32RegClass); | |||
33870 | Register FinalStackPtr = MRI.createVirtualRegister( | |||
33871 | TFI.Uses64BitFramePtr ? &X86::GR64RegClass : &X86::GR32RegClass); | |||
33872 | ||||
33873 | BuildMI(*MBB, {MI}, DL, TII->get(TargetOpcode::COPY), TmpStackPtr) | |||
33874 | .addReg(physSPReg); | |||
33875 | { | |||
33876 | const unsigned Opc = TFI.Uses64BitFramePtr ? X86::SUB64rr : X86::SUB32rr; | |||
33877 | BuildMI(*MBB, {MI}, DL, TII->get(Opc), FinalStackPtr) | |||
33878 | .addReg(TmpStackPtr) | |||
33879 | .addReg(sizeVReg); | |||
33880 | } | |||
33881 | ||||
33882 | // test rsp size | |||
33883 | ||||
33884 | BuildMI(testMBB, DL, | |||
33885 | TII->get(TFI.Uses64BitFramePtr ? X86::CMP64rr : X86::CMP32rr)) | |||
33886 | .addReg(FinalStackPtr) | |||
33887 | .addReg(physSPReg); | |||
33888 | ||||
33889 | BuildMI(testMBB, DL, TII->get(X86::JCC_1)) | |||
33890 | .addMBB(tailMBB) | |||
33891 | .addImm(X86::COND_GE); | |||
33892 | testMBB->addSuccessor(blockMBB); | |||
33893 | testMBB->addSuccessor(tailMBB); | |||
33894 | ||||
33895 | // Touch the block then extend it. This is done on the opposite side of | |||
33896 | // static probe where we allocate then touch, to avoid the need of probing the | |||
33897 | // tail of the static alloca. Possible scenarios are: | |||
33898 | // | |||
33899 | // + ---- <- ------------ <- ------------- <- ------------ + | |||
33900 | // | | | |||
33901 | // [free probe] -> [page alloc] -> [alloc probe] -> [tail alloc] + -> [dyn probe] -> [page alloc] -> [dyn probe] -> [tail alloc] + | |||
33902 | // | | | |||
33903 | // + <- ----------- <- ------------ <- ----------- <- ------------ + | |||
33904 | // | |||
33905 | // The property we want to enforce is to never have more than [page alloc] between two probes. | |||
33906 | ||||
33907 | const unsigned XORMIOpc = | |||
33908 | TFI.Uses64BitFramePtr ? X86::XOR64mi8 : X86::XOR32mi8; | |||
33909 | addRegOffset(BuildMI(blockMBB, DL, TII->get(XORMIOpc)), physSPReg, false, 0) | |||
33910 | .addImm(0); | |||
33911 | ||||
33912 | BuildMI(blockMBB, DL, | |||
33913 | TII->get(getSUBriOpcode(TFI.Uses64BitFramePtr, ProbeSize)), physSPReg) | |||
33914 | .addReg(physSPReg) | |||
33915 | .addImm(ProbeSize); | |||
33916 | ||||
33917 | ||||
33918 | BuildMI(blockMBB, DL, TII->get(X86::JMP_1)).addMBB(testMBB); | |||
33919 | blockMBB->addSuccessor(testMBB); | |||
33920 | ||||
33921 | // Replace original instruction by the expected stack ptr | |||
33922 | BuildMI(tailMBB, DL, TII->get(TargetOpcode::COPY), MI.getOperand(0).getReg()) | |||
33923 | .addReg(FinalStackPtr); | |||
33924 | ||||
33925 | tailMBB->splice(tailMBB->end(), MBB, | |||
33926 | std::next(MachineBasicBlock::iterator(MI)), MBB->end()); | |||
33927 | tailMBB->transferSuccessorsAndUpdatePHIs(MBB); | |||
33928 | MBB->addSuccessor(testMBB); | |||
33929 | ||||
33930 | // Delete the original pseudo instruction. | |||
33931 | MI.eraseFromParent(); | |||
33932 | ||||
33933 | // And we're done. | |||
33934 | return tailMBB; | |||
33935 | } | |||
33936 | ||||
33937 | MachineBasicBlock * | |||
33938 | X86TargetLowering::EmitLoweredSegAlloca(MachineInstr &MI, | |||
33939 | MachineBasicBlock *BB) const { | |||
33940 | MachineFunction *MF = BB->getParent(); | |||
33941 | const TargetInstrInfo *TII = Subtarget.getInstrInfo(); | |||
33942 | const DebugLoc &DL = MI.getDebugLoc(); | |||
33943 | const BasicBlock *LLVM_BB = BB->getBasicBlock(); | |||
33944 | ||||
33945 | assert(MF->shouldSplitStack())(static_cast <bool> (MF->shouldSplitStack()) ? void ( 0) : __assert_fail ("MF->shouldSplitStack()", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 33945, __extension__ __PRETTY_FUNCTION__)); | |||
33946 | ||||
33947 | const bool Is64Bit = Subtarget.is64Bit(); | |||
33948 | const bool IsLP64 = Subtarget.isTarget64BitLP64(); | |||
33949 | ||||
33950 | const unsigned TlsReg = Is64Bit ? X86::FS : X86::GS; | |||
33951 | const unsigned TlsOffset = IsLP64 ? 0x70 : Is64Bit ? 0x40 : 0x30; | |||
33952 | ||||
33953 | // BB: | |||
33954 | // ... [Till the alloca] | |||
33955 | // If stacklet is not large enough, jump to mallocMBB | |||
33956 | // | |||
33957 | // bumpMBB: | |||
33958 | // Allocate by subtracting from RSP | |||
33959 | // Jump to continueMBB | |||
33960 | // | |||
33961 | // mallocMBB: | |||
33962 | // Allocate by call to runtime | |||
33963 | // | |||
33964 | // continueMBB: | |||
33965 | // ... | |||
33966 | // [rest of original BB] | |||
33967 | // | |||
33968 | ||||
33969 | MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB); | |||
33970 | MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB); | |||
33971 | MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB); | |||
33972 | ||||
33973 | MachineRegisterInfo &MRI = MF->getRegInfo(); | |||
33974 | const TargetRegisterClass *AddrRegClass = | |||
33975 | getRegClassFor(getPointerTy(MF->getDataLayout())); | |||
33976 | ||||
33977 | Register mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass), | |||
33978 | bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass), | |||
33979 | tmpSPVReg = MRI.createVirtualRegister(AddrRegClass), | |||
33980 | SPLimitVReg = MRI.createVirtualRegister(AddrRegClass), | |||
33981 | sizeVReg = MI.getOperand(1).getReg(), | |||
33982 | physSPReg = | |||
33983 | IsLP64 || Subtarget.isTargetNaCl64() ? X86::RSP : X86::ESP; | |||
33984 | ||||
33985 | MachineFunction::iterator MBBIter = ++BB->getIterator(); | |||
33986 | ||||
33987 | MF->insert(MBBIter, bumpMBB); | |||
33988 | MF->insert(MBBIter, mallocMBB); | |||
33989 | MF->insert(MBBIter, continueMBB); | |||
33990 | ||||
33991 | continueMBB->splice(continueMBB->begin(), BB, | |||
33992 | std::next(MachineBasicBlock::iterator(MI)), BB->end()); | |||
33993 | continueMBB->transferSuccessorsAndUpdatePHIs(BB); | |||
33994 | ||||
33995 | // Add code to the main basic block to check if the stack limit has been hit, | |||
33996 | // and if so, jump to mallocMBB otherwise to bumpMBB. | |||
33997 | BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg); | |||
33998 | BuildMI(BB, DL, TII->get(IsLP64 ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg) | |||
33999 | .addReg(tmpSPVReg).addReg(sizeVReg); | |||
34000 | BuildMI(BB, DL, TII->get(IsLP64 ? X86::CMP64mr:X86::CMP32mr)) | |||
34001 | .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg) | |||
34002 | .addReg(SPLimitVReg); | |||
34003 | BuildMI(BB, DL, TII->get(X86::JCC_1)).addMBB(mallocMBB).addImm(X86::COND_G); | |||
34004 | ||||
34005 | // bumpMBB simply decreases the stack pointer, since we know the current | |||
34006 | // stacklet has enough space. | |||
34007 | BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg) | |||
34008 | .addReg(SPLimitVReg); | |||
34009 | BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg) | |||
34010 | .addReg(SPLimitVReg); | |||
34011 | BuildMI(bumpMBB, DL, TII->get(X86::JMP_1)).addMBB(continueMBB); | |||
34012 | ||||
34013 | // Calls into a routine in libgcc to allocate more space from the heap. | |||
34014 | const uint32_t *RegMask = | |||
34015 | Subtarget.getRegisterInfo()->getCallPreservedMask(*MF, CallingConv::C); | |||
34016 | if (IsLP64) { | |||
34017 | BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI) | |||
34018 | .addReg(sizeVReg); | |||
34019 | BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32)) | |||
34020 | .addExternalSymbol("__morestack_allocate_stack_space") | |||
34021 | .addRegMask(RegMask) | |||
34022 | .addReg(X86::RDI, RegState::Implicit) | |||
34023 | .addReg(X86::RAX, RegState::ImplicitDefine); | |||
34024 | } else if (Is64Bit) { | |||
34025 | BuildMI(mallocMBB, DL, TII->get(X86::MOV32rr), X86::EDI) | |||
34026 | .addReg(sizeVReg); | |||
34027 | BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32)) | |||
34028 | .addExternalSymbol("__morestack_allocate_stack_space") | |||
34029 | .addRegMask(RegMask) | |||
34030 | .addReg(X86::EDI, RegState::Implicit) | |||
34031 | .addReg(X86::EAX, RegState::ImplicitDefine); | |||
34032 | } else { | |||
34033 | BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg) | |||
34034 | .addImm(12); | |||
34035 | BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg); | |||
34036 | BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32)) | |||
34037 | .addExternalSymbol("__morestack_allocate_stack_space") | |||
34038 | .addRegMask(RegMask) | |||
34039 | .addReg(X86::EAX, RegState::ImplicitDefine); | |||
34040 | } | |||
34041 | ||||
34042 | if (!Is64Bit) | |||
34043 | BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg) | |||
34044 | .addImm(16); | |||
34045 | ||||
34046 | BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg) | |||
34047 | .addReg(IsLP64 ? X86::RAX : X86::EAX); | |||
34048 | BuildMI(mallocMBB, DL, TII->get(X86::JMP_1)).addMBB(continueMBB); | |||
34049 | ||||
34050 | // Set up the CFG correctly. | |||
34051 | BB->addSuccessor(bumpMBB); | |||
34052 | BB->addSuccessor(mallocMBB); | |||
34053 | mallocMBB->addSuccessor(continueMBB); | |||
34054 | bumpMBB->addSuccessor(continueMBB); | |||
34055 | ||||
34056 | // Take care of the PHI nodes. | |||
34057 | BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI), | |||
34058 | MI.getOperand(0).getReg()) | |||
34059 | .addReg(mallocPtrVReg) | |||
34060 | .addMBB(mallocMBB) | |||
34061 | .addReg(bumpSPPtrVReg) | |||
34062 | .addMBB(bumpMBB); | |||
34063 | ||||
34064 | // Delete the original pseudo instruction. | |||
34065 | MI.eraseFromParent(); | |||
34066 | ||||
34067 | // And we're done. | |||
34068 | return continueMBB; | |||
34069 | } | |||
34070 | ||||
34071 | MachineBasicBlock * | |||
34072 | X86TargetLowering::EmitLoweredCatchRet(MachineInstr &MI, | |||
34073 | MachineBasicBlock *BB) const { | |||
34074 | MachineFunction *MF = BB->getParent(); | |||
34075 | const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); | |||
34076 | MachineBasicBlock *TargetMBB = MI.getOperand(0).getMBB(); | |||
34077 | const DebugLoc &DL = MI.getDebugLoc(); | |||
34078 | ||||
34079 | assert(!isAsynchronousEHPersonality((static_cast <bool> (!isAsynchronousEHPersonality( classifyEHPersonality (MF->getFunction().getPersonalityFn())) && "SEH does not use catchret!" ) ? void (0) : __assert_fail ("!isAsynchronousEHPersonality( classifyEHPersonality(MF->getFunction().getPersonalityFn())) && \"SEH does not use catchret!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 34081, __extension__ __PRETTY_FUNCTION__)) | |||
34080 | classifyEHPersonality(MF->getFunction().getPersonalityFn())) &&(static_cast <bool> (!isAsynchronousEHPersonality( classifyEHPersonality (MF->getFunction().getPersonalityFn())) && "SEH does not use catchret!" ) ? void (0) : __assert_fail ("!isAsynchronousEHPersonality( classifyEHPersonality(MF->getFunction().getPersonalityFn())) && \"SEH does not use catchret!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 34081, __extension__ __PRETTY_FUNCTION__)) | |||
34081 | "SEH does not use catchret!")(static_cast <bool> (!isAsynchronousEHPersonality( classifyEHPersonality (MF->getFunction().getPersonalityFn())) && "SEH does not use catchret!" ) ? void (0) : __assert_fail ("!isAsynchronousEHPersonality( classifyEHPersonality(MF->getFunction().getPersonalityFn())) && \"SEH does not use catchret!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 34081, __extension__ __PRETTY_FUNCTION__)); | |||
34082 | ||||
34083 | // Only 32-bit EH needs to worry about manually restoring stack pointers. | |||
34084 | if (!Subtarget.is32Bit()) | |||
34085 | return BB; | |||
34086 | ||||
34087 | // C++ EH creates a new target block to hold the restore code, and wires up | |||
34088 | // the new block to the return destination with a normal JMP_4. | |||
34089 | MachineBasicBlock *RestoreMBB = | |||
34090 | MF->CreateMachineBasicBlock(BB->getBasicBlock()); | |||
34091 | assert(BB->succ_size() == 1)(static_cast <bool> (BB->succ_size() == 1) ? void (0 ) : __assert_fail ("BB->succ_size() == 1", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 34091, __extension__ __PRETTY_FUNCTION__)); | |||
34092 | MF->insert(std::next(BB->getIterator()), RestoreMBB); | |||
34093 | RestoreMBB->transferSuccessorsAndUpdatePHIs(BB); | |||
34094 | BB->addSuccessor(RestoreMBB); | |||
34095 | MI.getOperand(0).setMBB(RestoreMBB); | |||
34096 | ||||
34097 | // Marking this as an EH pad but not a funclet entry block causes PEI to | |||
34098 | // restore stack pointers in the block. | |||
34099 | RestoreMBB->setIsEHPad(true); | |||
34100 | ||||
34101 | auto RestoreMBBI = RestoreMBB->begin(); | |||
34102 | BuildMI(*RestoreMBB, RestoreMBBI, DL, TII.get(X86::JMP_4)).addMBB(TargetMBB); | |||
34103 | return BB; | |||
34104 | } | |||
34105 | ||||
34106 | MachineBasicBlock * | |||
34107 | X86TargetLowering::EmitLoweredTLSAddr(MachineInstr &MI, | |||
34108 | MachineBasicBlock *BB) const { | |||
34109 | // So, here we replace TLSADDR with the sequence: | |||
34110 | // adjust_stackdown -> TLSADDR -> adjust_stackup. | |||
34111 | // We need this because TLSADDR is lowered into calls | |||
34112 | // inside MC, therefore without the two markers shrink-wrapping | |||
34113 | // may push the prologue/epilogue pass them. | |||
34114 | const TargetInstrInfo &TII = *Subtarget.getInstrInfo(); | |||
34115 | const DebugLoc &DL = MI.getDebugLoc(); | |||
34116 | MachineFunction &MF = *BB->getParent(); | |||
34117 | ||||
34118 | // Emit CALLSEQ_START right before the instruction. | |||
34119 | unsigned AdjStackDown = TII.getCallFrameSetupOpcode(); | |||
34120 | MachineInstrBuilder CallseqStart = | |||
34121 | BuildMI(MF, DL, TII.get(AdjStackDown)).addImm(0).addImm(0).addImm(0); | |||
34122 | BB->insert(MachineBasicBlock::iterator(MI), CallseqStart); | |||
34123 | ||||
34124 | // Emit CALLSEQ_END right after the instruction. | |||
34125 | // We don't call erase from parent because we want to keep the | |||
34126 | // original instruction around. | |||
34127 | unsigned AdjStackUp = TII.getCallFrameDestroyOpcode(); | |||
34128 | MachineInstrBuilder CallseqEnd = | |||
34129 | BuildMI(MF, DL, TII.get(AdjStackUp)).addImm(0).addImm(0); | |||
34130 | BB->insertAfter(MachineBasicBlock::iterator(MI), CallseqEnd); | |||
34131 | ||||
34132 | return BB; | |||
34133 | } | |||
34134 | ||||
34135 | MachineBasicBlock * | |||
34136 | X86TargetLowering::EmitLoweredTLSCall(MachineInstr &MI, | |||
34137 | MachineBasicBlock *BB) const { | |||
34138 | // This is pretty easy. We're taking the value that we received from | |||
34139 | // our load from the relocation, sticking it in either RDI (x86-64) | |||
34140 | // or EAX and doing an indirect call. The return value will then | |||
34141 | // be in the normal return register. | |||
34142 | MachineFunction *F = BB->getParent(); | |||
34143 | const X86InstrInfo *TII = Subtarget.getInstrInfo(); | |||
34144 | const DebugLoc &DL = MI.getDebugLoc(); | |||
34145 | ||||
34146 | assert(Subtarget.isTargetDarwin() && "Darwin only instr emitted?")(static_cast <bool> (Subtarget.isTargetDarwin() && "Darwin only instr emitted?") ? void (0) : __assert_fail ("Subtarget.isTargetDarwin() && \"Darwin only instr emitted?\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 34146, __extension__ __PRETTY_FUNCTION__)); | |||
34147 | assert(MI.getOperand(3).isGlobal() && "This should be a global")(static_cast <bool> (MI.getOperand(3).isGlobal() && "This should be a global") ? void (0) : __assert_fail ("MI.getOperand(3).isGlobal() && \"This should be a global\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 34147, __extension__ __PRETTY_FUNCTION__)); | |||
34148 | ||||
34149 | // Get a register mask for the lowered call. | |||
34150 | // FIXME: The 32-bit calls have non-standard calling conventions. Use a | |||
34151 | // proper register mask. | |||
34152 | const uint32_t *RegMask = | |||
34153 | Subtarget.is64Bit() ? | |||
34154 | Subtarget.getRegisterInfo()->getDarwinTLSCallPreservedMask() : | |||
34155 | Subtarget.getRegisterInfo()->getCallPreservedMask(*F, CallingConv::C); | |||
34156 | if (Subtarget.is64Bit()) { | |||
34157 | MachineInstrBuilder MIB = | |||
34158 | BuildMI(*BB, MI, DL, TII->get(X86::MOV64rm), X86::RDI) | |||
34159 | .addReg(X86::RIP) | |||
34160 | .addImm(0) | |||
34161 | .addReg(0) | |||
34162 | .addGlobalAddress(MI.getOperand(3).getGlobal(), 0, | |||
34163 | MI.getOperand(3).getTargetFlags()) | |||
34164 | .addReg(0); | |||
34165 | MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m)); | |||
34166 | addDirectMem(MIB, X86::RDI); | |||
34167 | MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask); | |||
34168 | } else if (!isPositionIndependent()) { | |||
34169 | MachineInstrBuilder MIB = | |||
34170 | BuildMI(*BB, MI, DL, TII->get(X86::MOV32rm), X86::EAX) | |||
34171 | .addReg(0) | |||
34172 | .addImm(0) | |||
34173 | .addReg(0) | |||
34174 | .addGlobalAddress(MI.getOperand(3).getGlobal(), 0, | |||
34175 | MI.getOperand(3).getTargetFlags()) | |||
34176 | .addReg(0); | |||
34177 | MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m)); | |||
34178 | addDirectMem(MIB, X86::EAX); | |||
34179 | MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask); | |||
34180 | } else { | |||
34181 | MachineInstrBuilder MIB = | |||
34182 | BuildMI(*BB, MI, DL, TII->get(X86::MOV32rm), X86::EAX) | |||
34183 | .addReg(TII->getGlobalBaseReg(F)) | |||
34184 | .addImm(0) | |||
34185 | .addReg(0) | |||
34186 | .addGlobalAddress(MI.getOperand(3).getGlobal(), 0, | |||
34187 | MI.getOperand(3).getTargetFlags()) | |||
34188 | .addReg(0); | |||
34189 | MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m)); | |||
34190 | addDirectMem(MIB, X86::EAX); | |||
34191 | MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask); | |||
34192 | } | |||
34193 | ||||
34194 | MI.eraseFromParent(); // The pseudo instruction is gone now. | |||
34195 | return BB; | |||
34196 | } | |||
34197 | ||||
34198 | static unsigned getOpcodeForIndirectThunk(unsigned RPOpc) { | |||
34199 | switch (RPOpc) { | |||
34200 | case X86::INDIRECT_THUNK_CALL32: | |||
34201 | return X86::CALLpcrel32; | |||
34202 | case X86::INDIRECT_THUNK_CALL64: | |||
34203 | return X86::CALL64pcrel32; | |||
34204 | case X86::INDIRECT_THUNK_TCRETURN32: | |||
34205 | return X86::TCRETURNdi; | |||
34206 | case X86::INDIRECT_THUNK_TCRETURN64: | |||
34207 | return X86::TCRETURNdi64; | |||
34208 | } | |||
34209 | llvm_unreachable("not indirect thunk opcode")::llvm::llvm_unreachable_internal("not indirect thunk opcode" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 34209); | |||
34210 | } | |||
34211 | ||||
34212 | static const char *getIndirectThunkSymbol(const X86Subtarget &Subtarget, | |||
34213 | unsigned Reg) { | |||
34214 | if (Subtarget.useRetpolineExternalThunk()) { | |||
34215 | // When using an external thunk for retpolines, we pick names that match the | |||
34216 | // names GCC happens to use as well. This helps simplify the implementation | |||
34217 | // of the thunks for kernels where they have no easy ability to create | |||
34218 | // aliases and are doing non-trivial configuration of the thunk's body. For | |||
34219 | // example, the Linux kernel will do boot-time hot patching of the thunk | |||
34220 | // bodies and cannot easily export aliases of these to loaded modules. | |||
34221 | // | |||
34222 | // Note that at any point in the future, we may need to change the semantics | |||
34223 | // of how we implement retpolines and at that time will likely change the | |||
34224 | // name of the called thunk. Essentially, there is no hard guarantee that | |||
34225 | // LLVM will generate calls to specific thunks, we merely make a best-effort | |||
34226 | // attempt to help out kernels and other systems where duplicating the | |||
34227 | // thunks is costly. | |||
34228 | switch (Reg) { | |||
34229 | case X86::EAX: | |||
34230 | assert(!Subtarget.is64Bit() && "Should not be using a 32-bit thunk!")(static_cast <bool> (!Subtarget.is64Bit() && "Should not be using a 32-bit thunk!" ) ? void (0) : __assert_fail ("!Subtarget.is64Bit() && \"Should not be using a 32-bit thunk!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 34230, __extension__ __PRETTY_FUNCTION__)); | |||
34231 | return "__x86_indirect_thunk_eax"; | |||
34232 | case X86::ECX: | |||
34233 | assert(!Subtarget.is64Bit() && "Should not be using a 32-bit thunk!")(static_cast <bool> (!Subtarget.is64Bit() && "Should not be using a 32-bit thunk!" ) ? void (0) : __assert_fail ("!Subtarget.is64Bit() && \"Should not be using a 32-bit thunk!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 34233, __extension__ __PRETTY_FUNCTION__)); | |||
34234 | return "__x86_indirect_thunk_ecx"; | |||
34235 | case X86::EDX: | |||
34236 | assert(!Subtarget.is64Bit() && "Should not be using a 32-bit thunk!")(static_cast <bool> (!Subtarget.is64Bit() && "Should not be using a 32-bit thunk!" ) ? void (0) : __assert_fail ("!Subtarget.is64Bit() && \"Should not be using a 32-bit thunk!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 34236, __extension__ __PRETTY_FUNCTION__)); | |||
34237 | return "__x86_indirect_thunk_edx"; | |||
34238 | case X86::EDI: | |||
34239 | assert(!Subtarget.is64Bit() && "Should not be using a 32-bit thunk!")(static_cast <bool> (!Subtarget.is64Bit() && "Should not be using a 32-bit thunk!" ) ? void (0) : __assert_fail ("!Subtarget.is64Bit() && \"Should not be using a 32-bit thunk!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 34239, __extension__ __PRETTY_FUNCTION__)); | |||
34240 | return "__x86_indirect_thunk_edi"; | |||
34241 | case X86::R11: | |||
34242 | assert(Subtarget.is64Bit() && "Should not be using a 64-bit thunk!")(static_cast <bool> (Subtarget.is64Bit() && "Should not be using a 64-bit thunk!" ) ? void (0) : __assert_fail ("Subtarget.is64Bit() && \"Should not be using a 64-bit thunk!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 34242, __extension__ __PRETTY_FUNCTION__)); | |||
34243 | return "__x86_indirect_thunk_r11"; | |||
34244 | } | |||
34245 | llvm_unreachable("unexpected reg for external indirect thunk")::llvm::llvm_unreachable_internal("unexpected reg for external indirect thunk" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 34245); | |||
34246 | } | |||
34247 | ||||
34248 | if (Subtarget.useRetpolineIndirectCalls() || | |||
34249 | Subtarget.useRetpolineIndirectBranches()) { | |||
34250 | // When targeting an internal COMDAT thunk use an LLVM-specific name. | |||
34251 | switch (Reg) { | |||
34252 | case X86::EAX: | |||
34253 | assert(!Subtarget.is64Bit() && "Should not be using a 32-bit thunk!")(static_cast <bool> (!Subtarget.is64Bit() && "Should not be using a 32-bit thunk!" ) ? void (0) : __assert_fail ("!Subtarget.is64Bit() && \"Should not be using a 32-bit thunk!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 34253, __extension__ __PRETTY_FUNCTION__)); | |||
34254 | return "__llvm_retpoline_eax"; | |||
34255 | case X86::ECX: | |||
34256 | assert(!Subtarget.is64Bit() && "Should not be using a 32-bit thunk!")(static_cast <bool> (!Subtarget.is64Bit() && "Should not be using a 32-bit thunk!" ) ? void (0) : __assert_fail ("!Subtarget.is64Bit() && \"Should not be using a 32-bit thunk!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 34256, __extension__ __PRETTY_FUNCTION__)); | |||
34257 | return "__llvm_retpoline_ecx"; | |||
34258 | case X86::EDX: | |||
34259 | assert(!Subtarget.is64Bit() && "Should not be using a 32-bit thunk!")(static_cast <bool> (!Subtarget.is64Bit() && "Should not be using a 32-bit thunk!" ) ? void (0) : __assert_fail ("!Subtarget.is64Bit() && \"Should not be using a 32-bit thunk!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 34259, __extension__ __PRETTY_FUNCTION__)); | |||
34260 | return "__llvm_retpoline_edx"; | |||
34261 | case X86::EDI: | |||
34262 | assert(!Subtarget.is64Bit() && "Should not be using a 32-bit thunk!")(static_cast <bool> (!Subtarget.is64Bit() && "Should not be using a 32-bit thunk!" ) ? void (0) : __assert_fail ("!Subtarget.is64Bit() && \"Should not be using a 32-bit thunk!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 34262, __extension__ __PRETTY_FUNCTION__)); | |||
34263 | return "__llvm_retpoline_edi"; | |||
34264 | case X86::R11: | |||
34265 | assert(Subtarget.is64Bit() && "Should not be using a 64-bit thunk!")(static_cast <bool> (Subtarget.is64Bit() && "Should not be using a 64-bit thunk!" ) ? void (0) : __assert_fail ("Subtarget.is64Bit() && \"Should not be using a 64-bit thunk!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 34265, __extension__ __PRETTY_FUNCTION__)); | |||
34266 | return "__llvm_retpoline_r11"; | |||
34267 | } | |||
34268 | llvm_unreachable("unexpected reg for retpoline")::llvm::llvm_unreachable_internal("unexpected reg for retpoline" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 34268); | |||
34269 | } | |||
34270 | ||||
34271 | if (Subtarget.useLVIControlFlowIntegrity()) { | |||
34272 | assert(Subtarget.is64Bit() && "Should not be using a 64-bit thunk!")(static_cast <bool> (Subtarget.is64Bit() && "Should not be using a 64-bit thunk!" ) ? void (0) : __assert_fail ("Subtarget.is64Bit() && \"Should not be using a 64-bit thunk!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 34272, __extension__ __PRETTY_FUNCTION__)); | |||
34273 | return "__llvm_lvi_thunk_r11"; | |||
34274 | } | |||
34275 | llvm_unreachable("getIndirectThunkSymbol() invoked without thunk feature")::llvm::llvm_unreachable_internal("getIndirectThunkSymbol() invoked without thunk feature" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 34275); | |||
34276 | } | |||
34277 | ||||
34278 | MachineBasicBlock * | |||
34279 | X86TargetLowering::EmitLoweredIndirectThunk(MachineInstr &MI, | |||
34280 | MachineBasicBlock *BB) const { | |||
34281 | // Copy the virtual register into the R11 physical register and | |||
34282 | // call the retpoline thunk. | |||
34283 | const DebugLoc &DL = MI.getDebugLoc(); | |||
34284 | const X86InstrInfo *TII = Subtarget.getInstrInfo(); | |||
34285 | Register CalleeVReg = MI.getOperand(0).getReg(); | |||
34286 | unsigned Opc = getOpcodeForIndirectThunk(MI.getOpcode()); | |||
34287 | ||||
34288 | // Find an available scratch register to hold the callee. On 64-bit, we can | |||
34289 | // just use R11, but we scan for uses anyway to ensure we don't generate | |||
34290 | // incorrect code. On 32-bit, we use one of EAX, ECX, or EDX that isn't | |||
34291 | // already a register use operand to the call to hold the callee. If none | |||
34292 | // are available, use EDI instead. EDI is chosen because EBX is the PIC base | |||
34293 | // register and ESI is the base pointer to realigned stack frames with VLAs. | |||
34294 | SmallVector<unsigned, 3> AvailableRegs; | |||
34295 | if (Subtarget.is64Bit()) | |||
34296 | AvailableRegs.push_back(X86::R11); | |||
34297 | else | |||
34298 | AvailableRegs.append({X86::EAX, X86::ECX, X86::EDX, X86::EDI}); | |||
34299 | ||||
34300 | // Zero out any registers that are already used. | |||
34301 | for (const auto &MO : MI.operands()) { | |||
34302 | if (MO.isReg() && MO.isUse()) | |||
34303 | for (unsigned &Reg : AvailableRegs) | |||
34304 | if (Reg == MO.getReg()) | |||
34305 | Reg = 0; | |||
34306 | } | |||
34307 | ||||
34308 | // Choose the first remaining non-zero available register. | |||
34309 | unsigned AvailableReg = 0; | |||
34310 | for (unsigned MaybeReg : AvailableRegs) { | |||
34311 | if (MaybeReg) { | |||
34312 | AvailableReg = MaybeReg; | |||
34313 | break; | |||
34314 | } | |||
34315 | } | |||
34316 | if (!AvailableReg) | |||
34317 | report_fatal_error("calling convention incompatible with retpoline, no " | |||
34318 | "available registers"); | |||
34319 | ||||
34320 | const char *Symbol = getIndirectThunkSymbol(Subtarget, AvailableReg); | |||
34321 | ||||
34322 | BuildMI(*BB, MI, DL, TII->get(TargetOpcode::COPY), AvailableReg) | |||
34323 | .addReg(CalleeVReg); | |||
34324 | MI.getOperand(0).ChangeToES(Symbol); | |||
34325 | MI.setDesc(TII->get(Opc)); | |||
34326 | MachineInstrBuilder(*BB->getParent(), &MI) | |||
34327 | .addReg(AvailableReg, RegState::Implicit | RegState::Kill); | |||
34328 | return BB; | |||
34329 | } | |||
34330 | ||||
34331 | /// SetJmp implies future control flow change upon calling the corresponding | |||
34332 | /// LongJmp. | |||
34333 | /// Instead of using the 'return' instruction, the long jump fixes the stack and | |||
34334 | /// performs an indirect branch. To do so it uses the registers that were stored | |||
34335 | /// in the jump buffer (when calling SetJmp). | |||
34336 | /// In case the shadow stack is enabled we need to fix it as well, because some | |||
34337 | /// return addresses will be skipped. | |||
34338 | /// The function will save the SSP for future fixing in the function | |||
34339 | /// emitLongJmpShadowStackFix. | |||
34340 | /// \sa emitLongJmpShadowStackFix | |||
34341 | /// \param [in] MI The temporary Machine Instruction for the builtin. | |||
34342 | /// \param [in] MBB The Machine Basic Block that will be modified. | |||
34343 | void X86TargetLowering::emitSetJmpShadowStackFix(MachineInstr &MI, | |||
34344 | MachineBasicBlock *MBB) const { | |||
34345 | const DebugLoc &DL = MI.getDebugLoc(); | |||
34346 | MachineFunction *MF = MBB->getParent(); | |||
34347 | const TargetInstrInfo *TII = Subtarget.getInstrInfo(); | |||
34348 | MachineRegisterInfo &MRI = MF->getRegInfo(); | |||
34349 | MachineInstrBuilder MIB; | |||
34350 | ||||
34351 | // Memory Reference. | |||
34352 | SmallVector<MachineMemOperand *, 2> MMOs(MI.memoperands_begin(), | |||
34353 | MI.memoperands_end()); | |||
34354 | ||||
34355 | // Initialize a register with zero. | |||
34356 | MVT PVT = getPointerTy(MF->getDataLayout()); | |||
34357 | const TargetRegisterClass *PtrRC = getRegClassFor(PVT); | |||
34358 | Register ZReg = MRI.createVirtualRegister(PtrRC); | |||
34359 | unsigned XorRROpc = (PVT == MVT::i64) ? X86::XOR64rr : X86::XOR32rr; | |||
34360 | BuildMI(*MBB, MI, DL, TII->get(XorRROpc)) | |||
34361 | .addDef(ZReg) | |||
34362 | .addReg(ZReg, RegState::Undef) | |||
34363 | .addReg(ZReg, RegState::Undef); | |||
34364 | ||||
34365 | // Read the current SSP Register value to the zeroed register. | |||
34366 | Register SSPCopyReg = MRI.createVirtualRegister(PtrRC); | |||
34367 | unsigned RdsspOpc = (PVT == MVT::i64) ? X86::RDSSPQ : X86::RDSSPD; | |||
34368 | BuildMI(*MBB, MI, DL, TII->get(RdsspOpc), SSPCopyReg).addReg(ZReg); | |||
34369 | ||||
34370 | // Write the SSP register value to offset 3 in input memory buffer. | |||
34371 | unsigned PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mr : X86::MOV32mr; | |||
34372 | MIB = BuildMI(*MBB, MI, DL, TII->get(PtrStoreOpc)); | |||
34373 | const int64_t SSPOffset = 3 * PVT.getStoreSize(); | |||
34374 | const unsigned MemOpndSlot = 1; | |||
34375 | for (unsigned i = 0; i < X86::AddrNumOperands; ++i) { | |||
34376 | if (i == X86::AddrDisp) | |||
34377 | MIB.addDisp(MI.getOperand(MemOpndSlot + i), SSPOffset); | |||
34378 | else | |||
34379 | MIB.add(MI.getOperand(MemOpndSlot + i)); | |||
34380 | } | |||
34381 | MIB.addReg(SSPCopyReg); | |||
34382 | MIB.setMemRefs(MMOs); | |||
34383 | } | |||
34384 | ||||
34385 | MachineBasicBlock * | |||
34386 | X86TargetLowering::emitEHSjLjSetJmp(MachineInstr &MI, | |||
34387 | MachineBasicBlock *MBB) const { | |||
34388 | const DebugLoc &DL = MI.getDebugLoc(); | |||
34389 | MachineFunction *MF = MBB->getParent(); | |||
34390 | const TargetInstrInfo *TII = Subtarget.getInstrInfo(); | |||
34391 | const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo(); | |||
34392 | MachineRegisterInfo &MRI = MF->getRegInfo(); | |||
34393 | ||||
34394 | const BasicBlock *BB = MBB->getBasicBlock(); | |||
34395 | MachineFunction::iterator I = ++MBB->getIterator(); | |||
34396 | ||||
34397 | // Memory Reference | |||
34398 | SmallVector<MachineMemOperand *, 2> MMOs(MI.memoperands_begin(), | |||
34399 | MI.memoperands_end()); | |||
34400 | ||||
34401 | unsigned DstReg; | |||
34402 | unsigned MemOpndSlot = 0; | |||
34403 | ||||
34404 | unsigned CurOp = 0; | |||
34405 | ||||
34406 | DstReg = MI.getOperand(CurOp++).getReg(); | |||
34407 | const TargetRegisterClass *RC = MRI.getRegClass(DstReg); | |||
34408 | assert(TRI->isTypeLegalForClass(*RC, MVT::i32) && "Invalid destination!")(static_cast <bool> (TRI->isTypeLegalForClass(*RC, MVT ::i32) && "Invalid destination!") ? void (0) : __assert_fail ("TRI->isTypeLegalForClass(*RC, MVT::i32) && \"Invalid destination!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 34408, __extension__ __PRETTY_FUNCTION__)); | |||
34409 | (void)TRI; | |||
34410 | Register mainDstReg = MRI.createVirtualRegister(RC); | |||
34411 | Register restoreDstReg = MRI.createVirtualRegister(RC); | |||
34412 | ||||
34413 | MemOpndSlot = CurOp; | |||
34414 | ||||
34415 | MVT PVT = getPointerTy(MF->getDataLayout()); | |||
34416 | assert((PVT == MVT::i64 || PVT == MVT::i32) &&(static_cast <bool> ((PVT == MVT::i64 || PVT == MVT::i32 ) && "Invalid Pointer Size!") ? void (0) : __assert_fail ("(PVT == MVT::i64 || PVT == MVT::i32) && \"Invalid Pointer Size!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 34417, __extension__ __PRETTY_FUNCTION__)) | |||
34417 | "Invalid Pointer Size!")(static_cast <bool> ((PVT == MVT::i64 || PVT == MVT::i32 ) && "Invalid Pointer Size!") ? void (0) : __assert_fail ("(PVT == MVT::i64 || PVT == MVT::i32) && \"Invalid Pointer Size!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 34417, __extension__ __PRETTY_FUNCTION__)); | |||
34418 | ||||
34419 | // For v = setjmp(buf), we generate | |||
34420 | // | |||
34421 | // thisMBB: | |||
34422 | // buf[LabelOffset] = restoreMBB <-- takes address of restoreMBB | |||
34423 | // SjLjSetup restoreMBB | |||
34424 | // | |||
34425 | // mainMBB: | |||
34426 | // v_main = 0 | |||
34427 | // | |||
34428 | // sinkMBB: | |||
34429 | // v = phi(main, restore) | |||
34430 | // | |||
34431 | // restoreMBB: | |||
34432 | // if base pointer being used, load it from frame | |||
34433 | // v_restore = 1 | |||
34434 | ||||
34435 | MachineBasicBlock *thisMBB = MBB; | |||
34436 | MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB); | |||
34437 | MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB); | |||
34438 | MachineBasicBlock *restoreMBB = MF->CreateMachineBasicBlock(BB); | |||
34439 | MF->insert(I, mainMBB); | |||
34440 | MF->insert(I, sinkMBB); | |||
34441 | MF->push_back(restoreMBB); | |||
34442 | restoreMBB->setHasAddressTaken(); | |||
34443 | ||||
34444 | MachineInstrBuilder MIB; | |||
34445 | ||||
34446 | // Transfer the remainder of BB and its successor edges to sinkMBB. | |||
34447 | sinkMBB->splice(sinkMBB->begin(), MBB, | |||
34448 | std::next(MachineBasicBlock::iterator(MI)), MBB->end()); | |||
34449 | sinkMBB->transferSuccessorsAndUpdatePHIs(MBB); | |||
34450 | ||||
34451 | // thisMBB: | |||
34452 | unsigned PtrStoreOpc = 0; | |||
34453 | unsigned LabelReg = 0; | |||
34454 | const int64_t LabelOffset = 1 * PVT.getStoreSize(); | |||
34455 | bool UseImmLabel = (MF->getTarget().getCodeModel() == CodeModel::Small) && | |||
34456 | !isPositionIndependent(); | |||
34457 | ||||
34458 | // Prepare IP either in reg or imm. | |||
34459 | if (!UseImmLabel) { | |||
34460 | PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mr : X86::MOV32mr; | |||
34461 | const TargetRegisterClass *PtrRC = getRegClassFor(PVT); | |||
34462 | LabelReg = MRI.createVirtualRegister(PtrRC); | |||
34463 | if (Subtarget.is64Bit()) { | |||
34464 | MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA64r), LabelReg) | |||
34465 | .addReg(X86::RIP) | |||
34466 | .addImm(0) | |||
34467 | .addReg(0) | |||
34468 | .addMBB(restoreMBB) | |||
34469 | .addReg(0); | |||
34470 | } else { | |||
34471 | const X86InstrInfo *XII = static_cast<const X86InstrInfo*>(TII); | |||
34472 | MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA32r), LabelReg) | |||
34473 | .addReg(XII->getGlobalBaseReg(MF)) | |||
34474 | .addImm(0) | |||
34475 | .addReg(0) | |||
34476 | .addMBB(restoreMBB, Subtarget.classifyBlockAddressReference()) | |||
34477 | .addReg(0); | |||
34478 | } | |||
34479 | } else | |||
34480 | PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mi32 : X86::MOV32mi; | |||
34481 | // Store IP | |||
34482 | MIB = BuildMI(*thisMBB, MI, DL, TII->get(PtrStoreOpc)); | |||
34483 | for (unsigned i = 0; i < X86::AddrNumOperands; ++i) { | |||
34484 | if (i == X86::AddrDisp) | |||
34485 | MIB.addDisp(MI.getOperand(MemOpndSlot + i), LabelOffset); | |||
34486 | else | |||
34487 | MIB.add(MI.getOperand(MemOpndSlot + i)); | |||
34488 | } | |||
34489 | if (!UseImmLabel) | |||
34490 | MIB.addReg(LabelReg); | |||
34491 | else | |||
34492 | MIB.addMBB(restoreMBB); | |||
34493 | MIB.setMemRefs(MMOs); | |||
34494 | ||||
34495 | if (MF->getMMI().getModule()->getModuleFlag("cf-protection-return")) { | |||
34496 | emitSetJmpShadowStackFix(MI, thisMBB); | |||
34497 | } | |||
34498 | ||||
34499 | // Setup | |||
34500 | MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::EH_SjLj_Setup)) | |||
34501 | .addMBB(restoreMBB); | |||
34502 | ||||
34503 | const X86RegisterInfo *RegInfo = Subtarget.getRegisterInfo(); | |||
34504 | MIB.addRegMask(RegInfo->getNoPreservedMask()); | |||
34505 | thisMBB->addSuccessor(mainMBB); | |||
34506 | thisMBB->addSuccessor(restoreMBB); | |||
34507 | ||||
34508 | // mainMBB: | |||
34509 | // EAX = 0 | |||
34510 | BuildMI(mainMBB, DL, TII->get(X86::MOV32r0), mainDstReg); | |||
34511 | mainMBB->addSuccessor(sinkMBB); | |||
34512 | ||||
34513 | // sinkMBB: | |||
34514 | BuildMI(*sinkMBB, sinkMBB->begin(), DL, | |||
34515 | TII->get(X86::PHI), DstReg) | |||
34516 | .addReg(mainDstReg).addMBB(mainMBB) | |||
34517 | .addReg(restoreDstReg).addMBB(restoreMBB); | |||
34518 | ||||
34519 | // restoreMBB: | |||
34520 | if (RegInfo->hasBasePointer(*MF)) { | |||
34521 | const bool Uses64BitFramePtr = | |||
34522 | Subtarget.isTarget64BitLP64() || Subtarget.isTargetNaCl64(); | |||
34523 | X86MachineFunctionInfo *X86FI = MF->getInfo<X86MachineFunctionInfo>(); | |||
34524 | X86FI->setRestoreBasePointer(MF); | |||
34525 | Register FramePtr = RegInfo->getFrameRegister(*MF); | |||
34526 | Register BasePtr = RegInfo->getBaseRegister(); | |||
34527 | unsigned Opm = Uses64BitFramePtr ? X86::MOV64rm : X86::MOV32rm; | |||
34528 | addRegOffset(BuildMI(restoreMBB, DL, TII->get(Opm), BasePtr), | |||
34529 | FramePtr, true, X86FI->getRestoreBasePointerOffset()) | |||
34530 | .setMIFlag(MachineInstr::FrameSetup); | |||
34531 | } | |||
34532 | BuildMI(restoreMBB, DL, TII->get(X86::MOV32ri), restoreDstReg).addImm(1); | |||
34533 | BuildMI(restoreMBB, DL, TII->get(X86::JMP_1)).addMBB(sinkMBB); | |||
34534 | restoreMBB->addSuccessor(sinkMBB); | |||
34535 | ||||
34536 | MI.eraseFromParent(); | |||
34537 | return sinkMBB; | |||
34538 | } | |||
34539 | ||||
34540 | /// Fix the shadow stack using the previously saved SSP pointer. | |||
34541 | /// \sa emitSetJmpShadowStackFix | |||
34542 | /// \param [in] MI The temporary Machine Instruction for the builtin. | |||
34543 | /// \param [in] MBB The Machine Basic Block that will be modified. | |||
34544 | /// \return The sink MBB that will perform the future indirect branch. | |||
34545 | MachineBasicBlock * | |||
34546 | X86TargetLowering::emitLongJmpShadowStackFix(MachineInstr &MI, | |||
34547 | MachineBasicBlock *MBB) const { | |||
34548 | const DebugLoc &DL = MI.getDebugLoc(); | |||
34549 | MachineFunction *MF = MBB->getParent(); | |||
34550 | const TargetInstrInfo *TII = Subtarget.getInstrInfo(); | |||
34551 | MachineRegisterInfo &MRI = MF->getRegInfo(); | |||
34552 | ||||
34553 | // Memory Reference | |||
34554 | SmallVector<MachineMemOperand *, 2> MMOs(MI.memoperands_begin(), | |||
34555 | MI.memoperands_end()); | |||
34556 | ||||
34557 | MVT PVT = getPointerTy(MF->getDataLayout()); | |||
34558 | const TargetRegisterClass *PtrRC = getRegClassFor(PVT); | |||
34559 | ||||
34560 | // checkSspMBB: | |||
34561 | // xor vreg1, vreg1 | |||
34562 | // rdssp vreg1 | |||
34563 | // test vreg1, vreg1 | |||
34564 | // je sinkMBB # Jump if Shadow Stack is not supported | |||
34565 | // fallMBB: | |||
34566 | // mov buf+24/12(%rip), vreg2 | |||
34567 | // sub vreg1, vreg2 | |||
34568 | // jbe sinkMBB # No need to fix the Shadow Stack | |||
34569 | // fixShadowMBB: | |||
34570 | // shr 3/2, vreg2 | |||
34571 | // incssp vreg2 # fix the SSP according to the lower 8 bits | |||
34572 | // shr 8, vreg2 | |||
34573 | // je sinkMBB | |||
34574 | // fixShadowLoopPrepareMBB: | |||
34575 | // shl vreg2 | |||
34576 | // mov 128, vreg3 | |||
34577 | // fixShadowLoopMBB: | |||
34578 | // incssp vreg3 | |||
34579 | // dec vreg2 | |||
34580 | // jne fixShadowLoopMBB # Iterate until you finish fixing | |||
34581 | // # the Shadow Stack | |||
34582 | // sinkMBB: | |||
34583 | ||||
34584 | MachineFunction::iterator I = ++MBB->getIterator(); | |||
34585 | const BasicBlock *BB = MBB->getBasicBlock(); | |||
34586 | ||||
34587 | MachineBasicBlock *checkSspMBB = MF->CreateMachineBasicBlock(BB); | |||
34588 | MachineBasicBlock *fallMBB = MF->CreateMachineBasicBlock(BB); | |||
34589 | MachineBasicBlock *fixShadowMBB = MF->CreateMachineBasicBlock(BB); | |||
34590 | MachineBasicBlock *fixShadowLoopPrepareMBB = MF->CreateMachineBasicBlock(BB); | |||
34591 | MachineBasicBlock *fixShadowLoopMBB = MF->CreateMachineBasicBlock(BB); | |||
34592 | MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB); | |||
34593 | MF->insert(I, checkSspMBB); | |||
34594 | MF->insert(I, fallMBB); | |||
34595 | MF->insert(I, fixShadowMBB); | |||
34596 | MF->insert(I, fixShadowLoopPrepareMBB); | |||
34597 | MF->insert(I, fixShadowLoopMBB); | |||
34598 | MF->insert(I, sinkMBB); | |||
34599 | ||||
34600 | // Transfer the remainder of BB and its successor edges to sinkMBB. | |||
34601 | sinkMBB->splice(sinkMBB->begin(), MBB, MachineBasicBlock::iterator(MI), | |||
34602 | MBB->end()); | |||
34603 | sinkMBB->transferSuccessorsAndUpdatePHIs(MBB); | |||
34604 | ||||
34605 | MBB->addSuccessor(checkSspMBB); | |||
34606 | ||||
34607 | // Initialize a register with zero. | |||
34608 | Register ZReg = MRI.createVirtualRegister(&X86::GR32RegClass); | |||
34609 | BuildMI(checkSspMBB, DL, TII->get(X86::MOV32r0), ZReg); | |||
34610 | ||||
34611 | if (PVT == MVT::i64) { | |||
34612 | Register TmpZReg = MRI.createVirtualRegister(PtrRC); | |||
34613 | BuildMI(checkSspMBB, DL, TII->get(X86::SUBREG_TO_REG), TmpZReg) | |||
34614 | .addImm(0) | |||
34615 | .addReg(ZReg) | |||
34616 | .addImm(X86::sub_32bit); | |||
34617 | ZReg = TmpZReg; | |||
34618 | } | |||
34619 | ||||
34620 | // Read the current SSP Register value to the zeroed register. | |||
34621 | Register SSPCopyReg = MRI.createVirtualRegister(PtrRC); | |||
34622 | unsigned RdsspOpc = (PVT == MVT::i64) ? X86::RDSSPQ : X86::RDSSPD; | |||
34623 | BuildMI(checkSspMBB, DL, TII->get(RdsspOpc), SSPCopyReg).addReg(ZReg); | |||
34624 | ||||
34625 | // Check whether the result of the SSP register is zero and jump directly | |||
34626 | // to the sink. | |||
34627 | unsigned TestRROpc = (PVT == MVT::i64) ? X86::TEST64rr : X86::TEST32rr; | |||
34628 | BuildMI(checkSspMBB, DL, TII->get(TestRROpc)) | |||
34629 | .addReg(SSPCopyReg) | |||
34630 | .addReg(SSPCopyReg); | |||
34631 | BuildMI(checkSspMBB, DL, TII->get(X86::JCC_1)).addMBB(sinkMBB).addImm(X86::COND_E); | |||
34632 | checkSspMBB->addSuccessor(sinkMBB); | |||
34633 | checkSspMBB->addSuccessor(fallMBB); | |||
34634 | ||||
34635 | // Reload the previously saved SSP register value. | |||
34636 | Register PrevSSPReg = MRI.createVirtualRegister(PtrRC); | |||
34637 | unsigned PtrLoadOpc = (PVT == MVT::i64) ? X86::MOV64rm : X86::MOV32rm; | |||
34638 | const int64_t SPPOffset = 3 * PVT.getStoreSize(); | |||
34639 | MachineInstrBuilder MIB = | |||
34640 | BuildMI(fallMBB, DL, TII->get(PtrLoadOpc), PrevSSPReg); | |||
34641 | for (unsigned i = 0; i < X86::AddrNumOperands; ++i) { | |||
34642 | const MachineOperand &MO = MI.getOperand(i); | |||
34643 | if (i == X86::AddrDisp) | |||
34644 | MIB.addDisp(MO, SPPOffset); | |||
34645 | else if (MO.isReg()) // Don't add the whole operand, we don't want to | |||
34646 | // preserve kill flags. | |||
34647 | MIB.addReg(MO.getReg()); | |||
34648 | else | |||
34649 | MIB.add(MO); | |||
34650 | } | |||
34651 | MIB.setMemRefs(MMOs); | |||
34652 | ||||
34653 | // Subtract the current SSP from the previous SSP. | |||
34654 | Register SspSubReg = MRI.createVirtualRegister(PtrRC); | |||
34655 | unsigned SubRROpc = (PVT == MVT::i64) ? X86::SUB64rr : X86::SUB32rr; | |||
34656 | BuildMI(fallMBB, DL, TII->get(SubRROpc), SspSubReg) | |||
34657 | .addReg(PrevSSPReg) | |||
34658 | .addReg(SSPCopyReg); | |||
34659 | ||||
34660 | // Jump to sink in case PrevSSPReg <= SSPCopyReg. | |||
34661 | BuildMI(fallMBB, DL, TII->get(X86::JCC_1)).addMBB(sinkMBB).addImm(X86::COND_BE); | |||
34662 | fallMBB->addSuccessor(sinkMBB); | |||
34663 | fallMBB->addSuccessor(fixShadowMBB); | |||
34664 | ||||
34665 | // Shift right by 2/3 for 32/64 because incssp multiplies the argument by 4/8. | |||
34666 | unsigned ShrRIOpc = (PVT == MVT::i64) ? X86::SHR64ri : X86::SHR32ri; | |||
34667 | unsigned Offset = (PVT == MVT::i64) ? 3 : 2; | |||
34668 | Register SspFirstShrReg = MRI.createVirtualRegister(PtrRC); | |||
34669 | BuildMI(fixShadowMBB, DL, TII->get(ShrRIOpc), SspFirstShrReg) | |||
34670 | .addReg(SspSubReg) | |||
34671 | .addImm(Offset); | |||
34672 | ||||
34673 | // Increase SSP when looking only on the lower 8 bits of the delta. | |||
34674 | unsigned IncsspOpc = (PVT == MVT::i64) ? X86::INCSSPQ : X86::INCSSPD; | |||
34675 | BuildMI(fixShadowMBB, DL, TII->get(IncsspOpc)).addReg(SspFirstShrReg); | |||
34676 | ||||
34677 | // Reset the lower 8 bits. | |||
34678 | Register SspSecondShrReg = MRI.createVirtualRegister(PtrRC); | |||
34679 | BuildMI(fixShadowMBB, DL, TII->get(ShrRIOpc), SspSecondShrReg) | |||
34680 | .addReg(SspFirstShrReg) | |||
34681 | .addImm(8); | |||
34682 | ||||
34683 | // Jump if the result of the shift is zero. | |||
34684 | BuildMI(fixShadowMBB, DL, TII->get(X86::JCC_1)).addMBB(sinkMBB).addImm(X86::COND_E); | |||
34685 | fixShadowMBB->addSuccessor(sinkMBB); | |||
34686 | fixShadowMBB->addSuccessor(fixShadowLoopPrepareMBB); | |||
34687 | ||||
34688 | // Do a single shift left. | |||
34689 | unsigned ShlR1Opc = (PVT == MVT::i64) ? X86::SHL64r1 : X86::SHL32r1; | |||
34690 | Register SspAfterShlReg = MRI.createVirtualRegister(PtrRC); | |||
34691 | BuildMI(fixShadowLoopPrepareMBB, DL, TII->get(ShlR1Opc), SspAfterShlReg) | |||
34692 | .addReg(SspSecondShrReg); | |||
34693 | ||||
34694 | // Save the value 128 to a register (will be used next with incssp). | |||
34695 | Register Value128InReg = MRI.createVirtualRegister(PtrRC); | |||
34696 | unsigned MovRIOpc = (PVT == MVT::i64) ? X86::MOV64ri32 : X86::MOV32ri; | |||
34697 | BuildMI(fixShadowLoopPrepareMBB, DL, TII->get(MovRIOpc), Value128InReg) | |||
34698 | .addImm(128); | |||
34699 | fixShadowLoopPrepareMBB->addSuccessor(fixShadowLoopMBB); | |||
34700 | ||||
34701 | // Since incssp only looks at the lower 8 bits, we might need to do several | |||
34702 | // iterations of incssp until we finish fixing the shadow stack. | |||
34703 | Register DecReg = MRI.createVirtualRegister(PtrRC); | |||
34704 | Register CounterReg = MRI.createVirtualRegister(PtrRC); | |||
34705 | BuildMI(fixShadowLoopMBB, DL, TII->get(X86::PHI), CounterReg) | |||
34706 | .addReg(SspAfterShlReg) | |||
34707 | .addMBB(fixShadowLoopPrepareMBB) | |||
34708 | .addReg(DecReg) | |||
34709 | .addMBB(fixShadowLoopMBB); | |||
34710 | ||||
34711 | // Every iteration we increase the SSP by 128. | |||
34712 | BuildMI(fixShadowLoopMBB, DL, TII->get(IncsspOpc)).addReg(Value128InReg); | |||
34713 | ||||
34714 | // Every iteration we decrement the counter by 1. | |||
34715 | unsigned DecROpc = (PVT == MVT::i64) ? X86::DEC64r : X86::DEC32r; | |||
34716 | BuildMI(fixShadowLoopMBB, DL, TII->get(DecROpc), DecReg).addReg(CounterReg); | |||
34717 | ||||
34718 | // Jump if the counter is not zero yet. | |||
34719 | BuildMI(fixShadowLoopMBB, DL, TII->get(X86::JCC_1)).addMBB(fixShadowLoopMBB).addImm(X86::COND_NE); | |||
34720 | fixShadowLoopMBB->addSuccessor(sinkMBB); | |||
34721 | fixShadowLoopMBB->addSuccessor(fixShadowLoopMBB); | |||
34722 | ||||
34723 | return sinkMBB; | |||
34724 | } | |||
34725 | ||||
34726 | MachineBasicBlock * | |||
34727 | X86TargetLowering::emitEHSjLjLongJmp(MachineInstr &MI, | |||
34728 | MachineBasicBlock *MBB) const { | |||
34729 | const DebugLoc &DL = MI.getDebugLoc(); | |||
34730 | MachineFunction *MF = MBB->getParent(); | |||
34731 | const TargetInstrInfo *TII = Subtarget.getInstrInfo(); | |||
34732 | MachineRegisterInfo &MRI = MF->getRegInfo(); | |||
34733 | ||||
34734 | // Memory Reference | |||
34735 | SmallVector<MachineMemOperand *, 2> MMOs(MI.memoperands_begin(), | |||
34736 | MI.memoperands_end()); | |||
34737 | ||||
34738 | MVT PVT = getPointerTy(MF->getDataLayout()); | |||
34739 | assert((PVT == MVT::i64 || PVT == MVT::i32) &&(static_cast <bool> ((PVT == MVT::i64 || PVT == MVT::i32 ) && "Invalid Pointer Size!") ? void (0) : __assert_fail ("(PVT == MVT::i64 || PVT == MVT::i32) && \"Invalid Pointer Size!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 34740, __extension__ __PRETTY_FUNCTION__)) | |||
34740 | "Invalid Pointer Size!")(static_cast <bool> ((PVT == MVT::i64 || PVT == MVT::i32 ) && "Invalid Pointer Size!") ? void (0) : __assert_fail ("(PVT == MVT::i64 || PVT == MVT::i32) && \"Invalid Pointer Size!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 34740, __extension__ __PRETTY_FUNCTION__)); | |||
34741 | ||||
34742 | const TargetRegisterClass *RC = | |||
34743 | (PVT == MVT::i64) ? &X86::GR64RegClass : &X86::GR32RegClass; | |||
34744 | Register Tmp = MRI.createVirtualRegister(RC); | |||
34745 | // Since FP is only updated here but NOT referenced, it's treated as GPR. | |||
34746 | const X86RegisterInfo *RegInfo = Subtarget.getRegisterInfo(); | |||
34747 | Register FP = (PVT == MVT::i64) ? X86::RBP : X86::EBP; | |||
34748 | Register SP = RegInfo->getStackRegister(); | |||
34749 | ||||
34750 | MachineInstrBuilder MIB; | |||
34751 | ||||
34752 | const int64_t LabelOffset = 1 * PVT.getStoreSize(); | |||
34753 | const int64_t SPOffset = 2 * PVT.getStoreSize(); | |||
34754 | ||||
34755 | unsigned PtrLoadOpc = (PVT == MVT::i64) ? X86::MOV64rm : X86::MOV32rm; | |||
34756 | unsigned IJmpOpc = (PVT == MVT::i64) ? X86::JMP64r : X86::JMP32r; | |||
34757 | ||||
34758 | MachineBasicBlock *thisMBB = MBB; | |||
34759 | ||||
34760 | // When CET and shadow stack is enabled, we need to fix the Shadow Stack. | |||
34761 | if (MF->getMMI().getModule()->getModuleFlag("cf-protection-return")) { | |||
34762 | thisMBB = emitLongJmpShadowStackFix(MI, thisMBB); | |||
34763 | } | |||
34764 | ||||
34765 | // Reload FP | |||
34766 | MIB = BuildMI(*thisMBB, MI, DL, TII->get(PtrLoadOpc), FP); | |||
34767 | for (unsigned i = 0; i < X86::AddrNumOperands; ++i) { | |||
34768 | const MachineOperand &MO = MI.getOperand(i); | |||
34769 | if (MO.isReg()) // Don't add the whole operand, we don't want to | |||
34770 | // preserve kill flags. | |||
34771 | MIB.addReg(MO.getReg()); | |||
34772 | else | |||
34773 | MIB.add(MO); | |||
34774 | } | |||
34775 | MIB.setMemRefs(MMOs); | |||
34776 | ||||
34777 | // Reload IP | |||
34778 | MIB = BuildMI(*thisMBB, MI, DL, TII->get(PtrLoadOpc), Tmp); | |||
34779 | for (unsigned i = 0; i < X86::AddrNumOperands; ++i) { | |||
34780 | const MachineOperand &MO = MI.getOperand(i); | |||
34781 | if (i == X86::AddrDisp) | |||
34782 | MIB.addDisp(MO, LabelOffset); | |||
34783 | else if (MO.isReg()) // Don't add the whole operand, we don't want to | |||
34784 | // preserve kill flags. | |||
34785 | MIB.addReg(MO.getReg()); | |||
34786 | else | |||
34787 | MIB.add(MO); | |||
34788 | } | |||
34789 | MIB.setMemRefs(MMOs); | |||
34790 | ||||
34791 | // Reload SP | |||
34792 | MIB = BuildMI(*thisMBB, MI, DL, TII->get(PtrLoadOpc), SP); | |||
34793 | for (unsigned i = 0; i < X86::AddrNumOperands; ++i) { | |||
34794 | if (i == X86::AddrDisp) | |||
34795 | MIB.addDisp(MI.getOperand(i), SPOffset); | |||
34796 | else | |||
34797 | MIB.add(MI.getOperand(i)); // We can preserve the kill flags here, it's | |||
34798 | // the last instruction of the expansion. | |||
34799 | } | |||
34800 | MIB.setMemRefs(MMOs); | |||
34801 | ||||
34802 | // Jump | |||
34803 | BuildMI(*thisMBB, MI, DL, TII->get(IJmpOpc)).addReg(Tmp); | |||
34804 | ||||
34805 | MI.eraseFromParent(); | |||
34806 | return thisMBB; | |||
34807 | } | |||
34808 | ||||
34809 | void X86TargetLowering::SetupEntryBlockForSjLj(MachineInstr &MI, | |||
34810 | MachineBasicBlock *MBB, | |||
34811 | MachineBasicBlock *DispatchBB, | |||
34812 | int FI) const { | |||
34813 | const DebugLoc &DL = MI.getDebugLoc(); | |||
34814 | MachineFunction *MF = MBB->getParent(); | |||
34815 | MachineRegisterInfo *MRI = &MF->getRegInfo(); | |||
34816 | const X86InstrInfo *TII = Subtarget.getInstrInfo(); | |||
34817 | ||||
34818 | MVT PVT = getPointerTy(MF->getDataLayout()); | |||
34819 | assert((PVT == MVT::i64 || PVT == MVT::i32) && "Invalid Pointer Size!")(static_cast <bool> ((PVT == MVT::i64 || PVT == MVT::i32 ) && "Invalid Pointer Size!") ? void (0) : __assert_fail ("(PVT == MVT::i64 || PVT == MVT::i32) && \"Invalid Pointer Size!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 34819, __extension__ __PRETTY_FUNCTION__)); | |||
34820 | ||||
34821 | unsigned Op = 0; | |||
34822 | unsigned VR = 0; | |||
34823 | ||||
34824 | bool UseImmLabel = (MF->getTarget().getCodeModel() == CodeModel::Small) && | |||
34825 | !isPositionIndependent(); | |||
34826 | ||||
34827 | if (UseImmLabel) { | |||
34828 | Op = (PVT == MVT::i64) ? X86::MOV64mi32 : X86::MOV32mi; | |||
34829 | } else { | |||
34830 | const TargetRegisterClass *TRC = | |||
34831 | (PVT == MVT::i64) ? &X86::GR64RegClass : &X86::GR32RegClass; | |||
34832 | VR = MRI->createVirtualRegister(TRC); | |||
34833 | Op = (PVT == MVT::i64) ? X86::MOV64mr : X86::MOV32mr; | |||
34834 | ||||
34835 | if (Subtarget.is64Bit()) | |||
34836 | BuildMI(*MBB, MI, DL, TII->get(X86::LEA64r), VR) | |||
34837 | .addReg(X86::RIP) | |||
34838 | .addImm(1) | |||
34839 | .addReg(0) | |||
34840 | .addMBB(DispatchBB) | |||
34841 | .addReg(0); | |||
34842 | else | |||
34843 | BuildMI(*MBB, MI, DL, TII->get(X86::LEA32r), VR) | |||
34844 | .addReg(0) /* TII->getGlobalBaseReg(MF) */ | |||
34845 | .addImm(1) | |||
34846 | .addReg(0) | |||
34847 | .addMBB(DispatchBB, Subtarget.classifyBlockAddressReference()) | |||
34848 | .addReg(0); | |||
34849 | } | |||
34850 | ||||
34851 | MachineInstrBuilder MIB = BuildMI(*MBB, MI, DL, TII->get(Op)); | |||
34852 | addFrameReference(MIB, FI, Subtarget.is64Bit() ? 56 : 36); | |||
34853 | if (UseImmLabel) | |||
34854 | MIB.addMBB(DispatchBB); | |||
34855 | else | |||
34856 | MIB.addReg(VR); | |||
34857 | } | |||
34858 | ||||
34859 | MachineBasicBlock * | |||
34860 | X86TargetLowering::EmitSjLjDispatchBlock(MachineInstr &MI, | |||
34861 | MachineBasicBlock *BB) const { | |||
34862 | const DebugLoc &DL = MI.getDebugLoc(); | |||
34863 | MachineFunction *MF = BB->getParent(); | |||
34864 | MachineRegisterInfo *MRI = &MF->getRegInfo(); | |||
34865 | const X86InstrInfo *TII = Subtarget.getInstrInfo(); | |||
34866 | int FI = MF->getFrameInfo().getFunctionContextIndex(); | |||
34867 | ||||
34868 | // Get a mapping of the call site numbers to all of the landing pads they're | |||
34869 | // associated with. | |||
34870 | DenseMap<unsigned, SmallVector<MachineBasicBlock *, 2>> CallSiteNumToLPad; | |||
34871 | unsigned MaxCSNum = 0; | |||
34872 | for (auto &MBB : *MF) { | |||
34873 | if (!MBB.isEHPad()) | |||
34874 | continue; | |||
34875 | ||||
34876 | MCSymbol *Sym = nullptr; | |||
34877 | for (const auto &MI : MBB) { | |||
34878 | if (MI.isDebugInstr()) | |||
34879 | continue; | |||
34880 | ||||
34881 | assert(MI.isEHLabel() && "expected EH_LABEL")(static_cast <bool> (MI.isEHLabel() && "expected EH_LABEL" ) ? void (0) : __assert_fail ("MI.isEHLabel() && \"expected EH_LABEL\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 34881, __extension__ __PRETTY_FUNCTION__)); | |||
34882 | Sym = MI.getOperand(0).getMCSymbol(); | |||
34883 | break; | |||
34884 | } | |||
34885 | ||||
34886 | if (!MF->hasCallSiteLandingPad(Sym)) | |||
34887 | continue; | |||
34888 | ||||
34889 | for (unsigned CSI : MF->getCallSiteLandingPad(Sym)) { | |||
34890 | CallSiteNumToLPad[CSI].push_back(&MBB); | |||
34891 | MaxCSNum = std::max(MaxCSNum, CSI); | |||
34892 | } | |||
34893 | } | |||
34894 | ||||
34895 | // Get an ordered list of the machine basic blocks for the jump table. | |||
34896 | std::vector<MachineBasicBlock *> LPadList; | |||
34897 | SmallPtrSet<MachineBasicBlock *, 32> InvokeBBs; | |||
34898 | LPadList.reserve(CallSiteNumToLPad.size()); | |||
34899 | ||||
34900 | for (unsigned CSI = 1; CSI <= MaxCSNum; ++CSI) { | |||
34901 | for (auto &LP : CallSiteNumToLPad[CSI]) { | |||
34902 | LPadList.push_back(LP); | |||
34903 | InvokeBBs.insert(LP->pred_begin(), LP->pred_end()); | |||
34904 | } | |||
34905 | } | |||
34906 | ||||
34907 | assert(!LPadList.empty() &&(static_cast <bool> (!LPadList.empty() && "No landing pad destinations for the dispatch jump table!" ) ? void (0) : __assert_fail ("!LPadList.empty() && \"No landing pad destinations for the dispatch jump table!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 34908, __extension__ __PRETTY_FUNCTION__)) | |||
34908 | "No landing pad destinations for the dispatch jump table!")(static_cast <bool> (!LPadList.empty() && "No landing pad destinations for the dispatch jump table!" ) ? void (0) : __assert_fail ("!LPadList.empty() && \"No landing pad destinations for the dispatch jump table!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 34908, __extension__ __PRETTY_FUNCTION__)); | |||
34909 | ||||
34910 | // Create the MBBs for the dispatch code. | |||
34911 | ||||
34912 | // Shove the dispatch's address into the return slot in the function context. | |||
34913 | MachineBasicBlock *DispatchBB = MF->CreateMachineBasicBlock(); | |||
34914 | DispatchBB->setIsEHPad(true); | |||
34915 | ||||
34916 | MachineBasicBlock *TrapBB = MF->CreateMachineBasicBlock(); | |||
34917 | BuildMI(TrapBB, DL, TII->get(X86::TRAP)); | |||
34918 | DispatchBB->addSuccessor(TrapBB); | |||
34919 | ||||
34920 | MachineBasicBlock *DispContBB = MF->CreateMachineBasicBlock(); | |||
34921 | DispatchBB->addSuccessor(DispContBB); | |||
34922 | ||||
34923 | // Insert MBBs. | |||
34924 | MF->push_back(DispatchBB); | |||
34925 | MF->push_back(DispContBB); | |||
34926 | MF->push_back(TrapBB); | |||
34927 | ||||
34928 | // Insert code into the entry block that creates and registers the function | |||
34929 | // context. | |||
34930 | SetupEntryBlockForSjLj(MI, BB, DispatchBB, FI); | |||
34931 | ||||
34932 | // Create the jump table and associated information | |||
34933 | unsigned JTE = getJumpTableEncoding(); | |||
34934 | MachineJumpTableInfo *JTI = MF->getOrCreateJumpTableInfo(JTE); | |||
34935 | unsigned MJTI = JTI->createJumpTableIndex(LPadList); | |||
34936 | ||||
34937 | const X86RegisterInfo &RI = TII->getRegisterInfo(); | |||
34938 | // Add a register mask with no preserved registers. This results in all | |||
34939 | // registers being marked as clobbered. | |||
34940 | if (RI.hasBasePointer(*MF)) { | |||
34941 | const bool FPIs64Bit = | |||
34942 | Subtarget.isTarget64BitLP64() || Subtarget.isTargetNaCl64(); | |||
34943 | X86MachineFunctionInfo *MFI = MF->getInfo<X86MachineFunctionInfo>(); | |||
34944 | MFI->setRestoreBasePointer(MF); | |||
34945 | ||||
34946 | Register FP = RI.getFrameRegister(*MF); | |||
34947 | Register BP = RI.getBaseRegister(); | |||
34948 | unsigned Op = FPIs64Bit ? X86::MOV64rm : X86::MOV32rm; | |||
34949 | addRegOffset(BuildMI(DispatchBB, DL, TII->get(Op), BP), FP, true, | |||
34950 | MFI->getRestoreBasePointerOffset()) | |||
34951 | .addRegMask(RI.getNoPreservedMask()); | |||
34952 | } else { | |||
34953 | BuildMI(DispatchBB, DL, TII->get(X86::NOOP)) | |||
34954 | .addRegMask(RI.getNoPreservedMask()); | |||
34955 | } | |||
34956 | ||||
34957 | // IReg is used as an index in a memory operand and therefore can't be SP | |||
34958 | Register IReg = MRI->createVirtualRegister(&X86::GR32_NOSPRegClass); | |||
34959 | addFrameReference(BuildMI(DispatchBB, DL, TII->get(X86::MOV32rm), IReg), FI, | |||
34960 | Subtarget.is64Bit() ? 8 : 4); | |||
34961 | BuildMI(DispatchBB, DL, TII->get(X86::CMP32ri)) | |||
34962 | .addReg(IReg) | |||
34963 | .addImm(LPadList.size()); | |||
34964 | BuildMI(DispatchBB, DL, TII->get(X86::JCC_1)).addMBB(TrapBB).addImm(X86::COND_AE); | |||
34965 | ||||
34966 | if (Subtarget.is64Bit()) { | |||
34967 | Register BReg = MRI->createVirtualRegister(&X86::GR64RegClass); | |||
34968 | Register IReg64 = MRI->createVirtualRegister(&X86::GR64_NOSPRegClass); | |||
34969 | ||||
34970 | // leaq .LJTI0_0(%rip), BReg | |||
34971 | BuildMI(DispContBB, DL, TII->get(X86::LEA64r), BReg) | |||
34972 | .addReg(X86::RIP) | |||
34973 | .addImm(1) | |||
34974 | .addReg(0) | |||
34975 | .addJumpTableIndex(MJTI) | |||
34976 | .addReg(0); | |||
34977 | // movzx IReg64, IReg | |||
34978 | BuildMI(DispContBB, DL, TII->get(TargetOpcode::SUBREG_TO_REG), IReg64) | |||
34979 | .addImm(0) | |||
34980 | .addReg(IReg) | |||
34981 | .addImm(X86::sub_32bit); | |||
34982 | ||||
34983 | switch (JTE) { | |||
34984 | case MachineJumpTableInfo::EK_BlockAddress: | |||
34985 | // jmpq *(BReg,IReg64,8) | |||
34986 | BuildMI(DispContBB, DL, TII->get(X86::JMP64m)) | |||
34987 | .addReg(BReg) | |||
34988 | .addImm(8) | |||
34989 | .addReg(IReg64) | |||
34990 | .addImm(0) | |||
34991 | .addReg(0); | |||
34992 | break; | |||
34993 | case MachineJumpTableInfo::EK_LabelDifference32: { | |||
34994 | Register OReg = MRI->createVirtualRegister(&X86::GR32RegClass); | |||
34995 | Register OReg64 = MRI->createVirtualRegister(&X86::GR64RegClass); | |||
34996 | Register TReg = MRI->createVirtualRegister(&X86::GR64RegClass); | |||
34997 | ||||
34998 | // movl (BReg,IReg64,4), OReg | |||
34999 | BuildMI(DispContBB, DL, TII->get(X86::MOV32rm), OReg) | |||
35000 | .addReg(BReg) | |||
35001 | .addImm(4) | |||
35002 | .addReg(IReg64) | |||
35003 | .addImm(0) | |||
35004 | .addReg(0); | |||
35005 | // movsx OReg64, OReg | |||
35006 | BuildMI(DispContBB, DL, TII->get(X86::MOVSX64rr32), OReg64).addReg(OReg); | |||
35007 | // addq BReg, OReg64, TReg | |||
35008 | BuildMI(DispContBB, DL, TII->get(X86::ADD64rr), TReg) | |||
35009 | .addReg(OReg64) | |||
35010 | .addReg(BReg); | |||
35011 | // jmpq *TReg | |||
35012 | BuildMI(DispContBB, DL, TII->get(X86::JMP64r)).addReg(TReg); | |||
35013 | break; | |||
35014 | } | |||
35015 | default: | |||
35016 | llvm_unreachable("Unexpected jump table encoding")::llvm::llvm_unreachable_internal("Unexpected jump table encoding" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 35016); | |||
35017 | } | |||
35018 | } else { | |||
35019 | // jmpl *.LJTI0_0(,IReg,4) | |||
35020 | BuildMI(DispContBB, DL, TII->get(X86::JMP32m)) | |||
35021 | .addReg(0) | |||
35022 | .addImm(4) | |||
35023 | .addReg(IReg) | |||
35024 | .addJumpTableIndex(MJTI) | |||
35025 | .addReg(0); | |||
35026 | } | |||
35027 | ||||
35028 | // Add the jump table entries as successors to the MBB. | |||
35029 | SmallPtrSet<MachineBasicBlock *, 8> SeenMBBs; | |||
35030 | for (auto &LP : LPadList) | |||
35031 | if (SeenMBBs.insert(LP).second) | |||
35032 | DispContBB->addSuccessor(LP); | |||
35033 | ||||
35034 | // N.B. the order the invoke BBs are processed in doesn't matter here. | |||
35035 | SmallVector<MachineBasicBlock *, 64> MBBLPads; | |||
35036 | const MCPhysReg *SavedRegs = MF->getRegInfo().getCalleeSavedRegs(); | |||
35037 | for (MachineBasicBlock *MBB : InvokeBBs) { | |||
35038 | // Remove the landing pad successor from the invoke block and replace it | |||
35039 | // with the new dispatch block. | |||
35040 | // Keep a copy of Successors since it's modified inside the loop. | |||
35041 | SmallVector<MachineBasicBlock *, 8> Successors(MBB->succ_rbegin(), | |||
35042 | MBB->succ_rend()); | |||
35043 | // FIXME: Avoid quadratic complexity. | |||
35044 | for (auto MBBS : Successors) { | |||
35045 | if (MBBS->isEHPad()) { | |||
35046 | MBB->removeSuccessor(MBBS); | |||
35047 | MBBLPads.push_back(MBBS); | |||
35048 | } | |||
35049 | } | |||
35050 | ||||
35051 | MBB->addSuccessor(DispatchBB); | |||
35052 | ||||
35053 | // Find the invoke call and mark all of the callee-saved registers as | |||
35054 | // 'implicit defined' so that they're spilled. This prevents code from | |||
35055 | // moving instructions to before the EH block, where they will never be | |||
35056 | // executed. | |||
35057 | for (auto &II : reverse(*MBB)) { | |||
35058 | if (!II.isCall()) | |||
35059 | continue; | |||
35060 | ||||
35061 | DenseMap<unsigned, bool> DefRegs; | |||
35062 | for (auto &MOp : II.operands()) | |||
35063 | if (MOp.isReg()) | |||
35064 | DefRegs[MOp.getReg()] = true; | |||
35065 | ||||
35066 | MachineInstrBuilder MIB(*MF, &II); | |||
35067 | for (unsigned RegIdx = 0; SavedRegs[RegIdx]; ++RegIdx) { | |||
35068 | unsigned Reg = SavedRegs[RegIdx]; | |||
35069 | if (!DefRegs[Reg]) | |||
35070 | MIB.addReg(Reg, RegState::ImplicitDefine | RegState::Dead); | |||
35071 | } | |||
35072 | ||||
35073 | break; | |||
35074 | } | |||
35075 | } | |||
35076 | ||||
35077 | // Mark all former landing pads as non-landing pads. The dispatch is the only | |||
35078 | // landing pad now. | |||
35079 | for (auto &LP : MBBLPads) | |||
35080 | LP->setIsEHPad(false); | |||
35081 | ||||
35082 | // The instruction is gone now. | |||
35083 | MI.eraseFromParent(); | |||
35084 | return BB; | |||
35085 | } | |||
35086 | ||||
35087 | MachineBasicBlock * | |||
35088 | X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr &MI, | |||
35089 | MachineBasicBlock *BB) const { | |||
35090 | MachineFunction *MF = BB->getParent(); | |||
35091 | const TargetInstrInfo *TII = Subtarget.getInstrInfo(); | |||
35092 | const DebugLoc &DL = MI.getDebugLoc(); | |||
35093 | ||||
35094 | auto TMMImmToTMMReg = [](unsigned Imm) { | |||
35095 | assert (Imm < 8 && "Illegal tmm index")(static_cast <bool> (Imm < 8 && "Illegal tmm index" ) ? void (0) : __assert_fail ("Imm < 8 && \"Illegal tmm index\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 35095, __extension__ __PRETTY_FUNCTION__)); | |||
35096 | return X86::TMM0 + Imm; | |||
35097 | }; | |||
35098 | switch (MI.getOpcode()) { | |||
35099 | default: llvm_unreachable("Unexpected instr type to insert")::llvm::llvm_unreachable_internal("Unexpected instr type to insert" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 35099); | |||
35100 | case X86::TLS_addr32: | |||
35101 | case X86::TLS_addr64: | |||
35102 | case X86::TLS_addrX32: | |||
35103 | case X86::TLS_base_addr32: | |||
35104 | case X86::TLS_base_addr64: | |||
35105 | case X86::TLS_base_addrX32: | |||
35106 | return EmitLoweredTLSAddr(MI, BB); | |||
35107 | case X86::INDIRECT_THUNK_CALL32: | |||
35108 | case X86::INDIRECT_THUNK_CALL64: | |||
35109 | case X86::INDIRECT_THUNK_TCRETURN32: | |||
35110 | case X86::INDIRECT_THUNK_TCRETURN64: | |||
35111 | return EmitLoweredIndirectThunk(MI, BB); | |||
35112 | case X86::CATCHRET: | |||
35113 | return EmitLoweredCatchRet(MI, BB); | |||
35114 | case X86::SEG_ALLOCA_32: | |||
35115 | case X86::SEG_ALLOCA_64: | |||
35116 | return EmitLoweredSegAlloca(MI, BB); | |||
35117 | case X86::PROBED_ALLOCA_32: | |||
35118 | case X86::PROBED_ALLOCA_64: | |||
35119 | return EmitLoweredProbedAlloca(MI, BB); | |||
35120 | case X86::TLSCall_32: | |||
35121 | case X86::TLSCall_64: | |||
35122 | return EmitLoweredTLSCall(MI, BB); | |||
35123 | case X86::CMOV_FR32: | |||
35124 | case X86::CMOV_FR32X: | |||
35125 | case X86::CMOV_FR64: | |||
35126 | case X86::CMOV_FR64X: | |||
35127 | case X86::CMOV_GR8: | |||
35128 | case X86::CMOV_GR16: | |||
35129 | case X86::CMOV_GR32: | |||
35130 | case X86::CMOV_RFP32: | |||
35131 | case X86::CMOV_RFP64: | |||
35132 | case X86::CMOV_RFP80: | |||
35133 | case X86::CMOV_VR64: | |||
35134 | case X86::CMOV_VR128: | |||
35135 | case X86::CMOV_VR128X: | |||
35136 | case X86::CMOV_VR256: | |||
35137 | case X86::CMOV_VR256X: | |||
35138 | case X86::CMOV_VR512: | |||
35139 | case X86::CMOV_VK1: | |||
35140 | case X86::CMOV_VK2: | |||
35141 | case X86::CMOV_VK4: | |||
35142 | case X86::CMOV_VK8: | |||
35143 | case X86::CMOV_VK16: | |||
35144 | case X86::CMOV_VK32: | |||
35145 | case X86::CMOV_VK64: | |||
35146 | return EmitLoweredSelect(MI, BB); | |||
35147 | ||||
35148 | case X86::RDFLAGS32: | |||
35149 | case X86::RDFLAGS64: { | |||
35150 | unsigned PushF = | |||
35151 | MI.getOpcode() == X86::RDFLAGS32 ? X86::PUSHF32 : X86::PUSHF64; | |||
35152 | unsigned Pop = MI.getOpcode() == X86::RDFLAGS32 ? X86::POP32r : X86::POP64r; | |||
35153 | MachineInstr *Push = BuildMI(*BB, MI, DL, TII->get(PushF)); | |||
35154 | // Permit reads of the EFLAGS and DF registers without them being defined. | |||
35155 | // This intrinsic exists to read external processor state in flags, such as | |||
35156 | // the trap flag, interrupt flag, and direction flag, none of which are | |||
35157 | // modeled by the backend. | |||
35158 | assert(Push->getOperand(2).getReg() == X86::EFLAGS &&(static_cast <bool> (Push->getOperand(2).getReg() == X86::EFLAGS && "Unexpected register in operand!") ? void (0) : __assert_fail ("Push->getOperand(2).getReg() == X86::EFLAGS && \"Unexpected register in operand!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 35159, __extension__ __PRETTY_FUNCTION__)) | |||
35159 | "Unexpected register in operand!")(static_cast <bool> (Push->getOperand(2).getReg() == X86::EFLAGS && "Unexpected register in operand!") ? void (0) : __assert_fail ("Push->getOperand(2).getReg() == X86::EFLAGS && \"Unexpected register in operand!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 35159, __extension__ __PRETTY_FUNCTION__)); | |||
35160 | Push->getOperand(2).setIsUndef(); | |||
35161 | assert(Push->getOperand(3).getReg() == X86::DF &&(static_cast <bool> (Push->getOperand(3).getReg() == X86::DF && "Unexpected register in operand!") ? void (0) : __assert_fail ("Push->getOperand(3).getReg() == X86::DF && \"Unexpected register in operand!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 35162, __extension__ __PRETTY_FUNCTION__)) | |||
35162 | "Unexpected register in operand!")(static_cast <bool> (Push->getOperand(3).getReg() == X86::DF && "Unexpected register in operand!") ? void (0) : __assert_fail ("Push->getOperand(3).getReg() == X86::DF && \"Unexpected register in operand!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 35162, __extension__ __PRETTY_FUNCTION__)); | |||
35163 | Push->getOperand(3).setIsUndef(); | |||
35164 | BuildMI(*BB, MI, DL, TII->get(Pop), MI.getOperand(0).getReg()); | |||
35165 | ||||
35166 | MI.eraseFromParent(); // The pseudo is gone now. | |||
35167 | return BB; | |||
35168 | } | |||
35169 | ||||
35170 | case X86::WRFLAGS32: | |||
35171 | case X86::WRFLAGS64: { | |||
35172 | unsigned Push = | |||
35173 | MI.getOpcode() == X86::WRFLAGS32 ? X86::PUSH32r : X86::PUSH64r; | |||
35174 | unsigned PopF = | |||
35175 | MI.getOpcode() == X86::WRFLAGS32 ? X86::POPF32 : X86::POPF64; | |||
35176 | BuildMI(*BB, MI, DL, TII->get(Push)).addReg(MI.getOperand(0).getReg()); | |||
35177 | BuildMI(*BB, MI, DL, TII->get(PopF)); | |||
35178 | ||||
35179 | MI.eraseFromParent(); // The pseudo is gone now. | |||
35180 | return BB; | |||
35181 | } | |||
35182 | ||||
35183 | case X86::FP32_TO_INT16_IN_MEM: | |||
35184 | case X86::FP32_TO_INT32_IN_MEM: | |||
35185 | case X86::FP32_TO_INT64_IN_MEM: | |||
35186 | case X86::FP64_TO_INT16_IN_MEM: | |||
35187 | case X86::FP64_TO_INT32_IN_MEM: | |||
35188 | case X86::FP64_TO_INT64_IN_MEM: | |||
35189 | case X86::FP80_TO_INT16_IN_MEM: | |||
35190 | case X86::FP80_TO_INT32_IN_MEM: | |||
35191 | case X86::FP80_TO_INT64_IN_MEM: { | |||
35192 | // Change the floating point control register to use "round towards zero" | |||
35193 | // mode when truncating to an integer value. | |||
35194 | int OrigCWFrameIdx = | |||
35195 | MF->getFrameInfo().CreateStackObject(2, Align(2), false); | |||
35196 | addFrameReference(BuildMI(*BB, MI, DL, | |||
35197 | TII->get(X86::FNSTCW16m)), OrigCWFrameIdx); | |||
35198 | ||||
35199 | // Load the old value of the control word... | |||
35200 | Register OldCW = MF->getRegInfo().createVirtualRegister(&X86::GR32RegClass); | |||
35201 | addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOVZX32rm16), OldCW), | |||
35202 | OrigCWFrameIdx); | |||
35203 | ||||
35204 | // OR 0b11 into bit 10 and 11. 0b11 is the encoding for round toward zero. | |||
35205 | Register NewCW = MF->getRegInfo().createVirtualRegister(&X86::GR32RegClass); | |||
35206 | BuildMI(*BB, MI, DL, TII->get(X86::OR32ri), NewCW) | |||
35207 | .addReg(OldCW, RegState::Kill).addImm(0xC00); | |||
35208 | ||||
35209 | // Extract to 16 bits. | |||
35210 | Register NewCW16 = | |||
35211 | MF->getRegInfo().createVirtualRegister(&X86::GR16RegClass); | |||
35212 | BuildMI(*BB, MI, DL, TII->get(TargetOpcode::COPY), NewCW16) | |||
35213 | .addReg(NewCW, RegState::Kill, X86::sub_16bit); | |||
35214 | ||||
35215 | // Prepare memory for FLDCW. | |||
35216 | int NewCWFrameIdx = | |||
35217 | MF->getFrameInfo().CreateStackObject(2, Align(2), false); | |||
35218 | addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), | |||
35219 | NewCWFrameIdx) | |||
35220 | .addReg(NewCW16, RegState::Kill); | |||
35221 | ||||
35222 | // Reload the modified control word now... | |||
35223 | addFrameReference(BuildMI(*BB, MI, DL, | |||
35224 | TII->get(X86::FLDCW16m)), NewCWFrameIdx); | |||
35225 | ||||
35226 | // Get the X86 opcode to use. | |||
35227 | unsigned Opc; | |||
35228 | switch (MI.getOpcode()) { | |||
35229 | default: llvm_unreachable("illegal opcode!")::llvm::llvm_unreachable_internal("illegal opcode!", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 35229); | |||
35230 | case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break; | |||
35231 | case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break; | |||
35232 | case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break; | |||
35233 | case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break; | |||
35234 | case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break; | |||
35235 | case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break; | |||
35236 | case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break; | |||
35237 | case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break; | |||
35238 | case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break; | |||
35239 | } | |||
35240 | ||||
35241 | X86AddressMode AM = getAddressFromInstr(&MI, 0); | |||
35242 | addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM) | |||
35243 | .addReg(MI.getOperand(X86::AddrNumOperands).getReg()); | |||
35244 | ||||
35245 | // Reload the original control word now. | |||
35246 | addFrameReference(BuildMI(*BB, MI, DL, | |||
35247 | TII->get(X86::FLDCW16m)), OrigCWFrameIdx); | |||
35248 | ||||
35249 | MI.eraseFromParent(); // The pseudo instruction is gone now. | |||
35250 | return BB; | |||
35251 | } | |||
35252 | ||||
35253 | // xbegin | |||
35254 | case X86::XBEGIN: | |||
35255 | return emitXBegin(MI, BB, Subtarget.getInstrInfo()); | |||
35256 | ||||
35257 | case X86::VAARG_64: | |||
35258 | case X86::VAARG_X32: | |||
35259 | return EmitVAARGWithCustomInserter(MI, BB); | |||
35260 | ||||
35261 | case X86::EH_SjLj_SetJmp32: | |||
35262 | case X86::EH_SjLj_SetJmp64: | |||
35263 | return emitEHSjLjSetJmp(MI, BB); | |||
35264 | ||||
35265 | case X86::EH_SjLj_LongJmp32: | |||
35266 | case X86::EH_SjLj_LongJmp64: | |||
35267 | return emitEHSjLjLongJmp(MI, BB); | |||
35268 | ||||
35269 | case X86::Int_eh_sjlj_setup_dispatch: | |||
35270 | return EmitSjLjDispatchBlock(MI, BB); | |||
35271 | ||||
35272 | case TargetOpcode::STATEPOINT: | |||
35273 | // As an implementation detail, STATEPOINT shares the STACKMAP format at | |||
35274 | // this point in the process. We diverge later. | |||
35275 | return emitPatchPoint(MI, BB); | |||
35276 | ||||
35277 | case TargetOpcode::STACKMAP: | |||
35278 | case TargetOpcode::PATCHPOINT: | |||
35279 | return emitPatchPoint(MI, BB); | |||
35280 | ||||
35281 | case TargetOpcode::PATCHABLE_EVENT_CALL: | |||
35282 | case TargetOpcode::PATCHABLE_TYPED_EVENT_CALL: | |||
35283 | return BB; | |||
35284 | ||||
35285 | case X86::LCMPXCHG8B: { | |||
35286 | const X86RegisterInfo *TRI = Subtarget.getRegisterInfo(); | |||
35287 | // In addition to 4 E[ABCD] registers implied by encoding, CMPXCHG8B | |||
35288 | // requires a memory operand. If it happens that current architecture is | |||
35289 | // i686 and for current function we need a base pointer | |||
35290 | // - which is ESI for i686 - register allocator would not be able to | |||
35291 | // allocate registers for an address in form of X(%reg, %reg, Y) | |||
35292 | // - there never would be enough unreserved registers during regalloc | |||
35293 | // (without the need for base ptr the only option would be X(%edi, %esi, Y). | |||
35294 | // We are giving a hand to register allocator by precomputing the address in | |||
35295 | // a new vreg using LEA. | |||
35296 | ||||
35297 | // If it is not i686 or there is no base pointer - nothing to do here. | |||
35298 | if (!Subtarget.is32Bit() || !TRI->hasBasePointer(*MF)) | |||
35299 | return BB; | |||
35300 | ||||
35301 | // Even though this code does not necessarily needs the base pointer to | |||
35302 | // be ESI, we check for that. The reason: if this assert fails, there are | |||
35303 | // some changes happened in the compiler base pointer handling, which most | |||
35304 | // probably have to be addressed somehow here. | |||
35305 | assert(TRI->getBaseRegister() == X86::ESI &&(static_cast <bool> (TRI->getBaseRegister() == X86:: ESI && "LCMPXCHG8B custom insertion for i686 is written with X86::ESI as a " "base pointer in mind") ? void (0) : __assert_fail ("TRI->getBaseRegister() == X86::ESI && \"LCMPXCHG8B custom insertion for i686 is written with X86::ESI as a \" \"base pointer in mind\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 35307, __extension__ __PRETTY_FUNCTION__)) | |||
35306 | "LCMPXCHG8B custom insertion for i686 is written with X86::ESI as a "(static_cast <bool> (TRI->getBaseRegister() == X86:: ESI && "LCMPXCHG8B custom insertion for i686 is written with X86::ESI as a " "base pointer in mind") ? void (0) : __assert_fail ("TRI->getBaseRegister() == X86::ESI && \"LCMPXCHG8B custom insertion for i686 is written with X86::ESI as a \" \"base pointer in mind\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 35307, __extension__ __PRETTY_FUNCTION__)) | |||
35307 | "base pointer in mind")(static_cast <bool> (TRI->getBaseRegister() == X86:: ESI && "LCMPXCHG8B custom insertion for i686 is written with X86::ESI as a " "base pointer in mind") ? void (0) : __assert_fail ("TRI->getBaseRegister() == X86::ESI && \"LCMPXCHG8B custom insertion for i686 is written with X86::ESI as a \" \"base pointer in mind\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 35307, __extension__ __PRETTY_FUNCTION__)); | |||
35308 | ||||
35309 | MachineRegisterInfo &MRI = MF->getRegInfo(); | |||
35310 | MVT SPTy = getPointerTy(MF->getDataLayout()); | |||
35311 | const TargetRegisterClass *AddrRegClass = getRegClassFor(SPTy); | |||
35312 | Register computedAddrVReg = MRI.createVirtualRegister(AddrRegClass); | |||
35313 | ||||
35314 | X86AddressMode AM = getAddressFromInstr(&MI, 0); | |||
35315 | // Regalloc does not need any help when the memory operand of CMPXCHG8B | |||
35316 | // does not use index register. | |||
35317 | if (AM.IndexReg == X86::NoRegister) | |||
35318 | return BB; | |||
35319 | ||||
35320 | // After X86TargetLowering::ReplaceNodeResults CMPXCHG8B is glued to its | |||
35321 | // four operand definitions that are E[ABCD] registers. We skip them and | |||
35322 | // then insert the LEA. | |||
35323 | MachineBasicBlock::reverse_iterator RMBBI(MI.getReverseIterator()); | |||
35324 | while (RMBBI != BB->rend() && (RMBBI->definesRegister(X86::EAX) || | |||
35325 | RMBBI->definesRegister(X86::EBX) || | |||
35326 | RMBBI->definesRegister(X86::ECX) || | |||
35327 | RMBBI->definesRegister(X86::EDX))) { | |||
35328 | ++RMBBI; | |||
35329 | } | |||
35330 | MachineBasicBlock::iterator MBBI(RMBBI); | |||
35331 | addFullAddress( | |||
35332 | BuildMI(*BB, *MBBI, DL, TII->get(X86::LEA32r), computedAddrVReg), AM); | |||
35333 | ||||
35334 | setDirectAddressInInstr(&MI, 0, computedAddrVReg); | |||
35335 | ||||
35336 | return BB; | |||
35337 | } | |||
35338 | case X86::LCMPXCHG16B_NO_RBX: { | |||
35339 | const X86RegisterInfo *TRI = Subtarget.getRegisterInfo(); | |||
35340 | Register BasePtr = TRI->getBaseRegister(); | |||
35341 | if (TRI->hasBasePointer(*MF) && | |||
35342 | (BasePtr == X86::RBX || BasePtr == X86::EBX)) { | |||
35343 | if (!BB->isLiveIn(BasePtr)) | |||
35344 | BB->addLiveIn(BasePtr); | |||
35345 | // Save RBX into a virtual register. | |||
35346 | Register SaveRBX = | |||
35347 | MF->getRegInfo().createVirtualRegister(&X86::GR64RegClass); | |||
35348 | BuildMI(*BB, MI, DL, TII->get(TargetOpcode::COPY), SaveRBX) | |||
35349 | .addReg(X86::RBX); | |||
35350 | Register Dst = MF->getRegInfo().createVirtualRegister(&X86::GR64RegClass); | |||
35351 | MachineInstrBuilder MIB = | |||
35352 | BuildMI(*BB, MI, DL, TII->get(X86::LCMPXCHG16B_SAVE_RBX), Dst); | |||
35353 | for (unsigned Idx = 0; Idx < X86::AddrNumOperands; ++Idx) | |||
35354 | MIB.add(MI.getOperand(Idx)); | |||
35355 | MIB.add(MI.getOperand(X86::AddrNumOperands)); | |||
35356 | MIB.addReg(SaveRBX); | |||
35357 | } else { | |||
35358 | // Simple case, just copy the virtual register to RBX. | |||
35359 | BuildMI(*BB, MI, DL, TII->get(TargetOpcode::COPY), X86::RBX) | |||
35360 | .add(MI.getOperand(X86::AddrNumOperands)); | |||
35361 | MachineInstrBuilder MIB = | |||
35362 | BuildMI(*BB, MI, DL, TII->get(X86::LCMPXCHG16B)); | |||
35363 | for (unsigned Idx = 0; Idx < X86::AddrNumOperands; ++Idx) | |||
35364 | MIB.add(MI.getOperand(Idx)); | |||
35365 | } | |||
35366 | MI.eraseFromParent(); | |||
35367 | return BB; | |||
35368 | } | |||
35369 | case X86::MWAITX: { | |||
35370 | const X86RegisterInfo *TRI = Subtarget.getRegisterInfo(); | |||
35371 | Register BasePtr = TRI->getBaseRegister(); | |||
35372 | bool IsRBX = (BasePtr == X86::RBX || BasePtr == X86::EBX); | |||
35373 | // If no need to save the base pointer, we generate MWAITXrrr, | |||
35374 | // else we generate pseudo MWAITX_SAVE_RBX. | |||
35375 | if (!IsRBX || !TRI->hasBasePointer(*MF)) { | |||
35376 | BuildMI(*BB, MI, DL, TII->get(TargetOpcode::COPY), X86::ECX) | |||
35377 | .addReg(MI.getOperand(0).getReg()); | |||
35378 | BuildMI(*BB, MI, DL, TII->get(TargetOpcode::COPY), X86::EAX) | |||
35379 | .addReg(MI.getOperand(1).getReg()); | |||
35380 | BuildMI(*BB, MI, DL, TII->get(TargetOpcode::COPY), X86::EBX) | |||
35381 | .addReg(MI.getOperand(2).getReg()); | |||
35382 | BuildMI(*BB, MI, DL, TII->get(X86::MWAITXrrr)); | |||
35383 | MI.eraseFromParent(); | |||
35384 | } else { | |||
35385 | if (!BB->isLiveIn(BasePtr)) { | |||
35386 | BB->addLiveIn(BasePtr); | |||
35387 | } | |||
35388 | // Parameters can be copied into ECX and EAX but not EBX yet. | |||
35389 | BuildMI(*BB, MI, DL, TII->get(TargetOpcode::COPY), X86::ECX) | |||
35390 | .addReg(MI.getOperand(0).getReg()); | |||
35391 | BuildMI(*BB, MI, DL, TII->get(TargetOpcode::COPY), X86::EAX) | |||
35392 | .addReg(MI.getOperand(1).getReg()); | |||
35393 | assert(Subtarget.is64Bit() && "Expected 64-bit mode!")(static_cast <bool> (Subtarget.is64Bit() && "Expected 64-bit mode!" ) ? void (0) : __assert_fail ("Subtarget.is64Bit() && \"Expected 64-bit mode!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 35393, __extension__ __PRETTY_FUNCTION__)); | |||
35394 | // Save RBX into a virtual register. | |||
35395 | Register SaveRBX = | |||
35396 | MF->getRegInfo().createVirtualRegister(&X86::GR64RegClass); | |||
35397 | BuildMI(*BB, MI, DL, TII->get(TargetOpcode::COPY), SaveRBX) | |||
35398 | .addReg(X86::RBX); | |||
35399 | // Generate mwaitx pseudo. | |||
35400 | Register Dst = MF->getRegInfo().createVirtualRegister(&X86::GR64RegClass); | |||
35401 | BuildMI(*BB, MI, DL, TII->get(X86::MWAITX_SAVE_RBX)) | |||
35402 | .addDef(Dst) // Destination tied in with SaveRBX. | |||
35403 | .addReg(MI.getOperand(2).getReg()) // input value of EBX. | |||
35404 | .addUse(SaveRBX); // Save of base pointer. | |||
35405 | MI.eraseFromParent(); | |||
35406 | } | |||
35407 | return BB; | |||
35408 | } | |||
35409 | case TargetOpcode::PREALLOCATED_SETUP: { | |||
35410 | assert(Subtarget.is32Bit() && "preallocated only used in 32-bit")(static_cast <bool> (Subtarget.is32Bit() && "preallocated only used in 32-bit" ) ? void (0) : __assert_fail ("Subtarget.is32Bit() && \"preallocated only used in 32-bit\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 35410, __extension__ __PRETTY_FUNCTION__)); | |||
35411 | auto MFI = MF->getInfo<X86MachineFunctionInfo>(); | |||
35412 | MFI->setHasPreallocatedCall(true); | |||
35413 | int64_t PreallocatedId = MI.getOperand(0).getImm(); | |||
35414 | size_t StackAdjustment = MFI->getPreallocatedStackSize(PreallocatedId); | |||
35415 | assert(StackAdjustment != 0 && "0 stack adjustment")(static_cast <bool> (StackAdjustment != 0 && "0 stack adjustment" ) ? void (0) : __assert_fail ("StackAdjustment != 0 && \"0 stack adjustment\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 35415, __extension__ __PRETTY_FUNCTION__)); | |||
35416 | LLVM_DEBUG(dbgs() << "PREALLOCATED_SETUP stack adjustment "do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("x86-isel")) { dbgs() << "PREALLOCATED_SETUP stack adjustment " << StackAdjustment << "\n"; } } while (false) | |||
35417 | << StackAdjustment << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("x86-isel")) { dbgs() << "PREALLOCATED_SETUP stack adjustment " << StackAdjustment << "\n"; } } while (false); | |||
35418 | BuildMI(*BB, MI, DL, TII->get(X86::SUB32ri), X86::ESP) | |||
35419 | .addReg(X86::ESP) | |||
35420 | .addImm(StackAdjustment); | |||
35421 | MI.eraseFromParent(); | |||
35422 | return BB; | |||
35423 | } | |||
35424 | case TargetOpcode::PREALLOCATED_ARG: { | |||
35425 | assert(Subtarget.is32Bit() && "preallocated calls only used in 32-bit")(static_cast <bool> (Subtarget.is32Bit() && "preallocated calls only used in 32-bit" ) ? void (0) : __assert_fail ("Subtarget.is32Bit() && \"preallocated calls only used in 32-bit\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 35425, __extension__ __PRETTY_FUNCTION__)); | |||
35426 | int64_t PreallocatedId = MI.getOperand(1).getImm(); | |||
35427 | int64_t ArgIdx = MI.getOperand(2).getImm(); | |||
35428 | auto MFI = MF->getInfo<X86MachineFunctionInfo>(); | |||
35429 | size_t ArgOffset = MFI->getPreallocatedArgOffsets(PreallocatedId)[ArgIdx]; | |||
35430 | LLVM_DEBUG(dbgs() << "PREALLOCATED_ARG arg index " << ArgIdxdo { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("x86-isel")) { dbgs() << "PREALLOCATED_ARG arg index " << ArgIdx << ", arg offset " << ArgOffset << "\n"; } } while (false) | |||
35431 | << ", arg offset " << ArgOffset << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("x86-isel")) { dbgs() << "PREALLOCATED_ARG arg index " << ArgIdx << ", arg offset " << ArgOffset << "\n"; } } while (false); | |||
35432 | // stack pointer + offset | |||
35433 | addRegOffset( | |||
35434 | BuildMI(*BB, MI, DL, TII->get(X86::LEA32r), MI.getOperand(0).getReg()), | |||
35435 | X86::ESP, false, ArgOffset); | |||
35436 | MI.eraseFromParent(); | |||
35437 | return BB; | |||
35438 | } | |||
35439 | case X86::PTDPBSSD: | |||
35440 | case X86::PTDPBSUD: | |||
35441 | case X86::PTDPBUSD: | |||
35442 | case X86::PTDPBUUD: | |||
35443 | case X86::PTDPBF16PS: { | |||
35444 | unsigned Opc; | |||
35445 | switch (MI.getOpcode()) { | |||
35446 | default: llvm_unreachable("illegal opcode!")::llvm::llvm_unreachable_internal("illegal opcode!", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 35446); | |||
35447 | case X86::PTDPBSSD: Opc = X86::TDPBSSD; break; | |||
35448 | case X86::PTDPBSUD: Opc = X86::TDPBSUD; break; | |||
35449 | case X86::PTDPBUSD: Opc = X86::TDPBUSD; break; | |||
35450 | case X86::PTDPBUUD: Opc = X86::TDPBUUD; break; | |||
35451 | case X86::PTDPBF16PS: Opc = X86::TDPBF16PS; break; | |||
35452 | } | |||
35453 | ||||
35454 | MachineInstrBuilder MIB = BuildMI(*BB, MI, DL, TII->get(Opc)); | |||
35455 | MIB.addReg(TMMImmToTMMReg(MI.getOperand(0).getImm()), RegState::Define); | |||
35456 | MIB.addReg(TMMImmToTMMReg(MI.getOperand(0).getImm()), RegState::Undef); | |||
35457 | MIB.addReg(TMMImmToTMMReg(MI.getOperand(1).getImm()), RegState::Undef); | |||
35458 | MIB.addReg(TMMImmToTMMReg(MI.getOperand(2).getImm()), RegState::Undef); | |||
35459 | ||||
35460 | MI.eraseFromParent(); // The pseudo is gone now. | |||
35461 | return BB; | |||
35462 | } | |||
35463 | case X86::PTILEZERO: { | |||
35464 | unsigned Imm = MI.getOperand(0).getImm(); | |||
35465 | BuildMI(*BB, MI, DL, TII->get(X86::TILEZERO), TMMImmToTMMReg(Imm)); | |||
35466 | MI.eraseFromParent(); // The pseudo is gone now. | |||
35467 | return BB; | |||
35468 | } | |||
35469 | case X86::PTILELOADD: | |||
35470 | case X86::PTILELOADDT1: | |||
35471 | case X86::PTILESTORED: { | |||
35472 | unsigned Opc; | |||
35473 | switch (MI.getOpcode()) { | |||
35474 | default: llvm_unreachable("illegal opcode!")::llvm::llvm_unreachable_internal("illegal opcode!", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 35474); | |||
35475 | case X86::PTILELOADD: Opc = X86::TILELOADD; break; | |||
35476 | case X86::PTILELOADDT1: Opc = X86::TILELOADDT1; break; | |||
35477 | case X86::PTILESTORED: Opc = X86::TILESTORED; break; | |||
35478 | } | |||
35479 | ||||
35480 | MachineInstrBuilder MIB = BuildMI(*BB, MI, DL, TII->get(Opc)); | |||
35481 | unsigned CurOp = 0; | |||
35482 | if (Opc != X86::TILESTORED) | |||
35483 | MIB.addReg(TMMImmToTMMReg(MI.getOperand(CurOp++).getImm()), | |||
35484 | RegState::Define); | |||
35485 | ||||
35486 | MIB.add(MI.getOperand(CurOp++)); // base | |||
35487 | MIB.add(MI.getOperand(CurOp++)); // scale | |||
35488 | MIB.add(MI.getOperand(CurOp++)); // index -- stride | |||
35489 | MIB.add(MI.getOperand(CurOp++)); // displacement | |||
35490 | MIB.add(MI.getOperand(CurOp++)); // segment | |||
35491 | ||||
35492 | if (Opc == X86::TILESTORED) | |||
35493 | MIB.addReg(TMMImmToTMMReg(MI.getOperand(CurOp++).getImm()), | |||
35494 | RegState::Undef); | |||
35495 | ||||
35496 | MI.eraseFromParent(); // The pseudo is gone now. | |||
35497 | return BB; | |||
35498 | } | |||
35499 | } | |||
35500 | } | |||
35501 | ||||
35502 | //===----------------------------------------------------------------------===// | |||
35503 | // X86 Optimization Hooks | |||
35504 | //===----------------------------------------------------------------------===// | |||
35505 | ||||
35506 | bool | |||
35507 | X86TargetLowering::targetShrinkDemandedConstant(SDValue Op, | |||
35508 | const APInt &DemandedBits, | |||
35509 | const APInt &DemandedElts, | |||
35510 | TargetLoweringOpt &TLO) const { | |||
35511 | EVT VT = Op.getValueType(); | |||
35512 | unsigned Opcode = Op.getOpcode(); | |||
35513 | unsigned EltSize = VT.getScalarSizeInBits(); | |||
35514 | ||||
35515 | if (VT.isVector()) { | |||
35516 | // If the constant is only all signbits in the active bits, then we should | |||
35517 | // extend it to the entire constant to allow it act as a boolean constant | |||
35518 | // vector. | |||
35519 | auto NeedsSignExtension = [&](SDValue V, unsigned ActiveBits) { | |||
35520 | if (!ISD::isBuildVectorOfConstantSDNodes(V.getNode())) | |||
35521 | return false; | |||
35522 | for (unsigned i = 0, e = V.getNumOperands(); i != e; ++i) { | |||
35523 | if (!DemandedElts[i] || V.getOperand(i).isUndef()) | |||
35524 | continue; | |||
35525 | const APInt &Val = V.getConstantOperandAPInt(i); | |||
35526 | if (Val.getBitWidth() > Val.getNumSignBits() && | |||
35527 | Val.trunc(ActiveBits).getNumSignBits() == ActiveBits) | |||
35528 | return true; | |||
35529 | } | |||
35530 | return false; | |||
35531 | }; | |||
35532 | // For vectors - if we have a constant, then try to sign extend. | |||
35533 | // TODO: Handle AND/ANDN cases. | |||
35534 | unsigned ActiveBits = DemandedBits.getActiveBits(); | |||
35535 | if (EltSize > ActiveBits && EltSize > 1 && isTypeLegal(VT) && | |||
35536 | (Opcode == ISD::OR || Opcode == ISD::XOR) && | |||
35537 | NeedsSignExtension(Op.getOperand(1), ActiveBits)) { | |||
35538 | EVT ExtSVT = EVT::getIntegerVT(*TLO.DAG.getContext(), ActiveBits); | |||
35539 | EVT ExtVT = EVT::getVectorVT(*TLO.DAG.getContext(), ExtSVT, | |||
35540 | VT.getVectorNumElements()); | |||
35541 | SDValue NewC = | |||
35542 | TLO.DAG.getNode(ISD::SIGN_EXTEND_INREG, SDLoc(Op), VT, | |||
35543 | Op.getOperand(1), TLO.DAG.getValueType(ExtVT)); | |||
35544 | SDValue NewOp = | |||
35545 | TLO.DAG.getNode(Opcode, SDLoc(Op), VT, Op.getOperand(0), NewC); | |||
35546 | return TLO.CombineTo(Op, NewOp); | |||
35547 | } | |||
35548 | return false; | |||
35549 | } | |||
35550 | ||||
35551 | // Only optimize Ands to prevent shrinking a constant that could be | |||
35552 | // matched by movzx. | |||
35553 | if (Opcode != ISD::AND) | |||
35554 | return false; | |||
35555 | ||||
35556 | // Make sure the RHS really is a constant. | |||
35557 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1)); | |||
35558 | if (!C) | |||
35559 | return false; | |||
35560 | ||||
35561 | const APInt &Mask = C->getAPIntValue(); | |||
35562 | ||||
35563 | // Clear all non-demanded bits initially. | |||
35564 | APInt ShrunkMask = Mask & DemandedBits; | |||
35565 | ||||
35566 | // Find the width of the shrunk mask. | |||
35567 | unsigned Width = ShrunkMask.getActiveBits(); | |||
35568 | ||||
35569 | // If the mask is all 0s there's nothing to do here. | |||
35570 | if (Width == 0) | |||
35571 | return false; | |||
35572 | ||||
35573 | // Find the next power of 2 width, rounding up to a byte. | |||
35574 | Width = PowerOf2Ceil(std::max(Width, 8U)); | |||
35575 | // Truncate the width to size to handle illegal types. | |||
35576 | Width = std::min(Width, EltSize); | |||
35577 | ||||
35578 | // Calculate a possible zero extend mask for this constant. | |||
35579 | APInt ZeroExtendMask = APInt::getLowBitsSet(EltSize, Width); | |||
35580 | ||||
35581 | // If we aren't changing the mask, just return true to keep it and prevent | |||
35582 | // the caller from optimizing. | |||
35583 | if (ZeroExtendMask == Mask) | |||
35584 | return true; | |||
35585 | ||||
35586 | // Make sure the new mask can be represented by a combination of mask bits | |||
35587 | // and non-demanded bits. | |||
35588 | if (!ZeroExtendMask.isSubsetOf(Mask | ~DemandedBits)) | |||
35589 | return false; | |||
35590 | ||||
35591 | // Replace the constant with the zero extend mask. | |||
35592 | SDLoc DL(Op); | |||
35593 | SDValue NewC = TLO.DAG.getConstant(ZeroExtendMask, DL, VT); | |||
35594 | SDValue NewOp = TLO.DAG.getNode(ISD::AND, DL, VT, Op.getOperand(0), NewC); | |||
35595 | return TLO.CombineTo(Op, NewOp); | |||
35596 | } | |||
35597 | ||||
35598 | void X86TargetLowering::computeKnownBitsForTargetNode(const SDValue Op, | |||
35599 | KnownBits &Known, | |||
35600 | const APInt &DemandedElts, | |||
35601 | const SelectionDAG &DAG, | |||
35602 | unsigned Depth) const { | |||
35603 | unsigned BitWidth = Known.getBitWidth(); | |||
35604 | unsigned NumElts = DemandedElts.getBitWidth(); | |||
35605 | unsigned Opc = Op.getOpcode(); | |||
35606 | EVT VT = Op.getValueType(); | |||
35607 | assert((Opc >= ISD::BUILTIN_OP_END ||(static_cast <bool> ((Opc >= ISD::BUILTIN_OP_END || Opc == ISD::INTRINSIC_WO_CHAIN || Opc == ISD::INTRINSIC_W_CHAIN || Opc == ISD::INTRINSIC_VOID) && "Should use MaskedValueIsZero if you don't know whether Op" " is a target node!") ? void (0) : __assert_fail ("(Opc >= ISD::BUILTIN_OP_END || Opc == ISD::INTRINSIC_WO_CHAIN || Opc == ISD::INTRINSIC_W_CHAIN || Opc == ISD::INTRINSIC_VOID) && \"Should use MaskedValueIsZero if you don't know whether Op\" \" is a target node!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 35612, __extension__ __PRETTY_FUNCTION__)) | |||
35608 | Opc == ISD::INTRINSIC_WO_CHAIN ||(static_cast <bool> ((Opc >= ISD::BUILTIN_OP_END || Opc == ISD::INTRINSIC_WO_CHAIN || Opc == ISD::INTRINSIC_W_CHAIN || Opc == ISD::INTRINSIC_VOID) && "Should use MaskedValueIsZero if you don't know whether Op" " is a target node!") ? void (0) : __assert_fail ("(Opc >= ISD::BUILTIN_OP_END || Opc == ISD::INTRINSIC_WO_CHAIN || Opc == ISD::INTRINSIC_W_CHAIN || Opc == ISD::INTRINSIC_VOID) && \"Should use MaskedValueIsZero if you don't know whether Op\" \" is a target node!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 35612, __extension__ __PRETTY_FUNCTION__)) | |||
35609 | Opc == ISD::INTRINSIC_W_CHAIN ||(static_cast <bool> ((Opc >= ISD::BUILTIN_OP_END || Opc == ISD::INTRINSIC_WO_CHAIN || Opc == ISD::INTRINSIC_W_CHAIN || Opc == ISD::INTRINSIC_VOID) && "Should use MaskedValueIsZero if you don't know whether Op" " is a target node!") ? void (0) : __assert_fail ("(Opc >= ISD::BUILTIN_OP_END || Opc == ISD::INTRINSIC_WO_CHAIN || Opc == ISD::INTRINSIC_W_CHAIN || Opc == ISD::INTRINSIC_VOID) && \"Should use MaskedValueIsZero if you don't know whether Op\" \" is a target node!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 35612, __extension__ __PRETTY_FUNCTION__)) | |||
35610 | Opc == ISD::INTRINSIC_VOID) &&(static_cast <bool> ((Opc >= ISD::BUILTIN_OP_END || Opc == ISD::INTRINSIC_WO_CHAIN || Opc == ISD::INTRINSIC_W_CHAIN || Opc == ISD::INTRINSIC_VOID) && "Should use MaskedValueIsZero if you don't know whether Op" " is a target node!") ? void (0) : __assert_fail ("(Opc >= ISD::BUILTIN_OP_END || Opc == ISD::INTRINSIC_WO_CHAIN || Opc == ISD::INTRINSIC_W_CHAIN || Opc == ISD::INTRINSIC_VOID) && \"Should use MaskedValueIsZero if you don't know whether Op\" \" is a target node!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 35612, __extension__ __PRETTY_FUNCTION__)) | |||
35611 | "Should use MaskedValueIsZero if you don't know whether Op"(static_cast <bool> ((Opc >= ISD::BUILTIN_OP_END || Opc == ISD::INTRINSIC_WO_CHAIN || Opc == ISD::INTRINSIC_W_CHAIN || Opc == ISD::INTRINSIC_VOID) && "Should use MaskedValueIsZero if you don't know whether Op" " is a target node!") ? void (0) : __assert_fail ("(Opc >= ISD::BUILTIN_OP_END || Opc == ISD::INTRINSIC_WO_CHAIN || Opc == ISD::INTRINSIC_W_CHAIN || Opc == ISD::INTRINSIC_VOID) && \"Should use MaskedValueIsZero if you don't know whether Op\" \" is a target node!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 35612, __extension__ __PRETTY_FUNCTION__)) | |||
35612 | " is a target node!")(static_cast <bool> ((Opc >= ISD::BUILTIN_OP_END || Opc == ISD::INTRINSIC_WO_CHAIN || Opc == ISD::INTRINSIC_W_CHAIN || Opc == ISD::INTRINSIC_VOID) && "Should use MaskedValueIsZero if you don't know whether Op" " is a target node!") ? void (0) : __assert_fail ("(Opc >= ISD::BUILTIN_OP_END || Opc == ISD::INTRINSIC_WO_CHAIN || Opc == ISD::INTRINSIC_W_CHAIN || Opc == ISD::INTRINSIC_VOID) && \"Should use MaskedValueIsZero if you don't know whether Op\" \" is a target node!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 35612, __extension__ __PRETTY_FUNCTION__)); | |||
35613 | ||||
35614 | Known.resetAll(); | |||
35615 | switch (Opc) { | |||
35616 | default: break; | |||
35617 | case X86ISD::SETCC: | |||
35618 | Known.Zero.setBitsFrom(1); | |||
35619 | break; | |||
35620 | case X86ISD::MOVMSK: { | |||
35621 | unsigned NumLoBits = Op.getOperand(0).getValueType().getVectorNumElements(); | |||
35622 | Known.Zero.setBitsFrom(NumLoBits); | |||
35623 | break; | |||
35624 | } | |||
35625 | case X86ISD::PEXTRB: | |||
35626 | case X86ISD::PEXTRW: { | |||
35627 | SDValue Src = Op.getOperand(0); | |||
35628 | EVT SrcVT = Src.getValueType(); | |||
35629 | APInt DemandedElt = APInt::getOneBitSet(SrcVT.getVectorNumElements(), | |||
35630 | Op.getConstantOperandVal(1)); | |||
35631 | Known = DAG.computeKnownBits(Src, DemandedElt, Depth + 1); | |||
35632 | Known = Known.anyextOrTrunc(BitWidth); | |||
35633 | Known.Zero.setBitsFrom(SrcVT.getScalarSizeInBits()); | |||
35634 | break; | |||
35635 | } | |||
35636 | case X86ISD::VSRAI: | |||
35637 | case X86ISD::VSHLI: | |||
35638 | case X86ISD::VSRLI: { | |||
35639 | unsigned ShAmt = Op.getConstantOperandVal(1); | |||
35640 | if (ShAmt >= VT.getScalarSizeInBits()) { | |||
35641 | Known.setAllZero(); | |||
35642 | break; | |||
35643 | } | |||
35644 | ||||
35645 | Known = DAG.computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1); | |||
35646 | if (Opc == X86ISD::VSHLI) { | |||
35647 | Known.Zero <<= ShAmt; | |||
35648 | Known.One <<= ShAmt; | |||
35649 | // Low bits are known zero. | |||
35650 | Known.Zero.setLowBits(ShAmt); | |||
35651 | } else if (Opc == X86ISD::VSRLI) { | |||
35652 | Known.Zero.lshrInPlace(ShAmt); | |||
35653 | Known.One.lshrInPlace(ShAmt); | |||
35654 | // High bits are known zero. | |||
35655 | Known.Zero.setHighBits(ShAmt); | |||
35656 | } else { | |||
35657 | Known.Zero.ashrInPlace(ShAmt); | |||
35658 | Known.One.ashrInPlace(ShAmt); | |||
35659 | } | |||
35660 | break; | |||
35661 | } | |||
35662 | case X86ISD::PACKUS: { | |||
35663 | // PACKUS is just a truncation if the upper half is zero. | |||
35664 | APInt DemandedLHS, DemandedRHS; | |||
35665 | getPackDemandedElts(VT, DemandedElts, DemandedLHS, DemandedRHS); | |||
35666 | ||||
35667 | Known.One = APInt::getAllOnes(BitWidth * 2); | |||
35668 | Known.Zero = APInt::getAllOnes(BitWidth * 2); | |||
35669 | ||||
35670 | KnownBits Known2; | |||
35671 | if (!!DemandedLHS) { | |||
35672 | Known2 = DAG.computeKnownBits(Op.getOperand(0), DemandedLHS, Depth + 1); | |||
35673 | Known = KnownBits::commonBits(Known, Known2); | |||
35674 | } | |||
35675 | if (!!DemandedRHS) { | |||
35676 | Known2 = DAG.computeKnownBits(Op.getOperand(1), DemandedRHS, Depth + 1); | |||
35677 | Known = KnownBits::commonBits(Known, Known2); | |||
35678 | } | |||
35679 | ||||
35680 | if (Known.countMinLeadingZeros() < BitWidth) | |||
35681 | Known.resetAll(); | |||
35682 | Known = Known.trunc(BitWidth); | |||
35683 | break; | |||
35684 | } | |||
35685 | case X86ISD::VBROADCAST: { | |||
35686 | SDValue Src = Op.getOperand(0); | |||
35687 | if (!Src.getSimpleValueType().isVector()) { | |||
35688 | Known = DAG.computeKnownBits(Src, Depth + 1); | |||
35689 | return; | |||
35690 | } | |||
35691 | break; | |||
35692 | } | |||
35693 | case X86ISD::ANDNP: { | |||
35694 | KnownBits Known2; | |||
35695 | Known = DAG.computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1); | |||
35696 | Known2 = DAG.computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1); | |||
35697 | ||||
35698 | // ANDNP = (~X & Y); | |||
35699 | Known.One &= Known2.Zero; | |||
35700 | Known.Zero |= Known2.One; | |||
35701 | break; | |||
35702 | } | |||
35703 | case X86ISD::FOR: { | |||
35704 | KnownBits Known2; | |||
35705 | Known = DAG.computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1); | |||
35706 | Known2 = DAG.computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1); | |||
35707 | ||||
35708 | Known |= Known2; | |||
35709 | break; | |||
35710 | } | |||
35711 | case X86ISD::PSADBW: { | |||
35712 | assert(VT.getScalarType() == MVT::i64 &&(static_cast <bool> (VT.getScalarType() == MVT::i64 && Op.getOperand(0).getValueType().getScalarType() == MVT::i8 && "Unexpected PSADBW types") ? void (0) : __assert_fail ("VT.getScalarType() == MVT::i64 && Op.getOperand(0).getValueType().getScalarType() == MVT::i8 && \"Unexpected PSADBW types\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 35714, __extension__ __PRETTY_FUNCTION__)) | |||
35713 | Op.getOperand(0).getValueType().getScalarType() == MVT::i8 &&(static_cast <bool> (VT.getScalarType() == MVT::i64 && Op.getOperand(0).getValueType().getScalarType() == MVT::i8 && "Unexpected PSADBW types") ? void (0) : __assert_fail ("VT.getScalarType() == MVT::i64 && Op.getOperand(0).getValueType().getScalarType() == MVT::i8 && \"Unexpected PSADBW types\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 35714, __extension__ __PRETTY_FUNCTION__)) | |||
35714 | "Unexpected PSADBW types")(static_cast <bool> (VT.getScalarType() == MVT::i64 && Op.getOperand(0).getValueType().getScalarType() == MVT::i8 && "Unexpected PSADBW types") ? void (0) : __assert_fail ("VT.getScalarType() == MVT::i64 && Op.getOperand(0).getValueType().getScalarType() == MVT::i8 && \"Unexpected PSADBW types\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 35714, __extension__ __PRETTY_FUNCTION__)); | |||
35715 | ||||
35716 | // PSADBW - fills low 16 bits and zeros upper 48 bits of each i64 result. | |||
35717 | Known.Zero.setBitsFrom(16); | |||
35718 | break; | |||
35719 | } | |||
35720 | case X86ISD::PMULUDQ: { | |||
35721 | KnownBits Known2; | |||
35722 | Known = DAG.computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1); | |||
35723 | Known2 = DAG.computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1); | |||
35724 | ||||
35725 | Known = Known.trunc(BitWidth / 2).zext(BitWidth); | |||
35726 | Known2 = Known2.trunc(BitWidth / 2).zext(BitWidth); | |||
35727 | Known = KnownBits::mul(Known, Known2); | |||
35728 | break; | |||
35729 | } | |||
35730 | case X86ISD::CMOV: { | |||
35731 | Known = DAG.computeKnownBits(Op.getOperand(1), Depth + 1); | |||
35732 | // If we don't know any bits, early out. | |||
35733 | if (Known.isUnknown()) | |||
35734 | break; | |||
35735 | KnownBits Known2 = DAG.computeKnownBits(Op.getOperand(0), Depth + 1); | |||
35736 | ||||
35737 | // Only known if known in both the LHS and RHS. | |||
35738 | Known = KnownBits::commonBits(Known, Known2); | |||
35739 | break; | |||
35740 | } | |||
35741 | case X86ISD::BEXTR: | |||
35742 | case X86ISD::BEXTRI: { | |||
35743 | SDValue Op0 = Op.getOperand(0); | |||
35744 | SDValue Op1 = Op.getOperand(1); | |||
35745 | ||||
35746 | if (auto* Cst1 = dyn_cast<ConstantSDNode>(Op1)) { | |||
35747 | unsigned Shift = Cst1->getAPIntValue().extractBitsAsZExtValue(8, 0); | |||
35748 | unsigned Length = Cst1->getAPIntValue().extractBitsAsZExtValue(8, 8); | |||
35749 | ||||
35750 | // If the length is 0, the result is 0. | |||
35751 | if (Length == 0) { | |||
35752 | Known.setAllZero(); | |||
35753 | break; | |||
35754 | } | |||
35755 | ||||
35756 | if ((Shift + Length) <= BitWidth) { | |||
35757 | Known = DAG.computeKnownBits(Op0, Depth + 1); | |||
35758 | Known = Known.extractBits(Length, Shift); | |||
35759 | Known = Known.zextOrTrunc(BitWidth); | |||
35760 | } | |||
35761 | } | |||
35762 | break; | |||
35763 | } | |||
35764 | case X86ISD::PDEP: { | |||
35765 | KnownBits Known2; | |||
35766 | Known = DAG.computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1); | |||
35767 | Known2 = DAG.computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1); | |||
35768 | // Zeros are retained from the mask operand. But not ones. | |||
35769 | Known.One.clearAllBits(); | |||
35770 | // The result will have at least as many trailing zeros as the non-mask | |||
35771 | // operand since bits can only map to the same or higher bit position. | |||
35772 | Known.Zero.setLowBits(Known2.countMinTrailingZeros()); | |||
35773 | break; | |||
35774 | } | |||
35775 | case X86ISD::PEXT: { | |||
35776 | Known = DAG.computeKnownBits(Op.getOperand(1), DemandedElts, Depth + 1); | |||
35777 | // The result has as many leading zeros as the number of zeroes in the mask. | |||
35778 | unsigned Count = Known.Zero.countPopulation(); | |||
35779 | Known.Zero = APInt::getHighBitsSet(BitWidth, Count); | |||
35780 | Known.One.clearAllBits(); | |||
35781 | break; | |||
35782 | } | |||
35783 | case X86ISD::VTRUNC: | |||
35784 | case X86ISD::VTRUNCS: | |||
35785 | case X86ISD::VTRUNCUS: | |||
35786 | case X86ISD::CVTSI2P: | |||
35787 | case X86ISD::CVTUI2P: | |||
35788 | case X86ISD::CVTP2SI: | |||
35789 | case X86ISD::CVTP2UI: | |||
35790 | case X86ISD::MCVTP2SI: | |||
35791 | case X86ISD::MCVTP2UI: | |||
35792 | case X86ISD::CVTTP2SI: | |||
35793 | case X86ISD::CVTTP2UI: | |||
35794 | case X86ISD::MCVTTP2SI: | |||
35795 | case X86ISD::MCVTTP2UI: | |||
35796 | case X86ISD::MCVTSI2P: | |||
35797 | case X86ISD::MCVTUI2P: | |||
35798 | case X86ISD::VFPROUND: | |||
35799 | case X86ISD::VMFPROUND: | |||
35800 | case X86ISD::CVTPS2PH: | |||
35801 | case X86ISD::MCVTPS2PH: { | |||
35802 | // Truncations/Conversions - upper elements are known zero. | |||
35803 | EVT SrcVT = Op.getOperand(0).getValueType(); | |||
35804 | if (SrcVT.isVector()) { | |||
35805 | unsigned NumSrcElts = SrcVT.getVectorNumElements(); | |||
35806 | if (NumElts > NumSrcElts && | |||
35807 | DemandedElts.countTrailingZeros() >= NumSrcElts) | |||
35808 | Known.setAllZero(); | |||
35809 | } | |||
35810 | break; | |||
35811 | } | |||
35812 | case X86ISD::STRICT_CVTTP2SI: | |||
35813 | case X86ISD::STRICT_CVTTP2UI: | |||
35814 | case X86ISD::STRICT_CVTSI2P: | |||
35815 | case X86ISD::STRICT_CVTUI2P: | |||
35816 | case X86ISD::STRICT_VFPROUND: | |||
35817 | case X86ISD::STRICT_CVTPS2PH: { | |||
35818 | // Strict Conversions - upper elements are known zero. | |||
35819 | EVT SrcVT = Op.getOperand(1).getValueType(); | |||
35820 | if (SrcVT.isVector()) { | |||
35821 | unsigned NumSrcElts = SrcVT.getVectorNumElements(); | |||
35822 | if (NumElts > NumSrcElts && | |||
35823 | DemandedElts.countTrailingZeros() >= NumSrcElts) | |||
35824 | Known.setAllZero(); | |||
35825 | } | |||
35826 | break; | |||
35827 | } | |||
35828 | case X86ISD::MOVQ2DQ: { | |||
35829 | // Move from MMX to XMM. Upper half of XMM should be 0. | |||
35830 | if (DemandedElts.countTrailingZeros() >= (NumElts / 2)) | |||
35831 | Known.setAllZero(); | |||
35832 | break; | |||
35833 | } | |||
35834 | } | |||
35835 | ||||
35836 | // Handle target shuffles. | |||
35837 | // TODO - use resolveTargetShuffleInputs once we can limit recursive depth. | |||
35838 | if (isTargetShuffle(Opc)) { | |||
35839 | SmallVector<int, 64> Mask; | |||
35840 | SmallVector<SDValue, 2> Ops; | |||
35841 | if (getTargetShuffleMask(Op.getNode(), VT.getSimpleVT(), true, Ops, Mask)) { | |||
35842 | unsigned NumOps = Ops.size(); | |||
35843 | unsigned NumElts = VT.getVectorNumElements(); | |||
35844 | if (Mask.size() == NumElts) { | |||
35845 | SmallVector<APInt, 2> DemandedOps(NumOps, APInt(NumElts, 0)); | |||
35846 | Known.Zero.setAllBits(); Known.One.setAllBits(); | |||
35847 | for (unsigned i = 0; i != NumElts; ++i) { | |||
35848 | if (!DemandedElts[i]) | |||
35849 | continue; | |||
35850 | int M = Mask[i]; | |||
35851 | if (M == SM_SentinelUndef) { | |||
35852 | // For UNDEF elements, we don't know anything about the common state | |||
35853 | // of the shuffle result. | |||
35854 | Known.resetAll(); | |||
35855 | break; | |||
35856 | } | |||
35857 | if (M == SM_SentinelZero) { | |||
35858 | Known.One.clearAllBits(); | |||
35859 | continue; | |||
35860 | } | |||
35861 | assert(0 <= M && (unsigned)M < (NumOps * NumElts) &&(static_cast <bool> (0 <= M && (unsigned)M < (NumOps * NumElts) && "Shuffle index out of range") ? void (0) : __assert_fail ("0 <= M && (unsigned)M < (NumOps * NumElts) && \"Shuffle index out of range\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 35862, __extension__ __PRETTY_FUNCTION__)) | |||
35862 | "Shuffle index out of range")(static_cast <bool> (0 <= M && (unsigned)M < (NumOps * NumElts) && "Shuffle index out of range") ? void (0) : __assert_fail ("0 <= M && (unsigned)M < (NumOps * NumElts) && \"Shuffle index out of range\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 35862, __extension__ __PRETTY_FUNCTION__)); | |||
35863 | ||||
35864 | unsigned OpIdx = (unsigned)M / NumElts; | |||
35865 | unsigned EltIdx = (unsigned)M % NumElts; | |||
35866 | if (Ops[OpIdx].getValueType() != VT) { | |||
35867 | // TODO - handle target shuffle ops with different value types. | |||
35868 | Known.resetAll(); | |||
35869 | break; | |||
35870 | } | |||
35871 | DemandedOps[OpIdx].setBit(EltIdx); | |||
35872 | } | |||
35873 | // Known bits are the values that are shared by every demanded element. | |||
35874 | for (unsigned i = 0; i != NumOps && !Known.isUnknown(); ++i) { | |||
35875 | if (!DemandedOps[i]) | |||
35876 | continue; | |||
35877 | KnownBits Known2 = | |||
35878 | DAG.computeKnownBits(Ops[i], DemandedOps[i], Depth + 1); | |||
35879 | Known = KnownBits::commonBits(Known, Known2); | |||
35880 | } | |||
35881 | } | |||
35882 | } | |||
35883 | } | |||
35884 | } | |||
35885 | ||||
35886 | unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode( | |||
35887 | SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG, | |||
35888 | unsigned Depth) const { | |||
35889 | EVT VT = Op.getValueType(); | |||
35890 | unsigned VTBits = VT.getScalarSizeInBits(); | |||
35891 | unsigned Opcode = Op.getOpcode(); | |||
35892 | switch (Opcode) { | |||
35893 | case X86ISD::SETCC_CARRY: | |||
35894 | // SETCC_CARRY sets the dest to ~0 for true or 0 for false. | |||
35895 | return VTBits; | |||
35896 | ||||
35897 | case X86ISD::VTRUNC: { | |||
35898 | SDValue Src = Op.getOperand(0); | |||
35899 | MVT SrcVT = Src.getSimpleValueType(); | |||
35900 | unsigned NumSrcBits = SrcVT.getScalarSizeInBits(); | |||
35901 | assert(VTBits < NumSrcBits && "Illegal truncation input type")(static_cast <bool> (VTBits < NumSrcBits && "Illegal truncation input type" ) ? void (0) : __assert_fail ("VTBits < NumSrcBits && \"Illegal truncation input type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 35901, __extension__ __PRETTY_FUNCTION__)); | |||
35902 | APInt DemandedSrc = DemandedElts.zextOrTrunc(SrcVT.getVectorNumElements()); | |||
35903 | unsigned Tmp = DAG.ComputeNumSignBits(Src, DemandedSrc, Depth + 1); | |||
35904 | if (Tmp > (NumSrcBits - VTBits)) | |||
35905 | return Tmp - (NumSrcBits - VTBits); | |||
35906 | return 1; | |||
35907 | } | |||
35908 | ||||
35909 | case X86ISD::PACKSS: { | |||
35910 | // PACKSS is just a truncation if the sign bits extend to the packed size. | |||
35911 | APInt DemandedLHS, DemandedRHS; | |||
35912 | getPackDemandedElts(Op.getValueType(), DemandedElts, DemandedLHS, | |||
35913 | DemandedRHS); | |||
35914 | ||||
35915 | unsigned SrcBits = Op.getOperand(0).getScalarValueSizeInBits(); | |||
35916 | unsigned Tmp0 = SrcBits, Tmp1 = SrcBits; | |||
35917 | if (!!DemandedLHS) | |||
35918 | Tmp0 = DAG.ComputeNumSignBits(Op.getOperand(0), DemandedLHS, Depth + 1); | |||
35919 | if (!!DemandedRHS) | |||
35920 | Tmp1 = DAG.ComputeNumSignBits(Op.getOperand(1), DemandedRHS, Depth + 1); | |||
35921 | unsigned Tmp = std::min(Tmp0, Tmp1); | |||
35922 | if (Tmp > (SrcBits - VTBits)) | |||
35923 | return Tmp - (SrcBits - VTBits); | |||
35924 | return 1; | |||
35925 | } | |||
35926 | ||||
35927 | case X86ISD::VBROADCAST: { | |||
35928 | SDValue Src = Op.getOperand(0); | |||
35929 | if (!Src.getSimpleValueType().isVector()) | |||
35930 | return DAG.ComputeNumSignBits(Src, Depth + 1); | |||
35931 | break; | |||
35932 | } | |||
35933 | ||||
35934 | case X86ISD::VSHLI: { | |||
35935 | SDValue Src = Op.getOperand(0); | |||
35936 | const APInt &ShiftVal = Op.getConstantOperandAPInt(1); | |||
35937 | if (ShiftVal.uge(VTBits)) | |||
35938 | return VTBits; // Shifted all bits out --> zero. | |||
35939 | unsigned Tmp = DAG.ComputeNumSignBits(Src, DemandedElts, Depth + 1); | |||
35940 | if (ShiftVal.uge(Tmp)) | |||
35941 | return 1; // Shifted all sign bits out --> unknown. | |||
35942 | return Tmp - ShiftVal.getZExtValue(); | |||
35943 | } | |||
35944 | ||||
35945 | case X86ISD::VSRAI: { | |||
35946 | SDValue Src = Op.getOperand(0); | |||
35947 | APInt ShiftVal = Op.getConstantOperandAPInt(1); | |||
35948 | if (ShiftVal.uge(VTBits - 1)) | |||
35949 | return VTBits; // Sign splat. | |||
35950 | unsigned Tmp = DAG.ComputeNumSignBits(Src, DemandedElts, Depth + 1); | |||
35951 | ShiftVal += Tmp; | |||
35952 | return ShiftVal.uge(VTBits) ? VTBits : ShiftVal.getZExtValue(); | |||
35953 | } | |||
35954 | ||||
35955 | case X86ISD::FSETCC: | |||
35956 | // cmpss/cmpsd return zero/all-bits result values in the bottom element. | |||
35957 | if (VT == MVT::f32 || VT == MVT::f64 || | |||
35958 | ((VT == MVT::v4f32 || VT == MVT::v2f64) && DemandedElts == 1)) | |||
35959 | return VTBits; | |||
35960 | break; | |||
35961 | ||||
35962 | case X86ISD::PCMPGT: | |||
35963 | case X86ISD::PCMPEQ: | |||
35964 | case X86ISD::CMPP: | |||
35965 | case X86ISD::VPCOM: | |||
35966 | case X86ISD::VPCOMU: | |||
35967 | // Vector compares return zero/all-bits result values. | |||
35968 | return VTBits; | |||
35969 | ||||
35970 | case X86ISD::ANDNP: { | |||
35971 | unsigned Tmp0 = | |||
35972 | DAG.ComputeNumSignBits(Op.getOperand(0), DemandedElts, Depth + 1); | |||
35973 | if (Tmp0 == 1) return 1; // Early out. | |||
35974 | unsigned Tmp1 = | |||
35975 | DAG.ComputeNumSignBits(Op.getOperand(1), DemandedElts, Depth + 1); | |||
35976 | return std::min(Tmp0, Tmp1); | |||
35977 | } | |||
35978 | ||||
35979 | case X86ISD::CMOV: { | |||
35980 | unsigned Tmp0 = DAG.ComputeNumSignBits(Op.getOperand(0), Depth+1); | |||
35981 | if (Tmp0 == 1) return 1; // Early out. | |||
35982 | unsigned Tmp1 = DAG.ComputeNumSignBits(Op.getOperand(1), Depth+1); | |||
35983 | return std::min(Tmp0, Tmp1); | |||
35984 | } | |||
35985 | } | |||
35986 | ||||
35987 | // Handle target shuffles. | |||
35988 | // TODO - use resolveTargetShuffleInputs once we can limit recursive depth. | |||
35989 | if (isTargetShuffle(Opcode)) { | |||
35990 | SmallVector<int, 64> Mask; | |||
35991 | SmallVector<SDValue, 2> Ops; | |||
35992 | if (getTargetShuffleMask(Op.getNode(), VT.getSimpleVT(), true, Ops, Mask)) { | |||
35993 | unsigned NumOps = Ops.size(); | |||
35994 | unsigned NumElts = VT.getVectorNumElements(); | |||
35995 | if (Mask.size() == NumElts) { | |||
35996 | SmallVector<APInt, 2> DemandedOps(NumOps, APInt(NumElts, 0)); | |||
35997 | for (unsigned i = 0; i != NumElts; ++i) { | |||
35998 | if (!DemandedElts[i]) | |||
35999 | continue; | |||
36000 | int M = Mask[i]; | |||
36001 | if (M == SM_SentinelUndef) { | |||
36002 | // For UNDEF elements, we don't know anything about the common state | |||
36003 | // of the shuffle result. | |||
36004 | return 1; | |||
36005 | } else if (M == SM_SentinelZero) { | |||
36006 | // Zero = all sign bits. | |||
36007 | continue; | |||
36008 | } | |||
36009 | assert(0 <= M && (unsigned)M < (NumOps * NumElts) &&(static_cast <bool> (0 <= M && (unsigned)M < (NumOps * NumElts) && "Shuffle index out of range") ? void (0) : __assert_fail ("0 <= M && (unsigned)M < (NumOps * NumElts) && \"Shuffle index out of range\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 36010, __extension__ __PRETTY_FUNCTION__)) | |||
36010 | "Shuffle index out of range")(static_cast <bool> (0 <= M && (unsigned)M < (NumOps * NumElts) && "Shuffle index out of range") ? void (0) : __assert_fail ("0 <= M && (unsigned)M < (NumOps * NumElts) && \"Shuffle index out of range\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 36010, __extension__ __PRETTY_FUNCTION__)); | |||
36011 | ||||
36012 | unsigned OpIdx = (unsigned)M / NumElts; | |||
36013 | unsigned EltIdx = (unsigned)M % NumElts; | |||
36014 | if (Ops[OpIdx].getValueType() != VT) { | |||
36015 | // TODO - handle target shuffle ops with different value types. | |||
36016 | return 1; | |||
36017 | } | |||
36018 | DemandedOps[OpIdx].setBit(EltIdx); | |||
36019 | } | |||
36020 | unsigned Tmp0 = VTBits; | |||
36021 | for (unsigned i = 0; i != NumOps && Tmp0 > 1; ++i) { | |||
36022 | if (!DemandedOps[i]) | |||
36023 | continue; | |||
36024 | unsigned Tmp1 = | |||
36025 | DAG.ComputeNumSignBits(Ops[i], DemandedOps[i], Depth + 1); | |||
36026 | Tmp0 = std::min(Tmp0, Tmp1); | |||
36027 | } | |||
36028 | return Tmp0; | |||
36029 | } | |||
36030 | } | |||
36031 | } | |||
36032 | ||||
36033 | // Fallback case. | |||
36034 | return 1; | |||
36035 | } | |||
36036 | ||||
36037 | SDValue X86TargetLowering::unwrapAddress(SDValue N) const { | |||
36038 | if (N->getOpcode() == X86ISD::Wrapper || N->getOpcode() == X86ISD::WrapperRIP) | |||
36039 | return N->getOperand(0); | |||
36040 | return N; | |||
36041 | } | |||
36042 | ||||
36043 | // Helper to look for a normal load that can be narrowed into a vzload with the | |||
36044 | // specified VT and memory VT. Returns SDValue() on failure. | |||
36045 | static SDValue narrowLoadToVZLoad(LoadSDNode *LN, MVT MemVT, MVT VT, | |||
36046 | SelectionDAG &DAG) { | |||
36047 | // Can't if the load is volatile or atomic. | |||
36048 | if (!LN->isSimple()) | |||
36049 | return SDValue(); | |||
36050 | ||||
36051 | SDVTList Tys = DAG.getVTList(VT, MVT::Other); | |||
36052 | SDValue Ops[] = {LN->getChain(), LN->getBasePtr()}; | |||
36053 | return DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, SDLoc(LN), Tys, Ops, MemVT, | |||
36054 | LN->getPointerInfo(), LN->getOriginalAlign(), | |||
36055 | LN->getMemOperand()->getFlags()); | |||
36056 | } | |||
36057 | ||||
36058 | // Attempt to match a combined shuffle mask against supported unary shuffle | |||
36059 | // instructions. | |||
36060 | // TODO: Investigate sharing more of this with shuffle lowering. | |||
36061 | static bool matchUnaryShuffle(MVT MaskVT, ArrayRef<int> Mask, | |||
36062 | bool AllowFloatDomain, bool AllowIntDomain, | |||
36063 | SDValue &V1, const SDLoc &DL, SelectionDAG &DAG, | |||
36064 | const X86Subtarget &Subtarget, unsigned &Shuffle, | |||
36065 | MVT &SrcVT, MVT &DstVT) { | |||
36066 | unsigned NumMaskElts = Mask.size(); | |||
36067 | unsigned MaskEltSize = MaskVT.getScalarSizeInBits(); | |||
36068 | ||||
36069 | // Match against a VZEXT_MOVL vXi32 and vXi16 zero-extending instruction. | |||
36070 | if (Mask[0] == 0 && | |||
36071 | (MaskEltSize == 32 || (MaskEltSize == 16 && Subtarget.hasFP16()))) { | |||
36072 | if ((isUndefOrZero(Mask[1]) && isUndefInRange(Mask, 2, NumMaskElts - 2)) || | |||
36073 | (V1.getOpcode() == ISD::SCALAR_TO_VECTOR && | |||
36074 | isUndefOrZeroInRange(Mask, 1, NumMaskElts - 1))) { | |||
36075 | Shuffle = X86ISD::VZEXT_MOVL; | |||
36076 | SrcVT = DstVT = | |||
36077 | !Subtarget.hasSSE2() && MaskEltSize == 32 ? MVT::v4f32 : MaskVT; | |||
36078 | return true; | |||
36079 | } | |||
36080 | } | |||
36081 | ||||
36082 | // Match against a ANY/ZERO_EXTEND_VECTOR_INREG instruction. | |||
36083 | // TODO: Add 512-bit vector support (split AVX512F and AVX512BW). | |||
36084 | if (AllowIntDomain && ((MaskVT.is128BitVector() && Subtarget.hasSSE41()) || | |||
36085 | (MaskVT.is256BitVector() && Subtarget.hasInt256()))) { | |||
36086 | unsigned MaxScale = 64 / MaskEltSize; | |||
36087 | for (unsigned Scale = 2; Scale <= MaxScale; Scale *= 2) { | |||
36088 | bool MatchAny = true; | |||
36089 | bool MatchZero = true; | |||
36090 | unsigned NumDstElts = NumMaskElts / Scale; | |||
36091 | for (unsigned i = 0; i != NumDstElts && (MatchAny || MatchZero); ++i) { | |||
36092 | if (!isUndefOrEqual(Mask[i * Scale], (int)i)) { | |||
36093 | MatchAny = MatchZero = false; | |||
36094 | break; | |||
36095 | } | |||
36096 | MatchAny &= isUndefInRange(Mask, (i * Scale) + 1, Scale - 1); | |||
36097 | MatchZero &= isUndefOrZeroInRange(Mask, (i * Scale) + 1, Scale - 1); | |||
36098 | } | |||
36099 | if (MatchAny || MatchZero) { | |||
36100 | assert(MatchZero && "Failed to match zext but matched aext?")(static_cast <bool> (MatchZero && "Failed to match zext but matched aext?" ) ? void (0) : __assert_fail ("MatchZero && \"Failed to match zext but matched aext?\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 36100, __extension__ __PRETTY_FUNCTION__)); | |||
36101 | unsigned SrcSize = std::max(128u, NumDstElts * MaskEltSize); | |||
36102 | MVT ScalarTy = MaskVT.isInteger() ? MaskVT.getScalarType() : | |||
36103 | MVT::getIntegerVT(MaskEltSize); | |||
36104 | SrcVT = MVT::getVectorVT(ScalarTy, SrcSize / MaskEltSize); | |||
36105 | ||||
36106 | if (SrcVT.getSizeInBits() != MaskVT.getSizeInBits()) | |||
36107 | V1 = extractSubVector(V1, 0, DAG, DL, SrcSize); | |||
36108 | ||||
36109 | Shuffle = unsigned(MatchAny ? ISD::ANY_EXTEND : ISD::ZERO_EXTEND); | |||
36110 | if (SrcVT.getVectorNumElements() != NumDstElts) | |||
36111 | Shuffle = getOpcode_EXTEND_VECTOR_INREG(Shuffle); | |||
36112 | ||||
36113 | DstVT = MVT::getIntegerVT(Scale * MaskEltSize); | |||
36114 | DstVT = MVT::getVectorVT(DstVT, NumDstElts); | |||
36115 | return true; | |||
36116 | } | |||
36117 | } | |||
36118 | } | |||
36119 | ||||
36120 | // Match against a VZEXT_MOVL instruction, SSE1 only supports 32-bits (MOVSS). | |||
36121 | if (((MaskEltSize == 32) || (MaskEltSize == 64 && Subtarget.hasSSE2())) && | |||
36122 | isUndefOrEqual(Mask[0], 0) && | |||
36123 | isUndefOrZeroInRange(Mask, 1, NumMaskElts - 1)) { | |||
36124 | Shuffle = X86ISD::VZEXT_MOVL; | |||
36125 | SrcVT = DstVT = !Subtarget.hasSSE2() ? MVT::v4f32 : MaskVT; | |||
36126 | return true; | |||
36127 | } | |||
36128 | ||||
36129 | // Check if we have SSE3 which will let us use MOVDDUP etc. The | |||
36130 | // instructions are no slower than UNPCKLPD but has the option to | |||
36131 | // fold the input operand into even an unaligned memory load. | |||
36132 | if (MaskVT.is128BitVector() && Subtarget.hasSSE3() && AllowFloatDomain) { | |||
36133 | if (isTargetShuffleEquivalent(MaskVT, Mask, {0, 0}, V1)) { | |||
36134 | Shuffle = X86ISD::MOVDDUP; | |||
36135 | SrcVT = DstVT = MVT::v2f64; | |||
36136 | return true; | |||
36137 | } | |||
36138 | if (isTargetShuffleEquivalent(MaskVT, Mask, {0, 0, 2, 2}, V1)) { | |||
36139 | Shuffle = X86ISD::MOVSLDUP; | |||
36140 | SrcVT = DstVT = MVT::v4f32; | |||
36141 | return true; | |||
36142 | } | |||
36143 | if (isTargetShuffleEquivalent(MaskVT, Mask, {1, 1, 3, 3}, V1)) { | |||
36144 | Shuffle = X86ISD::MOVSHDUP; | |||
36145 | SrcVT = DstVT = MVT::v4f32; | |||
36146 | return true; | |||
36147 | } | |||
36148 | } | |||
36149 | ||||
36150 | if (MaskVT.is256BitVector() && AllowFloatDomain) { | |||
36151 | assert(Subtarget.hasAVX() && "AVX required for 256-bit vector shuffles")(static_cast <bool> (Subtarget.hasAVX() && "AVX required for 256-bit vector shuffles" ) ? void (0) : __assert_fail ("Subtarget.hasAVX() && \"AVX required for 256-bit vector shuffles\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 36151, __extension__ __PRETTY_FUNCTION__)); | |||
36152 | if (isTargetShuffleEquivalent(MaskVT, Mask, {0, 0, 2, 2}, V1)) { | |||
36153 | Shuffle = X86ISD::MOVDDUP; | |||
36154 | SrcVT = DstVT = MVT::v4f64; | |||
36155 | return true; | |||
36156 | } | |||
36157 | if (isTargetShuffleEquivalent(MaskVT, Mask, {0, 0, 2, 2, 4, 4, 6, 6}, V1)) { | |||
36158 | Shuffle = X86ISD::MOVSLDUP; | |||
36159 | SrcVT = DstVT = MVT::v8f32; | |||
36160 | return true; | |||
36161 | } | |||
36162 | if (isTargetShuffleEquivalent(MaskVT, Mask, {1, 1, 3, 3, 5, 5, 7, 7}, V1)) { | |||
36163 | Shuffle = X86ISD::MOVSHDUP; | |||
36164 | SrcVT = DstVT = MVT::v8f32; | |||
36165 | return true; | |||
36166 | } | |||
36167 | } | |||
36168 | ||||
36169 | if (MaskVT.is512BitVector() && AllowFloatDomain) { | |||
36170 | assert(Subtarget.hasAVX512() &&(static_cast <bool> (Subtarget.hasAVX512() && "AVX512 required for 512-bit vector shuffles" ) ? void (0) : __assert_fail ("Subtarget.hasAVX512() && \"AVX512 required for 512-bit vector shuffles\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 36171, __extension__ __PRETTY_FUNCTION__)) | |||
36171 | "AVX512 required for 512-bit vector shuffles")(static_cast <bool> (Subtarget.hasAVX512() && "AVX512 required for 512-bit vector shuffles" ) ? void (0) : __assert_fail ("Subtarget.hasAVX512() && \"AVX512 required for 512-bit vector shuffles\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 36171, __extension__ __PRETTY_FUNCTION__)); | |||
36172 | if (isTargetShuffleEquivalent(MaskVT, Mask, {0, 0, 2, 2, 4, 4, 6, 6}, V1)) { | |||
36173 | Shuffle = X86ISD::MOVDDUP; | |||
36174 | SrcVT = DstVT = MVT::v8f64; | |||
36175 | return true; | |||
36176 | } | |||
36177 | if (isTargetShuffleEquivalent( | |||
36178 | MaskVT, Mask, | |||
36179 | {0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14}, V1)) { | |||
36180 | Shuffle = X86ISD::MOVSLDUP; | |||
36181 | SrcVT = DstVT = MVT::v16f32; | |||
36182 | return true; | |||
36183 | } | |||
36184 | if (isTargetShuffleEquivalent( | |||
36185 | MaskVT, Mask, | |||
36186 | {1, 1, 3, 3, 5, 5, 7, 7, 9, 9, 11, 11, 13, 13, 15, 15}, V1)) { | |||
36187 | Shuffle = X86ISD::MOVSHDUP; | |||
36188 | SrcVT = DstVT = MVT::v16f32; | |||
36189 | return true; | |||
36190 | } | |||
36191 | } | |||
36192 | ||||
36193 | return false; | |||
36194 | } | |||
36195 | ||||
36196 | // Attempt to match a combined shuffle mask against supported unary immediate | |||
36197 | // permute instructions. | |||
36198 | // TODO: Investigate sharing more of this with shuffle lowering. | |||
36199 | static bool matchUnaryPermuteShuffle(MVT MaskVT, ArrayRef<int> Mask, | |||
36200 | const APInt &Zeroable, | |||
36201 | bool AllowFloatDomain, bool AllowIntDomain, | |||
36202 | const X86Subtarget &Subtarget, | |||
36203 | unsigned &Shuffle, MVT &ShuffleVT, | |||
36204 | unsigned &PermuteImm) { | |||
36205 | unsigned NumMaskElts = Mask.size(); | |||
36206 | unsigned InputSizeInBits = MaskVT.getSizeInBits(); | |||
36207 | unsigned MaskScalarSizeInBits = InputSizeInBits / NumMaskElts; | |||
36208 | MVT MaskEltVT = MVT::getIntegerVT(MaskScalarSizeInBits); | |||
36209 | bool ContainsZeros = isAnyZero(Mask); | |||
36210 | ||||
36211 | // Handle VPERMI/VPERMILPD vXi64/vXi64 patterns. | |||
36212 | if (!ContainsZeros && MaskScalarSizeInBits == 64) { | |||
36213 | // Check for lane crossing permutes. | |||
36214 | if (is128BitLaneCrossingShuffleMask(MaskEltVT, Mask)) { | |||
36215 | // PERMPD/PERMQ permutes within a 256-bit vector (AVX2+). | |||
36216 | if (Subtarget.hasAVX2() && MaskVT.is256BitVector()) { | |||
36217 | Shuffle = X86ISD::VPERMI; | |||
36218 | ShuffleVT = (AllowFloatDomain ? MVT::v4f64 : MVT::v4i64); | |||
36219 | PermuteImm = getV4X86ShuffleImm(Mask); | |||
36220 | return true; | |||
36221 | } | |||
36222 | if (Subtarget.hasAVX512() && MaskVT.is512BitVector()) { | |||
36223 | SmallVector<int, 4> RepeatedMask; | |||
36224 | if (is256BitLaneRepeatedShuffleMask(MVT::v8f64, Mask, RepeatedMask)) { | |||
36225 | Shuffle = X86ISD::VPERMI; | |||
36226 | ShuffleVT = (AllowFloatDomain ? MVT::v8f64 : MVT::v8i64); | |||
36227 | PermuteImm = getV4X86ShuffleImm(RepeatedMask); | |||
36228 | return true; | |||
36229 | } | |||
36230 | } | |||
36231 | } else if (AllowFloatDomain && Subtarget.hasAVX()) { | |||
36232 | // VPERMILPD can permute with a non-repeating shuffle. | |||
36233 | Shuffle = X86ISD::VPERMILPI; | |||
36234 | ShuffleVT = MVT::getVectorVT(MVT::f64, Mask.size()); | |||
36235 | PermuteImm = 0; | |||
36236 | for (int i = 0, e = Mask.size(); i != e; ++i) { | |||
36237 | int M = Mask[i]; | |||
36238 | if (M == SM_SentinelUndef) | |||
36239 | continue; | |||
36240 | assert(((M / 2) == (i / 2)) && "Out of range shuffle mask index")(static_cast <bool> (((M / 2) == (i / 2)) && "Out of range shuffle mask index" ) ? void (0) : __assert_fail ("((M / 2) == (i / 2)) && \"Out of range shuffle mask index\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 36240, __extension__ __PRETTY_FUNCTION__)); | |||
36241 | PermuteImm |= (M & 1) << i; | |||
36242 | } | |||
36243 | return true; | |||
36244 | } | |||
36245 | } | |||
36246 | ||||
36247 | // Handle PSHUFD/VPERMILPI vXi32/vXf32 repeated patterns. | |||
36248 | // AVX introduced the VPERMILPD/VPERMILPS float permutes, before then we | |||
36249 | // had to use 2-input SHUFPD/SHUFPS shuffles (not handled here). | |||
36250 | if ((MaskScalarSizeInBits == 64 || MaskScalarSizeInBits == 32) && | |||
36251 | !ContainsZeros && (AllowIntDomain || Subtarget.hasAVX())) { | |||
36252 | SmallVector<int, 4> RepeatedMask; | |||
36253 | if (is128BitLaneRepeatedShuffleMask(MaskEltVT, Mask, RepeatedMask)) { | |||
36254 | // Narrow the repeated mask to create 32-bit element permutes. | |||
36255 | SmallVector<int, 4> WordMask = RepeatedMask; | |||
36256 | if (MaskScalarSizeInBits == 64) | |||
36257 | narrowShuffleMaskElts(2, RepeatedMask, WordMask); | |||
36258 | ||||
36259 | Shuffle = (AllowIntDomain ? X86ISD::PSHUFD : X86ISD::VPERMILPI); | |||
36260 | ShuffleVT = (AllowIntDomain ? MVT::i32 : MVT::f32); | |||
36261 | ShuffleVT = MVT::getVectorVT(ShuffleVT, InputSizeInBits / 32); | |||
36262 | PermuteImm = getV4X86ShuffleImm(WordMask); | |||
36263 | return true; | |||
36264 | } | |||
36265 | } | |||
36266 | ||||
36267 | // Handle PSHUFLW/PSHUFHW vXi16 repeated patterns. | |||
36268 | if (!ContainsZeros && AllowIntDomain && MaskScalarSizeInBits == 16 && | |||
36269 | ((MaskVT.is128BitVector() && Subtarget.hasSSE2()) || | |||
36270 | (MaskVT.is256BitVector() && Subtarget.hasAVX2()) || | |||
36271 | (MaskVT.is512BitVector() && Subtarget.hasBWI()))) { | |||
36272 | SmallVector<int, 4> RepeatedMask; | |||
36273 | if (is128BitLaneRepeatedShuffleMask(MaskEltVT, Mask, RepeatedMask)) { | |||
36274 | ArrayRef<int> LoMask(RepeatedMask.data() + 0, 4); | |||
36275 | ArrayRef<int> HiMask(RepeatedMask.data() + 4, 4); | |||
36276 | ||||
36277 | // PSHUFLW: permute lower 4 elements only. | |||
36278 | if (isUndefOrInRange(LoMask, 0, 4) && | |||
36279 | isSequentialOrUndefInRange(HiMask, 0, 4, 4)) { | |||
36280 | Shuffle = X86ISD::PSHUFLW; | |||
36281 | ShuffleVT = MVT::getVectorVT(MVT::i16, InputSizeInBits / 16); | |||
36282 | PermuteImm = getV4X86ShuffleImm(LoMask); | |||
36283 | return true; | |||
36284 | } | |||
36285 | ||||
36286 | // PSHUFHW: permute upper 4 elements only. | |||
36287 | if (isUndefOrInRange(HiMask, 4, 8) && | |||
36288 | isSequentialOrUndefInRange(LoMask, 0, 4, 0)) { | |||
36289 | // Offset the HiMask so that we can create the shuffle immediate. | |||
36290 | int OffsetHiMask[4]; | |||
36291 | for (int i = 0; i != 4; ++i) | |||
36292 | OffsetHiMask[i] = (HiMask[i] < 0 ? HiMask[i] : HiMask[i] - 4); | |||
36293 | ||||
36294 | Shuffle = X86ISD::PSHUFHW; | |||
36295 | ShuffleVT = MVT::getVectorVT(MVT::i16, InputSizeInBits / 16); | |||
36296 | PermuteImm = getV4X86ShuffleImm(OffsetHiMask); | |||
36297 | return true; | |||
36298 | } | |||
36299 | } | |||
36300 | } | |||
36301 | ||||
36302 | // Attempt to match against byte/bit shifts. | |||
36303 | if (AllowIntDomain && | |||
36304 | ((MaskVT.is128BitVector() && Subtarget.hasSSE2()) || | |||
36305 | (MaskVT.is256BitVector() && Subtarget.hasAVX2()) || | |||
36306 | (MaskVT.is512BitVector() && Subtarget.hasAVX512()))) { | |||
36307 | int ShiftAmt = matchShuffleAsShift(ShuffleVT, Shuffle, MaskScalarSizeInBits, | |||
36308 | Mask, 0, Zeroable, Subtarget); | |||
36309 | if (0 < ShiftAmt && (!ShuffleVT.is512BitVector() || Subtarget.hasBWI() || | |||
36310 | 32 <= ShuffleVT.getScalarSizeInBits())) { | |||
36311 | PermuteImm = (unsigned)ShiftAmt; | |||
36312 | return true; | |||
36313 | } | |||
36314 | } | |||
36315 | ||||
36316 | // Attempt to match against bit rotates. | |||
36317 | if (!ContainsZeros && AllowIntDomain && MaskScalarSizeInBits < 64 && | |||
36318 | ((MaskVT.is128BitVector() && Subtarget.hasXOP()) || | |||
36319 | Subtarget.hasAVX512())) { | |||
36320 | int RotateAmt = matchShuffleAsBitRotate(ShuffleVT, MaskScalarSizeInBits, | |||
36321 | Subtarget, Mask); | |||
36322 | if (0 < RotateAmt) { | |||
36323 | Shuffle = X86ISD::VROTLI; | |||
36324 | PermuteImm = (unsigned)RotateAmt; | |||
36325 | return true; | |||
36326 | } | |||
36327 | } | |||
36328 | ||||
36329 | return false; | |||
36330 | } | |||
36331 | ||||
36332 | // Attempt to match a combined unary shuffle mask against supported binary | |||
36333 | // shuffle instructions. | |||
36334 | // TODO: Investigate sharing more of this with shuffle lowering. | |||
36335 | static bool matchBinaryShuffle(MVT MaskVT, ArrayRef<int> Mask, | |||
36336 | bool AllowFloatDomain, bool AllowIntDomain, | |||
36337 | SDValue &V1, SDValue &V2, const SDLoc &DL, | |||
36338 | SelectionDAG &DAG, const X86Subtarget &Subtarget, | |||
36339 | unsigned &Shuffle, MVT &SrcVT, MVT &DstVT, | |||
36340 | bool IsUnary) { | |||
36341 | unsigned NumMaskElts = Mask.size(); | |||
36342 | unsigned EltSizeInBits = MaskVT.getScalarSizeInBits(); | |||
36343 | ||||
36344 | if (MaskVT.is128BitVector()) { | |||
36345 | if (isTargetShuffleEquivalent(MaskVT, Mask, {0, 0}) && AllowFloatDomain) { | |||
36346 | V2 = V1; | |||
36347 | V1 = (SM_SentinelUndef == Mask[0] ? DAG.getUNDEF(MVT::v4f32) : V1); | |||
36348 | Shuffle = Subtarget.hasSSE2() ? X86ISD::UNPCKL : X86ISD::MOVLHPS; | |||
36349 | SrcVT = DstVT = Subtarget.hasSSE2() ? MVT::v2f64 : MVT::v4f32; | |||
36350 | return true; | |||
36351 | } | |||
36352 | if (isTargetShuffleEquivalent(MaskVT, Mask, {1, 1}) && AllowFloatDomain) { | |||
36353 | V2 = V1; | |||
36354 | Shuffle = Subtarget.hasSSE2() ? X86ISD::UNPCKH : X86ISD::MOVHLPS; | |||
36355 | SrcVT = DstVT = Subtarget.hasSSE2() ? MVT::v2f64 : MVT::v4f32; | |||
36356 | return true; | |||
36357 | } | |||
36358 | if (isTargetShuffleEquivalent(MaskVT, Mask, {0, 3}) && | |||
36359 | Subtarget.hasSSE2() && (AllowFloatDomain || !Subtarget.hasSSE41())) { | |||
36360 | std::swap(V1, V2); | |||
36361 | Shuffle = X86ISD::MOVSD; | |||
36362 | SrcVT = DstVT = MVT::v2f64; | |||
36363 | return true; | |||
36364 | } | |||
36365 | if (isTargetShuffleEquivalent(MaskVT, Mask, {4, 1, 2, 3}) && | |||
36366 | (AllowFloatDomain || !Subtarget.hasSSE41())) { | |||
36367 | Shuffle = X86ISD::MOVSS; | |||
36368 | SrcVT = DstVT = MVT::v4f32; | |||
36369 | return true; | |||
36370 | } | |||
36371 | if (isTargetShuffleEquivalent(MaskVT, Mask, {8, 1, 2, 3, 4, 5, 6, 7}) && | |||
36372 | Subtarget.hasFP16()) { | |||
36373 | Shuffle = X86ISD::MOVSH; | |||
36374 | SrcVT = DstVT = MVT::v8f16; | |||
36375 | return true; | |||
36376 | } | |||
36377 | } | |||
36378 | ||||
36379 | // Attempt to match against either an unary or binary PACKSS/PACKUS shuffle. | |||
36380 | if (((MaskVT == MVT::v8i16 || MaskVT == MVT::v16i8) && Subtarget.hasSSE2()) || | |||
36381 | ((MaskVT == MVT::v16i16 || MaskVT == MVT::v32i8) && Subtarget.hasInt256()) || | |||
36382 | ((MaskVT == MVT::v32i16 || MaskVT == MVT::v64i8) && Subtarget.hasBWI())) { | |||
36383 | if (matchShuffleWithPACK(MaskVT, SrcVT, V1, V2, Shuffle, Mask, DAG, | |||
36384 | Subtarget)) { | |||
36385 | DstVT = MaskVT; | |||
36386 | return true; | |||
36387 | } | |||
36388 | } | |||
36389 | ||||
36390 | // Attempt to match against either a unary or binary UNPCKL/UNPCKH shuffle. | |||
36391 | if ((MaskVT == MVT::v4f32 && Subtarget.hasSSE1()) || | |||
36392 | (MaskVT.is128BitVector() && Subtarget.hasSSE2()) || | |||
36393 | (MaskVT.is256BitVector() && 32 <= EltSizeInBits && Subtarget.hasAVX()) || | |||
36394 | (MaskVT.is256BitVector() && Subtarget.hasAVX2()) || | |||
36395 | (MaskVT.is512BitVector() && Subtarget.hasAVX512())) { | |||
36396 | if (matchShuffleWithUNPCK(MaskVT, V1, V2, Shuffle, IsUnary, Mask, DL, DAG, | |||
36397 | Subtarget)) { | |||
36398 | SrcVT = DstVT = MaskVT; | |||
36399 | if (MaskVT.is256BitVector() && !Subtarget.hasAVX2()) | |||
36400 | SrcVT = DstVT = (32 == EltSizeInBits ? MVT::v8f32 : MVT::v4f64); | |||
36401 | return true; | |||
36402 | } | |||
36403 | } | |||
36404 | ||||
36405 | // Attempt to match against a OR if we're performing a blend shuffle and the | |||
36406 | // non-blended source element is zero in each case. | |||
36407 | if ((EltSizeInBits % V1.getScalarValueSizeInBits()) == 0 && | |||
36408 | (EltSizeInBits % V2.getScalarValueSizeInBits()) == 0) { | |||
36409 | bool IsBlend = true; | |||
36410 | unsigned NumV1Elts = V1.getValueType().getVectorNumElements(); | |||
36411 | unsigned NumV2Elts = V2.getValueType().getVectorNumElements(); | |||
36412 | unsigned Scale1 = NumV1Elts / NumMaskElts; | |||
36413 | unsigned Scale2 = NumV2Elts / NumMaskElts; | |||
36414 | APInt DemandedZeroV1 = APInt::getZero(NumV1Elts); | |||
36415 | APInt DemandedZeroV2 = APInt::getZero(NumV2Elts); | |||
36416 | for (unsigned i = 0; i != NumMaskElts; ++i) { | |||
36417 | int M = Mask[i]; | |||
36418 | if (M == SM_SentinelUndef) | |||
36419 | continue; | |||
36420 | if (M == SM_SentinelZero) { | |||
36421 | DemandedZeroV1.setBits(i * Scale1, (i + 1) * Scale1); | |||
36422 | DemandedZeroV2.setBits(i * Scale2, (i + 1) * Scale2); | |||
36423 | continue; | |||
36424 | } | |||
36425 | if (M == (int)i) { | |||
36426 | DemandedZeroV2.setBits(i * Scale2, (i + 1) * Scale2); | |||
36427 | continue; | |||
36428 | } | |||
36429 | if (M == (int)(i + NumMaskElts)) { | |||
36430 | DemandedZeroV1.setBits(i * Scale1, (i + 1) * Scale1); | |||
36431 | continue; | |||
36432 | } | |||
36433 | IsBlend = false; | |||
36434 | break; | |||
36435 | } | |||
36436 | if (IsBlend) { | |||
36437 | if (DAG.computeKnownBits(V1, DemandedZeroV1).isZero() && | |||
36438 | DAG.computeKnownBits(V2, DemandedZeroV2).isZero()) { | |||
36439 | Shuffle = ISD::OR; | |||
36440 | SrcVT = DstVT = MaskVT.changeTypeToInteger(); | |||
36441 | return true; | |||
36442 | } | |||
36443 | if (NumV1Elts == NumV2Elts && NumV1Elts == NumMaskElts) { | |||
36444 | // FIXME: handle mismatched sizes? | |||
36445 | // TODO: investigate if `ISD::OR` handling in | |||
36446 | // `TargetLowering::SimplifyDemandedVectorElts` can be improved instead. | |||
36447 | auto computeKnownBitsElementWise = [&DAG](SDValue V) { | |||
36448 | unsigned NumElts = V.getValueType().getVectorNumElements(); | |||
36449 | KnownBits Known(NumElts); | |||
36450 | for (unsigned EltIdx = 0; EltIdx != NumElts; ++EltIdx) { | |||
36451 | APInt Mask = APInt::getOneBitSet(NumElts, EltIdx); | |||
36452 | KnownBits PeepholeKnown = DAG.computeKnownBits(V, Mask); | |||
36453 | if (PeepholeKnown.isZero()) | |||
36454 | Known.Zero.setBit(EltIdx); | |||
36455 | if (PeepholeKnown.isAllOnes()) | |||
36456 | Known.One.setBit(EltIdx); | |||
36457 | } | |||
36458 | return Known; | |||
36459 | }; | |||
36460 | ||||
36461 | KnownBits V1Known = computeKnownBitsElementWise(V1); | |||
36462 | KnownBits V2Known = computeKnownBitsElementWise(V2); | |||
36463 | ||||
36464 | for (unsigned i = 0; i != NumMaskElts && IsBlend; ++i) { | |||
36465 | int M = Mask[i]; | |||
36466 | if (M == SM_SentinelUndef) | |||
36467 | continue; | |||
36468 | if (M == SM_SentinelZero) { | |||
36469 | IsBlend &= V1Known.Zero[i] && V2Known.Zero[i]; | |||
36470 | continue; | |||
36471 | } | |||
36472 | if (M == (int)i) { | |||
36473 | IsBlend &= V2Known.Zero[i] || V1Known.One[i]; | |||
36474 | continue; | |||
36475 | } | |||
36476 | if (M == (int)(i + NumMaskElts)) { | |||
36477 | IsBlend &= V1Known.Zero[i] || V2Known.One[i]; | |||
36478 | continue; | |||
36479 | } | |||
36480 | llvm_unreachable("will not get here.")::llvm::llvm_unreachable_internal("will not get here.", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 36480); | |||
36481 | } | |||
36482 | if (IsBlend) { | |||
36483 | Shuffle = ISD::OR; | |||
36484 | SrcVT = DstVT = MaskVT.changeTypeToInteger(); | |||
36485 | return true; | |||
36486 | } | |||
36487 | } | |||
36488 | } | |||
36489 | } | |||
36490 | ||||
36491 | return false; | |||
36492 | } | |||
36493 | ||||
36494 | static bool matchBinaryPermuteShuffle( | |||
36495 | MVT MaskVT, ArrayRef<int> Mask, const APInt &Zeroable, | |||
36496 | bool AllowFloatDomain, bool AllowIntDomain, SDValue &V1, SDValue &V2, | |||
36497 | const SDLoc &DL, SelectionDAG &DAG, const X86Subtarget &Subtarget, | |||
36498 | unsigned &Shuffle, MVT &ShuffleVT, unsigned &PermuteImm) { | |||
36499 | unsigned NumMaskElts = Mask.size(); | |||
36500 | unsigned EltSizeInBits = MaskVT.getScalarSizeInBits(); | |||
36501 | ||||
36502 | // Attempt to match against VALIGND/VALIGNQ rotate. | |||
36503 | if (AllowIntDomain && (EltSizeInBits == 64 || EltSizeInBits == 32) && | |||
36504 | ((MaskVT.is128BitVector() && Subtarget.hasVLX()) || | |||
36505 | (MaskVT.is256BitVector() && Subtarget.hasVLX()) || | |||
36506 | (MaskVT.is512BitVector() && Subtarget.hasAVX512()))) { | |||
36507 | if (!isAnyZero(Mask)) { | |||
36508 | int Rotation = matchShuffleAsElementRotate(V1, V2, Mask); | |||
36509 | if (0 < Rotation) { | |||
36510 | Shuffle = X86ISD::VALIGN; | |||
36511 | if (EltSizeInBits == 64) | |||
36512 | ShuffleVT = MVT::getVectorVT(MVT::i64, MaskVT.getSizeInBits() / 64); | |||
36513 | else | |||
36514 | ShuffleVT = MVT::getVectorVT(MVT::i32, MaskVT.getSizeInBits() / 32); | |||
36515 | PermuteImm = Rotation; | |||
36516 | return true; | |||
36517 | } | |||
36518 | } | |||
36519 | } | |||
36520 | ||||
36521 | // Attempt to match against PALIGNR byte rotate. | |||
36522 | if (AllowIntDomain && ((MaskVT.is128BitVector() && Subtarget.hasSSSE3()) || | |||
36523 | (MaskVT.is256BitVector() && Subtarget.hasAVX2()) || | |||
36524 | (MaskVT.is512BitVector() && Subtarget.hasBWI()))) { | |||
36525 | int ByteRotation = matchShuffleAsByteRotate(MaskVT, V1, V2, Mask); | |||
36526 | if (0 < ByteRotation) { | |||
36527 | Shuffle = X86ISD::PALIGNR; | |||
36528 | ShuffleVT = MVT::getVectorVT(MVT::i8, MaskVT.getSizeInBits() / 8); | |||
36529 | PermuteImm = ByteRotation; | |||
36530 | return true; | |||
36531 | } | |||
36532 | } | |||
36533 | ||||
36534 | // Attempt to combine to X86ISD::BLENDI. | |||
36535 | if ((NumMaskElts <= 8 && ((Subtarget.hasSSE41() && MaskVT.is128BitVector()) || | |||
36536 | (Subtarget.hasAVX() && MaskVT.is256BitVector()))) || | |||
36537 | (MaskVT == MVT::v16i16 && Subtarget.hasAVX2())) { | |||
36538 | uint64_t BlendMask = 0; | |||
36539 | bool ForceV1Zero = false, ForceV2Zero = false; | |||
36540 | SmallVector<int, 8> TargetMask(Mask.begin(), Mask.end()); | |||
36541 | if (matchShuffleAsBlend(V1, V2, TargetMask, Zeroable, ForceV1Zero, | |||
36542 | ForceV2Zero, BlendMask)) { | |||
36543 | if (MaskVT == MVT::v16i16) { | |||
36544 | // We can only use v16i16 PBLENDW if the lanes are repeated. | |||
36545 | SmallVector<int, 8> RepeatedMask; | |||
36546 | if (isRepeatedTargetShuffleMask(128, MaskVT, TargetMask, | |||
36547 | RepeatedMask)) { | |||
36548 | assert(RepeatedMask.size() == 8 &&(static_cast <bool> (RepeatedMask.size() == 8 && "Repeated mask size doesn't match!") ? void (0) : __assert_fail ("RepeatedMask.size() == 8 && \"Repeated mask size doesn't match!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 36549, __extension__ __PRETTY_FUNCTION__)) | |||
36549 | "Repeated mask size doesn't match!")(static_cast <bool> (RepeatedMask.size() == 8 && "Repeated mask size doesn't match!") ? void (0) : __assert_fail ("RepeatedMask.size() == 8 && \"Repeated mask size doesn't match!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 36549, __extension__ __PRETTY_FUNCTION__)); | |||
36550 | PermuteImm = 0; | |||
36551 | for (int i = 0; i < 8; ++i) | |||
36552 | if (RepeatedMask[i] >= 8) | |||
36553 | PermuteImm |= 1 << i; | |||
36554 | V1 = ForceV1Zero ? getZeroVector(MaskVT, Subtarget, DAG, DL) : V1; | |||
36555 | V2 = ForceV2Zero ? getZeroVector(MaskVT, Subtarget, DAG, DL) : V2; | |||
36556 | Shuffle = X86ISD::BLENDI; | |||
36557 | ShuffleVT = MaskVT; | |||
36558 | return true; | |||
36559 | } | |||
36560 | } else { | |||
36561 | V1 = ForceV1Zero ? getZeroVector(MaskVT, Subtarget, DAG, DL) : V1; | |||
36562 | V2 = ForceV2Zero ? getZeroVector(MaskVT, Subtarget, DAG, DL) : V2; | |||
36563 | PermuteImm = (unsigned)BlendMask; | |||
36564 | Shuffle = X86ISD::BLENDI; | |||
36565 | ShuffleVT = MaskVT; | |||
36566 | return true; | |||
36567 | } | |||
36568 | } | |||
36569 | } | |||
36570 | ||||
36571 | // Attempt to combine to INSERTPS, but only if it has elements that need to | |||
36572 | // be set to zero. | |||
36573 | if (AllowFloatDomain && EltSizeInBits == 32 && Subtarget.hasSSE41() && | |||
36574 | MaskVT.is128BitVector() && isAnyZero(Mask) && | |||
36575 | matchShuffleAsInsertPS(V1, V2, PermuteImm, Zeroable, Mask, DAG)) { | |||
36576 | Shuffle = X86ISD::INSERTPS; | |||
36577 | ShuffleVT = MVT::v4f32; | |||
36578 | return true; | |||
36579 | } | |||
36580 | ||||
36581 | // Attempt to combine to SHUFPD. | |||
36582 | if (AllowFloatDomain && EltSizeInBits == 64 && | |||
36583 | ((MaskVT.is128BitVector() && Subtarget.hasSSE2()) || | |||
36584 | (MaskVT.is256BitVector() && Subtarget.hasAVX()) || | |||
36585 | (MaskVT.is512BitVector() && Subtarget.hasAVX512()))) { | |||
36586 | bool ForceV1Zero = false, ForceV2Zero = false; | |||
36587 | if (matchShuffleWithSHUFPD(MaskVT, V1, V2, ForceV1Zero, ForceV2Zero, | |||
36588 | PermuteImm, Mask, Zeroable)) { | |||
36589 | V1 = ForceV1Zero ? getZeroVector(MaskVT, Subtarget, DAG, DL) : V1; | |||
36590 | V2 = ForceV2Zero ? getZeroVector(MaskVT, Subtarget, DAG, DL) : V2; | |||
36591 | Shuffle = X86ISD::SHUFP; | |||
36592 | ShuffleVT = MVT::getVectorVT(MVT::f64, MaskVT.getSizeInBits() / 64); | |||
36593 | return true; | |||
36594 | } | |||
36595 | } | |||
36596 | ||||
36597 | // Attempt to combine to SHUFPS. | |||
36598 | if (AllowFloatDomain && EltSizeInBits == 32 && | |||
36599 | ((MaskVT.is128BitVector() && Subtarget.hasSSE1()) || | |||
36600 | (MaskVT.is256BitVector() && Subtarget.hasAVX()) || | |||
36601 | (MaskVT.is512BitVector() && Subtarget.hasAVX512()))) { | |||
36602 | SmallVector<int, 4> RepeatedMask; | |||
36603 | if (isRepeatedTargetShuffleMask(128, MaskVT, Mask, RepeatedMask)) { | |||
36604 | // Match each half of the repeated mask, to determine if its just | |||
36605 | // referencing one of the vectors, is zeroable or entirely undef. | |||
36606 | auto MatchHalf = [&](unsigned Offset, int &S0, int &S1) { | |||
36607 | int M0 = RepeatedMask[Offset]; | |||
36608 | int M1 = RepeatedMask[Offset + 1]; | |||
36609 | ||||
36610 | if (isUndefInRange(RepeatedMask, Offset, 2)) { | |||
36611 | return DAG.getUNDEF(MaskVT); | |||
36612 | } else if (isUndefOrZeroInRange(RepeatedMask, Offset, 2)) { | |||
36613 | S0 = (SM_SentinelUndef == M0 ? -1 : 0); | |||
36614 | S1 = (SM_SentinelUndef == M1 ? -1 : 1); | |||
36615 | return getZeroVector(MaskVT, Subtarget, DAG, DL); | |||
36616 | } else if (isUndefOrInRange(M0, 0, 4) && isUndefOrInRange(M1, 0, 4)) { | |||
36617 | S0 = (SM_SentinelUndef == M0 ? -1 : M0 & 3); | |||
36618 | S1 = (SM_SentinelUndef == M1 ? -1 : M1 & 3); | |||
36619 | return V1; | |||
36620 | } else if (isUndefOrInRange(M0, 4, 8) && isUndefOrInRange(M1, 4, 8)) { | |||
36621 | S0 = (SM_SentinelUndef == M0 ? -1 : M0 & 3); | |||
36622 | S1 = (SM_SentinelUndef == M1 ? -1 : M1 & 3); | |||
36623 | return V2; | |||
36624 | } | |||
36625 | ||||
36626 | return SDValue(); | |||
36627 | }; | |||
36628 | ||||
36629 | int ShufMask[4] = {-1, -1, -1, -1}; | |||
36630 | SDValue Lo = MatchHalf(0, ShufMask[0], ShufMask[1]); | |||
36631 | SDValue Hi = MatchHalf(2, ShufMask[2], ShufMask[3]); | |||
36632 | ||||
36633 | if (Lo && Hi) { | |||
36634 | V1 = Lo; | |||
36635 | V2 = Hi; | |||
36636 | Shuffle = X86ISD::SHUFP; | |||
36637 | ShuffleVT = MVT::getVectorVT(MVT::f32, MaskVT.getSizeInBits() / 32); | |||
36638 | PermuteImm = getV4X86ShuffleImm(ShufMask); | |||
36639 | return true; | |||
36640 | } | |||
36641 | } | |||
36642 | } | |||
36643 | ||||
36644 | // Attempt to combine to INSERTPS more generally if X86ISD::SHUFP failed. | |||
36645 | if (AllowFloatDomain && EltSizeInBits == 32 && Subtarget.hasSSE41() && | |||
36646 | MaskVT.is128BitVector() && | |||
36647 | matchShuffleAsInsertPS(V1, V2, PermuteImm, Zeroable, Mask, DAG)) { | |||
36648 | Shuffle = X86ISD::INSERTPS; | |||
36649 | ShuffleVT = MVT::v4f32; | |||
36650 | return true; | |||
36651 | } | |||
36652 | ||||
36653 | return false; | |||
36654 | } | |||
36655 | ||||
36656 | static SDValue combineX86ShuffleChainWithExtract( | |||
36657 | ArrayRef<SDValue> Inputs, SDValue Root, ArrayRef<int> BaseMask, int Depth, | |||
36658 | bool HasVariableMask, bool AllowVariableCrossLaneMask, | |||
36659 | bool AllowVariablePerLaneMask, SelectionDAG &DAG, | |||
36660 | const X86Subtarget &Subtarget); | |||
36661 | ||||
36662 | /// Combine an arbitrary chain of shuffles into a single instruction if | |||
36663 | /// possible. | |||
36664 | /// | |||
36665 | /// This is the leaf of the recursive combine below. When we have found some | |||
36666 | /// chain of single-use x86 shuffle instructions and accumulated the combined | |||
36667 | /// shuffle mask represented by them, this will try to pattern match that mask | |||
36668 | /// into either a single instruction if there is a special purpose instruction | |||
36669 | /// for this operation, or into a PSHUFB instruction which is a fully general | |||
36670 | /// instruction but should only be used to replace chains over a certain depth. | |||
36671 | static SDValue combineX86ShuffleChain(ArrayRef<SDValue> Inputs, SDValue Root, | |||
36672 | ArrayRef<int> BaseMask, int Depth, | |||
36673 | bool HasVariableMask, | |||
36674 | bool AllowVariableCrossLaneMask, | |||
36675 | bool AllowVariablePerLaneMask, | |||
36676 | SelectionDAG &DAG, | |||
36677 | const X86Subtarget &Subtarget) { | |||
36678 | assert(!BaseMask.empty() && "Cannot combine an empty shuffle mask!")(static_cast <bool> (!BaseMask.empty() && "Cannot combine an empty shuffle mask!" ) ? void (0) : __assert_fail ("!BaseMask.empty() && \"Cannot combine an empty shuffle mask!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 36678, __extension__ __PRETTY_FUNCTION__)); | |||
36679 | assert((Inputs.size() == 1 || Inputs.size() == 2) &&(static_cast <bool> ((Inputs.size() == 1 || Inputs.size () == 2) && "Unexpected number of shuffle inputs!") ? void (0) : __assert_fail ("(Inputs.size() == 1 || Inputs.size() == 2) && \"Unexpected number of shuffle inputs!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 36680, __extension__ __PRETTY_FUNCTION__)) | |||
36680 | "Unexpected number of shuffle inputs!")(static_cast <bool> ((Inputs.size() == 1 || Inputs.size () == 2) && "Unexpected number of shuffle inputs!") ? void (0) : __assert_fail ("(Inputs.size() == 1 || Inputs.size() == 2) && \"Unexpected number of shuffle inputs!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 36680, __extension__ __PRETTY_FUNCTION__)); | |||
36681 | ||||
36682 | MVT RootVT = Root.getSimpleValueType(); | |||
36683 | unsigned RootSizeInBits = RootVT.getSizeInBits(); | |||
36684 | unsigned NumRootElts = RootVT.getVectorNumElements(); | |||
36685 | ||||
36686 | // Canonicalize shuffle input op to the requested type. | |||
36687 | // TODO: Support cases where Op is smaller than VT. | |||
36688 | auto CanonicalizeShuffleInput = [&](MVT VT, SDValue Op) { | |||
36689 | return DAG.getBitcast(VT, Op); | |||
36690 | }; | |||
36691 | ||||
36692 | // Find the inputs that enter the chain. Note that multiple uses are OK | |||
36693 | // here, we're not going to remove the operands we find. | |||
36694 | bool UnaryShuffle = (Inputs.size() == 1); | |||
36695 | SDValue V1 = peekThroughBitcasts(Inputs[0]); | |||
36696 | SDValue V2 = (UnaryShuffle ? DAG.getUNDEF(V1.getValueType()) | |||
36697 | : peekThroughBitcasts(Inputs[1])); | |||
36698 | ||||
36699 | MVT VT1 = V1.getSimpleValueType(); | |||
36700 | MVT VT2 = V2.getSimpleValueType(); | |||
36701 | assert(VT1.getSizeInBits() == RootSizeInBits &&(static_cast <bool> (VT1.getSizeInBits() == RootSizeInBits && VT2.getSizeInBits() == RootSizeInBits && "Vector size mismatch" ) ? void (0) : __assert_fail ("VT1.getSizeInBits() == RootSizeInBits && VT2.getSizeInBits() == RootSizeInBits && \"Vector size mismatch\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 36702, __extension__ __PRETTY_FUNCTION__)) | |||
36702 | VT2.getSizeInBits() == RootSizeInBits && "Vector size mismatch")(static_cast <bool> (VT1.getSizeInBits() == RootSizeInBits && VT2.getSizeInBits() == RootSizeInBits && "Vector size mismatch" ) ? void (0) : __assert_fail ("VT1.getSizeInBits() == RootSizeInBits && VT2.getSizeInBits() == RootSizeInBits && \"Vector size mismatch\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 36702, __extension__ __PRETTY_FUNCTION__)); | |||
36703 | ||||
36704 | SDLoc DL(Root); | |||
36705 | SDValue Res; | |||
36706 | ||||
36707 | unsigned NumBaseMaskElts = BaseMask.size(); | |||
36708 | if (NumBaseMaskElts == 1) { | |||
36709 | assert(BaseMask[0] == 0 && "Invalid shuffle index found!")(static_cast <bool> (BaseMask[0] == 0 && "Invalid shuffle index found!" ) ? void (0) : __assert_fail ("BaseMask[0] == 0 && \"Invalid shuffle index found!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 36709, __extension__ __PRETTY_FUNCTION__)); | |||
36710 | return CanonicalizeShuffleInput(RootVT, V1); | |||
36711 | } | |||
36712 | ||||
36713 | bool OptForSize = DAG.shouldOptForSize(); | |||
36714 | unsigned BaseMaskEltSizeInBits = RootSizeInBits / NumBaseMaskElts; | |||
36715 | bool FloatDomain = VT1.isFloatingPoint() || VT2.isFloatingPoint() || | |||
36716 | (RootVT.isFloatingPoint() && Depth >= 1) || | |||
36717 | (RootVT.is256BitVector() && !Subtarget.hasAVX2()); | |||
36718 | ||||
36719 | // Don't combine if we are a AVX512/EVEX target and the mask element size | |||
36720 | // is different from the root element size - this would prevent writemasks | |||
36721 | // from being reused. | |||
36722 | bool IsMaskedShuffle = false; | |||
36723 | if (RootSizeInBits == 512 || (Subtarget.hasVLX() && RootSizeInBits >= 128)) { | |||
36724 | if (Root.hasOneUse() && Root->use_begin()->getOpcode() == ISD::VSELECT && | |||
36725 | Root->use_begin()->getOperand(0).getScalarValueSizeInBits() == 1) { | |||
36726 | IsMaskedShuffle = true; | |||
36727 | } | |||
36728 | } | |||
36729 | ||||
36730 | // If we are shuffling a broadcast (and not introducing zeros) then | |||
36731 | // we can just use the broadcast directly. This works for smaller broadcast | |||
36732 | // elements as well as they already repeat across each mask element | |||
36733 | if (UnaryShuffle && isTargetShuffleSplat(V1) && !isAnyZero(BaseMask) && | |||
36734 | (BaseMaskEltSizeInBits % V1.getScalarValueSizeInBits()) == 0 && | |||
36735 | V1.getValueSizeInBits() >= RootSizeInBits) { | |||
36736 | return CanonicalizeShuffleInput(RootVT, V1); | |||
36737 | } | |||
36738 | ||||
36739 | SmallVector<int, 64> Mask(BaseMask.begin(), BaseMask.end()); | |||
36740 | ||||
36741 | // See if the shuffle is a hidden identity shuffle - repeated args in HOPs | |||
36742 | // etc. can be simplified. | |||
36743 | if (VT1 == VT2 && VT1.getSizeInBits() == RootSizeInBits && VT1.isVector()) { | |||
36744 | SmallVector<int> ScaledMask, IdentityMask; | |||
36745 | unsigned NumElts = VT1.getVectorNumElements(); | |||
36746 | if (Mask.size() <= NumElts && | |||
36747 | scaleShuffleElements(Mask, NumElts, ScaledMask)) { | |||
36748 | for (unsigned i = 0; i != NumElts; ++i) | |||
36749 | IdentityMask.push_back(i); | |||
36750 | if (isTargetShuffleEquivalent(RootVT, ScaledMask, IdentityMask, V1, V2)) | |||
36751 | return CanonicalizeShuffleInput(RootVT, V1); | |||
36752 | } | |||
36753 | } | |||
36754 | ||||
36755 | // Handle 128/256-bit lane shuffles of 512-bit vectors. | |||
36756 | if (RootVT.is512BitVector() && | |||
36757 | (NumBaseMaskElts == 2 || NumBaseMaskElts == 4)) { | |||
36758 | // If the upper subvectors are zeroable, then an extract+insert is more | |||
36759 | // optimal than using X86ISD::SHUF128. The insertion is free, even if it has | |||
36760 | // to zero the upper subvectors. | |||
36761 | if (isUndefOrZeroInRange(Mask, 1, NumBaseMaskElts - 1)) { | |||
36762 | if (Depth == 0 && Root.getOpcode() == ISD::INSERT_SUBVECTOR) | |||
36763 | return SDValue(); // Nothing to do! | |||
36764 | assert(isInRange(Mask[0], 0, NumBaseMaskElts) &&(static_cast <bool> (isInRange(Mask[0], 0, NumBaseMaskElts ) && "Unexpected lane shuffle") ? void (0) : __assert_fail ("isInRange(Mask[0], 0, NumBaseMaskElts) && \"Unexpected lane shuffle\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 36765, __extension__ __PRETTY_FUNCTION__)) | |||
36765 | "Unexpected lane shuffle")(static_cast <bool> (isInRange(Mask[0], 0, NumBaseMaskElts ) && "Unexpected lane shuffle") ? void (0) : __assert_fail ("isInRange(Mask[0], 0, NumBaseMaskElts) && \"Unexpected lane shuffle\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 36765, __extension__ __PRETTY_FUNCTION__)); | |||
36766 | Res = CanonicalizeShuffleInput(RootVT, V1); | |||
36767 | unsigned SubIdx = Mask[0] * (NumRootElts / NumBaseMaskElts); | |||
36768 | bool UseZero = isAnyZero(Mask); | |||
36769 | Res = extractSubVector(Res, SubIdx, DAG, DL, BaseMaskEltSizeInBits); | |||
36770 | return widenSubVector(Res, UseZero, Subtarget, DAG, DL, RootSizeInBits); | |||
36771 | } | |||
36772 | ||||
36773 | // Narrow shuffle mask to v4x128. | |||
36774 | SmallVector<int, 4> ScaledMask; | |||
36775 | assert((BaseMaskEltSizeInBits % 128) == 0 && "Illegal mask size")(static_cast <bool> ((BaseMaskEltSizeInBits % 128) == 0 && "Illegal mask size") ? void (0) : __assert_fail ( "(BaseMaskEltSizeInBits % 128) == 0 && \"Illegal mask size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 36775, __extension__ __PRETTY_FUNCTION__)); | |||
36776 | narrowShuffleMaskElts(BaseMaskEltSizeInBits / 128, Mask, ScaledMask); | |||
36777 | ||||
36778 | // Try to lower to vshuf64x2/vshuf32x4. | |||
36779 | auto MatchSHUF128 = [&](MVT ShuffleVT, const SDLoc &DL, | |||
36780 | ArrayRef<int> ScaledMask, SDValue V1, SDValue V2, | |||
36781 | SelectionDAG &DAG) { | |||
36782 | unsigned PermMask = 0; | |||
36783 | // Insure elements came from the same Op. | |||
36784 | SDValue Ops[2] = {DAG.getUNDEF(ShuffleVT), DAG.getUNDEF(ShuffleVT)}; | |||
36785 | for (int i = 0; i < 4; ++i) { | |||
36786 | assert(ScaledMask[i] >= -1 && "Illegal shuffle sentinel value")(static_cast <bool> (ScaledMask[i] >= -1 && "Illegal shuffle sentinel value" ) ? void (0) : __assert_fail ("ScaledMask[i] >= -1 && \"Illegal shuffle sentinel value\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 36786, __extension__ __PRETTY_FUNCTION__)); | |||
36787 | if (ScaledMask[i] < 0) | |||
36788 | continue; | |||
36789 | ||||
36790 | SDValue Op = ScaledMask[i] >= 4 ? V2 : V1; | |||
36791 | unsigned OpIndex = i / 2; | |||
36792 | if (Ops[OpIndex].isUndef()) | |||
36793 | Ops[OpIndex] = Op; | |||
36794 | else if (Ops[OpIndex] != Op) | |||
36795 | return SDValue(); | |||
36796 | ||||
36797 | // Convert the 128-bit shuffle mask selection values into 128-bit | |||
36798 | // selection bits defined by a vshuf64x2 instruction's immediate control | |||
36799 | // byte. | |||
36800 | PermMask |= (ScaledMask[i] % 4) << (i * 2); | |||
36801 | } | |||
36802 | ||||
36803 | return DAG.getNode(X86ISD::SHUF128, DL, ShuffleVT, | |||
36804 | CanonicalizeShuffleInput(ShuffleVT, Ops[0]), | |||
36805 | CanonicalizeShuffleInput(ShuffleVT, Ops[1]), | |||
36806 | DAG.getTargetConstant(PermMask, DL, MVT::i8)); | |||
36807 | }; | |||
36808 | ||||
36809 | // FIXME: Is there a better way to do this? is256BitLaneRepeatedShuffleMask | |||
36810 | // doesn't work because our mask is for 128 bits and we don't have an MVT | |||
36811 | // to match that. | |||
36812 | bool PreferPERMQ = UnaryShuffle && isUndefOrInRange(ScaledMask[0], 0, 2) && | |||
36813 | isUndefOrInRange(ScaledMask[1], 0, 2) && | |||
36814 | isUndefOrInRange(ScaledMask[2], 2, 4) && | |||
36815 | isUndefOrInRange(ScaledMask[3], 2, 4) && | |||
36816 | (ScaledMask[0] < 0 || ScaledMask[2] < 0 || | |||
36817 | ScaledMask[0] == (ScaledMask[2] % 2)) && | |||
36818 | (ScaledMask[1] < 0 || ScaledMask[3] < 0 || | |||
36819 | ScaledMask[1] == (ScaledMask[3] % 2)); | |||
36820 | ||||
36821 | if (!isAnyZero(ScaledMask) && !PreferPERMQ) { | |||
36822 | if (Depth == 0 && Root.getOpcode() == X86ISD::SHUF128) | |||
36823 | return SDValue(); // Nothing to do! | |||
36824 | MVT ShuffleVT = (FloatDomain ? MVT::v8f64 : MVT::v8i64); | |||
36825 | if (SDValue V = MatchSHUF128(ShuffleVT, DL, ScaledMask, V1, V2, DAG)) | |||
36826 | return DAG.getBitcast(RootVT, V); | |||
36827 | } | |||
36828 | } | |||
36829 | ||||
36830 | // Handle 128-bit lane shuffles of 256-bit vectors. | |||
36831 | if (RootVT.is256BitVector() && NumBaseMaskElts == 2) { | |||
36832 | // If the upper half is zeroable, then an extract+insert is more optimal | |||
36833 | // than using X86ISD::VPERM2X128. The insertion is free, even if it has to | |||
36834 | // zero the upper half. | |||
36835 | if (isUndefOrZero(Mask[1])) { | |||
36836 | if (Depth == 0 && Root.getOpcode() == ISD::INSERT_SUBVECTOR) | |||
36837 | return SDValue(); // Nothing to do! | |||
36838 | assert(isInRange(Mask[0], 0, 2) && "Unexpected lane shuffle")(static_cast <bool> (isInRange(Mask[0], 0, 2) && "Unexpected lane shuffle") ? void (0) : __assert_fail ("isInRange(Mask[0], 0, 2) && \"Unexpected lane shuffle\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 36838, __extension__ __PRETTY_FUNCTION__)); | |||
36839 | Res = CanonicalizeShuffleInput(RootVT, V1); | |||
36840 | Res = extract128BitVector(Res, Mask[0] * (NumRootElts / 2), DAG, DL); | |||
36841 | return widenSubVector(Res, Mask[1] == SM_SentinelZero, Subtarget, DAG, DL, | |||
36842 | 256); | |||
36843 | } | |||
36844 | ||||
36845 | // If we're splatting the low subvector, an insert-subvector 'concat' | |||
36846 | // pattern is quicker than VPERM2X128. | |||
36847 | // TODO: Add AVX2 support instead of VPERMQ/VPERMPD. | |||
36848 | if (Mask[0] == 0 && Mask[1] == 0 && !Subtarget.hasAVX2()) { | |||
36849 | if (Depth == 0 && Root.getOpcode() == ISD::INSERT_SUBVECTOR) | |||
36850 | return SDValue(); // Nothing to do! | |||
36851 | Res = CanonicalizeShuffleInput(RootVT, V1); | |||
36852 | Res = extractSubVector(Res, 0, DAG, DL, 128); | |||
36853 | return concatSubVectors(Res, Res, DAG, DL); | |||
36854 | } | |||
36855 | ||||
36856 | if (Depth == 0 && Root.getOpcode() == X86ISD::VPERM2X128) | |||
36857 | return SDValue(); // Nothing to do! | |||
36858 | ||||
36859 | // If we have AVX2, prefer to use VPERMQ/VPERMPD for unary shuffles unless | |||
36860 | // we need to use the zeroing feature. | |||
36861 | // Prefer blends for sequential shuffles unless we are optimizing for size. | |||
36862 | if (UnaryShuffle && | |||
36863 | !(Subtarget.hasAVX2() && isUndefOrInRange(Mask, 0, 2)) && | |||
36864 | (OptForSize || !isSequentialOrUndefOrZeroInRange(Mask, 0, 2, 0))) { | |||
36865 | unsigned PermMask = 0; | |||
36866 | PermMask |= ((Mask[0] < 0 ? 0x8 : (Mask[0] & 1)) << 0); | |||
36867 | PermMask |= ((Mask[1] < 0 ? 0x8 : (Mask[1] & 1)) << 4); | |||
36868 | return DAG.getNode( | |||
36869 | X86ISD::VPERM2X128, DL, RootVT, CanonicalizeShuffleInput(RootVT, V1), | |||
36870 | DAG.getUNDEF(RootVT), DAG.getTargetConstant(PermMask, DL, MVT::i8)); | |||
36871 | } | |||
36872 | ||||
36873 | if (Depth == 0 && Root.getOpcode() == X86ISD::SHUF128) | |||
36874 | return SDValue(); // Nothing to do! | |||
36875 | ||||
36876 | // TODO - handle AVX512VL cases with X86ISD::SHUF128. | |||
36877 | if (!UnaryShuffle && !IsMaskedShuffle) { | |||
36878 | assert(llvm::all_of(Mask, [](int M) { return 0 <= M && M < 4; }) &&(static_cast <bool> (llvm::all_of(Mask, [](int M) { return 0 <= M && M < 4; }) && "Unexpected shuffle sentinel value" ) ? void (0) : __assert_fail ("llvm::all_of(Mask, [](int M) { return 0 <= M && M < 4; }) && \"Unexpected shuffle sentinel value\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 36879, __extension__ __PRETTY_FUNCTION__)) | |||
36879 | "Unexpected shuffle sentinel value")(static_cast <bool> (llvm::all_of(Mask, [](int M) { return 0 <= M && M < 4; }) && "Unexpected shuffle sentinel value" ) ? void (0) : __assert_fail ("llvm::all_of(Mask, [](int M) { return 0 <= M && M < 4; }) && \"Unexpected shuffle sentinel value\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 36879, __extension__ __PRETTY_FUNCTION__)); | |||
36880 | // Prefer blends to X86ISD::VPERM2X128. | |||
36881 | if (!((Mask[0] == 0 && Mask[1] == 3) || (Mask[0] == 2 && Mask[1] == 1))) { | |||
36882 | unsigned PermMask = 0; | |||
36883 | PermMask |= ((Mask[0] & 3) << 0); | |||
36884 | PermMask |= ((Mask[1] & 3) << 4); | |||
36885 | SDValue LHS = isInRange(Mask[0], 0, 2) ? V1 : V2; | |||
36886 | SDValue RHS = isInRange(Mask[1], 0, 2) ? V1 : V2; | |||
36887 | return DAG.getNode(X86ISD::VPERM2X128, DL, RootVT, | |||
36888 | CanonicalizeShuffleInput(RootVT, LHS), | |||
36889 | CanonicalizeShuffleInput(RootVT, RHS), | |||
36890 | DAG.getTargetConstant(PermMask, DL, MVT::i8)); | |||
36891 | } | |||
36892 | } | |||
36893 | } | |||
36894 | ||||
36895 | // For masks that have been widened to 128-bit elements or more, | |||
36896 | // narrow back down to 64-bit elements. | |||
36897 | if (BaseMaskEltSizeInBits > 64) { | |||
36898 | assert((BaseMaskEltSizeInBits % 64) == 0 && "Illegal mask size")(static_cast <bool> ((BaseMaskEltSizeInBits % 64) == 0 && "Illegal mask size") ? void (0) : __assert_fail ("(BaseMaskEltSizeInBits % 64) == 0 && \"Illegal mask size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 36898, __extension__ __PRETTY_FUNCTION__)); | |||
36899 | int MaskScale = BaseMaskEltSizeInBits / 64; | |||
36900 | SmallVector<int, 64> ScaledMask; | |||
36901 | narrowShuffleMaskElts(MaskScale, Mask, ScaledMask); | |||
36902 | Mask = std::move(ScaledMask); | |||
36903 | } | |||
36904 | ||||
36905 | // For masked shuffles, we're trying to match the root width for better | |||
36906 | // writemask folding, attempt to scale the mask. | |||
36907 | // TODO - variable shuffles might need this to be widened again. | |||
36908 | if (IsMaskedShuffle && NumRootElts > Mask.size()) { | |||
36909 | assert((NumRootElts % Mask.size()) == 0 && "Illegal mask size")(static_cast <bool> ((NumRootElts % Mask.size()) == 0 && "Illegal mask size") ? void (0) : __assert_fail ("(NumRootElts % Mask.size()) == 0 && \"Illegal mask size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 36909, __extension__ __PRETTY_FUNCTION__)); | |||
36910 | int MaskScale = NumRootElts / Mask.size(); | |||
36911 | SmallVector<int, 64> ScaledMask; | |||
36912 | narrowShuffleMaskElts(MaskScale, Mask, ScaledMask); | |||
36913 | Mask = std::move(ScaledMask); | |||
36914 | } | |||
36915 | ||||
36916 | unsigned NumMaskElts = Mask.size(); | |||
36917 | unsigned MaskEltSizeInBits = RootSizeInBits / NumMaskElts; | |||
36918 | ||||
36919 | // Determine the effective mask value type. | |||
36920 | FloatDomain &= (32 <= MaskEltSizeInBits); | |||
36921 | MVT MaskVT = FloatDomain ? MVT::getFloatingPointVT(MaskEltSizeInBits) | |||
36922 | : MVT::getIntegerVT(MaskEltSizeInBits); | |||
36923 | MaskVT = MVT::getVectorVT(MaskVT, NumMaskElts); | |||
36924 | ||||
36925 | // Only allow legal mask types. | |||
36926 | if (!DAG.getTargetLoweringInfo().isTypeLegal(MaskVT)) | |||
36927 | return SDValue(); | |||
36928 | ||||
36929 | // Attempt to match the mask against known shuffle patterns. | |||
36930 | MVT ShuffleSrcVT, ShuffleVT; | |||
36931 | unsigned Shuffle, PermuteImm; | |||
36932 | ||||
36933 | // Which shuffle domains are permitted? | |||
36934 | // Permit domain crossing at higher combine depths. | |||
36935 | // TODO: Should we indicate which domain is preferred if both are allowed? | |||
36936 | bool AllowFloatDomain = FloatDomain || (Depth >= 3); | |||
36937 | bool AllowIntDomain = (!FloatDomain || (Depth >= 3)) && Subtarget.hasSSE2() && | |||
36938 | (!MaskVT.is256BitVector() || Subtarget.hasAVX2()); | |||
36939 | ||||
36940 | // Determine zeroable mask elements. | |||
36941 | APInt KnownUndef, KnownZero; | |||
36942 | resolveZeroablesFromTargetShuffle(Mask, KnownUndef, KnownZero); | |||
36943 | APInt Zeroable = KnownUndef | KnownZero; | |||
36944 | ||||
36945 | if (UnaryShuffle) { | |||
36946 | // Attempt to match against broadcast-from-vector. | |||
36947 | // Limit AVX1 to cases where we're loading+broadcasting a scalar element. | |||
36948 | if ((Subtarget.hasAVX2() || | |||
36949 | (Subtarget.hasAVX() && 32 <= MaskEltSizeInBits)) && | |||
36950 | (!IsMaskedShuffle || NumRootElts == NumMaskElts)) { | |||
36951 | if (isUndefOrEqual(Mask, 0)) { | |||
36952 | if (V1.getValueType() == MaskVT && | |||
36953 | V1.getOpcode() == ISD::SCALAR_TO_VECTOR && | |||
36954 | MayFoldLoad(V1.getOperand(0))) { | |||
36955 | if (Depth == 0 && Root.getOpcode() == X86ISD::VBROADCAST) | |||
36956 | return SDValue(); // Nothing to do! | |||
36957 | Res = V1.getOperand(0); | |||
36958 | Res = DAG.getNode(X86ISD::VBROADCAST, DL, MaskVT, Res); | |||
36959 | return DAG.getBitcast(RootVT, Res); | |||
36960 | } | |||
36961 | if (Subtarget.hasAVX2()) { | |||
36962 | if (Depth == 0 && Root.getOpcode() == X86ISD::VBROADCAST) | |||
36963 | return SDValue(); // Nothing to do! | |||
36964 | Res = CanonicalizeShuffleInput(MaskVT, V1); | |||
36965 | Res = DAG.getNode(X86ISD::VBROADCAST, DL, MaskVT, Res); | |||
36966 | return DAG.getBitcast(RootVT, Res); | |||
36967 | } | |||
36968 | } | |||
36969 | } | |||
36970 | ||||
36971 | SDValue NewV1 = V1; // Save operand in case early exit happens. | |||
36972 | if (matchUnaryShuffle(MaskVT, Mask, AllowFloatDomain, AllowIntDomain, NewV1, | |||
36973 | DL, DAG, Subtarget, Shuffle, ShuffleSrcVT, | |||
36974 | ShuffleVT) && | |||
36975 | (!IsMaskedShuffle || | |||
36976 | (NumRootElts == ShuffleVT.getVectorNumElements()))) { | |||
36977 | if (Depth == 0 && Root.getOpcode() == Shuffle) | |||
36978 | return SDValue(); // Nothing to do! | |||
36979 | Res = CanonicalizeShuffleInput(ShuffleSrcVT, NewV1); | |||
36980 | Res = DAG.getNode(Shuffle, DL, ShuffleVT, Res); | |||
36981 | return DAG.getBitcast(RootVT, Res); | |||
36982 | } | |||
36983 | ||||
36984 | if (matchUnaryPermuteShuffle(MaskVT, Mask, Zeroable, AllowFloatDomain, | |||
36985 | AllowIntDomain, Subtarget, Shuffle, ShuffleVT, | |||
36986 | PermuteImm) && | |||
36987 | (!IsMaskedShuffle || | |||
36988 | (NumRootElts == ShuffleVT.getVectorNumElements()))) { | |||
36989 | if (Depth == 0 && Root.getOpcode() == Shuffle) | |||
36990 | return SDValue(); // Nothing to do! | |||
36991 | Res = CanonicalizeShuffleInput(ShuffleVT, V1); | |||
36992 | Res = DAG.getNode(Shuffle, DL, ShuffleVT, Res, | |||
36993 | DAG.getTargetConstant(PermuteImm, DL, MVT::i8)); | |||
36994 | return DAG.getBitcast(RootVT, Res); | |||
36995 | } | |||
36996 | } | |||
36997 | ||||
36998 | // Attempt to combine to INSERTPS, but only if the inserted element has come | |||
36999 | // from a scalar. | |||
37000 | // TODO: Handle other insertions here as well? | |||
37001 | if (!UnaryShuffle && AllowFloatDomain && RootSizeInBits == 128 && | |||
37002 | Subtarget.hasSSE41() && | |||
37003 | !isTargetShuffleEquivalent(MaskVT, Mask, {4, 1, 2, 3})) { | |||
37004 | if (MaskEltSizeInBits == 32) { | |||
37005 | SDValue SrcV1 = V1, SrcV2 = V2; | |||
37006 | if (matchShuffleAsInsertPS(SrcV1, SrcV2, PermuteImm, Zeroable, Mask, | |||
37007 | DAG) && | |||
37008 | SrcV2.getOpcode() == ISD::SCALAR_TO_VECTOR) { | |||
37009 | if (Depth == 0 && Root.getOpcode() == X86ISD::INSERTPS) | |||
37010 | return SDValue(); // Nothing to do! | |||
37011 | Res = DAG.getNode(X86ISD::INSERTPS, DL, MVT::v4f32, | |||
37012 | CanonicalizeShuffleInput(MVT::v4f32, SrcV1), | |||
37013 | CanonicalizeShuffleInput(MVT::v4f32, SrcV2), | |||
37014 | DAG.getTargetConstant(PermuteImm, DL, MVT::i8)); | |||
37015 | return DAG.getBitcast(RootVT, Res); | |||
37016 | } | |||
37017 | } | |||
37018 | if (MaskEltSizeInBits == 64 && | |||
37019 | isTargetShuffleEquivalent(MaskVT, Mask, {0, 2}) && | |||
37020 | V2.getOpcode() == ISD::SCALAR_TO_VECTOR && | |||
37021 | V2.getScalarValueSizeInBits() <= 32) { | |||
37022 | if (Depth == 0 && Root.getOpcode() == X86ISD::INSERTPS) | |||
37023 | return SDValue(); // Nothing to do! | |||
37024 | PermuteImm = (/*DstIdx*/2 << 4) | (/*SrcIdx*/0 << 0); | |||
37025 | Res = DAG.getNode(X86ISD::INSERTPS, DL, MVT::v4f32, | |||
37026 | CanonicalizeShuffleInput(MVT::v4f32, V1), | |||
37027 | CanonicalizeShuffleInput(MVT::v4f32, V2), | |||
37028 | DAG.getTargetConstant(PermuteImm, DL, MVT::i8)); | |||
37029 | return DAG.getBitcast(RootVT, Res); | |||
37030 | } | |||
37031 | } | |||
37032 | ||||
37033 | SDValue NewV1 = V1; // Save operands in case early exit happens. | |||
37034 | SDValue NewV2 = V2; | |||
37035 | if (matchBinaryShuffle(MaskVT, Mask, AllowFloatDomain, AllowIntDomain, NewV1, | |||
37036 | NewV2, DL, DAG, Subtarget, Shuffle, ShuffleSrcVT, | |||
37037 | ShuffleVT, UnaryShuffle) && | |||
37038 | (!IsMaskedShuffle || (NumRootElts == ShuffleVT.getVectorNumElements()))) { | |||
37039 | if (Depth == 0 && Root.getOpcode() == Shuffle) | |||
37040 | return SDValue(); // Nothing to do! | |||
37041 | NewV1 = CanonicalizeShuffleInput(ShuffleSrcVT, NewV1); | |||
37042 | NewV2 = CanonicalizeShuffleInput(ShuffleSrcVT, NewV2); | |||
37043 | Res = DAG.getNode(Shuffle, DL, ShuffleVT, NewV1, NewV2); | |||
37044 | return DAG.getBitcast(RootVT, Res); | |||
37045 | } | |||
37046 | ||||
37047 | NewV1 = V1; // Save operands in case early exit happens. | |||
37048 | NewV2 = V2; | |||
37049 | if (matchBinaryPermuteShuffle(MaskVT, Mask, Zeroable, AllowFloatDomain, | |||
37050 | AllowIntDomain, NewV1, NewV2, DL, DAG, | |||
37051 | Subtarget, Shuffle, ShuffleVT, PermuteImm) && | |||
37052 | (!IsMaskedShuffle || (NumRootElts == ShuffleVT.getVectorNumElements()))) { | |||
37053 | if (Depth == 0 && Root.getOpcode() == Shuffle) | |||
37054 | return SDValue(); // Nothing to do! | |||
37055 | NewV1 = CanonicalizeShuffleInput(ShuffleVT, NewV1); | |||
37056 | NewV2 = CanonicalizeShuffleInput(ShuffleVT, NewV2); | |||
37057 | Res = DAG.getNode(Shuffle, DL, ShuffleVT, NewV1, NewV2, | |||
37058 | DAG.getTargetConstant(PermuteImm, DL, MVT::i8)); | |||
37059 | return DAG.getBitcast(RootVT, Res); | |||
37060 | } | |||
37061 | ||||
37062 | // Typically from here on, we need an integer version of MaskVT. | |||
37063 | MVT IntMaskVT = MVT::getIntegerVT(MaskEltSizeInBits); | |||
37064 | IntMaskVT = MVT::getVectorVT(IntMaskVT, NumMaskElts); | |||
37065 | ||||
37066 | // Annoyingly, SSE4A instructions don't map into the above match helpers. | |||
37067 | if (Subtarget.hasSSE4A() && AllowIntDomain && RootSizeInBits == 128) { | |||
37068 | uint64_t BitLen, BitIdx; | |||
37069 | if (matchShuffleAsEXTRQ(IntMaskVT, V1, V2, Mask, BitLen, BitIdx, | |||
37070 | Zeroable)) { | |||
37071 | if (Depth == 0 && Root.getOpcode() == X86ISD::EXTRQI) | |||
37072 | return SDValue(); // Nothing to do! | |||
37073 | V1 = CanonicalizeShuffleInput(IntMaskVT, V1); | |||
37074 | Res = DAG.getNode(X86ISD::EXTRQI, DL, IntMaskVT, V1, | |||
37075 | DAG.getTargetConstant(BitLen, DL, MVT::i8), | |||
37076 | DAG.getTargetConstant(BitIdx, DL, MVT::i8)); | |||
37077 | return DAG.getBitcast(RootVT, Res); | |||
37078 | } | |||
37079 | ||||
37080 | if (matchShuffleAsINSERTQ(IntMaskVT, V1, V2, Mask, BitLen, BitIdx)) { | |||
37081 | if (Depth == 0 && Root.getOpcode() == X86ISD::INSERTQI) | |||
37082 | return SDValue(); // Nothing to do! | |||
37083 | V1 = CanonicalizeShuffleInput(IntMaskVT, V1); | |||
37084 | V2 = CanonicalizeShuffleInput(IntMaskVT, V2); | |||
37085 | Res = DAG.getNode(X86ISD::INSERTQI, DL, IntMaskVT, V1, V2, | |||
37086 | DAG.getTargetConstant(BitLen, DL, MVT::i8), | |||
37087 | DAG.getTargetConstant(BitIdx, DL, MVT::i8)); | |||
37088 | return DAG.getBitcast(RootVT, Res); | |||
37089 | } | |||
37090 | } | |||
37091 | ||||
37092 | // Match shuffle against TRUNCATE patterns. | |||
37093 | if (AllowIntDomain && MaskEltSizeInBits < 64 && Subtarget.hasAVX512()) { | |||
37094 | // Match against a VTRUNC instruction, accounting for src/dst sizes. | |||
37095 | if (matchShuffleAsVTRUNC(ShuffleSrcVT, ShuffleVT, IntMaskVT, Mask, Zeroable, | |||
37096 | Subtarget)) { | |||
37097 | bool IsTRUNCATE = ShuffleVT.getVectorNumElements() == | |||
37098 | ShuffleSrcVT.getVectorNumElements(); | |||
37099 | unsigned Opc = | |||
37100 | IsTRUNCATE ? (unsigned)ISD::TRUNCATE : (unsigned)X86ISD::VTRUNC; | |||
37101 | if (Depth == 0 && Root.getOpcode() == Opc) | |||
37102 | return SDValue(); // Nothing to do! | |||
37103 | V1 = CanonicalizeShuffleInput(ShuffleSrcVT, V1); | |||
37104 | Res = DAG.getNode(Opc, DL, ShuffleVT, V1); | |||
37105 | if (ShuffleVT.getSizeInBits() < RootSizeInBits) | |||
37106 | Res = widenSubVector(Res, true, Subtarget, DAG, DL, RootSizeInBits); | |||
37107 | return DAG.getBitcast(RootVT, Res); | |||
37108 | } | |||
37109 | ||||
37110 | // Do we need a more general binary truncation pattern? | |||
37111 | if (RootSizeInBits < 512 && | |||
37112 | ((RootVT.is256BitVector() && Subtarget.useAVX512Regs()) || | |||
37113 | (RootVT.is128BitVector() && Subtarget.hasVLX())) && | |||
37114 | (MaskEltSizeInBits > 8 || Subtarget.hasBWI()) && | |||
37115 | isSequentialOrUndefInRange(Mask, 0, NumMaskElts, 0, 2)) { | |||
37116 | if (Depth == 0 && Root.getOpcode() == ISD::TRUNCATE) | |||
37117 | return SDValue(); // Nothing to do! | |||
37118 | ShuffleSrcVT = MVT::getIntegerVT(MaskEltSizeInBits * 2); | |||
37119 | ShuffleSrcVT = MVT::getVectorVT(ShuffleSrcVT, NumMaskElts / 2); | |||
37120 | V1 = CanonicalizeShuffleInput(ShuffleSrcVT, V1); | |||
37121 | V2 = CanonicalizeShuffleInput(ShuffleSrcVT, V2); | |||
37122 | ShuffleSrcVT = MVT::getIntegerVT(MaskEltSizeInBits * 2); | |||
37123 | ShuffleSrcVT = MVT::getVectorVT(ShuffleSrcVT, NumMaskElts); | |||
37124 | Res = DAG.getNode(ISD::CONCAT_VECTORS, DL, ShuffleSrcVT, V1, V2); | |||
37125 | Res = DAG.getNode(ISD::TRUNCATE, DL, IntMaskVT, Res); | |||
37126 | return DAG.getBitcast(RootVT, Res); | |||
37127 | } | |||
37128 | } | |||
37129 | ||||
37130 | // Don't try to re-form single instruction chains under any circumstances now | |||
37131 | // that we've done encoding canonicalization for them. | |||
37132 | if (Depth < 1) | |||
37133 | return SDValue(); | |||
37134 | ||||
37135 | // Depth threshold above which we can efficiently use variable mask shuffles. | |||
37136 | int VariableCrossLaneShuffleDepth = | |||
37137 | Subtarget.hasFastVariableCrossLaneShuffle() ? 1 : 2; | |||
37138 | int VariablePerLaneShuffleDepth = | |||
37139 | Subtarget.hasFastVariablePerLaneShuffle() ? 1 : 2; | |||
37140 | AllowVariableCrossLaneMask &= | |||
37141 | (Depth >= VariableCrossLaneShuffleDepth) || HasVariableMask; | |||
37142 | AllowVariablePerLaneMask &= | |||
37143 | (Depth >= VariablePerLaneShuffleDepth) || HasVariableMask; | |||
37144 | // VPERMI2W/VPERMI2B are 3 uops on Skylake and Icelake so we require a | |||
37145 | // higher depth before combining them. | |||
37146 | bool AllowBWIVPERMV3 = | |||
37147 | (Depth >= (VariableCrossLaneShuffleDepth + 2) || HasVariableMask); | |||
37148 | ||||
37149 | bool MaskContainsZeros = isAnyZero(Mask); | |||
37150 | ||||
37151 | if (is128BitLaneCrossingShuffleMask(MaskVT, Mask)) { | |||
37152 | // If we have a single input lane-crossing shuffle then lower to VPERMV. | |||
37153 | if (UnaryShuffle && AllowVariableCrossLaneMask && !MaskContainsZeros) { | |||
37154 | if (Subtarget.hasAVX2() && | |||
37155 | (MaskVT == MVT::v8f32 || MaskVT == MVT::v8i32)) { | |||
37156 | SDValue VPermMask = getConstVector(Mask, IntMaskVT, DAG, DL, true); | |||
37157 | Res = CanonicalizeShuffleInput(MaskVT, V1); | |||
37158 | Res = DAG.getNode(X86ISD::VPERMV, DL, MaskVT, VPermMask, Res); | |||
37159 | return DAG.getBitcast(RootVT, Res); | |||
37160 | } | |||
37161 | // AVX512 variants (non-VLX will pad to 512-bit shuffles). | |||
37162 | if ((Subtarget.hasAVX512() && | |||
37163 | (MaskVT == MVT::v8f64 || MaskVT == MVT::v8i64 || | |||
37164 | MaskVT == MVT::v16f32 || MaskVT == MVT::v16i32)) || | |||
37165 | (Subtarget.hasBWI() && | |||
37166 | (MaskVT == MVT::v16i16 || MaskVT == MVT::v32i16)) || | |||
37167 | (Subtarget.hasVBMI() && | |||
37168 | (MaskVT == MVT::v32i8 || MaskVT == MVT::v64i8))) { | |||
37169 | V1 = CanonicalizeShuffleInput(MaskVT, V1); | |||
37170 | V2 = DAG.getUNDEF(MaskVT); | |||
37171 | Res = lowerShuffleWithPERMV(DL, MaskVT, Mask, V1, V2, Subtarget, DAG); | |||
37172 | return DAG.getBitcast(RootVT, Res); | |||
37173 | } | |||
37174 | } | |||
37175 | ||||
37176 | // Lower a unary+zero lane-crossing shuffle as VPERMV3 with a zero | |||
37177 | // vector as the second source (non-VLX will pad to 512-bit shuffles). | |||
37178 | if (UnaryShuffle && AllowVariableCrossLaneMask && | |||
37179 | ((Subtarget.hasAVX512() && | |||
37180 | (MaskVT == MVT::v8f64 || MaskVT == MVT::v8i64 || | |||
37181 | MaskVT == MVT::v4f64 || MaskVT == MVT::v4i64 || | |||
37182 | MaskVT == MVT::v8f32 || MaskVT == MVT::v8i32 || | |||
37183 | MaskVT == MVT::v16f32 || MaskVT == MVT::v16i32)) || | |||
37184 | (Subtarget.hasBWI() && AllowBWIVPERMV3 && | |||
37185 | (MaskVT == MVT::v16i16 || MaskVT == MVT::v32i16)) || | |||
37186 | (Subtarget.hasVBMI() && AllowBWIVPERMV3 && | |||
37187 | (MaskVT == MVT::v32i8 || MaskVT == MVT::v64i8)))) { | |||
37188 | // Adjust shuffle mask - replace SM_SentinelZero with second source index. | |||
37189 | for (unsigned i = 0; i != NumMaskElts; ++i) | |||
37190 | if (Mask[i] == SM_SentinelZero) | |||
37191 | Mask[i] = NumMaskElts + i; | |||
37192 | V1 = CanonicalizeShuffleInput(MaskVT, V1); | |||
37193 | V2 = getZeroVector(MaskVT, Subtarget, DAG, DL); | |||
37194 | Res = lowerShuffleWithPERMV(DL, MaskVT, Mask, V1, V2, Subtarget, DAG); | |||
37195 | return DAG.getBitcast(RootVT, Res); | |||
37196 | } | |||
37197 | ||||
37198 | // If that failed and either input is extracted then try to combine as a | |||
37199 | // shuffle with the larger type. | |||
37200 | if (SDValue WideShuffle = combineX86ShuffleChainWithExtract( | |||
37201 | Inputs, Root, BaseMask, Depth, HasVariableMask, | |||
37202 | AllowVariableCrossLaneMask, AllowVariablePerLaneMask, DAG, | |||
37203 | Subtarget)) | |||
37204 | return WideShuffle; | |||
37205 | ||||
37206 | // If we have a dual input lane-crossing shuffle then lower to VPERMV3, | |||
37207 | // (non-VLX will pad to 512-bit shuffles). | |||
37208 | if (AllowVariableCrossLaneMask && !MaskContainsZeros && | |||
37209 | ((Subtarget.hasAVX512() && | |||
37210 | (MaskVT == MVT::v8f64 || MaskVT == MVT::v8i64 || | |||
37211 | MaskVT == MVT::v4f64 || MaskVT == MVT::v4i64 || | |||
37212 | MaskVT == MVT::v16f32 || MaskVT == MVT::v16i32 || | |||
37213 | MaskVT == MVT::v8f32 || MaskVT == MVT::v8i32)) || | |||
37214 | (Subtarget.hasBWI() && AllowBWIVPERMV3 && | |||
37215 | (MaskVT == MVT::v16i16 || MaskVT == MVT::v32i16)) || | |||
37216 | (Subtarget.hasVBMI() && AllowBWIVPERMV3 && | |||
37217 | (MaskVT == MVT::v32i8 || MaskVT == MVT::v64i8)))) { | |||
37218 | V1 = CanonicalizeShuffleInput(MaskVT, V1); | |||
37219 | V2 = CanonicalizeShuffleInput(MaskVT, V2); | |||
37220 | Res = lowerShuffleWithPERMV(DL, MaskVT, Mask, V1, V2, Subtarget, DAG); | |||
37221 | return DAG.getBitcast(RootVT, Res); | |||
37222 | } | |||
37223 | return SDValue(); | |||
37224 | } | |||
37225 | ||||
37226 | // See if we can combine a single input shuffle with zeros to a bit-mask, | |||
37227 | // which is much simpler than any shuffle. | |||
37228 | if (UnaryShuffle && MaskContainsZeros && AllowVariablePerLaneMask && | |||
37229 | isSequentialOrUndefOrZeroInRange(Mask, 0, NumMaskElts, 0) && | |||
37230 | DAG.getTargetLoweringInfo().isTypeLegal(MaskVT)) { | |||
37231 | APInt Zero = APInt::getZero(MaskEltSizeInBits); | |||
37232 | APInt AllOnes = APInt::getAllOnes(MaskEltSizeInBits); | |||
37233 | APInt UndefElts(NumMaskElts, 0); | |||
37234 | SmallVector<APInt, 64> EltBits(NumMaskElts, Zero); | |||
37235 | for (unsigned i = 0; i != NumMaskElts; ++i) { | |||
37236 | int M = Mask[i]; | |||
37237 | if (M == SM_SentinelUndef) { | |||
37238 | UndefElts.setBit(i); | |||
37239 | continue; | |||
37240 | } | |||
37241 | if (M == SM_SentinelZero) | |||
37242 | continue; | |||
37243 | EltBits[i] = AllOnes; | |||
37244 | } | |||
37245 | SDValue BitMask = getConstVector(EltBits, UndefElts, MaskVT, DAG, DL); | |||
37246 | Res = CanonicalizeShuffleInput(MaskVT, V1); | |||
37247 | unsigned AndOpcode = | |||
37248 | MaskVT.isFloatingPoint() ? unsigned(X86ISD::FAND) : unsigned(ISD::AND); | |||
37249 | Res = DAG.getNode(AndOpcode, DL, MaskVT, Res, BitMask); | |||
37250 | return DAG.getBitcast(RootVT, Res); | |||
37251 | } | |||
37252 | ||||
37253 | // If we have a single input shuffle with different shuffle patterns in the | |||
37254 | // the 128-bit lanes use the variable mask to VPERMILPS. | |||
37255 | // TODO Combine other mask types at higher depths. | |||
37256 | if (UnaryShuffle && AllowVariablePerLaneMask && !MaskContainsZeros && | |||
37257 | ((MaskVT == MVT::v8f32 && Subtarget.hasAVX()) || | |||
37258 | (MaskVT == MVT::v16f32 && Subtarget.hasAVX512()))) { | |||
37259 | SmallVector<SDValue, 16> VPermIdx; | |||
37260 | for (int M : Mask) { | |||
37261 | SDValue Idx = | |||
37262 | M < 0 ? DAG.getUNDEF(MVT::i32) : DAG.getConstant(M % 4, DL, MVT::i32); | |||
37263 | VPermIdx.push_back(Idx); | |||
37264 | } | |||
37265 | SDValue VPermMask = DAG.getBuildVector(IntMaskVT, DL, VPermIdx); | |||
37266 | Res = CanonicalizeShuffleInput(MaskVT, V1); | |||
37267 | Res = DAG.getNode(X86ISD::VPERMILPV, DL, MaskVT, Res, VPermMask); | |||
37268 | return DAG.getBitcast(RootVT, Res); | |||
37269 | } | |||
37270 | ||||
37271 | // With XOP, binary shuffles of 128/256-bit floating point vectors can combine | |||
37272 | // to VPERMIL2PD/VPERMIL2PS. | |||
37273 | if (AllowVariablePerLaneMask && Subtarget.hasXOP() && | |||
37274 | (MaskVT == MVT::v2f64 || MaskVT == MVT::v4f64 || MaskVT == MVT::v4f32 || | |||
37275 | MaskVT == MVT::v8f32)) { | |||
37276 | // VPERMIL2 Operation. | |||
37277 | // Bits[3] - Match Bit. | |||
37278 | // Bits[2:1] - (Per Lane) PD Shuffle Mask. | |||
37279 | // Bits[2:0] - (Per Lane) PS Shuffle Mask. | |||
37280 | unsigned NumLanes = MaskVT.getSizeInBits() / 128; | |||
37281 | unsigned NumEltsPerLane = NumMaskElts / NumLanes; | |||
37282 | SmallVector<int, 8> VPerm2Idx; | |||
37283 | unsigned M2ZImm = 0; | |||
37284 | for (int M : Mask) { | |||
37285 | if (M == SM_SentinelUndef) { | |||
37286 | VPerm2Idx.push_back(-1); | |||
37287 | continue; | |||
37288 | } | |||
37289 | if (M == SM_SentinelZero) { | |||
37290 | M2ZImm = 2; | |||
37291 | VPerm2Idx.push_back(8); | |||
37292 | continue; | |||
37293 | } | |||
37294 | int Index = (M % NumEltsPerLane) + ((M / NumMaskElts) * NumEltsPerLane); | |||
37295 | Index = (MaskVT.getScalarSizeInBits() == 64 ? Index << 1 : Index); | |||
37296 | VPerm2Idx.push_back(Index); | |||
37297 | } | |||
37298 | V1 = CanonicalizeShuffleInput(MaskVT, V1); | |||
37299 | V2 = CanonicalizeShuffleInput(MaskVT, V2); | |||
37300 | SDValue VPerm2MaskOp = getConstVector(VPerm2Idx, IntMaskVT, DAG, DL, true); | |||
37301 | Res = DAG.getNode(X86ISD::VPERMIL2, DL, MaskVT, V1, V2, VPerm2MaskOp, | |||
37302 | DAG.getTargetConstant(M2ZImm, DL, MVT::i8)); | |||
37303 | return DAG.getBitcast(RootVT, Res); | |||
37304 | } | |||
37305 | ||||
37306 | // If we have 3 or more shuffle instructions or a chain involving a variable | |||
37307 | // mask, we can replace them with a single PSHUFB instruction profitably. | |||
37308 | // Intel's manuals suggest only using PSHUFB if doing so replacing 5 | |||
37309 | // instructions, but in practice PSHUFB tends to be *very* fast so we're | |||
37310 | // more aggressive. | |||
37311 | if (UnaryShuffle && AllowVariablePerLaneMask && | |||
37312 | ((RootVT.is128BitVector() && Subtarget.hasSSSE3()) || | |||
37313 | (RootVT.is256BitVector() && Subtarget.hasAVX2()) || | |||
37314 | (RootVT.is512BitVector() && Subtarget.hasBWI()))) { | |||
37315 | SmallVector<SDValue, 16> PSHUFBMask; | |||
37316 | int NumBytes = RootVT.getSizeInBits() / 8; | |||
37317 | int Ratio = NumBytes / NumMaskElts; | |||
37318 | for (int i = 0; i < NumBytes; ++i) { | |||
37319 | int M = Mask[i / Ratio]; | |||
37320 | if (M == SM_SentinelUndef) { | |||
37321 | PSHUFBMask.push_back(DAG.getUNDEF(MVT::i8)); | |||
37322 | continue; | |||
37323 | } | |||
37324 | if (M == SM_SentinelZero) { | |||
37325 | PSHUFBMask.push_back(DAG.getConstant(0x80, DL, MVT::i8)); | |||
37326 | continue; | |||
37327 | } | |||
37328 | M = Ratio * M + i % Ratio; | |||
37329 | assert((M / 16) == (i / 16) && "Lane crossing detected")(static_cast <bool> ((M / 16) == (i / 16) && "Lane crossing detected" ) ? void (0) : __assert_fail ("(M / 16) == (i / 16) && \"Lane crossing detected\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37329, __extension__ __PRETTY_FUNCTION__)); | |||
37330 | PSHUFBMask.push_back(DAG.getConstant(M, DL, MVT::i8)); | |||
37331 | } | |||
37332 | MVT ByteVT = MVT::getVectorVT(MVT::i8, NumBytes); | |||
37333 | Res = CanonicalizeShuffleInput(ByteVT, V1); | |||
37334 | SDValue PSHUFBMaskOp = DAG.getBuildVector(ByteVT, DL, PSHUFBMask); | |||
37335 | Res = DAG.getNode(X86ISD::PSHUFB, DL, ByteVT, Res, PSHUFBMaskOp); | |||
37336 | return DAG.getBitcast(RootVT, Res); | |||
37337 | } | |||
37338 | ||||
37339 | // With XOP, if we have a 128-bit binary input shuffle we can always combine | |||
37340 | // to VPPERM. We match the depth requirement of PSHUFB - VPPERM is never | |||
37341 | // slower than PSHUFB on targets that support both. | |||
37342 | if (AllowVariablePerLaneMask && RootVT.is128BitVector() && | |||
37343 | Subtarget.hasXOP()) { | |||
37344 | // VPPERM Mask Operation | |||
37345 | // Bits[4:0] - Byte Index (0 - 31) | |||
37346 | // Bits[7:5] - Permute Operation (0 - Source byte, 4 - ZERO) | |||
37347 | SmallVector<SDValue, 16> VPPERMMask; | |||
37348 | int NumBytes = 16; | |||
37349 | int Ratio = NumBytes / NumMaskElts; | |||
37350 | for (int i = 0; i < NumBytes; ++i) { | |||
37351 | int M = Mask[i / Ratio]; | |||
37352 | if (M == SM_SentinelUndef) { | |||
37353 | VPPERMMask.push_back(DAG.getUNDEF(MVT::i8)); | |||
37354 | continue; | |||
37355 | } | |||
37356 | if (M == SM_SentinelZero) { | |||
37357 | VPPERMMask.push_back(DAG.getConstant(0x80, DL, MVT::i8)); | |||
37358 | continue; | |||
37359 | } | |||
37360 | M = Ratio * M + i % Ratio; | |||
37361 | VPPERMMask.push_back(DAG.getConstant(M, DL, MVT::i8)); | |||
37362 | } | |||
37363 | MVT ByteVT = MVT::v16i8; | |||
37364 | V1 = CanonicalizeShuffleInput(ByteVT, V1); | |||
37365 | V2 = CanonicalizeShuffleInput(ByteVT, V2); | |||
37366 | SDValue VPPERMMaskOp = DAG.getBuildVector(ByteVT, DL, VPPERMMask); | |||
37367 | Res = DAG.getNode(X86ISD::VPPERM, DL, ByteVT, V1, V2, VPPERMMaskOp); | |||
37368 | return DAG.getBitcast(RootVT, Res); | |||
37369 | } | |||
37370 | ||||
37371 | // If that failed and either input is extracted then try to combine as a | |||
37372 | // shuffle with the larger type. | |||
37373 | if (SDValue WideShuffle = combineX86ShuffleChainWithExtract( | |||
37374 | Inputs, Root, BaseMask, Depth, HasVariableMask, | |||
37375 | AllowVariableCrossLaneMask, AllowVariablePerLaneMask, DAG, Subtarget)) | |||
37376 | return WideShuffle; | |||
37377 | ||||
37378 | // If we have a dual input shuffle then lower to VPERMV3, | |||
37379 | // (non-VLX will pad to 512-bit shuffles) | |||
37380 | if (!UnaryShuffle && AllowVariablePerLaneMask && !MaskContainsZeros && | |||
37381 | ((Subtarget.hasAVX512() && | |||
37382 | (MaskVT == MVT::v2f64 || MaskVT == MVT::v4f64 || MaskVT == MVT::v8f64 || | |||
37383 | MaskVT == MVT::v2i64 || MaskVT == MVT::v4i64 || MaskVT == MVT::v8i64 || | |||
37384 | MaskVT == MVT::v4f32 || MaskVT == MVT::v4i32 || MaskVT == MVT::v8f32 || | |||
37385 | MaskVT == MVT::v8i32 || MaskVT == MVT::v16f32 || | |||
37386 | MaskVT == MVT::v16i32)) || | |||
37387 | (Subtarget.hasBWI() && AllowBWIVPERMV3 && | |||
37388 | (MaskVT == MVT::v8i16 || MaskVT == MVT::v16i16 || | |||
37389 | MaskVT == MVT::v32i16)) || | |||
37390 | (Subtarget.hasVBMI() && AllowBWIVPERMV3 && | |||
37391 | (MaskVT == MVT::v16i8 || MaskVT == MVT::v32i8 || | |||
37392 | MaskVT == MVT::v64i8)))) { | |||
37393 | V1 = CanonicalizeShuffleInput(MaskVT, V1); | |||
37394 | V2 = CanonicalizeShuffleInput(MaskVT, V2); | |||
37395 | Res = lowerShuffleWithPERMV(DL, MaskVT, Mask, V1, V2, Subtarget, DAG); | |||
37396 | return DAG.getBitcast(RootVT, Res); | |||
37397 | } | |||
37398 | ||||
37399 | // Failed to find any combines. | |||
37400 | return SDValue(); | |||
37401 | } | |||
37402 | ||||
37403 | // Combine an arbitrary chain of shuffles + extract_subvectors into a single | |||
37404 | // instruction if possible. | |||
37405 | // | |||
37406 | // Wrapper for combineX86ShuffleChain that extends the shuffle mask to a larger | |||
37407 | // type size to attempt to combine: | |||
37408 | // shuffle(extract_subvector(x,c1),extract_subvector(y,c2),m1) | |||
37409 | // --> | |||
37410 | // extract_subvector(shuffle(x,y,m2),0) | |||
37411 | static SDValue combineX86ShuffleChainWithExtract( | |||
37412 | ArrayRef<SDValue> Inputs, SDValue Root, ArrayRef<int> BaseMask, int Depth, | |||
37413 | bool HasVariableMask, bool AllowVariableCrossLaneMask, | |||
37414 | bool AllowVariablePerLaneMask, SelectionDAG &DAG, | |||
37415 | const X86Subtarget &Subtarget) { | |||
37416 | unsigned NumMaskElts = BaseMask.size(); | |||
37417 | unsigned NumInputs = Inputs.size(); | |||
37418 | if (NumInputs == 0) | |||
37419 | return SDValue(); | |||
37420 | ||||
37421 | EVT RootVT = Root.getValueType(); | |||
37422 | unsigned RootSizeInBits = RootVT.getSizeInBits(); | |||
37423 | assert((RootSizeInBits % NumMaskElts) == 0 && "Unexpected root shuffle mask")(static_cast <bool> ((RootSizeInBits % NumMaskElts) == 0 && "Unexpected root shuffle mask") ? void (0) : __assert_fail ("(RootSizeInBits % NumMaskElts) == 0 && \"Unexpected root shuffle mask\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37423, __extension__ __PRETTY_FUNCTION__)); | |||
37424 | ||||
37425 | SmallVector<SDValue, 4> WideInputs(Inputs.begin(), Inputs.end()); | |||
37426 | SmallVector<unsigned, 4> Offsets(NumInputs, 0); | |||
37427 | ||||
37428 | // Peek through subvectors. | |||
37429 | // TODO: Support inter-mixed EXTRACT_SUBVECTORs + BITCASTs? | |||
37430 | unsigned WideSizeInBits = RootSizeInBits; | |||
37431 | for (unsigned i = 0; i != NumInputs; ++i) { | |||
37432 | SDValue &Src = WideInputs[i]; | |||
37433 | unsigned &Offset = Offsets[i]; | |||
37434 | Src = peekThroughBitcasts(Src); | |||
37435 | EVT BaseVT = Src.getValueType(); | |||
37436 | while (Src.getOpcode() == ISD::EXTRACT_SUBVECTOR) { | |||
37437 | Offset += Src.getConstantOperandVal(1); | |||
37438 | Src = Src.getOperand(0); | |||
37439 | } | |||
37440 | WideSizeInBits = std::max(WideSizeInBits, | |||
37441 | (unsigned)Src.getValueSizeInBits()); | |||
37442 | assert((Offset % BaseVT.getVectorNumElements()) == 0 &&(static_cast <bool> ((Offset % BaseVT.getVectorNumElements ()) == 0 && "Unexpected subvector extraction") ? void (0) : __assert_fail ("(Offset % BaseVT.getVectorNumElements()) == 0 && \"Unexpected subvector extraction\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37443, __extension__ __PRETTY_FUNCTION__)) | |||
37443 | "Unexpected subvector extraction")(static_cast <bool> ((Offset % BaseVT.getVectorNumElements ()) == 0 && "Unexpected subvector extraction") ? void (0) : __assert_fail ("(Offset % BaseVT.getVectorNumElements()) == 0 && \"Unexpected subvector extraction\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37443, __extension__ __PRETTY_FUNCTION__)); | |||
37444 | Offset /= BaseVT.getVectorNumElements(); | |||
37445 | Offset *= NumMaskElts; | |||
37446 | } | |||
37447 | ||||
37448 | // Bail if we're always extracting from the lowest subvectors, | |||
37449 | // combineX86ShuffleChain should match this for the current width. | |||
37450 | if (llvm::all_of(Offsets, [](unsigned Offset) { return Offset == 0; })) | |||
37451 | return SDValue(); | |||
37452 | ||||
37453 | unsigned Scale = WideSizeInBits / RootSizeInBits; | |||
37454 | assert((WideSizeInBits % RootSizeInBits) == 0 &&(static_cast <bool> ((WideSizeInBits % RootSizeInBits) == 0 && "Unexpected subvector extraction") ? void (0) : __assert_fail ("(WideSizeInBits % RootSizeInBits) == 0 && \"Unexpected subvector extraction\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37455, __extension__ __PRETTY_FUNCTION__)) | |||
37455 | "Unexpected subvector extraction")(static_cast <bool> ((WideSizeInBits % RootSizeInBits) == 0 && "Unexpected subvector extraction") ? void (0) : __assert_fail ("(WideSizeInBits % RootSizeInBits) == 0 && \"Unexpected subvector extraction\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37455, __extension__ __PRETTY_FUNCTION__)); | |||
37456 | ||||
37457 | // If the src vector types aren't the same, see if we can extend | |||
37458 | // them to match each other. | |||
37459 | // TODO: Support different scalar types? | |||
37460 | EVT WideSVT = WideInputs[0].getValueType().getScalarType(); | |||
37461 | if (llvm::any_of(WideInputs, [&WideSVT, &DAG](SDValue Op) { | |||
37462 | return !DAG.getTargetLoweringInfo().isTypeLegal(Op.getValueType()) || | |||
37463 | Op.getValueType().getScalarType() != WideSVT; | |||
37464 | })) | |||
37465 | return SDValue(); | |||
37466 | ||||
37467 | for (SDValue &NewInput : WideInputs) { | |||
37468 | assert((WideSizeInBits % NewInput.getValueSizeInBits()) == 0 &&(static_cast <bool> ((WideSizeInBits % NewInput.getValueSizeInBits ()) == 0 && "Shuffle vector size mismatch") ? void (0 ) : __assert_fail ("(WideSizeInBits % NewInput.getValueSizeInBits()) == 0 && \"Shuffle vector size mismatch\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37469, __extension__ __PRETTY_FUNCTION__)) | |||
37469 | "Shuffle vector size mismatch")(static_cast <bool> ((WideSizeInBits % NewInput.getValueSizeInBits ()) == 0 && "Shuffle vector size mismatch") ? void (0 ) : __assert_fail ("(WideSizeInBits % NewInput.getValueSizeInBits()) == 0 && \"Shuffle vector size mismatch\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37469, __extension__ __PRETTY_FUNCTION__)); | |||
37470 | if (WideSizeInBits > NewInput.getValueSizeInBits()) | |||
37471 | NewInput = widenSubVector(NewInput, false, Subtarget, DAG, | |||
37472 | SDLoc(NewInput), WideSizeInBits); | |||
37473 | assert(WideSizeInBits == NewInput.getValueSizeInBits() &&(static_cast <bool> (WideSizeInBits == NewInput.getValueSizeInBits () && "Unexpected subvector extraction") ? void (0) : __assert_fail ("WideSizeInBits == NewInput.getValueSizeInBits() && \"Unexpected subvector extraction\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37474, __extension__ __PRETTY_FUNCTION__)) | |||
37474 | "Unexpected subvector extraction")(static_cast <bool> (WideSizeInBits == NewInput.getValueSizeInBits () && "Unexpected subvector extraction") ? void (0) : __assert_fail ("WideSizeInBits == NewInput.getValueSizeInBits() && \"Unexpected subvector extraction\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37474, __extension__ __PRETTY_FUNCTION__)); | |||
37475 | } | |||
37476 | ||||
37477 | // Create new mask for larger type. | |||
37478 | for (unsigned i = 1; i != NumInputs; ++i) | |||
37479 | Offsets[i] += i * Scale * NumMaskElts; | |||
37480 | ||||
37481 | SmallVector<int, 64> WideMask(BaseMask.begin(), BaseMask.end()); | |||
37482 | for (int &M : WideMask) { | |||
37483 | if (M < 0) | |||
37484 | continue; | |||
37485 | M = (M % NumMaskElts) + Offsets[M / NumMaskElts]; | |||
37486 | } | |||
37487 | WideMask.append((Scale - 1) * NumMaskElts, SM_SentinelUndef); | |||
37488 | ||||
37489 | // Remove unused/repeated shuffle source ops. | |||
37490 | resolveTargetShuffleInputsAndMask(WideInputs, WideMask); | |||
37491 | assert(!WideInputs.empty() && "Shuffle with no inputs detected")(static_cast <bool> (!WideInputs.empty() && "Shuffle with no inputs detected" ) ? void (0) : __assert_fail ("!WideInputs.empty() && \"Shuffle with no inputs detected\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37491, __extension__ __PRETTY_FUNCTION__)); | |||
37492 | ||||
37493 | if (WideInputs.size() > 2) | |||
37494 | return SDValue(); | |||
37495 | ||||
37496 | // Increase depth for every upper subvector we've peeked through. | |||
37497 | Depth += count_if(Offsets, [](unsigned Offset) { return Offset > 0; }); | |||
37498 | ||||
37499 | // Attempt to combine wider chain. | |||
37500 | // TODO: Can we use a better Root? | |||
37501 | SDValue WideRoot = WideInputs[0]; | |||
37502 | if (SDValue WideShuffle = | |||
37503 | combineX86ShuffleChain(WideInputs, WideRoot, WideMask, Depth, | |||
37504 | HasVariableMask, AllowVariableCrossLaneMask, | |||
37505 | AllowVariablePerLaneMask, DAG, Subtarget)) { | |||
37506 | WideShuffle = | |||
37507 | extractSubVector(WideShuffle, 0, DAG, SDLoc(Root), RootSizeInBits); | |||
37508 | return DAG.getBitcast(RootVT, WideShuffle); | |||
37509 | } | |||
37510 | return SDValue(); | |||
37511 | } | |||
37512 | ||||
37513 | // Canonicalize the combined shuffle mask chain with horizontal ops. | |||
37514 | // NOTE: This may update the Ops and Mask. | |||
37515 | static SDValue canonicalizeShuffleMaskWithHorizOp( | |||
37516 | MutableArrayRef<SDValue> Ops, MutableArrayRef<int> Mask, | |||
37517 | unsigned RootSizeInBits, const SDLoc &DL, SelectionDAG &DAG, | |||
37518 | const X86Subtarget &Subtarget) { | |||
37519 | if (Mask.empty() || Ops.empty()) | |||
37520 | return SDValue(); | |||
37521 | ||||
37522 | SmallVector<SDValue> BC; | |||
37523 | for (SDValue Op : Ops) | |||
37524 | BC.push_back(peekThroughBitcasts(Op)); | |||
37525 | ||||
37526 | // All ops must be the same horizop + type. | |||
37527 | SDValue BC0 = BC[0]; | |||
37528 | EVT VT0 = BC0.getValueType(); | |||
37529 | unsigned Opcode0 = BC0.getOpcode(); | |||
37530 | if (VT0.getSizeInBits() != RootSizeInBits || llvm::any_of(BC, [&](SDValue V) { | |||
37531 | return V.getOpcode() != Opcode0 || V.getValueType() != VT0; | |||
37532 | })) | |||
37533 | return SDValue(); | |||
37534 | ||||
37535 | bool isHoriz = (Opcode0 == X86ISD::FHADD || Opcode0 == X86ISD::HADD || | |||
37536 | Opcode0 == X86ISD::FHSUB || Opcode0 == X86ISD::HSUB); | |||
37537 | bool isPack = (Opcode0 == X86ISD::PACKSS || Opcode0 == X86ISD::PACKUS); | |||
37538 | if (!isHoriz && !isPack) | |||
37539 | return SDValue(); | |||
37540 | ||||
37541 | // Do all ops have a single use? | |||
37542 | bool OneUseOps = llvm::all_of(Ops, [](SDValue Op) { | |||
37543 | return Op.hasOneUse() && | |||
37544 | peekThroughBitcasts(Op) == peekThroughOneUseBitcasts(Op); | |||
37545 | }); | |||
37546 | ||||
37547 | int NumElts = VT0.getVectorNumElements(); | |||
37548 | int NumLanes = VT0.getSizeInBits() / 128; | |||
37549 | int NumEltsPerLane = NumElts / NumLanes; | |||
37550 | int NumHalfEltsPerLane = NumEltsPerLane / 2; | |||
37551 | MVT SrcVT = BC0.getOperand(0).getSimpleValueType(); | |||
37552 | unsigned EltSizeInBits = RootSizeInBits / Mask.size(); | |||
37553 | ||||
37554 | if (NumEltsPerLane >= 4 && | |||
37555 | (isPack || shouldUseHorizontalOp(Ops.size() == 1, DAG, Subtarget))) { | |||
37556 | SmallVector<int> LaneMask, ScaledMask; | |||
37557 | if (isRepeatedTargetShuffleMask(128, EltSizeInBits, Mask, LaneMask) && | |||
37558 | scaleShuffleElements(LaneMask, 4, ScaledMask)) { | |||
37559 | // See if we can remove the shuffle by resorting the HOP chain so that | |||
37560 | // the HOP args are pre-shuffled. | |||
37561 | // TODO: Generalize to any sized/depth chain. | |||
37562 | // TODO: Add support for PACKSS/PACKUS. | |||
37563 | if (isHoriz) { | |||
37564 | // Attempt to find a HOP(HOP(X,Y),HOP(Z,W)) source operand. | |||
37565 | auto GetHOpSrc = [&](int M) { | |||
37566 | if (M == SM_SentinelUndef) | |||
37567 | return DAG.getUNDEF(VT0); | |||
37568 | if (M == SM_SentinelZero) | |||
37569 | return getZeroVector(VT0.getSimpleVT(), Subtarget, DAG, DL); | |||
37570 | SDValue Src0 = BC[M / 4]; | |||
37571 | SDValue Src1 = Src0.getOperand((M % 4) >= 2); | |||
37572 | if (Src1.getOpcode() == Opcode0 && Src0->isOnlyUserOf(Src1.getNode())) | |||
37573 | return Src1.getOperand(M % 2); | |||
37574 | return SDValue(); | |||
37575 | }; | |||
37576 | SDValue M0 = GetHOpSrc(ScaledMask[0]); | |||
37577 | SDValue M1 = GetHOpSrc(ScaledMask[1]); | |||
37578 | SDValue M2 = GetHOpSrc(ScaledMask[2]); | |||
37579 | SDValue M3 = GetHOpSrc(ScaledMask[3]); | |||
37580 | if (M0 && M1 && M2 && M3) { | |||
37581 | SDValue LHS = DAG.getNode(Opcode0, DL, SrcVT, M0, M1); | |||
37582 | SDValue RHS = DAG.getNode(Opcode0, DL, SrcVT, M2, M3); | |||
37583 | return DAG.getNode(Opcode0, DL, VT0, LHS, RHS); | |||
37584 | } | |||
37585 | } | |||
37586 | // shuffle(hop(x,y),hop(z,w)) -> permute(hop(x,z)) etc. | |||
37587 | if (Ops.size() >= 2) { | |||
37588 | SDValue LHS, RHS; | |||
37589 | auto GetHOpSrc = [&](int M, int &OutM) { | |||
37590 | // TODO: Support SM_SentinelZero | |||
37591 | if (M < 0) | |||
37592 | return M == SM_SentinelUndef; | |||
37593 | SDValue Src = BC[M / 4].getOperand((M % 4) >= 2); | |||
37594 | if (!LHS || LHS == Src) { | |||
37595 | LHS = Src; | |||
37596 | OutM = (M % 2); | |||
37597 | return true; | |||
37598 | } | |||
37599 | if (!RHS || RHS == Src) { | |||
37600 | RHS = Src; | |||
37601 | OutM = (M % 2) + 2; | |||
37602 | return true; | |||
37603 | } | |||
37604 | return false; | |||
37605 | }; | |||
37606 | int PostMask[4] = {-1, -1, -1, -1}; | |||
37607 | if (GetHOpSrc(ScaledMask[0], PostMask[0]) && | |||
37608 | GetHOpSrc(ScaledMask[1], PostMask[1]) && | |||
37609 | GetHOpSrc(ScaledMask[2], PostMask[2]) && | |||
37610 | GetHOpSrc(ScaledMask[3], PostMask[3])) { | |||
37611 | LHS = DAG.getBitcast(SrcVT, LHS); | |||
37612 | RHS = DAG.getBitcast(SrcVT, RHS ? RHS : LHS); | |||
37613 | SDValue Res = DAG.getNode(Opcode0, DL, VT0, LHS, RHS); | |||
37614 | // Use SHUFPS for the permute so this will work on SSE3 targets, | |||
37615 | // shuffle combining and domain handling will simplify this later on. | |||
37616 | MVT ShuffleVT = MVT::getVectorVT(MVT::f32, RootSizeInBits / 32); | |||
37617 | Res = DAG.getBitcast(ShuffleVT, Res); | |||
37618 | return DAG.getNode(X86ISD::SHUFP, DL, ShuffleVT, Res, Res, | |||
37619 | getV4X86ShuffleImm8ForMask(PostMask, DL, DAG)); | |||
37620 | } | |||
37621 | } | |||
37622 | } | |||
37623 | } | |||
37624 | ||||
37625 | if (2 < Ops.size()) | |||
37626 | return SDValue(); | |||
37627 | ||||
37628 | SDValue BC1 = BC[BC.size() - 1]; | |||
37629 | if (Mask.size() == VT0.getVectorNumElements()) { | |||
37630 | // Canonicalize binary shuffles of horizontal ops that use the | |||
37631 | // same sources to an unary shuffle. | |||
37632 | // TODO: Try to perform this fold even if the shuffle remains. | |||
37633 | if (Ops.size() == 2) { | |||
37634 | auto ContainsOps = [](SDValue HOp, SDValue Op) { | |||
37635 | return Op == HOp.getOperand(0) || Op == HOp.getOperand(1); | |||
37636 | }; | |||
37637 | // Commute if all BC0's ops are contained in BC1. | |||
37638 | if (ContainsOps(BC1, BC0.getOperand(0)) && | |||
37639 | ContainsOps(BC1, BC0.getOperand(1))) { | |||
37640 | ShuffleVectorSDNode::commuteMask(Mask); | |||
37641 | std::swap(Ops[0], Ops[1]); | |||
37642 | std::swap(BC0, BC1); | |||
37643 | } | |||
37644 | ||||
37645 | // If BC1 can be represented by BC0, then convert to unary shuffle. | |||
37646 | if (ContainsOps(BC0, BC1.getOperand(0)) && | |||
37647 | ContainsOps(BC0, BC1.getOperand(1))) { | |||
37648 | for (int &M : Mask) { | |||
37649 | if (M < NumElts) // BC0 element or UNDEF/Zero sentinel. | |||
37650 | continue; | |||
37651 | int SubLane = ((M % NumEltsPerLane) >= NumHalfEltsPerLane) ? 1 : 0; | |||
37652 | M -= NumElts + (SubLane * NumHalfEltsPerLane); | |||
37653 | if (BC1.getOperand(SubLane) != BC0.getOperand(0)) | |||
37654 | M += NumHalfEltsPerLane; | |||
37655 | } | |||
37656 | } | |||
37657 | } | |||
37658 | ||||
37659 | // Canonicalize unary horizontal ops to only refer to lower halves. | |||
37660 | for (int i = 0; i != NumElts; ++i) { | |||
37661 | int &M = Mask[i]; | |||
37662 | if (isUndefOrZero(M)) | |||
37663 | continue; | |||
37664 | if (M < NumElts && BC0.getOperand(0) == BC0.getOperand(1) && | |||
37665 | (M % NumEltsPerLane) >= NumHalfEltsPerLane) | |||
37666 | M -= NumHalfEltsPerLane; | |||
37667 | if (NumElts <= M && BC1.getOperand(0) == BC1.getOperand(1) && | |||
37668 | (M % NumEltsPerLane) >= NumHalfEltsPerLane) | |||
37669 | M -= NumHalfEltsPerLane; | |||
37670 | } | |||
37671 | } | |||
37672 | ||||
37673 | // Combine binary shuffle of 2 similar 'Horizontal' instructions into a | |||
37674 | // single instruction. Attempt to match a v2X64 repeating shuffle pattern that | |||
37675 | // represents the LHS/RHS inputs for the lower/upper halves. | |||
37676 | SmallVector<int, 16> TargetMask128, WideMask128; | |||
37677 | if (isRepeatedTargetShuffleMask(128, EltSizeInBits, Mask, TargetMask128) && | |||
37678 | scaleShuffleElements(TargetMask128, 2, WideMask128)) { | |||
37679 | assert(isUndefOrZeroOrInRange(WideMask128, 0, 4) && "Illegal shuffle")(static_cast <bool> (isUndefOrZeroOrInRange(WideMask128 , 0, 4) && "Illegal shuffle") ? void (0) : __assert_fail ("isUndefOrZeroOrInRange(WideMask128, 0, 4) && \"Illegal shuffle\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37679, __extension__ __PRETTY_FUNCTION__)); | |||
37680 | bool SingleOp = (Ops.size() == 1); | |||
37681 | if (isPack || OneUseOps || | |||
37682 | shouldUseHorizontalOp(SingleOp, DAG, Subtarget)) { | |||
37683 | SDValue Lo = isInRange(WideMask128[0], 0, 2) ? BC0 : BC1; | |||
37684 | SDValue Hi = isInRange(WideMask128[1], 0, 2) ? BC0 : BC1; | |||
37685 | Lo = Lo.getOperand(WideMask128[0] & 1); | |||
37686 | Hi = Hi.getOperand(WideMask128[1] & 1); | |||
37687 | if (SingleOp) { | |||
37688 | SDValue Undef = DAG.getUNDEF(SrcVT); | |||
37689 | SDValue Zero = getZeroVector(SrcVT, Subtarget, DAG, DL); | |||
37690 | Lo = (WideMask128[0] == SM_SentinelZero ? Zero : Lo); | |||
37691 | Hi = (WideMask128[1] == SM_SentinelZero ? Zero : Hi); | |||
37692 | Lo = (WideMask128[0] == SM_SentinelUndef ? Undef : Lo); | |||
37693 | Hi = (WideMask128[1] == SM_SentinelUndef ? Undef : Hi); | |||
37694 | } | |||
37695 | return DAG.getNode(Opcode0, DL, VT0, Lo, Hi); | |||
37696 | } | |||
37697 | } | |||
37698 | ||||
37699 | return SDValue(); | |||
37700 | } | |||
37701 | ||||
37702 | // Attempt to constant fold all of the constant source ops. | |||
37703 | // Returns true if the entire shuffle is folded to a constant. | |||
37704 | // TODO: Extend this to merge multiple constant Ops and update the mask. | |||
37705 | static SDValue combineX86ShufflesConstants(ArrayRef<SDValue> Ops, | |||
37706 | ArrayRef<int> Mask, SDValue Root, | |||
37707 | bool HasVariableMask, | |||
37708 | SelectionDAG &DAG, | |||
37709 | const X86Subtarget &Subtarget) { | |||
37710 | MVT VT = Root.getSimpleValueType(); | |||
37711 | ||||
37712 | unsigned SizeInBits = VT.getSizeInBits(); | |||
37713 | unsigned NumMaskElts = Mask.size(); | |||
37714 | unsigned MaskSizeInBits = SizeInBits / NumMaskElts; | |||
37715 | unsigned NumOps = Ops.size(); | |||
37716 | ||||
37717 | // Extract constant bits from each source op. | |||
37718 | bool OneUseConstantOp = false; | |||
37719 | SmallVector<APInt, 16> UndefEltsOps(NumOps); | |||
37720 | SmallVector<SmallVector<APInt, 16>, 16> RawBitsOps(NumOps); | |||
37721 | for (unsigned i = 0; i != NumOps; ++i) { | |||
37722 | SDValue SrcOp = Ops[i]; | |||
37723 | OneUseConstantOp |= SrcOp.hasOneUse(); | |||
37724 | if (!getTargetConstantBitsFromNode(SrcOp, MaskSizeInBits, UndefEltsOps[i], | |||
37725 | RawBitsOps[i])) | |||
37726 | return SDValue(); | |||
37727 | } | |||
37728 | ||||
37729 | // Only fold if at least one of the constants is only used once or | |||
37730 | // the combined shuffle has included a variable mask shuffle, this | |||
37731 | // is to avoid constant pool bloat. | |||
37732 | if (!OneUseConstantOp && !HasVariableMask) | |||
37733 | return SDValue(); | |||
37734 | ||||
37735 | // Shuffle the constant bits according to the mask. | |||
37736 | SDLoc DL(Root); | |||
37737 | APInt UndefElts(NumMaskElts, 0); | |||
37738 | APInt ZeroElts(NumMaskElts, 0); | |||
37739 | APInt ConstantElts(NumMaskElts, 0); | |||
37740 | SmallVector<APInt, 8> ConstantBitData(NumMaskElts, | |||
37741 | APInt::getZero(MaskSizeInBits)); | |||
37742 | for (unsigned i = 0; i != NumMaskElts; ++i) { | |||
37743 | int M = Mask[i]; | |||
37744 | if (M == SM_SentinelUndef) { | |||
37745 | UndefElts.setBit(i); | |||
37746 | continue; | |||
37747 | } else if (M == SM_SentinelZero) { | |||
37748 | ZeroElts.setBit(i); | |||
37749 | continue; | |||
37750 | } | |||
37751 | assert(0 <= M && M < (int)(NumMaskElts * NumOps))(static_cast <bool> (0 <= M && M < (int)( NumMaskElts * NumOps)) ? void (0) : __assert_fail ("0 <= M && M < (int)(NumMaskElts * NumOps)" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37751, __extension__ __PRETTY_FUNCTION__)); | |||
37752 | ||||
37753 | unsigned SrcOpIdx = (unsigned)M / NumMaskElts; | |||
37754 | unsigned SrcMaskIdx = (unsigned)M % NumMaskElts; | |||
37755 | ||||
37756 | auto &SrcUndefElts = UndefEltsOps[SrcOpIdx]; | |||
37757 | if (SrcUndefElts[SrcMaskIdx]) { | |||
37758 | UndefElts.setBit(i); | |||
37759 | continue; | |||
37760 | } | |||
37761 | ||||
37762 | auto &SrcEltBits = RawBitsOps[SrcOpIdx]; | |||
37763 | APInt &Bits = SrcEltBits[SrcMaskIdx]; | |||
37764 | if (!Bits) { | |||
37765 | ZeroElts.setBit(i); | |||
37766 | continue; | |||
37767 | } | |||
37768 | ||||
37769 | ConstantElts.setBit(i); | |||
37770 | ConstantBitData[i] = Bits; | |||
37771 | } | |||
37772 | assert((UndefElts | ZeroElts | ConstantElts).isAllOnes())(static_cast <bool> ((UndefElts | ZeroElts | ConstantElts ).isAllOnes()) ? void (0) : __assert_fail ("(UndefElts | ZeroElts | ConstantElts).isAllOnes()" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37772, __extension__ __PRETTY_FUNCTION__)); | |||
37773 | ||||
37774 | // Attempt to create a zero vector. | |||
37775 | if ((UndefElts | ZeroElts).isAllOnes()) | |||
37776 | return getZeroVector(Root.getSimpleValueType(), Subtarget, DAG, DL); | |||
37777 | ||||
37778 | // Create the constant data. | |||
37779 | MVT MaskSVT; | |||
37780 | if (VT.isFloatingPoint() && (MaskSizeInBits == 32 || MaskSizeInBits == 64)) | |||
37781 | MaskSVT = MVT::getFloatingPointVT(MaskSizeInBits); | |||
37782 | else | |||
37783 | MaskSVT = MVT::getIntegerVT(MaskSizeInBits); | |||
37784 | ||||
37785 | MVT MaskVT = MVT::getVectorVT(MaskSVT, NumMaskElts); | |||
37786 | if (!DAG.getTargetLoweringInfo().isTypeLegal(MaskVT)) | |||
37787 | return SDValue(); | |||
37788 | ||||
37789 | SDValue CstOp = getConstVector(ConstantBitData, UndefElts, MaskVT, DAG, DL); | |||
37790 | return DAG.getBitcast(VT, CstOp); | |||
37791 | } | |||
37792 | ||||
37793 | namespace llvm { | |||
37794 | namespace X86 { | |||
37795 | enum { | |||
37796 | MaxShuffleCombineDepth = 8 | |||
37797 | }; | |||
37798 | } | |||
37799 | } // namespace llvm | |||
37800 | ||||
37801 | /// Fully generic combining of x86 shuffle instructions. | |||
37802 | /// | |||
37803 | /// This should be the last combine run over the x86 shuffle instructions. Once | |||
37804 | /// they have been fully optimized, this will recursively consider all chains | |||
37805 | /// of single-use shuffle instructions, build a generic model of the cumulative | |||
37806 | /// shuffle operation, and check for simpler instructions which implement this | |||
37807 | /// operation. We use this primarily for two purposes: | |||
37808 | /// | |||
37809 | /// 1) Collapse generic shuffles to specialized single instructions when | |||
37810 | /// equivalent. In most cases, this is just an encoding size win, but | |||
37811 | /// sometimes we will collapse multiple generic shuffles into a single | |||
37812 | /// special-purpose shuffle. | |||
37813 | /// 2) Look for sequences of shuffle instructions with 3 or more total | |||
37814 | /// instructions, and replace them with the slightly more expensive SSSE3 | |||
37815 | /// PSHUFB instruction if available. We do this as the last combining step | |||
37816 | /// to ensure we avoid using PSHUFB if we can implement the shuffle with | |||
37817 | /// a suitable short sequence of other instructions. The PSHUFB will either | |||
37818 | /// use a register or have to read from memory and so is slightly (but only | |||
37819 | /// slightly) more expensive than the other shuffle instructions. | |||
37820 | /// | |||
37821 | /// Because this is inherently a quadratic operation (for each shuffle in | |||
37822 | /// a chain, we recurse up the chain), the depth is limited to 8 instructions. | |||
37823 | /// This should never be an issue in practice as the shuffle lowering doesn't | |||
37824 | /// produce sequences of more than 8 instructions. | |||
37825 | /// | |||
37826 | /// FIXME: We will currently miss some cases where the redundant shuffling | |||
37827 | /// would simplify under the threshold for PSHUFB formation because of | |||
37828 | /// combine-ordering. To fix this, we should do the redundant instruction | |||
37829 | /// combining in this recursive walk. | |||
37830 | static SDValue combineX86ShufflesRecursively( | |||
37831 | ArrayRef<SDValue> SrcOps, int SrcOpIndex, SDValue Root, | |||
37832 | ArrayRef<int> RootMask, ArrayRef<const SDNode *> SrcNodes, unsigned Depth, | |||
37833 | unsigned MaxDepth, bool HasVariableMask, bool AllowVariableCrossLaneMask, | |||
37834 | bool AllowVariablePerLaneMask, SelectionDAG &DAG, | |||
37835 | const X86Subtarget &Subtarget) { | |||
37836 | assert(RootMask.size() > 0 &&(static_cast <bool> (RootMask.size() > 0 && ( RootMask.size() > 1 || (RootMask[0] == 0 && SrcOpIndex == 0)) && "Illegal shuffle root mask") ? void (0) : __assert_fail ("RootMask.size() > 0 && (RootMask.size() > 1 || (RootMask[0] == 0 && SrcOpIndex == 0)) && \"Illegal shuffle root mask\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37838, __extension__ __PRETTY_FUNCTION__)) | |||
37837 | (RootMask.size() > 1 || (RootMask[0] == 0 && SrcOpIndex == 0)) &&(static_cast <bool> (RootMask.size() > 0 && ( RootMask.size() > 1 || (RootMask[0] == 0 && SrcOpIndex == 0)) && "Illegal shuffle root mask") ? void (0) : __assert_fail ("RootMask.size() > 0 && (RootMask.size() > 1 || (RootMask[0] == 0 && SrcOpIndex == 0)) && \"Illegal shuffle root mask\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37838, __extension__ __PRETTY_FUNCTION__)) | |||
37838 | "Illegal shuffle root mask")(static_cast <bool> (RootMask.size() > 0 && ( RootMask.size() > 1 || (RootMask[0] == 0 && SrcOpIndex == 0)) && "Illegal shuffle root mask") ? void (0) : __assert_fail ("RootMask.size() > 0 && (RootMask.size() > 1 || (RootMask[0] == 0 && SrcOpIndex == 0)) && \"Illegal shuffle root mask\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37838, __extension__ __PRETTY_FUNCTION__)); | |||
37839 | assert(Root.getSimpleValueType().isVector() &&(static_cast <bool> (Root.getSimpleValueType().isVector () && "Shuffles operate on vector types!") ? void (0) : __assert_fail ("Root.getSimpleValueType().isVector() && \"Shuffles operate on vector types!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37840, __extension__ __PRETTY_FUNCTION__)) | |||
37840 | "Shuffles operate on vector types!")(static_cast <bool> (Root.getSimpleValueType().isVector () && "Shuffles operate on vector types!") ? void (0) : __assert_fail ("Root.getSimpleValueType().isVector() && \"Shuffles operate on vector types!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37840, __extension__ __PRETTY_FUNCTION__)); | |||
37841 | unsigned RootSizeInBits = Root.getSimpleValueType().getSizeInBits(); | |||
37842 | ||||
37843 | // Bound the depth of our recursive combine because this is ultimately | |||
37844 | // quadratic in nature. | |||
37845 | if (Depth >= MaxDepth) | |||
37846 | return SDValue(); | |||
37847 | ||||
37848 | // Directly rip through bitcasts to find the underlying operand. | |||
37849 | SDValue Op = SrcOps[SrcOpIndex]; | |||
37850 | Op = peekThroughOneUseBitcasts(Op); | |||
37851 | ||||
37852 | EVT VT = Op.getValueType(); | |||
37853 | if (!VT.isVector() || !VT.isSimple()) | |||
37854 | return SDValue(); // Bail if we hit a non-simple non-vector. | |||
37855 | ||||
37856 | // FIXME: Just bail on f16 for now. | |||
37857 | if (VT.getVectorElementType() == MVT::f16) | |||
37858 | return SDValue(); | |||
37859 | ||||
37860 | assert((RootSizeInBits % VT.getSizeInBits()) == 0 &&(static_cast <bool> ((RootSizeInBits % VT.getSizeInBits ()) == 0 && "Can only combine shuffles upto size of the root op." ) ? void (0) : __assert_fail ("(RootSizeInBits % VT.getSizeInBits()) == 0 && \"Can only combine shuffles upto size of the root op.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37861, __extension__ __PRETTY_FUNCTION__)) | |||
37861 | "Can only combine shuffles upto size of the root op.")(static_cast <bool> ((RootSizeInBits % VT.getSizeInBits ()) == 0 && "Can only combine shuffles upto size of the root op." ) ? void (0) : __assert_fail ("(RootSizeInBits % VT.getSizeInBits()) == 0 && \"Can only combine shuffles upto size of the root op.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37861, __extension__ __PRETTY_FUNCTION__)); | |||
37862 | ||||
37863 | // Extract target shuffle mask and resolve sentinels and inputs. | |||
37864 | // TODO - determine Op's demanded elts from RootMask. | |||
37865 | SmallVector<int, 64> OpMask; | |||
37866 | SmallVector<SDValue, 2> OpInputs; | |||
37867 | APInt OpUndef, OpZero; | |||
37868 | APInt OpDemandedElts = APInt::getAllOnes(VT.getVectorNumElements()); | |||
37869 | bool IsOpVariableMask = isTargetShuffleVariableMask(Op.getOpcode()); | |||
37870 | if (!getTargetShuffleInputs(Op, OpDemandedElts, OpInputs, OpMask, OpUndef, | |||
37871 | OpZero, DAG, Depth, false)) | |||
37872 | return SDValue(); | |||
37873 | ||||
37874 | // Shuffle inputs must not be larger than the shuffle result. | |||
37875 | // TODO: Relax this for single input faux shuffles (trunc/extract_subvector). | |||
37876 | if (llvm::any_of(OpInputs, [VT](SDValue OpInput) { | |||
37877 | return OpInput.getValueSizeInBits() > VT.getSizeInBits(); | |||
37878 | })) | |||
37879 | return SDValue(); | |||
37880 | ||||
37881 | // If the shuffle result was smaller than the root, we need to adjust the | |||
37882 | // mask indices and pad the mask with undefs. | |||
37883 | if (RootSizeInBits > VT.getSizeInBits()) { | |||
37884 | unsigned NumSubVecs = RootSizeInBits / VT.getSizeInBits(); | |||
37885 | unsigned OpMaskSize = OpMask.size(); | |||
37886 | if (OpInputs.size() > 1) { | |||
37887 | unsigned PaddedMaskSize = NumSubVecs * OpMaskSize; | |||
37888 | for (int &M : OpMask) { | |||
37889 | if (M < 0) | |||
37890 | continue; | |||
37891 | int EltIdx = M % OpMaskSize; | |||
37892 | int OpIdx = M / OpMaskSize; | |||
37893 | M = (PaddedMaskSize * OpIdx) + EltIdx; | |||
37894 | } | |||
37895 | } | |||
37896 | OpZero = OpZero.zext(NumSubVecs * OpMaskSize); | |||
37897 | OpUndef = OpUndef.zext(NumSubVecs * OpMaskSize); | |||
37898 | OpMask.append((NumSubVecs - 1) * OpMaskSize, SM_SentinelUndef); | |||
37899 | } | |||
37900 | ||||
37901 | SmallVector<int, 64> Mask; | |||
37902 | SmallVector<SDValue, 16> Ops; | |||
37903 | ||||
37904 | // We don't need to merge masks if the root is empty. | |||
37905 | bool EmptyRoot = (Depth == 0) && (RootMask.size() == 1); | |||
37906 | if (EmptyRoot) { | |||
37907 | // Only resolve zeros if it will remove an input, otherwise we might end | |||
37908 | // up in an infinite loop. | |||
37909 | bool ResolveKnownZeros = true; | |||
37910 | if (!OpZero.isZero()) { | |||
37911 | APInt UsedInputs = APInt::getZero(OpInputs.size()); | |||
37912 | for (int i = 0, e = OpMask.size(); i != e; ++i) { | |||
37913 | int M = OpMask[i]; | |||
37914 | if (OpUndef[i] || OpZero[i] || isUndefOrZero(M)) | |||
37915 | continue; | |||
37916 | UsedInputs.setBit(M / OpMask.size()); | |||
37917 | if (UsedInputs.isAllOnes()) { | |||
37918 | ResolveKnownZeros = false; | |||
37919 | break; | |||
37920 | } | |||
37921 | } | |||
37922 | } | |||
37923 | resolveTargetShuffleFromZeroables(OpMask, OpUndef, OpZero, | |||
37924 | ResolveKnownZeros); | |||
37925 | ||||
37926 | Mask = OpMask; | |||
37927 | Ops.append(OpInputs.begin(), OpInputs.end()); | |||
37928 | } else { | |||
37929 | resolveTargetShuffleFromZeroables(OpMask, OpUndef, OpZero); | |||
37930 | ||||
37931 | // Add the inputs to the Ops list, avoiding duplicates. | |||
37932 | Ops.append(SrcOps.begin(), SrcOps.end()); | |||
37933 | ||||
37934 | auto AddOp = [&Ops](SDValue Input, int InsertionPoint) -> int { | |||
37935 | // Attempt to find an existing match. | |||
37936 | SDValue InputBC = peekThroughBitcasts(Input); | |||
37937 | for (int i = 0, e = Ops.size(); i < e; ++i) | |||
37938 | if (InputBC == peekThroughBitcasts(Ops[i])) | |||
37939 | return i; | |||
37940 | // Match failed - should we replace an existing Op? | |||
37941 | if (InsertionPoint >= 0) { | |||
37942 | Ops[InsertionPoint] = Input; | |||
37943 | return InsertionPoint; | |||
37944 | } | |||
37945 | // Add to the end of the Ops list. | |||
37946 | Ops.push_back(Input); | |||
37947 | return Ops.size() - 1; | |||
37948 | }; | |||
37949 | ||||
37950 | SmallVector<int, 2> OpInputIdx; | |||
37951 | for (SDValue OpInput : OpInputs) | |||
37952 | OpInputIdx.push_back( | |||
37953 | AddOp(OpInput, OpInputIdx.empty() ? SrcOpIndex : -1)); | |||
37954 | ||||
37955 | assert(((RootMask.size() > OpMask.size() &&(static_cast <bool> (((RootMask.size() > OpMask.size () && RootMask.size() % OpMask.size() == 0) || (OpMask .size() > RootMask.size() && OpMask.size() % RootMask .size() == 0) || OpMask.size() == RootMask.size()) && "The smaller number of elements must divide the larger.") ? void (0) : __assert_fail ("((RootMask.size() > OpMask.size() && RootMask.size() % OpMask.size() == 0) || (OpMask.size() > RootMask.size() && OpMask.size() % RootMask.size() == 0) || OpMask.size() == RootMask.size()) && \"The smaller number of elements must divide the larger.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37960, __extension__ __PRETTY_FUNCTION__)) | |||
37956 | RootMask.size() % OpMask.size() == 0) ||(static_cast <bool> (((RootMask.size() > OpMask.size () && RootMask.size() % OpMask.size() == 0) || (OpMask .size() > RootMask.size() && OpMask.size() % RootMask .size() == 0) || OpMask.size() == RootMask.size()) && "The smaller number of elements must divide the larger.") ? void (0) : __assert_fail ("((RootMask.size() > OpMask.size() && RootMask.size() % OpMask.size() == 0) || (OpMask.size() > RootMask.size() && OpMask.size() % RootMask.size() == 0) || OpMask.size() == RootMask.size()) && \"The smaller number of elements must divide the larger.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37960, __extension__ __PRETTY_FUNCTION__)) | |||
37957 | (OpMask.size() > RootMask.size() &&(static_cast <bool> (((RootMask.size() > OpMask.size () && RootMask.size() % OpMask.size() == 0) || (OpMask .size() > RootMask.size() && OpMask.size() % RootMask .size() == 0) || OpMask.size() == RootMask.size()) && "The smaller number of elements must divide the larger.") ? void (0) : __assert_fail ("((RootMask.size() > OpMask.size() && RootMask.size() % OpMask.size() == 0) || (OpMask.size() > RootMask.size() && OpMask.size() % RootMask.size() == 0) || OpMask.size() == RootMask.size()) && \"The smaller number of elements must divide the larger.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37960, __extension__ __PRETTY_FUNCTION__)) | |||
37958 | OpMask.size() % RootMask.size() == 0) ||(static_cast <bool> (((RootMask.size() > OpMask.size () && RootMask.size() % OpMask.size() == 0) || (OpMask .size() > RootMask.size() && OpMask.size() % RootMask .size() == 0) || OpMask.size() == RootMask.size()) && "The smaller number of elements must divide the larger.") ? void (0) : __assert_fail ("((RootMask.size() > OpMask.size() && RootMask.size() % OpMask.size() == 0) || (OpMask.size() > RootMask.size() && OpMask.size() % RootMask.size() == 0) || OpMask.size() == RootMask.size()) && \"The smaller number of elements must divide the larger.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37960, __extension__ __PRETTY_FUNCTION__)) | |||
37959 | OpMask.size() == RootMask.size()) &&(static_cast <bool> (((RootMask.size() > OpMask.size () && RootMask.size() % OpMask.size() == 0) || (OpMask .size() > RootMask.size() && OpMask.size() % RootMask .size() == 0) || OpMask.size() == RootMask.size()) && "The smaller number of elements must divide the larger.") ? void (0) : __assert_fail ("((RootMask.size() > OpMask.size() && RootMask.size() % OpMask.size() == 0) || (OpMask.size() > RootMask.size() && OpMask.size() % RootMask.size() == 0) || OpMask.size() == RootMask.size()) && \"The smaller number of elements must divide the larger.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37960, __extension__ __PRETTY_FUNCTION__)) | |||
37960 | "The smaller number of elements must divide the larger.")(static_cast <bool> (((RootMask.size() > OpMask.size () && RootMask.size() % OpMask.size() == 0) || (OpMask .size() > RootMask.size() && OpMask.size() % RootMask .size() == 0) || OpMask.size() == RootMask.size()) && "The smaller number of elements must divide the larger.") ? void (0) : __assert_fail ("((RootMask.size() > OpMask.size() && RootMask.size() % OpMask.size() == 0) || (OpMask.size() > RootMask.size() && OpMask.size() % RootMask.size() == 0) || OpMask.size() == RootMask.size()) && \"The smaller number of elements must divide the larger.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37960, __extension__ __PRETTY_FUNCTION__)); | |||
37961 | ||||
37962 | // This function can be performance-critical, so we rely on the power-of-2 | |||
37963 | // knowledge that we have about the mask sizes to replace div/rem ops with | |||
37964 | // bit-masks and shifts. | |||
37965 | assert(isPowerOf2_32(RootMask.size()) &&(static_cast <bool> (isPowerOf2_32(RootMask.size()) && "Non-power-of-2 shuffle mask sizes") ? void (0) : __assert_fail ("isPowerOf2_32(RootMask.size()) && \"Non-power-of-2 shuffle mask sizes\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37966, __extension__ __PRETTY_FUNCTION__)) | |||
37966 | "Non-power-of-2 shuffle mask sizes")(static_cast <bool> (isPowerOf2_32(RootMask.size()) && "Non-power-of-2 shuffle mask sizes") ? void (0) : __assert_fail ("isPowerOf2_32(RootMask.size()) && \"Non-power-of-2 shuffle mask sizes\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37966, __extension__ __PRETTY_FUNCTION__)); | |||
37967 | assert(isPowerOf2_32(OpMask.size()) && "Non-power-of-2 shuffle mask sizes")(static_cast <bool> (isPowerOf2_32(OpMask.size()) && "Non-power-of-2 shuffle mask sizes") ? void (0) : __assert_fail ("isPowerOf2_32(OpMask.size()) && \"Non-power-of-2 shuffle mask sizes\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37967, __extension__ __PRETTY_FUNCTION__)); | |||
37968 | unsigned RootMaskSizeLog2 = countTrailingZeros(RootMask.size()); | |||
37969 | unsigned OpMaskSizeLog2 = countTrailingZeros(OpMask.size()); | |||
37970 | ||||
37971 | unsigned MaskWidth = std::max<unsigned>(OpMask.size(), RootMask.size()); | |||
37972 | unsigned RootRatio = | |||
37973 | std::max<unsigned>(1, OpMask.size() >> RootMaskSizeLog2); | |||
37974 | unsigned OpRatio = std::max<unsigned>(1, RootMask.size() >> OpMaskSizeLog2); | |||
37975 | assert((RootRatio == 1 || OpRatio == 1) &&(static_cast <bool> ((RootRatio == 1 || OpRatio == 1) && "Must not have a ratio for both incoming and op masks!") ? void (0) : __assert_fail ("(RootRatio == 1 || OpRatio == 1) && \"Must not have a ratio for both incoming and op masks!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37976, __extension__ __PRETTY_FUNCTION__)) | |||
37976 | "Must not have a ratio for both incoming and op masks!")(static_cast <bool> ((RootRatio == 1 || OpRatio == 1) && "Must not have a ratio for both incoming and op masks!") ? void (0) : __assert_fail ("(RootRatio == 1 || OpRatio == 1) && \"Must not have a ratio for both incoming and op masks!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37976, __extension__ __PRETTY_FUNCTION__)); | |||
37977 | ||||
37978 | assert(isPowerOf2_32(MaskWidth) && "Non-power-of-2 shuffle mask sizes")(static_cast <bool> (isPowerOf2_32(MaskWidth) && "Non-power-of-2 shuffle mask sizes") ? void (0) : __assert_fail ("isPowerOf2_32(MaskWidth) && \"Non-power-of-2 shuffle mask sizes\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37978, __extension__ __PRETTY_FUNCTION__)); | |||
37979 | assert(isPowerOf2_32(RootRatio) && "Non-power-of-2 shuffle mask sizes")(static_cast <bool> (isPowerOf2_32(RootRatio) && "Non-power-of-2 shuffle mask sizes") ? void (0) : __assert_fail ("isPowerOf2_32(RootRatio) && \"Non-power-of-2 shuffle mask sizes\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37979, __extension__ __PRETTY_FUNCTION__)); | |||
37980 | assert(isPowerOf2_32(OpRatio) && "Non-power-of-2 shuffle mask sizes")(static_cast <bool> (isPowerOf2_32(OpRatio) && "Non-power-of-2 shuffle mask sizes" ) ? void (0) : __assert_fail ("isPowerOf2_32(OpRatio) && \"Non-power-of-2 shuffle mask sizes\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 37980, __extension__ __PRETTY_FUNCTION__)); | |||
37981 | unsigned RootRatioLog2 = countTrailingZeros(RootRatio); | |||
37982 | unsigned OpRatioLog2 = countTrailingZeros(OpRatio); | |||
37983 | ||||
37984 | Mask.resize(MaskWidth, SM_SentinelUndef); | |||
37985 | ||||
37986 | // Merge this shuffle operation's mask into our accumulated mask. Note that | |||
37987 | // this shuffle's mask will be the first applied to the input, followed by | |||
37988 | // the root mask to get us all the way to the root value arrangement. The | |||
37989 | // reason for this order is that we are recursing up the operation chain. | |||
37990 | for (unsigned i = 0; i < MaskWidth; ++i) { | |||
37991 | unsigned RootIdx = i >> RootRatioLog2; | |||
37992 | if (RootMask[RootIdx] < 0) { | |||
37993 | // This is a zero or undef lane, we're done. | |||
37994 | Mask[i] = RootMask[RootIdx]; | |||
37995 | continue; | |||
37996 | } | |||
37997 | ||||
37998 | unsigned RootMaskedIdx = | |||
37999 | RootRatio == 1 | |||
38000 | ? RootMask[RootIdx] | |||
38001 | : (RootMask[RootIdx] << RootRatioLog2) + (i & (RootRatio - 1)); | |||
38002 | ||||
38003 | // Just insert the scaled root mask value if it references an input other | |||
38004 | // than the SrcOp we're currently inserting. | |||
38005 | if ((RootMaskedIdx < (SrcOpIndex * MaskWidth)) || | |||
38006 | (((SrcOpIndex + 1) * MaskWidth) <= RootMaskedIdx)) { | |||
38007 | Mask[i] = RootMaskedIdx; | |||
38008 | continue; | |||
38009 | } | |||
38010 | ||||
38011 | RootMaskedIdx = RootMaskedIdx & (MaskWidth - 1); | |||
38012 | unsigned OpIdx = RootMaskedIdx >> OpRatioLog2; | |||
38013 | if (OpMask[OpIdx] < 0) { | |||
38014 | // The incoming lanes are zero or undef, it doesn't matter which ones we | |||
38015 | // are using. | |||
38016 | Mask[i] = OpMask[OpIdx]; | |||
38017 | continue; | |||
38018 | } | |||
38019 | ||||
38020 | // Ok, we have non-zero lanes, map them through to one of the Op's inputs. | |||
38021 | unsigned OpMaskedIdx = OpRatio == 1 ? OpMask[OpIdx] | |||
38022 | : (OpMask[OpIdx] << OpRatioLog2) + | |||
38023 | (RootMaskedIdx & (OpRatio - 1)); | |||
38024 | ||||
38025 | OpMaskedIdx = OpMaskedIdx & (MaskWidth - 1); | |||
38026 | int InputIdx = OpMask[OpIdx] / (int)OpMask.size(); | |||
38027 | assert(0 <= OpInputIdx[InputIdx] && "Unknown target shuffle input")(static_cast <bool> (0 <= OpInputIdx[InputIdx] && "Unknown target shuffle input") ? void (0) : __assert_fail ( "0 <= OpInputIdx[InputIdx] && \"Unknown target shuffle input\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 38027, __extension__ __PRETTY_FUNCTION__)); | |||
38028 | OpMaskedIdx += OpInputIdx[InputIdx] * MaskWidth; | |||
38029 | ||||
38030 | Mask[i] = OpMaskedIdx; | |||
38031 | } | |||
38032 | } | |||
38033 | ||||
38034 | // Remove unused/repeated shuffle source ops. | |||
38035 | resolveTargetShuffleInputsAndMask(Ops, Mask); | |||
38036 | ||||
38037 | // Handle the all undef/zero/ones cases early. | |||
38038 | if (all_of(Mask, [](int Idx) { return Idx == SM_SentinelUndef; })) | |||
38039 | return DAG.getUNDEF(Root.getValueType()); | |||
38040 | if (all_of(Mask, [](int Idx) { return Idx < 0; })) | |||
38041 | return getZeroVector(Root.getSimpleValueType(), Subtarget, DAG, | |||
38042 | SDLoc(Root)); | |||
38043 | if (Ops.size() == 1 && ISD::isBuildVectorAllOnes(Ops[0].getNode()) && | |||
38044 | none_of(Mask, [](int M) { return M == SM_SentinelZero; })) | |||
38045 | return getOnesVector(Root.getValueType(), DAG, SDLoc(Root)); | |||
38046 | ||||
38047 | assert(!Ops.empty() && "Shuffle with no inputs detected")(static_cast <bool> (!Ops.empty() && "Shuffle with no inputs detected" ) ? void (0) : __assert_fail ("!Ops.empty() && \"Shuffle with no inputs detected\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 38047, __extension__ __PRETTY_FUNCTION__)); | |||
38048 | HasVariableMask |= IsOpVariableMask; | |||
38049 | ||||
38050 | // Update the list of shuffle nodes that have been combined so far. | |||
38051 | SmallVector<const SDNode *, 16> CombinedNodes(SrcNodes.begin(), | |||
38052 | SrcNodes.end()); | |||
38053 | CombinedNodes.push_back(Op.getNode()); | |||
38054 | ||||
38055 | // See if we can recurse into each shuffle source op (if it's a target | |||
38056 | // shuffle). The source op should only be generally combined if it either has | |||
38057 | // a single use (i.e. current Op) or all its users have already been combined, | |||
38058 | // if not then we can still combine but should prevent generation of variable | |||
38059 | // shuffles to avoid constant pool bloat. | |||
38060 | // Don't recurse if we already have more source ops than we can combine in | |||
38061 | // the remaining recursion depth. | |||
38062 | if (Ops.size() < (MaxDepth - Depth)) { | |||
38063 | for (int i = 0, e = Ops.size(); i < e; ++i) { | |||
38064 | // For empty roots, we need to resolve zeroable elements before combining | |||
38065 | // them with other shuffles. | |||
38066 | SmallVector<int, 64> ResolvedMask = Mask; | |||
38067 | if (EmptyRoot) | |||
38068 | resolveTargetShuffleFromZeroables(ResolvedMask, OpUndef, OpZero); | |||
38069 | bool AllowCrossLaneVar = false; | |||
38070 | bool AllowPerLaneVar = false; | |||
38071 | if (Ops[i].getNode()->hasOneUse() || | |||
38072 | SDNode::areOnlyUsersOf(CombinedNodes, Ops[i].getNode())) { | |||
38073 | AllowCrossLaneVar = AllowVariableCrossLaneMask; | |||
38074 | AllowPerLaneVar = AllowVariablePerLaneMask; | |||
38075 | } | |||
38076 | if (SDValue Res = combineX86ShufflesRecursively( | |||
38077 | Ops, i, Root, ResolvedMask, CombinedNodes, Depth + 1, MaxDepth, | |||
38078 | HasVariableMask, AllowCrossLaneVar, AllowPerLaneVar, DAG, | |||
38079 | Subtarget)) | |||
38080 | return Res; | |||
38081 | } | |||
38082 | } | |||
38083 | ||||
38084 | // Attempt to constant fold all of the constant source ops. | |||
38085 | if (SDValue Cst = combineX86ShufflesConstants( | |||
38086 | Ops, Mask, Root, HasVariableMask, DAG, Subtarget)) | |||
38087 | return Cst; | |||
38088 | ||||
38089 | // If constant fold failed and we only have constants - then we have | |||
38090 | // multiple uses by a single non-variable shuffle - just bail. | |||
38091 | if (Depth == 0 && llvm::all_of(Ops, [&](SDValue Op) { | |||
38092 | APInt UndefElts; | |||
38093 | SmallVector<APInt> RawBits; | |||
38094 | unsigned EltSizeInBits = RootSizeInBits / Mask.size(); | |||
38095 | return getTargetConstantBitsFromNode(Op, EltSizeInBits, UndefElts, | |||
38096 | RawBits); | |||
38097 | })) { | |||
38098 | return SDValue(); | |||
38099 | } | |||
38100 | ||||
38101 | // Canonicalize the combined shuffle mask chain with horizontal ops. | |||
38102 | // NOTE: This will update the Ops and Mask. | |||
38103 | if (SDValue HOp = canonicalizeShuffleMaskWithHorizOp( | |||
38104 | Ops, Mask, RootSizeInBits, SDLoc(Root), DAG, Subtarget)) | |||
38105 | return DAG.getBitcast(Root.getValueType(), HOp); | |||
38106 | ||||
38107 | // Try to refine our inputs given our knowledge of target shuffle mask. | |||
38108 | for (auto I : enumerate(Ops)) { | |||
38109 | int OpIdx = I.index(); | |||
38110 | SDValue &Op = I.value(); | |||
38111 | ||||
38112 | // What range of shuffle mask element values results in picking from Op? | |||
38113 | int Lo = OpIdx * Mask.size(); | |||
38114 | int Hi = Lo + Mask.size(); | |||
38115 | ||||
38116 | // Which elements of Op do we demand, given the mask's granularity? | |||
38117 | APInt OpDemandedElts(Mask.size(), 0); | |||
38118 | for (int MaskElt : Mask) { | |||
38119 | if (isInRange(MaskElt, Lo, Hi)) { // Picks from Op? | |||
38120 | int OpEltIdx = MaskElt - Lo; | |||
38121 | OpDemandedElts.setBit(OpEltIdx); | |||
38122 | } | |||
38123 | } | |||
38124 | ||||
38125 | // Is the shuffle result smaller than the root? | |||
38126 | if (Op.getValueSizeInBits() < RootSizeInBits) { | |||
38127 | // We padded the mask with undefs. But we now need to undo that. | |||
38128 | unsigned NumExpectedVectorElts = Mask.size(); | |||
38129 | unsigned EltSizeInBits = RootSizeInBits / NumExpectedVectorElts; | |||
38130 | unsigned NumOpVectorElts = Op.getValueSizeInBits() / EltSizeInBits; | |||
38131 | assert(!OpDemandedElts.extractBits((static_cast <bool> (!OpDemandedElts.extractBits( NumExpectedVectorElts - NumOpVectorElts, NumOpVectorElts) && "Demanding the virtual undef widening padding?" ) ? void (0) : __assert_fail ("!OpDemandedElts.extractBits( NumExpectedVectorElts - NumOpVectorElts, NumOpVectorElts) && \"Demanding the virtual undef widening padding?\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 38133, __extension__ __PRETTY_FUNCTION__)) | |||
38132 | NumExpectedVectorElts - NumOpVectorElts, NumOpVectorElts) &&(static_cast <bool> (!OpDemandedElts.extractBits( NumExpectedVectorElts - NumOpVectorElts, NumOpVectorElts) && "Demanding the virtual undef widening padding?" ) ? void (0) : __assert_fail ("!OpDemandedElts.extractBits( NumExpectedVectorElts - NumOpVectorElts, NumOpVectorElts) && \"Demanding the virtual undef widening padding?\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 38133, __extension__ __PRETTY_FUNCTION__)) | |||
38133 | "Demanding the virtual undef widening padding?")(static_cast <bool> (!OpDemandedElts.extractBits( NumExpectedVectorElts - NumOpVectorElts, NumOpVectorElts) && "Demanding the virtual undef widening padding?" ) ? void (0) : __assert_fail ("!OpDemandedElts.extractBits( NumExpectedVectorElts - NumOpVectorElts, NumOpVectorElts) && \"Demanding the virtual undef widening padding?\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 38133, __extension__ __PRETTY_FUNCTION__)); | |||
38134 | OpDemandedElts = OpDemandedElts.trunc(NumOpVectorElts); // NUW | |||
38135 | } | |||
38136 | ||||
38137 | // The Op itself may be of different VT, so we need to scale the mask. | |||
38138 | unsigned NumOpElts = Op.getValueType().getVectorNumElements(); | |||
38139 | APInt OpScaledDemandedElts = APIntOps::ScaleBitMask(OpDemandedElts, NumOpElts); | |||
38140 | ||||
38141 | // Can this operand be simplified any further, given it's demanded elements? | |||
38142 | if (SDValue NewOp = | |||
38143 | DAG.getTargetLoweringInfo().SimplifyMultipleUseDemandedVectorElts( | |||
38144 | Op, OpScaledDemandedElts, DAG)) | |||
38145 | Op = NewOp; | |||
38146 | } | |||
38147 | // FIXME: should we rerun resolveTargetShuffleInputsAndMask() now? | |||
38148 | ||||
38149 | // Widen any subvector shuffle inputs we've collected. | |||
38150 | if (any_of(Ops, [RootSizeInBits](SDValue Op) { | |||
38151 | return Op.getValueSizeInBits() < RootSizeInBits; | |||
38152 | })) { | |||
38153 | for (SDValue &Op : Ops) | |||
38154 | if (Op.getValueSizeInBits() < RootSizeInBits) | |||
38155 | Op = widenSubVector(Op, false, Subtarget, DAG, SDLoc(Op), | |||
38156 | RootSizeInBits); | |||
38157 | // Reresolve - we might have repeated subvector sources. | |||
38158 | resolveTargetShuffleInputsAndMask(Ops, Mask); | |||
38159 | } | |||
38160 | ||||
38161 | // We can only combine unary and binary shuffle mask cases. | |||
38162 | if (Ops.size() <= 2) { | |||
38163 | // Minor canonicalization of the accumulated shuffle mask to make it easier | |||
38164 | // to match below. All this does is detect masks with sequential pairs of | |||
38165 | // elements, and shrink them to the half-width mask. It does this in a loop | |||
38166 | // so it will reduce the size of the mask to the minimal width mask which | |||
38167 | // performs an equivalent shuffle. | |||
38168 | while (Mask.size() > 1) { | |||
38169 | SmallVector<int, 64> WidenedMask; | |||
38170 | if (!canWidenShuffleElements(Mask, WidenedMask)) | |||
38171 | break; | |||
38172 | Mask = std::move(WidenedMask); | |||
38173 | } | |||
38174 | ||||
38175 | // Canonicalization of binary shuffle masks to improve pattern matching by | |||
38176 | // commuting the inputs. | |||
38177 | if (Ops.size() == 2 && canonicalizeShuffleMaskWithCommute(Mask)) { | |||
38178 | ShuffleVectorSDNode::commuteMask(Mask); | |||
38179 | std::swap(Ops[0], Ops[1]); | |||
38180 | } | |||
38181 | ||||
38182 | // Finally, try to combine into a single shuffle instruction. | |||
38183 | return combineX86ShuffleChain(Ops, Root, Mask, Depth, HasVariableMask, | |||
38184 | AllowVariableCrossLaneMask, | |||
38185 | AllowVariablePerLaneMask, DAG, Subtarget); | |||
38186 | } | |||
38187 | ||||
38188 | // If that failed and any input is extracted then try to combine as a | |||
38189 | // shuffle with the larger type. | |||
38190 | return combineX86ShuffleChainWithExtract( | |||
38191 | Ops, Root, Mask, Depth, HasVariableMask, AllowVariableCrossLaneMask, | |||
38192 | AllowVariablePerLaneMask, DAG, Subtarget); | |||
38193 | } | |||
38194 | ||||
38195 | /// Helper entry wrapper to combineX86ShufflesRecursively. | |||
38196 | static SDValue combineX86ShufflesRecursively(SDValue Op, SelectionDAG &DAG, | |||
38197 | const X86Subtarget &Subtarget) { | |||
38198 | return combineX86ShufflesRecursively( | |||
38199 | {Op}, 0, Op, {0}, {}, /*Depth*/ 0, X86::MaxShuffleCombineDepth, | |||
38200 | /*HasVarMask*/ false, | |||
38201 | /*AllowCrossLaneVarMask*/ true, /*AllowPerLaneVarMask*/ true, DAG, | |||
38202 | Subtarget); | |||
38203 | } | |||
38204 | ||||
38205 | /// Get the PSHUF-style mask from PSHUF node. | |||
38206 | /// | |||
38207 | /// This is a very minor wrapper around getTargetShuffleMask to easy forming v4 | |||
38208 | /// PSHUF-style masks that can be reused with such instructions. | |||
38209 | static SmallVector<int, 4> getPSHUFShuffleMask(SDValue N) { | |||
38210 | MVT VT = N.getSimpleValueType(); | |||
38211 | SmallVector<int, 4> Mask; | |||
38212 | SmallVector<SDValue, 2> Ops; | |||
38213 | bool HaveMask = | |||
38214 | getTargetShuffleMask(N.getNode(), VT, false, Ops, Mask); | |||
38215 | (void)HaveMask; | |||
38216 | assert(HaveMask)(static_cast <bool> (HaveMask) ? void (0) : __assert_fail ("HaveMask", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 38216, __extension__ __PRETTY_FUNCTION__)); | |||
38217 | ||||
38218 | // If we have more than 128-bits, only the low 128-bits of shuffle mask | |||
38219 | // matter. Check that the upper masks are repeats and remove them. | |||
38220 | if (VT.getSizeInBits() > 128) { | |||
38221 | int LaneElts = 128 / VT.getScalarSizeInBits(); | |||
38222 | #ifndef NDEBUG | |||
38223 | for (int i = 1, NumLanes = VT.getSizeInBits() / 128; i < NumLanes; ++i) | |||
38224 | for (int j = 0; j < LaneElts; ++j) | |||
38225 | assert(Mask[j] == Mask[i * LaneElts + j] - (LaneElts * i) &&(static_cast <bool> (Mask[j] == Mask[i * LaneElts + j] - (LaneElts * i) && "Mask doesn't repeat in high 128-bit lanes!" ) ? void (0) : __assert_fail ("Mask[j] == Mask[i * LaneElts + j] - (LaneElts * i) && \"Mask doesn't repeat in high 128-bit lanes!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 38226, __extension__ __PRETTY_FUNCTION__)) | |||
38226 | "Mask doesn't repeat in high 128-bit lanes!")(static_cast <bool> (Mask[j] == Mask[i * LaneElts + j] - (LaneElts * i) && "Mask doesn't repeat in high 128-bit lanes!" ) ? void (0) : __assert_fail ("Mask[j] == Mask[i * LaneElts + j] - (LaneElts * i) && \"Mask doesn't repeat in high 128-bit lanes!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 38226, __extension__ __PRETTY_FUNCTION__)); | |||
38227 | #endif | |||
38228 | Mask.resize(LaneElts); | |||
38229 | } | |||
38230 | ||||
38231 | switch (N.getOpcode()) { | |||
38232 | case X86ISD::PSHUFD: | |||
38233 | return Mask; | |||
38234 | case X86ISD::PSHUFLW: | |||
38235 | Mask.resize(4); | |||
38236 | return Mask; | |||
38237 | case X86ISD::PSHUFHW: | |||
38238 | Mask.erase(Mask.begin(), Mask.begin() + 4); | |||
38239 | for (int &M : Mask) | |||
38240 | M -= 4; | |||
38241 | return Mask; | |||
38242 | default: | |||
38243 | llvm_unreachable("No valid shuffle instruction found!")::llvm::llvm_unreachable_internal("No valid shuffle instruction found!" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 38243); | |||
38244 | } | |||
38245 | } | |||
38246 | ||||
38247 | /// Search for a combinable shuffle across a chain ending in pshufd. | |||
38248 | /// | |||
38249 | /// We walk up the chain and look for a combinable shuffle, skipping over | |||
38250 | /// shuffles that we could hoist this shuffle's transformation past without | |||
38251 | /// altering anything. | |||
38252 | static SDValue | |||
38253 | combineRedundantDWordShuffle(SDValue N, MutableArrayRef<int> Mask, | |||
38254 | SelectionDAG &DAG) { | |||
38255 | assert(N.getOpcode() == X86ISD::PSHUFD &&(static_cast <bool> (N.getOpcode() == X86ISD::PSHUFD && "Called with something other than an x86 128-bit half shuffle!" ) ? void (0) : __assert_fail ("N.getOpcode() == X86ISD::PSHUFD && \"Called with something other than an x86 128-bit half shuffle!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 38256, __extension__ __PRETTY_FUNCTION__)) | |||
38256 | "Called with something other than an x86 128-bit half shuffle!")(static_cast <bool> (N.getOpcode() == X86ISD::PSHUFD && "Called with something other than an x86 128-bit half shuffle!" ) ? void (0) : __assert_fail ("N.getOpcode() == X86ISD::PSHUFD && \"Called with something other than an x86 128-bit half shuffle!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 38256, __extension__ __PRETTY_FUNCTION__)); | |||
38257 | SDLoc DL(N); | |||
38258 | ||||
38259 | // Walk up a single-use chain looking for a combinable shuffle. Keep a stack | |||
38260 | // of the shuffles in the chain so that we can form a fresh chain to replace | |||
38261 | // this one. | |||
38262 | SmallVector<SDValue, 8> Chain; | |||
38263 | SDValue V = N.getOperand(0); | |||
38264 | for (; V.hasOneUse(); V = V.getOperand(0)) { | |||
38265 | switch (V.getOpcode()) { | |||
38266 | default: | |||
38267 | return SDValue(); // Nothing combined! | |||
38268 | ||||
38269 | case ISD::BITCAST: | |||
38270 | // Skip bitcasts as we always know the type for the target specific | |||
38271 | // instructions. | |||
38272 | continue; | |||
38273 | ||||
38274 | case X86ISD::PSHUFD: | |||
38275 | // Found another dword shuffle. | |||
38276 | break; | |||
38277 | ||||
38278 | case X86ISD::PSHUFLW: | |||
38279 | // Check that the low words (being shuffled) are the identity in the | |||
38280 | // dword shuffle, and the high words are self-contained. | |||
38281 | if (Mask[0] != 0 || Mask[1] != 1 || | |||
38282 | !(Mask[2] >= 2 && Mask[2] < 4 && Mask[3] >= 2 && Mask[3] < 4)) | |||
38283 | return SDValue(); | |||
38284 | ||||
38285 | Chain.push_back(V); | |||
38286 | continue; | |||
38287 | ||||
38288 | case X86ISD::PSHUFHW: | |||
38289 | // Check that the high words (being shuffled) are the identity in the | |||
38290 | // dword shuffle, and the low words are self-contained. | |||
38291 | if (Mask[2] != 2 || Mask[3] != 3 || | |||
38292 | !(Mask[0] >= 0 && Mask[0] < 2 && Mask[1] >= 0 && Mask[1] < 2)) | |||
38293 | return SDValue(); | |||
38294 | ||||
38295 | Chain.push_back(V); | |||
38296 | continue; | |||
38297 | ||||
38298 | case X86ISD::UNPCKL: | |||
38299 | case X86ISD::UNPCKH: | |||
38300 | // For either i8 -> i16 or i16 -> i32 unpacks, we can combine a dword | |||
38301 | // shuffle into a preceding word shuffle. | |||
38302 | if (V.getSimpleValueType().getVectorElementType() != MVT::i8 && | |||
38303 | V.getSimpleValueType().getVectorElementType() != MVT::i16) | |||
38304 | return SDValue(); | |||
38305 | ||||
38306 | // Search for a half-shuffle which we can combine with. | |||
38307 | unsigned CombineOp = | |||
38308 | V.getOpcode() == X86ISD::UNPCKL ? X86ISD::PSHUFLW : X86ISD::PSHUFHW; | |||
38309 | if (V.getOperand(0) != V.getOperand(1) || | |||
38310 | !V->isOnlyUserOf(V.getOperand(0).getNode())) | |||
38311 | return SDValue(); | |||
38312 | Chain.push_back(V); | |||
38313 | V = V.getOperand(0); | |||
38314 | do { | |||
38315 | switch (V.getOpcode()) { | |||
38316 | default: | |||
38317 | return SDValue(); // Nothing to combine. | |||
38318 | ||||
38319 | case X86ISD::PSHUFLW: | |||
38320 | case X86ISD::PSHUFHW: | |||
38321 | if (V.getOpcode() == CombineOp) | |||
38322 | break; | |||
38323 | ||||
38324 | Chain.push_back(V); | |||
38325 | ||||
38326 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
38327 | case ISD::BITCAST: | |||
38328 | V = V.getOperand(0); | |||
38329 | continue; | |||
38330 | } | |||
38331 | break; | |||
38332 | } while (V.hasOneUse()); | |||
38333 | break; | |||
38334 | } | |||
38335 | // Break out of the loop if we break out of the switch. | |||
38336 | break; | |||
38337 | } | |||
38338 | ||||
38339 | if (!V.hasOneUse()) | |||
38340 | // We fell out of the loop without finding a viable combining instruction. | |||
38341 | return SDValue(); | |||
38342 | ||||
38343 | // Merge this node's mask and our incoming mask. | |||
38344 | SmallVector<int, 4> VMask = getPSHUFShuffleMask(V); | |||
38345 | for (int &M : Mask) | |||
38346 | M = VMask[M]; | |||
38347 | V = DAG.getNode(V.getOpcode(), DL, V.getValueType(), V.getOperand(0), | |||
38348 | getV4X86ShuffleImm8ForMask(Mask, DL, DAG)); | |||
38349 | ||||
38350 | // Rebuild the chain around this new shuffle. | |||
38351 | while (!Chain.empty()) { | |||
38352 | SDValue W = Chain.pop_back_val(); | |||
38353 | ||||
38354 | if (V.getValueType() != W.getOperand(0).getValueType()) | |||
38355 | V = DAG.getBitcast(W.getOperand(0).getValueType(), V); | |||
38356 | ||||
38357 | switch (W.getOpcode()) { | |||
38358 | default: | |||
38359 | llvm_unreachable("Only PSHUF and UNPCK instructions get here!")::llvm::llvm_unreachable_internal("Only PSHUF and UNPCK instructions get here!" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 38359); | |||
38360 | ||||
38361 | case X86ISD::UNPCKL: | |||
38362 | case X86ISD::UNPCKH: | |||
38363 | V = DAG.getNode(W.getOpcode(), DL, W.getValueType(), V, V); | |||
38364 | break; | |||
38365 | ||||
38366 | case X86ISD::PSHUFD: | |||
38367 | case X86ISD::PSHUFLW: | |||
38368 | case X86ISD::PSHUFHW: | |||
38369 | V = DAG.getNode(W.getOpcode(), DL, W.getValueType(), V, W.getOperand(1)); | |||
38370 | break; | |||
38371 | } | |||
38372 | } | |||
38373 | if (V.getValueType() != N.getValueType()) | |||
38374 | V = DAG.getBitcast(N.getValueType(), V); | |||
38375 | ||||
38376 | // Return the new chain to replace N. | |||
38377 | return V; | |||
38378 | } | |||
38379 | ||||
38380 | // Attempt to commute shufps LHS loads: | |||
38381 | // permilps(shufps(load(),x)) --> permilps(shufps(x,load())) | |||
38382 | static SDValue combineCommutableSHUFP(SDValue N, MVT VT, const SDLoc &DL, | |||
38383 | SelectionDAG &DAG) { | |||
38384 | // TODO: Add vXf64 support. | |||
38385 | if (VT != MVT::v4f32 && VT != MVT::v8f32 && VT != MVT::v16f32) | |||
38386 | return SDValue(); | |||
38387 | ||||
38388 | // SHUFP(LHS, RHS) -> SHUFP(RHS, LHS) iff LHS is foldable + RHS is not. | |||
38389 | auto commuteSHUFP = [&VT, &DL, &DAG](SDValue Parent, SDValue V) { | |||
38390 | if (V.getOpcode() != X86ISD::SHUFP || !Parent->isOnlyUserOf(V.getNode())) | |||
38391 | return SDValue(); | |||
38392 | SDValue N0 = V.getOperand(0); | |||
38393 | SDValue N1 = V.getOperand(1); | |||
38394 | unsigned Imm = V.getConstantOperandVal(2); | |||
38395 | if (!MayFoldLoad(peekThroughOneUseBitcasts(N0)) || | |||
38396 | MayFoldLoad(peekThroughOneUseBitcasts(N1))) | |||
38397 | return SDValue(); | |||
38398 | Imm = ((Imm & 0x0F) << 4) | ((Imm & 0xF0) >> 4); | |||
38399 | return DAG.getNode(X86ISD::SHUFP, DL, VT, N1, N0, | |||
38400 | DAG.getTargetConstant(Imm, DL, MVT::i8)); | |||
38401 | }; | |||
38402 | ||||
38403 | switch (N.getOpcode()) { | |||
38404 | case X86ISD::VPERMILPI: | |||
38405 | if (SDValue NewSHUFP = commuteSHUFP(N, N.getOperand(0))) { | |||
38406 | unsigned Imm = N.getConstantOperandVal(1); | |||
38407 | return DAG.getNode(X86ISD::VPERMILPI, DL, VT, NewSHUFP, | |||
38408 | DAG.getTargetConstant(Imm ^ 0xAA, DL, MVT::i8)); | |||
38409 | } | |||
38410 | break; | |||
38411 | case X86ISD::SHUFP: { | |||
38412 | SDValue N0 = N.getOperand(0); | |||
38413 | SDValue N1 = N.getOperand(1); | |||
38414 | unsigned Imm = N.getConstantOperandVal(2); | |||
38415 | if (N0 == N1) { | |||
38416 | if (SDValue NewSHUFP = commuteSHUFP(N, N0)) | |||
38417 | return DAG.getNode(X86ISD::SHUFP, DL, VT, NewSHUFP, NewSHUFP, | |||
38418 | DAG.getTargetConstant(Imm ^ 0xAA, DL, MVT::i8)); | |||
38419 | } else if (SDValue NewSHUFP = commuteSHUFP(N, N0)) { | |||
38420 | return DAG.getNode(X86ISD::SHUFP, DL, VT, NewSHUFP, N1, | |||
38421 | DAG.getTargetConstant(Imm ^ 0x0A, DL, MVT::i8)); | |||
38422 | } else if (SDValue NewSHUFP = commuteSHUFP(N, N1)) { | |||
38423 | return DAG.getNode(X86ISD::SHUFP, DL, VT, N0, NewSHUFP, | |||
38424 | DAG.getTargetConstant(Imm ^ 0xA0, DL, MVT::i8)); | |||
38425 | } | |||
38426 | break; | |||
38427 | } | |||
38428 | } | |||
38429 | ||||
38430 | return SDValue(); | |||
38431 | } | |||
38432 | ||||
38433 | // Canonicalize SHUFFLE(BINOP(X,Y)) -> BINOP(SHUFFLE(X),SHUFFLE(Y)). | |||
38434 | static SDValue canonicalizeShuffleWithBinOps(SDValue N, SelectionDAG &DAG, | |||
38435 | const SDLoc &DL) { | |||
38436 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
38437 | EVT ShuffleVT = N.getValueType(); | |||
38438 | ||||
38439 | auto IsMergeableWithShuffle = [](SDValue Op) { | |||
38440 | // AllZeros/AllOnes constants are freely shuffled and will peek through | |||
38441 | // bitcasts. Other constant build vectors do not peek through bitcasts. Only | |||
38442 | // merge with target shuffles if it has one use so shuffle combining is | |||
38443 | // likely to kick in. | |||
38444 | return ISD::isBuildVectorAllOnes(Op.getNode()) || | |||
38445 | ISD::isBuildVectorAllZeros(Op.getNode()) || | |||
38446 | ISD::isBuildVectorOfConstantSDNodes(Op.getNode()) || | |||
38447 | ISD::isBuildVectorOfConstantFPSDNodes(Op.getNode()) || | |||
38448 | (isTargetShuffle(Op.getOpcode()) && Op->hasOneUse()); | |||
38449 | }; | |||
38450 | auto IsSafeToMoveShuffle = [ShuffleVT](SDValue Op, unsigned BinOp) { | |||
38451 | // Ensure we only shuffle whole vector src elements, unless its a logical | |||
38452 | // binops where we can more aggressively move shuffles from dst to src. | |||
38453 | return BinOp == ISD::AND || BinOp == ISD::OR || BinOp == ISD::XOR || | |||
38454 | (Op.getScalarValueSizeInBits() <= ShuffleVT.getScalarSizeInBits()); | |||
38455 | }; | |||
38456 | ||||
38457 | unsigned Opc = N.getOpcode(); | |||
38458 | switch (Opc) { | |||
38459 | // Unary and Unary+Permute Shuffles. | |||
38460 | case X86ISD::PSHUFB: { | |||
38461 | // Don't merge PSHUFB if it contains zero'd elements. | |||
38462 | SmallVector<int> Mask; | |||
38463 | SmallVector<SDValue> Ops; | |||
38464 | if (!getTargetShuffleMask(N.getNode(), ShuffleVT.getSimpleVT(), false, Ops, | |||
38465 | Mask)) | |||
38466 | break; | |||
38467 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
38468 | } | |||
38469 | case X86ISD::VBROADCAST: | |||
38470 | case X86ISD::MOVDDUP: | |||
38471 | case X86ISD::PSHUFD: | |||
38472 | case X86ISD::VPERMI: | |||
38473 | case X86ISD::VPERMILPI: { | |||
38474 | if (N.getOperand(0).getValueType() == ShuffleVT && | |||
38475 | N->isOnlyUserOf(N.getOperand(0).getNode())) { | |||
38476 | SDValue N0 = peekThroughOneUseBitcasts(N.getOperand(0)); | |||
38477 | unsigned SrcOpcode = N0.getOpcode(); | |||
38478 | if (TLI.isBinOp(SrcOpcode) && IsSafeToMoveShuffle(N0, SrcOpcode)) { | |||
38479 | SDValue Op00 = peekThroughOneUseBitcasts(N0.getOperand(0)); | |||
38480 | SDValue Op01 = peekThroughOneUseBitcasts(N0.getOperand(1)); | |||
38481 | if (IsMergeableWithShuffle(Op00) || IsMergeableWithShuffle(Op01)) { | |||
38482 | SDValue LHS, RHS; | |||
38483 | Op00 = DAG.getBitcast(ShuffleVT, Op00); | |||
38484 | Op01 = DAG.getBitcast(ShuffleVT, Op01); | |||
38485 | if (N.getNumOperands() == 2) { | |||
38486 | LHS = DAG.getNode(Opc, DL, ShuffleVT, Op00, N.getOperand(1)); | |||
38487 | RHS = DAG.getNode(Opc, DL, ShuffleVT, Op01, N.getOperand(1)); | |||
38488 | } else { | |||
38489 | LHS = DAG.getNode(Opc, DL, ShuffleVT, Op00); | |||
38490 | RHS = DAG.getNode(Opc, DL, ShuffleVT, Op01); | |||
38491 | } | |||
38492 | EVT OpVT = N0.getValueType(); | |||
38493 | return DAG.getBitcast(ShuffleVT, | |||
38494 | DAG.getNode(SrcOpcode, DL, OpVT, | |||
38495 | DAG.getBitcast(OpVT, LHS), | |||
38496 | DAG.getBitcast(OpVT, RHS))); | |||
38497 | } | |||
38498 | } | |||
38499 | } | |||
38500 | break; | |||
38501 | } | |||
38502 | // Binary and Binary+Permute Shuffles. | |||
38503 | case X86ISD::INSERTPS: { | |||
38504 | // Don't merge INSERTPS if it contains zero'd elements. | |||
38505 | unsigned InsertPSMask = N.getConstantOperandVal(2); | |||
38506 | unsigned ZeroMask = InsertPSMask & 0xF; | |||
38507 | if (ZeroMask != 0) | |||
38508 | break; | |||
38509 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
38510 | } | |||
38511 | case X86ISD::MOVSD: | |||
38512 | case X86ISD::MOVSS: | |||
38513 | case X86ISD::BLENDI: | |||
38514 | case X86ISD::SHUFP: | |||
38515 | case X86ISD::UNPCKH: | |||
38516 | case X86ISD::UNPCKL: { | |||
38517 | if (N->isOnlyUserOf(N.getOperand(0).getNode()) && | |||
38518 | N->isOnlyUserOf(N.getOperand(1).getNode())) { | |||
38519 | SDValue N0 = peekThroughOneUseBitcasts(N.getOperand(0)); | |||
38520 | SDValue N1 = peekThroughOneUseBitcasts(N.getOperand(1)); | |||
38521 | unsigned SrcOpcode = N0.getOpcode(); | |||
38522 | if (TLI.isBinOp(SrcOpcode) && N1.getOpcode() == SrcOpcode && | |||
38523 | IsSafeToMoveShuffle(N0, SrcOpcode) && | |||
38524 | IsSafeToMoveShuffle(N1, SrcOpcode)) { | |||
38525 | SDValue Op00 = peekThroughOneUseBitcasts(N0.getOperand(0)); | |||
38526 | SDValue Op10 = peekThroughOneUseBitcasts(N1.getOperand(0)); | |||
38527 | SDValue Op01 = peekThroughOneUseBitcasts(N0.getOperand(1)); | |||
38528 | SDValue Op11 = peekThroughOneUseBitcasts(N1.getOperand(1)); | |||
38529 | // Ensure the total number of shuffles doesn't increase by folding this | |||
38530 | // shuffle through to the source ops. | |||
38531 | if (((IsMergeableWithShuffle(Op00) && IsMergeableWithShuffle(Op10)) || | |||
38532 | (IsMergeableWithShuffle(Op01) && IsMergeableWithShuffle(Op11))) || | |||
38533 | ((IsMergeableWithShuffle(Op00) || IsMergeableWithShuffle(Op10)) && | |||
38534 | (IsMergeableWithShuffle(Op01) || IsMergeableWithShuffle(Op11)))) { | |||
38535 | SDValue LHS, RHS; | |||
38536 | Op00 = DAG.getBitcast(ShuffleVT, Op00); | |||
38537 | Op10 = DAG.getBitcast(ShuffleVT, Op10); | |||
38538 | Op01 = DAG.getBitcast(ShuffleVT, Op01); | |||
38539 | Op11 = DAG.getBitcast(ShuffleVT, Op11); | |||
38540 | if (N.getNumOperands() == 3) { | |||
38541 | LHS = DAG.getNode(Opc, DL, ShuffleVT, Op00, Op10, N.getOperand(2)); | |||
38542 | RHS = DAG.getNode(Opc, DL, ShuffleVT, Op01, Op11, N.getOperand(2)); | |||
38543 | } else { | |||
38544 | LHS = DAG.getNode(Opc, DL, ShuffleVT, Op00, Op10); | |||
38545 | RHS = DAG.getNode(Opc, DL, ShuffleVT, Op01, Op11); | |||
38546 | } | |||
38547 | EVT OpVT = N0.getValueType(); | |||
38548 | return DAG.getBitcast(ShuffleVT, | |||
38549 | DAG.getNode(SrcOpcode, DL, OpVT, | |||
38550 | DAG.getBitcast(OpVT, LHS), | |||
38551 | DAG.getBitcast(OpVT, RHS))); | |||
38552 | } | |||
38553 | } | |||
38554 | } | |||
38555 | break; | |||
38556 | } | |||
38557 | } | |||
38558 | return SDValue(); | |||
38559 | } | |||
38560 | ||||
38561 | /// Attempt to fold vpermf128(op(),op()) -> op(vpermf128(),vpermf128()). | |||
38562 | static SDValue canonicalizeLaneShuffleWithRepeatedOps(SDValue V, | |||
38563 | SelectionDAG &DAG, | |||
38564 | const SDLoc &DL) { | |||
38565 | assert(V.getOpcode() == X86ISD::VPERM2X128 && "Unknown lane shuffle")(static_cast <bool> (V.getOpcode() == X86ISD::VPERM2X128 && "Unknown lane shuffle") ? void (0) : __assert_fail ("V.getOpcode() == X86ISD::VPERM2X128 && \"Unknown lane shuffle\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 38565, __extension__ __PRETTY_FUNCTION__)); | |||
38566 | ||||
38567 | MVT VT = V.getSimpleValueType(); | |||
38568 | SDValue Src0 = peekThroughBitcasts(V.getOperand(0)); | |||
38569 | SDValue Src1 = peekThroughBitcasts(V.getOperand(1)); | |||
38570 | unsigned SrcOpc0 = Src0.getOpcode(); | |||
38571 | unsigned SrcOpc1 = Src1.getOpcode(); | |||
38572 | EVT SrcVT0 = Src0.getValueType(); | |||
38573 | EVT SrcVT1 = Src1.getValueType(); | |||
38574 | ||||
38575 | if (!Src1.isUndef() && (SrcVT0 != SrcVT1 || SrcOpc0 != SrcOpc1)) | |||
38576 | return SDValue(); | |||
38577 | ||||
38578 | switch (SrcOpc0) { | |||
38579 | case X86ISD::MOVDDUP: { | |||
38580 | SDValue LHS = Src0.getOperand(0); | |||
38581 | SDValue RHS = Src1.isUndef() ? Src1 : Src1.getOperand(0); | |||
38582 | SDValue Res = | |||
38583 | DAG.getNode(X86ISD::VPERM2X128, DL, SrcVT0, LHS, RHS, V.getOperand(2)); | |||
38584 | Res = DAG.getNode(SrcOpc0, DL, SrcVT0, Res); | |||
38585 | return DAG.getBitcast(VT, Res); | |||
38586 | } | |||
38587 | case X86ISD::VPERMILPI: | |||
38588 | // TODO: Handle v4f64 permutes with different low/high lane masks. | |||
38589 | if (SrcVT0 == MVT::v4f64) { | |||
38590 | uint64_t Mask = Src0.getConstantOperandVal(1); | |||
38591 | if ((Mask & 0x3) != ((Mask >> 2) & 0x3)) | |||
38592 | break; | |||
38593 | } | |||
38594 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
38595 | case X86ISD::VSHLI: | |||
38596 | case X86ISD::VSRLI: | |||
38597 | case X86ISD::VSRAI: | |||
38598 | case X86ISD::PSHUFD: | |||
38599 | if (Src1.isUndef() || Src0.getOperand(1) == Src1.getOperand(1)) { | |||
38600 | SDValue LHS = Src0.getOperand(0); | |||
38601 | SDValue RHS = Src1.isUndef() ? Src1 : Src1.getOperand(0); | |||
38602 | SDValue Res = DAG.getNode(X86ISD::VPERM2X128, DL, SrcVT0, LHS, RHS, | |||
38603 | V.getOperand(2)); | |||
38604 | Res = DAG.getNode(SrcOpc0, DL, SrcVT0, Res, Src0.getOperand(1)); | |||
38605 | return DAG.getBitcast(VT, Res); | |||
38606 | } | |||
38607 | break; | |||
38608 | } | |||
38609 | ||||
38610 | return SDValue(); | |||
38611 | } | |||
38612 | ||||
38613 | /// Try to combine x86 target specific shuffles. | |||
38614 | static SDValue combineTargetShuffle(SDValue N, SelectionDAG &DAG, | |||
38615 | TargetLowering::DAGCombinerInfo &DCI, | |||
38616 | const X86Subtarget &Subtarget) { | |||
38617 | SDLoc DL(N); | |||
38618 | MVT VT = N.getSimpleValueType(); | |||
38619 | SmallVector<int, 4> Mask; | |||
38620 | unsigned Opcode = N.getOpcode(); | |||
38621 | ||||
38622 | if (SDValue R = combineCommutableSHUFP(N, VT, DL, DAG)) | |||
38623 | return R; | |||
38624 | ||||
38625 | if (SDValue R = canonicalizeShuffleWithBinOps(N, DAG, DL)) | |||
38626 | return R; | |||
38627 | ||||
38628 | // Handle specific target shuffles. | |||
38629 | switch (Opcode) { | |||
38630 | case X86ISD::MOVDDUP: { | |||
38631 | SDValue Src = N.getOperand(0); | |||
38632 | // Turn a 128-bit MOVDDUP of a full vector load into movddup+vzload. | |||
38633 | if (VT == MVT::v2f64 && Src.hasOneUse() && | |||
38634 | ISD::isNormalLoad(Src.getNode())) { | |||
38635 | LoadSDNode *LN = cast<LoadSDNode>(Src); | |||
38636 | if (SDValue VZLoad = narrowLoadToVZLoad(LN, MVT::f64, MVT::v2f64, DAG)) { | |||
38637 | SDValue Movddup = DAG.getNode(X86ISD::MOVDDUP, DL, MVT::v2f64, VZLoad); | |||
38638 | DCI.CombineTo(N.getNode(), Movddup); | |||
38639 | DAG.ReplaceAllUsesOfValueWith(SDValue(LN, 1), VZLoad.getValue(1)); | |||
38640 | DCI.recursivelyDeleteUnusedNodes(LN); | |||
38641 | return N; // Return N so it doesn't get rechecked! | |||
38642 | } | |||
38643 | } | |||
38644 | ||||
38645 | return SDValue(); | |||
38646 | } | |||
38647 | case X86ISD::VBROADCAST: { | |||
38648 | SDValue Src = N.getOperand(0); | |||
38649 | SDValue BC = peekThroughBitcasts(Src); | |||
38650 | EVT SrcVT = Src.getValueType(); | |||
38651 | EVT BCVT = BC.getValueType(); | |||
38652 | ||||
38653 | // If broadcasting from another shuffle, attempt to simplify it. | |||
38654 | // TODO - we really need a general SimplifyDemandedVectorElts mechanism. | |||
38655 | if (isTargetShuffle(BC.getOpcode()) && | |||
38656 | VT.getScalarSizeInBits() % BCVT.getScalarSizeInBits() == 0) { | |||
38657 | unsigned Scale = VT.getScalarSizeInBits() / BCVT.getScalarSizeInBits(); | |||
38658 | SmallVector<int, 16> DemandedMask(BCVT.getVectorNumElements(), | |||
38659 | SM_SentinelUndef); | |||
38660 | for (unsigned i = 0; i != Scale; ++i) | |||
38661 | DemandedMask[i] = i; | |||
38662 | if (SDValue Res = combineX86ShufflesRecursively( | |||
38663 | {BC}, 0, BC, DemandedMask, {}, /*Depth*/ 0, | |||
38664 | X86::MaxShuffleCombineDepth, | |||
38665 | /*HasVarMask*/ false, /*AllowCrossLaneVarMask*/ true, | |||
38666 | /*AllowPerLaneVarMask*/ true, DAG, Subtarget)) | |||
38667 | return DAG.getNode(X86ISD::VBROADCAST, DL, VT, | |||
38668 | DAG.getBitcast(SrcVT, Res)); | |||
38669 | } | |||
38670 | ||||
38671 | // broadcast(bitcast(src)) -> bitcast(broadcast(src)) | |||
38672 | // 32-bit targets have to bitcast i64 to f64, so better to bitcast upward. | |||
38673 | if (Src.getOpcode() == ISD::BITCAST && | |||
38674 | SrcVT.getScalarSizeInBits() == BCVT.getScalarSizeInBits() && | |||
38675 | DAG.getTargetLoweringInfo().isTypeLegal(BCVT) && | |||
38676 | FixedVectorType::isValidElementType( | |||
38677 | BCVT.getScalarType().getTypeForEVT(*DAG.getContext()))) { | |||
38678 | EVT NewVT = EVT::getVectorVT(*DAG.getContext(), BCVT.getScalarType(), | |||
38679 | VT.getVectorNumElements()); | |||
38680 | return DAG.getBitcast(VT, DAG.getNode(X86ISD::VBROADCAST, DL, NewVT, BC)); | |||
38681 | } | |||
38682 | ||||
38683 | // Reduce broadcast source vector to lowest 128-bits. | |||
38684 | if (SrcVT.getSizeInBits() > 128) | |||
38685 | return DAG.getNode(X86ISD::VBROADCAST, DL, VT, | |||
38686 | extract128BitVector(Src, 0, DAG, DL)); | |||
38687 | ||||
38688 | // broadcast(scalar_to_vector(x)) -> broadcast(x). | |||
38689 | if (Src.getOpcode() == ISD::SCALAR_TO_VECTOR) | |||
38690 | return DAG.getNode(X86ISD::VBROADCAST, DL, VT, Src.getOperand(0)); | |||
38691 | ||||
38692 | // broadcast(extract_vector_elt(x, 0)) -> broadcast(x). | |||
38693 | if (Src.getOpcode() == ISD::EXTRACT_VECTOR_ELT && | |||
38694 | isNullConstant(Src.getOperand(1)) && | |||
38695 | DAG.getTargetLoweringInfo().isTypeLegal( | |||
38696 | Src.getOperand(0).getValueType())) | |||
38697 | return DAG.getNode(X86ISD::VBROADCAST, DL, VT, Src.getOperand(0)); | |||
38698 | ||||
38699 | // Share broadcast with the longest vector and extract low subvector (free). | |||
38700 | // Ensure the same SDValue from the SDNode use is being used. | |||
38701 | for (SDNode *User : Src->uses()) | |||
38702 | if (User != N.getNode() && User->getOpcode() == X86ISD::VBROADCAST && | |||
38703 | Src == User->getOperand(0) && | |||
38704 | User->getValueSizeInBits(0).getFixedSize() > | |||
38705 | VT.getFixedSizeInBits()) { | |||
38706 | return extractSubVector(SDValue(User, 0), 0, DAG, DL, | |||
38707 | VT.getSizeInBits()); | |||
38708 | } | |||
38709 | ||||
38710 | // vbroadcast(scalarload X) -> vbroadcast_load X | |||
38711 | // For float loads, extract other uses of the scalar from the broadcast. | |||
38712 | if (!SrcVT.isVector() && (Src.hasOneUse() || VT.isFloatingPoint()) && | |||
38713 | ISD::isNormalLoad(Src.getNode())) { | |||
38714 | LoadSDNode *LN = cast<LoadSDNode>(Src); | |||
38715 | SDVTList Tys = DAG.getVTList(VT, MVT::Other); | |||
38716 | SDValue Ops[] = { LN->getChain(), LN->getBasePtr() }; | |||
38717 | SDValue BcastLd = | |||
38718 | DAG.getMemIntrinsicNode(X86ISD::VBROADCAST_LOAD, DL, Tys, Ops, | |||
38719 | LN->getMemoryVT(), LN->getMemOperand()); | |||
38720 | // If the load value is used only by N, replace it via CombineTo N. | |||
38721 | bool NoReplaceExtract = Src.hasOneUse(); | |||
38722 | DCI.CombineTo(N.getNode(), BcastLd); | |||
38723 | if (NoReplaceExtract) { | |||
38724 | DAG.ReplaceAllUsesOfValueWith(SDValue(LN, 1), BcastLd.getValue(1)); | |||
38725 | DCI.recursivelyDeleteUnusedNodes(LN); | |||
38726 | } else { | |||
38727 | SDValue Scl = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, SrcVT, BcastLd, | |||
38728 | DAG.getIntPtrConstant(0, DL)); | |||
38729 | DCI.CombineTo(LN, Scl, BcastLd.getValue(1)); | |||
38730 | } | |||
38731 | return N; // Return N so it doesn't get rechecked! | |||
38732 | } | |||
38733 | ||||
38734 | // Due to isTypeDesirableForOp, we won't always shrink a load truncated to | |||
38735 | // i16. So shrink it ourselves if we can make a broadcast_load. | |||
38736 | if (SrcVT == MVT::i16 && Src.getOpcode() == ISD::TRUNCATE && | |||
38737 | Src.hasOneUse() && Src.getOperand(0).hasOneUse()) { | |||
38738 | assert(Subtarget.hasAVX2() && "Expected AVX2")(static_cast <bool> (Subtarget.hasAVX2() && "Expected AVX2" ) ? void (0) : __assert_fail ("Subtarget.hasAVX2() && \"Expected AVX2\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 38738, __extension__ __PRETTY_FUNCTION__)); | |||
38739 | SDValue TruncIn = Src.getOperand(0); | |||
38740 | ||||
38741 | // If this is a truncate of a non extending load we can just narrow it to | |||
38742 | // use a broadcast_load. | |||
38743 | if (ISD::isNormalLoad(TruncIn.getNode())) { | |||
38744 | LoadSDNode *LN = cast<LoadSDNode>(TruncIn); | |||
38745 | // Unless its volatile or atomic. | |||
38746 | if (LN->isSimple()) { | |||
38747 | SDVTList Tys = DAG.getVTList(VT, MVT::Other); | |||
38748 | SDValue Ops[] = { LN->getChain(), LN->getBasePtr() }; | |||
38749 | SDValue BcastLd = DAG.getMemIntrinsicNode( | |||
38750 | X86ISD::VBROADCAST_LOAD, DL, Tys, Ops, MVT::i16, | |||
38751 | LN->getPointerInfo(), LN->getOriginalAlign(), | |||
38752 | LN->getMemOperand()->getFlags()); | |||
38753 | DCI.CombineTo(N.getNode(), BcastLd); | |||
38754 | DAG.ReplaceAllUsesOfValueWith(SDValue(LN, 1), BcastLd.getValue(1)); | |||
38755 | DCI.recursivelyDeleteUnusedNodes(Src.getNode()); | |||
38756 | return N; // Return N so it doesn't get rechecked! | |||
38757 | } | |||
38758 | } | |||
38759 | ||||
38760 | // If this is a truncate of an i16 extload, we can directly replace it. | |||
38761 | if (ISD::isUNINDEXEDLoad(Src.getOperand(0).getNode()) && | |||
38762 | ISD::isEXTLoad(Src.getOperand(0).getNode())) { | |||
38763 | LoadSDNode *LN = cast<LoadSDNode>(Src.getOperand(0)); | |||
38764 | if (LN->getMemoryVT().getSizeInBits() == 16) { | |||
38765 | SDVTList Tys = DAG.getVTList(VT, MVT::Other); | |||
38766 | SDValue Ops[] = { LN->getChain(), LN->getBasePtr() }; | |||
38767 | SDValue BcastLd = | |||
38768 | DAG.getMemIntrinsicNode(X86ISD::VBROADCAST_LOAD, DL, Tys, Ops, | |||
38769 | LN->getMemoryVT(), LN->getMemOperand()); | |||
38770 | DCI.CombineTo(N.getNode(), BcastLd); | |||
38771 | DAG.ReplaceAllUsesOfValueWith(SDValue(LN, 1), BcastLd.getValue(1)); | |||
38772 | DCI.recursivelyDeleteUnusedNodes(Src.getNode()); | |||
38773 | return N; // Return N so it doesn't get rechecked! | |||
38774 | } | |||
38775 | } | |||
38776 | ||||
38777 | // If this is a truncate of load that has been shifted right, we can | |||
38778 | // offset the pointer and use a narrower load. | |||
38779 | if (TruncIn.getOpcode() == ISD::SRL && | |||
38780 | TruncIn.getOperand(0).hasOneUse() && | |||
38781 | isa<ConstantSDNode>(TruncIn.getOperand(1)) && | |||
38782 | ISD::isNormalLoad(TruncIn.getOperand(0).getNode())) { | |||
38783 | LoadSDNode *LN = cast<LoadSDNode>(TruncIn.getOperand(0)); | |||
38784 | unsigned ShiftAmt = TruncIn.getConstantOperandVal(1); | |||
38785 | // Make sure the shift amount and the load size are divisible by 16. | |||
38786 | // Don't do this if the load is volatile or atomic. | |||
38787 | if (ShiftAmt % 16 == 0 && TruncIn.getValueSizeInBits() % 16 == 0 && | |||
38788 | LN->isSimple()) { | |||
38789 | unsigned Offset = ShiftAmt / 8; | |||
38790 | SDVTList Tys = DAG.getVTList(VT, MVT::Other); | |||
38791 | SDValue Ptr = DAG.getMemBasePlusOffset(LN->getBasePtr(), | |||
38792 | TypeSize::Fixed(Offset), DL); | |||
38793 | SDValue Ops[] = { LN->getChain(), Ptr }; | |||
38794 | SDValue BcastLd = DAG.getMemIntrinsicNode( | |||
38795 | X86ISD::VBROADCAST_LOAD, DL, Tys, Ops, MVT::i16, | |||
38796 | LN->getPointerInfo().getWithOffset(Offset), | |||
38797 | LN->getOriginalAlign(), | |||
38798 | LN->getMemOperand()->getFlags()); | |||
38799 | DCI.CombineTo(N.getNode(), BcastLd); | |||
38800 | DAG.ReplaceAllUsesOfValueWith(SDValue(LN, 1), BcastLd.getValue(1)); | |||
38801 | DCI.recursivelyDeleteUnusedNodes(Src.getNode()); | |||
38802 | return N; // Return N so it doesn't get rechecked! | |||
38803 | } | |||
38804 | } | |||
38805 | } | |||
38806 | ||||
38807 | // vbroadcast(vzload X) -> vbroadcast_load X | |||
38808 | if (Src.getOpcode() == X86ISD::VZEXT_LOAD && Src.hasOneUse()) { | |||
38809 | MemSDNode *LN = cast<MemIntrinsicSDNode>(Src); | |||
38810 | if (LN->getMemoryVT().getSizeInBits() == VT.getScalarSizeInBits()) { | |||
38811 | SDVTList Tys = DAG.getVTList(VT, MVT::Other); | |||
38812 | SDValue Ops[] = { LN->getChain(), LN->getBasePtr() }; | |||
38813 | SDValue BcastLd = | |||
38814 | DAG.getMemIntrinsicNode(X86ISD::VBROADCAST_LOAD, DL, Tys, Ops, | |||
38815 | LN->getMemoryVT(), LN->getMemOperand()); | |||
38816 | DCI.CombineTo(N.getNode(), BcastLd); | |||
38817 | DAG.ReplaceAllUsesOfValueWith(SDValue(LN, 1), BcastLd.getValue(1)); | |||
38818 | DCI.recursivelyDeleteUnusedNodes(LN); | |||
38819 | return N; // Return N so it doesn't get rechecked! | |||
38820 | } | |||
38821 | } | |||
38822 | ||||
38823 | // vbroadcast(vector load X) -> vbroadcast_load | |||
38824 | if ((SrcVT == MVT::v2f64 || SrcVT == MVT::v4f32 || SrcVT == MVT::v2i64 || | |||
38825 | SrcVT == MVT::v4i32) && | |||
38826 | Src.hasOneUse() && ISD::isNormalLoad(Src.getNode())) { | |||
38827 | LoadSDNode *LN = cast<LoadSDNode>(Src); | |||
38828 | // Unless the load is volatile or atomic. | |||
38829 | if (LN->isSimple()) { | |||
38830 | SDVTList Tys = DAG.getVTList(VT, MVT::Other); | |||
38831 | SDValue Ops[] = {LN->getChain(), LN->getBasePtr()}; | |||
38832 | SDValue BcastLd = DAG.getMemIntrinsicNode( | |||
38833 | X86ISD::VBROADCAST_LOAD, DL, Tys, Ops, SrcVT.getScalarType(), | |||
38834 | LN->getPointerInfo(), LN->getOriginalAlign(), | |||
38835 | LN->getMemOperand()->getFlags()); | |||
38836 | DCI.CombineTo(N.getNode(), BcastLd); | |||
38837 | DAG.ReplaceAllUsesOfValueWith(SDValue(LN, 1), BcastLd.getValue(1)); | |||
38838 | DCI.recursivelyDeleteUnusedNodes(LN); | |||
38839 | return N; // Return N so it doesn't get rechecked! | |||
38840 | } | |||
38841 | } | |||
38842 | ||||
38843 | return SDValue(); | |||
38844 | } | |||
38845 | case X86ISD::VZEXT_MOVL: { | |||
38846 | SDValue N0 = N.getOperand(0); | |||
38847 | ||||
38848 | // If this a vzmovl of a full vector load, replace it with a vzload, unless | |||
38849 | // the load is volatile. | |||
38850 | if (N0.hasOneUse() && ISD::isNormalLoad(N0.getNode())) { | |||
38851 | auto *LN = cast<LoadSDNode>(N0); | |||
38852 | if (SDValue VZLoad = | |||
38853 | narrowLoadToVZLoad(LN, VT.getVectorElementType(), VT, DAG)) { | |||
38854 | DCI.CombineTo(N.getNode(), VZLoad); | |||
38855 | DAG.ReplaceAllUsesOfValueWith(SDValue(LN, 1), VZLoad.getValue(1)); | |||
38856 | DCI.recursivelyDeleteUnusedNodes(LN); | |||
38857 | return N; | |||
38858 | } | |||
38859 | } | |||
38860 | ||||
38861 | // If this a VZEXT_MOVL of a VBROADCAST_LOAD, we don't need the broadcast | |||
38862 | // and can just use a VZEXT_LOAD. | |||
38863 | // FIXME: Is there some way to do this with SimplifyDemandedVectorElts? | |||
38864 | if (N0.hasOneUse() && N0.getOpcode() == X86ISD::VBROADCAST_LOAD) { | |||
38865 | auto *LN = cast<MemSDNode>(N0); | |||
38866 | if (VT.getScalarSizeInBits() == LN->getMemoryVT().getSizeInBits()) { | |||
38867 | SDVTList Tys = DAG.getVTList(VT, MVT::Other); | |||
38868 | SDValue Ops[] = {LN->getChain(), LN->getBasePtr()}; | |||
38869 | SDValue VZLoad = | |||
38870 | DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, | |||
38871 | LN->getMemoryVT(), LN->getMemOperand()); | |||
38872 | DCI.CombineTo(N.getNode(), VZLoad); | |||
38873 | DAG.ReplaceAllUsesOfValueWith(SDValue(LN, 1), VZLoad.getValue(1)); | |||
38874 | DCI.recursivelyDeleteUnusedNodes(LN); | |||
38875 | return N; | |||
38876 | } | |||
38877 | } | |||
38878 | ||||
38879 | // Turn (v2i64 (vzext_movl (scalar_to_vector (i64 X)))) into | |||
38880 | // (v2i64 (bitcast (v4i32 (vzext_movl (scalar_to_vector (i32 (trunc X))))))) | |||
38881 | // if the upper bits of the i64 are zero. | |||
38882 | if (N0.hasOneUse() && N0.getOpcode() == ISD::SCALAR_TO_VECTOR && | |||
38883 | N0.getOperand(0).hasOneUse() && | |||
38884 | N0.getOperand(0).getValueType() == MVT::i64) { | |||
38885 | SDValue In = N0.getOperand(0); | |||
38886 | APInt Mask = APInt::getHighBitsSet(64, 32); | |||
38887 | if (DAG.MaskedValueIsZero(In, Mask)) { | |||
38888 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, In); | |||
38889 | MVT VecVT = MVT::getVectorVT(MVT::i32, VT.getVectorNumElements() * 2); | |||
38890 | SDValue SclVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VecVT, Trunc); | |||
38891 | SDValue Movl = DAG.getNode(X86ISD::VZEXT_MOVL, DL, VecVT, SclVec); | |||
38892 | return DAG.getBitcast(VT, Movl); | |||
38893 | } | |||
38894 | } | |||
38895 | ||||
38896 | // Load a scalar integer constant directly to XMM instead of transferring an | |||
38897 | // immediate value from GPR. | |||
38898 | // vzext_movl (scalar_to_vector C) --> load [C,0...] | |||
38899 | if (N0.getOpcode() == ISD::SCALAR_TO_VECTOR) { | |||
38900 | if (auto *C = dyn_cast<ConstantSDNode>(N0.getOperand(0))) { | |||
38901 | // Create a vector constant - scalar constant followed by zeros. | |||
38902 | EVT ScalarVT = N0.getOperand(0).getValueType(); | |||
38903 | Type *ScalarTy = ScalarVT.getTypeForEVT(*DAG.getContext()); | |||
38904 | unsigned NumElts = VT.getVectorNumElements(); | |||
38905 | Constant *Zero = ConstantInt::getNullValue(ScalarTy); | |||
38906 | SmallVector<Constant *, 32> ConstantVec(NumElts, Zero); | |||
38907 | ConstantVec[0] = const_cast<ConstantInt *>(C->getConstantIntValue()); | |||
38908 | ||||
38909 | // Load the vector constant from constant pool. | |||
38910 | MVT PVT = DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout()); | |||
38911 | SDValue CP = DAG.getConstantPool(ConstantVector::get(ConstantVec), PVT); | |||
38912 | MachinePointerInfo MPI = | |||
38913 | MachinePointerInfo::getConstantPool(DAG.getMachineFunction()); | |||
38914 | Align Alignment = cast<ConstantPoolSDNode>(CP)->getAlign(); | |||
38915 | return DAG.getLoad(VT, DL, DAG.getEntryNode(), CP, MPI, Alignment, | |||
38916 | MachineMemOperand::MOLoad); | |||
38917 | } | |||
38918 | } | |||
38919 | ||||
38920 | // Pull subvector inserts into undef through VZEXT_MOVL by making it an | |||
38921 | // insert into a zero vector. This helps get VZEXT_MOVL closer to | |||
38922 | // scalar_to_vectors where 256/512 are canonicalized to an insert and a | |||
38923 | // 128-bit scalar_to_vector. This reduces the number of isel patterns. | |||
38924 | if (!DCI.isBeforeLegalizeOps() && N0.hasOneUse()) { | |||
38925 | SDValue V = peekThroughOneUseBitcasts(N0); | |||
38926 | ||||
38927 | if (V.getOpcode() == ISD::INSERT_SUBVECTOR && V.getOperand(0).isUndef() && | |||
38928 | isNullConstant(V.getOperand(2))) { | |||
38929 | SDValue In = V.getOperand(1); | |||
38930 | MVT SubVT = MVT::getVectorVT(VT.getVectorElementType(), | |||
38931 | In.getValueSizeInBits() / | |||
38932 | VT.getScalarSizeInBits()); | |||
38933 | In = DAG.getBitcast(SubVT, In); | |||
38934 | SDValue Movl = DAG.getNode(X86ISD::VZEXT_MOVL, DL, SubVT, In); | |||
38935 | return DAG.getNode(ISD::INSERT_SUBVECTOR, DL, VT, | |||
38936 | getZeroVector(VT, Subtarget, DAG, DL), Movl, | |||
38937 | V.getOperand(2)); | |||
38938 | } | |||
38939 | } | |||
38940 | ||||
38941 | return SDValue(); | |||
38942 | } | |||
38943 | case X86ISD::BLENDI: { | |||
38944 | SDValue N0 = N.getOperand(0); | |||
38945 | SDValue N1 = N.getOperand(1); | |||
38946 | ||||
38947 | // blend(bitcast(x),bitcast(y)) -> bitcast(blend(x,y)) to narrower types. | |||
38948 | // TODO: Handle MVT::v16i16 repeated blend mask. | |||
38949 | if (N0.getOpcode() == ISD::BITCAST && N1.getOpcode() == ISD::BITCAST && | |||
38950 | N0.getOperand(0).getValueType() == N1.getOperand(0).getValueType()) { | |||
38951 | MVT SrcVT = N0.getOperand(0).getSimpleValueType(); | |||
38952 | if ((VT.getScalarSizeInBits() % SrcVT.getScalarSizeInBits()) == 0 && | |||
38953 | SrcVT.getScalarSizeInBits() >= 32) { | |||
38954 | unsigned BlendMask = N.getConstantOperandVal(2); | |||
38955 | unsigned Size = VT.getVectorNumElements(); | |||
38956 | unsigned Scale = VT.getScalarSizeInBits() / SrcVT.getScalarSizeInBits(); | |||
38957 | BlendMask = scaleVectorShuffleBlendMask(BlendMask, Size, Scale); | |||
38958 | return DAG.getBitcast( | |||
38959 | VT, DAG.getNode(X86ISD::BLENDI, DL, SrcVT, N0.getOperand(0), | |||
38960 | N1.getOperand(0), | |||
38961 | DAG.getTargetConstant(BlendMask, DL, MVT::i8))); | |||
38962 | } | |||
38963 | } | |||
38964 | return SDValue(); | |||
38965 | } | |||
38966 | case X86ISD::SHUFP: { | |||
38967 | // Fold shufps(shuffle(x),shuffle(y)) -> shufps(x,y). | |||
38968 | // This is a more relaxed shuffle combiner that can ignore oneuse limits. | |||
38969 | // TODO: Support types other than v4f32. | |||
38970 | if (VT == MVT::v4f32) { | |||
38971 | bool Updated = false; | |||
38972 | SmallVector<int> Mask; | |||
38973 | SmallVector<SDValue> Ops; | |||
38974 | if (getTargetShuffleMask(N.getNode(), VT, false, Ops, Mask) && | |||
38975 | Ops.size() == 2) { | |||
38976 | for (int i = 0; i != 2; ++i) { | |||
38977 | SmallVector<SDValue> SubOps; | |||
38978 | SmallVector<int> SubMask, SubScaledMask; | |||
38979 | SDValue Sub = peekThroughBitcasts(Ops[i]); | |||
38980 | // TODO: Scaling might be easier if we specify the demanded elts. | |||
38981 | if (getTargetShuffleInputs(Sub, SubOps, SubMask, DAG, 0, false) && | |||
38982 | scaleShuffleElements(SubMask, 4, SubScaledMask) && | |||
38983 | SubOps.size() == 1 && isUndefOrInRange(SubScaledMask, 0, 4)) { | |||
38984 | int Ofs = i * 2; | |||
38985 | Mask[Ofs + 0] = SubScaledMask[Mask[Ofs + 0] % 4] + (i * 4); | |||
38986 | Mask[Ofs + 1] = SubScaledMask[Mask[Ofs + 1] % 4] + (i * 4); | |||
38987 | Ops[i] = DAG.getBitcast(VT, SubOps[0]); | |||
38988 | Updated = true; | |||
38989 | } | |||
38990 | } | |||
38991 | } | |||
38992 | if (Updated) { | |||
38993 | for (int &M : Mask) | |||
38994 | M %= 4; | |||
38995 | Ops.push_back(getV4X86ShuffleImm8ForMask(Mask, DL, DAG)); | |||
38996 | return DAG.getNode(X86ISD::SHUFP, DL, VT, Ops); | |||
38997 | } | |||
38998 | } | |||
38999 | return SDValue(); | |||
39000 | } | |||
39001 | case X86ISD::VPERMI: { | |||
39002 | // vpermi(bitcast(x)) -> bitcast(vpermi(x)) for same number of elements. | |||
39003 | // TODO: Remove when we have preferred domains in combineX86ShuffleChain. | |||
39004 | SDValue N0 = N.getOperand(0); | |||
39005 | SDValue N1 = N.getOperand(1); | |||
39006 | unsigned EltSizeInBits = VT.getScalarSizeInBits(); | |||
39007 | if (N0.getOpcode() == ISD::BITCAST && | |||
39008 | N0.getOperand(0).getScalarValueSizeInBits() == EltSizeInBits) { | |||
39009 | SDValue Src = N0.getOperand(0); | |||
39010 | EVT SrcVT = Src.getValueType(); | |||
39011 | SDValue Res = DAG.getNode(X86ISD::VPERMI, DL, SrcVT, Src, N1); | |||
39012 | return DAG.getBitcast(VT, Res); | |||
39013 | } | |||
39014 | return SDValue(); | |||
39015 | } | |||
39016 | case X86ISD::VPERM2X128: { | |||
39017 | // Fold vperm2x128(bitcast(x),bitcast(y),c) -> bitcast(vperm2x128(x,y,c)). | |||
39018 | SDValue LHS = N->getOperand(0); | |||
39019 | SDValue RHS = N->getOperand(1); | |||
39020 | if (LHS.getOpcode() == ISD::BITCAST && | |||
39021 | (RHS.getOpcode() == ISD::BITCAST || RHS.isUndef())) { | |||
39022 | EVT SrcVT = LHS.getOperand(0).getValueType(); | |||
39023 | if (RHS.isUndef() || SrcVT == RHS.getOperand(0).getValueType()) { | |||
39024 | return DAG.getBitcast(VT, DAG.getNode(X86ISD::VPERM2X128, DL, SrcVT, | |||
39025 | DAG.getBitcast(SrcVT, LHS), | |||
39026 | DAG.getBitcast(SrcVT, RHS), | |||
39027 | N->getOperand(2))); | |||
39028 | } | |||
39029 | } | |||
39030 | ||||
39031 | // Fold vperm2x128(op(),op()) -> op(vperm2x128(),vperm2x128()). | |||
39032 | if (SDValue Res = canonicalizeLaneShuffleWithRepeatedOps(N, DAG, DL)) | |||
39033 | return Res; | |||
39034 | ||||
39035 | // Fold vperm2x128 subvector shuffle with an inner concat pattern. | |||
39036 | // vperm2x128(concat(X,Y),concat(Z,W)) --> concat X,Y etc. | |||
39037 | auto FindSubVector128 = [&](unsigned Idx) { | |||
39038 | if (Idx > 3) | |||
39039 | return SDValue(); | |||
39040 | SDValue Src = peekThroughBitcasts(N.getOperand(Idx < 2 ? 0 : 1)); | |||
39041 | SmallVector<SDValue> SubOps; | |||
39042 | if (collectConcatOps(Src.getNode(), SubOps) && SubOps.size() == 2) | |||
39043 | return SubOps[Idx & 1]; | |||
39044 | unsigned NumElts = Src.getValueType().getVectorNumElements(); | |||
39045 | if ((Idx & 1) == 1 && Src.getOpcode() == ISD::INSERT_SUBVECTOR && | |||
39046 | Src.getOperand(1).getValueSizeInBits() == 128 && | |||
39047 | Src.getConstantOperandAPInt(2) == (NumElts / 2)) { | |||
39048 | return Src.getOperand(1); | |||
39049 | } | |||
39050 | return SDValue(); | |||
39051 | }; | |||
39052 | unsigned Imm = N.getConstantOperandVal(2); | |||
39053 | if (SDValue SubLo = FindSubVector128(Imm & 0x0F)) { | |||
39054 | if (SDValue SubHi = FindSubVector128((Imm & 0xF0) >> 4)) { | |||
39055 | MVT SubVT = VT.getHalfNumVectorElementsVT(); | |||
39056 | SubLo = DAG.getBitcast(SubVT, SubLo); | |||
39057 | SubHi = DAG.getBitcast(SubVT, SubHi); | |||
39058 | return DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, SubLo, SubHi); | |||
39059 | } | |||
39060 | } | |||
39061 | return SDValue(); | |||
39062 | } | |||
39063 | case X86ISD::PSHUFD: | |||
39064 | case X86ISD::PSHUFLW: | |||
39065 | case X86ISD::PSHUFHW: | |||
39066 | Mask = getPSHUFShuffleMask(N); | |||
39067 | assert(Mask.size() == 4)(static_cast <bool> (Mask.size() == 4) ? void (0) : __assert_fail ("Mask.size() == 4", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 39067, __extension__ __PRETTY_FUNCTION__)); | |||
39068 | break; | |||
39069 | case X86ISD::MOVSD: | |||
39070 | case X86ISD::MOVSH: | |||
39071 | case X86ISD::MOVSS: { | |||
39072 | SDValue N0 = N.getOperand(0); | |||
39073 | SDValue N1 = N.getOperand(1); | |||
39074 | ||||
39075 | // Canonicalize scalar FPOps: | |||
39076 | // MOVS*(N0, OP(N0, N1)) --> MOVS*(N0, SCALAR_TO_VECTOR(OP(N0[0], N1[0]))) | |||
39077 | // If commutable, allow OP(N1[0], N0[0]). | |||
39078 | unsigned Opcode1 = N1.getOpcode(); | |||
39079 | if (Opcode1 == ISD::FADD || Opcode1 == ISD::FMUL || Opcode1 == ISD::FSUB || | |||
39080 | Opcode1 == ISD::FDIV) { | |||
39081 | SDValue N10 = N1.getOperand(0); | |||
39082 | SDValue N11 = N1.getOperand(1); | |||
39083 | if (N10 == N0 || | |||
39084 | (N11 == N0 && (Opcode1 == ISD::FADD || Opcode1 == ISD::FMUL))) { | |||
39085 | if (N10 != N0) | |||
39086 | std::swap(N10, N11); | |||
39087 | MVT SVT = VT.getVectorElementType(); | |||
39088 | SDValue ZeroIdx = DAG.getIntPtrConstant(0, DL); | |||
39089 | N10 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, SVT, N10, ZeroIdx); | |||
39090 | N11 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, SVT, N11, ZeroIdx); | |||
39091 | SDValue Scl = DAG.getNode(Opcode1, DL, SVT, N10, N11); | |||
39092 | SDValue SclVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, Scl); | |||
39093 | return DAG.getNode(Opcode, DL, VT, N0, SclVec); | |||
39094 | } | |||
39095 | } | |||
39096 | ||||
39097 | return SDValue(); | |||
39098 | } | |||
39099 | case X86ISD::INSERTPS: { | |||
39100 | assert(VT == MVT::v4f32 && "INSERTPS ValueType must be MVT::v4f32")(static_cast <bool> (VT == MVT::v4f32 && "INSERTPS ValueType must be MVT::v4f32" ) ? void (0) : __assert_fail ("VT == MVT::v4f32 && \"INSERTPS ValueType must be MVT::v4f32\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 39100, __extension__ __PRETTY_FUNCTION__)); | |||
39101 | SDValue Op0 = N.getOperand(0); | |||
39102 | SDValue Op1 = N.getOperand(1); | |||
39103 | unsigned InsertPSMask = N.getConstantOperandVal(2); | |||
39104 | unsigned SrcIdx = (InsertPSMask >> 6) & 0x3; | |||
39105 | unsigned DstIdx = (InsertPSMask >> 4) & 0x3; | |||
39106 | unsigned ZeroMask = InsertPSMask & 0xF; | |||
39107 | ||||
39108 | // If we zero out all elements from Op0 then we don't need to reference it. | |||
39109 | if (((ZeroMask | (1u << DstIdx)) == 0xF) && !Op0.isUndef()) | |||
39110 | return DAG.getNode(X86ISD::INSERTPS, DL, VT, DAG.getUNDEF(VT), Op1, | |||
39111 | DAG.getTargetConstant(InsertPSMask, DL, MVT::i8)); | |||
39112 | ||||
39113 | // If we zero out the element from Op1 then we don't need to reference it. | |||
39114 | if ((ZeroMask & (1u << DstIdx)) && !Op1.isUndef()) | |||
39115 | return DAG.getNode(X86ISD::INSERTPS, DL, VT, Op0, DAG.getUNDEF(VT), | |||
39116 | DAG.getTargetConstant(InsertPSMask, DL, MVT::i8)); | |||
39117 | ||||
39118 | // Attempt to merge insertps Op1 with an inner target shuffle node. | |||
39119 | SmallVector<int, 8> TargetMask1; | |||
39120 | SmallVector<SDValue, 2> Ops1; | |||
39121 | APInt KnownUndef1, KnownZero1; | |||
39122 | if (getTargetShuffleAndZeroables(Op1, TargetMask1, Ops1, KnownUndef1, | |||
39123 | KnownZero1)) { | |||
39124 | if (KnownUndef1[SrcIdx] || KnownZero1[SrcIdx]) { | |||
39125 | // Zero/UNDEF insertion - zero out element and remove dependency. | |||
39126 | InsertPSMask |= (1u << DstIdx); | |||
39127 | return DAG.getNode(X86ISD::INSERTPS, DL, VT, Op0, DAG.getUNDEF(VT), | |||
39128 | DAG.getTargetConstant(InsertPSMask, DL, MVT::i8)); | |||
39129 | } | |||
39130 | // Update insertps mask srcidx and reference the source input directly. | |||
39131 | int M = TargetMask1[SrcIdx]; | |||
39132 | assert(0 <= M && M < 8 && "Shuffle index out of range")(static_cast <bool> (0 <= M && M < 8 && "Shuffle index out of range") ? void (0) : __assert_fail ("0 <= M && M < 8 && \"Shuffle index out of range\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 39132, __extension__ __PRETTY_FUNCTION__)); | |||
39133 | InsertPSMask = (InsertPSMask & 0x3f) | ((M & 0x3) << 6); | |||
39134 | Op1 = Ops1[M < 4 ? 0 : 1]; | |||
39135 | return DAG.getNode(X86ISD::INSERTPS, DL, VT, Op0, Op1, | |||
39136 | DAG.getTargetConstant(InsertPSMask, DL, MVT::i8)); | |||
39137 | } | |||
39138 | ||||
39139 | // Attempt to merge insertps Op0 with an inner target shuffle node. | |||
39140 | SmallVector<int, 8> TargetMask0; | |||
39141 | SmallVector<SDValue, 2> Ops0; | |||
39142 | APInt KnownUndef0, KnownZero0; | |||
39143 | if (getTargetShuffleAndZeroables(Op0, TargetMask0, Ops0, KnownUndef0, | |||
39144 | KnownZero0)) { | |||
39145 | bool Updated = false; | |||
39146 | bool UseInput00 = false; | |||
39147 | bool UseInput01 = false; | |||
39148 | for (int i = 0; i != 4; ++i) { | |||
39149 | if ((InsertPSMask & (1u << i)) || (i == (int)DstIdx)) { | |||
39150 | // No change if element is already zero or the inserted element. | |||
39151 | continue; | |||
39152 | } else if (KnownUndef0[i] || KnownZero0[i]) { | |||
39153 | // If the target mask is undef/zero then we must zero the element. | |||
39154 | InsertPSMask |= (1u << i); | |||
39155 | Updated = true; | |||
39156 | continue; | |||
39157 | } | |||
39158 | ||||
39159 | // The input vector element must be inline. | |||
39160 | int M = TargetMask0[i]; | |||
39161 | if (M != i && M != (i + 4)) | |||
39162 | return SDValue(); | |||
39163 | ||||
39164 | // Determine which inputs of the target shuffle we're using. | |||
39165 | UseInput00 |= (0 <= M && M < 4); | |||
39166 | UseInput01 |= (4 <= M); | |||
39167 | } | |||
39168 | ||||
39169 | // If we're not using both inputs of the target shuffle then use the | |||
39170 | // referenced input directly. | |||
39171 | if (UseInput00 && !UseInput01) { | |||
39172 | Updated = true; | |||
39173 | Op0 = Ops0[0]; | |||
39174 | } else if (!UseInput00 && UseInput01) { | |||
39175 | Updated = true; | |||
39176 | Op0 = Ops0[1]; | |||
39177 | } | |||
39178 | ||||
39179 | if (Updated) | |||
39180 | return DAG.getNode(X86ISD::INSERTPS, DL, VT, Op0, Op1, | |||
39181 | DAG.getTargetConstant(InsertPSMask, DL, MVT::i8)); | |||
39182 | } | |||
39183 | ||||
39184 | // If we're inserting an element from a vbroadcast load, fold the | |||
39185 | // load into the X86insertps instruction. We need to convert the scalar | |||
39186 | // load to a vector and clear the source lane of the INSERTPS control. | |||
39187 | if (Op1.getOpcode() == X86ISD::VBROADCAST_LOAD && Op1.hasOneUse()) { | |||
39188 | auto *MemIntr = cast<MemIntrinsicSDNode>(Op1); | |||
39189 | if (MemIntr->getMemoryVT().getScalarSizeInBits() == 32) { | |||
39190 | SDValue Load = DAG.getLoad(MVT::f32, DL, MemIntr->getChain(), | |||
39191 | MemIntr->getBasePtr(), | |||
39192 | MemIntr->getMemOperand()); | |||
39193 | SDValue Insert = DAG.getNode(X86ISD::INSERTPS, DL, VT, Op0, | |||
39194 | DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, | |||
39195 | Load), | |||
39196 | DAG.getTargetConstant(InsertPSMask & 0x3f, DL, MVT::i8)); | |||
39197 | DAG.ReplaceAllUsesOfValueWith(SDValue(MemIntr, 1), Load.getValue(1)); | |||
39198 | return Insert; | |||
39199 | } | |||
39200 | } | |||
39201 | ||||
39202 | return SDValue(); | |||
39203 | } | |||
39204 | default: | |||
39205 | return SDValue(); | |||
39206 | } | |||
39207 | ||||
39208 | // Nuke no-op shuffles that show up after combining. | |||
39209 | if (isNoopShuffleMask(Mask)) | |||
39210 | return N.getOperand(0); | |||
39211 | ||||
39212 | // Look for simplifications involving one or two shuffle instructions. | |||
39213 | SDValue V = N.getOperand(0); | |||
39214 | switch (N.getOpcode()) { | |||
39215 | default: | |||
39216 | break; | |||
39217 | case X86ISD::PSHUFLW: | |||
39218 | case X86ISD::PSHUFHW: | |||
39219 | assert(VT.getVectorElementType() == MVT::i16 && "Bad word shuffle type!")(static_cast <bool> (VT.getVectorElementType() == MVT:: i16 && "Bad word shuffle type!") ? void (0) : __assert_fail ("VT.getVectorElementType() == MVT::i16 && \"Bad word shuffle type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 39219, __extension__ __PRETTY_FUNCTION__)); | |||
39220 | ||||
39221 | // See if this reduces to a PSHUFD which is no more expensive and can | |||
39222 | // combine with more operations. Note that it has to at least flip the | |||
39223 | // dwords as otherwise it would have been removed as a no-op. | |||
39224 | if (makeArrayRef(Mask).equals({2, 3, 0, 1})) { | |||
39225 | int DMask[] = {0, 1, 2, 3}; | |||
39226 | int DOffset = N.getOpcode() == X86ISD::PSHUFLW ? 0 : 2; | |||
39227 | DMask[DOffset + 0] = DOffset + 1; | |||
39228 | DMask[DOffset + 1] = DOffset + 0; | |||
39229 | MVT DVT = MVT::getVectorVT(MVT::i32, VT.getVectorNumElements() / 2); | |||
39230 | V = DAG.getBitcast(DVT, V); | |||
39231 | V = DAG.getNode(X86ISD::PSHUFD, DL, DVT, V, | |||
39232 | getV4X86ShuffleImm8ForMask(DMask, DL, DAG)); | |||
39233 | return DAG.getBitcast(VT, V); | |||
39234 | } | |||
39235 | ||||
39236 | // Look for shuffle patterns which can be implemented as a single unpack. | |||
39237 | // FIXME: This doesn't handle the location of the PSHUFD generically, and | |||
39238 | // only works when we have a PSHUFD followed by two half-shuffles. | |||
39239 | if (Mask[0] == Mask[1] && Mask[2] == Mask[3] && | |||
39240 | (V.getOpcode() == X86ISD::PSHUFLW || | |||
39241 | V.getOpcode() == X86ISD::PSHUFHW) && | |||
39242 | V.getOpcode() != N.getOpcode() && | |||
39243 | V.hasOneUse() && V.getOperand(0).hasOneUse()) { | |||
39244 | SDValue D = peekThroughOneUseBitcasts(V.getOperand(0)); | |||
39245 | if (D.getOpcode() == X86ISD::PSHUFD) { | |||
39246 | SmallVector<int, 4> VMask = getPSHUFShuffleMask(V); | |||
39247 | SmallVector<int, 4> DMask = getPSHUFShuffleMask(D); | |||
39248 | int NOffset = N.getOpcode() == X86ISD::PSHUFLW ? 0 : 4; | |||
39249 | int VOffset = V.getOpcode() == X86ISD::PSHUFLW ? 0 : 4; | |||
39250 | int WordMask[8]; | |||
39251 | for (int i = 0; i < 4; ++i) { | |||
39252 | WordMask[i + NOffset] = Mask[i] + NOffset; | |||
39253 | WordMask[i + VOffset] = VMask[i] + VOffset; | |||
39254 | } | |||
39255 | // Map the word mask through the DWord mask. | |||
39256 | int MappedMask[8]; | |||
39257 | for (int i = 0; i < 8; ++i) | |||
39258 | MappedMask[i] = 2 * DMask[WordMask[i] / 2] + WordMask[i] % 2; | |||
39259 | if (makeArrayRef(MappedMask).equals({0, 0, 1, 1, 2, 2, 3, 3}) || | |||
39260 | makeArrayRef(MappedMask).equals({4, 4, 5, 5, 6, 6, 7, 7})) { | |||
39261 | // We can replace all three shuffles with an unpack. | |||
39262 | V = DAG.getBitcast(VT, D.getOperand(0)); | |||
39263 | return DAG.getNode(MappedMask[0] == 0 ? X86ISD::UNPCKL | |||
39264 | : X86ISD::UNPCKH, | |||
39265 | DL, VT, V, V); | |||
39266 | } | |||
39267 | } | |||
39268 | } | |||
39269 | ||||
39270 | break; | |||
39271 | ||||
39272 | case X86ISD::PSHUFD: | |||
39273 | if (SDValue NewN = combineRedundantDWordShuffle(N, Mask, DAG)) | |||
39274 | return NewN; | |||
39275 | ||||
39276 | break; | |||
39277 | } | |||
39278 | ||||
39279 | return SDValue(); | |||
39280 | } | |||
39281 | ||||
39282 | /// Checks if the shuffle mask takes subsequent elements | |||
39283 | /// alternately from two vectors. | |||
39284 | /// For example <0, 5, 2, 7> or <8, 1, 10, 3, 12, 5, 14, 7> are both correct. | |||
39285 | static bool isAddSubOrSubAddMask(ArrayRef<int> Mask, bool &Op0Even) { | |||
39286 | ||||
39287 | int ParitySrc[2] = {-1, -1}; | |||
39288 | unsigned Size = Mask.size(); | |||
39289 | for (unsigned i = 0; i != Size; ++i) { | |||
39290 | int M = Mask[i]; | |||
39291 | if (M < 0) | |||
39292 | continue; | |||
39293 | ||||
39294 | // Make sure we are using the matching element from the input. | |||
39295 | if ((M % Size) != i) | |||
39296 | return false; | |||
39297 | ||||
39298 | // Make sure we use the same input for all elements of the same parity. | |||
39299 | int Src = M / Size; | |||
39300 | if (ParitySrc[i % 2] >= 0 && ParitySrc[i % 2] != Src) | |||
39301 | return false; | |||
39302 | ParitySrc[i % 2] = Src; | |||
39303 | } | |||
39304 | ||||
39305 | // Make sure each input is used. | |||
39306 | if (ParitySrc[0] < 0 || ParitySrc[1] < 0 || ParitySrc[0] == ParitySrc[1]) | |||
39307 | return false; | |||
39308 | ||||
39309 | Op0Even = ParitySrc[0] == 0; | |||
39310 | return true; | |||
39311 | } | |||
39312 | ||||
39313 | /// Returns true iff the shuffle node \p N can be replaced with ADDSUB(SUBADD) | |||
39314 | /// operation. If true is returned then the operands of ADDSUB(SUBADD) operation | |||
39315 | /// are written to the parameters \p Opnd0 and \p Opnd1. | |||
39316 | /// | |||
39317 | /// We combine shuffle to ADDSUB(SUBADD) directly on the abstract vector shuffle nodes | |||
39318 | /// so it is easier to generically match. We also insert dummy vector shuffle | |||
39319 | /// nodes for the operands which explicitly discard the lanes which are unused | |||
39320 | /// by this operation to try to flow through the rest of the combiner | |||
39321 | /// the fact that they're unused. | |||
39322 | static bool isAddSubOrSubAdd(SDNode *N, const X86Subtarget &Subtarget, | |||
39323 | SelectionDAG &DAG, SDValue &Opnd0, SDValue &Opnd1, | |||
39324 | bool &IsSubAdd) { | |||
39325 | ||||
39326 | EVT VT = N->getValueType(0); | |||
39327 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
39328 | if (!Subtarget.hasSSE3() || !TLI.isTypeLegal(VT) || | |||
39329 | !VT.getSimpleVT().isFloatingPoint()) | |||
39330 | return false; | |||
39331 | ||||
39332 | // We only handle target-independent shuffles. | |||
39333 | // FIXME: It would be easy and harmless to use the target shuffle mask | |||
39334 | // extraction tool to support more. | |||
39335 | if (N->getOpcode() != ISD::VECTOR_SHUFFLE) | |||
39336 | return false; | |||
39337 | ||||
39338 | SDValue V1 = N->getOperand(0); | |||
39339 | SDValue V2 = N->getOperand(1); | |||
39340 | ||||
39341 | // Make sure we have an FADD and an FSUB. | |||
39342 | if ((V1.getOpcode() != ISD::FADD && V1.getOpcode() != ISD::FSUB) || | |||
39343 | (V2.getOpcode() != ISD::FADD && V2.getOpcode() != ISD::FSUB) || | |||
39344 | V1.getOpcode() == V2.getOpcode()) | |||
39345 | return false; | |||
39346 | ||||
39347 | // If there are other uses of these operations we can't fold them. | |||
39348 | if (!V1->hasOneUse() || !V2->hasOneUse()) | |||
39349 | return false; | |||
39350 | ||||
39351 | // Ensure that both operations have the same operands. Note that we can | |||
39352 | // commute the FADD operands. | |||
39353 | SDValue LHS, RHS; | |||
39354 | if (V1.getOpcode() == ISD::FSUB) { | |||
39355 | LHS = V1->getOperand(0); RHS = V1->getOperand(1); | |||
39356 | if ((V2->getOperand(0) != LHS || V2->getOperand(1) != RHS) && | |||
39357 | (V2->getOperand(0) != RHS || V2->getOperand(1) != LHS)) | |||
39358 | return false; | |||
39359 | } else { | |||
39360 | assert(V2.getOpcode() == ISD::FSUB && "Unexpected opcode")(static_cast <bool> (V2.getOpcode() == ISD::FSUB && "Unexpected opcode") ? void (0) : __assert_fail ("V2.getOpcode() == ISD::FSUB && \"Unexpected opcode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 39360, __extension__ __PRETTY_FUNCTION__)); | |||
39361 | LHS = V2->getOperand(0); RHS = V2->getOperand(1); | |||
39362 | if ((V1->getOperand(0) != LHS || V1->getOperand(1) != RHS) && | |||
39363 | (V1->getOperand(0) != RHS || V1->getOperand(1) != LHS)) | |||
39364 | return false; | |||
39365 | } | |||
39366 | ||||
39367 | ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(N)->getMask(); | |||
39368 | bool Op0Even; | |||
39369 | if (!isAddSubOrSubAddMask(Mask, Op0Even)) | |||
39370 | return false; | |||
39371 | ||||
39372 | // It's a subadd if the vector in the even parity is an FADD. | |||
39373 | IsSubAdd = Op0Even ? V1->getOpcode() == ISD::FADD | |||
39374 | : V2->getOpcode() == ISD::FADD; | |||
39375 | ||||
39376 | Opnd0 = LHS; | |||
39377 | Opnd1 = RHS; | |||
39378 | return true; | |||
39379 | } | |||
39380 | ||||
39381 | /// Combine shuffle of two fma nodes into FMAddSub or FMSubAdd. | |||
39382 | static SDValue combineShuffleToFMAddSub(SDNode *N, | |||
39383 | const X86Subtarget &Subtarget, | |||
39384 | SelectionDAG &DAG) { | |||
39385 | // We only handle target-independent shuffles. | |||
39386 | // FIXME: It would be easy and harmless to use the target shuffle mask | |||
39387 | // extraction tool to support more. | |||
39388 | if (N->getOpcode() != ISD::VECTOR_SHUFFLE) | |||
39389 | return SDValue(); | |||
39390 | ||||
39391 | MVT VT = N->getSimpleValueType(0); | |||
39392 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
39393 | if (!Subtarget.hasAnyFMA() || !TLI.isTypeLegal(VT)) | |||
39394 | return SDValue(); | |||
39395 | ||||
39396 | // We're trying to match (shuffle fma(a, b, c), X86Fmsub(a, b, c). | |||
39397 | SDValue Op0 = N->getOperand(0); | |||
39398 | SDValue Op1 = N->getOperand(1); | |||
39399 | SDValue FMAdd = Op0, FMSub = Op1; | |||
39400 | if (FMSub.getOpcode() != X86ISD::FMSUB) | |||
39401 | std::swap(FMAdd, FMSub); | |||
39402 | ||||
39403 | if (FMAdd.getOpcode() != ISD::FMA || FMSub.getOpcode() != X86ISD::FMSUB || | |||
39404 | FMAdd.getOperand(0) != FMSub.getOperand(0) || !FMAdd.hasOneUse() || | |||
39405 | FMAdd.getOperand(1) != FMSub.getOperand(1) || !FMSub.hasOneUse() || | |||
39406 | FMAdd.getOperand(2) != FMSub.getOperand(2)) | |||
39407 | return SDValue(); | |||
39408 | ||||
39409 | // Check for correct shuffle mask. | |||
39410 | ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(N)->getMask(); | |||
39411 | bool Op0Even; | |||
39412 | if (!isAddSubOrSubAddMask(Mask, Op0Even)) | |||
39413 | return SDValue(); | |||
39414 | ||||
39415 | // FMAddSub takes zeroth operand from FMSub node. | |||
39416 | SDLoc DL(N); | |||
39417 | bool IsSubAdd = Op0Even ? Op0 == FMAdd : Op1 == FMAdd; | |||
39418 | unsigned Opcode = IsSubAdd ? X86ISD::FMSUBADD : X86ISD::FMADDSUB; | |||
39419 | return DAG.getNode(Opcode, DL, VT, FMAdd.getOperand(0), FMAdd.getOperand(1), | |||
39420 | FMAdd.getOperand(2)); | |||
39421 | } | |||
39422 | ||||
39423 | /// Try to combine a shuffle into a target-specific add-sub or | |||
39424 | /// mul-add-sub node. | |||
39425 | static SDValue combineShuffleToAddSubOrFMAddSub(SDNode *N, | |||
39426 | const X86Subtarget &Subtarget, | |||
39427 | SelectionDAG &DAG) { | |||
39428 | if (SDValue V = combineShuffleToFMAddSub(N, Subtarget, DAG)) | |||
39429 | return V; | |||
39430 | ||||
39431 | SDValue Opnd0, Opnd1; | |||
39432 | bool IsSubAdd; | |||
39433 | if (!isAddSubOrSubAdd(N, Subtarget, DAG, Opnd0, Opnd1, IsSubAdd)) | |||
39434 | return SDValue(); | |||
39435 | ||||
39436 | MVT VT = N->getSimpleValueType(0); | |||
39437 | SDLoc DL(N); | |||
39438 | ||||
39439 | // Try to generate X86ISD::FMADDSUB node here. | |||
39440 | SDValue Opnd2; | |||
39441 | if (isFMAddSubOrFMSubAdd(Subtarget, DAG, Opnd0, Opnd1, Opnd2, 2)) { | |||
39442 | unsigned Opc = IsSubAdd ? X86ISD::FMSUBADD : X86ISD::FMADDSUB; | |||
39443 | return DAG.getNode(Opc, DL, VT, Opnd0, Opnd1, Opnd2); | |||
39444 | } | |||
39445 | ||||
39446 | if (IsSubAdd) | |||
39447 | return SDValue(); | |||
39448 | ||||
39449 | // Do not generate X86ISD::ADDSUB node for 512-bit types even though | |||
39450 | // the ADDSUB idiom has been successfully recognized. There are no known | |||
39451 | // X86 targets with 512-bit ADDSUB instructions! | |||
39452 | if (VT.is512BitVector()) | |||
39453 | return SDValue(); | |||
39454 | ||||
39455 | // Do not generate X86ISD::ADDSUB node for FP16's vector types even though | |||
39456 | // the ADDSUB idiom has been successfully recognized. There are no known | |||
39457 | // X86 targets with FP16 ADDSUB instructions! | |||
39458 | if (VT.getVectorElementType() == MVT::f16) | |||
39459 | return SDValue(); | |||
39460 | ||||
39461 | return DAG.getNode(X86ISD::ADDSUB, DL, VT, Opnd0, Opnd1); | |||
39462 | } | |||
39463 | ||||
39464 | // We are looking for a shuffle where both sources are concatenated with undef | |||
39465 | // and have a width that is half of the output's width. AVX2 has VPERMD/Q, so | |||
39466 | // if we can express this as a single-source shuffle, that's preferable. | |||
39467 | static SDValue combineShuffleOfConcatUndef(SDNode *N, SelectionDAG &DAG, | |||
39468 | const X86Subtarget &Subtarget) { | |||
39469 | if (!Subtarget.hasAVX2() || !isa<ShuffleVectorSDNode>(N)) | |||
39470 | return SDValue(); | |||
39471 | ||||
39472 | EVT VT = N->getValueType(0); | |||
39473 | ||||
39474 | // We only care about shuffles of 128/256-bit vectors of 32/64-bit values. | |||
39475 | if (!VT.is128BitVector() && !VT.is256BitVector()) | |||
39476 | return SDValue(); | |||
39477 | ||||
39478 | if (VT.getVectorElementType() != MVT::i32 && | |||
39479 | VT.getVectorElementType() != MVT::i64 && | |||
39480 | VT.getVectorElementType() != MVT::f32 && | |||
39481 | VT.getVectorElementType() != MVT::f64) | |||
39482 | return SDValue(); | |||
39483 | ||||
39484 | SDValue N0 = N->getOperand(0); | |||
39485 | SDValue N1 = N->getOperand(1); | |||
39486 | ||||
39487 | // Check that both sources are concats with undef. | |||
39488 | if (N0.getOpcode() != ISD::CONCAT_VECTORS || | |||
39489 | N1.getOpcode() != ISD::CONCAT_VECTORS || N0.getNumOperands() != 2 || | |||
39490 | N1.getNumOperands() != 2 || !N0.getOperand(1).isUndef() || | |||
39491 | !N1.getOperand(1).isUndef()) | |||
39492 | return SDValue(); | |||
39493 | ||||
39494 | // Construct the new shuffle mask. Elements from the first source retain their | |||
39495 | // index, but elements from the second source no longer need to skip an undef. | |||
39496 | SmallVector<int, 8> Mask; | |||
39497 | int NumElts = VT.getVectorNumElements(); | |||
39498 | ||||
39499 | ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N); | |||
39500 | for (int Elt : SVOp->getMask()) | |||
39501 | Mask.push_back(Elt < NumElts ? Elt : (Elt - NumElts / 2)); | |||
39502 | ||||
39503 | SDLoc DL(N); | |||
39504 | SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, N0.getOperand(0), | |||
39505 | N1.getOperand(0)); | |||
39506 | return DAG.getVectorShuffle(VT, DL, Concat, DAG.getUNDEF(VT), Mask); | |||
39507 | } | |||
39508 | ||||
39509 | /// If we have a shuffle of AVX/AVX512 (256/512 bit) vectors that only uses the | |||
39510 | /// low half of each source vector and does not set any high half elements in | |||
39511 | /// the destination vector, narrow the shuffle to half its original size. | |||
39512 | static SDValue narrowShuffle(ShuffleVectorSDNode *Shuf, SelectionDAG &DAG) { | |||
39513 | if (!Shuf->getValueType(0).isSimple()) | |||
39514 | return SDValue(); | |||
39515 | MVT VT = Shuf->getSimpleValueType(0); | |||
39516 | if (!VT.is256BitVector() && !VT.is512BitVector()) | |||
39517 | return SDValue(); | |||
39518 | ||||
39519 | // See if we can ignore all of the high elements of the shuffle. | |||
39520 | ArrayRef<int> Mask = Shuf->getMask(); | |||
39521 | if (!isUndefUpperHalf(Mask)) | |||
39522 | return SDValue(); | |||
39523 | ||||
39524 | // Check if the shuffle mask accesses only the low half of each input vector | |||
39525 | // (half-index output is 0 or 2). | |||
39526 | int HalfIdx1, HalfIdx2; | |||
39527 | SmallVector<int, 8> HalfMask(Mask.size() / 2); | |||
39528 | if (!getHalfShuffleMask(Mask, HalfMask, HalfIdx1, HalfIdx2) || | |||
39529 | (HalfIdx1 % 2 == 1) || (HalfIdx2 % 2 == 1)) | |||
39530 | return SDValue(); | |||
39531 | ||||
39532 | // Create a half-width shuffle to replace the unnecessarily wide shuffle. | |||
39533 | // The trick is knowing that all of the insert/extract are actually free | |||
39534 | // subregister (zmm<->ymm or ymm<->xmm) ops. That leaves us with a shuffle | |||
39535 | // of narrow inputs into a narrow output, and that is always cheaper than | |||
39536 | // the wide shuffle that we started with. | |||
39537 | return getShuffleHalfVectors(SDLoc(Shuf), Shuf->getOperand(0), | |||
39538 | Shuf->getOperand(1), HalfMask, HalfIdx1, | |||
39539 | HalfIdx2, false, DAG, /*UseConcat*/true); | |||
39540 | } | |||
39541 | ||||
39542 | static SDValue combineShuffle(SDNode *N, SelectionDAG &DAG, | |||
39543 | TargetLowering::DAGCombinerInfo &DCI, | |||
39544 | const X86Subtarget &Subtarget) { | |||
39545 | if (auto *Shuf = dyn_cast<ShuffleVectorSDNode>(N)) | |||
39546 | if (SDValue V = narrowShuffle(Shuf, DAG)) | |||
39547 | return V; | |||
39548 | ||||
39549 | // If we have legalized the vector types, look for blends of FADD and FSUB | |||
39550 | // nodes that we can fuse into an ADDSUB, FMADDSUB, or FMSUBADD node. | |||
39551 | SDLoc dl(N); | |||
39552 | EVT VT = N->getValueType(0); | |||
39553 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
39554 | if (TLI.isTypeLegal(VT)) | |||
39555 | if (SDValue AddSub = combineShuffleToAddSubOrFMAddSub(N, Subtarget, DAG)) | |||
39556 | return AddSub; | |||
39557 | ||||
39558 | // Attempt to combine into a vector load/broadcast. | |||
39559 | if (SDValue LD = combineToConsecutiveLoads( | |||
39560 | VT, SDValue(N, 0), dl, DAG, Subtarget, /*IsAfterLegalize*/ true)) | |||
39561 | return LD; | |||
39562 | ||||
39563 | // For AVX2, we sometimes want to combine | |||
39564 | // (vector_shuffle <mask> (concat_vectors t1, undef) | |||
39565 | // (concat_vectors t2, undef)) | |||
39566 | // Into: | |||
39567 | // (vector_shuffle <mask> (concat_vectors t1, t2), undef) | |||
39568 | // Since the latter can be efficiently lowered with VPERMD/VPERMQ | |||
39569 | if (SDValue ShufConcat = combineShuffleOfConcatUndef(N, DAG, Subtarget)) | |||
39570 | return ShufConcat; | |||
39571 | ||||
39572 | if (isTargetShuffle(N->getOpcode())) { | |||
39573 | SDValue Op(N, 0); | |||
39574 | if (SDValue Shuffle = combineTargetShuffle(Op, DAG, DCI, Subtarget)) | |||
39575 | return Shuffle; | |||
39576 | ||||
39577 | // Try recursively combining arbitrary sequences of x86 shuffle | |||
39578 | // instructions into higher-order shuffles. We do this after combining | |||
39579 | // specific PSHUF instruction sequences into their minimal form so that we | |||
39580 | // can evaluate how many specialized shuffle instructions are involved in | |||
39581 | // a particular chain. | |||
39582 | if (SDValue Res = combineX86ShufflesRecursively(Op, DAG, Subtarget)) | |||
39583 | return Res; | |||
39584 | ||||
39585 | // Simplify source operands based on shuffle mask. | |||
39586 | // TODO - merge this into combineX86ShufflesRecursively. | |||
39587 | APInt KnownUndef, KnownZero; | |||
39588 | APInt DemandedElts = APInt::getAllOnes(VT.getVectorNumElements()); | |||
39589 | if (TLI.SimplifyDemandedVectorElts(Op, DemandedElts, KnownUndef, KnownZero, | |||
39590 | DCI)) | |||
39591 | return SDValue(N, 0); | |||
39592 | } | |||
39593 | ||||
39594 | return SDValue(); | |||
39595 | } | |||
39596 | ||||
39597 | // Simplify variable target shuffle masks based on the demanded elements. | |||
39598 | // TODO: Handle DemandedBits in mask indices as well? | |||
39599 | bool X86TargetLowering::SimplifyDemandedVectorEltsForTargetShuffle( | |||
39600 | SDValue Op, const APInt &DemandedElts, unsigned MaskIndex, | |||
39601 | TargetLowering::TargetLoweringOpt &TLO, unsigned Depth) const { | |||
39602 | // If we're demanding all elements don't bother trying to simplify the mask. | |||
39603 | unsigned NumElts = DemandedElts.getBitWidth(); | |||
39604 | if (DemandedElts.isAllOnes()) | |||
39605 | return false; | |||
39606 | ||||
39607 | SDValue Mask = Op.getOperand(MaskIndex); | |||
39608 | if (!Mask.hasOneUse()) | |||
39609 | return false; | |||
39610 | ||||
39611 | // Attempt to generically simplify the variable shuffle mask. | |||
39612 | APInt MaskUndef, MaskZero; | |||
39613 | if (SimplifyDemandedVectorElts(Mask, DemandedElts, MaskUndef, MaskZero, TLO, | |||
39614 | Depth + 1)) | |||
39615 | return true; | |||
39616 | ||||
39617 | // Attempt to extract+simplify a (constant pool load) shuffle mask. | |||
39618 | // TODO: Support other types from getTargetShuffleMaskIndices? | |||
39619 | SDValue BC = peekThroughOneUseBitcasts(Mask); | |||
39620 | EVT BCVT = BC.getValueType(); | |||
39621 | auto *Load = dyn_cast<LoadSDNode>(BC); | |||
39622 | if (!Load) | |||
39623 | return false; | |||
39624 | ||||
39625 | const Constant *C = getTargetConstantFromNode(Load); | |||
39626 | if (!C) | |||
39627 | return false; | |||
39628 | ||||
39629 | Type *CTy = C->getType(); | |||
39630 | if (!CTy->isVectorTy() || | |||
39631 | CTy->getPrimitiveSizeInBits() != Mask.getValueSizeInBits()) | |||
39632 | return false; | |||
39633 | ||||
39634 | // Handle scaling for i64 elements on 32-bit targets. | |||
39635 | unsigned NumCstElts = cast<FixedVectorType>(CTy)->getNumElements(); | |||
39636 | if (NumCstElts != NumElts && NumCstElts != (NumElts * 2)) | |||
39637 | return false; | |||
39638 | unsigned Scale = NumCstElts / NumElts; | |||
39639 | ||||
39640 | // Simplify mask if we have an undemanded element that is not undef. | |||
39641 | bool Simplified = false; | |||
39642 | SmallVector<Constant *, 32> ConstVecOps; | |||
39643 | for (unsigned i = 0; i != NumCstElts; ++i) { | |||
39644 | Constant *Elt = C->getAggregateElement(i); | |||
39645 | if (!DemandedElts[i / Scale] && !isa<UndefValue>(Elt)) { | |||
39646 | ConstVecOps.push_back(UndefValue::get(Elt->getType())); | |||
39647 | Simplified = true; | |||
39648 | continue; | |||
39649 | } | |||
39650 | ConstVecOps.push_back(Elt); | |||
39651 | } | |||
39652 | if (!Simplified) | |||
39653 | return false; | |||
39654 | ||||
39655 | // Generate new constant pool entry + legalize immediately for the load. | |||
39656 | SDLoc DL(Op); | |||
39657 | SDValue CV = TLO.DAG.getConstantPool(ConstantVector::get(ConstVecOps), BCVT); | |||
39658 | SDValue LegalCV = LowerConstantPool(CV, TLO.DAG); | |||
39659 | SDValue NewMask = TLO.DAG.getLoad( | |||
39660 | BCVT, DL, TLO.DAG.getEntryNode(), LegalCV, | |||
39661 | MachinePointerInfo::getConstantPool(TLO.DAG.getMachineFunction()), | |||
39662 | Load->getAlign()); | |||
39663 | return TLO.CombineTo(Mask, TLO.DAG.getBitcast(Mask.getValueType(), NewMask)); | |||
39664 | } | |||
39665 | ||||
39666 | bool X86TargetLowering::SimplifyDemandedVectorEltsForTargetNode( | |||
39667 | SDValue Op, const APInt &DemandedElts, APInt &KnownUndef, APInt &KnownZero, | |||
39668 | TargetLoweringOpt &TLO, unsigned Depth) const { | |||
39669 | int NumElts = DemandedElts.getBitWidth(); | |||
39670 | unsigned Opc = Op.getOpcode(); | |||
39671 | EVT VT = Op.getValueType(); | |||
39672 | ||||
39673 | // Handle special case opcodes. | |||
39674 | switch (Opc) { | |||
39675 | case X86ISD::PMULDQ: | |||
39676 | case X86ISD::PMULUDQ: { | |||
39677 | APInt LHSUndef, LHSZero; | |||
39678 | APInt RHSUndef, RHSZero; | |||
39679 | SDValue LHS = Op.getOperand(0); | |||
39680 | SDValue RHS = Op.getOperand(1); | |||
39681 | if (SimplifyDemandedVectorElts(LHS, DemandedElts, LHSUndef, LHSZero, TLO, | |||
39682 | Depth + 1)) | |||
39683 | return true; | |||
39684 | if (SimplifyDemandedVectorElts(RHS, DemandedElts, RHSUndef, RHSZero, TLO, | |||
39685 | Depth + 1)) | |||
39686 | return true; | |||
39687 | // Multiply by zero. | |||
39688 | KnownZero = LHSZero | RHSZero; | |||
39689 | break; | |||
39690 | } | |||
39691 | case X86ISD::VPMADDWD: { | |||
39692 | APInt LHSUndef, LHSZero; | |||
39693 | APInt RHSUndef, RHSZero; | |||
39694 | SDValue LHS = Op.getOperand(0); | |||
39695 | SDValue RHS = Op.getOperand(1); | |||
39696 | APInt DemandedSrcElts = APIntOps::ScaleBitMask(DemandedElts, 2 * NumElts); | |||
39697 | ||||
39698 | APInt DemandedRHSElts = DemandedSrcElts; | |||
39699 | if (SimplifyDemandedVectorElts(RHS, DemandedRHSElts, RHSUndef, RHSZero, TLO, | |||
39700 | Depth + 1)) | |||
39701 | return true; | |||
39702 | ||||
39703 | // If RHS elements are known zero then we don't need the LHS equivalent. | |||
39704 | APInt DemandedLHSElts = DemandedSrcElts & ~RHSZero; | |||
39705 | if (SimplifyDemandedVectorElts(LHS, DemandedLHSElts, LHSUndef, LHSZero, TLO, | |||
39706 | Depth + 1)) | |||
39707 | return true; | |||
39708 | // TODO: Multiply by zero. | |||
39709 | break; | |||
39710 | } | |||
39711 | case X86ISD::PSADBW: { | |||
39712 | SDValue LHS = Op.getOperand(0); | |||
39713 | SDValue RHS = Op.getOperand(1); | |||
39714 | assert(VT.getScalarType() == MVT::i64 &&(static_cast <bool> (VT.getScalarType() == MVT::i64 && LHS.getValueType() == RHS.getValueType() && LHS.getValueType ().getScalarType() == MVT::i8 && "Unexpected PSADBW types" ) ? void (0) : __assert_fail ("VT.getScalarType() == MVT::i64 && LHS.getValueType() == RHS.getValueType() && LHS.getValueType().getScalarType() == MVT::i8 && \"Unexpected PSADBW types\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 39717, __extension__ __PRETTY_FUNCTION__)) | |||
39715 | LHS.getValueType() == RHS.getValueType() &&(static_cast <bool> (VT.getScalarType() == MVT::i64 && LHS.getValueType() == RHS.getValueType() && LHS.getValueType ().getScalarType() == MVT::i8 && "Unexpected PSADBW types" ) ? void (0) : __assert_fail ("VT.getScalarType() == MVT::i64 && LHS.getValueType() == RHS.getValueType() && LHS.getValueType().getScalarType() == MVT::i8 && \"Unexpected PSADBW types\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 39717, __extension__ __PRETTY_FUNCTION__)) | |||
39716 | LHS.getValueType().getScalarType() == MVT::i8 &&(static_cast <bool> (VT.getScalarType() == MVT::i64 && LHS.getValueType() == RHS.getValueType() && LHS.getValueType ().getScalarType() == MVT::i8 && "Unexpected PSADBW types" ) ? void (0) : __assert_fail ("VT.getScalarType() == MVT::i64 && LHS.getValueType() == RHS.getValueType() && LHS.getValueType().getScalarType() == MVT::i8 && \"Unexpected PSADBW types\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 39717, __extension__ __PRETTY_FUNCTION__)) | |||
39717 | "Unexpected PSADBW types")(static_cast <bool> (VT.getScalarType() == MVT::i64 && LHS.getValueType() == RHS.getValueType() && LHS.getValueType ().getScalarType() == MVT::i8 && "Unexpected PSADBW types" ) ? void (0) : __assert_fail ("VT.getScalarType() == MVT::i64 && LHS.getValueType() == RHS.getValueType() && LHS.getValueType().getScalarType() == MVT::i8 && \"Unexpected PSADBW types\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 39717, __extension__ __PRETTY_FUNCTION__)); | |||
39718 | ||||
39719 | // Aggressively peek through ops to get at the demanded elts. | |||
39720 | if (!DemandedElts.isAllOnes()) { | |||
39721 | unsigned NumSrcElts = LHS.getValueType().getVectorNumElements(); | |||
39722 | APInt DemandedSrcElts = APIntOps::ScaleBitMask(DemandedElts, NumSrcElts); | |||
39723 | SDValue NewLHS = SimplifyMultipleUseDemandedVectorElts( | |||
39724 | LHS, DemandedSrcElts, TLO.DAG, Depth + 1); | |||
39725 | SDValue NewRHS = SimplifyMultipleUseDemandedVectorElts( | |||
39726 | RHS, DemandedSrcElts, TLO.DAG, Depth + 1); | |||
39727 | if (NewLHS || NewRHS) { | |||
39728 | NewLHS = NewLHS ? NewLHS : LHS; | |||
39729 | NewRHS = NewRHS ? NewRHS : RHS; | |||
39730 | return TLO.CombineTo( | |||
39731 | Op, TLO.DAG.getNode(Opc, SDLoc(Op), VT, NewLHS, NewRHS)); | |||
39732 | } | |||
39733 | } | |||
39734 | break; | |||
39735 | } | |||
39736 | case X86ISD::VSHL: | |||
39737 | case X86ISD::VSRL: | |||
39738 | case X86ISD::VSRA: { | |||
39739 | // We only need the bottom 64-bits of the (128-bit) shift amount. | |||
39740 | SDValue Amt = Op.getOperand(1); | |||
39741 | MVT AmtVT = Amt.getSimpleValueType(); | |||
39742 | assert(AmtVT.is128BitVector() && "Unexpected value type")(static_cast <bool> (AmtVT.is128BitVector() && "Unexpected value type" ) ? void (0) : __assert_fail ("AmtVT.is128BitVector() && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 39742, __extension__ __PRETTY_FUNCTION__)); | |||
39743 | ||||
39744 | // If we reuse the shift amount just for sse shift amounts then we know that | |||
39745 | // only the bottom 64-bits are only ever used. | |||
39746 | bool AssumeSingleUse = llvm::all_of(Amt->uses(), [&Amt](SDNode *Use) { | |||
39747 | unsigned UseOpc = Use->getOpcode(); | |||
39748 | return (UseOpc == X86ISD::VSHL || UseOpc == X86ISD::VSRL || | |||
39749 | UseOpc == X86ISD::VSRA) && | |||
39750 | Use->getOperand(0) != Amt; | |||
39751 | }); | |||
39752 | ||||
39753 | APInt AmtUndef, AmtZero; | |||
39754 | unsigned NumAmtElts = AmtVT.getVectorNumElements(); | |||
39755 | APInt AmtElts = APInt::getLowBitsSet(NumAmtElts, NumAmtElts / 2); | |||
39756 | if (SimplifyDemandedVectorElts(Amt, AmtElts, AmtUndef, AmtZero, TLO, | |||
39757 | Depth + 1, AssumeSingleUse)) | |||
39758 | return true; | |||
39759 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
39760 | } | |||
39761 | case X86ISD::VSHLI: | |||
39762 | case X86ISD::VSRLI: | |||
39763 | case X86ISD::VSRAI: { | |||
39764 | SDValue Src = Op.getOperand(0); | |||
39765 | APInt SrcUndef; | |||
39766 | if (SimplifyDemandedVectorElts(Src, DemandedElts, SrcUndef, KnownZero, TLO, | |||
39767 | Depth + 1)) | |||
39768 | return true; | |||
39769 | ||||
39770 | // Aggressively peek through ops to get at the demanded elts. | |||
39771 | if (!DemandedElts.isAllOnes()) | |||
39772 | if (SDValue NewSrc = SimplifyMultipleUseDemandedVectorElts( | |||
39773 | Src, DemandedElts, TLO.DAG, Depth + 1)) | |||
39774 | return TLO.CombineTo( | |||
39775 | Op, TLO.DAG.getNode(Opc, SDLoc(Op), VT, NewSrc, Op.getOperand(1))); | |||
39776 | break; | |||
39777 | } | |||
39778 | case X86ISD::KSHIFTL: { | |||
39779 | SDValue Src = Op.getOperand(0); | |||
39780 | auto *Amt = cast<ConstantSDNode>(Op.getOperand(1)); | |||
39781 | assert(Amt->getAPIntValue().ult(NumElts) && "Out of range shift amount")(static_cast <bool> (Amt->getAPIntValue().ult(NumElts ) && "Out of range shift amount") ? void (0) : __assert_fail ("Amt->getAPIntValue().ult(NumElts) && \"Out of range shift amount\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 39781, __extension__ __PRETTY_FUNCTION__)); | |||
39782 | unsigned ShiftAmt = Amt->getZExtValue(); | |||
39783 | ||||
39784 | if (ShiftAmt == 0) | |||
39785 | return TLO.CombineTo(Op, Src); | |||
39786 | ||||
39787 | // If this is ((X >>u C1) << ShAmt), see if we can simplify this into a | |||
39788 | // single shift. We can do this if the bottom bits (which are shifted | |||
39789 | // out) are never demanded. | |||
39790 | if (Src.getOpcode() == X86ISD::KSHIFTR) { | |||
39791 | if (!DemandedElts.intersects(APInt::getLowBitsSet(NumElts, ShiftAmt))) { | |||
39792 | unsigned C1 = Src.getConstantOperandVal(1); | |||
39793 | unsigned NewOpc = X86ISD::KSHIFTL; | |||
39794 | int Diff = ShiftAmt - C1; | |||
39795 | if (Diff < 0) { | |||
39796 | Diff = -Diff; | |||
39797 | NewOpc = X86ISD::KSHIFTR; | |||
39798 | } | |||
39799 | ||||
39800 | SDLoc dl(Op); | |||
39801 | SDValue NewSA = TLO.DAG.getTargetConstant(Diff, dl, MVT::i8); | |||
39802 | return TLO.CombineTo( | |||
39803 | Op, TLO.DAG.getNode(NewOpc, dl, VT, Src.getOperand(0), NewSA)); | |||
39804 | } | |||
39805 | } | |||
39806 | ||||
39807 | APInt DemandedSrc = DemandedElts.lshr(ShiftAmt); | |||
39808 | if (SimplifyDemandedVectorElts(Src, DemandedSrc, KnownUndef, KnownZero, TLO, | |||
39809 | Depth + 1)) | |||
39810 | return true; | |||
39811 | ||||
39812 | KnownUndef <<= ShiftAmt; | |||
39813 | KnownZero <<= ShiftAmt; | |||
39814 | KnownZero.setLowBits(ShiftAmt); | |||
39815 | break; | |||
39816 | } | |||
39817 | case X86ISD::KSHIFTR: { | |||
39818 | SDValue Src = Op.getOperand(0); | |||
39819 | auto *Amt = cast<ConstantSDNode>(Op.getOperand(1)); | |||
39820 | assert(Amt->getAPIntValue().ult(NumElts) && "Out of range shift amount")(static_cast <bool> (Amt->getAPIntValue().ult(NumElts ) && "Out of range shift amount") ? void (0) : __assert_fail ("Amt->getAPIntValue().ult(NumElts) && \"Out of range shift amount\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 39820, __extension__ __PRETTY_FUNCTION__)); | |||
39821 | unsigned ShiftAmt = Amt->getZExtValue(); | |||
39822 | ||||
39823 | if (ShiftAmt == 0) | |||
39824 | return TLO.CombineTo(Op, Src); | |||
39825 | ||||
39826 | // If this is ((X << C1) >>u ShAmt), see if we can simplify this into a | |||
39827 | // single shift. We can do this if the top bits (which are shifted | |||
39828 | // out) are never demanded. | |||
39829 | if (Src.getOpcode() == X86ISD::KSHIFTL) { | |||
39830 | if (!DemandedElts.intersects(APInt::getHighBitsSet(NumElts, ShiftAmt))) { | |||
39831 | unsigned C1 = Src.getConstantOperandVal(1); | |||
39832 | unsigned NewOpc = X86ISD::KSHIFTR; | |||
39833 | int Diff = ShiftAmt - C1; | |||
39834 | if (Diff < 0) { | |||
39835 | Diff = -Diff; | |||
39836 | NewOpc = X86ISD::KSHIFTL; | |||
39837 | } | |||
39838 | ||||
39839 | SDLoc dl(Op); | |||
39840 | SDValue NewSA = TLO.DAG.getTargetConstant(Diff, dl, MVT::i8); | |||
39841 | return TLO.CombineTo( | |||
39842 | Op, TLO.DAG.getNode(NewOpc, dl, VT, Src.getOperand(0), NewSA)); | |||
39843 | } | |||
39844 | } | |||
39845 | ||||
39846 | APInt DemandedSrc = DemandedElts.shl(ShiftAmt); | |||
39847 | if (SimplifyDemandedVectorElts(Src, DemandedSrc, KnownUndef, KnownZero, TLO, | |||
39848 | Depth + 1)) | |||
39849 | return true; | |||
39850 | ||||
39851 | KnownUndef.lshrInPlace(ShiftAmt); | |||
39852 | KnownZero.lshrInPlace(ShiftAmt); | |||
39853 | KnownZero.setHighBits(ShiftAmt); | |||
39854 | break; | |||
39855 | } | |||
39856 | case X86ISD::CVTSI2P: | |||
39857 | case X86ISD::CVTUI2P: { | |||
39858 | SDValue Src = Op.getOperand(0); | |||
39859 | MVT SrcVT = Src.getSimpleValueType(); | |||
39860 | APInt SrcUndef, SrcZero; | |||
39861 | APInt SrcElts = DemandedElts.zextOrTrunc(SrcVT.getVectorNumElements()); | |||
39862 | if (SimplifyDemandedVectorElts(Src, SrcElts, SrcUndef, SrcZero, TLO, | |||
39863 | Depth + 1)) | |||
39864 | return true; | |||
39865 | break; | |||
39866 | } | |||
39867 | case X86ISD::PACKSS: | |||
39868 | case X86ISD::PACKUS: { | |||
39869 | SDValue N0 = Op.getOperand(0); | |||
39870 | SDValue N1 = Op.getOperand(1); | |||
39871 | ||||
39872 | APInt DemandedLHS, DemandedRHS; | |||
39873 | getPackDemandedElts(VT, DemandedElts, DemandedLHS, DemandedRHS); | |||
39874 | ||||
39875 | APInt LHSUndef, LHSZero; | |||
39876 | if (SimplifyDemandedVectorElts(N0, DemandedLHS, LHSUndef, LHSZero, TLO, | |||
39877 | Depth + 1)) | |||
39878 | return true; | |||
39879 | APInt RHSUndef, RHSZero; | |||
39880 | if (SimplifyDemandedVectorElts(N1, DemandedRHS, RHSUndef, RHSZero, TLO, | |||
39881 | Depth + 1)) | |||
39882 | return true; | |||
39883 | ||||
39884 | // TODO - pass on known zero/undef. | |||
39885 | ||||
39886 | // Aggressively peek through ops to get at the demanded elts. | |||
39887 | // TODO - we should do this for all target/faux shuffles ops. | |||
39888 | if (!DemandedElts.isAllOnes()) { | |||
39889 | SDValue NewN0 = SimplifyMultipleUseDemandedVectorElts(N0, DemandedLHS, | |||
39890 | TLO.DAG, Depth + 1); | |||
39891 | SDValue NewN1 = SimplifyMultipleUseDemandedVectorElts(N1, DemandedRHS, | |||
39892 | TLO.DAG, Depth + 1); | |||
39893 | if (NewN0 || NewN1) { | |||
39894 | NewN0 = NewN0 ? NewN0 : N0; | |||
39895 | NewN1 = NewN1 ? NewN1 : N1; | |||
39896 | return TLO.CombineTo(Op, | |||
39897 | TLO.DAG.getNode(Opc, SDLoc(Op), VT, NewN0, NewN1)); | |||
39898 | } | |||
39899 | } | |||
39900 | break; | |||
39901 | } | |||
39902 | case X86ISD::HADD: | |||
39903 | case X86ISD::HSUB: | |||
39904 | case X86ISD::FHADD: | |||
39905 | case X86ISD::FHSUB: { | |||
39906 | SDValue N0 = Op.getOperand(0); | |||
39907 | SDValue N1 = Op.getOperand(1); | |||
39908 | ||||
39909 | APInt DemandedLHS, DemandedRHS; | |||
39910 | getHorizDemandedElts(VT, DemandedElts, DemandedLHS, DemandedRHS); | |||
39911 | ||||
39912 | APInt LHSUndef, LHSZero; | |||
39913 | if (SimplifyDemandedVectorElts(N0, DemandedLHS, LHSUndef, LHSZero, TLO, | |||
39914 | Depth + 1)) | |||
39915 | return true; | |||
39916 | APInt RHSUndef, RHSZero; | |||
39917 | if (SimplifyDemandedVectorElts(N1, DemandedRHS, RHSUndef, RHSZero, TLO, | |||
39918 | Depth + 1)) | |||
39919 | return true; | |||
39920 | ||||
39921 | // TODO - pass on known zero/undef. | |||
39922 | ||||
39923 | // Aggressively peek through ops to get at the demanded elts. | |||
39924 | // TODO: Handle repeated operands. | |||
39925 | if (N0 != N1 && !DemandedElts.isAllOnes()) { | |||
39926 | SDValue NewN0 = SimplifyMultipleUseDemandedVectorElts(N0, DemandedLHS, | |||
39927 | TLO.DAG, Depth + 1); | |||
39928 | SDValue NewN1 = SimplifyMultipleUseDemandedVectorElts(N1, DemandedRHS, | |||
39929 | TLO.DAG, Depth + 1); | |||
39930 | if (NewN0 || NewN1) { | |||
39931 | NewN0 = NewN0 ? NewN0 : N0; | |||
39932 | NewN1 = NewN1 ? NewN1 : N1; | |||
39933 | return TLO.CombineTo(Op, | |||
39934 | TLO.DAG.getNode(Opc, SDLoc(Op), VT, NewN0, NewN1)); | |||
39935 | } | |||
39936 | } | |||
39937 | break; | |||
39938 | } | |||
39939 | case X86ISD::VTRUNC: | |||
39940 | case X86ISD::VTRUNCS: | |||
39941 | case X86ISD::VTRUNCUS: { | |||
39942 | SDValue Src = Op.getOperand(0); | |||
39943 | MVT SrcVT = Src.getSimpleValueType(); | |||
39944 | APInt DemandedSrc = DemandedElts.zextOrTrunc(SrcVT.getVectorNumElements()); | |||
39945 | APInt SrcUndef, SrcZero; | |||
39946 | if (SimplifyDemandedVectorElts(Src, DemandedSrc, SrcUndef, SrcZero, TLO, | |||
39947 | Depth + 1)) | |||
39948 | return true; | |||
39949 | KnownZero = SrcZero.zextOrTrunc(NumElts); | |||
39950 | KnownUndef = SrcUndef.zextOrTrunc(NumElts); | |||
39951 | break; | |||
39952 | } | |||
39953 | case X86ISD::BLENDV: { | |||
39954 | APInt SelUndef, SelZero; | |||
39955 | if (SimplifyDemandedVectorElts(Op.getOperand(0), DemandedElts, SelUndef, | |||
39956 | SelZero, TLO, Depth + 1)) | |||
39957 | return true; | |||
39958 | ||||
39959 | // TODO: Use SelZero to adjust LHS/RHS DemandedElts. | |||
39960 | APInt LHSUndef, LHSZero; | |||
39961 | if (SimplifyDemandedVectorElts(Op.getOperand(1), DemandedElts, LHSUndef, | |||
39962 | LHSZero, TLO, Depth + 1)) | |||
39963 | return true; | |||
39964 | ||||
39965 | APInt RHSUndef, RHSZero; | |||
39966 | if (SimplifyDemandedVectorElts(Op.getOperand(2), DemandedElts, RHSUndef, | |||
39967 | RHSZero, TLO, Depth + 1)) | |||
39968 | return true; | |||
39969 | ||||
39970 | KnownZero = LHSZero & RHSZero; | |||
39971 | KnownUndef = LHSUndef & RHSUndef; | |||
39972 | break; | |||
39973 | } | |||
39974 | case X86ISD::VZEXT_MOVL: { | |||
39975 | // If upper demanded elements are already zero then we have nothing to do. | |||
39976 | SDValue Src = Op.getOperand(0); | |||
39977 | APInt DemandedUpperElts = DemandedElts; | |||
39978 | DemandedUpperElts.clearLowBits(1); | |||
39979 | if (TLO.DAG.computeKnownBits(Src, DemandedUpperElts, Depth + 1).isZero()) | |||
39980 | return TLO.CombineTo(Op, Src); | |||
39981 | break; | |||
39982 | } | |||
39983 | case X86ISD::VBROADCAST: { | |||
39984 | SDValue Src = Op.getOperand(0); | |||
39985 | MVT SrcVT = Src.getSimpleValueType(); | |||
39986 | if (!SrcVT.isVector()) | |||
39987 | break; | |||
39988 | // Don't bother broadcasting if we just need the 0'th element. | |||
39989 | if (DemandedElts == 1) { | |||
39990 | if (Src.getValueType() != VT) | |||
39991 | Src = widenSubVector(VT.getSimpleVT(), Src, false, Subtarget, TLO.DAG, | |||
39992 | SDLoc(Op)); | |||
39993 | return TLO.CombineTo(Op, Src); | |||
39994 | } | |||
39995 | APInt SrcUndef, SrcZero; | |||
39996 | APInt SrcElts = APInt::getOneBitSet(SrcVT.getVectorNumElements(), 0); | |||
39997 | if (SimplifyDemandedVectorElts(Src, SrcElts, SrcUndef, SrcZero, TLO, | |||
39998 | Depth + 1)) | |||
39999 | return true; | |||
40000 | // Aggressively peek through src to get at the demanded elt. | |||
40001 | // TODO - we should do this for all target/faux shuffles ops. | |||
40002 | if (SDValue NewSrc = SimplifyMultipleUseDemandedVectorElts( | |||
40003 | Src, SrcElts, TLO.DAG, Depth + 1)) | |||
40004 | return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, SDLoc(Op), VT, NewSrc)); | |||
40005 | break; | |||
40006 | } | |||
40007 | case X86ISD::VPERMV: | |||
40008 | if (SimplifyDemandedVectorEltsForTargetShuffle(Op, DemandedElts, 0, TLO, | |||
40009 | Depth)) | |||
40010 | return true; | |||
40011 | break; | |||
40012 | case X86ISD::PSHUFB: | |||
40013 | case X86ISD::VPERMV3: | |||
40014 | case X86ISD::VPERMILPV: | |||
40015 | if (SimplifyDemandedVectorEltsForTargetShuffle(Op, DemandedElts, 1, TLO, | |||
40016 | Depth)) | |||
40017 | return true; | |||
40018 | break; | |||
40019 | case X86ISD::VPPERM: | |||
40020 | case X86ISD::VPERMIL2: | |||
40021 | if (SimplifyDemandedVectorEltsForTargetShuffle(Op, DemandedElts, 2, TLO, | |||
40022 | Depth)) | |||
40023 | return true; | |||
40024 | break; | |||
40025 | } | |||
40026 | ||||
40027 | // For 256/512-bit ops that are 128/256-bit ops glued together, if we do not | |||
40028 | // demand any of the high elements, then narrow the op to 128/256-bits: e.g. | |||
40029 | // (op ymm0, ymm1) --> insert undef, (op xmm0, xmm1), 0 | |||
40030 | if ((VT.is256BitVector() || VT.is512BitVector()) && | |||
40031 | DemandedElts.lshr(NumElts / 2) == 0) { | |||
40032 | unsigned SizeInBits = VT.getSizeInBits(); | |||
40033 | unsigned ExtSizeInBits = SizeInBits / 2; | |||
40034 | ||||
40035 | // See if 512-bit ops only use the bottom 128-bits. | |||
40036 | if (VT.is512BitVector() && DemandedElts.lshr(NumElts / 4) == 0) | |||
40037 | ExtSizeInBits = SizeInBits / 4; | |||
40038 | ||||
40039 | switch (Opc) { | |||
40040 | // Scalar broadcast. | |||
40041 | case X86ISD::VBROADCAST: { | |||
40042 | SDLoc DL(Op); | |||
40043 | SDValue Src = Op.getOperand(0); | |||
40044 | if (Src.getValueSizeInBits() > ExtSizeInBits) | |||
40045 | Src = extractSubVector(Src, 0, TLO.DAG, DL, ExtSizeInBits); | |||
40046 | EVT BcstVT = EVT::getVectorVT(*TLO.DAG.getContext(), VT.getScalarType(), | |||
40047 | ExtSizeInBits / VT.getScalarSizeInBits()); | |||
40048 | SDValue Bcst = TLO.DAG.getNode(X86ISD::VBROADCAST, DL, BcstVT, Src); | |||
40049 | return TLO.CombineTo(Op, insertSubVector(TLO.DAG.getUNDEF(VT), Bcst, 0, | |||
40050 | TLO.DAG, DL, ExtSizeInBits)); | |||
40051 | } | |||
40052 | case X86ISD::VBROADCAST_LOAD: { | |||
40053 | SDLoc DL(Op); | |||
40054 | auto *MemIntr = cast<MemIntrinsicSDNode>(Op); | |||
40055 | EVT BcstVT = EVT::getVectorVT(*TLO.DAG.getContext(), VT.getScalarType(), | |||
40056 | ExtSizeInBits / VT.getScalarSizeInBits()); | |||
40057 | SDVTList Tys = TLO.DAG.getVTList(BcstVT, MVT::Other); | |||
40058 | SDValue Ops[] = {MemIntr->getOperand(0), MemIntr->getOperand(1)}; | |||
40059 | SDValue Bcst = TLO.DAG.getMemIntrinsicNode( | |||
40060 | X86ISD::VBROADCAST_LOAD, DL, Tys, Ops, MemIntr->getMemoryVT(), | |||
40061 | MemIntr->getMemOperand()); | |||
40062 | TLO.DAG.makeEquivalentMemoryOrdering(SDValue(MemIntr, 1), | |||
40063 | Bcst.getValue(1)); | |||
40064 | return TLO.CombineTo(Op, insertSubVector(TLO.DAG.getUNDEF(VT), Bcst, 0, | |||
40065 | TLO.DAG, DL, ExtSizeInBits)); | |||
40066 | } | |||
40067 | // Subvector broadcast. | |||
40068 | case X86ISD::SUBV_BROADCAST_LOAD: { | |||
40069 | auto *MemIntr = cast<MemIntrinsicSDNode>(Op); | |||
40070 | EVT MemVT = MemIntr->getMemoryVT(); | |||
40071 | if (ExtSizeInBits == MemVT.getStoreSizeInBits()) { | |||
40072 | SDLoc DL(Op); | |||
40073 | SDValue Ld = | |||
40074 | TLO.DAG.getLoad(MemVT, DL, MemIntr->getChain(), | |||
40075 | MemIntr->getBasePtr(), MemIntr->getMemOperand()); | |||
40076 | TLO.DAG.makeEquivalentMemoryOrdering(SDValue(MemIntr, 1), | |||
40077 | Ld.getValue(1)); | |||
40078 | return TLO.CombineTo(Op, insertSubVector(TLO.DAG.getUNDEF(VT), Ld, 0, | |||
40079 | TLO.DAG, DL, ExtSizeInBits)); | |||
40080 | } else if ((ExtSizeInBits % MemVT.getStoreSizeInBits()) == 0) { | |||
40081 | SDLoc DL(Op); | |||
40082 | EVT BcstVT = EVT::getVectorVT(*TLO.DAG.getContext(), VT.getScalarType(), | |||
40083 | ExtSizeInBits / VT.getScalarSizeInBits()); | |||
40084 | SDVTList Tys = TLO.DAG.getVTList(BcstVT, MVT::Other); | |||
40085 | SDValue Ops[] = {MemIntr->getOperand(0), MemIntr->getOperand(1)}; | |||
40086 | SDValue Bcst = | |||
40087 | TLO.DAG.getMemIntrinsicNode(X86ISD::SUBV_BROADCAST_LOAD, DL, Tys, | |||
40088 | Ops, MemVT, MemIntr->getMemOperand()); | |||
40089 | TLO.DAG.makeEquivalentMemoryOrdering(SDValue(MemIntr, 1), | |||
40090 | Bcst.getValue(1)); | |||
40091 | return TLO.CombineTo(Op, insertSubVector(TLO.DAG.getUNDEF(VT), Bcst, 0, | |||
40092 | TLO.DAG, DL, ExtSizeInBits)); | |||
40093 | } | |||
40094 | break; | |||
40095 | } | |||
40096 | // Byte shifts by immediate. | |||
40097 | case X86ISD::VSHLDQ: | |||
40098 | case X86ISD::VSRLDQ: | |||
40099 | // Shift by uniform. | |||
40100 | case X86ISD::VSHL: | |||
40101 | case X86ISD::VSRL: | |||
40102 | case X86ISD::VSRA: | |||
40103 | // Shift by immediate. | |||
40104 | case X86ISD::VSHLI: | |||
40105 | case X86ISD::VSRLI: | |||
40106 | case X86ISD::VSRAI: { | |||
40107 | SDLoc DL(Op); | |||
40108 | SDValue Ext0 = | |||
40109 | extractSubVector(Op.getOperand(0), 0, TLO.DAG, DL, ExtSizeInBits); | |||
40110 | SDValue ExtOp = | |||
40111 | TLO.DAG.getNode(Opc, DL, Ext0.getValueType(), Ext0, Op.getOperand(1)); | |||
40112 | SDValue UndefVec = TLO.DAG.getUNDEF(VT); | |||
40113 | SDValue Insert = | |||
40114 | insertSubVector(UndefVec, ExtOp, 0, TLO.DAG, DL, ExtSizeInBits); | |||
40115 | return TLO.CombineTo(Op, Insert); | |||
40116 | } | |||
40117 | case X86ISD::VPERMI: { | |||
40118 | // Simplify PERMPD/PERMQ to extract_subvector. | |||
40119 | // TODO: This should be done in shuffle combining. | |||
40120 | if (VT == MVT::v4f64 || VT == MVT::v4i64) { | |||
40121 | SmallVector<int, 4> Mask; | |||
40122 | DecodeVPERMMask(NumElts, Op.getConstantOperandVal(1), Mask); | |||
40123 | if (isUndefOrEqual(Mask[0], 2) && isUndefOrEqual(Mask[1], 3)) { | |||
40124 | SDLoc DL(Op); | |||
40125 | SDValue Ext = extractSubVector(Op.getOperand(0), 2, TLO.DAG, DL, 128); | |||
40126 | SDValue UndefVec = TLO.DAG.getUNDEF(VT); | |||
40127 | SDValue Insert = insertSubVector(UndefVec, Ext, 0, TLO.DAG, DL, 128); | |||
40128 | return TLO.CombineTo(Op, Insert); | |||
40129 | } | |||
40130 | } | |||
40131 | break; | |||
40132 | } | |||
40133 | case X86ISD::VPERM2X128: { | |||
40134 | // Simplify VPERM2F128/VPERM2I128 to extract_subvector. | |||
40135 | SDLoc DL(Op); | |||
40136 | unsigned LoMask = Op.getConstantOperandVal(2) & 0xF; | |||
40137 | if (LoMask & 0x8) | |||
40138 | return TLO.CombineTo( | |||
40139 | Op, getZeroVector(VT.getSimpleVT(), Subtarget, TLO.DAG, DL)); | |||
40140 | unsigned EltIdx = (LoMask & 0x1) * (NumElts / 2); | |||
40141 | unsigned SrcIdx = (LoMask & 0x2) >> 1; | |||
40142 | SDValue ExtOp = | |||
40143 | extractSubVector(Op.getOperand(SrcIdx), EltIdx, TLO.DAG, DL, 128); | |||
40144 | SDValue UndefVec = TLO.DAG.getUNDEF(VT); | |||
40145 | SDValue Insert = | |||
40146 | insertSubVector(UndefVec, ExtOp, 0, TLO.DAG, DL, ExtSizeInBits); | |||
40147 | return TLO.CombineTo(Op, Insert); | |||
40148 | } | |||
40149 | // Zero upper elements. | |||
40150 | case X86ISD::VZEXT_MOVL: | |||
40151 | // Target unary shuffles by immediate: | |||
40152 | case X86ISD::PSHUFD: | |||
40153 | case X86ISD::PSHUFLW: | |||
40154 | case X86ISD::PSHUFHW: | |||
40155 | case X86ISD::VPERMILPI: | |||
40156 | // (Non-Lane Crossing) Target Shuffles. | |||
40157 | case X86ISD::VPERMILPV: | |||
40158 | case X86ISD::VPERMIL2: | |||
40159 | case X86ISD::PSHUFB: | |||
40160 | case X86ISD::UNPCKL: | |||
40161 | case X86ISD::UNPCKH: | |||
40162 | case X86ISD::BLENDI: | |||
40163 | // Integer ops. | |||
40164 | case X86ISD::AVG: | |||
40165 | case X86ISD::PACKSS: | |||
40166 | case X86ISD::PACKUS: | |||
40167 | // Horizontal Ops. | |||
40168 | case X86ISD::HADD: | |||
40169 | case X86ISD::HSUB: | |||
40170 | case X86ISD::FHADD: | |||
40171 | case X86ISD::FHSUB: { | |||
40172 | SDLoc DL(Op); | |||
40173 | SmallVector<SDValue, 4> Ops; | |||
40174 | for (unsigned i = 0, e = Op.getNumOperands(); i != e; ++i) { | |||
40175 | SDValue SrcOp = Op.getOperand(i); | |||
40176 | EVT SrcVT = SrcOp.getValueType(); | |||
40177 | assert((!SrcVT.isVector() || SrcVT.getSizeInBits() == SizeInBits) &&(static_cast <bool> ((!SrcVT.isVector() || SrcVT.getSizeInBits () == SizeInBits) && "Unsupported vector size") ? void (0) : __assert_fail ("(!SrcVT.isVector() || SrcVT.getSizeInBits() == SizeInBits) && \"Unsupported vector size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 40178, __extension__ __PRETTY_FUNCTION__)) | |||
40178 | "Unsupported vector size")(static_cast <bool> ((!SrcVT.isVector() || SrcVT.getSizeInBits () == SizeInBits) && "Unsupported vector size") ? void (0) : __assert_fail ("(!SrcVT.isVector() || SrcVT.getSizeInBits() == SizeInBits) && \"Unsupported vector size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 40178, __extension__ __PRETTY_FUNCTION__)); | |||
40179 | Ops.push_back(SrcVT.isVector() ? extractSubVector(SrcOp, 0, TLO.DAG, DL, | |||
40180 | ExtSizeInBits) | |||
40181 | : SrcOp); | |||
40182 | } | |||
40183 | MVT ExtVT = VT.getSimpleVT(); | |||
40184 | ExtVT = MVT::getVectorVT(ExtVT.getScalarType(), | |||
40185 | ExtSizeInBits / ExtVT.getScalarSizeInBits()); | |||
40186 | SDValue ExtOp = TLO.DAG.getNode(Opc, DL, ExtVT, Ops); | |||
40187 | SDValue UndefVec = TLO.DAG.getUNDEF(VT); | |||
40188 | SDValue Insert = | |||
40189 | insertSubVector(UndefVec, ExtOp, 0, TLO.DAG, DL, ExtSizeInBits); | |||
40190 | return TLO.CombineTo(Op, Insert); | |||
40191 | } | |||
40192 | } | |||
40193 | } | |||
40194 | ||||
40195 | // For broadcasts, unless we *only* demand the 0'th element, | |||
40196 | // stop attempts at simplification here, we aren't going to improve things, | |||
40197 | // this is better than any potential shuffle. | |||
40198 | if (isTargetShuffleSplat(Op) && !DemandedElts.isOne()) | |||
40199 | return false; | |||
40200 | ||||
40201 | // Get target/faux shuffle mask. | |||
40202 | APInt OpUndef, OpZero; | |||
40203 | SmallVector<int, 64> OpMask; | |||
40204 | SmallVector<SDValue, 2> OpInputs; | |||
40205 | if (!getTargetShuffleInputs(Op, DemandedElts, OpInputs, OpMask, OpUndef, | |||
40206 | OpZero, TLO.DAG, Depth, false)) | |||
40207 | return false; | |||
40208 | ||||
40209 | // Shuffle inputs must be the same size as the result. | |||
40210 | if (OpMask.size() != (unsigned)NumElts || | |||
40211 | llvm::any_of(OpInputs, [VT](SDValue V) { | |||
40212 | return VT.getSizeInBits() != V.getValueSizeInBits() || | |||
40213 | !V.getValueType().isVector(); | |||
40214 | })) | |||
40215 | return false; | |||
40216 | ||||
40217 | KnownZero = OpZero; | |||
40218 | KnownUndef = OpUndef; | |||
40219 | ||||
40220 | // Check if shuffle mask can be simplified to undef/zero/identity. | |||
40221 | int NumSrcs = OpInputs.size(); | |||
40222 | for (int i = 0; i != NumElts; ++i) | |||
40223 | if (!DemandedElts[i]) | |||
40224 | OpMask[i] = SM_SentinelUndef; | |||
40225 | ||||
40226 | if (isUndefInRange(OpMask, 0, NumElts)) { | |||
40227 | KnownUndef.setAllBits(); | |||
40228 | return TLO.CombineTo(Op, TLO.DAG.getUNDEF(VT)); | |||
40229 | } | |||
40230 | if (isUndefOrZeroInRange(OpMask, 0, NumElts)) { | |||
40231 | KnownZero.setAllBits(); | |||
40232 | return TLO.CombineTo( | |||
40233 | Op, getZeroVector(VT.getSimpleVT(), Subtarget, TLO.DAG, SDLoc(Op))); | |||
40234 | } | |||
40235 | for (int Src = 0; Src != NumSrcs; ++Src) | |||
40236 | if (isSequentialOrUndefInRange(OpMask, 0, NumElts, Src * NumElts)) | |||
40237 | return TLO.CombineTo(Op, TLO.DAG.getBitcast(VT, OpInputs[Src])); | |||
40238 | ||||
40239 | // Attempt to simplify inputs. | |||
40240 | for (int Src = 0; Src != NumSrcs; ++Src) { | |||
40241 | // TODO: Support inputs of different types. | |||
40242 | if (OpInputs[Src].getValueType() != VT) | |||
40243 | continue; | |||
40244 | ||||
40245 | int Lo = Src * NumElts; | |||
40246 | APInt SrcElts = APInt::getZero(NumElts); | |||
40247 | for (int i = 0; i != NumElts; ++i) | |||
40248 | if (DemandedElts[i]) { | |||
40249 | int M = OpMask[i] - Lo; | |||
40250 | if (0 <= M && M < NumElts) | |||
40251 | SrcElts.setBit(M); | |||
40252 | } | |||
40253 | ||||
40254 | // TODO - Propagate input undef/zero elts. | |||
40255 | APInt SrcUndef, SrcZero; | |||
40256 | if (SimplifyDemandedVectorElts(OpInputs[Src], SrcElts, SrcUndef, SrcZero, | |||
40257 | TLO, Depth + 1)) | |||
40258 | return true; | |||
40259 | } | |||
40260 | ||||
40261 | // If we don't demand all elements, then attempt to combine to a simpler | |||
40262 | // shuffle. | |||
40263 | // We need to convert the depth to something combineX86ShufflesRecursively | |||
40264 | // can handle - so pretend its Depth == 0 again, and reduce the max depth | |||
40265 | // to match. This prevents combineX86ShuffleChain from returning a | |||
40266 | // combined shuffle that's the same as the original root, causing an | |||
40267 | // infinite loop. | |||
40268 | if (!DemandedElts.isAllOnes()) { | |||
40269 | assert(Depth < X86::MaxShuffleCombineDepth && "Depth out of range")(static_cast <bool> (Depth < X86::MaxShuffleCombineDepth && "Depth out of range") ? void (0) : __assert_fail ( "Depth < X86::MaxShuffleCombineDepth && \"Depth out of range\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 40269, __extension__ __PRETTY_FUNCTION__)); | |||
40270 | ||||
40271 | SmallVector<int, 64> DemandedMask(NumElts, SM_SentinelUndef); | |||
40272 | for (int i = 0; i != NumElts; ++i) | |||
40273 | if (DemandedElts[i]) | |||
40274 | DemandedMask[i] = i; | |||
40275 | ||||
40276 | SDValue NewShuffle = combineX86ShufflesRecursively( | |||
40277 | {Op}, 0, Op, DemandedMask, {}, 0, X86::MaxShuffleCombineDepth - Depth, | |||
40278 | /*HasVarMask*/ false, | |||
40279 | /*AllowCrossLaneVarMask*/ true, /*AllowPerLaneVarMask*/ true, TLO.DAG, | |||
40280 | Subtarget); | |||
40281 | if (NewShuffle) | |||
40282 | return TLO.CombineTo(Op, NewShuffle); | |||
40283 | } | |||
40284 | ||||
40285 | return false; | |||
40286 | } | |||
40287 | ||||
40288 | bool X86TargetLowering::SimplifyDemandedBitsForTargetNode( | |||
40289 | SDValue Op, const APInt &OriginalDemandedBits, | |||
40290 | const APInt &OriginalDemandedElts, KnownBits &Known, TargetLoweringOpt &TLO, | |||
40291 | unsigned Depth) const { | |||
40292 | EVT VT = Op.getValueType(); | |||
40293 | unsigned BitWidth = OriginalDemandedBits.getBitWidth(); | |||
40294 | unsigned Opc = Op.getOpcode(); | |||
40295 | switch(Opc) { | |||
40296 | case X86ISD::VTRUNC: { | |||
40297 | KnownBits KnownOp; | |||
40298 | SDValue Src = Op.getOperand(0); | |||
40299 | MVT SrcVT = Src.getSimpleValueType(); | |||
40300 | ||||
40301 | // Simplify the input, using demanded bit information. | |||
40302 | APInt TruncMask = OriginalDemandedBits.zext(SrcVT.getScalarSizeInBits()); | |||
40303 | APInt DemandedElts = OriginalDemandedElts.trunc(SrcVT.getVectorNumElements()); | |||
40304 | if (SimplifyDemandedBits(Src, TruncMask, DemandedElts, KnownOp, TLO, Depth + 1)) | |||
40305 | return true; | |||
40306 | break; | |||
40307 | } | |||
40308 | case X86ISD::PMULDQ: | |||
40309 | case X86ISD::PMULUDQ: { | |||
40310 | // PMULDQ/PMULUDQ only uses lower 32 bits from each vector element. | |||
40311 | KnownBits KnownOp; | |||
40312 | SDValue LHS = Op.getOperand(0); | |||
40313 | SDValue RHS = Op.getOperand(1); | |||
40314 | // FIXME: Can we bound this better? | |||
40315 | APInt DemandedMask = APInt::getLowBitsSet(64, 32); | |||
40316 | if (SimplifyDemandedBits(LHS, DemandedMask, OriginalDemandedElts, KnownOp, | |||
40317 | TLO, Depth + 1)) | |||
40318 | return true; | |||
40319 | if (SimplifyDemandedBits(RHS, DemandedMask, OriginalDemandedElts, KnownOp, | |||
40320 | TLO, Depth + 1)) | |||
40321 | return true; | |||
40322 | ||||
40323 | // Aggressively peek through ops to get at the demanded low bits. | |||
40324 | SDValue DemandedLHS = SimplifyMultipleUseDemandedBits( | |||
40325 | LHS, DemandedMask, OriginalDemandedElts, TLO.DAG, Depth + 1); | |||
40326 | SDValue DemandedRHS = SimplifyMultipleUseDemandedBits( | |||
40327 | RHS, DemandedMask, OriginalDemandedElts, TLO.DAG, Depth + 1); | |||
40328 | if (DemandedLHS || DemandedRHS) { | |||
40329 | DemandedLHS = DemandedLHS ? DemandedLHS : LHS; | |||
40330 | DemandedRHS = DemandedRHS ? DemandedRHS : RHS; | |||
40331 | return TLO.CombineTo( | |||
40332 | Op, TLO.DAG.getNode(Opc, SDLoc(Op), VT, DemandedLHS, DemandedRHS)); | |||
40333 | } | |||
40334 | break; | |||
40335 | } | |||
40336 | case X86ISD::VSHLI: { | |||
40337 | SDValue Op0 = Op.getOperand(0); | |||
40338 | ||||
40339 | unsigned ShAmt = Op.getConstantOperandVal(1); | |||
40340 | if (ShAmt >= BitWidth) | |||
40341 | break; | |||
40342 | ||||
40343 | APInt DemandedMask = OriginalDemandedBits.lshr(ShAmt); | |||
40344 | ||||
40345 | // If this is ((X >>u C1) << ShAmt), see if we can simplify this into a | |||
40346 | // single shift. We can do this if the bottom bits (which are shifted | |||
40347 | // out) are never demanded. | |||
40348 | if (Op0.getOpcode() == X86ISD::VSRLI && | |||
40349 | OriginalDemandedBits.countTrailingZeros() >= ShAmt) { | |||
40350 | unsigned Shift2Amt = Op0.getConstantOperandVal(1); | |||
40351 | if (Shift2Amt < BitWidth) { | |||
40352 | int Diff = ShAmt - Shift2Amt; | |||
40353 | if (Diff == 0) | |||
40354 | return TLO.CombineTo(Op, Op0.getOperand(0)); | |||
40355 | ||||
40356 | unsigned NewOpc = Diff < 0 ? X86ISD::VSRLI : X86ISD::VSHLI; | |||
40357 | SDValue NewShift = TLO.DAG.getNode( | |||
40358 | NewOpc, SDLoc(Op), VT, Op0.getOperand(0), | |||
40359 | TLO.DAG.getTargetConstant(std::abs(Diff), SDLoc(Op), MVT::i8)); | |||
40360 | return TLO.CombineTo(Op, NewShift); | |||
40361 | } | |||
40362 | } | |||
40363 | ||||
40364 | // If we are only demanding sign bits then we can use the shift source directly. | |||
40365 | unsigned NumSignBits = | |||
40366 | TLO.DAG.ComputeNumSignBits(Op0, OriginalDemandedElts, Depth + 1); | |||
40367 | unsigned UpperDemandedBits = | |||
40368 | BitWidth - OriginalDemandedBits.countTrailingZeros(); | |||
40369 | if (NumSignBits > ShAmt && (NumSignBits - ShAmt) >= UpperDemandedBits) | |||
40370 | return TLO.CombineTo(Op, Op0); | |||
40371 | ||||
40372 | if (SimplifyDemandedBits(Op0, DemandedMask, OriginalDemandedElts, Known, | |||
40373 | TLO, Depth + 1)) | |||
40374 | return true; | |||
40375 | ||||
40376 | assert(!Known.hasConflict() && "Bits known to be one AND zero?")(static_cast <bool> (!Known.hasConflict() && "Bits known to be one AND zero?" ) ? void (0) : __assert_fail ("!Known.hasConflict() && \"Bits known to be one AND zero?\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 40376, __extension__ __PRETTY_FUNCTION__)); | |||
40377 | Known.Zero <<= ShAmt; | |||
40378 | Known.One <<= ShAmt; | |||
40379 | ||||
40380 | // Low bits known zero. | |||
40381 | Known.Zero.setLowBits(ShAmt); | |||
40382 | return false; | |||
40383 | } | |||
40384 | case X86ISD::VSRLI: { | |||
40385 | unsigned ShAmt = Op.getConstantOperandVal(1); | |||
40386 | if (ShAmt >= BitWidth) | |||
40387 | break; | |||
40388 | ||||
40389 | APInt DemandedMask = OriginalDemandedBits << ShAmt; | |||
40390 | ||||
40391 | if (SimplifyDemandedBits(Op.getOperand(0), DemandedMask, | |||
40392 | OriginalDemandedElts, Known, TLO, Depth + 1)) | |||
40393 | return true; | |||
40394 | ||||
40395 | assert(!Known.hasConflict() && "Bits known to be one AND zero?")(static_cast <bool> (!Known.hasConflict() && "Bits known to be one AND zero?" ) ? void (0) : __assert_fail ("!Known.hasConflict() && \"Bits known to be one AND zero?\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 40395, __extension__ __PRETTY_FUNCTION__)); | |||
40396 | Known.Zero.lshrInPlace(ShAmt); | |||
40397 | Known.One.lshrInPlace(ShAmt); | |||
40398 | ||||
40399 | // High bits known zero. | |||
40400 | Known.Zero.setHighBits(ShAmt); | |||
40401 | return false; | |||
40402 | } | |||
40403 | case X86ISD::VSRAI: { | |||
40404 | SDValue Op0 = Op.getOperand(0); | |||
40405 | SDValue Op1 = Op.getOperand(1); | |||
40406 | ||||
40407 | unsigned ShAmt = cast<ConstantSDNode>(Op1)->getZExtValue(); | |||
40408 | if (ShAmt >= BitWidth) | |||
40409 | break; | |||
40410 | ||||
40411 | APInt DemandedMask = OriginalDemandedBits << ShAmt; | |||
40412 | ||||
40413 | // If we just want the sign bit then we don't need to shift it. | |||
40414 | if (OriginalDemandedBits.isSignMask()) | |||
40415 | return TLO.CombineTo(Op, Op0); | |||
40416 | ||||
40417 | // fold (VSRAI (VSHLI X, C1), C1) --> X iff NumSignBits(X) > C1 | |||
40418 | if (Op0.getOpcode() == X86ISD::VSHLI && | |||
40419 | Op.getOperand(1) == Op0.getOperand(1)) { | |||
40420 | SDValue Op00 = Op0.getOperand(0); | |||
40421 | unsigned NumSignBits = | |||
40422 | TLO.DAG.ComputeNumSignBits(Op00, OriginalDemandedElts); | |||
40423 | if (ShAmt < NumSignBits) | |||
40424 | return TLO.CombineTo(Op, Op00); | |||
40425 | } | |||
40426 | ||||
40427 | // If any of the demanded bits are produced by the sign extension, we also | |||
40428 | // demand the input sign bit. | |||
40429 | if (OriginalDemandedBits.countLeadingZeros() < ShAmt) | |||
40430 | DemandedMask.setSignBit(); | |||
40431 | ||||
40432 | if (SimplifyDemandedBits(Op0, DemandedMask, OriginalDemandedElts, Known, | |||
40433 | TLO, Depth + 1)) | |||
40434 | return true; | |||
40435 | ||||
40436 | assert(!Known.hasConflict() && "Bits known to be one AND zero?")(static_cast <bool> (!Known.hasConflict() && "Bits known to be one AND zero?" ) ? void (0) : __assert_fail ("!Known.hasConflict() && \"Bits known to be one AND zero?\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 40436, __extension__ __PRETTY_FUNCTION__)); | |||
40437 | Known.Zero.lshrInPlace(ShAmt); | |||
40438 | Known.One.lshrInPlace(ShAmt); | |||
40439 | ||||
40440 | // If the input sign bit is known to be zero, or if none of the top bits | |||
40441 | // are demanded, turn this into an unsigned shift right. | |||
40442 | if (Known.Zero[BitWidth - ShAmt - 1] || | |||
40443 | OriginalDemandedBits.countLeadingZeros() >= ShAmt) | |||
40444 | return TLO.CombineTo( | |||
40445 | Op, TLO.DAG.getNode(X86ISD::VSRLI, SDLoc(Op), VT, Op0, Op1)); | |||
40446 | ||||
40447 | // High bits are known one. | |||
40448 | if (Known.One[BitWidth - ShAmt - 1]) | |||
40449 | Known.One.setHighBits(ShAmt); | |||
40450 | return false; | |||
40451 | } | |||
40452 | case X86ISD::PEXTRB: | |||
40453 | case X86ISD::PEXTRW: { | |||
40454 | SDValue Vec = Op.getOperand(0); | |||
40455 | auto *CIdx = dyn_cast<ConstantSDNode>(Op.getOperand(1)); | |||
40456 | MVT VecVT = Vec.getSimpleValueType(); | |||
40457 | unsigned NumVecElts = VecVT.getVectorNumElements(); | |||
40458 | ||||
40459 | if (CIdx && CIdx->getAPIntValue().ult(NumVecElts)) { | |||
40460 | unsigned Idx = CIdx->getZExtValue(); | |||
40461 | unsigned VecBitWidth = VecVT.getScalarSizeInBits(); | |||
40462 | ||||
40463 | // If we demand no bits from the vector then we must have demanded | |||
40464 | // bits from the implict zext - simplify to zero. | |||
40465 | APInt DemandedVecBits = OriginalDemandedBits.trunc(VecBitWidth); | |||
40466 | if (DemandedVecBits == 0) | |||
40467 | return TLO.CombineTo(Op, TLO.DAG.getConstant(0, SDLoc(Op), VT)); | |||
40468 | ||||
40469 | APInt KnownUndef, KnownZero; | |||
40470 | APInt DemandedVecElts = APInt::getOneBitSet(NumVecElts, Idx); | |||
40471 | if (SimplifyDemandedVectorElts(Vec, DemandedVecElts, KnownUndef, | |||
40472 | KnownZero, TLO, Depth + 1)) | |||
40473 | return true; | |||
40474 | ||||
40475 | KnownBits KnownVec; | |||
40476 | if (SimplifyDemandedBits(Vec, DemandedVecBits, DemandedVecElts, | |||
40477 | KnownVec, TLO, Depth + 1)) | |||
40478 | return true; | |||
40479 | ||||
40480 | if (SDValue V = SimplifyMultipleUseDemandedBits( | |||
40481 | Vec, DemandedVecBits, DemandedVecElts, TLO.DAG, Depth + 1)) | |||
40482 | return TLO.CombineTo( | |||
40483 | Op, TLO.DAG.getNode(Opc, SDLoc(Op), VT, V, Op.getOperand(1))); | |||
40484 | ||||
40485 | Known = KnownVec.zext(BitWidth); | |||
40486 | return false; | |||
40487 | } | |||
40488 | break; | |||
40489 | } | |||
40490 | case X86ISD::PINSRB: | |||
40491 | case X86ISD::PINSRW: { | |||
40492 | SDValue Vec = Op.getOperand(0); | |||
40493 | SDValue Scl = Op.getOperand(1); | |||
40494 | auto *CIdx = dyn_cast<ConstantSDNode>(Op.getOperand(2)); | |||
40495 | MVT VecVT = Vec.getSimpleValueType(); | |||
40496 | ||||
40497 | if (CIdx && CIdx->getAPIntValue().ult(VecVT.getVectorNumElements())) { | |||
40498 | unsigned Idx = CIdx->getZExtValue(); | |||
40499 | if (!OriginalDemandedElts[Idx]) | |||
40500 | return TLO.CombineTo(Op, Vec); | |||
40501 | ||||
40502 | KnownBits KnownVec; | |||
40503 | APInt DemandedVecElts(OriginalDemandedElts); | |||
40504 | DemandedVecElts.clearBit(Idx); | |||
40505 | if (SimplifyDemandedBits(Vec, OriginalDemandedBits, DemandedVecElts, | |||
40506 | KnownVec, TLO, Depth + 1)) | |||
40507 | return true; | |||
40508 | ||||
40509 | KnownBits KnownScl; | |||
40510 | unsigned NumSclBits = Scl.getScalarValueSizeInBits(); | |||
40511 | APInt DemandedSclBits = OriginalDemandedBits.zext(NumSclBits); | |||
40512 | if (SimplifyDemandedBits(Scl, DemandedSclBits, KnownScl, TLO, Depth + 1)) | |||
40513 | return true; | |||
40514 | ||||
40515 | KnownScl = KnownScl.trunc(VecVT.getScalarSizeInBits()); | |||
40516 | Known = KnownBits::commonBits(KnownVec, KnownScl); | |||
40517 | return false; | |||
40518 | } | |||
40519 | break; | |||
40520 | } | |||
40521 | case X86ISD::PACKSS: | |||
40522 | // PACKSS saturates to MIN/MAX integer values. So if we just want the | |||
40523 | // sign bit then we can just ask for the source operands sign bit. | |||
40524 | // TODO - add known bits handling. | |||
40525 | if (OriginalDemandedBits.isSignMask()) { | |||
40526 | APInt DemandedLHS, DemandedRHS; | |||
40527 | getPackDemandedElts(VT, OriginalDemandedElts, DemandedLHS, DemandedRHS); | |||
40528 | ||||
40529 | KnownBits KnownLHS, KnownRHS; | |||
40530 | APInt SignMask = APInt::getSignMask(BitWidth * 2); | |||
40531 | if (SimplifyDemandedBits(Op.getOperand(0), SignMask, DemandedLHS, | |||
40532 | KnownLHS, TLO, Depth + 1)) | |||
40533 | return true; | |||
40534 | if (SimplifyDemandedBits(Op.getOperand(1), SignMask, DemandedRHS, | |||
40535 | KnownRHS, TLO, Depth + 1)) | |||
40536 | return true; | |||
40537 | ||||
40538 | // Attempt to avoid multi-use ops if we don't need anything from them. | |||
40539 | SDValue DemandedOp0 = SimplifyMultipleUseDemandedBits( | |||
40540 | Op.getOperand(0), SignMask, DemandedLHS, TLO.DAG, Depth + 1); | |||
40541 | SDValue DemandedOp1 = SimplifyMultipleUseDemandedBits( | |||
40542 | Op.getOperand(1), SignMask, DemandedRHS, TLO.DAG, Depth + 1); | |||
40543 | if (DemandedOp0 || DemandedOp1) { | |||
40544 | SDValue Op0 = DemandedOp0 ? DemandedOp0 : Op.getOperand(0); | |||
40545 | SDValue Op1 = DemandedOp1 ? DemandedOp1 : Op.getOperand(1); | |||
40546 | return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, SDLoc(Op), VT, Op0, Op1)); | |||
40547 | } | |||
40548 | } | |||
40549 | // TODO - add general PACKSS/PACKUS SimplifyDemandedBits support. | |||
40550 | break; | |||
40551 | case X86ISD::VBROADCAST: { | |||
40552 | SDValue Src = Op.getOperand(0); | |||
40553 | MVT SrcVT = Src.getSimpleValueType(); | |||
40554 | APInt DemandedElts = APInt::getOneBitSet( | |||
40555 | SrcVT.isVector() ? SrcVT.getVectorNumElements() : 1, 0); | |||
40556 | if (SimplifyDemandedBits(Src, OriginalDemandedBits, DemandedElts, Known, | |||
40557 | TLO, Depth + 1)) | |||
40558 | return true; | |||
40559 | // If we don't need the upper bits, attempt to narrow the broadcast source. | |||
40560 | // Don't attempt this on AVX512 as it might affect broadcast folding. | |||
40561 | // TODO: Should we attempt this for i32/i16 splats? They tend to be slower. | |||
40562 | if ((BitWidth == 64) && SrcVT.isScalarInteger() && !Subtarget.hasAVX512() && | |||
40563 | OriginalDemandedBits.countLeadingZeros() >= (BitWidth / 2) && | |||
40564 | Src->hasOneUse()) { | |||
40565 | MVT NewSrcVT = MVT::getIntegerVT(BitWidth / 2); | |||
40566 | SDValue NewSrc = | |||
40567 | TLO.DAG.getNode(ISD::TRUNCATE, SDLoc(Src), NewSrcVT, Src); | |||
40568 | MVT NewVT = MVT::getVectorVT(NewSrcVT, VT.getVectorNumElements() * 2); | |||
40569 | SDValue NewBcst = | |||
40570 | TLO.DAG.getNode(X86ISD::VBROADCAST, SDLoc(Op), NewVT, NewSrc); | |||
40571 | return TLO.CombineTo(Op, TLO.DAG.getBitcast(VT, NewBcst)); | |||
40572 | } | |||
40573 | break; | |||
40574 | } | |||
40575 | case X86ISD::PCMPGT: | |||
40576 | // icmp sgt(0, R) == ashr(R, BitWidth-1). | |||
40577 | // iff we only need the sign bit then we can use R directly. | |||
40578 | if (OriginalDemandedBits.isSignMask() && | |||
40579 | ISD::isBuildVectorAllZeros(Op.getOperand(0).getNode())) | |||
40580 | return TLO.CombineTo(Op, Op.getOperand(1)); | |||
40581 | break; | |||
40582 | case X86ISD::MOVMSK: { | |||
40583 | SDValue Src = Op.getOperand(0); | |||
40584 | MVT SrcVT = Src.getSimpleValueType(); | |||
40585 | unsigned SrcBits = SrcVT.getScalarSizeInBits(); | |||
40586 | unsigned NumElts = SrcVT.getVectorNumElements(); | |||
40587 | ||||
40588 | // If we don't need the sign bits at all just return zero. | |||
40589 | if (OriginalDemandedBits.countTrailingZeros() >= NumElts) | |||
40590 | return TLO.CombineTo(Op, TLO.DAG.getConstant(0, SDLoc(Op), VT)); | |||
40591 | ||||
40592 | // Only demand the vector elements of the sign bits we need. | |||
40593 | APInt KnownUndef, KnownZero; | |||
40594 | APInt DemandedElts = OriginalDemandedBits.zextOrTrunc(NumElts); | |||
40595 | if (SimplifyDemandedVectorElts(Src, DemandedElts, KnownUndef, KnownZero, | |||
40596 | TLO, Depth + 1)) | |||
40597 | return true; | |||
40598 | ||||
40599 | Known.Zero = KnownZero.zextOrSelf(BitWidth); | |||
40600 | Known.Zero.setHighBits(BitWidth - NumElts); | |||
40601 | ||||
40602 | // MOVMSK only uses the MSB from each vector element. | |||
40603 | KnownBits KnownSrc; | |||
40604 | APInt DemandedSrcBits = APInt::getSignMask(SrcBits); | |||
40605 | if (SimplifyDemandedBits(Src, DemandedSrcBits, DemandedElts, KnownSrc, TLO, | |||
40606 | Depth + 1)) | |||
40607 | return true; | |||
40608 | ||||
40609 | if (KnownSrc.One[SrcBits - 1]) | |||
40610 | Known.One.setLowBits(NumElts); | |||
40611 | else if (KnownSrc.Zero[SrcBits - 1]) | |||
40612 | Known.Zero.setLowBits(NumElts); | |||
40613 | ||||
40614 | // Attempt to avoid multi-use os if we don't need anything from it. | |||
40615 | if (SDValue NewSrc = SimplifyMultipleUseDemandedBits( | |||
40616 | Src, DemandedSrcBits, DemandedElts, TLO.DAG, Depth + 1)) | |||
40617 | return TLO.CombineTo(Op, TLO.DAG.getNode(Opc, SDLoc(Op), VT, NewSrc)); | |||
40618 | return false; | |||
40619 | } | |||
40620 | case X86ISD::BEXTR: | |||
40621 | case X86ISD::BEXTRI: { | |||
40622 | SDValue Op0 = Op.getOperand(0); | |||
40623 | SDValue Op1 = Op.getOperand(1); | |||
40624 | ||||
40625 | // Only bottom 16-bits of the control bits are required. | |||
40626 | if (auto *Cst1 = dyn_cast<ConstantSDNode>(Op1)) { | |||
40627 | // NOTE: SimplifyDemandedBits won't do this for constants. | |||
40628 | uint64_t Val1 = Cst1->getZExtValue(); | |||
40629 | uint64_t MaskedVal1 = Val1 & 0xFFFF; | |||
40630 | if (Opc == X86ISD::BEXTR && MaskedVal1 != Val1) { | |||
40631 | SDLoc DL(Op); | |||
40632 | return TLO.CombineTo( | |||
40633 | Op, TLO.DAG.getNode(X86ISD::BEXTR, DL, VT, Op0, | |||
40634 | TLO.DAG.getConstant(MaskedVal1, DL, VT))); | |||
40635 | } | |||
40636 | ||||
40637 | unsigned Shift = Cst1->getAPIntValue().extractBitsAsZExtValue(8, 0); | |||
40638 | unsigned Length = Cst1->getAPIntValue().extractBitsAsZExtValue(8, 8); | |||
40639 | ||||
40640 | // If the length is 0, the result is 0. | |||
40641 | if (Length == 0) { | |||
40642 | Known.setAllZero(); | |||
40643 | return false; | |||
40644 | } | |||
40645 | ||||
40646 | if ((Shift + Length) <= BitWidth) { | |||
40647 | APInt DemandedMask = APInt::getBitsSet(BitWidth, Shift, Shift + Length); | |||
40648 | if (SimplifyDemandedBits(Op0, DemandedMask, Known, TLO, Depth + 1)) | |||
40649 | return true; | |||
40650 | ||||
40651 | Known = Known.extractBits(Length, Shift); | |||
40652 | Known = Known.zextOrTrunc(BitWidth); | |||
40653 | return false; | |||
40654 | } | |||
40655 | } else { | |||
40656 | assert(Opc == X86ISD::BEXTR && "Unexpected opcode!")(static_cast <bool> (Opc == X86ISD::BEXTR && "Unexpected opcode!" ) ? void (0) : __assert_fail ("Opc == X86ISD::BEXTR && \"Unexpected opcode!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 40656, __extension__ __PRETTY_FUNCTION__)); | |||
40657 | KnownBits Known1; | |||
40658 | APInt DemandedMask(APInt::getLowBitsSet(BitWidth, 16)); | |||
40659 | if (SimplifyDemandedBits(Op1, DemandedMask, Known1, TLO, Depth + 1)) | |||
40660 | return true; | |||
40661 | ||||
40662 | // If the length is 0, replace with 0. | |||
40663 | KnownBits LengthBits = Known1.extractBits(8, 8); | |||
40664 | if (LengthBits.isZero()) | |||
40665 | return TLO.CombineTo(Op, TLO.DAG.getConstant(0, SDLoc(Op), VT)); | |||
40666 | } | |||
40667 | ||||
40668 | break; | |||
40669 | } | |||
40670 | case X86ISD::PDEP: { | |||
40671 | SDValue Op0 = Op.getOperand(0); | |||
40672 | SDValue Op1 = Op.getOperand(1); | |||
40673 | ||||
40674 | unsigned DemandedBitsLZ = OriginalDemandedBits.countLeadingZeros(); | |||
40675 | APInt LoMask = APInt::getLowBitsSet(BitWidth, BitWidth - DemandedBitsLZ); | |||
40676 | ||||
40677 | // If the demanded bits has leading zeroes, we don't demand those from the | |||
40678 | // mask. | |||
40679 | if (SimplifyDemandedBits(Op1, LoMask, Known, TLO, Depth + 1)) | |||
40680 | return true; | |||
40681 | ||||
40682 | // The number of possible 1s in the mask determines the number of LSBs of | |||
40683 | // operand 0 used. Undemanded bits from the mask don't matter so filter | |||
40684 | // them before counting. | |||
40685 | KnownBits Known2; | |||
40686 | uint64_t Count = (~Known.Zero & LoMask).countPopulation(); | |||
40687 | APInt DemandedMask(APInt::getLowBitsSet(BitWidth, Count)); | |||
40688 | if (SimplifyDemandedBits(Op0, DemandedMask, Known2, TLO, Depth + 1)) | |||
40689 | return true; | |||
40690 | ||||
40691 | // Zeroes are retained from the mask, but not ones. | |||
40692 | Known.One.clearAllBits(); | |||
40693 | // The result will have at least as many trailing zeros as the non-mask | |||
40694 | // operand since bits can only map to the same or higher bit position. | |||
40695 | Known.Zero.setLowBits(Known2.countMinTrailingZeros()); | |||
40696 | return false; | |||
40697 | } | |||
40698 | } | |||
40699 | ||||
40700 | return TargetLowering::SimplifyDemandedBitsForTargetNode( | |||
40701 | Op, OriginalDemandedBits, OriginalDemandedElts, Known, TLO, Depth); | |||
40702 | } | |||
40703 | ||||
40704 | SDValue X86TargetLowering::SimplifyMultipleUseDemandedBitsForTargetNode( | |||
40705 | SDValue Op, const APInt &DemandedBits, const APInt &DemandedElts, | |||
40706 | SelectionDAG &DAG, unsigned Depth) const { | |||
40707 | int NumElts = DemandedElts.getBitWidth(); | |||
40708 | unsigned Opc = Op.getOpcode(); | |||
40709 | EVT VT = Op.getValueType(); | |||
40710 | ||||
40711 | switch (Opc) { | |||
40712 | case X86ISD::PINSRB: | |||
40713 | case X86ISD::PINSRW: { | |||
40714 | // If we don't demand the inserted element, return the base vector. | |||
40715 | SDValue Vec = Op.getOperand(0); | |||
40716 | auto *CIdx = dyn_cast<ConstantSDNode>(Op.getOperand(2)); | |||
40717 | MVT VecVT = Vec.getSimpleValueType(); | |||
40718 | if (CIdx && CIdx->getAPIntValue().ult(VecVT.getVectorNumElements()) && | |||
40719 | !DemandedElts[CIdx->getZExtValue()]) | |||
40720 | return Vec; | |||
40721 | break; | |||
40722 | } | |||
40723 | case X86ISD::VSHLI: { | |||
40724 | // If we are only demanding sign bits then we can use the shift source | |||
40725 | // directly. | |||
40726 | SDValue Op0 = Op.getOperand(0); | |||
40727 | unsigned ShAmt = Op.getConstantOperandVal(1); | |||
40728 | unsigned BitWidth = DemandedBits.getBitWidth(); | |||
40729 | unsigned NumSignBits = DAG.ComputeNumSignBits(Op0, DemandedElts, Depth + 1); | |||
40730 | unsigned UpperDemandedBits = BitWidth - DemandedBits.countTrailingZeros(); | |||
40731 | if (NumSignBits > ShAmt && (NumSignBits - ShAmt) >= UpperDemandedBits) | |||
40732 | return Op0; | |||
40733 | break; | |||
40734 | } | |||
40735 | case X86ISD::VSRAI: | |||
40736 | // iff we only need the sign bit then we can use the source directly. | |||
40737 | // TODO: generalize where we only demand extended signbits. | |||
40738 | if (DemandedBits.isSignMask()) | |||
40739 | return Op.getOperand(0); | |||
40740 | break; | |||
40741 | case X86ISD::PCMPGT: | |||
40742 | // icmp sgt(0, R) == ashr(R, BitWidth-1). | |||
40743 | // iff we only need the sign bit then we can use R directly. | |||
40744 | if (DemandedBits.isSignMask() && | |||
40745 | ISD::isBuildVectorAllZeros(Op.getOperand(0).getNode())) | |||
40746 | return Op.getOperand(1); | |||
40747 | break; | |||
40748 | } | |||
40749 | ||||
40750 | APInt ShuffleUndef, ShuffleZero; | |||
40751 | SmallVector<int, 16> ShuffleMask; | |||
40752 | SmallVector<SDValue, 2> ShuffleOps; | |||
40753 | if (getTargetShuffleInputs(Op, DemandedElts, ShuffleOps, ShuffleMask, | |||
40754 | ShuffleUndef, ShuffleZero, DAG, Depth, false)) { | |||
40755 | // If all the demanded elts are from one operand and are inline, | |||
40756 | // then we can use the operand directly. | |||
40757 | int NumOps = ShuffleOps.size(); | |||
40758 | if (ShuffleMask.size() == (unsigned)NumElts && | |||
40759 | llvm::all_of(ShuffleOps, [VT](SDValue V) { | |||
40760 | return VT.getSizeInBits() == V.getValueSizeInBits(); | |||
40761 | })) { | |||
40762 | ||||
40763 | if (DemandedElts.isSubsetOf(ShuffleUndef)) | |||
40764 | return DAG.getUNDEF(VT); | |||
40765 | if (DemandedElts.isSubsetOf(ShuffleUndef | ShuffleZero)) | |||
40766 | return getZeroVector(VT.getSimpleVT(), Subtarget, DAG, SDLoc(Op)); | |||
40767 | ||||
40768 | // Bitmask that indicates which ops have only been accessed 'inline'. | |||
40769 | APInt IdentityOp = APInt::getAllOnes(NumOps); | |||
40770 | for (int i = 0; i != NumElts; ++i) { | |||
40771 | int M = ShuffleMask[i]; | |||
40772 | if (!DemandedElts[i] || ShuffleUndef[i]) | |||
40773 | continue; | |||
40774 | int OpIdx = M / NumElts; | |||
40775 | int EltIdx = M % NumElts; | |||
40776 | if (M < 0 || EltIdx != i) { | |||
40777 | IdentityOp.clearAllBits(); | |||
40778 | break; | |||
40779 | } | |||
40780 | IdentityOp &= APInt::getOneBitSet(NumOps, OpIdx); | |||
40781 | if (IdentityOp == 0) | |||
40782 | break; | |||
40783 | } | |||
40784 | assert((IdentityOp == 0 || IdentityOp.countPopulation() == 1) &&(static_cast <bool> ((IdentityOp == 0 || IdentityOp.countPopulation () == 1) && "Multiple identity shuffles detected") ? void (0) : __assert_fail ("(IdentityOp == 0 || IdentityOp.countPopulation() == 1) && \"Multiple identity shuffles detected\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 40785, __extension__ __PRETTY_FUNCTION__)) | |||
40785 | "Multiple identity shuffles detected")(static_cast <bool> ((IdentityOp == 0 || IdentityOp.countPopulation () == 1) && "Multiple identity shuffles detected") ? void (0) : __assert_fail ("(IdentityOp == 0 || IdentityOp.countPopulation() == 1) && \"Multiple identity shuffles detected\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 40785, __extension__ __PRETTY_FUNCTION__)); | |||
40786 | ||||
40787 | if (IdentityOp != 0) | |||
40788 | return DAG.getBitcast(VT, ShuffleOps[IdentityOp.countTrailingZeros()]); | |||
40789 | } | |||
40790 | } | |||
40791 | ||||
40792 | return TargetLowering::SimplifyMultipleUseDemandedBitsForTargetNode( | |||
40793 | Op, DemandedBits, DemandedElts, DAG, Depth); | |||
40794 | } | |||
40795 | ||||
40796 | // Helper to peek through bitops/trunc/setcc to determine size of source vector. | |||
40797 | // Allows combineBitcastvxi1 to determine what size vector generated a <X x i1>. | |||
40798 | static bool checkBitcastSrcVectorSize(SDValue Src, unsigned Size, | |||
40799 | bool AllowTruncate) { | |||
40800 | switch (Src.getOpcode()) { | |||
40801 | case ISD::TRUNCATE: | |||
40802 | if (!AllowTruncate) | |||
40803 | return false; | |||
40804 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
40805 | case ISD::SETCC: | |||
40806 | return Src.getOperand(0).getValueSizeInBits() == Size; | |||
40807 | case ISD::AND: | |||
40808 | case ISD::XOR: | |||
40809 | case ISD::OR: | |||
40810 | return checkBitcastSrcVectorSize(Src.getOperand(0), Size, AllowTruncate) && | |||
40811 | checkBitcastSrcVectorSize(Src.getOperand(1), Size, AllowTruncate); | |||
40812 | } | |||
40813 | return false; | |||
40814 | } | |||
40815 | ||||
40816 | // Helper to flip between AND/OR/XOR opcodes and their X86ISD FP equivalents. | |||
40817 | static unsigned getAltBitOpcode(unsigned Opcode) { | |||
40818 | switch(Opcode) { | |||
40819 | case ISD::AND: return X86ISD::FAND; | |||
40820 | case ISD::OR: return X86ISD::FOR; | |||
40821 | case ISD::XOR: return X86ISD::FXOR; | |||
40822 | case X86ISD::ANDNP: return X86ISD::FANDN; | |||
40823 | } | |||
40824 | llvm_unreachable("Unknown bitwise opcode")::llvm::llvm_unreachable_internal("Unknown bitwise opcode", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 40824); | |||
40825 | } | |||
40826 | ||||
40827 | // Helper to adjust v4i32 MOVMSK expansion to work with SSE1-only targets. | |||
40828 | static SDValue adjustBitcastSrcVectorSSE1(SelectionDAG &DAG, SDValue Src, | |||
40829 | const SDLoc &DL) { | |||
40830 | EVT SrcVT = Src.getValueType(); | |||
40831 | if (SrcVT != MVT::v4i1) | |||
40832 | return SDValue(); | |||
40833 | ||||
40834 | switch (Src.getOpcode()) { | |||
40835 | case ISD::SETCC: | |||
40836 | if (Src.getOperand(0).getValueType() == MVT::v4i32 && | |||
40837 | ISD::isBuildVectorAllZeros(Src.getOperand(1).getNode()) && | |||
40838 | cast<CondCodeSDNode>(Src.getOperand(2))->get() == ISD::SETLT) { | |||
40839 | SDValue Op0 = Src.getOperand(0); | |||
40840 | if (ISD::isNormalLoad(Op0.getNode())) | |||
40841 | return DAG.getBitcast(MVT::v4f32, Op0); | |||
40842 | if (Op0.getOpcode() == ISD::BITCAST && | |||
40843 | Op0.getOperand(0).getValueType() == MVT::v4f32) | |||
40844 | return Op0.getOperand(0); | |||
40845 | } | |||
40846 | break; | |||
40847 | case ISD::AND: | |||
40848 | case ISD::XOR: | |||
40849 | case ISD::OR: { | |||
40850 | SDValue Op0 = adjustBitcastSrcVectorSSE1(DAG, Src.getOperand(0), DL); | |||
40851 | SDValue Op1 = adjustBitcastSrcVectorSSE1(DAG, Src.getOperand(1), DL); | |||
40852 | if (Op0 && Op1) | |||
40853 | return DAG.getNode(getAltBitOpcode(Src.getOpcode()), DL, MVT::v4f32, Op0, | |||
40854 | Op1); | |||
40855 | break; | |||
40856 | } | |||
40857 | } | |||
40858 | return SDValue(); | |||
40859 | } | |||
40860 | ||||
40861 | // Helper to push sign extension of vXi1 SETCC result through bitops. | |||
40862 | static SDValue signExtendBitcastSrcVector(SelectionDAG &DAG, EVT SExtVT, | |||
40863 | SDValue Src, const SDLoc &DL) { | |||
40864 | switch (Src.getOpcode()) { | |||
40865 | case ISD::SETCC: | |||
40866 | case ISD::TRUNCATE: | |||
40867 | return DAG.getNode(ISD::SIGN_EXTEND, DL, SExtVT, Src); | |||
40868 | case ISD::AND: | |||
40869 | case ISD::XOR: | |||
40870 | case ISD::OR: | |||
40871 | return DAG.getNode( | |||
40872 | Src.getOpcode(), DL, SExtVT, | |||
40873 | signExtendBitcastSrcVector(DAG, SExtVT, Src.getOperand(0), DL), | |||
40874 | signExtendBitcastSrcVector(DAG, SExtVT, Src.getOperand(1), DL)); | |||
40875 | } | |||
40876 | llvm_unreachable("Unexpected node type for vXi1 sign extension")::llvm::llvm_unreachable_internal("Unexpected node type for vXi1 sign extension" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 40876); | |||
40877 | } | |||
40878 | ||||
40879 | // Try to match patterns such as | |||
40880 | // (i16 bitcast (v16i1 x)) | |||
40881 | // -> | |||
40882 | // (i16 movmsk (16i8 sext (v16i1 x))) | |||
40883 | // before the illegal vector is scalarized on subtargets that don't have legal | |||
40884 | // vxi1 types. | |||
40885 | static SDValue combineBitcastvxi1(SelectionDAG &DAG, EVT VT, SDValue Src, | |||
40886 | const SDLoc &DL, | |||
40887 | const X86Subtarget &Subtarget) { | |||
40888 | EVT SrcVT = Src.getValueType(); | |||
40889 | if (!SrcVT.isSimple() || SrcVT.getScalarType() != MVT::i1) | |||
40890 | return SDValue(); | |||
40891 | ||||
40892 | // Recognize the IR pattern for the movmsk intrinsic under SSE1 before type | |||
40893 | // legalization destroys the v4i32 type. | |||
40894 | if (Subtarget.hasSSE1() && !Subtarget.hasSSE2()) { | |||
40895 | if (SDValue V = adjustBitcastSrcVectorSSE1(DAG, Src, DL)) { | |||
40896 | V = DAG.getNode(X86ISD::MOVMSK, DL, MVT::i32, | |||
40897 | DAG.getBitcast(MVT::v4f32, V)); | |||
40898 | return DAG.getZExtOrTrunc(V, DL, VT); | |||
40899 | } | |||
40900 | } | |||
40901 | ||||
40902 | // If the input is a truncate from v16i8 or v32i8 go ahead and use a | |||
40903 | // movmskb even with avx512. This will be better than truncating to vXi1 and | |||
40904 | // using a kmov. This can especially help KNL if the input is a v16i8/v32i8 | |||
40905 | // vpcmpeqb/vpcmpgtb. | |||
40906 | bool PreferMovMsk = Src.getOpcode() == ISD::TRUNCATE && Src.hasOneUse() && | |||
40907 | (Src.getOperand(0).getValueType() == MVT::v16i8 || | |||
40908 | Src.getOperand(0).getValueType() == MVT::v32i8 || | |||
40909 | Src.getOperand(0).getValueType() == MVT::v64i8); | |||
40910 | ||||
40911 | // Prefer movmsk for AVX512 for (bitcast (setlt X, 0)) which can be handled | |||
40912 | // directly with vpmovmskb/vmovmskps/vmovmskpd. | |||
40913 | if (Src.getOpcode() == ISD::SETCC && Src.hasOneUse() && | |||
40914 | cast<CondCodeSDNode>(Src.getOperand(2))->get() == ISD::SETLT && | |||
40915 | ISD::isBuildVectorAllZeros(Src.getOperand(1).getNode())) { | |||
40916 | EVT CmpVT = Src.getOperand(0).getValueType(); | |||
40917 | EVT EltVT = CmpVT.getVectorElementType(); | |||
40918 | if (CmpVT.getSizeInBits() <= 256 && | |||
40919 | (EltVT == MVT::i8 || EltVT == MVT::i32 || EltVT == MVT::i64)) | |||
40920 | PreferMovMsk = true; | |||
40921 | } | |||
40922 | ||||
40923 | // With AVX512 vxi1 types are legal and we prefer using k-regs. | |||
40924 | // MOVMSK is supported in SSE2 or later. | |||
40925 | if (!Subtarget.hasSSE2() || (Subtarget.hasAVX512() && !PreferMovMsk)) | |||
40926 | return SDValue(); | |||
40927 | ||||
40928 | // There are MOVMSK flavors for types v16i8, v32i8, v4f32, v8f32, v4f64 and | |||
40929 | // v8f64. So all legal 128-bit and 256-bit vectors are covered except for | |||
40930 | // v8i16 and v16i16. | |||
40931 | // For these two cases, we can shuffle the upper element bytes to a | |||
40932 | // consecutive sequence at the start of the vector and treat the results as | |||
40933 | // v16i8 or v32i8, and for v16i8 this is the preferable solution. However, | |||
40934 | // for v16i16 this is not the case, because the shuffle is expensive, so we | |||
40935 | // avoid sign-extending to this type entirely. | |||
40936 | // For example, t0 := (v8i16 sext(v8i1 x)) needs to be shuffled as: | |||
40937 | // (v16i8 shuffle <0,2,4,6,8,10,12,14,u,u,...,u> (v16i8 bitcast t0), undef) | |||
40938 | MVT SExtVT; | |||
40939 | bool PropagateSExt = false; | |||
40940 | switch (SrcVT.getSimpleVT().SimpleTy) { | |||
40941 | default: | |||
40942 | return SDValue(); | |||
40943 | case MVT::v2i1: | |||
40944 | SExtVT = MVT::v2i64; | |||
40945 | break; | |||
40946 | case MVT::v4i1: | |||
40947 | SExtVT = MVT::v4i32; | |||
40948 | // For cases such as (i4 bitcast (v4i1 setcc v4i64 v1, v2)) | |||
40949 | // sign-extend to a 256-bit operation to avoid truncation. | |||
40950 | if (Subtarget.hasAVX() && | |||
40951 | checkBitcastSrcVectorSize(Src, 256, Subtarget.hasAVX2())) { | |||
40952 | SExtVT = MVT::v4i64; | |||
40953 | PropagateSExt = true; | |||
40954 | } | |||
40955 | break; | |||
40956 | case MVT::v8i1: | |||
40957 | SExtVT = MVT::v8i16; | |||
40958 | // For cases such as (i8 bitcast (v8i1 setcc v8i32 v1, v2)), | |||
40959 | // sign-extend to a 256-bit operation to match the compare. | |||
40960 | // If the setcc operand is 128-bit, prefer sign-extending to 128-bit over | |||
40961 | // 256-bit because the shuffle is cheaper than sign extending the result of | |||
40962 | // the compare. | |||
40963 | if (Subtarget.hasAVX() && (checkBitcastSrcVectorSize(Src, 256, true) || | |||
40964 | checkBitcastSrcVectorSize(Src, 512, true))) { | |||
40965 | SExtVT = MVT::v8i32; | |||
40966 | PropagateSExt = true; | |||
40967 | } | |||
40968 | break; | |||
40969 | case MVT::v16i1: | |||
40970 | SExtVT = MVT::v16i8; | |||
40971 | // For the case (i16 bitcast (v16i1 setcc v16i16 v1, v2)), | |||
40972 | // it is not profitable to sign-extend to 256-bit because this will | |||
40973 | // require an extra cross-lane shuffle which is more expensive than | |||
40974 | // truncating the result of the compare to 128-bits. | |||
40975 | break; | |||
40976 | case MVT::v32i1: | |||
40977 | SExtVT = MVT::v32i8; | |||
40978 | break; | |||
40979 | case MVT::v64i1: | |||
40980 | // If we have AVX512F, but not AVX512BW and the input is truncated from | |||
40981 | // v64i8 checked earlier. Then split the input and make two pmovmskbs. | |||
40982 | if (Subtarget.hasAVX512()) { | |||
40983 | if (Subtarget.hasBWI()) | |||
40984 | return SDValue(); | |||
40985 | SExtVT = MVT::v64i8; | |||
40986 | break; | |||
40987 | } | |||
40988 | // Split if this is a <64 x i8> comparison result. | |||
40989 | if (checkBitcastSrcVectorSize(Src, 512, false)) { | |||
40990 | SExtVT = MVT::v64i8; | |||
40991 | break; | |||
40992 | } | |||
40993 | return SDValue(); | |||
40994 | }; | |||
40995 | ||||
40996 | SDValue V = PropagateSExt ? signExtendBitcastSrcVector(DAG, SExtVT, Src, DL) | |||
40997 | : DAG.getNode(ISD::SIGN_EXTEND, DL, SExtVT, Src); | |||
40998 | ||||
40999 | if (SExtVT == MVT::v16i8 || SExtVT == MVT::v32i8 || SExtVT == MVT::v64i8) { | |||
41000 | V = getPMOVMSKB(DL, V, DAG, Subtarget); | |||
41001 | } else { | |||
41002 | if (SExtVT == MVT::v8i16) | |||
41003 | V = DAG.getNode(X86ISD::PACKSS, DL, MVT::v16i8, V, | |||
41004 | DAG.getUNDEF(MVT::v8i16)); | |||
41005 | V = DAG.getNode(X86ISD::MOVMSK, DL, MVT::i32, V); | |||
41006 | } | |||
41007 | ||||
41008 | EVT IntVT = | |||
41009 | EVT::getIntegerVT(*DAG.getContext(), SrcVT.getVectorNumElements()); | |||
41010 | V = DAG.getZExtOrTrunc(V, DL, IntVT); | |||
41011 | return DAG.getBitcast(VT, V); | |||
41012 | } | |||
41013 | ||||
41014 | // Convert a vXi1 constant build vector to the same width scalar integer. | |||
41015 | static SDValue combinevXi1ConstantToInteger(SDValue Op, SelectionDAG &DAG) { | |||
41016 | EVT SrcVT = Op.getValueType(); | |||
41017 | assert(SrcVT.getVectorElementType() == MVT::i1 &&(static_cast <bool> (SrcVT.getVectorElementType() == MVT ::i1 && "Expected a vXi1 vector") ? void (0) : __assert_fail ("SrcVT.getVectorElementType() == MVT::i1 && \"Expected a vXi1 vector\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 41018, __extension__ __PRETTY_FUNCTION__)) | |||
41018 | "Expected a vXi1 vector")(static_cast <bool> (SrcVT.getVectorElementType() == MVT ::i1 && "Expected a vXi1 vector") ? void (0) : __assert_fail ("SrcVT.getVectorElementType() == MVT::i1 && \"Expected a vXi1 vector\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 41018, __extension__ __PRETTY_FUNCTION__)); | |||
41019 | assert(ISD::isBuildVectorOfConstantSDNodes(Op.getNode()) &&(static_cast <bool> (ISD::isBuildVectorOfConstantSDNodes (Op.getNode()) && "Expected a constant build vector") ? void (0) : __assert_fail ("ISD::isBuildVectorOfConstantSDNodes(Op.getNode()) && \"Expected a constant build vector\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 41020, __extension__ __PRETTY_FUNCTION__)) | |||
41020 | "Expected a constant build vector")(static_cast <bool> (ISD::isBuildVectorOfConstantSDNodes (Op.getNode()) && "Expected a constant build vector") ? void (0) : __assert_fail ("ISD::isBuildVectorOfConstantSDNodes(Op.getNode()) && \"Expected a constant build vector\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 41020, __extension__ __PRETTY_FUNCTION__)); | |||
41021 | ||||
41022 | APInt Imm(SrcVT.getVectorNumElements(), 0); | |||
41023 | for (unsigned Idx = 0, e = Op.getNumOperands(); Idx < e; ++Idx) { | |||
41024 | SDValue In = Op.getOperand(Idx); | |||
41025 | if (!In.isUndef() && (cast<ConstantSDNode>(In)->getZExtValue() & 0x1)) | |||
41026 | Imm.setBit(Idx); | |||
41027 | } | |||
41028 | EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), Imm.getBitWidth()); | |||
41029 | return DAG.getConstant(Imm, SDLoc(Op), IntVT); | |||
41030 | } | |||
41031 | ||||
41032 | static SDValue combineCastedMaskArithmetic(SDNode *N, SelectionDAG &DAG, | |||
41033 | TargetLowering::DAGCombinerInfo &DCI, | |||
41034 | const X86Subtarget &Subtarget) { | |||
41035 | assert(N->getOpcode() == ISD::BITCAST && "Expected a bitcast")(static_cast <bool> (N->getOpcode() == ISD::BITCAST && "Expected a bitcast") ? void (0) : __assert_fail ("N->getOpcode() == ISD::BITCAST && \"Expected a bitcast\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 41035, __extension__ __PRETTY_FUNCTION__)); | |||
41036 | ||||
41037 | if (!DCI.isBeforeLegalizeOps()) | |||
41038 | return SDValue(); | |||
41039 | ||||
41040 | // Only do this if we have k-registers. | |||
41041 | if (!Subtarget.hasAVX512()) | |||
41042 | return SDValue(); | |||
41043 | ||||
41044 | EVT DstVT = N->getValueType(0); | |||
41045 | SDValue Op = N->getOperand(0); | |||
41046 | EVT SrcVT = Op.getValueType(); | |||
41047 | ||||
41048 | if (!Op.hasOneUse()) | |||
41049 | return SDValue(); | |||
41050 | ||||
41051 | // Look for logic ops. | |||
41052 | if (Op.getOpcode() != ISD::AND && | |||
41053 | Op.getOpcode() != ISD::OR && | |||
41054 | Op.getOpcode() != ISD::XOR) | |||
41055 | return SDValue(); | |||
41056 | ||||
41057 | // Make sure we have a bitcast between mask registers and a scalar type. | |||
41058 | if (!(SrcVT.isVector() && SrcVT.getVectorElementType() == MVT::i1 && | |||
41059 | DstVT.isScalarInteger()) && | |||
41060 | !(DstVT.isVector() && DstVT.getVectorElementType() == MVT::i1 && | |||
41061 | SrcVT.isScalarInteger())) | |||
41062 | return SDValue(); | |||
41063 | ||||
41064 | SDValue LHS = Op.getOperand(0); | |||
41065 | SDValue RHS = Op.getOperand(1); | |||
41066 | ||||
41067 | if (LHS.hasOneUse() && LHS.getOpcode() == ISD::BITCAST && | |||
41068 | LHS.getOperand(0).getValueType() == DstVT) | |||
41069 | return DAG.getNode(Op.getOpcode(), SDLoc(N), DstVT, LHS.getOperand(0), | |||
41070 | DAG.getBitcast(DstVT, RHS)); | |||
41071 | ||||
41072 | if (RHS.hasOneUse() && RHS.getOpcode() == ISD::BITCAST && | |||
41073 | RHS.getOperand(0).getValueType() == DstVT) | |||
41074 | return DAG.getNode(Op.getOpcode(), SDLoc(N), DstVT, | |||
41075 | DAG.getBitcast(DstVT, LHS), RHS.getOperand(0)); | |||
41076 | ||||
41077 | // If the RHS is a vXi1 build vector, this is a good reason to flip too. | |||
41078 | // Most of these have to move a constant from the scalar domain anyway. | |||
41079 | if (ISD::isBuildVectorOfConstantSDNodes(RHS.getNode())) { | |||
41080 | RHS = combinevXi1ConstantToInteger(RHS, DAG); | |||
41081 | return DAG.getNode(Op.getOpcode(), SDLoc(N), DstVT, | |||
41082 | DAG.getBitcast(DstVT, LHS), RHS); | |||
41083 | } | |||
41084 | ||||
41085 | return SDValue(); | |||
41086 | } | |||
41087 | ||||
41088 | static SDValue createMMXBuildVector(BuildVectorSDNode *BV, SelectionDAG &DAG, | |||
41089 | const X86Subtarget &Subtarget) { | |||
41090 | SDLoc DL(BV); | |||
41091 | unsigned NumElts = BV->getNumOperands(); | |||
41092 | SDValue Splat = BV->getSplatValue(); | |||
41093 | ||||
41094 | // Build MMX element from integer GPR or SSE float values. | |||
41095 | auto CreateMMXElement = [&](SDValue V) { | |||
41096 | if (V.isUndef()) | |||
41097 | return DAG.getUNDEF(MVT::x86mmx); | |||
41098 | if (V.getValueType().isFloatingPoint()) { | |||
41099 | if (Subtarget.hasSSE1() && !isa<ConstantFPSDNode>(V)) { | |||
41100 | V = DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, MVT::v4f32, V); | |||
41101 | V = DAG.getBitcast(MVT::v2i64, V); | |||
41102 | return DAG.getNode(X86ISD::MOVDQ2Q, DL, MVT::x86mmx, V); | |||
41103 | } | |||
41104 | V = DAG.getBitcast(MVT::i32, V); | |||
41105 | } else { | |||
41106 | V = DAG.getAnyExtOrTrunc(V, DL, MVT::i32); | |||
41107 | } | |||
41108 | return DAG.getNode(X86ISD::MMX_MOVW2D, DL, MVT::x86mmx, V); | |||
41109 | }; | |||
41110 | ||||
41111 | // Convert build vector ops to MMX data in the bottom elements. | |||
41112 | SmallVector<SDValue, 8> Ops; | |||
41113 | ||||
41114 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
41115 | ||||
41116 | // Broadcast - use (PUNPCKL+)PSHUFW to broadcast single element. | |||
41117 | if (Splat) { | |||
41118 | if (Splat.isUndef()) | |||
41119 | return DAG.getUNDEF(MVT::x86mmx); | |||
41120 | ||||
41121 | Splat = CreateMMXElement(Splat); | |||
41122 | ||||
41123 | if (Subtarget.hasSSE1()) { | |||
41124 | // Unpack v8i8 to splat i8 elements to lowest 16-bits. | |||
41125 | if (NumElts == 8) | |||
41126 | Splat = DAG.getNode( | |||
41127 | ISD::INTRINSIC_WO_CHAIN, DL, MVT::x86mmx, | |||
41128 | DAG.getTargetConstant(Intrinsic::x86_mmx_punpcklbw, DL, | |||
41129 | TLI.getPointerTy(DAG.getDataLayout())), | |||
41130 | Splat, Splat); | |||
41131 | ||||
41132 | // Use PSHUFW to repeat 16-bit elements. | |||
41133 | unsigned ShufMask = (NumElts > 2 ? 0 : 0x44); | |||
41134 | return DAG.getNode( | |||
41135 | ISD::INTRINSIC_WO_CHAIN, DL, MVT::x86mmx, | |||
41136 | DAG.getTargetConstant(Intrinsic::x86_sse_pshuf_w, DL, | |||
41137 | TLI.getPointerTy(DAG.getDataLayout())), | |||
41138 | Splat, DAG.getTargetConstant(ShufMask, DL, MVT::i8)); | |||
41139 | } | |||
41140 | Ops.append(NumElts, Splat); | |||
41141 | } else { | |||
41142 | for (unsigned i = 0; i != NumElts; ++i) | |||
41143 | Ops.push_back(CreateMMXElement(BV->getOperand(i))); | |||
41144 | } | |||
41145 | ||||
41146 | // Use tree of PUNPCKLs to build up general MMX vector. | |||
41147 | while (Ops.size() > 1) { | |||
41148 | unsigned NumOps = Ops.size(); | |||
41149 | unsigned IntrinOp = | |||
41150 | (NumOps == 2 ? Intrinsic::x86_mmx_punpckldq | |||
41151 | : (NumOps == 4 ? Intrinsic::x86_mmx_punpcklwd | |||
41152 | : Intrinsic::x86_mmx_punpcklbw)); | |||
41153 | SDValue Intrin = DAG.getTargetConstant( | |||
41154 | IntrinOp, DL, TLI.getPointerTy(DAG.getDataLayout())); | |||
41155 | for (unsigned i = 0; i != NumOps; i += 2) | |||
41156 | Ops[i / 2] = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, MVT::x86mmx, Intrin, | |||
41157 | Ops[i], Ops[i + 1]); | |||
41158 | Ops.resize(NumOps / 2); | |||
41159 | } | |||
41160 | ||||
41161 | return Ops[0]; | |||
41162 | } | |||
41163 | ||||
41164 | // Recursive function that attempts to find if a bool vector node was originally | |||
41165 | // a vector/float/double that got truncated/extended/bitcast to/from a scalar | |||
41166 | // integer. If so, replace the scalar ops with bool vector equivalents back down | |||
41167 | // the chain. | |||
41168 | static SDValue combineBitcastToBoolVector(EVT VT, SDValue V, const SDLoc &DL, | |||
41169 | SelectionDAG &DAG, | |||
41170 | const X86Subtarget &Subtarget) { | |||
41171 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
41172 | unsigned Opc = V.getOpcode(); | |||
41173 | switch (Opc) { | |||
41174 | case ISD::BITCAST: { | |||
41175 | // Bitcast from a vector/float/double, we can cheaply bitcast to VT. | |||
41176 | SDValue Src = V.getOperand(0); | |||
41177 | EVT SrcVT = Src.getValueType(); | |||
41178 | if (SrcVT.isVector() || SrcVT.isFloatingPoint()) | |||
41179 | return DAG.getBitcast(VT, Src); | |||
41180 | break; | |||
41181 | } | |||
41182 | case ISD::TRUNCATE: { | |||
41183 | // If we find a suitable source, a truncated scalar becomes a subvector. | |||
41184 | SDValue Src = V.getOperand(0); | |||
41185 | EVT NewSrcVT = | |||
41186 | EVT::getVectorVT(*DAG.getContext(), MVT::i1, Src.getValueSizeInBits()); | |||
41187 | if (TLI.isTypeLegal(NewSrcVT)) | |||
41188 | if (SDValue N0 = | |||
41189 | combineBitcastToBoolVector(NewSrcVT, Src, DL, DAG, Subtarget)) | |||
41190 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, N0, | |||
41191 | DAG.getIntPtrConstant(0, DL)); | |||
41192 | break; | |||
41193 | } | |||
41194 | case ISD::ANY_EXTEND: | |||
41195 | case ISD::ZERO_EXTEND: { | |||
41196 | // If we find a suitable source, an extended scalar becomes a subvector. | |||
41197 | SDValue Src = V.getOperand(0); | |||
41198 | EVT NewSrcVT = EVT::getVectorVT(*DAG.getContext(), MVT::i1, | |||
41199 | Src.getScalarValueSizeInBits()); | |||
41200 | if (TLI.isTypeLegal(NewSrcVT)) | |||
41201 | if (SDValue N0 = | |||
41202 | combineBitcastToBoolVector(NewSrcVT, Src, DL, DAG, Subtarget)) | |||
41203 | return DAG.getNode(ISD::INSERT_SUBVECTOR, DL, VT, | |||
41204 | Opc == ISD::ANY_EXTEND ? DAG.getUNDEF(VT) | |||
41205 | : DAG.getConstant(0, DL, VT), | |||
41206 | N0, DAG.getIntPtrConstant(0, DL)); | |||
41207 | break; | |||
41208 | } | |||
41209 | case ISD::OR: { | |||
41210 | // If we find suitable sources, we can just move an OR to the vector domain. | |||
41211 | SDValue Src0 = V.getOperand(0); | |||
41212 | SDValue Src1 = V.getOperand(1); | |||
41213 | if (SDValue N0 = combineBitcastToBoolVector(VT, Src0, DL, DAG, Subtarget)) | |||
41214 | if (SDValue N1 = combineBitcastToBoolVector(VT, Src1, DL, DAG, Subtarget)) | |||
41215 | return DAG.getNode(Opc, DL, VT, N0, N1); | |||
41216 | break; | |||
41217 | } | |||
41218 | case ISD::SHL: { | |||
41219 | // If we find a suitable source, a SHL becomes a KSHIFTL. | |||
41220 | SDValue Src0 = V.getOperand(0); | |||
41221 | if ((VT == MVT::v8i1 && !Subtarget.hasDQI()) || | |||
41222 | ((VT == MVT::v32i1 || VT == MVT::v64i1) && !Subtarget.hasBWI())) | |||
41223 | break; | |||
41224 | ||||
41225 | if (auto *Amt = dyn_cast<ConstantSDNode>(V.getOperand(1))) | |||
41226 | if (SDValue N0 = combineBitcastToBoolVector(VT, Src0, DL, DAG, Subtarget)) | |||
41227 | return DAG.getNode( | |||
41228 | X86ISD::KSHIFTL, DL, VT, N0, | |||
41229 | DAG.getTargetConstant(Amt->getZExtValue(), DL, MVT::i8)); | |||
41230 | break; | |||
41231 | } | |||
41232 | } | |||
41233 | return SDValue(); | |||
41234 | } | |||
41235 | ||||
41236 | static SDValue combineBitcast(SDNode *N, SelectionDAG &DAG, | |||
41237 | TargetLowering::DAGCombinerInfo &DCI, | |||
41238 | const X86Subtarget &Subtarget) { | |||
41239 | SDValue N0 = N->getOperand(0); | |||
41240 | EVT VT = N->getValueType(0); | |||
41241 | EVT SrcVT = N0.getValueType(); | |||
41242 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
41243 | ||||
41244 | // Try to match patterns such as | |||
41245 | // (i16 bitcast (v16i1 x)) | |||
41246 | // -> | |||
41247 | // (i16 movmsk (16i8 sext (v16i1 x))) | |||
41248 | // before the setcc result is scalarized on subtargets that don't have legal | |||
41249 | // vxi1 types. | |||
41250 | if (DCI.isBeforeLegalize()) { | |||
41251 | SDLoc dl(N); | |||
41252 | if (SDValue V = combineBitcastvxi1(DAG, VT, N0, dl, Subtarget)) | |||
41253 | return V; | |||
41254 | ||||
41255 | // If this is a bitcast between a MVT::v4i1/v2i1 and an illegal integer | |||
41256 | // type, widen both sides to avoid a trip through memory. | |||
41257 | if ((VT == MVT::v4i1 || VT == MVT::v2i1) && SrcVT.isScalarInteger() && | |||
41258 | Subtarget.hasAVX512()) { | |||
41259 | N0 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i8, N0); | |||
41260 | N0 = DAG.getBitcast(MVT::v8i1, N0); | |||
41261 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, N0, | |||
41262 | DAG.getIntPtrConstant(0, dl)); | |||
41263 | } | |||
41264 | ||||
41265 | // If this is a bitcast between a MVT::v4i1/v2i1 and an illegal integer | |||
41266 | // type, widen both sides to avoid a trip through memory. | |||
41267 | if ((SrcVT == MVT::v4i1 || SrcVT == MVT::v2i1) && VT.isScalarInteger() && | |||
41268 | Subtarget.hasAVX512()) { | |||
41269 | // Use zeros for the widening if we already have some zeroes. This can | |||
41270 | // allow SimplifyDemandedBits to remove scalar ANDs that may be down | |||
41271 | // stream of this. | |||
41272 | // FIXME: It might make sense to detect a concat_vectors with a mix of | |||
41273 | // zeroes and undef and turn it into insert_subvector for i1 vectors as | |||
41274 | // a separate combine. What we can't do is canonicalize the operands of | |||
41275 | // such a concat or we'll get into a loop with SimplifyDemandedBits. | |||
41276 | if (N0.getOpcode() == ISD::CONCAT_VECTORS) { | |||
41277 | SDValue LastOp = N0.getOperand(N0.getNumOperands() - 1); | |||
41278 | if (ISD::isBuildVectorAllZeros(LastOp.getNode())) { | |||
41279 | SrcVT = LastOp.getValueType(); | |||
41280 | unsigned NumConcats = 8 / SrcVT.getVectorNumElements(); | |||
41281 | SmallVector<SDValue, 4> Ops(N0->op_begin(), N0->op_end()); | |||
41282 | Ops.resize(NumConcats, DAG.getConstant(0, dl, SrcVT)); | |||
41283 | N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i1, Ops); | |||
41284 | N0 = DAG.getBitcast(MVT::i8, N0); | |||
41285 | return DAG.getNode(ISD::TRUNCATE, dl, VT, N0); | |||
41286 | } | |||
41287 | } | |||
41288 | ||||
41289 | unsigned NumConcats = 8 / SrcVT.getVectorNumElements(); | |||
41290 | SmallVector<SDValue, 4> Ops(NumConcats, DAG.getUNDEF(SrcVT)); | |||
41291 | Ops[0] = N0; | |||
41292 | N0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i1, Ops); | |||
41293 | N0 = DAG.getBitcast(MVT::i8, N0); | |||
41294 | return DAG.getNode(ISD::TRUNCATE, dl, VT, N0); | |||
41295 | } | |||
41296 | } else { | |||
41297 | // If we're bitcasting from iX to vXi1, see if the integer originally | |||
41298 | // began as a vXi1 and whether we can remove the bitcast entirely. | |||
41299 | if (VT.isVector() && VT.getScalarType() == MVT::i1 && | |||
41300 | SrcVT.isScalarInteger() && TLI.isTypeLegal(VT)) { | |||
41301 | if (SDValue V = | |||
41302 | combineBitcastToBoolVector(VT, N0, SDLoc(N), DAG, Subtarget)) | |||
41303 | return V; | |||
41304 | } | |||
41305 | } | |||
41306 | ||||
41307 | // Look for (i8 (bitcast (v8i1 (extract_subvector (v16i1 X), 0)))) and | |||
41308 | // replace with (i8 (trunc (i16 (bitcast (v16i1 X))))). This can occur | |||
41309 | // due to insert_subvector legalization on KNL. By promoting the copy to i16 | |||
41310 | // we can help with known bits propagation from the vXi1 domain to the | |||
41311 | // scalar domain. | |||
41312 | if (VT == MVT::i8 && SrcVT == MVT::v8i1 && Subtarget.hasAVX512() && | |||
41313 | !Subtarget.hasDQI() && N0.getOpcode() == ISD::EXTRACT_SUBVECTOR && | |||
41314 | N0.getOperand(0).getValueType() == MVT::v16i1 && | |||
41315 | isNullConstant(N0.getOperand(1))) | |||
41316 | return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, | |||
41317 | DAG.getBitcast(MVT::i16, N0.getOperand(0))); | |||
41318 | ||||
41319 | // Canonicalize (bitcast (vbroadcast_load)) so that the output of the bitcast | |||
41320 | // and the vbroadcast_load are both integer or both fp. In some cases this | |||
41321 | // will remove the bitcast entirely. | |||
41322 | if (N0.getOpcode() == X86ISD::VBROADCAST_LOAD && N0.hasOneUse() && | |||
41323 | VT.isFloatingPoint() != SrcVT.isFloatingPoint() && VT.isVector()) { | |||
41324 | auto *BCast = cast<MemIntrinsicSDNode>(N0); | |||
41325 | unsigned SrcVTSize = SrcVT.getScalarSizeInBits(); | |||
41326 | unsigned MemSize = BCast->getMemoryVT().getScalarSizeInBits(); | |||
41327 | // Don't swap i8/i16 since don't have fp types that size. | |||
41328 | if (MemSize >= 32) { | |||
41329 | MVT MemVT = VT.isFloatingPoint() ? MVT::getFloatingPointVT(MemSize) | |||
41330 | : MVT::getIntegerVT(MemSize); | |||
41331 | MVT LoadVT = VT.isFloatingPoint() ? MVT::getFloatingPointVT(SrcVTSize) | |||
41332 | : MVT::getIntegerVT(SrcVTSize); | |||
41333 | LoadVT = MVT::getVectorVT(LoadVT, SrcVT.getVectorNumElements()); | |||
41334 | ||||
41335 | SDVTList Tys = DAG.getVTList(LoadVT, MVT::Other); | |||
41336 | SDValue Ops[] = { BCast->getChain(), BCast->getBasePtr() }; | |||
41337 | SDValue ResNode = | |||
41338 | DAG.getMemIntrinsicNode(X86ISD::VBROADCAST_LOAD, SDLoc(N), Tys, Ops, | |||
41339 | MemVT, BCast->getMemOperand()); | |||
41340 | DAG.ReplaceAllUsesOfValueWith(SDValue(BCast, 1), ResNode.getValue(1)); | |||
41341 | return DAG.getBitcast(VT, ResNode); | |||
41342 | } | |||
41343 | } | |||
41344 | ||||
41345 | // Since MMX types are special and don't usually play with other vector types, | |||
41346 | // it's better to handle them early to be sure we emit efficient code by | |||
41347 | // avoiding store-load conversions. | |||
41348 | if (VT == MVT::x86mmx) { | |||
41349 | // Detect MMX constant vectors. | |||
41350 | APInt UndefElts; | |||
41351 | SmallVector<APInt, 1> EltBits; | |||
41352 | if (getTargetConstantBitsFromNode(N0, 64, UndefElts, EltBits)) { | |||
41353 | SDLoc DL(N0); | |||
41354 | // Handle zero-extension of i32 with MOVD. | |||
41355 | if (EltBits[0].countLeadingZeros() >= 32) | |||
41356 | return DAG.getNode(X86ISD::MMX_MOVW2D, DL, VT, | |||
41357 | DAG.getConstant(EltBits[0].trunc(32), DL, MVT::i32)); | |||
41358 | // Else, bitcast to a double. | |||
41359 | // TODO - investigate supporting sext 32-bit immediates on x86_64. | |||
41360 | APFloat F64(APFloat::IEEEdouble(), EltBits[0]); | |||
41361 | return DAG.getBitcast(VT, DAG.getConstantFP(F64, DL, MVT::f64)); | |||
41362 | } | |||
41363 | ||||
41364 | // Detect bitcasts to x86mmx low word. | |||
41365 | if (N0.getOpcode() == ISD::BUILD_VECTOR && | |||
41366 | (SrcVT == MVT::v2i32 || SrcVT == MVT::v4i16 || SrcVT == MVT::v8i8) && | |||
41367 | N0.getOperand(0).getValueType() == SrcVT.getScalarType()) { | |||
41368 | bool LowUndef = true, AllUndefOrZero = true; | |||
41369 | for (unsigned i = 1, e = SrcVT.getVectorNumElements(); i != e; ++i) { | |||
41370 | SDValue Op = N0.getOperand(i); | |||
41371 | LowUndef &= Op.isUndef() || (i >= e/2); | |||
41372 | AllUndefOrZero &= (Op.isUndef() || isNullConstant(Op)); | |||
41373 | } | |||
41374 | if (AllUndefOrZero) { | |||
41375 | SDValue N00 = N0.getOperand(0); | |||
41376 | SDLoc dl(N00); | |||
41377 | N00 = LowUndef ? DAG.getAnyExtOrTrunc(N00, dl, MVT::i32) | |||
41378 | : DAG.getZExtOrTrunc(N00, dl, MVT::i32); | |||
41379 | return DAG.getNode(X86ISD::MMX_MOVW2D, dl, VT, N00); | |||
41380 | } | |||
41381 | } | |||
41382 | ||||
41383 | // Detect bitcasts of 64-bit build vectors and convert to a | |||
41384 | // MMX UNPCK/PSHUFW which takes MMX type inputs with the value in the | |||
41385 | // lowest element. | |||
41386 | if (N0.getOpcode() == ISD::BUILD_VECTOR && | |||
41387 | (SrcVT == MVT::v2f32 || SrcVT == MVT::v2i32 || SrcVT == MVT::v4i16 || | |||
41388 | SrcVT == MVT::v8i8)) | |||
41389 | return createMMXBuildVector(cast<BuildVectorSDNode>(N0), DAG, Subtarget); | |||
41390 | ||||
41391 | // Detect bitcasts between element or subvector extraction to x86mmx. | |||
41392 | if ((N0.getOpcode() == ISD::EXTRACT_VECTOR_ELT || | |||
41393 | N0.getOpcode() == ISD::EXTRACT_SUBVECTOR) && | |||
41394 | isNullConstant(N0.getOperand(1))) { | |||
41395 | SDValue N00 = N0.getOperand(0); | |||
41396 | if (N00.getValueType().is128BitVector()) | |||
41397 | return DAG.getNode(X86ISD::MOVDQ2Q, SDLoc(N00), VT, | |||
41398 | DAG.getBitcast(MVT::v2i64, N00)); | |||
41399 | } | |||
41400 | ||||
41401 | // Detect bitcasts from FP_TO_SINT to x86mmx. | |||
41402 | if (SrcVT == MVT::v2i32 && N0.getOpcode() == ISD::FP_TO_SINT) { | |||
41403 | SDLoc DL(N0); | |||
41404 | SDValue Res = DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v4i32, N0, | |||
41405 | DAG.getUNDEF(MVT::v2i32)); | |||
41406 | return DAG.getNode(X86ISD::MOVDQ2Q, DL, VT, | |||
41407 | DAG.getBitcast(MVT::v2i64, Res)); | |||
41408 | } | |||
41409 | } | |||
41410 | ||||
41411 | // Try to remove a bitcast of constant vXi1 vector. We have to legalize | |||
41412 | // most of these to scalar anyway. | |||
41413 | if (Subtarget.hasAVX512() && VT.isScalarInteger() && | |||
41414 | SrcVT.isVector() && SrcVT.getVectorElementType() == MVT::i1 && | |||
41415 | ISD::isBuildVectorOfConstantSDNodes(N0.getNode())) { | |||
41416 | return combinevXi1ConstantToInteger(N0, DAG); | |||
41417 | } | |||
41418 | ||||
41419 | if (Subtarget.hasAVX512() && SrcVT.isScalarInteger() && | |||
41420 | VT.isVector() && VT.getVectorElementType() == MVT::i1 && | |||
41421 | isa<ConstantSDNode>(N0)) { | |||
41422 | auto *C = cast<ConstantSDNode>(N0); | |||
41423 | if (C->isAllOnes()) | |||
41424 | return DAG.getConstant(1, SDLoc(N0), VT); | |||
41425 | if (C->isZero()) | |||
41426 | return DAG.getConstant(0, SDLoc(N0), VT); | |||
41427 | } | |||
41428 | ||||
41429 | // Look for MOVMSK that is maybe truncated and then bitcasted to vXi1. | |||
41430 | // Turn it into a sign bit compare that produces a k-register. This avoids | |||
41431 | // a trip through a GPR. | |||
41432 | if (Subtarget.hasAVX512() && SrcVT.isScalarInteger() && | |||
41433 | VT.isVector() && VT.getVectorElementType() == MVT::i1 && | |||
41434 | isPowerOf2_32(VT.getVectorNumElements())) { | |||
41435 | unsigned NumElts = VT.getVectorNumElements(); | |||
41436 | SDValue Src = N0; | |||
41437 | ||||
41438 | // Peek through truncate. | |||
41439 | if (N0.getOpcode() == ISD::TRUNCATE && N0.hasOneUse()) | |||
41440 | Src = N0.getOperand(0); | |||
41441 | ||||
41442 | if (Src.getOpcode() == X86ISD::MOVMSK && Src.hasOneUse()) { | |||
41443 | SDValue MovmskIn = Src.getOperand(0); | |||
41444 | MVT MovmskVT = MovmskIn.getSimpleValueType(); | |||
41445 | unsigned MovMskElts = MovmskVT.getVectorNumElements(); | |||
41446 | ||||
41447 | // We allow extra bits of the movmsk to be used since they are known zero. | |||
41448 | // We can't convert a VPMOVMSKB without avx512bw. | |||
41449 | if (MovMskElts <= NumElts && | |||
41450 | (Subtarget.hasBWI() || MovmskVT.getVectorElementType() != MVT::i8)) { | |||
41451 | EVT IntVT = EVT(MovmskVT).changeVectorElementTypeToInteger(); | |||
41452 | MovmskIn = DAG.getBitcast(IntVT, MovmskIn); | |||
41453 | SDLoc dl(N); | |||
41454 | MVT CmpVT = MVT::getVectorVT(MVT::i1, MovMskElts); | |||
41455 | SDValue Cmp = DAG.getSetCC(dl, CmpVT, MovmskIn, | |||
41456 | DAG.getConstant(0, dl, IntVT), ISD::SETLT); | |||
41457 | if (EVT(CmpVT) == VT) | |||
41458 | return Cmp; | |||
41459 | ||||
41460 | // Pad with zeroes up to original VT to replace the zeroes that were | |||
41461 | // being used from the MOVMSK. | |||
41462 | unsigned NumConcats = NumElts / MovMskElts; | |||
41463 | SmallVector<SDValue, 4> Ops(NumConcats, DAG.getConstant(0, dl, CmpVT)); | |||
41464 | Ops[0] = Cmp; | |||
41465 | return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Ops); | |||
41466 | } | |||
41467 | } | |||
41468 | } | |||
41469 | ||||
41470 | // Try to remove bitcasts from input and output of mask arithmetic to | |||
41471 | // remove GPR<->K-register crossings. | |||
41472 | if (SDValue V = combineCastedMaskArithmetic(N, DAG, DCI, Subtarget)) | |||
41473 | return V; | |||
41474 | ||||
41475 | // Convert a bitcasted integer logic operation that has one bitcasted | |||
41476 | // floating-point operand into a floating-point logic operation. This may | |||
41477 | // create a load of a constant, but that is cheaper than materializing the | |||
41478 | // constant in an integer register and transferring it to an SSE register or | |||
41479 | // transferring the SSE operand to integer register and back. | |||
41480 | unsigned FPOpcode; | |||
41481 | switch (N0.getOpcode()) { | |||
41482 | case ISD::AND: FPOpcode = X86ISD::FAND; break; | |||
41483 | case ISD::OR: FPOpcode = X86ISD::FOR; break; | |||
41484 | case ISD::XOR: FPOpcode = X86ISD::FXOR; break; | |||
41485 | default: return SDValue(); | |||
41486 | } | |||
41487 | ||||
41488 | // Check if we have a bitcast from another integer type as well. | |||
41489 | if (!((Subtarget.hasSSE1() && VT == MVT::f32) || | |||
41490 | (Subtarget.hasSSE2() && VT == MVT::f64) || | |||
41491 | (Subtarget.hasFP16() && VT == MVT::f16) || | |||
41492 | (Subtarget.hasSSE2() && VT.isInteger() && VT.isVector() && | |||
41493 | TLI.isTypeLegal(VT)))) | |||
41494 | return SDValue(); | |||
41495 | ||||
41496 | SDValue LogicOp0 = N0.getOperand(0); | |||
41497 | SDValue LogicOp1 = N0.getOperand(1); | |||
41498 | SDLoc DL0(N0); | |||
41499 | ||||
41500 | // bitcast(logic(bitcast(X), Y)) --> logic'(X, bitcast(Y)) | |||
41501 | if (N0.hasOneUse() && LogicOp0.getOpcode() == ISD::BITCAST && | |||
41502 | LogicOp0.hasOneUse() && LogicOp0.getOperand(0).hasOneUse() && | |||
41503 | LogicOp0.getOperand(0).getValueType() == VT && | |||
41504 | !isa<ConstantSDNode>(LogicOp0.getOperand(0))) { | |||
41505 | SDValue CastedOp1 = DAG.getBitcast(VT, LogicOp1); | |||
41506 | unsigned Opcode = VT.isFloatingPoint() ? FPOpcode : N0.getOpcode(); | |||
41507 | return DAG.getNode(Opcode, DL0, VT, LogicOp0.getOperand(0), CastedOp1); | |||
41508 | } | |||
41509 | // bitcast(logic(X, bitcast(Y))) --> logic'(bitcast(X), Y) | |||
41510 | if (N0.hasOneUse() && LogicOp1.getOpcode() == ISD::BITCAST && | |||
41511 | LogicOp1.hasOneUse() && LogicOp1.getOperand(0).hasOneUse() && | |||
41512 | LogicOp1.getOperand(0).getValueType() == VT && | |||
41513 | !isa<ConstantSDNode>(LogicOp1.getOperand(0))) { | |||
41514 | SDValue CastedOp0 = DAG.getBitcast(VT, LogicOp0); | |||
41515 | unsigned Opcode = VT.isFloatingPoint() ? FPOpcode : N0.getOpcode(); | |||
41516 | return DAG.getNode(Opcode, DL0, VT, LogicOp1.getOperand(0), CastedOp0); | |||
41517 | } | |||
41518 | ||||
41519 | return SDValue(); | |||
41520 | } | |||
41521 | ||||
41522 | // Given a ABS node, detect the following pattern: | |||
41523 | // (ABS (SUB (ZERO_EXTEND a), (ZERO_EXTEND b))). | |||
41524 | // This is useful as it is the input into a SAD pattern. | |||
41525 | static bool detectZextAbsDiff(const SDValue &Abs, SDValue &Op0, SDValue &Op1) { | |||
41526 | SDValue AbsOp1 = Abs->getOperand(0); | |||
41527 | if (AbsOp1.getOpcode() != ISD::SUB) | |||
41528 | return false; | |||
41529 | ||||
41530 | Op0 = AbsOp1.getOperand(0); | |||
41531 | Op1 = AbsOp1.getOperand(1); | |||
41532 | ||||
41533 | // Check if the operands of the sub are zero-extended from vectors of i8. | |||
41534 | if (Op0.getOpcode() != ISD::ZERO_EXTEND || | |||
41535 | Op0.getOperand(0).getValueType().getVectorElementType() != MVT::i8 || | |||
41536 | Op1.getOpcode() != ISD::ZERO_EXTEND || | |||
41537 | Op1.getOperand(0).getValueType().getVectorElementType() != MVT::i8) | |||
41538 | return false; | |||
41539 | ||||
41540 | return true; | |||
41541 | } | |||
41542 | ||||
41543 | // Given two zexts of <k x i8> to <k x i32>, create a PSADBW of the inputs | |||
41544 | // to these zexts. | |||
41545 | static SDValue createPSADBW(SelectionDAG &DAG, const SDValue &Zext0, | |||
41546 | const SDValue &Zext1, const SDLoc &DL, | |||
41547 | const X86Subtarget &Subtarget) { | |||
41548 | // Find the appropriate width for the PSADBW. | |||
41549 | EVT InVT = Zext0.getOperand(0).getValueType(); | |||
41550 | unsigned RegSize = std::max(128u, (unsigned)InVT.getSizeInBits()); | |||
41551 | ||||
41552 | // "Zero-extend" the i8 vectors. This is not a per-element zext, rather we | |||
41553 | // fill in the missing vector elements with 0. | |||
41554 | unsigned NumConcat = RegSize / InVT.getSizeInBits(); | |||
41555 | SmallVector<SDValue, 16> Ops(NumConcat, DAG.getConstant(0, DL, InVT)); | |||
41556 | Ops[0] = Zext0.getOperand(0); | |||
41557 | MVT ExtendedVT = MVT::getVectorVT(MVT::i8, RegSize / 8); | |||
41558 | SDValue SadOp0 = DAG.getNode(ISD::CONCAT_VECTORS, DL, ExtendedVT, Ops); | |||
41559 | Ops[0] = Zext1.getOperand(0); | |||
41560 | SDValue SadOp1 = DAG.getNode(ISD::CONCAT_VECTORS, DL, ExtendedVT, Ops); | |||
41561 | ||||
41562 | // Actually build the SAD, split as 128/256/512 bits for SSE/AVX2/AVX512BW. | |||
41563 | auto PSADBWBuilder = [](SelectionDAG &DAG, const SDLoc &DL, | |||
41564 | ArrayRef<SDValue> Ops) { | |||
41565 | MVT VT = MVT::getVectorVT(MVT::i64, Ops[0].getValueSizeInBits() / 64); | |||
41566 | return DAG.getNode(X86ISD::PSADBW, DL, VT, Ops); | |||
41567 | }; | |||
41568 | MVT SadVT = MVT::getVectorVT(MVT::i64, RegSize / 64); | |||
41569 | return SplitOpsAndApply(DAG, Subtarget, DL, SadVT, { SadOp0, SadOp1 }, | |||
41570 | PSADBWBuilder); | |||
41571 | } | |||
41572 | ||||
41573 | // Attempt to replace an min/max v8i16/v16i8 horizontal reduction with | |||
41574 | // PHMINPOSUW. | |||
41575 | static SDValue combineMinMaxReduction(SDNode *Extract, SelectionDAG &DAG, | |||
41576 | const X86Subtarget &Subtarget) { | |||
41577 | // Bail without SSE41. | |||
41578 | if (!Subtarget.hasSSE41()) | |||
41579 | return SDValue(); | |||
41580 | ||||
41581 | EVT ExtractVT = Extract->getValueType(0); | |||
41582 | if (ExtractVT != MVT::i16 && ExtractVT != MVT::i8) | |||
41583 | return SDValue(); | |||
41584 | ||||
41585 | // Check for SMAX/SMIN/UMAX/UMIN horizontal reduction patterns. | |||
41586 | ISD::NodeType BinOp; | |||
41587 | SDValue Src = DAG.matchBinOpReduction( | |||
41588 | Extract, BinOp, {ISD::SMAX, ISD::SMIN, ISD::UMAX, ISD::UMIN}, true); | |||
41589 | if (!Src) | |||
41590 | return SDValue(); | |||
41591 | ||||
41592 | EVT SrcVT = Src.getValueType(); | |||
41593 | EVT SrcSVT = SrcVT.getScalarType(); | |||
41594 | if (SrcSVT != ExtractVT || (SrcVT.getSizeInBits() % 128) != 0) | |||
41595 | return SDValue(); | |||
41596 | ||||
41597 | SDLoc DL(Extract); | |||
41598 | SDValue MinPos = Src; | |||
41599 | ||||
41600 | // First, reduce the source down to 128-bit, applying BinOp to lo/hi. | |||
41601 | while (SrcVT.getSizeInBits() > 128) { | |||
41602 | SDValue Lo, Hi; | |||
41603 | std::tie(Lo, Hi) = splitVector(MinPos, DAG, DL); | |||
41604 | SrcVT = Lo.getValueType(); | |||
41605 | MinPos = DAG.getNode(BinOp, DL, SrcVT, Lo, Hi); | |||
41606 | } | |||
41607 | assert(((SrcVT == MVT::v8i16 && ExtractVT == MVT::i16) ||(static_cast <bool> (((SrcVT == MVT::v8i16 && ExtractVT == MVT::i16) || (SrcVT == MVT::v16i8 && ExtractVT == MVT::i8)) && "Unexpected value type") ? void (0) : __assert_fail ("((SrcVT == MVT::v8i16 && ExtractVT == MVT::i16) || (SrcVT == MVT::v16i8 && ExtractVT == MVT::i8)) && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 41609, __extension__ __PRETTY_FUNCTION__)) | |||
41608 | (SrcVT == MVT::v16i8 && ExtractVT == MVT::i8)) &&(static_cast <bool> (((SrcVT == MVT::v8i16 && ExtractVT == MVT::i16) || (SrcVT == MVT::v16i8 && ExtractVT == MVT::i8)) && "Unexpected value type") ? void (0) : __assert_fail ("((SrcVT == MVT::v8i16 && ExtractVT == MVT::i16) || (SrcVT == MVT::v16i8 && ExtractVT == MVT::i8)) && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 41609, __extension__ __PRETTY_FUNCTION__)) | |||
41609 | "Unexpected value type")(static_cast <bool> (((SrcVT == MVT::v8i16 && ExtractVT == MVT::i16) || (SrcVT == MVT::v16i8 && ExtractVT == MVT::i8)) && "Unexpected value type") ? void (0) : __assert_fail ("((SrcVT == MVT::v8i16 && ExtractVT == MVT::i16) || (SrcVT == MVT::v16i8 && ExtractVT == MVT::i8)) && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 41609, __extension__ __PRETTY_FUNCTION__)); | |||
41610 | ||||
41611 | // PHMINPOSUW applies to UMIN(v8i16), for SMIN/SMAX/UMAX we must apply a mask | |||
41612 | // to flip the value accordingly. | |||
41613 | SDValue Mask; | |||
41614 | unsigned MaskEltsBits = ExtractVT.getSizeInBits(); | |||
41615 | if (BinOp == ISD::SMAX) | |||
41616 | Mask = DAG.getConstant(APInt::getSignedMaxValue(MaskEltsBits), DL, SrcVT); | |||
41617 | else if (BinOp == ISD::SMIN) | |||
41618 | Mask = DAG.getConstant(APInt::getSignedMinValue(MaskEltsBits), DL, SrcVT); | |||
41619 | else if (BinOp == ISD::UMAX) | |||
41620 | Mask = DAG.getAllOnesConstant(DL, SrcVT); | |||
41621 | ||||
41622 | if (Mask) | |||
41623 | MinPos = DAG.getNode(ISD::XOR, DL, SrcVT, Mask, MinPos); | |||
41624 | ||||
41625 | // For v16i8 cases we need to perform UMIN on pairs of byte elements, | |||
41626 | // shuffling each upper element down and insert zeros. This means that the | |||
41627 | // v16i8 UMIN will leave the upper element as zero, performing zero-extension | |||
41628 | // ready for the PHMINPOS. | |||
41629 | if (ExtractVT == MVT::i8) { | |||
41630 | SDValue Upper = DAG.getVectorShuffle( | |||
41631 | SrcVT, DL, MinPos, DAG.getConstant(0, DL, MVT::v16i8), | |||
41632 | {1, 16, 3, 16, 5, 16, 7, 16, 9, 16, 11, 16, 13, 16, 15, 16}); | |||
41633 | MinPos = DAG.getNode(ISD::UMIN, DL, SrcVT, MinPos, Upper); | |||
41634 | } | |||
41635 | ||||
41636 | // Perform the PHMINPOS on a v8i16 vector, | |||
41637 | MinPos = DAG.getBitcast(MVT::v8i16, MinPos); | |||
41638 | MinPos = DAG.getNode(X86ISD::PHMINPOS, DL, MVT::v8i16, MinPos); | |||
41639 | MinPos = DAG.getBitcast(SrcVT, MinPos); | |||
41640 | ||||
41641 | if (Mask) | |||
41642 | MinPos = DAG.getNode(ISD::XOR, DL, SrcVT, Mask, MinPos); | |||
41643 | ||||
41644 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ExtractVT, MinPos, | |||
41645 | DAG.getIntPtrConstant(0, DL)); | |||
41646 | } | |||
41647 | ||||
41648 | // Attempt to replace an all_of/any_of/parity style horizontal reduction with a MOVMSK. | |||
41649 | static SDValue combinePredicateReduction(SDNode *Extract, SelectionDAG &DAG, | |||
41650 | const X86Subtarget &Subtarget) { | |||
41651 | // Bail without SSE2. | |||
41652 | if (!Subtarget.hasSSE2()) | |||
41653 | return SDValue(); | |||
41654 | ||||
41655 | EVT ExtractVT = Extract->getValueType(0); | |||
41656 | unsigned BitWidth = ExtractVT.getSizeInBits(); | |||
41657 | if (ExtractVT != MVT::i64 && ExtractVT != MVT::i32 && ExtractVT != MVT::i16 && | |||
41658 | ExtractVT != MVT::i8 && ExtractVT != MVT::i1) | |||
41659 | return SDValue(); | |||
41660 | ||||
41661 | // Check for OR(any_of)/AND(all_of)/XOR(parity) horizontal reduction patterns. | |||
41662 | ISD::NodeType BinOp; | |||
41663 | SDValue Match = DAG.matchBinOpReduction(Extract, BinOp, {ISD::OR, ISD::AND}); | |||
41664 | if (!Match && ExtractVT == MVT::i1) | |||
41665 | Match = DAG.matchBinOpReduction(Extract, BinOp, {ISD::XOR}); | |||
41666 | if (!Match) | |||
41667 | return SDValue(); | |||
41668 | ||||
41669 | // EXTRACT_VECTOR_ELT can require implicit extension of the vector element | |||
41670 | // which we can't support here for now. | |||
41671 | if (Match.getScalarValueSizeInBits() != BitWidth) | |||
41672 | return SDValue(); | |||
41673 | ||||
41674 | SDValue Movmsk; | |||
41675 | SDLoc DL(Extract); | |||
41676 | EVT MatchVT = Match.getValueType(); | |||
41677 | unsigned NumElts = MatchVT.getVectorNumElements(); | |||
41678 | unsigned MaxElts = Subtarget.hasInt256() ? 32 : 16; | |||
41679 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
41680 | ||||
41681 | if (ExtractVT == MVT::i1) { | |||
41682 | // Special case for (pre-legalization) vXi1 reductions. | |||
41683 | if (NumElts > 64 || !isPowerOf2_32(NumElts)) | |||
41684 | return SDValue(); | |||
41685 | if (TLI.isTypeLegal(MatchVT)) { | |||
41686 | // If this is a legal AVX512 predicate type then we can just bitcast. | |||
41687 | EVT MovmskVT = EVT::getIntegerVT(*DAG.getContext(), NumElts); | |||
41688 | Movmsk = DAG.getBitcast(MovmskVT, Match); | |||
41689 | } else { | |||
41690 | // For all_of(setcc(vec,0,eq)) - avoid vXi64 comparisons if we don't have | |||
41691 | // PCMPEQQ (SSE41+), use PCMPEQD instead. | |||
41692 | if (BinOp == ISD::AND && !Subtarget.hasSSE41() && | |||
41693 | Match.getOpcode() == ISD::SETCC && | |||
41694 | ISD::isBuildVectorAllZeros(Match.getOperand(1).getNode()) && | |||
41695 | cast<CondCodeSDNode>(Match.getOperand(2))->get() == | |||
41696 | ISD::CondCode::SETEQ) { | |||
41697 | SDValue Vec = Match.getOperand(0); | |||
41698 | if (Vec.getValueType().getScalarType() == MVT::i64 && | |||
41699 | (2 * NumElts) <= MaxElts) { | |||
41700 | NumElts *= 2; | |||
41701 | EVT CmpVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts); | |||
41702 | MatchVT = EVT::getVectorVT(*DAG.getContext(), MVT::i1, NumElts); | |||
41703 | Match = DAG.getSetCC( | |||
41704 | DL, MatchVT, DAG.getBitcast(CmpVT, Match.getOperand(0)), | |||
41705 | DAG.getBitcast(CmpVT, Match.getOperand(1)), ISD::CondCode::SETEQ); | |||
41706 | } | |||
41707 | } | |||
41708 | ||||
41709 | // Use combineBitcastvxi1 to create the MOVMSK. | |||
41710 | while (NumElts > MaxElts) { | |||
41711 | SDValue Lo, Hi; | |||
41712 | std::tie(Lo, Hi) = DAG.SplitVector(Match, DL); | |||
41713 | Match = DAG.getNode(BinOp, DL, Lo.getValueType(), Lo, Hi); | |||
41714 | NumElts /= 2; | |||
41715 | } | |||
41716 | EVT MovmskVT = EVT::getIntegerVT(*DAG.getContext(), NumElts); | |||
41717 | Movmsk = combineBitcastvxi1(DAG, MovmskVT, Match, DL, Subtarget); | |||
41718 | } | |||
41719 | if (!Movmsk) | |||
41720 | return SDValue(); | |||
41721 | Movmsk = DAG.getZExtOrTrunc(Movmsk, DL, NumElts > 32 ? MVT::i64 : MVT::i32); | |||
41722 | } else { | |||
41723 | // FIXME: Better handling of k-registers or 512-bit vectors? | |||
41724 | unsigned MatchSizeInBits = Match.getValueSizeInBits(); | |||
41725 | if (!(MatchSizeInBits == 128 || | |||
41726 | (MatchSizeInBits == 256 && Subtarget.hasAVX()))) | |||
41727 | return SDValue(); | |||
41728 | ||||
41729 | // Make sure this isn't a vector of 1 element. The perf win from using | |||
41730 | // MOVMSK diminishes with less elements in the reduction, but it is | |||
41731 | // generally better to get the comparison over to the GPRs as soon as | |||
41732 | // possible to reduce the number of vector ops. | |||
41733 | if (Match.getValueType().getVectorNumElements() < 2) | |||
41734 | return SDValue(); | |||
41735 | ||||
41736 | // Check that we are extracting a reduction of all sign bits. | |||
41737 | if (DAG.ComputeNumSignBits(Match) != BitWidth) | |||
41738 | return SDValue(); | |||
41739 | ||||
41740 | if (MatchSizeInBits == 256 && BitWidth < 32 && !Subtarget.hasInt256()) { | |||
41741 | SDValue Lo, Hi; | |||
41742 | std::tie(Lo, Hi) = DAG.SplitVector(Match, DL); | |||
41743 | Match = DAG.getNode(BinOp, DL, Lo.getValueType(), Lo, Hi); | |||
41744 | MatchSizeInBits = Match.getValueSizeInBits(); | |||
41745 | } | |||
41746 | ||||
41747 | // For 32/64 bit comparisons use MOVMSKPS/MOVMSKPD, else PMOVMSKB. | |||
41748 | MVT MaskSrcVT; | |||
41749 | if (64 == BitWidth || 32 == BitWidth) | |||
41750 | MaskSrcVT = MVT::getVectorVT(MVT::getFloatingPointVT(BitWidth), | |||
41751 | MatchSizeInBits / BitWidth); | |||
41752 | else | |||
41753 | MaskSrcVT = MVT::getVectorVT(MVT::i8, MatchSizeInBits / 8); | |||
41754 | ||||
41755 | SDValue BitcastLogicOp = DAG.getBitcast(MaskSrcVT, Match); | |||
41756 | Movmsk = getPMOVMSKB(DL, BitcastLogicOp, DAG, Subtarget); | |||
41757 | NumElts = MaskSrcVT.getVectorNumElements(); | |||
41758 | } | |||
41759 | assert((NumElts <= 32 || NumElts == 64) &&(static_cast <bool> ((NumElts <= 32 || NumElts == 64 ) && "Not expecting more than 64 elements") ? void (0 ) : __assert_fail ("(NumElts <= 32 || NumElts == 64) && \"Not expecting more than 64 elements\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 41760, __extension__ __PRETTY_FUNCTION__)) | |||
41760 | "Not expecting more than 64 elements")(static_cast <bool> ((NumElts <= 32 || NumElts == 64 ) && "Not expecting more than 64 elements") ? void (0 ) : __assert_fail ("(NumElts <= 32 || NumElts == 64) && \"Not expecting more than 64 elements\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 41760, __extension__ __PRETTY_FUNCTION__)); | |||
41761 | ||||
41762 | MVT CmpVT = NumElts == 64 ? MVT::i64 : MVT::i32; | |||
41763 | if (BinOp == ISD::XOR) { | |||
41764 | // parity -> (PARITY(MOVMSK X)) | |||
41765 | SDValue Result = DAG.getNode(ISD::PARITY, DL, CmpVT, Movmsk); | |||
41766 | return DAG.getZExtOrTrunc(Result, DL, ExtractVT); | |||
41767 | } | |||
41768 | ||||
41769 | SDValue CmpC; | |||
41770 | ISD::CondCode CondCode; | |||
41771 | if (BinOp == ISD::OR) { | |||
41772 | // any_of -> MOVMSK != 0 | |||
41773 | CmpC = DAG.getConstant(0, DL, CmpVT); | |||
41774 | CondCode = ISD::CondCode::SETNE; | |||
41775 | } else { | |||
41776 | // all_of -> MOVMSK == ((1 << NumElts) - 1) | |||
41777 | CmpC = DAG.getConstant(APInt::getLowBitsSet(CmpVT.getSizeInBits(), NumElts), | |||
41778 | DL, CmpVT); | |||
41779 | CondCode = ISD::CondCode::SETEQ; | |||
41780 | } | |||
41781 | ||||
41782 | // The setcc produces an i8 of 0/1, so extend that to the result width and | |||
41783 | // negate to get the final 0/-1 mask value. | |||
41784 | EVT SetccVT = | |||
41785 | TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), CmpVT); | |||
41786 | SDValue Setcc = DAG.getSetCC(DL, SetccVT, Movmsk, CmpC, CondCode); | |||
41787 | SDValue Zext = DAG.getZExtOrTrunc(Setcc, DL, ExtractVT); | |||
41788 | SDValue Zero = DAG.getConstant(0, DL, ExtractVT); | |||
41789 | return DAG.getNode(ISD::SUB, DL, ExtractVT, Zero, Zext); | |||
41790 | } | |||
41791 | ||||
41792 | static SDValue combineBasicSADPattern(SDNode *Extract, SelectionDAG &DAG, | |||
41793 | const X86Subtarget &Subtarget) { | |||
41794 | // PSADBW is only supported on SSE2 and up. | |||
41795 | if (!Subtarget.hasSSE2()) | |||
41796 | return SDValue(); | |||
41797 | ||||
41798 | EVT ExtractVT = Extract->getValueType(0); | |||
41799 | // Verify the type we're extracting is either i32 or i64. | |||
41800 | // FIXME: Could support other types, but this is what we have coverage for. | |||
41801 | if (ExtractVT != MVT::i32 && ExtractVT != MVT::i64) | |||
41802 | return SDValue(); | |||
41803 | ||||
41804 | EVT VT = Extract->getOperand(0).getValueType(); | |||
41805 | if (!isPowerOf2_32(VT.getVectorNumElements())) | |||
41806 | return SDValue(); | |||
41807 | ||||
41808 | // Match shuffle + add pyramid. | |||
41809 | ISD::NodeType BinOp; | |||
41810 | SDValue Root = DAG.matchBinOpReduction(Extract, BinOp, {ISD::ADD}); | |||
41811 | ||||
41812 | // The operand is expected to be zero extended from i8 | |||
41813 | // (verified in detectZextAbsDiff). | |||
41814 | // In order to convert to i64 and above, additional any/zero/sign | |||
41815 | // extend is expected. | |||
41816 | // The zero extend from 32 bit has no mathematical effect on the result. | |||
41817 | // Also the sign extend is basically zero extend | |||
41818 | // (extends the sign bit which is zero). | |||
41819 | // So it is correct to skip the sign/zero extend instruction. | |||
41820 | if (Root && (Root.getOpcode() == ISD::SIGN_EXTEND || | |||
41821 | Root.getOpcode() == ISD::ZERO_EXTEND || | |||
41822 | Root.getOpcode() == ISD::ANY_EXTEND)) | |||
41823 | Root = Root.getOperand(0); | |||
41824 | ||||
41825 | // If there was a match, we want Root to be a select that is the root of an | |||
41826 | // abs-diff pattern. | |||
41827 | if (!Root || Root.getOpcode() != ISD::ABS) | |||
41828 | return SDValue(); | |||
41829 | ||||
41830 | // Check whether we have an abs-diff pattern feeding into the select. | |||
41831 | SDValue Zext0, Zext1; | |||
41832 | if (!detectZextAbsDiff(Root, Zext0, Zext1)) | |||
41833 | return SDValue(); | |||
41834 | ||||
41835 | // Create the SAD instruction. | |||
41836 | SDLoc DL(Extract); | |||
41837 | SDValue SAD = createPSADBW(DAG, Zext0, Zext1, DL, Subtarget); | |||
41838 | ||||
41839 | // If the original vector was wider than 8 elements, sum over the results | |||
41840 | // in the SAD vector. | |||
41841 | unsigned Stages = Log2_32(VT.getVectorNumElements()); | |||
41842 | EVT SadVT = SAD.getValueType(); | |||
41843 | if (Stages > 3) { | |||
41844 | unsigned SadElems = SadVT.getVectorNumElements(); | |||
41845 | ||||
41846 | for(unsigned i = Stages - 3; i > 0; --i) { | |||
41847 | SmallVector<int, 16> Mask(SadElems, -1); | |||
41848 | for(unsigned j = 0, MaskEnd = 1 << (i - 1); j < MaskEnd; ++j) | |||
41849 | Mask[j] = MaskEnd + j; | |||
41850 | ||||
41851 | SDValue Shuffle = | |||
41852 | DAG.getVectorShuffle(SadVT, DL, SAD, DAG.getUNDEF(SadVT), Mask); | |||
41853 | SAD = DAG.getNode(ISD::ADD, DL, SadVT, SAD, Shuffle); | |||
41854 | } | |||
41855 | } | |||
41856 | ||||
41857 | unsigned ExtractSizeInBits = ExtractVT.getSizeInBits(); | |||
41858 | // Return the lowest ExtractSizeInBits bits. | |||
41859 | EVT ResVT = EVT::getVectorVT(*DAG.getContext(), ExtractVT, | |||
41860 | SadVT.getSizeInBits() / ExtractSizeInBits); | |||
41861 | SAD = DAG.getBitcast(ResVT, SAD); | |||
41862 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ExtractVT, SAD, | |||
41863 | Extract->getOperand(1)); | |||
41864 | } | |||
41865 | ||||
41866 | // Attempt to peek through a target shuffle and extract the scalar from the | |||
41867 | // source. | |||
41868 | static SDValue combineExtractWithShuffle(SDNode *N, SelectionDAG &DAG, | |||
41869 | TargetLowering::DAGCombinerInfo &DCI, | |||
41870 | const X86Subtarget &Subtarget) { | |||
41871 | if (DCI.isBeforeLegalizeOps()) | |||
41872 | return SDValue(); | |||
41873 | ||||
41874 | SDLoc dl(N); | |||
41875 | SDValue Src = N->getOperand(0); | |||
41876 | SDValue Idx = N->getOperand(1); | |||
41877 | ||||
41878 | EVT VT = N->getValueType(0); | |||
41879 | EVT SrcVT = Src.getValueType(); | |||
41880 | EVT SrcSVT = SrcVT.getVectorElementType(); | |||
41881 | unsigned SrcEltBits = SrcSVT.getSizeInBits(); | |||
41882 | unsigned NumSrcElts = SrcVT.getVectorNumElements(); | |||
41883 | ||||
41884 | // Don't attempt this for boolean mask vectors or unknown extraction indices. | |||
41885 | if (SrcSVT == MVT::i1 || !isa<ConstantSDNode>(Idx)) | |||
41886 | return SDValue(); | |||
41887 | ||||
41888 | const APInt &IdxC = N->getConstantOperandAPInt(1); | |||
41889 | if (IdxC.uge(NumSrcElts)) | |||
41890 | return SDValue(); | |||
41891 | ||||
41892 | SDValue SrcBC = peekThroughBitcasts(Src); | |||
41893 | ||||
41894 | // Handle extract(bitcast(broadcast(scalar_value))). | |||
41895 | if (X86ISD::VBROADCAST == SrcBC.getOpcode()) { | |||
41896 | SDValue SrcOp = SrcBC.getOperand(0); | |||
41897 | EVT SrcOpVT = SrcOp.getValueType(); | |||
41898 | if (SrcOpVT.isScalarInteger() && VT.isInteger() && | |||
41899 | (SrcOpVT.getSizeInBits() % SrcEltBits) == 0) { | |||
41900 | unsigned Scale = SrcOpVT.getSizeInBits() / SrcEltBits; | |||
41901 | unsigned Offset = IdxC.urem(Scale) * SrcEltBits; | |||
41902 | // TODO support non-zero offsets. | |||
41903 | if (Offset == 0) { | |||
41904 | SrcOp = DAG.getZExtOrTrunc(SrcOp, dl, SrcVT.getScalarType()); | |||
41905 | SrcOp = DAG.getZExtOrTrunc(SrcOp, dl, VT); | |||
41906 | return SrcOp; | |||
41907 | } | |||
41908 | } | |||
41909 | } | |||
41910 | ||||
41911 | // If we're extracting a single element from a broadcast load and there are | |||
41912 | // no other users, just create a single load. | |||
41913 | if (SrcBC.getOpcode() == X86ISD::VBROADCAST_LOAD && SrcBC.hasOneUse()) { | |||
41914 | auto *MemIntr = cast<MemIntrinsicSDNode>(SrcBC); | |||
41915 | unsigned SrcBCWidth = SrcBC.getScalarValueSizeInBits(); | |||
41916 | if (MemIntr->getMemoryVT().getSizeInBits() == SrcBCWidth && | |||
41917 | VT.getSizeInBits() == SrcBCWidth && SrcEltBits == SrcBCWidth) { | |||
41918 | SDValue Load = DAG.getLoad(VT, dl, MemIntr->getChain(), | |||
41919 | MemIntr->getBasePtr(), | |||
41920 | MemIntr->getPointerInfo(), | |||
41921 | MemIntr->getOriginalAlign(), | |||
41922 | MemIntr->getMemOperand()->getFlags()); | |||
41923 | DAG.ReplaceAllUsesOfValueWith(SDValue(MemIntr, 1), Load.getValue(1)); | |||
41924 | return Load; | |||
41925 | } | |||
41926 | } | |||
41927 | ||||
41928 | // Handle extract(bitcast(scalar_to_vector(scalar_value))) for integers. | |||
41929 | // TODO: Move to DAGCombine? | |||
41930 | if (SrcBC.getOpcode() == ISD::SCALAR_TO_VECTOR && VT.isInteger() && | |||
41931 | SrcBC.getValueType().isInteger() && | |||
41932 | (SrcBC.getScalarValueSizeInBits() % SrcEltBits) == 0 && | |||
41933 | SrcBC.getScalarValueSizeInBits() == | |||
41934 | SrcBC.getOperand(0).getValueSizeInBits()) { | |||
41935 | unsigned Scale = SrcBC.getScalarValueSizeInBits() / SrcEltBits; | |||
41936 | if (IdxC.ult(Scale)) { | |||
41937 | unsigned Offset = IdxC.getZExtValue() * SrcVT.getScalarSizeInBits(); | |||
41938 | SDValue Scl = SrcBC.getOperand(0); | |||
41939 | EVT SclVT = Scl.getValueType(); | |||
41940 | if (Offset) { | |||
41941 | Scl = DAG.getNode(ISD::SRL, dl, SclVT, Scl, | |||
41942 | DAG.getShiftAmountConstant(Offset, SclVT, dl)); | |||
41943 | } | |||
41944 | Scl = DAG.getZExtOrTrunc(Scl, dl, SrcVT.getScalarType()); | |||
41945 | Scl = DAG.getZExtOrTrunc(Scl, dl, VT); | |||
41946 | return Scl; | |||
41947 | } | |||
41948 | } | |||
41949 | ||||
41950 | // Handle extract(truncate(x)) for 0'th index. | |||
41951 | // TODO: Treat this as a faux shuffle? | |||
41952 | // TODO: When can we use this for general indices? | |||
41953 | if (ISD::TRUNCATE == Src.getOpcode() && IdxC == 0 && | |||
41954 | (SrcVT.getSizeInBits() % 128) == 0) { | |||
41955 | Src = extract128BitVector(Src.getOperand(0), 0, DAG, dl); | |||
41956 | MVT ExtractVT = MVT::getVectorVT(SrcSVT.getSimpleVT(), 128 / SrcEltBits); | |||
41957 | return DAG.getNode(N->getOpcode(), dl, VT, DAG.getBitcast(ExtractVT, Src), | |||
41958 | Idx); | |||
41959 | } | |||
41960 | ||||
41961 | // We can only legally extract other elements from 128-bit vectors and in | |||
41962 | // certain circumstances, depending on SSE-level. | |||
41963 | // TODO: Investigate float/double extraction if it will be just stored. | |||
41964 | auto GetLegalExtract = [&Subtarget, &DAG, &dl](SDValue Vec, EVT VecVT, | |||
41965 | unsigned Idx) { | |||
41966 | EVT VecSVT = VecVT.getScalarType(); | |||
41967 | if ((VecVT.is256BitVector() || VecVT.is512BitVector()) && | |||
41968 | (VecSVT == MVT::i8 || VecSVT == MVT::i16 || VecSVT == MVT::i32 || | |||
41969 | VecSVT == MVT::i64)) { | |||
41970 | unsigned EltSizeInBits = VecSVT.getSizeInBits(); | |||
41971 | unsigned NumEltsPerLane = 128 / EltSizeInBits; | |||
41972 | unsigned LaneOffset = (Idx & ~(NumEltsPerLane - 1)) * EltSizeInBits; | |||
41973 | unsigned LaneIdx = LaneOffset / Vec.getScalarValueSizeInBits(); | |||
41974 | VecVT = EVT::getVectorVT(*DAG.getContext(), VecSVT, NumEltsPerLane); | |||
41975 | Vec = extract128BitVector(Vec, LaneIdx, DAG, dl); | |||
41976 | Idx &= (NumEltsPerLane - 1); | |||
41977 | } | |||
41978 | if ((VecVT == MVT::v4i32 || VecVT == MVT::v2i64) && | |||
41979 | ((Idx == 0 && Subtarget.hasSSE2()) || Subtarget.hasSSE41())) { | |||
41980 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VecVT.getScalarType(), | |||
41981 | DAG.getBitcast(VecVT, Vec), | |||
41982 | DAG.getIntPtrConstant(Idx, dl)); | |||
41983 | } | |||
41984 | if ((VecVT == MVT::v8i16 && Subtarget.hasSSE2()) || | |||
41985 | (VecVT == MVT::v16i8 && Subtarget.hasSSE41())) { | |||
41986 | unsigned OpCode = (VecVT == MVT::v8i16 ? X86ISD::PEXTRW : X86ISD::PEXTRB); | |||
41987 | return DAG.getNode(OpCode, dl, MVT::i32, DAG.getBitcast(VecVT, Vec), | |||
41988 | DAG.getTargetConstant(Idx, dl, MVT::i8)); | |||
41989 | } | |||
41990 | return SDValue(); | |||
41991 | }; | |||
41992 | ||||
41993 | // Resolve the target shuffle inputs and mask. | |||
41994 | SmallVector<int, 16> Mask; | |||
41995 | SmallVector<SDValue, 2> Ops; | |||
41996 | if (!getTargetShuffleInputs(SrcBC, Ops, Mask, DAG)) | |||
41997 | return SDValue(); | |||
41998 | ||||
41999 | // Shuffle inputs must be the same size as the result. | |||
42000 | if (llvm::any_of(Ops, [SrcVT](SDValue Op) { | |||
42001 | return SrcVT.getSizeInBits() != Op.getValueSizeInBits(); | |||
42002 | })) | |||
42003 | return SDValue(); | |||
42004 | ||||
42005 | // Attempt to narrow/widen the shuffle mask to the correct size. | |||
42006 | if (Mask.size() != NumSrcElts) { | |||
42007 | if ((NumSrcElts % Mask.size()) == 0) { | |||
42008 | SmallVector<int, 16> ScaledMask; | |||
42009 | int Scale = NumSrcElts / Mask.size(); | |||
42010 | narrowShuffleMaskElts(Scale, Mask, ScaledMask); | |||
42011 | Mask = std::move(ScaledMask); | |||
42012 | } else if ((Mask.size() % NumSrcElts) == 0) { | |||
42013 | // Simplify Mask based on demanded element. | |||
42014 | int ExtractIdx = (int)IdxC.getZExtValue(); | |||
42015 | int Scale = Mask.size() / NumSrcElts; | |||
42016 | int Lo = Scale * ExtractIdx; | |||
42017 | int Hi = Scale * (ExtractIdx + 1); | |||
42018 | for (int i = 0, e = (int)Mask.size(); i != e; ++i) | |||
42019 | if (i < Lo || Hi <= i) | |||
42020 | Mask[i] = SM_SentinelUndef; | |||
42021 | ||||
42022 | SmallVector<int, 16> WidenedMask; | |||
42023 | while (Mask.size() > NumSrcElts && | |||
42024 | canWidenShuffleElements(Mask, WidenedMask)) | |||
42025 | Mask = std::move(WidenedMask); | |||
42026 | } | |||
42027 | } | |||
42028 | ||||
42029 | // If narrowing/widening failed, see if we can extract+zero-extend. | |||
42030 | int ExtractIdx; | |||
42031 | EVT ExtractVT; | |||
42032 | if (Mask.size() == NumSrcElts) { | |||
42033 | ExtractIdx = Mask[IdxC.getZExtValue()]; | |||
42034 | ExtractVT = SrcVT; | |||
42035 | } else { | |||
42036 | unsigned Scale = Mask.size() / NumSrcElts; | |||
42037 | if ((Mask.size() % NumSrcElts) != 0 || SrcVT.isFloatingPoint()) | |||
42038 | return SDValue(); | |||
42039 | unsigned ScaledIdx = Scale * IdxC.getZExtValue(); | |||
42040 | if (!isUndefOrZeroInRange(Mask, ScaledIdx + 1, Scale - 1)) | |||
42041 | return SDValue(); | |||
42042 | ExtractIdx = Mask[ScaledIdx]; | |||
42043 | EVT ExtractSVT = EVT::getIntegerVT(*DAG.getContext(), SrcEltBits / Scale); | |||
42044 | ExtractVT = EVT::getVectorVT(*DAG.getContext(), ExtractSVT, Mask.size()); | |||
42045 | assert(SrcVT.getSizeInBits() == ExtractVT.getSizeInBits() &&(static_cast <bool> (SrcVT.getSizeInBits() == ExtractVT .getSizeInBits() && "Failed to widen vector type") ? void (0) : __assert_fail ("SrcVT.getSizeInBits() == ExtractVT.getSizeInBits() && \"Failed to widen vector type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 42046, __extension__ __PRETTY_FUNCTION__)) | |||
42046 | "Failed to widen vector type")(static_cast <bool> (SrcVT.getSizeInBits() == ExtractVT .getSizeInBits() && "Failed to widen vector type") ? void (0) : __assert_fail ("SrcVT.getSizeInBits() == ExtractVT.getSizeInBits() && \"Failed to widen vector type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 42046, __extension__ __PRETTY_FUNCTION__)); | |||
42047 | } | |||
42048 | ||||
42049 | // If the shuffle source element is undef/zero then we can just accept it. | |||
42050 | if (ExtractIdx == SM_SentinelUndef) | |||
42051 | return DAG.getUNDEF(VT); | |||
42052 | ||||
42053 | if (ExtractIdx == SM_SentinelZero) | |||
42054 | return VT.isFloatingPoint() ? DAG.getConstantFP(0.0, dl, VT) | |||
42055 | : DAG.getConstant(0, dl, VT); | |||
42056 | ||||
42057 | SDValue SrcOp = Ops[ExtractIdx / Mask.size()]; | |||
42058 | ExtractIdx = ExtractIdx % Mask.size(); | |||
42059 | if (SDValue V = GetLegalExtract(SrcOp, ExtractVT, ExtractIdx)) | |||
42060 | return DAG.getZExtOrTrunc(V, dl, VT); | |||
42061 | ||||
42062 | return SDValue(); | |||
42063 | } | |||
42064 | ||||
42065 | /// Extracting a scalar FP value from vector element 0 is free, so extract each | |||
42066 | /// operand first, then perform the math as a scalar op. | |||
42067 | static SDValue scalarizeExtEltFP(SDNode *ExtElt, SelectionDAG &DAG, | |||
42068 | const X86Subtarget &Subtarget) { | |||
42069 | assert(ExtElt->getOpcode() == ISD::EXTRACT_VECTOR_ELT && "Expected extract")(static_cast <bool> (ExtElt->getOpcode() == ISD::EXTRACT_VECTOR_ELT && "Expected extract") ? void (0) : __assert_fail ("ExtElt->getOpcode() == ISD::EXTRACT_VECTOR_ELT && \"Expected extract\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 42069, __extension__ __PRETTY_FUNCTION__)); | |||
42070 | SDValue Vec = ExtElt->getOperand(0); | |||
42071 | SDValue Index = ExtElt->getOperand(1); | |||
42072 | EVT VT = ExtElt->getValueType(0); | |||
42073 | EVT VecVT = Vec.getValueType(); | |||
42074 | ||||
42075 | // TODO: If this is a unary/expensive/expand op, allow extraction from a | |||
42076 | // non-zero element because the shuffle+scalar op will be cheaper? | |||
42077 | if (!Vec.hasOneUse() || !isNullConstant(Index) || VecVT.getScalarType() != VT) | |||
42078 | return SDValue(); | |||
42079 | ||||
42080 | // Vector FP compares don't fit the pattern of FP math ops (propagate, not | |||
42081 | // extract, the condition code), so deal with those as a special-case. | |||
42082 | if (Vec.getOpcode() == ISD::SETCC && VT == MVT::i1) { | |||
42083 | EVT OpVT = Vec.getOperand(0).getValueType().getScalarType(); | |||
42084 | if (OpVT != MVT::f32 && OpVT != MVT::f64) | |||
42085 | return SDValue(); | |||
42086 | ||||
42087 | // extract (setcc X, Y, CC), 0 --> setcc (extract X, 0), (extract Y, 0), CC | |||
42088 | SDLoc DL(ExtElt); | |||
42089 | SDValue Ext0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, OpVT, | |||
42090 | Vec.getOperand(0), Index); | |||
42091 | SDValue Ext1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, OpVT, | |||
42092 | Vec.getOperand(1), Index); | |||
42093 | return DAG.getNode(Vec.getOpcode(), DL, VT, Ext0, Ext1, Vec.getOperand(2)); | |||
42094 | } | |||
42095 | ||||
42096 | if (!(VT == MVT::f16 && Subtarget.hasFP16()) && VT != MVT::f32 && | |||
42097 | VT != MVT::f64) | |||
42098 | return SDValue(); | |||
42099 | ||||
42100 | // Vector FP selects don't fit the pattern of FP math ops (because the | |||
42101 | // condition has a different type and we have to change the opcode), so deal | |||
42102 | // with those here. | |||
42103 | // FIXME: This is restricted to pre type legalization by ensuring the setcc | |||
42104 | // has i1 elements. If we loosen this we need to convert vector bool to a | |||
42105 | // scalar bool. | |||
42106 | if (Vec.getOpcode() == ISD::VSELECT && | |||
42107 | Vec.getOperand(0).getOpcode() == ISD::SETCC && | |||
42108 | Vec.getOperand(0).getValueType().getScalarType() == MVT::i1 && | |||
42109 | Vec.getOperand(0).getOperand(0).getValueType() == VecVT) { | |||
42110 | // ext (sel Cond, X, Y), 0 --> sel (ext Cond, 0), (ext X, 0), (ext Y, 0) | |||
42111 | SDLoc DL(ExtElt); | |||
42112 | SDValue Ext0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, | |||
42113 | Vec.getOperand(0).getValueType().getScalarType(), | |||
42114 | Vec.getOperand(0), Index); | |||
42115 | SDValue Ext1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, | |||
42116 | Vec.getOperand(1), Index); | |||
42117 | SDValue Ext2 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, | |||
42118 | Vec.getOperand(2), Index); | |||
42119 | return DAG.getNode(ISD::SELECT, DL, VT, Ext0, Ext1, Ext2); | |||
42120 | } | |||
42121 | ||||
42122 | // TODO: This switch could include FNEG and the x86-specific FP logic ops | |||
42123 | // (FAND, FANDN, FOR, FXOR). But that may require enhancements to avoid | |||
42124 | // missed load folding and fma+fneg combining. | |||
42125 | switch (Vec.getOpcode()) { | |||
42126 | case ISD::FMA: // Begin 3 operands | |||
42127 | case ISD::FMAD: | |||
42128 | case ISD::FADD: // Begin 2 operands | |||
42129 | case ISD::FSUB: | |||
42130 | case ISD::FMUL: | |||
42131 | case ISD::FDIV: | |||
42132 | case ISD::FREM: | |||
42133 | case ISD::FCOPYSIGN: | |||
42134 | case ISD::FMINNUM: | |||
42135 | case ISD::FMAXNUM: | |||
42136 | case ISD::FMINNUM_IEEE: | |||
42137 | case ISD::FMAXNUM_IEEE: | |||
42138 | case ISD::FMAXIMUM: | |||
42139 | case ISD::FMINIMUM: | |||
42140 | case X86ISD::FMAX: | |||
42141 | case X86ISD::FMIN: | |||
42142 | case ISD::FABS: // Begin 1 operand | |||
42143 | case ISD::FSQRT: | |||
42144 | case ISD::FRINT: | |||
42145 | case ISD::FCEIL: | |||
42146 | case ISD::FTRUNC: | |||
42147 | case ISD::FNEARBYINT: | |||
42148 | case ISD::FROUND: | |||
42149 | case ISD::FFLOOR: | |||
42150 | case X86ISD::FRCP: | |||
42151 | case X86ISD::FRSQRT: { | |||
42152 | // extract (fp X, Y, ...), 0 --> fp (extract X, 0), (extract Y, 0), ... | |||
42153 | SDLoc DL(ExtElt); | |||
42154 | SmallVector<SDValue, 4> ExtOps; | |||
42155 | for (SDValue Op : Vec->ops()) | |||
42156 | ExtOps.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, Op, Index)); | |||
42157 | return DAG.getNode(Vec.getOpcode(), DL, VT, ExtOps); | |||
42158 | } | |||
42159 | default: | |||
42160 | return SDValue(); | |||
42161 | } | |||
42162 | llvm_unreachable("All opcodes should return within switch")::llvm::llvm_unreachable_internal("All opcodes should return within switch" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 42162); | |||
42163 | } | |||
42164 | ||||
42165 | /// Try to convert a vector reduction sequence composed of binops and shuffles | |||
42166 | /// into horizontal ops. | |||
42167 | static SDValue combineArithReduction(SDNode *ExtElt, SelectionDAG &DAG, | |||
42168 | const X86Subtarget &Subtarget) { | |||
42169 | assert(ExtElt->getOpcode() == ISD::EXTRACT_VECTOR_ELT && "Unexpected caller")(static_cast <bool> (ExtElt->getOpcode() == ISD::EXTRACT_VECTOR_ELT && "Unexpected caller") ? void (0) : __assert_fail ( "ExtElt->getOpcode() == ISD::EXTRACT_VECTOR_ELT && \"Unexpected caller\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 42169, __extension__ __PRETTY_FUNCTION__)); | |||
42170 | ||||
42171 | // We need at least SSE2 to anything here. | |||
42172 | if (!Subtarget.hasSSE2()) | |||
42173 | return SDValue(); | |||
42174 | ||||
42175 | ISD::NodeType Opc; | |||
42176 | SDValue Rdx = DAG.matchBinOpReduction(ExtElt, Opc, | |||
42177 | {ISD::ADD, ISD::MUL, ISD::FADD}, true); | |||
42178 | if (!Rdx) | |||
42179 | return SDValue(); | |||
42180 | ||||
42181 | SDValue Index = ExtElt->getOperand(1); | |||
42182 | assert(isNullConstant(Index) &&(static_cast <bool> (isNullConstant(Index) && "Reduction doesn't end in an extract from index 0" ) ? void (0) : __assert_fail ("isNullConstant(Index) && \"Reduction doesn't end in an extract from index 0\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 42183, __extension__ __PRETTY_FUNCTION__)) | |||
42183 | "Reduction doesn't end in an extract from index 0")(static_cast <bool> (isNullConstant(Index) && "Reduction doesn't end in an extract from index 0" ) ? void (0) : __assert_fail ("isNullConstant(Index) && \"Reduction doesn't end in an extract from index 0\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 42183, __extension__ __PRETTY_FUNCTION__)); | |||
42184 | ||||
42185 | EVT VT = ExtElt->getValueType(0); | |||
42186 | EVT VecVT = Rdx.getValueType(); | |||
42187 | if (VecVT.getScalarType() != VT) | |||
42188 | return SDValue(); | |||
42189 | ||||
42190 | SDLoc DL(ExtElt); | |||
42191 | ||||
42192 | // vXi8 mul reduction - promote to vXi16 mul reduction. | |||
42193 | if (Opc == ISD::MUL) { | |||
42194 | unsigned NumElts = VecVT.getVectorNumElements(); | |||
42195 | if (VT != MVT::i8 || NumElts < 4 || !isPowerOf2_32(NumElts)) | |||
42196 | return SDValue(); | |||
42197 | if (VecVT.getSizeInBits() >= 128) { | |||
42198 | EVT WideVT = EVT::getVectorVT(*DAG.getContext(), MVT::i16, NumElts / 2); | |||
42199 | SDValue Lo = getUnpackl(DAG, DL, VecVT, Rdx, DAG.getUNDEF(VecVT)); | |||
42200 | SDValue Hi = getUnpackh(DAG, DL, VecVT, Rdx, DAG.getUNDEF(VecVT)); | |||
42201 | Lo = DAG.getBitcast(WideVT, Lo); | |||
42202 | Hi = DAG.getBitcast(WideVT, Hi); | |||
42203 | Rdx = DAG.getNode(Opc, DL, WideVT, Lo, Hi); | |||
42204 | while (Rdx.getValueSizeInBits() > 128) { | |||
42205 | std::tie(Lo, Hi) = splitVector(Rdx, DAG, DL); | |||
42206 | Rdx = DAG.getNode(Opc, DL, Lo.getValueType(), Lo, Hi); | |||
42207 | } | |||
42208 | } else { | |||
42209 | if (VecVT == MVT::v4i8) | |||
42210 | Rdx = DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v8i8, Rdx, | |||
42211 | DAG.getUNDEF(MVT::v4i8)); | |||
42212 | Rdx = DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v16i8, Rdx, | |||
42213 | DAG.getUNDEF(MVT::v8i8)); | |||
42214 | Rdx = getUnpackl(DAG, DL, MVT::v16i8, Rdx, DAG.getUNDEF(MVT::v16i8)); | |||
42215 | Rdx = DAG.getBitcast(MVT::v8i16, Rdx); | |||
42216 | } | |||
42217 | if (NumElts >= 8) | |||
42218 | Rdx = DAG.getNode(Opc, DL, MVT::v8i16, Rdx, | |||
42219 | DAG.getVectorShuffle(MVT::v8i16, DL, Rdx, Rdx, | |||
42220 | {4, 5, 6, 7, -1, -1, -1, -1})); | |||
42221 | Rdx = DAG.getNode(Opc, DL, MVT::v8i16, Rdx, | |||
42222 | DAG.getVectorShuffle(MVT::v8i16, DL, Rdx, Rdx, | |||
42223 | {2, 3, -1, -1, -1, -1, -1, -1})); | |||
42224 | Rdx = DAG.getNode(Opc, DL, MVT::v8i16, Rdx, | |||
42225 | DAG.getVectorShuffle(MVT::v8i16, DL, Rdx, Rdx, | |||
42226 | {1, -1, -1, -1, -1, -1, -1, -1})); | |||
42227 | Rdx = DAG.getBitcast(MVT::v16i8, Rdx); | |||
42228 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, Rdx, Index); | |||
42229 | } | |||
42230 | ||||
42231 | // vXi8 add reduction - sub 128-bit vector. | |||
42232 | if (VecVT == MVT::v4i8 || VecVT == MVT::v8i8) { | |||
42233 | if (VecVT == MVT::v4i8) { | |||
42234 | // Pad with zero. | |||
42235 | if (Subtarget.hasSSE41()) { | |||
42236 | Rdx = DAG.getBitcast(MVT::i32, Rdx); | |||
42237 | Rdx = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, MVT::v4i32, | |||
42238 | DAG.getConstant(0, DL, MVT::v4i32), Rdx, | |||
42239 | DAG.getIntPtrConstant(0, DL)); | |||
42240 | Rdx = DAG.getBitcast(MVT::v16i8, Rdx); | |||
42241 | } else { | |||
42242 | Rdx = DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v8i8, Rdx, | |||
42243 | DAG.getConstant(0, DL, VecVT)); | |||
42244 | } | |||
42245 | } | |||
42246 | if (Rdx.getValueType() == MVT::v8i8) { | |||
42247 | // Pad with undef. | |||
42248 | Rdx = DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v16i8, Rdx, | |||
42249 | DAG.getUNDEF(MVT::v8i8)); | |||
42250 | } | |||
42251 | Rdx = DAG.getNode(X86ISD::PSADBW, DL, MVT::v2i64, Rdx, | |||
42252 | DAG.getConstant(0, DL, MVT::v16i8)); | |||
42253 | Rdx = DAG.getBitcast(MVT::v16i8, Rdx); | |||
42254 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, Rdx, Index); | |||
42255 | } | |||
42256 | ||||
42257 | // Must be a >=128-bit vector with pow2 elements. | |||
42258 | if ((VecVT.getSizeInBits() % 128) != 0 || | |||
42259 | !isPowerOf2_32(VecVT.getVectorNumElements())) | |||
42260 | return SDValue(); | |||
42261 | ||||
42262 | // vXi8 add reduction - sum lo/hi halves then use PSADBW. | |||
42263 | if (VT == MVT::i8) { | |||
42264 | while (Rdx.getValueSizeInBits() > 128) { | |||
42265 | SDValue Lo, Hi; | |||
42266 | std::tie(Lo, Hi) = splitVector(Rdx, DAG, DL); | |||
42267 | VecVT = Lo.getValueType(); | |||
42268 | Rdx = DAG.getNode(ISD::ADD, DL, VecVT, Lo, Hi); | |||
42269 | } | |||
42270 | assert(VecVT == MVT::v16i8 && "v16i8 reduction expected")(static_cast <bool> (VecVT == MVT::v16i8 && "v16i8 reduction expected" ) ? void (0) : __assert_fail ("VecVT == MVT::v16i8 && \"v16i8 reduction expected\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 42270, __extension__ __PRETTY_FUNCTION__)); | |||
42271 | ||||
42272 | SDValue Hi = DAG.getVectorShuffle( | |||
42273 | MVT::v16i8, DL, Rdx, Rdx, | |||
42274 | {8, 9, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1}); | |||
42275 | Rdx = DAG.getNode(ISD::ADD, DL, MVT::v16i8, Rdx, Hi); | |||
42276 | Rdx = DAG.getNode(X86ISD::PSADBW, DL, MVT::v2i64, Rdx, | |||
42277 | getZeroVector(MVT::v16i8, Subtarget, DAG, DL)); | |||
42278 | Rdx = DAG.getBitcast(MVT::v16i8, Rdx); | |||
42279 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, Rdx, Index); | |||
42280 | } | |||
42281 | ||||
42282 | // Only use (F)HADD opcodes if they aren't microcoded or minimizes codesize. | |||
42283 | if (!shouldUseHorizontalOp(true, DAG, Subtarget)) | |||
42284 | return SDValue(); | |||
42285 | ||||
42286 | unsigned HorizOpcode = Opc == ISD::ADD ? X86ISD::HADD : X86ISD::FHADD; | |||
42287 | ||||
42288 | // 256-bit horizontal instructions operate on 128-bit chunks rather than | |||
42289 | // across the whole vector, so we need an extract + hop preliminary stage. | |||
42290 | // This is the only step where the operands of the hop are not the same value. | |||
42291 | // TODO: We could extend this to handle 512-bit or even longer vectors. | |||
42292 | if (((VecVT == MVT::v16i16 || VecVT == MVT::v8i32) && Subtarget.hasSSSE3()) || | |||
42293 | ((VecVT == MVT::v8f32 || VecVT == MVT::v4f64) && Subtarget.hasSSE3())) { | |||
42294 | unsigned NumElts = VecVT.getVectorNumElements(); | |||
42295 | SDValue Hi = extract128BitVector(Rdx, NumElts / 2, DAG, DL); | |||
42296 | SDValue Lo = extract128BitVector(Rdx, 0, DAG, DL); | |||
42297 | Rdx = DAG.getNode(HorizOpcode, DL, Lo.getValueType(), Hi, Lo); | |||
42298 | VecVT = Rdx.getValueType(); | |||
42299 | } | |||
42300 | if (!((VecVT == MVT::v8i16 || VecVT == MVT::v4i32) && Subtarget.hasSSSE3()) && | |||
42301 | !((VecVT == MVT::v4f32 || VecVT == MVT::v2f64) && Subtarget.hasSSE3())) | |||
42302 | return SDValue(); | |||
42303 | ||||
42304 | // extract (add (shuf X), X), 0 --> extract (hadd X, X), 0 | |||
42305 | unsigned ReductionSteps = Log2_32(VecVT.getVectorNumElements()); | |||
42306 | for (unsigned i = 0; i != ReductionSteps; ++i) | |||
42307 | Rdx = DAG.getNode(HorizOpcode, DL, VecVT, Rdx, Rdx); | |||
42308 | ||||
42309 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, Rdx, Index); | |||
42310 | } | |||
42311 | ||||
42312 | /// Detect vector gather/scatter index generation and convert it from being a | |||
42313 | /// bunch of shuffles and extracts into a somewhat faster sequence. | |||
42314 | /// For i686, the best sequence is apparently storing the value and loading | |||
42315 | /// scalars back, while for x64 we should use 64-bit extracts and shifts. | |||
42316 | static SDValue combineExtractVectorElt(SDNode *N, SelectionDAG &DAG, | |||
42317 | TargetLowering::DAGCombinerInfo &DCI, | |||
42318 | const X86Subtarget &Subtarget) { | |||
42319 | if (SDValue NewOp = combineExtractWithShuffle(N, DAG, DCI, Subtarget)) | |||
42320 | return NewOp; | |||
42321 | ||||
42322 | SDValue InputVector = N->getOperand(0); | |||
42323 | SDValue EltIdx = N->getOperand(1); | |||
42324 | auto *CIdx = dyn_cast<ConstantSDNode>(EltIdx); | |||
42325 | ||||
42326 | EVT SrcVT = InputVector.getValueType(); | |||
42327 | EVT VT = N->getValueType(0); | |||
42328 | SDLoc dl(InputVector); | |||
42329 | bool IsPextr = N->getOpcode() != ISD::EXTRACT_VECTOR_ELT; | |||
42330 | unsigned NumSrcElts = SrcVT.getVectorNumElements(); | |||
42331 | ||||
42332 | if (CIdx && CIdx->getAPIntValue().uge(NumSrcElts)) | |||
42333 | return IsPextr ? DAG.getConstant(0, dl, VT) : DAG.getUNDEF(VT); | |||
42334 | ||||
42335 | // Integer Constant Folding. | |||
42336 | if (CIdx && VT.isInteger()) { | |||
42337 | APInt UndefVecElts; | |||
42338 | SmallVector<APInt, 16> EltBits; | |||
42339 | unsigned VecEltBitWidth = SrcVT.getScalarSizeInBits(); | |||
42340 | if (getTargetConstantBitsFromNode(InputVector, VecEltBitWidth, UndefVecElts, | |||
42341 | EltBits, true, false)) { | |||
42342 | uint64_t Idx = CIdx->getZExtValue(); | |||
42343 | if (UndefVecElts[Idx]) | |||
42344 | return IsPextr ? DAG.getConstant(0, dl, VT) : DAG.getUNDEF(VT); | |||
42345 | return DAG.getConstant(EltBits[Idx].zextOrSelf(VT.getScalarSizeInBits()), | |||
42346 | dl, VT); | |||
42347 | } | |||
42348 | } | |||
42349 | ||||
42350 | if (IsPextr) { | |||
42351 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
42352 | if (TLI.SimplifyDemandedBits(SDValue(N, 0), | |||
42353 | APInt::getAllOnes(VT.getSizeInBits()), DCI)) | |||
42354 | return SDValue(N, 0); | |||
42355 | ||||
42356 | // PEXTR*(PINSR*(v, s, c), c) -> s (with implicit zext handling). | |||
42357 | if ((InputVector.getOpcode() == X86ISD::PINSRB || | |||
42358 | InputVector.getOpcode() == X86ISD::PINSRW) && | |||
42359 | InputVector.getOperand(2) == EltIdx) { | |||
42360 | assert(SrcVT == InputVector.getOperand(0).getValueType() &&(static_cast <bool> (SrcVT == InputVector.getOperand(0) .getValueType() && "Vector type mismatch") ? void (0) : __assert_fail ("SrcVT == InputVector.getOperand(0).getValueType() && \"Vector type mismatch\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 42361, __extension__ __PRETTY_FUNCTION__)) | |||
42361 | "Vector type mismatch")(static_cast <bool> (SrcVT == InputVector.getOperand(0) .getValueType() && "Vector type mismatch") ? void (0) : __assert_fail ("SrcVT == InputVector.getOperand(0).getValueType() && \"Vector type mismatch\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 42361, __extension__ __PRETTY_FUNCTION__)); | |||
42362 | SDValue Scl = InputVector.getOperand(1); | |||
42363 | Scl = DAG.getNode(ISD::TRUNCATE, dl, SrcVT.getScalarType(), Scl); | |||
42364 | return DAG.getZExtOrTrunc(Scl, dl, VT); | |||
42365 | } | |||
42366 | ||||
42367 | // TODO - Remove this once we can handle the implicit zero-extension of | |||
42368 | // X86ISD::PEXTRW/X86ISD::PEXTRB in combinePredicateReduction and | |||
42369 | // combineBasicSADPattern. | |||
42370 | return SDValue(); | |||
42371 | } | |||
42372 | ||||
42373 | // Detect mmx extraction of all bits as a i64. It works better as a bitcast. | |||
42374 | if (InputVector.getOpcode() == ISD::BITCAST && InputVector.hasOneUse() && | |||
42375 | VT == MVT::i64 && SrcVT == MVT::v1i64 && isNullConstant(EltIdx)) { | |||
42376 | SDValue MMXSrc = InputVector.getOperand(0); | |||
42377 | ||||
42378 | // The bitcast source is a direct mmx result. | |||
42379 | if (MMXSrc.getValueType() == MVT::x86mmx) | |||
42380 | return DAG.getBitcast(VT, InputVector); | |||
42381 | } | |||
42382 | ||||
42383 | // Detect mmx to i32 conversion through a v2i32 elt extract. | |||
42384 | if (InputVector.getOpcode() == ISD::BITCAST && InputVector.hasOneUse() && | |||
42385 | VT == MVT::i32 && SrcVT == MVT::v2i32 && isNullConstant(EltIdx)) { | |||
42386 | SDValue MMXSrc = InputVector.getOperand(0); | |||
42387 | ||||
42388 | // The bitcast source is a direct mmx result. | |||
42389 | if (MMXSrc.getValueType() == MVT::x86mmx) | |||
42390 | return DAG.getNode(X86ISD::MMX_MOVD2W, dl, MVT::i32, MMXSrc); | |||
42391 | } | |||
42392 | ||||
42393 | // Check whether this extract is the root of a sum of absolute differences | |||
42394 | // pattern. This has to be done here because we really want it to happen | |||
42395 | // pre-legalization, | |||
42396 | if (SDValue SAD = combineBasicSADPattern(N, DAG, Subtarget)) | |||
42397 | return SAD; | |||
42398 | ||||
42399 | // Attempt to replace an all_of/any_of horizontal reduction with a MOVMSK. | |||
42400 | if (SDValue Cmp = combinePredicateReduction(N, DAG, Subtarget)) | |||
42401 | return Cmp; | |||
42402 | ||||
42403 | // Attempt to replace min/max v8i16/v16i8 reductions with PHMINPOSUW. | |||
42404 | if (SDValue MinMax = combineMinMaxReduction(N, DAG, Subtarget)) | |||
42405 | return MinMax; | |||
42406 | ||||
42407 | // Attempt to optimize ADD/FADD/MUL reductions with HADD, promotion etc.. | |||
42408 | if (SDValue V = combineArithReduction(N, DAG, Subtarget)) | |||
42409 | return V; | |||
42410 | ||||
42411 | if (SDValue V = scalarizeExtEltFP(N, DAG, Subtarget)) | |||
42412 | return V; | |||
42413 | ||||
42414 | // Attempt to extract a i1 element by using MOVMSK to extract the signbits | |||
42415 | // and then testing the relevant element. | |||
42416 | // | |||
42417 | // Note that we only combine extracts on the *same* result number, i.e. | |||
42418 | // t0 = merge_values a0, a1, a2, a3 | |||
42419 | // i1 = extract_vector_elt t0, Constant:i64<2> | |||
42420 | // i1 = extract_vector_elt t0, Constant:i64<3> | |||
42421 | // but not | |||
42422 | // i1 = extract_vector_elt t0:1, Constant:i64<2> | |||
42423 | // since the latter would need its own MOVMSK. | |||
42424 | if (CIdx && SrcVT.getScalarType() == MVT::i1) { | |||
42425 | SmallVector<SDNode *, 16> BoolExtracts; | |||
42426 | unsigned ResNo = InputVector.getResNo(); | |||
42427 | auto IsBoolExtract = [&BoolExtracts, &ResNo](SDNode *Use) { | |||
42428 | if (Use->getOpcode() == ISD::EXTRACT_VECTOR_ELT && | |||
42429 | isa<ConstantSDNode>(Use->getOperand(1)) && | |||
42430 | Use->getOperand(0).getResNo() == ResNo && | |||
42431 | Use->getValueType(0) == MVT::i1) { | |||
42432 | BoolExtracts.push_back(Use); | |||
42433 | return true; | |||
42434 | } | |||
42435 | return false; | |||
42436 | }; | |||
42437 | if (all_of(InputVector->uses(), IsBoolExtract) && | |||
42438 | BoolExtracts.size() > 1) { | |||
42439 | EVT BCVT = EVT::getIntegerVT(*DAG.getContext(), NumSrcElts); | |||
42440 | if (SDValue BC = | |||
42441 | combineBitcastvxi1(DAG, BCVT, InputVector, dl, Subtarget)) { | |||
42442 | for (SDNode *Use : BoolExtracts) { | |||
42443 | // extractelement vXi1 X, MaskIdx --> ((movmsk X) & Mask) == Mask | |||
42444 | unsigned MaskIdx = Use->getConstantOperandVal(1); | |||
42445 | APInt MaskBit = APInt::getOneBitSet(NumSrcElts, MaskIdx); | |||
42446 | SDValue Mask = DAG.getConstant(MaskBit, dl, BCVT); | |||
42447 | SDValue Res = DAG.getNode(ISD::AND, dl, BCVT, BC, Mask); | |||
42448 | Res = DAG.getSetCC(dl, MVT::i1, Res, Mask, ISD::SETEQ); | |||
42449 | DCI.CombineTo(Use, Res); | |||
42450 | } | |||
42451 | return SDValue(N, 0); | |||
42452 | } | |||
42453 | } | |||
42454 | } | |||
42455 | ||||
42456 | return SDValue(); | |||
42457 | } | |||
42458 | ||||
42459 | /// If a vector select has an operand that is -1 or 0, try to simplify the | |||
42460 | /// select to a bitwise logic operation. | |||
42461 | /// TODO: Move to DAGCombiner, possibly using TargetLowering::hasAndNot()? | |||
42462 | static SDValue | |||
42463 | combineVSelectWithAllOnesOrZeros(SDNode *N, SelectionDAG &DAG, | |||
42464 | TargetLowering::DAGCombinerInfo &DCI, | |||
42465 | const X86Subtarget &Subtarget) { | |||
42466 | SDValue Cond = N->getOperand(0); | |||
42467 | SDValue LHS = N->getOperand(1); | |||
42468 | SDValue RHS = N->getOperand(2); | |||
42469 | EVT VT = LHS.getValueType(); | |||
42470 | EVT CondVT = Cond.getValueType(); | |||
42471 | SDLoc DL(N); | |||
42472 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
42473 | ||||
42474 | if (N->getOpcode() != ISD::VSELECT) | |||
42475 | return SDValue(); | |||
42476 | ||||
42477 | assert(CondVT.isVector() && "Vector select expects a vector selector!")(static_cast <bool> (CondVT.isVector() && "Vector select expects a vector selector!" ) ? void (0) : __assert_fail ("CondVT.isVector() && \"Vector select expects a vector selector!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 42477, __extension__ __PRETTY_FUNCTION__)); | |||
42478 | ||||
42479 | // TODO: Use isNullOrNullSplat() to distinguish constants with undefs? | |||
42480 | // TODO: Can we assert that both operands are not zeros (because that should | |||
42481 | // get simplified at node creation time)? | |||
42482 | bool TValIsAllZeros = ISD::isBuildVectorAllZeros(LHS.getNode()); | |||
42483 | bool FValIsAllZeros = ISD::isBuildVectorAllZeros(RHS.getNode()); | |||
42484 | ||||
42485 | // If both inputs are 0/undef, create a complete zero vector. | |||
42486 | // FIXME: As noted above this should be handled by DAGCombiner/getNode. | |||
42487 | if (TValIsAllZeros && FValIsAllZeros) { | |||
42488 | if (VT.isFloatingPoint()) | |||
42489 | return DAG.getConstantFP(0.0, DL, VT); | |||
42490 | return DAG.getConstant(0, DL, VT); | |||
42491 | } | |||
42492 | ||||
42493 | // To use the condition operand as a bitwise mask, it must have elements that | |||
42494 | // are the same size as the select elements. Ie, the condition operand must | |||
42495 | // have already been promoted from the IR select condition type <N x i1>. | |||
42496 | // Don't check if the types themselves are equal because that excludes | |||
42497 | // vector floating-point selects. | |||
42498 | if (CondVT.getScalarSizeInBits() != VT.getScalarSizeInBits()) | |||
42499 | return SDValue(); | |||
42500 | ||||
42501 | // Try to invert the condition if true value is not all 1s and false value is | |||
42502 | // not all 0s. Only do this if the condition has one use. | |||
42503 | bool TValIsAllOnes = ISD::isBuildVectorAllOnes(LHS.getNode()); | |||
42504 | if (!TValIsAllOnes && !FValIsAllZeros && Cond.hasOneUse() && | |||
42505 | // Check if the selector will be produced by CMPP*/PCMP*. | |||
42506 | Cond.getOpcode() == ISD::SETCC && | |||
42507 | // Check if SETCC has already been promoted. | |||
42508 | TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT) == | |||
42509 | CondVT) { | |||
42510 | bool FValIsAllOnes = ISD::isBuildVectorAllOnes(RHS.getNode()); | |||
42511 | ||||
42512 | if (TValIsAllZeros || FValIsAllOnes) { | |||
42513 | SDValue CC = Cond.getOperand(2); | |||
42514 | ISD::CondCode NewCC = ISD::getSetCCInverse( | |||
42515 | cast<CondCodeSDNode>(CC)->get(), Cond.getOperand(0).getValueType()); | |||
42516 | Cond = DAG.getSetCC(DL, CondVT, Cond.getOperand(0), Cond.getOperand(1), | |||
42517 | NewCC); | |||
42518 | std::swap(LHS, RHS); | |||
42519 | TValIsAllOnes = FValIsAllOnes; | |||
42520 | FValIsAllZeros = TValIsAllZeros; | |||
42521 | } | |||
42522 | } | |||
42523 | ||||
42524 | // Cond value must be 'sign splat' to be converted to a logical op. | |||
42525 | if (DAG.ComputeNumSignBits(Cond) != CondVT.getScalarSizeInBits()) | |||
42526 | return SDValue(); | |||
42527 | ||||
42528 | // vselect Cond, 111..., 000... -> Cond | |||
42529 | if (TValIsAllOnes && FValIsAllZeros) | |||
42530 | return DAG.getBitcast(VT, Cond); | |||
42531 | ||||
42532 | if (!TLI.isTypeLegal(CondVT)) | |||
42533 | return SDValue(); | |||
42534 | ||||
42535 | // vselect Cond, 111..., X -> or Cond, X | |||
42536 | if (TValIsAllOnes) { | |||
42537 | SDValue CastRHS = DAG.getBitcast(CondVT, RHS); | |||
42538 | SDValue Or = DAG.getNode(ISD::OR, DL, CondVT, Cond, CastRHS); | |||
42539 | return DAG.getBitcast(VT, Or); | |||
42540 | } | |||
42541 | ||||
42542 | // vselect Cond, X, 000... -> and Cond, X | |||
42543 | if (FValIsAllZeros) { | |||
42544 | SDValue CastLHS = DAG.getBitcast(CondVT, LHS); | |||
42545 | SDValue And = DAG.getNode(ISD::AND, DL, CondVT, Cond, CastLHS); | |||
42546 | return DAG.getBitcast(VT, And); | |||
42547 | } | |||
42548 | ||||
42549 | // vselect Cond, 000..., X -> andn Cond, X | |||
42550 | if (TValIsAllZeros) { | |||
42551 | SDValue CastRHS = DAG.getBitcast(CondVT, RHS); | |||
42552 | SDValue AndN; | |||
42553 | // The canonical form differs for i1 vectors - x86andnp is not used | |||
42554 | if (CondVT.getScalarType() == MVT::i1) | |||
42555 | AndN = DAG.getNode(ISD::AND, DL, CondVT, DAG.getNOT(DL, Cond, CondVT), | |||
42556 | CastRHS); | |||
42557 | else | |||
42558 | AndN = DAG.getNode(X86ISD::ANDNP, DL, CondVT, Cond, CastRHS); | |||
42559 | return DAG.getBitcast(VT, AndN); | |||
42560 | } | |||
42561 | ||||
42562 | return SDValue(); | |||
42563 | } | |||
42564 | ||||
42565 | /// If both arms of a vector select are concatenated vectors, split the select, | |||
42566 | /// and concatenate the result to eliminate a wide (256-bit) vector instruction: | |||
42567 | /// vselect Cond, (concat T0, T1), (concat F0, F1) --> | |||
42568 | /// concat (vselect (split Cond), T0, F0), (vselect (split Cond), T1, F1) | |||
42569 | static SDValue narrowVectorSelect(SDNode *N, SelectionDAG &DAG, | |||
42570 | const X86Subtarget &Subtarget) { | |||
42571 | unsigned Opcode = N->getOpcode(); | |||
42572 | if (Opcode != X86ISD::BLENDV && Opcode != ISD::VSELECT) | |||
42573 | return SDValue(); | |||
42574 | ||||
42575 | // TODO: Split 512-bit vectors too? | |||
42576 | EVT VT = N->getValueType(0); | |||
42577 | if (!VT.is256BitVector()) | |||
42578 | return SDValue(); | |||
42579 | ||||
42580 | // TODO: Split as long as any 2 of the 3 operands are concatenated? | |||
42581 | SDValue Cond = N->getOperand(0); | |||
42582 | SDValue TVal = N->getOperand(1); | |||
42583 | SDValue FVal = N->getOperand(2); | |||
42584 | SmallVector<SDValue, 4> CatOpsT, CatOpsF; | |||
42585 | if (!TVal.hasOneUse() || !FVal.hasOneUse() || | |||
42586 | !collectConcatOps(TVal.getNode(), CatOpsT) || | |||
42587 | !collectConcatOps(FVal.getNode(), CatOpsF)) | |||
42588 | return SDValue(); | |||
42589 | ||||
42590 | auto makeBlend = [Opcode](SelectionDAG &DAG, const SDLoc &DL, | |||
42591 | ArrayRef<SDValue> Ops) { | |||
42592 | return DAG.getNode(Opcode, DL, Ops[1].getValueType(), Ops); | |||
42593 | }; | |||
42594 | return SplitOpsAndApply(DAG, Subtarget, SDLoc(N), VT, { Cond, TVal, FVal }, | |||
42595 | makeBlend, /*CheckBWI*/ false); | |||
42596 | } | |||
42597 | ||||
42598 | static SDValue combineSelectOfTwoConstants(SDNode *N, SelectionDAG &DAG) { | |||
42599 | SDValue Cond = N->getOperand(0); | |||
42600 | SDValue LHS = N->getOperand(1); | |||
42601 | SDValue RHS = N->getOperand(2); | |||
42602 | SDLoc DL(N); | |||
42603 | ||||
42604 | auto *TrueC = dyn_cast<ConstantSDNode>(LHS); | |||
42605 | auto *FalseC = dyn_cast<ConstantSDNode>(RHS); | |||
42606 | if (!TrueC || !FalseC) | |||
42607 | return SDValue(); | |||
42608 | ||||
42609 | // Don't do this for crazy integer types. | |||
42610 | EVT VT = N->getValueType(0); | |||
42611 | if (!DAG.getTargetLoweringInfo().isTypeLegal(VT)) | |||
42612 | return SDValue(); | |||
42613 | ||||
42614 | // We're going to use the condition bit in math or logic ops. We could allow | |||
42615 | // this with a wider condition value (post-legalization it becomes an i8), | |||
42616 | // but if nothing is creating selects that late, it doesn't matter. | |||
42617 | if (Cond.getValueType() != MVT::i1) | |||
42618 | return SDValue(); | |||
42619 | ||||
42620 | // A power-of-2 multiply is just a shift. LEA also cheaply handles multiply by | |||
42621 | // 3, 5, or 9 with i32/i64, so those get transformed too. | |||
42622 | // TODO: For constants that overflow or do not differ by power-of-2 or small | |||
42623 | // multiplier, convert to 'and' + 'add'. | |||
42624 | const APInt &TrueVal = TrueC->getAPIntValue(); | |||
42625 | const APInt &FalseVal = FalseC->getAPIntValue(); | |||
42626 | bool OV; | |||
42627 | APInt Diff = TrueVal.ssub_ov(FalseVal, OV); | |||
42628 | if (OV) | |||
42629 | return SDValue(); | |||
42630 | ||||
42631 | APInt AbsDiff = Diff.abs(); | |||
42632 | if (AbsDiff.isPowerOf2() || | |||
42633 | ((VT == MVT::i32 || VT == MVT::i64) && | |||
42634 | (AbsDiff == 3 || AbsDiff == 5 || AbsDiff == 9))) { | |||
42635 | ||||
42636 | // We need a positive multiplier constant for shift/LEA codegen. The 'not' | |||
42637 | // of the condition can usually be folded into a compare predicate, but even | |||
42638 | // without that, the sequence should be cheaper than a CMOV alternative. | |||
42639 | if (TrueVal.slt(FalseVal)) { | |||
42640 | Cond = DAG.getNOT(DL, Cond, MVT::i1); | |||
42641 | std::swap(TrueC, FalseC); | |||
42642 | } | |||
42643 | ||||
42644 | // select Cond, TC, FC --> (zext(Cond) * (TC - FC)) + FC | |||
42645 | SDValue R = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, Cond); | |||
42646 | ||||
42647 | // Multiply condition by the difference if non-one. | |||
42648 | if (!AbsDiff.isOne()) | |||
42649 | R = DAG.getNode(ISD::MUL, DL, VT, R, DAG.getConstant(AbsDiff, DL, VT)); | |||
42650 | ||||
42651 | // Add the base if non-zero. | |||
42652 | if (!FalseC->isZero()) | |||
42653 | R = DAG.getNode(ISD::ADD, DL, VT, R, SDValue(FalseC, 0)); | |||
42654 | ||||
42655 | return R; | |||
42656 | } | |||
42657 | ||||
42658 | return SDValue(); | |||
42659 | } | |||
42660 | ||||
42661 | /// If this is a *dynamic* select (non-constant condition) and we can match | |||
42662 | /// this node with one of the variable blend instructions, restructure the | |||
42663 | /// condition so that blends can use the high (sign) bit of each element. | |||
42664 | /// This function will also call SimplifyDemandedBits on already created | |||
42665 | /// BLENDV to perform additional simplifications. | |||
42666 | static SDValue combineVSelectToBLENDV(SDNode *N, SelectionDAG &DAG, | |||
42667 | TargetLowering::DAGCombinerInfo &DCI, | |||
42668 | const X86Subtarget &Subtarget) { | |||
42669 | SDValue Cond = N->getOperand(0); | |||
42670 | if ((N->getOpcode() != ISD::VSELECT && | |||
42671 | N->getOpcode() != X86ISD::BLENDV) || | |||
42672 | ISD::isBuildVectorOfConstantSDNodes(Cond.getNode())) | |||
42673 | return SDValue(); | |||
42674 | ||||
42675 | // Don't optimize before the condition has been transformed to a legal type | |||
42676 | // and don't ever optimize vector selects that map to AVX512 mask-registers. | |||
42677 | unsigned BitWidth = Cond.getScalarValueSizeInBits(); | |||
42678 | if (BitWidth < 8 || BitWidth > 64) | |||
42679 | return SDValue(); | |||
42680 | ||||
42681 | // We can only handle the cases where VSELECT is directly legal on the | |||
42682 | // subtarget. We custom lower VSELECT nodes with constant conditions and | |||
42683 | // this makes it hard to see whether a dynamic VSELECT will correctly | |||
42684 | // lower, so we both check the operation's status and explicitly handle the | |||
42685 | // cases where a *dynamic* blend will fail even though a constant-condition | |||
42686 | // blend could be custom lowered. | |||
42687 | // FIXME: We should find a better way to handle this class of problems. | |||
42688 | // Potentially, we should combine constant-condition vselect nodes | |||
42689 | // pre-legalization into shuffles and not mark as many types as custom | |||
42690 | // lowered. | |||
42691 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
42692 | EVT VT = N->getValueType(0); | |||
42693 | if (!TLI.isOperationLegalOrCustom(ISD::VSELECT, VT)) | |||
42694 | return SDValue(); | |||
42695 | // FIXME: We don't support i16-element blends currently. We could and | |||
42696 | // should support them by making *all* the bits in the condition be set | |||
42697 | // rather than just the high bit and using an i8-element blend. | |||
42698 | if (VT.getVectorElementType() == MVT::i16) | |||
42699 | return SDValue(); | |||
42700 | // Dynamic blending was only available from SSE4.1 onward. | |||
42701 | if (VT.is128BitVector() && !Subtarget.hasSSE41()) | |||
42702 | return SDValue(); | |||
42703 | // Byte blends are only available in AVX2 | |||
42704 | if (VT == MVT::v32i8 && !Subtarget.hasAVX2()) | |||
42705 | return SDValue(); | |||
42706 | // There are no 512-bit blend instructions that use sign bits. | |||
42707 | if (VT.is512BitVector()) | |||
42708 | return SDValue(); | |||
42709 | ||||
42710 | auto OnlyUsedAsSelectCond = [](SDValue Cond) { | |||
42711 | for (SDNode::use_iterator UI = Cond->use_begin(), UE = Cond->use_end(); | |||
42712 | UI != UE; ++UI) | |||
42713 | if ((UI->getOpcode() != ISD::VSELECT && | |||
42714 | UI->getOpcode() != X86ISD::BLENDV) || | |||
42715 | UI.getOperandNo() != 0) | |||
42716 | return false; | |||
42717 | ||||
42718 | return true; | |||
42719 | }; | |||
42720 | ||||
42721 | APInt DemandedBits(APInt::getSignMask(BitWidth)); | |||
42722 | ||||
42723 | if (OnlyUsedAsSelectCond(Cond)) { | |||
42724 | KnownBits Known; | |||
42725 | TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(), | |||
42726 | !DCI.isBeforeLegalizeOps()); | |||
42727 | if (!TLI.SimplifyDemandedBits(Cond, DemandedBits, Known, TLO, 0, true)) | |||
42728 | return SDValue(); | |||
42729 | ||||
42730 | // If we changed the computation somewhere in the DAG, this change will | |||
42731 | // affect all users of Cond. Update all the nodes so that we do not use | |||
42732 | // the generic VSELECT anymore. Otherwise, we may perform wrong | |||
42733 | // optimizations as we messed with the actual expectation for the vector | |||
42734 | // boolean values. | |||
42735 | for (SDNode *U : Cond->uses()) { | |||
42736 | if (U->getOpcode() == X86ISD::BLENDV) | |||
42737 | continue; | |||
42738 | ||||
42739 | SDValue SB = DAG.getNode(X86ISD::BLENDV, SDLoc(U), U->getValueType(0), | |||
42740 | Cond, U->getOperand(1), U->getOperand(2)); | |||
42741 | DAG.ReplaceAllUsesOfValueWith(SDValue(U, 0), SB); | |||
42742 | DCI.AddToWorklist(U); | |||
42743 | } | |||
42744 | DCI.CommitTargetLoweringOpt(TLO); | |||
42745 | return SDValue(N, 0); | |||
42746 | } | |||
42747 | ||||
42748 | // Otherwise we can still at least try to simplify multiple use bits. | |||
42749 | if (SDValue V = TLI.SimplifyMultipleUseDemandedBits(Cond, DemandedBits, DAG)) | |||
42750 | return DAG.getNode(X86ISD::BLENDV, SDLoc(N), N->getValueType(0), V, | |||
42751 | N->getOperand(1), N->getOperand(2)); | |||
42752 | ||||
42753 | return SDValue(); | |||
42754 | } | |||
42755 | ||||
42756 | // Try to match: | |||
42757 | // (or (and (M, (sub 0, X)), (pandn M, X))) | |||
42758 | // which is a special case of: | |||
42759 | // (select M, (sub 0, X), X) | |||
42760 | // Per: | |||
42761 | // http://graphics.stanford.edu/~seander/bithacks.html#ConditionalNegate | |||
42762 | // We know that, if fNegate is 0 or 1: | |||
42763 | // (fNegate ? -v : v) == ((v ^ -fNegate) + fNegate) | |||
42764 | // | |||
42765 | // Here, we have a mask, M (all 1s or 0), and, similarly, we know that: | |||
42766 | // ((M & 1) ? -X : X) == ((X ^ -(M & 1)) + (M & 1)) | |||
42767 | // ( M ? -X : X) == ((X ^ M ) + (M & 1)) | |||
42768 | // This lets us transform our vselect to: | |||
42769 | // (add (xor X, M), (and M, 1)) | |||
42770 | // And further to: | |||
42771 | // (sub (xor X, M), M) | |||
42772 | static SDValue combineLogicBlendIntoConditionalNegate( | |||
42773 | EVT VT, SDValue Mask, SDValue X, SDValue Y, const SDLoc &DL, | |||
42774 | SelectionDAG &DAG, const X86Subtarget &Subtarget) { | |||
42775 | EVT MaskVT = Mask.getValueType(); | |||
42776 | assert(MaskVT.isInteger() &&(static_cast <bool> (MaskVT.isInteger() && DAG. ComputeNumSignBits(Mask) == MaskVT.getScalarSizeInBits() && "Mask must be zero/all-bits") ? void (0) : __assert_fail ("MaskVT.isInteger() && DAG.ComputeNumSignBits(Mask) == MaskVT.getScalarSizeInBits() && \"Mask must be zero/all-bits\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 42778, __extension__ __PRETTY_FUNCTION__)) | |||
42777 | DAG.ComputeNumSignBits(Mask) == MaskVT.getScalarSizeInBits() &&(static_cast <bool> (MaskVT.isInteger() && DAG. ComputeNumSignBits(Mask) == MaskVT.getScalarSizeInBits() && "Mask must be zero/all-bits") ? void (0) : __assert_fail ("MaskVT.isInteger() && DAG.ComputeNumSignBits(Mask) == MaskVT.getScalarSizeInBits() && \"Mask must be zero/all-bits\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 42778, __extension__ __PRETTY_FUNCTION__)) | |||
42778 | "Mask must be zero/all-bits")(static_cast <bool> (MaskVT.isInteger() && DAG. ComputeNumSignBits(Mask) == MaskVT.getScalarSizeInBits() && "Mask must be zero/all-bits") ? void (0) : __assert_fail ("MaskVT.isInteger() && DAG.ComputeNumSignBits(Mask) == MaskVT.getScalarSizeInBits() && \"Mask must be zero/all-bits\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 42778, __extension__ __PRETTY_FUNCTION__)); | |||
42779 | ||||
42780 | if (X.getValueType() != MaskVT || Y.getValueType() != MaskVT) | |||
42781 | return SDValue(); | |||
42782 | if (!DAG.getTargetLoweringInfo().isOperationLegal(ISD::SUB, MaskVT)) | |||
42783 | return SDValue(); | |||
42784 | ||||
42785 | auto IsNegV = [](SDNode *N, SDValue V) { | |||
42786 | return N->getOpcode() == ISD::SUB && N->getOperand(1) == V && | |||
42787 | ISD::isBuildVectorAllZeros(N->getOperand(0).getNode()); | |||
42788 | }; | |||
42789 | ||||
42790 | SDValue V; | |||
42791 | if (IsNegV(Y.getNode(), X)) | |||
42792 | V = X; | |||
42793 | else if (IsNegV(X.getNode(), Y)) | |||
42794 | V = Y; | |||
42795 | else | |||
42796 | return SDValue(); | |||
42797 | ||||
42798 | SDValue SubOp1 = DAG.getNode(ISD::XOR, DL, MaskVT, V, Mask); | |||
42799 | SDValue SubOp2 = Mask; | |||
42800 | ||||
42801 | // If the negate was on the false side of the select, then | |||
42802 | // the operands of the SUB need to be swapped. PR 27251. | |||
42803 | // This is because the pattern being matched above is | |||
42804 | // (vselect M, (sub (0, X), X) -> (sub (xor X, M), M) | |||
42805 | // but if the pattern matched was | |||
42806 | // (vselect M, X, (sub (0, X))), that is really negation of the pattern | |||
42807 | // above, -(vselect M, (sub 0, X), X), and therefore the replacement | |||
42808 | // pattern also needs to be a negation of the replacement pattern above. | |||
42809 | // And -(sub X, Y) is just sub (Y, X), so swapping the operands of the | |||
42810 | // sub accomplishes the negation of the replacement pattern. | |||
42811 | if (V == Y) | |||
42812 | std::swap(SubOp1, SubOp2); | |||
42813 | ||||
42814 | SDValue Res = DAG.getNode(ISD::SUB, DL, MaskVT, SubOp1, SubOp2); | |||
42815 | return DAG.getBitcast(VT, Res); | |||
42816 | } | |||
42817 | ||||
42818 | /// Do target-specific dag combines on SELECT and VSELECT nodes. | |||
42819 | static SDValue combineSelect(SDNode *N, SelectionDAG &DAG, | |||
42820 | TargetLowering::DAGCombinerInfo &DCI, | |||
42821 | const X86Subtarget &Subtarget) { | |||
42822 | SDLoc DL(N); | |||
42823 | SDValue Cond = N->getOperand(0); | |||
42824 | SDValue LHS = N->getOperand(1); | |||
42825 | SDValue RHS = N->getOperand(2); | |||
42826 | ||||
42827 | // Try simplification again because we use this function to optimize | |||
42828 | // BLENDV nodes that are not handled by the generic combiner. | |||
42829 | if (SDValue V = DAG.simplifySelect(Cond, LHS, RHS)) | |||
42830 | return V; | |||
42831 | ||||
42832 | EVT VT = LHS.getValueType(); | |||
42833 | EVT CondVT = Cond.getValueType(); | |||
42834 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
42835 | bool CondConstantVector = ISD::isBuildVectorOfConstantSDNodes(Cond.getNode()); | |||
42836 | ||||
42837 | // Attempt to combine (select M, (sub 0, X), X) -> (sub (xor X, M), M). | |||
42838 | // Limit this to cases of non-constant masks that createShuffleMaskFromVSELECT | |||
42839 | // can't catch, plus vXi8 cases where we'd likely end up with BLENDV. | |||
42840 | if (CondVT.isVector() && CondVT.isInteger() && | |||
42841 | CondVT.getScalarSizeInBits() == VT.getScalarSizeInBits() && | |||
42842 | (!CondConstantVector || CondVT.getScalarType() == MVT::i8) && | |||
42843 | DAG.ComputeNumSignBits(Cond) == CondVT.getScalarSizeInBits()) | |||
42844 | if (SDValue V = combineLogicBlendIntoConditionalNegate(VT, Cond, RHS, LHS, | |||
42845 | DL, DAG, Subtarget)) | |||
42846 | return V; | |||
42847 | ||||
42848 | // Convert vselects with constant condition into shuffles. | |||
42849 | if (CondConstantVector && DCI.isBeforeLegalizeOps()) { | |||
42850 | SmallVector<int, 64> Mask; | |||
42851 | if (createShuffleMaskFromVSELECT(Mask, Cond)) | |||
42852 | return DAG.getVectorShuffle(VT, DL, LHS, RHS, Mask); | |||
42853 | } | |||
42854 | ||||
42855 | // fold vselect(cond, pshufb(x), pshufb(y)) -> or (pshufb(x), pshufb(y)) | |||
42856 | // by forcing the unselected elements to zero. | |||
42857 | // TODO: Can we handle more shuffles with this? | |||
42858 | if (N->getOpcode() == ISD::VSELECT && CondVT.isVector() && | |||
42859 | LHS.getOpcode() == X86ISD::PSHUFB && RHS.getOpcode() == X86ISD::PSHUFB && | |||
42860 | LHS.hasOneUse() && RHS.hasOneUse()) { | |||
42861 | MVT SimpleVT = VT.getSimpleVT(); | |||
42862 | SmallVector<SDValue, 1> LHSOps, RHSOps; | |||
42863 | SmallVector<int, 64> LHSMask, RHSMask, CondMask; | |||
42864 | if (createShuffleMaskFromVSELECT(CondMask, Cond) && | |||
42865 | getTargetShuffleMask(LHS.getNode(), SimpleVT, true, LHSOps, LHSMask) && | |||
42866 | getTargetShuffleMask(RHS.getNode(), SimpleVT, true, RHSOps, RHSMask)) { | |||
42867 | int NumElts = VT.getVectorNumElements(); | |||
42868 | for (int i = 0; i != NumElts; ++i) { | |||
42869 | // getConstVector sets negative shuffle mask values as undef, so ensure | |||
42870 | // we hardcode SM_SentinelZero values to zero (0x80). | |||
42871 | if (CondMask[i] < NumElts) { | |||
42872 | LHSMask[i] = (LHSMask[i] == SM_SentinelZero) ? 0x80 : LHSMask[i]; | |||
42873 | RHSMask[i] = 0x80; | |||
42874 | } else { | |||
42875 | LHSMask[i] = 0x80; | |||
42876 | RHSMask[i] = (RHSMask[i] == SM_SentinelZero) ? 0x80 : RHSMask[i]; | |||
42877 | } | |||
42878 | } | |||
42879 | LHS = DAG.getNode(X86ISD::PSHUFB, DL, VT, LHS.getOperand(0), | |||
42880 | getConstVector(LHSMask, SimpleVT, DAG, DL, true)); | |||
42881 | RHS = DAG.getNode(X86ISD::PSHUFB, DL, VT, RHS.getOperand(0), | |||
42882 | getConstVector(RHSMask, SimpleVT, DAG, DL, true)); | |||
42883 | return DAG.getNode(ISD::OR, DL, VT, LHS, RHS); | |||
42884 | } | |||
42885 | } | |||
42886 | ||||
42887 | // If we have SSE[12] support, try to form min/max nodes. SSE min/max | |||
42888 | // instructions match the semantics of the common C idiom x<y?x:y but not | |||
42889 | // x<=y?x:y, because of how they handle negative zero (which can be | |||
42890 | // ignored in unsafe-math mode). | |||
42891 | // We also try to create v2f32 min/max nodes, which we later widen to v4f32. | |||
42892 | if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() && | |||
42893 | VT != MVT::f80 && VT != MVT::f128 && | |||
42894 | (TLI.isTypeLegal(VT) || VT == MVT::v2f32) && | |||
42895 | (Subtarget.hasSSE2() || | |||
42896 | (Subtarget.hasSSE1() && VT.getScalarType() == MVT::f32))) { | |||
42897 | ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get(); | |||
42898 | ||||
42899 | unsigned Opcode = 0; | |||
42900 | // Check for x CC y ? x : y. | |||
42901 | if (DAG.isEqualTo(LHS, Cond.getOperand(0)) && | |||
42902 | DAG.isEqualTo(RHS, Cond.getOperand(1))) { | |||
42903 | switch (CC) { | |||
42904 | default: break; | |||
42905 | case ISD::SETULT: | |||
42906 | // Converting this to a min would handle NaNs incorrectly, and swapping | |||
42907 | // the operands would cause it to handle comparisons between positive | |||
42908 | // and negative zero incorrectly. | |||
42909 | if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) { | |||
42910 | if (!DAG.getTarget().Options.NoSignedZerosFPMath && | |||
42911 | !(DAG.isKnownNeverZeroFloat(LHS) || | |||
42912 | DAG.isKnownNeverZeroFloat(RHS))) | |||
42913 | break; | |||
42914 | std::swap(LHS, RHS); | |||
42915 | } | |||
42916 | Opcode = X86ISD::FMIN; | |||
42917 | break; | |||
42918 | case ISD::SETOLE: | |||
42919 | // Converting this to a min would handle comparisons between positive | |||
42920 | // and negative zero incorrectly. | |||
42921 | if (!DAG.getTarget().Options.NoSignedZerosFPMath && | |||
42922 | !DAG.isKnownNeverZeroFloat(LHS) && !DAG.isKnownNeverZeroFloat(RHS)) | |||
42923 | break; | |||
42924 | Opcode = X86ISD::FMIN; | |||
42925 | break; | |||
42926 | case ISD::SETULE: | |||
42927 | // Converting this to a min would handle both negative zeros and NaNs | |||
42928 | // incorrectly, but we can swap the operands to fix both. | |||
42929 | std::swap(LHS, RHS); | |||
42930 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
42931 | case ISD::SETOLT: | |||
42932 | case ISD::SETLT: | |||
42933 | case ISD::SETLE: | |||
42934 | Opcode = X86ISD::FMIN; | |||
42935 | break; | |||
42936 | ||||
42937 | case ISD::SETOGE: | |||
42938 | // Converting this to a max would handle comparisons between positive | |||
42939 | // and negative zero incorrectly. | |||
42940 | if (!DAG.getTarget().Options.NoSignedZerosFPMath && | |||
42941 | !DAG.isKnownNeverZeroFloat(LHS) && !DAG.isKnownNeverZeroFloat(RHS)) | |||
42942 | break; | |||
42943 | Opcode = X86ISD::FMAX; | |||
42944 | break; | |||
42945 | case ISD::SETUGT: | |||
42946 | // Converting this to a max would handle NaNs incorrectly, and swapping | |||
42947 | // the operands would cause it to handle comparisons between positive | |||
42948 | // and negative zero incorrectly. | |||
42949 | if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) { | |||
42950 | if (!DAG.getTarget().Options.NoSignedZerosFPMath && | |||
42951 | !(DAG.isKnownNeverZeroFloat(LHS) || | |||
42952 | DAG.isKnownNeverZeroFloat(RHS))) | |||
42953 | break; | |||
42954 | std::swap(LHS, RHS); | |||
42955 | } | |||
42956 | Opcode = X86ISD::FMAX; | |||
42957 | break; | |||
42958 | case ISD::SETUGE: | |||
42959 | // Converting this to a max would handle both negative zeros and NaNs | |||
42960 | // incorrectly, but we can swap the operands to fix both. | |||
42961 | std::swap(LHS, RHS); | |||
42962 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
42963 | case ISD::SETOGT: | |||
42964 | case ISD::SETGT: | |||
42965 | case ISD::SETGE: | |||
42966 | Opcode = X86ISD::FMAX; | |||
42967 | break; | |||
42968 | } | |||
42969 | // Check for x CC y ? y : x -- a min/max with reversed arms. | |||
42970 | } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) && | |||
42971 | DAG.isEqualTo(RHS, Cond.getOperand(0))) { | |||
42972 | switch (CC) { | |||
42973 | default: break; | |||
42974 | case ISD::SETOGE: | |||
42975 | // Converting this to a min would handle comparisons between positive | |||
42976 | // and negative zero incorrectly, and swapping the operands would | |||
42977 | // cause it to handle NaNs incorrectly. | |||
42978 | if (!DAG.getTarget().Options.NoSignedZerosFPMath && | |||
42979 | !(DAG.isKnownNeverZeroFloat(LHS) || | |||
42980 | DAG.isKnownNeverZeroFloat(RHS))) { | |||
42981 | if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) | |||
42982 | break; | |||
42983 | std::swap(LHS, RHS); | |||
42984 | } | |||
42985 | Opcode = X86ISD::FMIN; | |||
42986 | break; | |||
42987 | case ISD::SETUGT: | |||
42988 | // Converting this to a min would handle NaNs incorrectly. | |||
42989 | if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) | |||
42990 | break; | |||
42991 | Opcode = X86ISD::FMIN; | |||
42992 | break; | |||
42993 | case ISD::SETUGE: | |||
42994 | // Converting this to a min would handle both negative zeros and NaNs | |||
42995 | // incorrectly, but we can swap the operands to fix both. | |||
42996 | std::swap(LHS, RHS); | |||
42997 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
42998 | case ISD::SETOGT: | |||
42999 | case ISD::SETGT: | |||
43000 | case ISD::SETGE: | |||
43001 | Opcode = X86ISD::FMIN; | |||
43002 | break; | |||
43003 | ||||
43004 | case ISD::SETULT: | |||
43005 | // Converting this to a max would handle NaNs incorrectly. | |||
43006 | if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) | |||
43007 | break; | |||
43008 | Opcode = X86ISD::FMAX; | |||
43009 | break; | |||
43010 | case ISD::SETOLE: | |||
43011 | // Converting this to a max would handle comparisons between positive | |||
43012 | // and negative zero incorrectly, and swapping the operands would | |||
43013 | // cause it to handle NaNs incorrectly. | |||
43014 | if (!DAG.getTarget().Options.NoSignedZerosFPMath && | |||
43015 | !DAG.isKnownNeverZeroFloat(LHS) && | |||
43016 | !DAG.isKnownNeverZeroFloat(RHS)) { | |||
43017 | if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) | |||
43018 | break; | |||
43019 | std::swap(LHS, RHS); | |||
43020 | } | |||
43021 | Opcode = X86ISD::FMAX; | |||
43022 | break; | |||
43023 | case ISD::SETULE: | |||
43024 | // Converting this to a max would handle both negative zeros and NaNs | |||
43025 | // incorrectly, but we can swap the operands to fix both. | |||
43026 | std::swap(LHS, RHS); | |||
43027 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
43028 | case ISD::SETOLT: | |||
43029 | case ISD::SETLT: | |||
43030 | case ISD::SETLE: | |||
43031 | Opcode = X86ISD::FMAX; | |||
43032 | break; | |||
43033 | } | |||
43034 | } | |||
43035 | ||||
43036 | if (Opcode) | |||
43037 | return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS); | |||
43038 | } | |||
43039 | ||||
43040 | // Some mask scalar intrinsics rely on checking if only one bit is set | |||
43041 | // and implement it in C code like this: | |||
43042 | // A[0] = (U & 1) ? A[0] : W[0]; | |||
43043 | // This creates some redundant instructions that break pattern matching. | |||
43044 | // fold (select (setcc (and (X, 1), 0, seteq), Y, Z)) -> select(and(X, 1),Z,Y) | |||
43045 | if (Subtarget.hasAVX512() && N->getOpcode() == ISD::SELECT && | |||
43046 | Cond.getOpcode() == ISD::SETCC && (VT == MVT::f32 || VT == MVT::f64)) { | |||
43047 | ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get(); | |||
43048 | SDValue AndNode = Cond.getOperand(0); | |||
43049 | if (AndNode.getOpcode() == ISD::AND && CC == ISD::SETEQ && | |||
43050 | isNullConstant(Cond.getOperand(1)) && | |||
43051 | isOneConstant(AndNode.getOperand(1))) { | |||
43052 | // LHS and RHS swapped due to | |||
43053 | // setcc outputting 1 when AND resulted in 0 and vice versa. | |||
43054 | AndNode = DAG.getZExtOrTrunc(AndNode, DL, MVT::i8); | |||
43055 | return DAG.getNode(ISD::SELECT, DL, VT, AndNode, RHS, LHS); | |||
43056 | } | |||
43057 | } | |||
43058 | ||||
43059 | // v16i8 (select v16i1, v16i8, v16i8) does not have a proper | |||
43060 | // lowering on KNL. In this case we convert it to | |||
43061 | // v16i8 (select v16i8, v16i8, v16i8) and use AVX instruction. | |||
43062 | // The same situation all vectors of i8 and i16 without BWI. | |||
43063 | // Make sure we extend these even before type legalization gets a chance to | |||
43064 | // split wide vectors. | |||
43065 | // Since SKX these selects have a proper lowering. | |||
43066 | if (Subtarget.hasAVX512() && !Subtarget.hasBWI() && CondVT.isVector() && | |||
43067 | CondVT.getVectorElementType() == MVT::i1 && | |||
43068 | (VT.getVectorElementType() == MVT::i8 || | |||
43069 | VT.getVectorElementType() == MVT::i16)) { | |||
43070 | Cond = DAG.getNode(ISD::SIGN_EXTEND, DL, VT, Cond); | |||
43071 | return DAG.getNode(N->getOpcode(), DL, VT, Cond, LHS, RHS); | |||
43072 | } | |||
43073 | ||||
43074 | // AVX512 - Extend select with zero to merge with target shuffle. | |||
43075 | // select(mask, extract_subvector(shuffle(x)), zero) --> | |||
43076 | // extract_subvector(select(insert_subvector(mask), shuffle(x), zero)) | |||
43077 | // TODO - support non target shuffles as well. | |||
43078 | if (Subtarget.hasAVX512() && CondVT.isVector() && | |||
43079 | CondVT.getVectorElementType() == MVT::i1) { | |||
43080 | auto SelectableOp = [&TLI](SDValue Op) { | |||
43081 | return Op.getOpcode() == ISD::EXTRACT_SUBVECTOR && | |||
43082 | isTargetShuffle(Op.getOperand(0).getOpcode()) && | |||
43083 | isNullConstant(Op.getOperand(1)) && | |||
43084 | TLI.isTypeLegal(Op.getOperand(0).getValueType()) && | |||
43085 | Op.hasOneUse() && Op.getOperand(0).hasOneUse(); | |||
43086 | }; | |||
43087 | ||||
43088 | bool SelectableLHS = SelectableOp(LHS); | |||
43089 | bool SelectableRHS = SelectableOp(RHS); | |||
43090 | bool ZeroLHS = ISD::isBuildVectorAllZeros(LHS.getNode()); | |||
43091 | bool ZeroRHS = ISD::isBuildVectorAllZeros(RHS.getNode()); | |||
43092 | ||||
43093 | if ((SelectableLHS && ZeroRHS) || (SelectableRHS && ZeroLHS)) { | |||
43094 | EVT SrcVT = SelectableLHS ? LHS.getOperand(0).getValueType() | |||
43095 | : RHS.getOperand(0).getValueType(); | |||
43096 | EVT SrcCondVT = SrcVT.changeVectorElementType(MVT::i1); | |||
43097 | LHS = insertSubVector(DAG.getUNDEF(SrcVT), LHS, 0, DAG, DL, | |||
43098 | VT.getSizeInBits()); | |||
43099 | RHS = insertSubVector(DAG.getUNDEF(SrcVT), RHS, 0, DAG, DL, | |||
43100 | VT.getSizeInBits()); | |||
43101 | Cond = DAG.getNode(ISD::INSERT_SUBVECTOR, DL, SrcCondVT, | |||
43102 | DAG.getUNDEF(SrcCondVT), Cond, | |||
43103 | DAG.getIntPtrConstant(0, DL)); | |||
43104 | SDValue Res = DAG.getSelect(DL, SrcVT, Cond, LHS, RHS); | |||
43105 | return extractSubVector(Res, 0, DAG, DL, VT.getSizeInBits()); | |||
43106 | } | |||
43107 | } | |||
43108 | ||||
43109 | if (SDValue V = combineSelectOfTwoConstants(N, DAG)) | |||
43110 | return V; | |||
43111 | ||||
43112 | if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC && | |||
43113 | Cond.hasOneUse()) { | |||
43114 | EVT CondVT = Cond.getValueType(); | |||
43115 | SDValue Cond0 = Cond.getOperand(0); | |||
43116 | SDValue Cond1 = Cond.getOperand(1); | |||
43117 | ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get(); | |||
43118 | ||||
43119 | // Canonicalize min/max: | |||
43120 | // (x > 0) ? x : 0 -> (x >= 0) ? x : 0 | |||
43121 | // (x < -1) ? x : -1 -> (x <= -1) ? x : -1 | |||
43122 | // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates | |||
43123 | // the need for an extra compare against zero. e.g. | |||
43124 | // (a - b) > 0 : (a - b) ? 0 -> (a - b) >= 0 : (a - b) ? 0 | |||
43125 | // subl %esi, %edi | |||
43126 | // testl %edi, %edi | |||
43127 | // movl $0, %eax | |||
43128 | // cmovgl %edi, %eax | |||
43129 | // => | |||
43130 | // xorl %eax, %eax | |||
43131 | // subl %esi, $edi | |||
43132 | // cmovsl %eax, %edi | |||
43133 | // | |||
43134 | // We can also canonicalize | |||
43135 | // (x s> 1) ? x : 1 -> (x s>= 1) ? x : 1 -> (x s> 0) ? x : 1 | |||
43136 | // (x u> 1) ? x : 1 -> (x u>= 1) ? x : 1 -> (x != 0) ? x : 1 | |||
43137 | // This allows the use of a test instruction for the compare. | |||
43138 | if (LHS == Cond0 && RHS == Cond1) { | |||
43139 | if ((CC == ISD::SETGT && (isNullConstant(RHS) || isOneConstant(RHS))) || | |||
43140 | (CC == ISD::SETLT && isAllOnesConstant(RHS))) { | |||
43141 | ISD::CondCode NewCC = CC == ISD::SETGT ? ISD::SETGE : ISD::SETLE; | |||
43142 | Cond = DAG.getSetCC(SDLoc(Cond), CondVT, Cond0, Cond1, NewCC); | |||
43143 | return DAG.getSelect(DL, VT, Cond, LHS, RHS); | |||
43144 | } | |||
43145 | if (CC == ISD::SETUGT && isOneConstant(RHS)) { | |||
43146 | ISD::CondCode NewCC = ISD::SETUGE; | |||
43147 | Cond = DAG.getSetCC(SDLoc(Cond), CondVT, Cond0, Cond1, NewCC); | |||
43148 | return DAG.getSelect(DL, VT, Cond, LHS, RHS); | |||
43149 | } | |||
43150 | } | |||
43151 | ||||
43152 | // Similar to DAGCombine's select(or(CC0,CC1),X,Y) fold but for legal types. | |||
43153 | // fold eq + gt/lt nested selects into ge/le selects | |||
43154 | // select (cmpeq Cond0, Cond1), LHS, (select (cmpugt Cond0, Cond1), LHS, Y) | |||
43155 | // --> (select (cmpuge Cond0, Cond1), LHS, Y) | |||
43156 | // select (cmpslt Cond0, Cond1), LHS, (select (cmpeq Cond0, Cond1), LHS, Y) | |||
43157 | // --> (select (cmpsle Cond0, Cond1), LHS, Y) | |||
43158 | // .. etc .. | |||
43159 | if (RHS.getOpcode() == ISD::SELECT && RHS.getOperand(1) == LHS && | |||
43160 | RHS.getOperand(0).getOpcode() == ISD::SETCC) { | |||
43161 | SDValue InnerSetCC = RHS.getOperand(0); | |||
43162 | ISD::CondCode InnerCC = | |||
43163 | cast<CondCodeSDNode>(InnerSetCC.getOperand(2))->get(); | |||
43164 | if ((CC == ISD::SETEQ || InnerCC == ISD::SETEQ) && | |||
43165 | Cond0 == InnerSetCC.getOperand(0) && | |||
43166 | Cond1 == InnerSetCC.getOperand(1)) { | |||
43167 | ISD::CondCode NewCC; | |||
43168 | switch (CC == ISD::SETEQ ? InnerCC : CC) { | |||
43169 | case ISD::SETGT: NewCC = ISD::SETGE; break; | |||
43170 | case ISD::SETLT: NewCC = ISD::SETLE; break; | |||
43171 | case ISD::SETUGT: NewCC = ISD::SETUGE; break; | |||
43172 | case ISD::SETULT: NewCC = ISD::SETULE; break; | |||
43173 | default: NewCC = ISD::SETCC_INVALID; break; | |||
43174 | } | |||
43175 | if (NewCC != ISD::SETCC_INVALID) { | |||
43176 | Cond = DAG.getSetCC(DL, CondVT, Cond0, Cond1, NewCC); | |||
43177 | return DAG.getSelect(DL, VT, Cond, LHS, RHS.getOperand(2)); | |||
43178 | } | |||
43179 | } | |||
43180 | } | |||
43181 | } | |||
43182 | ||||
43183 | // Check if the first operand is all zeros and Cond type is vXi1. | |||
43184 | // If this an avx512 target we can improve the use of zero masking by | |||
43185 | // swapping the operands and inverting the condition. | |||
43186 | if (N->getOpcode() == ISD::VSELECT && Cond.hasOneUse() && | |||
43187 | Subtarget.hasAVX512() && CondVT.getVectorElementType() == MVT::i1 && | |||
43188 | ISD::isBuildVectorAllZeros(LHS.getNode()) && | |||
43189 | !ISD::isBuildVectorAllZeros(RHS.getNode())) { | |||
43190 | // Invert the cond to not(cond) : xor(op,allones)=not(op) | |||
43191 | SDValue CondNew = DAG.getNOT(DL, Cond, CondVT); | |||
43192 | // Vselect cond, op1, op2 = Vselect not(cond), op2, op1 | |||
43193 | return DAG.getSelect(DL, VT, CondNew, RHS, LHS); | |||
43194 | } | |||
43195 | ||||
43196 | // Early exit check | |||
43197 | if (!TLI.isTypeLegal(VT)) | |||
43198 | return SDValue(); | |||
43199 | ||||
43200 | if (SDValue V = combineVSelectWithAllOnesOrZeros(N, DAG, DCI, Subtarget)) | |||
43201 | return V; | |||
43202 | ||||
43203 | if (SDValue V = combineVSelectToBLENDV(N, DAG, DCI, Subtarget)) | |||
43204 | return V; | |||
43205 | ||||
43206 | if (SDValue V = narrowVectorSelect(N, DAG, Subtarget)) | |||
43207 | return V; | |||
43208 | ||||
43209 | // select(~Cond, X, Y) -> select(Cond, Y, X) | |||
43210 | if (CondVT.getScalarType() != MVT::i1) { | |||
43211 | if (SDValue CondNot = IsNOT(Cond, DAG)) | |||
43212 | return DAG.getNode(N->getOpcode(), DL, VT, | |||
43213 | DAG.getBitcast(CondVT, CondNot), RHS, LHS); | |||
43214 | // pcmpgt(X, -1) -> pcmpgt(0, X) to help select/blendv just use the signbit. | |||
43215 | if (Cond.getOpcode() == X86ISD::PCMPGT && Cond.hasOneUse() && | |||
43216 | ISD::isBuildVectorAllOnes(Cond.getOperand(1).getNode())) { | |||
43217 | Cond = DAG.getNode(X86ISD::PCMPGT, DL, CondVT, | |||
43218 | DAG.getConstant(0, DL, CondVT), Cond.getOperand(0)); | |||
43219 | return DAG.getNode(N->getOpcode(), DL, VT, Cond, RHS, LHS); | |||
43220 | } | |||
43221 | } | |||
43222 | ||||
43223 | // Try to optimize vXi1 selects if both operands are either all constants or | |||
43224 | // bitcasts from scalar integer type. In that case we can convert the operands | |||
43225 | // to integer and use an integer select which will be converted to a CMOV. | |||
43226 | // We need to take a little bit of care to avoid creating an i64 type after | |||
43227 | // type legalization. | |||
43228 | if (N->getOpcode() == ISD::SELECT && VT.isVector() && | |||
43229 | VT.getVectorElementType() == MVT::i1 && | |||
43230 | (DCI.isBeforeLegalize() || (VT != MVT::v64i1 || Subtarget.is64Bit()))) { | |||
43231 | EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), VT.getVectorNumElements()); | |||
43232 | bool LHSIsConst = ISD::isBuildVectorOfConstantSDNodes(LHS.getNode()); | |||
43233 | bool RHSIsConst = ISD::isBuildVectorOfConstantSDNodes(RHS.getNode()); | |||
43234 | ||||
43235 | if ((LHSIsConst || | |||
43236 | (LHS.getOpcode() == ISD::BITCAST && | |||
43237 | LHS.getOperand(0).getValueType() == IntVT)) && | |||
43238 | (RHSIsConst || | |||
43239 | (RHS.getOpcode() == ISD::BITCAST && | |||
43240 | RHS.getOperand(0).getValueType() == IntVT))) { | |||
43241 | if (LHSIsConst) | |||
43242 | LHS = combinevXi1ConstantToInteger(LHS, DAG); | |||
43243 | else | |||
43244 | LHS = LHS.getOperand(0); | |||
43245 | ||||
43246 | if (RHSIsConst) | |||
43247 | RHS = combinevXi1ConstantToInteger(RHS, DAG); | |||
43248 | else | |||
43249 | RHS = RHS.getOperand(0); | |||
43250 | ||||
43251 | SDValue Select = DAG.getSelect(DL, IntVT, Cond, LHS, RHS); | |||
43252 | return DAG.getBitcast(VT, Select); | |||
43253 | } | |||
43254 | } | |||
43255 | ||||
43256 | // If this is "((X & C) == 0) ? Y : Z" and C is a constant mask vector of | |||
43257 | // single bits, then invert the predicate and swap the select operands. | |||
43258 | // This can lower using a vector shift bit-hack rather than mask and compare. | |||
43259 | if (DCI.isBeforeLegalize() && !Subtarget.hasAVX512() && | |||
43260 | N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC && | |||
43261 | Cond.hasOneUse() && CondVT.getVectorElementType() == MVT::i1 && | |||
43262 | Cond.getOperand(0).getOpcode() == ISD::AND && | |||
43263 | isNullOrNullSplat(Cond.getOperand(1)) && | |||
43264 | cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ && | |||
43265 | Cond.getOperand(0).getValueType() == VT) { | |||
43266 | // The 'and' mask must be composed of power-of-2 constants. | |||
43267 | SDValue And = Cond.getOperand(0); | |||
43268 | auto *C = isConstOrConstSplat(And.getOperand(1)); | |||
43269 | if (C && C->getAPIntValue().isPowerOf2()) { | |||
43270 | // vselect (X & C == 0), LHS, RHS --> vselect (X & C != 0), RHS, LHS | |||
43271 | SDValue NotCond = | |||
43272 | DAG.getSetCC(DL, CondVT, And, Cond.getOperand(1), ISD::SETNE); | |||
43273 | return DAG.getSelect(DL, VT, NotCond, RHS, LHS); | |||
43274 | } | |||
43275 | ||||
43276 | // If we have a non-splat but still powers-of-2 mask, AVX1 can use pmulld | |||
43277 | // and AVX2 can use vpsllv{dq}. 8-bit lacks a proper shift or multiply. | |||
43278 | // 16-bit lacks a proper blendv. | |||
43279 | unsigned EltBitWidth = VT.getScalarSizeInBits(); | |||
43280 | bool CanShiftBlend = | |||
43281 | TLI.isTypeLegal(VT) && ((Subtarget.hasAVX() && EltBitWidth == 32) || | |||
43282 | (Subtarget.hasAVX2() && EltBitWidth == 64) || | |||
43283 | (Subtarget.hasXOP())); | |||
43284 | if (CanShiftBlend && | |||
43285 | ISD::matchUnaryPredicate(And.getOperand(1), [](ConstantSDNode *C) { | |||
43286 | return C->getAPIntValue().isPowerOf2(); | |||
43287 | })) { | |||
43288 | // Create a left-shift constant to get the mask bits over to the sign-bit. | |||
43289 | SDValue Mask = And.getOperand(1); | |||
43290 | SmallVector<int, 32> ShlVals; | |||
43291 | for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i) { | |||
43292 | auto *MaskVal = cast<ConstantSDNode>(Mask.getOperand(i)); | |||
43293 | ShlVals.push_back(EltBitWidth - 1 - | |||
43294 | MaskVal->getAPIntValue().exactLogBase2()); | |||
43295 | } | |||
43296 | // vsel ((X & C) == 0), LHS, RHS --> vsel ((shl X, C') < 0), RHS, LHS | |||
43297 | SDValue ShlAmt = getConstVector(ShlVals, VT.getSimpleVT(), DAG, DL); | |||
43298 | SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, And.getOperand(0), ShlAmt); | |||
43299 | SDValue NewCond = | |||
43300 | DAG.getSetCC(DL, CondVT, Shl, Cond.getOperand(1), ISD::SETLT); | |||
43301 | return DAG.getSelect(DL, VT, NewCond, RHS, LHS); | |||
43302 | } | |||
43303 | } | |||
43304 | ||||
43305 | return SDValue(); | |||
43306 | } | |||
43307 | ||||
43308 | /// Combine: | |||
43309 | /// (brcond/cmov/setcc .., (cmp (atomic_load_add x, 1), 0), COND_S) | |||
43310 | /// to: | |||
43311 | /// (brcond/cmov/setcc .., (LADD x, 1), COND_LE) | |||
43312 | /// i.e., reusing the EFLAGS produced by the LOCKed instruction. | |||
43313 | /// Note that this is only legal for some op/cc combinations. | |||
43314 | static SDValue combineSetCCAtomicArith(SDValue Cmp, X86::CondCode &CC, | |||
43315 | SelectionDAG &DAG, | |||
43316 | const X86Subtarget &Subtarget) { | |||
43317 | // This combine only operates on CMP-like nodes. | |||
43318 | if (!(Cmp.getOpcode() == X86ISD::CMP || | |||
43319 | (Cmp.getOpcode() == X86ISD::SUB && !Cmp->hasAnyUseOfValue(0)))) | |||
43320 | return SDValue(); | |||
43321 | ||||
43322 | // Can't replace the cmp if it has more uses than the one we're looking at. | |||
43323 | // FIXME: We would like to be able to handle this, but would need to make sure | |||
43324 | // all uses were updated. | |||
43325 | if (!Cmp.hasOneUse()) | |||
43326 | return SDValue(); | |||
43327 | ||||
43328 | // This only applies to variations of the common case: | |||
43329 | // (icmp slt x, 0) -> (icmp sle (add x, 1), 0) | |||
43330 | // (icmp sge x, 0) -> (icmp sgt (add x, 1), 0) | |||
43331 | // (icmp sle x, 0) -> (icmp slt (sub x, 1), 0) | |||
43332 | // (icmp sgt x, 0) -> (icmp sge (sub x, 1), 0) | |||
43333 | // Using the proper condcodes (see below), overflow is checked for. | |||
43334 | ||||
43335 | // FIXME: We can generalize both constraints: | |||
43336 | // - XOR/OR/AND (if they were made to survive AtomicExpand) | |||
43337 | // - LHS != 1 | |||
43338 | // if the result is compared. | |||
43339 | ||||
43340 | SDValue CmpLHS = Cmp.getOperand(0); | |||
43341 | SDValue CmpRHS = Cmp.getOperand(1); | |||
43342 | EVT CmpVT = CmpLHS.getValueType(); | |||
43343 | ||||
43344 | if (!CmpLHS.hasOneUse()) | |||
43345 | return SDValue(); | |||
43346 | ||||
43347 | unsigned Opc = CmpLHS.getOpcode(); | |||
43348 | if (Opc != ISD::ATOMIC_LOAD_ADD && Opc != ISD::ATOMIC_LOAD_SUB) | |||
43349 | return SDValue(); | |||
43350 | ||||
43351 | SDValue OpRHS = CmpLHS.getOperand(2); | |||
43352 | auto *OpRHSC = dyn_cast<ConstantSDNode>(OpRHS); | |||
43353 | if (!OpRHSC) | |||
43354 | return SDValue(); | |||
43355 | ||||
43356 | APInt Addend = OpRHSC->getAPIntValue(); | |||
43357 | if (Opc == ISD::ATOMIC_LOAD_SUB) | |||
43358 | Addend = -Addend; | |||
43359 | ||||
43360 | auto *CmpRHSC = dyn_cast<ConstantSDNode>(CmpRHS); | |||
43361 | if (!CmpRHSC) | |||
43362 | return SDValue(); | |||
43363 | ||||
43364 | APInt Comparison = CmpRHSC->getAPIntValue(); | |||
43365 | APInt NegAddend = -Addend; | |||
43366 | ||||
43367 | // See if we can adjust the CC to make the comparison match the negated | |||
43368 | // addend. | |||
43369 | if (Comparison != NegAddend) { | |||
43370 | APInt IncComparison = Comparison + 1; | |||
43371 | if (IncComparison == NegAddend) { | |||
43372 | if (CC == X86::COND_A && !Comparison.isMaxValue()) { | |||
43373 | Comparison = IncComparison; | |||
43374 | CC = X86::COND_AE; | |||
43375 | } else if (CC == X86::COND_LE && !Comparison.isMaxSignedValue()) { | |||
43376 | Comparison = IncComparison; | |||
43377 | CC = X86::COND_L; | |||
43378 | } | |||
43379 | } | |||
43380 | APInt DecComparison = Comparison - 1; | |||
43381 | if (DecComparison == NegAddend) { | |||
43382 | if (CC == X86::COND_AE && !Comparison.isMinValue()) { | |||
43383 | Comparison = DecComparison; | |||
43384 | CC = X86::COND_A; | |||
43385 | } else if (CC == X86::COND_L && !Comparison.isMinSignedValue()) { | |||
43386 | Comparison = DecComparison; | |||
43387 | CC = X86::COND_LE; | |||
43388 | } | |||
43389 | } | |||
43390 | } | |||
43391 | ||||
43392 | // If the addend is the negation of the comparison value, then we can do | |||
43393 | // a full comparison by emitting the atomic arithmetic as a locked sub. | |||
43394 | if (Comparison == NegAddend) { | |||
43395 | // The CC is fine, but we need to rewrite the LHS of the comparison as an | |||
43396 | // atomic sub. | |||
43397 | auto *AN = cast<AtomicSDNode>(CmpLHS.getNode()); | |||
43398 | auto AtomicSub = DAG.getAtomic( | |||
43399 | ISD::ATOMIC_LOAD_SUB, SDLoc(CmpLHS), CmpVT, | |||
43400 | /*Chain*/ CmpLHS.getOperand(0), /*LHS*/ CmpLHS.getOperand(1), | |||
43401 | /*RHS*/ DAG.getConstant(NegAddend, SDLoc(CmpRHS), CmpVT), | |||
43402 | AN->getMemOperand()); | |||
43403 | auto LockOp = lowerAtomicArithWithLOCK(AtomicSub, DAG, Subtarget); | |||
43404 | DAG.ReplaceAllUsesOfValueWith(CmpLHS.getValue(0), DAG.getUNDEF(CmpVT)); | |||
43405 | DAG.ReplaceAllUsesOfValueWith(CmpLHS.getValue(1), LockOp.getValue(1)); | |||
43406 | return LockOp; | |||
43407 | } | |||
43408 | ||||
43409 | // We can handle comparisons with zero in a number of cases by manipulating | |||
43410 | // the CC used. | |||
43411 | if (!Comparison.isZero()) | |||
43412 | return SDValue(); | |||
43413 | ||||
43414 | if (CC == X86::COND_S && Addend == 1) | |||
43415 | CC = X86::COND_LE; | |||
43416 | else if (CC == X86::COND_NS && Addend == 1) | |||
43417 | CC = X86::COND_G; | |||
43418 | else if (CC == X86::COND_G && Addend == -1) | |||
43419 | CC = X86::COND_GE; | |||
43420 | else if (CC == X86::COND_LE && Addend == -1) | |||
43421 | CC = X86::COND_L; | |||
43422 | else | |||
43423 | return SDValue(); | |||
43424 | ||||
43425 | SDValue LockOp = lowerAtomicArithWithLOCK(CmpLHS, DAG, Subtarget); | |||
43426 | DAG.ReplaceAllUsesOfValueWith(CmpLHS.getValue(0), DAG.getUNDEF(CmpVT)); | |||
43427 | DAG.ReplaceAllUsesOfValueWith(CmpLHS.getValue(1), LockOp.getValue(1)); | |||
43428 | return LockOp; | |||
43429 | } | |||
43430 | ||||
43431 | // Check whether a boolean test is testing a boolean value generated by | |||
43432 | // X86ISD::SETCC. If so, return the operand of that SETCC and proper condition | |||
43433 | // code. | |||
43434 | // | |||
43435 | // Simplify the following patterns: | |||
43436 | // (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or | |||
43437 | // (Op (CMP (SETCC Cond EFLAGS) 0) NEQ) | |||
43438 | // to (Op EFLAGS Cond) | |||
43439 | // | |||
43440 | // (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or | |||
43441 | // (Op (CMP (SETCC Cond EFLAGS) 1) NEQ) | |||
43442 | // to (Op EFLAGS !Cond) | |||
43443 | // | |||
43444 | // where Op could be BRCOND or CMOV. | |||
43445 | // | |||
43446 | static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) { | |||
43447 | // This combine only operates on CMP-like nodes. | |||
43448 | if (!(Cmp.getOpcode() == X86ISD::CMP || | |||
43449 | (Cmp.getOpcode() == X86ISD::SUB && !Cmp->hasAnyUseOfValue(0)))) | |||
43450 | return SDValue(); | |||
43451 | ||||
43452 | // Quit if not used as a boolean value. | |||
43453 | if (CC != X86::COND_E && CC != X86::COND_NE) | |||
43454 | return SDValue(); | |||
43455 | ||||
43456 | // Check CMP operands. One of them should be 0 or 1 and the other should be | |||
43457 | // an SetCC or extended from it. | |||
43458 | SDValue Op1 = Cmp.getOperand(0); | |||
43459 | SDValue Op2 = Cmp.getOperand(1); | |||
43460 | ||||
43461 | SDValue SetCC; | |||
43462 | const ConstantSDNode* C = nullptr; | |||
43463 | bool needOppositeCond = (CC == X86::COND_E); | |||
43464 | bool checkAgainstTrue = false; // Is it a comparison against 1? | |||
43465 | ||||
43466 | if ((C = dyn_cast<ConstantSDNode>(Op1))) | |||
43467 | SetCC = Op2; | |||
43468 | else if ((C = dyn_cast<ConstantSDNode>(Op2))) | |||
43469 | SetCC = Op1; | |||
43470 | else // Quit if all operands are not constants. | |||
43471 | return SDValue(); | |||
43472 | ||||
43473 | if (C->getZExtValue() == 1) { | |||
43474 | needOppositeCond = !needOppositeCond; | |||
43475 | checkAgainstTrue = true; | |||
43476 | } else if (C->getZExtValue() != 0) | |||
43477 | // Quit if the constant is neither 0 or 1. | |||
43478 | return SDValue(); | |||
43479 | ||||
43480 | bool truncatedToBoolWithAnd = false; | |||
43481 | // Skip (zext $x), (trunc $x), or (and $x, 1) node. | |||
43482 | while (SetCC.getOpcode() == ISD::ZERO_EXTEND || | |||
43483 | SetCC.getOpcode() == ISD::TRUNCATE || | |||
43484 | SetCC.getOpcode() == ISD::AND) { | |||
43485 | if (SetCC.getOpcode() == ISD::AND) { | |||
43486 | int OpIdx = -1; | |||
43487 | if (isOneConstant(SetCC.getOperand(0))) | |||
43488 | OpIdx = 1; | |||
43489 | if (isOneConstant(SetCC.getOperand(1))) | |||
43490 | OpIdx = 0; | |||
43491 | if (OpIdx < 0) | |||
43492 | break; | |||
43493 | SetCC = SetCC.getOperand(OpIdx); | |||
43494 | truncatedToBoolWithAnd = true; | |||
43495 | } else | |||
43496 | SetCC = SetCC.getOperand(0); | |||
43497 | } | |||
43498 | ||||
43499 | switch (SetCC.getOpcode()) { | |||
43500 | case X86ISD::SETCC_CARRY: | |||
43501 | // Since SETCC_CARRY gives output based on R = CF ? ~0 : 0, it's unsafe to | |||
43502 | // simplify it if the result of SETCC_CARRY is not canonicalized to 0 or 1, | |||
43503 | // i.e. it's a comparison against true but the result of SETCC_CARRY is not | |||
43504 | // truncated to i1 using 'and'. | |||
43505 | if (checkAgainstTrue && !truncatedToBoolWithAnd) | |||
43506 | break; | |||
43507 | assert(X86::CondCode(SetCC.getConstantOperandVal(0)) == X86::COND_B &&(static_cast <bool> (X86::CondCode(SetCC.getConstantOperandVal (0)) == X86::COND_B && "Invalid use of SETCC_CARRY!") ? void (0) : __assert_fail ("X86::CondCode(SetCC.getConstantOperandVal(0)) == X86::COND_B && \"Invalid use of SETCC_CARRY!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 43508, __extension__ __PRETTY_FUNCTION__)) | |||
43508 | "Invalid use of SETCC_CARRY!")(static_cast <bool> (X86::CondCode(SetCC.getConstantOperandVal (0)) == X86::COND_B && "Invalid use of SETCC_CARRY!") ? void (0) : __assert_fail ("X86::CondCode(SetCC.getConstantOperandVal(0)) == X86::COND_B && \"Invalid use of SETCC_CARRY!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 43508, __extension__ __PRETTY_FUNCTION__)); | |||
43509 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
43510 | case X86ISD::SETCC: | |||
43511 | // Set the condition code or opposite one if necessary. | |||
43512 | CC = X86::CondCode(SetCC.getConstantOperandVal(0)); | |||
43513 | if (needOppositeCond) | |||
43514 | CC = X86::GetOppositeBranchCondition(CC); | |||
43515 | return SetCC.getOperand(1); | |||
43516 | case X86ISD::CMOV: { | |||
43517 | // Check whether false/true value has canonical one, i.e. 0 or 1. | |||
43518 | ConstantSDNode *FVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(0)); | |||
43519 | ConstantSDNode *TVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(1)); | |||
43520 | // Quit if true value is not a constant. | |||
43521 | if (!TVal) | |||
43522 | return SDValue(); | |||
43523 | // Quit if false value is not a constant. | |||
43524 | if (!FVal) { | |||
43525 | SDValue Op = SetCC.getOperand(0); | |||
43526 | // Skip 'zext' or 'trunc' node. | |||
43527 | if (Op.getOpcode() == ISD::ZERO_EXTEND || | |||
43528 | Op.getOpcode() == ISD::TRUNCATE) | |||
43529 | Op = Op.getOperand(0); | |||
43530 | // A special case for rdrand/rdseed, where 0 is set if false cond is | |||
43531 | // found. | |||
43532 | if ((Op.getOpcode() != X86ISD::RDRAND && | |||
43533 | Op.getOpcode() != X86ISD::RDSEED) || Op.getResNo() != 0) | |||
43534 | return SDValue(); | |||
43535 | } | |||
43536 | // Quit if false value is not the constant 0 or 1. | |||
43537 | bool FValIsFalse = true; | |||
43538 | if (FVal && FVal->getZExtValue() != 0) { | |||
43539 | if (FVal->getZExtValue() != 1) | |||
43540 | return SDValue(); | |||
43541 | // If FVal is 1, opposite cond is needed. | |||
43542 | needOppositeCond = !needOppositeCond; | |||
43543 | FValIsFalse = false; | |||
43544 | } | |||
43545 | // Quit if TVal is not the constant opposite of FVal. | |||
43546 | if (FValIsFalse && TVal->getZExtValue() != 1) | |||
43547 | return SDValue(); | |||
43548 | if (!FValIsFalse && TVal->getZExtValue() != 0) | |||
43549 | return SDValue(); | |||
43550 | CC = X86::CondCode(SetCC.getConstantOperandVal(2)); | |||
43551 | if (needOppositeCond) | |||
43552 | CC = X86::GetOppositeBranchCondition(CC); | |||
43553 | return SetCC.getOperand(3); | |||
43554 | } | |||
43555 | } | |||
43556 | ||||
43557 | return SDValue(); | |||
43558 | } | |||
43559 | ||||
43560 | /// Check whether Cond is an AND/OR of SETCCs off of the same EFLAGS. | |||
43561 | /// Match: | |||
43562 | /// (X86or (X86setcc) (X86setcc)) | |||
43563 | /// (X86cmp (and (X86setcc) (X86setcc)), 0) | |||
43564 | static bool checkBoolTestAndOrSetCCCombine(SDValue Cond, X86::CondCode &CC0, | |||
43565 | X86::CondCode &CC1, SDValue &Flags, | |||
43566 | bool &isAnd) { | |||
43567 | if (Cond->getOpcode() == X86ISD::CMP) { | |||
43568 | if (!isNullConstant(Cond->getOperand(1))) | |||
43569 | return false; | |||
43570 | ||||
43571 | Cond = Cond->getOperand(0); | |||
43572 | } | |||
43573 | ||||
43574 | isAnd = false; | |||
43575 | ||||
43576 | SDValue SetCC0, SetCC1; | |||
43577 | switch (Cond->getOpcode()) { | |||
43578 | default: return false; | |||
43579 | case ISD::AND: | |||
43580 | case X86ISD::AND: | |||
43581 | isAnd = true; | |||
43582 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
43583 | case ISD::OR: | |||
43584 | case X86ISD::OR: | |||
43585 | SetCC0 = Cond->getOperand(0); | |||
43586 | SetCC1 = Cond->getOperand(1); | |||
43587 | break; | |||
43588 | }; | |||
43589 | ||||
43590 | // Make sure we have SETCC nodes, using the same flags value. | |||
43591 | if (SetCC0.getOpcode() != X86ISD::SETCC || | |||
43592 | SetCC1.getOpcode() != X86ISD::SETCC || | |||
43593 | SetCC0->getOperand(1) != SetCC1->getOperand(1)) | |||
43594 | return false; | |||
43595 | ||||
43596 | CC0 = (X86::CondCode)SetCC0->getConstantOperandVal(0); | |||
43597 | CC1 = (X86::CondCode)SetCC1->getConstantOperandVal(0); | |||
43598 | Flags = SetCC0->getOperand(1); | |||
43599 | return true; | |||
43600 | } | |||
43601 | ||||
43602 | // When legalizing carry, we create carries via add X, -1 | |||
43603 | // If that comes from an actual carry, via setcc, we use the | |||
43604 | // carry directly. | |||
43605 | static SDValue combineCarryThroughADD(SDValue EFLAGS, SelectionDAG &DAG) { | |||
43606 | if (EFLAGS.getOpcode() == X86ISD::ADD) { | |||
43607 | if (isAllOnesConstant(EFLAGS.getOperand(1))) { | |||
43608 | SDValue Carry = EFLAGS.getOperand(0); | |||
43609 | while (Carry.getOpcode() == ISD::TRUNCATE || | |||
43610 | Carry.getOpcode() == ISD::ZERO_EXTEND || | |||
43611 | Carry.getOpcode() == ISD::SIGN_EXTEND || | |||
43612 | Carry.getOpcode() == ISD::ANY_EXTEND || | |||
43613 | (Carry.getOpcode() == ISD::AND && | |||
43614 | isOneConstant(Carry.getOperand(1)))) | |||
43615 | Carry = Carry.getOperand(0); | |||
43616 | if (Carry.getOpcode() == X86ISD::SETCC || | |||
43617 | Carry.getOpcode() == X86ISD::SETCC_CARRY) { | |||
43618 | // TODO: Merge this code with equivalent in combineAddOrSubToADCOrSBB? | |||
43619 | uint64_t CarryCC = Carry.getConstantOperandVal(0); | |||
43620 | SDValue CarryOp1 = Carry.getOperand(1); | |||
43621 | if (CarryCC == X86::COND_B) | |||
43622 | return CarryOp1; | |||
43623 | if (CarryCC == X86::COND_A) { | |||
43624 | // Try to convert COND_A into COND_B in an attempt to facilitate | |||
43625 | // materializing "setb reg". | |||
43626 | // | |||
43627 | // Do not flip "e > c", where "c" is a constant, because Cmp | |||
43628 | // instruction cannot take an immediate as its first operand. | |||
43629 | // | |||
43630 | if (CarryOp1.getOpcode() == X86ISD::SUB && | |||
43631 | CarryOp1.getNode()->hasOneUse() && | |||
43632 | CarryOp1.getValueType().isInteger() && | |||
43633 | !isa<ConstantSDNode>(CarryOp1.getOperand(1))) { | |||
43634 | SDValue SubCommute = | |||
43635 | DAG.getNode(X86ISD::SUB, SDLoc(CarryOp1), CarryOp1->getVTList(), | |||
43636 | CarryOp1.getOperand(1), CarryOp1.getOperand(0)); | |||
43637 | return SDValue(SubCommute.getNode(), CarryOp1.getResNo()); | |||
43638 | } | |||
43639 | } | |||
43640 | // If this is a check of the z flag of an add with 1, switch to the | |||
43641 | // C flag. | |||
43642 | if (CarryCC == X86::COND_E && | |||
43643 | CarryOp1.getOpcode() == X86ISD::ADD && | |||
43644 | isOneConstant(CarryOp1.getOperand(1))) | |||
43645 | return CarryOp1; | |||
43646 | } | |||
43647 | } | |||
43648 | } | |||
43649 | ||||
43650 | return SDValue(); | |||
43651 | } | |||
43652 | ||||
43653 | /// If we are inverting an PTEST/TESTP operand, attempt to adjust the CC | |||
43654 | /// to avoid the inversion. | |||
43655 | static SDValue combinePTESTCC(SDValue EFLAGS, X86::CondCode &CC, | |||
43656 | SelectionDAG &DAG, | |||
43657 | const X86Subtarget &Subtarget) { | |||
43658 | // TODO: Handle X86ISD::KTEST/X86ISD::KORTEST. | |||
43659 | if (EFLAGS.getOpcode() != X86ISD::PTEST && | |||
43660 | EFLAGS.getOpcode() != X86ISD::TESTP) | |||
43661 | return SDValue(); | |||
43662 | ||||
43663 | // PTEST/TESTP sets EFLAGS as: | |||
43664 | // TESTZ: ZF = (Op0 & Op1) == 0 | |||
43665 | // TESTC: CF = (~Op0 & Op1) == 0 | |||
43666 | // TESTNZC: ZF == 0 && CF == 0 | |||
43667 | EVT VT = EFLAGS.getValueType(); | |||
43668 | SDValue Op0 = EFLAGS.getOperand(0); | |||
43669 | SDValue Op1 = EFLAGS.getOperand(1); | |||
43670 | EVT OpVT = Op0.getValueType(); | |||
43671 | ||||
43672 | // TEST*(~X,Y) == TEST*(X,Y) | |||
43673 | if (SDValue NotOp0 = IsNOT(Op0, DAG)) { | |||
43674 | X86::CondCode InvCC; | |||
43675 | switch (CC) { | |||
43676 | case X86::COND_B: | |||
43677 | // testc -> testz. | |||
43678 | InvCC = X86::COND_E; | |||
43679 | break; | |||
43680 | case X86::COND_AE: | |||
43681 | // !testc -> !testz. | |||
43682 | InvCC = X86::COND_NE; | |||
43683 | break; | |||
43684 | case X86::COND_E: | |||
43685 | // testz -> testc. | |||
43686 | InvCC = X86::COND_B; | |||
43687 | break; | |||
43688 | case X86::COND_NE: | |||
43689 | // !testz -> !testc. | |||
43690 | InvCC = X86::COND_AE; | |||
43691 | break; | |||
43692 | case X86::COND_A: | |||
43693 | case X86::COND_BE: | |||
43694 | // testnzc -> testnzc (no change). | |||
43695 | InvCC = CC; | |||
43696 | break; | |||
43697 | default: | |||
43698 | InvCC = X86::COND_INVALID; | |||
43699 | break; | |||
43700 | } | |||
43701 | ||||
43702 | if (InvCC != X86::COND_INVALID) { | |||
43703 | CC = InvCC; | |||
43704 | return DAG.getNode(EFLAGS.getOpcode(), SDLoc(EFLAGS), VT, | |||
43705 | DAG.getBitcast(OpVT, NotOp0), Op1); | |||
43706 | } | |||
43707 | } | |||
43708 | ||||
43709 | if (CC == X86::COND_E || CC == X86::COND_NE) { | |||
43710 | // TESTZ(X,~Y) == TESTC(Y,X) | |||
43711 | if (SDValue NotOp1 = IsNOT(Op1, DAG)) { | |||
43712 | CC = (CC == X86::COND_E ? X86::COND_B : X86::COND_AE); | |||
43713 | return DAG.getNode(EFLAGS.getOpcode(), SDLoc(EFLAGS), VT, | |||
43714 | DAG.getBitcast(OpVT, NotOp1), Op0); | |||
43715 | } | |||
43716 | ||||
43717 | if (Op0 == Op1) { | |||
43718 | SDValue BC = peekThroughBitcasts(Op0); | |||
43719 | EVT BCVT = BC.getValueType(); | |||
43720 | assert(BCVT.isVector() && DAG.getTargetLoweringInfo().isTypeLegal(BCVT) &&(static_cast <bool> (BCVT.isVector() && DAG.getTargetLoweringInfo ().isTypeLegal(BCVT) && "Unexpected vector type") ? void (0) : __assert_fail ("BCVT.isVector() && DAG.getTargetLoweringInfo().isTypeLegal(BCVT) && \"Unexpected vector type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 43721, __extension__ __PRETTY_FUNCTION__)) | |||
43721 | "Unexpected vector type")(static_cast <bool> (BCVT.isVector() && DAG.getTargetLoweringInfo ().isTypeLegal(BCVT) && "Unexpected vector type") ? void (0) : __assert_fail ("BCVT.isVector() && DAG.getTargetLoweringInfo().isTypeLegal(BCVT) && \"Unexpected vector type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 43721, __extension__ __PRETTY_FUNCTION__)); | |||
43722 | ||||
43723 | // TESTZ(AND(X,Y),AND(X,Y)) == TESTZ(X,Y) | |||
43724 | if (BC.getOpcode() == ISD::AND || BC.getOpcode() == X86ISD::FAND) { | |||
43725 | return DAG.getNode(EFLAGS.getOpcode(), SDLoc(EFLAGS), VT, | |||
43726 | DAG.getBitcast(OpVT, BC.getOperand(0)), | |||
43727 | DAG.getBitcast(OpVT, BC.getOperand(1))); | |||
43728 | } | |||
43729 | ||||
43730 | // TESTZ(AND(~X,Y),AND(~X,Y)) == TESTC(X,Y) | |||
43731 | if (BC.getOpcode() == X86ISD::ANDNP || BC.getOpcode() == X86ISD::FANDN) { | |||
43732 | CC = (CC == X86::COND_E ? X86::COND_B : X86::COND_AE); | |||
43733 | return DAG.getNode(EFLAGS.getOpcode(), SDLoc(EFLAGS), VT, | |||
43734 | DAG.getBitcast(OpVT, BC.getOperand(0)), | |||
43735 | DAG.getBitcast(OpVT, BC.getOperand(1))); | |||
43736 | } | |||
43737 | ||||
43738 | // If every element is an all-sign value, see if we can use MOVMSK to | |||
43739 | // more efficiently extract the sign bits and compare that. | |||
43740 | // TODO: Handle TESTC with comparison inversion. | |||
43741 | // TODO: Can we remove SimplifyMultipleUseDemandedBits and rely on | |||
43742 | // MOVMSK combines to make sure its never worse than PTEST? | |||
43743 | unsigned EltBits = BCVT.getScalarSizeInBits(); | |||
43744 | if (DAG.ComputeNumSignBits(BC) == EltBits) { | |||
43745 | assert(VT == MVT::i32 && "Expected i32 EFLAGS comparison result")(static_cast <bool> (VT == MVT::i32 && "Expected i32 EFLAGS comparison result" ) ? void (0) : __assert_fail ("VT == MVT::i32 && \"Expected i32 EFLAGS comparison result\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 43745, __extension__ __PRETTY_FUNCTION__)); | |||
43746 | APInt SignMask = APInt::getSignMask(EltBits); | |||
43747 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
43748 | if (SDValue Res = | |||
43749 | TLI.SimplifyMultipleUseDemandedBits(BC, SignMask, DAG)) { | |||
43750 | // For vXi16 cases we need to use pmovmksb and extract every other | |||
43751 | // sign bit. | |||
43752 | SDLoc DL(EFLAGS); | |||
43753 | if (EltBits == 16) { | |||
43754 | MVT MovmskVT = BCVT.is128BitVector() ? MVT::v16i8 : MVT::v32i8; | |||
43755 | Res = DAG.getBitcast(MovmskVT, Res); | |||
43756 | Res = getPMOVMSKB(DL, Res, DAG, Subtarget); | |||
43757 | Res = DAG.getNode(ISD::AND, DL, MVT::i32, Res, | |||
43758 | DAG.getConstant(0xAAAAAAAA, DL, MVT::i32)); | |||
43759 | } else { | |||
43760 | Res = getPMOVMSKB(DL, Res, DAG, Subtarget); | |||
43761 | } | |||
43762 | return DAG.getNode(X86ISD::CMP, DL, MVT::i32, Res, | |||
43763 | DAG.getConstant(0, DL, MVT::i32)); | |||
43764 | } | |||
43765 | } | |||
43766 | } | |||
43767 | ||||
43768 | // TESTZ(-1,X) == TESTZ(X,X) | |||
43769 | if (ISD::isBuildVectorAllOnes(Op0.getNode())) | |||
43770 | return DAG.getNode(EFLAGS.getOpcode(), SDLoc(EFLAGS), VT, Op1, Op1); | |||
43771 | ||||
43772 | // TESTZ(X,-1) == TESTZ(X,X) | |||
43773 | if (ISD::isBuildVectorAllOnes(Op1.getNode())) | |||
43774 | return DAG.getNode(EFLAGS.getOpcode(), SDLoc(EFLAGS), VT, Op0, Op0); | |||
43775 | } | |||
43776 | ||||
43777 | return SDValue(); | |||
43778 | } | |||
43779 | ||||
43780 | // Attempt to simplify the MOVMSK input based on the comparison type. | |||
43781 | static SDValue combineSetCCMOVMSK(SDValue EFLAGS, X86::CondCode &CC, | |||
43782 | SelectionDAG &DAG, | |||
43783 | const X86Subtarget &Subtarget) { | |||
43784 | // Handle eq/ne against zero (any_of). | |||
43785 | // Handle eq/ne against -1 (all_of). | |||
43786 | if (!(CC == X86::COND_E || CC == X86::COND_NE)) | |||
43787 | return SDValue(); | |||
43788 | if (EFLAGS.getValueType() != MVT::i32) | |||
43789 | return SDValue(); | |||
43790 | unsigned CmpOpcode = EFLAGS.getOpcode(); | |||
43791 | if (CmpOpcode != X86ISD::CMP && CmpOpcode != X86ISD::SUB) | |||
43792 | return SDValue(); | |||
43793 | auto *CmpConstant = dyn_cast<ConstantSDNode>(EFLAGS.getOperand(1)); | |||
43794 | if (!CmpConstant) | |||
43795 | return SDValue(); | |||
43796 | const APInt &CmpVal = CmpConstant->getAPIntValue(); | |||
43797 | ||||
43798 | SDValue CmpOp = EFLAGS.getOperand(0); | |||
43799 | unsigned CmpBits = CmpOp.getValueSizeInBits(); | |||
43800 | assert(CmpBits == CmpVal.getBitWidth() && "Value size mismatch")(static_cast <bool> (CmpBits == CmpVal.getBitWidth() && "Value size mismatch") ? void (0) : __assert_fail ("CmpBits == CmpVal.getBitWidth() && \"Value size mismatch\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 43800, __extension__ __PRETTY_FUNCTION__)); | |||
43801 | ||||
43802 | // Peek through any truncate. | |||
43803 | if (CmpOp.getOpcode() == ISD::TRUNCATE) | |||
43804 | CmpOp = CmpOp.getOperand(0); | |||
43805 | ||||
43806 | // Bail if we don't find a MOVMSK. | |||
43807 | if (CmpOp.getOpcode() != X86ISD::MOVMSK) | |||
43808 | return SDValue(); | |||
43809 | ||||
43810 | SDValue Vec = CmpOp.getOperand(0); | |||
43811 | MVT VecVT = Vec.getSimpleValueType(); | |||
43812 | assert((VecVT.is128BitVector() || VecVT.is256BitVector()) &&(static_cast <bool> ((VecVT.is128BitVector() || VecVT.is256BitVector ()) && "Unexpected MOVMSK operand") ? void (0) : __assert_fail ("(VecVT.is128BitVector() || VecVT.is256BitVector()) && \"Unexpected MOVMSK operand\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 43813, __extension__ __PRETTY_FUNCTION__)) | |||
43813 | "Unexpected MOVMSK operand")(static_cast <bool> ((VecVT.is128BitVector() || VecVT.is256BitVector ()) && "Unexpected MOVMSK operand") ? void (0) : __assert_fail ("(VecVT.is128BitVector() || VecVT.is256BitVector()) && \"Unexpected MOVMSK operand\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 43813, __extension__ __PRETTY_FUNCTION__)); | |||
43814 | unsigned NumElts = VecVT.getVectorNumElements(); | |||
43815 | unsigned NumEltBits = VecVT.getScalarSizeInBits(); | |||
43816 | ||||
43817 | bool IsAnyOf = CmpOpcode == X86ISD::CMP && CmpVal.isZero(); | |||
43818 | bool IsAllOf = CmpOpcode == X86ISD::SUB && NumElts <= CmpBits && | |||
43819 | CmpVal.isMask(NumElts); | |||
43820 | if (!IsAnyOf && !IsAllOf) | |||
43821 | return SDValue(); | |||
43822 | ||||
43823 | // See if we can peek through to a vector with a wider element type, if the | |||
43824 | // signbits extend down to all the sub-elements as well. | |||
43825 | // Calling MOVMSK with the wider type, avoiding the bitcast, helps expose | |||
43826 | // potential SimplifyDemandedBits/Elts cases. | |||
43827 | if (Vec.getOpcode() == ISD::BITCAST) { | |||
43828 | SDValue BC = peekThroughBitcasts(Vec); | |||
43829 | MVT BCVT = BC.getSimpleValueType(); | |||
43830 | unsigned BCNumElts = BCVT.getVectorNumElements(); | |||
43831 | unsigned BCNumEltBits = BCVT.getScalarSizeInBits(); | |||
43832 | if ((BCNumEltBits == 32 || BCNumEltBits == 64) && | |||
43833 | BCNumEltBits > NumEltBits && | |||
43834 | DAG.ComputeNumSignBits(BC) > (BCNumEltBits - NumEltBits)) { | |||
43835 | SDLoc DL(EFLAGS); | |||
43836 | unsigned CmpMask = IsAnyOf ? 0 : ((1 << BCNumElts) - 1); | |||
43837 | return DAG.getNode(X86ISD::CMP, DL, MVT::i32, | |||
43838 | DAG.getNode(X86ISD::MOVMSK, DL, MVT::i32, BC), | |||
43839 | DAG.getConstant(CmpMask, DL, MVT::i32)); | |||
43840 | } | |||
43841 | } | |||
43842 | ||||
43843 | // MOVMSK(PCMPEQ(X,0)) == -1 -> PTESTZ(X,X). | |||
43844 | // MOVMSK(PCMPEQ(X,0)) != -1 -> !PTESTZ(X,X). | |||
43845 | if (IsAllOf && Subtarget.hasSSE41()) { | |||
43846 | SDValue BC = peekThroughBitcasts(Vec); | |||
43847 | if (BC.getOpcode() == X86ISD::PCMPEQ && | |||
43848 | ISD::isBuildVectorAllZeros(BC.getOperand(1).getNode())) { | |||
43849 | MVT TestVT = VecVT.is128BitVector() ? MVT::v2i64 : MVT::v4i64; | |||
43850 | SDValue V = DAG.getBitcast(TestVT, BC.getOperand(0)); | |||
43851 | return DAG.getNode(X86ISD::PTEST, SDLoc(EFLAGS), MVT::i32, V, V); | |||
43852 | } | |||
43853 | } | |||
43854 | ||||
43855 | // See if we can avoid a PACKSS by calling MOVMSK on the sources. | |||
43856 | // For vXi16 cases we can use a v2Xi8 PMOVMSKB. We must mask out | |||
43857 | // sign bits prior to the comparison with zero unless we know that | |||
43858 | // the vXi16 splats the sign bit down to the lower i8 half. | |||
43859 | // TODO: Handle all_of patterns. | |||
43860 | if (Vec.getOpcode() == X86ISD::PACKSS && VecVT == MVT::v16i8) { | |||
43861 | SDValue VecOp0 = Vec.getOperand(0); | |||
43862 | SDValue VecOp1 = Vec.getOperand(1); | |||
43863 | bool SignExt0 = DAG.ComputeNumSignBits(VecOp0) > 8; | |||
43864 | bool SignExt1 = DAG.ComputeNumSignBits(VecOp1) > 8; | |||
43865 | // PMOVMSKB(PACKSSBW(X, undef)) -> PMOVMSKB(BITCAST_v16i8(X)) & 0xAAAA. | |||
43866 | if (IsAnyOf && CmpBits == 8 && VecOp1.isUndef()) { | |||
43867 | SDLoc DL(EFLAGS); | |||
43868 | SDValue Result = DAG.getBitcast(MVT::v16i8, VecOp0); | |||
43869 | Result = DAG.getNode(X86ISD::MOVMSK, DL, MVT::i32, Result); | |||
43870 | Result = DAG.getZExtOrTrunc(Result, DL, MVT::i16); | |||
43871 | if (!SignExt0) { | |||
43872 | Result = DAG.getNode(ISD::AND, DL, MVT::i16, Result, | |||
43873 | DAG.getConstant(0xAAAA, DL, MVT::i16)); | |||
43874 | } | |||
43875 | return DAG.getNode(X86ISD::CMP, DL, MVT::i32, Result, | |||
43876 | DAG.getConstant(0, DL, MVT::i16)); | |||
43877 | } | |||
43878 | // PMOVMSKB(PACKSSBW(LO(X), HI(X))) | |||
43879 | // -> PMOVMSKB(BITCAST_v32i8(X)) & 0xAAAAAAAA. | |||
43880 | if (CmpBits >= 16 && Subtarget.hasInt256() && | |||
43881 | VecOp0.getOpcode() == ISD::EXTRACT_SUBVECTOR && | |||
43882 | VecOp1.getOpcode() == ISD::EXTRACT_SUBVECTOR && | |||
43883 | VecOp0.getOperand(0) == VecOp1.getOperand(0) && | |||
43884 | VecOp0.getConstantOperandAPInt(1) == 0 && | |||
43885 | VecOp1.getConstantOperandAPInt(1) == 8 && | |||
43886 | (IsAnyOf || (SignExt0 && SignExt1))) { | |||
43887 | SDLoc DL(EFLAGS); | |||
43888 | SDValue Result = DAG.getBitcast(MVT::v32i8, VecOp0.getOperand(0)); | |||
43889 | Result = DAG.getNode(X86ISD::MOVMSK, DL, MVT::i32, Result); | |||
43890 | unsigned CmpMask = IsAnyOf ? 0 : 0xFFFFFFFF; | |||
43891 | if (!SignExt0 || !SignExt1) { | |||
43892 | assert(IsAnyOf && "Only perform v16i16 signmasks for any_of patterns")(static_cast <bool> (IsAnyOf && "Only perform v16i16 signmasks for any_of patterns" ) ? void (0) : __assert_fail ("IsAnyOf && \"Only perform v16i16 signmasks for any_of patterns\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 43892, __extension__ __PRETTY_FUNCTION__)); | |||
43893 | Result = DAG.getNode(ISD::AND, DL, MVT::i32, Result, | |||
43894 | DAG.getConstant(0xAAAAAAAA, DL, MVT::i32)); | |||
43895 | } | |||
43896 | return DAG.getNode(X86ISD::CMP, DL, MVT::i32, Result, | |||
43897 | DAG.getConstant(CmpMask, DL, MVT::i32)); | |||
43898 | } | |||
43899 | } | |||
43900 | ||||
43901 | // MOVMSK(SHUFFLE(X,u)) -> MOVMSK(X) iff every element is referenced. | |||
43902 | SmallVector<int, 32> ShuffleMask; | |||
43903 | SmallVector<SDValue, 2> ShuffleInputs; | |||
43904 | if (NumElts <= CmpBits && | |||
43905 | getTargetShuffleInputs(peekThroughBitcasts(Vec), ShuffleInputs, | |||
43906 | ShuffleMask, DAG) && | |||
43907 | ShuffleInputs.size() == 1 && !isAnyZeroOrUndef(ShuffleMask) && | |||
43908 | ShuffleInputs[0].getValueSizeInBits() == VecVT.getSizeInBits()) { | |||
43909 | unsigned NumShuffleElts = ShuffleMask.size(); | |||
43910 | APInt DemandedElts = APInt::getZero(NumShuffleElts); | |||
43911 | for (int M : ShuffleMask) { | |||
43912 | assert(0 <= M && M < (int)NumShuffleElts && "Bad unary shuffle index")(static_cast <bool> (0 <= M && M < (int)NumShuffleElts && "Bad unary shuffle index") ? void (0) : __assert_fail ("0 <= M && M < (int)NumShuffleElts && \"Bad unary shuffle index\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 43912, __extension__ __PRETTY_FUNCTION__)); | |||
43913 | DemandedElts.setBit(M); | |||
43914 | } | |||
43915 | if (DemandedElts.isAllOnes()) { | |||
43916 | SDLoc DL(EFLAGS); | |||
43917 | SDValue Result = DAG.getBitcast(VecVT, ShuffleInputs[0]); | |||
43918 | Result = DAG.getNode(X86ISD::MOVMSK, DL, MVT::i32, Result); | |||
43919 | Result = | |||
43920 | DAG.getZExtOrTrunc(Result, DL, EFLAGS.getOperand(0).getValueType()); | |||
43921 | return DAG.getNode(X86ISD::CMP, DL, MVT::i32, Result, | |||
43922 | EFLAGS.getOperand(1)); | |||
43923 | } | |||
43924 | } | |||
43925 | ||||
43926 | return SDValue(); | |||
43927 | } | |||
43928 | ||||
43929 | /// Optimize an EFLAGS definition used according to the condition code \p CC | |||
43930 | /// into a simpler EFLAGS value, potentially returning a new \p CC and replacing | |||
43931 | /// uses of chain values. | |||
43932 | static SDValue combineSetCCEFLAGS(SDValue EFLAGS, X86::CondCode &CC, | |||
43933 | SelectionDAG &DAG, | |||
43934 | const X86Subtarget &Subtarget) { | |||
43935 | if (CC == X86::COND_B) | |||
43936 | if (SDValue Flags = combineCarryThroughADD(EFLAGS, DAG)) | |||
43937 | return Flags; | |||
43938 | ||||
43939 | if (SDValue R = checkBoolTestSetCCCombine(EFLAGS, CC)) | |||
43940 | return R; | |||
43941 | ||||
43942 | if (SDValue R = combinePTESTCC(EFLAGS, CC, DAG, Subtarget)) | |||
43943 | return R; | |||
43944 | ||||
43945 | if (SDValue R = combineSetCCMOVMSK(EFLAGS, CC, DAG, Subtarget)) | |||
43946 | return R; | |||
43947 | ||||
43948 | return combineSetCCAtomicArith(EFLAGS, CC, DAG, Subtarget); | |||
43949 | } | |||
43950 | ||||
43951 | /// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL] | |||
43952 | static SDValue combineCMov(SDNode *N, SelectionDAG &DAG, | |||
43953 | TargetLowering::DAGCombinerInfo &DCI, | |||
43954 | const X86Subtarget &Subtarget) { | |||
43955 | SDLoc DL(N); | |||
43956 | ||||
43957 | SDValue FalseOp = N->getOperand(0); | |||
43958 | SDValue TrueOp = N->getOperand(1); | |||
43959 | X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2); | |||
43960 | SDValue Cond = N->getOperand(3); | |||
43961 | ||||
43962 | // cmov X, X, ?, ? --> X | |||
43963 | if (TrueOp == FalseOp) | |||
43964 | return TrueOp; | |||
43965 | ||||
43966 | // Try to simplify the EFLAGS and condition code operands. | |||
43967 | // We can't always do this as FCMOV only supports a subset of X86 cond. | |||
43968 | if (SDValue Flags = combineSetCCEFLAGS(Cond, CC, DAG, Subtarget)) { | |||
43969 | if (!(FalseOp.getValueType() == MVT::f80 || | |||
43970 | (FalseOp.getValueType() == MVT::f64 && !Subtarget.hasSSE2()) || | |||
43971 | (FalseOp.getValueType() == MVT::f32 && !Subtarget.hasSSE1())) || | |||
43972 | !Subtarget.hasCMov() || hasFPCMov(CC)) { | |||
43973 | SDValue Ops[] = {FalseOp, TrueOp, DAG.getTargetConstant(CC, DL, MVT::i8), | |||
43974 | Flags}; | |||
43975 | return DAG.getNode(X86ISD::CMOV, DL, N->getValueType(0), Ops); | |||
43976 | } | |||
43977 | } | |||
43978 | ||||
43979 | // If this is a select between two integer constants, try to do some | |||
43980 | // optimizations. Note that the operands are ordered the opposite of SELECT | |||
43981 | // operands. | |||
43982 | if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) { | |||
43983 | if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) { | |||
43984 | // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is | |||
43985 | // larger than FalseC (the false value). | |||
43986 | if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) { | |||
43987 | CC = X86::GetOppositeBranchCondition(CC); | |||
43988 | std::swap(TrueC, FalseC); | |||
43989 | std::swap(TrueOp, FalseOp); | |||
43990 | } | |||
43991 | ||||
43992 | // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0. | |||
43993 | // This is efficient for any integer data type (including i8/i16) and | |||
43994 | // shift amount. | |||
43995 | if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) { | |||
43996 | Cond = getSETCC(CC, Cond, DL, DAG); | |||
43997 | ||||
43998 | // Zero extend the condition if needed. | |||
43999 | Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond); | |||
44000 | ||||
44001 | unsigned ShAmt = TrueC->getAPIntValue().logBase2(); | |||
44002 | Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond, | |||
44003 | DAG.getConstant(ShAmt, DL, MVT::i8)); | |||
44004 | return Cond; | |||
44005 | } | |||
44006 | ||||
44007 | // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient | |||
44008 | // for any integer data type, including i8/i16. | |||
44009 | if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) { | |||
44010 | Cond = getSETCC(CC, Cond, DL, DAG); | |||
44011 | ||||
44012 | // Zero extend the condition if needed. | |||
44013 | Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, | |||
44014 | FalseC->getValueType(0), Cond); | |||
44015 | Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond, | |||
44016 | SDValue(FalseC, 0)); | |||
44017 | return Cond; | |||
44018 | } | |||
44019 | ||||
44020 | // Optimize cases that will turn into an LEA instruction. This requires | |||
44021 | // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9). | |||
44022 | if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) { | |||
44023 | APInt Diff = TrueC->getAPIntValue() - FalseC->getAPIntValue(); | |||
44024 | assert(Diff.getBitWidth() == N->getValueType(0).getSizeInBits() &&(static_cast <bool> (Diff.getBitWidth() == N->getValueType (0).getSizeInBits() && "Implicit constant truncation" ) ? void (0) : __assert_fail ("Diff.getBitWidth() == N->getValueType(0).getSizeInBits() && \"Implicit constant truncation\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 44025, __extension__ __PRETTY_FUNCTION__)) | |||
44025 | "Implicit constant truncation")(static_cast <bool> (Diff.getBitWidth() == N->getValueType (0).getSizeInBits() && "Implicit constant truncation" ) ? void (0) : __assert_fail ("Diff.getBitWidth() == N->getValueType(0).getSizeInBits() && \"Implicit constant truncation\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 44025, __extension__ __PRETTY_FUNCTION__)); | |||
44026 | ||||
44027 | bool isFastMultiplier = false; | |||
44028 | if (Diff.ult(10)) { | |||
44029 | switch (Diff.getZExtValue()) { | |||
44030 | default: break; | |||
44031 | case 1: // result = add base, cond | |||
44032 | case 2: // result = lea base( , cond*2) | |||
44033 | case 3: // result = lea base(cond, cond*2) | |||
44034 | case 4: // result = lea base( , cond*4) | |||
44035 | case 5: // result = lea base(cond, cond*4) | |||
44036 | case 8: // result = lea base( , cond*8) | |||
44037 | case 9: // result = lea base(cond, cond*8) | |||
44038 | isFastMultiplier = true; | |||
44039 | break; | |||
44040 | } | |||
44041 | } | |||
44042 | ||||
44043 | if (isFastMultiplier) { | |||
44044 | Cond = getSETCC(CC, Cond, DL ,DAG); | |||
44045 | // Zero extend the condition if needed. | |||
44046 | Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0), | |||
44047 | Cond); | |||
44048 | // Scale the condition by the difference. | |||
44049 | if (Diff != 1) | |||
44050 | Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond, | |||
44051 | DAG.getConstant(Diff, DL, Cond.getValueType())); | |||
44052 | ||||
44053 | // Add the base if non-zero. | |||
44054 | if (FalseC->getAPIntValue() != 0) | |||
44055 | Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond, | |||
44056 | SDValue(FalseC, 0)); | |||
44057 | return Cond; | |||
44058 | } | |||
44059 | } | |||
44060 | } | |||
44061 | } | |||
44062 | ||||
44063 | // Handle these cases: | |||
44064 | // (select (x != c), e, c) -> select (x != c), e, x), | |||
44065 | // (select (x == c), c, e) -> select (x == c), x, e) | |||
44066 | // where the c is an integer constant, and the "select" is the combination | |||
44067 | // of CMOV and CMP. | |||
44068 | // | |||
44069 | // The rationale for this change is that the conditional-move from a constant | |||
44070 | // needs two instructions, however, conditional-move from a register needs | |||
44071 | // only one instruction. | |||
44072 | // | |||
44073 | // CAVEAT: By replacing a constant with a symbolic value, it may obscure | |||
44074 | // some instruction-combining opportunities. This opt needs to be | |||
44075 | // postponed as late as possible. | |||
44076 | // | |||
44077 | if (!DCI.isBeforeLegalize() && !DCI.isBeforeLegalizeOps()) { | |||
44078 | // the DCI.xxxx conditions are provided to postpone the optimization as | |||
44079 | // late as possible. | |||
44080 | ||||
44081 | ConstantSDNode *CmpAgainst = nullptr; | |||
44082 | if ((Cond.getOpcode() == X86ISD::CMP || Cond.getOpcode() == X86ISD::SUB) && | |||
44083 | (CmpAgainst = dyn_cast<ConstantSDNode>(Cond.getOperand(1))) && | |||
44084 | !isa<ConstantSDNode>(Cond.getOperand(0))) { | |||
44085 | ||||
44086 | if (CC == X86::COND_NE && | |||
44087 | CmpAgainst == dyn_cast<ConstantSDNode>(FalseOp)) { | |||
44088 | CC = X86::GetOppositeBranchCondition(CC); | |||
44089 | std::swap(TrueOp, FalseOp); | |||
44090 | } | |||
44091 | ||||
44092 | if (CC == X86::COND_E && | |||
44093 | CmpAgainst == dyn_cast<ConstantSDNode>(TrueOp)) { | |||
44094 | SDValue Ops[] = {FalseOp, Cond.getOperand(0), | |||
44095 | DAG.getTargetConstant(CC, DL, MVT::i8), Cond}; | |||
44096 | return DAG.getNode(X86ISD::CMOV, DL, N->getValueType(0), Ops); | |||
44097 | } | |||
44098 | } | |||
44099 | } | |||
44100 | ||||
44101 | // Fold and/or of setcc's to double CMOV: | |||
44102 | // (CMOV F, T, ((cc1 | cc2) != 0)) -> (CMOV (CMOV F, T, cc1), T, cc2) | |||
44103 | // (CMOV F, T, ((cc1 & cc2) != 0)) -> (CMOV (CMOV T, F, !cc1), F, !cc2) | |||
44104 | // | |||
44105 | // This combine lets us generate: | |||
44106 | // cmovcc1 (jcc1 if we don't have CMOV) | |||
44107 | // cmovcc2 (same) | |||
44108 | // instead of: | |||
44109 | // setcc1 | |||
44110 | // setcc2 | |||
44111 | // and/or | |||
44112 | // cmovne (jne if we don't have CMOV) | |||
44113 | // When we can't use the CMOV instruction, it might increase branch | |||
44114 | // mispredicts. | |||
44115 | // When we can use CMOV, or when there is no mispredict, this improves | |||
44116 | // throughput and reduces register pressure. | |||
44117 | // | |||
44118 | if (CC == X86::COND_NE) { | |||
44119 | SDValue Flags; | |||
44120 | X86::CondCode CC0, CC1; | |||
44121 | bool isAndSetCC; | |||
44122 | if (checkBoolTestAndOrSetCCCombine(Cond, CC0, CC1, Flags, isAndSetCC)) { | |||
44123 | if (isAndSetCC) { | |||
44124 | std::swap(FalseOp, TrueOp); | |||
44125 | CC0 = X86::GetOppositeBranchCondition(CC0); | |||
44126 | CC1 = X86::GetOppositeBranchCondition(CC1); | |||
44127 | } | |||
44128 | ||||
44129 | SDValue LOps[] = {FalseOp, TrueOp, | |||
44130 | DAG.getTargetConstant(CC0, DL, MVT::i8), Flags}; | |||
44131 | SDValue LCMOV = DAG.getNode(X86ISD::CMOV, DL, N->getValueType(0), LOps); | |||
44132 | SDValue Ops[] = {LCMOV, TrueOp, DAG.getTargetConstant(CC1, DL, MVT::i8), | |||
44133 | Flags}; | |||
44134 | SDValue CMOV = DAG.getNode(X86ISD::CMOV, DL, N->getValueType(0), Ops); | |||
44135 | return CMOV; | |||
44136 | } | |||
44137 | } | |||
44138 | ||||
44139 | // Fold (CMOV C1, (ADD (CTTZ X), C2), (X != 0)) -> | |||
44140 | // (ADD (CMOV C1-C2, (CTTZ X), (X != 0)), C2) | |||
44141 | // Or (CMOV (ADD (CTTZ X), C2), C1, (X == 0)) -> | |||
44142 | // (ADD (CMOV (CTTZ X), C1-C2, (X == 0)), C2) | |||
44143 | if ((CC == X86::COND_NE || CC == X86::COND_E) && | |||
44144 | Cond.getOpcode() == X86ISD::CMP && isNullConstant(Cond.getOperand(1))) { | |||
44145 | SDValue Add = TrueOp; | |||
44146 | SDValue Const = FalseOp; | |||
44147 | // Canonicalize the condition code for easier matching and output. | |||
44148 | if (CC == X86::COND_E) | |||
44149 | std::swap(Add, Const); | |||
44150 | ||||
44151 | // We might have replaced the constant in the cmov with the LHS of the | |||
44152 | // compare. If so change it to the RHS of the compare. | |||
44153 | if (Const == Cond.getOperand(0)) | |||
44154 | Const = Cond.getOperand(1); | |||
44155 | ||||
44156 | // Ok, now make sure that Add is (add (cttz X), C2) and Const is a constant. | |||
44157 | if (isa<ConstantSDNode>(Const) && Add.getOpcode() == ISD::ADD && | |||
44158 | Add.hasOneUse() && isa<ConstantSDNode>(Add.getOperand(1)) && | |||
44159 | (Add.getOperand(0).getOpcode() == ISD::CTTZ_ZERO_UNDEF || | |||
44160 | Add.getOperand(0).getOpcode() == ISD::CTTZ) && | |||
44161 | Add.getOperand(0).getOperand(0) == Cond.getOperand(0)) { | |||
44162 | EVT VT = N->getValueType(0); | |||
44163 | // This should constant fold. | |||
44164 | SDValue Diff = DAG.getNode(ISD::SUB, DL, VT, Const, Add.getOperand(1)); | |||
44165 | SDValue CMov = | |||
44166 | DAG.getNode(X86ISD::CMOV, DL, VT, Diff, Add.getOperand(0), | |||
44167 | DAG.getTargetConstant(X86::COND_NE, DL, MVT::i8), Cond); | |||
44168 | return DAG.getNode(ISD::ADD, DL, VT, CMov, Add.getOperand(1)); | |||
44169 | } | |||
44170 | } | |||
44171 | ||||
44172 | return SDValue(); | |||
44173 | } | |||
44174 | ||||
44175 | /// Different mul shrinking modes. | |||
44176 | enum class ShrinkMode { MULS8, MULU8, MULS16, MULU16 }; | |||
44177 | ||||
44178 | static bool canReduceVMulWidth(SDNode *N, SelectionDAG &DAG, ShrinkMode &Mode) { | |||
44179 | EVT VT = N->getOperand(0).getValueType(); | |||
44180 | if (VT.getScalarSizeInBits() != 32) | |||
44181 | return false; | |||
44182 | ||||
44183 | assert(N->getNumOperands() == 2 && "NumOperands of Mul are 2")(static_cast <bool> (N->getNumOperands() == 2 && "NumOperands of Mul are 2") ? void (0) : __assert_fail ("N->getNumOperands() == 2 && \"NumOperands of Mul are 2\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 44183, __extension__ __PRETTY_FUNCTION__)); | |||
44184 | unsigned SignBits[2] = {1, 1}; | |||
44185 | bool IsPositive[2] = {false, false}; | |||
44186 | for (unsigned i = 0; i < 2; i++) { | |||
44187 | SDValue Opd = N->getOperand(i); | |||
44188 | ||||
44189 | SignBits[i] = DAG.ComputeNumSignBits(Opd); | |||
44190 | IsPositive[i] = DAG.SignBitIsZero(Opd); | |||
44191 | } | |||
44192 | ||||
44193 | bool AllPositive = IsPositive[0] && IsPositive[1]; | |||
44194 | unsigned MinSignBits = std::min(SignBits[0], SignBits[1]); | |||
44195 | // When ranges are from -128 ~ 127, use MULS8 mode. | |||
44196 | if (MinSignBits >= 25) | |||
44197 | Mode = ShrinkMode::MULS8; | |||
44198 | // When ranges are from 0 ~ 255, use MULU8 mode. | |||
44199 | else if (AllPositive && MinSignBits >= 24) | |||
44200 | Mode = ShrinkMode::MULU8; | |||
44201 | // When ranges are from -32768 ~ 32767, use MULS16 mode. | |||
44202 | else if (MinSignBits >= 17) | |||
44203 | Mode = ShrinkMode::MULS16; | |||
44204 | // When ranges are from 0 ~ 65535, use MULU16 mode. | |||
44205 | else if (AllPositive && MinSignBits >= 16) | |||
44206 | Mode = ShrinkMode::MULU16; | |||
44207 | else | |||
44208 | return false; | |||
44209 | return true; | |||
44210 | } | |||
44211 | ||||
44212 | /// When the operands of vector mul are extended from smaller size values, | |||
44213 | /// like i8 and i16, the type of mul may be shrinked to generate more | |||
44214 | /// efficient code. Two typical patterns are handled: | |||
44215 | /// Pattern1: | |||
44216 | /// %2 = sext/zext <N x i8> %1 to <N x i32> | |||
44217 | /// %4 = sext/zext <N x i8> %3 to <N x i32> | |||
44218 | // or %4 = build_vector <N x i32> %C1, ..., %CN (%C1..%CN are constants) | |||
44219 | /// %5 = mul <N x i32> %2, %4 | |||
44220 | /// | |||
44221 | /// Pattern2: | |||
44222 | /// %2 = zext/sext <N x i16> %1 to <N x i32> | |||
44223 | /// %4 = zext/sext <N x i16> %3 to <N x i32> | |||
44224 | /// or %4 = build_vector <N x i32> %C1, ..., %CN (%C1..%CN are constants) | |||
44225 | /// %5 = mul <N x i32> %2, %4 | |||
44226 | /// | |||
44227 | /// There are four mul shrinking modes: | |||
44228 | /// If %2 == sext32(trunc8(%2)), i.e., the scalar value range of %2 is | |||
44229 | /// -128 to 128, and the scalar value range of %4 is also -128 to 128, | |||
44230 | /// generate pmullw+sext32 for it (MULS8 mode). | |||
44231 | /// If %2 == zext32(trunc8(%2)), i.e., the scalar value range of %2 is | |||
44232 | /// 0 to 255, and the scalar value range of %4 is also 0 to 255, | |||
44233 | /// generate pmullw+zext32 for it (MULU8 mode). | |||
44234 | /// If %2 == sext32(trunc16(%2)), i.e., the scalar value range of %2 is | |||
44235 | /// -32768 to 32767, and the scalar value range of %4 is also -32768 to 32767, | |||
44236 | /// generate pmullw+pmulhw for it (MULS16 mode). | |||
44237 | /// If %2 == zext32(trunc16(%2)), i.e., the scalar value range of %2 is | |||
44238 | /// 0 to 65535, and the scalar value range of %4 is also 0 to 65535, | |||
44239 | /// generate pmullw+pmulhuw for it (MULU16 mode). | |||
44240 | static SDValue reduceVMULWidth(SDNode *N, SelectionDAG &DAG, | |||
44241 | const X86Subtarget &Subtarget) { | |||
44242 | // Check for legality | |||
44243 | // pmullw/pmulhw are not supported by SSE. | |||
44244 | if (!Subtarget.hasSSE2()) | |||
44245 | return SDValue(); | |||
44246 | ||||
44247 | // Check for profitability | |||
44248 | // pmulld is supported since SSE41. It is better to use pmulld | |||
44249 | // instead of pmullw+pmulhw, except for subtargets where pmulld is slower than | |||
44250 | // the expansion. | |||
44251 | bool OptForMinSize = DAG.getMachineFunction().getFunction().hasMinSize(); | |||
44252 | if (Subtarget.hasSSE41() && (OptForMinSize || !Subtarget.isPMULLDSlow())) | |||
44253 | return SDValue(); | |||
44254 | ||||
44255 | ShrinkMode Mode; | |||
44256 | if (!canReduceVMulWidth(N, DAG, Mode)) | |||
44257 | return SDValue(); | |||
44258 | ||||
44259 | SDLoc DL(N); | |||
44260 | SDValue N0 = N->getOperand(0); | |||
44261 | SDValue N1 = N->getOperand(1); | |||
44262 | EVT VT = N->getOperand(0).getValueType(); | |||
44263 | unsigned NumElts = VT.getVectorNumElements(); | |||
44264 | if ((NumElts % 2) != 0) | |||
44265 | return SDValue(); | |||
44266 | ||||
44267 | EVT ReducedVT = EVT::getVectorVT(*DAG.getContext(), MVT::i16, NumElts); | |||
44268 | ||||
44269 | // Shrink the operands of mul. | |||
44270 | SDValue NewN0 = DAG.getNode(ISD::TRUNCATE, DL, ReducedVT, N0); | |||
44271 | SDValue NewN1 = DAG.getNode(ISD::TRUNCATE, DL, ReducedVT, N1); | |||
44272 | ||||
44273 | // Generate the lower part of mul: pmullw. For MULU8/MULS8, only the | |||
44274 | // lower part is needed. | |||
44275 | SDValue MulLo = DAG.getNode(ISD::MUL, DL, ReducedVT, NewN0, NewN1); | |||
44276 | if (Mode == ShrinkMode::MULU8 || Mode == ShrinkMode::MULS8) | |||
44277 | return DAG.getNode((Mode == ShrinkMode::MULU8) ? ISD::ZERO_EXTEND | |||
44278 | : ISD::SIGN_EXTEND, | |||
44279 | DL, VT, MulLo); | |||
44280 | ||||
44281 | EVT ResVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, NumElts / 2); | |||
44282 | // Generate the higher part of mul: pmulhw/pmulhuw. For MULU16/MULS16, | |||
44283 | // the higher part is also needed. | |||
44284 | SDValue MulHi = | |||
44285 | DAG.getNode(Mode == ShrinkMode::MULS16 ? ISD::MULHS : ISD::MULHU, DL, | |||
44286 | ReducedVT, NewN0, NewN1); | |||
44287 | ||||
44288 | // Repack the lower part and higher part result of mul into a wider | |||
44289 | // result. | |||
44290 | // Generate shuffle functioning as punpcklwd. | |||
44291 | SmallVector<int, 16> ShuffleMask(NumElts); | |||
44292 | for (unsigned i = 0, e = NumElts / 2; i < e; i++) { | |||
44293 | ShuffleMask[2 * i] = i; | |||
44294 | ShuffleMask[2 * i + 1] = i + NumElts; | |||
44295 | } | |||
44296 | SDValue ResLo = | |||
44297 | DAG.getVectorShuffle(ReducedVT, DL, MulLo, MulHi, ShuffleMask); | |||
44298 | ResLo = DAG.getBitcast(ResVT, ResLo); | |||
44299 | // Generate shuffle functioning as punpckhwd. | |||
44300 | for (unsigned i = 0, e = NumElts / 2; i < e; i++) { | |||
44301 | ShuffleMask[2 * i] = i + NumElts / 2; | |||
44302 | ShuffleMask[2 * i + 1] = i + NumElts * 3 / 2; | |||
44303 | } | |||
44304 | SDValue ResHi = | |||
44305 | DAG.getVectorShuffle(ReducedVT, DL, MulLo, MulHi, ShuffleMask); | |||
44306 | ResHi = DAG.getBitcast(ResVT, ResHi); | |||
44307 | return DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, ResLo, ResHi); | |||
44308 | } | |||
44309 | ||||
44310 | static SDValue combineMulSpecial(uint64_t MulAmt, SDNode *N, SelectionDAG &DAG, | |||
44311 | EVT VT, const SDLoc &DL) { | |||
44312 | ||||
44313 | auto combineMulShlAddOrSub = [&](int Mult, int Shift, bool isAdd) { | |||
44314 | SDValue Result = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0), | |||
44315 | DAG.getConstant(Mult, DL, VT)); | |||
44316 | Result = DAG.getNode(ISD::SHL, DL, VT, Result, | |||
44317 | DAG.getConstant(Shift, DL, MVT::i8)); | |||
44318 | Result = DAG.getNode(isAdd ? ISD::ADD : ISD::SUB, DL, VT, Result, | |||
44319 | N->getOperand(0)); | |||
44320 | return Result; | |||
44321 | }; | |||
44322 | ||||
44323 | auto combineMulMulAddOrSub = [&](int Mul1, int Mul2, bool isAdd) { | |||
44324 | SDValue Result = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0), | |||
44325 | DAG.getConstant(Mul1, DL, VT)); | |||
44326 | Result = DAG.getNode(X86ISD::MUL_IMM, DL, VT, Result, | |||
44327 | DAG.getConstant(Mul2, DL, VT)); | |||
44328 | Result = DAG.getNode(isAdd ? ISD::ADD : ISD::SUB, DL, VT, Result, | |||
44329 | N->getOperand(0)); | |||
44330 | return Result; | |||
44331 | }; | |||
44332 | ||||
44333 | switch (MulAmt) { | |||
44334 | default: | |||
44335 | break; | |||
44336 | case 11: | |||
44337 | // mul x, 11 => add ((shl (mul x, 5), 1), x) | |||
44338 | return combineMulShlAddOrSub(5, 1, /*isAdd*/ true); | |||
44339 | case 21: | |||
44340 | // mul x, 21 => add ((shl (mul x, 5), 2), x) | |||
44341 | return combineMulShlAddOrSub(5, 2, /*isAdd*/ true); | |||
44342 | case 41: | |||
44343 | // mul x, 41 => add ((shl (mul x, 5), 3), x) | |||
44344 | return combineMulShlAddOrSub(5, 3, /*isAdd*/ true); | |||
44345 | case 22: | |||
44346 | // mul x, 22 => add (add ((shl (mul x, 5), 2), x), x) | |||
44347 | return DAG.getNode(ISD::ADD, DL, VT, N->getOperand(0), | |||
44348 | combineMulShlAddOrSub(5, 2, /*isAdd*/ true)); | |||
44349 | case 19: | |||
44350 | // mul x, 19 => add ((shl (mul x, 9), 1), x) | |||
44351 | return combineMulShlAddOrSub(9, 1, /*isAdd*/ true); | |||
44352 | case 37: | |||
44353 | // mul x, 37 => add ((shl (mul x, 9), 2), x) | |||
44354 | return combineMulShlAddOrSub(9, 2, /*isAdd*/ true); | |||
44355 | case 73: | |||
44356 | // mul x, 73 => add ((shl (mul x, 9), 3), x) | |||
44357 | return combineMulShlAddOrSub(9, 3, /*isAdd*/ true); | |||
44358 | case 13: | |||
44359 | // mul x, 13 => add ((shl (mul x, 3), 2), x) | |||
44360 | return combineMulShlAddOrSub(3, 2, /*isAdd*/ true); | |||
44361 | case 23: | |||
44362 | // mul x, 23 => sub ((shl (mul x, 3), 3), x) | |||
44363 | return combineMulShlAddOrSub(3, 3, /*isAdd*/ false); | |||
44364 | case 26: | |||
44365 | // mul x, 26 => add ((mul (mul x, 5), 5), x) | |||
44366 | return combineMulMulAddOrSub(5, 5, /*isAdd*/ true); | |||
44367 | case 28: | |||
44368 | // mul x, 28 => add ((mul (mul x, 9), 3), x) | |||
44369 | return combineMulMulAddOrSub(9, 3, /*isAdd*/ true); | |||
44370 | case 29: | |||
44371 | // mul x, 29 => add (add ((mul (mul x, 9), 3), x), x) | |||
44372 | return DAG.getNode(ISD::ADD, DL, VT, N->getOperand(0), | |||
44373 | combineMulMulAddOrSub(9, 3, /*isAdd*/ true)); | |||
44374 | } | |||
44375 | ||||
44376 | // Another trick. If this is a power 2 + 2/4/8, we can use a shift followed | |||
44377 | // by a single LEA. | |||
44378 | // First check if this a sum of two power of 2s because that's easy. Then | |||
44379 | // count how many zeros are up to the first bit. | |||
44380 | // TODO: We can do this even without LEA at a cost of two shifts and an add. | |||
44381 | if (isPowerOf2_64(MulAmt & (MulAmt - 1))) { | |||
44382 | unsigned ScaleShift = countTrailingZeros(MulAmt); | |||
44383 | if (ScaleShift >= 1 && ScaleShift < 4) { | |||
44384 | unsigned ShiftAmt = Log2_64((MulAmt & (MulAmt - 1))); | |||
44385 | SDValue Shift1 = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0), | |||
44386 | DAG.getConstant(ShiftAmt, DL, MVT::i8)); | |||
44387 | SDValue Shift2 = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0), | |||
44388 | DAG.getConstant(ScaleShift, DL, MVT::i8)); | |||
44389 | return DAG.getNode(ISD::ADD, DL, VT, Shift1, Shift2); | |||
44390 | } | |||
44391 | } | |||
44392 | ||||
44393 | return SDValue(); | |||
44394 | } | |||
44395 | ||||
44396 | // If the upper 17 bits of either element are zero and the other element are | |||
44397 | // zero/sign bits then we can use PMADDWD, which is always at least as quick as | |||
44398 | // PMULLD, except on KNL. | |||
44399 | static SDValue combineMulToPMADDWD(SDNode *N, SelectionDAG &DAG, | |||
44400 | const X86Subtarget &Subtarget) { | |||
44401 | if (!Subtarget.hasSSE2()) | |||
44402 | return SDValue(); | |||
44403 | ||||
44404 | if (Subtarget.isPMADDWDSlow()) | |||
44405 | return SDValue(); | |||
44406 | ||||
44407 | EVT VT = N->getValueType(0); | |||
44408 | ||||
44409 | // Only support vXi32 vectors. | |||
44410 | if (!VT.isVector() || VT.getVectorElementType() != MVT::i32) | |||
44411 | return SDValue(); | |||
44412 | ||||
44413 | // Make sure the type is legal or will be widened to a legal type. | |||
44414 | if (VT != MVT::v2i32 && !DAG.getTargetLoweringInfo().isTypeLegal(VT)) | |||
44415 | return SDValue(); | |||
44416 | ||||
44417 | MVT WVT = MVT::getVectorVT(MVT::i16, 2 * VT.getVectorNumElements()); | |||
44418 | ||||
44419 | // Without BWI, we would need to split v32i16. | |||
44420 | if (WVT == MVT::v32i16 && !Subtarget.hasBWI()) | |||
44421 | return SDValue(); | |||
44422 | ||||
44423 | SDValue N0 = N->getOperand(0); | |||
44424 | SDValue N1 = N->getOperand(1); | |||
44425 | ||||
44426 | // If we are zero extending two steps without SSE4.1, its better to reduce | |||
44427 | // the vmul width instead. | |||
44428 | if (!Subtarget.hasSSE41() && | |||
44429 | (N0.getOpcode() == ISD::ZERO_EXTEND && | |||
44430 | N0.getOperand(0).getScalarValueSizeInBits() <= 8) && | |||
44431 | (N1.getOpcode() == ISD::ZERO_EXTEND && | |||
44432 | N1.getOperand(0).getScalarValueSizeInBits() <= 8)) | |||
44433 | return SDValue(); | |||
44434 | ||||
44435 | // Sign bits must extend through the upper 17 bits. | |||
44436 | if (DAG.ComputeNumSignBits(N1) < 17 || DAG.ComputeNumSignBits(N0) < 17) | |||
44437 | return SDValue(); | |||
44438 | ||||
44439 | // At least one of the elements must be zero in the upper 17 bits, or can be | |||
44440 | // safely made zero without altering the final result. | |||
44441 | auto GetZeroableOp = [&](SDValue Op) { | |||
44442 | APInt Mask17 = APInt::getHighBitsSet(32, 17); | |||
44443 | if (DAG.MaskedValueIsZero(Op, Mask17)) | |||
44444 | return Op; | |||
44445 | // Mask off upper 16-bits of sign-extended constants. | |||
44446 | if (ISD::isBuildVectorOfConstantSDNodes(Op.getNode())) | |||
44447 | return DAG.getNode(ISD::AND, SDLoc(N), VT, Op, | |||
44448 | DAG.getConstant(0xFFFF, SDLoc(N), VT)); | |||
44449 | // Convert sext(vXi16) to zext(vXi16). | |||
44450 | if (Op.getOpcode() == ISD::SIGN_EXTEND && VT.getSizeInBits() <= 128 && | |||
44451 | N->isOnlyUserOf(Op.getNode())) { | |||
44452 | SDValue Src = Op.getOperand(0); | |||
44453 | if (Src.getScalarValueSizeInBits() == 16) | |||
44454 | return DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), VT, Src); | |||
44455 | } | |||
44456 | // Convert SIGN_EXTEND_VECTOR_INREG to ZEXT_EXTEND_VECTOR_INREG. | |||
44457 | if (Op.getOpcode() == ISD::SIGN_EXTEND_VECTOR_INREG && | |||
44458 | N->isOnlyUserOf(Op.getNode())) { | |||
44459 | SDValue Src = Op.getOperand(0); | |||
44460 | if (Src.getScalarValueSizeInBits() == 16) | |||
44461 | return DAG.getNode(ISD::ZERO_EXTEND_VECTOR_INREG, SDLoc(N), VT, Src); | |||
44462 | } | |||
44463 | return SDValue(); | |||
44464 | }; | |||
44465 | SDValue ZeroN0 = GetZeroableOp(N0); | |||
44466 | SDValue ZeroN1 = GetZeroableOp(N1); | |||
44467 | if (!ZeroN0 && !ZeroN1) | |||
44468 | return SDValue(); | |||
44469 | N0 = ZeroN0 ? ZeroN0 : N0; | |||
44470 | N1 = ZeroN1 ? ZeroN1 : N1; | |||
44471 | ||||
44472 | // Use SplitOpsAndApply to handle AVX splitting. | |||
44473 | auto PMADDWDBuilder = [](SelectionDAG &DAG, const SDLoc &DL, | |||
44474 | ArrayRef<SDValue> Ops) { | |||
44475 | MVT OpVT = MVT::getVectorVT(MVT::i32, Ops[0].getValueSizeInBits() / 32); | |||
44476 | return DAG.getNode(X86ISD::VPMADDWD, DL, OpVT, Ops); | |||
44477 | }; | |||
44478 | return SplitOpsAndApply(DAG, Subtarget, SDLoc(N), VT, | |||
44479 | { DAG.getBitcast(WVT, N0), DAG.getBitcast(WVT, N1) }, | |||
44480 | PMADDWDBuilder); | |||
44481 | } | |||
44482 | ||||
44483 | static SDValue combineMulToPMULDQ(SDNode *N, SelectionDAG &DAG, | |||
44484 | const X86Subtarget &Subtarget) { | |||
44485 | if (!Subtarget.hasSSE2()) | |||
44486 | return SDValue(); | |||
44487 | ||||
44488 | EVT VT = N->getValueType(0); | |||
44489 | ||||
44490 | // Only support vXi64 vectors. | |||
44491 | if (!VT.isVector() || VT.getVectorElementType() != MVT::i64 || | |||
44492 | VT.getVectorNumElements() < 2 || | |||
44493 | !isPowerOf2_32(VT.getVectorNumElements())) | |||
44494 | return SDValue(); | |||
44495 | ||||
44496 | SDValue N0 = N->getOperand(0); | |||
44497 | SDValue N1 = N->getOperand(1); | |||
44498 | ||||
44499 | // MULDQ returns the 64-bit result of the signed multiplication of the lower | |||
44500 | // 32-bits. We can lower with this if the sign bits stretch that far. | |||
44501 | if (Subtarget.hasSSE41() && DAG.ComputeNumSignBits(N0) > 32 && | |||
44502 | DAG.ComputeNumSignBits(N1) > 32) { | |||
44503 | auto PMULDQBuilder = [](SelectionDAG &DAG, const SDLoc &DL, | |||
44504 | ArrayRef<SDValue> Ops) { | |||
44505 | return DAG.getNode(X86ISD::PMULDQ, DL, Ops[0].getValueType(), Ops); | |||
44506 | }; | |||
44507 | return SplitOpsAndApply(DAG, Subtarget, SDLoc(N), VT, { N0, N1 }, | |||
44508 | PMULDQBuilder, /*CheckBWI*/false); | |||
44509 | } | |||
44510 | ||||
44511 | // If the upper bits are zero we can use a single pmuludq. | |||
44512 | APInt Mask = APInt::getHighBitsSet(64, 32); | |||
44513 | if (DAG.MaskedValueIsZero(N0, Mask) && DAG.MaskedValueIsZero(N1, Mask)) { | |||
44514 | auto PMULUDQBuilder = [](SelectionDAG &DAG, const SDLoc &DL, | |||
44515 | ArrayRef<SDValue> Ops) { | |||
44516 | return DAG.getNode(X86ISD::PMULUDQ, DL, Ops[0].getValueType(), Ops); | |||
44517 | }; | |||
44518 | return SplitOpsAndApply(DAG, Subtarget, SDLoc(N), VT, { N0, N1 }, | |||
44519 | PMULUDQBuilder, /*CheckBWI*/false); | |||
44520 | } | |||
44521 | ||||
44522 | return SDValue(); | |||
44523 | } | |||
44524 | ||||
44525 | static SDValue combineMul(SDNode *N, SelectionDAG &DAG, | |||
44526 | TargetLowering::DAGCombinerInfo &DCI, | |||
44527 | const X86Subtarget &Subtarget) { | |||
44528 | EVT VT = N->getValueType(0); | |||
44529 | ||||
44530 | if (SDValue V = combineMulToPMADDWD(N, DAG, Subtarget)) | |||
44531 | return V; | |||
44532 | ||||
44533 | if (SDValue V = combineMulToPMULDQ(N, DAG, Subtarget)) | |||
44534 | return V; | |||
44535 | ||||
44536 | if (DCI.isBeforeLegalize() && VT.isVector()) | |||
44537 | return reduceVMULWidth(N, DAG, Subtarget); | |||
44538 | ||||
44539 | // Optimize a single multiply with constant into two operations in order to | |||
44540 | // implement it with two cheaper instructions, e.g. LEA + SHL, LEA + LEA. | |||
44541 | if (!MulConstantOptimization) | |||
44542 | return SDValue(); | |||
44543 | ||||
44544 | // An imul is usually smaller than the alternative sequence. | |||
44545 | if (DAG.getMachineFunction().getFunction().hasMinSize()) | |||
44546 | return SDValue(); | |||
44547 | ||||
44548 | if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer()) | |||
44549 | return SDValue(); | |||
44550 | ||||
44551 | if (VT != MVT::i64 && VT != MVT::i32) | |||
44552 | return SDValue(); | |||
44553 | ||||
44554 | ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)); | |||
44555 | if (!C) | |||
44556 | return SDValue(); | |||
44557 | if (isPowerOf2_64(C->getZExtValue())) | |||
44558 | return SDValue(); | |||
44559 | ||||
44560 | int64_t SignMulAmt = C->getSExtValue(); | |||
44561 | assert(SignMulAmt != INT64_MIN && "Int min should have been handled!")(static_cast <bool> (SignMulAmt != (-9223372036854775807L -1) && "Int min should have been handled!") ? void ( 0) : __assert_fail ("SignMulAmt != INT64_MIN && \"Int min should have been handled!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 44561, __extension__ __PRETTY_FUNCTION__)); | |||
44562 | uint64_t AbsMulAmt = SignMulAmt < 0 ? -SignMulAmt : SignMulAmt; | |||
44563 | ||||
44564 | SDLoc DL(N); | |||
44565 | if (AbsMulAmt == 3 || AbsMulAmt == 5 || AbsMulAmt == 9) { | |||
44566 | SDValue NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0), | |||
44567 | DAG.getConstant(AbsMulAmt, DL, VT)); | |||
44568 | if (SignMulAmt < 0) | |||
44569 | NewMul = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), | |||
44570 | NewMul); | |||
44571 | ||||
44572 | return NewMul; | |||
44573 | } | |||
44574 | ||||
44575 | uint64_t MulAmt1 = 0; | |||
44576 | uint64_t MulAmt2 = 0; | |||
44577 | if ((AbsMulAmt % 9) == 0) { | |||
44578 | MulAmt1 = 9; | |||
44579 | MulAmt2 = AbsMulAmt / 9; | |||
44580 | } else if ((AbsMulAmt % 5) == 0) { | |||
44581 | MulAmt1 = 5; | |||
44582 | MulAmt2 = AbsMulAmt / 5; | |||
44583 | } else if ((AbsMulAmt % 3) == 0) { | |||
44584 | MulAmt1 = 3; | |||
44585 | MulAmt2 = AbsMulAmt / 3; | |||
44586 | } | |||
44587 | ||||
44588 | SDValue NewMul; | |||
44589 | // For negative multiply amounts, only allow MulAmt2 to be a power of 2. | |||
44590 | if (MulAmt2 && | |||
44591 | (isPowerOf2_64(MulAmt2) || | |||
44592 | (SignMulAmt >= 0 && (MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)))) { | |||
44593 | ||||
44594 | if (isPowerOf2_64(MulAmt2) && | |||
44595 | !(SignMulAmt >= 0 && N->hasOneUse() && | |||
44596 | N->use_begin()->getOpcode() == ISD::ADD)) | |||
44597 | // If second multiplifer is pow2, issue it first. We want the multiply by | |||
44598 | // 3, 5, or 9 to be folded into the addressing mode unless the lone use | |||
44599 | // is an add. Only do this for positive multiply amounts since the | |||
44600 | // negate would prevent it from being used as an address mode anyway. | |||
44601 | std::swap(MulAmt1, MulAmt2); | |||
44602 | ||||
44603 | if (isPowerOf2_64(MulAmt1)) | |||
44604 | NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0), | |||
44605 | DAG.getConstant(Log2_64(MulAmt1), DL, MVT::i8)); | |||
44606 | else | |||
44607 | NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0), | |||
44608 | DAG.getConstant(MulAmt1, DL, VT)); | |||
44609 | ||||
44610 | if (isPowerOf2_64(MulAmt2)) | |||
44611 | NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul, | |||
44612 | DAG.getConstant(Log2_64(MulAmt2), DL, MVT::i8)); | |||
44613 | else | |||
44614 | NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul, | |||
44615 | DAG.getConstant(MulAmt2, DL, VT)); | |||
44616 | ||||
44617 | // Negate the result. | |||
44618 | if (SignMulAmt < 0) | |||
44619 | NewMul = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), | |||
44620 | NewMul); | |||
44621 | } else if (!Subtarget.slowLEA()) | |||
44622 | NewMul = combineMulSpecial(C->getZExtValue(), N, DAG, VT, DL); | |||
44623 | ||||
44624 | if (!NewMul) { | |||
44625 | assert(C->getZExtValue() != 0 &&(static_cast <bool> (C->getZExtValue() != 0 && C->getZExtValue() != (VT == MVT::i64 ? (18446744073709551615UL ) : (4294967295U)) && "Both cases that could cause potential overflows should have " "already been handled.") ? void (0) : __assert_fail ("C->getZExtValue() != 0 && C->getZExtValue() != (VT == MVT::i64 ? UINT64_MAX : UINT32_MAX) && \"Both cases that could cause potential overflows should have \" \"already been handled.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 44628, __extension__ __PRETTY_FUNCTION__)) | |||
44626 | C->getZExtValue() != (VT == MVT::i64 ? UINT64_MAX : UINT32_MAX) &&(static_cast <bool> (C->getZExtValue() != 0 && C->getZExtValue() != (VT == MVT::i64 ? (18446744073709551615UL ) : (4294967295U)) && "Both cases that could cause potential overflows should have " "already been handled.") ? void (0) : __assert_fail ("C->getZExtValue() != 0 && C->getZExtValue() != (VT == MVT::i64 ? UINT64_MAX : UINT32_MAX) && \"Both cases that could cause potential overflows should have \" \"already been handled.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 44628, __extension__ __PRETTY_FUNCTION__)) | |||
44627 | "Both cases that could cause potential overflows should have "(static_cast <bool> (C->getZExtValue() != 0 && C->getZExtValue() != (VT == MVT::i64 ? (18446744073709551615UL ) : (4294967295U)) && "Both cases that could cause potential overflows should have " "already been handled.") ? void (0) : __assert_fail ("C->getZExtValue() != 0 && C->getZExtValue() != (VT == MVT::i64 ? UINT64_MAX : UINT32_MAX) && \"Both cases that could cause potential overflows should have \" \"already been handled.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 44628, __extension__ __PRETTY_FUNCTION__)) | |||
44628 | "already been handled.")(static_cast <bool> (C->getZExtValue() != 0 && C->getZExtValue() != (VT == MVT::i64 ? (18446744073709551615UL ) : (4294967295U)) && "Both cases that could cause potential overflows should have " "already been handled.") ? void (0) : __assert_fail ("C->getZExtValue() != 0 && C->getZExtValue() != (VT == MVT::i64 ? UINT64_MAX : UINT32_MAX) && \"Both cases that could cause potential overflows should have \" \"already been handled.\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 44628, __extension__ __PRETTY_FUNCTION__)); | |||
44629 | if (isPowerOf2_64(AbsMulAmt - 1)) { | |||
44630 | // (mul x, 2^N + 1) => (add (shl x, N), x) | |||
44631 | NewMul = DAG.getNode( | |||
44632 | ISD::ADD, DL, VT, N->getOperand(0), | |||
44633 | DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0), | |||
44634 | DAG.getConstant(Log2_64(AbsMulAmt - 1), DL, | |||
44635 | MVT::i8))); | |||
44636 | // To negate, subtract the number from zero | |||
44637 | if (SignMulAmt < 0) | |||
44638 | NewMul = DAG.getNode(ISD::SUB, DL, VT, | |||
44639 | DAG.getConstant(0, DL, VT), NewMul); | |||
44640 | } else if (isPowerOf2_64(AbsMulAmt + 1)) { | |||
44641 | // (mul x, 2^N - 1) => (sub (shl x, N), x) | |||
44642 | NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0), | |||
44643 | DAG.getConstant(Log2_64(AbsMulAmt + 1), | |||
44644 | DL, MVT::i8)); | |||
44645 | // To negate, reverse the operands of the subtract. | |||
44646 | if (SignMulAmt < 0) | |||
44647 | NewMul = DAG.getNode(ISD::SUB, DL, VT, N->getOperand(0), NewMul); | |||
44648 | else | |||
44649 | NewMul = DAG.getNode(ISD::SUB, DL, VT, NewMul, N->getOperand(0)); | |||
44650 | } else if (SignMulAmt >= 0 && isPowerOf2_64(AbsMulAmt - 2)) { | |||
44651 | // (mul x, 2^N + 2) => (add (add (shl x, N), x), x) | |||
44652 | NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0), | |||
44653 | DAG.getConstant(Log2_64(AbsMulAmt - 2), | |||
44654 | DL, MVT::i8)); | |||
44655 | NewMul = DAG.getNode(ISD::ADD, DL, VT, NewMul, N->getOperand(0)); | |||
44656 | NewMul = DAG.getNode(ISD::ADD, DL, VT, NewMul, N->getOperand(0)); | |||
44657 | } else if (SignMulAmt >= 0 && isPowerOf2_64(AbsMulAmt + 2)) { | |||
44658 | // (mul x, 2^N - 2) => (sub (sub (shl x, N), x), x) | |||
44659 | NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0), | |||
44660 | DAG.getConstant(Log2_64(AbsMulAmt + 2), | |||
44661 | DL, MVT::i8)); | |||
44662 | NewMul = DAG.getNode(ISD::SUB, DL, VT, NewMul, N->getOperand(0)); | |||
44663 | NewMul = DAG.getNode(ISD::SUB, DL, VT, NewMul, N->getOperand(0)); | |||
44664 | } | |||
44665 | } | |||
44666 | ||||
44667 | return NewMul; | |||
44668 | } | |||
44669 | ||||
44670 | // Try to form a MULHU or MULHS node by looking for | |||
44671 | // (srl (mul ext, ext), 16) | |||
44672 | // TODO: This is X86 specific because we want to be able to handle wide types | |||
44673 | // before type legalization. But we can only do it if the vector will be | |||
44674 | // legalized via widening/splitting. Type legalization can't handle promotion | |||
44675 | // of a MULHU/MULHS. There isn't a way to convey this to the generic DAG | |||
44676 | // combiner. | |||
44677 | static SDValue combineShiftToPMULH(SDNode *N, SelectionDAG &DAG, | |||
44678 | const X86Subtarget &Subtarget) { | |||
44679 | assert((N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) &&(static_cast <bool> ((N->getOpcode() == ISD::SRL || N ->getOpcode() == ISD::SRA) && "SRL or SRA node is required here!" ) ? void (0) : __assert_fail ("(N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) && \"SRL or SRA node is required here!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 44680, __extension__ __PRETTY_FUNCTION__)) | |||
44680 | "SRL or SRA node is required here!")(static_cast <bool> ((N->getOpcode() == ISD::SRL || N ->getOpcode() == ISD::SRA) && "SRL or SRA node is required here!" ) ? void (0) : __assert_fail ("(N->getOpcode() == ISD::SRL || N->getOpcode() == ISD::SRA) && \"SRL or SRA node is required here!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 44680, __extension__ __PRETTY_FUNCTION__)); | |||
44681 | SDLoc DL(N); | |||
44682 | ||||
44683 | if (!Subtarget.hasSSE2()) | |||
44684 | return SDValue(); | |||
44685 | ||||
44686 | // The operation feeding into the shift must be a multiply. | |||
44687 | SDValue ShiftOperand = N->getOperand(0); | |||
44688 | if (ShiftOperand.getOpcode() != ISD::MUL || !ShiftOperand.hasOneUse()) | |||
44689 | return SDValue(); | |||
44690 | ||||
44691 | // Input type should be at least vXi32. | |||
44692 | EVT VT = N->getValueType(0); | |||
44693 | if (!VT.isVector() || VT.getVectorElementType().getSizeInBits() < 32) | |||
44694 | return SDValue(); | |||
44695 | ||||
44696 | // Need a shift by 16. | |||
44697 | APInt ShiftAmt; | |||
44698 | if (!ISD::isConstantSplatVector(N->getOperand(1).getNode(), ShiftAmt) || | |||
44699 | ShiftAmt != 16) | |||
44700 | return SDValue(); | |||
44701 | ||||
44702 | SDValue LHS = ShiftOperand.getOperand(0); | |||
44703 | SDValue RHS = ShiftOperand.getOperand(1); | |||
44704 | ||||
44705 | unsigned ExtOpc = LHS.getOpcode(); | |||
44706 | if ((ExtOpc != ISD::SIGN_EXTEND && ExtOpc != ISD::ZERO_EXTEND) || | |||
44707 | RHS.getOpcode() != ExtOpc) | |||
44708 | return SDValue(); | |||
44709 | ||||
44710 | // Peek through the extends. | |||
44711 | LHS = LHS.getOperand(0); | |||
44712 | RHS = RHS.getOperand(0); | |||
44713 | ||||
44714 | // Ensure the input types match. | |||
44715 | EVT MulVT = LHS.getValueType(); | |||
44716 | if (MulVT.getVectorElementType() != MVT::i16 || RHS.getValueType() != MulVT) | |||
44717 | return SDValue(); | |||
44718 | ||||
44719 | unsigned Opc = ExtOpc == ISD::SIGN_EXTEND ? ISD::MULHS : ISD::MULHU; | |||
44720 | SDValue Mulh = DAG.getNode(Opc, DL, MulVT, LHS, RHS); | |||
44721 | ||||
44722 | ExtOpc = N->getOpcode() == ISD::SRA ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; | |||
44723 | return DAG.getNode(ExtOpc, DL, VT, Mulh); | |||
44724 | } | |||
44725 | ||||
44726 | static SDValue combineShiftLeft(SDNode *N, SelectionDAG &DAG) { | |||
44727 | SDValue N0 = N->getOperand(0); | |||
44728 | SDValue N1 = N->getOperand(1); | |||
44729 | ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1); | |||
44730 | EVT VT = N0.getValueType(); | |||
44731 | ||||
44732 | // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2)) | |||
44733 | // since the result of setcc_c is all zero's or all ones. | |||
44734 | if (VT.isInteger() && !VT.isVector() && | |||
44735 | N1C && N0.getOpcode() == ISD::AND && | |||
44736 | N0.getOperand(1).getOpcode() == ISD::Constant) { | |||
44737 | SDValue N00 = N0.getOperand(0); | |||
44738 | APInt Mask = N0.getConstantOperandAPInt(1); | |||
44739 | Mask <<= N1C->getAPIntValue(); | |||
44740 | bool MaskOK = false; | |||
44741 | // We can handle cases concerning bit-widening nodes containing setcc_c if | |||
44742 | // we carefully interrogate the mask to make sure we are semantics | |||
44743 | // preserving. | |||
44744 | // The transform is not safe if the result of C1 << C2 exceeds the bitwidth | |||
44745 | // of the underlying setcc_c operation if the setcc_c was zero extended. | |||
44746 | // Consider the following example: | |||
44747 | // zext(setcc_c) -> i32 0x0000FFFF | |||
44748 | // c1 -> i32 0x0000FFFF | |||
44749 | // c2 -> i32 0x00000001 | |||
44750 | // (shl (and (setcc_c), c1), c2) -> i32 0x0001FFFE | |||
44751 | // (and setcc_c, (c1 << c2)) -> i32 0x0000FFFE | |||
44752 | if (N00.getOpcode() == X86ISD::SETCC_CARRY) { | |||
44753 | MaskOK = true; | |||
44754 | } else if (N00.getOpcode() == ISD::SIGN_EXTEND && | |||
44755 | N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) { | |||
44756 | MaskOK = true; | |||
44757 | } else if ((N00.getOpcode() == ISD::ZERO_EXTEND || | |||
44758 | N00.getOpcode() == ISD::ANY_EXTEND) && | |||
44759 | N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) { | |||
44760 | MaskOK = Mask.isIntN(N00.getOperand(0).getValueSizeInBits()); | |||
44761 | } | |||
44762 | if (MaskOK && Mask != 0) { | |||
44763 | SDLoc DL(N); | |||
44764 | return DAG.getNode(ISD::AND, DL, VT, N00, DAG.getConstant(Mask, DL, VT)); | |||
44765 | } | |||
44766 | } | |||
44767 | ||||
44768 | // Hardware support for vector shifts is sparse which makes us scalarize the | |||
44769 | // vector operations in many cases. Also, on sandybridge ADD is faster than | |||
44770 | // shl. | |||
44771 | // (shl V, 1) -> add V,V | |||
44772 | if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1)) | |||
44773 | if (auto *N1SplatC = N1BV->getConstantSplatNode()) { | |||
44774 | assert(N0.getValueType().isVector() && "Invalid vector shift type")(static_cast <bool> (N0.getValueType().isVector() && "Invalid vector shift type") ? void (0) : __assert_fail ("N0.getValueType().isVector() && \"Invalid vector shift type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 44774, __extension__ __PRETTY_FUNCTION__)); | |||
44775 | // We shift all of the values by one. In many cases we do not have | |||
44776 | // hardware support for this operation. This is better expressed as an ADD | |||
44777 | // of two values. | |||
44778 | if (N1SplatC->isOne()) | |||
44779 | return DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, N0); | |||
44780 | } | |||
44781 | ||||
44782 | return SDValue(); | |||
44783 | } | |||
44784 | ||||
44785 | static SDValue combineShiftRightArithmetic(SDNode *N, SelectionDAG &DAG, | |||
44786 | const X86Subtarget &Subtarget) { | |||
44787 | SDValue N0 = N->getOperand(0); | |||
44788 | SDValue N1 = N->getOperand(1); | |||
44789 | EVT VT = N0.getValueType(); | |||
44790 | unsigned Size = VT.getSizeInBits(); | |||
44791 | ||||
44792 | if (SDValue V = combineShiftToPMULH(N, DAG, Subtarget)) | |||
44793 | return V; | |||
44794 | ||||
44795 | // fold (ashr (shl, a, [56,48,32,24,16]), SarConst) | |||
44796 | // into (shl, (sext (a), [56,48,32,24,16] - SarConst)) or | |||
44797 | // into (lshr, (sext (a), SarConst - [56,48,32,24,16])) | |||
44798 | // depending on sign of (SarConst - [56,48,32,24,16]) | |||
44799 | ||||
44800 | // sexts in X86 are MOVs. The MOVs have the same code size | |||
44801 | // as above SHIFTs (only SHIFT on 1 has lower code size). | |||
44802 | // However the MOVs have 2 advantages to a SHIFT: | |||
44803 | // 1. MOVs can write to a register that differs from source | |||
44804 | // 2. MOVs accept memory operands | |||
44805 | ||||
44806 | if (VT.isVector() || N1.getOpcode() != ISD::Constant || | |||
44807 | N0.getOpcode() != ISD::SHL || !N0.hasOneUse() || | |||
44808 | N0.getOperand(1).getOpcode() != ISD::Constant) | |||
44809 | return SDValue(); | |||
44810 | ||||
44811 | SDValue N00 = N0.getOperand(0); | |||
44812 | SDValue N01 = N0.getOperand(1); | |||
44813 | APInt ShlConst = (cast<ConstantSDNode>(N01))->getAPIntValue(); | |||
44814 | APInt SarConst = (cast<ConstantSDNode>(N1))->getAPIntValue(); | |||
44815 | EVT CVT = N1.getValueType(); | |||
44816 | ||||
44817 | if (SarConst.isNegative()) | |||
44818 | return SDValue(); | |||
44819 | ||||
44820 | for (MVT SVT : { MVT::i8, MVT::i16, MVT::i32 }) { | |||
44821 | unsigned ShiftSize = SVT.getSizeInBits(); | |||
44822 | // skipping types without corresponding sext/zext and | |||
44823 | // ShlConst that is not one of [56,48,32,24,16] | |||
44824 | if (ShiftSize >= Size || ShlConst != Size - ShiftSize) | |||
44825 | continue; | |||
44826 | SDLoc DL(N); | |||
44827 | SDValue NN = | |||
44828 | DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, N00, DAG.getValueType(SVT)); | |||
44829 | SarConst = SarConst - (Size - ShiftSize); | |||
44830 | if (SarConst == 0) | |||
44831 | return NN; | |||
44832 | else if (SarConst.isNegative()) | |||
44833 | return DAG.getNode(ISD::SHL, DL, VT, NN, | |||
44834 | DAG.getConstant(-SarConst, DL, CVT)); | |||
44835 | else | |||
44836 | return DAG.getNode(ISD::SRA, DL, VT, NN, | |||
44837 | DAG.getConstant(SarConst, DL, CVT)); | |||
44838 | } | |||
44839 | return SDValue(); | |||
44840 | } | |||
44841 | ||||
44842 | static SDValue combineShiftRightLogical(SDNode *N, SelectionDAG &DAG, | |||
44843 | TargetLowering::DAGCombinerInfo &DCI, | |||
44844 | const X86Subtarget &Subtarget) { | |||
44845 | SDValue N0 = N->getOperand(0); | |||
44846 | SDValue N1 = N->getOperand(1); | |||
44847 | EVT VT = N0.getValueType(); | |||
44848 | ||||
44849 | if (SDValue V = combineShiftToPMULH(N, DAG, Subtarget)) | |||
44850 | return V; | |||
44851 | ||||
44852 | // Only do this on the last DAG combine as it can interfere with other | |||
44853 | // combines. | |||
44854 | if (!DCI.isAfterLegalizeDAG()) | |||
44855 | return SDValue(); | |||
44856 | ||||
44857 | // Try to improve a sequence of srl (and X, C1), C2 by inverting the order. | |||
44858 | // TODO: This is a generic DAG combine that became an x86-only combine to | |||
44859 | // avoid shortcomings in other folds such as bswap, bit-test ('bt'), and | |||
44860 | // and-not ('andn'). | |||
44861 | if (N0.getOpcode() != ISD::AND || !N0.hasOneUse()) | |||
44862 | return SDValue(); | |||
44863 | ||||
44864 | auto *ShiftC = dyn_cast<ConstantSDNode>(N1); | |||
44865 | auto *AndC = dyn_cast<ConstantSDNode>(N0.getOperand(1)); | |||
44866 | if (!ShiftC || !AndC) | |||
44867 | return SDValue(); | |||
44868 | ||||
44869 | // If we can shrink the constant mask below 8-bits or 32-bits, then this | |||
44870 | // transform should reduce code size. It may also enable secondary transforms | |||
44871 | // from improved known-bits analysis or instruction selection. | |||
44872 | APInt MaskVal = AndC->getAPIntValue(); | |||
44873 | ||||
44874 | // If this can be matched by a zero extend, don't optimize. | |||
44875 | if (MaskVal.isMask()) { | |||
44876 | unsigned TO = MaskVal.countTrailingOnes(); | |||
44877 | if (TO >= 8 && isPowerOf2_32(TO)) | |||
44878 | return SDValue(); | |||
44879 | } | |||
44880 | ||||
44881 | APInt NewMaskVal = MaskVal.lshr(ShiftC->getAPIntValue()); | |||
44882 | unsigned OldMaskSize = MaskVal.getMinSignedBits(); | |||
44883 | unsigned NewMaskSize = NewMaskVal.getMinSignedBits(); | |||
44884 | if ((OldMaskSize > 8 && NewMaskSize <= 8) || | |||
44885 | (OldMaskSize > 32 && NewMaskSize <= 32)) { | |||
44886 | // srl (and X, AndC), ShiftC --> and (srl X, ShiftC), (AndC >> ShiftC) | |||
44887 | SDLoc DL(N); | |||
44888 | SDValue NewMask = DAG.getConstant(NewMaskVal, DL, VT); | |||
44889 | SDValue NewShift = DAG.getNode(ISD::SRL, DL, VT, N0.getOperand(0), N1); | |||
44890 | return DAG.getNode(ISD::AND, DL, VT, NewShift, NewMask); | |||
44891 | } | |||
44892 | return SDValue(); | |||
44893 | } | |||
44894 | ||||
44895 | static SDValue combineHorizOpWithShuffle(SDNode *N, SelectionDAG &DAG, | |||
44896 | const X86Subtarget &Subtarget) { | |||
44897 | unsigned Opcode = N->getOpcode(); | |||
44898 | assert(isHorizOp(Opcode) && "Unexpected hadd/hsub/pack opcode")(static_cast <bool> (isHorizOp(Opcode) && "Unexpected hadd/hsub/pack opcode" ) ? void (0) : __assert_fail ("isHorizOp(Opcode) && \"Unexpected hadd/hsub/pack opcode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 44898, __extension__ __PRETTY_FUNCTION__)); | |||
44899 | ||||
44900 | SDLoc DL(N); | |||
44901 | EVT VT = N->getValueType(0); | |||
44902 | SDValue N0 = N->getOperand(0); | |||
44903 | SDValue N1 = N->getOperand(1); | |||
44904 | EVT SrcVT = N0.getValueType(); | |||
44905 | ||||
44906 | SDValue BC0 = | |||
44907 | N->isOnlyUserOf(N0.getNode()) ? peekThroughOneUseBitcasts(N0) : N0; | |||
44908 | SDValue BC1 = | |||
44909 | N->isOnlyUserOf(N1.getNode()) ? peekThroughOneUseBitcasts(N1) : N1; | |||
44910 | ||||
44911 | // Attempt to fold HOP(LOSUBVECTOR(SHUFFLE(X)),HISUBVECTOR(SHUFFLE(X))) | |||
44912 | // to SHUFFLE(HOP(LOSUBVECTOR(X),HISUBVECTOR(X))), this is mainly for | |||
44913 | // truncation trees that help us avoid lane crossing shuffles. | |||
44914 | // TODO: There's a lot more we can do for PACK/HADD style shuffle combines. | |||
44915 | // TODO: We don't handle vXf64 shuffles yet. | |||
44916 | if (VT.is128BitVector() && SrcVT.getScalarSizeInBits() <= 32 && | |||
44917 | BC0.getOpcode() == ISD::EXTRACT_SUBVECTOR && | |||
44918 | BC1.getOpcode() == ISD::EXTRACT_SUBVECTOR && | |||
44919 | BC0.getOperand(0) == BC1.getOperand(0) && | |||
44920 | BC0.getOperand(0).getValueType().is256BitVector() && | |||
44921 | BC0.getConstantOperandAPInt(1) == 0 && | |||
44922 | BC1.getConstantOperandAPInt(1) == | |||
44923 | BC0.getValueType().getVectorNumElements()) { | |||
44924 | SmallVector<SDValue> ShuffleOps; | |||
44925 | SmallVector<int> ShuffleMask, ScaledMask; | |||
44926 | SDValue Vec = peekThroughBitcasts(BC0.getOperand(0)); | |||
44927 | if (getTargetShuffleInputs(Vec, ShuffleOps, ShuffleMask, DAG)) { | |||
44928 | resolveTargetShuffleInputsAndMask(ShuffleOps, ShuffleMask); | |||
44929 | // To keep the HOP LHS/RHS coherency, we must be able to scale the unary | |||
44930 | // shuffle to a v4X64 width - we can probably relax this in the future. | |||
44931 | if (!isAnyZero(ShuffleMask) && ShuffleOps.size() == 1 && | |||
44932 | ShuffleOps[0].getValueType().is256BitVector() && | |||
44933 | scaleShuffleElements(ShuffleMask, 4, ScaledMask)) { | |||
44934 | SDValue Lo, Hi; | |||
44935 | MVT ShufVT = VT.isFloatingPoint() ? MVT::v4f32 : MVT::v4i32; | |||
44936 | std::tie(Lo, Hi) = DAG.SplitVector(ShuffleOps[0], DL); | |||
44937 | Lo = DAG.getBitcast(SrcVT, Lo); | |||
44938 | Hi = DAG.getBitcast(SrcVT, Hi); | |||
44939 | SDValue Res = DAG.getNode(Opcode, DL, VT, Lo, Hi); | |||
44940 | Res = DAG.getBitcast(ShufVT, Res); | |||
44941 | Res = DAG.getVectorShuffle(ShufVT, DL, Res, Res, ScaledMask); | |||
44942 | return DAG.getBitcast(VT, Res); | |||
44943 | } | |||
44944 | } | |||
44945 | } | |||
44946 | ||||
44947 | // Attempt to fold HOP(SHUFFLE(X,Y),SHUFFLE(Z,W)) -> SHUFFLE(HOP()). | |||
44948 | if (VT.is128BitVector() && SrcVT.getScalarSizeInBits() <= 32) { | |||
44949 | // If either/both ops are a shuffle that can scale to v2x64, | |||
44950 | // then see if we can perform this as a v4x32 post shuffle. | |||
44951 | SmallVector<SDValue> Ops0, Ops1; | |||
44952 | SmallVector<int> Mask0, Mask1, ScaledMask0, ScaledMask1; | |||
44953 | bool IsShuf0 = | |||
44954 | getTargetShuffleInputs(BC0, Ops0, Mask0, DAG) && !isAnyZero(Mask0) && | |||
44955 | scaleShuffleElements(Mask0, 2, ScaledMask0) && | |||
44956 | all_of(Ops0, [](SDValue Op) { return Op.getValueSizeInBits() == 128; }); | |||
44957 | bool IsShuf1 = | |||
44958 | getTargetShuffleInputs(BC1, Ops1, Mask1, DAG) && !isAnyZero(Mask1) && | |||
44959 | scaleShuffleElements(Mask1, 2, ScaledMask1) && | |||
44960 | all_of(Ops1, [](SDValue Op) { return Op.getValueSizeInBits() == 128; }); | |||
44961 | if (IsShuf0 || IsShuf1) { | |||
44962 | if (!IsShuf0) { | |||
44963 | Ops0.assign({BC0}); | |||
44964 | ScaledMask0.assign({0, 1}); | |||
44965 | } | |||
44966 | if (!IsShuf1) { | |||
44967 | Ops1.assign({BC1}); | |||
44968 | ScaledMask1.assign({0, 1}); | |||
44969 | } | |||
44970 | ||||
44971 | SDValue LHS, RHS; | |||
44972 | int PostShuffle[4] = {-1, -1, -1, -1}; | |||
44973 | auto FindShuffleOpAndIdx = [&](int M, int &Idx, ArrayRef<SDValue> Ops) { | |||
44974 | if (M < 0) | |||
44975 | return true; | |||
44976 | Idx = M % 2; | |||
44977 | SDValue Src = Ops[M / 2]; | |||
44978 | if (!LHS || LHS == Src) { | |||
44979 | LHS = Src; | |||
44980 | return true; | |||
44981 | } | |||
44982 | if (!RHS || RHS == Src) { | |||
44983 | Idx += 2; | |||
44984 | RHS = Src; | |||
44985 | return true; | |||
44986 | } | |||
44987 | return false; | |||
44988 | }; | |||
44989 | if (FindShuffleOpAndIdx(ScaledMask0[0], PostShuffle[0], Ops0) && | |||
44990 | FindShuffleOpAndIdx(ScaledMask0[1], PostShuffle[1], Ops0) && | |||
44991 | FindShuffleOpAndIdx(ScaledMask1[0], PostShuffle[2], Ops1) && | |||
44992 | FindShuffleOpAndIdx(ScaledMask1[1], PostShuffle[3], Ops1)) { | |||
44993 | LHS = DAG.getBitcast(SrcVT, LHS); | |||
44994 | RHS = DAG.getBitcast(SrcVT, RHS ? RHS : LHS); | |||
44995 | MVT ShufVT = VT.isFloatingPoint() ? MVT::v4f32 : MVT::v4i32; | |||
44996 | SDValue Res = DAG.getNode(Opcode, DL, VT, LHS, RHS); | |||
44997 | Res = DAG.getBitcast(ShufVT, Res); | |||
44998 | Res = DAG.getVectorShuffle(ShufVT, DL, Res, Res, PostShuffle); | |||
44999 | return DAG.getBitcast(VT, Res); | |||
45000 | } | |||
45001 | } | |||
45002 | } | |||
45003 | ||||
45004 | // Attempt to fold HOP(SHUFFLE(X,Y),SHUFFLE(X,Y)) -> SHUFFLE(HOP(X,Y)). | |||
45005 | if (VT.is256BitVector() && Subtarget.hasInt256()) { | |||
45006 | SmallVector<int> Mask0, Mask1; | |||
45007 | SmallVector<SDValue> Ops0, Ops1; | |||
45008 | SmallVector<int, 2> ScaledMask0, ScaledMask1; | |||
45009 | if (getTargetShuffleInputs(BC0, Ops0, Mask0, DAG) && !isAnyZero(Mask0) && | |||
45010 | getTargetShuffleInputs(BC1, Ops1, Mask1, DAG) && !isAnyZero(Mask1) && | |||
45011 | !Ops0.empty() && !Ops1.empty() && | |||
45012 | all_of(Ops0, | |||
45013 | [](SDValue Op) { return Op.getValueType().is256BitVector(); }) && | |||
45014 | all_of(Ops1, | |||
45015 | [](SDValue Op) { return Op.getValueType().is256BitVector(); }) && | |||
45016 | scaleShuffleElements(Mask0, 2, ScaledMask0) && | |||
45017 | scaleShuffleElements(Mask1, 2, ScaledMask1)) { | |||
45018 | SDValue Op00 = peekThroughBitcasts(Ops0.front()); | |||
45019 | SDValue Op10 = peekThroughBitcasts(Ops1.front()); | |||
45020 | SDValue Op01 = peekThroughBitcasts(Ops0.back()); | |||
45021 | SDValue Op11 = peekThroughBitcasts(Ops1.back()); | |||
45022 | if ((Op00 == Op11) && (Op01 == Op10)) { | |||
45023 | std::swap(Op10, Op11); | |||
45024 | ShuffleVectorSDNode::commuteMask(ScaledMask1); | |||
45025 | } | |||
45026 | if ((Op00 == Op10) && (Op01 == Op11)) { | |||
45027 | const int Map[4] = {0, 2, 1, 3}; | |||
45028 | SmallVector<int, 4> ShuffleMask( | |||
45029 | {Map[ScaledMask0[0]], Map[ScaledMask1[0]], Map[ScaledMask0[1]], | |||
45030 | Map[ScaledMask1[1]]}); | |||
45031 | MVT ShufVT = VT.isFloatingPoint() ? MVT::v4f64 : MVT::v4i64; | |||
45032 | SDValue Res = DAG.getNode(Opcode, DL, VT, DAG.getBitcast(SrcVT, Op00), | |||
45033 | DAG.getBitcast(SrcVT, Op01)); | |||
45034 | Res = DAG.getBitcast(ShufVT, Res); | |||
45035 | Res = DAG.getVectorShuffle(ShufVT, DL, Res, Res, ShuffleMask); | |||
45036 | return DAG.getBitcast(VT, Res); | |||
45037 | } | |||
45038 | } | |||
45039 | } | |||
45040 | ||||
45041 | return SDValue(); | |||
45042 | } | |||
45043 | ||||
45044 | static SDValue combineVectorPack(SDNode *N, SelectionDAG &DAG, | |||
45045 | TargetLowering::DAGCombinerInfo &DCI, | |||
45046 | const X86Subtarget &Subtarget) { | |||
45047 | unsigned Opcode = N->getOpcode(); | |||
45048 | assert((X86ISD::PACKSS == Opcode || X86ISD::PACKUS == Opcode) &&(static_cast <bool> ((X86ISD::PACKSS == Opcode || X86ISD ::PACKUS == Opcode) && "Unexpected pack opcode") ? void (0) : __assert_fail ("(X86ISD::PACKSS == Opcode || X86ISD::PACKUS == Opcode) && \"Unexpected pack opcode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45049, __extension__ __PRETTY_FUNCTION__)) | |||
45049 | "Unexpected pack opcode")(static_cast <bool> ((X86ISD::PACKSS == Opcode || X86ISD ::PACKUS == Opcode) && "Unexpected pack opcode") ? void (0) : __assert_fail ("(X86ISD::PACKSS == Opcode || X86ISD::PACKUS == Opcode) && \"Unexpected pack opcode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45049, __extension__ __PRETTY_FUNCTION__)); | |||
45050 | ||||
45051 | EVT VT = N->getValueType(0); | |||
45052 | SDValue N0 = N->getOperand(0); | |||
45053 | SDValue N1 = N->getOperand(1); | |||
45054 | unsigned NumDstElts = VT.getVectorNumElements(); | |||
45055 | unsigned DstBitsPerElt = VT.getScalarSizeInBits(); | |||
45056 | unsigned SrcBitsPerElt = 2 * DstBitsPerElt; | |||
45057 | assert(N0.getScalarValueSizeInBits() == SrcBitsPerElt &&(static_cast <bool> (N0.getScalarValueSizeInBits() == SrcBitsPerElt && N1.getScalarValueSizeInBits() == SrcBitsPerElt && "Unexpected PACKSS/PACKUS input type") ? void (0) : __assert_fail ("N0.getScalarValueSizeInBits() == SrcBitsPerElt && N1.getScalarValueSizeInBits() == SrcBitsPerElt && \"Unexpected PACKSS/PACKUS input type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45059, __extension__ __PRETTY_FUNCTION__)) | |||
45058 | N1.getScalarValueSizeInBits() == SrcBitsPerElt &&(static_cast <bool> (N0.getScalarValueSizeInBits() == SrcBitsPerElt && N1.getScalarValueSizeInBits() == SrcBitsPerElt && "Unexpected PACKSS/PACKUS input type") ? void (0) : __assert_fail ("N0.getScalarValueSizeInBits() == SrcBitsPerElt && N1.getScalarValueSizeInBits() == SrcBitsPerElt && \"Unexpected PACKSS/PACKUS input type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45059, __extension__ __PRETTY_FUNCTION__)) | |||
45059 | "Unexpected PACKSS/PACKUS input type")(static_cast <bool> (N0.getScalarValueSizeInBits() == SrcBitsPerElt && N1.getScalarValueSizeInBits() == SrcBitsPerElt && "Unexpected PACKSS/PACKUS input type") ? void (0) : __assert_fail ("N0.getScalarValueSizeInBits() == SrcBitsPerElt && N1.getScalarValueSizeInBits() == SrcBitsPerElt && \"Unexpected PACKSS/PACKUS input type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45059, __extension__ __PRETTY_FUNCTION__)); | |||
45060 | ||||
45061 | bool IsSigned = (X86ISD::PACKSS == Opcode); | |||
45062 | ||||
45063 | // Constant Folding. | |||
45064 | APInt UndefElts0, UndefElts1; | |||
45065 | SmallVector<APInt, 32> EltBits0, EltBits1; | |||
45066 | if ((N0.isUndef() || N->isOnlyUserOf(N0.getNode())) && | |||
45067 | (N1.isUndef() || N->isOnlyUserOf(N1.getNode())) && | |||
45068 | getTargetConstantBitsFromNode(N0, SrcBitsPerElt, UndefElts0, EltBits0) && | |||
45069 | getTargetConstantBitsFromNode(N1, SrcBitsPerElt, UndefElts1, EltBits1)) { | |||
45070 | unsigned NumLanes = VT.getSizeInBits() / 128; | |||
45071 | unsigned NumSrcElts = NumDstElts / 2; | |||
45072 | unsigned NumDstEltsPerLane = NumDstElts / NumLanes; | |||
45073 | unsigned NumSrcEltsPerLane = NumSrcElts / NumLanes; | |||
45074 | ||||
45075 | APInt Undefs(NumDstElts, 0); | |||
45076 | SmallVector<APInt, 32> Bits(NumDstElts, APInt::getZero(DstBitsPerElt)); | |||
45077 | for (unsigned Lane = 0; Lane != NumLanes; ++Lane) { | |||
45078 | for (unsigned Elt = 0; Elt != NumDstEltsPerLane; ++Elt) { | |||
45079 | unsigned SrcIdx = Lane * NumSrcEltsPerLane + Elt % NumSrcEltsPerLane; | |||
45080 | auto &UndefElts = (Elt >= NumSrcEltsPerLane ? UndefElts1 : UndefElts0); | |||
45081 | auto &EltBits = (Elt >= NumSrcEltsPerLane ? EltBits1 : EltBits0); | |||
45082 | ||||
45083 | if (UndefElts[SrcIdx]) { | |||
45084 | Undefs.setBit(Lane * NumDstEltsPerLane + Elt); | |||
45085 | continue; | |||
45086 | } | |||
45087 | ||||
45088 | APInt &Val = EltBits[SrcIdx]; | |||
45089 | if (IsSigned) { | |||
45090 | // PACKSS: Truncate signed value with signed saturation. | |||
45091 | // Source values less than dst minint are saturated to minint. | |||
45092 | // Source values greater than dst maxint are saturated to maxint. | |||
45093 | if (Val.isSignedIntN(DstBitsPerElt)) | |||
45094 | Val = Val.trunc(DstBitsPerElt); | |||
45095 | else if (Val.isNegative()) | |||
45096 | Val = APInt::getSignedMinValue(DstBitsPerElt); | |||
45097 | else | |||
45098 | Val = APInt::getSignedMaxValue(DstBitsPerElt); | |||
45099 | } else { | |||
45100 | // PACKUS: Truncate signed value with unsigned saturation. | |||
45101 | // Source values less than zero are saturated to zero. | |||
45102 | // Source values greater than dst maxuint are saturated to maxuint. | |||
45103 | if (Val.isIntN(DstBitsPerElt)) | |||
45104 | Val = Val.trunc(DstBitsPerElt); | |||
45105 | else if (Val.isNegative()) | |||
45106 | Val = APInt::getZero(DstBitsPerElt); | |||
45107 | else | |||
45108 | Val = APInt::getAllOnes(DstBitsPerElt); | |||
45109 | } | |||
45110 | Bits[Lane * NumDstEltsPerLane + Elt] = Val; | |||
45111 | } | |||
45112 | } | |||
45113 | ||||
45114 | return getConstVector(Bits, Undefs, VT.getSimpleVT(), DAG, SDLoc(N)); | |||
45115 | } | |||
45116 | ||||
45117 | // Try to fold PACK(SHUFFLE(),SHUFFLE()) -> SHUFFLE(PACK()). | |||
45118 | if (SDValue V = combineHorizOpWithShuffle(N, DAG, Subtarget)) | |||
45119 | return V; | |||
45120 | ||||
45121 | // Try to combine a PACKUSWB/PACKSSWB implemented truncate with a regular | |||
45122 | // truncate to create a larger truncate. | |||
45123 | if (Subtarget.hasAVX512() && | |||
45124 | N0.getOpcode() == ISD::TRUNCATE && N1.isUndef() && VT == MVT::v16i8 && | |||
45125 | N0.getOperand(0).getValueType() == MVT::v8i32) { | |||
45126 | if ((IsSigned && DAG.ComputeNumSignBits(N0) > 8) || | |||
45127 | (!IsSigned && | |||
45128 | DAG.MaskedValueIsZero(N0, APInt::getHighBitsSet(16, 8)))) { | |||
45129 | if (Subtarget.hasVLX()) | |||
45130 | return DAG.getNode(X86ISD::VTRUNC, SDLoc(N), VT, N0.getOperand(0)); | |||
45131 | ||||
45132 | // Widen input to v16i32 so we can truncate that. | |||
45133 | SDLoc dl(N); | |||
45134 | SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v16i32, | |||
45135 | N0.getOperand(0), DAG.getUNDEF(MVT::v8i32)); | |||
45136 | return DAG.getNode(ISD::TRUNCATE, SDLoc(N), VT, Concat); | |||
45137 | } | |||
45138 | } | |||
45139 | ||||
45140 | // Try to fold PACK(EXTEND(X),EXTEND(Y)) -> CONCAT(X,Y) subvectors. | |||
45141 | if (VT.is128BitVector()) { | |||
45142 | unsigned ExtOpc = IsSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND; | |||
45143 | SDValue Src0, Src1; | |||
45144 | if (N0.getOpcode() == ExtOpc && | |||
45145 | N0.getOperand(0).getValueType().is64BitVector() && | |||
45146 | N0.getOperand(0).getScalarValueSizeInBits() == DstBitsPerElt) { | |||
45147 | Src0 = N0.getOperand(0); | |||
45148 | } | |||
45149 | if (N1.getOpcode() == ExtOpc && | |||
45150 | N1.getOperand(0).getValueType().is64BitVector() && | |||
45151 | N1.getOperand(0).getScalarValueSizeInBits() == DstBitsPerElt) { | |||
45152 | Src1 = N1.getOperand(0); | |||
45153 | } | |||
45154 | if ((Src0 || N0.isUndef()) && (Src1 || N1.isUndef())) { | |||
45155 | assert((Src0 || Src1) && "Found PACK(UNDEF,UNDEF)")(static_cast <bool> ((Src0 || Src1) && "Found PACK(UNDEF,UNDEF)" ) ? void (0) : __assert_fail ("(Src0 || Src1) && \"Found PACK(UNDEF,UNDEF)\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45155, __extension__ __PRETTY_FUNCTION__)); | |||
45156 | Src0 = Src0 ? Src0 : DAG.getUNDEF(Src1.getValueType()); | |||
45157 | Src1 = Src1 ? Src1 : DAG.getUNDEF(Src0.getValueType()); | |||
45158 | return DAG.getNode(ISD::CONCAT_VECTORS, SDLoc(N), VT, Src0, Src1); | |||
45159 | } | |||
45160 | ||||
45161 | // Try again with pack(*_extend_vector_inreg, undef). | |||
45162 | unsigned VecInRegOpc = IsSigned ? ISD::SIGN_EXTEND_VECTOR_INREG | |||
45163 | : ISD::ZERO_EXTEND_VECTOR_INREG; | |||
45164 | if (N0.getOpcode() == VecInRegOpc && N1.isUndef() && | |||
45165 | N0.getOperand(0).getScalarValueSizeInBits() < DstBitsPerElt) | |||
45166 | return getEXTEND_VECTOR_INREG(ExtOpc, SDLoc(N), VT, N0.getOperand(0), | |||
45167 | DAG); | |||
45168 | } | |||
45169 | ||||
45170 | // Attempt to combine as shuffle. | |||
45171 | SDValue Op(N, 0); | |||
45172 | if (SDValue Res = combineX86ShufflesRecursively(Op, DAG, Subtarget)) | |||
45173 | return Res; | |||
45174 | ||||
45175 | return SDValue(); | |||
45176 | } | |||
45177 | ||||
45178 | static SDValue combineVectorHADDSUB(SDNode *N, SelectionDAG &DAG, | |||
45179 | TargetLowering::DAGCombinerInfo &DCI, | |||
45180 | const X86Subtarget &Subtarget) { | |||
45181 | assert((X86ISD::HADD == N->getOpcode() || X86ISD::FHADD == N->getOpcode() ||(static_cast <bool> ((X86ISD::HADD == N->getOpcode() || X86ISD::FHADD == N->getOpcode() || X86ISD::HSUB == N-> getOpcode() || X86ISD::FHSUB == N->getOpcode()) && "Unexpected horizontal add/sub opcode") ? void (0) : __assert_fail ("(X86ISD::HADD == N->getOpcode() || X86ISD::FHADD == N->getOpcode() || X86ISD::HSUB == N->getOpcode() || X86ISD::FHSUB == N->getOpcode()) && \"Unexpected horizontal add/sub opcode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45183, __extension__ __PRETTY_FUNCTION__)) | |||
45182 | X86ISD::HSUB == N->getOpcode() || X86ISD::FHSUB == N->getOpcode()) &&(static_cast <bool> ((X86ISD::HADD == N->getOpcode() || X86ISD::FHADD == N->getOpcode() || X86ISD::HSUB == N-> getOpcode() || X86ISD::FHSUB == N->getOpcode()) && "Unexpected horizontal add/sub opcode") ? void (0) : __assert_fail ("(X86ISD::HADD == N->getOpcode() || X86ISD::FHADD == N->getOpcode() || X86ISD::HSUB == N->getOpcode() || X86ISD::FHSUB == N->getOpcode()) && \"Unexpected horizontal add/sub opcode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45183, __extension__ __PRETTY_FUNCTION__)) | |||
45183 | "Unexpected horizontal add/sub opcode")(static_cast <bool> ((X86ISD::HADD == N->getOpcode() || X86ISD::FHADD == N->getOpcode() || X86ISD::HSUB == N-> getOpcode() || X86ISD::FHSUB == N->getOpcode()) && "Unexpected horizontal add/sub opcode") ? void (0) : __assert_fail ("(X86ISD::HADD == N->getOpcode() || X86ISD::FHADD == N->getOpcode() || X86ISD::HSUB == N->getOpcode() || X86ISD::FHSUB == N->getOpcode()) && \"Unexpected horizontal add/sub opcode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45183, __extension__ __PRETTY_FUNCTION__)); | |||
45184 | ||||
45185 | if (!shouldUseHorizontalOp(true, DAG, Subtarget)) { | |||
45186 | MVT VT = N->getSimpleValueType(0); | |||
45187 | SDValue LHS = N->getOperand(0); | |||
45188 | SDValue RHS = N->getOperand(1); | |||
45189 | ||||
45190 | // HOP(HOP'(X,X),HOP'(Y,Y)) -> HOP(PERMUTE(HOP'(X,Y)),PERMUTE(HOP'(X,Y)). | |||
45191 | if (LHS != RHS && LHS.getOpcode() == N->getOpcode() && | |||
45192 | LHS.getOpcode() == RHS.getOpcode() && | |||
45193 | LHS.getValueType() == RHS.getValueType() && | |||
45194 | N->isOnlyUserOf(LHS.getNode()) && N->isOnlyUserOf(RHS.getNode())) { | |||
45195 | SDValue LHS0 = LHS.getOperand(0); | |||
45196 | SDValue LHS1 = LHS.getOperand(1); | |||
45197 | SDValue RHS0 = RHS.getOperand(0); | |||
45198 | SDValue RHS1 = RHS.getOperand(1); | |||
45199 | if ((LHS0 == LHS1 || LHS0.isUndef() || LHS1.isUndef()) && | |||
45200 | (RHS0 == RHS1 || RHS0.isUndef() || RHS1.isUndef())) { | |||
45201 | SDLoc DL(N); | |||
45202 | SDValue Res = DAG.getNode(LHS.getOpcode(), DL, LHS.getValueType(), | |||
45203 | LHS0.isUndef() ? LHS1 : LHS0, | |||
45204 | RHS0.isUndef() ? RHS1 : RHS0); | |||
45205 | MVT ShufVT = MVT::getVectorVT(MVT::i32, VT.getSizeInBits() / 32); | |||
45206 | Res = DAG.getBitcast(ShufVT, Res); | |||
45207 | SDValue NewLHS = | |||
45208 | DAG.getNode(X86ISD::PSHUFD, DL, ShufVT, Res, | |||
45209 | getV4X86ShuffleImm8ForMask({0, 1, 0, 1}, DL, DAG)); | |||
45210 | SDValue NewRHS = | |||
45211 | DAG.getNode(X86ISD::PSHUFD, DL, ShufVT, Res, | |||
45212 | getV4X86ShuffleImm8ForMask({2, 3, 2, 3}, DL, DAG)); | |||
45213 | return DAG.getNode(N->getOpcode(), DL, VT, DAG.getBitcast(VT, NewLHS), | |||
45214 | DAG.getBitcast(VT, NewRHS)); | |||
45215 | } | |||
45216 | } | |||
45217 | } | |||
45218 | ||||
45219 | // Try to fold HOP(SHUFFLE(),SHUFFLE()) -> SHUFFLE(HOP()). | |||
45220 | if (SDValue V = combineHorizOpWithShuffle(N, DAG, Subtarget)) | |||
45221 | return V; | |||
45222 | ||||
45223 | return SDValue(); | |||
45224 | } | |||
45225 | ||||
45226 | static SDValue combineVectorShiftVar(SDNode *N, SelectionDAG &DAG, | |||
45227 | TargetLowering::DAGCombinerInfo &DCI, | |||
45228 | const X86Subtarget &Subtarget) { | |||
45229 | assert((X86ISD::VSHL == N->getOpcode() || X86ISD::VSRA == N->getOpcode() ||(static_cast <bool> ((X86ISD::VSHL == N->getOpcode() || X86ISD::VSRA == N->getOpcode() || X86ISD::VSRL == N-> getOpcode()) && "Unexpected shift opcode") ? void (0) : __assert_fail ("(X86ISD::VSHL == N->getOpcode() || X86ISD::VSRA == N->getOpcode() || X86ISD::VSRL == N->getOpcode()) && \"Unexpected shift opcode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45231, __extension__ __PRETTY_FUNCTION__)) | |||
45230 | X86ISD::VSRL == N->getOpcode()) &&(static_cast <bool> ((X86ISD::VSHL == N->getOpcode() || X86ISD::VSRA == N->getOpcode() || X86ISD::VSRL == N-> getOpcode()) && "Unexpected shift opcode") ? void (0) : __assert_fail ("(X86ISD::VSHL == N->getOpcode() || X86ISD::VSRA == N->getOpcode() || X86ISD::VSRL == N->getOpcode()) && \"Unexpected shift opcode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45231, __extension__ __PRETTY_FUNCTION__)) | |||
45231 | "Unexpected shift opcode")(static_cast <bool> ((X86ISD::VSHL == N->getOpcode() || X86ISD::VSRA == N->getOpcode() || X86ISD::VSRL == N-> getOpcode()) && "Unexpected shift opcode") ? void (0) : __assert_fail ("(X86ISD::VSHL == N->getOpcode() || X86ISD::VSRA == N->getOpcode() || X86ISD::VSRL == N->getOpcode()) && \"Unexpected shift opcode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45231, __extension__ __PRETTY_FUNCTION__)); | |||
45232 | EVT VT = N->getValueType(0); | |||
45233 | SDValue N0 = N->getOperand(0); | |||
45234 | SDValue N1 = N->getOperand(1); | |||
45235 | ||||
45236 | // Shift zero -> zero. | |||
45237 | if (ISD::isBuildVectorAllZeros(N0.getNode())) | |||
45238 | return DAG.getConstant(0, SDLoc(N), VT); | |||
45239 | ||||
45240 | // Detect constant shift amounts. | |||
45241 | APInt UndefElts; | |||
45242 | SmallVector<APInt, 32> EltBits; | |||
45243 | if (getTargetConstantBitsFromNode(N1, 64, UndefElts, EltBits, true, false)) { | |||
45244 | unsigned X86Opc = getTargetVShiftUniformOpcode(N->getOpcode(), false); | |||
45245 | return getTargetVShiftByConstNode(X86Opc, SDLoc(N), VT.getSimpleVT(), N0, | |||
45246 | EltBits[0].getZExtValue(), DAG); | |||
45247 | } | |||
45248 | ||||
45249 | APInt KnownUndef, KnownZero; | |||
45250 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
45251 | APInt DemandedElts = APInt::getAllOnes(VT.getVectorNumElements()); | |||
45252 | if (TLI.SimplifyDemandedVectorElts(SDValue(N, 0), DemandedElts, KnownUndef, | |||
45253 | KnownZero, DCI)) | |||
45254 | return SDValue(N, 0); | |||
45255 | ||||
45256 | return SDValue(); | |||
45257 | } | |||
45258 | ||||
45259 | static SDValue combineVectorShiftImm(SDNode *N, SelectionDAG &DAG, | |||
45260 | TargetLowering::DAGCombinerInfo &DCI, | |||
45261 | const X86Subtarget &Subtarget) { | |||
45262 | unsigned Opcode = N->getOpcode(); | |||
45263 | assert((X86ISD::VSHLI == Opcode || X86ISD::VSRAI == Opcode ||(static_cast <bool> ((X86ISD::VSHLI == Opcode || X86ISD ::VSRAI == Opcode || X86ISD::VSRLI == Opcode) && "Unexpected shift opcode" ) ? void (0) : __assert_fail ("(X86ISD::VSHLI == Opcode || X86ISD::VSRAI == Opcode || X86ISD::VSRLI == Opcode) && \"Unexpected shift opcode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45265, __extension__ __PRETTY_FUNCTION__)) | |||
45264 | X86ISD::VSRLI == Opcode) &&(static_cast <bool> ((X86ISD::VSHLI == Opcode || X86ISD ::VSRAI == Opcode || X86ISD::VSRLI == Opcode) && "Unexpected shift opcode" ) ? void (0) : __assert_fail ("(X86ISD::VSHLI == Opcode || X86ISD::VSRAI == Opcode || X86ISD::VSRLI == Opcode) && \"Unexpected shift opcode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45265, __extension__ __PRETTY_FUNCTION__)) | |||
45265 | "Unexpected shift opcode")(static_cast <bool> ((X86ISD::VSHLI == Opcode || X86ISD ::VSRAI == Opcode || X86ISD::VSRLI == Opcode) && "Unexpected shift opcode" ) ? void (0) : __assert_fail ("(X86ISD::VSHLI == Opcode || X86ISD::VSRAI == Opcode || X86ISD::VSRLI == Opcode) && \"Unexpected shift opcode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45265, __extension__ __PRETTY_FUNCTION__)); | |||
45266 | bool LogicalShift = X86ISD::VSHLI == Opcode || X86ISD::VSRLI == Opcode; | |||
45267 | EVT VT = N->getValueType(0); | |||
45268 | SDValue N0 = N->getOperand(0); | |||
45269 | unsigned NumBitsPerElt = VT.getScalarSizeInBits(); | |||
45270 | assert(VT == N0.getValueType() && (NumBitsPerElt % 8) == 0 &&(static_cast <bool> (VT == N0.getValueType() && (NumBitsPerElt % 8) == 0 && "Unexpected value type") ? void (0) : __assert_fail ("VT == N0.getValueType() && (NumBitsPerElt % 8) == 0 && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45271, __extension__ __PRETTY_FUNCTION__)) | |||
45271 | "Unexpected value type")(static_cast <bool> (VT == N0.getValueType() && (NumBitsPerElt % 8) == 0 && "Unexpected value type") ? void (0) : __assert_fail ("VT == N0.getValueType() && (NumBitsPerElt % 8) == 0 && \"Unexpected value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45271, __extension__ __PRETTY_FUNCTION__)); | |||
45272 | assert(N->getOperand(1).getValueType() == MVT::i8 &&(static_cast <bool> (N->getOperand(1).getValueType() == MVT::i8 && "Unexpected shift amount type") ? void (0) : __assert_fail ("N->getOperand(1).getValueType() == MVT::i8 && \"Unexpected shift amount type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45273, __extension__ __PRETTY_FUNCTION__)) | |||
45273 | "Unexpected shift amount type")(static_cast <bool> (N->getOperand(1).getValueType() == MVT::i8 && "Unexpected shift amount type") ? void (0) : __assert_fail ("N->getOperand(1).getValueType() == MVT::i8 && \"Unexpected shift amount type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45273, __extension__ __PRETTY_FUNCTION__)); | |||
45274 | ||||
45275 | // (shift undef, X) -> 0 | |||
45276 | if (N0.isUndef()) | |||
45277 | return DAG.getConstant(0, SDLoc(N), VT); | |||
45278 | ||||
45279 | // Out of range logical bit shifts are guaranteed to be zero. | |||
45280 | // Out of range arithmetic bit shifts splat the sign bit. | |||
45281 | unsigned ShiftVal = N->getConstantOperandVal(1); | |||
45282 | if (ShiftVal >= NumBitsPerElt) { | |||
45283 | if (LogicalShift) | |||
45284 | return DAG.getConstant(0, SDLoc(N), VT); | |||
45285 | ShiftVal = NumBitsPerElt - 1; | |||
45286 | } | |||
45287 | ||||
45288 | // (shift X, 0) -> X | |||
45289 | if (!ShiftVal) | |||
45290 | return N0; | |||
45291 | ||||
45292 | // (shift 0, C) -> 0 | |||
45293 | if (ISD::isBuildVectorAllZeros(N0.getNode())) | |||
45294 | // N0 is all zeros or undef. We guarantee that the bits shifted into the | |||
45295 | // result are all zeros, not undef. | |||
45296 | return DAG.getConstant(0, SDLoc(N), VT); | |||
45297 | ||||
45298 | // (VSRAI -1, C) -> -1 | |||
45299 | if (!LogicalShift && ISD::isBuildVectorAllOnes(N0.getNode())) | |||
45300 | // N0 is all ones or undef. We guarantee that the bits shifted into the | |||
45301 | // result are all ones, not undef. | |||
45302 | return DAG.getConstant(-1, SDLoc(N), VT); | |||
45303 | ||||
45304 | // (shift (shift X, C2), C1) -> (shift X, (C1 + C2)) | |||
45305 | if (Opcode == N0.getOpcode()) { | |||
45306 | unsigned ShiftVal2 = cast<ConstantSDNode>(N0.getOperand(1))->getZExtValue(); | |||
45307 | unsigned NewShiftVal = ShiftVal + ShiftVal2; | |||
45308 | if (NewShiftVal >= NumBitsPerElt) { | |||
45309 | // Out of range logical bit shifts are guaranteed to be zero. | |||
45310 | // Out of range arithmetic bit shifts splat the sign bit. | |||
45311 | if (LogicalShift) | |||
45312 | return DAG.getConstant(0, SDLoc(N), VT); | |||
45313 | NewShiftVal = NumBitsPerElt - 1; | |||
45314 | } | |||
45315 | return DAG.getNode(Opcode, SDLoc(N), VT, N0.getOperand(0), | |||
45316 | DAG.getTargetConstant(NewShiftVal, SDLoc(N), MVT::i8)); | |||
45317 | } | |||
45318 | ||||
45319 | // We can decode 'whole byte' logical bit shifts as shuffles. | |||
45320 | if (LogicalShift && (ShiftVal % 8) == 0) { | |||
45321 | SDValue Op(N, 0); | |||
45322 | if (SDValue Res = combineX86ShufflesRecursively(Op, DAG, Subtarget)) | |||
45323 | return Res; | |||
45324 | } | |||
45325 | ||||
45326 | // Constant Folding. | |||
45327 | APInt UndefElts; | |||
45328 | SmallVector<APInt, 32> EltBits; | |||
45329 | if (N->isOnlyUserOf(N0.getNode()) && | |||
45330 | getTargetConstantBitsFromNode(N0, NumBitsPerElt, UndefElts, EltBits)) { | |||
45331 | assert(EltBits.size() == VT.getVectorNumElements() &&(static_cast <bool> (EltBits.size() == VT.getVectorNumElements () && "Unexpected shift value type") ? void (0) : __assert_fail ("EltBits.size() == VT.getVectorNumElements() && \"Unexpected shift value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45332, __extension__ __PRETTY_FUNCTION__)) | |||
45332 | "Unexpected shift value type")(static_cast <bool> (EltBits.size() == VT.getVectorNumElements () && "Unexpected shift value type") ? void (0) : __assert_fail ("EltBits.size() == VT.getVectorNumElements() && \"Unexpected shift value type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45332, __extension__ __PRETTY_FUNCTION__)); | |||
45333 | // Undef elements need to fold to 0. It's possible SimplifyDemandedBits | |||
45334 | // created an undef input due to no input bits being demanded, but user | |||
45335 | // still expects 0 in other bits. | |||
45336 | for (unsigned i = 0, e = EltBits.size(); i != e; ++i) { | |||
45337 | APInt &Elt = EltBits[i]; | |||
45338 | if (UndefElts[i]) | |||
45339 | Elt = 0; | |||
45340 | else if (X86ISD::VSHLI == Opcode) | |||
45341 | Elt <<= ShiftVal; | |||
45342 | else if (X86ISD::VSRAI == Opcode) | |||
45343 | Elt.ashrInPlace(ShiftVal); | |||
45344 | else | |||
45345 | Elt.lshrInPlace(ShiftVal); | |||
45346 | } | |||
45347 | // Reset undef elements since they were zeroed above. | |||
45348 | UndefElts = 0; | |||
45349 | return getConstVector(EltBits, UndefElts, VT.getSimpleVT(), DAG, SDLoc(N)); | |||
45350 | } | |||
45351 | ||||
45352 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
45353 | if (TLI.SimplifyDemandedBits(SDValue(N, 0), APInt::getAllOnes(NumBitsPerElt), | |||
45354 | DCI)) | |||
45355 | return SDValue(N, 0); | |||
45356 | ||||
45357 | return SDValue(); | |||
45358 | } | |||
45359 | ||||
45360 | static SDValue combineVectorInsert(SDNode *N, SelectionDAG &DAG, | |||
45361 | TargetLowering::DAGCombinerInfo &DCI, | |||
45362 | const X86Subtarget &Subtarget) { | |||
45363 | EVT VT = N->getValueType(0); | |||
45364 | assert(((N->getOpcode() == X86ISD::PINSRB && VT == MVT::v16i8) ||(static_cast <bool> (((N->getOpcode() == X86ISD::PINSRB && VT == MVT::v16i8) || (N->getOpcode() == X86ISD ::PINSRW && VT == MVT::v8i16) || N->getOpcode() == ISD::INSERT_VECTOR_ELT) && "Unexpected vector insertion" ) ? void (0) : __assert_fail ("((N->getOpcode() == X86ISD::PINSRB && VT == MVT::v16i8) || (N->getOpcode() == X86ISD::PINSRW && VT == MVT::v8i16) || N->getOpcode() == ISD::INSERT_VECTOR_ELT) && \"Unexpected vector insertion\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45367, __extension__ __PRETTY_FUNCTION__)) | |||
45365 | (N->getOpcode() == X86ISD::PINSRW && VT == MVT::v8i16) ||(static_cast <bool> (((N->getOpcode() == X86ISD::PINSRB && VT == MVT::v16i8) || (N->getOpcode() == X86ISD ::PINSRW && VT == MVT::v8i16) || N->getOpcode() == ISD::INSERT_VECTOR_ELT) && "Unexpected vector insertion" ) ? void (0) : __assert_fail ("((N->getOpcode() == X86ISD::PINSRB && VT == MVT::v16i8) || (N->getOpcode() == X86ISD::PINSRW && VT == MVT::v8i16) || N->getOpcode() == ISD::INSERT_VECTOR_ELT) && \"Unexpected vector insertion\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45367, __extension__ __PRETTY_FUNCTION__)) | |||
45366 | N->getOpcode() == ISD::INSERT_VECTOR_ELT) &&(static_cast <bool> (((N->getOpcode() == X86ISD::PINSRB && VT == MVT::v16i8) || (N->getOpcode() == X86ISD ::PINSRW && VT == MVT::v8i16) || N->getOpcode() == ISD::INSERT_VECTOR_ELT) && "Unexpected vector insertion" ) ? void (0) : __assert_fail ("((N->getOpcode() == X86ISD::PINSRB && VT == MVT::v16i8) || (N->getOpcode() == X86ISD::PINSRW && VT == MVT::v8i16) || N->getOpcode() == ISD::INSERT_VECTOR_ELT) && \"Unexpected vector insertion\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45367, __extension__ __PRETTY_FUNCTION__)) | |||
45367 | "Unexpected vector insertion")(static_cast <bool> (((N->getOpcode() == X86ISD::PINSRB && VT == MVT::v16i8) || (N->getOpcode() == X86ISD ::PINSRW && VT == MVT::v8i16) || N->getOpcode() == ISD::INSERT_VECTOR_ELT) && "Unexpected vector insertion" ) ? void (0) : __assert_fail ("((N->getOpcode() == X86ISD::PINSRB && VT == MVT::v16i8) || (N->getOpcode() == X86ISD::PINSRW && VT == MVT::v8i16) || N->getOpcode() == ISD::INSERT_VECTOR_ELT) && \"Unexpected vector insertion\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45367, __extension__ __PRETTY_FUNCTION__)); | |||
45368 | ||||
45369 | if (N->getOpcode() == X86ISD::PINSRB || N->getOpcode() == X86ISD::PINSRW) { | |||
45370 | unsigned NumBitsPerElt = VT.getScalarSizeInBits(); | |||
45371 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
45372 | if (TLI.SimplifyDemandedBits(SDValue(N, 0), | |||
45373 | APInt::getAllOnes(NumBitsPerElt), DCI)) | |||
45374 | return SDValue(N, 0); | |||
45375 | } | |||
45376 | ||||
45377 | // Attempt to combine insertion patterns to a shuffle. | |||
45378 | if (VT.isSimple() && DCI.isAfterLegalizeDAG()) { | |||
45379 | SDValue Op(N, 0); | |||
45380 | if (SDValue Res = combineX86ShufflesRecursively(Op, DAG, Subtarget)) | |||
45381 | return Res; | |||
45382 | } | |||
45383 | ||||
45384 | return SDValue(); | |||
45385 | } | |||
45386 | ||||
45387 | /// Recognize the distinctive (AND (setcc ...) (setcc ..)) where both setccs | |||
45388 | /// reference the same FP CMP, and rewrite for CMPEQSS and friends. Likewise for | |||
45389 | /// OR -> CMPNEQSS. | |||
45390 | static SDValue combineCompareEqual(SDNode *N, SelectionDAG &DAG, | |||
45391 | TargetLowering::DAGCombinerInfo &DCI, | |||
45392 | const X86Subtarget &Subtarget) { | |||
45393 | unsigned opcode; | |||
45394 | ||||
45395 | // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but | |||
45396 | // we're requiring SSE2 for both. | |||
45397 | if (Subtarget.hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) { | |||
45398 | SDValue N0 = N->getOperand(0); | |||
45399 | SDValue N1 = N->getOperand(1); | |||
45400 | SDValue CMP0 = N0.getOperand(1); | |||
45401 | SDValue CMP1 = N1.getOperand(1); | |||
45402 | SDLoc DL(N); | |||
45403 | ||||
45404 | // The SETCCs should both refer to the same CMP. | |||
45405 | if (CMP0.getOpcode() != X86ISD::FCMP || CMP0 != CMP1) | |||
45406 | return SDValue(); | |||
45407 | ||||
45408 | SDValue CMP00 = CMP0->getOperand(0); | |||
45409 | SDValue CMP01 = CMP0->getOperand(1); | |||
45410 | EVT VT = CMP00.getValueType(); | |||
45411 | ||||
45412 | if (VT == MVT::f32 || VT == MVT::f64 || | |||
45413 | (VT == MVT::f16 && Subtarget.hasFP16())) { | |||
45414 | bool ExpectingFlags = false; | |||
45415 | // Check for any users that want flags: | |||
45416 | for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end(); | |||
45417 | !ExpectingFlags && UI != UE; ++UI) | |||
45418 | switch (UI->getOpcode()) { | |||
45419 | default: | |||
45420 | case ISD::BR_CC: | |||
45421 | case ISD::BRCOND: | |||
45422 | case ISD::SELECT: | |||
45423 | ExpectingFlags = true; | |||
45424 | break; | |||
45425 | case ISD::CopyToReg: | |||
45426 | case ISD::SIGN_EXTEND: | |||
45427 | case ISD::ZERO_EXTEND: | |||
45428 | case ISD::ANY_EXTEND: | |||
45429 | break; | |||
45430 | } | |||
45431 | ||||
45432 | if (!ExpectingFlags) { | |||
45433 | enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0); | |||
45434 | enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0); | |||
45435 | ||||
45436 | if (cc1 == X86::COND_E || cc1 == X86::COND_NE) { | |||
45437 | X86::CondCode tmp = cc0; | |||
45438 | cc0 = cc1; | |||
45439 | cc1 = tmp; | |||
45440 | } | |||
45441 | ||||
45442 | if ((cc0 == X86::COND_E && cc1 == X86::COND_NP) || | |||
45443 | (cc0 == X86::COND_NE && cc1 == X86::COND_P)) { | |||
45444 | // FIXME: need symbolic constants for these magic numbers. | |||
45445 | // See X86ATTInstPrinter.cpp:printSSECC(). | |||
45446 | unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4; | |||
45447 | if (Subtarget.hasAVX512()) { | |||
45448 | SDValue FSetCC = | |||
45449 | DAG.getNode(X86ISD::FSETCCM, DL, MVT::v1i1, CMP00, CMP01, | |||
45450 | DAG.getTargetConstant(x86cc, DL, MVT::i8)); | |||
45451 | // Need to fill with zeros to ensure the bitcast will produce zeroes | |||
45452 | // for the upper bits. An EXTRACT_ELEMENT here wouldn't guarantee that. | |||
45453 | SDValue Ins = DAG.getNode(ISD::INSERT_SUBVECTOR, DL, MVT::v16i1, | |||
45454 | DAG.getConstant(0, DL, MVT::v16i1), | |||
45455 | FSetCC, DAG.getIntPtrConstant(0, DL)); | |||
45456 | return DAG.getZExtOrTrunc(DAG.getBitcast(MVT::i16, Ins), DL, | |||
45457 | N->getSimpleValueType(0)); | |||
45458 | } | |||
45459 | SDValue OnesOrZeroesF = | |||
45460 | DAG.getNode(X86ISD::FSETCC, DL, CMP00.getValueType(), CMP00, | |||
45461 | CMP01, DAG.getTargetConstant(x86cc, DL, MVT::i8)); | |||
45462 | ||||
45463 | bool is64BitFP = (CMP00.getValueType() == MVT::f64); | |||
45464 | MVT IntVT = is64BitFP ? MVT::i64 : MVT::i32; | |||
45465 | ||||
45466 | if (is64BitFP && !Subtarget.is64Bit()) { | |||
45467 | // On a 32-bit target, we cannot bitcast the 64-bit float to a | |||
45468 | // 64-bit integer, since that's not a legal type. Since | |||
45469 | // OnesOrZeroesF is all ones of all zeroes, we don't need all the | |||
45470 | // bits, but can do this little dance to extract the lowest 32 bits | |||
45471 | // and work with those going forward. | |||
45472 | SDValue Vector64 = DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, MVT::v2f64, | |||
45473 | OnesOrZeroesF); | |||
45474 | SDValue Vector32 = DAG.getBitcast(MVT::v4f32, Vector64); | |||
45475 | OnesOrZeroesF = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32, | |||
45476 | Vector32, DAG.getIntPtrConstant(0, DL)); | |||
45477 | IntVT = MVT::i32; | |||
45478 | } | |||
45479 | ||||
45480 | SDValue OnesOrZeroesI = DAG.getBitcast(IntVT, OnesOrZeroesF); | |||
45481 | SDValue ANDed = DAG.getNode(ISD::AND, DL, IntVT, OnesOrZeroesI, | |||
45482 | DAG.getConstant(1, DL, IntVT)); | |||
45483 | SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, | |||
45484 | ANDed); | |||
45485 | return OneBitOfTruth; | |||
45486 | } | |||
45487 | } | |||
45488 | } | |||
45489 | } | |||
45490 | return SDValue(); | |||
45491 | } | |||
45492 | ||||
45493 | /// Try to fold: (and (xor X, -1), Y) -> (andnp X, Y). | |||
45494 | static SDValue combineANDXORWithAllOnesIntoANDNP(SDNode *N, SelectionDAG &DAG) { | |||
45495 | assert(N->getOpcode() == ISD::AND)(static_cast <bool> (N->getOpcode() == ISD::AND) ? void (0) : __assert_fail ("N->getOpcode() == ISD::AND", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45495, __extension__ __PRETTY_FUNCTION__)); | |||
45496 | ||||
45497 | MVT VT = N->getSimpleValueType(0); | |||
45498 | if (!VT.is128BitVector() && !VT.is256BitVector() && !VT.is512BitVector()) | |||
45499 | return SDValue(); | |||
45500 | ||||
45501 | SDValue X, Y; | |||
45502 | SDValue N0 = N->getOperand(0); | |||
45503 | SDValue N1 = N->getOperand(1); | |||
45504 | ||||
45505 | auto GetNot = [&VT, &DAG](SDValue V) { | |||
45506 | // Basic X = NOT(Y) detection. | |||
45507 | if (SDValue Not = IsNOT(V, DAG)) | |||
45508 | return Not; | |||
45509 | // Fold BROADCAST(NOT(Y)) -> BROADCAST(Y). | |||
45510 | if (V.getOpcode() == X86ISD::VBROADCAST) { | |||
45511 | SDValue Src = V.getOperand(0); | |||
45512 | EVT SrcVT = Src.getValueType(); | |||
45513 | if (!SrcVT.isVector()) | |||
45514 | return SDValue(); | |||
45515 | if (SDValue Not = IsNOT(Src, DAG)) | |||
45516 | return DAG.getNode(X86ISD::VBROADCAST, SDLoc(V), VT, | |||
45517 | DAG.getBitcast(SrcVT, Not)); | |||
45518 | } | |||
45519 | return SDValue(); | |||
45520 | }; | |||
45521 | ||||
45522 | if (SDValue Not = GetNot(N0)) { | |||
45523 | X = Not; | |||
45524 | Y = N1; | |||
45525 | } else if (SDValue Not = GetNot(N1)) { | |||
45526 | X = Not; | |||
45527 | Y = N0; | |||
45528 | } else | |||
45529 | return SDValue(); | |||
45530 | ||||
45531 | X = DAG.getBitcast(VT, X); | |||
45532 | Y = DAG.getBitcast(VT, Y); | |||
45533 | return DAG.getNode(X86ISD::ANDNP, SDLoc(N), VT, X, Y); | |||
45534 | } | |||
45535 | ||||
45536 | // Try to widen AND, OR and XOR nodes to VT in order to remove casts around | |||
45537 | // logical operations, like in the example below. | |||
45538 | // or (and (truncate x, truncate y)), | |||
45539 | // (xor (truncate z, build_vector (constants))) | |||
45540 | // Given a target type \p VT, we generate | |||
45541 | // or (and x, y), (xor z, zext(build_vector (constants))) | |||
45542 | // given x, y and z are of type \p VT. We can do so, if operands are either | |||
45543 | // truncates from VT types, the second operand is a vector of constants or can | |||
45544 | // be recursively promoted. | |||
45545 | static SDValue PromoteMaskArithmetic(SDNode *N, EVT VT, SelectionDAG &DAG, | |||
45546 | unsigned Depth) { | |||
45547 | // Limit recursion to avoid excessive compile times. | |||
45548 | if (Depth >= SelectionDAG::MaxRecursionDepth) | |||
45549 | return SDValue(); | |||
45550 | ||||
45551 | if (N->getOpcode() != ISD::XOR && N->getOpcode() != ISD::AND && | |||
45552 | N->getOpcode() != ISD::OR) | |||
45553 | return SDValue(); | |||
45554 | ||||
45555 | SDValue N0 = N->getOperand(0); | |||
45556 | SDValue N1 = N->getOperand(1); | |||
45557 | SDLoc DL(N); | |||
45558 | ||||
45559 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
45560 | if (!TLI.isOperationLegalOrPromote(N->getOpcode(), VT)) | |||
45561 | return SDValue(); | |||
45562 | ||||
45563 | if (SDValue NN0 = PromoteMaskArithmetic(N0.getNode(), VT, DAG, Depth + 1)) | |||
45564 | N0 = NN0; | |||
45565 | else { | |||
45566 | // The Left side has to be a trunc. | |||
45567 | if (N0.getOpcode() != ISD::TRUNCATE) | |||
45568 | return SDValue(); | |||
45569 | ||||
45570 | // The type of the truncated inputs. | |||
45571 | if (N0.getOperand(0).getValueType() != VT) | |||
45572 | return SDValue(); | |||
45573 | ||||
45574 | N0 = N0.getOperand(0); | |||
45575 | } | |||
45576 | ||||
45577 | if (SDValue NN1 = PromoteMaskArithmetic(N1.getNode(), VT, DAG, Depth + 1)) | |||
45578 | N1 = NN1; | |||
45579 | else { | |||
45580 | // The right side has to be a 'trunc' or a constant vector. | |||
45581 | bool RHSTrunc = N1.getOpcode() == ISD::TRUNCATE && | |||
45582 | N1.getOperand(0).getValueType() == VT; | |||
45583 | if (!RHSTrunc && !ISD::isBuildVectorOfConstantSDNodes(N1.getNode())) | |||
45584 | return SDValue(); | |||
45585 | ||||
45586 | if (RHSTrunc) | |||
45587 | N1 = N1.getOperand(0); | |||
45588 | else | |||
45589 | N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, VT, N1); | |||
45590 | } | |||
45591 | ||||
45592 | return DAG.getNode(N->getOpcode(), DL, VT, N0, N1); | |||
45593 | } | |||
45594 | ||||
45595 | // On AVX/AVX2 the type v8i1 is legalized to v8i16, which is an XMM sized | |||
45596 | // register. In most cases we actually compare or select YMM-sized registers | |||
45597 | // and mixing the two types creates horrible code. This method optimizes | |||
45598 | // some of the transition sequences. | |||
45599 | // Even with AVX-512 this is still useful for removing casts around logical | |||
45600 | // operations on vXi1 mask types. | |||
45601 | static SDValue PromoteMaskArithmetic(SDNode *N, SelectionDAG &DAG, | |||
45602 | const X86Subtarget &Subtarget) { | |||
45603 | EVT VT = N->getValueType(0); | |||
45604 | assert(VT.isVector() && "Expected vector type")(static_cast <bool> (VT.isVector() && "Expected vector type" ) ? void (0) : __assert_fail ("VT.isVector() && \"Expected vector type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45604, __extension__ __PRETTY_FUNCTION__)); | |||
45605 | ||||
45606 | SDLoc DL(N); | |||
45607 | assert((N->getOpcode() == ISD::ANY_EXTEND ||(static_cast <bool> ((N->getOpcode() == ISD::ANY_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND || N->getOpcode( ) == ISD::SIGN_EXTEND) && "Invalid Node") ? void (0) : __assert_fail ("(N->getOpcode() == ISD::ANY_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND || N->getOpcode() == ISD::SIGN_EXTEND) && \"Invalid Node\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45609, __extension__ __PRETTY_FUNCTION__)) | |||
45608 | N->getOpcode() == ISD::ZERO_EXTEND ||(static_cast <bool> ((N->getOpcode() == ISD::ANY_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND || N->getOpcode( ) == ISD::SIGN_EXTEND) && "Invalid Node") ? void (0) : __assert_fail ("(N->getOpcode() == ISD::ANY_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND || N->getOpcode() == ISD::SIGN_EXTEND) && \"Invalid Node\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45609, __extension__ __PRETTY_FUNCTION__)) | |||
45609 | N->getOpcode() == ISD::SIGN_EXTEND) && "Invalid Node")(static_cast <bool> ((N->getOpcode() == ISD::ANY_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND || N->getOpcode( ) == ISD::SIGN_EXTEND) && "Invalid Node") ? void (0) : __assert_fail ("(N->getOpcode() == ISD::ANY_EXTEND || N->getOpcode() == ISD::ZERO_EXTEND || N->getOpcode() == ISD::SIGN_EXTEND) && \"Invalid Node\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45609, __extension__ __PRETTY_FUNCTION__)); | |||
45610 | ||||
45611 | SDValue Narrow = N->getOperand(0); | |||
45612 | EVT NarrowVT = Narrow.getValueType(); | |||
45613 | ||||
45614 | // Generate the wide operation. | |||
45615 | SDValue Op = PromoteMaskArithmetic(Narrow.getNode(), VT, DAG, 0); | |||
45616 | if (!Op) | |||
45617 | return SDValue(); | |||
45618 | switch (N->getOpcode()) { | |||
45619 | default: llvm_unreachable("Unexpected opcode")::llvm::llvm_unreachable_internal("Unexpected opcode", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45619); | |||
45620 | case ISD::ANY_EXTEND: | |||
45621 | return Op; | |||
45622 | case ISD::ZERO_EXTEND: | |||
45623 | return DAG.getZeroExtendInReg(Op, DL, NarrowVT); | |||
45624 | case ISD::SIGN_EXTEND: | |||
45625 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, | |||
45626 | Op, DAG.getValueType(NarrowVT)); | |||
45627 | } | |||
45628 | } | |||
45629 | ||||
45630 | static unsigned convertIntLogicToFPLogicOpcode(unsigned Opcode) { | |||
45631 | unsigned FPOpcode; | |||
45632 | switch (Opcode) { | |||
45633 | default: llvm_unreachable("Unexpected input node for FP logic conversion")::llvm::llvm_unreachable_internal("Unexpected input node for FP logic conversion" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45633); | |||
45634 | case ISD::AND: FPOpcode = X86ISD::FAND; break; | |||
45635 | case ISD::OR: FPOpcode = X86ISD::FOR; break; | |||
45636 | case ISD::XOR: FPOpcode = X86ISD::FXOR; break; | |||
45637 | } | |||
45638 | return FPOpcode; | |||
45639 | } | |||
45640 | ||||
45641 | /// If both input operands of a logic op are being cast from floating-point | |||
45642 | /// types or FP compares, try to convert this into a floating-point logic node | |||
45643 | /// to avoid unnecessary moves from SSE to integer registers. | |||
45644 | static SDValue convertIntLogicToFPLogic(SDNode *N, SelectionDAG &DAG, | |||
45645 | TargetLowering::DAGCombinerInfo &DCI, | |||
45646 | const X86Subtarget &Subtarget) { | |||
45647 | EVT VT = N->getValueType(0); | |||
45648 | SDValue N0 = N->getOperand(0); | |||
45649 | SDValue N1 = N->getOperand(1); | |||
45650 | SDLoc DL(N); | |||
45651 | ||||
45652 | if (!((N0.getOpcode() == ISD::BITCAST && N1.getOpcode() == ISD::BITCAST) || | |||
45653 | (N0.getOpcode() == ISD::SETCC && N1.getOpcode() == ISD::SETCC))) | |||
45654 | return SDValue(); | |||
45655 | ||||
45656 | SDValue N00 = N0.getOperand(0); | |||
45657 | SDValue N10 = N1.getOperand(0); | |||
45658 | EVT N00Type = N00.getValueType(); | |||
45659 | EVT N10Type = N10.getValueType(); | |||
45660 | ||||
45661 | // Ensure that both types are the same and are legal scalar fp types. | |||
45662 | if (N00Type != N10Type || !((Subtarget.hasSSE1() && N00Type == MVT::f32) || | |||
45663 | (Subtarget.hasSSE2() && N00Type == MVT::f64) || | |||
45664 | (Subtarget.hasFP16() && N00Type == MVT::f16))) | |||
45665 | return SDValue(); | |||
45666 | ||||
45667 | if (N0.getOpcode() == ISD::BITCAST && !DCI.isBeforeLegalizeOps()) { | |||
45668 | unsigned FPOpcode = convertIntLogicToFPLogicOpcode(N->getOpcode()); | |||
45669 | SDValue FPLogic = DAG.getNode(FPOpcode, DL, N00Type, N00, N10); | |||
45670 | return DAG.getBitcast(VT, FPLogic); | |||
45671 | } | |||
45672 | ||||
45673 | // The vector ISA for FP predicates is incomplete before AVX, so converting | |||
45674 | // COMIS* to CMPS* may not be a win before AVX. | |||
45675 | // TODO: Check types/predicates to see if they are available with SSE/SSE2. | |||
45676 | if (!Subtarget.hasAVX() || VT != MVT::i1 || N0.getOpcode() != ISD::SETCC || | |||
45677 | !N0.hasOneUse() || !N1.hasOneUse()) | |||
45678 | return SDValue(); | |||
45679 | ||||
45680 | // Convert scalar FP compares and logic to vector compares (COMIS* to CMPS*) | |||
45681 | // and vector logic: | |||
45682 | // logic (setcc N00, N01), (setcc N10, N11) --> | |||
45683 | // extelt (logic (setcc (s2v N00), (s2v N01)), setcc (s2v N10), (s2v N11))), 0 | |||
45684 | unsigned NumElts = 128 / N00Type.getSizeInBits(); | |||
45685 | EVT VecVT = EVT::getVectorVT(*DAG.getContext(), N00Type, NumElts); | |||
45686 | EVT BoolVecVT = EVT::getVectorVT(*DAG.getContext(), MVT::i1, NumElts); | |||
45687 | SDValue ZeroIndex = DAG.getVectorIdxConstant(0, DL); | |||
45688 | SDValue N01 = N0.getOperand(1); | |||
45689 | SDValue N11 = N1.getOperand(1); | |||
45690 | SDValue Vec00 = DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VecVT, N00); | |||
45691 | SDValue Vec01 = DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VecVT, N01); | |||
45692 | SDValue Vec10 = DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VecVT, N10); | |||
45693 | SDValue Vec11 = DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VecVT, N11); | |||
45694 | SDValue Setcc0 = DAG.getSetCC(DL, BoolVecVT, Vec00, Vec01, | |||
45695 | cast<CondCodeSDNode>(N0.getOperand(2))->get()); | |||
45696 | SDValue Setcc1 = DAG.getSetCC(DL, BoolVecVT, Vec10, Vec11, | |||
45697 | cast<CondCodeSDNode>(N1.getOperand(2))->get()); | |||
45698 | SDValue Logic = DAG.getNode(N->getOpcode(), DL, BoolVecVT, Setcc0, Setcc1); | |||
45699 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT, Logic, ZeroIndex); | |||
45700 | } | |||
45701 | ||||
45702 | // Attempt to fold BITOP(MOVMSK(X),MOVMSK(Y)) -> MOVMSK(BITOP(X,Y)) | |||
45703 | // to reduce XMM->GPR traffic. | |||
45704 | static SDValue combineBitOpWithMOVMSK(SDNode *N, SelectionDAG &DAG) { | |||
45705 | unsigned Opc = N->getOpcode(); | |||
45706 | assert((Opc == ISD::OR || Opc == ISD::AND || Opc == ISD::XOR) &&(static_cast <bool> ((Opc == ISD::OR || Opc == ISD::AND || Opc == ISD::XOR) && "Unexpected bit opcode") ? void (0) : __assert_fail ("(Opc == ISD::OR || Opc == ISD::AND || Opc == ISD::XOR) && \"Unexpected bit opcode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45707, __extension__ __PRETTY_FUNCTION__)) | |||
45707 | "Unexpected bit opcode")(static_cast <bool> ((Opc == ISD::OR || Opc == ISD::AND || Opc == ISD::XOR) && "Unexpected bit opcode") ? void (0) : __assert_fail ("(Opc == ISD::OR || Opc == ISD::AND || Opc == ISD::XOR) && \"Unexpected bit opcode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45707, __extension__ __PRETTY_FUNCTION__)); | |||
45708 | ||||
45709 | SDValue N0 = N->getOperand(0); | |||
45710 | SDValue N1 = N->getOperand(1); | |||
45711 | ||||
45712 | // Both operands must be single use MOVMSK. | |||
45713 | if (N0.getOpcode() != X86ISD::MOVMSK || !N0.hasOneUse() || | |||
45714 | N1.getOpcode() != X86ISD::MOVMSK || !N1.hasOneUse()) | |||
45715 | return SDValue(); | |||
45716 | ||||
45717 | SDValue Vec0 = N0.getOperand(0); | |||
45718 | SDValue Vec1 = N1.getOperand(0); | |||
45719 | EVT VecVT0 = Vec0.getValueType(); | |||
45720 | EVT VecVT1 = Vec1.getValueType(); | |||
45721 | ||||
45722 | // Both MOVMSK operands must be from vectors of the same size and same element | |||
45723 | // size, but its OK for a fp/int diff. | |||
45724 | if (VecVT0.getSizeInBits() != VecVT1.getSizeInBits() || | |||
45725 | VecVT0.getScalarSizeInBits() != VecVT1.getScalarSizeInBits()) | |||
45726 | return SDValue(); | |||
45727 | ||||
45728 | SDLoc DL(N); | |||
45729 | unsigned VecOpc = | |||
45730 | VecVT0.isFloatingPoint() ? convertIntLogicToFPLogicOpcode(Opc) : Opc; | |||
45731 | SDValue Result = | |||
45732 | DAG.getNode(VecOpc, DL, VecVT0, Vec0, DAG.getBitcast(VecVT0, Vec1)); | |||
45733 | return DAG.getNode(X86ISD::MOVMSK, DL, MVT::i32, Result); | |||
45734 | } | |||
45735 | ||||
45736 | /// If this is a zero/all-bits result that is bitwise-anded with a low bits | |||
45737 | /// mask. (Mask == 1 for the x86 lowering of a SETCC + ZEXT), replace the 'and' | |||
45738 | /// with a shift-right to eliminate loading the vector constant mask value. | |||
45739 | static SDValue combineAndMaskToShift(SDNode *N, SelectionDAG &DAG, | |||
45740 | const X86Subtarget &Subtarget) { | |||
45741 | SDValue Op0 = peekThroughBitcasts(N->getOperand(0)); | |||
45742 | SDValue Op1 = peekThroughBitcasts(N->getOperand(1)); | |||
45743 | EVT VT0 = Op0.getValueType(); | |||
45744 | EVT VT1 = Op1.getValueType(); | |||
45745 | ||||
45746 | if (VT0 != VT1 || !VT0.isSimple() || !VT0.isInteger()) | |||
45747 | return SDValue(); | |||
45748 | ||||
45749 | APInt SplatVal; | |||
45750 | if (!ISD::isConstantSplatVector(Op1.getNode(), SplatVal) || | |||
45751 | !SplatVal.isMask()) | |||
45752 | return SDValue(); | |||
45753 | ||||
45754 | // Don't prevent creation of ANDN. | |||
45755 | if (isBitwiseNot(Op0)) | |||
45756 | return SDValue(); | |||
45757 | ||||
45758 | if (!SupportedVectorShiftWithImm(VT0.getSimpleVT(), Subtarget, ISD::SRL)) | |||
45759 | return SDValue(); | |||
45760 | ||||
45761 | unsigned EltBitWidth = VT0.getScalarSizeInBits(); | |||
45762 | if (EltBitWidth != DAG.ComputeNumSignBits(Op0)) | |||
45763 | return SDValue(); | |||
45764 | ||||
45765 | SDLoc DL(N); | |||
45766 | unsigned ShiftVal = SplatVal.countTrailingOnes(); | |||
45767 | SDValue ShAmt = DAG.getTargetConstant(EltBitWidth - ShiftVal, DL, MVT::i8); | |||
45768 | SDValue Shift = DAG.getNode(X86ISD::VSRLI, DL, VT0, Op0, ShAmt); | |||
45769 | return DAG.getBitcast(N->getValueType(0), Shift); | |||
45770 | } | |||
45771 | ||||
45772 | // Get the index node from the lowered DAG of a GEP IR instruction with one | |||
45773 | // indexing dimension. | |||
45774 | static SDValue getIndexFromUnindexedLoad(LoadSDNode *Ld) { | |||
45775 | if (Ld->isIndexed()) | |||
45776 | return SDValue(); | |||
45777 | ||||
45778 | SDValue Base = Ld->getBasePtr(); | |||
45779 | ||||
45780 | if (Base.getOpcode() != ISD::ADD) | |||
45781 | return SDValue(); | |||
45782 | ||||
45783 | SDValue ShiftedIndex = Base.getOperand(0); | |||
45784 | ||||
45785 | if (ShiftedIndex.getOpcode() != ISD::SHL) | |||
45786 | return SDValue(); | |||
45787 | ||||
45788 | return ShiftedIndex.getOperand(0); | |||
45789 | ||||
45790 | } | |||
45791 | ||||
45792 | static bool hasBZHI(const X86Subtarget &Subtarget, MVT VT) { | |||
45793 | if (Subtarget.hasBMI2() && VT.isScalarInteger()) { | |||
45794 | switch (VT.getSizeInBits()) { | |||
45795 | default: return false; | |||
45796 | case 64: return Subtarget.is64Bit() ? true : false; | |||
45797 | case 32: return true; | |||
45798 | } | |||
45799 | } | |||
45800 | return false; | |||
45801 | } | |||
45802 | ||||
45803 | // This function recognizes cases where X86 bzhi instruction can replace and | |||
45804 | // 'and-load' sequence. | |||
45805 | // In case of loading integer value from an array of constants which is defined | |||
45806 | // as follows: | |||
45807 | // | |||
45808 | // int array[SIZE] = {0x0, 0x1, 0x3, 0x7, 0xF ..., 2^(SIZE-1) - 1} | |||
45809 | // | |||
45810 | // then applying a bitwise and on the result with another input. | |||
45811 | // It's equivalent to performing bzhi (zero high bits) on the input, with the | |||
45812 | // same index of the load. | |||
45813 | static SDValue combineAndLoadToBZHI(SDNode *Node, SelectionDAG &DAG, | |||
45814 | const X86Subtarget &Subtarget) { | |||
45815 | MVT VT = Node->getSimpleValueType(0); | |||
45816 | SDLoc dl(Node); | |||
45817 | ||||
45818 | // Check if subtarget has BZHI instruction for the node's type | |||
45819 | if (!hasBZHI(Subtarget, VT)) | |||
45820 | return SDValue(); | |||
45821 | ||||
45822 | // Try matching the pattern for both operands. | |||
45823 | for (unsigned i = 0; i < 2; i++) { | |||
45824 | SDValue N = Node->getOperand(i); | |||
45825 | LoadSDNode *Ld = dyn_cast<LoadSDNode>(N.getNode()); | |||
45826 | ||||
45827 | // continue if the operand is not a load instruction | |||
45828 | if (!Ld) | |||
45829 | return SDValue(); | |||
45830 | ||||
45831 | const Value *MemOp = Ld->getMemOperand()->getValue(); | |||
45832 | ||||
45833 | if (!MemOp) | |||
45834 | return SDValue(); | |||
45835 | ||||
45836 | if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(MemOp)) { | |||
45837 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0))) { | |||
45838 | if (GV->isConstant() && GV->hasDefinitiveInitializer()) { | |||
45839 | ||||
45840 | Constant *Init = GV->getInitializer(); | |||
45841 | Type *Ty = Init->getType(); | |||
45842 | if (!isa<ConstantDataArray>(Init) || | |||
45843 | !Ty->getArrayElementType()->isIntegerTy() || | |||
45844 | Ty->getArrayElementType()->getScalarSizeInBits() != | |||
45845 | VT.getSizeInBits() || | |||
45846 | Ty->getArrayNumElements() > | |||
45847 | Ty->getArrayElementType()->getScalarSizeInBits()) | |||
45848 | continue; | |||
45849 | ||||
45850 | // Check if the array's constant elements are suitable to our case. | |||
45851 | uint64_t ArrayElementCount = Init->getType()->getArrayNumElements(); | |||
45852 | bool ConstantsMatch = true; | |||
45853 | for (uint64_t j = 0; j < ArrayElementCount; j++) { | |||
45854 | auto *Elem = cast<ConstantInt>(Init->getAggregateElement(j)); | |||
45855 | if (Elem->getZExtValue() != (((uint64_t)1 << j) - 1)) { | |||
45856 | ConstantsMatch = false; | |||
45857 | break; | |||
45858 | } | |||
45859 | } | |||
45860 | if (!ConstantsMatch) | |||
45861 | continue; | |||
45862 | ||||
45863 | // Do the transformation (For 32-bit type): | |||
45864 | // -> (and (load arr[idx]), inp) | |||
45865 | // <- (and (srl 0xFFFFFFFF, (sub 32, idx))) | |||
45866 | // that will be replaced with one bzhi instruction. | |||
45867 | SDValue Inp = (i == 0) ? Node->getOperand(1) : Node->getOperand(0); | |||
45868 | SDValue SizeC = DAG.getConstant(VT.getSizeInBits(), dl, MVT::i32); | |||
45869 | ||||
45870 | // Get the Node which indexes into the array. | |||
45871 | SDValue Index = getIndexFromUnindexedLoad(Ld); | |||
45872 | if (!Index) | |||
45873 | return SDValue(); | |||
45874 | Index = DAG.getZExtOrTrunc(Index, dl, MVT::i32); | |||
45875 | ||||
45876 | SDValue Sub = DAG.getNode(ISD::SUB, dl, MVT::i32, SizeC, Index); | |||
45877 | Sub = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Sub); | |||
45878 | ||||
45879 | SDValue AllOnes = DAG.getAllOnesConstant(dl, VT); | |||
45880 | SDValue LShr = DAG.getNode(ISD::SRL, dl, VT, AllOnes, Sub); | |||
45881 | ||||
45882 | return DAG.getNode(ISD::AND, dl, VT, Inp, LShr); | |||
45883 | } | |||
45884 | } | |||
45885 | } | |||
45886 | } | |||
45887 | return SDValue(); | |||
45888 | } | |||
45889 | ||||
45890 | // Look for (and (bitcast (vXi1 (concat_vectors (vYi1 setcc), undef,))), C) | |||
45891 | // Where C is a mask containing the same number of bits as the setcc and | |||
45892 | // where the setcc will freely 0 upper bits of k-register. We can replace the | |||
45893 | // undef in the concat with 0s and remove the AND. This mainly helps with | |||
45894 | // v2i1/v4i1 setcc being casted to scalar. | |||
45895 | static SDValue combineScalarAndWithMaskSetcc(SDNode *N, SelectionDAG &DAG, | |||
45896 | const X86Subtarget &Subtarget) { | |||
45897 | assert(N->getOpcode() == ISD::AND && "Unexpected opcode!")(static_cast <bool> (N->getOpcode() == ISD::AND && "Unexpected opcode!") ? void (0) : __assert_fail ("N->getOpcode() == ISD::AND && \"Unexpected opcode!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45897, __extension__ __PRETTY_FUNCTION__)); | |||
45898 | ||||
45899 | EVT VT = N->getValueType(0); | |||
45900 | ||||
45901 | // Make sure this is an AND with constant. We will check the value of the | |||
45902 | // constant later. | |||
45903 | if (!isa<ConstantSDNode>(N->getOperand(1))) | |||
45904 | return SDValue(); | |||
45905 | ||||
45906 | // This is implied by the ConstantSDNode. | |||
45907 | assert(!VT.isVector() && "Expected scalar VT!")(static_cast <bool> (!VT.isVector() && "Expected scalar VT!" ) ? void (0) : __assert_fail ("!VT.isVector() && \"Expected scalar VT!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45907, __extension__ __PRETTY_FUNCTION__)); | |||
45908 | ||||
45909 | if (N->getOperand(0).getOpcode() != ISD::BITCAST || | |||
45910 | !N->getOperand(0).hasOneUse() || | |||
45911 | !N->getOperand(0).getOperand(0).hasOneUse()) | |||
45912 | return SDValue(); | |||
45913 | ||||
45914 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
45915 | SDValue Src = N->getOperand(0).getOperand(0); | |||
45916 | EVT SrcVT = Src.getValueType(); | |||
45917 | if (!SrcVT.isVector() || SrcVT.getVectorElementType() != MVT::i1 || | |||
45918 | !TLI.isTypeLegal(SrcVT)) | |||
45919 | return SDValue(); | |||
45920 | ||||
45921 | if (Src.getOpcode() != ISD::CONCAT_VECTORS) | |||
45922 | return SDValue(); | |||
45923 | ||||
45924 | // We only care about the first subvector of the concat, we expect the | |||
45925 | // other subvectors to be ignored due to the AND if we make the change. | |||
45926 | SDValue SubVec = Src.getOperand(0); | |||
45927 | EVT SubVecVT = SubVec.getValueType(); | |||
45928 | ||||
45929 | // First subvector should be a setcc with a legal result type. The RHS of the | |||
45930 | // AND should be a mask with this many bits. | |||
45931 | if (SubVec.getOpcode() != ISD::SETCC || !TLI.isTypeLegal(SubVecVT) || | |||
45932 | !N->getConstantOperandAPInt(1).isMask(SubVecVT.getVectorNumElements())) | |||
45933 | return SDValue(); | |||
45934 | ||||
45935 | EVT SetccVT = SubVec.getOperand(0).getValueType(); | |||
45936 | if (!TLI.isTypeLegal(SetccVT) || | |||
45937 | !(Subtarget.hasVLX() || SetccVT.is512BitVector())) | |||
45938 | return SDValue(); | |||
45939 | ||||
45940 | if (!(Subtarget.hasBWI() || SetccVT.getScalarSizeInBits() >= 32)) | |||
45941 | return SDValue(); | |||
45942 | ||||
45943 | // We passed all the checks. Rebuild the concat_vectors with zeroes | |||
45944 | // and cast it back to VT. | |||
45945 | SDLoc dl(N); | |||
45946 | SmallVector<SDValue, 4> Ops(Src.getNumOperands(), | |||
45947 | DAG.getConstant(0, dl, SubVecVT)); | |||
45948 | Ops[0] = SubVec; | |||
45949 | SDValue Concat = DAG.getNode(ISD::CONCAT_VECTORS, dl, SrcVT, | |||
45950 | Ops); | |||
45951 | return DAG.getBitcast(VT, Concat); | |||
45952 | } | |||
45953 | ||||
45954 | static SDValue combineAnd(SDNode *N, SelectionDAG &DAG, | |||
45955 | TargetLowering::DAGCombinerInfo &DCI, | |||
45956 | const X86Subtarget &Subtarget) { | |||
45957 | EVT VT = N->getValueType(0); | |||
45958 | ||||
45959 | // If this is SSE1 only convert to FAND to avoid scalarization. | |||
45960 | if (Subtarget.hasSSE1() && !Subtarget.hasSSE2() && VT == MVT::v4i32) { | |||
45961 | return DAG.getBitcast( | |||
45962 | MVT::v4i32, DAG.getNode(X86ISD::FAND, SDLoc(N), MVT::v4f32, | |||
45963 | DAG.getBitcast(MVT::v4f32, N->getOperand(0)), | |||
45964 | DAG.getBitcast(MVT::v4f32, N->getOperand(1)))); | |||
45965 | } | |||
45966 | ||||
45967 | // Use a 32-bit and+zext if upper bits known zero. | |||
45968 | if (VT == MVT::i64 && Subtarget.is64Bit() && | |||
45969 | !isa<ConstantSDNode>(N->getOperand(1))) { | |||
45970 | APInt HiMask = APInt::getHighBitsSet(64, 32); | |||
45971 | if (DAG.MaskedValueIsZero(N->getOperand(1), HiMask) || | |||
45972 | DAG.MaskedValueIsZero(N->getOperand(0), HiMask)) { | |||
45973 | SDLoc dl(N); | |||
45974 | SDValue LHS = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, N->getOperand(0)); | |||
45975 | SDValue RHS = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, N->getOperand(1)); | |||
45976 | return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, | |||
45977 | DAG.getNode(ISD::AND, dl, MVT::i32, LHS, RHS)); | |||
45978 | } | |||
45979 | } | |||
45980 | ||||
45981 | // Match all-of bool scalar reductions into a bitcast/movmsk + cmp. | |||
45982 | // TODO: Support multiple SrcOps. | |||
45983 | if (VT == MVT::i1) { | |||
45984 | SmallVector<SDValue, 2> SrcOps; | |||
45985 | SmallVector<APInt, 2> SrcPartials; | |||
45986 | if (matchScalarReduction(SDValue(N, 0), ISD::AND, SrcOps, &SrcPartials) && | |||
45987 | SrcOps.size() == 1) { | |||
45988 | SDLoc dl(N); | |||
45989 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
45990 | unsigned NumElts = SrcOps[0].getValueType().getVectorNumElements(); | |||
45991 | EVT MaskVT = EVT::getIntegerVT(*DAG.getContext(), NumElts); | |||
45992 | SDValue Mask = combineBitcastvxi1(DAG, MaskVT, SrcOps[0], dl, Subtarget); | |||
45993 | if (!Mask && TLI.isTypeLegal(SrcOps[0].getValueType())) | |||
45994 | Mask = DAG.getBitcast(MaskVT, SrcOps[0]); | |||
45995 | if (Mask) { | |||
45996 | assert(SrcPartials[0].getBitWidth() == NumElts &&(static_cast <bool> (SrcPartials[0].getBitWidth() == NumElts && "Unexpected partial reduction mask") ? void (0) : __assert_fail ("SrcPartials[0].getBitWidth() == NumElts && \"Unexpected partial reduction mask\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45997, __extension__ __PRETTY_FUNCTION__)) | |||
45997 | "Unexpected partial reduction mask")(static_cast <bool> (SrcPartials[0].getBitWidth() == NumElts && "Unexpected partial reduction mask") ? void (0) : __assert_fail ("SrcPartials[0].getBitWidth() == NumElts && \"Unexpected partial reduction mask\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 45997, __extension__ __PRETTY_FUNCTION__)); | |||
45998 | SDValue PartialBits = DAG.getConstant(SrcPartials[0], dl, MaskVT); | |||
45999 | Mask = DAG.getNode(ISD::AND, dl, MaskVT, Mask, PartialBits); | |||
46000 | return DAG.getSetCC(dl, MVT::i1, Mask, PartialBits, ISD::SETEQ); | |||
46001 | } | |||
46002 | } | |||
46003 | } | |||
46004 | ||||
46005 | if (SDValue V = combineScalarAndWithMaskSetcc(N, DAG, Subtarget)) | |||
46006 | return V; | |||
46007 | ||||
46008 | if (SDValue R = combineBitOpWithMOVMSK(N, DAG)) | |||
46009 | return R; | |||
46010 | ||||
46011 | if (SDValue FPLogic = convertIntLogicToFPLogic(N, DAG, DCI, Subtarget)) | |||
46012 | return FPLogic; | |||
46013 | ||||
46014 | if (DCI.isBeforeLegalizeOps()) | |||
46015 | return SDValue(); | |||
46016 | ||||
46017 | if (SDValue R = combineCompareEqual(N, DAG, DCI, Subtarget)) | |||
46018 | return R; | |||
46019 | ||||
46020 | if (SDValue R = combineANDXORWithAllOnesIntoANDNP(N, DAG)) | |||
46021 | return R; | |||
46022 | ||||
46023 | if (SDValue ShiftRight = combineAndMaskToShift(N, DAG, Subtarget)) | |||
46024 | return ShiftRight; | |||
46025 | ||||
46026 | if (SDValue R = combineAndLoadToBZHI(N, DAG, Subtarget)) | |||
46027 | return R; | |||
46028 | ||||
46029 | // Attempt to recursively combine a bitmask AND with shuffles. | |||
46030 | if (VT.isVector() && (VT.getScalarSizeInBits() % 8) == 0) { | |||
46031 | SDValue Op(N, 0); | |||
46032 | if (SDValue Res = combineX86ShufflesRecursively(Op, DAG, Subtarget)) | |||
46033 | return Res; | |||
46034 | } | |||
46035 | ||||
46036 | // Attempt to combine a scalar bitmask AND with an extracted shuffle. | |||
46037 | if ((VT.getScalarSizeInBits() % 8) == 0 && | |||
46038 | N->getOperand(0).getOpcode() == ISD::EXTRACT_VECTOR_ELT && | |||
46039 | isa<ConstantSDNode>(N->getOperand(0).getOperand(1))) { | |||
46040 | SDValue BitMask = N->getOperand(1); | |||
46041 | SDValue SrcVec = N->getOperand(0).getOperand(0); | |||
46042 | EVT SrcVecVT = SrcVec.getValueType(); | |||
46043 | ||||
46044 | // Check that the constant bitmask masks whole bytes. | |||
46045 | APInt UndefElts; | |||
46046 | SmallVector<APInt, 64> EltBits; | |||
46047 | if (VT == SrcVecVT.getScalarType() && | |||
46048 | N->getOperand(0)->isOnlyUserOf(SrcVec.getNode()) && | |||
46049 | getTargetConstantBitsFromNode(BitMask, 8, UndefElts, EltBits) && | |||
46050 | llvm::all_of(EltBits, [](const APInt &M) { | |||
46051 | return M.isZero() || M.isAllOnes(); | |||
46052 | })) { | |||
46053 | unsigned NumElts = SrcVecVT.getVectorNumElements(); | |||
46054 | unsigned Scale = SrcVecVT.getScalarSizeInBits() / 8; | |||
46055 | unsigned Idx = N->getOperand(0).getConstantOperandVal(1); | |||
46056 | ||||
46057 | // Create a root shuffle mask from the byte mask and the extracted index. | |||
46058 | SmallVector<int, 16> ShuffleMask(NumElts * Scale, SM_SentinelUndef); | |||
46059 | for (unsigned i = 0; i != Scale; ++i) { | |||
46060 | if (UndefElts[i]) | |||
46061 | continue; | |||
46062 | int VecIdx = Scale * Idx + i; | |||
46063 | ShuffleMask[VecIdx] = EltBits[i].isZero() ? SM_SentinelZero : VecIdx; | |||
46064 | } | |||
46065 | ||||
46066 | if (SDValue Shuffle = combineX86ShufflesRecursively( | |||
46067 | {SrcVec}, 0, SrcVec, ShuffleMask, {}, /*Depth*/ 1, | |||
46068 | X86::MaxShuffleCombineDepth, | |||
46069 | /*HasVarMask*/ false, /*AllowVarCrossLaneMask*/ true, | |||
46070 | /*AllowVarPerLaneMask*/ true, DAG, Subtarget)) | |||
46071 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N), VT, Shuffle, | |||
46072 | N->getOperand(0).getOperand(1)); | |||
46073 | } | |||
46074 | } | |||
46075 | ||||
46076 | return SDValue(); | |||
46077 | } | |||
46078 | ||||
46079 | // Canonicalize OR(AND(X,C),AND(Y,~C)) -> OR(AND(X,C),ANDNP(C,Y)) | |||
46080 | static SDValue canonicalizeBitSelect(SDNode *N, SelectionDAG &DAG, | |||
46081 | const X86Subtarget &Subtarget) { | |||
46082 | assert(N->getOpcode() == ISD::OR && "Unexpected Opcode")(static_cast <bool> (N->getOpcode() == ISD::OR && "Unexpected Opcode") ? void (0) : __assert_fail ("N->getOpcode() == ISD::OR && \"Unexpected Opcode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 46082, __extension__ __PRETTY_FUNCTION__)); | |||
46083 | ||||
46084 | MVT VT = N->getSimpleValueType(0); | |||
46085 | if (!VT.isVector() || (VT.getScalarSizeInBits() % 8) != 0) | |||
46086 | return SDValue(); | |||
46087 | ||||
46088 | SDValue N0 = peekThroughBitcasts(N->getOperand(0)); | |||
46089 | SDValue N1 = peekThroughBitcasts(N->getOperand(1)); | |||
46090 | if (N0.getOpcode() != ISD::AND || N1.getOpcode() != ISD::AND) | |||
46091 | return SDValue(); | |||
46092 | ||||
46093 | // On XOP we'll lower to PCMOV so accept one use. With AVX512, we can use | |||
46094 | // VPTERNLOG. Otherwise only do this if either mask has multiple uses already. | |||
46095 | bool UseVPTERNLOG = (Subtarget.hasAVX512() && VT.is512BitVector()) || | |||
46096 | Subtarget.hasVLX(); | |||
46097 | if (!(Subtarget.hasXOP() || UseVPTERNLOG || | |||
46098 | !N0.getOperand(1).hasOneUse() || !N1.getOperand(1).hasOneUse())) | |||
46099 | return SDValue(); | |||
46100 | ||||
46101 | // Attempt to extract constant byte masks. | |||
46102 | APInt UndefElts0, UndefElts1; | |||
46103 | SmallVector<APInt, 32> EltBits0, EltBits1; | |||
46104 | if (!getTargetConstantBitsFromNode(N0.getOperand(1), 8, UndefElts0, EltBits0, | |||
46105 | false, false)) | |||
46106 | return SDValue(); | |||
46107 | if (!getTargetConstantBitsFromNode(N1.getOperand(1), 8, UndefElts1, EltBits1, | |||
46108 | false, false)) | |||
46109 | return SDValue(); | |||
46110 | ||||
46111 | for (unsigned i = 0, e = EltBits0.size(); i != e; ++i) { | |||
46112 | // TODO - add UNDEF elts support. | |||
46113 | if (UndefElts0[i] || UndefElts1[i]) | |||
46114 | return SDValue(); | |||
46115 | if (EltBits0[i] != ~EltBits1[i]) | |||
46116 | return SDValue(); | |||
46117 | } | |||
46118 | ||||
46119 | SDLoc DL(N); | |||
46120 | ||||
46121 | if (UseVPTERNLOG) { | |||
46122 | // Emit a VPTERNLOG node directly. | |||
46123 | SDValue A = DAG.getBitcast(VT, N0.getOperand(1)); | |||
46124 | SDValue B = DAG.getBitcast(VT, N0.getOperand(0)); | |||
46125 | SDValue C = DAG.getBitcast(VT, N1.getOperand(0)); | |||
46126 | SDValue Imm = DAG.getTargetConstant(0xCA, DL, MVT::i8); | |||
46127 | return DAG.getNode(X86ISD::VPTERNLOG, DL, VT, A, B, C, Imm); | |||
46128 | } | |||
46129 | ||||
46130 | SDValue X = N->getOperand(0); | |||
46131 | SDValue Y = | |||
46132 | DAG.getNode(X86ISD::ANDNP, DL, VT, DAG.getBitcast(VT, N0.getOperand(1)), | |||
46133 | DAG.getBitcast(VT, N1.getOperand(0))); | |||
46134 | return DAG.getNode(ISD::OR, DL, VT, X, Y); | |||
46135 | } | |||
46136 | ||||
46137 | // Try to match OR(AND(~MASK,X),AND(MASK,Y)) logic pattern. | |||
46138 | static bool matchLogicBlend(SDNode *N, SDValue &X, SDValue &Y, SDValue &Mask) { | |||
46139 | if (N->getOpcode() != ISD::OR) | |||
46140 | return false; | |||
46141 | ||||
46142 | SDValue N0 = N->getOperand(0); | |||
46143 | SDValue N1 = N->getOperand(1); | |||
46144 | ||||
46145 | // Canonicalize AND to LHS. | |||
46146 | if (N1.getOpcode() == ISD::AND) | |||
46147 | std::swap(N0, N1); | |||
46148 | ||||
46149 | // Attempt to match OR(AND(M,Y),ANDNP(M,X)). | |||
46150 | if (N0.getOpcode() != ISD::AND || N1.getOpcode() != X86ISD::ANDNP) | |||
46151 | return false; | |||
46152 | ||||
46153 | Mask = N1.getOperand(0); | |||
46154 | X = N1.getOperand(1); | |||
46155 | ||||
46156 | // Check to see if the mask appeared in both the AND and ANDNP. | |||
46157 | if (N0.getOperand(0) == Mask) | |||
46158 | Y = N0.getOperand(1); | |||
46159 | else if (N0.getOperand(1) == Mask) | |||
46160 | Y = N0.getOperand(0); | |||
46161 | else | |||
46162 | return false; | |||
46163 | ||||
46164 | // TODO: Attempt to match against AND(XOR(-1,M),Y) as well, waiting for | |||
46165 | // ANDNP combine allows other combines to happen that prevent matching. | |||
46166 | return true; | |||
46167 | } | |||
46168 | ||||
46169 | // Try to fold: | |||
46170 | // (or (and (m, y), (pandn m, x))) | |||
46171 | // into: | |||
46172 | // (vselect m, x, y) | |||
46173 | // As a special case, try to fold: | |||
46174 | // (or (and (m, (sub 0, x)), (pandn m, x))) | |||
46175 | // into: | |||
46176 | // (sub (xor X, M), M) | |||
46177 | static SDValue combineLogicBlendIntoPBLENDV(SDNode *N, SelectionDAG &DAG, | |||
46178 | const X86Subtarget &Subtarget) { | |||
46179 | assert(N->getOpcode() == ISD::OR && "Unexpected Opcode")(static_cast <bool> (N->getOpcode() == ISD::OR && "Unexpected Opcode") ? void (0) : __assert_fail ("N->getOpcode() == ISD::OR && \"Unexpected Opcode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 46179, __extension__ __PRETTY_FUNCTION__)); | |||
46180 | ||||
46181 | EVT VT = N->getValueType(0); | |||
46182 | if (!((VT.is128BitVector() && Subtarget.hasSSE2()) || | |||
46183 | (VT.is256BitVector() && Subtarget.hasInt256()))) | |||
46184 | return SDValue(); | |||
46185 | ||||
46186 | SDValue X, Y, Mask; | |||
46187 | if (!matchLogicBlend(N, X, Y, Mask)) | |||
46188 | return SDValue(); | |||
46189 | ||||
46190 | // Validate that X, Y, and Mask are bitcasts, and see through them. | |||
46191 | Mask = peekThroughBitcasts(Mask); | |||
46192 | X = peekThroughBitcasts(X); | |||
46193 | Y = peekThroughBitcasts(Y); | |||
46194 | ||||
46195 | EVT MaskVT = Mask.getValueType(); | |||
46196 | unsigned EltBits = MaskVT.getScalarSizeInBits(); | |||
46197 | ||||
46198 | // TODO: Attempt to handle floating point cases as well? | |||
46199 | if (!MaskVT.isInteger() || DAG.ComputeNumSignBits(Mask) != EltBits) | |||
46200 | return SDValue(); | |||
46201 | ||||
46202 | SDLoc DL(N); | |||
46203 | ||||
46204 | // Attempt to combine to conditional negate: (sub (xor X, M), M) | |||
46205 | if (SDValue Res = combineLogicBlendIntoConditionalNegate(VT, Mask, X, Y, DL, | |||
46206 | DAG, Subtarget)) | |||
46207 | return Res; | |||
46208 | ||||
46209 | // PBLENDVB is only available on SSE 4.1. | |||
46210 | if (!Subtarget.hasSSE41()) | |||
46211 | return SDValue(); | |||
46212 | ||||
46213 | // If we have VPTERNLOG we should prefer that since PBLENDVB is multiple uops. | |||
46214 | if (Subtarget.hasVLX()) | |||
46215 | return SDValue(); | |||
46216 | ||||
46217 | MVT BlendVT = VT.is256BitVector() ? MVT::v32i8 : MVT::v16i8; | |||
46218 | ||||
46219 | X = DAG.getBitcast(BlendVT, X); | |||
46220 | Y = DAG.getBitcast(BlendVT, Y); | |||
46221 | Mask = DAG.getBitcast(BlendVT, Mask); | |||
46222 | Mask = DAG.getSelect(DL, BlendVT, Mask, Y, X); | |||
46223 | return DAG.getBitcast(VT, Mask); | |||
46224 | } | |||
46225 | ||||
46226 | // Helper function for combineOrCmpEqZeroToCtlzSrl | |||
46227 | // Transforms: | |||
46228 | // seteq(cmp x, 0) | |||
46229 | // into: | |||
46230 | // srl(ctlz x), log2(bitsize(x)) | |||
46231 | // Input pattern is checked by caller. | |||
46232 | static SDValue lowerX86CmpEqZeroToCtlzSrl(SDValue Op, EVT ExtTy, | |||
46233 | SelectionDAG &DAG) { | |||
46234 | SDValue Cmp = Op.getOperand(1); | |||
46235 | EVT VT = Cmp.getOperand(0).getValueType(); | |||
46236 | unsigned Log2b = Log2_32(VT.getSizeInBits()); | |||
46237 | SDLoc dl(Op); | |||
46238 | SDValue Clz = DAG.getNode(ISD::CTLZ, dl, VT, Cmp->getOperand(0)); | |||
46239 | // The result of the shift is true or false, and on X86, the 32-bit | |||
46240 | // encoding of shr and lzcnt is more desirable. | |||
46241 | SDValue Trunc = DAG.getZExtOrTrunc(Clz, dl, MVT::i32); | |||
46242 | SDValue Scc = DAG.getNode(ISD::SRL, dl, MVT::i32, Trunc, | |||
46243 | DAG.getConstant(Log2b, dl, MVT::i8)); | |||
46244 | return DAG.getZExtOrTrunc(Scc, dl, ExtTy); | |||
46245 | } | |||
46246 | ||||
46247 | // Try to transform: | |||
46248 | // zext(or(setcc(eq, (cmp x, 0)), setcc(eq, (cmp y, 0)))) | |||
46249 | // into: | |||
46250 | // srl(or(ctlz(x), ctlz(y)), log2(bitsize(x)) | |||
46251 | // Will also attempt to match more generic cases, eg: | |||
46252 | // zext(or(or(setcc(eq, cmp 0), setcc(eq, cmp 0)), setcc(eq, cmp 0))) | |||
46253 | // Only applies if the target supports the FastLZCNT feature. | |||
46254 | static SDValue combineOrCmpEqZeroToCtlzSrl(SDNode *N, SelectionDAG &DAG, | |||
46255 | TargetLowering::DAGCombinerInfo &DCI, | |||
46256 | const X86Subtarget &Subtarget) { | |||
46257 | if (DCI.isBeforeLegalize() || !Subtarget.getTargetLowering()->isCtlzFast()) | |||
46258 | return SDValue(); | |||
46259 | ||||
46260 | auto isORCandidate = [](SDValue N) { | |||
46261 | return (N->getOpcode() == ISD::OR && N->hasOneUse()); | |||
46262 | }; | |||
46263 | ||||
46264 | // Check the zero extend is extending to 32-bit or more. The code generated by | |||
46265 | // srl(ctlz) for 16-bit or less variants of the pattern would require extra | |||
46266 | // instructions to clear the upper bits. | |||
46267 | if (!N->hasOneUse() || !N->getSimpleValueType(0).bitsGE(MVT::i32) || | |||
46268 | !isORCandidate(N->getOperand(0))) | |||
46269 | return SDValue(); | |||
46270 | ||||
46271 | // Check the node matches: setcc(eq, cmp 0) | |||
46272 | auto isSetCCCandidate = [](SDValue N) { | |||
46273 | return N->getOpcode() == X86ISD::SETCC && N->hasOneUse() && | |||
46274 | X86::CondCode(N->getConstantOperandVal(0)) == X86::COND_E && | |||
46275 | N->getOperand(1).getOpcode() == X86ISD::CMP && | |||
46276 | isNullConstant(N->getOperand(1).getOperand(1)) && | |||
46277 | N->getOperand(1).getValueType().bitsGE(MVT::i32); | |||
46278 | }; | |||
46279 | ||||
46280 | SDNode *OR = N->getOperand(0).getNode(); | |||
46281 | SDValue LHS = OR->getOperand(0); | |||
46282 | SDValue RHS = OR->getOperand(1); | |||
46283 | ||||
46284 | // Save nodes matching or(or, setcc(eq, cmp 0)). | |||
46285 | SmallVector<SDNode *, 2> ORNodes; | |||
46286 | while (((isORCandidate(LHS) && isSetCCCandidate(RHS)) || | |||
46287 | (isORCandidate(RHS) && isSetCCCandidate(LHS)))) { | |||
46288 | ORNodes.push_back(OR); | |||
46289 | OR = (LHS->getOpcode() == ISD::OR) ? LHS.getNode() : RHS.getNode(); | |||
46290 | LHS = OR->getOperand(0); | |||
46291 | RHS = OR->getOperand(1); | |||
46292 | } | |||
46293 | ||||
46294 | // The last OR node should match or(setcc(eq, cmp 0), setcc(eq, cmp 0)). | |||
46295 | if (!(isSetCCCandidate(LHS) && isSetCCCandidate(RHS)) || | |||
46296 | !isORCandidate(SDValue(OR, 0))) | |||
46297 | return SDValue(); | |||
46298 | ||||
46299 | // We have a or(setcc(eq, cmp 0), setcc(eq, cmp 0)) pattern, try to lower it | |||
46300 | // to | |||
46301 | // or(srl(ctlz),srl(ctlz)). | |||
46302 | // The dag combiner can then fold it into: | |||
46303 | // srl(or(ctlz, ctlz)). | |||
46304 | EVT VT = OR->getValueType(0); | |||
46305 | SDValue NewLHS = lowerX86CmpEqZeroToCtlzSrl(LHS, VT, DAG); | |||
46306 | SDValue Ret, NewRHS; | |||
46307 | if (NewLHS && (NewRHS = lowerX86CmpEqZeroToCtlzSrl(RHS, VT, DAG))) | |||
46308 | Ret = DAG.getNode(ISD::OR, SDLoc(OR), VT, NewLHS, NewRHS); | |||
46309 | ||||
46310 | if (!Ret) | |||
46311 | return SDValue(); | |||
46312 | ||||
46313 | // Try to lower nodes matching the or(or, setcc(eq, cmp 0)) pattern. | |||
46314 | while (ORNodes.size() > 0) { | |||
46315 | OR = ORNodes.pop_back_val(); | |||
46316 | LHS = OR->getOperand(0); | |||
46317 | RHS = OR->getOperand(1); | |||
46318 | // Swap rhs with lhs to match or(setcc(eq, cmp, 0), or). | |||
46319 | if (RHS->getOpcode() == ISD::OR) | |||
46320 | std::swap(LHS, RHS); | |||
46321 | NewRHS = lowerX86CmpEqZeroToCtlzSrl(RHS, VT, DAG); | |||
46322 | if (!NewRHS) | |||
46323 | return SDValue(); | |||
46324 | Ret = DAG.getNode(ISD::OR, SDLoc(OR), VT, Ret, NewRHS); | |||
46325 | } | |||
46326 | ||||
46327 | if (Ret) | |||
46328 | Ret = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(N), N->getValueType(0), Ret); | |||
46329 | ||||
46330 | return Ret; | |||
46331 | } | |||
46332 | ||||
46333 | static SDValue combineOr(SDNode *N, SelectionDAG &DAG, | |||
46334 | TargetLowering::DAGCombinerInfo &DCI, | |||
46335 | const X86Subtarget &Subtarget) { | |||
46336 | SDValue N0 = N->getOperand(0); | |||
46337 | SDValue N1 = N->getOperand(1); | |||
46338 | EVT VT = N->getValueType(0); | |||
46339 | ||||
46340 | // If this is SSE1 only convert to FOR to avoid scalarization. | |||
46341 | if (Subtarget.hasSSE1() && !Subtarget.hasSSE2() && VT == MVT::v4i32) { | |||
46342 | return DAG.getBitcast(MVT::v4i32, | |||
46343 | DAG.getNode(X86ISD::FOR, SDLoc(N), MVT::v4f32, | |||
46344 | DAG.getBitcast(MVT::v4f32, N0), | |||
46345 | DAG.getBitcast(MVT::v4f32, N1))); | |||
46346 | } | |||
46347 | ||||
46348 | // Match any-of bool scalar reductions into a bitcast/movmsk + cmp. | |||
46349 | // TODO: Support multiple SrcOps. | |||
46350 | if (VT == MVT::i1) { | |||
46351 | SmallVector<SDValue, 2> SrcOps; | |||
46352 | SmallVector<APInt, 2> SrcPartials; | |||
46353 | if (matchScalarReduction(SDValue(N, 0), ISD::OR, SrcOps, &SrcPartials) && | |||
46354 | SrcOps.size() == 1) { | |||
46355 | SDLoc dl(N); | |||
46356 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
46357 | unsigned NumElts = SrcOps[0].getValueType().getVectorNumElements(); | |||
46358 | EVT MaskVT = EVT::getIntegerVT(*DAG.getContext(), NumElts); | |||
46359 | SDValue Mask = combineBitcastvxi1(DAG, MaskVT, SrcOps[0], dl, Subtarget); | |||
46360 | if (!Mask && TLI.isTypeLegal(SrcOps[0].getValueType())) | |||
46361 | Mask = DAG.getBitcast(MaskVT, SrcOps[0]); | |||
46362 | if (Mask) { | |||
46363 | assert(SrcPartials[0].getBitWidth() == NumElts &&(static_cast <bool> (SrcPartials[0].getBitWidth() == NumElts && "Unexpected partial reduction mask") ? void (0) : __assert_fail ("SrcPartials[0].getBitWidth() == NumElts && \"Unexpected partial reduction mask\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 46364, __extension__ __PRETTY_FUNCTION__)) | |||
46364 | "Unexpected partial reduction mask")(static_cast <bool> (SrcPartials[0].getBitWidth() == NumElts && "Unexpected partial reduction mask") ? void (0) : __assert_fail ("SrcPartials[0].getBitWidth() == NumElts && \"Unexpected partial reduction mask\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 46364, __extension__ __PRETTY_FUNCTION__)); | |||
46365 | SDValue ZeroBits = DAG.getConstant(0, dl, MaskVT); | |||
46366 | SDValue PartialBits = DAG.getConstant(SrcPartials[0], dl, MaskVT); | |||
46367 | Mask = DAG.getNode(ISD::AND, dl, MaskVT, Mask, PartialBits); | |||
46368 | return DAG.getSetCC(dl, MVT::i1, Mask, ZeroBits, ISD::SETNE); | |||
46369 | } | |||
46370 | } | |||
46371 | } | |||
46372 | ||||
46373 | if (SDValue R = combineBitOpWithMOVMSK(N, DAG)) | |||
46374 | return R; | |||
46375 | ||||
46376 | if (SDValue FPLogic = convertIntLogicToFPLogic(N, DAG, DCI, Subtarget)) | |||
46377 | return FPLogic; | |||
46378 | ||||
46379 | if (DCI.isBeforeLegalizeOps()) | |||
46380 | return SDValue(); | |||
46381 | ||||
46382 | if (SDValue R = combineCompareEqual(N, DAG, DCI, Subtarget)) | |||
46383 | return R; | |||
46384 | ||||
46385 | if (SDValue R = canonicalizeBitSelect(N, DAG, Subtarget)) | |||
46386 | return R; | |||
46387 | ||||
46388 | if (SDValue R = combineLogicBlendIntoPBLENDV(N, DAG, Subtarget)) | |||
46389 | return R; | |||
46390 | ||||
46391 | // Combine OR(X,KSHIFTL(Y,Elts/2)) -> CONCAT_VECTORS(X,Y) == KUNPCK(X,Y). | |||
46392 | // Combine OR(KSHIFTL(X,Elts/2),Y) -> CONCAT_VECTORS(Y,X) == KUNPCK(Y,X). | |||
46393 | // iff the upper elements of the non-shifted arg are zero. | |||
46394 | // KUNPCK require 16+ bool vector elements. | |||
46395 | if (N0.getOpcode() == X86ISD::KSHIFTL || N1.getOpcode() == X86ISD::KSHIFTL) { | |||
46396 | unsigned NumElts = VT.getVectorNumElements(); | |||
46397 | unsigned HalfElts = NumElts / 2; | |||
46398 | APInt UpperElts = APInt::getHighBitsSet(NumElts, HalfElts); | |||
46399 | if (NumElts >= 16 && N1.getOpcode() == X86ISD::KSHIFTL && | |||
46400 | N1.getConstantOperandAPInt(1) == HalfElts && | |||
46401 | DAG.MaskedValueIsZero(N0, APInt(1, 1), UpperElts)) { | |||
46402 | SDLoc dl(N); | |||
46403 | return DAG.getNode( | |||
46404 | ISD::CONCAT_VECTORS, dl, VT, | |||
46405 | extractSubVector(N0, 0, DAG, dl, HalfElts), | |||
46406 | extractSubVector(N1.getOperand(0), 0, DAG, dl, HalfElts)); | |||
46407 | } | |||
46408 | if (NumElts >= 16 && N0.getOpcode() == X86ISD::KSHIFTL && | |||
46409 | N0.getConstantOperandAPInt(1) == HalfElts && | |||
46410 | DAG.MaskedValueIsZero(N1, APInt(1, 1), UpperElts)) { | |||
46411 | SDLoc dl(N); | |||
46412 | return DAG.getNode( | |||
46413 | ISD::CONCAT_VECTORS, dl, VT, | |||
46414 | extractSubVector(N1, 0, DAG, dl, HalfElts), | |||
46415 | extractSubVector(N0.getOperand(0), 0, DAG, dl, HalfElts)); | |||
46416 | } | |||
46417 | } | |||
46418 | ||||
46419 | // Attempt to recursively combine an OR of shuffles. | |||
46420 | if (VT.isVector() && (VT.getScalarSizeInBits() % 8) == 0) { | |||
46421 | SDValue Op(N, 0); | |||
46422 | if (SDValue Res = combineX86ShufflesRecursively(Op, DAG, Subtarget)) | |||
46423 | return Res; | |||
46424 | } | |||
46425 | ||||
46426 | return SDValue(); | |||
46427 | } | |||
46428 | ||||
46429 | /// Try to turn tests against the signbit in the form of: | |||
46430 | /// XOR(TRUNCATE(SRL(X, size(X)-1)), 1) | |||
46431 | /// into: | |||
46432 | /// SETGT(X, -1) | |||
46433 | static SDValue foldXorTruncShiftIntoCmp(SDNode *N, SelectionDAG &DAG) { | |||
46434 | // This is only worth doing if the output type is i8 or i1. | |||
46435 | EVT ResultType = N->getValueType(0); | |||
46436 | if (ResultType != MVT::i8 && ResultType != MVT::i1) | |||
46437 | return SDValue(); | |||
46438 | ||||
46439 | SDValue N0 = N->getOperand(0); | |||
46440 | SDValue N1 = N->getOperand(1); | |||
46441 | ||||
46442 | // We should be performing an xor against a truncated shift. | |||
46443 | if (N0.getOpcode() != ISD::TRUNCATE || !N0.hasOneUse()) | |||
46444 | return SDValue(); | |||
46445 | ||||
46446 | // Make sure we are performing an xor against one. | |||
46447 | if (!isOneConstant(N1)) | |||
46448 | return SDValue(); | |||
46449 | ||||
46450 | // SetCC on x86 zero extends so only act on this if it's a logical shift. | |||
46451 | SDValue Shift = N0.getOperand(0); | |||
46452 | if (Shift.getOpcode() != ISD::SRL || !Shift.hasOneUse()) | |||
46453 | return SDValue(); | |||
46454 | ||||
46455 | // Make sure we are truncating from one of i16, i32 or i64. | |||
46456 | EVT ShiftTy = Shift.getValueType(); | |||
46457 | if (ShiftTy != MVT::i16 && ShiftTy != MVT::i32 && ShiftTy != MVT::i64) | |||
46458 | return SDValue(); | |||
46459 | ||||
46460 | // Make sure the shift amount extracts the sign bit. | |||
46461 | if (!isa<ConstantSDNode>(Shift.getOperand(1)) || | |||
46462 | Shift.getConstantOperandAPInt(1) != (ShiftTy.getSizeInBits() - 1)) | |||
46463 | return SDValue(); | |||
46464 | ||||
46465 | // Create a greater-than comparison against -1. | |||
46466 | // N.B. Using SETGE against 0 works but we want a canonical looking | |||
46467 | // comparison, using SETGT matches up with what TranslateX86CC. | |||
46468 | SDLoc DL(N); | |||
46469 | SDValue ShiftOp = Shift.getOperand(0); | |||
46470 | EVT ShiftOpTy = ShiftOp.getValueType(); | |||
46471 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
46472 | EVT SetCCResultType = TLI.getSetCCResultType(DAG.getDataLayout(), | |||
46473 | *DAG.getContext(), ResultType); | |||
46474 | SDValue Cond = DAG.getSetCC(DL, SetCCResultType, ShiftOp, | |||
46475 | DAG.getConstant(-1, DL, ShiftOpTy), ISD::SETGT); | |||
46476 | if (SetCCResultType != ResultType) | |||
46477 | Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, ResultType, Cond); | |||
46478 | return Cond; | |||
46479 | } | |||
46480 | ||||
46481 | /// Turn vector tests of the signbit in the form of: | |||
46482 | /// xor (sra X, elt_size(X)-1), -1 | |||
46483 | /// into: | |||
46484 | /// pcmpgt X, -1 | |||
46485 | /// | |||
46486 | /// This should be called before type legalization because the pattern may not | |||
46487 | /// persist after that. | |||
46488 | static SDValue foldVectorXorShiftIntoCmp(SDNode *N, SelectionDAG &DAG, | |||
46489 | const X86Subtarget &Subtarget) { | |||
46490 | EVT VT = N->getValueType(0); | |||
46491 | if (!VT.isSimple()) | |||
46492 | return SDValue(); | |||
46493 | ||||
46494 | switch (VT.getSimpleVT().SimpleTy) { | |||
46495 | default: return SDValue(); | |||
46496 | case MVT::v16i8: | |||
46497 | case MVT::v8i16: | |||
46498 | case MVT::v4i32: | |||
46499 | case MVT::v2i64: if (!Subtarget.hasSSE2()) return SDValue(); break; | |||
46500 | case MVT::v32i8: | |||
46501 | case MVT::v16i16: | |||
46502 | case MVT::v8i32: | |||
46503 | case MVT::v4i64: if (!Subtarget.hasAVX2()) return SDValue(); break; | |||
46504 | } | |||
46505 | ||||
46506 | // There must be a shift right algebraic before the xor, and the xor must be a | |||
46507 | // 'not' operation. | |||
46508 | SDValue Shift = N->getOperand(0); | |||
46509 | SDValue Ones = N->getOperand(1); | |||
46510 | if (Shift.getOpcode() != ISD::SRA || !Shift.hasOneUse() || | |||
46511 | !ISD::isBuildVectorAllOnes(Ones.getNode())) | |||
46512 | return SDValue(); | |||
46513 | ||||
46514 | // The shift should be smearing the sign bit across each vector element. | |||
46515 | auto *ShiftAmt = | |||
46516 | isConstOrConstSplat(Shift.getOperand(1), /*AllowUndefs*/ true); | |||
46517 | if (!ShiftAmt || | |||
46518 | ShiftAmt->getAPIntValue() != (Shift.getScalarValueSizeInBits() - 1)) | |||
46519 | return SDValue(); | |||
46520 | ||||
46521 | // Create a greater-than comparison against -1. We don't use the more obvious | |||
46522 | // greater-than-or-equal-to-zero because SSE/AVX don't have that instruction. | |||
46523 | return DAG.getSetCC(SDLoc(N), VT, Shift.getOperand(0), Ones, ISD::SETGT); | |||
46524 | } | |||
46525 | ||||
46526 | /// Detect patterns of truncation with unsigned saturation: | |||
46527 | /// | |||
46528 | /// 1. (truncate (umin (x, unsigned_max_of_dest_type)) to dest_type). | |||
46529 | /// Return the source value x to be truncated or SDValue() if the pattern was | |||
46530 | /// not matched. | |||
46531 | /// | |||
46532 | /// 2. (truncate (smin (smax (x, C1), C2)) to dest_type), | |||
46533 | /// where C1 >= 0 and C2 is unsigned max of destination type. | |||
46534 | /// | |||
46535 | /// (truncate (smax (smin (x, C2), C1)) to dest_type) | |||
46536 | /// where C1 >= 0, C2 is unsigned max of destination type and C1 <= C2. | |||
46537 | /// | |||
46538 | /// These two patterns are equivalent to: | |||
46539 | /// (truncate (umin (smax(x, C1), unsigned_max_of_dest_type)) to dest_type) | |||
46540 | /// So return the smax(x, C1) value to be truncated or SDValue() if the | |||
46541 | /// pattern was not matched. | |||
46542 | static SDValue detectUSatPattern(SDValue In, EVT VT, SelectionDAG &DAG, | |||
46543 | const SDLoc &DL) { | |||
46544 | EVT InVT = In.getValueType(); | |||
46545 | ||||
46546 | // Saturation with truncation. We truncate from InVT to VT. | |||
46547 | assert(InVT.getScalarSizeInBits() > VT.getScalarSizeInBits() &&(static_cast <bool> (InVT.getScalarSizeInBits() > VT .getScalarSizeInBits() && "Unexpected types for truncate operation" ) ? void (0) : __assert_fail ("InVT.getScalarSizeInBits() > VT.getScalarSizeInBits() && \"Unexpected types for truncate operation\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 46548, __extension__ __PRETTY_FUNCTION__)) | |||
46548 | "Unexpected types for truncate operation")(static_cast <bool> (InVT.getScalarSizeInBits() > VT .getScalarSizeInBits() && "Unexpected types for truncate operation" ) ? void (0) : __assert_fail ("InVT.getScalarSizeInBits() > VT.getScalarSizeInBits() && \"Unexpected types for truncate operation\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 46548, __extension__ __PRETTY_FUNCTION__)); | |||
46549 | ||||
46550 | // Match min/max and return limit value as a parameter. | |||
46551 | auto MatchMinMax = [](SDValue V, unsigned Opcode, APInt &Limit) -> SDValue { | |||
46552 | if (V.getOpcode() == Opcode && | |||
46553 | ISD::isConstantSplatVector(V.getOperand(1).getNode(), Limit)) | |||
46554 | return V.getOperand(0); | |||
46555 | return SDValue(); | |||
46556 | }; | |||
46557 | ||||
46558 | APInt C1, C2; | |||
46559 | if (SDValue UMin = MatchMinMax(In, ISD::UMIN, C2)) | |||
46560 | // C2 should be equal to UINT32_MAX / UINT16_MAX / UINT8_MAX according | |||
46561 | // the element size of the destination type. | |||
46562 | if (C2.isMask(VT.getScalarSizeInBits())) | |||
46563 | return UMin; | |||
46564 | ||||
46565 | if (SDValue SMin = MatchMinMax(In, ISD::SMIN, C2)) | |||
46566 | if (MatchMinMax(SMin, ISD::SMAX, C1)) | |||
46567 | if (C1.isNonNegative() && C2.isMask(VT.getScalarSizeInBits())) | |||
46568 | return SMin; | |||
46569 | ||||
46570 | if (SDValue SMax = MatchMinMax(In, ISD::SMAX, C1)) | |||
46571 | if (SDValue SMin = MatchMinMax(SMax, ISD::SMIN, C2)) | |||
46572 | if (C1.isNonNegative() && C2.isMask(VT.getScalarSizeInBits()) && | |||
46573 | C2.uge(C1)) { | |||
46574 | return DAG.getNode(ISD::SMAX, DL, InVT, SMin, In.getOperand(1)); | |||
46575 | } | |||
46576 | ||||
46577 | return SDValue(); | |||
46578 | } | |||
46579 | ||||
46580 | /// Detect patterns of truncation with signed saturation: | |||
46581 | /// (truncate (smin ((smax (x, signed_min_of_dest_type)), | |||
46582 | /// signed_max_of_dest_type)) to dest_type) | |||
46583 | /// or: | |||
46584 | /// (truncate (smax ((smin (x, signed_max_of_dest_type)), | |||
46585 | /// signed_min_of_dest_type)) to dest_type). | |||
46586 | /// With MatchPackUS, the smax/smin range is [0, unsigned_max_of_dest_type]. | |||
46587 | /// Return the source value to be truncated or SDValue() if the pattern was not | |||
46588 | /// matched. | |||
46589 | static SDValue detectSSatPattern(SDValue In, EVT VT, bool MatchPackUS = false) { | |||
46590 | unsigned NumDstBits = VT.getScalarSizeInBits(); | |||
46591 | unsigned NumSrcBits = In.getScalarValueSizeInBits(); | |||
46592 | assert(NumSrcBits > NumDstBits && "Unexpected types for truncate operation")(static_cast <bool> (NumSrcBits > NumDstBits && "Unexpected types for truncate operation") ? void (0) : __assert_fail ("NumSrcBits > NumDstBits && \"Unexpected types for truncate operation\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 46592, __extension__ __PRETTY_FUNCTION__)); | |||
46593 | ||||
46594 | auto MatchMinMax = [](SDValue V, unsigned Opcode, | |||
46595 | const APInt &Limit) -> SDValue { | |||
46596 | APInt C; | |||
46597 | if (V.getOpcode() == Opcode && | |||
46598 | ISD::isConstantSplatVector(V.getOperand(1).getNode(), C) && C == Limit) | |||
46599 | return V.getOperand(0); | |||
46600 | return SDValue(); | |||
46601 | }; | |||
46602 | ||||
46603 | APInt SignedMax, SignedMin; | |||
46604 | if (MatchPackUS) { | |||
46605 | SignedMax = APInt::getAllOnes(NumDstBits).zext(NumSrcBits); | |||
46606 | SignedMin = APInt(NumSrcBits, 0); | |||
46607 | } else { | |||
46608 | SignedMax = APInt::getSignedMaxValue(NumDstBits).sext(NumSrcBits); | |||
46609 | SignedMin = APInt::getSignedMinValue(NumDstBits).sext(NumSrcBits); | |||
46610 | } | |||
46611 | ||||
46612 | if (SDValue SMin = MatchMinMax(In, ISD::SMIN, SignedMax)) | |||
46613 | if (SDValue SMax = MatchMinMax(SMin, ISD::SMAX, SignedMin)) | |||
46614 | return SMax; | |||
46615 | ||||
46616 | if (SDValue SMax = MatchMinMax(In, ISD::SMAX, SignedMin)) | |||
46617 | if (SDValue SMin = MatchMinMax(SMax, ISD::SMIN, SignedMax)) | |||
46618 | return SMin; | |||
46619 | ||||
46620 | return SDValue(); | |||
46621 | } | |||
46622 | ||||
46623 | static SDValue combineTruncateWithSat(SDValue In, EVT VT, const SDLoc &DL, | |||
46624 | SelectionDAG &DAG, | |||
46625 | const X86Subtarget &Subtarget) { | |||
46626 | if (!Subtarget.hasSSE2() || !VT.isVector()) | |||
46627 | return SDValue(); | |||
46628 | ||||
46629 | EVT SVT = VT.getVectorElementType(); | |||
46630 | EVT InVT = In.getValueType(); | |||
46631 | EVT InSVT = InVT.getVectorElementType(); | |||
46632 | ||||
46633 | // If we're clamping a signed 32-bit vector to 0-255 and the 32-bit vector is | |||
46634 | // split across two registers. We can use a packusdw+perm to clamp to 0-65535 | |||
46635 | // and concatenate at the same time. Then we can use a final vpmovuswb to | |||
46636 | // clip to 0-255. | |||
46637 | if (Subtarget.hasBWI() && !Subtarget.useAVX512Regs() && | |||
46638 | InVT == MVT::v16i32 && VT == MVT::v16i8) { | |||
46639 | if (auto USatVal = detectSSatPattern(In, VT, true)) { | |||
46640 | // Emit a VPACKUSDW+VPERMQ followed by a VPMOVUSWB. | |||
46641 | SDValue Mid = truncateVectorWithPACK(X86ISD::PACKUS, MVT::v16i16, USatVal, | |||
46642 | DL, DAG, Subtarget); | |||
46643 | assert(Mid && "Failed to pack!")(static_cast <bool> (Mid && "Failed to pack!") ? void (0) : __assert_fail ("Mid && \"Failed to pack!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 46643, __extension__ __PRETTY_FUNCTION__)); | |||
46644 | return DAG.getNode(X86ISD::VTRUNCUS, DL, VT, Mid); | |||
46645 | } | |||
46646 | } | |||
46647 | ||||
46648 | // vXi32 truncate instructions are available with AVX512F. | |||
46649 | // vXi16 truncate instructions are only available with AVX512BW. | |||
46650 | // For 256-bit or smaller vectors, we require VLX. | |||
46651 | // FIXME: We could widen truncates to 512 to remove the VLX restriction. | |||
46652 | // If the result type is 256-bits or larger and we have disable 512-bit | |||
46653 | // registers, we should go ahead and use the pack instructions if possible. | |||
46654 | bool PreferAVX512 = ((Subtarget.hasAVX512() && InSVT == MVT::i32) || | |||
46655 | (Subtarget.hasBWI() && InSVT == MVT::i16)) && | |||
46656 | (InVT.getSizeInBits() > 128) && | |||
46657 | (Subtarget.hasVLX() || InVT.getSizeInBits() > 256) && | |||
46658 | !(!Subtarget.useAVX512Regs() && VT.getSizeInBits() >= 256); | |||
46659 | ||||
46660 | if (isPowerOf2_32(VT.getVectorNumElements()) && !PreferAVX512 && | |||
46661 | VT.getSizeInBits() >= 64 && | |||
46662 | (SVT == MVT::i8 || SVT == MVT::i16) && | |||
46663 | (InSVT == MVT::i16 || InSVT == MVT::i32)) { | |||
46664 | if (auto USatVal = detectSSatPattern(In, VT, true)) { | |||
46665 | // vXi32 -> vXi8 must be performed as PACKUSWB(PACKSSDW,PACKSSDW). | |||
46666 | // Only do this when the result is at least 64 bits or we'll leaving | |||
46667 | // dangling PACKSSDW nodes. | |||
46668 | if (SVT == MVT::i8 && InSVT == MVT::i32) { | |||
46669 | EVT MidVT = VT.changeVectorElementType(MVT::i16); | |||
46670 | SDValue Mid = truncateVectorWithPACK(X86ISD::PACKSS, MidVT, USatVal, DL, | |||
46671 | DAG, Subtarget); | |||
46672 | assert(Mid && "Failed to pack!")(static_cast <bool> (Mid && "Failed to pack!") ? void (0) : __assert_fail ("Mid && \"Failed to pack!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 46672, __extension__ __PRETTY_FUNCTION__)); | |||
46673 | SDValue V = truncateVectorWithPACK(X86ISD::PACKUS, VT, Mid, DL, DAG, | |||
46674 | Subtarget); | |||
46675 | assert(V && "Failed to pack!")(static_cast <bool> (V && "Failed to pack!") ? void (0) : __assert_fail ("V && \"Failed to pack!\"", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 46675, __extension__ __PRETTY_FUNCTION__)); | |||
46676 | return V; | |||
46677 | } else if (SVT == MVT::i8 || Subtarget.hasSSE41()) | |||
46678 | return truncateVectorWithPACK(X86ISD::PACKUS, VT, USatVal, DL, DAG, | |||
46679 | Subtarget); | |||
46680 | } | |||
46681 | if (auto SSatVal = detectSSatPattern(In, VT)) | |||
46682 | return truncateVectorWithPACK(X86ISD::PACKSS, VT, SSatVal, DL, DAG, | |||
46683 | Subtarget); | |||
46684 | } | |||
46685 | ||||
46686 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
46687 | if (TLI.isTypeLegal(InVT) && InVT.isVector() && SVT != MVT::i1 && | |||
46688 | Subtarget.hasAVX512() && (InSVT != MVT::i16 || Subtarget.hasBWI()) && | |||
46689 | (SVT == MVT::i32 || SVT == MVT::i16 || SVT == MVT::i8)) { | |||
46690 | unsigned TruncOpc = 0; | |||
46691 | SDValue SatVal; | |||
46692 | if (auto SSatVal = detectSSatPattern(In, VT)) { | |||
46693 | SatVal = SSatVal; | |||
46694 | TruncOpc = X86ISD::VTRUNCS; | |||
46695 | } else if (auto USatVal = detectUSatPattern(In, VT, DAG, DL)) { | |||
46696 | SatVal = USatVal; | |||
46697 | TruncOpc = X86ISD::VTRUNCUS; | |||
46698 | } | |||
46699 | if (SatVal) { | |||
46700 | unsigned ResElts = VT.getVectorNumElements(); | |||
46701 | // If the input type is less than 512 bits and we don't have VLX, we need | |||
46702 | // to widen to 512 bits. | |||
46703 | if (!Subtarget.hasVLX() && !InVT.is512BitVector()) { | |||
46704 | unsigned NumConcats = 512 / InVT.getSizeInBits(); | |||
46705 | ResElts *= NumConcats; | |||
46706 | SmallVector<SDValue, 4> ConcatOps(NumConcats, DAG.getUNDEF(InVT)); | |||
46707 | ConcatOps[0] = SatVal; | |||
46708 | InVT = EVT::getVectorVT(*DAG.getContext(), InSVT, | |||
46709 | NumConcats * InVT.getVectorNumElements()); | |||
46710 | SatVal = DAG.getNode(ISD::CONCAT_VECTORS, DL, InVT, ConcatOps); | |||
46711 | } | |||
46712 | // Widen the result if its narrower than 128 bits. | |||
46713 | if (ResElts * SVT.getSizeInBits() < 128) | |||
46714 | ResElts = 128 / SVT.getSizeInBits(); | |||
46715 | EVT TruncVT = EVT::getVectorVT(*DAG.getContext(), SVT, ResElts); | |||
46716 | SDValue Res = DAG.getNode(TruncOpc, DL, TruncVT, SatVal); | |||
46717 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Res, | |||
46718 | DAG.getIntPtrConstant(0, DL)); | |||
46719 | } | |||
46720 | } | |||
46721 | ||||
46722 | return SDValue(); | |||
46723 | } | |||
46724 | ||||
46725 | /// This function detects the AVG pattern between vectors of unsigned i8/i16, | |||
46726 | /// which is c = (a + b + 1) / 2, and replace this operation with the efficient | |||
46727 | /// X86ISD::AVG instruction. | |||
46728 | static SDValue detectAVGPattern(SDValue In, EVT VT, SelectionDAG &DAG, | |||
46729 | const X86Subtarget &Subtarget, | |||
46730 | const SDLoc &DL) { | |||
46731 | if (!VT.isVector()) | |||
46732 | return SDValue(); | |||
46733 | EVT InVT = In.getValueType(); | |||
46734 | unsigned NumElems = VT.getVectorNumElements(); | |||
46735 | ||||
46736 | EVT ScalarVT = VT.getVectorElementType(); | |||
46737 | if (!((ScalarVT == MVT::i8 || ScalarVT == MVT::i16) && NumElems >= 2)) | |||
46738 | return SDValue(); | |||
46739 | ||||
46740 | // InScalarVT is the intermediate type in AVG pattern and it should be greater | |||
46741 | // than the original input type (i8/i16). | |||
46742 | EVT InScalarVT = InVT.getVectorElementType(); | |||
46743 | if (InScalarVT.getFixedSizeInBits() <= ScalarVT.getFixedSizeInBits()) | |||
46744 | return SDValue(); | |||
46745 | ||||
46746 | if (!Subtarget.hasSSE2()) | |||
46747 | return SDValue(); | |||
46748 | ||||
46749 | // Detect the following pattern: | |||
46750 | // | |||
46751 | // %1 = zext <N x i8> %a to <N x i32> | |||
46752 | // %2 = zext <N x i8> %b to <N x i32> | |||
46753 | // %3 = add nuw nsw <N x i32> %1, <i32 1 x N> | |||
46754 | // %4 = add nuw nsw <N x i32> %3, %2 | |||
46755 | // %5 = lshr <N x i32> %N, <i32 1 x N> | |||
46756 | // %6 = trunc <N x i32> %5 to <N x i8> | |||
46757 | // | |||
46758 | // In AVX512, the last instruction can also be a trunc store. | |||
46759 | if (In.getOpcode() != ISD::SRL) | |||
46760 | return SDValue(); | |||
46761 | ||||
46762 | // A lambda checking the given SDValue is a constant vector and each element | |||
46763 | // is in the range [Min, Max]. | |||
46764 | auto IsConstVectorInRange = [](SDValue V, unsigned Min, unsigned Max) { | |||
46765 | return ISD::matchUnaryPredicate(V, [Min, Max](ConstantSDNode *C) { | |||
46766 | return !(C->getAPIntValue().ult(Min) || C->getAPIntValue().ugt(Max)); | |||
46767 | }); | |||
46768 | }; | |||
46769 | ||||
46770 | auto IsZExtLike = [DAG = &DAG, ScalarVT](SDValue V) { | |||
46771 | unsigned MaxActiveBits = DAG->computeKnownBits(V).countMaxActiveBits(); | |||
46772 | return MaxActiveBits <= ScalarVT.getSizeInBits(); | |||
46773 | }; | |||
46774 | ||||
46775 | // Check if each element of the vector is right-shifted by one. | |||
46776 | SDValue LHS = In.getOperand(0); | |||
46777 | SDValue RHS = In.getOperand(1); | |||
46778 | if (!IsConstVectorInRange(RHS, 1, 1)) | |||
46779 | return SDValue(); | |||
46780 | if (LHS.getOpcode() != ISD::ADD) | |||
46781 | return SDValue(); | |||
46782 | ||||
46783 | // Detect a pattern of a + b + 1 where the order doesn't matter. | |||
46784 | SDValue Operands[3]; | |||
46785 | Operands[0] = LHS.getOperand(0); | |||
46786 | Operands[1] = LHS.getOperand(1); | |||
46787 | ||||
46788 | auto AVGBuilder = [](SelectionDAG &DAG, const SDLoc &DL, | |||
46789 | ArrayRef<SDValue> Ops) { | |||
46790 | return DAG.getNode(X86ISD::AVG, DL, Ops[0].getValueType(), Ops); | |||
46791 | }; | |||
46792 | ||||
46793 | auto AVGSplitter = [&](std::array<SDValue, 2> Ops) { | |||
46794 | for (SDValue &Op : Ops) | |||
46795 | if (Op.getValueType() != VT) | |||
46796 | Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op); | |||
46797 | // Pad to a power-of-2 vector, split+apply and extract the original vector. | |||
46798 | unsigned NumElemsPow2 = PowerOf2Ceil(NumElems); | |||
46799 | EVT Pow2VT = EVT::getVectorVT(*DAG.getContext(), ScalarVT, NumElemsPow2); | |||
46800 | if (NumElemsPow2 != NumElems) { | |||
46801 | for (SDValue &Op : Ops) { | |||
46802 | SmallVector<SDValue, 32> EltsOfOp(NumElemsPow2, DAG.getUNDEF(ScalarVT)); | |||
46803 | for (unsigned i = 0; i != NumElems; ++i) { | |||
46804 | SDValue Idx = DAG.getIntPtrConstant(i, DL); | |||
46805 | EltsOfOp[i] = | |||
46806 | DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ScalarVT, Op, Idx); | |||
46807 | } | |||
46808 | Op = DAG.getBuildVector(Pow2VT, DL, EltsOfOp); | |||
46809 | } | |||
46810 | } | |||
46811 | SDValue Res = SplitOpsAndApply(DAG, Subtarget, DL, Pow2VT, Ops, AVGBuilder); | |||
46812 | if (NumElemsPow2 == NumElems) | |||
46813 | return Res; | |||
46814 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Res, | |||
46815 | DAG.getIntPtrConstant(0, DL)); | |||
46816 | }; | |||
46817 | ||||
46818 | // Take care of the case when one of the operands is a constant vector whose | |||
46819 | // element is in the range [1, 256]. | |||
46820 | if (IsConstVectorInRange(Operands[1], 1, ScalarVT == MVT::i8 ? 256 : 65536) && | |||
46821 | IsZExtLike(Operands[0])) { | |||
46822 | // The pattern is detected. Subtract one from the constant vector, then | |||
46823 | // demote it and emit X86ISD::AVG instruction. | |||
46824 | SDValue VecOnes = DAG.getConstant(1, DL, InVT); | |||
46825 | Operands[1] = DAG.getNode(ISD::SUB, DL, InVT, Operands[1], VecOnes); | |||
46826 | return AVGSplitter({Operands[0], Operands[1]}); | |||
46827 | } | |||
46828 | ||||
46829 | // Matches 'add like' patterns: add(Op0,Op1) + zext(or(Op0,Op1)). | |||
46830 | // Match the or case only if its 'add-like' - can be replaced by an add. | |||
46831 | auto FindAddLike = [&](SDValue V, SDValue &Op0, SDValue &Op1) { | |||
46832 | if (ISD::ADD == V.getOpcode()) { | |||
46833 | Op0 = V.getOperand(0); | |||
46834 | Op1 = V.getOperand(1); | |||
46835 | return true; | |||
46836 | } | |||
46837 | if (ISD::ZERO_EXTEND != V.getOpcode()) | |||
46838 | return false; | |||
46839 | V = V.getOperand(0); | |||
46840 | if (V.getValueType() != VT || ISD::OR != V.getOpcode() || | |||
46841 | !DAG.haveNoCommonBitsSet(V.getOperand(0), V.getOperand(1))) | |||
46842 | return false; | |||
46843 | Op0 = V.getOperand(0); | |||
46844 | Op1 = V.getOperand(1); | |||
46845 | return true; | |||
46846 | }; | |||
46847 | ||||
46848 | SDValue Op0, Op1; | |||
46849 | if (FindAddLike(Operands[0], Op0, Op1)) | |||
46850 | std::swap(Operands[0], Operands[1]); | |||
46851 | else if (!FindAddLike(Operands[1], Op0, Op1)) | |||
46852 | return SDValue(); | |||
46853 | Operands[2] = Op0; | |||
46854 | Operands[1] = Op1; | |||
46855 | ||||
46856 | // Now we have three operands of two additions. Check that one of them is a | |||
46857 | // constant vector with ones, and the other two can be promoted from i8/i16. | |||
46858 | for (int i = 0; i < 3; ++i) { | |||
46859 | if (!IsConstVectorInRange(Operands[i], 1, 1)) | |||
46860 | continue; | |||
46861 | std::swap(Operands[i], Operands[2]); | |||
46862 | ||||
46863 | // Check if Operands[0] and Operands[1] are results of type promotion. | |||
46864 | for (int j = 0; j < 2; ++j) | |||
46865 | if (Operands[j].getValueType() != VT) | |||
46866 | if (!IsZExtLike(Operands[j])) | |||
46867 | return SDValue(); | |||
46868 | ||||
46869 | // The pattern is detected, emit X86ISD::AVG instruction(s). | |||
46870 | return AVGSplitter({Operands[0], Operands[1]}); | |||
46871 | } | |||
46872 | ||||
46873 | return SDValue(); | |||
46874 | } | |||
46875 | ||||
46876 | static SDValue combineLoad(SDNode *N, SelectionDAG &DAG, | |||
46877 | TargetLowering::DAGCombinerInfo &DCI, | |||
46878 | const X86Subtarget &Subtarget) { | |||
46879 | LoadSDNode *Ld = cast<LoadSDNode>(N); | |||
46880 | EVT RegVT = Ld->getValueType(0); | |||
46881 | EVT MemVT = Ld->getMemoryVT(); | |||
46882 | SDLoc dl(Ld); | |||
46883 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
46884 | ||||
46885 | // For chips with slow 32-byte unaligned loads, break the 32-byte operation | |||
46886 | // into two 16-byte operations. Also split non-temporal aligned loads on | |||
46887 | // pre-AVX2 targets as 32-byte loads will lower to regular temporal loads. | |||
46888 | ISD::LoadExtType Ext = Ld->getExtensionType(); | |||
46889 | bool Fast; | |||
46890 | if (RegVT.is256BitVector() && !DCI.isBeforeLegalizeOps() && | |||
46891 | Ext == ISD::NON_EXTLOAD && | |||
46892 | ((Ld->isNonTemporal() && !Subtarget.hasInt256() && | |||
46893 | Ld->getAlignment() >= 16) || | |||
46894 | (TLI.allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), RegVT, | |||
46895 | *Ld->getMemOperand(), &Fast) && | |||
46896 | !Fast))) { | |||
46897 | unsigned NumElems = RegVT.getVectorNumElements(); | |||
46898 | if (NumElems < 2) | |||
46899 | return SDValue(); | |||
46900 | ||||
46901 | unsigned HalfOffset = 16; | |||
46902 | SDValue Ptr1 = Ld->getBasePtr(); | |||
46903 | SDValue Ptr2 = | |||
46904 | DAG.getMemBasePlusOffset(Ptr1, TypeSize::Fixed(HalfOffset), dl); | |||
46905 | EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(), | |||
46906 | NumElems / 2); | |||
46907 | SDValue Load1 = | |||
46908 | DAG.getLoad(HalfVT, dl, Ld->getChain(), Ptr1, Ld->getPointerInfo(), | |||
46909 | Ld->getOriginalAlign(), | |||
46910 | Ld->getMemOperand()->getFlags()); | |||
46911 | SDValue Load2 = DAG.getLoad(HalfVT, dl, Ld->getChain(), Ptr2, | |||
46912 | Ld->getPointerInfo().getWithOffset(HalfOffset), | |||
46913 | Ld->getOriginalAlign(), | |||
46914 | Ld->getMemOperand()->getFlags()); | |||
46915 | SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, | |||
46916 | Load1.getValue(1), Load2.getValue(1)); | |||
46917 | ||||
46918 | SDValue NewVec = DAG.getNode(ISD::CONCAT_VECTORS, dl, RegVT, Load1, Load2); | |||
46919 | return DCI.CombineTo(N, NewVec, TF, true); | |||
46920 | } | |||
46921 | ||||
46922 | // Bool vector load - attempt to cast to an integer, as we have good | |||
46923 | // (vXiY *ext(vXi1 bitcast(iX))) handling. | |||
46924 | if (Ext == ISD::NON_EXTLOAD && !Subtarget.hasAVX512() && RegVT.isVector() && | |||
46925 | RegVT.getScalarType() == MVT::i1 && DCI.isBeforeLegalize()) { | |||
46926 | unsigned NumElts = RegVT.getVectorNumElements(); | |||
46927 | EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), NumElts); | |||
46928 | if (TLI.isTypeLegal(IntVT)) { | |||
46929 | SDValue IntLoad = DAG.getLoad(IntVT, dl, Ld->getChain(), Ld->getBasePtr(), | |||
46930 | Ld->getPointerInfo(), | |||
46931 | Ld->getOriginalAlign(), | |||
46932 | Ld->getMemOperand()->getFlags()); | |||
46933 | SDValue BoolVec = DAG.getBitcast(RegVT, IntLoad); | |||
46934 | return DCI.CombineTo(N, BoolVec, IntLoad.getValue(1), true); | |||
46935 | } | |||
46936 | } | |||
46937 | ||||
46938 | // If we also broadcast this as a subvector to a wider type, then just extract | |||
46939 | // the lowest subvector. | |||
46940 | if (Ext == ISD::NON_EXTLOAD && Subtarget.hasAVX() && Ld->isSimple() && | |||
46941 | (RegVT.is128BitVector() || RegVT.is256BitVector())) { | |||
46942 | SDValue Ptr = Ld->getBasePtr(); | |||
46943 | SDValue Chain = Ld->getChain(); | |||
46944 | for (SDNode *User : Ptr->uses()) { | |||
46945 | if (User != N && User->getOpcode() == X86ISD::SUBV_BROADCAST_LOAD && | |||
46946 | cast<MemIntrinsicSDNode>(User)->getBasePtr() == Ptr && | |||
46947 | cast<MemIntrinsicSDNode>(User)->getChain() == Chain && | |||
46948 | cast<MemIntrinsicSDNode>(User)->getMemoryVT().getSizeInBits() == | |||
46949 | MemVT.getSizeInBits() && | |||
46950 | !User->hasAnyUseOfValue(1) && | |||
46951 | User->getValueSizeInBits(0).getFixedSize() > | |||
46952 | RegVT.getFixedSizeInBits()) { | |||
46953 | SDValue Extract = extractSubVector(SDValue(User, 0), 0, DAG, SDLoc(N), | |||
46954 | RegVT.getSizeInBits()); | |||
46955 | Extract = DAG.getBitcast(RegVT, Extract); | |||
46956 | return DCI.CombineTo(N, Extract, SDValue(User, 1)); | |||
46957 | } | |||
46958 | } | |||
46959 | } | |||
46960 | ||||
46961 | // Cast ptr32 and ptr64 pointers to the default address space before a load. | |||
46962 | unsigned AddrSpace = Ld->getAddressSpace(); | |||
46963 | if (AddrSpace == X86AS::PTR64 || AddrSpace == X86AS::PTR32_SPTR || | |||
46964 | AddrSpace == X86AS::PTR32_UPTR) { | |||
46965 | MVT PtrVT = TLI.getPointerTy(DAG.getDataLayout()); | |||
46966 | if (PtrVT != Ld->getBasePtr().getSimpleValueType()) { | |||
46967 | SDValue Cast = | |||
46968 | DAG.getAddrSpaceCast(dl, PtrVT, Ld->getBasePtr(), AddrSpace, 0); | |||
46969 | return DAG.getLoad(RegVT, dl, Ld->getChain(), Cast, Ld->getPointerInfo(), | |||
46970 | Ld->getOriginalAlign(), | |||
46971 | Ld->getMemOperand()->getFlags()); | |||
46972 | } | |||
46973 | } | |||
46974 | ||||
46975 | return SDValue(); | |||
46976 | } | |||
46977 | ||||
46978 | /// If V is a build vector of boolean constants and exactly one of those | |||
46979 | /// constants is true, return the operand index of that true element. | |||
46980 | /// Otherwise, return -1. | |||
46981 | static int getOneTrueElt(SDValue V) { | |||
46982 | // This needs to be a build vector of booleans. | |||
46983 | // TODO: Checking for the i1 type matches the IR definition for the mask, | |||
46984 | // but the mask check could be loosened to i8 or other types. That might | |||
46985 | // also require checking more than 'allOnesValue'; eg, the x86 HW | |||
46986 | // instructions only require that the MSB is set for each mask element. | |||
46987 | // The ISD::MSTORE comments/definition do not specify how the mask operand | |||
46988 | // is formatted. | |||
46989 | auto *BV = dyn_cast<BuildVectorSDNode>(V); | |||
46990 | if (!BV || BV->getValueType(0).getVectorElementType() != MVT::i1) | |||
46991 | return -1; | |||
46992 | ||||
46993 | int TrueIndex = -1; | |||
46994 | unsigned NumElts = BV->getValueType(0).getVectorNumElements(); | |||
46995 | for (unsigned i = 0; i < NumElts; ++i) { | |||
46996 | const SDValue &Op = BV->getOperand(i); | |||
46997 | if (Op.isUndef()) | |||
46998 | continue; | |||
46999 | auto *ConstNode = dyn_cast<ConstantSDNode>(Op); | |||
47000 | if (!ConstNode) | |||
47001 | return -1; | |||
47002 | if (ConstNode->getAPIntValue().countTrailingOnes() >= 1) { | |||
47003 | // If we already found a one, this is too many. | |||
47004 | if (TrueIndex >= 0) | |||
47005 | return -1; | |||
47006 | TrueIndex = i; | |||
47007 | } | |||
47008 | } | |||
47009 | return TrueIndex; | |||
47010 | } | |||
47011 | ||||
47012 | /// Given a masked memory load/store operation, return true if it has one mask | |||
47013 | /// bit set. If it has one mask bit set, then also return the memory address of | |||
47014 | /// the scalar element to load/store, the vector index to insert/extract that | |||
47015 | /// scalar element, and the alignment for the scalar memory access. | |||
47016 | static bool getParamsForOneTrueMaskedElt(MaskedLoadStoreSDNode *MaskedOp, | |||
47017 | SelectionDAG &DAG, SDValue &Addr, | |||
47018 | SDValue &Index, Align &Alignment, | |||
47019 | unsigned &Offset) { | |||
47020 | int TrueMaskElt = getOneTrueElt(MaskedOp->getMask()); | |||
47021 | if (TrueMaskElt < 0) | |||
47022 | return false; | |||
47023 | ||||
47024 | // Get the address of the one scalar element that is specified by the mask | |||
47025 | // using the appropriate offset from the base pointer. | |||
47026 | EVT EltVT = MaskedOp->getMemoryVT().getVectorElementType(); | |||
47027 | Offset = 0; | |||
47028 | Addr = MaskedOp->getBasePtr(); | |||
47029 | if (TrueMaskElt != 0) { | |||
47030 | Offset = TrueMaskElt * EltVT.getStoreSize(); | |||
47031 | Addr = DAG.getMemBasePlusOffset(Addr, TypeSize::Fixed(Offset), | |||
47032 | SDLoc(MaskedOp)); | |||
47033 | } | |||
47034 | ||||
47035 | Index = DAG.getIntPtrConstant(TrueMaskElt, SDLoc(MaskedOp)); | |||
47036 | Alignment = commonAlignment(MaskedOp->getOriginalAlign(), | |||
47037 | EltVT.getStoreSize()); | |||
47038 | return true; | |||
47039 | } | |||
47040 | ||||
47041 | /// If exactly one element of the mask is set for a non-extending masked load, | |||
47042 | /// it is a scalar load and vector insert. | |||
47043 | /// Note: It is expected that the degenerate cases of an all-zeros or all-ones | |||
47044 | /// mask have already been optimized in IR, so we don't bother with those here. | |||
47045 | static SDValue | |||
47046 | reduceMaskedLoadToScalarLoad(MaskedLoadSDNode *ML, SelectionDAG &DAG, | |||
47047 | TargetLowering::DAGCombinerInfo &DCI, | |||
47048 | const X86Subtarget &Subtarget) { | |||
47049 | assert(ML->isUnindexed() && "Unexpected indexed masked load!")(static_cast <bool> (ML->isUnindexed() && "Unexpected indexed masked load!" ) ? void (0) : __assert_fail ("ML->isUnindexed() && \"Unexpected indexed masked load!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 47049, __extension__ __PRETTY_FUNCTION__)); | |||
47050 | // TODO: This is not x86-specific, so it could be lifted to DAGCombiner. | |||
47051 | // However, some target hooks may need to be added to know when the transform | |||
47052 | // is profitable. Endianness would also have to be considered. | |||
47053 | ||||
47054 | SDValue Addr, VecIndex; | |||
47055 | Align Alignment; | |||
47056 | unsigned Offset; | |||
47057 | if (!getParamsForOneTrueMaskedElt(ML, DAG, Addr, VecIndex, Alignment, Offset)) | |||
47058 | return SDValue(); | |||
47059 | ||||
47060 | // Load the one scalar element that is specified by the mask using the | |||
47061 | // appropriate offset from the base pointer. | |||
47062 | SDLoc DL(ML); | |||
47063 | EVT VT = ML->getValueType(0); | |||
47064 | EVT EltVT = VT.getVectorElementType(); | |||
47065 | ||||
47066 | EVT CastVT = VT; | |||
47067 | if (EltVT == MVT::i64 && !Subtarget.is64Bit()) { | |||
47068 | EltVT = MVT::f64; | |||
47069 | CastVT = VT.changeVectorElementType(EltVT); | |||
47070 | } | |||
47071 | ||||
47072 | SDValue Load = | |||
47073 | DAG.getLoad(EltVT, DL, ML->getChain(), Addr, | |||
47074 | ML->getPointerInfo().getWithOffset(Offset), | |||
47075 | Alignment, ML->getMemOperand()->getFlags()); | |||
47076 | ||||
47077 | SDValue PassThru = DAG.getBitcast(CastVT, ML->getPassThru()); | |||
47078 | ||||
47079 | // Insert the loaded element into the appropriate place in the vector. | |||
47080 | SDValue Insert = | |||
47081 | DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, CastVT, PassThru, Load, VecIndex); | |||
47082 | Insert = DAG.getBitcast(VT, Insert); | |||
47083 | return DCI.CombineTo(ML, Insert, Load.getValue(1), true); | |||
47084 | } | |||
47085 | ||||
47086 | static SDValue | |||
47087 | combineMaskedLoadConstantMask(MaskedLoadSDNode *ML, SelectionDAG &DAG, | |||
47088 | TargetLowering::DAGCombinerInfo &DCI) { | |||
47089 | assert(ML->isUnindexed() && "Unexpected indexed masked load!")(static_cast <bool> (ML->isUnindexed() && "Unexpected indexed masked load!" ) ? void (0) : __assert_fail ("ML->isUnindexed() && \"Unexpected indexed masked load!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 47089, __extension__ __PRETTY_FUNCTION__)); | |||
47090 | if (!ISD::isBuildVectorOfConstantSDNodes(ML->getMask().getNode())) | |||
47091 | return SDValue(); | |||
47092 | ||||
47093 | SDLoc DL(ML); | |||
47094 | EVT VT = ML->getValueType(0); | |||
47095 | ||||
47096 | // If we are loading the first and last elements of a vector, it is safe and | |||
47097 | // always faster to load the whole vector. Replace the masked load with a | |||
47098 | // vector load and select. | |||
47099 | unsigned NumElts = VT.getVectorNumElements(); | |||
47100 | BuildVectorSDNode *MaskBV = cast<BuildVectorSDNode>(ML->getMask()); | |||
47101 | bool LoadFirstElt = !isNullConstant(MaskBV->getOperand(0)); | |||
47102 | bool LoadLastElt = !isNullConstant(MaskBV->getOperand(NumElts - 1)); | |||
47103 | if (LoadFirstElt && LoadLastElt) { | |||
47104 | SDValue VecLd = DAG.getLoad(VT, DL, ML->getChain(), ML->getBasePtr(), | |||
47105 | ML->getMemOperand()); | |||
47106 | SDValue Blend = DAG.getSelect(DL, VT, ML->getMask(), VecLd, | |||
47107 | ML->getPassThru()); | |||
47108 | return DCI.CombineTo(ML, Blend, VecLd.getValue(1), true); | |||
47109 | } | |||
47110 | ||||
47111 | // Convert a masked load with a constant mask into a masked load and a select. | |||
47112 | // This allows the select operation to use a faster kind of select instruction | |||
47113 | // (for example, vblendvps -> vblendps). | |||
47114 | ||||
47115 | // Don't try this if the pass-through operand is already undefined. That would | |||
47116 | // cause an infinite loop because that's what we're about to create. | |||
47117 | if (ML->getPassThru().isUndef()) | |||
47118 | return SDValue(); | |||
47119 | ||||
47120 | if (ISD::isBuildVectorAllZeros(ML->getPassThru().getNode())) | |||
47121 | return SDValue(); | |||
47122 | ||||
47123 | // The new masked load has an undef pass-through operand. The select uses the | |||
47124 | // original pass-through operand. | |||
47125 | SDValue NewML = DAG.getMaskedLoad( | |||
47126 | VT, DL, ML->getChain(), ML->getBasePtr(), ML->getOffset(), ML->getMask(), | |||
47127 | DAG.getUNDEF(VT), ML->getMemoryVT(), ML->getMemOperand(), | |||
47128 | ML->getAddressingMode(), ML->getExtensionType()); | |||
47129 | SDValue Blend = DAG.getSelect(DL, VT, ML->getMask(), NewML, | |||
47130 | ML->getPassThru()); | |||
47131 | ||||
47132 | return DCI.CombineTo(ML, Blend, NewML.getValue(1), true); | |||
47133 | } | |||
47134 | ||||
47135 | static SDValue combineMaskedLoad(SDNode *N, SelectionDAG &DAG, | |||
47136 | TargetLowering::DAGCombinerInfo &DCI, | |||
47137 | const X86Subtarget &Subtarget) { | |||
47138 | auto *Mld = cast<MaskedLoadSDNode>(N); | |||
47139 | ||||
47140 | // TODO: Expanding load with constant mask may be optimized as well. | |||
47141 | if (Mld->isExpandingLoad()) | |||
47142 | return SDValue(); | |||
47143 | ||||
47144 | if (Mld->getExtensionType() == ISD::NON_EXTLOAD) { | |||
47145 | if (SDValue ScalarLoad = | |||
47146 | reduceMaskedLoadToScalarLoad(Mld, DAG, DCI, Subtarget)) | |||
47147 | return ScalarLoad; | |||
47148 | ||||
47149 | // TODO: Do some AVX512 subsets benefit from this transform? | |||
47150 | if (!Subtarget.hasAVX512()) | |||
47151 | if (SDValue Blend = combineMaskedLoadConstantMask(Mld, DAG, DCI)) | |||
47152 | return Blend; | |||
47153 | } | |||
47154 | ||||
47155 | // If the mask value has been legalized to a non-boolean vector, try to | |||
47156 | // simplify ops leading up to it. We only demand the MSB of each lane. | |||
47157 | SDValue Mask = Mld->getMask(); | |||
47158 | if (Mask.getScalarValueSizeInBits() != 1) { | |||
47159 | EVT VT = Mld->getValueType(0); | |||
47160 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
47161 | APInt DemandedBits(APInt::getSignMask(VT.getScalarSizeInBits())); | |||
47162 | if (TLI.SimplifyDemandedBits(Mask, DemandedBits, DCI)) { | |||
47163 | if (N->getOpcode() != ISD::DELETED_NODE) | |||
47164 | DCI.AddToWorklist(N); | |||
47165 | return SDValue(N, 0); | |||
47166 | } | |||
47167 | if (SDValue NewMask = | |||
47168 | TLI.SimplifyMultipleUseDemandedBits(Mask, DemandedBits, DAG)) | |||
47169 | return DAG.getMaskedLoad( | |||
47170 | VT, SDLoc(N), Mld->getChain(), Mld->getBasePtr(), Mld->getOffset(), | |||
47171 | NewMask, Mld->getPassThru(), Mld->getMemoryVT(), Mld->getMemOperand(), | |||
47172 | Mld->getAddressingMode(), Mld->getExtensionType()); | |||
47173 | } | |||
47174 | ||||
47175 | return SDValue(); | |||
47176 | } | |||
47177 | ||||
47178 | /// If exactly one element of the mask is set for a non-truncating masked store, | |||
47179 | /// it is a vector extract and scalar store. | |||
47180 | /// Note: It is expected that the degenerate cases of an all-zeros or all-ones | |||
47181 | /// mask have already been optimized in IR, so we don't bother with those here. | |||
47182 | static SDValue reduceMaskedStoreToScalarStore(MaskedStoreSDNode *MS, | |||
47183 | SelectionDAG &DAG, | |||
47184 | const X86Subtarget &Subtarget) { | |||
47185 | // TODO: This is not x86-specific, so it could be lifted to DAGCombiner. | |||
47186 | // However, some target hooks may need to be added to know when the transform | |||
47187 | // is profitable. Endianness would also have to be considered. | |||
47188 | ||||
47189 | SDValue Addr, VecIndex; | |||
47190 | Align Alignment; | |||
47191 | unsigned Offset; | |||
47192 | if (!getParamsForOneTrueMaskedElt(MS, DAG, Addr, VecIndex, Alignment, Offset)) | |||
47193 | return SDValue(); | |||
47194 | ||||
47195 | // Extract the one scalar element that is actually being stored. | |||
47196 | SDLoc DL(MS); | |||
47197 | SDValue Value = MS->getValue(); | |||
47198 | EVT VT = Value.getValueType(); | |||
47199 | EVT EltVT = VT.getVectorElementType(); | |||
47200 | if (EltVT == MVT::i64 && !Subtarget.is64Bit()) { | |||
47201 | EltVT = MVT::f64; | |||
47202 | EVT CastVT = VT.changeVectorElementType(EltVT); | |||
47203 | Value = DAG.getBitcast(CastVT, Value); | |||
47204 | } | |||
47205 | SDValue Extract = | |||
47206 | DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, Value, VecIndex); | |||
47207 | ||||
47208 | // Store that element at the appropriate offset from the base pointer. | |||
47209 | return DAG.getStore(MS->getChain(), DL, Extract, Addr, | |||
47210 | MS->getPointerInfo().getWithOffset(Offset), | |||
47211 | Alignment, MS->getMemOperand()->getFlags()); | |||
47212 | } | |||
47213 | ||||
47214 | static SDValue combineMaskedStore(SDNode *N, SelectionDAG &DAG, | |||
47215 | TargetLowering::DAGCombinerInfo &DCI, | |||
47216 | const X86Subtarget &Subtarget) { | |||
47217 | MaskedStoreSDNode *Mst = cast<MaskedStoreSDNode>(N); | |||
47218 | if (Mst->isCompressingStore()) | |||
47219 | return SDValue(); | |||
47220 | ||||
47221 | EVT VT = Mst->getValue().getValueType(); | |||
47222 | SDLoc dl(Mst); | |||
47223 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
47224 | ||||
47225 | if (Mst->isTruncatingStore()) | |||
47226 | return SDValue(); | |||
47227 | ||||
47228 | if (SDValue ScalarStore = reduceMaskedStoreToScalarStore(Mst, DAG, Subtarget)) | |||
47229 | return ScalarStore; | |||
47230 | ||||
47231 | // If the mask value has been legalized to a non-boolean vector, try to | |||
47232 | // simplify ops leading up to it. We only demand the MSB of each lane. | |||
47233 | SDValue Mask = Mst->getMask(); | |||
47234 | if (Mask.getScalarValueSizeInBits() != 1) { | |||
47235 | APInt DemandedBits(APInt::getSignMask(VT.getScalarSizeInBits())); | |||
47236 | if (TLI.SimplifyDemandedBits(Mask, DemandedBits, DCI)) { | |||
47237 | if (N->getOpcode() != ISD::DELETED_NODE) | |||
47238 | DCI.AddToWorklist(N); | |||
47239 | return SDValue(N, 0); | |||
47240 | } | |||
47241 | if (SDValue NewMask = | |||
47242 | TLI.SimplifyMultipleUseDemandedBits(Mask, DemandedBits, DAG)) | |||
47243 | return DAG.getMaskedStore(Mst->getChain(), SDLoc(N), Mst->getValue(), | |||
47244 | Mst->getBasePtr(), Mst->getOffset(), NewMask, | |||
47245 | Mst->getMemoryVT(), Mst->getMemOperand(), | |||
47246 | Mst->getAddressingMode()); | |||
47247 | } | |||
47248 | ||||
47249 | SDValue Value = Mst->getValue(); | |||
47250 | if (Value.getOpcode() == ISD::TRUNCATE && Value.getNode()->hasOneUse() && | |||
47251 | TLI.isTruncStoreLegal(Value.getOperand(0).getValueType(), | |||
47252 | Mst->getMemoryVT())) { | |||
47253 | return DAG.getMaskedStore(Mst->getChain(), SDLoc(N), Value.getOperand(0), | |||
47254 | Mst->getBasePtr(), Mst->getOffset(), Mask, | |||
47255 | Mst->getMemoryVT(), Mst->getMemOperand(), | |||
47256 | Mst->getAddressingMode(), true); | |||
47257 | } | |||
47258 | ||||
47259 | return SDValue(); | |||
47260 | } | |||
47261 | ||||
47262 | static SDValue combineStore(SDNode *N, SelectionDAG &DAG, | |||
47263 | TargetLowering::DAGCombinerInfo &DCI, | |||
47264 | const X86Subtarget &Subtarget) { | |||
47265 | StoreSDNode *St = cast<StoreSDNode>(N); | |||
47266 | EVT StVT = St->getMemoryVT(); | |||
47267 | SDLoc dl(St); | |||
47268 | SDValue StoredVal = St->getValue(); | |||
47269 | EVT VT = StoredVal.getValueType(); | |||
47270 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
47271 | ||||
47272 | // Convert a store of vXi1 into a store of iX and a bitcast. | |||
47273 | if (!Subtarget.hasAVX512() && VT == StVT && VT.isVector() && | |||
47274 | VT.getVectorElementType() == MVT::i1) { | |||
47275 | ||||
47276 | EVT NewVT = EVT::getIntegerVT(*DAG.getContext(), VT.getVectorNumElements()); | |||
47277 | StoredVal = DAG.getBitcast(NewVT, StoredVal); | |||
47278 | ||||
47279 | return DAG.getStore(St->getChain(), dl, StoredVal, St->getBasePtr(), | |||
47280 | St->getPointerInfo(), St->getOriginalAlign(), | |||
47281 | St->getMemOperand()->getFlags()); | |||
47282 | } | |||
47283 | ||||
47284 | // If this is a store of a scalar_to_vector to v1i1, just use a scalar store. | |||
47285 | // This will avoid a copy to k-register. | |||
47286 | if (VT == MVT::v1i1 && VT == StVT && Subtarget.hasAVX512() && | |||
47287 | StoredVal.getOpcode() == ISD::SCALAR_TO_VECTOR && | |||
47288 | StoredVal.getOperand(0).getValueType() == MVT::i8) { | |||
47289 | SDValue Val = StoredVal.getOperand(0); | |||
47290 | // We must store zeros to the unused bits. | |||
47291 | Val = DAG.getZeroExtendInReg(Val, dl, MVT::i1); | |||
47292 | return DAG.getStore(St->getChain(), dl, Val, | |||
47293 | St->getBasePtr(), St->getPointerInfo(), | |||
47294 | St->getOriginalAlign(), | |||
47295 | St->getMemOperand()->getFlags()); | |||
47296 | } | |||
47297 | ||||
47298 | // Widen v2i1/v4i1 stores to v8i1. | |||
47299 | if ((VT == MVT::v1i1 || VT == MVT::v2i1 || VT == MVT::v4i1) && VT == StVT && | |||
47300 | Subtarget.hasAVX512()) { | |||
47301 | unsigned NumConcats = 8 / VT.getVectorNumElements(); | |||
47302 | // We must store zeros to the unused bits. | |||
47303 | SmallVector<SDValue, 4> Ops(NumConcats, DAG.getConstant(0, dl, VT)); | |||
47304 | Ops[0] = StoredVal; | |||
47305 | StoredVal = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i1, Ops); | |||
47306 | return DAG.getStore(St->getChain(), dl, StoredVal, St->getBasePtr(), | |||
47307 | St->getPointerInfo(), St->getOriginalAlign(), | |||
47308 | St->getMemOperand()->getFlags()); | |||
47309 | } | |||
47310 | ||||
47311 | // Turn vXi1 stores of constants into a scalar store. | |||
47312 | if ((VT == MVT::v8i1 || VT == MVT::v16i1 || VT == MVT::v32i1 || | |||
47313 | VT == MVT::v64i1) && VT == StVT && TLI.isTypeLegal(VT) && | |||
47314 | ISD::isBuildVectorOfConstantSDNodes(StoredVal.getNode())) { | |||
47315 | // If its a v64i1 store without 64-bit support, we need two stores. | |||
47316 | if (!DCI.isBeforeLegalize() && VT == MVT::v64i1 && !Subtarget.is64Bit()) { | |||
47317 | SDValue Lo = DAG.getBuildVector(MVT::v32i1, dl, | |||
47318 | StoredVal->ops().slice(0, 32)); | |||
47319 | Lo = combinevXi1ConstantToInteger(Lo, DAG); | |||
47320 | SDValue Hi = DAG.getBuildVector(MVT::v32i1, dl, | |||
47321 | StoredVal->ops().slice(32, 32)); | |||
47322 | Hi = combinevXi1ConstantToInteger(Hi, DAG); | |||
47323 | ||||
47324 | SDValue Ptr0 = St->getBasePtr(); | |||
47325 | SDValue Ptr1 = DAG.getMemBasePlusOffset(Ptr0, TypeSize::Fixed(4), dl); | |||
47326 | ||||
47327 | SDValue Ch0 = | |||
47328 | DAG.getStore(St->getChain(), dl, Lo, Ptr0, St->getPointerInfo(), | |||
47329 | St->getOriginalAlign(), | |||
47330 | St->getMemOperand()->getFlags()); | |||
47331 | SDValue Ch1 = | |||
47332 | DAG.getStore(St->getChain(), dl, Hi, Ptr1, | |||
47333 | St->getPointerInfo().getWithOffset(4), | |||
47334 | St->getOriginalAlign(), | |||
47335 | St->getMemOperand()->getFlags()); | |||
47336 | return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1); | |||
47337 | } | |||
47338 | ||||
47339 | StoredVal = combinevXi1ConstantToInteger(StoredVal, DAG); | |||
47340 | return DAG.getStore(St->getChain(), dl, StoredVal, St->getBasePtr(), | |||
47341 | St->getPointerInfo(), St->getOriginalAlign(), | |||
47342 | St->getMemOperand()->getFlags()); | |||
47343 | } | |||
47344 | ||||
47345 | // If we are saving a 32-byte vector and 32-byte stores are slow, such as on | |||
47346 | // Sandy Bridge, perform two 16-byte stores. | |||
47347 | bool Fast; | |||
47348 | if (VT.is256BitVector() && StVT == VT && | |||
47349 | TLI.allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), VT, | |||
47350 | *St->getMemOperand(), &Fast) && | |||
47351 | !Fast) { | |||
47352 | unsigned NumElems = VT.getVectorNumElements(); | |||
47353 | if (NumElems < 2) | |||
47354 | return SDValue(); | |||
47355 | ||||
47356 | return splitVectorStore(St, DAG); | |||
47357 | } | |||
47358 | ||||
47359 | // Split under-aligned vector non-temporal stores. | |||
47360 | if (St->isNonTemporal() && StVT == VT && | |||
47361 | St->getAlignment() < VT.getStoreSize()) { | |||
47362 | // ZMM/YMM nt-stores - either it can be stored as a series of shorter | |||
47363 | // vectors or the legalizer can scalarize it to use MOVNTI. | |||
47364 | if (VT.is256BitVector() || VT.is512BitVector()) { | |||
47365 | unsigned NumElems = VT.getVectorNumElements(); | |||
47366 | if (NumElems < 2) | |||
47367 | return SDValue(); | |||
47368 | return splitVectorStore(St, DAG); | |||
47369 | } | |||
47370 | ||||
47371 | // XMM nt-stores - scalarize this to f64 nt-stores on SSE4A, else i32/i64 | |||
47372 | // to use MOVNTI. | |||
47373 | if (VT.is128BitVector() && Subtarget.hasSSE2()) { | |||
47374 | MVT NTVT = Subtarget.hasSSE4A() | |||
47375 | ? MVT::v2f64 | |||
47376 | : (TLI.isTypeLegal(MVT::i64) ? MVT::v2i64 : MVT::v4i32); | |||
47377 | return scalarizeVectorStore(St, NTVT, DAG); | |||
47378 | } | |||
47379 | } | |||
47380 | ||||
47381 | // Try to optimize v16i16->v16i8 truncating stores when BWI is not | |||
47382 | // supported, but avx512f is by extending to v16i32 and truncating. | |||
47383 | if (!St->isTruncatingStore() && VT == MVT::v16i8 && !Subtarget.hasBWI() && | |||
47384 | St->getValue().getOpcode() == ISD::TRUNCATE && | |||
47385 | St->getValue().getOperand(0).getValueType() == MVT::v16i16 && | |||
47386 | TLI.isTruncStoreLegal(MVT::v16i32, MVT::v16i8) && | |||
47387 | St->getValue().hasOneUse() && !DCI.isBeforeLegalizeOps()) { | |||
47388 | SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::v16i32, St->getValue()); | |||
47389 | return DAG.getTruncStore(St->getChain(), dl, Ext, St->getBasePtr(), | |||
47390 | MVT::v16i8, St->getMemOperand()); | |||
47391 | } | |||
47392 | ||||
47393 | // Try to fold a VTRUNCUS or VTRUNCS into a truncating store. | |||
47394 | if (!St->isTruncatingStore() && StoredVal.hasOneUse() && | |||
47395 | (StoredVal.getOpcode() == X86ISD::VTRUNCUS || | |||
47396 | StoredVal.getOpcode() == X86ISD::VTRUNCS) && | |||
47397 | TLI.isTruncStoreLegal(StoredVal.getOperand(0).getValueType(), VT)) { | |||
47398 | bool IsSigned = StoredVal.getOpcode() == X86ISD::VTRUNCS; | |||
47399 | return EmitTruncSStore(IsSigned, St->getChain(), | |||
47400 | dl, StoredVal.getOperand(0), St->getBasePtr(), | |||
47401 | VT, St->getMemOperand(), DAG); | |||
47402 | } | |||
47403 | ||||
47404 | // Try to fold a extract_element(VTRUNC) pattern into a truncating store. | |||
47405 | if (!St->isTruncatingStore() && StoredVal.hasOneUse()) { | |||
47406 | auto IsExtractedElement = [](SDValue V) { | |||
47407 | if (V.getOpcode() == ISD::TRUNCATE && V.getOperand(0).hasOneUse()) | |||
47408 | V = V.getOperand(0); | |||
47409 | unsigned Opc = V.getOpcode(); | |||
47410 | if (Opc == ISD::EXTRACT_VECTOR_ELT || Opc == X86ISD::PEXTRW) { | |||
47411 | if (V.getOperand(0).hasOneUse() && isNullConstant(V.getOperand(1))) | |||
47412 | return V.getOperand(0); | |||
47413 | } | |||
47414 | return SDValue(); | |||
47415 | }; | |||
47416 | if (SDValue Extract = IsExtractedElement(StoredVal)) { | |||
47417 | SDValue Trunc = peekThroughOneUseBitcasts(Extract); | |||
47418 | if (Trunc.getOpcode() == X86ISD::VTRUNC) { | |||
47419 | SDValue Src = Trunc.getOperand(0); | |||
47420 | MVT DstVT = Trunc.getSimpleValueType(); | |||
47421 | MVT SrcVT = Src.getSimpleValueType(); | |||
47422 | unsigned NumSrcElts = SrcVT.getVectorNumElements(); | |||
47423 | unsigned NumTruncBits = DstVT.getScalarSizeInBits() * NumSrcElts; | |||
47424 | MVT TruncVT = MVT::getVectorVT(DstVT.getScalarType(), NumSrcElts); | |||
47425 | if (NumTruncBits == VT.getSizeInBits() && | |||
47426 | TLI.isTruncStoreLegal(SrcVT, TruncVT)) { | |||
47427 | return DAG.getTruncStore(St->getChain(), dl, Src, St->getBasePtr(), | |||
47428 | TruncVT, St->getMemOperand()); | |||
47429 | } | |||
47430 | } | |||
47431 | } | |||
47432 | } | |||
47433 | ||||
47434 | // Optimize trunc store (of multiple scalars) to shuffle and store. | |||
47435 | // First, pack all of the elements in one place. Next, store to memory | |||
47436 | // in fewer chunks. | |||
47437 | if (St->isTruncatingStore() && VT.isVector()) { | |||
47438 | // Check if we can detect an AVG pattern from the truncation. If yes, | |||
47439 | // replace the trunc store by a normal store with the result of X86ISD::AVG | |||
47440 | // instruction. | |||
47441 | if (DCI.isBeforeLegalize() || TLI.isTypeLegal(St->getMemoryVT())) | |||
47442 | if (SDValue Avg = detectAVGPattern(St->getValue(), St->getMemoryVT(), DAG, | |||
47443 | Subtarget, dl)) | |||
47444 | return DAG.getStore(St->getChain(), dl, Avg, St->getBasePtr(), | |||
47445 | St->getPointerInfo(), St->getOriginalAlign(), | |||
47446 | St->getMemOperand()->getFlags()); | |||
47447 | ||||
47448 | if (TLI.isTruncStoreLegal(VT, StVT)) { | |||
47449 | if (SDValue Val = detectSSatPattern(St->getValue(), St->getMemoryVT())) | |||
47450 | return EmitTruncSStore(true /* Signed saturation */, St->getChain(), | |||
47451 | dl, Val, St->getBasePtr(), | |||
47452 | St->getMemoryVT(), St->getMemOperand(), DAG); | |||
47453 | if (SDValue Val = detectUSatPattern(St->getValue(), St->getMemoryVT(), | |||
47454 | DAG, dl)) | |||
47455 | return EmitTruncSStore(false /* Unsigned saturation */, St->getChain(), | |||
47456 | dl, Val, St->getBasePtr(), | |||
47457 | St->getMemoryVT(), St->getMemOperand(), DAG); | |||
47458 | } | |||
47459 | ||||
47460 | return SDValue(); | |||
47461 | } | |||
47462 | ||||
47463 | // Cast ptr32 and ptr64 pointers to the default address space before a store. | |||
47464 | unsigned AddrSpace = St->getAddressSpace(); | |||
47465 | if (AddrSpace == X86AS::PTR64 || AddrSpace == X86AS::PTR32_SPTR || | |||
47466 | AddrSpace == X86AS::PTR32_UPTR) { | |||
47467 | MVT PtrVT = TLI.getPointerTy(DAG.getDataLayout()); | |||
47468 | if (PtrVT != St->getBasePtr().getSimpleValueType()) { | |||
47469 | SDValue Cast = | |||
47470 | DAG.getAddrSpaceCast(dl, PtrVT, St->getBasePtr(), AddrSpace, 0); | |||
47471 | return DAG.getStore(St->getChain(), dl, StoredVal, Cast, | |||
47472 | St->getPointerInfo(), St->getOriginalAlign(), | |||
47473 | St->getMemOperand()->getFlags(), St->getAAInfo()); | |||
47474 | } | |||
47475 | } | |||
47476 | ||||
47477 | // Turn load->store of MMX types into GPR load/stores. This avoids clobbering | |||
47478 | // the FP state in cases where an emms may be missing. | |||
47479 | // A preferable solution to the general problem is to figure out the right | |||
47480 | // places to insert EMMS. This qualifies as a quick hack. | |||
47481 | ||||
47482 | // Similarly, turn load->store of i64 into double load/stores in 32-bit mode. | |||
47483 | if (VT.getSizeInBits() != 64) | |||
47484 | return SDValue(); | |||
47485 | ||||
47486 | const Function &F = DAG.getMachineFunction().getFunction(); | |||
47487 | bool NoImplicitFloatOps = F.hasFnAttribute(Attribute::NoImplicitFloat); | |||
47488 | bool F64IsLegal = | |||
47489 | !Subtarget.useSoftFloat() && !NoImplicitFloatOps && Subtarget.hasSSE2(); | |||
47490 | if ((VT == MVT::i64 && F64IsLegal && !Subtarget.is64Bit()) && | |||
47491 | isa<LoadSDNode>(St->getValue()) && | |||
47492 | cast<LoadSDNode>(St->getValue())->isSimple() && | |||
47493 | St->getChain().hasOneUse() && St->isSimple()) { | |||
47494 | LoadSDNode *Ld = cast<LoadSDNode>(St->getValue().getNode()); | |||
47495 | ||||
47496 | if (!ISD::isNormalLoad(Ld)) | |||
47497 | return SDValue(); | |||
47498 | ||||
47499 | // Avoid the transformation if there are multiple uses of the loaded value. | |||
47500 | if (!Ld->hasNUsesOfValue(1, 0)) | |||
47501 | return SDValue(); | |||
47502 | ||||
47503 | SDLoc LdDL(Ld); | |||
47504 | SDLoc StDL(N); | |||
47505 | // Lower to a single movq load/store pair. | |||
47506 | SDValue NewLd = DAG.getLoad(MVT::f64, LdDL, Ld->getChain(), | |||
47507 | Ld->getBasePtr(), Ld->getMemOperand()); | |||
47508 | ||||
47509 | // Make sure new load is placed in same chain order. | |||
47510 | DAG.makeEquivalentMemoryOrdering(Ld, NewLd); | |||
47511 | return DAG.getStore(St->getChain(), StDL, NewLd, St->getBasePtr(), | |||
47512 | St->getMemOperand()); | |||
47513 | } | |||
47514 | ||||
47515 | // This is similar to the above case, but here we handle a scalar 64-bit | |||
47516 | // integer store that is extracted from a vector on a 32-bit target. | |||
47517 | // If we have SSE2, then we can treat it like a floating-point double | |||
47518 | // to get past legalization. The execution dependencies fixup pass will | |||
47519 | // choose the optimal machine instruction for the store if this really is | |||
47520 | // an integer or v2f32 rather than an f64. | |||
47521 | if (VT == MVT::i64 && F64IsLegal && !Subtarget.is64Bit() && | |||
47522 | St->getOperand(1).getOpcode() == ISD::EXTRACT_VECTOR_ELT) { | |||
47523 | SDValue OldExtract = St->getOperand(1); | |||
47524 | SDValue ExtOp0 = OldExtract.getOperand(0); | |||
47525 | unsigned VecSize = ExtOp0.getValueSizeInBits(); | |||
47526 | EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, VecSize / 64); | |||
47527 | SDValue BitCast = DAG.getBitcast(VecVT, ExtOp0); | |||
47528 | SDValue NewExtract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, | |||
47529 | BitCast, OldExtract.getOperand(1)); | |||
47530 | return DAG.getStore(St->getChain(), dl, NewExtract, St->getBasePtr(), | |||
47531 | St->getPointerInfo(), St->getOriginalAlign(), | |||
47532 | St->getMemOperand()->getFlags()); | |||
47533 | } | |||
47534 | ||||
47535 | return SDValue(); | |||
47536 | } | |||
47537 | ||||
47538 | static SDValue combineVEXTRACT_STORE(SDNode *N, SelectionDAG &DAG, | |||
47539 | TargetLowering::DAGCombinerInfo &DCI, | |||
47540 | const X86Subtarget &Subtarget) { | |||
47541 | auto *St = cast<MemIntrinsicSDNode>(N); | |||
47542 | ||||
47543 | SDValue StoredVal = N->getOperand(1); | |||
47544 | MVT VT = StoredVal.getSimpleValueType(); | |||
47545 | EVT MemVT = St->getMemoryVT(); | |||
47546 | ||||
47547 | // Figure out which elements we demand. | |||
47548 | unsigned StElts = MemVT.getSizeInBits() / VT.getScalarSizeInBits(); | |||
47549 | APInt DemandedElts = APInt::getLowBitsSet(VT.getVectorNumElements(), StElts); | |||
47550 | ||||
47551 | APInt KnownUndef, KnownZero; | |||
47552 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
47553 | if (TLI.SimplifyDemandedVectorElts(StoredVal, DemandedElts, KnownUndef, | |||
47554 | KnownZero, DCI)) { | |||
47555 | if (N->getOpcode() != ISD::DELETED_NODE) | |||
47556 | DCI.AddToWorklist(N); | |||
47557 | return SDValue(N, 0); | |||
47558 | } | |||
47559 | ||||
47560 | return SDValue(); | |||
47561 | } | |||
47562 | ||||
47563 | /// Return 'true' if this vector operation is "horizontal" | |||
47564 | /// and return the operands for the horizontal operation in LHS and RHS. A | |||
47565 | /// horizontal operation performs the binary operation on successive elements | |||
47566 | /// of its first operand, then on successive elements of its second operand, | |||
47567 | /// returning the resulting values in a vector. For example, if | |||
47568 | /// A = < float a0, float a1, float a2, float a3 > | |||
47569 | /// and | |||
47570 | /// B = < float b0, float b1, float b2, float b3 > | |||
47571 | /// then the result of doing a horizontal operation on A and B is | |||
47572 | /// A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >. | |||
47573 | /// In short, LHS and RHS are inspected to see if LHS op RHS is of the form | |||
47574 | /// A horizontal-op B, for some already available A and B, and if so then LHS is | |||
47575 | /// set to A, RHS to B, and the routine returns 'true'. | |||
47576 | static bool isHorizontalBinOp(unsigned HOpcode, SDValue &LHS, SDValue &RHS, | |||
47577 | SelectionDAG &DAG, const X86Subtarget &Subtarget, | |||
47578 | bool IsCommutative, | |||
47579 | SmallVectorImpl<int> &PostShuffleMask) { | |||
47580 | // If either operand is undef, bail out. The binop should be simplified. | |||
47581 | if (LHS.isUndef() || RHS.isUndef()) | |||
47582 | return false; | |||
47583 | ||||
47584 | // Look for the following pattern: | |||
47585 | // A = < float a0, float a1, float a2, float a3 > | |||
47586 | // B = < float b0, float b1, float b2, float b3 > | |||
47587 | // and | |||
47588 | // LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6> | |||
47589 | // RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7> | |||
47590 | // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 > | |||
47591 | // which is A horizontal-op B. | |||
47592 | ||||
47593 | MVT VT = LHS.getSimpleValueType(); | |||
47594 | assert((VT.is128BitVector() || VT.is256BitVector()) &&(static_cast <bool> ((VT.is128BitVector() || VT.is256BitVector ()) && "Unsupported vector type for horizontal add/sub" ) ? void (0) : __assert_fail ("(VT.is128BitVector() || VT.is256BitVector()) && \"Unsupported vector type for horizontal add/sub\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 47595, __extension__ __PRETTY_FUNCTION__)) | |||
47595 | "Unsupported vector type for horizontal add/sub")(static_cast <bool> ((VT.is128BitVector() || VT.is256BitVector ()) && "Unsupported vector type for horizontal add/sub" ) ? void (0) : __assert_fail ("(VT.is128BitVector() || VT.is256BitVector()) && \"Unsupported vector type for horizontal add/sub\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 47595, __extension__ __PRETTY_FUNCTION__)); | |||
47596 | unsigned NumElts = VT.getVectorNumElements(); | |||
47597 | ||||
47598 | auto GetShuffle = [&](SDValue Op, SDValue &N0, SDValue &N1, | |||
47599 | SmallVectorImpl<int> &ShuffleMask) { | |||
47600 | bool UseSubVector = false; | |||
47601 | if (Op.getOpcode() == ISD::EXTRACT_SUBVECTOR && | |||
47602 | Op.getOperand(0).getValueType().is256BitVector() && | |||
47603 | llvm::isNullConstant(Op.getOperand(1))) { | |||
47604 | Op = Op.getOperand(0); | |||
47605 | UseSubVector = true; | |||
47606 | } | |||
47607 | SmallVector<SDValue, 2> SrcOps; | |||
47608 | SmallVector<int, 16> SrcMask, ScaledMask; | |||
47609 | SDValue BC = peekThroughBitcasts(Op); | |||
47610 | if (getTargetShuffleInputs(BC, SrcOps, SrcMask, DAG) && | |||
47611 | !isAnyZero(SrcMask) && all_of(SrcOps, [BC](SDValue Op) { | |||
47612 | return Op.getValueSizeInBits() == BC.getValueSizeInBits(); | |||
47613 | })) { | |||
47614 | resolveTargetShuffleInputsAndMask(SrcOps, SrcMask); | |||
47615 | if (!UseSubVector && SrcOps.size() <= 2 && | |||
47616 | scaleShuffleElements(SrcMask, NumElts, ScaledMask)) { | |||
47617 | N0 = SrcOps.size() > 0 ? SrcOps[0] : SDValue(); | |||
47618 | N1 = SrcOps.size() > 1 ? SrcOps[1] : SDValue(); | |||
47619 | ShuffleMask.assign(ScaledMask.begin(), ScaledMask.end()); | |||
47620 | } | |||
47621 | if (UseSubVector && SrcOps.size() == 1 && | |||
47622 | scaleShuffleElements(SrcMask, 2 * NumElts, ScaledMask)) { | |||
47623 | std::tie(N0, N1) = DAG.SplitVector(SrcOps[0], SDLoc(Op)); | |||
47624 | ArrayRef<int> Mask = ArrayRef<int>(ScaledMask).slice(0, NumElts); | |||
47625 | ShuffleMask.assign(Mask.begin(), Mask.end()); | |||
47626 | } | |||
47627 | } | |||
47628 | }; | |||
47629 | ||||
47630 | // View LHS in the form | |||
47631 | // LHS = VECTOR_SHUFFLE A, B, LMask | |||
47632 | // If LHS is not a shuffle, then pretend it is the identity shuffle: | |||
47633 | // LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1> | |||
47634 | // NOTE: A default initialized SDValue represents an UNDEF of type VT. | |||
47635 | SDValue A, B; | |||
47636 | SmallVector<int, 16> LMask; | |||
47637 | GetShuffle(LHS, A, B, LMask); | |||
47638 | ||||
47639 | // Likewise, view RHS in the form | |||
47640 | // RHS = VECTOR_SHUFFLE C, D, RMask | |||
47641 | SDValue C, D; | |||
47642 | SmallVector<int, 16> RMask; | |||
47643 | GetShuffle(RHS, C, D, RMask); | |||
47644 | ||||
47645 | // At least one of the operands should be a vector shuffle. | |||
47646 | unsigned NumShuffles = (LMask.empty() ? 0 : 1) + (RMask.empty() ? 0 : 1); | |||
47647 | if (NumShuffles == 0) | |||
47648 | return false; | |||
47649 | ||||
47650 | if (LMask.empty()) { | |||
47651 | A = LHS; | |||
47652 | for (unsigned i = 0; i != NumElts; ++i) | |||
47653 | LMask.push_back(i); | |||
47654 | } | |||
47655 | ||||
47656 | if (RMask.empty()) { | |||
47657 | C = RHS; | |||
47658 | for (unsigned i = 0; i != NumElts; ++i) | |||
47659 | RMask.push_back(i); | |||
47660 | } | |||
47661 | ||||
47662 | // If we have an unary mask, ensure the other op is set to null. | |||
47663 | if (isUndefOrInRange(LMask, 0, NumElts)) | |||
47664 | B = SDValue(); | |||
47665 | else if (isUndefOrInRange(LMask, NumElts, NumElts * 2)) | |||
47666 | A = SDValue(); | |||
47667 | ||||
47668 | if (isUndefOrInRange(RMask, 0, NumElts)) | |||
47669 | D = SDValue(); | |||
47670 | else if (isUndefOrInRange(RMask, NumElts, NumElts * 2)) | |||
47671 | C = SDValue(); | |||
47672 | ||||
47673 | // If A and B occur in reverse order in RHS, then canonicalize by commuting | |||
47674 | // RHS operands and shuffle mask. | |||
47675 | if (A != C) { | |||
47676 | std::swap(C, D); | |||
47677 | ShuffleVectorSDNode::commuteMask(RMask); | |||
47678 | } | |||
47679 | // Check that the shuffles are both shuffling the same vectors. | |||
47680 | if (!(A == C && B == D)) | |||
47681 | return false; | |||
47682 | ||||
47683 | PostShuffleMask.clear(); | |||
47684 | PostShuffleMask.append(NumElts, SM_SentinelUndef); | |||
47685 | ||||
47686 | // LHS and RHS are now: | |||
47687 | // LHS = shuffle A, B, LMask | |||
47688 | // RHS = shuffle A, B, RMask | |||
47689 | // Check that the masks correspond to performing a horizontal operation. | |||
47690 | // AVX defines horizontal add/sub to operate independently on 128-bit lanes, | |||
47691 | // so we just repeat the inner loop if this is a 256-bit op. | |||
47692 | unsigned Num128BitChunks = VT.getSizeInBits() / 128; | |||
47693 | unsigned NumEltsPer128BitChunk = NumElts / Num128BitChunks; | |||
47694 | unsigned NumEltsPer64BitChunk = NumEltsPer128BitChunk / 2; | |||
47695 | assert((NumEltsPer128BitChunk % 2 == 0) &&(static_cast <bool> ((NumEltsPer128BitChunk % 2 == 0) && "Vector type should have an even number of elements in each lane" ) ? void (0) : __assert_fail ("(NumEltsPer128BitChunk % 2 == 0) && \"Vector type should have an even number of elements in each lane\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 47696, __extension__ __PRETTY_FUNCTION__)) | |||
47696 | "Vector type should have an even number of elements in each lane")(static_cast <bool> ((NumEltsPer128BitChunk % 2 == 0) && "Vector type should have an even number of elements in each lane" ) ? void (0) : __assert_fail ("(NumEltsPer128BitChunk % 2 == 0) && \"Vector type should have an even number of elements in each lane\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 47696, __extension__ __PRETTY_FUNCTION__)); | |||
47697 | for (unsigned j = 0; j != NumElts; j += NumEltsPer128BitChunk) { | |||
47698 | for (unsigned i = 0; i != NumEltsPer128BitChunk; ++i) { | |||
47699 | // Ignore undefined components. | |||
47700 | int LIdx = LMask[i + j], RIdx = RMask[i + j]; | |||
47701 | if (LIdx < 0 || RIdx < 0 || | |||
47702 | (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) || | |||
47703 | (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts))) | |||
47704 | continue; | |||
47705 | ||||
47706 | // Check that successive odd/even elements are being operated on. If not, | |||
47707 | // this is not a horizontal operation. | |||
47708 | if (!((RIdx & 1) == 1 && (LIdx + 1) == RIdx) && | |||
47709 | !((LIdx & 1) == 1 && (RIdx + 1) == LIdx && IsCommutative)) | |||
47710 | return false; | |||
47711 | ||||
47712 | // Compute the post-shuffle mask index based on where the element | |||
47713 | // is stored in the HOP result, and where it needs to be moved to. | |||
47714 | int Base = LIdx & ~1u; | |||
47715 | int Index = ((Base % NumEltsPer128BitChunk) / 2) + | |||
47716 | ((Base % NumElts) & ~(NumEltsPer128BitChunk - 1)); | |||
47717 | ||||
47718 | // The low half of the 128-bit result must choose from A. | |||
47719 | // The high half of the 128-bit result must choose from B, | |||
47720 | // unless B is undef. In that case, we are always choosing from A. | |||
47721 | if ((B && Base >= (int)NumElts) || (!B && i >= NumEltsPer64BitChunk)) | |||
47722 | Index += NumEltsPer64BitChunk; | |||
47723 | PostShuffleMask[i + j] = Index; | |||
47724 | } | |||
47725 | } | |||
47726 | ||||
47727 | SDValue NewLHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it. | |||
47728 | SDValue NewRHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it. | |||
47729 | ||||
47730 | bool IsIdentityPostShuffle = | |||
47731 | isSequentialOrUndefInRange(PostShuffleMask, 0, NumElts, 0); | |||
47732 | if (IsIdentityPostShuffle) | |||
47733 | PostShuffleMask.clear(); | |||
47734 | ||||
47735 | // Avoid 128-bit multi lane shuffles if pre-AVX2 and FP (integer will split). | |||
47736 | if (!IsIdentityPostShuffle && !Subtarget.hasAVX2() && VT.isFloatingPoint() && | |||
47737 | isMultiLaneShuffleMask(128, VT.getScalarSizeInBits(), PostShuffleMask)) | |||
47738 | return false; | |||
47739 | ||||
47740 | // If the source nodes are already used in HorizOps then always accept this. | |||
47741 | // Shuffle folding should merge these back together. | |||
47742 | bool FoundHorizLHS = llvm::any_of(NewLHS->uses(), [&](SDNode *User) { | |||
47743 | return User->getOpcode() == HOpcode && User->getValueType(0) == VT; | |||
47744 | }); | |||
47745 | bool FoundHorizRHS = llvm::any_of(NewRHS->uses(), [&](SDNode *User) { | |||
47746 | return User->getOpcode() == HOpcode && User->getValueType(0) == VT; | |||
47747 | }); | |||
47748 | bool ForceHorizOp = FoundHorizLHS && FoundHorizRHS; | |||
47749 | ||||
47750 | // Assume a SingleSource HOP if we only shuffle one input and don't need to | |||
47751 | // shuffle the result. | |||
47752 | if (!ForceHorizOp && | |||
47753 | !shouldUseHorizontalOp(NewLHS == NewRHS && | |||
47754 | (NumShuffles < 2 || !IsIdentityPostShuffle), | |||
47755 | DAG, Subtarget)) | |||
47756 | return false; | |||
47757 | ||||
47758 | LHS = DAG.getBitcast(VT, NewLHS); | |||
47759 | RHS = DAG.getBitcast(VT, NewRHS); | |||
47760 | return true; | |||
47761 | } | |||
47762 | ||||
47763 | // Try to synthesize horizontal (f)hadd/hsub from (f)adds/subs of shuffles. | |||
47764 | static SDValue combineToHorizontalAddSub(SDNode *N, SelectionDAG &DAG, | |||
47765 | const X86Subtarget &Subtarget) { | |||
47766 | EVT VT = N->getValueType(0); | |||
47767 | unsigned Opcode = N->getOpcode(); | |||
47768 | bool IsAdd = (Opcode == ISD::FADD) || (Opcode == ISD::ADD); | |||
47769 | SmallVector<int, 8> PostShuffleMask; | |||
47770 | ||||
47771 | switch (Opcode) { | |||
47772 | case ISD::FADD: | |||
47773 | case ISD::FSUB: | |||
47774 | if ((Subtarget.hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) || | |||
47775 | (Subtarget.hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))) { | |||
47776 | SDValue LHS = N->getOperand(0); | |||
47777 | SDValue RHS = N->getOperand(1); | |||
47778 | auto HorizOpcode = IsAdd ? X86ISD::FHADD : X86ISD::FHSUB; | |||
47779 | if (isHorizontalBinOp(HorizOpcode, LHS, RHS, DAG, Subtarget, IsAdd, | |||
47780 | PostShuffleMask)) { | |||
47781 | SDValue HorizBinOp = DAG.getNode(HorizOpcode, SDLoc(N), VT, LHS, RHS); | |||
47782 | if (!PostShuffleMask.empty()) | |||
47783 | HorizBinOp = DAG.getVectorShuffle(VT, SDLoc(HorizBinOp), HorizBinOp, | |||
47784 | DAG.getUNDEF(VT), PostShuffleMask); | |||
47785 | return HorizBinOp; | |||
47786 | } | |||
47787 | } | |||
47788 | break; | |||
47789 | case ISD::ADD: | |||
47790 | case ISD::SUB: | |||
47791 | if (Subtarget.hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32 || | |||
47792 | VT == MVT::v16i16 || VT == MVT::v8i32)) { | |||
47793 | SDValue LHS = N->getOperand(0); | |||
47794 | SDValue RHS = N->getOperand(1); | |||
47795 | auto HorizOpcode = IsAdd ? X86ISD::HADD : X86ISD::HSUB; | |||
47796 | if (isHorizontalBinOp(HorizOpcode, LHS, RHS, DAG, Subtarget, IsAdd, | |||
47797 | PostShuffleMask)) { | |||
47798 | auto HOpBuilder = [HorizOpcode](SelectionDAG &DAG, const SDLoc &DL, | |||
47799 | ArrayRef<SDValue> Ops) { | |||
47800 | return DAG.getNode(HorizOpcode, DL, Ops[0].getValueType(), Ops); | |||
47801 | }; | |||
47802 | SDValue HorizBinOp = SplitOpsAndApply(DAG, Subtarget, SDLoc(N), VT, | |||
47803 | {LHS, RHS}, HOpBuilder); | |||
47804 | if (!PostShuffleMask.empty()) | |||
47805 | HorizBinOp = DAG.getVectorShuffle(VT, SDLoc(HorizBinOp), HorizBinOp, | |||
47806 | DAG.getUNDEF(VT), PostShuffleMask); | |||
47807 | return HorizBinOp; | |||
47808 | } | |||
47809 | } | |||
47810 | break; | |||
47811 | } | |||
47812 | ||||
47813 | return SDValue(); | |||
47814 | } | |||
47815 | ||||
47816 | // Try to combine the following nodes | |||
47817 | // t29: i64 = X86ISD::Wrapper TargetConstantPool:i64 | |||
47818 | // <i32 -2147483648[float -0.000000e+00]> 0 | |||
47819 | // t27: v16i32[v16f32],ch = X86ISD::VBROADCAST_LOAD | |||
47820 | // <(load 4 from constant-pool)> t0, t29 | |||
47821 | // [t30: v16i32 = bitcast t27] | |||
47822 | // t6: v16i32 = xor t7, t27[t30] | |||
47823 | // t11: v16f32 = bitcast t6 | |||
47824 | // t21: v16f32 = X86ISD::VFMULC[X86ISD::VCFMULC] t11, t8 | |||
47825 | // into X86ISD::VFCMULC[X86ISD::VFMULC] if possible: | |||
47826 | // t22: v16f32 = bitcast t7 | |||
47827 | // t23: v16f32 = X86ISD::VFCMULC[X86ISD::VFMULC] t8, t22 | |||
47828 | // t24: v32f16 = bitcast t23 | |||
47829 | static SDValue combineFMulcFCMulc(SDNode *N, SelectionDAG &DAG, | |||
47830 | const X86Subtarget &Subtarget) { | |||
47831 | EVT VT = N->getValueType(0); | |||
47832 | SDValue LHS = N->getOperand(0); | |||
47833 | SDValue RHS = N->getOperand(1); | |||
47834 | int CombineOpcode = | |||
47835 | N->getOpcode() == X86ISD::VFCMULC ? X86ISD::VFMULC : X86ISD::VFCMULC; | |||
47836 | auto isConjugationConstant = [](const Constant *c) { | |||
47837 | if (const auto *CI = dyn_cast<ConstantInt>(c)) { | |||
47838 | APInt ConjugationInt32 = APInt(32, 0x80000000, true); | |||
47839 | APInt ConjugationInt64 = APInt(64, 0x8000000080000000ULL, true); | |||
47840 | switch (CI->getBitWidth()) { | |||
47841 | case 16: | |||
47842 | return false; | |||
47843 | case 32: | |||
47844 | return CI->getValue() == ConjugationInt32; | |||
47845 | case 64: | |||
47846 | return CI->getValue() == ConjugationInt64; | |||
47847 | default: | |||
47848 | llvm_unreachable("Unexpected bit width")::llvm::llvm_unreachable_internal("Unexpected bit width", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 47848); | |||
47849 | } | |||
47850 | } | |||
47851 | if (const auto *CF = dyn_cast<ConstantFP>(c)) | |||
47852 | return CF->isNegativeZeroValue(); | |||
47853 | return false; | |||
47854 | }; | |||
47855 | auto combineConjugation = [&](SDValue &r) { | |||
47856 | if (LHS->getOpcode() == ISD::BITCAST && RHS.hasOneUse()) { | |||
47857 | SDValue XOR = LHS.getOperand(0); | |||
47858 | if (XOR->getOpcode() == ISD::XOR && XOR.hasOneUse()) { | |||
47859 | SDValue XORRHS = XOR.getOperand(1); | |||
47860 | if (XORRHS.getOpcode() == ISD::BITCAST && XORRHS.hasOneUse()) | |||
47861 | XORRHS = XORRHS.getOperand(0); | |||
47862 | if (XORRHS.getOpcode() == X86ISD::VBROADCAST_LOAD && | |||
47863 | XORRHS.getOperand(1).getNumOperands()) { | |||
47864 | ConstantPoolSDNode *CP = | |||
47865 | dyn_cast<ConstantPoolSDNode>(XORRHS.getOperand(1).getOperand(0)); | |||
47866 | if (CP && isConjugationConstant(CP->getConstVal())) { | |||
47867 | SelectionDAG::FlagInserter FlagsInserter(DAG, N); | |||
47868 | SDValue I2F = DAG.getBitcast(VT, LHS.getOperand(0).getOperand(0)); | |||
47869 | SDValue FCMulC = DAG.getNode(CombineOpcode, SDLoc(N), VT, RHS, I2F); | |||
47870 | r = DAG.getBitcast(VT, FCMulC); | |||
47871 | return true; | |||
47872 | } | |||
47873 | } | |||
47874 | } | |||
47875 | } | |||
47876 | return false; | |||
47877 | }; | |||
47878 | SDValue Res; | |||
47879 | if (combineConjugation(Res)) | |||
47880 | return Res; | |||
47881 | std::swap(LHS, RHS); | |||
47882 | if (combineConjugation(Res)) | |||
47883 | return Res; | |||
47884 | return Res; | |||
47885 | } | |||
47886 | ||||
47887 | // Try to combine the following nodes: | |||
47888 | // FADD(A, FMA(B, C, 0)) and FADD(A, FMUL(B, C)) to FMA(B, C, A) | |||
47889 | static SDValue combineFaddCFmul(SDNode *N, SelectionDAG &DAG, | |||
47890 | const X86Subtarget &Subtarget) { | |||
47891 | auto AllowContract = [&DAG](const SDNodeFlags &Flags) { | |||
47892 | return DAG.getTarget().Options.AllowFPOpFusion == FPOpFusion::Fast || | |||
47893 | Flags.hasAllowContract(); | |||
47894 | }; | |||
47895 | ||||
47896 | auto HasNoSignedZero = [&DAG](const SDNodeFlags &Flags) { | |||
47897 | return DAG.getTarget().Options.NoSignedZerosFPMath || | |||
47898 | Flags.hasNoSignedZeros(); | |||
47899 | }; | |||
47900 | auto IsVectorAllNegativeZero = [](const SDNode *N) { | |||
47901 | if (N->getOpcode() != X86ISD::VBROADCAST_LOAD) | |||
47902 | return false; | |||
47903 | assert(N->getSimpleValueType(0).getScalarType() == MVT::f32 &&(static_cast <bool> (N->getSimpleValueType(0).getScalarType () == MVT::f32 && "Unexpected vector type!") ? void ( 0) : __assert_fail ("N->getSimpleValueType(0).getScalarType() == MVT::f32 && \"Unexpected vector type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 47904, __extension__ __PRETTY_FUNCTION__)) | |||
47904 | "Unexpected vector type!")(static_cast <bool> (N->getSimpleValueType(0).getScalarType () == MVT::f32 && "Unexpected vector type!") ? void ( 0) : __assert_fail ("N->getSimpleValueType(0).getScalarType() == MVT::f32 && \"Unexpected vector type!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 47904, __extension__ __PRETTY_FUNCTION__)); | |||
47905 | if (ConstantPoolSDNode *CP = | |||
47906 | dyn_cast<ConstantPoolSDNode>(N->getOperand(1)->getOperand(0))) { | |||
47907 | APInt AI = APInt(32, 0x80008000, true); | |||
47908 | if (const auto *CI = dyn_cast<ConstantInt>(CP->getConstVal())) | |||
47909 | return CI->getValue() == AI; | |||
47910 | if (const auto *CF = dyn_cast<ConstantFP>(CP->getConstVal())) | |||
47911 | return CF->getValue() == APFloat(APFloat::IEEEsingle(), AI); | |||
47912 | } | |||
47913 | return false; | |||
47914 | }; | |||
47915 | ||||
47916 | if (N->getOpcode() != ISD::FADD || !Subtarget.hasFP16() || | |||
47917 | !AllowContract(N->getFlags())) | |||
47918 | return SDValue(); | |||
47919 | ||||
47920 | EVT VT = N->getValueType(0); | |||
47921 | if (VT != MVT::v8f16 && VT != MVT::v16f16 && VT != MVT::v32f16) | |||
47922 | return SDValue(); | |||
47923 | ||||
47924 | SDValue LHS = N->getOperand(0); | |||
47925 | SDValue RHS = N->getOperand(1); | |||
47926 | bool IsConj; | |||
47927 | SDValue FAddOp1, MulOp0, MulOp1; | |||
47928 | auto GetCFmulFrom = [&MulOp0, &MulOp1, &IsConj, &AllowContract, | |||
47929 | &IsVectorAllNegativeZero, | |||
47930 | &HasNoSignedZero](SDValue N) -> bool { | |||
47931 | if (!N.hasOneUse() || N.getOpcode() != ISD::BITCAST) | |||
47932 | return false; | |||
47933 | SDValue Op0 = N.getOperand(0); | |||
47934 | unsigned Opcode = Op0.getOpcode(); | |||
47935 | if (Op0.hasOneUse() && AllowContract(Op0->getFlags())) { | |||
47936 | if ((Opcode == X86ISD::VFMULC || Opcode == X86ISD::VFCMULC)) { | |||
47937 | MulOp0 = Op0.getOperand(0); | |||
47938 | MulOp1 = Op0.getOperand(1); | |||
47939 | IsConj = Opcode == X86ISD::VFCMULC; | |||
47940 | return true; | |||
47941 | } | |||
47942 | if ((Opcode == X86ISD::VFMADDC || Opcode == X86ISD::VFCMADDC) && | |||
47943 | ((ISD::isBuildVectorAllZeros(Op0->getOperand(2).getNode()) && | |||
47944 | HasNoSignedZero(Op0->getFlags())) || | |||
47945 | IsVectorAllNegativeZero(Op0->getOperand(2).getNode()))) { | |||
47946 | MulOp0 = Op0.getOperand(0); | |||
47947 | MulOp1 = Op0.getOperand(1); | |||
47948 | IsConj = Opcode == X86ISD::VFCMADDC; | |||
47949 | return true; | |||
47950 | } | |||
47951 | } | |||
47952 | return false; | |||
47953 | }; | |||
47954 | ||||
47955 | if (GetCFmulFrom(LHS)) | |||
47956 | FAddOp1 = RHS; | |||
47957 | else if (GetCFmulFrom(RHS)) | |||
47958 | FAddOp1 = LHS; | |||
47959 | else | |||
47960 | return SDValue(); | |||
47961 | ||||
47962 | MVT CVT = MVT::getVectorVT(MVT::f32, VT.getVectorNumElements() / 2); | |||
47963 | FAddOp1 = DAG.getBitcast(CVT, FAddOp1); | |||
47964 | unsigned NewOp = IsConj ? X86ISD::VFCMADDC : X86ISD::VFMADDC; | |||
47965 | // FIXME: How do we handle when fast math flags of FADD are different from | |||
47966 | // CFMUL's? | |||
47967 | SDValue CFmul = | |||
47968 | DAG.getNode(NewOp, SDLoc(N), CVT, MulOp0, MulOp1, FAddOp1, N->getFlags()); | |||
47969 | return DAG.getBitcast(VT, CFmul); | |||
47970 | } | |||
47971 | ||||
47972 | /// Do target-specific dag combines on floating-point adds/subs. | |||
47973 | static SDValue combineFaddFsub(SDNode *N, SelectionDAG &DAG, | |||
47974 | const X86Subtarget &Subtarget) { | |||
47975 | if (SDValue HOp = combineToHorizontalAddSub(N, DAG, Subtarget)) | |||
47976 | return HOp; | |||
47977 | ||||
47978 | if (SDValue COp = combineFaddCFmul(N, DAG, Subtarget)) | |||
47979 | return COp; | |||
47980 | ||||
47981 | return SDValue(); | |||
47982 | } | |||
47983 | ||||
47984 | /// Attempt to pre-truncate inputs to arithmetic ops if it will simplify | |||
47985 | /// the codegen. | |||
47986 | /// e.g. TRUNC( BINOP( X, Y ) ) --> BINOP( TRUNC( X ), TRUNC( Y ) ) | |||
47987 | /// TODO: This overlaps with the generic combiner's visitTRUNCATE. Remove | |||
47988 | /// anything that is guaranteed to be transformed by DAGCombiner. | |||
47989 | static SDValue combineTruncatedArithmetic(SDNode *N, SelectionDAG &DAG, | |||
47990 | const X86Subtarget &Subtarget, | |||
47991 | const SDLoc &DL) { | |||
47992 | assert(N->getOpcode() == ISD::TRUNCATE && "Wrong opcode")(static_cast <bool> (N->getOpcode() == ISD::TRUNCATE && "Wrong opcode") ? void (0) : __assert_fail ("N->getOpcode() == ISD::TRUNCATE && \"Wrong opcode\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 47992, __extension__ __PRETTY_FUNCTION__)); | |||
47993 | SDValue Src = N->getOperand(0); | |||
47994 | unsigned SrcOpcode = Src.getOpcode(); | |||
47995 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
47996 | ||||
47997 | EVT VT = N->getValueType(0); | |||
47998 | EVT SrcVT = Src.getValueType(); | |||
47999 | ||||
48000 | auto IsFreeTruncation = [VT](SDValue Op) { | |||
48001 | unsigned TruncSizeInBits = VT.getScalarSizeInBits(); | |||
48002 | ||||
48003 | // See if this has been extended from a smaller/equal size to | |||
48004 | // the truncation size, allowing a truncation to combine with the extend. | |||
48005 | unsigned Opcode = Op.getOpcode(); | |||
48006 | if ((Opcode == ISD::ANY_EXTEND || Opcode == ISD::SIGN_EXTEND || | |||
48007 | Opcode == ISD::ZERO_EXTEND) && | |||
48008 | Op.getOperand(0).getScalarValueSizeInBits() <= TruncSizeInBits) | |||
48009 | return true; | |||
48010 | ||||
48011 | // See if this is a single use constant which can be constant folded. | |||
48012 | // NOTE: We don't peek throught bitcasts here because there is currently | |||
48013 | // no support for constant folding truncate+bitcast+vector_of_constants. So | |||
48014 | // we'll just send up with a truncate on both operands which will | |||
48015 | // get turned back into (truncate (binop)) causing an infinite loop. | |||
48016 | return ISD::isBuildVectorOfConstantSDNodes(Op.getNode()); | |||
48017 | }; | |||
48018 | ||||
48019 | auto TruncateArithmetic = [&](SDValue N0, SDValue N1) { | |||
48020 | SDValue Trunc0 = DAG.getNode(ISD::TRUNCATE, DL, VT, N0); | |||
48021 | SDValue Trunc1 = DAG.getNode(ISD::TRUNCATE, DL, VT, N1); | |||
48022 | return DAG.getNode(SrcOpcode, DL, VT, Trunc0, Trunc1); | |||
48023 | }; | |||
48024 | ||||
48025 | // Don't combine if the operation has other uses. | |||
48026 | if (!Src.hasOneUse()) | |||
48027 | return SDValue(); | |||
48028 | ||||
48029 | // Only support vector truncation for now. | |||
48030 | // TODO: i64 scalar math would benefit as well. | |||
48031 | if (!VT.isVector()) | |||
48032 | return SDValue(); | |||
48033 | ||||
48034 | // In most cases its only worth pre-truncating if we're only facing the cost | |||
48035 | // of one truncation. | |||
48036 | // i.e. if one of the inputs will constant fold or the input is repeated. | |||
48037 | switch (SrcOpcode) { | |||
48038 | case ISD::MUL: | |||
48039 | // X86 is rubbish at scalar and vector i64 multiplies (until AVX512DQ) - its | |||
48040 | // better to truncate if we have the chance. | |||
48041 | if (SrcVT.getScalarType() == MVT::i64 && | |||
48042 | TLI.isOperationLegal(SrcOpcode, VT) && | |||
48043 | !TLI.isOperationLegal(SrcOpcode, SrcVT)) | |||
48044 | return TruncateArithmetic(Src.getOperand(0), Src.getOperand(1)); | |||
48045 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
48046 | case ISD::AND: | |||
48047 | case ISD::XOR: | |||
48048 | case ISD::OR: | |||
48049 | case ISD::ADD: | |||
48050 | case ISD::SUB: { | |||
48051 | SDValue Op0 = Src.getOperand(0); | |||
48052 | SDValue Op1 = Src.getOperand(1); | |||
48053 | if (TLI.isOperationLegal(SrcOpcode, VT) && | |||
48054 | (Op0 == Op1 || IsFreeTruncation(Op0) || IsFreeTruncation(Op1))) | |||
48055 | return TruncateArithmetic(Op0, Op1); | |||
48056 | break; | |||
48057 | } | |||
48058 | } | |||
48059 | ||||
48060 | return SDValue(); | |||
48061 | } | |||
48062 | ||||
48063 | /// Truncate using ISD::AND mask and X86ISD::PACKUS. | |||
48064 | /// e.g. trunc <8 x i32> X to <8 x i16> --> | |||
48065 | /// MaskX = X & 0xffff (clear high bits to prevent saturation) | |||
48066 | /// packus (extract_subv MaskX, 0), (extract_subv MaskX, 1) | |||
48067 | static SDValue combineVectorTruncationWithPACKUS(SDNode *N, const SDLoc &DL, | |||
48068 | const X86Subtarget &Subtarget, | |||
48069 | SelectionDAG &DAG) { | |||
48070 | SDValue In = N->getOperand(0); | |||
48071 | EVT InVT = In.getValueType(); | |||
48072 | EVT OutVT = N->getValueType(0); | |||
48073 | ||||
48074 | APInt Mask = APInt::getLowBitsSet(InVT.getScalarSizeInBits(), | |||
48075 | OutVT.getScalarSizeInBits()); | |||
48076 | In = DAG.getNode(ISD::AND, DL, InVT, In, DAG.getConstant(Mask, DL, InVT)); | |||
48077 | return truncateVectorWithPACK(X86ISD::PACKUS, OutVT, In, DL, DAG, Subtarget); | |||
48078 | } | |||
48079 | ||||
48080 | /// Truncate a group of v4i32 into v8i16 using X86ISD::PACKSS. | |||
48081 | static SDValue combineVectorTruncationWithPACKSS(SDNode *N, const SDLoc &DL, | |||
48082 | const X86Subtarget &Subtarget, | |||
48083 | SelectionDAG &DAG) { | |||
48084 | SDValue In = N->getOperand(0); | |||
48085 | EVT InVT = In.getValueType(); | |||
48086 | EVT OutVT = N->getValueType(0); | |||
48087 | In = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, InVT, In, | |||
48088 | DAG.getValueType(OutVT)); | |||
48089 | return truncateVectorWithPACK(X86ISD::PACKSS, OutVT, In, DL, DAG, Subtarget); | |||
48090 | } | |||
48091 | ||||
48092 | /// This function transforms truncation from vXi32/vXi64 to vXi8/vXi16 into | |||
48093 | /// X86ISD::PACKUS/X86ISD::PACKSS operations. We do it here because after type | |||
48094 | /// legalization the truncation will be translated into a BUILD_VECTOR with each | |||
48095 | /// element that is extracted from a vector and then truncated, and it is | |||
48096 | /// difficult to do this optimization based on them. | |||
48097 | static SDValue combineVectorTruncation(SDNode *N, SelectionDAG &DAG, | |||
48098 | const X86Subtarget &Subtarget) { | |||
48099 | EVT OutVT = N->getValueType(0); | |||
48100 | if (!OutVT.isVector()) | |||
48101 | return SDValue(); | |||
48102 | ||||
48103 | SDValue In = N->getOperand(0); | |||
48104 | if (!In.getValueType().isSimple()) | |||
48105 | return SDValue(); | |||
48106 | ||||
48107 | EVT InVT = In.getValueType(); | |||
48108 | unsigned NumElems = OutVT.getVectorNumElements(); | |||
48109 | ||||
48110 | // AVX512 provides fast truncate ops. | |||
48111 | if (!Subtarget.hasSSE2() || Subtarget.hasAVX512()) | |||
48112 | return SDValue(); | |||
48113 | ||||
48114 | EVT OutSVT = OutVT.getVectorElementType(); | |||
48115 | EVT InSVT = InVT.getVectorElementType(); | |||
48116 | if (!((InSVT == MVT::i16 || InSVT == MVT::i32 || InSVT == MVT::i64) && | |||
48117 | (OutSVT == MVT::i8 || OutSVT == MVT::i16) && isPowerOf2_32(NumElems) && | |||
48118 | NumElems >= 8)) | |||
48119 | return SDValue(); | |||
48120 | ||||
48121 | // SSSE3's pshufb results in less instructions in the cases below. | |||
48122 | if (Subtarget.hasSSSE3() && NumElems == 8 && InSVT != MVT::i64) | |||
48123 | return SDValue(); | |||
48124 | ||||
48125 | SDLoc DL(N); | |||
48126 | // SSE2 provides PACKUS for only 2 x v8i16 -> v16i8 and SSE4.1 provides PACKUS | |||
48127 | // for 2 x v4i32 -> v8i16. For SSSE3 and below, we need to use PACKSS to | |||
48128 | // truncate 2 x v4i32 to v8i16. | |||
48129 | if (Subtarget.hasSSE41() || OutSVT == MVT::i8) | |||
48130 | return combineVectorTruncationWithPACKUS(N, DL, Subtarget, DAG); | |||
48131 | if (InSVT == MVT::i32) | |||
48132 | return combineVectorTruncationWithPACKSS(N, DL, Subtarget, DAG); | |||
48133 | ||||
48134 | return SDValue(); | |||
48135 | } | |||
48136 | ||||
48137 | /// This function transforms vector truncation of 'extended sign-bits' or | |||
48138 | /// 'extended zero-bits' values. | |||
48139 | /// vXi16/vXi32/vXi64 to vXi8/vXi16/vXi32 into X86ISD::PACKSS/PACKUS operations. | |||
48140 | static SDValue combineVectorSignBitsTruncation(SDNode *N, const SDLoc &DL, | |||
48141 | SelectionDAG &DAG, | |||
48142 | const X86Subtarget &Subtarget) { | |||
48143 | // Requires SSE2. | |||
48144 | if (!Subtarget.hasSSE2()) | |||
48145 | return SDValue(); | |||
48146 | ||||
48147 | if (!N->getValueType(0).isVector() || !N->getValueType(0).isSimple()) | |||
48148 | return SDValue(); | |||
48149 | ||||
48150 | SDValue In = N->getOperand(0); | |||
48151 | if (!In.getValueType().isSimple()) | |||
48152 | return SDValue(); | |||
48153 | ||||
48154 | MVT VT = N->getValueType(0).getSimpleVT(); | |||
48155 | MVT SVT = VT.getScalarType(); | |||
48156 | ||||
48157 | MVT InVT = In.getValueType().getSimpleVT(); | |||
48158 | MVT InSVT = InVT.getScalarType(); | |||
48159 | ||||
48160 | // Check we have a truncation suited for PACKSS/PACKUS. | |||
48161 | if (!isPowerOf2_32(VT.getVectorNumElements())) | |||
48162 | return SDValue(); | |||
48163 | if (SVT != MVT::i8 && SVT != MVT::i16 && SVT != MVT::i32) | |||
48164 | return SDValue(); | |||
48165 | if (InSVT != MVT::i16 && InSVT != MVT::i32 && InSVT != MVT::i64) | |||
48166 | return SDValue(); | |||
48167 | ||||
48168 | // Truncation to sub-128bit vXi32 can be better handled with shuffles. | |||
48169 | if (SVT == MVT::i32 && VT.getSizeInBits() < 128) | |||
48170 | return SDValue(); | |||
48171 | ||||
48172 | // AVX512 has fast truncate, but if the input is already going to be split, | |||
48173 | // there's no harm in trying pack. | |||
48174 | if (Subtarget.hasAVX512() && | |||
48175 | !(!Subtarget.useAVX512Regs() && VT.is256BitVector() && | |||
48176 | InVT.is512BitVector())) { | |||
48177 | // PACK should still be worth it for 128-bit vectors if the sources were | |||
48178 | // originally concatenated from subvectors. | |||
48179 | SmallVector<SDValue> ConcatOps; | |||
48180 | if (VT.getSizeInBits() > 128 || !collectConcatOps(In.getNode(), ConcatOps)) | |||
48181 | return SDValue(); | |||
48182 | } | |||
48183 | ||||
48184 | unsigned NumPackedSignBits = std::min<unsigned>(SVT.getSizeInBits(), 16); | |||
48185 | unsigned NumPackedZeroBits = Subtarget.hasSSE41() ? NumPackedSignBits : 8; | |||
48186 | ||||
48187 | // Use PACKUS if the input has zero-bits that extend all the way to the | |||
48188 | // packed/truncated value. e.g. masks, zext_in_reg, etc. | |||
48189 | KnownBits Known = DAG.computeKnownBits(In); | |||
48190 | unsigned NumLeadingZeroBits = Known.countMinLeadingZeros(); | |||
48191 | if (NumLeadingZeroBits >= (InSVT.getSizeInBits() - NumPackedZeroBits)) | |||
48192 | return truncateVectorWithPACK(X86ISD::PACKUS, VT, In, DL, DAG, Subtarget); | |||
48193 | ||||
48194 | // Use PACKSS if the input has sign-bits that extend all the way to the | |||
48195 | // packed/truncated value. e.g. Comparison result, sext_in_reg, etc. | |||
48196 | unsigned NumSignBits = DAG.ComputeNumSignBits(In); | |||
48197 | ||||
48198 | // Don't use PACKSS for vXi64 -> vXi32 truncations unless we're dealing with | |||
48199 | // a sign splat. ComputeNumSignBits struggles to see through BITCASTs later | |||
48200 | // on and combines/simplifications can't then use it. | |||
48201 | if (SVT == MVT::i32 && NumSignBits != InSVT.getSizeInBits()) | |||
48202 | return SDValue(); | |||
48203 | ||||
48204 | unsigned MinSignBits = InSVT.getSizeInBits() - NumPackedSignBits; | |||
48205 | if (NumSignBits > MinSignBits) | |||
48206 | return truncateVectorWithPACK(X86ISD::PACKSS, VT, In, DL, DAG, Subtarget); | |||
48207 | ||||
48208 | // If we have a srl that only generates signbits that we will discard in | |||
48209 | // the truncation then we can use PACKSS by converting the srl to a sra. | |||
48210 | // SimplifyDemandedBits often relaxes sra to srl so we need to reverse it. | |||
48211 | if (In.getOpcode() == ISD::SRL && N->isOnlyUserOf(In.getNode())) | |||
48212 | if (const APInt *ShAmt = DAG.getValidShiftAmountConstant( | |||
48213 | In, APInt::getAllOnes(VT.getVectorNumElements()))) { | |||
48214 | if (*ShAmt == MinSignBits) { | |||
48215 | SDValue NewIn = DAG.getNode(ISD::SRA, DL, InVT, In->ops()); | |||
48216 | return truncateVectorWithPACK(X86ISD::PACKSS, VT, NewIn, DL, DAG, | |||
48217 | Subtarget); | |||
48218 | } | |||
48219 | } | |||
48220 | ||||
48221 | return SDValue(); | |||
48222 | } | |||
48223 | ||||
48224 | // Try to form a MULHU or MULHS node by looking for | |||
48225 | // (trunc (srl (mul ext, ext), 16)) | |||
48226 | // TODO: This is X86 specific because we want to be able to handle wide types | |||
48227 | // before type legalization. But we can only do it if the vector will be | |||
48228 | // legalized via widening/splitting. Type legalization can't handle promotion | |||
48229 | // of a MULHU/MULHS. There isn't a way to convey this to the generic DAG | |||
48230 | // combiner. | |||
48231 | static SDValue combinePMULH(SDValue Src, EVT VT, const SDLoc &DL, | |||
48232 | SelectionDAG &DAG, const X86Subtarget &Subtarget) { | |||
48233 | // First instruction should be a right shift of a multiply. | |||
48234 | if (Src.getOpcode() != ISD::SRL || | |||
48235 | Src.getOperand(0).getOpcode() != ISD::MUL) | |||
48236 | return SDValue(); | |||
48237 | ||||
48238 | if (!Subtarget.hasSSE2()) | |||
48239 | return SDValue(); | |||
48240 | ||||
48241 | // Only handle vXi16 types that are at least 128-bits unless they will be | |||
48242 | // widened. | |||
48243 | if (!VT.isVector() || VT.getVectorElementType() != MVT::i16) | |||
48244 | return SDValue(); | |||
48245 | ||||
48246 | // Input type should be at least vXi32. | |||
48247 | EVT InVT = Src.getValueType(); | |||
48248 | if (InVT.getVectorElementType().getSizeInBits() < 32) | |||
48249 | return SDValue(); | |||
48250 | ||||
48251 | // Need a shift by 16. | |||
48252 | APInt ShiftAmt; | |||
48253 | if (!ISD::isConstantSplatVector(Src.getOperand(1).getNode(), ShiftAmt) || | |||
48254 | ShiftAmt != 16) | |||
48255 | return SDValue(); | |||
48256 | ||||
48257 | SDValue LHS = Src.getOperand(0).getOperand(0); | |||
48258 | SDValue RHS = Src.getOperand(0).getOperand(1); | |||
48259 | ||||
48260 | unsigned ExtOpc = LHS.getOpcode(); | |||
48261 | if ((ExtOpc != ISD::SIGN_EXTEND && ExtOpc != ISD::ZERO_EXTEND) || | |||
48262 | RHS.getOpcode() != ExtOpc) | |||
48263 | return SDValue(); | |||
48264 | ||||
48265 | // Peek through the extends. | |||
48266 | LHS = LHS.getOperand(0); | |||
48267 | RHS = RHS.getOperand(0); | |||
48268 | ||||
48269 | // Ensure the input types match. | |||
48270 | if (LHS.getValueType() != VT || RHS.getValueType() != VT) | |||
48271 | return SDValue(); | |||
48272 | ||||
48273 | unsigned Opc = ExtOpc == ISD::SIGN_EXTEND ? ISD::MULHS : ISD::MULHU; | |||
48274 | return DAG.getNode(Opc, DL, VT, LHS, RHS); | |||
48275 | } | |||
48276 | ||||
48277 | // Attempt to match PMADDUBSW, which multiplies corresponding unsigned bytes | |||
48278 | // from one vector with signed bytes from another vector, adds together | |||
48279 | // adjacent pairs of 16-bit products, and saturates the result before | |||
48280 | // truncating to 16-bits. | |||
48281 | // | |||
48282 | // Which looks something like this: | |||
48283 | // (i16 (ssat (add (mul (zext (even elts (i8 A))), (sext (even elts (i8 B)))), | |||
48284 | // (mul (zext (odd elts (i8 A)), (sext (odd elts (i8 B)))))))) | |||
48285 | static SDValue detectPMADDUBSW(SDValue In, EVT VT, SelectionDAG &DAG, | |||
48286 | const X86Subtarget &Subtarget, | |||
48287 | const SDLoc &DL) { | |||
48288 | if (!VT.isVector() || !Subtarget.hasSSSE3()) | |||
48289 | return SDValue(); | |||
48290 | ||||
48291 | unsigned NumElems = VT.getVectorNumElements(); | |||
48292 | EVT ScalarVT = VT.getVectorElementType(); | |||
48293 | if (ScalarVT != MVT::i16 || NumElems < 8 || !isPowerOf2_32(NumElems)) | |||
48294 | return SDValue(); | |||
48295 | ||||
48296 | SDValue SSatVal = detectSSatPattern(In, VT); | |||
48297 | if (!SSatVal || SSatVal.getOpcode() != ISD::ADD) | |||
48298 | return SDValue(); | |||
48299 | ||||
48300 | // Ok this is a signed saturation of an ADD. See if this ADD is adding pairs | |||
48301 | // of multiplies from even/odd elements. | |||
48302 | SDValue N0 = SSatVal.getOperand(0); | |||
48303 | SDValue N1 = SSatVal.getOperand(1); | |||
48304 | ||||
48305 | if (N0.getOpcode() != ISD::MUL || N1.getOpcode() != ISD::MUL) | |||
48306 | return SDValue(); | |||
48307 | ||||
48308 | SDValue N00 = N0.getOperand(0); | |||
48309 | SDValue N01 = N0.getOperand(1); | |||
48310 | SDValue N10 = N1.getOperand(0); | |||
48311 | SDValue N11 = N1.getOperand(1); | |||
48312 | ||||
48313 | // TODO: Handle constant vectors and use knownbits/computenumsignbits? | |||
48314 | // Canonicalize zero_extend to LHS. | |||
48315 | if (N01.getOpcode() == ISD::ZERO_EXTEND) | |||
48316 | std::swap(N00, N01); | |||
48317 | if (N11.getOpcode() == ISD::ZERO_EXTEND) | |||
48318 | std::swap(N10, N11); | |||
48319 | ||||
48320 | // Ensure we have a zero_extend and a sign_extend. | |||
48321 | if (N00.getOpcode() != ISD::ZERO_EXTEND || | |||
48322 | N01.getOpcode() != ISD::SIGN_EXTEND || | |||
48323 | N10.getOpcode() != ISD::ZERO_EXTEND || | |||
48324 | N11.getOpcode() != ISD::SIGN_EXTEND) | |||
48325 | return SDValue(); | |||
48326 | ||||
48327 | // Peek through the extends. | |||
48328 | N00 = N00.getOperand(0); | |||
48329 | N01 = N01.getOperand(0); | |||
48330 | N10 = N10.getOperand(0); | |||
48331 | N11 = N11.getOperand(0); | |||
48332 | ||||
48333 | // Ensure the extend is from vXi8. | |||
48334 | if (N00.getValueType().getVectorElementType() != MVT::i8 || | |||
48335 | N01.getValueType().getVectorElementType() != MVT::i8 || | |||
48336 | N10.getValueType().getVectorElementType() != MVT::i8 || | |||
48337 | N11.getValueType().getVectorElementType() != MVT::i8) | |||
48338 | return SDValue(); | |||
48339 | ||||
48340 | // All inputs should be build_vectors. | |||
48341 | if (N00.getOpcode() != ISD::BUILD_VECTOR || | |||
48342 | N01.getOpcode() != ISD::BUILD_VECTOR || | |||
48343 | N10.getOpcode() != ISD::BUILD_VECTOR || | |||
48344 | N11.getOpcode() != ISD::BUILD_VECTOR) | |||
48345 | return SDValue(); | |||
48346 | ||||
48347 | // N00/N10 are zero extended. N01/N11 are sign extended. | |||
48348 | ||||
48349 | // For each element, we need to ensure we have an odd element from one vector | |||
48350 | // multiplied by the odd element of another vector and the even element from | |||
48351 | // one of the same vectors being multiplied by the even element from the | |||
48352 | // other vector. So we need to make sure for each element i, this operator | |||
48353 | // is being performed: | |||
48354 | // A[2 * i] * B[2 * i] + A[2 * i + 1] * B[2 * i + 1] | |||
48355 | SDValue ZExtIn, SExtIn; | |||
48356 | for (unsigned i = 0; i != NumElems; ++i) { | |||
48357 | SDValue N00Elt = N00.getOperand(i); | |||
48358 | SDValue N01Elt = N01.getOperand(i); | |||
48359 | SDValue N10Elt = N10.getOperand(i); | |||
48360 | SDValue N11Elt = N11.getOperand(i); | |||
48361 | // TODO: Be more tolerant to undefs. | |||
48362 | if (N00Elt.getOpcode() != ISD::EXTRACT_VECTOR_ELT || | |||
48363 | N01Elt.getOpcode() != ISD::EXTRACT_VECTOR_ELT || | |||
48364 | N10Elt.getOpcode() != ISD::EXTRACT_VECTOR_ELT || | |||
48365 | N11Elt.getOpcode() != ISD::EXTRACT_VECTOR_ELT) | |||
48366 | return SDValue(); | |||
48367 | auto *ConstN00Elt = dyn_cast<ConstantSDNode>(N00Elt.getOperand(1)); | |||
48368 | auto *ConstN01Elt = dyn_cast<ConstantSDNode>(N01Elt.getOperand(1)); | |||
48369 | auto *ConstN10Elt = dyn_cast<ConstantSDNode>(N10Elt.getOperand(1)); | |||
48370 | auto *ConstN11Elt = dyn_cast<ConstantSDNode>(N11Elt.getOperand(1)); | |||
48371 | if (!ConstN00Elt || !ConstN01Elt || !ConstN10Elt || !ConstN11Elt) | |||
48372 | return SDValue(); | |||
48373 | unsigned IdxN00 = ConstN00Elt->getZExtValue(); | |||
48374 | unsigned IdxN01 = ConstN01Elt->getZExtValue(); | |||
48375 | unsigned IdxN10 = ConstN10Elt->getZExtValue(); | |||
48376 | unsigned IdxN11 = ConstN11Elt->getZExtValue(); | |||
48377 | // Add is commutative so indices can be reordered. | |||
48378 | if (IdxN00 > IdxN10) { | |||
48379 | std::swap(IdxN00, IdxN10); | |||
48380 | std::swap(IdxN01, IdxN11); | |||
48381 | } | |||
48382 | // N0 indices be the even element. N1 indices must be the next odd element. | |||
48383 | if (IdxN00 != 2 * i || IdxN10 != 2 * i + 1 || | |||
48384 | IdxN01 != 2 * i || IdxN11 != 2 * i + 1) | |||
48385 | return SDValue(); | |||
48386 | SDValue N00In = N00Elt.getOperand(0); | |||
48387 | SDValue N01In = N01Elt.getOperand(0); | |||
48388 | SDValue N10In = N10Elt.getOperand(0); | |||
48389 | SDValue N11In = N11Elt.getOperand(0); | |||
48390 | // First time we find an input capture it. | |||
48391 | if (!ZExtIn) { | |||
48392 | ZExtIn = N00In; | |||
48393 | SExtIn = N01In; | |||
48394 | } | |||
48395 | if (ZExtIn != N00In || SExtIn != N01In || | |||
48396 | ZExtIn != N10In || SExtIn != N11In) | |||
48397 | return SDValue(); | |||
48398 | } | |||
48399 | ||||
48400 | auto PMADDBuilder = [](SelectionDAG &DAG, const SDLoc &DL, | |||
48401 | ArrayRef<SDValue> Ops) { | |||
48402 | // Shrink by adding truncate nodes and let DAGCombine fold with the | |||
48403 | // sources. | |||
48404 | EVT InVT = Ops[0].getValueType(); | |||
48405 | assert(InVT.getScalarType() == MVT::i8 &&(static_cast <bool> (InVT.getScalarType() == MVT::i8 && "Unexpected scalar element type") ? void (0) : __assert_fail ("InVT.getScalarType() == MVT::i8 && \"Unexpected scalar element type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 48406, __extension__ __PRETTY_FUNCTION__)) | |||
48406 | "Unexpected scalar element type")(static_cast <bool> (InVT.getScalarType() == MVT::i8 && "Unexpected scalar element type") ? void (0) : __assert_fail ("InVT.getScalarType() == MVT::i8 && \"Unexpected scalar element type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 48406, __extension__ __PRETTY_FUNCTION__)); | |||
48407 | assert(InVT == Ops[1].getValueType() && "Operands' types mismatch")(static_cast <bool> (InVT == Ops[1].getValueType() && "Operands' types mismatch") ? void (0) : __assert_fail ("InVT == Ops[1].getValueType() && \"Operands' types mismatch\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 48407, __extension__ __PRETTY_FUNCTION__)); | |||
48408 | EVT ResVT = EVT::getVectorVT(*DAG.getContext(), MVT::i16, | |||
48409 | InVT.getVectorNumElements() / 2); | |||
48410 | return DAG.getNode(X86ISD::VPMADDUBSW, DL, ResVT, Ops[0], Ops[1]); | |||
48411 | }; | |||
48412 | return SplitOpsAndApply(DAG, Subtarget, DL, VT, { ZExtIn, SExtIn }, | |||
48413 | PMADDBuilder); | |||
48414 | } | |||
48415 | ||||
48416 | static SDValue combineTruncate(SDNode *N, SelectionDAG &DAG, | |||
48417 | const X86Subtarget &Subtarget) { | |||
48418 | EVT VT = N->getValueType(0); | |||
48419 | SDValue Src = N->getOperand(0); | |||
48420 | SDLoc DL(N); | |||
48421 | ||||
48422 | // Attempt to pre-truncate inputs to arithmetic ops instead. | |||
48423 | if (SDValue V = combineTruncatedArithmetic(N, DAG, Subtarget, DL)) | |||
48424 | return V; | |||
48425 | ||||
48426 | // Try to detect AVG pattern first. | |||
48427 | if (SDValue Avg = detectAVGPattern(Src, VT, DAG, Subtarget, DL)) | |||
48428 | return Avg; | |||
48429 | ||||
48430 | // Try to detect PMADD | |||
48431 | if (SDValue PMAdd = detectPMADDUBSW(Src, VT, DAG, Subtarget, DL)) | |||
48432 | return PMAdd; | |||
48433 | ||||
48434 | // Try to combine truncation with signed/unsigned saturation. | |||
48435 | if (SDValue Val = combineTruncateWithSat(Src, VT, DL, DAG, Subtarget)) | |||
48436 | return Val; | |||
48437 | ||||
48438 | // Try to combine PMULHUW/PMULHW for vXi16. | |||
48439 | if (SDValue V = combinePMULH(Src, VT, DL, DAG, Subtarget)) | |||
48440 | return V; | |||
48441 | ||||
48442 | // The bitcast source is a direct mmx result. | |||
48443 | // Detect bitcasts between i32 to x86mmx | |||
48444 | if (Src.getOpcode() == ISD::BITCAST && VT == MVT::i32) { | |||
48445 | SDValue BCSrc = Src.getOperand(0); | |||
48446 | if (BCSrc.getValueType() == MVT::x86mmx) | |||
48447 | return DAG.getNode(X86ISD::MMX_MOVD2W, DL, MVT::i32, BCSrc); | |||
48448 | } | |||
48449 | ||||
48450 | // Try to truncate extended sign/zero bits with PACKSS/PACKUS. | |||
48451 | if (SDValue V = combineVectorSignBitsTruncation(N, DL, DAG, Subtarget)) | |||
48452 | return V; | |||
48453 | ||||
48454 | return combineVectorTruncation(N, DAG, Subtarget); | |||
48455 | } | |||
48456 | ||||
48457 | static SDValue combineVTRUNC(SDNode *N, SelectionDAG &DAG, | |||
48458 | TargetLowering::DAGCombinerInfo &DCI) { | |||
48459 | EVT VT = N->getValueType(0); | |||
48460 | SDValue In = N->getOperand(0); | |||
48461 | SDLoc DL(N); | |||
48462 | ||||
48463 | if (auto SSatVal = detectSSatPattern(In, VT)) | |||
48464 | return DAG.getNode(X86ISD::VTRUNCS, DL, VT, SSatVal); | |||
48465 | if (auto USatVal = detectUSatPattern(In, VT, DAG, DL)) | |||
48466 | return DAG.getNode(X86ISD::VTRUNCUS, DL, VT, USatVal); | |||
48467 | ||||
48468 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
48469 | APInt DemandedMask(APInt::getAllOnes(VT.getScalarSizeInBits())); | |||
48470 | if (TLI.SimplifyDemandedBits(SDValue(N, 0), DemandedMask, DCI)) | |||
48471 | return SDValue(N, 0); | |||
48472 | ||||
48473 | return SDValue(); | |||
48474 | } | |||
48475 | ||||
48476 | /// Returns the negated value if the node \p N flips sign of FP value. | |||
48477 | /// | |||
48478 | /// FP-negation node may have different forms: FNEG(x), FXOR (x, 0x80000000) | |||
48479 | /// or FSUB(0, x) | |||
48480 | /// AVX512F does not have FXOR, so FNEG is lowered as | |||
48481 | /// (bitcast (xor (bitcast x), (bitcast ConstantFP(0x80000000)))). | |||
48482 | /// In this case we go though all bitcasts. | |||
48483 | /// This also recognizes splat of a negated value and returns the splat of that | |||
48484 | /// value. | |||
48485 | static SDValue isFNEG(SelectionDAG &DAG, SDNode *N, unsigned Depth = 0) { | |||
48486 | if (N->getOpcode() == ISD::FNEG) | |||
48487 | return N->getOperand(0); | |||
48488 | ||||
48489 | // Don't recurse exponentially. | |||
48490 | if (Depth > SelectionDAG::MaxRecursionDepth) | |||
48491 | return SDValue(); | |||
48492 | ||||
48493 | unsigned ScalarSize = N->getValueType(0).getScalarSizeInBits(); | |||
48494 | ||||
48495 | SDValue Op = peekThroughBitcasts(SDValue(N, 0)); | |||
48496 | EVT VT = Op->getValueType(0); | |||
48497 | ||||
48498 | // Make sure the element size doesn't change. | |||
48499 | if (VT.getScalarSizeInBits() != ScalarSize) | |||
48500 | return SDValue(); | |||
48501 | ||||
48502 | unsigned Opc = Op.getOpcode(); | |||
48503 | switch (Opc) { | |||
48504 | case ISD::VECTOR_SHUFFLE: { | |||
48505 | // For a VECTOR_SHUFFLE(VEC1, VEC2), if the VEC2 is undef, then the negate | |||
48506 | // of this is VECTOR_SHUFFLE(-VEC1, UNDEF). The mask can be anything here. | |||
48507 | if (!Op.getOperand(1).isUndef()) | |||
48508 | return SDValue(); | |||
48509 | if (SDValue NegOp0 = isFNEG(DAG, Op.getOperand(0).getNode(), Depth + 1)) | |||
48510 | if (NegOp0.getValueType() == VT) // FIXME: Can we do better? | |||
48511 | return DAG.getVectorShuffle(VT, SDLoc(Op), NegOp0, DAG.getUNDEF(VT), | |||
48512 | cast<ShuffleVectorSDNode>(Op)->getMask()); | |||
48513 | break; | |||
48514 | } | |||
48515 | case ISD::INSERT_VECTOR_ELT: { | |||
48516 | // Negate of INSERT_VECTOR_ELT(UNDEF, V, INDEX) is INSERT_VECTOR_ELT(UNDEF, | |||
48517 | // -V, INDEX). | |||
48518 | SDValue InsVector = Op.getOperand(0); | |||
48519 | SDValue InsVal = Op.getOperand(1); | |||
48520 | if (!InsVector.isUndef()) | |||
48521 | return SDValue(); | |||
48522 | if (SDValue NegInsVal = isFNEG(DAG, InsVal.getNode(), Depth + 1)) | |||
48523 | if (NegInsVal.getValueType() == VT.getVectorElementType()) // FIXME | |||
48524 | return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(Op), VT, InsVector, | |||
48525 | NegInsVal, Op.getOperand(2)); | |||
48526 | break; | |||
48527 | } | |||
48528 | case ISD::FSUB: | |||
48529 | case ISD::XOR: | |||
48530 | case X86ISD::FXOR: { | |||
48531 | SDValue Op1 = Op.getOperand(1); | |||
48532 | SDValue Op0 = Op.getOperand(0); | |||
48533 | ||||
48534 | // For XOR and FXOR, we want to check if constant | |||
48535 | // bits of Op1 are sign bit masks. For FSUB, we | |||
48536 | // have to check if constant bits of Op0 are sign | |||
48537 | // bit masks and hence we swap the operands. | |||
48538 | if (Opc == ISD::FSUB) | |||
48539 | std::swap(Op0, Op1); | |||
48540 | ||||
48541 | APInt UndefElts; | |||
48542 | SmallVector<APInt, 16> EltBits; | |||
48543 | // Extract constant bits and see if they are all | |||
48544 | // sign bit masks. Ignore the undef elements. | |||
48545 | if (getTargetConstantBitsFromNode(Op1, ScalarSize, UndefElts, EltBits, | |||
48546 | /* AllowWholeUndefs */ true, | |||
48547 | /* AllowPartialUndefs */ false)) { | |||
48548 | for (unsigned I = 0, E = EltBits.size(); I < E; I++) | |||
48549 | if (!UndefElts[I] && !EltBits[I].isSignMask()) | |||
48550 | return SDValue(); | |||
48551 | ||||
48552 | return peekThroughBitcasts(Op0); | |||
48553 | } | |||
48554 | } | |||
48555 | } | |||
48556 | ||||
48557 | return SDValue(); | |||
48558 | } | |||
48559 | ||||
48560 | static unsigned negateFMAOpcode(unsigned Opcode, bool NegMul, bool NegAcc, | |||
48561 | bool NegRes) { | |||
48562 | if (NegMul) { | |||
48563 | switch (Opcode) { | |||
48564 | default: llvm_unreachable("Unexpected opcode")::llvm::llvm_unreachable_internal("Unexpected opcode", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 48564); | |||
48565 | case ISD::FMA: Opcode = X86ISD::FNMADD; break; | |||
48566 | case ISD::STRICT_FMA: Opcode = X86ISD::STRICT_FNMADD; break; | |||
48567 | case X86ISD::FMADD_RND: Opcode = X86ISD::FNMADD_RND; break; | |||
48568 | case X86ISD::FMSUB: Opcode = X86ISD::FNMSUB; break; | |||
48569 | case X86ISD::STRICT_FMSUB: Opcode = X86ISD::STRICT_FNMSUB; break; | |||
48570 | case X86ISD::FMSUB_RND: Opcode = X86ISD::FNMSUB_RND; break; | |||
48571 | case X86ISD::FNMADD: Opcode = ISD::FMA; break; | |||
48572 | case X86ISD::STRICT_FNMADD: Opcode = ISD::STRICT_FMA; break; | |||
48573 | case X86ISD::FNMADD_RND: Opcode = X86ISD::FMADD_RND; break; | |||
48574 | case X86ISD::FNMSUB: Opcode = X86ISD::FMSUB; break; | |||
48575 | case X86ISD::STRICT_FNMSUB: Opcode = X86ISD::STRICT_FMSUB; break; | |||
48576 | case X86ISD::FNMSUB_RND: Opcode = X86ISD::FMSUB_RND; break; | |||
48577 | } | |||
48578 | } | |||
48579 | ||||
48580 | if (NegAcc) { | |||
48581 | switch (Opcode) { | |||
48582 | default: llvm_unreachable("Unexpected opcode")::llvm::llvm_unreachable_internal("Unexpected opcode", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 48582); | |||
48583 | case ISD::FMA: Opcode = X86ISD::FMSUB; break; | |||
48584 | case ISD::STRICT_FMA: Opcode = X86ISD::STRICT_FMSUB; break; | |||
48585 | case X86ISD::FMADD_RND: Opcode = X86ISD::FMSUB_RND; break; | |||
48586 | case X86ISD::FMSUB: Opcode = ISD::FMA; break; | |||
48587 | case X86ISD::STRICT_FMSUB: Opcode = ISD::STRICT_FMA; break; | |||
48588 | case X86ISD::FMSUB_RND: Opcode = X86ISD::FMADD_RND; break; | |||
48589 | case X86ISD::FNMADD: Opcode = X86ISD::FNMSUB; break; | |||
48590 | case X86ISD::STRICT_FNMADD: Opcode = X86ISD::STRICT_FNMSUB; break; | |||
48591 | case X86ISD::FNMADD_RND: Opcode = X86ISD::FNMSUB_RND; break; | |||
48592 | case X86ISD::FNMSUB: Opcode = X86ISD::FNMADD; break; | |||
48593 | case X86ISD::STRICT_FNMSUB: Opcode = X86ISD::STRICT_FNMADD; break; | |||
48594 | case X86ISD::FNMSUB_RND: Opcode = X86ISD::FNMADD_RND; break; | |||
48595 | case X86ISD::FMADDSUB: Opcode = X86ISD::FMSUBADD; break; | |||
48596 | case X86ISD::FMADDSUB_RND: Opcode = X86ISD::FMSUBADD_RND; break; | |||
48597 | case X86ISD::FMSUBADD: Opcode = X86ISD::FMADDSUB; break; | |||
48598 | case X86ISD::FMSUBADD_RND: Opcode = X86ISD::FMADDSUB_RND; break; | |||
48599 | } | |||
48600 | } | |||
48601 | ||||
48602 | if (NegRes) { | |||
48603 | switch (Opcode) { | |||
48604 | // For accuracy reason, we never combine fneg and fma under strict FP. | |||
48605 | default: llvm_unreachable("Unexpected opcode")::llvm::llvm_unreachable_internal("Unexpected opcode", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 48605); | |||
48606 | case ISD::FMA: Opcode = X86ISD::FNMSUB; break; | |||
48607 | case X86ISD::FMADD_RND: Opcode = X86ISD::FNMSUB_RND; break; | |||
48608 | case X86ISD::FMSUB: Opcode = X86ISD::FNMADD; break; | |||
48609 | case X86ISD::FMSUB_RND: Opcode = X86ISD::FNMADD_RND; break; | |||
48610 | case X86ISD::FNMADD: Opcode = X86ISD::FMSUB; break; | |||
48611 | case X86ISD::FNMADD_RND: Opcode = X86ISD::FMSUB_RND; break; | |||
48612 | case X86ISD::FNMSUB: Opcode = ISD::FMA; break; | |||
48613 | case X86ISD::FNMSUB_RND: Opcode = X86ISD::FMADD_RND; break; | |||
48614 | } | |||
48615 | } | |||
48616 | ||||
48617 | return Opcode; | |||
48618 | } | |||
48619 | ||||
48620 | /// Do target-specific dag combines on floating point negations. | |||
48621 | static SDValue combineFneg(SDNode *N, SelectionDAG &DAG, | |||
48622 | TargetLowering::DAGCombinerInfo &DCI, | |||
48623 | const X86Subtarget &Subtarget) { | |||
48624 | EVT OrigVT = N->getValueType(0); | |||
48625 | SDValue Arg = isFNEG(DAG, N); | |||
48626 | if (!Arg) | |||
48627 | return SDValue(); | |||
48628 | ||||
48629 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
48630 | EVT VT = Arg.getValueType(); | |||
48631 | EVT SVT = VT.getScalarType(); | |||
48632 | SDLoc DL(N); | |||
48633 | ||||
48634 | // Let legalize expand this if it isn't a legal type yet. | |||
48635 | if (!TLI.isTypeLegal(VT)) | |||
48636 | return SDValue(); | |||
48637 | ||||
48638 | // If we're negating a FMUL node on a target with FMA, then we can avoid the | |||
48639 | // use of a constant by performing (-0 - A*B) instead. | |||
48640 | // FIXME: Check rounding control flags as well once it becomes available. | |||
48641 | if (Arg.getOpcode() == ISD::FMUL && (SVT == MVT::f32 || SVT == MVT::f64) && | |||
48642 | Arg->getFlags().hasNoSignedZeros() && Subtarget.hasAnyFMA()) { | |||
48643 | SDValue Zero = DAG.getConstantFP(0.0, DL, VT); | |||
48644 | SDValue NewNode = DAG.getNode(X86ISD::FNMSUB, DL, VT, Arg.getOperand(0), | |||
48645 | Arg.getOperand(1), Zero); | |||
48646 | return DAG.getBitcast(OrigVT, NewNode); | |||
48647 | } | |||
48648 | ||||
48649 | bool CodeSize = DAG.getMachineFunction().getFunction().hasOptSize(); | |||
48650 | bool LegalOperations = !DCI.isBeforeLegalizeOps(); | |||
48651 | if (SDValue NegArg = | |||
48652 | TLI.getNegatedExpression(Arg, DAG, LegalOperations, CodeSize)) | |||
48653 | return DAG.getBitcast(OrigVT, NegArg); | |||
48654 | ||||
48655 | return SDValue(); | |||
48656 | } | |||
48657 | ||||
48658 | SDValue X86TargetLowering::getNegatedExpression(SDValue Op, SelectionDAG &DAG, | |||
48659 | bool LegalOperations, | |||
48660 | bool ForCodeSize, | |||
48661 | NegatibleCost &Cost, | |||
48662 | unsigned Depth) const { | |||
48663 | // fneg patterns are removable even if they have multiple uses. | |||
48664 | if (SDValue Arg = isFNEG(DAG, Op.getNode(), Depth)) { | |||
48665 | Cost = NegatibleCost::Cheaper; | |||
48666 | return DAG.getBitcast(Op.getValueType(), Arg); | |||
48667 | } | |||
48668 | ||||
48669 | EVT VT = Op.getValueType(); | |||
48670 | EVT SVT = VT.getScalarType(); | |||
48671 | unsigned Opc = Op.getOpcode(); | |||
48672 | SDNodeFlags Flags = Op.getNode()->getFlags(); | |||
48673 | switch (Opc) { | |||
48674 | case ISD::FMA: | |||
48675 | case X86ISD::FMSUB: | |||
48676 | case X86ISD::FNMADD: | |||
48677 | case X86ISD::FNMSUB: | |||
48678 | case X86ISD::FMADD_RND: | |||
48679 | case X86ISD::FMSUB_RND: | |||
48680 | case X86ISD::FNMADD_RND: | |||
48681 | case X86ISD::FNMSUB_RND: { | |||
48682 | if (!Op.hasOneUse() || !Subtarget.hasAnyFMA() || !isTypeLegal(VT) || | |||
48683 | !(SVT == MVT::f32 || SVT == MVT::f64) || | |||
48684 | !isOperationLegal(ISD::FMA, VT)) | |||
48685 | break; | |||
48686 | ||||
48687 | // Don't fold (fneg (fma (fneg x), y, (fneg z))) to (fma x, y, z) | |||
48688 | // if it may have signed zeros. | |||
48689 | if (!Flags.hasNoSignedZeros()) | |||
48690 | break; | |||
48691 | ||||
48692 | // This is always negatible for free but we might be able to remove some | |||
48693 | // extra operand negations as well. | |||
48694 | SmallVector<SDValue, 4> NewOps(Op.getNumOperands(), SDValue()); | |||
48695 | for (int i = 0; i != 3; ++i) | |||
48696 | NewOps[i] = getCheaperNegatedExpression( | |||
48697 | Op.getOperand(i), DAG, LegalOperations, ForCodeSize, Depth + 1); | |||
48698 | ||||
48699 | bool NegA = !!NewOps[0]; | |||
48700 | bool NegB = !!NewOps[1]; | |||
48701 | bool NegC = !!NewOps[2]; | |||
48702 | unsigned NewOpc = negateFMAOpcode(Opc, NegA != NegB, NegC, true); | |||
48703 | ||||
48704 | Cost = (NegA || NegB || NegC) ? NegatibleCost::Cheaper | |||
48705 | : NegatibleCost::Neutral; | |||
48706 | ||||
48707 | // Fill in the non-negated ops with the original values. | |||
48708 | for (int i = 0, e = Op.getNumOperands(); i != e; ++i) | |||
48709 | if (!NewOps[i]) | |||
48710 | NewOps[i] = Op.getOperand(i); | |||
48711 | return DAG.getNode(NewOpc, SDLoc(Op), VT, NewOps); | |||
48712 | } | |||
48713 | case X86ISD::FRCP: | |||
48714 | if (SDValue NegOp0 = | |||
48715 | getNegatedExpression(Op.getOperand(0), DAG, LegalOperations, | |||
48716 | ForCodeSize, Cost, Depth + 1)) | |||
48717 | return DAG.getNode(Opc, SDLoc(Op), VT, NegOp0); | |||
48718 | break; | |||
48719 | } | |||
48720 | ||||
48721 | return TargetLowering::getNegatedExpression(Op, DAG, LegalOperations, | |||
48722 | ForCodeSize, Cost, Depth); | |||
48723 | } | |||
48724 | ||||
48725 | static SDValue lowerX86FPLogicOp(SDNode *N, SelectionDAG &DAG, | |||
48726 | const X86Subtarget &Subtarget) { | |||
48727 | MVT VT = N->getSimpleValueType(0); | |||
48728 | // If we have integer vector types available, use the integer opcodes. | |||
48729 | if (!VT.isVector() || !Subtarget.hasSSE2()) | |||
48730 | return SDValue(); | |||
48731 | ||||
48732 | SDLoc dl(N); | |||
48733 | ||||
48734 | unsigned IntBits = VT.getScalarSizeInBits(); | |||
48735 | MVT IntSVT = MVT::getIntegerVT(IntBits); | |||
48736 | MVT IntVT = MVT::getVectorVT(IntSVT, VT.getSizeInBits() / IntBits); | |||
48737 | ||||
48738 | SDValue Op0 = DAG.getBitcast(IntVT, N->getOperand(0)); | |||
48739 | SDValue Op1 = DAG.getBitcast(IntVT, N->getOperand(1)); | |||
48740 | unsigned IntOpcode; | |||
48741 | switch (N->getOpcode()) { | |||
48742 | default: llvm_unreachable("Unexpected FP logic op")::llvm::llvm_unreachable_internal("Unexpected FP logic op", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 48742); | |||
48743 | case X86ISD::FOR: IntOpcode = ISD::OR; break; | |||
48744 | case X86ISD::FXOR: IntOpcode = ISD::XOR; break; | |||
48745 | case X86ISD::FAND: IntOpcode = ISD::AND; break; | |||
48746 | case X86ISD::FANDN: IntOpcode = X86ISD::ANDNP; break; | |||
48747 | } | |||
48748 | SDValue IntOp = DAG.getNode(IntOpcode, dl, IntVT, Op0, Op1); | |||
48749 | return DAG.getBitcast(VT, IntOp); | |||
48750 | } | |||
48751 | ||||
48752 | ||||
48753 | /// Fold a xor(setcc cond, val), 1 --> setcc (inverted(cond), val) | |||
48754 | static SDValue foldXor1SetCC(SDNode *N, SelectionDAG &DAG) { | |||
48755 | if (N->getOpcode() != ISD::XOR) | |||
48756 | return SDValue(); | |||
48757 | ||||
48758 | SDValue LHS = N->getOperand(0); | |||
48759 | if (!isOneConstant(N->getOperand(1)) || LHS->getOpcode() != X86ISD::SETCC) | |||
48760 | return SDValue(); | |||
48761 | ||||
48762 | X86::CondCode NewCC = X86::GetOppositeBranchCondition( | |||
48763 | X86::CondCode(LHS->getConstantOperandVal(0))); | |||
48764 | SDLoc DL(N); | |||
48765 | return getSETCC(NewCC, LHS->getOperand(1), DL, DAG); | |||
48766 | } | |||
48767 | ||||
48768 | static SDValue combineXor(SDNode *N, SelectionDAG &DAG, | |||
48769 | TargetLowering::DAGCombinerInfo &DCI, | |||
48770 | const X86Subtarget &Subtarget) { | |||
48771 | SDValue N0 = N->getOperand(0); | |||
48772 | SDValue N1 = N->getOperand(1); | |||
48773 | EVT VT = N->getValueType(0); | |||
48774 | ||||
48775 | // If this is SSE1 only convert to FXOR to avoid scalarization. | |||
48776 | if (Subtarget.hasSSE1() && !Subtarget.hasSSE2() && VT == MVT::v4i32) { | |||
48777 | return DAG.getBitcast(MVT::v4i32, | |||
48778 | DAG.getNode(X86ISD::FXOR, SDLoc(N), MVT::v4f32, | |||
48779 | DAG.getBitcast(MVT::v4f32, N0), | |||
48780 | DAG.getBitcast(MVT::v4f32, N1))); | |||
48781 | } | |||
48782 | ||||
48783 | if (SDValue Cmp = foldVectorXorShiftIntoCmp(N, DAG, Subtarget)) | |||
48784 | return Cmp; | |||
48785 | ||||
48786 | if (SDValue R = combineBitOpWithMOVMSK(N, DAG)) | |||
48787 | return R; | |||
48788 | ||||
48789 | if (SDValue FPLogic = convertIntLogicToFPLogic(N, DAG, DCI, Subtarget)) | |||
48790 | return FPLogic; | |||
48791 | ||||
48792 | if (DCI.isBeforeLegalizeOps()) | |||
48793 | return SDValue(); | |||
48794 | ||||
48795 | if (SDValue SetCC = foldXor1SetCC(N, DAG)) | |||
48796 | return SetCC; | |||
48797 | ||||
48798 | if (SDValue RV = foldXorTruncShiftIntoCmp(N, DAG)) | |||
48799 | return RV; | |||
48800 | ||||
48801 | // Fold not(iX bitcast(vXi1)) -> (iX bitcast(not(vec))) for legal boolvecs. | |||
48802 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
48803 | if (llvm::isAllOnesConstant(N1) && N0.getOpcode() == ISD::BITCAST && | |||
48804 | N0.getOperand(0).getValueType().isVector() && | |||
48805 | N0.getOperand(0).getValueType().getVectorElementType() == MVT::i1 && | |||
48806 | TLI.isTypeLegal(N0.getOperand(0).getValueType()) && N0.hasOneUse()) { | |||
48807 | return DAG.getBitcast(VT, DAG.getNOT(SDLoc(N), N0.getOperand(0), | |||
48808 | N0.getOperand(0).getValueType())); | |||
48809 | } | |||
48810 | ||||
48811 | // Handle AVX512 mask widening. | |||
48812 | // Fold not(insert_subvector(undef,sub)) -> insert_subvector(undef,not(sub)) | |||
48813 | if (ISD::isBuildVectorAllOnes(N1.getNode()) && VT.isVector() && | |||
48814 | VT.getVectorElementType() == MVT::i1 && | |||
48815 | N0.getOpcode() == ISD::INSERT_SUBVECTOR && N0.getOperand(0).isUndef() && | |||
48816 | TLI.isTypeLegal(N0.getOperand(1).getValueType())) { | |||
48817 | return DAG.getNode( | |||
48818 | ISD::INSERT_SUBVECTOR, SDLoc(N), VT, N0.getOperand(0), | |||
48819 | DAG.getNOT(SDLoc(N), N0.getOperand(1), N0.getOperand(1).getValueType()), | |||
48820 | N0.getOperand(2)); | |||
48821 | } | |||
48822 | ||||
48823 | // Fold xor(zext(xor(x,c1)),c2) -> xor(zext(x),xor(zext(c1),c2)) | |||
48824 | // Fold xor(truncate(xor(x,c1)),c2) -> xor(truncate(x),xor(truncate(c1),c2)) | |||
48825 | // TODO: Under what circumstances could this be performed in DAGCombine? | |||
48826 | if ((N0.getOpcode() == ISD::TRUNCATE || N0.getOpcode() == ISD::ZERO_EXTEND) && | |||
48827 | N0.getOperand(0).getOpcode() == N->getOpcode()) { | |||
48828 | SDValue TruncExtSrc = N0.getOperand(0); | |||
48829 | auto *N1C = dyn_cast<ConstantSDNode>(N1); | |||
48830 | auto *N001C = dyn_cast<ConstantSDNode>(TruncExtSrc.getOperand(1)); | |||
48831 | if (N1C && !N1C->isOpaque() && N001C && !N001C->isOpaque()) { | |||
48832 | SDLoc DL(N); | |||
48833 | SDValue LHS = DAG.getZExtOrTrunc(TruncExtSrc.getOperand(0), DL, VT); | |||
48834 | SDValue RHS = DAG.getZExtOrTrunc(TruncExtSrc.getOperand(1), DL, VT); | |||
48835 | return DAG.getNode(ISD::XOR, DL, VT, LHS, | |||
48836 | DAG.getNode(ISD::XOR, DL, VT, RHS, N1)); | |||
48837 | } | |||
48838 | } | |||
48839 | ||||
48840 | return combineFneg(N, DAG, DCI, Subtarget); | |||
48841 | } | |||
48842 | ||||
48843 | static SDValue combineBEXTR(SDNode *N, SelectionDAG &DAG, | |||
48844 | TargetLowering::DAGCombinerInfo &DCI, | |||
48845 | const X86Subtarget &Subtarget) { | |||
48846 | EVT VT = N->getValueType(0); | |||
48847 | unsigned NumBits = VT.getSizeInBits(); | |||
48848 | ||||
48849 | // TODO - Constant Folding. | |||
48850 | ||||
48851 | // Simplify the inputs. | |||
48852 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
48853 | APInt DemandedMask(APInt::getAllOnes(NumBits)); | |||
48854 | if (TLI.SimplifyDemandedBits(SDValue(N, 0), DemandedMask, DCI)) | |||
48855 | return SDValue(N, 0); | |||
48856 | ||||
48857 | return SDValue(); | |||
48858 | } | |||
48859 | ||||
48860 | static bool isNullFPScalarOrVectorConst(SDValue V) { | |||
48861 | return isNullFPConstant(V) || ISD::isBuildVectorAllZeros(V.getNode()); | |||
48862 | } | |||
48863 | ||||
48864 | /// If a value is a scalar FP zero or a vector FP zero (potentially including | |||
48865 | /// undefined elements), return a zero constant that may be used to fold away | |||
48866 | /// that value. In the case of a vector, the returned constant will not contain | |||
48867 | /// undefined elements even if the input parameter does. This makes it suitable | |||
48868 | /// to be used as a replacement operand with operations (eg, bitwise-and) where | |||
48869 | /// an undef should not propagate. | |||
48870 | static SDValue getNullFPConstForNullVal(SDValue V, SelectionDAG &DAG, | |||
48871 | const X86Subtarget &Subtarget) { | |||
48872 | if (!isNullFPScalarOrVectorConst(V)) | |||
48873 | return SDValue(); | |||
48874 | ||||
48875 | if (V.getValueType().isVector()) | |||
48876 | return getZeroVector(V.getSimpleValueType(), Subtarget, DAG, SDLoc(V)); | |||
48877 | ||||
48878 | return V; | |||
48879 | } | |||
48880 | ||||
48881 | static SDValue combineFAndFNotToFAndn(SDNode *N, SelectionDAG &DAG, | |||
48882 | const X86Subtarget &Subtarget) { | |||
48883 | SDValue N0 = N->getOperand(0); | |||
48884 | SDValue N1 = N->getOperand(1); | |||
48885 | EVT VT = N->getValueType(0); | |||
48886 | SDLoc DL(N); | |||
48887 | ||||
48888 | // Vector types are handled in combineANDXORWithAllOnesIntoANDNP(). | |||
48889 | if (!((VT == MVT::f32 && Subtarget.hasSSE1()) || | |||
48890 | (VT == MVT::f64 && Subtarget.hasSSE2()) || | |||
48891 | (VT == MVT::v4f32 && Subtarget.hasSSE1() && !Subtarget.hasSSE2()))) | |||
48892 | return SDValue(); | |||
48893 | ||||
48894 | auto isAllOnesConstantFP = [](SDValue V) { | |||
48895 | if (V.getSimpleValueType().isVector()) | |||
48896 | return ISD::isBuildVectorAllOnes(V.getNode()); | |||
48897 | auto *C = dyn_cast<ConstantFPSDNode>(V); | |||
48898 | return C && C->getConstantFPValue()->isAllOnesValue(); | |||
48899 | }; | |||
48900 | ||||
48901 | // fand (fxor X, -1), Y --> fandn X, Y | |||
48902 | if (N0.getOpcode() == X86ISD::FXOR && isAllOnesConstantFP(N0.getOperand(1))) | |||
48903 | return DAG.getNode(X86ISD::FANDN, DL, VT, N0.getOperand(0), N1); | |||
48904 | ||||
48905 | // fand X, (fxor Y, -1) --> fandn Y, X | |||
48906 | if (N1.getOpcode() == X86ISD::FXOR && isAllOnesConstantFP(N1.getOperand(1))) | |||
48907 | return DAG.getNode(X86ISD::FANDN, DL, VT, N1.getOperand(0), N0); | |||
48908 | ||||
48909 | return SDValue(); | |||
48910 | } | |||
48911 | ||||
48912 | /// Do target-specific dag combines on X86ISD::FAND nodes. | |||
48913 | static SDValue combineFAnd(SDNode *N, SelectionDAG &DAG, | |||
48914 | const X86Subtarget &Subtarget) { | |||
48915 | // FAND(0.0, x) -> 0.0 | |||
48916 | if (SDValue V = getNullFPConstForNullVal(N->getOperand(0), DAG, Subtarget)) | |||
48917 | return V; | |||
48918 | ||||
48919 | // FAND(x, 0.0) -> 0.0 | |||
48920 | if (SDValue V = getNullFPConstForNullVal(N->getOperand(1), DAG, Subtarget)) | |||
48921 | return V; | |||
48922 | ||||
48923 | if (SDValue V = combineFAndFNotToFAndn(N, DAG, Subtarget)) | |||
48924 | return V; | |||
48925 | ||||
48926 | return lowerX86FPLogicOp(N, DAG, Subtarget); | |||
48927 | } | |||
48928 | ||||
48929 | /// Do target-specific dag combines on X86ISD::FANDN nodes. | |||
48930 | static SDValue combineFAndn(SDNode *N, SelectionDAG &DAG, | |||
48931 | const X86Subtarget &Subtarget) { | |||
48932 | // FANDN(0.0, x) -> x | |||
48933 | if (isNullFPScalarOrVectorConst(N->getOperand(0))) | |||
48934 | return N->getOperand(1); | |||
48935 | ||||
48936 | // FANDN(x, 0.0) -> 0.0 | |||
48937 | if (SDValue V = getNullFPConstForNullVal(N->getOperand(1), DAG, Subtarget)) | |||
48938 | return V; | |||
48939 | ||||
48940 | return lowerX86FPLogicOp(N, DAG, Subtarget); | |||
48941 | } | |||
48942 | ||||
48943 | /// Do target-specific dag combines on X86ISD::FOR and X86ISD::FXOR nodes. | |||
48944 | static SDValue combineFOr(SDNode *N, SelectionDAG &DAG, | |||
48945 | TargetLowering::DAGCombinerInfo &DCI, | |||
48946 | const X86Subtarget &Subtarget) { | |||
48947 | assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR)(static_cast <bool> (N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR) ? void (0) : __assert_fail ("N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 48947, __extension__ __PRETTY_FUNCTION__)); | |||
48948 | ||||
48949 | // F[X]OR(0.0, x) -> x | |||
48950 | if (isNullFPScalarOrVectorConst(N->getOperand(0))) | |||
48951 | return N->getOperand(1); | |||
48952 | ||||
48953 | // F[X]OR(x, 0.0) -> x | |||
48954 | if (isNullFPScalarOrVectorConst(N->getOperand(1))) | |||
48955 | return N->getOperand(0); | |||
48956 | ||||
48957 | if (SDValue NewVal = combineFneg(N, DAG, DCI, Subtarget)) | |||
48958 | return NewVal; | |||
48959 | ||||
48960 | return lowerX86FPLogicOp(N, DAG, Subtarget); | |||
48961 | } | |||
48962 | ||||
48963 | /// Do target-specific dag combines on X86ISD::FMIN and X86ISD::FMAX nodes. | |||
48964 | static SDValue combineFMinFMax(SDNode *N, SelectionDAG &DAG) { | |||
48965 | assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX)(static_cast <bool> (N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX) ? void (0) : __assert_fail ("N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 48965, __extension__ __PRETTY_FUNCTION__)); | |||
48966 | ||||
48967 | // FMIN/FMAX are commutative if no NaNs and no negative zeros are allowed. | |||
48968 | if (!DAG.getTarget().Options.NoNaNsFPMath || | |||
48969 | !DAG.getTarget().Options.NoSignedZerosFPMath) | |||
48970 | return SDValue(); | |||
48971 | ||||
48972 | // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes | |||
48973 | // into FMINC and FMAXC, which are Commutative operations. | |||
48974 | unsigned NewOp = 0; | |||
48975 | switch (N->getOpcode()) { | |||
48976 | default: llvm_unreachable("unknown opcode")::llvm::llvm_unreachable_internal("unknown opcode", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 48976); | |||
48977 | case X86ISD::FMIN: NewOp = X86ISD::FMINC; break; | |||
48978 | case X86ISD::FMAX: NewOp = X86ISD::FMAXC; break; | |||
48979 | } | |||
48980 | ||||
48981 | return DAG.getNode(NewOp, SDLoc(N), N->getValueType(0), | |||
48982 | N->getOperand(0), N->getOperand(1)); | |||
48983 | } | |||
48984 | ||||
48985 | static SDValue combineFMinNumFMaxNum(SDNode *N, SelectionDAG &DAG, | |||
48986 | const X86Subtarget &Subtarget) { | |||
48987 | if (Subtarget.useSoftFloat()) | |||
48988 | return SDValue(); | |||
48989 | ||||
48990 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
48991 | ||||
48992 | EVT VT = N->getValueType(0); | |||
48993 | if (!((Subtarget.hasSSE1() && VT == MVT::f32) || | |||
48994 | (Subtarget.hasSSE2() && VT == MVT::f64) || | |||
48995 | (Subtarget.hasFP16() && VT == MVT::f16) || | |||
48996 | (VT.isVector() && TLI.isTypeLegal(VT)))) | |||
48997 | return SDValue(); | |||
48998 | ||||
48999 | SDValue Op0 = N->getOperand(0); | |||
49000 | SDValue Op1 = N->getOperand(1); | |||
49001 | SDLoc DL(N); | |||
49002 | auto MinMaxOp = N->getOpcode() == ISD::FMAXNUM ? X86ISD::FMAX : X86ISD::FMIN; | |||
49003 | ||||
49004 | // If we don't have to respect NaN inputs, this is a direct translation to x86 | |||
49005 | // min/max instructions. | |||
49006 | if (DAG.getTarget().Options.NoNaNsFPMath || N->getFlags().hasNoNaNs()) | |||
49007 | return DAG.getNode(MinMaxOp, DL, VT, Op0, Op1, N->getFlags()); | |||
49008 | ||||
49009 | // If one of the operands is known non-NaN use the native min/max instructions | |||
49010 | // with the non-NaN input as second operand. | |||
49011 | if (DAG.isKnownNeverNaN(Op1)) | |||
49012 | return DAG.getNode(MinMaxOp, DL, VT, Op0, Op1, N->getFlags()); | |||
49013 | if (DAG.isKnownNeverNaN(Op0)) | |||
49014 | return DAG.getNode(MinMaxOp, DL, VT, Op1, Op0, N->getFlags()); | |||
49015 | ||||
49016 | // If we have to respect NaN inputs, this takes at least 3 instructions. | |||
49017 | // Favor a library call when operating on a scalar and minimizing code size. | |||
49018 | if (!VT.isVector() && DAG.getMachineFunction().getFunction().hasMinSize()) | |||
49019 | return SDValue(); | |||
49020 | ||||
49021 | EVT SetCCType = TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), | |||
49022 | VT); | |||
49023 | ||||
49024 | // There are 4 possibilities involving NaN inputs, and these are the required | |||
49025 | // outputs: | |||
49026 | // Op1 | |||
49027 | // Num NaN | |||
49028 | // ---------------- | |||
49029 | // Num | Max | Op0 | | |||
49030 | // Op0 ---------------- | |||
49031 | // NaN | Op1 | NaN | | |||
49032 | // ---------------- | |||
49033 | // | |||
49034 | // The SSE FP max/min instructions were not designed for this case, but rather | |||
49035 | // to implement: | |||
49036 | // Min = Op1 < Op0 ? Op1 : Op0 | |||
49037 | // Max = Op1 > Op0 ? Op1 : Op0 | |||
49038 | // | |||
49039 | // So they always return Op0 if either input is a NaN. However, we can still | |||
49040 | // use those instructions for fmaxnum by selecting away a NaN input. | |||
49041 | ||||
49042 | // If either operand is NaN, the 2nd source operand (Op0) is passed through. | |||
49043 | SDValue MinOrMax = DAG.getNode(MinMaxOp, DL, VT, Op1, Op0); | |||
49044 | SDValue IsOp0Nan = DAG.getSetCC(DL, SetCCType, Op0, Op0, ISD::SETUO); | |||
49045 | ||||
49046 | // If Op0 is a NaN, select Op1. Otherwise, select the max. If both operands | |||
49047 | // are NaN, the NaN value of Op1 is the result. | |||
49048 | return DAG.getSelect(DL, VT, IsOp0Nan, Op1, MinOrMax); | |||
49049 | } | |||
49050 | ||||
49051 | static SDValue combineX86INT_TO_FP(SDNode *N, SelectionDAG &DAG, | |||
49052 | TargetLowering::DAGCombinerInfo &DCI) { | |||
49053 | EVT VT = N->getValueType(0); | |||
49054 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
49055 | ||||
49056 | APInt KnownUndef, KnownZero; | |||
49057 | APInt DemandedElts = APInt::getAllOnes(VT.getVectorNumElements()); | |||
49058 | if (TLI.SimplifyDemandedVectorElts(SDValue(N, 0), DemandedElts, KnownUndef, | |||
49059 | KnownZero, DCI)) | |||
49060 | return SDValue(N, 0); | |||
49061 | ||||
49062 | // Convert a full vector load into vzload when not all bits are needed. | |||
49063 | SDValue In = N->getOperand(0); | |||
49064 | MVT InVT = In.getSimpleValueType(); | |||
49065 | if (VT.getVectorNumElements() < InVT.getVectorNumElements() && | |||
49066 | ISD::isNormalLoad(In.getNode()) && In.hasOneUse()) { | |||
49067 | assert(InVT.is128BitVector() && "Expected 128-bit input vector")(static_cast <bool> (InVT.is128BitVector() && "Expected 128-bit input vector" ) ? void (0) : __assert_fail ("InVT.is128BitVector() && \"Expected 128-bit input vector\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 49067, __extension__ __PRETTY_FUNCTION__)); | |||
49068 | LoadSDNode *LN = cast<LoadSDNode>(N->getOperand(0)); | |||
49069 | unsigned NumBits = InVT.getScalarSizeInBits() * VT.getVectorNumElements(); | |||
49070 | MVT MemVT = MVT::getIntegerVT(NumBits); | |||
49071 | MVT LoadVT = MVT::getVectorVT(MemVT, 128 / NumBits); | |||
49072 | if (SDValue VZLoad = narrowLoadToVZLoad(LN, MemVT, LoadVT, DAG)) { | |||
49073 | SDLoc dl(N); | |||
49074 | SDValue Convert = DAG.getNode(N->getOpcode(), dl, VT, | |||
49075 | DAG.getBitcast(InVT, VZLoad)); | |||
49076 | DCI.CombineTo(N, Convert); | |||
49077 | DAG.ReplaceAllUsesOfValueWith(SDValue(LN, 1), VZLoad.getValue(1)); | |||
49078 | DCI.recursivelyDeleteUnusedNodes(LN); | |||
49079 | return SDValue(N, 0); | |||
49080 | } | |||
49081 | } | |||
49082 | ||||
49083 | return SDValue(); | |||
49084 | } | |||
49085 | ||||
49086 | static SDValue combineCVTP2I_CVTTP2I(SDNode *N, SelectionDAG &DAG, | |||
49087 | TargetLowering::DAGCombinerInfo &DCI) { | |||
49088 | bool IsStrict = N->isTargetStrictFPOpcode(); | |||
49089 | EVT VT = N->getValueType(0); | |||
49090 | ||||
49091 | // Convert a full vector load into vzload when not all bits are needed. | |||
49092 | SDValue In = N->getOperand(IsStrict ? 1 : 0); | |||
49093 | MVT InVT = In.getSimpleValueType(); | |||
49094 | if (VT.getVectorNumElements() < InVT.getVectorNumElements() && | |||
49095 | ISD::isNormalLoad(In.getNode()) && In.hasOneUse()) { | |||
49096 | assert(InVT.is128BitVector() && "Expected 128-bit input vector")(static_cast <bool> (InVT.is128BitVector() && "Expected 128-bit input vector" ) ? void (0) : __assert_fail ("InVT.is128BitVector() && \"Expected 128-bit input vector\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 49096, __extension__ __PRETTY_FUNCTION__)); | |||
49097 | LoadSDNode *LN = cast<LoadSDNode>(In); | |||
49098 | unsigned NumBits = InVT.getScalarSizeInBits() * VT.getVectorNumElements(); | |||
49099 | MVT MemVT = MVT::getFloatingPointVT(NumBits); | |||
49100 | MVT LoadVT = MVT::getVectorVT(MemVT, 128 / NumBits); | |||
49101 | if (SDValue VZLoad = narrowLoadToVZLoad(LN, MemVT, LoadVT, DAG)) { | |||
49102 | SDLoc dl(N); | |||
49103 | if (IsStrict) { | |||
49104 | SDValue Convert = | |||
49105 | DAG.getNode(N->getOpcode(), dl, {VT, MVT::Other}, | |||
49106 | {N->getOperand(0), DAG.getBitcast(InVT, VZLoad)}); | |||
49107 | DCI.CombineTo(N, Convert, Convert.getValue(1)); | |||
49108 | } else { | |||
49109 | SDValue Convert = | |||
49110 | DAG.getNode(N->getOpcode(), dl, VT, DAG.getBitcast(InVT, VZLoad)); | |||
49111 | DCI.CombineTo(N, Convert); | |||
49112 | } | |||
49113 | DAG.ReplaceAllUsesOfValueWith(SDValue(LN, 1), VZLoad.getValue(1)); | |||
49114 | DCI.recursivelyDeleteUnusedNodes(LN); | |||
49115 | return SDValue(N, 0); | |||
49116 | } | |||
49117 | } | |||
49118 | ||||
49119 | return SDValue(); | |||
49120 | } | |||
49121 | ||||
49122 | /// Do target-specific dag combines on X86ISD::ANDNP nodes. | |||
49123 | static SDValue combineAndnp(SDNode *N, SelectionDAG &DAG, | |||
49124 | TargetLowering::DAGCombinerInfo &DCI, | |||
49125 | const X86Subtarget &Subtarget) { | |||
49126 | MVT VT = N->getSimpleValueType(0); | |||
49127 | ||||
49128 | // ANDNP(0, x) -> x | |||
49129 | if (ISD::isBuildVectorAllZeros(N->getOperand(0).getNode())) | |||
49130 | return N->getOperand(1); | |||
49131 | ||||
49132 | // ANDNP(x, 0) -> 0 | |||
49133 | if (ISD::isBuildVectorAllZeros(N->getOperand(1).getNode())) | |||
49134 | return DAG.getConstant(0, SDLoc(N), VT); | |||
49135 | ||||
49136 | // Turn ANDNP back to AND if input is inverted. | |||
49137 | if (SDValue Not = IsNOT(N->getOperand(0), DAG)) | |||
49138 | return DAG.getNode(ISD::AND, SDLoc(N), VT, DAG.getBitcast(VT, Not), | |||
49139 | N->getOperand(1)); | |||
49140 | ||||
49141 | // Attempt to recursively combine a bitmask ANDNP with shuffles. | |||
49142 | if (VT.isVector() && (VT.getScalarSizeInBits() % 8) == 0) { | |||
49143 | SDValue Op(N, 0); | |||
49144 | if (SDValue Res = combineX86ShufflesRecursively(Op, DAG, Subtarget)) | |||
49145 | return Res; | |||
49146 | } | |||
49147 | ||||
49148 | return SDValue(); | |||
49149 | } | |||
49150 | ||||
49151 | static SDValue combineBT(SDNode *N, SelectionDAG &DAG, | |||
49152 | TargetLowering::DAGCombinerInfo &DCI) { | |||
49153 | SDValue N1 = N->getOperand(1); | |||
49154 | ||||
49155 | // BT ignores high bits in the bit index operand. | |||
49156 | unsigned BitWidth = N1.getValueSizeInBits(); | |||
49157 | APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth)); | |||
49158 | if (DAG.getTargetLoweringInfo().SimplifyDemandedBits(N1, DemandedMask, DCI)) { | |||
49159 | if (N->getOpcode() != ISD::DELETED_NODE) | |||
49160 | DCI.AddToWorklist(N); | |||
49161 | return SDValue(N, 0); | |||
49162 | } | |||
49163 | ||||
49164 | return SDValue(); | |||
49165 | } | |||
49166 | ||||
49167 | static SDValue combineCVTPH2PS(SDNode *N, SelectionDAG &DAG, | |||
49168 | TargetLowering::DAGCombinerInfo &DCI) { | |||
49169 | bool IsStrict = N->getOpcode() == X86ISD::STRICT_CVTPH2PS; | |||
49170 | SDValue Src = N->getOperand(IsStrict ? 1 : 0); | |||
49171 | ||||
49172 | if (N->getValueType(0) == MVT::v4f32 && Src.getValueType() == MVT::v8i16) { | |||
49173 | APInt KnownUndef, KnownZero; | |||
49174 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
49175 | APInt DemandedElts = APInt::getLowBitsSet(8, 4); | |||
49176 | if (TLI.SimplifyDemandedVectorElts(Src, DemandedElts, KnownUndef, KnownZero, | |||
49177 | DCI)) { | |||
49178 | if (N->getOpcode() != ISD::DELETED_NODE) | |||
49179 | DCI.AddToWorklist(N); | |||
49180 | return SDValue(N, 0); | |||
49181 | } | |||
49182 | ||||
49183 | // Convert a full vector load into vzload when not all bits are needed. | |||
49184 | if (ISD::isNormalLoad(Src.getNode()) && Src.hasOneUse()) { | |||
49185 | LoadSDNode *LN = cast<LoadSDNode>(N->getOperand(IsStrict ? 1 : 0)); | |||
49186 | if (SDValue VZLoad = narrowLoadToVZLoad(LN, MVT::i64, MVT::v2i64, DAG)) { | |||
49187 | SDLoc dl(N); | |||
49188 | if (IsStrict) { | |||
49189 | SDValue Convert = DAG.getNode( | |||
49190 | N->getOpcode(), dl, {MVT::v4f32, MVT::Other}, | |||
49191 | {N->getOperand(0), DAG.getBitcast(MVT::v8i16, VZLoad)}); | |||
49192 | DCI.CombineTo(N, Convert, Convert.getValue(1)); | |||
49193 | } else { | |||
49194 | SDValue Convert = DAG.getNode(N->getOpcode(), dl, MVT::v4f32, | |||
49195 | DAG.getBitcast(MVT::v8i16, VZLoad)); | |||
49196 | DCI.CombineTo(N, Convert); | |||
49197 | } | |||
49198 | ||||
49199 | DAG.ReplaceAllUsesOfValueWith(SDValue(LN, 1), VZLoad.getValue(1)); | |||
49200 | DCI.recursivelyDeleteUnusedNodes(LN); | |||
49201 | return SDValue(N, 0); | |||
49202 | } | |||
49203 | } | |||
49204 | } | |||
49205 | ||||
49206 | return SDValue(); | |||
49207 | } | |||
49208 | ||||
49209 | // Try to combine sext_in_reg of a cmov of constants by extending the constants. | |||
49210 | static SDValue combineSextInRegCmov(SDNode *N, SelectionDAG &DAG) { | |||
49211 | assert(N->getOpcode() == ISD::SIGN_EXTEND_INREG)(static_cast <bool> (N->getOpcode() == ISD::SIGN_EXTEND_INREG ) ? void (0) : __assert_fail ("N->getOpcode() == ISD::SIGN_EXTEND_INREG" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 49211, __extension__ __PRETTY_FUNCTION__)); | |||
49212 | ||||
49213 | EVT DstVT = N->getValueType(0); | |||
49214 | ||||
49215 | SDValue N0 = N->getOperand(0); | |||
49216 | SDValue N1 = N->getOperand(1); | |||
49217 | EVT ExtraVT = cast<VTSDNode>(N1)->getVT(); | |||
49218 | ||||
49219 | if (ExtraVT != MVT::i8 && ExtraVT != MVT::i16) | |||
49220 | return SDValue(); | |||
49221 | ||||
49222 | // Look through single use any_extends / truncs. | |||
49223 | SDValue IntermediateBitwidthOp; | |||
49224 | if ((N0.getOpcode() == ISD::ANY_EXTEND || N0.getOpcode() == ISD::TRUNCATE) && | |||
49225 | N0.hasOneUse()) { | |||
49226 | IntermediateBitwidthOp = N0; | |||
49227 | N0 = N0.getOperand(0); | |||
49228 | } | |||
49229 | ||||
49230 | // See if we have a single use cmov. | |||
49231 | if (N0.getOpcode() != X86ISD::CMOV || !N0.hasOneUse()) | |||
49232 | return SDValue(); | |||
49233 | ||||
49234 | SDValue CMovOp0 = N0.getOperand(0); | |||
49235 | SDValue CMovOp1 = N0.getOperand(1); | |||
49236 | ||||
49237 | // Make sure both operands are constants. | |||
49238 | if (!isa<ConstantSDNode>(CMovOp0.getNode()) || | |||
49239 | !isa<ConstantSDNode>(CMovOp1.getNode())) | |||
49240 | return SDValue(); | |||
49241 | ||||
49242 | SDLoc DL(N); | |||
49243 | ||||
49244 | // If we looked through an any_extend/trunc above, add one to the constants. | |||
49245 | if (IntermediateBitwidthOp) { | |||
49246 | unsigned IntermediateOpc = IntermediateBitwidthOp.getOpcode(); | |||
49247 | CMovOp0 = DAG.getNode(IntermediateOpc, DL, DstVT, CMovOp0); | |||
49248 | CMovOp1 = DAG.getNode(IntermediateOpc, DL, DstVT, CMovOp1); | |||
49249 | } | |||
49250 | ||||
49251 | CMovOp0 = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, DstVT, CMovOp0, N1); | |||
49252 | CMovOp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, DstVT, CMovOp1, N1); | |||
49253 | ||||
49254 | EVT CMovVT = DstVT; | |||
49255 | // We do not want i16 CMOV's. Promote to i32 and truncate afterwards. | |||
49256 | if (DstVT == MVT::i16) { | |||
49257 | CMovVT = MVT::i32; | |||
49258 | CMovOp0 = DAG.getNode(ISD::ZERO_EXTEND, DL, CMovVT, CMovOp0); | |||
49259 | CMovOp1 = DAG.getNode(ISD::ZERO_EXTEND, DL, CMovVT, CMovOp1); | |||
49260 | } | |||
49261 | ||||
49262 | SDValue CMov = DAG.getNode(X86ISD::CMOV, DL, CMovVT, CMovOp0, CMovOp1, | |||
49263 | N0.getOperand(2), N0.getOperand(3)); | |||
49264 | ||||
49265 | if (CMovVT != DstVT) | |||
49266 | CMov = DAG.getNode(ISD::TRUNCATE, DL, DstVT, CMov); | |||
49267 | ||||
49268 | return CMov; | |||
49269 | } | |||
49270 | ||||
49271 | static SDValue combineSignExtendInReg(SDNode *N, SelectionDAG &DAG, | |||
49272 | const X86Subtarget &Subtarget) { | |||
49273 | assert(N->getOpcode() == ISD::SIGN_EXTEND_INREG)(static_cast <bool> (N->getOpcode() == ISD::SIGN_EXTEND_INREG ) ? void (0) : __assert_fail ("N->getOpcode() == ISD::SIGN_EXTEND_INREG" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 49273, __extension__ __PRETTY_FUNCTION__)); | |||
49274 | ||||
49275 | if (SDValue V = combineSextInRegCmov(N, DAG)) | |||
49276 | return V; | |||
49277 | ||||
49278 | EVT VT = N->getValueType(0); | |||
49279 | SDValue N0 = N->getOperand(0); | |||
49280 | SDValue N1 = N->getOperand(1); | |||
49281 | EVT ExtraVT = cast<VTSDNode>(N1)->getVT(); | |||
49282 | SDLoc dl(N); | |||
49283 | ||||
49284 | // The SIGN_EXTEND_INREG to v4i64 is expensive operation on the | |||
49285 | // both SSE and AVX2 since there is no sign-extended shift right | |||
49286 | // operation on a vector with 64-bit elements. | |||
49287 | //(sext_in_reg (v4i64 anyext (v4i32 x )), ExtraVT) -> | |||
49288 | // (v4i64 sext (v4i32 sext_in_reg (v4i32 x , ExtraVT))) | |||
49289 | if (VT == MVT::v4i64 && (N0.getOpcode() == ISD::ANY_EXTEND || | |||
49290 | N0.getOpcode() == ISD::SIGN_EXTEND)) { | |||
49291 | SDValue N00 = N0.getOperand(0); | |||
49292 | ||||
49293 | // EXTLOAD has a better solution on AVX2, | |||
49294 | // it may be replaced with X86ISD::VSEXT node. | |||
49295 | if (N00.getOpcode() == ISD::LOAD && Subtarget.hasInt256()) | |||
49296 | if (!ISD::isNormalLoad(N00.getNode())) | |||
49297 | return SDValue(); | |||
49298 | ||||
49299 | // Attempt to promote any comparison mask ops before moving the | |||
49300 | // SIGN_EXTEND_INREG in the way. | |||
49301 | if (SDValue Promote = PromoteMaskArithmetic(N0.getNode(), DAG, Subtarget)) | |||
49302 | return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, VT, Promote, N1); | |||
49303 | ||||
49304 | if (N00.getValueType() == MVT::v4i32 && ExtraVT.getSizeInBits() < 128) { | |||
49305 | SDValue Tmp = | |||
49306 | DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v4i32, N00, N1); | |||
49307 | return DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i64, Tmp); | |||
49308 | } | |||
49309 | } | |||
49310 | return SDValue(); | |||
49311 | } | |||
49312 | ||||
49313 | /// sext(add_nsw(x, C)) --> add(sext(x), C_sext) | |||
49314 | /// zext(add_nuw(x, C)) --> add(zext(x), C_zext) | |||
49315 | /// Promoting a sign/zero extension ahead of a no overflow 'add' exposes | |||
49316 | /// opportunities to combine math ops, use an LEA, or use a complex addressing | |||
49317 | /// mode. This can eliminate extend, add, and shift instructions. | |||
49318 | static SDValue promoteExtBeforeAdd(SDNode *Ext, SelectionDAG &DAG, | |||
49319 | const X86Subtarget &Subtarget) { | |||
49320 | if (Ext->getOpcode() != ISD::SIGN_EXTEND && | |||
49321 | Ext->getOpcode() != ISD::ZERO_EXTEND) | |||
49322 | return SDValue(); | |||
49323 | ||||
49324 | // TODO: This should be valid for other integer types. | |||
49325 | EVT VT = Ext->getValueType(0); | |||
49326 | if (VT != MVT::i64) | |||
49327 | return SDValue(); | |||
49328 | ||||
49329 | SDValue Add = Ext->getOperand(0); | |||
49330 | if (Add.getOpcode() != ISD::ADD) | |||
49331 | return SDValue(); | |||
49332 | ||||
49333 | bool Sext = Ext->getOpcode() == ISD::SIGN_EXTEND; | |||
49334 | bool NSW = Add->getFlags().hasNoSignedWrap(); | |||
49335 | bool NUW = Add->getFlags().hasNoUnsignedWrap(); | |||
49336 | ||||
49337 | // We need an 'add nsw' feeding into the 'sext' or 'add nuw' feeding | |||
49338 | // into the 'zext' | |||
49339 | if ((Sext && !NSW) || (!Sext && !NUW)) | |||
49340 | return SDValue(); | |||
49341 | ||||
49342 | // Having a constant operand to the 'add' ensures that we are not increasing | |||
49343 | // the instruction count because the constant is extended for free below. | |||
49344 | // A constant operand can also become the displacement field of an LEA. | |||
49345 | auto *AddOp1 = dyn_cast<ConstantSDNode>(Add.getOperand(1)); | |||
49346 | if (!AddOp1) | |||
49347 | return SDValue(); | |||
49348 | ||||
49349 | // Don't make the 'add' bigger if there's no hope of combining it with some | |||
49350 | // other 'add' or 'shl' instruction. | |||
49351 | // TODO: It may be profitable to generate simpler LEA instructions in place | |||
49352 | // of single 'add' instructions, but the cost model for selecting an LEA | |||
49353 | // currently has a high threshold. | |||
49354 | bool HasLEAPotential = false; | |||
49355 | for (auto *User : Ext->uses()) { | |||
49356 | if (User->getOpcode() == ISD::ADD || User->getOpcode() == ISD::SHL) { | |||
49357 | HasLEAPotential = true; | |||
49358 | break; | |||
49359 | } | |||
49360 | } | |||
49361 | if (!HasLEAPotential) | |||
49362 | return SDValue(); | |||
49363 | ||||
49364 | // Everything looks good, so pull the '{s|z}ext' ahead of the 'add'. | |||
49365 | int64_t AddConstant = Sext ? AddOp1->getSExtValue() : AddOp1->getZExtValue(); | |||
49366 | SDValue AddOp0 = Add.getOperand(0); | |||
49367 | SDValue NewExt = DAG.getNode(Ext->getOpcode(), SDLoc(Ext), VT, AddOp0); | |||
49368 | SDValue NewConstant = DAG.getConstant(AddConstant, SDLoc(Add), VT); | |||
49369 | ||||
49370 | // The wider add is guaranteed to not wrap because both operands are | |||
49371 | // sign-extended. | |||
49372 | SDNodeFlags Flags; | |||
49373 | Flags.setNoSignedWrap(NSW); | |||
49374 | Flags.setNoUnsignedWrap(NUW); | |||
49375 | return DAG.getNode(ISD::ADD, SDLoc(Add), VT, NewExt, NewConstant, Flags); | |||
49376 | } | |||
49377 | ||||
49378 | // If we face {ANY,SIGN,ZERO}_EXTEND that is applied to a CMOV with constant | |||
49379 | // operands and the result of CMOV is not used anywhere else - promote CMOV | |||
49380 | // itself instead of promoting its result. This could be beneficial, because: | |||
49381 | // 1) X86TargetLowering::EmitLoweredSelect later can do merging of two | |||
49382 | // (or more) pseudo-CMOVs only when they go one-after-another and | |||
49383 | // getting rid of result extension code after CMOV will help that. | |||
49384 | // 2) Promotion of constant CMOV arguments is free, hence the | |||
49385 | // {ANY,SIGN,ZERO}_EXTEND will just be deleted. | |||
49386 | // 3) 16-bit CMOV encoding is 4 bytes, 32-bit CMOV is 3-byte, so this | |||
49387 | // promotion is also good in terms of code-size. | |||
49388 | // (64-bit CMOV is 4-bytes, that's why we don't do 32-bit => 64-bit | |||
49389 | // promotion). | |||
49390 | static SDValue combineToExtendCMOV(SDNode *Extend, SelectionDAG &DAG) { | |||
49391 | SDValue CMovN = Extend->getOperand(0); | |||
49392 | if (CMovN.getOpcode() != X86ISD::CMOV || !CMovN.hasOneUse()) | |||
49393 | return SDValue(); | |||
49394 | ||||
49395 | EVT TargetVT = Extend->getValueType(0); | |||
49396 | unsigned ExtendOpcode = Extend->getOpcode(); | |||
49397 | SDLoc DL(Extend); | |||
49398 | ||||
49399 | EVT VT = CMovN.getValueType(); | |||
49400 | SDValue CMovOp0 = CMovN.getOperand(0); | |||
49401 | SDValue CMovOp1 = CMovN.getOperand(1); | |||
49402 | ||||
49403 | if (!isa<ConstantSDNode>(CMovOp0.getNode()) || | |||
49404 | !isa<ConstantSDNode>(CMovOp1.getNode())) | |||
49405 | return SDValue(); | |||
49406 | ||||
49407 | // Only extend to i32 or i64. | |||
49408 | if (TargetVT != MVT::i32 && TargetVT != MVT::i64) | |||
49409 | return SDValue(); | |||
49410 | ||||
49411 | // Only extend from i16 unless its a sign_extend from i32. Zext/aext from i32 | |||
49412 | // are free. | |||
49413 | if (VT != MVT::i16 && !(ExtendOpcode == ISD::SIGN_EXTEND && VT == MVT::i32)) | |||
49414 | return SDValue(); | |||
49415 | ||||
49416 | // If this a zero extend to i64, we should only extend to i32 and use a free | |||
49417 | // zero extend to finish. | |||
49418 | EVT ExtendVT = TargetVT; | |||
49419 | if (TargetVT == MVT::i64 && ExtendOpcode != ISD::SIGN_EXTEND) | |||
49420 | ExtendVT = MVT::i32; | |||
49421 | ||||
49422 | CMovOp0 = DAG.getNode(ExtendOpcode, DL, ExtendVT, CMovOp0); | |||
49423 | CMovOp1 = DAG.getNode(ExtendOpcode, DL, ExtendVT, CMovOp1); | |||
49424 | ||||
49425 | SDValue Res = DAG.getNode(X86ISD::CMOV, DL, ExtendVT, CMovOp0, CMovOp1, | |||
49426 | CMovN.getOperand(2), CMovN.getOperand(3)); | |||
49427 | ||||
49428 | // Finish extending if needed. | |||
49429 | if (ExtendVT != TargetVT) | |||
49430 | Res = DAG.getNode(ExtendOpcode, DL, TargetVT, Res); | |||
49431 | ||||
49432 | return Res; | |||
49433 | } | |||
49434 | ||||
49435 | // Convert (vXiY *ext(vXi1 bitcast(iX))) to extend_in_reg(broadcast(iX)). | |||
49436 | // This is more or less the reverse of combineBitcastvxi1. | |||
49437 | static SDValue | |||
49438 | combineToExtendBoolVectorInReg(SDNode *N, SelectionDAG &DAG, | |||
49439 | TargetLowering::DAGCombinerInfo &DCI, | |||
49440 | const X86Subtarget &Subtarget) { | |||
49441 | unsigned Opcode = N->getOpcode(); | |||
49442 | if (Opcode != ISD::SIGN_EXTEND && Opcode != ISD::ZERO_EXTEND && | |||
49443 | Opcode != ISD::ANY_EXTEND) | |||
49444 | return SDValue(); | |||
49445 | if (!DCI.isBeforeLegalizeOps()) | |||
49446 | return SDValue(); | |||
49447 | if (!Subtarget.hasSSE2() || Subtarget.hasAVX512()) | |||
49448 | return SDValue(); | |||
49449 | ||||
49450 | SDValue N0 = N->getOperand(0); | |||
49451 | EVT VT = N->getValueType(0); | |||
49452 | EVT SVT = VT.getScalarType(); | |||
49453 | EVT InSVT = N0.getValueType().getScalarType(); | |||
49454 | unsigned EltSizeInBits = SVT.getSizeInBits(); | |||
49455 | ||||
49456 | // Input type must be extending a bool vector (bit-casted from a scalar | |||
49457 | // integer) to legal integer types. | |||
49458 | if (!VT.isVector()) | |||
49459 | return SDValue(); | |||
49460 | if (SVT != MVT::i64 && SVT != MVT::i32 && SVT != MVT::i16 && SVT != MVT::i8) | |||
49461 | return SDValue(); | |||
49462 | if (InSVT != MVT::i1 || N0.getOpcode() != ISD::BITCAST) | |||
49463 | return SDValue(); | |||
49464 | ||||
49465 | SDValue N00 = N0.getOperand(0); | |||
49466 | EVT SclVT = N0.getOperand(0).getValueType(); | |||
49467 | if (!SclVT.isScalarInteger()) | |||
49468 | return SDValue(); | |||
49469 | ||||
49470 | SDLoc DL(N); | |||
49471 | SDValue Vec; | |||
49472 | SmallVector<int, 32> ShuffleMask; | |||
49473 | unsigned NumElts = VT.getVectorNumElements(); | |||
49474 | assert(NumElts == SclVT.getSizeInBits() && "Unexpected bool vector size")(static_cast <bool> (NumElts == SclVT.getSizeInBits() && "Unexpected bool vector size") ? void (0) : __assert_fail ("NumElts == SclVT.getSizeInBits() && \"Unexpected bool vector size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 49474, __extension__ __PRETTY_FUNCTION__)); | |||
49475 | ||||
49476 | // Broadcast the scalar integer to the vector elements. | |||
49477 | if (NumElts > EltSizeInBits) { | |||
49478 | // If the scalar integer is greater than the vector element size, then we | |||
49479 | // must split it down into sub-sections for broadcasting. For example: | |||
49480 | // i16 -> v16i8 (i16 -> v8i16 -> v16i8) with 2 sub-sections. | |||
49481 | // i32 -> v32i8 (i32 -> v8i32 -> v32i8) with 4 sub-sections. | |||
49482 | assert((NumElts % EltSizeInBits) == 0 && "Unexpected integer scale")(static_cast <bool> ((NumElts % EltSizeInBits) == 0 && "Unexpected integer scale") ? void (0) : __assert_fail ("(NumElts % EltSizeInBits) == 0 && \"Unexpected integer scale\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 49482, __extension__ __PRETTY_FUNCTION__)); | |||
49483 | unsigned Scale = NumElts / EltSizeInBits; | |||
49484 | EVT BroadcastVT = | |||
49485 | EVT::getVectorVT(*DAG.getContext(), SclVT, EltSizeInBits); | |||
49486 | Vec = DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, BroadcastVT, N00); | |||
49487 | Vec = DAG.getBitcast(VT, Vec); | |||
49488 | ||||
49489 | for (unsigned i = 0; i != Scale; ++i) | |||
49490 | ShuffleMask.append(EltSizeInBits, i); | |||
49491 | Vec = DAG.getVectorShuffle(VT, DL, Vec, Vec, ShuffleMask); | |||
49492 | } else if (Subtarget.hasAVX2() && NumElts < EltSizeInBits && | |||
49493 | (SclVT == MVT::i8 || SclVT == MVT::i16 || SclVT == MVT::i32)) { | |||
49494 | // If we have register broadcast instructions, use the scalar size as the | |||
49495 | // element type for the shuffle. Then cast to the wider element type. The | |||
49496 | // widened bits won't be used, and this might allow the use of a broadcast | |||
49497 | // load. | |||
49498 | assert((EltSizeInBits % NumElts) == 0 && "Unexpected integer scale")(static_cast <bool> ((EltSizeInBits % NumElts) == 0 && "Unexpected integer scale") ? void (0) : __assert_fail ("(EltSizeInBits % NumElts) == 0 && \"Unexpected integer scale\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 49498, __extension__ __PRETTY_FUNCTION__)); | |||
49499 | unsigned Scale = EltSizeInBits / NumElts; | |||
49500 | EVT BroadcastVT = | |||
49501 | EVT::getVectorVT(*DAG.getContext(), SclVT, NumElts * Scale); | |||
49502 | Vec = DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, BroadcastVT, N00); | |||
49503 | ShuffleMask.append(NumElts * Scale, 0); | |||
49504 | Vec = DAG.getVectorShuffle(BroadcastVT, DL, Vec, Vec, ShuffleMask); | |||
49505 | Vec = DAG.getBitcast(VT, Vec); | |||
49506 | } else { | |||
49507 | // For smaller scalar integers, we can simply any-extend it to the vector | |||
49508 | // element size (we don't care about the upper bits) and broadcast it to all | |||
49509 | // elements. | |||
49510 | SDValue Scl = DAG.getAnyExtOrTrunc(N00, DL, SVT); | |||
49511 | Vec = DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, Scl); | |||
49512 | ShuffleMask.append(NumElts, 0); | |||
49513 | Vec = DAG.getVectorShuffle(VT, DL, Vec, Vec, ShuffleMask); | |||
49514 | } | |||
49515 | ||||
49516 | // Now, mask the relevant bit in each element. | |||
49517 | SmallVector<SDValue, 32> Bits; | |||
49518 | for (unsigned i = 0; i != NumElts; ++i) { | |||
49519 | int BitIdx = (i % EltSizeInBits); | |||
49520 | APInt Bit = APInt::getBitsSet(EltSizeInBits, BitIdx, BitIdx + 1); | |||
49521 | Bits.push_back(DAG.getConstant(Bit, DL, SVT)); | |||
49522 | } | |||
49523 | SDValue BitMask = DAG.getBuildVector(VT, DL, Bits); | |||
49524 | Vec = DAG.getNode(ISD::AND, DL, VT, Vec, BitMask); | |||
49525 | ||||
49526 | // Compare against the bitmask and extend the result. | |||
49527 | EVT CCVT = VT.changeVectorElementType(MVT::i1); | |||
49528 | Vec = DAG.getSetCC(DL, CCVT, Vec, BitMask, ISD::SETEQ); | |||
49529 | Vec = DAG.getSExtOrTrunc(Vec, DL, VT); | |||
49530 | ||||
49531 | // For SEXT, this is now done, otherwise shift the result down for | |||
49532 | // zero-extension. | |||
49533 | if (Opcode == ISD::SIGN_EXTEND) | |||
49534 | return Vec; | |||
49535 | return DAG.getNode(ISD::SRL, DL, VT, Vec, | |||
49536 | DAG.getConstant(EltSizeInBits - 1, DL, VT)); | |||
49537 | } | |||
49538 | ||||
49539 | // Attempt to combine a (sext/zext (setcc)) to a setcc with a xmm/ymm/zmm | |||
49540 | // result type. | |||
49541 | static SDValue combineExtSetcc(SDNode *N, SelectionDAG &DAG, | |||
49542 | const X86Subtarget &Subtarget) { | |||
49543 | SDValue N0 = N->getOperand(0); | |||
49544 | EVT VT = N->getValueType(0); | |||
49545 | SDLoc dl(N); | |||
49546 | ||||
49547 | // Only do this combine with AVX512 for vector extends. | |||
49548 | if (!Subtarget.hasAVX512() || !VT.isVector() || N0.getOpcode() != ISD::SETCC) | |||
49549 | return SDValue(); | |||
49550 | ||||
49551 | // Only combine legal element types. | |||
49552 | EVT SVT = VT.getVectorElementType(); | |||
49553 | if (SVT != MVT::i8 && SVT != MVT::i16 && SVT != MVT::i32 && | |||
49554 | SVT != MVT::i64 && SVT != MVT::f32 && SVT != MVT::f64) | |||
49555 | return SDValue(); | |||
49556 | ||||
49557 | // We don't have CMPP Instruction for vxf16 | |||
49558 | if (N0.getOperand(0).getValueType().getVectorElementType() == MVT::f16) | |||
49559 | return SDValue(); | |||
49560 | // We can only do this if the vector size in 256 bits or less. | |||
49561 | unsigned Size = VT.getSizeInBits(); | |||
49562 | if (Size > 256 && Subtarget.useAVX512Regs()) | |||
49563 | return SDValue(); | |||
49564 | ||||
49565 | // Don't fold if the condition code can't be handled by PCMPEQ/PCMPGT since | |||
49566 | // that's the only integer compares with we have. | |||
49567 | ISD::CondCode CC = cast<CondCodeSDNode>(N0.getOperand(2))->get(); | |||
49568 | if (ISD::isUnsignedIntSetCC(CC)) | |||
49569 | return SDValue(); | |||
49570 | ||||
49571 | // Only do this combine if the extension will be fully consumed by the setcc. | |||
49572 | EVT N00VT = N0.getOperand(0).getValueType(); | |||
49573 | EVT MatchingVecType = N00VT.changeVectorElementTypeToInteger(); | |||
49574 | if (Size != MatchingVecType.getSizeInBits()) | |||
49575 | return SDValue(); | |||
49576 | ||||
49577 | SDValue Res = DAG.getSetCC(dl, VT, N0.getOperand(0), N0.getOperand(1), CC); | |||
49578 | ||||
49579 | if (N->getOpcode() == ISD::ZERO_EXTEND) | |||
49580 | Res = DAG.getZeroExtendInReg(Res, dl, N0.getValueType()); | |||
49581 | ||||
49582 | return Res; | |||
49583 | } | |||
49584 | ||||
49585 | static SDValue combineSext(SDNode *N, SelectionDAG &DAG, | |||
49586 | TargetLowering::DAGCombinerInfo &DCI, | |||
49587 | const X86Subtarget &Subtarget) { | |||
49588 | SDValue N0 = N->getOperand(0); | |||
49589 | EVT VT = N->getValueType(0); | |||
49590 | SDLoc DL(N); | |||
49591 | ||||
49592 | // (i32 (sext (i8 (x86isd::setcc_carry)))) -> (i32 (x86isd::setcc_carry)) | |||
49593 | if (!DCI.isBeforeLegalizeOps() && | |||
49594 | N0.getOpcode() == X86ISD::SETCC_CARRY) { | |||
49595 | SDValue Setcc = DAG.getNode(X86ISD::SETCC_CARRY, DL, VT, N0->getOperand(0), | |||
49596 | N0->getOperand(1)); | |||
49597 | bool ReplaceOtherUses = !N0.hasOneUse(); | |||
49598 | DCI.CombineTo(N, Setcc); | |||
49599 | // Replace other uses with a truncate of the widened setcc_carry. | |||
49600 | if (ReplaceOtherUses) { | |||
49601 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), | |||
49602 | N0.getValueType(), Setcc); | |||
49603 | DCI.CombineTo(N0.getNode(), Trunc); | |||
49604 | } | |||
49605 | ||||
49606 | return SDValue(N, 0); | |||
49607 | } | |||
49608 | ||||
49609 | if (SDValue NewCMov = combineToExtendCMOV(N, DAG)) | |||
49610 | return NewCMov; | |||
49611 | ||||
49612 | if (!DCI.isBeforeLegalizeOps()) | |||
49613 | return SDValue(); | |||
49614 | ||||
49615 | if (SDValue V = combineExtSetcc(N, DAG, Subtarget)) | |||
49616 | return V; | |||
49617 | ||||
49618 | if (SDValue V = combineToExtendBoolVectorInReg(N, DAG, DCI, Subtarget)) | |||
49619 | return V; | |||
49620 | ||||
49621 | if (VT.isVector()) { | |||
49622 | if (SDValue R = PromoteMaskArithmetic(N, DAG, Subtarget)) | |||
49623 | return R; | |||
49624 | ||||
49625 | if (N0.getOpcode() == ISD::SIGN_EXTEND_VECTOR_INREG) | |||
49626 | return DAG.getNode(N0.getOpcode(), DL, VT, N0.getOperand(0)); | |||
49627 | } | |||
49628 | ||||
49629 | if (SDValue NewAdd = promoteExtBeforeAdd(N, DAG, Subtarget)) | |||
49630 | return NewAdd; | |||
49631 | ||||
49632 | return SDValue(); | |||
49633 | } | |||
49634 | ||||
49635 | static SDValue combineFMA(SDNode *N, SelectionDAG &DAG, | |||
49636 | TargetLowering::DAGCombinerInfo &DCI, | |||
49637 | const X86Subtarget &Subtarget) { | |||
49638 | SDLoc dl(N); | |||
49639 | EVT VT = N->getValueType(0); | |||
49640 | bool IsStrict = N->isStrictFPOpcode() || N->isTargetStrictFPOpcode(); | |||
49641 | ||||
49642 | // Let legalize expand this if it isn't a legal type yet. | |||
49643 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
49644 | if (!TLI.isTypeLegal(VT)) | |||
49645 | return SDValue(); | |||
49646 | ||||
49647 | SDValue A = N->getOperand(IsStrict ? 1 : 0); | |||
49648 | SDValue B = N->getOperand(IsStrict ? 2 : 1); | |||
49649 | SDValue C = N->getOperand(IsStrict ? 3 : 2); | |||
49650 | ||||
49651 | // If the operation allows fast-math and the target does not support FMA, | |||
49652 | // split this into mul+add to avoid libcall(s). | |||
49653 | SDNodeFlags Flags = N->getFlags(); | |||
49654 | if (!IsStrict && Flags.hasAllowReassociation() && | |||
49655 | TLI.isOperationExpand(ISD::FMA, VT)) { | |||
49656 | SDValue Fmul = DAG.getNode(ISD::FMUL, dl, VT, A, B, Flags); | |||
49657 | return DAG.getNode(ISD::FADD, dl, VT, Fmul, C, Flags); | |||
49658 | } | |||
49659 | ||||
49660 | EVT ScalarVT = VT.getScalarType(); | |||
49661 | if (((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) || | |||
49662 | !Subtarget.hasAnyFMA()) && | |||
49663 | !(ScalarVT == MVT::f16 && Subtarget.hasFP16())) | |||
49664 | return SDValue(); | |||
49665 | ||||
49666 | auto invertIfNegative = [&DAG, &TLI, &DCI](SDValue &V) { | |||
49667 | bool CodeSize = DAG.getMachineFunction().getFunction().hasOptSize(); | |||
49668 | bool LegalOperations = !DCI.isBeforeLegalizeOps(); | |||
49669 | if (SDValue NegV = TLI.getCheaperNegatedExpression(V, DAG, LegalOperations, | |||
49670 | CodeSize)) { | |||
49671 | V = NegV; | |||
49672 | return true; | |||
49673 | } | |||
49674 | // Look through extract_vector_elts. If it comes from an FNEG, create a | |||
49675 | // new extract from the FNEG input. | |||
49676 | if (V.getOpcode() == ISD::EXTRACT_VECTOR_ELT && | |||
49677 | isNullConstant(V.getOperand(1))) { | |||
49678 | SDValue Vec = V.getOperand(0); | |||
49679 | if (SDValue NegV = TLI.getCheaperNegatedExpression( | |||
49680 | Vec, DAG, LegalOperations, CodeSize)) { | |||
49681 | V = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(V), V.getValueType(), | |||
49682 | NegV, V.getOperand(1)); | |||
49683 | return true; | |||
49684 | } | |||
49685 | } | |||
49686 | ||||
49687 | return false; | |||
49688 | }; | |||
49689 | ||||
49690 | // Do not convert the passthru input of scalar intrinsics. | |||
49691 | // FIXME: We could allow negations of the lower element only. | |||
49692 | bool NegA = invertIfNegative(A); | |||
49693 | bool NegB = invertIfNegative(B); | |||
49694 | bool NegC = invertIfNegative(C); | |||
49695 | ||||
49696 | if (!NegA && !NegB && !NegC) | |||
49697 | return SDValue(); | |||
49698 | ||||
49699 | unsigned NewOpcode = | |||
49700 | negateFMAOpcode(N->getOpcode(), NegA != NegB, NegC, false); | |||
49701 | ||||
49702 | // Propagate fast-math-flags to new FMA node. | |||
49703 | SelectionDAG::FlagInserter FlagsInserter(DAG, Flags); | |||
49704 | if (IsStrict) { | |||
49705 | assert(N->getNumOperands() == 4 && "Shouldn't be greater than 4")(static_cast <bool> (N->getNumOperands() == 4 && "Shouldn't be greater than 4") ? void (0) : __assert_fail ("N->getNumOperands() == 4 && \"Shouldn't be greater than 4\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 49705, __extension__ __PRETTY_FUNCTION__)); | |||
49706 | return DAG.getNode(NewOpcode, dl, {VT, MVT::Other}, | |||
49707 | {N->getOperand(0), A, B, C}); | |||
49708 | } else { | |||
49709 | if (N->getNumOperands() == 4) | |||
49710 | return DAG.getNode(NewOpcode, dl, VT, A, B, C, N->getOperand(3)); | |||
49711 | return DAG.getNode(NewOpcode, dl, VT, A, B, C); | |||
49712 | } | |||
49713 | } | |||
49714 | ||||
49715 | // Combine FMADDSUB(A, B, FNEG(C)) -> FMSUBADD(A, B, C) | |||
49716 | // Combine FMSUBADD(A, B, FNEG(C)) -> FMADDSUB(A, B, C) | |||
49717 | static SDValue combineFMADDSUB(SDNode *N, SelectionDAG &DAG, | |||
49718 | TargetLowering::DAGCombinerInfo &DCI) { | |||
49719 | SDLoc dl(N); | |||
49720 | EVT VT = N->getValueType(0); | |||
49721 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
49722 | bool CodeSize = DAG.getMachineFunction().getFunction().hasOptSize(); | |||
49723 | bool LegalOperations = !DCI.isBeforeLegalizeOps(); | |||
49724 | ||||
49725 | SDValue N2 = N->getOperand(2); | |||
49726 | ||||
49727 | SDValue NegN2 = | |||
49728 | TLI.getCheaperNegatedExpression(N2, DAG, LegalOperations, CodeSize); | |||
49729 | if (!NegN2) | |||
49730 | return SDValue(); | |||
49731 | unsigned NewOpcode = negateFMAOpcode(N->getOpcode(), false, true, false); | |||
49732 | ||||
49733 | if (N->getNumOperands() == 4) | |||
49734 | return DAG.getNode(NewOpcode, dl, VT, N->getOperand(0), N->getOperand(1), | |||
49735 | NegN2, N->getOperand(3)); | |||
49736 | return DAG.getNode(NewOpcode, dl, VT, N->getOperand(0), N->getOperand(1), | |||
49737 | NegN2); | |||
49738 | } | |||
49739 | ||||
49740 | static SDValue combineZext(SDNode *N, SelectionDAG &DAG, | |||
49741 | TargetLowering::DAGCombinerInfo &DCI, | |||
49742 | const X86Subtarget &Subtarget) { | |||
49743 | SDLoc dl(N); | |||
49744 | SDValue N0 = N->getOperand(0); | |||
49745 | EVT VT = N->getValueType(0); | |||
49746 | ||||
49747 | // (i32 (aext (i8 (x86isd::setcc_carry)))) -> (i32 (x86isd::setcc_carry)) | |||
49748 | // FIXME: Is this needed? We don't seem to have any tests for it. | |||
49749 | if (!DCI.isBeforeLegalizeOps() && N->getOpcode() == ISD::ANY_EXTEND && | |||
49750 | N0.getOpcode() == X86ISD::SETCC_CARRY) { | |||
49751 | SDValue Setcc = DAG.getNode(X86ISD::SETCC_CARRY, dl, VT, N0->getOperand(0), | |||
49752 | N0->getOperand(1)); | |||
49753 | bool ReplaceOtherUses = !N0.hasOneUse(); | |||
49754 | DCI.CombineTo(N, Setcc); | |||
49755 | // Replace other uses with a truncate of the widened setcc_carry. | |||
49756 | if (ReplaceOtherUses) { | |||
49757 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, SDLoc(N0), | |||
49758 | N0.getValueType(), Setcc); | |||
49759 | DCI.CombineTo(N0.getNode(), Trunc); | |||
49760 | } | |||
49761 | ||||
49762 | return SDValue(N, 0); | |||
49763 | } | |||
49764 | ||||
49765 | if (SDValue NewCMov = combineToExtendCMOV(N, DAG)) | |||
49766 | return NewCMov; | |||
49767 | ||||
49768 | if (DCI.isBeforeLegalizeOps()) | |||
49769 | if (SDValue V = combineExtSetcc(N, DAG, Subtarget)) | |||
49770 | return V; | |||
49771 | ||||
49772 | if (SDValue V = combineToExtendBoolVectorInReg(N, DAG, DCI, Subtarget)) | |||
49773 | return V; | |||
49774 | ||||
49775 | if (VT.isVector()) | |||
49776 | if (SDValue R = PromoteMaskArithmetic(N, DAG, Subtarget)) | |||
49777 | return R; | |||
49778 | ||||
49779 | if (SDValue NewAdd = promoteExtBeforeAdd(N, DAG, Subtarget)) | |||
49780 | return NewAdd; | |||
49781 | ||||
49782 | if (SDValue R = combineOrCmpEqZeroToCtlzSrl(N, DAG, DCI, Subtarget)) | |||
49783 | return R; | |||
49784 | ||||
49785 | // TODO: Combine with any target/faux shuffle. | |||
49786 | if (N0.getOpcode() == X86ISD::PACKUS && N0.getValueSizeInBits() == 128 && | |||
49787 | VT.getScalarSizeInBits() == N0.getOperand(0).getScalarValueSizeInBits()) { | |||
49788 | SDValue N00 = N0.getOperand(0); | |||
49789 | SDValue N01 = N0.getOperand(1); | |||
49790 | unsigned NumSrcEltBits = N00.getScalarValueSizeInBits(); | |||
49791 | APInt ZeroMask = APInt::getHighBitsSet(NumSrcEltBits, NumSrcEltBits / 2); | |||
49792 | if ((N00.isUndef() || DAG.MaskedValueIsZero(N00, ZeroMask)) && | |||
49793 | (N01.isUndef() || DAG.MaskedValueIsZero(N01, ZeroMask))) { | |||
49794 | return concatSubVectors(N00, N01, DAG, dl); | |||
49795 | } | |||
49796 | } | |||
49797 | ||||
49798 | return SDValue(); | |||
49799 | } | |||
49800 | ||||
49801 | /// Recursive helper for combineVectorSizedSetCCEquality() to see if we have a | |||
49802 | /// recognizable memcmp expansion. | |||
49803 | static bool isOrXorXorTree(SDValue X, bool Root = true) { | |||
49804 | if (X.getOpcode() == ISD::OR) | |||
49805 | return isOrXorXorTree(X.getOperand(0), false) && | |||
49806 | isOrXorXorTree(X.getOperand(1), false); | |||
49807 | if (Root) | |||
49808 | return false; | |||
49809 | return X.getOpcode() == ISD::XOR; | |||
49810 | } | |||
49811 | ||||
49812 | /// Recursive helper for combineVectorSizedSetCCEquality() to emit the memcmp | |||
49813 | /// expansion. | |||
49814 | template<typename F> | |||
49815 | static SDValue emitOrXorXorTree(SDValue X, SDLoc &DL, SelectionDAG &DAG, | |||
49816 | EVT VecVT, EVT CmpVT, bool HasPT, F SToV) { | |||
49817 | SDValue Op0 = X.getOperand(0); | |||
49818 | SDValue Op1 = X.getOperand(1); | |||
49819 | if (X.getOpcode() == ISD::OR) { | |||
49820 | SDValue A = emitOrXorXorTree(Op0, DL, DAG, VecVT, CmpVT, HasPT, SToV); | |||
49821 | SDValue B = emitOrXorXorTree(Op1, DL, DAG, VecVT, CmpVT, HasPT, SToV); | |||
49822 | if (VecVT != CmpVT) | |||
49823 | return DAG.getNode(ISD::OR, DL, CmpVT, A, B); | |||
49824 | if (HasPT) | |||
49825 | return DAG.getNode(ISD::OR, DL, VecVT, A, B); | |||
49826 | return DAG.getNode(ISD::AND, DL, CmpVT, A, B); | |||
49827 | } else if (X.getOpcode() == ISD::XOR) { | |||
49828 | SDValue A = SToV(Op0); | |||
49829 | SDValue B = SToV(Op1); | |||
49830 | if (VecVT != CmpVT) | |||
49831 | return DAG.getSetCC(DL, CmpVT, A, B, ISD::SETNE); | |||
49832 | if (HasPT) | |||
49833 | return DAG.getNode(ISD::XOR, DL, VecVT, A, B); | |||
49834 | return DAG.getSetCC(DL, CmpVT, A, B, ISD::SETEQ); | |||
49835 | } | |||
49836 | llvm_unreachable("Impossible")::llvm::llvm_unreachable_internal("Impossible", "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 49836); | |||
49837 | } | |||
49838 | ||||
49839 | /// Try to map a 128-bit or larger integer comparison to vector instructions | |||
49840 | /// before type legalization splits it up into chunks. | |||
49841 | static SDValue combineVectorSizedSetCCEquality(SDNode *SetCC, SelectionDAG &DAG, | |||
49842 | const X86Subtarget &Subtarget) { | |||
49843 | ISD::CondCode CC = cast<CondCodeSDNode>(SetCC->getOperand(2))->get(); | |||
49844 | assert((CC == ISD::SETNE || CC == ISD::SETEQ) && "Bad comparison predicate")(static_cast <bool> ((CC == ISD::SETNE || CC == ISD::SETEQ ) && "Bad comparison predicate") ? void (0) : __assert_fail ("(CC == ISD::SETNE || CC == ISD::SETEQ) && \"Bad comparison predicate\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 49844, __extension__ __PRETTY_FUNCTION__)); | |||
49845 | ||||
49846 | // We're looking for an oversized integer equality comparison. | |||
49847 | SDValue X = SetCC->getOperand(0); | |||
49848 | SDValue Y = SetCC->getOperand(1); | |||
49849 | EVT OpVT = X.getValueType(); | |||
49850 | unsigned OpSize = OpVT.getSizeInBits(); | |||
49851 | if (!OpVT.isScalarInteger() || OpSize < 128) | |||
49852 | return SDValue(); | |||
49853 | ||||
49854 | // Ignore a comparison with zero because that gets special treatment in | |||
49855 | // EmitTest(). But make an exception for the special case of a pair of | |||
49856 | // logically-combined vector-sized operands compared to zero. This pattern may | |||
49857 | // be generated by the memcmp expansion pass with oversized integer compares | |||
49858 | // (see PR33325). | |||
49859 | bool IsOrXorXorTreeCCZero = isNullConstant(Y) && isOrXorXorTree(X); | |||
49860 | if (isNullConstant(Y) && !IsOrXorXorTreeCCZero) | |||
49861 | return SDValue(); | |||
49862 | ||||
49863 | // Don't perform this combine if constructing the vector will be expensive. | |||
49864 | auto IsVectorBitCastCheap = [](SDValue X) { | |||
49865 | X = peekThroughBitcasts(X); | |||
49866 | return isa<ConstantSDNode>(X) || X.getValueType().isVector() || | |||
49867 | X.getOpcode() == ISD::LOAD; | |||
49868 | }; | |||
49869 | if ((!IsVectorBitCastCheap(X) || !IsVectorBitCastCheap(Y)) && | |||
49870 | !IsOrXorXorTreeCCZero) | |||
49871 | return SDValue(); | |||
49872 | ||||
49873 | EVT VT = SetCC->getValueType(0); | |||
49874 | SDLoc DL(SetCC); | |||
49875 | ||||
49876 | // Use XOR (plus OR) and PTEST after SSE4.1 for 128/256-bit operands. | |||
49877 | // Use PCMPNEQ (plus OR) and KORTEST for 512-bit operands. | |||
49878 | // Otherwise use PCMPEQ (plus AND) and mask testing. | |||
49879 | if ((OpSize == 128 && Subtarget.hasSSE2()) || | |||
49880 | (OpSize == 256 && Subtarget.hasAVX()) || | |||
49881 | (OpSize == 512 && Subtarget.useAVX512Regs())) { | |||
49882 | bool HasPT = Subtarget.hasSSE41(); | |||
49883 | ||||
49884 | // PTEST and MOVMSK are slow on Knights Landing and Knights Mill and widened | |||
49885 | // vector registers are essentially free. (Technically, widening registers | |||
49886 | // prevents load folding, but the tradeoff is worth it.) | |||
49887 | bool PreferKOT = Subtarget.preferMaskRegisters(); | |||
49888 | bool NeedZExt = PreferKOT && !Subtarget.hasVLX() && OpSize != 512; | |||
49889 | ||||
49890 | EVT VecVT = MVT::v16i8; | |||
49891 | EVT CmpVT = PreferKOT ? MVT::v16i1 : VecVT; | |||
49892 | if (OpSize == 256) { | |||
49893 | VecVT = MVT::v32i8; | |||
49894 | CmpVT = PreferKOT ? MVT::v32i1 : VecVT; | |||
49895 | } | |||
49896 | EVT CastVT = VecVT; | |||
49897 | bool NeedsAVX512FCast = false; | |||
49898 | if (OpSize == 512 || NeedZExt) { | |||
49899 | if (Subtarget.hasBWI()) { | |||
49900 | VecVT = MVT::v64i8; | |||
49901 | CmpVT = MVT::v64i1; | |||
49902 | if (OpSize == 512) | |||
49903 | CastVT = VecVT; | |||
49904 | } else { | |||
49905 | VecVT = MVT::v16i32; | |||
49906 | CmpVT = MVT::v16i1; | |||
49907 | CastVT = OpSize == 512 ? VecVT : | |||
49908 | OpSize == 256 ? MVT::v8i32 : MVT::v4i32; | |||
49909 | NeedsAVX512FCast = true; | |||
49910 | } | |||
49911 | } | |||
49912 | ||||
49913 | auto ScalarToVector = [&](SDValue X) -> SDValue { | |||
49914 | bool TmpZext = false; | |||
49915 | EVT TmpCastVT = CastVT; | |||
49916 | if (X.getOpcode() == ISD::ZERO_EXTEND) { | |||
49917 | SDValue OrigX = X.getOperand(0); | |||
49918 | unsigned OrigSize = OrigX.getScalarValueSizeInBits(); | |||
49919 | if (OrigSize < OpSize) { | |||
49920 | if (OrigSize == 128) { | |||
49921 | TmpCastVT = NeedsAVX512FCast ? MVT::v4i32 : MVT::v16i8; | |||
49922 | X = OrigX; | |||
49923 | TmpZext = true; | |||
49924 | } else if (OrigSize == 256) { | |||
49925 | TmpCastVT = NeedsAVX512FCast ? MVT::v8i32 : MVT::v32i8; | |||
49926 | X = OrigX; | |||
49927 | TmpZext = true; | |||
49928 | } | |||
49929 | } | |||
49930 | } | |||
49931 | X = DAG.getBitcast(TmpCastVT, X); | |||
49932 | if (!NeedZExt && !TmpZext) | |||
49933 | return X; | |||
49934 | return DAG.getNode(ISD::INSERT_SUBVECTOR, DL, VecVT, | |||
49935 | DAG.getConstant(0, DL, VecVT), X, | |||
49936 | DAG.getVectorIdxConstant(0, DL)); | |||
49937 | }; | |||
49938 | ||||
49939 | SDValue Cmp; | |||
49940 | if (IsOrXorXorTreeCCZero) { | |||
49941 | // This is a bitwise-combined equality comparison of 2 pairs of vectors: | |||
49942 | // setcc i128 (or (xor A, B), (xor C, D)), 0, eq|ne | |||
49943 | // Use 2 vector equality compares and 'and' the results before doing a | |||
49944 | // MOVMSK. | |||
49945 | Cmp = emitOrXorXorTree(X, DL, DAG, VecVT, CmpVT, HasPT, ScalarToVector); | |||
49946 | } else { | |||
49947 | SDValue VecX = ScalarToVector(X); | |||
49948 | SDValue VecY = ScalarToVector(Y); | |||
49949 | if (VecVT != CmpVT) { | |||
49950 | Cmp = DAG.getSetCC(DL, CmpVT, VecX, VecY, ISD::SETNE); | |||
49951 | } else if (HasPT) { | |||
49952 | Cmp = DAG.getNode(ISD::XOR, DL, VecVT, VecX, VecY); | |||
49953 | } else { | |||
49954 | Cmp = DAG.getSetCC(DL, CmpVT, VecX, VecY, ISD::SETEQ); | |||
49955 | } | |||
49956 | } | |||
49957 | // AVX512 should emit a setcc that will lower to kortest. | |||
49958 | if (VecVT != CmpVT) { | |||
49959 | EVT KRegVT = CmpVT == MVT::v64i1 ? MVT::i64 : | |||
49960 | CmpVT == MVT::v32i1 ? MVT::i32 : MVT::i16; | |||
49961 | return DAG.getSetCC(DL, VT, DAG.getBitcast(KRegVT, Cmp), | |||
49962 | DAG.getConstant(0, DL, KRegVT), CC); | |||
49963 | } | |||
49964 | if (HasPT) { | |||
49965 | SDValue BCCmp = DAG.getBitcast(OpSize == 256 ? MVT::v4i64 : MVT::v2i64, | |||
49966 | Cmp); | |||
49967 | SDValue PT = DAG.getNode(X86ISD::PTEST, DL, MVT::i32, BCCmp, BCCmp); | |||
49968 | X86::CondCode X86CC = CC == ISD::SETEQ ? X86::COND_E : X86::COND_NE; | |||
49969 | SDValue X86SetCC = getSETCC(X86CC, PT, DL, DAG); | |||
49970 | return DAG.getNode(ISD::TRUNCATE, DL, VT, X86SetCC.getValue(0)); | |||
49971 | } | |||
49972 | // If all bytes match (bitmask is 0x(FFFF)FFFF), that's equality. | |||
49973 | // setcc i128 X, Y, eq --> setcc (pmovmskb (pcmpeqb X, Y)), 0xFFFF, eq | |||
49974 | // setcc i128 X, Y, ne --> setcc (pmovmskb (pcmpeqb X, Y)), 0xFFFF, ne | |||
49975 | assert(Cmp.getValueType() == MVT::v16i8 &&(static_cast <bool> (Cmp.getValueType() == MVT::v16i8 && "Non 128-bit vector on pre-SSE41 target") ? void (0) : __assert_fail ("Cmp.getValueType() == MVT::v16i8 && \"Non 128-bit vector on pre-SSE41 target\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 49976, __extension__ __PRETTY_FUNCTION__)) | |||
49976 | "Non 128-bit vector on pre-SSE41 target")(static_cast <bool> (Cmp.getValueType() == MVT::v16i8 && "Non 128-bit vector on pre-SSE41 target") ? void (0) : __assert_fail ("Cmp.getValueType() == MVT::v16i8 && \"Non 128-bit vector on pre-SSE41 target\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 49976, __extension__ __PRETTY_FUNCTION__)); | |||
49977 | SDValue MovMsk = DAG.getNode(X86ISD::MOVMSK, DL, MVT::i32, Cmp); | |||
49978 | SDValue FFFFs = DAG.getConstant(0xFFFF, DL, MVT::i32); | |||
49979 | return DAG.getSetCC(DL, VT, MovMsk, FFFFs, CC); | |||
49980 | } | |||
49981 | ||||
49982 | return SDValue(); | |||
49983 | } | |||
49984 | ||||
49985 | static SDValue combineSetCC(SDNode *N, SelectionDAG &DAG, | |||
49986 | TargetLowering::DAGCombinerInfo &DCI, | |||
49987 | const X86Subtarget &Subtarget) { | |||
49988 | const ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get(); | |||
49989 | const SDValue LHS = N->getOperand(0); | |||
49990 | const SDValue RHS = N->getOperand(1); | |||
49991 | EVT VT = N->getValueType(0); | |||
49992 | EVT OpVT = LHS.getValueType(); | |||
49993 | SDLoc DL(N); | |||
49994 | ||||
49995 | if (CC == ISD::SETNE || CC == ISD::SETEQ) { | |||
49996 | if (SDValue V = combineVectorSizedSetCCEquality(N, DAG, Subtarget)) | |||
49997 | return V; | |||
49998 | ||||
49999 | if (VT == MVT::i1 && isNullConstant(RHS)) { | |||
50000 | SDValue X86CC; | |||
50001 | if (SDValue V = | |||
50002 | MatchVectorAllZeroTest(LHS, CC, DL, Subtarget, DAG, X86CC)) | |||
50003 | return DAG.getNode(ISD::TRUNCATE, DL, VT, | |||
50004 | DAG.getNode(X86ISD::SETCC, DL, MVT::i8, X86CC, V)); | |||
50005 | } | |||
50006 | ||||
50007 | if (OpVT.isScalarInteger()) { | |||
50008 | // cmpeq(or(X,Y),X) --> cmpeq(and(~X,Y),0) | |||
50009 | // cmpne(or(X,Y),X) --> cmpne(and(~X,Y),0) | |||
50010 | auto MatchOrCmpEq = [&](SDValue N0, SDValue N1) { | |||
50011 | if (N0.getOpcode() == ISD::OR && N0->hasOneUse()) { | |||
50012 | if (N0.getOperand(0) == N1) | |||
50013 | return DAG.getNode(ISD::AND, DL, OpVT, DAG.getNOT(DL, N1, OpVT), | |||
50014 | N0.getOperand(1)); | |||
50015 | if (N0.getOperand(1) == N1) | |||
50016 | return DAG.getNode(ISD::AND, DL, OpVT, DAG.getNOT(DL, N1, OpVT), | |||
50017 | N0.getOperand(0)); | |||
50018 | } | |||
50019 | return SDValue(); | |||
50020 | }; | |||
50021 | if (SDValue AndN = MatchOrCmpEq(LHS, RHS)) | |||
50022 | return DAG.getSetCC(DL, VT, AndN, DAG.getConstant(0, DL, OpVT), CC); | |||
50023 | if (SDValue AndN = MatchOrCmpEq(RHS, LHS)) | |||
50024 | return DAG.getSetCC(DL, VT, AndN, DAG.getConstant(0, DL, OpVT), CC); | |||
50025 | ||||
50026 | // cmpeq(and(X,Y),Y) --> cmpeq(and(~X,Y),0) | |||
50027 | // cmpne(and(X,Y),Y) --> cmpne(and(~X,Y),0) | |||
50028 | auto MatchAndCmpEq = [&](SDValue N0, SDValue N1) { | |||
50029 | if (N0.getOpcode() == ISD::AND && N0->hasOneUse()) { | |||
50030 | if (N0.getOperand(0) == N1) | |||
50031 | return DAG.getNode(ISD::AND, DL, OpVT, N1, | |||
50032 | DAG.getNOT(DL, N0.getOperand(1), OpVT)); | |||
50033 | if (N0.getOperand(1) == N1) | |||
50034 | return DAG.getNode(ISD::AND, DL, OpVT, N1, | |||
50035 | DAG.getNOT(DL, N0.getOperand(0), OpVT)); | |||
50036 | } | |||
50037 | return SDValue(); | |||
50038 | }; | |||
50039 | if (SDValue AndN = MatchAndCmpEq(LHS, RHS)) | |||
50040 | return DAG.getSetCC(DL, VT, AndN, DAG.getConstant(0, DL, OpVT), CC); | |||
50041 | if (SDValue AndN = MatchAndCmpEq(RHS, LHS)) | |||
50042 | return DAG.getSetCC(DL, VT, AndN, DAG.getConstant(0, DL, OpVT), CC); | |||
50043 | ||||
50044 | // cmpeq(trunc(x),0) --> cmpeq(x,0) | |||
50045 | // cmpne(trunc(x),0) --> cmpne(x,0) | |||
50046 | // iff x upper bits are zero. | |||
50047 | // TODO: Add support for RHS to be truncate as well? | |||
50048 | if (LHS.getOpcode() == ISD::TRUNCATE && | |||
50049 | LHS.getOperand(0).getScalarValueSizeInBits() >= 32 && | |||
50050 | isNullConstant(RHS) && !DCI.isBeforeLegalize()) { | |||
50051 | EVT SrcVT = LHS.getOperand(0).getValueType(); | |||
50052 | APInt UpperBits = APInt::getBitsSetFrom(SrcVT.getScalarSizeInBits(), | |||
50053 | OpVT.getScalarSizeInBits()); | |||
50054 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
50055 | if (DAG.MaskedValueIsZero(LHS.getOperand(0), UpperBits) && | |||
50056 | TLI.isTypeLegal(LHS.getOperand(0).getValueType())) | |||
50057 | return DAG.getSetCC(DL, VT, LHS.getOperand(0), | |||
50058 | DAG.getConstant(0, DL, SrcVT), CC); | |||
50059 | } | |||
50060 | } | |||
50061 | } | |||
50062 | ||||
50063 | if (VT.isVector() && VT.getVectorElementType() == MVT::i1 && | |||
50064 | (CC == ISD::SETNE || CC == ISD::SETEQ || ISD::isSignedIntSetCC(CC))) { | |||
50065 | // Using temporaries to avoid messing up operand ordering for later | |||
50066 | // transformations if this doesn't work. | |||
50067 | SDValue Op0 = LHS; | |||
50068 | SDValue Op1 = RHS; | |||
50069 | ISD::CondCode TmpCC = CC; | |||
50070 | // Put build_vector on the right. | |||
50071 | if (Op0.getOpcode() == ISD::BUILD_VECTOR) { | |||
50072 | std::swap(Op0, Op1); | |||
50073 | TmpCC = ISD::getSetCCSwappedOperands(TmpCC); | |||
50074 | } | |||
50075 | ||||
50076 | bool IsSEXT0 = | |||
50077 | (Op0.getOpcode() == ISD::SIGN_EXTEND) && | |||
50078 | (Op0.getOperand(0).getValueType().getVectorElementType() == MVT::i1); | |||
50079 | bool IsVZero1 = ISD::isBuildVectorAllZeros(Op1.getNode()); | |||
50080 | ||||
50081 | if (IsSEXT0 && IsVZero1) { | |||
50082 | assert(VT == Op0.getOperand(0).getValueType() &&(static_cast <bool> (VT == Op0.getOperand(0).getValueType () && "Unexpected operand type") ? void (0) : __assert_fail ("VT == Op0.getOperand(0).getValueType() && \"Unexpected operand type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 50083, __extension__ __PRETTY_FUNCTION__)) | |||
50083 | "Unexpected operand type")(static_cast <bool> (VT == Op0.getOperand(0).getValueType () && "Unexpected operand type") ? void (0) : __assert_fail ("VT == Op0.getOperand(0).getValueType() && \"Unexpected operand type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 50083, __extension__ __PRETTY_FUNCTION__)); | |||
50084 | if (TmpCC == ISD::SETGT) | |||
50085 | return DAG.getConstant(0, DL, VT); | |||
50086 | if (TmpCC == ISD::SETLE) | |||
50087 | return DAG.getConstant(1, DL, VT); | |||
50088 | if (TmpCC == ISD::SETEQ || TmpCC == ISD::SETGE) | |||
50089 | return DAG.getNOT(DL, Op0.getOperand(0), VT); | |||
50090 | ||||
50091 | assert((TmpCC == ISD::SETNE || TmpCC == ISD::SETLT) &&(static_cast <bool> ((TmpCC == ISD::SETNE || TmpCC == ISD ::SETLT) && "Unexpected condition code!") ? void (0) : __assert_fail ("(TmpCC == ISD::SETNE || TmpCC == ISD::SETLT) && \"Unexpected condition code!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 50092, __extension__ __PRETTY_FUNCTION__)) | |||
50092 | "Unexpected condition code!")(static_cast <bool> ((TmpCC == ISD::SETNE || TmpCC == ISD ::SETLT) && "Unexpected condition code!") ? void (0) : __assert_fail ("(TmpCC == ISD::SETNE || TmpCC == ISD::SETLT) && \"Unexpected condition code!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 50092, __extension__ __PRETTY_FUNCTION__)); | |||
50093 | return Op0.getOperand(0); | |||
50094 | } | |||
50095 | } | |||
50096 | ||||
50097 | // If we have AVX512, but not BWI and this is a vXi16/vXi8 setcc, just | |||
50098 | // pre-promote its result type since vXi1 vectors don't get promoted | |||
50099 | // during type legalization. | |||
50100 | // NOTE: The element count check is to ignore operand types that need to | |||
50101 | // go through type promotion to a 128-bit vector. | |||
50102 | if (Subtarget.hasAVX512() && !Subtarget.hasBWI() && VT.isVector() && | |||
50103 | VT.getVectorElementType() == MVT::i1 && | |||
50104 | (OpVT.getVectorElementType() == MVT::i8 || | |||
50105 | OpVT.getVectorElementType() == MVT::i16)) { | |||
50106 | SDValue Setcc = DAG.getSetCC(DL, OpVT, LHS, RHS, CC); | |||
50107 | return DAG.getNode(ISD::TRUNCATE, DL, VT, Setcc); | |||
50108 | } | |||
50109 | ||||
50110 | // For an SSE1-only target, lower a comparison of v4f32 to X86ISD::CMPP early | |||
50111 | // to avoid scalarization via legalization because v4i32 is not a legal type. | |||
50112 | if (Subtarget.hasSSE1() && !Subtarget.hasSSE2() && VT == MVT::v4i32 && | |||
50113 | LHS.getValueType() == MVT::v4f32) | |||
50114 | return LowerVSETCC(SDValue(N, 0), Subtarget, DAG); | |||
50115 | ||||
50116 | return SDValue(); | |||
50117 | } | |||
50118 | ||||
50119 | static SDValue combineMOVMSK(SDNode *N, SelectionDAG &DAG, | |||
50120 | TargetLowering::DAGCombinerInfo &DCI, | |||
50121 | const X86Subtarget &Subtarget) { | |||
50122 | SDValue Src = N->getOperand(0); | |||
50123 | MVT SrcVT = Src.getSimpleValueType(); | |||
50124 | MVT VT = N->getSimpleValueType(0); | |||
50125 | unsigned NumBits = VT.getScalarSizeInBits(); | |||
50126 | unsigned NumElts = SrcVT.getVectorNumElements(); | |||
50127 | ||||
50128 | // Perform constant folding. | |||
50129 | if (ISD::isBuildVectorOfConstantSDNodes(Src.getNode())) { | |||
50130 | assert(VT == MVT::i32 && "Unexpected result type")(static_cast <bool> (VT == MVT::i32 && "Unexpected result type" ) ? void (0) : __assert_fail ("VT == MVT::i32 && \"Unexpected result type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 50130, __extension__ __PRETTY_FUNCTION__)); | |||
50131 | APInt Imm(32, 0); | |||
50132 | for (unsigned Idx = 0, e = Src.getNumOperands(); Idx < e; ++Idx) { | |||
50133 | if (!Src.getOperand(Idx).isUndef() && | |||
50134 | Src.getConstantOperandAPInt(Idx).isNegative()) | |||
50135 | Imm.setBit(Idx); | |||
50136 | } | |||
50137 | return DAG.getConstant(Imm, SDLoc(N), VT); | |||
50138 | } | |||
50139 | ||||
50140 | // Look through int->fp bitcasts that don't change the element width. | |||
50141 | unsigned EltWidth = SrcVT.getScalarSizeInBits(); | |||
50142 | if (Subtarget.hasSSE2() && Src.getOpcode() == ISD::BITCAST && | |||
50143 | Src.getOperand(0).getScalarValueSizeInBits() == EltWidth) | |||
50144 | return DAG.getNode(X86ISD::MOVMSK, SDLoc(N), VT, Src.getOperand(0)); | |||
50145 | ||||
50146 | // Fold movmsk(not(x)) -> not(movmsk(x)) to improve folding of movmsk results | |||
50147 | // with scalar comparisons. | |||
50148 | if (SDValue NotSrc = IsNOT(Src, DAG)) { | |||
50149 | SDLoc DL(N); | |||
50150 | APInt NotMask = APInt::getLowBitsSet(NumBits, NumElts); | |||
50151 | NotSrc = DAG.getBitcast(SrcVT, NotSrc); | |||
50152 | return DAG.getNode(ISD::XOR, DL, VT, | |||
50153 | DAG.getNode(X86ISD::MOVMSK, DL, VT, NotSrc), | |||
50154 | DAG.getConstant(NotMask, DL, VT)); | |||
50155 | } | |||
50156 | ||||
50157 | // Fold movmsk(icmp_sgt(x,-1)) -> not(movmsk(x)) to improve folding of movmsk | |||
50158 | // results with scalar comparisons. | |||
50159 | if (Src.getOpcode() == X86ISD::PCMPGT && | |||
50160 | ISD::isBuildVectorAllOnes(Src.getOperand(1).getNode())) { | |||
50161 | SDLoc DL(N); | |||
50162 | APInt NotMask = APInt::getLowBitsSet(NumBits, NumElts); | |||
50163 | return DAG.getNode(ISD::XOR, DL, VT, | |||
50164 | DAG.getNode(X86ISD::MOVMSK, DL, VT, Src.getOperand(0)), | |||
50165 | DAG.getConstant(NotMask, DL, VT)); | |||
50166 | } | |||
50167 | ||||
50168 | // Simplify the inputs. | |||
50169 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
50170 | APInt DemandedMask(APInt::getAllOnes(NumBits)); | |||
50171 | if (TLI.SimplifyDemandedBits(SDValue(N, 0), DemandedMask, DCI)) | |||
50172 | return SDValue(N, 0); | |||
50173 | ||||
50174 | return SDValue(); | |||
50175 | } | |||
50176 | ||||
50177 | static SDValue combineX86GatherScatter(SDNode *N, SelectionDAG &DAG, | |||
50178 | TargetLowering::DAGCombinerInfo &DCI) { | |||
50179 | // With vector masks we only demand the upper bit of the mask. | |||
50180 | SDValue Mask = cast<X86MaskedGatherScatterSDNode>(N)->getMask(); | |||
50181 | if (Mask.getScalarValueSizeInBits() != 1) { | |||
50182 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
50183 | APInt DemandedMask(APInt::getSignMask(Mask.getScalarValueSizeInBits())); | |||
50184 | if (TLI.SimplifyDemandedBits(Mask, DemandedMask, DCI)) { | |||
50185 | if (N->getOpcode() != ISD::DELETED_NODE) | |||
50186 | DCI.AddToWorklist(N); | |||
50187 | return SDValue(N, 0); | |||
50188 | } | |||
50189 | } | |||
50190 | ||||
50191 | return SDValue(); | |||
50192 | } | |||
50193 | ||||
50194 | static SDValue rebuildGatherScatter(MaskedGatherScatterSDNode *GorS, | |||
50195 | SDValue Index, SDValue Base, SDValue Scale, | |||
50196 | SelectionDAG &DAG) { | |||
50197 | SDLoc DL(GorS); | |||
50198 | ||||
50199 | if (auto *Gather = dyn_cast<MaskedGatherSDNode>(GorS)) { | |||
50200 | SDValue Ops[] = { Gather->getChain(), Gather->getPassThru(), | |||
50201 | Gather->getMask(), Base, Index, Scale } ; | |||
50202 | return DAG.getMaskedGather(Gather->getVTList(), | |||
50203 | Gather->getMemoryVT(), DL, Ops, | |||
50204 | Gather->getMemOperand(), | |||
50205 | Gather->getIndexType(), | |||
50206 | Gather->getExtensionType()); | |||
50207 | } | |||
50208 | auto *Scatter = cast<MaskedScatterSDNode>(GorS); | |||
50209 | SDValue Ops[] = { Scatter->getChain(), Scatter->getValue(), | |||
50210 | Scatter->getMask(), Base, Index, Scale }; | |||
50211 | return DAG.getMaskedScatter(Scatter->getVTList(), | |||
50212 | Scatter->getMemoryVT(), DL, | |||
50213 | Ops, Scatter->getMemOperand(), | |||
50214 | Scatter->getIndexType(), | |||
50215 | Scatter->isTruncatingStore()); | |||
50216 | } | |||
50217 | ||||
50218 | static SDValue combineGatherScatter(SDNode *N, SelectionDAG &DAG, | |||
50219 | TargetLowering::DAGCombinerInfo &DCI) { | |||
50220 | SDLoc DL(N); | |||
50221 | auto *GorS = cast<MaskedGatherScatterSDNode>(N); | |||
50222 | SDValue Index = GorS->getIndex(); | |||
50223 | SDValue Base = GorS->getBasePtr(); | |||
50224 | SDValue Scale = GorS->getScale(); | |||
50225 | ||||
50226 | if (DCI.isBeforeLegalize()) { | |||
50227 | unsigned IndexWidth = Index.getScalarValueSizeInBits(); | |||
50228 | ||||
50229 | // Shrink constant indices if they are larger than 32-bits. | |||
50230 | // Only do this before legalize types since v2i64 could become v2i32. | |||
50231 | // FIXME: We could check that the type is legal if we're after legalize | |||
50232 | // types, but then we would need to construct test cases where that happens. | |||
50233 | // FIXME: We could support more than just constant vectors, but we need to | |||
50234 | // careful with costing. A truncate that can be optimized out would be fine. | |||
50235 | // Otherwise we might only want to create a truncate if it avoids a split. | |||
50236 | if (auto *BV = dyn_cast<BuildVectorSDNode>(Index)) { | |||
50237 | if (BV->isConstant() && IndexWidth > 32 && | |||
50238 | DAG.ComputeNumSignBits(Index) > (IndexWidth - 32)) { | |||
50239 | EVT NewVT = Index.getValueType().changeVectorElementType(MVT::i32); | |||
50240 | Index = DAG.getNode(ISD::TRUNCATE, DL, NewVT, Index); | |||
50241 | return rebuildGatherScatter(GorS, Index, Base, Scale, DAG); | |||
50242 | } | |||
50243 | } | |||
50244 | ||||
50245 | // Shrink any sign/zero extends from 32 or smaller to larger than 32 if | |||
50246 | // there are sufficient sign bits. Only do this before legalize types to | |||
50247 | // avoid creating illegal types in truncate. | |||
50248 | if ((Index.getOpcode() == ISD::SIGN_EXTEND || | |||
50249 | Index.getOpcode() == ISD::ZERO_EXTEND) && | |||
50250 | IndexWidth > 32 && | |||
50251 | Index.getOperand(0).getScalarValueSizeInBits() <= 32 && | |||
50252 | DAG.ComputeNumSignBits(Index) > (IndexWidth - 32)) { | |||
50253 | EVT NewVT = Index.getValueType().changeVectorElementType(MVT::i32); | |||
50254 | Index = DAG.getNode(ISD::TRUNCATE, DL, NewVT, Index); | |||
50255 | return rebuildGatherScatter(GorS, Index, Base, Scale, DAG); | |||
50256 | } | |||
50257 | } | |||
50258 | ||||
50259 | if (DCI.isBeforeLegalizeOps()) { | |||
50260 | unsigned IndexWidth = Index.getScalarValueSizeInBits(); | |||
50261 | ||||
50262 | // Make sure the index is either i32 or i64 | |||
50263 | if (IndexWidth != 32 && IndexWidth != 64) { | |||
50264 | MVT EltVT = IndexWidth > 32 ? MVT::i64 : MVT::i32; | |||
50265 | EVT IndexVT = Index.getValueType().changeVectorElementType(EltVT); | |||
50266 | Index = DAG.getSExtOrTrunc(Index, DL, IndexVT); | |||
50267 | return rebuildGatherScatter(GorS, Index, Base, Scale, DAG); | |||
50268 | } | |||
50269 | } | |||
50270 | ||||
50271 | // With vector masks we only demand the upper bit of the mask. | |||
50272 | SDValue Mask = GorS->getMask(); | |||
50273 | if (Mask.getScalarValueSizeInBits() != 1) { | |||
50274 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
50275 | APInt DemandedMask(APInt::getSignMask(Mask.getScalarValueSizeInBits())); | |||
50276 | if (TLI.SimplifyDemandedBits(Mask, DemandedMask, DCI)) { | |||
50277 | if (N->getOpcode() != ISD::DELETED_NODE) | |||
50278 | DCI.AddToWorklist(N); | |||
50279 | return SDValue(N, 0); | |||
50280 | } | |||
50281 | } | |||
50282 | ||||
50283 | return SDValue(); | |||
50284 | } | |||
50285 | ||||
50286 | // Optimize RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT | |||
50287 | static SDValue combineX86SetCC(SDNode *N, SelectionDAG &DAG, | |||
50288 | const X86Subtarget &Subtarget) { | |||
50289 | SDLoc DL(N); | |||
50290 | X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0)); | |||
50291 | SDValue EFLAGS = N->getOperand(1); | |||
50292 | ||||
50293 | // Try to simplify the EFLAGS and condition code operands. | |||
50294 | if (SDValue Flags = combineSetCCEFLAGS(EFLAGS, CC, DAG, Subtarget)) | |||
50295 | return getSETCC(CC, Flags, DL, DAG); | |||
50296 | ||||
50297 | return SDValue(); | |||
50298 | } | |||
50299 | ||||
50300 | /// Optimize branch condition evaluation. | |||
50301 | static SDValue combineBrCond(SDNode *N, SelectionDAG &DAG, | |||
50302 | const X86Subtarget &Subtarget) { | |||
50303 | SDLoc DL(N); | |||
50304 | SDValue EFLAGS = N->getOperand(3); | |||
50305 | X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2)); | |||
50306 | ||||
50307 | // Try to simplify the EFLAGS and condition code operands. | |||
50308 | // Make sure to not keep references to operands, as combineSetCCEFLAGS can | |||
50309 | // RAUW them under us. | |||
50310 | if (SDValue Flags = combineSetCCEFLAGS(EFLAGS, CC, DAG, Subtarget)) { | |||
50311 | SDValue Cond = DAG.getTargetConstant(CC, DL, MVT::i8); | |||
50312 | return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), N->getOperand(0), | |||
50313 | N->getOperand(1), Cond, Flags); | |||
50314 | } | |||
50315 | ||||
50316 | return SDValue(); | |||
50317 | } | |||
50318 | ||||
50319 | // TODO: Could we move this to DAGCombine? | |||
50320 | static SDValue combineVectorCompareAndMaskUnaryOp(SDNode *N, | |||
50321 | SelectionDAG &DAG) { | |||
50322 | // Take advantage of vector comparisons (etc.) producing 0 or -1 in each lane | |||
50323 | // to optimize away operation when it's from a constant. | |||
50324 | // | |||
50325 | // The general transformation is: | |||
50326 | // UNARYOP(AND(VECTOR_CMP(x,y), constant)) --> | |||
50327 | // AND(VECTOR_CMP(x,y), constant2) | |||
50328 | // constant2 = UNARYOP(constant) | |||
50329 | ||||
50330 | // Early exit if this isn't a vector operation, the operand of the | |||
50331 | // unary operation isn't a bitwise AND, or if the sizes of the operations | |||
50332 | // aren't the same. | |||
50333 | EVT VT = N->getValueType(0); | |||
50334 | bool IsStrict = N->isStrictFPOpcode(); | |||
50335 | unsigned NumEltBits = VT.getScalarSizeInBits(); | |||
50336 | SDValue Op0 = N->getOperand(IsStrict ? 1 : 0); | |||
50337 | if (!VT.isVector() || Op0.getOpcode() != ISD::AND || | |||
50338 | DAG.ComputeNumSignBits(Op0.getOperand(0)) != NumEltBits || | |||
50339 | VT.getSizeInBits() != Op0.getValueSizeInBits()) | |||
50340 | return SDValue(); | |||
50341 | ||||
50342 | // Now check that the other operand of the AND is a constant. We could | |||
50343 | // make the transformation for non-constant splats as well, but it's unclear | |||
50344 | // that would be a benefit as it would not eliminate any operations, just | |||
50345 | // perform one more step in scalar code before moving to the vector unit. | |||
50346 | if (auto *BV = dyn_cast<BuildVectorSDNode>(Op0.getOperand(1))) { | |||
50347 | // Bail out if the vector isn't a constant. | |||
50348 | if (!BV->isConstant()) | |||
50349 | return SDValue(); | |||
50350 | ||||
50351 | // Everything checks out. Build up the new and improved node. | |||
50352 | SDLoc DL(N); | |||
50353 | EVT IntVT = BV->getValueType(0); | |||
50354 | // Create a new constant of the appropriate type for the transformed | |||
50355 | // DAG. | |||
50356 | SDValue SourceConst; | |||
50357 | if (IsStrict) | |||
50358 | SourceConst = DAG.getNode(N->getOpcode(), DL, {VT, MVT::Other}, | |||
50359 | {N->getOperand(0), SDValue(BV, 0)}); | |||
50360 | else | |||
50361 | SourceConst = DAG.getNode(N->getOpcode(), DL, VT, SDValue(BV, 0)); | |||
50362 | // The AND node needs bitcasts to/from an integer vector type around it. | |||
50363 | SDValue MaskConst = DAG.getBitcast(IntVT, SourceConst); | |||
50364 | SDValue NewAnd = DAG.getNode(ISD::AND, DL, IntVT, Op0->getOperand(0), | |||
50365 | MaskConst); | |||
50366 | SDValue Res = DAG.getBitcast(VT, NewAnd); | |||
50367 | if (IsStrict) | |||
50368 | return DAG.getMergeValues({Res, SourceConst.getValue(1)}, DL); | |||
50369 | return Res; | |||
50370 | } | |||
50371 | ||||
50372 | return SDValue(); | |||
50373 | } | |||
50374 | ||||
50375 | /// If we are converting a value to floating-point, try to replace scalar | |||
50376 | /// truncate of an extracted vector element with a bitcast. This tries to keep | |||
50377 | /// the sequence on XMM registers rather than moving between vector and GPRs. | |||
50378 | static SDValue combineToFPTruncExtElt(SDNode *N, SelectionDAG &DAG) { | |||
50379 | // TODO: This is currently only used by combineSIntToFP, but it is generalized | |||
50380 | // to allow being called by any similar cast opcode. | |||
50381 | // TODO: Consider merging this into lowering: vectorizeExtractedCast(). | |||
50382 | SDValue Trunc = N->getOperand(0); | |||
50383 | if (!Trunc.hasOneUse() || Trunc.getOpcode() != ISD::TRUNCATE) | |||
50384 | return SDValue(); | |||
50385 | ||||
50386 | SDValue ExtElt = Trunc.getOperand(0); | |||
50387 | if (!ExtElt.hasOneUse() || ExtElt.getOpcode() != ISD::EXTRACT_VECTOR_ELT || | |||
50388 | !isNullConstant(ExtElt.getOperand(1))) | |||
50389 | return SDValue(); | |||
50390 | ||||
50391 | EVT TruncVT = Trunc.getValueType(); | |||
50392 | EVT SrcVT = ExtElt.getValueType(); | |||
50393 | unsigned DestWidth = TruncVT.getSizeInBits(); | |||
50394 | unsigned SrcWidth = SrcVT.getSizeInBits(); | |||
50395 | if (SrcWidth % DestWidth != 0) | |||
50396 | return SDValue(); | |||
50397 | ||||
50398 | // inttofp (trunc (extelt X, 0)) --> inttofp (extelt (bitcast X), 0) | |||
50399 | EVT SrcVecVT = ExtElt.getOperand(0).getValueType(); | |||
50400 | unsigned VecWidth = SrcVecVT.getSizeInBits(); | |||
50401 | unsigned NumElts = VecWidth / DestWidth; | |||
50402 | EVT BitcastVT = EVT::getVectorVT(*DAG.getContext(), TruncVT, NumElts); | |||
50403 | SDValue BitcastVec = DAG.getBitcast(BitcastVT, ExtElt.getOperand(0)); | |||
50404 | SDLoc DL(N); | |||
50405 | SDValue NewExtElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, TruncVT, | |||
50406 | BitcastVec, ExtElt.getOperand(1)); | |||
50407 | return DAG.getNode(N->getOpcode(), DL, N->getValueType(0), NewExtElt); | |||
50408 | } | |||
50409 | ||||
50410 | static SDValue combineUIntToFP(SDNode *N, SelectionDAG &DAG, | |||
50411 | const X86Subtarget &Subtarget) { | |||
50412 | bool IsStrict = N->isStrictFPOpcode(); | |||
50413 | SDValue Op0 = N->getOperand(IsStrict ? 1 : 0); | |||
50414 | EVT VT = N->getValueType(0); | |||
50415 | EVT InVT = Op0.getValueType(); | |||
50416 | ||||
50417 | // UINT_TO_FP(vXi1~15) -> UINT_TO_FP(ZEXT(vXi1~15 to vXi16)) | |||
50418 | // UINT_TO_FP(vXi17~31) -> UINT_TO_FP(ZEXT(vXi17~31 to vXi32)) | |||
50419 | // UINT_TO_FP(vXi33~63) -> UINT_TO_FP(ZEXT(vXi33~63 to vXi64)) | |||
50420 | if (InVT.isVector() && VT.getVectorElementType() == MVT::f16) { | |||
50421 | unsigned ScalarSize = InVT.getScalarSizeInBits(); | |||
50422 | if (ScalarSize == 16 || ScalarSize == 32 || ScalarSize >= 64) | |||
50423 | return SDValue(); | |||
50424 | SDLoc dl(N); | |||
50425 | EVT DstVT = EVT::getVectorVT(*DAG.getContext(), | |||
50426 | ScalarSize < 16 ? MVT::i16 | |||
50427 | : ScalarSize < 32 ? MVT::i32 | |||
50428 | : MVT::i64, | |||
50429 | InVT.getVectorNumElements()); | |||
50430 | SDValue P = DAG.getNode(ISD::ZERO_EXTEND, dl, DstVT, Op0); | |||
50431 | if (IsStrict) | |||
50432 | return DAG.getNode(ISD::STRICT_UINT_TO_FP, dl, {VT, MVT::Other}, | |||
50433 | {N->getOperand(0), P}); | |||
50434 | return DAG.getNode(ISD::UINT_TO_FP, dl, VT, P); | |||
50435 | } | |||
50436 | ||||
50437 | // UINT_TO_FP(vXi1) -> SINT_TO_FP(ZEXT(vXi1 to vXi32)) | |||
50438 | // UINT_TO_FP(vXi8) -> SINT_TO_FP(ZEXT(vXi8 to vXi32)) | |||
50439 | // UINT_TO_FP(vXi16) -> SINT_TO_FP(ZEXT(vXi16 to vXi32)) | |||
50440 | if (InVT.isVector() && InVT.getScalarSizeInBits() < 32 && | |||
50441 | VT.getScalarType() != MVT::f16) { | |||
50442 | SDLoc dl(N); | |||
50443 | EVT DstVT = InVT.changeVectorElementType(MVT::i32); | |||
50444 | SDValue P = DAG.getNode(ISD::ZERO_EXTEND, dl, DstVT, Op0); | |||
50445 | ||||
50446 | // UINT_TO_FP isn't legal without AVX512 so use SINT_TO_FP. | |||
50447 | if (IsStrict) | |||
50448 | return DAG.getNode(ISD::STRICT_SINT_TO_FP, dl, {VT, MVT::Other}, | |||
50449 | {N->getOperand(0), P}); | |||
50450 | return DAG.getNode(ISD::SINT_TO_FP, dl, VT, P); | |||
50451 | } | |||
50452 | ||||
50453 | // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't | |||
50454 | // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform | |||
50455 | // the optimization here. | |||
50456 | if (DAG.SignBitIsZero(Op0)) { | |||
50457 | if (IsStrict) | |||
50458 | return DAG.getNode(ISD::STRICT_SINT_TO_FP, SDLoc(N), {VT, MVT::Other}, | |||
50459 | {N->getOperand(0), Op0}); | |||
50460 | return DAG.getNode(ISD::SINT_TO_FP, SDLoc(N), VT, Op0); | |||
50461 | } | |||
50462 | ||||
50463 | return SDValue(); | |||
50464 | } | |||
50465 | ||||
50466 | static SDValue combineSIntToFP(SDNode *N, SelectionDAG &DAG, | |||
50467 | TargetLowering::DAGCombinerInfo &DCI, | |||
50468 | const X86Subtarget &Subtarget) { | |||
50469 | // First try to optimize away the conversion entirely when it's | |||
50470 | // conditionally from a constant. Vectors only. | |||
50471 | bool IsStrict = N->isStrictFPOpcode(); | |||
50472 | if (SDValue Res = combineVectorCompareAndMaskUnaryOp(N, DAG)) | |||
50473 | return Res; | |||
50474 | ||||
50475 | // Now move on to more general possibilities. | |||
50476 | SDValue Op0 = N->getOperand(IsStrict ? 1 : 0); | |||
50477 | EVT VT = N->getValueType(0); | |||
50478 | EVT InVT = Op0.getValueType(); | |||
50479 | ||||
50480 | // SINT_TO_FP(vXi1~15) -> SINT_TO_FP(SEXT(vXi1~15 to vXi16)) | |||
50481 | // SINT_TO_FP(vXi17~31) -> SINT_TO_FP(SEXT(vXi17~31 to vXi32)) | |||
50482 | // SINT_TO_FP(vXi33~63) -> SINT_TO_FP(SEXT(vXi33~63 to vXi64)) | |||
50483 | if (InVT.isVector() && VT.getVectorElementType() == MVT::f16) { | |||
50484 | unsigned ScalarSize = InVT.getScalarSizeInBits(); | |||
50485 | if (ScalarSize == 16 || ScalarSize == 32 || ScalarSize >= 64) | |||
50486 | return SDValue(); | |||
50487 | SDLoc dl(N); | |||
50488 | EVT DstVT = EVT::getVectorVT(*DAG.getContext(), | |||
50489 | ScalarSize < 16 ? MVT::i16 | |||
50490 | : ScalarSize < 32 ? MVT::i32 | |||
50491 | : MVT::i64, | |||
50492 | InVT.getVectorNumElements()); | |||
50493 | SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0); | |||
50494 | if (IsStrict) | |||
50495 | return DAG.getNode(ISD::STRICT_SINT_TO_FP, dl, {VT, MVT::Other}, | |||
50496 | {N->getOperand(0), P}); | |||
50497 | return DAG.getNode(ISD::SINT_TO_FP, dl, VT, P); | |||
50498 | } | |||
50499 | ||||
50500 | // SINT_TO_FP(vXi1) -> SINT_TO_FP(SEXT(vXi1 to vXi32)) | |||
50501 | // SINT_TO_FP(vXi8) -> SINT_TO_FP(SEXT(vXi8 to vXi32)) | |||
50502 | // SINT_TO_FP(vXi16) -> SINT_TO_FP(SEXT(vXi16 to vXi32)) | |||
50503 | if (InVT.isVector() && InVT.getScalarSizeInBits() < 32 && | |||
50504 | VT.getScalarType() != MVT::f16) { | |||
50505 | SDLoc dl(N); | |||
50506 | EVT DstVT = InVT.changeVectorElementType(MVT::i32); | |||
50507 | SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0); | |||
50508 | if (IsStrict) | |||
50509 | return DAG.getNode(ISD::STRICT_SINT_TO_FP, dl, {VT, MVT::Other}, | |||
50510 | {N->getOperand(0), P}); | |||
50511 | return DAG.getNode(ISD::SINT_TO_FP, dl, VT, P); | |||
50512 | } | |||
50513 | ||||
50514 | // Without AVX512DQ we only support i64 to float scalar conversion. For both | |||
50515 | // vectors and scalars, see if we know that the upper bits are all the sign | |||
50516 | // bit, in which case we can truncate the input to i32 and convert from that. | |||
50517 | if (InVT.getScalarSizeInBits() > 32 && !Subtarget.hasDQI()) { | |||
50518 | unsigned BitWidth = InVT.getScalarSizeInBits(); | |||
50519 | unsigned NumSignBits = DAG.ComputeNumSignBits(Op0); | |||
50520 | if (NumSignBits >= (BitWidth - 31)) { | |||
50521 | EVT TruncVT = MVT::i32; | |||
50522 | if (InVT.isVector()) | |||
50523 | TruncVT = InVT.changeVectorElementType(TruncVT); | |||
50524 | SDLoc dl(N); | |||
50525 | if (DCI.isBeforeLegalize() || TruncVT != MVT::v2i32) { | |||
50526 | SDValue Trunc = DAG.getNode(ISD::TRUNCATE, dl, TruncVT, Op0); | |||
50527 | if (IsStrict) | |||
50528 | return DAG.getNode(ISD::STRICT_SINT_TO_FP, dl, {VT, MVT::Other}, | |||
50529 | {N->getOperand(0), Trunc}); | |||
50530 | return DAG.getNode(ISD::SINT_TO_FP, dl, VT, Trunc); | |||
50531 | } | |||
50532 | // If we're after legalize and the type is v2i32 we need to shuffle and | |||
50533 | // use CVTSI2P. | |||
50534 | assert(InVT == MVT::v2i64 && "Unexpected VT!")(static_cast <bool> (InVT == MVT::v2i64 && "Unexpected VT!" ) ? void (0) : __assert_fail ("InVT == MVT::v2i64 && \"Unexpected VT!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 50534, __extension__ __PRETTY_FUNCTION__)); | |||
50535 | SDValue Cast = DAG.getBitcast(MVT::v4i32, Op0); | |||
50536 | SDValue Shuf = DAG.getVectorShuffle(MVT::v4i32, dl, Cast, Cast, | |||
50537 | { 0, 2, -1, -1 }); | |||
50538 | if (IsStrict) | |||
50539 | return DAG.getNode(X86ISD::STRICT_CVTSI2P, dl, {VT, MVT::Other}, | |||
50540 | {N->getOperand(0), Shuf}); | |||
50541 | return DAG.getNode(X86ISD::CVTSI2P, dl, VT, Shuf); | |||
50542 | } | |||
50543 | } | |||
50544 | ||||
50545 | // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have | |||
50546 | // a 32-bit target where SSE doesn't support i64->FP operations. | |||
50547 | if (!Subtarget.useSoftFloat() && Subtarget.hasX87() && | |||
50548 | Op0.getOpcode() == ISD::LOAD) { | |||
50549 | LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode()); | |||
50550 | ||||
50551 | // This transformation is not supported if the result type is f16 or f128. | |||
50552 | if (VT == MVT::f16 || VT == MVT::f128) | |||
50553 | return SDValue(); | |||
50554 | ||||
50555 | // If we have AVX512DQ we can use packed conversion instructions unless | |||
50556 | // the VT is f80. | |||
50557 | if (Subtarget.hasDQI() && VT != MVT::f80) | |||
50558 | return SDValue(); | |||
50559 | ||||
50560 | if (Ld->isSimple() && !VT.isVector() && ISD::isNormalLoad(Op0.getNode()) && | |||
50561 | Op0.hasOneUse() && !Subtarget.is64Bit() && InVT == MVT::i64) { | |||
50562 | std::pair<SDValue, SDValue> Tmp = | |||
50563 | Subtarget.getTargetLowering()->BuildFILD( | |||
50564 | VT, InVT, SDLoc(N), Ld->getChain(), Ld->getBasePtr(), | |||
50565 | Ld->getPointerInfo(), Ld->getOriginalAlign(), DAG); | |||
50566 | DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), Tmp.second); | |||
50567 | return Tmp.first; | |||
50568 | } | |||
50569 | } | |||
50570 | ||||
50571 | if (IsStrict) | |||
50572 | return SDValue(); | |||
50573 | ||||
50574 | if (SDValue V = combineToFPTruncExtElt(N, DAG)) | |||
50575 | return V; | |||
50576 | ||||
50577 | return SDValue(); | |||
50578 | } | |||
50579 | ||||
50580 | static bool needCarryOrOverflowFlag(SDValue Flags) { | |||
50581 | assert(Flags.getValueType() == MVT::i32 && "Unexpected VT!")(static_cast <bool> (Flags.getValueType() == MVT::i32 && "Unexpected VT!") ? void (0) : __assert_fail ("Flags.getValueType() == MVT::i32 && \"Unexpected VT!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 50581, __extension__ __PRETTY_FUNCTION__)); | |||
50582 | ||||
50583 | for (SDNode::use_iterator UI = Flags->use_begin(), UE = Flags->use_end(); | |||
50584 | UI != UE; ++UI) { | |||
50585 | SDNode *User = *UI; | |||
50586 | ||||
50587 | X86::CondCode CC; | |||
50588 | switch (User->getOpcode()) { | |||
50589 | default: | |||
50590 | // Be conservative. | |||
50591 | return true; | |||
50592 | case X86ISD::SETCC: | |||
50593 | case X86ISD::SETCC_CARRY: | |||
50594 | CC = (X86::CondCode)User->getConstantOperandVal(0); | |||
50595 | break; | |||
50596 | case X86ISD::BRCOND: | |||
50597 | CC = (X86::CondCode)User->getConstantOperandVal(2); | |||
50598 | break; | |||
50599 | case X86ISD::CMOV: | |||
50600 | CC = (X86::CondCode)User->getConstantOperandVal(2); | |||
50601 | break; | |||
50602 | } | |||
50603 | ||||
50604 | switch (CC) { | |||
50605 | default: break; | |||
50606 | case X86::COND_A: case X86::COND_AE: | |||
50607 | case X86::COND_B: case X86::COND_BE: | |||
50608 | case X86::COND_O: case X86::COND_NO: | |||
50609 | case X86::COND_G: case X86::COND_GE: | |||
50610 | case X86::COND_L: case X86::COND_LE: | |||
50611 | return true; | |||
50612 | } | |||
50613 | } | |||
50614 | ||||
50615 | return false; | |||
50616 | } | |||
50617 | ||||
50618 | static bool onlyZeroFlagUsed(SDValue Flags) { | |||
50619 | assert(Flags.getValueType() == MVT::i32 && "Unexpected VT!")(static_cast <bool> (Flags.getValueType() == MVT::i32 && "Unexpected VT!") ? void (0) : __assert_fail ("Flags.getValueType() == MVT::i32 && \"Unexpected VT!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 50619, __extension__ __PRETTY_FUNCTION__)); | |||
50620 | ||||
50621 | for (SDNode::use_iterator UI = Flags->use_begin(), UE = Flags->use_end(); | |||
50622 | UI != UE; ++UI) { | |||
50623 | SDNode *User = *UI; | |||
50624 | ||||
50625 | unsigned CCOpNo; | |||
50626 | switch (User->getOpcode()) { | |||
50627 | default: | |||
50628 | // Be conservative. | |||
50629 | return false; | |||
50630 | case X86ISD::SETCC: CCOpNo = 0; break; | |||
50631 | case X86ISD::SETCC_CARRY: CCOpNo = 0; break; | |||
50632 | case X86ISD::BRCOND: CCOpNo = 2; break; | |||
50633 | case X86ISD::CMOV: CCOpNo = 2; break; | |||
50634 | } | |||
50635 | ||||
50636 | X86::CondCode CC = (X86::CondCode)User->getConstantOperandVal(CCOpNo); | |||
50637 | if (CC != X86::COND_E && CC != X86::COND_NE) | |||
50638 | return false; | |||
50639 | } | |||
50640 | ||||
50641 | return true; | |||
50642 | } | |||
50643 | ||||
50644 | static SDValue combineCMP(SDNode *N, SelectionDAG &DAG) { | |||
50645 | // Only handle test patterns. | |||
50646 | if (!isNullConstant(N->getOperand(1))) | |||
50647 | return SDValue(); | |||
50648 | ||||
50649 | // If we have a CMP of a truncated binop, see if we can make a smaller binop | |||
50650 | // and use its flags directly. | |||
50651 | // TODO: Maybe we should try promoting compares that only use the zero flag | |||
50652 | // first if we can prove the upper bits with computeKnownBits? | |||
50653 | SDLoc dl(N); | |||
50654 | SDValue Op = N->getOperand(0); | |||
50655 | EVT VT = Op.getValueType(); | |||
50656 | ||||
50657 | // If we have a constant logical shift that's only used in a comparison | |||
50658 | // against zero turn it into an equivalent AND. This allows turning it into | |||
50659 | // a TEST instruction later. | |||
50660 | if ((Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SHL) && | |||
50661 | Op.hasOneUse() && isa<ConstantSDNode>(Op.getOperand(1)) && | |||
50662 | onlyZeroFlagUsed(SDValue(N, 0))) { | |||
50663 | unsigned BitWidth = VT.getSizeInBits(); | |||
50664 | const APInt &ShAmt = Op.getConstantOperandAPInt(1); | |||
50665 | if (ShAmt.ult(BitWidth)) { // Avoid undefined shifts. | |||
50666 | unsigned MaskBits = BitWidth - ShAmt.getZExtValue(); | |||
50667 | APInt Mask = Op.getOpcode() == ISD::SRL | |||
50668 | ? APInt::getHighBitsSet(BitWidth, MaskBits) | |||
50669 | : APInt::getLowBitsSet(BitWidth, MaskBits); | |||
50670 | if (Mask.isSignedIntN(32)) { | |||
50671 | Op = DAG.getNode(ISD::AND, dl, VT, Op.getOperand(0), | |||
50672 | DAG.getConstant(Mask, dl, VT)); | |||
50673 | return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op, | |||
50674 | DAG.getConstant(0, dl, VT)); | |||
50675 | } | |||
50676 | } | |||
50677 | } | |||
50678 | ||||
50679 | // Look for a truncate. | |||
50680 | if (Op.getOpcode() != ISD::TRUNCATE) | |||
50681 | return SDValue(); | |||
50682 | ||||
50683 | SDValue Trunc = Op; | |||
50684 | Op = Op.getOperand(0); | |||
50685 | ||||
50686 | // See if we can compare with zero against the truncation source, | |||
50687 | // which should help using the Z flag from many ops. Only do this for | |||
50688 | // i32 truncated op to prevent partial-reg compares of promoted ops. | |||
50689 | EVT OpVT = Op.getValueType(); | |||
50690 | APInt UpperBits = | |||
50691 | APInt::getBitsSetFrom(OpVT.getSizeInBits(), VT.getSizeInBits()); | |||
50692 | if (OpVT == MVT::i32 && DAG.MaskedValueIsZero(Op, UpperBits) && | |||
50693 | onlyZeroFlagUsed(SDValue(N, 0))) { | |||
50694 | return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op, | |||
50695 | DAG.getConstant(0, dl, OpVT)); | |||
50696 | } | |||
50697 | ||||
50698 | // After this the truncate and arithmetic op must have a single use. | |||
50699 | if (!Trunc.hasOneUse() || !Op.hasOneUse()) | |||
50700 | return SDValue(); | |||
50701 | ||||
50702 | unsigned NewOpc; | |||
50703 | switch (Op.getOpcode()) { | |||
50704 | default: return SDValue(); | |||
50705 | case ISD::AND: | |||
50706 | // Skip and with constant. We have special handling for and with immediate | |||
50707 | // during isel to generate test instructions. | |||
50708 | if (isa<ConstantSDNode>(Op.getOperand(1))) | |||
50709 | return SDValue(); | |||
50710 | NewOpc = X86ISD::AND; | |||
50711 | break; | |||
50712 | case ISD::OR: NewOpc = X86ISD::OR; break; | |||
50713 | case ISD::XOR: NewOpc = X86ISD::XOR; break; | |||
50714 | case ISD::ADD: | |||
50715 | // If the carry or overflow flag is used, we can't truncate. | |||
50716 | if (needCarryOrOverflowFlag(SDValue(N, 0))) | |||
50717 | return SDValue(); | |||
50718 | NewOpc = X86ISD::ADD; | |||
50719 | break; | |||
50720 | case ISD::SUB: | |||
50721 | // If the carry or overflow flag is used, we can't truncate. | |||
50722 | if (needCarryOrOverflowFlag(SDValue(N, 0))) | |||
50723 | return SDValue(); | |||
50724 | NewOpc = X86ISD::SUB; | |||
50725 | break; | |||
50726 | } | |||
50727 | ||||
50728 | // We found an op we can narrow. Truncate its inputs. | |||
50729 | SDValue Op0 = DAG.getNode(ISD::TRUNCATE, dl, VT, Op.getOperand(0)); | |||
50730 | SDValue Op1 = DAG.getNode(ISD::TRUNCATE, dl, VT, Op.getOperand(1)); | |||
50731 | ||||
50732 | // Use a X86 specific opcode to avoid DAG combine messing with it. | |||
50733 | SDVTList VTs = DAG.getVTList(VT, MVT::i32); | |||
50734 | Op = DAG.getNode(NewOpc, dl, VTs, Op0, Op1); | |||
50735 | ||||
50736 | // For AND, keep a CMP so that we can match the test pattern. | |||
50737 | if (NewOpc == X86ISD::AND) | |||
50738 | return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op, | |||
50739 | DAG.getConstant(0, dl, VT)); | |||
50740 | ||||
50741 | // Return the flags. | |||
50742 | return Op.getValue(1); | |||
50743 | } | |||
50744 | ||||
50745 | static SDValue combineX86AddSub(SDNode *N, SelectionDAG &DAG, | |||
50746 | TargetLowering::DAGCombinerInfo &DCI) { | |||
50747 | assert((X86ISD::ADD == N->getOpcode() || X86ISD::SUB == N->getOpcode()) &&(static_cast <bool> ((X86ISD::ADD == N->getOpcode() || X86ISD::SUB == N->getOpcode()) && "Expected X86ISD::ADD or X86ISD::SUB" ) ? void (0) : __assert_fail ("(X86ISD::ADD == N->getOpcode() || X86ISD::SUB == N->getOpcode()) && \"Expected X86ISD::ADD or X86ISD::SUB\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 50748, __extension__ __PRETTY_FUNCTION__)) | |||
50748 | "Expected X86ISD::ADD or X86ISD::SUB")(static_cast <bool> ((X86ISD::ADD == N->getOpcode() || X86ISD::SUB == N->getOpcode()) && "Expected X86ISD::ADD or X86ISD::SUB" ) ? void (0) : __assert_fail ("(X86ISD::ADD == N->getOpcode() || X86ISD::SUB == N->getOpcode()) && \"Expected X86ISD::ADD or X86ISD::SUB\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 50748, __extension__ __PRETTY_FUNCTION__)); | |||
50749 | ||||
50750 | SDLoc DL(N); | |||
50751 | SDValue LHS = N->getOperand(0); | |||
50752 | SDValue RHS = N->getOperand(1); | |||
50753 | MVT VT = LHS.getSimpleValueType(); | |||
50754 | unsigned GenericOpc = X86ISD::ADD == N->getOpcode() ? ISD::ADD : ISD::SUB; | |||
50755 | ||||
50756 | // If we don't use the flag result, simplify back to a generic ADD/SUB. | |||
50757 | if (!N->hasAnyUseOfValue(1)) { | |||
50758 | SDValue Res = DAG.getNode(GenericOpc, DL, VT, LHS, RHS); | |||
50759 | return DAG.getMergeValues({Res, DAG.getConstant(0, DL, MVT::i32)}, DL); | |||
50760 | } | |||
50761 | ||||
50762 | // Fold any similar generic ADD/SUB opcodes to reuse this node. | |||
50763 | auto MatchGeneric = [&](SDValue N0, SDValue N1, bool Negate) { | |||
50764 | SDValue Ops[] = {N0, N1}; | |||
50765 | SDVTList VTs = DAG.getVTList(N->getValueType(0)); | |||
50766 | if (SDNode *GenericAddSub = DAG.getNodeIfExists(GenericOpc, VTs, Ops)) { | |||
50767 | SDValue Op(N, 0); | |||
50768 | if (Negate) | |||
50769 | Op = DAG.getNode(ISD::SUB, DL, VT, DAG.getConstant(0, DL, VT), Op); | |||
50770 | DCI.CombineTo(GenericAddSub, Op); | |||
50771 | } | |||
50772 | }; | |||
50773 | MatchGeneric(LHS, RHS, false); | |||
50774 | MatchGeneric(RHS, LHS, X86ISD::SUB == N->getOpcode()); | |||
50775 | ||||
50776 | return SDValue(); | |||
50777 | } | |||
50778 | ||||
50779 | static SDValue combineSBB(SDNode *N, SelectionDAG &DAG) { | |||
50780 | if (SDValue Flags = combineCarryThroughADD(N->getOperand(2), DAG)) { | |||
50781 | MVT VT = N->getSimpleValueType(0); | |||
50782 | SDVTList VTs = DAG.getVTList(VT, MVT::i32); | |||
50783 | return DAG.getNode(X86ISD::SBB, SDLoc(N), VTs, | |||
50784 | N->getOperand(0), N->getOperand(1), | |||
50785 | Flags); | |||
50786 | } | |||
50787 | ||||
50788 | // Fold SBB(SUB(X,Y),0,Carry) -> SBB(X,Y,Carry) | |||
50789 | // iff the flag result is dead. | |||
50790 | SDValue Op0 = N->getOperand(0); | |||
50791 | SDValue Op1 = N->getOperand(1); | |||
50792 | if (Op0.getOpcode() == ISD::SUB && isNullConstant(Op1) && | |||
50793 | !N->hasAnyUseOfValue(1)) | |||
50794 | return DAG.getNode(X86ISD::SBB, SDLoc(N), N->getVTList(), Op0.getOperand(0), | |||
50795 | Op0.getOperand(1), N->getOperand(2)); | |||
50796 | ||||
50797 | return SDValue(); | |||
50798 | } | |||
50799 | ||||
50800 | // Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS | |||
50801 | static SDValue combineADC(SDNode *N, SelectionDAG &DAG, | |||
50802 | TargetLowering::DAGCombinerInfo &DCI) { | |||
50803 | // If the LHS and RHS of the ADC node are zero, then it can't overflow and | |||
50804 | // the result is either zero or one (depending on the input carry bit). | |||
50805 | // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1. | |||
50806 | if (X86::isZeroNode(N->getOperand(0)) && | |||
50807 | X86::isZeroNode(N->getOperand(1)) && | |||
50808 | // We don't have a good way to replace an EFLAGS use, so only do this when | |||
50809 | // dead right now. | |||
50810 | SDValue(N, 1).use_empty()) { | |||
50811 | SDLoc DL(N); | |||
50812 | EVT VT = N->getValueType(0); | |||
50813 | SDValue CarryOut = DAG.getConstant(0, DL, N->getValueType(1)); | |||
50814 | SDValue Res1 = | |||
50815 | DAG.getNode(ISD::AND, DL, VT, | |||
50816 | DAG.getNode(X86ISD::SETCC_CARRY, DL, VT, | |||
50817 | DAG.getTargetConstant(X86::COND_B, DL, MVT::i8), | |||
50818 | N->getOperand(2)), | |||
50819 | DAG.getConstant(1, DL, VT)); | |||
50820 | return DCI.CombineTo(N, Res1, CarryOut); | |||
50821 | } | |||
50822 | ||||
50823 | if (SDValue Flags = combineCarryThroughADD(N->getOperand(2), DAG)) { | |||
50824 | MVT VT = N->getSimpleValueType(0); | |||
50825 | SDVTList VTs = DAG.getVTList(VT, MVT::i32); | |||
50826 | return DAG.getNode(X86ISD::ADC, SDLoc(N), VTs, | |||
50827 | N->getOperand(0), N->getOperand(1), | |||
50828 | Flags); | |||
50829 | } | |||
50830 | ||||
50831 | return SDValue(); | |||
50832 | } | |||
50833 | ||||
50834 | /// If this is an add or subtract where one operand is produced by a cmp+setcc, | |||
50835 | /// then try to convert it to an ADC or SBB. This replaces TEST+SET+{ADD/SUB} | |||
50836 | /// with CMP+{ADC, SBB}. | |||
50837 | static SDValue combineAddOrSubToADCOrSBB(SDNode *N, SelectionDAG &DAG) { | |||
50838 | bool IsSub = N->getOpcode() == ISD::SUB; | |||
50839 | SDValue X = N->getOperand(0); | |||
50840 | SDValue Y = N->getOperand(1); | |||
50841 | ||||
50842 | // If this is an add, canonicalize a zext operand to the RHS. | |||
50843 | // TODO: Incomplete? What if both sides are zexts? | |||
50844 | if (!IsSub && X.getOpcode() == ISD::ZERO_EXTEND && | |||
50845 | Y.getOpcode() != ISD::ZERO_EXTEND) | |||
50846 | std::swap(X, Y); | |||
50847 | ||||
50848 | // Look through a one-use zext. | |||
50849 | bool PeekedThroughZext = false; | |||
50850 | if (Y.getOpcode() == ISD::ZERO_EXTEND && Y.hasOneUse()) { | |||
50851 | Y = Y.getOperand(0); | |||
50852 | PeekedThroughZext = true; | |||
50853 | } | |||
50854 | ||||
50855 | // If this is an add, canonicalize a setcc operand to the RHS. | |||
50856 | // TODO: Incomplete? What if both sides are setcc? | |||
50857 | // TODO: Should we allow peeking through a zext of the other operand? | |||
50858 | if (!IsSub && !PeekedThroughZext && X.getOpcode() == X86ISD::SETCC && | |||
50859 | Y.getOpcode() != X86ISD::SETCC) | |||
50860 | std::swap(X, Y); | |||
50861 | ||||
50862 | if (Y.getOpcode() != X86ISD::SETCC || !Y.hasOneUse()) | |||
50863 | return SDValue(); | |||
50864 | ||||
50865 | SDLoc DL(N); | |||
50866 | EVT VT = N->getValueType(0); | |||
50867 | X86::CondCode CC = (X86::CondCode)Y.getConstantOperandVal(0); | |||
50868 | ||||
50869 | // If X is -1 or 0, then we have an opportunity to avoid constants required in | |||
50870 | // the general case below. | |||
50871 | auto *ConstantX = dyn_cast<ConstantSDNode>(X); | |||
50872 | if (ConstantX) { | |||
50873 | if ((!IsSub && CC == X86::COND_AE && ConstantX->isAllOnes()) || | |||
50874 | (IsSub && CC == X86::COND_B && ConstantX->isZero())) { | |||
50875 | // This is a complicated way to get -1 or 0 from the carry flag: | |||
50876 | // -1 + SETAE --> -1 + (!CF) --> CF ? -1 : 0 --> SBB %eax, %eax | |||
50877 | // 0 - SETB --> 0 - (CF) --> CF ? -1 : 0 --> SBB %eax, %eax | |||
50878 | return DAG.getNode(X86ISD::SETCC_CARRY, DL, VT, | |||
50879 | DAG.getTargetConstant(X86::COND_B, DL, MVT::i8), | |||
50880 | Y.getOperand(1)); | |||
50881 | } | |||
50882 | ||||
50883 | if ((!IsSub && CC == X86::COND_BE && ConstantX->isAllOnes()) || | |||
50884 | (IsSub && CC == X86::COND_A && ConstantX->isZero())) { | |||
50885 | SDValue EFLAGS = Y->getOperand(1); | |||
50886 | if (EFLAGS.getOpcode() == X86ISD::SUB && EFLAGS.hasOneUse() && | |||
50887 | EFLAGS.getValueType().isInteger() && | |||
50888 | !isa<ConstantSDNode>(EFLAGS.getOperand(1))) { | |||
50889 | // Swap the operands of a SUB, and we have the same pattern as above. | |||
50890 | // -1 + SETBE (SUB A, B) --> -1 + SETAE (SUB B, A) --> SUB + SBB | |||
50891 | // 0 - SETA (SUB A, B) --> 0 - SETB (SUB B, A) --> SUB + SBB | |||
50892 | SDValue NewSub = DAG.getNode( | |||
50893 | X86ISD::SUB, SDLoc(EFLAGS), EFLAGS.getNode()->getVTList(), | |||
50894 | EFLAGS.getOperand(1), EFLAGS.getOperand(0)); | |||
50895 | SDValue NewEFLAGS = SDValue(NewSub.getNode(), EFLAGS.getResNo()); | |||
50896 | return DAG.getNode(X86ISD::SETCC_CARRY, DL, VT, | |||
50897 | DAG.getTargetConstant(X86::COND_B, DL, MVT::i8), | |||
50898 | NewEFLAGS); | |||
50899 | } | |||
50900 | } | |||
50901 | } | |||
50902 | ||||
50903 | if (CC == X86::COND_B) { | |||
50904 | // X + SETB Z --> adc X, 0 | |||
50905 | // X - SETB Z --> sbb X, 0 | |||
50906 | return DAG.getNode(IsSub ? X86ISD::SBB : X86ISD::ADC, DL, | |||
50907 | DAG.getVTList(VT, MVT::i32), X, | |||
50908 | DAG.getConstant(0, DL, VT), Y.getOperand(1)); | |||
50909 | } | |||
50910 | ||||
50911 | if (CC == X86::COND_A) { | |||
50912 | SDValue EFLAGS = Y.getOperand(1); | |||
50913 | // Try to convert COND_A into COND_B in an attempt to facilitate | |||
50914 | // materializing "setb reg". | |||
50915 | // | |||
50916 | // Do not flip "e > c", where "c" is a constant, because Cmp instruction | |||
50917 | // cannot take an immediate as its first operand. | |||
50918 | // | |||
50919 | if (EFLAGS.getOpcode() == X86ISD::SUB && EFLAGS.getNode()->hasOneUse() && | |||
50920 | EFLAGS.getValueType().isInteger() && | |||
50921 | !isa<ConstantSDNode>(EFLAGS.getOperand(1))) { | |||
50922 | SDValue NewSub = DAG.getNode(X86ISD::SUB, SDLoc(EFLAGS), | |||
50923 | EFLAGS.getNode()->getVTList(), | |||
50924 | EFLAGS.getOperand(1), EFLAGS.getOperand(0)); | |||
50925 | SDValue NewEFLAGS = NewSub.getValue(EFLAGS.getResNo()); | |||
50926 | return DAG.getNode(IsSub ? X86ISD::SBB : X86ISD::ADC, DL, | |||
50927 | DAG.getVTList(VT, MVT::i32), X, | |||
50928 | DAG.getConstant(0, DL, VT), NewEFLAGS); | |||
50929 | } | |||
50930 | } | |||
50931 | ||||
50932 | if (CC == X86::COND_AE) { | |||
50933 | // X + SETAE --> sbb X, -1 | |||
50934 | // X - SETAE --> adc X, -1 | |||
50935 | return DAG.getNode(IsSub ? X86ISD::ADC : X86ISD::SBB, DL, | |||
50936 | DAG.getVTList(VT, MVT::i32), X, | |||
50937 | DAG.getConstant(-1, DL, VT), Y.getOperand(1)); | |||
50938 | } | |||
50939 | ||||
50940 | if (CC == X86::COND_BE) { | |||
50941 | // X + SETBE --> sbb X, -1 | |||
50942 | // X - SETBE --> adc X, -1 | |||
50943 | SDValue EFLAGS = Y.getOperand(1); | |||
50944 | // Try to convert COND_BE into COND_AE in an attempt to facilitate | |||
50945 | // materializing "setae reg". | |||
50946 | // | |||
50947 | // Do not flip "e <= c", where "c" is a constant, because Cmp instruction | |||
50948 | // cannot take an immediate as its first operand. | |||
50949 | // | |||
50950 | if (EFLAGS.getOpcode() == X86ISD::SUB && EFLAGS.getNode()->hasOneUse() && | |||
50951 | EFLAGS.getValueType().isInteger() && | |||
50952 | !isa<ConstantSDNode>(EFLAGS.getOperand(1))) { | |||
50953 | SDValue NewSub = DAG.getNode( | |||
50954 | X86ISD::SUB, SDLoc(EFLAGS), EFLAGS.getNode()->getVTList(), | |||
50955 | EFLAGS.getOperand(1), EFLAGS.getOperand(0)); | |||
50956 | SDValue NewEFLAGS = NewSub.getValue(EFLAGS.getResNo()); | |||
50957 | return DAG.getNode(IsSub ? X86ISD::ADC : X86ISD::SBB, DL, | |||
50958 | DAG.getVTList(VT, MVT::i32), X, | |||
50959 | DAG.getConstant(-1, DL, VT), NewEFLAGS); | |||
50960 | } | |||
50961 | } | |||
50962 | ||||
50963 | if (CC != X86::COND_E && CC != X86::COND_NE) | |||
50964 | return SDValue(); | |||
50965 | ||||
50966 | SDValue Cmp = Y.getOperand(1); | |||
50967 | if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() || | |||
50968 | !X86::isZeroNode(Cmp.getOperand(1)) || | |||
50969 | !Cmp.getOperand(0).getValueType().isInteger()) | |||
50970 | return SDValue(); | |||
50971 | ||||
50972 | SDValue Z = Cmp.getOperand(0); | |||
50973 | EVT ZVT = Z.getValueType(); | |||
50974 | ||||
50975 | // If X is -1 or 0, then we have an opportunity to avoid constants required in | |||
50976 | // the general case below. | |||
50977 | if (ConstantX) { | |||
50978 | // 'neg' sets the carry flag when Z != 0, so create 0 or -1 using 'sbb' with | |||
50979 | // fake operands: | |||
50980 | // 0 - (Z != 0) --> sbb %eax, %eax, (neg Z) | |||
50981 | // -1 + (Z == 0) --> sbb %eax, %eax, (neg Z) | |||
50982 | if ((IsSub && CC == X86::COND_NE && ConstantX->isZero()) || | |||
50983 | (!IsSub && CC == X86::COND_E && ConstantX->isAllOnes())) { | |||
50984 | SDValue Zero = DAG.getConstant(0, DL, ZVT); | |||
50985 | SDVTList X86SubVTs = DAG.getVTList(ZVT, MVT::i32); | |||
50986 | SDValue Neg = DAG.getNode(X86ISD::SUB, DL, X86SubVTs, Zero, Z); | |||
50987 | return DAG.getNode(X86ISD::SETCC_CARRY, DL, VT, | |||
50988 | DAG.getTargetConstant(X86::COND_B, DL, MVT::i8), | |||
50989 | SDValue(Neg.getNode(), 1)); | |||
50990 | } | |||
50991 | ||||
50992 | // cmp with 1 sets the carry flag when Z == 0, so create 0 or -1 using 'sbb' | |||
50993 | // with fake operands: | |||
50994 | // 0 - (Z == 0) --> sbb %eax, %eax, (cmp Z, 1) | |||
50995 | // -1 + (Z != 0) --> sbb %eax, %eax, (cmp Z, 1) | |||
50996 | if ((IsSub && CC == X86::COND_E && ConstantX->isZero()) || | |||
50997 | (!IsSub && CC == X86::COND_NE && ConstantX->isAllOnes())) { | |||
50998 | SDValue One = DAG.getConstant(1, DL, ZVT); | |||
50999 | SDVTList X86SubVTs = DAG.getVTList(ZVT, MVT::i32); | |||
51000 | SDValue Cmp1 = DAG.getNode(X86ISD::SUB, DL, X86SubVTs, Z, One); | |||
51001 | return DAG.getNode(X86ISD::SETCC_CARRY, DL, VT, | |||
51002 | DAG.getTargetConstant(X86::COND_B, DL, MVT::i8), | |||
51003 | Cmp1.getValue(1)); | |||
51004 | } | |||
51005 | } | |||
51006 | ||||
51007 | // (cmp Z, 1) sets the carry flag if Z is 0. | |||
51008 | SDValue One = DAG.getConstant(1, DL, ZVT); | |||
51009 | SDVTList X86SubVTs = DAG.getVTList(ZVT, MVT::i32); | |||
51010 | SDValue Cmp1 = DAG.getNode(X86ISD::SUB, DL, X86SubVTs, Z, One); | |||
51011 | ||||
51012 | // Add the flags type for ADC/SBB nodes. | |||
51013 | SDVTList VTs = DAG.getVTList(VT, MVT::i32); | |||
51014 | ||||
51015 | // X - (Z != 0) --> sub X, (zext(setne Z, 0)) --> adc X, -1, (cmp Z, 1) | |||
51016 | // X + (Z != 0) --> add X, (zext(setne Z, 0)) --> sbb X, -1, (cmp Z, 1) | |||
51017 | if (CC == X86::COND_NE) | |||
51018 | return DAG.getNode(IsSub ? X86ISD::ADC : X86ISD::SBB, DL, VTs, X, | |||
51019 | DAG.getConstant(-1ULL, DL, VT), Cmp1.getValue(1)); | |||
51020 | ||||
51021 | // X - (Z == 0) --> sub X, (zext(sete Z, 0)) --> sbb X, 0, (cmp Z, 1) | |||
51022 | // X + (Z == 0) --> add X, (zext(sete Z, 0)) --> adc X, 0, (cmp Z, 1) | |||
51023 | return DAG.getNode(IsSub ? X86ISD::SBB : X86ISD::ADC, DL, VTs, X, | |||
51024 | DAG.getConstant(0, DL, VT), Cmp1.getValue(1)); | |||
51025 | } | |||
51026 | ||||
51027 | static SDValue matchPMADDWD(SelectionDAG &DAG, SDValue Op0, SDValue Op1, | |||
51028 | const SDLoc &DL, EVT VT, | |||
51029 | const X86Subtarget &Subtarget) { | |||
51030 | // Example of pattern we try to detect: | |||
51031 | // t := (v8i32 mul (sext (v8i16 x0), (sext (v8i16 x1)))) | |||
51032 | //(add (build_vector (extract_elt t, 0), | |||
51033 | // (extract_elt t, 2), | |||
51034 | // (extract_elt t, 4), | |||
51035 | // (extract_elt t, 6)), | |||
51036 | // (build_vector (extract_elt t, 1), | |||
51037 | // (extract_elt t, 3), | |||
51038 | // (extract_elt t, 5), | |||
51039 | // (extract_elt t, 7))) | |||
51040 | ||||
51041 | if (!Subtarget.hasSSE2()) | |||
51042 | return SDValue(); | |||
51043 | ||||
51044 | if (Op0.getOpcode() != ISD::BUILD_VECTOR || | |||
51045 | Op1.getOpcode() != ISD::BUILD_VECTOR) | |||
51046 | return SDValue(); | |||
51047 | ||||
51048 | if (!VT.isVector() || VT.getVectorElementType() != MVT::i32 || | |||
51049 | VT.getVectorNumElements() < 4 || | |||
51050 | !isPowerOf2_32(VT.getVectorNumElements())) | |||
51051 | return SDValue(); | |||
51052 | ||||
51053 | // Check if one of Op0,Op1 is of the form: | |||
51054 | // (build_vector (extract_elt Mul, 0), | |||
51055 | // (extract_elt Mul, 2), | |||
51056 | // (extract_elt Mul, 4), | |||
51057 | // ... | |||
51058 | // the other is of the form: | |||
51059 | // (build_vector (extract_elt Mul, 1), | |||
51060 | // (extract_elt Mul, 3), | |||
51061 | // (extract_elt Mul, 5), | |||
51062 | // ... | |||
51063 | // and identify Mul. | |||
51064 | SDValue Mul; | |||
51065 | for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; i += 2) { | |||
51066 | SDValue Op0L = Op0->getOperand(i), Op1L = Op1->getOperand(i), | |||
51067 | Op0H = Op0->getOperand(i + 1), Op1H = Op1->getOperand(i + 1); | |||
51068 | // TODO: Be more tolerant to undefs. | |||
51069 | if (Op0L.getOpcode() != ISD::EXTRACT_VECTOR_ELT || | |||
51070 | Op1L.getOpcode() != ISD::EXTRACT_VECTOR_ELT || | |||
51071 | Op0H.getOpcode() != ISD::EXTRACT_VECTOR_ELT || | |||
51072 | Op1H.getOpcode() != ISD::EXTRACT_VECTOR_ELT) | |||
51073 | return SDValue(); | |||
51074 | auto *Const0L = dyn_cast<ConstantSDNode>(Op0L->getOperand(1)); | |||
51075 | auto *Const1L = dyn_cast<ConstantSDNode>(Op1L->getOperand(1)); | |||
51076 | auto *Const0H = dyn_cast<ConstantSDNode>(Op0H->getOperand(1)); | |||
51077 | auto *Const1H = dyn_cast<ConstantSDNode>(Op1H->getOperand(1)); | |||
51078 | if (!Const0L || !Const1L || !Const0H || !Const1H) | |||
51079 | return SDValue(); | |||
51080 | unsigned Idx0L = Const0L->getZExtValue(), Idx1L = Const1L->getZExtValue(), | |||
51081 | Idx0H = Const0H->getZExtValue(), Idx1H = Const1H->getZExtValue(); | |||
51082 | // Commutativity of mul allows factors of a product to reorder. | |||
51083 | if (Idx0L > Idx1L) | |||
51084 | std::swap(Idx0L, Idx1L); | |||
51085 | if (Idx0H > Idx1H) | |||
51086 | std::swap(Idx0H, Idx1H); | |||
51087 | // Commutativity of add allows pairs of factors to reorder. | |||
51088 | if (Idx0L > Idx0H) { | |||
51089 | std::swap(Idx0L, Idx0H); | |||
51090 | std::swap(Idx1L, Idx1H); | |||
51091 | } | |||
51092 | if (Idx0L != 2 * i || Idx1L != 2 * i + 1 || Idx0H != 2 * i + 2 || | |||
51093 | Idx1H != 2 * i + 3) | |||
51094 | return SDValue(); | |||
51095 | if (!Mul) { | |||
51096 | // First time an extract_elt's source vector is visited. Must be a MUL | |||
51097 | // with 2X number of vector elements than the BUILD_VECTOR. | |||
51098 | // Both extracts must be from same MUL. | |||
51099 | Mul = Op0L->getOperand(0); | |||
51100 | if (Mul->getOpcode() != ISD::MUL || | |||
51101 | Mul.getValueType().getVectorNumElements() != 2 * e) | |||
51102 | return SDValue(); | |||
51103 | } | |||
51104 | // Check that the extract is from the same MUL previously seen. | |||
51105 | if (Mul != Op0L->getOperand(0) || Mul != Op1L->getOperand(0) || | |||
51106 | Mul != Op0H->getOperand(0) || Mul != Op1H->getOperand(0)) | |||
51107 | return SDValue(); | |||
51108 | } | |||
51109 | ||||
51110 | // Check if the Mul source can be safely shrunk. | |||
51111 | ShrinkMode Mode; | |||
51112 | if (!canReduceVMulWidth(Mul.getNode(), DAG, Mode) || | |||
51113 | Mode == ShrinkMode::MULU16) | |||
51114 | return SDValue(); | |||
51115 | ||||
51116 | EVT TruncVT = EVT::getVectorVT(*DAG.getContext(), MVT::i16, | |||
51117 | VT.getVectorNumElements() * 2); | |||
51118 | SDValue N0 = DAG.getNode(ISD::TRUNCATE, DL, TruncVT, Mul.getOperand(0)); | |||
51119 | SDValue N1 = DAG.getNode(ISD::TRUNCATE, DL, TruncVT, Mul.getOperand(1)); | |||
51120 | ||||
51121 | auto PMADDBuilder = [](SelectionDAG &DAG, const SDLoc &DL, | |||
51122 | ArrayRef<SDValue> Ops) { | |||
51123 | EVT InVT = Ops[0].getValueType(); | |||
51124 | assert(InVT == Ops[1].getValueType() && "Operands' types mismatch")(static_cast <bool> (InVT == Ops[1].getValueType() && "Operands' types mismatch") ? void (0) : __assert_fail ("InVT == Ops[1].getValueType() && \"Operands' types mismatch\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 51124, __extension__ __PRETTY_FUNCTION__)); | |||
51125 | EVT ResVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, | |||
51126 | InVT.getVectorNumElements() / 2); | |||
51127 | return DAG.getNode(X86ISD::VPMADDWD, DL, ResVT, Ops[0], Ops[1]); | |||
51128 | }; | |||
51129 | return SplitOpsAndApply(DAG, Subtarget, DL, VT, { N0, N1 }, PMADDBuilder); | |||
51130 | } | |||
51131 | ||||
51132 | // Attempt to turn this pattern into PMADDWD. | |||
51133 | // (add (mul (sext (build_vector)), (sext (build_vector))), | |||
51134 | // (mul (sext (build_vector)), (sext (build_vector))) | |||
51135 | static SDValue matchPMADDWD_2(SelectionDAG &DAG, SDValue N0, SDValue N1, | |||
51136 | const SDLoc &DL, EVT VT, | |||
51137 | const X86Subtarget &Subtarget) { | |||
51138 | if (!Subtarget.hasSSE2()) | |||
51139 | return SDValue(); | |||
51140 | ||||
51141 | if (N0.getOpcode() != ISD::MUL || N1.getOpcode() != ISD::MUL) | |||
51142 | return SDValue(); | |||
51143 | ||||
51144 | if (!VT.isVector() || VT.getVectorElementType() != MVT::i32 || | |||
51145 | VT.getVectorNumElements() < 4 || | |||
51146 | !isPowerOf2_32(VT.getVectorNumElements())) | |||
51147 | return SDValue(); | |||
51148 | ||||
51149 | SDValue N00 = N0.getOperand(0); | |||
51150 | SDValue N01 = N0.getOperand(1); | |||
51151 | SDValue N10 = N1.getOperand(0); | |||
51152 | SDValue N11 = N1.getOperand(1); | |||
51153 | ||||
51154 | // All inputs need to be sign extends. | |||
51155 | // TODO: Support ZERO_EXTEND from known positive? | |||
51156 | if (N00.getOpcode() != ISD::SIGN_EXTEND || | |||
51157 | N01.getOpcode() != ISD::SIGN_EXTEND || | |||
51158 | N10.getOpcode() != ISD::SIGN_EXTEND || | |||
51159 | N11.getOpcode() != ISD::SIGN_EXTEND) | |||
51160 | return SDValue(); | |||
51161 | ||||
51162 | // Peek through the extends. | |||
51163 | N00 = N00.getOperand(0); | |||
51164 | N01 = N01.getOperand(0); | |||
51165 | N10 = N10.getOperand(0); | |||
51166 | N11 = N11.getOperand(0); | |||
51167 | ||||
51168 | // Must be extending from vXi16. | |||
51169 | EVT InVT = N00.getValueType(); | |||
51170 | if (InVT.getVectorElementType() != MVT::i16 || N01.getValueType() != InVT || | |||
51171 | N10.getValueType() != InVT || N11.getValueType() != InVT) | |||
51172 | return SDValue(); | |||
51173 | ||||
51174 | // All inputs should be build_vectors. | |||
51175 | if (N00.getOpcode() != ISD::BUILD_VECTOR || | |||
51176 | N01.getOpcode() != ISD::BUILD_VECTOR || | |||
51177 | N10.getOpcode() != ISD::BUILD_VECTOR || | |||
51178 | N11.getOpcode() != ISD::BUILD_VECTOR) | |||
51179 | return SDValue(); | |||
51180 | ||||
51181 | // For each element, we need to ensure we have an odd element from one vector | |||
51182 | // multiplied by the odd element of another vector and the even element from | |||
51183 | // one of the same vectors being multiplied by the even element from the | |||
51184 | // other vector. So we need to make sure for each element i, this operator | |||
51185 | // is being performed: | |||
51186 | // A[2 * i] * B[2 * i] + A[2 * i + 1] * B[2 * i + 1] | |||
51187 | SDValue In0, In1; | |||
51188 | for (unsigned i = 0; i != N00.getNumOperands(); ++i) { | |||
51189 | SDValue N00Elt = N00.getOperand(i); | |||
51190 | SDValue N01Elt = N01.getOperand(i); | |||
51191 | SDValue N10Elt = N10.getOperand(i); | |||
51192 | SDValue N11Elt = N11.getOperand(i); | |||
51193 | // TODO: Be more tolerant to undefs. | |||
51194 | if (N00Elt.getOpcode() != ISD::EXTRACT_VECTOR_ELT || | |||
51195 | N01Elt.getOpcode() != ISD::EXTRACT_VECTOR_ELT || | |||
51196 | N10Elt.getOpcode() != ISD::EXTRACT_VECTOR_ELT || | |||
51197 | N11Elt.getOpcode() != ISD::EXTRACT_VECTOR_ELT) | |||
51198 | return SDValue(); | |||
51199 | auto *ConstN00Elt = dyn_cast<ConstantSDNode>(N00Elt.getOperand(1)); | |||
51200 | auto *ConstN01Elt = dyn_cast<ConstantSDNode>(N01Elt.getOperand(1)); | |||
51201 | auto *ConstN10Elt = dyn_cast<ConstantSDNode>(N10Elt.getOperand(1)); | |||
51202 | auto *ConstN11Elt = dyn_cast<ConstantSDNode>(N11Elt.getOperand(1)); | |||
51203 | if (!ConstN00Elt || !ConstN01Elt || !ConstN10Elt || !ConstN11Elt) | |||
51204 | return SDValue(); | |||
51205 | unsigned IdxN00 = ConstN00Elt->getZExtValue(); | |||
51206 | unsigned IdxN01 = ConstN01Elt->getZExtValue(); | |||
51207 | unsigned IdxN10 = ConstN10Elt->getZExtValue(); | |||
51208 | unsigned IdxN11 = ConstN11Elt->getZExtValue(); | |||
51209 | // Add is commutative so indices can be reordered. | |||
51210 | if (IdxN00 > IdxN10) { | |||
51211 | std::swap(IdxN00, IdxN10); | |||
51212 | std::swap(IdxN01, IdxN11); | |||
51213 | } | |||
51214 | // N0 indices be the even element. N1 indices must be the next odd element. | |||
51215 | if (IdxN00 != 2 * i || IdxN10 != 2 * i + 1 || | |||
51216 | IdxN01 != 2 * i || IdxN11 != 2 * i + 1) | |||
51217 | return SDValue(); | |||
51218 | SDValue N00In = N00Elt.getOperand(0); | |||
51219 | SDValue N01In = N01Elt.getOperand(0); | |||
51220 | SDValue N10In = N10Elt.getOperand(0); | |||
51221 | SDValue N11In = N11Elt.getOperand(0); | |||
51222 | ||||
51223 | // First time we find an input capture it. | |||
51224 | if (!In0) { | |||
51225 | In0 = N00In; | |||
51226 | In1 = N01In; | |||
51227 | ||||
51228 | // The input vectors must be at least as wide as the output. | |||
51229 | // If they are larger than the output, we extract subvector below. | |||
51230 | if (In0.getValueSizeInBits() < VT.getSizeInBits() || | |||
51231 | In1.getValueSizeInBits() < VT.getSizeInBits()) | |||
51232 | return SDValue(); | |||
51233 | } | |||
51234 | // Mul is commutative so the input vectors can be in any order. | |||
51235 | // Canonicalize to make the compares easier. | |||
51236 | if (In0 != N00In) | |||
51237 | std::swap(N00In, N01In); | |||
51238 | if (In0 != N10In) | |||
51239 | std::swap(N10In, N11In); | |||
51240 | if (In0 != N00In || In1 != N01In || In0 != N10In || In1 != N11In) | |||
51241 | return SDValue(); | |||
51242 | } | |||
51243 | ||||
51244 | auto PMADDBuilder = [](SelectionDAG &DAG, const SDLoc &DL, | |||
51245 | ArrayRef<SDValue> Ops) { | |||
51246 | EVT OpVT = Ops[0].getValueType(); | |||
51247 | assert(OpVT.getScalarType() == MVT::i16 &&(static_cast <bool> (OpVT.getScalarType() == MVT::i16 && "Unexpected scalar element type") ? void (0) : __assert_fail ("OpVT.getScalarType() == MVT::i16 && \"Unexpected scalar element type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 51248, __extension__ __PRETTY_FUNCTION__)) | |||
51248 | "Unexpected scalar element type")(static_cast <bool> (OpVT.getScalarType() == MVT::i16 && "Unexpected scalar element type") ? void (0) : __assert_fail ("OpVT.getScalarType() == MVT::i16 && \"Unexpected scalar element type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 51248, __extension__ __PRETTY_FUNCTION__)); | |||
51249 | assert(OpVT == Ops[1].getValueType() && "Operands' types mismatch")(static_cast <bool> (OpVT == Ops[1].getValueType() && "Operands' types mismatch") ? void (0) : __assert_fail ("OpVT == Ops[1].getValueType() && \"Operands' types mismatch\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 51249, __extension__ __PRETTY_FUNCTION__)); | |||
51250 | EVT ResVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32, | |||
51251 | OpVT.getVectorNumElements() / 2); | |||
51252 | return DAG.getNode(X86ISD::VPMADDWD, DL, ResVT, Ops[0], Ops[1]); | |||
51253 | }; | |||
51254 | ||||
51255 | // If the output is narrower than an input, extract the low part of the input | |||
51256 | // vector. | |||
51257 | EVT OutVT16 = EVT::getVectorVT(*DAG.getContext(), MVT::i16, | |||
51258 | VT.getVectorNumElements() * 2); | |||
51259 | if (OutVT16.bitsLT(In0.getValueType())) { | |||
51260 | In0 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, OutVT16, In0, | |||
51261 | DAG.getIntPtrConstant(0, DL)); | |||
51262 | } | |||
51263 | if (OutVT16.bitsLT(In1.getValueType())) { | |||
51264 | In1 = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, OutVT16, In1, | |||
51265 | DAG.getIntPtrConstant(0, DL)); | |||
51266 | } | |||
51267 | return SplitOpsAndApply(DAG, Subtarget, DL, VT, { In0, In1 }, | |||
51268 | PMADDBuilder); | |||
51269 | } | |||
51270 | ||||
51271 | // ADD(VPMADDWD(X,Y),VPMADDWD(Z,W)) -> VPMADDWD(SHUFFLE(X,Z), SHUFFLE(Y,W)) | |||
51272 | // If upper element in each pair of both VPMADDWD are zero then we can merge | |||
51273 | // the operand elements and use the implicit add of VPMADDWD. | |||
51274 | // TODO: Add support for VPMADDUBSW (which isn't commutable). | |||
51275 | static SDValue combineAddOfPMADDWD(SelectionDAG &DAG, SDValue N0, SDValue N1, | |||
51276 | const SDLoc &DL, EVT VT) { | |||
51277 | if (N0.getOpcode() != N1.getOpcode() || N0.getOpcode() != X86ISD::VPMADDWD) | |||
51278 | return SDValue(); | |||
51279 | ||||
51280 | // TODO: Add 256/512-bit support once VPMADDWD combines with shuffles. | |||
51281 | if (VT.getSizeInBits() > 128) | |||
51282 | return SDValue(); | |||
51283 | ||||
51284 | unsigned NumElts = VT.getVectorNumElements(); | |||
51285 | MVT OpVT = N0.getOperand(0).getSimpleValueType(); | |||
51286 | APInt DemandedBits = APInt::getAllOnes(OpVT.getScalarSizeInBits()); | |||
51287 | APInt DemandedHiElts = APInt::getSplat(2 * NumElts, APInt(2, 2)); | |||
51288 | ||||
51289 | bool Op0HiZero = | |||
51290 | DAG.MaskedValueIsZero(N0.getOperand(0), DemandedBits, DemandedHiElts) || | |||
51291 | DAG.MaskedValueIsZero(N0.getOperand(1), DemandedBits, DemandedHiElts); | |||
51292 | bool Op1HiZero = | |||
51293 | DAG.MaskedValueIsZero(N1.getOperand(0), DemandedBits, DemandedHiElts) || | |||
51294 | DAG.MaskedValueIsZero(N1.getOperand(1), DemandedBits, DemandedHiElts); | |||
51295 | ||||
51296 | // TODO: Check for zero lower elements once we have actual codegen that | |||
51297 | // creates them. | |||
51298 | if (!Op0HiZero || !Op1HiZero) | |||
51299 | return SDValue(); | |||
51300 | ||||
51301 | // Create a shuffle mask packing the lower elements from each VPMADDWD. | |||
51302 | SmallVector<int> Mask; | |||
51303 | for (int i = 0; i != (int)NumElts; ++i) { | |||
51304 | Mask.push_back(2 * i); | |||
51305 | Mask.push_back(2 * (i + NumElts)); | |||
51306 | } | |||
51307 | ||||
51308 | SDValue LHS = | |||
51309 | DAG.getVectorShuffle(OpVT, DL, N0.getOperand(0), N1.getOperand(0), Mask); | |||
51310 | SDValue RHS = | |||
51311 | DAG.getVectorShuffle(OpVT, DL, N0.getOperand(1), N1.getOperand(1), Mask); | |||
51312 | return DAG.getNode(X86ISD::VPMADDWD, DL, VT, LHS, RHS); | |||
51313 | } | |||
51314 | ||||
51315 | /// CMOV of constants requires materializing constant operands in registers. | |||
51316 | /// Try to fold those constants into an 'add' instruction to reduce instruction | |||
51317 | /// count. We do this with CMOV rather the generic 'select' because there are | |||
51318 | /// earlier folds that may be used to turn select-of-constants into logic hacks. | |||
51319 | static SDValue pushAddIntoCmovOfConsts(SDNode *N, SelectionDAG &DAG) { | |||
51320 | // If an operand is zero, add-of-0 gets simplified away, so that's clearly | |||
51321 | // better because we eliminate 1-2 instructions. This transform is still | |||
51322 | // an improvement without zero operands because we trade 2 move constants and | |||
51323 | // 1 add for 2 adds (LEA) as long as the constants can be represented as | |||
51324 | // immediate asm operands (fit in 32-bits). | |||
51325 | auto isSuitableCmov = [](SDValue V) { | |||
51326 | if (V.getOpcode() != X86ISD::CMOV || !V.hasOneUse()) | |||
51327 | return false; | |||
51328 | if (!isa<ConstantSDNode>(V.getOperand(0)) || | |||
51329 | !isa<ConstantSDNode>(V.getOperand(1))) | |||
51330 | return false; | |||
51331 | return isNullConstant(V.getOperand(0)) || isNullConstant(V.getOperand(1)) || | |||
51332 | (V.getConstantOperandAPInt(0).isSignedIntN(32) && | |||
51333 | V.getConstantOperandAPInt(1).isSignedIntN(32)); | |||
51334 | }; | |||
51335 | ||||
51336 | // Match an appropriate CMOV as the first operand of the add. | |||
51337 | SDValue Cmov = N->getOperand(0); | |||
51338 | SDValue OtherOp = N->getOperand(1); | |||
51339 | if (!isSuitableCmov(Cmov)) | |||
51340 | std::swap(Cmov, OtherOp); | |||
51341 | if (!isSuitableCmov(Cmov)) | |||
51342 | return SDValue(); | |||
51343 | ||||
51344 | EVT VT = N->getValueType(0); | |||
51345 | SDLoc DL(N); | |||
51346 | SDValue FalseOp = Cmov.getOperand(0); | |||
51347 | SDValue TrueOp = Cmov.getOperand(1); | |||
51348 | ||||
51349 | // We will push the add through the select, but we can potentially do better | |||
51350 | // if we know there is another add in the sequence and this is pointer math. | |||
51351 | // In that case, we can absorb an add into the trailing memory op and avoid | |||
51352 | // a 3-operand LEA which is likely slower than a 2-operand LEA. | |||
51353 | // TODO: If target has "slow3OpsLEA", do this even without the trailing memop? | |||
51354 | if (OtherOp.getOpcode() == ISD::ADD && OtherOp.hasOneUse() && | |||
51355 | !isa<ConstantSDNode>(OtherOp.getOperand(0)) && | |||
51356 | all_of(N->uses(), [&](SDNode *Use) { | |||
51357 | auto *MemNode = dyn_cast<MemSDNode>(Use); | |||
51358 | return MemNode && MemNode->getBasePtr().getNode() == N; | |||
51359 | })) { | |||
51360 | // add (cmov C1, C2), add (X, Y) --> add (cmov (add X, C1), (add X, C2)), Y | |||
51361 | // TODO: We are arbitrarily choosing op0 as the 1st piece of the sum, but | |||
51362 | // it is possible that choosing op1 might be better. | |||
51363 | SDValue X = OtherOp.getOperand(0), Y = OtherOp.getOperand(1); | |||
51364 | FalseOp = DAG.getNode(ISD::ADD, DL, VT, X, FalseOp); | |||
51365 | TrueOp = DAG.getNode(ISD::ADD, DL, VT, X, TrueOp); | |||
51366 | Cmov = DAG.getNode(X86ISD::CMOV, DL, VT, FalseOp, TrueOp, | |||
51367 | Cmov.getOperand(2), Cmov.getOperand(3)); | |||
51368 | return DAG.getNode(ISD::ADD, DL, VT, Cmov, Y); | |||
51369 | } | |||
51370 | ||||
51371 | // add (cmov C1, C2), OtherOp --> cmov (add OtherOp, C1), (add OtherOp, C2) | |||
51372 | FalseOp = DAG.getNode(ISD::ADD, DL, VT, OtherOp, FalseOp); | |||
51373 | TrueOp = DAG.getNode(ISD::ADD, DL, VT, OtherOp, TrueOp); | |||
51374 | return DAG.getNode(X86ISD::CMOV, DL, VT, FalseOp, TrueOp, Cmov.getOperand(2), | |||
51375 | Cmov.getOperand(3)); | |||
51376 | } | |||
51377 | ||||
51378 | static SDValue combineAdd(SDNode *N, SelectionDAG &DAG, | |||
51379 | TargetLowering::DAGCombinerInfo &DCI, | |||
51380 | const X86Subtarget &Subtarget) { | |||
51381 | EVT VT = N->getValueType(0); | |||
51382 | SDValue Op0 = N->getOperand(0); | |||
51383 | SDValue Op1 = N->getOperand(1); | |||
51384 | SDLoc DL(N); | |||
51385 | ||||
51386 | if (SDValue Select = pushAddIntoCmovOfConsts(N, DAG)) | |||
51387 | return Select; | |||
51388 | ||||
51389 | if (SDValue MAdd = matchPMADDWD(DAG, Op0, Op1, DL, VT, Subtarget)) | |||
51390 | return MAdd; | |||
51391 | if (SDValue MAdd = matchPMADDWD_2(DAG, Op0, Op1, DL, VT, Subtarget)) | |||
51392 | return MAdd; | |||
51393 | if (SDValue MAdd = combineAddOfPMADDWD(DAG, Op0, Op1, DL, VT)) | |||
51394 | return MAdd; | |||
51395 | ||||
51396 | // Try to synthesize horizontal adds from adds of shuffles. | |||
51397 | if (SDValue V = combineToHorizontalAddSub(N, DAG, Subtarget)) | |||
51398 | return V; | |||
51399 | ||||
51400 | // If vectors of i1 are legal, turn (add (zext (vXi1 X)), Y) into | |||
51401 | // (sub Y, (sext (vXi1 X))). | |||
51402 | // FIXME: We have the (sub Y, (zext (vXi1 X))) -> (add (sext (vXi1 X)), Y) in | |||
51403 | // generic DAG combine without a legal type check, but adding this there | |||
51404 | // caused regressions. | |||
51405 | if (VT.isVector()) { | |||
51406 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
51407 | if (Op0.getOpcode() == ISD::ZERO_EXTEND && | |||
51408 | Op0.getOperand(0).getValueType().getVectorElementType() == MVT::i1 && | |||
51409 | TLI.isTypeLegal(Op0.getOperand(0).getValueType())) { | |||
51410 | SDValue SExt = DAG.getNode(ISD::SIGN_EXTEND, DL, VT, Op0.getOperand(0)); | |||
51411 | return DAG.getNode(ISD::SUB, DL, VT, Op1, SExt); | |||
51412 | } | |||
51413 | ||||
51414 | if (Op1.getOpcode() == ISD::ZERO_EXTEND && | |||
51415 | Op1.getOperand(0).getValueType().getVectorElementType() == MVT::i1 && | |||
51416 | TLI.isTypeLegal(Op1.getOperand(0).getValueType())) { | |||
51417 | SDValue SExt = DAG.getNode(ISD::SIGN_EXTEND, DL, VT, Op1.getOperand(0)); | |||
51418 | return DAG.getNode(ISD::SUB, DL, VT, Op0, SExt); | |||
51419 | } | |||
51420 | } | |||
51421 | ||||
51422 | return combineAddOrSubToADCOrSBB(N, DAG); | |||
51423 | } | |||
51424 | ||||
51425 | static SDValue combineSub(SDNode *N, SelectionDAG &DAG, | |||
51426 | TargetLowering::DAGCombinerInfo &DCI, | |||
51427 | const X86Subtarget &Subtarget) { | |||
51428 | SDValue Op0 = N->getOperand(0); | |||
51429 | SDValue Op1 = N->getOperand(1); | |||
51430 | ||||
51431 | // TODO: Add NoOpaque handling to isConstantIntBuildVectorOrConstantInt. | |||
51432 | auto IsNonOpaqueConstant = [&](SDValue Op) { | |||
51433 | if (SDNode *C = DAG.isConstantIntBuildVectorOrConstantInt(Op)) { | |||
51434 | if (auto *Cst = dyn_cast<ConstantSDNode>(C)) | |||
51435 | return !Cst->isOpaque(); | |||
51436 | return true; | |||
51437 | } | |||
51438 | return false; | |||
51439 | }; | |||
51440 | ||||
51441 | // X86 can't encode an immediate LHS of a sub. See if we can push the | |||
51442 | // negation into a preceding instruction. If the RHS of the sub is a XOR with | |||
51443 | // one use and a constant, invert the immediate, saving one register. | |||
51444 | // sub(C1, xor(X, C2)) -> add(xor(X, ~C2), C1+1) | |||
51445 | if (Op1.getOpcode() == ISD::XOR && IsNonOpaqueConstant(Op0) && | |||
51446 | IsNonOpaqueConstant(Op1.getOperand(1)) && Op1->hasOneUse()) { | |||
51447 | SDLoc DL(N); | |||
51448 | EVT VT = Op0.getValueType(); | |||
51449 | SDValue NewXor = DAG.getNode(ISD::XOR, SDLoc(Op1), VT, Op1.getOperand(0), | |||
51450 | DAG.getNOT(SDLoc(Op1), Op1.getOperand(1), VT)); | |||
51451 | SDValue NewAdd = | |||
51452 | DAG.getNode(ISD::ADD, DL, VT, Op0, DAG.getConstant(1, DL, VT)); | |||
51453 | return DAG.getNode(ISD::ADD, DL, VT, NewXor, NewAdd); | |||
51454 | } | |||
51455 | ||||
51456 | // Try to synthesize horizontal subs from subs of shuffles. | |||
51457 | if (SDValue V = combineToHorizontalAddSub(N, DAG, Subtarget)) | |||
51458 | return V; | |||
51459 | ||||
51460 | return combineAddOrSubToADCOrSBB(N, DAG); | |||
51461 | } | |||
51462 | ||||
51463 | static SDValue combineVectorCompare(SDNode *N, SelectionDAG &DAG, | |||
51464 | const X86Subtarget &Subtarget) { | |||
51465 | MVT VT = N->getSimpleValueType(0); | |||
51466 | SDLoc DL(N); | |||
51467 | ||||
51468 | if (N->getOperand(0) == N->getOperand(1)) { | |||
51469 | if (N->getOpcode() == X86ISD::PCMPEQ) | |||
51470 | return DAG.getConstant(-1, DL, VT); | |||
51471 | if (N->getOpcode() == X86ISD::PCMPGT) | |||
51472 | return DAG.getConstant(0, DL, VT); | |||
51473 | } | |||
51474 | ||||
51475 | return SDValue(); | |||
51476 | } | |||
51477 | ||||
51478 | /// Helper that combines an array of subvector ops as if they were the operands | |||
51479 | /// of a ISD::CONCAT_VECTORS node, but may have come from another source (e.g. | |||
51480 | /// ISD::INSERT_SUBVECTOR). The ops are assumed to be of the same type. | |||
51481 | static SDValue combineConcatVectorOps(const SDLoc &DL, MVT VT, | |||
51482 | ArrayRef<SDValue> Ops, SelectionDAG &DAG, | |||
51483 | TargetLowering::DAGCombinerInfo &DCI, | |||
51484 | const X86Subtarget &Subtarget) { | |||
51485 | assert(Subtarget.hasAVX() && "AVX assumed for concat_vectors")(static_cast <bool> (Subtarget.hasAVX() && "AVX assumed for concat_vectors" ) ? void (0) : __assert_fail ("Subtarget.hasAVX() && \"AVX assumed for concat_vectors\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 51485, __extension__ __PRETTY_FUNCTION__)); | |||
51486 | unsigned EltSizeInBits = VT.getScalarSizeInBits(); | |||
51487 | ||||
51488 | if (llvm::all_of(Ops, [](SDValue Op) { return Op.isUndef(); })) | |||
51489 | return DAG.getUNDEF(VT); | |||
51490 | ||||
51491 | if (llvm::all_of(Ops, [](SDValue Op) { | |||
51492 | return ISD::isBuildVectorAllZeros(Op.getNode()); | |||
51493 | })) | |||
51494 | return getZeroVector(VT, Subtarget, DAG, DL); | |||
51495 | ||||
51496 | SDValue Op0 = Ops[0]; | |||
51497 | bool IsSplat = llvm::all_of(Ops, [&Op0](SDValue Op) { return Op == Op0; }); | |||
51498 | ||||
51499 | // Repeated subvectors. | |||
51500 | if (IsSplat && | |||
51501 | (VT.is256BitVector() || (VT.is512BitVector() && Subtarget.hasAVX512()))) { | |||
51502 | // If this broadcast is inserted into both halves, use a larger broadcast. | |||
51503 | if (Op0.getOpcode() == X86ISD::VBROADCAST) | |||
51504 | return DAG.getNode(Op0.getOpcode(), DL, VT, Op0.getOperand(0)); | |||
51505 | ||||
51506 | // If this scalar/subvector broadcast_load is inserted into both halves, use | |||
51507 | // a larger broadcast_load. Update other uses to use an extracted subvector. | |||
51508 | if (Op0.getOpcode() == X86ISD::VBROADCAST_LOAD || | |||
51509 | Op0.getOpcode() == X86ISD::SUBV_BROADCAST_LOAD) { | |||
51510 | auto *MemIntr = cast<MemIntrinsicSDNode>(Op0); | |||
51511 | SDVTList Tys = DAG.getVTList(VT, MVT::Other); | |||
51512 | SDValue Ops[] = {MemIntr->getChain(), MemIntr->getBasePtr()}; | |||
51513 | SDValue BcastLd = DAG.getMemIntrinsicNode(Op0.getOpcode(), DL, Tys, Ops, | |||
51514 | MemIntr->getMemoryVT(), | |||
51515 | MemIntr->getMemOperand()); | |||
51516 | DAG.ReplaceAllUsesOfValueWith( | |||
51517 | Op0, extractSubVector(BcastLd, 0, DAG, DL, Op0.getValueSizeInBits())); | |||
51518 | DAG.ReplaceAllUsesOfValueWith(SDValue(MemIntr, 1), BcastLd.getValue(1)); | |||
51519 | return BcastLd; | |||
51520 | } | |||
51521 | ||||
51522 | // If this is a simple subvector load repeated across multiple lanes, then | |||
51523 | // broadcast the load. Update other uses to use an extracted subvector. | |||
51524 | if (auto *Ld = dyn_cast<LoadSDNode>(Op0)) { | |||
51525 | if (Ld->isSimple() && !Ld->isNonTemporal() && | |||
51526 | Ld->getExtensionType() == ISD::NON_EXTLOAD) { | |||
51527 | SDVTList Tys = DAG.getVTList(VT, MVT::Other); | |||
51528 | SDValue Ops[] = {Ld->getChain(), Ld->getBasePtr()}; | |||
51529 | SDValue BcastLd = | |||
51530 | DAG.getMemIntrinsicNode(X86ISD::SUBV_BROADCAST_LOAD, DL, Tys, Ops, | |||
51531 | Ld->getMemoryVT(), Ld->getMemOperand()); | |||
51532 | DAG.ReplaceAllUsesOfValueWith( | |||
51533 | Op0, | |||
51534 | extractSubVector(BcastLd, 0, DAG, DL, Op0.getValueSizeInBits())); | |||
51535 | DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), BcastLd.getValue(1)); | |||
51536 | return BcastLd; | |||
51537 | } | |||
51538 | } | |||
51539 | ||||
51540 | // concat_vectors(movddup(x),movddup(x)) -> broadcast(x) | |||
51541 | if (Op0.getOpcode() == X86ISD::MOVDDUP && VT == MVT::v4f64 && | |||
51542 | (Subtarget.hasAVX2() || MayFoldLoadIntoBroadcastFromMem( | |||
51543 | Op0.getOperand(0), VT.getScalarType()))) | |||
51544 | return DAG.getNode(X86ISD::VBROADCAST, DL, VT, | |||
51545 | DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f64, | |||
51546 | Op0.getOperand(0), | |||
51547 | DAG.getIntPtrConstant(0, DL))); | |||
51548 | ||||
51549 | // concat_vectors(scalar_to_vector(x),scalar_to_vector(x)) -> broadcast(x) | |||
51550 | if (Op0.getOpcode() == ISD::SCALAR_TO_VECTOR && | |||
51551 | (Subtarget.hasAVX2() || | |||
51552 | (EltSizeInBits >= 32 && MayFoldLoad(Op0.getOperand(0)))) && | |||
51553 | Op0.getOperand(0).getValueType() == VT.getScalarType()) | |||
51554 | return DAG.getNode(X86ISD::VBROADCAST, DL, VT, Op0.getOperand(0)); | |||
51555 | ||||
51556 | // concat_vectors(extract_subvector(broadcast(x)), | |||
51557 | // extract_subvector(broadcast(x))) -> broadcast(x) | |||
51558 | if (Op0.getOpcode() == ISD::EXTRACT_SUBVECTOR && | |||
51559 | Op0.getOperand(0).getValueType() == VT) { | |||
51560 | if (Op0.getOperand(0).getOpcode() == X86ISD::VBROADCAST || | |||
51561 | Op0.getOperand(0).getOpcode() == X86ISD::VBROADCAST_LOAD) | |||
51562 | return Op0.getOperand(0); | |||
51563 | } | |||
51564 | } | |||
51565 | ||||
51566 | // concat(extract_subvector(v0,c0), extract_subvector(v1,c1)) -> vperm2x128. | |||
51567 | // Only concat of subvector high halves which vperm2x128 is best at. | |||
51568 | // TODO: This should go in combineX86ShufflesRecursively eventually. | |||
51569 | if (VT.is256BitVector() && Ops.size() == 2) { | |||
51570 | SDValue Src0 = peekThroughBitcasts(Ops[0]); | |||
51571 | SDValue Src1 = peekThroughBitcasts(Ops[1]); | |||
51572 | if (Src0.getOpcode() == ISD::EXTRACT_SUBVECTOR && | |||
51573 | Src1.getOpcode() == ISD::EXTRACT_SUBVECTOR) { | |||
51574 | EVT SrcVT0 = Src0.getOperand(0).getValueType(); | |||
51575 | EVT SrcVT1 = Src1.getOperand(0).getValueType(); | |||
51576 | unsigned NumSrcElts0 = SrcVT0.getVectorNumElements(); | |||
51577 | unsigned NumSrcElts1 = SrcVT1.getVectorNumElements(); | |||
51578 | if (SrcVT0.is256BitVector() && SrcVT1.is256BitVector() && | |||
51579 | Src0.getConstantOperandAPInt(1) == (NumSrcElts0 / 2) && | |||
51580 | Src1.getConstantOperandAPInt(1) == (NumSrcElts1 / 2)) { | |||
51581 | return DAG.getNode(X86ISD::VPERM2X128, DL, VT, | |||
51582 | DAG.getBitcast(VT, Src0.getOperand(0)), | |||
51583 | DAG.getBitcast(VT, Src1.getOperand(0)), | |||
51584 | DAG.getTargetConstant(0x31, DL, MVT::i8)); | |||
51585 | } | |||
51586 | } | |||
51587 | } | |||
51588 | ||||
51589 | // Repeated opcode. | |||
51590 | // TODO - combineX86ShufflesRecursively should handle shuffle concatenation | |||
51591 | // but it currently struggles with different vector widths. | |||
51592 | if (llvm::all_of(Ops, [Op0](SDValue Op) { | |||
51593 | return Op.getOpcode() == Op0.getOpcode(); | |||
51594 | })) { | |||
51595 | auto ConcatSubOperand = [&](MVT VT, ArrayRef<SDValue> SubOps, unsigned I) { | |||
51596 | SmallVector<SDValue> Subs; | |||
51597 | for (SDValue SubOp : SubOps) | |||
51598 | Subs.push_back(SubOp.getOperand(I)); | |||
51599 | return DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, Subs); | |||
51600 | }; | |||
51601 | ||||
51602 | unsigned NumOps = Ops.size(); | |||
51603 | switch (Op0.getOpcode()) { | |||
51604 | case X86ISD::SHUFP: { | |||
51605 | // Add SHUFPD support if/when necessary. | |||
51606 | if (!IsSplat && VT.getScalarType() == MVT::f32 && | |||
51607 | llvm::all_of(Ops, [Op0](SDValue Op) { | |||
51608 | return Op.getOperand(2) == Op0.getOperand(2); | |||
51609 | })) { | |||
51610 | return DAG.getNode(Op0.getOpcode(), DL, VT, | |||
51611 | ConcatSubOperand(VT, Ops, 0), | |||
51612 | ConcatSubOperand(VT, Ops, 1), Op0.getOperand(2)); | |||
51613 | } | |||
51614 | break; | |||
51615 | } | |||
51616 | case X86ISD::PSHUFHW: | |||
51617 | case X86ISD::PSHUFLW: | |||
51618 | case X86ISD::PSHUFD: | |||
51619 | if (!IsSplat && NumOps == 2 && VT.is256BitVector() && | |||
51620 | Subtarget.hasInt256() && Op0.getOperand(1) == Ops[1].getOperand(1)) { | |||
51621 | return DAG.getNode(Op0.getOpcode(), DL, VT, | |||
51622 | ConcatSubOperand(VT, Ops, 0), Op0.getOperand(1)); | |||
51623 | } | |||
51624 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
51625 | case X86ISD::VPERMILPI: | |||
51626 | // TODO - add support for vXf64/vXi64 shuffles. | |||
51627 | if (!IsSplat && NumOps == 2 && (VT == MVT::v8f32 || VT == MVT::v8i32) && | |||
51628 | Subtarget.hasAVX() && Op0.getOperand(1) == Ops[1].getOperand(1)) { | |||
51629 | SDValue Res = DAG.getBitcast(MVT::v8f32, ConcatSubOperand(VT, Ops, 0)); | |||
51630 | Res = DAG.getNode(X86ISD::VPERMILPI, DL, MVT::v8f32, Res, | |||
51631 | Op0.getOperand(1)); | |||
51632 | return DAG.getBitcast(VT, Res); | |||
51633 | } | |||
51634 | break; | |||
51635 | case X86ISD::VPERMV3: | |||
51636 | if (!IsSplat && NumOps == 2 && VT.is512BitVector()) { | |||
51637 | MVT OpVT = Op0.getSimpleValueType(); | |||
51638 | int NumSrcElts = OpVT.getVectorNumElements(); | |||
51639 | SmallVector<int, 64> ConcatMask; | |||
51640 | for (unsigned i = 0; i != NumOps; ++i) { | |||
51641 | SmallVector<int, 64> SubMask; | |||
51642 | SmallVector<SDValue, 2> SubOps; | |||
51643 | if (!getTargetShuffleMask(Ops[i].getNode(), OpVT, false, SubOps, | |||
51644 | SubMask)) | |||
51645 | break; | |||
51646 | for (int M : SubMask) { | |||
51647 | if (0 <= M) { | |||
51648 | M += M < NumSrcElts ? 0 : NumSrcElts; | |||
51649 | M += i * NumSrcElts; | |||
51650 | } | |||
51651 | ConcatMask.push_back(M); | |||
51652 | } | |||
51653 | } | |||
51654 | if (ConcatMask.size() == (NumOps * NumSrcElts)) { | |||
51655 | SDValue Src0 = concatSubVectors(Ops[0].getOperand(0), | |||
51656 | Ops[1].getOperand(0), DAG, DL); | |||
51657 | SDValue Src1 = concatSubVectors(Ops[0].getOperand(2), | |||
51658 | Ops[1].getOperand(2), DAG, DL); | |||
51659 | MVT IntMaskSVT = MVT::getIntegerVT(VT.getScalarSizeInBits()); | |||
51660 | MVT IntMaskVT = MVT::getVectorVT(IntMaskSVT, NumOps * NumSrcElts); | |||
51661 | SDValue Mask = getConstVector(ConcatMask, IntMaskVT, DAG, DL, true); | |||
51662 | return DAG.getNode(X86ISD::VPERMV3, DL, VT, Src0, Mask, Src1); | |||
51663 | } | |||
51664 | } | |||
51665 | break; | |||
51666 | case X86ISD::VSHLI: | |||
51667 | case X86ISD::VSRLI: | |||
51668 | // Special case: SHL/SRL AVX1 V4i64 by 32-bits can lower as a shuffle. | |||
51669 | // TODO: Move this to LowerScalarImmediateShift? | |||
51670 | if (VT == MVT::v4i64 && !Subtarget.hasInt256() && | |||
51671 | llvm::all_of(Ops, [](SDValue Op) { | |||
51672 | return Op.getConstantOperandAPInt(1) == 32; | |||
51673 | })) { | |||
51674 | SDValue Res = DAG.getBitcast(MVT::v8i32, ConcatSubOperand(VT, Ops, 0)); | |||
51675 | SDValue Zero = getZeroVector(MVT::v8i32, Subtarget, DAG, DL); | |||
51676 | if (Op0.getOpcode() == X86ISD::VSHLI) { | |||
51677 | Res = DAG.getVectorShuffle(MVT::v8i32, DL, Res, Zero, | |||
51678 | {8, 0, 8, 2, 8, 4, 8, 6}); | |||
51679 | } else { | |||
51680 | Res = DAG.getVectorShuffle(MVT::v8i32, DL, Res, Zero, | |||
51681 | {1, 8, 3, 8, 5, 8, 7, 8}); | |||
51682 | } | |||
51683 | return DAG.getBitcast(VT, Res); | |||
51684 | } | |||
51685 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
51686 | case X86ISD::VSRAI: | |||
51687 | if (((VT.is256BitVector() && Subtarget.hasInt256()) || | |||
51688 | (VT.is512BitVector() && Subtarget.useAVX512Regs() && | |||
51689 | (EltSizeInBits >= 32 || Subtarget.useBWIRegs()))) && | |||
51690 | llvm::all_of(Ops, [Op0](SDValue Op) { | |||
51691 | return Op0.getOperand(1) == Op.getOperand(1); | |||
51692 | })) { | |||
51693 | return DAG.getNode(Op0.getOpcode(), DL, VT, | |||
51694 | ConcatSubOperand(VT, Ops, 0), Op0.getOperand(1)); | |||
51695 | } | |||
51696 | break; | |||
51697 | case X86ISD::VPERMI: | |||
51698 | case X86ISD::VROTLI: | |||
51699 | case X86ISD::VROTRI: | |||
51700 | if (VT.is512BitVector() && Subtarget.useAVX512Regs() && | |||
51701 | llvm::all_of(Ops, [Op0](SDValue Op) { | |||
51702 | return Op0.getOperand(1) == Op.getOperand(1); | |||
51703 | })) { | |||
51704 | return DAG.getNode(Op0.getOpcode(), DL, VT, | |||
51705 | ConcatSubOperand(VT, Ops, 0), Op0.getOperand(1)); | |||
51706 | } | |||
51707 | break; | |||
51708 | case ISD::AND: | |||
51709 | case ISD::OR: | |||
51710 | case ISD::XOR: | |||
51711 | case X86ISD::ANDNP: | |||
51712 | // TODO: Add 256-bit support. | |||
51713 | if (!IsSplat && VT.is512BitVector()) { | |||
51714 | MVT SrcVT = Op0.getOperand(0).getSimpleValueType(); | |||
51715 | SrcVT = MVT::getVectorVT(SrcVT.getScalarType(), | |||
51716 | NumOps * SrcVT.getVectorNumElements()); | |||
51717 | return DAG.getNode(Op0.getOpcode(), DL, VT, | |||
51718 | ConcatSubOperand(SrcVT, Ops, 0), | |||
51719 | ConcatSubOperand(SrcVT, Ops, 1)); | |||
51720 | } | |||
51721 | break; | |||
51722 | case X86ISD::HADD: | |||
51723 | case X86ISD::HSUB: | |||
51724 | case X86ISD::FHADD: | |||
51725 | case X86ISD::FHSUB: | |||
51726 | case X86ISD::PACKSS: | |||
51727 | case X86ISD::PACKUS: | |||
51728 | if (!IsSplat && VT.is256BitVector() && | |||
51729 | (VT.isFloatingPoint() || Subtarget.hasInt256())) { | |||
51730 | MVT SrcVT = Op0.getOperand(0).getSimpleValueType(); | |||
51731 | SrcVT = MVT::getVectorVT(SrcVT.getScalarType(), | |||
51732 | NumOps * SrcVT.getVectorNumElements()); | |||
51733 | return DAG.getNode(Op0.getOpcode(), DL, VT, | |||
51734 | ConcatSubOperand(SrcVT, Ops, 0), | |||
51735 | ConcatSubOperand(SrcVT, Ops, 1)); | |||
51736 | } | |||
51737 | break; | |||
51738 | case X86ISD::PALIGNR: | |||
51739 | if (!IsSplat && | |||
51740 | ((VT.is256BitVector() && Subtarget.hasInt256()) || | |||
51741 | (VT.is512BitVector() && Subtarget.useBWIRegs())) && | |||
51742 | llvm::all_of(Ops, [Op0](SDValue Op) { | |||
51743 | return Op0.getOperand(2) == Op.getOperand(2); | |||
51744 | })) { | |||
51745 | return DAG.getNode(Op0.getOpcode(), DL, VT, | |||
51746 | ConcatSubOperand(VT, Ops, 0), | |||
51747 | ConcatSubOperand(VT, Ops, 1), Op0.getOperand(2)); | |||
51748 | } | |||
51749 | break; | |||
51750 | } | |||
51751 | } | |||
51752 | ||||
51753 | // Fold subvector loads into one. | |||
51754 | // If needed, look through bitcasts to get to the load. | |||
51755 | if (auto *FirstLd = dyn_cast<LoadSDNode>(peekThroughBitcasts(Op0))) { | |||
51756 | bool Fast; | |||
51757 | const X86TargetLowering *TLI = Subtarget.getTargetLowering(); | |||
51758 | if (TLI->allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), VT, | |||
51759 | *FirstLd->getMemOperand(), &Fast) && | |||
51760 | Fast) { | |||
51761 | if (SDValue Ld = | |||
51762 | EltsFromConsecutiveLoads(VT, Ops, DL, DAG, Subtarget, false)) | |||
51763 | return Ld; | |||
51764 | } | |||
51765 | } | |||
51766 | ||||
51767 | return SDValue(); | |||
51768 | } | |||
51769 | ||||
51770 | static SDValue combineConcatVectors(SDNode *N, SelectionDAG &DAG, | |||
51771 | TargetLowering::DAGCombinerInfo &DCI, | |||
51772 | const X86Subtarget &Subtarget) { | |||
51773 | EVT VT = N->getValueType(0); | |||
51774 | EVT SrcVT = N->getOperand(0).getValueType(); | |||
51775 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
51776 | ||||
51777 | // Don't do anything for i1 vectors. | |||
51778 | if (VT.getVectorElementType() == MVT::i1) | |||
51779 | return SDValue(); | |||
51780 | ||||
51781 | if (Subtarget.hasAVX() && TLI.isTypeLegal(VT) && TLI.isTypeLegal(SrcVT)) { | |||
51782 | SmallVector<SDValue, 4> Ops(N->op_begin(), N->op_end()); | |||
51783 | if (SDValue R = combineConcatVectorOps(SDLoc(N), VT.getSimpleVT(), Ops, DAG, | |||
51784 | DCI, Subtarget)) | |||
51785 | return R; | |||
51786 | } | |||
51787 | ||||
51788 | return SDValue(); | |||
51789 | } | |||
51790 | ||||
51791 | static SDValue combineInsertSubvector(SDNode *N, SelectionDAG &DAG, | |||
51792 | TargetLowering::DAGCombinerInfo &DCI, | |||
51793 | const X86Subtarget &Subtarget) { | |||
51794 | if (DCI.isBeforeLegalizeOps()) | |||
51795 | return SDValue(); | |||
51796 | ||||
51797 | MVT OpVT = N->getSimpleValueType(0); | |||
51798 | ||||
51799 | bool IsI1Vector = OpVT.getVectorElementType() == MVT::i1; | |||
51800 | ||||
51801 | SDLoc dl(N); | |||
51802 | SDValue Vec = N->getOperand(0); | |||
51803 | SDValue SubVec = N->getOperand(1); | |||
51804 | ||||
51805 | uint64_t IdxVal = N->getConstantOperandVal(2); | |||
51806 | MVT SubVecVT = SubVec.getSimpleValueType(); | |||
51807 | ||||
51808 | if (Vec.isUndef() && SubVec.isUndef()) | |||
51809 | return DAG.getUNDEF(OpVT); | |||
51810 | ||||
51811 | // Inserting undefs/zeros into zeros/undefs is a zero vector. | |||
51812 | if ((Vec.isUndef() || ISD::isBuildVectorAllZeros(Vec.getNode())) && | |||
51813 | (SubVec.isUndef() || ISD::isBuildVectorAllZeros(SubVec.getNode()))) | |||
51814 | return getZeroVector(OpVT, Subtarget, DAG, dl); | |||
51815 | ||||
51816 | if (ISD::isBuildVectorAllZeros(Vec.getNode())) { | |||
51817 | // If we're inserting into a zero vector and then into a larger zero vector, | |||
51818 | // just insert into the larger zero vector directly. | |||
51819 | if (SubVec.getOpcode() == ISD::INSERT_SUBVECTOR && | |||
51820 | ISD::isBuildVectorAllZeros(SubVec.getOperand(0).getNode())) { | |||
51821 | uint64_t Idx2Val = SubVec.getConstantOperandVal(2); | |||
51822 | return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, OpVT, | |||
51823 | getZeroVector(OpVT, Subtarget, DAG, dl), | |||
51824 | SubVec.getOperand(1), | |||
51825 | DAG.getIntPtrConstant(IdxVal + Idx2Val, dl)); | |||
51826 | } | |||
51827 | ||||
51828 | // If we're inserting into a zero vector and our input was extracted from an | |||
51829 | // insert into a zero vector of the same type and the extraction was at | |||
51830 | // least as large as the original insertion. Just insert the original | |||
51831 | // subvector into a zero vector. | |||
51832 | if (SubVec.getOpcode() == ISD::EXTRACT_SUBVECTOR && IdxVal == 0 && | |||
51833 | isNullConstant(SubVec.getOperand(1)) && | |||
51834 | SubVec.getOperand(0).getOpcode() == ISD::INSERT_SUBVECTOR) { | |||
51835 | SDValue Ins = SubVec.getOperand(0); | |||
51836 | if (isNullConstant(Ins.getOperand(2)) && | |||
51837 | ISD::isBuildVectorAllZeros(Ins.getOperand(0).getNode()) && | |||
51838 | Ins.getOperand(1).getValueSizeInBits().getFixedSize() <= | |||
51839 | SubVecVT.getFixedSizeInBits()) | |||
51840 | return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, OpVT, | |||
51841 | getZeroVector(OpVT, Subtarget, DAG, dl), | |||
51842 | Ins.getOperand(1), N->getOperand(2)); | |||
51843 | } | |||
51844 | } | |||
51845 | ||||
51846 | // Stop here if this is an i1 vector. | |||
51847 | if (IsI1Vector) | |||
51848 | return SDValue(); | |||
51849 | ||||
51850 | // If this is an insert of an extract, combine to a shuffle. Don't do this | |||
51851 | // if the insert or extract can be represented with a subregister operation. | |||
51852 | if (SubVec.getOpcode() == ISD::EXTRACT_SUBVECTOR && | |||
51853 | SubVec.getOperand(0).getSimpleValueType() == OpVT && | |||
51854 | (IdxVal != 0 || | |||
51855 | !(Vec.isUndef() || ISD::isBuildVectorAllZeros(Vec.getNode())))) { | |||
51856 | int ExtIdxVal = SubVec.getConstantOperandVal(1); | |||
51857 | if (ExtIdxVal != 0) { | |||
51858 | int VecNumElts = OpVT.getVectorNumElements(); | |||
51859 | int SubVecNumElts = SubVecVT.getVectorNumElements(); | |||
51860 | SmallVector<int, 64> Mask(VecNumElts); | |||
51861 | // First create an identity shuffle mask. | |||
51862 | for (int i = 0; i != VecNumElts; ++i) | |||
51863 | Mask[i] = i; | |||
51864 | // Now insert the extracted portion. | |||
51865 | for (int i = 0; i != SubVecNumElts; ++i) | |||
51866 | Mask[i + IdxVal] = i + ExtIdxVal + VecNumElts; | |||
51867 | ||||
51868 | return DAG.getVectorShuffle(OpVT, dl, Vec, SubVec.getOperand(0), Mask); | |||
51869 | } | |||
51870 | } | |||
51871 | ||||
51872 | // Match concat_vector style patterns. | |||
51873 | SmallVector<SDValue, 2> SubVectorOps; | |||
51874 | if (collectConcatOps(N, SubVectorOps)) { | |||
51875 | if (SDValue Fold = | |||
51876 | combineConcatVectorOps(dl, OpVT, SubVectorOps, DAG, DCI, Subtarget)) | |||
51877 | return Fold; | |||
51878 | ||||
51879 | // If we're inserting all zeros into the upper half, change this to | |||
51880 | // a concat with zero. We will match this to a move | |||
51881 | // with implicit upper bit zeroing during isel. | |||
51882 | // We do this here because we don't want combineConcatVectorOps to | |||
51883 | // create INSERT_SUBVECTOR from CONCAT_VECTORS. | |||
51884 | if (SubVectorOps.size() == 2 && | |||
51885 | ISD::isBuildVectorAllZeros(SubVectorOps[1].getNode())) | |||
51886 | return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, OpVT, | |||
51887 | getZeroVector(OpVT, Subtarget, DAG, dl), | |||
51888 | SubVectorOps[0], DAG.getIntPtrConstant(0, dl)); | |||
51889 | } | |||
51890 | ||||
51891 | // If this is a broadcast insert into an upper undef, use a larger broadcast. | |||
51892 | if (Vec.isUndef() && IdxVal != 0 && SubVec.getOpcode() == X86ISD::VBROADCAST) | |||
51893 | return DAG.getNode(X86ISD::VBROADCAST, dl, OpVT, SubVec.getOperand(0)); | |||
51894 | ||||
51895 | // If this is a broadcast load inserted into an upper undef, use a larger | |||
51896 | // broadcast load. | |||
51897 | if (Vec.isUndef() && IdxVal != 0 && SubVec.hasOneUse() && | |||
51898 | SubVec.getOpcode() == X86ISD::VBROADCAST_LOAD) { | |||
51899 | auto *MemIntr = cast<MemIntrinsicSDNode>(SubVec); | |||
51900 | SDVTList Tys = DAG.getVTList(OpVT, MVT::Other); | |||
51901 | SDValue Ops[] = { MemIntr->getChain(), MemIntr->getBasePtr() }; | |||
51902 | SDValue BcastLd = | |||
51903 | DAG.getMemIntrinsicNode(X86ISD::VBROADCAST_LOAD, dl, Tys, Ops, | |||
51904 | MemIntr->getMemoryVT(), | |||
51905 | MemIntr->getMemOperand()); | |||
51906 | DAG.ReplaceAllUsesOfValueWith(SDValue(MemIntr, 1), BcastLd.getValue(1)); | |||
51907 | return BcastLd; | |||
51908 | } | |||
51909 | ||||
51910 | // If we're splatting the lower half subvector of a full vector load into the | |||
51911 | // upper half, attempt to create a subvector broadcast. | |||
51912 | if (IdxVal == (OpVT.getVectorNumElements() / 2) && SubVec.hasOneUse() && | |||
51913 | Vec.getValueSizeInBits() == (2 * SubVec.getValueSizeInBits())) { | |||
51914 | auto *VecLd = dyn_cast<LoadSDNode>(Vec); | |||
51915 | auto *SubLd = dyn_cast<LoadSDNode>(SubVec); | |||
51916 | if (VecLd && SubLd && | |||
51917 | DAG.areNonVolatileConsecutiveLoads(SubLd, VecLd, | |||
51918 | SubVec.getValueSizeInBits() / 8, 0)) | |||
51919 | return getBROADCAST_LOAD(X86ISD::SUBV_BROADCAST_LOAD, dl, OpVT, SubVecVT, | |||
51920 | SubLd, 0, DAG); | |||
51921 | } | |||
51922 | ||||
51923 | return SDValue(); | |||
51924 | } | |||
51925 | ||||
51926 | /// If we are extracting a subvector of a vector select and the select condition | |||
51927 | /// is composed of concatenated vectors, try to narrow the select width. This | |||
51928 | /// is a common pattern for AVX1 integer code because 256-bit selects may be | |||
51929 | /// legal, but there is almost no integer math/logic available for 256-bit. | |||
51930 | /// This function should only be called with legal types (otherwise, the calls | |||
51931 | /// to get simple value types will assert). | |||
51932 | static SDValue narrowExtractedVectorSelect(SDNode *Ext, SelectionDAG &DAG) { | |||
51933 | SDValue Sel = peekThroughBitcasts(Ext->getOperand(0)); | |||
51934 | SmallVector<SDValue, 4> CatOps; | |||
51935 | if (Sel.getOpcode() != ISD::VSELECT || | |||
51936 | !collectConcatOps(Sel.getOperand(0).getNode(), CatOps)) | |||
51937 | return SDValue(); | |||
51938 | ||||
51939 | // Note: We assume simple value types because this should only be called with | |||
51940 | // legal operations/types. | |||
51941 | // TODO: This can be extended to handle extraction to 256-bits. | |||
51942 | MVT VT = Ext->getSimpleValueType(0); | |||
51943 | if (!VT.is128BitVector()) | |||
51944 | return SDValue(); | |||
51945 | ||||
51946 | MVT SelCondVT = Sel.getOperand(0).getSimpleValueType(); | |||
51947 | if (!SelCondVT.is256BitVector() && !SelCondVT.is512BitVector()) | |||
51948 | return SDValue(); | |||
51949 | ||||
51950 | MVT WideVT = Ext->getOperand(0).getSimpleValueType(); | |||
51951 | MVT SelVT = Sel.getSimpleValueType(); | |||
51952 | assert((SelVT.is256BitVector() || SelVT.is512BitVector()) &&(static_cast <bool> ((SelVT.is256BitVector() || SelVT.is512BitVector ()) && "Unexpected vector type with legal operations" ) ? void (0) : __assert_fail ("(SelVT.is256BitVector() || SelVT.is512BitVector()) && \"Unexpected vector type with legal operations\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 51953, __extension__ __PRETTY_FUNCTION__)) | |||
51953 | "Unexpected vector type with legal operations")(static_cast <bool> ((SelVT.is256BitVector() || SelVT.is512BitVector ()) && "Unexpected vector type with legal operations" ) ? void (0) : __assert_fail ("(SelVT.is256BitVector() || SelVT.is512BitVector()) && \"Unexpected vector type with legal operations\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 51953, __extension__ __PRETTY_FUNCTION__)); | |||
51954 | ||||
51955 | unsigned SelElts = SelVT.getVectorNumElements(); | |||
51956 | unsigned CastedElts = WideVT.getVectorNumElements(); | |||
51957 | unsigned ExtIdx = Ext->getConstantOperandVal(1); | |||
51958 | if (SelElts % CastedElts == 0) { | |||
51959 | // The select has the same or more (narrower) elements than the extract | |||
51960 | // operand. The extraction index gets scaled by that factor. | |||
51961 | ExtIdx *= (SelElts / CastedElts); | |||
51962 | } else if (CastedElts % SelElts == 0) { | |||
51963 | // The select has less (wider) elements than the extract operand. Make sure | |||
51964 | // that the extraction index can be divided evenly. | |||
51965 | unsigned IndexDivisor = CastedElts / SelElts; | |||
51966 | if (ExtIdx % IndexDivisor != 0) | |||
51967 | return SDValue(); | |||
51968 | ExtIdx /= IndexDivisor; | |||
51969 | } else { | |||
51970 | llvm_unreachable("Element count of simple vector types are not divisible?")::llvm::llvm_unreachable_internal("Element count of simple vector types are not divisible?" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 51970); | |||
51971 | } | |||
51972 | ||||
51973 | unsigned NarrowingFactor = WideVT.getSizeInBits() / VT.getSizeInBits(); | |||
51974 | unsigned NarrowElts = SelElts / NarrowingFactor; | |||
51975 | MVT NarrowSelVT = MVT::getVectorVT(SelVT.getVectorElementType(), NarrowElts); | |||
51976 | SDLoc DL(Ext); | |||
51977 | SDValue ExtCond = extract128BitVector(Sel.getOperand(0), ExtIdx, DAG, DL); | |||
51978 | SDValue ExtT = extract128BitVector(Sel.getOperand(1), ExtIdx, DAG, DL); | |||
51979 | SDValue ExtF = extract128BitVector(Sel.getOperand(2), ExtIdx, DAG, DL); | |||
51980 | SDValue NarrowSel = DAG.getSelect(DL, NarrowSelVT, ExtCond, ExtT, ExtF); | |||
51981 | return DAG.getBitcast(VT, NarrowSel); | |||
51982 | } | |||
51983 | ||||
51984 | static SDValue combineExtractSubvector(SDNode *N, SelectionDAG &DAG, | |||
51985 | TargetLowering::DAGCombinerInfo &DCI, | |||
51986 | const X86Subtarget &Subtarget) { | |||
51987 | // For AVX1 only, if we are extracting from a 256-bit and+not (which will | |||
51988 | // eventually get combined/lowered into ANDNP) with a concatenated operand, | |||
51989 | // split the 'and' into 128-bit ops to avoid the concatenate and extract. | |||
51990 | // We let generic combining take over from there to simplify the | |||
51991 | // insert/extract and 'not'. | |||
51992 | // This pattern emerges during AVX1 legalization. We handle it before lowering | |||
51993 | // to avoid complications like splitting constant vector loads. | |||
51994 | ||||
51995 | // Capture the original wide type in the likely case that we need to bitcast | |||
51996 | // back to this type. | |||
51997 | if (!N->getValueType(0).isSimple()) | |||
51998 | return SDValue(); | |||
51999 | ||||
52000 | MVT VT = N->getSimpleValueType(0); | |||
52001 | SDValue InVec = N->getOperand(0); | |||
52002 | unsigned IdxVal = N->getConstantOperandVal(1); | |||
52003 | SDValue InVecBC = peekThroughBitcasts(InVec); | |||
52004 | EVT InVecVT = InVec.getValueType(); | |||
52005 | unsigned SizeInBits = VT.getSizeInBits(); | |||
52006 | unsigned InSizeInBits = InVecVT.getSizeInBits(); | |||
52007 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
52008 | ||||
52009 | if (Subtarget.hasAVX() && !Subtarget.hasAVX2() && | |||
52010 | TLI.isTypeLegal(InVecVT) && | |||
52011 | InSizeInBits == 256 && InVecBC.getOpcode() == ISD::AND) { | |||
52012 | auto isConcatenatedNot = [](SDValue V) { | |||
52013 | V = peekThroughBitcasts(V); | |||
52014 | if (!isBitwiseNot(V)) | |||
52015 | return false; | |||
52016 | SDValue NotOp = V->getOperand(0); | |||
52017 | return peekThroughBitcasts(NotOp).getOpcode() == ISD::CONCAT_VECTORS; | |||
52018 | }; | |||
52019 | if (isConcatenatedNot(InVecBC.getOperand(0)) || | |||
52020 | isConcatenatedNot(InVecBC.getOperand(1))) { | |||
52021 | // extract (and v4i64 X, (not (concat Y1, Y2))), n -> andnp v2i64 X(n), Y1 | |||
52022 | SDValue Concat = splitVectorIntBinary(InVecBC, DAG); | |||
52023 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, SDLoc(N), VT, | |||
52024 | DAG.getBitcast(InVecVT, Concat), N->getOperand(1)); | |||
52025 | } | |||
52026 | } | |||
52027 | ||||
52028 | if (DCI.isBeforeLegalizeOps()) | |||
52029 | return SDValue(); | |||
52030 | ||||
52031 | if (SDValue V = narrowExtractedVectorSelect(N, DAG)) | |||
52032 | return V; | |||
52033 | ||||
52034 | if (ISD::isBuildVectorAllZeros(InVec.getNode())) | |||
52035 | return getZeroVector(VT, Subtarget, DAG, SDLoc(N)); | |||
52036 | ||||
52037 | if (ISD::isBuildVectorAllOnes(InVec.getNode())) { | |||
52038 | if (VT.getScalarType() == MVT::i1) | |||
52039 | return DAG.getConstant(1, SDLoc(N), VT); | |||
52040 | return getOnesVector(VT, DAG, SDLoc(N)); | |||
52041 | } | |||
52042 | ||||
52043 | if (InVec.getOpcode() == ISD::BUILD_VECTOR) | |||
52044 | return DAG.getBuildVector( | |||
52045 | VT, SDLoc(N), | |||
52046 | InVec.getNode()->ops().slice(IdxVal, VT.getVectorNumElements())); | |||
52047 | ||||
52048 | // If we are extracting from an insert into a zero vector, replace with a | |||
52049 | // smaller insert into zero if we don't access less than the original | |||
52050 | // subvector. Don't do this for i1 vectors. | |||
52051 | if (VT.getVectorElementType() != MVT::i1 && | |||
52052 | InVec.getOpcode() == ISD::INSERT_SUBVECTOR && IdxVal == 0 && | |||
52053 | InVec.hasOneUse() && isNullConstant(InVec.getOperand(2)) && | |||
52054 | ISD::isBuildVectorAllZeros(InVec.getOperand(0).getNode()) && | |||
52055 | InVec.getOperand(1).getValueSizeInBits() <= SizeInBits) { | |||
52056 | SDLoc DL(N); | |||
52057 | return DAG.getNode(ISD::INSERT_SUBVECTOR, DL, VT, | |||
52058 | getZeroVector(VT, Subtarget, DAG, DL), | |||
52059 | InVec.getOperand(1), InVec.getOperand(2)); | |||
52060 | } | |||
52061 | ||||
52062 | // If we're extracting an upper subvector from a broadcast we should just | |||
52063 | // extract the lowest subvector instead which should allow | |||
52064 | // SimplifyDemandedVectorElts do more simplifications. | |||
52065 | if (IdxVal != 0 && (InVec.getOpcode() == X86ISD::VBROADCAST || | |||
52066 | InVec.getOpcode() == X86ISD::VBROADCAST_LOAD || | |||
52067 | DAG.isSplatValue(InVec, /*AllowUndefs*/ false))) | |||
52068 | return extractSubVector(InVec, 0, DAG, SDLoc(N), SizeInBits); | |||
52069 | ||||
52070 | // If we're extracting a broadcasted subvector, just use the lowest subvector. | |||
52071 | if (IdxVal != 0 && InVec.getOpcode() == X86ISD::SUBV_BROADCAST_LOAD && | |||
52072 | cast<MemIntrinsicSDNode>(InVec)->getMemoryVT() == VT) | |||
52073 | return extractSubVector(InVec, 0, DAG, SDLoc(N), SizeInBits); | |||
52074 | ||||
52075 | // Attempt to extract from the source of a shuffle vector. | |||
52076 | if ((InSizeInBits % SizeInBits) == 0 && | |||
52077 | (IdxVal % VT.getVectorNumElements()) == 0) { | |||
52078 | SmallVector<int, 32> ShuffleMask; | |||
52079 | SmallVector<int, 32> ScaledMask; | |||
52080 | SmallVector<SDValue, 2> ShuffleInputs; | |||
52081 | unsigned NumSubVecs = InSizeInBits / SizeInBits; | |||
52082 | // Decode the shuffle mask and scale it so its shuffling subvectors. | |||
52083 | if (getTargetShuffleInputs(InVecBC, ShuffleInputs, ShuffleMask, DAG) && | |||
52084 | scaleShuffleElements(ShuffleMask, NumSubVecs, ScaledMask)) { | |||
52085 | unsigned SubVecIdx = IdxVal / VT.getVectorNumElements(); | |||
52086 | if (ScaledMask[SubVecIdx] == SM_SentinelUndef) | |||
52087 | return DAG.getUNDEF(VT); | |||
52088 | if (ScaledMask[SubVecIdx] == SM_SentinelZero) | |||
52089 | return getZeroVector(VT, Subtarget, DAG, SDLoc(N)); | |||
52090 | SDValue Src = ShuffleInputs[ScaledMask[SubVecIdx] / NumSubVecs]; | |||
52091 | if (Src.getValueSizeInBits() == InSizeInBits) { | |||
52092 | unsigned SrcSubVecIdx = ScaledMask[SubVecIdx] % NumSubVecs; | |||
52093 | unsigned SrcEltIdx = SrcSubVecIdx * VT.getVectorNumElements(); | |||
52094 | return extractSubVector(DAG.getBitcast(InVecVT, Src), SrcEltIdx, DAG, | |||
52095 | SDLoc(N), SizeInBits); | |||
52096 | } | |||
52097 | } | |||
52098 | } | |||
52099 | ||||
52100 | // If we're extracting the lowest subvector and we're the only user, | |||
52101 | // we may be able to perform this with a smaller vector width. | |||
52102 | unsigned InOpcode = InVec.getOpcode(); | |||
52103 | if (IdxVal == 0 && InVec.hasOneUse()) { | |||
52104 | if (VT == MVT::v2f64 && InVecVT == MVT::v4f64) { | |||
52105 | // v2f64 CVTDQ2PD(v4i32). | |||
52106 | if (InOpcode == ISD::SINT_TO_FP && | |||
52107 | InVec.getOperand(0).getValueType() == MVT::v4i32) { | |||
52108 | return DAG.getNode(X86ISD::CVTSI2P, SDLoc(N), VT, InVec.getOperand(0)); | |||
52109 | } | |||
52110 | // v2f64 CVTUDQ2PD(v4i32). | |||
52111 | if (InOpcode == ISD::UINT_TO_FP && Subtarget.hasVLX() && | |||
52112 | InVec.getOperand(0).getValueType() == MVT::v4i32) { | |||
52113 | return DAG.getNode(X86ISD::CVTUI2P, SDLoc(N), VT, InVec.getOperand(0)); | |||
52114 | } | |||
52115 | // v2f64 CVTPS2PD(v4f32). | |||
52116 | if (InOpcode == ISD::FP_EXTEND && | |||
52117 | InVec.getOperand(0).getValueType() == MVT::v4f32) { | |||
52118 | return DAG.getNode(X86ISD::VFPEXT, SDLoc(N), VT, InVec.getOperand(0)); | |||
52119 | } | |||
52120 | } | |||
52121 | if ((InOpcode == ISD::ANY_EXTEND || | |||
52122 | InOpcode == ISD::ANY_EXTEND_VECTOR_INREG || | |||
52123 | InOpcode == ISD::ZERO_EXTEND || | |||
52124 | InOpcode == ISD::ZERO_EXTEND_VECTOR_INREG || | |||
52125 | InOpcode == ISD::SIGN_EXTEND || | |||
52126 | InOpcode == ISD::SIGN_EXTEND_VECTOR_INREG) && | |||
52127 | (SizeInBits == 128 || SizeInBits == 256) && | |||
52128 | InVec.getOperand(0).getValueSizeInBits() >= SizeInBits) { | |||
52129 | SDLoc DL(N); | |||
52130 | SDValue Ext = InVec.getOperand(0); | |||
52131 | if (Ext.getValueSizeInBits() > SizeInBits) | |||
52132 | Ext = extractSubVector(Ext, 0, DAG, DL, SizeInBits); | |||
52133 | unsigned ExtOp = getOpcode_EXTEND_VECTOR_INREG(InOpcode); | |||
52134 | return DAG.getNode(ExtOp, DL, VT, Ext); | |||
52135 | } | |||
52136 | if (InOpcode == ISD::VSELECT && | |||
52137 | InVec.getOperand(0).getValueType().is256BitVector() && | |||
52138 | InVec.getOperand(1).getValueType().is256BitVector() && | |||
52139 | InVec.getOperand(2).getValueType().is256BitVector()) { | |||
52140 | SDLoc DL(N); | |||
52141 | SDValue Ext0 = extractSubVector(InVec.getOperand(0), 0, DAG, DL, 128); | |||
52142 | SDValue Ext1 = extractSubVector(InVec.getOperand(1), 0, DAG, DL, 128); | |||
52143 | SDValue Ext2 = extractSubVector(InVec.getOperand(2), 0, DAG, DL, 128); | |||
52144 | return DAG.getNode(InOpcode, DL, VT, Ext0, Ext1, Ext2); | |||
52145 | } | |||
52146 | if (InOpcode == ISD::TRUNCATE && Subtarget.hasVLX() && | |||
52147 | (VT.is128BitVector() || VT.is256BitVector())) { | |||
52148 | SDLoc DL(N); | |||
52149 | SDValue InVecSrc = InVec.getOperand(0); | |||
52150 | unsigned Scale = InVecSrc.getValueSizeInBits() / InSizeInBits; | |||
52151 | SDValue Ext = extractSubVector(InVecSrc, 0, DAG, DL, Scale * SizeInBits); | |||
52152 | return DAG.getNode(InOpcode, DL, VT, Ext); | |||
52153 | } | |||
52154 | } | |||
52155 | ||||
52156 | // Always split vXi64 logical shifts where we're extracting the upper 32-bits | |||
52157 | // as this is very likely to fold into a shuffle/truncation. | |||
52158 | if ((InOpcode == X86ISD::VSHLI || InOpcode == X86ISD::VSRLI) && | |||
52159 | InVecVT.getScalarSizeInBits() == 64 && | |||
52160 | InVec.getConstantOperandAPInt(1) == 32) { | |||
52161 | SDLoc DL(N); | |||
52162 | SDValue Ext = | |||
52163 | extractSubVector(InVec.getOperand(0), IdxVal, DAG, DL, SizeInBits); | |||
52164 | return DAG.getNode(InOpcode, DL, VT, Ext, InVec.getOperand(1)); | |||
52165 | } | |||
52166 | ||||
52167 | return SDValue(); | |||
52168 | } | |||
52169 | ||||
52170 | static SDValue combineScalarToVector(SDNode *N, SelectionDAG &DAG) { | |||
52171 | EVT VT = N->getValueType(0); | |||
52172 | SDValue Src = N->getOperand(0); | |||
52173 | SDLoc DL(N); | |||
52174 | ||||
52175 | // If this is a scalar to vector to v1i1 from an AND with 1, bypass the and. | |||
52176 | // This occurs frequently in our masked scalar intrinsic code and our | |||
52177 | // floating point select lowering with AVX512. | |||
52178 | // TODO: SimplifyDemandedBits instead? | |||
52179 | if (VT == MVT::v1i1 && Src.getOpcode() == ISD::AND && Src.hasOneUse()) | |||
52180 | if (auto *C = dyn_cast<ConstantSDNode>(Src.getOperand(1))) | |||
52181 | if (C->getAPIntValue().isOne()) | |||
52182 | return DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, MVT::v1i1, | |||
52183 | Src.getOperand(0)); | |||
52184 | ||||
52185 | // Combine scalar_to_vector of an extract_vector_elt into an extract_subvec. | |||
52186 | if (VT == MVT::v1i1 && Src.getOpcode() == ISD::EXTRACT_VECTOR_ELT && | |||
52187 | Src.hasOneUse() && Src.getOperand(0).getValueType().isVector() && | |||
52188 | Src.getOperand(0).getValueType().getVectorElementType() == MVT::i1) | |||
52189 | if (auto *C = dyn_cast<ConstantSDNode>(Src.getOperand(1))) | |||
52190 | if (C->isZero()) | |||
52191 | return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, Src.getOperand(0), | |||
52192 | Src.getOperand(1)); | |||
52193 | ||||
52194 | // Reduce v2i64 to v4i32 if we don't need the upper bits. | |||
52195 | // TODO: Move to DAGCombine/SimplifyDemandedBits? | |||
52196 | if (VT == MVT::v2i64 || VT == MVT::v2f64) { | |||
52197 | auto IsAnyExt64 = [](SDValue Op) { | |||
52198 | if (Op.getValueType() != MVT::i64 || !Op.hasOneUse()) | |||
52199 | return SDValue(); | |||
52200 | if (Op.getOpcode() == ISD::ANY_EXTEND && | |||
52201 | Op.getOperand(0).getScalarValueSizeInBits() <= 32) | |||
52202 | return Op.getOperand(0); | |||
52203 | if (auto *Ld = dyn_cast<LoadSDNode>(Op)) | |||
52204 | if (Ld->getExtensionType() == ISD::EXTLOAD && | |||
52205 | Ld->getMemoryVT().getScalarSizeInBits() <= 32) | |||
52206 | return Op; | |||
52207 | return SDValue(); | |||
52208 | }; | |||
52209 | if (SDValue ExtSrc = IsAnyExt64(peekThroughOneUseBitcasts(Src))) | |||
52210 | return DAG.getBitcast( | |||
52211 | VT, DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, MVT::v4i32, | |||
52212 | DAG.getAnyExtOrTrunc(ExtSrc, DL, MVT::i32))); | |||
52213 | } | |||
52214 | ||||
52215 | // Combine (v2i64 (scalar_to_vector (i64 (bitconvert (mmx))))) to MOVQ2DQ. | |||
52216 | if (VT == MVT::v2i64 && Src.getOpcode() == ISD::BITCAST && | |||
52217 | Src.getOperand(0).getValueType() == MVT::x86mmx) | |||
52218 | return DAG.getNode(X86ISD::MOVQ2DQ, DL, VT, Src.getOperand(0)); | |||
52219 | ||||
52220 | // See if we're broadcasting the scalar value, in which case just reuse that. | |||
52221 | // Ensure the same SDValue from the SDNode use is being used. | |||
52222 | if (VT.getScalarType() == Src.getValueType()) | |||
52223 | for (SDNode *User : Src->uses()) | |||
52224 | if (User->getOpcode() == X86ISD::VBROADCAST && | |||
52225 | Src == User->getOperand(0)) { | |||
52226 | unsigned SizeInBits = VT.getFixedSizeInBits(); | |||
52227 | unsigned BroadcastSizeInBits = | |||
52228 | User->getValueSizeInBits(0).getFixedSize(); | |||
52229 | if (BroadcastSizeInBits == SizeInBits) | |||
52230 | return SDValue(User, 0); | |||
52231 | if (BroadcastSizeInBits > SizeInBits) | |||
52232 | return extractSubVector(SDValue(User, 0), 0, DAG, DL, SizeInBits); | |||
52233 | // TODO: Handle BroadcastSizeInBits < SizeInBits when we have test | |||
52234 | // coverage. | |||
52235 | } | |||
52236 | ||||
52237 | return SDValue(); | |||
52238 | } | |||
52239 | ||||
52240 | // Simplify PMULDQ and PMULUDQ operations. | |||
52241 | static SDValue combinePMULDQ(SDNode *N, SelectionDAG &DAG, | |||
52242 | TargetLowering::DAGCombinerInfo &DCI, | |||
52243 | const X86Subtarget &Subtarget) { | |||
52244 | SDValue LHS = N->getOperand(0); | |||
52245 | SDValue RHS = N->getOperand(1); | |||
52246 | ||||
52247 | // Canonicalize constant to RHS. | |||
52248 | if (DAG.isConstantIntBuildVectorOrConstantInt(LHS) && | |||
52249 | !DAG.isConstantIntBuildVectorOrConstantInt(RHS)) | |||
52250 | return DAG.getNode(N->getOpcode(), SDLoc(N), N->getValueType(0), RHS, LHS); | |||
52251 | ||||
52252 | // Multiply by zero. | |||
52253 | // Don't return RHS as it may contain UNDEFs. | |||
52254 | if (ISD::isBuildVectorAllZeros(RHS.getNode())) | |||
52255 | return DAG.getConstant(0, SDLoc(N), N->getValueType(0)); | |||
52256 | ||||
52257 | // PMULDQ/PMULUDQ only uses lower 32 bits from each vector element. | |||
52258 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
52259 | if (TLI.SimplifyDemandedBits(SDValue(N, 0), APInt::getAllOnes(64), DCI)) | |||
52260 | return SDValue(N, 0); | |||
52261 | ||||
52262 | // If the input is an extend_invec and the SimplifyDemandedBits call didn't | |||
52263 | // convert it to any_extend_invec, due to the LegalOperations check, do the | |||
52264 | // conversion directly to a vector shuffle manually. This exposes combine | |||
52265 | // opportunities missed by combineEXTEND_VECTOR_INREG not calling | |||
52266 | // combineX86ShufflesRecursively on SSE4.1 targets. | |||
52267 | // FIXME: This is basically a hack around several other issues related to | |||
52268 | // ANY_EXTEND_VECTOR_INREG. | |||
52269 | if (N->getValueType(0) == MVT::v2i64 && LHS.hasOneUse() && | |||
52270 | (LHS.getOpcode() == ISD::ZERO_EXTEND_VECTOR_INREG || | |||
52271 | LHS.getOpcode() == ISD::SIGN_EXTEND_VECTOR_INREG) && | |||
52272 | LHS.getOperand(0).getValueType() == MVT::v4i32) { | |||
52273 | SDLoc dl(N); | |||
52274 | LHS = DAG.getVectorShuffle(MVT::v4i32, dl, LHS.getOperand(0), | |||
52275 | LHS.getOperand(0), { 0, -1, 1, -1 }); | |||
52276 | LHS = DAG.getBitcast(MVT::v2i64, LHS); | |||
52277 | return DAG.getNode(N->getOpcode(), dl, MVT::v2i64, LHS, RHS); | |||
52278 | } | |||
52279 | if (N->getValueType(0) == MVT::v2i64 && RHS.hasOneUse() && | |||
52280 | (RHS.getOpcode() == ISD::ZERO_EXTEND_VECTOR_INREG || | |||
52281 | RHS.getOpcode() == ISD::SIGN_EXTEND_VECTOR_INREG) && | |||
52282 | RHS.getOperand(0).getValueType() == MVT::v4i32) { | |||
52283 | SDLoc dl(N); | |||
52284 | RHS = DAG.getVectorShuffle(MVT::v4i32, dl, RHS.getOperand(0), | |||
52285 | RHS.getOperand(0), { 0, -1, 1, -1 }); | |||
52286 | RHS = DAG.getBitcast(MVT::v2i64, RHS); | |||
52287 | return DAG.getNode(N->getOpcode(), dl, MVT::v2i64, LHS, RHS); | |||
52288 | } | |||
52289 | ||||
52290 | return SDValue(); | |||
52291 | } | |||
52292 | ||||
52293 | // Simplify VPMADDUBSW/VPMADDWD operations. | |||
52294 | static SDValue combineVPMADD(SDNode *N, SelectionDAG &DAG, | |||
52295 | TargetLowering::DAGCombinerInfo &DCI) { | |||
52296 | SDValue LHS = N->getOperand(0); | |||
52297 | SDValue RHS = N->getOperand(1); | |||
52298 | ||||
52299 | // Multiply by zero. | |||
52300 | // Don't return LHS/RHS as it may contain UNDEFs. | |||
52301 | if (ISD::isBuildVectorAllZeros(LHS.getNode()) || | |||
52302 | ISD::isBuildVectorAllZeros(RHS.getNode())) | |||
52303 | return DAG.getConstant(0, SDLoc(N), N->getValueType(0)); | |||
52304 | ||||
52305 | return SDValue(); | |||
52306 | } | |||
52307 | ||||
52308 | static SDValue combineEXTEND_VECTOR_INREG(SDNode *N, SelectionDAG &DAG, | |||
52309 | TargetLowering::DAGCombinerInfo &DCI, | |||
52310 | const X86Subtarget &Subtarget) { | |||
52311 | EVT VT = N->getValueType(0); | |||
52312 | SDValue In = N->getOperand(0); | |||
52313 | unsigned Opcode = N->getOpcode(); | |||
52314 | unsigned InOpcode = In.getOpcode(); | |||
52315 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
52316 | ||||
52317 | // Try to merge vector loads and extend_inreg to an extload. | |||
52318 | if (!DCI.isBeforeLegalizeOps() && ISD::isNormalLoad(In.getNode()) && | |||
52319 | In.hasOneUse()) { | |||
52320 | auto *Ld = cast<LoadSDNode>(In); | |||
52321 | if (Ld->isSimple()) { | |||
52322 | MVT SVT = In.getSimpleValueType().getVectorElementType(); | |||
52323 | ISD::LoadExtType Ext = Opcode == ISD::SIGN_EXTEND_VECTOR_INREG | |||
52324 | ? ISD::SEXTLOAD | |||
52325 | : ISD::ZEXTLOAD; | |||
52326 | EVT MemVT = VT.changeVectorElementType(SVT); | |||
52327 | if (TLI.isLoadExtLegal(Ext, VT, MemVT)) { | |||
52328 | SDValue Load = | |||
52329 | DAG.getExtLoad(Ext, SDLoc(N), VT, Ld->getChain(), Ld->getBasePtr(), | |||
52330 | Ld->getPointerInfo(), MemVT, Ld->getOriginalAlign(), | |||
52331 | Ld->getMemOperand()->getFlags()); | |||
52332 | DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), Load.getValue(1)); | |||
52333 | return Load; | |||
52334 | } | |||
52335 | } | |||
52336 | } | |||
52337 | ||||
52338 | // Fold EXTEND_VECTOR_INREG(EXTEND_VECTOR_INREG(X)) -> EXTEND_VECTOR_INREG(X). | |||
52339 | if (Opcode == InOpcode) | |||
52340 | return DAG.getNode(Opcode, SDLoc(N), VT, In.getOperand(0)); | |||
52341 | ||||
52342 | // Fold EXTEND_VECTOR_INREG(EXTRACT_SUBVECTOR(EXTEND(X),0)) | |||
52343 | // -> EXTEND_VECTOR_INREG(X). | |||
52344 | // TODO: Handle non-zero subvector indices. | |||
52345 | if (InOpcode == ISD::EXTRACT_SUBVECTOR && In.getConstantOperandVal(1) == 0 && | |||
52346 | In.getOperand(0).getOpcode() == getOpcode_EXTEND(Opcode) && | |||
52347 | In.getOperand(0).getOperand(0).getValueSizeInBits() == | |||
52348 | In.getValueSizeInBits()) | |||
52349 | return DAG.getNode(Opcode, SDLoc(N), VT, In.getOperand(0).getOperand(0)); | |||
52350 | ||||
52351 | // Attempt to combine as a shuffle. | |||
52352 | // TODO: General ZERO_EXTEND_VECTOR_INREG support. | |||
52353 | if (Opcode == ISD::ANY_EXTEND_VECTOR_INREG || | |||
52354 | (Opcode == ISD::ZERO_EXTEND_VECTOR_INREG && Subtarget.hasSSE41())) { | |||
52355 | SDValue Op(N, 0); | |||
52356 | if (TLI.isTypeLegal(VT) && TLI.isTypeLegal(In.getValueType())) | |||
52357 | if (SDValue Res = combineX86ShufflesRecursively(Op, DAG, Subtarget)) | |||
52358 | return Res; | |||
52359 | } | |||
52360 | ||||
52361 | return SDValue(); | |||
52362 | } | |||
52363 | ||||
52364 | static SDValue combineKSHIFT(SDNode *N, SelectionDAG &DAG, | |||
52365 | TargetLowering::DAGCombinerInfo &DCI) { | |||
52366 | EVT VT = N->getValueType(0); | |||
52367 | ||||
52368 | if (ISD::isBuildVectorAllZeros(N->getOperand(0).getNode())) | |||
52369 | return DAG.getConstant(0, SDLoc(N), VT); | |||
52370 | ||||
52371 | APInt KnownUndef, KnownZero; | |||
52372 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
52373 | APInt DemandedElts = APInt::getAllOnes(VT.getVectorNumElements()); | |||
52374 | if (TLI.SimplifyDemandedVectorElts(SDValue(N, 0), DemandedElts, KnownUndef, | |||
52375 | KnownZero, DCI)) | |||
52376 | return SDValue(N, 0); | |||
52377 | ||||
52378 | return SDValue(); | |||
52379 | } | |||
52380 | ||||
52381 | // Optimize (fp16_to_fp (fp_to_fp16 X)) to VCVTPS2PH followed by VCVTPH2PS. | |||
52382 | // Done as a combine because the lowering for fp16_to_fp and fp_to_fp16 produce | |||
52383 | // extra instructions between the conversion due to going to scalar and back. | |||
52384 | static SDValue combineFP16_TO_FP(SDNode *N, SelectionDAG &DAG, | |||
52385 | const X86Subtarget &Subtarget) { | |||
52386 | if (Subtarget.useSoftFloat() || !Subtarget.hasF16C()) | |||
52387 | return SDValue(); | |||
52388 | ||||
52389 | if (N->getOperand(0).getOpcode() != ISD::FP_TO_FP16) | |||
52390 | return SDValue(); | |||
52391 | ||||
52392 | if (N->getValueType(0) != MVT::f32 || | |||
52393 | N->getOperand(0).getOperand(0).getValueType() != MVT::f32) | |||
52394 | return SDValue(); | |||
52395 | ||||
52396 | SDLoc dl(N); | |||
52397 | SDValue Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, | |||
52398 | N->getOperand(0).getOperand(0)); | |||
52399 | Res = DAG.getNode(X86ISD::CVTPS2PH, dl, MVT::v8i16, Res, | |||
52400 | DAG.getTargetConstant(4, dl, MVT::i32)); | |||
52401 | Res = DAG.getNode(X86ISD::CVTPH2PS, dl, MVT::v4f32, Res); | |||
52402 | return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, Res, | |||
52403 | DAG.getIntPtrConstant(0, dl)); | |||
52404 | } | |||
52405 | ||||
52406 | static SDValue combineFP_EXTEND(SDNode *N, SelectionDAG &DAG, | |||
52407 | const X86Subtarget &Subtarget) { | |||
52408 | if (!Subtarget.hasF16C() || Subtarget.useSoftFloat()) | |||
52409 | return SDValue(); | |||
52410 | ||||
52411 | if (Subtarget.hasFP16()) | |||
52412 | return SDValue(); | |||
52413 | ||||
52414 | bool IsStrict = N->isStrictFPOpcode(); | |||
52415 | EVT VT = N->getValueType(0); | |||
52416 | SDValue Src = N->getOperand(IsStrict ? 1 : 0); | |||
52417 | EVT SrcVT = Src.getValueType(); | |||
52418 | ||||
52419 | if (!SrcVT.isVector() || SrcVT.getVectorElementType() != MVT::f16) | |||
52420 | return SDValue(); | |||
52421 | ||||
52422 | if (VT.getVectorElementType() != MVT::f32 && | |||
52423 | VT.getVectorElementType() != MVT::f64) | |||
52424 | return SDValue(); | |||
52425 | ||||
52426 | unsigned NumElts = VT.getVectorNumElements(); | |||
52427 | if (NumElts == 1 || !isPowerOf2_32(NumElts)) | |||
52428 | return SDValue(); | |||
52429 | ||||
52430 | SDLoc dl(N); | |||
52431 | ||||
52432 | // Convert the input to vXi16. | |||
52433 | EVT IntVT = SrcVT.changeVectorElementTypeToInteger(); | |||
52434 | Src = DAG.getBitcast(IntVT, Src); | |||
52435 | ||||
52436 | // Widen to at least 8 input elements. | |||
52437 | if (NumElts < 8) { | |||
52438 | unsigned NumConcats = 8 / NumElts; | |||
52439 | SDValue Fill = NumElts == 4 ? DAG.getUNDEF(IntVT) | |||
52440 | : DAG.getConstant(0, dl, IntVT); | |||
52441 | SmallVector<SDValue, 4> Ops(NumConcats, Fill); | |||
52442 | Ops[0] = Src; | |||
52443 | Src = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v8i16, Ops); | |||
52444 | } | |||
52445 | ||||
52446 | // Destination is vXf32 with at least 4 elements. | |||
52447 | EVT CvtVT = EVT::getVectorVT(*DAG.getContext(), MVT::f32, | |||
52448 | std::max(4U, NumElts)); | |||
52449 | SDValue Cvt, Chain; | |||
52450 | if (IsStrict) { | |||
52451 | Cvt = DAG.getNode(X86ISD::STRICT_CVTPH2PS, dl, {CvtVT, MVT::Other}, | |||
52452 | {N->getOperand(0), Src}); | |||
52453 | Chain = Cvt.getValue(1); | |||
52454 | } else { | |||
52455 | Cvt = DAG.getNode(X86ISD::CVTPH2PS, dl, CvtVT, Src); | |||
52456 | } | |||
52457 | ||||
52458 | if (NumElts < 4) { | |||
52459 | assert(NumElts == 2 && "Unexpected size")(static_cast <bool> (NumElts == 2 && "Unexpected size" ) ? void (0) : __assert_fail ("NumElts == 2 && \"Unexpected size\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 52459, __extension__ __PRETTY_FUNCTION__)); | |||
52460 | Cvt = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MVT::v2f32, Cvt, | |||
52461 | DAG.getIntPtrConstant(0, dl)); | |||
52462 | } | |||
52463 | ||||
52464 | if (IsStrict) { | |||
52465 | // Extend to the original VT if necessary. | |||
52466 | if (Cvt.getValueType() != VT) { | |||
52467 | Cvt = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {VT, MVT::Other}, | |||
52468 | {Chain, Cvt}); | |||
52469 | Chain = Cvt.getValue(1); | |||
52470 | } | |||
52471 | return DAG.getMergeValues({Cvt, Chain}, dl); | |||
52472 | } | |||
52473 | ||||
52474 | // Extend to the original VT if necessary. | |||
52475 | return DAG.getNode(ISD::FP_EXTEND, dl, VT, Cvt); | |||
52476 | } | |||
52477 | ||||
52478 | // Try to find a larger VBROADCAST_LOAD/SUBV_BROADCAST_LOAD that we can extract | |||
52479 | // from. Limit this to cases where the loads have the same input chain and the | |||
52480 | // output chains are unused. This avoids any memory ordering issues. | |||
52481 | static SDValue combineBROADCAST_LOAD(SDNode *N, SelectionDAG &DAG, | |||
52482 | TargetLowering::DAGCombinerInfo &DCI) { | |||
52483 | assert((N->getOpcode() == X86ISD::VBROADCAST_LOAD ||(static_cast <bool> ((N->getOpcode() == X86ISD::VBROADCAST_LOAD || N->getOpcode() == X86ISD::SUBV_BROADCAST_LOAD) && "Unknown broadcast load type") ? void (0) : __assert_fail ("(N->getOpcode() == X86ISD::VBROADCAST_LOAD || N->getOpcode() == X86ISD::SUBV_BROADCAST_LOAD) && \"Unknown broadcast load type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 52485, __extension__ __PRETTY_FUNCTION__)) | |||
52484 | N->getOpcode() == X86ISD::SUBV_BROADCAST_LOAD) &&(static_cast <bool> ((N->getOpcode() == X86ISD::VBROADCAST_LOAD || N->getOpcode() == X86ISD::SUBV_BROADCAST_LOAD) && "Unknown broadcast load type") ? void (0) : __assert_fail ("(N->getOpcode() == X86ISD::VBROADCAST_LOAD || N->getOpcode() == X86ISD::SUBV_BROADCAST_LOAD) && \"Unknown broadcast load type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 52485, __extension__ __PRETTY_FUNCTION__)) | |||
52485 | "Unknown broadcast load type")(static_cast <bool> ((N->getOpcode() == X86ISD::VBROADCAST_LOAD || N->getOpcode() == X86ISD::SUBV_BROADCAST_LOAD) && "Unknown broadcast load type") ? void (0) : __assert_fail ("(N->getOpcode() == X86ISD::VBROADCAST_LOAD || N->getOpcode() == X86ISD::SUBV_BROADCAST_LOAD) && \"Unknown broadcast load type\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 52485, __extension__ __PRETTY_FUNCTION__)); | |||
52486 | ||||
52487 | // Only do this if the chain result is unused. | |||
52488 | if (N->hasAnyUseOfValue(1)) | |||
52489 | return SDValue(); | |||
52490 | ||||
52491 | auto *MemIntrin = cast<MemIntrinsicSDNode>(N); | |||
52492 | ||||
52493 | SDValue Ptr = MemIntrin->getBasePtr(); | |||
52494 | SDValue Chain = MemIntrin->getChain(); | |||
52495 | EVT VT = N->getSimpleValueType(0); | |||
52496 | EVT MemVT = MemIntrin->getMemoryVT(); | |||
52497 | ||||
52498 | // Look at other users of our base pointer and try to find a wider broadcast. | |||
52499 | // The input chain and the size of the memory VT must match. | |||
52500 | for (SDNode *User : Ptr->uses()) | |||
52501 | if (User != N && User->getOpcode() == N->getOpcode() && | |||
52502 | cast<MemIntrinsicSDNode>(User)->getBasePtr() == Ptr && | |||
52503 | cast<MemIntrinsicSDNode>(User)->getChain() == Chain && | |||
52504 | cast<MemIntrinsicSDNode>(User)->getMemoryVT().getSizeInBits() == | |||
52505 | MemVT.getSizeInBits() && | |||
52506 | !User->hasAnyUseOfValue(1) && | |||
52507 | User->getValueSizeInBits(0).getFixedSize() > VT.getFixedSizeInBits()) { | |||
52508 | SDValue Extract = extractSubVector(SDValue(User, 0), 0, DAG, SDLoc(N), | |||
52509 | VT.getSizeInBits()); | |||
52510 | Extract = DAG.getBitcast(VT, Extract); | |||
52511 | return DCI.CombineTo(N, Extract, SDValue(User, 1)); | |||
52512 | } | |||
52513 | ||||
52514 | return SDValue(); | |||
52515 | } | |||
52516 | ||||
52517 | static SDValue combineFP_ROUND(SDNode *N, SelectionDAG &DAG, | |||
52518 | const X86Subtarget &Subtarget) { | |||
52519 | if (!Subtarget.hasF16C() || Subtarget.useSoftFloat()) | |||
52520 | return SDValue(); | |||
52521 | ||||
52522 | if (Subtarget.hasFP16()) | |||
52523 | return SDValue(); | |||
52524 | ||||
52525 | EVT VT = N->getValueType(0); | |||
52526 | SDValue Src = N->getOperand(0); | |||
52527 | EVT SrcVT = Src.getValueType(); | |||
52528 | ||||
52529 | if (!VT.isVector() || VT.getVectorElementType() != MVT::f16 || | |||
52530 | SrcVT.getVectorElementType() != MVT::f32) | |||
52531 | return SDValue(); | |||
52532 | ||||
52533 | unsigned NumElts = VT.getVectorNumElements(); | |||
52534 | if (NumElts == 1 || !isPowerOf2_32(NumElts)) | |||
52535 | return SDValue(); | |||
52536 | ||||
52537 | SDLoc dl(N); | |||
52538 | ||||
52539 | // Widen to at least 4 input elements. | |||
52540 | if (NumElts < 4) | |||
52541 | Src = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v4f32, Src, | |||
52542 | DAG.getConstantFP(0.0, dl, SrcVT)); | |||
52543 | ||||
52544 | // Destination is v8i16 with at least 8 elements. | |||
52545 | EVT CvtVT = EVT::getVectorVT(*DAG.getContext(), MVT::i16, | |||
52546 | std::max(8U, NumElts)); | |||
52547 | SDValue Cvt = DAG.getNode(X86ISD::CVTPS2PH, dl, CvtVT, Src, | |||
52548 | DAG.getTargetConstant(4, dl, MVT::i32)); | |||
52549 | ||||
52550 | // Extract down to real number of elements. | |||
52551 | if (NumElts < 8) { | |||
52552 | EVT IntVT = VT.changeVectorElementTypeToInteger(); | |||
52553 | Cvt = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, IntVT, Cvt, | |||
52554 | DAG.getIntPtrConstant(0, dl)); | |||
52555 | } | |||
52556 | ||||
52557 | return DAG.getBitcast(VT, Cvt); | |||
52558 | } | |||
52559 | ||||
52560 | static SDValue combineMOVDQ2Q(SDNode *N, SelectionDAG &DAG) { | |||
52561 | SDValue Src = N->getOperand(0); | |||
52562 | ||||
52563 | // Turn MOVDQ2Q+simple_load into an mmx load. | |||
52564 | if (ISD::isNormalLoad(Src.getNode()) && Src.hasOneUse()) { | |||
52565 | LoadSDNode *LN = cast<LoadSDNode>(Src.getNode()); | |||
52566 | ||||
52567 | if (LN->isSimple()) { | |||
52568 | SDValue NewLd = DAG.getLoad(MVT::x86mmx, SDLoc(N), LN->getChain(), | |||
52569 | LN->getBasePtr(), | |||
52570 | LN->getPointerInfo(), | |||
52571 | LN->getOriginalAlign(), | |||
52572 | LN->getMemOperand()->getFlags()); | |||
52573 | DAG.ReplaceAllUsesOfValueWith(SDValue(LN, 1), NewLd.getValue(1)); | |||
52574 | return NewLd; | |||
52575 | } | |||
52576 | } | |||
52577 | ||||
52578 | return SDValue(); | |||
52579 | } | |||
52580 | ||||
52581 | static SDValue combinePDEP(SDNode *N, SelectionDAG &DAG, | |||
52582 | TargetLowering::DAGCombinerInfo &DCI) { | |||
52583 | unsigned NumBits = N->getSimpleValueType(0).getSizeInBits(); | |||
52584 | const TargetLowering &TLI = DAG.getTargetLoweringInfo(); | |||
52585 | if (TLI.SimplifyDemandedBits(SDValue(N, 0), APInt::getAllOnes(NumBits), DCI)) | |||
52586 | return SDValue(N, 0); | |||
52587 | ||||
52588 | return SDValue(); | |||
52589 | } | |||
52590 | ||||
52591 | SDValue X86TargetLowering::PerformDAGCombine(SDNode *N, | |||
52592 | DAGCombinerInfo &DCI) const { | |||
52593 | SelectionDAG &DAG = DCI.DAG; | |||
52594 | switch (N->getOpcode()) { | |||
52595 | default: break; | |||
52596 | case ISD::SCALAR_TO_VECTOR: | |||
52597 | return combineScalarToVector(N, DAG); | |||
52598 | case ISD::EXTRACT_VECTOR_ELT: | |||
52599 | case X86ISD::PEXTRW: | |||
52600 | case X86ISD::PEXTRB: | |||
52601 | return combineExtractVectorElt(N, DAG, DCI, Subtarget); | |||
52602 | case ISD::CONCAT_VECTORS: | |||
52603 | return combineConcatVectors(N, DAG, DCI, Subtarget); | |||
52604 | case ISD::INSERT_SUBVECTOR: | |||
52605 | return combineInsertSubvector(N, DAG, DCI, Subtarget); | |||
52606 | case ISD::EXTRACT_SUBVECTOR: | |||
52607 | return combineExtractSubvector(N, DAG, DCI, Subtarget); | |||
52608 | case ISD::VSELECT: | |||
52609 | case ISD::SELECT: | |||
52610 | case X86ISD::BLENDV: return combineSelect(N, DAG, DCI, Subtarget); | |||
52611 | case ISD::BITCAST: return combineBitcast(N, DAG, DCI, Subtarget); | |||
52612 | case X86ISD::CMOV: return combineCMov(N, DAG, DCI, Subtarget); | |||
52613 | case X86ISD::CMP: return combineCMP(N, DAG); | |||
52614 | case ISD::ADD: return combineAdd(N, DAG, DCI, Subtarget); | |||
52615 | case ISD::SUB: return combineSub(N, DAG, DCI, Subtarget); | |||
52616 | case X86ISD::ADD: | |||
52617 | case X86ISD::SUB: return combineX86AddSub(N, DAG, DCI); | |||
52618 | case X86ISD::SBB: return combineSBB(N, DAG); | |||
52619 | case X86ISD::ADC: return combineADC(N, DAG, DCI); | |||
52620 | case ISD::MUL: return combineMul(N, DAG, DCI, Subtarget); | |||
52621 | case ISD::SHL: return combineShiftLeft(N, DAG); | |||
52622 | case ISD::SRA: return combineShiftRightArithmetic(N, DAG, Subtarget); | |||
52623 | case ISD::SRL: return combineShiftRightLogical(N, DAG, DCI, Subtarget); | |||
52624 | case ISD::AND: return combineAnd(N, DAG, DCI, Subtarget); | |||
52625 | case ISD::OR: return combineOr(N, DAG, DCI, Subtarget); | |||
52626 | case ISD::XOR: return combineXor(N, DAG, DCI, Subtarget); | |||
52627 | case X86ISD::BEXTR: | |||
52628 | case X86ISD::BEXTRI: return combineBEXTR(N, DAG, DCI, Subtarget); | |||
52629 | case ISD::LOAD: return combineLoad(N, DAG, DCI, Subtarget); | |||
52630 | case ISD::MLOAD: return combineMaskedLoad(N, DAG, DCI, Subtarget); | |||
52631 | case ISD::STORE: return combineStore(N, DAG, DCI, Subtarget); | |||
52632 | case ISD::MSTORE: return combineMaskedStore(N, DAG, DCI, Subtarget); | |||
52633 | case X86ISD::VEXTRACT_STORE: | |||
52634 | return combineVEXTRACT_STORE(N, DAG, DCI, Subtarget); | |||
52635 | case ISD::SINT_TO_FP: | |||
52636 | case ISD::STRICT_SINT_TO_FP: | |||
52637 | return combineSIntToFP(N, DAG, DCI, Subtarget); | |||
52638 | case ISD::UINT_TO_FP: | |||
52639 | case ISD::STRICT_UINT_TO_FP: | |||
52640 | return combineUIntToFP(N, DAG, Subtarget); | |||
52641 | case ISD::FADD: | |||
52642 | case ISD::FSUB: return combineFaddFsub(N, DAG, Subtarget); | |||
52643 | case X86ISD::VFCMULC: | |||
52644 | case X86ISD::VFMULC: return combineFMulcFCMulc(N, DAG, Subtarget); | |||
52645 | case ISD::FNEG: return combineFneg(N, DAG, DCI, Subtarget); | |||
52646 | case ISD::TRUNCATE: return combineTruncate(N, DAG, Subtarget); | |||
52647 | case X86ISD::VTRUNC: return combineVTRUNC(N, DAG, DCI); | |||
52648 | case X86ISD::ANDNP: return combineAndnp(N, DAG, DCI, Subtarget); | |||
52649 | case X86ISD::FAND: return combineFAnd(N, DAG, Subtarget); | |||
52650 | case X86ISD::FANDN: return combineFAndn(N, DAG, Subtarget); | |||
52651 | case X86ISD::FXOR: | |||
52652 | case X86ISD::FOR: return combineFOr(N, DAG, DCI, Subtarget); | |||
52653 | case X86ISD::FMIN: | |||
52654 | case X86ISD::FMAX: return combineFMinFMax(N, DAG); | |||
52655 | case ISD::FMINNUM: | |||
52656 | case ISD::FMAXNUM: return combineFMinNumFMaxNum(N, DAG, Subtarget); | |||
52657 | case X86ISD::CVTSI2P: | |||
52658 | case X86ISD::CVTUI2P: return combineX86INT_TO_FP(N, DAG, DCI); | |||
52659 | case X86ISD::CVTP2SI: | |||
52660 | case X86ISD::CVTP2UI: | |||
52661 | case X86ISD::STRICT_CVTTP2SI: | |||
52662 | case X86ISD::CVTTP2SI: | |||
52663 | case X86ISD::STRICT_CVTTP2UI: | |||
52664 | case X86ISD::CVTTP2UI: | |||
52665 | return combineCVTP2I_CVTTP2I(N, DAG, DCI); | |||
52666 | case X86ISD::STRICT_CVTPH2PS: | |||
52667 | case X86ISD::CVTPH2PS: return combineCVTPH2PS(N, DAG, DCI); | |||
52668 | case X86ISD::BT: return combineBT(N, DAG, DCI); | |||
52669 | case ISD::ANY_EXTEND: | |||
52670 | case ISD::ZERO_EXTEND: return combineZext(N, DAG, DCI, Subtarget); | |||
52671 | case ISD::SIGN_EXTEND: return combineSext(N, DAG, DCI, Subtarget); | |||
52672 | case ISD::SIGN_EXTEND_INREG: return combineSignExtendInReg(N, DAG, Subtarget); | |||
52673 | case ISD::ANY_EXTEND_VECTOR_INREG: | |||
52674 | case ISD::SIGN_EXTEND_VECTOR_INREG: | |||
52675 | case ISD::ZERO_EXTEND_VECTOR_INREG: | |||
52676 | return combineEXTEND_VECTOR_INREG(N, DAG, DCI, Subtarget); | |||
52677 | case ISD::SETCC: return combineSetCC(N, DAG, DCI, Subtarget); | |||
52678 | case X86ISD::SETCC: return combineX86SetCC(N, DAG, Subtarget); | |||
52679 | case X86ISD::BRCOND: return combineBrCond(N, DAG, Subtarget); | |||
52680 | case X86ISD::PACKSS: | |||
52681 | case X86ISD::PACKUS: return combineVectorPack(N, DAG, DCI, Subtarget); | |||
52682 | case X86ISD::HADD: | |||
52683 | case X86ISD::HSUB: | |||
52684 | case X86ISD::FHADD: | |||
52685 | case X86ISD::FHSUB: return combineVectorHADDSUB(N, DAG, DCI, Subtarget); | |||
52686 | case X86ISD::VSHL: | |||
52687 | case X86ISD::VSRA: | |||
52688 | case X86ISD::VSRL: | |||
52689 | return combineVectorShiftVar(N, DAG, DCI, Subtarget); | |||
52690 | case X86ISD::VSHLI: | |||
52691 | case X86ISD::VSRAI: | |||
52692 | case X86ISD::VSRLI: | |||
52693 | return combineVectorShiftImm(N, DAG, DCI, Subtarget); | |||
52694 | case ISD::INSERT_VECTOR_ELT: | |||
52695 | case X86ISD::PINSRB: | |||
52696 | case X86ISD::PINSRW: return combineVectorInsert(N, DAG, DCI, Subtarget); | |||
52697 | case X86ISD::SHUFP: // Handle all target specific shuffles | |||
52698 | case X86ISD::INSERTPS: | |||
52699 | case X86ISD::EXTRQI: | |||
52700 | case X86ISD::INSERTQI: | |||
52701 | case X86ISD::VALIGN: | |||
52702 | case X86ISD::PALIGNR: | |||
52703 | case X86ISD::VSHLDQ: | |||
52704 | case X86ISD::VSRLDQ: | |||
52705 | case X86ISD::BLENDI: | |||
52706 | case X86ISD::UNPCKH: | |||
52707 | case X86ISD::UNPCKL: | |||
52708 | case X86ISD::MOVHLPS: | |||
52709 | case X86ISD::MOVLHPS: | |||
52710 | case X86ISD::PSHUFB: | |||
52711 | case X86ISD::PSHUFD: | |||
52712 | case X86ISD::PSHUFHW: | |||
52713 | case X86ISD::PSHUFLW: | |||
52714 | case X86ISD::MOVSHDUP: | |||
52715 | case X86ISD::MOVSLDUP: | |||
52716 | case X86ISD::MOVDDUP: | |||
52717 | case X86ISD::MOVSS: | |||
52718 | case X86ISD::MOVSD: | |||
52719 | case X86ISD::MOVSH: | |||
52720 | case X86ISD::VBROADCAST: | |||
52721 | case X86ISD::VPPERM: | |||
52722 | case X86ISD::VPERMI: | |||
52723 | case X86ISD::VPERMV: | |||
52724 | case X86ISD::VPERMV3: | |||
52725 | case X86ISD::VPERMIL2: | |||
52726 | case X86ISD::VPERMILPI: | |||
52727 | case X86ISD::VPERMILPV: | |||
52728 | case X86ISD::VPERM2X128: | |||
52729 | case X86ISD::SHUF128: | |||
52730 | case X86ISD::VZEXT_MOVL: | |||
52731 | case ISD::VECTOR_SHUFFLE: return combineShuffle(N, DAG, DCI,Subtarget); | |||
52732 | case X86ISD::FMADD_RND: | |||
52733 | case X86ISD::FMSUB: | |||
52734 | case X86ISD::STRICT_FMSUB: | |||
52735 | case X86ISD::FMSUB_RND: | |||
52736 | case X86ISD::FNMADD: | |||
52737 | case X86ISD::STRICT_FNMADD: | |||
52738 | case X86ISD::FNMADD_RND: | |||
52739 | case X86ISD::FNMSUB: | |||
52740 | case X86ISD::STRICT_FNMSUB: | |||
52741 | case X86ISD::FNMSUB_RND: | |||
52742 | case ISD::FMA: | |||
52743 | case ISD::STRICT_FMA: return combineFMA(N, DAG, DCI, Subtarget); | |||
52744 | case X86ISD::FMADDSUB_RND: | |||
52745 | case X86ISD::FMSUBADD_RND: | |||
52746 | case X86ISD::FMADDSUB: | |||
52747 | case X86ISD::FMSUBADD: return combineFMADDSUB(N, DAG, DCI); | |||
52748 | case X86ISD::MOVMSK: return combineMOVMSK(N, DAG, DCI, Subtarget); | |||
52749 | case X86ISD::MGATHER: | |||
52750 | case X86ISD::MSCATTER: return combineX86GatherScatter(N, DAG, DCI); | |||
52751 | case ISD::MGATHER: | |||
52752 | case ISD::MSCATTER: return combineGatherScatter(N, DAG, DCI); | |||
52753 | case X86ISD::PCMPEQ: | |||
52754 | case X86ISD::PCMPGT: return combineVectorCompare(N, DAG, Subtarget); | |||
52755 | case X86ISD::PMULDQ: | |||
52756 | case X86ISD::PMULUDQ: return combinePMULDQ(N, DAG, DCI, Subtarget); | |||
52757 | case X86ISD::VPMADDUBSW: | |||
52758 | case X86ISD::VPMADDWD: return combineVPMADD(N, DAG, DCI); | |||
52759 | case X86ISD::KSHIFTL: | |||
52760 | case X86ISD::KSHIFTR: return combineKSHIFT(N, DAG, DCI); | |||
52761 | case ISD::FP16_TO_FP: return combineFP16_TO_FP(N, DAG, Subtarget); | |||
52762 | case ISD::STRICT_FP_EXTEND: | |||
52763 | case ISD::FP_EXTEND: return combineFP_EXTEND(N, DAG, Subtarget); | |||
52764 | case ISD::FP_ROUND: return combineFP_ROUND(N, DAG, Subtarget); | |||
52765 | case X86ISD::VBROADCAST_LOAD: | |||
52766 | case X86ISD::SUBV_BROADCAST_LOAD: return combineBROADCAST_LOAD(N, DAG, DCI); | |||
52767 | case X86ISD::MOVDQ2Q: return combineMOVDQ2Q(N, DAG); | |||
52768 | case X86ISD::PDEP: return combinePDEP(N, DAG, DCI); | |||
52769 | } | |||
52770 | ||||
52771 | return SDValue(); | |||
52772 | } | |||
52773 | ||||
52774 | bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const { | |||
52775 | if (!isTypeLegal(VT)) | |||
52776 | return false; | |||
52777 | ||||
52778 | // There are no vXi8 shifts. | |||
52779 | if (Opc == ISD::SHL && VT.isVector() && VT.getVectorElementType() == MVT::i8) | |||
52780 | return false; | |||
52781 | ||||
52782 | // TODO: Almost no 8-bit ops are desirable because they have no actual | |||
52783 | // size/speed advantages vs. 32-bit ops, but they do have a major | |||
52784 | // potential disadvantage by causing partial register stalls. | |||
52785 | // | |||
52786 | // 8-bit multiply/shl is probably not cheaper than 32-bit multiply/shl, and | |||
52787 | // we have specializations to turn 32-bit multiply/shl into LEA or other ops. | |||
52788 | // Also, see the comment in "IsDesirableToPromoteOp" - where we additionally | |||
52789 | // check for a constant operand to the multiply. | |||
52790 | if ((Opc == ISD::MUL || Opc == ISD::SHL) && VT == MVT::i8) | |||
52791 | return false; | |||
52792 | ||||
52793 | // i16 instruction encodings are longer and some i16 instructions are slow, | |||
52794 | // so those are not desirable. | |||
52795 | if (VT == MVT::i16) { | |||
52796 | switch (Opc) { | |||
52797 | default: | |||
52798 | break; | |||
52799 | case ISD::LOAD: | |||
52800 | case ISD::SIGN_EXTEND: | |||
52801 | case ISD::ZERO_EXTEND: | |||
52802 | case ISD::ANY_EXTEND: | |||
52803 | case ISD::SHL: | |||
52804 | case ISD::SRA: | |||
52805 | case ISD::SRL: | |||
52806 | case ISD::SUB: | |||
52807 | case ISD::ADD: | |||
52808 | case ISD::MUL: | |||
52809 | case ISD::AND: | |||
52810 | case ISD::OR: | |||
52811 | case ISD::XOR: | |||
52812 | return false; | |||
52813 | } | |||
52814 | } | |||
52815 | ||||
52816 | // Any legal type not explicitly accounted for above here is desirable. | |||
52817 | return true; | |||
52818 | } | |||
52819 | ||||
52820 | SDValue X86TargetLowering::expandIndirectJTBranch(const SDLoc& dl, | |||
52821 | SDValue Value, SDValue Addr, | |||
52822 | SelectionDAG &DAG) const { | |||
52823 | const Module *M = DAG.getMachineFunction().getMMI().getModule(); | |||
52824 | Metadata *IsCFProtectionSupported = M->getModuleFlag("cf-protection-branch"); | |||
52825 | if (IsCFProtectionSupported) { | |||
52826 | // In case control-flow branch protection is enabled, we need to add | |||
52827 | // notrack prefix to the indirect branch. | |||
52828 | // In order to do that we create NT_BRIND SDNode. | |||
52829 | // Upon ISEL, the pattern will convert it to jmp with NoTrack prefix. | |||
52830 | return DAG.getNode(X86ISD::NT_BRIND, dl, MVT::Other, Value, Addr); | |||
52831 | } | |||
52832 | ||||
52833 | return TargetLowering::expandIndirectJTBranch(dl, Value, Addr, DAG); | |||
52834 | } | |||
52835 | ||||
52836 | bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const { | |||
52837 | EVT VT = Op.getValueType(); | |||
52838 | bool Is8BitMulByConstant = VT == MVT::i8 && Op.getOpcode() == ISD::MUL && | |||
52839 | isa<ConstantSDNode>(Op.getOperand(1)); | |||
52840 | ||||
52841 | // i16 is legal, but undesirable since i16 instruction encodings are longer | |||
52842 | // and some i16 instructions are slow. | |||
52843 | // 8-bit multiply-by-constant can usually be expanded to something cheaper | |||
52844 | // using LEA and/or other ALU ops. | |||
52845 | if (VT != MVT::i16 && !Is8BitMulByConstant) | |||
52846 | return false; | |||
52847 | ||||
52848 | auto IsFoldableRMW = [](SDValue Load, SDValue Op) { | |||
52849 | if (!Op.hasOneUse()) | |||
52850 | return false; | |||
52851 | SDNode *User = *Op->use_begin(); | |||
52852 | if (!ISD::isNormalStore(User)) | |||
52853 | return false; | |||
52854 | auto *Ld = cast<LoadSDNode>(Load); | |||
52855 | auto *St = cast<StoreSDNode>(User); | |||
52856 | return Ld->getBasePtr() == St->getBasePtr(); | |||
52857 | }; | |||
52858 | ||||
52859 | auto IsFoldableAtomicRMW = [](SDValue Load, SDValue Op) { | |||
52860 | if (!Load.hasOneUse() || Load.getOpcode() != ISD::ATOMIC_LOAD) | |||
52861 | return false; | |||
52862 | if (!Op.hasOneUse()) | |||
52863 | return false; | |||
52864 | SDNode *User = *Op->use_begin(); | |||
52865 | if (User->getOpcode() != ISD::ATOMIC_STORE) | |||
52866 | return false; | |||
52867 | auto *Ld = cast<AtomicSDNode>(Load); | |||
52868 | auto *St = cast<AtomicSDNode>(User); | |||
52869 | return Ld->getBasePtr() == St->getBasePtr(); | |||
52870 | }; | |||
52871 | ||||
52872 | bool Commute = false; | |||
52873 | switch (Op.getOpcode()) { | |||
52874 | default: return false; | |||
52875 | case ISD::SIGN_EXTEND: | |||
52876 | case ISD::ZERO_EXTEND: | |||
52877 | case ISD::ANY_EXTEND: | |||
52878 | break; | |||
52879 | case ISD::SHL: | |||
52880 | case ISD::SRA: | |||
52881 | case ISD::SRL: { | |||
52882 | SDValue N0 = Op.getOperand(0); | |||
52883 | // Look out for (store (shl (load), x)). | |||
52884 | if (MayFoldLoad(N0) && IsFoldableRMW(N0, Op)) | |||
52885 | return false; | |||
52886 | break; | |||
52887 | } | |||
52888 | case ISD::ADD: | |||
52889 | case ISD::MUL: | |||
52890 | case ISD::AND: | |||
52891 | case ISD::OR: | |||
52892 | case ISD::XOR: | |||
52893 | Commute = true; | |||
52894 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
52895 | case ISD::SUB: { | |||
52896 | SDValue N0 = Op.getOperand(0); | |||
52897 | SDValue N1 = Op.getOperand(1); | |||
52898 | // Avoid disabling potential load folding opportunities. | |||
52899 | if (MayFoldLoad(N1) && | |||
52900 | (!Commute || !isa<ConstantSDNode>(N0) || | |||
52901 | (Op.getOpcode() != ISD::MUL && IsFoldableRMW(N1, Op)))) | |||
52902 | return false; | |||
52903 | if (MayFoldLoad(N0) && | |||
52904 | ((Commute && !isa<ConstantSDNode>(N1)) || | |||
52905 | (Op.getOpcode() != ISD::MUL && IsFoldableRMW(N0, Op)))) | |||
52906 | return false; | |||
52907 | if (IsFoldableAtomicRMW(N0, Op) || | |||
52908 | (Commute && IsFoldableAtomicRMW(N1, Op))) | |||
52909 | return false; | |||
52910 | } | |||
52911 | } | |||
52912 | ||||
52913 | PVT = MVT::i32; | |||
52914 | return true; | |||
52915 | } | |||
52916 | ||||
52917 | //===----------------------------------------------------------------------===// | |||
52918 | // X86 Inline Assembly Support | |||
52919 | //===----------------------------------------------------------------------===// | |||
52920 | ||||
52921 | // Helper to match a string separated by whitespace. | |||
52922 | static bool matchAsm(StringRef S, ArrayRef<const char *> Pieces) { | |||
52923 | S = S.substr(S.find_first_not_of(" \t")); // Skip leading whitespace. | |||
52924 | ||||
52925 | for (StringRef Piece : Pieces) { | |||
52926 | if (!S.startswith(Piece)) // Check if the piece matches. | |||
52927 | return false; | |||
52928 | ||||
52929 | S = S.substr(Piece.size()); | |||
52930 | StringRef::size_type Pos = S.find_first_not_of(" \t"); | |||
52931 | if (Pos == 0) // We matched a prefix. | |||
52932 | return false; | |||
52933 | ||||
52934 | S = S.substr(Pos); | |||
52935 | } | |||
52936 | ||||
52937 | return S.empty(); | |||
52938 | } | |||
52939 | ||||
52940 | static bool clobbersFlagRegisters(const SmallVector<StringRef, 4> &AsmPieces) { | |||
52941 | ||||
52942 | if (AsmPieces.size() == 3 || AsmPieces.size() == 4) { | |||
52943 | if (llvm::is_contained(AsmPieces, "~{cc}") && | |||
52944 | llvm::is_contained(AsmPieces, "~{flags}") && | |||
52945 | llvm::is_contained(AsmPieces, "~{fpsr}")) { | |||
52946 | ||||
52947 | if (AsmPieces.size() == 3) | |||
52948 | return true; | |||
52949 | else if (llvm::is_contained(AsmPieces, "~{dirflag}")) | |||
52950 | return true; | |||
52951 | } | |||
52952 | } | |||
52953 | return false; | |||
52954 | } | |||
52955 | ||||
52956 | bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const { | |||
52957 | InlineAsm *IA = cast<InlineAsm>(CI->getCalledOperand()); | |||
52958 | ||||
52959 | const std::string &AsmStr = IA->getAsmString(); | |||
52960 | ||||
52961 | IntegerType *Ty = dyn_cast<IntegerType>(CI->getType()); | |||
52962 | if (!Ty || Ty->getBitWidth() % 16 != 0) | |||
52963 | return false; | |||
52964 | ||||
52965 | // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a" | |||
52966 | SmallVector<StringRef, 4> AsmPieces; | |||
52967 | SplitString(AsmStr, AsmPieces, ";\n"); | |||
52968 | ||||
52969 | switch (AsmPieces.size()) { | |||
52970 | default: return false; | |||
52971 | case 1: | |||
52972 | // FIXME: this should verify that we are targeting a 486 or better. If not, | |||
52973 | // we will turn this bswap into something that will be lowered to logical | |||
52974 | // ops instead of emitting the bswap asm. For now, we don't support 486 or | |||
52975 | // lower so don't worry about this. | |||
52976 | // bswap $0 | |||
52977 | if (matchAsm(AsmPieces[0], {"bswap", "$0"}) || | |||
52978 | matchAsm(AsmPieces[0], {"bswapl", "$0"}) || | |||
52979 | matchAsm(AsmPieces[0], {"bswapq", "$0"}) || | |||
52980 | matchAsm(AsmPieces[0], {"bswap", "${0:q}"}) || | |||
52981 | matchAsm(AsmPieces[0], {"bswapl", "${0:q}"}) || | |||
52982 | matchAsm(AsmPieces[0], {"bswapq", "${0:q}"})) { | |||
52983 | // No need to check constraints, nothing other than the equivalent of | |||
52984 | // "=r,0" would be valid here. | |||
52985 | return IntrinsicLowering::LowerToByteSwap(CI); | |||
52986 | } | |||
52987 | ||||
52988 | // rorw $$8, ${0:w} --> llvm.bswap.i16 | |||
52989 | if (CI->getType()->isIntegerTy(16) && | |||
52990 | IA->getConstraintString().compare(0, 5, "=r,0,") == 0 && | |||
52991 | (matchAsm(AsmPieces[0], {"rorw", "$$8,", "${0:w}"}) || | |||
52992 | matchAsm(AsmPieces[0], {"rolw", "$$8,", "${0:w}"}))) { | |||
52993 | AsmPieces.clear(); | |||
52994 | StringRef ConstraintsStr = IA->getConstraintString(); | |||
52995 | SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ","); | |||
52996 | array_pod_sort(AsmPieces.begin(), AsmPieces.end()); | |||
52997 | if (clobbersFlagRegisters(AsmPieces)) | |||
52998 | return IntrinsicLowering::LowerToByteSwap(CI); | |||
52999 | } | |||
53000 | break; | |||
53001 | case 3: | |||
53002 | if (CI->getType()->isIntegerTy(32) && | |||
53003 | IA->getConstraintString().compare(0, 5, "=r,0,") == 0 && | |||
53004 | matchAsm(AsmPieces[0], {"rorw", "$$8,", "${0:w}"}) && | |||
53005 | matchAsm(AsmPieces[1], {"rorl", "$$16,", "$0"}) && | |||
53006 | matchAsm(AsmPieces[2], {"rorw", "$$8,", "${0:w}"})) { | |||
53007 | AsmPieces.clear(); | |||
53008 | StringRef ConstraintsStr = IA->getConstraintString(); | |||
53009 | SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ","); | |||
53010 | array_pod_sort(AsmPieces.begin(), AsmPieces.end()); | |||
53011 | if (clobbersFlagRegisters(AsmPieces)) | |||
53012 | return IntrinsicLowering::LowerToByteSwap(CI); | |||
53013 | } | |||
53014 | ||||
53015 | if (CI->getType()->isIntegerTy(64)) { | |||
53016 | InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints(); | |||
53017 | if (Constraints.size() >= 2 && | |||
53018 | Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" && | |||
53019 | Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") { | |||
53020 | // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64 | |||
53021 | if (matchAsm(AsmPieces[0], {"bswap", "%eax"}) && | |||
53022 | matchAsm(AsmPieces[1], {"bswap", "%edx"}) && | |||
53023 | matchAsm(AsmPieces[2], {"xchgl", "%eax,", "%edx"})) | |||
53024 | return IntrinsicLowering::LowerToByteSwap(CI); | |||
53025 | } | |||
53026 | } | |||
53027 | break; | |||
53028 | } | |||
53029 | return false; | |||
53030 | } | |||
53031 | ||||
53032 | static X86::CondCode parseConstraintCode(llvm::StringRef Constraint) { | |||
53033 | X86::CondCode Cond = StringSwitch<X86::CondCode>(Constraint) | |||
53034 | .Case("{@cca}", X86::COND_A) | |||
53035 | .Case("{@ccae}", X86::COND_AE) | |||
53036 | .Case("{@ccb}", X86::COND_B) | |||
53037 | .Case("{@ccbe}", X86::COND_BE) | |||
53038 | .Case("{@ccc}", X86::COND_B) | |||
53039 | .Case("{@cce}", X86::COND_E) | |||
53040 | .Case("{@ccz}", X86::COND_E) | |||
53041 | .Case("{@ccg}", X86::COND_G) | |||
53042 | .Case("{@ccge}", X86::COND_GE) | |||
53043 | .Case("{@ccl}", X86::COND_L) | |||
53044 | .Case("{@ccle}", X86::COND_LE) | |||
53045 | .Case("{@ccna}", X86::COND_BE) | |||
53046 | .Case("{@ccnae}", X86::COND_B) | |||
53047 | .Case("{@ccnb}", X86::COND_AE) | |||
53048 | .Case("{@ccnbe}", X86::COND_A) | |||
53049 | .Case("{@ccnc}", X86::COND_AE) | |||
53050 | .Case("{@ccne}", X86::COND_NE) | |||
53051 | .Case("{@ccnz}", X86::COND_NE) | |||
53052 | .Case("{@ccng}", X86::COND_LE) | |||
53053 | .Case("{@ccnge}", X86::COND_L) | |||
53054 | .Case("{@ccnl}", X86::COND_GE) | |||
53055 | .Case("{@ccnle}", X86::COND_G) | |||
53056 | .Case("{@ccno}", X86::COND_NO) | |||
53057 | .Case("{@ccnp}", X86::COND_NP) | |||
53058 | .Case("{@ccns}", X86::COND_NS) | |||
53059 | .Case("{@cco}", X86::COND_O) | |||
53060 | .Case("{@ccp}", X86::COND_P) | |||
53061 | .Case("{@ccs}", X86::COND_S) | |||
53062 | .Default(X86::COND_INVALID); | |||
53063 | return Cond; | |||
53064 | } | |||
53065 | ||||
53066 | /// Given a constraint letter, return the type of constraint for this target. | |||
53067 | X86TargetLowering::ConstraintType | |||
53068 | X86TargetLowering::getConstraintType(StringRef Constraint) const { | |||
53069 | if (Constraint.size() == 1) { | |||
53070 | switch (Constraint[0]) { | |||
53071 | case 'R': | |||
53072 | case 'q': | |||
53073 | case 'Q': | |||
53074 | case 'f': | |||
53075 | case 't': | |||
53076 | case 'u': | |||
53077 | case 'y': | |||
53078 | case 'x': | |||
53079 | case 'v': | |||
53080 | case 'l': | |||
53081 | case 'k': // AVX512 masking registers. | |||
53082 | return C_RegisterClass; | |||
53083 | case 'a': | |||
53084 | case 'b': | |||
53085 | case 'c': | |||
53086 | case 'd': | |||
53087 | case 'S': | |||
53088 | case 'D': | |||
53089 | case 'A': | |||
53090 | return C_Register; | |||
53091 | case 'I': | |||
53092 | case 'J': | |||
53093 | case 'K': | |||
53094 | case 'N': | |||
53095 | case 'G': | |||
53096 | case 'L': | |||
53097 | case 'M': | |||
53098 | return C_Immediate; | |||
53099 | case 'C': | |||
53100 | case 'e': | |||
53101 | case 'Z': | |||
53102 | return C_Other; | |||
53103 | default: | |||
53104 | break; | |||
53105 | } | |||
53106 | } | |||
53107 | else if (Constraint.size() == 2) { | |||
53108 | switch (Constraint[0]) { | |||
53109 | default: | |||
53110 | break; | |||
53111 | case 'Y': | |||
53112 | switch (Constraint[1]) { | |||
53113 | default: | |||
53114 | break; | |||
53115 | case 'z': | |||
53116 | return C_Register; | |||
53117 | case 'i': | |||
53118 | case 'm': | |||
53119 | case 'k': | |||
53120 | case 't': | |||
53121 | case '2': | |||
53122 | return C_RegisterClass; | |||
53123 | } | |||
53124 | } | |||
53125 | } else if (parseConstraintCode(Constraint) != X86::COND_INVALID) | |||
53126 | return C_Other; | |||
53127 | return TargetLowering::getConstraintType(Constraint); | |||
53128 | } | |||
53129 | ||||
53130 | /// Examine constraint type and operand type and determine a weight value. | |||
53131 | /// This object must already have been set up with the operand type | |||
53132 | /// and the current alternative constraint selected. | |||
53133 | TargetLowering::ConstraintWeight | |||
53134 | X86TargetLowering::getSingleConstraintMatchWeight( | |||
53135 | AsmOperandInfo &info, const char *constraint) const { | |||
53136 | ConstraintWeight weight = CW_Invalid; | |||
53137 | Value *CallOperandVal = info.CallOperandVal; | |||
53138 | // If we don't have a value, we can't do a match, | |||
53139 | // but allow it at the lowest weight. | |||
53140 | if (!CallOperandVal) | |||
53141 | return CW_Default; | |||
53142 | Type *type = CallOperandVal->getType(); | |||
53143 | // Look at the constraint type. | |||
53144 | switch (*constraint) { | |||
53145 | default: | |||
53146 | weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint); | |||
53147 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
53148 | case 'R': | |||
53149 | case 'q': | |||
53150 | case 'Q': | |||
53151 | case 'a': | |||
53152 | case 'b': | |||
53153 | case 'c': | |||
53154 | case 'd': | |||
53155 | case 'S': | |||
53156 | case 'D': | |||
53157 | case 'A': | |||
53158 | if (CallOperandVal->getType()->isIntegerTy()) | |||
53159 | weight = CW_SpecificReg; | |||
53160 | break; | |||
53161 | case 'f': | |||
53162 | case 't': | |||
53163 | case 'u': | |||
53164 | if (type->isFloatingPointTy()) | |||
53165 | weight = CW_SpecificReg; | |||
53166 | break; | |||
53167 | case 'y': | |||
53168 | if (type->isX86_MMXTy() && Subtarget.hasMMX()) | |||
53169 | weight = CW_SpecificReg; | |||
53170 | break; | |||
53171 | case 'Y': | |||
53172 | if (StringRef(constraint).size() != 2) | |||
53173 | break; | |||
53174 | switch (constraint[1]) { | |||
53175 | default: | |||
53176 | return CW_Invalid; | |||
53177 | // XMM0 | |||
53178 | case 'z': | |||
53179 | if (((type->getPrimitiveSizeInBits() == 128) && Subtarget.hasSSE1()) || | |||
53180 | ((type->getPrimitiveSizeInBits() == 256) && Subtarget.hasAVX()) || | |||
53181 | ((type->getPrimitiveSizeInBits() == 512) && Subtarget.hasAVX512())) | |||
53182 | return CW_SpecificReg; | |||
53183 | return CW_Invalid; | |||
53184 | // Conditional OpMask regs (AVX512) | |||
53185 | case 'k': | |||
53186 | if ((type->getPrimitiveSizeInBits() == 64) && Subtarget.hasAVX512()) | |||
53187 | return CW_Register; | |||
53188 | return CW_Invalid; | |||
53189 | // Any MMX reg | |||
53190 | case 'm': | |||
53191 | if (type->isX86_MMXTy() && Subtarget.hasMMX()) | |||
53192 | return weight; | |||
53193 | return CW_Invalid; | |||
53194 | // Any SSE reg when ISA >= SSE2, same as 'x' | |||
53195 | case 'i': | |||
53196 | case 't': | |||
53197 | case '2': | |||
53198 | if (!Subtarget.hasSSE2()) | |||
53199 | return CW_Invalid; | |||
53200 | break; | |||
53201 | } | |||
53202 | break; | |||
53203 | case 'v': | |||
53204 | if ((type->getPrimitiveSizeInBits() == 512) && Subtarget.hasAVX512()) | |||
53205 | weight = CW_Register; | |||
53206 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
53207 | case 'x': | |||
53208 | if (((type->getPrimitiveSizeInBits() == 128) && Subtarget.hasSSE1()) || | |||
53209 | ((type->getPrimitiveSizeInBits() == 256) && Subtarget.hasAVX())) | |||
53210 | weight = CW_Register; | |||
53211 | break; | |||
53212 | case 'k': | |||
53213 | // Enable conditional vector operations using %k<#> registers. | |||
53214 | if ((type->getPrimitiveSizeInBits() == 64) && Subtarget.hasAVX512()) | |||
53215 | weight = CW_Register; | |||
53216 | break; | |||
53217 | case 'I': | |||
53218 | if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) { | |||
53219 | if (C->getZExtValue() <= 31) | |||
53220 | weight = CW_Constant; | |||
53221 | } | |||
53222 | break; | |||
53223 | case 'J': | |||
53224 | if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) { | |||
53225 | if (C->getZExtValue() <= 63) | |||
53226 | weight = CW_Constant; | |||
53227 | } | |||
53228 | break; | |||
53229 | case 'K': | |||
53230 | if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) { | |||
53231 | if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f)) | |||
53232 | weight = CW_Constant; | |||
53233 | } | |||
53234 | break; | |||
53235 | case 'L': | |||
53236 | if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) { | |||
53237 | if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff)) | |||
53238 | weight = CW_Constant; | |||
53239 | } | |||
53240 | break; | |||
53241 | case 'M': | |||
53242 | if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) { | |||
53243 | if (C->getZExtValue() <= 3) | |||
53244 | weight = CW_Constant; | |||
53245 | } | |||
53246 | break; | |||
53247 | case 'N': | |||
53248 | if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) { | |||
53249 | if (C->getZExtValue() <= 0xff) | |||
53250 | weight = CW_Constant; | |||
53251 | } | |||
53252 | break; | |||
53253 | case 'G': | |||
53254 | case 'C': | |||
53255 | if (isa<ConstantFP>(CallOperandVal)) { | |||
53256 | weight = CW_Constant; | |||
53257 | } | |||
53258 | break; | |||
53259 | case 'e': | |||
53260 | if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) { | |||
53261 | if ((C->getSExtValue() >= -0x80000000LL) && | |||
53262 | (C->getSExtValue() <= 0x7fffffffLL)) | |||
53263 | weight = CW_Constant; | |||
53264 | } | |||
53265 | break; | |||
53266 | case 'Z': | |||
53267 | if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) { | |||
53268 | if (C->getZExtValue() <= 0xffffffff) | |||
53269 | weight = CW_Constant; | |||
53270 | } | |||
53271 | break; | |||
53272 | } | |||
53273 | return weight; | |||
53274 | } | |||
53275 | ||||
53276 | /// Try to replace an X constraint, which matches anything, with another that | |||
53277 | /// has more specific requirements based on the type of the corresponding | |||
53278 | /// operand. | |||
53279 | const char *X86TargetLowering:: | |||
53280 | LowerXConstraint(EVT ConstraintVT) const { | |||
53281 | // FP X constraints get lowered to SSE1/2 registers if available, otherwise | |||
53282 | // 'f' like normal targets. | |||
53283 | if (ConstraintVT.isFloatingPoint()) { | |||
53284 | if (Subtarget.hasSSE1()) | |||
53285 | return "x"; | |||
53286 | } | |||
53287 | ||||
53288 | return TargetLowering::LowerXConstraint(ConstraintVT); | |||
53289 | } | |||
53290 | ||||
53291 | // Lower @cc targets via setcc. | |||
53292 | SDValue X86TargetLowering::LowerAsmOutputForConstraint( | |||
53293 | SDValue &Chain, SDValue &Flag, const SDLoc &DL, | |||
53294 | const AsmOperandInfo &OpInfo, SelectionDAG &DAG) const { | |||
53295 | X86::CondCode Cond = parseConstraintCode(OpInfo.ConstraintCode); | |||
53296 | if (Cond == X86::COND_INVALID) | |||
53297 | return SDValue(); | |||
53298 | // Check that return type is valid. | |||
53299 | if (OpInfo.ConstraintVT.isVector() || !OpInfo.ConstraintVT.isInteger() || | |||
53300 | OpInfo.ConstraintVT.getSizeInBits() < 8) | |||
53301 | report_fatal_error("Flag output operand is of invalid type"); | |||
53302 | ||||
53303 | // Get EFLAGS register. Only update chain when copyfrom is glued. | |||
53304 | if (Flag.getNode()) { | |||
53305 | Flag = DAG.getCopyFromReg(Chain, DL, X86::EFLAGS, MVT::i32, Flag); | |||
53306 | Chain = Flag.getValue(1); | |||
53307 | } else | |||
53308 | Flag = DAG.getCopyFromReg(Chain, DL, X86::EFLAGS, MVT::i32); | |||
53309 | // Extract CC code. | |||
53310 | SDValue CC = getSETCC(Cond, Flag, DL, DAG); | |||
53311 | // Extend to 32-bits | |||
53312 | SDValue Result = DAG.getNode(ISD::ZERO_EXTEND, DL, OpInfo.ConstraintVT, CC); | |||
53313 | ||||
53314 | return Result; | |||
53315 | } | |||
53316 | ||||
53317 | /// Lower the specified operand into the Ops vector. | |||
53318 | /// If it is invalid, don't add anything to Ops. | |||
53319 | void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op, | |||
53320 | std::string &Constraint, | |||
53321 | std::vector<SDValue>&Ops, | |||
53322 | SelectionDAG &DAG) const { | |||
53323 | SDValue Result; | |||
53324 | ||||
53325 | // Only support length 1 constraints for now. | |||
53326 | if (Constraint.length() > 1) return; | |||
53327 | ||||
53328 | char ConstraintLetter = Constraint[0]; | |||
53329 | switch (ConstraintLetter) { | |||
53330 | default: break; | |||
53331 | case 'I': | |||
53332 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { | |||
53333 | if (C->getZExtValue() <= 31) { | |||
53334 | Result = DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op), | |||
53335 | Op.getValueType()); | |||
53336 | break; | |||
53337 | } | |||
53338 | } | |||
53339 | return; | |||
53340 | case 'J': | |||
53341 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { | |||
53342 | if (C->getZExtValue() <= 63) { | |||
53343 | Result = DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op), | |||
53344 | Op.getValueType()); | |||
53345 | break; | |||
53346 | } | |||
53347 | } | |||
53348 | return; | |||
53349 | case 'K': | |||
53350 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { | |||
53351 | if (isInt<8>(C->getSExtValue())) { | |||
53352 | Result = DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op), | |||
53353 | Op.getValueType()); | |||
53354 | break; | |||
53355 | } | |||
53356 | } | |||
53357 | return; | |||
53358 | case 'L': | |||
53359 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { | |||
53360 | if (C->getZExtValue() == 0xff || C->getZExtValue() == 0xffff || | |||
53361 | (Subtarget.is64Bit() && C->getZExtValue() == 0xffffffff)) { | |||
53362 | Result = DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op), | |||
53363 | Op.getValueType()); | |||
53364 | break; | |||
53365 | } | |||
53366 | } | |||
53367 | return; | |||
53368 | case 'M': | |||
53369 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { | |||
53370 | if (C->getZExtValue() <= 3) { | |||
53371 | Result = DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op), | |||
53372 | Op.getValueType()); | |||
53373 | break; | |||
53374 | } | |||
53375 | } | |||
53376 | return; | |||
53377 | case 'N': | |||
53378 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { | |||
53379 | if (C->getZExtValue() <= 255) { | |||
53380 | Result = DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op), | |||
53381 | Op.getValueType()); | |||
53382 | break; | |||
53383 | } | |||
53384 | } | |||
53385 | return; | |||
53386 | case 'O': | |||
53387 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { | |||
53388 | if (C->getZExtValue() <= 127) { | |||
53389 | Result = DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op), | |||
53390 | Op.getValueType()); | |||
53391 | break; | |||
53392 | } | |||
53393 | } | |||
53394 | return; | |||
53395 | case 'e': { | |||
53396 | // 32-bit signed value | |||
53397 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { | |||
53398 | if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()), | |||
53399 | C->getSExtValue())) { | |||
53400 | // Widen to 64 bits here to get it sign extended. | |||
53401 | Result = DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op), MVT::i64); | |||
53402 | break; | |||
53403 | } | |||
53404 | // FIXME gcc accepts some relocatable values here too, but only in certain | |||
53405 | // memory models; it's complicated. | |||
53406 | } | |||
53407 | return; | |||
53408 | } | |||
53409 | case 'Z': { | |||
53410 | // 32-bit unsigned value | |||
53411 | if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) { | |||
53412 | if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()), | |||
53413 | C->getZExtValue())) { | |||
53414 | Result = DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op), | |||
53415 | Op.getValueType()); | |||
53416 | break; | |||
53417 | } | |||
53418 | } | |||
53419 | // FIXME gcc accepts some relocatable values here too, but only in certain | |||
53420 | // memory models; it's complicated. | |||
53421 | return; | |||
53422 | } | |||
53423 | case 'i': { | |||
53424 | // Literal immediates are always ok. | |||
53425 | if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) { | |||
53426 | bool IsBool = CST->getConstantIntValue()->getBitWidth() == 1; | |||
53427 | BooleanContent BCont = getBooleanContents(MVT::i64); | |||
53428 | ISD::NodeType ExtOpc = IsBool ? getExtendForContent(BCont) | |||
53429 | : ISD::SIGN_EXTEND; | |||
53430 | int64_t ExtVal = ExtOpc == ISD::ZERO_EXTEND ? CST->getZExtValue() | |||
53431 | : CST->getSExtValue(); | |||
53432 | Result = DAG.getTargetConstant(ExtVal, SDLoc(Op), MVT::i64); | |||
53433 | break; | |||
53434 | } | |||
53435 | ||||
53436 | // In any sort of PIC mode addresses need to be computed at runtime by | |||
53437 | // adding in a register or some sort of table lookup. These can't | |||
53438 | // be used as immediates. | |||
53439 | if (Subtarget.isPICStyleGOT() || Subtarget.isPICStyleStubPIC()) | |||
53440 | return; | |||
53441 | ||||
53442 | // If we are in non-pic codegen mode, we allow the address of a global (with | |||
53443 | // an optional displacement) to be used with 'i'. | |||
53444 | if (auto *GA = dyn_cast<GlobalAddressSDNode>(Op)) | |||
53445 | // If we require an extra load to get this address, as in PIC mode, we | |||
53446 | // can't accept it. | |||
53447 | if (isGlobalStubReference( | |||
53448 | Subtarget.classifyGlobalReference(GA->getGlobal()))) | |||
53449 | return; | |||
53450 | break; | |||
53451 | } | |||
53452 | } | |||
53453 | ||||
53454 | if (Result.getNode()) { | |||
53455 | Ops.push_back(Result); | |||
53456 | return; | |||
53457 | } | |||
53458 | return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG); | |||
53459 | } | |||
53460 | ||||
53461 | /// Check if \p RC is a general purpose register class. | |||
53462 | /// I.e., GR* or one of their variant. | |||
53463 | static bool isGRClass(const TargetRegisterClass &RC) { | |||
53464 | return RC.hasSuperClassEq(&X86::GR8RegClass) || | |||
53465 | RC.hasSuperClassEq(&X86::GR16RegClass) || | |||
53466 | RC.hasSuperClassEq(&X86::GR32RegClass) || | |||
53467 | RC.hasSuperClassEq(&X86::GR64RegClass) || | |||
53468 | RC.hasSuperClassEq(&X86::LOW32_ADDR_ACCESS_RBPRegClass); | |||
53469 | } | |||
53470 | ||||
53471 | /// Check if \p RC is a vector register class. | |||
53472 | /// I.e., FR* / VR* or one of their variant. | |||
53473 | static bool isFRClass(const TargetRegisterClass &RC) { | |||
53474 | return RC.hasSuperClassEq(&X86::FR16XRegClass) || | |||
53475 | RC.hasSuperClassEq(&X86::FR32XRegClass) || | |||
53476 | RC.hasSuperClassEq(&X86::FR64XRegClass) || | |||
53477 | RC.hasSuperClassEq(&X86::VR128XRegClass) || | |||
53478 | RC.hasSuperClassEq(&X86::VR256XRegClass) || | |||
53479 | RC.hasSuperClassEq(&X86::VR512RegClass); | |||
53480 | } | |||
53481 | ||||
53482 | /// Check if \p RC is a mask register class. | |||
53483 | /// I.e., VK* or one of their variant. | |||
53484 | static bool isVKClass(const TargetRegisterClass &RC) { | |||
53485 | return RC.hasSuperClassEq(&X86::VK1RegClass) || | |||
53486 | RC.hasSuperClassEq(&X86::VK2RegClass) || | |||
53487 | RC.hasSuperClassEq(&X86::VK4RegClass) || | |||
53488 | RC.hasSuperClassEq(&X86::VK8RegClass) || | |||
53489 | RC.hasSuperClassEq(&X86::VK16RegClass) || | |||
53490 | RC.hasSuperClassEq(&X86::VK32RegClass) || | |||
53491 | RC.hasSuperClassEq(&X86::VK64RegClass); | |||
53492 | } | |||
53493 | ||||
53494 | std::pair<unsigned, const TargetRegisterClass *> | |||
53495 | X86TargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, | |||
53496 | StringRef Constraint, | |||
53497 | MVT VT) const { | |||
53498 | // First, see if this is a constraint that directly corresponds to an LLVM | |||
53499 | // register class. | |||
53500 | if (Constraint.size() == 1) { | |||
53501 | // GCC Constraint Letters | |||
53502 | switch (Constraint[0]) { | |||
53503 | default: break; | |||
53504 | // 'A' means [ER]AX + [ER]DX. | |||
53505 | case 'A': | |||
53506 | if (Subtarget.is64Bit()) | |||
53507 | return std::make_pair(X86::RAX, &X86::GR64_ADRegClass); | |||
53508 | assert((Subtarget.is32Bit() || Subtarget.is16Bit()) &&(static_cast <bool> ((Subtarget.is32Bit() || Subtarget. is16Bit()) && "Expecting 64, 32 or 16 bit subtarget") ? void (0) : __assert_fail ("(Subtarget.is32Bit() || Subtarget.is16Bit()) && \"Expecting 64, 32 or 16 bit subtarget\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 53509, __extension__ __PRETTY_FUNCTION__)) | |||
53509 | "Expecting 64, 32 or 16 bit subtarget")(static_cast <bool> ((Subtarget.is32Bit() || Subtarget. is16Bit()) && "Expecting 64, 32 or 16 bit subtarget") ? void (0) : __assert_fail ("(Subtarget.is32Bit() || Subtarget.is16Bit()) && \"Expecting 64, 32 or 16 bit subtarget\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 53509, __extension__ __PRETTY_FUNCTION__)); | |||
53510 | return std::make_pair(X86::EAX, &X86::GR32_ADRegClass); | |||
53511 | ||||
53512 | // TODO: Slight differences here in allocation order and leaving | |||
53513 | // RIP in the class. Do they matter any more here than they do | |||
53514 | // in the normal allocation? | |||
53515 | case 'k': | |||
53516 | if (Subtarget.hasAVX512()) { | |||
53517 | if (VT == MVT::i1) | |||
53518 | return std::make_pair(0U, &X86::VK1RegClass); | |||
53519 | if (VT == MVT::i8) | |||
53520 | return std::make_pair(0U, &X86::VK8RegClass); | |||
53521 | if (VT == MVT::i16) | |||
53522 | return std::make_pair(0U, &X86::VK16RegClass); | |||
53523 | } | |||
53524 | if (Subtarget.hasBWI()) { | |||
53525 | if (VT == MVT::i32) | |||
53526 | return std::make_pair(0U, &X86::VK32RegClass); | |||
53527 | if (VT == MVT::i64) | |||
53528 | return std::make_pair(0U, &X86::VK64RegClass); | |||
53529 | } | |||
53530 | break; | |||
53531 | case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode. | |||
53532 | if (Subtarget.is64Bit()) { | |||
53533 | if (VT == MVT::i8 || VT == MVT::i1) | |||
53534 | return std::make_pair(0U, &X86::GR8RegClass); | |||
53535 | if (VT == MVT::i16) | |||
53536 | return std::make_pair(0U, &X86::GR16RegClass); | |||
53537 | if (VT == MVT::i32 || VT == MVT::f32) | |||
53538 | return std::make_pair(0U, &X86::GR32RegClass); | |||
53539 | if (VT != MVT::f80 && !VT.isVector()) | |||
53540 | return std::make_pair(0U, &X86::GR64RegClass); | |||
53541 | break; | |||
53542 | } | |||
53543 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
53544 | // 32-bit fallthrough | |||
53545 | case 'Q': // Q_REGS | |||
53546 | if (VT == MVT::i8 || VT == MVT::i1) | |||
53547 | return std::make_pair(0U, &X86::GR8_ABCD_LRegClass); | |||
53548 | if (VT == MVT::i16) | |||
53549 | return std::make_pair(0U, &X86::GR16_ABCDRegClass); | |||
53550 | if (VT == MVT::i32 || VT == MVT::f32 || | |||
53551 | (!VT.isVector() && !Subtarget.is64Bit())) | |||
53552 | return std::make_pair(0U, &X86::GR32_ABCDRegClass); | |||
53553 | if (VT != MVT::f80 && !VT.isVector()) | |||
53554 | return std::make_pair(0U, &X86::GR64_ABCDRegClass); | |||
53555 | break; | |||
53556 | case 'r': // GENERAL_REGS | |||
53557 | case 'l': // INDEX_REGS | |||
53558 | if (VT == MVT::i8 || VT == MVT::i1) | |||
53559 | return std::make_pair(0U, &X86::GR8RegClass); | |||
53560 | if (VT == MVT::i16) | |||
53561 | return std::make_pair(0U, &X86::GR16RegClass); | |||
53562 | if (VT == MVT::i32 || VT == MVT::f32 || | |||
53563 | (!VT.isVector() && !Subtarget.is64Bit())) | |||
53564 | return std::make_pair(0U, &X86::GR32RegClass); | |||
53565 | if (VT != MVT::f80 && !VT.isVector()) | |||
53566 | return std::make_pair(0U, &X86::GR64RegClass); | |||
53567 | break; | |||
53568 | case 'R': // LEGACY_REGS | |||
53569 | if (VT == MVT::i8 || VT == MVT::i1) | |||
53570 | return std::make_pair(0U, &X86::GR8_NOREXRegClass); | |||
53571 | if (VT == MVT::i16) | |||
53572 | return std::make_pair(0U, &X86::GR16_NOREXRegClass); | |||
53573 | if (VT == MVT::i32 || VT == MVT::f32 || | |||
53574 | (!VT.isVector() && !Subtarget.is64Bit())) | |||
53575 | return std::make_pair(0U, &X86::GR32_NOREXRegClass); | |||
53576 | if (VT != MVT::f80 && !VT.isVector()) | |||
53577 | return std::make_pair(0U, &X86::GR64_NOREXRegClass); | |||
53578 | break; | |||
53579 | case 'f': // FP Stack registers. | |||
53580 | // If SSE is enabled for this VT, use f80 to ensure the isel moves the | |||
53581 | // value to the correct fpstack register class. | |||
53582 | if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT)) | |||
53583 | return std::make_pair(0U, &X86::RFP32RegClass); | |||
53584 | if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT)) | |||
53585 | return std::make_pair(0U, &X86::RFP64RegClass); | |||
53586 | if (VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f80) | |||
53587 | return std::make_pair(0U, &X86::RFP80RegClass); | |||
53588 | break; | |||
53589 | case 'y': // MMX_REGS if MMX allowed. | |||
53590 | if (!Subtarget.hasMMX()) break; | |||
53591 | return std::make_pair(0U, &X86::VR64RegClass); | |||
53592 | case 'v': | |||
53593 | case 'x': // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed | |||
53594 | if (!Subtarget.hasSSE1()) break; | |||
53595 | bool VConstraint = (Constraint[0] == 'v'); | |||
53596 | ||||
53597 | switch (VT.SimpleTy) { | |||
53598 | default: break; | |||
53599 | // Scalar SSE types. | |||
53600 | case MVT::f16: | |||
53601 | if (VConstraint && Subtarget.hasFP16()) | |||
53602 | return std::make_pair(0U, &X86::FR16XRegClass); | |||
53603 | break; | |||
53604 | case MVT::f32: | |||
53605 | case MVT::i32: | |||
53606 | if (VConstraint && Subtarget.hasVLX()) | |||
53607 | return std::make_pair(0U, &X86::FR32XRegClass); | |||
53608 | return std::make_pair(0U, &X86::FR32RegClass); | |||
53609 | case MVT::f64: | |||
53610 | case MVT::i64: | |||
53611 | if (VConstraint && Subtarget.hasVLX()) | |||
53612 | return std::make_pair(0U, &X86::FR64XRegClass); | |||
53613 | return std::make_pair(0U, &X86::FR64RegClass); | |||
53614 | case MVT::i128: | |||
53615 | if (Subtarget.is64Bit()) { | |||
53616 | if (VConstraint && Subtarget.hasVLX()) | |||
53617 | return std::make_pair(0U, &X86::VR128XRegClass); | |||
53618 | return std::make_pair(0U, &X86::VR128RegClass); | |||
53619 | } | |||
53620 | break; | |||
53621 | // Vector types and fp128. | |||
53622 | case MVT::v8f16: | |||
53623 | if (!Subtarget.hasFP16()) | |||
53624 | break; | |||
53625 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
53626 | case MVT::f128: | |||
53627 | case MVT::v16i8: | |||
53628 | case MVT::v8i16: | |||
53629 | case MVT::v4i32: | |||
53630 | case MVT::v2i64: | |||
53631 | case MVT::v4f32: | |||
53632 | case MVT::v2f64: | |||
53633 | if (VConstraint && Subtarget.hasVLX()) | |||
53634 | return std::make_pair(0U, &X86::VR128XRegClass); | |||
53635 | return std::make_pair(0U, &X86::VR128RegClass); | |||
53636 | // AVX types. | |||
53637 | case MVT::v16f16: | |||
53638 | if (!Subtarget.hasFP16()) | |||
53639 | break; | |||
53640 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
53641 | case MVT::v32i8: | |||
53642 | case MVT::v16i16: | |||
53643 | case MVT::v8i32: | |||
53644 | case MVT::v4i64: | |||
53645 | case MVT::v8f32: | |||
53646 | case MVT::v4f64: | |||
53647 | if (VConstraint && Subtarget.hasVLX()) | |||
53648 | return std::make_pair(0U, &X86::VR256XRegClass); | |||
53649 | if (Subtarget.hasAVX()) | |||
53650 | return std::make_pair(0U, &X86::VR256RegClass); | |||
53651 | break; | |||
53652 | case MVT::v32f16: | |||
53653 | if (!Subtarget.hasFP16()) | |||
53654 | break; | |||
53655 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
53656 | case MVT::v64i8: | |||
53657 | case MVT::v32i16: | |||
53658 | case MVT::v8f64: | |||
53659 | case MVT::v16f32: | |||
53660 | case MVT::v16i32: | |||
53661 | case MVT::v8i64: | |||
53662 | if (!Subtarget.hasAVX512()) break; | |||
53663 | if (VConstraint) | |||
53664 | return std::make_pair(0U, &X86::VR512RegClass); | |||
53665 | return std::make_pair(0U, &X86::VR512_0_15RegClass); | |||
53666 | } | |||
53667 | break; | |||
53668 | } | |||
53669 | } else if (Constraint.size() == 2 && Constraint[0] == 'Y') { | |||
53670 | switch (Constraint[1]) { | |||
53671 | default: | |||
53672 | break; | |||
53673 | case 'i': | |||
53674 | case 't': | |||
53675 | case '2': | |||
53676 | return getRegForInlineAsmConstraint(TRI, "x", VT); | |||
53677 | case 'm': | |||
53678 | if (!Subtarget.hasMMX()) break; | |||
53679 | return std::make_pair(0U, &X86::VR64RegClass); | |||
53680 | case 'z': | |||
53681 | if (!Subtarget.hasSSE1()) break; | |||
53682 | switch (VT.SimpleTy) { | |||
53683 | default: break; | |||
53684 | // Scalar SSE types. | |||
53685 | case MVT::f16: | |||
53686 | if (!Subtarget.hasFP16()) | |||
53687 | break; | |||
53688 | return std::make_pair(X86::XMM0, &X86::FR16XRegClass); | |||
53689 | case MVT::f32: | |||
53690 | case MVT::i32: | |||
53691 | return std::make_pair(X86::XMM0, &X86::FR32RegClass); | |||
53692 | case MVT::f64: | |||
53693 | case MVT::i64: | |||
53694 | return std::make_pair(X86::XMM0, &X86::FR64RegClass); | |||
53695 | case MVT::v8f16: | |||
53696 | if (!Subtarget.hasFP16()) | |||
53697 | break; | |||
53698 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
53699 | case MVT::f128: | |||
53700 | case MVT::v16i8: | |||
53701 | case MVT::v8i16: | |||
53702 | case MVT::v4i32: | |||
53703 | case MVT::v2i64: | |||
53704 | case MVT::v4f32: | |||
53705 | case MVT::v2f64: | |||
53706 | return std::make_pair(X86::XMM0, &X86::VR128RegClass); | |||
53707 | // AVX types. | |||
53708 | case MVT::v16f16: | |||
53709 | if (!Subtarget.hasFP16()) | |||
53710 | break; | |||
53711 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
53712 | case MVT::v32i8: | |||
53713 | case MVT::v16i16: | |||
53714 | case MVT::v8i32: | |||
53715 | case MVT::v4i64: | |||
53716 | case MVT::v8f32: | |||
53717 | case MVT::v4f64: | |||
53718 | if (Subtarget.hasAVX()) | |||
53719 | return std::make_pair(X86::YMM0, &X86::VR256RegClass); | |||
53720 | break; | |||
53721 | case MVT::v32f16: | |||
53722 | if (!Subtarget.hasFP16()) | |||
53723 | break; | |||
53724 | LLVM_FALLTHROUGH[[gnu::fallthrough]]; | |||
53725 | case MVT::v64i8: | |||
53726 | case MVT::v32i16: | |||
53727 | case MVT::v8f64: | |||
53728 | case MVT::v16f32: | |||
53729 | case MVT::v16i32: | |||
53730 | case MVT::v8i64: | |||
53731 | if (Subtarget.hasAVX512()) | |||
53732 | return std::make_pair(X86::ZMM0, &X86::VR512_0_15RegClass); | |||
53733 | break; | |||
53734 | } | |||
53735 | break; | |||
53736 | case 'k': | |||
53737 | // This register class doesn't allocate k0 for masked vector operation. | |||
53738 | if (Subtarget.hasAVX512()) { | |||
53739 | if (VT == MVT::i1) | |||
53740 | return std::make_pair(0U, &X86::VK1WMRegClass); | |||
53741 | if (VT == MVT::i8) | |||
53742 | return std::make_pair(0U, &X86::VK8WMRegClass); | |||
53743 | if (VT == MVT::i16) | |||
53744 | return std::make_pair(0U, &X86::VK16WMRegClass); | |||
53745 | } | |||
53746 | if (Subtarget.hasBWI()) { | |||
53747 | if (VT == MVT::i32) | |||
53748 | return std::make_pair(0U, &X86::VK32WMRegClass); | |||
53749 | if (VT == MVT::i64) | |||
53750 | return std::make_pair(0U, &X86::VK64WMRegClass); | |||
53751 | } | |||
53752 | break; | |||
53753 | } | |||
53754 | } | |||
53755 | ||||
53756 | if (parseConstraintCode(Constraint) != X86::COND_INVALID) | |||
53757 | return std::make_pair(0U, &X86::GR32RegClass); | |||
53758 | ||||
53759 | // Use the default implementation in TargetLowering to convert the register | |||
53760 | // constraint into a member of a register class. | |||
53761 | std::pair<Register, const TargetRegisterClass*> Res; | |||
53762 | Res = TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT); | |||
53763 | ||||
53764 | // Not found as a standard register? | |||
53765 | if (!Res.second) { | |||
53766 | // Only match x87 registers if the VT is one SelectionDAGBuilder can convert | |||
53767 | // to/from f80. | |||
53768 | if (VT == MVT::Other || VT == MVT::f32 || VT == MVT::f64 || VT == MVT::f80) { | |||
53769 | // Map st(0) -> st(7) -> ST0 | |||
53770 | if (Constraint.size() == 7 && Constraint[0] == '{' && | |||
53771 | tolower(Constraint[1]) == 's' && tolower(Constraint[2]) == 't' && | |||
53772 | Constraint[3] == '(' && | |||
53773 | (Constraint[4] >= '0' && Constraint[4] <= '7') && | |||
53774 | Constraint[5] == ')' && Constraint[6] == '}') { | |||
53775 | // st(7) is not allocatable and thus not a member of RFP80. Return | |||
53776 | // singleton class in cases where we have a reference to it. | |||
53777 | if (Constraint[4] == '7') | |||
53778 | return std::make_pair(X86::FP7, &X86::RFP80_7RegClass); | |||
53779 | return std::make_pair(X86::FP0 + Constraint[4] - '0', | |||
53780 | &X86::RFP80RegClass); | |||
53781 | } | |||
53782 | ||||
53783 | // GCC allows "st(0)" to be called just plain "st". | |||
53784 | if (StringRef("{st}").equals_insensitive(Constraint)) | |||
53785 | return std::make_pair(X86::FP0, &X86::RFP80RegClass); | |||
53786 | } | |||
53787 | ||||
53788 | // flags -> EFLAGS | |||
53789 | if (StringRef("{flags}").equals_insensitive(Constraint)) | |||
53790 | return std::make_pair(X86::EFLAGS, &X86::CCRRegClass); | |||
53791 | ||||
53792 | // dirflag -> DF | |||
53793 | // Only allow for clobber. | |||
53794 | if (StringRef("{dirflag}").equals_insensitive(Constraint) && | |||
53795 | VT == MVT::Other) | |||
53796 | return std::make_pair(X86::DF, &X86::DFCCRRegClass); | |||
53797 | ||||
53798 | // fpsr -> FPSW | |||
53799 | if (StringRef("{fpsr}").equals_insensitive(Constraint)) | |||
53800 | return std::make_pair(X86::FPSW, &X86::FPCCRRegClass); | |||
53801 | ||||
53802 | return Res; | |||
53803 | } | |||
53804 | ||||
53805 | // Make sure it isn't a register that requires 64-bit mode. | |||
53806 | if (!Subtarget.is64Bit() && | |||
53807 | (isFRClass(*Res.second) || isGRClass(*Res.second)) && | |||
53808 | TRI->getEncodingValue(Res.first) >= 8) { | |||
53809 | // Register requires REX prefix, but we're in 32-bit mode. | |||
53810 | return std::make_pair(0, nullptr); | |||
53811 | } | |||
53812 | ||||
53813 | // Make sure it isn't a register that requires AVX512. | |||
53814 | if (!Subtarget.hasAVX512() && isFRClass(*Res.second) && | |||
53815 | TRI->getEncodingValue(Res.first) & 0x10) { | |||
53816 | // Register requires EVEX prefix. | |||
53817 | return std::make_pair(0, nullptr); | |||
53818 | } | |||
53819 | ||||
53820 | // Otherwise, check to see if this is a register class of the wrong value | |||
53821 | // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to | |||
53822 | // turn into {ax},{dx}. | |||
53823 | // MVT::Other is used to specify clobber names. | |||
53824 | if (TRI->isTypeLegalForClass(*Res.second, VT) || VT == MVT::Other) | |||
53825 | return Res; // Correct type already, nothing to do. | |||
53826 | ||||
53827 | // Get a matching integer of the correct size. i.e. "ax" with MVT::32 should | |||
53828 | // return "eax". This should even work for things like getting 64bit integer | |||
53829 | // registers when given an f64 type. | |||
53830 | const TargetRegisterClass *Class = Res.second; | |||
53831 | // The generic code will match the first register class that contains the | |||
53832 | // given register. Thus, based on the ordering of the tablegened file, | |||
53833 | // the "plain" GR classes might not come first. | |||
53834 | // Therefore, use a helper method. | |||
53835 | if (isGRClass(*Class)) { | |||
53836 | unsigned Size = VT.getSizeInBits(); | |||
53837 | if (Size == 1) Size = 8; | |||
53838 | Register DestReg = getX86SubSuperRegisterOrZero(Res.first, Size); | |||
53839 | if (DestReg > 0) { | |||
53840 | bool is64Bit = Subtarget.is64Bit(); | |||
53841 | const TargetRegisterClass *RC = | |||
53842 | Size == 8 ? (is64Bit ? &X86::GR8RegClass : &X86::GR8_NOREXRegClass) | |||
53843 | : Size == 16 ? (is64Bit ? &X86::GR16RegClass : &X86::GR16_NOREXRegClass) | |||
53844 | : Size == 32 ? (is64Bit ? &X86::GR32RegClass : &X86::GR32_NOREXRegClass) | |||
53845 | : Size == 64 ? (is64Bit ? &X86::GR64RegClass : nullptr) | |||
53846 | : nullptr; | |||
53847 | if (Size == 64 && !is64Bit) { | |||
53848 | // Model GCC's behavior here and select a fixed pair of 32-bit | |||
53849 | // registers. | |||
53850 | switch (DestReg) { | |||
53851 | case X86::RAX: | |||
53852 | return std::make_pair(X86::EAX, &X86::GR32_ADRegClass); | |||
53853 | case X86::RDX: | |||
53854 | return std::make_pair(X86::EDX, &X86::GR32_DCRegClass); | |||
53855 | case X86::RCX: | |||
53856 | return std::make_pair(X86::ECX, &X86::GR32_CBRegClass); | |||
53857 | case X86::RBX: | |||
53858 | return std::make_pair(X86::EBX, &X86::GR32_BSIRegClass); | |||
53859 | case X86::RSI: | |||
53860 | return std::make_pair(X86::ESI, &X86::GR32_SIDIRegClass); | |||
53861 | case X86::RDI: | |||
53862 | return std::make_pair(X86::EDI, &X86::GR32_DIBPRegClass); | |||
53863 | case X86::RBP: | |||
53864 | return std::make_pair(X86::EBP, &X86::GR32_BPSPRegClass); | |||
53865 | default: | |||
53866 | return std::make_pair(0, nullptr); | |||
53867 | } | |||
53868 | } | |||
53869 | if (RC && RC->contains(DestReg)) | |||
53870 | return std::make_pair(DestReg, RC); | |||
53871 | return Res; | |||
53872 | } | |||
53873 | // No register found/type mismatch. | |||
53874 | return std::make_pair(0, nullptr); | |||
53875 | } else if (isFRClass(*Class)) { | |||
53876 | // Handle references to XMM physical registers that got mapped into the | |||
53877 | // wrong class. This can happen with constraints like {xmm0} where the | |||
53878 | // target independent register mapper will just pick the first match it can | |||
53879 | // find, ignoring the required type. | |||
53880 | ||||
53881 | // TODO: Handle f128 and i128 in FR128RegClass after it is tested well. | |||
53882 | if (VT == MVT::f16) | |||
53883 | Res.second = &X86::FR16XRegClass; | |||
53884 | else if (VT == MVT::f32 || VT == MVT::i32) | |||
53885 | Res.second = &X86::FR32XRegClass; | |||
53886 | else if (VT == MVT::f64 || VT == MVT::i64) | |||
53887 | Res.second = &X86::FR64XRegClass; | |||
53888 | else if (TRI->isTypeLegalForClass(X86::VR128XRegClass, VT)) | |||
53889 | Res.second = &X86::VR128XRegClass; | |||
53890 | else if (TRI->isTypeLegalForClass(X86::VR256XRegClass, VT)) | |||
53891 | Res.second = &X86::VR256XRegClass; | |||
53892 | else if (TRI->isTypeLegalForClass(X86::VR512RegClass, VT)) | |||
53893 | Res.second = &X86::VR512RegClass; | |||
53894 | else { | |||
53895 | // Type mismatch and not a clobber: Return an error; | |||
53896 | Res.first = 0; | |||
53897 | Res.second = nullptr; | |||
53898 | } | |||
53899 | } else if (isVKClass(*Class)) { | |||
53900 | if (VT == MVT::i1) | |||
53901 | Res.second = &X86::VK1RegClass; | |||
53902 | else if (VT == MVT::i8) | |||
53903 | Res.second = &X86::VK8RegClass; | |||
53904 | else if (VT == MVT::i16) | |||
53905 | Res.second = &X86::VK16RegClass; | |||
53906 | else if (VT == MVT::i32) | |||
53907 | Res.second = &X86::VK32RegClass; | |||
53908 | else if (VT == MVT::i64) | |||
53909 | Res.second = &X86::VK64RegClass; | |||
53910 | else { | |||
53911 | // Type mismatch and not a clobber: Return an error; | |||
53912 | Res.first = 0; | |||
53913 | Res.second = nullptr; | |||
53914 | } | |||
53915 | } | |||
53916 | ||||
53917 | return Res; | |||
53918 | } | |||
53919 | ||||
53920 | InstructionCost X86TargetLowering::getScalingFactorCost(const DataLayout &DL, | |||
53921 | const AddrMode &AM, | |||
53922 | Type *Ty, | |||
53923 | unsigned AS) const { | |||
53924 | // Scaling factors are not free at all. | |||
53925 | // An indexed folded instruction, i.e., inst (reg1, reg2, scale), | |||
53926 | // will take 2 allocations in the out of order engine instead of 1 | |||
53927 | // for plain addressing mode, i.e. inst (reg1). | |||
53928 | // E.g., | |||
53929 | // vaddps (%rsi,%rdx), %ymm0, %ymm1 | |||
53930 | // Requires two allocations (one for the load, one for the computation) | |||
53931 | // whereas: | |||
53932 | // vaddps (%rsi), %ymm0, %ymm1 | |||
53933 | // Requires just 1 allocation, i.e., freeing allocations for other operations | |||
53934 | // and having less micro operations to execute. | |||
53935 | // | |||
53936 | // For some X86 architectures, this is even worse because for instance for | |||
53937 | // stores, the complex addressing mode forces the instruction to use the | |||
53938 | // "load" ports instead of the dedicated "store" port. | |||
53939 | // E.g., on Haswell: | |||
53940 | // vmovaps %ymm1, (%r8, %rdi) can use port 2 or 3. | |||
53941 | // vmovaps %ymm1, (%r8) can use port 2, 3, or 7. | |||
53942 | if (isLegalAddressingMode(DL, AM, Ty, AS)) | |||
53943 | // Scale represents reg2 * scale, thus account for 1 | |||
53944 | // as soon as we use a second register. | |||
53945 | return AM.Scale != 0; | |||
53946 | return -1; | |||
53947 | } | |||
53948 | ||||
53949 | bool X86TargetLowering::isIntDivCheap(EVT VT, AttributeList Attr) const { | |||
53950 | // Integer division on x86 is expensive. However, when aggressively optimizing | |||
53951 | // for code size, we prefer to use a div instruction, as it is usually smaller | |||
53952 | // than the alternative sequence. | |||
53953 | // The exception to this is vector division. Since x86 doesn't have vector | |||
53954 | // integer division, leaving the division as-is is a loss even in terms of | |||
53955 | // size, because it will have to be scalarized, while the alternative code | |||
53956 | // sequence can be performed in vector form. | |||
53957 | bool OptSize = Attr.hasFnAttr(Attribute::MinSize); | |||
53958 | return OptSize && !VT.isVector(); | |||
53959 | } | |||
53960 | ||||
53961 | void X86TargetLowering::initializeSplitCSR(MachineBasicBlock *Entry) const { | |||
53962 | if (!Subtarget.is64Bit()) | |||
53963 | return; | |||
53964 | ||||
53965 | // Update IsSplitCSR in X86MachineFunctionInfo. | |||
53966 | X86MachineFunctionInfo *AFI = | |||
53967 | Entry->getParent()->getInfo<X86MachineFunctionInfo>(); | |||
53968 | AFI->setIsSplitCSR(true); | |||
53969 | } | |||
53970 | ||||
53971 | void X86TargetLowering::insertCopiesSplitCSR( | |||
53972 | MachineBasicBlock *Entry, | |||
53973 | const SmallVectorImpl<MachineBasicBlock *> &Exits) const { | |||
53974 | const X86RegisterInfo *TRI = Subtarget.getRegisterInfo(); | |||
53975 | const MCPhysReg *IStart = TRI->getCalleeSavedRegsViaCopy(Entry->getParent()); | |||
53976 | if (!IStart) | |||
53977 | return; | |||
53978 | ||||
53979 | const TargetInstrInfo *TII = Subtarget.getInstrInfo(); | |||
53980 | MachineRegisterInfo *MRI = &Entry->getParent()->getRegInfo(); | |||
53981 | MachineBasicBlock::iterator MBBI = Entry->begin(); | |||
53982 | for (const MCPhysReg *I = IStart; *I; ++I) { | |||
53983 | const TargetRegisterClass *RC = nullptr; | |||
53984 | if (X86::GR64RegClass.contains(*I)) | |||
53985 | RC = &X86::GR64RegClass; | |||
53986 | else | |||
53987 | llvm_unreachable("Unexpected register class in CSRsViaCopy!")::llvm::llvm_unreachable_internal("Unexpected register class in CSRsViaCopy!" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 53987); | |||
53988 | ||||
53989 | Register NewVR = MRI->createVirtualRegister(RC); | |||
53990 | // Create copy from CSR to a virtual register. | |||
53991 | // FIXME: this currently does not emit CFI pseudo-instructions, it works | |||
53992 | // fine for CXX_FAST_TLS since the C++-style TLS access functions should be | |||
53993 | // nounwind. If we want to generalize this later, we may need to emit | |||
53994 | // CFI pseudo-instructions. | |||
53995 | assert((static_cast <bool> (Entry->getParent()->getFunction ().hasFnAttribute(Attribute::NoUnwind) && "Function should be nounwind in insertCopiesSplitCSR!" ) ? void (0) : __assert_fail ("Entry->getParent()->getFunction().hasFnAttribute(Attribute::NoUnwind) && \"Function should be nounwind in insertCopiesSplitCSR!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 53997, __extension__ __PRETTY_FUNCTION__)) | |||
53996 | Entry->getParent()->getFunction().hasFnAttribute(Attribute::NoUnwind) &&(static_cast <bool> (Entry->getParent()->getFunction ().hasFnAttribute(Attribute::NoUnwind) && "Function should be nounwind in insertCopiesSplitCSR!" ) ? void (0) : __assert_fail ("Entry->getParent()->getFunction().hasFnAttribute(Attribute::NoUnwind) && \"Function should be nounwind in insertCopiesSplitCSR!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 53997, __extension__ __PRETTY_FUNCTION__)) | |||
53997 | "Function should be nounwind in insertCopiesSplitCSR!")(static_cast <bool> (Entry->getParent()->getFunction ().hasFnAttribute(Attribute::NoUnwind) && "Function should be nounwind in insertCopiesSplitCSR!" ) ? void (0) : __assert_fail ("Entry->getParent()->getFunction().hasFnAttribute(Attribute::NoUnwind) && \"Function should be nounwind in insertCopiesSplitCSR!\"" , "/build/llvm-toolchain-snapshot-14~++20211015062411+81e9c90686f7/llvm/lib/Target/X86/X86ISelLowering.cpp" , 53997, __extension__ __PRETTY_FUNCTION__)); | |||
53998 | Entry->addLiveIn(*I); | |||
53999 | BuildMI(*Entry, MBBI, DebugLoc(), TII->get(TargetOpcode::COPY), NewVR) | |||
54000 | .addReg(*I); | |||
54001 | ||||
54002 | // Insert the copy-back instructions right before the terminator. | |||
54003 | for (auto *Exit : Exits) | |||
54004 | BuildMI(*Exit, Exit->getFirstTerminator(), DebugLoc(), | |||
54005 | TII->get(TargetOpcode::COPY), *I) | |||
54006 | .addReg(NewVR); | |||
54007 | } | |||
54008 | } | |||
54009 | ||||
54010 | bool X86TargetLowering::supportSwiftError() const { | |||
54011 | return Subtarget.is64Bit(); | |||
54012 | } | |||
54013 | ||||
54014 | /// Returns true if stack probing through a function call is requested. | |||
54015 | bool X86TargetLowering::hasStackProbeSymbol(MachineFunction &MF) const { | |||
54016 | return !getStackProbeSymbolName(MF).empty(); | |||
54017 | } | |||
54018 | ||||
54019 | /// Returns true if stack probing through inline assembly is requested. | |||
54020 | bool X86TargetLowering::hasInlineStackProbe(MachineFunction &MF) const { | |||
54021 | ||||
54022 | // No inline stack probe for Windows, they have their own mechanism. | |||
54023 | if (Subtarget.isOSWindows() || | |||
54024 | MF.getFunction().hasFnAttribute("no-stack-arg-probe")) | |||
54025 | return false; | |||
54026 | ||||
54027 | // If the function specifically requests inline stack probes, emit them. | |||
54028 | if (MF.getFunction().hasFnAttribute("probe-stack")) | |||
54029 | return MF.getFunction().getFnAttribute("probe-stack").getValueAsString() == | |||
54030 | "inline-asm"; | |||
54031 | ||||
54032 | return false; | |||
54033 | } | |||
54034 | ||||
54035 | /// Returns the name of the symbol used to emit stack probes or the empty | |||
54036 | /// string if not applicable. | |||
54037 | StringRef | |||
54038 | X86TargetLowering::getStackProbeSymbolName(MachineFunction &MF) const { | |||
54039 | // Inline Stack probes disable stack probe call | |||
54040 | if (hasInlineStackProbe(MF)) | |||
54041 | return ""; | |||
54042 | ||||
54043 | // If the function specifically requests stack probes, emit them. | |||
54044 | if (MF.getFunction().hasFnAttribute("probe-stack")) | |||
54045 | return MF.getFunction().getFnAttribute("probe-stack").getValueAsString(); | |||
54046 | ||||
54047 | // Generally, if we aren't on Windows, the platform ABI does not include | |||
54048 | // support for stack probes, so don't emit them. | |||
54049 | if (!Subtarget.isOSWindows() || Subtarget.isTargetMachO() || | |||
54050 | MF.getFunction().hasFnAttribute("no-stack-arg-probe")) | |||
54051 | return ""; | |||
54052 | ||||
54053 | // We need a stack probe to conform to the Windows ABI. Choose the right | |||
54054 | // symbol. | |||
54055 | if (Subtarget.is64Bit()) | |||
54056 | return Subtarget.isTargetCygMing() ? "___chkstk_ms" : "__chkstk"; | |||
54057 | return Subtarget.isTargetCygMing() ? "_alloca" : "_chkstk"; | |||
54058 | } | |||
54059 | ||||
54060 | unsigned | |||
54061 | X86TargetLowering::getStackProbeSize(MachineFunction &MF) const { | |||
54062 | // The default stack probe size is 4096 if the function has no stackprobesize | |||
54063 | // attribute. | |||
54064 | unsigned StackProbeSize = 4096; | |||
54065 | const Function &Fn = MF.getFunction(); | |||
54066 | if (Fn.hasFnAttribute("stack-probe-size")) | |||
54067 | Fn.getFnAttribute("stack-probe-size") | |||
54068 | .getValueAsString() | |||
54069 | .getAsInteger(0, StackProbeSize); | |||
54070 | return StackProbeSize; | |||
54071 | } | |||
54072 | ||||
54073 | Align X86TargetLowering::getPrefLoopAlignment(MachineLoop *ML) const { | |||
54074 | if (ML->isInnermost() && | |||
54075 | ExperimentalPrefInnermostLoopAlignment.getNumOccurrences()) | |||
54076 | return Align(1ULL << ExperimentalPrefInnermostLoopAlignment); | |||
54077 | return TargetLowering::getPrefLoopAlignment(); | |||
54078 | } |
1 | //===-- X86Subtarget.h - Define Subtarget for the X86 ----------*- 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 declares the X86 specific subclass of TargetSubtargetInfo. | ||||
10 | // | ||||
11 | //===----------------------------------------------------------------------===// | ||||
12 | |||||
13 | #ifndef LLVM_LIB_TARGET_X86_X86SUBTARGET_H | ||||
14 | #define LLVM_LIB_TARGET_X86_X86SUBTARGET_H | ||||
15 | |||||
16 | #include "X86FrameLowering.h" | ||||
17 | #include "X86ISelLowering.h" | ||||
18 | #include "X86InstrInfo.h" | ||||
19 | #include "X86SelectionDAGInfo.h" | ||||
20 | #include "llvm/ADT/Triple.h" | ||||
21 | #include "llvm/CodeGen/TargetSubtargetInfo.h" | ||||
22 | #include "llvm/IR/CallingConv.h" | ||||
23 | #include <climits> | ||||
24 | #include <memory> | ||||
25 | |||||
26 | #define GET_SUBTARGETINFO_HEADER | ||||
27 | #include "X86GenSubtargetInfo.inc" | ||||
28 | |||||
29 | namespace llvm { | ||||
30 | |||||
31 | class CallLowering; | ||||
32 | class GlobalValue; | ||||
33 | class InstructionSelector; | ||||
34 | class LegalizerInfo; | ||||
35 | class RegisterBankInfo; | ||||
36 | class StringRef; | ||||
37 | class TargetMachine; | ||||
38 | |||||
39 | /// The X86 backend supports a number of different styles of PIC. | ||||
40 | /// | ||||
41 | namespace PICStyles { | ||||
42 | |||||
43 | enum class Style { | ||||
44 | StubPIC, // Used on i386-darwin in pic mode. | ||||
45 | GOT, // Used on 32 bit elf on when in pic mode. | ||||
46 | RIPRel, // Used on X86-64 when in pic mode. | ||||
47 | None // Set when not in pic mode. | ||||
48 | }; | ||||
49 | |||||
50 | } // end namespace PICStyles | ||||
51 | |||||
52 | class X86Subtarget final : public X86GenSubtargetInfo { | ||||
53 | // NOTE: Do not add anything new to this list. Coarse, CPU name based flags | ||||
54 | // are not a good idea. We should be migrating away from these. | ||||
55 | enum X86ProcFamilyEnum { | ||||
56 | Others, | ||||
57 | IntelAtom, | ||||
58 | IntelSLM | ||||
59 | }; | ||||
60 | |||||
61 | enum X86SSEEnum { | ||||
62 | NoSSE, SSE1, SSE2, SSE3, SSSE3, SSE41, SSE42, AVX, AVX2, AVX512F | ||||
63 | }; | ||||
64 | |||||
65 | enum X863DNowEnum { | ||||
66 | NoThreeDNow, MMX, ThreeDNow, ThreeDNowA | ||||
67 | }; | ||||
68 | |||||
69 | /// X86 processor family: Intel Atom, and others | ||||
70 | X86ProcFamilyEnum X86ProcFamily = Others; | ||||
71 | |||||
72 | /// Which PIC style to use | ||||
73 | PICStyles::Style PICStyle; | ||||
74 | |||||
75 | const TargetMachine &TM; | ||||
76 | |||||
77 | /// SSE1, SSE2, SSE3, SSSE3, SSE41, SSE42, or none supported. | ||||
78 | X86SSEEnum X86SSELevel = NoSSE; | ||||
79 | |||||
80 | /// MMX, 3DNow, 3DNow Athlon, or none supported. | ||||
81 | X863DNowEnum X863DNowLevel = NoThreeDNow; | ||||
82 | |||||
83 | /// True if the processor supports X87 instructions. | ||||
84 | bool HasX87 = false; | ||||
85 | |||||
86 | /// True if the processor supports CMPXCHG8B. | ||||
87 | bool HasCmpxchg8b = false; | ||||
88 | |||||
89 | /// True if this processor has NOPL instruction | ||||
90 | /// (generally pentium pro+). | ||||
91 | bool HasNOPL = false; | ||||
92 | |||||
93 | /// True if this processor has conditional move instructions | ||||
94 | /// (generally pentium pro+). | ||||
95 | bool HasCMov = false; | ||||
96 | |||||
97 | /// True if the processor supports X86-64 instructions. | ||||
98 | bool HasX86_64 = false; | ||||
99 | |||||
100 | /// True if the processor supports POPCNT. | ||||
101 | bool HasPOPCNT = false; | ||||
102 | |||||
103 | /// True if the processor supports SSE4A instructions. | ||||
104 | bool HasSSE4A = false; | ||||
105 | |||||
106 | /// Target has AES instructions | ||||
107 | bool HasAES = false; | ||||
108 | bool HasVAES = false; | ||||
109 | |||||
110 | /// Target has FXSAVE/FXRESTOR instructions | ||||
111 | bool HasFXSR = false; | ||||
112 | |||||
113 | /// Target has XSAVE instructions | ||||
114 | bool HasXSAVE = false; | ||||
115 | |||||
116 | /// Target has XSAVEOPT instructions | ||||
117 | bool HasXSAVEOPT = false; | ||||
118 | |||||
119 | /// Target has XSAVEC instructions | ||||
120 | bool HasXSAVEC = false; | ||||
121 | |||||
122 | /// Target has XSAVES instructions | ||||
123 | bool HasXSAVES = false; | ||||
124 | |||||
125 | /// Target has carry-less multiplication | ||||
126 | bool HasPCLMUL = false; | ||||
127 | bool HasVPCLMULQDQ = false; | ||||
128 | |||||
129 | /// Target has Galois Field Arithmetic instructions | ||||
130 | bool HasGFNI = false; | ||||
131 | |||||
132 | /// Target has 3-operand fused multiply-add | ||||
133 | bool HasFMA = false; | ||||
134 | |||||
135 | /// Target has 4-operand fused multiply-add | ||||
136 | bool HasFMA4 = false; | ||||
137 | |||||
138 | /// Target has XOP instructions | ||||
139 | bool HasXOP = false; | ||||
140 | |||||
141 | /// Target has TBM instructions. | ||||
142 | bool HasTBM = false; | ||||
143 | |||||
144 | /// Target has LWP instructions | ||||
145 | bool HasLWP = false; | ||||
146 | |||||
147 | /// True if the processor has the MOVBE instruction. | ||||
148 | bool HasMOVBE = false; | ||||
149 | |||||
150 | /// True if the processor has the RDRAND instruction. | ||||
151 | bool HasRDRAND = false; | ||||
152 | |||||
153 | /// Processor has 16-bit floating point conversion instructions. | ||||
154 | bool HasF16C = false; | ||||
155 | |||||
156 | /// Processor has FS/GS base insturctions. | ||||
157 | bool HasFSGSBase = false; | ||||
158 | |||||
159 | /// Processor has LZCNT instruction. | ||||
160 | bool HasLZCNT = false; | ||||
161 | |||||
162 | /// Processor has BMI1 instructions. | ||||
163 | bool HasBMI = false; | ||||
164 | |||||
165 | /// Processor has BMI2 instructions. | ||||
166 | bool HasBMI2 = false; | ||||
167 | |||||
168 | /// Processor has VBMI instructions. | ||||
169 | bool HasVBMI = false; | ||||
170 | |||||
171 | /// Processor has VBMI2 instructions. | ||||
172 | bool HasVBMI2 = false; | ||||
173 | |||||
174 | /// Processor has Integer Fused Multiply Add | ||||
175 | bool HasIFMA = false; | ||||
176 | |||||
177 | /// Processor has RTM instructions. | ||||
178 | bool HasRTM = false; | ||||
179 | |||||
180 | /// Processor has ADX instructions. | ||||
181 | bool HasADX = false; | ||||
182 | |||||
183 | /// Processor has SHA instructions. | ||||
184 | bool HasSHA = false; | ||||
185 | |||||
186 | /// Processor has PRFCHW instructions. | ||||
187 | bool HasPRFCHW = false; | ||||
188 | |||||
189 | /// Processor has RDSEED instructions. | ||||
190 | bool HasRDSEED = false; | ||||
191 | |||||
192 | /// Processor has LAHF/SAHF instructions in 64-bit mode. | ||||
193 | bool HasLAHFSAHF64 = false; | ||||
194 | |||||
195 | /// Processor has MONITORX/MWAITX instructions. | ||||
196 | bool HasMWAITX = false; | ||||
197 | |||||
198 | /// Processor has Cache Line Zero instruction | ||||
199 | bool HasCLZERO = false; | ||||
200 | |||||
201 | /// Processor has Cache Line Demote instruction | ||||
202 | bool HasCLDEMOTE = false; | ||||
203 | |||||
204 | /// Processor has MOVDIRI instruction (direct store integer). | ||||
205 | bool HasMOVDIRI = false; | ||||
206 | |||||
207 | /// Processor has MOVDIR64B instruction (direct store 64 bytes). | ||||
208 | bool HasMOVDIR64B = false; | ||||
209 | |||||
210 | /// Processor has ptwrite instruction. | ||||
211 | bool HasPTWRITE = false; | ||||
212 | |||||
213 | /// Processor has Prefetch with intent to Write instruction | ||||
214 | bool HasPREFETCHWT1 = false; | ||||
215 | |||||
216 | /// True if SHLD instructions are slow. | ||||
217 | bool IsSHLDSlow = false; | ||||
218 | |||||
219 | /// True if the PMULLD instruction is slow compared to PMULLW/PMULHW and | ||||
220 | // PMULUDQ. | ||||
221 | bool IsPMULLDSlow = false; | ||||
222 | |||||
223 | /// True if the PMADDWD instruction is slow compared to PMULLD. | ||||
224 | bool IsPMADDWDSlow = false; | ||||
225 | |||||
226 | /// True if unaligned memory accesses of 16-bytes are slow. | ||||
227 | bool IsUAMem16Slow = false; | ||||
228 | |||||
229 | /// True if unaligned memory accesses of 32-bytes are slow. | ||||
230 | bool IsUAMem32Slow = false; | ||||
231 | |||||
232 | /// True if SSE operations can have unaligned memory operands. | ||||
233 | /// This may require setting a configuration bit in the processor. | ||||
234 | bool HasSSEUnalignedMem = false; | ||||
235 | |||||
236 | /// True if this processor has the CMPXCHG16B instruction; | ||||
237 | /// this is true for most x86-64 chips, but not the first AMD chips. | ||||
238 | bool HasCmpxchg16b = false; | ||||
239 | |||||
240 | /// True if the LEA instruction should be used for adjusting | ||||
241 | /// the stack pointer. This is an optimization for Intel Atom processors. | ||||
242 | bool UseLeaForSP = false; | ||||
243 | |||||
244 | /// True if POPCNT instruction has a false dependency on the destination register. | ||||
245 | bool HasPOPCNTFalseDeps = false; | ||||
246 | |||||
247 | /// True if LZCNT/TZCNT instructions have a false dependency on the destination register. | ||||
248 | bool HasLZCNTFalseDeps = false; | ||||
249 | |||||
250 | /// True if its preferable to combine to a single cross-lane shuffle | ||||
251 | /// using a variable mask over multiple fixed shuffles. | ||||
252 | bool HasFastVariableCrossLaneShuffle = false; | ||||
253 | |||||
254 | /// True if its preferable to combine to a single per-lane shuffle | ||||
255 | /// using a variable mask over multiple fixed shuffles. | ||||
256 | bool HasFastVariablePerLaneShuffle = false; | ||||
257 | |||||
258 | /// True if vzeroupper instructions should be inserted after code that uses | ||||
259 | /// ymm or zmm registers. | ||||
260 | bool InsertVZEROUPPER = false; | ||||
261 | |||||
262 | /// True if there is no performance penalty for writing NOPs with up to | ||||
263 | /// 7 bytes. | ||||
264 | bool HasFast7ByteNOP = false; | ||||
265 | |||||
266 | /// True if there is no performance penalty for writing NOPs with up to | ||||
267 | /// 11 bytes. | ||||
268 | bool HasFast11ByteNOP = false; | ||||
269 | |||||
270 | /// True if there is no performance penalty for writing NOPs with up to | ||||
271 | /// 15 bytes. | ||||
272 | bool HasFast15ByteNOP = false; | ||||
273 | |||||
274 | /// True if gather is reasonably fast. This is true for Skylake client and | ||||
275 | /// all AVX-512 CPUs. | ||||
276 | bool HasFastGather = false; | ||||
277 | |||||
278 | /// True if hardware SQRTSS instruction is at least as fast (latency) as | ||||
279 | /// RSQRTSS followed by a Newton-Raphson iteration. | ||||
280 | bool HasFastScalarFSQRT = false; | ||||
281 | |||||
282 | /// True if hardware SQRTPS/VSQRTPS instructions are at least as fast | ||||
283 | /// (throughput) as RSQRTPS/VRSQRTPS followed by a Newton-Raphson iteration. | ||||
284 | bool HasFastVectorFSQRT = false; | ||||
285 | |||||
286 | /// True if 8-bit divisions are significantly faster than | ||||
287 | /// 32-bit divisions and should be used when possible. | ||||
288 | bool HasSlowDivide32 = false; | ||||
289 | |||||
290 | /// True if 32-bit divides are significantly faster than | ||||
291 | /// 64-bit divisions and should be used when possible. | ||||
292 | bool HasSlowDivide64 = false; | ||||
293 | |||||
294 | /// True if LZCNT instruction is fast. | ||||
295 | bool HasFastLZCNT = false; | ||||
296 | |||||
297 | /// True if SHLD based rotate is fast. | ||||
298 | bool HasFastSHLDRotate = false; | ||||
299 | |||||
300 | /// True if the processor supports macrofusion. | ||||
301 | bool HasMacroFusion = false; | ||||
302 | |||||
303 | /// True if the processor supports branch fusion. | ||||
304 | bool HasBranchFusion = false; | ||||
305 | |||||
306 | /// True if the processor has enhanced REP MOVSB/STOSB. | ||||
307 | bool HasERMSB = false; | ||||
308 | |||||
309 | /// True if the processor has fast short REP MOV. | ||||
310 | bool HasFSRM = false; | ||||
311 | |||||
312 | /// True if the short functions should be padded to prevent | ||||
313 | /// a stall when returning too early. | ||||
314 | bool PadShortFunctions = false; | ||||
315 | |||||
316 | /// True if two memory operand instructions should use a temporary register | ||||
317 | /// instead. | ||||
318 | bool SlowTwoMemOps = false; | ||||
319 | |||||
320 | /// True if the LEA instruction inputs have to be ready at address generation | ||||
321 | /// (AG) time. | ||||
322 | bool LEAUsesAG = false; | ||||
323 | |||||
324 | /// True if the LEA instruction with certain arguments is slow | ||||
325 | bool SlowLEA = false; | ||||
326 | |||||
327 | /// True if the LEA instruction has all three source operands: base, index, | ||||
328 | /// and offset or if the LEA instruction uses base and index registers where | ||||
329 | /// the base is EBP, RBP,or R13 | ||||
330 | bool Slow3OpsLEA = false; | ||||
331 | |||||
332 | /// True if INC and DEC instructions are slow when writing to flags | ||||
333 | bool SlowIncDec = false; | ||||
334 | |||||
335 | /// Processor has AVX-512 PreFetch Instructions | ||||
336 | bool HasPFI = false; | ||||
337 | |||||
338 | /// Processor has AVX-512 Exponential and Reciprocal Instructions | ||||
339 | bool HasERI = false; | ||||
340 | |||||
341 | /// Processor has AVX-512 Conflict Detection Instructions | ||||
342 | bool HasCDI = false; | ||||
343 | |||||
344 | /// Processor has AVX-512 population count Instructions | ||||
345 | bool HasVPOPCNTDQ = false; | ||||
346 | |||||
347 | /// Processor has AVX-512 Doubleword and Quadword instructions | ||||
348 | bool HasDQI = false; | ||||
349 | |||||
350 | /// Processor has AVX-512 Byte and Word instructions | ||||
351 | bool HasBWI = false; | ||||
352 | |||||
353 | /// Processor has AVX-512 Vector Length eXtenstions | ||||
354 | bool HasVLX = false; | ||||
355 | |||||
356 | /// Processor has AVX-512 16 bit floating-point extenstions | ||||
357 | bool HasFP16 = false; | ||||
358 | |||||
359 | /// Processor has PKU extenstions | ||||
360 | bool HasPKU = false; | ||||
361 | |||||
362 | /// Processor has AVX-512 Vector Neural Network Instructions | ||||
363 | bool HasVNNI = false; | ||||
364 | |||||
365 | /// Processor has AVX Vector Neural Network Instructions | ||||
366 | bool HasAVXVNNI = false; | ||||
367 | |||||
368 | /// Processor has AVX-512 bfloat16 floating-point extensions | ||||
369 | bool HasBF16 = false; | ||||
370 | |||||
371 | /// Processor supports ENQCMD instructions | ||||
372 | bool HasENQCMD = false; | ||||
373 | |||||
374 | /// Processor has AVX-512 Bit Algorithms instructions | ||||
375 | bool HasBITALG = false; | ||||
376 | |||||
377 | /// Processor has AVX-512 vp2intersect instructions | ||||
378 | bool HasVP2INTERSECT = false; | ||||
379 | |||||
380 | /// Processor supports CET SHSTK - Control-Flow Enforcement Technology | ||||
381 | /// using Shadow Stack | ||||
382 | bool HasSHSTK = false; | ||||
383 | |||||
384 | /// Processor supports Invalidate Process-Context Identifier | ||||
385 | bool HasINVPCID = false; | ||||
386 | |||||
387 | /// Processor has Software Guard Extensions | ||||
388 | bool HasSGX = false; | ||||
389 | |||||
390 | /// Processor supports Flush Cache Line instruction | ||||
391 | bool HasCLFLUSHOPT = false; | ||||
392 | |||||
393 | /// Processor supports Cache Line Write Back instruction | ||||
394 | bool HasCLWB = false; | ||||
395 | |||||
396 | /// Processor supports Write Back No Invalidate instruction | ||||
397 | bool HasWBNOINVD = false; | ||||
398 | |||||
399 | /// Processor support RDPID instruction | ||||
400 | bool HasRDPID = false; | ||||
401 | |||||
402 | /// Processor supports WaitPKG instructions | ||||
403 | bool HasWAITPKG = false; | ||||
404 | |||||
405 | /// Processor supports PCONFIG instruction | ||||
406 | bool HasPCONFIG = false; | ||||
407 | |||||
408 | /// Processor support key locker instructions | ||||
409 | bool HasKL = false; | ||||
410 | |||||
411 | /// Processor support key locker wide instructions | ||||
412 | bool HasWIDEKL = false; | ||||
413 | |||||
414 | /// Processor supports HRESET instruction | ||||
415 | bool HasHRESET = false; | ||||
416 | |||||
417 | /// Processor supports SERIALIZE instruction | ||||
418 | bool HasSERIALIZE = false; | ||||
419 | |||||
420 | /// Processor supports TSXLDTRK instruction | ||||
421 | bool HasTSXLDTRK = false; | ||||
422 | |||||
423 | /// Processor has AMX support | ||||
424 | bool HasAMXTILE = false; | ||||
425 | bool HasAMXBF16 = false; | ||||
426 | bool HasAMXINT8 = false; | ||||
427 | |||||
428 | /// Processor supports User Level Interrupt instructions | ||||
429 | bool HasUINTR = false; | ||||
430 | |||||
431 | /// Enable SSE4.2 CRC32 instruction (Used when SSE4.2 is supported but | ||||
432 | /// function is GPR only) | ||||
433 | bool HasCRC32 = false; | ||||
434 | |||||
435 | /// Processor has a single uop BEXTR implementation. | ||||
436 | bool HasFastBEXTR = false; | ||||
437 | |||||
438 | /// Try harder to combine to horizontal vector ops if they are fast. | ||||
439 | bool HasFastHorizontalOps = false; | ||||
440 | |||||
441 | /// Prefer a left/right scalar logical shifts pair over a shift+and pair. | ||||
442 | bool HasFastScalarShiftMasks = false; | ||||
443 | |||||
444 | /// Prefer a left/right vector logical shifts pair over a shift+and pair. | ||||
445 | bool HasFastVectorShiftMasks = false; | ||||
446 | |||||
447 | /// Prefer a movbe over a single-use load + bswap / single-use bswap + store. | ||||
448 | bool HasFastMOVBE = false; | ||||
449 | |||||
450 | /// Use a retpoline thunk rather than indirect calls to block speculative | ||||
451 | /// execution. | ||||
452 | bool UseRetpolineIndirectCalls = false; | ||||
453 | |||||
454 | /// Use a retpoline thunk or remove any indirect branch to block speculative | ||||
455 | /// execution. | ||||
456 | bool UseRetpolineIndirectBranches = false; | ||||
457 | |||||
458 | /// Deprecated flag, query `UseRetpolineIndirectCalls` and | ||||
459 | /// `UseRetpolineIndirectBranches` instead. | ||||
460 | bool DeprecatedUseRetpoline = false; | ||||
461 | |||||
462 | /// When using a retpoline thunk, call an externally provided thunk rather | ||||
463 | /// than emitting one inside the compiler. | ||||
464 | bool UseRetpolineExternalThunk = false; | ||||
465 | |||||
466 | /// Prevent generation of indirect call/branch instructions from memory, | ||||
467 | /// and force all indirect call/branch instructions from a register to be | ||||
468 | /// preceded by an LFENCE. Also decompose RET instructions into a | ||||
469 | /// POP+LFENCE+JMP sequence. | ||||
470 | bool UseLVIControlFlowIntegrity = false; | ||||
471 | |||||
472 | /// Enable Speculative Execution Side Effect Suppression | ||||
473 | bool UseSpeculativeExecutionSideEffectSuppression = false; | ||||
474 | |||||
475 | /// Insert LFENCE instructions to prevent data speculatively injected into | ||||
476 | /// loads from being used maliciously. | ||||
477 | bool UseLVILoadHardening = false; | ||||
478 | |||||
479 | /// Use software floating point for code generation. | ||||
480 | bool UseSoftFloat = false; | ||||
481 | |||||
482 | /// Use alias analysis during code generation. | ||||
483 | bool UseAA = false; | ||||
484 | |||||
485 | /// The minimum alignment known to hold of the stack frame on | ||||
486 | /// entry to the function and which must be maintained by every function. | ||||
487 | Align stackAlignment = Align(4); | ||||
488 | |||||
489 | Align TileConfigAlignment = Align(4); | ||||
490 | |||||
491 | /// Max. memset / memcpy size that is turned into rep/movs, rep/stos ops. | ||||
492 | /// | ||||
493 | // FIXME: this is a known good value for Yonah. How about others? | ||||
494 | unsigned MaxInlineSizeThreshold = 128; | ||||
495 | |||||
496 | /// Indicates target prefers 128 bit instructions. | ||||
497 | bool Prefer128Bit = false; | ||||
498 | |||||
499 | /// Indicates target prefers 256 bit instructions. | ||||
500 | bool Prefer256Bit = false; | ||||
501 | |||||
502 | /// Indicates target prefers AVX512 mask registers. | ||||
503 | bool PreferMaskRegisters = false; | ||||
504 | |||||
505 | /// Use Goldmont specific floating point div/sqrt costs. | ||||
506 | bool UseGLMDivSqrtCosts = false; | ||||
507 | |||||
508 | /// What processor and OS we're targeting. | ||||
509 | Triple TargetTriple; | ||||
510 | |||||
511 | /// GlobalISel related APIs. | ||||
512 | std::unique_ptr<CallLowering> CallLoweringInfo; | ||||
513 | std::unique_ptr<LegalizerInfo> Legalizer; | ||||
514 | std::unique_ptr<RegisterBankInfo> RegBankInfo; | ||||
515 | std::unique_ptr<InstructionSelector> InstSelector; | ||||
516 | |||||
517 | private: | ||||
518 | /// Override the stack alignment. | ||||
519 | MaybeAlign StackAlignOverride; | ||||
520 | |||||
521 | /// Preferred vector width from function attribute. | ||||
522 | unsigned PreferVectorWidthOverride; | ||||
523 | |||||
524 | /// Resolved preferred vector width from function attribute and subtarget | ||||
525 | /// features. | ||||
526 | unsigned PreferVectorWidth = UINT32_MAX(4294967295U); | ||||
527 | |||||
528 | /// Required vector width from function attribute. | ||||
529 | unsigned RequiredVectorWidth; | ||||
530 | |||||
531 | /// True if compiling for 64-bit, false for 16-bit or 32-bit. | ||||
532 | bool In64BitMode = false; | ||||
533 | |||||
534 | /// True if compiling for 32-bit, false for 16-bit or 64-bit. | ||||
535 | bool In32BitMode = false; | ||||
536 | |||||
537 | /// True if compiling for 16-bit, false for 32-bit or 64-bit. | ||||
538 | bool In16BitMode = false; | ||||
539 | |||||
540 | X86SelectionDAGInfo TSInfo; | ||||
541 | // Ordering here is important. X86InstrInfo initializes X86RegisterInfo which | ||||
542 | // X86TargetLowering needs. | ||||
543 | X86InstrInfo InstrInfo; | ||||
544 | X86TargetLowering TLInfo; | ||||
545 | X86FrameLowering FrameLowering; | ||||
546 | |||||
547 | public: | ||||
548 | /// This constructor initializes the data members to match that | ||||
549 | /// of the specified triple. | ||||
550 | /// | ||||
551 | X86Subtarget(const Triple &TT, StringRef CPU, StringRef TuneCPU, StringRef FS, | ||||
552 | const X86TargetMachine &TM, MaybeAlign StackAlignOverride, | ||||
553 | unsigned PreferVectorWidthOverride, | ||||
554 | unsigned RequiredVectorWidth); | ||||
555 | |||||
556 | const X86TargetLowering *getTargetLowering() const override { | ||||
557 | return &TLInfo; | ||||
558 | } | ||||
559 | |||||
560 | const X86InstrInfo *getInstrInfo() const override { return &InstrInfo; } | ||||
561 | |||||
562 | const X86FrameLowering *getFrameLowering() const override { | ||||
563 | return &FrameLowering; | ||||
564 | } | ||||
565 | |||||
566 | const X86SelectionDAGInfo *getSelectionDAGInfo() const override { | ||||
567 | return &TSInfo; | ||||
568 | } | ||||
569 | |||||
570 | const X86RegisterInfo *getRegisterInfo() const override { | ||||
571 | return &getInstrInfo()->getRegisterInfo(); | ||||
572 | } | ||||
573 | |||||
574 | unsigned getTileConfigSize() const { return 64; } | ||||
575 | Align getTileConfigAlignment() const { return TileConfigAlignment; } | ||||
576 | |||||
577 | /// Returns the minimum alignment known to hold of the | ||||
578 | /// stack frame on entry to the function and which must be maintained by every | ||||
579 | /// function for this subtarget. | ||||
580 | Align getStackAlignment() const { return stackAlignment; } | ||||
581 | |||||
582 | /// Returns the maximum memset / memcpy size | ||||
583 | /// that still makes it profitable to inline the call. | ||||
584 | unsigned getMaxInlineSizeThreshold() const { return MaxInlineSizeThreshold; } | ||||
585 | |||||
586 | /// ParseSubtargetFeatures - Parses features string setting specified | ||||
587 | /// subtarget options. Definition of function is auto generated by tblgen. | ||||
588 | void ParseSubtargetFeatures(StringRef CPU, StringRef TuneCPU, StringRef FS); | ||||
589 | |||||
590 | /// Methods used by Global ISel | ||||
591 | const CallLowering *getCallLowering() const override; | ||||
592 | InstructionSelector *getInstructionSelector() const override; | ||||
593 | const LegalizerInfo *getLegalizerInfo() const override; | ||||
594 | const RegisterBankInfo *getRegBankInfo() const override; | ||||
595 | |||||
596 | private: | ||||
597 | /// Initialize the full set of dependencies so we can use an initializer | ||||
598 | /// list for X86Subtarget. | ||||
599 | X86Subtarget &initializeSubtargetDependencies(StringRef CPU, | ||||
600 | StringRef TuneCPU, | ||||
601 | StringRef FS); | ||||
602 | void initSubtargetFeatures(StringRef CPU, StringRef TuneCPU, StringRef FS); | ||||
603 | |||||
604 | public: | ||||
605 | /// Is this x86_64? (disregarding specific ABI / programming model) | ||||
606 | bool is64Bit() const { | ||||
607 | return In64BitMode; | ||||
608 | } | ||||
609 | |||||
610 | bool is32Bit() const { | ||||
611 | return In32BitMode; | ||||
612 | } | ||||
613 | |||||
614 | bool is16Bit() const { | ||||
615 | return In16BitMode; | ||||
616 | } | ||||
617 | |||||
618 | /// Is this x86_64 with the ILP32 programming model (x32 ABI)? | ||||
619 | bool isTarget64BitILP32() const { | ||||
620 | return In64BitMode && (TargetTriple.isX32() || TargetTriple.isOSNaCl()); | ||||
621 | } | ||||
622 | |||||
623 | /// Is this x86_64 with the LP64 programming model (standard AMD64, no x32)? | ||||
624 | bool isTarget64BitLP64() const { | ||||
625 | return In64BitMode && (!TargetTriple.isX32() && !TargetTriple.isOSNaCl()); | ||||
626 | } | ||||
627 | |||||
628 | PICStyles::Style getPICStyle() const { return PICStyle; } | ||||
629 | void setPICStyle(PICStyles::Style Style) { PICStyle = Style; } | ||||
630 | |||||
631 | bool hasX87() const { return HasX87; } | ||||
632 | bool hasCmpxchg8b() const { return HasCmpxchg8b; } | ||||
633 | bool hasNOPL() const { return HasNOPL; } | ||||
634 | // SSE codegen depends on cmovs, and all SSE1+ processors support them. | ||||
635 | // All 64-bit processors support cmov. | ||||
636 | bool hasCMov() const { return HasCMov || X86SSELevel >= SSE1 || is64Bit(); } | ||||
637 | bool hasSSE1() const { return X86SSELevel >= SSE1; } | ||||
638 | bool hasSSE2() const { return X86SSELevel >= SSE2; } | ||||
639 | bool hasSSE3() const { return X86SSELevel >= SSE3; } | ||||
640 | bool hasSSSE3() const { return X86SSELevel >= SSSE3; } | ||||
641 | bool hasSSE41() const { return X86SSELevel >= SSE41; } | ||||
642 | bool hasSSE42() const { return X86SSELevel >= SSE42; } | ||||
643 | bool hasAVX() const { return X86SSELevel >= AVX; } | ||||
644 | bool hasAVX2() const { return X86SSELevel
| ||||
645 | bool hasAVX512() const { return X86SSELevel >= AVX512F; } | ||||
646 | bool hasInt256() const { return hasAVX2(); } | ||||
647 | bool hasSSE4A() const { return HasSSE4A; } | ||||
648 | bool hasMMX() const { return X863DNowLevel >= MMX; } | ||||
649 | bool has3DNow() const { return X863DNowLevel >= ThreeDNow; } | ||||
650 | bool has3DNowA() const { return X863DNowLevel >= ThreeDNowA; } | ||||
651 | bool hasPOPCNT() const { return HasPOPCNT; } | ||||
652 | bool hasAES() const { return HasAES; } | ||||
653 | bool hasVAES() const { return HasVAES; } | ||||
654 | bool hasFXSR() const { return HasFXSR; } | ||||
655 | bool hasXSAVE() const { return HasXSAVE; } | ||||
656 | bool hasXSAVEOPT() const { return HasXSAVEOPT; } | ||||
657 | bool hasXSAVEC() const { return HasXSAVEC; } | ||||
658 | bool hasXSAVES() const { return HasXSAVES; } | ||||
659 | bool hasPCLMUL() const { return HasPCLMUL; } | ||||
660 | bool hasVPCLMULQDQ() const { return HasVPCLMULQDQ; } | ||||
661 | bool hasGFNI() const { return HasGFNI; } | ||||
662 | // Prefer FMA4 to FMA - its better for commutation/memory folding and | ||||
663 | // has equal or better performance on all supported targets. | ||||
664 | bool hasFMA() const { return HasFMA; } | ||||
665 | bool hasFMA4() const { return HasFMA4; } | ||||
666 | bool hasAnyFMA() const { return hasFMA() || hasFMA4(); } | ||||
667 | bool hasXOP() const { return HasXOP; } | ||||
668 | bool hasTBM() const { return HasTBM; } | ||||
669 | bool hasLWP() const { return HasLWP; } | ||||
670 | bool hasMOVBE() const { return HasMOVBE; } | ||||
671 | bool hasRDRAND() const { return HasRDRAND; } | ||||
672 | bool hasF16C() const { return HasF16C; } | ||||
673 | bool hasFSGSBase() const { return HasFSGSBase; } | ||||
674 | bool hasLZCNT() const { return HasLZCNT; } | ||||
675 | bool hasBMI() const { return HasBMI; } | ||||
676 | bool hasBMI2() const { return HasBMI2; } | ||||
677 | bool hasVBMI() const { return HasVBMI; } | ||||
678 | bool hasVBMI2() const { return HasVBMI2; } | ||||
679 | bool hasIFMA() const { return HasIFMA; } | ||||
680 | bool hasRTM() const { return HasRTM; } | ||||
681 | bool hasADX() const { return HasADX; } | ||||
682 | bool hasSHA() const { return HasSHA; } | ||||
683 | bool hasPRFCHW() const { return HasPRFCHW; } | ||||
684 | bool hasPREFETCHWT1() const { return HasPREFETCHWT1; } | ||||
685 | bool hasPrefetchW() const { | ||||
686 | // The PREFETCHW instruction was added with 3DNow but later CPUs gave it | ||||
687 | // its own CPUID bit as part of deprecating 3DNow. Intel eventually added | ||||
688 | // it and KNL has another that prefetches to L2 cache. We assume the | ||||
689 | // L1 version exists if the L2 version does. | ||||
690 | return has3DNow() || hasPRFCHW() || hasPREFETCHWT1(); | ||||
691 | } | ||||
692 | bool hasSSEPrefetch() const { | ||||
693 | // We implicitly enable these when we have a write prefix supporting cache | ||||
694 | // level OR if we have prfchw, but don't already have a read prefetch from | ||||
695 | // 3dnow. | ||||
696 | return hasSSE1() || (hasPRFCHW() && !has3DNow()) || hasPREFETCHWT1(); | ||||
697 | } | ||||
698 | bool hasRDSEED() const { return HasRDSEED; } | ||||
699 | bool hasLAHFSAHF() const { return HasLAHFSAHF64 || !is64Bit(); } | ||||
700 | bool hasMWAITX() const { return HasMWAITX; } | ||||
701 | bool hasCLZERO() const { return HasCLZERO; } | ||||
702 | bool hasCLDEMOTE() const { return HasCLDEMOTE; } | ||||
703 | bool hasMOVDIRI() const { return HasMOVDIRI; } | ||||
704 | bool hasMOVDIR64B() const { return HasMOVDIR64B; } | ||||
705 | bool hasPTWRITE() const { return HasPTWRITE; } | ||||
706 | bool isSHLDSlow() const { return IsSHLDSlow; } | ||||
707 | bool isPMULLDSlow() const { return IsPMULLDSlow; } | ||||
708 | bool isPMADDWDSlow() const { return IsPMADDWDSlow; } | ||||
709 | bool isUnalignedMem16Slow() const { return IsUAMem16Slow; } | ||||
710 | bool isUnalignedMem32Slow() const { return IsUAMem32Slow; } | ||||
711 | bool hasSSEUnalignedMem() const { return HasSSEUnalignedMem; } | ||||
712 | bool hasCmpxchg16b() const { return HasCmpxchg16b && is64Bit(); } | ||||
713 | bool useLeaForSP() const { return UseLeaForSP; } | ||||
714 | bool hasPOPCNTFalseDeps() const { return HasPOPCNTFalseDeps; } | ||||
715 | bool hasLZCNTFalseDeps() const { return HasLZCNTFalseDeps; } | ||||
716 | bool hasFastVariableCrossLaneShuffle() const { | ||||
717 | return HasFastVariableCrossLaneShuffle; | ||||
718 | } | ||||
719 | bool hasFastVariablePerLaneShuffle() const { | ||||
720 | return HasFastVariablePerLaneShuffle; | ||||
721 | } | ||||
722 | bool insertVZEROUPPER() const { return InsertVZEROUPPER; } | ||||
723 | bool hasFastGather() const { return HasFastGather; } | ||||
724 | bool hasFastScalarFSQRT() const { return HasFastScalarFSQRT; } | ||||
725 | bool hasFastVectorFSQRT() const { return HasFastVectorFSQRT; } | ||||
726 | bool hasFastLZCNT() const { return HasFastLZCNT; } | ||||
727 | bool hasFastSHLDRotate() const { return HasFastSHLDRotate; } | ||||
728 | bool hasFastBEXTR() const { return HasFastBEXTR; } | ||||
729 | bool hasFastHorizontalOps() const { return HasFastHorizontalOps; } | ||||
730 | bool hasFastScalarShiftMasks() const { return HasFastScalarShiftMasks; } | ||||
731 | bool hasFastVectorShiftMasks() const { return HasFastVectorShiftMasks; } | ||||
732 | bool hasFastMOVBE() const { return HasFastMOVBE; } | ||||
733 | bool hasMacroFusion() const { return HasMacroFusion; } | ||||
734 | bool hasBranchFusion() const { return HasBranchFusion; } | ||||
735 | bool hasERMSB() const { return HasERMSB; } | ||||
736 | bool hasFSRM() const { return HasFSRM; } | ||||
737 | bool hasSlowDivide32() const { return HasSlowDivide32; } | ||||
738 | bool hasSlowDivide64() const { return HasSlowDivide64; } | ||||
739 | bool padShortFunctions() const { return PadShortFunctions; } | ||||
740 | bool slowTwoMemOps() const { return SlowTwoMemOps; } | ||||
741 | bool LEAusesAG() const { return LEAUsesAG; } | ||||
742 | bool slowLEA() const { return SlowLEA; } | ||||
743 | bool slow3OpsLEA() const { return Slow3OpsLEA; } | ||||
744 | bool slowIncDec() const { return SlowIncDec; } | ||||
745 | bool hasCDI() const { return HasCDI; } | ||||
746 | bool hasVPOPCNTDQ() const { return HasVPOPCNTDQ; } | ||||
747 | bool hasPFI() const { return HasPFI; } | ||||
748 | bool hasERI() const { return HasERI; } | ||||
749 | bool hasDQI() const { return HasDQI; } | ||||
750 | bool hasBWI() const { return HasBWI; } | ||||
751 | bool hasVLX() const { return HasVLX; } | ||||
752 | bool hasFP16() const { return HasFP16; } | ||||
753 | bool hasPKU() const { return HasPKU; } | ||||
754 | bool hasVNNI() const { return HasVNNI; } | ||||
755 | bool hasBF16() const { return HasBF16; } | ||||
756 | bool hasVP2INTERSECT() const { return HasVP2INTERSECT; } | ||||
757 | bool hasBITALG() const { return HasBITALG; } | ||||
758 | bool hasSHSTK() const { return HasSHSTK; } | ||||
759 | bool hasCLFLUSHOPT() const { return HasCLFLUSHOPT; } | ||||
760 | bool hasCLWB() const { return HasCLWB; } | ||||
761 | bool hasWBNOINVD() const { return HasWBNOINVD; } | ||||
762 | bool hasRDPID() const { return HasRDPID; } | ||||
763 | bool hasWAITPKG() const { return HasWAITPKG; } | ||||
764 | bool hasPCONFIG() const { return HasPCONFIG; } | ||||
765 | bool hasSGX() const { return HasSGX; } | ||||
766 | bool hasINVPCID() const { return HasINVPCID; } | ||||
767 | bool hasENQCMD() const { return HasENQCMD; } | ||||
768 | bool hasKL() const { return HasKL; } | ||||
769 | bool hasWIDEKL() const { return HasWIDEKL; } | ||||
770 | bool hasHRESET() const { return HasHRESET; } | ||||
771 | bool hasSERIALIZE() const { return HasSERIALIZE; } | ||||
772 | bool hasTSXLDTRK() const { return HasTSXLDTRK; } | ||||
773 | bool hasUINTR() const { return HasUINTR; } | ||||
774 | bool hasCRC32() const { return HasCRC32; } | ||||
775 | bool useRetpolineIndirectCalls() const { return UseRetpolineIndirectCalls; } | ||||
776 | bool useRetpolineIndirectBranches() const { | ||||
777 | return UseRetpolineIndirectBranches; | ||||
778 | } | ||||
779 | bool hasAVXVNNI() const { return HasAVXVNNI; } | ||||
780 | bool hasAMXTILE() const { return HasAMXTILE; } | ||||
781 | bool hasAMXBF16() const { return HasAMXBF16; } | ||||
782 | bool hasAMXINT8() const { return HasAMXINT8; } | ||||
783 | bool useRetpolineExternalThunk() const { return UseRetpolineExternalThunk; } | ||||
784 | |||||
785 | // These are generic getters that OR together all of the thunk types | ||||
786 | // supported by the subtarget. Therefore useIndirectThunk*() will return true | ||||
787 | // if any respective thunk feature is enabled. | ||||
788 | bool useIndirectThunkCalls() const { | ||||
789 | return useRetpolineIndirectCalls() || useLVIControlFlowIntegrity(); | ||||
790 | } | ||||
791 | bool useIndirectThunkBranches() const { | ||||
792 | return useRetpolineIndirectBranches() || useLVIControlFlowIntegrity(); | ||||
793 | } | ||||
794 | |||||
795 | bool preferMaskRegisters() const { return PreferMaskRegisters; } | ||||
796 | bool useGLMDivSqrtCosts() const { return UseGLMDivSqrtCosts; } | ||||
797 | bool useLVIControlFlowIntegrity() const { return UseLVIControlFlowIntegrity; } | ||||
798 | bool useLVILoadHardening() const { return UseLVILoadHardening; } | ||||
799 | bool useSpeculativeExecutionSideEffectSuppression() const { | ||||
800 | return UseSpeculativeExecutionSideEffectSuppression; | ||||
801 | } | ||||
802 | |||||
803 | unsigned getPreferVectorWidth() const { return PreferVectorWidth; } | ||||
804 | unsigned getRequiredVectorWidth() const { return RequiredVectorWidth; } | ||||
805 | |||||
806 | // Helper functions to determine when we should allow widening to 512-bit | ||||
807 | // during codegen. | ||||
808 | // TODO: Currently we're always allowing widening on CPUs without VLX, | ||||
809 | // because for many cases we don't have a better option. | ||||
810 | bool canExtendTo512DQ() const { | ||||
811 | return hasAVX512() && (!hasVLX() || getPreferVectorWidth() >= 512); | ||||
812 | } | ||||
813 | bool canExtendTo512BW() const { | ||||
814 | return hasBWI() && canExtendTo512DQ(); | ||||
815 | } | ||||
816 | |||||
817 | // If there are no 512-bit vectors and we prefer not to use 512-bit registers, | ||||
818 | // disable them in the legalizer. | ||||
819 | bool useAVX512Regs() const { | ||||
820 | return hasAVX512() && (canExtendTo512DQ() || RequiredVectorWidth > 256); | ||||
821 | } | ||||
822 | |||||
823 | bool useBWIRegs() const { | ||||
824 | return hasBWI() && useAVX512Regs(); | ||||
825 | } | ||||
826 | |||||
827 | bool isXRaySupported() const override { return is64Bit(); } | ||||
828 | |||||
829 | /// TODO: to be removed later and replaced with suitable properties | ||||
830 | bool isAtom() const { return X86ProcFamily == IntelAtom; } | ||||
831 | bool isSLM() const { return X86ProcFamily == IntelSLM; } | ||||
832 | bool useSoftFloat() const { return UseSoftFloat; } | ||||
833 | bool useAA() const override { return UseAA; } | ||||
834 | |||||
835 | /// Use mfence if we have SSE2 or we're on x86-64 (even if we asked for | ||||
836 | /// no-sse2). There isn't any reason to disable it if the target processor | ||||
837 | /// supports it. | ||||
838 | bool hasMFence() const { return hasSSE2() || is64Bit(); } | ||||
839 | |||||
840 | const Triple &getTargetTriple() const { return TargetTriple; } | ||||
841 | |||||
842 | bool isTargetDarwin() const { return TargetTriple.isOSDarwin(); } | ||||
843 | bool isTargetFreeBSD() const { return TargetTriple.isOSFreeBSD(); } | ||||
844 | bool isTargetDragonFly() const { return TargetTriple.isOSDragonFly(); } | ||||
845 | bool isTargetSolaris() const { return TargetTriple.isOSSolaris(); } | ||||
846 | bool isTargetPS4() const { return TargetTriple.isPS4CPU(); } | ||||
847 | |||||
848 | bool isTargetELF() const { return TargetTriple.isOSBinFormatELF(); } | ||||
849 | bool isTargetCOFF() const { return TargetTriple.isOSBinFormatCOFF(); } | ||||
850 | bool isTargetMachO() const { return TargetTriple.isOSBinFormatMachO(); } | ||||
851 | |||||
852 | bool isTargetLinux() const { return TargetTriple.isOSLinux(); } | ||||
853 | bool isTargetKFreeBSD() const { return TargetTriple.isOSKFreeBSD(); } | ||||
854 | bool isTargetGlibc() const { return TargetTriple.isOSGlibc(); } | ||||
855 | bool isTargetAndroid() const { return TargetTriple.isAndroid(); } | ||||
856 | bool isTargetNaCl() const { return TargetTriple.isOSNaCl(); } | ||||
857 | bool isTargetNaCl32() const { return isTargetNaCl() && !is64Bit(); } | ||||
858 | bool isTargetNaCl64() const { return isTargetNaCl() && is64Bit(); } | ||||
859 | bool isTargetMCU() const { return TargetTriple.isOSIAMCU(); } | ||||
860 | bool isTargetFuchsia() const { return TargetTriple.isOSFuchsia(); } | ||||
861 | |||||
862 | bool isTargetWindowsMSVC() const { | ||||
863 | return TargetTriple.isWindowsMSVCEnvironment(); | ||||
864 | } | ||||
865 | |||||
866 | bool isTargetWindowsCoreCLR() const { | ||||
867 | return TargetTriple.isWindowsCoreCLREnvironment(); | ||||
868 | } | ||||
869 | |||||
870 | bool isTargetWindowsCygwin() const { | ||||
871 | return TargetTriple.isWindowsCygwinEnvironment(); | ||||
872 | } | ||||
873 | |||||
874 | bool isTargetWindowsGNU() const { | ||||
875 | return TargetTriple.isWindowsGNUEnvironment(); | ||||
876 | } | ||||
877 | |||||
878 | bool isTargetWindowsItanium() const { | ||||
879 | return TargetTriple.isWindowsItaniumEnvironment(); | ||||
880 | } | ||||
881 | |||||
882 | bool isTargetCygMing() const { return TargetTriple.isOSCygMing(); } | ||||
883 | |||||
884 | bool isOSWindows() const { return TargetTriple.isOSWindows(); } | ||||
885 | |||||
886 | bool isTargetWin64() const { return In64BitMode && isOSWindows(); } | ||||
887 | |||||
888 | bool isTargetWin32() const { return !In64BitMode && isOSWindows(); } | ||||
889 | |||||
890 | bool isPICStyleGOT() const { return PICStyle == PICStyles::Style::GOT; } | ||||
891 | bool isPICStyleRIPRel() const { return PICStyle == PICStyles::Style::RIPRel; } | ||||
892 | |||||
893 | bool isPICStyleStubPIC() const { | ||||
894 | return PICStyle == PICStyles::Style::StubPIC; | ||||
895 | } | ||||
896 | |||||
897 | bool isPositionIndependent() const; | ||||
898 | |||||
899 | bool isCallingConvWin64(CallingConv::ID CC) const { | ||||
900 | switch (CC) { | ||||
901 | // On Win64, all these conventions just use the default convention. | ||||
902 | case CallingConv::C: | ||||
903 | case CallingConv::Fast: | ||||
904 | case CallingConv::Tail: | ||||
905 | case CallingConv::Swift: | ||||
906 | case CallingConv::SwiftTail: | ||||
907 | case CallingConv::X86_FastCall: | ||||
908 | case CallingConv::X86_StdCall: | ||||
909 | case CallingConv::X86_ThisCall: | ||||
910 | case CallingConv::X86_VectorCall: | ||||
911 | case CallingConv::Intel_OCL_BI: | ||||
912 | return isTargetWin64(); | ||||
913 | // This convention allows using the Win64 convention on other targets. | ||||
914 | case CallingConv::Win64: | ||||
915 | return true; | ||||
916 | // This convention allows using the SysV convention on Windows targets. | ||||
917 | case CallingConv::X86_64_SysV: | ||||
918 | return false; | ||||
919 | // Otherwise, who knows what this is. | ||||
920 | default: | ||||
921 | return false; | ||||
922 | } | ||||
923 | } | ||||
924 | |||||
925 | /// Classify a global variable reference for the current subtarget according | ||||
926 | /// to how we should reference it in a non-pcrel context. | ||||
927 | unsigned char classifyLocalReference(const GlobalValue *GV) const; | ||||
928 | |||||
929 | unsigned char classifyGlobalReference(const GlobalValue *GV, | ||||
930 | const Module &M) const; | ||||
931 | unsigned char classifyGlobalReference(const GlobalValue *GV) const; | ||||
932 | |||||
933 | /// Classify a global function reference for the current subtarget. | ||||
934 | unsigned char classifyGlobalFunctionReference(const GlobalValue *GV, | ||||
935 | const Module &M) const; | ||||
936 | unsigned char classifyGlobalFunctionReference(const GlobalValue *GV) const; | ||||
937 | |||||
938 | /// Classify a blockaddress reference for the current subtarget according to | ||||
939 | /// how we should reference it in a non-pcrel context. | ||||
940 | unsigned char classifyBlockAddressReference() const; | ||||
941 | |||||
942 | /// Return true if the subtarget allows calls to immediate address. | ||||
943 | bool isLegalToCallImmediateAddr() const; | ||||
944 | |||||
945 | /// Return whether FrameLowering should always set the "extended frame | ||||
946 | /// present" bit in FP, or set it based on a symbol in the runtime. | ||||
947 | bool swiftAsyncContextIsDynamicallySet() const { | ||||
948 | // Older OS versions (particularly system unwinders) are confused by the | ||||
949 | // Swift extended frame, so when building code that might be run on them we | ||||
950 | // must dynamically query the concurrency library to determine whether | ||||
951 | // extended frames should be flagged as present. | ||||
952 | const Triple &TT = getTargetTriple(); | ||||
953 | |||||
954 | unsigned Major, Minor, Micro; | ||||
955 | TT.getOSVersion(Major, Minor, Micro); | ||||
956 | switch(TT.getOS()) { | ||||
957 | default: | ||||
958 | return false; | ||||
959 | case Triple::IOS: | ||||
960 | case Triple::TvOS: | ||||
961 | return Major < 15; | ||||
962 | case Triple::WatchOS: | ||||
963 | return Major < 8; | ||||
964 | case Triple::MacOSX: | ||||
965 | case Triple::Darwin: | ||||
966 | return Major < 12; | ||||
967 | } | ||||
968 | } | ||||
969 | |||||
970 | /// If we are using indirect thunks, we need to expand indirectbr to avoid it | ||||
971 | /// lowering to an actual indirect jump. | ||||
972 | bool enableIndirectBrExpand() const override { | ||||
973 | return useIndirectThunkBranches(); | ||||
974 | } | ||||
975 | |||||
976 | /// Enable the MachineScheduler pass for all X86 subtargets. | ||||
977 | bool enableMachineScheduler() const override { return true; } | ||||
978 | |||||
979 | bool enableEarlyIfConversion() const override; | ||||
980 | |||||
981 | void getPostRAMutations(std::vector<std::unique_ptr<ScheduleDAGMutation>> | ||||
982 | &Mutations) const override; | ||||
983 | |||||
984 | AntiDepBreakMode getAntiDepBreakMode() const override { | ||||
985 | return TargetSubtargetInfo::ANTIDEP_CRITICAL; | ||||
986 | } | ||||
987 | |||||
988 | bool enableAdvancedRASplitCost() const override { return false; } | ||||
989 | }; | ||||
990 | |||||
991 | } // end namespace llvm | ||||
992 | |||||
993 | #endif // LLVM_LIB_TARGET_X86_X86SUBTARGET_H |