[x86] lower calls to fmin and llvm.minnum.* using minss/minsd/minps/minpd (PR24475)
[oota-llvm.git] / lib / Target / X86 / X86ISelLowering.cpp
1 //===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the interfaces that X86 uses to lower LLVM code into a
11 // selection DAG.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "X86ISelLowering.h"
16 #include "Utils/X86ShuffleDecode.h"
17 #include "X86CallingConv.h"
18 #include "X86FrameLowering.h"
19 #include "X86InstrBuilder.h"
20 #include "X86MachineFunctionInfo.h"
21 #include "X86TargetMachine.h"
22 #include "X86TargetObjectFile.h"
23 #include "llvm/ADT/SmallBitVector.h"
24 #include "llvm/ADT/SmallSet.h"
25 #include "llvm/ADT/Statistic.h"
26 #include "llvm/ADT/StringExtras.h"
27 #include "llvm/ADT/StringSwitch.h"
28 #include "llvm/Analysis/EHPersonalities.h"
29 #include "llvm/CodeGen/IntrinsicLowering.h"
30 #include "llvm/CodeGen/MachineFrameInfo.h"
31 #include "llvm/CodeGen/MachineFunction.h"
32 #include "llvm/CodeGen/MachineInstrBuilder.h"
33 #include "llvm/CodeGen/MachineJumpTableInfo.h"
34 #include "llvm/CodeGen/MachineModuleInfo.h"
35 #include "llvm/CodeGen/MachineRegisterInfo.h"
36 #include "llvm/CodeGen/WinEHFuncInfo.h"
37 #include "llvm/IR/CallSite.h"
38 #include "llvm/IR/CallingConv.h"
39 #include "llvm/IR/Constants.h"
40 #include "llvm/IR/DerivedTypes.h"
41 #include "llvm/IR/Function.h"
42 #include "llvm/IR/GlobalAlias.h"
43 #include "llvm/IR/GlobalVariable.h"
44 #include "llvm/IR/Instructions.h"
45 #include "llvm/IR/Intrinsics.h"
46 #include "llvm/MC/MCAsmInfo.h"
47 #include "llvm/MC/MCContext.h"
48 #include "llvm/MC/MCExpr.h"
49 #include "llvm/MC/MCSymbol.h"
50 #include "llvm/Support/CommandLine.h"
51 #include "llvm/Support/Debug.h"
52 #include "llvm/Support/ErrorHandling.h"
53 #include "llvm/Support/MathExtras.h"
54 #include "llvm/Target/TargetOptions.h"
55 #include "X86IntrinsicsInfo.h"
56 #include <bitset>
57 #include <numeric>
58 #include <cctype>
59 using namespace llvm;
60
61 #define DEBUG_TYPE "x86-isel"
62
63 STATISTIC(NumTailCalls, "Number of tail calls");
64
65 static cl::opt<bool> ExperimentalVectorWideningLegalization(
66     "x86-experimental-vector-widening-legalization", cl::init(false),
67     cl::desc("Enable an experimental vector type legalization through widening "
68              "rather than promotion."),
69     cl::Hidden);
70
71 X86TargetLowering::X86TargetLowering(const X86TargetMachine &TM,
72                                      const X86Subtarget &STI)
73     : TargetLowering(TM), Subtarget(&STI) {
74   X86ScalarSSEf64 = Subtarget->hasSSE2();
75   X86ScalarSSEf32 = Subtarget->hasSSE1();
76   MVT PtrVT = MVT::getIntegerVT(8 * TM.getPointerSize());
77
78   // Set up the TargetLowering object.
79
80   // X86 is weird. It always uses i8 for shift amounts and setcc results.
81   setBooleanContents(ZeroOrOneBooleanContent);
82   // X86-SSE is even stranger. It uses -1 or 0 for vector masks.
83   setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
84
85   // For 64-bit, since we have so many registers, use the ILP scheduler.
86   // For 32-bit, use the register pressure specific scheduling.
87   // For Atom, always use ILP scheduling.
88   if (Subtarget->isAtom())
89     setSchedulingPreference(Sched::ILP);
90   else if (Subtarget->is64Bit())
91     setSchedulingPreference(Sched::ILP);
92   else
93     setSchedulingPreference(Sched::RegPressure);
94   const X86RegisterInfo *RegInfo = Subtarget->getRegisterInfo();
95   setStackPointerRegisterToSaveRestore(RegInfo->getStackRegister());
96
97   // Bypass expensive divides on Atom when compiling with O2.
98   if (TM.getOptLevel() >= CodeGenOpt::Default) {
99     if (Subtarget->hasSlowDivide32())
100       addBypassSlowDiv(32, 8);
101     if (Subtarget->hasSlowDivide64() && Subtarget->is64Bit())
102       addBypassSlowDiv(64, 16);
103   }
104
105   if (Subtarget->isTargetKnownWindowsMSVC()) {
106     // Setup Windows compiler runtime calls.
107     setLibcallName(RTLIB::SDIV_I64, "_alldiv");
108     setLibcallName(RTLIB::UDIV_I64, "_aulldiv");
109     setLibcallName(RTLIB::SREM_I64, "_allrem");
110     setLibcallName(RTLIB::UREM_I64, "_aullrem");
111     setLibcallName(RTLIB::MUL_I64, "_allmul");
112     setLibcallCallingConv(RTLIB::SDIV_I64, CallingConv::X86_StdCall);
113     setLibcallCallingConv(RTLIB::UDIV_I64, CallingConv::X86_StdCall);
114     setLibcallCallingConv(RTLIB::SREM_I64, CallingConv::X86_StdCall);
115     setLibcallCallingConv(RTLIB::UREM_I64, CallingConv::X86_StdCall);
116     setLibcallCallingConv(RTLIB::MUL_I64, CallingConv::X86_StdCall);
117   }
118
119   if (Subtarget->isTargetDarwin()) {
120     // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
121     setUseUnderscoreSetJmp(false);
122     setUseUnderscoreLongJmp(false);
123   } else if (Subtarget->isTargetWindowsGNU()) {
124     // MS runtime is weird: it exports _setjmp, but longjmp!
125     setUseUnderscoreSetJmp(true);
126     setUseUnderscoreLongJmp(false);
127   } else {
128     setUseUnderscoreSetJmp(true);
129     setUseUnderscoreLongJmp(true);
130   }
131
132   // Set up the register classes.
133   addRegisterClass(MVT::i8, &X86::GR8RegClass);
134   addRegisterClass(MVT::i16, &X86::GR16RegClass);
135   addRegisterClass(MVT::i32, &X86::GR32RegClass);
136   if (Subtarget->is64Bit())
137     addRegisterClass(MVT::i64, &X86::GR64RegClass);
138
139   for (MVT VT : MVT::integer_valuetypes())
140     setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
141
142   // We don't accept any truncstore of integer registers.
143   setTruncStoreAction(MVT::i64, MVT::i32, Expand);
144   setTruncStoreAction(MVT::i64, MVT::i16, Expand);
145   setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
146   setTruncStoreAction(MVT::i32, MVT::i16, Expand);
147   setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
148   setTruncStoreAction(MVT::i16, MVT::i8,  Expand);
149
150   setTruncStoreAction(MVT::f64, MVT::f32, Expand);
151
152   // SETOEQ and SETUNE require checking two conditions.
153   setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
154   setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
155   setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
156   setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
157   setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
158   setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
159
160   // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
161   // operation.
162   setOperationAction(ISD::UINT_TO_FP       , MVT::i1   , Promote);
163   setOperationAction(ISD::UINT_TO_FP       , MVT::i8   , Promote);
164   setOperationAction(ISD::UINT_TO_FP       , MVT::i16  , Promote);
165
166   if (Subtarget->is64Bit()) {
167     if (!Subtarget->useSoftFloat() && Subtarget->hasAVX512())
168       // f32/f64 are legal, f80 is custom.
169       setOperationAction(ISD::UINT_TO_FP   , MVT::i32  , Custom);
170     else
171       setOperationAction(ISD::UINT_TO_FP   , MVT::i32  , Promote);
172     setOperationAction(ISD::UINT_TO_FP     , MVT::i64  , Custom);
173   } else if (!Subtarget->useSoftFloat()) {
174     // We have an algorithm for SSE2->double, and we turn this into a
175     // 64-bit FILD followed by conditional FADD for other targets.
176     setOperationAction(ISD::UINT_TO_FP     , MVT::i64  , Custom);
177     // We have an algorithm for SSE2, and we turn this into a 64-bit
178     // FILD or VCVTUSI2SS/SD for other targets.
179     setOperationAction(ISD::UINT_TO_FP     , MVT::i32  , Custom);
180   }
181
182   // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
183   // this operation.
184   setOperationAction(ISD::SINT_TO_FP       , MVT::i1   , Promote);
185   setOperationAction(ISD::SINT_TO_FP       , MVT::i8   , Promote);
186
187   if (!Subtarget->useSoftFloat()) {
188     // SSE has no i16 to fp conversion, only i32
189     if (X86ScalarSSEf32) {
190       setOperationAction(ISD::SINT_TO_FP     , MVT::i16  , Promote);
191       // f32 and f64 cases are Legal, f80 case is not
192       setOperationAction(ISD::SINT_TO_FP     , MVT::i32  , Custom);
193     } else {
194       setOperationAction(ISD::SINT_TO_FP     , MVT::i16  , Custom);
195       setOperationAction(ISD::SINT_TO_FP     , MVT::i32  , Custom);
196     }
197   } else {
198     setOperationAction(ISD::SINT_TO_FP     , MVT::i16  , Promote);
199     setOperationAction(ISD::SINT_TO_FP     , MVT::i32  , Promote);
200   }
201
202   // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
203   // this operation.
204   setOperationAction(ISD::FP_TO_SINT       , MVT::i1   , Promote);
205   setOperationAction(ISD::FP_TO_SINT       , MVT::i8   , Promote);
206
207   if (!Subtarget->useSoftFloat()) {
208     // In 32-bit mode these are custom lowered.  In 64-bit mode F32 and F64
209     // are Legal, f80 is custom lowered.
210     setOperationAction(ISD::FP_TO_SINT     , MVT::i64  , Custom);
211     setOperationAction(ISD::SINT_TO_FP     , MVT::i64  , Custom);
212
213     if (X86ScalarSSEf32) {
214       setOperationAction(ISD::FP_TO_SINT     , MVT::i16  , Promote);
215       // f32 and f64 cases are Legal, f80 case is not
216       setOperationAction(ISD::FP_TO_SINT     , MVT::i32  , Custom);
217     } else {
218       setOperationAction(ISD::FP_TO_SINT     , MVT::i16  , Custom);
219       setOperationAction(ISD::FP_TO_SINT     , MVT::i32  , Custom);
220     }
221   } else {
222     setOperationAction(ISD::FP_TO_SINT     , MVT::i16  , Promote);
223     setOperationAction(ISD::FP_TO_SINT     , MVT::i32  , Expand);
224     setOperationAction(ISD::FP_TO_SINT     , MVT::i64  , Expand);
225   }
226
227   // Handle FP_TO_UINT by promoting the destination to a larger signed
228   // conversion.
229   setOperationAction(ISD::FP_TO_UINT       , MVT::i1   , Promote);
230   setOperationAction(ISD::FP_TO_UINT       , MVT::i8   , Promote);
231   setOperationAction(ISD::FP_TO_UINT       , MVT::i16  , Promote);
232
233   if (Subtarget->is64Bit()) {
234     if (!Subtarget->useSoftFloat() && Subtarget->hasAVX512()) {
235       // FP_TO_UINT-i32/i64 is legal for f32/f64, but custom for f80.
236       setOperationAction(ISD::FP_TO_UINT   , MVT::i32  , Custom);
237       setOperationAction(ISD::FP_TO_UINT   , MVT::i64  , Custom);
238     } else {
239       setOperationAction(ISD::FP_TO_UINT   , MVT::i32  , Promote);
240       setOperationAction(ISD::FP_TO_UINT   , MVT::i64  , Expand);
241     }
242   } else if (!Subtarget->useSoftFloat()) {
243     // Since AVX is a superset of SSE3, only check for SSE here.
244     if (Subtarget->hasSSE1() && !Subtarget->hasSSE3())
245       // Expand FP_TO_UINT into a select.
246       // FIXME: We would like to use a Custom expander here eventually to do
247       // the optimal thing for SSE vs. the default expansion in the legalizer.
248       setOperationAction(ISD::FP_TO_UINT   , MVT::i32  , Expand);
249     else
250       // With AVX512 we can use vcvts[ds]2usi for f32/f64->i32, f80 is custom.
251       // With SSE3 we can use fisttpll to convert to a signed i64; without
252       // SSE, we're stuck with a fistpll.
253       setOperationAction(ISD::FP_TO_UINT   , MVT::i32  , Custom);
254
255     setOperationAction(ISD::FP_TO_UINT     , MVT::i64  , Custom);
256   }
257
258   // TODO: when we have SSE, these could be more efficient, by using movd/movq.
259   if (!X86ScalarSSEf64) {
260     setOperationAction(ISD::BITCAST        , MVT::f32  , Expand);
261     setOperationAction(ISD::BITCAST        , MVT::i32  , Expand);
262     if (Subtarget->is64Bit()) {
263       setOperationAction(ISD::BITCAST      , MVT::f64  , Expand);
264       // Without SSE, i64->f64 goes through memory.
265       setOperationAction(ISD::BITCAST      , MVT::i64  , Expand);
266     }
267   }
268
269   // Scalar integer divide and remainder are lowered to use operations that
270   // produce two results, to match the available instructions. This exposes
271   // the two-result form to trivial CSE, which is able to combine x/y and x%y
272   // into a single instruction.
273   //
274   // Scalar integer multiply-high is also lowered to use two-result
275   // operations, to match the available instructions. However, plain multiply
276   // (low) operations are left as Legal, as there are single-result
277   // instructions for this in x86. Using the two-result multiply instructions
278   // when both high and low results are needed must be arranged by dagcombine.
279   for (auto VT : { MVT::i8, MVT::i16, MVT::i32, MVT::i64 }) {
280     setOperationAction(ISD::MULHS, VT, Expand);
281     setOperationAction(ISD::MULHU, VT, Expand);
282     setOperationAction(ISD::SDIV, VT, Expand);
283     setOperationAction(ISD::UDIV, VT, Expand);
284     setOperationAction(ISD::SREM, VT, Expand);
285     setOperationAction(ISD::UREM, VT, Expand);
286
287     // Add/Sub overflow ops with MVT::Glues are lowered to EFLAGS dependences.
288     setOperationAction(ISD::ADDC, VT, Custom);
289     setOperationAction(ISD::ADDE, VT, Custom);
290     setOperationAction(ISD::SUBC, VT, Custom);
291     setOperationAction(ISD::SUBE, VT, Custom);
292   }
293
294   setOperationAction(ISD::BR_JT            , MVT::Other, Expand);
295   setOperationAction(ISD::BRCOND           , MVT::Other, Custom);
296   setOperationAction(ISD::BR_CC            , MVT::f32,   Expand);
297   setOperationAction(ISD::BR_CC            , MVT::f64,   Expand);
298   setOperationAction(ISD::BR_CC            , MVT::f80,   Expand);
299   setOperationAction(ISD::BR_CC            , MVT::f128,  Expand);
300   setOperationAction(ISD::BR_CC            , MVT::i8,    Expand);
301   setOperationAction(ISD::BR_CC            , MVT::i16,   Expand);
302   setOperationAction(ISD::BR_CC            , MVT::i32,   Expand);
303   setOperationAction(ISD::BR_CC            , MVT::i64,   Expand);
304   setOperationAction(ISD::SELECT_CC        , MVT::f32,   Expand);
305   setOperationAction(ISD::SELECT_CC        , MVT::f64,   Expand);
306   setOperationAction(ISD::SELECT_CC        , MVT::f80,   Expand);
307   setOperationAction(ISD::SELECT_CC        , MVT::f128,  Expand);
308   setOperationAction(ISD::SELECT_CC        , MVT::i8,    Expand);
309   setOperationAction(ISD::SELECT_CC        , MVT::i16,   Expand);
310   setOperationAction(ISD::SELECT_CC        , MVT::i32,   Expand);
311   setOperationAction(ISD::SELECT_CC        , MVT::i64,   Expand);
312   if (Subtarget->is64Bit())
313     setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
314   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16  , Legal);
315   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8   , Legal);
316   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1   , Expand);
317   setOperationAction(ISD::FP_ROUND_INREG   , MVT::f32  , Expand);
318
319   if (Subtarget->is32Bit() && Subtarget->isTargetKnownWindowsMSVC()) {
320     // On 32 bit MSVC, `fmodf(f32)` is not defined - only `fmod(f64)`
321     // is. We should promote the value to 64-bits to solve this.
322     // This is what the CRT headers do - `fmodf` is an inline header
323     // function casting to f64 and calling `fmod`.
324     setOperationAction(ISD::FREM           , MVT::f32  , Promote);
325   } else {
326     setOperationAction(ISD::FREM           , MVT::f32  , Expand);
327   }
328
329   setOperationAction(ISD::FREM             , MVT::f64  , Expand);
330   setOperationAction(ISD::FREM             , MVT::f80  , Expand);
331   setOperationAction(ISD::FLT_ROUNDS_      , MVT::i32  , Custom);
332
333   // Promote the i8 variants and force them on up to i32 which has a shorter
334   // encoding.
335   setOperationAction(ISD::CTTZ             , MVT::i8   , Promote);
336   AddPromotedToType (ISD::CTTZ             , MVT::i8   , MVT::i32);
337   setOperationAction(ISD::CTTZ_ZERO_UNDEF  , MVT::i8   , Promote);
338   AddPromotedToType (ISD::CTTZ_ZERO_UNDEF  , MVT::i8   , MVT::i32);
339   if (Subtarget->hasBMI()) {
340     setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i16  , Expand);
341     setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i32  , Expand);
342     if (Subtarget->is64Bit())
343       setOperationAction(ISD::CTTZ_ZERO_UNDEF, MVT::i64, Expand);
344   } else {
345     setOperationAction(ISD::CTTZ           , MVT::i16  , Custom);
346     setOperationAction(ISD::CTTZ           , MVT::i32  , Custom);
347     if (Subtarget->is64Bit())
348       setOperationAction(ISD::CTTZ         , MVT::i64  , Custom);
349   }
350
351   if (Subtarget->hasLZCNT()) {
352     // When promoting the i8 variants, force them to i32 for a shorter
353     // encoding.
354     setOperationAction(ISD::CTLZ           , MVT::i8   , Promote);
355     AddPromotedToType (ISD::CTLZ           , MVT::i8   , MVT::i32);
356     setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8   , Promote);
357     AddPromotedToType (ISD::CTLZ_ZERO_UNDEF, MVT::i8   , MVT::i32);
358     setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16  , Expand);
359     setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32  , Expand);
360     if (Subtarget->is64Bit())
361       setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Expand);
362   } else {
363     setOperationAction(ISD::CTLZ           , MVT::i8   , Custom);
364     setOperationAction(ISD::CTLZ           , MVT::i16  , Custom);
365     setOperationAction(ISD::CTLZ           , MVT::i32  , Custom);
366     setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i8   , Custom);
367     setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i16  , Custom);
368     setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32  , Custom);
369     if (Subtarget->is64Bit()) {
370       setOperationAction(ISD::CTLZ         , MVT::i64  , Custom);
371       setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i64, Custom);
372     }
373   }
374
375   // Special handling for half-precision floating point conversions.
376   // If we don't have F16C support, then lower half float conversions
377   // into library calls.
378   if (Subtarget->useSoftFloat() || !Subtarget->hasF16C()) {
379     setOperationAction(ISD::FP16_TO_FP, MVT::f32, Expand);
380     setOperationAction(ISD::FP_TO_FP16, MVT::f32, Expand);
381   }
382
383   // There's never any support for operations beyond MVT::f32.
384   setOperationAction(ISD::FP16_TO_FP, MVT::f64, Expand);
385   setOperationAction(ISD::FP16_TO_FP, MVT::f80, Expand);
386   setOperationAction(ISD::FP_TO_FP16, MVT::f64, Expand);
387   setOperationAction(ISD::FP_TO_FP16, MVT::f80, Expand);
388
389   setLoadExtAction(ISD::EXTLOAD, MVT::f32, MVT::f16, Expand);
390   setLoadExtAction(ISD::EXTLOAD, MVT::f64, MVT::f16, Expand);
391   setLoadExtAction(ISD::EXTLOAD, MVT::f80, MVT::f16, Expand);
392   setTruncStoreAction(MVT::f32, MVT::f16, Expand);
393   setTruncStoreAction(MVT::f64, MVT::f16, Expand);
394   setTruncStoreAction(MVT::f80, MVT::f16, Expand);
395
396   if (Subtarget->hasPOPCNT()) {
397     setOperationAction(ISD::CTPOP          , MVT::i8   , Promote);
398   } else {
399     setOperationAction(ISD::CTPOP          , MVT::i8   , Expand);
400     setOperationAction(ISD::CTPOP          , MVT::i16  , Expand);
401     setOperationAction(ISD::CTPOP          , MVT::i32  , Expand);
402     if (Subtarget->is64Bit())
403       setOperationAction(ISD::CTPOP        , MVT::i64  , Expand);
404   }
405
406   setOperationAction(ISD::READCYCLECOUNTER , MVT::i64  , Custom);
407
408   if (!Subtarget->hasMOVBE())
409     setOperationAction(ISD::BSWAP          , MVT::i16  , Expand);
410
411   // These should be promoted to a larger select which is supported.
412   setOperationAction(ISD::SELECT          , MVT::i1   , Promote);
413   // X86 wants to expand cmov itself.
414   setOperationAction(ISD::SELECT          , MVT::i8   , Custom);
415   setOperationAction(ISD::SELECT          , MVT::i16  , Custom);
416   setOperationAction(ISD::SELECT          , MVT::i32  , Custom);
417   setOperationAction(ISD::SELECT          , MVT::f32  , Custom);
418   setOperationAction(ISD::SELECT          , MVT::f64  , Custom);
419   setOperationAction(ISD::SELECT          , MVT::f80  , Custom);
420   setOperationAction(ISD::SELECT          , MVT::f128 , Custom);
421   setOperationAction(ISD::SETCC           , MVT::i8   , Custom);
422   setOperationAction(ISD::SETCC           , MVT::i16  , Custom);
423   setOperationAction(ISD::SETCC           , MVT::i32  , Custom);
424   setOperationAction(ISD::SETCC           , MVT::f32  , Custom);
425   setOperationAction(ISD::SETCC           , MVT::f64  , Custom);
426   setOperationAction(ISD::SETCC           , MVT::f80  , Custom);
427   setOperationAction(ISD::SETCC           , MVT::f128 , Custom);
428   setOperationAction(ISD::SETCCE          , MVT::i8   , Custom);
429   setOperationAction(ISD::SETCCE          , MVT::i16  , Custom);
430   setOperationAction(ISD::SETCCE          , MVT::i32  , Custom);
431   if (Subtarget->is64Bit()) {
432     setOperationAction(ISD::SELECT        , MVT::i64  , Custom);
433     setOperationAction(ISD::SETCC         , MVT::i64  , Custom);
434     setOperationAction(ISD::SETCCE        , MVT::i64  , Custom);
435   }
436   setOperationAction(ISD::EH_RETURN       , MVT::Other, Custom);
437   // NOTE: EH_SJLJ_SETJMP/_LONGJMP supported here is NOT intended to support
438   // SjLj exception handling but a light-weight setjmp/longjmp replacement to
439   // support continuation, user-level threading, and etc.. As a result, no
440   // other SjLj exception interfaces are implemented and please don't build
441   // your own exception handling based on them.
442   // LLVM/Clang supports zero-cost DWARF exception handling.
443   setOperationAction(ISD::EH_SJLJ_SETJMP, MVT::i32, Custom);
444   setOperationAction(ISD::EH_SJLJ_LONGJMP, MVT::Other, Custom);
445
446   // Darwin ABI issue.
447   setOperationAction(ISD::ConstantPool    , MVT::i32  , Custom);
448   setOperationAction(ISD::JumpTable       , MVT::i32  , Custom);
449   setOperationAction(ISD::GlobalAddress   , MVT::i32  , Custom);
450   setOperationAction(ISD::GlobalTLSAddress, MVT::i32  , Custom);
451   if (Subtarget->is64Bit())
452     setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
453   setOperationAction(ISD::ExternalSymbol  , MVT::i32  , Custom);
454   setOperationAction(ISD::BlockAddress    , MVT::i32  , Custom);
455   if (Subtarget->is64Bit()) {
456     setOperationAction(ISD::ConstantPool  , MVT::i64  , Custom);
457     setOperationAction(ISD::JumpTable     , MVT::i64  , Custom);
458     setOperationAction(ISD::GlobalAddress , MVT::i64  , Custom);
459     setOperationAction(ISD::ExternalSymbol, MVT::i64  , Custom);
460     setOperationAction(ISD::BlockAddress  , MVT::i64  , Custom);
461   }
462   // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
463   setOperationAction(ISD::SHL_PARTS       , MVT::i32  , Custom);
464   setOperationAction(ISD::SRA_PARTS       , MVT::i32  , Custom);
465   setOperationAction(ISD::SRL_PARTS       , MVT::i32  , Custom);
466   if (Subtarget->is64Bit()) {
467     setOperationAction(ISD::SHL_PARTS     , MVT::i64  , Custom);
468     setOperationAction(ISD::SRA_PARTS     , MVT::i64  , Custom);
469     setOperationAction(ISD::SRL_PARTS     , MVT::i64  , Custom);
470   }
471
472   if (Subtarget->hasSSE1())
473     setOperationAction(ISD::PREFETCH      , MVT::Other, Legal);
474
475   setOperationAction(ISD::ATOMIC_FENCE  , MVT::Other, Custom);
476
477   // Expand certain atomics
478   for (auto VT : { MVT::i8, MVT::i16, MVT::i32, MVT::i64 }) {
479     setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, VT, Custom);
480     setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
481     setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
482   }
483
484   if (Subtarget->hasCmpxchg16b()) {
485     setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i128, Custom);
486   }
487
488   // FIXME - use subtarget debug flags
489   if (!Subtarget->isTargetDarwin() && !Subtarget->isTargetELF() &&
490       !Subtarget->isTargetCygMing() && !Subtarget->isTargetWin64()) {
491     setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
492   }
493
494   setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
495   setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
496
497   setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
498   setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
499
500   setOperationAction(ISD::TRAP, MVT::Other, Legal);
501   setOperationAction(ISD::DEBUGTRAP, MVT::Other, Legal);
502
503   // VASTART needs to be custom lowered to use the VarArgsFrameIndex
504   setOperationAction(ISD::VASTART           , MVT::Other, Custom);
505   setOperationAction(ISD::VAEND             , MVT::Other, Expand);
506   if (Subtarget->is64Bit()) {
507     setOperationAction(ISD::VAARG           , MVT::Other, Custom);
508     setOperationAction(ISD::VACOPY          , MVT::Other, Custom);
509   } else {
510     // TargetInfo::CharPtrBuiltinVaList
511     setOperationAction(ISD::VAARG           , MVT::Other, Expand);
512     setOperationAction(ISD::VACOPY          , MVT::Other, Expand);
513   }
514
515   setOperationAction(ISD::STACKSAVE,          MVT::Other, Expand);
516   setOperationAction(ISD::STACKRESTORE,       MVT::Other, Expand);
517
518   setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom);
519
520   // GC_TRANSITION_START and GC_TRANSITION_END need custom lowering.
521   setOperationAction(ISD::GC_TRANSITION_START, MVT::Other, Custom);
522   setOperationAction(ISD::GC_TRANSITION_END, MVT::Other, Custom);
523
524   if (!Subtarget->useSoftFloat() && X86ScalarSSEf64) {
525     // f32 and f64 use SSE.
526     // Set up the FP register classes.
527     addRegisterClass(MVT::f32, &X86::FR32RegClass);
528     addRegisterClass(MVT::f64, &X86::FR64RegClass);
529
530     // Use ANDPD to simulate FABS.
531     setOperationAction(ISD::FABS , MVT::f64, Custom);
532     setOperationAction(ISD::FABS , MVT::f32, Custom);
533
534     // Use XORP to simulate FNEG.
535     setOperationAction(ISD::FNEG , MVT::f64, Custom);
536     setOperationAction(ISD::FNEG , MVT::f32, Custom);
537
538     // Use ANDPD and ORPD to simulate FCOPYSIGN.
539     setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
540     setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
541
542     // Lower this to FGETSIGNx86 plus an AND.
543     setOperationAction(ISD::FGETSIGN, MVT::i64, Custom);
544     setOperationAction(ISD::FGETSIGN, MVT::i32, Custom);
545
546     // We don't support sin/cos/fmod
547     setOperationAction(ISD::FSIN   , MVT::f64, Expand);
548     setOperationAction(ISD::FCOS   , MVT::f64, Expand);
549     setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
550     setOperationAction(ISD::FSIN   , MVT::f32, Expand);
551     setOperationAction(ISD::FCOS   , MVT::f32, Expand);
552     setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
553
554     // Expand FP immediates into loads from the stack, except for the special
555     // cases we handle.
556     addLegalFPImmediate(APFloat(+0.0)); // xorpd
557     addLegalFPImmediate(APFloat(+0.0f)); // xorps
558   } else if (!Subtarget->useSoftFloat() && X86ScalarSSEf32) {
559     // Use SSE for f32, x87 for f64.
560     // Set up the FP register classes.
561     addRegisterClass(MVT::f32, &X86::FR32RegClass);
562     addRegisterClass(MVT::f64, &X86::RFP64RegClass);
563
564     // Use ANDPS to simulate FABS.
565     setOperationAction(ISD::FABS , MVT::f32, Custom);
566
567     // Use XORP to simulate FNEG.
568     setOperationAction(ISD::FNEG , MVT::f32, Custom);
569
570     setOperationAction(ISD::UNDEF,     MVT::f64, Expand);
571
572     // Use ANDPS and ORPS to simulate FCOPYSIGN.
573     setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
574     setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
575
576     // We don't support sin/cos/fmod
577     setOperationAction(ISD::FSIN   , MVT::f32, Expand);
578     setOperationAction(ISD::FCOS   , MVT::f32, Expand);
579     setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
580
581     // Special cases we handle for FP constants.
582     addLegalFPImmediate(APFloat(+0.0f)); // xorps
583     addLegalFPImmediate(APFloat(+0.0)); // FLD0
584     addLegalFPImmediate(APFloat(+1.0)); // FLD1
585     addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
586     addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
587
588     if (!TM.Options.UnsafeFPMath) {
589       setOperationAction(ISD::FSIN   , MVT::f64, Expand);
590       setOperationAction(ISD::FCOS   , MVT::f64, Expand);
591       setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
592     }
593   } else if (!Subtarget->useSoftFloat()) {
594     // f32 and f64 in x87.
595     // Set up the FP register classes.
596     addRegisterClass(MVT::f64, &X86::RFP64RegClass);
597     addRegisterClass(MVT::f32, &X86::RFP32RegClass);
598
599     setOperationAction(ISD::UNDEF,     MVT::f64, Expand);
600     setOperationAction(ISD::UNDEF,     MVT::f32, Expand);
601     setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
602     setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
603
604     if (!TM.Options.UnsafeFPMath) {
605       setOperationAction(ISD::FSIN   , MVT::f64, Expand);
606       setOperationAction(ISD::FSIN   , MVT::f32, Expand);
607       setOperationAction(ISD::FCOS   , MVT::f64, Expand);
608       setOperationAction(ISD::FCOS   , MVT::f32, Expand);
609       setOperationAction(ISD::FSINCOS, MVT::f64, Expand);
610       setOperationAction(ISD::FSINCOS, MVT::f32, Expand);
611     }
612     addLegalFPImmediate(APFloat(+0.0)); // FLD0
613     addLegalFPImmediate(APFloat(+1.0)); // FLD1
614     addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
615     addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
616     addLegalFPImmediate(APFloat(+0.0f)); // FLD0
617     addLegalFPImmediate(APFloat(+1.0f)); // FLD1
618     addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
619     addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
620   }
621
622   // We don't support FMA.
623   setOperationAction(ISD::FMA, MVT::f64, Expand);
624   setOperationAction(ISD::FMA, MVT::f32, Expand);
625
626   // Long double always uses X87, except f128 in MMX.
627   if (!Subtarget->useSoftFloat()) {
628     if (Subtarget->is64Bit() && Subtarget->hasMMX()) {
629       addRegisterClass(MVT::f128, &X86::FR128RegClass);
630       ValueTypeActions.setTypeAction(MVT::f128, TypeSoftenFloat);
631       setOperationAction(ISD::FABS , MVT::f128, Custom);
632       setOperationAction(ISD::FNEG , MVT::f128, Custom);
633       setOperationAction(ISD::FCOPYSIGN, MVT::f128, Custom);
634     }
635
636     addRegisterClass(MVT::f80, &X86::RFP80RegClass);
637     setOperationAction(ISD::UNDEF,     MVT::f80, Expand);
638     setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
639     {
640       APFloat TmpFlt = APFloat::getZero(APFloat::x87DoubleExtended);
641       addLegalFPImmediate(TmpFlt);  // FLD0
642       TmpFlt.changeSign();
643       addLegalFPImmediate(TmpFlt);  // FLD0/FCHS
644
645       bool ignored;
646       APFloat TmpFlt2(+1.0);
647       TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
648                       &ignored);
649       addLegalFPImmediate(TmpFlt2);  // FLD1
650       TmpFlt2.changeSign();
651       addLegalFPImmediate(TmpFlt2);  // FLD1/FCHS
652     }
653
654     if (!TM.Options.UnsafeFPMath) {
655       setOperationAction(ISD::FSIN   , MVT::f80, Expand);
656       setOperationAction(ISD::FCOS   , MVT::f80, Expand);
657       setOperationAction(ISD::FSINCOS, MVT::f80, Expand);
658     }
659
660     setOperationAction(ISD::FFLOOR, MVT::f80, Expand);
661     setOperationAction(ISD::FCEIL,  MVT::f80, Expand);
662     setOperationAction(ISD::FTRUNC, MVT::f80, Expand);
663     setOperationAction(ISD::FRINT,  MVT::f80, Expand);
664     setOperationAction(ISD::FNEARBYINT, MVT::f80, Expand);
665     setOperationAction(ISD::FMA, MVT::f80, Expand);
666   }
667
668   // Always use a library call for pow.
669   setOperationAction(ISD::FPOW             , MVT::f32  , Expand);
670   setOperationAction(ISD::FPOW             , MVT::f64  , Expand);
671   setOperationAction(ISD::FPOW             , MVT::f80  , Expand);
672
673   setOperationAction(ISD::FLOG, MVT::f80, Expand);
674   setOperationAction(ISD::FLOG2, MVT::f80, Expand);
675   setOperationAction(ISD::FLOG10, MVT::f80, Expand);
676   setOperationAction(ISD::FEXP, MVT::f80, Expand);
677   setOperationAction(ISD::FEXP2, MVT::f80, Expand);
678   setOperationAction(ISD::FMINNUM, MVT::f80, Expand);
679   setOperationAction(ISD::FMAXNUM, MVT::f80, Expand);
680
681   // First set operation action for all vector types to either promote
682   // (for widening) or expand (for scalarization). Then we will selectively
683   // turn on ones that can be effectively codegen'd.
684   for (MVT VT : MVT::vector_valuetypes()) {
685     setOperationAction(ISD::ADD , VT, Expand);
686     setOperationAction(ISD::SUB , VT, Expand);
687     setOperationAction(ISD::FADD, VT, Expand);
688     setOperationAction(ISD::FNEG, VT, Expand);
689     setOperationAction(ISD::FSUB, VT, Expand);
690     setOperationAction(ISD::MUL , VT, Expand);
691     setOperationAction(ISD::FMUL, VT, Expand);
692     setOperationAction(ISD::SDIV, VT, Expand);
693     setOperationAction(ISD::UDIV, VT, Expand);
694     setOperationAction(ISD::FDIV, VT, Expand);
695     setOperationAction(ISD::SREM, VT, Expand);
696     setOperationAction(ISD::UREM, VT, Expand);
697     setOperationAction(ISD::LOAD, VT, Expand);
698     setOperationAction(ISD::VECTOR_SHUFFLE, VT, Expand);
699     setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT,Expand);
700     setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Expand);
701     setOperationAction(ISD::EXTRACT_SUBVECTOR, VT,Expand);
702     setOperationAction(ISD::INSERT_SUBVECTOR, VT,Expand);
703     setOperationAction(ISD::FABS, VT, Expand);
704     setOperationAction(ISD::FSIN, VT, Expand);
705     setOperationAction(ISD::FSINCOS, VT, Expand);
706     setOperationAction(ISD::FCOS, VT, Expand);
707     setOperationAction(ISD::FSINCOS, VT, Expand);
708     setOperationAction(ISD::FREM, VT, Expand);
709     setOperationAction(ISD::FMA,  VT, Expand);
710     setOperationAction(ISD::FPOWI, VT, Expand);
711     setOperationAction(ISD::FSQRT, VT, Expand);
712     setOperationAction(ISD::FCOPYSIGN, VT, Expand);
713     setOperationAction(ISD::FFLOOR, VT, Expand);
714     setOperationAction(ISD::FCEIL, VT, Expand);
715     setOperationAction(ISD::FTRUNC, VT, Expand);
716     setOperationAction(ISD::FRINT, VT, Expand);
717     setOperationAction(ISD::FNEARBYINT, VT, Expand);
718     setOperationAction(ISD::SMUL_LOHI, VT, Expand);
719     setOperationAction(ISD::MULHS, VT, Expand);
720     setOperationAction(ISD::UMUL_LOHI, VT, Expand);
721     setOperationAction(ISD::MULHU, VT, Expand);
722     setOperationAction(ISD::SDIVREM, VT, Expand);
723     setOperationAction(ISD::UDIVREM, VT, Expand);
724     setOperationAction(ISD::FPOW, VT, Expand);
725     setOperationAction(ISD::CTPOP, VT, Expand);
726     setOperationAction(ISD::CTTZ, VT, Expand);
727     setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
728     setOperationAction(ISD::CTLZ, VT, Expand);
729     setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
730     setOperationAction(ISD::SHL, VT, Expand);
731     setOperationAction(ISD::SRA, VT, Expand);
732     setOperationAction(ISD::SRL, VT, Expand);
733     setOperationAction(ISD::ROTL, VT, Expand);
734     setOperationAction(ISD::ROTR, VT, Expand);
735     setOperationAction(ISD::BSWAP, VT, Expand);
736     setOperationAction(ISD::SETCC, VT, Expand);
737     setOperationAction(ISD::FLOG, VT, Expand);
738     setOperationAction(ISD::FLOG2, VT, Expand);
739     setOperationAction(ISD::FLOG10, VT, Expand);
740     setOperationAction(ISD::FEXP, VT, Expand);
741     setOperationAction(ISD::FEXP2, VT, Expand);
742     setOperationAction(ISD::FP_TO_UINT, VT, Expand);
743     setOperationAction(ISD::FP_TO_SINT, VT, Expand);
744     setOperationAction(ISD::UINT_TO_FP, VT, Expand);
745     setOperationAction(ISD::SINT_TO_FP, VT, Expand);
746     setOperationAction(ISD::SIGN_EXTEND_INREG, VT,Expand);
747     setOperationAction(ISD::TRUNCATE, VT, Expand);
748     setOperationAction(ISD::SIGN_EXTEND, VT, Expand);
749     setOperationAction(ISD::ZERO_EXTEND, VT, Expand);
750     setOperationAction(ISD::ANY_EXTEND, VT, Expand);
751     setOperationAction(ISD::VSELECT, VT, Expand);
752     setOperationAction(ISD::SELECT_CC, VT, Expand);
753     for (MVT InnerVT : MVT::vector_valuetypes()) {
754       setTruncStoreAction(InnerVT, VT, Expand);
755
756       setLoadExtAction(ISD::SEXTLOAD, InnerVT, VT, Expand);
757       setLoadExtAction(ISD::ZEXTLOAD, InnerVT, VT, Expand);
758
759       // N.b. ISD::EXTLOAD legality is basically ignored except for i1-like
760       // types, we have to deal with them whether we ask for Expansion or not.
761       // Setting Expand causes its own optimisation problems though, so leave
762       // them legal.
763       if (VT.getVectorElementType() == MVT::i1)
764         setLoadExtAction(ISD::EXTLOAD, InnerVT, VT, Expand);
765
766       // EXTLOAD for MVT::f16 vectors is not legal because f16 vectors are
767       // split/scalarized right now.
768       if (VT.getVectorElementType() == MVT::f16)
769         setLoadExtAction(ISD::EXTLOAD, InnerVT, VT, Expand);
770     }
771   }
772
773   // FIXME: In order to prevent SSE instructions being expanded to MMX ones
774   // with -msoft-float, disable use of MMX as well.
775   if (!Subtarget->useSoftFloat() && Subtarget->hasMMX()) {
776     addRegisterClass(MVT::x86mmx, &X86::VR64RegClass);
777     // No operations on x86mmx supported, everything uses intrinsics.
778   }
779
780   // MMX-sized vectors (other than x86mmx) are expected to be expanded
781   // into smaller operations.
782   for (MVT MMXTy : {MVT::v8i8, MVT::v4i16, MVT::v2i32, MVT::v1i64}) {
783     setOperationAction(ISD::MULHS,              MMXTy,      Expand);
784     setOperationAction(ISD::AND,                MMXTy,      Expand);
785     setOperationAction(ISD::OR,                 MMXTy,      Expand);
786     setOperationAction(ISD::XOR,                MMXTy,      Expand);
787     setOperationAction(ISD::SCALAR_TO_VECTOR,   MMXTy,      Expand);
788     setOperationAction(ISD::SELECT,             MMXTy,      Expand);
789     setOperationAction(ISD::BITCAST,            MMXTy,      Expand);
790   }
791   setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v1i64, Expand);
792
793   if (!Subtarget->useSoftFloat() && Subtarget->hasSSE1()) {
794     addRegisterClass(MVT::v4f32, &X86::VR128RegClass);
795
796     setOperationAction(ISD::FADD,               MVT::v4f32, Legal);
797     setOperationAction(ISD::FSUB,               MVT::v4f32, Legal);
798     setOperationAction(ISD::FMUL,               MVT::v4f32, Legal);
799     setOperationAction(ISD::FDIV,               MVT::v4f32, Legal);
800     setOperationAction(ISD::FSQRT,              MVT::v4f32, Legal);
801     setOperationAction(ISD::FNEG,               MVT::v4f32, Custom);
802     setOperationAction(ISD::FABS,               MVT::v4f32, Custom);
803     setOperationAction(ISD::LOAD,               MVT::v4f32, Legal);
804     setOperationAction(ISD::BUILD_VECTOR,       MVT::v4f32, Custom);
805     setOperationAction(ISD::VECTOR_SHUFFLE,     MVT::v4f32, Custom);
806     setOperationAction(ISD::VSELECT,            MVT::v4f32, Custom);
807     setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
808     setOperationAction(ISD::SELECT,             MVT::v4f32, Custom);
809     setOperationAction(ISD::UINT_TO_FP,         MVT::v4i32, Custom);
810   }
811
812   if (!Subtarget->useSoftFloat() && Subtarget->hasSSE2()) {
813     addRegisterClass(MVT::v2f64, &X86::VR128RegClass);
814
815     // FIXME: Unfortunately, -soft-float and -no-implicit-float mean XMM
816     // registers cannot be used even for integer operations.
817     addRegisterClass(MVT::v16i8, &X86::VR128RegClass);
818     addRegisterClass(MVT::v8i16, &X86::VR128RegClass);
819     addRegisterClass(MVT::v4i32, &X86::VR128RegClass);
820     addRegisterClass(MVT::v2i64, &X86::VR128RegClass);
821
822     setOperationAction(ISD::ADD,                MVT::v16i8, Legal);
823     setOperationAction(ISD::ADD,                MVT::v8i16, Legal);
824     setOperationAction(ISD::ADD,                MVT::v4i32, Legal);
825     setOperationAction(ISD::ADD,                MVT::v2i64, Legal);
826     setOperationAction(ISD::MUL,                MVT::v16i8, Custom);
827     setOperationAction(ISD::MUL,                MVT::v4i32, Custom);
828     setOperationAction(ISD::MUL,                MVT::v2i64, Custom);
829     setOperationAction(ISD::UMUL_LOHI,          MVT::v4i32, Custom);
830     setOperationAction(ISD::SMUL_LOHI,          MVT::v4i32, Custom);
831     setOperationAction(ISD::MULHU,              MVT::v8i16, Legal);
832     setOperationAction(ISD::MULHS,              MVT::v8i16, Legal);
833     setOperationAction(ISD::SUB,                MVT::v16i8, Legal);
834     setOperationAction(ISD::SUB,                MVT::v8i16, Legal);
835     setOperationAction(ISD::SUB,                MVT::v4i32, Legal);
836     setOperationAction(ISD::SUB,                MVT::v2i64, Legal);
837     setOperationAction(ISD::MUL,                MVT::v8i16, Legal);
838     setOperationAction(ISD::FADD,               MVT::v2f64, Legal);
839     setOperationAction(ISD::FSUB,               MVT::v2f64, Legal);
840     setOperationAction(ISD::FMUL,               MVT::v2f64, Legal);
841     setOperationAction(ISD::FDIV,               MVT::v2f64, Legal);
842     setOperationAction(ISD::FSQRT,              MVT::v2f64, Legal);
843     setOperationAction(ISD::FNEG,               MVT::v2f64, Custom);
844     setOperationAction(ISD::FABS,               MVT::v2f64, Custom);
845
846     setOperationAction(ISD::SMAX,               MVT::v8i16, Legal);
847     setOperationAction(ISD::UMAX,               MVT::v16i8, Legal);
848     setOperationAction(ISD::SMIN,               MVT::v8i16, Legal);
849     setOperationAction(ISD::UMIN,               MVT::v16i8, Legal);
850
851     setOperationAction(ISD::SETCC,              MVT::v2i64, Custom);
852     setOperationAction(ISD::SETCC,              MVT::v16i8, Custom);
853     setOperationAction(ISD::SETCC,              MVT::v8i16, Custom);
854     setOperationAction(ISD::SETCC,              MVT::v4i32, Custom);
855
856     setOperationAction(ISD::SCALAR_TO_VECTOR,   MVT::v16i8, Custom);
857     setOperationAction(ISD::SCALAR_TO_VECTOR,   MVT::v8i16, Custom);
858     setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v8i16, Custom);
859     setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v4i32, Custom);
860     setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v4f32, Custom);
861
862     setOperationAction(ISD::CTPOP,              MVT::v16i8, Custom);
863     setOperationAction(ISD::CTPOP,              MVT::v8i16, Custom);
864     setOperationAction(ISD::CTPOP,              MVT::v4i32, Custom);
865     setOperationAction(ISD::CTPOP,              MVT::v2i64, Custom);
866
867     setOperationAction(ISD::CTTZ,               MVT::v16i8, Custom);
868     setOperationAction(ISD::CTTZ,               MVT::v8i16, Custom);
869     setOperationAction(ISD::CTTZ,               MVT::v4i32, Custom);
870     // ISD::CTTZ v2i64 - scalarization is faster.
871     setOperationAction(ISD::CTTZ_ZERO_UNDEF,    MVT::v16i8, Custom);
872     setOperationAction(ISD::CTTZ_ZERO_UNDEF,    MVT::v8i16, Custom);
873     setOperationAction(ISD::CTTZ_ZERO_UNDEF,    MVT::v4i32, Custom);
874     // ISD::CTTZ_ZERO_UNDEF v2i64 - scalarization is faster.
875
876     // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
877     for (auto VT : { MVT::v16i8, MVT::v8i16, MVT::v4i32 }) {
878       setOperationAction(ISD::BUILD_VECTOR,       VT, Custom);
879       setOperationAction(ISD::VECTOR_SHUFFLE,     VT, Custom);
880       setOperationAction(ISD::VSELECT,            VT, Custom);
881       setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
882     }
883
884     // We support custom legalizing of sext and anyext loads for specific
885     // memory vector types which we can load as a scalar (or sequence of
886     // scalars) and extend in-register to a legal 128-bit vector type. For sext
887     // loads these must work with a single scalar load.
888     for (MVT VT : MVT::integer_vector_valuetypes()) {
889       setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v4i8, Custom);
890       setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v4i16, Custom);
891       setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v8i8, Custom);
892       setLoadExtAction(ISD::EXTLOAD, VT, MVT::v2i8, Custom);
893       setLoadExtAction(ISD::EXTLOAD, VT, MVT::v2i16, Custom);
894       setLoadExtAction(ISD::EXTLOAD, VT, MVT::v2i32, Custom);
895       setLoadExtAction(ISD::EXTLOAD, VT, MVT::v4i8, Custom);
896       setLoadExtAction(ISD::EXTLOAD, VT, MVT::v4i16, Custom);
897       setLoadExtAction(ISD::EXTLOAD, VT, MVT::v8i8, Custom);
898     }
899
900     setOperationAction(ISD::BUILD_VECTOR,       MVT::v2f64, Custom);
901     setOperationAction(ISD::BUILD_VECTOR,       MVT::v2i64, Custom);
902     setOperationAction(ISD::VECTOR_SHUFFLE,     MVT::v2f64, Custom);
903     setOperationAction(ISD::VECTOR_SHUFFLE,     MVT::v2i64, Custom);
904     setOperationAction(ISD::VSELECT,            MVT::v2f64, Custom);
905     setOperationAction(ISD::VSELECT,            MVT::v2i64, Custom);
906     setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v2f64, Custom);
907     setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
908
909     if (Subtarget->is64Bit()) {
910       setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v2i64, Custom);
911       setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
912     }
913
914     // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
915     for (auto VT : { MVT::v16i8, MVT::v8i16, MVT::v4i32 }) {
916       setOperationAction(ISD::AND,    VT, Promote);
917       AddPromotedToType (ISD::AND,    VT, MVT::v2i64);
918       setOperationAction(ISD::OR,     VT, Promote);
919       AddPromotedToType (ISD::OR,     VT, MVT::v2i64);
920       setOperationAction(ISD::XOR,    VT, Promote);
921       AddPromotedToType (ISD::XOR,    VT, MVT::v2i64);
922       setOperationAction(ISD::LOAD,   VT, Promote);
923       AddPromotedToType (ISD::LOAD,   VT, MVT::v2i64);
924       setOperationAction(ISD::SELECT, VT, Promote);
925       AddPromotedToType (ISD::SELECT, VT, MVT::v2i64);
926     }
927
928     // Custom lower v2i64 and v2f64 selects.
929     setOperationAction(ISD::LOAD,               MVT::v2f64, Legal);
930     setOperationAction(ISD::LOAD,               MVT::v2i64, Legal);
931     setOperationAction(ISD::SELECT,             MVT::v2f64, Custom);
932     setOperationAction(ISD::SELECT,             MVT::v2i64, Custom);
933
934     setOperationAction(ISD::FP_TO_SINT,         MVT::v4i32, Legal);
935     setOperationAction(ISD::SINT_TO_FP,         MVT::v4i32, Legal);
936
937     setOperationAction(ISD::SINT_TO_FP,         MVT::v2i32, Custom);
938
939     setOperationAction(ISD::UINT_TO_FP,         MVT::v4i8,  Custom);
940     setOperationAction(ISD::UINT_TO_FP,         MVT::v4i16, Custom);
941     // As there is no 64-bit GPR available, we need build a special custom
942     // sequence to convert from v2i32 to v2f32.
943     if (!Subtarget->is64Bit())
944       setOperationAction(ISD::UINT_TO_FP,       MVT::v2f32, Custom);
945
946     setOperationAction(ISD::FP_EXTEND,          MVT::v2f32, Custom);
947     setOperationAction(ISD::FP_ROUND,           MVT::v2f32, Custom);
948
949     for (MVT VT : MVT::fp_vector_valuetypes())
950       setLoadExtAction(ISD::EXTLOAD, VT, MVT::v2f32, Legal);
951
952     setOperationAction(ISD::BITCAST,            MVT::v2i32, Custom);
953     setOperationAction(ISD::BITCAST,            MVT::v4i16, Custom);
954     setOperationAction(ISD::BITCAST,            MVT::v8i8,  Custom);
955   }
956
957   if (!Subtarget->useSoftFloat() && Subtarget->hasSSE41()) {
958     for (MVT RoundedTy : {MVT::f32, MVT::f64, MVT::v4f32, MVT::v2f64}) {
959       setOperationAction(ISD::FFLOOR,           RoundedTy,  Legal);
960       setOperationAction(ISD::FCEIL,            RoundedTy,  Legal);
961       setOperationAction(ISD::FTRUNC,           RoundedTy,  Legal);
962       setOperationAction(ISD::FRINT,            RoundedTy,  Legal);
963       setOperationAction(ISD::FNEARBYINT,       RoundedTy,  Legal);
964     }
965
966     setOperationAction(ISD::SMAX,               MVT::v16i8, Legal);
967     setOperationAction(ISD::SMAX,               MVT::v4i32, Legal);
968     setOperationAction(ISD::UMAX,               MVT::v8i16, Legal);
969     setOperationAction(ISD::UMAX,               MVT::v4i32, Legal);
970     setOperationAction(ISD::SMIN,               MVT::v16i8, Legal);
971     setOperationAction(ISD::SMIN,               MVT::v4i32, Legal);
972     setOperationAction(ISD::UMIN,               MVT::v8i16, Legal);
973     setOperationAction(ISD::UMIN,               MVT::v4i32, Legal);
974
975     // FIXME: Do we need to handle scalar-to-vector here?
976     setOperationAction(ISD::MUL,                MVT::v4i32, Legal);
977
978     // We directly match byte blends in the backend as they match the VSELECT
979     // condition form.
980     setOperationAction(ISD::VSELECT,            MVT::v16i8, Legal);
981
982     // SSE41 brings specific instructions for doing vector sign extend even in
983     // cases where we don't have SRA.
984     for (MVT VT : MVT::integer_vector_valuetypes()) {
985       setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v2i8, Custom);
986       setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v2i16, Custom);
987       setLoadExtAction(ISD::SEXTLOAD, VT, MVT::v2i32, Custom);
988     }
989
990     // SSE41 also has vector sign/zero extending loads, PMOV[SZ]X
991     setLoadExtAction(ISD::SEXTLOAD, MVT::v8i16, MVT::v8i8,  Legal);
992     setLoadExtAction(ISD::SEXTLOAD, MVT::v4i32, MVT::v4i8,  Legal);
993     setLoadExtAction(ISD::SEXTLOAD, MVT::v2i64, MVT::v2i8,  Legal);
994     setLoadExtAction(ISD::SEXTLOAD, MVT::v4i32, MVT::v4i16, Legal);
995     setLoadExtAction(ISD::SEXTLOAD, MVT::v2i64, MVT::v2i16, Legal);
996     setLoadExtAction(ISD::SEXTLOAD, MVT::v2i64, MVT::v2i32, Legal);
997
998     setLoadExtAction(ISD::ZEXTLOAD, MVT::v8i16, MVT::v8i8,  Legal);
999     setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i32, MVT::v4i8,  Legal);
1000     setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i64, MVT::v2i8,  Legal);
1001     setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i32, MVT::v4i16, Legal);
1002     setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i64, MVT::v2i16, Legal);
1003     setLoadExtAction(ISD::ZEXTLOAD, MVT::v2i64, MVT::v2i32, Legal);
1004
1005     // i8 and i16 vectors are custom because the source register and source
1006     // source memory operand types are not the same width.  f32 vectors are
1007     // custom since the immediate controlling the insert encodes additional
1008     // information.
1009     setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v16i8, Custom);
1010     setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v8i16, Custom);
1011     setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v4i32, Custom);
1012     setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v4f32, Custom);
1013
1014     setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
1015     setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
1016     setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
1017     setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
1018
1019     // FIXME: these should be Legal, but that's only for the case where
1020     // the index is constant.  For now custom expand to deal with that.
1021     if (Subtarget->is64Bit()) {
1022       setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v2i64, Custom);
1023       setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
1024     }
1025   }
1026
1027   if (Subtarget->hasSSE2()) {
1028     setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, MVT::v2i64, Custom);
1029     setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, MVT::v4i32, Custom);
1030     setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, MVT::v8i16, Custom);
1031
1032     setOperationAction(ISD::SRL,               MVT::v8i16, Custom);
1033     setOperationAction(ISD::SRL,               MVT::v16i8, Custom);
1034
1035     setOperationAction(ISD::SHL,               MVT::v8i16, Custom);
1036     setOperationAction(ISD::SHL,               MVT::v16i8, Custom);
1037
1038     setOperationAction(ISD::SRA,               MVT::v8i16, Custom);
1039     setOperationAction(ISD::SRA,               MVT::v16i8, Custom);
1040
1041     // In the customized shift lowering, the legal cases in AVX2 will be
1042     // recognized.
1043     setOperationAction(ISD::SRL,               MVT::v2i64, Custom);
1044     setOperationAction(ISD::SRL,               MVT::v4i32, Custom);
1045
1046     setOperationAction(ISD::SHL,               MVT::v2i64, Custom);
1047     setOperationAction(ISD::SHL,               MVT::v4i32, Custom);
1048
1049     setOperationAction(ISD::SRA,               MVT::v2i64, Custom);
1050     setOperationAction(ISD::SRA,               MVT::v4i32, Custom);
1051   }
1052
1053   if (Subtarget->hasXOP()) {
1054     setOperationAction(ISD::ROTL,              MVT::v16i8, Custom);
1055     setOperationAction(ISD::ROTL,              MVT::v8i16, Custom);
1056     setOperationAction(ISD::ROTL,              MVT::v4i32, Custom);
1057     setOperationAction(ISD::ROTL,              MVT::v2i64, Custom);
1058     setOperationAction(ISD::ROTL,              MVT::v32i8, Custom);
1059     setOperationAction(ISD::ROTL,              MVT::v16i16, Custom);
1060     setOperationAction(ISD::ROTL,              MVT::v8i32, Custom);
1061     setOperationAction(ISD::ROTL,              MVT::v4i64, Custom);
1062   }
1063
1064   if (!Subtarget->useSoftFloat() && Subtarget->hasFp256()) {
1065     addRegisterClass(MVT::v32i8,  &X86::VR256RegClass);
1066     addRegisterClass(MVT::v16i16, &X86::VR256RegClass);
1067     addRegisterClass(MVT::v8i32,  &X86::VR256RegClass);
1068     addRegisterClass(MVT::v8f32,  &X86::VR256RegClass);
1069     addRegisterClass(MVT::v4i64,  &X86::VR256RegClass);
1070     addRegisterClass(MVT::v4f64,  &X86::VR256RegClass);
1071
1072     setOperationAction(ISD::LOAD,               MVT::v8f32, Legal);
1073     setOperationAction(ISD::LOAD,               MVT::v4f64, Legal);
1074     setOperationAction(ISD::LOAD,               MVT::v4i64, Legal);
1075
1076     setOperationAction(ISD::FADD,               MVT::v8f32, Legal);
1077     setOperationAction(ISD::FSUB,               MVT::v8f32, Legal);
1078     setOperationAction(ISD::FMUL,               MVT::v8f32, Legal);
1079     setOperationAction(ISD::FDIV,               MVT::v8f32, Legal);
1080     setOperationAction(ISD::FSQRT,              MVT::v8f32, Legal);
1081     setOperationAction(ISD::FFLOOR,             MVT::v8f32, Legal);
1082     setOperationAction(ISD::FCEIL,              MVT::v8f32, Legal);
1083     setOperationAction(ISD::FTRUNC,             MVT::v8f32, Legal);
1084     setOperationAction(ISD::FRINT,              MVT::v8f32, Legal);
1085     setOperationAction(ISD::FNEARBYINT,         MVT::v8f32, Legal);
1086     setOperationAction(ISD::FNEG,               MVT::v8f32, Custom);
1087     setOperationAction(ISD::FABS,               MVT::v8f32, Custom);
1088
1089     setOperationAction(ISD::FADD,               MVT::v4f64, Legal);
1090     setOperationAction(ISD::FSUB,               MVT::v4f64, Legal);
1091     setOperationAction(ISD::FMUL,               MVT::v4f64, Legal);
1092     setOperationAction(ISD::FDIV,               MVT::v4f64, Legal);
1093     setOperationAction(ISD::FSQRT,              MVT::v4f64, Legal);
1094     setOperationAction(ISD::FFLOOR,             MVT::v4f64, Legal);
1095     setOperationAction(ISD::FCEIL,              MVT::v4f64, Legal);
1096     setOperationAction(ISD::FTRUNC,             MVT::v4f64, Legal);
1097     setOperationAction(ISD::FRINT,              MVT::v4f64, Legal);
1098     setOperationAction(ISD::FNEARBYINT,         MVT::v4f64, Legal);
1099     setOperationAction(ISD::FNEG,               MVT::v4f64, Custom);
1100     setOperationAction(ISD::FABS,               MVT::v4f64, Custom);
1101
1102     // (fp_to_int:v8i16 (v8f32 ..)) requires the result type to be promoted
1103     // even though v8i16 is a legal type.
1104     setOperationAction(ISD::FP_TO_SINT,         MVT::v8i16, Promote);
1105     setOperationAction(ISD::FP_TO_UINT,         MVT::v8i16, Promote);
1106     setOperationAction(ISD::FP_TO_SINT,         MVT::v8i32, Legal);
1107
1108     setOperationAction(ISD::SINT_TO_FP,         MVT::v8i16, Promote);
1109     setOperationAction(ISD::SINT_TO_FP,         MVT::v8i32, Legal);
1110     setOperationAction(ISD::FP_ROUND,           MVT::v4f32, Legal);
1111
1112     setOperationAction(ISD::UINT_TO_FP,         MVT::v8i8,  Custom);
1113     setOperationAction(ISD::UINT_TO_FP,         MVT::v8i16, Custom);
1114
1115     for (MVT VT : MVT::fp_vector_valuetypes())
1116       setLoadExtAction(ISD::EXTLOAD, VT, MVT::v4f32, Legal);
1117
1118     setOperationAction(ISD::SRL,               MVT::v16i16, Custom);
1119     setOperationAction(ISD::SRL,               MVT::v32i8, Custom);
1120
1121     setOperationAction(ISD::SHL,               MVT::v16i16, Custom);
1122     setOperationAction(ISD::SHL,               MVT::v32i8, Custom);
1123
1124     setOperationAction(ISD::SRA,               MVT::v16i16, Custom);
1125     setOperationAction(ISD::SRA,               MVT::v32i8, Custom);
1126
1127     setOperationAction(ISD::SETCC,             MVT::v32i8, Custom);
1128     setOperationAction(ISD::SETCC,             MVT::v16i16, Custom);
1129     setOperationAction(ISD::SETCC,             MVT::v8i32, Custom);
1130     setOperationAction(ISD::SETCC,             MVT::v4i64, Custom);
1131
1132     setOperationAction(ISD::SELECT,            MVT::v4f64, Custom);
1133     setOperationAction(ISD::SELECT,            MVT::v4i64, Custom);
1134     setOperationAction(ISD::SELECT,            MVT::v8f32, Custom);
1135
1136     setOperationAction(ISD::SIGN_EXTEND,       MVT::v4i64, Custom);
1137     setOperationAction(ISD::SIGN_EXTEND,       MVT::v8i32, Custom);
1138     setOperationAction(ISD::SIGN_EXTEND,       MVT::v16i16, Custom);
1139     setOperationAction(ISD::ZERO_EXTEND,       MVT::v4i64, Custom);
1140     setOperationAction(ISD::ZERO_EXTEND,       MVT::v8i32, Custom);
1141     setOperationAction(ISD::ZERO_EXTEND,       MVT::v16i16, Custom);
1142     setOperationAction(ISD::ANY_EXTEND,        MVT::v4i64, Custom);
1143     setOperationAction(ISD::ANY_EXTEND,        MVT::v8i32, Custom);
1144     setOperationAction(ISD::ANY_EXTEND,        MVT::v16i16, Custom);
1145     setOperationAction(ISD::TRUNCATE,          MVT::v16i8, Custom);
1146     setOperationAction(ISD::TRUNCATE,          MVT::v8i16, Custom);
1147     setOperationAction(ISD::TRUNCATE,          MVT::v4i32, Custom);
1148
1149     setOperationAction(ISD::CTPOP,             MVT::v32i8, Custom);
1150     setOperationAction(ISD::CTPOP,             MVT::v16i16, Custom);
1151     setOperationAction(ISD::CTPOP,             MVT::v8i32, Custom);
1152     setOperationAction(ISD::CTPOP,             MVT::v4i64, Custom);
1153
1154     setOperationAction(ISD::CTTZ,              MVT::v32i8, Custom);
1155     setOperationAction(ISD::CTTZ,              MVT::v16i16, Custom);
1156     setOperationAction(ISD::CTTZ,              MVT::v8i32, Custom);
1157     setOperationAction(ISD::CTTZ,              MVT::v4i64, Custom);
1158     setOperationAction(ISD::CTTZ_ZERO_UNDEF,   MVT::v32i8, Custom);
1159     setOperationAction(ISD::CTTZ_ZERO_UNDEF,   MVT::v16i16, Custom);
1160     setOperationAction(ISD::CTTZ_ZERO_UNDEF,   MVT::v8i32, Custom);
1161     setOperationAction(ISD::CTTZ_ZERO_UNDEF,   MVT::v4i64, Custom);
1162
1163     if (Subtarget->hasAnyFMA()) {
1164       setOperationAction(ISD::FMA,             MVT::v8f32, Legal);
1165       setOperationAction(ISD::FMA,             MVT::v4f64, Legal);
1166       setOperationAction(ISD::FMA,             MVT::v4f32, Legal);
1167       setOperationAction(ISD::FMA,             MVT::v2f64, Legal);
1168       setOperationAction(ISD::FMA,             MVT::f32, Legal);
1169       setOperationAction(ISD::FMA,             MVT::f64, Legal);
1170     }
1171
1172     if (Subtarget->hasInt256()) {
1173       setOperationAction(ISD::ADD,             MVT::v4i64, Legal);
1174       setOperationAction(ISD::ADD,             MVT::v8i32, Legal);
1175       setOperationAction(ISD::ADD,             MVT::v16i16, Legal);
1176       setOperationAction(ISD::ADD,             MVT::v32i8, Legal);
1177
1178       setOperationAction(ISD::SUB,             MVT::v4i64, Legal);
1179       setOperationAction(ISD::SUB,             MVT::v8i32, Legal);
1180       setOperationAction(ISD::SUB,             MVT::v16i16, Legal);
1181       setOperationAction(ISD::SUB,             MVT::v32i8, Legal);
1182
1183       setOperationAction(ISD::MUL,             MVT::v4i64, Custom);
1184       setOperationAction(ISD::MUL,             MVT::v8i32, Legal);
1185       setOperationAction(ISD::MUL,             MVT::v16i16, Legal);
1186       setOperationAction(ISD::MUL,             MVT::v32i8, Custom);
1187
1188       setOperationAction(ISD::UMUL_LOHI,       MVT::v8i32, Custom);
1189       setOperationAction(ISD::SMUL_LOHI,       MVT::v8i32, Custom);
1190       setOperationAction(ISD::MULHU,           MVT::v16i16, Legal);
1191       setOperationAction(ISD::MULHS,           MVT::v16i16, Legal);
1192
1193       setOperationAction(ISD::SMAX,            MVT::v32i8,  Legal);
1194       setOperationAction(ISD::SMAX,            MVT::v16i16, Legal);
1195       setOperationAction(ISD::SMAX,            MVT::v8i32,  Legal);
1196       setOperationAction(ISD::UMAX,            MVT::v32i8,  Legal);
1197       setOperationAction(ISD::UMAX,            MVT::v16i16, Legal);
1198       setOperationAction(ISD::UMAX,            MVT::v8i32,  Legal);
1199       setOperationAction(ISD::SMIN,            MVT::v32i8,  Legal);
1200       setOperationAction(ISD::SMIN,            MVT::v16i16, Legal);
1201       setOperationAction(ISD::SMIN,            MVT::v8i32,  Legal);
1202       setOperationAction(ISD::UMIN,            MVT::v32i8,  Legal);
1203       setOperationAction(ISD::UMIN,            MVT::v16i16, Legal);
1204       setOperationAction(ISD::UMIN,            MVT::v8i32,  Legal);
1205
1206       // The custom lowering for UINT_TO_FP for v8i32 becomes interesting
1207       // when we have a 256bit-wide blend with immediate.
1208       setOperationAction(ISD::UINT_TO_FP, MVT::v8i32, Custom);
1209
1210       // AVX2 also has wider vector sign/zero extending loads, VPMOV[SZ]X
1211       setLoadExtAction(ISD::SEXTLOAD, MVT::v16i16, MVT::v16i8, Legal);
1212       setLoadExtAction(ISD::SEXTLOAD, MVT::v8i32,  MVT::v8i8,  Legal);
1213       setLoadExtAction(ISD::SEXTLOAD, MVT::v4i64,  MVT::v4i8,  Legal);
1214       setLoadExtAction(ISD::SEXTLOAD, MVT::v8i32,  MVT::v8i16, Legal);
1215       setLoadExtAction(ISD::SEXTLOAD, MVT::v4i64,  MVT::v4i16, Legal);
1216       setLoadExtAction(ISD::SEXTLOAD, MVT::v4i64,  MVT::v4i32, Legal);
1217
1218       setLoadExtAction(ISD::ZEXTLOAD, MVT::v16i16, MVT::v16i8, Legal);
1219       setLoadExtAction(ISD::ZEXTLOAD, MVT::v8i32,  MVT::v8i8,  Legal);
1220       setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i64,  MVT::v4i8,  Legal);
1221       setLoadExtAction(ISD::ZEXTLOAD, MVT::v8i32,  MVT::v8i16, Legal);
1222       setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i64,  MVT::v4i16, Legal);
1223       setLoadExtAction(ISD::ZEXTLOAD, MVT::v4i64,  MVT::v4i32, Legal);
1224     } else {
1225       setOperationAction(ISD::ADD,             MVT::v4i64, Custom);
1226       setOperationAction(ISD::ADD,             MVT::v8i32, Custom);
1227       setOperationAction(ISD::ADD,             MVT::v16i16, Custom);
1228       setOperationAction(ISD::ADD,             MVT::v32i8, Custom);
1229
1230       setOperationAction(ISD::SUB,             MVT::v4i64, Custom);
1231       setOperationAction(ISD::SUB,             MVT::v8i32, Custom);
1232       setOperationAction(ISD::SUB,             MVT::v16i16, Custom);
1233       setOperationAction(ISD::SUB,             MVT::v32i8, Custom);
1234
1235       setOperationAction(ISD::MUL,             MVT::v4i64, Custom);
1236       setOperationAction(ISD::MUL,             MVT::v8i32, Custom);
1237       setOperationAction(ISD::MUL,             MVT::v16i16, Custom);
1238       setOperationAction(ISD::MUL,             MVT::v32i8, Custom);
1239
1240       setOperationAction(ISD::SMAX,            MVT::v32i8,  Custom);
1241       setOperationAction(ISD::SMAX,            MVT::v16i16, Custom);
1242       setOperationAction(ISD::SMAX,            MVT::v8i32,  Custom);
1243       setOperationAction(ISD::UMAX,            MVT::v32i8,  Custom);
1244       setOperationAction(ISD::UMAX,            MVT::v16i16, Custom);
1245       setOperationAction(ISD::UMAX,            MVT::v8i32,  Custom);
1246       setOperationAction(ISD::SMIN,            MVT::v32i8,  Custom);
1247       setOperationAction(ISD::SMIN,            MVT::v16i16, Custom);
1248       setOperationAction(ISD::SMIN,            MVT::v8i32,  Custom);
1249       setOperationAction(ISD::UMIN,            MVT::v32i8,  Custom);
1250       setOperationAction(ISD::UMIN,            MVT::v16i16, Custom);
1251       setOperationAction(ISD::UMIN,            MVT::v8i32,  Custom);
1252     }
1253
1254     // In the customized shift lowering, the legal cases in AVX2 will be
1255     // recognized.
1256     setOperationAction(ISD::SRL,               MVT::v4i64, Custom);
1257     setOperationAction(ISD::SRL,               MVT::v8i32, Custom);
1258
1259     setOperationAction(ISD::SHL,               MVT::v4i64, Custom);
1260     setOperationAction(ISD::SHL,               MVT::v8i32, Custom);
1261
1262     setOperationAction(ISD::SRA,               MVT::v4i64, Custom);
1263     setOperationAction(ISD::SRA,               MVT::v8i32, Custom);
1264
1265     // Custom lower several nodes for 256-bit types.
1266     for (MVT VT : MVT::vector_valuetypes()) {
1267       if (VT.getScalarSizeInBits() >= 32) {
1268         setOperationAction(ISD::MLOAD,  VT, Legal);
1269         setOperationAction(ISD::MSTORE, VT, Legal);
1270       }
1271       // Extract subvector is special because the value type
1272       // (result) is 128-bit but the source is 256-bit wide.
1273       if (VT.is128BitVector()) {
1274         setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
1275       }
1276       // Do not attempt to custom lower other non-256-bit vectors
1277       if (!VT.is256BitVector())
1278         continue;
1279
1280       setOperationAction(ISD::BUILD_VECTOR,       VT, Custom);
1281       setOperationAction(ISD::VECTOR_SHUFFLE,     VT, Custom);
1282       setOperationAction(ISD::VSELECT,            VT, Custom);
1283       setOperationAction(ISD::INSERT_VECTOR_ELT,  VT, Custom);
1284       setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
1285       setOperationAction(ISD::SCALAR_TO_VECTOR,   VT, Custom);
1286       setOperationAction(ISD::INSERT_SUBVECTOR,   VT, Custom);
1287       setOperationAction(ISD::CONCAT_VECTORS,     VT, Custom);
1288     }
1289
1290     if (Subtarget->hasInt256())
1291       setOperationAction(ISD::VSELECT,         MVT::v32i8, Legal);
1292
1293     // Promote v32i8, v16i16, v8i32 select, and, or, xor to v4i64.
1294     for (auto VT : { MVT::v32i8, MVT::v16i16, MVT::v8i32 }) {
1295       setOperationAction(ISD::AND,    VT, Promote);
1296       AddPromotedToType (ISD::AND,    VT, MVT::v4i64);
1297       setOperationAction(ISD::OR,     VT, Promote);
1298       AddPromotedToType (ISD::OR,     VT, MVT::v4i64);
1299       setOperationAction(ISD::XOR,    VT, Promote);
1300       AddPromotedToType (ISD::XOR,    VT, MVT::v4i64);
1301       setOperationAction(ISD::LOAD,   VT, Promote);
1302       AddPromotedToType (ISD::LOAD,   VT, MVT::v4i64);
1303       setOperationAction(ISD::SELECT, VT, Promote);
1304       AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
1305     }
1306   }
1307
1308   if (!Subtarget->useSoftFloat() && Subtarget->hasAVX512()) {
1309     addRegisterClass(MVT::v16i32, &X86::VR512RegClass);
1310     addRegisterClass(MVT::v16f32, &X86::VR512RegClass);
1311     addRegisterClass(MVT::v8i64,  &X86::VR512RegClass);
1312     addRegisterClass(MVT::v8f64,  &X86::VR512RegClass);
1313
1314     addRegisterClass(MVT::i1,     &X86::VK1RegClass);
1315     addRegisterClass(MVT::v8i1,   &X86::VK8RegClass);
1316     addRegisterClass(MVT::v16i1,  &X86::VK16RegClass);
1317
1318     for (MVT VT : MVT::fp_vector_valuetypes())
1319       setLoadExtAction(ISD::EXTLOAD, VT, MVT::v8f32, Legal);
1320
1321     setLoadExtAction(ISD::ZEXTLOAD, MVT::v16i32, MVT::v16i8, Legal);
1322     setLoadExtAction(ISD::SEXTLOAD, MVT::v16i32, MVT::v16i8, Legal);
1323     setLoadExtAction(ISD::ZEXTLOAD, MVT::v16i32, MVT::v16i16, Legal);
1324     setLoadExtAction(ISD::SEXTLOAD, MVT::v16i32, MVT::v16i16, Legal);
1325     setLoadExtAction(ISD::ZEXTLOAD, MVT::v32i16, MVT::v32i8, Legal);
1326     setLoadExtAction(ISD::SEXTLOAD, MVT::v32i16, MVT::v32i8, Legal);
1327     setLoadExtAction(ISD::ZEXTLOAD, MVT::v8i64,  MVT::v8i8,  Legal);
1328     setLoadExtAction(ISD::SEXTLOAD, MVT::v8i64,  MVT::v8i8,  Legal);
1329     setLoadExtAction(ISD::ZEXTLOAD, MVT::v8i64,  MVT::v8i16,  Legal);
1330     setLoadExtAction(ISD::SEXTLOAD, MVT::v8i64,  MVT::v8i16,  Legal);
1331     setLoadExtAction(ISD::ZEXTLOAD, MVT::v8i64,  MVT::v8i32,  Legal);
1332     setLoadExtAction(ISD::SEXTLOAD, MVT::v8i64,  MVT::v8i32,  Legal);
1333
1334     setOperationAction(ISD::BR_CC,              MVT::i1,    Expand);
1335     setOperationAction(ISD::SETCC,              MVT::i1,    Custom);
1336     setOperationAction(ISD::SELECT_CC,          MVT::i1,    Expand);
1337     setOperationAction(ISD::XOR,                MVT::i1,    Legal);
1338     setOperationAction(ISD::OR,                 MVT::i1,    Legal);
1339     setOperationAction(ISD::AND,                MVT::i1,    Legal);
1340     setOperationAction(ISD::SUB,                MVT::i1,    Custom);
1341     setOperationAction(ISD::ADD,                MVT::i1,    Custom);
1342     setOperationAction(ISD::MUL,                MVT::i1,    Custom);
1343     setOperationAction(ISD::LOAD,               MVT::v16f32, Legal);
1344     setOperationAction(ISD::LOAD,               MVT::v8f64, Legal);
1345     setOperationAction(ISD::LOAD,               MVT::v8i64, Legal);
1346     setOperationAction(ISD::LOAD,               MVT::v16i32, Legal);
1347     setOperationAction(ISD::LOAD,               MVT::v16i1, Legal);
1348
1349     setOperationAction(ISD::FADD,               MVT::v16f32, Legal);
1350     setOperationAction(ISD::FSUB,               MVT::v16f32, Legal);
1351     setOperationAction(ISD::FMUL,               MVT::v16f32, Legal);
1352     setOperationAction(ISD::FDIV,               MVT::v16f32, Legal);
1353     setOperationAction(ISD::FSQRT,              MVT::v16f32, Legal);
1354     setOperationAction(ISD::FNEG,               MVT::v16f32, Custom);
1355     setOperationAction(ISD::FABS,               MVT::v16f32, Custom);
1356
1357     setOperationAction(ISD::FADD,               MVT::v8f64, Legal);
1358     setOperationAction(ISD::FSUB,               MVT::v8f64, Legal);
1359     setOperationAction(ISD::FMUL,               MVT::v8f64, Legal);
1360     setOperationAction(ISD::FDIV,               MVT::v8f64, Legal);
1361     setOperationAction(ISD::FSQRT,              MVT::v8f64, Legal);
1362     setOperationAction(ISD::FNEG,               MVT::v8f64, Custom);
1363     setOperationAction(ISD::FABS,               MVT::v8f64, Custom);
1364     setOperationAction(ISD::FMA,                MVT::v8f64, Legal);
1365     setOperationAction(ISD::FMA,                MVT::v16f32, Legal);
1366
1367     setOperationAction(ISD::FP_TO_SINT,         MVT::v16i32, Legal);
1368     setOperationAction(ISD::FP_TO_UINT,         MVT::v16i32, Legal);
1369     setOperationAction(ISD::FP_TO_UINT,         MVT::v8i32, Legal);
1370     setOperationAction(ISD::FP_TO_UINT,         MVT::v4i32, Legal);
1371     setOperationAction(ISD::SINT_TO_FP,         MVT::v16i32, Legal);
1372     setOperationAction(ISD::SINT_TO_FP,         MVT::v8i1,   Custom);
1373     setOperationAction(ISD::SINT_TO_FP,         MVT::v16i1,  Custom);
1374     setOperationAction(ISD::SINT_TO_FP,         MVT::v16i8,  Promote);
1375     setOperationAction(ISD::SINT_TO_FP,         MVT::v16i16, Promote);
1376     setOperationAction(ISD::UINT_TO_FP,         MVT::v16i32, Legal);
1377     setOperationAction(ISD::UINT_TO_FP,         MVT::v8i32, Legal);
1378     setOperationAction(ISD::UINT_TO_FP,         MVT::v4i32, Legal);
1379     setOperationAction(ISD::UINT_TO_FP,         MVT::v16i8, Custom);
1380     setOperationAction(ISD::UINT_TO_FP,         MVT::v16i16, Custom);
1381     setOperationAction(ISD::FP_ROUND,           MVT::v8f32, Legal);
1382     setOperationAction(ISD::FP_EXTEND,          MVT::v8f32, Legal);
1383
1384     setTruncStoreAction(MVT::v8i64,   MVT::v8i8,   Legal);
1385     setTruncStoreAction(MVT::v8i64,   MVT::v8i16,  Legal);
1386     setTruncStoreAction(MVT::v8i64,   MVT::v8i32,  Legal);
1387     setTruncStoreAction(MVT::v16i32,  MVT::v16i8,  Legal);
1388     setTruncStoreAction(MVT::v16i32,  MVT::v16i16, Legal);
1389     if (Subtarget->hasVLX()){
1390       setTruncStoreAction(MVT::v4i64, MVT::v4i8,  Legal);
1391       setTruncStoreAction(MVT::v4i64, MVT::v4i16, Legal);
1392       setTruncStoreAction(MVT::v4i64, MVT::v4i32, Legal);
1393       setTruncStoreAction(MVT::v8i32, MVT::v8i8,  Legal);
1394       setTruncStoreAction(MVT::v8i32, MVT::v8i16, Legal);
1395
1396       setTruncStoreAction(MVT::v2i64, MVT::v2i8,  Legal);
1397       setTruncStoreAction(MVT::v2i64, MVT::v2i16, Legal);
1398       setTruncStoreAction(MVT::v2i64, MVT::v2i32, Legal);
1399       setTruncStoreAction(MVT::v4i32, MVT::v4i8,  Legal);
1400       setTruncStoreAction(MVT::v4i32, MVT::v4i16, Legal);
1401     } else {
1402       setOperationAction(ISD::MLOAD,    MVT::v8i32, Custom);
1403       setOperationAction(ISD::MLOAD,    MVT::v8f32, Custom);
1404       setOperationAction(ISD::MSTORE,   MVT::v8i32, Custom);
1405       setOperationAction(ISD::MSTORE,   MVT::v8f32, Custom);
1406     }
1407     setOperationAction(ISD::TRUNCATE,           MVT::i1, Custom);
1408     setOperationAction(ISD::TRUNCATE,           MVT::v16i8, Custom);
1409     setOperationAction(ISD::TRUNCATE,           MVT::v8i32, Custom);
1410     setOperationAction(ISD::VECTOR_SHUFFLE,     MVT::v8i1,  Custom);
1411     setOperationAction(ISD::VECTOR_SHUFFLE,     MVT::v16i1, Custom);
1412     if (Subtarget->hasDQI()) {
1413       setOperationAction(ISD::TRUNCATE,         MVT::v2i1, Custom);
1414       setOperationAction(ISD::TRUNCATE,         MVT::v4i1, Custom);
1415
1416       setOperationAction(ISD::SINT_TO_FP,       MVT::v8i64, Legal);
1417       setOperationAction(ISD::UINT_TO_FP,       MVT::v8i64, Legal);
1418       setOperationAction(ISD::FP_TO_SINT,       MVT::v8i64, Legal);
1419       setOperationAction(ISD::FP_TO_UINT,       MVT::v8i64, Legal);
1420       if (Subtarget->hasVLX()) {
1421         setOperationAction(ISD::SINT_TO_FP,    MVT::v4i64, Legal);
1422         setOperationAction(ISD::SINT_TO_FP,    MVT::v2i64, Legal);
1423         setOperationAction(ISD::UINT_TO_FP,    MVT::v4i64, Legal);
1424         setOperationAction(ISD::UINT_TO_FP,    MVT::v2i64, Legal);
1425         setOperationAction(ISD::FP_TO_SINT,    MVT::v4i64, Legal);
1426         setOperationAction(ISD::FP_TO_SINT,    MVT::v2i64, Legal);
1427         setOperationAction(ISD::FP_TO_UINT,    MVT::v4i64, Legal);
1428         setOperationAction(ISD::FP_TO_UINT,    MVT::v2i64, Legal);
1429       }
1430     }
1431     if (Subtarget->hasVLX()) {
1432       setOperationAction(ISD::SINT_TO_FP,       MVT::v8i32, Legal);
1433       setOperationAction(ISD::UINT_TO_FP,       MVT::v8i32, Legal);
1434       setOperationAction(ISD::FP_TO_SINT,       MVT::v8i32, Legal);
1435       setOperationAction(ISD::FP_TO_UINT,       MVT::v8i32, Legal);
1436       setOperationAction(ISD::SINT_TO_FP,       MVT::v4i32, Legal);
1437       setOperationAction(ISD::UINT_TO_FP,       MVT::v4i32, Legal);
1438       setOperationAction(ISD::FP_TO_SINT,       MVT::v4i32, Legal);
1439       setOperationAction(ISD::FP_TO_UINT,       MVT::v4i32, Legal);
1440     }
1441     setOperationAction(ISD::TRUNCATE,           MVT::v8i1, Custom);
1442     setOperationAction(ISD::TRUNCATE,           MVT::v16i1, Custom);
1443     setOperationAction(ISD::TRUNCATE,           MVT::v16i16, Custom);
1444     setOperationAction(ISD::ZERO_EXTEND,        MVT::v16i32, Custom);
1445     setOperationAction(ISD::ZERO_EXTEND,        MVT::v8i64, Custom);
1446     setOperationAction(ISD::ANY_EXTEND,         MVT::v16i32, Custom);
1447     setOperationAction(ISD::ANY_EXTEND,         MVT::v8i64, Custom);
1448     setOperationAction(ISD::SIGN_EXTEND,        MVT::v16i32, Custom);
1449     setOperationAction(ISD::SIGN_EXTEND,        MVT::v8i64, Custom);
1450     setOperationAction(ISD::SIGN_EXTEND,        MVT::v16i8, Custom);
1451     setOperationAction(ISD::SIGN_EXTEND,        MVT::v8i16, Custom);
1452     setOperationAction(ISD::SIGN_EXTEND,        MVT::v16i16, Custom);
1453     if (Subtarget->hasDQI()) {
1454       setOperationAction(ISD::SIGN_EXTEND,        MVT::v4i32, Custom);
1455       setOperationAction(ISD::SIGN_EXTEND,        MVT::v2i64, Custom);
1456     }
1457     setOperationAction(ISD::FFLOOR,             MVT::v16f32, Legal);
1458     setOperationAction(ISD::FFLOOR,             MVT::v8f64, Legal);
1459     setOperationAction(ISD::FCEIL,              MVT::v16f32, Legal);
1460     setOperationAction(ISD::FCEIL,              MVT::v8f64, Legal);
1461     setOperationAction(ISD::FTRUNC,             MVT::v16f32, Legal);
1462     setOperationAction(ISD::FTRUNC,             MVT::v8f64, Legal);
1463     setOperationAction(ISD::FRINT,              MVT::v16f32, Legal);
1464     setOperationAction(ISD::FRINT,              MVT::v8f64, Legal);
1465     setOperationAction(ISD::FNEARBYINT,         MVT::v16f32, Legal);
1466     setOperationAction(ISD::FNEARBYINT,         MVT::v8f64, Legal);
1467
1468     setOperationAction(ISD::CONCAT_VECTORS,     MVT::v8f64,  Custom);
1469     setOperationAction(ISD::CONCAT_VECTORS,     MVT::v8i64,  Custom);
1470     setOperationAction(ISD::CONCAT_VECTORS,     MVT::v16f32,  Custom);
1471     setOperationAction(ISD::CONCAT_VECTORS,     MVT::v16i32,  Custom);
1472     setOperationAction(ISD::CONCAT_VECTORS,     MVT::v16i1,   Custom);
1473
1474     setOperationAction(ISD::SETCC,              MVT::v16i1, Custom);
1475     setOperationAction(ISD::SETCC,              MVT::v8i1, Custom);
1476
1477     setOperationAction(ISD::MUL,              MVT::v8i64, Custom);
1478
1479     setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i1,  Custom);
1480     setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i1, Custom);
1481     setOperationAction(ISD::INSERT_SUBVECTOR,   MVT::v16i1, Custom);
1482     setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v16i1, Custom);
1483     setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v8i1, Custom);
1484     setOperationAction(ISD::BUILD_VECTOR,       MVT::v8i1, Custom);
1485     setOperationAction(ISD::BUILD_VECTOR,       MVT::v16i1, Custom);
1486     setOperationAction(ISD::SELECT,             MVT::v8f64, Custom);
1487     setOperationAction(ISD::SELECT,             MVT::v8i64, Custom);
1488     setOperationAction(ISD::SELECT,             MVT::v16f32, Custom);
1489     setOperationAction(ISD::SELECT,             MVT::v16i1, Custom);
1490     setOperationAction(ISD::SELECT,             MVT::v8i1,  Custom);
1491
1492     setOperationAction(ISD::SMAX,               MVT::v16i32, Legal);
1493     setOperationAction(ISD::SMAX,               MVT::v8i64, Legal);
1494     setOperationAction(ISD::UMAX,               MVT::v16i32, Legal);
1495     setOperationAction(ISD::UMAX,               MVT::v8i64, Legal);
1496     setOperationAction(ISD::SMIN,               MVT::v16i32, Legal);
1497     setOperationAction(ISD::SMIN,               MVT::v8i64, Legal);
1498     setOperationAction(ISD::UMIN,               MVT::v16i32, Legal);
1499     setOperationAction(ISD::UMIN,               MVT::v8i64, Legal);
1500
1501     setOperationAction(ISD::ADD,                MVT::v8i64, Legal);
1502     setOperationAction(ISD::ADD,                MVT::v16i32, Legal);
1503
1504     setOperationAction(ISD::SUB,                MVT::v8i64, Legal);
1505     setOperationAction(ISD::SUB,                MVT::v16i32, Legal);
1506
1507     setOperationAction(ISD::MUL,                MVT::v16i32, Legal);
1508
1509     setOperationAction(ISD::SRL,                MVT::v8i64, Custom);
1510     setOperationAction(ISD::SRL,                MVT::v16i32, Custom);
1511
1512     setOperationAction(ISD::SHL,                MVT::v8i64, Custom);
1513     setOperationAction(ISD::SHL,                MVT::v16i32, Custom);
1514
1515     setOperationAction(ISD::SRA,                MVT::v8i64, Custom);
1516     setOperationAction(ISD::SRA,                MVT::v16i32, Custom);
1517
1518     setOperationAction(ISD::AND,                MVT::v8i64, Legal);
1519     setOperationAction(ISD::OR,                 MVT::v8i64, Legal);
1520     setOperationAction(ISD::XOR,                MVT::v8i64, Legal);
1521     setOperationAction(ISD::AND,                MVT::v16i32, Legal);
1522     setOperationAction(ISD::OR,                 MVT::v16i32, Legal);
1523     setOperationAction(ISD::XOR,                MVT::v16i32, Legal);
1524
1525     if (Subtarget->hasCDI()) {
1526       setOperationAction(ISD::CTLZ,             MVT::v8i64,  Legal);
1527       setOperationAction(ISD::CTLZ,             MVT::v16i32, Legal);
1528       setOperationAction(ISD::CTLZ_ZERO_UNDEF,  MVT::v8i64,  Expand);
1529       setOperationAction(ISD::CTLZ_ZERO_UNDEF,  MVT::v16i32, Expand);
1530
1531       setOperationAction(ISD::CTLZ,             MVT::v8i16,  Custom);
1532       setOperationAction(ISD::CTLZ,             MVT::v16i8,  Custom);
1533       setOperationAction(ISD::CTLZ,             MVT::v16i16, Custom);
1534       setOperationAction(ISD::CTLZ,             MVT::v32i8,  Custom);
1535       setOperationAction(ISD::CTLZ_ZERO_UNDEF,  MVT::v8i16,  Expand);
1536       setOperationAction(ISD::CTLZ_ZERO_UNDEF,  MVT::v16i8,  Expand);
1537       setOperationAction(ISD::CTLZ_ZERO_UNDEF,  MVT::v16i16, Expand);
1538       setOperationAction(ISD::CTLZ_ZERO_UNDEF,  MVT::v32i8,  Expand);
1539
1540       setOperationAction(ISD::CTTZ_ZERO_UNDEF,  MVT::v8i64,  Custom);
1541       setOperationAction(ISD::CTTZ_ZERO_UNDEF,  MVT::v16i32, Custom);
1542
1543       if (Subtarget->hasVLX()) {
1544         setOperationAction(ISD::CTLZ,             MVT::v4i64, Legal);
1545         setOperationAction(ISD::CTLZ,             MVT::v8i32, Legal);
1546         setOperationAction(ISD::CTLZ,             MVT::v2i64, Legal);
1547         setOperationAction(ISD::CTLZ,             MVT::v4i32, Legal);
1548         setOperationAction(ISD::CTLZ_ZERO_UNDEF,  MVT::v4i64, Expand);
1549         setOperationAction(ISD::CTLZ_ZERO_UNDEF,  MVT::v8i32, Expand);
1550         setOperationAction(ISD::CTLZ_ZERO_UNDEF,  MVT::v2i64, Expand);
1551         setOperationAction(ISD::CTLZ_ZERO_UNDEF,  MVT::v4i32, Expand);
1552
1553         setOperationAction(ISD::CTTZ_ZERO_UNDEF,  MVT::v4i64, Custom);
1554         setOperationAction(ISD::CTTZ_ZERO_UNDEF,  MVT::v8i32, Custom);
1555         setOperationAction(ISD::CTTZ_ZERO_UNDEF,  MVT::v2i64, Custom);
1556         setOperationAction(ISD::CTTZ_ZERO_UNDEF,  MVT::v4i32, Custom);
1557       } else {
1558         setOperationAction(ISD::CTLZ,             MVT::v4i64, Custom);
1559         setOperationAction(ISD::CTLZ,             MVT::v8i32, Custom);
1560         setOperationAction(ISD::CTLZ,             MVT::v2i64, Custom);
1561         setOperationAction(ISD::CTLZ,             MVT::v4i32, Custom);
1562         setOperationAction(ISD::CTLZ_ZERO_UNDEF,  MVT::v4i64, Expand);
1563         setOperationAction(ISD::CTLZ_ZERO_UNDEF,  MVT::v8i32, Expand);
1564         setOperationAction(ISD::CTLZ_ZERO_UNDEF,  MVT::v2i64, Expand);
1565         setOperationAction(ISD::CTLZ_ZERO_UNDEF,  MVT::v4i32, Expand);
1566       }
1567     } // Subtarget->hasCDI()
1568
1569     if (Subtarget->hasDQI()) {
1570       setOperationAction(ISD::MUL,             MVT::v2i64, Legal);
1571       setOperationAction(ISD::MUL,             MVT::v4i64, Legal);
1572       setOperationAction(ISD::MUL,             MVT::v8i64, Legal);
1573     }
1574     // Custom lower several nodes.
1575     for (MVT VT : MVT::vector_valuetypes()) {
1576       unsigned EltSize = VT.getVectorElementType().getSizeInBits();
1577       if (EltSize == 1) {
1578         setOperationAction(ISD::AND, VT, Legal);
1579         setOperationAction(ISD::OR,  VT, Legal);
1580         setOperationAction(ISD::XOR,  VT, Legal);
1581       }
1582       if ((VT.is128BitVector() || VT.is256BitVector()) && EltSize >= 32) {
1583         setOperationAction(ISD::MGATHER,  VT, Custom);
1584         setOperationAction(ISD::MSCATTER, VT, Custom);
1585       }
1586       // Extract subvector is special because the value type
1587       // (result) is 256/128-bit but the source is 512-bit wide.
1588       if (VT.is128BitVector() || VT.is256BitVector()) {
1589         setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Custom);
1590       }
1591       if (VT.getVectorElementType() == MVT::i1)
1592         setOperationAction(ISD::EXTRACT_SUBVECTOR, VT, Legal);
1593
1594       // Do not attempt to custom lower other non-512-bit vectors
1595       if (!VT.is512BitVector())
1596         continue;
1597
1598       if (EltSize >= 32) {
1599         setOperationAction(ISD::VECTOR_SHUFFLE,      VT, Custom);
1600         setOperationAction(ISD::INSERT_VECTOR_ELT,   VT, Custom);
1601         setOperationAction(ISD::BUILD_VECTOR,        VT, Custom);
1602         setOperationAction(ISD::VSELECT,             VT, Legal);
1603         setOperationAction(ISD::EXTRACT_VECTOR_ELT,  VT, Custom);
1604         setOperationAction(ISD::SCALAR_TO_VECTOR,    VT, Custom);
1605         setOperationAction(ISD::INSERT_SUBVECTOR,    VT, Custom);
1606         setOperationAction(ISD::MLOAD,               VT, Legal);
1607         setOperationAction(ISD::MSTORE,              VT, Legal);
1608         setOperationAction(ISD::MGATHER,  VT, Legal);
1609         setOperationAction(ISD::MSCATTER, VT, Custom);
1610       }
1611     }
1612     for (auto VT : { MVT::v64i8, MVT::v32i16, MVT::v16i32 }) {
1613       setOperationAction(ISD::SELECT, VT, Promote);
1614       AddPromotedToType (ISD::SELECT, VT, MVT::v8i64);
1615     }
1616   }// has  AVX-512
1617
1618   if (!Subtarget->useSoftFloat() && Subtarget->hasBWI()) {
1619     addRegisterClass(MVT::v32i16, &X86::VR512RegClass);
1620     addRegisterClass(MVT::v64i8,  &X86::VR512RegClass);
1621
1622     addRegisterClass(MVT::v32i1,  &X86::VK32RegClass);
1623     addRegisterClass(MVT::v64i1,  &X86::VK64RegClass);
1624
1625     setOperationAction(ISD::LOAD,               MVT::v32i16, Legal);
1626     setOperationAction(ISD::LOAD,               MVT::v64i8, Legal);
1627     setOperationAction(ISD::SETCC,              MVT::v32i1, Custom);
1628     setOperationAction(ISD::SETCC,              MVT::v64i1, Custom);
1629     setOperationAction(ISD::ADD,                MVT::v32i16, Legal);
1630     setOperationAction(ISD::ADD,                MVT::v64i8, Legal);
1631     setOperationAction(ISD::SUB,                MVT::v32i16, Legal);
1632     setOperationAction(ISD::SUB,                MVT::v64i8, Legal);
1633     setOperationAction(ISD::MUL,                MVT::v32i16, Legal);
1634     setOperationAction(ISD::MULHS,              MVT::v32i16, Legal);
1635     setOperationAction(ISD::MULHU,              MVT::v32i16, Legal);
1636     setOperationAction(ISD::CONCAT_VECTORS,     MVT::v32i1, Custom);
1637     setOperationAction(ISD::CONCAT_VECTORS,     MVT::v64i1, Custom);
1638     setOperationAction(ISD::CONCAT_VECTORS,     MVT::v32i16, Custom);
1639     setOperationAction(ISD::CONCAT_VECTORS,     MVT::v64i8, Custom);
1640     setOperationAction(ISD::INSERT_SUBVECTOR,   MVT::v32i1, Custom);
1641     setOperationAction(ISD::INSERT_SUBVECTOR,   MVT::v64i1, Custom);
1642     setOperationAction(ISD::INSERT_SUBVECTOR,   MVT::v32i16, Custom);
1643     setOperationAction(ISD::INSERT_SUBVECTOR,   MVT::v64i8, Custom);
1644     setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v32i16, Custom);
1645     setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v64i8, Custom);
1646     setOperationAction(ISD::SELECT,             MVT::v32i1, Custom);
1647     setOperationAction(ISD::SELECT,             MVT::v64i1, Custom);
1648     setOperationAction(ISD::SIGN_EXTEND,        MVT::v32i8, Custom);
1649     setOperationAction(ISD::ZERO_EXTEND,        MVT::v32i8, Custom);
1650     setOperationAction(ISD::SIGN_EXTEND,        MVT::v32i16, Custom);
1651     setOperationAction(ISD::ZERO_EXTEND,        MVT::v32i16, Custom);
1652     setOperationAction(ISD::VECTOR_SHUFFLE,     MVT::v32i16, Custom);
1653     setOperationAction(ISD::VECTOR_SHUFFLE,     MVT::v64i8, Custom);
1654     setOperationAction(ISD::SIGN_EXTEND,        MVT::v64i8, Custom);
1655     setOperationAction(ISD::ZERO_EXTEND,        MVT::v64i8, Custom);
1656     setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v32i1, Custom);
1657     setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v64i1, Custom);
1658     setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v32i16, Custom);
1659     setOperationAction(ISD::INSERT_VECTOR_ELT,  MVT::v64i8, Custom);
1660     setOperationAction(ISD::VSELECT,            MVT::v32i16, Legal);
1661     setOperationAction(ISD::VSELECT,            MVT::v64i8, Legal);
1662     setOperationAction(ISD::TRUNCATE,           MVT::v32i1, Custom);
1663     setOperationAction(ISD::TRUNCATE,           MVT::v64i1, Custom);
1664     setOperationAction(ISD::TRUNCATE,           MVT::v32i8, Custom);
1665     setOperationAction(ISD::VECTOR_SHUFFLE,     MVT::v32i1, Custom);
1666     setOperationAction(ISD::VECTOR_SHUFFLE,     MVT::v64i1, Custom);
1667
1668     setOperationAction(ISD::SMAX,               MVT::v64i8, Legal);
1669     setOperationAction(ISD::SMAX,               MVT::v32i16, Legal);
1670     setOperationAction(ISD::UMAX,               MVT::v64i8, Legal);
1671     setOperationAction(ISD::UMAX,               MVT::v32i16, Legal);
1672     setOperationAction(ISD::SMIN,               MVT::v64i8, Legal);
1673     setOperationAction(ISD::SMIN,               MVT::v32i16, Legal);
1674     setOperationAction(ISD::UMIN,               MVT::v64i8, Legal);
1675     setOperationAction(ISD::UMIN,               MVT::v32i16, Legal);
1676
1677     setTruncStoreAction(MVT::v32i16,  MVT::v32i8, Legal);
1678     setTruncStoreAction(MVT::v16i16,  MVT::v16i8, Legal);
1679     if (Subtarget->hasVLX())
1680       setTruncStoreAction(MVT::v8i16,   MVT::v8i8,  Legal);
1681
1682     if (Subtarget->hasCDI()) {
1683       setOperationAction(ISD::CTLZ,            MVT::v32i16, Custom);
1684       setOperationAction(ISD::CTLZ,            MVT::v64i8,  Custom);
1685       setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::v32i16, Expand);
1686       setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::v64i8,  Expand);
1687     }
1688
1689     for (auto VT : { MVT::v64i8, MVT::v32i16 }) {
1690       setOperationAction(ISD::BUILD_VECTOR,        VT, Custom);
1691       setOperationAction(ISD::VSELECT,             VT, Legal);
1692       setOperationAction(ISD::SRL,                 VT, Custom);
1693       setOperationAction(ISD::SHL,                 VT, Custom);
1694       setOperationAction(ISD::SRA,                 VT, Custom);
1695
1696       setOperationAction(ISD::AND,    VT, Promote);
1697       AddPromotedToType (ISD::AND,    VT, MVT::v8i64);
1698       setOperationAction(ISD::OR,     VT, Promote);
1699       AddPromotedToType (ISD::OR,     VT, MVT::v8i64);
1700       setOperationAction(ISD::XOR,    VT, Promote);
1701       AddPromotedToType (ISD::XOR,    VT, MVT::v8i64);
1702     }
1703   }
1704
1705   if (!Subtarget->useSoftFloat() && Subtarget->hasVLX()) {
1706     addRegisterClass(MVT::v4i1,   &X86::VK4RegClass);
1707     addRegisterClass(MVT::v2i1,   &X86::VK2RegClass);
1708
1709     setOperationAction(ISD::SETCC,              MVT::v4i1, Custom);
1710     setOperationAction(ISD::SETCC,              MVT::v2i1, Custom);
1711     setOperationAction(ISD::CONCAT_VECTORS,     MVT::v4i1, Custom);
1712     setOperationAction(ISD::CONCAT_VECTORS,     MVT::v8i1, Custom);
1713     setOperationAction(ISD::INSERT_SUBVECTOR,   MVT::v8i1, Custom);
1714     setOperationAction(ISD::INSERT_SUBVECTOR,   MVT::v4i1, Custom);
1715     setOperationAction(ISD::SELECT,             MVT::v4i1, Custom);
1716     setOperationAction(ISD::SELECT,             MVT::v2i1, Custom);
1717     setOperationAction(ISD::BUILD_VECTOR,       MVT::v4i1, Custom);
1718     setOperationAction(ISD::BUILD_VECTOR,       MVT::v2i1, Custom);
1719     setOperationAction(ISD::VECTOR_SHUFFLE,     MVT::v2i1, Custom);
1720     setOperationAction(ISD::VECTOR_SHUFFLE,     MVT::v4i1, Custom);
1721
1722     setOperationAction(ISD::AND,                MVT::v8i32, Legal);
1723     setOperationAction(ISD::OR,                 MVT::v8i32, Legal);
1724     setOperationAction(ISD::XOR,                MVT::v8i32, Legal);
1725     setOperationAction(ISD::AND,                MVT::v4i32, Legal);
1726     setOperationAction(ISD::OR,                 MVT::v4i32, Legal);
1727     setOperationAction(ISD::XOR,                MVT::v4i32, Legal);
1728     setOperationAction(ISD::SRA,                MVT::v2i64, Custom);
1729     setOperationAction(ISD::SRA,                MVT::v4i64, Custom);
1730
1731     setOperationAction(ISD::SMAX,               MVT::v2i64, Legal);
1732     setOperationAction(ISD::SMAX,               MVT::v4i64, Legal);
1733     setOperationAction(ISD::UMAX,               MVT::v2i64, Legal);
1734     setOperationAction(ISD::UMAX,               MVT::v4i64, Legal);
1735     setOperationAction(ISD::SMIN,               MVT::v2i64, Legal);
1736     setOperationAction(ISD::SMIN,               MVT::v4i64, Legal);
1737     setOperationAction(ISD::UMIN,               MVT::v2i64, Legal);
1738     setOperationAction(ISD::UMIN,               MVT::v4i64, Legal);
1739   }
1740
1741   // We want to custom lower some of our intrinsics.
1742   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
1743   setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
1744   setOperationAction(ISD::INTRINSIC_VOID, MVT::Other, Custom);
1745   if (!Subtarget->is64Bit()) {
1746     setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::i64, Custom);
1747     setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i64, Custom);
1748   }
1749
1750   // Only custom-lower 64-bit SADDO and friends on 64-bit because we don't
1751   // handle type legalization for these operations here.
1752   //
1753   // FIXME: We really should do custom legalization for addition and
1754   // subtraction on x86-32 once PR3203 is fixed.  We really can't do much better
1755   // than generic legalization for 64-bit multiplication-with-overflow, though.
1756   for (auto VT : { MVT::i8, MVT::i16, MVT::i32, MVT::i64 }) {
1757     if (VT == MVT::i64 && !Subtarget->is64Bit())
1758       continue;
1759     // Add/Sub/Mul with overflow operations are custom lowered.
1760     setOperationAction(ISD::SADDO, VT, Custom);
1761     setOperationAction(ISD::UADDO, VT, Custom);
1762     setOperationAction(ISD::SSUBO, VT, Custom);
1763     setOperationAction(ISD::USUBO, VT, Custom);
1764     setOperationAction(ISD::SMULO, VT, Custom);
1765     setOperationAction(ISD::UMULO, VT, Custom);
1766   }
1767
1768   if (!Subtarget->is64Bit()) {
1769     // These libcalls are not available in 32-bit.
1770     setLibcallName(RTLIB::SHL_I128, nullptr);
1771     setLibcallName(RTLIB::SRL_I128, nullptr);
1772     setLibcallName(RTLIB::SRA_I128, nullptr);
1773   }
1774
1775   // Combine sin / cos into one node or libcall if possible.
1776   if (Subtarget->hasSinCos()) {
1777     setLibcallName(RTLIB::SINCOS_F32, "sincosf");
1778     setLibcallName(RTLIB::SINCOS_F64, "sincos");
1779     if (Subtarget->isTargetDarwin()) {
1780       // For MacOSX, we don't want the normal expansion of a libcall to sincos.
1781       // We want to issue a libcall to __sincos_stret to avoid memory traffic.
1782       setOperationAction(ISD::FSINCOS, MVT::f64, Custom);
1783       setOperationAction(ISD::FSINCOS, MVT::f32, Custom);
1784     }
1785   }
1786
1787   if (Subtarget->isTargetWin64()) {
1788     setOperationAction(ISD::SDIV, MVT::i128, Custom);
1789     setOperationAction(ISD::UDIV, MVT::i128, Custom);
1790     setOperationAction(ISD::SREM, MVT::i128, Custom);
1791     setOperationAction(ISD::UREM, MVT::i128, Custom);
1792     setOperationAction(ISD::SDIVREM, MVT::i128, Custom);
1793     setOperationAction(ISD::UDIVREM, MVT::i128, Custom);
1794   }
1795
1796   // We have target-specific dag combine patterns for the following nodes:
1797   setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
1798   setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
1799   setTargetDAGCombine(ISD::BITCAST);
1800   setTargetDAGCombine(ISD::VSELECT);
1801   setTargetDAGCombine(ISD::SELECT);
1802   setTargetDAGCombine(ISD::SHL);
1803   setTargetDAGCombine(ISD::SRA);
1804   setTargetDAGCombine(ISD::SRL);
1805   setTargetDAGCombine(ISD::OR);
1806   setTargetDAGCombine(ISD::AND);
1807   setTargetDAGCombine(ISD::ADD);
1808   setTargetDAGCombine(ISD::FADD);
1809   setTargetDAGCombine(ISD::FSUB);
1810   setTargetDAGCombine(ISD::FNEG);
1811   setTargetDAGCombine(ISD::FMA);
1812   setTargetDAGCombine(ISD::FMINNUM);
1813   setTargetDAGCombine(ISD::FMAXNUM);
1814   setTargetDAGCombine(ISD::SUB);
1815   setTargetDAGCombine(ISD::LOAD);
1816   setTargetDAGCombine(ISD::MLOAD);
1817   setTargetDAGCombine(ISD::STORE);
1818   setTargetDAGCombine(ISD::MSTORE);
1819   setTargetDAGCombine(ISD::TRUNCATE);
1820   setTargetDAGCombine(ISD::ZERO_EXTEND);
1821   setTargetDAGCombine(ISD::ANY_EXTEND);
1822   setTargetDAGCombine(ISD::SIGN_EXTEND);
1823   setTargetDAGCombine(ISD::SIGN_EXTEND_INREG);
1824   setTargetDAGCombine(ISD::SINT_TO_FP);
1825   setTargetDAGCombine(ISD::UINT_TO_FP);
1826   setTargetDAGCombine(ISD::SETCC);
1827   setTargetDAGCombine(ISD::BUILD_VECTOR);
1828   setTargetDAGCombine(ISD::MUL);
1829   setTargetDAGCombine(ISD::XOR);
1830   setTargetDAGCombine(ISD::MSCATTER);
1831   setTargetDAGCombine(ISD::MGATHER);
1832
1833   computeRegisterProperties(Subtarget->getRegisterInfo());
1834
1835   MaxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
1836   MaxStoresPerMemsetOptSize = 8;
1837   MaxStoresPerMemcpy = 8; // For @llvm.memcpy -> sequence of stores
1838   MaxStoresPerMemcpyOptSize = 4;
1839   MaxStoresPerMemmove = 8; // For @llvm.memmove -> sequence of stores
1840   MaxStoresPerMemmoveOptSize = 4;
1841   setPrefLoopAlignment(4); // 2^4 bytes.
1842
1843   // A predictable cmov does not hurt on an in-order CPU.
1844   // FIXME: Use a CPU attribute to trigger this, not a CPU model.
1845   PredictableSelectIsExpensive = !Subtarget->isAtom();
1846   EnableExtLdPromotion = true;
1847   setPrefFunctionAlignment(4); // 2^4 bytes.
1848
1849   verifyIntrinsicTables();
1850 }
1851
1852 // This has so far only been implemented for 64-bit MachO.
1853 bool X86TargetLowering::useLoadStackGuardNode() const {
1854   return Subtarget->isTargetMachO() && Subtarget->is64Bit();
1855 }
1856
1857 TargetLoweringBase::LegalizeTypeAction
1858 X86TargetLowering::getPreferredVectorAction(EVT VT) const {
1859   if (ExperimentalVectorWideningLegalization &&
1860       VT.getVectorNumElements() != 1 &&
1861       VT.getVectorElementType().getSimpleVT() != MVT::i1)
1862     return TypeWidenVector;
1863
1864   return TargetLoweringBase::getPreferredVectorAction(VT);
1865 }
1866
1867 EVT X86TargetLowering::getSetCCResultType(const DataLayout &DL, LLVMContext &,
1868                                           EVT VT) const {
1869   if (!VT.isVector())
1870     return Subtarget->hasAVX512() ? MVT::i1: MVT::i8;
1871
1872   if (VT.isSimple()) {
1873     MVT VVT = VT.getSimpleVT();
1874     const unsigned NumElts = VVT.getVectorNumElements();
1875     const MVT EltVT = VVT.getVectorElementType();
1876     if (VVT.is512BitVector()) {
1877       if (Subtarget->hasAVX512())
1878         if (EltVT == MVT::i32 || EltVT == MVT::i64 ||
1879             EltVT == MVT::f32 || EltVT == MVT::f64)
1880           switch(NumElts) {
1881           case  8: return MVT::v8i1;
1882           case 16: return MVT::v16i1;
1883         }
1884       if (Subtarget->hasBWI())
1885         if (EltVT == MVT::i8 || EltVT == MVT::i16)
1886           switch(NumElts) {
1887           case 32: return MVT::v32i1;
1888           case 64: return MVT::v64i1;
1889         }
1890     }
1891
1892     if (VVT.is256BitVector() || VVT.is128BitVector()) {
1893       if (Subtarget->hasVLX())
1894         if (EltVT == MVT::i32 || EltVT == MVT::i64 ||
1895             EltVT == MVT::f32 || EltVT == MVT::f64)
1896           switch(NumElts) {
1897           case 2: return MVT::v2i1;
1898           case 4: return MVT::v4i1;
1899           case 8: return MVT::v8i1;
1900         }
1901       if (Subtarget->hasBWI() && Subtarget->hasVLX())
1902         if (EltVT == MVT::i8 || EltVT == MVT::i16)
1903           switch(NumElts) {
1904           case  8: return MVT::v8i1;
1905           case 16: return MVT::v16i1;
1906           case 32: return MVT::v32i1;
1907         }
1908     }
1909   }
1910
1911   return VT.changeVectorElementTypeToInteger();
1912 }
1913
1914 /// Helper for getByValTypeAlignment to determine
1915 /// the desired ByVal argument alignment.
1916 static void getMaxByValAlign(Type *Ty, unsigned &MaxAlign) {
1917   if (MaxAlign == 16)
1918     return;
1919   if (VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1920     if (VTy->getBitWidth() == 128)
1921       MaxAlign = 16;
1922   } else if (ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
1923     unsigned EltAlign = 0;
1924     getMaxByValAlign(ATy->getElementType(), EltAlign);
1925     if (EltAlign > MaxAlign)
1926       MaxAlign = EltAlign;
1927   } else if (StructType *STy = dyn_cast<StructType>(Ty)) {
1928     for (auto *EltTy : STy->elements()) {
1929       unsigned EltAlign = 0;
1930       getMaxByValAlign(EltTy, EltAlign);
1931       if (EltAlign > MaxAlign)
1932         MaxAlign = EltAlign;
1933       if (MaxAlign == 16)
1934         break;
1935     }
1936   }
1937 }
1938
1939 /// Return the desired alignment for ByVal aggregate
1940 /// function arguments in the caller parameter area. For X86, aggregates
1941 /// that contain SSE vectors are placed at 16-byte boundaries while the rest
1942 /// are at 4-byte boundaries.
1943 unsigned X86TargetLowering::getByValTypeAlignment(Type *Ty,
1944                                                   const DataLayout &DL) const {
1945   if (Subtarget->is64Bit()) {
1946     // Max of 8 and alignment of type.
1947     unsigned TyAlign = DL.getABITypeAlignment(Ty);
1948     if (TyAlign > 8)
1949       return TyAlign;
1950     return 8;
1951   }
1952
1953   unsigned Align = 4;
1954   if (Subtarget->hasSSE1())
1955     getMaxByValAlign(Ty, Align);
1956   return Align;
1957 }
1958
1959 /// Returns the target specific optimal type for load
1960 /// and store operations as a result of memset, memcpy, and memmove
1961 /// lowering. If DstAlign is zero that means it's safe to destination
1962 /// alignment can satisfy any constraint. Similarly if SrcAlign is zero it
1963 /// means there isn't a need to check it against alignment requirement,
1964 /// probably because the source does not need to be loaded. If 'IsMemset' is
1965 /// true, that means it's expanding a memset. If 'ZeroMemset' is true, that
1966 /// means it's a memset of zero. 'MemcpyStrSrc' indicates whether the memcpy
1967 /// source is constant so it does not need to be loaded.
1968 /// It returns EVT::Other if the type should be determined using generic
1969 /// target-independent logic.
1970 EVT
1971 X86TargetLowering::getOptimalMemOpType(uint64_t Size,
1972                                        unsigned DstAlign, unsigned SrcAlign,
1973                                        bool IsMemset, bool ZeroMemset,
1974                                        bool MemcpyStrSrc,
1975                                        MachineFunction &MF) const {
1976   const Function *F = MF.getFunction();
1977   if ((!IsMemset || ZeroMemset) &&
1978       !F->hasFnAttribute(Attribute::NoImplicitFloat)) {
1979     if (Size >= 16 &&
1980         (!Subtarget->isUnalignedMem16Slow() ||
1981          ((DstAlign == 0 || DstAlign >= 16) &&
1982           (SrcAlign == 0 || SrcAlign >= 16)))) {
1983       if (Size >= 32) {
1984         // FIXME: Check if unaligned 32-byte accesses are slow.
1985         if (Subtarget->hasInt256())
1986           return MVT::v8i32;
1987         if (Subtarget->hasFp256())
1988           return MVT::v8f32;
1989       }
1990       if (Subtarget->hasSSE2())
1991         return MVT::v4i32;
1992       if (Subtarget->hasSSE1())
1993         return MVT::v4f32;
1994     } else if (!MemcpyStrSrc && Size >= 8 &&
1995                !Subtarget->is64Bit() &&
1996                Subtarget->hasSSE2()) {
1997       // Do not use f64 to lower memcpy if source is string constant. It's
1998       // better to use i32 to avoid the loads.
1999       return MVT::f64;
2000     }
2001   }
2002   // This is a compromise. If we reach here, unaligned accesses may be slow on
2003   // this target. However, creating smaller, aligned accesses could be even
2004   // slower and would certainly be a lot more code.
2005   if (Subtarget->is64Bit() && Size >= 8)
2006     return MVT::i64;
2007   return MVT::i32;
2008 }
2009
2010 bool X86TargetLowering::isSafeMemOpType(MVT VT) const {
2011   if (VT == MVT::f32)
2012     return X86ScalarSSEf32;
2013   else if (VT == MVT::f64)
2014     return X86ScalarSSEf64;
2015   return true;
2016 }
2017
2018 bool
2019 X86TargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
2020                                                   unsigned,
2021                                                   unsigned,
2022                                                   bool *Fast) const {
2023   if (Fast) {
2024     switch (VT.getSizeInBits()) {
2025     default:
2026       // 8-byte and under are always assumed to be fast.
2027       *Fast = true;
2028       break;
2029     case 128:
2030       *Fast = !Subtarget->isUnalignedMem16Slow();
2031       break;
2032     case 256:
2033       *Fast = !Subtarget->isUnalignedMem32Slow();
2034       break;
2035     // TODO: What about AVX-512 (512-bit) accesses?
2036     }
2037   }
2038   // Misaligned accesses of any size are always allowed.
2039   return true;
2040 }
2041
2042 /// Return the entry encoding for a jump table in the
2043 /// current function.  The returned value is a member of the
2044 /// MachineJumpTableInfo::JTEntryKind enum.
2045 unsigned X86TargetLowering::getJumpTableEncoding() const {
2046   // In GOT pic mode, each entry in the jump table is emitted as a @GOTOFF
2047   // symbol.
2048   if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
2049       Subtarget->isPICStyleGOT())
2050     return MachineJumpTableInfo::EK_Custom32;
2051
2052   // Otherwise, use the normal jump table encoding heuristics.
2053   return TargetLowering::getJumpTableEncoding();
2054 }
2055
2056 bool X86TargetLowering::useSoftFloat() const {
2057   return Subtarget->useSoftFloat();
2058 }
2059
2060 const MCExpr *
2061 X86TargetLowering::LowerCustomJumpTableEntry(const MachineJumpTableInfo *MJTI,
2062                                              const MachineBasicBlock *MBB,
2063                                              unsigned uid,MCContext &Ctx) const{
2064   assert(MBB->getParent()->getTarget().getRelocationModel() == Reloc::PIC_ &&
2065          Subtarget->isPICStyleGOT());
2066   // In 32-bit ELF systems, our jump table entries are formed with @GOTOFF
2067   // entries.
2068   return MCSymbolRefExpr::create(MBB->getSymbol(),
2069                                  MCSymbolRefExpr::VK_GOTOFF, Ctx);
2070 }
2071
2072 /// Returns relocation base for the given PIC jumptable.
2073 SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
2074                                                     SelectionDAG &DAG) const {
2075   if (!Subtarget->is64Bit())
2076     // This doesn't have SDLoc associated with it, but is not really the
2077     // same as a Register.
2078     return DAG.getNode(X86ISD::GlobalBaseReg, SDLoc(),
2079                        getPointerTy(DAG.getDataLayout()));
2080   return Table;
2081 }
2082
2083 /// This returns the relocation base for the given PIC jumptable,
2084 /// the same as getPICJumpTableRelocBase, but as an MCExpr.
2085 const MCExpr *X86TargetLowering::
2086 getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
2087                              MCContext &Ctx) const {
2088   // X86-64 uses RIP relative addressing based on the jump table label.
2089   if (Subtarget->isPICStyleRIPRel())
2090     return TargetLowering::getPICJumpTableRelocBaseExpr(MF, JTI, Ctx);
2091
2092   // Otherwise, the reference is relative to the PIC base.
2093   return MCSymbolRefExpr::create(MF->getPICBaseSymbol(), Ctx);
2094 }
2095
2096 std::pair<const TargetRegisterClass *, uint8_t>
2097 X86TargetLowering::findRepresentativeClass(const TargetRegisterInfo *TRI,
2098                                            MVT VT) const {
2099   const TargetRegisterClass *RRC = nullptr;
2100   uint8_t Cost = 1;
2101   switch (VT.SimpleTy) {
2102   default:
2103     return TargetLowering::findRepresentativeClass(TRI, VT);
2104   case MVT::i8: case MVT::i16: case MVT::i32: case MVT::i64:
2105     RRC = Subtarget->is64Bit() ? &X86::GR64RegClass : &X86::GR32RegClass;
2106     break;
2107   case MVT::x86mmx:
2108     RRC = &X86::VR64RegClass;
2109     break;
2110   case MVT::f32: case MVT::f64:
2111   case MVT::v16i8: case MVT::v8i16: case MVT::v4i32: case MVT::v2i64:
2112   case MVT::v4f32: case MVT::v2f64:
2113   case MVT::v32i8: case MVT::v8i32: case MVT::v4i64: case MVT::v8f32:
2114   case MVT::v4f64:
2115     RRC = &X86::VR128RegClass;
2116     break;
2117   }
2118   return std::make_pair(RRC, Cost);
2119 }
2120
2121 bool X86TargetLowering::getStackCookieLocation(unsigned &AddressSpace,
2122                                                unsigned &Offset) const {
2123   if (!Subtarget->isTargetLinux())
2124     return false;
2125
2126   if (Subtarget->is64Bit()) {
2127     // %fs:0x28, unless we're using a Kernel code model, in which case it's %gs:
2128     Offset = 0x28;
2129     if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
2130       AddressSpace = 256;
2131     else
2132       AddressSpace = 257;
2133   } else {
2134     // %gs:0x14 on i386
2135     Offset = 0x14;
2136     AddressSpace = 256;
2137   }
2138   return true;
2139 }
2140
2141 Value *X86TargetLowering::getSafeStackPointerLocation(IRBuilder<> &IRB) const {
2142   if (!Subtarget->isTargetAndroid())
2143     return TargetLowering::getSafeStackPointerLocation(IRB);
2144
2145   // Android provides a fixed TLS slot for the SafeStack pointer. See the
2146   // definition of TLS_SLOT_SAFESTACK in
2147   // https://android.googlesource.com/platform/bionic/+/master/libc/private/bionic_tls.h
2148   unsigned AddressSpace, Offset;
2149   if (Subtarget->is64Bit()) {
2150     // %fs:0x48, unless we're using a Kernel code model, in which case it's %gs:
2151     Offset = 0x48;
2152     if (getTargetMachine().getCodeModel() == CodeModel::Kernel)
2153       AddressSpace = 256;
2154     else
2155       AddressSpace = 257;
2156   } else {
2157     // %gs:0x24 on i386
2158     Offset = 0x24;
2159     AddressSpace = 256;
2160   }
2161
2162   return ConstantExpr::getIntToPtr(
2163       ConstantInt::get(Type::getInt32Ty(IRB.getContext()), Offset),
2164       Type::getInt8PtrTy(IRB.getContext())->getPointerTo(AddressSpace));
2165 }
2166
2167 bool X86TargetLowering::isNoopAddrSpaceCast(unsigned SrcAS,
2168                                             unsigned DestAS) const {
2169   assert(SrcAS != DestAS && "Expected different address spaces!");
2170
2171   return SrcAS < 256 && DestAS < 256;
2172 }
2173
2174 //===----------------------------------------------------------------------===//
2175 //               Return Value Calling Convention Implementation
2176 //===----------------------------------------------------------------------===//
2177
2178 #include "X86GenCallingConv.inc"
2179
2180 bool X86TargetLowering::CanLowerReturn(
2181     CallingConv::ID CallConv, MachineFunction &MF, bool isVarArg,
2182     const SmallVectorImpl<ISD::OutputArg> &Outs, LLVMContext &Context) const {
2183   SmallVector<CCValAssign, 16> RVLocs;
2184   CCState CCInfo(CallConv, isVarArg, MF, RVLocs, Context);
2185   return CCInfo.CheckReturn(Outs, RetCC_X86);
2186 }
2187
2188 const MCPhysReg *X86TargetLowering::getScratchRegisters(CallingConv::ID) const {
2189   static const MCPhysReg ScratchRegs[] = { X86::R11, 0 };
2190   return ScratchRegs;
2191 }
2192
2193 SDValue
2194 X86TargetLowering::LowerReturn(SDValue Chain,
2195                                CallingConv::ID CallConv, bool isVarArg,
2196                                const SmallVectorImpl<ISD::OutputArg> &Outs,
2197                                const SmallVectorImpl<SDValue> &OutVals,
2198                                SDLoc dl, SelectionDAG &DAG) const {
2199   MachineFunction &MF = DAG.getMachineFunction();
2200   X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2201
2202   if (CallConv == CallingConv::X86_INTR && !Outs.empty())
2203     report_fatal_error("X86 interrupts may not return any value");
2204
2205   SmallVector<CCValAssign, 16> RVLocs;
2206   CCState CCInfo(CallConv, isVarArg, MF, RVLocs, *DAG.getContext());
2207   CCInfo.AnalyzeReturn(Outs, RetCC_X86);
2208
2209   SDValue Flag;
2210   SmallVector<SDValue, 6> RetOps;
2211   RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
2212   // Operand #1 = Bytes To Pop
2213   RetOps.push_back(DAG.getTargetConstant(FuncInfo->getBytesToPopOnReturn(), dl,
2214                    MVT::i16));
2215
2216   // Copy the result values into the output registers.
2217   for (unsigned i = 0; i != RVLocs.size(); ++i) {
2218     CCValAssign &VA = RVLocs[i];
2219     assert(VA.isRegLoc() && "Can only return in registers!");
2220     SDValue ValToCopy = OutVals[i];
2221     EVT ValVT = ValToCopy.getValueType();
2222
2223     // Promote values to the appropriate types.
2224     if (VA.getLocInfo() == CCValAssign::SExt)
2225       ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
2226     else if (VA.getLocInfo() == CCValAssign::ZExt)
2227       ValToCopy = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), ValToCopy);
2228     else if (VA.getLocInfo() == CCValAssign::AExt) {
2229       if (ValVT.isVector() && ValVT.getVectorElementType() == MVT::i1)
2230         ValToCopy = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), ValToCopy);
2231       else
2232         ValToCopy = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), ValToCopy);
2233     }
2234     else if (VA.getLocInfo() == CCValAssign::BCvt)
2235       ValToCopy = DAG.getBitcast(VA.getLocVT(), ValToCopy);
2236
2237     assert(VA.getLocInfo() != CCValAssign::FPExt &&
2238            "Unexpected FP-extend for return value.");
2239
2240     // If this is x86-64, and we disabled SSE, we can't return FP values,
2241     // or SSE or MMX vectors.
2242     if ((ValVT == MVT::f32 || ValVT == MVT::f64 ||
2243          VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) &&
2244           (Subtarget->is64Bit() && !Subtarget->hasSSE1())) {
2245       report_fatal_error("SSE register return with SSE disabled");
2246     }
2247     // Likewise we can't return F64 values with SSE1 only.  gcc does so, but
2248     // llvm-gcc has never done it right and no one has noticed, so this
2249     // should be OK for now.
2250     if (ValVT == MVT::f64 &&
2251         (Subtarget->is64Bit() && !Subtarget->hasSSE2()))
2252       report_fatal_error("SSE2 register return with SSE2 disabled");
2253
2254     // Returns in ST0/ST1 are handled specially: these are pushed as operands to
2255     // the RET instruction and handled by the FP Stackifier.
2256     if (VA.getLocReg() == X86::FP0 ||
2257         VA.getLocReg() == X86::FP1) {
2258       // If this is a copy from an xmm register to ST(0), use an FPExtend to
2259       // change the value to the FP stack register class.
2260       if (isScalarFPTypeInSSEReg(VA.getValVT()))
2261         ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
2262       RetOps.push_back(ValToCopy);
2263       // Don't emit a copytoreg.
2264       continue;
2265     }
2266
2267     // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
2268     // which is returned in RAX / RDX.
2269     if (Subtarget->is64Bit()) {
2270       if (ValVT == MVT::x86mmx) {
2271         if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
2272           ValToCopy = DAG.getBitcast(MVT::i64, ValToCopy);
2273           ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
2274                                   ValToCopy);
2275           // If we don't have SSE2 available, convert to v4f32 so the generated
2276           // register is legal.
2277           if (!Subtarget->hasSSE2())
2278             ValToCopy = DAG.getBitcast(MVT::v4f32, ValToCopy);
2279         }
2280       }
2281     }
2282
2283     Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
2284     Flag = Chain.getValue(1);
2285     RetOps.push_back(DAG.getRegister(VA.getLocReg(), VA.getLocVT()));
2286   }
2287
2288   // All x86 ABIs require that for returning structs by value we copy
2289   // the sret argument into %rax/%eax (depending on ABI) for the return.
2290   // We saved the argument into a virtual register in the entry block,
2291   // so now we copy the value out and into %rax/%eax.
2292   //
2293   // Checking Function.hasStructRetAttr() here is insufficient because the IR
2294   // may not have an explicit sret argument. If FuncInfo.CanLowerReturn is
2295   // false, then an sret argument may be implicitly inserted in the SelDAG. In
2296   // either case FuncInfo->setSRetReturnReg() will have been called.
2297   if (unsigned SRetReg = FuncInfo->getSRetReturnReg()) {
2298     SDValue Val = DAG.getCopyFromReg(Chain, dl, SRetReg,
2299                                      getPointerTy(MF.getDataLayout()));
2300
2301     unsigned RetValReg
2302         = (Subtarget->is64Bit() && !Subtarget->isTarget64BitILP32()) ?
2303           X86::RAX : X86::EAX;
2304     Chain = DAG.getCopyToReg(Chain, dl, RetValReg, Val, Flag);
2305     Flag = Chain.getValue(1);
2306
2307     // RAX/EAX now acts like a return value.
2308     RetOps.push_back(
2309         DAG.getRegister(RetValReg, getPointerTy(DAG.getDataLayout())));
2310   }
2311
2312   RetOps[0] = Chain;  // Update chain.
2313
2314   // Add the flag if we have it.
2315   if (Flag.getNode())
2316     RetOps.push_back(Flag);
2317
2318   X86ISD::NodeType opcode = X86ISD::RET_FLAG;
2319   if (CallConv == CallingConv::X86_INTR)
2320     opcode = X86ISD::IRET;
2321   return DAG.getNode(opcode, dl, MVT::Other, RetOps);
2322 }
2323
2324 bool X86TargetLowering::isUsedByReturnOnly(SDNode *N, SDValue &Chain) const {
2325   if (N->getNumValues() != 1)
2326     return false;
2327   if (!N->hasNUsesOfValue(1, 0))
2328     return false;
2329
2330   SDValue TCChain = Chain;
2331   SDNode *Copy = *N->use_begin();
2332   if (Copy->getOpcode() == ISD::CopyToReg) {
2333     // If the copy has a glue operand, we conservatively assume it isn't safe to
2334     // perform a tail call.
2335     if (Copy->getOperand(Copy->getNumOperands()-1).getValueType() == MVT::Glue)
2336       return false;
2337     TCChain = Copy->getOperand(0);
2338   } else if (Copy->getOpcode() != ISD::FP_EXTEND)
2339     return false;
2340
2341   bool HasRet = false;
2342   for (SDNode::use_iterator UI = Copy->use_begin(), UE = Copy->use_end();
2343        UI != UE; ++UI) {
2344     if (UI->getOpcode() != X86ISD::RET_FLAG)
2345       return false;
2346     // If we are returning more than one value, we can definitely
2347     // not make a tail call see PR19530
2348     if (UI->getNumOperands() > 4)
2349       return false;
2350     if (UI->getNumOperands() == 4 &&
2351         UI->getOperand(UI->getNumOperands()-1).getValueType() != MVT::Glue)
2352       return false;
2353     HasRet = true;
2354   }
2355
2356   if (!HasRet)
2357     return false;
2358
2359   Chain = TCChain;
2360   return true;
2361 }
2362
2363 EVT
2364 X86TargetLowering::getTypeForExtArgOrReturn(LLVMContext &Context, EVT VT,
2365                                             ISD::NodeType ExtendKind) const {
2366   MVT ReturnMVT;
2367   // TODO: Is this also valid on 32-bit?
2368   if (Subtarget->is64Bit() && VT == MVT::i1 && ExtendKind == ISD::ZERO_EXTEND)
2369     ReturnMVT = MVT::i8;
2370   else
2371     ReturnMVT = MVT::i32;
2372
2373   EVT MinVT = getRegisterType(Context, ReturnMVT);
2374   return VT.bitsLT(MinVT) ? MinVT : VT;
2375 }
2376
2377 /// Lower the result values of a call into the
2378 /// appropriate copies out of appropriate physical registers.
2379 ///
2380 SDValue
2381 X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
2382                                    CallingConv::ID CallConv, bool isVarArg,
2383                                    const SmallVectorImpl<ISD::InputArg> &Ins,
2384                                    SDLoc dl, SelectionDAG &DAG,
2385                                    SmallVectorImpl<SDValue> &InVals) const {
2386
2387   // Assign locations to each value returned by this call.
2388   SmallVector<CCValAssign, 16> RVLocs;
2389   bool Is64Bit = Subtarget->is64Bit();
2390   CCState CCInfo(CallConv, isVarArg, DAG.getMachineFunction(), RVLocs,
2391                  *DAG.getContext());
2392   CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
2393
2394   // Copy all of the result registers out of their specified physreg.
2395   for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
2396     CCValAssign &VA = RVLocs[i];
2397     EVT CopyVT = VA.getLocVT();
2398
2399     // If this is x86-64, and we disabled SSE, we can't return FP values
2400     if ((CopyVT == MVT::f32 || CopyVT == MVT::f64 || CopyVT == MVT::f128) &&
2401         ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
2402       report_fatal_error("SSE register return with SSE disabled");
2403     }
2404
2405     // If we prefer to use the value in xmm registers, copy it out as f80 and
2406     // use a truncate to move it from fp stack reg to xmm reg.
2407     bool RoundAfterCopy = false;
2408     if ((VA.getLocReg() == X86::FP0 || VA.getLocReg() == X86::FP1) &&
2409         isScalarFPTypeInSSEReg(VA.getValVT())) {
2410       CopyVT = MVT::f80;
2411       RoundAfterCopy = (CopyVT != VA.getLocVT());
2412     }
2413
2414     Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
2415                                CopyVT, InFlag).getValue(1);
2416     SDValue Val = Chain.getValue(0);
2417
2418     if (RoundAfterCopy)
2419       Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
2420                         // This truncation won't change the value.
2421                         DAG.getIntPtrConstant(1, dl));
2422
2423     if (VA.isExtInLoc() && VA.getValVT().getScalarType() == MVT::i1)
2424       Val = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val);
2425
2426     InFlag = Chain.getValue(2);
2427     InVals.push_back(Val);
2428   }
2429
2430   return Chain;
2431 }
2432
2433 //===----------------------------------------------------------------------===//
2434 //                C & StdCall & Fast Calling Convention implementation
2435 //===----------------------------------------------------------------------===//
2436 //  StdCall calling convention seems to be standard for many Windows' API
2437 //  routines and around. It differs from C calling convention just a little:
2438 //  callee should clean up the stack, not caller. Symbols should be also
2439 //  decorated in some fancy way :) It doesn't support any vector arguments.
2440 //  For info on fast calling convention see Fast Calling Convention (tail call)
2441 //  implementation LowerX86_32FastCCCallTo.
2442
2443 /// CallIsStructReturn - Determines whether a call uses struct return
2444 /// semantics.
2445 enum StructReturnType {
2446   NotStructReturn,
2447   RegStructReturn,
2448   StackStructReturn
2449 };
2450 static StructReturnType
2451 callIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs, bool IsMCU) {
2452   if (Outs.empty())
2453     return NotStructReturn;
2454
2455   const ISD::ArgFlagsTy &Flags = Outs[0].Flags;
2456   if (!Flags.isSRet())
2457     return NotStructReturn;
2458   if (Flags.isInReg() || IsMCU)
2459     return RegStructReturn;
2460   return StackStructReturn;
2461 }
2462
2463 /// Determines whether a function uses struct return semantics.
2464 static StructReturnType
2465 argsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins, bool IsMCU) {
2466   if (Ins.empty())
2467     return NotStructReturn;
2468
2469   const ISD::ArgFlagsTy &Flags = Ins[0].Flags;
2470   if (!Flags.isSRet())
2471     return NotStructReturn;
2472   if (Flags.isInReg() || IsMCU)
2473     return RegStructReturn;
2474   return StackStructReturn;
2475 }
2476
2477 /// Make a copy of an aggregate at address specified by "Src" to address
2478 /// "Dst" with size and alignment information specified by the specific
2479 /// parameter attribute. The copy will be passed as a byval function parameter.
2480 static SDValue
2481 CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
2482                           ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
2483                           SDLoc dl) {
2484   SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), dl, MVT::i32);
2485
2486   return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
2487                        /*isVolatile*/false, /*AlwaysInline=*/true,
2488                        /*isTailCall*/false,
2489                        MachinePointerInfo(), MachinePointerInfo());
2490 }
2491
2492 /// Return true if the calling convention is one that we can guarantee TCO for.
2493 static bool canGuaranteeTCO(CallingConv::ID CC) {
2494   return (CC == CallingConv::Fast || CC == CallingConv::GHC ||
2495           CC == CallingConv::HiPE || CC == CallingConv::HHVM);
2496 }
2497
2498 /// Return true if we might ever do TCO for calls with this calling convention.
2499 static bool mayTailCallThisCC(CallingConv::ID CC) {
2500   switch (CC) {
2501   // C calling conventions:
2502   case CallingConv::C:
2503   case CallingConv::X86_64_Win64:
2504   case CallingConv::X86_64_SysV:
2505   // Callee pop conventions:
2506   case CallingConv::X86_ThisCall:
2507   case CallingConv::X86_StdCall:
2508   case CallingConv::X86_VectorCall:
2509   case CallingConv::X86_FastCall:
2510     return true;
2511   default:
2512     return canGuaranteeTCO(CC);
2513   }
2514 }
2515
2516 /// Return true if the function is being made into a tailcall target by
2517 /// changing its ABI.
2518 static bool shouldGuaranteeTCO(CallingConv::ID CC, bool GuaranteedTailCallOpt) {
2519   return GuaranteedTailCallOpt && canGuaranteeTCO(CC);
2520 }
2521
2522 bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
2523   auto Attr =
2524       CI->getParent()->getParent()->getFnAttribute("disable-tail-calls");
2525   if (!CI->isTailCall() || Attr.getValueAsString() == "true")
2526     return false;
2527
2528   CallSite CS(CI);
2529   CallingConv::ID CalleeCC = CS.getCallingConv();
2530   if (!mayTailCallThisCC(CalleeCC))
2531     return false;
2532
2533   return true;
2534 }
2535
2536 SDValue
2537 X86TargetLowering::LowerMemArgument(SDValue Chain,
2538                                     CallingConv::ID CallConv,
2539                                     const SmallVectorImpl<ISD::InputArg> &Ins,
2540                                     SDLoc dl, SelectionDAG &DAG,
2541                                     const CCValAssign &VA,
2542                                     MachineFrameInfo *MFI,
2543                                     unsigned i) const {
2544   // Create the nodes corresponding to a load from this parameter slot.
2545   ISD::ArgFlagsTy Flags = Ins[i].Flags;
2546   bool AlwaysUseMutable = shouldGuaranteeTCO(
2547       CallConv, DAG.getTarget().Options.GuaranteedTailCallOpt);
2548   bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
2549   EVT ValVT;
2550
2551   // If value is passed by pointer we have address passed instead of the value
2552   // itself.
2553   bool ExtendedInMem = VA.isExtInLoc() &&
2554     VA.getValVT().getScalarType() == MVT::i1;
2555
2556   if (VA.getLocInfo() == CCValAssign::Indirect || ExtendedInMem)
2557     ValVT = VA.getLocVT();
2558   else
2559     ValVT = VA.getValVT();
2560
2561   // Calculate SP offset of interrupt parameter, re-arrange the slot normally
2562   // taken by a return address.
2563   int Offset = 0;
2564   if (CallConv == CallingConv::X86_INTR) {
2565     const X86Subtarget& Subtarget =
2566         static_cast<const X86Subtarget&>(DAG.getSubtarget());
2567     // X86 interrupts may take one or two arguments.
2568     // On the stack there will be no return address as in regular call.
2569     // Offset of last argument need to be set to -4/-8 bytes.
2570     // Where offset of the first argument out of two, should be set to 0 bytes.
2571     Offset = (Subtarget.is64Bit() ? 8 : 4) * ((i + 1) % Ins.size() - 1);
2572   }
2573
2574   // FIXME: For now, all byval parameter objects are marked mutable. This can be
2575   // changed with more analysis.
2576   // In case of tail call optimization mark all arguments mutable. Since they
2577   // could be overwritten by lowering of arguments in case of a tail call.
2578   if (Flags.isByVal()) {
2579     unsigned Bytes = Flags.getByValSize();
2580     if (Bytes == 0) Bytes = 1; // Don't create zero-sized stack objects.
2581     int FI = MFI->CreateFixedObject(Bytes, VA.getLocMemOffset(), isImmutable);
2582     // Adjust SP offset of interrupt parameter.
2583     if (CallConv == CallingConv::X86_INTR) {
2584       MFI->setObjectOffset(FI, Offset);
2585     }
2586     return DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
2587   } else {
2588     int FI = MFI->CreateFixedObject(ValVT.getSizeInBits()/8,
2589                                     VA.getLocMemOffset(), isImmutable);
2590     // Adjust SP offset of interrupt parameter.
2591     if (CallConv == CallingConv::X86_INTR) {
2592       MFI->setObjectOffset(FI, Offset);
2593     }
2594
2595     SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
2596     SDValue Val = DAG.getLoad(
2597         ValVT, dl, Chain, FIN,
2598         MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI), false,
2599         false, false, 0);
2600     return ExtendedInMem ?
2601       DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), Val) : Val;
2602   }
2603 }
2604
2605 // FIXME: Get this from tablegen.
2606 static ArrayRef<MCPhysReg> get64BitArgumentGPRs(CallingConv::ID CallConv,
2607                                                 const X86Subtarget *Subtarget) {
2608   assert(Subtarget->is64Bit());
2609
2610   if (Subtarget->isCallingConvWin64(CallConv)) {
2611     static const MCPhysReg GPR64ArgRegsWin64[] = {
2612       X86::RCX, X86::RDX, X86::R8,  X86::R9
2613     };
2614     return makeArrayRef(std::begin(GPR64ArgRegsWin64), std::end(GPR64ArgRegsWin64));
2615   }
2616
2617   static const MCPhysReg GPR64ArgRegs64Bit[] = {
2618     X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
2619   };
2620   return makeArrayRef(std::begin(GPR64ArgRegs64Bit), std::end(GPR64ArgRegs64Bit));
2621 }
2622
2623 // FIXME: Get this from tablegen.
2624 static ArrayRef<MCPhysReg> get64BitArgumentXMMs(MachineFunction &MF,
2625                                                 CallingConv::ID CallConv,
2626                                                 const X86Subtarget *Subtarget) {
2627   assert(Subtarget->is64Bit());
2628   if (Subtarget->isCallingConvWin64(CallConv)) {
2629     // The XMM registers which might contain var arg parameters are shadowed
2630     // in their paired GPR.  So we only need to save the GPR to their home
2631     // slots.
2632     // TODO: __vectorcall will change this.
2633     return None;
2634   }
2635
2636   const Function *Fn = MF.getFunction();
2637   bool NoImplicitFloatOps = Fn->hasFnAttribute(Attribute::NoImplicitFloat);
2638   bool isSoftFloat = Subtarget->useSoftFloat();
2639   assert(!(isSoftFloat && NoImplicitFloatOps) &&
2640          "SSE register cannot be used when SSE is disabled!");
2641   if (isSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
2642     // Kernel mode asks for SSE to be disabled, so there are no XMM argument
2643     // registers.
2644     return None;
2645
2646   static const MCPhysReg XMMArgRegs64Bit[] = {
2647     X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2648     X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2649   };
2650   return makeArrayRef(std::begin(XMMArgRegs64Bit), std::end(XMMArgRegs64Bit));
2651 }
2652
2653 SDValue X86TargetLowering::LowerFormalArguments(
2654     SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
2655     const SmallVectorImpl<ISD::InputArg> &Ins, SDLoc dl, SelectionDAG &DAG,
2656     SmallVectorImpl<SDValue> &InVals) const {
2657   MachineFunction &MF = DAG.getMachineFunction();
2658   X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2659   const TargetFrameLowering &TFI = *Subtarget->getFrameLowering();
2660
2661   const Function* Fn = MF.getFunction();
2662   if (Fn->hasExternalLinkage() &&
2663       Subtarget->isTargetCygMing() &&
2664       Fn->getName() == "main")
2665     FuncInfo->setForceFramePointer(true);
2666
2667   MachineFrameInfo *MFI = MF.getFrameInfo();
2668   bool Is64Bit = Subtarget->is64Bit();
2669   bool IsWin64 = Subtarget->isCallingConvWin64(CallConv);
2670
2671   assert(!(isVarArg && canGuaranteeTCO(CallConv)) &&
2672          "Var args not supported with calling convention fastcc, ghc or hipe");
2673
2674   if (CallConv == CallingConv::X86_INTR) {
2675     bool isLegal = Ins.size() == 1 ||
2676                    (Ins.size() == 2 && ((Is64Bit && Ins[1].VT == MVT::i64) ||
2677                                         (!Is64Bit && Ins[1].VT == MVT::i32)));
2678     if (!isLegal)
2679       report_fatal_error("X86 interrupts may take one or two arguments");
2680   }
2681
2682   // Assign locations to all of the incoming arguments.
2683   SmallVector<CCValAssign, 16> ArgLocs;
2684   CCState CCInfo(CallConv, isVarArg, MF, ArgLocs, *DAG.getContext());
2685
2686   // Allocate shadow area for Win64
2687   if (IsWin64)
2688     CCInfo.AllocateStack(32, 8);
2689
2690   CCInfo.AnalyzeFormalArguments(Ins, CC_X86);
2691
2692   unsigned LastVal = ~0U;
2693   SDValue ArgValue;
2694   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2695     CCValAssign &VA = ArgLocs[i];
2696     // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
2697     // places.
2698     assert(VA.getValNo() != LastVal &&
2699            "Don't support value assigned to multiple locs yet");
2700     (void)LastVal;
2701     LastVal = VA.getValNo();
2702
2703     if (VA.isRegLoc()) {
2704       EVT RegVT = VA.getLocVT();
2705       const TargetRegisterClass *RC;
2706       if (RegVT == MVT::i32)
2707         RC = &X86::GR32RegClass;
2708       else if (Is64Bit && RegVT == MVT::i64)
2709         RC = &X86::GR64RegClass;
2710       else if (RegVT == MVT::f32)
2711         RC = &X86::FR32RegClass;
2712       else if (RegVT == MVT::f64)
2713         RC = &X86::FR64RegClass;
2714       else if (RegVT == MVT::f128)
2715         RC = &X86::FR128RegClass;
2716       else if (RegVT.is512BitVector())
2717         RC = &X86::VR512RegClass;
2718       else if (RegVT.is256BitVector())
2719         RC = &X86::VR256RegClass;
2720       else if (RegVT.is128BitVector())
2721         RC = &X86::VR128RegClass;
2722       else if (RegVT == MVT::x86mmx)
2723         RC = &X86::VR64RegClass;
2724       else if (RegVT == MVT::i1)
2725         RC = &X86::VK1RegClass;
2726       else if (RegVT == MVT::v8i1)
2727         RC = &X86::VK8RegClass;
2728       else if (RegVT == MVT::v16i1)
2729         RC = &X86::VK16RegClass;
2730       else if (RegVT == MVT::v32i1)
2731         RC = &X86::VK32RegClass;
2732       else if (RegVT == MVT::v64i1)
2733         RC = &X86::VK64RegClass;
2734       else
2735         llvm_unreachable("Unknown argument type!");
2736
2737       unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
2738       ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
2739
2740       // If this is an 8 or 16-bit value, it is really passed promoted to 32
2741       // bits.  Insert an assert[sz]ext to capture this, then truncate to the
2742       // right size.
2743       if (VA.getLocInfo() == CCValAssign::SExt)
2744         ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
2745                                DAG.getValueType(VA.getValVT()));
2746       else if (VA.getLocInfo() == CCValAssign::ZExt)
2747         ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
2748                                DAG.getValueType(VA.getValVT()));
2749       else if (VA.getLocInfo() == CCValAssign::BCvt)
2750         ArgValue = DAG.getBitcast(VA.getValVT(), ArgValue);
2751
2752       if (VA.isExtInLoc()) {
2753         // Handle MMX values passed in XMM regs.
2754         if (RegVT.isVector() && VA.getValVT().getScalarType() != MVT::i1)
2755           ArgValue = DAG.getNode(X86ISD::MOVDQ2Q, dl, VA.getValVT(), ArgValue);
2756         else
2757           ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
2758       }
2759     } else {
2760       assert(VA.isMemLoc());
2761       ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
2762     }
2763
2764     // If value is passed via pointer - do a load.
2765     if (VA.getLocInfo() == CCValAssign::Indirect)
2766       ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue,
2767                              MachinePointerInfo(), false, false, false, 0);
2768
2769     InVals.push_back(ArgValue);
2770   }
2771
2772   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2773     // All x86 ABIs require that for returning structs by value we copy the
2774     // sret argument into %rax/%eax (depending on ABI) for the return. Save
2775     // the argument into a virtual register so that we can access it from the
2776     // return points.
2777     if (Ins[i].Flags.isSRet()) {
2778       unsigned Reg = FuncInfo->getSRetReturnReg();
2779       if (!Reg) {
2780         MVT PtrTy = getPointerTy(DAG.getDataLayout());
2781         Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(PtrTy));
2782         FuncInfo->setSRetReturnReg(Reg);
2783       }
2784       SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[i]);
2785       Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
2786       break;
2787     }
2788   }
2789
2790   unsigned StackSize = CCInfo.getNextStackOffset();
2791   // Align stack specially for tail calls.
2792   if (shouldGuaranteeTCO(CallConv,
2793                          MF.getTarget().Options.GuaranteedTailCallOpt))
2794     StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
2795
2796   // If the function takes variable number of arguments, make a frame index for
2797   // the start of the first vararg value... for expansion of llvm.va_start. We
2798   // can skip this if there are no va_start calls.
2799   if (MFI->hasVAStart() &&
2800       (Is64Bit || (CallConv != CallingConv::X86_FastCall &&
2801                    CallConv != CallingConv::X86_ThisCall))) {
2802     FuncInfo->setVarArgsFrameIndex(
2803         MFI->CreateFixedObject(1, StackSize, true));
2804   }
2805
2806   // Figure out if XMM registers are in use.
2807   assert(!(Subtarget->useSoftFloat() &&
2808            Fn->hasFnAttribute(Attribute::NoImplicitFloat)) &&
2809          "SSE register cannot be used when SSE is disabled!");
2810
2811   // 64-bit calling conventions support varargs and register parameters, so we
2812   // have to do extra work to spill them in the prologue.
2813   if (Is64Bit && isVarArg && MFI->hasVAStart()) {
2814     // Find the first unallocated argument registers.
2815     ArrayRef<MCPhysReg> ArgGPRs = get64BitArgumentGPRs(CallConv, Subtarget);
2816     ArrayRef<MCPhysReg> ArgXMMs = get64BitArgumentXMMs(MF, CallConv, Subtarget);
2817     unsigned NumIntRegs = CCInfo.getFirstUnallocated(ArgGPRs);
2818     unsigned NumXMMRegs = CCInfo.getFirstUnallocated(ArgXMMs);
2819     assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
2820            "SSE register cannot be used when SSE is disabled!");
2821
2822     // Gather all the live in physical registers.
2823     SmallVector<SDValue, 6> LiveGPRs;
2824     SmallVector<SDValue, 8> LiveXMMRegs;
2825     SDValue ALVal;
2826     for (MCPhysReg Reg : ArgGPRs.slice(NumIntRegs)) {
2827       unsigned GPR = MF.addLiveIn(Reg, &X86::GR64RegClass);
2828       LiveGPRs.push_back(
2829           DAG.getCopyFromReg(Chain, dl, GPR, MVT::i64));
2830     }
2831     if (!ArgXMMs.empty()) {
2832       unsigned AL = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
2833       ALVal = DAG.getCopyFromReg(Chain, dl, AL, MVT::i8);
2834       for (MCPhysReg Reg : ArgXMMs.slice(NumXMMRegs)) {
2835         unsigned XMMReg = MF.addLiveIn(Reg, &X86::VR128RegClass);
2836         LiveXMMRegs.push_back(
2837             DAG.getCopyFromReg(Chain, dl, XMMReg, MVT::v4f32));
2838       }
2839     }
2840
2841     if (IsWin64) {
2842       // Get to the caller-allocated home save location.  Add 8 to account
2843       // for the return address.
2844       int HomeOffset = TFI.getOffsetOfLocalArea() + 8;
2845       FuncInfo->setRegSaveFrameIndex(
2846           MFI->CreateFixedObject(1, NumIntRegs * 8 + HomeOffset, false));
2847       // Fixup to set vararg frame on shadow area (4 x i64).
2848       if (NumIntRegs < 4)
2849         FuncInfo->setVarArgsFrameIndex(FuncInfo->getRegSaveFrameIndex());
2850     } else {
2851       // For X86-64, if there are vararg parameters that are passed via
2852       // registers, then we must store them to their spots on the stack so
2853       // they may be loaded by deferencing the result of va_next.
2854       FuncInfo->setVarArgsGPOffset(NumIntRegs * 8);
2855       FuncInfo->setVarArgsFPOffset(ArgGPRs.size() * 8 + NumXMMRegs * 16);
2856       FuncInfo->setRegSaveFrameIndex(MFI->CreateStackObject(
2857           ArgGPRs.size() * 8 + ArgXMMs.size() * 16, 16, false));
2858     }
2859
2860     // Store the integer parameter registers.
2861     SmallVector<SDValue, 8> MemOps;
2862     SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(),
2863                                       getPointerTy(DAG.getDataLayout()));
2864     unsigned Offset = FuncInfo->getVarArgsGPOffset();
2865     for (SDValue Val : LiveGPRs) {
2866       SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(DAG.getDataLayout()),
2867                                 RSFIN, DAG.getIntPtrConstant(Offset, dl));
2868       SDValue Store =
2869           DAG.getStore(Val.getValue(1), dl, Val, FIN,
2870                        MachinePointerInfo::getFixedStack(
2871                            DAG.getMachineFunction(),
2872                            FuncInfo->getRegSaveFrameIndex(), Offset),
2873                        false, false, 0);
2874       MemOps.push_back(Store);
2875       Offset += 8;
2876     }
2877
2878     if (!ArgXMMs.empty() && NumXMMRegs != ArgXMMs.size()) {
2879       // Now store the XMM (fp + vector) parameter registers.
2880       SmallVector<SDValue, 12> SaveXMMOps;
2881       SaveXMMOps.push_back(Chain);
2882       SaveXMMOps.push_back(ALVal);
2883       SaveXMMOps.push_back(DAG.getIntPtrConstant(
2884                              FuncInfo->getRegSaveFrameIndex(), dl));
2885       SaveXMMOps.push_back(DAG.getIntPtrConstant(
2886                              FuncInfo->getVarArgsFPOffset(), dl));
2887       SaveXMMOps.insert(SaveXMMOps.end(), LiveXMMRegs.begin(),
2888                         LiveXMMRegs.end());
2889       MemOps.push_back(DAG.getNode(X86ISD::VASTART_SAVE_XMM_REGS, dl,
2890                                    MVT::Other, SaveXMMOps));
2891     }
2892
2893     if (!MemOps.empty())
2894       Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOps);
2895   }
2896
2897   if (isVarArg && MFI->hasMustTailInVarArgFunc()) {
2898     // Find the largest legal vector type.
2899     MVT VecVT = MVT::Other;
2900     // FIXME: Only some x86_32 calling conventions support AVX512.
2901     if (Subtarget->hasAVX512() &&
2902         (Is64Bit || (CallConv == CallingConv::X86_VectorCall ||
2903                      CallConv == CallingConv::Intel_OCL_BI)))
2904       VecVT = MVT::v16f32;
2905     else if (Subtarget->hasAVX())
2906       VecVT = MVT::v8f32;
2907     else if (Subtarget->hasSSE2())
2908       VecVT = MVT::v4f32;
2909
2910     // We forward some GPRs and some vector types.
2911     SmallVector<MVT, 2> RegParmTypes;
2912     MVT IntVT = Is64Bit ? MVT::i64 : MVT::i32;
2913     RegParmTypes.push_back(IntVT);
2914     if (VecVT != MVT::Other)
2915       RegParmTypes.push_back(VecVT);
2916
2917     // Compute the set of forwarded registers. The rest are scratch.
2918     SmallVectorImpl<ForwardedRegister> &Forwards =
2919         FuncInfo->getForwardedMustTailRegParms();
2920     CCInfo.analyzeMustTailForwardedRegisters(Forwards, RegParmTypes, CC_X86);
2921
2922     // Conservatively forward AL on x86_64, since it might be used for varargs.
2923     if (Is64Bit && !CCInfo.isAllocated(X86::AL)) {
2924       unsigned ALVReg = MF.addLiveIn(X86::AL, &X86::GR8RegClass);
2925       Forwards.push_back(ForwardedRegister(ALVReg, X86::AL, MVT::i8));
2926     }
2927
2928     // Copy all forwards from physical to virtual registers.
2929     for (ForwardedRegister &F : Forwards) {
2930       // FIXME: Can we use a less constrained schedule?
2931       SDValue RegVal = DAG.getCopyFromReg(Chain, dl, F.VReg, F.VT);
2932       F.VReg = MF.getRegInfo().createVirtualRegister(getRegClassFor(F.VT));
2933       Chain = DAG.getCopyToReg(Chain, dl, F.VReg, RegVal);
2934     }
2935   }
2936
2937   // Some CCs need callee pop.
2938   if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
2939                        MF.getTarget().Options.GuaranteedTailCallOpt)) {
2940     FuncInfo->setBytesToPopOnReturn(StackSize); // Callee pops everything.
2941   } else if (CallConv == CallingConv::X86_INTR && Ins.size() == 2) {
2942     // X86 interrupts must pop the error code if present
2943     FuncInfo->setBytesToPopOnReturn(Is64Bit ? 8 : 4);
2944   } else {
2945     FuncInfo->setBytesToPopOnReturn(0); // Callee pops nothing.
2946     // If this is an sret function, the return should pop the hidden pointer.
2947     if (!Is64Bit && !canGuaranteeTCO(CallConv) &&
2948         !Subtarget->getTargetTriple().isOSMSVCRT() &&
2949         argsAreStructReturn(Ins, Subtarget->isTargetMCU()) == StackStructReturn)
2950       FuncInfo->setBytesToPopOnReturn(4);
2951   }
2952
2953   if (!Is64Bit) {
2954     // RegSaveFrameIndex is X86-64 only.
2955     FuncInfo->setRegSaveFrameIndex(0xAAAAAAA);
2956     if (CallConv == CallingConv::X86_FastCall ||
2957         CallConv == CallingConv::X86_ThisCall)
2958       // fastcc functions can't have varargs.
2959       FuncInfo->setVarArgsFrameIndex(0xAAAAAAA);
2960   }
2961
2962   FuncInfo->setArgumentStackSize(StackSize);
2963
2964   if (WinEHFuncInfo *EHInfo = MF.getWinEHFuncInfo()) {
2965     EHPersonality Personality = classifyEHPersonality(Fn->getPersonalityFn());
2966     if (Personality == EHPersonality::CoreCLR) {
2967       assert(Is64Bit);
2968       // TODO: Add a mechanism to frame lowering that will allow us to indicate
2969       // that we'd prefer this slot be allocated towards the bottom of the frame
2970       // (i.e. near the stack pointer after allocating the frame).  Every
2971       // funclet needs a copy of this slot in its (mostly empty) frame, and the
2972       // offset from the bottom of this and each funclet's frame must be the
2973       // same, so the size of funclets' (mostly empty) frames is dictated by
2974       // how far this slot is from the bottom (since they allocate just enough
2975       // space to accomodate holding this slot at the correct offset).
2976       int PSPSymFI = MFI->CreateStackObject(8, 8, /*isSS=*/false);
2977       EHInfo->PSPSymFrameIdx = PSPSymFI;
2978     }
2979   }
2980
2981   return Chain;
2982 }
2983
2984 SDValue
2985 X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
2986                                     SDValue StackPtr, SDValue Arg,
2987                                     SDLoc dl, SelectionDAG &DAG,
2988                                     const CCValAssign &VA,
2989                                     ISD::ArgFlagsTy Flags) const {
2990   unsigned LocMemOffset = VA.getLocMemOffset();
2991   SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset, dl);
2992   PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(DAG.getDataLayout()),
2993                        StackPtr, PtrOff);
2994   if (Flags.isByVal())
2995     return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
2996
2997   return DAG.getStore(
2998       Chain, dl, Arg, PtrOff,
2999       MachinePointerInfo::getStack(DAG.getMachineFunction(), LocMemOffset),
3000       false, false, 0);
3001 }
3002
3003 /// Emit a load of return address if tail call
3004 /// optimization is performed and it is required.
3005 SDValue
3006 X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
3007                                            SDValue &OutRetAddr, SDValue Chain,
3008                                            bool IsTailCall, bool Is64Bit,
3009                                            int FPDiff, SDLoc dl) const {
3010   // Adjust the Return address stack slot.
3011   EVT VT = getPointerTy(DAG.getDataLayout());
3012   OutRetAddr = getReturnAddressFrameIndex(DAG);
3013
3014   // Load the "old" Return address.
3015   OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, MachinePointerInfo(),
3016                            false, false, false, 0);
3017   return SDValue(OutRetAddr.getNode(), 1);
3018 }
3019
3020 /// Emit a store of the return address if tail call
3021 /// optimization is performed and it is required (FPDiff!=0).
3022 static SDValue EmitTailCallStoreRetAddr(SelectionDAG &DAG, MachineFunction &MF,
3023                                         SDValue Chain, SDValue RetAddrFrIdx,
3024                                         EVT PtrVT, unsigned SlotSize,
3025                                         int FPDiff, SDLoc dl) {
3026   // Store the return address to the appropriate stack slot.
3027   if (!FPDiff) return Chain;
3028   // Calculate the new stack slot for the return address.
3029   int NewReturnAddrFI =
3030     MF.getFrameInfo()->CreateFixedObject(SlotSize, (int64_t)FPDiff - SlotSize,
3031                                          false);
3032   SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, PtrVT);
3033   Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
3034                        MachinePointerInfo::getFixedStack(
3035                            DAG.getMachineFunction(), NewReturnAddrFI),
3036                        false, false, 0);
3037   return Chain;
3038 }
3039
3040 /// Returns a vector_shuffle mask for an movs{s|d}, movd
3041 /// operation of specified width.
3042 static SDValue getMOVL(SelectionDAG &DAG, SDLoc dl, MVT VT, SDValue V1,
3043                        SDValue V2) {
3044   unsigned NumElems = VT.getVectorNumElements();
3045   SmallVector<int, 8> Mask;
3046   Mask.push_back(NumElems);
3047   for (unsigned i = 1; i != NumElems; ++i)
3048     Mask.push_back(i);
3049   return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
3050 }
3051
3052 SDValue
3053 X86TargetLowering::LowerCall(TargetLowering::CallLoweringInfo &CLI,
3054                              SmallVectorImpl<SDValue> &InVals) const {
3055   SelectionDAG &DAG                     = CLI.DAG;
3056   SDLoc &dl                             = CLI.DL;
3057   SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
3058   SmallVectorImpl<SDValue> &OutVals     = CLI.OutVals;
3059   SmallVectorImpl<ISD::InputArg> &Ins   = CLI.Ins;
3060   SDValue Chain                         = CLI.Chain;
3061   SDValue Callee                        = CLI.Callee;
3062   CallingConv::ID CallConv              = CLI.CallConv;
3063   bool &isTailCall                      = CLI.IsTailCall;
3064   bool isVarArg                         = CLI.IsVarArg;
3065
3066   MachineFunction &MF = DAG.getMachineFunction();
3067   bool Is64Bit        = Subtarget->is64Bit();
3068   bool IsWin64        = Subtarget->isCallingConvWin64(CallConv);
3069   StructReturnType SR = callIsStructReturn(Outs, Subtarget->isTargetMCU());
3070   bool IsSibcall      = false;
3071   X86MachineFunctionInfo *X86Info = MF.getInfo<X86MachineFunctionInfo>();
3072   auto Attr = MF.getFunction()->getFnAttribute("disable-tail-calls");
3073
3074   if (CallConv == CallingConv::X86_INTR)
3075     report_fatal_error("X86 interrupts may not be called directly");
3076
3077   if (Attr.getValueAsString() == "true")
3078     isTailCall = false;
3079
3080   if (Subtarget->isPICStyleGOT() &&
3081       !MF.getTarget().Options.GuaranteedTailCallOpt) {
3082     // If we are using a GOT, disable tail calls to external symbols with
3083     // default visibility. Tail calling such a symbol requires using a GOT
3084     // relocation, which forces early binding of the symbol. This breaks code
3085     // that require lazy function symbol resolution. Using musttail or
3086     // GuaranteedTailCallOpt will override this.
3087     GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
3088     if (!G || (!G->getGlobal()->hasLocalLinkage() &&
3089                G->getGlobal()->hasDefaultVisibility()))
3090       isTailCall = false;
3091   }
3092
3093   bool IsMustTail = CLI.CS && CLI.CS->isMustTailCall();
3094   if (IsMustTail) {
3095     // Force this to be a tail call.  The verifier rules are enough to ensure
3096     // that we can lower this successfully without moving the return address
3097     // around.
3098     isTailCall = true;
3099   } else if (isTailCall) {
3100     // Check if it's really possible to do a tail call.
3101     isTailCall = IsEligibleForTailCallOptimization(Callee, CallConv,
3102                     isVarArg, SR != NotStructReturn,
3103                     MF.getFunction()->hasStructRetAttr(), CLI.RetTy,
3104                     Outs, OutVals, Ins, DAG);
3105
3106     // Sibcalls are automatically detected tailcalls which do not require
3107     // ABI changes.
3108     if (!MF.getTarget().Options.GuaranteedTailCallOpt && isTailCall)
3109       IsSibcall = true;
3110
3111     if (isTailCall)
3112       ++NumTailCalls;
3113   }
3114
3115   assert(!(isVarArg && canGuaranteeTCO(CallConv)) &&
3116          "Var args not supported with calling convention fastcc, ghc or hipe");
3117
3118   // Analyze operands of the call, assigning locations to each operand.
3119   SmallVector<CCValAssign, 16> ArgLocs;
3120   CCState CCInfo(CallConv, isVarArg, MF, ArgLocs, *DAG.getContext());
3121
3122   // Allocate shadow area for Win64
3123   if (IsWin64)
3124     CCInfo.AllocateStack(32, 8);
3125
3126   CCInfo.AnalyzeCallOperands(Outs, CC_X86);
3127
3128   // Get a count of how many bytes are to be pushed on the stack.
3129   unsigned NumBytes = CCInfo.getAlignedCallFrameSize();
3130   if (IsSibcall)
3131     // This is a sibcall. The memory operands are available in caller's
3132     // own caller's stack.
3133     NumBytes = 0;
3134   else if (MF.getTarget().Options.GuaranteedTailCallOpt &&
3135            canGuaranteeTCO(CallConv))
3136     NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
3137
3138   int FPDiff = 0;
3139   if (isTailCall && !IsSibcall && !IsMustTail) {
3140     // Lower arguments at fp - stackoffset + fpdiff.
3141     unsigned NumBytesCallerPushed = X86Info->getBytesToPopOnReturn();
3142
3143     FPDiff = NumBytesCallerPushed - NumBytes;
3144
3145     // Set the delta of movement of the returnaddr stackslot.
3146     // But only set if delta is greater than previous delta.
3147     if (FPDiff < X86Info->getTCReturnAddrDelta())
3148       X86Info->setTCReturnAddrDelta(FPDiff);
3149   }
3150
3151   unsigned NumBytesToPush = NumBytes;
3152   unsigned NumBytesToPop = NumBytes;
3153
3154   // If we have an inalloca argument, all stack space has already been allocated
3155   // for us and be right at the top of the stack.  We don't support multiple
3156   // arguments passed in memory when using inalloca.
3157   if (!Outs.empty() && Outs.back().Flags.isInAlloca()) {
3158     NumBytesToPush = 0;
3159     if (!ArgLocs.back().isMemLoc())
3160       report_fatal_error("cannot use inalloca attribute on a register "
3161                          "parameter");
3162     if (ArgLocs.back().getLocMemOffset() != 0)
3163       report_fatal_error("any parameter with the inalloca attribute must be "
3164                          "the only memory argument");
3165   }
3166
3167   if (!IsSibcall)
3168     Chain = DAG.getCALLSEQ_START(
3169         Chain, DAG.getIntPtrConstant(NumBytesToPush, dl, true), dl);
3170
3171   SDValue RetAddrFrIdx;
3172   // Load return address for tail calls.
3173   if (isTailCall && FPDiff)
3174     Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall,
3175                                     Is64Bit, FPDiff, dl);
3176
3177   SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
3178   SmallVector<SDValue, 8> MemOpChains;
3179   SDValue StackPtr;
3180
3181   // Walk the register/memloc assignments, inserting copies/loads.  In the case
3182   // of tail call optimization arguments are handle later.
3183   const X86RegisterInfo *RegInfo = Subtarget->getRegisterInfo();
3184   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3185     // Skip inalloca arguments, they have already been written.
3186     ISD::ArgFlagsTy Flags = Outs[i].Flags;
3187     if (Flags.isInAlloca())
3188       continue;
3189
3190     CCValAssign &VA = ArgLocs[i];
3191     EVT RegVT = VA.getLocVT();
3192     SDValue Arg = OutVals[i];
3193     bool isByVal = Flags.isByVal();
3194
3195     // Promote the value if needed.
3196     switch (VA.getLocInfo()) {
3197     default: llvm_unreachable("Unknown loc info!");
3198     case CCValAssign::Full: break;
3199     case CCValAssign::SExt:
3200       Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
3201       break;
3202     case CCValAssign::ZExt:
3203       Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
3204       break;
3205     case CCValAssign::AExt:
3206       if (Arg.getValueType().isVector() &&
3207           Arg.getValueType().getVectorElementType() == MVT::i1)
3208         Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
3209       else if (RegVT.is128BitVector()) {
3210         // Special case: passing MMX values in XMM registers.
3211         Arg = DAG.getBitcast(MVT::i64, Arg);
3212         Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
3213         Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
3214       } else
3215         Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
3216       break;
3217     case CCValAssign::BCvt:
3218       Arg = DAG.getBitcast(RegVT, Arg);
3219       break;
3220     case CCValAssign::Indirect: {
3221       // Store the argument.
3222       SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
3223       int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
3224       Chain = DAG.getStore(
3225           Chain, dl, Arg, SpillSlot,
3226           MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
3227           false, false, 0);
3228       Arg = SpillSlot;
3229       break;
3230     }
3231     }
3232
3233     if (VA.isRegLoc()) {
3234       RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
3235       if (isVarArg && IsWin64) {
3236         // Win64 ABI requires argument XMM reg to be copied to the corresponding
3237         // shadow reg if callee is a varargs function.
3238         unsigned ShadowReg = 0;
3239         switch (VA.getLocReg()) {
3240         case X86::XMM0: ShadowReg = X86::RCX; break;
3241         case X86::XMM1: ShadowReg = X86::RDX; break;
3242         case X86::XMM2: ShadowReg = X86::R8; break;
3243         case X86::XMM3: ShadowReg = X86::R9; break;
3244         }
3245         if (ShadowReg)
3246           RegsToPass.push_back(std::make_pair(ShadowReg, Arg));
3247       }
3248     } else if (!IsSibcall && (!isTailCall || isByVal)) {
3249       assert(VA.isMemLoc());
3250       if (!StackPtr.getNode())
3251         StackPtr = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
3252                                       getPointerTy(DAG.getDataLayout()));
3253       MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
3254                                              dl, DAG, VA, Flags));
3255     }
3256   }
3257
3258   if (!MemOpChains.empty())
3259     Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains);
3260
3261   if (Subtarget->isPICStyleGOT()) {
3262     // ELF / PIC requires GOT in the EBX register before function calls via PLT
3263     // GOT pointer.
3264     if (!isTailCall) {
3265       RegsToPass.push_back(std::make_pair(
3266           unsigned(X86::EBX), DAG.getNode(X86ISD::GlobalBaseReg, SDLoc(),
3267                                           getPointerTy(DAG.getDataLayout()))));
3268     } else {
3269       // If we are tail calling and generating PIC/GOT style code load the
3270       // address of the callee into ECX. The value in ecx is used as target of
3271       // the tail jump. This is done to circumvent the ebx/callee-saved problem
3272       // for tail calls on PIC/GOT architectures. Normally we would just put the
3273       // address of GOT into ebx and then call target@PLT. But for tail calls
3274       // ebx would be restored (since ebx is callee saved) before jumping to the
3275       // target@PLT.
3276
3277       // Note: The actual moving to ECX is done further down.
3278       GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
3279       if (G && !G->getGlobal()->hasLocalLinkage() &&
3280           G->getGlobal()->hasDefaultVisibility())
3281         Callee = LowerGlobalAddress(Callee, DAG);
3282       else if (isa<ExternalSymbolSDNode>(Callee))
3283         Callee = LowerExternalSymbol(Callee, DAG);
3284     }
3285   }
3286
3287   if (Is64Bit && isVarArg && !IsWin64 && !IsMustTail) {
3288     // From AMD64 ABI document:
3289     // For calls that may call functions that use varargs or stdargs
3290     // (prototype-less calls or calls to functions containing ellipsis (...) in
3291     // the declaration) %al is used as hidden argument to specify the number
3292     // of SSE registers used. The contents of %al do not need to match exactly
3293     // the number of registers, but must be an ubound on the number of SSE
3294     // registers used and is in the range 0 - 8 inclusive.
3295
3296     // Count the number of XMM registers allocated.
3297     static const MCPhysReg XMMArgRegs[] = {
3298       X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3299       X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
3300     };
3301     unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs);
3302     assert((Subtarget->hasSSE1() || !NumXMMRegs)
3303            && "SSE registers cannot be used when SSE is disabled");
3304
3305     RegsToPass.push_back(std::make_pair(unsigned(X86::AL),
3306                                         DAG.getConstant(NumXMMRegs, dl,
3307                                                         MVT::i8)));
3308   }
3309
3310   if (isVarArg && IsMustTail) {
3311     const auto &Forwards = X86Info->getForwardedMustTailRegParms();
3312     for (const auto &F : Forwards) {
3313       SDValue Val = DAG.getCopyFromReg(Chain, dl, F.VReg, F.VT);
3314       RegsToPass.push_back(std::make_pair(unsigned(F.PReg), Val));
3315     }
3316   }
3317
3318   // For tail calls lower the arguments to the 'real' stack slots.  Sibcalls
3319   // don't need this because the eligibility check rejects calls that require
3320   // shuffling arguments passed in memory.
3321   if (!IsSibcall && isTailCall) {
3322     // Force all the incoming stack arguments to be loaded from the stack
3323     // before any new outgoing arguments are stored to the stack, because the
3324     // outgoing stack slots may alias the incoming argument stack slots, and
3325     // the alias isn't otherwise explicit. This is slightly more conservative
3326     // than necessary, because it means that each store effectively depends
3327     // on every argument instead of just those arguments it would clobber.
3328     SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
3329
3330     SmallVector<SDValue, 8> MemOpChains2;
3331     SDValue FIN;
3332     int FI = 0;
3333     for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3334       CCValAssign &VA = ArgLocs[i];
3335       if (VA.isRegLoc())
3336         continue;
3337       assert(VA.isMemLoc());
3338       SDValue Arg = OutVals[i];
3339       ISD::ArgFlagsTy Flags = Outs[i].Flags;
3340       // Skip inalloca arguments.  They don't require any work.
3341       if (Flags.isInAlloca())
3342         continue;
3343       // Create frame index.
3344       int32_t Offset = VA.getLocMemOffset()+FPDiff;
3345       uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
3346       FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset, true);
3347       FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
3348
3349       if (Flags.isByVal()) {
3350         // Copy relative to framepointer.
3351         SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset(), dl);
3352         if (!StackPtr.getNode())
3353           StackPtr = DAG.getCopyFromReg(Chain, dl, RegInfo->getStackRegister(),
3354                                         getPointerTy(DAG.getDataLayout()));
3355         Source = DAG.getNode(ISD::ADD, dl, getPointerTy(DAG.getDataLayout()),
3356                              StackPtr, Source);
3357
3358         MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
3359                                                          ArgChain,
3360                                                          Flags, DAG, dl));
3361       } else {
3362         // Store relative to framepointer.
3363         MemOpChains2.push_back(DAG.getStore(
3364             ArgChain, dl, Arg, FIN,
3365             MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), FI),
3366             false, false, 0));
3367       }
3368     }
3369
3370     if (!MemOpChains2.empty())
3371       Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, MemOpChains2);
3372
3373     // Store the return address to the appropriate stack slot.
3374     Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx,
3375                                      getPointerTy(DAG.getDataLayout()),
3376                                      RegInfo->getSlotSize(), FPDiff, dl);
3377   }
3378
3379   // Build a sequence of copy-to-reg nodes chained together with token chain
3380   // and flag operands which copy the outgoing args into registers.
3381   SDValue InFlag;
3382   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
3383     Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
3384                              RegsToPass[i].second, InFlag);
3385     InFlag = Chain.getValue(1);
3386   }
3387
3388   if (DAG.getTarget().getCodeModel() == CodeModel::Large) {
3389     assert(Is64Bit && "Large code model is only legal in 64-bit mode.");
3390     // In the 64-bit large code model, we have to make all calls
3391     // through a register, since the call instruction's 32-bit
3392     // pc-relative offset may not be large enough to hold the whole
3393     // address.
3394   } else if (Callee->getOpcode() == ISD::GlobalAddress) {
3395     // If the callee is a GlobalAddress node (quite common, every direct call
3396     // is) turn it into a TargetGlobalAddress node so that legalize doesn't hack
3397     // it.
3398     GlobalAddressSDNode* G = cast<GlobalAddressSDNode>(Callee);
3399
3400     // We should use extra load for direct calls to dllimported functions in
3401     // non-JIT mode.
3402     const GlobalValue *GV = G->getGlobal();
3403     if (!GV->hasDLLImportStorageClass()) {
3404       unsigned char OpFlags = 0;
3405       bool ExtraLoad = false;
3406       unsigned WrapperKind = ISD::DELETED_NODE;
3407
3408       // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
3409       // external symbols most go through the PLT in PIC mode.  If the symbol
3410       // has hidden or protected visibility, or if it is static or local, then
3411       // we don't need to use the PLT - we can directly call it.
3412       if (Subtarget->isTargetELF() &&
3413           DAG.getTarget().getRelocationModel() == Reloc::PIC_ &&
3414           GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
3415         OpFlags = X86II::MO_PLT;
3416       } else if (Subtarget->isPICStyleStubAny() &&
3417                  !GV->isStrongDefinitionForLinker() &&
3418                  (!Subtarget->getTargetTriple().isMacOSX() ||
3419                   Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
3420         // PC-relative references to external symbols should go through $stub,
3421         // unless we're building with the leopard linker or later, which
3422         // automatically synthesizes these stubs.
3423         OpFlags = X86II::MO_DARWIN_STUB;
3424       } else if (Subtarget->isPICStyleRIPRel() && isa<Function>(GV) &&
3425                  cast<Function>(GV)->hasFnAttribute(Attribute::NonLazyBind)) {
3426         // If the function is marked as non-lazy, generate an indirect call
3427         // which loads from the GOT directly. This avoids runtime overhead
3428         // at the cost of eager binding (and one extra byte of encoding).
3429         OpFlags = X86II::MO_GOTPCREL;
3430         WrapperKind = X86ISD::WrapperRIP;
3431         ExtraLoad = true;
3432       }
3433
3434       Callee = DAG.getTargetGlobalAddress(
3435           GV, dl, getPointerTy(DAG.getDataLayout()), G->getOffset(), OpFlags);
3436
3437       // Add a wrapper if needed.
3438       if (WrapperKind != ISD::DELETED_NODE)
3439         Callee = DAG.getNode(X86ISD::WrapperRIP, dl,
3440                              getPointerTy(DAG.getDataLayout()), Callee);
3441       // Add extra indirection if needed.
3442       if (ExtraLoad)
3443         Callee = DAG.getLoad(
3444             getPointerTy(DAG.getDataLayout()), dl, DAG.getEntryNode(), Callee,
3445             MachinePointerInfo::getGOT(DAG.getMachineFunction()), false, false,
3446             false, 0);
3447     }
3448   } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
3449     unsigned char OpFlags = 0;
3450
3451     // On ELF targets, in either X86-64 or X86-32 mode, direct calls to
3452     // external symbols should go through the PLT.
3453     if (Subtarget->isTargetELF() &&
3454         DAG.getTarget().getRelocationModel() == Reloc::PIC_) {
3455       OpFlags = X86II::MO_PLT;
3456     } else if (Subtarget->isPICStyleStubAny() &&
3457                (!Subtarget->getTargetTriple().isMacOSX() ||
3458                 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
3459       // PC-relative references to external symbols should go through $stub,
3460       // unless we're building with the leopard linker or later, which
3461       // automatically synthesizes these stubs.
3462       OpFlags = X86II::MO_DARWIN_STUB;
3463     }
3464
3465     Callee = DAG.getTargetExternalSymbol(
3466         S->getSymbol(), getPointerTy(DAG.getDataLayout()), OpFlags);
3467   } else if (Subtarget->isTarget64BitILP32() &&
3468              Callee->getValueType(0) == MVT::i32) {
3469     // Zero-extend the 32-bit Callee address into a 64-bit according to x32 ABI
3470     Callee = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, Callee);
3471   }
3472
3473   // Returns a chain & a flag for retval copy to use.
3474   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
3475   SmallVector<SDValue, 8> Ops;
3476
3477   if (!IsSibcall && isTailCall) {
3478     Chain = DAG.getCALLSEQ_END(Chain,
3479                                DAG.getIntPtrConstant(NumBytesToPop, dl, true),
3480                                DAG.getIntPtrConstant(0, dl, true), InFlag, dl);
3481     InFlag = Chain.getValue(1);
3482   }
3483
3484   Ops.push_back(Chain);
3485   Ops.push_back(Callee);
3486
3487   if (isTailCall)
3488     Ops.push_back(DAG.getConstant(FPDiff, dl, MVT::i32));
3489
3490   // Add argument registers to the end of the list so that they are known live
3491   // into the call.
3492   for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
3493     Ops.push_back(DAG.getRegister(RegsToPass[i].first,
3494                                   RegsToPass[i].second.getValueType()));
3495
3496   // Add a register mask operand representing the call-preserved registers.
3497   const uint32_t *Mask = RegInfo->getCallPreservedMask(MF, CallConv);
3498   assert(Mask && "Missing call preserved mask for calling convention");
3499
3500   // If this is an invoke in a 32-bit function using a funclet-based
3501   // personality, assume the function clobbers all registers. If an exception
3502   // is thrown, the runtime will not restore CSRs.
3503   // FIXME: Model this more precisely so that we can register allocate across
3504   // the normal edge and spill and fill across the exceptional edge.
3505   if (!Is64Bit && CLI.CS && CLI.CS->isInvoke()) {
3506     const Function *CallerFn = MF.getFunction();
3507     EHPersonality Pers =
3508         CallerFn->hasPersonalityFn()
3509             ? classifyEHPersonality(CallerFn->getPersonalityFn())
3510             : EHPersonality::Unknown;
3511     if (isFuncletEHPersonality(Pers))
3512       Mask = RegInfo->getNoPreservedMask();
3513   }
3514
3515   Ops.push_back(DAG.getRegisterMask(Mask));
3516
3517   if (InFlag.getNode())
3518     Ops.push_back(InFlag);
3519
3520   if (isTailCall) {
3521     // We used to do:
3522     //// If this is the first return lowered for this function, add the regs
3523     //// to the liveout set for the function.
3524     // This isn't right, although it's probably harmless on x86; liveouts
3525     // should be computed from returns not tail calls.  Consider a void
3526     // function making a tail call to a function returning int.
3527     MF.getFrameInfo()->setHasTailCall();
3528     return DAG.getNode(X86ISD::TC_RETURN, dl, NodeTys, Ops);
3529   }
3530
3531   Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops);
3532   InFlag = Chain.getValue(1);
3533
3534   // Create the CALLSEQ_END node.
3535   unsigned NumBytesForCalleeToPop;
3536   if (X86::isCalleePop(CallConv, Is64Bit, isVarArg,
3537                        DAG.getTarget().Options.GuaranteedTailCallOpt))
3538     NumBytesForCalleeToPop = NumBytes;    // Callee pops everything
3539   else if (!Is64Bit && !canGuaranteeTCO(CallConv) &&
3540            !Subtarget->getTargetTriple().isOSMSVCRT() &&
3541            SR == StackStructReturn)
3542     // If this is a call to a struct-return function, the callee
3543     // pops the hidden struct pointer, so we have to push it back.
3544     // This is common for Darwin/X86, Linux & Mingw32 targets.
3545     // For MSVC Win32 targets, the caller pops the hidden struct pointer.
3546     NumBytesForCalleeToPop = 4;
3547   else
3548     NumBytesForCalleeToPop = 0;  // Callee pops nothing.
3549
3550   // Returns a flag for retval copy to use.
3551   if (!IsSibcall) {
3552     Chain = DAG.getCALLSEQ_END(Chain,
3553                                DAG.getIntPtrConstant(NumBytesToPop, dl, true),
3554                                DAG.getIntPtrConstant(NumBytesForCalleeToPop, dl,
3555                                                      true),
3556                                InFlag, dl);
3557     InFlag = Chain.getValue(1);
3558   }
3559
3560   // Handle result values, copying them out of physregs into vregs that we
3561   // return.
3562   return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
3563                          Ins, dl, DAG, InVals);
3564 }
3565
3566 //===----------------------------------------------------------------------===//
3567 //                Fast Calling Convention (tail call) implementation
3568 //===----------------------------------------------------------------------===//
3569
3570 //  Like std call, callee cleans arguments, convention except that ECX is
3571 //  reserved for storing the tail called function address. Only 2 registers are
3572 //  free for argument passing (inreg). Tail call optimization is performed
3573 //  provided:
3574 //                * tailcallopt is enabled
3575 //                * caller/callee are fastcc
3576 //  On X86_64 architecture with GOT-style position independent code only local
3577 //  (within module) calls are supported at the moment.
3578 //  To keep the stack aligned according to platform abi the function
3579 //  GetAlignedArgumentStackSize ensures that argument delta is always multiples
3580 //  of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
3581 //  If a tail called function callee has more arguments than the caller the
3582 //  caller needs to make sure that there is room to move the RETADDR to. This is
3583 //  achieved by reserving an area the size of the argument delta right after the
3584 //  original RETADDR, but before the saved framepointer or the spilled registers
3585 //  e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
3586 //  stack layout:
3587 //    arg1
3588 //    arg2
3589 //    RETADDR
3590 //    [ new RETADDR
3591 //      move area ]
3592 //    (possible EBP)
3593 //    ESI
3594 //    EDI
3595 //    local1 ..
3596
3597 /// Make the stack size align e.g 16n + 12 aligned for a 16-byte align
3598 /// requirement.
3599 unsigned
3600 X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
3601                                                SelectionDAG& DAG) const {
3602   const X86RegisterInfo *RegInfo = Subtarget->getRegisterInfo();
3603   const TargetFrameLowering &TFI = *Subtarget->getFrameLowering();
3604   unsigned StackAlignment = TFI.getStackAlignment();
3605   uint64_t AlignMask = StackAlignment - 1;
3606   int64_t Offset = StackSize;
3607   unsigned SlotSize = RegInfo->getSlotSize();
3608   if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
3609     // Number smaller than 12 so just add the difference.
3610     Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
3611   } else {
3612     // Mask out lower bits, add stackalignment once plus the 12 bytes.
3613     Offset = ((~AlignMask) & Offset) + StackAlignment +
3614       (StackAlignment-SlotSize);
3615   }
3616   return Offset;
3617 }
3618
3619 /// Return true if the given stack call argument is already available in the
3620 /// same position (relatively) of the caller's incoming argument stack.
3621 static
3622 bool MatchingStackOffset(SDValue Arg, unsigned Offset, ISD::ArgFlagsTy Flags,
3623                          MachineFrameInfo *MFI, const MachineRegisterInfo *MRI,
3624                          const X86InstrInfo *TII) {
3625   unsigned Bytes = Arg.getValueType().getSizeInBits() / 8;
3626   int FI = INT_MAX;
3627   if (Arg.getOpcode() == ISD::CopyFromReg) {
3628     unsigned VR = cast<RegisterSDNode>(Arg.getOperand(1))->getReg();
3629     if (!TargetRegisterInfo::isVirtualRegister(VR))
3630       return false;
3631     MachineInstr *Def = MRI->getVRegDef(VR);
3632     if (!Def)
3633       return false;
3634     if (!Flags.isByVal()) {
3635       if (!TII->isLoadFromStackSlot(Def, FI))
3636         return false;
3637     } else {
3638       unsigned Opcode = Def->getOpcode();
3639       if ((Opcode == X86::LEA32r || Opcode == X86::LEA64r ||
3640            Opcode == X86::LEA64_32r) &&
3641           Def->getOperand(1).isFI()) {
3642         FI = Def->getOperand(1).getIndex();
3643         Bytes = Flags.getByValSize();
3644       } else
3645         return false;
3646     }
3647   } else if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(Arg)) {
3648     if (Flags.isByVal())
3649       // ByVal argument is passed in as a pointer but it's now being
3650       // dereferenced. e.g.
3651       // define @foo(%struct.X* %A) {
3652       //   tail call @bar(%struct.X* byval %A)
3653       // }
3654       return false;
3655     SDValue Ptr = Ld->getBasePtr();
3656     FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr);
3657     if (!FINode)
3658       return false;
3659     FI = FINode->getIndex();
3660   } else if (Arg.getOpcode() == ISD::FrameIndex && Flags.isByVal()) {
3661     FrameIndexSDNode *FINode = cast<FrameIndexSDNode>(Arg);
3662     FI = FINode->getIndex();
3663     Bytes = Flags.getByValSize();
3664   } else
3665     return false;
3666
3667   assert(FI != INT_MAX);
3668   if (!MFI->isFixedObjectIndex(FI))
3669     return false;
3670   return Offset == MFI->getObjectOffset(FI) && Bytes == MFI->getObjectSize(FI);
3671 }
3672
3673 /// Check whether the call is eligible for tail call optimization. Targets
3674 /// that want to do tail call optimization should implement this function.
3675 bool X86TargetLowering::IsEligibleForTailCallOptimization(
3676     SDValue Callee, CallingConv::ID CalleeCC, bool isVarArg,
3677     bool isCalleeStructRet, bool isCallerStructRet, Type *RetTy,
3678     const SmallVectorImpl<ISD::OutputArg> &Outs,
3679     const SmallVectorImpl<SDValue> &OutVals,
3680     const SmallVectorImpl<ISD::InputArg> &Ins, SelectionDAG &DAG) const {
3681   if (!mayTailCallThisCC(CalleeCC))
3682     return false;
3683
3684   // If -tailcallopt is specified, make fastcc functions tail-callable.
3685   MachineFunction &MF = DAG.getMachineFunction();
3686   const Function *CallerF = MF.getFunction();
3687
3688   // If the function return type is x86_fp80 and the callee return type is not,
3689   // then the FP_EXTEND of the call result is not a nop. It's not safe to
3690   // perform a tailcall optimization here.
3691   if (CallerF->getReturnType()->isX86_FP80Ty() && !RetTy->isX86_FP80Ty())
3692     return false;
3693
3694   CallingConv::ID CallerCC = CallerF->getCallingConv();
3695   bool CCMatch = CallerCC == CalleeCC;
3696   bool IsCalleeWin64 = Subtarget->isCallingConvWin64(CalleeCC);
3697   bool IsCallerWin64 = Subtarget->isCallingConvWin64(CallerCC);
3698
3699   // Win64 functions have extra shadow space for argument homing. Don't do the
3700   // sibcall if the caller and callee have mismatched expectations for this
3701   // space.
3702   if (IsCalleeWin64 != IsCallerWin64)
3703     return false;
3704
3705   if (DAG.getTarget().Options.GuaranteedTailCallOpt) {
3706     if (canGuaranteeTCO(CalleeCC) && CCMatch)
3707       return true;
3708     return false;
3709   }
3710
3711   // Look for obvious safe cases to perform tail call optimization that do not
3712   // require ABI changes. This is what gcc calls sibcall.
3713
3714   // Can't do sibcall if stack needs to be dynamically re-aligned. PEI needs to
3715   // emit a special epilogue.
3716   const X86RegisterInfo *RegInfo = Subtarget->getRegisterInfo();
3717   if (RegInfo->needsStackRealignment(MF))
3718     return false;
3719
3720   // Also avoid sibcall optimization if either caller or callee uses struct
3721   // return semantics.
3722   if (isCalleeStructRet || isCallerStructRet)
3723     return false;
3724
3725   // Do not sibcall optimize vararg calls unless all arguments are passed via
3726   // registers.
3727   if (isVarArg && !Outs.empty()) {
3728     // Optimizing for varargs on Win64 is unlikely to be safe without
3729     // additional testing.
3730     if (IsCalleeWin64 || IsCallerWin64)
3731       return false;
3732
3733     SmallVector<CCValAssign, 16> ArgLocs;
3734     CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), ArgLocs,
3735                    *DAG.getContext());
3736
3737     CCInfo.AnalyzeCallOperands(Outs, CC_X86);
3738     for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i)
3739       if (!ArgLocs[i].isRegLoc())
3740         return false;
3741   }
3742
3743   // If the call result is in ST0 / ST1, it needs to be popped off the x87
3744   // stack.  Therefore, if it's not used by the call it is not safe to optimize
3745   // this into a sibcall.
3746   bool Unused = false;
3747   for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
3748     if (!Ins[i].Used) {
3749       Unused = true;
3750       break;
3751     }
3752   }
3753   if (Unused) {
3754     SmallVector<CCValAssign, 16> RVLocs;
3755     CCState CCInfo(CalleeCC, false, DAG.getMachineFunction(), RVLocs,
3756                    *DAG.getContext());
3757     CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
3758     for (unsigned i = 0, e = RVLocs.size(); i != e; ++i) {
3759       CCValAssign &VA = RVLocs[i];
3760       if (VA.getLocReg() == X86::FP0 || VA.getLocReg() == X86::FP1)
3761         return false;
3762     }
3763   }
3764
3765   // If the calling conventions do not match, then we'd better make sure the
3766   // results are returned in the same way as what the caller expects.
3767   if (!CCMatch) {
3768     SmallVector<CCValAssign, 16> RVLocs1;
3769     CCState CCInfo1(CalleeCC, false, DAG.getMachineFunction(), RVLocs1,
3770                     *DAG.getContext());
3771     CCInfo1.AnalyzeCallResult(Ins, RetCC_X86);
3772
3773     SmallVector<CCValAssign, 16> RVLocs2;
3774     CCState CCInfo2(CallerCC, false, DAG.getMachineFunction(), RVLocs2,
3775                     *DAG.getContext());
3776     CCInfo2.AnalyzeCallResult(Ins, RetCC_X86);
3777
3778     if (RVLocs1.size() != RVLocs2.size())
3779       return false;
3780     for (unsigned i = 0, e = RVLocs1.size(); i != e; ++i) {
3781       if (RVLocs1[i].isRegLoc() != RVLocs2[i].isRegLoc())
3782         return false;
3783       if (RVLocs1[i].getLocInfo() != RVLocs2[i].getLocInfo())
3784         return false;
3785       if (RVLocs1[i].isRegLoc()) {
3786         if (RVLocs1[i].getLocReg() != RVLocs2[i].getLocReg())
3787           return false;
3788       } else {
3789         if (RVLocs1[i].getLocMemOffset() != RVLocs2[i].getLocMemOffset())
3790           return false;
3791       }
3792     }
3793   }
3794
3795   unsigned StackArgsSize = 0;
3796
3797   // If the callee takes no arguments then go on to check the results of the
3798   // call.
3799   if (!Outs.empty()) {
3800     // Check if stack adjustment is needed. For now, do not do this if any
3801     // argument is passed on the stack.
3802     SmallVector<CCValAssign, 16> ArgLocs;
3803     CCState CCInfo(CalleeCC, isVarArg, DAG.getMachineFunction(), ArgLocs,
3804                    *DAG.getContext());
3805
3806     // Allocate shadow area for Win64
3807     if (IsCalleeWin64)
3808       CCInfo.AllocateStack(32, 8);
3809
3810     CCInfo.AnalyzeCallOperands(Outs, CC_X86);
3811     StackArgsSize = CCInfo.getNextStackOffset();
3812
3813     if (CCInfo.getNextStackOffset()) {
3814       // Check if the arguments are already laid out in the right way as
3815       // the caller's fixed stack objects.
3816       MachineFrameInfo *MFI = MF.getFrameInfo();
3817       const MachineRegisterInfo *MRI = &MF.getRegInfo();
3818       const X86InstrInfo *TII = Subtarget->getInstrInfo();
3819       for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3820         CCValAssign &VA = ArgLocs[i];
3821         SDValue Arg = OutVals[i];
3822         ISD::ArgFlagsTy Flags = Outs[i].Flags;
3823         if (VA.getLocInfo() == CCValAssign::Indirect)
3824           return false;
3825         if (!VA.isRegLoc()) {
3826           if (!MatchingStackOffset(Arg, VA.getLocMemOffset(), Flags,
3827                                    MFI, MRI, TII))
3828             return false;
3829         }
3830       }
3831     }
3832
3833     // If the tailcall address may be in a register, then make sure it's
3834     // possible to register allocate for it. In 32-bit, the call address can
3835     // only target EAX, EDX, or ECX since the tail call must be scheduled after
3836     // callee-saved registers are restored. These happen to be the same
3837     // registers used to pass 'inreg' arguments so watch out for those.
3838     if (!Subtarget->is64Bit() &&
3839         ((!isa<GlobalAddressSDNode>(Callee) &&
3840           !isa<ExternalSymbolSDNode>(Callee)) ||
3841          DAG.getTarget().getRelocationModel() == Reloc::PIC_)) {
3842       unsigned NumInRegs = 0;
3843       // In PIC we need an extra register to formulate the address computation
3844       // for the callee.
3845       unsigned MaxInRegs =
3846         (DAG.getTarget().getRelocationModel() == Reloc::PIC_) ? 2 : 3;
3847
3848       for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
3849         CCValAssign &VA = ArgLocs[i];
3850         if (!VA.isRegLoc())
3851           continue;
3852         unsigned Reg = VA.getLocReg();
3853         switch (Reg) {
3854         default: break;
3855         case X86::EAX: case X86::EDX: case X86::ECX:
3856           if (++NumInRegs == MaxInRegs)
3857             return false;
3858           break;
3859         }
3860       }
3861     }
3862   }
3863
3864   bool CalleeWillPop =
3865       X86::isCalleePop(CalleeCC, Subtarget->is64Bit(), isVarArg,
3866                        MF.getTarget().Options.GuaranteedTailCallOpt);
3867
3868   if (unsigned BytesToPop =
3869           MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn()) {
3870     // If we have bytes to pop, the callee must pop them.
3871     bool CalleePopMatches = CalleeWillPop && BytesToPop == StackArgsSize;
3872     if (!CalleePopMatches)
3873       return false;
3874   } else if (CalleeWillPop && StackArgsSize > 0) {
3875     // If we don't have bytes to pop, make sure the callee doesn't pop any.
3876     return false;
3877   }
3878
3879   return true;
3880 }
3881
3882 FastISel *
3883 X86TargetLowering::createFastISel(FunctionLoweringInfo &funcInfo,
3884                                   const TargetLibraryInfo *libInfo) const {
3885   return X86::createFastISel(funcInfo, libInfo);
3886 }
3887
3888 //===----------------------------------------------------------------------===//
3889 //                           Other Lowering Hooks
3890 //===----------------------------------------------------------------------===//
3891
3892 static bool MayFoldLoad(SDValue Op) {
3893   return Op.hasOneUse() && ISD::isNormalLoad(Op.getNode());
3894 }
3895
3896 static bool MayFoldIntoStore(SDValue Op) {
3897   return Op.hasOneUse() && ISD::isNormalStore(*Op.getNode()->use_begin());
3898 }
3899
3900 static bool isTargetShuffle(unsigned Opcode) {
3901   switch(Opcode) {
3902   default: return false;
3903   case X86ISD::BLENDI:
3904   case X86ISD::PSHUFB:
3905   case X86ISD::PSHUFD:
3906   case X86ISD::PSHUFHW:
3907   case X86ISD::PSHUFLW:
3908   case X86ISD::SHUFP:
3909   case X86ISD::PALIGNR:
3910   case X86ISD::MOVLHPS:
3911   case X86ISD::MOVLHPD:
3912   case X86ISD::MOVHLPS:
3913   case X86ISD::MOVLPS:
3914   case X86ISD::MOVLPD:
3915   case X86ISD::MOVSHDUP:
3916   case X86ISD::MOVSLDUP:
3917   case X86ISD::MOVDDUP:
3918   case X86ISD::MOVSS:
3919   case X86ISD::MOVSD:
3920   case X86ISD::UNPCKL:
3921   case X86ISD::UNPCKH:
3922   case X86ISD::VPERMILPI:
3923   case X86ISD::VPERM2X128:
3924   case X86ISD::VPERMI:
3925   case X86ISD::VPERMV:
3926   case X86ISD::VPERMV3:
3927     return true;
3928   }
3929 }
3930
3931 static SDValue getTargetShuffleNode(unsigned Opc, SDLoc dl, MVT VT,
3932                                     SDValue V1, unsigned TargetMask,
3933                                     SelectionDAG &DAG) {
3934   switch(Opc) {
3935   default: llvm_unreachable("Unknown x86 shuffle node");
3936   case X86ISD::PSHUFD:
3937   case X86ISD::PSHUFHW:
3938   case X86ISD::PSHUFLW:
3939   case X86ISD::VPERMILPI:
3940   case X86ISD::VPERMI:
3941     return DAG.getNode(Opc, dl, VT, V1,
3942                        DAG.getConstant(TargetMask, dl, MVT::i8));
3943   }
3944 }
3945
3946 static SDValue getTargetShuffleNode(unsigned Opc, SDLoc dl, MVT VT,
3947                                     SDValue V1, SDValue V2, SelectionDAG &DAG) {
3948   switch(Opc) {
3949   default: llvm_unreachable("Unknown x86 shuffle node");
3950   case X86ISD::MOVLHPS:
3951   case X86ISD::MOVLHPD:
3952   case X86ISD::MOVHLPS:
3953   case X86ISD::MOVLPS:
3954   case X86ISD::MOVLPD:
3955   case X86ISD::MOVSS:
3956   case X86ISD::MOVSD:
3957   case X86ISD::UNPCKL:
3958   case X86ISD::UNPCKH:
3959     return DAG.getNode(Opc, dl, VT, V1, V2);
3960   }
3961 }
3962
3963 SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) const {
3964   MachineFunction &MF = DAG.getMachineFunction();
3965   const X86RegisterInfo *RegInfo = Subtarget->getRegisterInfo();
3966   X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
3967   int ReturnAddrIndex = FuncInfo->getRAIndex();
3968
3969   if (ReturnAddrIndex == 0) {
3970     // Set up a frame object for the return address.
3971     unsigned SlotSize = RegInfo->getSlotSize();
3972     ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize,
3973                                                            -(int64_t)SlotSize,
3974                                                            false);
3975     FuncInfo->setRAIndex(ReturnAddrIndex);
3976   }
3977
3978   return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy(DAG.getDataLayout()));
3979 }
3980
3981 bool X86::isOffsetSuitableForCodeModel(int64_t Offset, CodeModel::Model M,
3982                                        bool hasSymbolicDisplacement) {
3983   // Offset should fit into 32 bit immediate field.
3984   if (!isInt<32>(Offset))
3985     return false;
3986
3987   // If we don't have a symbolic displacement - we don't have any extra
3988   // restrictions.
3989   if (!hasSymbolicDisplacement)
3990     return true;
3991
3992   // FIXME: Some tweaks might be needed for medium code model.
3993   if (M != CodeModel::Small && M != CodeModel::Kernel)
3994     return false;
3995
3996   // For small code model we assume that latest object is 16MB before end of 31
3997   // bits boundary. We may also accept pretty large negative constants knowing
3998   // that all objects are in the positive half of address space.
3999   if (M == CodeModel::Small && Offset < 16*1024*1024)
4000     return true;
4001
4002   // For kernel code model we know that all object resist in the negative half
4003   // of 32bits address space. We may not accept negative offsets, since they may
4004   // be just off and we may accept pretty large positive ones.
4005   if (M == CodeModel::Kernel && Offset >= 0)
4006     return true;
4007
4008   return false;
4009 }
4010
4011 /// Determines whether the callee is required to pop its own arguments.
4012 /// Callee pop is necessary to support tail calls.
4013 bool X86::isCalleePop(CallingConv::ID CallingConv,
4014                       bool is64Bit, bool IsVarArg, bool GuaranteeTCO) {
4015   // If GuaranteeTCO is true, we force some calls to be callee pop so that we
4016   // can guarantee TCO.
4017   if (!IsVarArg && shouldGuaranteeTCO(CallingConv, GuaranteeTCO))
4018     return true;
4019
4020   switch (CallingConv) {
4021   default:
4022     return false;
4023   case CallingConv::X86_StdCall:
4024   case CallingConv::X86_FastCall:
4025   case CallingConv::X86_ThisCall:
4026   case CallingConv::X86_VectorCall:
4027     return !is64Bit;
4028   }
4029 }
4030
4031 /// \brief Return true if the condition is an unsigned comparison operation.
4032 static bool isX86CCUnsigned(unsigned X86CC) {
4033   switch (X86CC) {
4034   default: llvm_unreachable("Invalid integer condition!");
4035   case X86::COND_E:     return true;
4036   case X86::COND_G:     return false;
4037   case X86::COND_GE:    return false;
4038   case X86::COND_L:     return false;
4039   case X86::COND_LE:    return false;
4040   case X86::COND_NE:    return true;
4041   case X86::COND_B:     return true;
4042   case X86::COND_A:     return true;
4043   case X86::COND_BE:    return true;
4044   case X86::COND_AE:    return true;
4045   }
4046 }
4047
4048 static X86::CondCode TranslateIntegerX86CC(ISD::CondCode SetCCOpcode) {
4049   switch (SetCCOpcode) {
4050   default: llvm_unreachable("Invalid integer condition!");
4051   case ISD::SETEQ:  return X86::COND_E;
4052   case ISD::SETGT:  return X86::COND_G;
4053   case ISD::SETGE:  return X86::COND_GE;
4054   case ISD::SETLT:  return X86::COND_L;
4055   case ISD::SETLE:  return X86::COND_LE;
4056   case ISD::SETNE:  return X86::COND_NE;
4057   case ISD::SETULT: return X86::COND_B;
4058   case ISD::SETUGT: return X86::COND_A;
4059   case ISD::SETULE: return X86::COND_BE;
4060   case ISD::SETUGE: return X86::COND_AE;
4061   }
4062 }
4063
4064 /// Do a one-to-one translation of a ISD::CondCode to the X86-specific
4065 /// condition code, returning the condition code and the LHS/RHS of the
4066 /// comparison to make.
4067 static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, SDLoc DL, bool isFP,
4068                                SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
4069   if (!isFP) {
4070     if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
4071       if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
4072         // X > -1   -> X == 0, jump !sign.
4073         RHS = DAG.getConstant(0, DL, RHS.getValueType());
4074         return X86::COND_NS;
4075       }
4076       if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
4077         // X < 0   -> X == 0, jump on sign.
4078         return X86::COND_S;
4079       }
4080       if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
4081         // X < 1   -> X <= 0
4082         RHS = DAG.getConstant(0, DL, RHS.getValueType());
4083         return X86::COND_LE;
4084       }
4085     }
4086
4087     return TranslateIntegerX86CC(SetCCOpcode);
4088   }
4089
4090   // First determine if it is required or is profitable to flip the operands.
4091
4092   // If LHS is a foldable load, but RHS is not, flip the condition.
4093   if (ISD::isNON_EXTLoad(LHS.getNode()) &&
4094       !ISD::isNON_EXTLoad(RHS.getNode())) {
4095     SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
4096     std::swap(LHS, RHS);
4097   }
4098
4099   switch (SetCCOpcode) {
4100   default: break;
4101   case ISD::SETOLT:
4102   case ISD::SETOLE:
4103   case ISD::SETUGT:
4104   case ISD::SETUGE:
4105     std::swap(LHS, RHS);
4106     break;
4107   }
4108
4109   // On a floating point condition, the flags are set as follows:
4110   // ZF  PF  CF   op
4111   //  0 | 0 | 0 | X > Y
4112   //  0 | 0 | 1 | X < Y
4113   //  1 | 0 | 0 | X == Y
4114   //  1 | 1 | 1 | unordered
4115   switch (SetCCOpcode) {
4116   default: llvm_unreachable("Condcode should be pre-legalized away");
4117   case ISD::SETUEQ:
4118   case ISD::SETEQ:   return X86::COND_E;
4119   case ISD::SETOLT:              // flipped
4120   case ISD::SETOGT:
4121   case ISD::SETGT:   return X86::COND_A;
4122   case ISD::SETOLE:              // flipped
4123   case ISD::SETOGE:
4124   case ISD::SETGE:   return X86::COND_AE;
4125   case ISD::SETUGT:              // flipped
4126   case ISD::SETULT:
4127   case ISD::SETLT:   return X86::COND_B;
4128   case ISD::SETUGE:              // flipped
4129   case ISD::SETULE:
4130   case ISD::SETLE:   return X86::COND_BE;
4131   case ISD::SETONE:
4132   case ISD::SETNE:   return X86::COND_NE;
4133   case ISD::SETUO:   return X86::COND_P;
4134   case ISD::SETO:    return X86::COND_NP;
4135   case ISD::SETOEQ:
4136   case ISD::SETUNE:  return X86::COND_INVALID;
4137   }
4138 }
4139
4140 /// Is there a floating point cmov for the specific X86 condition code?
4141 /// Current x86 isa includes the following FP cmov instructions:
4142 /// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
4143 static bool hasFPCMov(unsigned X86CC) {
4144   switch (X86CC) {
4145   default:
4146     return false;
4147   case X86::COND_B:
4148   case X86::COND_BE:
4149   case X86::COND_E:
4150   case X86::COND_P:
4151   case X86::COND_A:
4152   case X86::COND_AE:
4153   case X86::COND_NE:
4154   case X86::COND_NP:
4155     return true;
4156   }
4157 }
4158
4159 /// Returns true if the target can instruction select the
4160 /// specified FP immediate natively. If false, the legalizer will
4161 /// materialize the FP immediate as a load from a constant pool.
4162 bool X86TargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
4163   for (unsigned i = 0, e = LegalFPImmediates.size(); i != e; ++i) {
4164     if (Imm.bitwiseIsEqual(LegalFPImmediates[i]))
4165       return true;
4166   }
4167   return false;
4168 }
4169
4170 bool X86TargetLowering::shouldReduceLoadWidth(SDNode *Load,
4171                                               ISD::LoadExtType ExtTy,
4172                                               EVT NewVT) const {
4173   // "ELF Handling for Thread-Local Storage" specifies that R_X86_64_GOTTPOFF
4174   // relocation target a movq or addq instruction: don't let the load shrink.
4175   SDValue BasePtr = cast<LoadSDNode>(Load)->getBasePtr();
4176   if (BasePtr.getOpcode() == X86ISD::WrapperRIP)
4177     if (const auto *GA = dyn_cast<GlobalAddressSDNode>(BasePtr.getOperand(0)))
4178       return GA->getTargetFlags() != X86II::MO_GOTTPOFF;
4179   return true;
4180 }
4181
4182 /// \brief Returns true if it is beneficial to convert a load of a constant
4183 /// to just the constant itself.
4184 bool X86TargetLowering::shouldConvertConstantLoadToIntImm(const APInt &Imm,
4185                                                           Type *Ty) const {
4186   assert(Ty->isIntegerTy());
4187
4188   unsigned BitSize = Ty->getPrimitiveSizeInBits();
4189   if (BitSize == 0 || BitSize > 64)
4190     return false;
4191   return true;
4192 }
4193
4194 bool X86TargetLowering::isExtractSubvectorCheap(EVT ResVT,
4195                                                 unsigned Index) const {
4196   if (!isOperationLegalOrCustom(ISD::EXTRACT_SUBVECTOR, ResVT))
4197     return false;
4198
4199   return (Index == 0 || Index == ResVT.getVectorNumElements());
4200 }
4201
4202 bool X86TargetLowering::isCheapToSpeculateCttz() const {
4203   // Speculate cttz only if we can directly use TZCNT.
4204   return Subtarget->hasBMI();
4205 }
4206
4207 bool X86TargetLowering::isCheapToSpeculateCtlz() const {
4208   // Speculate ctlz only if we can directly use LZCNT.
4209   return Subtarget->hasLZCNT();
4210 }
4211
4212 /// Return true if every element in Mask, beginning
4213 /// from position Pos and ending in Pos+Size is undef.
4214 static bool isUndefInRange(ArrayRef<int> Mask, unsigned Pos, unsigned Size) {
4215   for (unsigned i = Pos, e = Pos + Size; i != e; ++i)
4216     if (0 <= Mask[i])
4217       return false;
4218   return true;
4219 }
4220
4221 /// Return true if Val is undef or if its value falls within the
4222 /// specified range (L, H].
4223 static bool isUndefOrInRange(int Val, int Low, int Hi) {
4224   return (Val < 0) || (Val >= Low && Val < Hi);
4225 }
4226
4227 /// Val is either less than zero (undef) or equal to the specified value.
4228 static bool isUndefOrEqual(int Val, int CmpVal) {
4229   return (Val < 0 || Val == CmpVal);
4230 }
4231
4232 /// Return true if every element in Mask, beginning
4233 /// from position Pos and ending in Pos+Size, falls within the specified
4234 /// sequential range (Low, Low+Size]. or is undef.
4235 static bool isSequentialOrUndefInRange(ArrayRef<int> Mask,
4236                                        unsigned Pos, unsigned Size, int Low) {
4237   for (unsigned i = Pos, e = Pos+Size; i != e; ++i, ++Low)
4238     if (!isUndefOrEqual(Mask[i], Low))
4239       return false;
4240   return true;
4241 }
4242
4243 /// Return true if the specified EXTRACT_SUBVECTOR operand specifies a vector
4244 /// extract that is suitable for instruction that extract 128 or 256 bit vectors
4245 static bool isVEXTRACTIndex(SDNode *N, unsigned vecWidth) {
4246   assert((vecWidth == 128 || vecWidth == 256) && "Unexpected vector width");
4247   if (!isa<ConstantSDNode>(N->getOperand(1).getNode()))
4248     return false;
4249
4250   // The index should be aligned on a vecWidth-bit boundary.
4251   uint64_t Index =
4252     cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4253
4254   MVT VT = N->getSimpleValueType(0);
4255   unsigned ElSize = VT.getVectorElementType().getSizeInBits();
4256   bool Result = (Index * ElSize) % vecWidth == 0;
4257
4258   return Result;
4259 }
4260
4261 /// Return true if the specified INSERT_SUBVECTOR
4262 /// operand specifies a subvector insert that is suitable for input to
4263 /// insertion of 128 or 256-bit subvectors
4264 static bool isVINSERTIndex(SDNode *N, unsigned vecWidth) {
4265   assert((vecWidth == 128 || vecWidth == 256) && "Unexpected vector width");
4266   if (!isa<ConstantSDNode>(N->getOperand(2).getNode()))
4267     return false;
4268   // The index should be aligned on a vecWidth-bit boundary.
4269   uint64_t Index =
4270     cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4271
4272   MVT VT = N->getSimpleValueType(0);
4273   unsigned ElSize = VT.getVectorElementType().getSizeInBits();
4274   bool Result = (Index * ElSize) % vecWidth == 0;
4275
4276   return Result;
4277 }
4278
4279 bool X86::isVINSERT128Index(SDNode *N) {
4280   return isVINSERTIndex(N, 128);
4281 }
4282
4283 bool X86::isVINSERT256Index(SDNode *N) {
4284   return isVINSERTIndex(N, 256);
4285 }
4286
4287 bool X86::isVEXTRACT128Index(SDNode *N) {
4288   return isVEXTRACTIndex(N, 128);
4289 }
4290
4291 bool X86::isVEXTRACT256Index(SDNode *N) {
4292   return isVEXTRACTIndex(N, 256);
4293 }
4294
4295 static unsigned getExtractVEXTRACTImmediate(SDNode *N, unsigned vecWidth) {
4296   assert((vecWidth == 128 || vecWidth == 256) && "Unsupported vector width");
4297   assert(isa<ConstantSDNode>(N->getOperand(1).getNode()) &&
4298          "Illegal extract subvector for VEXTRACT");
4299
4300   uint64_t Index =
4301     cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
4302
4303   MVT VecVT = N->getOperand(0).getSimpleValueType();
4304   MVT ElVT = VecVT.getVectorElementType();
4305
4306   unsigned NumElemsPerChunk = vecWidth / ElVT.getSizeInBits();
4307   return Index / NumElemsPerChunk;
4308 }
4309
4310 static unsigned getInsertVINSERTImmediate(SDNode *N, unsigned vecWidth) {
4311   assert((vecWidth == 128 || vecWidth == 256) && "Unsupported vector width");
4312   assert(isa<ConstantSDNode>(N->getOperand(2).getNode()) &&
4313          "Illegal insert subvector for VINSERT");
4314
4315   uint64_t Index =
4316     cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
4317
4318   MVT VecVT = N->getSimpleValueType(0);
4319   MVT ElVT = VecVT.getVectorElementType();
4320
4321   unsigned NumElemsPerChunk = vecWidth / ElVT.getSizeInBits();
4322   return Index / NumElemsPerChunk;
4323 }
4324
4325 /// Return the appropriate immediate to extract the specified
4326 /// EXTRACT_SUBVECTOR index with VEXTRACTF128 and VINSERTI128 instructions.
4327 unsigned X86::getExtractVEXTRACT128Immediate(SDNode *N) {
4328   return getExtractVEXTRACTImmediate(N, 128);
4329 }
4330
4331 /// Return the appropriate immediate to extract the specified
4332 /// EXTRACT_SUBVECTOR index with VEXTRACTF64x4 and VINSERTI64x4 instructions.
4333 unsigned X86::getExtractVEXTRACT256Immediate(SDNode *N) {
4334   return getExtractVEXTRACTImmediate(N, 256);
4335 }
4336
4337 /// Return the appropriate immediate to insert at the specified
4338 /// INSERT_SUBVECTOR index with VINSERTF128 and VINSERTI128 instructions.
4339 unsigned X86::getInsertVINSERT128Immediate(SDNode *N) {
4340   return getInsertVINSERTImmediate(N, 128);
4341 }
4342
4343 /// Return the appropriate immediate to insert at the specified
4344 /// INSERT_SUBVECTOR index with VINSERTF46x4 and VINSERTI64x4 instructions.
4345 unsigned X86::getInsertVINSERT256Immediate(SDNode *N) {
4346   return getInsertVINSERTImmediate(N, 256);
4347 }
4348
4349 /// Returns true if Elt is a constant zero or a floating point constant +0.0.
4350 bool X86::isZeroNode(SDValue Elt) {
4351   return isNullConstant(Elt) || isNullFPConstant(Elt);
4352 }
4353
4354 // Build a vector of constants
4355 // Use an UNDEF node if MaskElt == -1.
4356 // Spilt 64-bit constants in the 32-bit mode.
4357 static SDValue getConstVector(ArrayRef<int> Values, MVT VT,
4358                               SelectionDAG &DAG,
4359                               SDLoc dl, bool IsMask = false) {
4360
4361   SmallVector<SDValue, 32>  Ops;
4362   bool Split = false;
4363
4364   MVT ConstVecVT = VT;
4365   unsigned NumElts = VT.getVectorNumElements();
4366   bool In64BitMode = DAG.getTargetLoweringInfo().isTypeLegal(MVT::i64);
4367   if (!In64BitMode && VT.getVectorElementType() == MVT::i64) {
4368     ConstVecVT = MVT::getVectorVT(MVT::i32, NumElts * 2);
4369     Split = true;
4370   }
4371
4372   MVT EltVT = ConstVecVT.getVectorElementType();
4373   for (unsigned i = 0; i < NumElts; ++i) {
4374     bool IsUndef = Values[i] < 0 && IsMask;
4375     SDValue OpNode = IsUndef ? DAG.getUNDEF(EltVT) :
4376       DAG.getConstant(Values[i], dl, EltVT);
4377     Ops.push_back(OpNode);
4378     if (Split)
4379       Ops.push_back(IsUndef ? DAG.getUNDEF(EltVT) :
4380                     DAG.getConstant(0, dl, EltVT));
4381   }
4382   SDValue ConstsNode = DAG.getNode(ISD::BUILD_VECTOR, dl, ConstVecVT, Ops);
4383   if (Split)
4384     ConstsNode = DAG.getBitcast(VT, ConstsNode);
4385   return ConstsNode;
4386 }
4387
4388 /// Returns a vector of specified type with all zero elements.
4389 static SDValue getZeroVector(MVT VT, const X86Subtarget *Subtarget,
4390                              SelectionDAG &DAG, SDLoc dl) {
4391   assert(VT.isVector() && "Expected a vector type");
4392
4393   // Always build SSE zero vectors as <4 x i32> bitcasted
4394   // to their dest type. This ensures they get CSE'd.
4395   SDValue Vec;
4396   if (VT.is128BitVector()) {  // SSE
4397     if (Subtarget->hasSSE2()) {  // SSE2
4398       SDValue Cst = DAG.getConstant(0, dl, MVT::i32);
4399       Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4400     } else { // SSE1
4401       SDValue Cst = DAG.getConstantFP(+0.0, dl, MVT::f32);
4402       Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
4403     }
4404   } else if (VT.is256BitVector()) { // AVX
4405     if (Subtarget->hasInt256()) { // AVX2
4406       SDValue Cst = DAG.getConstant(0, dl, MVT::i32);
4407       SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4408       Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops);
4409     } else {
4410       // 256-bit logic and arithmetic instructions in AVX are all
4411       // floating-point, no support for integer ops. Emit fp zeroed vectors.
4412       SDValue Cst = DAG.getConstantFP(+0.0, dl, MVT::f32);
4413       SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4414       Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8f32, Ops);
4415     }
4416   } else if (VT.is512BitVector()) { // AVX-512
4417       SDValue Cst = DAG.getConstant(0, dl, MVT::i32);
4418       SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst,
4419                         Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4420       Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i32, Ops);
4421   } else if (VT.getVectorElementType() == MVT::i1) {
4422
4423     assert((Subtarget->hasBWI() || VT.getVectorNumElements() <= 16)
4424             && "Unexpected vector type");
4425     assert((Subtarget->hasVLX() || VT.getVectorNumElements() >= 8)
4426             && "Unexpected vector type");
4427     SDValue Cst = DAG.getConstant(0, dl, MVT::i1);
4428     SmallVector<SDValue, 64> Ops(VT.getVectorNumElements(), Cst);
4429     return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Ops);
4430   } else
4431     llvm_unreachable("Unexpected vector type");
4432
4433   return DAG.getBitcast(VT, Vec);
4434 }
4435
4436 static SDValue ExtractSubVector(SDValue Vec, unsigned IdxVal,
4437                                 SelectionDAG &DAG, SDLoc dl,
4438                                 unsigned vectorWidth) {
4439   assert((vectorWidth == 128 || vectorWidth == 256) &&
4440          "Unsupported vector width");
4441   EVT VT = Vec.getValueType();
4442   EVT ElVT = VT.getVectorElementType();
4443   unsigned Factor = VT.getSizeInBits()/vectorWidth;
4444   EVT ResultVT = EVT::getVectorVT(*DAG.getContext(), ElVT,
4445                                   VT.getVectorNumElements()/Factor);
4446
4447   // Extract from UNDEF is UNDEF.
4448   if (Vec.getOpcode() == ISD::UNDEF)
4449     return DAG.getUNDEF(ResultVT);
4450
4451   // Extract the relevant vectorWidth bits.  Generate an EXTRACT_SUBVECTOR
4452   unsigned ElemsPerChunk = vectorWidth / ElVT.getSizeInBits();
4453   assert(isPowerOf2_32(ElemsPerChunk) && "Elements per chunk not power of 2");
4454
4455   // This is the index of the first element of the vectorWidth-bit chunk
4456   // we want. Since ElemsPerChunk is a power of 2 just need to clear bits.
4457   IdxVal &= ~(ElemsPerChunk - 1);
4458
4459   // If the input is a buildvector just emit a smaller one.
4460   if (Vec.getOpcode() == ISD::BUILD_VECTOR)
4461     return DAG.getNode(ISD::BUILD_VECTOR, dl, ResultVT,
4462                        makeArrayRef(Vec->op_begin() + IdxVal, ElemsPerChunk));
4463
4464   SDValue VecIdx = DAG.getIntPtrConstant(IdxVal, dl);
4465   return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, ResultVT, Vec, VecIdx);
4466 }
4467
4468 /// Generate a DAG to grab 128-bits from a vector > 128 bits.  This
4469 /// sets things up to match to an AVX VEXTRACTF128 / VEXTRACTI128
4470 /// or AVX-512 VEXTRACTF32x4 / VEXTRACTI32x4
4471 /// instructions or a simple subregister reference. Idx is an index in the
4472 /// 128 bits we want.  It need not be aligned to a 128-bit boundary.  That makes
4473 /// lowering EXTRACT_VECTOR_ELT operations easier.
4474 static SDValue Extract128BitVector(SDValue Vec, unsigned IdxVal,
4475                                    SelectionDAG &DAG, SDLoc dl) {
4476   assert((Vec.getValueType().is256BitVector() ||
4477           Vec.getValueType().is512BitVector()) && "Unexpected vector size!");
4478   return ExtractSubVector(Vec, IdxVal, DAG, dl, 128);
4479 }
4480
4481 /// Generate a DAG to grab 256-bits from a 512-bit vector.
4482 static SDValue Extract256BitVector(SDValue Vec, unsigned IdxVal,
4483                                    SelectionDAG &DAG, SDLoc dl) {
4484   assert(Vec.getValueType().is512BitVector() && "Unexpected vector size!");
4485   return ExtractSubVector(Vec, IdxVal, DAG, dl, 256);
4486 }
4487
4488 static SDValue InsertSubVector(SDValue Result, SDValue Vec,
4489                                unsigned IdxVal, SelectionDAG &DAG,
4490                                SDLoc dl, unsigned vectorWidth) {
4491   assert((vectorWidth == 128 || vectorWidth == 256) &&
4492          "Unsupported vector width");
4493   // Inserting UNDEF is Result
4494   if (Vec.getOpcode() == ISD::UNDEF)
4495     return Result;
4496   EVT VT = Vec.getValueType();
4497   EVT ElVT = VT.getVectorElementType();
4498   EVT ResultVT = Result.getValueType();
4499
4500   // Insert the relevant vectorWidth bits.
4501   unsigned ElemsPerChunk = vectorWidth/ElVT.getSizeInBits();
4502   assert(isPowerOf2_32(ElemsPerChunk) && "Elements per chunk not power of 2");
4503
4504   // This is the index of the first element of the vectorWidth-bit chunk
4505   // we want. Since ElemsPerChunk is a power of 2 just need to clear bits.
4506   IdxVal &= ~(ElemsPerChunk - 1);
4507
4508   SDValue VecIdx = DAG.getIntPtrConstant(IdxVal, dl);
4509   return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Result, Vec, VecIdx);
4510 }
4511
4512 /// Generate a DAG to put 128-bits into a vector > 128 bits.  This
4513 /// sets things up to match to an AVX VINSERTF128/VINSERTI128 or
4514 /// AVX-512 VINSERTF32x4/VINSERTI32x4 instructions or a
4515 /// simple superregister reference.  Idx is an index in the 128 bits
4516 /// we want.  It need not be aligned to a 128-bit boundary.  That makes
4517 /// lowering INSERT_VECTOR_ELT operations easier.
4518 static SDValue Insert128BitVector(SDValue Result, SDValue Vec, unsigned IdxVal,
4519                                   SelectionDAG &DAG, SDLoc dl) {
4520   assert(Vec.getValueType().is128BitVector() && "Unexpected vector size!");
4521
4522   // For insertion into the zero index (low half) of a 256-bit vector, it is
4523   // more efficient to generate a blend with immediate instead of an insert*128.
4524   // We are still creating an INSERT_SUBVECTOR below with an undef node to
4525   // extend the subvector to the size of the result vector. Make sure that
4526   // we are not recursing on that node by checking for undef here.
4527   if (IdxVal == 0 && Result.getValueType().is256BitVector() &&
4528       Result.getOpcode() != ISD::UNDEF) {
4529     EVT ResultVT = Result.getValueType();
4530     SDValue ZeroIndex = DAG.getIntPtrConstant(0, dl);
4531     SDValue Undef = DAG.getUNDEF(ResultVT);
4532     SDValue Vec256 = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResultVT, Undef,
4533                                  Vec, ZeroIndex);
4534
4535     // The blend instruction, and therefore its mask, depend on the data type.
4536     MVT ScalarType = ResultVT.getVectorElementType().getSimpleVT();
4537     if (ScalarType.isFloatingPoint()) {
4538       // Choose either vblendps (float) or vblendpd (double).
4539       unsigned ScalarSize = ScalarType.getSizeInBits();
4540       assert((ScalarSize == 64 || ScalarSize == 32) && "Unknown float type");
4541       unsigned MaskVal = (ScalarSize == 64) ? 0x03 : 0x0f;
4542       SDValue Mask = DAG.getConstant(MaskVal, dl, MVT::i8);
4543       return DAG.getNode(X86ISD::BLENDI, dl, ResultVT, Result, Vec256, Mask);
4544     }
4545
4546     const X86Subtarget &Subtarget =
4547     static_cast<const X86Subtarget &>(DAG.getSubtarget());
4548
4549     // AVX2 is needed for 256-bit integer blend support.
4550     // Integers must be cast to 32-bit because there is only vpblendd;
4551     // vpblendw can't be used for this because it has a handicapped mask.
4552
4553     // If we don't have AVX2, then cast to float. Using a wrong domain blend
4554     // is still more efficient than using the wrong domain vinsertf128 that
4555     // will be created by InsertSubVector().
4556     MVT CastVT = Subtarget.hasAVX2() ? MVT::v8i32 : MVT::v8f32;
4557
4558     SDValue Mask = DAG.getConstant(0x0f, dl, MVT::i8);
4559     Vec256 = DAG.getBitcast(CastVT, Vec256);
4560     Vec256 = DAG.getNode(X86ISD::BLENDI, dl, CastVT, Result, Vec256, Mask);
4561     return DAG.getBitcast(ResultVT, Vec256);
4562   }
4563
4564   return InsertSubVector(Result, Vec, IdxVal, DAG, dl, 128);
4565 }
4566
4567 static SDValue Insert256BitVector(SDValue Result, SDValue Vec, unsigned IdxVal,
4568                                   SelectionDAG &DAG, SDLoc dl) {
4569   assert(Vec.getValueType().is256BitVector() && "Unexpected vector size!");
4570   return InsertSubVector(Result, Vec, IdxVal, DAG, dl, 256);
4571 }
4572
4573 /// Insert i1-subvector to i1-vector.
4574 static SDValue Insert1BitVector(SDValue Op, SelectionDAG &DAG) {
4575
4576   SDLoc dl(Op);
4577   SDValue Vec = Op.getOperand(0);
4578   SDValue SubVec = Op.getOperand(1);
4579   SDValue Idx = Op.getOperand(2);
4580
4581   if (!isa<ConstantSDNode>(Idx))
4582     return SDValue();
4583
4584   unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
4585   if (IdxVal == 0  && Vec.isUndef()) // the operation is legal
4586     return Op;
4587
4588   MVT OpVT = Op.getSimpleValueType();
4589   MVT SubVecVT = SubVec.getSimpleValueType();
4590   unsigned NumElems = OpVT.getVectorNumElements();
4591   unsigned SubVecNumElems = SubVecVT.getVectorNumElements();
4592
4593   assert(IdxVal + SubVecNumElems <= NumElems &&
4594          IdxVal % SubVecVT.getSizeInBits() == 0 &&
4595          "Unexpected index value in INSERT_SUBVECTOR");
4596
4597   // There are 3 possible cases:
4598   // 1. Subvector should be inserted in the lower part (IdxVal == 0)
4599   // 2. Subvector should be inserted in the upper part
4600   //    (IdxVal + SubVecNumElems == NumElems)
4601   // 3. Subvector should be inserted in the middle (for example v2i1
4602   //    to v16i1, index 2)
4603
4604   SDValue ZeroIdx = DAG.getIntPtrConstant(0, dl);
4605   SDValue Undef = DAG.getUNDEF(OpVT);
4606   SDValue WideSubVec =
4607     DAG.getNode(ISD::INSERT_SUBVECTOR, dl, OpVT, Undef, SubVec, ZeroIdx);
4608   if (Vec.isUndef())
4609     return DAG.getNode(X86ISD::VSHLI, dl, OpVT, WideSubVec,
4610       DAG.getConstant(IdxVal, dl, MVT::i8));
4611
4612   if (ISD::isBuildVectorAllZeros(Vec.getNode())) {
4613     unsigned ShiftLeft = NumElems - SubVecNumElems;
4614     unsigned ShiftRight = NumElems - SubVecNumElems - IdxVal;
4615     WideSubVec = DAG.getNode(X86ISD::VSHLI, dl, OpVT, WideSubVec,
4616       DAG.getConstant(ShiftLeft, dl, MVT::i8));
4617     return ShiftRight ? DAG.getNode(X86ISD::VSRLI, dl, OpVT, WideSubVec,
4618       DAG.getConstant(ShiftRight, dl, MVT::i8)) : WideSubVec;
4619   }
4620
4621   if (IdxVal == 0) {
4622     // Zero lower bits of the Vec
4623     SDValue ShiftBits = DAG.getConstant(SubVecNumElems, dl, MVT::i8);
4624     Vec = DAG.getNode(X86ISD::VSRLI, dl, OpVT, Vec, ShiftBits);
4625     Vec = DAG.getNode(X86ISD::VSHLI, dl, OpVT, Vec, ShiftBits);
4626     // Merge them together
4627     return DAG.getNode(ISD::OR, dl, OpVT, Vec, WideSubVec);
4628   }
4629
4630   // Simple case when we put subvector in the upper part
4631   if (IdxVal + SubVecNumElems == NumElems) {
4632     // Zero upper bits of the Vec
4633     WideSubVec = DAG.getNode(X86ISD::VSHLI, dl, OpVT, Vec,
4634                         DAG.getConstant(IdxVal, dl, MVT::i8));
4635     SDValue ShiftBits = DAG.getConstant(SubVecNumElems, dl, MVT::i8);
4636     Vec = DAG.getNode(X86ISD::VSHLI, dl, OpVT, Vec, ShiftBits);
4637     Vec = DAG.getNode(X86ISD::VSRLI, dl, OpVT, Vec, ShiftBits);
4638     return DAG.getNode(ISD::OR, dl, OpVT, Vec, WideSubVec);
4639   }
4640   // Subvector should be inserted in the middle - use shuffle
4641   SmallVector<int, 64> Mask;
4642   for (unsigned i = 0; i < NumElems; ++i)
4643     Mask.push_back(i >= IdxVal && i < IdxVal + SubVecNumElems ?
4644                     i : i + NumElems);
4645   return DAG.getVectorShuffle(OpVT, dl, WideSubVec, Vec, Mask);
4646 }
4647
4648 /// Concat two 128-bit vectors into a 256 bit vector using VINSERTF128
4649 /// instructions. This is used because creating CONCAT_VECTOR nodes of
4650 /// BUILD_VECTORS returns a larger BUILD_VECTOR while we're trying to lower
4651 /// large BUILD_VECTORS.
4652 static SDValue Concat128BitVectors(SDValue V1, SDValue V2, EVT VT,
4653                                    unsigned NumElems, SelectionDAG &DAG,
4654                                    SDLoc dl) {
4655   SDValue V = Insert128BitVector(DAG.getUNDEF(VT), V1, 0, DAG, dl);
4656   return Insert128BitVector(V, V2, NumElems/2, DAG, dl);
4657 }
4658
4659 static SDValue Concat256BitVectors(SDValue V1, SDValue V2, EVT VT,
4660                                    unsigned NumElems, SelectionDAG &DAG,
4661                                    SDLoc dl) {
4662   SDValue V = Insert256BitVector(DAG.getUNDEF(VT), V1, 0, DAG, dl);
4663   return Insert256BitVector(V, V2, NumElems/2, DAG, dl);
4664 }
4665
4666 /// Returns a vector of specified type with all bits set.
4667 /// Always build ones vectors as <4 x i32> or <8 x i32>. For 256-bit types with
4668 /// no AVX2 supprt, use two <4 x i32> inserted in a <8 x i32> appropriately.
4669 /// Then bitcast to their original type, ensuring they get CSE'd.
4670 static SDValue getOnesVector(EVT VT, const X86Subtarget *Subtarget,
4671                              SelectionDAG &DAG, SDLoc dl) {
4672   assert(VT.isVector() && "Expected a vector type");
4673
4674   SDValue Cst = DAG.getConstant(~0U, dl, MVT::i32);
4675   SDValue Vec;
4676   if (VT.is512BitVector()) {
4677     SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst,
4678                       Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4679     Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v16i32, Ops);
4680   } else if (VT.is256BitVector()) {
4681     if (Subtarget->hasInt256()) { // AVX2
4682       SDValue Ops[] = { Cst, Cst, Cst, Cst, Cst, Cst, Cst, Cst };
4683       Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v8i32, Ops);
4684     } else { // AVX
4685       Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4686       Vec = Concat128BitVectors(Vec, Vec, MVT::v8i32, 8, DAG, dl);
4687     }
4688   } else if (VT.is128BitVector()) {
4689     Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
4690   } else
4691     llvm_unreachable("Unexpected vector type");
4692
4693   return DAG.getBitcast(VT, Vec);
4694 }
4695
4696 /// Returns a vector_shuffle node for an unpackl operation.
4697 static SDValue getUnpackl(SelectionDAG &DAG, SDLoc dl, MVT VT, SDValue V1,
4698                           SDValue V2) {
4699   unsigned NumElems = VT.getVectorNumElements();
4700   SmallVector<int, 8> Mask;
4701   for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
4702     Mask.push_back(i);
4703     Mask.push_back(i + NumElems);
4704   }
4705   return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
4706 }
4707
4708 /// Returns a vector_shuffle node for an unpackh operation.
4709 static SDValue getUnpackh(SelectionDAG &DAG, SDLoc dl, MVT VT, SDValue V1,
4710                           SDValue V2) {
4711   unsigned NumElems = VT.getVectorNumElements();
4712   SmallVector<int, 8> Mask;
4713   for (unsigned i = 0, Half = NumElems/2; i != Half; ++i) {
4714     Mask.push_back(i + Half);
4715     Mask.push_back(i + NumElems + Half);
4716   }
4717   return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
4718 }
4719
4720 /// Return a vector_shuffle of the specified vector of zero or undef vector.
4721 /// This produces a shuffle where the low element of V2 is swizzled into the
4722 /// zero/undef vector, landing at element Idx.
4723 /// This produces a shuffle mask like 4,1,2,3 (idx=0) or  0,1,2,4 (idx=3).
4724 static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
4725                                            bool IsZero,
4726                                            const X86Subtarget *Subtarget,
4727                                            SelectionDAG &DAG) {
4728   MVT VT = V2.getSimpleValueType();
4729   SDValue V1 = IsZero
4730     ? getZeroVector(VT, Subtarget, DAG, SDLoc(V2)) : DAG.getUNDEF(VT);
4731   unsigned NumElems = VT.getVectorNumElements();
4732   SmallVector<int, 16> MaskVec;
4733   for (unsigned i = 0; i != NumElems; ++i)
4734     // If this is the insertion idx, put the low elt of V2 here.
4735     MaskVec.push_back(i == Idx ? NumElems : i);
4736   return DAG.getVectorShuffle(VT, SDLoc(V2), V1, V2, &MaskVec[0]);
4737 }
4738
4739 /// Calculates the shuffle mask corresponding to the target-specific opcode.
4740 /// Returns true if the Mask could be calculated. Sets IsUnary to true if only
4741 /// uses one source. Note that this will set IsUnary for shuffles which use a
4742 /// single input multiple times, and in those cases it will
4743 /// adjust the mask to only have indices within that single input.
4744 /// FIXME: Add support for Decode*Mask functions that return SM_SentinelZero.
4745 static bool getTargetShuffleMask(SDNode *N, MVT VT,
4746                                  SmallVectorImpl<int> &Mask, bool &IsUnary) {
4747   unsigned NumElems = VT.getVectorNumElements();
4748   SDValue ImmN;
4749
4750   IsUnary = false;
4751   bool IsFakeUnary = false;
4752   switch(N->getOpcode()) {
4753   case X86ISD::BLENDI:
4754     ImmN = N->getOperand(N->getNumOperands()-1);
4755     DecodeBLENDMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4756     break;
4757   case X86ISD::SHUFP:
4758     ImmN = N->getOperand(N->getNumOperands()-1);
4759     DecodeSHUFPMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4760     IsUnary = IsFakeUnary = N->getOperand(0) == N->getOperand(1);
4761     break;
4762   case X86ISD::UNPCKH:
4763     DecodeUNPCKHMask(VT, Mask);
4764     IsUnary = IsFakeUnary = N->getOperand(0) == N->getOperand(1);
4765     break;
4766   case X86ISD::UNPCKL:
4767     DecodeUNPCKLMask(VT, Mask);
4768     IsUnary = IsFakeUnary = N->getOperand(0) == N->getOperand(1);
4769     break;
4770   case X86ISD::MOVHLPS:
4771     DecodeMOVHLPSMask(NumElems, Mask);
4772     IsUnary = IsFakeUnary = N->getOperand(0) == N->getOperand(1);
4773     break;
4774   case X86ISD::MOVLHPS:
4775     DecodeMOVLHPSMask(NumElems, Mask);
4776     IsUnary = IsFakeUnary = N->getOperand(0) == N->getOperand(1);
4777     break;
4778   case X86ISD::PALIGNR:
4779     ImmN = N->getOperand(N->getNumOperands()-1);
4780     DecodePALIGNRMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4781     break;
4782   case X86ISD::PSHUFD:
4783   case X86ISD::VPERMILPI:
4784     ImmN = N->getOperand(N->getNumOperands()-1);
4785     DecodePSHUFMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4786     IsUnary = true;
4787     break;
4788   case X86ISD::PSHUFHW:
4789     ImmN = N->getOperand(N->getNumOperands()-1);
4790     DecodePSHUFHWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4791     IsUnary = true;
4792     break;
4793   case X86ISD::PSHUFLW:
4794     ImmN = N->getOperand(N->getNumOperands()-1);
4795     DecodePSHUFLWMask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4796     IsUnary = true;
4797     break;
4798   case X86ISD::PSHUFB: {
4799     IsUnary = true;
4800     SDValue MaskNode = N->getOperand(1);
4801     while (MaskNode->getOpcode() == ISD::BITCAST)
4802       MaskNode = MaskNode->getOperand(0);
4803
4804     if (MaskNode->getOpcode() == ISD::BUILD_VECTOR) {
4805       // If we have a build-vector, then things are easy.
4806       MVT VT = MaskNode.getSimpleValueType();
4807       assert(VT.isVector() &&
4808              "Can't produce a non-vector with a build_vector!");
4809       if (!VT.isInteger())
4810         return false;
4811
4812       int NumBytesPerElement = VT.getVectorElementType().getSizeInBits() / 8;
4813
4814       SmallVector<uint64_t, 32> RawMask;
4815       for (int i = 0, e = MaskNode->getNumOperands(); i < e; ++i) {
4816         SDValue Op = MaskNode->getOperand(i);
4817         if (Op->getOpcode() == ISD::UNDEF) {
4818           RawMask.push_back((uint64_t)SM_SentinelUndef);
4819           continue;
4820         }
4821         auto *CN = dyn_cast<ConstantSDNode>(Op.getNode());
4822         if (!CN)
4823           return false;
4824         APInt MaskElement = CN->getAPIntValue();
4825
4826         // We now have to decode the element which could be any integer size and
4827         // extract each byte of it.
4828         for (int j = 0; j < NumBytesPerElement; ++j) {
4829           // Note that this is x86 and so always little endian: the low byte is
4830           // the first byte of the mask.
4831           RawMask.push_back(MaskElement.getLoBits(8).getZExtValue());
4832           MaskElement = MaskElement.lshr(8);
4833         }
4834       }
4835       DecodePSHUFBMask(RawMask, Mask);
4836       break;
4837     }
4838
4839     auto *MaskLoad = dyn_cast<LoadSDNode>(MaskNode);
4840     if (!MaskLoad)
4841       return false;
4842
4843     SDValue Ptr = MaskLoad->getBasePtr();
4844     if (Ptr->getOpcode() == X86ISD::Wrapper ||
4845         Ptr->getOpcode() == X86ISD::WrapperRIP)
4846       Ptr = Ptr->getOperand(0);
4847
4848     auto *MaskCP = dyn_cast<ConstantPoolSDNode>(Ptr);
4849     if (!MaskCP || MaskCP->isMachineConstantPoolEntry())
4850       return false;
4851
4852     if (auto *C = dyn_cast<Constant>(MaskCP->getConstVal())) {
4853       DecodePSHUFBMask(C, Mask);
4854       if (Mask.empty())
4855         return false;
4856       break;
4857     }
4858
4859     return false;
4860   }
4861   case X86ISD::VPERMI:
4862     ImmN = N->getOperand(N->getNumOperands()-1);
4863     DecodeVPERMMask(cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4864     IsUnary = true;
4865     break;
4866   case X86ISD::MOVSS:
4867   case X86ISD::MOVSD:
4868     DecodeScalarMoveMask(VT, /* IsLoad */ false, Mask);
4869     break;
4870   case X86ISD::VPERM2X128:
4871     ImmN = N->getOperand(N->getNumOperands()-1);
4872     DecodeVPERM2X128Mask(VT, cast<ConstantSDNode>(ImmN)->getZExtValue(), Mask);
4873     if (Mask.empty()) return false;
4874     // Mask only contains negative index if an element is zero.
4875     if (std::any_of(Mask.begin(), Mask.end(),
4876                     [](int M){ return M == SM_SentinelZero; }))
4877       return false;
4878     break;
4879   case X86ISD::MOVSLDUP:
4880     DecodeMOVSLDUPMask(VT, Mask);
4881     IsUnary = true;
4882     break;
4883   case X86ISD::MOVSHDUP:
4884     DecodeMOVSHDUPMask(VT, Mask);
4885     IsUnary = true;
4886     break;
4887   case X86ISD::MOVDDUP:
4888     DecodeMOVDDUPMask(VT, Mask);
4889     IsUnary = true;
4890     break;
4891   case X86ISD::MOVLHPD:
4892   case X86ISD::MOVLPD:
4893   case X86ISD::MOVLPS:
4894     // Not yet implemented
4895     return false;
4896   case X86ISD::VPERMV: {
4897     IsUnary = true;
4898     SDValue MaskNode = N->getOperand(0);
4899     while (MaskNode->getOpcode() == ISD::BITCAST)
4900       MaskNode = MaskNode->getOperand(0);
4901
4902     unsigned MaskLoBits = Log2_64(VT.getVectorNumElements());
4903     SmallVector<uint64_t, 32> RawMask;
4904     if (MaskNode->getOpcode() == ISD::BUILD_VECTOR) {
4905       // If we have a build-vector, then things are easy.
4906       assert(MaskNode.getSimpleValueType().isInteger() &&
4907              MaskNode.getSimpleValueType().getVectorNumElements() ==
4908              VT.getVectorNumElements());
4909
4910       for (unsigned i = 0; i < MaskNode->getNumOperands(); ++i) {
4911         SDValue Op = MaskNode->getOperand(i);
4912         if (Op->getOpcode() == ISD::UNDEF)
4913           RawMask.push_back((uint64_t)SM_SentinelUndef);
4914         else if (isa<ConstantSDNode>(Op)) {
4915           APInt MaskElement = cast<ConstantSDNode>(Op)->getAPIntValue();
4916           RawMask.push_back(MaskElement.getLoBits(MaskLoBits).getZExtValue());
4917         } else
4918           return false;
4919       }
4920       DecodeVPERMVMask(RawMask, Mask);
4921       break;
4922     }
4923     if (MaskNode->getOpcode() == X86ISD::VBROADCAST) {
4924       unsigned NumEltsInMask = MaskNode->getNumOperands();
4925       MaskNode = MaskNode->getOperand(0);
4926       if (auto *CN = dyn_cast<ConstantSDNode>(MaskNode)) {
4927         APInt MaskEltValue = CN->getAPIntValue();
4928         for (unsigned i = 0; i < NumEltsInMask; ++i)
4929           RawMask.push_back(MaskEltValue.getLoBits(MaskLoBits).getZExtValue());
4930         DecodeVPERMVMask(RawMask, Mask);
4931         break;
4932       }
4933       // It may be a scalar load
4934     }
4935
4936     auto *MaskLoad = dyn_cast<LoadSDNode>(MaskNode);
4937     if (!MaskLoad)
4938       return false;
4939
4940     SDValue Ptr = MaskLoad->getBasePtr();
4941     if (Ptr->getOpcode() == X86ISD::Wrapper ||
4942         Ptr->getOpcode() == X86ISD::WrapperRIP)
4943       Ptr = Ptr->getOperand(0);
4944
4945     auto *MaskCP = dyn_cast<ConstantPoolSDNode>(Ptr);
4946     if (!MaskCP || MaskCP->isMachineConstantPoolEntry())
4947       return false;
4948
4949     if (auto *C = dyn_cast<Constant>(MaskCP->getConstVal())) {
4950       DecodeVPERMVMask(C, VT, Mask);
4951       if (Mask.empty())
4952         return false;
4953       break;
4954     }
4955     return false;
4956   }
4957   case X86ISD::VPERMV3: {
4958     IsUnary = false;
4959     SDValue MaskNode = N->getOperand(1);
4960     while (MaskNode->getOpcode() == ISD::BITCAST)
4961       MaskNode = MaskNode->getOperand(1);
4962
4963     if (MaskNode->getOpcode() == ISD::BUILD_VECTOR) {
4964       // If we have a build-vector, then things are easy.
4965       assert(MaskNode.getSimpleValueType().isInteger() &&
4966              MaskNode.getSimpleValueType().getVectorNumElements() ==
4967              VT.getVectorNumElements());
4968
4969       SmallVector<uint64_t, 32> RawMask;
4970       unsigned MaskLoBits = Log2_64(VT.getVectorNumElements()*2);
4971
4972       for (unsigned i = 0; i < MaskNode->getNumOperands(); ++i) {
4973         SDValue Op = MaskNode->getOperand(i);
4974         if (Op->getOpcode() == ISD::UNDEF)
4975           RawMask.push_back((uint64_t)SM_SentinelUndef);
4976         else {
4977           auto *CN = dyn_cast<ConstantSDNode>(Op.getNode());
4978           if (!CN)
4979             return false;
4980           APInt MaskElement = CN->getAPIntValue();
4981           RawMask.push_back(MaskElement.getLoBits(MaskLoBits).getZExtValue());
4982         }
4983       }
4984       DecodeVPERMV3Mask(RawMask, Mask);
4985       break;
4986     }
4987
4988     auto *MaskLoad = dyn_cast<LoadSDNode>(MaskNode);
4989     if (!MaskLoad)
4990       return false;
4991
4992     SDValue Ptr = MaskLoad->getBasePtr();
4993     if (Ptr->getOpcode() == X86ISD::Wrapper ||
4994         Ptr->getOpcode() == X86ISD::WrapperRIP)
4995       Ptr = Ptr->getOperand(0);
4996
4997     auto *MaskCP = dyn_cast<ConstantPoolSDNode>(Ptr);
4998     if (!MaskCP || MaskCP->isMachineConstantPoolEntry())
4999       return false;
5000
5001     if (auto *C = dyn_cast<Constant>(MaskCP->getConstVal())) {
5002       DecodeVPERMV3Mask(C, VT, Mask);
5003       if (Mask.empty())
5004         return false;
5005       break;
5006     }
5007     return false;
5008   }
5009   default: llvm_unreachable("unknown target shuffle node");
5010   }
5011
5012   // If we have a fake unary shuffle, the shuffle mask is spread across two
5013   // inputs that are actually the same node. Re-map the mask to always point
5014   // into the first input.
5015   if (IsFakeUnary)
5016     for (int &M : Mask)
5017       if (M >= (int)Mask.size())
5018         M -= Mask.size();
5019
5020   return true;
5021 }
5022
5023 /// Returns the scalar element that will make up the ith
5024 /// element of the result of the vector shuffle.
5025 static SDValue getShuffleScalarElt(SDNode *N, unsigned Index, SelectionDAG &DAG,
5026                                    unsigned Depth) {
5027   if (Depth == 6)
5028     return SDValue();  // Limit search depth.
5029
5030   SDValue V = SDValue(N, 0);
5031   EVT VT = V.getValueType();
5032   unsigned Opcode = V.getOpcode();
5033
5034   // Recurse into ISD::VECTOR_SHUFFLE node to find scalars.
5035   if (const ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(N)) {
5036     int Elt = SV->getMaskElt(Index);
5037
5038     if (Elt < 0)
5039       return DAG.getUNDEF(VT.getVectorElementType());
5040
5041     unsigned NumElems = VT.getVectorNumElements();
5042     SDValue NewV = (Elt < (int)NumElems) ? SV->getOperand(0)
5043                                          : SV->getOperand(1);
5044     return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG, Depth+1);
5045   }
5046
5047   // Recurse into target specific vector shuffles to find scalars.
5048   if (isTargetShuffle(Opcode)) {
5049     MVT ShufVT = V.getSimpleValueType();
5050     unsigned NumElems = ShufVT.getVectorNumElements();
5051     SmallVector<int, 16> ShuffleMask;
5052     bool IsUnary;
5053
5054     if (!getTargetShuffleMask(N, ShufVT, ShuffleMask, IsUnary))
5055       return SDValue();
5056
5057     int Elt = ShuffleMask[Index];
5058     if (Elt < 0)
5059       return DAG.getUNDEF(ShufVT.getVectorElementType());
5060
5061     SDValue NewV = (Elt < (int)NumElems) ? N->getOperand(0)
5062                                          : N->getOperand(1);
5063     return getShuffleScalarElt(NewV.getNode(), Elt % NumElems, DAG,
5064                                Depth+1);
5065   }
5066
5067   // Actual nodes that may contain scalar elements
5068   if (Opcode == ISD::BITCAST) {
5069     V = V.getOperand(0);
5070     EVT SrcVT = V.getValueType();
5071     unsigned NumElems = VT.getVectorNumElements();
5072
5073     if (!SrcVT.isVector() || SrcVT.getVectorNumElements() != NumElems)
5074       return SDValue();
5075   }
5076
5077   if (V.getOpcode() == ISD::SCALAR_TO_VECTOR)
5078     return (Index == 0) ? V.getOperand(0)
5079                         : DAG.getUNDEF(VT.getVectorElementType());
5080
5081   if (V.getOpcode() == ISD::BUILD_VECTOR)
5082     return V.getOperand(Index);
5083
5084   return SDValue();
5085 }
5086
5087 /// Custom lower build_vector of v16i8.
5088 static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
5089                                        unsigned NumNonZero, unsigned NumZero,
5090                                        SelectionDAG &DAG,
5091                                        const X86Subtarget* Subtarget,
5092                                        const TargetLowering &TLI) {
5093   if (NumNonZero > 8)
5094     return SDValue();
5095
5096   SDLoc dl(Op);
5097   SDValue V;
5098   bool First = true;
5099
5100   // SSE4.1 - use PINSRB to insert each byte directly.
5101   if (Subtarget->hasSSE41()) {
5102     for (unsigned i = 0; i < 16; ++i) {
5103       bool isNonZero = (NonZeros & (1 << i)) != 0;
5104       if (isNonZero) {
5105         if (First) {
5106           if (NumZero)
5107             V = getZeroVector(MVT::v16i8, Subtarget, DAG, dl);
5108           else
5109             V = DAG.getUNDEF(MVT::v16i8);
5110           First = false;
5111         }
5112         V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
5113                         MVT::v16i8, V, Op.getOperand(i),
5114                         DAG.getIntPtrConstant(i, dl));
5115       }
5116     }
5117
5118     return V;
5119   }
5120
5121   // Pre-SSE4.1 - merge byte pairs and insert with PINSRW.
5122   for (unsigned i = 0; i < 16; ++i) {
5123     bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
5124     if (ThisIsNonZero && First) {
5125       if (NumZero)
5126         V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
5127       else
5128         V = DAG.getUNDEF(MVT::v8i16);
5129       First = false;
5130     }
5131
5132     if ((i & 1) != 0) {
5133       SDValue ThisElt, LastElt;
5134       bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
5135       if (LastIsNonZero) {
5136         LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
5137                               MVT::i16, Op.getOperand(i-1));
5138       }
5139       if (ThisIsNonZero) {
5140         ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
5141         ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
5142                               ThisElt, DAG.getConstant(8, dl, MVT::i8));
5143         if (LastIsNonZero)
5144           ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
5145       } else
5146         ThisElt = LastElt;
5147
5148       if (ThisElt.getNode())
5149         V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
5150                         DAG.getIntPtrConstant(i/2, dl));
5151     }
5152   }
5153
5154   return DAG.getBitcast(MVT::v16i8, V);
5155 }
5156
5157 /// Custom lower build_vector of v8i16.
5158 static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
5159                                      unsigned NumNonZero, unsigned NumZero,
5160                                      SelectionDAG &DAG,
5161                                      const X86Subtarget* Subtarget,
5162                                      const TargetLowering &TLI) {
5163   if (NumNonZero > 4)
5164     return SDValue();
5165
5166   SDLoc dl(Op);
5167   SDValue V;
5168   bool First = true;
5169   for (unsigned i = 0; i < 8; ++i) {
5170     bool isNonZero = (NonZeros & (1 << i)) != 0;
5171     if (isNonZero) {
5172       if (First) {
5173         if (NumZero)
5174           V = getZeroVector(MVT::v8i16, Subtarget, DAG, dl);
5175         else
5176           V = DAG.getUNDEF(MVT::v8i16);
5177         First = false;
5178       }
5179       V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
5180                       MVT::v8i16, V, Op.getOperand(i),
5181                       DAG.getIntPtrConstant(i, dl));
5182     }
5183   }
5184
5185   return V;
5186 }
5187
5188 /// Custom lower build_vector of v4i32 or v4f32.
5189 static SDValue LowerBuildVectorv4x32(SDValue Op, SelectionDAG &DAG,
5190                                      const X86Subtarget *Subtarget,
5191                                      const TargetLowering &TLI) {
5192   // Find all zeroable elements.
5193   std::bitset<4> Zeroable;
5194   for (int i=0; i < 4; ++i) {
5195     SDValue Elt = Op->getOperand(i);
5196     Zeroable[i] = (Elt.getOpcode() == ISD::UNDEF || X86::isZeroNode(Elt));
5197   }
5198   assert(Zeroable.size() - Zeroable.count() > 1 &&
5199          "We expect at least two non-zero elements!");
5200
5201   // We only know how to deal with build_vector nodes where elements are either
5202   // zeroable or extract_vector_elt with constant index.
5203   SDValue FirstNonZero;
5204   unsigned FirstNonZeroIdx;
5205   for (unsigned i=0; i < 4; ++i) {
5206     if (Zeroable[i])
5207       continue;
5208     SDValue Elt = Op->getOperand(i);
5209     if (Elt.getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
5210         !isa<ConstantSDNode>(Elt.getOperand(1)))
5211       return SDValue();
5212     // Make sure that this node is extracting from a 128-bit vector.
5213     MVT VT = Elt.getOperand(0).getSimpleValueType();
5214     if (!VT.is128BitVector())
5215       return SDValue();
5216     if (!FirstNonZero.getNode()) {
5217       FirstNonZero = Elt;
5218       FirstNonZeroIdx = i;
5219     }
5220   }
5221
5222   assert(FirstNonZero.getNode() && "Unexpected build vector of all zeros!");
5223   SDValue V1 = FirstNonZero.getOperand(0);
5224   MVT VT = V1.getSimpleValueType();
5225
5226   // See if this build_vector can be lowered as a blend with zero.
5227   SDValue Elt;
5228   unsigned EltMaskIdx, EltIdx;
5229   int Mask[4];
5230   for (EltIdx = 0; EltIdx < 4; ++EltIdx) {
5231     if (Zeroable[EltIdx]) {
5232       // The zero vector will be on the right hand side.
5233       Mask[EltIdx] = EltIdx+4;
5234       continue;
5235     }
5236
5237     Elt = Op->getOperand(EltIdx);
5238     // By construction, Elt is a EXTRACT_VECTOR_ELT with constant index.
5239     EltMaskIdx = cast<ConstantSDNode>(Elt.getOperand(1))->getZExtValue();
5240     if (Elt.getOperand(0) != V1 || EltMaskIdx != EltIdx)
5241       break;
5242     Mask[EltIdx] = EltIdx;
5243   }
5244
5245   if (EltIdx == 4) {
5246     // Let the shuffle legalizer deal with blend operations.
5247     SDValue VZero = getZeroVector(VT, Subtarget, DAG, SDLoc(Op));
5248     if (V1.getSimpleValueType() != VT)
5249       V1 = DAG.getNode(ISD::BITCAST, SDLoc(V1), VT, V1);
5250     return DAG.getVectorShuffle(VT, SDLoc(V1), V1, VZero, &Mask[0]);
5251   }
5252
5253   // See if we can lower this build_vector to a INSERTPS.
5254   if (!Subtarget->hasSSE41())
5255     return SDValue();
5256
5257   SDValue V2 = Elt.getOperand(0);
5258   if (Elt == FirstNonZero && EltIdx == FirstNonZeroIdx)
5259     V1 = SDValue();
5260
5261   bool CanFold = true;
5262   for (unsigned i = EltIdx + 1; i < 4 && CanFold; ++i) {
5263     if (Zeroable[i])
5264       continue;
5265
5266     SDValue Current = Op->getOperand(i);
5267     SDValue SrcVector = Current->getOperand(0);
5268     if (!V1.getNode())
5269       V1 = SrcVector;
5270     CanFold = SrcVector == V1 &&
5271       cast<ConstantSDNode>(Current.getOperand(1))->getZExtValue() == i;
5272   }
5273
5274   if (!CanFold)
5275     return SDValue();
5276
5277   assert(V1.getNode() && "Expected at least two non-zero elements!");
5278   if (V1.getSimpleValueType() != MVT::v4f32)
5279     V1 = DAG.getNode(ISD::BITCAST, SDLoc(V1), MVT::v4f32, V1);
5280   if (V2.getSimpleValueType() != MVT::v4f32)
5281     V2 = DAG.getNode(ISD::BITCAST, SDLoc(V2), MVT::v4f32, V2);
5282
5283   // Ok, we can emit an INSERTPS instruction.
5284   unsigned ZMask = Zeroable.to_ulong();
5285
5286   unsigned InsertPSMask = EltMaskIdx << 6 | EltIdx << 4 | ZMask;
5287   assert((InsertPSMask & ~0xFFu) == 0 && "Invalid mask!");
5288   SDLoc DL(Op);
5289   SDValue Result = DAG.getNode(X86ISD::INSERTPS, DL, MVT::v4f32, V1, V2,
5290                                DAG.getIntPtrConstant(InsertPSMask, DL));
5291   return DAG.getBitcast(VT, Result);
5292 }
5293
5294 /// Return a vector logical shift node.
5295 static SDValue getVShift(bool isLeft, EVT VT, SDValue SrcOp,
5296                          unsigned NumBits, SelectionDAG &DAG,
5297                          const TargetLowering &TLI, SDLoc dl) {
5298   assert(VT.is128BitVector() && "Unknown type for VShift");
5299   MVT ShVT = MVT::v2i64;
5300   unsigned Opc = isLeft ? X86ISD::VSHLDQ : X86ISD::VSRLDQ;
5301   SrcOp = DAG.getBitcast(ShVT, SrcOp);
5302   MVT ScalarShiftTy = TLI.getScalarShiftAmountTy(DAG.getDataLayout(), VT);
5303   assert(NumBits % 8 == 0 && "Only support byte sized shifts");
5304   SDValue ShiftVal = DAG.getConstant(NumBits/8, dl, ScalarShiftTy);
5305   return DAG.getBitcast(VT, DAG.getNode(Opc, dl, ShVT, SrcOp, ShiftVal));
5306 }
5307
5308 static SDValue
5309 LowerAsSplatVectorLoad(SDValue SrcOp, MVT VT, SDLoc dl, SelectionDAG &DAG) {
5310
5311   // Check if the scalar load can be widened into a vector load. And if
5312   // the address is "base + cst" see if the cst can be "absorbed" into
5313   // the shuffle mask.
5314   if (LoadSDNode *LD = dyn_cast<LoadSDNode>(SrcOp)) {
5315     SDValue Ptr = LD->getBasePtr();
5316     if (!ISD::isNormalLoad(LD) || LD->isVolatile())
5317       return SDValue();
5318     EVT PVT = LD->getValueType(0);
5319     if (PVT != MVT::i32 && PVT != MVT::f32)
5320       return SDValue();
5321
5322     int FI = -1;
5323     int64_t Offset = 0;
5324     if (FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(Ptr)) {
5325       FI = FINode->getIndex();
5326       Offset = 0;
5327     } else if (DAG.isBaseWithConstantOffset(Ptr) &&
5328                isa<FrameIndexSDNode>(Ptr.getOperand(0))) {
5329       FI = cast<FrameIndexSDNode>(Ptr.getOperand(0))->getIndex();
5330       Offset = Ptr.getConstantOperandVal(1);
5331       Ptr = Ptr.getOperand(0);
5332     } else {
5333       return SDValue();
5334     }
5335
5336     // FIXME: 256-bit vector instructions don't require a strict alignment,
5337     // improve this code to support it better.
5338     unsigned RequiredAlign = VT.getSizeInBits()/8;
5339     SDValue Chain = LD->getChain();
5340     // Make sure the stack object alignment is at least 16 or 32.
5341     MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
5342     if (DAG.InferPtrAlignment(Ptr) < RequiredAlign) {
5343       if (MFI->isFixedObjectIndex(FI)) {
5344         // Can't change the alignment. FIXME: It's possible to compute
5345         // the exact stack offset and reference FI + adjust offset instead.
5346         // If someone *really* cares about this. That's the way to implement it.
5347         return SDValue();
5348       } else {
5349         MFI->setObjectAlignment(FI, RequiredAlign);
5350       }
5351     }
5352
5353     // (Offset % 16 or 32) must be multiple of 4. Then address is then
5354     // Ptr + (Offset & ~15).
5355     if (Offset < 0)
5356       return SDValue();
5357     if ((Offset % RequiredAlign) & 3)
5358       return SDValue();
5359     int64_t StartOffset = Offset & ~int64_t(RequiredAlign - 1);
5360     if (StartOffset) {
5361       SDLoc DL(Ptr);
5362       Ptr = DAG.getNode(ISD::ADD, DL, Ptr.getValueType(), Ptr,
5363                         DAG.getConstant(StartOffset, DL, Ptr.getValueType()));
5364     }
5365
5366     int EltNo = (Offset - StartOffset) >> 2;
5367     unsigned NumElems = VT.getVectorNumElements();
5368
5369     EVT NVT = EVT::getVectorVT(*DAG.getContext(), PVT, NumElems);
5370     SDValue V1 = DAG.getLoad(NVT, dl, Chain, Ptr,
5371                              LD->getPointerInfo().getWithOffset(StartOffset),
5372                              false, false, false, 0);
5373
5374     SmallVector<int, 8> Mask(NumElems, EltNo);
5375
5376     return DAG.getVectorShuffle(NVT, dl, V1, DAG.getUNDEF(NVT), &Mask[0]);
5377   }
5378
5379   return SDValue();
5380 }
5381
5382 /// Given the initializing elements 'Elts' of a vector of type 'VT', see if the
5383 /// elements can be replaced by a single large load which has the same value as
5384 /// a build_vector or insert_subvector whose loaded operands are 'Elts'.
5385 ///
5386 /// Example: <load i32 *a, load i32 *a+4, undef, undef> -> zextload a
5387 ///
5388 /// FIXME: we'd also like to handle the case where the last elements are zero
5389 /// rather than undef via VZEXT_LOAD, but we do not detect that case today.
5390 /// There's even a handy isZeroNode for that purpose.
5391 static SDValue EltsFromConsecutiveLoads(EVT VT, ArrayRef<SDValue> Elts,
5392                                         SDLoc &DL, SelectionDAG &DAG,
5393                                         bool isAfterLegalize) {
5394   unsigned NumElems = Elts.size();
5395
5396   LoadSDNode *LDBase = nullptr;
5397   unsigned LastLoadedElt = -1U;
5398
5399   // For each element in the initializer, see if we've found a load or an undef.
5400   // If we don't find an initial load element, or later load elements are
5401   // non-consecutive, bail out.
5402   for (unsigned i = 0; i < NumElems; ++i) {
5403     SDValue Elt = Elts[i];
5404     // Look through a bitcast.
5405     if (Elt.getNode() && Elt.getOpcode() == ISD::BITCAST)
5406       Elt = Elt.getOperand(0);
5407     if (!Elt.getNode() ||
5408         (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
5409       return SDValue();
5410     if (!LDBase) {
5411       if (Elt.getNode()->getOpcode() == ISD::UNDEF)
5412         return SDValue();
5413       LDBase = cast<LoadSDNode>(Elt.getNode());
5414       LastLoadedElt = i;
5415       continue;
5416     }
5417     if (Elt.getOpcode() == ISD::UNDEF)
5418       continue;
5419
5420     LoadSDNode *LD = cast<LoadSDNode>(Elt);
5421     EVT LdVT = Elt.getValueType();
5422     // Each loaded element must be the correct fractional portion of the
5423     // requested vector load.
5424     if (LdVT.getSizeInBits() != VT.getSizeInBits() / NumElems)
5425       return SDValue();
5426     if (!DAG.isConsecutiveLoad(LD, LDBase, LdVT.getSizeInBits() / 8, i))
5427       return SDValue();
5428     LastLoadedElt = i;
5429   }
5430
5431   // If we have found an entire vector of loads and undefs, then return a large
5432   // load of the entire vector width starting at the base pointer.  If we found
5433   // consecutive loads for the low half, generate a vzext_load node.
5434   if (LastLoadedElt == NumElems - 1) {
5435     assert(LDBase && "Did not find base load for merging consecutive loads");
5436     EVT EltVT = LDBase->getValueType(0);
5437     // Ensure that the input vector size for the merged loads matches the
5438     // cumulative size of the input elements.
5439     if (VT.getSizeInBits() != EltVT.getSizeInBits() * NumElems)
5440       return SDValue();
5441
5442     if (isAfterLegalize &&
5443         !DAG.getTargetLoweringInfo().isOperationLegal(ISD::LOAD, VT))
5444       return SDValue();
5445
5446     SDValue NewLd = SDValue();
5447
5448     NewLd = DAG.getLoad(VT, DL, LDBase->getChain(), LDBase->getBasePtr(),
5449                         LDBase->getPointerInfo(), LDBase->isVolatile(),
5450                         LDBase->isNonTemporal(), LDBase->isInvariant(),
5451                         LDBase->getAlignment());
5452
5453     if (LDBase->hasAnyUseOfValue(1)) {
5454       SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5455                                      SDValue(LDBase, 1),
5456                                      SDValue(NewLd.getNode(), 1));
5457       DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
5458       DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
5459                              SDValue(NewLd.getNode(), 1));
5460     }
5461
5462     return NewLd;
5463   }
5464
5465   //TODO: The code below fires only for for loading the low v2i32 / v2f32
5466   //of a v4i32 / v4f32. It's probably worth generalizing.
5467   EVT EltVT = VT.getVectorElementType();
5468   if (NumElems == 4 && LastLoadedElt == 1 && (EltVT.getSizeInBits() == 32) &&
5469       DAG.getTargetLoweringInfo().isTypeLegal(MVT::v2i64)) {
5470     SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
5471     SDValue Ops[] = { LDBase->getChain(), LDBase->getBasePtr() };
5472     SDValue ResNode =
5473         DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, DL, Tys, Ops, MVT::i64,
5474                                 LDBase->getPointerInfo(),
5475                                 LDBase->getAlignment(),
5476                                 false/*isVolatile*/, true/*ReadMem*/,
5477                                 false/*WriteMem*/);
5478
5479     // Make sure the newly-created LOAD is in the same position as LDBase in
5480     // terms of dependency. We create a TokenFactor for LDBase and ResNode, and
5481     // update uses of LDBase's output chain to use the TokenFactor.
5482     if (LDBase->hasAnyUseOfValue(1)) {
5483       SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
5484                              SDValue(LDBase, 1), SDValue(ResNode.getNode(), 1));
5485       DAG.ReplaceAllUsesOfValueWith(SDValue(LDBase, 1), NewChain);
5486       DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(LDBase, 1),
5487                              SDValue(ResNode.getNode(), 1));
5488     }
5489
5490     return DAG.getBitcast(VT, ResNode);
5491   }
5492   return SDValue();
5493 }
5494
5495 /// LowerVectorBroadcast - Attempt to use the vbroadcast instruction
5496 /// to generate a splat value for the following cases:
5497 /// 1. A splat BUILD_VECTOR which uses a single scalar load, or a constant.
5498 /// 2. A splat shuffle which uses a scalar_to_vector node which comes from
5499 /// a scalar load, or a constant.
5500 /// The VBROADCAST node is returned when a pattern is found,
5501 /// or SDValue() otherwise.
5502 static SDValue LowerVectorBroadcast(SDValue Op, const X86Subtarget* Subtarget,
5503                                     SelectionDAG &DAG) {
5504   // VBROADCAST requires AVX.
5505   // TODO: Splats could be generated for non-AVX CPUs using SSE
5506   // instructions, but there's less potential gain for only 128-bit vectors.
5507   if (!Subtarget->hasAVX())
5508     return SDValue();
5509
5510   MVT VT = Op.getSimpleValueType();
5511   SDLoc dl(Op);
5512
5513   assert((VT.is128BitVector() || VT.is256BitVector() || VT.is512BitVector()) &&
5514          "Unsupported vector type for broadcast.");
5515
5516   SDValue Ld;
5517   bool ConstSplatVal;
5518
5519   switch (Op.getOpcode()) {
5520     default:
5521       // Unknown pattern found.
5522       return SDValue();
5523
5524     case ISD::BUILD_VECTOR: {
5525       auto *BVOp = cast<BuildVectorSDNode>(Op.getNode());
5526       BitVector UndefElements;
5527       SDValue Splat = BVOp->getSplatValue(&UndefElements);
5528
5529       // We need a splat of a single value to use broadcast, and it doesn't
5530       // make any sense if the value is only in one element of the vector.
5531       if (!Splat || (VT.getVectorNumElements() - UndefElements.count()) <= 1)
5532         return SDValue();
5533
5534       Ld = Splat;
5535       ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5536                        Ld.getOpcode() == ISD::ConstantFP);
5537
5538       // Make sure that all of the users of a non-constant load are from the
5539       // BUILD_VECTOR node.
5540       if (!ConstSplatVal && !BVOp->isOnlyUserOf(Ld.getNode()))
5541         return SDValue();
5542       break;
5543     }
5544
5545     case ISD::VECTOR_SHUFFLE: {
5546       ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
5547
5548       // Shuffles must have a splat mask where the first element is
5549       // broadcasted.
5550       if ((!SVOp->isSplat()) || SVOp->getMaskElt(0) != 0)
5551         return SDValue();
5552
5553       SDValue Sc = Op.getOperand(0);
5554       if (Sc.getOpcode() != ISD::SCALAR_TO_VECTOR &&
5555           Sc.getOpcode() != ISD::BUILD_VECTOR) {
5556
5557         if (!Subtarget->hasInt256())
5558           return SDValue();
5559
5560         // Use the register form of the broadcast instruction available on AVX2.
5561         if (VT.getSizeInBits() >= 256)
5562           Sc = Extract128BitVector(Sc, 0, DAG, dl);
5563         return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Sc);
5564       }
5565
5566       Ld = Sc.getOperand(0);
5567       ConstSplatVal = (Ld.getOpcode() == ISD::Constant ||
5568                        Ld.getOpcode() == ISD::ConstantFP);
5569
5570       // The scalar_to_vector node and the suspected
5571       // load node must have exactly one user.
5572       // Constants may have multiple users.
5573
5574       // AVX-512 has register version of the broadcast
5575       bool hasRegVer = Subtarget->hasAVX512() && VT.is512BitVector() &&
5576         Ld.getValueType().getSizeInBits() >= 32;
5577       if (!ConstSplatVal && ((!Sc.hasOneUse() || !Ld.hasOneUse()) &&
5578           !hasRegVer))
5579         return SDValue();
5580       break;
5581     }
5582   }
5583
5584   unsigned ScalarSize = Ld.getValueType().getSizeInBits();
5585   bool IsGE256 = (VT.getSizeInBits() >= 256);
5586
5587   // When optimizing for size, generate up to 5 extra bytes for a broadcast
5588   // instruction to save 8 or more bytes of constant pool data.
5589   // TODO: If multiple splats are generated to load the same constant,
5590   // it may be detrimental to overall size. There needs to be a way to detect
5591   // that condition to know if this is truly a size win.
5592   bool OptForSize = DAG.getMachineFunction().getFunction()->optForSize();
5593
5594   // Handle broadcasting a single constant scalar from the constant pool
5595   // into a vector.
5596   // On Sandybridge (no AVX2), it is still better to load a constant vector
5597   // from the constant pool and not to broadcast it from a scalar.
5598   // But override that restriction when optimizing for size.
5599   // TODO: Check if splatting is recommended for other AVX-capable CPUs.
5600   if (ConstSplatVal && (Subtarget->hasAVX2() || OptForSize)) {
5601     EVT CVT = Ld.getValueType();
5602     assert(!CVT.isVector() && "Must not broadcast a vector type");
5603
5604     // Splat f32, i32, v4f64, v4i64 in all cases with AVX2.
5605     // For size optimization, also splat v2f64 and v2i64, and for size opt
5606     // with AVX2, also splat i8 and i16.
5607     // With pattern matching, the VBROADCAST node may become a VMOVDDUP.
5608     if (ScalarSize == 32 || (IsGE256 && ScalarSize == 64) ||
5609         (OptForSize && (ScalarSize == 64 || Subtarget->hasAVX2()))) {
5610       const Constant *C = nullptr;
5611       if (ConstantSDNode *CI = dyn_cast<ConstantSDNode>(Ld))
5612         C = CI->getConstantIntValue();
5613       else if (ConstantFPSDNode *CF = dyn_cast<ConstantFPSDNode>(Ld))
5614         C = CF->getConstantFPValue();
5615
5616       assert(C && "Invalid constant type");
5617
5618       const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5619       SDValue CP =
5620           DAG.getConstantPool(C, TLI.getPointerTy(DAG.getDataLayout()));
5621       unsigned Alignment = cast<ConstantPoolSDNode>(CP)->getAlignment();
5622       Ld = DAG.getLoad(
5623           CVT, dl, DAG.getEntryNode(), CP,
5624           MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
5625           false, false, Alignment);
5626
5627       return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5628     }
5629   }
5630
5631   bool IsLoad = ISD::isNormalLoad(Ld.getNode());
5632
5633   // Handle AVX2 in-register broadcasts.
5634   if (!IsLoad && Subtarget->hasInt256() &&
5635       (ScalarSize == 32 || (IsGE256 && ScalarSize == 64)))
5636     return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5637
5638   // The scalar source must be a normal load.
5639   if (!IsLoad)
5640     return SDValue();
5641
5642   if (ScalarSize == 32 || (IsGE256 && ScalarSize == 64) ||
5643       (Subtarget->hasVLX() && ScalarSize == 64))
5644     return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5645
5646   // The integer check is needed for the 64-bit into 128-bit so it doesn't match
5647   // double since there is no vbroadcastsd xmm
5648   if (Subtarget->hasInt256() && Ld.getValueType().isInteger()) {
5649     if (ScalarSize == 8 || ScalarSize == 16 || ScalarSize == 64)
5650       return DAG.getNode(X86ISD::VBROADCAST, dl, VT, Ld);
5651   }
5652
5653   // Unsupported broadcast.
5654   return SDValue();
5655 }
5656
5657 /// \brief For an EXTRACT_VECTOR_ELT with a constant index return the real
5658 /// underlying vector and index.
5659 ///
5660 /// Modifies \p ExtractedFromVec to the real vector and returns the real
5661 /// index.
5662 static int getUnderlyingExtractedFromVec(SDValue &ExtractedFromVec,
5663                                          SDValue ExtIdx) {
5664   int Idx = cast<ConstantSDNode>(ExtIdx)->getZExtValue();
5665   if (!isa<ShuffleVectorSDNode>(ExtractedFromVec))
5666     return Idx;
5667
5668   // For 256-bit vectors, LowerEXTRACT_VECTOR_ELT_SSE4 may have already
5669   // lowered this:
5670   //   (extract_vector_elt (v8f32 %vreg1), Constant<6>)
5671   // to:
5672   //   (extract_vector_elt (vector_shuffle<2,u,u,u>
5673   //                           (extract_subvector (v8f32 %vreg0), Constant<4>),
5674   //                           undef)
5675   //                       Constant<0>)
5676   // In this case the vector is the extract_subvector expression and the index
5677   // is 2, as specified by the shuffle.
5678   ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(ExtractedFromVec);
5679   SDValue ShuffleVec = SVOp->getOperand(0);
5680   MVT ShuffleVecVT = ShuffleVec.getSimpleValueType();
5681   assert(ShuffleVecVT.getVectorElementType() ==
5682          ExtractedFromVec.getSimpleValueType().getVectorElementType());
5683
5684   int ShuffleIdx = SVOp->getMaskElt(Idx);
5685   if (isUndefOrInRange(ShuffleIdx, 0, ShuffleVecVT.getVectorNumElements())) {
5686     ExtractedFromVec = ShuffleVec;
5687     return ShuffleIdx;
5688   }
5689   return Idx;
5690 }
5691
5692 static SDValue buildFromShuffleMostly(SDValue Op, SelectionDAG &DAG) {
5693   MVT VT = Op.getSimpleValueType();
5694
5695   // Skip if insert_vec_elt is not supported.
5696   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
5697   if (!TLI.isOperationLegalOrCustom(ISD::INSERT_VECTOR_ELT, VT))
5698     return SDValue();
5699
5700   SDLoc DL(Op);
5701   unsigned NumElems = Op.getNumOperands();
5702
5703   SDValue VecIn1;
5704   SDValue VecIn2;
5705   SmallVector<unsigned, 4> InsertIndices;
5706   SmallVector<int, 8> Mask(NumElems, -1);
5707
5708   for (unsigned i = 0; i != NumElems; ++i) {
5709     unsigned Opc = Op.getOperand(i).getOpcode();
5710
5711     if (Opc == ISD::UNDEF)
5712       continue;
5713
5714     if (Opc != ISD::EXTRACT_VECTOR_ELT) {
5715       // Quit if more than 1 elements need inserting.
5716       if (InsertIndices.size() > 1)
5717         return SDValue();
5718
5719       InsertIndices.push_back(i);
5720       continue;
5721     }
5722
5723     SDValue ExtractedFromVec = Op.getOperand(i).getOperand(0);
5724     SDValue ExtIdx = Op.getOperand(i).getOperand(1);
5725     // Quit if non-constant index.
5726     if (!isa<ConstantSDNode>(ExtIdx))
5727       return SDValue();
5728     int Idx = getUnderlyingExtractedFromVec(ExtractedFromVec, ExtIdx);
5729
5730     // Quit if extracted from vector of different type.
5731     if (ExtractedFromVec.getValueType() != VT)
5732       return SDValue();
5733
5734     if (!VecIn1.getNode())
5735       VecIn1 = ExtractedFromVec;
5736     else if (VecIn1 != ExtractedFromVec) {
5737       if (!VecIn2.getNode())
5738         VecIn2 = ExtractedFromVec;
5739       else if (VecIn2 != ExtractedFromVec)
5740         // Quit if more than 2 vectors to shuffle
5741         return SDValue();
5742     }
5743
5744     if (ExtractedFromVec == VecIn1)
5745       Mask[i] = Idx;
5746     else if (ExtractedFromVec == VecIn2)
5747       Mask[i] = Idx + NumElems;
5748   }
5749
5750   if (!VecIn1.getNode())
5751     return SDValue();
5752
5753   VecIn2 = VecIn2.getNode() ? VecIn2 : DAG.getUNDEF(VT);
5754   SDValue NV = DAG.getVectorShuffle(VT, DL, VecIn1, VecIn2, &Mask[0]);
5755   for (unsigned i = 0, e = InsertIndices.size(); i != e; ++i) {
5756     unsigned Idx = InsertIndices[i];
5757     NV = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, NV, Op.getOperand(Idx),
5758                      DAG.getIntPtrConstant(Idx, DL));
5759   }
5760
5761   return NV;
5762 }
5763
5764 static SDValue ConvertI1VectorToInteger(SDValue Op, SelectionDAG &DAG) {
5765   assert(ISD::isBuildVectorOfConstantSDNodes(Op.getNode()) &&
5766          Op.getScalarValueSizeInBits() == 1 &&
5767          "Can not convert non-constant vector");
5768   uint64_t Immediate = 0;
5769   for (unsigned idx = 0, e = Op.getNumOperands(); idx < e; ++idx) {
5770     SDValue In = Op.getOperand(idx);
5771     if (In.getOpcode() != ISD::UNDEF)
5772       Immediate |= cast<ConstantSDNode>(In)->getZExtValue() << idx;
5773   }
5774   SDLoc dl(Op);
5775   MVT VT =
5776    MVT::getIntegerVT(std::max((int)Op.getValueType().getSizeInBits(), 8));
5777   return DAG.getConstant(Immediate, dl, VT);
5778 }
5779 // Lower BUILD_VECTOR operation for v8i1 and v16i1 types.
5780 SDValue
5781 X86TargetLowering::LowerBUILD_VECTORvXi1(SDValue Op, SelectionDAG &DAG) const {
5782
5783   MVT VT = Op.getSimpleValueType();
5784   assert((VT.getVectorElementType() == MVT::i1) &&
5785          "Unexpected type in LowerBUILD_VECTORvXi1!");
5786
5787   SDLoc dl(Op);
5788   if (ISD::isBuildVectorAllZeros(Op.getNode())) {
5789     SDValue Cst = DAG.getTargetConstant(0, dl, MVT::i1);
5790     SmallVector<SDValue, 16> Ops(VT.getVectorNumElements(), Cst);
5791     return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Ops);
5792   }
5793
5794   if (ISD::isBuildVectorAllOnes(Op.getNode())) {
5795     SDValue Cst = DAG.getTargetConstant(1, dl, MVT::i1);
5796     SmallVector<SDValue, 16> Ops(VT.getVectorNumElements(), Cst);
5797     return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Ops);
5798   }
5799
5800   if (ISD::isBuildVectorOfConstantSDNodes(Op.getNode())) {
5801     SDValue Imm = ConvertI1VectorToInteger(Op, DAG);
5802     if (Imm.getValueSizeInBits() == VT.getSizeInBits())
5803       return DAG.getBitcast(VT, Imm);
5804     SDValue ExtVec = DAG.getBitcast(MVT::v8i1, Imm);
5805     return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, ExtVec,
5806                         DAG.getIntPtrConstant(0, dl));
5807   }
5808
5809   // Vector has one or more non-const elements
5810   uint64_t Immediate = 0;
5811   SmallVector<unsigned, 16> NonConstIdx;
5812   bool IsSplat = true;
5813   bool HasConstElts = false;
5814   int SplatIdx = -1;
5815   for (unsigned idx = 0, e = Op.getNumOperands(); idx < e; ++idx) {
5816     SDValue In = Op.getOperand(idx);
5817     if (In.getOpcode() == ISD::UNDEF)
5818       continue;
5819     if (!isa<ConstantSDNode>(In))
5820       NonConstIdx.push_back(idx);
5821     else {
5822       Immediate |= cast<ConstantSDNode>(In)->getZExtValue() << idx;
5823       HasConstElts = true;
5824     }
5825     if (SplatIdx == -1)
5826       SplatIdx = idx;
5827     else if (In != Op.getOperand(SplatIdx))
5828       IsSplat = false;
5829   }
5830
5831   // for splat use " (select i1 splat_elt, all-ones, all-zeroes)"
5832   if (IsSplat)
5833     return DAG.getNode(ISD::SELECT, dl, VT, Op.getOperand(SplatIdx),
5834                        DAG.getConstant(1, dl, VT),
5835                        DAG.getConstant(0, dl, VT));
5836
5837   // insert elements one by one
5838   SDValue DstVec;
5839   SDValue Imm;
5840   if (Immediate) {
5841     MVT ImmVT = MVT::getIntegerVT(std::max((int)VT.getSizeInBits(), 8));
5842     Imm = DAG.getConstant(Immediate, dl, ImmVT);
5843   }
5844   else if (HasConstElts)
5845     Imm = DAG.getConstant(0, dl, VT);
5846   else
5847     Imm = DAG.getUNDEF(VT);
5848   if (Imm.getValueSizeInBits() == VT.getSizeInBits())
5849     DstVec = DAG.getBitcast(VT, Imm);
5850   else {
5851     SDValue ExtVec = DAG.getBitcast(MVT::v8i1, Imm);
5852     DstVec = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, ExtVec,
5853                          DAG.getIntPtrConstant(0, dl));
5854   }
5855
5856   for (unsigned i = 0; i < NonConstIdx.size(); ++i) {
5857     unsigned InsertIdx = NonConstIdx[i];
5858     DstVec = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, DstVec,
5859                          Op.getOperand(InsertIdx),
5860                          DAG.getIntPtrConstant(InsertIdx, dl));
5861   }
5862   return DstVec;
5863 }
5864
5865 /// \brief Return true if \p N implements a horizontal binop and return the
5866 /// operands for the horizontal binop into V0 and V1.
5867 ///
5868 /// This is a helper function of LowerToHorizontalOp().
5869 /// This function checks that the build_vector \p N in input implements a
5870 /// horizontal operation. Parameter \p Opcode defines the kind of horizontal
5871 /// operation to match.
5872 /// For example, if \p Opcode is equal to ISD::ADD, then this function
5873 /// checks if \p N implements a horizontal arithmetic add; if instead \p Opcode
5874 /// is equal to ISD::SUB, then this function checks if this is a horizontal
5875 /// arithmetic sub.
5876 ///
5877 /// This function only analyzes elements of \p N whose indices are
5878 /// in range [BaseIdx, LastIdx).
5879 static bool isHorizontalBinOp(const BuildVectorSDNode *N, unsigned Opcode,
5880                               SelectionDAG &DAG,
5881                               unsigned BaseIdx, unsigned LastIdx,
5882                               SDValue &V0, SDValue &V1) {
5883   EVT VT = N->getValueType(0);
5884
5885   assert(BaseIdx * 2 <= LastIdx && "Invalid Indices in input!");
5886   assert(VT.isVector() && VT.getVectorNumElements() >= LastIdx &&
5887          "Invalid Vector in input!");
5888
5889   bool IsCommutable = (Opcode == ISD::ADD || Opcode == ISD::FADD);
5890   bool CanFold = true;
5891   unsigned ExpectedVExtractIdx = BaseIdx;
5892   unsigned NumElts = LastIdx - BaseIdx;
5893   V0 = DAG.getUNDEF(VT);
5894   V1 = DAG.getUNDEF(VT);
5895
5896   // Check if N implements a horizontal binop.
5897   for (unsigned i = 0, e = NumElts; i != e && CanFold; ++i) {
5898     SDValue Op = N->getOperand(i + BaseIdx);
5899
5900     // Skip UNDEFs.
5901     if (Op->getOpcode() == ISD::UNDEF) {
5902       // Update the expected vector extract index.
5903       if (i * 2 == NumElts)
5904         ExpectedVExtractIdx = BaseIdx;
5905       ExpectedVExtractIdx += 2;
5906       continue;
5907     }
5908
5909     CanFold = Op->getOpcode() == Opcode && Op->hasOneUse();
5910
5911     if (!CanFold)
5912       break;
5913
5914     SDValue Op0 = Op.getOperand(0);
5915     SDValue Op1 = Op.getOperand(1);
5916
5917     // Try to match the following pattern:
5918     // (BINOP (extract_vector_elt A, I), (extract_vector_elt A, I+1))
5919     CanFold = (Op0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5920         Op1.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
5921         Op0.getOperand(0) == Op1.getOperand(0) &&
5922         isa<ConstantSDNode>(Op0.getOperand(1)) &&
5923         isa<ConstantSDNode>(Op1.getOperand(1)));
5924     if (!CanFold)
5925       break;
5926
5927     unsigned I0 = cast<ConstantSDNode>(Op0.getOperand(1))->getZExtValue();
5928     unsigned I1 = cast<ConstantSDNode>(Op1.getOperand(1))->getZExtValue();
5929
5930     if (i * 2 < NumElts) {
5931       if (V0.getOpcode() == ISD::UNDEF) {
5932         V0 = Op0.getOperand(0);
5933         if (V0.getValueType() != VT)
5934           return false;
5935       }
5936     } else {
5937       if (V1.getOpcode() == ISD::UNDEF) {
5938         V1 = Op0.getOperand(0);
5939         if (V1.getValueType() != VT)
5940           return false;
5941       }
5942       if (i * 2 == NumElts)
5943         ExpectedVExtractIdx = BaseIdx;
5944     }
5945
5946     SDValue Expected = (i * 2 < NumElts) ? V0 : V1;
5947     if (I0 == ExpectedVExtractIdx)
5948       CanFold = I1 == I0 + 1 && Op0.getOperand(0) == Expected;
5949     else if (IsCommutable && I1 == ExpectedVExtractIdx) {
5950       // Try to match the following dag sequence:
5951       // (BINOP (extract_vector_elt A, I+1), (extract_vector_elt A, I))
5952       CanFold = I0 == I1 + 1 && Op1.getOperand(0) == Expected;
5953     } else
5954       CanFold = false;
5955
5956     ExpectedVExtractIdx += 2;
5957   }
5958
5959   return CanFold;
5960 }
5961
5962 /// \brief Emit a sequence of two 128-bit horizontal add/sub followed by
5963 /// a concat_vector.
5964 ///
5965 /// This is a helper function of LowerToHorizontalOp().
5966 /// This function expects two 256-bit vectors called V0 and V1.
5967 /// At first, each vector is split into two separate 128-bit vectors.
5968 /// Then, the resulting 128-bit vectors are used to implement two
5969 /// horizontal binary operations.
5970 ///
5971 /// The kind of horizontal binary operation is defined by \p X86Opcode.
5972 ///
5973 /// \p Mode specifies how the 128-bit parts of V0 and V1 are passed in input to
5974 /// the two new horizontal binop.
5975 /// When Mode is set, the first horizontal binop dag node would take as input
5976 /// the lower 128-bit of V0 and the upper 128-bit of V0. The second
5977 /// horizontal binop dag node would take as input the lower 128-bit of V1
5978 /// and the upper 128-bit of V1.
5979 ///   Example:
5980 ///     HADD V0_LO, V0_HI
5981 ///     HADD V1_LO, V1_HI
5982 ///
5983 /// Otherwise, the first horizontal binop dag node takes as input the lower
5984 /// 128-bit of V0 and the lower 128-bit of V1, and the second horizontal binop
5985 /// dag node takes the upper 128-bit of V0 and the upper 128-bit of V1.
5986 ///   Example:
5987 ///     HADD V0_LO, V1_LO
5988 ///     HADD V0_HI, V1_HI
5989 ///
5990 /// If \p isUndefLO is set, then the algorithm propagates UNDEF to the lower
5991 /// 128-bits of the result. If \p isUndefHI is set, then UNDEF is propagated to
5992 /// the upper 128-bits of the result.
5993 static SDValue ExpandHorizontalBinOp(const SDValue &V0, const SDValue &V1,
5994                                      SDLoc DL, SelectionDAG &DAG,
5995                                      unsigned X86Opcode, bool Mode,
5996                                      bool isUndefLO, bool isUndefHI) {
5997   EVT VT = V0.getValueType();
5998   assert(VT.is256BitVector() && VT == V1.getValueType() &&
5999          "Invalid nodes in input!");
6000
6001   unsigned NumElts = VT.getVectorNumElements();
6002   SDValue V0_LO = Extract128BitVector(V0, 0, DAG, DL);
6003   SDValue V0_HI = Extract128BitVector(V0, NumElts/2, DAG, DL);
6004   SDValue V1_LO = Extract128BitVector(V1, 0, DAG, DL);
6005   SDValue V1_HI = Extract128BitVector(V1, NumElts/2, DAG, DL);
6006   EVT NewVT = V0_LO.getValueType();
6007
6008   SDValue LO = DAG.getUNDEF(NewVT);
6009   SDValue HI = DAG.getUNDEF(NewVT);
6010
6011   if (Mode) {
6012     // Don't emit a horizontal binop if the result is expected to be UNDEF.
6013     if (!isUndefLO && V0->getOpcode() != ISD::UNDEF)
6014       LO = DAG.getNode(X86Opcode, DL, NewVT, V0_LO, V0_HI);
6015     if (!isUndefHI && V1->getOpcode() != ISD::UNDEF)
6016       HI = DAG.getNode(X86Opcode, DL, NewVT, V1_LO, V1_HI);
6017   } else {
6018     // Don't emit a horizontal binop if the result is expected to be UNDEF.
6019     if (!isUndefLO && (V0_LO->getOpcode() != ISD::UNDEF ||
6020                        V1_LO->getOpcode() != ISD::UNDEF))
6021       LO = DAG.getNode(X86Opcode, DL, NewVT, V0_LO, V1_LO);
6022
6023     if (!isUndefHI && (V0_HI->getOpcode() != ISD::UNDEF ||
6024                        V1_HI->getOpcode() != ISD::UNDEF))
6025       HI = DAG.getNode(X86Opcode, DL, NewVT, V0_HI, V1_HI);
6026   }
6027
6028   return DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, LO, HI);
6029 }
6030
6031 /// Try to fold a build_vector that performs an 'addsub' to an X86ISD::ADDSUB
6032 /// node.
6033 static SDValue LowerToAddSub(const BuildVectorSDNode *BV,
6034                              const X86Subtarget *Subtarget, SelectionDAG &DAG) {
6035   MVT VT = BV->getSimpleValueType(0);
6036   if ((!Subtarget->hasSSE3() || (VT != MVT::v4f32 && VT != MVT::v2f64)) &&
6037       (!Subtarget->hasAVX() || (VT != MVT::v8f32 && VT != MVT::v4f64)))
6038     return SDValue();
6039
6040   SDLoc DL(BV);
6041   unsigned NumElts = VT.getVectorNumElements();
6042   SDValue InVec0 = DAG.getUNDEF(VT);
6043   SDValue InVec1 = DAG.getUNDEF(VT);
6044
6045   assert((VT == MVT::v8f32 || VT == MVT::v4f64 || VT == MVT::v4f32 ||
6046           VT == MVT::v2f64) && "build_vector with an invalid type found!");
6047
6048   // Odd-numbered elements in the input build vector are obtained from
6049   // adding two integer/float elements.
6050   // Even-numbered elements in the input build vector are obtained from
6051   // subtracting two integer/float elements.
6052   unsigned ExpectedOpcode = ISD::FSUB;
6053   unsigned NextExpectedOpcode = ISD::FADD;
6054   bool AddFound = false;
6055   bool SubFound = false;
6056
6057   for (unsigned i = 0, e = NumElts; i != e; ++i) {
6058     SDValue Op = BV->getOperand(i);
6059
6060     // Skip 'undef' values.
6061     unsigned Opcode = Op.getOpcode();
6062     if (Opcode == ISD::UNDEF) {
6063       std::swap(ExpectedOpcode, NextExpectedOpcode);
6064       continue;
6065     }
6066
6067     // Early exit if we found an unexpected opcode.
6068     if (Opcode != ExpectedOpcode)
6069       return SDValue();
6070
6071     SDValue Op0 = Op.getOperand(0);
6072     SDValue Op1 = Op.getOperand(1);
6073
6074     // Try to match the following pattern:
6075     // (BINOP (extract_vector_elt A, i), (extract_vector_elt B, i))
6076     // Early exit if we cannot match that sequence.
6077     if (Op0.getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
6078         Op1.getOpcode() != ISD::EXTRACT_VECTOR_ELT ||
6079         !isa<ConstantSDNode>(Op0.getOperand(1)) ||
6080         !isa<ConstantSDNode>(Op1.getOperand(1)) ||
6081         Op0.getOperand(1) != Op1.getOperand(1))
6082       return SDValue();
6083
6084     unsigned I0 = cast<ConstantSDNode>(Op0.getOperand(1))->getZExtValue();
6085     if (I0 != i)
6086       return SDValue();
6087
6088     // We found a valid add/sub node. Update the information accordingly.
6089     if (i & 1)
6090       AddFound = true;
6091     else
6092       SubFound = true;
6093
6094     // Update InVec0 and InVec1.
6095     if (InVec0.getOpcode() == ISD::UNDEF) {
6096       InVec0 = Op0.getOperand(0);
6097       if (InVec0.getSimpleValueType() != VT)
6098         return SDValue();
6099     }
6100     if (InVec1.getOpcode() == ISD::UNDEF) {
6101       InVec1 = Op1.getOperand(0);
6102       if (InVec1.getSimpleValueType() != VT)
6103         return SDValue();
6104     }
6105
6106     // Make sure that operands in input to each add/sub node always
6107     // come from a same pair of vectors.
6108     if (InVec0 != Op0.getOperand(0)) {
6109       if (ExpectedOpcode == ISD::FSUB)
6110         return SDValue();
6111
6112       // FADD is commutable. Try to commute the operands
6113       // and then test again.
6114       std::swap(Op0, Op1);
6115       if (InVec0 != Op0.getOperand(0))
6116         return SDValue();
6117     }
6118
6119     if (InVec1 != Op1.getOperand(0))
6120       return SDValue();
6121
6122     // Update the pair of expected opcodes.
6123     std::swap(ExpectedOpcode, NextExpectedOpcode);
6124   }
6125
6126   // Don't try to fold this build_vector into an ADDSUB if the inputs are undef.
6127   if (AddFound && SubFound && InVec0.getOpcode() != ISD::UNDEF &&
6128       InVec1.getOpcode() != ISD::UNDEF)
6129     return DAG.getNode(X86ISD::ADDSUB, DL, VT, InVec0, InVec1);
6130
6131   return SDValue();
6132 }
6133
6134 /// Lower BUILD_VECTOR to a horizontal add/sub operation if possible.
6135 static SDValue LowerToHorizontalOp(const BuildVectorSDNode *BV,
6136                                    const X86Subtarget *Subtarget,
6137                                    SelectionDAG &DAG) {
6138   MVT VT = BV->getSimpleValueType(0);
6139   unsigned NumElts = VT.getVectorNumElements();
6140   unsigned NumUndefsLO = 0;
6141   unsigned NumUndefsHI = 0;
6142   unsigned Half = NumElts/2;
6143
6144   // Count the number of UNDEF operands in the build_vector in input.
6145   for (unsigned i = 0, e = Half; i != e; ++i)
6146     if (BV->getOperand(i)->getOpcode() == ISD::UNDEF)
6147       NumUndefsLO++;
6148
6149   for (unsigned i = Half, e = NumElts; i != e; ++i)
6150     if (BV->getOperand(i)->getOpcode() == ISD::UNDEF)
6151       NumUndefsHI++;
6152
6153   // Early exit if this is either a build_vector of all UNDEFs or all the
6154   // operands but one are UNDEF.
6155   if (NumUndefsLO + NumUndefsHI + 1 >= NumElts)
6156     return SDValue();
6157
6158   SDLoc DL(BV);
6159   SDValue InVec0, InVec1;
6160   if ((VT == MVT::v4f32 || VT == MVT::v2f64) && Subtarget->hasSSE3()) {
6161     // Try to match an SSE3 float HADD/HSUB.
6162     if (isHorizontalBinOp(BV, ISD::FADD, DAG, 0, NumElts, InVec0, InVec1))
6163       return DAG.getNode(X86ISD::FHADD, DL, VT, InVec0, InVec1);
6164
6165     if (isHorizontalBinOp(BV, ISD::FSUB, DAG, 0, NumElts, InVec0, InVec1))
6166       return DAG.getNode(X86ISD::FHSUB, DL, VT, InVec0, InVec1);
6167   } else if ((VT == MVT::v4i32 || VT == MVT::v8i16) && Subtarget->hasSSSE3()) {
6168     // Try to match an SSSE3 integer HADD/HSUB.
6169     if (isHorizontalBinOp(BV, ISD::ADD, DAG, 0, NumElts, InVec0, InVec1))
6170       return DAG.getNode(X86ISD::HADD, DL, VT, InVec0, InVec1);
6171
6172     if (isHorizontalBinOp(BV, ISD::SUB, DAG, 0, NumElts, InVec0, InVec1))
6173       return DAG.getNode(X86ISD::HSUB, DL, VT, InVec0, InVec1);
6174   }
6175
6176   if (!Subtarget->hasAVX())
6177     return SDValue();
6178
6179   if ((VT == MVT::v8f32 || VT == MVT::v4f64)) {
6180     // Try to match an AVX horizontal add/sub of packed single/double
6181     // precision floating point values from 256-bit vectors.
6182     SDValue InVec2, InVec3;
6183     if (isHorizontalBinOp(BV, ISD::FADD, DAG, 0, Half, InVec0, InVec1) &&
6184         isHorizontalBinOp(BV, ISD::FADD, DAG, Half, NumElts, InVec2, InVec3) &&
6185         ((InVec0.getOpcode() == ISD::UNDEF ||
6186           InVec2.getOpcode() == ISD::UNDEF) || InVec0 == InVec2) &&
6187         ((InVec1.getOpcode() == ISD::UNDEF ||
6188           InVec3.getOpcode() == ISD::UNDEF) || InVec1 == InVec3))
6189       return DAG.getNode(X86ISD::FHADD, DL, VT, InVec0, InVec1);
6190
6191     if (isHorizontalBinOp(BV, ISD::FSUB, DAG, 0, Half, InVec0, InVec1) &&
6192         isHorizontalBinOp(BV, ISD::FSUB, DAG, Half, NumElts, InVec2, InVec3) &&
6193         ((InVec0.getOpcode() == ISD::UNDEF ||
6194           InVec2.getOpcode() == ISD::UNDEF) || InVec0 == InVec2) &&
6195         ((InVec1.getOpcode() == ISD::UNDEF ||
6196           InVec3.getOpcode() == ISD::UNDEF) || InVec1 == InVec3))
6197       return DAG.getNode(X86ISD::FHSUB, DL, VT, InVec0, InVec1);
6198   } else if (VT == MVT::v8i32 || VT == MVT::v16i16) {
6199     // Try to match an AVX2 horizontal add/sub of signed integers.
6200     SDValue InVec2, InVec3;
6201     unsigned X86Opcode;
6202     bool CanFold = true;
6203
6204     if (isHorizontalBinOp(BV, ISD::ADD, DAG, 0, Half, InVec0, InVec1) &&
6205         isHorizontalBinOp(BV, ISD::ADD, DAG, Half, NumElts, InVec2, InVec3) &&
6206         ((InVec0.getOpcode() == ISD::UNDEF ||
6207           InVec2.getOpcode() == ISD::UNDEF) || InVec0 == InVec2) &&
6208         ((InVec1.getOpcode() == ISD::UNDEF ||
6209           InVec3.getOpcode() == ISD::UNDEF) || InVec1 == InVec3))
6210       X86Opcode = X86ISD::HADD;
6211     else if (isHorizontalBinOp(BV, ISD::SUB, DAG, 0, Half, InVec0, InVec1) &&
6212         isHorizontalBinOp(BV, ISD::SUB, DAG, Half, NumElts, InVec2, InVec3) &&
6213         ((InVec0.getOpcode() == ISD::UNDEF ||
6214           InVec2.getOpcode() == ISD::UNDEF) || InVec0 == InVec2) &&
6215         ((InVec1.getOpcode() == ISD::UNDEF ||
6216           InVec3.getOpcode() == ISD::UNDEF) || InVec1 == InVec3))
6217       X86Opcode = X86ISD::HSUB;
6218     else
6219       CanFold = false;
6220
6221     if (CanFold) {
6222       // Fold this build_vector into a single horizontal add/sub.
6223       // Do this only if the target has AVX2.
6224       if (Subtarget->hasAVX2())
6225         return DAG.getNode(X86Opcode, DL, VT, InVec0, InVec1);
6226
6227       // Do not try to expand this build_vector into a pair of horizontal
6228       // add/sub if we can emit a pair of scalar add/sub.
6229       if (NumUndefsLO + 1 == Half || NumUndefsHI + 1 == Half)
6230         return SDValue();
6231
6232       // Convert this build_vector into a pair of horizontal binop followed by
6233       // a concat vector.
6234       bool isUndefLO = NumUndefsLO == Half;
6235       bool isUndefHI = NumUndefsHI == Half;
6236       return ExpandHorizontalBinOp(InVec0, InVec1, DL, DAG, X86Opcode, false,
6237                                    isUndefLO, isUndefHI);
6238     }
6239   }
6240
6241   if ((VT == MVT::v8f32 || VT == MVT::v4f64 || VT == MVT::v8i32 ||
6242        VT == MVT::v16i16) && Subtarget->hasAVX()) {
6243     unsigned X86Opcode;
6244     if (isHorizontalBinOp(BV, ISD::ADD, DAG, 0, NumElts, InVec0, InVec1))
6245       X86Opcode = X86ISD::HADD;
6246     else if (isHorizontalBinOp(BV, ISD::SUB, DAG, 0, NumElts, InVec0, InVec1))
6247       X86Opcode = X86ISD::HSUB;
6248     else if (isHorizontalBinOp(BV, ISD::FADD, DAG, 0, NumElts, InVec0, InVec1))
6249       X86Opcode = X86ISD::FHADD;
6250     else if (isHorizontalBinOp(BV, ISD::FSUB, DAG, 0, NumElts, InVec0, InVec1))
6251       X86Opcode = X86ISD::FHSUB;
6252     else
6253       return SDValue();
6254
6255     // Don't try to expand this build_vector into a pair of horizontal add/sub
6256     // if we can simply emit a pair of scalar add/sub.
6257     if (NumUndefsLO + 1 == Half || NumUndefsHI + 1 == Half)
6258       return SDValue();
6259
6260     // Convert this build_vector into two horizontal add/sub followed by
6261     // a concat vector.
6262     bool isUndefLO = NumUndefsLO == Half;
6263     bool isUndefHI = NumUndefsHI == Half;
6264     return ExpandHorizontalBinOp(InVec0, InVec1, DL, DAG, X86Opcode, true,
6265                                  isUndefLO, isUndefHI);
6266   }
6267
6268   return SDValue();
6269 }
6270
6271 SDValue
6272 X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const {
6273   SDLoc dl(Op);
6274
6275   MVT VT = Op.getSimpleValueType();
6276   MVT ExtVT = VT.getVectorElementType();
6277   unsigned NumElems = Op.getNumOperands();
6278
6279   // Generate vectors for predicate vectors.
6280   if (VT.getVectorElementType() == MVT::i1 && Subtarget->hasAVX512())
6281     return LowerBUILD_VECTORvXi1(Op, DAG);
6282
6283   // Vectors containing all zeros can be matched by pxor and xorps later
6284   if (ISD::isBuildVectorAllZeros(Op.getNode())) {
6285     // Canonicalize this to <4 x i32> to 1) ensure the zero vectors are CSE'd
6286     // and 2) ensure that i64 scalars are eliminated on x86-32 hosts.
6287     if (VT == MVT::v4i32 || VT == MVT::v8i32 || VT == MVT::v16i32)
6288       return Op;
6289
6290     return getZeroVector(VT, Subtarget, DAG, dl);
6291   }
6292
6293   // Vectors containing all ones can be matched by pcmpeqd on 128-bit width
6294   // vectors or broken into v4i32 operations on 256-bit vectors. AVX2 can use
6295   // vpcmpeqd on 256-bit vectors.
6296   if (Subtarget->hasSSE2() && ISD::isBuildVectorAllOnes(Op.getNode())) {
6297     if (VT == MVT::v4i32 || (VT == MVT::v8i32 && Subtarget->hasInt256()))
6298       return Op;
6299
6300     if (!VT.is512BitVector())
6301       return getOnesVector(VT, Subtarget, DAG, dl);
6302   }
6303
6304   BuildVectorSDNode *BV = cast<BuildVectorSDNode>(Op.getNode());
6305   if (SDValue AddSub = LowerToAddSub(BV, Subtarget, DAG))
6306     return AddSub;
6307   if (SDValue HorizontalOp = LowerToHorizontalOp(BV, Subtarget, DAG))
6308     return HorizontalOp;
6309   if (SDValue Broadcast = LowerVectorBroadcast(Op, Subtarget, DAG))
6310     return Broadcast;
6311
6312   unsigned EVTBits = ExtVT.getSizeInBits();
6313
6314   unsigned NumZero  = 0;
6315   unsigned NumNonZero = 0;
6316   uint64_t NonZeros = 0;
6317   bool IsAllConstants = true;
6318   SmallSet<SDValue, 8> Values;
6319   for (unsigned i = 0; i < NumElems; ++i) {
6320     SDValue Elt = Op.getOperand(i);
6321     if (Elt.getOpcode() == ISD::UNDEF)
6322       continue;
6323     Values.insert(Elt);
6324     if (Elt.getOpcode() != ISD::Constant &&
6325         Elt.getOpcode() != ISD::ConstantFP)
6326       IsAllConstants = false;
6327     if (X86::isZeroNode(Elt))
6328       NumZero++;
6329     else {
6330       assert(i < sizeof(NonZeros) * 8); // Make sure the shift is within range.
6331       NonZeros |= ((uint64_t)1 << i);
6332       NumNonZero++;
6333     }
6334   }
6335
6336   // All undef vector. Return an UNDEF.  All zero vectors were handled above.
6337   if (NumNonZero == 0)
6338     return DAG.getUNDEF(VT);
6339
6340   // Special case for single non-zero, non-undef, element.
6341   if (NumNonZero == 1) {
6342     unsigned Idx = countTrailingZeros(NonZeros);
6343     SDValue Item = Op.getOperand(Idx);
6344
6345     // If this is an insertion of an i64 value on x86-32, and if the top bits of
6346     // the value are obviously zero, truncate the value to i32 and do the
6347     // insertion that way.  Only do this if the value is non-constant or if the
6348     // value is a constant being inserted into element 0.  It is cheaper to do
6349     // a constant pool load than it is to do a movd + shuffle.
6350     if (ExtVT == MVT::i64 && !Subtarget->is64Bit() &&
6351         (!IsAllConstants || Idx == 0)) {
6352       if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
6353         // Handle SSE only.
6354         assert(VT == MVT::v2i64 && "Expected an SSE value type!");
6355         MVT VecVT = MVT::v4i32;
6356
6357         // Truncate the value (which may itself be a constant) to i32, and
6358         // convert it to a vector with movd (S2V+shuffle to zero extend).
6359         Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
6360         Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
6361         return DAG.getBitcast(VT, getShuffleVectorZeroOrUndef(
6362                                       Item, Idx * 2, true, Subtarget, DAG));
6363       }
6364     }
6365
6366     // If we have a constant or non-constant insertion into the low element of
6367     // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
6368     // the rest of the elements.  This will be matched as movd/movq/movss/movsd
6369     // depending on what the source datatype is.
6370     if (Idx == 0) {
6371       if (NumZero == 0)
6372         return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
6373
6374       if (ExtVT == MVT::i32 || ExtVT == MVT::f32 || ExtVT == MVT::f64 ||
6375           (ExtVT == MVT::i64 && Subtarget->is64Bit())) {
6376         if (VT.is512BitVector()) {
6377           SDValue ZeroVec = getZeroVector(VT, Subtarget, DAG, dl);
6378           return DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, ZeroVec,
6379                              Item, DAG.getIntPtrConstant(0, dl));
6380         }
6381         assert((VT.is128BitVector() || VT.is256BitVector()) &&
6382                "Expected an SSE value type!");
6383         Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
6384         // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
6385         return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
6386       }
6387
6388       // We can't directly insert an i8 or i16 into a vector, so zero extend
6389       // it to i32 first.
6390       if (ExtVT == MVT::i16 || ExtVT == MVT::i8) {
6391         Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
6392         if (VT.is256BitVector()) {
6393           if (Subtarget->hasAVX()) {
6394             Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v8i32, Item);
6395             Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
6396           } else {
6397             // Without AVX, we need to extend to a 128-bit vector and then
6398             // insert into the 256-bit vector.
6399             Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
6400             SDValue ZeroVec = getZeroVector(MVT::v8i32, Subtarget, DAG, dl);
6401             Item = Insert128BitVector(ZeroVec, Item, 0, DAG, dl);
6402           }
6403         } else {
6404           assert(VT.is128BitVector() && "Expected an SSE value type!");
6405           Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, Item);
6406           Item = getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget, DAG);
6407         }
6408         return DAG.getBitcast(VT, Item);
6409       }
6410     }
6411
6412     // Is it a vector logical left shift?
6413     if (NumElems == 2 && Idx == 1 &&
6414         X86::isZeroNode(Op.getOperand(0)) &&
6415         !X86::isZeroNode(Op.getOperand(1))) {
6416       unsigned NumBits = VT.getSizeInBits();
6417       return getVShift(true, VT,
6418                        DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
6419                                    VT, Op.getOperand(1)),
6420                        NumBits/2, DAG, *this, dl);
6421     }
6422
6423     if (IsAllConstants) // Otherwise, it's better to do a constpool load.
6424       return SDValue();
6425
6426     // Otherwise, if this is a vector with i32 or f32 elements, and the element
6427     // is a non-constant being inserted into an element other than the low one,
6428     // we can't use a constant pool load.  Instead, use SCALAR_TO_VECTOR (aka
6429     // movd/movss) to move this into the low element, then shuffle it into
6430     // place.
6431     if (EVTBits == 32) {
6432       Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
6433       return getShuffleVectorZeroOrUndef(Item, Idx, NumZero > 0, Subtarget, DAG);
6434     }
6435   }
6436
6437   // Splat is obviously ok. Let legalizer expand it to a shuffle.
6438   if (Values.size() == 1) {
6439     if (EVTBits == 32) {
6440       // Instead of a shuffle like this:
6441       // shuffle (scalar_to_vector (load (ptr + 4))), undef, <0, 0, 0, 0>
6442       // Check if it's possible to issue this instead.
6443       // shuffle (vload ptr)), undef, <1, 1, 1, 1>
6444       unsigned Idx = countTrailingZeros(NonZeros);
6445       SDValue Item = Op.getOperand(Idx);
6446       if (Op.getNode()->isOnlyUserOf(Item.getNode()))
6447         return LowerAsSplatVectorLoad(Item, VT, dl, DAG);
6448     }
6449     return SDValue();
6450   }
6451
6452   // A vector full of immediates; various special cases are already
6453   // handled, so this is best done with a single constant-pool load.
6454   if (IsAllConstants)
6455     return SDValue();
6456
6457   // For AVX-length vectors, see if we can use a vector load to get all of the
6458   // elements, otherwise build the individual 128-bit pieces and use
6459   // shuffles to put them in place.
6460   if (VT.is256BitVector() || VT.is512BitVector()) {
6461     SmallVector<SDValue, 64> V(Op->op_begin(), Op->op_begin() + NumElems);
6462
6463     // Check for a build vector of consecutive loads.
6464     if (SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG, false))
6465       return LD;
6466
6467     EVT HVT = EVT::getVectorVT(*DAG.getContext(), ExtVT, NumElems/2);
6468
6469     // Build both the lower and upper subvector.
6470     SDValue Lower = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT,
6471                                 makeArrayRef(&V[0], NumElems/2));
6472     SDValue Upper = DAG.getNode(ISD::BUILD_VECTOR, dl, HVT,
6473                                 makeArrayRef(&V[NumElems / 2], NumElems/2));
6474
6475     // Recreate the wider vector with the lower and upper part.
6476     if (VT.is256BitVector())
6477       return Concat128BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
6478     return Concat256BitVectors(Lower, Upper, VT, NumElems, DAG, dl);
6479   }
6480
6481   // Let legalizer expand 2-wide build_vectors.
6482   if (EVTBits == 64) {
6483     if (NumNonZero == 1) {
6484       // One half is zero or undef.
6485       unsigned Idx = countTrailingZeros(NonZeros);
6486       SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
6487                                Op.getOperand(Idx));
6488       return getShuffleVectorZeroOrUndef(V2, Idx, true, Subtarget, DAG);
6489     }
6490     return SDValue();
6491   }
6492
6493   // If element VT is < 32 bits, convert it to inserts into a zero vector.
6494   if (EVTBits == 8 && NumElems == 16)
6495     if (SDValue V = LowerBuildVectorv16i8(Op, NonZeros, NumNonZero, NumZero,
6496                                           DAG, Subtarget, *this))
6497       return V;
6498
6499   if (EVTBits == 16 && NumElems == 8)
6500     if (SDValue V = LowerBuildVectorv8i16(Op, NonZeros, NumNonZero, NumZero,
6501                                           DAG, Subtarget, *this))
6502       return V;
6503
6504   // If element VT is == 32 bits and has 4 elems, try to generate an INSERTPS
6505   if (EVTBits == 32 && NumElems == 4)
6506     if (SDValue V = LowerBuildVectorv4x32(Op, DAG, Subtarget, *this))
6507       return V;
6508
6509   // If element VT is == 32 bits, turn it into a number of shuffles.
6510   SmallVector<SDValue, 8> V(NumElems);
6511   if (NumElems == 4 && NumZero > 0) {
6512     for (unsigned i = 0; i < 4; ++i) {
6513       bool isZero = !(NonZeros & (1ULL << i));
6514       if (isZero)
6515         V[i] = getZeroVector(VT, Subtarget, DAG, dl);
6516       else
6517         V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
6518     }
6519
6520     for (unsigned i = 0; i < 2; ++i) {
6521       switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
6522         default: break;
6523         case 0:
6524           V[i] = V[i*2];  // Must be a zero vector.
6525           break;
6526         case 1:
6527           V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
6528           break;
6529         case 2:
6530           V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
6531           break;
6532         case 3:
6533           V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
6534           break;
6535       }
6536     }
6537
6538     bool Reverse1 = (NonZeros & 0x3) == 2;
6539     bool Reverse2 = ((NonZeros & (0x3 << 2)) >> 2) == 2;
6540     int MaskVec[] = {
6541       Reverse1 ? 1 : 0,
6542       Reverse1 ? 0 : 1,
6543       static_cast<int>(Reverse2 ? NumElems+1 : NumElems),
6544       static_cast<int>(Reverse2 ? NumElems   : NumElems+1)
6545     };
6546     return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
6547   }
6548
6549   if (Values.size() > 1 && VT.is128BitVector()) {
6550     // Check for a build vector of consecutive loads.
6551     for (unsigned i = 0; i < NumElems; ++i)
6552       V[i] = Op.getOperand(i);
6553
6554     // Check for elements which are consecutive loads.
6555     if (SDValue LD = EltsFromConsecutiveLoads(VT, V, dl, DAG, false))
6556       return LD;
6557
6558     // Check for a build vector from mostly shuffle plus few inserting.
6559     if (SDValue Sh = buildFromShuffleMostly(Op, DAG))
6560       return Sh;
6561
6562     // For SSE 4.1, use insertps to put the high elements into the low element.
6563     if (Subtarget->hasSSE41()) {
6564       SDValue Result;
6565       if (Op.getOperand(0).getOpcode() != ISD::UNDEF)
6566         Result = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(0));
6567       else
6568         Result = DAG.getUNDEF(VT);
6569
6570       for (unsigned i = 1; i < NumElems; ++i) {
6571         if (Op.getOperand(i).getOpcode() == ISD::UNDEF) continue;
6572         Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, Result,
6573                              Op.getOperand(i), DAG.getIntPtrConstant(i, dl));
6574       }
6575       return Result;
6576     }
6577
6578     // Otherwise, expand into a number of unpckl*, start by extending each of
6579     // our (non-undef) elements to the full vector width with the element in the
6580     // bottom slot of the vector (which generates no code for SSE).
6581     for (unsigned i = 0; i < NumElems; ++i) {
6582       if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
6583         V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
6584       else
6585         V[i] = DAG.getUNDEF(VT);
6586     }
6587
6588     // Next, we iteratively mix elements, e.g. for v4f32:
6589     //   Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
6590     //         : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
6591     //   Step 2: unpcklps X, Y ==>    <3, 2, 1, 0>
6592     unsigned EltStride = NumElems >> 1;
6593     while (EltStride != 0) {
6594       for (unsigned i = 0; i < EltStride; ++i) {
6595         // If V[i+EltStride] is undef and this is the first round of mixing,
6596         // then it is safe to just drop this shuffle: V[i] is already in the
6597         // right place, the one element (since it's the first round) being
6598         // inserted as undef can be dropped.  This isn't safe for successive
6599         // rounds because they will permute elements within both vectors.
6600         if (V[i+EltStride].getOpcode() == ISD::UNDEF &&
6601             EltStride == NumElems/2)
6602           continue;
6603
6604         V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + EltStride]);
6605       }
6606       EltStride >>= 1;
6607     }
6608     return V[0];
6609   }
6610   return SDValue();
6611 }
6612
6613 // 256-bit AVX can use the vinsertf128 instruction
6614 // to create 256-bit vectors from two other 128-bit ones.
6615 static SDValue LowerAVXCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) {
6616   SDLoc dl(Op);
6617   MVT ResVT = Op.getSimpleValueType();
6618
6619   assert((ResVT.is256BitVector() ||
6620           ResVT.is512BitVector()) && "Value type must be 256-/512-bit wide");
6621
6622   SDValue V1 = Op.getOperand(0);
6623   SDValue V2 = Op.getOperand(1);
6624   unsigned NumElems = ResVT.getVectorNumElements();
6625   if (ResVT.is256BitVector())
6626     return Concat128BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
6627
6628   if (Op.getNumOperands() == 4) {
6629     MVT HalfVT = MVT::getVectorVT(ResVT.getVectorElementType(),
6630                                   ResVT.getVectorNumElements()/2);
6631     SDValue V3 = Op.getOperand(2);
6632     SDValue V4 = Op.getOperand(3);
6633     return Concat256BitVectors(Concat128BitVectors(V1, V2, HalfVT, NumElems/2, DAG, dl),
6634       Concat128BitVectors(V3, V4, HalfVT, NumElems/2, DAG, dl), ResVT, NumElems, DAG, dl);
6635   }
6636   return Concat256BitVectors(V1, V2, ResVT, NumElems, DAG, dl);
6637 }
6638
6639 static SDValue LowerCONCAT_VECTORSvXi1(SDValue Op,
6640                                        const X86Subtarget *Subtarget,
6641                                        SelectionDAG & DAG) {
6642   SDLoc dl(Op);
6643   MVT ResVT = Op.getSimpleValueType();
6644   unsigned NumOfOperands = Op.getNumOperands();
6645
6646   assert(isPowerOf2_32(NumOfOperands) &&
6647          "Unexpected number of operands in CONCAT_VECTORS");
6648
6649   SDValue Undef = DAG.getUNDEF(ResVT);
6650   if (NumOfOperands > 2) {
6651     // Specialize the cases when all, or all but one, of the operands are undef.
6652     unsigned NumOfDefinedOps = 0;
6653     unsigned OpIdx = 0;
6654     for (unsigned i = 0; i < NumOfOperands; i++)
6655       if (!Op.getOperand(i).isUndef()) {
6656         NumOfDefinedOps++;
6657         OpIdx = i;
6658       }
6659     if (NumOfDefinedOps == 0)
6660       return Undef;
6661     if (NumOfDefinedOps == 1) {
6662       unsigned SubVecNumElts =
6663         Op.getOperand(OpIdx).getValueType().getVectorNumElements();
6664       SDValue IdxVal = DAG.getIntPtrConstant(SubVecNumElts * OpIdx, dl);
6665       return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResVT, Undef,
6666                          Op.getOperand(OpIdx), IdxVal);
6667     }
6668
6669     MVT HalfVT = MVT::getVectorVT(ResVT.getVectorElementType(),
6670                                   ResVT.getVectorNumElements()/2);
6671     SmallVector<SDValue, 2> Ops;
6672     for (unsigned i = 0; i < NumOfOperands/2; i++)
6673       Ops.push_back(Op.getOperand(i));
6674     SDValue Lo = DAG.getNode(ISD::CONCAT_VECTORS, dl, HalfVT, Ops);
6675     Ops.clear();
6676     for (unsigned i = NumOfOperands/2; i < NumOfOperands; i++)
6677       Ops.push_back(Op.getOperand(i));
6678     SDValue Hi = DAG.getNode(ISD::CONCAT_VECTORS, dl, HalfVT, Ops);
6679     return DAG.getNode(ISD::CONCAT_VECTORS, dl, ResVT, Lo, Hi);
6680   }
6681
6682   // 2 operands
6683   SDValue V1 = Op.getOperand(0);
6684   SDValue V2 = Op.getOperand(1);
6685   unsigned NumElems = ResVT.getVectorNumElements();
6686   assert(V1.getValueType() == V2.getValueType() &&
6687          V1.getValueType().getVectorNumElements() == NumElems/2 &&
6688          "Unexpected operands in CONCAT_VECTORS");
6689
6690   if (ResVT.getSizeInBits() >= 16)
6691     return Op; // The operation is legal with KUNPCK
6692
6693   bool IsZeroV1 = ISD::isBuildVectorAllZeros(V1.getNode());
6694   bool IsZeroV2 = ISD::isBuildVectorAllZeros(V2.getNode());
6695   SDValue ZeroVec = getZeroVector(ResVT, Subtarget, DAG, dl);
6696   if (IsZeroV1 && IsZeroV2)
6697     return ZeroVec;
6698
6699   SDValue ZeroIdx = DAG.getIntPtrConstant(0, dl);
6700   if (V2.isUndef())
6701     return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResVT, Undef, V1, ZeroIdx);
6702   if (IsZeroV2)
6703     return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResVT, ZeroVec, V1, ZeroIdx);
6704
6705   SDValue IdxVal = DAG.getIntPtrConstant(NumElems/2, dl);
6706   if (V1.isUndef())
6707     V2 = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResVT, Undef, V2, IdxVal);
6708
6709   if (IsZeroV1)
6710     return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResVT, ZeroVec, V2, IdxVal);
6711
6712   V1 = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResVT, Undef, V1, ZeroIdx);
6713   return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, ResVT, V1, V2, IdxVal);
6714 }
6715
6716 static SDValue LowerCONCAT_VECTORS(SDValue Op,
6717                                    const X86Subtarget *Subtarget,
6718                                    SelectionDAG &DAG) {
6719   MVT VT = Op.getSimpleValueType();
6720   if (VT.getVectorElementType() == MVT::i1)
6721     return LowerCONCAT_VECTORSvXi1(Op, Subtarget, DAG);
6722
6723   assert((VT.is256BitVector() && Op.getNumOperands() == 2) ||
6724          (VT.is512BitVector() && (Op.getNumOperands() == 2 ||
6725           Op.getNumOperands() == 4)));
6726
6727   // AVX can use the vinsertf128 instruction to create 256-bit vectors
6728   // from two other 128-bit ones.
6729
6730   // 512-bit vector may contain 2 256-bit vectors or 4 128-bit vectors
6731   return LowerAVXCONCAT_VECTORS(Op, DAG);
6732 }
6733
6734 //===----------------------------------------------------------------------===//
6735 // Vector shuffle lowering
6736 //
6737 // This is an experimental code path for lowering vector shuffles on x86. It is
6738 // designed to handle arbitrary vector shuffles and blends, gracefully
6739 // degrading performance as necessary. It works hard to recognize idiomatic
6740 // shuffles and lower them to optimal instruction patterns without leaving
6741 // a framework that allows reasonably efficient handling of all vector shuffle
6742 // patterns.
6743 //===----------------------------------------------------------------------===//
6744
6745 /// \brief Tiny helper function to identify a no-op mask.
6746 ///
6747 /// This is a somewhat boring predicate function. It checks whether the mask
6748 /// array input, which is assumed to be a single-input shuffle mask of the kind
6749 /// used by the X86 shuffle instructions (not a fully general
6750 /// ShuffleVectorSDNode mask) requires any shuffles to occur. Both undef and an
6751 /// in-place shuffle are 'no-op's.
6752 static bool isNoopShuffleMask(ArrayRef<int> Mask) {
6753   for (int i = 0, Size = Mask.size(); i < Size; ++i)
6754     if (Mask[i] != -1 && Mask[i] != i)
6755       return false;
6756   return true;
6757 }
6758
6759 /// \brief Helper function to classify a mask as a single-input mask.
6760 ///
6761 /// This isn't a generic single-input test because in the vector shuffle
6762 /// lowering we canonicalize single inputs to be the first input operand. This
6763 /// means we can more quickly test for a single input by only checking whether
6764 /// an input from the second operand exists. We also assume that the size of
6765 /// mask corresponds to the size of the input vectors which isn't true in the
6766 /// fully general case.
6767 static bool isSingleInputShuffleMask(ArrayRef<int> Mask) {
6768   for (int M : Mask)
6769     if (M >= (int)Mask.size())
6770       return false;
6771   return true;
6772 }
6773
6774 /// \brief Test whether there are elements crossing 128-bit lanes in this
6775 /// shuffle mask.
6776 ///
6777 /// X86 divides up its shuffles into in-lane and cross-lane shuffle operations
6778 /// and we routinely test for these.
6779 static bool is128BitLaneCrossingShuffleMask(MVT VT, ArrayRef<int> Mask) {
6780   int LaneSize = 128 / VT.getScalarSizeInBits();
6781   int Size = Mask.size();
6782   for (int i = 0; i < Size; ++i)
6783     if (Mask[i] >= 0 && (Mask[i] % Size) / LaneSize != i / LaneSize)
6784       return true;
6785   return false;
6786 }
6787
6788 /// \brief Test whether a shuffle mask is equivalent within each 128-bit lane.
6789 ///
6790 /// This checks a shuffle mask to see if it is performing the same
6791 /// 128-bit lane-relative shuffle in each 128-bit lane. This trivially implies
6792 /// that it is also not lane-crossing. It may however involve a blend from the
6793 /// same lane of a second vector.
6794 ///
6795 /// The specific repeated shuffle mask is populated in \p RepeatedMask, as it is
6796 /// non-trivial to compute in the face of undef lanes. The representation is
6797 /// *not* suitable for use with existing 128-bit shuffles as it will contain
6798 /// entries from both V1 and V2 inputs to the wider mask.
6799 static bool
6800 is128BitLaneRepeatedShuffleMask(MVT VT, ArrayRef<int> Mask,
6801                                 SmallVectorImpl<int> &RepeatedMask) {
6802   int LaneSize = 128 / VT.getScalarSizeInBits();
6803   RepeatedMask.resize(LaneSize, -1);
6804   int Size = Mask.size();
6805   for (int i = 0; i < Size; ++i) {
6806     if (Mask[i] < 0)
6807       continue;
6808     if ((Mask[i] % Size) / LaneSize != i / LaneSize)
6809       // This entry crosses lanes, so there is no way to model this shuffle.
6810       return false;
6811
6812     // Ok, handle the in-lane shuffles by detecting if and when they repeat.
6813     if (RepeatedMask[i % LaneSize] == -1)
6814       // This is the first non-undef entry in this slot of a 128-bit lane.
6815       RepeatedMask[i % LaneSize] =
6816           Mask[i] < Size ? Mask[i] % LaneSize : Mask[i] % LaneSize + Size;
6817     else if (RepeatedMask[i % LaneSize] + (i / LaneSize) * LaneSize != Mask[i])
6818       // Found a mismatch with the repeated mask.
6819       return false;
6820   }
6821   return true;
6822 }
6823
6824 /// \brief Checks whether a shuffle mask is equivalent to an explicit list of
6825 /// arguments.
6826 ///
6827 /// This is a fast way to test a shuffle mask against a fixed pattern:
6828 ///
6829 ///   if (isShuffleEquivalent(Mask, 3, 2, {1, 0})) { ... }
6830 ///
6831 /// It returns true if the mask is exactly as wide as the argument list, and
6832 /// each element of the mask is either -1 (signifying undef) or the value given
6833 /// in the argument.
6834 static bool isShuffleEquivalent(SDValue V1, SDValue V2, ArrayRef<int> Mask,
6835                                 ArrayRef<int> ExpectedMask) {
6836   if (Mask.size() != ExpectedMask.size())
6837     return false;
6838
6839   int Size = Mask.size();
6840
6841   // If the values are build vectors, we can look through them to find
6842   // equivalent inputs that make the shuffles equivalent.
6843   auto *BV1 = dyn_cast<BuildVectorSDNode>(V1);
6844   auto *BV2 = dyn_cast<BuildVectorSDNode>(V2);
6845
6846   for (int i = 0; i < Size; ++i)
6847     if (Mask[i] != -1 && Mask[i] != ExpectedMask[i]) {
6848       auto *MaskBV = Mask[i] < Size ? BV1 : BV2;
6849       auto *ExpectedBV = ExpectedMask[i] < Size ? BV1 : BV2;
6850       if (!MaskBV || !ExpectedBV ||
6851           MaskBV->getOperand(Mask[i] % Size) !=
6852               ExpectedBV->getOperand(ExpectedMask[i] % Size))
6853         return false;
6854     }
6855
6856   return true;
6857 }
6858
6859 /// \brief Get a 4-lane 8-bit shuffle immediate for a mask.
6860 ///
6861 /// This helper function produces an 8-bit shuffle immediate corresponding to
6862 /// the ubiquitous shuffle encoding scheme used in x86 instructions for
6863 /// shuffling 4 lanes. It can be used with most of the PSHUF instructions for
6864 /// example.
6865 ///
6866 /// NB: We rely heavily on "undef" masks preserving the input lane.
6867 static SDValue getV4X86ShuffleImm8ForMask(ArrayRef<int> Mask, SDLoc DL,
6868                                           SelectionDAG &DAG) {
6869   assert(Mask.size() == 4 && "Only 4-lane shuffle masks");
6870   assert(Mask[0] >= -1 && Mask[0] < 4 && "Out of bound mask element!");
6871   assert(Mask[1] >= -1 && Mask[1] < 4 && "Out of bound mask element!");
6872   assert(Mask[2] >= -1 && Mask[2] < 4 && "Out of bound mask element!");
6873   assert(Mask[3] >= -1 && Mask[3] < 4 && "Out of bound mask element!");
6874
6875   unsigned Imm = 0;
6876   Imm |= (Mask[0] == -1 ? 0 : Mask[0]) << 0;
6877   Imm |= (Mask[1] == -1 ? 1 : Mask[1]) << 2;
6878   Imm |= (Mask[2] == -1 ? 2 : Mask[2]) << 4;
6879   Imm |= (Mask[3] == -1 ? 3 : Mask[3]) << 6;
6880   return DAG.getConstant(Imm, DL, MVT::i8);
6881 }
6882
6883 /// \brief Compute whether each element of a shuffle is zeroable.
6884 ///
6885 /// A "zeroable" vector shuffle element is one which can be lowered to zero.
6886 /// Either it is an undef element in the shuffle mask, the element of the input
6887 /// referenced is undef, or the element of the input referenced is known to be
6888 /// zero. Many x86 shuffles can zero lanes cheaply and we often want to handle
6889 /// as many lanes with this technique as possible to simplify the remaining
6890 /// shuffle.
6891 static SmallBitVector computeZeroableShuffleElements(ArrayRef<int> Mask,
6892                                                      SDValue V1, SDValue V2) {
6893   SmallBitVector Zeroable(Mask.size(), false);
6894
6895   while (V1.getOpcode() == ISD::BITCAST)
6896     V1 = V1->getOperand(0);
6897   while (V2.getOpcode() == ISD::BITCAST)
6898     V2 = V2->getOperand(0);
6899
6900   bool V1IsZero = ISD::isBuildVectorAllZeros(V1.getNode());
6901   bool V2IsZero = ISD::isBuildVectorAllZeros(V2.getNode());
6902
6903   for (int i = 0, Size = Mask.size(); i < Size; ++i) {
6904     int M = Mask[i];
6905     // Handle the easy cases.
6906     if (M < 0 || (M >= 0 && M < Size && V1IsZero) || (M >= Size && V2IsZero)) {
6907       Zeroable[i] = true;
6908       continue;
6909     }
6910
6911     // If this is an index into a build_vector node (which has the same number
6912     // of elements), dig out the input value and use it.
6913     SDValue V = M < Size ? V1 : V2;
6914     if (V.getOpcode() != ISD::BUILD_VECTOR || Size != (int)V.getNumOperands())
6915       continue;
6916
6917     SDValue Input = V.getOperand(M % Size);
6918     // The UNDEF opcode check really should be dead code here, but not quite
6919     // worth asserting on (it isn't invalid, just unexpected).
6920     if (Input.getOpcode() == ISD::UNDEF || X86::isZeroNode(Input))
6921       Zeroable[i] = true;
6922   }
6923
6924   return Zeroable;
6925 }
6926
6927 // X86 has dedicated unpack instructions that can handle specific blend
6928 // operations: UNPCKH and UNPCKL.
6929 static SDValue lowerVectorShuffleWithUNPCK(SDLoc DL, MVT VT, ArrayRef<int> Mask,
6930                                            SDValue V1, SDValue V2,
6931                                            SelectionDAG &DAG) {
6932   int NumElts = VT.getVectorNumElements();
6933   int NumEltsInLane = 128 / VT.getScalarSizeInBits();
6934   SmallVector<int, 8> Unpckl;
6935   SmallVector<int, 8> Unpckh;
6936
6937   for (int i = 0; i < NumElts; ++i) {
6938     unsigned LaneStart = (i / NumEltsInLane) * NumEltsInLane;
6939     int LoPos = (i % NumEltsInLane) / 2 + LaneStart + NumElts * (i % 2);
6940     int HiPos = LoPos + NumEltsInLane / 2;
6941     Unpckl.push_back(LoPos);
6942     Unpckh.push_back(HiPos);
6943   }
6944
6945   if (isShuffleEquivalent(V1, V2, Mask, Unpckl))
6946     return DAG.getNode(X86ISD::UNPCKL, DL, VT, V1, V2);
6947   if (isShuffleEquivalent(V1, V2, Mask, Unpckh))
6948     return DAG.getNode(X86ISD::UNPCKH, DL, VT, V1, V2);
6949
6950   // Commute and try again.
6951   ShuffleVectorSDNode::commuteMask(Unpckl);
6952   if (isShuffleEquivalent(V1, V2, Mask, Unpckl))
6953     return DAG.getNode(X86ISD::UNPCKL, DL, VT, V2, V1);
6954
6955   ShuffleVectorSDNode::commuteMask(Unpckh);
6956   if (isShuffleEquivalent(V1, V2, Mask, Unpckh))
6957     return DAG.getNode(X86ISD::UNPCKH, DL, VT, V2, V1);
6958
6959   return SDValue();
6960 }
6961
6962 /// \brief Try to emit a bitmask instruction for a shuffle.
6963 ///
6964 /// This handles cases where we can model a blend exactly as a bitmask due to
6965 /// one of the inputs being zeroable.
6966 static SDValue lowerVectorShuffleAsBitMask(SDLoc DL, MVT VT, SDValue V1,
6967                                            SDValue V2, ArrayRef<int> Mask,
6968                                            SelectionDAG &DAG) {
6969   MVT EltVT = VT.getVectorElementType();
6970   int NumEltBits = EltVT.getSizeInBits();
6971   MVT IntEltVT = MVT::getIntegerVT(NumEltBits);
6972   SDValue Zero = DAG.getConstant(0, DL, IntEltVT);
6973   SDValue AllOnes = DAG.getConstant(APInt::getAllOnesValue(NumEltBits), DL,
6974                                     IntEltVT);
6975   if (EltVT.isFloatingPoint()) {
6976     Zero = DAG.getBitcast(EltVT, Zero);
6977     AllOnes = DAG.getBitcast(EltVT, AllOnes);
6978   }
6979   SmallVector<SDValue, 16> VMaskOps(Mask.size(), Zero);
6980   SmallBitVector Zeroable = computeZeroableShuffleElements(Mask, V1, V2);
6981   SDValue V;
6982   for (int i = 0, Size = Mask.size(); i < Size; ++i) {
6983     if (Zeroable[i])
6984       continue;
6985     if (Mask[i] % Size != i)
6986       return SDValue(); // Not a blend.
6987     if (!V)
6988       V = Mask[i] < Size ? V1 : V2;
6989     else if (V != (Mask[i] < Size ? V1 : V2))
6990       return SDValue(); // Can only let one input through the mask.
6991
6992     VMaskOps[i] = AllOnes;
6993   }
6994   if (!V)
6995     return SDValue(); // No non-zeroable elements!
6996
6997   SDValue VMask = DAG.getNode(ISD::BUILD_VECTOR, DL, VT, VMaskOps);
6998   V = DAG.getNode(VT.isFloatingPoint()
6999                   ? (unsigned) X86ISD::FAND : (unsigned) ISD::AND,
7000                   DL, VT, V, VMask);
7001   return V;
7002 }
7003
7004 /// \brief Try to emit a blend instruction for a shuffle using bit math.
7005 ///
7006 /// This is used as a fallback approach when first class blend instructions are
7007 /// unavailable. Currently it is only suitable for integer vectors, but could
7008 /// be generalized for floating point vectors if desirable.
7009 static SDValue lowerVectorShuffleAsBitBlend(SDLoc DL, MVT VT, SDValue V1,
7010                                             SDValue V2, ArrayRef<int> Mask,
7011                                             SelectionDAG &DAG) {
7012   assert(VT.isInteger() && "Only supports integer vector types!");
7013   MVT EltVT = VT.getVectorElementType();
7014   int NumEltBits = EltVT.getSizeInBits();
7015   SDValue Zero = DAG.getConstant(0, DL, EltVT);
7016   SDValue AllOnes = DAG.getConstant(APInt::getAllOnesValue(NumEltBits), DL,
7017                                     EltVT);
7018   SmallVector<SDValue, 16> MaskOps;
7019   for (int i = 0, Size = Mask.size(); i < Size; ++i) {
7020     if (Mask[i] != -1 && Mask[i] != i && Mask[i] != i + Size)
7021       return SDValue(); // Shuffled input!
7022     MaskOps.push_back(Mask[i] < Size ? AllOnes : Zero);
7023   }
7024
7025   SDValue V1Mask = DAG.getNode(ISD::BUILD_VECTOR, DL, VT, MaskOps);
7026   V1 = DAG.getNode(ISD::AND, DL, VT, V1, V1Mask);
7027   // We have to cast V2 around.
7028   MVT MaskVT = MVT::getVectorVT(MVT::i64, VT.getSizeInBits() / 64);
7029   V2 = DAG.getBitcast(VT, DAG.getNode(X86ISD::ANDNP, DL, MaskVT,
7030                                       DAG.getBitcast(MaskVT, V1Mask),
7031                                       DAG.getBitcast(MaskVT, V2)));
7032   return DAG.getNode(ISD::OR, DL, VT, V1, V2);
7033 }
7034
7035 /// \brief Try to emit a blend instruction for a shuffle.
7036 ///
7037 /// This doesn't do any checks for the availability of instructions for blending
7038 /// these values. It relies on the availability of the X86ISD::BLENDI pattern to
7039 /// be matched in the backend with the type given. What it does check for is
7040 /// that the shuffle mask is a blend, or convertible into a blend with zero.
7041 static SDValue lowerVectorShuffleAsBlend(SDLoc DL, MVT VT, SDValue V1,
7042                                          SDValue V2, ArrayRef<int> Original,
7043                                          const X86Subtarget *Subtarget,
7044                                          SelectionDAG &DAG) {
7045   bool V1IsZero = ISD::isBuildVectorAllZeros(V1.getNode());
7046   bool V2IsZero = ISD::isBuildVectorAllZeros(V2.getNode());
7047   SmallVector<int, 8> Mask(Original.begin(), Original.end());
7048   SmallBitVector Zeroable = computeZeroableShuffleElements(Mask, V1, V2);
7049   bool ForceV1Zero = false, ForceV2Zero = false;
7050
7051   // Attempt to generate the binary blend mask. If an input is zero then
7052   // we can use any lane.
7053   // TODO: generalize the zero matching to any scalar like isShuffleEquivalent.
7054   unsigned BlendMask = 0;
7055   for (int i = 0, Size = Mask.size(); i < Size; ++i) {
7056     int M = Mask[i];
7057     if (M < 0)
7058       continue;
7059     if (M == i)
7060       continue;
7061     if (M == i + Size) {
7062       BlendMask |= 1u << i;
7063       continue;
7064     }
7065     if (Zeroable[i]) {
7066       if (V1IsZero) {
7067         ForceV1Zero = true;
7068         Mask[i] = i;
7069         continue;
7070       }
7071       if (V2IsZero) {
7072         ForceV2Zero = true;
7073         BlendMask |= 1u << i;
7074         Mask[i] = i + Size;
7075         continue;
7076       }
7077     }
7078     return SDValue(); // Shuffled input!
7079   }
7080
7081   // Create a REAL zero vector - ISD::isBuildVectorAllZeros allows UNDEFs.
7082   if (ForceV1Zero)
7083     V1 = getZeroVector(VT, Subtarget, DAG, DL);
7084   if (ForceV2Zero)
7085     V2 = getZeroVector(VT, Subtarget, DAG, DL);
7086
7087   auto ScaleBlendMask = [](unsigned BlendMask, int Size, int Scale) {
7088     unsigned ScaledMask = 0;
7089     for (int i = 0; i != Size; ++i)
7090       if (BlendMask & (1u << i))
7091         for (int j = 0; j != Scale; ++j)
7092           ScaledMask |= 1u << (i * Scale + j);
7093     return ScaledMask;
7094   };
7095
7096   switch (VT.SimpleTy) {
7097   case MVT::v2f64:
7098   case MVT::v4f32:
7099   case MVT::v4f64:
7100   case MVT::v8f32:
7101     return DAG.getNode(X86ISD::BLENDI, DL, VT, V1, V2,
7102                        DAG.getConstant(BlendMask, DL, MVT::i8));
7103
7104   case MVT::v4i64:
7105   case MVT::v8i32:
7106     assert(Subtarget->hasAVX2() && "256-bit integer blends require AVX2!");
7107     // FALLTHROUGH
7108   case MVT::v2i64:
7109   case MVT::v4i32:
7110     // If we have AVX2 it is faster to use VPBLENDD when the shuffle fits into
7111     // that instruction.
7112     if (Subtarget->hasAVX2()) {
7113       // Scale the blend by the number of 32-bit dwords per element.
7114       int Scale =  VT.getScalarSizeInBits() / 32;
7115       BlendMask = ScaleBlendMask(BlendMask, Mask.size(), Scale);
7116       MVT BlendVT = VT.getSizeInBits() > 128 ? MVT::v8i32 : MVT::v4i32;
7117       V1 = DAG.getBitcast(BlendVT, V1);
7118       V2 = DAG.getBitcast(BlendVT, V2);
7119       return DAG.getBitcast(
7120           VT, DAG.getNode(X86ISD::BLENDI, DL, BlendVT, V1, V2,
7121                           DAG.getConstant(BlendMask, DL, MVT::i8)));
7122     }
7123     // FALLTHROUGH
7124   case MVT::v8i16: {
7125     // For integer shuffles we need to expand the mask and cast the inputs to
7126     // v8i16s prior to blending.
7127     int Scale = 8 / VT.getVectorNumElements();
7128     BlendMask = ScaleBlendMask(BlendMask, Mask.size(), Scale);
7129     V1 = DAG.getBitcast(MVT::v8i16, V1);
7130     V2 = DAG.getBitcast(MVT::v8i16, V2);
7131     return DAG.getBitcast(VT,
7132                           DAG.getNode(X86ISD::BLENDI, DL, MVT::v8i16, V1, V2,
7133                                       DAG.getConstant(BlendMask, DL, MVT::i8)));
7134   }
7135
7136   case MVT::v16i16: {
7137     assert(Subtarget->hasAVX2() && "256-bit integer blends require AVX2!");
7138     SmallVector<int, 8> RepeatedMask;
7139     if (is128BitLaneRepeatedShuffleMask(MVT::v16i16, Mask, RepeatedMask)) {
7140       // We can lower these with PBLENDW which is mirrored across 128-bit lanes.
7141       assert(RepeatedMask.size() == 8 && "Repeated mask size doesn't match!");
7142       BlendMask = 0;
7143       for (int i = 0; i < 8; ++i)
7144         if (RepeatedMask[i] >= 16)
7145           BlendMask |= 1u << i;
7146       return DAG.getNode(X86ISD::BLENDI, DL, MVT::v16i16, V1, V2,
7147                          DAG.getConstant(BlendMask, DL, MVT::i8));
7148     }
7149   }
7150     // FALLTHROUGH
7151   case MVT::v16i8:
7152   case MVT::v32i8: {
7153     assert((VT.is128BitVector() || Subtarget->hasAVX2()) &&
7154            "256-bit byte-blends require AVX2 support!");
7155
7156     // Attempt to lower to a bitmask if we can. VPAND is faster than VPBLENDVB.
7157     if (SDValue Masked = lowerVectorShuffleAsBitMask(DL, VT, V1, V2, Mask, DAG))
7158       return Masked;
7159
7160     // Scale the blend by the number of bytes per element.
7161     int Scale = VT.getScalarSizeInBits() / 8;
7162
7163     // This form of blend is always done on bytes. Compute the byte vector
7164     // type.
7165     MVT BlendVT = MVT::getVectorVT(MVT::i8, VT.getSizeInBits() / 8);
7166
7167     // Compute the VSELECT mask. Note that VSELECT is really confusing in the
7168     // mix of LLVM's code generator and the x86 backend. We tell the code
7169     // generator that boolean values in the elements of an x86 vector register
7170     // are -1 for true and 0 for false. We then use the LLVM semantics of 'true'
7171     // mapping a select to operand #1, and 'false' mapping to operand #2. The
7172     // reality in x86 is that vector masks (pre-AVX-512) use only the high bit
7173     // of the element (the remaining are ignored) and 0 in that high bit would
7174     // mean operand #1 while 1 in the high bit would mean operand #2. So while
7175     // the LLVM model for boolean values in vector elements gets the relevant
7176     // bit set, it is set backwards and over constrained relative to x86's
7177     // actual model.
7178     SmallVector<SDValue, 32> VSELECTMask;
7179     for (int i = 0, Size = Mask.size(); i < Size; ++i)
7180       for (int j = 0; j < Scale; ++j)
7181         VSELECTMask.push_back(
7182             Mask[i] < 0 ? DAG.getUNDEF(MVT::i8)
7183                         : DAG.getConstant(Mask[i] < Size ? -1 : 0, DL,
7184                                           MVT::i8));
7185
7186     V1 = DAG.getBitcast(BlendVT, V1);
7187     V2 = DAG.getBitcast(BlendVT, V2);
7188     return DAG.getBitcast(VT, DAG.getNode(ISD::VSELECT, DL, BlendVT,
7189                                           DAG.getNode(ISD::BUILD_VECTOR, DL,
7190                                                       BlendVT, VSELECTMask),
7191                                           V1, V2));
7192   }
7193
7194   default:
7195     llvm_unreachable("Not a supported integer vector type!");
7196   }
7197 }
7198
7199 /// \brief Try to lower as a blend of elements from two inputs followed by
7200 /// a single-input permutation.
7201 ///
7202 /// This matches the pattern where we can blend elements from two inputs and
7203 /// then reduce the shuffle to a single-input permutation.
7204 static SDValue lowerVectorShuffleAsBlendAndPermute(SDLoc DL, MVT VT, SDValue V1,
7205                                                    SDValue V2,
7206                                                    ArrayRef<int> Mask,
7207                                                    SelectionDAG &DAG) {
7208   // We build up the blend mask while checking whether a blend is a viable way
7209   // to reduce the shuffle.
7210   SmallVector<int, 32> BlendMask(Mask.size(), -1);
7211   SmallVector<int, 32> PermuteMask(Mask.size(), -1);
7212
7213   for (int i = 0, Size = Mask.size(); i < Size; ++i) {
7214     if (Mask[i] < 0)
7215       continue;
7216
7217     assert(Mask[i] < Size * 2 && "Shuffle input is out of bounds.");
7218
7219     if (BlendMask[Mask[i] % Size] == -1)
7220       BlendMask[Mask[i] % Size] = Mask[i];
7221     else if (BlendMask[Mask[i] % Size] != Mask[i])
7222       return SDValue(); // Can't blend in the needed input!
7223
7224     PermuteMask[i] = Mask[i] % Size;
7225   }
7226
7227   SDValue V = DAG.getVectorShuffle(VT, DL, V1, V2, BlendMask);
7228   return DAG.getVectorShuffle(VT, DL, V, DAG.getUNDEF(VT), PermuteMask);
7229 }
7230
7231 /// \brief Generic routine to decompose a shuffle and blend into indepndent
7232 /// blends and permutes.
7233 ///
7234 /// This matches the extremely common pattern for handling combined
7235 /// shuffle+blend operations on newer X86 ISAs where we have very fast blend
7236 /// operations. It will try to pick the best arrangement of shuffles and
7237 /// blends.
7238 static SDValue lowerVectorShuffleAsDecomposedShuffleBlend(SDLoc DL, MVT VT,
7239                                                           SDValue V1,
7240                                                           SDValue V2,
7241                                                           ArrayRef<int> Mask,
7242                                                           SelectionDAG &DAG) {
7243   // Shuffle the input elements into the desired positions in V1 and V2 and
7244   // blend them together.
7245   SmallVector<int, 32> V1Mask(Mask.size(), -1);
7246   SmallVector<int, 32> V2Mask(Mask.size(), -1);
7247   SmallVector<int, 32> BlendMask(Mask.size(), -1);
7248   for (int i = 0, Size = Mask.size(); i < Size; ++i)
7249     if (Mask[i] >= 0 && Mask[i] < Size) {
7250       V1Mask[i] = Mask[i];
7251       BlendMask[i] = i;
7252     } else if (Mask[i] >= Size) {
7253       V2Mask[i] = Mask[i] - Size;
7254       BlendMask[i] = i + Size;
7255     }
7256
7257   // Try to lower with the simpler initial blend strategy unless one of the
7258   // input shuffles would be a no-op. We prefer to shuffle inputs as the
7259   // shuffle may be able to fold with a load or other benefit. However, when
7260   // we'll have to do 2x as many shuffles in order to achieve this, blending
7261   // first is a better strategy.
7262   if (!isNoopShuffleMask(V1Mask) && !isNoopShuffleMask(V2Mask))
7263     if (SDValue BlendPerm =
7264             lowerVectorShuffleAsBlendAndPermute(DL, VT, V1, V2, Mask, DAG))
7265       return BlendPerm;
7266
7267   V1 = DAG.getVectorShuffle(VT, DL, V1, DAG.getUNDEF(VT), V1Mask);
7268   V2 = DAG.getVectorShuffle(VT, DL, V2, DAG.getUNDEF(VT), V2Mask);
7269   return DAG.getVectorShuffle(VT, DL, V1, V2, BlendMask);
7270 }
7271
7272 /// \brief Try to lower a vector shuffle as a byte rotation.
7273 ///
7274 /// SSSE3 has a generic PALIGNR instruction in x86 that will do an arbitrary
7275 /// byte-rotation of the concatenation of two vectors; pre-SSSE3 can use
7276 /// a PSRLDQ/PSLLDQ/POR pattern to get a similar effect. This routine will
7277 /// try to generically lower a vector shuffle through such an pattern. It
7278 /// does not check for the profitability of lowering either as PALIGNR or
7279 /// PSRLDQ/PSLLDQ/POR, only whether the mask is valid to lower in that form.
7280 /// This matches shuffle vectors that look like:
7281 ///
7282 ///   v8i16 [11, 12, 13, 14, 15, 0, 1, 2]
7283 ///
7284 /// Essentially it concatenates V1 and V2, shifts right by some number of
7285 /// elements, and takes the low elements as the result. Note that while this is
7286 /// specified as a *right shift* because x86 is little-endian, it is a *left
7287 /// rotate* of the vector lanes.
7288 static SDValue lowerVectorShuffleAsByteRotate(SDLoc DL, MVT VT, SDValue V1,
7289                                               SDValue V2,
7290                                               ArrayRef<int> Mask,
7291                                               const X86Subtarget *Subtarget,
7292                                               SelectionDAG &DAG) {
7293   assert(!isNoopShuffleMask(Mask) && "We shouldn't lower no-op shuffles!");
7294
7295   int NumElts = Mask.size();
7296   int NumLanes = VT.getSizeInBits() / 128;
7297   int NumLaneElts = NumElts / NumLanes;
7298
7299   // We need to detect various ways of spelling a rotation:
7300   //   [11, 12, 13, 14, 15,  0,  1,  2]
7301   //   [-1, 12, 13, 14, -1, -1,  1, -1]
7302   //   [-1, -1, -1, -1, -1, -1,  1,  2]
7303   //   [ 3,  4,  5,  6,  7,  8,  9, 10]
7304   //   [-1,  4,  5,  6, -1, -1,  9, -1]
7305   //   [-1,  4,  5,  6, -1, -1, -1, -1]
7306   int Rotation = 0;
7307   SDValue Lo, Hi;
7308   for (int l = 0; l < NumElts; l += NumLaneElts) {
7309     for (int i = 0; i < NumLaneElts; ++i) {
7310       if (Mask[l + i] == -1)
7311         continue;
7312       assert(Mask[l + i] >= 0 && "Only -1 is a valid negative mask element!");
7313
7314       // Get the mod-Size index and lane correct it.
7315       int LaneIdx = (Mask[l + i] % NumElts) - l;
7316       // Make sure it was in this lane.
7317       if (LaneIdx < 0 || LaneIdx >= NumLaneElts)
7318         return SDValue();
7319
7320       // Determine where a rotated vector would have started.
7321       int StartIdx = i - LaneIdx;
7322       if (StartIdx == 0)
7323         // The identity rotation isn't interesting, stop.
7324         return SDValue();
7325
7326       // If we found the tail of a vector the rotation must be the missing
7327       // front. If we found the head of a vector, it must be how much of the
7328       // head.
7329       int CandidateRotation = StartIdx < 0 ? -StartIdx : NumLaneElts - StartIdx;
7330
7331       if (Rotation == 0)
7332         Rotation = CandidateRotation;
7333       else if (Rotation != CandidateRotation)
7334         // The rotations don't match, so we can't match this mask.
7335         return SDValue();
7336
7337       // Compute which value this mask is pointing at.
7338       SDValue MaskV = Mask[l + i] < NumElts ? V1 : V2;
7339
7340       // Compute which of the two target values this index should be assigned
7341       // to. This reflects whether the high elements are remaining or the low
7342       // elements are remaining.
7343       SDValue &TargetV = StartIdx < 0 ? Hi : Lo;
7344
7345       // Either set up this value if we've not encountered it before, or check
7346       // that it remains consistent.
7347       if (!TargetV)
7348         TargetV = MaskV;
7349       else if (TargetV != MaskV)
7350         // This may be a rotation, but it pulls from the inputs in some
7351         // unsupported interleaving.
7352         return SDValue();
7353     }
7354   }
7355
7356   // Check that we successfully analyzed the mask, and normalize the results.
7357   assert(Rotation != 0 && "Failed to locate a viable rotation!");
7358   assert((Lo || Hi) && "Failed to find a rotated input vector!");
7359   if (!Lo)
7360     Lo = Hi;
7361   else if (!Hi)
7362     Hi = Lo;
7363
7364   // The actual rotate instruction rotates bytes, so we need to scale the
7365   // rotation based on how many bytes are in the vector lane.
7366   int Scale = 16 / NumLaneElts;
7367
7368   // SSSE3 targets can use the palignr instruction.
7369   if (Subtarget->hasSSSE3()) {
7370     // Cast the inputs to i8 vector of correct length to match PALIGNR.
7371     MVT AlignVT = MVT::getVectorVT(MVT::i8, 16 * NumLanes);
7372     Lo = DAG.getBitcast(AlignVT, Lo);
7373     Hi = DAG.getBitcast(AlignVT, Hi);
7374
7375     return DAG.getBitcast(
7376         VT, DAG.getNode(X86ISD::PALIGNR, DL, AlignVT, Lo, Hi,
7377                         DAG.getConstant(Rotation * Scale, DL, MVT::i8)));
7378   }
7379
7380   assert(VT.is128BitVector() &&
7381          "Rotate-based lowering only supports 128-bit lowering!");
7382   assert(Mask.size() <= 16 &&
7383          "Can shuffle at most 16 bytes in a 128-bit vector!");
7384
7385   // Default SSE2 implementation
7386   int LoByteShift = 16 - Rotation * Scale;
7387   int HiByteShift = Rotation * Scale;
7388
7389   // Cast the inputs to v2i64 to match PSLLDQ/PSRLDQ.
7390   Lo = DAG.getBitcast(MVT::v2i64, Lo);
7391   Hi = DAG.getBitcast(MVT::v2i64, Hi);
7392
7393   SDValue LoShift = DAG.getNode(X86ISD::VSHLDQ, DL, MVT::v2i64, Lo,
7394                                 DAG.getConstant(LoByteShift, DL, MVT::i8));
7395   SDValue HiShift = DAG.getNode(X86ISD::VSRLDQ, DL, MVT::v2i64, Hi,
7396                                 DAG.getConstant(HiByteShift, DL, MVT::i8));
7397   return DAG.getBitcast(VT,
7398                         DAG.getNode(ISD::OR, DL, MVT::v2i64, LoShift, HiShift));
7399 }
7400
7401 /// \brief Try to lower a vector shuffle as a bit shift (shifts in zeros).
7402 ///
7403 /// Attempts to match a shuffle mask against the PSLL(W/D/Q/DQ) and
7404 /// PSRL(W/D/Q/DQ) SSE2 and AVX2 logical bit-shift instructions. The function
7405 /// matches elements from one of the input vectors shuffled to the left or
7406 /// right with zeroable elements 'shifted in'. It handles both the strictly
7407 /// bit-wise element shifts and the byte shift across an entire 128-bit double
7408 /// quad word lane.
7409 ///
7410 /// PSHL : (little-endian) left bit shift.
7411 /// [ zz, 0, zz,  2 ]
7412 /// [ -1, 4, zz, -1 ]
7413 /// PSRL : (little-endian) right bit shift.
7414 /// [  1, zz,  3, zz]
7415 /// [ -1, -1,  7, zz]
7416 /// PSLLDQ : (little-endian) left byte shift
7417 /// [ zz,  0,  1,  2,  3,  4,  5,  6]
7418 /// [ zz, zz, -1, -1,  2,  3,  4, -1]
7419 /// [ zz, zz, zz, zz, zz, zz, -1,  1]
7420 /// PSRLDQ : (little-endian) right byte shift
7421 /// [  5, 6,  7, zz, zz, zz, zz, zz]
7422 /// [ -1, 5,  6,  7, zz, zz, zz, zz]
7423 /// [  1, 2, -1, -1, -1, -1, zz, zz]
7424 static SDValue lowerVectorShuffleAsShift(SDLoc DL, MVT VT, SDValue V1,
7425                                          SDValue V2, ArrayRef<int> Mask,
7426                                          SelectionDAG &DAG) {
7427   SmallBitVector Zeroable = computeZeroableShuffleElements(Mask, V1, V2);
7428
7429   int Size = Mask.size();
7430   assert(Size == (int)VT.getVectorNumElements() && "Unexpected mask size");
7431
7432   auto CheckZeros = [&](int Shift, int Scale, bool Left) {
7433     for (int i = 0; i < Size; i += Scale)
7434       for (int j = 0; j < Shift; ++j)
7435         if (!Zeroable[i + j + (Left ? 0 : (Scale - Shift))])
7436           return false;
7437
7438     return true;
7439   };
7440
7441   auto MatchShift = [&](int Shift, int Scale, bool Left, SDValue V) {
7442     for (int i = 0; i != Size; i += Scale) {
7443       unsigned Pos = Left ? i + Shift : i;
7444       unsigned Low = Left ? i : i + Shift;
7445       unsigned Len = Scale - Shift;
7446       if (!isSequentialOrUndefInRange(Mask, Pos, Len,
7447                                       Low + (V == V1 ? 0 : Size)))
7448         return SDValue();
7449     }
7450
7451     int ShiftEltBits = VT.getScalarSizeInBits() * Scale;
7452     bool ByteShift = ShiftEltBits > 64;
7453     unsigned OpCode = Left ? (ByteShift ? X86ISD::VSHLDQ : X86ISD::VSHLI)
7454                            : (ByteShift ? X86ISD::VSRLDQ : X86ISD::VSRLI);
7455     int ShiftAmt = Shift * VT.getScalarSizeInBits() / (ByteShift ? 8 : 1);
7456
7457     // Normalize the scale for byte shifts to still produce an i64 element
7458     // type.
7459     Scale = ByteShift ? Scale / 2 : Scale;
7460
7461     // We need to round trip through the appropriate type for the shift.
7462     MVT ShiftSVT = MVT::getIntegerVT(VT.getScalarSizeInBits() * Scale);
7463     MVT ShiftVT = MVT::getVectorVT(ShiftSVT, Size / Scale);
7464     assert(DAG.getTargetLoweringInfo().isTypeLegal(ShiftVT) &&
7465            "Illegal integer vector type");
7466     V = DAG.getBitcast(ShiftVT, V);
7467
7468     V = DAG.getNode(OpCode, DL, ShiftVT, V,
7469                     DAG.getConstant(ShiftAmt, DL, MVT::i8));
7470     return DAG.getBitcast(VT, V);
7471   };
7472
7473   // SSE/AVX supports logical shifts up to 64-bit integers - so we can just
7474   // keep doubling the size of the integer elements up to that. We can
7475   // then shift the elements of the integer vector by whole multiples of
7476   // their width within the elements of the larger integer vector. Test each
7477   // multiple to see if we can find a match with the moved element indices
7478   // and that the shifted in elements are all zeroable.
7479   for (int Scale = 2; Scale * VT.getScalarSizeInBits() <= 128; Scale *= 2)
7480     for (int Shift = 1; Shift != Scale; ++Shift)
7481       for (bool Left : {true, false})
7482         if (CheckZeros(Shift, Scale, Left))
7483           for (SDValue V : {V1, V2})
7484             if (SDValue Match = MatchShift(Shift, Scale, Left, V))
7485               return Match;
7486
7487   // no match
7488   return SDValue();
7489 }
7490
7491 /// \brief Try to lower a vector shuffle using SSE4a EXTRQ/INSERTQ.
7492 static SDValue lowerVectorShuffleWithSSE4A(SDLoc DL, MVT VT, SDValue V1,
7493                                            SDValue V2, ArrayRef<int> Mask,
7494                                            SelectionDAG &DAG) {
7495   SmallBitVector Zeroable = computeZeroableShuffleElements(Mask, V1, V2);
7496   assert(!Zeroable.all() && "Fully zeroable shuffle mask");
7497
7498   int Size = Mask.size();
7499   int HalfSize = Size / 2;
7500   assert(Size == (int)VT.getVectorNumElements() && "Unexpected mask size");
7501
7502   // Upper half must be undefined.
7503   if (!isUndefInRange(Mask, HalfSize, HalfSize))
7504     return SDValue();
7505
7506   // EXTRQ: Extract Len elements from lower half of source, starting at Idx.
7507   // Remainder of lower half result is zero and upper half is all undef.
7508   auto LowerAsEXTRQ = [&]() {
7509     // Determine the extraction length from the part of the
7510     // lower half that isn't zeroable.
7511     int Len = HalfSize;
7512     for (; Len > 0; --Len)
7513       if (!Zeroable[Len - 1])
7514         break;
7515     assert(Len > 0 && "Zeroable shuffle mask");
7516
7517     // Attempt to match first Len sequential elements from the lower half.
7518     SDValue Src;
7519     int Idx = -1;
7520     for (int i = 0; i != Len; ++i) {
7521       int M = Mask[i];
7522       if (M < 0)
7523         continue;
7524       SDValue &V = (M < Size ? V1 : V2);
7525       M = M % Size;
7526
7527       // The extracted elements must start at a valid index and all mask
7528       // elements must be in the lower half.
7529       if (i > M || M >= HalfSize)
7530         return SDValue();
7531
7532       if (Idx < 0 || (Src == V && Idx == (M - i))) {
7533         Src = V;
7534         Idx = M - i;
7535         continue;
7536       }
7537       return SDValue();
7538     }
7539
7540     if (Idx < 0)
7541       return SDValue();
7542
7543     assert((Idx + Len) <= HalfSize && "Illegal extraction mask");
7544     int BitLen = (Len * VT.getScalarSizeInBits()) & 0x3f;
7545     int BitIdx = (Idx * VT.getScalarSizeInBits()) & 0x3f;
7546     return DAG.getNode(X86ISD::EXTRQI, DL, VT, Src,
7547                        DAG.getConstant(BitLen, DL, MVT::i8),
7548                        DAG.getConstant(BitIdx, DL, MVT::i8));
7549   };
7550
7551   if (SDValue ExtrQ = LowerAsEXTRQ())
7552     return ExtrQ;
7553
7554   // INSERTQ: Extract lowest Len elements from lower half of second source and
7555   // insert over first source, starting at Idx.
7556   // { A[0], .., A[Idx-1], B[0], .., B[Len-1], A[Idx+Len], .., UNDEF, ... }
7557   auto LowerAsInsertQ = [&]() {
7558     for (int Idx = 0; Idx != HalfSize; ++Idx) {
7559       SDValue Base;
7560
7561       // Attempt to match first source from mask before insertion point.
7562       if (isUndefInRange(Mask, 0, Idx)) {
7563         /* EMPTY */
7564       } else if (isSequentialOrUndefInRange(Mask, 0, Idx, 0)) {
7565         Base = V1;
7566       } else if (isSequentialOrUndefInRange(Mask, 0, Idx, Size)) {
7567         Base = V2;
7568       } else {
7569         continue;
7570       }
7571
7572       // Extend the extraction length looking to match both the insertion of
7573       // the second source and the remaining elements of the first.
7574       for (int Hi = Idx + 1; Hi <= HalfSize; ++Hi) {
7575         SDValue Insert;
7576         int Len = Hi - Idx;
7577
7578         // Match insertion.
7579         if (isSequentialOrUndefInRange(Mask, Idx, Len, 0)) {
7580           Insert = V1;
7581         } else if (isSequentialOrUndefInRange(Mask, Idx, Len, Size)) {
7582           Insert = V2;
7583         } else {
7584           continue;
7585         }
7586
7587         // Match the remaining elements of the lower half.
7588         if (isUndefInRange(Mask, Hi, HalfSize - Hi)) {
7589           /* EMPTY */
7590         } else if ((!Base || (Base == V1)) &&
7591                    isSequentialOrUndefInRange(Mask, Hi, HalfSize - Hi, Hi)) {
7592           Base = V1;
7593         } else if ((!Base || (Base == V2)) &&
7594                    isSequentialOrUndefInRange(Mask, Hi, HalfSize - Hi,
7595                                               Size + Hi)) {
7596           Base = V2;
7597         } else {
7598           continue;
7599         }
7600
7601         // We may not have a base (first source) - this can safely be undefined.
7602         if (!Base)
7603           Base = DAG.getUNDEF(VT);
7604
7605         int BitLen = (Len * VT.getScalarSizeInBits()) & 0x3f;
7606         int BitIdx = (Idx * VT.getScalarSizeInBits()) & 0x3f;
7607         return DAG.getNode(X86ISD::INSERTQI, DL, VT, Base, Insert,
7608                            DAG.getConstant(BitLen, DL, MVT::i8),
7609                            DAG.getConstant(BitIdx, DL, MVT::i8));
7610       }
7611     }
7612
7613     return SDValue();
7614   };
7615
7616   if (SDValue InsertQ = LowerAsInsertQ())
7617     return InsertQ;
7618
7619   return SDValue();
7620 }
7621
7622 /// \brief Lower a vector shuffle as a zero or any extension.
7623 ///
7624 /// Given a specific number of elements, element bit width, and extension
7625 /// stride, produce either a zero or any extension based on the available
7626 /// features of the subtarget. The extended elements are consecutive and
7627 /// begin and can start from an offseted element index in the input; to
7628 /// avoid excess shuffling the offset must either being in the bottom lane
7629 /// or at the start of a higher lane. All extended elements must be from
7630 /// the same lane.
7631 static SDValue lowerVectorShuffleAsSpecificZeroOrAnyExtend(
7632     SDLoc DL, MVT VT, int Scale, int Offset, bool AnyExt, SDValue InputV,
7633     ArrayRef<int> Mask, const X86Subtarget *Subtarget, SelectionDAG &DAG) {
7634   assert(Scale > 1 && "Need a scale to extend.");
7635   int EltBits = VT.getScalarSizeInBits();
7636   int NumElements = VT.getVectorNumElements();
7637   int NumEltsPerLane = 128 / EltBits;
7638   int OffsetLane = Offset / NumEltsPerLane;
7639   assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
7640          "Only 8, 16, and 32 bit elements can be extended.");
7641   assert(Scale * EltBits <= 64 && "Cannot zero extend past 64 bits.");
7642   assert(0 <= Offset && "Extension offset must be positive.");
7643   assert((Offset < NumEltsPerLane || Offset % NumEltsPerLane == 0) &&
7644          "Extension offset must be in the first lane or start an upper lane.");
7645
7646   // Check that an index is in same lane as the base offset.
7647   auto SafeOffset = [&](int Idx) {
7648     return OffsetLane == (Idx / NumEltsPerLane);
7649   };
7650
7651   // Shift along an input so that the offset base moves to the first element.
7652   auto ShuffleOffset = [&](SDValue V) {
7653     if (!Offset)
7654       return V;
7655
7656     SmallVector<int, 8> ShMask((unsigned)NumElements, -1);
7657     for (int i = 0; i * Scale < NumElements; ++i) {
7658       int SrcIdx = i + Offset;
7659       ShMask[i] = SafeOffset(SrcIdx) ? SrcIdx : -1;
7660     }
7661     return DAG.getVectorShuffle(VT, DL, V, DAG.getUNDEF(VT), ShMask);
7662   };
7663
7664   // Found a valid zext mask! Try various lowering strategies based on the
7665   // input type and available ISA extensions.
7666   if (Subtarget->hasSSE41()) {
7667     // Not worth offseting 128-bit vectors if scale == 2, a pattern using
7668     // PUNPCK will catch this in a later shuffle match.
7669     if (Offset && Scale == 2 && VT.is128BitVector())
7670       return SDValue();
7671     MVT ExtVT = MVT::getVectorVT(MVT::getIntegerVT(EltBits * Scale),
7672                                  NumElements / Scale);
7673     InputV = DAG.getNode(X86ISD::VZEXT, DL, ExtVT, ShuffleOffset(InputV));
7674     return DAG.getBitcast(VT, InputV);
7675   }
7676
7677   assert(VT.is128BitVector() && "Only 128-bit vectors can be extended.");
7678
7679   // For any extends we can cheat for larger element sizes and use shuffle
7680   // instructions that can fold with a load and/or copy.
7681   if (AnyExt && EltBits == 32) {
7682     int PSHUFDMask[4] = {Offset, -1, SafeOffset(Offset + 1) ? Offset + 1 : -1,
7683                          -1};
7684     return DAG.getBitcast(
7685         VT, DAG.getNode(X86ISD::PSHUFD, DL, MVT::v4i32,
7686                         DAG.getBitcast(MVT::v4i32, InputV),
7687                         getV4X86ShuffleImm8ForMask(PSHUFDMask, DL, DAG)));
7688   }
7689   if (AnyExt && EltBits == 16 && Scale > 2) {
7690     int PSHUFDMask[4] = {Offset / 2, -1,
7691                          SafeOffset(Offset + 1) ? (Offset + 1) / 2 : -1, -1};
7692     InputV = DAG.getNode(X86ISD::PSHUFD, DL, MVT::v4i32,
7693                          DAG.getBitcast(MVT::v4i32, InputV),
7694                          getV4X86ShuffleImm8ForMask(PSHUFDMask, DL, DAG));
7695     int PSHUFWMask[4] = {1, -1, -1, -1};
7696     unsigned OddEvenOp = (Offset & 1 ? X86ISD::PSHUFLW : X86ISD::PSHUFHW);
7697     return DAG.getBitcast(
7698         VT, DAG.getNode(OddEvenOp, DL, MVT::v8i16,
7699                         DAG.getBitcast(MVT::v8i16, InputV),
7700                         getV4X86ShuffleImm8ForMask(PSHUFWMask, DL, DAG)));
7701   }
7702
7703   // The SSE4A EXTRQ instruction can efficiently extend the first 2 lanes
7704   // to 64-bits.
7705   if ((Scale * EltBits) == 64 && EltBits < 32 && Subtarget->hasSSE4A()) {
7706     assert(NumElements == (int)Mask.size() && "Unexpected shuffle mask size!");
7707     assert(VT.is128BitVector() && "Unexpected vector width!");
7708
7709     int LoIdx = Offset * EltBits;
7710     SDValue Lo = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64,
7711                              DAG.getNode(X86ISD::EXTRQI, DL, VT, InputV,
7712                                          DAG.getConstant(EltBits, DL, MVT::i8),
7713                                          DAG.getConstant(LoIdx, DL, MVT::i8)));
7714
7715     if (isUndefInRange(Mask, NumElements / 2, NumElements / 2) ||
7716         !SafeOffset(Offset + 1))
7717       return DAG.getNode(ISD::BITCAST, DL, VT, Lo);
7718
7719     int HiIdx = (Offset + 1) * EltBits;
7720     SDValue Hi = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64,
7721                              DAG.getNode(X86ISD::EXTRQI, DL, VT, InputV,
7722                                          DAG.getConstant(EltBits, DL, MVT::i8),
7723                                          DAG.getConstant(HiIdx, DL, MVT::i8)));
7724     return DAG.getNode(ISD::BITCAST, DL, VT,
7725                        DAG.getNode(X86ISD::UNPCKL, DL, MVT::v2i64, Lo, Hi));
7726   }
7727
7728   // If this would require more than 2 unpack instructions to expand, use
7729   // pshufb when available. We can only use more than 2 unpack instructions
7730   // when zero extending i8 elements which also makes it easier to use pshufb.
7731   if (Scale > 4 && EltBits == 8 && Subtarget->hasSSSE3()) {
7732     assert(NumElements == 16 && "Unexpected byte vector width!");
7733     SDValue PSHUFBMask[16];
7734     for (int i = 0; i < 16; ++i) {
7735       int Idx = Offset + (i / Scale);
7736       PSHUFBMask[i] = DAG.getConstant(
7737           (i % Scale == 0 && SafeOffset(Idx)) ? Idx : 0x80, DL, MVT::i8);
7738     }
7739     InputV = DAG.getBitcast(MVT::v16i8, InputV);
7740     return DAG.getBitcast(VT,
7741                           DAG.getNode(X86ISD::PSHUFB, DL, MVT::v16i8, InputV,
7742                                       DAG.getNode(ISD::BUILD_VECTOR, DL,
7743                                                   MVT::v16i8, PSHUFBMask)));
7744   }
7745
7746   // If we are extending from an offset, ensure we start on a boundary that
7747   // we can unpack from.
7748   int AlignToUnpack = Offset % (NumElements / Scale);
7749   if (AlignToUnpack) {
7750     SmallVector<int, 8> ShMask((unsigned)NumElements, -1);
7751     for (int i = AlignToUnpack; i < NumElements; ++i)
7752       ShMask[i - AlignToUnpack] = i;
7753     InputV = DAG.getVectorShuffle(VT, DL, InputV, DAG.getUNDEF(VT), ShMask);
7754     Offset -= AlignToUnpack;
7755   }
7756
7757   // Otherwise emit a sequence of unpacks.
7758   do {
7759     unsigned UnpackLoHi = X86ISD::UNPCKL;
7760     if (Offset >= (NumElements / 2)) {
7761       UnpackLoHi = X86ISD::UNPCKH;
7762       Offset -= (NumElements / 2);
7763     }
7764
7765     MVT InputVT = MVT::getVectorVT(MVT::getIntegerVT(EltBits), NumElements);
7766     SDValue Ext = AnyExt ? DAG.getUNDEF(InputVT)
7767                          : getZeroVector(InputVT, Subtarget, DAG, DL);
7768     InputV = DAG.getBitcast(InputVT, InputV);
7769     InputV = DAG.getNode(UnpackLoHi, DL, InputVT, InputV, Ext);
7770     Scale /= 2;
7771     EltBits *= 2;
7772     NumElements /= 2;
7773   } while (Scale > 1);
7774   return DAG.getBitcast(VT, InputV);
7775 }
7776
7777 /// \brief Try to lower a vector shuffle as a zero extension on any microarch.
7778 ///
7779 /// This routine will try to do everything in its power to cleverly lower
7780 /// a shuffle which happens to match the pattern of a zero extend. It doesn't
7781 /// check for the profitability of this lowering,  it tries to aggressively
7782 /// match this pattern. It will use all of the micro-architectural details it
7783 /// can to emit an efficient lowering. It handles both blends with all-zero
7784 /// inputs to explicitly zero-extend and undef-lanes (sometimes undef due to
7785 /// masking out later).
7786 ///
7787 /// The reason we have dedicated lowering for zext-style shuffles is that they
7788 /// are both incredibly common and often quite performance sensitive.
7789 static SDValue lowerVectorShuffleAsZeroOrAnyExtend(
7790     SDLoc DL, MVT VT, SDValue V1, SDValue V2, ArrayRef<int> Mask,
7791     const X86Subtarget *Subtarget, SelectionDAG &DAG) {
7792   SmallBitVector Zeroable = computeZeroableShuffleElements(Mask, V1, V2);
7793
7794   int Bits = VT.getSizeInBits();
7795   int NumLanes = Bits / 128;
7796   int NumElements = VT.getVectorNumElements();
7797   int NumEltsPerLane = NumElements / NumLanes;
7798   assert(VT.getScalarSizeInBits() <= 32 &&
7799          "Exceeds 32-bit integer zero extension limit");
7800   assert((int)Mask.size() == NumElements && "Unexpected shuffle mask size");
7801
7802   // Define a helper function to check a particular ext-scale and lower to it if
7803   // valid.
7804   auto Lower = [&](int Scale) -> SDValue {
7805     SDValue InputV;
7806     bool AnyExt = true;
7807     int Offset = 0;
7808     int Matches = 0;
7809     for (int i = 0; i < NumElements; ++i) {
7810       int M = Mask[i];
7811       if (M == -1)
7812         continue; // Valid anywhere but doesn't tell us anything.
7813       if (i % Scale != 0) {
7814         // Each of the extended elements need to be zeroable.
7815         if (!Zeroable[i])
7816           return SDValue();
7817
7818         // We no longer are in the anyext case.
7819         AnyExt = false;
7820         continue;
7821       }
7822
7823       // Each of the base elements needs to be consecutive indices into the
7824       // same input vector.
7825       SDValue V = M < NumElements ? V1 : V2;
7826       M = M % NumElements;
7827       if (!InputV) {
7828         InputV = V;
7829         Offset = M - (i / Scale);
7830       } else if (InputV != V)
7831         return SDValue(); // Flip-flopping inputs.
7832
7833       // Offset must start in the lowest 128-bit lane or at the start of an
7834       // upper lane.
7835       // FIXME: Is it ever worth allowing a negative base offset?
7836       if (!((0 <= Offset && Offset < NumEltsPerLane) ||
7837             (Offset % NumEltsPerLane) == 0))
7838         return SDValue();
7839
7840       // If we are offsetting, all referenced entries must come from the same
7841       // lane.
7842       if (Offset && (Offset / NumEltsPerLane) != (M / NumEltsPerLane))
7843         return SDValue();
7844
7845       if ((M % NumElements) != (Offset + (i / Scale)))
7846         return SDValue(); // Non-consecutive strided elements.
7847       Matches++;
7848     }
7849
7850     // If we fail to find an input, we have a zero-shuffle which should always
7851     // have already been handled.
7852     // FIXME: Maybe handle this here in case during blending we end up with one?
7853     if (!InputV)
7854       return SDValue();
7855
7856     // If we are offsetting, don't extend if we only match a single input, we
7857     // can always do better by using a basic PSHUF or PUNPCK.
7858     if (Offset != 0 && Matches < 2)
7859       return SDValue();
7860
7861     return lowerVectorShuffleAsSpecificZeroOrAnyExtend(
7862         DL, VT, Scale, Offset, AnyExt, InputV, Mask, Subtarget, DAG);
7863   };
7864
7865   // The widest scale possible for extending is to a 64-bit integer.
7866   assert(Bits % 64 == 0 &&
7867          "The number of bits in a vector must be divisible by 64 on x86!");
7868   int NumExtElements = Bits / 64;
7869
7870   // Each iteration, try extending the elements half as much, but into twice as
7871   // many elements.
7872   for (; NumExtElements < NumElements; NumExtElements *= 2) {
7873     assert(NumElements % NumExtElements == 0 &&
7874            "The input vector size must be divisible by the extended size.");
7875     if (SDValue V = Lower(NumElements / NumExtElements))
7876       return V;
7877   }
7878
7879   // General extends failed, but 128-bit vectors may be able to use MOVQ.
7880   if (Bits != 128)
7881     return SDValue();
7882
7883   // Returns one of the source operands if the shuffle can be reduced to a
7884   // MOVQ, copying the lower 64-bits and zero-extending to the upper 64-bits.
7885   auto CanZExtLowHalf = [&]() {
7886     for (int i = NumElements / 2; i != NumElements; ++i)
7887       if (!Zeroable[i])
7888         return SDValue();
7889     if (isSequentialOrUndefInRange(Mask, 0, NumElements / 2, 0))
7890       return V1;
7891     if (isSequentialOrUndefInRange(Mask, 0, NumElements / 2, NumElements))
7892       return V2;
7893     return SDValue();
7894   };
7895
7896   if (SDValue V = CanZExtLowHalf()) {
7897     V = DAG.getBitcast(MVT::v2i64, V);
7898     V = DAG.getNode(X86ISD::VZEXT_MOVL, DL, MVT::v2i64, V);
7899     return DAG.getBitcast(VT, V);
7900   }
7901
7902   // No viable ext lowering found.
7903   return SDValue();
7904 }
7905
7906 /// \brief Try to get a scalar value for a specific element of a vector.
7907 ///
7908 /// Looks through BUILD_VECTOR and SCALAR_TO_VECTOR nodes to find a scalar.
7909 static SDValue getScalarValueForVectorElement(SDValue V, int Idx,
7910                                               SelectionDAG &DAG) {
7911   MVT VT = V.getSimpleValueType();
7912   MVT EltVT = VT.getVectorElementType();
7913   while (V.getOpcode() == ISD::BITCAST)
7914     V = V.getOperand(0);
7915   // If the bitcasts shift the element size, we can't extract an equivalent
7916   // element from it.
7917   MVT NewVT = V.getSimpleValueType();
7918   if (!NewVT.isVector() || NewVT.getScalarSizeInBits() != VT.getScalarSizeInBits())
7919     return SDValue();
7920
7921   if (V.getOpcode() == ISD::BUILD_VECTOR ||
7922       (Idx == 0 && V.getOpcode() == ISD::SCALAR_TO_VECTOR)) {
7923     // Ensure the scalar operand is the same size as the destination.
7924     // FIXME: Add support for scalar truncation where possible.
7925     SDValue S = V.getOperand(Idx);
7926     if (EltVT.getSizeInBits() == S.getSimpleValueType().getSizeInBits())
7927       return DAG.getNode(ISD::BITCAST, SDLoc(V), EltVT, S);
7928   }
7929
7930   return SDValue();
7931 }
7932
7933 /// \brief Helper to test for a load that can be folded with x86 shuffles.
7934 ///
7935 /// This is particularly important because the set of instructions varies
7936 /// significantly based on whether the operand is a load or not.
7937 static bool isShuffleFoldableLoad(SDValue V) {
7938   while (V.getOpcode() == ISD::BITCAST)
7939     V = V.getOperand(0);
7940
7941   return ISD::isNON_EXTLoad(V.getNode());
7942 }
7943
7944 /// \brief Try to lower insertion of a single element into a zero vector.
7945 ///
7946 /// This is a common pattern that we have especially efficient patterns to lower
7947 /// across all subtarget feature sets.
7948 static SDValue lowerVectorShuffleAsElementInsertion(
7949     SDLoc DL, MVT VT, SDValue V1, SDValue V2, ArrayRef<int> Mask,
7950     const X86Subtarget *Subtarget, SelectionDAG &DAG) {
7951   SmallBitVector Zeroable = computeZeroableShuffleElements(Mask, V1, V2);
7952   MVT ExtVT = VT;
7953   MVT EltVT = VT.getVectorElementType();
7954
7955   int V2Index = std::find_if(Mask.begin(), Mask.end(),
7956                              [&Mask](int M) { return M >= (int)Mask.size(); }) -
7957                 Mask.begin();
7958   bool IsV1Zeroable = true;
7959   for (int i = 0, Size = Mask.size(); i < Size; ++i)
7960     if (i != V2Index && !Zeroable[i]) {
7961       IsV1Zeroable = false;
7962       break;
7963     }
7964
7965   // Check for a single input from a SCALAR_TO_VECTOR node.
7966   // FIXME: All of this should be canonicalized into INSERT_VECTOR_ELT and
7967   // all the smarts here sunk into that routine. However, the current
7968   // lowering of BUILD_VECTOR makes that nearly impossible until the old
7969   // vector shuffle lowering is dead.
7970   SDValue V2S = getScalarValueForVectorElement(V2, Mask[V2Index] - Mask.size(),
7971                                                DAG);
7972   if (V2S && DAG.getTargetLoweringInfo().isTypeLegal(V2S.getValueType())) {
7973     // We need to zext the scalar if it is smaller than an i32.
7974     V2S = DAG.getBitcast(EltVT, V2S);
7975     if (EltVT == MVT::i8 || EltVT == MVT::i16) {
7976       // Using zext to expand a narrow element won't work for non-zero
7977       // insertions.
7978       if (!IsV1Zeroable)
7979         return SDValue();
7980
7981       // Zero-extend directly to i32.
7982       ExtVT = MVT::v4i32;
7983       V2S = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i32, V2S);
7984     }
7985     V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, ExtVT, V2S);
7986   } else if (Mask[V2Index] != (int)Mask.size() || EltVT == MVT::i8 ||
7987              EltVT == MVT::i16) {
7988     // Either not inserting from the low element of the input or the input
7989     // element size is too small to use VZEXT_MOVL to clear the high bits.
7990     return SDValue();
7991   }
7992
7993   if (!IsV1Zeroable) {
7994     // If V1 can't be treated as a zero vector we have fewer options to lower
7995     // this. We can't support integer vectors or non-zero targets cheaply, and
7996     // the V1 elements can't be permuted in any way.
7997     assert(VT == ExtVT && "Cannot change extended type when non-zeroable!");
7998     if (!VT.isFloatingPoint() || V2Index != 0)
7999       return SDValue();
8000     SmallVector<int, 8> V1Mask(Mask.begin(), Mask.end());
8001     V1Mask[V2Index] = -1;
8002     if (!isNoopShuffleMask(V1Mask))
8003       return SDValue();
8004     // This is essentially a special case blend operation, but if we have
8005     // general purpose blend operations, they are always faster. Bail and let
8006     // the rest of the lowering handle these as blends.
8007     if (Subtarget->hasSSE41())
8008       return SDValue();
8009
8010     // Otherwise, use MOVSD or MOVSS.
8011     assert((EltVT == MVT::f32 || EltVT == MVT::f64) &&
8012            "Only two types of floating point element types to handle!");
8013     return DAG.getNode(EltVT == MVT::f32 ? X86ISD::MOVSS : X86ISD::MOVSD, DL,
8014                        ExtVT, V1, V2);
8015   }
8016
8017   // This lowering only works for the low element with floating point vectors.
8018   if (VT.isFloatingPoint() && V2Index != 0)
8019     return SDValue();
8020
8021   V2 = DAG.getNode(X86ISD::VZEXT_MOVL, DL, ExtVT, V2);
8022   if (ExtVT != VT)
8023     V2 = DAG.getBitcast(VT, V2);
8024
8025   if (V2Index != 0) {
8026     // If we have 4 or fewer lanes we can cheaply shuffle the element into
8027     // the desired position. Otherwise it is more efficient to do a vector
8028     // shift left. We know that we can do a vector shift left because all
8029     // the inputs are zero.
8030     if (VT.isFloatingPoint() || VT.getVectorNumElements() <= 4) {
8031       SmallVector<int, 4> V2Shuffle(Mask.size(), 1);
8032       V2Shuffle[V2Index] = 0;
8033       V2 = DAG.getVectorShuffle(VT, DL, V2, DAG.getUNDEF(VT), V2Shuffle);
8034     } else {
8035       V2 = DAG.getBitcast(MVT::v2i64, V2);
8036       V2 = DAG.getNode(
8037           X86ISD::VSHLDQ, DL, MVT::v2i64, V2,
8038           DAG.getConstant(V2Index * EltVT.getSizeInBits() / 8, DL,
8039                           DAG.getTargetLoweringInfo().getScalarShiftAmountTy(
8040                               DAG.getDataLayout(), VT)));
8041       V2 = DAG.getBitcast(VT, V2);
8042     }
8043   }
8044   return V2;
8045 }
8046
8047 /// \brief Try to lower broadcast of a single - truncated - integer element,
8048 /// coming from a scalar_to_vector/build_vector node \p V0 with larger elements.
8049 ///
8050 /// This assumes we have AVX2.
8051 static SDValue lowerVectorShuffleAsTruncBroadcast(SDLoc DL, MVT VT, SDValue V0,
8052                                                   int BroadcastIdx,
8053                                                   const X86Subtarget *Subtarget,
8054                                                   SelectionDAG &DAG) {
8055   assert(Subtarget->hasAVX2() &&
8056          "We can only lower integer broadcasts with AVX2!");
8057
8058   EVT EltVT = VT.getVectorElementType();
8059   EVT V0VT = V0.getValueType();
8060
8061   assert(VT.isInteger() && "Unexpected non-integer trunc broadcast!");
8062   assert(V0VT.isVector() && "Unexpected non-vector vector-sized value!");
8063
8064   EVT V0EltVT = V0VT.getVectorElementType();
8065   if (!V0EltVT.isInteger())
8066     return SDValue();
8067
8068   const unsigned EltSize = EltVT.getSizeInBits();
8069   const unsigned V0EltSize = V0EltVT.getSizeInBits();
8070
8071   // This is only a truncation if the original element type is larger.
8072   if (V0EltSize <= EltSize)
8073     return SDValue();
8074
8075   assert(((V0EltSize % EltSize) == 0) &&
8076          "Scalar type sizes must all be powers of 2 on x86!");
8077
8078   const unsigned V0Opc = V0.getOpcode();
8079   const unsigned Scale = V0EltSize / EltSize;
8080   const unsigned V0BroadcastIdx = BroadcastIdx / Scale;
8081
8082   if ((V0Opc != ISD::SCALAR_TO_VECTOR || V0BroadcastIdx != 0) &&
8083       V0Opc != ISD::BUILD_VECTOR)
8084     return SDValue();
8085
8086   SDValue Scalar = V0.getOperand(V0BroadcastIdx);
8087
8088   // If we're extracting non-least-significant bits, shift so we can truncate.
8089   // Hopefully, we can fold away the trunc/srl/load into the broadcast.
8090   // Even if we can't (and !isShuffleFoldableLoad(Scalar)), prefer
8091   // vpbroadcast+vmovd+shr to vpshufb(m)+vmovd.
8092   if (const int OffsetIdx = BroadcastIdx % Scale)
8093     Scalar = DAG.getNode(ISD::SRL, DL, Scalar.getValueType(), Scalar,
8094             DAG.getConstant(OffsetIdx * EltSize, DL, Scalar.getValueType()));
8095
8096   return DAG.getNode(X86ISD::VBROADCAST, DL, VT,
8097                      DAG.getNode(ISD::TRUNCATE, DL, EltVT, Scalar));
8098 }
8099
8100 /// \brief Try to lower broadcast of a single element.
8101 ///
8102 /// For convenience, this code also bundles all of the subtarget feature set
8103 /// filtering. While a little annoying to re-dispatch on type here, there isn't
8104 /// a convenient way to factor it out.
8105 /// FIXME: This is very similar to LowerVectorBroadcast - can we merge them?
8106 static SDValue lowerVectorShuffleAsBroadcast(SDLoc DL, MVT VT, SDValue V,
8107                                              ArrayRef<int> Mask,
8108                                              const X86Subtarget *Subtarget,
8109                                              SelectionDAG &DAG) {
8110   if (!Subtarget->hasAVX())
8111     return SDValue();
8112   if (VT.isInteger() && !Subtarget->hasAVX2())
8113     return SDValue();
8114
8115   // Check that the mask is a broadcast.
8116   int BroadcastIdx = -1;
8117   for (int M : Mask)
8118     if (M >= 0 && BroadcastIdx == -1)
8119       BroadcastIdx = M;
8120     else if (M >= 0 && M != BroadcastIdx)
8121       return SDValue();
8122
8123   assert(BroadcastIdx < (int)Mask.size() && "We only expect to be called with "
8124                                             "a sorted mask where the broadcast "
8125                                             "comes from V1.");
8126
8127   // Go up the chain of (vector) values to find a scalar load that we can
8128   // combine with the broadcast.
8129   for (;;) {
8130     switch (V.getOpcode()) {
8131     case ISD::CONCAT_VECTORS: {
8132       int OperandSize = Mask.size() / V.getNumOperands();
8133       V = V.getOperand(BroadcastIdx / OperandSize);
8134       BroadcastIdx %= OperandSize;
8135       continue;
8136     }
8137
8138     case ISD::INSERT_SUBVECTOR: {
8139       SDValue VOuter = V.getOperand(0), VInner = V.getOperand(1);
8140       auto ConstantIdx = dyn_cast<ConstantSDNode>(V.getOperand(2));
8141       if (!ConstantIdx)
8142         break;
8143
8144       int BeginIdx = (int)ConstantIdx->getZExtValue();
8145       int EndIdx =
8146           BeginIdx + (int)VInner.getSimpleValueType().getVectorNumElements();
8147       if (BroadcastIdx >= BeginIdx && BroadcastIdx < EndIdx) {
8148         BroadcastIdx -= BeginIdx;
8149         V = VInner;
8150       } else {
8151         V = VOuter;
8152       }
8153       continue;
8154     }
8155     }
8156     break;
8157   }
8158
8159   // Check if this is a broadcast of a scalar. We special case lowering
8160   // for scalars so that we can more effectively fold with loads.
8161   // First, look through bitcast: if the original value has a larger element
8162   // type than the shuffle, the broadcast element is in essence truncated.
8163   // Make that explicit to ease folding.
8164   if (V.getOpcode() == ISD::BITCAST && VT.isInteger())
8165     if (SDValue TruncBroadcast = lowerVectorShuffleAsTruncBroadcast(
8166             DL, VT, V.getOperand(0), BroadcastIdx, Subtarget, DAG))
8167       return TruncBroadcast;
8168
8169   // Also check the simpler case, where we can directly reuse the scalar.
8170   if (V.getOpcode() == ISD::BUILD_VECTOR ||
8171       (V.getOpcode() == ISD::SCALAR_TO_VECTOR && BroadcastIdx == 0)) {
8172     V = V.getOperand(BroadcastIdx);
8173
8174     // If the scalar isn't a load, we can't broadcast from it in AVX1.
8175     // Only AVX2 has register broadcasts.
8176     if (!Subtarget->hasAVX2() && !isShuffleFoldableLoad(V))
8177       return SDValue();
8178   } else if (MayFoldLoad(V) && !cast<LoadSDNode>(V)->isVolatile()) {
8179     // If we are broadcasting a load that is only used by the shuffle
8180     // then we can reduce the vector load to the broadcasted scalar load.
8181     LoadSDNode *Ld = cast<LoadSDNode>(V);
8182     SDValue BaseAddr = Ld->getOperand(1);
8183     EVT AddrVT = BaseAddr.getValueType();
8184     EVT SVT = VT.getScalarType();
8185     unsigned Offset = BroadcastIdx * SVT.getStoreSize();
8186     SDValue NewAddr = DAG.getNode(
8187         ISD::ADD, DL, AddrVT, BaseAddr,
8188         DAG.getConstant(Offset, DL, AddrVT));
8189     V = DAG.getLoad(SVT, DL, Ld->getChain(), NewAddr,
8190                     DAG.getMachineFunction().getMachineMemOperand(
8191                         Ld->getMemOperand(), Offset, SVT.getStoreSize()));
8192   } else if (BroadcastIdx != 0 || !Subtarget->hasAVX2()) {
8193     // We can't broadcast from a vector register without AVX2, and we can only
8194     // broadcast from the zero-element of a vector register.
8195     return SDValue();
8196   }
8197
8198   return DAG.getNode(X86ISD::VBROADCAST, DL, VT, V);
8199 }
8200
8201 // Check for whether we can use INSERTPS to perform the shuffle. We only use
8202 // INSERTPS when the V1 elements are already in the correct locations
8203 // because otherwise we can just always use two SHUFPS instructions which
8204 // are much smaller to encode than a SHUFPS and an INSERTPS. We can also
8205 // perform INSERTPS if a single V1 element is out of place and all V2
8206 // elements are zeroable.
8207 static SDValue lowerVectorShuffleAsInsertPS(SDValue Op, SDValue V1, SDValue V2,
8208                                             ArrayRef<int> Mask,
8209                                             SelectionDAG &DAG) {
8210   assert(Op.getSimpleValueType() == MVT::v4f32 && "Bad shuffle type!");
8211   assert(V1.getSimpleValueType() == MVT::v4f32 && "Bad operand type!");
8212   assert(V2.getSimpleValueType() == MVT::v4f32 && "Bad operand type!");
8213   assert(Mask.size() == 4 && "Unexpected mask size for v4 shuffle!");
8214
8215   SmallBitVector Zeroable = computeZeroableShuffleElements(Mask, V1, V2);
8216
8217   unsigned ZMask = 0;
8218   int V1DstIndex = -1;
8219   int V2DstIndex = -1;
8220   bool V1UsedInPlace = false;
8221
8222   for (int i = 0; i < 4; ++i) {
8223     // Synthesize a zero mask from the zeroable elements (includes undefs).
8224     if (Zeroable[i]) {
8225       ZMask |= 1 << i;
8226       continue;
8227     }
8228
8229     // Flag if we use any V1 inputs in place.
8230     if (i == Mask[i]) {
8231       V1UsedInPlace = true;
8232       continue;
8233     }
8234
8235     // We can only insert a single non-zeroable element.
8236     if (V1DstIndex != -1 || V2DstIndex != -1)
8237       return SDValue();
8238
8239     if (Mask[i] < 4) {
8240       // V1 input out of place for insertion.
8241       V1DstIndex = i;
8242     } else {
8243       // V2 input for insertion.
8244       V2DstIndex = i;
8245     }
8246   }
8247
8248   // Don't bother if we have no (non-zeroable) element for insertion.
8249   if (V1DstIndex == -1 && V2DstIndex == -1)
8250     return SDValue();
8251
8252   // Determine element insertion src/dst indices. The src index is from the
8253   // start of the inserted vector, not the start of the concatenated vector.
8254   unsigned V2SrcIndex = 0;
8255   if (V1DstIndex != -1) {
8256     // If we have a V1 input out of place, we use V1 as the V2 element insertion
8257     // and don't use the original V2 at all.
8258     V2SrcIndex = Mask[V1DstIndex];
8259     V2DstIndex = V1DstIndex;
8260     V2 = V1;
8261   } else {
8262     V2SrcIndex = Mask[V2DstIndex] - 4;
8263   }
8264
8265   // If no V1 inputs are used in place, then the result is created only from
8266   // the zero mask and the V2 insertion - so remove V1 dependency.
8267   if (!V1UsedInPlace)
8268     V1 = DAG.getUNDEF(MVT::v4f32);
8269
8270   unsigned InsertPSMask = V2SrcIndex << 6 | V2DstIndex << 4 | ZMask;
8271   assert((InsertPSMask & ~0xFFu) == 0 && "Invalid mask!");
8272
8273   // Insert the V2 element into the desired position.
8274   SDLoc DL(Op);
8275   return DAG.getNode(X86ISD::INSERTPS, DL, MVT::v4f32, V1, V2,
8276                      DAG.getConstant(InsertPSMask, DL, MVT::i8));
8277 }
8278
8279 /// \brief Try to lower a shuffle as a permute of the inputs followed by an
8280 /// UNPCK instruction.
8281 ///
8282 /// This specifically targets cases where we end up with alternating between
8283 /// the two inputs, and so can permute them into something that feeds a single
8284 /// UNPCK instruction. Note that this routine only targets integer vectors
8285 /// because for floating point vectors we have a generalized SHUFPS lowering
8286 /// strategy that handles everything that doesn't *exactly* match an unpack,
8287 /// making this clever lowering unnecessary.
8288 static SDValue lowerVectorShuffleAsPermuteAndUnpack(SDLoc DL, MVT VT,
8289                                                     SDValue V1, SDValue V2,
8290                                                     ArrayRef<int> Mask,
8291                                                     SelectionDAG &DAG) {
8292   assert(!VT.isFloatingPoint() &&
8293          "This routine only supports integer vectors.");
8294   assert(!isSingleInputShuffleMask(Mask) &&
8295          "This routine should only be used when blending two inputs.");
8296   assert(Mask.size() >= 2 && "Single element masks are invalid.");
8297
8298   int Size = Mask.size();
8299
8300   int NumLoInputs = std::count_if(Mask.begin(), Mask.end(), [Size](int M) {
8301     return M >= 0 && M % Size < Size / 2;
8302   });
8303   int NumHiInputs = std::count_if(
8304       Mask.begin(), Mask.end(), [Size](int M) { return M % Size >= Size / 2; });
8305
8306   bool UnpackLo = NumLoInputs >= NumHiInputs;
8307
8308   auto TryUnpack = [&](MVT UnpackVT, int Scale) {
8309     SmallVector<int, 32> V1Mask(Mask.size(), -1);
8310     SmallVector<int, 32> V2Mask(Mask.size(), -1);
8311
8312     for (int i = 0; i < Size; ++i) {
8313       if (Mask[i] < 0)
8314         continue;
8315
8316       // Each element of the unpack contains Scale elements from this mask.
8317       int UnpackIdx = i / Scale;
8318
8319       // We only handle the case where V1 feeds the first slots of the unpack.
8320       // We rely on canonicalization to ensure this is the case.
8321       if ((UnpackIdx % 2 == 0) != (Mask[i] < Size))
8322         return SDValue();
8323
8324       // Setup the mask for this input. The indexing is tricky as we have to
8325       // handle the unpack stride.
8326       SmallVectorImpl<int> &VMask = (UnpackIdx % 2 == 0) ? V1Mask : V2Mask;
8327       VMask[(UnpackIdx / 2) * Scale + i % Scale + (UnpackLo ? 0 : Size / 2)] =
8328           Mask[i] % Size;
8329     }
8330
8331     // If we will have to shuffle both inputs to use the unpack, check whether
8332     // we can just unpack first and shuffle the result. If so, skip this unpack.
8333     if ((NumLoInputs == 0 || NumHiInputs == 0) && !isNoopShuffleMask(V1Mask) &&
8334         !isNoopShuffleMask(V2Mask))
8335       return SDValue();
8336
8337     // Shuffle the inputs into place.
8338     V1 = DAG.getVectorShuffle(VT, DL, V1, DAG.getUNDEF(VT), V1Mask);
8339     V2 = DAG.getVectorShuffle(VT, DL, V2, DAG.getUNDEF(VT), V2Mask);
8340
8341     // Cast the inputs to the type we will use to unpack them.
8342     V1 = DAG.getBitcast(UnpackVT, V1);
8343     V2 = DAG.getBitcast(UnpackVT, V2);
8344
8345     // Unpack the inputs and cast the result back to the desired type.
8346     return DAG.getBitcast(
8347         VT, DAG.getNode(UnpackLo ? X86ISD::UNPCKL : X86ISD::UNPCKH, DL,
8348                         UnpackVT, V1, V2));
8349   };
8350
8351   // We try each unpack from the largest to the smallest to try and find one
8352   // that fits this mask.
8353   int OrigNumElements = VT.getVectorNumElements();
8354   int OrigScalarSize = VT.getScalarSizeInBits();
8355   for (int ScalarSize = 64; ScalarSize >= OrigScalarSize; ScalarSize /= 2) {
8356     int Scale = ScalarSize / OrigScalarSize;
8357     int NumElements = OrigNumElements / Scale;
8358     MVT UnpackVT = MVT::getVectorVT(MVT::getIntegerVT(ScalarSize), NumElements);
8359     if (SDValue Unpack = TryUnpack(UnpackVT, Scale))
8360       return Unpack;
8361   }
8362
8363   // If none of the unpack-rooted lowerings worked (or were profitable) try an
8364   // initial unpack.
8365   if (NumLoInputs == 0 || NumHiInputs == 0) {
8366     assert((NumLoInputs > 0 || NumHiInputs > 0) &&
8367            "We have to have *some* inputs!");
8368     int HalfOffset = NumLoInputs == 0 ? Size / 2 : 0;
8369
8370     // FIXME: We could consider the total complexity of the permute of each
8371     // possible unpacking. Or at the least we should consider how many
8372     // half-crossings are created.
8373     // FIXME: We could consider commuting the unpacks.
8374
8375     SmallVector<int, 32> PermMask;
8376     PermMask.assign(Size, -1);
8377     for (int i = 0; i < Size; ++i) {
8378       if (Mask[i] < 0)
8379         continue;
8380
8381       assert(Mask[i] % Size >= HalfOffset && "Found input from wrong half!");
8382
8383       PermMask[i] =
8384           2 * ((Mask[i] % Size) - HalfOffset) + (Mask[i] < Size ? 0 : 1);
8385     }
8386     return DAG.getVectorShuffle(
8387         VT, DL, DAG.getNode(NumLoInputs == 0 ? X86ISD::UNPCKH : X86ISD::UNPCKL,
8388                             DL, VT, V1, V2),
8389         DAG.getUNDEF(VT), PermMask);
8390   }
8391
8392   return SDValue();
8393 }
8394
8395 /// \brief Handle lowering of 2-lane 64-bit floating point shuffles.
8396 ///
8397 /// This is the basis function for the 2-lane 64-bit shuffles as we have full
8398 /// support for floating point shuffles but not integer shuffles. These
8399 /// instructions will incur a domain crossing penalty on some chips though so
8400 /// it is better to avoid lowering through this for integer vectors where
8401 /// possible.
8402 static SDValue lowerV2F64VectorShuffle(SDValue Op, SDValue V1, SDValue V2,
8403                                        const X86Subtarget *Subtarget,
8404                                        SelectionDAG &DAG) {
8405   SDLoc DL(Op);
8406   assert(Op.getSimpleValueType() == MVT::v2f64 && "Bad shuffle type!");
8407   assert(V1.getSimpleValueType() == MVT::v2f64 && "Bad operand type!");
8408   assert(V2.getSimpleValueType() == MVT::v2f64 && "Bad operand type!");
8409   ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
8410   ArrayRef<int> Mask = SVOp->getMask();
8411   assert(Mask.size() == 2 && "Unexpected mask size for v2 shuffle!");
8412
8413   if (isSingleInputShuffleMask(Mask)) {
8414     // Use low duplicate instructions for masks that match their pattern.
8415     if (Subtarget->hasSSE3())
8416       if (isShuffleEquivalent(V1, V2, Mask, {0, 0}))
8417         return DAG.getNode(X86ISD::MOVDDUP, DL, MVT::v2f64, V1);
8418
8419     // Straight shuffle of a single input vector. Simulate this by using the
8420     // single input as both of the "inputs" to this instruction..
8421     unsigned SHUFPDMask = (Mask[0] == 1) | ((Mask[1] == 1) << 1);
8422
8423     if (Subtarget->hasAVX()) {
8424       // If we have AVX, we can use VPERMILPS which will allow folding a load
8425       // into the shuffle.
8426       return DAG.getNode(X86ISD::VPERMILPI, DL, MVT::v2f64, V1,
8427                          DAG.getConstant(SHUFPDMask, DL, MVT::i8));
8428     }
8429
8430     return DAG.getNode(X86ISD::SHUFP, DL, MVT::v2f64, V1, V1,
8431                        DAG.getConstant(SHUFPDMask, DL, MVT::i8));
8432   }
8433   assert(Mask[0] >= 0 && Mask[0] < 2 && "Non-canonicalized blend!");
8434   assert(Mask[1] >= 2 && "Non-canonicalized blend!");
8435
8436   // If we have a single input, insert that into V1 if we can do so cheaply.
8437   if ((Mask[0] >= 2) + (Mask[1] >= 2) == 1) {
8438     if (SDValue Insertion = lowerVectorShuffleAsElementInsertion(
8439             DL, MVT::v2f64, V1, V2, Mask, Subtarget, DAG))
8440       return Insertion;
8441     // Try inverting the insertion since for v2 masks it is easy to do and we
8442     // can't reliably sort the mask one way or the other.
8443     int InverseMask[2] = {Mask[0] < 0 ? -1 : (Mask[0] ^ 2),
8444                           Mask[1] < 0 ? -1 : (Mask[1] ^ 2)};
8445     if (SDValue Insertion = lowerVectorShuffleAsElementInsertion(
8446             DL, MVT::v2f64, V2, V1, InverseMask, Subtarget, DAG))
8447       return Insertion;
8448   }
8449
8450   // Try to use one of the special instruction patterns to handle two common
8451   // blend patterns if a zero-blend above didn't work.
8452   if (isShuffleEquivalent(V1, V2, Mask, {0, 3}) ||
8453       isShuffleEquivalent(V1, V2, Mask, {1, 3}))
8454     if (SDValue V1S = getScalarValueForVectorElement(V1, Mask[0], DAG))
8455       // We can either use a special instruction to load over the low double or
8456       // to move just the low double.
8457       return DAG.getNode(
8458           isShuffleFoldableLoad(V1S) ? X86ISD::MOVLPD : X86ISD::MOVSD,
8459           DL, MVT::v2f64, V2,
8460           DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, MVT::v2f64, V1S));
8461
8462   if (Subtarget->hasSSE41())
8463     if (SDValue Blend = lowerVectorShuffleAsBlend(DL, MVT::v2f64, V1, V2, Mask,
8464                                                   Subtarget, DAG))
8465       return Blend;
8466
8467   // Use dedicated unpack instructions for masks that match their pattern.
8468   if (SDValue V =
8469           lowerVectorShuffleWithUNPCK(DL, MVT::v2f64, Mask, V1, V2, DAG))
8470     return V;
8471
8472   unsigned SHUFPDMask = (Mask[0] == 1) | (((Mask[1] - 2) == 1) << 1);
8473   return DAG.getNode(X86ISD::SHUFP, DL, MVT::v2f64, V1, V2,
8474                      DAG.getConstant(SHUFPDMask, DL, MVT::i8));
8475 }
8476
8477 /// \brief Handle lowering of 2-lane 64-bit integer shuffles.
8478 ///
8479 /// Tries to lower a 2-lane 64-bit shuffle using shuffle operations provided by
8480 /// the integer unit to minimize domain crossing penalties. However, for blends
8481 /// it falls back to the floating point shuffle operation with appropriate bit
8482 /// casting.
8483 static SDValue lowerV2I64VectorShuffle(SDValue Op, SDValue V1, SDValue V2,
8484                                        const X86Subtarget *Subtarget,
8485                                        SelectionDAG &DAG) {
8486   SDLoc DL(Op);
8487   assert(Op.getSimpleValueType() == MVT::v2i64 && "Bad shuffle type!");
8488   assert(V1.getSimpleValueType() == MVT::v2i64 && "Bad operand type!");
8489   assert(V2.getSimpleValueType() == MVT::v2i64 && "Bad operand type!");
8490   ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
8491   ArrayRef<int> Mask = SVOp->getMask();
8492   assert(Mask.size() == 2 && "Unexpected mask size for v2 shuffle!");
8493
8494   if (isSingleInputShuffleMask(Mask)) {
8495     // Check for being able to broadcast a single element.
8496     if (SDValue Broadcast = lowerVectorShuffleAsBroadcast(DL, MVT::v2i64, V1,
8497                                                           Mask, Subtarget, DAG))
8498       return Broadcast;
8499
8500     // Straight shuffle of a single input vector. For everything from SSE2
8501     // onward this has a single fast instruction with no scary immediates.
8502     // We have to map the mask as it is actually a v4i32 shuffle instruction.
8503     V1 = DAG.getBitcast(MVT::v4i32, V1);
8504     int WidenedMask[4] = {
8505         std::max(Mask[0], 0) * 2, std::max(Mask[0], 0) * 2 + 1,
8506         std::max(Mask[1], 0) * 2, std::max(Mask[1], 0) * 2 + 1};
8507     return DAG.getBitcast(
8508         MVT::v2i64,
8509         DAG.getNode(X86ISD::PSHUFD, DL, MVT::v4i32, V1,
8510                     getV4X86ShuffleImm8ForMask(WidenedMask, DL, DAG)));
8511   }
8512   assert(Mask[0] != -1 && "No undef lanes in multi-input v2 shuffles!");
8513   assert(Mask[1] != -1 && "No undef lanes in multi-input v2 shuffles!");
8514   assert(Mask[0] < 2 && "We sort V1 to be the first input.");
8515   assert(Mask[1] >= 2 && "We sort V2 to be the second input.");
8516
8517   // If we have a blend of two PACKUS operations an the blend aligns with the
8518   // low and half halves, we can just merge the PACKUS operations. This is
8519   // particularly important as it lets us merge shuffles that this routine itself
8520   // creates.
8521   auto GetPackNode = [](SDValue V) {
8522     while (V.getOpcode() == ISD::BITCAST)
8523       V = V.getOperand(0);
8524
8525     return V.getOpcode() == X86ISD::PACKUS ? V : SDValue();
8526   };
8527   if (SDValue V1Pack = GetPackNode(V1))
8528     if (SDValue V2Pack = GetPackNode(V2))
8529       return DAG.getBitcast(MVT::v2i64,
8530                             DAG.getNode(X86ISD::PACKUS, DL, MVT::v16i8,
8531                                         Mask[0] == 0 ? V1Pack.getOperand(0)
8532                                                      : V1Pack.getOperand(1),
8533                                         Mask[1] == 2 ? V2Pack.getOperand(0)
8534                                                      : V2Pack.getOperand(1)));
8535
8536   // Try to use shift instructions.
8537   if (SDValue Shift =
8538           lowerVectorShuffleAsShift(DL, MVT::v2i64, V1, V2, Mask, DAG))
8539     return Shift;
8540
8541   // When loading a scalar and then shuffling it into a vector we can often do
8542   // the insertion cheaply.
8543   if (SDValue Insertion = lowerVectorShuffleAsElementInsertion(
8544           DL, MVT::v2i64, V1, V2, Mask, Subtarget, DAG))
8545     return Insertion;
8546   // Try inverting the insertion since for v2 masks it is easy to do and we
8547   // can't reliably sort the mask one way or the other.
8548   int InverseMask[2] = {Mask[0] ^ 2, Mask[1] ^ 2};
8549   if (SDValue Insertion = lowerVectorShuffleAsElementInsertion(
8550           DL, MVT::v2i64, V2, V1, InverseMask, Subtarget, DAG))
8551     return Insertion;
8552
8553   // We have different paths for blend lowering, but they all must use the
8554   // *exact* same predicate.
8555   bool IsBlendSupported = Subtarget->hasSSE41();
8556   if (IsBlendSupported)
8557     if (SDValue Blend = lowerVectorShuffleAsBlend(DL, MVT::v2i64, V1, V2, Mask,
8558                                                   Subtarget, DAG))
8559       return Blend;
8560
8561   // Use dedicated unpack instructions for masks that match their pattern.
8562   if (SDValue V =
8563           lowerVectorShuffleWithUNPCK(DL, MVT::v2i64, Mask, V1, V2, DAG))
8564     return V;
8565
8566   // Try to use byte rotation instructions.
8567   // Its more profitable for pre-SSSE3 to use shuffles/unpacks.
8568   if (Subtarget->hasSSSE3())
8569     if (SDValue Rotate = lowerVectorShuffleAsByteRotate(
8570             DL, MVT::v2i64, V1, V2, Mask, Subtarget, DAG))
8571       return Rotate;
8572
8573   // If we have direct support for blends, we should lower by decomposing into
8574   // a permute. That will be faster than the domain cross.
8575   if (IsBlendSupported)
8576     return lowerVectorShuffleAsDecomposedShuffleBlend(DL, MVT::v2i64, V1, V2,
8577                                                       Mask, DAG);
8578
8579   // We implement this with SHUFPD which is pretty lame because it will likely
8580   // incur 2 cycles of stall for integer vectors on Nehalem and older chips.
8581   // However, all the alternatives are still more cycles and newer chips don't
8582   // have this problem. It would be really nice if x86 had better shuffles here.
8583   V1 = DAG.getBitcast(MVT::v2f64, V1);
8584   V2 = DAG.getBitcast(MVT::v2f64, V2);
8585   return DAG.getBitcast(MVT::v2i64,
8586                         DAG.getVectorShuffle(MVT::v2f64, DL, V1, V2, Mask));
8587 }
8588
8589 /// \brief Test whether this can be lowered with a single SHUFPS instruction.
8590 ///
8591 /// This is used to disable more specialized lowerings when the shufps lowering
8592 /// will happen to be efficient.
8593 static bool isSingleSHUFPSMask(ArrayRef<int> Mask) {
8594   // This routine only handles 128-bit shufps.
8595   assert(Mask.size() == 4 && "Unsupported mask size!");
8596
8597   // To lower with a single SHUFPS we need to have the low half and high half
8598   // each requiring a single input.
8599   if (Mask[0] != -1 && Mask[1] != -1 && (Mask[0] < 4) != (Mask[1] < 4))
8600     return false;
8601   if (Mask[2] != -1 && Mask[3] != -1 && (Mask[2] < 4) != (Mask[3] < 4))
8602     return false;
8603
8604   return true;
8605 }
8606
8607 /// \brief Lower a vector shuffle using the SHUFPS instruction.
8608 ///
8609 /// This is a helper routine dedicated to lowering vector shuffles using SHUFPS.
8610 /// It makes no assumptions about whether this is the *best* lowering, it simply
8611 /// uses it.
8612 static SDValue lowerVectorShuffleWithSHUFPS(SDLoc DL, MVT VT,
8613                                             ArrayRef<int> Mask, SDValue V1,
8614                                             SDValue V2, SelectionDAG &DAG) {
8615   SDValue LowV = V1, HighV = V2;
8616   int NewMask[4] = {Mask[0], Mask[1], Mask[2], Mask[3]};
8617
8618   int NumV2Elements =
8619       std::count_if(Mask.begin(), Mask.end(), [](int M) { return M >= 4; });
8620
8621   if (NumV2Elements == 1) {
8622     int V2Index =
8623         std::find_if(Mask.begin(), Mask.end(), [](int M) { return M >= 4; }) -
8624         Mask.begin();
8625
8626     // Compute the index adjacent to V2Index and in the same half by toggling
8627     // the low bit.
8628     int V2AdjIndex = V2Index ^ 1;
8629
8630     if (Mask[V2AdjIndex] == -1) {
8631       // Handles all the cases where we have a single V2 element and an undef.
8632       // This will only ever happen in the high lanes because we commute the
8633       // vector otherwise.
8634       if (V2Index < 2)
8635         std::swap(LowV, HighV);
8636       NewMask[V2Index] -= 4;
8637     } else {
8638       // Handle the case where the V2 element ends up adjacent to a V1 element.
8639       // To make this work, blend them together as the first step.
8640       int V1Index = V2AdjIndex;
8641       int BlendMask[4] = {Mask[V2Index] - 4, 0, Mask[V1Index], 0};
8642       V2 = DAG.getNode(X86ISD::SHUFP, DL, VT, V2, V1,
8643                        getV4X86ShuffleImm8ForMask(BlendMask, DL, DAG));
8644
8645       // Now proceed to reconstruct the final blend as we have the necessary
8646       // high or low half formed.
8647       if (V2Index < 2) {
8648         LowV = V2;
8649         HighV = V1;
8650       } else {
8651         HighV = V2;
8652       }
8653       NewMask[V1Index] = 2; // We put the V1 element in V2[2].
8654       NewMask[V2Index] = 0; // We shifted the V2 element into V2[0].
8655     }
8656   } else if (NumV2Elements == 2) {
8657     if (Mask[0] < 4 && Mask[1] < 4) {
8658       // Handle the easy case where we have V1 in the low lanes and V2 in the
8659       // high lanes.
8660       NewMask[2] -= 4;
8661       NewMask[3] -= 4;
8662     } else if (Mask[2] < 4 && Mask[3] < 4) {
8663       // We also handle the reversed case because this utility may get called
8664       // when we detect a SHUFPS pattern but can't easily commute the shuffle to
8665       // arrange things in the right direction.
8666       NewMask[0] -= 4;
8667       NewMask[1] -= 4;
8668       HighV = V1;
8669       LowV = V2;
8670     } else {
8671       // We have a mixture of V1 and V2 in both low and high lanes. Rather than
8672       // trying to place elements directly, just blend them and set up the final
8673       // shuffle to place them.
8674
8675       // The first two blend mask elements are for V1, the second two are for
8676       // V2.
8677       int BlendMask[4] = {Mask[0] < 4 ? Mask[0] : Mask[1],
8678                           Mask[2] < 4 ? Mask[2] : Mask[3],
8679                           (Mask[0] >= 4 ? Mask[0] : Mask[1]) - 4,
8680                           (Mask[2] >= 4 ? Mask[2] : Mask[3]) - 4};
8681       V1 = DAG.getNode(X86ISD::SHUFP, DL, VT, V1, V2,
8682                        getV4X86ShuffleImm8ForMask(BlendMask, DL, DAG));
8683
8684       // Now we do a normal shuffle of V1 by giving V1 as both operands to
8685       // a blend.
8686       LowV = HighV = V1;
8687       NewMask[0] = Mask[0] < 4 ? 0 : 2;
8688       NewMask[1] = Mask[0] < 4 ? 2 : 0;
8689       NewMask[2] = Mask[2] < 4 ? 1 : 3;
8690       NewMask[3] = Mask[2] < 4 ? 3 : 1;
8691     }
8692   }
8693   return DAG.getNode(X86ISD::SHUFP, DL, VT, LowV, HighV,
8694                      getV4X86ShuffleImm8ForMask(NewMask, DL, DAG));
8695 }
8696
8697 /// \brief Lower 4-lane 32-bit floating point shuffles.
8698 ///
8699 /// Uses instructions exclusively from the floating point unit to minimize
8700 /// domain crossing penalties, as these are sufficient to implement all v4f32
8701 /// shuffles.
8702 static SDValue lowerV4F32VectorShuffle(SDValue Op, SDValue V1, SDValue V2,
8703                                        const X86Subtarget *Subtarget,
8704                                        SelectionDAG &DAG) {
8705   SDLoc DL(Op);
8706   assert(Op.getSimpleValueType() == MVT::v4f32 && "Bad shuffle type!");
8707   assert(V1.getSimpleValueType() == MVT::v4f32 && "Bad operand type!");
8708   assert(V2.getSimpleValueType() == MVT::v4f32 && "Bad operand type!");
8709   ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
8710   ArrayRef<int> Mask = SVOp->getMask();
8711   assert(Mask.size() == 4 && "Unexpected mask size for v4 shuffle!");
8712
8713   int NumV2Elements =
8714       std::count_if(Mask.begin(), Mask.end(), [](int M) { return M >= 4; });
8715
8716   if (NumV2Elements == 0) {
8717     // Check for being able to broadcast a single element.
8718     if (SDValue Broadcast = lowerVectorShuffleAsBroadcast(DL, MVT::v4f32, V1,
8719                                                           Mask, Subtarget, DAG))
8720       return Broadcast;
8721
8722     // Use even/odd duplicate instructions for masks that match their pattern.
8723     if (Subtarget->hasSSE3()) {
8724       if (isShuffleEquivalent(V1, V2, Mask, {0, 0, 2, 2}))
8725         return DAG.getNode(X86ISD::MOVSLDUP, DL, MVT::v4f32, V1);
8726       if (isShuffleEquivalent(V1, V2, Mask, {1, 1, 3, 3}))
8727         return DAG.getNode(X86ISD::MOVSHDUP, DL, MVT::v4f32, V1);
8728     }
8729
8730     if (Subtarget->hasAVX()) {
8731       // If we have AVX, we can use VPERMILPS which will allow folding a load
8732       // into the shuffle.
8733       return DAG.getNode(X86ISD::VPERMILPI, DL, MVT::v4f32, V1,
8734                          getV4X86ShuffleImm8ForMask(Mask, DL, DAG));
8735     }
8736
8737     // Otherwise, use a straight shuffle of a single input vector. We pass the
8738     // input vector to both operands to simulate this with a SHUFPS.
8739     return DAG.getNode(X86ISD::SHUFP, DL, MVT::v4f32, V1, V1,
8740                        getV4X86ShuffleImm8ForMask(Mask, DL, DAG));
8741   }
8742
8743   // There are special ways we can lower some single-element blends. However, we
8744   // have custom ways we can lower more complex single-element blends below that
8745   // we defer to if both this and BLENDPS fail to match, so restrict this to
8746   // when the V2 input is targeting element 0 of the mask -- that is the fast
8747   // case here.
8748   if (NumV2Elements == 1 && Mask[0] >= 4)
8749     if (SDValue V = lowerVectorShuffleAsElementInsertion(DL, MVT::v4f32, V1, V2,
8750                                                          Mask, Subtarget, DAG))
8751       return V;
8752
8753   if (Subtarget->hasSSE41()) {
8754     if (SDValue Blend = lowerVectorShuffleAsBlend(DL, MVT::v4f32, V1, V2, Mask,
8755                                                   Subtarget, DAG))
8756       return Blend;
8757
8758     // Use INSERTPS if we can complete the shuffle efficiently.
8759     if (SDValue V = lowerVectorShuffleAsInsertPS(Op, V1, V2, Mask, DAG))
8760       return V;
8761
8762     if (!isSingleSHUFPSMask(Mask))
8763       if (SDValue BlendPerm = lowerVectorShuffleAsBlendAndPermute(
8764               DL, MVT::v4f32, V1, V2, Mask, DAG))
8765         return BlendPerm;
8766   }
8767
8768   // Use dedicated unpack instructions for masks that match their pattern.
8769   if (SDValue V =
8770           lowerVectorShuffleWithUNPCK(DL, MVT::v4f32, Mask, V1, V2, DAG))
8771     return V;
8772
8773   // Otherwise fall back to a SHUFPS lowering strategy.
8774   return lowerVectorShuffleWithSHUFPS(DL, MVT::v4f32, Mask, V1, V2, DAG);
8775 }
8776
8777 /// \brief Lower 4-lane i32 vector shuffles.
8778 ///
8779 /// We try to handle these with integer-domain shuffles where we can, but for
8780 /// blends we use the floating point domain blend instructions.
8781 static SDValue lowerV4I32VectorShuffle(SDValue Op, SDValue V1, SDValue V2,
8782                                        const X86Subtarget *Subtarget,
8783                                        SelectionDAG &DAG) {
8784   SDLoc DL(Op);
8785   assert(Op.getSimpleValueType() == MVT::v4i32 && "Bad shuffle type!");
8786   assert(V1.getSimpleValueType() == MVT::v4i32 && "Bad operand type!");
8787   assert(V2.getSimpleValueType() == MVT::v4i32 && "Bad operand type!");
8788   ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
8789   ArrayRef<int> Mask = SVOp->getMask();
8790   assert(Mask.size() == 4 && "Unexpected mask size for v4 shuffle!");
8791
8792   // Whenever we can lower this as a zext, that instruction is strictly faster
8793   // than any alternative. It also allows us to fold memory operands into the
8794   // shuffle in many cases.
8795   if (SDValue ZExt = lowerVectorShuffleAsZeroOrAnyExtend(DL, MVT::v4i32, V1, V2,
8796                                                          Mask, Subtarget, DAG))
8797     return ZExt;
8798
8799   int NumV2Elements =
8800       std::count_if(Mask.begin(), Mask.end(), [](int M) { return M >= 4; });
8801
8802   if (NumV2Elements == 0) {
8803     // Check for being able to broadcast a single element.
8804     if (SDValue Broadcast = lowerVectorShuffleAsBroadcast(DL, MVT::v4i32, V1,
8805                                                           Mask, Subtarget, DAG))
8806       return Broadcast;
8807
8808     // Straight shuffle of a single input vector. For everything from SSE2
8809     // onward this has a single fast instruction with no scary immediates.
8810     // We coerce the shuffle pattern to be compatible with UNPCK instructions
8811     // but we aren't actually going to use the UNPCK instruction because doing
8812     // so prevents folding a load into this instruction or making a copy.
8813     const int UnpackLoMask[] = {0, 0, 1, 1};
8814     const int UnpackHiMask[] = {2, 2, 3, 3};
8815     if (isShuffleEquivalent(V1, V2, Mask, {0, 0, 1, 1}))
8816       Mask = UnpackLoMask;
8817     else if (isShuffleEquivalent(V1, V2, Mask, {2, 2, 3, 3}))
8818       Mask = UnpackHiMask;
8819
8820     return DAG.getNode(X86ISD::PSHUFD, DL, MVT::v4i32, V1,
8821                        getV4X86ShuffleImm8ForMask(Mask, DL, DAG));
8822   }
8823
8824   // Try to use shift instructions.
8825   if (SDValue Shift =
8826           lowerVectorShuffleAsShift(DL, MVT::v4i32, V1, V2, Mask, DAG))
8827     return Shift;
8828
8829   // There are special ways we can lower some single-element blends.
8830   if (NumV2Elements == 1)
8831     if (SDValue V = lowerVectorShuffleAsElementInsertion(DL, MVT::v4i32, V1, V2,
8832                                                          Mask, Subtarget, DAG))
8833       return V;
8834
8835   // We have different paths for blend lowering, but they all must use the
8836   // *exact* same predicate.
8837   bool IsBlendSupported = Subtarget->hasSSE41();
8838   if (IsBlendSupported)
8839     if (SDValue Blend = lowerVectorShuffleAsBlend(DL, MVT::v4i32, V1, V2, Mask,
8840                                                   Subtarget, DAG))
8841       return Blend;
8842
8843   if (SDValue Masked =
8844           lowerVectorShuffleAsBitMask(DL, MVT::v4i32, V1, V2, Mask, DAG))
8845     return Masked;
8846
8847   // Use dedicated unpack instructions for masks that match their pattern.
8848   if (SDValue V =
8849           lowerVectorShuffleWithUNPCK(DL, MVT::v4i32, Mask, V1, V2, DAG))
8850     return V;
8851
8852   // Try to use byte rotation instructions.
8853   // Its more profitable for pre-SSSE3 to use shuffles/unpacks.
8854   if (Subtarget->hasSSSE3())
8855     if (SDValue Rotate = lowerVectorShuffleAsByteRotate(
8856             DL, MVT::v4i32, V1, V2, Mask, Subtarget, DAG))
8857       return Rotate;
8858
8859   // If we have direct support for blends, we should lower by decomposing into
8860   // a permute. That will be faster than the domain cross.
8861   if (IsBlendSupported)
8862     return lowerVectorShuffleAsDecomposedShuffleBlend(DL, MVT::v4i32, V1, V2,
8863                                                       Mask, DAG);
8864
8865   // Try to lower by permuting the inputs into an unpack instruction.
8866   if (SDValue Unpack = lowerVectorShuffleAsPermuteAndUnpack(DL, MVT::v4i32, V1,
8867                                                             V2, Mask, DAG))
8868     return Unpack;
8869
8870   // We implement this with SHUFPS because it can blend from two vectors.
8871   // Because we're going to eventually use SHUFPS, we use SHUFPS even to build
8872   // up the inputs, bypassing domain shift penalties that we would encur if we
8873   // directly used PSHUFD on Nehalem and older. For newer chips, this isn't
8874   // relevant.
8875   return DAG.getBitcast(
8876       MVT::v4i32,
8877       DAG.getVectorShuffle(MVT::v4f32, DL, DAG.getBitcast(MVT::v4f32, V1),
8878                            DAG.getBitcast(MVT::v4f32, V2), Mask));
8879 }
8880
8881 /// \brief Lowering of single-input v8i16 shuffles is the cornerstone of SSE2
8882 /// shuffle lowering, and the most complex part.
8883 ///
8884 /// The lowering strategy is to try to form pairs of input lanes which are
8885 /// targeted at the same half of the final vector, and then use a dword shuffle
8886 /// to place them onto the right half, and finally unpack the paired lanes into
8887 /// their final position.
8888 ///
8889 /// The exact breakdown of how to form these dword pairs and align them on the
8890 /// correct sides is really tricky. See the comments within the function for
8891 /// more of the details.
8892 ///
8893 /// This code also handles repeated 128-bit lanes of v8i16 shuffles, but each
8894 /// lane must shuffle the *exact* same way. In fact, you must pass a v8 Mask to
8895 /// this routine for it to work correctly. To shuffle a 256-bit or 512-bit i16
8896 /// vector, form the analogous 128-bit 8-element Mask.
8897 static SDValue lowerV8I16GeneralSingleInputVectorShuffle(
8898     SDLoc DL, MVT VT, SDValue V, MutableArrayRef<int> Mask,
8899     const X86Subtarget *Subtarget, SelectionDAG &DAG) {
8900   assert(VT.getVectorElementType() == MVT::i16 && "Bad input type!");
8901   MVT PSHUFDVT = MVT::getVectorVT(MVT::i32, VT.getVectorNumElements() / 2);
8902
8903   assert(Mask.size() == 8 && "Shuffle mask length doen't match!");
8904   MutableArrayRef<int> LoMask = Mask.slice(0, 4);
8905   MutableArrayRef<int> HiMask = Mask.slice(4, 4);
8906
8907   SmallVector<int, 4> LoInputs;
8908   std::copy_if(LoMask.begin(), LoMask.end(), std::back_inserter(LoInputs),
8909                [](int M) { return M >= 0; });
8910   std::sort(LoInputs.begin(), LoInputs.end());
8911   LoInputs.erase(std::unique(LoInputs.begin(), LoInputs.end()), LoInputs.end());
8912   SmallVector<int, 4> HiInputs;
8913   std::copy_if(HiMask.begin(), HiMask.end(), std::back_inserter(HiInputs),
8914                [](int M) { return M >= 0; });
8915   std::sort(HiInputs.begin(), HiInputs.end());
8916   HiInputs.erase(std::unique(HiInputs.begin(), HiInputs.end()), HiInputs.end());
8917   int NumLToL =
8918       std::lower_bound(LoInputs.begin(), LoInputs.end(), 4) - LoInputs.begin();
8919   int NumHToL = LoInputs.size() - NumLToL;
8920   int NumLToH =
8921       std::lower_bound(HiInputs.begin(), HiInputs.end(), 4) - HiInputs.begin();
8922   int NumHToH = HiInputs.size() - NumLToH;
8923   MutableArrayRef<int> LToLInputs(LoInputs.data(), NumLToL);
8924   MutableArrayRef<int> LToHInputs(HiInputs.data(), NumLToH);
8925   MutableArrayRef<int> HToLInputs(LoInputs.data() + NumLToL, NumHToL);
8926   MutableArrayRef<int> HToHInputs(HiInputs.data() + NumLToH, NumHToH);
8927
8928   // Simplify the 1-into-3 and 3-into-1 cases with a single pshufd. For all
8929   // such inputs we can swap two of the dwords across the half mark and end up
8930   // with <=2 inputs to each half in each half. Once there, we can fall through
8931   // to the generic code below. For example:
8932   //
8933   // Input: [a, b, c, d, e, f, g, h] -PSHUFD[0,2,1,3]-> [a, b, e, f, c, d, g, h]
8934   // Mask:  [0, 1, 2, 7, 4, 5, 6, 3] -----------------> [0, 1, 4, 7, 2, 3, 6, 5]
8935   //
8936   // However in some very rare cases we have a 1-into-3 or 3-into-1 on one half
8937   // and an existing 2-into-2 on the other half. In this case we may have to
8938   // pre-shuffle the 2-into-2 half to avoid turning it into a 3-into-1 or
8939   // 1-into-3 which could cause us to cycle endlessly fixing each side in turn.
8940   // Fortunately, we don't have to handle anything but a 2-into-2 pattern
8941   // because any other situation (including a 3-into-1 or 1-into-3 in the other
8942   // half than the one we target for fixing) will be fixed when we re-enter this
8943   // path. We will also combine away any sequence of PSHUFD instructions that
8944   // result into a single instruction. Here is an example of the tricky case:
8945   //
8946   // Input: [a, b, c, d, e, f, g, h] -PSHUFD[0,2,1,3]-> [a, b, e, f, c, d, g, h]
8947   // Mask:  [3, 7, 1, 0, 2, 7, 3, 5] -THIS-IS-BAD!!!!-> [5, 7, 1, 0, 4, 7, 5, 3]
8948   //
8949   // This now has a 1-into-3 in the high half! Instead, we do two shuffles:
8950   //
8951   // Input: [a, b, c, d, e, f, g, h] PSHUFHW[0,2,1,3]-> [a, b, c, d, e, g, f, h]
8952   // Mask:  [3, 7, 1, 0, 2, 7, 3, 5] -----------------> [3, 7, 1, 0, 2, 7, 3, 6]
8953   //
8954   // Input: [a, b, c, d, e, g, f, h] -PSHUFD[0,2,1,3]-> [a, b, e, g, c, d, f, h]
8955   // Mask:  [3, 7, 1, 0, 2, 7, 3, 6] -----------------> [5, 7, 1, 0, 4, 7, 5, 6]
8956   //
8957   // The result is fine to be handled by the generic logic.
8958   auto balanceSides = [&](ArrayRef<int> AToAInputs, ArrayRef<int> BToAInputs,
8959                           ArrayRef<int> BToBInputs, ArrayRef<int> AToBInputs,
8960                           int AOffset, int BOffset) {
8961     assert((AToAInputs.size() == 3 || AToAInputs.size() == 1) &&
8962            "Must call this with A having 3 or 1 inputs from the A half.");
8963     assert((BToAInputs.size() == 1 || BToAInputs.size() == 3) &&
8964            "Must call this with B having 1 or 3 inputs from the B half.");
8965     assert(AToAInputs.size() + BToAInputs.size() == 4 &&
8966            "Must call this with either 3:1 or 1:3 inputs (summing to 4).");
8967
8968     bool ThreeAInputs = AToAInputs.size() == 3;
8969
8970     // Compute the index of dword with only one word among the three inputs in
8971     // a half by taking the sum of the half with three inputs and subtracting
8972     // the sum of the actual three inputs. The difference is the remaining
8973     // slot.
8974     int ADWord, BDWord;
8975     int &TripleDWord = ThreeAInputs ? ADWord : BDWord;
8976     int &OneInputDWord = ThreeAInputs ? BDWord : ADWord;
8977     int TripleInputOffset = ThreeAInputs ? AOffset : BOffset;
8978     ArrayRef<int> TripleInputs = ThreeAInputs ? AToAInputs : BToAInputs;
8979     int OneInput = ThreeAInputs ? BToAInputs[0] : AToAInputs[0];
8980     int TripleInputSum = 0 + 1 + 2 + 3 + (4 * TripleInputOffset);
8981     int TripleNonInputIdx =
8982         TripleInputSum - std::accumulate(TripleInputs.begin(), TripleInputs.end(), 0);
8983     TripleDWord = TripleNonInputIdx / 2;
8984
8985     // We use xor with one to compute the adjacent DWord to whichever one the
8986     // OneInput is in.
8987     OneInputDWord = (OneInput / 2) ^ 1;
8988
8989     // Check for one tricky case: We're fixing a 3<-1 or a 1<-3 shuffle for AToA
8990     // and BToA inputs. If there is also such a problem with the BToB and AToB
8991     // inputs, we don't try to fix it necessarily -- we'll recurse and see it in
8992     // the next pass. However, if we have a 2<-2 in the BToB and AToB inputs, it
8993     // is essential that we don't *create* a 3<-1 as then we might oscillate.
8994     if (BToBInputs.size() == 2 && AToBInputs.size() == 2) {
8995       // Compute how many inputs will be flipped by swapping these DWords. We
8996       // need
8997       // to balance this to ensure we don't form a 3-1 shuffle in the other
8998       // half.
8999       int NumFlippedAToBInputs =
9000           std::count(AToBInputs.begin(), AToBInputs.end(), 2 * ADWord) +
9001           std::count(AToBInputs.begin(), AToBInputs.end(), 2 * ADWord + 1);
9002       int NumFlippedBToBInputs =
9003           std::count(BToBInputs.begin(), BToBInputs.end(), 2 * BDWord) +
9004           std::count(BToBInputs.begin(), BToBInputs.end(), 2 * BDWord + 1);
9005       if ((NumFlippedAToBInputs == 1 &&
9006            (NumFlippedBToBInputs == 0 || NumFlippedBToBInputs == 2)) ||
9007           (NumFlippedBToBInputs == 1 &&
9008            (NumFlippedAToBInputs == 0 || NumFlippedAToBInputs == 2))) {
9009         // We choose whether to fix the A half or B half based on whether that
9010         // half has zero flipped inputs. At zero, we may not be able to fix it
9011         // with that half. We also bias towards fixing the B half because that
9012         // will more commonly be the high half, and we have to bias one way.
9013         auto FixFlippedInputs = [&V, &DL, &Mask, &DAG](int PinnedIdx, int DWord,
9014                                                        ArrayRef<int> Inputs) {
9015           int FixIdx = PinnedIdx ^ 1; // The adjacent slot to the pinned slot.
9016           bool IsFixIdxInput = std::find(Inputs.begin(), Inputs.end(),
9017                                          PinnedIdx ^ 1) != Inputs.end();
9018           // Determine whether the free index is in the flipped dword or the
9019           // unflipped dword based on where the pinned index is. We use this bit
9020           // in an xor to conditionally select the adjacent dword.
9021           int FixFreeIdx = 2 * (DWord ^ (PinnedIdx / 2 == DWord));
9022           bool IsFixFreeIdxInput = std::find(Inputs.begin(), Inputs.end(),
9023                                              FixFreeIdx) != Inputs.end();
9024           if (IsFixIdxInput == IsFixFreeIdxInput)
9025             FixFreeIdx += 1;
9026           IsFixFreeIdxInput = std::find(Inputs.begin(), Inputs.end(),
9027                                         FixFreeIdx) != Inputs.end();
9028           assert(IsFixIdxInput != IsFixFreeIdxInput &&
9029                  "We need to be changing the number of flipped inputs!");
9030           int PSHUFHalfMask[] = {0, 1, 2, 3};
9031           std::swap(PSHUFHalfMask[FixFreeIdx % 4], PSHUFHalfMask[FixIdx % 4]);
9032           V = DAG.getNode(FixIdx < 4 ? X86ISD::PSHUFLW : X86ISD::PSHUFHW, DL,
9033                           MVT::v8i16, V,
9034                           getV4X86ShuffleImm8ForMask(PSHUFHalfMask, DL, DAG));
9035
9036           for (int &M : Mask)
9037             if (M != -1 && M == FixIdx)
9038               M = FixFreeIdx;
9039             else if (M != -1 && M == FixFreeIdx)
9040               M = FixIdx;
9041         };
9042         if (NumFlippedBToBInputs != 0) {
9043           int BPinnedIdx =
9044               BToAInputs.size() == 3 ? TripleNonInputIdx : OneInput;
9045           FixFlippedInputs(BPinnedIdx, BDWord, BToBInputs);
9046         } else {
9047           assert(NumFlippedAToBInputs != 0 && "Impossible given predicates!");
9048           int APinnedIdx = ThreeAInputs ? TripleNonInputIdx : OneInput;
9049           FixFlippedInputs(APinnedIdx, ADWord, AToBInputs);
9050         }
9051       }
9052     }
9053
9054     int PSHUFDMask[] = {0, 1, 2, 3};
9055     PSHUFDMask[ADWord] = BDWord;
9056     PSHUFDMask[BDWord] = ADWord;
9057     V = DAG.getBitcast(
9058         VT,
9059         DAG.getNode(X86ISD::PSHUFD, DL, PSHUFDVT, DAG.getBitcast(PSHUFDVT, V),
9060                     getV4X86ShuffleImm8ForMask(PSHUFDMask, DL, DAG)));
9061
9062     // Adjust the mask to match the new locations of A and B.
9063     for (int &M : Mask)
9064       if (M != -1 && M/2 == ADWord)
9065         M = 2 * BDWord + M % 2;
9066       else if (M != -1 && M/2 == BDWord)
9067         M = 2 * ADWord + M % 2;
9068
9069     // Recurse back into this routine to re-compute state now that this isn't
9070     // a 3 and 1 problem.
9071     return lowerV8I16GeneralSingleInputVectorShuffle(DL, VT, V, Mask, Subtarget,
9072                                                      DAG);
9073   };
9074   if ((NumLToL == 3 && NumHToL == 1) || (NumLToL == 1 && NumHToL == 3))
9075     return balanceSides(LToLInputs, HToLInputs, HToHInputs, LToHInputs, 0, 4);
9076   else if ((NumHToH == 3 && NumLToH == 1) || (NumHToH == 1 && NumLToH == 3))
9077     return balanceSides(HToHInputs, LToHInputs, LToLInputs, HToLInputs, 4, 0);
9078
9079   // At this point there are at most two inputs to the low and high halves from
9080   // each half. That means the inputs can always be grouped into dwords and
9081   // those dwords can then be moved to the correct half with a dword shuffle.
9082   // We use at most one low and one high word shuffle to collect these paired
9083   // inputs into dwords, and finally a dword shuffle to place them.
9084   int PSHUFLMask[4] = {-1, -1, -1, -1};
9085   int PSHUFHMask[4] = {-1, -1, -1, -1};
9086   int PSHUFDMask[4] = {-1, -1, -1, -1};
9087
9088   // First fix the masks for all the inputs that are staying in their
9089   // original halves. This will then dictate the targets of the cross-half
9090   // shuffles.
9091   auto fixInPlaceInputs =
9092       [&PSHUFDMask](ArrayRef<int> InPlaceInputs, ArrayRef<int> IncomingInputs,
9093                     MutableArrayRef<int> SourceHalfMask,
9094                     MutableArrayRef<int> HalfMask, int HalfOffset) {
9095     if (InPlaceInputs.empty())
9096       return;
9097     if (InPlaceInputs.size() == 1) {
9098       SourceHalfMask[InPlaceInputs[0] - HalfOffset] =
9099           InPlaceInputs[0] - HalfOffset;
9100       PSHUFDMask[InPlaceInputs[0] / 2] = InPlaceInputs[0] / 2;
9101       return;
9102     }
9103     if (IncomingInputs.empty()) {
9104       // Just fix all of the in place inputs.
9105       for (int Input : InPlaceInputs) {
9106         SourceHalfMask[Input - HalfOffset] = Input - HalfOffset;
9107         PSHUFDMask[Input / 2] = Input / 2;
9108       }
9109       return;
9110     }
9111
9112     assert(InPlaceInputs.size() == 2 && "Cannot handle 3 or 4 inputs!");
9113     SourceHalfMask[InPlaceInputs[0] - HalfOffset] =
9114         InPlaceInputs[0] - HalfOffset;
9115     // Put the second input next to the first so that they are packed into
9116     // a dword. We find the adjacent index by toggling the low bit.
9117     int AdjIndex = InPlaceInputs[0] ^ 1;
9118     SourceHalfMask[AdjIndex - HalfOffset] = InPlaceInputs[1] - HalfOffset;
9119     std::replace(HalfMask.begin(), HalfMask.end(), InPlaceInputs[1], AdjIndex);
9120     PSHUFDMask[AdjIndex / 2] = AdjIndex / 2;
9121   };
9122   fixInPlaceInputs(LToLInputs, HToLInputs, PSHUFLMask, LoMask, 0);
9123   fixInPlaceInputs(HToHInputs, LToHInputs, PSHUFHMask, HiMask, 4);
9124
9125   // Now gather the cross-half inputs and place them into a free dword of
9126   // their target half.
9127   // FIXME: This operation could almost certainly be simplified dramatically to
9128   // look more like the 3-1 fixing operation.
9129   auto moveInputsToRightHalf = [&PSHUFDMask](
9130       MutableArrayRef<int> IncomingInputs, ArrayRef<int> ExistingInputs,
9131       MutableArrayRef<int> SourceHalfMask, MutableArrayRef<int> HalfMask,
9132       MutableArrayRef<int> FinalSourceHalfMask, int SourceOffset,
9133       int DestOffset) {
9134     auto isWordClobbered = [](ArrayRef<int> SourceHalfMask, int Word) {
9135       return SourceHalfMask[Word] != -1 && SourceHalfMask[Word] != Word;
9136     };
9137     auto isDWordClobbered = [&isWordClobbered](ArrayRef<int> SourceHalfMask,
9138                                                int Word) {
9139       int LowWord = Word & ~1;
9140       int HighWord = Word | 1;
9141       return isWordClobbered(SourceHalfMask, LowWord) ||
9142              isWordClobbered(SourceHalfMask, HighWord);
9143     };
9144
9145     if (IncomingInputs.empty())
9146       return;
9147
9148     if (ExistingInputs.empty()) {
9149       // Map any dwords with inputs from them into the right half.
9150       for (int Input : IncomingInputs) {
9151         // If the source half mask maps over the inputs, turn those into
9152         // swaps and use the swapped lane.
9153         if (isWordClobbered(SourceHalfMask, Input - SourceOffset)) {
9154           if (SourceHalfMask[SourceHalfMask[Input - SourceOffset]] == -1) {
9155             SourceHalfMask[SourceHalfMask[Input - SourceOffset]] =
9156                 Input - SourceOffset;
9157             // We have to swap the uses in our half mask in one sweep.
9158             for (int &M : HalfMask)
9159               if (M == SourceHalfMask[Input - SourceOffset] + SourceOffset)
9160                 M = Input;
9161               else if (M == Input)
9162                 M = SourceHalfMask[Input - SourceOffset] + SourceOffset;
9163           } else {
9164             assert(SourceHalfMask[SourceHalfMask[Input - SourceOffset]] ==
9165                        Input - SourceOffset &&
9166                    "Previous placement doesn't match!");
9167           }
9168           // Note that this correctly re-maps both when we do a swap and when
9169           // we observe the other side of the swap above. We rely on that to
9170           // avoid swapping the members of the input list directly.
9171           Input = SourceHalfMask[Input - SourceOffset] + SourceOffset;
9172         }
9173
9174         // Map the input's dword into the correct half.
9175         if (PSHUFDMask[(Input - SourceOffset + DestOffset) / 2] == -1)
9176           PSHUFDMask[(Input - SourceOffset + DestOffset) / 2] = Input / 2;
9177         else
9178           assert(PSHUFDMask[(Input - SourceOffset + DestOffset) / 2] ==
9179                      Input / 2 &&
9180                  "Previous placement doesn't match!");
9181       }
9182
9183       // And just directly shift any other-half mask elements to be same-half
9184       // as we will have mirrored the dword containing the element into the
9185       // same position within that half.
9186       for (int &M : HalfMask)
9187         if (M >= SourceOffset && M < SourceOffset + 4) {
9188           M = M - SourceOffset + DestOffset;
9189           assert(M >= 0 && "This should never wrap below zero!");
9190         }
9191       return;
9192     }
9193
9194     // Ensure we have the input in a viable dword of its current half. This
9195     // is particularly tricky because the original position may be clobbered
9196     // by inputs being moved and *staying* in that half.
9197     if (IncomingInputs.size() == 1) {
9198       if (isWordClobbered(SourceHalfMask, IncomingInputs[0] - SourceOffset)) {
9199         int InputFixed = std::find(std::begin(SourceHalfMask),
9200                                    std::end(SourceHalfMask), -1) -
9201                          std::begin(SourceHalfMask) + SourceOffset;
9202         SourceHalfMask[InputFixed - SourceOffset] =
9203             IncomingInputs[0] - SourceOffset;
9204         std::replace(HalfMask.begin(), HalfMask.end(), IncomingInputs[0],
9205                      InputFixed);
9206         IncomingInputs[0] = InputFixed;
9207       }
9208     } else if (IncomingInputs.size() == 2) {
9209       if (IncomingInputs[0] / 2 != IncomingInputs[1] / 2 ||
9210           isDWordClobbered(SourceHalfMask, IncomingInputs[0] - SourceOffset)) {
9211         // We have two non-adjacent or clobbered inputs we need to extract from
9212         // the source half. To do this, we need to map them into some adjacent
9213         // dword slot in the source mask.
9214         int InputsFixed[2] = {IncomingInputs[0] - SourceOffset,
9215                               IncomingInputs[1] - SourceOffset};
9216
9217         // If there is a free slot in the source half mask adjacent to one of
9218         // the inputs, place the other input in it. We use (Index XOR 1) to
9219         // compute an adjacent index.
9220         if (!isWordClobbered(SourceHalfMask, InputsFixed[0]) &&
9221             SourceHalfMask[InputsFixed[0] ^ 1] == -1) {
9222           SourceHalfMask[InputsFixed[0]] = InputsFixed[0];
9223           SourceHalfMask[InputsFixed[0] ^ 1] = InputsFixed[1];
9224           InputsFixed[1] = InputsFixed[0] ^ 1;
9225         } else if (!isWordClobbered(SourceHalfMask, InputsFixed[1]) &&
9226                    SourceHalfMask[InputsFixed[1] ^ 1] == -1) {
9227           SourceHalfMask[InputsFixed[1]] = InputsFixed[1];
9228           SourceHalfMask[InputsFixed[1] ^ 1] = InputsFixed[0];
9229           InputsFixed[0] = InputsFixed[1] ^ 1;
9230         } else if (SourceHalfMask[2 * ((InputsFixed[0] / 2) ^ 1)] == -1 &&
9231                    SourceHalfMask[2 * ((InputsFixed[0] / 2) ^ 1) + 1] == -1) {
9232           // The two inputs are in the same DWord but it is clobbered and the
9233           // adjacent DWord isn't used at all. Move both inputs to the free
9234           // slot.
9235           SourceHalfMask[2 * ((InputsFixed[0] / 2) ^ 1)] = InputsFixed[0];
9236           SourceHalfMask[2 * ((InputsFixed[0] / 2) ^ 1) + 1] = InputsFixed[1];
9237           InputsFixed[0] = 2 * ((InputsFixed[0] / 2) ^ 1);
9238           InputsFixed[1] = 2 * ((InputsFixed[0] / 2) ^ 1) + 1;
9239         } else {
9240           // The only way we hit this point is if there is no clobbering
9241           // (because there are no off-half inputs to this half) and there is no
9242           // free slot adjacent to one of the inputs. In this case, we have to
9243           // swap an input with a non-input.
9244           for (int i = 0; i < 4; ++i)
9245             assert((SourceHalfMask[i] == -1 || SourceHalfMask[i] == i) &&
9246                    "We can't handle any clobbers here!");
9247           assert(InputsFixed[1] != (InputsFixed[0] ^ 1) &&
9248                  "Cannot have adjacent inputs here!");
9249
9250           SourceHalfMask[InputsFixed[0] ^ 1] = InputsFixed[1];
9251           SourceHalfMask[InputsFixed[1]] = InputsFixed[0] ^ 1;
9252
9253           // We also have to update the final source mask in this case because
9254           // it may need to undo the above swap.
9255           for (int &M : FinalSourceHalfMask)
9256             if (M == (InputsFixed[0] ^ 1) + SourceOffset)
9257               M = InputsFixed[1] + SourceOffset;
9258             else if (M == InputsFixed[1] + SourceOffset)
9259               M = (InputsFixed[0] ^ 1) + SourceOffset;
9260
9261           InputsFixed[1] = InputsFixed[0] ^ 1;
9262         }
9263
9264         // Point everything at the fixed inputs.
9265         for (int &M : HalfMask)
9266           if (M == IncomingInputs[0])
9267             M = InputsFixed[0] + SourceOffset;
9268           else if (M == IncomingInputs[1])
9269             M = InputsFixed[1] + SourceOffset;
9270
9271         IncomingInputs[0] = InputsFixed[0] + SourceOffset;
9272         IncomingInputs[1] = InputsFixed[1] + SourceOffset;
9273       }
9274     } else {
9275       llvm_unreachable("Unhandled input size!");
9276     }
9277
9278     // Now hoist the DWord down to the right half.
9279     int FreeDWord = (PSHUFDMask[DestOffset / 2] == -1 ? 0 : 1) + DestOffset / 2;
9280     assert(PSHUFDMask[FreeDWord] == -1 && "DWord not free");
9281     PSHUFDMask[FreeDWord] = IncomingInputs[0] / 2;
9282     for (int &M : HalfMask)
9283       for (int Input : IncomingInputs)
9284         if (M == Input)
9285           M = FreeDWord * 2 + Input % 2;
9286   };
9287   moveInputsToRightHalf(HToLInputs, LToLInputs, PSHUFHMask, LoMask, HiMask,
9288                         /*SourceOffset*/ 4, /*DestOffset*/ 0);
9289   moveInputsToRightHalf(LToHInputs, HToHInputs, PSHUFLMask, HiMask, LoMask,
9290                         /*SourceOffset*/ 0, /*DestOffset*/ 4);
9291
9292   // Now enact all the shuffles we've computed to move the inputs into their
9293   // target half.
9294   if (!isNoopShuffleMask(PSHUFLMask))
9295     V = DAG.getNode(X86ISD::PSHUFLW, DL, VT, V,
9296                     getV4X86ShuffleImm8ForMask(PSHUFLMask, DL, DAG));
9297   if (!isNoopShuffleMask(PSHUFHMask))
9298     V = DAG.getNode(X86ISD::PSHUFHW, DL, VT, V,
9299                     getV4X86ShuffleImm8ForMask(PSHUFHMask, DL, DAG));
9300   if (!isNoopShuffleMask(PSHUFDMask))
9301     V = DAG.getBitcast(
9302         VT,
9303         DAG.getNode(X86ISD::PSHUFD, DL, PSHUFDVT, DAG.getBitcast(PSHUFDVT, V),
9304                     getV4X86ShuffleImm8ForMask(PSHUFDMask, DL, DAG)));
9305
9306   // At this point, each half should contain all its inputs, and we can then
9307   // just shuffle them into their final position.
9308   assert(std::count_if(LoMask.begin(), LoMask.end(),
9309                        [](int M) { return M >= 4; }) == 0 &&
9310          "Failed to lift all the high half inputs to the low mask!");
9311   assert(std::count_if(HiMask.begin(), HiMask.end(),
9312                        [](int M) { return M >= 0 && M < 4; }) == 0 &&
9313          "Failed to lift all the low half inputs to the high mask!");
9314
9315   // Do a half shuffle for the low mask.
9316   if (!isNoopShuffleMask(LoMask))
9317     V = DAG.getNode(X86ISD::PSHUFLW, DL, VT, V,
9318                     getV4X86ShuffleImm8ForMask(LoMask, DL, DAG));
9319
9320   // Do a half shuffle with the high mask after shifting its values down.
9321   for (int &M : HiMask)
9322     if (M >= 0)
9323       M -= 4;
9324   if (!isNoopShuffleMask(HiMask))
9325     V = DAG.getNode(X86ISD::PSHUFHW, DL, VT, V,
9326                     getV4X86ShuffleImm8ForMask(HiMask, DL, DAG));
9327
9328   return V;
9329 }
9330
9331 /// \brief Helper to form a PSHUFB-based shuffle+blend.
9332 static SDValue lowerVectorShuffleAsPSHUFB(SDLoc DL, MVT VT, SDValue V1,
9333                                           SDValue V2, ArrayRef<int> Mask,
9334                                           SelectionDAG &DAG, bool &V1InUse,
9335                                           bool &V2InUse) {
9336   SmallBitVector Zeroable = computeZeroableShuffleElements(Mask, V1, V2);
9337   SDValue V1Mask[16];
9338   SDValue V2Mask[16];
9339   V1InUse = false;
9340   V2InUse = false;
9341
9342   int Size = Mask.size();
9343   int Scale = 16 / Size;
9344   for (int i = 0; i < 16; ++i) {
9345     if (Mask[i / Scale] == -1) {
9346       V1Mask[i] = V2Mask[i] = DAG.getUNDEF(MVT::i8);
9347     } else {
9348       const int ZeroMask = 0x80;
9349       int V1Idx = Mask[i / Scale] < Size ? Mask[i / Scale] * Scale + i % Scale
9350                                           : ZeroMask;
9351       int V2Idx = Mask[i / Scale] < Size
9352                       ? ZeroMask
9353                       : (Mask[i / Scale] - Size) * Scale + i % Scale;
9354       if (Zeroable[i / Scale])
9355         V1Idx = V2Idx = ZeroMask;
9356       V1Mask[i] = DAG.getConstant(V1Idx, DL, MVT::i8);
9357       V2Mask[i] = DAG.getConstant(V2Idx, DL, MVT::i8);
9358       V1InUse |= (ZeroMask != V1Idx);
9359       V2InUse |= (ZeroMask != V2Idx);
9360     }
9361   }
9362
9363   if (V1InUse)
9364     V1 = DAG.getNode(X86ISD::PSHUFB, DL, MVT::v16i8,
9365                      DAG.getBitcast(MVT::v16i8, V1),
9366                      DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v16i8, V1Mask));
9367   if (V2InUse)
9368     V2 = DAG.getNode(X86ISD::PSHUFB, DL, MVT::v16i8,
9369                      DAG.getBitcast(MVT::v16i8, V2),
9370                      DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v16i8, V2Mask));
9371
9372   // If we need shuffled inputs from both, blend the two.
9373   SDValue V;
9374   if (V1InUse && V2InUse)
9375     V = DAG.getNode(ISD::OR, DL, MVT::v16i8, V1, V2);
9376   else
9377     V = V1InUse ? V1 : V2;
9378
9379   // Cast the result back to the correct type.
9380   return DAG.getBitcast(VT, V);
9381 }
9382
9383 /// \brief Generic lowering of 8-lane i16 shuffles.
9384 ///
9385 /// This handles both single-input shuffles and combined shuffle/blends with
9386 /// two inputs. The single input shuffles are immediately delegated to
9387 /// a dedicated lowering routine.
9388 ///
9389 /// The blends are lowered in one of three fundamental ways. If there are few
9390 /// enough inputs, it delegates to a basic UNPCK-based strategy. If the shuffle
9391 /// of the input is significantly cheaper when lowered as an interleaving of
9392 /// the two inputs, try to interleave them. Otherwise, blend the low and high
9393 /// halves of the inputs separately (making them have relatively few inputs)
9394 /// and then concatenate them.
9395 static SDValue lowerV8I16VectorShuffle(SDValue Op, SDValue V1, SDValue V2,
9396                                        const X86Subtarget *Subtarget,
9397                                        SelectionDAG &DAG) {
9398   SDLoc DL(Op);
9399   assert(Op.getSimpleValueType() == MVT::v8i16 && "Bad shuffle type!");
9400   assert(V1.getSimpleValueType() == MVT::v8i16 && "Bad operand type!");
9401   assert(V2.getSimpleValueType() == MVT::v8i16 && "Bad operand type!");
9402   ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
9403   ArrayRef<int> OrigMask = SVOp->getMask();
9404   int MaskStorage[8] = {OrigMask[0], OrigMask[1], OrigMask[2], OrigMask[3],
9405                         OrigMask[4], OrigMask[5], OrigMask[6], OrigMask[7]};
9406   MutableArrayRef<int> Mask(MaskStorage);
9407
9408   assert(Mask.size() == 8 && "Unexpected mask size for v8 shuffle!");
9409
9410   // Whenever we can lower this as a zext, that instruction is strictly faster
9411   // than any alternative.
9412   if (SDValue ZExt = lowerVectorShuffleAsZeroOrAnyExtend(
9413           DL, MVT::v8i16, V1, V2, OrigMask, Subtarget, DAG))
9414     return ZExt;
9415
9416   auto isV1 = [](int M) { return M >= 0 && M < 8; };
9417   (void)isV1;
9418   auto isV2 = [](int M) { return M >= 8; };
9419
9420   int NumV2Inputs = std::count_if(Mask.begin(), Mask.end(), isV2);
9421
9422   if (NumV2Inputs == 0) {
9423     // Check for being able to broadcast a single element.
9424     if (SDValue Broadcast = lowerVectorShuffleAsBroadcast(DL, MVT::v8i16, V1,
9425                                                           Mask, Subtarget, DAG))
9426       return Broadcast;
9427
9428     // Try to use shift instructions.
9429     if (SDValue Shift =
9430             lowerVectorShuffleAsShift(DL, MVT::v8i16, V1, V1, Mask, DAG))
9431       return Shift;
9432
9433     // Use dedicated unpack instructions for masks that match their pattern.
9434     if (SDValue V =
9435             lowerVectorShuffleWithUNPCK(DL, MVT::v8i16, Mask, V1, V2, DAG))
9436       return V;
9437
9438     // Try to use byte rotation instructions.
9439     if (SDValue Rotate = lowerVectorShuffleAsByteRotate(DL, MVT::v8i16, V1, V1,
9440                                                         Mask, Subtarget, DAG))
9441       return Rotate;
9442
9443     return lowerV8I16GeneralSingleInputVectorShuffle(DL, MVT::v8i16, V1, Mask,
9444                                                      Subtarget, DAG);
9445   }
9446
9447   assert(std::any_of(Mask.begin(), Mask.end(), isV1) &&
9448          "All single-input shuffles should be canonicalized to be V1-input "
9449          "shuffles.");
9450
9451   // Try to use shift instructions.
9452   if (SDValue Shift =
9453           lowerVectorShuffleAsShift(DL, MVT::v8i16, V1, V2, Mask, DAG))
9454     return Shift;
9455
9456   // See if we can use SSE4A Extraction / Insertion.
9457   if (Subtarget->hasSSE4A())
9458     if (SDValue V = lowerVectorShuffleWithSSE4A(DL, MVT::v8i16, V1, V2, Mask, DAG))
9459       return V;
9460
9461   // There are special ways we can lower some single-element blends.
9462   if (NumV2Inputs == 1)
9463     if (SDValue V = lowerVectorShuffleAsElementInsertion(DL, MVT::v8i16, V1, V2,
9464                                                          Mask, Subtarget, DAG))
9465       return V;
9466
9467   // We have different paths for blend lowering, but they all must use the
9468   // *exact* same predicate.
9469   bool IsBlendSupported = Subtarget->hasSSE41();
9470   if (IsBlendSupported)
9471     if (SDValue Blend = lowerVectorShuffleAsBlend(DL, MVT::v8i16, V1, V2, Mask,
9472                                                   Subtarget, DAG))
9473       return Blend;
9474
9475   if (SDValue Masked =
9476           lowerVectorShuffleAsBitMask(DL, MVT::v8i16, V1, V2, Mask, DAG))
9477     return Masked;
9478
9479   // Use dedicated unpack instructions for masks that match their pattern.
9480   if (SDValue V =
9481           lowerVectorShuffleWithUNPCK(DL, MVT::v8i16, Mask, V1, V2, DAG))
9482     return V;
9483
9484   // Try to use byte rotation instructions.
9485   if (SDValue Rotate = lowerVectorShuffleAsByteRotate(
9486           DL, MVT::v8i16, V1, V2, Mask, Subtarget, DAG))
9487     return Rotate;
9488
9489   if (SDValue BitBlend =
9490           lowerVectorShuffleAsBitBlend(DL, MVT::v8i16, V1, V2, Mask, DAG))
9491     return BitBlend;
9492
9493   if (SDValue Unpack = lowerVectorShuffleAsPermuteAndUnpack(DL, MVT::v8i16, V1,
9494                                                             V2, Mask, DAG))
9495     return Unpack;
9496
9497   // If we can't directly blend but can use PSHUFB, that will be better as it
9498   // can both shuffle and set up the inefficient blend.
9499   if (!IsBlendSupported && Subtarget->hasSSSE3()) {
9500     bool V1InUse, V2InUse;
9501     return lowerVectorShuffleAsPSHUFB(DL, MVT::v8i16, V1, V2, Mask, DAG,
9502                                       V1InUse, V2InUse);
9503   }
9504
9505   // We can always bit-blend if we have to so the fallback strategy is to
9506   // decompose into single-input permutes and blends.
9507   return lowerVectorShuffleAsDecomposedShuffleBlend(DL, MVT::v8i16, V1, V2,
9508                                                       Mask, DAG);
9509 }
9510
9511 /// \brief Check whether a compaction lowering can be done by dropping even
9512 /// elements and compute how many times even elements must be dropped.
9513 ///
9514 /// This handles shuffles which take every Nth element where N is a power of
9515 /// two. Example shuffle masks:
9516 ///
9517 ///  N = 1:  0,  2,  4,  6,  8, 10, 12, 14,  0,  2,  4,  6,  8, 10, 12, 14
9518 ///  N = 1:  0,  2,  4,  6,  8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30
9519 ///  N = 2:  0,  4,  8, 12,  0,  4,  8, 12,  0,  4,  8, 12,  0,  4,  8, 12
9520 ///  N = 2:  0,  4,  8, 12, 16, 20, 24, 28,  0,  4,  8, 12, 16, 20, 24, 28
9521 ///  N = 3:  0,  8,  0,  8,  0,  8,  0,  8,  0,  8,  0,  8,  0,  8,  0,  8
9522 ///  N = 3:  0,  8, 16, 24,  0,  8, 16, 24,  0,  8, 16, 24,  0,  8, 16, 24
9523 ///
9524 /// Any of these lanes can of course be undef.
9525 ///
9526 /// This routine only supports N <= 3.
9527 /// FIXME: Evaluate whether either AVX or AVX-512 have any opportunities here
9528 /// for larger N.
9529 ///
9530 /// \returns N above, or the number of times even elements must be dropped if
9531 /// there is such a number. Otherwise returns zero.
9532 static int canLowerByDroppingEvenElements(ArrayRef<int> Mask) {
9533   // Figure out whether we're looping over two inputs or just one.
9534   bool IsSingleInput = isSingleInputShuffleMask(Mask);
9535
9536   // The modulus for the shuffle vector entries is based on whether this is
9537   // a single input or not.
9538   int ShuffleModulus = Mask.size() * (IsSingleInput ? 1 : 2);
9539   assert(isPowerOf2_32((uint32_t)ShuffleModulus) &&
9540          "We should only be called with masks with a power-of-2 size!");
9541
9542   uint64_t ModMask = (uint64_t)ShuffleModulus - 1;
9543
9544   // We track whether the input is viable for all power-of-2 strides 2^1, 2^2,
9545   // and 2^3 simultaneously. This is because we may have ambiguity with
9546   // partially undef inputs.
9547   bool ViableForN[3] = {true, true, true};
9548
9549   for (int i = 0, e = Mask.size(); i < e; ++i) {
9550     // Ignore undef lanes, we'll optimistically collapse them to the pattern we
9551     // want.
9552     if (Mask[i] == -1)
9553       continue;
9554
9555     bool IsAnyViable = false;
9556     for (unsigned j = 0; j != array_lengthof(ViableForN); ++j)
9557       if (ViableForN[j]) {
9558         uint64_t N = j + 1;
9559
9560         // The shuffle mask must be equal to (i * 2^N) % M.
9561         if ((uint64_t)Mask[i] == (((uint64_t)i << N) & ModMask))
9562           IsAnyViable = true;
9563         else
9564           ViableForN[j] = false;
9565       }
9566     // Early exit if we exhaust the possible powers of two.
9567     if (!IsAnyViable)
9568       break;
9569   }
9570
9571   for (unsigned j = 0; j != array_lengthof(ViableForN); ++j)
9572     if (ViableForN[j])
9573       return j + 1;
9574
9575   // Return 0 as there is no viable power of two.
9576   return 0;
9577 }
9578
9579 /// \brief Generic lowering of v16i8 shuffles.
9580 ///
9581 /// This is a hybrid strategy to lower v16i8 vectors. It first attempts to
9582 /// detect any complexity reducing interleaving. If that doesn't help, it uses
9583 /// UNPCK to spread the i8 elements across two i16-element vectors, and uses
9584 /// the existing lowering for v8i16 blends on each half, finally PACK-ing them
9585 /// back together.
9586 static SDValue lowerV16I8VectorShuffle(SDValue Op, SDValue V1, SDValue V2,
9587                                        const X86Subtarget *Subtarget,
9588                                        SelectionDAG &DAG) {
9589   SDLoc DL(Op);
9590   assert(Op.getSimpleValueType() == MVT::v16i8 && "Bad shuffle type!");
9591   assert(V1.getSimpleValueType() == MVT::v16i8 && "Bad operand type!");
9592   assert(V2.getSimpleValueType() == MVT::v16i8 && "Bad operand type!");
9593   ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
9594   ArrayRef<int> Mask = SVOp->getMask();
9595   assert(Mask.size() == 16 && "Unexpected mask size for v16 shuffle!");
9596
9597   // Try to use shift instructions.
9598   if (SDValue Shift =
9599           lowerVectorShuffleAsShift(DL, MVT::v16i8, V1, V2, Mask, DAG))
9600     return Shift;
9601
9602   // Try to use byte rotation instructions.
9603   if (SDValue Rotate = lowerVectorShuffleAsByteRotate(
9604           DL, MVT::v16i8, V1, V2, Mask, Subtarget, DAG))
9605     return Rotate;
9606
9607   // Try to use a zext lowering.
9608   if (SDValue ZExt = lowerVectorShuffleAsZeroOrAnyExtend(
9609           DL, MVT::v16i8, V1, V2, Mask, Subtarget, DAG))
9610     return ZExt;
9611
9612   // See if we can use SSE4A Extraction / Insertion.
9613   if (Subtarget->hasSSE4A())
9614     if (SDValue V = lowerVectorShuffleWithSSE4A(DL, MVT::v16i8, V1, V2, Mask, DAG))
9615       return V;
9616
9617   int NumV2Elements =
9618       std::count_if(Mask.begin(), Mask.end(), [](int M) { return M >= 16; });
9619
9620   // For single-input shuffles, there are some nicer lowering tricks we can use.
9621   if (NumV2Elements == 0) {
9622     // Check for being able to broadcast a single element.
9623     if (SDValue Broadcast = lowerVectorShuffleAsBroadcast(DL, MVT::v16i8, V1,
9624                                                           Mask, Subtarget, DAG))
9625       return Broadcast;
9626
9627     // Check whether we can widen this to an i16 shuffle by duplicating bytes.
9628     // Notably, this handles splat and partial-splat shuffles more efficiently.
9629     // However, it only makes sense if the pre-duplication shuffle simplifies
9630     // things significantly. Currently, this means we need to be able to
9631     // express the pre-duplication shuffle as an i16 shuffle.
9632     //
9633     // FIXME: We should check for other patterns which can be widened into an
9634     // i16 shuffle as well.
9635     auto canWidenViaDuplication = [](ArrayRef<int> Mask) {
9636       for (int i = 0; i < 16; i += 2)
9637         if (Mask[i] != -1 && Mask[i + 1] != -1 && Mask[i] != Mask[i + 1])
9638           return false;
9639
9640       return true;
9641     };
9642     auto tryToWidenViaDuplication = [&]() -> SDValue {
9643       if (!canWidenViaDuplication(Mask))
9644         return SDValue();
9645       SmallVector<int, 4> LoInputs;
9646       std::copy_if(Mask.begin(), Mask.end(), std::back_inserter(LoInputs),
9647                    [](int M) { return M >= 0 && M < 8; });
9648       std::sort(LoInputs.begin(), LoInputs.end());
9649       LoInputs.erase(std::unique(LoInputs.begin(), LoInputs.end()),
9650                      LoInputs.end());
9651       SmallVector<int, 4> HiInputs;
9652       std::copy_if(Mask.begin(), Mask.end(), std::back_inserter(HiInputs),
9653                    [](int M) { return M >= 8; });
9654       std::sort(HiInputs.begin(), HiInputs.end());
9655       HiInputs.erase(std::unique(HiInputs.begin(), HiInputs.end()),
9656                      HiInputs.end());
9657
9658       bool TargetLo = LoInputs.size() >= HiInputs.size();
9659       ArrayRef<int> InPlaceInputs = TargetLo ? LoInputs : HiInputs;
9660       ArrayRef<int> MovingInputs = TargetLo ? HiInputs : LoInputs;
9661
9662       int PreDupI16Shuffle[] = {-1, -1, -1, -1, -1, -1, -1, -1};
9663       SmallDenseMap<int, int, 8> LaneMap;
9664       for (int I : InPlaceInputs) {
9665         PreDupI16Shuffle[I/2] = I/2;
9666         LaneMap[I] = I;
9667       }
9668       int j = TargetLo ? 0 : 4, je = j + 4;
9669       for (int i = 0, ie = MovingInputs.size(); i < ie; ++i) {
9670         // Check if j is already a shuffle of this input. This happens when
9671         // there are two adjacent bytes after we move the low one.
9672         if (PreDupI16Shuffle[j] != MovingInputs[i] / 2) {
9673           // If we haven't yet mapped the input, search for a slot into which
9674           // we can map it.
9675           while (j < je && PreDupI16Shuffle[j] != -1)
9676             ++j;
9677
9678           if (j == je)
9679             // We can't place the inputs into a single half with a simple i16 shuffle, so bail.
9680             return SDValue();
9681
9682           // Map this input with the i16 shuffle.
9683           PreDupI16Shuffle[j] = MovingInputs[i] / 2;
9684         }
9685
9686         // Update the lane map based on the mapping we ended up with.
9687         LaneMap[MovingInputs[i]] = 2 * j + MovingInputs[i] % 2;
9688       }
9689       V1 = DAG.getBitcast(
9690           MVT::v16i8,
9691           DAG.getVectorShuffle(MVT::v8i16, DL, DAG.getBitcast(MVT::v8i16, V1),
9692                                DAG.getUNDEF(MVT::v8i16), PreDupI16Shuffle));
9693
9694       // Unpack the bytes to form the i16s that will be shuffled into place.
9695       V1 = DAG.getNode(TargetLo ? X86ISD::UNPCKL : X86ISD::UNPCKH, DL,
9696                        MVT::v16i8, V1, V1);
9697
9698       int PostDupI16Shuffle[8] = {-1, -1, -1, -1, -1, -1, -1, -1};
9699       for (int i = 0; i < 16; ++i)
9700         if (Mask[i] != -1) {
9701           int MappedMask = LaneMap[Mask[i]] - (TargetLo ? 0 : 8);
9702           assert(MappedMask < 8 && "Invalid v8 shuffle mask!");
9703           if (PostDupI16Shuffle[i / 2] == -1)
9704             PostDupI16Shuffle[i / 2] = MappedMask;
9705           else
9706             assert(PostDupI16Shuffle[i / 2] == MappedMask &&
9707                    "Conflicting entrties in the original shuffle!");
9708         }
9709       return DAG.getBitcast(
9710           MVT::v16i8,
9711           DAG.getVectorShuffle(MVT::v8i16, DL, DAG.getBitcast(MVT::v8i16, V1),
9712                                DAG.getUNDEF(MVT::v8i16), PostDupI16Shuffle));
9713     };
9714     if (SDValue V = tryToWidenViaDuplication())
9715       return V;
9716   }
9717
9718   if (SDValue Masked =
9719           lowerVectorShuffleAsBitMask(DL, MVT::v16i8, V1, V2, Mask, DAG))
9720     return Masked;
9721
9722   // Use dedicated unpack instructions for masks that match their pattern.
9723   if (SDValue V =
9724           lowerVectorShuffleWithUNPCK(DL, MVT::v16i8, Mask, V1, V2, DAG))
9725     return V;
9726
9727   // Check for SSSE3 which lets us lower all v16i8 shuffles much more directly
9728   // with PSHUFB. It is important to do this before we attempt to generate any
9729   // blends but after all of the single-input lowerings. If the single input
9730   // lowerings can find an instruction sequence that is faster than a PSHUFB, we
9731   // want to preserve that and we can DAG combine any longer sequences into
9732   // a PSHUFB in the end. But once we start blending from multiple inputs,
9733   // the complexity of DAG combining bad patterns back into PSHUFB is too high,
9734   // and there are *very* few patterns that would actually be faster than the
9735   // PSHUFB approach because of its ability to zero lanes.
9736   //
9737   // FIXME: The only exceptions to the above are blends which are exact
9738   // interleavings with direct instructions supporting them. We currently don't
9739   // handle those well here.
9740   if (Subtarget->hasSSSE3()) {
9741     bool V1InUse = false;
9742     bool V2InUse = false;
9743
9744     SDValue PSHUFB = lowerVectorShuffleAsPSHUFB(DL, MVT::v16i8, V1, V2, Mask,
9745                                                 DAG, V1InUse, V2InUse);
9746
9747     // If both V1 and V2 are in use and we can use a direct blend or an unpack,
9748     // do so. This avoids using them to handle blends-with-zero which is
9749     // important as a single pshufb is significantly faster for that.
9750     if (V1InUse && V2InUse) {
9751       if (Subtarget->hasSSE41())
9752         if (SDValue Blend = lowerVectorShuffleAsBlend(DL, MVT::v16i8, V1, V2,
9753                                                       Mask, Subtarget, DAG))
9754           return Blend;
9755
9756       // We can use an unpack to do the blending rather than an or in some
9757       // cases. Even though the or may be (very minorly) more efficient, we
9758       // preference this lowering because there are common cases where part of
9759       // the complexity of the shuffles goes away when we do the final blend as
9760       // an unpack.
9761       // FIXME: It might be worth trying to detect if the unpack-feeding
9762       // shuffles will both be pshufb, in which case we shouldn't bother with
9763       // this.
9764       if (SDValue Unpack = lowerVectorShuffleAsPermuteAndUnpack(
9765               DL, MVT::v16i8, V1, V2, Mask, DAG))
9766         return Unpack;
9767     }
9768
9769     return PSHUFB;
9770   }
9771
9772   // There are special ways we can lower some single-element blends.
9773   if (NumV2Elements == 1)
9774     if (SDValue V = lowerVectorShuffleAsElementInsertion(DL, MVT::v16i8, V1, V2,
9775                                                          Mask, Subtarget, DAG))
9776       return V;
9777
9778   if (SDValue BitBlend =
9779           lowerVectorShuffleAsBitBlend(DL, MVT::v16i8, V1, V2, Mask, DAG))
9780     return BitBlend;
9781
9782   // Check whether a compaction lowering can be done. This handles shuffles
9783   // which take every Nth element for some even N. See the helper function for
9784   // details.
9785   //
9786   // We special case these as they can be particularly efficiently handled with
9787   // the PACKUSB instruction on x86 and they show up in common patterns of
9788   // rearranging bytes to truncate wide elements.
9789   if (int NumEvenDrops = canLowerByDroppingEvenElements(Mask)) {
9790     // NumEvenDrops is the power of two stride of the elements. Another way of
9791     // thinking about it is that we need to drop the even elements this many
9792     // times to get the original input.
9793     bool IsSingleInput = isSingleInputShuffleMask(Mask);
9794
9795     // First we need to zero all the dropped bytes.
9796     assert(NumEvenDrops <= 3 &&
9797            "No support for dropping even elements more than 3 times.");
9798     // We use the mask type to pick which bytes are preserved based on how many
9799     // elements are dropped.
9800     MVT MaskVTs[] = { MVT::v8i16, MVT::v4i32, MVT::v2i64 };
9801     SDValue ByteClearMask = DAG.getBitcast(
9802         MVT::v16i8, DAG.getConstant(0xFF, DL, MaskVTs[NumEvenDrops - 1]));
9803     V1 = DAG.getNode(ISD::AND, DL, MVT::v16i8, V1, ByteClearMask);
9804     if (!IsSingleInput)
9805       V2 = DAG.getNode(ISD::AND, DL, MVT::v16i8, V2, ByteClearMask);
9806
9807     // Now pack things back together.
9808     V1 = DAG.getBitcast(MVT::v8i16, V1);
9809     V2 = IsSingleInput ? V1 : DAG.getBitcast(MVT::v8i16, V2);
9810     SDValue Result = DAG.getNode(X86ISD::PACKUS, DL, MVT::v16i8, V1, V2);
9811     for (int i = 1; i < NumEvenDrops; ++i) {
9812       Result = DAG.getBitcast(MVT::v8i16, Result);
9813       Result = DAG.getNode(X86ISD::PACKUS, DL, MVT::v16i8, Result, Result);
9814     }
9815
9816     return Result;
9817   }
9818
9819   // Handle multi-input cases by blending single-input shuffles.
9820   if (NumV2Elements > 0)
9821     return lowerVectorShuffleAsDecomposedShuffleBlend(DL, MVT::v16i8, V1, V2,
9822                                                       Mask, DAG);
9823
9824   // The fallback path for single-input shuffles widens this into two v8i16
9825   // vectors with unpacks, shuffles those, and then pulls them back together
9826   // with a pack.
9827   SDValue V = V1;
9828
9829   int LoBlendMask[8] = {-1, -1, -1, -1, -1, -1, -1, -1};
9830   int HiBlendMask[8] = {-1, -1, -1, -1, -1, -1, -1, -1};
9831   for (int i = 0; i < 16; ++i)
9832     if (Mask[i] >= 0)
9833       (i < 8 ? LoBlendMask[i] : HiBlendMask[i % 8]) = Mask[i];
9834
9835   SDValue Zero = getZeroVector(MVT::v8i16, Subtarget, DAG, DL);
9836
9837   SDValue VLoHalf, VHiHalf;
9838   // Check if any of the odd lanes in the v16i8 are used. If not, we can mask
9839   // them out and avoid using UNPCK{L,H} to extract the elements of V as
9840   // i16s.
9841   if (std::none_of(std::begin(LoBlendMask), std::end(LoBlendMask),
9842                    [](int M) { return M >= 0 && M % 2 == 1; }) &&
9843       std::none_of(std::begin(HiBlendMask), std::end(HiBlendMask),
9844                    [](int M) { return M >= 0 && M % 2 == 1; })) {
9845     // Use a mask to drop the high bytes.
9846     VLoHalf = DAG.getBitcast(MVT::v8i16, V);
9847     VLoHalf = DAG.getNode(ISD::AND, DL, MVT::v8i16, VLoHalf,
9848                      DAG.getConstant(0x00FF, DL, MVT::v8i16));
9849
9850     // This will be a single vector shuffle instead of a blend so nuke VHiHalf.
9851     VHiHalf = DAG.getUNDEF(MVT::v8i16);
9852
9853     // Squash the masks to point directly into VLoHalf.
9854     for (int &M : LoBlendMask)
9855       if (M >= 0)
9856         M /= 2;
9857     for (int &M : HiBlendMask)
9858       if (M >= 0)
9859         M /= 2;
9860   } else {
9861     // Otherwise just unpack the low half of V into VLoHalf and the high half into
9862     // VHiHalf so that we can blend them as i16s.
9863     VLoHalf = DAG.getBitcast(
9864         MVT::v8i16, DAG.getNode(X86ISD::UNPCKL, DL, MVT::v16i8, V, Zero));
9865     VHiHalf = DAG.getBitcast(
9866         MVT::v8i16, DAG.getNode(X86ISD::UNPCKH, DL, MVT::v16i8, V, Zero));
9867   }
9868
9869   SDValue LoV = DAG.getVectorShuffle(MVT::v8i16, DL, VLoHalf, VHiHalf, LoBlendMask);
9870   SDValue HiV = DAG.getVectorShuffle(MVT::v8i16, DL, VLoHalf, VHiHalf, HiBlendMask);
9871
9872   return DAG.getNode(X86ISD::PACKUS, DL, MVT::v16i8, LoV, HiV);
9873 }
9874
9875 /// \brief Dispatching routine to lower various 128-bit x86 vector shuffles.
9876 ///
9877 /// This routine breaks down the specific type of 128-bit shuffle and
9878 /// dispatches to the lowering routines accordingly.
9879 static SDValue lower128BitVectorShuffle(SDValue Op, SDValue V1, SDValue V2,
9880                                         MVT VT, const X86Subtarget *Subtarget,
9881                                         SelectionDAG &DAG) {
9882   switch (VT.SimpleTy) {
9883   case MVT::v2i64:
9884     return lowerV2I64VectorShuffle(Op, V1, V2, Subtarget, DAG);
9885   case MVT::v2f64:
9886     return lowerV2F64VectorShuffle(Op, V1, V2, Subtarget, DAG);
9887   case MVT::v4i32:
9888     return lowerV4I32VectorShuffle(Op, V1, V2, Subtarget, DAG);
9889   case MVT::v4f32:
9890     return lowerV4F32VectorShuffle(Op, V1, V2, Subtarget, DAG);
9891   case MVT::v8i16:
9892     return lowerV8I16VectorShuffle(Op, V1, V2, Subtarget, DAG);
9893   case MVT::v16i8:
9894     return lowerV16I8VectorShuffle(Op, V1, V2, Subtarget, DAG);
9895
9896   default:
9897     llvm_unreachable("Unimplemented!");
9898   }
9899 }
9900
9901 /// \brief Helper function to test whether a shuffle mask could be
9902 /// simplified by widening the elements being shuffled.
9903 ///
9904 /// Appends the mask for wider elements in WidenedMask if valid. Otherwise
9905 /// leaves it in an unspecified state.
9906 ///
9907 /// NOTE: This must handle normal vector shuffle masks and *target* vector
9908 /// shuffle masks. The latter have the special property of a '-2' representing
9909 /// a zero-ed lane of a vector.
9910 static bool canWidenShuffleElements(ArrayRef<int> Mask,
9911                                     SmallVectorImpl<int> &WidenedMask) {
9912   for (int i = 0, Size = Mask.size(); i < Size; i += 2) {
9913     // If both elements are undef, its trivial.
9914     if (Mask[i] == SM_SentinelUndef && Mask[i + 1] == SM_SentinelUndef) {
9915       WidenedMask.push_back(SM_SentinelUndef);
9916       continue;
9917     }
9918
9919     // Check for an undef mask and a mask value properly aligned to fit with
9920     // a pair of values. If we find such a case, use the non-undef mask's value.
9921     if (Mask[i] == SM_SentinelUndef && Mask[i + 1] >= 0 && Mask[i + 1] % 2 == 1) {
9922       WidenedMask.push_back(Mask[i + 1] / 2);
9923       continue;
9924     }
9925     if (Mask[i + 1] == SM_SentinelUndef && Mask[i] >= 0 && Mask[i] % 2 == 0) {
9926       WidenedMask.push_back(Mask[i] / 2);
9927       continue;
9928     }
9929
9930     // When zeroing, we need to spread the zeroing across both lanes to widen.
9931     if (Mask[i] == SM_SentinelZero || Mask[i + 1] == SM_SentinelZero) {
9932       if ((Mask[i] == SM_SentinelZero || Mask[i] == SM_SentinelUndef) &&
9933           (Mask[i + 1] == SM_SentinelZero || Mask[i + 1] == SM_SentinelUndef)) {
9934         WidenedMask.push_back(SM_SentinelZero);
9935         continue;
9936       }
9937       return false;
9938     }
9939
9940     // Finally check if the two mask values are adjacent and aligned with
9941     // a pair.
9942     if (Mask[i] != SM_SentinelUndef && Mask[i] % 2 == 0 && Mask[i] + 1 == Mask[i + 1]) {
9943       WidenedMask.push_back(Mask[i] / 2);
9944       continue;
9945     }
9946
9947     // Otherwise we can't safely widen the elements used in this shuffle.
9948     return false;
9949   }
9950   assert(WidenedMask.size() == Mask.size() / 2 &&
9951          "Incorrect size of mask after widening the elements!");
9952
9953   return true;
9954 }
9955
9956 /// \brief Generic routine to split vector shuffle into half-sized shuffles.
9957 ///
9958 /// This routine just extracts two subvectors, shuffles them independently, and
9959 /// then concatenates them back together. This should work effectively with all
9960 /// AVX vector shuffle types.
9961 static SDValue splitAndLowerVectorShuffle(SDLoc DL, MVT VT, SDValue V1,
9962                                           SDValue V2, ArrayRef<int> Mask,
9963                                           SelectionDAG &DAG) {
9964   assert(VT.getSizeInBits() >= 256 &&
9965          "Only for 256-bit or wider vector shuffles!");
9966   assert(V1.getSimpleValueType() == VT && "Bad operand type!");
9967   assert(V2.getSimpleValueType() == VT && "Bad operand type!");
9968
9969   ArrayRef<int> LoMask = Mask.slice(0, Mask.size() / 2);
9970   ArrayRef<int> HiMask = Mask.slice(Mask.size() / 2);
9971
9972   int NumElements = VT.getVectorNumElements();
9973   int SplitNumElements = NumElements / 2;
9974   MVT ScalarVT = VT.getVectorElementType();
9975   MVT SplitVT = MVT::getVectorVT(ScalarVT, NumElements / 2);
9976
9977   // Rather than splitting build-vectors, just build two narrower build
9978   // vectors. This helps shuffling with splats and zeros.
9979   auto SplitVector = [&](SDValue V) {
9980     while (V.getOpcode() == ISD::BITCAST)
9981       V = V->getOperand(0);
9982
9983     MVT OrigVT = V.getSimpleValueType();
9984     int OrigNumElements = OrigVT.getVectorNumElements();
9985     int OrigSplitNumElements = OrigNumElements / 2;
9986     MVT OrigScalarVT = OrigVT.getVectorElementType();
9987     MVT OrigSplitVT = MVT::getVectorVT(OrigScalarVT, OrigNumElements / 2);
9988
9989     SDValue LoV, HiV;
9990
9991     auto *BV = dyn_cast<BuildVectorSDNode>(V);
9992     if (!BV) {
9993       LoV = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, OrigSplitVT, V,
9994                         DAG.getIntPtrConstant(0, DL));
9995       HiV = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, OrigSplitVT, V,
9996                         DAG.getIntPtrConstant(OrigSplitNumElements, DL));
9997     } else {
9998
9999       SmallVector<SDValue, 16> LoOps, HiOps;
10000       for (int i = 0; i < OrigSplitNumElements; ++i) {
10001         LoOps.push_back(BV->getOperand(i));
10002         HiOps.push_back(BV->getOperand(i + OrigSplitNumElements));
10003       }
10004       LoV = DAG.getNode(ISD::BUILD_VECTOR, DL, OrigSplitVT, LoOps);
10005       HiV = DAG.getNode(ISD::BUILD_VECTOR, DL, OrigSplitVT, HiOps);
10006     }
10007     return std::make_pair(DAG.getBitcast(SplitVT, LoV),
10008                           DAG.getBitcast(SplitVT, HiV));
10009   };
10010
10011   SDValue LoV1, HiV1, LoV2, HiV2;
10012   std::tie(LoV1, HiV1) = SplitVector(V1);
10013   std::tie(LoV2, HiV2) = SplitVector(V2);
10014
10015   // Now create two 4-way blends of these half-width vectors.
10016   auto HalfBlend = [&](ArrayRef<int> HalfMask) {
10017     bool UseLoV1 = false, UseHiV1 = false, UseLoV2 = false, UseHiV2 = false;
10018     SmallVector<int, 32> V1BlendMask, V2BlendMask, BlendMask;
10019     for (int i = 0; i < SplitNumElements; ++i) {
10020       int M = HalfMask[i];
10021       if (M >= NumElements) {
10022         if (M >= NumElements + SplitNumElements)
10023           UseHiV2 = true;
10024         else
10025           UseLoV2 = true;
10026         V2BlendMask.push_back(M - NumElements);
10027         V1BlendMask.push_back(-1);
10028         BlendMask.push_back(SplitNumElements + i);
10029       } else if (M >= 0) {
10030         if (M >= SplitNumElements)
10031           UseHiV1 = true;
10032         else
10033           UseLoV1 = true;
10034         V2BlendMask.push_back(-1);
10035         V1BlendMask.push_back(M);
10036         BlendMask.push_back(i);
10037       } else {
10038         V2BlendMask.push_back(-1);
10039         V1BlendMask.push_back(-1);
10040         BlendMask.push_back(-1);
10041       }
10042     }
10043
10044     // Because the lowering happens after all combining takes place, we need to
10045     // manually combine these blend masks as much as possible so that we create
10046     // a minimal number of high-level vector shuffle nodes.
10047
10048     // First try just blending the halves of V1 or V2.
10049     if (!UseLoV1 && !UseHiV1 && !UseLoV2 && !UseHiV2)
10050       return DAG.getUNDEF(SplitVT);
10051     if (!UseLoV2 && !UseHiV2)
10052       return DAG.getVectorShuffle(SplitVT, DL, LoV1, HiV1, V1BlendMask);
10053     if (!UseLoV1 && !UseHiV1)
10054       return DAG.getVectorShuffle(SplitVT, DL, LoV2, HiV2, V2BlendMask);
10055
10056     SDValue V1Blend, V2Blend;
10057     if (UseLoV1 && UseHiV1) {
10058       V1Blend =
10059         DAG.getVectorShuffle(SplitVT, DL, LoV1, HiV1, V1BlendMask);
10060     } else {
10061       // We only use half of V1 so map the usage down into the final blend mask.
10062       V1Blend = UseLoV1 ? LoV1 : HiV1;
10063       for (int i = 0; i < SplitNumElements; ++i)
10064         if (BlendMask[i] >= 0 && BlendMask[i] < SplitNumElements)
10065           BlendMask[i] = V1BlendMask[i] - (UseLoV1 ? 0 : SplitNumElements);
10066     }
10067     if (UseLoV2 && UseHiV2) {
10068       V2Blend =
10069         DAG.getVectorShuffle(SplitVT, DL, LoV2, HiV2, V2BlendMask);
10070     } else {
10071       // We only use half of V2 so map the usage down into the final blend mask.
10072       V2Blend = UseLoV2 ? LoV2 : HiV2;
10073       for (int i = 0; i < SplitNumElements; ++i)
10074         if (BlendMask[i] >= SplitNumElements)
10075           BlendMask[i] = V2BlendMask[i] + (UseLoV2 ? SplitNumElements : 0);
10076     }
10077     return DAG.getVectorShuffle(SplitVT, DL, V1Blend, V2Blend, BlendMask);
10078   };
10079   SDValue Lo = HalfBlend(LoMask);
10080   SDValue Hi = HalfBlend(HiMask);
10081   return DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, Lo, Hi);
10082 }
10083
10084 /// \brief Either split a vector in halves or decompose the shuffles and the
10085 /// blend.
10086 ///
10087 /// This is provided as a good fallback for many lowerings of non-single-input
10088 /// shuffles with more than one 128-bit lane. In those cases, we want to select
10089 /// between splitting the shuffle into 128-bit components and stitching those
10090 /// back together vs. extracting the single-input shuffles and blending those
10091 /// results.
10092 static SDValue lowerVectorShuffleAsSplitOrBlend(SDLoc DL, MVT VT, SDValue V1,
10093                                                 SDValue V2, ArrayRef<int> Mask,
10094                                                 SelectionDAG &DAG) {
10095   assert(!isSingleInputShuffleMask(Mask) && "This routine must not be used to "
10096                                             "lower single-input shuffles as it "
10097                                             "could then recurse on itself.");
10098   int Size = Mask.size();
10099
10100   // If this can be modeled as a broadcast of two elements followed by a blend,
10101   // prefer that lowering. This is especially important because broadcasts can
10102   // often fold with memory operands.
10103   auto DoBothBroadcast = [&] {
10104     int V1BroadcastIdx = -1, V2BroadcastIdx = -1;
10105     for (int M : Mask)
10106       if (M >= Size) {
10107         if (V2BroadcastIdx == -1)
10108           V2BroadcastIdx = M - Size;
10109         else if (M - Size != V2BroadcastIdx)
10110           return false;
10111       } else if (M >= 0) {
10112         if (V1BroadcastIdx == -1)
10113           V1BroadcastIdx = M;
10114         else if (M != V1BroadcastIdx)
10115           return false;
10116       }
10117     return true;
10118   };
10119   if (DoBothBroadcast())
10120     return lowerVectorShuffleAsDecomposedShuffleBlend(DL, VT, V1, V2, Mask,
10121                                                       DAG);
10122
10123   // If the inputs all stem from a single 128-bit lane of each input, then we
10124   // split them rather than blending because the split will decompose to
10125   // unusually few instructions.
10126   int LaneCount = VT.getSizeInBits() / 128;
10127   int LaneSize = Size / LaneCount;
10128   SmallBitVector LaneInputs[2];
10129   LaneInputs[0].resize(LaneCount, false);
10130   LaneInputs[1].resize(LaneCount, false);
10131   for (int i = 0; i < Size; ++i)
10132     if (Mask[i] >= 0)
10133       LaneInputs[Mask[i] / Size][(Mask[i] % Size) / LaneSize] = true;
10134   if (LaneInputs[0].count() <= 1 && LaneInputs[1].count() <= 1)
10135     return splitAndLowerVectorShuffle(DL, VT, V1, V2, Mask, DAG);
10136
10137   // Otherwise, just fall back to decomposed shuffles and a blend. This requires
10138   // that the decomposed single-input shuffles don't end up here.
10139   return lowerVectorShuffleAsDecomposedShuffleBlend(DL, VT, V1, V2, Mask, DAG);
10140 }
10141
10142 /// \brief Lower a vector shuffle crossing multiple 128-bit lanes as
10143 /// a permutation and blend of those lanes.
10144 ///
10145 /// This essentially blends the out-of-lane inputs to each lane into the lane
10146 /// from a permuted copy of the vector. This lowering strategy results in four
10147 /// instructions in the worst case for a single-input cross lane shuffle which
10148 /// is lower than any other fully general cross-lane shuffle strategy I'm aware
10149 /// of. Special cases for each particular shuffle pattern should be handled
10150 /// prior to trying this lowering.
10151 static SDValue lowerVectorShuffleAsLanePermuteAndBlend(SDLoc DL, MVT VT,
10152                                                        SDValue V1, SDValue V2,
10153                                                        ArrayRef<int> Mask,
10154                                                        SelectionDAG &DAG) {
10155   // FIXME: This should probably be generalized for 512-bit vectors as well.
10156   assert(VT.is256BitVector() && "Only for 256-bit vector shuffles!");
10157   int LaneSize = Mask.size() / 2;
10158
10159   // If there are only inputs from one 128-bit lane, splitting will in fact be
10160   // less expensive. The flags track whether the given lane contains an element
10161   // that crosses to another lane.
10162   bool LaneCrossing[2] = {false, false};
10163   for (int i = 0, Size = Mask.size(); i < Size; ++i)
10164     if (Mask[i] >= 0 && (Mask[i] % Size) / LaneSize != i / LaneSize)
10165       LaneCrossing[(Mask[i] % Size) / LaneSize] = true;
10166   if (!LaneCrossing[0] || !LaneCrossing[1])
10167     return splitAndLowerVectorShuffle(DL, VT, V1, V2, Mask, DAG);
10168
10169   if (isSingleInputShuffleMask(Mask)) {
10170     SmallVector<int, 32> FlippedBlendMask;
10171     for (int i = 0, Size = Mask.size(); i < Size; ++i)
10172       FlippedBlendMask.push_back(
10173           Mask[i] < 0 ? -1 : (((Mask[i] % Size) / LaneSize == i / LaneSize)
10174                                   ? Mask[i]
10175                                   : Mask[i] % LaneSize +
10176                                         (i / LaneSize) * LaneSize + Size));
10177
10178     // Flip the vector, and blend the results which should now be in-lane. The
10179     // VPERM2X128 mask uses the low 2 bits for the low source and bits 4 and
10180     // 5 for the high source. The value 3 selects the high half of source 2 and
10181     // the value 2 selects the low half of source 2. We only use source 2 to
10182     // allow folding it into a memory operand.
10183     unsigned PERMMask = 3 | 2 << 4;
10184     SDValue Flipped = DAG.getNode(X86ISD::VPERM2X128, DL, VT, DAG.getUNDEF(VT),
10185                                   V1, DAG.getConstant(PERMMask, DL, MVT::i8));
10186     return DAG.getVectorShuffle(VT, DL, V1, Flipped, FlippedBlendMask);
10187   }
10188
10189   // This now reduces to two single-input shuffles of V1 and V2 which at worst
10190   // will be handled by the above logic and a blend of the results, much like
10191   // other patterns in AVX.
10192   return lowerVectorShuffleAsDecomposedShuffleBlend(DL, VT, V1, V2, Mask, DAG);
10193 }
10194
10195 /// \brief Handle lowering 2-lane 128-bit shuffles.
10196 static SDValue lowerV2X128VectorShuffle(SDLoc DL, MVT VT, SDValue V1,
10197                                         SDValue V2, ArrayRef<int> Mask,
10198                                         const X86Subtarget *Subtarget,
10199                                         SelectionDAG &DAG) {
10200   // TODO: If minimizing size and one of the inputs is a zero vector and the
10201   // the zero vector has only one use, we could use a VPERM2X128 to save the
10202   // instruction bytes needed to explicitly generate the zero vector.
10203
10204   // Blends are faster and handle all the non-lane-crossing cases.
10205   if (SDValue Blend = lowerVectorShuffleAsBlend(DL, VT, V1, V2, Mask,
10206                                                 Subtarget, DAG))
10207     return Blend;
10208
10209   bool IsV1Zero = ISD::isBuildVectorAllZeros(V1.getNode());
10210   bool IsV2Zero = ISD::isBuildVectorAllZeros(V2.getNode());
10211
10212   // If either input operand is a zero vector, use VPERM2X128 because its mask
10213   // allows us to replace the zero input with an implicit zero.
10214   if (!IsV1Zero && !IsV2Zero) {
10215     // Check for patterns which can be matched with a single insert of a 128-bit
10216     // subvector.
10217     bool OnlyUsesV1 = isShuffleEquivalent(V1, V2, Mask, {0, 1, 0, 1});
10218     if (OnlyUsesV1 || isShuffleEquivalent(V1, V2, Mask, {0, 1, 4, 5})) {
10219       MVT SubVT = MVT::getVectorVT(VT.getVectorElementType(),
10220                                    VT.getVectorNumElements() / 2);
10221       SDValue LoV = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, SubVT, V1,
10222                                 DAG.getIntPtrConstant(0, DL));
10223       SDValue HiV = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, SubVT,
10224                                 OnlyUsesV1 ? V1 : V2,
10225                                 DAG.getIntPtrConstant(0, DL));
10226       return DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, LoV, HiV);
10227     }
10228   }
10229
10230   // Otherwise form a 128-bit permutation. After accounting for undefs,
10231   // convert the 64-bit shuffle mask selection values into 128-bit
10232   // selection bits by dividing the indexes by 2 and shifting into positions
10233   // defined by a vperm2*128 instruction's immediate control byte.
10234
10235   // The immediate permute control byte looks like this:
10236   //    [1:0] - select 128 bits from sources for low half of destination
10237   //    [2]   - ignore
10238   //    [3]   - zero low half of destination
10239   //    [5:4] - select 128 bits from sources for high half of destination
10240   //    [6]   - ignore
10241   //    [7]   - zero high half of destination
10242
10243   int MaskLO = Mask[0];
10244   if (MaskLO == SM_SentinelUndef)
10245     MaskLO = Mask[1] == SM_SentinelUndef ? 0 : Mask[1];
10246
10247   int MaskHI = Mask[2];
10248   if (MaskHI == SM_SentinelUndef)
10249     MaskHI = Mask[3] == SM_SentinelUndef ? 0 : Mask[3];
10250
10251   unsigned PermMask = MaskLO / 2 | (MaskHI / 2) << 4;
10252
10253   // If either input is a zero vector, replace it with an undef input.
10254   // Shuffle mask values <  4 are selecting elements of V1.
10255   // Shuffle mask values >= 4 are selecting elements of V2.
10256   // Adjust each half of the permute mask by clearing the half that was
10257   // selecting the zero vector and setting the zero mask bit.
10258   if (IsV1Zero) {
10259     V1 = DAG.getUNDEF(VT);
10260     if (MaskLO < 4)
10261       PermMask = (PermMask & 0xf0) | 0x08;
10262     if (MaskHI < 4)
10263       PermMask = (PermMask & 0x0f) | 0x80;
10264   }
10265   if (IsV2Zero) {
10266     V2 = DAG.getUNDEF(VT);
10267     if (MaskLO >= 4)
10268       PermMask = (PermMask & 0xf0) | 0x08;
10269     if (MaskHI >= 4)
10270       PermMask = (PermMask & 0x0f) | 0x80;
10271   }
10272
10273   return DAG.getNode(X86ISD::VPERM2X128, DL, VT, V1, V2,
10274                      DAG.getConstant(PermMask, DL, MVT::i8));
10275 }
10276
10277 /// \brief Lower a vector shuffle by first fixing the 128-bit lanes and then
10278 /// shuffling each lane.
10279 ///
10280 /// This will only succeed when the result of fixing the 128-bit lanes results
10281 /// in a single-input non-lane-crossing shuffle with a repeating shuffle mask in
10282 /// each 128-bit lanes. This handles many cases where we can quickly blend away
10283 /// the lane crosses early and then use simpler shuffles within each lane.
10284 ///
10285 /// FIXME: It might be worthwhile at some point to support this without
10286 /// requiring the 128-bit lane-relative shuffles to be repeating, but currently
10287 /// in x86 only floating point has interesting non-repeating shuffles, and even
10288 /// those are still *marginally* more expensive.
10289 static SDValue lowerVectorShuffleByMerging128BitLanes(
10290     SDLoc DL, MVT VT, SDValue V1, SDValue V2, ArrayRef<int> Mask,
10291     const X86Subtarget *Subtarget, SelectionDAG &DAG) {
10292   assert(!isSingleInputShuffleMask(Mask) &&
10293          "This is only useful with multiple inputs.");
10294
10295   int Size = Mask.size();
10296   int LaneSize = 128 / VT.getScalarSizeInBits();
10297   int NumLanes = Size / LaneSize;
10298   assert(NumLanes > 1 && "Only handles 256-bit and wider shuffles.");
10299
10300   // See if we can build a hypothetical 128-bit lane-fixing shuffle mask. Also
10301   // check whether the in-128-bit lane shuffles share a repeating pattern.
10302   SmallVector<int, 4> Lanes;
10303   Lanes.resize(NumLanes, -1);
10304   SmallVector<int, 4> InLaneMask;
10305   InLaneMask.resize(LaneSize, -1);
10306   for (int i = 0; i < Size; ++i) {
10307     if (Mask[i] < 0)
10308       continue;
10309
10310     int j = i / LaneSize;
10311
10312     if (Lanes[j] < 0) {
10313       // First entry we've seen for this lane.
10314       Lanes[j] = Mask[i] / LaneSize;
10315     } else if (Lanes[j] != Mask[i] / LaneSize) {
10316       // This doesn't match the lane selected previously!
10317       return SDValue();
10318     }
10319
10320     // Check that within each lane we have a consistent shuffle mask.
10321     int k = i % LaneSize;
10322     if (InLaneMask[k] < 0) {
10323       InLaneMask[k] = Mask[i] % LaneSize;
10324     } else if (InLaneMask[k] != Mask[i] % LaneSize) {
10325       // This doesn't fit a repeating in-lane mask.
10326       return SDValue();
10327     }
10328   }
10329
10330   // First shuffle the lanes into place.
10331   MVT LaneVT = MVT::getVectorVT(VT.isFloatingPoint() ? MVT::f64 : MVT::i64,
10332                                 VT.getSizeInBits() / 64);
10333   SmallVector<int, 8> LaneMask;
10334   LaneMask.resize(NumLanes * 2, -1);
10335   for (int i = 0; i < NumLanes; ++i)
10336     if (Lanes[i] >= 0) {
10337       LaneMask[2 * i + 0] = 2*Lanes[i] + 0;
10338       LaneMask[2 * i + 1] = 2*Lanes[i] + 1;
10339     }
10340
10341   V1 = DAG.getBitcast(LaneVT, V1);
10342   V2 = DAG.getBitcast(LaneVT, V2);
10343   SDValue LaneShuffle = DAG.getVectorShuffle(LaneVT, DL, V1, V2, LaneMask);
10344
10345   // Cast it back to the type we actually want.
10346   LaneShuffle = DAG.getBitcast(VT, LaneShuffle);
10347
10348   // Now do a simple shuffle that isn't lane crossing.
10349   SmallVector<int, 8> NewMask;
10350   NewMask.resize(Size, -1);
10351   for (int i = 0; i < Size; ++i)
10352     if (Mask[i] >= 0)
10353       NewMask[i] = (i / LaneSize) * LaneSize + Mask[i] % LaneSize;
10354   assert(!is128BitLaneCrossingShuffleMask(VT, NewMask) &&
10355          "Must not introduce lane crosses at this point!");
10356
10357   return DAG.getVectorShuffle(VT, DL, LaneShuffle, DAG.getUNDEF(VT), NewMask);
10358 }
10359
10360 /// Lower shuffles where an entire half of a 256-bit vector is UNDEF.
10361 /// This allows for fast cases such as subvector extraction/insertion
10362 /// or shuffling smaller vector types which can lower more efficiently.
10363 static SDValue lowerVectorShuffleWithUndefHalf(SDLoc DL, MVT VT, SDValue V1,
10364                                                SDValue V2, ArrayRef<int> Mask,
10365                                                const X86Subtarget *Subtarget,
10366                                                SelectionDAG &DAG) {
10367   assert(VT.getSizeInBits() == 256 && "Expected 256-bit vector");
10368
10369   unsigned NumElts = VT.getVectorNumElements();
10370   unsigned HalfNumElts = NumElts / 2;
10371   MVT HalfVT = MVT::getVectorVT(VT.getVectorElementType(), HalfNumElts);
10372
10373   bool UndefLower = isUndefInRange(Mask, 0, HalfNumElts);
10374   bool UndefUpper = isUndefInRange(Mask, HalfNumElts, HalfNumElts);
10375   if (!UndefLower && !UndefUpper)
10376     return SDValue();
10377
10378   // Upper half is undef and lower half is whole upper subvector.
10379   // e.g. vector_shuffle <4, 5, 6, 7, u, u, u, u> or <2, 3, u, u>
10380   if (UndefUpper &&
10381       isSequentialOrUndefInRange(Mask, 0, HalfNumElts, HalfNumElts)) {
10382     SDValue Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, HalfVT, V1,
10383                              DAG.getIntPtrConstant(HalfNumElts, DL));
10384     return DAG.getNode(ISD::INSERT_SUBVECTOR, DL, VT, DAG.getUNDEF(VT), Hi,
10385                        DAG.getIntPtrConstant(0, DL));
10386   }
10387
10388   // Lower half is undef and upper half is whole lower subvector.
10389   // e.g. vector_shuffle <u, u, u, u, 0, 1, 2, 3> or <u, u, 0, 1>
10390   if (UndefLower &&
10391       isSequentialOrUndefInRange(Mask, HalfNumElts, HalfNumElts, 0)) {
10392     SDValue Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, HalfVT, V1,
10393                              DAG.getIntPtrConstant(0, DL));
10394     return DAG.getNode(ISD::INSERT_SUBVECTOR, DL, VT, DAG.getUNDEF(VT), Hi,
10395                        DAG.getIntPtrConstant(HalfNumElts, DL));
10396   }
10397
10398   // AVX2 supports efficient immediate 64-bit element cross-lane shuffles.
10399   if (UndefLower && Subtarget->hasAVX2() &&
10400       (VT == MVT::v4f64 || VT == MVT::v4i64))
10401     return SDValue();
10402
10403   // If the shuffle only uses the lower halves of the input operands,
10404   // then extract them and perform the 'half' shuffle at half width.
10405   // e.g. vector_shuffle <X, X, X, X, u, u, u, u> or <X, X, u, u>
10406   int HalfIdx1 = -1, HalfIdx2 = -1;
10407   SmallVector<int, 8> HalfMask;
10408   unsigned Offset = UndefLower ? HalfNumElts : 0;
10409   for (unsigned i = 0; i != HalfNumElts; ++i) {
10410     int M = Mask[i + Offset];
10411     if (M < 0) {
10412       HalfMask.push_back(M);
10413       continue;
10414     }
10415
10416     // Determine which of the 4 half vectors this element is from.
10417     // i.e. 0 = Lower V1, 1 = Upper V1, 2 = Lower V2, 3 = Upper V2.
10418     int HalfIdx = M / HalfNumElts;
10419
10420     // Only shuffle using the lower halves of the inputs.
10421     // TODO: Investigate usefulness of shuffling with upper halves.
10422     if (HalfIdx != 0 && HalfIdx != 2)
10423       return SDValue();
10424
10425     // Determine the element index into its half vector source.
10426     int HalfElt = M % HalfNumElts;
10427
10428     // We can shuffle with up to 2 half vectors, set the new 'half'
10429     // shuffle mask accordingly.
10430     if (-1 == HalfIdx1 || HalfIdx1 == HalfIdx) {
10431       HalfMask.push_back(HalfElt);
10432       HalfIdx1 = HalfIdx;
10433       continue;
10434     }
10435     if (-1 == HalfIdx2 || HalfIdx2 == HalfIdx) {
10436       HalfMask.push_back(HalfElt + HalfNumElts);
10437       HalfIdx2 = HalfIdx;
10438       continue;
10439     }
10440
10441     // Too many half vectors referenced.
10442     return SDValue();
10443   }
10444   assert(HalfMask.size() == HalfNumElts && "Unexpected shuffle mask length");
10445
10446   auto GetHalfVector = [&](int HalfIdx) {
10447     if (HalfIdx < 0)
10448       return DAG.getUNDEF(HalfVT);
10449     SDValue V = (HalfIdx < 2 ? V1 : V2);
10450     HalfIdx = (HalfIdx % 2) * HalfNumElts;
10451     return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, HalfVT, V,
10452                        DAG.getIntPtrConstant(HalfIdx, DL));
10453   };
10454
10455   SDValue Half1 = GetHalfVector(HalfIdx1);
10456   SDValue Half2 = GetHalfVector(HalfIdx2);
10457   SDValue V = DAG.getVectorShuffle(HalfVT, DL, Half1, Half2, HalfMask);
10458   return DAG.getNode(ISD::INSERT_SUBVECTOR, DL, VT, DAG.getUNDEF(VT), V,
10459                      DAG.getIntPtrConstant(Offset, DL));
10460 }
10461
10462 /// \brief Test whether the specified input (0 or 1) is in-place blended by the
10463 /// given mask.
10464 ///
10465 /// This returns true if the elements from a particular input are already in the
10466 /// slot required by the given mask and require no permutation.
10467 static bool isShuffleMaskInputInPlace(int Input, ArrayRef<int> Mask) {
10468   assert((Input == 0 || Input == 1) && "Only two inputs to shuffles.");
10469   int Size = Mask.size();
10470   for (int i = 0; i < Size; ++i)
10471     if (Mask[i] >= 0 && Mask[i] / Size == Input && Mask[i] % Size != i)
10472       return false;
10473
10474   return true;
10475 }
10476
10477 static SDValue lowerVectorShuffleWithSHUFPD(SDLoc DL, MVT VT,
10478                                             ArrayRef<int> Mask, SDValue V1,
10479                                             SDValue V2, SelectionDAG &DAG) {
10480
10481   // Mask for V8F64: 0/1,  8/9,  2/3,  10/11, 4/5, ..
10482   // Mask for V4F64; 0/1,  4/5,  2/3,  6/7..
10483   assert(VT.getScalarSizeInBits() == 64 && "Unexpected data type for VSHUFPD");
10484   int NumElts = VT.getVectorNumElements();
10485   bool ShufpdMask = true;
10486   bool CommutableMask = true;
10487   unsigned Immediate = 0;
10488   for (int i = 0; i < NumElts; ++i) {
10489     if (Mask[i] < 0)
10490       continue;
10491     int Val = (i & 6) + NumElts * (i & 1);
10492     int CommutVal = (i & 0xe) + NumElts * ((i & 1)^1);
10493     if (Mask[i] < Val ||  Mask[i] > Val + 1)
10494       ShufpdMask = false;
10495     if (Mask[i] < CommutVal ||  Mask[i] > CommutVal + 1)
10496       CommutableMask = false;
10497     Immediate |= (Mask[i] % 2) << i;
10498   }
10499   if (ShufpdMask)
10500     return DAG.getNode(X86ISD::SHUFP, DL, VT, V1, V2,
10501                        DAG.getConstant(Immediate, DL, MVT::i8));
10502   if (CommutableMask)
10503     return DAG.getNode(X86ISD::SHUFP, DL, VT, V2, V1,
10504                        DAG.getConstant(Immediate, DL, MVT::i8));
10505   return SDValue();
10506 }
10507
10508 /// \brief Handle lowering of 4-lane 64-bit floating point shuffles.
10509 ///
10510 /// Also ends up handling lowering of 4-lane 64-bit integer shuffles when AVX2
10511 /// isn't available.
10512 static SDValue lowerV4F64VectorShuffle(SDValue Op, SDValue V1, SDValue V2,
10513                                        const X86Subtarget *Subtarget,
10514                                        SelectionDAG &DAG) {
10515   SDLoc DL(Op);
10516   assert(V1.getSimpleValueType() == MVT::v4f64 && "Bad operand type!");
10517   assert(V2.getSimpleValueType() == MVT::v4f64 && "Bad operand type!");
10518   ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
10519   ArrayRef<int> Mask = SVOp->getMask();
10520   assert(Mask.size() == 4 && "Unexpected mask size for v4 shuffle!");
10521
10522   SmallVector<int, 4> WidenedMask;
10523   if (canWidenShuffleElements(Mask, WidenedMask))
10524     return lowerV2X128VectorShuffle(DL, MVT::v4f64, V1, V2, Mask, Subtarget,
10525                                     DAG);
10526
10527   if (isSingleInputShuffleMask(Mask)) {
10528     // Check for being able to broadcast a single element.
10529     if (SDValue Broadcast = lowerVectorShuffleAsBroadcast(DL, MVT::v4f64, V1,
10530                                                           Mask, Subtarget, DAG))
10531       return Broadcast;
10532
10533     // Use low duplicate instructions for masks that match their pattern.
10534     if (isShuffleEquivalent(V1, V2, Mask, {0, 0, 2, 2}))
10535       return DAG.getNode(X86ISD::MOVDDUP, DL, MVT::v4f64, V1);
10536
10537     if (!is128BitLaneCrossingShuffleMask(MVT::v4f64, Mask)) {
10538       // Non-half-crossing single input shuffles can be lowerid with an
10539       // interleaved permutation.
10540       unsigned VPERMILPMask = (Mask[0] == 1) | ((Mask[1] == 1) << 1) |
10541                               ((Mask[2] == 3) << 2) | ((Mask[3] == 3) << 3);
10542       return DAG.getNode(X86ISD::VPERMILPI, DL, MVT::v4f64, V1,
10543                          DAG.getConstant(VPERMILPMask, DL, MVT::i8));
10544     }
10545
10546     // With AVX2 we have direct support for this permutation.
10547     if (Subtarget->hasAVX2())
10548       return DAG.getNode(X86ISD::VPERMI, DL, MVT::v4f64, V1,
10549                          getV4X86ShuffleImm8ForMask(Mask, DL, DAG));
10550
10551     // Otherwise, fall back.
10552     return lowerVectorShuffleAsLanePermuteAndBlend(DL, MVT::v4f64, V1, V2, Mask,
10553                                                    DAG);
10554   }
10555
10556   // Use dedicated unpack instructions for masks that match their pattern.
10557   if (SDValue V =
10558           lowerVectorShuffleWithUNPCK(DL, MVT::v4f64, Mask, V1, V2, DAG))
10559     return V;
10560
10561   if (SDValue Blend = lowerVectorShuffleAsBlend(DL, MVT::v4f64, V1, V2, Mask,
10562                                                 Subtarget, DAG))
10563     return Blend;
10564
10565   // Check if the blend happens to exactly fit that of SHUFPD.
10566   if (SDValue Op =
10567       lowerVectorShuffleWithSHUFPD(DL, MVT::v4f64, Mask, V1, V2, DAG))
10568     return Op;
10569
10570   // Try to simplify this by merging 128-bit lanes to enable a lane-based
10571   // shuffle. However, if we have AVX2 and either inputs are already in place,
10572   // we will be able to shuffle even across lanes the other input in a single
10573   // instruction so skip this pattern.
10574   if (!(Subtarget->hasAVX2() && (isShuffleMaskInputInPlace(0, Mask) ||
10575                                  isShuffleMaskInputInPlace(1, Mask))))
10576     if (SDValue Result = lowerVectorShuffleByMerging128BitLanes(
10577             DL, MVT::v4f64, V1, V2, Mask, Subtarget, DAG))
10578       return Result;
10579
10580   // If we have AVX2 then we always want to lower with a blend because an v4 we
10581   // can fully permute the elements.
10582   if (Subtarget->hasAVX2())
10583     return lowerVectorShuffleAsDecomposedShuffleBlend(DL, MVT::v4f64, V1, V2,
10584                                                       Mask, DAG);
10585
10586   // Otherwise fall back on generic lowering.
10587   return lowerVectorShuffleAsSplitOrBlend(DL, MVT::v4f64, V1, V2, Mask, DAG);
10588 }
10589
10590 /// \brief Handle lowering of 4-lane 64-bit integer shuffles.
10591 ///
10592 /// This routine is only called when we have AVX2 and thus a reasonable
10593 /// instruction set for v4i64 shuffling..
10594 static SDValue lowerV4I64VectorShuffle(SDValue Op, SDValue V1, SDValue V2,
10595                                        const X86Subtarget *Subtarget,
10596                                        SelectionDAG &DAG) {
10597   SDLoc DL(Op);
10598   assert(V1.getSimpleValueType() == MVT::v4i64 && "Bad operand type!");
10599   assert(V2.getSimpleValueType() == MVT::v4i64 && "Bad operand type!");
10600   ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
10601   ArrayRef<int> Mask = SVOp->getMask();
10602   assert(Mask.size() == 4 && "Unexpected mask size for v4 shuffle!");
10603   assert(Subtarget->hasAVX2() && "We can only lower v4i64 with AVX2!");
10604
10605   SmallVector<int, 4> WidenedMask;
10606   if (canWidenShuffleElements(Mask, WidenedMask))
10607     return lowerV2X128VectorShuffle(DL, MVT::v4i64, V1, V2, Mask, Subtarget,
10608                                     DAG);
10609
10610   if (SDValue Blend = lowerVectorShuffleAsBlend(DL, MVT::v4i64, V1, V2, Mask,
10611                                                 Subtarget, DAG))
10612     return Blend;
10613
10614   // Check for being able to broadcast a single element.
10615   if (SDValue Broadcast = lowerVectorShuffleAsBroadcast(DL, MVT::v4i64, V1,
10616                                                         Mask, Subtarget, DAG))
10617     return Broadcast;
10618
10619   // When the shuffle is mirrored between the 128-bit lanes of the unit, we can
10620   // use lower latency instructions that will operate on both 128-bit lanes.
10621   SmallVector<int, 2> RepeatedMask;
10622   if (is128BitLaneRepeatedShuffleMask(MVT::v4i64, Mask, RepeatedMask)) {
10623     if (isSingleInputShuffleMask(Mask)) {
10624       int PSHUFDMask[] = {-1, -1, -1, -1};
10625       for (int i = 0; i < 2; ++i)
10626         if (RepeatedMask[i] >= 0) {
10627           PSHUFDMask[2 * i] = 2 * RepeatedMask[i];
10628           PSHUFDMask[2 * i + 1] = 2 * RepeatedMask[i] + 1;
10629         }
10630       return DAG.getBitcast(
10631           MVT::v4i64,
10632           DAG.getNode(X86ISD::PSHUFD, DL, MVT::v8i32,
10633                       DAG.getBitcast(MVT::v8i32, V1),
10634                       getV4X86ShuffleImm8ForMask(PSHUFDMask, DL, DAG)));
10635     }
10636   }
10637
10638   // AVX2 provides a direct instruction for permuting a single input across
10639   // lanes.
10640   if (isSingleInputShuffleMask(Mask))
10641     return DAG.getNode(X86ISD::VPERMI, DL, MVT::v4i64, V1,
10642                        getV4X86ShuffleImm8ForMask(Mask, DL, DAG));
10643
10644   // Try to use shift instructions.
10645   if (SDValue Shift =
10646           lowerVectorShuffleAsShift(DL, MVT::v4i64, V1, V2, Mask, DAG))
10647     return Shift;
10648
10649   // Use dedicated unpack instructions for masks that match their pattern.
10650   if (SDValue V =
10651           lowerVectorShuffleWithUNPCK(DL, MVT::v4i64, Mask, V1, V2, DAG))
10652     return V;
10653
10654   // Try to simplify this by merging 128-bit lanes to enable a lane-based
10655   // shuffle. However, if we have AVX2 and either inputs are already in place,
10656   // we will be able to shuffle even across lanes the other input in a single
10657   // instruction so skip this pattern.
10658   if (!(Subtarget->hasAVX2() && (isShuffleMaskInputInPlace(0, Mask) ||
10659                                  isShuffleMaskInputInPlace(1, Mask))))
10660     if (SDValue Result = lowerVectorShuffleByMerging128BitLanes(
10661             DL, MVT::v4i64, V1, V2, Mask, Subtarget, DAG))
10662       return Result;
10663
10664   // Otherwise fall back on generic blend lowering.
10665   return lowerVectorShuffleAsDecomposedShuffleBlend(DL, MVT::v4i64, V1, V2,
10666                                                     Mask, DAG);
10667 }
10668
10669 /// \brief Handle lowering of 8-lane 32-bit floating point shuffles.
10670 ///
10671 /// Also ends up handling lowering of 8-lane 32-bit integer shuffles when AVX2
10672 /// isn't available.
10673 static SDValue lowerV8F32VectorShuffle(SDValue Op, SDValue V1, SDValue V2,
10674                                        const X86Subtarget *Subtarget,
10675                                        SelectionDAG &DAG) {
10676   SDLoc DL(Op);
10677   assert(V1.getSimpleValueType() == MVT::v8f32 && "Bad operand type!");
10678   assert(V2.getSimpleValueType() == MVT::v8f32 && "Bad operand type!");
10679   ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
10680   ArrayRef<int> Mask = SVOp->getMask();
10681   assert(Mask.size() == 8 && "Unexpected mask size for v8 shuffle!");
10682
10683   if (SDValue Blend = lowerVectorShuffleAsBlend(DL, MVT::v8f32, V1, V2, Mask,
10684                                                 Subtarget, DAG))
10685     return Blend;
10686
10687   // Check for being able to broadcast a single element.
10688   if (SDValue Broadcast = lowerVectorShuffleAsBroadcast(DL, MVT::v8f32, V1,
10689                                                         Mask, Subtarget, DAG))
10690     return Broadcast;
10691
10692   // If the shuffle mask is repeated in each 128-bit lane, we have many more
10693   // options to efficiently lower the shuffle.
10694   SmallVector<int, 4> RepeatedMask;
10695   if (is128BitLaneRepeatedShuffleMask(MVT::v8f32, Mask, RepeatedMask)) {
10696     assert(RepeatedMask.size() == 4 &&
10697            "Repeated masks must be half the mask width!");
10698
10699     // Use even/odd duplicate instructions for masks that match their pattern.
10700     if (isShuffleEquivalent(V1, V2, Mask, {0, 0, 2, 2, 4, 4, 6, 6}))
10701       return DAG.getNode(X86ISD::MOVSLDUP, DL, MVT::v8f32, V1);
10702     if (isShuffleEquivalent(V1, V2, Mask, {1, 1, 3, 3, 5, 5, 7, 7}))
10703       return DAG.getNode(X86ISD::MOVSHDUP, DL, MVT::v8f32, V1);
10704
10705     if (isSingleInputShuffleMask(Mask))
10706       return DAG.getNode(X86ISD::VPERMILPI, DL, MVT::v8f32, V1,
10707                          getV4X86ShuffleImm8ForMask(RepeatedMask, DL, DAG));
10708
10709     // Use dedicated unpack instructions for masks that match their pattern.
10710     if (SDValue V =
10711             lowerVectorShuffleWithUNPCK(DL, MVT::v8f32, Mask, V1, V2, DAG))
10712       return V;
10713
10714     // Otherwise, fall back to a SHUFPS sequence. Here it is important that we
10715     // have already handled any direct blends. We also need to squash the
10716     // repeated mask into a simulated v4f32 mask.
10717     for (int i = 0; i < 4; ++i)
10718       if (RepeatedMask[i] >= 8)
10719         RepeatedMask[i] -= 4;
10720     return lowerVectorShuffleWithSHUFPS(DL, MVT::v8f32, RepeatedMask, V1, V2, DAG);
10721   }
10722
10723   // If we have a single input shuffle with different shuffle patterns in the
10724   // two 128-bit lanes use the variable mask to VPERMILPS.
10725   if (isSingleInputShuffleMask(Mask)) {
10726     SDValue VPermMask[8];
10727     for (int i = 0; i < 8; ++i)
10728       VPermMask[i] = Mask[i] < 0 ? DAG.getUNDEF(MVT::i32)
10729                                  : DAG.getConstant(Mask[i], DL, MVT::i32);
10730     if (!is128BitLaneCrossingShuffleMask(MVT::v8f32, Mask))
10731       return DAG.getNode(
10732           X86ISD::VPERMILPV, DL, MVT::v8f32, V1,
10733           DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i32, VPermMask));
10734
10735     if (Subtarget->hasAVX2())
10736       return DAG.getNode(
10737           X86ISD::VPERMV, DL, MVT::v8f32,
10738           DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i32, VPermMask), V1);
10739
10740     // Otherwise, fall back.
10741     return lowerVectorShuffleAsLanePermuteAndBlend(DL, MVT::v8f32, V1, V2, Mask,
10742                                                    DAG);
10743   }
10744
10745   // Try to simplify this by merging 128-bit lanes to enable a lane-based
10746   // shuffle.
10747   if (SDValue Result = lowerVectorShuffleByMerging128BitLanes(
10748           DL, MVT::v8f32, V1, V2, Mask, Subtarget, DAG))
10749     return Result;
10750
10751   // If we have AVX2 then we always want to lower with a blend because at v8 we
10752   // can fully permute the elements.
10753   if (Subtarget->hasAVX2())
10754     return lowerVectorShuffleAsDecomposedShuffleBlend(DL, MVT::v8f32, V1, V2,
10755                                                       Mask, DAG);
10756
10757   // Otherwise fall back on generic lowering.
10758   return lowerVectorShuffleAsSplitOrBlend(DL, MVT::v8f32, V1, V2, Mask, DAG);
10759 }
10760
10761 /// \brief Handle lowering of 8-lane 32-bit integer shuffles.
10762 ///
10763 /// This routine is only called when we have AVX2 and thus a reasonable
10764 /// instruction set for v8i32 shuffling..
10765 static SDValue lowerV8I32VectorShuffle(SDValue Op, SDValue V1, SDValue V2,
10766                                        const X86Subtarget *Subtarget,
10767                                        SelectionDAG &DAG) {
10768   SDLoc DL(Op);
10769   assert(V1.getSimpleValueType() == MVT::v8i32 && "Bad operand type!");
10770   assert(V2.getSimpleValueType() == MVT::v8i32 && "Bad operand type!");
10771   ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
10772   ArrayRef<int> Mask = SVOp->getMask();
10773   assert(Mask.size() == 8 && "Unexpected mask size for v8 shuffle!");
10774   assert(Subtarget->hasAVX2() && "We can only lower v8i32 with AVX2!");
10775
10776   // Whenever we can lower this as a zext, that instruction is strictly faster
10777   // than any alternative. It also allows us to fold memory operands into the
10778   // shuffle in many cases.
10779   if (SDValue ZExt = lowerVectorShuffleAsZeroOrAnyExtend(DL, MVT::v8i32, V1, V2,
10780                                                          Mask, Subtarget, DAG))
10781     return ZExt;
10782
10783   if (SDValue Blend = lowerVectorShuffleAsBlend(DL, MVT::v8i32, V1, V2, Mask,
10784                                                 Subtarget, DAG))
10785     return Blend;
10786
10787   // Check for being able to broadcast a single element.
10788   if (SDValue Broadcast = lowerVectorShuffleAsBroadcast(DL, MVT::v8i32, V1,
10789                                                         Mask, Subtarget, DAG))
10790     return Broadcast;
10791
10792   // If the shuffle mask is repeated in each 128-bit lane we can use more
10793   // efficient instructions that mirror the shuffles across the two 128-bit
10794   // lanes.
10795   SmallVector<int, 4> RepeatedMask;
10796   if (is128BitLaneRepeatedShuffleMask(MVT::v8i32, Mask, RepeatedMask)) {
10797     assert(RepeatedMask.size() == 4 && "Unexpected repeated mask size!");
10798     if (isSingleInputShuffleMask(Mask))
10799       return DAG.getNode(X86ISD::PSHUFD, DL, MVT::v8i32, V1,
10800                          getV4X86ShuffleImm8ForMask(RepeatedMask, DL, DAG));
10801
10802     // Use dedicated unpack instructions for masks that match their pattern.
10803     if (SDValue V =
10804             lowerVectorShuffleWithUNPCK(DL, MVT::v8i32, Mask, V1, V2, DAG))
10805       return V;
10806   }
10807
10808   // Try to use shift instructions.
10809   if (SDValue Shift =
10810           lowerVectorShuffleAsShift(DL, MVT::v8i32, V1, V2, Mask, DAG))
10811     return Shift;
10812
10813   if (SDValue Rotate = lowerVectorShuffleAsByteRotate(
10814           DL, MVT::v8i32, V1, V2, Mask, Subtarget, DAG))
10815     return Rotate;
10816
10817   // If the shuffle patterns aren't repeated but it is a single input, directly
10818   // generate a cross-lane VPERMD instruction.
10819   if (isSingleInputShuffleMask(Mask)) {
10820     SDValue VPermMask[8];
10821     for (int i = 0; i < 8; ++i)
10822       VPermMask[i] = Mask[i] < 0 ? DAG.getUNDEF(MVT::i32)
10823                                  : DAG.getConstant(Mask[i], DL, MVT::i32);
10824     return DAG.getNode(
10825         X86ISD::VPERMV, DL, MVT::v8i32,
10826         DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v8i32, VPermMask), V1);
10827   }
10828
10829   // Try to simplify this by merging 128-bit lanes to enable a lane-based
10830   // shuffle.
10831   if (SDValue Result = lowerVectorShuffleByMerging128BitLanes(
10832           DL, MVT::v8i32, V1, V2, Mask, Subtarget, DAG))
10833     return Result;
10834
10835   // Otherwise fall back on generic blend lowering.
10836   return lowerVectorShuffleAsDecomposedShuffleBlend(DL, MVT::v8i32, V1, V2,
10837                                                     Mask, DAG);
10838 }
10839
10840 /// \brief Handle lowering of 16-lane 16-bit integer shuffles.
10841 ///
10842 /// This routine is only called when we have AVX2 and thus a reasonable
10843 /// instruction set for v16i16 shuffling..
10844 static SDValue lowerV16I16VectorShuffle(SDValue Op, SDValue V1, SDValue V2,
10845                                         const X86Subtarget *Subtarget,
10846                                         SelectionDAG &DAG) {
10847   SDLoc DL(Op);
10848   assert(V1.getSimpleValueType() == MVT::v16i16 && "Bad operand type!");
10849   assert(V2.getSimpleValueType() == MVT::v16i16 && "Bad operand type!");
10850   ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
10851   ArrayRef<int> Mask = SVOp->getMask();
10852   assert(Mask.size() == 16 && "Unexpected mask size for v16 shuffle!");
10853   assert(Subtarget->hasAVX2() && "We can only lower v16i16 with AVX2!");
10854
10855   // Whenever we can lower this as a zext, that instruction is strictly faster
10856   // than any alternative. It also allows us to fold memory operands into the
10857   // shuffle in many cases.
10858   if (SDValue ZExt = lowerVectorShuffleAsZeroOrAnyExtend(DL, MVT::v16i16, V1, V2,
10859                                                          Mask, Subtarget, DAG))
10860     return ZExt;
10861
10862   // Check for being able to broadcast a single element.
10863   if (SDValue Broadcast = lowerVectorShuffleAsBroadcast(DL, MVT::v16i16, V1,
10864                                                         Mask, Subtarget, DAG))
10865     return Broadcast;
10866
10867   if (SDValue Blend = lowerVectorShuffleAsBlend(DL, MVT::v16i16, V1, V2, Mask,
10868                                                 Subtarget, DAG))
10869     return Blend;
10870
10871   // Use dedicated unpack instructions for masks that match their pattern.
10872   if (SDValue V =
10873           lowerVectorShuffleWithUNPCK(DL, MVT::v16i16, Mask, V1, V2, DAG))
10874     return V;
10875
10876   // Try to use shift instructions.
10877   if (SDValue Shift =
10878           lowerVectorShuffleAsShift(DL, MVT::v16i16, V1, V2, Mask, DAG))
10879     return Shift;
10880
10881   // Try to use byte rotation instructions.
10882   if (SDValue Rotate = lowerVectorShuffleAsByteRotate(
10883           DL, MVT::v16i16, V1, V2, Mask, Subtarget, DAG))
10884     return Rotate;
10885
10886   if (isSingleInputShuffleMask(Mask)) {
10887     // There are no generalized cross-lane shuffle operations available on i16
10888     // element types.
10889     if (is128BitLaneCrossingShuffleMask(MVT::v16i16, Mask))
10890       return lowerVectorShuffleAsLanePermuteAndBlend(DL, MVT::v16i16, V1, V2,
10891                                                      Mask, DAG);
10892
10893     SmallVector<int, 8> RepeatedMask;
10894     if (is128BitLaneRepeatedShuffleMask(MVT::v16i16, Mask, RepeatedMask)) {
10895       // As this is a single-input shuffle, the repeated mask should be
10896       // a strictly valid v8i16 mask that we can pass through to the v8i16
10897       // lowering to handle even the v16 case.
10898       return lowerV8I16GeneralSingleInputVectorShuffle(
10899           DL, MVT::v16i16, V1, RepeatedMask, Subtarget, DAG);
10900     }
10901
10902     SDValue PSHUFBMask[32];
10903     for (int i = 0; i < 16; ++i) {
10904       if (Mask[i] == -1) {
10905         PSHUFBMask[2 * i] = PSHUFBMask[2 * i + 1] = DAG.getUNDEF(MVT::i8);
10906         continue;
10907       }
10908
10909       int M = i < 8 ? Mask[i] : Mask[i] - 8;
10910       assert(M >= 0 && M < 8 && "Invalid single-input mask!");
10911       PSHUFBMask[2 * i] = DAG.getConstant(2 * M, DL, MVT::i8);
10912       PSHUFBMask[2 * i + 1] = DAG.getConstant(2 * M + 1, DL, MVT::i8);
10913     }
10914     return DAG.getBitcast(MVT::v16i16,
10915                           DAG.getNode(X86ISD::PSHUFB, DL, MVT::v32i8,
10916                                       DAG.getBitcast(MVT::v32i8, V1),
10917                                       DAG.getNode(ISD::BUILD_VECTOR, DL,
10918                                                   MVT::v32i8, PSHUFBMask)));
10919   }
10920
10921   // Try to simplify this by merging 128-bit lanes to enable a lane-based
10922   // shuffle.
10923   if (SDValue Result = lowerVectorShuffleByMerging128BitLanes(
10924           DL, MVT::v16i16, V1, V2, Mask, Subtarget, DAG))
10925     return Result;
10926
10927   // Otherwise fall back on generic lowering.
10928   return lowerVectorShuffleAsSplitOrBlend(DL, MVT::v16i16, V1, V2, Mask, DAG);
10929 }
10930
10931 /// \brief Handle lowering of 32-lane 8-bit integer shuffles.
10932 ///
10933 /// This routine is only called when we have AVX2 and thus a reasonable
10934 /// instruction set for v32i8 shuffling..
10935 static SDValue lowerV32I8VectorShuffle(SDValue Op, SDValue V1, SDValue V2,
10936                                        const X86Subtarget *Subtarget,
10937                                        SelectionDAG &DAG) {
10938   SDLoc DL(Op);
10939   assert(V1.getSimpleValueType() == MVT::v32i8 && "Bad operand type!");
10940   assert(V2.getSimpleValueType() == MVT::v32i8 && "Bad operand type!");
10941   ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
10942   ArrayRef<int> Mask = SVOp->getMask();
10943   assert(Mask.size() == 32 && "Unexpected mask size for v32 shuffle!");
10944   assert(Subtarget->hasAVX2() && "We can only lower v32i8 with AVX2!");
10945
10946   // Whenever we can lower this as a zext, that instruction is strictly faster
10947   // than any alternative. It also allows us to fold memory operands into the
10948   // shuffle in many cases.
10949   if (SDValue ZExt = lowerVectorShuffleAsZeroOrAnyExtend(DL, MVT::v32i8, V1, V2,
10950                                                          Mask, Subtarget, DAG))
10951     return ZExt;
10952
10953   // Check for being able to broadcast a single element.
10954   if (SDValue Broadcast = lowerVectorShuffleAsBroadcast(DL, MVT::v32i8, V1,
10955                                                         Mask, Subtarget, DAG))
10956     return Broadcast;
10957
10958   if (SDValue Blend = lowerVectorShuffleAsBlend(DL, MVT::v32i8, V1, V2, Mask,
10959                                                 Subtarget, DAG))
10960     return Blend;
10961
10962   // Use dedicated unpack instructions for masks that match their pattern.
10963   if (SDValue V =
10964           lowerVectorShuffleWithUNPCK(DL, MVT::v32i8, Mask, V1, V2, DAG))
10965     return V;
10966
10967   // Try to use shift instructions.
10968   if (SDValue Shift =
10969           lowerVectorShuffleAsShift(DL, MVT::v32i8, V1, V2, Mask, DAG))
10970     return Shift;
10971
10972   // Try to use byte rotation instructions.
10973   if (SDValue Rotate = lowerVectorShuffleAsByteRotate(
10974           DL, MVT::v32i8, V1, V2, Mask, Subtarget, DAG))
10975     return Rotate;
10976
10977   if (isSingleInputShuffleMask(Mask)) {
10978     // There are no generalized cross-lane shuffle operations available on i8
10979     // element types.
10980     if (is128BitLaneCrossingShuffleMask(MVT::v32i8, Mask))
10981       return lowerVectorShuffleAsLanePermuteAndBlend(DL, MVT::v32i8, V1, V2,
10982                                                      Mask, DAG);
10983
10984     SDValue PSHUFBMask[32];
10985     for (int i = 0; i < 32; ++i)
10986       PSHUFBMask[i] =
10987           Mask[i] < 0
10988               ? DAG.getUNDEF(MVT::i8)
10989               : DAG.getConstant(Mask[i] < 16 ? Mask[i] : Mask[i] - 16, DL,
10990                                 MVT::i8);
10991
10992     return DAG.getNode(
10993         X86ISD::PSHUFB, DL, MVT::v32i8, V1,
10994         DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v32i8, PSHUFBMask));
10995   }
10996
10997   // Try to simplify this by merging 128-bit lanes to enable a lane-based
10998   // shuffle.
10999   if (SDValue Result = lowerVectorShuffleByMerging128BitLanes(
11000           DL, MVT::v32i8, V1, V2, Mask, Subtarget, DAG))
11001     return Result;
11002
11003   // Otherwise fall back on generic lowering.
11004   return lowerVectorShuffleAsSplitOrBlend(DL, MVT::v32i8, V1, V2, Mask, DAG);
11005 }
11006
11007 /// \brief High-level routine to lower various 256-bit x86 vector shuffles.
11008 ///
11009 /// This routine either breaks down the specific type of a 256-bit x86 vector
11010 /// shuffle or splits it into two 128-bit shuffles and fuses the results back
11011 /// together based on the available instructions.
11012 static SDValue lower256BitVectorShuffle(SDValue Op, SDValue V1, SDValue V2,
11013                                         MVT VT, const X86Subtarget *Subtarget,
11014                                         SelectionDAG &DAG) {
11015   SDLoc DL(Op);
11016   ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
11017   ArrayRef<int> Mask = SVOp->getMask();
11018
11019   // If we have a single input to the zero element, insert that into V1 if we
11020   // can do so cheaply.
11021   int NumElts = VT.getVectorNumElements();
11022   int NumV2Elements = std::count_if(Mask.begin(), Mask.end(), [NumElts](int M) {
11023     return M >= NumElts;
11024   });
11025
11026   if (NumV2Elements == 1 && Mask[0] >= NumElts)
11027     if (SDValue Insertion = lowerVectorShuffleAsElementInsertion(
11028                               DL, VT, V1, V2, Mask, Subtarget, DAG))
11029       return Insertion;
11030
11031   // Handle special cases where the lower or upper half is UNDEF.
11032   if (SDValue V =
11033           lowerVectorShuffleWithUndefHalf(DL, VT, V1, V2, Mask, Subtarget, DAG))
11034     return V;
11035
11036   // There is a really nice hard cut-over between AVX1 and AVX2 that means we
11037   // can check for those subtargets here and avoid much of the subtarget
11038   // querying in the per-vector-type lowering routines. With AVX1 we have
11039   // essentially *zero* ability to manipulate a 256-bit vector with integer
11040   // types. Since we'll use floating point types there eventually, just
11041   // immediately cast everything to a float and operate entirely in that domain.
11042   if (VT.isInteger() && !Subtarget->hasAVX2()) {
11043     int ElementBits = VT.getScalarSizeInBits();
11044     if (ElementBits < 32)
11045       // No floating point type available, decompose into 128-bit vectors.
11046       return splitAndLowerVectorShuffle(DL, VT, V1, V2, Mask, DAG);
11047
11048     MVT FpVT = MVT::getVectorVT(MVT::getFloatingPointVT(ElementBits),
11049                                 VT.getVectorNumElements());
11050     V1 = DAG.getBitcast(FpVT, V1);
11051     V2 = DAG.getBitcast(FpVT, V2);
11052     return DAG.getBitcast(VT, DAG.getVectorShuffle(FpVT, DL, V1, V2, Mask));
11053   }
11054
11055   switch (VT.SimpleTy) {
11056   case MVT::v4f64:
11057     return lowerV4F64VectorShuffle(Op, V1, V2, Subtarget, DAG);
11058   case MVT::v4i64:
11059     return lowerV4I64VectorShuffle(Op, V1, V2, Subtarget, DAG);
11060   case MVT::v8f32:
11061     return lowerV8F32VectorShuffle(Op, V1, V2, Subtarget, DAG);
11062   case MVT::v8i32:
11063     return lowerV8I32VectorShuffle(Op, V1, V2, Subtarget, DAG);
11064   case MVT::v16i16:
11065     return lowerV16I16VectorShuffle(Op, V1, V2, Subtarget, DAG);
11066   case MVT::v32i8:
11067     return lowerV32I8VectorShuffle(Op, V1, V2, Subtarget, DAG);
11068
11069   default:
11070     llvm_unreachable("Not a valid 256-bit x86 vector type!");
11071   }
11072 }
11073
11074 /// \brief Try to lower a vector shuffle as a 128-bit shuffles.
11075 static SDValue lowerV4X128VectorShuffle(SDLoc DL, MVT VT,
11076                                         ArrayRef<int> Mask,
11077                                         SDValue V1, SDValue V2,
11078                                         SelectionDAG &DAG) {
11079   assert(VT.getScalarSizeInBits() == 64 &&
11080          "Unexpected element type size for 128bit shuffle.");
11081
11082   // To handle 256 bit vector requires VLX and most probably
11083   // function lowerV2X128VectorShuffle() is better solution.
11084   assert(VT.is512BitVector() && "Unexpected vector size for 128bit shuffle.");
11085
11086   SmallVector<int, 4> WidenedMask;
11087   if (!canWidenShuffleElements(Mask, WidenedMask))
11088     return SDValue();
11089
11090   // Form a 128-bit permutation.
11091   // Convert the 64-bit shuffle mask selection values into 128-bit selection
11092   // bits defined by a vshuf64x2 instruction's immediate control byte.
11093   unsigned PermMask = 0, Imm = 0;
11094   unsigned ControlBitsNum = WidenedMask.size() / 2;
11095
11096   for (int i = 0, Size = WidenedMask.size(); i < Size; ++i) {
11097     if (WidenedMask[i] == SM_SentinelZero)
11098       return SDValue();
11099
11100     // Use first element in place of undef mask.
11101     Imm = (WidenedMask[i] == SM_SentinelUndef) ? 0 : WidenedMask[i];
11102     PermMask |= (Imm % WidenedMask.size()) << (i * ControlBitsNum);
11103   }
11104
11105   return DAG.getNode(X86ISD::SHUF128, DL, VT, V1, V2,
11106                      DAG.getConstant(PermMask, DL, MVT::i8));
11107 }
11108
11109 static SDValue lowerVectorShuffleWithPERMV(SDLoc DL, MVT VT,
11110                                            ArrayRef<int> Mask, SDValue V1,
11111                                            SDValue V2, SelectionDAG &DAG) {
11112
11113   assert(VT.getScalarSizeInBits() >= 16 && "Unexpected data type for PERMV");
11114
11115   MVT MaskEltVT = MVT::getIntegerVT(VT.getScalarSizeInBits());
11116   MVT MaskVecVT = MVT::getVectorVT(MaskEltVT, VT.getVectorNumElements());
11117
11118   SDValue MaskNode = getConstVector(Mask, MaskVecVT, DAG, DL, true);
11119   if (isSingleInputShuffleMask(Mask))
11120     return DAG.getNode(X86ISD::VPERMV, DL, VT, MaskNode, V1);
11121
11122   return DAG.getNode(X86ISD::VPERMV3, DL, VT, V1, MaskNode, V2);
11123 }
11124
11125 /// \brief Handle lowering of 8-lane 64-bit floating point shuffles.
11126 static SDValue lowerV8F64VectorShuffle(SDValue Op, SDValue V1, SDValue V2,
11127                                        const X86Subtarget *Subtarget,
11128                                        SelectionDAG &DAG) {
11129   SDLoc DL(Op);
11130   assert(V1.getSimpleValueType() == MVT::v8f64 && "Bad operand type!");
11131   assert(V2.getSimpleValueType() == MVT::v8f64 && "Bad operand type!");
11132   ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
11133   ArrayRef<int> Mask = SVOp->getMask();
11134   assert(Mask.size() == 8 && "Unexpected mask size for v8 shuffle!");
11135
11136   if (SDValue Shuf128 =
11137           lowerV4X128VectorShuffle(DL, MVT::v8f64, Mask, V1, V2, DAG))
11138     return Shuf128;
11139
11140   if (SDValue Unpck =
11141           lowerVectorShuffleWithUNPCK(DL, MVT::v8f64, Mask, V1, V2, DAG))
11142     return Unpck;
11143
11144   return lowerVectorShuffleWithPERMV(DL, MVT::v8f64, Mask, V1, V2, DAG);
11145 }
11146
11147 /// \brief Handle lowering of 16-lane 32-bit floating point shuffles.
11148 static SDValue lowerV16F32VectorShuffle(SDValue Op, SDValue V1, SDValue V2,
11149                                         const X86Subtarget *Subtarget,
11150                                         SelectionDAG &DAG) {
11151   SDLoc DL(Op);
11152   assert(V1.getSimpleValueType() == MVT::v16f32 && "Bad operand type!");
11153   assert(V2.getSimpleValueType() == MVT::v16f32 && "Bad operand type!");
11154   ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
11155   ArrayRef<int> Mask = SVOp->getMask();
11156   assert(Mask.size() == 16 && "Unexpected mask size for v16 shuffle!");
11157
11158   if (SDValue Unpck =
11159           lowerVectorShuffleWithUNPCK(DL, MVT::v16f32, Mask, V1, V2, DAG))
11160     return Unpck;
11161
11162   return lowerVectorShuffleWithPERMV(DL, MVT::v16f32, Mask, V1, V2, DAG);
11163 }
11164
11165 /// \brief Handle lowering of 8-lane 64-bit integer shuffles.
11166 static SDValue lowerV8I64VectorShuffle(SDValue Op, SDValue V1, SDValue V2,
11167                                        const X86Subtarget *Subtarget,
11168                                        SelectionDAG &DAG) {
11169   SDLoc DL(Op);
11170   assert(V1.getSimpleValueType() == MVT::v8i64 && "Bad operand type!");
11171   assert(V2.getSimpleValueType() == MVT::v8i64 && "Bad operand type!");
11172   ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
11173   ArrayRef<int> Mask = SVOp->getMask();
11174   assert(Mask.size() == 8 && "Unexpected mask size for v8 shuffle!");
11175
11176   if (SDValue Shuf128 =
11177           lowerV4X128VectorShuffle(DL, MVT::v8i64, Mask, V1, V2, DAG))
11178     return Shuf128;
11179
11180   if (SDValue Unpck =
11181           lowerVectorShuffleWithUNPCK(DL, MVT::v8i64, Mask, V1, V2, DAG))
11182     return Unpck;
11183
11184   return lowerVectorShuffleWithPERMV(DL, MVT::v8i64, Mask, V1, V2, DAG);
11185 }
11186
11187 /// \brief Handle lowering of 16-lane 32-bit integer shuffles.
11188 static SDValue lowerV16I32VectorShuffle(SDValue Op, SDValue V1, SDValue V2,
11189                                         const X86Subtarget *Subtarget,
11190                                         SelectionDAG &DAG) {
11191   SDLoc DL(Op);
11192   assert(V1.getSimpleValueType() == MVT::v16i32 && "Bad operand type!");
11193   assert(V2.getSimpleValueType() == MVT::v16i32 && "Bad operand type!");
11194   ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
11195   ArrayRef<int> Mask = SVOp->getMask();
11196   assert(Mask.size() == 16 && "Unexpected mask size for v16 shuffle!");
11197
11198   if (SDValue Unpck =
11199           lowerVectorShuffleWithUNPCK(DL, MVT::v16i32, Mask, V1, V2, DAG))
11200     return Unpck;
11201
11202   return lowerVectorShuffleWithPERMV(DL, MVT::v16i32, Mask, V1, V2, DAG);
11203 }
11204
11205 /// \brief Handle lowering of 32-lane 16-bit integer shuffles.
11206 static SDValue lowerV32I16VectorShuffle(SDValue Op, SDValue V1, SDValue V2,
11207                                         const X86Subtarget *Subtarget,
11208                                         SelectionDAG &DAG) {
11209   SDLoc DL(Op);
11210   assert(V1.getSimpleValueType() == MVT::v32i16 && "Bad operand type!");
11211   assert(V2.getSimpleValueType() == MVT::v32i16 && "Bad operand type!");
11212   ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
11213   ArrayRef<int> Mask = SVOp->getMask();
11214   assert(Mask.size() == 32 && "Unexpected mask size for v32 shuffle!");
11215   assert(Subtarget->hasBWI() && "We can only lower v32i16 with AVX-512-BWI!");
11216
11217   return lowerVectorShuffleWithPERMV(DL, MVT::v32i16, Mask, V1, V2, DAG);
11218 }
11219
11220 /// \brief Handle lowering of 64-lane 8-bit integer shuffles.
11221 static SDValue lowerV64I8VectorShuffle(SDValue Op, SDValue V1, SDValue V2,
11222                                        const X86Subtarget *Subtarget,
11223                                        SelectionDAG &DAG) {
11224   SDLoc DL(Op);
11225   assert(V1.getSimpleValueType() == MVT::v64i8 && "Bad operand type!");
11226   assert(V2.getSimpleValueType() == MVT::v64i8 && "Bad operand type!");
11227   ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
11228   ArrayRef<int> Mask = SVOp->getMask();
11229   assert(Mask.size() == 64 && "Unexpected mask size for v64 shuffle!");
11230   assert(Subtarget->hasBWI() && "We can only lower v64i8 with AVX-512-BWI!");
11231
11232   // FIXME: Implement direct support for this type!
11233   return splitAndLowerVectorShuffle(DL, MVT::v64i8, V1, V2, Mask, DAG);
11234 }
11235
11236 /// \brief High-level routine to lower various 512-bit x86 vector shuffles.
11237 ///
11238 /// This routine either breaks down the specific type of a 512-bit x86 vector
11239 /// shuffle or splits it into two 256-bit shuffles and fuses the results back
11240 /// together based on the available instructions.
11241 static SDValue lower512BitVectorShuffle(SDValue Op, SDValue V1, SDValue V2,
11242                                         MVT VT, const X86Subtarget *Subtarget,
11243                                         SelectionDAG &DAG) {
11244   SDLoc DL(Op);
11245   ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
11246   ArrayRef<int> Mask = SVOp->getMask();
11247   assert(Subtarget->hasAVX512() &&
11248          "Cannot lower 512-bit vectors w/ basic ISA!");
11249
11250   // Check for being able to broadcast a single element.
11251   if (SDValue Broadcast =
11252           lowerVectorShuffleAsBroadcast(DL, VT, V1, Mask, Subtarget, DAG))
11253     return Broadcast;
11254
11255   // Dispatch to each element type for lowering. If we don't have supprot for
11256   // specific element type shuffles at 512 bits, immediately split them and
11257   // lower them. Each lowering routine of a given type is allowed to assume that
11258   // the requisite ISA extensions for that element type are available.
11259   switch (VT.SimpleTy) {
11260   case MVT::v8f64:
11261     return lowerV8F64VectorShuffle(Op, V1, V2, Subtarget, DAG);
11262   case MVT::v16f32:
11263     return lowerV16F32VectorShuffle(Op, V1, V2, Subtarget, DAG);
11264   case MVT::v8i64:
11265     return lowerV8I64VectorShuffle(Op, V1, V2, Subtarget, DAG);
11266   case MVT::v16i32:
11267     return lowerV16I32VectorShuffle(Op, V1, V2, Subtarget, DAG);
11268   case MVT::v32i16:
11269     if (Subtarget->hasBWI())
11270       return lowerV32I16VectorShuffle(Op, V1, V2, Subtarget, DAG);
11271     break;
11272   case MVT::v64i8:
11273     if (Subtarget->hasBWI())
11274       return lowerV64I8VectorShuffle(Op, V1, V2, Subtarget, DAG);
11275     break;
11276
11277   default:
11278     llvm_unreachable("Not a valid 512-bit x86 vector type!");
11279   }
11280
11281   // Otherwise fall back on splitting.
11282   return splitAndLowerVectorShuffle(DL, VT, V1, V2, Mask, DAG);
11283 }
11284
11285 // Lower vXi1 vector shuffles.
11286 // There is no a dedicated instruction on AVX-512 that shuffles the masks.
11287 // The only way to shuffle bits is to sign-extend the mask vector to SIMD
11288 // vector, shuffle and then truncate it back.
11289 static SDValue lower1BitVectorShuffle(SDValue Op, SDValue V1, SDValue V2,
11290                                       MVT VT, const X86Subtarget *Subtarget,
11291                                       SelectionDAG &DAG) {
11292   SDLoc DL(Op);
11293   ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
11294   ArrayRef<int> Mask = SVOp->getMask();
11295   assert(Subtarget->hasAVX512() &&
11296          "Cannot lower 512-bit vectors w/o basic ISA!");
11297   MVT ExtVT;
11298   switch (VT.SimpleTy) {
11299   default:
11300     llvm_unreachable("Expected a vector of i1 elements");
11301   case MVT::v2i1:
11302     ExtVT = MVT::v2i64;
11303     break;
11304   case MVT::v4i1:
11305     ExtVT = MVT::v4i32;
11306     break;
11307   case MVT::v8i1:
11308     ExtVT = MVT::v8i64; // Take 512-bit type, more shuffles on KNL
11309     break;
11310   case MVT::v16i1:
11311     ExtVT = MVT::v16i32;
11312     break;
11313   case MVT::v32i1:
11314     ExtVT = MVT::v32i16;
11315     break;
11316   case MVT::v64i1:
11317     ExtVT = MVT::v64i8;
11318     break;
11319   }
11320
11321   if (ISD::isBuildVectorAllZeros(V1.getNode()))
11322     V1 = getZeroVector(ExtVT, Subtarget, DAG, DL);
11323   else if (ISD::isBuildVectorAllOnes(V1.getNode()))
11324     V1 = getOnesVector(ExtVT, Subtarget, DAG, DL);
11325   else
11326     V1 = DAG.getNode(ISD::SIGN_EXTEND, DL, ExtVT, V1);
11327
11328   if (V2.isUndef())
11329     V2 = DAG.getUNDEF(ExtVT);
11330   else if (ISD::isBuildVectorAllZeros(V2.getNode()))
11331     V2 = getZeroVector(ExtVT, Subtarget, DAG, DL);
11332   else if (ISD::isBuildVectorAllOnes(V2.getNode()))
11333     V2 = getOnesVector(ExtVT, Subtarget, DAG, DL);
11334   else
11335     V2 = DAG.getNode(ISD::SIGN_EXTEND, DL, ExtVT, V2);
11336   return DAG.getNode(ISD::TRUNCATE, DL, VT,
11337                      DAG.getVectorShuffle(ExtVT, DL, V1, V2, Mask));
11338 }
11339 /// \brief Top-level lowering for x86 vector shuffles.
11340 ///
11341 /// This handles decomposition, canonicalization, and lowering of all x86
11342 /// vector shuffles. Most of the specific lowering strategies are encapsulated
11343 /// above in helper routines. The canonicalization attempts to widen shuffles
11344 /// to involve fewer lanes of wider elements, consolidate symmetric patterns
11345 /// s.t. only one of the two inputs needs to be tested, etc.
11346 static SDValue lowerVectorShuffle(SDValue Op, const X86Subtarget *Subtarget,
11347                                   SelectionDAG &DAG) {
11348   ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
11349   ArrayRef<int> Mask = SVOp->getMask();
11350   SDValue V1 = Op.getOperand(0);
11351   SDValue V2 = Op.getOperand(1);
11352   MVT VT = Op.getSimpleValueType();
11353   int NumElements = VT.getVectorNumElements();
11354   SDLoc dl(Op);
11355   bool Is1BitVector = (VT.getVectorElementType() == MVT::i1);
11356
11357   assert((VT.getSizeInBits() != 64 || Is1BitVector) &&
11358          "Can't lower MMX shuffles");
11359
11360   bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
11361   bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
11362   if (V1IsUndef && V2IsUndef)
11363     return DAG.getUNDEF(VT);
11364
11365   // When we create a shuffle node we put the UNDEF node to second operand,
11366   // but in some cases the first operand may be transformed to UNDEF.
11367   // In this case we should just commute the node.
11368   if (V1IsUndef)
11369     return DAG.getCommutedVectorShuffle(*SVOp);
11370
11371   // Check for non-undef masks pointing at an undef vector and make the masks
11372   // undef as well. This makes it easier to match the shuffle based solely on
11373   // the mask.
11374   if (V2IsUndef)
11375     for (int M : Mask)
11376       if (M >= NumElements) {
11377         SmallVector<int, 8> NewMask(Mask.begin(), Mask.end());
11378         for (int &M : NewMask)
11379           if (M >= NumElements)
11380             M = -1;
11381         return DAG.getVectorShuffle(VT, dl, V1, V2, NewMask);
11382       }
11383
11384   // We actually see shuffles that are entirely re-arrangements of a set of
11385   // zero inputs. This mostly happens while decomposing complex shuffles into
11386   // simple ones. Directly lower these as a buildvector of zeros.
11387   SmallBitVector Zeroable = computeZeroableShuffleElements(Mask, V1, V2);
11388   if (Zeroable.all())
11389     return getZeroVector(VT, Subtarget, DAG, dl);
11390
11391   // Try to collapse shuffles into using a vector type with fewer elements but
11392   // wider element types. We cap this to not form integers or floating point
11393   // elements wider than 64 bits, but it might be interesting to form i128
11394   // integers to handle flipping the low and high halves of AVX 256-bit vectors.
11395   SmallVector<int, 16> WidenedMask;
11396   if (VT.getScalarSizeInBits() < 64 && !Is1BitVector &&
11397       canWidenShuffleElements(Mask, WidenedMask)) {
11398     MVT NewEltVT = VT.isFloatingPoint()
11399                        ? MVT::getFloatingPointVT(VT.getScalarSizeInBits() * 2)
11400                        : MVT::getIntegerVT(VT.getScalarSizeInBits() * 2);
11401     MVT NewVT = MVT::getVectorVT(NewEltVT, VT.getVectorNumElements() / 2);
11402     // Make sure that the new vector type is legal. For example, v2f64 isn't
11403     // legal on SSE1.
11404     if (DAG.getTargetLoweringInfo().isTypeLegal(NewVT)) {
11405       V1 = DAG.getBitcast(NewVT, V1);
11406       V2 = DAG.getBitcast(NewVT, V2);
11407       return DAG.getBitcast(
11408           VT, DAG.getVectorShuffle(NewVT, dl, V1, V2, WidenedMask));
11409     }
11410   }
11411
11412   int NumV1Elements = 0, NumUndefElements = 0, NumV2Elements = 0;
11413   for (int M : SVOp->getMask())
11414     if (M < 0)
11415       ++NumUndefElements;
11416     else if (M < NumElements)
11417       ++NumV1Elements;
11418     else
11419       ++NumV2Elements;
11420
11421   // Commute the shuffle as needed such that more elements come from V1 than
11422   // V2. This allows us to match the shuffle pattern strictly on how many
11423   // elements come from V1 without handling the symmetric cases.
11424   if (NumV2Elements > NumV1Elements)
11425     return DAG.getCommutedVectorShuffle(*SVOp);
11426
11427   // When the number of V1 and V2 elements are the same, try to minimize the
11428   // number of uses of V2 in the low half of the vector. When that is tied,
11429   // ensure that the sum of indices for V1 is equal to or lower than the sum
11430   // indices for V2. When those are equal, try to ensure that the number of odd
11431   // indices for V1 is lower than the number of odd indices for V2.
11432   if (NumV1Elements == NumV2Elements) {
11433     int LowV1Elements = 0, LowV2Elements = 0;
11434     for (int M : SVOp->getMask().slice(0, NumElements / 2))
11435       if (M >= NumElements)
11436         ++LowV2Elements;
11437       else if (M >= 0)
11438         ++LowV1Elements;
11439     if (LowV2Elements > LowV1Elements) {
11440       return DAG.getCommutedVectorShuffle(*SVOp);
11441     } else if (LowV2Elements == LowV1Elements) {
11442       int SumV1Indices = 0, SumV2Indices = 0;
11443       for (int i = 0, Size = SVOp->getMask().size(); i < Size; ++i)
11444         if (SVOp->getMask()[i] >= NumElements)
11445           SumV2Indices += i;
11446         else if (SVOp->getMask()[i] >= 0)
11447           SumV1Indices += i;
11448       if (SumV2Indices < SumV1Indices) {
11449         return DAG.getCommutedVectorShuffle(*SVOp);
11450       } else if (SumV2Indices == SumV1Indices) {
11451         int NumV1OddIndices = 0, NumV2OddIndices = 0;
11452         for (int i = 0, Size = SVOp->getMask().size(); i < Size; ++i)
11453           if (SVOp->getMask()[i] >= NumElements)
11454             NumV2OddIndices += i % 2;
11455           else if (SVOp->getMask()[i] >= 0)
11456             NumV1OddIndices += i % 2;
11457         if (NumV2OddIndices < NumV1OddIndices)
11458           return DAG.getCommutedVectorShuffle(*SVOp);
11459       }
11460     }
11461   }
11462
11463   // For each vector width, delegate to a specialized lowering routine.
11464   if (VT.is128BitVector())
11465     return lower128BitVectorShuffle(Op, V1, V2, VT, Subtarget, DAG);
11466
11467   if (VT.is256BitVector())
11468     return lower256BitVectorShuffle(Op, V1, V2, VT, Subtarget, DAG);
11469
11470   if (VT.is512BitVector())
11471     return lower512BitVectorShuffle(Op, V1, V2, VT, Subtarget, DAG);
11472
11473   if (Is1BitVector)
11474     return lower1BitVectorShuffle(Op, V1, V2, VT, Subtarget, DAG);
11475   llvm_unreachable("Unimplemented!");
11476 }
11477
11478 // This function assumes its argument is a BUILD_VECTOR of constants or
11479 // undef SDNodes. i.e: ISD::isBuildVectorOfConstantSDNodes(BuildVector) is
11480 // true.
11481 static bool BUILD_VECTORtoBlendMask(BuildVectorSDNode *BuildVector,
11482                                     unsigned &MaskValue) {
11483   MaskValue = 0;
11484   unsigned NumElems = BuildVector->getNumOperands();
11485
11486   // There are 2 lanes if (NumElems > 8), and 1 lane otherwise.
11487   // We don't handle the >2 lanes case right now.
11488   unsigned NumLanes = (NumElems - 1) / 8 + 1;
11489   if (NumLanes > 2)
11490     return false;
11491
11492   unsigned NumElemsInLane = NumElems / NumLanes;
11493
11494   // Blend for v16i16 should be symmetric for the both lanes.
11495   for (unsigned i = 0; i < NumElemsInLane; ++i) {
11496     SDValue EltCond = BuildVector->getOperand(i);
11497     SDValue SndLaneEltCond =
11498         (NumLanes == 2) ? BuildVector->getOperand(i + NumElemsInLane) : EltCond;
11499
11500     int Lane1Cond = -1, Lane2Cond = -1;
11501     if (isa<ConstantSDNode>(EltCond))
11502       Lane1Cond = !isNullConstant(EltCond);
11503     if (isa<ConstantSDNode>(SndLaneEltCond))
11504       Lane2Cond = !isNullConstant(SndLaneEltCond);
11505
11506     unsigned LaneMask = 0;
11507     if (Lane1Cond == Lane2Cond || Lane2Cond < 0)
11508       // Lane1Cond != 0, means we want the first argument.
11509       // Lane1Cond == 0, means we want the second argument.
11510       // The encoding of this argument is 0 for the first argument, 1
11511       // for the second. Therefore, invert the condition.
11512       LaneMask = !Lane1Cond << i;
11513     else if (Lane1Cond < 0)
11514       LaneMask = !Lane2Cond << i;
11515     else
11516       return false;
11517
11518     MaskValue |= LaneMask;
11519     if (NumLanes == 2)
11520       MaskValue |= LaneMask << NumElemsInLane;
11521   }
11522   return true;
11523 }
11524
11525 /// \brief Try to lower a VSELECT instruction to a vector shuffle.
11526 static SDValue lowerVSELECTtoVectorShuffle(SDValue Op,
11527                                            const X86Subtarget *Subtarget,
11528                                            SelectionDAG &DAG) {
11529   SDValue Cond = Op.getOperand(0);
11530   SDValue LHS = Op.getOperand(1);
11531   SDValue RHS = Op.getOperand(2);
11532   SDLoc dl(Op);
11533   MVT VT = Op.getSimpleValueType();
11534
11535   if (!ISD::isBuildVectorOfConstantSDNodes(Cond.getNode()))
11536     return SDValue();
11537   auto *CondBV = cast<BuildVectorSDNode>(Cond);
11538
11539   // Only non-legal VSELECTs reach this lowering, convert those into generic
11540   // shuffles and re-use the shuffle lowering path for blends.
11541   SmallVector<int, 32> Mask;
11542   for (int i = 0, Size = VT.getVectorNumElements(); i < Size; ++i) {
11543     SDValue CondElt = CondBV->getOperand(i);
11544     Mask.push_back(
11545         isa<ConstantSDNode>(CondElt) ? i + (isNullConstant(CondElt) ? Size : 0)
11546                                      : -1);
11547   }
11548   return DAG.getVectorShuffle(VT, dl, LHS, RHS, Mask);
11549 }
11550
11551 SDValue X86TargetLowering::LowerVSELECT(SDValue Op, SelectionDAG &DAG) const {
11552   // A vselect where all conditions and data are constants can be optimized into
11553   // a single vector load by SelectionDAGLegalize::ExpandBUILD_VECTOR().
11554   if (ISD::isBuildVectorOfConstantSDNodes(Op.getOperand(0).getNode()) &&
11555       ISD::isBuildVectorOfConstantSDNodes(Op.getOperand(1).getNode()) &&
11556       ISD::isBuildVectorOfConstantSDNodes(Op.getOperand(2).getNode()))
11557     return SDValue();
11558
11559   // Try to lower this to a blend-style vector shuffle. This can handle all
11560   // constant condition cases.
11561   if (SDValue BlendOp = lowerVSELECTtoVectorShuffle(Op, Subtarget, DAG))
11562     return BlendOp;
11563
11564   // Variable blends are only legal from SSE4.1 onward.
11565   if (!Subtarget->hasSSE41())
11566     return SDValue();
11567
11568   // Only some types will be legal on some subtargets. If we can emit a legal
11569   // VSELECT-matching blend, return Op, and but if we need to expand, return
11570   // a null value.
11571   switch (Op.getSimpleValueType().SimpleTy) {
11572   default:
11573     // Most of the vector types have blends past SSE4.1.
11574     return Op;
11575
11576   case MVT::v32i8:
11577     // The byte blends for AVX vectors were introduced only in AVX2.
11578     if (Subtarget->hasAVX2())
11579       return Op;
11580
11581     return SDValue();
11582
11583   case MVT::v8i16:
11584   case MVT::v16i16:
11585     // AVX-512 BWI and VLX features support VSELECT with i16 elements.
11586     if (Subtarget->hasBWI() && Subtarget->hasVLX())
11587       return Op;
11588
11589     // FIXME: We should custom lower this by fixing the condition and using i8
11590     // blends.
11591     return SDValue();
11592   }
11593 }
11594
11595 static SDValue LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG) {
11596   MVT VT = Op.getSimpleValueType();
11597   SDLoc dl(Op);
11598
11599   if (!Op.getOperand(0).getSimpleValueType().is128BitVector())
11600     return SDValue();
11601
11602   if (VT.getSizeInBits() == 8) {
11603     SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
11604                                   Op.getOperand(0), Op.getOperand(1));
11605     SDValue Assert  = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
11606                                   DAG.getValueType(VT));
11607     return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
11608   }
11609
11610   if (VT.getSizeInBits() == 16) {
11611     // If Idx is 0, it's cheaper to do a move instead of a pextrw.
11612     if (isNullConstant(Op.getOperand(1)))
11613       return DAG.getNode(
11614           ISD::TRUNCATE, dl, MVT::i16,
11615           DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
11616                       DAG.getBitcast(MVT::v4i32, Op.getOperand(0)),
11617                       Op.getOperand(1)));
11618     SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
11619                                   Op.getOperand(0), Op.getOperand(1));
11620     SDValue Assert  = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
11621                                   DAG.getValueType(VT));
11622     return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
11623   }
11624
11625   if (VT == MVT::f32) {
11626     // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
11627     // the result back to FR32 register. It's only worth matching if the
11628     // result has a single use which is a store or a bitcast to i32.  And in
11629     // the case of a store, it's not worth it if the index is a constant 0,
11630     // because a MOVSSmr can be used instead, which is smaller and faster.
11631     if (!Op.hasOneUse())
11632       return SDValue();
11633     SDNode *User = *Op.getNode()->use_begin();
11634     if ((User->getOpcode() != ISD::STORE ||
11635          isNullConstant(Op.getOperand(1))) &&
11636         (User->getOpcode() != ISD::BITCAST ||
11637          User->getValueType(0) != MVT::i32))
11638       return SDValue();
11639     SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
11640                                   DAG.getBitcast(MVT::v4i32, Op.getOperand(0)),
11641                                   Op.getOperand(1));
11642     return DAG.getBitcast(MVT::f32, Extract);
11643   }
11644
11645   if (VT == MVT::i32 || VT == MVT::i64) {
11646     // ExtractPS/pextrq works with constant index.
11647     if (isa<ConstantSDNode>(Op.getOperand(1)))
11648       return Op;
11649   }
11650   return SDValue();
11651 }
11652
11653 /// Extract one bit from mask vector, like v16i1 or v8i1.
11654 /// AVX-512 feature.
11655 SDValue
11656 X86TargetLowering::ExtractBitFromMaskVector(SDValue Op, SelectionDAG &DAG) const {
11657   SDValue Vec = Op.getOperand(0);
11658   SDLoc dl(Vec);
11659   MVT VecVT = Vec.getSimpleValueType();
11660   SDValue Idx = Op.getOperand(1);
11661   MVT EltVT = Op.getSimpleValueType();
11662
11663   assert((EltVT == MVT::i1) && "Unexpected operands in ExtractBitFromMaskVector");
11664   assert((VecVT.getVectorNumElements() <= 16 || Subtarget->hasBWI()) &&
11665          "Unexpected vector type in ExtractBitFromMaskVector");
11666
11667   // variable index can't be handled in mask registers,
11668   // extend vector to VR512
11669   if (!isa<ConstantSDNode>(Idx)) {
11670     MVT ExtVT = (VecVT == MVT::v8i1 ?  MVT::v8i64 : MVT::v16i32);
11671     SDValue Ext = DAG.getNode(ISD::ZERO_EXTEND, dl, ExtVT, Vec);
11672     SDValue Elt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
11673                               ExtVT.getVectorElementType(), Ext, Idx);
11674     return DAG.getNode(ISD::TRUNCATE, dl, EltVT, Elt);
11675   }
11676
11677   unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
11678   const TargetRegisterClass* rc = getRegClassFor(VecVT);
11679   if (!Subtarget->hasDQI() && (VecVT.getVectorNumElements() <= 8))
11680     rc = getRegClassFor(MVT::v16i1);
11681   unsigned MaxSift = rc->getSize()*8 - 1;
11682   Vec = DAG.getNode(X86ISD::VSHLI, dl, VecVT, Vec,
11683                     DAG.getConstant(MaxSift - IdxVal, dl, MVT::i8));
11684   Vec = DAG.getNode(X86ISD::VSRLI, dl, VecVT, Vec,
11685                     DAG.getConstant(MaxSift, dl, MVT::i8));
11686   return DAG.getNode(X86ISD::VEXTRACT, dl, MVT::i1, Vec,
11687                        DAG.getIntPtrConstant(0, dl));
11688 }
11689
11690 SDValue
11691 X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op,
11692                                            SelectionDAG &DAG) const {
11693   SDLoc dl(Op);
11694   SDValue Vec = Op.getOperand(0);
11695   MVT VecVT = Vec.getSimpleValueType();
11696   SDValue Idx = Op.getOperand(1);
11697
11698   if (Op.getSimpleValueType() == MVT::i1)
11699     return ExtractBitFromMaskVector(Op, DAG);
11700
11701   if (!isa<ConstantSDNode>(Idx)) {
11702     if (VecVT.is512BitVector() ||
11703         (VecVT.is256BitVector() && Subtarget->hasInt256() &&
11704          VecVT.getVectorElementType().getSizeInBits() == 32)) {
11705
11706       MVT MaskEltVT =
11707         MVT::getIntegerVT(VecVT.getVectorElementType().getSizeInBits());
11708       MVT MaskVT = MVT::getVectorVT(MaskEltVT, VecVT.getSizeInBits() /
11709                                     MaskEltVT.getSizeInBits());
11710
11711       Idx = DAG.getZExtOrTrunc(Idx, dl, MaskEltVT);
11712       auto PtrVT = getPointerTy(DAG.getDataLayout());
11713       SDValue Mask = DAG.getNode(X86ISD::VINSERT, dl, MaskVT,
11714                                  getZeroVector(MaskVT, Subtarget, DAG, dl), Idx,
11715                                  DAG.getConstant(0, dl, PtrVT));
11716       SDValue Perm = DAG.getNode(X86ISD::VPERMV, dl, VecVT, Mask, Vec);
11717       return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Perm,
11718                          DAG.getConstant(0, dl, PtrVT));
11719     }
11720     return SDValue();
11721   }
11722
11723   // If this is a 256-bit vector result, first extract the 128-bit vector and
11724   // then extract the element from the 128-bit vector.
11725   if (VecVT.is256BitVector() || VecVT.is512BitVector()) {
11726
11727     unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
11728     // Get the 128-bit vector.
11729     Vec = Extract128BitVector(Vec, IdxVal, DAG, dl);
11730     MVT EltVT = VecVT.getVectorElementType();
11731
11732     unsigned ElemsPerChunk = 128 / EltVT.getSizeInBits();
11733     assert(isPowerOf2_32(ElemsPerChunk) && "Elements per chunk not power of 2");
11734
11735     // Find IdxVal modulo ElemsPerChunk. Since ElemsPerChunk is a power of 2
11736     // this can be done with a mask.
11737     IdxVal &= ElemsPerChunk - 1;
11738     return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, Op.getValueType(), Vec,
11739                        DAG.getConstant(IdxVal, dl, MVT::i32));
11740   }
11741
11742   assert(VecVT.is128BitVector() && "Unexpected vector length");
11743
11744   if (Subtarget->hasSSE41())
11745     if (SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG))
11746       return Res;
11747
11748   MVT VT = Op.getSimpleValueType();
11749   // TODO: handle v16i8.
11750   if (VT.getSizeInBits() == 16) {
11751     SDValue Vec = Op.getOperand(0);
11752     if (isNullConstant(Op.getOperand(1)))
11753       return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
11754                          DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
11755                                      DAG.getBitcast(MVT::v4i32, Vec),
11756                                      Op.getOperand(1)));
11757     // Transform it so it match pextrw which produces a 32-bit result.
11758     MVT EltVT = MVT::i32;
11759     SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EltVT,
11760                                   Op.getOperand(0), Op.getOperand(1));
11761     SDValue Assert  = DAG.getNode(ISD::AssertZext, dl, EltVT, Extract,
11762                                   DAG.getValueType(VT));
11763     return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
11764   }
11765
11766   if (VT.getSizeInBits() == 32) {
11767     unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
11768     if (Idx == 0)
11769       return Op;
11770
11771     // SHUFPS the element to the lowest double word, then movss.
11772     int Mask[4] = { static_cast<int>(Idx), -1, -1, -1 };
11773     MVT VVT = Op.getOperand(0).getSimpleValueType();
11774     SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
11775                                        DAG.getUNDEF(VVT), Mask);
11776     return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
11777                        DAG.getIntPtrConstant(0, dl));
11778   }
11779
11780   if (VT.getSizeInBits() == 64) {
11781     // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
11782     // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
11783     //        to match extract_elt for f64.
11784     if (isNullConstant(Op.getOperand(1)))
11785       return Op;
11786
11787     // UNPCKHPD the element to the lowest double word, then movsd.
11788     // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
11789     // to a f64mem, the whole operation is folded into a single MOVHPDmr.
11790     int Mask[2] = { 1, -1 };
11791     MVT VVT = Op.getOperand(0).getSimpleValueType();
11792     SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
11793                                        DAG.getUNDEF(VVT), Mask);
11794     return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
11795                        DAG.getIntPtrConstant(0, dl));
11796   }
11797
11798   return SDValue();
11799 }
11800
11801 /// Insert one bit to mask vector, like v16i1 or v8i1.
11802 /// AVX-512 feature.
11803 SDValue
11804 X86TargetLowering::InsertBitToMaskVector(SDValue Op, SelectionDAG &DAG) const {
11805   SDLoc dl(Op);
11806   SDValue Vec = Op.getOperand(0);
11807   SDValue Elt = Op.getOperand(1);
11808   SDValue Idx = Op.getOperand(2);
11809   MVT VecVT = Vec.getSimpleValueType();
11810
11811   if (!isa<ConstantSDNode>(Idx)) {
11812     // Non constant index. Extend source and destination,
11813     // insert element and then truncate the result.
11814     MVT ExtVecVT = (VecVT == MVT::v8i1 ?  MVT::v8i64 : MVT::v16i32);
11815     MVT ExtEltVT = (VecVT == MVT::v8i1 ?  MVT::i64 : MVT::i32);
11816     SDValue ExtOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, ExtVecVT,
11817       DAG.getNode(ISD::ZERO_EXTEND, dl, ExtVecVT, Vec),
11818       DAG.getNode(ISD::ZERO_EXTEND, dl, ExtEltVT, Elt), Idx);
11819     return DAG.getNode(ISD::TRUNCATE, dl, VecVT, ExtOp);
11820   }
11821
11822   unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
11823   SDValue EltInVec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Elt);
11824   if (IdxVal)
11825     EltInVec = DAG.getNode(X86ISD::VSHLI, dl, VecVT, EltInVec,
11826                            DAG.getConstant(IdxVal, dl, MVT::i8));
11827   if (Vec.getOpcode() == ISD::UNDEF)
11828     return EltInVec;
11829   return DAG.getNode(ISD::OR, dl, VecVT, Vec, EltInVec);
11830 }
11831
11832 SDValue X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op,
11833                                                   SelectionDAG &DAG) const {
11834   MVT VT = Op.getSimpleValueType();
11835   MVT EltVT = VT.getVectorElementType();
11836
11837   if (EltVT == MVT::i1)
11838     return InsertBitToMaskVector(Op, DAG);
11839
11840   SDLoc dl(Op);
11841   SDValue N0 = Op.getOperand(0);
11842   SDValue N1 = Op.getOperand(1);
11843   SDValue N2 = Op.getOperand(2);
11844   if (!isa<ConstantSDNode>(N2))
11845     return SDValue();
11846   auto *N2C = cast<ConstantSDNode>(N2);
11847   unsigned IdxVal = N2C->getZExtValue();
11848
11849   // If the vector is wider than 128 bits, extract the 128-bit subvector, insert
11850   // into that, and then insert the subvector back into the result.
11851   if (VT.is256BitVector() || VT.is512BitVector()) {
11852     // With a 256-bit vector, we can insert into the zero element efficiently
11853     // using a blend if we have AVX or AVX2 and the right data type.
11854     if (VT.is256BitVector() && IdxVal == 0) {
11855       // TODO: It is worthwhile to cast integer to floating point and back
11856       // and incur a domain crossing penalty if that's what we'll end up
11857       // doing anyway after extracting to a 128-bit vector.
11858       if ((Subtarget->hasAVX() && (EltVT == MVT::f64 || EltVT == MVT::f32)) ||
11859           (Subtarget->hasAVX2() && EltVT == MVT::i32)) {
11860         SDValue N1Vec = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, N1);
11861         N2 = DAG.getIntPtrConstant(1, dl);
11862         return DAG.getNode(X86ISD::BLENDI, dl, VT, N0, N1Vec, N2);
11863       }
11864     }
11865
11866     // Get the desired 128-bit vector chunk.
11867     SDValue V = Extract128BitVector(N0, IdxVal, DAG, dl);
11868
11869     // Insert the element into the desired chunk.
11870     unsigned NumEltsIn128 = 128 / EltVT.getSizeInBits();
11871     assert(isPowerOf2_32(NumEltsIn128));
11872     // Since NumEltsIn128 is a power of 2 we can use mask instead of modulo.
11873     unsigned IdxIn128 = IdxVal & (NumEltsIn128 - 1);
11874
11875     V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, V.getValueType(), V, N1,
11876                     DAG.getConstant(IdxIn128, dl, MVT::i32));
11877
11878     // Insert the changed part back into the bigger vector
11879     return Insert128BitVector(N0, V, IdxVal, DAG, dl);
11880   }
11881   assert(VT.is128BitVector() && "Only 128-bit vector types should be left!");
11882
11883   if (Subtarget->hasSSE41()) {
11884     if (EltVT.getSizeInBits() == 8 || EltVT.getSizeInBits() == 16) {
11885       unsigned Opc;
11886       if (VT == MVT::v8i16) {
11887         Opc = X86ISD::PINSRW;
11888       } else {
11889         assert(VT == MVT::v16i8);
11890         Opc = X86ISD::PINSRB;
11891       }
11892
11893       // Transform it so it match pinsr{b,w} which expects a GR32 as its second
11894       // argument.
11895       if (N1.getValueType() != MVT::i32)
11896         N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
11897       if (N2.getValueType() != MVT::i32)
11898         N2 = DAG.getIntPtrConstant(IdxVal, dl);
11899       return DAG.getNode(Opc, dl, VT, N0, N1, N2);
11900     }
11901
11902     if (EltVT == MVT::f32) {
11903       // Bits [7:6] of the constant are the source select. This will always be
11904       //   zero here. The DAG Combiner may combine an extract_elt index into
11905       //   these bits. For example (insert (extract, 3), 2) could be matched by
11906       //   putting the '3' into bits [7:6] of X86ISD::INSERTPS.
11907       // Bits [5:4] of the constant are the destination select. This is the
11908       //   value of the incoming immediate.
11909       // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
11910       //   combine either bitwise AND or insert of float 0.0 to set these bits.
11911
11912       bool MinSize = DAG.getMachineFunction().getFunction()->optForMinSize();
11913       if (IdxVal == 0 && (!MinSize || !MayFoldLoad(N1))) {
11914         // If this is an insertion of 32-bits into the low 32-bits of
11915         // a vector, we prefer to generate a blend with immediate rather
11916         // than an insertps. Blends are simpler operations in hardware and so
11917         // will always have equal or better performance than insertps.
11918         // But if optimizing for size and there's a load folding opportunity,
11919         // generate insertps because blendps does not have a 32-bit memory
11920         // operand form.
11921         N2 = DAG.getIntPtrConstant(1, dl);
11922         N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
11923         return DAG.getNode(X86ISD::BLENDI, dl, VT, N0, N1, N2);
11924       }
11925       N2 = DAG.getIntPtrConstant(IdxVal << 4, dl);
11926       // Create this as a scalar to vector..
11927       N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
11928       return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
11929     }
11930
11931     if (EltVT == MVT::i32 || EltVT == MVT::i64) {
11932       // PINSR* works with constant index.
11933       return Op;
11934     }
11935   }
11936
11937   if (EltVT == MVT::i8)
11938     return SDValue();
11939
11940   if (EltVT.getSizeInBits() == 16) {
11941     // Transform it so it match pinsrw which expects a 16-bit value in a GR32
11942     // as its second argument.
11943     if (N1.getValueType() != MVT::i32)
11944       N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
11945     if (N2.getValueType() != MVT::i32)
11946       N2 = DAG.getIntPtrConstant(IdxVal, dl);
11947     return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
11948   }
11949   return SDValue();
11950 }
11951
11952 static SDValue LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
11953   SDLoc dl(Op);
11954   MVT OpVT = Op.getSimpleValueType();
11955
11956   // If this is a 256-bit vector result, first insert into a 128-bit
11957   // vector and then insert into the 256-bit vector.
11958   if (!OpVT.is128BitVector()) {
11959     // Insert into a 128-bit vector.
11960     unsigned SizeFactor = OpVT.getSizeInBits()/128;
11961     MVT VT128 = MVT::getVectorVT(OpVT.getVectorElementType(),
11962                                  OpVT.getVectorNumElements() / SizeFactor);
11963
11964     Op = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT128, Op.getOperand(0));
11965
11966     // Insert the 128-bit vector.
11967     return Insert128BitVector(DAG.getUNDEF(OpVT), Op, 0, DAG, dl);
11968   }
11969
11970   if (OpVT == MVT::v1i64 &&
11971       Op.getOperand(0).getValueType() == MVT::i64)
11972     return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
11973
11974   SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
11975   assert(OpVT.is128BitVector() && "Expected an SSE type!");
11976   return DAG.getBitcast(
11977       OpVT, DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32, AnyExt));
11978 }
11979
11980 // Lower a node with an EXTRACT_SUBVECTOR opcode.  This may result in
11981 // a simple subregister reference or explicit instructions to grab
11982 // upper bits of a vector.
11983 static SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
11984                                       SelectionDAG &DAG) {
11985   SDLoc dl(Op);
11986   SDValue In =  Op.getOperand(0);
11987   SDValue Idx = Op.getOperand(1);
11988   unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
11989   MVT ResVT   = Op.getSimpleValueType();
11990   MVT InVT    = In.getSimpleValueType();
11991
11992   if (Subtarget->hasFp256()) {
11993     if (ResVT.is128BitVector() &&
11994         (InVT.is256BitVector() || InVT.is512BitVector()) &&
11995         isa<ConstantSDNode>(Idx)) {
11996       return Extract128BitVector(In, IdxVal, DAG, dl);
11997     }
11998     if (ResVT.is256BitVector() && InVT.is512BitVector() &&
11999         isa<ConstantSDNode>(Idx)) {
12000       return Extract256BitVector(In, IdxVal, DAG, dl);
12001     }
12002   }
12003   return SDValue();
12004 }
12005
12006 // Lower a node with an INSERT_SUBVECTOR opcode.  This may result in a
12007 // simple superregister reference or explicit instructions to insert
12008 // the upper bits of a vector.
12009 static SDValue LowerINSERT_SUBVECTOR(SDValue Op, const X86Subtarget *Subtarget,
12010                                      SelectionDAG &DAG) {
12011   if (!Subtarget->hasAVX())
12012     return SDValue();
12013
12014   SDLoc dl(Op);
12015   SDValue Vec = Op.getOperand(0);
12016   SDValue SubVec = Op.getOperand(1);
12017   SDValue Idx = Op.getOperand(2);
12018
12019   if (!isa<ConstantSDNode>(Idx))
12020     return SDValue();
12021
12022   unsigned IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
12023   MVT OpVT = Op.getSimpleValueType();
12024   MVT SubVecVT = SubVec.getSimpleValueType();
12025
12026   // Fold two 16-byte subvector loads into one 32-byte load:
12027   // (insert_subvector (insert_subvector undef, (load addr), 0),
12028   //                   (load addr + 16), Elts/2)
12029   // --> load32 addr
12030   if ((IdxVal == OpVT.getVectorNumElements() / 2) &&
12031       Vec.getOpcode() == ISD::INSERT_SUBVECTOR &&
12032       OpVT.is256BitVector() && SubVecVT.is128BitVector()) {
12033     auto *Idx2 = dyn_cast<ConstantSDNode>(Vec.getOperand(2));
12034     if (Idx2 && Idx2->getZExtValue() == 0) {
12035       SDValue SubVec2 = Vec.getOperand(1);
12036       // If needed, look through a bitcast to get to the load.
12037       if (SubVec2.getNode() && SubVec2.getOpcode() == ISD::BITCAST)
12038         SubVec2 = SubVec2.getOperand(0);
12039
12040       if (auto *FirstLd = dyn_cast<LoadSDNode>(SubVec2)) {
12041         bool Fast;
12042         unsigned Alignment = FirstLd->getAlignment();
12043         unsigned AS = FirstLd->getAddressSpace();
12044         const X86TargetLowering *TLI = Subtarget->getTargetLowering();
12045         if (TLI->allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(),
12046                                     OpVT, AS, Alignment, &Fast) && Fast) {
12047           SDValue Ops[] = { SubVec2, SubVec };
12048           if (SDValue Ld = EltsFromConsecutiveLoads(OpVT, Ops, dl, DAG, false))
12049             return Ld;
12050         }
12051       }
12052     }
12053   }
12054
12055   if ((OpVT.is256BitVector() || OpVT.is512BitVector()) &&
12056       SubVecVT.is128BitVector())
12057     return Insert128BitVector(Vec, SubVec, IdxVal, DAG, dl);
12058
12059   if (OpVT.is512BitVector() && SubVecVT.is256BitVector())
12060     return Insert256BitVector(Vec, SubVec, IdxVal, DAG, dl);
12061
12062   if (OpVT.getVectorElementType() == MVT::i1)
12063     return Insert1BitVector(Op, DAG);
12064
12065   return SDValue();
12066 }
12067
12068 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
12069 // their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
12070 // one of the above mentioned nodes. It has to be wrapped because otherwise
12071 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
12072 // be used to form addressing mode. These wrapped nodes will be selected
12073 // into MOV32ri.
12074 SDValue
12075 X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) const {
12076   ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
12077
12078   // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
12079   // global base reg.
12080   unsigned char OpFlag = 0;
12081   unsigned WrapperKind = X86ISD::Wrapper;
12082   CodeModel::Model M = DAG.getTarget().getCodeModel();
12083
12084   if (Subtarget->isPICStyleRIPRel() &&
12085       (M == CodeModel::Small || M == CodeModel::Kernel))
12086     WrapperKind = X86ISD::WrapperRIP;
12087   else if (Subtarget->isPICStyleGOT())
12088     OpFlag = X86II::MO_GOTOFF;
12089   else if (Subtarget->isPICStyleStubPIC())
12090     OpFlag = X86II::MO_PIC_BASE_OFFSET;
12091
12092   auto PtrVT = getPointerTy(DAG.getDataLayout());
12093   SDValue Result = DAG.getTargetConstantPool(
12094       CP->getConstVal(), PtrVT, CP->getAlignment(), CP->getOffset(), OpFlag);
12095   SDLoc DL(CP);
12096   Result = DAG.getNode(WrapperKind, DL, PtrVT, Result);
12097   // With PIC, the address is actually $g + Offset.
12098   if (OpFlag) {
12099     Result =
12100         DAG.getNode(ISD::ADD, DL, PtrVT,
12101                     DAG.getNode(X86ISD::GlobalBaseReg, SDLoc(), PtrVT), Result);
12102   }
12103
12104   return Result;
12105 }
12106
12107 SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) const {
12108   JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
12109
12110   // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
12111   // global base reg.
12112   unsigned char OpFlag = 0;
12113   unsigned WrapperKind = X86ISD::Wrapper;
12114   CodeModel::Model M = DAG.getTarget().getCodeModel();
12115
12116   if (Subtarget->isPICStyleRIPRel() &&
12117       (M == CodeModel::Small || M == CodeModel::Kernel))
12118     WrapperKind = X86ISD::WrapperRIP;
12119   else if (Subtarget->isPICStyleGOT())
12120     OpFlag = X86II::MO_GOTOFF;
12121   else if (Subtarget->isPICStyleStubPIC())
12122     OpFlag = X86II::MO_PIC_BASE_OFFSET;
12123
12124   auto PtrVT = getPointerTy(DAG.getDataLayout());
12125   SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT, OpFlag);
12126   SDLoc DL(JT);
12127   Result = DAG.getNode(WrapperKind, DL, PtrVT, Result);
12128
12129   // With PIC, the address is actually $g + Offset.
12130   if (OpFlag)
12131     Result =
12132         DAG.getNode(ISD::ADD, DL, PtrVT,
12133                     DAG.getNode(X86ISD::GlobalBaseReg, SDLoc(), PtrVT), Result);
12134
12135   return Result;
12136 }
12137
12138 SDValue
12139 X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) const {
12140   const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
12141
12142   // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
12143   // global base reg.
12144   unsigned char OpFlag = 0;
12145   unsigned WrapperKind = X86ISD::Wrapper;
12146   CodeModel::Model M = DAG.getTarget().getCodeModel();
12147
12148   if (Subtarget->isPICStyleRIPRel() &&
12149       (M == CodeModel::Small || M == CodeModel::Kernel)) {
12150     if (Subtarget->isTargetDarwin() || Subtarget->isTargetELF())
12151       OpFlag = X86II::MO_GOTPCREL;
12152     WrapperKind = X86ISD::WrapperRIP;
12153   } else if (Subtarget->isPICStyleGOT()) {
12154     OpFlag = X86II::MO_GOT;
12155   } else if (Subtarget->isPICStyleStubPIC()) {
12156     OpFlag = X86II::MO_DARWIN_NONLAZY_PIC_BASE;
12157   } else if (Subtarget->isPICStyleStubNoDynamic()) {
12158     OpFlag = X86II::MO_DARWIN_NONLAZY;
12159   }
12160
12161   auto PtrVT = getPointerTy(DAG.getDataLayout());
12162   SDValue Result = DAG.getTargetExternalSymbol(Sym, PtrVT, OpFlag);
12163
12164   SDLoc DL(Op);
12165   Result = DAG.getNode(WrapperKind, DL, PtrVT, Result);
12166
12167   // With PIC, the address is actually $g + Offset.
12168   if (DAG.getTarget().getRelocationModel() == Reloc::PIC_ &&
12169       !Subtarget->is64Bit()) {
12170     Result =
12171         DAG.getNode(ISD::ADD, DL, PtrVT,
12172                     DAG.getNode(X86ISD::GlobalBaseReg, SDLoc(), PtrVT), Result);
12173   }
12174
12175   // For symbols that require a load from a stub to get the address, emit the
12176   // load.
12177   if (isGlobalStubReference(OpFlag))
12178     Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
12179                          MachinePointerInfo::getGOT(DAG.getMachineFunction()),
12180                          false, false, false, 0);
12181
12182   return Result;
12183 }
12184
12185 SDValue
12186 X86TargetLowering::LowerBlockAddress(SDValue Op, SelectionDAG &DAG) const {
12187   // Create the TargetBlockAddressAddress node.
12188   unsigned char OpFlags =
12189     Subtarget->ClassifyBlockAddressReference();
12190   CodeModel::Model M = DAG.getTarget().getCodeModel();
12191   const BlockAddress *BA = cast<BlockAddressSDNode>(Op)->getBlockAddress();
12192   int64_t Offset = cast<BlockAddressSDNode>(Op)->getOffset();
12193   SDLoc dl(Op);
12194   auto PtrVT = getPointerTy(DAG.getDataLayout());
12195   SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset, OpFlags);
12196
12197   if (Subtarget->isPICStyleRIPRel() &&
12198       (M == CodeModel::Small || M == CodeModel::Kernel))
12199     Result = DAG.getNode(X86ISD::WrapperRIP, dl, PtrVT, Result);
12200   else
12201     Result = DAG.getNode(X86ISD::Wrapper, dl, PtrVT, Result);
12202
12203   // With PIC, the address is actually $g + Offset.
12204   if (isGlobalRelativeToPICBase(OpFlags)) {
12205     Result = DAG.getNode(ISD::ADD, dl, PtrVT,
12206                          DAG.getNode(X86ISD::GlobalBaseReg, dl, PtrVT), Result);
12207   }
12208
12209   return Result;
12210 }
12211
12212 SDValue
12213 X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, SDLoc dl,
12214                                       int64_t Offset, SelectionDAG &DAG) const {
12215   // Create the TargetGlobalAddress node, folding in the constant
12216   // offset if it is legal.
12217   unsigned char OpFlags =
12218       Subtarget->ClassifyGlobalReference(GV, DAG.getTarget());
12219   CodeModel::Model M = DAG.getTarget().getCodeModel();
12220   auto PtrVT = getPointerTy(DAG.getDataLayout());
12221   SDValue Result;
12222   if (OpFlags == X86II::MO_NO_FLAG &&
12223       X86::isOffsetSuitableForCodeModel(Offset, M)) {
12224     // A direct static reference to a global.
12225     Result = DAG.getTargetGlobalAddress(GV, dl, PtrVT, Offset);
12226     Offset = 0;
12227   } else {
12228     Result = DAG.getTargetGlobalAddress(GV, dl, PtrVT, 0, OpFlags);
12229   }
12230
12231   if (Subtarget->isPICStyleRIPRel() &&
12232       (M == CodeModel::Small || M == CodeModel::Kernel))
12233     Result = DAG.getNode(X86ISD::WrapperRIP, dl, PtrVT, Result);
12234   else
12235     Result = DAG.getNode(X86ISD::Wrapper, dl, PtrVT, Result);
12236
12237   // With PIC, the address is actually $g + Offset.
12238   if (isGlobalRelativeToPICBase(OpFlags)) {
12239     Result = DAG.getNode(ISD::ADD, dl, PtrVT,
12240                          DAG.getNode(X86ISD::GlobalBaseReg, dl, PtrVT), Result);
12241   }
12242
12243   // For globals that require a load from a stub to get the address, emit the
12244   // load.
12245   if (isGlobalStubReference(OpFlags))
12246     Result = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Result,
12247                          MachinePointerInfo::getGOT(DAG.getMachineFunction()),
12248                          false, false, false, 0);
12249
12250   // If there was a non-zero offset that we didn't fold, create an explicit
12251   // addition for it.
12252   if (Offset != 0)
12253     Result = DAG.getNode(ISD::ADD, dl, PtrVT, Result,
12254                          DAG.getConstant(Offset, dl, PtrVT));
12255
12256   return Result;
12257 }
12258
12259 SDValue
12260 X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const {
12261   const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
12262   int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
12263   return LowerGlobalAddress(GV, SDLoc(Op), Offset, DAG);
12264 }
12265
12266 static SDValue
12267 GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
12268            SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
12269            unsigned char OperandFlags, bool LocalDynamic = false) {
12270   MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
12271   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
12272   SDLoc dl(GA);
12273   SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
12274                                            GA->getValueType(0),
12275                                            GA->getOffset(),
12276                                            OperandFlags);
12277
12278   X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
12279                                            : X86ISD::TLSADDR;
12280
12281   if (InFlag) {
12282     SDValue Ops[] = { Chain,  TGA, *InFlag };
12283     Chain = DAG.getNode(CallType, dl, NodeTys, Ops);
12284   } else {
12285     SDValue Ops[]  = { Chain, TGA };
12286     Chain = DAG.getNode(CallType, dl, NodeTys, Ops);
12287   }
12288
12289   // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
12290   MFI->setAdjustsStack(true);
12291   MFI->setHasCalls(true);
12292
12293   SDValue Flag = Chain.getValue(1);
12294   return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
12295 }
12296
12297 // Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
12298 static SDValue
12299 LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
12300                                 const EVT PtrVT) {
12301   SDValue InFlag;
12302   SDLoc dl(GA);  // ? function entry point might be better
12303   SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
12304                                    DAG.getNode(X86ISD::GlobalBaseReg,
12305                                                SDLoc(), PtrVT), InFlag);
12306   InFlag = Chain.getValue(1);
12307
12308   return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
12309 }
12310
12311 // Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
12312 static SDValue
12313 LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
12314                                 const EVT PtrVT) {
12315   return GetTLSADDR(DAG, DAG.getEntryNode(), GA, nullptr, PtrVT,
12316                     X86::RAX, X86II::MO_TLSGD);
12317 }
12318
12319 static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
12320                                            SelectionDAG &DAG,
12321                                            const EVT PtrVT,
12322                                            bool is64Bit) {
12323   SDLoc dl(GA);
12324
12325   // Get the start address of the TLS block for this module.
12326   X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
12327       .getInfo<X86MachineFunctionInfo>();
12328   MFI->incNumLocalDynamicTLSAccesses();
12329
12330   SDValue Base;
12331   if (is64Bit) {
12332     Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, nullptr, PtrVT, X86::RAX,
12333                       X86II::MO_TLSLD, /*LocalDynamic=*/true);
12334   } else {
12335     SDValue InFlag;
12336     SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
12337         DAG.getNode(X86ISD::GlobalBaseReg, SDLoc(), PtrVT), InFlag);
12338     InFlag = Chain.getValue(1);
12339     Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
12340                       X86II::MO_TLSLDM, /*LocalDynamic=*/true);
12341   }
12342
12343   // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
12344   // of Base.
12345
12346   // Build x@dtpoff.
12347   unsigned char OperandFlags = X86II::MO_DTPOFF;
12348   unsigned WrapperKind = X86ISD::Wrapper;
12349   SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
12350                                            GA->getValueType(0),
12351                                            GA->getOffset(), OperandFlags);
12352   SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
12353
12354   // Add x@dtpoff with the base.
12355   return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
12356 }
12357
12358 // Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
12359 static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
12360                                    const EVT PtrVT, TLSModel::Model model,
12361                                    bool is64Bit, bool isPIC) {
12362   SDLoc dl(GA);
12363
12364   // Get the Thread Pointer, which is %gs:0 (32-bit) or %fs:0 (64-bit).
12365   Value *Ptr = Constant::getNullValue(Type::getInt8PtrTy(*DAG.getContext(),
12366                                                          is64Bit ? 257 : 256));
12367
12368   SDValue ThreadPointer =
12369       DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), DAG.getIntPtrConstant(0, dl),
12370                   MachinePointerInfo(Ptr), false, false, false, 0);
12371
12372   unsigned char OperandFlags = 0;
12373   // Most TLS accesses are not RIP relative, even on x86-64.  One exception is
12374   // initialexec.
12375   unsigned WrapperKind = X86ISD::Wrapper;
12376   if (model == TLSModel::LocalExec) {
12377     OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
12378   } else if (model == TLSModel::InitialExec) {
12379     if (is64Bit) {
12380       OperandFlags = X86II::MO_GOTTPOFF;
12381       WrapperKind = X86ISD::WrapperRIP;
12382     } else {
12383       OperandFlags = isPIC ? X86II::MO_GOTNTPOFF : X86II::MO_INDNTPOFF;
12384     }
12385   } else {
12386     llvm_unreachable("Unexpected model");
12387   }
12388
12389   // emit "addl x@ntpoff,%eax" (local exec)
12390   // or "addl x@indntpoff,%eax" (initial exec)
12391   // or "addl x@gotntpoff(%ebx) ,%eax" (initial exec, 32-bit pic)
12392   SDValue TGA =
12393       DAG.getTargetGlobalAddress(GA->getGlobal(), dl, GA->getValueType(0),
12394                                  GA->getOffset(), OperandFlags);
12395   SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
12396
12397   if (model == TLSModel::InitialExec) {
12398     if (isPIC && !is64Bit) {
12399       Offset = DAG.getNode(ISD::ADD, dl, PtrVT,
12400                            DAG.getNode(X86ISD::GlobalBaseReg, SDLoc(), PtrVT),
12401                            Offset);
12402     }
12403
12404     Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
12405                          MachinePointerInfo::getGOT(DAG.getMachineFunction()),
12406                          false, false, false, 0);
12407   }
12408
12409   // The address of the thread local variable is the add of the thread
12410   // pointer with the offset of the variable.
12411   return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
12412 }
12413
12414 SDValue
12415 X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
12416
12417   GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
12418
12419   // Cygwin uses emutls.
12420   // FIXME: It may be EmulatedTLS-generic also for X86-Android.
12421   if (Subtarget->isTargetWindowsCygwin())
12422     return LowerToTLSEmulatedModel(GA, DAG);
12423
12424   const GlobalValue *GV = GA->getGlobal();
12425   auto PtrVT = getPointerTy(DAG.getDataLayout());
12426
12427   if (Subtarget->isTargetELF()) {
12428     if (DAG.getTarget().Options.EmulatedTLS)
12429       return LowerToTLSEmulatedModel(GA, DAG);
12430     TLSModel::Model model = DAG.getTarget().getTLSModel(GV);
12431     switch (model) {
12432       case TLSModel::GeneralDynamic:
12433         if (Subtarget->is64Bit())
12434           return LowerToTLSGeneralDynamicModel64(GA, DAG, PtrVT);
12435         return LowerToTLSGeneralDynamicModel32(GA, DAG, PtrVT);
12436       case TLSModel::LocalDynamic:
12437         return LowerToTLSLocalDynamicModel(GA, DAG, PtrVT,
12438                                            Subtarget->is64Bit());
12439       case TLSModel::InitialExec:
12440       case TLSModel::LocalExec:
12441         return LowerToTLSExecModel(GA, DAG, PtrVT, model, Subtarget->is64Bit(),
12442                                    DAG.getTarget().getRelocationModel() ==
12443                                        Reloc::PIC_);
12444     }
12445     llvm_unreachable("Unknown TLS model.");
12446   }
12447
12448   if (Subtarget->isTargetDarwin()) {
12449     // Darwin only has one model of TLS.  Lower to that.
12450     unsigned char OpFlag = 0;
12451     unsigned WrapperKind = Subtarget->isPICStyleRIPRel() ?
12452                            X86ISD::WrapperRIP : X86ISD::Wrapper;
12453
12454     // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
12455     // global base reg.
12456     bool PIC32 = (DAG.getTarget().getRelocationModel() == Reloc::PIC_) &&
12457                  !Subtarget->is64Bit();
12458     if (PIC32)
12459       OpFlag = X86II::MO_TLVP_PIC_BASE;
12460     else
12461       OpFlag = X86II::MO_TLVP;
12462     SDLoc DL(Op);
12463     SDValue Result = DAG.getTargetGlobalAddress(GA->getGlobal(), DL,
12464                                                 GA->getValueType(0),
12465                                                 GA->getOffset(), OpFlag);
12466     SDValue Offset = DAG.getNode(WrapperKind, DL, PtrVT, Result);
12467
12468     // With PIC32, the address is actually $g + Offset.
12469     if (PIC32)
12470       Offset = DAG.getNode(ISD::ADD, DL, PtrVT,
12471                            DAG.getNode(X86ISD::GlobalBaseReg, SDLoc(), PtrVT),
12472                            Offset);
12473
12474     // Lowering the machine isd will make sure everything is in the right
12475     // location.
12476     SDValue Chain = DAG.getEntryNode();
12477     SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
12478     SDValue Args[] = { Chain, Offset };
12479     Chain = DAG.getNode(X86ISD::TLSCALL, DL, NodeTys, Args);
12480
12481     // TLSCALL will be codegen'ed as call. Inform MFI that function has calls.
12482     MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
12483     MFI->setAdjustsStack(true);
12484
12485     // And our return value (tls address) is in the standard call return value
12486     // location.
12487     unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
12488     return DAG.getCopyFromReg(Chain, DL, Reg, PtrVT, Chain.getValue(1));
12489   }
12490
12491   if (Subtarget->isTargetKnownWindowsMSVC() ||
12492       Subtarget->isTargetWindowsGNU()) {
12493     // Just use the implicit TLS architecture
12494     // Need to generate someting similar to:
12495     //   mov     rdx, qword [gs:abs 58H]; Load pointer to ThreadLocalStorage
12496     //                                  ; from TEB
12497     //   mov     ecx, dword [rel _tls_index]: Load index (from C runtime)
12498     //   mov     rcx, qword [rdx+rcx*8]
12499     //   mov     eax, .tls$:tlsvar
12500     //   [rax+rcx] contains the address
12501     // Windows 64bit: gs:0x58
12502     // Windows 32bit: fs:__tls_array
12503
12504     SDLoc dl(GA);
12505     SDValue Chain = DAG.getEntryNode();
12506
12507     // Get the Thread Pointer, which is %fs:__tls_array (32-bit) or
12508     // %gs:0x58 (64-bit). On MinGW, __tls_array is not available, so directly
12509     // use its literal value of 0x2C.
12510     Value *Ptr = Constant::getNullValue(Subtarget->is64Bit()
12511                                         ? Type::getInt8PtrTy(*DAG.getContext(),
12512                                                              256)
12513                                         : Type::getInt32PtrTy(*DAG.getContext(),
12514                                                               257));
12515
12516     SDValue TlsArray = Subtarget->is64Bit()
12517                            ? DAG.getIntPtrConstant(0x58, dl)
12518                            : (Subtarget->isTargetWindowsGNU()
12519                                   ? DAG.getIntPtrConstant(0x2C, dl)
12520                                   : DAG.getExternalSymbol("_tls_array", PtrVT));
12521
12522     SDValue ThreadPointer =
12523         DAG.getLoad(PtrVT, dl, Chain, TlsArray, MachinePointerInfo(Ptr), false,
12524                     false, false, 0);
12525
12526     SDValue res;
12527     if (GV->getThreadLocalMode() == GlobalVariable::LocalExecTLSModel) {
12528       res = ThreadPointer;
12529     } else {
12530       // Load the _tls_index variable
12531       SDValue IDX = DAG.getExternalSymbol("_tls_index", PtrVT);
12532       if (Subtarget->is64Bit())
12533         IDX = DAG.getExtLoad(ISD::ZEXTLOAD, dl, PtrVT, Chain, IDX,
12534                              MachinePointerInfo(), MVT::i32, false, false,
12535                              false, 0);
12536       else
12537         IDX = DAG.getLoad(PtrVT, dl, Chain, IDX, MachinePointerInfo(), false,
12538                           false, false, 0);
12539
12540       auto &DL = DAG.getDataLayout();
12541       SDValue Scale =
12542           DAG.getConstant(Log2_64_Ceil(DL.getPointerSize()), dl, PtrVT);
12543       IDX = DAG.getNode(ISD::SHL, dl, PtrVT, IDX, Scale);
12544
12545       res = DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, IDX);
12546     }
12547
12548     res = DAG.getLoad(PtrVT, dl, Chain, res, MachinePointerInfo(), false, false,
12549                       false, 0);
12550
12551     // Get the offset of start of .tls section
12552     SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
12553                                              GA->getValueType(0),
12554                                              GA->getOffset(), X86II::MO_SECREL);
12555     SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, PtrVT, TGA);
12556
12557     // The address of the thread local variable is the add of the thread
12558     // pointer with the offset of the variable.
12559     return DAG.getNode(ISD::ADD, dl, PtrVT, res, Offset);
12560   }
12561
12562   llvm_unreachable("TLS not implemented for this target.");
12563 }
12564
12565 /// LowerShiftParts - Lower SRA_PARTS and friends, which return two i32 values
12566 /// and take a 2 x i32 value to shift plus a shift amount.
12567 static SDValue LowerShiftParts(SDValue Op, SelectionDAG &DAG) {
12568   assert(Op.getNumOperands() == 3 && "Not a double-shift!");
12569   MVT VT = Op.getSimpleValueType();
12570   unsigned VTBits = VT.getSizeInBits();
12571   SDLoc dl(Op);
12572   bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
12573   SDValue ShOpLo = Op.getOperand(0);
12574   SDValue ShOpHi = Op.getOperand(1);
12575   SDValue ShAmt  = Op.getOperand(2);
12576   // X86ISD::SHLD and X86ISD::SHRD have defined overflow behavior but the
12577   // generic ISD nodes haven't. Insert an AND to be safe, it's optimized away
12578   // during isel.
12579   SDValue SafeShAmt = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
12580                                   DAG.getConstant(VTBits - 1, dl, MVT::i8));
12581   SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
12582                                      DAG.getConstant(VTBits - 1, dl, MVT::i8))
12583                        : DAG.getConstant(0, dl, VT);
12584
12585   SDValue Tmp2, Tmp3;
12586   if (Op.getOpcode() == ISD::SHL_PARTS) {
12587     Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
12588     Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, SafeShAmt);
12589   } else {
12590     Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
12591     Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, SafeShAmt);
12592   }
12593
12594   // If the shift amount is larger or equal than the width of a part we can't
12595   // rely on the results of shld/shrd. Insert a test and select the appropriate
12596   // values for large shift amounts.
12597   SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
12598                                 DAG.getConstant(VTBits, dl, MVT::i8));
12599   SDValue Cond = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
12600                              AndNode, DAG.getConstant(0, dl, MVT::i8));
12601
12602   SDValue Hi, Lo;
12603   SDValue CC = DAG.getConstant(X86::COND_NE, dl, MVT::i8);
12604   SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
12605   SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
12606
12607   if (Op.getOpcode() == ISD::SHL_PARTS) {
12608     Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0);
12609     Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1);
12610   } else {
12611     Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0);
12612     Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1);
12613   }
12614
12615   SDValue Ops[2] = { Lo, Hi };
12616   return DAG.getMergeValues(Ops, dl);
12617 }
12618
12619 SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op,
12620                                            SelectionDAG &DAG) const {
12621   SDValue Src = Op.getOperand(0);
12622   MVT SrcVT = Src.getSimpleValueType();
12623   MVT VT = Op.getSimpleValueType();
12624   SDLoc dl(Op);
12625
12626   if (SrcVT.isVector()) {
12627     if (SrcVT == MVT::v2i32 && VT == MVT::v2f64) {
12628       return DAG.getNode(X86ISD::CVTDQ2PD, dl, VT,
12629                          DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v4i32, Src,
12630                          DAG.getUNDEF(SrcVT)));
12631     }
12632     if (SrcVT.getVectorElementType() == MVT::i1) {
12633       MVT IntegerVT = MVT::getVectorVT(MVT::i32, SrcVT.getVectorNumElements());
12634       return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(),
12635                          DAG.getNode(ISD::SIGN_EXTEND, dl, IntegerVT, Src));
12636     }
12637     return SDValue();
12638   }
12639
12640   assert(SrcVT <= MVT::i64 && SrcVT >= MVT::i16 &&
12641          "Unknown SINT_TO_FP to lower!");
12642
12643   // These are really Legal; return the operand so the caller accepts it as
12644   // Legal.
12645   if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
12646     return Op;
12647   if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
12648       Subtarget->is64Bit()) {
12649     return Op;
12650   }
12651
12652   unsigned Size = SrcVT.getSizeInBits()/8;
12653   MachineFunction &MF = DAG.getMachineFunction();
12654   auto PtrVT = getPointerTy(MF.getDataLayout());
12655   int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size, false);
12656   SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
12657   SDValue Chain = DAG.getStore(
12658       DAG.getEntryNode(), dl, Op.getOperand(0), StackSlot,
12659       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SSFI), false,
12660       false, 0);
12661   return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
12662 }
12663
12664 SDValue X86TargetLowering::BuildFILD(SDValue Op, EVT SrcVT, SDValue Chain,
12665                                      SDValue StackSlot,
12666                                      SelectionDAG &DAG) const {
12667   // Build the FILD
12668   SDLoc DL(Op);
12669   SDVTList Tys;
12670   bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
12671   if (useSSE)
12672     Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Glue);
12673   else
12674     Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
12675
12676   unsigned ByteSize = SrcVT.getSizeInBits()/8;
12677
12678   FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(StackSlot);
12679   MachineMemOperand *MMO;
12680   if (FI) {
12681     int SSFI = FI->getIndex();
12682     MMO = DAG.getMachineFunction().getMachineMemOperand(
12683         MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SSFI),
12684         MachineMemOperand::MOLoad, ByteSize, ByteSize);
12685   } else {
12686     MMO = cast<LoadSDNode>(StackSlot)->getMemOperand();
12687     StackSlot = StackSlot.getOperand(1);
12688   }
12689   SDValue Ops[] = { Chain, StackSlot, DAG.getValueType(SrcVT) };
12690   SDValue Result = DAG.getMemIntrinsicNode(useSSE ? X86ISD::FILD_FLAG :
12691                                            X86ISD::FILD, DL,
12692                                            Tys, Ops, SrcVT, MMO);
12693
12694   if (useSSE) {
12695     Chain = Result.getValue(1);
12696     SDValue InFlag = Result.getValue(2);
12697
12698     // FIXME: Currently the FST is flagged to the FILD_FLAG. This
12699     // shouldn't be necessary except that RFP cannot be live across
12700     // multiple blocks. When stackifier is fixed, they can be uncoupled.
12701     MachineFunction &MF = DAG.getMachineFunction();
12702     unsigned SSFISize = Op.getValueType().getSizeInBits()/8;
12703     int SSFI = MF.getFrameInfo()->CreateStackObject(SSFISize, SSFISize, false);
12704     auto PtrVT = getPointerTy(MF.getDataLayout());
12705     SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
12706     Tys = DAG.getVTList(MVT::Other);
12707     SDValue Ops[] = {
12708       Chain, Result, StackSlot, DAG.getValueType(Op.getValueType()), InFlag
12709     };
12710     MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
12711         MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SSFI),
12712         MachineMemOperand::MOStore, SSFISize, SSFISize);
12713
12714     Chain = DAG.getMemIntrinsicNode(X86ISD::FST, DL, Tys,
12715                                     Ops, Op.getValueType(), MMO);
12716     Result = DAG.getLoad(
12717         Op.getValueType(), DL, Chain, StackSlot,
12718         MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SSFI),
12719         false, false, false, 0);
12720   }
12721
12722   return Result;
12723 }
12724
12725 // LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
12726 SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op,
12727                                                SelectionDAG &DAG) const {
12728   // This algorithm is not obvious. Here it is what we're trying to output:
12729   /*
12730      movq       %rax,  %xmm0
12731      punpckldq  (c0),  %xmm0  // c0: (uint4){ 0x43300000U, 0x45300000U, 0U, 0U }
12732      subpd      (c1),  %xmm0  // c1: (double2){ 0x1.0p52, 0x1.0p52 * 0x1.0p32 }
12733      #ifdef __SSE3__
12734        haddpd   %xmm0, %xmm0
12735      #else
12736        pshufd   $0x4e, %xmm0, %xmm1
12737        addpd    %xmm1, %xmm0
12738      #endif
12739   */
12740
12741   SDLoc dl(Op);
12742   LLVMContext *Context = DAG.getContext();
12743
12744   // Build some magic constants.
12745   static const uint32_t CV0[] = { 0x43300000, 0x45300000, 0, 0 };
12746   Constant *C0 = ConstantDataVector::get(*Context, CV0);
12747   auto PtrVT = getPointerTy(DAG.getDataLayout());
12748   SDValue CPIdx0 = DAG.getConstantPool(C0, PtrVT, 16);
12749
12750   SmallVector<Constant*,2> CV1;
12751   CV1.push_back(
12752     ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
12753                                       APInt(64, 0x4330000000000000ULL))));
12754   CV1.push_back(
12755     ConstantFP::get(*Context, APFloat(APFloat::IEEEdouble,
12756                                       APInt(64, 0x4530000000000000ULL))));
12757   Constant *C1 = ConstantVector::get(CV1);
12758   SDValue CPIdx1 = DAG.getConstantPool(C1, PtrVT, 16);
12759
12760   // Load the 64-bit value into an XMM register.
12761   SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64,
12762                             Op.getOperand(0));
12763   SDValue CLod0 =
12764       DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
12765                   MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
12766                   false, false, false, 16);
12767   SDValue Unpck1 =
12768       getUnpackl(DAG, dl, MVT::v4i32, DAG.getBitcast(MVT::v4i32, XR1), CLod0);
12769
12770   SDValue CLod1 =
12771       DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
12772                   MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
12773                   false, false, false, 16);
12774   SDValue XR2F = DAG.getBitcast(MVT::v2f64, Unpck1);
12775   // TODO: Are there any fast-math-flags to propagate here?
12776   SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
12777   SDValue Result;
12778
12779   if (Subtarget->hasSSE3()) {
12780     // FIXME: The 'haddpd' instruction may be slower than 'movhlps + addsd'.
12781     Result = DAG.getNode(X86ISD::FHADD, dl, MVT::v2f64, Sub, Sub);
12782   } else {
12783     SDValue S2F = DAG.getBitcast(MVT::v4i32, Sub);
12784     SDValue Shuffle = getTargetShuffleNode(X86ISD::PSHUFD, dl, MVT::v4i32,
12785                                            S2F, 0x4E, DAG);
12786     Result = DAG.getNode(ISD::FADD, dl, MVT::v2f64,
12787                          DAG.getBitcast(MVT::v2f64, Shuffle), Sub);
12788   }
12789
12790   return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Result,
12791                      DAG.getIntPtrConstant(0, dl));
12792 }
12793
12794 // LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
12795 SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op,
12796                                                SelectionDAG &DAG) const {
12797   SDLoc dl(Op);
12798   // FP constant to bias correct the final result.
12799   SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL), dl,
12800                                    MVT::f64);
12801
12802   // Load the 32-bit value into an XMM register.
12803   SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
12804                              Op.getOperand(0));
12805
12806   // Zero out the upper parts of the register.
12807   Load = getShuffleVectorZeroOrUndef(Load, 0, true, Subtarget, DAG);
12808
12809   Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
12810                      DAG.getBitcast(MVT::v2f64, Load),
12811                      DAG.getIntPtrConstant(0, dl));
12812
12813   // Or the load with the bias.
12814   SDValue Or = DAG.getNode(
12815       ISD::OR, dl, MVT::v2i64,
12816       DAG.getBitcast(MVT::v2i64,
12817                      DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, Load)),
12818       DAG.getBitcast(MVT::v2i64,
12819                      DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, Bias)));
12820   Or =
12821       DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
12822                   DAG.getBitcast(MVT::v2f64, Or), DAG.getIntPtrConstant(0, dl));
12823
12824   // Subtract the bias.
12825   // TODO: Are there any fast-math-flags to propagate here?
12826   SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
12827
12828   // Handle final rounding.
12829   MVT DestVT = Op.getSimpleValueType();
12830
12831   if (DestVT.bitsLT(MVT::f64))
12832     return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
12833                        DAG.getIntPtrConstant(0, dl));
12834   if (DestVT.bitsGT(MVT::f64))
12835     return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
12836
12837   // Handle final rounding.
12838   return Sub;
12839 }
12840
12841 static SDValue lowerUINT_TO_FP_vXi32(SDValue Op, SelectionDAG &DAG,
12842                                      const X86Subtarget &Subtarget) {
12843   // The algorithm is the following:
12844   // #ifdef __SSE4_1__
12845   //     uint4 lo = _mm_blend_epi16( v, (uint4) 0x4b000000, 0xaa);
12846   //     uint4 hi = _mm_blend_epi16( _mm_srli_epi32(v,16),
12847   //                                 (uint4) 0x53000000, 0xaa);
12848   // #else
12849   //     uint4 lo = (v & (uint4) 0xffff) | (uint4) 0x4b000000;
12850   //     uint4 hi = (v >> 16) | (uint4) 0x53000000;
12851   // #endif
12852   //     float4 fhi = (float4) hi - (0x1.0p39f + 0x1.0p23f);
12853   //     return (float4) lo + fhi;
12854
12855   // We shouldn't use it when unsafe-fp-math is enabled though: we might later
12856   // reassociate the two FADDs, and if we do that, the algorithm fails
12857   // spectacularly (PR24512).
12858   // FIXME: If we ever have some kind of Machine FMF, this should be marked
12859   // as non-fast and always be enabled. Why isn't SDAG FMF enough? Because
12860   // there's also the MachineCombiner reassociations happening on Machine IR.
12861   if (DAG.getTarget().Options.UnsafeFPMath)
12862     return SDValue();
12863
12864   SDLoc DL(Op);
12865   SDValue V = Op->getOperand(0);
12866   MVT VecIntVT = V.getSimpleValueType();
12867   bool Is128 = VecIntVT == MVT::v4i32;
12868   MVT VecFloatVT = Is128 ? MVT::v4f32 : MVT::v8f32;
12869   // If we convert to something else than the supported type, e.g., to v4f64,
12870   // abort early.
12871   if (VecFloatVT != Op->getSimpleValueType(0))
12872     return SDValue();
12873
12874   unsigned NumElts = VecIntVT.getVectorNumElements();
12875   assert((VecIntVT == MVT::v4i32 || VecIntVT == MVT::v8i32) &&
12876          "Unsupported custom type");
12877   assert(NumElts <= 8 && "The size of the constant array must be fixed");
12878
12879   // In the #idef/#else code, we have in common:
12880   // - The vector of constants:
12881   // -- 0x4b000000
12882   // -- 0x53000000
12883   // - A shift:
12884   // -- v >> 16
12885
12886   // Create the splat vector for 0x4b000000.
12887   SDValue CstLow = DAG.getConstant(0x4b000000, DL, MVT::i32);
12888   SDValue CstLowArray[] = {CstLow, CstLow, CstLow, CstLow,
12889                            CstLow, CstLow, CstLow, CstLow};
12890   SDValue VecCstLow = DAG.getNode(ISD::BUILD_VECTOR, DL, VecIntVT,
12891                                   makeArrayRef(&CstLowArray[0], NumElts));
12892   // Create the splat vector for 0x53000000.
12893   SDValue CstHigh = DAG.getConstant(0x53000000, DL, MVT::i32);
12894   SDValue CstHighArray[] = {CstHigh, CstHigh, CstHigh, CstHigh,
12895                             CstHigh, CstHigh, CstHigh, CstHigh};
12896   SDValue VecCstHigh = DAG.getNode(ISD::BUILD_VECTOR, DL, VecIntVT,
12897                                    makeArrayRef(&CstHighArray[0], NumElts));
12898
12899   // Create the right shift.
12900   SDValue CstShift = DAG.getConstant(16, DL, MVT::i32);
12901   SDValue CstShiftArray[] = {CstShift, CstShift, CstShift, CstShift,
12902                              CstShift, CstShift, CstShift, CstShift};
12903   SDValue VecCstShift = DAG.getNode(ISD::BUILD_VECTOR, DL, VecIntVT,
12904                                     makeArrayRef(&CstShiftArray[0], NumElts));
12905   SDValue HighShift = DAG.getNode(ISD::SRL, DL, VecIntVT, V, VecCstShift);
12906
12907   SDValue Low, High;
12908   if (Subtarget.hasSSE41()) {
12909     MVT VecI16VT = Is128 ? MVT::v8i16 : MVT::v16i16;
12910     //     uint4 lo = _mm_blend_epi16( v, (uint4) 0x4b000000, 0xaa);
12911     SDValue VecCstLowBitcast = DAG.getBitcast(VecI16VT, VecCstLow);
12912     SDValue VecBitcast = DAG.getBitcast(VecI16VT, V);
12913     // Low will be bitcasted right away, so do not bother bitcasting back to its
12914     // original type.
12915     Low = DAG.getNode(X86ISD::BLENDI, DL, VecI16VT, VecBitcast,
12916                       VecCstLowBitcast, DAG.getConstant(0xaa, DL, MVT::i32));
12917     //     uint4 hi = _mm_blend_epi16( _mm_srli_epi32(v,16),
12918     //                                 (uint4) 0x53000000, 0xaa);
12919     SDValue VecCstHighBitcast = DAG.getBitcast(VecI16VT, VecCstHigh);
12920     SDValue VecShiftBitcast = DAG.getBitcast(VecI16VT, HighShift);
12921     // High will be bitcasted right away, so do not bother bitcasting back to
12922     // its original type.
12923     High = DAG.getNode(X86ISD::BLENDI, DL, VecI16VT, VecShiftBitcast,
12924                        VecCstHighBitcast, DAG.getConstant(0xaa, DL, MVT::i32));
12925   } else {
12926     SDValue CstMask = DAG.getConstant(0xffff, DL, MVT::i32);
12927     SDValue VecCstMask = DAG.getNode(ISD::BUILD_VECTOR, DL, VecIntVT, CstMask,
12928                                      CstMask, CstMask, CstMask);
12929     //     uint4 lo = (v & (uint4) 0xffff) | (uint4) 0x4b000000;
12930     SDValue LowAnd = DAG.getNode(ISD::AND, DL, VecIntVT, V, VecCstMask);
12931     Low = DAG.getNode(ISD::OR, DL, VecIntVT, LowAnd, VecCstLow);
12932
12933     //     uint4 hi = (v >> 16) | (uint4) 0x53000000;
12934     High = DAG.getNode(ISD::OR, DL, VecIntVT, HighShift, VecCstHigh);
12935   }
12936
12937   // Create the vector constant for -(0x1.0p39f + 0x1.0p23f).
12938   SDValue CstFAdd = DAG.getConstantFP(
12939       APFloat(APFloat::IEEEsingle, APInt(32, 0xD3000080)), DL, MVT::f32);
12940   SDValue CstFAddArray[] = {CstFAdd, CstFAdd, CstFAdd, CstFAdd,
12941                             CstFAdd, CstFAdd, CstFAdd, CstFAdd};
12942   SDValue VecCstFAdd = DAG.getNode(ISD::BUILD_VECTOR, DL, VecFloatVT,
12943                                    makeArrayRef(&CstFAddArray[0], NumElts));
12944
12945   //     float4 fhi = (float4) hi - (0x1.0p39f + 0x1.0p23f);
12946   SDValue HighBitcast = DAG.getBitcast(VecFloatVT, High);
12947   // TODO: Are there any fast-math-flags to propagate here?
12948   SDValue FHigh =
12949       DAG.getNode(ISD::FADD, DL, VecFloatVT, HighBitcast, VecCstFAdd);
12950   //     return (float4) lo + fhi;
12951   SDValue LowBitcast = DAG.getBitcast(VecFloatVT, Low);
12952   return DAG.getNode(ISD::FADD, DL, VecFloatVT, LowBitcast, FHigh);
12953 }
12954
12955 SDValue X86TargetLowering::lowerUINT_TO_FP_vec(SDValue Op,
12956                                                SelectionDAG &DAG) const {
12957   SDValue N0 = Op.getOperand(0);
12958   MVT SVT = N0.getSimpleValueType();
12959   SDLoc dl(Op);
12960
12961   switch (SVT.SimpleTy) {
12962   default:
12963     llvm_unreachable("Custom UINT_TO_FP is not supported!");
12964   case MVT::v4i8:
12965   case MVT::v4i16:
12966   case MVT::v8i8:
12967   case MVT::v8i16: {
12968     MVT NVT = MVT::getVectorVT(MVT::i32, SVT.getVectorNumElements());
12969     return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(),
12970                        DAG.getNode(ISD::ZERO_EXTEND, dl, NVT, N0));
12971   }
12972   case MVT::v4i32:
12973   case MVT::v8i32:
12974     return lowerUINT_TO_FP_vXi32(Op, DAG, *Subtarget);
12975   case MVT::v16i8:
12976   case MVT::v16i16:
12977     assert(Subtarget->hasAVX512());
12978     return DAG.getNode(ISD::UINT_TO_FP, dl, Op.getValueType(),
12979                        DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v16i32, N0));
12980   }
12981 }
12982
12983 SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op,
12984                                            SelectionDAG &DAG) const {
12985   SDValue N0 = Op.getOperand(0);
12986   SDLoc dl(Op);
12987   auto PtrVT = getPointerTy(DAG.getDataLayout());
12988
12989   if (Op.getSimpleValueType().isVector())
12990     return lowerUINT_TO_FP_vec(Op, DAG);
12991
12992   // Since UINT_TO_FP is legal (it's marked custom), dag combiner won't
12993   // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
12994   // the optimization here.
12995   if (DAG.SignBitIsZero(N0))
12996     return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
12997
12998   MVT SrcVT = N0.getSimpleValueType();
12999   MVT DstVT = Op.getSimpleValueType();
13000
13001   if (Subtarget->hasAVX512() && isScalarFPTypeInSSEReg(DstVT) &&
13002       (SrcVT == MVT::i32 || (SrcVT == MVT::i64 && Subtarget->is64Bit()))) {
13003     // Conversions from unsigned i32 to f32/f64 are legal,
13004     // using VCVTUSI2SS/SD.  Same for i64 in 64-bit mode.
13005     return Op;
13006   }
13007
13008   if (SrcVT == MVT::i64 && DstVT == MVT::f64 && X86ScalarSSEf64)
13009     return LowerUINT_TO_FP_i64(Op, DAG);
13010   if (SrcVT == MVT::i32 && X86ScalarSSEf64)
13011     return LowerUINT_TO_FP_i32(Op, DAG);
13012   if (Subtarget->is64Bit() && SrcVT == MVT::i64 && DstVT == MVT::f32)
13013     return SDValue();
13014
13015   // Make a 64-bit buffer, and use it to build an FILD.
13016   SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
13017   if (SrcVT == MVT::i32) {
13018     SDValue WordOff = DAG.getConstant(4, dl, PtrVT);
13019     SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl, PtrVT, StackSlot, WordOff);
13020     SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
13021                                   StackSlot, MachinePointerInfo(),
13022                                   false, false, 0);
13023     SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, dl, MVT::i32),
13024                                   OffsetSlot, MachinePointerInfo(),
13025                                   false, false, 0);
13026     SDValue Fild = BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
13027     return Fild;
13028   }
13029
13030   assert(SrcVT == MVT::i64 && "Unexpected type in UINT_TO_FP");
13031   SDValue Store = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
13032                                StackSlot, MachinePointerInfo(),
13033                                false, false, 0);
13034   // For i64 source, we need to add the appropriate power of 2 if the input
13035   // was negative.  This is the same as the optimization in
13036   // DAGTypeLegalizer::ExpandIntOp_UNIT_TO_FP, and for it to be safe here,
13037   // we must be careful to do the computation in x87 extended precision, not
13038   // in SSE. (The generic code can't know it's OK to do this, or how to.)
13039   int SSFI = cast<FrameIndexSDNode>(StackSlot)->getIndex();
13040   MachineMemOperand *MMO = DAG.getMachineFunction().getMachineMemOperand(
13041       MachinePointerInfo::getFixedStack(DAG.getMachineFunction(), SSFI),
13042       MachineMemOperand::MOLoad, 8, 8);
13043
13044   SDVTList Tys = DAG.getVTList(MVT::f80, MVT::Other);
13045   SDValue Ops[] = { Store, StackSlot, DAG.getValueType(MVT::i64) };
13046   SDValue Fild = DAG.getMemIntrinsicNode(X86ISD::FILD, dl, Tys, Ops,
13047                                          MVT::i64, MMO);
13048
13049   APInt FF(32, 0x5F800000ULL);
13050
13051   // Check whether the sign bit is set.
13052   SDValue SignSet = DAG.getSetCC(
13053       dl, getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), MVT::i64),
13054       Op.getOperand(0), DAG.getConstant(0, dl, MVT::i64), ISD::SETLT);
13055
13056   // Build a 64 bit pair (0, FF) in the constant pool, with FF in the lo bits.
13057   SDValue FudgePtr = DAG.getConstantPool(
13058       ConstantInt::get(*DAG.getContext(), FF.zext(64)), PtrVT);
13059
13060   // Get a pointer to FF if the sign bit was set, or to 0 otherwise.
13061   SDValue Zero = DAG.getIntPtrConstant(0, dl);
13062   SDValue Four = DAG.getIntPtrConstant(4, dl);
13063   SDValue Offset = DAG.getNode(ISD::SELECT, dl, Zero.getValueType(), SignSet,
13064                                Zero, Four);
13065   FudgePtr = DAG.getNode(ISD::ADD, dl, PtrVT, FudgePtr, Offset);
13066
13067   // Load the value out, extending it from f32 to f80.
13068   // FIXME: Avoid the extend by constructing the right constant pool?
13069   SDValue Fudge = DAG.getExtLoad(
13070       ISD::EXTLOAD, dl, MVT::f80, DAG.getEntryNode(), FudgePtr,
13071       MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), MVT::f32,
13072       false, false, false, 4);
13073   // Extend everything to 80 bits to force it to be done on x87.
13074   // TODO: Are there any fast-math-flags to propagate here?
13075   SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::f80, Fild, Fudge);
13076   return DAG.getNode(ISD::FP_ROUND, dl, DstVT, Add,
13077                      DAG.getIntPtrConstant(0, dl));
13078 }
13079
13080 // If the given FP_TO_SINT (IsSigned) or FP_TO_UINT (!IsSigned) operation
13081 // is legal, or has an fp128 or f16 source (which needs to be promoted to f32),
13082 // just return an <SDValue(), SDValue()> pair.
13083 // Otherwise it is assumed to be a conversion from one of f32, f64 or f80
13084 // to i16, i32 or i64, and we lower it to a legal sequence.
13085 // If lowered to the final integer result we return a <result, SDValue()> pair.
13086 // Otherwise we lower it to a sequence ending with a FIST, return a
13087 // <FIST, StackSlot> pair, and the caller is responsible for loading
13088 // the final integer result from StackSlot.
13089 std::pair<SDValue,SDValue>
13090 X86TargetLowering::FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG,
13091                                    bool IsSigned, bool IsReplace) const {
13092   SDLoc DL(Op);
13093
13094   EVT DstTy = Op.getValueType();
13095   EVT TheVT = Op.getOperand(0).getValueType();
13096   auto PtrVT = getPointerTy(DAG.getDataLayout());
13097
13098   if (TheVT != MVT::f32 && TheVT != MVT::f64 && TheVT != MVT::f80) {
13099     // f16 must be promoted before using the lowering in this routine.
13100     // fp128 does not use this lowering.
13101     return std::make_pair(SDValue(), SDValue());
13102   }
13103
13104   // If using FIST to compute an unsigned i64, we'll need some fixup
13105   // to handle values above the maximum signed i64.  A FIST is always
13106   // used for the 32-bit subtarget, but also for f80 on a 64-bit target.
13107   bool UnsignedFixup = !IsSigned &&
13108                        DstTy == MVT::i64 &&
13109                        (!Subtarget->is64Bit() ||
13110                         !isScalarFPTypeInSSEReg(TheVT));
13111
13112   if (!IsSigned && DstTy != MVT::i64 && !Subtarget->hasAVX512()) {
13113     // Replace the fp-to-uint32 operation with an fp-to-sint64 FIST.
13114     // The low 32 bits of the fist result will have the correct uint32 result.
13115     assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
13116     DstTy = MVT::i64;
13117   }
13118
13119   assert(DstTy.getSimpleVT() <= MVT::i64 &&
13120          DstTy.getSimpleVT() >= MVT::i16 &&
13121          "Unknown FP_TO_INT to lower!");
13122
13123   // These are really Legal.
13124   if (DstTy == MVT::i32 &&
13125       isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
13126     return std::make_pair(SDValue(), SDValue());
13127   if (Subtarget->is64Bit() &&
13128       DstTy == MVT::i64 &&
13129       isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
13130     return std::make_pair(SDValue(), SDValue());
13131
13132   // We lower FP->int64 into FISTP64 followed by a load from a temporary
13133   // stack slot.
13134   MachineFunction &MF = DAG.getMachineFunction();
13135   unsigned MemSize = DstTy.getSizeInBits()/8;
13136   int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
13137   SDValue StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
13138
13139   unsigned Opc;
13140   switch (DstTy.getSimpleVT().SimpleTy) {
13141   default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
13142   case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
13143   case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
13144   case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
13145   }
13146
13147   SDValue Chain = DAG.getEntryNode();
13148   SDValue Value = Op.getOperand(0);
13149   SDValue Adjust; // 0x0 or 0x80000000, for result sign bit adjustment.
13150
13151   if (UnsignedFixup) {
13152     //
13153     // Conversion to unsigned i64 is implemented with a select,
13154     // depending on whether the source value fits in the range
13155     // of a signed i64.  Let Thresh be the FP equivalent of
13156     // 0x8000000000000000ULL.
13157     //
13158     //  Adjust i32 = (Value < Thresh) ? 0 : 0x80000000;
13159     //  FistSrc    = (Value < Thresh) ? Value : (Value - Thresh);
13160     //  Fist-to-mem64 FistSrc
13161     //  Add 0 or 0x800...0ULL to the 64-bit result, which is equivalent
13162     //  to XOR'ing the high 32 bits with Adjust.
13163     //
13164     // Being a power of 2, Thresh is exactly representable in all FP formats.
13165     // For X87 we'd like to use the smallest FP type for this constant, but
13166     // for DAG type consistency we have to match the FP operand type.
13167
13168     APFloat Thresh(APFloat::IEEEsingle, APInt(32, 0x5f000000));
13169     LLVM_ATTRIBUTE_UNUSED APFloat::opStatus Status = APFloat::opOK;
13170     bool LosesInfo = false;
13171     if (TheVT == MVT::f64)
13172       // The rounding mode is irrelevant as the conversion should be exact.
13173       Status = Thresh.convert(APFloat::IEEEdouble, APFloat::rmNearestTiesToEven,
13174                               &LosesInfo);
13175     else if (TheVT == MVT::f80)
13176       Status = Thresh.convert(APFloat::x87DoubleExtended,
13177                               APFloat::rmNearestTiesToEven, &LosesInfo);
13178
13179     assert(Status == APFloat::opOK && !LosesInfo &&
13180            "FP conversion should have been exact");
13181
13182     SDValue ThreshVal = DAG.getConstantFP(Thresh, DL, TheVT);
13183
13184     SDValue Cmp = DAG.getSetCC(DL,
13185                                getSetCCResultType(DAG.getDataLayout(),
13186                                                   *DAG.getContext(), TheVT),
13187                                Value, ThreshVal, ISD::SETLT);
13188     Adjust = DAG.getSelect(DL, MVT::i32, Cmp,
13189                            DAG.getConstant(0, DL, MVT::i32),
13190                            DAG.getConstant(0x80000000, DL, MVT::i32));
13191     SDValue Sub = DAG.getNode(ISD::FSUB, DL, TheVT, Value, ThreshVal);
13192     Cmp = DAG.getSetCC(DL, getSetCCResultType(DAG.getDataLayout(),
13193                                               *DAG.getContext(), TheVT),
13194                        Value, ThreshVal, ISD::SETLT);
13195     Value = DAG.getSelect(DL, TheVT, Cmp, Value, Sub);
13196   }
13197
13198   // FIXME This causes a redundant load/store if the SSE-class value is already
13199   // in memory, such as if it is on the callstack.
13200   if (isScalarFPTypeInSSEReg(TheVT)) {
13201     assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
13202     Chain = DAG.getStore(Chain, DL, Value, StackSlot,
13203                          MachinePointerInfo::getFixedStack(MF, SSFI), false,
13204                          false, 0);
13205     SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
13206     SDValue Ops[] = {
13207       Chain, StackSlot, DAG.getValueType(TheVT)
13208     };
13209
13210     MachineMemOperand *MMO =
13211         MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(MF, SSFI),
13212                                 MachineMemOperand::MOLoad, MemSize, MemSize);
13213     Value = DAG.getMemIntrinsicNode(X86ISD::FLD, DL, Tys, Ops, DstTy, MMO);
13214     Chain = Value.getValue(1);
13215     SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize, false);
13216     StackSlot = DAG.getFrameIndex(SSFI, PtrVT);
13217   }
13218
13219   MachineMemOperand *MMO =
13220       MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(MF, SSFI),
13221                               MachineMemOperand::MOStore, MemSize, MemSize);
13222
13223   if (UnsignedFixup) {
13224
13225     // Insert the FIST, load its result as two i32's,
13226     // and XOR the high i32 with Adjust.
13227
13228     SDValue FistOps[] = { Chain, Value, StackSlot };
13229     SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
13230                                            FistOps, DstTy, MMO);
13231
13232     SDValue Low32 = DAG.getLoad(MVT::i32, DL, FIST, StackSlot,
13233                                 MachinePointerInfo(),
13234                                 false, false, false, 0);
13235     SDValue HighAddr = DAG.getNode(ISD::ADD, DL, PtrVT, StackSlot,
13236                                    DAG.getConstant(4, DL, PtrVT));
13237
13238     SDValue High32 = DAG.getLoad(MVT::i32, DL, FIST, HighAddr,
13239                                  MachinePointerInfo(),
13240                                  false, false, false, 0);
13241     High32 = DAG.getNode(ISD::XOR, DL, MVT::i32, High32, Adjust);
13242
13243     if (Subtarget->is64Bit()) {
13244       // Join High32 and Low32 into a 64-bit result.
13245       // (High32 << 32) | Low32
13246       Low32 = DAG.getNode(ISD::ZERO_EXTEND, DL, MVT::i64, Low32);
13247       High32 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, High32);
13248       High32 = DAG.getNode(ISD::SHL, DL, MVT::i64, High32,
13249                            DAG.getConstant(32, DL, MVT::i8));
13250       SDValue Result = DAG.getNode(ISD::OR, DL, MVT::i64, High32, Low32);
13251       return std::make_pair(Result, SDValue());
13252     }
13253
13254     SDValue ResultOps[] = { Low32, High32 };
13255
13256     SDValue pair = IsReplace
13257       ? DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, ResultOps)
13258       : DAG.getMergeValues(ResultOps, DL);
13259     return std::make_pair(pair, SDValue());
13260   } else {
13261     // Build the FP_TO_INT*_IN_MEM
13262     SDValue Ops[] = { Chain, Value, StackSlot };
13263     SDValue FIST = DAG.getMemIntrinsicNode(Opc, DL, DAG.getVTList(MVT::Other),
13264                                            Ops, DstTy, MMO);
13265     return std::make_pair(FIST, StackSlot);
13266   }
13267 }
13268
13269 static SDValue LowerAVXExtend(SDValue Op, SelectionDAG &DAG,
13270                               const X86Subtarget *Subtarget) {
13271   MVT VT = Op->getSimpleValueType(0);
13272   SDValue In = Op->getOperand(0);
13273   MVT InVT = In.getSimpleValueType();
13274   SDLoc dl(Op);
13275
13276   if (VT.is512BitVector() || InVT.getVectorElementType() == MVT::i1)
13277     return DAG.getNode(ISD::ZERO_EXTEND, dl, VT, In);
13278
13279   // Optimize vectors in AVX mode:
13280   //
13281   //   v8i16 -> v8i32
13282   //   Use vpunpcklwd for 4 lower elements  v8i16 -> v4i32.
13283   //   Use vpunpckhwd for 4 upper elements  v8i16 -> v4i32.
13284   //   Concat upper and lower parts.
13285   //
13286   //   v4i32 -> v4i64
13287   //   Use vpunpckldq for 4 lower elements  v4i32 -> v2i64.
13288   //   Use vpunpckhdq for 4 upper elements  v4i32 -> v2i64.
13289   //   Concat upper and lower parts.
13290   //
13291
13292   if (((VT != MVT::v16i16) || (InVT != MVT::v16i8)) &&
13293       ((VT != MVT::v8i32) || (InVT != MVT::v8i16)) &&
13294       ((VT != MVT::v4i64) || (InVT != MVT::v4i32)))
13295     return SDValue();
13296
13297   if (Subtarget->hasInt256())
13298     return DAG.getNode(X86ISD::VZEXT, dl, VT, In);
13299
13300   SDValue ZeroVec = getZeroVector(InVT, Subtarget, DAG, dl);
13301   SDValue Undef = DAG.getUNDEF(InVT);
13302   bool NeedZero = Op.getOpcode() == ISD::ZERO_EXTEND;
13303   SDValue OpLo = getUnpackl(DAG, dl, InVT, In, NeedZero ? ZeroVec : Undef);
13304   SDValue OpHi = getUnpackh(DAG, dl, InVT, In, NeedZero ? ZeroVec : Undef);
13305
13306   MVT HVT = MVT::getVectorVT(VT.getVectorElementType(),
13307                              VT.getVectorNumElements()/2);
13308
13309   OpLo = DAG.getBitcast(HVT, OpLo);
13310   OpHi = DAG.getBitcast(HVT, OpHi);
13311
13312   return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
13313 }
13314
13315 static  SDValue LowerZERO_EXTEND_AVX512(SDValue Op,
13316                   const X86Subtarget *Subtarget, SelectionDAG &DAG) {
13317   MVT VT = Op->getSimpleValueType(0);
13318   SDValue In = Op->getOperand(0);
13319   MVT InVT = In.getSimpleValueType();
13320   SDLoc DL(Op);
13321   unsigned int NumElts = VT.getVectorNumElements();
13322   if (NumElts != 8 && NumElts != 16 && !Subtarget->hasBWI())
13323     return SDValue();
13324
13325   if (VT.is512BitVector() && InVT.getVectorElementType() != MVT::i1)
13326     return DAG.getNode(X86ISD::VZEXT, DL, VT, In);
13327
13328   assert(InVT.getVectorElementType() == MVT::i1);
13329   MVT ExtVT = NumElts == 8 ? MVT::v8i64 : MVT::v16i32;
13330   SDValue One =
13331    DAG.getConstant(APInt(ExtVT.getScalarSizeInBits(), 1), DL, ExtVT);
13332   SDValue Zero =
13333    DAG.getConstant(APInt::getNullValue(ExtVT.getScalarSizeInBits()), DL, ExtVT);
13334
13335   SDValue V = DAG.getNode(ISD::VSELECT, DL, ExtVT, In, One, Zero);
13336   if (VT.is512BitVector())
13337     return V;
13338   return DAG.getNode(X86ISD::VTRUNC, DL, VT, V);
13339 }
13340
13341 static SDValue LowerANY_EXTEND(SDValue Op, const X86Subtarget *Subtarget,
13342                                SelectionDAG &DAG) {
13343   if (Subtarget->hasFp256())
13344     if (SDValue Res = LowerAVXExtend(Op, DAG, Subtarget))
13345       return Res;
13346
13347   return SDValue();
13348 }
13349
13350 static SDValue LowerZERO_EXTEND(SDValue Op, const X86Subtarget *Subtarget,
13351                                 SelectionDAG &DAG) {
13352   SDLoc DL(Op);
13353   MVT VT = Op.getSimpleValueType();
13354   SDValue In = Op.getOperand(0);
13355   MVT SVT = In.getSimpleValueType();
13356
13357   if (VT.is512BitVector() || SVT.getVectorElementType() == MVT::i1)
13358     return LowerZERO_EXTEND_AVX512(Op, Subtarget, DAG);
13359
13360   if (Subtarget->hasFp256())
13361     if (SDValue Res = LowerAVXExtend(Op, DAG, Subtarget))
13362       return Res;
13363
13364   assert(!VT.is256BitVector() || !SVT.is128BitVector() ||
13365          VT.getVectorNumElements() != SVT.getVectorNumElements());
13366   return SDValue();
13367 }
13368
13369 static SDValue LowerTruncateVecI1(SDValue Op, SelectionDAG &DAG,
13370                                   const X86Subtarget *Subtarget) {
13371
13372   SDLoc DL(Op);
13373   MVT VT = Op.getSimpleValueType();
13374   SDValue In = Op.getOperand(0);
13375   MVT InVT = In.getSimpleValueType();
13376
13377   assert(VT.getVectorElementType() == MVT::i1 && "Unexected vector type.");
13378
13379   // Shift LSB to MSB and use VPMOVB2M - SKX.
13380   unsigned ShiftInx = InVT.getScalarSizeInBits() - 1;
13381   if ((InVT.is512BitVector() && InVT.getScalarSizeInBits() <= 16 &&
13382          Subtarget->hasBWI()) ||     // legal, will go to VPMOVB2M, VPMOVW2M
13383       ((InVT.is256BitVector() || InVT.is128BitVector()) &&
13384              InVT.getScalarSizeInBits() <= 16 && Subtarget->hasBWI() &&
13385              Subtarget->hasVLX())) { // legal, will go to VPMOVB2M, VPMOVW2M
13386     // Shift packed bytes not supported natively, bitcast to dword
13387     MVT ExtVT = MVT::getVectorVT(MVT::i16, InVT.getSizeInBits()/16);
13388     SDValue  ShiftNode = DAG.getNode(ISD::SHL, DL, ExtVT,
13389                                      DAG.getBitcast(ExtVT, In),
13390                                      DAG.getConstant(ShiftInx, DL, ExtVT));
13391     ShiftNode = DAG.getBitcast(InVT, ShiftNode);
13392     return DAG.getNode(X86ISD::CVT2MASK, DL, VT, ShiftNode);
13393   }
13394   if ((InVT.is512BitVector() && InVT.getScalarSizeInBits() >= 32 &&
13395          Subtarget->hasDQI()) ||  // legal, will go to VPMOVD2M, VPMOVQ2M
13396       ((InVT.is256BitVector() || InVT.is128BitVector()) &&
13397          InVT.getScalarSizeInBits() >= 32 && Subtarget->hasDQI() &&
13398          Subtarget->hasVLX())) {  // legal, will go to VPMOVD2M, VPMOVQ2M
13399
13400     SDValue  ShiftNode = DAG.getNode(ISD::SHL, DL, InVT, In,
13401                                      DAG.getConstant(ShiftInx, DL, InVT));
13402     return DAG.getNode(X86ISD::CVT2MASK, DL, VT, ShiftNode);
13403   }
13404
13405   // Shift LSB to MSB, extend if necessary and use TESTM.
13406   unsigned NumElts = InVT.getVectorNumElements();
13407   if (InVT.getSizeInBits() < 512 &&
13408       (InVT.getScalarType() == MVT::i8 || InVT.getScalarType() == MVT::i16 ||
13409        !Subtarget->hasVLX())) {
13410     assert((NumElts == 8 || NumElts == 16) && "Unexected vector type.");
13411
13412     // TESTD/Q should be used (if BW supported we use CVT2MASK above),
13413     // so vector should be extended to packed dword/qword.
13414     MVT ExtVT = MVT::getVectorVT(MVT::getIntegerVT(512/NumElts), NumElts);
13415     In = DAG.getNode(ISD::SIGN_EXTEND, DL, ExtVT, In);
13416     InVT = ExtVT;
13417     ShiftInx = InVT.getScalarSizeInBits() - 1;
13418   }
13419
13420   SDValue  ShiftNode = DAG.getNode(ISD::SHL, DL, InVT, In,
13421                                    DAG.getConstant(ShiftInx, DL, InVT));
13422   return DAG.getNode(X86ISD::TESTM, DL, VT, ShiftNode, ShiftNode);
13423 }
13424
13425 SDValue X86TargetLowering::LowerTRUNCATE(SDValue Op, SelectionDAG &DAG) const {
13426   SDLoc DL(Op);
13427   MVT VT = Op.getSimpleValueType();
13428   SDValue In = Op.getOperand(0);
13429   MVT InVT = In.getSimpleValueType();
13430
13431   if (VT == MVT::i1) {
13432     assert((InVT.isInteger() && (InVT.getSizeInBits() <= 64)) &&
13433            "Invalid scalar TRUNCATE operation");
13434     if (InVT.getSizeInBits() >= 32)
13435       return SDValue();
13436     In = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, In);
13437     return DAG.getNode(ISD::TRUNCATE, DL, VT, In);
13438   }
13439   assert(VT.getVectorNumElements() == InVT.getVectorNumElements() &&
13440          "Invalid TRUNCATE operation");
13441
13442   if (VT.getVectorElementType() == MVT::i1)
13443     return LowerTruncateVecI1(Op, DAG, Subtarget);
13444
13445   // vpmovqb/w/d, vpmovdb/w, vpmovwb
13446   if (Subtarget->hasAVX512()) {
13447     // word to byte only under BWI
13448     if (InVT == MVT::v16i16 && !Subtarget->hasBWI()) // v16i16 -> v16i8
13449       return DAG.getNode(X86ISD::VTRUNC, DL, VT,
13450                          DAG.getNode(X86ISD::VSEXT, DL, MVT::v16i32, In));
13451     return DAG.getNode(X86ISD::VTRUNC, DL, VT, In);
13452   }
13453   if ((VT == MVT::v4i32) && (InVT == MVT::v4i64)) {
13454     // On AVX2, v4i64 -> v4i32 becomes VPERMD.
13455     if (Subtarget->hasInt256()) {
13456       static const int ShufMask[] = {0, 2, 4, 6, -1, -1, -1, -1};
13457       In = DAG.getBitcast(MVT::v8i32, In);
13458       In = DAG.getVectorShuffle(MVT::v8i32, DL, In, DAG.getUNDEF(MVT::v8i32),
13459                                 ShufMask);
13460       return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, In,
13461                          DAG.getIntPtrConstant(0, DL));
13462     }
13463
13464     SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
13465                                DAG.getIntPtrConstant(0, DL));
13466     SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
13467                                DAG.getIntPtrConstant(2, DL));
13468     OpLo = DAG.getBitcast(MVT::v4i32, OpLo);
13469     OpHi = DAG.getBitcast(MVT::v4i32, OpHi);
13470     static const int ShufMask[] = {0, 2, 4, 6};
13471     return DAG.getVectorShuffle(VT, DL, OpLo, OpHi, ShufMask);
13472   }
13473
13474   if ((VT == MVT::v8i16) && (InVT == MVT::v8i32)) {
13475     // On AVX2, v8i32 -> v8i16 becomed PSHUFB.
13476     if (Subtarget->hasInt256()) {
13477       In = DAG.getBitcast(MVT::v32i8, In);
13478
13479       SmallVector<SDValue,32> pshufbMask;
13480       for (unsigned i = 0; i < 2; ++i) {
13481         pshufbMask.push_back(DAG.getConstant(0x0, DL, MVT::i8));
13482         pshufbMask.push_back(DAG.getConstant(0x1, DL, MVT::i8));
13483         pshufbMask.push_back(DAG.getConstant(0x4, DL, MVT::i8));
13484         pshufbMask.push_back(DAG.getConstant(0x5, DL, MVT::i8));
13485         pshufbMask.push_back(DAG.getConstant(0x8, DL, MVT::i8));
13486         pshufbMask.push_back(DAG.getConstant(0x9, DL, MVT::i8));
13487         pshufbMask.push_back(DAG.getConstant(0xc, DL, MVT::i8));
13488         pshufbMask.push_back(DAG.getConstant(0xd, DL, MVT::i8));
13489         for (unsigned j = 0; j < 8; ++j)
13490           pshufbMask.push_back(DAG.getConstant(0x80, DL, MVT::i8));
13491       }
13492       SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v32i8, pshufbMask);
13493       In = DAG.getNode(X86ISD::PSHUFB, DL, MVT::v32i8, In, BV);
13494       In = DAG.getBitcast(MVT::v4i64, In);
13495
13496       static const int ShufMask[] = {0,  2,  -1,  -1};
13497       In = DAG.getVectorShuffle(MVT::v4i64, DL,  In, DAG.getUNDEF(MVT::v4i64),
13498                                 &ShufMask[0]);
13499       In = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
13500                        DAG.getIntPtrConstant(0, DL));
13501       return DAG.getBitcast(VT, In);
13502     }
13503
13504     SDValue OpLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In,
13505                                DAG.getIntPtrConstant(0, DL));
13506
13507     SDValue OpHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In,
13508                                DAG.getIntPtrConstant(4, DL));
13509
13510     OpLo = DAG.getBitcast(MVT::v16i8, OpLo);
13511     OpHi = DAG.getBitcast(MVT::v16i8, OpHi);
13512
13513     // The PSHUFB mask:
13514     static const int ShufMask1[] = {0,  1,  4,  5,  8,  9, 12, 13,
13515                                    -1, -1, -1, -1, -1, -1, -1, -1};
13516
13517     SDValue Undef = DAG.getUNDEF(MVT::v16i8);
13518     OpLo = DAG.getVectorShuffle(MVT::v16i8, DL, OpLo, Undef, ShufMask1);
13519     OpHi = DAG.getVectorShuffle(MVT::v16i8, DL, OpHi, Undef, ShufMask1);
13520
13521     OpLo = DAG.getBitcast(MVT::v4i32, OpLo);
13522     OpHi = DAG.getBitcast(MVT::v4i32, OpHi);
13523
13524     // The MOVLHPS Mask:
13525     static const int ShufMask2[] = {0, 1, 4, 5};
13526     SDValue res = DAG.getVectorShuffle(MVT::v4i32, DL, OpLo, OpHi, ShufMask2);
13527     return DAG.getBitcast(MVT::v8i16, res);
13528   }
13529
13530   // Handle truncation of V256 to V128 using shuffles.
13531   if (!VT.is128BitVector() || !InVT.is256BitVector())
13532     return SDValue();
13533
13534   assert(Subtarget->hasFp256() && "256-bit vector without AVX!");
13535
13536   unsigned NumElems = VT.getVectorNumElements();
13537   MVT NVT = MVT::getVectorVT(VT.getVectorElementType(), NumElems * 2);
13538
13539   SmallVector<int, 16> MaskVec(NumElems * 2, -1);
13540   // Prepare truncation shuffle mask
13541   for (unsigned i = 0; i != NumElems; ++i)
13542     MaskVec[i] = i * 2;
13543   SDValue V = DAG.getVectorShuffle(NVT, DL, DAG.getBitcast(NVT, In),
13544                                    DAG.getUNDEF(NVT), &MaskVec[0]);
13545   return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, V,
13546                      DAG.getIntPtrConstant(0, DL));
13547 }
13548
13549 SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op,
13550                                            SelectionDAG &DAG) const {
13551   assert(!Op.getSimpleValueType().isVector());
13552
13553   std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
13554     /*IsSigned=*/ true, /*IsReplace=*/ false);
13555   SDValue FIST = Vals.first, StackSlot = Vals.second;
13556   // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
13557   if (!FIST.getNode())
13558     return Op;
13559
13560   if (StackSlot.getNode())
13561     // Load the result.
13562     return DAG.getLoad(Op.getValueType(), SDLoc(Op),
13563                        FIST, StackSlot, MachinePointerInfo(),
13564                        false, false, false, 0);
13565
13566   // The node is the result.
13567   return FIST;
13568 }
13569
13570 SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op,
13571                                            SelectionDAG &DAG) const {
13572   std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG,
13573     /*IsSigned=*/ false, /*IsReplace=*/ false);
13574   SDValue FIST = Vals.first, StackSlot = Vals.second;
13575   // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
13576   if (!FIST.getNode())
13577     return Op;
13578
13579   if (StackSlot.getNode())
13580     // Load the result.
13581     return DAG.getLoad(Op.getValueType(), SDLoc(Op),
13582                        FIST, StackSlot, MachinePointerInfo(),
13583                        false, false, false, 0);
13584
13585   // The node is the result.
13586   return FIST;
13587 }
13588
13589 static SDValue LowerFP_EXTEND(SDValue Op, SelectionDAG &DAG) {
13590   SDLoc DL(Op);
13591   MVT VT = Op.getSimpleValueType();
13592   SDValue In = Op.getOperand(0);
13593   MVT SVT = In.getSimpleValueType();
13594
13595   assert(SVT == MVT::v2f32 && "Only customize MVT::v2f32 type legalization!");
13596
13597   return DAG.getNode(X86ISD::VFPEXT, DL, VT,
13598                      DAG.getNode(ISD::CONCAT_VECTORS, DL, MVT::v4f32,
13599                                  In, DAG.getUNDEF(SVT)));
13600 }
13601
13602 /// The only differences between FABS and FNEG are the mask and the logic op.
13603 /// FNEG also has a folding opportunity for FNEG(FABS(x)).
13604 static SDValue LowerFABSorFNEG(SDValue Op, SelectionDAG &DAG) {
13605   assert((Op.getOpcode() == ISD::FABS || Op.getOpcode() == ISD::FNEG) &&
13606          "Wrong opcode for lowering FABS or FNEG.");
13607
13608   bool IsFABS = (Op.getOpcode() == ISD::FABS);
13609
13610   // If this is a FABS and it has an FNEG user, bail out to fold the combination
13611   // into an FNABS. We'll lower the FABS after that if it is still in use.
13612   if (IsFABS)
13613     for (SDNode *User : Op->uses())
13614       if (User->getOpcode() == ISD::FNEG)
13615         return Op;
13616
13617   SDLoc dl(Op);
13618   MVT VT = Op.getSimpleValueType();
13619
13620   bool IsF128 = (VT == MVT::f128);
13621
13622   // FIXME: Use function attribute "OptimizeForSize" and/or CodeGenOpt::Level to
13623   // decide if we should generate a 16-byte constant mask when we only need 4 or
13624   // 8 bytes for the scalar case.
13625
13626   MVT LogicVT;
13627   MVT EltVT;
13628   unsigned NumElts;
13629
13630   if (VT.isVector()) {
13631     LogicVT = VT;
13632     EltVT = VT.getVectorElementType();
13633     NumElts = VT.getVectorNumElements();
13634   } else if (IsF128) {
13635     // SSE instructions are used for optimized f128 logical operations.
13636     LogicVT = MVT::f128;
13637     EltVT = VT;
13638     NumElts = 1;
13639   } else {
13640     // There are no scalar bitwise logical SSE/AVX instructions, so we
13641     // generate a 16-byte vector constant and logic op even for the scalar case.
13642     // Using a 16-byte mask allows folding the load of the mask with
13643     // the logic op, so it can save (~4 bytes) on code size.
13644     LogicVT = (VT == MVT::f64) ? MVT::v2f64 : MVT::v4f32;
13645     EltVT = VT;
13646     NumElts = (VT == MVT::f64) ? 2 : 4;
13647   }
13648
13649   unsigned EltBits = EltVT.getSizeInBits();
13650   LLVMContext *Context = DAG.getContext();
13651   // For FABS, mask is 0x7f...; for FNEG, mask is 0x80...
13652   APInt MaskElt =
13653     IsFABS ? APInt::getSignedMaxValue(EltBits) : APInt::getSignBit(EltBits);
13654   Constant *C = ConstantInt::get(*Context, MaskElt);
13655   C = ConstantVector::getSplat(NumElts, C);
13656   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13657   SDValue CPIdx = DAG.getConstantPool(C, TLI.getPointerTy(DAG.getDataLayout()));
13658   unsigned Alignment = cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
13659   SDValue Mask =
13660       DAG.getLoad(LogicVT, dl, DAG.getEntryNode(), CPIdx,
13661                   MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
13662                   false, false, false, Alignment);
13663
13664   SDValue Op0 = Op.getOperand(0);
13665   bool IsFNABS = !IsFABS && (Op0.getOpcode() == ISD::FABS);
13666   unsigned LogicOp =
13667     IsFABS ? X86ISD::FAND : IsFNABS ? X86ISD::FOR : X86ISD::FXOR;
13668   SDValue Operand = IsFNABS ? Op0.getOperand(0) : Op0;
13669
13670   if (VT.isVector() || IsF128)
13671     return DAG.getNode(LogicOp, dl, LogicVT, Operand, Mask);
13672
13673   // For the scalar case extend to a 128-bit vector, perform the logic op,
13674   // and extract the scalar result back out.
13675   Operand = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LogicVT, Operand);
13676   SDValue LogicNode = DAG.getNode(LogicOp, dl, LogicVT, Operand, Mask);
13677   return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, LogicNode,
13678                      DAG.getIntPtrConstant(0, dl));
13679 }
13680
13681 static SDValue LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
13682   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
13683   LLVMContext *Context = DAG.getContext();
13684   SDValue Op0 = Op.getOperand(0);
13685   SDValue Op1 = Op.getOperand(1);
13686   SDLoc dl(Op);
13687   MVT VT = Op.getSimpleValueType();
13688   MVT SrcVT = Op1.getSimpleValueType();
13689   bool IsF128 = (VT == MVT::f128);
13690
13691   // If second operand is smaller, extend it first.
13692   if (SrcVT.bitsLT(VT)) {
13693     Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
13694     SrcVT = VT;
13695   }
13696   // And if it is bigger, shrink it first.
13697   if (SrcVT.bitsGT(VT)) {
13698     Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1, dl));
13699     SrcVT = VT;
13700   }
13701
13702   // At this point the operands and the result should have the same
13703   // type, and that won't be f80 since that is not custom lowered.
13704   assert((VT == MVT::f64 || VT == MVT::f32 || IsF128) &&
13705          "Unexpected type in LowerFCOPYSIGN");
13706
13707   const fltSemantics &Sem =
13708       VT == MVT::f64 ? APFloat::IEEEdouble :
13709           (IsF128 ? APFloat::IEEEquad : APFloat::IEEEsingle);
13710   const unsigned SizeInBits = VT.getSizeInBits();
13711
13712   SmallVector<Constant *, 4> CV(
13713       VT == MVT::f64 ? 2 : (IsF128 ? 1 : 4),
13714       ConstantFP::get(*Context, APFloat(Sem, APInt(SizeInBits, 0))));
13715
13716   // First, clear all bits but the sign bit from the second operand (sign).
13717   CV[0] = ConstantFP::get(*Context,
13718                           APFloat(Sem, APInt::getHighBitsSet(SizeInBits, 1)));
13719   Constant *C = ConstantVector::get(CV);
13720   auto PtrVT = TLI.getPointerTy(DAG.getDataLayout());
13721   SDValue CPIdx = DAG.getConstantPool(C, PtrVT, 16);
13722
13723   // Perform all logic operations as 16-byte vectors because there are no
13724   // scalar FP logic instructions in SSE. This allows load folding of the
13725   // constants into the logic instructions.
13726   MVT LogicVT = (VT == MVT::f64) ? MVT::v2f64 : (IsF128 ? MVT::f128 : MVT::v4f32);
13727   SDValue Mask1 =
13728       DAG.getLoad(LogicVT, dl, DAG.getEntryNode(), CPIdx,
13729                   MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
13730                   false, false, false, 16);
13731   if (!IsF128)
13732     Op1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LogicVT, Op1);
13733   SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, LogicVT, Op1, Mask1);
13734
13735   // Next, clear the sign bit from the first operand (magnitude).
13736   // If it's a constant, we can clear it here.
13737   if (ConstantFPSDNode *Op0CN = dyn_cast<ConstantFPSDNode>(Op0)) {
13738     APFloat APF = Op0CN->getValueAPF();
13739     // If the magnitude is a positive zero, the sign bit alone is enough.
13740     if (APF.isPosZero())
13741       return IsF128 ? SignBit :
13742           DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, SrcVT, SignBit,
13743                       DAG.getIntPtrConstant(0, dl));
13744     APF.clearSign();
13745     CV[0] = ConstantFP::get(*Context, APF);
13746   } else {
13747     CV[0] = ConstantFP::get(
13748         *Context,
13749         APFloat(Sem, APInt::getLowBitsSet(SizeInBits, SizeInBits - 1)));
13750   }
13751   C = ConstantVector::get(CV);
13752   CPIdx = DAG.getConstantPool(C, PtrVT, 16);
13753   SDValue Val =
13754       DAG.getLoad(LogicVT, dl, DAG.getEntryNode(), CPIdx,
13755                   MachinePointerInfo::getConstantPool(DAG.getMachineFunction()),
13756                   false, false, false, 16);
13757   // If the magnitude operand wasn't a constant, we need to AND out the sign.
13758   if (!isa<ConstantFPSDNode>(Op0)) {
13759     if (!IsF128)
13760       Op0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LogicVT, Op0);
13761     Val = DAG.getNode(X86ISD::FAND, dl, LogicVT, Op0, Val);
13762   }
13763   // OR the magnitude value with the sign bit.
13764   Val = DAG.getNode(X86ISD::FOR, dl, LogicVT, Val, SignBit);
13765   return IsF128 ? Val :
13766       DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, SrcVT, Val,
13767                   DAG.getIntPtrConstant(0, dl));
13768 }
13769
13770 static SDValue LowerFGETSIGN(SDValue Op, SelectionDAG &DAG) {
13771   SDValue N0 = Op.getOperand(0);
13772   SDLoc dl(Op);
13773   MVT VT = Op.getSimpleValueType();
13774
13775   // Lower ISD::FGETSIGN to (AND (X86ISD::FGETSIGNx86 ...) 1).
13776   SDValue xFGETSIGN = DAG.getNode(X86ISD::FGETSIGNx86, dl, VT, N0,
13777                                   DAG.getConstant(1, dl, VT));
13778   return DAG.getNode(ISD::AND, dl, VT, xFGETSIGN, DAG.getConstant(1, dl, VT));
13779 }
13780
13781 // Check whether an OR'd tree is PTEST-able.
13782 static SDValue LowerVectorAllZeroTest(SDValue Op, const X86Subtarget *Subtarget,
13783                                       SelectionDAG &DAG) {
13784   assert(Op.getOpcode() == ISD::OR && "Only check OR'd tree.");
13785
13786   if (!Subtarget->hasSSE41())
13787     return SDValue();
13788
13789   if (!Op->hasOneUse())
13790     return SDValue();
13791
13792   SDNode *N = Op.getNode();
13793   SDLoc DL(N);
13794
13795   SmallVector<SDValue, 8> Opnds;
13796   DenseMap<SDValue, unsigned> VecInMap;
13797   SmallVector<SDValue, 8> VecIns;
13798   EVT VT = MVT::Other;
13799
13800   // Recognize a special case where a vector is casted into wide integer to
13801   // test all 0s.
13802   Opnds.push_back(N->getOperand(0));
13803   Opnds.push_back(N->getOperand(1));
13804
13805   for (unsigned Slot = 0, e = Opnds.size(); Slot < e; ++Slot) {
13806     SmallVectorImpl<SDValue>::const_iterator I = Opnds.begin() + Slot;
13807     // BFS traverse all OR'd operands.
13808     if (I->getOpcode() == ISD::OR) {
13809       Opnds.push_back(I->getOperand(0));
13810       Opnds.push_back(I->getOperand(1));
13811       // Re-evaluate the number of nodes to be traversed.
13812       e += 2; // 2 more nodes (LHS and RHS) are pushed.
13813       continue;
13814     }
13815
13816     // Quit if a non-EXTRACT_VECTOR_ELT
13817     if (I->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
13818       return SDValue();
13819
13820     // Quit if without a constant index.
13821     SDValue Idx = I->getOperand(1);
13822     if (!isa<ConstantSDNode>(Idx))
13823       return SDValue();
13824
13825     SDValue ExtractedFromVec = I->getOperand(0);
13826     DenseMap<SDValue, unsigned>::iterator M = VecInMap.find(ExtractedFromVec);
13827     if (M == VecInMap.end()) {
13828       VT = ExtractedFromVec.getValueType();
13829       // Quit if not 128/256-bit vector.
13830       if (!VT.is128BitVector() && !VT.is256BitVector())
13831         return SDValue();
13832       // Quit if not the same type.
13833       if (VecInMap.begin() != VecInMap.end() &&
13834           VT != VecInMap.begin()->first.getValueType())
13835         return SDValue();
13836       M = VecInMap.insert(std::make_pair(ExtractedFromVec, 0)).first;
13837       VecIns.push_back(ExtractedFromVec);
13838     }
13839     M->second |= 1U << cast<ConstantSDNode>(Idx)->getZExtValue();
13840   }
13841
13842   assert((VT.is128BitVector() || VT.is256BitVector()) &&
13843          "Not extracted from 128-/256-bit vector.");
13844
13845   unsigned FullMask = (1U << VT.getVectorNumElements()) - 1U;
13846
13847   for (DenseMap<SDValue, unsigned>::const_iterator
13848         I = VecInMap.begin(), E = VecInMap.end(); I != E; ++I) {
13849     // Quit if not all elements are used.
13850     if (I->second != FullMask)
13851       return SDValue();
13852   }
13853
13854   MVT TestVT = VT.is128BitVector() ? MVT::v2i64 : MVT::v4i64;
13855
13856   // Cast all vectors into TestVT for PTEST.
13857   for (unsigned i = 0, e = VecIns.size(); i < e; ++i)
13858     VecIns[i] = DAG.getBitcast(TestVT, VecIns[i]);
13859
13860   // If more than one full vectors are evaluated, OR them first before PTEST.
13861   for (unsigned Slot = 0, e = VecIns.size(); e - Slot > 1; Slot += 2, e += 1) {
13862     // Each iteration will OR 2 nodes and append the result until there is only
13863     // 1 node left, i.e. the final OR'd value of all vectors.
13864     SDValue LHS = VecIns[Slot];
13865     SDValue RHS = VecIns[Slot + 1];
13866     VecIns.push_back(DAG.getNode(ISD::OR, DL, TestVT, LHS, RHS));
13867   }
13868
13869   return DAG.getNode(X86ISD::PTEST, DL, MVT::i32,
13870                      VecIns.back(), VecIns.back());
13871 }
13872
13873 /// \brief return true if \c Op has a use that doesn't just read flags.
13874 static bool hasNonFlagsUse(SDValue Op) {
13875   for (SDNode::use_iterator UI = Op->use_begin(), UE = Op->use_end(); UI != UE;
13876        ++UI) {
13877     SDNode *User = *UI;
13878     unsigned UOpNo = UI.getOperandNo();
13879     if (User->getOpcode() == ISD::TRUNCATE && User->hasOneUse()) {
13880       // Look pass truncate.
13881       UOpNo = User->use_begin().getOperandNo();
13882       User = *User->use_begin();
13883     }
13884
13885     if (User->getOpcode() != ISD::BRCOND && User->getOpcode() != ISD::SETCC &&
13886         !(User->getOpcode() == ISD::SELECT && UOpNo == 0))
13887       return true;
13888   }
13889   return false;
13890 }
13891
13892 /// Emit nodes that will be selected as "test Op0,Op0", or something
13893 /// equivalent.
13894 SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC, SDLoc dl,
13895                                     SelectionDAG &DAG) const {
13896   if (Op.getValueType() == MVT::i1) {
13897     SDValue ExtOp = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i8, Op);
13898     return DAG.getNode(X86ISD::CMP, dl, MVT::i32, ExtOp,
13899                        DAG.getConstant(0, dl, MVT::i8));
13900   }
13901   // CF and OF aren't always set the way we want. Determine which
13902   // of these we need.
13903   bool NeedCF = false;
13904   bool NeedOF = false;
13905   switch (X86CC) {
13906   default: break;
13907   case X86::COND_A: case X86::COND_AE:
13908   case X86::COND_B: case X86::COND_BE:
13909     NeedCF = true;
13910     break;
13911   case X86::COND_G: case X86::COND_GE:
13912   case X86::COND_L: case X86::COND_LE:
13913   case X86::COND_O: case X86::COND_NO: {
13914     // Check if we really need to set the
13915     // Overflow flag. If NoSignedWrap is present
13916     // that is not actually needed.
13917     switch (Op->getOpcode()) {
13918     case ISD::ADD:
13919     case ISD::SUB:
13920     case ISD::MUL:
13921     case ISD::SHL: {
13922       const auto *BinNode = cast<BinaryWithFlagsSDNode>(Op.getNode());
13923       if (BinNode->Flags.hasNoSignedWrap())
13924         break;
13925     }
13926     default:
13927       NeedOF = true;
13928       break;
13929     }
13930     break;
13931   }
13932   }
13933   // See if we can use the EFLAGS value from the operand instead of
13934   // doing a separate TEST. TEST always sets OF and CF to 0, so unless
13935   // we prove that the arithmetic won't overflow, we can't use OF or CF.
13936   if (Op.getResNo() != 0 || NeedOF || NeedCF) {
13937     // Emit a CMP with 0, which is the TEST pattern.
13938     //if (Op.getValueType() == MVT::i1)
13939     //  return DAG.getNode(X86ISD::CMP, dl, MVT::i1, Op,
13940     //                     DAG.getConstant(0, MVT::i1));
13941     return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
13942                        DAG.getConstant(0, dl, Op.getValueType()));
13943   }
13944   unsigned Opcode = 0;
13945   unsigned NumOperands = 0;
13946
13947   // Truncate operations may prevent the merge of the SETCC instruction
13948   // and the arithmetic instruction before it. Attempt to truncate the operands
13949   // of the arithmetic instruction and use a reduced bit-width instruction.
13950   bool NeedTruncation = false;
13951   SDValue ArithOp = Op;
13952   if (Op->getOpcode() == ISD::TRUNCATE && Op->hasOneUse()) {
13953     SDValue Arith = Op->getOperand(0);
13954     // Both the trunc and the arithmetic op need to have one user each.
13955     if (Arith->hasOneUse())
13956       switch (Arith.getOpcode()) {
13957         default: break;
13958         case ISD::ADD:
13959         case ISD::SUB:
13960         case ISD::AND:
13961         case ISD::OR:
13962         case ISD::XOR: {
13963           NeedTruncation = true;
13964           ArithOp = Arith;
13965         }
13966       }
13967   }
13968
13969   // NOTICE: In the code below we use ArithOp to hold the arithmetic operation
13970   // which may be the result of a CAST.  We use the variable 'Op', which is the
13971   // non-casted variable when we check for possible users.
13972   switch (ArithOp.getOpcode()) {
13973   case ISD::ADD:
13974     // Due to an isel shortcoming, be conservative if this add is likely to be
13975     // selected as part of a load-modify-store instruction. When the root node
13976     // in a match is a store, isel doesn't know how to remap non-chain non-flag
13977     // uses of other nodes in the match, such as the ADD in this case. This
13978     // leads to the ADD being left around and reselected, with the result being
13979     // two adds in the output.  Alas, even if none our users are stores, that
13980     // doesn't prove we're O.K.  Ergo, if we have any parents that aren't
13981     // CopyToReg or SETCC, eschew INC/DEC.  A better fix seems to require
13982     // climbing the DAG back to the root, and it doesn't seem to be worth the
13983     // effort.
13984     for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
13985          UE = Op.getNode()->use_end(); UI != UE; ++UI)
13986       if (UI->getOpcode() != ISD::CopyToReg &&
13987           UI->getOpcode() != ISD::SETCC &&
13988           UI->getOpcode() != ISD::STORE)
13989         goto default_case;
13990
13991     if (ConstantSDNode *C =
13992         dyn_cast<ConstantSDNode>(ArithOp.getNode()->getOperand(1))) {
13993       // An add of one will be selected as an INC.
13994       if (C->isOne() && !Subtarget->slowIncDec()) {
13995         Opcode = X86ISD::INC;
13996         NumOperands = 1;
13997         break;
13998       }
13999
14000       // An add of negative one (subtract of one) will be selected as a DEC.
14001       if (C->isAllOnesValue() && !Subtarget->slowIncDec()) {
14002         Opcode = X86ISD::DEC;
14003         NumOperands = 1;
14004         break;
14005       }
14006     }
14007
14008     // Otherwise use a regular EFLAGS-setting add.
14009     Opcode = X86ISD::ADD;
14010     NumOperands = 2;
14011     break;
14012   case ISD::SHL:
14013   case ISD::SRL:
14014     // If we have a constant logical shift that's only used in a comparison
14015     // against zero turn it into an equivalent AND. This allows turning it into
14016     // a TEST instruction later.
14017     if ((X86CC == X86::COND_E || X86CC == X86::COND_NE) && Op->hasOneUse() &&
14018         isa<ConstantSDNode>(Op->getOperand(1)) && !hasNonFlagsUse(Op)) {
14019       EVT VT = Op.getValueType();
14020       unsigned BitWidth = VT.getSizeInBits();
14021       unsigned ShAmt = Op->getConstantOperandVal(1);
14022       if (ShAmt >= BitWidth) // Avoid undefined shifts.
14023         break;
14024       APInt Mask = ArithOp.getOpcode() == ISD::SRL
14025                        ? APInt::getHighBitsSet(BitWidth, BitWidth - ShAmt)
14026                        : APInt::getLowBitsSet(BitWidth, BitWidth - ShAmt);
14027       if (!Mask.isSignedIntN(32)) // Avoid large immediates.
14028         break;
14029       SDValue New = DAG.getNode(ISD::AND, dl, VT, Op->getOperand(0),
14030                                 DAG.getConstant(Mask, dl, VT));
14031       DAG.ReplaceAllUsesWith(Op, New);
14032       Op = New;
14033     }
14034     break;
14035
14036   case ISD::AND:
14037     // If the primary and result isn't used, don't bother using X86ISD::AND,
14038     // because a TEST instruction will be better.
14039     if (!hasNonFlagsUse(Op))
14040       break;
14041     // FALL THROUGH
14042   case ISD::SUB:
14043   case ISD::OR:
14044   case ISD::XOR:
14045     // Due to the ISEL shortcoming noted above, be conservative if this op is
14046     // likely to be selected as part of a load-modify-store instruction.
14047     for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
14048            UE = Op.getNode()->use_end(); UI != UE; ++UI)
14049       if (UI->getOpcode() == ISD::STORE)
14050         goto default_case;
14051
14052     // Otherwise use a regular EFLAGS-setting instruction.
14053     switch (ArithOp.getOpcode()) {
14054     default: llvm_unreachable("unexpected operator!");
14055     case ISD::SUB: Opcode = X86ISD::SUB; break;
14056     case ISD::XOR: Opcode = X86ISD::XOR; break;
14057     case ISD::AND: Opcode = X86ISD::AND; break;
14058     case ISD::OR: {
14059       if (!NeedTruncation && (X86CC == X86::COND_E || X86CC == X86::COND_NE)) {
14060         SDValue EFLAGS = LowerVectorAllZeroTest(Op, Subtarget, DAG);
14061         if (EFLAGS.getNode())
14062           return EFLAGS;
14063       }
14064       Opcode = X86ISD::OR;
14065       break;
14066     }
14067     }
14068
14069     NumOperands = 2;
14070     break;
14071   case X86ISD::ADD:
14072   case X86ISD::SUB:
14073   case X86ISD::INC:
14074   case X86ISD::DEC:
14075   case X86ISD::OR:
14076   case X86ISD::XOR:
14077   case X86ISD::AND:
14078     return SDValue(Op.getNode(), 1);
14079   default:
14080   default_case:
14081     break;
14082   }
14083
14084   // If we found that truncation is beneficial, perform the truncation and
14085   // update 'Op'.
14086   if (NeedTruncation) {
14087     EVT VT = Op.getValueType();
14088     SDValue WideVal = Op->getOperand(0);
14089     EVT WideVT = WideVal.getValueType();
14090     unsigned ConvertedOp = 0;
14091     // Use a target machine opcode to prevent further DAGCombine
14092     // optimizations that may separate the arithmetic operations
14093     // from the setcc node.
14094     switch (WideVal.getOpcode()) {
14095       default: break;
14096       case ISD::ADD: ConvertedOp = X86ISD::ADD; break;
14097       case ISD::SUB: ConvertedOp = X86ISD::SUB; break;
14098       case ISD::AND: ConvertedOp = X86ISD::AND; break;
14099       case ISD::OR:  ConvertedOp = X86ISD::OR;  break;
14100       case ISD::XOR: ConvertedOp = X86ISD::XOR; break;
14101     }
14102
14103     if (ConvertedOp) {
14104       const TargetLowering &TLI = DAG.getTargetLoweringInfo();
14105       if (TLI.isOperationLegal(WideVal.getOpcode(), WideVT)) {
14106         SDValue V0 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(0));
14107         SDValue V1 = DAG.getNode(ISD::TRUNCATE, dl, VT, WideVal.getOperand(1));
14108         Op = DAG.getNode(ConvertedOp, dl, VT, V0, V1);
14109       }
14110     }
14111   }
14112
14113   if (Opcode == 0)
14114     // Emit a CMP with 0, which is the TEST pattern.
14115     return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
14116                        DAG.getConstant(0, dl, Op.getValueType()));
14117
14118   SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
14119   SmallVector<SDValue, 4> Ops(Op->op_begin(), Op->op_begin() + NumOperands);
14120
14121   SDValue New = DAG.getNode(Opcode, dl, VTs, Ops);
14122   DAG.ReplaceAllUsesWith(Op, New);
14123   return SDValue(New.getNode(), 1);
14124 }
14125
14126 /// Emit nodes that will be selected as "cmp Op0,Op1", or something
14127 /// equivalent.
14128 SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
14129                                    SDLoc dl, SelectionDAG &DAG) const {
14130   if (isNullConstant(Op1))
14131     return EmitTest(Op0, X86CC, dl, DAG);
14132
14133   assert(!(isa<ConstantSDNode>(Op1) && Op0.getValueType() == MVT::i1) &&
14134          "Unexpected comparison operation for MVT::i1 operands");
14135
14136   if ((Op0.getValueType() == MVT::i8 || Op0.getValueType() == MVT::i16 ||
14137        Op0.getValueType() == MVT::i32 || Op0.getValueType() == MVT::i64)) {
14138     // Do the comparison at i32 if it's smaller, besides the Atom case.
14139     // This avoids subregister aliasing issues. Keep the smaller reference
14140     // if we're optimizing for size, however, as that'll allow better folding
14141     // of memory operations.
14142     if (Op0.getValueType() != MVT::i32 && Op0.getValueType() != MVT::i64 &&
14143         !DAG.getMachineFunction().getFunction()->optForMinSize() &&
14144         !Subtarget->isAtom()) {
14145       unsigned ExtendOp =
14146           isX86CCUnsigned(X86CC) ? ISD::ZERO_EXTEND : ISD::SIGN_EXTEND;
14147       Op0 = DAG.getNode(ExtendOp, dl, MVT::i32, Op0);
14148       Op1 = DAG.getNode(ExtendOp, dl, MVT::i32, Op1);
14149     }
14150     // Use SUB instead of CMP to enable CSE between SUB and CMP.
14151     SDVTList VTs = DAG.getVTList(Op0.getValueType(), MVT::i32);
14152     SDValue Sub = DAG.getNode(X86ISD::SUB, dl, VTs,
14153                               Op0, Op1);
14154     return SDValue(Sub.getNode(), 1);
14155   }
14156   return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
14157 }
14158
14159 /// Convert a comparison if required by the subtarget.
14160 SDValue X86TargetLowering::ConvertCmpIfNecessary(SDValue Cmp,
14161                                                  SelectionDAG &DAG) const {
14162   // If the subtarget does not support the FUCOMI instruction, floating-point
14163   // comparisons have to be converted.
14164   if (Subtarget->hasCMov() ||
14165       Cmp.getOpcode() != X86ISD::CMP ||
14166       !Cmp.getOperand(0).getValueType().isFloatingPoint() ||
14167       !Cmp.getOperand(1).getValueType().isFloatingPoint())
14168     return Cmp;
14169
14170   // The instruction selector will select an FUCOM instruction instead of
14171   // FUCOMI, which writes the comparison result to FPSW instead of EFLAGS. Hence
14172   // build an SDNode sequence that transfers the result from FPSW into EFLAGS:
14173   // (X86sahf (trunc (srl (X86fp_stsw (trunc (X86cmp ...)), 8))))
14174   SDLoc dl(Cmp);
14175   SDValue TruncFPSW = DAG.getNode(ISD::TRUNCATE, dl, MVT::i16, Cmp);
14176   SDValue FNStSW = DAG.getNode(X86ISD::FNSTSW16r, dl, MVT::i16, TruncFPSW);
14177   SDValue Srl = DAG.getNode(ISD::SRL, dl, MVT::i16, FNStSW,
14178                             DAG.getConstant(8, dl, MVT::i8));
14179   SDValue TruncSrl = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Srl);
14180
14181   // Some 64-bit targets lack SAHF support, but they do support FCOMI.
14182   assert(Subtarget->hasLAHFSAHF() && "Target doesn't support SAHF or FCOMI?");
14183   return DAG.getNode(X86ISD::SAHF, dl, MVT::i32, TruncSrl);
14184 }
14185
14186 /// The minimum architected relative accuracy is 2^-12. We need one
14187 /// Newton-Raphson step to have a good float result (24 bits of precision).
14188 SDValue X86TargetLowering::getRsqrtEstimate(SDValue Op,
14189                                             DAGCombinerInfo &DCI,
14190                                             unsigned &RefinementSteps,
14191                                             bool &UseOneConstNR) const {
14192   EVT VT = Op.getValueType();
14193   const char *RecipOp;
14194
14195   // SSE1 has rsqrtss and rsqrtps. AVX adds a 256-bit variant for rsqrtps.
14196   // TODO: Add support for AVX512 (v16f32).
14197   // It is likely not profitable to do this for f64 because a double-precision
14198   // rsqrt estimate with refinement on x86 prior to FMA requires at least 16
14199   // instructions: convert to single, rsqrtss, convert back to double, refine
14200   // (3 steps = at least 13 insts). If an 'rsqrtsd' variant was added to the ISA
14201   // along with FMA, this could be a throughput win.
14202   if (VT == MVT::f32 && Subtarget->hasSSE1())
14203     RecipOp = "sqrtf";
14204   else if ((VT == MVT::v4f32 && Subtarget->hasSSE1()) ||
14205            (VT == MVT::v8f32 && Subtarget->hasAVX()))
14206     RecipOp = "vec-sqrtf";
14207   else
14208     return SDValue();
14209
14210   TargetRecip Recips = DCI.DAG.getTarget().Options.Reciprocals;
14211   if (!Recips.isEnabled(RecipOp))
14212     return SDValue();
14213
14214   RefinementSteps = Recips.getRefinementSteps(RecipOp);
14215   UseOneConstNR = false;
14216   return DCI.DAG.getNode(X86ISD::FRSQRT, SDLoc(Op), VT, Op);
14217 }
14218
14219 /// The minimum architected relative accuracy is 2^-12. We need one
14220 /// Newton-Raphson step to have a good float result (24 bits of precision).
14221 SDValue X86TargetLowering::getRecipEstimate(SDValue Op,
14222                                             DAGCombinerInfo &DCI,
14223                                             unsigned &RefinementSteps) const {
14224   EVT VT = Op.getValueType();
14225   const char *RecipOp;
14226
14227   // SSE1 has rcpss and rcpps. AVX adds a 256-bit variant for rcpps.
14228   // TODO: Add support for AVX512 (v16f32).
14229   // It is likely not profitable to do this for f64 because a double-precision
14230   // reciprocal estimate with refinement on x86 prior to FMA requires
14231   // 15 instructions: convert to single, rcpss, convert back to double, refine
14232   // (3 steps = 12 insts). If an 'rcpsd' variant was added to the ISA
14233   // along with FMA, this could be a throughput win.
14234   if (VT == MVT::f32 && Subtarget->hasSSE1())
14235     RecipOp = "divf";
14236   else if ((VT == MVT::v4f32 && Subtarget->hasSSE1()) ||
14237            (VT == MVT::v8f32 && Subtarget->hasAVX()))
14238     RecipOp = "vec-divf";
14239   else
14240     return SDValue();
14241
14242   TargetRecip Recips = DCI.DAG.getTarget().Options.Reciprocals;
14243   if (!Recips.isEnabled(RecipOp))
14244     return SDValue();
14245
14246   RefinementSteps = Recips.getRefinementSteps(RecipOp);
14247   return DCI.DAG.getNode(X86ISD::FRCP, SDLoc(Op), VT, Op);
14248 }
14249
14250 /// If we have at least two divisions that use the same divisor, convert to
14251 /// multplication by a reciprocal. This may need to be adjusted for a given
14252 /// CPU if a division's cost is not at least twice the cost of a multiplication.
14253 /// This is because we still need one division to calculate the reciprocal and
14254 /// then we need two multiplies by that reciprocal as replacements for the
14255 /// original divisions.
14256 unsigned X86TargetLowering::combineRepeatedFPDivisors() const {
14257   return 2;
14258 }
14259
14260 /// LowerToBT - Result of 'and' is compared against zero. Turn it into a BT node
14261 /// if it's possible.
14262 SDValue X86TargetLowering::LowerToBT(SDValue And, ISD::CondCode CC,
14263                                      SDLoc dl, SelectionDAG &DAG) const {
14264   SDValue Op0 = And.getOperand(0);
14265   SDValue Op1 = And.getOperand(1);
14266   if (Op0.getOpcode() == ISD::TRUNCATE)
14267     Op0 = Op0.getOperand(0);
14268   if (Op1.getOpcode() == ISD::TRUNCATE)
14269     Op1 = Op1.getOperand(0);
14270
14271   SDValue LHS, RHS;
14272   if (Op1.getOpcode() == ISD::SHL)
14273     std::swap(Op0, Op1);
14274   if (Op0.getOpcode() == ISD::SHL) {
14275     if (isOneConstant(Op0.getOperand(0))) {
14276         // If we looked past a truncate, check that it's only truncating away
14277         // known zeros.
14278         unsigned BitWidth = Op0.getValueSizeInBits();
14279         unsigned AndBitWidth = And.getValueSizeInBits();
14280         if (BitWidth > AndBitWidth) {
14281           APInt Zeros, Ones;
14282           DAG.computeKnownBits(Op0, Zeros, Ones);
14283           if (Zeros.countLeadingOnes() < BitWidth - AndBitWidth)
14284             return SDValue();
14285         }
14286         LHS = Op1;
14287         RHS = Op0.getOperand(1);
14288       }
14289   } else if (Op1.getOpcode() == ISD::Constant) {
14290     ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op1);
14291     uint64_t AndRHSVal = AndRHS->getZExtValue();
14292     SDValue AndLHS = Op0;
14293
14294     if (AndRHSVal == 1 && AndLHS.getOpcode() == ISD::SRL) {
14295       LHS = AndLHS.getOperand(0);
14296       RHS = AndLHS.getOperand(1);
14297     }
14298
14299     // Use BT if the immediate can't be encoded in a TEST instruction.
14300     if (!isUInt<32>(AndRHSVal) && isPowerOf2_64(AndRHSVal)) {
14301       LHS = AndLHS;
14302       RHS = DAG.getConstant(Log2_64_Ceil(AndRHSVal), dl, LHS.getValueType());
14303     }
14304   }
14305
14306   if (LHS.getNode()) {
14307     // If LHS is i8, promote it to i32 with any_extend.  There is no i8 BT
14308     // instruction.  Since the shift amount is in-range-or-undefined, we know
14309     // that doing a bittest on the i32 value is ok.  We extend to i32 because
14310     // the encoding for the i16 version is larger than the i32 version.
14311     // Also promote i16 to i32 for performance / code size reason.
14312     if (LHS.getValueType() == MVT::i8 ||
14313         LHS.getValueType() == MVT::i16)
14314       LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
14315
14316     // If the operand types disagree, extend the shift amount to match.  Since
14317     // BT ignores high bits (like shifts) we can use anyextend.
14318     if (LHS.getValueType() != RHS.getValueType())
14319       RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
14320
14321     SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
14322     X86::CondCode Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
14323     return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
14324                        DAG.getConstant(Cond, dl, MVT::i8), BT);
14325   }
14326
14327   return SDValue();
14328 }
14329
14330 /// \brief - Turns an ISD::CondCode into a value suitable for SSE floating point
14331 /// mask CMPs.
14332 static int translateX86FSETCC(ISD::CondCode SetCCOpcode, SDValue &Op0,
14333                               SDValue &Op1) {
14334   unsigned SSECC;
14335   bool Swap = false;
14336
14337   // SSE Condition code mapping:
14338   //  0 - EQ
14339   //  1 - LT
14340   //  2 - LE
14341   //  3 - UNORD
14342   //  4 - NEQ
14343   //  5 - NLT
14344   //  6 - NLE
14345   //  7 - ORD
14346   switch (SetCCOpcode) {
14347   default: llvm_unreachable("Unexpected SETCC condition");
14348   case ISD::SETOEQ:
14349   case ISD::SETEQ:  SSECC = 0; break;
14350   case ISD::SETOGT:
14351   case ISD::SETGT:  Swap = true; // Fallthrough
14352   case ISD::SETLT:
14353   case ISD::SETOLT: SSECC = 1; break;
14354   case ISD::SETOGE:
14355   case ISD::SETGE:  Swap = true; // Fallthrough
14356   case ISD::SETLE:
14357   case ISD::SETOLE: SSECC = 2; break;
14358   case ISD::SETUO:  SSECC = 3; break;
14359   case ISD::SETUNE:
14360   case ISD::SETNE:  SSECC = 4; break;
14361   case ISD::SETULE: Swap = true; // Fallthrough
14362   case ISD::SETUGE: SSECC = 5; break;
14363   case ISD::SETULT: Swap = true; // Fallthrough
14364   case ISD::SETUGT: SSECC = 6; break;
14365   case ISD::SETO:   SSECC = 7; break;
14366   case ISD::SETUEQ:
14367   case ISD::SETONE: SSECC = 8; break;
14368   }
14369   if (Swap)
14370     std::swap(Op0, Op1);
14371
14372   return SSECC;
14373 }
14374
14375 // Lower256IntVSETCC - Break a VSETCC 256-bit integer VSETCC into two new 128
14376 // ones, and then concatenate the result back.
14377 static SDValue Lower256IntVSETCC(SDValue Op, SelectionDAG &DAG) {
14378   MVT VT = Op.getSimpleValueType();
14379
14380   assert(VT.is256BitVector() && Op.getOpcode() == ISD::SETCC &&
14381          "Unsupported value type for operation");
14382
14383   unsigned NumElems = VT.getVectorNumElements();
14384   SDLoc dl(Op);
14385   SDValue CC = Op.getOperand(2);
14386
14387   // Extract the LHS vectors
14388   SDValue LHS = Op.getOperand(0);
14389   SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
14390   SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
14391
14392   // Extract the RHS vectors
14393   SDValue RHS = Op.getOperand(1);
14394   SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
14395   SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
14396
14397   // Issue the operation on the smaller types and concatenate the result back
14398   MVT EltVT = VT.getVectorElementType();
14399   MVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
14400   return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
14401                      DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1, CC),
14402                      DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2, CC));
14403 }
14404
14405 static SDValue LowerBoolVSETCC_AVX512(SDValue Op, SelectionDAG &DAG) {
14406   SDValue Op0 = Op.getOperand(0);
14407   SDValue Op1 = Op.getOperand(1);
14408   SDValue CC = Op.getOperand(2);
14409   MVT VT = Op.getSimpleValueType();
14410   SDLoc dl(Op);
14411
14412   assert(Op0.getSimpleValueType().getVectorElementType() == MVT::i1 &&
14413          "Unexpected type for boolean compare operation");
14414   ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
14415   SDValue NotOp0 = DAG.getNode(ISD::XOR, dl, VT, Op0,
14416                                DAG.getConstant(-1, dl, VT));
14417   SDValue NotOp1 = DAG.getNode(ISD::XOR, dl, VT, Op1,
14418                                DAG.getConstant(-1, dl, VT));
14419   switch (SetCCOpcode) {
14420   default: llvm_unreachable("Unexpected SETCC condition");
14421   case ISD::SETEQ:
14422     // (x == y) -> ~(x ^ y)
14423     return DAG.getNode(ISD::XOR, dl, VT,
14424                        DAG.getNode(ISD::XOR, dl, VT, Op0, Op1),
14425                        DAG.getConstant(-1, dl, VT));
14426   case ISD::SETNE:
14427     // (x != y) -> (x ^ y)
14428     return DAG.getNode(ISD::XOR, dl, VT, Op0, Op1);
14429   case ISD::SETUGT:
14430   case ISD::SETGT:
14431     // (x > y) -> (x & ~y)
14432     return DAG.getNode(ISD::AND, dl, VT, Op0, NotOp1);
14433   case ISD::SETULT:
14434   case ISD::SETLT:
14435     // (x < y) -> (~x & y)
14436     return DAG.getNode(ISD::AND, dl, VT, NotOp0, Op1);
14437   case ISD::SETULE:
14438   case ISD::SETLE:
14439     // (x <= y) -> (~x | y)
14440     return DAG.getNode(ISD::OR, dl, VT, NotOp0, Op1);
14441   case ISD::SETUGE:
14442   case ISD::SETGE:
14443     // (x >=y) -> (x | ~y)
14444     return DAG.getNode(ISD::OR, dl, VT, Op0, NotOp1);
14445   }
14446 }
14447
14448 static SDValue LowerIntVSETCC_AVX512(SDValue Op, SelectionDAG &DAG,
14449                                      const X86Subtarget *Subtarget) {
14450   SDValue Op0 = Op.getOperand(0);
14451   SDValue Op1 = Op.getOperand(1);
14452   SDValue CC = Op.getOperand(2);
14453   MVT VT = Op.getSimpleValueType();
14454   SDLoc dl(Op);
14455
14456   assert(Op0.getSimpleValueType().getVectorElementType().getSizeInBits() >= 8 &&
14457          Op.getSimpleValueType().getVectorElementType() == MVT::i1 &&
14458          "Cannot set masked compare for this operation");
14459
14460   ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
14461   unsigned  Opc = 0;
14462   bool Unsigned = false;
14463   bool Swap = false;
14464   unsigned SSECC;
14465   switch (SetCCOpcode) {
14466   default: llvm_unreachable("Unexpected SETCC condition");
14467   case ISD::SETNE:  SSECC = 4; break;
14468   case ISD::SETEQ:  Opc = X86ISD::PCMPEQM; break;
14469   case ISD::SETUGT: SSECC = 6; Unsigned = true; break;
14470   case ISD::SETLT:  Swap = true; //fall-through
14471   case ISD::SETGT:  Opc = X86ISD::PCMPGTM; break;
14472   case ISD::SETULT: SSECC = 1; Unsigned = true; break;
14473   case ISD::SETUGE: SSECC = 5; Unsigned = true; break; //NLT
14474   case ISD::SETGE:  Swap = true; SSECC = 2; break; // LE + swap
14475   case ISD::SETULE: Unsigned = true; //fall-through
14476   case ISD::SETLE:  SSECC = 2; break;
14477   }
14478
14479   if (Swap)
14480     std::swap(Op0, Op1);
14481   if (Opc)
14482     return DAG.getNode(Opc, dl, VT, Op0, Op1);
14483   Opc = Unsigned ? X86ISD::CMPMU: X86ISD::CMPM;
14484   return DAG.getNode(Opc, dl, VT, Op0, Op1,
14485                      DAG.getConstant(SSECC, dl, MVT::i8));
14486 }
14487
14488 /// \brief Try to turn a VSETULT into a VSETULE by modifying its second
14489 /// operand \p Op1.  If non-trivial (for example because it's not constant)
14490 /// return an empty value.
14491 static SDValue ChangeVSETULTtoVSETULE(SDLoc dl, SDValue Op1, SelectionDAG &DAG)
14492 {
14493   BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(Op1.getNode());
14494   if (!BV)
14495     return SDValue();
14496
14497   MVT VT = Op1.getSimpleValueType();
14498   MVT EVT = VT.getVectorElementType();
14499   unsigned n = VT.getVectorNumElements();
14500   SmallVector<SDValue, 8> ULTOp1;
14501
14502   for (unsigned i = 0; i < n; ++i) {
14503     ConstantSDNode *Elt = dyn_cast<ConstantSDNode>(BV->getOperand(i));
14504     if (!Elt || Elt->isOpaque() || Elt->getSimpleValueType(0) != EVT)
14505       return SDValue();
14506
14507     // Avoid underflow.
14508     APInt Val = Elt->getAPIntValue();
14509     if (Val == 0)
14510       return SDValue();
14511
14512     ULTOp1.push_back(DAG.getConstant(Val - 1, dl, EVT));
14513   }
14514
14515   return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, ULTOp1);
14516 }
14517
14518 static SDValue LowerVSETCC(SDValue Op, const X86Subtarget *Subtarget,
14519                            SelectionDAG &DAG) {
14520   SDValue Op0 = Op.getOperand(0);
14521   SDValue Op1 = Op.getOperand(1);
14522   SDValue CC = Op.getOperand(2);
14523   MVT VT = Op.getSimpleValueType();
14524   ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
14525   bool isFP = Op.getOperand(1).getSimpleValueType().isFloatingPoint();
14526   SDLoc dl(Op);
14527
14528   if (isFP) {
14529 #ifndef NDEBUG
14530     MVT EltVT = Op0.getSimpleValueType().getVectorElementType();
14531     assert(EltVT == MVT::f32 || EltVT == MVT::f64);
14532 #endif
14533
14534     unsigned SSECC = translateX86FSETCC(SetCCOpcode, Op0, Op1);
14535     unsigned Opc = X86ISD::CMPP;
14536     if (Subtarget->hasAVX512() && VT.getVectorElementType() == MVT::i1) {
14537       assert(VT.getVectorNumElements() <= 16);
14538       Opc = X86ISD::CMPM;
14539     }
14540     // In the two special cases we can't handle, emit two comparisons.
14541     if (SSECC == 8) {
14542       unsigned CC0, CC1;
14543       unsigned CombineOpc;
14544       if (SetCCOpcode == ISD::SETUEQ) {
14545         CC0 = 3; CC1 = 0; CombineOpc = ISD::OR;
14546       } else {
14547         assert(SetCCOpcode == ISD::SETONE);
14548         CC0 = 7; CC1 = 4; CombineOpc = ISD::AND;
14549       }
14550
14551       SDValue Cmp0 = DAG.getNode(Opc, dl, VT, Op0, Op1,
14552                                  DAG.getConstant(CC0, dl, MVT::i8));
14553       SDValue Cmp1 = DAG.getNode(Opc, dl, VT, Op0, Op1,
14554                                  DAG.getConstant(CC1, dl, MVT::i8));
14555       return DAG.getNode(CombineOpc, dl, VT, Cmp0, Cmp1);
14556     }
14557     // Handle all other FP comparisons here.
14558     return DAG.getNode(Opc, dl, VT, Op0, Op1,
14559                        DAG.getConstant(SSECC, dl, MVT::i8));
14560   }
14561
14562   MVT VTOp0 = Op0.getSimpleValueType();
14563   assert(VTOp0 == Op1.getSimpleValueType() &&
14564          "Expected operands with same type!");
14565   assert(VT.getVectorNumElements() == VTOp0.getVectorNumElements() &&
14566          "Invalid number of packed elements for source and destination!");
14567
14568   if (VT.is128BitVector() && VTOp0.is256BitVector()) {
14569     // On non-AVX512 targets, a vector of MVT::i1 is promoted by the type
14570     // legalizer to a wider vector type.  In the case of 'vsetcc' nodes, the
14571     // legalizer firstly checks if the first operand in input to the setcc has
14572     // a legal type. If so, then it promotes the return type to that same type.
14573     // Otherwise, the return type is promoted to the 'next legal type' which,
14574     // for a vector of MVT::i1 is always a 128-bit integer vector type.
14575     //
14576     // We reach this code only if the following two conditions are met:
14577     // 1. Both return type and operand type have been promoted to wider types
14578     //    by the type legalizer.
14579     // 2. The original operand type has been promoted to a 256-bit vector.
14580     //
14581     // Note that condition 2. only applies for AVX targets.
14582     SDValue NewOp = DAG.getSetCC(dl, VTOp0, Op0, Op1, SetCCOpcode);
14583     return DAG.getZExtOrTrunc(NewOp, dl, VT);
14584   }
14585
14586   // The non-AVX512 code below works under the assumption that source and
14587   // destination types are the same.
14588   assert((Subtarget->hasAVX512() || (VT == VTOp0)) &&
14589          "Value types for source and destination must be the same!");
14590
14591   // Break 256-bit integer vector compare into smaller ones.
14592   if (VT.is256BitVector() && !Subtarget->hasInt256())
14593     return Lower256IntVSETCC(Op, DAG);
14594
14595   MVT OpVT = Op1.getSimpleValueType();
14596   if (OpVT.getVectorElementType() == MVT::i1)
14597     return LowerBoolVSETCC_AVX512(Op, DAG);
14598
14599   bool MaskResult = (VT.getVectorElementType() == MVT::i1);
14600   if (Subtarget->hasAVX512()) {
14601     if (Op1.getSimpleValueType().is512BitVector() ||
14602         (Subtarget->hasBWI() && Subtarget->hasVLX()) ||
14603         (MaskResult && OpVT.getVectorElementType().getSizeInBits() >= 32))
14604       return LowerIntVSETCC_AVX512(Op, DAG, Subtarget);
14605
14606     // In AVX-512 architecture setcc returns mask with i1 elements,
14607     // But there is no compare instruction for i8 and i16 elements in KNL.
14608     // We are not talking about 512-bit operands in this case, these
14609     // types are illegal.
14610     if (MaskResult &&
14611         (OpVT.getVectorElementType().getSizeInBits() < 32 &&
14612          OpVT.getVectorElementType().getSizeInBits() >= 8))
14613       return DAG.getNode(ISD::TRUNCATE, dl, VT,
14614                          DAG.getNode(ISD::SETCC, dl, OpVT, Op0, Op1, CC));
14615   }
14616
14617   // Lower using XOP integer comparisons.
14618   if ((VT == MVT::v16i8 || VT == MVT::v8i16 ||
14619        VT == MVT::v4i32 || VT == MVT::v2i64) && Subtarget->hasXOP()) {
14620     // Translate compare code to XOP PCOM compare mode.
14621     unsigned CmpMode = 0;
14622     switch (SetCCOpcode) {
14623     default: llvm_unreachable("Unexpected SETCC condition");
14624     case ISD::SETULT:
14625     case ISD::SETLT: CmpMode = 0x00; break;
14626     case ISD::SETULE:
14627     case ISD::SETLE: CmpMode = 0x01; break;
14628     case ISD::SETUGT:
14629     case ISD::SETGT: CmpMode = 0x02; break;
14630     case ISD::SETUGE:
14631     case ISD::SETGE: CmpMode = 0x03; break;
14632     case ISD::SETEQ: CmpMode = 0x04; break;
14633     case ISD::SETNE: CmpMode = 0x05; break;
14634     }
14635
14636     // Are we comparing unsigned or signed integers?
14637     unsigned Opc = ISD::isUnsignedIntSetCC(SetCCOpcode)
14638       ? X86ISD::VPCOMU : X86ISD::VPCOM;
14639
14640     return DAG.getNode(Opc, dl, VT, Op0, Op1,
14641                        DAG.getConstant(CmpMode, dl, MVT::i8));
14642   }
14643
14644   // We are handling one of the integer comparisons here.  Since SSE only has
14645   // GT and EQ comparisons for integer, swapping operands and multiple
14646   // operations may be required for some comparisons.
14647   unsigned Opc;
14648   bool Swap = false, Invert = false, FlipSigns = false, MinMax = false;
14649   bool Subus = false;
14650
14651   switch (SetCCOpcode) {
14652   default: llvm_unreachable("Unexpected SETCC condition");
14653   case ISD::SETNE:  Invert = true;
14654   case ISD::SETEQ:  Opc = X86ISD::PCMPEQ; break;
14655   case ISD::SETLT:  Swap = true;
14656   case ISD::SETGT:  Opc = X86ISD::PCMPGT; break;
14657   case ISD::SETGE:  Swap = true;
14658   case ISD::SETLE:  Opc = X86ISD::PCMPGT;
14659                     Invert = true; break;
14660   case ISD::SETULT: Swap = true;
14661   case ISD::SETUGT: Opc = X86ISD::PCMPGT;
14662                     FlipSigns = true; break;
14663   case ISD::SETUGE: Swap = true;
14664   case ISD::SETULE: Opc = X86ISD::PCMPGT;
14665                     FlipSigns = true; Invert = true; break;
14666   }
14667
14668   // Special case: Use min/max operations for SETULE/SETUGE
14669   MVT VET = VT.getVectorElementType();
14670   bool hasMinMax =
14671        (Subtarget->hasSSE41() && (VET >= MVT::i8 && VET <= MVT::i32))
14672     || (Subtarget->hasSSE2()  && (VET == MVT::i8));
14673
14674   if (hasMinMax) {
14675     switch (SetCCOpcode) {
14676     default: break;
14677     case ISD::SETULE: Opc = ISD::UMIN; MinMax = true; break;
14678     case ISD::SETUGE: Opc = ISD::UMAX; MinMax = true; break;
14679     }
14680
14681     if (MinMax) { Swap = false; Invert = false; FlipSigns = false; }
14682   }
14683
14684   bool hasSubus = Subtarget->hasSSE2() && (VET == MVT::i8 || VET == MVT::i16);
14685   if (!MinMax && hasSubus) {
14686     // As another special case, use PSUBUS[BW] when it's profitable. E.g. for
14687     // Op0 u<= Op1:
14688     //   t = psubus Op0, Op1
14689     //   pcmpeq t, <0..0>
14690     switch (SetCCOpcode) {
14691     default: break;
14692     case ISD::SETULT: {
14693       // If the comparison is against a constant we can turn this into a
14694       // setule.  With psubus, setule does not require a swap.  This is
14695       // beneficial because the constant in the register is no longer
14696       // destructed as the destination so it can be hoisted out of a loop.
14697       // Only do this pre-AVX since vpcmp* is no longer destructive.
14698       if (Subtarget->hasAVX())
14699         break;
14700       SDValue ULEOp1 = ChangeVSETULTtoVSETULE(dl, Op1, DAG);
14701       if (ULEOp1.getNode()) {
14702         Op1 = ULEOp1;
14703         Subus = true; Invert = false; Swap = false;
14704       }
14705       break;
14706     }
14707     // Psubus is better than flip-sign because it requires no inversion.
14708     case ISD::SETUGE: Subus = true; Invert = false; Swap = true;  break;
14709     case ISD::SETULE: Subus = true; Invert = false; Swap = false; break;
14710     }
14711
14712     if (Subus) {
14713       Opc = X86ISD::SUBUS;
14714       FlipSigns = false;
14715     }
14716   }
14717
14718   if (Swap)
14719     std::swap(Op0, Op1);
14720
14721   // Check that the operation in question is available (most are plain SSE2,
14722   // but PCMPGTQ and PCMPEQQ have different requirements).
14723   if (VT == MVT::v2i64) {
14724     if (Opc == X86ISD::PCMPGT && !Subtarget->hasSSE42()) {
14725       assert(Subtarget->hasSSE2() && "Don't know how to lower!");
14726
14727       // First cast everything to the right type.
14728       Op0 = DAG.getBitcast(MVT::v4i32, Op0);
14729       Op1 = DAG.getBitcast(MVT::v4i32, Op1);
14730
14731       // Since SSE has no unsigned integer comparisons, we need to flip the sign
14732       // bits of the inputs before performing those operations. The lower
14733       // compare is always unsigned.
14734       SDValue SB;
14735       if (FlipSigns) {
14736         SB = DAG.getConstant(0x80000000U, dl, MVT::v4i32);
14737       } else {
14738         SDValue Sign = DAG.getConstant(0x80000000U, dl, MVT::i32);
14739         SDValue Zero = DAG.getConstant(0x00000000U, dl, MVT::i32);
14740         SB = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32,
14741                          Sign, Zero, Sign, Zero);
14742       }
14743       Op0 = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Op0, SB);
14744       Op1 = DAG.getNode(ISD::XOR, dl, MVT::v4i32, Op1, SB);
14745
14746       // Emulate PCMPGTQ with (hi1 > hi2) | ((hi1 == hi2) & (lo1 > lo2))
14747       SDValue GT = DAG.getNode(X86ISD::PCMPGT, dl, MVT::v4i32, Op0, Op1);
14748       SDValue EQ = DAG.getNode(X86ISD::PCMPEQ, dl, MVT::v4i32, Op0, Op1);
14749
14750       // Create masks for only the low parts/high parts of the 64 bit integers.
14751       static const int MaskHi[] = { 1, 1, 3, 3 };
14752       static const int MaskLo[] = { 0, 0, 2, 2 };
14753       SDValue EQHi = DAG.getVectorShuffle(MVT::v4i32, dl, EQ, EQ, MaskHi);
14754       SDValue GTLo = DAG.getVectorShuffle(MVT::v4i32, dl, GT, GT, MaskLo);
14755       SDValue GTHi = DAG.getVectorShuffle(MVT::v4i32, dl, GT, GT, MaskHi);
14756
14757       SDValue Result = DAG.getNode(ISD::AND, dl, MVT::v4i32, EQHi, GTLo);
14758       Result = DAG.getNode(ISD::OR, dl, MVT::v4i32, Result, GTHi);
14759
14760       if (Invert)
14761         Result = DAG.getNOT(dl, Result, MVT::v4i32);
14762
14763       return DAG.getBitcast(VT, Result);
14764     }
14765
14766     if (Opc == X86ISD::PCMPEQ && !Subtarget->hasSSE41()) {
14767       // If pcmpeqq is missing but pcmpeqd is available synthesize pcmpeqq with
14768       // pcmpeqd + pshufd + pand.
14769       assert(Subtarget->hasSSE2() && !FlipSigns && "Don't know how to lower!");
14770
14771       // First cast everything to the right type.
14772       Op0 = DAG.getBitcast(MVT::v4i32, Op0);
14773       Op1 = DAG.getBitcast(MVT::v4i32, Op1);
14774
14775       // Do the compare.
14776       SDValue Result = DAG.getNode(Opc, dl, MVT::v4i32, Op0, Op1);
14777
14778       // Make sure the lower and upper halves are both all-ones.
14779       static const int Mask[] = { 1, 0, 3, 2 };
14780       SDValue Shuf = DAG.getVectorShuffle(MVT::v4i32, dl, Result, Result, Mask);
14781       Result = DAG.getNode(ISD::AND, dl, MVT::v4i32, Result, Shuf);
14782
14783       if (Invert)
14784         Result = DAG.getNOT(dl, Result, MVT::v4i32);
14785
14786       return DAG.getBitcast(VT, Result);
14787     }
14788   }
14789
14790   // Since SSE has no unsigned integer comparisons, we need to flip the sign
14791   // bits of the inputs before performing those operations.
14792   if (FlipSigns) {
14793     MVT EltVT = VT.getVectorElementType();
14794     SDValue SB = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()), dl,
14795                                  VT);
14796     Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SB);
14797     Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SB);
14798   }
14799
14800   SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
14801
14802   // If the logical-not of the result is required, perform that now.
14803   if (Invert)
14804     Result = DAG.getNOT(dl, Result, VT);
14805
14806   if (MinMax)
14807     Result = DAG.getNode(X86ISD::PCMPEQ, dl, VT, Op0, Result);
14808
14809   if (Subus)
14810     Result = DAG.getNode(X86ISD::PCMPEQ, dl, VT, Result,
14811                          getZeroVector(VT, Subtarget, DAG, dl));
14812
14813   return Result;
14814 }
14815
14816 SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) const {
14817
14818   MVT VT = Op.getSimpleValueType();
14819
14820   if (VT.isVector()) return LowerVSETCC(Op, Subtarget, DAG);
14821
14822   assert(((!Subtarget->hasAVX512() && VT == MVT::i8) || (VT == MVT::i1))
14823          && "SetCC type must be 8-bit or 1-bit integer");
14824   SDValue Op0 = Op.getOperand(0);
14825   SDValue Op1 = Op.getOperand(1);
14826   SDLoc dl(Op);
14827   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
14828
14829   // Optimize to BT if possible.
14830   // Lower (X & (1 << N)) == 0 to BT(X, N).
14831   // Lower ((X >>u N) & 1) != 0 to BT(X, N).
14832   // Lower ((X >>s N) & 1) != 0 to BT(X, N).
14833   if (Op0.getOpcode() == ISD::AND && Op0.hasOneUse() &&
14834       isNullConstant(Op1) &&
14835       (CC == ISD::SETEQ || CC == ISD::SETNE)) {
14836     if (SDValue NewSetCC = LowerToBT(Op0, CC, dl, DAG)) {
14837       if (VT == MVT::i1)
14838         return DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, NewSetCC);
14839       return NewSetCC;
14840     }
14841   }
14842
14843   // Look for X == 0, X == 1, X != 0, or X != 1.  We can simplify some forms of
14844   // these.
14845   if ((isOneConstant(Op1) || isNullConstant(Op1)) &&
14846       (CC == ISD::SETEQ || CC == ISD::SETNE)) {
14847
14848     // If the input is a setcc, then reuse the input setcc or use a new one with
14849     // the inverted condition.
14850     if (Op0.getOpcode() == X86ISD::SETCC) {
14851       X86::CondCode CCode = (X86::CondCode)Op0.getConstantOperandVal(0);
14852       bool Invert = (CC == ISD::SETNE) ^ isNullConstant(Op1);
14853       if (!Invert)
14854         return Op0;
14855
14856       CCode = X86::GetOppositeBranchCondition(CCode);
14857       SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
14858                                   DAG.getConstant(CCode, dl, MVT::i8),
14859                                   Op0.getOperand(1));
14860       if (VT == MVT::i1)
14861         return DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, SetCC);
14862       return SetCC;
14863     }
14864   }
14865   if ((Op0.getValueType() == MVT::i1) && isOneConstant(Op1) &&
14866       (CC == ISD::SETEQ || CC == ISD::SETNE)) {
14867
14868     ISD::CondCode NewCC = ISD::getSetCCInverse(CC, true);
14869     return DAG.getSetCC(dl, VT, Op0, DAG.getConstant(0, dl, MVT::i1), NewCC);
14870   }
14871
14872   bool isFP = Op1.getSimpleValueType().isFloatingPoint();
14873   unsigned X86CC = TranslateX86CC(CC, dl, isFP, Op0, Op1, DAG);
14874   if (X86CC == X86::COND_INVALID)
14875     return SDValue();
14876
14877   SDValue EFLAGS = EmitCmp(Op0, Op1, X86CC, dl, DAG);
14878   EFLAGS = ConvertCmpIfNecessary(EFLAGS, DAG);
14879   SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
14880                               DAG.getConstant(X86CC, dl, MVT::i8), EFLAGS);
14881   if (VT == MVT::i1)
14882     return DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, SetCC);
14883   return SetCC;
14884 }
14885
14886 SDValue X86TargetLowering::LowerSETCCE(SDValue Op, SelectionDAG &DAG) const {
14887   SDValue LHS = Op.getOperand(0);
14888   SDValue RHS = Op.getOperand(1);
14889   SDValue Carry = Op.getOperand(2);
14890   SDValue Cond = Op.getOperand(3);
14891   SDLoc DL(Op);
14892
14893   assert(LHS.getSimpleValueType().isInteger() && "SETCCE is integer only.");
14894   X86::CondCode CC = TranslateIntegerX86CC(cast<CondCodeSDNode>(Cond)->get());
14895
14896   assert(Carry.getOpcode() != ISD::CARRY_FALSE);
14897   SDVTList VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
14898   SDValue Cmp = DAG.getNode(X86ISD::SBB, DL, VTs, LHS, RHS, Carry);
14899   return DAG.getNode(X86ISD::SETCC, DL, Op.getValueType(),
14900                      DAG.getConstant(CC, DL, MVT::i8), Cmp.getValue(1));
14901 }
14902
14903 // isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
14904 static bool isX86LogicalCmp(SDValue Op) {
14905   unsigned Opc = Op.getNode()->getOpcode();
14906   if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI ||
14907       Opc == X86ISD::SAHF)
14908     return true;
14909   if (Op.getResNo() == 1 &&
14910       (Opc == X86ISD::ADD ||
14911        Opc == X86ISD::SUB ||
14912        Opc == X86ISD::ADC ||
14913        Opc == X86ISD::SBB ||
14914        Opc == X86ISD::SMUL ||
14915        Opc == X86ISD::UMUL ||
14916        Opc == X86ISD::INC ||
14917        Opc == X86ISD::DEC ||
14918        Opc == X86ISD::OR ||
14919        Opc == X86ISD::XOR ||
14920        Opc == X86ISD::AND))
14921     return true;
14922
14923   if (Op.getResNo() == 2 && Opc == X86ISD::UMUL)
14924     return true;
14925
14926   return false;
14927 }
14928
14929 static bool isTruncWithZeroHighBitsInput(SDValue V, SelectionDAG &DAG) {
14930   if (V.getOpcode() != ISD::TRUNCATE)
14931     return false;
14932
14933   SDValue VOp0 = V.getOperand(0);
14934   unsigned InBits = VOp0.getValueSizeInBits();
14935   unsigned Bits = V.getValueSizeInBits();
14936   return DAG.MaskedValueIsZero(VOp0, APInt::getHighBitsSet(InBits,InBits-Bits));
14937 }
14938
14939 SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) const {
14940   bool addTest = true;
14941   SDValue Cond  = Op.getOperand(0);
14942   SDValue Op1 = Op.getOperand(1);
14943   SDValue Op2 = Op.getOperand(2);
14944   SDLoc DL(Op);
14945   MVT VT = Op1.getSimpleValueType();
14946   SDValue CC;
14947
14948   // Lower FP selects into a CMP/AND/ANDN/OR sequence when the necessary SSE ops
14949   // are available or VBLENDV if AVX is available.
14950   // Otherwise FP cmovs get lowered into a less efficient branch sequence later.
14951   if (Cond.getOpcode() == ISD::SETCC &&
14952       ((Subtarget->hasSSE2() && (VT == MVT::f32 || VT == MVT::f64)) ||
14953        (Subtarget->hasSSE1() && VT == MVT::f32)) &&
14954       VT == Cond.getOperand(0).getSimpleValueType() && Cond->hasOneUse()) {
14955     SDValue CondOp0 = Cond.getOperand(0), CondOp1 = Cond.getOperand(1);
14956     int SSECC = translateX86FSETCC(
14957         cast<CondCodeSDNode>(Cond.getOperand(2))->get(), CondOp0, CondOp1);
14958
14959     if (SSECC != 8) {
14960       if (Subtarget->hasAVX512()) {
14961         SDValue Cmp = DAG.getNode(X86ISD::FSETCC, DL, MVT::i1, CondOp0, CondOp1,
14962                                   DAG.getConstant(SSECC, DL, MVT::i8));
14963         return DAG.getNode(X86ISD::SELECT, DL, VT, Cmp, Op1, Op2);
14964       }
14965
14966       SDValue Cmp = DAG.getNode(X86ISD::FSETCC, DL, VT, CondOp0, CondOp1,
14967                                 DAG.getConstant(SSECC, DL, MVT::i8));
14968
14969       // If we have AVX, we can use a variable vector select (VBLENDV) instead
14970       // of 3 logic instructions for size savings and potentially speed.
14971       // Unfortunately, there is no scalar form of VBLENDV.
14972
14973       // If either operand is a constant, don't try this. We can expect to
14974       // optimize away at least one of the logic instructions later in that
14975       // case, so that sequence would be faster than a variable blend.
14976
14977       // BLENDV was introduced with SSE 4.1, but the 2 register form implicitly
14978       // uses XMM0 as the selection register. That may need just as many
14979       // instructions as the AND/ANDN/OR sequence due to register moves, so
14980       // don't bother.
14981
14982       if (Subtarget->hasAVX() &&
14983           !isa<ConstantFPSDNode>(Op1) && !isa<ConstantFPSDNode>(Op2)) {
14984
14985         // Convert to vectors, do a VSELECT, and convert back to scalar.
14986         // All of the conversions should be optimized away.
14987
14988         MVT VecVT = VT == MVT::f32 ? MVT::v4f32 : MVT::v2f64;
14989         SDValue VOp1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VecVT, Op1);
14990         SDValue VOp2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VecVT, Op2);
14991         SDValue VCmp = DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VecVT, Cmp);
14992
14993         MVT VCmpVT = VT == MVT::f32 ? MVT::v4i32 : MVT::v2i64;
14994         VCmp = DAG.getBitcast(VCmpVT, VCmp);
14995
14996         SDValue VSel = DAG.getNode(ISD::VSELECT, DL, VecVT, VCmp, VOp1, VOp2);
14997
14998         return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VT,
14999                            VSel, DAG.getIntPtrConstant(0, DL));
15000       }
15001       SDValue AndN = DAG.getNode(X86ISD::FANDN, DL, VT, Cmp, Op2);
15002       SDValue And = DAG.getNode(X86ISD::FAND, DL, VT, Cmp, Op1);
15003       return DAG.getNode(X86ISD::FOR, DL, VT, AndN, And);
15004     }
15005   }
15006
15007   if (VT.isVector() && VT.getVectorElementType() == MVT::i1) {
15008     SDValue Op1Scalar;
15009     if (ISD::isBuildVectorOfConstantSDNodes(Op1.getNode()))
15010       Op1Scalar = ConvertI1VectorToInteger(Op1, DAG);
15011     else if (Op1.getOpcode() == ISD::BITCAST && Op1.getOperand(0))
15012       Op1Scalar = Op1.getOperand(0);
15013     SDValue Op2Scalar;
15014     if (ISD::isBuildVectorOfConstantSDNodes(Op2.getNode()))
15015       Op2Scalar = ConvertI1VectorToInteger(Op2, DAG);
15016     else if (Op2.getOpcode() == ISD::BITCAST && Op2.getOperand(0))
15017       Op2Scalar = Op2.getOperand(0);
15018     if (Op1Scalar.getNode() && Op2Scalar.getNode()) {
15019       SDValue newSelect = DAG.getNode(ISD::SELECT, DL,
15020                                       Op1Scalar.getValueType(),
15021                                       Cond, Op1Scalar, Op2Scalar);
15022       if (newSelect.getValueSizeInBits() == VT.getSizeInBits())
15023         return DAG.getBitcast(VT, newSelect);
15024       SDValue ExtVec = DAG.getBitcast(MVT::v8i1, newSelect);
15025       return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, ExtVec,
15026                          DAG.getIntPtrConstant(0, DL));
15027     }
15028   }
15029
15030   if (VT == MVT::v4i1 || VT == MVT::v2i1) {
15031     SDValue zeroConst = DAG.getIntPtrConstant(0, DL);
15032     Op1 = DAG.getNode(ISD::INSERT_SUBVECTOR, DL, MVT::v8i1,
15033                       DAG.getUNDEF(MVT::v8i1), Op1, zeroConst);
15034     Op2 = DAG.getNode(ISD::INSERT_SUBVECTOR, DL, MVT::v8i1,
15035                       DAG.getUNDEF(MVT::v8i1), Op2, zeroConst);
15036     SDValue newSelect = DAG.getNode(ISD::SELECT, DL, MVT::v8i1,
15037                                     Cond, Op1, Op2);
15038     return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, newSelect, zeroConst);
15039   }
15040
15041   if (Cond.getOpcode() == ISD::SETCC) {
15042     SDValue NewCond = LowerSETCC(Cond, DAG);
15043     if (NewCond.getNode())
15044       Cond = NewCond;
15045   }
15046
15047   // (select (x == 0), -1, y) -> (sign_bit (x - 1)) | y
15048   // (select (x == 0), y, -1) -> ~(sign_bit (x - 1)) | y
15049   // (select (x != 0), y, -1) -> (sign_bit (x - 1)) | y
15050   // (select (x != 0), -1, y) -> ~(sign_bit (x - 1)) | y
15051   if (Cond.getOpcode() == X86ISD::SETCC &&
15052       Cond.getOperand(1).getOpcode() == X86ISD::CMP &&
15053       isNullConstant(Cond.getOperand(1).getOperand(1))) {
15054     SDValue Cmp = Cond.getOperand(1);
15055
15056     unsigned CondCode =cast<ConstantSDNode>(Cond.getOperand(0))->getZExtValue();
15057
15058     if ((isAllOnesConstant(Op1) || isAllOnesConstant(Op2)) &&
15059         (CondCode == X86::COND_E || CondCode == X86::COND_NE)) {
15060       SDValue Y = isAllOnesConstant(Op2) ? Op1 : Op2;
15061
15062       SDValue CmpOp0 = Cmp.getOperand(0);
15063       // Apply further optimizations for special cases
15064       // (select (x != 0), -1, 0) -> neg & sbb
15065       // (select (x == 0), 0, -1) -> neg & sbb
15066       if (isNullConstant(Y) &&
15067             (isAllOnesConstant(Op1) == (CondCode == X86::COND_NE))) {
15068           SDVTList VTs = DAG.getVTList(CmpOp0.getValueType(), MVT::i32);
15069           SDValue Neg = DAG.getNode(X86ISD::SUB, DL, VTs,
15070                                     DAG.getConstant(0, DL,
15071                                                     CmpOp0.getValueType()),
15072                                     CmpOp0);
15073           SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
15074                                     DAG.getConstant(X86::COND_B, DL, MVT::i8),
15075                                     SDValue(Neg.getNode(), 1));
15076           return Res;
15077         }
15078
15079       Cmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32,
15080                         CmpOp0, DAG.getConstant(1, DL, CmpOp0.getValueType()));
15081       Cmp = ConvertCmpIfNecessary(Cmp, DAG);
15082
15083       SDValue Res =   // Res = 0 or -1.
15084         DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
15085                     DAG.getConstant(X86::COND_B, DL, MVT::i8), Cmp);
15086
15087       if (isAllOnesConstant(Op1) != (CondCode == X86::COND_E))
15088         Res = DAG.getNOT(DL, Res, Res.getValueType());
15089
15090       if (!isNullConstant(Op2))
15091         Res = DAG.getNode(ISD::OR, DL, Res.getValueType(), Res, Y);
15092       return Res;
15093     }
15094   }
15095
15096   // Look past (and (setcc_carry (cmp ...)), 1).
15097   if (Cond.getOpcode() == ISD::AND &&
15098       Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY &&
15099       isOneConstant(Cond.getOperand(1)))
15100     Cond = Cond.getOperand(0);
15101
15102   // If condition flag is set by a X86ISD::CMP, then use it as the condition
15103   // setting operand in place of the X86ISD::SETCC.
15104   unsigned CondOpcode = Cond.getOpcode();
15105   if (CondOpcode == X86ISD::SETCC ||
15106       CondOpcode == X86ISD::SETCC_CARRY) {
15107     CC = Cond.getOperand(0);
15108
15109     SDValue Cmp = Cond.getOperand(1);
15110     unsigned Opc = Cmp.getOpcode();
15111     MVT VT = Op.getSimpleValueType();
15112
15113     bool IllegalFPCMov = false;
15114     if (VT.isFloatingPoint() && !VT.isVector() &&
15115         !isScalarFPTypeInSSEReg(VT))  // FPStack?
15116       IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
15117
15118     if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
15119         Opc == X86ISD::BT) { // FIXME
15120       Cond = Cmp;
15121       addTest = false;
15122     }
15123   } else if (CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
15124              CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
15125              ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
15126               Cond.getOperand(0).getValueType() != MVT::i8)) {
15127     SDValue LHS = Cond.getOperand(0);
15128     SDValue RHS = Cond.getOperand(1);
15129     unsigned X86Opcode;
15130     unsigned X86Cond;
15131     SDVTList VTs;
15132     switch (CondOpcode) {
15133     case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
15134     case ISD::SADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
15135     case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
15136     case ISD::SSUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
15137     case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
15138     case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
15139     default: llvm_unreachable("unexpected overflowing operator");
15140     }
15141     if (CondOpcode == ISD::UMULO)
15142       VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
15143                           MVT::i32);
15144     else
15145       VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
15146
15147     SDValue X86Op = DAG.getNode(X86Opcode, DL, VTs, LHS, RHS);
15148
15149     if (CondOpcode == ISD::UMULO)
15150       Cond = X86Op.getValue(2);
15151     else
15152       Cond = X86Op.getValue(1);
15153
15154     CC = DAG.getConstant(X86Cond, DL, MVT::i8);
15155     addTest = false;
15156   }
15157
15158   if (addTest) {
15159     // Look past the truncate if the high bits are known zero.
15160     if (isTruncWithZeroHighBitsInput(Cond, DAG))
15161       Cond = Cond.getOperand(0);
15162
15163     // We know the result of AND is compared against zero. Try to match
15164     // it to BT.
15165     if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
15166       if (SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, DL, DAG)) {
15167         CC = NewSetCC.getOperand(0);
15168         Cond = NewSetCC.getOperand(1);
15169         addTest = false;
15170       }
15171     }
15172   }
15173
15174   if (addTest) {
15175     CC = DAG.getConstant(X86::COND_NE, DL, MVT::i8);
15176     Cond = EmitTest(Cond, X86::COND_NE, DL, DAG);
15177   }
15178
15179   // a <  b ? -1 :  0 -> RES = ~setcc_carry
15180   // a <  b ?  0 : -1 -> RES = setcc_carry
15181   // a >= b ? -1 :  0 -> RES = setcc_carry
15182   // a >= b ?  0 : -1 -> RES = ~setcc_carry
15183   if (Cond.getOpcode() == X86ISD::SUB) {
15184     Cond = ConvertCmpIfNecessary(Cond, DAG);
15185     unsigned CondCode = cast<ConstantSDNode>(CC)->getZExtValue();
15186
15187     if ((CondCode == X86::COND_AE || CondCode == X86::COND_B) &&
15188         (isAllOnesConstant(Op1) || isAllOnesConstant(Op2)) &&
15189         (isNullConstant(Op1) || isNullConstant(Op2))) {
15190       SDValue Res = DAG.getNode(X86ISD::SETCC_CARRY, DL, Op.getValueType(),
15191                                 DAG.getConstant(X86::COND_B, DL, MVT::i8),
15192                                 Cond);
15193       if (isAllOnesConstant(Op1) != (CondCode == X86::COND_B))
15194         return DAG.getNOT(DL, Res, Res.getValueType());
15195       return Res;
15196     }
15197   }
15198
15199   // X86 doesn't have an i8 cmov. If both operands are the result of a truncate
15200   // widen the cmov and push the truncate through. This avoids introducing a new
15201   // branch during isel and doesn't add any extensions.
15202   if (Op.getValueType() == MVT::i8 &&
15203       Op1.getOpcode() == ISD::TRUNCATE && Op2.getOpcode() == ISD::TRUNCATE) {
15204     SDValue T1 = Op1.getOperand(0), T2 = Op2.getOperand(0);
15205     if (T1.getValueType() == T2.getValueType() &&
15206         // Blacklist CopyFromReg to avoid partial register stalls.
15207         T1.getOpcode() != ISD::CopyFromReg && T2.getOpcode()!=ISD::CopyFromReg){
15208       SDVTList VTs = DAG.getVTList(T1.getValueType(), MVT::Glue);
15209       SDValue Cmov = DAG.getNode(X86ISD::CMOV, DL, VTs, T2, T1, CC, Cond);
15210       return DAG.getNode(ISD::TRUNCATE, DL, Op.getValueType(), Cmov);
15211     }
15212   }
15213
15214   // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
15215   // condition is true.
15216   SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
15217   SDValue Ops[] = { Op2, Op1, CC, Cond };
15218   return DAG.getNode(X86ISD::CMOV, DL, VTs, Ops);
15219 }
15220
15221 static SDValue LowerSIGN_EXTEND_AVX512(SDValue Op,
15222                                        const X86Subtarget *Subtarget,
15223                                        SelectionDAG &DAG) {
15224   MVT VT = Op->getSimpleValueType(0);
15225   SDValue In = Op->getOperand(0);
15226   MVT InVT = In.getSimpleValueType();
15227   MVT VTElt = VT.getVectorElementType();
15228   MVT InVTElt = InVT.getVectorElementType();
15229   SDLoc dl(Op);
15230
15231   // SKX processor
15232   if ((InVTElt == MVT::i1) &&
15233       (((Subtarget->hasBWI() && Subtarget->hasVLX() &&
15234         VT.getSizeInBits() <= 256 && VTElt.getSizeInBits() <= 16)) ||
15235
15236        ((Subtarget->hasBWI() && VT.is512BitVector() &&
15237         VTElt.getSizeInBits() <= 16)) ||
15238
15239        ((Subtarget->hasDQI() && Subtarget->hasVLX() &&
15240         VT.getSizeInBits() <= 256 && VTElt.getSizeInBits() >= 32)) ||
15241
15242        ((Subtarget->hasDQI() && VT.is512BitVector() &&
15243         VTElt.getSizeInBits() >= 32))))
15244     return DAG.getNode(X86ISD::VSEXT, dl, VT, In);
15245
15246   unsigned int NumElts = VT.getVectorNumElements();
15247
15248   if (NumElts != 8 && NumElts != 16 && !Subtarget->hasBWI())
15249     return SDValue();
15250
15251   if (VT.is512BitVector() && InVT.getVectorElementType() != MVT::i1) {
15252     if (In.getOpcode() == X86ISD::VSEXT || In.getOpcode() == X86ISD::VZEXT)
15253       return DAG.getNode(In.getOpcode(), dl, VT, In.getOperand(0));
15254     return DAG.getNode(X86ISD::VSEXT, dl, VT, In);
15255   }
15256
15257   assert (InVT.getVectorElementType() == MVT::i1 && "Unexpected vector type");
15258   MVT ExtVT = NumElts == 8 ? MVT::v8i64 : MVT::v16i32;
15259   SDValue NegOne =
15260    DAG.getConstant(APInt::getAllOnesValue(ExtVT.getScalarSizeInBits()), dl,
15261                    ExtVT);
15262   SDValue Zero =
15263    DAG.getConstant(APInt::getNullValue(ExtVT.getScalarSizeInBits()), dl, ExtVT);
15264
15265   SDValue V = DAG.getNode(ISD::VSELECT, dl, ExtVT, In, NegOne, Zero);
15266   if (VT.is512BitVector())
15267     return V;
15268   return DAG.getNode(X86ISD::VTRUNC, dl, VT, V);
15269 }
15270
15271 static SDValue LowerSIGN_EXTEND_VECTOR_INREG(SDValue Op,
15272                                              const X86Subtarget *Subtarget,
15273                                              SelectionDAG &DAG) {
15274   SDValue In = Op->getOperand(0);
15275   MVT VT = Op->getSimpleValueType(0);
15276   MVT InVT = In.getSimpleValueType();
15277   assert(VT.getSizeInBits() == InVT.getSizeInBits());
15278
15279   MVT InSVT = InVT.getVectorElementType();
15280   assert(VT.getVectorElementType().getSizeInBits() > InSVT.getSizeInBits());
15281
15282   if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
15283     return SDValue();
15284   if (InSVT != MVT::i32 && InSVT != MVT::i16 && InSVT != MVT::i8)
15285     return SDValue();
15286
15287   SDLoc dl(Op);
15288
15289   // SSE41 targets can use the pmovsx* instructions directly.
15290   if (Subtarget->hasSSE41())
15291     return DAG.getNode(X86ISD::VSEXT, dl, VT, In);
15292
15293   // pre-SSE41 targets unpack lower lanes and then sign-extend using SRAI.
15294   SDValue Curr = In;
15295   MVT CurrVT = InVT;
15296
15297   // As SRAI is only available on i16/i32 types, we expand only up to i32
15298   // and handle i64 separately.
15299   while (CurrVT != VT && CurrVT.getVectorElementType() != MVT::i32) {
15300     Curr = DAG.getNode(X86ISD::UNPCKL, dl, CurrVT, DAG.getUNDEF(CurrVT), Curr);
15301     MVT CurrSVT = MVT::getIntegerVT(CurrVT.getScalarSizeInBits() * 2);
15302     CurrVT = MVT::getVectorVT(CurrSVT, CurrVT.getVectorNumElements() / 2);
15303     Curr = DAG.getBitcast(CurrVT, Curr);
15304   }
15305
15306   SDValue SignExt = Curr;
15307   if (CurrVT != InVT) {
15308     unsigned SignExtShift =
15309         CurrVT.getVectorElementType().getSizeInBits() - InSVT.getSizeInBits();
15310     SignExt = DAG.getNode(X86ISD::VSRAI, dl, CurrVT, Curr,
15311                           DAG.getConstant(SignExtShift, dl, MVT::i8));
15312   }
15313
15314   if (CurrVT == VT)
15315     return SignExt;
15316
15317   if (VT == MVT::v2i64 && CurrVT == MVT::v4i32) {
15318     SDValue Sign = DAG.getNode(X86ISD::VSRAI, dl, CurrVT, Curr,
15319                                DAG.getConstant(31, dl, MVT::i8));
15320     SDValue Ext = DAG.getVectorShuffle(CurrVT, dl, SignExt, Sign, {0, 4, 1, 5});
15321     return DAG.getBitcast(VT, Ext);
15322   }
15323
15324   return SDValue();
15325 }
15326
15327 static SDValue LowerSIGN_EXTEND(SDValue Op, const X86Subtarget *Subtarget,
15328                                 SelectionDAG &DAG) {
15329   MVT VT = Op->getSimpleValueType(0);
15330   SDValue In = Op->getOperand(0);
15331   MVT InVT = In.getSimpleValueType();
15332   SDLoc dl(Op);
15333
15334   if (VT.is512BitVector() || InVT.getVectorElementType() == MVT::i1)
15335     return LowerSIGN_EXTEND_AVX512(Op, Subtarget, DAG);
15336
15337   if ((VT != MVT::v4i64 || InVT != MVT::v4i32) &&
15338       (VT != MVT::v8i32 || InVT != MVT::v8i16) &&
15339       (VT != MVT::v16i16 || InVT != MVT::v16i8))
15340     return SDValue();
15341
15342   if (Subtarget->hasInt256())
15343     return DAG.getNode(X86ISD::VSEXT, dl, VT, In);
15344
15345   // Optimize vectors in AVX mode
15346   // Sign extend  v8i16 to v8i32 and
15347   //              v4i32 to v4i64
15348   //
15349   // Divide input vector into two parts
15350   // for v4i32 the shuffle mask will be { 0, 1, -1, -1} {2, 3, -1, -1}
15351   // use vpmovsx instruction to extend v4i32 -> v2i64; v8i16 -> v4i32
15352   // concat the vectors to original VT
15353
15354   unsigned NumElems = InVT.getVectorNumElements();
15355   SDValue Undef = DAG.getUNDEF(InVT);
15356
15357   SmallVector<int,8> ShufMask1(NumElems, -1);
15358   for (unsigned i = 0; i != NumElems/2; ++i)
15359     ShufMask1[i] = i;
15360
15361   SDValue OpLo = DAG.getVectorShuffle(InVT, dl, In, Undef, &ShufMask1[0]);
15362
15363   SmallVector<int,8> ShufMask2(NumElems, -1);
15364   for (unsigned i = 0; i != NumElems/2; ++i)
15365     ShufMask2[i] = i + NumElems/2;
15366
15367   SDValue OpHi = DAG.getVectorShuffle(InVT, dl, In, Undef, &ShufMask2[0]);
15368
15369   MVT HalfVT = MVT::getVectorVT(VT.getVectorElementType(),
15370                                 VT.getVectorNumElements()/2);
15371
15372   OpLo = DAG.getNode(X86ISD::VSEXT, dl, HalfVT, OpLo);
15373   OpHi = DAG.getNode(X86ISD::VSEXT, dl, HalfVT, OpHi);
15374
15375   return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, OpLo, OpHi);
15376 }
15377
15378 // Lower vector extended loads using a shuffle. If SSSE3 is not available we
15379 // may emit an illegal shuffle but the expansion is still better than scalar
15380 // code. We generate X86ISD::VSEXT for SEXTLOADs if it's available, otherwise
15381 // we'll emit a shuffle and a arithmetic shift.
15382 // FIXME: Is the expansion actually better than scalar code? It doesn't seem so.
15383 // TODO: It is possible to support ZExt by zeroing the undef values during
15384 // the shuffle phase or after the shuffle.
15385 static SDValue LowerExtendedLoad(SDValue Op, const X86Subtarget *Subtarget,
15386                                  SelectionDAG &DAG) {
15387   MVT RegVT = Op.getSimpleValueType();
15388   assert(RegVT.isVector() && "We only custom lower vector sext loads.");
15389   assert(RegVT.isInteger() &&
15390          "We only custom lower integer vector sext loads.");
15391
15392   // Nothing useful we can do without SSE2 shuffles.
15393   assert(Subtarget->hasSSE2() && "We only custom lower sext loads with SSE2.");
15394
15395   LoadSDNode *Ld = cast<LoadSDNode>(Op.getNode());
15396   SDLoc dl(Ld);
15397   EVT MemVT = Ld->getMemoryVT();
15398   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15399   unsigned RegSz = RegVT.getSizeInBits();
15400
15401   ISD::LoadExtType Ext = Ld->getExtensionType();
15402
15403   assert((Ext == ISD::EXTLOAD || Ext == ISD::SEXTLOAD)
15404          && "Only anyext and sext are currently implemented.");
15405   assert(MemVT != RegVT && "Cannot extend to the same type");
15406   assert(MemVT.isVector() && "Must load a vector from memory");
15407
15408   unsigned NumElems = RegVT.getVectorNumElements();
15409   unsigned MemSz = MemVT.getSizeInBits();
15410   assert(RegSz > MemSz && "Register size must be greater than the mem size");
15411
15412   if (Ext == ISD::SEXTLOAD && RegSz == 256 && !Subtarget->hasInt256()) {
15413     // The only way in which we have a legal 256-bit vector result but not the
15414     // integer 256-bit operations needed to directly lower a sextload is if we
15415     // have AVX1 but not AVX2. In that case, we can always emit a sextload to
15416     // a 128-bit vector and a normal sign_extend to 256-bits that should get
15417     // correctly legalized. We do this late to allow the canonical form of
15418     // sextload to persist throughout the rest of the DAG combiner -- it wants
15419     // to fold together any extensions it can, and so will fuse a sign_extend
15420     // of an sextload into a sextload targeting a wider value.
15421     SDValue Load;
15422     if (MemSz == 128) {
15423       // Just switch this to a normal load.
15424       assert(TLI.isTypeLegal(MemVT) && "If the memory type is a 128-bit type, "
15425                                        "it must be a legal 128-bit vector "
15426                                        "type!");
15427       Load = DAG.getLoad(MemVT, dl, Ld->getChain(), Ld->getBasePtr(),
15428                   Ld->getPointerInfo(), Ld->isVolatile(), Ld->isNonTemporal(),
15429                   Ld->isInvariant(), Ld->getAlignment());
15430     } else {
15431       assert(MemSz < 128 &&
15432              "Can't extend a type wider than 128 bits to a 256 bit vector!");
15433       // Do an sext load to a 128-bit vector type. We want to use the same
15434       // number of elements, but elements half as wide. This will end up being
15435       // recursively lowered by this routine, but will succeed as we definitely
15436       // have all the necessary features if we're using AVX1.
15437       EVT HalfEltVT =
15438           EVT::getIntegerVT(*DAG.getContext(), RegVT.getScalarSizeInBits() / 2);
15439       EVT HalfVecVT = EVT::getVectorVT(*DAG.getContext(), HalfEltVT, NumElems);
15440       Load =
15441           DAG.getExtLoad(Ext, dl, HalfVecVT, Ld->getChain(), Ld->getBasePtr(),
15442                          Ld->getPointerInfo(), MemVT, Ld->isVolatile(),
15443                          Ld->isNonTemporal(), Ld->isInvariant(),
15444                          Ld->getAlignment());
15445     }
15446
15447     // Replace chain users with the new chain.
15448     assert(Load->getNumValues() == 2 && "Loads must carry a chain!");
15449     DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), Load.getValue(1));
15450
15451     // Finally, do a normal sign-extend to the desired register.
15452     return DAG.getSExtOrTrunc(Load, dl, RegVT);
15453   }
15454
15455   // All sizes must be a power of two.
15456   assert(isPowerOf2_32(RegSz * MemSz * NumElems) &&
15457          "Non-power-of-two elements are not custom lowered!");
15458
15459   // Attempt to load the original value using scalar loads.
15460   // Find the largest scalar type that divides the total loaded size.
15461   MVT SclrLoadTy = MVT::i8;
15462   for (MVT Tp : MVT::integer_valuetypes()) {
15463     if (TLI.isTypeLegal(Tp) && ((MemSz % Tp.getSizeInBits()) == 0)) {
15464       SclrLoadTy = Tp;
15465     }
15466   }
15467
15468   // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
15469   if (TLI.isTypeLegal(MVT::f64) && SclrLoadTy.getSizeInBits() < 64 &&
15470       (64 <= MemSz))
15471     SclrLoadTy = MVT::f64;
15472
15473   // Calculate the number of scalar loads that we need to perform
15474   // in order to load our vector from memory.
15475   unsigned NumLoads = MemSz / SclrLoadTy.getSizeInBits();
15476
15477   assert((Ext != ISD::SEXTLOAD || NumLoads == 1) &&
15478          "Can only lower sext loads with a single scalar load!");
15479
15480   unsigned loadRegZize = RegSz;
15481   if (Ext == ISD::SEXTLOAD && RegSz >= 256)
15482     loadRegZize = 128;
15483
15484   // Represent our vector as a sequence of elements which are the
15485   // largest scalar that we can load.
15486   EVT LoadUnitVecVT = EVT::getVectorVT(
15487       *DAG.getContext(), SclrLoadTy, loadRegZize / SclrLoadTy.getSizeInBits());
15488
15489   // Represent the data using the same element type that is stored in
15490   // memory. In practice, we ''widen'' MemVT.
15491   EVT WideVecVT =
15492       EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
15493                        loadRegZize / MemVT.getScalarSizeInBits());
15494
15495   assert(WideVecVT.getSizeInBits() == LoadUnitVecVT.getSizeInBits() &&
15496          "Invalid vector type");
15497
15498   // We can't shuffle using an illegal type.
15499   assert(TLI.isTypeLegal(WideVecVT) &&
15500          "We only lower types that form legal widened vector types");
15501
15502   SmallVector<SDValue, 8> Chains;
15503   SDValue Ptr = Ld->getBasePtr();
15504   SDValue Increment = DAG.getConstant(SclrLoadTy.getSizeInBits() / 8, dl,
15505                                       TLI.getPointerTy(DAG.getDataLayout()));
15506   SDValue Res = DAG.getUNDEF(LoadUnitVecVT);
15507
15508   for (unsigned i = 0; i < NumLoads; ++i) {
15509     // Perform a single load.
15510     SDValue ScalarLoad =
15511         DAG.getLoad(SclrLoadTy, dl, Ld->getChain(), Ptr, Ld->getPointerInfo(),
15512                     Ld->isVolatile(), Ld->isNonTemporal(), Ld->isInvariant(),
15513                     Ld->getAlignment());
15514     Chains.push_back(ScalarLoad.getValue(1));
15515     // Create the first element type using SCALAR_TO_VECTOR in order to avoid
15516     // another round of DAGCombining.
15517     if (i == 0)
15518       Res = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, LoadUnitVecVT, ScalarLoad);
15519     else
15520       Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, LoadUnitVecVT, Res,
15521                         ScalarLoad, DAG.getIntPtrConstant(i, dl));
15522
15523     Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
15524   }
15525
15526   SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains);
15527
15528   // Bitcast the loaded value to a vector of the original element type, in
15529   // the size of the target vector type.
15530   SDValue SlicedVec = DAG.getBitcast(WideVecVT, Res);
15531   unsigned SizeRatio = RegSz / MemSz;
15532
15533   if (Ext == ISD::SEXTLOAD) {
15534     // If we have SSE4.1, we can directly emit a VSEXT node.
15535     if (Subtarget->hasSSE41()) {
15536       SDValue Sext = DAG.getNode(X86ISD::VSEXT, dl, RegVT, SlicedVec);
15537       DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), TF);
15538       return Sext;
15539     }
15540
15541     // Otherwise we'll use SIGN_EXTEND_VECTOR_INREG to sign extend the lowest
15542     // lanes.
15543     assert(TLI.isOperationLegalOrCustom(ISD::SIGN_EXTEND_VECTOR_INREG, RegVT) &&
15544            "We can't implement a sext load without SIGN_EXTEND_VECTOR_INREG!");
15545
15546     SDValue Shuff = DAG.getSignExtendVectorInReg(SlicedVec, dl, RegVT);
15547     DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), TF);
15548     return Shuff;
15549   }
15550
15551   // Redistribute the loaded elements into the different locations.
15552   SmallVector<int, 16> ShuffleVec(NumElems * SizeRatio, -1);
15553   for (unsigned i = 0; i != NumElems; ++i)
15554     ShuffleVec[i * SizeRatio] = i;
15555
15556   SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, SlicedVec,
15557                                        DAG.getUNDEF(WideVecVT), &ShuffleVec[0]);
15558
15559   // Bitcast to the requested type.
15560   Shuff = DAG.getBitcast(RegVT, Shuff);
15561   DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), TF);
15562   return Shuff;
15563 }
15564
15565 // isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
15566 // ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
15567 // from the AND / OR.
15568 static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
15569   Opc = Op.getOpcode();
15570   if (Opc != ISD::OR && Opc != ISD::AND)
15571     return false;
15572   return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
15573           Op.getOperand(0).hasOneUse() &&
15574           Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
15575           Op.getOperand(1).hasOneUse());
15576 }
15577
15578 // isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
15579 // 1 and that the SETCC node has a single use.
15580 static bool isXor1OfSetCC(SDValue Op) {
15581   if (Op.getOpcode() != ISD::XOR)
15582     return false;
15583   if (isOneConstant(Op.getOperand(1)))
15584     return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
15585            Op.getOperand(0).hasOneUse();
15586   return false;
15587 }
15588
15589 SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) const {
15590   bool addTest = true;
15591   SDValue Chain = Op.getOperand(0);
15592   SDValue Cond  = Op.getOperand(1);
15593   SDValue Dest  = Op.getOperand(2);
15594   SDLoc dl(Op);
15595   SDValue CC;
15596   bool Inverted = false;
15597
15598   if (Cond.getOpcode() == ISD::SETCC) {
15599     // Check for setcc([su]{add,sub,mul}o == 0).
15600     if (cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETEQ &&
15601         isNullConstant(Cond.getOperand(1)) &&
15602         Cond.getOperand(0).getResNo() == 1 &&
15603         (Cond.getOperand(0).getOpcode() == ISD::SADDO ||
15604          Cond.getOperand(0).getOpcode() == ISD::UADDO ||
15605          Cond.getOperand(0).getOpcode() == ISD::SSUBO ||
15606          Cond.getOperand(0).getOpcode() == ISD::USUBO ||
15607          Cond.getOperand(0).getOpcode() == ISD::SMULO ||
15608          Cond.getOperand(0).getOpcode() == ISD::UMULO)) {
15609       Inverted = true;
15610       Cond = Cond.getOperand(0);
15611     } else {
15612       SDValue NewCond = LowerSETCC(Cond, DAG);
15613       if (NewCond.getNode())
15614         Cond = NewCond;
15615     }
15616   }
15617 #if 0
15618   // FIXME: LowerXALUO doesn't handle these!!
15619   else if (Cond.getOpcode() == X86ISD::ADD  ||
15620            Cond.getOpcode() == X86ISD::SUB  ||
15621            Cond.getOpcode() == X86ISD::SMUL ||
15622            Cond.getOpcode() == X86ISD::UMUL)
15623     Cond = LowerXALUO(Cond, DAG);
15624 #endif
15625
15626   // Look pass (and (setcc_carry (cmp ...)), 1).
15627   if (Cond.getOpcode() == ISD::AND &&
15628       Cond.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY &&
15629       isOneConstant(Cond.getOperand(1)))
15630     Cond = Cond.getOperand(0);
15631
15632   // If condition flag is set by a X86ISD::CMP, then use it as the condition
15633   // setting operand in place of the X86ISD::SETCC.
15634   unsigned CondOpcode = Cond.getOpcode();
15635   if (CondOpcode == X86ISD::SETCC ||
15636       CondOpcode == X86ISD::SETCC_CARRY) {
15637     CC = Cond.getOperand(0);
15638
15639     SDValue Cmp = Cond.getOperand(1);
15640     unsigned Opc = Cmp.getOpcode();
15641     // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
15642     if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
15643       Cond = Cmp;
15644       addTest = false;
15645     } else {
15646       switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
15647       default: break;
15648       case X86::COND_O:
15649       case X86::COND_B:
15650         // These can only come from an arithmetic instruction with overflow,
15651         // e.g. SADDO, UADDO.
15652         Cond = Cond.getNode()->getOperand(1);
15653         addTest = false;
15654         break;
15655       }
15656     }
15657   }
15658   CondOpcode = Cond.getOpcode();
15659   if (CondOpcode == ISD::UADDO || CondOpcode == ISD::SADDO ||
15660       CondOpcode == ISD::USUBO || CondOpcode == ISD::SSUBO ||
15661       ((CondOpcode == ISD::UMULO || CondOpcode == ISD::SMULO) &&
15662        Cond.getOperand(0).getValueType() != MVT::i8)) {
15663     SDValue LHS = Cond.getOperand(0);
15664     SDValue RHS = Cond.getOperand(1);
15665     unsigned X86Opcode;
15666     unsigned X86Cond;
15667     SDVTList VTs;
15668     // Keep this in sync with LowerXALUO, otherwise we might create redundant
15669     // instructions that can't be removed afterwards (i.e. X86ISD::ADD and
15670     // X86ISD::INC).
15671     switch (CondOpcode) {
15672     case ISD::UADDO: X86Opcode = X86ISD::ADD; X86Cond = X86::COND_B; break;
15673     case ISD::SADDO:
15674       if (isOneConstant(RHS)) {
15675           X86Opcode = X86ISD::INC; X86Cond = X86::COND_O;
15676           break;
15677         }
15678       X86Opcode = X86ISD::ADD; X86Cond = X86::COND_O; break;
15679     case ISD::USUBO: X86Opcode = X86ISD::SUB; X86Cond = X86::COND_B; break;
15680     case ISD::SSUBO:
15681       if (isOneConstant(RHS)) {
15682           X86Opcode = X86ISD::DEC; X86Cond = X86::COND_O;
15683           break;
15684         }
15685       X86Opcode = X86ISD::SUB; X86Cond = X86::COND_O; break;
15686     case ISD::UMULO: X86Opcode = X86ISD::UMUL; X86Cond = X86::COND_O; break;
15687     case ISD::SMULO: X86Opcode = X86ISD::SMUL; X86Cond = X86::COND_O; break;
15688     default: llvm_unreachable("unexpected overflowing operator");
15689     }
15690     if (Inverted)
15691       X86Cond = X86::GetOppositeBranchCondition((X86::CondCode)X86Cond);
15692     if (CondOpcode == ISD::UMULO)
15693       VTs = DAG.getVTList(LHS.getValueType(), LHS.getValueType(),
15694                           MVT::i32);
15695     else
15696       VTs = DAG.getVTList(LHS.getValueType(), MVT::i32);
15697
15698     SDValue X86Op = DAG.getNode(X86Opcode, dl, VTs, LHS, RHS);
15699
15700     if (CondOpcode == ISD::UMULO)
15701       Cond = X86Op.getValue(2);
15702     else
15703       Cond = X86Op.getValue(1);
15704
15705     CC = DAG.getConstant(X86Cond, dl, MVT::i8);
15706     addTest = false;
15707   } else {
15708     unsigned CondOpc;
15709     if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
15710       SDValue Cmp = Cond.getOperand(0).getOperand(1);
15711       if (CondOpc == ISD::OR) {
15712         // Also, recognize the pattern generated by an FCMP_UNE. We can emit
15713         // two branches instead of an explicit OR instruction with a
15714         // separate test.
15715         if (Cmp == Cond.getOperand(1).getOperand(1) &&
15716             isX86LogicalCmp(Cmp)) {
15717           CC = Cond.getOperand(0).getOperand(0);
15718           Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
15719                               Chain, Dest, CC, Cmp);
15720           CC = Cond.getOperand(1).getOperand(0);
15721           Cond = Cmp;
15722           addTest = false;
15723         }
15724       } else { // ISD::AND
15725         // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
15726         // two branches instead of an explicit AND instruction with a
15727         // separate test. However, we only do this if this block doesn't
15728         // have a fall-through edge, because this requires an explicit
15729         // jmp when the condition is false.
15730         if (Cmp == Cond.getOperand(1).getOperand(1) &&
15731             isX86LogicalCmp(Cmp) &&
15732             Op.getNode()->hasOneUse()) {
15733           X86::CondCode CCode =
15734             (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
15735           CCode = X86::GetOppositeBranchCondition(CCode);
15736           CC = DAG.getConstant(CCode, dl, MVT::i8);
15737           SDNode *User = *Op.getNode()->use_begin();
15738           // Look for an unconditional branch following this conditional branch.
15739           // We need this because we need to reverse the successors in order
15740           // to implement FCMP_OEQ.
15741           if (User->getOpcode() == ISD::BR) {
15742             SDValue FalseBB = User->getOperand(1);
15743             SDNode *NewBR =
15744               DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
15745             assert(NewBR == User);
15746             (void)NewBR;
15747             Dest = FalseBB;
15748
15749             Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
15750                                 Chain, Dest, CC, Cmp);
15751             X86::CondCode CCode =
15752               (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
15753             CCode = X86::GetOppositeBranchCondition(CCode);
15754             CC = DAG.getConstant(CCode, dl, MVT::i8);
15755             Cond = Cmp;
15756             addTest = false;
15757           }
15758         }
15759       }
15760     } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
15761       // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
15762       // It should be transformed during dag combiner except when the condition
15763       // is set by a arithmetics with overflow node.
15764       X86::CondCode CCode =
15765         (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
15766       CCode = X86::GetOppositeBranchCondition(CCode);
15767       CC = DAG.getConstant(CCode, dl, MVT::i8);
15768       Cond = Cond.getOperand(0).getOperand(1);
15769       addTest = false;
15770     } else if (Cond.getOpcode() == ISD::SETCC &&
15771                cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETOEQ) {
15772       // For FCMP_OEQ, we can emit
15773       // two branches instead of an explicit AND instruction with a
15774       // separate test. However, we only do this if this block doesn't
15775       // have a fall-through edge, because this requires an explicit
15776       // jmp when the condition is false.
15777       if (Op.getNode()->hasOneUse()) {
15778         SDNode *User = *Op.getNode()->use_begin();
15779         // Look for an unconditional branch following this conditional branch.
15780         // We need this because we need to reverse the successors in order
15781         // to implement FCMP_OEQ.
15782         if (User->getOpcode() == ISD::BR) {
15783           SDValue FalseBB = User->getOperand(1);
15784           SDNode *NewBR =
15785             DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
15786           assert(NewBR == User);
15787           (void)NewBR;
15788           Dest = FalseBB;
15789
15790           SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
15791                                     Cond.getOperand(0), Cond.getOperand(1));
15792           Cmp = ConvertCmpIfNecessary(Cmp, DAG);
15793           CC = DAG.getConstant(X86::COND_NE, dl, MVT::i8);
15794           Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
15795                               Chain, Dest, CC, Cmp);
15796           CC = DAG.getConstant(X86::COND_P, dl, MVT::i8);
15797           Cond = Cmp;
15798           addTest = false;
15799         }
15800       }
15801     } else if (Cond.getOpcode() == ISD::SETCC &&
15802                cast<CondCodeSDNode>(Cond.getOperand(2))->get() == ISD::SETUNE) {
15803       // For FCMP_UNE, we can emit
15804       // two branches instead of an explicit AND instruction with a
15805       // separate test. However, we only do this if this block doesn't
15806       // have a fall-through edge, because this requires an explicit
15807       // jmp when the condition is false.
15808       if (Op.getNode()->hasOneUse()) {
15809         SDNode *User = *Op.getNode()->use_begin();
15810         // Look for an unconditional branch following this conditional branch.
15811         // We need this because we need to reverse the successors in order
15812         // to implement FCMP_UNE.
15813         if (User->getOpcode() == ISD::BR) {
15814           SDValue FalseBB = User->getOperand(1);
15815           SDNode *NewBR =
15816             DAG.UpdateNodeOperands(User, User->getOperand(0), Dest);
15817           assert(NewBR == User);
15818           (void)NewBR;
15819
15820           SDValue Cmp = DAG.getNode(X86ISD::CMP, dl, MVT::i32,
15821                                     Cond.getOperand(0), Cond.getOperand(1));
15822           Cmp = ConvertCmpIfNecessary(Cmp, DAG);
15823           CC = DAG.getConstant(X86::COND_NE, dl, MVT::i8);
15824           Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
15825                               Chain, Dest, CC, Cmp);
15826           CC = DAG.getConstant(X86::COND_NP, dl, MVT::i8);
15827           Cond = Cmp;
15828           addTest = false;
15829           Dest = FalseBB;
15830         }
15831       }
15832     }
15833   }
15834
15835   if (addTest) {
15836     // Look pass the truncate if the high bits are known zero.
15837     if (isTruncWithZeroHighBitsInput(Cond, DAG))
15838         Cond = Cond.getOperand(0);
15839
15840     // We know the result of AND is compared against zero. Try to match
15841     // it to BT.
15842     if (Cond.getOpcode() == ISD::AND && Cond.hasOneUse()) {
15843       if (SDValue NewSetCC = LowerToBT(Cond, ISD::SETNE, dl, DAG)) {
15844         CC = NewSetCC.getOperand(0);
15845         Cond = NewSetCC.getOperand(1);
15846         addTest = false;
15847       }
15848     }
15849   }
15850
15851   if (addTest) {
15852     X86::CondCode X86Cond = Inverted ? X86::COND_E : X86::COND_NE;
15853     CC = DAG.getConstant(X86Cond, dl, MVT::i8);
15854     Cond = EmitTest(Cond, X86Cond, dl, DAG);
15855   }
15856   Cond = ConvertCmpIfNecessary(Cond, DAG);
15857   return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
15858                      Chain, Dest, CC, Cond);
15859 }
15860
15861 // Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
15862 // Calls to _alloca are needed to probe the stack when allocating more than 4k
15863 // bytes in one go. Touching the stack at 4K increments is necessary to ensure
15864 // that the guard pages used by the OS virtual memory manager are allocated in
15865 // correct sequence.
15866 SDValue
15867 X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
15868                                            SelectionDAG &DAG) const {
15869   MachineFunction &MF = DAG.getMachineFunction();
15870   bool SplitStack = MF.shouldSplitStack();
15871   bool Lower = (Subtarget->isOSWindows() && !Subtarget->isTargetMachO()) ||
15872                SplitStack;
15873   SDLoc dl(Op);
15874
15875   // Get the inputs.
15876   SDNode *Node = Op.getNode();
15877   SDValue Chain = Op.getOperand(0);
15878   SDValue Size  = Op.getOperand(1);
15879   unsigned Align = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
15880   EVT VT = Node->getValueType(0);
15881
15882   // Chain the dynamic stack allocation so that it doesn't modify the stack
15883   // pointer when other instructions are using the stack.
15884   Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, dl, true), dl);
15885
15886   bool Is64Bit = Subtarget->is64Bit();
15887   MVT SPTy = getPointerTy(DAG.getDataLayout());
15888
15889   SDValue Result;
15890   if (!Lower) {
15891     const TargetLowering &TLI = DAG.getTargetLoweringInfo();
15892     unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
15893     assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
15894                     " not tell us which reg is the stack pointer!");
15895     EVT VT = Node->getValueType(0);
15896     SDValue Tmp3 = Node->getOperand(2);
15897
15898     SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, VT);
15899     Chain = SP.getValue(1);
15900     unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
15901     const TargetFrameLowering &TFI = *Subtarget->getFrameLowering();
15902     unsigned StackAlign = TFI.getStackAlignment();
15903     Result = DAG.getNode(ISD::SUB, dl, VT, SP, Size); // Value
15904     if (Align > StackAlign)
15905       Result = DAG.getNode(ISD::AND, dl, VT, Result,
15906                          DAG.getConstant(-(uint64_t)Align, dl, VT));
15907     Chain = DAG.getCopyToReg(Chain, dl, SPReg, Result); // Output chain
15908   } else if (SplitStack) {
15909     MachineRegisterInfo &MRI = MF.getRegInfo();
15910
15911     if (Is64Bit) {
15912       // The 64 bit implementation of segmented stacks needs to clobber both r10
15913       // r11. This makes it impossible to use it along with nested parameters.
15914       const Function *F = MF.getFunction();
15915
15916       for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
15917            I != E; ++I)
15918         if (I->hasNestAttr())
15919           report_fatal_error("Cannot use segmented stacks with functions that "
15920                              "have nested arguments.");
15921     }
15922
15923     const TargetRegisterClass *AddrRegClass = getRegClassFor(SPTy);
15924     unsigned Vreg = MRI.createVirtualRegister(AddrRegClass);
15925     Chain = DAG.getCopyToReg(Chain, dl, Vreg, Size);
15926     Result = DAG.getNode(X86ISD::SEG_ALLOCA, dl, SPTy, Chain,
15927                                 DAG.getRegister(Vreg, SPTy));
15928   } else {
15929     SDValue Flag;
15930     const unsigned Reg = (Subtarget->isTarget64BitLP64() ? X86::RAX : X86::EAX);
15931
15932     Chain = DAG.getCopyToReg(Chain, dl, Reg, Size, Flag);
15933     Flag = Chain.getValue(1);
15934     SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
15935
15936     Chain = DAG.getNode(X86ISD::WIN_ALLOCA, dl, NodeTys, Chain, Flag);
15937
15938     const X86RegisterInfo *RegInfo = Subtarget->getRegisterInfo();
15939     unsigned SPReg = RegInfo->getStackRegister();
15940     SDValue SP = DAG.getCopyFromReg(Chain, dl, SPReg, SPTy);
15941     Chain = SP.getValue(1);
15942
15943     if (Align) {
15944       SP = DAG.getNode(ISD::AND, dl, VT, SP.getValue(0),
15945                        DAG.getConstant(-(uint64_t)Align, dl, VT));
15946       Chain = DAG.getCopyToReg(Chain, dl, SPReg, SP);
15947     }
15948
15949     Result = SP;
15950   }
15951
15952   Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, dl, true),
15953                              DAG.getIntPtrConstant(0, dl, true), SDValue(), dl);
15954
15955   SDValue Ops[2] = {Result, Chain};
15956   return DAG.getMergeValues(Ops, dl);
15957 }
15958
15959 SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) const {
15960   MachineFunction &MF = DAG.getMachineFunction();
15961   auto PtrVT = getPointerTy(MF.getDataLayout());
15962   X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
15963
15964   const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
15965   SDLoc DL(Op);
15966
15967   if (!Subtarget->is64Bit() ||
15968       Subtarget->isCallingConvWin64(MF.getFunction()->getCallingConv())) {
15969     // vastart just stores the address of the VarArgsFrameIndex slot into the
15970     // memory location argument.
15971     SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
15972     return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
15973                         MachinePointerInfo(SV), false, false, 0);
15974   }
15975
15976   // __va_list_tag:
15977   //   gp_offset         (0 - 6 * 8)
15978   //   fp_offset         (48 - 48 + 8 * 16)
15979   //   overflow_arg_area (point to parameters coming in memory).
15980   //   reg_save_area
15981   SmallVector<SDValue, 8> MemOps;
15982   SDValue FIN = Op.getOperand(1);
15983   // Store gp_offset
15984   SDValue Store = DAG.getStore(Op.getOperand(0), DL,
15985                                DAG.getConstant(FuncInfo->getVarArgsGPOffset(),
15986                                                DL, MVT::i32),
15987                                FIN, MachinePointerInfo(SV), false, false, 0);
15988   MemOps.push_back(Store);
15989
15990   // Store fp_offset
15991   FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN, DAG.getIntPtrConstant(4, DL));
15992   Store = DAG.getStore(Op.getOperand(0), DL,
15993                        DAG.getConstant(FuncInfo->getVarArgsFPOffset(), DL,
15994                                        MVT::i32),
15995                        FIN, MachinePointerInfo(SV, 4), false, false, 0);
15996   MemOps.push_back(Store);
15997
15998   // Store ptr to overflow_arg_area
15999   FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN, DAG.getIntPtrConstant(4, DL));
16000   SDValue OVFIN = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
16001   Store = DAG.getStore(Op.getOperand(0), DL, OVFIN, FIN,
16002                        MachinePointerInfo(SV, 8),
16003                        false, false, 0);
16004   MemOps.push_back(Store);
16005
16006   // Store ptr to reg_save_area.
16007   FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN, DAG.getIntPtrConstant(
16008       Subtarget->isTarget64BitLP64() ? 8 : 4, DL));
16009   SDValue RSFIN = DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), PtrVT);
16010   Store = DAG.getStore(Op.getOperand(0), DL, RSFIN, FIN, MachinePointerInfo(
16011       SV, Subtarget->isTarget64BitLP64() ? 16 : 12), false, false, 0);
16012   MemOps.push_back(Store);
16013   return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps);
16014 }
16015
16016 SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) const {
16017   assert(Subtarget->is64Bit() &&
16018          "LowerVAARG only handles 64-bit va_arg!");
16019   assert(Op.getNode()->getNumOperands() == 4);
16020
16021   MachineFunction &MF = DAG.getMachineFunction();
16022   if (Subtarget->isCallingConvWin64(MF.getFunction()->getCallingConv()))
16023     // The Win64 ABI uses char* instead of a structure.
16024     return DAG.expandVAArg(Op.getNode());
16025
16026   SDValue Chain = Op.getOperand(0);
16027   SDValue SrcPtr = Op.getOperand(1);
16028   const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
16029   unsigned Align = Op.getConstantOperandVal(3);
16030   SDLoc dl(Op);
16031
16032   EVT ArgVT = Op.getNode()->getValueType(0);
16033   Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
16034   uint32_t ArgSize = DAG.getDataLayout().getTypeAllocSize(ArgTy);
16035   uint8_t ArgMode;
16036
16037   // Decide which area this value should be read from.
16038   // TODO: Implement the AMD64 ABI in its entirety. This simple
16039   // selection mechanism works only for the basic types.
16040   if (ArgVT == MVT::f80) {
16041     llvm_unreachable("va_arg for f80 not yet implemented");
16042   } else if (ArgVT.isFloatingPoint() && ArgSize <= 16 /*bytes*/) {
16043     ArgMode = 2;  // Argument passed in XMM register. Use fp_offset.
16044   } else if (ArgVT.isInteger() && ArgSize <= 32 /*bytes*/) {
16045     ArgMode = 1;  // Argument passed in GPR64 register(s). Use gp_offset.
16046   } else {
16047     llvm_unreachable("Unhandled argument type in LowerVAARG");
16048   }
16049
16050   if (ArgMode == 2) {
16051     // Sanity Check: Make sure using fp_offset makes sense.
16052     assert(!Subtarget->useSoftFloat() &&
16053            !(MF.getFunction()->hasFnAttribute(Attribute::NoImplicitFloat)) &&
16054            Subtarget->hasSSE1());
16055   }
16056
16057   // Insert VAARG_64 node into the DAG
16058   // VAARG_64 returns two values: Variable Argument Address, Chain
16059   SDValue InstOps[] = {Chain, SrcPtr, DAG.getConstant(ArgSize, dl, MVT::i32),
16060                        DAG.getConstant(ArgMode, dl, MVT::i8),
16061                        DAG.getConstant(Align, dl, MVT::i32)};
16062   SDVTList VTs = DAG.getVTList(getPointerTy(DAG.getDataLayout()), MVT::Other);
16063   SDValue VAARG = DAG.getMemIntrinsicNode(X86ISD::VAARG_64, dl,
16064                                           VTs, InstOps, MVT::i64,
16065                                           MachinePointerInfo(SV),
16066                                           /*Align=*/0,
16067                                           /*Volatile=*/false,
16068                                           /*ReadMem=*/true,
16069                                           /*WriteMem=*/true);
16070   Chain = VAARG.getValue(1);
16071
16072   // Load the next argument and return it
16073   return DAG.getLoad(ArgVT, dl,
16074                      Chain,
16075                      VAARG,
16076                      MachinePointerInfo(),
16077                      false, false, false, 0);
16078 }
16079
16080 static SDValue LowerVACOPY(SDValue Op, const X86Subtarget *Subtarget,
16081                            SelectionDAG &DAG) {
16082   // X86-64 va_list is a struct { i32, i32, i8*, i8* }, except on Windows,
16083   // where a va_list is still an i8*.
16084   assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
16085   if (Subtarget->isCallingConvWin64(
16086         DAG.getMachineFunction().getFunction()->getCallingConv()))
16087     // Probably a Win64 va_copy.
16088     return DAG.expandVACopy(Op.getNode());
16089
16090   SDValue Chain = Op.getOperand(0);
16091   SDValue DstPtr = Op.getOperand(1);
16092   SDValue SrcPtr = Op.getOperand(2);
16093   const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
16094   const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
16095   SDLoc DL(Op);
16096
16097   return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr,
16098                        DAG.getIntPtrConstant(24, DL), 8, /*isVolatile*/false,
16099                        false, false,
16100                        MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
16101 }
16102
16103 // getTargetVShiftByConstNode - Handle vector element shifts where the shift
16104 // amount is a constant. Takes immediate version of shift as input.
16105 static SDValue getTargetVShiftByConstNode(unsigned Opc, SDLoc dl, MVT VT,
16106                                           SDValue SrcOp, uint64_t ShiftAmt,
16107                                           SelectionDAG &DAG) {
16108   MVT ElementType = VT.getVectorElementType();
16109
16110   // Fold this packed shift into its first operand if ShiftAmt is 0.
16111   if (ShiftAmt == 0)
16112     return SrcOp;
16113
16114   // Check for ShiftAmt >= element width
16115   if (ShiftAmt >= ElementType.getSizeInBits()) {
16116     if (Opc == X86ISD::VSRAI)
16117       ShiftAmt = ElementType.getSizeInBits() - 1;
16118     else
16119       return DAG.getConstant(0, dl, VT);
16120   }
16121
16122   assert((Opc == X86ISD::VSHLI || Opc == X86ISD::VSRLI || Opc == X86ISD::VSRAI)
16123          && "Unknown target vector shift-by-constant node");
16124
16125   // Fold this packed vector shift into a build vector if SrcOp is a
16126   // vector of Constants or UNDEFs, and SrcOp valuetype is the same as VT.
16127   if (VT == SrcOp.getSimpleValueType() &&
16128       ISD::isBuildVectorOfConstantSDNodes(SrcOp.getNode())) {
16129     SmallVector<SDValue, 8> Elts;
16130     unsigned NumElts = SrcOp->getNumOperands();
16131     ConstantSDNode *ND;
16132
16133     switch(Opc) {
16134     default: llvm_unreachable(nullptr);
16135     case X86ISD::VSHLI:
16136       for (unsigned i=0; i!=NumElts; ++i) {
16137         SDValue CurrentOp = SrcOp->getOperand(i);
16138         if (CurrentOp->getOpcode() == ISD::UNDEF) {
16139           Elts.push_back(CurrentOp);
16140           continue;
16141         }
16142         ND = cast<ConstantSDNode>(CurrentOp);
16143         const APInt &C = ND->getAPIntValue();
16144         Elts.push_back(DAG.getConstant(C.shl(ShiftAmt), dl, ElementType));
16145       }
16146       break;
16147     case X86ISD::VSRLI:
16148       for (unsigned i=0; i!=NumElts; ++i) {
16149         SDValue CurrentOp = SrcOp->getOperand(i);
16150         if (CurrentOp->getOpcode() == ISD::UNDEF) {
16151           Elts.push_back(CurrentOp);
16152           continue;
16153         }
16154         ND = cast<ConstantSDNode>(CurrentOp);
16155         const APInt &C = ND->getAPIntValue();
16156         Elts.push_back(DAG.getConstant(C.lshr(ShiftAmt), dl, ElementType));
16157       }
16158       break;
16159     case X86ISD::VSRAI:
16160       for (unsigned i=0; i!=NumElts; ++i) {
16161         SDValue CurrentOp = SrcOp->getOperand(i);
16162         if (CurrentOp->getOpcode() == ISD::UNDEF) {
16163           Elts.push_back(CurrentOp);
16164           continue;
16165         }
16166         ND = cast<ConstantSDNode>(CurrentOp);
16167         const APInt &C = ND->getAPIntValue();
16168         Elts.push_back(DAG.getConstant(C.ashr(ShiftAmt), dl, ElementType));
16169       }
16170       break;
16171     }
16172
16173     return DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Elts);
16174   }
16175
16176   return DAG.getNode(Opc, dl, VT, SrcOp,
16177                      DAG.getConstant(ShiftAmt, dl, MVT::i8));
16178 }
16179
16180 // getTargetVShiftNode - Handle vector element shifts where the shift amount
16181 // may or may not be a constant. Takes immediate version of shift as input.
16182 static SDValue getTargetVShiftNode(unsigned Opc, SDLoc dl, MVT VT,
16183                                    SDValue SrcOp, SDValue ShAmt,
16184                                    SelectionDAG &DAG) {
16185   MVT SVT = ShAmt.getSimpleValueType();
16186   assert((SVT == MVT::i32 || SVT == MVT::i64) && "Unexpected value type!");
16187
16188   // Catch shift-by-constant.
16189   if (ConstantSDNode *CShAmt = dyn_cast<ConstantSDNode>(ShAmt))
16190     return getTargetVShiftByConstNode(Opc, dl, VT, SrcOp,
16191                                       CShAmt->getZExtValue(), DAG);
16192
16193   // Change opcode to non-immediate version
16194   switch (Opc) {
16195     default: llvm_unreachable("Unknown target vector shift node");
16196     case X86ISD::VSHLI: Opc = X86ISD::VSHL; break;
16197     case X86ISD::VSRLI: Opc = X86ISD::VSRL; break;
16198     case X86ISD::VSRAI: Opc = X86ISD::VSRA; break;
16199   }
16200
16201   const X86Subtarget &Subtarget =
16202       static_cast<const X86Subtarget &>(DAG.getSubtarget());
16203   if (Subtarget.hasSSE41() && ShAmt.getOpcode() == ISD::ZERO_EXTEND &&
16204       ShAmt.getOperand(0).getSimpleValueType() == MVT::i16) {
16205     // Let the shuffle legalizer expand this shift amount node.
16206     SDValue Op0 = ShAmt.getOperand(0);
16207     Op0 = DAG.getNode(ISD::SCALAR_TO_VECTOR, SDLoc(Op0), MVT::v8i16, Op0);
16208     ShAmt = getShuffleVectorZeroOrUndef(Op0, 0, true, &Subtarget, DAG);
16209   } else {
16210     // Need to build a vector containing shift amount.
16211     // SSE/AVX packed shifts only use the lower 64-bit of the shift count.
16212     SmallVector<SDValue, 4> ShOps;
16213     ShOps.push_back(ShAmt);
16214     if (SVT == MVT::i32) {
16215       ShOps.push_back(DAG.getConstant(0, dl, SVT));
16216       ShOps.push_back(DAG.getUNDEF(SVT));
16217     }
16218     ShOps.push_back(DAG.getUNDEF(SVT));
16219
16220     MVT BVT = SVT == MVT::i32 ? MVT::v4i32 : MVT::v2i64;
16221     ShAmt = DAG.getNode(ISD::BUILD_VECTOR, dl, BVT, ShOps);
16222   }
16223
16224   // The return type has to be a 128-bit type with the same element
16225   // type as the input type.
16226   MVT EltVT = VT.getVectorElementType();
16227   MVT ShVT = MVT::getVectorVT(EltVT, 128/EltVT.getSizeInBits());
16228
16229   ShAmt = DAG.getBitcast(ShVT, ShAmt);
16230   return DAG.getNode(Opc, dl, VT, SrcOp, ShAmt);
16231 }
16232
16233 /// \brief Return Mask with the necessary casting or extending
16234 /// for \p Mask according to \p MaskVT when lowering masking intrinsics
16235 static SDValue getMaskNode(SDValue Mask, MVT MaskVT,
16236                            const X86Subtarget *Subtarget,
16237                            SelectionDAG &DAG, SDLoc dl) {
16238
16239   if (MaskVT.bitsGT(Mask.getSimpleValueType())) {
16240     // Mask should be extended
16241     Mask = DAG.getNode(ISD::ANY_EXTEND, dl,
16242                        MVT::getIntegerVT(MaskVT.getSizeInBits()), Mask);
16243   }
16244
16245   if (Mask.getSimpleValueType() == MVT::i64 && Subtarget->is32Bit()) {
16246     if (MaskVT == MVT::v64i1) {
16247       assert(Subtarget->hasBWI() && "Expected AVX512BW target!");
16248       // In case 32bit mode, bitcast i64 is illegal, extend/split it.
16249       SDValue Lo, Hi;
16250       Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Mask,
16251                           DAG.getConstant(0, dl, MVT::i32));
16252       Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, Mask,
16253                           DAG.getConstant(1, dl, MVT::i32));
16254
16255       Lo = DAG.getBitcast(MVT::v32i1, Lo);
16256       Hi = DAG.getBitcast(MVT::v32i1, Hi);
16257
16258       return DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v64i1, Lo, Hi);
16259     } else {
16260       // MaskVT require < 64bit. Truncate mask (should succeed in any case),
16261       // and bitcast.
16262       MVT TruncVT = MVT::getIntegerVT(MaskVT.getSizeInBits());
16263       return DAG.getBitcast(MaskVT,
16264                             DAG.getNode(ISD::TRUNCATE, dl, TruncVT, Mask));
16265     }
16266
16267   } else {
16268     MVT BitcastVT = MVT::getVectorVT(MVT::i1,
16269                                      Mask.getSimpleValueType().getSizeInBits());
16270     // In case when MaskVT equals v2i1 or v4i1, low 2 or 4 elements
16271     // are extracted by EXTRACT_SUBVECTOR.
16272     return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MaskVT,
16273                        DAG.getBitcast(BitcastVT, Mask),
16274                        DAG.getIntPtrConstant(0, dl));
16275   }
16276 }
16277
16278 /// \brief Return (and \p Op, \p Mask) for compare instructions or
16279 /// (vselect \p Mask, \p Op, \p PreservedSrc) for others along with the
16280 /// necessary casting or extending for \p Mask when lowering masking intrinsics
16281 static SDValue getVectorMaskingNode(SDValue Op, SDValue Mask,
16282                   SDValue PreservedSrc,
16283                   const X86Subtarget *Subtarget,
16284                   SelectionDAG &DAG) {
16285   MVT VT = Op.getSimpleValueType();
16286   MVT MaskVT = MVT::getVectorVT(MVT::i1, VT.getVectorNumElements());
16287   unsigned OpcodeSelect = ISD::VSELECT;
16288   SDLoc dl(Op);
16289
16290   if (isAllOnesConstant(Mask))
16291     return Op;
16292
16293   SDValue VMask = getMaskNode(Mask, MaskVT, Subtarget, DAG, dl);
16294
16295   switch (Op.getOpcode()) {
16296   default: break;
16297   case X86ISD::PCMPEQM:
16298   case X86ISD::PCMPGTM:
16299   case X86ISD::CMPM:
16300   case X86ISD::CMPMU:
16301     return DAG.getNode(ISD::AND, dl, VT, Op, VMask);
16302   case X86ISD::VFPCLASS:
16303     case X86ISD::VFPCLASSS:
16304     return DAG.getNode(ISD::OR, dl, VT, Op, VMask);
16305   case X86ISD::VTRUNC:
16306   case X86ISD::VTRUNCS:
16307   case X86ISD::VTRUNCUS:
16308     // We can't use ISD::VSELECT here because it is not always "Legal"
16309     // for the destination type. For example vpmovqb require only AVX512
16310     // and vselect that can operate on byte element type require BWI
16311     OpcodeSelect = X86ISD::SELECT;
16312     break;
16313   }
16314   if (PreservedSrc.getOpcode() == ISD::UNDEF)
16315     PreservedSrc = getZeroVector(VT, Subtarget, DAG, dl);
16316   return DAG.getNode(OpcodeSelect, dl, VT, VMask, Op, PreservedSrc);
16317 }
16318
16319 /// \brief Creates an SDNode for a predicated scalar operation.
16320 /// \returns (X86vselect \p Mask, \p Op, \p PreservedSrc).
16321 /// The mask is coming as MVT::i8 and it should be truncated
16322 /// to MVT::i1 while lowering masking intrinsics.
16323 /// The main difference between ScalarMaskingNode and VectorMaskingNode is using
16324 /// "X86select" instead of "vselect". We just can't create the "vselect" node
16325 /// for a scalar instruction.
16326 static SDValue getScalarMaskingNode(SDValue Op, SDValue Mask,
16327                                     SDValue PreservedSrc,
16328                                     const X86Subtarget *Subtarget,
16329                                     SelectionDAG &DAG) {
16330   if (isAllOnesConstant(Mask))
16331     return Op;
16332
16333   MVT VT = Op.getSimpleValueType();
16334   SDLoc dl(Op);
16335   // The mask should be of type MVT::i1
16336   SDValue IMask = DAG.getNode(ISD::TRUNCATE, dl, MVT::i1, Mask);
16337
16338   if (Op.getOpcode() == X86ISD::FSETCC)
16339     return DAG.getNode(ISD::AND, dl, VT, Op, IMask);
16340   if (Op.getOpcode() == X86ISD::VFPCLASS ||
16341       Op.getOpcode() == X86ISD::VFPCLASSS)
16342     return DAG.getNode(ISD::OR, dl, VT, Op, IMask);
16343
16344   if (PreservedSrc.getOpcode() == ISD::UNDEF)
16345     PreservedSrc = getZeroVector(VT, Subtarget, DAG, dl);
16346   return DAG.getNode(X86ISD::SELECT, dl, VT, IMask, Op, PreservedSrc);
16347 }
16348
16349 static int getSEHRegistrationNodeSize(const Function *Fn) {
16350   if (!Fn->hasPersonalityFn())
16351     report_fatal_error(
16352         "querying registration node size for function without personality");
16353   // The RegNodeSize is 6 32-bit words for SEH and 4 for C++ EH. See
16354   // WinEHStatePass for the full struct definition.
16355   switch (classifyEHPersonality(Fn->getPersonalityFn())) {
16356   case EHPersonality::MSVC_X86SEH: return 24;
16357   case EHPersonality::MSVC_CXX: return 16;
16358   default: break;
16359   }
16360   report_fatal_error(
16361       "can only recover FP for 32-bit MSVC EH personality functions");
16362 }
16363
16364 /// When the MSVC runtime transfers control to us, either to an outlined
16365 /// function or when returning to a parent frame after catching an exception, we
16366 /// recover the parent frame pointer by doing arithmetic on the incoming EBP.
16367 /// Here's the math:
16368 ///   RegNodeBase = EntryEBP - RegNodeSize
16369 ///   ParentFP = RegNodeBase - ParentFrameOffset
16370 /// Subtracting RegNodeSize takes us to the offset of the registration node, and
16371 /// subtracting the offset (negative on x86) takes us back to the parent FP.
16372 static SDValue recoverFramePointer(SelectionDAG &DAG, const Function *Fn,
16373                                    SDValue EntryEBP) {
16374   MachineFunction &MF = DAG.getMachineFunction();
16375   SDLoc dl;
16376
16377   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
16378   MVT PtrVT = TLI.getPointerTy(DAG.getDataLayout());
16379
16380   // It's possible that the parent function no longer has a personality function
16381   // if the exceptional code was optimized away, in which case we just return
16382   // the incoming EBP.
16383   if (!Fn->hasPersonalityFn())
16384     return EntryEBP;
16385
16386   // Get an MCSymbol that will ultimately resolve to the frame offset of the EH
16387   // registration, or the .set_setframe offset.
16388   MCSymbol *OffsetSym =
16389       MF.getMMI().getContext().getOrCreateParentFrameOffsetSymbol(
16390           GlobalValue::getRealLinkageName(Fn->getName()));
16391   SDValue OffsetSymVal = DAG.getMCSymbol(OffsetSym, PtrVT);
16392   SDValue ParentFrameOffset =
16393       DAG.getNode(ISD::LOCAL_RECOVER, dl, PtrVT, OffsetSymVal);
16394
16395   // Return EntryEBP + ParentFrameOffset for x64. This adjusts from RSP after
16396   // prologue to RBP in the parent function.
16397   const X86Subtarget &Subtarget =
16398       static_cast<const X86Subtarget &>(DAG.getSubtarget());
16399   if (Subtarget.is64Bit())
16400     return DAG.getNode(ISD::ADD, dl, PtrVT, EntryEBP, ParentFrameOffset);
16401
16402   int RegNodeSize = getSEHRegistrationNodeSize(Fn);
16403   // RegNodeBase = EntryEBP - RegNodeSize
16404   // ParentFP = RegNodeBase - ParentFrameOffset
16405   SDValue RegNodeBase = DAG.getNode(ISD::SUB, dl, PtrVT, EntryEBP,
16406                                     DAG.getConstant(RegNodeSize, dl, PtrVT));
16407   return DAG.getNode(ISD::SUB, dl, PtrVT, RegNodeBase, ParentFrameOffset);
16408 }
16409
16410 static SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, const X86Subtarget *Subtarget,
16411                                        SelectionDAG &DAG) {
16412   SDLoc dl(Op);
16413   unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
16414   MVT VT = Op.getSimpleValueType();
16415   const IntrinsicData* IntrData = getIntrinsicWithoutChain(IntNo);
16416   if (IntrData) {
16417     switch(IntrData->Type) {
16418     case INTR_TYPE_1OP:
16419       return DAG.getNode(IntrData->Opc0, dl, Op.getValueType(), Op.getOperand(1));
16420     case INTR_TYPE_2OP:
16421       return DAG.getNode(IntrData->Opc0, dl, Op.getValueType(), Op.getOperand(1),
16422         Op.getOperand(2));
16423     case INTR_TYPE_2OP_IMM8:
16424       return DAG.getNode(IntrData->Opc0, dl, Op.getValueType(), Op.getOperand(1),
16425                          DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op.getOperand(2)));
16426     case INTR_TYPE_3OP:
16427       return DAG.getNode(IntrData->Opc0, dl, Op.getValueType(), Op.getOperand(1),
16428         Op.getOperand(2), Op.getOperand(3));
16429     case INTR_TYPE_4OP:
16430       return DAG.getNode(IntrData->Opc0, dl, Op.getValueType(), Op.getOperand(1),
16431         Op.getOperand(2), Op.getOperand(3), Op.getOperand(4));
16432     case INTR_TYPE_1OP_MASK_RM: {
16433       SDValue Src = Op.getOperand(1);
16434       SDValue PassThru = Op.getOperand(2);
16435       SDValue Mask = Op.getOperand(3);
16436       SDValue RoundingMode;
16437       // We allways add rounding mode to the Node.
16438       // If the rounding mode is not specified, we add the
16439       // "current direction" mode.
16440       if (Op.getNumOperands() == 4)
16441         RoundingMode =
16442           DAG.getConstant(X86::STATIC_ROUNDING::CUR_DIRECTION, dl, MVT::i32);
16443       else
16444         RoundingMode = Op.getOperand(4);
16445       unsigned IntrWithRoundingModeOpcode = IntrData->Opc1;
16446       if (IntrWithRoundingModeOpcode != 0)
16447         if (cast<ConstantSDNode>(RoundingMode)->getZExtValue() !=
16448             X86::STATIC_ROUNDING::CUR_DIRECTION)
16449           return getVectorMaskingNode(DAG.getNode(IntrWithRoundingModeOpcode,
16450                                       dl, Op.getValueType(), Src, RoundingMode),
16451                                       Mask, PassThru, Subtarget, DAG);
16452       return getVectorMaskingNode(DAG.getNode(IntrData->Opc0, dl, VT, Src,
16453                                               RoundingMode),
16454                                   Mask, PassThru, Subtarget, DAG);
16455     }
16456     case INTR_TYPE_1OP_MASK: {
16457       SDValue Src = Op.getOperand(1);
16458       SDValue PassThru = Op.getOperand(2);
16459       SDValue Mask = Op.getOperand(3);
16460       // We add rounding mode to the Node when
16461       //   - RM Opcode is specified and
16462       //   - RM is not "current direction".
16463       unsigned IntrWithRoundingModeOpcode = IntrData->Opc1;
16464       if (IntrWithRoundingModeOpcode != 0) {
16465         SDValue Rnd = Op.getOperand(4);
16466         unsigned Round = cast<ConstantSDNode>(Rnd)->getZExtValue();
16467         if (Round != X86::STATIC_ROUNDING::CUR_DIRECTION) {
16468           return getVectorMaskingNode(DAG.getNode(IntrWithRoundingModeOpcode,
16469                                       dl, Op.getValueType(),
16470                                       Src, Rnd),
16471                                       Mask, PassThru, Subtarget, DAG);
16472         }
16473       }
16474       return getVectorMaskingNode(DAG.getNode(IntrData->Opc0, dl, VT, Src),
16475                                   Mask, PassThru, Subtarget, DAG);
16476     }
16477     case INTR_TYPE_SCALAR_MASK: {
16478       SDValue Src1 = Op.getOperand(1);
16479       SDValue Src2 = Op.getOperand(2);
16480       SDValue passThru = Op.getOperand(3);
16481       SDValue Mask = Op.getOperand(4);
16482       return getScalarMaskingNode(DAG.getNode(IntrData->Opc0, dl, VT, Src1, Src2),
16483                                   Mask, passThru, Subtarget, DAG);
16484     }
16485     case INTR_TYPE_SCALAR_MASK_RM: {
16486       SDValue Src1 = Op.getOperand(1);
16487       SDValue Src2 = Op.getOperand(2);
16488       SDValue Src0 = Op.getOperand(3);
16489       SDValue Mask = Op.getOperand(4);
16490       // There are 2 kinds of intrinsics in this group:
16491       // (1) With suppress-all-exceptions (sae) or rounding mode- 6 operands
16492       // (2) With rounding mode and sae - 7 operands.
16493       if (Op.getNumOperands() == 6) {
16494         SDValue Sae  = Op.getOperand(5);
16495         unsigned Opc = IntrData->Opc1 ? IntrData->Opc1 : IntrData->Opc0;
16496         return getScalarMaskingNode(DAG.getNode(Opc, dl, VT, Src1, Src2,
16497                                                 Sae),
16498                                     Mask, Src0, Subtarget, DAG);
16499       }
16500       assert(Op.getNumOperands() == 7 && "Unexpected intrinsic form");
16501       SDValue RoundingMode  = Op.getOperand(5);
16502       SDValue Sae  = Op.getOperand(6);
16503       return getScalarMaskingNode(DAG.getNode(IntrData->Opc0, dl, VT, Src1, Src2,
16504                                               RoundingMode, Sae),
16505                                   Mask, Src0, Subtarget, DAG);
16506     }
16507     case INTR_TYPE_2OP_MASK:
16508     case INTR_TYPE_2OP_IMM8_MASK: {
16509       SDValue Src1 = Op.getOperand(1);
16510       SDValue Src2 = Op.getOperand(2);
16511       SDValue PassThru = Op.getOperand(3);
16512       SDValue Mask = Op.getOperand(4);
16513
16514       if (IntrData->Type == INTR_TYPE_2OP_IMM8_MASK)
16515         Src2 = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Src2);
16516
16517       // We specify 2 possible opcodes for intrinsics with rounding modes.
16518       // First, we check if the intrinsic may have non-default rounding mode,
16519       // (IntrData->Opc1 != 0), then we check the rounding mode operand.
16520       unsigned IntrWithRoundingModeOpcode = IntrData->Opc1;
16521       if (IntrWithRoundingModeOpcode != 0) {
16522         SDValue Rnd = Op.getOperand(5);
16523         unsigned Round = cast<ConstantSDNode>(Rnd)->getZExtValue();
16524         if (Round != X86::STATIC_ROUNDING::CUR_DIRECTION) {
16525           return getVectorMaskingNode(DAG.getNode(IntrWithRoundingModeOpcode,
16526                                       dl, Op.getValueType(),
16527                                       Src1, Src2, Rnd),
16528                                       Mask, PassThru, Subtarget, DAG);
16529         }
16530       }
16531       // TODO: Intrinsics should have fast-math-flags to propagate.
16532       return getVectorMaskingNode(DAG.getNode(IntrData->Opc0, dl, VT,Src1,Src2),
16533                                   Mask, PassThru, Subtarget, DAG);
16534     }
16535     case INTR_TYPE_2OP_MASK_RM: {
16536       SDValue Src1 = Op.getOperand(1);
16537       SDValue Src2 = Op.getOperand(2);
16538       SDValue PassThru = Op.getOperand(3);
16539       SDValue Mask = Op.getOperand(4);
16540       // We specify 2 possible modes for intrinsics, with/without rounding
16541       // modes.
16542       // First, we check if the intrinsic have rounding mode (6 operands),
16543       // if not, we set rounding mode to "current".
16544       SDValue Rnd;
16545       if (Op.getNumOperands() == 6)
16546         Rnd = Op.getOperand(5);
16547       else
16548         Rnd = DAG.getConstant(X86::STATIC_ROUNDING::CUR_DIRECTION, dl, MVT::i32);
16549       return getVectorMaskingNode(DAG.getNode(IntrData->Opc0, dl, VT,
16550                                               Src1, Src2, Rnd),
16551                                   Mask, PassThru, Subtarget, DAG);
16552     }
16553     case INTR_TYPE_3OP_SCALAR_MASK_RM: {
16554       SDValue Src1 = Op.getOperand(1);
16555       SDValue Src2 = Op.getOperand(2);
16556       SDValue Src3 = Op.getOperand(3);
16557       SDValue PassThru = Op.getOperand(4);
16558       SDValue Mask = Op.getOperand(5);
16559       SDValue Sae  = Op.getOperand(6);
16560
16561       return getScalarMaskingNode(DAG.getNode(IntrData->Opc0, dl, VT, Src1,
16562                                               Src2, Src3, Sae),
16563                                   Mask, PassThru, Subtarget, DAG);
16564     }
16565     case INTR_TYPE_3OP_MASK_RM: {
16566       SDValue Src1 = Op.getOperand(1);
16567       SDValue Src2 = Op.getOperand(2);
16568       SDValue Imm = Op.getOperand(3);
16569       SDValue PassThru = Op.getOperand(4);
16570       SDValue Mask = Op.getOperand(5);
16571       // We specify 2 possible modes for intrinsics, with/without rounding
16572       // modes.
16573       // First, we check if the intrinsic have rounding mode (7 operands),
16574       // if not, we set rounding mode to "current".
16575       SDValue Rnd;
16576       if (Op.getNumOperands() == 7)
16577         Rnd = Op.getOperand(6);
16578       else
16579         Rnd = DAG.getConstant(X86::STATIC_ROUNDING::CUR_DIRECTION, dl, MVT::i32);
16580       return getVectorMaskingNode(DAG.getNode(IntrData->Opc0, dl, VT,
16581         Src1, Src2, Imm, Rnd),
16582         Mask, PassThru, Subtarget, DAG);
16583     }
16584     case INTR_TYPE_3OP_IMM8_MASK:
16585     case INTR_TYPE_3OP_MASK:
16586     case INSERT_SUBVEC: {
16587       SDValue Src1 = Op.getOperand(1);
16588       SDValue Src2 = Op.getOperand(2);
16589       SDValue Src3 = Op.getOperand(3);
16590       SDValue PassThru = Op.getOperand(4);
16591       SDValue Mask = Op.getOperand(5);
16592
16593       if (IntrData->Type == INTR_TYPE_3OP_IMM8_MASK)
16594         Src3 = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Src3);
16595       else if (IntrData->Type == INSERT_SUBVEC) {
16596         // imm should be adapted to ISD::INSERT_SUBVECTOR behavior
16597         assert(isa<ConstantSDNode>(Src3) && "Expected a ConstantSDNode here!");
16598         unsigned Imm = cast<ConstantSDNode>(Src3)->getZExtValue();
16599         Imm *= Src2.getSimpleValueType().getVectorNumElements();
16600         Src3 = DAG.getTargetConstant(Imm, dl, MVT::i32);
16601       }
16602
16603       // We specify 2 possible opcodes for intrinsics with rounding modes.
16604       // First, we check if the intrinsic may have non-default rounding mode,
16605       // (IntrData->Opc1 != 0), then we check the rounding mode operand.
16606       unsigned IntrWithRoundingModeOpcode = IntrData->Opc1;
16607       if (IntrWithRoundingModeOpcode != 0) {
16608         SDValue Rnd = Op.getOperand(6);
16609         unsigned Round = cast<ConstantSDNode>(Rnd)->getZExtValue();
16610         if (Round != X86::STATIC_ROUNDING::CUR_DIRECTION) {
16611           return getVectorMaskingNode(DAG.getNode(IntrWithRoundingModeOpcode,
16612                                       dl, Op.getValueType(),
16613                                       Src1, Src2, Src3, Rnd),
16614                                       Mask, PassThru, Subtarget, DAG);
16615         }
16616       }
16617       return getVectorMaskingNode(DAG.getNode(IntrData->Opc0, dl, VT,
16618                                               Src1, Src2, Src3),
16619                                   Mask, PassThru, Subtarget, DAG);
16620     }
16621     case VPERM_3OP_MASKZ:
16622     case VPERM_3OP_MASK:{
16623       // Src2 is the PassThru
16624       SDValue Src1 = Op.getOperand(1);
16625       SDValue Src2 = Op.getOperand(2);
16626       SDValue Src3 = Op.getOperand(3);
16627       SDValue Mask = Op.getOperand(4);
16628       MVT VT = Op.getSimpleValueType();
16629       SDValue PassThru = SDValue();
16630
16631       // set PassThru element
16632       if (IntrData->Type == VPERM_3OP_MASKZ)
16633         PassThru = getZeroVector(VT, Subtarget, DAG, dl);
16634       else
16635         PassThru = DAG.getBitcast(VT, Src2);
16636
16637       // Swap Src1 and Src2 in the node creation
16638       return getVectorMaskingNode(DAG.getNode(IntrData->Opc0,
16639                                               dl, Op.getValueType(),
16640                                               Src2, Src1, Src3),
16641                                   Mask, PassThru, Subtarget, DAG);
16642     }
16643     case FMA_OP_MASK3:
16644     case FMA_OP_MASKZ:
16645     case FMA_OP_MASK: {
16646       SDValue Src1 = Op.getOperand(1);
16647       SDValue Src2 = Op.getOperand(2);
16648       SDValue Src3 = Op.getOperand(3);
16649       SDValue Mask = Op.getOperand(4);
16650       MVT VT = Op.getSimpleValueType();
16651       SDValue PassThru = SDValue();
16652
16653       // set PassThru element
16654       if (IntrData->Type == FMA_OP_MASKZ)
16655         PassThru = getZeroVector(VT, Subtarget, DAG, dl);
16656       else if (IntrData->Type == FMA_OP_MASK3)
16657         PassThru = Src3;
16658       else
16659         PassThru = Src1;
16660
16661       // We specify 2 possible opcodes for intrinsics with rounding modes.
16662       // First, we check if the intrinsic may have non-default rounding mode,
16663       // (IntrData->Opc1 != 0), then we check the rounding mode operand.
16664       unsigned IntrWithRoundingModeOpcode = IntrData->Opc1;
16665       if (IntrWithRoundingModeOpcode != 0) {
16666         SDValue Rnd = Op.getOperand(5);
16667         if (cast<ConstantSDNode>(Rnd)->getZExtValue() !=
16668             X86::STATIC_ROUNDING::CUR_DIRECTION)
16669           return getVectorMaskingNode(DAG.getNode(IntrWithRoundingModeOpcode,
16670                                                   dl, Op.getValueType(),
16671                                                   Src1, Src2, Src3, Rnd),
16672                                       Mask, PassThru, Subtarget, DAG);
16673       }
16674       return getVectorMaskingNode(DAG.getNode(IntrData->Opc0,
16675                                               dl, Op.getValueType(),
16676                                               Src1, Src2, Src3),
16677                                   Mask, PassThru, Subtarget, DAG);
16678     }
16679     case TERLOG_OP_MASK:
16680     case TERLOG_OP_MASKZ: {
16681       SDValue Src1 = Op.getOperand(1);
16682       SDValue Src2 = Op.getOperand(2);
16683       SDValue Src3 = Op.getOperand(3);
16684       SDValue Src4 = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op.getOperand(4));
16685       SDValue Mask = Op.getOperand(5);
16686       MVT VT = Op.getSimpleValueType();
16687       SDValue PassThru = Src1;
16688       // Set PassThru element.
16689       if (IntrData->Type == TERLOG_OP_MASKZ)
16690         PassThru = getZeroVector(VT, Subtarget, DAG, dl);
16691
16692       return getVectorMaskingNode(DAG.getNode(IntrData->Opc0, dl, VT,
16693                                               Src1, Src2, Src3, Src4),
16694                                   Mask, PassThru, Subtarget, DAG);
16695     }
16696     case FPCLASS: {
16697       // FPclass intrinsics with mask
16698        SDValue Src1 = Op.getOperand(1);
16699        MVT VT = Src1.getSimpleValueType();
16700        MVT MaskVT = MVT::getVectorVT(MVT::i1, VT.getVectorNumElements());
16701        SDValue Imm = Op.getOperand(2);
16702        SDValue Mask = Op.getOperand(3);
16703        MVT BitcastVT = MVT::getVectorVT(MVT::i1,
16704                                      Mask.getSimpleValueType().getSizeInBits());
16705        SDValue FPclass = DAG.getNode(IntrData->Opc0, dl, MaskVT, Src1, Imm);
16706        SDValue FPclassMask = getVectorMaskingNode(FPclass, Mask,
16707                                                  DAG.getTargetConstant(0, dl, MaskVT),
16708                                                  Subtarget, DAG);
16709        SDValue Res = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, BitcastVT,
16710                                  DAG.getUNDEF(BitcastVT), FPclassMask,
16711                                  DAG.getIntPtrConstant(0, dl));
16712        return DAG.getBitcast(Op.getValueType(), Res);
16713     }
16714     case FPCLASSS: {
16715       SDValue Src1 = Op.getOperand(1);
16716       SDValue Imm = Op.getOperand(2);
16717       SDValue Mask = Op.getOperand(3);
16718       SDValue FPclass = DAG.getNode(IntrData->Opc0, dl, MVT::i1, Src1, Imm);
16719       SDValue FPclassMask = getScalarMaskingNode(FPclass, Mask,
16720         DAG.getTargetConstant(0, dl, MVT::i1), Subtarget, DAG);
16721       return DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::i8, FPclassMask);
16722     }
16723     case CMP_MASK:
16724     case CMP_MASK_CC: {
16725       // Comparison intrinsics with masks.
16726       // Example of transformation:
16727       // (i8 (int_x86_avx512_mask_pcmpeq_q_128
16728       //             (v2i64 %a), (v2i64 %b), (i8 %mask))) ->
16729       // (i8 (bitcast
16730       //   (v8i1 (insert_subvector undef,
16731       //           (v2i1 (and (PCMPEQM %a, %b),
16732       //                      (extract_subvector
16733       //                         (v8i1 (bitcast %mask)), 0))), 0))))
16734       MVT VT = Op.getOperand(1).getSimpleValueType();
16735       MVT MaskVT = MVT::getVectorVT(MVT::i1, VT.getVectorNumElements());
16736       SDValue Mask = Op.getOperand((IntrData->Type == CMP_MASK_CC) ? 4 : 3);
16737       MVT BitcastVT = MVT::getVectorVT(MVT::i1,
16738                                        Mask.getSimpleValueType().getSizeInBits());
16739       SDValue Cmp;
16740       if (IntrData->Type == CMP_MASK_CC) {
16741         SDValue CC = Op.getOperand(3);
16742         CC = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, CC);
16743         // We specify 2 possible opcodes for intrinsics with rounding modes.
16744         // First, we check if the intrinsic may have non-default rounding mode,
16745         // (IntrData->Opc1 != 0), then we check the rounding mode operand.
16746         if (IntrData->Opc1 != 0) {
16747           SDValue Rnd = Op.getOperand(5);
16748           if (cast<ConstantSDNode>(Rnd)->getZExtValue() !=
16749               X86::STATIC_ROUNDING::CUR_DIRECTION)
16750             Cmp = DAG.getNode(IntrData->Opc1, dl, MaskVT, Op.getOperand(1),
16751                               Op.getOperand(2), CC, Rnd);
16752         }
16753         //default rounding mode
16754         if(!Cmp.getNode())
16755             Cmp = DAG.getNode(IntrData->Opc0, dl, MaskVT, Op.getOperand(1),
16756                               Op.getOperand(2), CC);
16757
16758       } else {
16759         assert(IntrData->Type == CMP_MASK && "Unexpected intrinsic type!");
16760         Cmp = DAG.getNode(IntrData->Opc0, dl, MaskVT, Op.getOperand(1),
16761                           Op.getOperand(2));
16762       }
16763       SDValue CmpMask = getVectorMaskingNode(Cmp, Mask,
16764                                              DAG.getTargetConstant(0, dl,
16765                                                                    MaskVT),
16766                                              Subtarget, DAG);
16767       SDValue Res = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, BitcastVT,
16768                                 DAG.getUNDEF(BitcastVT), CmpMask,
16769                                 DAG.getIntPtrConstant(0, dl));
16770       return DAG.getBitcast(Op.getValueType(), Res);
16771     }
16772     case CMP_MASK_SCALAR_CC: {
16773       SDValue Src1 = Op.getOperand(1);
16774       SDValue Src2 = Op.getOperand(2);
16775       SDValue CC = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op.getOperand(3));
16776       SDValue Mask = Op.getOperand(4);
16777
16778       SDValue Cmp;
16779       if (IntrData->Opc1 != 0) {
16780         SDValue Rnd = Op.getOperand(5);
16781         if (cast<ConstantSDNode>(Rnd)->getZExtValue() !=
16782             X86::STATIC_ROUNDING::CUR_DIRECTION)
16783           Cmp = DAG.getNode(IntrData->Opc1, dl, MVT::i1, Src1, Src2, CC, Rnd);
16784       }
16785       //default rounding mode
16786       if(!Cmp.getNode())
16787         Cmp = DAG.getNode(IntrData->Opc0, dl, MVT::i1, Src1, Src2, CC);
16788
16789       SDValue CmpMask = getScalarMaskingNode(Cmp, Mask,
16790                                              DAG.getTargetConstant(0, dl,
16791                                                                    MVT::i1),
16792                                              Subtarget, DAG);
16793
16794       return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::i8,
16795                          DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i8, CmpMask),
16796                          DAG.getValueType(MVT::i1));
16797     }
16798     case COMI: { // Comparison intrinsics
16799       ISD::CondCode CC = (ISD::CondCode)IntrData->Opc1;
16800       SDValue LHS = Op.getOperand(1);
16801       SDValue RHS = Op.getOperand(2);
16802       unsigned X86CC = TranslateX86CC(CC, dl, true, LHS, RHS, DAG);
16803       assert(X86CC != X86::COND_INVALID && "Unexpected illegal condition!");
16804       SDValue Cond = DAG.getNode(IntrData->Opc0, dl, MVT::i32, LHS, RHS);
16805       SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
16806                                   DAG.getConstant(X86CC, dl, MVT::i8), Cond);
16807       return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
16808     }
16809     case COMI_RM: { // Comparison intrinsics with Sae
16810       SDValue LHS = Op.getOperand(1);
16811       SDValue RHS = Op.getOperand(2);
16812       SDValue CC = Op.getOperand(3);
16813       SDValue Sae = Op.getOperand(4);
16814       auto ComiType = TranslateX86ConstCondToX86CC(CC);
16815       // choose between ordered and unordered (comi/ucomi)
16816       unsigned comiOp = std::get<0>(ComiType) ? IntrData->Opc0 : IntrData->Opc1;
16817       SDValue Cond;
16818       if (cast<ConstantSDNode>(Sae)->getZExtValue() !=
16819                                            X86::STATIC_ROUNDING::CUR_DIRECTION)
16820         Cond = DAG.getNode(comiOp, dl, MVT::i32, LHS, RHS, Sae);
16821       else
16822         Cond = DAG.getNode(comiOp, dl, MVT::i32, LHS, RHS);
16823       SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
16824         DAG.getConstant(std::get<1>(ComiType), dl, MVT::i8), Cond);
16825       return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
16826     }
16827     case VSHIFT:
16828       return getTargetVShiftNode(IntrData->Opc0, dl, Op.getSimpleValueType(),
16829                                  Op.getOperand(1), Op.getOperand(2), DAG);
16830     case VSHIFT_MASK:
16831       return getVectorMaskingNode(getTargetVShiftNode(IntrData->Opc0, dl,
16832                                                       Op.getSimpleValueType(),
16833                                                       Op.getOperand(1),
16834                                                       Op.getOperand(2), DAG),
16835                                   Op.getOperand(4), Op.getOperand(3), Subtarget,
16836                                   DAG);
16837     case COMPRESS_EXPAND_IN_REG: {
16838       SDValue Mask = Op.getOperand(3);
16839       SDValue DataToCompress = Op.getOperand(1);
16840       SDValue PassThru = Op.getOperand(2);
16841       if (isAllOnesConstant(Mask)) // return data as is
16842         return Op.getOperand(1);
16843
16844       return getVectorMaskingNode(DAG.getNode(IntrData->Opc0, dl, VT,
16845                                               DataToCompress),
16846                                   Mask, PassThru, Subtarget, DAG);
16847     }
16848     case BROADCASTM: {
16849       SDValue Mask = Op.getOperand(1);
16850       MVT MaskVT = MVT::getVectorVT(MVT::i1,
16851                                     Mask.getSimpleValueType().getSizeInBits());
16852       Mask = DAG.getBitcast(MaskVT, Mask);
16853       return DAG.getNode(IntrData->Opc0, dl, Op.getValueType(), Mask);
16854     }
16855     case BLEND: {
16856       SDValue Mask = Op.getOperand(3);
16857       MVT VT = Op.getSimpleValueType();
16858       MVT MaskVT = MVT::getVectorVT(MVT::i1, VT.getVectorNumElements());
16859       SDValue VMask = getMaskNode(Mask, MaskVT, Subtarget, DAG, dl);
16860       return DAG.getNode(IntrData->Opc0, dl, VT, VMask, Op.getOperand(1),
16861                          Op.getOperand(2));
16862     }
16863     case KUNPCK: {
16864       MVT VT = Op.getSimpleValueType();
16865       MVT MaskVT = MVT::getVectorVT(MVT::i1, VT.getSizeInBits()/2);
16866
16867       SDValue Src1 = getMaskNode(Op.getOperand(1), MaskVT, Subtarget, DAG, dl);
16868       SDValue Src2 = getMaskNode(Op.getOperand(2), MaskVT, Subtarget, DAG, dl);
16869       // Arguments should be swapped.
16870       SDValue Res = DAG.getNode(IntrData->Opc0, dl,
16871                                 MVT::getVectorVT(MVT::i1, VT.getSizeInBits()),
16872                                 Src2, Src1);
16873       return DAG.getBitcast(VT, Res);
16874     }
16875     case CONVERT_TO_MASK: {
16876       MVT SrcVT = Op.getOperand(1).getSimpleValueType();
16877       MVT MaskVT = MVT::getVectorVT(MVT::i1, SrcVT.getVectorNumElements());
16878       MVT BitcastVT = MVT::getVectorVT(MVT::i1, VT.getSizeInBits());
16879
16880       SDValue CvtMask = DAG.getNode(IntrData->Opc0, dl, MaskVT,
16881                                     Op.getOperand(1));
16882       SDValue Res = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, BitcastVT,
16883                                 DAG.getUNDEF(BitcastVT), CvtMask,
16884                                 DAG.getIntPtrConstant(0, dl));
16885       return DAG.getBitcast(Op.getValueType(), Res);
16886     }
16887     case CONVERT_MASK_TO_VEC: {
16888       SDValue Mask = Op.getOperand(1);
16889       MVT MaskVT = MVT::getVectorVT(MVT::i1, VT.getVectorNumElements());
16890       SDValue VMask = getMaskNode(Mask, MaskVT, Subtarget, DAG, dl);
16891       return DAG.getNode(IntrData->Opc0, dl, VT, VMask);
16892     }
16893     case BRCST_SUBVEC_TO_VEC: {
16894       SDValue Src = Op.getOperand(1);
16895       SDValue Passthru = Op.getOperand(2);
16896       SDValue Mask = Op.getOperand(3);
16897       EVT resVT = Passthru.getValueType();
16898       SDValue subVec = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, resVT,
16899                                        DAG.getUNDEF(resVT), Src,
16900                                        DAG.getIntPtrConstant(0, dl));
16901       SDValue immVal;
16902       if (Src.getSimpleValueType().is256BitVector() && resVT.is512BitVector())
16903         immVal = DAG.getConstant(0x44, dl, MVT::i8);
16904       else
16905         immVal = DAG.getConstant(0, dl, MVT::i8);
16906       return getVectorMaskingNode(DAG.getNode(IntrData->Opc0, dl, VT,
16907                                               subVec, subVec, immVal),
16908                                   Mask, Passthru, Subtarget, DAG);
16909     }
16910     default:
16911       break;
16912     }
16913   }
16914
16915   switch (IntNo) {
16916   default: return SDValue();    // Don't custom lower most intrinsics.
16917
16918   case Intrinsic::x86_avx2_permd:
16919   case Intrinsic::x86_avx2_permps:
16920     // Operands intentionally swapped. Mask is last operand to intrinsic,
16921     // but second operand for node/instruction.
16922     return DAG.getNode(X86ISD::VPERMV, dl, Op.getValueType(),
16923                        Op.getOperand(2), Op.getOperand(1));
16924
16925   // ptest and testp intrinsics. The intrinsic these come from are designed to
16926   // return an integer value, not just an instruction so lower it to the ptest
16927   // or testp pattern and a setcc for the result.
16928   case Intrinsic::x86_sse41_ptestz:
16929   case Intrinsic::x86_sse41_ptestc:
16930   case Intrinsic::x86_sse41_ptestnzc:
16931   case Intrinsic::x86_avx_ptestz_256:
16932   case Intrinsic::x86_avx_ptestc_256:
16933   case Intrinsic::x86_avx_ptestnzc_256:
16934   case Intrinsic::x86_avx_vtestz_ps:
16935   case Intrinsic::x86_avx_vtestc_ps:
16936   case Intrinsic::x86_avx_vtestnzc_ps:
16937   case Intrinsic::x86_avx_vtestz_pd:
16938   case Intrinsic::x86_avx_vtestc_pd:
16939   case Intrinsic::x86_avx_vtestnzc_pd:
16940   case Intrinsic::x86_avx_vtestz_ps_256:
16941   case Intrinsic::x86_avx_vtestc_ps_256:
16942   case Intrinsic::x86_avx_vtestnzc_ps_256:
16943   case Intrinsic::x86_avx_vtestz_pd_256:
16944   case Intrinsic::x86_avx_vtestc_pd_256:
16945   case Intrinsic::x86_avx_vtestnzc_pd_256: {
16946     bool IsTestPacked = false;
16947     unsigned X86CC;
16948     switch (IntNo) {
16949     default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
16950     case Intrinsic::x86_avx_vtestz_ps:
16951     case Intrinsic::x86_avx_vtestz_pd:
16952     case Intrinsic::x86_avx_vtestz_ps_256:
16953     case Intrinsic::x86_avx_vtestz_pd_256:
16954       IsTestPacked = true; // Fallthrough
16955     case Intrinsic::x86_sse41_ptestz:
16956     case Intrinsic::x86_avx_ptestz_256:
16957       // ZF = 1
16958       X86CC = X86::COND_E;
16959       break;
16960     case Intrinsic::x86_avx_vtestc_ps:
16961     case Intrinsic::x86_avx_vtestc_pd:
16962     case Intrinsic::x86_avx_vtestc_ps_256:
16963     case Intrinsic::x86_avx_vtestc_pd_256:
16964       IsTestPacked = true; // Fallthrough
16965     case Intrinsic::x86_sse41_ptestc:
16966     case Intrinsic::x86_avx_ptestc_256:
16967       // CF = 1
16968       X86CC = X86::COND_B;
16969       break;
16970     case Intrinsic::x86_avx_vtestnzc_ps:
16971     case Intrinsic::x86_avx_vtestnzc_pd:
16972     case Intrinsic::x86_avx_vtestnzc_ps_256:
16973     case Intrinsic::x86_avx_vtestnzc_pd_256:
16974       IsTestPacked = true; // Fallthrough
16975     case Intrinsic::x86_sse41_ptestnzc:
16976     case Intrinsic::x86_avx_ptestnzc_256:
16977       // ZF and CF = 0
16978       X86CC = X86::COND_A;
16979       break;
16980     }
16981
16982     SDValue LHS = Op.getOperand(1);
16983     SDValue RHS = Op.getOperand(2);
16984     unsigned TestOpc = IsTestPacked ? X86ISD::TESTP : X86ISD::PTEST;
16985     SDValue Test = DAG.getNode(TestOpc, dl, MVT::i32, LHS, RHS);
16986     SDValue CC = DAG.getConstant(X86CC, dl, MVT::i8);
16987     SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
16988     return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
16989   }
16990   case Intrinsic::x86_avx512_kortestz_w:
16991   case Intrinsic::x86_avx512_kortestc_w: {
16992     unsigned X86CC = (IntNo == Intrinsic::x86_avx512_kortestz_w)? X86::COND_E: X86::COND_B;
16993     SDValue LHS = DAG.getBitcast(MVT::v16i1, Op.getOperand(1));
16994     SDValue RHS = DAG.getBitcast(MVT::v16i1, Op.getOperand(2));
16995     SDValue CC = DAG.getConstant(X86CC, dl, MVT::i8);
16996     SDValue Test = DAG.getNode(X86ISD::KORTEST, dl, MVT::i32, LHS, RHS);
16997     SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i1, CC, Test);
16998     return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
16999   }
17000
17001   case Intrinsic::x86_sse42_pcmpistria128:
17002   case Intrinsic::x86_sse42_pcmpestria128:
17003   case Intrinsic::x86_sse42_pcmpistric128:
17004   case Intrinsic::x86_sse42_pcmpestric128:
17005   case Intrinsic::x86_sse42_pcmpistrio128:
17006   case Intrinsic::x86_sse42_pcmpestrio128:
17007   case Intrinsic::x86_sse42_pcmpistris128:
17008   case Intrinsic::x86_sse42_pcmpestris128:
17009   case Intrinsic::x86_sse42_pcmpistriz128:
17010   case Intrinsic::x86_sse42_pcmpestriz128: {
17011     unsigned Opcode;
17012     unsigned X86CC;
17013     switch (IntNo) {
17014     default: llvm_unreachable("Impossible intrinsic");  // Can't reach here.
17015     case Intrinsic::x86_sse42_pcmpistria128:
17016       Opcode = X86ISD::PCMPISTRI;
17017       X86CC = X86::COND_A;
17018       break;
17019     case Intrinsic::x86_sse42_pcmpestria128:
17020       Opcode = X86ISD::PCMPESTRI;
17021       X86CC = X86::COND_A;
17022       break;
17023     case Intrinsic::x86_sse42_pcmpistric128:
17024       Opcode = X86ISD::PCMPISTRI;
17025       X86CC = X86::COND_B;
17026       break;
17027     case Intrinsic::x86_sse42_pcmpestric128:
17028       Opcode = X86ISD::PCMPESTRI;
17029       X86CC = X86::COND_B;
17030       break;
17031     case Intrinsic::x86_sse42_pcmpistrio128:
17032       Opcode = X86ISD::PCMPISTRI;
17033       X86CC = X86::COND_O;
17034       break;
17035     case Intrinsic::x86_sse42_pcmpestrio128:
17036       Opcode = X86ISD::PCMPESTRI;
17037       X86CC = X86::COND_O;
17038       break;
17039     case Intrinsic::x86_sse42_pcmpistris128:
17040       Opcode = X86ISD::PCMPISTRI;
17041       X86CC = X86::COND_S;
17042       break;
17043     case Intrinsic::x86_sse42_pcmpestris128:
17044       Opcode = X86ISD::PCMPESTRI;
17045       X86CC = X86::COND_S;
17046       break;
17047     case Intrinsic::x86_sse42_pcmpistriz128:
17048       Opcode = X86ISD::PCMPISTRI;
17049       X86CC = X86::COND_E;
17050       break;
17051     case Intrinsic::x86_sse42_pcmpestriz128:
17052       Opcode = X86ISD::PCMPESTRI;
17053       X86CC = X86::COND_E;
17054       break;
17055     }
17056     SmallVector<SDValue, 5> NewOps(Op->op_begin()+1, Op->op_end());
17057     SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
17058     SDValue PCMP = DAG.getNode(Opcode, dl, VTs, NewOps);
17059     SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
17060                                 DAG.getConstant(X86CC, dl, MVT::i8),
17061                                 SDValue(PCMP.getNode(), 1));
17062     return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
17063   }
17064
17065   case Intrinsic::x86_sse42_pcmpistri128:
17066   case Intrinsic::x86_sse42_pcmpestri128: {
17067     unsigned Opcode;
17068     if (IntNo == Intrinsic::x86_sse42_pcmpistri128)
17069       Opcode = X86ISD::PCMPISTRI;
17070     else
17071       Opcode = X86ISD::PCMPESTRI;
17072
17073     SmallVector<SDValue, 5> NewOps(Op->op_begin()+1, Op->op_end());
17074     SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
17075     return DAG.getNode(Opcode, dl, VTs, NewOps);
17076   }
17077
17078   case Intrinsic::x86_seh_lsda: {
17079     // Compute the symbol for the LSDA. We know it'll get emitted later.
17080     MachineFunction &MF = DAG.getMachineFunction();
17081     SDValue Op1 = Op.getOperand(1);
17082     auto *Fn = cast<Function>(cast<GlobalAddressSDNode>(Op1)->getGlobal());
17083     MCSymbol *LSDASym = MF.getMMI().getContext().getOrCreateLSDASymbol(
17084         GlobalValue::getRealLinkageName(Fn->getName()));
17085
17086     // Generate a simple absolute symbol reference. This intrinsic is only
17087     // supported on 32-bit Windows, which isn't PIC.
17088     SDValue Result = DAG.getMCSymbol(LSDASym, VT);
17089     return DAG.getNode(X86ISD::Wrapper, dl, VT, Result);
17090   }
17091
17092   case Intrinsic::x86_seh_recoverfp: {
17093     SDValue FnOp = Op.getOperand(1);
17094     SDValue IncomingFPOp = Op.getOperand(2);
17095     GlobalAddressSDNode *GSD = dyn_cast<GlobalAddressSDNode>(FnOp);
17096     auto *Fn = dyn_cast_or_null<Function>(GSD ? GSD->getGlobal() : nullptr);
17097     if (!Fn)
17098       report_fatal_error(
17099           "llvm.x86.seh.recoverfp must take a function as the first argument");
17100     return recoverFramePointer(DAG, Fn, IncomingFPOp);
17101   }
17102
17103   case Intrinsic::localaddress: {
17104     // Returns one of the stack, base, or frame pointer registers, depending on
17105     // which is used to reference local variables.
17106     MachineFunction &MF = DAG.getMachineFunction();
17107     const X86RegisterInfo *RegInfo = Subtarget->getRegisterInfo();
17108     unsigned Reg;
17109     if (RegInfo->hasBasePointer(MF))
17110       Reg = RegInfo->getBaseRegister();
17111     else // This function handles the SP or FP case.
17112       Reg = RegInfo->getPtrSizedFrameRegister(MF);
17113     return DAG.getCopyFromReg(DAG.getEntryNode(), dl, Reg, VT);
17114   }
17115   }
17116 }
17117
17118 static SDValue getGatherNode(unsigned Opc, SDValue Op, SelectionDAG &DAG,
17119                               SDValue Src, SDValue Mask, SDValue Base,
17120                               SDValue Index, SDValue ScaleOp, SDValue Chain,
17121                               const X86Subtarget * Subtarget) {
17122   SDLoc dl(Op);
17123   auto *C = cast<ConstantSDNode>(ScaleOp);
17124   SDValue Scale = DAG.getTargetConstant(C->getZExtValue(), dl, MVT::i8);
17125   MVT MaskVT = MVT::getVectorVT(MVT::i1,
17126                              Index.getSimpleValueType().getVectorNumElements());
17127   SDValue MaskInReg;
17128   ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(Mask);
17129   if (MaskC)
17130     MaskInReg = DAG.getTargetConstant(MaskC->getSExtValue(), dl, MaskVT);
17131   else {
17132     MVT BitcastVT = MVT::getVectorVT(MVT::i1,
17133                                      Mask.getSimpleValueType().getSizeInBits());
17134
17135     // In case when MaskVT equals v2i1 or v4i1, low 2 or 4 elements
17136     // are extracted by EXTRACT_SUBVECTOR.
17137     MaskInReg = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MaskVT,
17138                             DAG.getBitcast(BitcastVT, Mask),
17139                             DAG.getIntPtrConstant(0, dl));
17140   }
17141   SDVTList VTs = DAG.getVTList(Op.getValueType(), MaskVT, MVT::Other);
17142   SDValue Disp = DAG.getTargetConstant(0, dl, MVT::i32);
17143   SDValue Segment = DAG.getRegister(0, MVT::i32);
17144   if (Src.getOpcode() == ISD::UNDEF)
17145     Src = getZeroVector(Op.getSimpleValueType(), Subtarget, DAG, dl);
17146   SDValue Ops[] = {Src, MaskInReg, Base, Scale, Index, Disp, Segment, Chain};
17147   SDNode *Res = DAG.getMachineNode(Opc, dl, VTs, Ops);
17148   SDValue RetOps[] = { SDValue(Res, 0), SDValue(Res, 2) };
17149   return DAG.getMergeValues(RetOps, dl);
17150 }
17151
17152 static SDValue getScatterNode(unsigned Opc, SDValue Op, SelectionDAG &DAG,
17153                                SDValue Src, SDValue Mask, SDValue Base,
17154                                SDValue Index, SDValue ScaleOp, SDValue Chain) {
17155   SDLoc dl(Op);
17156   auto *C = cast<ConstantSDNode>(ScaleOp);
17157   SDValue Scale = DAG.getTargetConstant(C->getZExtValue(), dl, MVT::i8);
17158   SDValue Disp = DAG.getTargetConstant(0, dl, MVT::i32);
17159   SDValue Segment = DAG.getRegister(0, MVT::i32);
17160   MVT MaskVT = MVT::getVectorVT(MVT::i1,
17161                              Index.getSimpleValueType().getVectorNumElements());
17162   SDValue MaskInReg;
17163   ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(Mask);
17164   if (MaskC)
17165     MaskInReg = DAG.getTargetConstant(MaskC->getSExtValue(), dl, MaskVT);
17166   else {
17167     MVT BitcastVT = MVT::getVectorVT(MVT::i1,
17168                                      Mask.getSimpleValueType().getSizeInBits());
17169
17170     // In case when MaskVT equals v2i1 or v4i1, low 2 or 4 elements
17171     // are extracted by EXTRACT_SUBVECTOR.
17172     MaskInReg = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MaskVT,
17173                             DAG.getBitcast(BitcastVT, Mask),
17174                             DAG.getIntPtrConstant(0, dl));
17175   }
17176   SDVTList VTs = DAG.getVTList(MaskVT, MVT::Other);
17177   SDValue Ops[] = {Base, Scale, Index, Disp, Segment, MaskInReg, Src, Chain};
17178   SDNode *Res = DAG.getMachineNode(Opc, dl, VTs, Ops);
17179   return SDValue(Res, 1);
17180 }
17181
17182 static SDValue getPrefetchNode(unsigned Opc, SDValue Op, SelectionDAG &DAG,
17183                                SDValue Mask, SDValue Base, SDValue Index,
17184                                SDValue ScaleOp, SDValue Chain) {
17185   SDLoc dl(Op);
17186   auto *C = cast<ConstantSDNode>(ScaleOp);
17187   SDValue Scale = DAG.getTargetConstant(C->getZExtValue(), dl, MVT::i8);
17188   SDValue Disp = DAG.getTargetConstant(0, dl, MVT::i32);
17189   SDValue Segment = DAG.getRegister(0, MVT::i32);
17190   MVT MaskVT =
17191     MVT::getVectorVT(MVT::i1, Index.getSimpleValueType().getVectorNumElements());
17192   SDValue MaskInReg;
17193   ConstantSDNode *MaskC = dyn_cast<ConstantSDNode>(Mask);
17194   if (MaskC)
17195     MaskInReg = DAG.getTargetConstant(MaskC->getSExtValue(), dl, MaskVT);
17196   else
17197     MaskInReg = DAG.getBitcast(MaskVT, Mask);
17198   //SDVTList VTs = DAG.getVTList(MVT::Other);
17199   SDValue Ops[] = {MaskInReg, Base, Scale, Index, Disp, Segment, Chain};
17200   SDNode *Res = DAG.getMachineNode(Opc, dl, MVT::Other, Ops);
17201   return SDValue(Res, 0);
17202 }
17203
17204 // getReadPerformanceCounter - Handles the lowering of builtin intrinsics that
17205 // read performance monitor counters (x86_rdpmc).
17206 static void getReadPerformanceCounter(SDNode *N, SDLoc DL,
17207                               SelectionDAG &DAG, const X86Subtarget *Subtarget,
17208                               SmallVectorImpl<SDValue> &Results) {
17209   assert(N->getNumOperands() == 3 && "Unexpected number of operands!");
17210   SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
17211   SDValue LO, HI;
17212
17213   // The ECX register is used to select the index of the performance counter
17214   // to read.
17215   SDValue Chain = DAG.getCopyToReg(N->getOperand(0), DL, X86::ECX,
17216                                    N->getOperand(2));
17217   SDValue rd = DAG.getNode(X86ISD::RDPMC_DAG, DL, Tys, Chain);
17218
17219   // Reads the content of a 64-bit performance counter and returns it in the
17220   // registers EDX:EAX.
17221   if (Subtarget->is64Bit()) {
17222     LO = DAG.getCopyFromReg(rd, DL, X86::RAX, MVT::i64, rd.getValue(1));
17223     HI = DAG.getCopyFromReg(LO.getValue(1), DL, X86::RDX, MVT::i64,
17224                             LO.getValue(2));
17225   } else {
17226     LO = DAG.getCopyFromReg(rd, DL, X86::EAX, MVT::i32, rd.getValue(1));
17227     HI = DAG.getCopyFromReg(LO.getValue(1), DL, X86::EDX, MVT::i32,
17228                             LO.getValue(2));
17229   }
17230   Chain = HI.getValue(1);
17231
17232   if (Subtarget->is64Bit()) {
17233     // The EAX register is loaded with the low-order 32 bits. The EDX register
17234     // is loaded with the supported high-order bits of the counter.
17235     SDValue Tmp = DAG.getNode(ISD::SHL, DL, MVT::i64, HI,
17236                               DAG.getConstant(32, DL, MVT::i8));
17237     Results.push_back(DAG.getNode(ISD::OR, DL, MVT::i64, LO, Tmp));
17238     Results.push_back(Chain);
17239     return;
17240   }
17241
17242   // Use a buildpair to merge the two 32-bit values into a 64-bit one.
17243   SDValue Ops[] = { LO, HI };
17244   SDValue Pair = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops);
17245   Results.push_back(Pair);
17246   Results.push_back(Chain);
17247 }
17248
17249 // getReadTimeStampCounter - Handles the lowering of builtin intrinsics that
17250 // read the time stamp counter (x86_rdtsc and x86_rdtscp). This function is
17251 // also used to custom lower READCYCLECOUNTER nodes.
17252 static void getReadTimeStampCounter(SDNode *N, SDLoc DL, unsigned Opcode,
17253                               SelectionDAG &DAG, const X86Subtarget *Subtarget,
17254                               SmallVectorImpl<SDValue> &Results) {
17255   SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
17256   SDValue rd = DAG.getNode(Opcode, DL, Tys, N->getOperand(0));
17257   SDValue LO, HI;
17258
17259   // The processor's time-stamp counter (a 64-bit MSR) is stored into the
17260   // EDX:EAX registers. EDX is loaded with the high-order 32 bits of the MSR
17261   // and the EAX register is loaded with the low-order 32 bits.
17262   if (Subtarget->is64Bit()) {
17263     LO = DAG.getCopyFromReg(rd, DL, X86::RAX, MVT::i64, rd.getValue(1));
17264     HI = DAG.getCopyFromReg(LO.getValue(1), DL, X86::RDX, MVT::i64,
17265                             LO.getValue(2));
17266   } else {
17267     LO = DAG.getCopyFromReg(rd, DL, X86::EAX, MVT::i32, rd.getValue(1));
17268     HI = DAG.getCopyFromReg(LO.getValue(1), DL, X86::EDX, MVT::i32,
17269                             LO.getValue(2));
17270   }
17271   SDValue Chain = HI.getValue(1);
17272
17273   if (Opcode == X86ISD::RDTSCP_DAG) {
17274     assert(N->getNumOperands() == 3 && "Unexpected number of operands!");
17275
17276     // Instruction RDTSCP loads the IA32:TSC_AUX_MSR (address C000_0103H) into
17277     // the ECX register. Add 'ecx' explicitly to the chain.
17278     SDValue ecx = DAG.getCopyFromReg(Chain, DL, X86::ECX, MVT::i32,
17279                                      HI.getValue(2));
17280     // Explicitly store the content of ECX at the location passed in input
17281     // to the 'rdtscp' intrinsic.
17282     Chain = DAG.getStore(ecx.getValue(1), DL, ecx, N->getOperand(2),
17283                          MachinePointerInfo(), false, false, 0);
17284   }
17285
17286   if (Subtarget->is64Bit()) {
17287     // The EDX register is loaded with the high-order 32 bits of the MSR, and
17288     // the EAX register is loaded with the low-order 32 bits.
17289     SDValue Tmp = DAG.getNode(ISD::SHL, DL, MVT::i64, HI,
17290                               DAG.getConstant(32, DL, MVT::i8));
17291     Results.push_back(DAG.getNode(ISD::OR, DL, MVT::i64, LO, Tmp));
17292     Results.push_back(Chain);
17293     return;
17294   }
17295
17296   // Use a buildpair to merge the two 32-bit values into a 64-bit one.
17297   SDValue Ops[] = { LO, HI };
17298   SDValue Pair = DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i64, Ops);
17299   Results.push_back(Pair);
17300   Results.push_back(Chain);
17301 }
17302
17303 static SDValue LowerREADCYCLECOUNTER(SDValue Op, const X86Subtarget *Subtarget,
17304                                      SelectionDAG &DAG) {
17305   SmallVector<SDValue, 2> Results;
17306   SDLoc DL(Op);
17307   getReadTimeStampCounter(Op.getNode(), DL, X86ISD::RDTSC_DAG, DAG, Subtarget,
17308                           Results);
17309   return DAG.getMergeValues(Results, DL);
17310 }
17311
17312 static SDValue MarkEHRegistrationNode(SDValue Op, SelectionDAG &DAG) {
17313   MachineFunction &MF = DAG.getMachineFunction();
17314   SDValue Chain = Op.getOperand(0);
17315   SDValue RegNode = Op.getOperand(2);
17316   WinEHFuncInfo *EHInfo = MF.getWinEHFuncInfo();
17317   if (!EHInfo)
17318     report_fatal_error("EH registrations only live in functions using WinEH");
17319
17320   // Cast the operand to an alloca, and remember the frame index.
17321   auto *FINode = dyn_cast<FrameIndexSDNode>(RegNode);
17322   if (!FINode)
17323     report_fatal_error("llvm.x86.seh.ehregnode expects a static alloca");
17324   EHInfo->EHRegNodeFrameIndex = FINode->getIndex();
17325
17326   // Return the chain operand without making any DAG nodes.
17327   return Chain;
17328 }
17329
17330 /// \brief Lower intrinsics for TRUNCATE_TO_MEM case
17331 /// return truncate Store/MaskedStore Node
17332 static SDValue LowerINTRINSIC_TRUNCATE_TO_MEM(const SDValue & Op,
17333                                                SelectionDAG &DAG,
17334                                                MVT ElementType) {
17335   SDLoc dl(Op);
17336   SDValue Mask = Op.getOperand(4);
17337   SDValue DataToTruncate = Op.getOperand(3);
17338   SDValue Addr = Op.getOperand(2);
17339   SDValue Chain = Op.getOperand(0);
17340
17341   MVT VT  = DataToTruncate.getSimpleValueType();
17342   MVT SVT = MVT::getVectorVT(ElementType, VT.getVectorNumElements());
17343
17344   if (isAllOnesConstant(Mask)) // return just a truncate store
17345     return DAG.getTruncStore(Chain, dl, DataToTruncate, Addr,
17346                              MachinePointerInfo(), SVT, false, false,
17347                              SVT.getScalarSizeInBits()/8);
17348
17349   MVT MaskVT = MVT::getVectorVT(MVT::i1, VT.getVectorNumElements());
17350   MVT BitcastVT = MVT::getVectorVT(MVT::i1,
17351                                    Mask.getSimpleValueType().getSizeInBits());
17352   // In case when MaskVT equals v2i1 or v4i1, low 2 or 4 elements
17353   // are extracted by EXTRACT_SUBVECTOR.
17354   SDValue VMask = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, MaskVT,
17355                               DAG.getBitcast(BitcastVT, Mask),
17356                               DAG.getIntPtrConstant(0, dl));
17357
17358   MachineMemOperand *MMO = DAG.getMachineFunction().
17359     getMachineMemOperand(MachinePointerInfo(),
17360                          MachineMemOperand::MOStore, SVT.getStoreSize(),
17361                          SVT.getScalarSizeInBits()/8);
17362
17363   return DAG.getMaskedStore(Chain, dl, DataToTruncate, Addr,
17364                             VMask, SVT, MMO, true);
17365 }
17366
17367 static SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, const X86Subtarget *Subtarget,
17368                                       SelectionDAG &DAG) {
17369   unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
17370
17371   const IntrinsicData* IntrData = getIntrinsicWithChain(IntNo);
17372   if (!IntrData) {
17373     if (IntNo == llvm::Intrinsic::x86_seh_ehregnode)
17374       return MarkEHRegistrationNode(Op, DAG);
17375     return SDValue();
17376   }
17377
17378   SDLoc dl(Op);
17379   switch(IntrData->Type) {
17380   default: llvm_unreachable("Unknown Intrinsic Type");
17381   case RDSEED:
17382   case RDRAND: {
17383     // Emit the node with the right value type.
17384     SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Glue, MVT::Other);
17385     SDValue Result = DAG.getNode(IntrData->Opc0, dl, VTs, Op.getOperand(0));
17386
17387     // If the value returned by RDRAND/RDSEED was valid (CF=1), return 1.
17388     // Otherwise return the value from Rand, which is always 0, casted to i32.
17389     SDValue Ops[] = { DAG.getZExtOrTrunc(Result, dl, Op->getValueType(1)),
17390                       DAG.getConstant(1, dl, Op->getValueType(1)),
17391                       DAG.getConstant(X86::COND_B, dl, MVT::i32),
17392                       SDValue(Result.getNode(), 1) };
17393     SDValue isValid = DAG.getNode(X86ISD::CMOV, dl,
17394                                   DAG.getVTList(Op->getValueType(1), MVT::Glue),
17395                                   Ops);
17396
17397     // Return { result, isValid, chain }.
17398     return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(), Result, isValid,
17399                        SDValue(Result.getNode(), 2));
17400   }
17401   case GATHER: {
17402   //gather(v1, mask, index, base, scale);
17403     SDValue Chain = Op.getOperand(0);
17404     SDValue Src   = Op.getOperand(2);
17405     SDValue Base  = Op.getOperand(3);
17406     SDValue Index = Op.getOperand(4);
17407     SDValue Mask  = Op.getOperand(5);
17408     SDValue Scale = Op.getOperand(6);
17409     return getGatherNode(IntrData->Opc0, Op, DAG, Src, Mask, Base, Index, Scale,
17410                          Chain, Subtarget);
17411   }
17412   case SCATTER: {
17413   //scatter(base, mask, index, v1, scale);
17414     SDValue Chain = Op.getOperand(0);
17415     SDValue Base  = Op.getOperand(2);
17416     SDValue Mask  = Op.getOperand(3);
17417     SDValue Index = Op.getOperand(4);
17418     SDValue Src   = Op.getOperand(5);
17419     SDValue Scale = Op.getOperand(6);
17420     return getScatterNode(IntrData->Opc0, Op, DAG, Src, Mask, Base, Index,
17421                           Scale, Chain);
17422   }
17423   case PREFETCH: {
17424     SDValue Hint = Op.getOperand(6);
17425     unsigned HintVal = cast<ConstantSDNode>(Hint)->getZExtValue();
17426     assert(HintVal < 2 && "Wrong prefetch hint in intrinsic: should be 0 or 1");
17427     unsigned Opcode = (HintVal ? IntrData->Opc1 : IntrData->Opc0);
17428     SDValue Chain = Op.getOperand(0);
17429     SDValue Mask  = Op.getOperand(2);
17430     SDValue Index = Op.getOperand(3);
17431     SDValue Base  = Op.getOperand(4);
17432     SDValue Scale = Op.getOperand(5);
17433     return getPrefetchNode(Opcode, Op, DAG, Mask, Base, Index, Scale, Chain);
17434   }
17435   // Read Time Stamp Counter (RDTSC) and Processor ID (RDTSCP).
17436   case RDTSC: {
17437     SmallVector<SDValue, 2> Results;
17438     getReadTimeStampCounter(Op.getNode(), dl, IntrData->Opc0, DAG, Subtarget,
17439                             Results);
17440     return DAG.getMergeValues(Results, dl);
17441   }
17442   // Read Performance Monitoring Counters.
17443   case RDPMC: {
17444     SmallVector<SDValue, 2> Results;
17445     getReadPerformanceCounter(Op.getNode(), dl, DAG, Subtarget, Results);
17446     return DAG.getMergeValues(Results, dl);
17447   }
17448   // XTEST intrinsics.
17449   case XTEST: {
17450     SDVTList VTs = DAG.getVTList(Op->getValueType(0), MVT::Other);
17451     SDValue InTrans = DAG.getNode(IntrData->Opc0, dl, VTs, Op.getOperand(0));
17452     SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
17453                                 DAG.getConstant(X86::COND_NE, dl, MVT::i8),
17454                                 InTrans);
17455     SDValue Ret = DAG.getNode(ISD::ZERO_EXTEND, dl, Op->getValueType(0), SetCC);
17456     return DAG.getNode(ISD::MERGE_VALUES, dl, Op->getVTList(),
17457                        Ret, SDValue(InTrans.getNode(), 1));
17458   }
17459   // ADC/ADCX/SBB
17460   case ADX: {
17461     SmallVector<SDValue, 2> Results;
17462     SDVTList CFVTs = DAG.getVTList(Op->getValueType(0), MVT::Other);
17463     SDVTList VTs = DAG.getVTList(Op.getOperand(3)->getValueType(0), MVT::Other);
17464     SDValue GenCF = DAG.getNode(X86ISD::ADD, dl, CFVTs, Op.getOperand(2),
17465                                 DAG.getConstant(-1, dl, MVT::i8));
17466     SDValue Res = DAG.getNode(IntrData->Opc0, dl, VTs, Op.getOperand(3),
17467                               Op.getOperand(4), GenCF.getValue(1));
17468     SDValue Store = DAG.getStore(Op.getOperand(0), dl, Res.getValue(0),
17469                                  Op.getOperand(5), MachinePointerInfo(),
17470                                  false, false, 0);
17471     SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
17472                                 DAG.getConstant(X86::COND_B, dl, MVT::i8),
17473                                 Res.getValue(1));
17474     Results.push_back(SetCC);
17475     Results.push_back(Store);
17476     return DAG.getMergeValues(Results, dl);
17477   }
17478   case COMPRESS_TO_MEM: {
17479     SDLoc dl(Op);
17480     SDValue Mask = Op.getOperand(4);
17481     SDValue DataToCompress = Op.getOperand(3);
17482     SDValue Addr = Op.getOperand(2);
17483     SDValue Chain = Op.getOperand(0);
17484
17485     MVT VT = DataToCompress.getSimpleValueType();
17486     if (isAllOnesConstant(Mask)) // return just a store
17487       return DAG.getStore(Chain, dl, DataToCompress, Addr,
17488                           MachinePointerInfo(), false, false,
17489                           VT.getScalarSizeInBits()/8);
17490
17491     SDValue Compressed =
17492       getVectorMaskingNode(DAG.getNode(IntrData->Opc0, dl, VT, DataToCompress),
17493                            Mask, DAG.getUNDEF(VT), Subtarget, DAG);
17494     return DAG.getStore(Chain, dl, Compressed, Addr,
17495                         MachinePointerInfo(), false, false,
17496                         VT.getScalarSizeInBits()/8);
17497   }
17498   case TRUNCATE_TO_MEM_VI8:
17499     return LowerINTRINSIC_TRUNCATE_TO_MEM(Op, DAG, MVT::i8);
17500   case TRUNCATE_TO_MEM_VI16:
17501     return LowerINTRINSIC_TRUNCATE_TO_MEM(Op, DAG, MVT::i16);
17502   case TRUNCATE_TO_MEM_VI32:
17503     return LowerINTRINSIC_TRUNCATE_TO_MEM(Op, DAG, MVT::i32);
17504   case EXPAND_FROM_MEM: {
17505     SDLoc dl(Op);
17506     SDValue Mask = Op.getOperand(4);
17507     SDValue PassThru = Op.getOperand(3);
17508     SDValue Addr = Op.getOperand(2);
17509     SDValue Chain = Op.getOperand(0);
17510     MVT VT = Op.getSimpleValueType();
17511
17512     if (isAllOnesConstant(Mask)) // return just a load
17513       return DAG.getLoad(VT, dl, Chain, Addr, MachinePointerInfo(), false, false,
17514                          false, VT.getScalarSizeInBits()/8);
17515
17516     SDValue DataToExpand = DAG.getLoad(VT, dl, Chain, Addr, MachinePointerInfo(),
17517                                        false, false, false,
17518                                        VT.getScalarSizeInBits()/8);
17519
17520     SDValue Results[] = {
17521       getVectorMaskingNode(DAG.getNode(IntrData->Opc0, dl, VT, DataToExpand),
17522                            Mask, PassThru, Subtarget, DAG), Chain};
17523     return DAG.getMergeValues(Results, dl);
17524   }
17525   }
17526 }
17527
17528 SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op,
17529                                            SelectionDAG &DAG) const {
17530   MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
17531   MFI->setReturnAddressIsTaken(true);
17532
17533   if (verifyReturnAddressArgumentIsConstant(Op, DAG))
17534     return SDValue();
17535
17536   unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
17537   SDLoc dl(Op);
17538   EVT PtrVT = getPointerTy(DAG.getDataLayout());
17539
17540   if (Depth > 0) {
17541     SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
17542     const X86RegisterInfo *RegInfo = Subtarget->getRegisterInfo();
17543     SDValue Offset = DAG.getConstant(RegInfo->getSlotSize(), dl, PtrVT);
17544     return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
17545                        DAG.getNode(ISD::ADD, dl, PtrVT,
17546                                    FrameAddr, Offset),
17547                        MachinePointerInfo(), false, false, false, 0);
17548   }
17549
17550   // Just load the return address.
17551   SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
17552   return DAG.getLoad(PtrVT, dl, DAG.getEntryNode(),
17553                      RetAddrFI, MachinePointerInfo(), false, false, false, 0);
17554 }
17555
17556 SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const {
17557   MachineFunction &MF = DAG.getMachineFunction();
17558   MachineFrameInfo *MFI = MF.getFrameInfo();
17559   X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
17560   const X86RegisterInfo *RegInfo = Subtarget->getRegisterInfo();
17561   EVT VT = Op.getValueType();
17562
17563   MFI->setFrameAddressIsTaken(true);
17564
17565   if (MF.getTarget().getMCAsmInfo()->usesWindowsCFI()) {
17566     // Depth > 0 makes no sense on targets which use Windows unwind codes.  It
17567     // is not possible to crawl up the stack without looking at the unwind codes
17568     // simultaneously.
17569     int FrameAddrIndex = FuncInfo->getFAIndex();
17570     if (!FrameAddrIndex) {
17571       // Set up a frame object for the return address.
17572       unsigned SlotSize = RegInfo->getSlotSize();
17573       FrameAddrIndex = MF.getFrameInfo()->CreateFixedObject(
17574           SlotSize, /*Offset=*/0, /*IsImmutable=*/false);
17575       FuncInfo->setFAIndex(FrameAddrIndex);
17576     }
17577     return DAG.getFrameIndex(FrameAddrIndex, VT);
17578   }
17579
17580   unsigned FrameReg =
17581       RegInfo->getPtrSizedFrameRegister(DAG.getMachineFunction());
17582   SDLoc dl(Op);  // FIXME probably not meaningful
17583   unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
17584   assert(((FrameReg == X86::RBP && VT == MVT::i64) ||
17585           (FrameReg == X86::EBP && VT == MVT::i32)) &&
17586          "Invalid Frame Register!");
17587   SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
17588   while (Depth--)
17589     FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr,
17590                             MachinePointerInfo(),
17591                             false, false, false, 0);
17592   return FrameAddr;
17593 }
17594
17595 // FIXME? Maybe this could be a TableGen attribute on some registers and
17596 // this table could be generated automatically from RegInfo.
17597 unsigned X86TargetLowering::getRegisterByName(const char* RegName, EVT VT,
17598                                               SelectionDAG &DAG) const {
17599   const TargetFrameLowering &TFI = *Subtarget->getFrameLowering();
17600   const MachineFunction &MF = DAG.getMachineFunction();
17601
17602   unsigned Reg = StringSwitch<unsigned>(RegName)
17603                        .Case("esp", X86::ESP)
17604                        .Case("rsp", X86::RSP)
17605                        .Case("ebp", X86::EBP)
17606                        .Case("rbp", X86::RBP)
17607                        .Default(0);
17608
17609   if (Reg == X86::EBP || Reg == X86::RBP) {
17610     if (!TFI.hasFP(MF))
17611       report_fatal_error("register " + StringRef(RegName) +
17612                          " is allocatable: function has no frame pointer");
17613 #ifndef NDEBUG
17614     else {
17615       const X86RegisterInfo *RegInfo = Subtarget->getRegisterInfo();
17616       unsigned FrameReg =
17617           RegInfo->getPtrSizedFrameRegister(DAG.getMachineFunction());
17618       assert((FrameReg == X86::EBP || FrameReg == X86::RBP) &&
17619              "Invalid Frame Register!");
17620     }
17621 #endif
17622   }
17623
17624   if (Reg)
17625     return Reg;
17626
17627   report_fatal_error("Invalid register name global variable");
17628 }
17629
17630 SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
17631                                                      SelectionDAG &DAG) const {
17632   const X86RegisterInfo *RegInfo = Subtarget->getRegisterInfo();
17633   return DAG.getIntPtrConstant(2 * RegInfo->getSlotSize(), SDLoc(Op));
17634 }
17635
17636 unsigned X86TargetLowering::getExceptionPointerRegister(
17637     const Constant *PersonalityFn) const {
17638   if (classifyEHPersonality(PersonalityFn) == EHPersonality::CoreCLR)
17639     return Subtarget->isTarget64BitLP64() ? X86::RDX : X86::EDX;
17640
17641   return Subtarget->isTarget64BitLP64() ? X86::RAX : X86::EAX;
17642 }
17643
17644 unsigned X86TargetLowering::getExceptionSelectorRegister(
17645     const Constant *PersonalityFn) const {
17646   // Funclet personalities don't use selectors (the runtime does the selection).
17647   assert(!isFuncletEHPersonality(classifyEHPersonality(PersonalityFn)));
17648   return Subtarget->isTarget64BitLP64() ? X86::RDX : X86::EDX;
17649 }
17650
17651 SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const {
17652   SDValue Chain     = Op.getOperand(0);
17653   SDValue Offset    = Op.getOperand(1);
17654   SDValue Handler   = Op.getOperand(2);
17655   SDLoc dl      (Op);
17656
17657   EVT PtrVT = getPointerTy(DAG.getDataLayout());
17658   const X86RegisterInfo *RegInfo = Subtarget->getRegisterInfo();
17659   unsigned FrameReg = RegInfo->getFrameRegister(DAG.getMachineFunction());
17660   assert(((FrameReg == X86::RBP && PtrVT == MVT::i64) ||
17661           (FrameReg == X86::EBP && PtrVT == MVT::i32)) &&
17662          "Invalid Frame Register!");
17663   SDValue Frame = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, PtrVT);
17664   unsigned StoreAddrReg = (PtrVT == MVT::i64) ? X86::RCX : X86::ECX;
17665
17666   SDValue StoreAddr = DAG.getNode(ISD::ADD, dl, PtrVT, Frame,
17667                                  DAG.getIntPtrConstant(RegInfo->getSlotSize(),
17668                                                        dl));
17669   StoreAddr = DAG.getNode(ISD::ADD, dl, PtrVT, StoreAddr, Offset);
17670   Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, MachinePointerInfo(),
17671                        false, false, 0);
17672   Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
17673
17674   return DAG.getNode(X86ISD::EH_RETURN, dl, MVT::Other, Chain,
17675                      DAG.getRegister(StoreAddrReg, PtrVT));
17676 }
17677
17678 SDValue X86TargetLowering::lowerEH_SJLJ_SETJMP(SDValue Op,
17679                                                SelectionDAG &DAG) const {
17680   SDLoc DL(Op);
17681   return DAG.getNode(X86ISD::EH_SJLJ_SETJMP, DL,
17682                      DAG.getVTList(MVT::i32, MVT::Other),
17683                      Op.getOperand(0), Op.getOperand(1));
17684 }
17685
17686 SDValue X86TargetLowering::lowerEH_SJLJ_LONGJMP(SDValue Op,
17687                                                 SelectionDAG &DAG) const {
17688   SDLoc DL(Op);
17689   return DAG.getNode(X86ISD::EH_SJLJ_LONGJMP, DL, MVT::Other,
17690                      Op.getOperand(0), Op.getOperand(1));
17691 }
17692
17693 static SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) {
17694   return Op.getOperand(0);
17695 }
17696
17697 SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
17698                                                 SelectionDAG &DAG) const {
17699   SDValue Root = Op.getOperand(0);
17700   SDValue Trmp = Op.getOperand(1); // trampoline
17701   SDValue FPtr = Op.getOperand(2); // nested function
17702   SDValue Nest = Op.getOperand(3); // 'nest' parameter value
17703   SDLoc dl (Op);
17704
17705   const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
17706   const TargetRegisterInfo *TRI = Subtarget->getRegisterInfo();
17707
17708   if (Subtarget->is64Bit()) {
17709     SDValue OutChains[6];
17710
17711     // Large code-model.
17712     const unsigned char JMP64r  = 0xFF; // 64-bit jmp through register opcode.
17713     const unsigned char MOV64ri = 0xB8; // X86::MOV64ri opcode.
17714
17715     const unsigned char N86R10 = TRI->getEncodingValue(X86::R10) & 0x7;
17716     const unsigned char N86R11 = TRI->getEncodingValue(X86::R11) & 0x7;
17717
17718     const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
17719
17720     // Load the pointer to the nested function into R11.
17721     unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
17722     SDValue Addr = Trmp;
17723     OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, dl, MVT::i16),
17724                                 Addr, MachinePointerInfo(TrmpAddr),
17725                                 false, false, 0);
17726
17727     Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
17728                        DAG.getConstant(2, dl, MVT::i64));
17729     OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr,
17730                                 MachinePointerInfo(TrmpAddr, 2),
17731                                 false, false, 2);
17732
17733     // Load the 'nest' parameter value into R10.
17734     // R10 is specified in X86CallingConv.td
17735     OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
17736     Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
17737                        DAG.getConstant(10, dl, MVT::i64));
17738     OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, dl, MVT::i16),
17739                                 Addr, MachinePointerInfo(TrmpAddr, 10),
17740                                 false, false, 0);
17741
17742     Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
17743                        DAG.getConstant(12, dl, MVT::i64));
17744     OutChains[3] = DAG.getStore(Root, dl, Nest, Addr,
17745                                 MachinePointerInfo(TrmpAddr, 12),
17746                                 false, false, 2);
17747
17748     // Jump to the nested function.
17749     OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
17750     Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
17751                        DAG.getConstant(20, dl, MVT::i64));
17752     OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, dl, MVT::i16),
17753                                 Addr, MachinePointerInfo(TrmpAddr, 20),
17754                                 false, false, 0);
17755
17756     unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
17757     Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
17758                        DAG.getConstant(22, dl, MVT::i64));
17759     OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, dl, MVT::i8),
17760                                 Addr, MachinePointerInfo(TrmpAddr, 22),
17761                                 false, false, 0);
17762
17763     return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
17764   } else {
17765     const Function *Func =
17766       cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
17767     CallingConv::ID CC = Func->getCallingConv();
17768     unsigned NestReg;
17769
17770     switch (CC) {
17771     default:
17772       llvm_unreachable("Unsupported calling convention");
17773     case CallingConv::C:
17774     case CallingConv::X86_StdCall: {
17775       // Pass 'nest' parameter in ECX.
17776       // Must be kept in sync with X86CallingConv.td
17777       NestReg = X86::ECX;
17778
17779       // Check that ECX wasn't needed by an 'inreg' parameter.
17780       FunctionType *FTy = Func->getFunctionType();
17781       const AttributeSet &Attrs = Func->getAttributes();
17782
17783       if (!Attrs.isEmpty() && !Func->isVarArg()) {
17784         unsigned InRegCount = 0;
17785         unsigned Idx = 1;
17786
17787         for (FunctionType::param_iterator I = FTy->param_begin(),
17788              E = FTy->param_end(); I != E; ++I, ++Idx)
17789           if (Attrs.hasAttribute(Idx, Attribute::InReg)) {
17790             auto &DL = DAG.getDataLayout();
17791             // FIXME: should only count parameters that are lowered to integers.
17792             InRegCount += (DL.getTypeSizeInBits(*I) + 31) / 32;
17793           }
17794
17795         if (InRegCount > 2) {
17796           report_fatal_error("Nest register in use - reduce number of inreg"
17797                              " parameters!");
17798         }
17799       }
17800       break;
17801     }
17802     case CallingConv::X86_FastCall:
17803     case CallingConv::X86_ThisCall:
17804     case CallingConv::Fast:
17805       // Pass 'nest' parameter in EAX.
17806       // Must be kept in sync with X86CallingConv.td
17807       NestReg = X86::EAX;
17808       break;
17809     }
17810
17811     SDValue OutChains[4];
17812     SDValue Addr, Disp;
17813
17814     Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
17815                        DAG.getConstant(10, dl, MVT::i32));
17816     Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
17817
17818     // This is storing the opcode for MOV32ri.
17819     const unsigned char MOV32ri = 0xB8; // X86::MOV32ri's opcode byte.
17820     const unsigned char N86Reg = TRI->getEncodingValue(NestReg) & 0x7;
17821     OutChains[0] = DAG.getStore(Root, dl,
17822                                 DAG.getConstant(MOV32ri|N86Reg, dl, MVT::i8),
17823                                 Trmp, MachinePointerInfo(TrmpAddr),
17824                                 false, false, 0);
17825
17826     Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
17827                        DAG.getConstant(1, dl, MVT::i32));
17828     OutChains[1] = DAG.getStore(Root, dl, Nest, Addr,
17829                                 MachinePointerInfo(TrmpAddr, 1),
17830                                 false, false, 1);
17831
17832     const unsigned char JMP = 0xE9; // jmp <32bit dst> opcode.
17833     Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
17834                        DAG.getConstant(5, dl, MVT::i32));
17835     OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, dl, MVT::i8),
17836                                 Addr, MachinePointerInfo(TrmpAddr, 5),
17837                                 false, false, 1);
17838
17839     Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
17840                        DAG.getConstant(6, dl, MVT::i32));
17841     OutChains[3] = DAG.getStore(Root, dl, Disp, Addr,
17842                                 MachinePointerInfo(TrmpAddr, 6),
17843                                 false, false, 1);
17844
17845     return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains);
17846   }
17847 }
17848
17849 SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op,
17850                                             SelectionDAG &DAG) const {
17851   /*
17852    The rounding mode is in bits 11:10 of FPSR, and has the following
17853    settings:
17854      00 Round to nearest
17855      01 Round to -inf
17856      10 Round to +inf
17857      11 Round to 0
17858
17859   FLT_ROUNDS, on the other hand, expects the following:
17860     -1 Undefined
17861      0 Round to 0
17862      1 Round to nearest
17863      2 Round to +inf
17864      3 Round to -inf
17865
17866   To perform the conversion, we do:
17867     (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
17868   */
17869
17870   MachineFunction &MF = DAG.getMachineFunction();
17871   const TargetFrameLowering &TFI = *Subtarget->getFrameLowering();
17872   unsigned StackAlignment = TFI.getStackAlignment();
17873   MVT VT = Op.getSimpleValueType();
17874   SDLoc DL(Op);
17875
17876   // Save FP Control Word to stack slot
17877   int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment, false);
17878   SDValue StackSlot =
17879       DAG.getFrameIndex(SSFI, getPointerTy(DAG.getDataLayout()));
17880
17881   MachineMemOperand *MMO =
17882       MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(MF, SSFI),
17883                               MachineMemOperand::MOStore, 2, 2);
17884
17885   SDValue Ops[] = { DAG.getEntryNode(), StackSlot };
17886   SDValue Chain = DAG.getMemIntrinsicNode(X86ISD::FNSTCW16m, DL,
17887                                           DAG.getVTList(MVT::Other),
17888                                           Ops, MVT::i16, MMO);
17889
17890   // Load FP Control Word from stack slot
17891   SDValue CWD = DAG.getLoad(MVT::i16, DL, Chain, StackSlot,
17892                             MachinePointerInfo(), false, false, false, 0);
17893
17894   // Transform as necessary
17895   SDValue CWD1 =
17896     DAG.getNode(ISD::SRL, DL, MVT::i16,
17897                 DAG.getNode(ISD::AND, DL, MVT::i16,
17898                             CWD, DAG.getConstant(0x800, DL, MVT::i16)),
17899                 DAG.getConstant(11, DL, MVT::i8));
17900   SDValue CWD2 =
17901     DAG.getNode(ISD::SRL, DL, MVT::i16,
17902                 DAG.getNode(ISD::AND, DL, MVT::i16,
17903                             CWD, DAG.getConstant(0x400, DL, MVT::i16)),
17904                 DAG.getConstant(9, DL, MVT::i8));
17905
17906   SDValue RetVal =
17907     DAG.getNode(ISD::AND, DL, MVT::i16,
17908                 DAG.getNode(ISD::ADD, DL, MVT::i16,
17909                             DAG.getNode(ISD::OR, DL, MVT::i16, CWD1, CWD2),
17910                             DAG.getConstant(1, DL, MVT::i16)),
17911                 DAG.getConstant(3, DL, MVT::i16));
17912
17913   return DAG.getNode((VT.getSizeInBits() < 16 ?
17914                       ISD::TRUNCATE : ISD::ZERO_EXTEND), DL, VT, RetVal);
17915 }
17916
17917 /// \brief Lower a vector CTLZ using native supported vector CTLZ instruction.
17918 //
17919 // 1. i32/i64 128/256-bit vector (native support require VLX) are expended
17920 //    to 512-bit vector.
17921 // 2. i8/i16 vector implemented using dword LZCNT vector instruction
17922 //    ( sub(trunc(lzcnt(zext32(x)))) ). In case zext32(x) is illegal,
17923 //    split the vector, perform operation on it's Lo a Hi part and
17924 //    concatenate the results.
17925 static SDValue LowerVectorCTLZ_AVX512(SDValue Op, SelectionDAG &DAG) {
17926   SDLoc dl(Op);
17927   MVT VT = Op.getSimpleValueType();
17928   MVT EltVT = VT.getVectorElementType();
17929   unsigned NumElems = VT.getVectorNumElements();
17930
17931   if (EltVT == MVT::i64 || EltVT == MVT::i32) {
17932     // Extend to 512 bit vector.
17933     assert((VT.is256BitVector() || VT.is128BitVector()) &&
17934               "Unsupported value type for operation");
17935
17936     MVT NewVT = MVT::getVectorVT(EltVT, 512 / VT.getScalarSizeInBits());
17937     SDValue Vec512 = DAG.getNode(ISD::INSERT_SUBVECTOR, dl, NewVT,
17938                                  DAG.getUNDEF(NewVT),
17939                                  Op.getOperand(0),
17940                                  DAG.getIntPtrConstant(0, dl));
17941     SDValue CtlzNode = DAG.getNode(ISD::CTLZ, dl, NewVT, Vec512);
17942
17943     return DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT, CtlzNode,
17944                        DAG.getIntPtrConstant(0, dl));
17945   }
17946
17947   assert((EltVT == MVT::i8 || EltVT == MVT::i16) &&
17948           "Unsupported element type");
17949
17950   if (16 < NumElems) {
17951     // Split vector, it's Lo and Hi parts will be handled in next iteration.
17952     SDValue Lo, Hi;
17953     std::tie(Lo, Hi) = DAG.SplitVector(Op.getOperand(0), dl);
17954     MVT OutVT = MVT::getVectorVT(EltVT, NumElems/2);
17955
17956     Lo = DAG.getNode(Op.getOpcode(), dl, OutVT, Lo);
17957     Hi = DAG.getNode(Op.getOpcode(), dl, OutVT, Hi);
17958
17959     return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, Lo, Hi);
17960   }
17961
17962   MVT NewVT = MVT::getVectorVT(MVT::i32, NumElems);
17963
17964   assert((NewVT.is256BitVector() || NewVT.is512BitVector()) &&
17965           "Unsupported value type for operation");
17966
17967   // Use native supported vector instruction vplzcntd.
17968   Op = DAG.getNode(ISD::ZERO_EXTEND, dl, NewVT, Op.getOperand(0));
17969   SDValue CtlzNode = DAG.getNode(ISD::CTLZ, dl, NewVT, Op);
17970   SDValue TruncNode = DAG.getNode(ISD::TRUNCATE, dl, VT, CtlzNode);
17971   SDValue Delta = DAG.getConstant(32 - EltVT.getSizeInBits(), dl, VT);
17972
17973   return DAG.getNode(ISD::SUB, dl, VT, TruncNode, Delta);
17974 }
17975
17976 static SDValue LowerCTLZ(SDValue Op, const X86Subtarget *Subtarget,
17977                          SelectionDAG &DAG) {
17978   MVT VT = Op.getSimpleValueType();
17979   MVT OpVT = VT;
17980   unsigned NumBits = VT.getSizeInBits();
17981   SDLoc dl(Op);
17982
17983   if (VT.isVector() && Subtarget->hasAVX512())
17984     return LowerVectorCTLZ_AVX512(Op, DAG);
17985
17986   Op = Op.getOperand(0);
17987   if (VT == MVT::i8) {
17988     // Zero extend to i32 since there is not an i8 bsr.
17989     OpVT = MVT::i32;
17990     Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
17991   }
17992
17993   // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
17994   SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
17995   Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
17996
17997   // If src is zero (i.e. bsr sets ZF), returns NumBits.
17998   SDValue Ops[] = {
17999     Op,
18000     DAG.getConstant(NumBits + NumBits - 1, dl, OpVT),
18001     DAG.getConstant(X86::COND_E, dl, MVT::i8),
18002     Op.getValue(1)
18003   };
18004   Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, Ops);
18005
18006   // Finally xor with NumBits-1.
18007   Op = DAG.getNode(ISD::XOR, dl, OpVT, Op,
18008                    DAG.getConstant(NumBits - 1, dl, OpVT));
18009
18010   if (VT == MVT::i8)
18011     Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
18012   return Op;
18013 }
18014
18015 static SDValue LowerCTLZ_ZERO_UNDEF(SDValue Op, const X86Subtarget *Subtarget,
18016                                     SelectionDAG &DAG) {
18017   MVT VT = Op.getSimpleValueType();
18018   EVT OpVT = VT;
18019   unsigned NumBits = VT.getSizeInBits();
18020   SDLoc dl(Op);
18021
18022   Op = Op.getOperand(0);
18023   if (VT == MVT::i8) {
18024     // Zero extend to i32 since there is not an i8 bsr.
18025     OpVT = MVT::i32;
18026     Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
18027   }
18028
18029   // Issue a bsr (scan bits in reverse).
18030   SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
18031   Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
18032
18033   // And xor with NumBits-1.
18034   Op = DAG.getNode(ISD::XOR, dl, OpVT, Op,
18035                    DAG.getConstant(NumBits - 1, dl, OpVT));
18036
18037   if (VT == MVT::i8)
18038     Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
18039   return Op;
18040 }
18041
18042 static SDValue LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
18043   MVT VT = Op.getSimpleValueType();
18044   unsigned NumBits = VT.getScalarSizeInBits();
18045   SDLoc dl(Op);
18046
18047   if (VT.isVector()) {
18048     const TargetLowering &TLI = DAG.getTargetLoweringInfo();
18049
18050     SDValue N0 = Op.getOperand(0);
18051     SDValue Zero = DAG.getConstant(0, dl, VT);
18052
18053     // lsb(x) = (x & -x)
18054     SDValue LSB = DAG.getNode(ISD::AND, dl, VT, N0,
18055                               DAG.getNode(ISD::SUB, dl, VT, Zero, N0));
18056
18057     // cttz_undef(x) = (width - 1) - ctlz(lsb)
18058     if (Op.getOpcode() == ISD::CTTZ_ZERO_UNDEF &&
18059         TLI.isOperationLegal(ISD::CTLZ, VT)) {
18060       SDValue WidthMinusOne = DAG.getConstant(NumBits - 1, dl, VT);
18061       return DAG.getNode(ISD::SUB, dl, VT, WidthMinusOne,
18062                          DAG.getNode(ISD::CTLZ, dl, VT, LSB));
18063     }
18064
18065     // cttz(x) = ctpop(lsb - 1)
18066     SDValue One = DAG.getConstant(1, dl, VT);
18067     return DAG.getNode(ISD::CTPOP, dl, VT,
18068                        DAG.getNode(ISD::SUB, dl, VT, LSB, One));
18069   }
18070
18071   assert(Op.getOpcode() == ISD::CTTZ &&
18072          "Only scalar CTTZ requires custom lowering");
18073
18074   // Issue a bsf (scan bits forward) which also sets EFLAGS.
18075   SDVTList VTs = DAG.getVTList(VT, MVT::i32);
18076   Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op.getOperand(0));
18077
18078   // If src is zero (i.e. bsf sets ZF), returns NumBits.
18079   SDValue Ops[] = {
18080     Op,
18081     DAG.getConstant(NumBits, dl, VT),
18082     DAG.getConstant(X86::COND_E, dl, MVT::i8),
18083     Op.getValue(1)
18084   };
18085   return DAG.getNode(X86ISD::CMOV, dl, VT, Ops);
18086 }
18087
18088 // Lower256IntArith - Break a 256-bit integer operation into two new 128-bit
18089 // ones, and then concatenate the result back.
18090 static SDValue Lower256IntArith(SDValue Op, SelectionDAG &DAG) {
18091   MVT VT = Op.getSimpleValueType();
18092
18093   assert(VT.is256BitVector() && VT.isInteger() &&
18094          "Unsupported value type for operation");
18095
18096   unsigned NumElems = VT.getVectorNumElements();
18097   SDLoc dl(Op);
18098
18099   // Extract the LHS vectors
18100   SDValue LHS = Op.getOperand(0);
18101   SDValue LHS1 = Extract128BitVector(LHS, 0, DAG, dl);
18102   SDValue LHS2 = Extract128BitVector(LHS, NumElems/2, DAG, dl);
18103
18104   // Extract the RHS vectors
18105   SDValue RHS = Op.getOperand(1);
18106   SDValue RHS1 = Extract128BitVector(RHS, 0, DAG, dl);
18107   SDValue RHS2 = Extract128BitVector(RHS, NumElems/2, DAG, dl);
18108
18109   MVT EltVT = VT.getVectorElementType();
18110   MVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
18111
18112   return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
18113                      DAG.getNode(Op.getOpcode(), dl, NewVT, LHS1, RHS1),
18114                      DAG.getNode(Op.getOpcode(), dl, NewVT, LHS2, RHS2));
18115 }
18116
18117 static SDValue LowerADD(SDValue Op, SelectionDAG &DAG) {
18118   if (Op.getValueType() == MVT::i1)
18119     return DAG.getNode(ISD::XOR, SDLoc(Op), Op.getValueType(),
18120                        Op.getOperand(0), Op.getOperand(1));
18121   assert(Op.getSimpleValueType().is256BitVector() &&
18122          Op.getSimpleValueType().isInteger() &&
18123          "Only handle AVX 256-bit vector integer operation");
18124   return Lower256IntArith(Op, DAG);
18125 }
18126
18127 static SDValue LowerSUB(SDValue Op, SelectionDAG &DAG) {
18128   if (Op.getValueType() == MVT::i1)
18129     return DAG.getNode(ISD::XOR, SDLoc(Op), Op.getValueType(),
18130                        Op.getOperand(0), Op.getOperand(1));
18131   assert(Op.getSimpleValueType().is256BitVector() &&
18132          Op.getSimpleValueType().isInteger() &&
18133          "Only handle AVX 256-bit vector integer operation");
18134   return Lower256IntArith(Op, DAG);
18135 }
18136
18137 static SDValue LowerMINMAX(SDValue Op, SelectionDAG &DAG) {
18138   assert(Op.getSimpleValueType().is256BitVector() &&
18139          Op.getSimpleValueType().isInteger() &&
18140          "Only handle AVX 256-bit vector integer operation");
18141   return Lower256IntArith(Op, DAG);
18142 }
18143
18144 static SDValue LowerMUL(SDValue Op, const X86Subtarget *Subtarget,
18145                         SelectionDAG &DAG) {
18146   SDLoc dl(Op);
18147   MVT VT = Op.getSimpleValueType();
18148
18149   if (VT == MVT::i1)
18150     return DAG.getNode(ISD::AND, dl, VT, Op.getOperand(0), Op.getOperand(1));
18151
18152   // Decompose 256-bit ops into smaller 128-bit ops.
18153   if (VT.is256BitVector() && !Subtarget->hasInt256())
18154     return Lower256IntArith(Op, DAG);
18155
18156   SDValue A = Op.getOperand(0);
18157   SDValue B = Op.getOperand(1);
18158
18159   // Lower v16i8/v32i8 mul as promotion to v8i16/v16i16 vector
18160   // pairs, multiply and truncate.
18161   if (VT == MVT::v16i8 || VT == MVT::v32i8) {
18162     if (Subtarget->hasInt256()) {
18163       if (VT == MVT::v32i8) {
18164         MVT SubVT = MVT::getVectorVT(MVT::i8, VT.getVectorNumElements() / 2);
18165         SDValue Lo = DAG.getIntPtrConstant(0, dl);
18166         SDValue Hi = DAG.getIntPtrConstant(VT.getVectorNumElements() / 2, dl);
18167         SDValue ALo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, SubVT, A, Lo);
18168         SDValue BLo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, SubVT, B, Lo);
18169         SDValue AHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, SubVT, A, Hi);
18170         SDValue BHi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, SubVT, B, Hi);
18171         return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT,
18172                            DAG.getNode(ISD::MUL, dl, SubVT, ALo, BLo),
18173                            DAG.getNode(ISD::MUL, dl, SubVT, AHi, BHi));
18174       }
18175
18176       MVT ExVT = MVT::getVectorVT(MVT::i16, VT.getVectorNumElements());
18177       return DAG.getNode(
18178           ISD::TRUNCATE, dl, VT,
18179           DAG.getNode(ISD::MUL, dl, ExVT,
18180                       DAG.getNode(ISD::SIGN_EXTEND, dl, ExVT, A),
18181                       DAG.getNode(ISD::SIGN_EXTEND, dl, ExVT, B)));
18182     }
18183
18184     assert(VT == MVT::v16i8 &&
18185            "Pre-AVX2 support only supports v16i8 multiplication");
18186     MVT ExVT = MVT::v8i16;
18187
18188     // Extract the lo parts and sign extend to i16
18189     SDValue ALo, BLo;
18190     if (Subtarget->hasSSE41()) {
18191       ALo = DAG.getNode(X86ISD::VSEXT, dl, ExVT, A);
18192       BLo = DAG.getNode(X86ISD::VSEXT, dl, ExVT, B);
18193     } else {
18194       const int ShufMask[] = {-1, 0, -1, 1, -1, 2, -1, 3,
18195                               -1, 4, -1, 5, -1, 6, -1, 7};
18196       ALo = DAG.getVectorShuffle(VT, dl, A, A, ShufMask);
18197       BLo = DAG.getVectorShuffle(VT, dl, B, B, ShufMask);
18198       ALo = DAG.getBitcast(ExVT, ALo);
18199       BLo = DAG.getBitcast(ExVT, BLo);
18200       ALo = DAG.getNode(ISD::SRA, dl, ExVT, ALo, DAG.getConstant(8, dl, ExVT));
18201       BLo = DAG.getNode(ISD::SRA, dl, ExVT, BLo, DAG.getConstant(8, dl, ExVT));
18202     }
18203
18204     // Extract the hi parts and sign extend to i16
18205     SDValue AHi, BHi;
18206     if (Subtarget->hasSSE41()) {
18207       const int ShufMask[] = {8,  9,  10, 11, 12, 13, 14, 15,
18208                               -1, -1, -1, -1, -1, -1, -1, -1};
18209       AHi = DAG.getVectorShuffle(VT, dl, A, A, ShufMask);
18210       BHi = DAG.getVectorShuffle(VT, dl, B, B, ShufMask);
18211       AHi = DAG.getNode(X86ISD::VSEXT, dl, ExVT, AHi);
18212       BHi = DAG.getNode(X86ISD::VSEXT, dl, ExVT, BHi);
18213     } else {
18214       const int ShufMask[] = {-1, 8,  -1, 9,  -1, 10, -1, 11,
18215                               -1, 12, -1, 13, -1, 14, -1, 15};
18216       AHi = DAG.getVectorShuffle(VT, dl, A, A, ShufMask);
18217       BHi = DAG.getVectorShuffle(VT, dl, B, B, ShufMask);
18218       AHi = DAG.getBitcast(ExVT, AHi);
18219       BHi = DAG.getBitcast(ExVT, BHi);
18220       AHi = DAG.getNode(ISD::SRA, dl, ExVT, AHi, DAG.getConstant(8, dl, ExVT));
18221       BHi = DAG.getNode(ISD::SRA, dl, ExVT, BHi, DAG.getConstant(8, dl, ExVT));
18222     }
18223
18224     // Multiply, mask the lower 8bits of the lo/hi results and pack
18225     SDValue RLo = DAG.getNode(ISD::MUL, dl, ExVT, ALo, BLo);
18226     SDValue RHi = DAG.getNode(ISD::MUL, dl, ExVT, AHi, BHi);
18227     RLo = DAG.getNode(ISD::AND, dl, ExVT, RLo, DAG.getConstant(255, dl, ExVT));
18228     RHi = DAG.getNode(ISD::AND, dl, ExVT, RHi, DAG.getConstant(255, dl, ExVT));
18229     return DAG.getNode(X86ISD::PACKUS, dl, VT, RLo, RHi);
18230   }
18231
18232   // Lower v4i32 mul as 2x shuffle, 2x pmuludq, 2x shuffle.
18233   if (VT == MVT::v4i32) {
18234     assert(Subtarget->hasSSE2() && !Subtarget->hasSSE41() &&
18235            "Should not custom lower when pmuldq is available!");
18236
18237     // Extract the odd parts.
18238     static const int UnpackMask[] = { 1, -1, 3, -1 };
18239     SDValue Aodds = DAG.getVectorShuffle(VT, dl, A, A, UnpackMask);
18240     SDValue Bodds = DAG.getVectorShuffle(VT, dl, B, B, UnpackMask);
18241
18242     // Multiply the even parts.
18243     SDValue Evens = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, A, B);
18244     // Now multiply odd parts.
18245     SDValue Odds = DAG.getNode(X86ISD::PMULUDQ, dl, MVT::v2i64, Aodds, Bodds);
18246
18247     Evens = DAG.getBitcast(VT, Evens);
18248     Odds = DAG.getBitcast(VT, Odds);
18249
18250     // Merge the two vectors back together with a shuffle. This expands into 2
18251     // shuffles.
18252     static const int ShufMask[] = { 0, 4, 2, 6 };
18253     return DAG.getVectorShuffle(VT, dl, Evens, Odds, ShufMask);
18254   }
18255
18256   assert((VT == MVT::v2i64 || VT == MVT::v4i64 || VT == MVT::v8i64) &&
18257          "Only know how to lower V2I64/V4I64/V8I64 multiply");
18258
18259   //  Ahi = psrlqi(a, 32);
18260   //  Bhi = psrlqi(b, 32);
18261   //
18262   //  AloBlo = pmuludq(a, b);
18263   //  AloBhi = pmuludq(a, Bhi);
18264   //  AhiBlo = pmuludq(Ahi, b);
18265
18266   //  AloBhi = psllqi(AloBhi, 32);
18267   //  AhiBlo = psllqi(AhiBlo, 32);
18268   //  return AloBlo + AloBhi + AhiBlo;
18269
18270   SDValue Ahi = getTargetVShiftByConstNode(X86ISD::VSRLI, dl, VT, A, 32, DAG);
18271   SDValue Bhi = getTargetVShiftByConstNode(X86ISD::VSRLI, dl, VT, B, 32, DAG);
18272
18273   SDValue AhiBlo = Ahi;
18274   SDValue AloBhi = Bhi;
18275   // Bit cast to 32-bit vectors for MULUDQ
18276   MVT MulVT = (VT == MVT::v2i64) ? MVT::v4i32 :
18277                                   (VT == MVT::v4i64) ? MVT::v8i32 : MVT::v16i32;
18278   A = DAG.getBitcast(MulVT, A);
18279   B = DAG.getBitcast(MulVT, B);
18280   Ahi = DAG.getBitcast(MulVT, Ahi);
18281   Bhi = DAG.getBitcast(MulVT, Bhi);
18282
18283   SDValue AloBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, B);
18284   // After shifting right const values the result may be all-zero.
18285   if (!ISD::isBuildVectorAllZeros(Ahi.getNode())) {
18286     AhiBlo = DAG.getNode(X86ISD::PMULUDQ, dl, VT, Ahi, B);
18287     AhiBlo = getTargetVShiftByConstNode(X86ISD::VSHLI, dl, VT, AhiBlo, 32, DAG);
18288   }
18289   if (!ISD::isBuildVectorAllZeros(Bhi.getNode())) {
18290     AloBhi = DAG.getNode(X86ISD::PMULUDQ, dl, VT, A, Bhi);
18291     AloBhi = getTargetVShiftByConstNode(X86ISD::VSHLI, dl, VT, AloBhi, 32, DAG);
18292   }
18293
18294   SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
18295   return DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
18296 }
18297
18298 SDValue X86TargetLowering::LowerWin64_i128OP(SDValue Op, SelectionDAG &DAG) const {
18299   assert(Subtarget->isTargetWin64() && "Unexpected target");
18300   EVT VT = Op.getValueType();
18301   assert(VT.isInteger() && VT.getSizeInBits() == 128 &&
18302          "Unexpected return type for lowering");
18303
18304   RTLIB::Libcall LC;
18305   bool isSigned;
18306   switch (Op->getOpcode()) {
18307   default: llvm_unreachable("Unexpected request for libcall!");
18308   case ISD::SDIV:      isSigned = true;  LC = RTLIB::SDIV_I128;    break;
18309   case ISD::UDIV:      isSigned = false; LC = RTLIB::UDIV_I128;    break;
18310   case ISD::SREM:      isSigned = true;  LC = RTLIB::SREM_I128;    break;
18311   case ISD::UREM:      isSigned = false; LC = RTLIB::UREM_I128;    break;
18312   case ISD::SDIVREM:   isSigned = true;  LC = RTLIB::SDIVREM_I128; break;
18313   case ISD::UDIVREM:   isSigned = false; LC = RTLIB::UDIVREM_I128; break;
18314   }
18315
18316   SDLoc dl(Op);
18317   SDValue InChain = DAG.getEntryNode();
18318
18319   TargetLowering::ArgListTy Args;
18320   TargetLowering::ArgListEntry Entry;
18321   for (unsigned i = 0, e = Op->getNumOperands(); i != e; ++i) {
18322     EVT ArgVT = Op->getOperand(i).getValueType();
18323     assert(ArgVT.isInteger() && ArgVT.getSizeInBits() == 128 &&
18324            "Unexpected argument type for lowering");
18325     SDValue StackPtr = DAG.CreateStackTemporary(ArgVT, 16);
18326     Entry.Node = StackPtr;
18327     InChain = DAG.getStore(InChain, dl, Op->getOperand(i), StackPtr, MachinePointerInfo(),
18328                            false, false, 16);
18329     Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
18330     Entry.Ty = PointerType::get(ArgTy,0);
18331     Entry.isSExt = false;
18332     Entry.isZExt = false;
18333     Args.push_back(Entry);
18334   }
18335
18336   SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
18337                                          getPointerTy(DAG.getDataLayout()));
18338
18339   TargetLowering::CallLoweringInfo CLI(DAG);
18340   CLI.setDebugLoc(dl).setChain(InChain)
18341     .setCallee(getLibcallCallingConv(LC),
18342                static_cast<EVT>(MVT::v2i64).getTypeForEVT(*DAG.getContext()),
18343                Callee, std::move(Args), 0)
18344     .setInRegister().setSExtResult(isSigned).setZExtResult(!isSigned);
18345
18346   std::pair<SDValue, SDValue> CallInfo = LowerCallTo(CLI);
18347   return DAG.getBitcast(VT, CallInfo.first);
18348 }
18349
18350 static SDValue LowerMUL_LOHI(SDValue Op, const X86Subtarget *Subtarget,
18351                              SelectionDAG &DAG) {
18352   SDValue Op0 = Op.getOperand(0), Op1 = Op.getOperand(1);
18353   MVT VT = Op0.getSimpleValueType();
18354   SDLoc dl(Op);
18355
18356   assert((VT == MVT::v4i32 && Subtarget->hasSSE2()) ||
18357          (VT == MVT::v8i32 && Subtarget->hasInt256()));
18358
18359   // PMULxD operations multiply each even value (starting at 0) of LHS with
18360   // the related value of RHS and produce a widen result.
18361   // E.g., PMULUDQ <4 x i32> <a|b|c|d>, <4 x i32> <e|f|g|h>
18362   // => <2 x i64> <ae|cg>
18363   //
18364   // In other word, to have all the results, we need to perform two PMULxD:
18365   // 1. one with the even values.
18366   // 2. one with the odd values.
18367   // To achieve #2, with need to place the odd values at an even position.
18368   //
18369   // Place the odd value at an even position (basically, shift all values 1
18370   // step to the left):
18371   const int Mask[] = {1, -1, 3, -1, 5, -1, 7, -1};
18372   // <a|b|c|d> => <b|undef|d|undef>
18373   SDValue Odd0 = DAG.getVectorShuffle(VT, dl, Op0, Op0, Mask);
18374   // <e|f|g|h> => <f|undef|h|undef>
18375   SDValue Odd1 = DAG.getVectorShuffle(VT, dl, Op1, Op1, Mask);
18376
18377   // Emit two multiplies, one for the lower 2 ints and one for the higher 2
18378   // ints.
18379   MVT MulVT = VT == MVT::v4i32 ? MVT::v2i64 : MVT::v4i64;
18380   bool IsSigned = Op->getOpcode() == ISD::SMUL_LOHI;
18381   unsigned Opcode =
18382       (!IsSigned || !Subtarget->hasSSE41()) ? X86ISD::PMULUDQ : X86ISD::PMULDQ;
18383   // PMULUDQ <4 x i32> <a|b|c|d>, <4 x i32> <e|f|g|h>
18384   // => <2 x i64> <ae|cg>
18385   SDValue Mul1 = DAG.getBitcast(VT, DAG.getNode(Opcode, dl, MulVT, Op0, Op1));
18386   // PMULUDQ <4 x i32> <b|undef|d|undef>, <4 x i32> <f|undef|h|undef>
18387   // => <2 x i64> <bf|dh>
18388   SDValue Mul2 = DAG.getBitcast(VT, DAG.getNode(Opcode, dl, MulVT, Odd0, Odd1));
18389
18390   // Shuffle it back into the right order.
18391   SDValue Highs, Lows;
18392   if (VT == MVT::v8i32) {
18393     const int HighMask[] = {1, 9, 3, 11, 5, 13, 7, 15};
18394     Highs = DAG.getVectorShuffle(VT, dl, Mul1, Mul2, HighMask);
18395     const int LowMask[] = {0, 8, 2, 10, 4, 12, 6, 14};
18396     Lows = DAG.getVectorShuffle(VT, dl, Mul1, Mul2, LowMask);
18397   } else {
18398     const int HighMask[] = {1, 5, 3, 7};
18399     Highs = DAG.getVectorShuffle(VT, dl, Mul1, Mul2, HighMask);
18400     const int LowMask[] = {0, 4, 2, 6};
18401     Lows = DAG.getVectorShuffle(VT, dl, Mul1, Mul2, LowMask);
18402   }
18403
18404   // If we have a signed multiply but no PMULDQ fix up the high parts of a
18405   // unsigned multiply.
18406   if (IsSigned && !Subtarget->hasSSE41()) {
18407     SDValue ShAmt = DAG.getConstant(
18408         31, dl,
18409         DAG.getTargetLoweringInfo().getShiftAmountTy(VT, DAG.getDataLayout()));
18410     SDValue T1 = DAG.getNode(ISD::AND, dl, VT,
18411                              DAG.getNode(ISD::SRA, dl, VT, Op0, ShAmt), Op1);
18412     SDValue T2 = DAG.getNode(ISD::AND, dl, VT,
18413                              DAG.getNode(ISD::SRA, dl, VT, Op1, ShAmt), Op0);
18414
18415     SDValue Fixup = DAG.getNode(ISD::ADD, dl, VT, T1, T2);
18416     Highs = DAG.getNode(ISD::SUB, dl, VT, Highs, Fixup);
18417   }
18418
18419   // The first result of MUL_LOHI is actually the low value, followed by the
18420   // high value.
18421   SDValue Ops[] = {Lows, Highs};
18422   return DAG.getMergeValues(Ops, dl);
18423 }
18424
18425 // Return true if the required (according to Opcode) shift-imm form is natively
18426 // supported by the Subtarget
18427 static bool SupportedVectorShiftWithImm(MVT VT, const X86Subtarget *Subtarget,
18428                                         unsigned Opcode) {
18429   if (VT.getScalarSizeInBits() < 16)
18430     return false;
18431
18432   if (VT.is512BitVector() &&
18433       (VT.getScalarSizeInBits() > 16 || Subtarget->hasBWI()))
18434     return true;
18435
18436   bool LShift = VT.is128BitVector() ||
18437     (VT.is256BitVector() && Subtarget->hasInt256());
18438
18439   bool AShift = LShift && (Subtarget->hasVLX() ||
18440     (VT != MVT::v2i64 && VT != MVT::v4i64));
18441   return (Opcode == ISD::SRA) ? AShift : LShift;
18442 }
18443
18444 // The shift amount is a variable, but it is the same for all vector lanes.
18445 // These instructions are defined together with shift-immediate.
18446 static
18447 bool SupportedVectorShiftWithBaseAmnt(MVT VT, const X86Subtarget *Subtarget,
18448                                       unsigned Opcode) {
18449   return SupportedVectorShiftWithImm(VT, Subtarget, Opcode);
18450 }
18451
18452 // Return true if the required (according to Opcode) variable-shift form is
18453 // natively supported by the Subtarget
18454 static bool SupportedVectorVarShift(MVT VT, const X86Subtarget *Subtarget,
18455                                     unsigned Opcode) {
18456
18457   if (!Subtarget->hasInt256() || VT.getScalarSizeInBits() < 16)
18458     return false;
18459
18460   // vXi16 supported only on AVX-512, BWI
18461   if (VT.getScalarSizeInBits() == 16 && !Subtarget->hasBWI())
18462     return false;
18463
18464   if (VT.is512BitVector() || Subtarget->hasVLX())
18465     return true;
18466
18467   bool LShift = VT.is128BitVector() || VT.is256BitVector();
18468   bool AShift = LShift &&  VT != MVT::v2i64 && VT != MVT::v4i64;
18469   return (Opcode == ISD::SRA) ? AShift : LShift;
18470 }
18471
18472 static SDValue LowerScalarImmediateShift(SDValue Op, SelectionDAG &DAG,
18473                                          const X86Subtarget *Subtarget) {
18474   MVT VT = Op.getSimpleValueType();
18475   SDLoc dl(Op);
18476   SDValue R = Op.getOperand(0);
18477   SDValue Amt = Op.getOperand(1);
18478
18479   unsigned X86Opc = (Op.getOpcode() == ISD::SHL) ? X86ISD::VSHLI :
18480     (Op.getOpcode() == ISD::SRL) ? X86ISD::VSRLI : X86ISD::VSRAI;
18481
18482   auto ArithmeticShiftRight64 = [&](uint64_t ShiftAmt) {
18483     assert((VT == MVT::v2i64 || VT == MVT::v4i64) && "Unexpected SRA type");
18484     MVT ExVT = MVT::getVectorVT(MVT::i32, VT.getVectorNumElements() * 2);
18485     SDValue Ex = DAG.getBitcast(ExVT, R);
18486
18487     if (ShiftAmt >= 32) {
18488       // Splat sign to upper i32 dst, and SRA upper i32 src to lower i32.
18489       SDValue Upper =
18490           getTargetVShiftByConstNode(X86ISD::VSRAI, dl, ExVT, Ex, 31, DAG);
18491       SDValue Lower = getTargetVShiftByConstNode(X86ISD::VSRAI, dl, ExVT, Ex,
18492                                                  ShiftAmt - 32, DAG);
18493       if (VT == MVT::v2i64)
18494         Ex = DAG.getVectorShuffle(ExVT, dl, Upper, Lower, {5, 1, 7, 3});
18495       if (VT == MVT::v4i64)
18496         Ex = DAG.getVectorShuffle(ExVT, dl, Upper, Lower,
18497                                   {9, 1, 11, 3, 13, 5, 15, 7});
18498     } else {
18499       // SRA upper i32, SHL whole i64 and select lower i32.
18500       SDValue Upper = getTargetVShiftByConstNode(X86ISD::VSRAI, dl, ExVT, Ex,
18501                                                  ShiftAmt, DAG);
18502       SDValue Lower =
18503           getTargetVShiftByConstNode(X86ISD::VSRLI, dl, VT, R, ShiftAmt, DAG);
18504       Lower = DAG.getBitcast(ExVT, Lower);
18505       if (VT == MVT::v2i64)
18506         Ex = DAG.getVectorShuffle(ExVT, dl, Upper, Lower, {4, 1, 6, 3});
18507       if (VT == MVT::v4i64)
18508         Ex = DAG.getVectorShuffle(ExVT, dl, Upper, Lower,
18509                                   {8, 1, 10, 3, 12, 5, 14, 7});
18510     }
18511     return DAG.getBitcast(VT, Ex);
18512   };
18513
18514   // Optimize shl/srl/sra with constant shift amount.
18515   if (auto *BVAmt = dyn_cast<BuildVectorSDNode>(Amt)) {
18516     if (auto *ShiftConst = BVAmt->getConstantSplatNode()) {
18517       uint64_t ShiftAmt = ShiftConst->getZExtValue();
18518
18519       if (SupportedVectorShiftWithImm(VT, Subtarget, Op.getOpcode()))
18520         return getTargetVShiftByConstNode(X86Opc, dl, VT, R, ShiftAmt, DAG);
18521
18522       // i64 SRA needs to be performed as partial shifts.
18523       if ((VT == MVT::v2i64 || (Subtarget->hasInt256() && VT == MVT::v4i64)) &&
18524           Op.getOpcode() == ISD::SRA && !Subtarget->hasXOP())
18525         return ArithmeticShiftRight64(ShiftAmt);
18526
18527       if (VT == MVT::v16i8 ||
18528           (Subtarget->hasInt256() && VT == MVT::v32i8) ||
18529           VT == MVT::v64i8) {
18530         unsigned NumElts = VT.getVectorNumElements();
18531         MVT ShiftVT = MVT::getVectorVT(MVT::i16, NumElts / 2);
18532
18533         // Simple i8 add case
18534         if (Op.getOpcode() == ISD::SHL && ShiftAmt == 1)
18535           return DAG.getNode(ISD::ADD, dl, VT, R, R);
18536
18537         // ashr(R, 7)  === cmp_slt(R, 0)
18538         if (Op.getOpcode() == ISD::SRA && ShiftAmt == 7) {
18539           SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
18540           return DAG.getNode(X86ISD::PCMPGT, dl, VT, Zeros, R);
18541         }
18542
18543         // XOP can shift v16i8 directly instead of as shift v8i16 + mask.
18544         if (VT == MVT::v16i8 && Subtarget->hasXOP())
18545           return SDValue();
18546
18547         if (Op.getOpcode() == ISD::SHL) {
18548           // Make a large shift.
18549           SDValue SHL = getTargetVShiftByConstNode(X86ISD::VSHLI, dl, ShiftVT,
18550                                                    R, ShiftAmt, DAG);
18551           SHL = DAG.getBitcast(VT, SHL);
18552           // Zero out the rightmost bits.
18553           return DAG.getNode(ISD::AND, dl, VT, SHL,
18554                              DAG.getConstant(uint8_t(-1U << ShiftAmt), dl, VT));
18555         }
18556         if (Op.getOpcode() == ISD::SRL) {
18557           // Make a large shift.
18558           SDValue SRL = getTargetVShiftByConstNode(X86ISD::VSRLI, dl, ShiftVT,
18559                                                    R, ShiftAmt, DAG);
18560           SRL = DAG.getBitcast(VT, SRL);
18561           // Zero out the leftmost bits.
18562           return DAG.getNode(ISD::AND, dl, VT, SRL,
18563                              DAG.getConstant(uint8_t(-1U) >> ShiftAmt, dl, VT));
18564         }
18565         if (Op.getOpcode() == ISD::SRA) {
18566           // ashr(R, Amt) === sub(xor(lshr(R, Amt), Mask), Mask)
18567           SDValue Res = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
18568
18569           SDValue Mask = DAG.getConstant(128 >> ShiftAmt, dl, VT);
18570           Res = DAG.getNode(ISD::XOR, dl, VT, Res, Mask);
18571           Res = DAG.getNode(ISD::SUB, dl, VT, Res, Mask);
18572           return Res;
18573         }
18574         llvm_unreachable("Unknown shift opcode.");
18575       }
18576     }
18577   }
18578
18579   // Special case in 32-bit mode, where i64 is expanded into high and low parts.
18580   if (!Subtarget->is64Bit() && !Subtarget->hasXOP() &&
18581       (VT == MVT::v2i64 || (Subtarget->hasInt256() && VT == MVT::v4i64))) {
18582
18583     // Peek through any splat that was introduced for i64 shift vectorization.
18584     int SplatIndex = -1;
18585     if (ShuffleVectorSDNode *SVN = dyn_cast<ShuffleVectorSDNode>(Amt.getNode()))
18586       if (SVN->isSplat()) {
18587         SplatIndex = SVN->getSplatIndex();
18588         Amt = Amt.getOperand(0);
18589         assert(SplatIndex < (int)VT.getVectorNumElements() &&
18590                "Splat shuffle referencing second operand");
18591       }
18592
18593     if (Amt.getOpcode() != ISD::BITCAST ||
18594         Amt.getOperand(0).getOpcode() != ISD::BUILD_VECTOR)
18595       return SDValue();
18596
18597     Amt = Amt.getOperand(0);
18598     unsigned Ratio = Amt.getSimpleValueType().getVectorNumElements() /
18599                      VT.getVectorNumElements();
18600     unsigned RatioInLog2 = Log2_32_Ceil(Ratio);
18601     uint64_t ShiftAmt = 0;
18602     unsigned BaseOp = (SplatIndex < 0 ? 0 : SplatIndex * Ratio);
18603     for (unsigned i = 0; i != Ratio; ++i) {
18604       ConstantSDNode *C = dyn_cast<ConstantSDNode>(Amt.getOperand(i + BaseOp));
18605       if (!C)
18606         return SDValue();
18607       // 6 == Log2(64)
18608       ShiftAmt |= C->getZExtValue() << (i * (1 << (6 - RatioInLog2)));
18609     }
18610
18611     // Check remaining shift amounts (if not a splat).
18612     if (SplatIndex < 0) {
18613       for (unsigned i = Ratio; i != Amt.getNumOperands(); i += Ratio) {
18614         uint64_t ShAmt = 0;
18615         for (unsigned j = 0; j != Ratio; ++j) {
18616           ConstantSDNode *C = dyn_cast<ConstantSDNode>(Amt.getOperand(i + j));
18617           if (!C)
18618             return SDValue();
18619           // 6 == Log2(64)
18620           ShAmt |= C->getZExtValue() << (j * (1 << (6 - RatioInLog2)));
18621         }
18622         if (ShAmt != ShiftAmt)
18623           return SDValue();
18624       }
18625     }
18626
18627     if (SupportedVectorShiftWithImm(VT, Subtarget, Op.getOpcode()))
18628       return getTargetVShiftByConstNode(X86Opc, dl, VT, R, ShiftAmt, DAG);
18629
18630     if (Op.getOpcode() == ISD::SRA)
18631       return ArithmeticShiftRight64(ShiftAmt);
18632   }
18633
18634   return SDValue();
18635 }
18636
18637 static SDValue LowerScalarVariableShift(SDValue Op, SelectionDAG &DAG,
18638                                         const X86Subtarget* Subtarget) {
18639   MVT VT = Op.getSimpleValueType();
18640   SDLoc dl(Op);
18641   SDValue R = Op.getOperand(0);
18642   SDValue Amt = Op.getOperand(1);
18643
18644   unsigned X86OpcI = (Op.getOpcode() == ISD::SHL) ? X86ISD::VSHLI :
18645     (Op.getOpcode() == ISD::SRL) ? X86ISD::VSRLI : X86ISD::VSRAI;
18646
18647   unsigned X86OpcV = (Op.getOpcode() == ISD::SHL) ? X86ISD::VSHL :
18648     (Op.getOpcode() == ISD::SRL) ? X86ISD::VSRL : X86ISD::VSRA;
18649
18650   if (SupportedVectorShiftWithBaseAmnt(VT, Subtarget, Op.getOpcode())) {
18651     SDValue BaseShAmt;
18652     MVT EltVT = VT.getVectorElementType();
18653
18654     if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(Amt)) {
18655       // Check if this build_vector node is doing a splat.
18656       // If so, then set BaseShAmt equal to the splat value.
18657       BaseShAmt = BV->getSplatValue();
18658       if (BaseShAmt && BaseShAmt.getOpcode() == ISD::UNDEF)
18659         BaseShAmt = SDValue();
18660     } else {
18661       if (Amt.getOpcode() == ISD::EXTRACT_SUBVECTOR)
18662         Amt = Amt.getOperand(0);
18663
18664       ShuffleVectorSDNode *SVN = dyn_cast<ShuffleVectorSDNode>(Amt);
18665       if (SVN && SVN->isSplat()) {
18666         unsigned SplatIdx = (unsigned)SVN->getSplatIndex();
18667         SDValue InVec = Amt.getOperand(0);
18668         if (InVec.getOpcode() == ISD::BUILD_VECTOR) {
18669           assert((SplatIdx < InVec.getSimpleValueType().getVectorNumElements()) &&
18670                  "Unexpected shuffle index found!");
18671           BaseShAmt = InVec.getOperand(SplatIdx);
18672         } else if (InVec.getOpcode() == ISD::INSERT_VECTOR_ELT) {
18673            if (ConstantSDNode *C =
18674                dyn_cast<ConstantSDNode>(InVec.getOperand(2))) {
18675              if (C->getZExtValue() == SplatIdx)
18676                BaseShAmt = InVec.getOperand(1);
18677            }
18678         }
18679
18680         if (!BaseShAmt)
18681           // Avoid introducing an extract element from a shuffle.
18682           BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, EltVT, InVec,
18683                                   DAG.getIntPtrConstant(SplatIdx, dl));
18684       }
18685     }
18686
18687     if (BaseShAmt.getNode()) {
18688       assert(EltVT.bitsLE(MVT::i64) && "Unexpected element type!");
18689       if (EltVT != MVT::i64 && EltVT.bitsGT(MVT::i32))
18690         BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i64, BaseShAmt);
18691       else if (EltVT.bitsLT(MVT::i32))
18692         BaseShAmt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, BaseShAmt);
18693
18694       return getTargetVShiftNode(X86OpcI, dl, VT, R, BaseShAmt, DAG);
18695     }
18696   }
18697
18698   // Special case in 32-bit mode, where i64 is expanded into high and low parts.
18699   if (!Subtarget->is64Bit() && VT == MVT::v2i64  &&
18700       Amt.getOpcode() == ISD::BITCAST &&
18701       Amt.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) {
18702     Amt = Amt.getOperand(0);
18703     unsigned Ratio = Amt.getSimpleValueType().getVectorNumElements() /
18704                      VT.getVectorNumElements();
18705     std::vector<SDValue> Vals(Ratio);
18706     for (unsigned i = 0; i != Ratio; ++i)
18707       Vals[i] = Amt.getOperand(i);
18708     for (unsigned i = Ratio; i != Amt.getNumOperands(); i += Ratio) {
18709       for (unsigned j = 0; j != Ratio; ++j)
18710         if (Vals[j] != Amt.getOperand(i + j))
18711           return SDValue();
18712     }
18713
18714     if (SupportedVectorShiftWithBaseAmnt(VT, Subtarget, Op.getOpcode()))
18715       return DAG.getNode(X86OpcV, dl, VT, R, Op.getOperand(1));
18716   }
18717   return SDValue();
18718 }
18719
18720 static SDValue LowerShift(SDValue Op, const X86Subtarget* Subtarget,
18721                           SelectionDAG &DAG) {
18722   MVT VT = Op.getSimpleValueType();
18723   SDLoc dl(Op);
18724   SDValue R = Op.getOperand(0);
18725   SDValue Amt = Op.getOperand(1);
18726
18727   assert(VT.isVector() && "Custom lowering only for vector shifts!");
18728   assert(Subtarget->hasSSE2() && "Only custom lower when we have SSE2!");
18729
18730   if (SDValue V = LowerScalarImmediateShift(Op, DAG, Subtarget))
18731     return V;
18732
18733   if (SDValue V = LowerScalarVariableShift(Op, DAG, Subtarget))
18734     return V;
18735
18736   if (SupportedVectorVarShift(VT, Subtarget, Op.getOpcode()))
18737     return Op;
18738
18739   // XOP has 128-bit variable logical/arithmetic shifts.
18740   // +ve/-ve Amt = shift left/right.
18741   if (Subtarget->hasXOP() &&
18742       (VT == MVT::v2i64 || VT == MVT::v4i32 ||
18743        VT == MVT::v8i16 || VT == MVT::v16i8)) {
18744     if (Op.getOpcode() == ISD::SRL || Op.getOpcode() == ISD::SRA) {
18745       SDValue Zero = getZeroVector(VT, Subtarget, DAG, dl);
18746       Amt = DAG.getNode(ISD::SUB, dl, VT, Zero, Amt);
18747     }
18748     if (Op.getOpcode() == ISD::SHL || Op.getOpcode() == ISD::SRL)
18749       return DAG.getNode(X86ISD::VPSHL, dl, VT, R, Amt);
18750     if (Op.getOpcode() == ISD::SRA)
18751       return DAG.getNode(X86ISD::VPSHA, dl, VT, R, Amt);
18752   }
18753
18754   // 2i64 vector logical shifts can efficiently avoid scalarization - do the
18755   // shifts per-lane and then shuffle the partial results back together.
18756   if (VT == MVT::v2i64 && Op.getOpcode() != ISD::SRA) {
18757     // Splat the shift amounts so the scalar shifts above will catch it.
18758     SDValue Amt0 = DAG.getVectorShuffle(VT, dl, Amt, Amt, {0, 0});
18759     SDValue Amt1 = DAG.getVectorShuffle(VT, dl, Amt, Amt, {1, 1});
18760     SDValue R0 = DAG.getNode(Op->getOpcode(), dl, VT, R, Amt0);
18761     SDValue R1 = DAG.getNode(Op->getOpcode(), dl, VT, R, Amt1);
18762     return DAG.getVectorShuffle(VT, dl, R0, R1, {0, 3});
18763   }
18764
18765   // i64 vector arithmetic shift can be emulated with the transform:
18766   // M = lshr(SIGN_BIT, Amt)
18767   // ashr(R, Amt) === sub(xor(lshr(R, Amt), M), M)
18768   if ((VT == MVT::v2i64 || (VT == MVT::v4i64 && Subtarget->hasInt256())) &&
18769       Op.getOpcode() == ISD::SRA) {
18770     SDValue S = DAG.getConstant(APInt::getSignBit(64), dl, VT);
18771     SDValue M = DAG.getNode(ISD::SRL, dl, VT, S, Amt);
18772     R = DAG.getNode(ISD::SRL, dl, VT, R, Amt);
18773     R = DAG.getNode(ISD::XOR, dl, VT, R, M);
18774     R = DAG.getNode(ISD::SUB, dl, VT, R, M);
18775     return R;
18776   }
18777
18778   // If possible, lower this packed shift into a vector multiply instead of
18779   // expanding it into a sequence of scalar shifts.
18780   // Do this only if the vector shift count is a constant build_vector.
18781   if (Op.getOpcode() == ISD::SHL &&
18782       (VT == MVT::v8i16 || VT == MVT::v4i32 ||
18783        (Subtarget->hasInt256() && VT == MVT::v16i16)) &&
18784       ISD::isBuildVectorOfConstantSDNodes(Amt.getNode())) {
18785     SmallVector<SDValue, 8> Elts;
18786     MVT SVT = VT.getVectorElementType();
18787     unsigned SVTBits = SVT.getSizeInBits();
18788     APInt One(SVTBits, 1);
18789     unsigned NumElems = VT.getVectorNumElements();
18790
18791     for (unsigned i=0; i !=NumElems; ++i) {
18792       SDValue Op = Amt->getOperand(i);
18793       if (Op->getOpcode() == ISD::UNDEF) {
18794         Elts.push_back(Op);
18795         continue;
18796       }
18797
18798       ConstantSDNode *ND = cast<ConstantSDNode>(Op);
18799       APInt C(SVTBits, ND->getAPIntValue().getZExtValue());
18800       uint64_t ShAmt = C.getZExtValue();
18801       if (ShAmt >= SVTBits) {
18802         Elts.push_back(DAG.getUNDEF(SVT));
18803         continue;
18804       }
18805       Elts.push_back(DAG.getConstant(One.shl(ShAmt), dl, SVT));
18806     }
18807     SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, Elts);
18808     return DAG.getNode(ISD::MUL, dl, VT, R, BV);
18809   }
18810
18811   // Lower SHL with variable shift amount.
18812   if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) {
18813     Op = DAG.getNode(ISD::SHL, dl, VT, Amt, DAG.getConstant(23, dl, VT));
18814
18815     Op = DAG.getNode(ISD::ADD, dl, VT, Op,
18816                      DAG.getConstant(0x3f800000U, dl, VT));
18817     Op = DAG.getBitcast(MVT::v4f32, Op);
18818     Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);
18819     return DAG.getNode(ISD::MUL, dl, VT, Op, R);
18820   }
18821
18822   // If possible, lower this shift as a sequence of two shifts by
18823   // constant plus a MOVSS/MOVSD instead of scalarizing it.
18824   // Example:
18825   //   (v4i32 (srl A, (build_vector < X, Y, Y, Y>)))
18826   //
18827   // Could be rewritten as:
18828   //   (v4i32 (MOVSS (srl A, <Y,Y,Y,Y>), (srl A, <X,X,X,X>)))
18829   //
18830   // The advantage is that the two shifts from the example would be
18831   // lowered as X86ISD::VSRLI nodes. This would be cheaper than scalarizing
18832   // the vector shift into four scalar shifts plus four pairs of vector
18833   // insert/extract.
18834   if ((VT == MVT::v8i16 || VT == MVT::v4i32) &&
18835       ISD::isBuildVectorOfConstantSDNodes(Amt.getNode())) {
18836     unsigned TargetOpcode = X86ISD::MOVSS;
18837     bool CanBeSimplified;
18838     // The splat value for the first packed shift (the 'X' from the example).
18839     SDValue Amt1 = Amt->getOperand(0);
18840     // The splat value for the second packed shift (the 'Y' from the example).
18841     SDValue Amt2 = (VT == MVT::v4i32) ? Amt->getOperand(1) :
18842                                         Amt->getOperand(2);
18843
18844     // See if it is possible to replace this node with a sequence of
18845     // two shifts followed by a MOVSS/MOVSD
18846     if (VT == MVT::v4i32) {
18847       // Check if it is legal to use a MOVSS.
18848       CanBeSimplified = Amt2 == Amt->getOperand(2) &&
18849                         Amt2 == Amt->getOperand(3);
18850       if (!CanBeSimplified) {
18851         // Otherwise, check if we can still simplify this node using a MOVSD.
18852         CanBeSimplified = Amt1 == Amt->getOperand(1) &&
18853                           Amt->getOperand(2) == Amt->getOperand(3);
18854         TargetOpcode = X86ISD::MOVSD;
18855         Amt2 = Amt->getOperand(2);
18856       }
18857     } else {
18858       // Do similar checks for the case where the machine value type
18859       // is MVT::v8i16.
18860       CanBeSimplified = Amt1 == Amt->getOperand(1);
18861       for (unsigned i=3; i != 8 && CanBeSimplified; ++i)
18862         CanBeSimplified = Amt2 == Amt->getOperand(i);
18863
18864       if (!CanBeSimplified) {
18865         TargetOpcode = X86ISD::MOVSD;
18866         CanBeSimplified = true;
18867         Amt2 = Amt->getOperand(4);
18868         for (unsigned i=0; i != 4 && CanBeSimplified; ++i)
18869           CanBeSimplified = Amt1 == Amt->getOperand(i);
18870         for (unsigned j=4; j != 8 && CanBeSimplified; ++j)
18871           CanBeSimplified = Amt2 == Amt->getOperand(j);
18872       }
18873     }
18874
18875     if (CanBeSimplified && isa<ConstantSDNode>(Amt1) &&
18876         isa<ConstantSDNode>(Amt2)) {
18877       // Replace this node with two shifts followed by a MOVSS/MOVSD.
18878       MVT CastVT = MVT::v4i32;
18879       SDValue Splat1 =
18880         DAG.getConstant(cast<ConstantSDNode>(Amt1)->getAPIntValue(), dl, VT);
18881       SDValue Shift1 = DAG.getNode(Op->getOpcode(), dl, VT, R, Splat1);
18882       SDValue Splat2 =
18883         DAG.getConstant(cast<ConstantSDNode>(Amt2)->getAPIntValue(), dl, VT);
18884       SDValue Shift2 = DAG.getNode(Op->getOpcode(), dl, VT, R, Splat2);
18885       if (TargetOpcode == X86ISD::MOVSD)
18886         CastVT = MVT::v2i64;
18887       SDValue BitCast1 = DAG.getBitcast(CastVT, Shift1);
18888       SDValue BitCast2 = DAG.getBitcast(CastVT, Shift2);
18889       SDValue Result = getTargetShuffleNode(TargetOpcode, dl, CastVT, BitCast2,
18890                                             BitCast1, DAG);
18891       return DAG.getBitcast(VT, Result);
18892     }
18893   }
18894
18895   // v4i32 Non Uniform Shifts.
18896   // If the shift amount is constant we can shift each lane using the SSE2
18897   // immediate shifts, else we need to zero-extend each lane to the lower i64
18898   // and shift using the SSE2 variable shifts.
18899   // The separate results can then be blended together.
18900   if (VT == MVT::v4i32) {
18901     unsigned Opc = Op.getOpcode();
18902     SDValue Amt0, Amt1, Amt2, Amt3;
18903     if (ISD::isBuildVectorOfConstantSDNodes(Amt.getNode())) {
18904       Amt0 = DAG.getVectorShuffle(VT, dl, Amt, DAG.getUNDEF(VT), {0, 0, 0, 0});
18905       Amt1 = DAG.getVectorShuffle(VT, dl, Amt, DAG.getUNDEF(VT), {1, 1, 1, 1});
18906       Amt2 = DAG.getVectorShuffle(VT, dl, Amt, DAG.getUNDEF(VT), {2, 2, 2, 2});
18907       Amt3 = DAG.getVectorShuffle(VT, dl, Amt, DAG.getUNDEF(VT), {3, 3, 3, 3});
18908     } else {
18909       // ISD::SHL is handled above but we include it here for completeness.
18910       switch (Opc) {
18911       default:
18912         llvm_unreachable("Unknown target vector shift node");
18913       case ISD::SHL:
18914         Opc = X86ISD::VSHL;
18915         break;
18916       case ISD::SRL:
18917         Opc = X86ISD::VSRL;
18918         break;
18919       case ISD::SRA:
18920         Opc = X86ISD::VSRA;
18921         break;
18922       }
18923       // The SSE2 shifts use the lower i64 as the same shift amount for
18924       // all lanes and the upper i64 is ignored. These shuffle masks
18925       // optimally zero-extend each lanes on SSE2/SSE41/AVX targets.
18926       SDValue Z = getZeroVector(VT, Subtarget, DAG, dl);
18927       Amt0 = DAG.getVectorShuffle(VT, dl, Amt, Z, {0, 4, -1, -1});
18928       Amt1 = DAG.getVectorShuffle(VT, dl, Amt, Z, {1, 5, -1, -1});
18929       Amt2 = DAG.getVectorShuffle(VT, dl, Amt, Z, {2, 6, -1, -1});
18930       Amt3 = DAG.getVectorShuffle(VT, dl, Amt, Z, {3, 7, -1, -1});
18931     }
18932
18933     SDValue R0 = DAG.getNode(Opc, dl, VT, R, Amt0);
18934     SDValue R1 = DAG.getNode(Opc, dl, VT, R, Amt1);
18935     SDValue R2 = DAG.getNode(Opc, dl, VT, R, Amt2);
18936     SDValue R3 = DAG.getNode(Opc, dl, VT, R, Amt3);
18937     SDValue R02 = DAG.getVectorShuffle(VT, dl, R0, R2, {0, -1, 6, -1});
18938     SDValue R13 = DAG.getVectorShuffle(VT, dl, R1, R3, {-1, 1, -1, 7});
18939     return DAG.getVectorShuffle(VT, dl, R02, R13, {0, 5, 2, 7});
18940   }
18941
18942   if (VT == MVT::v16i8 ||
18943       (VT == MVT::v32i8 && Subtarget->hasInt256() && !Subtarget->hasXOP())) {
18944     MVT ExtVT = MVT::getVectorVT(MVT::i16, VT.getVectorNumElements() / 2);
18945     unsigned ShiftOpcode = Op->getOpcode();
18946
18947     auto SignBitSelect = [&](MVT SelVT, SDValue Sel, SDValue V0, SDValue V1) {
18948       // On SSE41 targets we make use of the fact that VSELECT lowers
18949       // to PBLENDVB which selects bytes based just on the sign bit.
18950       if (Subtarget->hasSSE41()) {
18951         V0 = DAG.getBitcast(VT, V0);
18952         V1 = DAG.getBitcast(VT, V1);
18953         Sel = DAG.getBitcast(VT, Sel);
18954         return DAG.getBitcast(SelVT,
18955                               DAG.getNode(ISD::VSELECT, dl, VT, Sel, V0, V1));
18956       }
18957       // On pre-SSE41 targets we test for the sign bit by comparing to
18958       // zero - a negative value will set all bits of the lanes to true
18959       // and VSELECT uses that in its OR(AND(V0,C),AND(V1,~C)) lowering.
18960       SDValue Z = getZeroVector(SelVT, Subtarget, DAG, dl);
18961       SDValue C = DAG.getNode(X86ISD::PCMPGT, dl, SelVT, Z, Sel);
18962       return DAG.getNode(ISD::VSELECT, dl, SelVT, C, V0, V1);
18963     };
18964
18965     // Turn 'a' into a mask suitable for VSELECT: a = a << 5;
18966     // We can safely do this using i16 shifts as we're only interested in
18967     // the 3 lower bits of each byte.
18968     Amt = DAG.getBitcast(ExtVT, Amt);
18969     Amt = DAG.getNode(ISD::SHL, dl, ExtVT, Amt, DAG.getConstant(5, dl, ExtVT));
18970     Amt = DAG.getBitcast(VT, Amt);
18971
18972     if (Op->getOpcode() == ISD::SHL || Op->getOpcode() == ISD::SRL) {
18973       // r = VSELECT(r, shift(r, 4), a);
18974       SDValue M =
18975           DAG.getNode(ShiftOpcode, dl, VT, R, DAG.getConstant(4, dl, VT));
18976       R = SignBitSelect(VT, Amt, M, R);
18977
18978       // a += a
18979       Amt = DAG.getNode(ISD::ADD, dl, VT, Amt, Amt);
18980
18981       // r = VSELECT(r, shift(r, 2), a);
18982       M = DAG.getNode(ShiftOpcode, dl, VT, R, DAG.getConstant(2, dl, VT));
18983       R = SignBitSelect(VT, Amt, M, R);
18984
18985       // a += a
18986       Amt = DAG.getNode(ISD::ADD, dl, VT, Amt, Amt);
18987
18988       // return VSELECT(r, shift(r, 1), a);
18989       M = DAG.getNode(ShiftOpcode, dl, VT, R, DAG.getConstant(1, dl, VT));
18990       R = SignBitSelect(VT, Amt, M, R);
18991       return R;
18992     }
18993
18994     if (Op->getOpcode() == ISD::SRA) {
18995       // For SRA we need to unpack each byte to the higher byte of a i16 vector
18996       // so we can correctly sign extend. We don't care what happens to the
18997       // lower byte.
18998       SDValue ALo = DAG.getNode(X86ISD::UNPCKL, dl, VT, DAG.getUNDEF(VT), Amt);
18999       SDValue AHi = DAG.getNode(X86ISD::UNPCKH, dl, VT, DAG.getUNDEF(VT), Amt);
19000       SDValue RLo = DAG.getNode(X86ISD::UNPCKL, dl, VT, DAG.getUNDEF(VT), R);
19001       SDValue RHi = DAG.getNode(X86ISD::UNPCKH, dl, VT, DAG.getUNDEF(VT), R);
19002       ALo = DAG.getBitcast(ExtVT, ALo);
19003       AHi = DAG.getBitcast(ExtVT, AHi);
19004       RLo = DAG.getBitcast(ExtVT, RLo);
19005       RHi = DAG.getBitcast(ExtVT, RHi);
19006
19007       // r = VSELECT(r, shift(r, 4), a);
19008       SDValue MLo = DAG.getNode(ShiftOpcode, dl, ExtVT, RLo,
19009                                 DAG.getConstant(4, dl, ExtVT));
19010       SDValue MHi = DAG.getNode(ShiftOpcode, dl, ExtVT, RHi,
19011                                 DAG.getConstant(4, dl, ExtVT));
19012       RLo = SignBitSelect(ExtVT, ALo, MLo, RLo);
19013       RHi = SignBitSelect(ExtVT, AHi, MHi, RHi);
19014
19015       // a += a
19016       ALo = DAG.getNode(ISD::ADD, dl, ExtVT, ALo, ALo);
19017       AHi = DAG.getNode(ISD::ADD, dl, ExtVT, AHi, AHi);
19018
19019       // r = VSELECT(r, shift(r, 2), a);
19020       MLo = DAG.getNode(ShiftOpcode, dl, ExtVT, RLo,
19021                         DAG.getConstant(2, dl, ExtVT));
19022       MHi = DAG.getNode(ShiftOpcode, dl, ExtVT, RHi,
19023                         DAG.getConstant(2, dl, ExtVT));
19024       RLo = SignBitSelect(ExtVT, ALo, MLo, RLo);
19025       RHi = SignBitSelect(ExtVT, AHi, MHi, RHi);
19026
19027       // a += a
19028       ALo = DAG.getNode(ISD::ADD, dl, ExtVT, ALo, ALo);
19029       AHi = DAG.getNode(ISD::ADD, dl, ExtVT, AHi, AHi);
19030
19031       // r = VSELECT(r, shift(r, 1), a);
19032       MLo = DAG.getNode(ShiftOpcode, dl, ExtVT, RLo,
19033                         DAG.getConstant(1, dl, ExtVT));
19034       MHi = DAG.getNode(ShiftOpcode, dl, ExtVT, RHi,
19035                         DAG.getConstant(1, dl, ExtVT));
19036       RLo = SignBitSelect(ExtVT, ALo, MLo, RLo);
19037       RHi = SignBitSelect(ExtVT, AHi, MHi, RHi);
19038
19039       // Logical shift the result back to the lower byte, leaving a zero upper
19040       // byte
19041       // meaning that we can safely pack with PACKUSWB.
19042       RLo =
19043           DAG.getNode(ISD::SRL, dl, ExtVT, RLo, DAG.getConstant(8, dl, ExtVT));
19044       RHi =
19045           DAG.getNode(ISD::SRL, dl, ExtVT, RHi, DAG.getConstant(8, dl, ExtVT));
19046       return DAG.getNode(X86ISD::PACKUS, dl, VT, RLo, RHi);
19047     }
19048   }
19049
19050   // It's worth extending once and using the v8i32 shifts for 16-bit types, but
19051   // the extra overheads to get from v16i8 to v8i32 make the existing SSE
19052   // solution better.
19053   if (Subtarget->hasInt256() && VT == MVT::v8i16) {
19054     MVT ExtVT = MVT::v8i32;
19055     unsigned ExtOpc =
19056         Op.getOpcode() == ISD::SRA ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
19057     R = DAG.getNode(ExtOpc, dl, ExtVT, R);
19058     Amt = DAG.getNode(ISD::ANY_EXTEND, dl, ExtVT, Amt);
19059     return DAG.getNode(ISD::TRUNCATE, dl, VT,
19060                        DAG.getNode(Op.getOpcode(), dl, ExtVT, R, Amt));
19061   }
19062
19063   if (Subtarget->hasInt256() && !Subtarget->hasXOP() && VT == MVT::v16i16) {
19064     MVT ExtVT = MVT::v8i32;
19065     SDValue Z = getZeroVector(VT, Subtarget, DAG, dl);
19066     SDValue ALo = DAG.getNode(X86ISD::UNPCKL, dl, VT, Amt, Z);
19067     SDValue AHi = DAG.getNode(X86ISD::UNPCKH, dl, VT, Amt, Z);
19068     SDValue RLo = DAG.getNode(X86ISD::UNPCKL, dl, VT, R, R);
19069     SDValue RHi = DAG.getNode(X86ISD::UNPCKH, dl, VT, R, R);
19070     ALo = DAG.getBitcast(ExtVT, ALo);
19071     AHi = DAG.getBitcast(ExtVT, AHi);
19072     RLo = DAG.getBitcast(ExtVT, RLo);
19073     RHi = DAG.getBitcast(ExtVT, RHi);
19074     SDValue Lo = DAG.getNode(Op.getOpcode(), dl, ExtVT, RLo, ALo);
19075     SDValue Hi = DAG.getNode(Op.getOpcode(), dl, ExtVT, RHi, AHi);
19076     Lo = DAG.getNode(ISD::SRL, dl, ExtVT, Lo, DAG.getConstant(16, dl, ExtVT));
19077     Hi = DAG.getNode(ISD::SRL, dl, ExtVT, Hi, DAG.getConstant(16, dl, ExtVT));
19078     return DAG.getNode(X86ISD::PACKUS, dl, VT, Lo, Hi);
19079   }
19080
19081   if (VT == MVT::v8i16) {
19082     unsigned ShiftOpcode = Op->getOpcode();
19083
19084     auto SignBitSelect = [&](SDValue Sel, SDValue V0, SDValue V1) {
19085       // On SSE41 targets we make use of the fact that VSELECT lowers
19086       // to PBLENDVB which selects bytes based just on the sign bit.
19087       if (Subtarget->hasSSE41()) {
19088         MVT ExtVT = MVT::getVectorVT(MVT::i8, VT.getVectorNumElements() * 2);
19089         V0 = DAG.getBitcast(ExtVT, V0);
19090         V1 = DAG.getBitcast(ExtVT, V1);
19091         Sel = DAG.getBitcast(ExtVT, Sel);
19092         return DAG.getBitcast(
19093             VT, DAG.getNode(ISD::VSELECT, dl, ExtVT, Sel, V0, V1));
19094       }
19095       // On pre-SSE41 targets we splat the sign bit - a negative value will
19096       // set all bits of the lanes to true and VSELECT uses that in
19097       // its OR(AND(V0,C),AND(V1,~C)) lowering.
19098       SDValue C =
19099           DAG.getNode(ISD::SRA, dl, VT, Sel, DAG.getConstant(15, dl, VT));
19100       return DAG.getNode(ISD::VSELECT, dl, VT, C, V0, V1);
19101     };
19102
19103     // Turn 'a' into a mask suitable for VSELECT: a = a << 12;
19104     if (Subtarget->hasSSE41()) {
19105       // On SSE41 targets we need to replicate the shift mask in both
19106       // bytes for PBLENDVB.
19107       Amt = DAG.getNode(
19108           ISD::OR, dl, VT,
19109           DAG.getNode(ISD::SHL, dl, VT, Amt, DAG.getConstant(4, dl, VT)),
19110           DAG.getNode(ISD::SHL, dl, VT, Amt, DAG.getConstant(12, dl, VT)));
19111     } else {
19112       Amt = DAG.getNode(ISD::SHL, dl, VT, Amt, DAG.getConstant(12, dl, VT));
19113     }
19114
19115     // r = VSELECT(r, shift(r, 8), a);
19116     SDValue M = DAG.getNode(ShiftOpcode, dl, VT, R, DAG.getConstant(8, dl, VT));
19117     R = SignBitSelect(Amt, M, R);
19118
19119     // a += a
19120     Amt = DAG.getNode(ISD::ADD, dl, VT, Amt, Amt);
19121
19122     // r = VSELECT(r, shift(r, 4), a);
19123     M = DAG.getNode(ShiftOpcode, dl, VT, R, DAG.getConstant(4, dl, VT));
19124     R = SignBitSelect(Amt, M, R);
19125
19126     // a += a
19127     Amt = DAG.getNode(ISD::ADD, dl, VT, Amt, Amt);
19128
19129     // r = VSELECT(r, shift(r, 2), a);
19130     M = DAG.getNode(ShiftOpcode, dl, VT, R, DAG.getConstant(2, dl, VT));
19131     R = SignBitSelect(Amt, M, R);
19132
19133     // a += a
19134     Amt = DAG.getNode(ISD::ADD, dl, VT, Amt, Amt);
19135
19136     // return VSELECT(r, shift(r, 1), a);
19137     M = DAG.getNode(ShiftOpcode, dl, VT, R, DAG.getConstant(1, dl, VT));
19138     R = SignBitSelect(Amt, M, R);
19139     return R;
19140   }
19141
19142   // Decompose 256-bit shifts into smaller 128-bit shifts.
19143   if (VT.is256BitVector()) {
19144     unsigned NumElems = VT.getVectorNumElements();
19145     MVT EltVT = VT.getVectorElementType();
19146     MVT NewVT = MVT::getVectorVT(EltVT, NumElems/2);
19147
19148     // Extract the two vectors
19149     SDValue V1 = Extract128BitVector(R, 0, DAG, dl);
19150     SDValue V2 = Extract128BitVector(R, NumElems/2, DAG, dl);
19151
19152     // Recreate the shift amount vectors
19153     SDValue Amt1, Amt2;
19154     if (Amt.getOpcode() == ISD::BUILD_VECTOR) {
19155       // Constant shift amount
19156       SmallVector<SDValue, 8> Ops(Amt->op_begin(), Amt->op_begin() + NumElems);
19157       ArrayRef<SDValue> Amt1Csts = makeArrayRef(Ops).slice(0, NumElems / 2);
19158       ArrayRef<SDValue> Amt2Csts = makeArrayRef(Ops).slice(NumElems / 2);
19159
19160       Amt1 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT, Amt1Csts);
19161       Amt2 = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT, Amt2Csts);
19162     } else {
19163       // Variable shift amount
19164       Amt1 = Extract128BitVector(Amt, 0, DAG, dl);
19165       Amt2 = Extract128BitVector(Amt, NumElems/2, DAG, dl);
19166     }
19167
19168     // Issue new vector shifts for the smaller types
19169     V1 = DAG.getNode(Op.getOpcode(), dl, NewVT, V1, Amt1);
19170     V2 = DAG.getNode(Op.getOpcode(), dl, NewVT, V2, Amt2);
19171
19172     // Concatenate the result back
19173     return DAG.getNode(ISD::CONCAT_VECTORS, dl, VT, V1, V2);
19174   }
19175
19176   return SDValue();
19177 }
19178
19179 static SDValue LowerRotate(SDValue Op, const X86Subtarget *Subtarget,
19180                            SelectionDAG &DAG) {
19181   MVT VT = Op.getSimpleValueType();
19182   SDLoc DL(Op);
19183   SDValue R = Op.getOperand(0);
19184   SDValue Amt = Op.getOperand(1);
19185
19186   assert(VT.isVector() && "Custom lowering only for vector rotates!");
19187   assert(Subtarget->hasXOP() && "XOP support required for vector rotates!");
19188   assert((Op.getOpcode() == ISD::ROTL) && "Only ROTL supported");
19189
19190   // XOP has 128-bit vector variable + immediate rotates.
19191   // +ve/-ve Amt = rotate left/right.
19192
19193   // Split 256-bit integers.
19194   if (VT.is256BitVector())
19195     return Lower256IntArith(Op, DAG);
19196
19197   assert(VT.is128BitVector() && "Only rotate 128-bit vectors!");
19198
19199   // Attempt to rotate by immediate.
19200   if (auto *BVAmt = dyn_cast<BuildVectorSDNode>(Amt)) {
19201     if (auto *RotateConst = BVAmt->getConstantSplatNode()) {
19202       uint64_t RotateAmt = RotateConst->getAPIntValue().getZExtValue();
19203       assert(RotateAmt < VT.getScalarSizeInBits() && "Rotation out of range");
19204       return DAG.getNode(X86ISD::VPROTI, DL, VT, R,
19205                          DAG.getConstant(RotateAmt, DL, MVT::i8));
19206     }
19207   }
19208
19209   // Use general rotate by variable (per-element).
19210   return DAG.getNode(X86ISD::VPROT, DL, VT, R, Amt);
19211 }
19212
19213 static SDValue LowerXALUO(SDValue Op, SelectionDAG &DAG) {
19214   // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
19215   // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
19216   // looks for this combo and may remove the "setcc" instruction if the "setcc"
19217   // has only one use.
19218   SDNode *N = Op.getNode();
19219   SDValue LHS = N->getOperand(0);
19220   SDValue RHS = N->getOperand(1);
19221   unsigned BaseOp = 0;
19222   unsigned Cond = 0;
19223   SDLoc DL(Op);
19224   switch (Op.getOpcode()) {
19225   default: llvm_unreachable("Unknown ovf instruction!");
19226   case ISD::SADDO:
19227     // A subtract of one will be selected as a INC. Note that INC doesn't
19228     // set CF, so we can't do this for UADDO.
19229     if (isOneConstant(RHS)) {
19230         BaseOp = X86ISD::INC;
19231         Cond = X86::COND_O;
19232         break;
19233       }
19234     BaseOp = X86ISD::ADD;
19235     Cond = X86::COND_O;
19236     break;
19237   case ISD::UADDO:
19238     BaseOp = X86ISD::ADD;
19239     Cond = X86::COND_B;
19240     break;
19241   case ISD::SSUBO:
19242     // A subtract of one will be selected as a DEC. Note that DEC doesn't
19243     // set CF, so we can't do this for USUBO.
19244     if (isOneConstant(RHS)) {
19245         BaseOp = X86ISD::DEC;
19246         Cond = X86::COND_O;
19247         break;
19248       }
19249     BaseOp = X86ISD::SUB;
19250     Cond = X86::COND_O;
19251     break;
19252   case ISD::USUBO:
19253     BaseOp = X86ISD::SUB;
19254     Cond = X86::COND_B;
19255     break;
19256   case ISD::SMULO:
19257     BaseOp = N->getValueType(0) == MVT::i8 ? X86ISD::SMUL8 : X86ISD::SMUL;
19258     Cond = X86::COND_O;
19259     break;
19260   case ISD::UMULO: { // i64, i8 = umulo lhs, rhs --> i64, i64, i32 umul lhs,rhs
19261     if (N->getValueType(0) == MVT::i8) {
19262       BaseOp = X86ISD::UMUL8;
19263       Cond = X86::COND_O;
19264       break;
19265     }
19266     SDVTList VTs = DAG.getVTList(N->getValueType(0), N->getValueType(0),
19267                                  MVT::i32);
19268     SDValue Sum = DAG.getNode(X86ISD::UMUL, DL, VTs, LHS, RHS);
19269
19270     SDValue SetCC =
19271       DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
19272                   DAG.getConstant(X86::COND_O, DL, MVT::i32),
19273                   SDValue(Sum.getNode(), 2));
19274
19275     return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
19276   }
19277   }
19278
19279   // Also sets EFLAGS.
19280   SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
19281   SDValue Sum = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
19282
19283   SDValue SetCC =
19284     DAG.getNode(X86ISD::SETCC, DL, N->getValueType(1),
19285                 DAG.getConstant(Cond, DL, MVT::i32),
19286                 SDValue(Sum.getNode(), 1));
19287
19288   return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Sum, SetCC);
19289 }
19290
19291 /// Returns true if the operand type is exactly twice the native width, and
19292 /// the corresponding cmpxchg8b or cmpxchg16b instruction is available.
19293 /// Used to know whether to use cmpxchg8/16b when expanding atomic operations
19294 /// (otherwise we leave them alone to become __sync_fetch_and_... calls).
19295 bool X86TargetLowering::needsCmpXchgNb(Type *MemType) const {
19296   unsigned OpWidth = MemType->getPrimitiveSizeInBits();
19297
19298   if (OpWidth == 64)
19299     return !Subtarget->is64Bit(); // FIXME this should be Subtarget.hasCmpxchg8b
19300   else if (OpWidth == 128)
19301     return Subtarget->hasCmpxchg16b();
19302   else
19303     return false;
19304 }
19305
19306 bool X86TargetLowering::shouldExpandAtomicStoreInIR(StoreInst *SI) const {
19307   return needsCmpXchgNb(SI->getValueOperand()->getType());
19308 }
19309
19310 // Note: this turns large loads into lock cmpxchg8b/16b.
19311 // FIXME: On 32 bits x86, fild/movq might be faster than lock cmpxchg8b.
19312 TargetLowering::AtomicExpansionKind
19313 X86TargetLowering::shouldExpandAtomicLoadInIR(LoadInst *LI) const {
19314   auto PTy = cast<PointerType>(LI->getPointerOperand()->getType());
19315   return needsCmpXchgNb(PTy->getElementType()) ? AtomicExpansionKind::CmpXChg
19316                                                : AtomicExpansionKind::None;
19317 }
19318
19319 TargetLowering::AtomicExpansionKind
19320 X86TargetLowering::shouldExpandAtomicRMWInIR(AtomicRMWInst *AI) const {
19321   unsigned NativeWidth = Subtarget->is64Bit() ? 64 : 32;
19322   Type *MemType = AI->getType();
19323
19324   // If the operand is too big, we must see if cmpxchg8/16b is available
19325   // and default to library calls otherwise.
19326   if (MemType->getPrimitiveSizeInBits() > NativeWidth) {
19327     return needsCmpXchgNb(MemType) ? AtomicExpansionKind::CmpXChg
19328                                    : AtomicExpansionKind::None;
19329   }
19330
19331   AtomicRMWInst::BinOp Op = AI->getOperation();
19332   switch (Op) {
19333   default:
19334     llvm_unreachable("Unknown atomic operation");
19335   case AtomicRMWInst::Xchg:
19336   case AtomicRMWInst::Add:
19337   case AtomicRMWInst::Sub:
19338     // It's better to use xadd, xsub or xchg for these in all cases.
19339     return AtomicExpansionKind::None;
19340   case AtomicRMWInst::Or:
19341   case AtomicRMWInst::And:
19342   case AtomicRMWInst::Xor:
19343     // If the atomicrmw's result isn't actually used, we can just add a "lock"
19344     // prefix to a normal instruction for these operations.
19345     return !AI->use_empty() ? AtomicExpansionKind::CmpXChg
19346                             : AtomicExpansionKind::None;
19347   case AtomicRMWInst::Nand:
19348   case AtomicRMWInst::Max:
19349   case AtomicRMWInst::Min:
19350   case AtomicRMWInst::UMax:
19351   case AtomicRMWInst::UMin:
19352     // These always require a non-trivial set of data operations on x86. We must
19353     // use a cmpxchg loop.
19354     return AtomicExpansionKind::CmpXChg;
19355   }
19356 }
19357
19358 static bool hasMFENCE(const X86Subtarget& Subtarget) {
19359   // Use mfence if we have SSE2 or we're on x86-64 (even if we asked for
19360   // no-sse2). There isn't any reason to disable it if the target processor
19361   // supports it.
19362   return Subtarget.hasSSE2() || Subtarget.is64Bit();
19363 }
19364
19365 LoadInst *
19366 X86TargetLowering::lowerIdempotentRMWIntoFencedLoad(AtomicRMWInst *AI) const {
19367   unsigned NativeWidth = Subtarget->is64Bit() ? 64 : 32;
19368   Type *MemType = AI->getType();
19369   // Accesses larger than the native width are turned into cmpxchg/libcalls, so
19370   // there is no benefit in turning such RMWs into loads, and it is actually
19371   // harmful as it introduces a mfence.
19372   if (MemType->getPrimitiveSizeInBits() > NativeWidth)
19373     return nullptr;
19374
19375   auto Builder = IRBuilder<>(AI);
19376   Module *M = Builder.GetInsertBlock()->getParent()->getParent();
19377   auto SynchScope = AI->getSynchScope();
19378   // We must restrict the ordering to avoid generating loads with Release or
19379   // ReleaseAcquire orderings.
19380   auto Order = AtomicCmpXchgInst::getStrongestFailureOrdering(AI->getOrdering());
19381   auto Ptr = AI->getPointerOperand();
19382
19383   // Before the load we need a fence. Here is an example lifted from
19384   // http://www.hpl.hp.com/techreports/2012/HPL-2012-68.pdf showing why a fence
19385   // is required:
19386   // Thread 0:
19387   //   x.store(1, relaxed);
19388   //   r1 = y.fetch_add(0, release);
19389   // Thread 1:
19390   //   y.fetch_add(42, acquire);
19391   //   r2 = x.load(relaxed);
19392   // r1 = r2 = 0 is impossible, but becomes possible if the idempotent rmw is
19393   // lowered to just a load without a fence. A mfence flushes the store buffer,
19394   // making the optimization clearly correct.
19395   // FIXME: it is required if isAtLeastRelease(Order) but it is not clear
19396   // otherwise, we might be able to be more aggressive on relaxed idempotent
19397   // rmw. In practice, they do not look useful, so we don't try to be
19398   // especially clever.
19399   if (SynchScope == SingleThread)
19400     // FIXME: we could just insert an X86ISD::MEMBARRIER here, except we are at
19401     // the IR level, so we must wrap it in an intrinsic.
19402     return nullptr;
19403
19404   if (!hasMFENCE(*Subtarget))
19405     // FIXME: it might make sense to use a locked operation here but on a
19406     // different cache-line to prevent cache-line bouncing. In practice it
19407     // is probably a small win, and x86 processors without mfence are rare
19408     // enough that we do not bother.
19409     return nullptr;
19410
19411   Function *MFence =
19412       llvm::Intrinsic::getDeclaration(M, Intrinsic::x86_sse2_mfence);
19413   Builder.CreateCall(MFence, {});
19414
19415   // Finally we can emit the atomic load.
19416   LoadInst *Loaded = Builder.CreateAlignedLoad(Ptr,
19417           AI->getType()->getPrimitiveSizeInBits());
19418   Loaded->setAtomic(Order, SynchScope);
19419   AI->replaceAllUsesWith(Loaded);
19420   AI->eraseFromParent();
19421   return Loaded;
19422 }
19423
19424 static SDValue LowerATOMIC_FENCE(SDValue Op, const X86Subtarget *Subtarget,
19425                                  SelectionDAG &DAG) {
19426   SDLoc dl(Op);
19427   AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
19428     cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
19429   SynchronizationScope FenceScope = static_cast<SynchronizationScope>(
19430     cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
19431
19432   // The only fence that needs an instruction is a sequentially-consistent
19433   // cross-thread fence.
19434   if (FenceOrdering == SequentiallyConsistent && FenceScope == CrossThread) {
19435     if (hasMFENCE(*Subtarget))
19436       return DAG.getNode(X86ISD::MFENCE, dl, MVT::Other, Op.getOperand(0));
19437
19438     SDValue Chain = Op.getOperand(0);
19439     SDValue Zero = DAG.getConstant(0, dl, MVT::i32);
19440     SDValue Ops[] = {
19441       DAG.getRegister(X86::ESP, MVT::i32),     // Base
19442       DAG.getTargetConstant(1, dl, MVT::i8),   // Scale
19443       DAG.getRegister(0, MVT::i32),            // Index
19444       DAG.getTargetConstant(0, dl, MVT::i32),  // Disp
19445       DAG.getRegister(0, MVT::i32),            // Segment.
19446       Zero,
19447       Chain
19448     };
19449     SDNode *Res = DAG.getMachineNode(X86::OR32mrLocked, dl, MVT::Other, Ops);
19450     return SDValue(Res, 0);
19451   }
19452
19453   // MEMBARRIER is a compiler barrier; it codegens to a no-op.
19454   return DAG.getNode(X86ISD::MEMBARRIER, dl, MVT::Other, Op.getOperand(0));
19455 }
19456
19457 static SDValue LowerCMP_SWAP(SDValue Op, const X86Subtarget *Subtarget,
19458                              SelectionDAG &DAG) {
19459   MVT T = Op.getSimpleValueType();
19460   SDLoc DL(Op);
19461   unsigned Reg = 0;
19462   unsigned size = 0;
19463   switch(T.SimpleTy) {
19464   default: llvm_unreachable("Invalid value type!");
19465   case MVT::i8:  Reg = X86::AL;  size = 1; break;
19466   case MVT::i16: Reg = X86::AX;  size = 2; break;
19467   case MVT::i32: Reg = X86::EAX; size = 4; break;
19468   case MVT::i64:
19469     assert(Subtarget->is64Bit() && "Node not type legal!");
19470     Reg = X86::RAX; size = 8;
19471     break;
19472   }
19473   SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), DL, Reg,
19474                                   Op.getOperand(2), SDValue());
19475   SDValue Ops[] = { cpIn.getValue(0),
19476                     Op.getOperand(1),
19477                     Op.getOperand(3),
19478                     DAG.getTargetConstant(size, DL, MVT::i8),
19479                     cpIn.getValue(1) };
19480   SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
19481   MachineMemOperand *MMO = cast<AtomicSDNode>(Op)->getMemOperand();
19482   SDValue Result = DAG.getMemIntrinsicNode(X86ISD::LCMPXCHG_DAG, DL, Tys,
19483                                            Ops, T, MMO);
19484
19485   SDValue cpOut =
19486     DAG.getCopyFromReg(Result.getValue(0), DL, Reg, T, Result.getValue(1));
19487   SDValue EFLAGS = DAG.getCopyFromReg(cpOut.getValue(1), DL, X86::EFLAGS,
19488                                       MVT::i32, cpOut.getValue(2));
19489   SDValue Success = DAG.getNode(X86ISD::SETCC, DL, Op->getValueType(1),
19490                                 DAG.getConstant(X86::COND_E, DL, MVT::i8),
19491                                 EFLAGS);
19492
19493   DAG.ReplaceAllUsesOfValueWith(Op.getValue(0), cpOut);
19494   DAG.ReplaceAllUsesOfValueWith(Op.getValue(1), Success);
19495   DAG.ReplaceAllUsesOfValueWith(Op.getValue(2), EFLAGS.getValue(1));
19496   return SDValue();
19497 }
19498
19499 static SDValue LowerBITCAST(SDValue Op, const X86Subtarget *Subtarget,
19500                             SelectionDAG &DAG) {
19501   MVT SrcVT = Op.getOperand(0).getSimpleValueType();
19502   MVT DstVT = Op.getSimpleValueType();
19503
19504   if (SrcVT == MVT::v2i32 || SrcVT == MVT::v4i16 || SrcVT == MVT::v8i8) {
19505     assert(Subtarget->hasSSE2() && "Requires at least SSE2!");
19506     if (DstVT != MVT::f64)
19507       // This conversion needs to be expanded.
19508       return SDValue();
19509
19510     SDValue InVec = Op->getOperand(0);
19511     SDLoc dl(Op);
19512     unsigned NumElts = SrcVT.getVectorNumElements();
19513     MVT SVT = SrcVT.getVectorElementType();
19514
19515     // Widen the vector in input in the case of MVT::v2i32.
19516     // Example: from MVT::v2i32 to MVT::v4i32.
19517     SmallVector<SDValue, 16> Elts;
19518     for (unsigned i = 0, e = NumElts; i != e; ++i)
19519       Elts.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, SVT, InVec,
19520                                  DAG.getIntPtrConstant(i, dl)));
19521
19522     // Explicitly mark the extra elements as Undef.
19523     Elts.append(NumElts, DAG.getUNDEF(SVT));
19524
19525     EVT NewVT = EVT::getVectorVT(*DAG.getContext(), SVT, NumElts * 2);
19526     SDValue BV = DAG.getNode(ISD::BUILD_VECTOR, dl, NewVT, Elts);
19527     SDValue ToV2F64 = DAG.getBitcast(MVT::v2f64, BV);
19528     return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, ToV2F64,
19529                        DAG.getIntPtrConstant(0, dl));
19530   }
19531
19532   assert(Subtarget->is64Bit() && !Subtarget->hasSSE2() &&
19533          Subtarget->hasMMX() && "Unexpected custom BITCAST");
19534   assert((DstVT == MVT::i64 ||
19535           (DstVT.isVector() && DstVT.getSizeInBits()==64)) &&
19536          "Unexpected custom BITCAST");
19537   // i64 <=> MMX conversions are Legal.
19538   if (SrcVT==MVT::i64 && DstVT.isVector())
19539     return Op;
19540   if (DstVT==MVT::i64 && SrcVT.isVector())
19541     return Op;
19542   // MMX <=> MMX conversions are Legal.
19543   if (SrcVT.isVector() && DstVT.isVector())
19544     return Op;
19545   // All other conversions need to be expanded.
19546   return SDValue();
19547 }
19548
19549 /// Compute the horizontal sum of bytes in V for the elements of VT.
19550 ///
19551 /// Requires V to be a byte vector and VT to be an integer vector type with
19552 /// wider elements than V's type. The width of the elements of VT determines
19553 /// how many bytes of V are summed horizontally to produce each element of the
19554 /// result.
19555 static SDValue LowerHorizontalByteSum(SDValue V, MVT VT,
19556                                       const X86Subtarget *Subtarget,
19557                                       SelectionDAG &DAG) {
19558   SDLoc DL(V);
19559   MVT ByteVecVT = V.getSimpleValueType();
19560   MVT EltVT = VT.getVectorElementType();
19561   int NumElts = VT.getVectorNumElements();
19562   assert(ByteVecVT.getVectorElementType() == MVT::i8 &&
19563          "Expected value to have byte element type.");
19564   assert(EltVT != MVT::i8 &&
19565          "Horizontal byte sum only makes sense for wider elements!");
19566   unsigned VecSize = VT.getSizeInBits();
19567   assert(ByteVecVT.getSizeInBits() == VecSize && "Cannot change vector size!");
19568
19569   // PSADBW instruction horizontally add all bytes and leave the result in i64
19570   // chunks, thus directly computes the pop count for v2i64 and v4i64.
19571   if (EltVT == MVT::i64) {
19572     SDValue Zeros = getZeroVector(ByteVecVT, Subtarget, DAG, DL);
19573     MVT SadVecVT = MVT::getVectorVT(MVT::i64, VecSize / 64);
19574     V = DAG.getNode(X86ISD::PSADBW, DL, SadVecVT, V, Zeros);
19575     return DAG.getBitcast(VT, V);
19576   }
19577
19578   if (EltVT == MVT::i32) {
19579     // We unpack the low half and high half into i32s interleaved with zeros so
19580     // that we can use PSADBW to horizontally sum them. The most useful part of
19581     // this is that it lines up the results of two PSADBW instructions to be
19582     // two v2i64 vectors which concatenated are the 4 population counts. We can
19583     // then use PACKUSWB to shrink and concatenate them into a v4i32 again.
19584     SDValue Zeros = getZeroVector(VT, Subtarget, DAG, DL);
19585     SDValue Low = DAG.getNode(X86ISD::UNPCKL, DL, VT, V, Zeros);
19586     SDValue High = DAG.getNode(X86ISD::UNPCKH, DL, VT, V, Zeros);
19587
19588     // Do the horizontal sums into two v2i64s.
19589     Zeros = getZeroVector(ByteVecVT, Subtarget, DAG, DL);
19590     MVT SadVecVT = MVT::getVectorVT(MVT::i64, VecSize / 64);
19591     Low = DAG.getNode(X86ISD::PSADBW, DL, SadVecVT,
19592                       DAG.getBitcast(ByteVecVT, Low), Zeros);
19593     High = DAG.getNode(X86ISD::PSADBW, DL, SadVecVT,
19594                        DAG.getBitcast(ByteVecVT, High), Zeros);
19595
19596     // Merge them together.
19597     MVT ShortVecVT = MVT::getVectorVT(MVT::i16, VecSize / 16);
19598     V = DAG.getNode(X86ISD::PACKUS, DL, ByteVecVT,
19599                     DAG.getBitcast(ShortVecVT, Low),
19600                     DAG.getBitcast(ShortVecVT, High));
19601
19602     return DAG.getBitcast(VT, V);
19603   }
19604
19605   // The only element type left is i16.
19606   assert(EltVT == MVT::i16 && "Unknown how to handle type");
19607
19608   // To obtain pop count for each i16 element starting from the pop count for
19609   // i8 elements, shift the i16s left by 8, sum as i8s, and then shift as i16s
19610   // right by 8. It is important to shift as i16s as i8 vector shift isn't
19611   // directly supported.
19612   SmallVector<SDValue, 16> Shifters(NumElts, DAG.getConstant(8, DL, EltVT));
19613   SDValue Shifter = DAG.getNode(ISD::BUILD_VECTOR, DL, VT, Shifters);
19614   SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, DAG.getBitcast(VT, V), Shifter);
19615   V = DAG.getNode(ISD::ADD, DL, ByteVecVT, DAG.getBitcast(ByteVecVT, Shl),
19616                   DAG.getBitcast(ByteVecVT, V));
19617   return DAG.getNode(ISD::SRL, DL, VT, DAG.getBitcast(VT, V), Shifter);
19618 }
19619
19620 static SDValue LowerVectorCTPOPInRegLUT(SDValue Op, SDLoc DL,
19621                                         const X86Subtarget *Subtarget,
19622                                         SelectionDAG &DAG) {
19623   MVT VT = Op.getSimpleValueType();
19624   MVT EltVT = VT.getVectorElementType();
19625   unsigned VecSize = VT.getSizeInBits();
19626
19627   // Implement a lookup table in register by using an algorithm based on:
19628   // http://wm.ite.pl/articles/sse-popcount.html
19629   //
19630   // The general idea is that every lower byte nibble in the input vector is an
19631   // index into a in-register pre-computed pop count table. We then split up the
19632   // input vector in two new ones: (1) a vector with only the shifted-right
19633   // higher nibbles for each byte and (2) a vector with the lower nibbles (and
19634   // masked out higher ones) for each byte. PSHUB is used separately with both
19635   // to index the in-register table. Next, both are added and the result is a
19636   // i8 vector where each element contains the pop count for input byte.
19637   //
19638   // To obtain the pop count for elements != i8, we follow up with the same
19639   // approach and use additional tricks as described below.
19640   //
19641   const int LUT[16] = {/* 0 */ 0, /* 1 */ 1, /* 2 */ 1, /* 3 */ 2,
19642                        /* 4 */ 1, /* 5 */ 2, /* 6 */ 2, /* 7 */ 3,
19643                        /* 8 */ 1, /* 9 */ 2, /* a */ 2, /* b */ 3,
19644                        /* c */ 2, /* d */ 3, /* e */ 3, /* f */ 4};
19645
19646   int NumByteElts = VecSize / 8;
19647   MVT ByteVecVT = MVT::getVectorVT(MVT::i8, NumByteElts);
19648   SDValue In = DAG.getBitcast(ByteVecVT, Op);
19649   SmallVector<SDValue, 16> LUTVec;
19650   for (int i = 0; i < NumByteElts; ++i)
19651     LUTVec.push_back(DAG.getConstant(LUT[i % 16], DL, MVT::i8));
19652   SDValue InRegLUT = DAG.getNode(ISD::BUILD_VECTOR, DL, ByteVecVT, LUTVec);
19653   SmallVector<SDValue, 16> Mask0F(NumByteElts,
19654                                   DAG.getConstant(0x0F, DL, MVT::i8));
19655   SDValue M0F = DAG.getNode(ISD::BUILD_VECTOR, DL, ByteVecVT, Mask0F);
19656
19657   // High nibbles
19658   SmallVector<SDValue, 16> Four(NumByteElts, DAG.getConstant(4, DL, MVT::i8));
19659   SDValue FourV = DAG.getNode(ISD::BUILD_VECTOR, DL, ByteVecVT, Four);
19660   SDValue HighNibbles = DAG.getNode(ISD::SRL, DL, ByteVecVT, In, FourV);
19661
19662   // Low nibbles
19663   SDValue LowNibbles = DAG.getNode(ISD::AND, DL, ByteVecVT, In, M0F);
19664
19665   // The input vector is used as the shuffle mask that index elements into the
19666   // LUT. After counting low and high nibbles, add the vector to obtain the
19667   // final pop count per i8 element.
19668   SDValue HighPopCnt =
19669       DAG.getNode(X86ISD::PSHUFB, DL, ByteVecVT, InRegLUT, HighNibbles);
19670   SDValue LowPopCnt =
19671       DAG.getNode(X86ISD::PSHUFB, DL, ByteVecVT, InRegLUT, LowNibbles);
19672   SDValue PopCnt = DAG.getNode(ISD::ADD, DL, ByteVecVT, HighPopCnt, LowPopCnt);
19673
19674   if (EltVT == MVT::i8)
19675     return PopCnt;
19676
19677   return LowerHorizontalByteSum(PopCnt, VT, Subtarget, DAG);
19678 }
19679
19680 static SDValue LowerVectorCTPOPBitmath(SDValue Op, SDLoc DL,
19681                                        const X86Subtarget *Subtarget,
19682                                        SelectionDAG &DAG) {
19683   MVT VT = Op.getSimpleValueType();
19684   assert(VT.is128BitVector() &&
19685          "Only 128-bit vector bitmath lowering supported.");
19686
19687   int VecSize = VT.getSizeInBits();
19688   MVT EltVT = VT.getVectorElementType();
19689   int Len = EltVT.getSizeInBits();
19690
19691   // This is the vectorized version of the "best" algorithm from
19692   // http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
19693   // with a minor tweak to use a series of adds + shifts instead of vector
19694   // multiplications. Implemented for all integer vector types. We only use
19695   // this when we don't have SSSE3 which allows a LUT-based lowering that is
19696   // much faster, even faster than using native popcnt instructions.
19697
19698   auto GetShift = [&](unsigned OpCode, SDValue V, int Shifter) {
19699     MVT VT = V.getSimpleValueType();
19700     SmallVector<SDValue, 32> Shifters(
19701         VT.getVectorNumElements(),
19702         DAG.getConstant(Shifter, DL, VT.getVectorElementType()));
19703     return DAG.getNode(OpCode, DL, VT, V,
19704                        DAG.getNode(ISD::BUILD_VECTOR, DL, VT, Shifters));
19705   };
19706   auto GetMask = [&](SDValue V, APInt Mask) {
19707     MVT VT = V.getSimpleValueType();
19708     SmallVector<SDValue, 32> Masks(
19709         VT.getVectorNumElements(),
19710         DAG.getConstant(Mask, DL, VT.getVectorElementType()));
19711     return DAG.getNode(ISD::AND, DL, VT, V,
19712                        DAG.getNode(ISD::BUILD_VECTOR, DL, VT, Masks));
19713   };
19714
19715   // We don't want to incur the implicit masks required to SRL vNi8 vectors on
19716   // x86, so set the SRL type to have elements at least i16 wide. This is
19717   // correct because all of our SRLs are followed immediately by a mask anyways
19718   // that handles any bits that sneak into the high bits of the byte elements.
19719   MVT SrlVT = Len > 8 ? VT : MVT::getVectorVT(MVT::i16, VecSize / 16);
19720
19721   SDValue V = Op;
19722
19723   // v = v - ((v >> 1) & 0x55555555...)
19724   SDValue Srl =
19725       DAG.getBitcast(VT, GetShift(ISD::SRL, DAG.getBitcast(SrlVT, V), 1));
19726   SDValue And = GetMask(Srl, APInt::getSplat(Len, APInt(8, 0x55)));
19727   V = DAG.getNode(ISD::SUB, DL, VT, V, And);
19728
19729   // v = (v & 0x33333333...) + ((v >> 2) & 0x33333333...)
19730   SDValue AndLHS = GetMask(V, APInt::getSplat(Len, APInt(8, 0x33)));
19731   Srl = DAG.getBitcast(VT, GetShift(ISD::SRL, DAG.getBitcast(SrlVT, V), 2));
19732   SDValue AndRHS = GetMask(Srl, APInt::getSplat(Len, APInt(8, 0x33)));
19733   V = DAG.getNode(ISD::ADD, DL, VT, AndLHS, AndRHS);
19734
19735   // v = (v + (v >> 4)) & 0x0F0F0F0F...
19736   Srl = DAG.getBitcast(VT, GetShift(ISD::SRL, DAG.getBitcast(SrlVT, V), 4));
19737   SDValue Add = DAG.getNode(ISD::ADD, DL, VT, V, Srl);
19738   V = GetMask(Add, APInt::getSplat(Len, APInt(8, 0x0F)));
19739
19740   // At this point, V contains the byte-wise population count, and we are
19741   // merely doing a horizontal sum if necessary to get the wider element
19742   // counts.
19743   if (EltVT == MVT::i8)
19744     return V;
19745
19746   return LowerHorizontalByteSum(
19747       DAG.getBitcast(MVT::getVectorVT(MVT::i8, VecSize / 8), V), VT, Subtarget,
19748       DAG);
19749 }
19750
19751 static SDValue LowerVectorCTPOP(SDValue Op, const X86Subtarget *Subtarget,
19752                                 SelectionDAG &DAG) {
19753   MVT VT = Op.getSimpleValueType();
19754   // FIXME: Need to add AVX-512 support here!
19755   assert((VT.is256BitVector() || VT.is128BitVector()) &&
19756          "Unknown CTPOP type to handle");
19757   SDLoc DL(Op.getNode());
19758   SDValue Op0 = Op.getOperand(0);
19759
19760   if (!Subtarget->hasSSSE3()) {
19761     // We can't use the fast LUT approach, so fall back on vectorized bitmath.
19762     assert(VT.is128BitVector() && "Only 128-bit vectors supported in SSE!");
19763     return LowerVectorCTPOPBitmath(Op0, DL, Subtarget, DAG);
19764   }
19765
19766   if (VT.is256BitVector() && !Subtarget->hasInt256()) {
19767     unsigned NumElems = VT.getVectorNumElements();
19768
19769     // Extract each 128-bit vector, compute pop count and concat the result.
19770     SDValue LHS = Extract128BitVector(Op0, 0, DAG, DL);
19771     SDValue RHS = Extract128BitVector(Op0, NumElems/2, DAG, DL);
19772
19773     return DAG.getNode(ISD::CONCAT_VECTORS, DL, VT,
19774                        LowerVectorCTPOPInRegLUT(LHS, DL, Subtarget, DAG),
19775                        LowerVectorCTPOPInRegLUT(RHS, DL, Subtarget, DAG));
19776   }
19777
19778   return LowerVectorCTPOPInRegLUT(Op0, DL, Subtarget, DAG);
19779 }
19780
19781 static SDValue LowerCTPOP(SDValue Op, const X86Subtarget *Subtarget,
19782                           SelectionDAG &DAG) {
19783   assert(Op.getSimpleValueType().isVector() &&
19784          "We only do custom lowering for vector population count.");
19785   return LowerVectorCTPOP(Op, Subtarget, DAG);
19786 }
19787
19788 static SDValue LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
19789   SDNode *Node = Op.getNode();
19790   SDLoc dl(Node);
19791   EVT T = Node->getValueType(0);
19792   SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
19793                               DAG.getConstant(0, dl, T), Node->getOperand(2));
19794   return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
19795                        cast<AtomicSDNode>(Node)->getMemoryVT(),
19796                        Node->getOperand(0),
19797                        Node->getOperand(1), negOp,
19798                        cast<AtomicSDNode>(Node)->getMemOperand(),
19799                        cast<AtomicSDNode>(Node)->getOrdering(),
19800                        cast<AtomicSDNode>(Node)->getSynchScope());
19801 }
19802
19803 static SDValue LowerATOMIC_STORE(SDValue Op, SelectionDAG &DAG) {
19804   SDNode *Node = Op.getNode();
19805   SDLoc dl(Node);
19806   EVT VT = cast<AtomicSDNode>(Node)->getMemoryVT();
19807
19808   // Convert seq_cst store -> xchg
19809   // Convert wide store -> swap (-> cmpxchg8b/cmpxchg16b)
19810   // FIXME: On 32-bit, store -> fist or movq would be more efficient
19811   //        (The only way to get a 16-byte store is cmpxchg16b)
19812   // FIXME: 16-byte ATOMIC_SWAP isn't actually hooked up at the moment.
19813   if (cast<AtomicSDNode>(Node)->getOrdering() == SequentiallyConsistent ||
19814       !DAG.getTargetLoweringInfo().isTypeLegal(VT)) {
19815     SDValue Swap = DAG.getAtomic(ISD::ATOMIC_SWAP, dl,
19816                                  cast<AtomicSDNode>(Node)->getMemoryVT(),
19817                                  Node->getOperand(0),
19818                                  Node->getOperand(1), Node->getOperand(2),
19819                                  cast<AtomicSDNode>(Node)->getMemOperand(),
19820                                  cast<AtomicSDNode>(Node)->getOrdering(),
19821                                  cast<AtomicSDNode>(Node)->getSynchScope());
19822     return Swap.getValue(1);
19823   }
19824   // Other atomic stores have a simple pattern.
19825   return Op;
19826 }
19827
19828 static SDValue LowerADDC_ADDE_SUBC_SUBE(SDValue Op, SelectionDAG &DAG) {
19829   MVT VT = Op.getNode()->getSimpleValueType(0);
19830
19831   // Let legalize expand this if it isn't a legal type yet.
19832   if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
19833     return SDValue();
19834
19835   SDVTList VTs = DAG.getVTList(VT, MVT::i32);
19836
19837   unsigned Opc;
19838   bool ExtraOp = false;
19839   switch (Op.getOpcode()) {
19840   default: llvm_unreachable("Invalid code");
19841   case ISD::ADDC: Opc = X86ISD::ADD; break;
19842   case ISD::ADDE: Opc = X86ISD::ADC; ExtraOp = true; break;
19843   case ISD::SUBC: Opc = X86ISD::SUB; break;
19844   case ISD::SUBE: Opc = X86ISD::SBB; ExtraOp = true; break;
19845   }
19846
19847   if (!ExtraOp)
19848     return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
19849                        Op.getOperand(1));
19850   return DAG.getNode(Opc, SDLoc(Op), VTs, Op.getOperand(0),
19851                      Op.getOperand(1), Op.getOperand(2));
19852 }
19853
19854 static SDValue LowerFSINCOS(SDValue Op, const X86Subtarget *Subtarget,
19855                             SelectionDAG &DAG) {
19856   assert(Subtarget->isTargetDarwin() && Subtarget->is64Bit());
19857
19858   // For MacOSX, we want to call an alternative entry point: __sincos_stret,
19859   // which returns the values as { float, float } (in XMM0) or
19860   // { double, double } (which is returned in XMM0, XMM1).
19861   SDLoc dl(Op);
19862   SDValue Arg = Op.getOperand(0);
19863   EVT ArgVT = Arg.getValueType();
19864   Type *ArgTy = ArgVT.getTypeForEVT(*DAG.getContext());
19865
19866   TargetLowering::ArgListTy Args;
19867   TargetLowering::ArgListEntry Entry;
19868
19869   Entry.Node = Arg;
19870   Entry.Ty = ArgTy;
19871   Entry.isSExt = false;
19872   Entry.isZExt = false;
19873   Args.push_back(Entry);
19874
19875   bool isF64 = ArgVT == MVT::f64;
19876   // Only optimize x86_64 for now. i386 is a bit messy. For f32,
19877   // the small struct {f32, f32} is returned in (eax, edx). For f64,
19878   // the results are returned via SRet in memory.
19879   const char *LibcallName =  isF64 ? "__sincos_stret" : "__sincosf_stret";
19880   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
19881   SDValue Callee =
19882       DAG.getExternalSymbol(LibcallName, TLI.getPointerTy(DAG.getDataLayout()));
19883
19884   Type *RetTy = isF64
19885     ? (Type*)StructType::get(ArgTy, ArgTy, nullptr)
19886     : (Type*)VectorType::get(ArgTy, 4);
19887
19888   TargetLowering::CallLoweringInfo CLI(DAG);
19889   CLI.setDebugLoc(dl).setChain(DAG.getEntryNode())
19890     .setCallee(CallingConv::C, RetTy, Callee, std::move(Args), 0);
19891
19892   std::pair<SDValue, SDValue> CallResult = TLI.LowerCallTo(CLI);
19893
19894   if (isF64)
19895     // Returned in xmm0 and xmm1.
19896     return CallResult.first;
19897
19898   // Returned in bits 0:31 and 32:64 xmm0.
19899   SDValue SinVal = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, ArgVT,
19900                                CallResult.first, DAG.getIntPtrConstant(0, dl));
19901   SDValue CosVal = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, ArgVT,
19902                                CallResult.first, DAG.getIntPtrConstant(1, dl));
19903   SDVTList Tys = DAG.getVTList(ArgVT, ArgVT);
19904   return DAG.getNode(ISD::MERGE_VALUES, dl, Tys, SinVal, CosVal);
19905 }
19906
19907 /// Widen a vector input to a vector of NVT.  The
19908 /// input vector must have the same element type as NVT.
19909 static SDValue ExtendToType(SDValue InOp, MVT NVT, SelectionDAG &DAG,
19910                             bool FillWithZeroes = false) {
19911   // Check if InOp already has the right width.
19912   MVT InVT = InOp.getSimpleValueType();
19913   if (InVT == NVT)
19914     return InOp;
19915
19916   if (InOp.isUndef())
19917     return DAG.getUNDEF(NVT);
19918
19919   assert(InVT.getVectorElementType() == NVT.getVectorElementType() &&
19920          "input and widen element type must match");
19921
19922   unsigned InNumElts = InVT.getVectorNumElements();
19923   unsigned WidenNumElts = NVT.getVectorNumElements();
19924   assert(WidenNumElts > InNumElts && WidenNumElts % InNumElts == 0 &&
19925          "Unexpected request for vector widening");
19926
19927   EVT EltVT = NVT.getVectorElementType();
19928
19929   SDLoc dl(InOp);
19930   if (InOp.getOpcode() == ISD::CONCAT_VECTORS &&
19931       InOp.getNumOperands() == 2) {
19932     SDValue N1 = InOp.getOperand(1);
19933     if ((ISD::isBuildVectorAllZeros(N1.getNode()) && FillWithZeroes) ||
19934         N1.isUndef()) {
19935       InOp = InOp.getOperand(0);
19936       InVT = InOp.getSimpleValueType();
19937       InNumElts = InVT.getVectorNumElements();
19938     }
19939   }
19940   if (ISD::isBuildVectorOfConstantSDNodes(InOp.getNode()) ||
19941       ISD::isBuildVectorOfConstantFPSDNodes(InOp.getNode())) {
19942     SmallVector<SDValue, 16> Ops;
19943     for (unsigned i = 0; i < InNumElts; ++i)
19944       Ops.push_back(InOp.getOperand(i));
19945
19946     SDValue FillVal = FillWithZeroes ? DAG.getConstant(0, dl, EltVT) :
19947       DAG.getUNDEF(EltVT);
19948     for (unsigned i = 0; i < WidenNumElts - InNumElts; ++i)
19949       Ops.push_back(FillVal);
19950     return DAG.getNode(ISD::BUILD_VECTOR, dl, NVT, Ops);
19951   }
19952   SDValue FillVal = FillWithZeroes ? DAG.getConstant(0, dl, NVT) :
19953     DAG.getUNDEF(NVT);
19954   return DAG.getNode(ISD::INSERT_SUBVECTOR, dl, NVT, FillVal,
19955                      InOp, DAG.getIntPtrConstant(0, dl));
19956 }
19957
19958 static SDValue LowerMSCATTER(SDValue Op, const X86Subtarget *Subtarget,
19959                              SelectionDAG &DAG) {
19960   assert(Subtarget->hasAVX512() &&
19961          "MGATHER/MSCATTER are supported on AVX-512 arch only");
19962
19963   // X86 scatter kills mask register, so its type should be added to
19964   // the list of return values.
19965   // If the "scatter" has 2 return values, it is already handled.
19966   if (Op.getNode()->getNumValues() == 2)
19967     return Op;
19968
19969   MaskedScatterSDNode *N = cast<MaskedScatterSDNode>(Op.getNode());
19970   SDValue Src = N->getValue();
19971   MVT VT = Src.getSimpleValueType();
19972   assert(VT.getScalarSizeInBits() >= 32 && "Unsupported scatter op");
19973   SDLoc dl(Op);
19974
19975   SDValue NewScatter;
19976   SDValue Index = N->getIndex();
19977   SDValue Mask = N->getMask();
19978   SDValue Chain = N->getChain();
19979   SDValue BasePtr = N->getBasePtr();
19980   MVT MemVT = N->getMemoryVT().getSimpleVT();
19981   MVT IndexVT = Index.getSimpleValueType();
19982   MVT MaskVT = Mask.getSimpleValueType();
19983
19984   if (MemVT.getScalarSizeInBits() < VT.getScalarSizeInBits()) {
19985     // The v2i32 value was promoted to v2i64.
19986     // Now we "redo" the type legalizer's work and widen the original
19987     // v2i32 value to v4i32. The original v2i32 is retrieved from v2i64
19988     // with a shuffle.
19989     assert((MemVT == MVT::v2i32 && VT == MVT::v2i64) &&
19990            "Unexpected memory type");
19991     int ShuffleMask[] = {0, 2, -1, -1};
19992     Src = DAG.getVectorShuffle(MVT::v4i32, dl, DAG.getBitcast(MVT::v4i32, Src),
19993                                DAG.getUNDEF(MVT::v4i32), ShuffleMask);
19994     // Now we have 4 elements instead of 2.
19995     // Expand the index.
19996     MVT NewIndexVT = MVT::getVectorVT(IndexVT.getScalarType(), 4);
19997     Index = ExtendToType(Index, NewIndexVT, DAG);
19998
19999     // Expand the mask with zeroes
20000     // Mask may be <2 x i64> or <2 x i1> at this moment
20001     assert((MaskVT == MVT::v2i1 || MaskVT == MVT::v2i64) &&
20002            "Unexpected mask type");
20003     MVT ExtMaskVT = MVT::getVectorVT(MaskVT.getScalarType(), 4);
20004     Mask = ExtendToType(Mask, ExtMaskVT, DAG, true);
20005     VT = MVT::v4i32;
20006   }
20007
20008   unsigned NumElts = VT.getVectorNumElements();
20009   if (!Subtarget->hasVLX() && !VT.is512BitVector() &&
20010       !Index.getSimpleValueType().is512BitVector()) {
20011     // AVX512F supports only 512-bit vectors. Or data or index should
20012     // be 512 bit wide. If now the both index and data are 256-bit, but
20013     // the vector contains 8 elements, we just sign-extend the index
20014     if (IndexVT == MVT::v8i32)
20015       // Just extend index
20016       Index = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i64, Index);
20017     else {
20018       // The minimal number of elts in scatter is 8
20019       NumElts = 8;
20020       // Index
20021       MVT NewIndexVT = MVT::getVectorVT(IndexVT.getScalarType(), NumElts);
20022       // Use original index here, do not modify the index twice
20023       Index = ExtendToType(N->getIndex(), NewIndexVT, DAG);
20024       if (IndexVT.getScalarType() == MVT::i32)
20025         Index = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i64, Index);
20026
20027       // Mask
20028       // At this point we have promoted mask operand
20029       assert(MaskVT.getScalarSizeInBits() >= 32 && "unexpected mask type");
20030       MVT ExtMaskVT = MVT::getVectorVT(MaskVT.getScalarType(), NumElts);
20031       // Use the original mask here, do not modify the mask twice
20032       Mask = ExtendToType(N->getMask(), ExtMaskVT, DAG, true);
20033
20034       // The value that should be stored
20035       MVT NewVT = MVT::getVectorVT(VT.getScalarType(), NumElts);
20036       Src = ExtendToType(Src, NewVT, DAG);
20037     }
20038   }
20039   // If the mask is "wide" at this point - truncate it to i1 vector
20040   MVT BitMaskVT = MVT::getVectorVT(MVT::i1, NumElts);
20041   Mask = DAG.getNode(ISD::TRUNCATE, dl, BitMaskVT, Mask);
20042
20043   // The mask is killed by scatter, add it to the values
20044   SDVTList VTs = DAG.getVTList(BitMaskVT, MVT::Other);
20045   SDValue Ops[] = {Chain, Src, Mask, BasePtr, Index};
20046   NewScatter = DAG.getMaskedScatter(VTs, N->getMemoryVT(), dl, Ops,
20047                                     N->getMemOperand());
20048   DAG.ReplaceAllUsesWith(Op, SDValue(NewScatter.getNode(), 1));
20049   return SDValue(NewScatter.getNode(), 0);
20050 }
20051
20052 static SDValue LowerMLOAD(SDValue Op, const X86Subtarget *Subtarget,
20053                           SelectionDAG &DAG) {
20054
20055   MaskedLoadSDNode *N = cast<MaskedLoadSDNode>(Op.getNode());
20056   MVT VT = Op.getSimpleValueType();
20057   SDValue Mask = N->getMask();
20058   SDLoc dl(Op);
20059
20060   if (Subtarget->hasAVX512() && !Subtarget->hasVLX() &&
20061       !VT.is512BitVector() && Mask.getValueType() == MVT::v8i1) {
20062     // This operation is legal for targets with VLX, but without
20063     // VLX the vector should be widened to 512 bit
20064     unsigned NumEltsInWideVec = 512/VT.getScalarSizeInBits();
20065     MVT WideDataVT = MVT::getVectorVT(VT.getScalarType(), NumEltsInWideVec);
20066     MVT WideMaskVT = MVT::getVectorVT(MVT::i1, NumEltsInWideVec);
20067     SDValue Src0 = N->getSrc0();
20068     Src0 = ExtendToType(Src0, WideDataVT, DAG);
20069     Mask = ExtendToType(Mask, WideMaskVT, DAG, true);
20070     SDValue NewLoad = DAG.getMaskedLoad(WideDataVT, dl, N->getChain(),
20071                                         N->getBasePtr(), Mask, Src0,
20072                                         N->getMemoryVT(), N->getMemOperand(),
20073                                         N->getExtensionType());
20074
20075     SDValue Exract = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
20076                                  NewLoad.getValue(0),
20077                                  DAG.getIntPtrConstant(0, dl));
20078     SDValue RetOps[] = {Exract, NewLoad.getValue(1)};
20079     return DAG.getMergeValues(RetOps, dl);
20080   }
20081   return Op;
20082 }
20083
20084 static SDValue LowerMSTORE(SDValue Op, const X86Subtarget *Subtarget,
20085                            SelectionDAG &DAG) {
20086   MaskedStoreSDNode *N = cast<MaskedStoreSDNode>(Op.getNode());
20087   SDValue DataToStore = N->getValue();
20088   MVT VT = DataToStore.getSimpleValueType();
20089   SDValue Mask = N->getMask();
20090   SDLoc dl(Op);
20091
20092   if (Subtarget->hasAVX512() && !Subtarget->hasVLX() &&
20093       !VT.is512BitVector() && Mask.getValueType() == MVT::v8i1) {
20094     // This operation is legal for targets with VLX, but without
20095     // VLX the vector should be widened to 512 bit
20096     unsigned NumEltsInWideVec = 512/VT.getScalarSizeInBits();
20097     MVT WideDataVT = MVT::getVectorVT(VT.getScalarType(), NumEltsInWideVec);
20098     MVT WideMaskVT = MVT::getVectorVT(MVT::i1, NumEltsInWideVec);
20099     DataToStore = ExtendToType(DataToStore, WideDataVT, DAG);
20100     Mask = ExtendToType(Mask, WideMaskVT, DAG, true);
20101     return DAG.getMaskedStore(N->getChain(), dl, DataToStore, N->getBasePtr(),
20102                               Mask, N->getMemoryVT(), N->getMemOperand(),
20103                               N->isTruncatingStore());
20104   }
20105   return Op;
20106 }
20107
20108 static SDValue LowerMGATHER(SDValue Op, const X86Subtarget *Subtarget,
20109                             SelectionDAG &DAG) {
20110   assert(Subtarget->hasAVX512() &&
20111          "MGATHER/MSCATTER are supported on AVX-512 arch only");
20112
20113   MaskedGatherSDNode *N = cast<MaskedGatherSDNode>(Op.getNode());
20114   SDLoc dl(Op);
20115   MVT VT = Op.getSimpleValueType();
20116   SDValue Index = N->getIndex();
20117   SDValue Mask = N->getMask();
20118   SDValue Src0 = N->getValue();
20119   MVT IndexVT = Index.getSimpleValueType();
20120   MVT MaskVT = Mask.getSimpleValueType();
20121
20122   unsigned NumElts = VT.getVectorNumElements();
20123   assert(VT.getScalarSizeInBits() >= 32 && "Unsupported gather op");
20124
20125   if (!Subtarget->hasVLX() && !VT.is512BitVector() &&
20126       !Index.getSimpleValueType().is512BitVector()) {
20127     // AVX512F supports only 512-bit vectors. Or data or index should
20128     // be 512 bit wide. If now the both index and data are 256-bit, but
20129     // the vector contains 8 elements, we just sign-extend the index
20130     if (NumElts == 8) {
20131       Index = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i64, Index);
20132       SDValue Ops[] = { N->getOperand(0), N->getOperand(1),  N->getOperand(2),
20133                         N->getOperand(3), Index };
20134       DAG.UpdateNodeOperands(N, Ops);
20135       return Op;
20136     }
20137
20138     // Minimal number of elements in Gather
20139     NumElts = 8;
20140     // Index
20141     MVT NewIndexVT = MVT::getVectorVT(IndexVT.getScalarType(), NumElts);
20142     Index = ExtendToType(Index, NewIndexVT, DAG);
20143     if (IndexVT.getScalarType() == MVT::i32)
20144       Index = DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v8i64, Index);
20145
20146     // Mask
20147     MVT MaskBitVT = MVT::getVectorVT(MVT::i1, NumElts);
20148     // At this point we have promoted mask operand
20149     assert(MaskVT.getScalarSizeInBits() >= 32 && "unexpected mask type");
20150     MVT ExtMaskVT = MVT::getVectorVT(MaskVT.getScalarType(), NumElts);
20151     Mask = ExtendToType(Mask, ExtMaskVT, DAG, true);
20152     Mask = DAG.getNode(ISD::TRUNCATE, dl, MaskBitVT, Mask);
20153
20154     // The pass-thru value
20155     MVT NewVT = MVT::getVectorVT(VT.getScalarType(), NumElts);
20156     Src0 = ExtendToType(Src0, NewVT, DAG);
20157
20158     SDValue Ops[] = { N->getChain(), Src0, Mask, N->getBasePtr(), Index };
20159     SDValue NewGather = DAG.getMaskedGather(DAG.getVTList(NewVT, MVT::Other),
20160                                             N->getMemoryVT(), dl, Ops,
20161                                             N->getMemOperand());
20162     SDValue Exract = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, VT,
20163                                  NewGather.getValue(0),
20164                                  DAG.getIntPtrConstant(0, dl));
20165     SDValue RetOps[] = {Exract, NewGather.getValue(1)};
20166     return DAG.getMergeValues(RetOps, dl);
20167   }
20168   return Op;
20169 }
20170
20171 SDValue X86TargetLowering::LowerGC_TRANSITION_START(SDValue Op,
20172                                                     SelectionDAG &DAG) const {
20173   // TODO: Eventually, the lowering of these nodes should be informed by or
20174   // deferred to the GC strategy for the function in which they appear. For
20175   // now, however, they must be lowered to something. Since they are logically
20176   // no-ops in the case of a null GC strategy (or a GC strategy which does not
20177   // require special handling for these nodes), lower them as literal NOOPs for
20178   // the time being.
20179   SmallVector<SDValue, 2> Ops;
20180
20181   Ops.push_back(Op.getOperand(0));
20182   if (Op->getGluedNode())
20183     Ops.push_back(Op->getOperand(Op->getNumOperands() - 1));
20184
20185   SDLoc OpDL(Op);
20186   SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
20187   SDValue NOOP(DAG.getMachineNode(X86::NOOP, SDLoc(Op), VTs, Ops), 0);
20188
20189   return NOOP;
20190 }
20191
20192 SDValue X86TargetLowering::LowerGC_TRANSITION_END(SDValue Op,
20193                                                   SelectionDAG &DAG) const {
20194   // TODO: Eventually, the lowering of these nodes should be informed by or
20195   // deferred to the GC strategy for the function in which they appear. For
20196   // now, however, they must be lowered to something. Since they are logically
20197   // no-ops in the case of a null GC strategy (or a GC strategy which does not
20198   // require special handling for these nodes), lower them as literal NOOPs for
20199   // the time being.
20200   SmallVector<SDValue, 2> Ops;
20201
20202   Ops.push_back(Op.getOperand(0));
20203   if (Op->getGluedNode())
20204     Ops.push_back(Op->getOperand(Op->getNumOperands() - 1));
20205
20206   SDLoc OpDL(Op);
20207   SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
20208   SDValue NOOP(DAG.getMachineNode(X86::NOOP, SDLoc(Op), VTs, Ops), 0);
20209
20210   return NOOP;
20211 }
20212
20213 /// LowerOperation - Provide custom lowering hooks for some operations.
20214 ///
20215 SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
20216   switch (Op.getOpcode()) {
20217   default: llvm_unreachable("Should not custom lower this!");
20218   case ISD::ATOMIC_FENCE:       return LowerATOMIC_FENCE(Op, Subtarget, DAG);
20219   case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
20220     return LowerCMP_SWAP(Op, Subtarget, DAG);
20221   case ISD::CTPOP:              return LowerCTPOP(Op, Subtarget, DAG);
20222   case ISD::ATOMIC_LOAD_SUB:    return LowerLOAD_SUB(Op,DAG);
20223   case ISD::ATOMIC_STORE:       return LowerATOMIC_STORE(Op,DAG);
20224   case ISD::BUILD_VECTOR:       return LowerBUILD_VECTOR(Op, DAG);
20225   case ISD::CONCAT_VECTORS:     return LowerCONCAT_VECTORS(Op, Subtarget, DAG);
20226   case ISD::VECTOR_SHUFFLE:     return lowerVectorShuffle(Op, Subtarget, DAG);
20227   case ISD::VSELECT:            return LowerVSELECT(Op, DAG);
20228   case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
20229   case ISD::INSERT_VECTOR_ELT:  return LowerINSERT_VECTOR_ELT(Op, DAG);
20230   case ISD::EXTRACT_SUBVECTOR:  return LowerEXTRACT_SUBVECTOR(Op,Subtarget,DAG);
20231   case ISD::INSERT_SUBVECTOR:   return LowerINSERT_SUBVECTOR(Op, Subtarget,DAG);
20232   case ISD::SCALAR_TO_VECTOR:   return LowerSCALAR_TO_VECTOR(Op, DAG);
20233   case ISD::ConstantPool:       return LowerConstantPool(Op, DAG);
20234   case ISD::GlobalAddress:      return LowerGlobalAddress(Op, DAG);
20235   case ISD::GlobalTLSAddress:   return LowerGlobalTLSAddress(Op, DAG);
20236   case ISD::ExternalSymbol:     return LowerExternalSymbol(Op, DAG);
20237   case ISD::BlockAddress:       return LowerBlockAddress(Op, DAG);
20238   case ISD::SHL_PARTS:
20239   case ISD::SRA_PARTS:
20240   case ISD::SRL_PARTS:          return LowerShiftParts(Op, DAG);
20241   case ISD::SINT_TO_FP:         return LowerSINT_TO_FP(Op, DAG);
20242   case ISD::UINT_TO_FP:         return LowerUINT_TO_FP(Op, DAG);
20243   case ISD::TRUNCATE:           return LowerTRUNCATE(Op, DAG);
20244   case ISD::ZERO_EXTEND:        return LowerZERO_EXTEND(Op, Subtarget, DAG);
20245   case ISD::SIGN_EXTEND:        return LowerSIGN_EXTEND(Op, Subtarget, DAG);
20246   case ISD::ANY_EXTEND:         return LowerANY_EXTEND(Op, Subtarget, DAG);
20247   case ISD::SIGN_EXTEND_VECTOR_INREG:
20248     return LowerSIGN_EXTEND_VECTOR_INREG(Op, Subtarget, DAG);
20249   case ISD::FP_TO_SINT:         return LowerFP_TO_SINT(Op, DAG);
20250   case ISD::FP_TO_UINT:         return LowerFP_TO_UINT(Op, DAG);
20251   case ISD::FP_EXTEND:          return LowerFP_EXTEND(Op, DAG);
20252   case ISD::LOAD:               return LowerExtendedLoad(Op, Subtarget, DAG);
20253   case ISD::FABS:
20254   case ISD::FNEG:               return LowerFABSorFNEG(Op, DAG);
20255   case ISD::FCOPYSIGN:          return LowerFCOPYSIGN(Op, DAG);
20256   case ISD::FGETSIGN:           return LowerFGETSIGN(Op, DAG);
20257   case ISD::SETCC:              return LowerSETCC(Op, DAG);
20258   case ISD::SETCCE:             return LowerSETCCE(Op, DAG);
20259   case ISD::SELECT:             return LowerSELECT(Op, DAG);
20260   case ISD::BRCOND:             return LowerBRCOND(Op, DAG);
20261   case ISD::JumpTable:          return LowerJumpTable(Op, DAG);
20262   case ISD::VASTART:            return LowerVASTART(Op, DAG);
20263   case ISD::VAARG:              return LowerVAARG(Op, DAG);
20264   case ISD::VACOPY:             return LowerVACOPY(Op, Subtarget, DAG);
20265   case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, Subtarget, DAG);
20266   case ISD::INTRINSIC_VOID:
20267   case ISD::INTRINSIC_W_CHAIN:  return LowerINTRINSIC_W_CHAIN(Op, Subtarget, DAG);
20268   case ISD::RETURNADDR:         return LowerRETURNADDR(Op, DAG);
20269   case ISD::FRAMEADDR:          return LowerFRAMEADDR(Op, DAG);
20270   case ISD::FRAME_TO_ARGS_OFFSET:
20271                                 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
20272   case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
20273   case ISD::EH_RETURN:          return LowerEH_RETURN(Op, DAG);
20274   case ISD::EH_SJLJ_SETJMP:     return lowerEH_SJLJ_SETJMP(Op, DAG);
20275   case ISD::EH_SJLJ_LONGJMP:    return lowerEH_SJLJ_LONGJMP(Op, DAG);
20276   case ISD::INIT_TRAMPOLINE:    return LowerINIT_TRAMPOLINE(Op, DAG);
20277   case ISD::ADJUST_TRAMPOLINE:  return LowerADJUST_TRAMPOLINE(Op, DAG);
20278   case ISD::FLT_ROUNDS_:        return LowerFLT_ROUNDS_(Op, DAG);
20279   case ISD::CTLZ:               return LowerCTLZ(Op, Subtarget, DAG);
20280   case ISD::CTLZ_ZERO_UNDEF:    return LowerCTLZ_ZERO_UNDEF(Op, Subtarget, DAG);
20281   case ISD::CTTZ:
20282   case ISD::CTTZ_ZERO_UNDEF:    return LowerCTTZ(Op, DAG);
20283   case ISD::MUL:                return LowerMUL(Op, Subtarget, DAG);
20284   case ISD::UMUL_LOHI:
20285   case ISD::SMUL_LOHI:          return LowerMUL_LOHI(Op, Subtarget, DAG);
20286   case ISD::ROTL:               return LowerRotate(Op, Subtarget, DAG);
20287   case ISD::SRA:
20288   case ISD::SRL:
20289   case ISD::SHL:                return LowerShift(Op, Subtarget, DAG);
20290   case ISD::SADDO:
20291   case ISD::UADDO:
20292   case ISD::SSUBO:
20293   case ISD::USUBO:
20294   case ISD::SMULO:
20295   case ISD::UMULO:              return LowerXALUO(Op, DAG);
20296   case ISD::READCYCLECOUNTER:   return LowerREADCYCLECOUNTER(Op, Subtarget,DAG);
20297   case ISD::BITCAST:            return LowerBITCAST(Op, Subtarget, DAG);
20298   case ISD::ADDC:
20299   case ISD::ADDE:
20300   case ISD::SUBC:
20301   case ISD::SUBE:               return LowerADDC_ADDE_SUBC_SUBE(Op, DAG);
20302   case ISD::ADD:                return LowerADD(Op, DAG);
20303   case ISD::SUB:                return LowerSUB(Op, DAG);
20304   case ISD::SMAX:
20305   case ISD::SMIN:
20306   case ISD::UMAX:
20307   case ISD::UMIN:               return LowerMINMAX(Op, DAG);
20308   case ISD::FSINCOS:            return LowerFSINCOS(Op, Subtarget, DAG);
20309   case ISD::MLOAD:              return LowerMLOAD(Op, Subtarget, DAG);
20310   case ISD::MSTORE:             return LowerMSTORE(Op, Subtarget, DAG);
20311   case ISD::MGATHER:            return LowerMGATHER(Op, Subtarget, DAG);
20312   case ISD::MSCATTER:           return LowerMSCATTER(Op, Subtarget, DAG);
20313   case ISD::GC_TRANSITION_START:
20314                                 return LowerGC_TRANSITION_START(Op, DAG);
20315   case ISD::GC_TRANSITION_END:  return LowerGC_TRANSITION_END(Op, DAG);
20316   }
20317 }
20318
20319 /// ReplaceNodeResults - Replace a node with an illegal result type
20320 /// with a new node built out of custom code.
20321 void X86TargetLowering::ReplaceNodeResults(SDNode *N,
20322                                            SmallVectorImpl<SDValue>&Results,
20323                                            SelectionDAG &DAG) const {
20324   SDLoc dl(N);
20325   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
20326   switch (N->getOpcode()) {
20327   default:
20328     llvm_unreachable("Do not know how to custom type legalize this operation!");
20329   case X86ISD::AVG: {
20330     // Legalize types for X86ISD::AVG by expanding vectors.
20331     assert(Subtarget->hasSSE2() && "Requires at least SSE2!");
20332
20333     auto InVT = N->getValueType(0);
20334     auto InVTSize = InVT.getSizeInBits();
20335     const unsigned RegSize =
20336         (InVTSize > 128) ? ((InVTSize > 256) ? 512 : 256) : 128;
20337     assert((!Subtarget->hasAVX512() || RegSize < 512) &&
20338            "512-bit vector requires AVX512");
20339     assert((!Subtarget->hasAVX2() || RegSize < 256) &&
20340            "256-bit vector requires AVX2");
20341
20342     auto ElemVT = InVT.getVectorElementType();
20343     auto RegVT = EVT::getVectorVT(*DAG.getContext(), ElemVT,
20344                                   RegSize / ElemVT.getSizeInBits());
20345     assert(RegSize % InVT.getSizeInBits() == 0);
20346     unsigned NumConcat = RegSize / InVT.getSizeInBits();
20347
20348     SmallVector<SDValue, 16> Ops(NumConcat, DAG.getUNDEF(InVT));
20349     Ops[0] = N->getOperand(0);
20350     SDValue InVec0 = DAG.getNode(ISD::CONCAT_VECTORS, dl, RegVT, Ops);
20351     Ops[0] = N->getOperand(1);
20352     SDValue InVec1 = DAG.getNode(ISD::CONCAT_VECTORS, dl, RegVT, Ops);
20353
20354     SDValue Res = DAG.getNode(X86ISD::AVG, dl, RegVT, InVec0, InVec1);
20355     Results.push_back(DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl, InVT, Res,
20356                                   DAG.getIntPtrConstant(0, dl)));
20357     return;
20358   }
20359   // We might have generated v2f32 FMIN/FMAX operations. Widen them to v4f32.
20360   case X86ISD::FMINC:
20361   case X86ISD::FMIN:
20362   case X86ISD::FMAXC:
20363   case X86ISD::FMAX: {
20364     EVT VT = N->getValueType(0);
20365     assert(VT == MVT::v2f32 && "Unexpected type (!= v2f32) on FMIN/FMAX.");
20366     SDValue UNDEF = DAG.getUNDEF(VT);
20367     SDValue LHS = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v4f32,
20368                               N->getOperand(0), UNDEF);
20369     SDValue RHS = DAG.getNode(ISD::CONCAT_VECTORS, dl, MVT::v4f32,
20370                               N->getOperand(1), UNDEF);
20371     Results.push_back(DAG.getNode(N->getOpcode(), dl, MVT::v4f32, LHS, RHS));
20372     return;
20373   }
20374   case ISD::SIGN_EXTEND_INREG:
20375   case ISD::ADDC:
20376   case ISD::ADDE:
20377   case ISD::SUBC:
20378   case ISD::SUBE:
20379     // We don't want to expand or promote these.
20380     return;
20381   case ISD::SDIV:
20382   case ISD::UDIV:
20383   case ISD::SREM:
20384   case ISD::UREM:
20385   case ISD::SDIVREM:
20386   case ISD::UDIVREM: {
20387     SDValue V = LowerWin64_i128OP(SDValue(N,0), DAG);
20388     Results.push_back(V);
20389     return;
20390   }
20391   case ISD::FP_TO_SINT:
20392   case ISD::FP_TO_UINT: {
20393     bool IsSigned = N->getOpcode() == ISD::FP_TO_SINT;
20394
20395     std::pair<SDValue,SDValue> Vals =
20396         FP_TO_INTHelper(SDValue(N, 0), DAG, IsSigned, /*IsReplace=*/ true);
20397     SDValue FIST = Vals.first, StackSlot = Vals.second;
20398     if (FIST.getNode()) {
20399       EVT VT = N->getValueType(0);
20400       // Return a load from the stack slot.
20401       if (StackSlot.getNode())
20402         Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot,
20403                                       MachinePointerInfo(),
20404                                       false, false, false, 0));
20405       else
20406         Results.push_back(FIST);
20407     }
20408     return;
20409   }
20410   case ISD::UINT_TO_FP: {
20411     assert(Subtarget->hasSSE2() && "Requires at least SSE2!");
20412     if (N->getOperand(0).getValueType() != MVT::v2i32 ||
20413         N->getValueType(0) != MVT::v2f32)
20414       return;
20415     SDValue ZExtIn = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::v2i64,
20416                                  N->getOperand(0));
20417     SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL), dl,
20418                                      MVT::f64);
20419     SDValue VBias = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2f64, Bias, Bias);
20420     SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64, ZExtIn,
20421                              DAG.getBitcast(MVT::v2i64, VBias));
20422     Or = DAG.getBitcast(MVT::v2f64, Or);
20423     // TODO: Are there any fast-math-flags to propagate here?
20424     SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, Or, VBias);
20425     Results.push_back(DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, Sub));
20426     return;
20427   }
20428   case ISD::FP_ROUND: {
20429     if (!TLI.isTypeLegal(N->getOperand(0).getValueType()))
20430         return;
20431     SDValue V = DAG.getNode(X86ISD::VFPROUND, dl, MVT::v4f32, N->getOperand(0));
20432     Results.push_back(V);
20433     return;
20434   }
20435   case ISD::FP_EXTEND: {
20436     // Right now, only MVT::v2f32 has OperationAction for FP_EXTEND.
20437     // No other ValueType for FP_EXTEND should reach this point.
20438     assert(N->getValueType(0) == MVT::v2f32 &&
20439            "Do not know how to legalize this Node");
20440     return;
20441   }
20442   case ISD::INTRINSIC_W_CHAIN: {
20443     unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
20444     switch (IntNo) {
20445     default : llvm_unreachable("Do not know how to custom type "
20446                                "legalize this intrinsic operation!");
20447     case Intrinsic::x86_rdtsc:
20448       return getReadTimeStampCounter(N, dl, X86ISD::RDTSC_DAG, DAG, Subtarget,
20449                                      Results);
20450     case Intrinsic::x86_rdtscp:
20451       return getReadTimeStampCounter(N, dl, X86ISD::RDTSCP_DAG, DAG, Subtarget,
20452                                      Results);
20453     case Intrinsic::x86_rdpmc:
20454       return getReadPerformanceCounter(N, dl, DAG, Subtarget, Results);
20455     }
20456   }
20457   case ISD::INTRINSIC_WO_CHAIN: {
20458     if (SDValue V = LowerINTRINSIC_WO_CHAIN(SDValue(N, 0), Subtarget, DAG))
20459       Results.push_back(V);
20460     return;
20461   }
20462   case ISD::READCYCLECOUNTER: {
20463     return getReadTimeStampCounter(N, dl, X86ISD::RDTSC_DAG, DAG, Subtarget,
20464                                    Results);
20465   }
20466   case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: {
20467     EVT T = N->getValueType(0);
20468     assert((T == MVT::i64 || T == MVT::i128) && "can only expand cmpxchg pair");
20469     bool Regs64bit = T == MVT::i128;
20470     MVT HalfT = Regs64bit ? MVT::i64 : MVT::i32;
20471     SDValue cpInL, cpInH;
20472     cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
20473                         DAG.getConstant(0, dl, HalfT));
20474     cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(2),
20475                         DAG.getConstant(1, dl, HalfT));
20476     cpInL = DAG.getCopyToReg(N->getOperand(0), dl,
20477                              Regs64bit ? X86::RAX : X86::EAX,
20478                              cpInL, SDValue());
20479     cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl,
20480                              Regs64bit ? X86::RDX : X86::EDX,
20481                              cpInH, cpInL.getValue(1));
20482     SDValue swapInL, swapInH;
20483     swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
20484                           DAG.getConstant(0, dl, HalfT));
20485     swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, HalfT, N->getOperand(3),
20486                           DAG.getConstant(1, dl, HalfT));
20487     swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl,
20488                                Regs64bit ? X86::RBX : X86::EBX,
20489                                swapInL, cpInH.getValue(1));
20490     swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl,
20491                                Regs64bit ? X86::RCX : X86::ECX,
20492                                swapInH, swapInL.getValue(1));
20493     SDValue Ops[] = { swapInH.getValue(0),
20494                       N->getOperand(1),
20495                       swapInH.getValue(1) };
20496     SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
20497     MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
20498     unsigned Opcode = Regs64bit ? X86ISD::LCMPXCHG16_DAG :
20499                                   X86ISD::LCMPXCHG8_DAG;
20500     SDValue Result = DAG.getMemIntrinsicNode(Opcode, dl, Tys, Ops, T, MMO);
20501     SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl,
20502                                         Regs64bit ? X86::RAX : X86::EAX,
20503                                         HalfT, Result.getValue(1));
20504     SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl,
20505                                         Regs64bit ? X86::RDX : X86::EDX,
20506                                         HalfT, cpOutL.getValue(2));
20507     SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
20508
20509     SDValue EFLAGS = DAG.getCopyFromReg(cpOutH.getValue(1), dl, X86::EFLAGS,
20510                                         MVT::i32, cpOutH.getValue(2));
20511     SDValue Success =
20512         DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
20513                     DAG.getConstant(X86::COND_E, dl, MVT::i8), EFLAGS);
20514     Success = DAG.getZExtOrTrunc(Success, dl, N->getValueType(1));
20515
20516     Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, T, OpsF));
20517     Results.push_back(Success);
20518     Results.push_back(EFLAGS.getValue(1));
20519     return;
20520   }
20521   case ISD::ATOMIC_SWAP:
20522   case ISD::ATOMIC_LOAD_ADD:
20523   case ISD::ATOMIC_LOAD_SUB:
20524   case ISD::ATOMIC_LOAD_AND:
20525   case ISD::ATOMIC_LOAD_OR:
20526   case ISD::ATOMIC_LOAD_XOR:
20527   case ISD::ATOMIC_LOAD_NAND:
20528   case ISD::ATOMIC_LOAD_MIN:
20529   case ISD::ATOMIC_LOAD_MAX:
20530   case ISD::ATOMIC_LOAD_UMIN:
20531   case ISD::ATOMIC_LOAD_UMAX:
20532   case ISD::ATOMIC_LOAD: {
20533     // Delegate to generic TypeLegalization. Situations we can really handle
20534     // should have already been dealt with by AtomicExpandPass.cpp.
20535     break;
20536   }
20537   case ISD::BITCAST: {
20538     assert(Subtarget->hasSSE2() && "Requires at least SSE2!");
20539     EVT DstVT = N->getValueType(0);
20540     EVT SrcVT = N->getOperand(0)->getValueType(0);
20541
20542     if (SrcVT != MVT::f64 ||
20543         (DstVT != MVT::v2i32 && DstVT != MVT::v4i16 && DstVT != MVT::v8i8))
20544       return;
20545
20546     unsigned NumElts = DstVT.getVectorNumElements();
20547     EVT SVT = DstVT.getVectorElementType();
20548     EVT WiderVT = EVT::getVectorVT(*DAG.getContext(), SVT, NumElts * 2);
20549     SDValue Expanded = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
20550                                    MVT::v2f64, N->getOperand(0));
20551     SDValue ToVecInt = DAG.getBitcast(WiderVT, Expanded);
20552
20553     if (ExperimentalVectorWideningLegalization) {
20554       // If we are legalizing vectors by widening, we already have the desired
20555       // legal vector type, just return it.
20556       Results.push_back(ToVecInt);
20557       return;
20558     }
20559
20560     SmallVector<SDValue, 8> Elts;
20561     for (unsigned i = 0, e = NumElts; i != e; ++i)
20562       Elts.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, SVT,
20563                                    ToVecInt, DAG.getIntPtrConstant(i, dl)));
20564
20565     Results.push_back(DAG.getNode(ISD::BUILD_VECTOR, dl, DstVT, Elts));
20566   }
20567   }
20568 }
20569
20570 const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
20571   switch ((X86ISD::NodeType)Opcode) {
20572   case X86ISD::FIRST_NUMBER:       break;
20573   case X86ISD::BSF:                return "X86ISD::BSF";
20574   case X86ISD::BSR:                return "X86ISD::BSR";
20575   case X86ISD::SHLD:               return "X86ISD::SHLD";
20576   case X86ISD::SHRD:               return "X86ISD::SHRD";
20577   case X86ISD::FAND:               return "X86ISD::FAND";
20578   case X86ISD::FANDN:              return "X86ISD::FANDN";
20579   case X86ISD::FOR:                return "X86ISD::FOR";
20580   case X86ISD::FXOR:               return "X86ISD::FXOR";
20581   case X86ISD::FILD:               return "X86ISD::FILD";
20582   case X86ISD::FILD_FLAG:          return "X86ISD::FILD_FLAG";
20583   case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
20584   case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
20585   case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
20586   case X86ISD::FLD:                return "X86ISD::FLD";
20587   case X86ISD::FST:                return "X86ISD::FST";
20588   case X86ISD::CALL:               return "X86ISD::CALL";
20589   case X86ISD::RDTSC_DAG:          return "X86ISD::RDTSC_DAG";
20590   case X86ISD::RDTSCP_DAG:         return "X86ISD::RDTSCP_DAG";
20591   case X86ISD::RDPMC_DAG:          return "X86ISD::RDPMC_DAG";
20592   case X86ISD::BT:                 return "X86ISD::BT";
20593   case X86ISD::CMP:                return "X86ISD::CMP";
20594   case X86ISD::COMI:               return "X86ISD::COMI";
20595   case X86ISD::UCOMI:              return "X86ISD::UCOMI";
20596   case X86ISD::CMPM:               return "X86ISD::CMPM";
20597   case X86ISD::CMPMU:              return "X86ISD::CMPMU";
20598   case X86ISD::CMPM_RND:           return "X86ISD::CMPM_RND";
20599   case X86ISD::SETCC:              return "X86ISD::SETCC";
20600   case X86ISD::SETCC_CARRY:        return "X86ISD::SETCC_CARRY";
20601   case X86ISD::FSETCC:             return "X86ISD::FSETCC";
20602   case X86ISD::FGETSIGNx86:        return "X86ISD::FGETSIGNx86";
20603   case X86ISD::CMOV:               return "X86ISD::CMOV";
20604   case X86ISD::BRCOND:             return "X86ISD::BRCOND";
20605   case X86ISD::RET_FLAG:           return "X86ISD::RET_FLAG";
20606   case X86ISD::IRET:               return "X86ISD::IRET";
20607   case X86ISD::REP_STOS:           return "X86ISD::REP_STOS";
20608   case X86ISD::REP_MOVS:           return "X86ISD::REP_MOVS";
20609   case X86ISD::GlobalBaseReg:      return "X86ISD::GlobalBaseReg";
20610   case X86ISD::Wrapper:            return "X86ISD::Wrapper";
20611   case X86ISD::WrapperRIP:         return "X86ISD::WrapperRIP";
20612   case X86ISD::MOVDQ2Q:            return "X86ISD::MOVDQ2Q";
20613   case X86ISD::MMX_MOVD2W:         return "X86ISD::MMX_MOVD2W";
20614   case X86ISD::MMX_MOVW2D:         return "X86ISD::MMX_MOVW2D";
20615   case X86ISD::PEXTRB:             return "X86ISD::PEXTRB";
20616   case X86ISD::PEXTRW:             return "X86ISD::PEXTRW";
20617   case X86ISD::INSERTPS:           return "X86ISD::INSERTPS";
20618   case X86ISD::PINSRB:             return "X86ISD::PINSRB";
20619   case X86ISD::PINSRW:             return "X86ISD::PINSRW";
20620   case X86ISD::MMX_PINSRW:         return "X86ISD::MMX_PINSRW";
20621   case X86ISD::PSHUFB:             return "X86ISD::PSHUFB";
20622   case X86ISD::ANDNP:              return "X86ISD::ANDNP";
20623   case X86ISD::PSIGN:              return "X86ISD::PSIGN";
20624   case X86ISD::BLENDI:             return "X86ISD::BLENDI";
20625   case X86ISD::SHRUNKBLEND:        return "X86ISD::SHRUNKBLEND";
20626   case X86ISD::ADDUS:              return "X86ISD::ADDUS";
20627   case X86ISD::SUBUS:              return "X86ISD::SUBUS";
20628   case X86ISD::HADD:               return "X86ISD::HADD";
20629   case X86ISD::HSUB:               return "X86ISD::HSUB";
20630   case X86ISD::FHADD:              return "X86ISD::FHADD";
20631   case X86ISD::FHSUB:              return "X86ISD::FHSUB";
20632   case X86ISD::ABS:                return "X86ISD::ABS";
20633   case X86ISD::CONFLICT:           return "X86ISD::CONFLICT";
20634   case X86ISD::FMAX:               return "X86ISD::FMAX";
20635   case X86ISD::FMAX_RND:           return "X86ISD::FMAX_RND";
20636   case X86ISD::FMIN:               return "X86ISD::FMIN";
20637   case X86ISD::FMIN_RND:           return "X86ISD::FMIN_RND";
20638   case X86ISD::FMAXC:              return "X86ISD::FMAXC";
20639   case X86ISD::FMINC:              return "X86ISD::FMINC";
20640   case X86ISD::FRSQRT:             return "X86ISD::FRSQRT";
20641   case X86ISD::FRCP:               return "X86ISD::FRCP";
20642   case X86ISD::EXTRQI:             return "X86ISD::EXTRQI";
20643   case X86ISD::INSERTQI:           return "X86ISD::INSERTQI";
20644   case X86ISD::TLSADDR:            return "X86ISD::TLSADDR";
20645   case X86ISD::TLSBASEADDR:        return "X86ISD::TLSBASEADDR";
20646   case X86ISD::TLSCALL:            return "X86ISD::TLSCALL";
20647   case X86ISD::EH_SJLJ_SETJMP:     return "X86ISD::EH_SJLJ_SETJMP";
20648   case X86ISD::EH_SJLJ_LONGJMP:    return "X86ISD::EH_SJLJ_LONGJMP";
20649   case X86ISD::EH_RETURN:          return "X86ISD::EH_RETURN";
20650   case X86ISD::TC_RETURN:          return "X86ISD::TC_RETURN";
20651   case X86ISD::FNSTCW16m:          return "X86ISD::FNSTCW16m";
20652   case X86ISD::FNSTSW16r:          return "X86ISD::FNSTSW16r";
20653   case X86ISD::LCMPXCHG_DAG:       return "X86ISD::LCMPXCHG_DAG";
20654   case X86ISD::LCMPXCHG8_DAG:      return "X86ISD::LCMPXCHG8_DAG";
20655   case X86ISD::LCMPXCHG16_DAG:     return "X86ISD::LCMPXCHG16_DAG";
20656   case X86ISD::VZEXT_MOVL:         return "X86ISD::VZEXT_MOVL";
20657   case X86ISD::VZEXT_LOAD:         return "X86ISD::VZEXT_LOAD";
20658   case X86ISD::VZEXT:              return "X86ISD::VZEXT";
20659   case X86ISD::VSEXT:              return "X86ISD::VSEXT";
20660   case X86ISD::VTRUNC:             return "X86ISD::VTRUNC";
20661   case X86ISD::VTRUNCS:            return "X86ISD::VTRUNCS";
20662   case X86ISD::VTRUNCUS:           return "X86ISD::VTRUNCUS";
20663   case X86ISD::VINSERT:            return "X86ISD::VINSERT";
20664   case X86ISD::VFPEXT:             return "X86ISD::VFPEXT";
20665   case X86ISD::VFPROUND:           return "X86ISD::VFPROUND";
20666   case X86ISD::CVTDQ2PD:           return "X86ISD::CVTDQ2PD";
20667   case X86ISD::CVTUDQ2PD:          return "X86ISD::CVTUDQ2PD";
20668   case X86ISD::CVT2MASK:           return "X86ISD::CVT2MASK";
20669   case X86ISD::VSHLDQ:             return "X86ISD::VSHLDQ";
20670   case X86ISD::VSRLDQ:             return "X86ISD::VSRLDQ";
20671   case X86ISD::VSHL:               return "X86ISD::VSHL";
20672   case X86ISD::VSRL:               return "X86ISD::VSRL";
20673   case X86ISD::VSRA:               return "X86ISD::VSRA";
20674   case X86ISD::VSHLI:              return "X86ISD::VSHLI";
20675   case X86ISD::VSRLI:              return "X86ISD::VSRLI";
20676   case X86ISD::VSRAI:              return "X86ISD::VSRAI";
20677   case X86ISD::CMPP:               return "X86ISD::CMPP";
20678   case X86ISD::PCMPEQ:             return "X86ISD::PCMPEQ";
20679   case X86ISD::PCMPGT:             return "X86ISD::PCMPGT";
20680   case X86ISD::PCMPEQM:            return "X86ISD::PCMPEQM";
20681   case X86ISD::PCMPGTM:            return "X86ISD::PCMPGTM";
20682   case X86ISD::ADD:                return "X86ISD::ADD";
20683   case X86ISD::SUB:                return "X86ISD::SUB";
20684   case X86ISD::ADC:                return "X86ISD::ADC";
20685   case X86ISD::SBB:                return "X86ISD::SBB";
20686   case X86ISD::SMUL:               return "X86ISD::SMUL";
20687   case X86ISD::UMUL:               return "X86ISD::UMUL";
20688   case X86ISD::SMUL8:              return "X86ISD::SMUL8";
20689   case X86ISD::UMUL8:              return "X86ISD::UMUL8";
20690   case X86ISD::SDIVREM8_SEXT_HREG: return "X86ISD::SDIVREM8_SEXT_HREG";
20691   case X86ISD::UDIVREM8_ZEXT_HREG: return "X86ISD::UDIVREM8_ZEXT_HREG";
20692   case X86ISD::INC:                return "X86ISD::INC";
20693   case X86ISD::DEC:                return "X86ISD::DEC";
20694   case X86ISD::OR:                 return "X86ISD::OR";
20695   case X86ISD::XOR:                return "X86ISD::XOR";
20696   case X86ISD::AND:                return "X86ISD::AND";
20697   case X86ISD::BEXTR:              return "X86ISD::BEXTR";
20698   case X86ISD::MUL_IMM:            return "X86ISD::MUL_IMM";
20699   case X86ISD::PTEST:              return "X86ISD::PTEST";
20700   case X86ISD::TESTP:              return "X86ISD::TESTP";
20701   case X86ISD::TESTM:              return "X86ISD::TESTM";
20702   case X86ISD::TESTNM:             return "X86ISD::TESTNM";
20703   case X86ISD::KORTEST:            return "X86ISD::KORTEST";
20704   case X86ISD::KTEST:              return "X86ISD::KTEST";
20705   case X86ISD::PACKSS:             return "X86ISD::PACKSS";
20706   case X86ISD::PACKUS:             return "X86ISD::PACKUS";
20707   case X86ISD::PALIGNR:            return "X86ISD::PALIGNR";
20708   case X86ISD::VALIGN:             return "X86ISD::VALIGN";
20709   case X86ISD::PSHUFD:             return "X86ISD::PSHUFD";
20710   case X86ISD::PSHUFHW:            return "X86ISD::PSHUFHW";
20711   case X86ISD::PSHUFLW:            return "X86ISD::PSHUFLW";
20712   case X86ISD::SHUFP:              return "X86ISD::SHUFP";
20713   case X86ISD::SHUF128:            return "X86ISD::SHUF128";
20714   case X86ISD::MOVLHPS:            return "X86ISD::MOVLHPS";
20715   case X86ISD::MOVLHPD:            return "X86ISD::MOVLHPD";
20716   case X86ISD::MOVHLPS:            return "X86ISD::MOVHLPS";
20717   case X86ISD::MOVLPS:             return "X86ISD::MOVLPS";
20718   case X86ISD::MOVLPD:             return "X86ISD::MOVLPD";
20719   case X86ISD::MOVDDUP:            return "X86ISD::MOVDDUP";
20720   case X86ISD::MOVSHDUP:           return "X86ISD::MOVSHDUP";
20721   case X86ISD::MOVSLDUP:           return "X86ISD::MOVSLDUP";
20722   case X86ISD::MOVSD:              return "X86ISD::MOVSD";
20723   case X86ISD::MOVSS:              return "X86ISD::MOVSS";
20724   case X86ISD::UNPCKL:             return "X86ISD::UNPCKL";
20725   case X86ISD::UNPCKH:             return "X86ISD::UNPCKH";
20726   case X86ISD::VBROADCAST:         return "X86ISD::VBROADCAST";
20727   case X86ISD::VBROADCASTM:        return "X86ISD::VBROADCASTM";
20728   case X86ISD::SUBV_BROADCAST:     return "X86ISD::SUBV_BROADCAST";
20729   case X86ISD::VEXTRACT:           return "X86ISD::VEXTRACT";
20730   case X86ISD::VPERMILPV:          return "X86ISD::VPERMILPV";
20731   case X86ISD::VPERMILPI:          return "X86ISD::VPERMILPI";
20732   case X86ISD::VPERM2X128:         return "X86ISD::VPERM2X128";
20733   case X86ISD::VPERMV:             return "X86ISD::VPERMV";
20734   case X86ISD::VPERMV3:            return "X86ISD::VPERMV3";
20735   case X86ISD::VPERMIV3:           return "X86ISD::VPERMIV3";
20736   case X86ISD::VPERMI:             return "X86ISD::VPERMI";
20737   case X86ISD::VPTERNLOG:          return "X86ISD::VPTERNLOG";
20738   case X86ISD::VFIXUPIMM:          return "X86ISD::VFIXUPIMM";
20739   case X86ISD::VRANGE:             return "X86ISD::VRANGE";
20740   case X86ISD::PMULUDQ:            return "X86ISD::PMULUDQ";
20741   case X86ISD::PMULDQ:             return "X86ISD::PMULDQ";
20742   case X86ISD::PSADBW:             return "X86ISD::PSADBW";
20743   case X86ISD::DBPSADBW:           return "X86ISD::DBPSADBW";
20744   case X86ISD::VASTART_SAVE_XMM_REGS: return "X86ISD::VASTART_SAVE_XMM_REGS";
20745   case X86ISD::VAARG_64:           return "X86ISD::VAARG_64";
20746   case X86ISD::WIN_ALLOCA:         return "X86ISD::WIN_ALLOCA";
20747   case X86ISD::MEMBARRIER:         return "X86ISD::MEMBARRIER";
20748   case X86ISD::MFENCE:             return "X86ISD::MFENCE";
20749   case X86ISD::SFENCE:             return "X86ISD::SFENCE";
20750   case X86ISD::LFENCE:             return "X86ISD::LFENCE";
20751   case X86ISD::SEG_ALLOCA:         return "X86ISD::SEG_ALLOCA";
20752   case X86ISD::SAHF:               return "X86ISD::SAHF";
20753   case X86ISD::RDRAND:             return "X86ISD::RDRAND";
20754   case X86ISD::RDSEED:             return "X86ISD::RDSEED";
20755   case X86ISD::VPMADDUBSW:         return "X86ISD::VPMADDUBSW";
20756   case X86ISD::VPMADDWD:           return "X86ISD::VPMADDWD";
20757   case X86ISD::VPROT:              return "X86ISD::VPROT";
20758   case X86ISD::VPROTI:             return "X86ISD::VPROTI";
20759   case X86ISD::VPSHA:              return "X86ISD::VPSHA";
20760   case X86ISD::VPSHL:              return "X86ISD::VPSHL";
20761   case X86ISD::VPCOM:              return "X86ISD::VPCOM";
20762   case X86ISD::VPCOMU:             return "X86ISD::VPCOMU";
20763   case X86ISD::FMADD:              return "X86ISD::FMADD";
20764   case X86ISD::FMSUB:              return "X86ISD::FMSUB";
20765   case X86ISD::FNMADD:             return "X86ISD::FNMADD";
20766   case X86ISD::FNMSUB:             return "X86ISD::FNMSUB";
20767   case X86ISD::FMADDSUB:           return "X86ISD::FMADDSUB";
20768   case X86ISD::FMSUBADD:           return "X86ISD::FMSUBADD";
20769   case X86ISD::FMADD_RND:          return "X86ISD::FMADD_RND";
20770   case X86ISD::FNMADD_RND:         return "X86ISD::FNMADD_RND";
20771   case X86ISD::FMSUB_RND:          return "X86ISD::FMSUB_RND";
20772   case X86ISD::FNMSUB_RND:         return "X86ISD::FNMSUB_RND";
20773   case X86ISD::FMADDSUB_RND:       return "X86ISD::FMADDSUB_RND";
20774   case X86ISD::FMSUBADD_RND:       return "X86ISD::FMSUBADD_RND";
20775   case X86ISD::VRNDSCALE:          return "X86ISD::VRNDSCALE";
20776   case X86ISD::VREDUCE:            return "X86ISD::VREDUCE";
20777   case X86ISD::VGETMANT:           return "X86ISD::VGETMANT";
20778   case X86ISD::PCMPESTRI:          return "X86ISD::PCMPESTRI";
20779   case X86ISD::PCMPISTRI:          return "X86ISD::PCMPISTRI";
20780   case X86ISD::XTEST:              return "X86ISD::XTEST";
20781   case X86ISD::COMPRESS:           return "X86ISD::COMPRESS";
20782   case X86ISD::EXPAND:             return "X86ISD::EXPAND";
20783   case X86ISD::SELECT:             return "X86ISD::SELECT";
20784   case X86ISD::ADDSUB:             return "X86ISD::ADDSUB";
20785   case X86ISD::RCP28:              return "X86ISD::RCP28";
20786   case X86ISD::EXP2:               return "X86ISD::EXP2";
20787   case X86ISD::RSQRT28:            return "X86ISD::RSQRT28";
20788   case X86ISD::FADD_RND:           return "X86ISD::FADD_RND";
20789   case X86ISD::FSUB_RND:           return "X86ISD::FSUB_RND";
20790   case X86ISD::FMUL_RND:           return "X86ISD::FMUL_RND";
20791   case X86ISD::FDIV_RND:           return "X86ISD::FDIV_RND";
20792   case X86ISD::FSQRT_RND:          return "X86ISD::FSQRT_RND";
20793   case X86ISD::FGETEXP_RND:        return "X86ISD::FGETEXP_RND";
20794   case X86ISD::SCALEF:             return "X86ISD::SCALEF";
20795   case X86ISD::ADDS:               return "X86ISD::ADDS";
20796   case X86ISD::SUBS:               return "X86ISD::SUBS";
20797   case X86ISD::AVG:                return "X86ISD::AVG";
20798   case X86ISD::MULHRS:             return "X86ISD::MULHRS";
20799   case X86ISD::SINT_TO_FP_RND:     return "X86ISD::SINT_TO_FP_RND";
20800   case X86ISD::UINT_TO_FP_RND:     return "X86ISD::UINT_TO_FP_RND";
20801   case X86ISD::FP_TO_SINT_RND:     return "X86ISD::FP_TO_SINT_RND";
20802   case X86ISD::FP_TO_UINT_RND:     return "X86ISD::FP_TO_UINT_RND";
20803   case X86ISD::VFPCLASS:           return "X86ISD::VFPCLASS";
20804   case X86ISD::VFPCLASSS:          return "X86ISD::VFPCLASSS";
20805   }
20806   return nullptr;
20807 }
20808
20809 // isLegalAddressingMode - Return true if the addressing mode represented
20810 // by AM is legal for this target, for a load/store of the specified type.
20811 bool X86TargetLowering::isLegalAddressingMode(const DataLayout &DL,
20812                                               const AddrMode &AM, Type *Ty,
20813                                               unsigned AS) const {
20814   // X86 supports extremely general addressing modes.
20815   CodeModel::Model M = getTargetMachine().getCodeModel();
20816   Reloc::Model R = getTargetMachine().getRelocationModel();
20817
20818   // X86 allows a sign-extended 32-bit immediate field as a displacement.
20819   if (!X86::isOffsetSuitableForCodeModel(AM.BaseOffs, M, AM.BaseGV != nullptr))
20820     return false;
20821
20822   if (AM.BaseGV) {
20823     unsigned GVFlags =
20824       Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
20825
20826     // If a reference to this global requires an extra load, we can't fold it.
20827     if (isGlobalStubReference(GVFlags))
20828       return false;
20829
20830     // If BaseGV requires a register for the PIC base, we cannot also have a
20831     // BaseReg specified.
20832     if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
20833       return false;
20834
20835     // If lower 4G is not available, then we must use rip-relative addressing.
20836     if ((M != CodeModel::Small || R != Reloc::Static) &&
20837         Subtarget->is64Bit() && (AM.BaseOffs || AM.Scale > 1))
20838       return false;
20839   }
20840
20841   switch (AM.Scale) {
20842   case 0:
20843   case 1:
20844   case 2:
20845   case 4:
20846   case 8:
20847     // These scales always work.
20848     break;
20849   case 3:
20850   case 5:
20851   case 9:
20852     // These scales are formed with basereg+scalereg.  Only accept if there is
20853     // no basereg yet.
20854     if (AM.HasBaseReg)
20855       return false;
20856     break;
20857   default:  // Other stuff never works.
20858     return false;
20859   }
20860
20861   return true;
20862 }
20863
20864 bool X86TargetLowering::isVectorShiftByScalarCheap(Type *Ty) const {
20865   unsigned Bits = Ty->getScalarSizeInBits();
20866
20867   // 8-bit shifts are always expensive, but versions with a scalar amount aren't
20868   // particularly cheaper than those without.
20869   if (Bits == 8)
20870     return false;
20871
20872   // On AVX2 there are new vpsllv[dq] instructions (and other shifts), that make
20873   // variable shifts just as cheap as scalar ones.
20874   if (Subtarget->hasInt256() && (Bits == 32 || Bits == 64))
20875     return false;
20876
20877   // Otherwise, it's significantly cheaper to shift by a scalar amount than by a
20878   // fully general vector.
20879   return true;
20880 }
20881
20882 bool X86TargetLowering::isTruncateFree(Type *Ty1, Type *Ty2) const {
20883   if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
20884     return false;
20885   unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
20886   unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
20887   return NumBits1 > NumBits2;
20888 }
20889
20890 bool X86TargetLowering::allowTruncateForTailCall(Type *Ty1, Type *Ty2) const {
20891   if (!Ty1->isIntegerTy() || !Ty2->isIntegerTy())
20892     return false;
20893
20894   if (!isTypeLegal(EVT::getEVT(Ty1)))
20895     return false;
20896
20897   assert(Ty1->getPrimitiveSizeInBits() <= 64 && "i128 is probably not a noop");
20898
20899   // Assuming the caller doesn't have a zeroext or signext return parameter,
20900   // truncation all the way down to i1 is valid.
20901   return true;
20902 }
20903
20904 bool X86TargetLowering::isLegalICmpImmediate(int64_t Imm) const {
20905   return isInt<32>(Imm);
20906 }
20907
20908 bool X86TargetLowering::isLegalAddImmediate(int64_t Imm) const {
20909   // Can also use sub to handle negated immediates.
20910   return isInt<32>(Imm);
20911 }
20912
20913 bool X86TargetLowering::isTruncateFree(EVT VT1, EVT VT2) const {
20914   if (!VT1.isInteger() || !VT2.isInteger())
20915     return false;
20916   unsigned NumBits1 = VT1.getSizeInBits();
20917   unsigned NumBits2 = VT2.getSizeInBits();
20918   return NumBits1 > NumBits2;
20919 }
20920
20921 bool X86TargetLowering::isZExtFree(Type *Ty1, Type *Ty2) const {
20922   // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
20923   return Ty1->isIntegerTy(32) && Ty2->isIntegerTy(64) && Subtarget->is64Bit();
20924 }
20925
20926 bool X86TargetLowering::isZExtFree(EVT VT1, EVT VT2) const {
20927   // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
20928   return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
20929 }
20930
20931 bool X86TargetLowering::isZExtFree(SDValue Val, EVT VT2) const {
20932   EVT VT1 = Val.getValueType();
20933   if (isZExtFree(VT1, VT2))
20934     return true;
20935
20936   if (Val.getOpcode() != ISD::LOAD)
20937     return false;
20938
20939   if (!VT1.isSimple() || !VT1.isInteger() ||
20940       !VT2.isSimple() || !VT2.isInteger())
20941     return false;
20942
20943   switch (VT1.getSimpleVT().SimpleTy) {
20944   default: break;
20945   case MVT::i8:
20946   case MVT::i16:
20947   case MVT::i32:
20948     // X86 has 8, 16, and 32-bit zero-extending loads.
20949     return true;
20950   }
20951
20952   return false;
20953 }
20954
20955 bool X86TargetLowering::isVectorLoadExtDesirable(SDValue) const { return true; }
20956
20957 bool
20958 X86TargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
20959   if (!Subtarget->hasAnyFMA())
20960     return false;
20961
20962   VT = VT.getScalarType();
20963
20964   if (!VT.isSimple())
20965     return false;
20966
20967   switch (VT.getSimpleVT().SimpleTy) {
20968   case MVT::f32:
20969   case MVT::f64:
20970     return true;
20971   default:
20972     break;
20973   }
20974
20975   return false;
20976 }
20977
20978 bool X86TargetLowering::isNarrowingProfitable(EVT VT1, EVT VT2) const {
20979   // i16 instructions are longer (0x66 prefix) and potentially slower.
20980   return !(VT1 == MVT::i32 && VT2 == MVT::i16);
20981 }
20982
20983 /// isShuffleMaskLegal - Targets can use this to indicate that they only
20984 /// support *some* VECTOR_SHUFFLE operations, those with specific masks.
20985 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
20986 /// are assumed to be legal.
20987 bool
20988 X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
20989                                       EVT VT) const {
20990   if (!VT.isSimple())
20991     return false;
20992
20993   // Not for i1 vectors
20994   if (VT.getSimpleVT().getScalarType() == MVT::i1)
20995     return false;
20996
20997   // Very little shuffling can be done for 64-bit vectors right now.
20998   if (VT.getSimpleVT().getSizeInBits() == 64)
20999     return false;
21000
21001   // We only care that the types being shuffled are legal. The lowering can
21002   // handle any possible shuffle mask that results.
21003   return isTypeLegal(VT.getSimpleVT());
21004 }
21005
21006 bool
21007 X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
21008                                           EVT VT) const {
21009   // Just delegate to the generic legality, clear masks aren't special.
21010   return isShuffleMaskLegal(Mask, VT);
21011 }
21012
21013 //===----------------------------------------------------------------------===//
21014 //                           X86 Scheduler Hooks
21015 //===----------------------------------------------------------------------===//
21016
21017 /// Utility function to emit xbegin specifying the start of an RTM region.
21018 static MachineBasicBlock *EmitXBegin(MachineInstr *MI, MachineBasicBlock *MBB,
21019                                      const TargetInstrInfo *TII) {
21020   DebugLoc DL = MI->getDebugLoc();
21021
21022   const BasicBlock *BB = MBB->getBasicBlock();
21023   MachineFunction::iterator I = ++MBB->getIterator();
21024
21025   // For the v = xbegin(), we generate
21026   //
21027   // thisMBB:
21028   //  xbegin sinkMBB
21029   //
21030   // mainMBB:
21031   //  eax = -1
21032   //
21033   // sinkMBB:
21034   //  v = eax
21035
21036   MachineBasicBlock *thisMBB = MBB;
21037   MachineFunction *MF = MBB->getParent();
21038   MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
21039   MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
21040   MF->insert(I, mainMBB);
21041   MF->insert(I, sinkMBB);
21042
21043   // Transfer the remainder of BB and its successor edges to sinkMBB.
21044   sinkMBB->splice(sinkMBB->begin(), MBB,
21045                   std::next(MachineBasicBlock::iterator(MI)), MBB->end());
21046   sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
21047
21048   // thisMBB:
21049   //  xbegin sinkMBB
21050   //  # fallthrough to mainMBB
21051   //  # abortion to sinkMBB
21052   BuildMI(thisMBB, DL, TII->get(X86::XBEGIN_4)).addMBB(sinkMBB);
21053   thisMBB->addSuccessor(mainMBB);
21054   thisMBB->addSuccessor(sinkMBB);
21055
21056   // mainMBB:
21057   //  EAX = -1
21058   BuildMI(mainMBB, DL, TII->get(X86::MOV32ri), X86::EAX).addImm(-1);
21059   mainMBB->addSuccessor(sinkMBB);
21060
21061   // sinkMBB:
21062   // EAX is live into the sinkMBB
21063   sinkMBB->addLiveIn(X86::EAX);
21064   BuildMI(*sinkMBB, sinkMBB->begin(), DL,
21065           TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
21066     .addReg(X86::EAX);
21067
21068   MI->eraseFromParent();
21069   return sinkMBB;
21070 }
21071
21072 // FIXME: When we get size specific XMM0 registers, i.e. XMM0_V16I8
21073 // or XMM0_V32I8 in AVX all of this code can be replaced with that
21074 // in the .td file.
21075 static MachineBasicBlock *EmitPCMPSTRM(MachineInstr *MI, MachineBasicBlock *BB,
21076                                        const TargetInstrInfo *TII) {
21077   unsigned Opc;
21078   switch (MI->getOpcode()) {
21079   default: llvm_unreachable("illegal opcode!");
21080   case X86::PCMPISTRM128REG:  Opc = X86::PCMPISTRM128rr;  break;
21081   case X86::VPCMPISTRM128REG: Opc = X86::VPCMPISTRM128rr; break;
21082   case X86::PCMPISTRM128MEM:  Opc = X86::PCMPISTRM128rm;  break;
21083   case X86::VPCMPISTRM128MEM: Opc = X86::VPCMPISTRM128rm; break;
21084   case X86::PCMPESTRM128REG:  Opc = X86::PCMPESTRM128rr;  break;
21085   case X86::VPCMPESTRM128REG: Opc = X86::VPCMPESTRM128rr; break;
21086   case X86::PCMPESTRM128MEM:  Opc = X86::PCMPESTRM128rm;  break;
21087   case X86::VPCMPESTRM128MEM: Opc = X86::VPCMPESTRM128rm; break;
21088   }
21089
21090   DebugLoc dl = MI->getDebugLoc();
21091   MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
21092
21093   unsigned NumArgs = MI->getNumOperands();
21094   for (unsigned i = 1; i < NumArgs; ++i) {
21095     MachineOperand &Op = MI->getOperand(i);
21096     if (!(Op.isReg() && Op.isImplicit()))
21097       MIB.addOperand(Op);
21098   }
21099   if (MI->hasOneMemOperand())
21100     MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
21101
21102   BuildMI(*BB, MI, dl,
21103     TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
21104     .addReg(X86::XMM0);
21105
21106   MI->eraseFromParent();
21107   return BB;
21108 }
21109
21110 // FIXME: Custom handling because TableGen doesn't support multiple implicit
21111 // defs in an instruction pattern
21112 static MachineBasicBlock *EmitPCMPSTRI(MachineInstr *MI, MachineBasicBlock *BB,
21113                                        const TargetInstrInfo *TII) {
21114   unsigned Opc;
21115   switch (MI->getOpcode()) {
21116   default: llvm_unreachable("illegal opcode!");
21117   case X86::PCMPISTRIREG:  Opc = X86::PCMPISTRIrr;  break;
21118   case X86::VPCMPISTRIREG: Opc = X86::VPCMPISTRIrr; break;
21119   case X86::PCMPISTRIMEM:  Opc = X86::PCMPISTRIrm;  break;
21120   case X86::VPCMPISTRIMEM: Opc = X86::VPCMPISTRIrm; break;
21121   case X86::PCMPESTRIREG:  Opc = X86::PCMPESTRIrr;  break;
21122   case X86::VPCMPESTRIREG: Opc = X86::VPCMPESTRIrr; break;
21123   case X86::PCMPESTRIMEM:  Opc = X86::PCMPESTRIrm;  break;
21124   case X86::VPCMPESTRIMEM: Opc = X86::VPCMPESTRIrm; break;
21125   }
21126
21127   DebugLoc dl = MI->getDebugLoc();
21128   MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(Opc));
21129
21130   unsigned NumArgs = MI->getNumOperands(); // remove the results
21131   for (unsigned i = 1; i < NumArgs; ++i) {
21132     MachineOperand &Op = MI->getOperand(i);
21133     if (!(Op.isReg() && Op.isImplicit()))
21134       MIB.addOperand(Op);
21135   }
21136   if (MI->hasOneMemOperand())
21137     MIB->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
21138
21139   BuildMI(*BB, MI, dl,
21140     TII->get(TargetOpcode::COPY), MI->getOperand(0).getReg())
21141     .addReg(X86::ECX);
21142
21143   MI->eraseFromParent();
21144   return BB;
21145 }
21146
21147 static MachineBasicBlock *EmitMonitor(MachineInstr *MI, MachineBasicBlock *BB,
21148                                       const X86Subtarget *Subtarget) {
21149   DebugLoc dl = MI->getDebugLoc();
21150   const TargetInstrInfo *TII = Subtarget->getInstrInfo();
21151   // Address into RAX/EAX, other two args into ECX, EDX.
21152   unsigned MemOpc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
21153   unsigned MemReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
21154   MachineInstrBuilder MIB = BuildMI(*BB, MI, dl, TII->get(MemOpc), MemReg);
21155   for (int i = 0; i < X86::AddrNumOperands; ++i)
21156     MIB.addOperand(MI->getOperand(i));
21157
21158   unsigned ValOps = X86::AddrNumOperands;
21159   BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::ECX)
21160     .addReg(MI->getOperand(ValOps).getReg());
21161   BuildMI(*BB, MI, dl, TII->get(TargetOpcode::COPY), X86::EDX)
21162     .addReg(MI->getOperand(ValOps+1).getReg());
21163
21164   // The instruction doesn't actually take any operands though.
21165   BuildMI(*BB, MI, dl, TII->get(X86::MONITORrrr));
21166
21167   MI->eraseFromParent(); // The pseudo is gone now.
21168   return BB;
21169 }
21170
21171 MachineBasicBlock *
21172 X86TargetLowering::EmitVAARG64WithCustomInserter(MachineInstr *MI,
21173                                                  MachineBasicBlock *MBB) const {
21174   // Emit va_arg instruction on X86-64.
21175
21176   // Operands to this pseudo-instruction:
21177   // 0  ) Output        : destination address (reg)
21178   // 1-5) Input         : va_list address (addr, i64mem)
21179   // 6  ) ArgSize       : Size (in bytes) of vararg type
21180   // 7  ) ArgMode       : 0=overflow only, 1=use gp_offset, 2=use fp_offset
21181   // 8  ) Align         : Alignment of type
21182   // 9  ) EFLAGS (implicit-def)
21183
21184   assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
21185   static_assert(X86::AddrNumOperands == 5,
21186                 "VAARG_64 assumes 5 address operands");
21187
21188   unsigned DestReg = MI->getOperand(0).getReg();
21189   MachineOperand &Base = MI->getOperand(1);
21190   MachineOperand &Scale = MI->getOperand(2);
21191   MachineOperand &Index = MI->getOperand(3);
21192   MachineOperand &Disp = MI->getOperand(4);
21193   MachineOperand &Segment = MI->getOperand(5);
21194   unsigned ArgSize = MI->getOperand(6).getImm();
21195   unsigned ArgMode = MI->getOperand(7).getImm();
21196   unsigned Align = MI->getOperand(8).getImm();
21197
21198   // Memory Reference
21199   assert(MI->hasOneMemOperand() && "Expected VAARG_64 to have one memoperand");
21200   MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
21201   MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
21202
21203   // Machine Information
21204   const TargetInstrInfo *TII = Subtarget->getInstrInfo();
21205   MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
21206   const TargetRegisterClass *AddrRegClass = getRegClassFor(MVT::i64);
21207   const TargetRegisterClass *OffsetRegClass = getRegClassFor(MVT::i32);
21208   DebugLoc DL = MI->getDebugLoc();
21209
21210   // struct va_list {
21211   //   i32   gp_offset
21212   //   i32   fp_offset
21213   //   i64   overflow_area (address)
21214   //   i64   reg_save_area (address)
21215   // }
21216   // sizeof(va_list) = 24
21217   // alignment(va_list) = 8
21218
21219   unsigned TotalNumIntRegs = 6;
21220   unsigned TotalNumXMMRegs = 8;
21221   bool UseGPOffset = (ArgMode == 1);
21222   bool UseFPOffset = (ArgMode == 2);
21223   unsigned MaxOffset = TotalNumIntRegs * 8 +
21224                        (UseFPOffset ? TotalNumXMMRegs * 16 : 0);
21225
21226   /* Align ArgSize to a multiple of 8 */
21227   unsigned ArgSizeA8 = (ArgSize + 7) & ~7;
21228   bool NeedsAlign = (Align > 8);
21229
21230   MachineBasicBlock *thisMBB = MBB;
21231   MachineBasicBlock *overflowMBB;
21232   MachineBasicBlock *offsetMBB;
21233   MachineBasicBlock *endMBB;
21234
21235   unsigned OffsetDestReg = 0;    // Argument address computed by offsetMBB
21236   unsigned OverflowDestReg = 0;  // Argument address computed by overflowMBB
21237   unsigned OffsetReg = 0;
21238
21239   if (!UseGPOffset && !UseFPOffset) {
21240     // If we only pull from the overflow region, we don't create a branch.
21241     // We don't need to alter control flow.
21242     OffsetDestReg = 0; // unused
21243     OverflowDestReg = DestReg;
21244
21245     offsetMBB = nullptr;
21246     overflowMBB = thisMBB;
21247     endMBB = thisMBB;
21248   } else {
21249     // First emit code to check if gp_offset (or fp_offset) is below the bound.
21250     // If so, pull the argument from reg_save_area. (branch to offsetMBB)
21251     // If not, pull from overflow_area. (branch to overflowMBB)
21252     //
21253     //       thisMBB
21254     //         |     .
21255     //         |        .
21256     //     offsetMBB   overflowMBB
21257     //         |        .
21258     //         |     .
21259     //        endMBB
21260
21261     // Registers for the PHI in endMBB
21262     OffsetDestReg = MRI.createVirtualRegister(AddrRegClass);
21263     OverflowDestReg = MRI.createVirtualRegister(AddrRegClass);
21264
21265     const BasicBlock *LLVM_BB = MBB->getBasicBlock();
21266     MachineFunction *MF = MBB->getParent();
21267     overflowMBB = MF->CreateMachineBasicBlock(LLVM_BB);
21268     offsetMBB = MF->CreateMachineBasicBlock(LLVM_BB);
21269     endMBB = MF->CreateMachineBasicBlock(LLVM_BB);
21270
21271     MachineFunction::iterator MBBIter = ++MBB->getIterator();
21272
21273     // Insert the new basic blocks
21274     MF->insert(MBBIter, offsetMBB);
21275     MF->insert(MBBIter, overflowMBB);
21276     MF->insert(MBBIter, endMBB);
21277
21278     // Transfer the remainder of MBB and its successor edges to endMBB.
21279     endMBB->splice(endMBB->begin(), thisMBB,
21280                    std::next(MachineBasicBlock::iterator(MI)), thisMBB->end());
21281     endMBB->transferSuccessorsAndUpdatePHIs(thisMBB);
21282
21283     // Make offsetMBB and overflowMBB successors of thisMBB
21284     thisMBB->addSuccessor(offsetMBB);
21285     thisMBB->addSuccessor(overflowMBB);
21286
21287     // endMBB is a successor of both offsetMBB and overflowMBB
21288     offsetMBB->addSuccessor(endMBB);
21289     overflowMBB->addSuccessor(endMBB);
21290
21291     // Load the offset value into a register
21292     OffsetReg = MRI.createVirtualRegister(OffsetRegClass);
21293     BuildMI(thisMBB, DL, TII->get(X86::MOV32rm), OffsetReg)
21294       .addOperand(Base)
21295       .addOperand(Scale)
21296       .addOperand(Index)
21297       .addDisp(Disp, UseFPOffset ? 4 : 0)
21298       .addOperand(Segment)
21299       .setMemRefs(MMOBegin, MMOEnd);
21300
21301     // Check if there is enough room left to pull this argument.
21302     BuildMI(thisMBB, DL, TII->get(X86::CMP32ri))
21303       .addReg(OffsetReg)
21304       .addImm(MaxOffset + 8 - ArgSizeA8);
21305
21306     // Branch to "overflowMBB" if offset >= max
21307     // Fall through to "offsetMBB" otherwise
21308     BuildMI(thisMBB, DL, TII->get(X86::GetCondBranchFromCond(X86::COND_AE)))
21309       .addMBB(overflowMBB);
21310   }
21311
21312   // In offsetMBB, emit code to use the reg_save_area.
21313   if (offsetMBB) {
21314     assert(OffsetReg != 0);
21315
21316     // Read the reg_save_area address.
21317     unsigned RegSaveReg = MRI.createVirtualRegister(AddrRegClass);
21318     BuildMI(offsetMBB, DL, TII->get(X86::MOV64rm), RegSaveReg)
21319       .addOperand(Base)
21320       .addOperand(Scale)
21321       .addOperand(Index)
21322       .addDisp(Disp, 16)
21323       .addOperand(Segment)
21324       .setMemRefs(MMOBegin, MMOEnd);
21325
21326     // Zero-extend the offset
21327     unsigned OffsetReg64 = MRI.createVirtualRegister(AddrRegClass);
21328       BuildMI(offsetMBB, DL, TII->get(X86::SUBREG_TO_REG), OffsetReg64)
21329         .addImm(0)
21330         .addReg(OffsetReg)
21331         .addImm(X86::sub_32bit);
21332
21333     // Add the offset to the reg_save_area to get the final address.
21334     BuildMI(offsetMBB, DL, TII->get(X86::ADD64rr), OffsetDestReg)
21335       .addReg(OffsetReg64)
21336       .addReg(RegSaveReg);
21337
21338     // Compute the offset for the next argument
21339     unsigned NextOffsetReg = MRI.createVirtualRegister(OffsetRegClass);
21340     BuildMI(offsetMBB, DL, TII->get(X86::ADD32ri), NextOffsetReg)
21341       .addReg(OffsetReg)
21342       .addImm(UseFPOffset ? 16 : 8);
21343
21344     // Store it back into the va_list.
21345     BuildMI(offsetMBB, DL, TII->get(X86::MOV32mr))
21346       .addOperand(Base)
21347       .addOperand(Scale)
21348       .addOperand(Index)
21349       .addDisp(Disp, UseFPOffset ? 4 : 0)
21350       .addOperand(Segment)
21351       .addReg(NextOffsetReg)
21352       .setMemRefs(MMOBegin, MMOEnd);
21353
21354     // Jump to endMBB
21355     BuildMI(offsetMBB, DL, TII->get(X86::JMP_1))
21356       .addMBB(endMBB);
21357   }
21358
21359   //
21360   // Emit code to use overflow area
21361   //
21362
21363   // Load the overflow_area address into a register.
21364   unsigned OverflowAddrReg = MRI.createVirtualRegister(AddrRegClass);
21365   BuildMI(overflowMBB, DL, TII->get(X86::MOV64rm), OverflowAddrReg)
21366     .addOperand(Base)
21367     .addOperand(Scale)
21368     .addOperand(Index)
21369     .addDisp(Disp, 8)
21370     .addOperand(Segment)
21371     .setMemRefs(MMOBegin, MMOEnd);
21372
21373   // If we need to align it, do so. Otherwise, just copy the address
21374   // to OverflowDestReg.
21375   if (NeedsAlign) {
21376     // Align the overflow address
21377     assert((Align & (Align-1)) == 0 && "Alignment must be a power of 2");
21378     unsigned TmpReg = MRI.createVirtualRegister(AddrRegClass);
21379
21380     // aligned_addr = (addr + (align-1)) & ~(align-1)
21381     BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), TmpReg)
21382       .addReg(OverflowAddrReg)
21383       .addImm(Align-1);
21384
21385     BuildMI(overflowMBB, DL, TII->get(X86::AND64ri32), OverflowDestReg)
21386       .addReg(TmpReg)
21387       .addImm(~(uint64_t)(Align-1));
21388   } else {
21389     BuildMI(overflowMBB, DL, TII->get(TargetOpcode::COPY), OverflowDestReg)
21390       .addReg(OverflowAddrReg);
21391   }
21392
21393   // Compute the next overflow address after this argument.
21394   // (the overflow address should be kept 8-byte aligned)
21395   unsigned NextAddrReg = MRI.createVirtualRegister(AddrRegClass);
21396   BuildMI(overflowMBB, DL, TII->get(X86::ADD64ri32), NextAddrReg)
21397     .addReg(OverflowDestReg)
21398     .addImm(ArgSizeA8);
21399
21400   // Store the new overflow address.
21401   BuildMI(overflowMBB, DL, TII->get(X86::MOV64mr))
21402     .addOperand(Base)
21403     .addOperand(Scale)
21404     .addOperand(Index)
21405     .addDisp(Disp, 8)
21406     .addOperand(Segment)
21407     .addReg(NextAddrReg)
21408     .setMemRefs(MMOBegin, MMOEnd);
21409
21410   // If we branched, emit the PHI to the front of endMBB.
21411   if (offsetMBB) {
21412     BuildMI(*endMBB, endMBB->begin(), DL,
21413             TII->get(X86::PHI), DestReg)
21414       .addReg(OffsetDestReg).addMBB(offsetMBB)
21415       .addReg(OverflowDestReg).addMBB(overflowMBB);
21416   }
21417
21418   // Erase the pseudo instruction
21419   MI->eraseFromParent();
21420
21421   return endMBB;
21422 }
21423
21424 MachineBasicBlock *
21425 X86TargetLowering::EmitVAStartSaveXMMRegsWithCustomInserter(
21426                                                  MachineInstr *MI,
21427                                                  MachineBasicBlock *MBB) const {
21428   // Emit code to save XMM registers to the stack. The ABI says that the
21429   // number of registers to save is given in %al, so it's theoretically
21430   // possible to do an indirect jump trick to avoid saving all of them,
21431   // however this code takes a simpler approach and just executes all
21432   // of the stores if %al is non-zero. It's less code, and it's probably
21433   // easier on the hardware branch predictor, and stores aren't all that
21434   // expensive anyway.
21435
21436   // Create the new basic blocks. One block contains all the XMM stores,
21437   // and one block is the final destination regardless of whether any
21438   // stores were performed.
21439   const BasicBlock *LLVM_BB = MBB->getBasicBlock();
21440   MachineFunction *F = MBB->getParent();
21441   MachineFunction::iterator MBBIter = ++MBB->getIterator();
21442   MachineBasicBlock *XMMSaveMBB = F->CreateMachineBasicBlock(LLVM_BB);
21443   MachineBasicBlock *EndMBB = F->CreateMachineBasicBlock(LLVM_BB);
21444   F->insert(MBBIter, XMMSaveMBB);
21445   F->insert(MBBIter, EndMBB);
21446
21447   // Transfer the remainder of MBB and its successor edges to EndMBB.
21448   EndMBB->splice(EndMBB->begin(), MBB,
21449                  std::next(MachineBasicBlock::iterator(MI)), MBB->end());
21450   EndMBB->transferSuccessorsAndUpdatePHIs(MBB);
21451
21452   // The original block will now fall through to the XMM save block.
21453   MBB->addSuccessor(XMMSaveMBB);
21454   // The XMMSaveMBB will fall through to the end block.
21455   XMMSaveMBB->addSuccessor(EndMBB);
21456
21457   // Now add the instructions.
21458   const TargetInstrInfo *TII = Subtarget->getInstrInfo();
21459   DebugLoc DL = MI->getDebugLoc();
21460
21461   unsigned CountReg = MI->getOperand(0).getReg();
21462   int64_t RegSaveFrameIndex = MI->getOperand(1).getImm();
21463   int64_t VarArgsFPOffset = MI->getOperand(2).getImm();
21464
21465   if (!Subtarget->isCallingConvWin64(F->getFunction()->getCallingConv())) {
21466     // If %al is 0, branch around the XMM save block.
21467     BuildMI(MBB, DL, TII->get(X86::TEST8rr)).addReg(CountReg).addReg(CountReg);
21468     BuildMI(MBB, DL, TII->get(X86::JE_1)).addMBB(EndMBB);
21469     MBB->addSuccessor(EndMBB);
21470   }
21471
21472   // Make sure the last operand is EFLAGS, which gets clobbered by the branch
21473   // that was just emitted, but clearly shouldn't be "saved".
21474   assert((MI->getNumOperands() <= 3 ||
21475           !MI->getOperand(MI->getNumOperands() - 1).isReg() ||
21476           MI->getOperand(MI->getNumOperands() - 1).getReg() == X86::EFLAGS)
21477          && "Expected last argument to be EFLAGS");
21478   unsigned MOVOpc = Subtarget->hasFp256() ? X86::VMOVAPSmr : X86::MOVAPSmr;
21479   // In the XMM save block, save all the XMM argument registers.
21480   for (int i = 3, e = MI->getNumOperands() - 1; i != e; ++i) {
21481     int64_t Offset = (i - 3) * 16 + VarArgsFPOffset;
21482     MachineMemOperand *MMO = F->getMachineMemOperand(
21483         MachinePointerInfo::getFixedStack(*F, RegSaveFrameIndex, Offset),
21484         MachineMemOperand::MOStore,
21485         /*Size=*/16, /*Align=*/16);
21486     BuildMI(XMMSaveMBB, DL, TII->get(MOVOpc))
21487       .addFrameIndex(RegSaveFrameIndex)
21488       .addImm(/*Scale=*/1)
21489       .addReg(/*IndexReg=*/0)
21490       .addImm(/*Disp=*/Offset)
21491       .addReg(/*Segment=*/0)
21492       .addReg(MI->getOperand(i).getReg())
21493       .addMemOperand(MMO);
21494   }
21495
21496   MI->eraseFromParent();   // The pseudo instruction is gone now.
21497
21498   return EndMBB;
21499 }
21500
21501 // The EFLAGS operand of SelectItr might be missing a kill marker
21502 // because there were multiple uses of EFLAGS, and ISel didn't know
21503 // which to mark. Figure out whether SelectItr should have had a
21504 // kill marker, and set it if it should. Returns the correct kill
21505 // marker value.
21506 static bool checkAndUpdateEFLAGSKill(MachineBasicBlock::iterator SelectItr,
21507                                      MachineBasicBlock* BB,
21508                                      const TargetRegisterInfo* TRI) {
21509   // Scan forward through BB for a use/def of EFLAGS.
21510   MachineBasicBlock::iterator miI(std::next(SelectItr));
21511   for (MachineBasicBlock::iterator miE = BB->end(); miI != miE; ++miI) {
21512     const MachineInstr& mi = *miI;
21513     if (mi.readsRegister(X86::EFLAGS))
21514       return false;
21515     if (mi.definesRegister(X86::EFLAGS))
21516       break; // Should have kill-flag - update below.
21517   }
21518
21519   // If we hit the end of the block, check whether EFLAGS is live into a
21520   // successor.
21521   if (miI == BB->end()) {
21522     for (MachineBasicBlock::succ_iterator sItr = BB->succ_begin(),
21523                                           sEnd = BB->succ_end();
21524          sItr != sEnd; ++sItr) {
21525       MachineBasicBlock* succ = *sItr;
21526       if (succ->isLiveIn(X86::EFLAGS))
21527         return false;
21528     }
21529   }
21530
21531   // We found a def, or hit the end of the basic block and EFLAGS wasn't live
21532   // out. SelectMI should have a kill flag on EFLAGS.
21533   SelectItr->addRegisterKilled(X86::EFLAGS, TRI);
21534   return true;
21535 }
21536
21537 // Return true if it is OK for this CMOV pseudo-opcode to be cascaded
21538 // together with other CMOV pseudo-opcodes into a single basic-block with
21539 // conditional jump around it.
21540 static bool isCMOVPseudo(MachineInstr *MI) {
21541   switch (MI->getOpcode()) {
21542   case X86::CMOV_FR32:
21543   case X86::CMOV_FR64:
21544   case X86::CMOV_GR8:
21545   case X86::CMOV_GR16:
21546   case X86::CMOV_GR32:
21547   case X86::CMOV_RFP32:
21548   case X86::CMOV_RFP64:
21549   case X86::CMOV_RFP80:
21550   case X86::CMOV_V2F64:
21551   case X86::CMOV_V2I64:
21552   case X86::CMOV_V4F32:
21553   case X86::CMOV_V4F64:
21554   case X86::CMOV_V4I64:
21555   case X86::CMOV_V16F32:
21556   case X86::CMOV_V8F32:
21557   case X86::CMOV_V8F64:
21558   case X86::CMOV_V8I64:
21559   case X86::CMOV_V8I1:
21560   case X86::CMOV_V16I1:
21561   case X86::CMOV_V32I1:
21562   case X86::CMOV_V64I1:
21563     return true;
21564
21565   default:
21566     return false;
21567   }
21568 }
21569
21570 MachineBasicBlock *
21571 X86TargetLowering::EmitLoweredSelect(MachineInstr *MI,
21572                                      MachineBasicBlock *BB) const {
21573   const TargetInstrInfo *TII = Subtarget->getInstrInfo();
21574   DebugLoc DL = MI->getDebugLoc();
21575
21576   // To "insert" a SELECT_CC instruction, we actually have to insert the
21577   // diamond control-flow pattern.  The incoming instruction knows the
21578   // destination vreg to set, the condition code register to branch on, the
21579   // true/false values to select between, and a branch opcode to use.
21580   const BasicBlock *LLVM_BB = BB->getBasicBlock();
21581   MachineFunction::iterator It = ++BB->getIterator();
21582
21583   //  thisMBB:
21584   //  ...
21585   //   TrueVal = ...
21586   //   cmpTY ccX, r1, r2
21587   //   bCC copy1MBB
21588   //   fallthrough --> copy0MBB
21589   MachineBasicBlock *thisMBB = BB;
21590   MachineFunction *F = BB->getParent();
21591
21592   // This code lowers all pseudo-CMOV instructions. Generally it lowers these
21593   // as described above, by inserting a BB, and then making a PHI at the join
21594   // point to select the true and false operands of the CMOV in the PHI.
21595   //
21596   // The code also handles two different cases of multiple CMOV opcodes
21597   // in a row.
21598   //
21599   // Case 1:
21600   // In this case, there are multiple CMOVs in a row, all which are based on
21601   // the same condition setting (or the exact opposite condition setting).
21602   // In this case we can lower all the CMOVs using a single inserted BB, and
21603   // then make a number of PHIs at the join point to model the CMOVs. The only
21604   // trickiness here, is that in a case like:
21605   //
21606   // t2 = CMOV cond1 t1, f1
21607   // t3 = CMOV cond1 t2, f2
21608   //
21609   // when rewriting this into PHIs, we have to perform some renaming on the
21610   // temps since you cannot have a PHI operand refer to a PHI result earlier
21611   // in the same block.  The "simple" but wrong lowering would be:
21612   //
21613   // t2 = PHI t1(BB1), f1(BB2)
21614   // t3 = PHI t2(BB1), f2(BB2)
21615   //
21616   // but clearly t2 is not defined in BB1, so that is incorrect. The proper
21617   // renaming is to note that on the path through BB1, t2 is really just a
21618   // copy of t1, and do that renaming, properly generating:
21619   //
21620   // t2 = PHI t1(BB1), f1(BB2)
21621   // t3 = PHI t1(BB1), f2(BB2)
21622   //
21623   // Case 2, we lower cascaded CMOVs such as
21624   //
21625   //   (CMOV (CMOV F, T, cc1), T, cc2)
21626   //
21627   // to two successives branches.  For that, we look for another CMOV as the
21628   // following instruction.
21629   //
21630   // Without this, we would add a PHI between the two jumps, which ends up
21631   // creating a few copies all around. For instance, for
21632   //
21633   //    (sitofp (zext (fcmp une)))
21634   //
21635   // we would generate:
21636   //
21637   //         ucomiss %xmm1, %xmm0
21638   //         movss  <1.0f>, %xmm0
21639   //         movaps  %xmm0, %xmm1
21640   //         jne     .LBB5_2
21641   //         xorps   %xmm1, %xmm1
21642   // .LBB5_2:
21643   //         jp      .LBB5_4
21644   //         movaps  %xmm1, %xmm0
21645   // .LBB5_4:
21646   //         retq
21647   //
21648   // because this custom-inserter would have generated:
21649   //
21650   //   A
21651   //   | \
21652   //   |  B
21653   //   | /
21654   //   C
21655   //   | \
21656   //   |  D
21657   //   | /
21658   //   E
21659   //
21660   // A: X = ...; Y = ...
21661   // B: empty
21662   // C: Z = PHI [X, A], [Y, B]
21663   // D: empty
21664   // E: PHI [X, C], [Z, D]
21665   //
21666   // If we lower both CMOVs in a single step, we can instead generate:
21667   //
21668   //   A
21669   //   | \
21670   //   |  C
21671   //   | /|
21672   //   |/ |
21673   //   |  |
21674   //   |  D
21675   //   | /
21676   //   E
21677   //
21678   // A: X = ...; Y = ...
21679   // D: empty
21680   // E: PHI [X, A], [X, C], [Y, D]
21681   //
21682   // Which, in our sitofp/fcmp example, gives us something like:
21683   //
21684   //         ucomiss %xmm1, %xmm0
21685   //         movss  <1.0f>, %xmm0
21686   //         jne     .LBB5_4
21687   //         jp      .LBB5_4
21688   //         xorps   %xmm0, %xmm0
21689   // .LBB5_4:
21690   //         retq
21691   //
21692   MachineInstr *CascadedCMOV = nullptr;
21693   MachineInstr *LastCMOV = MI;
21694   X86::CondCode CC = X86::CondCode(MI->getOperand(3).getImm());
21695   X86::CondCode OppCC = X86::GetOppositeBranchCondition(CC);
21696   MachineBasicBlock::iterator NextMIIt =
21697       std::next(MachineBasicBlock::iterator(MI));
21698
21699   // Check for case 1, where there are multiple CMOVs with the same condition
21700   // first.  Of the two cases of multiple CMOV lowerings, case 1 reduces the
21701   // number of jumps the most.
21702
21703   if (isCMOVPseudo(MI)) {
21704     // See if we have a string of CMOVS with the same condition.
21705     while (NextMIIt != BB->end() &&
21706            isCMOVPseudo(NextMIIt) &&
21707            (NextMIIt->getOperand(3).getImm() == CC ||
21708             NextMIIt->getOperand(3).getImm() == OppCC)) {
21709       LastCMOV = &*NextMIIt;
21710       ++NextMIIt;
21711     }
21712   }
21713
21714   // This checks for case 2, but only do this if we didn't already find
21715   // case 1, as indicated by LastCMOV == MI.
21716   if (LastCMOV == MI &&
21717       NextMIIt != BB->end() && NextMIIt->getOpcode() == MI->getOpcode() &&
21718       NextMIIt->getOperand(2).getReg() == MI->getOperand(2).getReg() &&
21719       NextMIIt->getOperand(1).getReg() == MI->getOperand(0).getReg()) {
21720     CascadedCMOV = &*NextMIIt;
21721   }
21722
21723   MachineBasicBlock *jcc1MBB = nullptr;
21724
21725   // If we have a cascaded CMOV, we lower it to two successive branches to
21726   // the same block.  EFLAGS is used by both, so mark it as live in the second.
21727   if (CascadedCMOV) {
21728     jcc1MBB = F->CreateMachineBasicBlock(LLVM_BB);
21729     F->insert(It, jcc1MBB);
21730     jcc1MBB->addLiveIn(X86::EFLAGS);
21731   }
21732
21733   MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
21734   MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
21735   F->insert(It, copy0MBB);
21736   F->insert(It, sinkMBB);
21737
21738   // If the EFLAGS register isn't dead in the terminator, then claim that it's
21739   // live into the sink and copy blocks.
21740   const TargetRegisterInfo *TRI = Subtarget->getRegisterInfo();
21741
21742   MachineInstr *LastEFLAGSUser = CascadedCMOV ? CascadedCMOV : LastCMOV;
21743   if (!LastEFLAGSUser->killsRegister(X86::EFLAGS) &&
21744       !checkAndUpdateEFLAGSKill(LastEFLAGSUser, BB, TRI)) {
21745     copy0MBB->addLiveIn(X86::EFLAGS);
21746     sinkMBB->addLiveIn(X86::EFLAGS);
21747   }
21748
21749   // Transfer the remainder of BB and its successor edges to sinkMBB.
21750   sinkMBB->splice(sinkMBB->begin(), BB,
21751                   std::next(MachineBasicBlock::iterator(LastCMOV)), BB->end());
21752   sinkMBB->transferSuccessorsAndUpdatePHIs(BB);
21753
21754   // Add the true and fallthrough blocks as its successors.
21755   if (CascadedCMOV) {
21756     // The fallthrough block may be jcc1MBB, if we have a cascaded CMOV.
21757     BB->addSuccessor(jcc1MBB);
21758
21759     // In that case, jcc1MBB will itself fallthrough the copy0MBB, and
21760     // jump to the sinkMBB.
21761     jcc1MBB->addSuccessor(copy0MBB);
21762     jcc1MBB->addSuccessor(sinkMBB);
21763   } else {
21764     BB->addSuccessor(copy0MBB);
21765   }
21766
21767   // The true block target of the first (or only) branch is always sinkMBB.
21768   BB->addSuccessor(sinkMBB);
21769
21770   // Create the conditional branch instruction.
21771   unsigned Opc = X86::GetCondBranchFromCond(CC);
21772   BuildMI(BB, DL, TII->get(Opc)).addMBB(sinkMBB);
21773
21774   if (CascadedCMOV) {
21775     unsigned Opc2 = X86::GetCondBranchFromCond(
21776         (X86::CondCode)CascadedCMOV->getOperand(3).getImm());
21777     BuildMI(jcc1MBB, DL, TII->get(Opc2)).addMBB(sinkMBB);
21778   }
21779
21780   //  copy0MBB:
21781   //   %FalseValue = ...
21782   //   # fallthrough to sinkMBB
21783   copy0MBB->addSuccessor(sinkMBB);
21784
21785   //  sinkMBB:
21786   //   %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
21787   //  ...
21788   MachineBasicBlock::iterator MIItBegin = MachineBasicBlock::iterator(MI);
21789   MachineBasicBlock::iterator MIItEnd =
21790     std::next(MachineBasicBlock::iterator(LastCMOV));
21791   MachineBasicBlock::iterator SinkInsertionPoint = sinkMBB->begin();
21792   DenseMap<unsigned, std::pair<unsigned, unsigned>> RegRewriteTable;
21793   MachineInstrBuilder MIB;
21794
21795   // As we are creating the PHIs, we have to be careful if there is more than
21796   // one.  Later CMOVs may reference the results of earlier CMOVs, but later
21797   // PHIs have to reference the individual true/false inputs from earlier PHIs.
21798   // That also means that PHI construction must work forward from earlier to
21799   // later, and that the code must maintain a mapping from earlier PHI's
21800   // destination registers, and the registers that went into the PHI.
21801
21802   for (MachineBasicBlock::iterator MIIt = MIItBegin; MIIt != MIItEnd; ++MIIt) {
21803     unsigned DestReg = MIIt->getOperand(0).getReg();
21804     unsigned Op1Reg = MIIt->getOperand(1).getReg();
21805     unsigned Op2Reg = MIIt->getOperand(2).getReg();
21806
21807     // If this CMOV we are generating is the opposite condition from
21808     // the jump we generated, then we have to swap the operands for the
21809     // PHI that is going to be generated.
21810     if (MIIt->getOperand(3).getImm() == OppCC)
21811         std::swap(Op1Reg, Op2Reg);
21812
21813     if (RegRewriteTable.find(Op1Reg) != RegRewriteTable.end())
21814       Op1Reg = RegRewriteTable[Op1Reg].first;
21815
21816     if (RegRewriteTable.find(Op2Reg) != RegRewriteTable.end())
21817       Op2Reg = RegRewriteTable[Op2Reg].second;
21818
21819     MIB = BuildMI(*sinkMBB, SinkInsertionPoint, DL,
21820                   TII->get(X86::PHI), DestReg)
21821           .addReg(Op1Reg).addMBB(copy0MBB)
21822           .addReg(Op2Reg).addMBB(thisMBB);
21823
21824     // Add this PHI to the rewrite table.
21825     RegRewriteTable[DestReg] = std::make_pair(Op1Reg, Op2Reg);
21826   }
21827
21828   // If we have a cascaded CMOV, the second Jcc provides the same incoming
21829   // value as the first Jcc (the True operand of the SELECT_CC/CMOV nodes).
21830   if (CascadedCMOV) {
21831     MIB.addReg(MI->getOperand(2).getReg()).addMBB(jcc1MBB);
21832     // Copy the PHI result to the register defined by the second CMOV.
21833     BuildMI(*sinkMBB, std::next(MachineBasicBlock::iterator(MIB.getInstr())),
21834             DL, TII->get(TargetOpcode::COPY),
21835             CascadedCMOV->getOperand(0).getReg())
21836         .addReg(MI->getOperand(0).getReg());
21837     CascadedCMOV->eraseFromParent();
21838   }
21839
21840   // Now remove the CMOV(s).
21841   for (MachineBasicBlock::iterator MIIt = MIItBegin; MIIt != MIItEnd; )
21842     (MIIt++)->eraseFromParent();
21843
21844   return sinkMBB;
21845 }
21846
21847 MachineBasicBlock *
21848 X86TargetLowering::EmitLoweredAtomicFP(MachineInstr *MI,
21849                                        MachineBasicBlock *BB) const {
21850   // Combine the following atomic floating-point modification pattern:
21851   //   a.store(reg OP a.load(acquire), release)
21852   // Transform them into:
21853   //   OPss (%gpr), %xmm
21854   //   movss %xmm, (%gpr)
21855   // Or sd equivalent for 64-bit operations.
21856   unsigned MOp, FOp;
21857   switch (MI->getOpcode()) {
21858   default: llvm_unreachable("unexpected instr type for EmitLoweredAtomicFP");
21859   case X86::RELEASE_FADD32mr: MOp = X86::MOVSSmr; FOp = X86::ADDSSrm; break;
21860   case X86::RELEASE_FADD64mr: MOp = X86::MOVSDmr; FOp = X86::ADDSDrm; break;
21861   }
21862   const X86InstrInfo *TII = Subtarget->getInstrInfo();
21863   DebugLoc DL = MI->getDebugLoc();
21864   MachineRegisterInfo &MRI = BB->getParent()->getRegInfo();
21865   MachineOperand MSrc = MI->getOperand(0);
21866   unsigned VSrc = MI->getOperand(5).getReg();
21867   const MachineOperand &Disp = MI->getOperand(3);
21868   MachineOperand ZeroDisp = MachineOperand::CreateImm(0);
21869   bool hasDisp = Disp.isGlobal() || Disp.isImm();
21870   if (hasDisp && MSrc.isReg())
21871     MSrc.setIsKill(false);
21872   MachineInstrBuilder MIM = BuildMI(*BB, MI, DL, TII->get(MOp))
21873                                 .addOperand(/*Base=*/MSrc)
21874                                 .addImm(/*Scale=*/1)
21875                                 .addReg(/*Index=*/0)
21876                                 .addDisp(hasDisp ? Disp : ZeroDisp, /*off=*/0)
21877                                 .addReg(0);
21878   MachineInstr *MIO = BuildMI(*BB, (MachineInstr *)MIM, DL, TII->get(FOp),
21879                               MRI.createVirtualRegister(MRI.getRegClass(VSrc)))
21880                           .addReg(VSrc)
21881                           .addOperand(/*Base=*/MSrc)
21882                           .addImm(/*Scale=*/1)
21883                           .addReg(/*Index=*/0)
21884                           .addDisp(hasDisp ? Disp : ZeroDisp, /*off=*/0)
21885                           .addReg(/*Segment=*/0);
21886   MIM.addReg(MIO->getOperand(0).getReg(), RegState::Kill);
21887   MI->eraseFromParent(); // The pseudo instruction is gone now.
21888   return BB;
21889 }
21890
21891 MachineBasicBlock *
21892 X86TargetLowering::EmitLoweredSegAlloca(MachineInstr *MI,
21893                                         MachineBasicBlock *BB) const {
21894   MachineFunction *MF = BB->getParent();
21895   const TargetInstrInfo *TII = Subtarget->getInstrInfo();
21896   DebugLoc DL = MI->getDebugLoc();
21897   const BasicBlock *LLVM_BB = BB->getBasicBlock();
21898
21899   assert(MF->shouldSplitStack());
21900
21901   const bool Is64Bit = Subtarget->is64Bit();
21902   const bool IsLP64 = Subtarget->isTarget64BitLP64();
21903
21904   const unsigned TlsReg = Is64Bit ? X86::FS : X86::GS;
21905   const unsigned TlsOffset = IsLP64 ? 0x70 : Is64Bit ? 0x40 : 0x30;
21906
21907   // BB:
21908   //  ... [Till the alloca]
21909   // If stacklet is not large enough, jump to mallocMBB
21910   //
21911   // bumpMBB:
21912   //  Allocate by subtracting from RSP
21913   //  Jump to continueMBB
21914   //
21915   // mallocMBB:
21916   //  Allocate by call to runtime
21917   //
21918   // continueMBB:
21919   //  ...
21920   //  [rest of original BB]
21921   //
21922
21923   MachineBasicBlock *mallocMBB = MF->CreateMachineBasicBlock(LLVM_BB);
21924   MachineBasicBlock *bumpMBB = MF->CreateMachineBasicBlock(LLVM_BB);
21925   MachineBasicBlock *continueMBB = MF->CreateMachineBasicBlock(LLVM_BB);
21926
21927   MachineRegisterInfo &MRI = MF->getRegInfo();
21928   const TargetRegisterClass *AddrRegClass =
21929       getRegClassFor(getPointerTy(MF->getDataLayout()));
21930
21931   unsigned mallocPtrVReg = MRI.createVirtualRegister(AddrRegClass),
21932     bumpSPPtrVReg = MRI.createVirtualRegister(AddrRegClass),
21933     tmpSPVReg = MRI.createVirtualRegister(AddrRegClass),
21934     SPLimitVReg = MRI.createVirtualRegister(AddrRegClass),
21935     sizeVReg = MI->getOperand(1).getReg(),
21936     physSPReg = IsLP64 || Subtarget->isTargetNaCl64() ? X86::RSP : X86::ESP;
21937
21938   MachineFunction::iterator MBBIter = ++BB->getIterator();
21939
21940   MF->insert(MBBIter, bumpMBB);
21941   MF->insert(MBBIter, mallocMBB);
21942   MF->insert(MBBIter, continueMBB);
21943
21944   continueMBB->splice(continueMBB->begin(), BB,
21945                       std::next(MachineBasicBlock::iterator(MI)), BB->end());
21946   continueMBB->transferSuccessorsAndUpdatePHIs(BB);
21947
21948   // Add code to the main basic block to check if the stack limit has been hit,
21949   // and if so, jump to mallocMBB otherwise to bumpMBB.
21950   BuildMI(BB, DL, TII->get(TargetOpcode::COPY), tmpSPVReg).addReg(physSPReg);
21951   BuildMI(BB, DL, TII->get(IsLP64 ? X86::SUB64rr:X86::SUB32rr), SPLimitVReg)
21952     .addReg(tmpSPVReg).addReg(sizeVReg);
21953   BuildMI(BB, DL, TII->get(IsLP64 ? X86::CMP64mr:X86::CMP32mr))
21954     .addReg(0).addImm(1).addReg(0).addImm(TlsOffset).addReg(TlsReg)
21955     .addReg(SPLimitVReg);
21956   BuildMI(BB, DL, TII->get(X86::JG_1)).addMBB(mallocMBB);
21957
21958   // bumpMBB simply decreases the stack pointer, since we know the current
21959   // stacklet has enough space.
21960   BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), physSPReg)
21961     .addReg(SPLimitVReg);
21962   BuildMI(bumpMBB, DL, TII->get(TargetOpcode::COPY), bumpSPPtrVReg)
21963     .addReg(SPLimitVReg);
21964   BuildMI(bumpMBB, DL, TII->get(X86::JMP_1)).addMBB(continueMBB);
21965
21966   // Calls into a routine in libgcc to allocate more space from the heap.
21967   const uint32_t *RegMask =
21968       Subtarget->getRegisterInfo()->getCallPreservedMask(*MF, CallingConv::C);
21969   if (IsLP64) {
21970     BuildMI(mallocMBB, DL, TII->get(X86::MOV64rr), X86::RDI)
21971       .addReg(sizeVReg);
21972     BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
21973       .addExternalSymbol("__morestack_allocate_stack_space")
21974       .addRegMask(RegMask)
21975       .addReg(X86::RDI, RegState::Implicit)
21976       .addReg(X86::RAX, RegState::ImplicitDefine);
21977   } else if (Is64Bit) {
21978     BuildMI(mallocMBB, DL, TII->get(X86::MOV32rr), X86::EDI)
21979       .addReg(sizeVReg);
21980     BuildMI(mallocMBB, DL, TII->get(X86::CALL64pcrel32))
21981       .addExternalSymbol("__morestack_allocate_stack_space")
21982       .addRegMask(RegMask)
21983       .addReg(X86::EDI, RegState::Implicit)
21984       .addReg(X86::EAX, RegState::ImplicitDefine);
21985   } else {
21986     BuildMI(mallocMBB, DL, TII->get(X86::SUB32ri), physSPReg).addReg(physSPReg)
21987       .addImm(12);
21988     BuildMI(mallocMBB, DL, TII->get(X86::PUSH32r)).addReg(sizeVReg);
21989     BuildMI(mallocMBB, DL, TII->get(X86::CALLpcrel32))
21990       .addExternalSymbol("__morestack_allocate_stack_space")
21991       .addRegMask(RegMask)
21992       .addReg(X86::EAX, RegState::ImplicitDefine);
21993   }
21994
21995   if (!Is64Bit)
21996     BuildMI(mallocMBB, DL, TII->get(X86::ADD32ri), physSPReg).addReg(physSPReg)
21997       .addImm(16);
21998
21999   BuildMI(mallocMBB, DL, TII->get(TargetOpcode::COPY), mallocPtrVReg)
22000     .addReg(IsLP64 ? X86::RAX : X86::EAX);
22001   BuildMI(mallocMBB, DL, TII->get(X86::JMP_1)).addMBB(continueMBB);
22002
22003   // Set up the CFG correctly.
22004   BB->addSuccessor(bumpMBB);
22005   BB->addSuccessor(mallocMBB);
22006   mallocMBB->addSuccessor(continueMBB);
22007   bumpMBB->addSuccessor(continueMBB);
22008
22009   // Take care of the PHI nodes.
22010   BuildMI(*continueMBB, continueMBB->begin(), DL, TII->get(X86::PHI),
22011           MI->getOperand(0).getReg())
22012     .addReg(mallocPtrVReg).addMBB(mallocMBB)
22013     .addReg(bumpSPPtrVReg).addMBB(bumpMBB);
22014
22015   // Delete the original pseudo instruction.
22016   MI->eraseFromParent();
22017
22018   // And we're done.
22019   return continueMBB;
22020 }
22021
22022 MachineBasicBlock *
22023 X86TargetLowering::EmitLoweredWinAlloca(MachineInstr *MI,
22024                                         MachineBasicBlock *BB) const {
22025   assert(!Subtarget->isTargetMachO());
22026   DebugLoc DL = MI->getDebugLoc();
22027   MachineInstr *ResumeMI = Subtarget->getFrameLowering()->emitStackProbe(
22028       *BB->getParent(), *BB, MI, DL, false);
22029   MachineBasicBlock *ResumeBB = ResumeMI->getParent();
22030   MI->eraseFromParent(); // The pseudo instruction is gone now.
22031   return ResumeBB;
22032 }
22033
22034 MachineBasicBlock *
22035 X86TargetLowering::EmitLoweredCatchRet(MachineInstr *MI,
22036                                        MachineBasicBlock *BB) const {
22037   MachineFunction *MF = BB->getParent();
22038   const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
22039   MachineBasicBlock *TargetMBB = MI->getOperand(0).getMBB();
22040   DebugLoc DL = MI->getDebugLoc();
22041
22042   assert(!isAsynchronousEHPersonality(
22043              classifyEHPersonality(MF->getFunction()->getPersonalityFn())) &&
22044          "SEH does not use catchret!");
22045
22046   // Only 32-bit EH needs to worry about manually restoring stack pointers.
22047   if (!Subtarget->is32Bit())
22048     return BB;
22049
22050   // C++ EH creates a new target block to hold the restore code, and wires up
22051   // the new block to the return destination with a normal JMP_4.
22052   MachineBasicBlock *RestoreMBB =
22053       MF->CreateMachineBasicBlock(BB->getBasicBlock());
22054   assert(BB->succ_size() == 1);
22055   MF->insert(std::next(BB->getIterator()), RestoreMBB);
22056   RestoreMBB->transferSuccessorsAndUpdatePHIs(BB);
22057   BB->addSuccessor(RestoreMBB);
22058   MI->getOperand(0).setMBB(RestoreMBB);
22059
22060   auto RestoreMBBI = RestoreMBB->begin();
22061   BuildMI(*RestoreMBB, RestoreMBBI, DL, TII.get(X86::EH_RESTORE));
22062   BuildMI(*RestoreMBB, RestoreMBBI, DL, TII.get(X86::JMP_4)).addMBB(TargetMBB);
22063   return BB;
22064 }
22065
22066 MachineBasicBlock *
22067 X86TargetLowering::EmitLoweredCatchPad(MachineInstr *MI,
22068                                        MachineBasicBlock *BB) const {
22069   MachineFunction *MF = BB->getParent();
22070   const Constant *PerFn = MF->getFunction()->getPersonalityFn();
22071   bool IsSEH = isAsynchronousEHPersonality(classifyEHPersonality(PerFn));
22072   // Only 32-bit SEH requires special handling for catchpad.
22073   if (IsSEH && Subtarget->is32Bit()) {
22074     const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
22075     DebugLoc DL = MI->getDebugLoc();
22076     BuildMI(*BB, MI, DL, TII.get(X86::EH_RESTORE));
22077   }
22078   MI->eraseFromParent();
22079   return BB;
22080 }
22081
22082 MachineBasicBlock *
22083 X86TargetLowering::EmitLoweredTLSCall(MachineInstr *MI,
22084                                       MachineBasicBlock *BB) const {
22085   // This is pretty easy.  We're taking the value that we received from
22086   // our load from the relocation, sticking it in either RDI (x86-64)
22087   // or EAX and doing an indirect call.  The return value will then
22088   // be in the normal return register.
22089   MachineFunction *F = BB->getParent();
22090   const X86InstrInfo *TII = Subtarget->getInstrInfo();
22091   DebugLoc DL = MI->getDebugLoc();
22092
22093   assert(Subtarget->isTargetDarwin() && "Darwin only instr emitted?");
22094   assert(MI->getOperand(3).isGlobal() && "This should be a global");
22095
22096   // Get a register mask for the lowered call.
22097   // FIXME: The 32-bit calls have non-standard calling conventions. Use a
22098   // proper register mask.
22099   const uint32_t *RegMask =
22100       Subtarget->is64Bit() ?
22101       Subtarget->getRegisterInfo()->getDarwinTLSCallPreservedMask() :
22102       Subtarget->getRegisterInfo()->getCallPreservedMask(*F, CallingConv::C);
22103   if (Subtarget->is64Bit()) {
22104     MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
22105                                       TII->get(X86::MOV64rm), X86::RDI)
22106     .addReg(X86::RIP)
22107     .addImm(0).addReg(0)
22108     .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
22109                       MI->getOperand(3).getTargetFlags())
22110     .addReg(0);
22111     MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL64m));
22112     addDirectMem(MIB, X86::RDI);
22113     MIB.addReg(X86::RAX, RegState::ImplicitDefine).addRegMask(RegMask);
22114   } else if (F->getTarget().getRelocationModel() != Reloc::PIC_) {
22115     MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
22116                                       TII->get(X86::MOV32rm), X86::EAX)
22117     .addReg(0)
22118     .addImm(0).addReg(0)
22119     .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
22120                       MI->getOperand(3).getTargetFlags())
22121     .addReg(0);
22122     MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
22123     addDirectMem(MIB, X86::EAX);
22124     MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
22125   } else {
22126     MachineInstrBuilder MIB = BuildMI(*BB, MI, DL,
22127                                       TII->get(X86::MOV32rm), X86::EAX)
22128     .addReg(TII->getGlobalBaseReg(F))
22129     .addImm(0).addReg(0)
22130     .addGlobalAddress(MI->getOperand(3).getGlobal(), 0,
22131                       MI->getOperand(3).getTargetFlags())
22132     .addReg(0);
22133     MIB = BuildMI(*BB, MI, DL, TII->get(X86::CALL32m));
22134     addDirectMem(MIB, X86::EAX);
22135     MIB.addReg(X86::EAX, RegState::ImplicitDefine).addRegMask(RegMask);
22136   }
22137
22138   MI->eraseFromParent(); // The pseudo instruction is gone now.
22139   return BB;
22140 }
22141
22142 MachineBasicBlock *
22143 X86TargetLowering::emitEHSjLjSetJmp(MachineInstr *MI,
22144                                     MachineBasicBlock *MBB) const {
22145   DebugLoc DL = MI->getDebugLoc();
22146   MachineFunction *MF = MBB->getParent();
22147   const TargetInstrInfo *TII = Subtarget->getInstrInfo();
22148   MachineRegisterInfo &MRI = MF->getRegInfo();
22149
22150   const BasicBlock *BB = MBB->getBasicBlock();
22151   MachineFunction::iterator I = ++MBB->getIterator();
22152
22153   // Memory Reference
22154   MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
22155   MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
22156
22157   unsigned DstReg;
22158   unsigned MemOpndSlot = 0;
22159
22160   unsigned CurOp = 0;
22161
22162   DstReg = MI->getOperand(CurOp++).getReg();
22163   const TargetRegisterClass *RC = MRI.getRegClass(DstReg);
22164   assert(RC->hasType(MVT::i32) && "Invalid destination!");
22165   unsigned mainDstReg = MRI.createVirtualRegister(RC);
22166   unsigned restoreDstReg = MRI.createVirtualRegister(RC);
22167
22168   MemOpndSlot = CurOp;
22169
22170   MVT PVT = getPointerTy(MF->getDataLayout());
22171   assert((PVT == MVT::i64 || PVT == MVT::i32) &&
22172          "Invalid Pointer Size!");
22173
22174   // For v = setjmp(buf), we generate
22175   //
22176   // thisMBB:
22177   //  buf[LabelOffset] = restoreMBB <-- takes address of restoreMBB
22178   //  SjLjSetup restoreMBB
22179   //
22180   // mainMBB:
22181   //  v_main = 0
22182   //
22183   // sinkMBB:
22184   //  v = phi(main, restore)
22185   //
22186   // restoreMBB:
22187   //  if base pointer being used, load it from frame
22188   //  v_restore = 1
22189
22190   MachineBasicBlock *thisMBB = MBB;
22191   MachineBasicBlock *mainMBB = MF->CreateMachineBasicBlock(BB);
22192   MachineBasicBlock *sinkMBB = MF->CreateMachineBasicBlock(BB);
22193   MachineBasicBlock *restoreMBB = MF->CreateMachineBasicBlock(BB);
22194   MF->insert(I, mainMBB);
22195   MF->insert(I, sinkMBB);
22196   MF->push_back(restoreMBB);
22197   restoreMBB->setHasAddressTaken();
22198
22199   MachineInstrBuilder MIB;
22200
22201   // Transfer the remainder of BB and its successor edges to sinkMBB.
22202   sinkMBB->splice(sinkMBB->begin(), MBB,
22203                   std::next(MachineBasicBlock::iterator(MI)), MBB->end());
22204   sinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
22205
22206   // thisMBB:
22207   unsigned PtrStoreOpc = 0;
22208   unsigned LabelReg = 0;
22209   const int64_t LabelOffset = 1 * PVT.getStoreSize();
22210   Reloc::Model RM = MF->getTarget().getRelocationModel();
22211   bool UseImmLabel = (MF->getTarget().getCodeModel() == CodeModel::Small) &&
22212                      (RM == Reloc::Static || RM == Reloc::DynamicNoPIC);
22213
22214   // Prepare IP either in reg or imm.
22215   if (!UseImmLabel) {
22216     PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mr : X86::MOV32mr;
22217     const TargetRegisterClass *PtrRC = getRegClassFor(PVT);
22218     LabelReg = MRI.createVirtualRegister(PtrRC);
22219     if (Subtarget->is64Bit()) {
22220       MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA64r), LabelReg)
22221               .addReg(X86::RIP)
22222               .addImm(0)
22223               .addReg(0)
22224               .addMBB(restoreMBB)
22225               .addReg(0);
22226     } else {
22227       const X86InstrInfo *XII = static_cast<const X86InstrInfo*>(TII);
22228       MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::LEA32r), LabelReg)
22229               .addReg(XII->getGlobalBaseReg(MF))
22230               .addImm(0)
22231               .addReg(0)
22232               .addMBB(restoreMBB, Subtarget->ClassifyBlockAddressReference())
22233               .addReg(0);
22234     }
22235   } else
22236     PtrStoreOpc = (PVT == MVT::i64) ? X86::MOV64mi32 : X86::MOV32mi;
22237   // Store IP
22238   MIB = BuildMI(*thisMBB, MI, DL, TII->get(PtrStoreOpc));
22239   for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
22240     if (i == X86::AddrDisp)
22241       MIB.addDisp(MI->getOperand(MemOpndSlot + i), LabelOffset);
22242     else
22243       MIB.addOperand(MI->getOperand(MemOpndSlot + i));
22244   }
22245   if (!UseImmLabel)
22246     MIB.addReg(LabelReg);
22247   else
22248     MIB.addMBB(restoreMBB);
22249   MIB.setMemRefs(MMOBegin, MMOEnd);
22250   // Setup
22251   MIB = BuildMI(*thisMBB, MI, DL, TII->get(X86::EH_SjLj_Setup))
22252           .addMBB(restoreMBB);
22253
22254   const X86RegisterInfo *RegInfo = Subtarget->getRegisterInfo();
22255   MIB.addRegMask(RegInfo->getNoPreservedMask());
22256   thisMBB->addSuccessor(mainMBB);
22257   thisMBB->addSuccessor(restoreMBB);
22258
22259   // mainMBB:
22260   //  EAX = 0
22261   BuildMI(mainMBB, DL, TII->get(X86::MOV32r0), mainDstReg);
22262   mainMBB->addSuccessor(sinkMBB);
22263
22264   // sinkMBB:
22265   BuildMI(*sinkMBB, sinkMBB->begin(), DL,
22266           TII->get(X86::PHI), DstReg)
22267     .addReg(mainDstReg).addMBB(mainMBB)
22268     .addReg(restoreDstReg).addMBB(restoreMBB);
22269
22270   // restoreMBB:
22271   if (RegInfo->hasBasePointer(*MF)) {
22272     const bool Uses64BitFramePtr =
22273         Subtarget->isTarget64BitLP64() || Subtarget->isTargetNaCl64();
22274     X86MachineFunctionInfo *X86FI = MF->getInfo<X86MachineFunctionInfo>();
22275     X86FI->setRestoreBasePointer(MF);
22276     unsigned FramePtr = RegInfo->getFrameRegister(*MF);
22277     unsigned BasePtr = RegInfo->getBaseRegister();
22278     unsigned Opm = Uses64BitFramePtr ? X86::MOV64rm : X86::MOV32rm;
22279     addRegOffset(BuildMI(restoreMBB, DL, TII->get(Opm), BasePtr),
22280                  FramePtr, true, X86FI->getRestoreBasePointerOffset())
22281       .setMIFlag(MachineInstr::FrameSetup);
22282   }
22283   BuildMI(restoreMBB, DL, TII->get(X86::MOV32ri), restoreDstReg).addImm(1);
22284   BuildMI(restoreMBB, DL, TII->get(X86::JMP_1)).addMBB(sinkMBB);
22285   restoreMBB->addSuccessor(sinkMBB);
22286
22287   MI->eraseFromParent();
22288   return sinkMBB;
22289 }
22290
22291 MachineBasicBlock *
22292 X86TargetLowering::emitEHSjLjLongJmp(MachineInstr *MI,
22293                                      MachineBasicBlock *MBB) const {
22294   DebugLoc DL = MI->getDebugLoc();
22295   MachineFunction *MF = MBB->getParent();
22296   const TargetInstrInfo *TII = Subtarget->getInstrInfo();
22297   MachineRegisterInfo &MRI = MF->getRegInfo();
22298
22299   // Memory Reference
22300   MachineInstr::mmo_iterator MMOBegin = MI->memoperands_begin();
22301   MachineInstr::mmo_iterator MMOEnd = MI->memoperands_end();
22302
22303   MVT PVT = getPointerTy(MF->getDataLayout());
22304   assert((PVT == MVT::i64 || PVT == MVT::i32) &&
22305          "Invalid Pointer Size!");
22306
22307   const TargetRegisterClass *RC =
22308     (PVT == MVT::i64) ? &X86::GR64RegClass : &X86::GR32RegClass;
22309   unsigned Tmp = MRI.createVirtualRegister(RC);
22310   // Since FP is only updated here but NOT referenced, it's treated as GPR.
22311   const X86RegisterInfo *RegInfo = Subtarget->getRegisterInfo();
22312   unsigned FP = (PVT == MVT::i64) ? X86::RBP : X86::EBP;
22313   unsigned SP = RegInfo->getStackRegister();
22314
22315   MachineInstrBuilder MIB;
22316
22317   const int64_t LabelOffset = 1 * PVT.getStoreSize();
22318   const int64_t SPOffset = 2 * PVT.getStoreSize();
22319
22320   unsigned PtrLoadOpc = (PVT == MVT::i64) ? X86::MOV64rm : X86::MOV32rm;
22321   unsigned IJmpOpc = (PVT == MVT::i64) ? X86::JMP64r : X86::JMP32r;
22322
22323   // Reload FP
22324   MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), FP);
22325   for (unsigned i = 0; i < X86::AddrNumOperands; ++i)
22326     MIB.addOperand(MI->getOperand(i));
22327   MIB.setMemRefs(MMOBegin, MMOEnd);
22328   // Reload IP
22329   MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), Tmp);
22330   for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
22331     if (i == X86::AddrDisp)
22332       MIB.addDisp(MI->getOperand(i), LabelOffset);
22333     else
22334       MIB.addOperand(MI->getOperand(i));
22335   }
22336   MIB.setMemRefs(MMOBegin, MMOEnd);
22337   // Reload SP
22338   MIB = BuildMI(*MBB, MI, DL, TII->get(PtrLoadOpc), SP);
22339   for (unsigned i = 0; i < X86::AddrNumOperands; ++i) {
22340     if (i == X86::AddrDisp)
22341       MIB.addDisp(MI->getOperand(i), SPOffset);
22342     else
22343       MIB.addOperand(MI->getOperand(i));
22344   }
22345   MIB.setMemRefs(MMOBegin, MMOEnd);
22346   // Jump
22347   BuildMI(*MBB, MI, DL, TII->get(IJmpOpc)).addReg(Tmp);
22348
22349   MI->eraseFromParent();
22350   return MBB;
22351 }
22352
22353 // Replace 213-type (isel default) FMA3 instructions with 231-type for
22354 // accumulator loops. Writing back to the accumulator allows the coalescer
22355 // to remove extra copies in the loop.
22356 // FIXME: Do this on AVX512.  We don't support 231 variants yet (PR23937).
22357 MachineBasicBlock *
22358 X86TargetLowering::emitFMA3Instr(MachineInstr *MI,
22359                                  MachineBasicBlock *MBB) const {
22360   MachineOperand &AddendOp = MI->getOperand(3);
22361
22362   // Bail out early if the addend isn't a register - we can't switch these.
22363   if (!AddendOp.isReg())
22364     return MBB;
22365
22366   MachineFunction &MF = *MBB->getParent();
22367   MachineRegisterInfo &MRI = MF.getRegInfo();
22368
22369   // Check whether the addend is defined by a PHI:
22370   assert(MRI.hasOneDef(AddendOp.getReg()) && "Multiple defs in SSA?");
22371   MachineInstr &AddendDef = *MRI.def_instr_begin(AddendOp.getReg());
22372   if (!AddendDef.isPHI())
22373     return MBB;
22374
22375   // Look for the following pattern:
22376   // loop:
22377   //   %addend = phi [%entry, 0], [%loop, %result]
22378   //   ...
22379   //   %result<tied1> = FMA213 %m2<tied0>, %m1, %addend
22380
22381   // Replace with:
22382   //   loop:
22383   //   %addend = phi [%entry, 0], [%loop, %result]
22384   //   ...
22385   //   %result<tied1> = FMA231 %addend<tied0>, %m1, %m2
22386
22387   for (unsigned i = 1, e = AddendDef.getNumOperands(); i < e; i += 2) {
22388     assert(AddendDef.getOperand(i).isReg());
22389     MachineOperand PHISrcOp = AddendDef.getOperand(i);
22390     MachineInstr &PHISrcInst = *MRI.def_instr_begin(PHISrcOp.getReg());
22391     if (&PHISrcInst == MI) {
22392       // Found a matching instruction.
22393       unsigned NewFMAOpc = 0;
22394       switch (MI->getOpcode()) {
22395         case X86::VFMADDPDr213r: NewFMAOpc = X86::VFMADDPDr231r; break;
22396         case X86::VFMADDPSr213r: NewFMAOpc = X86::VFMADDPSr231r; break;
22397         case X86::VFMADDSDr213r: NewFMAOpc = X86::VFMADDSDr231r; break;
22398         case X86::VFMADDSSr213r: NewFMAOpc = X86::VFMADDSSr231r; break;
22399         case X86::VFMSUBPDr213r: NewFMAOpc = X86::VFMSUBPDr231r; break;
22400         case X86::VFMSUBPSr213r: NewFMAOpc = X86::VFMSUBPSr231r; break;
22401         case X86::VFMSUBSDr213r: NewFMAOpc = X86::VFMSUBSDr231r; break;
22402         case X86::VFMSUBSSr213r: NewFMAOpc = X86::VFMSUBSSr231r; break;
22403         case X86::VFNMADDPDr213r: NewFMAOpc = X86::VFNMADDPDr231r; break;
22404         case X86::VFNMADDPSr213r: NewFMAOpc = X86::VFNMADDPSr231r; break;
22405         case X86::VFNMADDSDr213r: NewFMAOpc = X86::VFNMADDSDr231r; break;
22406         case X86::VFNMADDSSr213r: NewFMAOpc = X86::VFNMADDSSr231r; break;
22407         case X86::VFNMSUBPDr213r: NewFMAOpc = X86::VFNMSUBPDr231r; break;
22408         case X86::VFNMSUBPSr213r: NewFMAOpc = X86::VFNMSUBPSr231r; break;
22409         case X86::VFNMSUBSDr213r: NewFMAOpc = X86::VFNMSUBSDr231r; break;
22410         case X86::VFNMSUBSSr213r: NewFMAOpc = X86::VFNMSUBSSr231r; break;
22411         case X86::VFMADDSUBPDr213r: NewFMAOpc = X86::VFMADDSUBPDr231r; break;
22412         case X86::VFMADDSUBPSr213r: NewFMAOpc = X86::VFMADDSUBPSr231r; break;
22413         case X86::VFMSUBADDPDr213r: NewFMAOpc = X86::VFMSUBADDPDr231r; break;
22414         case X86::VFMSUBADDPSr213r: NewFMAOpc = X86::VFMSUBADDPSr231r; break;
22415
22416         case X86::VFMADDPDr213rY: NewFMAOpc = X86::VFMADDPDr231rY; break;
22417         case X86::VFMADDPSr213rY: NewFMAOpc = X86::VFMADDPSr231rY; break;
22418         case X86::VFMSUBPDr213rY: NewFMAOpc = X86::VFMSUBPDr231rY; break;
22419         case X86::VFMSUBPSr213rY: NewFMAOpc = X86::VFMSUBPSr231rY; break;
22420         case X86::VFNMADDPDr213rY: NewFMAOpc = X86::VFNMADDPDr231rY; break;
22421         case X86::VFNMADDPSr213rY: NewFMAOpc = X86::VFNMADDPSr231rY; break;
22422         case X86::VFNMSUBPDr213rY: NewFMAOpc = X86::VFNMSUBPDr231rY; break;
22423         case X86::VFNMSUBPSr213rY: NewFMAOpc = X86::VFNMSUBPSr231rY; break;
22424         case X86::VFMADDSUBPDr213rY: NewFMAOpc = X86::VFMADDSUBPDr231rY; break;
22425         case X86::VFMADDSUBPSr213rY: NewFMAOpc = X86::VFMADDSUBPSr231rY; break;
22426         case X86::VFMSUBADDPDr213rY: NewFMAOpc = X86::VFMSUBADDPDr231rY; break;
22427         case X86::VFMSUBADDPSr213rY: NewFMAOpc = X86::VFMSUBADDPSr231rY; break;
22428         default: llvm_unreachable("Unrecognized FMA variant.");
22429       }
22430
22431       const TargetInstrInfo &TII = *Subtarget->getInstrInfo();
22432       MachineInstrBuilder MIB =
22433         BuildMI(MF, MI->getDebugLoc(), TII.get(NewFMAOpc))
22434         .addOperand(MI->getOperand(0))
22435         .addOperand(MI->getOperand(3))
22436         .addOperand(MI->getOperand(2))
22437         .addOperand(MI->getOperand(1));
22438       MBB->insert(MachineBasicBlock::iterator(MI), MIB);
22439       MI->eraseFromParent();
22440     }
22441   }
22442
22443   return MBB;
22444 }
22445
22446 MachineBasicBlock *
22447 X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
22448                                                MachineBasicBlock *BB) const {
22449   switch (MI->getOpcode()) {
22450   default: llvm_unreachable("Unexpected instr type to insert");
22451   case X86::TAILJMPd64:
22452   case X86::TAILJMPr64:
22453   case X86::TAILJMPm64:
22454   case X86::TAILJMPd64_REX:
22455   case X86::TAILJMPr64_REX:
22456   case X86::TAILJMPm64_REX:
22457     llvm_unreachable("TAILJMP64 would not be touched here.");
22458   case X86::TCRETURNdi64:
22459   case X86::TCRETURNri64:
22460   case X86::TCRETURNmi64:
22461     return BB;
22462   case X86::WIN_ALLOCA:
22463     return EmitLoweredWinAlloca(MI, BB);
22464   case X86::CATCHRET:
22465     return EmitLoweredCatchRet(MI, BB);
22466   case X86::CATCHPAD:
22467     return EmitLoweredCatchPad(MI, BB);
22468   case X86::SEG_ALLOCA_32:
22469   case X86::SEG_ALLOCA_64:
22470     return EmitLoweredSegAlloca(MI, BB);
22471   case X86::TLSCall_32:
22472   case X86::TLSCall_64:
22473     return EmitLoweredTLSCall(MI, BB);
22474   case X86::CMOV_FR32:
22475   case X86::CMOV_FR64:
22476   case X86::CMOV_FR128:
22477   case X86::CMOV_GR8:
22478   case X86::CMOV_GR16:
22479   case X86::CMOV_GR32:
22480   case X86::CMOV_RFP32:
22481   case X86::CMOV_RFP64:
22482   case X86::CMOV_RFP80:
22483   case X86::CMOV_V2F64:
22484   case X86::CMOV_V2I64:
22485   case X86::CMOV_V4F32:
22486   case X86::CMOV_V4F64:
22487   case X86::CMOV_V4I64:
22488   case X86::CMOV_V16F32:
22489   case X86::CMOV_V8F32:
22490   case X86::CMOV_V8F64:
22491   case X86::CMOV_V8I64:
22492   case X86::CMOV_V8I1:
22493   case X86::CMOV_V16I1:
22494   case X86::CMOV_V32I1:
22495   case X86::CMOV_V64I1:
22496     return EmitLoweredSelect(MI, BB);
22497
22498   case X86::RELEASE_FADD32mr:
22499   case X86::RELEASE_FADD64mr:
22500     return EmitLoweredAtomicFP(MI, BB);
22501
22502   case X86::FP32_TO_INT16_IN_MEM:
22503   case X86::FP32_TO_INT32_IN_MEM:
22504   case X86::FP32_TO_INT64_IN_MEM:
22505   case X86::FP64_TO_INT16_IN_MEM:
22506   case X86::FP64_TO_INT32_IN_MEM:
22507   case X86::FP64_TO_INT64_IN_MEM:
22508   case X86::FP80_TO_INT16_IN_MEM:
22509   case X86::FP80_TO_INT32_IN_MEM:
22510   case X86::FP80_TO_INT64_IN_MEM: {
22511     MachineFunction *F = BB->getParent();
22512     const TargetInstrInfo *TII = Subtarget->getInstrInfo();
22513     DebugLoc DL = MI->getDebugLoc();
22514
22515     // Change the floating point control register to use "round towards zero"
22516     // mode when truncating to an integer value.
22517     int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2, false);
22518     addFrameReference(BuildMI(*BB, MI, DL,
22519                               TII->get(X86::FNSTCW16m)), CWFrameIdx);
22520
22521     // Load the old value of the high byte of the control word...
22522     unsigned OldCW =
22523       F->getRegInfo().createVirtualRegister(&X86::GR16RegClass);
22524     addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16rm), OldCW),
22525                       CWFrameIdx);
22526
22527     // Set the high part to be round to zero...
22528     addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mi)), CWFrameIdx)
22529       .addImm(0xC7F);
22530
22531     // Reload the modified control word now...
22532     addFrameReference(BuildMI(*BB, MI, DL,
22533                               TII->get(X86::FLDCW16m)), CWFrameIdx);
22534
22535     // Restore the memory image of control word to original value
22536     addFrameReference(BuildMI(*BB, MI, DL, TII->get(X86::MOV16mr)), CWFrameIdx)
22537       .addReg(OldCW);
22538
22539     // Get the X86 opcode to use.
22540     unsigned Opc;
22541     switch (MI->getOpcode()) {
22542     default: llvm_unreachable("illegal opcode!");
22543     case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
22544     case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
22545     case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
22546     case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
22547     case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
22548     case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
22549     case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
22550     case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
22551     case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
22552     }
22553
22554     X86AddressMode AM;
22555     MachineOperand &Op = MI->getOperand(0);
22556     if (Op.isReg()) {
22557       AM.BaseType = X86AddressMode::RegBase;
22558       AM.Base.Reg = Op.getReg();
22559     } else {
22560       AM.BaseType = X86AddressMode::FrameIndexBase;
22561       AM.Base.FrameIndex = Op.getIndex();
22562     }
22563     Op = MI->getOperand(1);
22564     if (Op.isImm())
22565       AM.Scale = Op.getImm();
22566     Op = MI->getOperand(2);
22567     if (Op.isImm())
22568       AM.IndexReg = Op.getImm();
22569     Op = MI->getOperand(3);
22570     if (Op.isGlobal()) {
22571       AM.GV = Op.getGlobal();
22572     } else {
22573       AM.Disp = Op.getImm();
22574     }
22575     addFullAddress(BuildMI(*BB, MI, DL, TII->get(Opc)), AM)
22576                       .addReg(MI->getOperand(X86::AddrNumOperands).getReg());
22577
22578     // Reload the original control word now.
22579     addFrameReference(BuildMI(*BB, MI, DL,
22580                               TII->get(X86::FLDCW16m)), CWFrameIdx);
22581
22582     MI->eraseFromParent();   // The pseudo instruction is gone now.
22583     return BB;
22584   }
22585     // String/text processing lowering.
22586   case X86::PCMPISTRM128REG:
22587   case X86::VPCMPISTRM128REG:
22588   case X86::PCMPISTRM128MEM:
22589   case X86::VPCMPISTRM128MEM:
22590   case X86::PCMPESTRM128REG:
22591   case X86::VPCMPESTRM128REG:
22592   case X86::PCMPESTRM128MEM:
22593   case X86::VPCMPESTRM128MEM:
22594     assert(Subtarget->hasSSE42() &&
22595            "Target must have SSE4.2 or AVX features enabled");
22596     return EmitPCMPSTRM(MI, BB, Subtarget->getInstrInfo());
22597
22598   // String/text processing lowering.
22599   case X86::PCMPISTRIREG:
22600   case X86::VPCMPISTRIREG:
22601   case X86::PCMPISTRIMEM:
22602   case X86::VPCMPISTRIMEM:
22603   case X86::PCMPESTRIREG:
22604   case X86::VPCMPESTRIREG:
22605   case X86::PCMPESTRIMEM:
22606   case X86::VPCMPESTRIMEM:
22607     assert(Subtarget->hasSSE42() &&
22608            "Target must have SSE4.2 or AVX features enabled");
22609     return EmitPCMPSTRI(MI, BB, Subtarget->getInstrInfo());
22610
22611   // Thread synchronization.
22612   case X86::MONITOR:
22613     return EmitMonitor(MI, BB, Subtarget);
22614
22615   // xbegin
22616   case X86::XBEGIN:
22617     return EmitXBegin(MI, BB, Subtarget->getInstrInfo());
22618
22619   case X86::VASTART_SAVE_XMM_REGS:
22620     return EmitVAStartSaveXMMRegsWithCustomInserter(MI, BB);
22621
22622   case X86::VAARG_64:
22623     return EmitVAARG64WithCustomInserter(MI, BB);
22624
22625   case X86::EH_SjLj_SetJmp32:
22626   case X86::EH_SjLj_SetJmp64:
22627     return emitEHSjLjSetJmp(MI, BB);
22628
22629   case X86::EH_SjLj_LongJmp32:
22630   case X86::EH_SjLj_LongJmp64:
22631     return emitEHSjLjLongJmp(MI, BB);
22632
22633   case TargetOpcode::STATEPOINT:
22634     // As an implementation detail, STATEPOINT shares the STACKMAP format at
22635     // this point in the process.  We diverge later.
22636     return emitPatchPoint(MI, BB);
22637
22638   case TargetOpcode::STACKMAP:
22639   case TargetOpcode::PATCHPOINT:
22640     return emitPatchPoint(MI, BB);
22641
22642   case X86::VFMADDPDr213r:
22643   case X86::VFMADDPSr213r:
22644   case X86::VFMADDSDr213r:
22645   case X86::VFMADDSSr213r:
22646   case X86::VFMSUBPDr213r:
22647   case X86::VFMSUBPSr213r:
22648   case X86::VFMSUBSDr213r:
22649   case X86::VFMSUBSSr213r:
22650   case X86::VFNMADDPDr213r:
22651   case X86::VFNMADDPSr213r:
22652   case X86::VFNMADDSDr213r:
22653   case X86::VFNMADDSSr213r:
22654   case X86::VFNMSUBPDr213r:
22655   case X86::VFNMSUBPSr213r:
22656   case X86::VFNMSUBSDr213r:
22657   case X86::VFNMSUBSSr213r:
22658   case X86::VFMADDSUBPDr213r:
22659   case X86::VFMADDSUBPSr213r:
22660   case X86::VFMSUBADDPDr213r:
22661   case X86::VFMSUBADDPSr213r:
22662   case X86::VFMADDPDr213rY:
22663   case X86::VFMADDPSr213rY:
22664   case X86::VFMSUBPDr213rY:
22665   case X86::VFMSUBPSr213rY:
22666   case X86::VFNMADDPDr213rY:
22667   case X86::VFNMADDPSr213rY:
22668   case X86::VFNMSUBPDr213rY:
22669   case X86::VFNMSUBPSr213rY:
22670   case X86::VFMADDSUBPDr213rY:
22671   case X86::VFMADDSUBPSr213rY:
22672   case X86::VFMSUBADDPDr213rY:
22673   case X86::VFMSUBADDPSr213rY:
22674     return emitFMA3Instr(MI, BB);
22675   }
22676 }
22677
22678 //===----------------------------------------------------------------------===//
22679 //                           X86 Optimization Hooks
22680 //===----------------------------------------------------------------------===//
22681
22682 void X86TargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
22683                                                       APInt &KnownZero,
22684                                                       APInt &KnownOne,
22685                                                       const SelectionDAG &DAG,
22686                                                       unsigned Depth) const {
22687   unsigned BitWidth = KnownZero.getBitWidth();
22688   unsigned Opc = Op.getOpcode();
22689   assert((Opc >= ISD::BUILTIN_OP_END ||
22690           Opc == ISD::INTRINSIC_WO_CHAIN ||
22691           Opc == ISD::INTRINSIC_W_CHAIN ||
22692           Opc == ISD::INTRINSIC_VOID) &&
22693          "Should use MaskedValueIsZero if you don't know whether Op"
22694          " is a target node!");
22695
22696   KnownZero = KnownOne = APInt(BitWidth, 0);   // Don't know anything.
22697   switch (Opc) {
22698   default: break;
22699   case X86ISD::ADD:
22700   case X86ISD::SUB:
22701   case X86ISD::ADC:
22702   case X86ISD::SBB:
22703   case X86ISD::SMUL:
22704   case X86ISD::UMUL:
22705   case X86ISD::INC:
22706   case X86ISD::DEC:
22707   case X86ISD::OR:
22708   case X86ISD::XOR:
22709   case X86ISD::AND:
22710     // These nodes' second result is a boolean.
22711     if (Op.getResNo() == 0)
22712       break;
22713     // Fallthrough
22714   case X86ISD::SETCC:
22715     KnownZero |= APInt::getHighBitsSet(BitWidth, BitWidth - 1);
22716     break;
22717   case ISD::INTRINSIC_WO_CHAIN: {
22718     unsigned IntId = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
22719     unsigned NumLoBits = 0;
22720     switch (IntId) {
22721     default: break;
22722     case Intrinsic::x86_sse_movmsk_ps:
22723     case Intrinsic::x86_avx_movmsk_ps_256:
22724     case Intrinsic::x86_sse2_movmsk_pd:
22725     case Intrinsic::x86_avx_movmsk_pd_256:
22726     case Intrinsic::x86_mmx_pmovmskb:
22727     case Intrinsic::x86_sse2_pmovmskb_128:
22728     case Intrinsic::x86_avx2_pmovmskb: {
22729       // High bits of movmskp{s|d}, pmovmskb are known zero.
22730       switch (IntId) {
22731         default: llvm_unreachable("Impossible intrinsic");  // Can't reach here.
22732         case Intrinsic::x86_sse_movmsk_ps:      NumLoBits = 4; break;
22733         case Intrinsic::x86_avx_movmsk_ps_256:  NumLoBits = 8; break;
22734         case Intrinsic::x86_sse2_movmsk_pd:     NumLoBits = 2; break;
22735         case Intrinsic::x86_avx_movmsk_pd_256:  NumLoBits = 4; break;
22736         case Intrinsic::x86_mmx_pmovmskb:       NumLoBits = 8; break;
22737         case Intrinsic::x86_sse2_pmovmskb_128:  NumLoBits = 16; break;
22738         case Intrinsic::x86_avx2_pmovmskb:      NumLoBits = 32; break;
22739       }
22740       KnownZero = APInt::getHighBitsSet(BitWidth, BitWidth - NumLoBits);
22741       break;
22742     }
22743     }
22744     break;
22745   }
22746   }
22747 }
22748
22749 unsigned X86TargetLowering::ComputeNumSignBitsForTargetNode(
22750   SDValue Op,
22751   const SelectionDAG &,
22752   unsigned Depth) const {
22753   // SETCC_CARRY sets the dest to ~0 for true or 0 for false.
22754   if (Op.getOpcode() == X86ISD::SETCC_CARRY)
22755     return Op.getValueType().getScalarSizeInBits();
22756
22757   // Fallback case.
22758   return 1;
22759 }
22760
22761 /// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
22762 /// node is a GlobalAddress + offset.
22763 bool X86TargetLowering::isGAPlusOffset(SDNode *N,
22764                                        const GlobalValue* &GA,
22765                                        int64_t &Offset) const {
22766   if (N->getOpcode() == X86ISD::Wrapper) {
22767     if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
22768       GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
22769       Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
22770       return true;
22771     }
22772   }
22773   return TargetLowering::isGAPlusOffset(N, GA, Offset);
22774 }
22775
22776 /// PerformShuffleCombine256 - Performs shuffle combines for 256-bit vectors.
22777 /// FIXME: This could be expanded to support 512 bit vectors as well.
22778 static SDValue PerformShuffleCombine256(SDNode *N, SelectionDAG &DAG,
22779                                         TargetLowering::DAGCombinerInfo &DCI,
22780                                         const X86Subtarget* Subtarget) {
22781   SDLoc dl(N);
22782   ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
22783   SDValue V1 = SVOp->getOperand(0);
22784   SDValue V2 = SVOp->getOperand(1);
22785   MVT VT = SVOp->getSimpleValueType(0);
22786   unsigned NumElems = VT.getVectorNumElements();
22787
22788   if (V1.getOpcode() == ISD::CONCAT_VECTORS &&
22789       V2.getOpcode() == ISD::CONCAT_VECTORS) {
22790     //
22791     //                   0,0,0,...
22792     //                      |
22793     //    V      UNDEF    BUILD_VECTOR    UNDEF
22794     //     \      /           \           /
22795     //  CONCAT_VECTOR         CONCAT_VECTOR
22796     //         \                  /
22797     //          \                /
22798     //          RESULT: V + zero extended
22799     //
22800     if (V2.getOperand(0).getOpcode() != ISD::BUILD_VECTOR ||
22801         V2.getOperand(1).getOpcode() != ISD::UNDEF ||
22802         V1.getOperand(1).getOpcode() != ISD::UNDEF)
22803       return SDValue();
22804
22805     if (!ISD::isBuildVectorAllZeros(V2.getOperand(0).getNode()))
22806       return SDValue();
22807
22808     // To match the shuffle mask, the first half of the mask should
22809     // be exactly the first vector, and all the rest a splat with the
22810     // first element of the second one.
22811     for (unsigned i = 0; i != NumElems/2; ++i)
22812       if (!isUndefOrEqual(SVOp->getMaskElt(i), i) ||
22813           !isUndefOrEqual(SVOp->getMaskElt(i+NumElems/2), NumElems))
22814         return SDValue();
22815
22816     // If V1 is coming from a vector load then just fold to a VZEXT_LOAD.
22817     if (LoadSDNode *Ld = dyn_cast<LoadSDNode>(V1.getOperand(0))) {
22818       if (Ld->hasNUsesOfValue(1, 0)) {
22819         SDVTList Tys = DAG.getVTList(MVT::v4i64, MVT::Other);
22820         SDValue Ops[] = { Ld->getChain(), Ld->getBasePtr() };
22821         SDValue ResNode =
22822           DAG.getMemIntrinsicNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops,
22823                                   Ld->getMemoryVT(),
22824                                   Ld->getPointerInfo(),
22825                                   Ld->getAlignment(),
22826                                   false/*isVolatile*/, true/*ReadMem*/,
22827                                   false/*WriteMem*/);
22828
22829         // Make sure the newly-created LOAD is in the same position as Ld in
22830         // terms of dependency. We create a TokenFactor for Ld and ResNode,
22831         // and update uses of Ld's output chain to use the TokenFactor.
22832         if (Ld->hasAnyUseOfValue(1)) {
22833           SDValue NewChain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
22834                              SDValue(Ld, 1), SDValue(ResNode.getNode(), 1));
22835           DAG.ReplaceAllUsesOfValueWith(SDValue(Ld, 1), NewChain);
22836           DAG.UpdateNodeOperands(NewChain.getNode(), SDValue(Ld, 1),
22837                                  SDValue(ResNode.getNode(), 1));
22838         }
22839
22840         return DAG.getBitcast(VT, ResNode);
22841       }
22842     }
22843
22844     // Emit a zeroed vector and insert the desired subvector on its
22845     // first half.
22846     SDValue Zeros = getZeroVector(VT, Subtarget, DAG, dl);
22847     SDValue InsV = Insert128BitVector(Zeros, V1.getOperand(0), 0, DAG, dl);
22848     return DCI.CombineTo(N, InsV);
22849   }
22850
22851   return SDValue();
22852 }
22853
22854 /// \brief Combine an arbitrary chain of shuffles into a single instruction if
22855 /// possible.
22856 ///
22857 /// This is the leaf of the recursive combinine below. When we have found some
22858 /// chain of single-use x86 shuffle instructions and accumulated the combined
22859 /// shuffle mask represented by them, this will try to pattern match that mask
22860 /// into either a single instruction if there is a special purpose instruction
22861 /// for this operation, or into a PSHUFB instruction which is a fully general
22862 /// instruction but should only be used to replace chains over a certain depth.
22863 static bool combineX86ShuffleChain(SDValue Op, SDValue Root, ArrayRef<int> Mask,
22864                                    int Depth, bool HasPSHUFB, SelectionDAG &DAG,
22865                                    TargetLowering::DAGCombinerInfo &DCI,
22866                                    const X86Subtarget *Subtarget) {
22867   assert(!Mask.empty() && "Cannot combine an empty shuffle mask!");
22868
22869   // Find the operand that enters the chain. Note that multiple uses are OK
22870   // here, we're not going to remove the operand we find.
22871   SDValue Input = Op.getOperand(0);
22872   while (Input.getOpcode() == ISD::BITCAST)
22873     Input = Input.getOperand(0);
22874
22875   MVT VT = Input.getSimpleValueType();
22876   MVT RootVT = Root.getSimpleValueType();
22877   SDLoc DL(Root);
22878
22879   if (Mask.size() == 1) {
22880     int Index = Mask[0];
22881     assert((Index >= 0 || Index == SM_SentinelUndef ||
22882             Index == SM_SentinelZero) &&
22883            "Invalid shuffle index found!");
22884
22885     // We may end up with an accumulated mask of size 1 as a result of
22886     // widening of shuffle operands (see function canWidenShuffleElements).
22887     // If the only shuffle index is equal to SM_SentinelZero then propagate
22888     // a zero vector. Otherwise, the combine shuffle mask is a no-op shuffle
22889     // mask, and therefore the entire chain of shuffles can be folded away.
22890     if (Index == SM_SentinelZero)
22891       DCI.CombineTo(Root.getNode(), getZeroVector(RootVT, Subtarget, DAG, DL));
22892     else
22893       DCI.CombineTo(Root.getNode(), DAG.getBitcast(RootVT, Input),
22894                     /*AddTo*/ true);
22895     return true;
22896   }
22897
22898   // Use the float domain if the operand type is a floating point type.
22899   bool FloatDomain = VT.isFloatingPoint();
22900
22901   // For floating point shuffles, we don't have free copies in the shuffle
22902   // instructions or the ability to load as part of the instruction, so
22903   // canonicalize their shuffles to UNPCK or MOV variants.
22904   //
22905   // Note that even with AVX we prefer the PSHUFD form of shuffle for integer
22906   // vectors because it can have a load folded into it that UNPCK cannot. This
22907   // doesn't preclude something switching to the shorter encoding post-RA.
22908   //
22909   // FIXME: Should teach these routines about AVX vector widths.
22910   if (FloatDomain && VT.is128BitVector()) {
22911     if (Mask.equals({0, 0}) || Mask.equals({1, 1})) {
22912       bool Lo = Mask.equals({0, 0});
22913       unsigned Shuffle;
22914       MVT ShuffleVT;
22915       // Check if we have SSE3 which will let us use MOVDDUP. That instruction
22916       // is no slower than UNPCKLPD but has the option to fold the input operand
22917       // into even an unaligned memory load.
22918       if (Lo && Subtarget->hasSSE3()) {
22919         Shuffle = X86ISD::MOVDDUP;
22920         ShuffleVT = MVT::v2f64;
22921       } else {
22922         // We have MOVLHPS and MOVHLPS throughout SSE and they encode smaller
22923         // than the UNPCK variants.
22924         Shuffle = Lo ? X86ISD::MOVLHPS : X86ISD::MOVHLPS;
22925         ShuffleVT = MVT::v4f32;
22926       }
22927       if (Depth == 1 && Root->getOpcode() == Shuffle)
22928         return false; // Nothing to do!
22929       Op = DAG.getBitcast(ShuffleVT, Input);
22930       DCI.AddToWorklist(Op.getNode());
22931       if (Shuffle == X86ISD::MOVDDUP)
22932         Op = DAG.getNode(Shuffle, DL, ShuffleVT, Op);
22933       else
22934         Op = DAG.getNode(Shuffle, DL, ShuffleVT, Op, Op);
22935       DCI.AddToWorklist(Op.getNode());
22936       DCI.CombineTo(Root.getNode(), DAG.getBitcast(RootVT, Op),
22937                     /*AddTo*/ true);
22938       return true;
22939     }
22940     if (Subtarget->hasSSE3() &&
22941         (Mask.equals({0, 0, 2, 2}) || Mask.equals({1, 1, 3, 3}))) {
22942       bool Lo = Mask.equals({0, 0, 2, 2});
22943       unsigned Shuffle = Lo ? X86ISD::MOVSLDUP : X86ISD::MOVSHDUP;
22944       MVT ShuffleVT = MVT::v4f32;
22945       if (Depth == 1 && Root->getOpcode() == Shuffle)
22946         return false; // Nothing to do!
22947       Op = DAG.getBitcast(ShuffleVT, Input);
22948       DCI.AddToWorklist(Op.getNode());
22949       Op = DAG.getNode(Shuffle, DL, ShuffleVT, Op);
22950       DCI.AddToWorklist(Op.getNode());
22951       DCI.CombineTo(Root.getNode(), DAG.getBitcast(RootVT, Op),
22952                     /*AddTo*/ true);
22953       return true;
22954     }
22955     if (Mask.equals({0, 0, 1, 1}) || Mask.equals({2, 2, 3, 3})) {
22956       bool Lo = Mask.equals({0, 0, 1, 1});
22957       unsigned Shuffle = Lo ? X86ISD::UNPCKL : X86ISD::UNPCKH;
22958       MVT ShuffleVT = MVT::v4f32;
22959       if (Depth == 1 && Root->getOpcode() == Shuffle)
22960         return false; // Nothing to do!
22961       Op = DAG.getBitcast(ShuffleVT, Input);
22962       DCI.AddToWorklist(Op.getNode());
22963       Op = DAG.getNode(Shuffle, DL, ShuffleVT, Op, Op);
22964       DCI.AddToWorklist(Op.getNode());
22965       DCI.CombineTo(Root.getNode(), DAG.getBitcast(RootVT, Op),
22966                     /*AddTo*/ true);
22967       return true;
22968     }
22969   }
22970
22971   // We always canonicalize the 8 x i16 and 16 x i8 shuffles into their UNPCK
22972   // variants as none of these have single-instruction variants that are
22973   // superior to the UNPCK formulation.
22974   if (!FloatDomain && VT.is128BitVector() &&
22975       (Mask.equals({0, 0, 1, 1, 2, 2, 3, 3}) ||
22976        Mask.equals({4, 4, 5, 5, 6, 6, 7, 7}) ||
22977        Mask.equals({0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7}) ||
22978        Mask.equals(
22979            {8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15}))) {
22980     bool Lo = Mask[0] == 0;
22981     unsigned Shuffle = Lo ? X86ISD::UNPCKL : X86ISD::UNPCKH;
22982     if (Depth == 1 && Root->getOpcode() == Shuffle)
22983       return false; // Nothing to do!
22984     MVT ShuffleVT;
22985     switch (Mask.size()) {
22986     case 8:
22987       ShuffleVT = MVT::v8i16;
22988       break;
22989     case 16:
22990       ShuffleVT = MVT::v16i8;
22991       break;
22992     default:
22993       llvm_unreachable("Impossible mask size!");
22994     };
22995     Op = DAG.getBitcast(ShuffleVT, Input);
22996     DCI.AddToWorklist(Op.getNode());
22997     Op = DAG.getNode(Shuffle, DL, ShuffleVT, Op, Op);
22998     DCI.AddToWorklist(Op.getNode());
22999     DCI.CombineTo(Root.getNode(), DAG.getBitcast(RootVT, Op),
23000                   /*AddTo*/ true);
23001     return true;
23002   }
23003
23004   // Don't try to re-form single instruction chains under any circumstances now
23005   // that we've done encoding canonicalization for them.
23006   if (Depth < 2)
23007     return false;
23008
23009   // If we have 3 or more shuffle instructions or a chain involving PSHUFB, we
23010   // can replace them with a single PSHUFB instruction profitably. Intel's
23011   // manuals suggest only using PSHUFB if doing so replacing 5 instructions, but
23012   // in practice PSHUFB tends to be *very* fast so we're more aggressive.
23013   if ((Depth >= 3 || HasPSHUFB) && Subtarget->hasSSSE3()) {
23014     SmallVector<SDValue, 16> PSHUFBMask;
23015     int NumBytes = VT.getSizeInBits() / 8;
23016     int Ratio = NumBytes / Mask.size();
23017     for (int i = 0; i < NumBytes; ++i) {
23018       if (Mask[i / Ratio] == SM_SentinelUndef) {
23019         PSHUFBMask.push_back(DAG.getUNDEF(MVT::i8));
23020         continue;
23021       }
23022       int M = Mask[i / Ratio] != SM_SentinelZero
23023                   ? Ratio * Mask[i / Ratio] + i % Ratio
23024                   : 255;
23025       PSHUFBMask.push_back(DAG.getConstant(M, DL, MVT::i8));
23026     }
23027     MVT ByteVT = MVT::getVectorVT(MVT::i8, NumBytes);
23028     Op = DAG.getBitcast(ByteVT, Input);
23029     DCI.AddToWorklist(Op.getNode());
23030     SDValue PSHUFBMaskOp =
23031         DAG.getNode(ISD::BUILD_VECTOR, DL, ByteVT, PSHUFBMask);
23032     DCI.AddToWorklist(PSHUFBMaskOp.getNode());
23033     Op = DAG.getNode(X86ISD::PSHUFB, DL, ByteVT, Op, PSHUFBMaskOp);
23034     DCI.AddToWorklist(Op.getNode());
23035     DCI.CombineTo(Root.getNode(), DAG.getBitcast(RootVT, Op),
23036                   /*AddTo*/ true);
23037     return true;
23038   }
23039
23040   // Failed to find any combines.
23041   return false;
23042 }
23043
23044 /// \brief Fully generic combining of x86 shuffle instructions.
23045 ///
23046 /// This should be the last combine run over the x86 shuffle instructions. Once
23047 /// they have been fully optimized, this will recursively consider all chains
23048 /// of single-use shuffle instructions, build a generic model of the cumulative
23049 /// shuffle operation, and check for simpler instructions which implement this
23050 /// operation. We use this primarily for two purposes:
23051 ///
23052 /// 1) Collapse generic shuffles to specialized single instructions when
23053 ///    equivalent. In most cases, this is just an encoding size win, but
23054 ///    sometimes we will collapse multiple generic shuffles into a single
23055 ///    special-purpose shuffle.
23056 /// 2) Look for sequences of shuffle instructions with 3 or more total
23057 ///    instructions, and replace them with the slightly more expensive SSSE3
23058 ///    PSHUFB instruction if available. We do this as the last combining step
23059 ///    to ensure we avoid using PSHUFB if we can implement the shuffle with
23060 ///    a suitable short sequence of other instructions. The PHUFB will either
23061 ///    use a register or have to read from memory and so is slightly (but only
23062 ///    slightly) more expensive than the other shuffle instructions.
23063 ///
23064 /// Because this is inherently a quadratic operation (for each shuffle in
23065 /// a chain, we recurse up the chain), the depth is limited to 8 instructions.
23066 /// This should never be an issue in practice as the shuffle lowering doesn't
23067 /// produce sequences of more than 8 instructions.
23068 ///
23069 /// FIXME: We will currently miss some cases where the redundant shuffling
23070 /// would simplify under the threshold for PSHUFB formation because of
23071 /// combine-ordering. To fix this, we should do the redundant instruction
23072 /// combining in this recursive walk.
23073 static bool combineX86ShufflesRecursively(SDValue Op, SDValue Root,
23074                                           ArrayRef<int> RootMask,
23075                                           int Depth, bool HasPSHUFB,
23076                                           SelectionDAG &DAG,
23077                                           TargetLowering::DAGCombinerInfo &DCI,
23078                                           const X86Subtarget *Subtarget) {
23079   // Bound the depth of our recursive combine because this is ultimately
23080   // quadratic in nature.
23081   if (Depth > 8)
23082     return false;
23083
23084   // Directly rip through bitcasts to find the underlying operand.
23085   while (Op.getOpcode() == ISD::BITCAST && Op.getOperand(0).hasOneUse())
23086     Op = Op.getOperand(0);
23087
23088   MVT VT = Op.getSimpleValueType();
23089   if (!VT.isVector())
23090     return false; // Bail if we hit a non-vector.
23091
23092   assert(Root.getSimpleValueType().isVector() &&
23093          "Shuffles operate on vector types!");
23094   assert(VT.getSizeInBits() == Root.getSimpleValueType().getSizeInBits() &&
23095          "Can only combine shuffles of the same vector register size.");
23096
23097   if (!isTargetShuffle(Op.getOpcode()))
23098     return false;
23099   SmallVector<int, 16> OpMask;
23100   bool IsUnary;
23101   bool HaveMask = getTargetShuffleMask(Op.getNode(), VT, OpMask, IsUnary);
23102   // We only can combine unary shuffles which we can decode the mask for.
23103   if (!HaveMask || !IsUnary)
23104     return false;
23105
23106   assert(VT.getVectorNumElements() == OpMask.size() &&
23107          "Different mask size from vector size!");
23108   assert(((RootMask.size() > OpMask.size() &&
23109            RootMask.size() % OpMask.size() == 0) ||
23110           (OpMask.size() > RootMask.size() &&
23111            OpMask.size() % RootMask.size() == 0) ||
23112           OpMask.size() == RootMask.size()) &&
23113          "The smaller number of elements must divide the larger.");
23114   int RootRatio = std::max<int>(1, OpMask.size() / RootMask.size());
23115   int OpRatio = std::max<int>(1, RootMask.size() / OpMask.size());
23116   assert(((RootRatio == 1 && OpRatio == 1) ||
23117           (RootRatio == 1) != (OpRatio == 1)) &&
23118          "Must not have a ratio for both incoming and op masks!");
23119
23120   SmallVector<int, 16> Mask;
23121   Mask.reserve(std::max(OpMask.size(), RootMask.size()));
23122
23123   // Merge this shuffle operation's mask into our accumulated mask. Note that
23124   // this shuffle's mask will be the first applied to the input, followed by the
23125   // root mask to get us all the way to the root value arrangement. The reason
23126   // for this order is that we are recursing up the operation chain.
23127   for (int i = 0, e = std::max(OpMask.size(), RootMask.size()); i < e; ++i) {
23128     int RootIdx = i / RootRatio;
23129     if (RootMask[RootIdx] < 0) {
23130       // This is a zero or undef lane, we're done.
23131       Mask.push_back(RootMask[RootIdx]);
23132       continue;
23133     }
23134
23135     int RootMaskedIdx = RootMask[RootIdx] * RootRatio + i % RootRatio;
23136     int OpIdx = RootMaskedIdx / OpRatio;
23137     if (OpMask[OpIdx] < 0) {
23138       // The incoming lanes are zero or undef, it doesn't matter which ones we
23139       // are using.
23140       Mask.push_back(OpMask[OpIdx]);
23141       continue;
23142     }
23143
23144     // Ok, we have non-zero lanes, map them through.
23145     Mask.push_back(OpMask[OpIdx] * OpRatio +
23146                    RootMaskedIdx % OpRatio);
23147   }
23148
23149   // See if we can recurse into the operand to combine more things.
23150   switch (Op.getOpcode()) {
23151   case X86ISD::PSHUFB:
23152     HasPSHUFB = true;
23153   case X86ISD::PSHUFD:
23154   case X86ISD::PSHUFHW:
23155   case X86ISD::PSHUFLW:
23156     if (Op.getOperand(0).hasOneUse() &&
23157         combineX86ShufflesRecursively(Op.getOperand(0), Root, Mask, Depth + 1,
23158                                       HasPSHUFB, DAG, DCI, Subtarget))
23159       return true;
23160     break;
23161
23162   case X86ISD::UNPCKL:
23163   case X86ISD::UNPCKH:
23164     assert(Op.getOperand(0) == Op.getOperand(1) &&
23165            "We only combine unary shuffles!");
23166     // We can't check for single use, we have to check that this shuffle is the
23167     // only user.
23168     if (Op->isOnlyUserOf(Op.getOperand(0).getNode()) &&
23169         combineX86ShufflesRecursively(Op.getOperand(0), Root, Mask, Depth + 1,
23170                                       HasPSHUFB, DAG, DCI, Subtarget))
23171       return true;
23172     break;
23173   }
23174
23175   // Minor canonicalization of the accumulated shuffle mask to make it easier
23176   // to match below. All this does is detect masks with squential pairs of
23177   // elements, and shrink them to the half-width mask. It does this in a loop
23178   // so it will reduce the size of the mask to the minimal width mask which
23179   // performs an equivalent shuffle.
23180   SmallVector<int, 16> WidenedMask;
23181   while (Mask.size() > 1 && canWidenShuffleElements(Mask, WidenedMask)) {
23182     Mask = std::move(WidenedMask);
23183     WidenedMask.clear();
23184   }
23185
23186   return combineX86ShuffleChain(Op, Root, Mask, Depth, HasPSHUFB, DAG, DCI,
23187                                 Subtarget);
23188 }
23189
23190 /// \brief Get the PSHUF-style mask from PSHUF node.
23191 ///
23192 /// This is a very minor wrapper around getTargetShuffleMask to easy forming v4
23193 /// PSHUF-style masks that can be reused with such instructions.
23194 static SmallVector<int, 4> getPSHUFShuffleMask(SDValue N) {
23195   MVT VT = N.getSimpleValueType();
23196   SmallVector<int, 4> Mask;
23197   bool IsUnary;
23198   bool HaveMask = getTargetShuffleMask(N.getNode(), VT, Mask, IsUnary);
23199   (void)HaveMask;
23200   assert(HaveMask);
23201
23202   // If we have more than 128-bits, only the low 128-bits of shuffle mask
23203   // matter. Check that the upper masks are repeats and remove them.
23204   if (VT.getSizeInBits() > 128) {
23205     int LaneElts = 128 / VT.getScalarSizeInBits();
23206 #ifndef NDEBUG
23207     for (int i = 1, NumLanes = VT.getSizeInBits() / 128; i < NumLanes; ++i)
23208       for (int j = 0; j < LaneElts; ++j)
23209         assert(Mask[j] == Mask[i * LaneElts + j] - (LaneElts * i) &&
23210                "Mask doesn't repeat in high 128-bit lanes!");
23211 #endif
23212     Mask.resize(LaneElts);
23213   }
23214
23215   switch (N.getOpcode()) {
23216   case X86ISD::PSHUFD:
23217     return Mask;
23218   case X86ISD::PSHUFLW:
23219     Mask.resize(4);
23220     return Mask;
23221   case X86ISD::PSHUFHW:
23222     Mask.erase(Mask.begin(), Mask.begin() + 4);
23223     for (int &M : Mask)
23224       M -= 4;
23225     return Mask;
23226   default:
23227     llvm_unreachable("No valid shuffle instruction found!");
23228   }
23229 }
23230
23231 /// \brief Search for a combinable shuffle across a chain ending in pshufd.
23232 ///
23233 /// We walk up the chain and look for a combinable shuffle, skipping over
23234 /// shuffles that we could hoist this shuffle's transformation past without
23235 /// altering anything.
23236 static SDValue
23237 combineRedundantDWordShuffle(SDValue N, MutableArrayRef<int> Mask,
23238                              SelectionDAG &DAG,
23239                              TargetLowering::DAGCombinerInfo &DCI) {
23240   assert(N.getOpcode() == X86ISD::PSHUFD &&
23241          "Called with something other than an x86 128-bit half shuffle!");
23242   SDLoc DL(N);
23243
23244   // Walk up a single-use chain looking for a combinable shuffle. Keep a stack
23245   // of the shuffles in the chain so that we can form a fresh chain to replace
23246   // this one.
23247   SmallVector<SDValue, 8> Chain;
23248   SDValue V = N.getOperand(0);
23249   for (; V.hasOneUse(); V = V.getOperand(0)) {
23250     switch (V.getOpcode()) {
23251     default:
23252       return SDValue(); // Nothing combined!
23253
23254     case ISD::BITCAST:
23255       // Skip bitcasts as we always know the type for the target specific
23256       // instructions.
23257       continue;
23258
23259     case X86ISD::PSHUFD:
23260       // Found another dword shuffle.
23261       break;
23262
23263     case X86ISD::PSHUFLW:
23264       // Check that the low words (being shuffled) are the identity in the
23265       // dword shuffle, and the high words are self-contained.
23266       if (Mask[0] != 0 || Mask[1] != 1 ||
23267           !(Mask[2] >= 2 && Mask[2] < 4 && Mask[3] >= 2 && Mask[3] < 4))
23268         return SDValue();
23269
23270       Chain.push_back(V);
23271       continue;
23272
23273     case X86ISD::PSHUFHW:
23274       // Check that the high words (being shuffled) are the identity in the
23275       // dword shuffle, and the low words are self-contained.
23276       if (Mask[2] != 2 || Mask[3] != 3 ||
23277           !(Mask[0] >= 0 && Mask[0] < 2 && Mask[1] >= 0 && Mask[1] < 2))
23278         return SDValue();
23279
23280       Chain.push_back(V);
23281       continue;
23282
23283     case X86ISD::UNPCKL:
23284     case X86ISD::UNPCKH:
23285       // For either i8 -> i16 or i16 -> i32 unpacks, we can combine a dword
23286       // shuffle into a preceding word shuffle.
23287       if (V.getSimpleValueType().getVectorElementType() != MVT::i8 &&
23288           V.getSimpleValueType().getVectorElementType() != MVT::i16)
23289         return SDValue();
23290
23291       // Search for a half-shuffle which we can combine with.
23292       unsigned CombineOp =
23293           V.getOpcode() == X86ISD::UNPCKL ? X86ISD::PSHUFLW : X86ISD::PSHUFHW;
23294       if (V.getOperand(0) != V.getOperand(1) ||
23295           !V->isOnlyUserOf(V.getOperand(0).getNode()))
23296         return SDValue();
23297       Chain.push_back(V);
23298       V = V.getOperand(0);
23299       do {
23300         switch (V.getOpcode()) {
23301         default:
23302           return SDValue(); // Nothing to combine.
23303
23304         case X86ISD::PSHUFLW:
23305         case X86ISD::PSHUFHW:
23306           if (V.getOpcode() == CombineOp)
23307             break;
23308
23309           Chain.push_back(V);
23310
23311           // Fallthrough!
23312         case ISD::BITCAST:
23313           V = V.getOperand(0);
23314           continue;
23315         }
23316         break;
23317       } while (V.hasOneUse());
23318       break;
23319     }
23320     // Break out of the loop if we break out of the switch.
23321     break;
23322   }
23323
23324   if (!V.hasOneUse())
23325     // We fell out of the loop without finding a viable combining instruction.
23326     return SDValue();
23327
23328   // Merge this node's mask and our incoming mask.
23329   SmallVector<int, 4> VMask = getPSHUFShuffleMask(V);
23330   for (int &M : Mask)
23331     M = VMask[M];
23332   V = DAG.getNode(V.getOpcode(), DL, V.getValueType(), V.getOperand(0),
23333                   getV4X86ShuffleImm8ForMask(Mask, DL, DAG));
23334
23335   // Rebuild the chain around this new shuffle.
23336   while (!Chain.empty()) {
23337     SDValue W = Chain.pop_back_val();
23338
23339     if (V.getValueType() != W.getOperand(0).getValueType())
23340       V = DAG.getBitcast(W.getOperand(0).getValueType(), V);
23341
23342     switch (W.getOpcode()) {
23343     default:
23344       llvm_unreachable("Only PSHUF and UNPCK instructions get here!");
23345
23346     case X86ISD::UNPCKL:
23347     case X86ISD::UNPCKH:
23348       V = DAG.getNode(W.getOpcode(), DL, W.getValueType(), V, V);
23349       break;
23350
23351     case X86ISD::PSHUFD:
23352     case X86ISD::PSHUFLW:
23353     case X86ISD::PSHUFHW:
23354       V = DAG.getNode(W.getOpcode(), DL, W.getValueType(), V, W.getOperand(1));
23355       break;
23356     }
23357   }
23358   if (V.getValueType() != N.getValueType())
23359     V = DAG.getBitcast(N.getValueType(), V);
23360
23361   // Return the new chain to replace N.
23362   return V;
23363 }
23364
23365 /// \brief Search for a combinable shuffle across a chain ending in pshuflw or
23366 /// pshufhw.
23367 ///
23368 /// We walk up the chain, skipping shuffles of the other half and looking
23369 /// through shuffles which switch halves trying to find a shuffle of the same
23370 /// pair of dwords.
23371 static bool combineRedundantHalfShuffle(SDValue N, MutableArrayRef<int> Mask,
23372                                         SelectionDAG &DAG,
23373                                         TargetLowering::DAGCombinerInfo &DCI) {
23374   assert(
23375       (N.getOpcode() == X86ISD::PSHUFLW || N.getOpcode() == X86ISD::PSHUFHW) &&
23376       "Called with something other than an x86 128-bit half shuffle!");
23377   SDLoc DL(N);
23378   unsigned CombineOpcode = N.getOpcode();
23379
23380   // Walk up a single-use chain looking for a combinable shuffle.
23381   SDValue V = N.getOperand(0);
23382   for (; V.hasOneUse(); V = V.getOperand(0)) {
23383     switch (V.getOpcode()) {
23384     default:
23385       return false; // Nothing combined!
23386
23387     case ISD::BITCAST:
23388       // Skip bitcasts as we always know the type for the target specific
23389       // instructions.
23390       continue;
23391
23392     case X86ISD::PSHUFLW:
23393     case X86ISD::PSHUFHW:
23394       if (V.getOpcode() == CombineOpcode)
23395         break;
23396
23397       // Other-half shuffles are no-ops.
23398       continue;
23399     }
23400     // Break out of the loop if we break out of the switch.
23401     break;
23402   }
23403
23404   if (!V.hasOneUse())
23405     // We fell out of the loop without finding a viable combining instruction.
23406     return false;
23407
23408   // Combine away the bottom node as its shuffle will be accumulated into
23409   // a preceding shuffle.
23410   DCI.CombineTo(N.getNode(), N.getOperand(0), /*AddTo*/ true);
23411
23412   // Record the old value.
23413   SDValue Old = V;
23414
23415   // Merge this node's mask and our incoming mask (adjusted to account for all
23416   // the pshufd instructions encountered).
23417   SmallVector<int, 4> VMask = getPSHUFShuffleMask(V);
23418   for (int &M : Mask)
23419     M = VMask[M];
23420   V = DAG.getNode(V.getOpcode(), DL, MVT::v8i16, V.getOperand(0),
23421                   getV4X86ShuffleImm8ForMask(Mask, DL, DAG));
23422
23423   // Check that the shuffles didn't cancel each other out. If not, we need to
23424   // combine to the new one.
23425   if (Old != V)
23426     // Replace the combinable shuffle with the combined one, updating all users
23427     // so that we re-evaluate the chain here.
23428     DCI.CombineTo(Old.getNode(), V, /*AddTo*/ true);
23429
23430   return true;
23431 }
23432
23433 /// \brief Try to combine x86 target specific shuffles.
23434 static SDValue PerformTargetShuffleCombine(SDValue N, SelectionDAG &DAG,
23435                                            TargetLowering::DAGCombinerInfo &DCI,
23436                                            const X86Subtarget *Subtarget) {
23437   SDLoc DL(N);
23438   MVT VT = N.getSimpleValueType();
23439   SmallVector<int, 4> Mask;
23440
23441   switch (N.getOpcode()) {
23442   case X86ISD::PSHUFD:
23443   case X86ISD::PSHUFLW:
23444   case X86ISD::PSHUFHW:
23445     Mask = getPSHUFShuffleMask(N);
23446     assert(Mask.size() == 4);
23447     break;
23448   case X86ISD::UNPCKL: {
23449     // Combine X86ISD::UNPCKL and ISD::VECTOR_SHUFFLE into X86ISD::UNPCKH, in
23450     // which X86ISD::UNPCKL has a ISD::UNDEF operand, and ISD::VECTOR_SHUFFLE
23451     // moves upper half elements into the lower half part. For example:
23452     //
23453     // t2: v16i8 = vector_shuffle<8,9,10,11,12,13,14,15,u,u,u,u,u,u,u,u> t1,
23454     //     undef:v16i8
23455     // t3: v16i8 = X86ISD::UNPCKL undef:v16i8, t2
23456     //
23457     // will be combined to:
23458     //
23459     // t3: v16i8 = X86ISD::UNPCKH undef:v16i8, t1
23460
23461     // This is only for 128-bit vectors. From SSE4.1 onward this combine may not
23462     // happen due to advanced instructions.
23463     if (!VT.is128BitVector())
23464       return SDValue();
23465
23466     auto Op0 = N.getOperand(0);
23467     auto Op1 = N.getOperand(1);
23468     if (Op0.getOpcode() == ISD::UNDEF &&
23469         Op1.getNode()->getOpcode() == ISD::VECTOR_SHUFFLE) {
23470       ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(Op1.getNode())->getMask();
23471
23472       unsigned NumElts = VT.getVectorNumElements();
23473       SmallVector<int, 8> ExpectedMask(NumElts, -1);
23474       std::iota(ExpectedMask.begin(), ExpectedMask.begin() + NumElts / 2,
23475                 NumElts / 2);
23476
23477       auto ShufOp = Op1.getOperand(0);
23478       if (isShuffleEquivalent(Op1, ShufOp, Mask, ExpectedMask))
23479         return DAG.getNode(X86ISD::UNPCKH, DL, VT, N.getOperand(0), ShufOp);
23480     }
23481     return SDValue();
23482   }
23483   default:
23484     return SDValue();
23485   }
23486
23487   // Nuke no-op shuffles that show up after combining.
23488   if (isNoopShuffleMask(Mask))
23489     return DCI.CombineTo(N.getNode(), N.getOperand(0), /*AddTo*/ true);
23490
23491   // Look for simplifications involving one or two shuffle instructions.
23492   SDValue V = N.getOperand(0);
23493   switch (N.getOpcode()) {
23494   default:
23495     break;
23496   case X86ISD::PSHUFLW:
23497   case X86ISD::PSHUFHW:
23498     assert(VT.getVectorElementType() == MVT::i16 && "Bad word shuffle type!");
23499
23500     if (combineRedundantHalfShuffle(N, Mask, DAG, DCI))
23501       return SDValue(); // We combined away this shuffle, so we're done.
23502
23503     // See if this reduces to a PSHUFD which is no more expensive and can
23504     // combine with more operations. Note that it has to at least flip the
23505     // dwords as otherwise it would have been removed as a no-op.
23506     if (makeArrayRef(Mask).equals({2, 3, 0, 1})) {
23507       int DMask[] = {0, 1, 2, 3};
23508       int DOffset = N.getOpcode() == X86ISD::PSHUFLW ? 0 : 2;
23509       DMask[DOffset + 0] = DOffset + 1;
23510       DMask[DOffset + 1] = DOffset + 0;
23511       MVT DVT = MVT::getVectorVT(MVT::i32, VT.getVectorNumElements() / 2);
23512       V = DAG.getBitcast(DVT, V);
23513       DCI.AddToWorklist(V.getNode());
23514       V = DAG.getNode(X86ISD::PSHUFD, DL, DVT, V,
23515                       getV4X86ShuffleImm8ForMask(DMask, DL, DAG));
23516       DCI.AddToWorklist(V.getNode());
23517       return DAG.getBitcast(VT, V);
23518     }
23519
23520     // Look for shuffle patterns which can be implemented as a single unpack.
23521     // FIXME: This doesn't handle the location of the PSHUFD generically, and
23522     // only works when we have a PSHUFD followed by two half-shuffles.
23523     if (Mask[0] == Mask[1] && Mask[2] == Mask[3] &&
23524         (V.getOpcode() == X86ISD::PSHUFLW ||
23525          V.getOpcode() == X86ISD::PSHUFHW) &&
23526         V.getOpcode() != N.getOpcode() &&
23527         V.hasOneUse()) {
23528       SDValue D = V.getOperand(0);
23529       while (D.getOpcode() == ISD::BITCAST && D.hasOneUse())
23530         D = D.getOperand(0);
23531       if (D.getOpcode() == X86ISD::PSHUFD && D.hasOneUse()) {
23532         SmallVector<int, 4> VMask = getPSHUFShuffleMask(V);
23533         SmallVector<int, 4> DMask = getPSHUFShuffleMask(D);
23534         int NOffset = N.getOpcode() == X86ISD::PSHUFLW ? 0 : 4;
23535         int VOffset = V.getOpcode() == X86ISD::PSHUFLW ? 0 : 4;
23536         int WordMask[8];
23537         for (int i = 0; i < 4; ++i) {
23538           WordMask[i + NOffset] = Mask[i] + NOffset;
23539           WordMask[i + VOffset] = VMask[i] + VOffset;
23540         }
23541         // Map the word mask through the DWord mask.
23542         int MappedMask[8];
23543         for (int i = 0; i < 8; ++i)
23544           MappedMask[i] = 2 * DMask[WordMask[i] / 2] + WordMask[i] % 2;
23545         if (makeArrayRef(MappedMask).equals({0, 0, 1, 1, 2, 2, 3, 3}) ||
23546             makeArrayRef(MappedMask).equals({4, 4, 5, 5, 6, 6, 7, 7})) {
23547           // We can replace all three shuffles with an unpack.
23548           V = DAG.getBitcast(VT, D.getOperand(0));
23549           DCI.AddToWorklist(V.getNode());
23550           return DAG.getNode(MappedMask[0] == 0 ? X86ISD::UNPCKL
23551                                                 : X86ISD::UNPCKH,
23552                              DL, VT, V, V);
23553         }
23554       }
23555     }
23556
23557     break;
23558
23559   case X86ISD::PSHUFD:
23560     if (SDValue NewN = combineRedundantDWordShuffle(N, Mask, DAG, DCI))
23561       return NewN;
23562
23563     break;
23564   }
23565
23566   return SDValue();
23567 }
23568
23569 /// \brief Try to combine a shuffle into a target-specific add-sub node.
23570 ///
23571 /// We combine this directly on the abstract vector shuffle nodes so it is
23572 /// easier to generically match. We also insert dummy vector shuffle nodes for
23573 /// the operands which explicitly discard the lanes which are unused by this
23574 /// operation to try to flow through the rest of the combiner the fact that
23575 /// they're unused.
23576 static SDValue combineShuffleToAddSub(SDNode *N, SelectionDAG &DAG) {
23577   SDLoc DL(N);
23578   EVT VT = N->getValueType(0);
23579
23580   // We only handle target-independent shuffles.
23581   // FIXME: It would be easy and harmless to use the target shuffle mask
23582   // extraction tool to support more.
23583   if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
23584     return SDValue();
23585
23586   auto *SVN = cast<ShuffleVectorSDNode>(N);
23587   SmallVector<int, 8> Mask;
23588   for (int M : SVN->getMask())
23589     Mask.push_back(M);
23590
23591   SDValue V1 = N->getOperand(0);
23592   SDValue V2 = N->getOperand(1);
23593
23594   // We require the first shuffle operand to be the FSUB node, and the second to
23595   // be the FADD node.
23596   if (V1.getOpcode() == ISD::FADD && V2.getOpcode() == ISD::FSUB) {
23597     ShuffleVectorSDNode::commuteMask(Mask);
23598     std::swap(V1, V2);
23599   } else if (V1.getOpcode() != ISD::FSUB || V2.getOpcode() != ISD::FADD)
23600     return SDValue();
23601
23602   // If there are other uses of these operations we can't fold them.
23603   if (!V1->hasOneUse() || !V2->hasOneUse())
23604     return SDValue();
23605
23606   // Ensure that both operations have the same operands. Note that we can
23607   // commute the FADD operands.
23608   SDValue LHS = V1->getOperand(0), RHS = V1->getOperand(1);
23609   if ((V2->getOperand(0) != LHS || V2->getOperand(1) != RHS) &&
23610       (V2->getOperand(0) != RHS || V2->getOperand(1) != LHS))
23611     return SDValue();
23612
23613   // We're looking for blends between FADD and FSUB nodes. We insist on these
23614   // nodes being lined up in a specific expected pattern.
23615   if (!(isShuffleEquivalent(V1, V2, Mask, {0, 3}) ||
23616         isShuffleEquivalent(V1, V2, Mask, {0, 5, 2, 7}) ||
23617         isShuffleEquivalent(V1, V2, Mask, {0, 9, 2, 11, 4, 13, 6, 15})))
23618     return SDValue();
23619
23620   // Only specific types are legal at this point, assert so we notice if and
23621   // when these change.
23622   assert((VT == MVT::v4f32 || VT == MVT::v2f64 || VT == MVT::v8f32 ||
23623           VT == MVT::v4f64) &&
23624          "Unknown vector type encountered!");
23625
23626   return DAG.getNode(X86ISD::ADDSUB, DL, VT, LHS, RHS);
23627 }
23628
23629 /// PerformShuffleCombine - Performs several different shuffle combines.
23630 static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
23631                                      TargetLowering::DAGCombinerInfo &DCI,
23632                                      const X86Subtarget *Subtarget) {
23633   SDLoc dl(N);
23634   SDValue N0 = N->getOperand(0);
23635   SDValue N1 = N->getOperand(1);
23636   EVT VT = N->getValueType(0);
23637
23638   // Don't create instructions with illegal types after legalize types has run.
23639   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
23640   if (!DCI.isBeforeLegalize() && !TLI.isTypeLegal(VT.getVectorElementType()))
23641     return SDValue();
23642
23643   // If we have legalized the vector types, look for blends of FADD and FSUB
23644   // nodes that we can fuse into an ADDSUB node.
23645   if (TLI.isTypeLegal(VT) && Subtarget->hasSSE3())
23646     if (SDValue AddSub = combineShuffleToAddSub(N, DAG))
23647       return AddSub;
23648
23649   // Combine 256-bit vector shuffles. This is only profitable when in AVX mode
23650   if (TLI.isTypeLegal(VT) && Subtarget->hasFp256() && VT.is256BitVector() &&
23651       N->getOpcode() == ISD::VECTOR_SHUFFLE)
23652     return PerformShuffleCombine256(N, DAG, DCI, Subtarget);
23653
23654   // During Type Legalization, when promoting illegal vector types,
23655   // the backend might introduce new shuffle dag nodes and bitcasts.
23656   //
23657   // This code performs the following transformation:
23658   // fold: (shuffle (bitcast (BINOP A, B)), Undef, <Mask>) ->
23659   //       (shuffle (BINOP (bitcast A), (bitcast B)), Undef, <Mask>)
23660   //
23661   // We do this only if both the bitcast and the BINOP dag nodes have
23662   // one use. Also, perform this transformation only if the new binary
23663   // operation is legal. This is to avoid introducing dag nodes that
23664   // potentially need to be further expanded (or custom lowered) into a
23665   // less optimal sequence of dag nodes.
23666   if (!DCI.isBeforeLegalize() && DCI.isBeforeLegalizeOps() &&
23667       N1.getOpcode() == ISD::UNDEF && N0.hasOneUse() &&
23668       N0.getOpcode() == ISD::BITCAST) {
23669     SDValue BC0 = N0.getOperand(0);
23670     EVT SVT = BC0.getValueType();
23671     unsigned Opcode = BC0.getOpcode();
23672     unsigned NumElts = VT.getVectorNumElements();
23673
23674     if (BC0.hasOneUse() && SVT.isVector() &&
23675         SVT.getVectorNumElements() * 2 == NumElts &&
23676         TLI.isOperationLegal(Opcode, VT)) {
23677       bool CanFold = false;
23678       switch (Opcode) {
23679       default : break;
23680       case ISD::ADD :
23681       case ISD::FADD :
23682       case ISD::SUB :
23683       case ISD::FSUB :
23684       case ISD::MUL :
23685       case ISD::FMUL :
23686         CanFold = true;
23687       }
23688
23689       unsigned SVTNumElts = SVT.getVectorNumElements();
23690       ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
23691       for (unsigned i = 0, e = SVTNumElts; i != e && CanFold; ++i)
23692         CanFold = SVOp->getMaskElt(i) == (int)(i * 2);
23693       for (unsigned i = SVTNumElts, e = NumElts; i != e && CanFold; ++i)
23694         CanFold = SVOp->getMaskElt(i) < 0;
23695
23696       if (CanFold) {
23697         SDValue BC00 = DAG.getBitcast(VT, BC0.getOperand(0));
23698         SDValue BC01 = DAG.getBitcast(VT, BC0.getOperand(1));
23699         SDValue NewBinOp = DAG.getNode(BC0.getOpcode(), dl, VT, BC00, BC01);
23700         return DAG.getVectorShuffle(VT, dl, NewBinOp, N1, &SVOp->getMask()[0]);
23701       }
23702     }
23703   }
23704
23705   // Combine a vector_shuffle that is equal to build_vector load1, load2, load3,
23706   // load4, <0, 1, 2, 3> into a 128-bit load if the load addresses are
23707   // consecutive, non-overlapping, and in the right order.
23708   SmallVector<SDValue, 16> Elts;
23709   for (unsigned i = 0, e = VT.getVectorNumElements(); i != e; ++i)
23710     Elts.push_back(getShuffleScalarElt(N, i, DAG, 0));
23711
23712   if (SDValue LD = EltsFromConsecutiveLoads(VT, Elts, dl, DAG, true))
23713     return LD;
23714
23715   if (isTargetShuffle(N->getOpcode())) {
23716     SDValue Shuffle =
23717         PerformTargetShuffleCombine(SDValue(N, 0), DAG, DCI, Subtarget);
23718     if (Shuffle.getNode())
23719       return Shuffle;
23720
23721     // Try recursively combining arbitrary sequences of x86 shuffle
23722     // instructions into higher-order shuffles. We do this after combining
23723     // specific PSHUF instruction sequences into their minimal form so that we
23724     // can evaluate how many specialized shuffle instructions are involved in
23725     // a particular chain.
23726     SmallVector<int, 1> NonceMask; // Just a placeholder.
23727     NonceMask.push_back(0);
23728     if (combineX86ShufflesRecursively(SDValue(N, 0), SDValue(N, 0), NonceMask,
23729                                       /*Depth*/ 1, /*HasPSHUFB*/ false, DAG,
23730                                       DCI, Subtarget))
23731       return SDValue(); // This routine will use CombineTo to replace N.
23732   }
23733
23734   return SDValue();
23735 }
23736
23737 /// XFormVExtractWithShuffleIntoLoad - Check if a vector extract from a target
23738 /// specific shuffle of a load can be folded into a single element load.
23739 /// Similar handling for VECTOR_SHUFFLE is performed by DAGCombiner, but
23740 /// shuffles have been custom lowered so we need to handle those here.
23741 static SDValue XFormVExtractWithShuffleIntoLoad(SDNode *N, SelectionDAG &DAG,
23742                                          TargetLowering::DAGCombinerInfo &DCI) {
23743   if (DCI.isBeforeLegalizeOps())
23744     return SDValue();
23745
23746   SDValue InVec = N->getOperand(0);
23747   SDValue EltNo = N->getOperand(1);
23748
23749   if (!isa<ConstantSDNode>(EltNo))
23750     return SDValue();
23751
23752   EVT OriginalVT = InVec.getValueType();
23753
23754   if (InVec.getOpcode() == ISD::BITCAST) {
23755     // Don't duplicate a load with other uses.
23756     if (!InVec.hasOneUse())
23757       return SDValue();
23758     EVT BCVT = InVec.getOperand(0).getValueType();
23759     if (!BCVT.isVector() ||
23760         BCVT.getVectorNumElements() != OriginalVT.getVectorNumElements())
23761       return SDValue();
23762     InVec = InVec.getOperand(0);
23763   }
23764
23765   EVT CurrentVT = InVec.getValueType();
23766
23767   if (!isTargetShuffle(InVec.getOpcode()))
23768     return SDValue();
23769
23770   // Don't duplicate a load with other uses.
23771   if (!InVec.hasOneUse())
23772     return SDValue();
23773
23774   SmallVector<int, 16> ShuffleMask;
23775   bool UnaryShuffle;
23776   if (!getTargetShuffleMask(InVec.getNode(), CurrentVT.getSimpleVT(),
23777                             ShuffleMask, UnaryShuffle))
23778     return SDValue();
23779
23780   // Select the input vector, guarding against out of range extract vector.
23781   unsigned NumElems = CurrentVT.getVectorNumElements();
23782   int Elt = cast<ConstantSDNode>(EltNo)->getZExtValue();
23783   int Idx = (Elt > (int)NumElems) ? -1 : ShuffleMask[Elt];
23784   SDValue LdNode = (Idx < (int)NumElems) ? InVec.getOperand(0)
23785                                          : InVec.getOperand(1);
23786
23787   // If inputs to shuffle are the same for both ops, then allow 2 uses
23788   unsigned AllowedUses = InVec.getNumOperands() > 1 &&
23789                          InVec.getOperand(0) == InVec.getOperand(1) ? 2 : 1;
23790
23791   if (LdNode.getOpcode() == ISD::BITCAST) {
23792     // Don't duplicate a load with other uses.
23793     if (!LdNode.getNode()->hasNUsesOfValue(AllowedUses, 0))
23794       return SDValue();
23795
23796     AllowedUses = 1; // only allow 1 load use if we have a bitcast
23797     LdNode = LdNode.getOperand(0);
23798   }
23799
23800   if (!ISD::isNormalLoad(LdNode.getNode()))
23801     return SDValue();
23802
23803   LoadSDNode *LN0 = cast<LoadSDNode>(LdNode);
23804
23805   if (!LN0 ||!LN0->hasNUsesOfValue(AllowedUses, 0) || LN0->isVolatile())
23806     return SDValue();
23807
23808   EVT EltVT = N->getValueType(0);
23809   // If there's a bitcast before the shuffle, check if the load type and
23810   // alignment is valid.
23811   unsigned Align = LN0->getAlignment();
23812   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
23813   unsigned NewAlign = DAG.getDataLayout().getABITypeAlignment(
23814       EltVT.getTypeForEVT(*DAG.getContext()));
23815
23816   if (NewAlign > Align || !TLI.isOperationLegalOrCustom(ISD::LOAD, EltVT))
23817     return SDValue();
23818
23819   // All checks match so transform back to vector_shuffle so that DAG combiner
23820   // can finish the job
23821   SDLoc dl(N);
23822
23823   // Create shuffle node taking into account the case that its a unary shuffle
23824   SDValue Shuffle = (UnaryShuffle) ? DAG.getUNDEF(CurrentVT)
23825                                    : InVec.getOperand(1);
23826   Shuffle = DAG.getVectorShuffle(CurrentVT, dl,
23827                                  InVec.getOperand(0), Shuffle,
23828                                  &ShuffleMask[0]);
23829   Shuffle = DAG.getBitcast(OriginalVT, Shuffle);
23830   return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, N->getValueType(0), Shuffle,
23831                      EltNo);
23832 }
23833
23834 static SDValue PerformBITCASTCombine(SDNode *N, SelectionDAG &DAG,
23835                                      const X86Subtarget *Subtarget) {
23836   SDValue N0 = N->getOperand(0);
23837   EVT VT = N->getValueType(0);
23838
23839   // Detect bitcasts between i32 to x86mmx low word. Since MMX types are
23840   // special and don't usually play with other vector types, it's better to
23841   // handle them early to be sure we emit efficient code by avoiding
23842   // store-load conversions.
23843   if (VT == MVT::x86mmx && N0.getOpcode() == ISD::BUILD_VECTOR &&
23844       N0.getValueType() == MVT::v2i32 &&
23845       isNullConstant(N0.getOperand(1))) {
23846     SDValue N00 = N0->getOperand(0);
23847     if (N00.getValueType() == MVT::i32)
23848       return DAG.getNode(X86ISD::MMX_MOVW2D, SDLoc(N00), VT, N00);
23849   }
23850
23851   // Convert a bitcasted integer logic operation that has one bitcasted
23852   // floating-point operand and one constant operand into a floating-point
23853   // logic operation. This may create a load of the constant, but that is
23854   // cheaper than materializing the constant in an integer register and
23855   // transferring it to an SSE register or transferring the SSE operand to
23856   // integer register and back.
23857   unsigned FPOpcode;
23858   switch (N0.getOpcode()) {
23859     case ISD::AND: FPOpcode = X86ISD::FAND; break;
23860     case ISD::OR:  FPOpcode = X86ISD::FOR;  break;
23861     case ISD::XOR: FPOpcode = X86ISD::FXOR; break;
23862     default: return SDValue();
23863   }
23864   if (((Subtarget->hasSSE1() && VT == MVT::f32) ||
23865        (Subtarget->hasSSE2() && VT == MVT::f64)) &&
23866       isa<ConstantSDNode>(N0.getOperand(1)) &&
23867       N0.getOperand(0).getOpcode() == ISD::BITCAST &&
23868       N0.getOperand(0).getOperand(0).getValueType() == VT) {
23869     SDValue N000 = N0.getOperand(0).getOperand(0);
23870     SDValue FPConst = DAG.getBitcast(VT, N0.getOperand(1));
23871     return DAG.getNode(FPOpcode, SDLoc(N0), VT, N000, FPConst);
23872   }
23873
23874   return SDValue();
23875 }
23876
23877 /// PerformEXTRACT_VECTOR_ELTCombine - Detect vector gather/scatter index
23878 /// generation and convert it from being a bunch of shuffles and extracts
23879 /// into a somewhat faster sequence. For i686, the best sequence is apparently
23880 /// storing the value and loading scalars back, while for x64 we should
23881 /// use 64-bit extracts and shifts.
23882 static SDValue PerformEXTRACT_VECTOR_ELTCombine(SDNode *N, SelectionDAG &DAG,
23883                                          TargetLowering::DAGCombinerInfo &DCI) {
23884   if (SDValue NewOp = XFormVExtractWithShuffleIntoLoad(N, DAG, DCI))
23885     return NewOp;
23886
23887   SDValue InputVector = N->getOperand(0);
23888   SDLoc dl(InputVector);
23889   // Detect mmx to i32 conversion through a v2i32 elt extract.
23890   if (InputVector.getOpcode() == ISD::BITCAST && InputVector.hasOneUse() &&
23891       N->getValueType(0) == MVT::i32 &&
23892       InputVector.getValueType() == MVT::v2i32) {
23893
23894     // The bitcast source is a direct mmx result.
23895     SDValue MMXSrc = InputVector.getNode()->getOperand(0);
23896     if (MMXSrc.getValueType() == MVT::x86mmx)
23897       return DAG.getNode(X86ISD::MMX_MOVD2W, SDLoc(InputVector),
23898                          N->getValueType(0),
23899                          InputVector.getNode()->getOperand(0));
23900
23901     // The mmx is indirect: (i64 extract_elt (v1i64 bitcast (x86mmx ...))).
23902     if (MMXSrc.getOpcode() == ISD::EXTRACT_VECTOR_ELT && MMXSrc.hasOneUse() &&
23903         MMXSrc.getValueType() == MVT::i64) {
23904       SDValue MMXSrcOp = MMXSrc.getOperand(0);
23905       if (MMXSrcOp.hasOneUse() && MMXSrcOp.getOpcode() == ISD::BITCAST &&
23906           MMXSrcOp.getValueType() == MVT::v1i64 &&
23907           MMXSrcOp.getOperand(0).getValueType() == MVT::x86mmx)
23908         return DAG.getNode(X86ISD::MMX_MOVD2W, SDLoc(InputVector),
23909                            N->getValueType(0), MMXSrcOp.getOperand(0));
23910     }
23911   }
23912
23913   EVT VT = N->getValueType(0);
23914
23915   if (VT == MVT::i1 && isa<ConstantSDNode>(N->getOperand(1)) &&
23916       InputVector.getOpcode() == ISD::BITCAST &&
23917       isa<ConstantSDNode>(InputVector.getOperand(0))) {
23918     uint64_t ExtractedElt =
23919         cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
23920     uint64_t InputValue =
23921         cast<ConstantSDNode>(InputVector.getOperand(0))->getZExtValue();
23922     uint64_t Res = (InputValue >> ExtractedElt) & 1;
23923     return DAG.getConstant(Res, dl, MVT::i1);
23924   }
23925   // Only operate on vectors of 4 elements, where the alternative shuffling
23926   // gets to be more expensive.
23927   if (InputVector.getValueType() != MVT::v4i32)
23928     return SDValue();
23929
23930   // Check whether every use of InputVector is an EXTRACT_VECTOR_ELT with a
23931   // single use which is a sign-extend or zero-extend, and all elements are
23932   // used.
23933   SmallVector<SDNode *, 4> Uses;
23934   unsigned ExtractedElements = 0;
23935   for (SDNode::use_iterator UI = InputVector.getNode()->use_begin(),
23936        UE = InputVector.getNode()->use_end(); UI != UE; ++UI) {
23937     if (UI.getUse().getResNo() != InputVector.getResNo())
23938       return SDValue();
23939
23940     SDNode *Extract = *UI;
23941     if (Extract->getOpcode() != ISD::EXTRACT_VECTOR_ELT)
23942       return SDValue();
23943
23944     if (Extract->getValueType(0) != MVT::i32)
23945       return SDValue();
23946     if (!Extract->hasOneUse())
23947       return SDValue();
23948     if (Extract->use_begin()->getOpcode() != ISD::SIGN_EXTEND &&
23949         Extract->use_begin()->getOpcode() != ISD::ZERO_EXTEND)
23950       return SDValue();
23951     if (!isa<ConstantSDNode>(Extract->getOperand(1)))
23952       return SDValue();
23953
23954     // Record which element was extracted.
23955     ExtractedElements |=
23956       1 << cast<ConstantSDNode>(Extract->getOperand(1))->getZExtValue();
23957
23958     Uses.push_back(Extract);
23959   }
23960
23961   // If not all the elements were used, this may not be worthwhile.
23962   if (ExtractedElements != 15)
23963     return SDValue();
23964
23965   // Ok, we've now decided to do the transformation.
23966   // If 64-bit shifts are legal, use the extract-shift sequence,
23967   // otherwise bounce the vector off the cache.
23968   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
23969   SDValue Vals[4];
23970
23971   if (TLI.isOperationLegal(ISD::SRA, MVT::i64)) {
23972     SDValue Cst = DAG.getBitcast(MVT::v2i64, InputVector);
23973     auto &DL = DAG.getDataLayout();
23974     EVT VecIdxTy = DAG.getTargetLoweringInfo().getVectorIdxTy(DL);
23975     SDValue BottomHalf = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64, Cst,
23976       DAG.getConstant(0, dl, VecIdxTy));
23977     SDValue TopHalf = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64, Cst,
23978       DAG.getConstant(1, dl, VecIdxTy));
23979
23980     SDValue ShAmt = DAG.getConstant(
23981         32, dl, DAG.getTargetLoweringInfo().getShiftAmountTy(MVT::i64, DL));
23982     Vals[0] = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, BottomHalf);
23983     Vals[1] = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32,
23984       DAG.getNode(ISD::SRA, dl, MVT::i64, BottomHalf, ShAmt));
23985     Vals[2] = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, TopHalf);
23986     Vals[3] = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32,
23987       DAG.getNode(ISD::SRA, dl, MVT::i64, TopHalf, ShAmt));
23988   } else {
23989     // Store the value to a temporary stack slot.
23990     SDValue StackPtr = DAG.CreateStackTemporary(InputVector.getValueType());
23991     SDValue Ch = DAG.getStore(DAG.getEntryNode(), dl, InputVector, StackPtr,
23992       MachinePointerInfo(), false, false, 0);
23993
23994     EVT ElementType = InputVector.getValueType().getVectorElementType();
23995     unsigned EltSize = ElementType.getSizeInBits() / 8;
23996
23997     // Replace each use (extract) with a load of the appropriate element.
23998     for (unsigned i = 0; i < 4; ++i) {
23999       uint64_t Offset = EltSize * i;
24000       auto PtrVT = TLI.getPointerTy(DAG.getDataLayout());
24001       SDValue OffsetVal = DAG.getConstant(Offset, dl, PtrVT);
24002
24003       SDValue ScalarAddr =
24004           DAG.getNode(ISD::ADD, dl, PtrVT, StackPtr, OffsetVal);
24005
24006       // Load the scalar.
24007       Vals[i] = DAG.getLoad(ElementType, dl, Ch,
24008                             ScalarAddr, MachinePointerInfo(),
24009                             false, false, false, 0);
24010
24011     }
24012   }
24013
24014   // Replace the extracts
24015   for (SmallVectorImpl<SDNode *>::iterator UI = Uses.begin(),
24016     UE = Uses.end(); UI != UE; ++UI) {
24017     SDNode *Extract = *UI;
24018
24019     SDValue Idx = Extract->getOperand(1);
24020     uint64_t IdxVal = cast<ConstantSDNode>(Idx)->getZExtValue();
24021     DAG.ReplaceAllUsesOfValueWith(SDValue(Extract, 0), Vals[IdxVal]);
24022   }
24023
24024   // The replacement was made in place; don't return anything.
24025   return SDValue();
24026 }
24027
24028 static SDValue
24029 transformVSELECTtoBlendVECTOR_SHUFFLE(SDNode *N, SelectionDAG &DAG,
24030                                       const X86Subtarget *Subtarget) {
24031   SDLoc dl(N);
24032   SDValue Cond = N->getOperand(0);
24033   SDValue LHS = N->getOperand(1);
24034   SDValue RHS = N->getOperand(2);
24035
24036   if (Cond.getOpcode() == ISD::SIGN_EXTEND) {
24037     SDValue CondSrc = Cond->getOperand(0);
24038     if (CondSrc->getOpcode() == ISD::SIGN_EXTEND_INREG)
24039       Cond = CondSrc->getOperand(0);
24040   }
24041
24042   if (!ISD::isBuildVectorOfConstantSDNodes(Cond.getNode()))
24043     return SDValue();
24044
24045   // A vselect where all conditions and data are constants can be optimized into
24046   // a single vector load by SelectionDAGLegalize::ExpandBUILD_VECTOR().
24047   if (ISD::isBuildVectorOfConstantSDNodes(LHS.getNode()) &&
24048       ISD::isBuildVectorOfConstantSDNodes(RHS.getNode()))
24049     return SDValue();
24050
24051   unsigned MaskValue = 0;
24052   if (!BUILD_VECTORtoBlendMask(cast<BuildVectorSDNode>(Cond), MaskValue))
24053     return SDValue();
24054
24055   MVT VT = N->getSimpleValueType(0);
24056   unsigned NumElems = VT.getVectorNumElements();
24057   SmallVector<int, 8> ShuffleMask(NumElems, -1);
24058   for (unsigned i = 0; i < NumElems; ++i) {
24059     // Be sure we emit undef where we can.
24060     if (Cond.getOperand(i)->getOpcode() == ISD::UNDEF)
24061       ShuffleMask[i] = -1;
24062     else
24063       ShuffleMask[i] = i + NumElems * ((MaskValue >> i) & 1);
24064   }
24065
24066   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
24067   if (!TLI.isShuffleMaskLegal(ShuffleMask, VT))
24068     return SDValue();
24069   return DAG.getVectorShuffle(VT, dl, LHS, RHS, &ShuffleMask[0]);
24070 }
24071
24072 /// PerformSELECTCombine - Do target-specific dag combines on SELECT and VSELECT
24073 /// nodes.
24074 static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
24075                                     TargetLowering::DAGCombinerInfo &DCI,
24076                                     const X86Subtarget *Subtarget) {
24077   SDLoc DL(N);
24078   SDValue Cond = N->getOperand(0);
24079   // Get the LHS/RHS of the select.
24080   SDValue LHS = N->getOperand(1);
24081   SDValue RHS = N->getOperand(2);
24082   EVT VT = LHS.getValueType();
24083   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
24084
24085   // If we have SSE[12] support, try to form min/max nodes. SSE min/max
24086   // instructions match the semantics of the common C idiom x<y?x:y but not
24087   // x<=y?x:y, because of how they handle negative zero (which can be
24088   // ignored in unsafe-math mode).
24089   // We also try to create v2f32 min/max nodes, which we later widen to v4f32.
24090   if (Cond.getOpcode() == ISD::SETCC && VT.isFloatingPoint() &&
24091       VT != MVT::f80 && VT != MVT::f128 &&
24092       (TLI.isTypeLegal(VT) || VT == MVT::v2f32) &&
24093       (Subtarget->hasSSE2() ||
24094        (Subtarget->hasSSE1() && VT.getScalarType() == MVT::f32))) {
24095     ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
24096
24097     unsigned Opcode = 0;
24098     // Check for x CC y ? x : y.
24099     if (DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
24100         DAG.isEqualTo(RHS, Cond.getOperand(1))) {
24101       switch (CC) {
24102       default: break;
24103       case ISD::SETULT:
24104         // Converting this to a min would handle NaNs incorrectly, and swapping
24105         // the operands would cause it to handle comparisons between positive
24106         // and negative zero incorrectly.
24107         if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
24108           if (!DAG.getTarget().Options.UnsafeFPMath &&
24109               !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
24110             break;
24111           std::swap(LHS, RHS);
24112         }
24113         Opcode = X86ISD::FMIN;
24114         break;
24115       case ISD::SETOLE:
24116         // Converting this to a min would handle comparisons between positive
24117         // and negative zero incorrectly.
24118         if (!DAG.getTarget().Options.UnsafeFPMath &&
24119             !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
24120           break;
24121         Opcode = X86ISD::FMIN;
24122         break;
24123       case ISD::SETULE:
24124         // Converting this to a min would handle both negative zeros and NaNs
24125         // incorrectly, but we can swap the operands to fix both.
24126         std::swap(LHS, RHS);
24127       case ISD::SETOLT:
24128       case ISD::SETLT:
24129       case ISD::SETLE:
24130         Opcode = X86ISD::FMIN;
24131         break;
24132
24133       case ISD::SETOGE:
24134         // Converting this to a max would handle comparisons between positive
24135         // and negative zero incorrectly.
24136         if (!DAG.getTarget().Options.UnsafeFPMath &&
24137             !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS))
24138           break;
24139         Opcode = X86ISD::FMAX;
24140         break;
24141       case ISD::SETUGT:
24142         // Converting this to a max would handle NaNs incorrectly, and swapping
24143         // the operands would cause it to handle comparisons between positive
24144         // and negative zero incorrectly.
24145         if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)) {
24146           if (!DAG.getTarget().Options.UnsafeFPMath &&
24147               !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS)))
24148             break;
24149           std::swap(LHS, RHS);
24150         }
24151         Opcode = X86ISD::FMAX;
24152         break;
24153       case ISD::SETUGE:
24154         // Converting this to a max would handle both negative zeros and NaNs
24155         // incorrectly, but we can swap the operands to fix both.
24156         std::swap(LHS, RHS);
24157       case ISD::SETOGT:
24158       case ISD::SETGT:
24159       case ISD::SETGE:
24160         Opcode = X86ISD::FMAX;
24161         break;
24162       }
24163     // Check for x CC y ? y : x -- a min/max with reversed arms.
24164     } else if (DAG.isEqualTo(LHS, Cond.getOperand(1)) &&
24165                DAG.isEqualTo(RHS, Cond.getOperand(0))) {
24166       switch (CC) {
24167       default: break;
24168       case ISD::SETOGE:
24169         // Converting this to a min would handle comparisons between positive
24170         // and negative zero incorrectly, and swapping the operands would
24171         // cause it to handle NaNs incorrectly.
24172         if (!DAG.getTarget().Options.UnsafeFPMath &&
24173             !(DAG.isKnownNeverZero(LHS) || DAG.isKnownNeverZero(RHS))) {
24174           if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
24175             break;
24176           std::swap(LHS, RHS);
24177         }
24178         Opcode = X86ISD::FMIN;
24179         break;
24180       case ISD::SETUGT:
24181         // Converting this to a min would handle NaNs incorrectly.
24182         if (!DAG.getTarget().Options.UnsafeFPMath &&
24183             (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS)))
24184           break;
24185         Opcode = X86ISD::FMIN;
24186         break;
24187       case ISD::SETUGE:
24188         // Converting this to a min would handle both negative zeros and NaNs
24189         // incorrectly, but we can swap the operands to fix both.
24190         std::swap(LHS, RHS);
24191       case ISD::SETOGT:
24192       case ISD::SETGT:
24193       case ISD::SETGE:
24194         Opcode = X86ISD::FMIN;
24195         break;
24196
24197       case ISD::SETULT:
24198         // Converting this to a max would handle NaNs incorrectly.
24199         if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
24200           break;
24201         Opcode = X86ISD::FMAX;
24202         break;
24203       case ISD::SETOLE:
24204         // Converting this to a max would handle comparisons between positive
24205         // and negative zero incorrectly, and swapping the operands would
24206         // cause it to handle NaNs incorrectly.
24207         if (!DAG.getTarget().Options.UnsafeFPMath &&
24208             !DAG.isKnownNeverZero(LHS) && !DAG.isKnownNeverZero(RHS)) {
24209           if (!DAG.isKnownNeverNaN(LHS) || !DAG.isKnownNeverNaN(RHS))
24210             break;
24211           std::swap(LHS, RHS);
24212         }
24213         Opcode = X86ISD::FMAX;
24214         break;
24215       case ISD::SETULE:
24216         // Converting this to a max would handle both negative zeros and NaNs
24217         // incorrectly, but we can swap the operands to fix both.
24218         std::swap(LHS, RHS);
24219       case ISD::SETOLT:
24220       case ISD::SETLT:
24221       case ISD::SETLE:
24222         Opcode = X86ISD::FMAX;
24223         break;
24224       }
24225     }
24226
24227     if (Opcode)
24228       return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
24229   }
24230
24231   EVT CondVT = Cond.getValueType();
24232   if (Subtarget->hasAVX512() && VT.isVector() && CondVT.isVector() &&
24233       CondVT.getVectorElementType() == MVT::i1) {
24234     // v16i8 (select v16i1, v16i8, v16i8) does not have a proper
24235     // lowering on KNL. In this case we convert it to
24236     // v16i8 (select v16i8, v16i8, v16i8) and use AVX instruction.
24237     // The same situation for all 128 and 256-bit vectors of i8 and i16.
24238     // Since SKX these selects have a proper lowering.
24239     EVT OpVT = LHS.getValueType();
24240     if ((OpVT.is128BitVector() || OpVT.is256BitVector()) &&
24241         (OpVT.getVectorElementType() == MVT::i8 ||
24242          OpVT.getVectorElementType() == MVT::i16) &&
24243         !(Subtarget->hasBWI() && Subtarget->hasVLX())) {
24244       Cond = DAG.getNode(ISD::SIGN_EXTEND, DL, OpVT, Cond);
24245       DCI.AddToWorklist(Cond.getNode());
24246       return DAG.getNode(N->getOpcode(), DL, OpVT, Cond, LHS, RHS);
24247     }
24248   }
24249   // If this is a select between two integer constants, try to do some
24250   // optimizations.
24251   if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
24252     if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
24253       // Don't do this for crazy integer types.
24254       if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
24255         // If this is efficiently invertible, canonicalize the LHSC/RHSC values
24256         // so that TrueC (the true value) is larger than FalseC.
24257         bool NeedsCondInvert = false;
24258
24259         if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
24260             // Efficiently invertible.
24261             (Cond.getOpcode() == ISD::SETCC ||  // setcc -> invertible.
24262              (Cond.getOpcode() == ISD::XOR &&   // xor(X, C) -> invertible.
24263               isa<ConstantSDNode>(Cond.getOperand(1))))) {
24264           NeedsCondInvert = true;
24265           std::swap(TrueC, FalseC);
24266         }
24267
24268         // Optimize C ? 8 : 0 -> zext(C) << 3.  Likewise for any pow2/0.
24269         if (FalseC->getAPIntValue() == 0 &&
24270             TrueC->getAPIntValue().isPowerOf2()) {
24271           if (NeedsCondInvert) // Invert the condition if needed.
24272             Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
24273                                DAG.getConstant(1, DL, Cond.getValueType()));
24274
24275           // Zero extend the condition if needed.
24276           Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
24277
24278           unsigned ShAmt = TrueC->getAPIntValue().logBase2();
24279           return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
24280                              DAG.getConstant(ShAmt, DL, MVT::i8));
24281         }
24282
24283         // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
24284         if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
24285           if (NeedsCondInvert) // Invert the condition if needed.
24286             Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
24287                                DAG.getConstant(1, DL, Cond.getValueType()));
24288
24289           // Zero extend the condition if needed.
24290           Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
24291                              FalseC->getValueType(0), Cond);
24292           return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
24293                              SDValue(FalseC, 0));
24294         }
24295
24296         // Optimize cases that will turn into an LEA instruction.  This requires
24297         // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
24298         if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
24299           uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
24300           if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
24301
24302           bool isFastMultiplier = false;
24303           if (Diff < 10) {
24304             switch ((unsigned char)Diff) {
24305               default: break;
24306               case 1:  // result = add base, cond
24307               case 2:  // result = lea base(    , cond*2)
24308               case 3:  // result = lea base(cond, cond*2)
24309               case 4:  // result = lea base(    , cond*4)
24310               case 5:  // result = lea base(cond, cond*4)
24311               case 8:  // result = lea base(    , cond*8)
24312               case 9:  // result = lea base(cond, cond*8)
24313                 isFastMultiplier = true;
24314                 break;
24315             }
24316           }
24317
24318           if (isFastMultiplier) {
24319             APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
24320             if (NeedsCondInvert) // Invert the condition if needed.
24321               Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
24322                                  DAG.getConstant(1, DL, Cond.getValueType()));
24323
24324             // Zero extend the condition if needed.
24325             Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
24326                                Cond);
24327             // Scale the condition by the difference.
24328             if (Diff != 1)
24329               Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
24330                                  DAG.getConstant(Diff, DL,
24331                                                  Cond.getValueType()));
24332
24333             // Add the base if non-zero.
24334             if (FalseC->getAPIntValue() != 0)
24335               Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
24336                                  SDValue(FalseC, 0));
24337             return Cond;
24338           }
24339         }
24340       }
24341   }
24342
24343   // Canonicalize max and min:
24344   // (x > y) ? x : y -> (x >= y) ? x : y
24345   // (x < y) ? x : y -> (x <= y) ? x : y
24346   // This allows use of COND_S / COND_NS (see TranslateX86CC) which eliminates
24347   // the need for an extra compare
24348   // against zero. e.g.
24349   // (x - y) > 0 : (x - y) ? 0 -> (x - y) >= 0 : (x - y) ? 0
24350   // subl   %esi, %edi
24351   // testl  %edi, %edi
24352   // movl   $0, %eax
24353   // cmovgl %edi, %eax
24354   // =>
24355   // xorl   %eax, %eax
24356   // subl   %esi, $edi
24357   // cmovsl %eax, %edi
24358   if (N->getOpcode() == ISD::SELECT && Cond.getOpcode() == ISD::SETCC &&
24359       DAG.isEqualTo(LHS, Cond.getOperand(0)) &&
24360       DAG.isEqualTo(RHS, Cond.getOperand(1))) {
24361     ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
24362     switch (CC) {
24363     default: break;
24364     case ISD::SETLT:
24365     case ISD::SETGT: {
24366       ISD::CondCode NewCC = (CC == ISD::SETLT) ? ISD::SETLE : ISD::SETGE;
24367       Cond = DAG.getSetCC(SDLoc(Cond), Cond.getValueType(),
24368                           Cond.getOperand(0), Cond.getOperand(1), NewCC);
24369       return DAG.getNode(ISD::SELECT, DL, VT, Cond, LHS, RHS);
24370     }
24371     }
24372   }
24373
24374   // Early exit check
24375   if (!TLI.isTypeLegal(VT))
24376     return SDValue();
24377
24378   // Match VSELECTs into subs with unsigned saturation.
24379   if (N->getOpcode() == ISD::VSELECT && Cond.getOpcode() == ISD::SETCC &&
24380       // psubus is available in SSE2 and AVX2 for i8 and i16 vectors.
24381       ((Subtarget->hasSSE2() && (VT == MVT::v16i8 || VT == MVT::v8i16)) ||
24382        (Subtarget->hasAVX2() && (VT == MVT::v32i8 || VT == MVT::v16i16)))) {
24383     ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
24384
24385     // Check if one of the arms of the VSELECT is a zero vector. If it's on the
24386     // left side invert the predicate to simplify logic below.
24387     SDValue Other;
24388     if (ISD::isBuildVectorAllZeros(LHS.getNode())) {
24389       Other = RHS;
24390       CC = ISD::getSetCCInverse(CC, true);
24391     } else if (ISD::isBuildVectorAllZeros(RHS.getNode())) {
24392       Other = LHS;
24393     }
24394
24395     if (Other.getNode() && Other->getNumOperands() == 2 &&
24396         DAG.isEqualTo(Other->getOperand(0), Cond.getOperand(0))) {
24397       SDValue OpLHS = Other->getOperand(0), OpRHS = Other->getOperand(1);
24398       SDValue CondRHS = Cond->getOperand(1);
24399
24400       // Look for a general sub with unsigned saturation first.
24401       // x >= y ? x-y : 0 --> subus x, y
24402       // x >  y ? x-y : 0 --> subus x, y
24403       if ((CC == ISD::SETUGE || CC == ISD::SETUGT) &&
24404           Other->getOpcode() == ISD::SUB && DAG.isEqualTo(OpRHS, CondRHS))
24405         return DAG.getNode(X86ISD::SUBUS, DL, VT, OpLHS, OpRHS);
24406
24407       if (auto *OpRHSBV = dyn_cast<BuildVectorSDNode>(OpRHS))
24408         if (auto *OpRHSConst = OpRHSBV->getConstantSplatNode()) {
24409           if (auto *CondRHSBV = dyn_cast<BuildVectorSDNode>(CondRHS))
24410             if (auto *CondRHSConst = CondRHSBV->getConstantSplatNode())
24411               // If the RHS is a constant we have to reverse the const
24412               // canonicalization.
24413               // x > C-1 ? x+-C : 0 --> subus x, C
24414               if (CC == ISD::SETUGT && Other->getOpcode() == ISD::ADD &&
24415                   CondRHSConst->getAPIntValue() ==
24416                       (-OpRHSConst->getAPIntValue() - 1))
24417                 return DAG.getNode(
24418                     X86ISD::SUBUS, DL, VT, OpLHS,
24419                     DAG.getConstant(-OpRHSConst->getAPIntValue(), DL, VT));
24420
24421           // Another special case: If C was a sign bit, the sub has been
24422           // canonicalized into a xor.
24423           // FIXME: Would it be better to use computeKnownBits to determine
24424           //        whether it's safe to decanonicalize the xor?
24425           // x s< 0 ? x^C : 0 --> subus x, C
24426           if (CC == ISD::SETLT && Other->getOpcode() == ISD::XOR &&
24427               ISD::isBuildVectorAllZeros(CondRHS.getNode()) &&
24428               OpRHSConst->getAPIntValue().isSignBit())
24429             // Note that we have to rebuild the RHS constant here to ensure we
24430             // don't rely on particular values of undef lanes.
24431             return DAG.getNode(
24432                 X86ISD::SUBUS, DL, VT, OpLHS,
24433                 DAG.getConstant(OpRHSConst->getAPIntValue(), DL, VT));
24434         }
24435     }
24436   }
24437
24438   // Simplify vector selection if condition value type matches vselect
24439   // operand type
24440   if (N->getOpcode() == ISD::VSELECT && CondVT == VT) {
24441     assert(Cond.getValueType().isVector() &&
24442            "vector select expects a vector selector!");
24443
24444     bool TValIsAllOnes = ISD::isBuildVectorAllOnes(LHS.getNode());
24445     bool FValIsAllZeros = ISD::isBuildVectorAllZeros(RHS.getNode());
24446
24447     // Try invert the condition if true value is not all 1s and false value
24448     // is not all 0s.
24449     if (!TValIsAllOnes && !FValIsAllZeros &&
24450         // Check if the selector will be produced by CMPP*/PCMP*
24451         Cond.getOpcode() == ISD::SETCC &&
24452         // Check if SETCC has already been promoted
24453         TLI.getSetCCResultType(DAG.getDataLayout(), *DAG.getContext(), VT) ==
24454             CondVT) {
24455       bool TValIsAllZeros = ISD::isBuildVectorAllZeros(LHS.getNode());
24456       bool FValIsAllOnes = ISD::isBuildVectorAllOnes(RHS.getNode());
24457
24458       if (TValIsAllZeros || FValIsAllOnes) {
24459         SDValue CC = Cond.getOperand(2);
24460         ISD::CondCode NewCC =
24461           ISD::getSetCCInverse(cast<CondCodeSDNode>(CC)->get(),
24462                                Cond.getOperand(0).getValueType().isInteger());
24463         Cond = DAG.getSetCC(DL, CondVT, Cond.getOperand(0), Cond.getOperand(1), NewCC);
24464         std::swap(LHS, RHS);
24465         TValIsAllOnes = FValIsAllOnes;
24466         FValIsAllZeros = TValIsAllZeros;
24467       }
24468     }
24469
24470     if (TValIsAllOnes || FValIsAllZeros) {
24471       SDValue Ret;
24472
24473       if (TValIsAllOnes && FValIsAllZeros)
24474         Ret = Cond;
24475       else if (TValIsAllOnes)
24476         Ret =
24477             DAG.getNode(ISD::OR, DL, CondVT, Cond, DAG.getBitcast(CondVT, RHS));
24478       else if (FValIsAllZeros)
24479         Ret = DAG.getNode(ISD::AND, DL, CondVT, Cond,
24480                           DAG.getBitcast(CondVT, LHS));
24481
24482       return DAG.getBitcast(VT, Ret);
24483     }
24484   }
24485
24486   // We should generate an X86ISD::BLENDI from a vselect if its argument
24487   // is a sign_extend_inreg of an any_extend of a BUILD_VECTOR of
24488   // constants. This specific pattern gets generated when we split a
24489   // selector for a 512 bit vector in a machine without AVX512 (but with
24490   // 256-bit vectors), during legalization:
24491   //
24492   // (vselect (sign_extend (any_extend (BUILD_VECTOR)) i1) LHS RHS)
24493   //
24494   // Iff we find this pattern and the build_vectors are built from
24495   // constants, we translate the vselect into a shuffle_vector that we
24496   // know will be matched by LowerVECTOR_SHUFFLEtoBlend.
24497   if ((N->getOpcode() == ISD::VSELECT ||
24498        N->getOpcode() == X86ISD::SHRUNKBLEND) &&
24499       !DCI.isBeforeLegalize() && !VT.is512BitVector()) {
24500     SDValue Shuffle = transformVSELECTtoBlendVECTOR_SHUFFLE(N, DAG, Subtarget);
24501     if (Shuffle.getNode())
24502       return Shuffle;
24503   }
24504
24505   // If this is a *dynamic* select (non-constant condition) and we can match
24506   // this node with one of the variable blend instructions, restructure the
24507   // condition so that the blends can use the high bit of each element and use
24508   // SimplifyDemandedBits to simplify the condition operand.
24509   if (N->getOpcode() == ISD::VSELECT && DCI.isBeforeLegalizeOps() &&
24510       !DCI.isBeforeLegalize() &&
24511       !ISD::isBuildVectorOfConstantSDNodes(Cond.getNode())) {
24512     unsigned BitWidth = Cond.getValueType().getScalarSizeInBits();
24513
24514     // Don't optimize vector selects that map to mask-registers.
24515     if (BitWidth == 1)
24516       return SDValue();
24517
24518     // We can only handle the cases where VSELECT is directly legal on the
24519     // subtarget. We custom lower VSELECT nodes with constant conditions and
24520     // this makes it hard to see whether a dynamic VSELECT will correctly
24521     // lower, so we both check the operation's status and explicitly handle the
24522     // cases where a *dynamic* blend will fail even though a constant-condition
24523     // blend could be custom lowered.
24524     // FIXME: We should find a better way to handle this class of problems.
24525     // Potentially, we should combine constant-condition vselect nodes
24526     // pre-legalization into shuffles and not mark as many types as custom
24527     // lowered.
24528     if (!TLI.isOperationLegalOrCustom(ISD::VSELECT, VT))
24529       return SDValue();
24530     // FIXME: We don't support i16-element blends currently. We could and
24531     // should support them by making *all* the bits in the condition be set
24532     // rather than just the high bit and using an i8-element blend.
24533     if (VT.getVectorElementType() == MVT::i16)
24534       return SDValue();
24535     // Dynamic blending was only available from SSE4.1 onward.
24536     if (VT.is128BitVector() && !Subtarget->hasSSE41())
24537       return SDValue();
24538     // Byte blends are only available in AVX2
24539     if (VT == MVT::v32i8 && !Subtarget->hasAVX2())
24540       return SDValue();
24541
24542     assert(BitWidth >= 8 && BitWidth <= 64 && "Invalid mask size");
24543     APInt DemandedMask = APInt::getHighBitsSet(BitWidth, 1);
24544
24545     APInt KnownZero, KnownOne;
24546     TargetLowering::TargetLoweringOpt TLO(DAG, DCI.isBeforeLegalize(),
24547                                           DCI.isBeforeLegalizeOps());
24548     if (TLO.ShrinkDemandedConstant(Cond, DemandedMask) ||
24549         TLI.SimplifyDemandedBits(Cond, DemandedMask, KnownZero, KnownOne,
24550                                  TLO)) {
24551       // If we changed the computation somewhere in the DAG, this change
24552       // will affect all users of Cond.
24553       // Make sure it is fine and update all the nodes so that we do not
24554       // use the generic VSELECT anymore. Otherwise, we may perform
24555       // wrong optimizations as we messed up with the actual expectation
24556       // for the vector boolean values.
24557       if (Cond != TLO.Old) {
24558         // Check all uses of that condition operand to check whether it will be
24559         // consumed by non-BLEND instructions, which may depend on all bits are
24560         // set properly.
24561         for (SDNode::use_iterator I = Cond->use_begin(), E = Cond->use_end();
24562              I != E; ++I)
24563           if (I->getOpcode() != ISD::VSELECT)
24564             // TODO: Add other opcodes eventually lowered into BLEND.
24565             return SDValue();
24566
24567         // Update all the users of the condition, before committing the change,
24568         // so that the VSELECT optimizations that expect the correct vector
24569         // boolean value will not be triggered.
24570         for (SDNode::use_iterator I = Cond->use_begin(), E = Cond->use_end();
24571              I != E; ++I)
24572           DAG.ReplaceAllUsesOfValueWith(
24573               SDValue(*I, 0),
24574               DAG.getNode(X86ISD::SHRUNKBLEND, SDLoc(*I), I->getValueType(0),
24575                           Cond, I->getOperand(1), I->getOperand(2)));
24576         DCI.CommitTargetLoweringOpt(TLO);
24577         return SDValue();
24578       }
24579       // At this point, only Cond is changed. Change the condition
24580       // just for N to keep the opportunity to optimize all other
24581       // users their own way.
24582       DAG.ReplaceAllUsesOfValueWith(
24583           SDValue(N, 0),
24584           DAG.getNode(X86ISD::SHRUNKBLEND, SDLoc(N), N->getValueType(0),
24585                       TLO.New, N->getOperand(1), N->getOperand(2)));
24586       return SDValue();
24587     }
24588   }
24589
24590   return SDValue();
24591 }
24592
24593 // Check whether a boolean test is testing a boolean value generated by
24594 // X86ISD::SETCC. If so, return the operand of that SETCC and proper condition
24595 // code.
24596 //
24597 // Simplify the following patterns:
24598 // (Op (CMP (SETCC Cond EFLAGS) 1) EQ) or
24599 // (Op (CMP (SETCC Cond EFLAGS) 0) NEQ)
24600 // to (Op EFLAGS Cond)
24601 //
24602 // (Op (CMP (SETCC Cond EFLAGS) 0) EQ) or
24603 // (Op (CMP (SETCC Cond EFLAGS) 1) NEQ)
24604 // to (Op EFLAGS !Cond)
24605 //
24606 // where Op could be BRCOND or CMOV.
24607 //
24608 static SDValue checkBoolTestSetCCCombine(SDValue Cmp, X86::CondCode &CC) {
24609   // Quit if not CMP and SUB with its value result used.
24610   if (Cmp.getOpcode() != X86ISD::CMP &&
24611       (Cmp.getOpcode() != X86ISD::SUB || Cmp.getNode()->hasAnyUseOfValue(0)))
24612       return SDValue();
24613
24614   // Quit if not used as a boolean value.
24615   if (CC != X86::COND_E && CC != X86::COND_NE)
24616     return SDValue();
24617
24618   // Check CMP operands. One of them should be 0 or 1 and the other should be
24619   // an SetCC or extended from it.
24620   SDValue Op1 = Cmp.getOperand(0);
24621   SDValue Op2 = Cmp.getOperand(1);
24622
24623   SDValue SetCC;
24624   const ConstantSDNode* C = nullptr;
24625   bool needOppositeCond = (CC == X86::COND_E);
24626   bool checkAgainstTrue = false; // Is it a comparison against 1?
24627
24628   if ((C = dyn_cast<ConstantSDNode>(Op1)))
24629     SetCC = Op2;
24630   else if ((C = dyn_cast<ConstantSDNode>(Op2)))
24631     SetCC = Op1;
24632   else // Quit if all operands are not constants.
24633     return SDValue();
24634
24635   if (C->getZExtValue() == 1) {
24636     needOppositeCond = !needOppositeCond;
24637     checkAgainstTrue = true;
24638   } else if (C->getZExtValue() != 0)
24639     // Quit if the constant is neither 0 or 1.
24640     return SDValue();
24641
24642   bool truncatedToBoolWithAnd = false;
24643   // Skip (zext $x), (trunc $x), or (and $x, 1) node.
24644   while (SetCC.getOpcode() == ISD::ZERO_EXTEND ||
24645          SetCC.getOpcode() == ISD::TRUNCATE ||
24646          SetCC.getOpcode() == ISD::AND) {
24647     if (SetCC.getOpcode() == ISD::AND) {
24648       int OpIdx = -1;
24649       if (isOneConstant(SetCC.getOperand(0)))
24650         OpIdx = 1;
24651       if (isOneConstant(SetCC.getOperand(1)))
24652         OpIdx = 0;
24653       if (OpIdx == -1)
24654         break;
24655       SetCC = SetCC.getOperand(OpIdx);
24656       truncatedToBoolWithAnd = true;
24657     } else
24658       SetCC = SetCC.getOperand(0);
24659   }
24660
24661   switch (SetCC.getOpcode()) {
24662   case X86ISD::SETCC_CARRY:
24663     // Since SETCC_CARRY gives output based on R = CF ? ~0 : 0, it's unsafe to
24664     // simplify it if the result of SETCC_CARRY is not canonicalized to 0 or 1,
24665     // i.e. it's a comparison against true but the result of SETCC_CARRY is not
24666     // truncated to i1 using 'and'.
24667     if (checkAgainstTrue && !truncatedToBoolWithAnd)
24668       break;
24669     assert(X86::CondCode(SetCC.getConstantOperandVal(0)) == X86::COND_B &&
24670            "Invalid use of SETCC_CARRY!");
24671     // FALL THROUGH
24672   case X86ISD::SETCC:
24673     // Set the condition code or opposite one if necessary.
24674     CC = X86::CondCode(SetCC.getConstantOperandVal(0));
24675     if (needOppositeCond)
24676       CC = X86::GetOppositeBranchCondition(CC);
24677     return SetCC.getOperand(1);
24678   case X86ISD::CMOV: {
24679     // Check whether false/true value has canonical one, i.e. 0 or 1.
24680     ConstantSDNode *FVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(0));
24681     ConstantSDNode *TVal = dyn_cast<ConstantSDNode>(SetCC.getOperand(1));
24682     // Quit if true value is not a constant.
24683     if (!TVal)
24684       return SDValue();
24685     // Quit if false value is not a constant.
24686     if (!FVal) {
24687       SDValue Op = SetCC.getOperand(0);
24688       // Skip 'zext' or 'trunc' node.
24689       if (Op.getOpcode() == ISD::ZERO_EXTEND ||
24690           Op.getOpcode() == ISD::TRUNCATE)
24691         Op = Op.getOperand(0);
24692       // A special case for rdrand/rdseed, where 0 is set if false cond is
24693       // found.
24694       if ((Op.getOpcode() != X86ISD::RDRAND &&
24695            Op.getOpcode() != X86ISD::RDSEED) || Op.getResNo() != 0)
24696         return SDValue();
24697     }
24698     // Quit if false value is not the constant 0 or 1.
24699     bool FValIsFalse = true;
24700     if (FVal && FVal->getZExtValue() != 0) {
24701       if (FVal->getZExtValue() != 1)
24702         return SDValue();
24703       // If FVal is 1, opposite cond is needed.
24704       needOppositeCond = !needOppositeCond;
24705       FValIsFalse = false;
24706     }
24707     // Quit if TVal is not the constant opposite of FVal.
24708     if (FValIsFalse && TVal->getZExtValue() != 1)
24709       return SDValue();
24710     if (!FValIsFalse && TVal->getZExtValue() != 0)
24711       return SDValue();
24712     CC = X86::CondCode(SetCC.getConstantOperandVal(2));
24713     if (needOppositeCond)
24714       CC = X86::GetOppositeBranchCondition(CC);
24715     return SetCC.getOperand(3);
24716   }
24717   }
24718
24719   return SDValue();
24720 }
24721
24722 /// Check whether Cond is an AND/OR of SETCCs off of the same EFLAGS.
24723 /// Match:
24724 ///   (X86or (X86setcc) (X86setcc))
24725 ///   (X86cmp (and (X86setcc) (X86setcc)), 0)
24726 static bool checkBoolTestAndOrSetCCCombine(SDValue Cond, X86::CondCode &CC0,
24727                                            X86::CondCode &CC1, SDValue &Flags,
24728                                            bool &isAnd) {
24729   if (Cond->getOpcode() == X86ISD::CMP) {
24730     if (!isNullConstant(Cond->getOperand(1)))
24731       return false;
24732
24733     Cond = Cond->getOperand(0);
24734   }
24735
24736   isAnd = false;
24737
24738   SDValue SetCC0, SetCC1;
24739   switch (Cond->getOpcode()) {
24740   default: return false;
24741   case ISD::AND:
24742   case X86ISD::AND:
24743     isAnd = true;
24744     // fallthru
24745   case ISD::OR:
24746   case X86ISD::OR:
24747     SetCC0 = Cond->getOperand(0);
24748     SetCC1 = Cond->getOperand(1);
24749     break;
24750   };
24751
24752   // Make sure we have SETCC nodes, using the same flags value.
24753   if (SetCC0.getOpcode() != X86ISD::SETCC ||
24754       SetCC1.getOpcode() != X86ISD::SETCC ||
24755       SetCC0->getOperand(1) != SetCC1->getOperand(1))
24756     return false;
24757
24758   CC0 = (X86::CondCode)SetCC0->getConstantOperandVal(0);
24759   CC1 = (X86::CondCode)SetCC1->getConstantOperandVal(0);
24760   Flags = SetCC0->getOperand(1);
24761   return true;
24762 }
24763
24764 /// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
24765 static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
24766                                   TargetLowering::DAGCombinerInfo &DCI,
24767                                   const X86Subtarget *Subtarget) {
24768   SDLoc DL(N);
24769
24770   // If the flag operand isn't dead, don't touch this CMOV.
24771   if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
24772     return SDValue();
24773
24774   SDValue FalseOp = N->getOperand(0);
24775   SDValue TrueOp = N->getOperand(1);
24776   X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
24777   SDValue Cond = N->getOperand(3);
24778
24779   if (CC == X86::COND_E || CC == X86::COND_NE) {
24780     switch (Cond.getOpcode()) {
24781     default: break;
24782     case X86ISD::BSR:
24783     case X86ISD::BSF:
24784       // If operand of BSR / BSF are proven never zero, then ZF cannot be set.
24785       if (DAG.isKnownNeverZero(Cond.getOperand(0)))
24786         return (CC == X86::COND_E) ? FalseOp : TrueOp;
24787     }
24788   }
24789
24790   SDValue Flags;
24791
24792   Flags = checkBoolTestSetCCCombine(Cond, CC);
24793   if (Flags.getNode() &&
24794       // Extra check as FCMOV only supports a subset of X86 cond.
24795       (FalseOp.getValueType() != MVT::f80 || hasFPCMov(CC))) {
24796     SDValue Ops[] = { FalseOp, TrueOp,
24797                       DAG.getConstant(CC, DL, MVT::i8), Flags };
24798     return DAG.getNode(X86ISD::CMOV, DL, N->getVTList(), Ops);
24799   }
24800
24801   // If this is a select between two integer constants, try to do some
24802   // optimizations.  Note that the operands are ordered the opposite of SELECT
24803   // operands.
24804   if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(TrueOp)) {
24805     if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(FalseOp)) {
24806       // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
24807       // larger than FalseC (the false value).
24808       if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
24809         CC = X86::GetOppositeBranchCondition(CC);
24810         std::swap(TrueC, FalseC);
24811         std::swap(TrueOp, FalseOp);
24812       }
24813
24814       // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3.  Likewise for any pow2/0.
24815       // This is efficient for any integer data type (including i8/i16) and
24816       // shift amount.
24817       if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
24818         Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
24819                            DAG.getConstant(CC, DL, MVT::i8), Cond);
24820
24821         // Zero extend the condition if needed.
24822         Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
24823
24824         unsigned ShAmt = TrueC->getAPIntValue().logBase2();
24825         Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
24826                            DAG.getConstant(ShAmt, DL, MVT::i8));
24827         if (N->getNumValues() == 2)  // Dead flag value?
24828           return DCI.CombineTo(N, Cond, SDValue());
24829         return Cond;
24830       }
24831
24832       // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.  This is efficient
24833       // for any integer data type, including i8/i16.
24834       if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
24835         Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
24836                            DAG.getConstant(CC, DL, MVT::i8), Cond);
24837
24838         // Zero extend the condition if needed.
24839         Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
24840                            FalseC->getValueType(0), Cond);
24841         Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
24842                            SDValue(FalseC, 0));
24843
24844         if (N->getNumValues() == 2)  // Dead flag value?
24845           return DCI.CombineTo(N, Cond, SDValue());
24846         return Cond;
24847       }
24848
24849       // Optimize cases that will turn into an LEA instruction.  This requires
24850       // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
24851       if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
24852         uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
24853         if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
24854
24855         bool isFastMultiplier = false;
24856         if (Diff < 10) {
24857           switch ((unsigned char)Diff) {
24858           default: break;
24859           case 1:  // result = add base, cond
24860           case 2:  // result = lea base(    , cond*2)
24861           case 3:  // result = lea base(cond, cond*2)
24862           case 4:  // result = lea base(    , cond*4)
24863           case 5:  // result = lea base(cond, cond*4)
24864           case 8:  // result = lea base(    , cond*8)
24865           case 9:  // result = lea base(cond, cond*8)
24866             isFastMultiplier = true;
24867             break;
24868           }
24869         }
24870
24871         if (isFastMultiplier) {
24872           APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
24873           Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
24874                              DAG.getConstant(CC, DL, MVT::i8), Cond);
24875           // Zero extend the condition if needed.
24876           Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
24877                              Cond);
24878           // Scale the condition by the difference.
24879           if (Diff != 1)
24880             Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
24881                                DAG.getConstant(Diff, DL, Cond.getValueType()));
24882
24883           // Add the base if non-zero.
24884           if (FalseC->getAPIntValue() != 0)
24885             Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
24886                                SDValue(FalseC, 0));
24887           if (N->getNumValues() == 2)  // Dead flag value?
24888             return DCI.CombineTo(N, Cond, SDValue());
24889           return Cond;
24890         }
24891       }
24892     }
24893   }
24894
24895   // Handle these cases:
24896   //   (select (x != c), e, c) -> select (x != c), e, x),
24897   //   (select (x == c), c, e) -> select (x == c), x, e)
24898   // where the c is an integer constant, and the "select" is the combination
24899   // of CMOV and CMP.
24900   //
24901   // The rationale for this change is that the conditional-move from a constant
24902   // needs two instructions, however, conditional-move from a register needs
24903   // only one instruction.
24904   //
24905   // CAVEAT: By replacing a constant with a symbolic value, it may obscure
24906   //  some instruction-combining opportunities. This opt needs to be
24907   //  postponed as late as possible.
24908   //
24909   if (!DCI.isBeforeLegalize() && !DCI.isBeforeLegalizeOps()) {
24910     // the DCI.xxxx conditions are provided to postpone the optimization as
24911     // late as possible.
24912
24913     ConstantSDNode *CmpAgainst = nullptr;
24914     if ((Cond.getOpcode() == X86ISD::CMP || Cond.getOpcode() == X86ISD::SUB) &&
24915         (CmpAgainst = dyn_cast<ConstantSDNode>(Cond.getOperand(1))) &&
24916         !isa<ConstantSDNode>(Cond.getOperand(0))) {
24917
24918       if (CC == X86::COND_NE &&
24919           CmpAgainst == dyn_cast<ConstantSDNode>(FalseOp)) {
24920         CC = X86::GetOppositeBranchCondition(CC);
24921         std::swap(TrueOp, FalseOp);
24922       }
24923
24924       if (CC == X86::COND_E &&
24925           CmpAgainst == dyn_cast<ConstantSDNode>(TrueOp)) {
24926         SDValue Ops[] = { FalseOp, Cond.getOperand(0),
24927                           DAG.getConstant(CC, DL, MVT::i8), Cond };
24928         return DAG.getNode(X86ISD::CMOV, DL, N->getVTList (), Ops);
24929       }
24930     }
24931   }
24932
24933   // Fold and/or of setcc's to double CMOV:
24934   //   (CMOV F, T, ((cc1 | cc2) != 0)) -> (CMOV (CMOV F, T, cc1), T, cc2)
24935   //   (CMOV F, T, ((cc1 & cc2) != 0)) -> (CMOV (CMOV T, F, !cc1), F, !cc2)
24936   //
24937   // This combine lets us generate:
24938   //   cmovcc1 (jcc1 if we don't have CMOV)
24939   //   cmovcc2 (same)
24940   // instead of:
24941   //   setcc1
24942   //   setcc2
24943   //   and/or
24944   //   cmovne (jne if we don't have CMOV)
24945   // When we can't use the CMOV instruction, it might increase branch
24946   // mispredicts.
24947   // When we can use CMOV, or when there is no mispredict, this improves
24948   // throughput and reduces register pressure.
24949   //
24950   if (CC == X86::COND_NE) {
24951     SDValue Flags;
24952     X86::CondCode CC0, CC1;
24953     bool isAndSetCC;
24954     if (checkBoolTestAndOrSetCCCombine(Cond, CC0, CC1, Flags, isAndSetCC)) {
24955       if (isAndSetCC) {
24956         std::swap(FalseOp, TrueOp);
24957         CC0 = X86::GetOppositeBranchCondition(CC0);
24958         CC1 = X86::GetOppositeBranchCondition(CC1);
24959       }
24960
24961       SDValue LOps[] = {FalseOp, TrueOp, DAG.getConstant(CC0, DL, MVT::i8),
24962         Flags};
24963       SDValue LCMOV = DAG.getNode(X86ISD::CMOV, DL, N->getVTList(), LOps);
24964       SDValue Ops[] = {LCMOV, TrueOp, DAG.getConstant(CC1, DL, MVT::i8), Flags};
24965       SDValue CMOV = DAG.getNode(X86ISD::CMOV, DL, N->getVTList(), Ops);
24966       DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SDValue(CMOV.getNode(), 1));
24967       return CMOV;
24968     }
24969   }
24970
24971   return SDValue();
24972 }
24973
24974 /// PerformMulCombine - Optimize a single multiply with constant into two
24975 /// in order to implement it with two cheaper instructions, e.g.
24976 /// LEA + SHL, LEA + LEA.
24977 static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
24978                                  TargetLowering::DAGCombinerInfo &DCI) {
24979   // An imul is usually smaller than the alternative sequence.
24980   if (DAG.getMachineFunction().getFunction()->optForMinSize())
24981     return SDValue();
24982
24983   if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
24984     return SDValue();
24985
24986   EVT VT = N->getValueType(0);
24987   if (VT != MVT::i64 && VT != MVT::i32)
24988     return SDValue();
24989
24990   ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
24991   if (!C)
24992     return SDValue();
24993   uint64_t MulAmt = C->getZExtValue();
24994   if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
24995     return SDValue();
24996
24997   uint64_t MulAmt1 = 0;
24998   uint64_t MulAmt2 = 0;
24999   if ((MulAmt % 9) == 0) {
25000     MulAmt1 = 9;
25001     MulAmt2 = MulAmt / 9;
25002   } else if ((MulAmt % 5) == 0) {
25003     MulAmt1 = 5;
25004     MulAmt2 = MulAmt / 5;
25005   } else if ((MulAmt % 3) == 0) {
25006     MulAmt1 = 3;
25007     MulAmt2 = MulAmt / 3;
25008   }
25009
25010   SDLoc DL(N);
25011   SDValue NewMul;
25012   if (MulAmt2 &&
25013       (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
25014
25015     if (isPowerOf2_64(MulAmt2) &&
25016         !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
25017       // If second multiplifer is pow2, issue it first. We want the multiply by
25018       // 3, 5, or 9 to be folded into the addressing mode unless the lone use
25019       // is an add.
25020       std::swap(MulAmt1, MulAmt2);
25021
25022     if (isPowerOf2_64(MulAmt1))
25023       NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
25024                            DAG.getConstant(Log2_64(MulAmt1), DL, MVT::i8));
25025     else
25026       NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
25027                            DAG.getConstant(MulAmt1, DL, VT));
25028
25029     if (isPowerOf2_64(MulAmt2))
25030       NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
25031                            DAG.getConstant(Log2_64(MulAmt2), DL, MVT::i8));
25032     else
25033       NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
25034                            DAG.getConstant(MulAmt2, DL, VT));
25035   }
25036
25037   if (!NewMul) {
25038     assert(MulAmt != 0 && MulAmt != (VT == MVT::i64 ? UINT64_MAX : UINT32_MAX)
25039            && "Both cases that could cause potential overflows should have "
25040               "already been handled.");
25041     if (isPowerOf2_64(MulAmt - 1))
25042       // (mul x, 2^N + 1) => (add (shl x, N), x)
25043       NewMul = DAG.getNode(ISD::ADD, DL, VT, N->getOperand(0),
25044                                 DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
25045                                 DAG.getConstant(Log2_64(MulAmt - 1), DL,
25046                                 MVT::i8)));
25047
25048     else if (isPowerOf2_64(MulAmt + 1))
25049       // (mul x, 2^N - 1) => (sub (shl x, N), x)
25050       NewMul = DAG.getNode(ISD::SUB, DL, VT, DAG.getNode(ISD::SHL, DL, VT,
25051                                 N->getOperand(0),
25052                                 DAG.getConstant(Log2_64(MulAmt + 1),
25053                                 DL, MVT::i8)), N->getOperand(0));
25054   }
25055
25056   if (NewMul)
25057     // Do not add new nodes to DAG combiner worklist.
25058     DCI.CombineTo(N, NewMul, false);
25059
25060   return SDValue();
25061 }
25062
25063 static SDValue PerformSHLCombine(SDNode *N, SelectionDAG &DAG) {
25064   SDValue N0 = N->getOperand(0);
25065   SDValue N1 = N->getOperand(1);
25066   ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N1);
25067   EVT VT = N0.getValueType();
25068
25069   // fold (shl (and (setcc_c), c1), c2) -> (and setcc_c, (c1 << c2))
25070   // since the result of setcc_c is all zero's or all ones.
25071   if (VT.isInteger() && !VT.isVector() &&
25072       N1C && N0.getOpcode() == ISD::AND &&
25073       N0.getOperand(1).getOpcode() == ISD::Constant) {
25074     SDValue N00 = N0.getOperand(0);
25075     APInt Mask = cast<ConstantSDNode>(N0.getOperand(1))->getAPIntValue();
25076     APInt ShAmt = N1C->getAPIntValue();
25077     Mask = Mask.shl(ShAmt);
25078     bool MaskOK = false;
25079     // We can handle cases concerning bit-widening nodes containing setcc_c if
25080     // we carefully interrogate the mask to make sure we are semantics
25081     // preserving.
25082     // The transform is not safe if the result of C1 << C2 exceeds the bitwidth
25083     // of the underlying setcc_c operation if the setcc_c was zero extended.
25084     // Consider the following example:
25085     //   zext(setcc_c)                 -> i32 0x0000FFFF
25086     //   c1                            -> i32 0x0000FFFF
25087     //   c2                            -> i32 0x00000001
25088     //   (shl (and (setcc_c), c1), c2) -> i32 0x0001FFFE
25089     //   (and setcc_c, (c1 << c2))     -> i32 0x0000FFFE
25090     if (N00.getOpcode() == X86ISD::SETCC_CARRY) {
25091       MaskOK = true;
25092     } else if (N00.getOpcode() == ISD::SIGN_EXTEND &&
25093                N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
25094       MaskOK = true;
25095     } else if ((N00.getOpcode() == ISD::ZERO_EXTEND ||
25096                 N00.getOpcode() == ISD::ANY_EXTEND) &&
25097                N00.getOperand(0).getOpcode() == X86ISD::SETCC_CARRY) {
25098       MaskOK = Mask.isIntN(N00.getOperand(0).getValueSizeInBits());
25099     }
25100     if (MaskOK && Mask != 0) {
25101       SDLoc DL(N);
25102       return DAG.getNode(ISD::AND, DL, VT, N00, DAG.getConstant(Mask, DL, VT));
25103     }
25104   }
25105
25106   // Hardware support for vector shifts is sparse which makes us scalarize the
25107   // vector operations in many cases. Also, on sandybridge ADD is faster than
25108   // shl.
25109   // (shl V, 1) -> add V,V
25110   if (auto *N1BV = dyn_cast<BuildVectorSDNode>(N1))
25111     if (auto *N1SplatC = N1BV->getConstantSplatNode()) {
25112       assert(N0.getValueType().isVector() && "Invalid vector shift type");
25113       // We shift all of the values by one. In many cases we do not have
25114       // hardware support for this operation. This is better expressed as an ADD
25115       // of two values.
25116       if (N1SplatC->getAPIntValue() == 1)
25117         return DAG.getNode(ISD::ADD, SDLoc(N), VT, N0, N0);
25118     }
25119
25120   return SDValue();
25121 }
25122
25123 static SDValue PerformSRACombine(SDNode *N, SelectionDAG &DAG) {
25124   SDValue N0 = N->getOperand(0);
25125   SDValue N1 = N->getOperand(1);
25126   EVT VT = N0.getValueType();
25127   unsigned Size = VT.getSizeInBits();
25128
25129   // fold (ashr (shl, a, [56,48,32,24,16]), SarConst)
25130   // into (shl, (sext (a), [56,48,32,24,16] - SarConst)) or
25131   // into (lshr, (sext (a), SarConst - [56,48,32,24,16]))
25132   // depending on sign of (SarConst - [56,48,32,24,16])
25133
25134   // sexts in X86 are MOVs. The MOVs have the same code size
25135   // as above SHIFTs (only SHIFT on 1 has lower code size).
25136   // However the MOVs have 2 advantages to a SHIFT:
25137   // 1. MOVs can write to a register that differs from source
25138   // 2. MOVs accept memory operands
25139
25140   if (!VT.isInteger() || VT.isVector() || N1.getOpcode() != ISD::Constant ||
25141       N0.getOpcode() != ISD::SHL || !N0.hasOneUse() ||
25142       N0.getOperand(1).getOpcode() != ISD::Constant)
25143     return SDValue();
25144
25145   SDValue N00 = N0.getOperand(0);
25146   SDValue N01 = N0.getOperand(1);
25147   APInt ShlConst = (cast<ConstantSDNode>(N01))->getAPIntValue();
25148   APInt SarConst = (cast<ConstantSDNode>(N1))->getAPIntValue();
25149   EVT CVT = N1.getValueType();
25150
25151   if (SarConst.isNegative())
25152     return SDValue();
25153
25154   for (MVT SVT : MVT::integer_valuetypes()) {
25155     unsigned ShiftSize = SVT.getSizeInBits();
25156     // skipping types without corresponding sext/zext and
25157     // ShlConst that is not one of [56,48,32,24,16]
25158     if (ShiftSize < 8 || ShiftSize > 64 || ShlConst != Size - ShiftSize)
25159       continue;
25160     SDLoc DL(N);
25161     SDValue NN =
25162         DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT, N00, DAG.getValueType(SVT));
25163     SarConst = SarConst - (Size - ShiftSize);
25164     if (SarConst == 0)
25165       return NN;
25166     else if (SarConst.isNegative())
25167       return DAG.getNode(ISD::SHL, DL, VT, NN,
25168                          DAG.getConstant(-SarConst, DL, CVT));
25169     else
25170       return DAG.getNode(ISD::SRA, DL, VT, NN,
25171                          DAG.getConstant(SarConst, DL, CVT));
25172   }
25173   return SDValue();
25174 }
25175
25176 /// \brief Returns a vector of 0s if the node in input is a vector logical
25177 /// shift by a constant amount which is known to be bigger than or equal
25178 /// to the vector element size in bits.
25179 static SDValue performShiftToAllZeros(SDNode *N, SelectionDAG &DAG,
25180                                       const X86Subtarget *Subtarget) {
25181   EVT VT = N->getValueType(0);
25182
25183   if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16 &&
25184       (!Subtarget->hasInt256() ||
25185        (VT != MVT::v4i64 && VT != MVT::v8i32 && VT != MVT::v16i16)))
25186     return SDValue();
25187
25188   SDValue Amt = N->getOperand(1);
25189   SDLoc DL(N);
25190   if (auto *AmtBV = dyn_cast<BuildVectorSDNode>(Amt))
25191     if (auto *AmtSplat = AmtBV->getConstantSplatNode()) {
25192       APInt ShiftAmt = AmtSplat->getAPIntValue();
25193       unsigned MaxAmount =
25194         VT.getSimpleVT().getVectorElementType().getSizeInBits();
25195
25196       // SSE2/AVX2 logical shifts always return a vector of 0s
25197       // if the shift amount is bigger than or equal to
25198       // the element size. The constant shift amount will be
25199       // encoded as a 8-bit immediate.
25200       if (ShiftAmt.trunc(8).uge(MaxAmount))
25201         return getZeroVector(VT.getSimpleVT(), Subtarget, DAG, DL);
25202     }
25203
25204   return SDValue();
25205 }
25206
25207 /// PerformShiftCombine - Combine shifts.
25208 static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
25209                                    TargetLowering::DAGCombinerInfo &DCI,
25210                                    const X86Subtarget *Subtarget) {
25211   if (N->getOpcode() == ISD::SHL)
25212     if (SDValue V = PerformSHLCombine(N, DAG))
25213       return V;
25214
25215   if (N->getOpcode() == ISD::SRA)
25216     if (SDValue V = PerformSRACombine(N, DAG))
25217       return V;
25218
25219   // Try to fold this logical shift into a zero vector.
25220   if (N->getOpcode() != ISD::SRA)
25221     if (SDValue V = performShiftToAllZeros(N, DAG, Subtarget))
25222       return V;
25223
25224   return SDValue();
25225 }
25226
25227 // CMPEQCombine - Recognize the distinctive  (AND (setcc ...) (setcc ..))
25228 // where both setccs reference the same FP CMP, and rewrite for CMPEQSS
25229 // and friends.  Likewise for OR -> CMPNEQSS.
25230 static SDValue CMPEQCombine(SDNode *N, SelectionDAG &DAG,
25231                             TargetLowering::DAGCombinerInfo &DCI,
25232                             const X86Subtarget *Subtarget) {
25233   unsigned opcode;
25234
25235   // SSE1 supports CMP{eq|ne}SS, and SSE2 added CMP{eq|ne}SD, but
25236   // we're requiring SSE2 for both.
25237   if (Subtarget->hasSSE2() && isAndOrOfSetCCs(SDValue(N, 0U), opcode)) {
25238     SDValue N0 = N->getOperand(0);
25239     SDValue N1 = N->getOperand(1);
25240     SDValue CMP0 = N0->getOperand(1);
25241     SDValue CMP1 = N1->getOperand(1);
25242     SDLoc DL(N);
25243
25244     // The SETCCs should both refer to the same CMP.
25245     if (CMP0.getOpcode() != X86ISD::CMP || CMP0 != CMP1)
25246       return SDValue();
25247
25248     SDValue CMP00 = CMP0->getOperand(0);
25249     SDValue CMP01 = CMP0->getOperand(1);
25250     EVT     VT    = CMP00.getValueType();
25251
25252     if (VT == MVT::f32 || VT == MVT::f64) {
25253       bool ExpectingFlags = false;
25254       // Check for any users that want flags:
25255       for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
25256            !ExpectingFlags && UI != UE; ++UI)
25257         switch (UI->getOpcode()) {
25258         default:
25259         case ISD::BR_CC:
25260         case ISD::BRCOND:
25261         case ISD::SELECT:
25262           ExpectingFlags = true;
25263           break;
25264         case ISD::CopyToReg:
25265         case ISD::SIGN_EXTEND:
25266         case ISD::ZERO_EXTEND:
25267         case ISD::ANY_EXTEND:
25268           break;
25269         }
25270
25271       if (!ExpectingFlags) {
25272         enum X86::CondCode cc0 = (enum X86::CondCode)N0.getConstantOperandVal(0);
25273         enum X86::CondCode cc1 = (enum X86::CondCode)N1.getConstantOperandVal(0);
25274
25275         if (cc1 == X86::COND_E || cc1 == X86::COND_NE) {
25276           X86::CondCode tmp = cc0;
25277           cc0 = cc1;
25278           cc1 = tmp;
25279         }
25280
25281         if ((cc0 == X86::COND_E  && cc1 == X86::COND_NP) ||
25282             (cc0 == X86::COND_NE && cc1 == X86::COND_P)) {
25283           // FIXME: need symbolic constants for these magic numbers.
25284           // See X86ATTInstPrinter.cpp:printSSECC().
25285           unsigned x86cc = (cc0 == X86::COND_E) ? 0 : 4;
25286           if (Subtarget->hasAVX512()) {
25287             SDValue FSetCC = DAG.getNode(X86ISD::FSETCC, DL, MVT::i1, CMP00,
25288                                          CMP01,
25289                                          DAG.getConstant(x86cc, DL, MVT::i8));
25290             if (N->getValueType(0) != MVT::i1)
25291               return DAG.getNode(ISD::ZERO_EXTEND, DL, N->getValueType(0),
25292                                  FSetCC);
25293             return FSetCC;
25294           }
25295           SDValue OnesOrZeroesF = DAG.getNode(X86ISD::FSETCC, DL,
25296                                               CMP00.getValueType(), CMP00, CMP01,
25297                                               DAG.getConstant(x86cc, DL,
25298                                                               MVT::i8));
25299
25300           bool is64BitFP = (CMP00.getValueType() == MVT::f64);
25301           MVT IntVT = is64BitFP ? MVT::i64 : MVT::i32;
25302
25303           if (is64BitFP && !Subtarget->is64Bit()) {
25304             // On a 32-bit target, we cannot bitcast the 64-bit float to a
25305             // 64-bit integer, since that's not a legal type. Since
25306             // OnesOrZeroesF is all ones of all zeroes, we don't need all the
25307             // bits, but can do this little dance to extract the lowest 32 bits
25308             // and work with those going forward.
25309             SDValue Vector64 = DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, MVT::v2f64,
25310                                            OnesOrZeroesF);
25311             SDValue Vector32 = DAG.getBitcast(MVT::v4f32, Vector64);
25312             OnesOrZeroesF = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::f32,
25313                                         Vector32, DAG.getIntPtrConstant(0, DL));
25314             IntVT = MVT::i32;
25315           }
25316
25317           SDValue OnesOrZeroesI = DAG.getBitcast(IntVT, OnesOrZeroesF);
25318           SDValue ANDed = DAG.getNode(ISD::AND, DL, IntVT, OnesOrZeroesI,
25319                                       DAG.getConstant(1, DL, IntVT));
25320           SDValue OneBitOfTruth = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8,
25321                                               ANDed);
25322           return OneBitOfTruth;
25323         }
25324       }
25325     }
25326   }
25327   return SDValue();
25328 }
25329
25330 /// CanFoldXORWithAllOnes - Test whether the XOR operand is a AllOnes vector
25331 /// so it can be folded inside ANDNP.
25332 static bool CanFoldXORWithAllOnes(const SDNode *N) {
25333   EVT VT = N->getValueType(0);
25334
25335   // Match direct AllOnes for 128 and 256-bit vectors
25336   if (ISD::isBuildVectorAllOnes(N))
25337     return true;
25338
25339   // Look through a bit convert.
25340   if (N->getOpcode() == ISD::BITCAST)
25341     N = N->getOperand(0).getNode();
25342
25343   // Sometimes the operand may come from a insert_subvector building a 256-bit
25344   // allones vector
25345   if (VT.is256BitVector() &&
25346       N->getOpcode() == ISD::INSERT_SUBVECTOR) {
25347     SDValue V1 = N->getOperand(0);
25348     SDValue V2 = N->getOperand(1);
25349
25350     if (V1.getOpcode() == ISD::INSERT_SUBVECTOR &&
25351         V1.getOperand(0).getOpcode() == ISD::UNDEF &&
25352         ISD::isBuildVectorAllOnes(V1.getOperand(1).getNode()) &&
25353         ISD::isBuildVectorAllOnes(V2.getNode()))
25354       return true;
25355   }
25356
25357   return false;
25358 }
25359
25360 // On AVX/AVX2 the type v8i1 is legalized to v8i16, which is an XMM sized
25361 // register. In most cases we actually compare or select YMM-sized registers
25362 // and mixing the two types creates horrible code. This method optimizes
25363 // some of the transition sequences.
25364 static SDValue WidenMaskArithmetic(SDNode *N, SelectionDAG &DAG,
25365                                  TargetLowering::DAGCombinerInfo &DCI,
25366                                  const X86Subtarget *Subtarget) {
25367   EVT VT = N->getValueType(0);
25368   if (!VT.is256BitVector())
25369     return SDValue();
25370
25371   assert((N->getOpcode() == ISD::ANY_EXTEND ||
25372           N->getOpcode() == ISD::ZERO_EXTEND ||
25373           N->getOpcode() == ISD::SIGN_EXTEND) && "Invalid Node");
25374
25375   SDValue Narrow = N->getOperand(0);
25376   EVT NarrowVT = Narrow->getValueType(0);
25377   if (!NarrowVT.is128BitVector())
25378     return SDValue();
25379
25380   if (Narrow->getOpcode() != ISD::XOR &&
25381       Narrow->getOpcode() != ISD::AND &&
25382       Narrow->getOpcode() != ISD::OR)
25383     return SDValue();
25384
25385   SDValue N0  = Narrow->getOperand(0);
25386   SDValue N1  = Narrow->getOperand(1);
25387   SDLoc DL(Narrow);
25388
25389   // The Left side has to be a trunc.
25390   if (N0.getOpcode() != ISD::TRUNCATE)
25391     return SDValue();
25392
25393   // The type of the truncated inputs.
25394   EVT WideVT = N0->getOperand(0)->getValueType(0);
25395   if (WideVT != VT)
25396     return SDValue();
25397
25398   // The right side has to be a 'trunc' or a constant vector.
25399   bool RHSTrunc = N1.getOpcode() == ISD::TRUNCATE;
25400   ConstantSDNode *RHSConstSplat = nullptr;
25401   if (auto *RHSBV = dyn_cast<BuildVectorSDNode>(N1))
25402     RHSConstSplat = RHSBV->getConstantSplatNode();
25403   if (!RHSTrunc && !RHSConstSplat)
25404     return SDValue();
25405
25406   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
25407
25408   if (!TLI.isOperationLegalOrPromote(Narrow->getOpcode(), WideVT))
25409     return SDValue();
25410
25411   // Set N0 and N1 to hold the inputs to the new wide operation.
25412   N0 = N0->getOperand(0);
25413   if (RHSConstSplat) {
25414     N1 = DAG.getNode(ISD::ZERO_EXTEND, DL, WideVT.getVectorElementType(),
25415                      SDValue(RHSConstSplat, 0));
25416     SmallVector<SDValue, 8> C(WideVT.getVectorNumElements(), N1);
25417     N1 = DAG.getNode(ISD::BUILD_VECTOR, DL, WideVT, C);
25418   } else if (RHSTrunc) {
25419     N1 = N1->getOperand(0);
25420   }
25421
25422   // Generate the wide operation.
25423   SDValue Op = DAG.getNode(Narrow->getOpcode(), DL, WideVT, N0, N1);
25424   unsigned Opcode = N->getOpcode();
25425   switch (Opcode) {
25426   case ISD::ANY_EXTEND:
25427     return Op;
25428   case ISD::ZERO_EXTEND: {
25429     unsigned InBits = NarrowVT.getScalarSizeInBits();
25430     APInt Mask = APInt::getAllOnesValue(InBits);
25431     Mask = Mask.zext(VT.getScalarSizeInBits());
25432     return DAG.getNode(ISD::AND, DL, VT,
25433                        Op, DAG.getConstant(Mask, DL, VT));
25434   }
25435   case ISD::SIGN_EXTEND:
25436     return DAG.getNode(ISD::SIGN_EXTEND_INREG, DL, VT,
25437                        Op, DAG.getValueType(NarrowVT));
25438   default:
25439     llvm_unreachable("Unexpected opcode");
25440   }
25441 }
25442
25443 static SDValue VectorZextCombine(SDNode *N, SelectionDAG &DAG,
25444                                  TargetLowering::DAGCombinerInfo &DCI,
25445                                  const X86Subtarget *Subtarget) {
25446   SDValue N0 = N->getOperand(0);
25447   SDValue N1 = N->getOperand(1);
25448   SDLoc DL(N);
25449
25450   // A vector zext_in_reg may be represented as a shuffle,
25451   // feeding into a bitcast (this represents anyext) feeding into
25452   // an and with a mask.
25453   // We'd like to try to combine that into a shuffle with zero
25454   // plus a bitcast, removing the and.
25455   if (N0.getOpcode() != ISD::BITCAST ||
25456       N0.getOperand(0).getOpcode() != ISD::VECTOR_SHUFFLE)
25457     return SDValue();
25458
25459   // The other side of the AND should be a splat of 2^C, where C
25460   // is the number of bits in the source type.
25461   if (N1.getOpcode() == ISD::BITCAST)
25462     N1 = N1.getOperand(0);
25463   if (N1.getOpcode() != ISD::BUILD_VECTOR)
25464     return SDValue();
25465   BuildVectorSDNode *Vector = cast<BuildVectorSDNode>(N1);
25466
25467   ShuffleVectorSDNode *Shuffle = cast<ShuffleVectorSDNode>(N0.getOperand(0));
25468   EVT SrcType = Shuffle->getValueType(0);
25469
25470   // We expect a single-source shuffle
25471   if (Shuffle->getOperand(1)->getOpcode() != ISD::UNDEF)
25472     return SDValue();
25473
25474   unsigned SrcSize = SrcType.getScalarSizeInBits();
25475
25476   APInt SplatValue, SplatUndef;
25477   unsigned SplatBitSize;
25478   bool HasAnyUndefs;
25479   if (!Vector->isConstantSplat(SplatValue, SplatUndef,
25480                                 SplatBitSize, HasAnyUndefs))
25481     return SDValue();
25482
25483   unsigned ResSize = N1.getValueType().getScalarSizeInBits();
25484   // Make sure the splat matches the mask we expect
25485   if (SplatBitSize > ResSize ||
25486       (SplatValue + 1).exactLogBase2() != (int)SrcSize)
25487     return SDValue();
25488
25489   // Make sure the input and output size make sense
25490   if (SrcSize >= ResSize || ResSize % SrcSize)
25491     return SDValue();
25492
25493   // We expect a shuffle of the form <0, u, u, u, 1, u, u, u...>
25494   // The number of u's between each two values depends on the ratio between
25495   // the source and dest type.
25496   unsigned ZextRatio = ResSize / SrcSize;
25497   bool IsZext = true;
25498   for (unsigned i = 0; i < SrcType.getVectorNumElements(); ++i) {
25499     if (i % ZextRatio) {
25500       if (Shuffle->getMaskElt(i) > 0) {
25501         // Expected undef
25502         IsZext = false;
25503         break;
25504       }
25505     } else {
25506       if (Shuffle->getMaskElt(i) != (int)(i / ZextRatio)) {
25507         // Expected element number
25508         IsZext = false;
25509         break;
25510       }
25511     }
25512   }
25513
25514   if (!IsZext)
25515     return SDValue();
25516
25517   // Ok, perform the transformation - replace the shuffle with
25518   // a shuffle of the form <0, k, k, k, 1, k, k, k> with zero
25519   // (instead of undef) where the k elements come from the zero vector.
25520   SmallVector<int, 8> Mask;
25521   unsigned NumElems = SrcType.getVectorNumElements();
25522   for (unsigned i = 0; i < NumElems; ++i)
25523     if (i % ZextRatio)
25524       Mask.push_back(NumElems);
25525     else
25526       Mask.push_back(i / ZextRatio);
25527
25528   SDValue NewShuffle = DAG.getVectorShuffle(Shuffle->getValueType(0), DL,
25529     Shuffle->getOperand(0), DAG.getConstant(0, DL, SrcType), Mask);
25530   return DAG.getBitcast(N0.getValueType(), NewShuffle);
25531 }
25532
25533 /// If both input operands of a logic op are being cast from floating point
25534 /// types, try to convert this into a floating point logic node to avoid
25535 /// unnecessary moves from SSE to integer registers.
25536 static SDValue convertIntLogicToFPLogic(SDNode *N, SelectionDAG &DAG,
25537                                         const X86Subtarget *Subtarget) {
25538   unsigned FPOpcode = ISD::DELETED_NODE;
25539   if (N->getOpcode() == ISD::AND)
25540     FPOpcode = X86ISD::FAND;
25541   else if (N->getOpcode() == ISD::OR)
25542     FPOpcode = X86ISD::FOR;
25543   else if (N->getOpcode() == ISD::XOR)
25544     FPOpcode = X86ISD::FXOR;
25545
25546   assert(FPOpcode != ISD::DELETED_NODE &&
25547          "Unexpected input node for FP logic conversion");
25548
25549   EVT VT = N->getValueType(0);
25550   SDValue N0 = N->getOperand(0);
25551   SDValue N1 = N->getOperand(1);
25552   SDLoc DL(N);
25553   if (N0.getOpcode() == ISD::BITCAST && N1.getOpcode() == ISD::BITCAST &&
25554       ((Subtarget->hasSSE1() && VT == MVT::i32) ||
25555        (Subtarget->hasSSE2() && VT == MVT::i64))) {
25556     SDValue N00 = N0.getOperand(0);
25557     SDValue N10 = N1.getOperand(0);
25558     EVT N00Type = N00.getValueType();
25559     EVT N10Type = N10.getValueType();
25560     if (N00Type.isFloatingPoint() && N10Type.isFloatingPoint()) {
25561       SDValue FPLogic = DAG.getNode(FPOpcode, DL, N00Type, N00, N10);
25562       return DAG.getBitcast(VT, FPLogic);
25563     }
25564   }
25565   return SDValue();
25566 }
25567
25568 static SDValue PerformAndCombine(SDNode *N, SelectionDAG &DAG,
25569                                  TargetLowering::DAGCombinerInfo &DCI,
25570                                  const X86Subtarget *Subtarget) {
25571   if (DCI.isBeforeLegalizeOps())
25572     return SDValue();
25573
25574   if (SDValue Zext = VectorZextCombine(N, DAG, DCI, Subtarget))
25575     return Zext;
25576
25577   if (SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget))
25578     return R;
25579
25580   if (SDValue FPLogic = convertIntLogicToFPLogic(N, DAG, Subtarget))
25581     return FPLogic;
25582
25583   EVT VT = N->getValueType(0);
25584   SDValue N0 = N->getOperand(0);
25585   SDValue N1 = N->getOperand(1);
25586   SDLoc DL(N);
25587
25588   // Create BEXTR instructions
25589   // BEXTR is ((X >> imm) & (2**size-1))
25590   if (VT == MVT::i32 || VT == MVT::i64) {
25591     // Check for BEXTR.
25592     if ((Subtarget->hasBMI() || Subtarget->hasTBM()) &&
25593         (N0.getOpcode() == ISD::SRA || N0.getOpcode() == ISD::SRL)) {
25594       ConstantSDNode *MaskNode = dyn_cast<ConstantSDNode>(N1);
25595       ConstantSDNode *ShiftNode = dyn_cast<ConstantSDNode>(N0.getOperand(1));
25596       if (MaskNode && ShiftNode) {
25597         uint64_t Mask = MaskNode->getZExtValue();
25598         uint64_t Shift = ShiftNode->getZExtValue();
25599         if (isMask_64(Mask)) {
25600           uint64_t MaskSize = countPopulation(Mask);
25601           if (Shift + MaskSize <= VT.getSizeInBits())
25602             return DAG.getNode(X86ISD::BEXTR, DL, VT, N0.getOperand(0),
25603                                DAG.getConstant(Shift | (MaskSize << 8), DL,
25604                                                VT));
25605         }
25606       }
25607     } // BEXTR
25608
25609     return SDValue();
25610   }
25611
25612   // Want to form ANDNP nodes:
25613   // 1) In the hopes of then easily combining them with OR and AND nodes
25614   //    to form PBLEND/PSIGN.
25615   // 2) To match ANDN packed intrinsics
25616   if (VT != MVT::v2i64 && VT != MVT::v4i64)
25617     return SDValue();
25618
25619   // Check LHS for vnot
25620   if (N0.getOpcode() == ISD::XOR &&
25621       //ISD::isBuildVectorAllOnes(N0.getOperand(1).getNode()))
25622       CanFoldXORWithAllOnes(N0.getOperand(1).getNode()))
25623     return DAG.getNode(X86ISD::ANDNP, DL, VT, N0.getOperand(0), N1);
25624
25625   // Check RHS for vnot
25626   if (N1.getOpcode() == ISD::XOR &&
25627       //ISD::isBuildVectorAllOnes(N1.getOperand(1).getNode()))
25628       CanFoldXORWithAllOnes(N1.getOperand(1).getNode()))
25629     return DAG.getNode(X86ISD::ANDNP, DL, VT, N1.getOperand(0), N0);
25630
25631   return SDValue();
25632 }
25633
25634 static SDValue PerformOrCombine(SDNode *N, SelectionDAG &DAG,
25635                                 TargetLowering::DAGCombinerInfo &DCI,
25636                                 const X86Subtarget *Subtarget) {
25637   if (DCI.isBeforeLegalizeOps())
25638     return SDValue();
25639
25640   if (SDValue R = CMPEQCombine(N, DAG, DCI, Subtarget))
25641     return R;
25642
25643   if (SDValue FPLogic = convertIntLogicToFPLogic(N, DAG, Subtarget))
25644     return FPLogic;
25645
25646   SDValue N0 = N->getOperand(0);
25647   SDValue N1 = N->getOperand(1);
25648   EVT VT = N->getValueType(0);
25649
25650   // look for psign/blend
25651   if (VT == MVT::v2i64 || VT == MVT::v4i64) {
25652     if (!Subtarget->hasSSSE3() ||
25653         (VT == MVT::v4i64 && !Subtarget->hasInt256()))
25654       return SDValue();
25655
25656     // Canonicalize pandn to RHS
25657     if (N0.getOpcode() == X86ISD::ANDNP)
25658       std::swap(N0, N1);
25659     // or (and (m, y), (pandn m, x))
25660     if (N0.getOpcode() == ISD::AND && N1.getOpcode() == X86ISD::ANDNP) {
25661       SDValue Mask = N1.getOperand(0);
25662       SDValue X    = N1.getOperand(1);
25663       SDValue Y;
25664       if (N0.getOperand(0) == Mask)
25665         Y = N0.getOperand(1);
25666       if (N0.getOperand(1) == Mask)
25667         Y = N0.getOperand(0);
25668
25669       // Check to see if the mask appeared in both the AND and ANDNP and
25670       if (!Y.getNode())
25671         return SDValue();
25672
25673       // Validate that X, Y, and Mask are BIT_CONVERTS, and see through them.
25674       // Look through mask bitcast.
25675       if (Mask.getOpcode() == ISD::BITCAST)
25676         Mask = Mask.getOperand(0);
25677       if (X.getOpcode() == ISD::BITCAST)
25678         X = X.getOperand(0);
25679       if (Y.getOpcode() == ISD::BITCAST)
25680         Y = Y.getOperand(0);
25681
25682       EVT MaskVT = Mask.getValueType();
25683
25684       // Validate that the Mask operand is a vector sra node.
25685       // FIXME: what to do for bytes, since there is a psignb/pblendvb, but
25686       // there is no psrai.b
25687       unsigned EltBits = MaskVT.getVectorElementType().getSizeInBits();
25688       unsigned SraAmt = ~0;
25689       if (Mask.getOpcode() == ISD::SRA) {
25690         if (auto *AmtBV = dyn_cast<BuildVectorSDNode>(Mask.getOperand(1)))
25691           if (auto *AmtConst = AmtBV->getConstantSplatNode())
25692             SraAmt = AmtConst->getZExtValue();
25693       } else if (Mask.getOpcode() == X86ISD::VSRAI) {
25694         SDValue SraC = Mask.getOperand(1);
25695         SraAmt  = cast<ConstantSDNode>(SraC)->getZExtValue();
25696       }
25697       if ((SraAmt + 1) != EltBits)
25698         return SDValue();
25699
25700       SDLoc DL(N);
25701
25702       // Now we know we at least have a plendvb with the mask val.  See if
25703       // we can form a psignb/w/d.
25704       // psign = x.type == y.type == mask.type && y = sub(0, x);
25705       if (Y.getOpcode() == ISD::SUB && Y.getOperand(1) == X &&
25706           ISD::isBuildVectorAllZeros(Y.getOperand(0).getNode()) &&
25707           X.getValueType() == MaskVT && Y.getValueType() == MaskVT) {
25708         assert((EltBits == 8 || EltBits == 16 || EltBits == 32) &&
25709                "Unsupported VT for PSIGN");
25710         Mask = DAG.getNode(X86ISD::PSIGN, DL, MaskVT, X, Mask.getOperand(0));
25711         return DAG.getBitcast(VT, Mask);
25712       }
25713       // PBLENDVB only available on SSE 4.1
25714       if (!Subtarget->hasSSE41())
25715         return SDValue();
25716
25717       MVT BlendVT = (VT == MVT::v4i64) ? MVT::v32i8 : MVT::v16i8;
25718
25719       X = DAG.getBitcast(BlendVT, X);
25720       Y = DAG.getBitcast(BlendVT, Y);
25721       Mask = DAG.getBitcast(BlendVT, Mask);
25722       Mask = DAG.getNode(ISD::VSELECT, DL, BlendVT, Mask, Y, X);
25723       return DAG.getBitcast(VT, Mask);
25724     }
25725   }
25726
25727   if (VT != MVT::i16 && VT != MVT::i32 && VT != MVT::i64)
25728     return SDValue();
25729
25730   // fold (or (x << c) | (y >> (64 - c))) ==> (shld64 x, y, c)
25731   bool OptForSize = DAG.getMachineFunction().getFunction()->optForSize();
25732
25733   // SHLD/SHRD instructions have lower register pressure, but on some
25734   // platforms they have higher latency than the equivalent
25735   // series of shifts/or that would otherwise be generated.
25736   // Don't fold (or (x << c) | (y >> (64 - c))) if SHLD/SHRD instructions
25737   // have higher latencies and we are not optimizing for size.
25738   if (!OptForSize && Subtarget->isSHLDSlow())
25739     return SDValue();
25740
25741   if (N0.getOpcode() == ISD::SRL && N1.getOpcode() == ISD::SHL)
25742     std::swap(N0, N1);
25743   if (N0.getOpcode() != ISD::SHL || N1.getOpcode() != ISD::SRL)
25744     return SDValue();
25745   if (!N0.hasOneUse() || !N1.hasOneUse())
25746     return SDValue();
25747
25748   SDValue ShAmt0 = N0.getOperand(1);
25749   if (ShAmt0.getValueType() != MVT::i8)
25750     return SDValue();
25751   SDValue ShAmt1 = N1.getOperand(1);
25752   if (ShAmt1.getValueType() != MVT::i8)
25753     return SDValue();
25754   if (ShAmt0.getOpcode() == ISD::TRUNCATE)
25755     ShAmt0 = ShAmt0.getOperand(0);
25756   if (ShAmt1.getOpcode() == ISD::TRUNCATE)
25757     ShAmt1 = ShAmt1.getOperand(0);
25758
25759   SDLoc DL(N);
25760   unsigned Opc = X86ISD::SHLD;
25761   SDValue Op0 = N0.getOperand(0);
25762   SDValue Op1 = N1.getOperand(0);
25763   if (ShAmt0.getOpcode() == ISD::SUB) {
25764     Opc = X86ISD::SHRD;
25765     std::swap(Op0, Op1);
25766     std::swap(ShAmt0, ShAmt1);
25767   }
25768
25769   unsigned Bits = VT.getSizeInBits();
25770   if (ShAmt1.getOpcode() == ISD::SUB) {
25771     SDValue Sum = ShAmt1.getOperand(0);
25772     if (ConstantSDNode *SumC = dyn_cast<ConstantSDNode>(Sum)) {
25773       SDValue ShAmt1Op1 = ShAmt1.getOperand(1);
25774       if (ShAmt1Op1.getNode()->getOpcode() == ISD::TRUNCATE)
25775         ShAmt1Op1 = ShAmt1Op1.getOperand(0);
25776       if (SumC->getSExtValue() == Bits && ShAmt1Op1 == ShAmt0)
25777         return DAG.getNode(Opc, DL, VT,
25778                            Op0, Op1,
25779                            DAG.getNode(ISD::TRUNCATE, DL,
25780                                        MVT::i8, ShAmt0));
25781     }
25782   } else if (ConstantSDNode *ShAmt1C = dyn_cast<ConstantSDNode>(ShAmt1)) {
25783     ConstantSDNode *ShAmt0C = dyn_cast<ConstantSDNode>(ShAmt0);
25784     if (ShAmt0C &&
25785         ShAmt0C->getSExtValue() + ShAmt1C->getSExtValue() == Bits)
25786       return DAG.getNode(Opc, DL, VT,
25787                          N0.getOperand(0), N1.getOperand(0),
25788                          DAG.getNode(ISD::TRUNCATE, DL,
25789                                        MVT::i8, ShAmt0));
25790   }
25791
25792   return SDValue();
25793 }
25794
25795 // Generate NEG and CMOV for integer abs.
25796 static SDValue performIntegerAbsCombine(SDNode *N, SelectionDAG &DAG) {
25797   EVT VT = N->getValueType(0);
25798
25799   // Since X86 does not have CMOV for 8-bit integer, we don't convert
25800   // 8-bit integer abs to NEG and CMOV.
25801   if (VT.isInteger() && VT.getSizeInBits() == 8)
25802     return SDValue();
25803
25804   SDValue N0 = N->getOperand(0);
25805   SDValue N1 = N->getOperand(1);
25806   SDLoc DL(N);
25807
25808   // Check pattern of XOR(ADD(X,Y), Y) where Y is SRA(X, size(X)-1)
25809   // and change it to SUB and CMOV.
25810   if (VT.isInteger() && N->getOpcode() == ISD::XOR &&
25811       N0.getOpcode() == ISD::ADD &&
25812       N0.getOperand(1) == N1 &&
25813       N1.getOpcode() == ISD::SRA &&
25814       N1.getOperand(0) == N0.getOperand(0))
25815     if (ConstantSDNode *Y1C = dyn_cast<ConstantSDNode>(N1.getOperand(1)))
25816       if (Y1C->getAPIntValue() == VT.getSizeInBits()-1) {
25817         // Generate SUB & CMOV.
25818         SDValue Neg = DAG.getNode(X86ISD::SUB, DL, DAG.getVTList(VT, MVT::i32),
25819                                   DAG.getConstant(0, DL, VT), N0.getOperand(0));
25820
25821         SDValue Ops[] = { N0.getOperand(0), Neg,
25822                           DAG.getConstant(X86::COND_GE, DL, MVT::i8),
25823                           SDValue(Neg.getNode(), 1) };
25824         return DAG.getNode(X86ISD::CMOV, DL, DAG.getVTList(VT, MVT::Glue), Ops);
25825       }
25826   return SDValue();
25827 }
25828
25829 // Try to turn tests against the signbit in the form of:
25830 //   XOR(TRUNCATE(SRL(X, size(X)-1)), 1)
25831 // into:
25832 //   SETGT(X, -1)
25833 static SDValue foldXorTruncShiftIntoCmp(SDNode *N, SelectionDAG &DAG) {
25834   // This is only worth doing if the output type is i8.
25835   if (N->getValueType(0) != MVT::i8)
25836     return SDValue();
25837
25838   SDValue N0 = N->getOperand(0);
25839   SDValue N1 = N->getOperand(1);
25840
25841   // We should be performing an xor against a truncated shift.
25842   if (N0.getOpcode() != ISD::TRUNCATE || !N0.hasOneUse())
25843     return SDValue();
25844
25845   // Make sure we are performing an xor against one.
25846   if (!isOneConstant(N1))
25847     return SDValue();
25848
25849   // SetCC on x86 zero extends so only act on this if it's a logical shift.
25850   SDValue Shift = N0.getOperand(0);
25851   if (Shift.getOpcode() != ISD::SRL || !Shift.hasOneUse())
25852     return SDValue();
25853
25854   // Make sure we are truncating from one of i16, i32 or i64.
25855   EVT ShiftTy = Shift.getValueType();
25856   if (ShiftTy != MVT::i16 && ShiftTy != MVT::i32 && ShiftTy != MVT::i64)
25857     return SDValue();
25858
25859   // Make sure the shift amount extracts the sign bit.
25860   if (!isa<ConstantSDNode>(Shift.getOperand(1)) ||
25861       Shift.getConstantOperandVal(1) != ShiftTy.getSizeInBits() - 1)
25862     return SDValue();
25863
25864   // Create a greater-than comparison against -1.
25865   // N.B. Using SETGE against 0 works but we want a canonical looking
25866   // comparison, using SETGT matches up with what TranslateX86CC.
25867   SDLoc DL(N);
25868   SDValue ShiftOp = Shift.getOperand(0);
25869   EVT ShiftOpTy = ShiftOp.getValueType();
25870   SDValue Cond = DAG.getSetCC(DL, MVT::i8, ShiftOp,
25871                               DAG.getConstant(-1, DL, ShiftOpTy), ISD::SETGT);
25872   return Cond;
25873 }
25874
25875 static SDValue PerformXorCombine(SDNode *N, SelectionDAG &DAG,
25876                                  TargetLowering::DAGCombinerInfo &DCI,
25877                                  const X86Subtarget *Subtarget) {
25878   if (DCI.isBeforeLegalizeOps())
25879     return SDValue();
25880
25881   if (SDValue RV = foldXorTruncShiftIntoCmp(N, DAG))
25882     return RV;
25883
25884   if (Subtarget->hasCMov())
25885     if (SDValue RV = performIntegerAbsCombine(N, DAG))
25886       return RV;
25887
25888   if (SDValue FPLogic = convertIntLogicToFPLogic(N, DAG, Subtarget))
25889     return FPLogic;
25890
25891   return SDValue();
25892 }
25893
25894 /// This function detects the AVG pattern between vectors of unsigned i8/i16,
25895 /// which is c = (a + b + 1) / 2, and replace this operation with the efficient
25896 /// X86ISD::AVG instruction.
25897 static SDValue detectAVGPattern(SDValue In, EVT VT, SelectionDAG &DAG,
25898                                 const X86Subtarget *Subtarget, SDLoc DL) {
25899   if (!VT.isVector() || !VT.isSimple())
25900     return SDValue();
25901   EVT InVT = In.getValueType();
25902   unsigned NumElems = VT.getVectorNumElements();
25903
25904   EVT ScalarVT = VT.getVectorElementType();
25905   if (!((ScalarVT == MVT::i8 || ScalarVT == MVT::i16) &&
25906         isPowerOf2_32(NumElems)))
25907     return SDValue();
25908
25909   // InScalarVT is the intermediate type in AVG pattern and it should be greater
25910   // than the original input type (i8/i16).
25911   EVT InScalarVT = InVT.getVectorElementType();
25912   if (InScalarVT.getSizeInBits() <= ScalarVT.getSizeInBits())
25913     return SDValue();
25914
25915   if (Subtarget->hasAVX512()) {
25916     if (VT.getSizeInBits() > 512)
25917       return SDValue();
25918   } else if (Subtarget->hasAVX2()) {
25919     if (VT.getSizeInBits() > 256)
25920       return SDValue();
25921   } else {
25922     if (VT.getSizeInBits() > 128)
25923       return SDValue();
25924   }
25925
25926   // Detect the following pattern:
25927   //
25928   //   %1 = zext <N x i8> %a to <N x i32>
25929   //   %2 = zext <N x i8> %b to <N x i32>
25930   //   %3 = add nuw nsw <N x i32> %1, <i32 1 x N>
25931   //   %4 = add nuw nsw <N x i32> %3, %2
25932   //   %5 = lshr <N x i32> %N, <i32 1 x N>
25933   //   %6 = trunc <N x i32> %5 to <N x i8>
25934   //
25935   // In AVX512, the last instruction can also be a trunc store.
25936
25937   if (In.getOpcode() != ISD::SRL)
25938     return SDValue();
25939
25940   // A lambda checking the given SDValue is a constant vector and each element
25941   // is in the range [Min, Max].
25942   auto IsConstVectorInRange = [](SDValue V, unsigned Min, unsigned Max) {
25943     BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(V);
25944     if (!BV || !BV->isConstant())
25945       return false;
25946     for (unsigned i = 0, e = V.getNumOperands(); i < e; i++) {
25947       ConstantSDNode *C = dyn_cast<ConstantSDNode>(V.getOperand(i));
25948       if (!C)
25949         return false;
25950       uint64_t Val = C->getZExtValue();
25951       if (Val < Min || Val > Max)
25952         return false;
25953     }
25954     return true;
25955   };
25956
25957   // Check if each element of the vector is left-shifted by one.
25958   auto LHS = In.getOperand(0);
25959   auto RHS = In.getOperand(1);
25960   if (!IsConstVectorInRange(RHS, 1, 1))
25961     return SDValue();
25962   if (LHS.getOpcode() != ISD::ADD)
25963     return SDValue();
25964
25965   // Detect a pattern of a + b + 1 where the order doesn't matter.
25966   SDValue Operands[3];
25967   Operands[0] = LHS.getOperand(0);
25968   Operands[1] = LHS.getOperand(1);
25969
25970   // Take care of the case when one of the operands is a constant vector whose
25971   // element is in the range [1, 256].
25972   if (IsConstVectorInRange(Operands[1], 1, ScalarVT == MVT::i8 ? 256 : 65536) &&
25973       Operands[0].getOpcode() == ISD::ZERO_EXTEND &&
25974       Operands[0].getOperand(0).getValueType() == VT) {
25975     // The pattern is detected. Subtract one from the constant vector, then
25976     // demote it and emit X86ISD::AVG instruction.
25977     SDValue One = DAG.getConstant(1, DL, InScalarVT);
25978     SDValue Ones = DAG.getNode(ISD::BUILD_VECTOR, DL, InVT,
25979                                SmallVector<SDValue, 8>(NumElems, One));
25980     Operands[1] = DAG.getNode(ISD::SUB, DL, InVT, Operands[1], Ones);
25981     Operands[1] = DAG.getNode(ISD::TRUNCATE, DL, VT, Operands[1]);
25982     return DAG.getNode(X86ISD::AVG, DL, VT, Operands[0].getOperand(0),
25983                        Operands[1]);
25984   }
25985
25986   if (Operands[0].getOpcode() == ISD::ADD)
25987     std::swap(Operands[0], Operands[1]);
25988   else if (Operands[1].getOpcode() != ISD::ADD)
25989     return SDValue();
25990   Operands[2] = Operands[1].getOperand(0);
25991   Operands[1] = Operands[1].getOperand(1);
25992
25993   // Now we have three operands of two additions. Check that one of them is a
25994   // constant vector with ones, and the other two are promoted from i8/i16.
25995   for (int i = 0; i < 3; ++i) {
25996     if (!IsConstVectorInRange(Operands[i], 1, 1))
25997       continue;
25998     std::swap(Operands[i], Operands[2]);
25999
26000     // Check if Operands[0] and Operands[1] are results of type promotion.
26001     for (int j = 0; j < 2; ++j)
26002       if (Operands[j].getOpcode() != ISD::ZERO_EXTEND ||
26003           Operands[j].getOperand(0).getValueType() != VT)
26004         return SDValue();
26005
26006     // The pattern is detected, emit X86ISD::AVG instruction.
26007     return DAG.getNode(X86ISD::AVG, DL, VT, Operands[0].getOperand(0),
26008                        Operands[1].getOperand(0));
26009   }
26010
26011   return SDValue();
26012 }
26013
26014 /// PerformLOADCombine - Do target-specific dag combines on LOAD nodes.
26015 static SDValue PerformLOADCombine(SDNode *N, SelectionDAG &DAG,
26016                                   TargetLowering::DAGCombinerInfo &DCI,
26017                                   const X86Subtarget *Subtarget) {
26018   LoadSDNode *Ld = cast<LoadSDNode>(N);
26019   EVT RegVT = Ld->getValueType(0);
26020   EVT MemVT = Ld->getMemoryVT();
26021   SDLoc dl(Ld);
26022   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
26023
26024   // For chips with slow 32-byte unaligned loads, break the 32-byte operation
26025   // into two 16-byte operations.
26026   ISD::LoadExtType Ext = Ld->getExtensionType();
26027   bool Fast;
26028   unsigned AddressSpace = Ld->getAddressSpace();
26029   unsigned Alignment = Ld->getAlignment();
26030   if (RegVT.is256BitVector() && !DCI.isBeforeLegalizeOps() &&
26031       Ext == ISD::NON_EXTLOAD &&
26032       TLI.allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), RegVT,
26033                              AddressSpace, Alignment, &Fast) && !Fast) {
26034     unsigned NumElems = RegVT.getVectorNumElements();
26035     if (NumElems < 2)
26036       return SDValue();
26037
26038     SDValue Ptr = Ld->getBasePtr();
26039     SDValue Increment =
26040         DAG.getConstant(16, dl, TLI.getPointerTy(DAG.getDataLayout()));
26041
26042     EVT HalfVT = EVT::getVectorVT(*DAG.getContext(), MemVT.getScalarType(),
26043                                   NumElems/2);
26044     SDValue Load1 = DAG.getLoad(HalfVT, dl, Ld->getChain(), Ptr,
26045                                 Ld->getPointerInfo(), Ld->isVolatile(),
26046                                 Ld->isNonTemporal(), Ld->isInvariant(),
26047                                 Alignment);
26048     Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
26049     SDValue Load2 = DAG.getLoad(HalfVT, dl, Ld->getChain(), Ptr,
26050                                 Ld->getPointerInfo(), Ld->isVolatile(),
26051                                 Ld->isNonTemporal(), Ld->isInvariant(),
26052                                 std::min(16U, Alignment));
26053     SDValue TF = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
26054                              Load1.getValue(1),
26055                              Load2.getValue(1));
26056
26057     SDValue NewVec = DAG.getUNDEF(RegVT);
26058     NewVec = Insert128BitVector(NewVec, Load1, 0, DAG, dl);
26059     NewVec = Insert128BitVector(NewVec, Load2, NumElems/2, DAG, dl);
26060     return DCI.CombineTo(N, NewVec, TF, true);
26061   }
26062
26063   return SDValue();
26064 }
26065
26066 /// PerformMLOADCombine - Resolve extending loads
26067 static SDValue PerformMLOADCombine(SDNode *N, SelectionDAG &DAG,
26068                                    TargetLowering::DAGCombinerInfo &DCI,
26069                                    const X86Subtarget *Subtarget) {
26070   MaskedLoadSDNode *Mld = cast<MaskedLoadSDNode>(N);
26071   if (Mld->getExtensionType() != ISD::SEXTLOAD)
26072     return SDValue();
26073
26074   EVT VT = Mld->getValueType(0);
26075   unsigned NumElems = VT.getVectorNumElements();
26076   EVT LdVT = Mld->getMemoryVT();
26077   SDLoc dl(Mld);
26078
26079   assert(LdVT != VT && "Cannot extend to the same type");
26080   unsigned ToSz = VT.getVectorElementType().getSizeInBits();
26081   unsigned FromSz = LdVT.getVectorElementType().getSizeInBits();
26082   // From, To sizes and ElemCount must be pow of two
26083   assert (isPowerOf2_32(NumElems * FromSz * ToSz) &&
26084     "Unexpected size for extending masked load");
26085
26086   unsigned SizeRatio  = ToSz / FromSz;
26087   assert(SizeRatio * NumElems * FromSz == VT.getSizeInBits());
26088
26089   // Create a type on which we perform the shuffle
26090   EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
26091           LdVT.getScalarType(), NumElems*SizeRatio);
26092   assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
26093
26094   // Convert Src0 value
26095   SDValue WideSrc0 = DAG.getBitcast(WideVecVT, Mld->getSrc0());
26096   if (Mld->getSrc0().getOpcode() != ISD::UNDEF) {
26097     SmallVector<int, 16> ShuffleVec(NumElems * SizeRatio, -1);
26098     for (unsigned i = 0; i != NumElems; ++i)
26099       ShuffleVec[i] = i * SizeRatio;
26100
26101     // Can't shuffle using an illegal type.
26102     assert(DAG.getTargetLoweringInfo().isTypeLegal(WideVecVT) &&
26103            "WideVecVT should be legal");
26104     WideSrc0 = DAG.getVectorShuffle(WideVecVT, dl, WideSrc0,
26105                                     DAG.getUNDEF(WideVecVT), &ShuffleVec[0]);
26106   }
26107   // Prepare the new mask
26108   SDValue NewMask;
26109   SDValue Mask = Mld->getMask();
26110   if (Mask.getValueType() == VT) {
26111     // Mask and original value have the same type
26112     NewMask = DAG.getBitcast(WideVecVT, Mask);
26113     SmallVector<int, 16> ShuffleVec(NumElems * SizeRatio, -1);
26114     for (unsigned i = 0; i != NumElems; ++i)
26115       ShuffleVec[i] = i * SizeRatio;
26116     for (unsigned i = NumElems; i != NumElems * SizeRatio; ++i)
26117       ShuffleVec[i] = NumElems * SizeRatio;
26118     NewMask = DAG.getVectorShuffle(WideVecVT, dl, NewMask,
26119                                    DAG.getConstant(0, dl, WideVecVT),
26120                                    &ShuffleVec[0]);
26121   }
26122   else {
26123     assert(Mask.getValueType().getVectorElementType() == MVT::i1);
26124     unsigned WidenNumElts = NumElems*SizeRatio;
26125     unsigned MaskNumElts = VT.getVectorNumElements();
26126     EVT NewMaskVT = EVT::getVectorVT(*DAG.getContext(),  MVT::i1,
26127                                      WidenNumElts);
26128
26129     unsigned NumConcat = WidenNumElts / MaskNumElts;
26130     SmallVector<SDValue, 16> Ops(NumConcat);
26131     SDValue ZeroVal = DAG.getConstant(0, dl, Mask.getValueType());
26132     Ops[0] = Mask;
26133     for (unsigned i = 1; i != NumConcat; ++i)
26134       Ops[i] = ZeroVal;
26135
26136     NewMask = DAG.getNode(ISD::CONCAT_VECTORS, dl, NewMaskVT, Ops);
26137   }
26138
26139   SDValue WideLd = DAG.getMaskedLoad(WideVecVT, dl, Mld->getChain(),
26140                                      Mld->getBasePtr(), NewMask, WideSrc0,
26141                                      Mld->getMemoryVT(), Mld->getMemOperand(),
26142                                      ISD::NON_EXTLOAD);
26143   SDValue NewVec = DAG.getNode(X86ISD::VSEXT, dl, VT, WideLd);
26144   return DCI.CombineTo(N, NewVec, WideLd.getValue(1), true);
26145 }
26146 /// PerformMSTORECombine - Resolve truncating stores
26147 static SDValue PerformMSTORECombine(SDNode *N, SelectionDAG &DAG,
26148                                     const X86Subtarget *Subtarget) {
26149   MaskedStoreSDNode *Mst = cast<MaskedStoreSDNode>(N);
26150   if (!Mst->isTruncatingStore())
26151     return SDValue();
26152
26153   EVT VT = Mst->getValue().getValueType();
26154   unsigned NumElems = VT.getVectorNumElements();
26155   EVT StVT = Mst->getMemoryVT();
26156   SDLoc dl(Mst);
26157
26158   assert(StVT != VT && "Cannot truncate to the same type");
26159   unsigned FromSz = VT.getVectorElementType().getSizeInBits();
26160   unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
26161
26162   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
26163
26164   // The truncating store is legal in some cases. For example
26165   // vpmovqb, vpmovqw, vpmovqd, vpmovdb, vpmovdw
26166   // are designated for truncate store.
26167   // In this case we don't need any further transformations.
26168   if (TLI.isTruncStoreLegal(VT, StVT))
26169     return SDValue();
26170
26171   // From, To sizes and ElemCount must be pow of two
26172   assert (isPowerOf2_32(NumElems * FromSz * ToSz) &&
26173     "Unexpected size for truncating masked store");
26174   // We are going to use the original vector elt for storing.
26175   // Accumulated smaller vector elements must be a multiple of the store size.
26176   assert (((NumElems * FromSz) % ToSz) == 0 &&
26177           "Unexpected ratio for truncating masked store");
26178
26179   unsigned SizeRatio  = FromSz / ToSz;
26180   assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
26181
26182   // Create a type on which we perform the shuffle
26183   EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
26184           StVT.getScalarType(), NumElems*SizeRatio);
26185
26186   assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
26187
26188   SDValue WideVec = DAG.getBitcast(WideVecVT, Mst->getValue());
26189   SmallVector<int, 16> ShuffleVec(NumElems * SizeRatio, -1);
26190   for (unsigned i = 0; i != NumElems; ++i)
26191     ShuffleVec[i] = i * SizeRatio;
26192
26193   // Can't shuffle using an illegal type.
26194   assert(DAG.getTargetLoweringInfo().isTypeLegal(WideVecVT) &&
26195          "WideVecVT should be legal");
26196
26197   SDValue TruncatedVal = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
26198                                               DAG.getUNDEF(WideVecVT),
26199                                               &ShuffleVec[0]);
26200
26201   SDValue NewMask;
26202   SDValue Mask = Mst->getMask();
26203   if (Mask.getValueType() == VT) {
26204     // Mask and original value have the same type
26205     NewMask = DAG.getBitcast(WideVecVT, Mask);
26206     for (unsigned i = 0; i != NumElems; ++i)
26207       ShuffleVec[i] = i * SizeRatio;
26208     for (unsigned i = NumElems; i != NumElems*SizeRatio; ++i)
26209       ShuffleVec[i] = NumElems*SizeRatio;
26210     NewMask = DAG.getVectorShuffle(WideVecVT, dl, NewMask,
26211                                    DAG.getConstant(0, dl, WideVecVT),
26212                                    &ShuffleVec[0]);
26213   }
26214   else {
26215     assert(Mask.getValueType().getVectorElementType() == MVT::i1);
26216     unsigned WidenNumElts = NumElems*SizeRatio;
26217     unsigned MaskNumElts = VT.getVectorNumElements();
26218     EVT NewMaskVT = EVT::getVectorVT(*DAG.getContext(),  MVT::i1,
26219                                      WidenNumElts);
26220
26221     unsigned NumConcat = WidenNumElts / MaskNumElts;
26222     SmallVector<SDValue, 16> Ops(NumConcat);
26223     SDValue ZeroVal = DAG.getConstant(0, dl, Mask.getValueType());
26224     Ops[0] = Mask;
26225     for (unsigned i = 1; i != NumConcat; ++i)
26226       Ops[i] = ZeroVal;
26227
26228     NewMask = DAG.getNode(ISD::CONCAT_VECTORS, dl, NewMaskVT, Ops);
26229   }
26230
26231   return DAG.getMaskedStore(Mst->getChain(), dl, TruncatedVal,
26232                             Mst->getBasePtr(), NewMask, StVT,
26233                             Mst->getMemOperand(), false);
26234 }
26235 /// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
26236 static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
26237                                    const X86Subtarget *Subtarget) {
26238   StoreSDNode *St = cast<StoreSDNode>(N);
26239   EVT VT = St->getValue().getValueType();
26240   EVT StVT = St->getMemoryVT();
26241   SDLoc dl(St);
26242   SDValue StoredVal = St->getOperand(1);
26243   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
26244
26245   // If we are saving a concatenation of two XMM registers and 32-byte stores
26246   // are slow, such as on Sandy Bridge, perform two 16-byte stores.
26247   bool Fast;
26248   unsigned AddressSpace = St->getAddressSpace();
26249   unsigned Alignment = St->getAlignment();
26250   if (VT.is256BitVector() && StVT == VT &&
26251       TLI.allowsMemoryAccess(*DAG.getContext(), DAG.getDataLayout(), VT,
26252                              AddressSpace, Alignment, &Fast) && !Fast) {
26253     unsigned NumElems = VT.getVectorNumElements();
26254     if (NumElems < 2)
26255       return SDValue();
26256
26257     SDValue Value0 = Extract128BitVector(StoredVal, 0, DAG, dl);
26258     SDValue Value1 = Extract128BitVector(StoredVal, NumElems/2, DAG, dl);
26259
26260     SDValue Stride =
26261         DAG.getConstant(16, dl, TLI.getPointerTy(DAG.getDataLayout()));
26262     SDValue Ptr0 = St->getBasePtr();
26263     SDValue Ptr1 = DAG.getNode(ISD::ADD, dl, Ptr0.getValueType(), Ptr0, Stride);
26264
26265     SDValue Ch0 = DAG.getStore(St->getChain(), dl, Value0, Ptr0,
26266                                 St->getPointerInfo(), St->isVolatile(),
26267                                 St->isNonTemporal(), Alignment);
26268     SDValue Ch1 = DAG.getStore(St->getChain(), dl, Value1, Ptr1,
26269                                 St->getPointerInfo(), St->isVolatile(),
26270                                 St->isNonTemporal(),
26271                                 std::min(16U, Alignment));
26272     return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Ch0, Ch1);
26273   }
26274
26275   // Optimize trunc store (of multiple scalars) to shuffle and store.
26276   // First, pack all of the elements in one place. Next, store to memory
26277   // in fewer chunks.
26278   if (St->isTruncatingStore() && VT.isVector()) {
26279     // Check if we can detect an AVG pattern from the truncation. If yes,
26280     // replace the trunc store by a normal store with the result of X86ISD::AVG
26281     // instruction.
26282     SDValue Avg =
26283         detectAVGPattern(St->getValue(), St->getMemoryVT(), DAG, Subtarget, dl);
26284     if (Avg.getNode())
26285       return DAG.getStore(St->getChain(), dl, Avg, St->getBasePtr(),
26286                           St->getPointerInfo(), St->isVolatile(),
26287                           St->isNonTemporal(), St->getAlignment());
26288
26289     const TargetLowering &TLI = DAG.getTargetLoweringInfo();
26290     unsigned NumElems = VT.getVectorNumElements();
26291     assert(StVT != VT && "Cannot truncate to the same type");
26292     unsigned FromSz = VT.getVectorElementType().getSizeInBits();
26293     unsigned ToSz = StVT.getVectorElementType().getSizeInBits();
26294
26295     // The truncating store is legal in some cases. For example
26296     // vpmovqb, vpmovqw, vpmovqd, vpmovdb, vpmovdw
26297     // are designated for truncate store.
26298     // In this case we don't need any further transformations.
26299     if (TLI.isTruncStoreLegal(VT, StVT))
26300       return SDValue();
26301
26302     // From, To sizes and ElemCount must be pow of two
26303     if (!isPowerOf2_32(NumElems * FromSz * ToSz)) return SDValue();
26304     // We are going to use the original vector elt for storing.
26305     // Accumulated smaller vector elements must be a multiple of the store size.
26306     if (0 != (NumElems * FromSz) % ToSz) return SDValue();
26307
26308     unsigned SizeRatio  = FromSz / ToSz;
26309
26310     assert(SizeRatio * NumElems * ToSz == VT.getSizeInBits());
26311
26312     // Create a type on which we perform the shuffle
26313     EVT WideVecVT = EVT::getVectorVT(*DAG.getContext(),
26314             StVT.getScalarType(), NumElems*SizeRatio);
26315
26316     assert(WideVecVT.getSizeInBits() == VT.getSizeInBits());
26317
26318     SDValue WideVec = DAG.getBitcast(WideVecVT, St->getValue());
26319     SmallVector<int, 8> ShuffleVec(NumElems * SizeRatio, -1);
26320     for (unsigned i = 0; i != NumElems; ++i)
26321       ShuffleVec[i] = i * SizeRatio;
26322
26323     // Can't shuffle using an illegal type.
26324     if (!TLI.isTypeLegal(WideVecVT))
26325       return SDValue();
26326
26327     SDValue Shuff = DAG.getVectorShuffle(WideVecVT, dl, WideVec,
26328                                          DAG.getUNDEF(WideVecVT),
26329                                          &ShuffleVec[0]);
26330     // At this point all of the data is stored at the bottom of the
26331     // register. We now need to save it to mem.
26332
26333     // Find the largest store unit
26334     MVT StoreType = MVT::i8;
26335     for (MVT Tp : MVT::integer_valuetypes()) {
26336       if (TLI.isTypeLegal(Tp) && Tp.getSizeInBits() <= NumElems * ToSz)
26337         StoreType = Tp;
26338     }
26339
26340     // On 32bit systems, we can't save 64bit integers. Try bitcasting to F64.
26341     if (TLI.isTypeLegal(MVT::f64) && StoreType.getSizeInBits() < 64 &&
26342         (64 <= NumElems * ToSz))
26343       StoreType = MVT::f64;
26344
26345     // Bitcast the original vector into a vector of store-size units
26346     EVT StoreVecVT = EVT::getVectorVT(*DAG.getContext(),
26347             StoreType, VT.getSizeInBits()/StoreType.getSizeInBits());
26348     assert(StoreVecVT.getSizeInBits() == VT.getSizeInBits());
26349     SDValue ShuffWide = DAG.getBitcast(StoreVecVT, Shuff);
26350     SmallVector<SDValue, 8> Chains;
26351     SDValue Increment = DAG.getConstant(StoreType.getSizeInBits() / 8, dl,
26352                                         TLI.getPointerTy(DAG.getDataLayout()));
26353     SDValue Ptr = St->getBasePtr();
26354
26355     // Perform one or more big stores into memory.
26356     for (unsigned i=0, e=(ToSz*NumElems)/StoreType.getSizeInBits(); i!=e; ++i) {
26357       SDValue SubVec = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
26358                                    StoreType, ShuffWide,
26359                                    DAG.getIntPtrConstant(i, dl));
26360       SDValue Ch = DAG.getStore(St->getChain(), dl, SubVec, Ptr,
26361                                 St->getPointerInfo(), St->isVolatile(),
26362                                 St->isNonTemporal(), St->getAlignment());
26363       Ptr = DAG.getNode(ISD::ADD, dl, Ptr.getValueType(), Ptr, Increment);
26364       Chains.push_back(Ch);
26365     }
26366
26367     return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Chains);
26368   }
26369
26370   // Turn load->store of MMX types into GPR load/stores.  This avoids clobbering
26371   // the FP state in cases where an emms may be missing.
26372   // A preferable solution to the general problem is to figure out the right
26373   // places to insert EMMS.  This qualifies as a quick hack.
26374
26375   // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
26376   if (VT.getSizeInBits() != 64)
26377     return SDValue();
26378
26379   const Function *F = DAG.getMachineFunction().getFunction();
26380   bool NoImplicitFloatOps = F->hasFnAttribute(Attribute::NoImplicitFloat);
26381   bool F64IsLegal =
26382       !Subtarget->useSoftFloat() && !NoImplicitFloatOps && Subtarget->hasSSE2();
26383   if ((VT.isVector() ||
26384        (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
26385       isa<LoadSDNode>(St->getValue()) &&
26386       !cast<LoadSDNode>(St->getValue())->isVolatile() &&
26387       St->getChain().hasOneUse() && !St->isVolatile()) {
26388     SDNode* LdVal = St->getValue().getNode();
26389     LoadSDNode *Ld = nullptr;
26390     int TokenFactorIndex = -1;
26391     SmallVector<SDValue, 8> Ops;
26392     SDNode* ChainVal = St->getChain().getNode();
26393     // Must be a store of a load.  We currently handle two cases:  the load
26394     // is a direct child, and it's under an intervening TokenFactor.  It is
26395     // possible to dig deeper under nested TokenFactors.
26396     if (ChainVal == LdVal)
26397       Ld = cast<LoadSDNode>(St->getChain());
26398     else if (St->getValue().hasOneUse() &&
26399              ChainVal->getOpcode() == ISD::TokenFactor) {
26400       for (unsigned i = 0, e = ChainVal->getNumOperands(); i != e; ++i) {
26401         if (ChainVal->getOperand(i).getNode() == LdVal) {
26402           TokenFactorIndex = i;
26403           Ld = cast<LoadSDNode>(St->getValue());
26404         } else
26405           Ops.push_back(ChainVal->getOperand(i));
26406       }
26407     }
26408
26409     if (!Ld || !ISD::isNormalLoad(Ld))
26410       return SDValue();
26411
26412     // If this is not the MMX case, i.e. we are just turning i64 load/store
26413     // into f64 load/store, avoid the transformation if there are multiple
26414     // uses of the loaded value.
26415     if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
26416       return SDValue();
26417
26418     SDLoc LdDL(Ld);
26419     SDLoc StDL(N);
26420     // If we are a 64-bit capable x86, lower to a single movq load/store pair.
26421     // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
26422     // pair instead.
26423     if (Subtarget->is64Bit() || F64IsLegal) {
26424       MVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
26425       SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(), Ld->getBasePtr(),
26426                                   Ld->getPointerInfo(), Ld->isVolatile(),
26427                                   Ld->isNonTemporal(), Ld->isInvariant(),
26428                                   Ld->getAlignment());
26429       SDValue NewChain = NewLd.getValue(1);
26430       if (TokenFactorIndex != -1) {
26431         Ops.push_back(NewChain);
26432         NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, Ops);
26433       }
26434       return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
26435                           St->getPointerInfo(),
26436                           St->isVolatile(), St->isNonTemporal(),
26437                           St->getAlignment());
26438     }
26439
26440     // Otherwise, lower to two pairs of 32-bit loads / stores.
26441     SDValue LoAddr = Ld->getBasePtr();
26442     SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
26443                                  DAG.getConstant(4, LdDL, MVT::i32));
26444
26445     SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
26446                                Ld->getPointerInfo(),
26447                                Ld->isVolatile(), Ld->isNonTemporal(),
26448                                Ld->isInvariant(), Ld->getAlignment());
26449     SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
26450                                Ld->getPointerInfo().getWithOffset(4),
26451                                Ld->isVolatile(), Ld->isNonTemporal(),
26452                                Ld->isInvariant(),
26453                                MinAlign(Ld->getAlignment(), 4));
26454
26455     SDValue NewChain = LoLd.getValue(1);
26456     if (TokenFactorIndex != -1) {
26457       Ops.push_back(LoLd);
26458       Ops.push_back(HiLd);
26459       NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, Ops);
26460     }
26461
26462     LoAddr = St->getBasePtr();
26463     HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
26464                          DAG.getConstant(4, StDL, MVT::i32));
26465
26466     SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
26467                                 St->getPointerInfo(),
26468                                 St->isVolatile(), St->isNonTemporal(),
26469                                 St->getAlignment());
26470     SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
26471                                 St->getPointerInfo().getWithOffset(4),
26472                                 St->isVolatile(),
26473                                 St->isNonTemporal(),
26474                                 MinAlign(St->getAlignment(), 4));
26475     return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
26476   }
26477
26478   // This is similar to the above case, but here we handle a scalar 64-bit
26479   // integer store that is extracted from a vector on a 32-bit target.
26480   // If we have SSE2, then we can treat it like a floating-point double
26481   // to get past legalization. The execution dependencies fixup pass will
26482   // choose the optimal machine instruction for the store if this really is
26483   // an integer or v2f32 rather than an f64.
26484   if (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit() &&
26485       St->getOperand(1).getOpcode() == ISD::EXTRACT_VECTOR_ELT) {
26486     SDValue OldExtract = St->getOperand(1);
26487     SDValue ExtOp0 = OldExtract.getOperand(0);
26488     unsigned VecSize = ExtOp0.getValueSizeInBits();
26489     EVT VecVT = EVT::getVectorVT(*DAG.getContext(), MVT::f64, VecSize / 64);
26490     SDValue BitCast = DAG.getBitcast(VecVT, ExtOp0);
26491     SDValue NewExtract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
26492                                      BitCast, OldExtract.getOperand(1));
26493     return DAG.getStore(St->getChain(), dl, NewExtract, St->getBasePtr(),
26494                         St->getPointerInfo(), St->isVolatile(),
26495                         St->isNonTemporal(), St->getAlignment());
26496   }
26497
26498   return SDValue();
26499 }
26500
26501 /// Return 'true' if this vector operation is "horizontal"
26502 /// and return the operands for the horizontal operation in LHS and RHS.  A
26503 /// horizontal operation performs the binary operation on successive elements
26504 /// of its first operand, then on successive elements of its second operand,
26505 /// returning the resulting values in a vector.  For example, if
26506 ///   A = < float a0, float a1, float a2, float a3 >
26507 /// and
26508 ///   B = < float b0, float b1, float b2, float b3 >
26509 /// then the result of doing a horizontal operation on A and B is
26510 ///   A horizontal-op B = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >.
26511 /// In short, LHS and RHS are inspected to see if LHS op RHS is of the form
26512 /// A horizontal-op B, for some already available A and B, and if so then LHS is
26513 /// set to A, RHS to B, and the routine returns 'true'.
26514 /// Note that the binary operation should have the property that if one of the
26515 /// operands is UNDEF then the result is UNDEF.
26516 static bool isHorizontalBinOp(SDValue &LHS, SDValue &RHS, bool IsCommutative) {
26517   // Look for the following pattern: if
26518   //   A = < float a0, float a1, float a2, float a3 >
26519   //   B = < float b0, float b1, float b2, float b3 >
26520   // and
26521   //   LHS = VECTOR_SHUFFLE A, B, <0, 2, 4, 6>
26522   //   RHS = VECTOR_SHUFFLE A, B, <1, 3, 5, 7>
26523   // then LHS op RHS = < a0 op a1, a2 op a3, b0 op b1, b2 op b3 >
26524   // which is A horizontal-op B.
26525
26526   // At least one of the operands should be a vector shuffle.
26527   if (LHS.getOpcode() != ISD::VECTOR_SHUFFLE &&
26528       RHS.getOpcode() != ISD::VECTOR_SHUFFLE)
26529     return false;
26530
26531   MVT VT = LHS.getSimpleValueType();
26532
26533   assert((VT.is128BitVector() || VT.is256BitVector()) &&
26534          "Unsupported vector type for horizontal add/sub");
26535
26536   // Handle 128 and 256-bit vector lengths. AVX defines horizontal add/sub to
26537   // operate independently on 128-bit lanes.
26538   unsigned NumElts = VT.getVectorNumElements();
26539   unsigned NumLanes = VT.getSizeInBits()/128;
26540   unsigned NumLaneElts = NumElts / NumLanes;
26541   assert((NumLaneElts % 2 == 0) &&
26542          "Vector type should have an even number of elements in each lane");
26543   unsigned HalfLaneElts = NumLaneElts/2;
26544
26545   // View LHS in the form
26546   //   LHS = VECTOR_SHUFFLE A, B, LMask
26547   // If LHS is not a shuffle then pretend it is the shuffle
26548   //   LHS = VECTOR_SHUFFLE LHS, undef, <0, 1, ..., N-1>
26549   // NOTE: in what follows a default initialized SDValue represents an UNDEF of
26550   // type VT.
26551   SDValue A, B;
26552   SmallVector<int, 16> LMask(NumElts);
26553   if (LHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
26554     if (LHS.getOperand(0).getOpcode() != ISD::UNDEF)
26555       A = LHS.getOperand(0);
26556     if (LHS.getOperand(1).getOpcode() != ISD::UNDEF)
26557       B = LHS.getOperand(1);
26558     ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(LHS.getNode())->getMask();
26559     std::copy(Mask.begin(), Mask.end(), LMask.begin());
26560   } else {
26561     if (LHS.getOpcode() != ISD::UNDEF)
26562       A = LHS;
26563     for (unsigned i = 0; i != NumElts; ++i)
26564       LMask[i] = i;
26565   }
26566
26567   // Likewise, view RHS in the form
26568   //   RHS = VECTOR_SHUFFLE C, D, RMask
26569   SDValue C, D;
26570   SmallVector<int, 16> RMask(NumElts);
26571   if (RHS.getOpcode() == ISD::VECTOR_SHUFFLE) {
26572     if (RHS.getOperand(0).getOpcode() != ISD::UNDEF)
26573       C = RHS.getOperand(0);
26574     if (RHS.getOperand(1).getOpcode() != ISD::UNDEF)
26575       D = RHS.getOperand(1);
26576     ArrayRef<int> Mask = cast<ShuffleVectorSDNode>(RHS.getNode())->getMask();
26577     std::copy(Mask.begin(), Mask.end(), RMask.begin());
26578   } else {
26579     if (RHS.getOpcode() != ISD::UNDEF)
26580       C = RHS;
26581     for (unsigned i = 0; i != NumElts; ++i)
26582       RMask[i] = i;
26583   }
26584
26585   // Check that the shuffles are both shuffling the same vectors.
26586   if (!(A == C && B == D) && !(A == D && B == C))
26587     return false;
26588
26589   // If everything is UNDEF then bail out: it would be better to fold to UNDEF.
26590   if (!A.getNode() && !B.getNode())
26591     return false;
26592
26593   // If A and B occur in reverse order in RHS, then "swap" them (which means
26594   // rewriting the mask).
26595   if (A != C)
26596     ShuffleVectorSDNode::commuteMask(RMask);
26597
26598   // At this point LHS and RHS are equivalent to
26599   //   LHS = VECTOR_SHUFFLE A, B, LMask
26600   //   RHS = VECTOR_SHUFFLE A, B, RMask
26601   // Check that the masks correspond to performing a horizontal operation.
26602   for (unsigned l = 0; l != NumElts; l += NumLaneElts) {
26603     for (unsigned i = 0; i != NumLaneElts; ++i) {
26604       int LIdx = LMask[i+l], RIdx = RMask[i+l];
26605
26606       // Ignore any UNDEF components.
26607       if (LIdx < 0 || RIdx < 0 ||
26608           (!A.getNode() && (LIdx < (int)NumElts || RIdx < (int)NumElts)) ||
26609           (!B.getNode() && (LIdx >= (int)NumElts || RIdx >= (int)NumElts)))
26610         continue;
26611
26612       // Check that successive elements are being operated on.  If not, this is
26613       // not a horizontal operation.
26614       unsigned Src = (i/HalfLaneElts); // each lane is split between srcs
26615       int Index = 2*(i%HalfLaneElts) + NumElts*Src + l;
26616       if (!(LIdx == Index && RIdx == Index + 1) &&
26617           !(IsCommutative && LIdx == Index + 1 && RIdx == Index))
26618         return false;
26619     }
26620   }
26621
26622   LHS = A.getNode() ? A : B; // If A is 'UNDEF', use B for it.
26623   RHS = B.getNode() ? B : A; // If B is 'UNDEF', use A for it.
26624   return true;
26625 }
26626
26627 /// Do target-specific dag combines on floating point adds.
26628 static SDValue PerformFADDCombine(SDNode *N, SelectionDAG &DAG,
26629                                   const X86Subtarget *Subtarget) {
26630   EVT VT = N->getValueType(0);
26631   SDValue LHS = N->getOperand(0);
26632   SDValue RHS = N->getOperand(1);
26633
26634   // Try to synthesize horizontal adds from adds of shuffles.
26635   if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
26636        (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
26637       isHorizontalBinOp(LHS, RHS, true))
26638     return DAG.getNode(X86ISD::FHADD, SDLoc(N), VT, LHS, RHS);
26639   return SDValue();
26640 }
26641
26642 /// Do target-specific dag combines on floating point subs.
26643 static SDValue PerformFSUBCombine(SDNode *N, SelectionDAG &DAG,
26644                                   const X86Subtarget *Subtarget) {
26645   EVT VT = N->getValueType(0);
26646   SDValue LHS = N->getOperand(0);
26647   SDValue RHS = N->getOperand(1);
26648
26649   // Try to synthesize horizontal subs from subs of shuffles.
26650   if (((Subtarget->hasSSE3() && (VT == MVT::v4f32 || VT == MVT::v2f64)) ||
26651        (Subtarget->hasFp256() && (VT == MVT::v8f32 || VT == MVT::v4f64))) &&
26652       isHorizontalBinOp(LHS, RHS, false))
26653     return DAG.getNode(X86ISD::FHSUB, SDLoc(N), VT, LHS, RHS);
26654   return SDValue();
26655 }
26656
26657 /// Truncate a group of v4i32 into v16i8/v8i16 using X86ISD::PACKUS.
26658 static SDValue
26659 combineVectorTruncationWithPACKUS(SDNode *N, SelectionDAG &DAG,
26660                                   SmallVector<SDValue, 8> &Regs) {
26661   assert(Regs.size() > 0 && (Regs[0].getValueType() == MVT::v4i32 ||
26662                              Regs[0].getValueType() == MVT::v2i64));
26663   EVT OutVT = N->getValueType(0);
26664   EVT OutSVT = OutVT.getVectorElementType();
26665   EVT InVT = Regs[0].getValueType();
26666   EVT InSVT = InVT.getVectorElementType();
26667   SDLoc DL(N);
26668
26669   // First, use mask to unset all bits that won't appear in the result.
26670   assert((OutSVT == MVT::i8 || OutSVT == MVT::i16) &&
26671          "OutSVT can only be either i8 or i16.");
26672   SDValue MaskVal =
26673       DAG.getConstant(OutSVT == MVT::i8 ? 0xFF : 0xFFFF, DL, InSVT);
26674   SDValue MaskVec = DAG.getNode(
26675       ISD::BUILD_VECTOR, DL, InVT,
26676       SmallVector<SDValue, 8>(InVT.getVectorNumElements(), MaskVal));
26677   for (auto &Reg : Regs)
26678     Reg = DAG.getNode(ISD::AND, DL, InVT, MaskVec, Reg);
26679
26680   MVT UnpackedVT, PackedVT;
26681   if (OutSVT == MVT::i8) {
26682     UnpackedVT = MVT::v8i16;
26683     PackedVT = MVT::v16i8;
26684   } else {
26685     UnpackedVT = MVT::v4i32;
26686     PackedVT = MVT::v8i16;
26687   }
26688
26689   // In each iteration, truncate the type by a half size.
26690   auto RegNum = Regs.size();
26691   for (unsigned j = 1, e = InSVT.getSizeInBits() / OutSVT.getSizeInBits();
26692        j < e; j *= 2, RegNum /= 2) {
26693     for (unsigned i = 0; i < RegNum; i++)
26694       Regs[i] = DAG.getNode(ISD::BITCAST, DL, UnpackedVT, Regs[i]);
26695     for (unsigned i = 0; i < RegNum / 2; i++)
26696       Regs[i] = DAG.getNode(X86ISD::PACKUS, DL, PackedVT, Regs[i * 2],
26697                             Regs[i * 2 + 1]);
26698   }
26699
26700   // If the type of the result is v8i8, we need do one more X86ISD::PACKUS, and
26701   // then extract a subvector as the result since v8i8 is not a legal type.
26702   if (OutVT == MVT::v8i8) {
26703     Regs[0] = DAG.getNode(X86ISD::PACKUS, DL, PackedVT, Regs[0], Regs[0]);
26704     Regs[0] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, OutVT, Regs[0],
26705                           DAG.getIntPtrConstant(0, DL));
26706     return Regs[0];
26707   } else if (RegNum > 1) {
26708     Regs.resize(RegNum);
26709     return DAG.getNode(ISD::CONCAT_VECTORS, DL, OutVT, Regs);
26710   } else
26711     return Regs[0];
26712 }
26713
26714 /// Truncate a group of v4i32 into v8i16 using X86ISD::PACKSS.
26715 static SDValue
26716 combineVectorTruncationWithPACKSS(SDNode *N, SelectionDAG &DAG,
26717                                   SmallVector<SDValue, 8> &Regs) {
26718   assert(Regs.size() > 0 && Regs[0].getValueType() == MVT::v4i32);
26719   EVT OutVT = N->getValueType(0);
26720   SDLoc DL(N);
26721
26722   // Shift left by 16 bits, then arithmetic-shift right by 16 bits.
26723   SDValue ShAmt = DAG.getConstant(16, DL, MVT::i32);
26724   for (auto &Reg : Regs) {
26725     Reg = getTargetVShiftNode(X86ISD::VSHLI, DL, MVT::v4i32, Reg, ShAmt, DAG);
26726     Reg = getTargetVShiftNode(X86ISD::VSRAI, DL, MVT::v4i32, Reg, ShAmt, DAG);
26727   }
26728
26729   for (unsigned i = 0, e = Regs.size() / 2; i < e; i++)
26730     Regs[i] = DAG.getNode(X86ISD::PACKSS, DL, MVT::v8i16, Regs[i * 2],
26731                           Regs[i * 2 + 1]);
26732
26733   if (Regs.size() > 2) {
26734     Regs.resize(Regs.size() / 2);
26735     return DAG.getNode(ISD::CONCAT_VECTORS, DL, OutVT, Regs);
26736   } else
26737     return Regs[0];
26738 }
26739
26740 /// This function transforms truncation from vXi32/vXi64 to vXi8/vXi16 into
26741 /// X86ISD::PACKUS/X86ISD::PACKSS operations. We do it here because after type
26742 /// legalization the truncation will be translated into a BUILD_VECTOR with each
26743 /// element that is extracted from a vector and then truncated, and it is
26744 /// diffcult to do this optimization based on them.
26745 static SDValue combineVectorTruncation(SDNode *N, SelectionDAG &DAG,
26746                                        const X86Subtarget *Subtarget) {
26747   EVT OutVT = N->getValueType(0);
26748   if (!OutVT.isVector())
26749     return SDValue();
26750
26751   SDValue In = N->getOperand(0);
26752   if (!In.getValueType().isSimple())
26753     return SDValue();
26754
26755   EVT InVT = In.getValueType();
26756   unsigned NumElems = OutVT.getVectorNumElements();
26757
26758   // TODO: On AVX2, the behavior of X86ISD::PACKUS is different from that on
26759   // SSE2, and we need to take care of it specially.
26760   // AVX512 provides vpmovdb.
26761   if (!Subtarget->hasSSE2() || Subtarget->hasAVX2())
26762     return SDValue();
26763
26764   EVT OutSVT = OutVT.getVectorElementType();
26765   EVT InSVT = InVT.getVectorElementType();
26766   if (!((InSVT == MVT::i32 || InSVT == MVT::i64) &&
26767         (OutSVT == MVT::i8 || OutSVT == MVT::i16) && isPowerOf2_32(NumElems) &&
26768         NumElems >= 8))
26769     return SDValue();
26770
26771   // SSSE3's pshufb results in less instructions in the cases below.
26772   if (Subtarget->hasSSSE3() && NumElems == 8 &&
26773       ((OutSVT == MVT::i8 && InSVT != MVT::i64) ||
26774        (InSVT == MVT::i32 && OutSVT == MVT::i16)))
26775     return SDValue();
26776
26777   SDLoc DL(N);
26778
26779   // Split a long vector into vectors of legal type.
26780   unsigned RegNum = InVT.getSizeInBits() / 128;
26781   SmallVector<SDValue, 8> SubVec(RegNum);
26782   if (InSVT == MVT::i32) {
26783     for (unsigned i = 0; i < RegNum; i++)
26784       SubVec[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v4i32, In,
26785                               DAG.getIntPtrConstant(i * 4, DL));
26786   } else {
26787     for (unsigned i = 0; i < RegNum; i++)
26788       SubVec[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, MVT::v2i64, In,
26789                               DAG.getIntPtrConstant(i * 2, DL));
26790   }
26791
26792   // SSE2 provides PACKUS for only 2 x v8i16 -> v16i8 and SSE4.1 provides PAKCUS
26793   // for 2 x v4i32 -> v8i16. For SSSE3 and below, we need to use PACKSS to
26794   // truncate 2 x v4i32 to v8i16.
26795   if (Subtarget->hasSSE41() || OutSVT == MVT::i8)
26796     return combineVectorTruncationWithPACKUS(N, DAG, SubVec);
26797   else if (InSVT == MVT::i32)
26798     return combineVectorTruncationWithPACKSS(N, DAG, SubVec);
26799   else
26800     return SDValue();
26801 }
26802
26803 static SDValue PerformTRUNCATECombine(SDNode *N, SelectionDAG &DAG,
26804                                       const X86Subtarget *Subtarget) {
26805   // Try to detect AVG pattern first.
26806   SDValue Avg = detectAVGPattern(N->getOperand(0), N->getValueType(0), DAG,
26807                                  Subtarget, SDLoc(N));
26808   if (Avg.getNode())
26809     return Avg;
26810
26811   return combineVectorTruncation(N, DAG, Subtarget);
26812 }
26813
26814 /// Do target-specific dag combines on floating point negations.
26815 static SDValue PerformFNEGCombine(SDNode *N, SelectionDAG &DAG,
26816                                   const X86Subtarget *Subtarget) {
26817   EVT VT = N->getValueType(0);
26818   EVT SVT = VT.getScalarType();
26819   SDValue Arg = N->getOperand(0);
26820   SDLoc DL(N);
26821
26822   // Let legalize expand this if it isn't a legal type yet.
26823   if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
26824     return SDValue();
26825
26826   // If we're negating a FMUL node on a target with FMA, then we can avoid the
26827   // use of a constant by performing (-0 - A*B) instead.
26828   // FIXME: Check rounding control flags as well once it becomes available.
26829   if (Arg.getOpcode() == ISD::FMUL && (SVT == MVT::f32 || SVT == MVT::f64) &&
26830       Arg->getFlags()->hasNoSignedZeros() && Subtarget->hasAnyFMA()) {
26831     SDValue Zero = DAG.getConstantFP(0.0, DL, VT);
26832     return DAG.getNode(X86ISD::FNMSUB, DL, VT, Arg.getOperand(0),
26833                        Arg.getOperand(1), Zero);
26834   }
26835
26836   // If we're negating a FMA node, then we can adjust the
26837   // instruction to include the extra negation.
26838   if (Arg.hasOneUse()) {
26839     switch (Arg.getOpcode()) {
26840     case X86ISD::FMADD:
26841       return DAG.getNode(X86ISD::FNMSUB, DL, VT, Arg.getOperand(0),
26842                          Arg.getOperand(1), Arg.getOperand(2));
26843     case X86ISD::FMSUB:
26844       return DAG.getNode(X86ISD::FNMADD, DL, VT, Arg.getOperand(0),
26845                          Arg.getOperand(1), Arg.getOperand(2));
26846     case X86ISD::FNMADD:
26847       return DAG.getNode(X86ISD::FMSUB, DL, VT, Arg.getOperand(0),
26848                          Arg.getOperand(1), Arg.getOperand(2));
26849     case X86ISD::FNMSUB:
26850       return DAG.getNode(X86ISD::FMADD, DL, VT, Arg.getOperand(0),
26851                          Arg.getOperand(1), Arg.getOperand(2));
26852     }
26853   }
26854   return SDValue();
26855 }
26856
26857 static SDValue lowerX86FPLogicOp(SDNode *N, SelectionDAG &DAG,
26858                               const X86Subtarget *Subtarget) {
26859   EVT VT = N->getValueType(0);
26860   if (VT.is512BitVector() && !Subtarget->hasDQI()) {
26861     // VXORPS, VORPS, VANDPS, VANDNPS are supported only under DQ extention.
26862     // These logic operations may be executed in the integer domain.
26863     SDLoc dl(N);
26864     MVT IntScalar = MVT::getIntegerVT(VT.getScalarSizeInBits());
26865     MVT IntVT = MVT::getVectorVT(IntScalar, VT.getVectorNumElements());
26866
26867     SDValue Op0 = DAG.getNode(ISD::BITCAST, dl, IntVT, N->getOperand(0));
26868     SDValue Op1 = DAG.getNode(ISD::BITCAST, dl, IntVT, N->getOperand(1));
26869     unsigned IntOpcode = 0;
26870     switch (N->getOpcode()) {
26871       default: llvm_unreachable("Unexpected FP logic op");
26872       case X86ISD::FOR: IntOpcode = ISD::OR; break;
26873       case X86ISD::FXOR: IntOpcode = ISD::XOR; break;
26874       case X86ISD::FAND: IntOpcode = ISD::AND; break;
26875       case X86ISD::FANDN: IntOpcode = X86ISD::ANDNP; break;
26876     }
26877     SDValue IntOp = DAG.getNode(IntOpcode, dl, IntVT, Op0, Op1);
26878     return  DAG.getNode(ISD::BITCAST, dl, VT, IntOp);
26879   }
26880   return SDValue();
26881 }
26882 /// Do target-specific dag combines on X86ISD::FOR and X86ISD::FXOR nodes.
26883 static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG,
26884                                  const X86Subtarget *Subtarget) {
26885   assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
26886
26887   // F[X]OR(0.0, x) -> x
26888   if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
26889     if (C->getValueAPF().isPosZero())
26890       return N->getOperand(1);
26891
26892   // F[X]OR(x, 0.0) -> x
26893   if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
26894     if (C->getValueAPF().isPosZero())
26895       return N->getOperand(0);
26896
26897   return lowerX86FPLogicOp(N, DAG, Subtarget);
26898 }
26899
26900 /// Do target-specific dag combines on X86ISD::FMIN and X86ISD::FMAX nodes.
26901 static SDValue PerformFMinFMaxCombine(SDNode *N, SelectionDAG &DAG) {
26902   assert(N->getOpcode() == X86ISD::FMIN || N->getOpcode() == X86ISD::FMAX);
26903
26904   // Only perform optimizations if UnsafeMath is used.
26905   if (!DAG.getTarget().Options.UnsafeFPMath)
26906     return SDValue();
26907
26908   // If we run in unsafe-math mode, then convert the FMAX and FMIN nodes
26909   // into FMINC and FMAXC, which are Commutative operations.
26910   unsigned NewOp = 0;
26911   switch (N->getOpcode()) {
26912     default: llvm_unreachable("unknown opcode");
26913     case X86ISD::FMIN:  NewOp = X86ISD::FMINC; break;
26914     case X86ISD::FMAX:  NewOp = X86ISD::FMAXC; break;
26915   }
26916
26917   return DAG.getNode(NewOp, SDLoc(N), N->getValueType(0),
26918                      N->getOperand(0), N->getOperand(1));
26919 }
26920
26921 static SDValue performFMinNumFMaxNumCombine(SDNode *N, SelectionDAG &DAG,
26922                                             const X86Subtarget *Subtarget) {
26923   if (Subtarget->useSoftFloat())
26924     return SDValue();
26925
26926   // TODO: Check for global or instruction-level "nnan". In that case, we
26927   //       should be able to lower to FMAX/FMIN alone.
26928   // TODO: If an operand is already known to be a NaN or not a NaN, this
26929   //       should be an optional swap and FMAX/FMIN.
26930
26931   EVT VT = N->getValueType(0);
26932   if (!((Subtarget->hasSSE1() && (VT == MVT::f32 || VT == MVT::v4f32)) ||
26933         (Subtarget->hasSSE2() && (VT == MVT::f64 || VT == MVT::v2f64)) ||
26934         (Subtarget->hasAVX() && (VT == MVT::v8f32 || VT == MVT::v4f64))))
26935     return SDValue();
26936
26937   // This takes at least 3 instructions, so favor a library call when operating
26938   // on a scalar and minimizing code size.
26939   if (!VT.isVector() && DAG.getMachineFunction().getFunction()->optForMinSize())
26940     return SDValue();
26941
26942   SDValue Op0 = N->getOperand(0);
26943   SDValue Op1 = N->getOperand(1);
26944   SDLoc DL(N);
26945   EVT SetCCType = DAG.getTargetLoweringInfo().getSetCCResultType(
26946       DAG.getDataLayout(), *DAG.getContext(), VT);
26947
26948   // There are 4 possibilities involving NaN inputs, and these are the required
26949   // outputs:
26950   //                   Op1
26951   //               Num     NaN
26952   //            ----------------
26953   //       Num  |  Max  |  Op0 |
26954   // Op0        ----------------
26955   //       NaN  |  Op1  |  NaN |
26956   //            ----------------
26957   //
26958   // The SSE FP max/min instructions were not designed for this case, but rather
26959   // to implement:
26960   //   Min = Op1 < Op0 ? Op1 : Op0
26961   //   Max = Op1 > Op0 ? Op1 : Op0
26962   //
26963   // So they always return Op0 if either input is a NaN. However, we can still
26964   // use those instructions for fmaxnum by selecting away a NaN input.
26965
26966   // If either operand is NaN, the 2nd source operand (Op0) is passed through.
26967   auto MinMaxOp = N->getOpcode() == ISD::FMAXNUM ? X86ISD::FMAX : X86ISD::FMIN;
26968   SDValue MinOrMax = DAG.getNode(MinMaxOp, DL, VT, Op1, Op0);
26969   SDValue IsOp0Nan = DAG.getSetCC(DL, SetCCType , Op0, Op0, ISD::SETUO);
26970
26971   // If Op0 is a NaN, select Op1. Otherwise, select the max. If both operands
26972   // are NaN, the NaN value of Op1 is the result.
26973   auto SelectOpcode = VT.isVector() ? ISD::VSELECT : ISD::SELECT;
26974   return DAG.getNode(SelectOpcode, DL, VT, IsOp0Nan, Op1, MinOrMax);
26975 }
26976
26977 /// Do target-specific dag combines on X86ISD::FAND nodes.
26978 static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG,
26979                                   const X86Subtarget *Subtarget) {
26980   // FAND(0.0, x) -> 0.0
26981   if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
26982     if (C->getValueAPF().isPosZero())
26983       return N->getOperand(0);
26984
26985   // FAND(x, 0.0) -> 0.0
26986   if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
26987     if (C->getValueAPF().isPosZero())
26988       return N->getOperand(1);
26989
26990   return lowerX86FPLogicOp(N, DAG, Subtarget);
26991 }
26992
26993 /// Do target-specific dag combines on X86ISD::FANDN nodes
26994 static SDValue PerformFANDNCombine(SDNode *N, SelectionDAG &DAG,
26995                                    const X86Subtarget *Subtarget) {
26996   // FANDN(0.0, x) -> x
26997   if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
26998     if (C->getValueAPF().isPosZero())
26999       return N->getOperand(1);
27000
27001   // FANDN(x, 0.0) -> 0.0
27002   if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
27003     if (C->getValueAPF().isPosZero())
27004       return N->getOperand(1);
27005
27006   return lowerX86FPLogicOp(N, DAG, Subtarget);
27007 }
27008
27009 static SDValue PerformBTCombine(SDNode *N,
27010                                 SelectionDAG &DAG,
27011                                 TargetLowering::DAGCombinerInfo &DCI) {
27012   // BT ignores high bits in the bit index operand.
27013   SDValue Op1 = N->getOperand(1);
27014   if (Op1.hasOneUse()) {
27015     unsigned BitWidth = Op1.getValueSizeInBits();
27016     APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
27017     APInt KnownZero, KnownOne;
27018     TargetLowering::TargetLoweringOpt TLO(DAG, !DCI.isBeforeLegalize(),
27019                                           !DCI.isBeforeLegalizeOps());
27020     const TargetLowering &TLI = DAG.getTargetLoweringInfo();
27021     if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
27022         TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
27023       DCI.CommitTargetLoweringOpt(TLO);
27024   }
27025   return SDValue();
27026 }
27027
27028 static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
27029   SDValue Op = N->getOperand(0);
27030   if (Op.getOpcode() == ISD::BITCAST)
27031     Op = Op.getOperand(0);
27032   EVT VT = N->getValueType(0), OpVT = Op.getValueType();
27033   if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
27034       VT.getVectorElementType().getSizeInBits() ==
27035       OpVT.getVectorElementType().getSizeInBits()) {
27036     return DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
27037   }
27038   return SDValue();
27039 }
27040
27041 static SDValue PerformSIGN_EXTEND_INREGCombine(SDNode *N, SelectionDAG &DAG,
27042                                                const X86Subtarget *Subtarget) {
27043   EVT VT = N->getValueType(0);
27044   if (!VT.isVector())
27045     return SDValue();
27046
27047   SDValue N0 = N->getOperand(0);
27048   SDValue N1 = N->getOperand(1);
27049   EVT ExtraVT = cast<VTSDNode>(N1)->getVT();
27050   SDLoc dl(N);
27051
27052   // The SIGN_EXTEND_INREG to v4i64 is expensive operation on the
27053   // both SSE and AVX2 since there is no sign-extended shift right
27054   // operation on a vector with 64-bit elements.
27055   //(sext_in_reg (v4i64 anyext (v4i32 x )), ExtraVT) ->
27056   // (v4i64 sext (v4i32 sext_in_reg (v4i32 x , ExtraVT)))
27057   if (VT == MVT::v4i64 && (N0.getOpcode() == ISD::ANY_EXTEND ||
27058       N0.getOpcode() == ISD::SIGN_EXTEND)) {
27059     SDValue N00 = N0.getOperand(0);
27060
27061     // EXTLOAD has a better solution on AVX2,
27062     // it may be replaced with X86ISD::VSEXT node.
27063     if (N00.getOpcode() == ISD::LOAD && Subtarget->hasInt256())
27064       if (!ISD::isNormalLoad(N00.getNode()))
27065         return SDValue();
27066
27067     if (N00.getValueType() == MVT::v4i32 && ExtraVT.getSizeInBits() < 128) {
27068         SDValue Tmp = DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, MVT::v4i32,
27069                                   N00, N1);
27070       return DAG.getNode(ISD::SIGN_EXTEND, dl, MVT::v4i64, Tmp);
27071     }
27072   }
27073   return SDValue();
27074 }
27075
27076 /// sext(add_nsw(x, C)) --> add(sext(x), C_sext)
27077 /// Promoting a sign extension ahead of an 'add nsw' exposes opportunities
27078 /// to combine math ops, use an LEA, or use a complex addressing mode. This can
27079 /// eliminate extend, add, and shift instructions.
27080 static SDValue promoteSextBeforeAddNSW(SDNode *Sext, SelectionDAG &DAG,
27081                                        const X86Subtarget *Subtarget) {
27082   // TODO: This should be valid for other integer types.
27083   EVT VT = Sext->getValueType(0);
27084   if (VT != MVT::i64)
27085     return SDValue();
27086
27087   // We need an 'add nsw' feeding into the 'sext'.
27088   SDValue Add = Sext->getOperand(0);
27089   if (Add.getOpcode() != ISD::ADD || !Add->getFlags()->hasNoSignedWrap())
27090     return SDValue();
27091
27092   // Having a constant operand to the 'add' ensures that we are not increasing
27093   // the instruction count because the constant is extended for free below.
27094   // A constant operand can also become the displacement field of an LEA.
27095   auto *AddOp1 = dyn_cast<ConstantSDNode>(Add.getOperand(1));
27096   if (!AddOp1)
27097     return SDValue();
27098
27099   // Don't make the 'add' bigger if there's no hope of combining it with some
27100   // other 'add' or 'shl' instruction.
27101   // TODO: It may be profitable to generate simpler LEA instructions in place
27102   // of single 'add' instructions, but the cost model for selecting an LEA
27103   // currently has a high threshold.
27104   bool HasLEAPotential = false;
27105   for (auto *User : Sext->uses()) {
27106     if (User->getOpcode() == ISD::ADD || User->getOpcode() == ISD::SHL) {
27107       HasLEAPotential = true;
27108       break;
27109     }
27110   }
27111   if (!HasLEAPotential)
27112     return SDValue();
27113
27114   // Everything looks good, so pull the 'sext' ahead of the 'add'.
27115   int64_t AddConstant = AddOp1->getSExtValue();
27116   SDValue AddOp0 = Add.getOperand(0);
27117   SDValue NewSext = DAG.getNode(ISD::SIGN_EXTEND, SDLoc(Sext), VT, AddOp0);
27118   SDValue NewConstant = DAG.getConstant(AddConstant, SDLoc(Add), VT);
27119
27120   // The wider add is guaranteed to not wrap because both operands are
27121   // sign-extended.
27122   SDNodeFlags Flags;
27123   Flags.setNoSignedWrap(true);
27124   return DAG.getNode(ISD::ADD, SDLoc(Add), VT, NewSext, NewConstant, &Flags);
27125 }
27126
27127 static SDValue PerformSExtCombine(SDNode *N, SelectionDAG &DAG,
27128                                   TargetLowering::DAGCombinerInfo &DCI,
27129                                   const X86Subtarget *Subtarget) {
27130   SDValue N0 = N->getOperand(0);
27131   EVT VT = N->getValueType(0);
27132   EVT SVT = VT.getScalarType();
27133   EVT InVT = N0.getValueType();
27134   EVT InSVT = InVT.getScalarType();
27135   SDLoc DL(N);
27136
27137   // (i8,i32 sext (sdivrem (i8 x, i8 y)) ->
27138   // (i8,i32 (sdivrem_sext_hreg (i8 x, i8 y)
27139   // This exposes the sext to the sdivrem lowering, so that it directly extends
27140   // from AH (which we otherwise need to do contortions to access).
27141   if (N0.getOpcode() == ISD::SDIVREM && N0.getResNo() == 1 &&
27142       InVT == MVT::i8 && VT == MVT::i32) {
27143     SDVTList NodeTys = DAG.getVTList(MVT::i8, VT);
27144     SDValue R = DAG.getNode(X86ISD::SDIVREM8_SEXT_HREG, DL, NodeTys,
27145                             N0.getOperand(0), N0.getOperand(1));
27146     DAG.ReplaceAllUsesOfValueWith(N0.getValue(0), R.getValue(0));
27147     return R.getValue(1);
27148   }
27149
27150   if (!DCI.isBeforeLegalizeOps()) {
27151     if (InVT == MVT::i1) {
27152       SDValue Zero = DAG.getConstant(0, DL, VT);
27153       SDValue AllOnes =
27154         DAG.getConstant(APInt::getAllOnesValue(VT.getSizeInBits()), DL, VT);
27155       return DAG.getNode(ISD::SELECT, DL, VT, N0, AllOnes, Zero);
27156     }
27157     return SDValue();
27158   }
27159
27160   if (VT.isVector() && Subtarget->hasSSE2()) {
27161     auto ExtendVecSize = [&DAG](SDLoc DL, SDValue N, unsigned Size) {
27162       EVT InVT = N.getValueType();
27163       EVT OutVT = EVT::getVectorVT(*DAG.getContext(), InVT.getScalarType(),
27164                                    Size / InVT.getScalarSizeInBits());
27165       SmallVector<SDValue, 8> Opnds(Size / InVT.getSizeInBits(),
27166                                     DAG.getUNDEF(InVT));
27167       Opnds[0] = N;
27168       return DAG.getNode(ISD::CONCAT_VECTORS, DL, OutVT, Opnds);
27169     };
27170
27171     // If target-size is less than 128-bits, extend to a type that would extend
27172     // to 128 bits, extend that and extract the original target vector.
27173     if (VT.getSizeInBits() < 128 && !(128 % VT.getSizeInBits()) &&
27174         (SVT == MVT::i64 || SVT == MVT::i32 || SVT == MVT::i16) &&
27175         (InSVT == MVT::i32 || InSVT == MVT::i16 || InSVT == MVT::i8)) {
27176       unsigned Scale = 128 / VT.getSizeInBits();
27177       EVT ExVT =
27178           EVT::getVectorVT(*DAG.getContext(), SVT, 128 / SVT.getSizeInBits());
27179       SDValue Ex = ExtendVecSize(DL, N0, Scale * InVT.getSizeInBits());
27180       SDValue SExt = DAG.getNode(ISD::SIGN_EXTEND, DL, ExVT, Ex);
27181       return DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, VT, SExt,
27182                          DAG.getIntPtrConstant(0, DL));
27183     }
27184
27185     // If target-size is 128-bits, then convert to ISD::SIGN_EXTEND_VECTOR_INREG
27186     // which ensures lowering to X86ISD::VSEXT (pmovsx*).
27187     if (VT.getSizeInBits() == 128 &&
27188         (SVT == MVT::i64 || SVT == MVT::i32 || SVT == MVT::i16) &&
27189         (InSVT == MVT::i32 || InSVT == MVT::i16 || InSVT == MVT::i8)) {
27190       SDValue ExOp = ExtendVecSize(DL, N0, 128);
27191       return DAG.getSignExtendVectorInReg(ExOp, DL, VT);
27192     }
27193
27194     // On pre-AVX2 targets, split into 128-bit nodes of
27195     // ISD::SIGN_EXTEND_VECTOR_INREG.
27196     if (!Subtarget->hasInt256() && !(VT.getSizeInBits() % 128) &&
27197         (SVT == MVT::i64 || SVT == MVT::i32 || SVT == MVT::i16) &&
27198         (InSVT == MVT::i32 || InSVT == MVT::i16 || InSVT == MVT::i8)) {
27199       unsigned NumVecs = VT.getSizeInBits() / 128;
27200       unsigned NumSubElts = 128 / SVT.getSizeInBits();
27201       EVT SubVT = EVT::getVectorVT(*DAG.getContext(), SVT, NumSubElts);
27202       EVT InSubVT = EVT::getVectorVT(*DAG.getContext(), InSVT, NumSubElts);
27203
27204       SmallVector<SDValue, 8> Opnds;
27205       for (unsigned i = 0, Offset = 0; i != NumVecs;
27206            ++i, Offset += NumSubElts) {
27207         SDValue SrcVec = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, InSubVT, N0,
27208                                      DAG.getIntPtrConstant(Offset, DL));
27209         SrcVec = ExtendVecSize(DL, SrcVec, 128);
27210         SrcVec = DAG.getSignExtendVectorInReg(SrcVec, DL, SubVT);
27211         Opnds.push_back(SrcVec);
27212       }
27213       return DAG.getNode(ISD::CONCAT_VECTORS, DL, VT, Opnds);
27214     }
27215   }
27216
27217   if (Subtarget->hasAVX() && VT.is256BitVector())
27218     if (SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget))
27219       return R;
27220
27221   if (SDValue NewAdd = promoteSextBeforeAddNSW(N, DAG, Subtarget))
27222     return NewAdd;
27223
27224   return SDValue();
27225 }
27226
27227 static SDValue PerformFMACombine(SDNode *N, SelectionDAG &DAG,
27228                                  const X86Subtarget* Subtarget) {
27229   SDLoc dl(N);
27230   EVT VT = N->getValueType(0);
27231
27232   // Let legalize expand this if it isn't a legal type yet.
27233   if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
27234     return SDValue();
27235
27236   EVT ScalarVT = VT.getScalarType();
27237   if ((ScalarVT != MVT::f32 && ScalarVT != MVT::f64) || !Subtarget->hasAnyFMA())
27238     return SDValue();
27239
27240   SDValue A = N->getOperand(0);
27241   SDValue B = N->getOperand(1);
27242   SDValue C = N->getOperand(2);
27243
27244   bool NegA = (A.getOpcode() == ISD::FNEG);
27245   bool NegB = (B.getOpcode() == ISD::FNEG);
27246   bool NegC = (C.getOpcode() == ISD::FNEG);
27247
27248   // Negative multiplication when NegA xor NegB
27249   bool NegMul = (NegA != NegB);
27250   if (NegA)
27251     A = A.getOperand(0);
27252   if (NegB)
27253     B = B.getOperand(0);
27254   if (NegC)
27255     C = C.getOperand(0);
27256
27257   unsigned Opcode;
27258   if (!NegMul)
27259     Opcode = (!NegC) ? X86ISD::FMADD : X86ISD::FMSUB;
27260   else
27261     Opcode = (!NegC) ? X86ISD::FNMADD : X86ISD::FNMSUB;
27262
27263   return DAG.getNode(Opcode, dl, VT, A, B, C);
27264 }
27265
27266 static SDValue PerformZExtCombine(SDNode *N, SelectionDAG &DAG,
27267                                   TargetLowering::DAGCombinerInfo &DCI,
27268                                   const X86Subtarget *Subtarget) {
27269   // (i32 zext (and (i8  x86isd::setcc_carry), 1)) ->
27270   //           (and (i32 x86isd::setcc_carry), 1)
27271   // This eliminates the zext. This transformation is necessary because
27272   // ISD::SETCC is always legalized to i8.
27273   SDLoc dl(N);
27274   SDValue N0 = N->getOperand(0);
27275   EVT VT = N->getValueType(0);
27276
27277   if (N0.getOpcode() == ISD::AND &&
27278       N0.hasOneUse() &&
27279       N0.getOperand(0).hasOneUse()) {
27280     SDValue N00 = N0.getOperand(0);
27281     if (N00.getOpcode() == X86ISD::SETCC_CARRY) {
27282       if (!isOneConstant(N0.getOperand(1)))
27283         return SDValue();
27284       return DAG.getNode(ISD::AND, dl, VT,
27285                          DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
27286                                      N00.getOperand(0), N00.getOperand(1)),
27287                          DAG.getConstant(1, dl, VT));
27288     }
27289   }
27290
27291   if (N0.getOpcode() == ISD::TRUNCATE &&
27292       N0.hasOneUse() &&
27293       N0.getOperand(0).hasOneUse()) {
27294     SDValue N00 = N0.getOperand(0);
27295     if (N00.getOpcode() == X86ISD::SETCC_CARRY) {
27296       return DAG.getNode(ISD::AND, dl, VT,
27297                          DAG.getNode(X86ISD::SETCC_CARRY, dl, VT,
27298                                      N00.getOperand(0), N00.getOperand(1)),
27299                          DAG.getConstant(1, dl, VT));
27300     }
27301   }
27302
27303   if (VT.is256BitVector())
27304     if (SDValue R = WidenMaskArithmetic(N, DAG, DCI, Subtarget))
27305       return R;
27306
27307   // (i8,i32 zext (udivrem (i8 x, i8 y)) ->
27308   // (i8,i32 (udivrem_zext_hreg (i8 x, i8 y)
27309   // This exposes the zext to the udivrem lowering, so that it directly extends
27310   // from AH (which we otherwise need to do contortions to access).
27311   if (N0.getOpcode() == ISD::UDIVREM &&
27312       N0.getResNo() == 1 && N0.getValueType() == MVT::i8 &&
27313       (VT == MVT::i32 || VT == MVT::i64)) {
27314     SDVTList NodeTys = DAG.getVTList(MVT::i8, VT);
27315     SDValue R = DAG.getNode(X86ISD::UDIVREM8_ZEXT_HREG, dl, NodeTys,
27316                             N0.getOperand(0), N0.getOperand(1));
27317     DAG.ReplaceAllUsesOfValueWith(N0.getValue(0), R.getValue(0));
27318     return R.getValue(1);
27319   }
27320
27321   return SDValue();
27322 }
27323
27324 // Optimize x == -y --> x+y == 0
27325 //          x != -y --> x+y != 0
27326 static SDValue PerformISDSETCCCombine(SDNode *N, SelectionDAG &DAG,
27327                                       const X86Subtarget* Subtarget) {
27328   ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
27329   SDValue LHS = N->getOperand(0);
27330   SDValue RHS = N->getOperand(1);
27331   EVT VT = N->getValueType(0);
27332   SDLoc DL(N);
27333
27334   if ((CC == ISD::SETNE || CC == ISD::SETEQ) && LHS.getOpcode() == ISD::SUB)
27335     if (isNullConstant(LHS.getOperand(0)) && LHS.hasOneUse()) {
27336       SDValue addV = DAG.getNode(ISD::ADD, DL, LHS.getValueType(), RHS,
27337                                  LHS.getOperand(1));
27338       return DAG.getSetCC(DL, N->getValueType(0), addV,
27339                           DAG.getConstant(0, DL, addV.getValueType()), CC);
27340     }
27341   if ((CC == ISD::SETNE || CC == ISD::SETEQ) && RHS.getOpcode() == ISD::SUB)
27342     if (isNullConstant(RHS.getOperand(0)) && RHS.hasOneUse()) {
27343       SDValue addV = DAG.getNode(ISD::ADD, DL, RHS.getValueType(), LHS,
27344                                  RHS.getOperand(1));
27345       return DAG.getSetCC(DL, N->getValueType(0), addV,
27346                           DAG.getConstant(0, DL, addV.getValueType()), CC);
27347     }
27348
27349   if (VT.getScalarType() == MVT::i1 &&
27350       (CC == ISD::SETNE || CC == ISD::SETEQ || ISD::isSignedIntSetCC(CC))) {
27351     bool IsSEXT0 =
27352         (LHS.getOpcode() == ISD::SIGN_EXTEND) &&
27353         (LHS.getOperand(0).getValueType().getScalarType() == MVT::i1);
27354     bool IsVZero1 = ISD::isBuildVectorAllZeros(RHS.getNode());
27355
27356     if (!IsSEXT0 || !IsVZero1) {
27357       // Swap the operands and update the condition code.
27358       std::swap(LHS, RHS);
27359       CC = ISD::getSetCCSwappedOperands(CC);
27360
27361       IsSEXT0 = (LHS.getOpcode() == ISD::SIGN_EXTEND) &&
27362                 (LHS.getOperand(0).getValueType().getScalarType() == MVT::i1);
27363       IsVZero1 = ISD::isBuildVectorAllZeros(RHS.getNode());
27364     }
27365
27366     if (IsSEXT0 && IsVZero1) {
27367       assert(VT == LHS.getOperand(0).getValueType() &&
27368              "Uexpected operand type");
27369       if (CC == ISD::SETGT)
27370         return DAG.getConstant(0, DL, VT);
27371       if (CC == ISD::SETLE)
27372         return DAG.getConstant(1, DL, VT);
27373       if (CC == ISD::SETEQ || CC == ISD::SETGE)
27374         return DAG.getNOT(DL, LHS.getOperand(0), VT);
27375
27376       assert((CC == ISD::SETNE || CC == ISD::SETLT) &&
27377              "Unexpected condition code!");
27378       return LHS.getOperand(0);
27379     }
27380   }
27381
27382   return SDValue();
27383 }
27384
27385 static SDValue PerformBLENDICombine(SDNode *N, SelectionDAG &DAG) {
27386   SDValue V0 = N->getOperand(0);
27387   SDValue V1 = N->getOperand(1);
27388   SDLoc DL(N);
27389   EVT VT = N->getValueType(0);
27390
27391   // Canonicalize a v2f64 blend with a mask of 2 by swapping the vector
27392   // operands and changing the mask to 1. This saves us a bunch of
27393   // pattern-matching possibilities related to scalar math ops in SSE/AVX.
27394   // x86InstrInfo knows how to commute this back after instruction selection
27395   // if it would help register allocation.
27396
27397   // TODO: If optimizing for size or a processor that doesn't suffer from
27398   // partial register update stalls, this should be transformed into a MOVSD
27399   // instruction because a MOVSD is 1-2 bytes smaller than a BLENDPD.
27400
27401   if (VT == MVT::v2f64)
27402     if (auto *Mask = dyn_cast<ConstantSDNode>(N->getOperand(2)))
27403       if (Mask->getZExtValue() == 2 && !isShuffleFoldableLoad(V0)) {
27404         SDValue NewMask = DAG.getConstant(1, DL, MVT::i8);
27405         return DAG.getNode(X86ISD::BLENDI, DL, VT, V1, V0, NewMask);
27406       }
27407
27408   return SDValue();
27409 }
27410
27411 static SDValue PerformGatherScatterCombine(SDNode *N, SelectionDAG &DAG) {
27412   SDLoc DL(N);
27413   // Gather and Scatter instructions use k-registers for masks. The type of
27414   // the masks is v*i1. So the mask will be truncated anyway.
27415   // The SIGN_EXTEND_INREG my be dropped.
27416   SDValue Mask = N->getOperand(2);
27417   if (Mask.getOpcode() == ISD::SIGN_EXTEND_INREG) {
27418     SmallVector<SDValue, 5> NewOps(N->op_begin(), N->op_end());
27419     NewOps[2] = Mask.getOperand(0);
27420     DAG.UpdateNodeOperands(N, NewOps);
27421   }
27422   return SDValue();
27423 }
27424
27425 // Helper function of PerformSETCCCombine. It is to materialize "setb reg"
27426 // as "sbb reg,reg", since it can be extended without zext and produces
27427 // an all-ones bit which is more useful than 0/1 in some cases.
27428 static SDValue MaterializeSETB(SDLoc DL, SDValue EFLAGS, SelectionDAG &DAG,
27429                                MVT VT) {
27430   if (VT == MVT::i8)
27431     return DAG.getNode(ISD::AND, DL, VT,
27432                        DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
27433                                    DAG.getConstant(X86::COND_B, DL, MVT::i8),
27434                                    EFLAGS),
27435                        DAG.getConstant(1, DL, VT));
27436   assert (VT == MVT::i1 && "Unexpected type for SECCC node");
27437   return DAG.getNode(ISD::TRUNCATE, DL, MVT::i1,
27438                      DAG.getNode(X86ISD::SETCC_CARRY, DL, MVT::i8,
27439                                  DAG.getConstant(X86::COND_B, DL, MVT::i8),
27440                                  EFLAGS));
27441 }
27442
27443 // Optimize  RES = X86ISD::SETCC CONDCODE, EFLAG_INPUT
27444 static SDValue PerformSETCCCombine(SDNode *N, SelectionDAG &DAG,
27445                                    TargetLowering::DAGCombinerInfo &DCI,
27446                                    const X86Subtarget *Subtarget) {
27447   SDLoc DL(N);
27448   X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(0));
27449   SDValue EFLAGS = N->getOperand(1);
27450
27451   if (CC == X86::COND_A) {
27452     // Try to convert COND_A into COND_B in an attempt to facilitate
27453     // materializing "setb reg".
27454     //
27455     // Do not flip "e > c", where "c" is a constant, because Cmp instruction
27456     // cannot take an immediate as its first operand.
27457     //
27458     if (EFLAGS.getOpcode() == X86ISD::SUB && EFLAGS.hasOneUse() &&
27459         EFLAGS.getValueType().isInteger() &&
27460         !isa<ConstantSDNode>(EFLAGS.getOperand(1))) {
27461       SDValue NewSub = DAG.getNode(X86ISD::SUB, SDLoc(EFLAGS),
27462                                    EFLAGS.getNode()->getVTList(),
27463                                    EFLAGS.getOperand(1), EFLAGS.getOperand(0));
27464       SDValue NewEFLAGS = SDValue(NewSub.getNode(), EFLAGS.getResNo());
27465       return MaterializeSETB(DL, NewEFLAGS, DAG, N->getSimpleValueType(0));
27466     }
27467   }
27468
27469   // Materialize "setb reg" as "sbb reg,reg", since it can be extended without
27470   // a zext and produces an all-ones bit which is more useful than 0/1 in some
27471   // cases.
27472   if (CC == X86::COND_B)
27473     return MaterializeSETB(DL, EFLAGS, DAG, N->getSimpleValueType(0));
27474
27475   if (SDValue Flags = checkBoolTestSetCCCombine(EFLAGS, CC)) {
27476     SDValue Cond = DAG.getConstant(CC, DL, MVT::i8);
27477     return DAG.getNode(X86ISD::SETCC, DL, N->getVTList(), Cond, Flags);
27478   }
27479
27480   return SDValue();
27481 }
27482
27483 // Optimize branch condition evaluation.
27484 //
27485 static SDValue PerformBrCondCombine(SDNode *N, SelectionDAG &DAG,
27486                                     TargetLowering::DAGCombinerInfo &DCI,
27487                                     const X86Subtarget *Subtarget) {
27488   SDLoc DL(N);
27489   SDValue Chain = N->getOperand(0);
27490   SDValue Dest = N->getOperand(1);
27491   SDValue EFLAGS = N->getOperand(3);
27492   X86::CondCode CC = X86::CondCode(N->getConstantOperandVal(2));
27493
27494   if (SDValue Flags = checkBoolTestSetCCCombine(EFLAGS, CC)) {
27495     SDValue Cond = DAG.getConstant(CC, DL, MVT::i8);
27496     return DAG.getNode(X86ISD::BRCOND, DL, N->getVTList(), Chain, Dest, Cond,
27497                        Flags);
27498   }
27499
27500   return SDValue();
27501 }
27502
27503 static SDValue performVectorCompareAndMaskUnaryOpCombine(SDNode *N,
27504                                                          SelectionDAG &DAG) {
27505   // Take advantage of vector comparisons producing 0 or -1 in each lane to
27506   // optimize away operation when it's from a constant.
27507   //
27508   // The general transformation is:
27509   //    UNARYOP(AND(VECTOR_CMP(x,y), constant)) -->
27510   //       AND(VECTOR_CMP(x,y), constant2)
27511   //    constant2 = UNARYOP(constant)
27512
27513   // Early exit if this isn't a vector operation, the operand of the
27514   // unary operation isn't a bitwise AND, or if the sizes of the operations
27515   // aren't the same.
27516   EVT VT = N->getValueType(0);
27517   if (!VT.isVector() || N->getOperand(0)->getOpcode() != ISD::AND ||
27518       N->getOperand(0)->getOperand(0)->getOpcode() != ISD::SETCC ||
27519       VT.getSizeInBits() != N->getOperand(0)->getValueType(0).getSizeInBits())
27520     return SDValue();
27521
27522   // Now check that the other operand of the AND is a constant. We could
27523   // make the transformation for non-constant splats as well, but it's unclear
27524   // that would be a benefit as it would not eliminate any operations, just
27525   // perform one more step in scalar code before moving to the vector unit.
27526   if (BuildVectorSDNode *BV =
27527           dyn_cast<BuildVectorSDNode>(N->getOperand(0)->getOperand(1))) {
27528     // Bail out if the vector isn't a constant.
27529     if (!BV->isConstant())
27530       return SDValue();
27531
27532     // Everything checks out. Build up the new and improved node.
27533     SDLoc DL(N);
27534     EVT IntVT = BV->getValueType(0);
27535     // Create a new constant of the appropriate type for the transformed
27536     // DAG.
27537     SDValue SourceConst = DAG.getNode(N->getOpcode(), DL, VT, SDValue(BV, 0));
27538     // The AND node needs bitcasts to/from an integer vector type around it.
27539     SDValue MaskConst = DAG.getBitcast(IntVT, SourceConst);
27540     SDValue NewAnd = DAG.getNode(ISD::AND, DL, IntVT,
27541                                  N->getOperand(0)->getOperand(0), MaskConst);
27542     SDValue Res = DAG.getBitcast(VT, NewAnd);
27543     return Res;
27544   }
27545
27546   return SDValue();
27547 }
27548
27549 static SDValue PerformUINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
27550                                         const X86Subtarget *Subtarget) {
27551   SDValue Op0 = N->getOperand(0);
27552   EVT VT = N->getValueType(0);
27553   EVT InVT = Op0.getValueType();
27554   EVT InSVT = InVT.getScalarType();
27555   const TargetLowering &TLI = DAG.getTargetLoweringInfo();
27556
27557   // UINT_TO_FP(vXi8) -> SINT_TO_FP(ZEXT(vXi8 to vXi32))
27558   // UINT_TO_FP(vXi16) -> SINT_TO_FP(ZEXT(vXi16 to vXi32))
27559   if (InVT.isVector() && (InSVT == MVT::i8 || InSVT == MVT::i16)) {
27560     SDLoc dl(N);
27561     EVT DstVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32,
27562                                  InVT.getVectorNumElements());
27563     SDValue P = DAG.getNode(ISD::ZERO_EXTEND, dl, DstVT, Op0);
27564
27565     if (TLI.isOperationLegal(ISD::UINT_TO_FP, DstVT))
27566       return DAG.getNode(ISD::UINT_TO_FP, dl, VT, P);
27567
27568     return DAG.getNode(ISD::SINT_TO_FP, dl, VT, P);
27569   }
27570
27571   return SDValue();
27572 }
27573
27574 static SDValue PerformSINT_TO_FPCombine(SDNode *N, SelectionDAG &DAG,
27575                                         const X86Subtarget *Subtarget) {
27576   // First try to optimize away the conversion entirely when it's
27577   // conditionally from a constant. Vectors only.
27578   if (SDValue Res = performVectorCompareAndMaskUnaryOpCombine(N, DAG))
27579     return Res;
27580
27581   // Now move on to more general possibilities.
27582   SDValue Op0 = N->getOperand(0);
27583   EVT VT = N->getValueType(0);
27584   EVT InVT = Op0.getValueType();
27585   EVT InSVT = InVT.getScalarType();
27586
27587   // SINT_TO_FP(vXi8) -> SINT_TO_FP(SEXT(vXi8 to vXi32))
27588   // SINT_TO_FP(vXi16) -> SINT_TO_FP(SEXT(vXi16 to vXi32))
27589   if (InVT.isVector() && (InSVT == MVT::i8 || InSVT == MVT::i16)) {
27590     SDLoc dl(N);
27591     EVT DstVT = EVT::getVectorVT(*DAG.getContext(), MVT::i32,
27592                                  InVT.getVectorNumElements());
27593     SDValue P = DAG.getNode(ISD::SIGN_EXTEND, dl, DstVT, Op0);
27594     return DAG.getNode(ISD::SINT_TO_FP, dl, VT, P);
27595   }
27596
27597   // Transform (SINT_TO_FP (i64 ...)) into an x87 operation if we have
27598   // a 32-bit target where SSE doesn't support i64->FP operations.
27599   if (!Subtarget->useSoftFloat() && Op0.getOpcode() == ISD::LOAD) {
27600     LoadSDNode *Ld = cast<LoadSDNode>(Op0.getNode());
27601     EVT LdVT = Ld->getValueType(0);
27602
27603     // This transformation is not supported if the result type is f16
27604     if (VT == MVT::f16)
27605       return SDValue();
27606
27607     if (!Ld->isVolatile() && !VT.isVector() &&
27608         ISD::isNON_EXTLoad(Op0.getNode()) && Op0.hasOneUse() &&
27609         !Subtarget->is64Bit() && LdVT == MVT::i64) {
27610       SDValue FILDChain = Subtarget->getTargetLowering()->BuildFILD(
27611           SDValue(N, 0), LdVT, Ld->getChain(), Op0, DAG);
27612       DAG.ReplaceAllUsesOfValueWith(Op0.getValue(1), FILDChain.getValue(1));
27613       return FILDChain;
27614     }
27615   }
27616   return SDValue();
27617 }
27618
27619 // Optimize RES, EFLAGS = X86ISD::ADC LHS, RHS, EFLAGS
27620 static SDValue PerformADCCombine(SDNode *N, SelectionDAG &DAG,
27621                                  X86TargetLowering::DAGCombinerInfo &DCI) {
27622   // If the LHS and RHS of the ADC node are zero, then it can't overflow and
27623   // the result is either zero or one (depending on the input carry bit).
27624   // Strength reduce this down to a "set on carry" aka SETCC_CARRY&1.
27625   if (X86::isZeroNode(N->getOperand(0)) &&
27626       X86::isZeroNode(N->getOperand(1)) &&
27627       // We don't have a good way to replace an EFLAGS use, so only do this when
27628       // dead right now.
27629       SDValue(N, 1).use_empty()) {
27630     SDLoc DL(N);
27631     EVT VT = N->getValueType(0);
27632     SDValue CarryOut = DAG.getConstant(0, DL, N->getValueType(1));
27633     SDValue Res1 = DAG.getNode(ISD::AND, DL, VT,
27634                                DAG.getNode(X86ISD::SETCC_CARRY, DL, VT,
27635                                            DAG.getConstant(X86::COND_B, DL,
27636                                                            MVT::i8),
27637                                            N->getOperand(2)),
27638                                DAG.getConstant(1, DL, VT));
27639     return DCI.CombineTo(N, Res1, CarryOut);
27640   }
27641
27642   return SDValue();
27643 }
27644
27645 // fold (add Y, (sete  X, 0)) -> adc  0, Y
27646 //      (add Y, (setne X, 0)) -> sbb -1, Y
27647 //      (sub (sete  X, 0), Y) -> sbb  0, Y
27648 //      (sub (setne X, 0), Y) -> adc -1, Y
27649 static SDValue OptimizeConditionalInDecrement(SDNode *N, SelectionDAG &DAG) {
27650   SDLoc DL(N);
27651
27652   // Look through ZExts.
27653   SDValue Ext = N->getOperand(N->getOpcode() == ISD::SUB ? 1 : 0);
27654   if (Ext.getOpcode() != ISD::ZERO_EXTEND || !Ext.hasOneUse())
27655     return SDValue();
27656
27657   SDValue SetCC = Ext.getOperand(0);
27658   if (SetCC.getOpcode() != X86ISD::SETCC || !SetCC.hasOneUse())
27659     return SDValue();
27660
27661   X86::CondCode CC = (X86::CondCode)SetCC.getConstantOperandVal(0);
27662   if (CC != X86::COND_E && CC != X86::COND_NE)
27663     return SDValue();
27664
27665   SDValue Cmp = SetCC.getOperand(1);
27666   if (Cmp.getOpcode() != X86ISD::CMP || !Cmp.hasOneUse() ||
27667       !X86::isZeroNode(Cmp.getOperand(1)) ||
27668       !Cmp.getOperand(0).getValueType().isInteger())
27669     return SDValue();
27670
27671   SDValue CmpOp0 = Cmp.getOperand(0);
27672   SDValue NewCmp = DAG.getNode(X86ISD::CMP, DL, MVT::i32, CmpOp0,
27673                                DAG.getConstant(1, DL, CmpOp0.getValueType()));
27674
27675   SDValue OtherVal = N->getOperand(N->getOpcode() == ISD::SUB ? 0 : 1);
27676   if (CC == X86::COND_NE)
27677     return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::ADC : X86ISD::SBB,
27678                        DL, OtherVal.getValueType(), OtherVal,
27679                        DAG.getConstant(-1ULL, DL, OtherVal.getValueType()),
27680                        NewCmp);
27681   return DAG.getNode(N->getOpcode() == ISD::SUB ? X86ISD::SBB : X86ISD::ADC,
27682                      DL, OtherVal.getValueType(), OtherVal,
27683                      DAG.getConstant(0, DL, OtherVal.getValueType()), NewCmp);
27684 }
27685
27686 /// PerformADDCombine - Do target-specific dag combines on integer adds.
27687 static SDValue PerformAddCombine(SDNode *N, SelectionDAG &DAG,
27688                                  const X86Subtarget *Subtarget) {
27689   EVT VT = N->getValueType(0);
27690   SDValue Op0 = N->getOperand(0);
27691   SDValue Op1 = N->getOperand(1);
27692
27693   // Try to synthesize horizontal adds from adds of shuffles.
27694   if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
27695        (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
27696       isHorizontalBinOp(Op0, Op1, true))
27697     return DAG.getNode(X86ISD::HADD, SDLoc(N), VT, Op0, Op1);
27698
27699   return OptimizeConditionalInDecrement(N, DAG);
27700 }
27701
27702 static SDValue PerformSubCombine(SDNode *N, SelectionDAG &DAG,
27703                                  const X86Subtarget *Subtarget) {
27704   SDValue Op0 = N->getOperand(0);
27705   SDValue Op1 = N->getOperand(1);
27706
27707   // X86 can't encode an immediate LHS of a sub. See if we can push the
27708   // negation into a preceding instruction.
27709   if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op0)) {
27710     // If the RHS of the sub is a XOR with one use and a constant, invert the
27711     // immediate. Then add one to the LHS of the sub so we can turn
27712     // X-Y -> X+~Y+1, saving one register.
27713     if (Op1->hasOneUse() && Op1.getOpcode() == ISD::XOR &&
27714         isa<ConstantSDNode>(Op1.getOperand(1))) {
27715       APInt XorC = cast<ConstantSDNode>(Op1.getOperand(1))->getAPIntValue();
27716       EVT VT = Op0.getValueType();
27717       SDValue NewXor = DAG.getNode(ISD::XOR, SDLoc(Op1), VT,
27718                                    Op1.getOperand(0),
27719                                    DAG.getConstant(~XorC, SDLoc(Op1), VT));
27720       return DAG.getNode(ISD::ADD, SDLoc(N), VT, NewXor,
27721                          DAG.getConstant(C->getAPIntValue() + 1, SDLoc(N), VT));
27722     }
27723   }
27724
27725   // Try to synthesize horizontal adds from adds of shuffles.
27726   EVT VT = N->getValueType(0);
27727   if (((Subtarget->hasSSSE3() && (VT == MVT::v8i16 || VT == MVT::v4i32)) ||
27728        (Subtarget->hasInt256() && (VT == MVT::v16i16 || VT == MVT::v8i32))) &&
27729       isHorizontalBinOp(Op0, Op1, true))
27730     return DAG.getNode(X86ISD::HSUB, SDLoc(N), VT, Op0, Op1);
27731
27732   return OptimizeConditionalInDecrement(N, DAG);
27733 }
27734
27735 /// performVZEXTCombine - Performs build vector combines
27736 static SDValue performVZEXTCombine(SDNode *N, SelectionDAG &DAG,
27737                                    TargetLowering::DAGCombinerInfo &DCI,
27738                                    const X86Subtarget *Subtarget) {
27739   SDLoc DL(N);
27740   MVT VT = N->getSimpleValueType(0);
27741   SDValue Op = N->getOperand(0);
27742   MVT OpVT = Op.getSimpleValueType();
27743   MVT OpEltVT = OpVT.getVectorElementType();
27744   unsigned InputBits = OpEltVT.getSizeInBits() * VT.getVectorNumElements();
27745
27746   // (vzext (bitcast (vzext (x)) -> (vzext x)
27747   SDValue V = Op;
27748   while (V.getOpcode() == ISD::BITCAST)
27749     V = V.getOperand(0);
27750
27751   if (V != Op && V.getOpcode() == X86ISD::VZEXT) {
27752     MVT InnerVT = V.getSimpleValueType();
27753     MVT InnerEltVT = InnerVT.getVectorElementType();
27754
27755     // If the element sizes match exactly, we can just do one larger vzext. This
27756     // is always an exact type match as vzext operates on integer types.
27757     if (OpEltVT == InnerEltVT) {
27758       assert(OpVT == InnerVT && "Types must match for vzext!");
27759       return DAG.getNode(X86ISD::VZEXT, DL, VT, V.getOperand(0));
27760     }
27761
27762     // The only other way we can combine them is if only a single element of the
27763     // inner vzext is used in the input to the outer vzext.
27764     if (InnerEltVT.getSizeInBits() < InputBits)
27765       return SDValue();
27766
27767     // In this case, the inner vzext is completely dead because we're going to
27768     // only look at bits inside of the low element. Just do the outer vzext on
27769     // a bitcast of the input to the inner.
27770     return DAG.getNode(X86ISD::VZEXT, DL, VT, DAG.getBitcast(OpVT, V));
27771   }
27772
27773   // Check if we can bypass extracting and re-inserting an element of an input
27774   // vector. Essentially:
27775   // (bitcast (sclr2vec (ext_vec_elt x))) -> (bitcast x)
27776   if (V.getOpcode() == ISD::SCALAR_TO_VECTOR &&
27777       V.getOperand(0).getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
27778       V.getOperand(0).getSimpleValueType().getSizeInBits() == InputBits) {
27779     SDValue ExtractedV = V.getOperand(0);
27780     SDValue OrigV = ExtractedV.getOperand(0);
27781     if (isNullConstant(ExtractedV.getOperand(1))) {
27782         MVT OrigVT = OrigV.getSimpleValueType();
27783         // Extract a subvector if necessary...
27784         if (OrigVT.getSizeInBits() > OpVT.getSizeInBits()) {
27785           int Ratio = OrigVT.getSizeInBits() / OpVT.getSizeInBits();
27786           OrigVT = MVT::getVectorVT(OrigVT.getVectorElementType(),
27787                                     OrigVT.getVectorNumElements() / Ratio);
27788           OrigV = DAG.getNode(ISD::EXTRACT_SUBVECTOR, DL, OrigVT, OrigV,
27789                               DAG.getIntPtrConstant(0, DL));
27790         }
27791         Op = DAG.getBitcast(OpVT, OrigV);
27792         return DAG.getNode(X86ISD::VZEXT, DL, VT, Op);
27793       }
27794   }
27795
27796   return SDValue();
27797 }
27798
27799 SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
27800                                              DAGCombinerInfo &DCI) const {
27801   SelectionDAG &DAG = DCI.DAG;
27802   switch (N->getOpcode()) {
27803   default: break;
27804   case ISD::EXTRACT_VECTOR_ELT:
27805     return PerformEXTRACT_VECTOR_ELTCombine(N, DAG, DCI);
27806   case ISD::VSELECT:
27807   case ISD::SELECT:
27808   case X86ISD::SHRUNKBLEND:
27809     return PerformSELECTCombine(N, DAG, DCI, Subtarget);
27810   case ISD::BITCAST:        return PerformBITCASTCombine(N, DAG, Subtarget);
27811   case X86ISD::CMOV:        return PerformCMOVCombine(N, DAG, DCI, Subtarget);
27812   case ISD::ADD:            return PerformAddCombine(N, DAG, Subtarget);
27813   case ISD::SUB:            return PerformSubCombine(N, DAG, Subtarget);
27814   case X86ISD::ADC:         return PerformADCCombine(N, DAG, DCI);
27815   case ISD::MUL:            return PerformMulCombine(N, DAG, DCI);
27816   case ISD::SHL:
27817   case ISD::SRA:
27818   case ISD::SRL:            return PerformShiftCombine(N, DAG, DCI, Subtarget);
27819   case ISD::AND:            return PerformAndCombine(N, DAG, DCI, Subtarget);
27820   case ISD::OR:             return PerformOrCombine(N, DAG, DCI, Subtarget);
27821   case ISD::XOR:            return PerformXorCombine(N, DAG, DCI, Subtarget);
27822   case ISD::LOAD:           return PerformLOADCombine(N, DAG, DCI, Subtarget);
27823   case ISD::MLOAD:          return PerformMLOADCombine(N, DAG, DCI, Subtarget);
27824   case ISD::STORE:          return PerformSTORECombine(N, DAG, Subtarget);
27825   case ISD::MSTORE:         return PerformMSTORECombine(N, DAG, Subtarget);
27826   case ISD::SINT_TO_FP:     return PerformSINT_TO_FPCombine(N, DAG, Subtarget);
27827   case ISD::UINT_TO_FP:     return PerformUINT_TO_FPCombine(N, DAG, Subtarget);
27828   case ISD::FADD:           return PerformFADDCombine(N, DAG, Subtarget);
27829   case ISD::FSUB:           return PerformFSUBCombine(N, DAG, Subtarget);
27830   case ISD::FNEG:           return PerformFNEGCombine(N, DAG, Subtarget);
27831   case ISD::TRUNCATE:       return PerformTRUNCATECombine(N, DAG, Subtarget);
27832   case X86ISD::FXOR:
27833   case X86ISD::FOR:         return PerformFORCombine(N, DAG, Subtarget);
27834   case X86ISD::FMIN:
27835   case X86ISD::FMAX:        return PerformFMinFMaxCombine(N, DAG);
27836   case ISD::FMINNUM:
27837   case ISD::FMAXNUM:        return performFMinNumFMaxNumCombine(N, DAG,
27838                                                                 Subtarget);
27839   case X86ISD::FAND:        return PerformFANDCombine(N, DAG, Subtarget);
27840   case X86ISD::FANDN:       return PerformFANDNCombine(N, DAG, Subtarget);
27841   case X86ISD::BT:          return PerformBTCombine(N, DAG, DCI);
27842   case X86ISD::VZEXT_MOVL:  return PerformVZEXT_MOVLCombine(N, DAG);
27843   case ISD::ANY_EXTEND:
27844   case ISD::ZERO_EXTEND:    return PerformZExtCombine(N, DAG, DCI, Subtarget);
27845   case ISD::SIGN_EXTEND:    return PerformSExtCombine(N, DAG, DCI, Subtarget);
27846   case ISD::SIGN_EXTEND_INREG:
27847     return PerformSIGN_EXTEND_INREGCombine(N, DAG, Subtarget);
27848   case ISD::SETCC:          return PerformISDSETCCCombine(N, DAG, Subtarget);
27849   case X86ISD::SETCC:       return PerformSETCCCombine(N, DAG, DCI, Subtarget);
27850   case X86ISD::BRCOND:      return PerformBrCondCombine(N, DAG, DCI, Subtarget);
27851   case X86ISD::VZEXT:       return performVZEXTCombine(N, DAG, DCI, Subtarget);
27852   case X86ISD::SHUFP:       // Handle all target specific shuffles
27853   case X86ISD::PALIGNR:
27854   case X86ISD::UNPCKH:
27855   case X86ISD::UNPCKL:
27856   case X86ISD::MOVHLPS:
27857   case X86ISD::MOVLHPS:
27858   case X86ISD::PSHUFB:
27859   case X86ISD::PSHUFD:
27860   case X86ISD::PSHUFHW:
27861   case X86ISD::PSHUFLW:
27862   case X86ISD::MOVSS:
27863   case X86ISD::MOVSD:
27864   case X86ISD::VPERMILPI:
27865   case X86ISD::VPERM2X128:
27866   case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, DCI,Subtarget);
27867   case ISD::FMA:            return PerformFMACombine(N, DAG, Subtarget);
27868   case X86ISD::BLENDI:    return PerformBLENDICombine(N, DAG);
27869   case ISD::MGATHER:
27870   case ISD::MSCATTER:       return PerformGatherScatterCombine(N, DAG);
27871   }
27872
27873   return SDValue();
27874 }
27875
27876 /// isTypeDesirableForOp - Return true if the target has native support for
27877 /// the specified value type and it is 'desirable' to use the type for the
27878 /// given node type. e.g. On x86 i16 is legal, but undesirable since i16
27879 /// instruction encodings are longer and some i16 instructions are slow.
27880 bool X86TargetLowering::isTypeDesirableForOp(unsigned Opc, EVT VT) const {
27881   if (!isTypeLegal(VT))
27882     return false;
27883   if (VT != MVT::i16)
27884     return true;
27885
27886   switch (Opc) {
27887   default:
27888     return true;
27889   case ISD::LOAD:
27890   case ISD::SIGN_EXTEND:
27891   case ISD::ZERO_EXTEND:
27892   case ISD::ANY_EXTEND:
27893   case ISD::SHL:
27894   case ISD::SRL:
27895   case ISD::SUB:
27896   case ISD::ADD:
27897   case ISD::MUL:
27898   case ISD::AND:
27899   case ISD::OR:
27900   case ISD::XOR:
27901     return false;
27902   }
27903 }
27904
27905 /// IsDesirableToPromoteOp - This method query the target whether it is
27906 /// beneficial for dag combiner to promote the specified node. If true, it
27907 /// should return the desired promotion type by reference.
27908 bool X86TargetLowering::IsDesirableToPromoteOp(SDValue Op, EVT &PVT) const {
27909   EVT VT = Op.getValueType();
27910   if (VT != MVT::i16)
27911     return false;
27912
27913   bool Promote = false;
27914   bool Commute = false;
27915   switch (Op.getOpcode()) {
27916   default: break;
27917   case ISD::LOAD: {
27918     LoadSDNode *LD = cast<LoadSDNode>(Op);
27919     // If the non-extending load has a single use and it's not live out, then it
27920     // might be folded.
27921     if (LD->getExtensionType() == ISD::NON_EXTLOAD /*&&
27922                                                      Op.hasOneUse()*/) {
27923       for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
27924              UE = Op.getNode()->use_end(); UI != UE; ++UI) {
27925         // The only case where we'd want to promote LOAD (rather then it being
27926         // promoted as an operand is when it's only use is liveout.
27927         if (UI->getOpcode() != ISD::CopyToReg)
27928           return false;
27929       }
27930     }
27931     Promote = true;
27932     break;
27933   }
27934   case ISD::SIGN_EXTEND:
27935   case ISD::ZERO_EXTEND:
27936   case ISD::ANY_EXTEND:
27937     Promote = true;
27938     break;
27939   case ISD::SHL:
27940   case ISD::SRL: {
27941     SDValue N0 = Op.getOperand(0);
27942     // Look out for (store (shl (load), x)).
27943     if (MayFoldLoad(N0) && MayFoldIntoStore(Op))
27944       return false;
27945     Promote = true;
27946     break;
27947   }
27948   case ISD::ADD:
27949   case ISD::MUL:
27950   case ISD::AND:
27951   case ISD::OR:
27952   case ISD::XOR:
27953     Commute = true;
27954     // fallthrough
27955   case ISD::SUB: {
27956     SDValue N0 = Op.getOperand(0);
27957     SDValue N1 = Op.getOperand(1);
27958     if (!Commute && MayFoldLoad(N1))
27959       return false;
27960     // Avoid disabling potential load folding opportunities.
27961     if (MayFoldLoad(N0) && (!isa<ConstantSDNode>(N1) || MayFoldIntoStore(Op)))
27962       return false;
27963     if (MayFoldLoad(N1) && (!isa<ConstantSDNode>(N0) || MayFoldIntoStore(Op)))
27964       return false;
27965     Promote = true;
27966   }
27967   }
27968
27969   PVT = MVT::i32;
27970   return Promote;
27971 }
27972
27973 //===----------------------------------------------------------------------===//
27974 //                           X86 Inline Assembly Support
27975 //===----------------------------------------------------------------------===//
27976
27977 // Helper to match a string separated by whitespace.
27978 static bool matchAsm(StringRef S, ArrayRef<const char *> Pieces) {
27979   S = S.substr(S.find_first_not_of(" \t")); // Skip leading whitespace.
27980
27981   for (StringRef Piece : Pieces) {
27982     if (!S.startswith(Piece)) // Check if the piece matches.
27983       return false;
27984
27985     S = S.substr(Piece.size());
27986     StringRef::size_type Pos = S.find_first_not_of(" \t");
27987     if (Pos == 0) // We matched a prefix.
27988       return false;
27989
27990     S = S.substr(Pos);
27991   }
27992
27993   return S.empty();
27994 }
27995
27996 static bool clobbersFlagRegisters(const SmallVector<StringRef, 4> &AsmPieces) {
27997
27998   if (AsmPieces.size() == 3 || AsmPieces.size() == 4) {
27999     if (std::count(AsmPieces.begin(), AsmPieces.end(), "~{cc}") &&
28000         std::count(AsmPieces.begin(), AsmPieces.end(), "~{flags}") &&
28001         std::count(AsmPieces.begin(), AsmPieces.end(), "~{fpsr}")) {
28002
28003       if (AsmPieces.size() == 3)
28004         return true;
28005       else if (std::count(AsmPieces.begin(), AsmPieces.end(), "~{dirflag}"))
28006         return true;
28007     }
28008   }
28009   return false;
28010 }
28011
28012 bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
28013   InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
28014
28015   std::string AsmStr = IA->getAsmString();
28016
28017   IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
28018   if (!Ty || Ty->getBitWidth() % 16 != 0)
28019     return false;
28020
28021   // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
28022   SmallVector<StringRef, 4> AsmPieces;
28023   SplitString(AsmStr, AsmPieces, ";\n");
28024
28025   switch (AsmPieces.size()) {
28026   default: return false;
28027   case 1:
28028     // FIXME: this should verify that we are targeting a 486 or better.  If not,
28029     // we will turn this bswap into something that will be lowered to logical
28030     // ops instead of emitting the bswap asm.  For now, we don't support 486 or
28031     // lower so don't worry about this.
28032     // bswap $0
28033     if (matchAsm(AsmPieces[0], {"bswap", "$0"}) ||
28034         matchAsm(AsmPieces[0], {"bswapl", "$0"}) ||
28035         matchAsm(AsmPieces[0], {"bswapq", "$0"}) ||
28036         matchAsm(AsmPieces[0], {"bswap", "${0:q}"}) ||
28037         matchAsm(AsmPieces[0], {"bswapl", "${0:q}"}) ||
28038         matchAsm(AsmPieces[0], {"bswapq", "${0:q}"})) {
28039       // No need to check constraints, nothing other than the equivalent of
28040       // "=r,0" would be valid here.
28041       return IntrinsicLowering::LowerToByteSwap(CI);
28042     }
28043
28044     // rorw $$8, ${0:w}  -->  llvm.bswap.i16
28045     if (CI->getType()->isIntegerTy(16) &&
28046         IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
28047         (matchAsm(AsmPieces[0], {"rorw", "$$8,", "${0:w}"}) ||
28048          matchAsm(AsmPieces[0], {"rolw", "$$8,", "${0:w}"}))) {
28049       AsmPieces.clear();
28050       StringRef ConstraintsStr = IA->getConstraintString();
28051       SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
28052       array_pod_sort(AsmPieces.begin(), AsmPieces.end());
28053       if (clobbersFlagRegisters(AsmPieces))
28054         return IntrinsicLowering::LowerToByteSwap(CI);
28055     }
28056     break;
28057   case 3:
28058     if (CI->getType()->isIntegerTy(32) &&
28059         IA->getConstraintString().compare(0, 5, "=r,0,") == 0 &&
28060         matchAsm(AsmPieces[0], {"rorw", "$$8,", "${0:w}"}) &&
28061         matchAsm(AsmPieces[1], {"rorl", "$$16,", "$0"}) &&
28062         matchAsm(AsmPieces[2], {"rorw", "$$8,", "${0:w}"})) {
28063       AsmPieces.clear();
28064       StringRef ConstraintsStr = IA->getConstraintString();
28065       SplitString(StringRef(ConstraintsStr).substr(5), AsmPieces, ",");
28066       array_pod_sort(AsmPieces.begin(), AsmPieces.end());
28067       if (clobbersFlagRegisters(AsmPieces))
28068         return IntrinsicLowering::LowerToByteSwap(CI);
28069     }
28070
28071     if (CI->getType()->isIntegerTy(64)) {
28072       InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
28073       if (Constraints.size() >= 2 &&
28074           Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
28075           Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
28076         // bswap %eax / bswap %edx / xchgl %eax, %edx  -> llvm.bswap.i64
28077         if (matchAsm(AsmPieces[0], {"bswap", "%eax"}) &&
28078             matchAsm(AsmPieces[1], {"bswap", "%edx"}) &&
28079             matchAsm(AsmPieces[2], {"xchgl", "%eax,", "%edx"}))
28080           return IntrinsicLowering::LowerToByteSwap(CI);
28081       }
28082     }
28083     break;
28084   }
28085   return false;
28086 }
28087
28088 /// getConstraintType - Given a constraint letter, return the type of
28089 /// constraint it is for this target.
28090 X86TargetLowering::ConstraintType
28091 X86TargetLowering::getConstraintType(StringRef Constraint) const {
28092   if (Constraint.size() == 1) {
28093     switch (Constraint[0]) {
28094     case 'R':
28095     case 'q':
28096     case 'Q':
28097     case 'f':
28098     case 't':
28099     case 'u':
28100     case 'y':
28101     case 'x':
28102     case 'Y':
28103     case 'l':
28104       return C_RegisterClass;
28105     case 'a':
28106     case 'b':
28107     case 'c':
28108     case 'd':
28109     case 'S':
28110     case 'D':
28111     case 'A':
28112       return C_Register;
28113     case 'I':
28114     case 'J':
28115     case 'K':
28116     case 'L':
28117     case 'M':
28118     case 'N':
28119     case 'G':
28120     case 'C':
28121     case 'e':
28122     case 'Z':
28123       return C_Other;
28124     default:
28125       break;
28126     }
28127   }
28128   return TargetLowering::getConstraintType(Constraint);
28129 }
28130
28131 /// Examine constraint type and operand type and determine a weight value.
28132 /// This object must already have been set up with the operand type
28133 /// and the current alternative constraint selected.
28134 TargetLowering::ConstraintWeight
28135   X86TargetLowering::getSingleConstraintMatchWeight(
28136     AsmOperandInfo &info, const char *constraint) const {
28137   ConstraintWeight weight = CW_Invalid;
28138   Value *CallOperandVal = info.CallOperandVal;
28139     // If we don't have a value, we can't do a match,
28140     // but allow it at the lowest weight.
28141   if (!CallOperandVal)
28142     return CW_Default;
28143   Type *type = CallOperandVal->getType();
28144   // Look at the constraint type.
28145   switch (*constraint) {
28146   default:
28147     weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
28148   case 'R':
28149   case 'q':
28150   case 'Q':
28151   case 'a':
28152   case 'b':
28153   case 'c':
28154   case 'd':
28155   case 'S':
28156   case 'D':
28157   case 'A':
28158     if (CallOperandVal->getType()->isIntegerTy())
28159       weight = CW_SpecificReg;
28160     break;
28161   case 'f':
28162   case 't':
28163   case 'u':
28164     if (type->isFloatingPointTy())
28165       weight = CW_SpecificReg;
28166     break;
28167   case 'y':
28168     if (type->isX86_MMXTy() && Subtarget->hasMMX())
28169       weight = CW_SpecificReg;
28170     break;
28171   case 'x':
28172   case 'Y':
28173     if (((type->getPrimitiveSizeInBits() == 128) && Subtarget->hasSSE1()) ||
28174         ((type->getPrimitiveSizeInBits() == 256) && Subtarget->hasFp256()))
28175       weight = CW_Register;
28176     break;
28177   case 'I':
28178     if (ConstantInt *C = dyn_cast<ConstantInt>(info.CallOperandVal)) {
28179       if (C->getZExtValue() <= 31)
28180         weight = CW_Constant;
28181     }
28182     break;
28183   case 'J':
28184     if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
28185       if (C->getZExtValue() <= 63)
28186         weight = CW_Constant;
28187     }
28188     break;
28189   case 'K':
28190     if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
28191       if ((C->getSExtValue() >= -0x80) && (C->getSExtValue() <= 0x7f))
28192         weight = CW_Constant;
28193     }
28194     break;
28195   case 'L':
28196     if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
28197       if ((C->getZExtValue() == 0xff) || (C->getZExtValue() == 0xffff))
28198         weight = CW_Constant;
28199     }
28200     break;
28201   case 'M':
28202     if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
28203       if (C->getZExtValue() <= 3)
28204         weight = CW_Constant;
28205     }
28206     break;
28207   case 'N':
28208     if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
28209       if (C->getZExtValue() <= 0xff)
28210         weight = CW_Constant;
28211     }
28212     break;
28213   case 'G':
28214   case 'C':
28215     if (isa<ConstantFP>(CallOperandVal)) {
28216       weight = CW_Constant;
28217     }
28218     break;
28219   case 'e':
28220     if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
28221       if ((C->getSExtValue() >= -0x80000000LL) &&
28222           (C->getSExtValue() <= 0x7fffffffLL))
28223         weight = CW_Constant;
28224     }
28225     break;
28226   case 'Z':
28227     if (ConstantInt *C = dyn_cast<ConstantInt>(CallOperandVal)) {
28228       if (C->getZExtValue() <= 0xffffffff)
28229         weight = CW_Constant;
28230     }
28231     break;
28232   }
28233   return weight;
28234 }
28235
28236 /// LowerXConstraint - try to replace an X constraint, which matches anything,
28237 /// with another that has more specific requirements based on the type of the
28238 /// corresponding operand.
28239 const char *X86TargetLowering::
28240 LowerXConstraint(EVT ConstraintVT) const {
28241   // FP X constraints get lowered to SSE1/2 registers if available, otherwise
28242   // 'f' like normal targets.
28243   if (ConstraintVT.isFloatingPoint()) {
28244     if (Subtarget->hasSSE2())
28245       return "Y";
28246     if (Subtarget->hasSSE1())
28247       return "x";
28248   }
28249
28250   return TargetLowering::LowerXConstraint(ConstraintVT);
28251 }
28252
28253 /// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
28254 /// vector.  If it is invalid, don't add anything to Ops.
28255 void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
28256                                                      std::string &Constraint,
28257                                                      std::vector<SDValue>&Ops,
28258                                                      SelectionDAG &DAG) const {
28259   SDValue Result;
28260
28261   // Only support length 1 constraints for now.
28262   if (Constraint.length() > 1) return;
28263
28264   char ConstraintLetter = Constraint[0];
28265   switch (ConstraintLetter) {
28266   default: break;
28267   case 'I':
28268     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
28269       if (C->getZExtValue() <= 31) {
28270         Result = DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
28271                                        Op.getValueType());
28272         break;
28273       }
28274     }
28275     return;
28276   case 'J':
28277     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
28278       if (C->getZExtValue() <= 63) {
28279         Result = DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
28280                                        Op.getValueType());
28281         break;
28282       }
28283     }
28284     return;
28285   case 'K':
28286     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
28287       if (isInt<8>(C->getSExtValue())) {
28288         Result = DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
28289                                        Op.getValueType());
28290         break;
28291       }
28292     }
28293     return;
28294   case 'L':
28295     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
28296       if (C->getZExtValue() == 0xff || C->getZExtValue() == 0xffff ||
28297           (Subtarget->is64Bit() && C->getZExtValue() == 0xffffffff)) {
28298         Result = DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
28299                                        Op.getValueType());
28300         break;
28301       }
28302     }
28303     return;
28304   case 'M':
28305     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
28306       if (C->getZExtValue() <= 3) {
28307         Result = DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
28308                                        Op.getValueType());
28309         break;
28310       }
28311     }
28312     return;
28313   case 'N':
28314     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
28315       if (C->getZExtValue() <= 255) {
28316         Result = DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
28317                                        Op.getValueType());
28318         break;
28319       }
28320     }
28321     return;
28322   case 'O':
28323     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
28324       if (C->getZExtValue() <= 127) {
28325         Result = DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
28326                                        Op.getValueType());
28327         break;
28328       }
28329     }
28330     return;
28331   case 'e': {
28332     // 32-bit signed value
28333     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
28334       if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
28335                                            C->getSExtValue())) {
28336         // Widen to 64 bits here to get it sign extended.
28337         Result = DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op), MVT::i64);
28338         break;
28339       }
28340     // FIXME gcc accepts some relocatable values here too, but only in certain
28341     // memory models; it's complicated.
28342     }
28343     return;
28344   }
28345   case 'Z': {
28346     // 32-bit unsigned value
28347     if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
28348       if (ConstantInt::isValueValidForType(Type::getInt32Ty(*DAG.getContext()),
28349                                            C->getZExtValue())) {
28350         Result = DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
28351                                        Op.getValueType());
28352         break;
28353       }
28354     }
28355     // FIXME gcc accepts some relocatable values here too, but only in certain
28356     // memory models; it's complicated.
28357     return;
28358   }
28359   case 'i': {
28360     // Literal immediates are always ok.
28361     if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
28362       // Widen to 64 bits here to get it sign extended.
28363       Result = DAG.getTargetConstant(CST->getSExtValue(), SDLoc(Op), MVT::i64);
28364       break;
28365     }
28366
28367     // In any sort of PIC mode addresses need to be computed at runtime by
28368     // adding in a register or some sort of table lookup.  These can't
28369     // be used as immediates.
28370     if (Subtarget->isPICStyleGOT() || Subtarget->isPICStyleStubPIC())
28371       return;
28372
28373     // If we are in non-pic codegen mode, we allow the address of a global (with
28374     // an optional displacement) to be used with 'i'.
28375     GlobalAddressSDNode *GA = nullptr;
28376     int64_t Offset = 0;
28377
28378     // Match either (GA), (GA+C), (GA+C1+C2), etc.
28379     while (1) {
28380       if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
28381         Offset += GA->getOffset();
28382         break;
28383       } else if (Op.getOpcode() == ISD::ADD) {
28384         if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
28385           Offset += C->getZExtValue();
28386           Op = Op.getOperand(0);
28387           continue;
28388         }
28389       } else if (Op.getOpcode() == ISD::SUB) {
28390         if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
28391           Offset += -C->getZExtValue();
28392           Op = Op.getOperand(0);
28393           continue;
28394         }
28395       }
28396
28397       // Otherwise, this isn't something we can handle, reject it.
28398       return;
28399     }
28400
28401     const GlobalValue *GV = GA->getGlobal();
28402     // If we require an extra load to get this address, as in PIC mode, we
28403     // can't accept it.
28404     if (isGlobalStubReference(
28405             Subtarget->ClassifyGlobalReference(GV, DAG.getTarget())))
28406       return;
28407
28408     Result = DAG.getTargetGlobalAddress(GV, SDLoc(Op),
28409                                         GA->getValueType(0), Offset);
28410     break;
28411   }
28412   }
28413
28414   if (Result.getNode()) {
28415     Ops.push_back(Result);
28416     return;
28417   }
28418   return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
28419 }
28420
28421 std::pair<unsigned, const TargetRegisterClass *>
28422 X86TargetLowering::getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI,
28423                                                 StringRef Constraint,
28424                                                 MVT VT) const {
28425   // First, see if this is a constraint that directly corresponds to an LLVM
28426   // register class.
28427   if (Constraint.size() == 1) {
28428     // GCC Constraint Letters
28429     switch (Constraint[0]) {
28430     default: break;
28431       // TODO: Slight differences here in allocation order and leaving
28432       // RIP in the class. Do they matter any more here than they do
28433       // in the normal allocation?
28434     case 'q':   // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
28435       if (Subtarget->is64Bit()) {
28436         if (VT == MVT::i32 || VT == MVT::f32)
28437           return std::make_pair(0U, &X86::GR32RegClass);
28438         if (VT == MVT::i16)
28439           return std::make_pair(0U, &X86::GR16RegClass);
28440         if (VT == MVT::i8 || VT == MVT::i1)
28441           return std::make_pair(0U, &X86::GR8RegClass);
28442         if (VT == MVT::i64 || VT == MVT::f64)
28443           return std::make_pair(0U, &X86::GR64RegClass);
28444         break;
28445       }
28446       // 32-bit fallthrough
28447     case 'Q':   // Q_REGS
28448       if (VT == MVT::i32 || VT == MVT::f32)
28449         return std::make_pair(0U, &X86::GR32_ABCDRegClass);
28450       if (VT == MVT::i16)
28451         return std::make_pair(0U, &X86::GR16_ABCDRegClass);
28452       if (VT == MVT::i8 || VT == MVT::i1)
28453         return std::make_pair(0U, &X86::GR8_ABCD_LRegClass);
28454       if (VT == MVT::i64)
28455         return std::make_pair(0U, &X86::GR64_ABCDRegClass);
28456       break;
28457     case 'r':   // GENERAL_REGS
28458     case 'l':   // INDEX_REGS
28459       if (VT == MVT::i8 || VT == MVT::i1)
28460         return std::make_pair(0U, &X86::GR8RegClass);
28461       if (VT == MVT::i16)
28462         return std::make_pair(0U, &X86::GR16RegClass);
28463       if (VT == MVT::i32 || VT == MVT::f32 || !Subtarget->is64Bit())
28464         return std::make_pair(0U, &X86::GR32RegClass);
28465       return std::make_pair(0U, &X86::GR64RegClass);
28466     case 'R':   // LEGACY_REGS
28467       if (VT == MVT::i8 || VT == MVT::i1)
28468         return std::make_pair(0U, &X86::GR8_NOREXRegClass);
28469       if (VT == MVT::i16)
28470         return std::make_pair(0U, &X86::GR16_NOREXRegClass);
28471       if (VT == MVT::i32 || !Subtarget->is64Bit())
28472         return std::make_pair(0U, &X86::GR32_NOREXRegClass);
28473       return std::make_pair(0U, &X86::GR64_NOREXRegClass);
28474     case 'f':  // FP Stack registers.
28475       // If SSE is enabled for this VT, use f80 to ensure the isel moves the
28476       // value to the correct fpstack register class.
28477       if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
28478         return std::make_pair(0U, &X86::RFP32RegClass);
28479       if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
28480         return std::make_pair(0U, &X86::RFP64RegClass);
28481       return std::make_pair(0U, &X86::RFP80RegClass);
28482     case 'y':   // MMX_REGS if MMX allowed.
28483       if (!Subtarget->hasMMX()) break;
28484       return std::make_pair(0U, &X86::VR64RegClass);
28485     case 'Y':   // SSE_REGS if SSE2 allowed
28486       if (!Subtarget->hasSSE2()) break;
28487       // FALL THROUGH.
28488     case 'x':   // SSE_REGS if SSE1 allowed or AVX_REGS if AVX allowed
28489       if (!Subtarget->hasSSE1()) break;
28490
28491       switch (VT.SimpleTy) {
28492       default: break;
28493       // Scalar SSE types.
28494       case MVT::f32:
28495       case MVT::i32:
28496         return std::make_pair(0U, &X86::FR32RegClass);
28497       case MVT::f64:
28498       case MVT::i64:
28499         return std::make_pair(0U, &X86::FR64RegClass);
28500       // TODO: Handle f128 and i128 in FR128RegClass after it is tested well.
28501       // Vector types.
28502       case MVT::v16i8:
28503       case MVT::v8i16:
28504       case MVT::v4i32:
28505       case MVT::v2i64:
28506       case MVT::v4f32:
28507       case MVT::v2f64:
28508         return std::make_pair(0U, &X86::VR128RegClass);
28509       // AVX types.
28510       case MVT::v32i8:
28511       case MVT::v16i16:
28512       case MVT::v8i32:
28513       case MVT::v4i64:
28514       case MVT::v8f32:
28515       case MVT::v4f64:
28516         return std::make_pair(0U, &X86::VR256RegClass);
28517       case MVT::v8f64:
28518       case MVT::v16f32:
28519       case MVT::v16i32:
28520       case MVT::v8i64:
28521         return std::make_pair(0U, &X86::VR512RegClass);
28522       }
28523       break;
28524     }
28525   }
28526
28527   // Use the default implementation in TargetLowering to convert the register
28528   // constraint into a member of a register class.
28529   std::pair<unsigned, const TargetRegisterClass*> Res;
28530   Res = TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
28531
28532   // Not found as a standard register?
28533   if (!Res.second) {
28534     // Map st(0) -> st(7) -> ST0
28535     if (Constraint.size() == 7 && Constraint[0] == '{' &&
28536         tolower(Constraint[1]) == 's' &&
28537         tolower(Constraint[2]) == 't' &&
28538         Constraint[3] == '(' &&
28539         (Constraint[4] >= '0' && Constraint[4] <= '7') &&
28540         Constraint[5] == ')' &&
28541         Constraint[6] == '}') {
28542
28543       Res.first = X86::FP0+Constraint[4]-'0';
28544       Res.second = &X86::RFP80RegClass;
28545       return Res;
28546     }
28547
28548     // GCC allows "st(0)" to be called just plain "st".
28549     if (StringRef("{st}").equals_lower(Constraint)) {
28550       Res.first = X86::FP0;
28551       Res.second = &X86::RFP80RegClass;
28552       return Res;
28553     }
28554
28555     // flags -> EFLAGS
28556     if (StringRef("{flags}").equals_lower(Constraint)) {
28557       Res.first = X86::EFLAGS;
28558       Res.second = &X86::CCRRegClass;
28559       return Res;
28560     }
28561
28562     // 'A' means EAX + EDX.
28563     if (Constraint == "A") {
28564       Res.first = X86::EAX;
28565       Res.second = &X86::GR32_ADRegClass;
28566       return Res;
28567     }
28568     return Res;
28569   }
28570
28571   // Otherwise, check to see if this is a register class of the wrong value
28572   // type.  For example, we want to map "{ax},i32" -> {eax}, we don't want it to
28573   // turn into {ax},{dx}.
28574   // MVT::Other is used to specify clobber names.
28575   if (Res.second->hasType(VT) || VT == MVT::Other)
28576     return Res;   // Correct type already, nothing to do.
28577
28578   // Get a matching integer of the correct size. i.e. "ax" with MVT::32 should
28579   // return "eax". This should even work for things like getting 64bit integer
28580   // registers when given an f64 type.
28581   const TargetRegisterClass *Class = Res.second;
28582   if (Class == &X86::GR8RegClass || Class == &X86::GR16RegClass ||
28583       Class == &X86::GR32RegClass || Class == &X86::GR64RegClass) {
28584     unsigned Size = VT.getSizeInBits();
28585     if (Size == 1) Size = 8;
28586     unsigned DestReg = getX86SubSuperRegisterOrZero(Res.first, Size);
28587     if (DestReg > 0) {
28588       Res.first = DestReg;
28589       Res.second = Size == 8 ? &X86::GR8RegClass
28590                  : Size == 16 ? &X86::GR16RegClass
28591                  : Size == 32 ? &X86::GR32RegClass
28592                  : &X86::GR64RegClass;
28593       assert(Res.second->contains(Res.first) && "Register in register class");
28594     } else {
28595       // No register found/type mismatch.
28596       Res.first = 0;
28597       Res.second = nullptr;
28598     }
28599   } else if (Class == &X86::FR32RegClass || Class == &X86::FR64RegClass ||
28600              Class == &X86::VR128RegClass || Class == &X86::VR256RegClass ||
28601              Class == &X86::FR32XRegClass || Class == &X86::FR64XRegClass ||
28602              Class == &X86::VR128XRegClass || Class == &X86::VR256XRegClass ||
28603              Class == &X86::VR512RegClass) {
28604     // Handle references to XMM physical registers that got mapped into the
28605     // wrong class.  This can happen with constraints like {xmm0} where the
28606     // target independent register mapper will just pick the first match it can
28607     // find, ignoring the required type.
28608
28609     // TODO: Handle f128 and i128 in FR128RegClass after it is tested well.
28610     if (VT == MVT::f32 || VT == MVT::i32)
28611       Res.second = &X86::FR32RegClass;
28612     else if (VT == MVT::f64 || VT == MVT::i64)
28613       Res.second = &X86::FR64RegClass;
28614     else if (X86::VR128RegClass.hasType(VT))
28615       Res.second = &X86::VR128RegClass;
28616     else if (X86::VR256RegClass.hasType(VT))
28617       Res.second = &X86::VR256RegClass;
28618     else if (X86::VR512RegClass.hasType(VT))
28619       Res.second = &X86::VR512RegClass;
28620     else {
28621       // Type mismatch and not a clobber: Return an error;
28622       Res.first = 0;
28623       Res.second = nullptr;
28624     }
28625   }
28626
28627   return Res;
28628 }
28629
28630 int X86TargetLowering::getScalingFactorCost(const DataLayout &DL,
28631                                             const AddrMode &AM, Type *Ty,
28632                                             unsigned AS) const {
28633   // Scaling factors are not free at all.
28634   // An indexed folded instruction, i.e., inst (reg1, reg2, scale),
28635   // will take 2 allocations in the out of order engine instead of 1
28636   // for plain addressing mode, i.e. inst (reg1).
28637   // E.g.,
28638   // vaddps (%rsi,%drx), %ymm0, %ymm1
28639   // Requires two allocations (one for the load, one for the computation)
28640   // whereas:
28641   // vaddps (%rsi), %ymm0, %ymm1
28642   // Requires just 1 allocation, i.e., freeing allocations for other operations
28643   // and having less micro operations to execute.
28644   //
28645   // For some X86 architectures, this is even worse because for instance for
28646   // stores, the complex addressing mode forces the instruction to use the
28647   // "load" ports instead of the dedicated "store" port.
28648   // E.g., on Haswell:
28649   // vmovaps %ymm1, (%r8, %rdi) can use port 2 or 3.
28650   // vmovaps %ymm1, (%r8) can use port 2, 3, or 7.
28651   if (isLegalAddressingMode(DL, AM, Ty, AS))
28652     // Scale represents reg2 * scale, thus account for 1
28653     // as soon as we use a second register.
28654     return AM.Scale != 0;
28655   return -1;
28656 }
28657
28658 bool X86TargetLowering::isIntDivCheap(EVT VT, AttributeSet Attr) const {
28659   // Integer division on x86 is expensive. However, when aggressively optimizing
28660   // for code size, we prefer to use a div instruction, as it is usually smaller
28661   // than the alternative sequence.
28662   // The exception to this is vector division. Since x86 doesn't have vector
28663   // integer division, leaving the division as-is is a loss even in terms of
28664   // size, because it will have to be scalarized, while the alternative code
28665   // sequence can be performed in vector form.
28666   bool OptSize = Attr.hasAttribute(AttributeSet::FunctionIndex,
28667                                    Attribute::MinSize);
28668   return OptSize && !VT.isVector();
28669 }