1 //===-- SystemZISelLowering.cpp - SystemZ 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 implements the SystemZTargetLowering class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "SystemZISelLowering.h"
15 #include "SystemZCallingConv.h"
16 #include "SystemZConstantPoolValue.h"
17 #include "SystemZMachineFunctionInfo.h"
18 #include "SystemZTargetMachine.h"
19 #include "llvm/CodeGen/CallingConvLower.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
23 #include "llvm/IR/Intrinsics.h"
24 #include <cctype>
25 
26 using namespace llvm;
27 
28 #define DEBUG_TYPE "systemz-lower"
29 
30 namespace {
31 // Represents a sequence for extracting a 0/1 value from an IPM result:
32 // (((X ^ XORValue) + AddValue) >> Bit)
33 struct IPMConversion {
34   IPMConversion(unsigned xorValue, int64_t addValue, unsigned bit)
35     : XORValue(xorValue), AddValue(addValue), Bit(bit) {}
36 
37   int64_t XORValue;
38   int64_t AddValue;
39   unsigned Bit;
40 };
41 
42 // Represents information about a comparison.
43 struct Comparison {
44   Comparison(SDValue Op0In, SDValue Op1In)
45     : Op0(Op0In), Op1(Op1In), Opcode(0), ICmpType(0), CCValid(0), CCMask(0) {}
46 
47   // The operands to the comparison.
48   SDValue Op0, Op1;
49 
50   // The opcode that should be used to compare Op0 and Op1.
51   unsigned Opcode;
52 
53   // A SystemZICMP value.  Only used for integer comparisons.
54   unsigned ICmpType;
55 
56   // The mask of CC values that Opcode can produce.
57   unsigned CCValid;
58 
59   // The mask of CC values for which the original condition is true.
60   unsigned CCMask;
61 };
62 } // end anonymous namespace
63 
64 // Classify VT as either 32 or 64 bit.
65 static bool is32Bit(EVT VT) {
66   switch (VT.getSimpleVT().SimpleTy) {
67   case MVT::i32:
68     return true;
69   case MVT::i64:
70     return false;
71   default:
72     llvm_unreachable("Unsupported type");
73   }
74 }
75 
76 // Return a version of MachineOperand that can be safely used before the
77 // final use.
78 static MachineOperand earlyUseOperand(MachineOperand Op) {
79   if (Op.isReg())
80     Op.setIsKill(false);
81   return Op;
82 }
83 
84 SystemZTargetLowering::SystemZTargetLowering(const TargetMachine &TM,
85                                              const SystemZSubtarget &STI)
86     : TargetLowering(TM), Subtarget(STI) {
87   MVT PtrVT = MVT::getIntegerVT(8 * TM.getPointerSize());
88 
89   // Set up the register classes.
90   if (Subtarget.hasHighWord())
91     addRegisterClass(MVT::i32, &SystemZ::GRX32BitRegClass);
92   else
93     addRegisterClass(MVT::i32, &SystemZ::GR32BitRegClass);
94   addRegisterClass(MVT::i64, &SystemZ::GR64BitRegClass);
95   if (Subtarget.hasVector()) {
96     addRegisterClass(MVT::f32, &SystemZ::VR32BitRegClass);
97     addRegisterClass(MVT::f64, &SystemZ::VR64BitRegClass);
98   } else {
99     addRegisterClass(MVT::f32, &SystemZ::FP32BitRegClass);
100     addRegisterClass(MVT::f64, &SystemZ::FP64BitRegClass);
101   }
102   addRegisterClass(MVT::f128, &SystemZ::FP128BitRegClass);
103 
104   if (Subtarget.hasVector()) {
105     addRegisterClass(MVT::v16i8, &SystemZ::VR128BitRegClass);
106     addRegisterClass(MVT::v8i16, &SystemZ::VR128BitRegClass);
107     addRegisterClass(MVT::v4i32, &SystemZ::VR128BitRegClass);
108     addRegisterClass(MVT::v2i64, &SystemZ::VR128BitRegClass);
109     addRegisterClass(MVT::v4f32, &SystemZ::VR128BitRegClass);
110     addRegisterClass(MVT::v2f64, &SystemZ::VR128BitRegClass);
111   }
112 
113   // Compute derived properties from the register classes
114   computeRegisterProperties(Subtarget.getRegisterInfo());
115 
116   // Set up special registers.
117   setStackPointerRegisterToSaveRestore(SystemZ::R15D);
118 
119   // TODO: It may be better to default to latency-oriented scheduling, however
120   // LLVM's current latency-oriented scheduler can't handle physreg definitions
121   // such as SystemZ has with CC, so set this to the register-pressure
122   // scheduler, because it can.
123   setSchedulingPreference(Sched::RegPressure);
124 
125   setBooleanContents(ZeroOrOneBooleanContent);
126   setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
127 
128   // Instructions are strings of 2-byte aligned 2-byte values.
129   setMinFunctionAlignment(2);
130 
131   // Handle operations that are handled in a similar way for all types.
132   for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
133        I <= MVT::LAST_FP_VALUETYPE;
134        ++I) {
135     MVT VT = MVT::SimpleValueType(I);
136     if (isTypeLegal(VT)) {
137       // Lower SET_CC into an IPM-based sequence.
138       setOperationAction(ISD::SETCC, VT, Custom);
139 
140       // Expand SELECT(C, A, B) into SELECT_CC(X, 0, A, B, NE).
141       setOperationAction(ISD::SELECT, VT, Expand);
142 
143       // Lower SELECT_CC and BR_CC into separate comparisons and branches.
144       setOperationAction(ISD::SELECT_CC, VT, Custom);
145       setOperationAction(ISD::BR_CC,     VT, Custom);
146     }
147   }
148 
149   // Expand jump table branches as address arithmetic followed by an
150   // indirect jump.
151   setOperationAction(ISD::BR_JT, MVT::Other, Expand);
152 
153   // Expand BRCOND into a BR_CC (see above).
154   setOperationAction(ISD::BRCOND, MVT::Other, Expand);
155 
156   // Handle integer types.
157   for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
158        I <= MVT::LAST_INTEGER_VALUETYPE;
159        ++I) {
160     MVT VT = MVT::SimpleValueType(I);
161     if (isTypeLegal(VT)) {
162       // Expand individual DIV and REMs into DIVREMs.
163       setOperationAction(ISD::SDIV, VT, Expand);
164       setOperationAction(ISD::UDIV, VT, Expand);
165       setOperationAction(ISD::SREM, VT, Expand);
166       setOperationAction(ISD::UREM, VT, Expand);
167       setOperationAction(ISD::SDIVREM, VT, Custom);
168       setOperationAction(ISD::UDIVREM, VT, Custom);
169 
170       // Lower ATOMIC_LOAD and ATOMIC_STORE into normal volatile loads and
171       // stores, putting a serialization instruction after the stores.
172       setOperationAction(ISD::ATOMIC_LOAD,  VT, Custom);
173       setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
174 
175       // Lower ATOMIC_LOAD_SUB into ATOMIC_LOAD_ADD if LAA and LAAG are
176       // available, or if the operand is constant.
177       setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
178 
179       // Use POPCNT on z196 and above.
180       if (Subtarget.hasPopulationCount())
181         setOperationAction(ISD::CTPOP, VT, Custom);
182       else
183         setOperationAction(ISD::CTPOP, VT, Expand);
184 
185       // No special instructions for these.
186       setOperationAction(ISD::CTTZ,            VT, Expand);
187       setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
188       setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
189       setOperationAction(ISD::ROTR,            VT, Expand);
190 
191       // Use *MUL_LOHI where possible instead of MULH*.
192       setOperationAction(ISD::MULHS, VT, Expand);
193       setOperationAction(ISD::MULHU, VT, Expand);
194       setOperationAction(ISD::SMUL_LOHI, VT, Custom);
195       setOperationAction(ISD::UMUL_LOHI, VT, Custom);
196 
197       // Only z196 and above have native support for conversions to unsigned.
198       if (!Subtarget.hasFPExtension())
199         setOperationAction(ISD::FP_TO_UINT, VT, Expand);
200     }
201   }
202 
203   // Type legalization will convert 8- and 16-bit atomic operations into
204   // forms that operate on i32s (but still keeping the original memory VT).
205   // Lower them into full i32 operations.
206   setOperationAction(ISD::ATOMIC_SWAP,      MVT::i32, Custom);
207   setOperationAction(ISD::ATOMIC_LOAD_ADD,  MVT::i32, Custom);
208   setOperationAction(ISD::ATOMIC_LOAD_SUB,  MVT::i32, Custom);
209   setOperationAction(ISD::ATOMIC_LOAD_AND,  MVT::i32, Custom);
210   setOperationAction(ISD::ATOMIC_LOAD_OR,   MVT::i32, Custom);
211   setOperationAction(ISD::ATOMIC_LOAD_XOR,  MVT::i32, Custom);
212   setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Custom);
213   setOperationAction(ISD::ATOMIC_LOAD_MIN,  MVT::i32, Custom);
214   setOperationAction(ISD::ATOMIC_LOAD_MAX,  MVT::i32, Custom);
215   setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Custom);
216   setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Custom);
217   setOperationAction(ISD::ATOMIC_CMP_SWAP,  MVT::i32, Custom);
218 
219   // z10 has instructions for signed but not unsigned FP conversion.
220   // Handle unsigned 32-bit types as signed 64-bit types.
221   if (!Subtarget.hasFPExtension()) {
222     setOperationAction(ISD::UINT_TO_FP, MVT::i32, Promote);
223     setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
224   }
225 
226   // We have native support for a 64-bit CTLZ, via FLOGR.
227   setOperationAction(ISD::CTLZ, MVT::i32, Promote);
228   setOperationAction(ISD::CTLZ, MVT::i64, Legal);
229 
230   // Give LowerOperation the chance to replace 64-bit ORs with subregs.
231   setOperationAction(ISD::OR, MVT::i64, Custom);
232 
233   // FIXME: Can we support these natively?
234   setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
235   setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
236   setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
237 
238   // We have native instructions for i8, i16 and i32 extensions, but not i1.
239   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
240   for (MVT VT : MVT::integer_valuetypes()) {
241     setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
242     setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
243     setLoadExtAction(ISD::EXTLOAD,  VT, MVT::i1, Promote);
244   }
245 
246   // Handle the various types of symbolic address.
247   setOperationAction(ISD::ConstantPool,     PtrVT, Custom);
248   setOperationAction(ISD::GlobalAddress,    PtrVT, Custom);
249   setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
250   setOperationAction(ISD::BlockAddress,     PtrVT, Custom);
251   setOperationAction(ISD::JumpTable,        PtrVT, Custom);
252 
253   // We need to handle dynamic allocations specially because of the
254   // 160-byte area at the bottom of the stack.
255   setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom);
256 
257   // Use custom expanders so that we can force the function to use
258   // a frame pointer.
259   setOperationAction(ISD::STACKSAVE,    MVT::Other, Custom);
260   setOperationAction(ISD::STACKRESTORE, MVT::Other, Custom);
261 
262   // Handle prefetches with PFD or PFDRL.
263   setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
264 
265   for (MVT VT : MVT::vector_valuetypes()) {
266     // Assume by default that all vector operations need to be expanded.
267     for (unsigned Opcode = 0; Opcode < ISD::BUILTIN_OP_END; ++Opcode)
268       if (getOperationAction(Opcode, VT) == Legal)
269         setOperationAction(Opcode, VT, Expand);
270 
271     // Likewise all truncating stores and extending loads.
272     for (MVT InnerVT : MVT::vector_valuetypes()) {
273       setTruncStoreAction(VT, InnerVT, Expand);
274       setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
275       setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
276       setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
277     }
278 
279     if (isTypeLegal(VT)) {
280       // These operations are legal for anything that can be stored in a
281       // vector register, even if there is no native support for the format
282       // as such.  In particular, we can do these for v4f32 even though there
283       // are no specific instructions for that format.
284       setOperationAction(ISD::LOAD, VT, Legal);
285       setOperationAction(ISD::STORE, VT, Legal);
286       setOperationAction(ISD::VSELECT, VT, Legal);
287       setOperationAction(ISD::BITCAST, VT, Legal);
288       setOperationAction(ISD::UNDEF, VT, Legal);
289 
290       // Likewise, except that we need to replace the nodes with something
291       // more specific.
292       setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
293       setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
294     }
295   }
296 
297   // Handle integer vector types.
298   for (MVT VT : MVT::integer_vector_valuetypes()) {
299     if (isTypeLegal(VT)) {
300       // These operations have direct equivalents.
301       setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Legal);
302       setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Legal);
303       setOperationAction(ISD::ADD, VT, Legal);
304       setOperationAction(ISD::SUB, VT, Legal);
305       if (VT != MVT::v2i64)
306         setOperationAction(ISD::MUL, VT, Legal);
307       setOperationAction(ISD::AND, VT, Legal);
308       setOperationAction(ISD::OR, VT, Legal);
309       setOperationAction(ISD::XOR, VT, Legal);
310       setOperationAction(ISD::CTPOP, VT, Custom);
311       setOperationAction(ISD::CTTZ, VT, Legal);
312       setOperationAction(ISD::CTLZ, VT, Legal);
313       setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Custom);
314       setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Custom);
315 
316       // Convert a GPR scalar to a vector by inserting it into element 0.
317       setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
318 
319       // Use a series of unpacks for extensions.
320       setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, VT, Custom);
321       setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, VT, Custom);
322 
323       // Detect shifts by a scalar amount and convert them into
324       // V*_BY_SCALAR.
325       setOperationAction(ISD::SHL, VT, Custom);
326       setOperationAction(ISD::SRA, VT, Custom);
327       setOperationAction(ISD::SRL, VT, Custom);
328 
329       // At present ROTL isn't matched by DAGCombiner.  ROTR should be
330       // converted into ROTL.
331       setOperationAction(ISD::ROTL, VT, Expand);
332       setOperationAction(ISD::ROTR, VT, Expand);
333 
334       // Map SETCCs onto one of VCE, VCH or VCHL, swapping the operands
335       // and inverting the result as necessary.
336       setOperationAction(ISD::SETCC, VT, Custom);
337     }
338   }
339 
340   if (Subtarget.hasVector()) {
341     // There should be no need to check for float types other than v2f64
342     // since <2 x f32> isn't a legal type.
343     setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal);
344     setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal);
345     setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal);
346     setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal);
347   }
348 
349   // Handle floating-point types.
350   for (unsigned I = MVT::FIRST_FP_VALUETYPE;
351        I <= MVT::LAST_FP_VALUETYPE;
352        ++I) {
353     MVT VT = MVT::SimpleValueType(I);
354     if (isTypeLegal(VT)) {
355       // We can use FI for FRINT.
356       setOperationAction(ISD::FRINT, VT, Legal);
357 
358       // We can use the extended form of FI for other rounding operations.
359       if (Subtarget.hasFPExtension()) {
360         setOperationAction(ISD::FNEARBYINT, VT, Legal);
361         setOperationAction(ISD::FFLOOR, VT, Legal);
362         setOperationAction(ISD::FCEIL, VT, Legal);
363         setOperationAction(ISD::FTRUNC, VT, Legal);
364         setOperationAction(ISD::FROUND, VT, Legal);
365       }
366 
367       // No special instructions for these.
368       setOperationAction(ISD::FSIN, VT, Expand);
369       setOperationAction(ISD::FCOS, VT, Expand);
370       setOperationAction(ISD::FSINCOS, VT, Expand);
371       setOperationAction(ISD::FREM, VT, Expand);
372       setOperationAction(ISD::FPOW, VT, Expand);
373     }
374   }
375 
376   // Handle floating-point vector types.
377   if (Subtarget.hasVector()) {
378     // Scalar-to-vector conversion is just a subreg.
379     setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Legal);
380     setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal);
381 
382     // Some insertions and extractions can be done directly but others
383     // need to go via integers.
384     setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
385     setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
386     setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
387     setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
388 
389     // These operations have direct equivalents.
390     setOperationAction(ISD::FADD, MVT::v2f64, Legal);
391     setOperationAction(ISD::FNEG, MVT::v2f64, Legal);
392     setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
393     setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
394     setOperationAction(ISD::FMA, MVT::v2f64, Legal);
395     setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
396     setOperationAction(ISD::FABS, MVT::v2f64, Legal);
397     setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
398     setOperationAction(ISD::FRINT, MVT::v2f64, Legal);
399     setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
400     setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
401     setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
402     setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
403     setOperationAction(ISD::FROUND, MVT::v2f64, Legal);
404   }
405 
406   // We have fused multiply-addition for f32 and f64 but not f128.
407   setOperationAction(ISD::FMA, MVT::f32,  Legal);
408   setOperationAction(ISD::FMA, MVT::f64,  Legal);
409   setOperationAction(ISD::FMA, MVT::f128, Expand);
410 
411   // Needed so that we don't try to implement f128 constant loads using
412   // a load-and-extend of a f80 constant (in cases where the constant
413   // would fit in an f80).
414   for (MVT VT : MVT::fp_valuetypes())
415     setLoadExtAction(ISD::EXTLOAD, VT, MVT::f80, Expand);
416 
417   // Floating-point truncation and stores need to be done separately.
418   setTruncStoreAction(MVT::f64,  MVT::f32, Expand);
419   setTruncStoreAction(MVT::f128, MVT::f32, Expand);
420   setTruncStoreAction(MVT::f128, MVT::f64, Expand);
421 
422   // We have 64-bit FPR<->GPR moves, but need special handling for
423   // 32-bit forms.
424   if (!Subtarget.hasVector()) {
425     setOperationAction(ISD::BITCAST, MVT::i32, Custom);
426     setOperationAction(ISD::BITCAST, MVT::f32, Custom);
427   }
428 
429   // VASTART and VACOPY need to deal with the SystemZ-specific varargs
430   // structure, but VAEND is a no-op.
431   setOperationAction(ISD::VASTART, MVT::Other, Custom);
432   setOperationAction(ISD::VACOPY,  MVT::Other, Custom);
433   setOperationAction(ISD::VAEND,   MVT::Other, Expand);
434 
435   // Codes for which we want to perform some z-specific combinations.
436   setTargetDAGCombine(ISD::SIGN_EXTEND);
437   setTargetDAGCombine(ISD::STORE);
438   setTargetDAGCombine(ISD::EXTRACT_VECTOR_ELT);
439   setTargetDAGCombine(ISD::FP_ROUND);
440 
441   // Handle intrinsics.
442   setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
443   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
444 
445   // We want to use MVC in preference to even a single load/store pair.
446   MaxStoresPerMemcpy = 0;
447   MaxStoresPerMemcpyOptSize = 0;
448 
449   // The main memset sequence is a byte store followed by an MVC.
450   // Two STC or MV..I stores win over that, but the kind of fused stores
451   // generated by target-independent code don't when the byte value is
452   // variable.  E.g.  "STC <reg>;MHI <reg>,257;STH <reg>" is not better
453   // than "STC;MVC".  Handle the choice in target-specific code instead.
454   MaxStoresPerMemset = 0;
455   MaxStoresPerMemsetOptSize = 0;
456 }
457 
458 EVT SystemZTargetLowering::getSetCCResultType(const DataLayout &DL,
459                                               LLVMContext &, EVT VT) const {
460   if (!VT.isVector())
461     return MVT::i32;
462   return VT.changeVectorElementTypeToInteger();
463 }
464 
465 bool SystemZTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
466   VT = VT.getScalarType();
467 
468   if (!VT.isSimple())
469     return false;
470 
471   switch (VT.getSimpleVT().SimpleTy) {
472   case MVT::f32:
473   case MVT::f64:
474     return true;
475   case MVT::f128:
476     return false;
477   default:
478     break;
479   }
480 
481   return false;
482 }
483 
484 bool SystemZTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
485   // We can load zero using LZ?R and negative zero using LZ?R;LC?BR.
486   return Imm.isZero() || Imm.isNegZero();
487 }
488 
489 bool SystemZTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
490   // We can use CGFI or CLGFI.
491   return isInt<32>(Imm) || isUInt<32>(Imm);
492 }
493 
494 bool SystemZTargetLowering::isLegalAddImmediate(int64_t Imm) const {
495   // We can use ALGFI or SLGFI.
496   return isUInt<32>(Imm) || isUInt<32>(-Imm);
497 }
498 
499 bool SystemZTargetLowering::allowsMisalignedMemoryAccesses(EVT VT,
500                                                            unsigned,
501                                                            unsigned,
502                                                            bool *Fast) const {
503   // Unaligned accesses should never be slower than the expanded version.
504   // We check specifically for aligned accesses in the few cases where
505   // they are required.
506   if (Fast)
507     *Fast = true;
508   return true;
509 }
510 
511 bool SystemZTargetLowering::isLegalAddressingMode(const DataLayout &DL,
512                                                   const AddrMode &AM, Type *Ty,
513                                                   unsigned AS) const {
514   // Punt on globals for now, although they can be used in limited
515   // RELATIVE LONG cases.
516   if (AM.BaseGV)
517     return false;
518 
519   // Require a 20-bit signed offset.
520   if (!isInt<20>(AM.BaseOffs))
521     return false;
522 
523   // Indexing is OK but no scale factor can be applied.
524   return AM.Scale == 0 || AM.Scale == 1;
525 }
526 
527 bool SystemZTargetLowering::isTruncateFree(Type *FromType, Type *ToType) const {
528   if (!FromType->isIntegerTy() || !ToType->isIntegerTy())
529     return false;
530   unsigned FromBits = FromType->getPrimitiveSizeInBits();
531   unsigned ToBits = ToType->getPrimitiveSizeInBits();
532   return FromBits > ToBits;
533 }
534 
535 bool SystemZTargetLowering::isTruncateFree(EVT FromVT, EVT ToVT) const {
536   if (!FromVT.isInteger() || !ToVT.isInteger())
537     return false;
538   unsigned FromBits = FromVT.getSizeInBits();
539   unsigned ToBits = ToVT.getSizeInBits();
540   return FromBits > ToBits;
541 }
542 
543 //===----------------------------------------------------------------------===//
544 // Inline asm support
545 //===----------------------------------------------------------------------===//
546 
547 TargetLowering::ConstraintType
548 SystemZTargetLowering::getConstraintType(StringRef Constraint) const {
549   if (Constraint.size() == 1) {
550     switch (Constraint[0]) {
551     case 'a': // Address register
552     case 'd': // Data register (equivalent to 'r')
553     case 'f': // Floating-point register
554     case 'h': // High-part register
555     case 'r': // General-purpose register
556       return C_RegisterClass;
557 
558     case 'Q': // Memory with base and unsigned 12-bit displacement
559     case 'R': // Likewise, plus an index
560     case 'S': // Memory with base and signed 20-bit displacement
561     case 'T': // Likewise, plus an index
562     case 'm': // Equivalent to 'T'.
563       return C_Memory;
564 
565     case 'I': // Unsigned 8-bit constant
566     case 'J': // Unsigned 12-bit constant
567     case 'K': // Signed 16-bit constant
568     case 'L': // Signed 20-bit displacement (on all targets we support)
569     case 'M': // 0x7fffffff
570       return C_Other;
571 
572     default:
573       break;
574     }
575   }
576   return TargetLowering::getConstraintType(Constraint);
577 }
578 
579 TargetLowering::ConstraintWeight SystemZTargetLowering::
580 getSingleConstraintMatchWeight(AsmOperandInfo &info,
581                                const char *constraint) const {
582   ConstraintWeight weight = CW_Invalid;
583   Value *CallOperandVal = info.CallOperandVal;
584   // If we don't have a value, we can't do a match,
585   // but allow it at the lowest weight.
586   if (!CallOperandVal)
587     return CW_Default;
588   Type *type = CallOperandVal->getType();
589   // Look at the constraint type.
590   switch (*constraint) {
591   default:
592     weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
593     break;
594 
595   case 'a': // Address register
596   case 'd': // Data register (equivalent to 'r')
597   case 'h': // High-part register
598   case 'r': // General-purpose register
599     if (CallOperandVal->getType()->isIntegerTy())
600       weight = CW_Register;
601     break;
602 
603   case 'f': // Floating-point register
604     if (type->isFloatingPointTy())
605       weight = CW_Register;
606     break;
607 
608   case 'I': // Unsigned 8-bit constant
609     if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
610       if (isUInt<8>(C->getZExtValue()))
611         weight = CW_Constant;
612     break;
613 
614   case 'J': // Unsigned 12-bit constant
615     if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
616       if (isUInt<12>(C->getZExtValue()))
617         weight = CW_Constant;
618     break;
619 
620   case 'K': // Signed 16-bit constant
621     if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
622       if (isInt<16>(C->getSExtValue()))
623         weight = CW_Constant;
624     break;
625 
626   case 'L': // Signed 20-bit displacement (on all targets we support)
627     if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
628       if (isInt<20>(C->getSExtValue()))
629         weight = CW_Constant;
630     break;
631 
632   case 'M': // 0x7fffffff
633     if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
634       if (C->getZExtValue() == 0x7fffffff)
635         weight = CW_Constant;
636     break;
637   }
638   return weight;
639 }
640 
641 // Parse a "{tNNN}" register constraint for which the register type "t"
642 // has already been verified.  MC is the class associated with "t" and
643 // Map maps 0-based register numbers to LLVM register numbers.
644 static std::pair<unsigned, const TargetRegisterClass *>
645 parseRegisterNumber(StringRef Constraint, const TargetRegisterClass *RC,
646                     const unsigned *Map) {
647   assert(*(Constraint.end()-1) == '}' && "Missing '}'");
648   if (isdigit(Constraint[2])) {
649     unsigned Index;
650     bool Failed =
651         Constraint.slice(2, Constraint.size() - 1).getAsInteger(10, Index);
652     if (!Failed && Index < 16 && Map[Index])
653       return std::make_pair(Map[Index], RC);
654   }
655   return std::make_pair(0U, nullptr);
656 }
657 
658 std::pair<unsigned, const TargetRegisterClass *>
659 SystemZTargetLowering::getRegForInlineAsmConstraint(
660     const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
661   if (Constraint.size() == 1) {
662     // GCC Constraint Letters
663     switch (Constraint[0]) {
664     default: break;
665     case 'd': // Data register (equivalent to 'r')
666     case 'r': // General-purpose register
667       if (VT == MVT::i64)
668         return std::make_pair(0U, &SystemZ::GR64BitRegClass);
669       else if (VT == MVT::i128)
670         return std::make_pair(0U, &SystemZ::GR128BitRegClass);
671       return std::make_pair(0U, &SystemZ::GR32BitRegClass);
672 
673     case 'a': // Address register
674       if (VT == MVT::i64)
675         return std::make_pair(0U, &SystemZ::ADDR64BitRegClass);
676       else if (VT == MVT::i128)
677         return std::make_pair(0U, &SystemZ::ADDR128BitRegClass);
678       return std::make_pair(0U, &SystemZ::ADDR32BitRegClass);
679 
680     case 'h': // High-part register (an LLVM extension)
681       return std::make_pair(0U, &SystemZ::GRH32BitRegClass);
682 
683     case 'f': // Floating-point register
684       if (VT == MVT::f64)
685         return std::make_pair(0U, &SystemZ::FP64BitRegClass);
686       else if (VT == MVT::f128)
687         return std::make_pair(0U, &SystemZ::FP128BitRegClass);
688       return std::make_pair(0U, &SystemZ::FP32BitRegClass);
689     }
690   }
691   if (Constraint.size() > 0 && Constraint[0] == '{') {
692     // We need to override the default register parsing for GPRs and FPRs
693     // because the interpretation depends on VT.  The internal names of
694     // the registers are also different from the external names
695     // (F0D and F0S instead of F0, etc.).
696     if (Constraint[1] == 'r') {
697       if (VT == MVT::i32)
698         return parseRegisterNumber(Constraint, &SystemZ::GR32BitRegClass,
699                                    SystemZMC::GR32Regs);
700       if (VT == MVT::i128)
701         return parseRegisterNumber(Constraint, &SystemZ::GR128BitRegClass,
702                                    SystemZMC::GR128Regs);
703       return parseRegisterNumber(Constraint, &SystemZ::GR64BitRegClass,
704                                  SystemZMC::GR64Regs);
705     }
706     if (Constraint[1] == 'f') {
707       if (VT == MVT::f32)
708         return parseRegisterNumber(Constraint, &SystemZ::FP32BitRegClass,
709                                    SystemZMC::FP32Regs);
710       if (VT == MVT::f128)
711         return parseRegisterNumber(Constraint, &SystemZ::FP128BitRegClass,
712                                    SystemZMC::FP128Regs);
713       return parseRegisterNumber(Constraint, &SystemZ::FP64BitRegClass,
714                                  SystemZMC::FP64Regs);
715     }
716   }
717   return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
718 }
719 
720 void SystemZTargetLowering::
721 LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint,
722                              std::vector<SDValue> &Ops,
723                              SelectionDAG &DAG) const {
724   // Only support length 1 constraints for now.
725   if (Constraint.length() == 1) {
726     switch (Constraint[0]) {
727     case 'I': // Unsigned 8-bit constant
728       if (auto *C = dyn_cast<ConstantSDNode>(Op))
729         if (isUInt<8>(C->getZExtValue()))
730           Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
731                                               Op.getValueType()));
732       return;
733 
734     case 'J': // Unsigned 12-bit constant
735       if (auto *C = dyn_cast<ConstantSDNode>(Op))
736         if (isUInt<12>(C->getZExtValue()))
737           Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
738                                               Op.getValueType()));
739       return;
740 
741     case 'K': // Signed 16-bit constant
742       if (auto *C = dyn_cast<ConstantSDNode>(Op))
743         if (isInt<16>(C->getSExtValue()))
744           Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
745                                               Op.getValueType()));
746       return;
747 
748     case 'L': // Signed 20-bit displacement (on all targets we support)
749       if (auto *C = dyn_cast<ConstantSDNode>(Op))
750         if (isInt<20>(C->getSExtValue()))
751           Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
752                                               Op.getValueType()));
753       return;
754 
755     case 'M': // 0x7fffffff
756       if (auto *C = dyn_cast<ConstantSDNode>(Op))
757         if (C->getZExtValue() == 0x7fffffff)
758           Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
759                                               Op.getValueType()));
760       return;
761     }
762   }
763   TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
764 }
765 
766 //===----------------------------------------------------------------------===//
767 // Calling conventions
768 //===----------------------------------------------------------------------===//
769 
770 #include "SystemZGenCallingConv.inc"
771 
772 bool SystemZTargetLowering::allowTruncateForTailCall(Type *FromType,
773                                                      Type *ToType) const {
774   return isTruncateFree(FromType, ToType);
775 }
776 
777 bool SystemZTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
778   return CI->isTailCall();
779 }
780 
781 // We do not yet support 128-bit single-element vector types.  If the user
782 // attempts to use such types as function argument or return type, prefer
783 // to error out instead of emitting code violating the ABI.
784 static void VerifyVectorType(MVT VT, EVT ArgVT) {
785   if (ArgVT.isVector() && !VT.isVector())
786     report_fatal_error("Unsupported vector argument or return type");
787 }
788 
789 static void VerifyVectorTypes(const SmallVectorImpl<ISD::InputArg> &Ins) {
790   for (unsigned i = 0; i < Ins.size(); ++i)
791     VerifyVectorType(Ins[i].VT, Ins[i].ArgVT);
792 }
793 
794 static void VerifyVectorTypes(const SmallVectorImpl<ISD::OutputArg> &Outs) {
795   for (unsigned i = 0; i < Outs.size(); ++i)
796     VerifyVectorType(Outs[i].VT, Outs[i].ArgVT);
797 }
798 
799 // Value is a value that has been passed to us in the location described by VA
800 // (and so has type VA.getLocVT()).  Convert Value to VA.getValVT(), chaining
801 // any loads onto Chain.
802 static SDValue convertLocVTToValVT(SelectionDAG &DAG, SDLoc DL,
803                                    CCValAssign &VA, SDValue Chain,
804                                    SDValue Value) {
805   // If the argument has been promoted from a smaller type, insert an
806   // assertion to capture this.
807   if (VA.getLocInfo() == CCValAssign::SExt)
808     Value = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Value,
809                         DAG.getValueType(VA.getValVT()));
810   else if (VA.getLocInfo() == CCValAssign::ZExt)
811     Value = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Value,
812                         DAG.getValueType(VA.getValVT()));
813 
814   if (VA.isExtInLoc())
815     Value = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Value);
816   else if (VA.getLocInfo() == CCValAssign::BCvt) {
817     // If this is a short vector argument loaded from the stack,
818     // extend from i64 to full vector size and then bitcast.
819     assert(VA.getLocVT() == MVT::i64);
820     assert(VA.getValVT().isVector());
821     Value = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i64,
822                         Value, DAG.getUNDEF(MVT::i64));
823     Value = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Value);
824   } else
825     assert(VA.getLocInfo() == CCValAssign::Full && "Unsupported getLocInfo");
826   return Value;
827 }
828 
829 // Value is a value of type VA.getValVT() that we need to copy into
830 // the location described by VA.  Return a copy of Value converted to
831 // VA.getValVT().  The caller is responsible for handling indirect values.
832 static SDValue convertValVTToLocVT(SelectionDAG &DAG, SDLoc DL,
833                                    CCValAssign &VA, SDValue Value) {
834   switch (VA.getLocInfo()) {
835   case CCValAssign::SExt:
836     return DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Value);
837   case CCValAssign::ZExt:
838     return DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Value);
839   case CCValAssign::AExt:
840     return DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Value);
841   case CCValAssign::BCvt:
842     // If this is a short vector argument to be stored to the stack,
843     // bitcast to v2i64 and then extract first element.
844     assert(VA.getLocVT() == MVT::i64);
845     assert(VA.getValVT().isVector());
846     Value = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Value);
847     return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VA.getLocVT(), Value,
848                        DAG.getConstant(0, DL, MVT::i32));
849   case CCValAssign::Full:
850     return Value;
851   default:
852     llvm_unreachable("Unhandled getLocInfo()");
853   }
854 }
855 
856 SDValue SystemZTargetLowering::
857 LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
858                      const SmallVectorImpl<ISD::InputArg> &Ins,
859                      SDLoc DL, SelectionDAG &DAG,
860                      SmallVectorImpl<SDValue> &InVals) const {
861   MachineFunction &MF = DAG.getMachineFunction();
862   MachineFrameInfo *MFI = MF.getFrameInfo();
863   MachineRegisterInfo &MRI = MF.getRegInfo();
864   SystemZMachineFunctionInfo *FuncInfo =
865       MF.getInfo<SystemZMachineFunctionInfo>();
866   auto *TFL =
867       static_cast<const SystemZFrameLowering *>(Subtarget.getFrameLowering());
868   EVT PtrVT = getPointerTy(DAG.getDataLayout());
869 
870   // Detect unsupported vector argument types.
871   if (Subtarget.hasVector())
872     VerifyVectorTypes(Ins);
873 
874   // Assign locations to all of the incoming arguments.
875   SmallVector<CCValAssign, 16> ArgLocs;
876   SystemZCCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
877   CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
878 
879   unsigned NumFixedGPRs = 0;
880   unsigned NumFixedFPRs = 0;
881   for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
882     SDValue ArgValue;
883     CCValAssign &VA = ArgLocs[I];
884     EVT LocVT = VA.getLocVT();
885     if (VA.isRegLoc()) {
886       // Arguments passed in registers
887       const TargetRegisterClass *RC;
888       switch (LocVT.getSimpleVT().SimpleTy) {
889       default:
890         // Integers smaller than i64 should be promoted to i64.
891         llvm_unreachable("Unexpected argument type");
892       case MVT::i32:
893         NumFixedGPRs += 1;
894         RC = &SystemZ::GR32BitRegClass;
895         break;
896       case MVT::i64:
897         NumFixedGPRs += 1;
898         RC = &SystemZ::GR64BitRegClass;
899         break;
900       case MVT::f32:
901         NumFixedFPRs += 1;
902         RC = &SystemZ::FP32BitRegClass;
903         break;
904       case MVT::f64:
905         NumFixedFPRs += 1;
906         RC = &SystemZ::FP64BitRegClass;
907         break;
908       case MVT::v16i8:
909       case MVT::v8i16:
910       case MVT::v4i32:
911       case MVT::v2i64:
912       case MVT::v4f32:
913       case MVT::v2f64:
914         RC = &SystemZ::VR128BitRegClass;
915         break;
916       }
917 
918       unsigned VReg = MRI.createVirtualRegister(RC);
919       MRI.addLiveIn(VA.getLocReg(), VReg);
920       ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
921     } else {
922       assert(VA.isMemLoc() && "Argument not register or memory");
923 
924       // Create the frame index object for this incoming parameter.
925       int FI = MFI->CreateFixedObject(LocVT.getSizeInBits() / 8,
926                                       VA.getLocMemOffset(), true);
927 
928       // Create the SelectionDAG nodes corresponding to a load
929       // from this parameter.  Unpromoted ints and floats are
930       // passed as right-justified 8-byte values.
931       SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
932       if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
933         FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN,
934                           DAG.getIntPtrConstant(4, DL));
935       ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
936                              MachinePointerInfo::getFixedStack(MF, FI), false,
937                              false, false, 0);
938     }
939 
940     // Convert the value of the argument register into the value that's
941     // being passed.
942     if (VA.getLocInfo() == CCValAssign::Indirect) {
943       InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain,
944                                    ArgValue, MachinePointerInfo(),
945                                    false, false, false, 0));
946       // If the original argument was split (e.g. i128), we need
947       // to load all parts of it here (using the same address).
948       unsigned ArgIndex = Ins[I].OrigArgIndex;
949       assert (Ins[I].PartOffset == 0);
950       while (I + 1 != E && Ins[I + 1].OrigArgIndex == ArgIndex) {
951         CCValAssign &PartVA = ArgLocs[I + 1];
952         unsigned PartOffset = Ins[I + 1].PartOffset;
953         SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, ArgValue,
954                                       DAG.getIntPtrConstant(PartOffset, DL));
955         InVals.push_back(DAG.getLoad(PartVA.getValVT(), DL, Chain,
956                                      Address, MachinePointerInfo(),
957                                      false, false, false, 0));
958         ++I;
959       }
960     } else
961       InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, ArgValue));
962   }
963 
964   if (IsVarArg) {
965     // Save the number of non-varargs registers for later use by va_start, etc.
966     FuncInfo->setVarArgsFirstGPR(NumFixedGPRs);
967     FuncInfo->setVarArgsFirstFPR(NumFixedFPRs);
968 
969     // Likewise the address (in the form of a frame index) of where the
970     // first stack vararg would be.  The 1-byte size here is arbitrary.
971     int64_t StackSize = CCInfo.getNextStackOffset();
972     FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize, true));
973 
974     // ...and a similar frame index for the caller-allocated save area
975     // that will be used to store the incoming registers.
976     int64_t RegSaveOffset = TFL->getOffsetOfLocalArea();
977     unsigned RegSaveIndex = MFI->CreateFixedObject(1, RegSaveOffset, true);
978     FuncInfo->setRegSaveFrameIndex(RegSaveIndex);
979 
980     // Store the FPR varargs in the reserved frame slots.  (We store the
981     // GPRs as part of the prologue.)
982     if (NumFixedFPRs < SystemZ::NumArgFPRs) {
983       SDValue MemOps[SystemZ::NumArgFPRs];
984       for (unsigned I = NumFixedFPRs; I < SystemZ::NumArgFPRs; ++I) {
985         unsigned Offset = TFL->getRegSpillOffset(SystemZ::ArgFPRs[I]);
986         int FI = MFI->CreateFixedObject(8, RegSaveOffset + Offset, true);
987         SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
988         unsigned VReg = MF.addLiveIn(SystemZ::ArgFPRs[I],
989                                      &SystemZ::FP64BitRegClass);
990         SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f64);
991         MemOps[I] = DAG.getStore(ArgValue.getValue(1), DL, ArgValue, FIN,
992                                  MachinePointerInfo::getFixedStack(MF, FI),
993                                  false, false, 0);
994       }
995       // Join the stores, which are independent of one another.
996       Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
997                           makeArrayRef(&MemOps[NumFixedFPRs],
998                                        SystemZ::NumArgFPRs-NumFixedFPRs));
999     }
1000   }
1001 
1002   return Chain;
1003 }
1004 
1005 static bool canUseSiblingCall(const CCState &ArgCCInfo,
1006                               SmallVectorImpl<CCValAssign> &ArgLocs) {
1007   // Punt if there are any indirect or stack arguments, or if the call
1008   // needs the call-saved argument register R6.
1009   for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1010     CCValAssign &VA = ArgLocs[I];
1011     if (VA.getLocInfo() == CCValAssign::Indirect)
1012       return false;
1013     if (!VA.isRegLoc())
1014       return false;
1015     unsigned Reg = VA.getLocReg();
1016     if (Reg == SystemZ::R6H || Reg == SystemZ::R6L || Reg == SystemZ::R6D)
1017       return false;
1018   }
1019   return true;
1020 }
1021 
1022 SDValue
1023 SystemZTargetLowering::LowerCall(CallLoweringInfo &CLI,
1024                                  SmallVectorImpl<SDValue> &InVals) const {
1025   SelectionDAG &DAG = CLI.DAG;
1026   SDLoc &DL = CLI.DL;
1027   SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
1028   SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
1029   SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
1030   SDValue Chain = CLI.Chain;
1031   SDValue Callee = CLI.Callee;
1032   bool &IsTailCall = CLI.IsTailCall;
1033   CallingConv::ID CallConv = CLI.CallConv;
1034   bool IsVarArg = CLI.IsVarArg;
1035   MachineFunction &MF = DAG.getMachineFunction();
1036   EVT PtrVT = getPointerTy(MF.getDataLayout());
1037 
1038   // Detect unsupported vector argument and return types.
1039   if (Subtarget.hasVector()) {
1040     VerifyVectorTypes(Outs);
1041     VerifyVectorTypes(Ins);
1042   }
1043 
1044   // Analyze the operands of the call, assigning locations to each operand.
1045   SmallVector<CCValAssign, 16> ArgLocs;
1046   SystemZCCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
1047   ArgCCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
1048 
1049   // We don't support GuaranteedTailCallOpt, only automatically-detected
1050   // sibling calls.
1051   if (IsTailCall && !canUseSiblingCall(ArgCCInfo, ArgLocs))
1052     IsTailCall = false;
1053 
1054   // Get a count of how many bytes are to be pushed on the stack.
1055   unsigned NumBytes = ArgCCInfo.getNextStackOffset();
1056 
1057   // Mark the start of the call.
1058   if (!IsTailCall)
1059     Chain = DAG.getCALLSEQ_START(Chain,
1060                                  DAG.getConstant(NumBytes, DL, PtrVT, true),
1061                                  DL);
1062 
1063   // Copy argument values to their designated locations.
1064   SmallVector<std::pair<unsigned, SDValue>, 9> RegsToPass;
1065   SmallVector<SDValue, 8> MemOpChains;
1066   SDValue StackPtr;
1067   for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
1068     CCValAssign &VA = ArgLocs[I];
1069     SDValue ArgValue = OutVals[I];
1070 
1071     if (VA.getLocInfo() == CCValAssign::Indirect) {
1072       // Store the argument in a stack slot and pass its address.
1073       SDValue SpillSlot = DAG.CreateStackTemporary(Outs[I].ArgVT);
1074       int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
1075       MemOpChains.push_back(DAG.getStore(
1076           Chain, DL, ArgValue, SpillSlot,
1077           MachinePointerInfo::getFixedStack(MF, FI), false, false, 0));
1078       // If the original argument was split (e.g. i128), we need
1079       // to store all parts of it here (and pass just one address).
1080       unsigned ArgIndex = Outs[I].OrigArgIndex;
1081       assert (Outs[I].PartOffset == 0);
1082       while (I + 1 != E && Outs[I + 1].OrigArgIndex == ArgIndex) {
1083         SDValue PartValue = OutVals[I + 1];
1084         unsigned PartOffset = Outs[I + 1].PartOffset;
1085         SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, SpillSlot,
1086                                       DAG.getIntPtrConstant(PartOffset, DL));
1087         MemOpChains.push_back(DAG.getStore(
1088             Chain, DL, PartValue, Address,
1089             MachinePointerInfo::getFixedStack(MF, FI), false, false, 0));
1090         ++I;
1091       }
1092       ArgValue = SpillSlot;
1093     } else
1094       ArgValue = convertValVTToLocVT(DAG, DL, VA, ArgValue);
1095 
1096     if (VA.isRegLoc())
1097       // Queue up the argument copies and emit them at the end.
1098       RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
1099     else {
1100       assert(VA.isMemLoc() && "Argument not register or memory");
1101 
1102       // Work out the address of the stack slot.  Unpromoted ints and
1103       // floats are passed as right-justified 8-byte values.
1104       if (!StackPtr.getNode())
1105         StackPtr = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, PtrVT);
1106       unsigned Offset = SystemZMC::CallFrameSize + VA.getLocMemOffset();
1107       if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
1108         Offset += 4;
1109       SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
1110                                     DAG.getIntPtrConstant(Offset, DL));
1111 
1112       // Emit the store.
1113       MemOpChains.push_back(DAG.getStore(Chain, DL, ArgValue, Address,
1114                                          MachinePointerInfo(),
1115                                          false, false, 0));
1116     }
1117   }
1118 
1119   // Join the stores, which are independent of one another.
1120   if (!MemOpChains.empty())
1121     Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
1122 
1123   // Accept direct calls by converting symbolic call addresses to the
1124   // associated Target* opcodes.  Force %r1 to be used for indirect
1125   // tail calls.
1126   SDValue Glue;
1127   if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1128     Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT);
1129     Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
1130   } else if (auto *E = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1131     Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT);
1132     Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
1133   } else if (IsTailCall) {
1134     Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R1D, Callee, Glue);
1135     Glue = Chain.getValue(1);
1136     Callee = DAG.getRegister(SystemZ::R1D, Callee.getValueType());
1137   }
1138 
1139   // Build a sequence of copy-to-reg nodes, chained and glued together.
1140   for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) {
1141     Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[I].first,
1142                              RegsToPass[I].second, Glue);
1143     Glue = Chain.getValue(1);
1144   }
1145 
1146   // The first call operand is the chain and the second is the target address.
1147   SmallVector<SDValue, 8> Ops;
1148   Ops.push_back(Chain);
1149   Ops.push_back(Callee);
1150 
1151   // Add argument registers to the end of the list so that they are
1152   // known live into the call.
1153   for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I)
1154     Ops.push_back(DAG.getRegister(RegsToPass[I].first,
1155                                   RegsToPass[I].second.getValueType()));
1156 
1157   // Add a register mask operand representing the call-preserved registers.
1158   const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
1159   const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv);
1160   assert(Mask && "Missing call preserved mask for calling convention");
1161   Ops.push_back(DAG.getRegisterMask(Mask));
1162 
1163   // Glue the call to the argument copies, if any.
1164   if (Glue.getNode())
1165     Ops.push_back(Glue);
1166 
1167   // Emit the call.
1168   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
1169   if (IsTailCall)
1170     return DAG.getNode(SystemZISD::SIBCALL, DL, NodeTys, Ops);
1171   Chain = DAG.getNode(SystemZISD::CALL, DL, NodeTys, Ops);
1172   Glue = Chain.getValue(1);
1173 
1174   // Mark the end of the call, which is glued to the call itself.
1175   Chain = DAG.getCALLSEQ_END(Chain,
1176                              DAG.getConstant(NumBytes, DL, PtrVT, true),
1177                              DAG.getConstant(0, DL, PtrVT, true),
1178                              Glue, DL);
1179   Glue = Chain.getValue(1);
1180 
1181   // Assign locations to each value returned by this call.
1182   SmallVector<CCValAssign, 16> RetLocs;
1183   CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
1184   RetCCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
1185 
1186   // Copy all of the result registers out of their specified physreg.
1187   for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
1188     CCValAssign &VA = RetLocs[I];
1189 
1190     // Copy the value out, gluing the copy to the end of the call sequence.
1191     SDValue RetValue = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(),
1192                                           VA.getLocVT(), Glue);
1193     Chain = RetValue.getValue(1);
1194     Glue = RetValue.getValue(2);
1195 
1196     // Convert the value of the return register into the value that's
1197     // being returned.
1198     InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, RetValue));
1199   }
1200 
1201   return Chain;
1202 }
1203 
1204 bool SystemZTargetLowering::
1205 CanLowerReturn(CallingConv::ID CallConv,
1206                MachineFunction &MF, bool isVarArg,
1207                const SmallVectorImpl<ISD::OutputArg> &Outs,
1208                LLVMContext &Context) const {
1209   // Detect unsupported vector return types.
1210   if (Subtarget.hasVector())
1211     VerifyVectorTypes(Outs);
1212 
1213   // Special case that we cannot easily detect in RetCC_SystemZ since
1214   // i128 is not a legal type.
1215   for (auto &Out : Outs)
1216     if (Out.ArgVT == MVT::i128)
1217       return false;
1218 
1219   SmallVector<CCValAssign, 16> RetLocs;
1220   CCState RetCCInfo(CallConv, isVarArg, MF, RetLocs, Context);
1221   return RetCCInfo.CheckReturn(Outs, RetCC_SystemZ);
1222 }
1223 
1224 SDValue
1225 SystemZTargetLowering::LowerReturn(SDValue Chain,
1226                                    CallingConv::ID CallConv, bool IsVarArg,
1227                                    const SmallVectorImpl<ISD::OutputArg> &Outs,
1228                                    const SmallVectorImpl<SDValue> &OutVals,
1229                                    SDLoc DL, SelectionDAG &DAG) const {
1230   MachineFunction &MF = DAG.getMachineFunction();
1231 
1232   // Detect unsupported vector return types.
1233   if (Subtarget.hasVector())
1234     VerifyVectorTypes(Outs);
1235 
1236   // Assign locations to each returned value.
1237   SmallVector<CCValAssign, 16> RetLocs;
1238   CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
1239   RetCCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
1240 
1241   // Quick exit for void returns
1242   if (RetLocs.empty())
1243     return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, Chain);
1244 
1245   // Copy the result values into the output registers.
1246   SDValue Glue;
1247   SmallVector<SDValue, 4> RetOps;
1248   RetOps.push_back(Chain);
1249   for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
1250     CCValAssign &VA = RetLocs[I];
1251     SDValue RetValue = OutVals[I];
1252 
1253     // Make the return register live on exit.
1254     assert(VA.isRegLoc() && "Can only return in registers!");
1255 
1256     // Promote the value as required.
1257     RetValue = convertValVTToLocVT(DAG, DL, VA, RetValue);
1258 
1259     // Chain and glue the copies together.
1260     unsigned Reg = VA.getLocReg();
1261     Chain = DAG.getCopyToReg(Chain, DL, Reg, RetValue, Glue);
1262     Glue = Chain.getValue(1);
1263     RetOps.push_back(DAG.getRegister(Reg, VA.getLocVT()));
1264   }
1265 
1266   // Update chain and glue.
1267   RetOps[0] = Chain;
1268   if (Glue.getNode())
1269     RetOps.push_back(Glue);
1270 
1271   return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, RetOps);
1272 }
1273 
1274 SDValue SystemZTargetLowering::
1275 prepareVolatileOrAtomicLoad(SDValue Chain, SDLoc DL, SelectionDAG &DAG) const {
1276   return DAG.getNode(SystemZISD::SERIALIZE, DL, MVT::Other, Chain);
1277 }
1278 
1279 // Return true if Op is an intrinsic node with chain that returns the CC value
1280 // as its only (other) argument.  Provide the associated SystemZISD opcode and
1281 // the mask of valid CC values if so.
1282 static bool isIntrinsicWithCCAndChain(SDValue Op, unsigned &Opcode,
1283                                       unsigned &CCValid) {
1284   unsigned Id = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
1285   switch (Id) {
1286   case Intrinsic::s390_tbegin:
1287     Opcode = SystemZISD::TBEGIN;
1288     CCValid = SystemZ::CCMASK_TBEGIN;
1289     return true;
1290 
1291   case Intrinsic::s390_tbegin_nofloat:
1292     Opcode = SystemZISD::TBEGIN_NOFLOAT;
1293     CCValid = SystemZ::CCMASK_TBEGIN;
1294     return true;
1295 
1296   case Intrinsic::s390_tend:
1297     Opcode = SystemZISD::TEND;
1298     CCValid = SystemZ::CCMASK_TEND;
1299     return true;
1300 
1301   default:
1302     return false;
1303   }
1304 }
1305 
1306 // Return true if Op is an intrinsic node without chain that returns the
1307 // CC value as its final argument.  Provide the associated SystemZISD
1308 // opcode and the mask of valid CC values if so.
1309 static bool isIntrinsicWithCC(SDValue Op, unsigned &Opcode, unsigned &CCValid) {
1310   unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
1311   switch (Id) {
1312   case Intrinsic::s390_vpkshs:
1313   case Intrinsic::s390_vpksfs:
1314   case Intrinsic::s390_vpksgs:
1315     Opcode = SystemZISD::PACKS_CC;
1316     CCValid = SystemZ::CCMASK_VCMP;
1317     return true;
1318 
1319   case Intrinsic::s390_vpklshs:
1320   case Intrinsic::s390_vpklsfs:
1321   case Intrinsic::s390_vpklsgs:
1322     Opcode = SystemZISD::PACKLS_CC;
1323     CCValid = SystemZ::CCMASK_VCMP;
1324     return true;
1325 
1326   case Intrinsic::s390_vceqbs:
1327   case Intrinsic::s390_vceqhs:
1328   case Intrinsic::s390_vceqfs:
1329   case Intrinsic::s390_vceqgs:
1330     Opcode = SystemZISD::VICMPES;
1331     CCValid = SystemZ::CCMASK_VCMP;
1332     return true;
1333 
1334   case Intrinsic::s390_vchbs:
1335   case Intrinsic::s390_vchhs:
1336   case Intrinsic::s390_vchfs:
1337   case Intrinsic::s390_vchgs:
1338     Opcode = SystemZISD::VICMPHS;
1339     CCValid = SystemZ::CCMASK_VCMP;
1340     return true;
1341 
1342   case Intrinsic::s390_vchlbs:
1343   case Intrinsic::s390_vchlhs:
1344   case Intrinsic::s390_vchlfs:
1345   case Intrinsic::s390_vchlgs:
1346     Opcode = SystemZISD::VICMPHLS;
1347     CCValid = SystemZ::CCMASK_VCMP;
1348     return true;
1349 
1350   case Intrinsic::s390_vtm:
1351     Opcode = SystemZISD::VTM;
1352     CCValid = SystemZ::CCMASK_VCMP;
1353     return true;
1354 
1355   case Intrinsic::s390_vfaebs:
1356   case Intrinsic::s390_vfaehs:
1357   case Intrinsic::s390_vfaefs:
1358     Opcode = SystemZISD::VFAE_CC;
1359     CCValid = SystemZ::CCMASK_ANY;
1360     return true;
1361 
1362   case Intrinsic::s390_vfaezbs:
1363   case Intrinsic::s390_vfaezhs:
1364   case Intrinsic::s390_vfaezfs:
1365     Opcode = SystemZISD::VFAEZ_CC;
1366     CCValid = SystemZ::CCMASK_ANY;
1367     return true;
1368 
1369   case Intrinsic::s390_vfeebs:
1370   case Intrinsic::s390_vfeehs:
1371   case Intrinsic::s390_vfeefs:
1372     Opcode = SystemZISD::VFEE_CC;
1373     CCValid = SystemZ::CCMASK_ANY;
1374     return true;
1375 
1376   case Intrinsic::s390_vfeezbs:
1377   case Intrinsic::s390_vfeezhs:
1378   case Intrinsic::s390_vfeezfs:
1379     Opcode = SystemZISD::VFEEZ_CC;
1380     CCValid = SystemZ::CCMASK_ANY;
1381     return true;
1382 
1383   case Intrinsic::s390_vfenebs:
1384   case Intrinsic::s390_vfenehs:
1385   case Intrinsic::s390_vfenefs:
1386     Opcode = SystemZISD::VFENE_CC;
1387     CCValid = SystemZ::CCMASK_ANY;
1388     return true;
1389 
1390   case Intrinsic::s390_vfenezbs:
1391   case Intrinsic::s390_vfenezhs:
1392   case Intrinsic::s390_vfenezfs:
1393     Opcode = SystemZISD::VFENEZ_CC;
1394     CCValid = SystemZ::CCMASK_ANY;
1395     return true;
1396 
1397   case Intrinsic::s390_vistrbs:
1398   case Intrinsic::s390_vistrhs:
1399   case Intrinsic::s390_vistrfs:
1400     Opcode = SystemZISD::VISTR_CC;
1401     CCValid = SystemZ::CCMASK_0 | SystemZ::CCMASK_3;
1402     return true;
1403 
1404   case Intrinsic::s390_vstrcbs:
1405   case Intrinsic::s390_vstrchs:
1406   case Intrinsic::s390_vstrcfs:
1407     Opcode = SystemZISD::VSTRC_CC;
1408     CCValid = SystemZ::CCMASK_ANY;
1409     return true;
1410 
1411   case Intrinsic::s390_vstrczbs:
1412   case Intrinsic::s390_vstrczhs:
1413   case Intrinsic::s390_vstrczfs:
1414     Opcode = SystemZISD::VSTRCZ_CC;
1415     CCValid = SystemZ::CCMASK_ANY;
1416     return true;
1417 
1418   case Intrinsic::s390_vfcedbs:
1419     Opcode = SystemZISD::VFCMPES;
1420     CCValid = SystemZ::CCMASK_VCMP;
1421     return true;
1422 
1423   case Intrinsic::s390_vfchdbs:
1424     Opcode = SystemZISD::VFCMPHS;
1425     CCValid = SystemZ::CCMASK_VCMP;
1426     return true;
1427 
1428   case Intrinsic::s390_vfchedbs:
1429     Opcode = SystemZISD::VFCMPHES;
1430     CCValid = SystemZ::CCMASK_VCMP;
1431     return true;
1432 
1433   case Intrinsic::s390_vftcidb:
1434     Opcode = SystemZISD::VFTCI;
1435     CCValid = SystemZ::CCMASK_VCMP;
1436     return true;
1437 
1438   default:
1439     return false;
1440   }
1441 }
1442 
1443 // Emit an intrinsic with chain with a glued value instead of its CC result.
1444 static SDValue emitIntrinsicWithChainAndGlue(SelectionDAG &DAG, SDValue Op,
1445                                              unsigned Opcode) {
1446   // Copy all operands except the intrinsic ID.
1447   unsigned NumOps = Op.getNumOperands();
1448   SmallVector<SDValue, 6> Ops;
1449   Ops.reserve(NumOps - 1);
1450   Ops.push_back(Op.getOperand(0));
1451   for (unsigned I = 2; I < NumOps; ++I)
1452     Ops.push_back(Op.getOperand(I));
1453 
1454   assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
1455   SDVTList RawVTs = DAG.getVTList(MVT::Other, MVT::Glue);
1456   SDValue Intr = DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops);
1457   SDValue OldChain = SDValue(Op.getNode(), 1);
1458   SDValue NewChain = SDValue(Intr.getNode(), 0);
1459   DAG.ReplaceAllUsesOfValueWith(OldChain, NewChain);
1460   return Intr;
1461 }
1462 
1463 // Emit an intrinsic with a glued value instead of its CC result.
1464 static SDValue emitIntrinsicWithGlue(SelectionDAG &DAG, SDValue Op,
1465                                      unsigned Opcode) {
1466   // Copy all operands except the intrinsic ID.
1467   unsigned NumOps = Op.getNumOperands();
1468   SmallVector<SDValue, 6> Ops;
1469   Ops.reserve(NumOps - 1);
1470   for (unsigned I = 1; I < NumOps; ++I)
1471     Ops.push_back(Op.getOperand(I));
1472 
1473   if (Op->getNumValues() == 1)
1474     return DAG.getNode(Opcode, SDLoc(Op), MVT::Glue, Ops);
1475   assert(Op->getNumValues() == 2 && "Expected exactly one non-CC result");
1476   SDVTList RawVTs = DAG.getVTList(Op->getValueType(0), MVT::Glue);
1477   return DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops);
1478 }
1479 
1480 // CC is a comparison that will be implemented using an integer or
1481 // floating-point comparison.  Return the condition code mask for
1482 // a branch on true.  In the integer case, CCMASK_CMP_UO is set for
1483 // unsigned comparisons and clear for signed ones.  In the floating-point
1484 // case, CCMASK_CMP_UO has its normal mask meaning (unordered).
1485 static unsigned CCMaskForCondCode(ISD::CondCode CC) {
1486 #define CONV(X) \
1487   case ISD::SET##X: return SystemZ::CCMASK_CMP_##X; \
1488   case ISD::SETO##X: return SystemZ::CCMASK_CMP_##X; \
1489   case ISD::SETU##X: return SystemZ::CCMASK_CMP_UO | SystemZ::CCMASK_CMP_##X
1490 
1491   switch (CC) {
1492   default:
1493     llvm_unreachable("Invalid integer condition!");
1494 
1495   CONV(EQ);
1496   CONV(NE);
1497   CONV(GT);
1498   CONV(GE);
1499   CONV(LT);
1500   CONV(LE);
1501 
1502   case ISD::SETO:  return SystemZ::CCMASK_CMP_O;
1503   case ISD::SETUO: return SystemZ::CCMASK_CMP_UO;
1504   }
1505 #undef CONV
1506 }
1507 
1508 // Return a sequence for getting a 1 from an IPM result when CC has a
1509 // value in CCMask and a 0 when CC has a value in CCValid & ~CCMask.
1510 // The handling of CC values outside CCValid doesn't matter.
1511 static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) {
1512   // Deal with cases where the result can be taken directly from a bit
1513   // of the IPM result.
1514   if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3)))
1515     return IPMConversion(0, 0, SystemZ::IPM_CC);
1516   if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3)))
1517     return IPMConversion(0, 0, SystemZ::IPM_CC + 1);
1518 
1519   // Deal with cases where we can add a value to force the sign bit
1520   // to contain the right value.  Putting the bit in 31 means we can
1521   // use SRL rather than RISBG(L), and also makes it easier to get a
1522   // 0/-1 value, so it has priority over the other tests below.
1523   //
1524   // These sequences rely on the fact that the upper two bits of the
1525   // IPM result are zero.
1526   uint64_t TopBit = uint64_t(1) << 31;
1527   if (CCMask == (CCValid & SystemZ::CCMASK_0))
1528     return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31);
1529   if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1)))
1530     return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31);
1531   if (CCMask == (CCValid & (SystemZ::CCMASK_0
1532                             | SystemZ::CCMASK_1
1533                             | SystemZ::CCMASK_2)))
1534     return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31);
1535   if (CCMask == (CCValid & SystemZ::CCMASK_3))
1536     return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31);
1537   if (CCMask == (CCValid & (SystemZ::CCMASK_1
1538                             | SystemZ::CCMASK_2
1539                             | SystemZ::CCMASK_3)))
1540     return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31);
1541 
1542   // Next try inverting the value and testing a bit.  0/1 could be
1543   // handled this way too, but we dealt with that case above.
1544   if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2)))
1545     return IPMConversion(-1, 0, SystemZ::IPM_CC);
1546 
1547   // Handle cases where adding a value forces a non-sign bit to contain
1548   // the right value.
1549   if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2)))
1550     return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1);
1551   if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3)))
1552     return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1);
1553 
1554   // The remaining cases are 1, 2, 0/1/3 and 0/2/3.  All these are
1555   // can be done by inverting the low CC bit and applying one of the
1556   // sign-based extractions above.
1557   if (CCMask == (CCValid & SystemZ::CCMASK_1))
1558     return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31);
1559   if (CCMask == (CCValid & SystemZ::CCMASK_2))
1560     return IPMConversion(1 << SystemZ::IPM_CC,
1561                          TopBit - (3 << SystemZ::IPM_CC), 31);
1562   if (CCMask == (CCValid & (SystemZ::CCMASK_0
1563                             | SystemZ::CCMASK_1
1564                             | SystemZ::CCMASK_3)))
1565     return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31);
1566   if (CCMask == (CCValid & (SystemZ::CCMASK_0
1567                             | SystemZ::CCMASK_2
1568                             | SystemZ::CCMASK_3)))
1569     return IPMConversion(1 << SystemZ::IPM_CC,
1570                          TopBit - (1 << SystemZ::IPM_CC), 31);
1571 
1572   llvm_unreachable("Unexpected CC combination");
1573 }
1574 
1575 // If C can be converted to a comparison against zero, adjust the operands
1576 // as necessary.
1577 static void adjustZeroCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
1578   if (C.ICmpType == SystemZICMP::UnsignedOnly)
1579     return;
1580 
1581   auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1.getNode());
1582   if (!ConstOp1)
1583     return;
1584 
1585   int64_t Value = ConstOp1->getSExtValue();
1586   if ((Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_GT) ||
1587       (Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_LE) ||
1588       (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_LT) ||
1589       (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_GE)) {
1590     C.CCMask ^= SystemZ::CCMASK_CMP_EQ;
1591     C.Op1 = DAG.getConstant(0, DL, C.Op1.getValueType());
1592   }
1593 }
1594 
1595 // If a comparison described by C is suitable for CLI(Y), CHHSI or CLHHSI,
1596 // adjust the operands as necessary.
1597 static void adjustSubwordCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
1598   // For us to make any changes, it must a comparison between a single-use
1599   // load and a constant.
1600   if (!C.Op0.hasOneUse() ||
1601       C.Op0.getOpcode() != ISD::LOAD ||
1602       C.Op1.getOpcode() != ISD::Constant)
1603     return;
1604 
1605   // We must have an 8- or 16-bit load.
1606   auto *Load = cast<LoadSDNode>(C.Op0);
1607   unsigned NumBits = Load->getMemoryVT().getStoreSizeInBits();
1608   if (NumBits != 8 && NumBits != 16)
1609     return;
1610 
1611   // The load must be an extending one and the constant must be within the
1612   // range of the unextended value.
1613   auto *ConstOp1 = cast<ConstantSDNode>(C.Op1);
1614   uint64_t Value = ConstOp1->getZExtValue();
1615   uint64_t Mask = (1 << NumBits) - 1;
1616   if (Load->getExtensionType() == ISD::SEXTLOAD) {
1617     // Make sure that ConstOp1 is in range of C.Op0.
1618     int64_t SignedValue = ConstOp1->getSExtValue();
1619     if (uint64_t(SignedValue) + (uint64_t(1) << (NumBits - 1)) > Mask)
1620       return;
1621     if (C.ICmpType != SystemZICMP::SignedOnly) {
1622       // Unsigned comparison between two sign-extended values is equivalent
1623       // to unsigned comparison between two zero-extended values.
1624       Value &= Mask;
1625     } else if (NumBits == 8) {
1626       // Try to treat the comparison as unsigned, so that we can use CLI.
1627       // Adjust CCMask and Value as necessary.
1628       if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_LT)
1629         // Test whether the high bit of the byte is set.
1630         Value = 127, C.CCMask = SystemZ::CCMASK_CMP_GT;
1631       else if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_GE)
1632         // Test whether the high bit of the byte is clear.
1633         Value = 128, C.CCMask = SystemZ::CCMASK_CMP_LT;
1634       else
1635         // No instruction exists for this combination.
1636         return;
1637       C.ICmpType = SystemZICMP::UnsignedOnly;
1638     }
1639   } else if (Load->getExtensionType() == ISD::ZEXTLOAD) {
1640     if (Value > Mask)
1641       return;
1642     // If the constant is in range, we can use any comparison.
1643     C.ICmpType = SystemZICMP::Any;
1644   } else
1645     return;
1646 
1647   // Make sure that the first operand is an i32 of the right extension type.
1648   ISD::LoadExtType ExtType = (C.ICmpType == SystemZICMP::SignedOnly ?
1649                               ISD::SEXTLOAD :
1650                               ISD::ZEXTLOAD);
1651   if (C.Op0.getValueType() != MVT::i32 ||
1652       Load->getExtensionType() != ExtType)
1653     C.Op0 = DAG.getExtLoad(ExtType, SDLoc(Load), MVT::i32,
1654                            Load->getChain(), Load->getBasePtr(),
1655                            Load->getPointerInfo(), Load->getMemoryVT(),
1656                            Load->isVolatile(), Load->isNonTemporal(),
1657                            Load->isInvariant(), Load->getAlignment());
1658 
1659   // Make sure that the second operand is an i32 with the right value.
1660   if (C.Op1.getValueType() != MVT::i32 ||
1661       Value != ConstOp1->getZExtValue())
1662     C.Op1 = DAG.getConstant(Value, DL, MVT::i32);
1663 }
1664 
1665 // Return true if Op is either an unextended load, or a load suitable
1666 // for integer register-memory comparisons of type ICmpType.
1667 static bool isNaturalMemoryOperand(SDValue Op, unsigned ICmpType) {
1668   auto *Load = dyn_cast<LoadSDNode>(Op.getNode());
1669   if (Load) {
1670     // There are no instructions to compare a register with a memory byte.
1671     if (Load->getMemoryVT() == MVT::i8)
1672       return false;
1673     // Otherwise decide on extension type.
1674     switch (Load->getExtensionType()) {
1675     case ISD::NON_EXTLOAD:
1676       return true;
1677     case ISD::SEXTLOAD:
1678       return ICmpType != SystemZICMP::UnsignedOnly;
1679     case ISD::ZEXTLOAD:
1680       return ICmpType != SystemZICMP::SignedOnly;
1681     default:
1682       break;
1683     }
1684   }
1685   return false;
1686 }
1687 
1688 // Return true if it is better to swap the operands of C.
1689 static bool shouldSwapCmpOperands(const Comparison &C) {
1690   // Leave f128 comparisons alone, since they have no memory forms.
1691   if (C.Op0.getValueType() == MVT::f128)
1692     return false;
1693 
1694   // Always keep a floating-point constant second, since comparisons with
1695   // zero can use LOAD TEST and comparisons with other constants make a
1696   // natural memory operand.
1697   if (isa<ConstantFPSDNode>(C.Op1))
1698     return false;
1699 
1700   // Never swap comparisons with zero since there are many ways to optimize
1701   // those later.
1702   auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
1703   if (ConstOp1 && ConstOp1->getZExtValue() == 0)
1704     return false;
1705 
1706   // Also keep natural memory operands second if the loaded value is
1707   // only used here.  Several comparisons have memory forms.
1708   if (isNaturalMemoryOperand(C.Op1, C.ICmpType) && C.Op1.hasOneUse())
1709     return false;
1710 
1711   // Look for cases where Cmp0 is a single-use load and Cmp1 isn't.
1712   // In that case we generally prefer the memory to be second.
1713   if (isNaturalMemoryOperand(C.Op0, C.ICmpType) && C.Op0.hasOneUse()) {
1714     // The only exceptions are when the second operand is a constant and
1715     // we can use things like CHHSI.
1716     if (!ConstOp1)
1717       return true;
1718     // The unsigned memory-immediate instructions can handle 16-bit
1719     // unsigned integers.
1720     if (C.ICmpType != SystemZICMP::SignedOnly &&
1721         isUInt<16>(ConstOp1->getZExtValue()))
1722       return false;
1723     // The signed memory-immediate instructions can handle 16-bit
1724     // signed integers.
1725     if (C.ICmpType != SystemZICMP::UnsignedOnly &&
1726         isInt<16>(ConstOp1->getSExtValue()))
1727       return false;
1728     return true;
1729   }
1730 
1731   // Try to promote the use of CGFR and CLGFR.
1732   unsigned Opcode0 = C.Op0.getOpcode();
1733   if (C.ICmpType != SystemZICMP::UnsignedOnly && Opcode0 == ISD::SIGN_EXTEND)
1734     return true;
1735   if (C.ICmpType != SystemZICMP::SignedOnly && Opcode0 == ISD::ZERO_EXTEND)
1736     return true;
1737   if (C.ICmpType != SystemZICMP::SignedOnly &&
1738       Opcode0 == ISD::AND &&
1739       C.Op0.getOperand(1).getOpcode() == ISD::Constant &&
1740       cast<ConstantSDNode>(C.Op0.getOperand(1))->getZExtValue() == 0xffffffff)
1741     return true;
1742 
1743   return false;
1744 }
1745 
1746 // Return a version of comparison CC mask CCMask in which the LT and GT
1747 // actions are swapped.
1748 static unsigned reverseCCMask(unsigned CCMask) {
1749   return ((CCMask & SystemZ::CCMASK_CMP_EQ) |
1750           (CCMask & SystemZ::CCMASK_CMP_GT ? SystemZ::CCMASK_CMP_LT : 0) |
1751           (CCMask & SystemZ::CCMASK_CMP_LT ? SystemZ::CCMASK_CMP_GT : 0) |
1752           (CCMask & SystemZ::CCMASK_CMP_UO));
1753 }
1754 
1755 // Check whether C tests for equality between X and Y and whether X - Y
1756 // or Y - X is also computed.  In that case it's better to compare the
1757 // result of the subtraction against zero.
1758 static void adjustForSubtraction(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
1759   if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
1760       C.CCMask == SystemZ::CCMASK_CMP_NE) {
1761     for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
1762       SDNode *N = *I;
1763       if (N->getOpcode() == ISD::SUB &&
1764           ((N->getOperand(0) == C.Op0 && N->getOperand(1) == C.Op1) ||
1765            (N->getOperand(0) == C.Op1 && N->getOperand(1) == C.Op0))) {
1766         C.Op0 = SDValue(N, 0);
1767         C.Op1 = DAG.getConstant(0, DL, N->getValueType(0));
1768         return;
1769       }
1770     }
1771   }
1772 }
1773 
1774 // Check whether C compares a floating-point value with zero and if that
1775 // floating-point value is also negated.  In this case we can use the
1776 // negation to set CC, so avoiding separate LOAD AND TEST and
1777 // LOAD (NEGATIVE/COMPLEMENT) instructions.
1778 static void adjustForFNeg(Comparison &C) {
1779   auto *C1 = dyn_cast<ConstantFPSDNode>(C.Op1);
1780   if (C1 && C1->isZero()) {
1781     for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
1782       SDNode *N = *I;
1783       if (N->getOpcode() == ISD::FNEG) {
1784         C.Op0 = SDValue(N, 0);
1785         C.CCMask = reverseCCMask(C.CCMask);
1786         return;
1787       }
1788     }
1789   }
1790 }
1791 
1792 // Check whether C compares (shl X, 32) with 0 and whether X is
1793 // also sign-extended.  In that case it is better to test the result
1794 // of the sign extension using LTGFR.
1795 //
1796 // This case is important because InstCombine transforms a comparison
1797 // with (sext (trunc X)) into a comparison with (shl X, 32).
1798 static void adjustForLTGFR(Comparison &C) {
1799   // Check for a comparison between (shl X, 32) and 0.
1800   if (C.Op0.getOpcode() == ISD::SHL &&
1801       C.Op0.getValueType() == MVT::i64 &&
1802       C.Op1.getOpcode() == ISD::Constant &&
1803       cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
1804     auto *C1 = dyn_cast<ConstantSDNode>(C.Op0.getOperand(1));
1805     if (C1 && C1->getZExtValue() == 32) {
1806       SDValue ShlOp0 = C.Op0.getOperand(0);
1807       // See whether X has any SIGN_EXTEND_INREG uses.
1808       for (auto I = ShlOp0->use_begin(), E = ShlOp0->use_end(); I != E; ++I) {
1809         SDNode *N = *I;
1810         if (N->getOpcode() == ISD::SIGN_EXTEND_INREG &&
1811             cast<VTSDNode>(N->getOperand(1))->getVT() == MVT::i32) {
1812           C.Op0 = SDValue(N, 0);
1813           return;
1814         }
1815       }
1816     }
1817   }
1818 }
1819 
1820 // If C compares the truncation of an extending load, try to compare
1821 // the untruncated value instead.  This exposes more opportunities to
1822 // reuse CC.
1823 static void adjustICmpTruncate(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
1824   if (C.Op0.getOpcode() == ISD::TRUNCATE &&
1825       C.Op0.getOperand(0).getOpcode() == ISD::LOAD &&
1826       C.Op1.getOpcode() == ISD::Constant &&
1827       cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
1828     auto *L = cast<LoadSDNode>(C.Op0.getOperand(0));
1829     if (L->getMemoryVT().getStoreSizeInBits()
1830         <= C.Op0.getValueType().getSizeInBits()) {
1831       unsigned Type = L->getExtensionType();
1832       if ((Type == ISD::ZEXTLOAD && C.ICmpType != SystemZICMP::SignedOnly) ||
1833           (Type == ISD::SEXTLOAD && C.ICmpType != SystemZICMP::UnsignedOnly)) {
1834         C.Op0 = C.Op0.getOperand(0);
1835         C.Op1 = DAG.getConstant(0, DL, C.Op0.getValueType());
1836       }
1837     }
1838   }
1839 }
1840 
1841 // Return true if shift operation N has an in-range constant shift value.
1842 // Store it in ShiftVal if so.
1843 static bool isSimpleShift(SDValue N, unsigned &ShiftVal) {
1844   auto *Shift = dyn_cast<ConstantSDNode>(N.getOperand(1));
1845   if (!Shift)
1846     return false;
1847 
1848   uint64_t Amount = Shift->getZExtValue();
1849   if (Amount >= N.getValueType().getSizeInBits())
1850     return false;
1851 
1852   ShiftVal = Amount;
1853   return true;
1854 }
1855 
1856 // Check whether an AND with Mask is suitable for a TEST UNDER MASK
1857 // instruction and whether the CC value is descriptive enough to handle
1858 // a comparison of type Opcode between the AND result and CmpVal.
1859 // CCMask says which comparison result is being tested and BitSize is
1860 // the number of bits in the operands.  If TEST UNDER MASK can be used,
1861 // return the corresponding CC mask, otherwise return 0.
1862 static unsigned getTestUnderMaskCond(unsigned BitSize, unsigned CCMask,
1863                                      uint64_t Mask, uint64_t CmpVal,
1864                                      unsigned ICmpType) {
1865   assert(Mask != 0 && "ANDs with zero should have been removed by now");
1866 
1867   // Check whether the mask is suitable for TMHH, TMHL, TMLH or TMLL.
1868   if (!SystemZ::isImmLL(Mask) && !SystemZ::isImmLH(Mask) &&
1869       !SystemZ::isImmHL(Mask) && !SystemZ::isImmHH(Mask))
1870     return 0;
1871 
1872   // Work out the masks for the lowest and highest bits.
1873   unsigned HighShift = 63 - countLeadingZeros(Mask);
1874   uint64_t High = uint64_t(1) << HighShift;
1875   uint64_t Low = uint64_t(1) << countTrailingZeros(Mask);
1876 
1877   // Signed ordered comparisons are effectively unsigned if the sign
1878   // bit is dropped.
1879   bool EffectivelyUnsigned = (ICmpType != SystemZICMP::SignedOnly);
1880 
1881   // Check for equality comparisons with 0, or the equivalent.
1882   if (CmpVal == 0) {
1883     if (CCMask == SystemZ::CCMASK_CMP_EQ)
1884       return SystemZ::CCMASK_TM_ALL_0;
1885     if (CCMask == SystemZ::CCMASK_CMP_NE)
1886       return SystemZ::CCMASK_TM_SOME_1;
1887   }
1888   if (EffectivelyUnsigned && CmpVal > 0 && CmpVal <= Low) {
1889     if (CCMask == SystemZ::CCMASK_CMP_LT)
1890       return SystemZ::CCMASK_TM_ALL_0;
1891     if (CCMask == SystemZ::CCMASK_CMP_GE)
1892       return SystemZ::CCMASK_TM_SOME_1;
1893   }
1894   if (EffectivelyUnsigned && CmpVal < Low) {
1895     if (CCMask == SystemZ::CCMASK_CMP_LE)
1896       return SystemZ::CCMASK_TM_ALL_0;
1897     if (CCMask == SystemZ::CCMASK_CMP_GT)
1898       return SystemZ::CCMASK_TM_SOME_1;
1899   }
1900 
1901   // Check for equality comparisons with the mask, or the equivalent.
1902   if (CmpVal == Mask) {
1903     if (CCMask == SystemZ::CCMASK_CMP_EQ)
1904       return SystemZ::CCMASK_TM_ALL_1;
1905     if (CCMask == SystemZ::CCMASK_CMP_NE)
1906       return SystemZ::CCMASK_TM_SOME_0;
1907   }
1908   if (EffectivelyUnsigned && CmpVal >= Mask - Low && CmpVal < Mask) {
1909     if (CCMask == SystemZ::CCMASK_CMP_GT)
1910       return SystemZ::CCMASK_TM_ALL_1;
1911     if (CCMask == SystemZ::CCMASK_CMP_LE)
1912       return SystemZ::CCMASK_TM_SOME_0;
1913   }
1914   if (EffectivelyUnsigned && CmpVal > Mask - Low && CmpVal <= Mask) {
1915     if (CCMask == SystemZ::CCMASK_CMP_GE)
1916       return SystemZ::CCMASK_TM_ALL_1;
1917     if (CCMask == SystemZ::CCMASK_CMP_LT)
1918       return SystemZ::CCMASK_TM_SOME_0;
1919   }
1920 
1921   // Check for ordered comparisons with the top bit.
1922   if (EffectivelyUnsigned && CmpVal >= Mask - High && CmpVal < High) {
1923     if (CCMask == SystemZ::CCMASK_CMP_LE)
1924       return SystemZ::CCMASK_TM_MSB_0;
1925     if (CCMask == SystemZ::CCMASK_CMP_GT)
1926       return SystemZ::CCMASK_TM_MSB_1;
1927   }
1928   if (EffectivelyUnsigned && CmpVal > Mask - High && CmpVal <= High) {
1929     if (CCMask == SystemZ::CCMASK_CMP_LT)
1930       return SystemZ::CCMASK_TM_MSB_0;
1931     if (CCMask == SystemZ::CCMASK_CMP_GE)
1932       return SystemZ::CCMASK_TM_MSB_1;
1933   }
1934 
1935   // If there are just two bits, we can do equality checks for Low and High
1936   // as well.
1937   if (Mask == Low + High) {
1938     if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == Low)
1939       return SystemZ::CCMASK_TM_MIXED_MSB_0;
1940     if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == Low)
1941       return SystemZ::CCMASK_TM_MIXED_MSB_0 ^ SystemZ::CCMASK_ANY;
1942     if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == High)
1943       return SystemZ::CCMASK_TM_MIXED_MSB_1;
1944     if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == High)
1945       return SystemZ::CCMASK_TM_MIXED_MSB_1 ^ SystemZ::CCMASK_ANY;
1946   }
1947 
1948   // Looks like we've exhausted our options.
1949   return 0;
1950 }
1951 
1952 // See whether C can be implemented as a TEST UNDER MASK instruction.
1953 // Update the arguments with the TM version if so.
1954 static void adjustForTestUnderMask(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
1955   // Check that we have a comparison with a constant.
1956   auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
1957   if (!ConstOp1)
1958     return;
1959   uint64_t CmpVal = ConstOp1->getZExtValue();
1960 
1961   // Check whether the nonconstant input is an AND with a constant mask.
1962   Comparison NewC(C);
1963   uint64_t MaskVal;
1964   ConstantSDNode *Mask = nullptr;
1965   if (C.Op0.getOpcode() == ISD::AND) {
1966     NewC.Op0 = C.Op0.getOperand(0);
1967     NewC.Op1 = C.Op0.getOperand(1);
1968     Mask = dyn_cast<ConstantSDNode>(NewC.Op1);
1969     if (!Mask)
1970       return;
1971     MaskVal = Mask->getZExtValue();
1972   } else {
1973     // There is no instruction to compare with a 64-bit immediate
1974     // so use TMHH instead if possible.  We need an unsigned ordered
1975     // comparison with an i64 immediate.
1976     if (NewC.Op0.getValueType() != MVT::i64 ||
1977         NewC.CCMask == SystemZ::CCMASK_CMP_EQ ||
1978         NewC.CCMask == SystemZ::CCMASK_CMP_NE ||
1979         NewC.ICmpType == SystemZICMP::SignedOnly)
1980       return;
1981     // Convert LE and GT comparisons into LT and GE.
1982     if (NewC.CCMask == SystemZ::CCMASK_CMP_LE ||
1983         NewC.CCMask == SystemZ::CCMASK_CMP_GT) {
1984       if (CmpVal == uint64_t(-1))
1985         return;
1986       CmpVal += 1;
1987       NewC.CCMask ^= SystemZ::CCMASK_CMP_EQ;
1988     }
1989     // If the low N bits of Op1 are zero than the low N bits of Op0 can
1990     // be masked off without changing the result.
1991     MaskVal = -(CmpVal & -CmpVal);
1992     NewC.ICmpType = SystemZICMP::UnsignedOnly;
1993   }
1994   if (!MaskVal)
1995     return;
1996 
1997   // Check whether the combination of mask, comparison value and comparison
1998   // type are suitable.
1999   unsigned BitSize = NewC.Op0.getValueType().getSizeInBits();
2000   unsigned NewCCMask, ShiftVal;
2001   if (NewC.ICmpType != SystemZICMP::SignedOnly &&
2002       NewC.Op0.getOpcode() == ISD::SHL &&
2003       isSimpleShift(NewC.Op0, ShiftVal) &&
2004       (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
2005                                         MaskVal >> ShiftVal,
2006                                         CmpVal >> ShiftVal,
2007                                         SystemZICMP::Any))) {
2008     NewC.Op0 = NewC.Op0.getOperand(0);
2009     MaskVal >>= ShiftVal;
2010   } else if (NewC.ICmpType != SystemZICMP::SignedOnly &&
2011              NewC.Op0.getOpcode() == ISD::SRL &&
2012              isSimpleShift(NewC.Op0, ShiftVal) &&
2013              (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
2014                                                MaskVal << ShiftVal,
2015                                                CmpVal << ShiftVal,
2016                                                SystemZICMP::UnsignedOnly))) {
2017     NewC.Op0 = NewC.Op0.getOperand(0);
2018     MaskVal <<= ShiftVal;
2019   } else {
2020     NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, MaskVal, CmpVal,
2021                                      NewC.ICmpType);
2022     if (!NewCCMask)
2023       return;
2024   }
2025 
2026   // Go ahead and make the change.
2027   C.Opcode = SystemZISD::TM;
2028   C.Op0 = NewC.Op0;
2029   if (Mask && Mask->getZExtValue() == MaskVal)
2030     C.Op1 = SDValue(Mask, 0);
2031   else
2032     C.Op1 = DAG.getConstant(MaskVal, DL, C.Op0.getValueType());
2033   C.CCValid = SystemZ::CCMASK_TM;
2034   C.CCMask = NewCCMask;
2035 }
2036 
2037 // Return a Comparison that tests the condition-code result of intrinsic
2038 // node Call against constant integer CC using comparison code Cond.
2039 // Opcode is the opcode of the SystemZISD operation for the intrinsic
2040 // and CCValid is the set of possible condition-code results.
2041 static Comparison getIntrinsicCmp(SelectionDAG &DAG, unsigned Opcode,
2042                                   SDValue Call, unsigned CCValid, uint64_t CC,
2043                                   ISD::CondCode Cond) {
2044   Comparison C(Call, SDValue());
2045   C.Opcode = Opcode;
2046   C.CCValid = CCValid;
2047   if (Cond == ISD::SETEQ)
2048     // bit 3 for CC==0, bit 0 for CC==3, always false for CC>3.
2049     C.CCMask = CC < 4 ? 1 << (3 - CC) : 0;
2050   else if (Cond == ISD::SETNE)
2051     // ...and the inverse of that.
2052     C.CCMask = CC < 4 ? ~(1 << (3 - CC)) : -1;
2053   else if (Cond == ISD::SETLT || Cond == ISD::SETULT)
2054     // bits above bit 3 for CC==0 (always false), bits above bit 0 for CC==3,
2055     // always true for CC>3.
2056     C.CCMask = CC < 4 ? ~0U << (4 - CC) : -1;
2057   else if (Cond == ISD::SETGE || Cond == ISD::SETUGE)
2058     // ...and the inverse of that.
2059     C.CCMask = CC < 4 ? ~(~0U << (4 - CC)) : 0;
2060   else if (Cond == ISD::SETLE || Cond == ISD::SETULE)
2061     // bit 3 and above for CC==0, bit 0 and above for CC==3 (always true),
2062     // always true for CC>3.
2063     C.CCMask = CC < 4 ? ~0U << (3 - CC) : -1;
2064   else if (Cond == ISD::SETGT || Cond == ISD::SETUGT)
2065     // ...and the inverse of that.
2066     C.CCMask = CC < 4 ? ~(~0U << (3 - CC)) : 0;
2067   else
2068     llvm_unreachable("Unexpected integer comparison type");
2069   C.CCMask &= CCValid;
2070   return C;
2071 }
2072 
2073 // Decide how to implement a comparison of type Cond between CmpOp0 with CmpOp1.
2074 static Comparison getCmp(SelectionDAG &DAG, SDValue CmpOp0, SDValue CmpOp1,
2075                          ISD::CondCode Cond, SDLoc DL) {
2076   if (CmpOp1.getOpcode() == ISD::Constant) {
2077     uint64_t Constant = cast<ConstantSDNode>(CmpOp1)->getZExtValue();
2078     unsigned Opcode, CCValid;
2079     if (CmpOp0.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
2080         CmpOp0.getResNo() == 0 && CmpOp0->hasNUsesOfValue(1, 0) &&
2081         isIntrinsicWithCCAndChain(CmpOp0, Opcode, CCValid))
2082       return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond);
2083     if (CmpOp0.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
2084         CmpOp0.getResNo() == CmpOp0->getNumValues() - 1 &&
2085         isIntrinsicWithCC(CmpOp0, Opcode, CCValid))
2086       return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond);
2087   }
2088   Comparison C(CmpOp0, CmpOp1);
2089   C.CCMask = CCMaskForCondCode(Cond);
2090   if (C.Op0.getValueType().isFloatingPoint()) {
2091     C.CCValid = SystemZ::CCMASK_FCMP;
2092     C.Opcode = SystemZISD::FCMP;
2093     adjustForFNeg(C);
2094   } else {
2095     C.CCValid = SystemZ::CCMASK_ICMP;
2096     C.Opcode = SystemZISD::ICMP;
2097     // Choose the type of comparison.  Equality and inequality tests can
2098     // use either signed or unsigned comparisons.  The choice also doesn't
2099     // matter if both sign bits are known to be clear.  In those cases we
2100     // want to give the main isel code the freedom to choose whichever
2101     // form fits best.
2102     if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
2103         C.CCMask == SystemZ::CCMASK_CMP_NE ||
2104         (DAG.SignBitIsZero(C.Op0) && DAG.SignBitIsZero(C.Op1)))
2105       C.ICmpType = SystemZICMP::Any;
2106     else if (C.CCMask & SystemZ::CCMASK_CMP_UO)
2107       C.ICmpType = SystemZICMP::UnsignedOnly;
2108     else
2109       C.ICmpType = SystemZICMP::SignedOnly;
2110     C.CCMask &= ~SystemZ::CCMASK_CMP_UO;
2111     adjustZeroCmp(DAG, DL, C);
2112     adjustSubwordCmp(DAG, DL, C);
2113     adjustForSubtraction(DAG, DL, C);
2114     adjustForLTGFR(C);
2115     adjustICmpTruncate(DAG, DL, C);
2116   }
2117 
2118   if (shouldSwapCmpOperands(C)) {
2119     std::swap(C.Op0, C.Op1);
2120     C.CCMask = reverseCCMask(C.CCMask);
2121   }
2122 
2123   adjustForTestUnderMask(DAG, DL, C);
2124   return C;
2125 }
2126 
2127 // Emit the comparison instruction described by C.
2128 static SDValue emitCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
2129   if (!C.Op1.getNode()) {
2130     SDValue Op;
2131     switch (C.Op0.getOpcode()) {
2132     case ISD::INTRINSIC_W_CHAIN:
2133       Op = emitIntrinsicWithChainAndGlue(DAG, C.Op0, C.Opcode);
2134       break;
2135     case ISD::INTRINSIC_WO_CHAIN:
2136       Op = emitIntrinsicWithGlue(DAG, C.Op0, C.Opcode);
2137       break;
2138     default:
2139       llvm_unreachable("Invalid comparison operands");
2140     }
2141     return SDValue(Op.getNode(), Op->getNumValues() - 1);
2142   }
2143   if (C.Opcode == SystemZISD::ICMP)
2144     return DAG.getNode(SystemZISD::ICMP, DL, MVT::Glue, C.Op0, C.Op1,
2145                        DAG.getConstant(C.ICmpType, DL, MVT::i32));
2146   if (C.Opcode == SystemZISD::TM) {
2147     bool RegisterOnly = (bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_0) !=
2148                          bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_1));
2149     return DAG.getNode(SystemZISD::TM, DL, MVT::Glue, C.Op0, C.Op1,
2150                        DAG.getConstant(RegisterOnly, DL, MVT::i32));
2151   }
2152   return DAG.getNode(C.Opcode, DL, MVT::Glue, C.Op0, C.Op1);
2153 }
2154 
2155 // Implement a 32-bit *MUL_LOHI operation by extending both operands to
2156 // 64 bits.  Extend is the extension type to use.  Store the high part
2157 // in Hi and the low part in Lo.
2158 static void lowerMUL_LOHI32(SelectionDAG &DAG, SDLoc DL,
2159                             unsigned Extend, SDValue Op0, SDValue Op1,
2160                             SDValue &Hi, SDValue &Lo) {
2161   Op0 = DAG.getNode(Extend, DL, MVT::i64, Op0);
2162   Op1 = DAG.getNode(Extend, DL, MVT::i64, Op1);
2163   SDValue Mul = DAG.getNode(ISD::MUL, DL, MVT::i64, Op0, Op1);
2164   Hi = DAG.getNode(ISD::SRL, DL, MVT::i64, Mul,
2165                    DAG.getConstant(32, DL, MVT::i64));
2166   Hi = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Hi);
2167   Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mul);
2168 }
2169 
2170 // Lower a binary operation that produces two VT results, one in each
2171 // half of a GR128 pair.  Op0 and Op1 are the VT operands to the operation,
2172 // Extend extends Op0 to a GR128, and Opcode performs the GR128 operation
2173 // on the extended Op0 and (unextended) Op1.  Store the even register result
2174 // in Even and the odd register result in Odd.
2175 static void lowerGR128Binary(SelectionDAG &DAG, SDLoc DL, EVT VT,
2176                              unsigned Extend, unsigned Opcode,
2177                              SDValue Op0, SDValue Op1,
2178                              SDValue &Even, SDValue &Odd) {
2179   SDNode *In128 = DAG.getMachineNode(Extend, DL, MVT::Untyped, Op0);
2180   SDValue Result = DAG.getNode(Opcode, DL, MVT::Untyped,
2181                                SDValue(In128, 0), Op1);
2182   bool Is32Bit = is32Bit(VT);
2183   Even = DAG.getTargetExtractSubreg(SystemZ::even128(Is32Bit), DL, VT, Result);
2184   Odd = DAG.getTargetExtractSubreg(SystemZ::odd128(Is32Bit), DL, VT, Result);
2185 }
2186 
2187 // Return an i32 value that is 1 if the CC value produced by Glue is
2188 // in the mask CCMask and 0 otherwise.  CC is known to have a value
2189 // in CCValid, so other values can be ignored.
2190 static SDValue emitSETCC(SelectionDAG &DAG, SDLoc DL, SDValue Glue,
2191                          unsigned CCValid, unsigned CCMask) {
2192   IPMConversion Conversion = getIPMConversion(CCValid, CCMask);
2193   SDValue Result = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
2194 
2195   if (Conversion.XORValue)
2196     Result = DAG.getNode(ISD::XOR, DL, MVT::i32, Result,
2197                          DAG.getConstant(Conversion.XORValue, DL, MVT::i32));
2198 
2199   if (Conversion.AddValue)
2200     Result = DAG.getNode(ISD::ADD, DL, MVT::i32, Result,
2201                          DAG.getConstant(Conversion.AddValue, DL, MVT::i32));
2202 
2203   // The SHR/AND sequence should get optimized to an RISBG.
2204   Result = DAG.getNode(ISD::SRL, DL, MVT::i32, Result,
2205                        DAG.getConstant(Conversion.Bit, DL, MVT::i32));
2206   if (Conversion.Bit != 31)
2207     Result = DAG.getNode(ISD::AND, DL, MVT::i32, Result,
2208                          DAG.getConstant(1, DL, MVT::i32));
2209   return Result;
2210 }
2211 
2212 // Return the SystemISD vector comparison operation for CC, or 0 if it cannot
2213 // be done directly.  IsFP is true if CC is for a floating-point rather than
2214 // integer comparison.
2215 static unsigned getVectorComparison(ISD::CondCode CC, bool IsFP) {
2216   switch (CC) {
2217   case ISD::SETOEQ:
2218   case ISD::SETEQ:
2219     return IsFP ? SystemZISD::VFCMPE : SystemZISD::VICMPE;
2220 
2221   case ISD::SETOGE:
2222   case ISD::SETGE:
2223     return IsFP ? SystemZISD::VFCMPHE : static_cast<SystemZISD::NodeType>(0);
2224 
2225   case ISD::SETOGT:
2226   case ISD::SETGT:
2227     return IsFP ? SystemZISD::VFCMPH : SystemZISD::VICMPH;
2228 
2229   case ISD::SETUGT:
2230     return IsFP ? static_cast<SystemZISD::NodeType>(0) : SystemZISD::VICMPHL;
2231 
2232   default:
2233     return 0;
2234   }
2235 }
2236 
2237 // Return the SystemZISD vector comparison operation for CC or its inverse,
2238 // or 0 if neither can be done directly.  Indicate in Invert whether the
2239 // result is for the inverse of CC.  IsFP is true if CC is for a
2240 // floating-point rather than integer comparison.
2241 static unsigned getVectorComparisonOrInvert(ISD::CondCode CC, bool IsFP,
2242                                             bool &Invert) {
2243   if (unsigned Opcode = getVectorComparison(CC, IsFP)) {
2244     Invert = false;
2245     return Opcode;
2246   }
2247 
2248   CC = ISD::getSetCCInverse(CC, !IsFP);
2249   if (unsigned Opcode = getVectorComparison(CC, IsFP)) {
2250     Invert = true;
2251     return Opcode;
2252   }
2253 
2254   return 0;
2255 }
2256 
2257 // Return a v2f64 that contains the extended form of elements Start and Start+1
2258 // of v4f32 value Op.
2259 static SDValue expandV4F32ToV2F64(SelectionDAG &DAG, int Start, SDLoc DL,
2260                                   SDValue Op) {
2261   int Mask[] = { Start, -1, Start + 1, -1 };
2262   Op = DAG.getVectorShuffle(MVT::v4f32, DL, Op, DAG.getUNDEF(MVT::v4f32), Mask);
2263   return DAG.getNode(SystemZISD::VEXTEND, DL, MVT::v2f64, Op);
2264 }
2265 
2266 // Build a comparison of vectors CmpOp0 and CmpOp1 using opcode Opcode,
2267 // producing a result of type VT.
2268 static SDValue getVectorCmp(SelectionDAG &DAG, unsigned Opcode, SDLoc DL,
2269                             EVT VT, SDValue CmpOp0, SDValue CmpOp1) {
2270   // There is no hardware support for v4f32, so extend the vector into
2271   // two v2f64s and compare those.
2272   if (CmpOp0.getValueType() == MVT::v4f32) {
2273     SDValue H0 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp0);
2274     SDValue L0 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp0);
2275     SDValue H1 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp1);
2276     SDValue L1 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp1);
2277     SDValue HRes = DAG.getNode(Opcode, DL, MVT::v2i64, H0, H1);
2278     SDValue LRes = DAG.getNode(Opcode, DL, MVT::v2i64, L0, L1);
2279     return DAG.getNode(SystemZISD::PACK, DL, VT, HRes, LRes);
2280   }
2281   return DAG.getNode(Opcode, DL, VT, CmpOp0, CmpOp1);
2282 }
2283 
2284 // Lower a vector comparison of type CC between CmpOp0 and CmpOp1, producing
2285 // an integer mask of type VT.
2286 static SDValue lowerVectorSETCC(SelectionDAG &DAG, SDLoc DL, EVT VT,
2287                                 ISD::CondCode CC, SDValue CmpOp0,
2288                                 SDValue CmpOp1) {
2289   bool IsFP = CmpOp0.getValueType().isFloatingPoint();
2290   bool Invert = false;
2291   SDValue Cmp;
2292   switch (CC) {
2293     // Handle tests for order using (or (ogt y x) (oge x y)).
2294   case ISD::SETUO:
2295     Invert = true;
2296   case ISD::SETO: {
2297     assert(IsFP && "Unexpected integer comparison");
2298     SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0);
2299     SDValue GE = getVectorCmp(DAG, SystemZISD::VFCMPHE, DL, VT, CmpOp0, CmpOp1);
2300     Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GE);
2301     break;
2302   }
2303 
2304     // Handle <> tests using (or (ogt y x) (ogt x y)).
2305   case ISD::SETUEQ:
2306     Invert = true;
2307   case ISD::SETONE: {
2308     assert(IsFP && "Unexpected integer comparison");
2309     SDValue LT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp1, CmpOp0);
2310     SDValue GT = getVectorCmp(DAG, SystemZISD::VFCMPH, DL, VT, CmpOp0, CmpOp1);
2311     Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GT);
2312     break;
2313   }
2314 
2315     // Otherwise a single comparison is enough.  It doesn't really
2316     // matter whether we try the inversion or the swap first, since
2317     // there are no cases where both work.
2318   default:
2319     if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert))
2320       Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp0, CmpOp1);
2321     else {
2322       CC = ISD::getSetCCSwappedOperands(CC);
2323       if (unsigned Opcode = getVectorComparisonOrInvert(CC, IsFP, Invert))
2324         Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp1, CmpOp0);
2325       else
2326         llvm_unreachable("Unhandled comparison");
2327     }
2328     break;
2329   }
2330   if (Invert) {
2331     SDValue Mask = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
2332                                DAG.getConstant(65535, DL, MVT::i32));
2333     Mask = DAG.getNode(ISD::BITCAST, DL, VT, Mask);
2334     Cmp = DAG.getNode(ISD::XOR, DL, VT, Cmp, Mask);
2335   }
2336   return Cmp;
2337 }
2338 
2339 SDValue SystemZTargetLowering::lowerSETCC(SDValue Op,
2340                                           SelectionDAG &DAG) const {
2341   SDValue CmpOp0   = Op.getOperand(0);
2342   SDValue CmpOp1   = Op.getOperand(1);
2343   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
2344   SDLoc DL(Op);
2345   EVT VT = Op.getValueType();
2346   if (VT.isVector())
2347     return lowerVectorSETCC(DAG, DL, VT, CC, CmpOp0, CmpOp1);
2348 
2349   Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
2350   SDValue Glue = emitCmp(DAG, DL, C);
2351   return emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
2352 }
2353 
2354 SDValue SystemZTargetLowering::lowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
2355   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
2356   SDValue CmpOp0   = Op.getOperand(2);
2357   SDValue CmpOp1   = Op.getOperand(3);
2358   SDValue Dest     = Op.getOperand(4);
2359   SDLoc DL(Op);
2360 
2361   Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
2362   SDValue Glue = emitCmp(DAG, DL, C);
2363   return DAG.getNode(SystemZISD::BR_CCMASK, DL, Op.getValueType(),
2364                      Op.getOperand(0), DAG.getConstant(C.CCValid, DL, MVT::i32),
2365                      DAG.getConstant(C.CCMask, DL, MVT::i32), Dest, Glue);
2366 }
2367 
2368 // Return true if Pos is CmpOp and Neg is the negative of CmpOp,
2369 // allowing Pos and Neg to be wider than CmpOp.
2370 static bool isAbsolute(SDValue CmpOp, SDValue Pos, SDValue Neg) {
2371   return (Neg.getOpcode() == ISD::SUB &&
2372           Neg.getOperand(0).getOpcode() == ISD::Constant &&
2373           cast<ConstantSDNode>(Neg.getOperand(0))->getZExtValue() == 0 &&
2374           Neg.getOperand(1) == Pos &&
2375           (Pos == CmpOp ||
2376            (Pos.getOpcode() == ISD::SIGN_EXTEND &&
2377             Pos.getOperand(0) == CmpOp)));
2378 }
2379 
2380 // Return the absolute or negative absolute of Op; IsNegative decides which.
2381 static SDValue getAbsolute(SelectionDAG &DAG, SDLoc DL, SDValue Op,
2382                            bool IsNegative) {
2383   Op = DAG.getNode(SystemZISD::IABS, DL, Op.getValueType(), Op);
2384   if (IsNegative)
2385     Op = DAG.getNode(ISD::SUB, DL, Op.getValueType(),
2386                      DAG.getConstant(0, DL, Op.getValueType()), Op);
2387   return Op;
2388 }
2389 
2390 SDValue SystemZTargetLowering::lowerSELECT_CC(SDValue Op,
2391                                               SelectionDAG &DAG) const {
2392   SDValue CmpOp0   = Op.getOperand(0);
2393   SDValue CmpOp1   = Op.getOperand(1);
2394   SDValue TrueOp   = Op.getOperand(2);
2395   SDValue FalseOp  = Op.getOperand(3);
2396   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
2397   SDLoc DL(Op);
2398 
2399   Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
2400 
2401   // Check for absolute and negative-absolute selections, including those
2402   // where the comparison value is sign-extended (for LPGFR and LNGFR).
2403   // This check supplements the one in DAGCombiner.
2404   if (C.Opcode == SystemZISD::ICMP &&
2405       C.CCMask != SystemZ::CCMASK_CMP_EQ &&
2406       C.CCMask != SystemZ::CCMASK_CMP_NE &&
2407       C.Op1.getOpcode() == ISD::Constant &&
2408       cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
2409     if (isAbsolute(C.Op0, TrueOp, FalseOp))
2410       return getAbsolute(DAG, DL, TrueOp, C.CCMask & SystemZ::CCMASK_CMP_LT);
2411     if (isAbsolute(C.Op0, FalseOp, TrueOp))
2412       return getAbsolute(DAG, DL, FalseOp, C.CCMask & SystemZ::CCMASK_CMP_GT);
2413   }
2414 
2415   SDValue Glue = emitCmp(DAG, DL, C);
2416 
2417   // Special case for handling -1/0 results.  The shifts we use here
2418   // should get optimized with the IPM conversion sequence.
2419   auto *TrueC = dyn_cast<ConstantSDNode>(TrueOp);
2420   auto *FalseC = dyn_cast<ConstantSDNode>(FalseOp);
2421   if (TrueC && FalseC) {
2422     int64_t TrueVal = TrueC->getSExtValue();
2423     int64_t FalseVal = FalseC->getSExtValue();
2424     if ((TrueVal == -1 && FalseVal == 0) || (TrueVal == 0 && FalseVal == -1)) {
2425       // Invert the condition if we want -1 on false.
2426       if (TrueVal == 0)
2427         C.CCMask ^= C.CCValid;
2428       SDValue Result = emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
2429       EVT VT = Op.getValueType();
2430       // Extend the result to VT.  Upper bits are ignored.
2431       if (!is32Bit(VT))
2432         Result = DAG.getNode(ISD::ANY_EXTEND, DL, VT, Result);
2433       // Sign-extend from the low bit.
2434       SDValue ShAmt = DAG.getConstant(VT.getSizeInBits() - 1, DL, MVT::i32);
2435       SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, Result, ShAmt);
2436       return DAG.getNode(ISD::SRA, DL, VT, Shl, ShAmt);
2437     }
2438   }
2439 
2440   SDValue Ops[] = {TrueOp, FalseOp, DAG.getConstant(C.CCValid, DL, MVT::i32),
2441                    DAG.getConstant(C.CCMask, DL, MVT::i32), Glue};
2442 
2443   SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
2444   return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VTs, Ops);
2445 }
2446 
2447 SDValue SystemZTargetLowering::lowerGlobalAddress(GlobalAddressSDNode *Node,
2448                                                   SelectionDAG &DAG) const {
2449   SDLoc DL(Node);
2450   const GlobalValue *GV = Node->getGlobal();
2451   int64_t Offset = Node->getOffset();
2452   EVT PtrVT = getPointerTy(DAG.getDataLayout());
2453   Reloc::Model RM = DAG.getTarget().getRelocationModel();
2454   CodeModel::Model CM = DAG.getTarget().getCodeModel();
2455 
2456   SDValue Result;
2457   if (Subtarget.isPC32DBLSymbol(GV, RM, CM)) {
2458     // Assign anchors at 1<<12 byte boundaries.
2459     uint64_t Anchor = Offset & ~uint64_t(0xfff);
2460     Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor);
2461     Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2462 
2463     // The offset can be folded into the address if it is aligned to a halfword.
2464     Offset -= Anchor;
2465     if (Offset != 0 && (Offset & 1) == 0) {
2466       SDValue Full = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor + Offset);
2467       Result = DAG.getNode(SystemZISD::PCREL_OFFSET, DL, PtrVT, Full, Result);
2468       Offset = 0;
2469     }
2470   } else {
2471     Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, SystemZII::MO_GOT);
2472     Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2473     Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
2474                          MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2475                          false, false, false, 0);
2476   }
2477 
2478   // If there was a non-zero offset that we didn't fold, create an explicit
2479   // addition for it.
2480   if (Offset != 0)
2481     Result = DAG.getNode(ISD::ADD, DL, PtrVT, Result,
2482                          DAG.getConstant(Offset, DL, PtrVT));
2483 
2484   return Result;
2485 }
2486 
2487 SDValue SystemZTargetLowering::lowerTLSGetOffset(GlobalAddressSDNode *Node,
2488                                                  SelectionDAG &DAG,
2489                                                  unsigned Opcode,
2490                                                  SDValue GOTOffset) const {
2491   SDLoc DL(Node);
2492   EVT PtrVT = getPointerTy(DAG.getDataLayout());
2493   SDValue Chain = DAG.getEntryNode();
2494   SDValue Glue;
2495 
2496   // __tls_get_offset takes the GOT offset in %r2 and the GOT in %r12.
2497   SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
2498   Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R12D, GOT, Glue);
2499   Glue = Chain.getValue(1);
2500   Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R2D, GOTOffset, Glue);
2501   Glue = Chain.getValue(1);
2502 
2503   // The first call operand is the chain and the second is the TLS symbol.
2504   SmallVector<SDValue, 8> Ops;
2505   Ops.push_back(Chain);
2506   Ops.push_back(DAG.getTargetGlobalAddress(Node->getGlobal(), DL,
2507                                            Node->getValueType(0),
2508                                            0, 0));
2509 
2510   // Add argument registers to the end of the list so that they are
2511   // known live into the call.
2512   Ops.push_back(DAG.getRegister(SystemZ::R2D, PtrVT));
2513   Ops.push_back(DAG.getRegister(SystemZ::R12D, PtrVT));
2514 
2515   // Add a register mask operand representing the call-preserved registers.
2516   const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
2517   const uint32_t *Mask =
2518       TRI->getCallPreservedMask(DAG.getMachineFunction(), CallingConv::C);
2519   assert(Mask && "Missing call preserved mask for calling convention");
2520   Ops.push_back(DAG.getRegisterMask(Mask));
2521 
2522   // Glue the call to the argument copies.
2523   Ops.push_back(Glue);
2524 
2525   // Emit the call.
2526   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
2527   Chain = DAG.getNode(Opcode, DL, NodeTys, Ops);
2528   Glue = Chain.getValue(1);
2529 
2530   // Copy the return value from %r2.
2531   return DAG.getCopyFromReg(Chain, DL, SystemZ::R2D, PtrVT, Glue);
2532 }
2533 
2534 SDValue SystemZTargetLowering::lowerGlobalTLSAddress(GlobalAddressSDNode *Node,
2535                                                      SelectionDAG &DAG) const {
2536   if (DAG.getTarget().Options.EmulatedTLS)
2537     return LowerToTLSEmulatedModel(Node, DAG);
2538   SDLoc DL(Node);
2539   const GlobalValue *GV = Node->getGlobal();
2540   EVT PtrVT = getPointerTy(DAG.getDataLayout());
2541   TLSModel::Model model = DAG.getTarget().getTLSModel(GV);
2542 
2543   // The high part of the thread pointer is in access register 0.
2544   SDValue TPHi = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
2545                              DAG.getConstant(0, DL, MVT::i32));
2546   TPHi = DAG.getNode(ISD::ANY_EXTEND, DL, PtrVT, TPHi);
2547 
2548   // The low part of the thread pointer is in access register 1.
2549   SDValue TPLo = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
2550                              DAG.getConstant(1, DL, MVT::i32));
2551   TPLo = DAG.getNode(ISD::ZERO_EXTEND, DL, PtrVT, TPLo);
2552 
2553   // Merge them into a single 64-bit address.
2554   SDValue TPHiShifted = DAG.getNode(ISD::SHL, DL, PtrVT, TPHi,
2555                                     DAG.getConstant(32, DL, PtrVT));
2556   SDValue TP = DAG.getNode(ISD::OR, DL, PtrVT, TPHiShifted, TPLo);
2557 
2558   // Get the offset of GA from the thread pointer, based on the TLS model.
2559   SDValue Offset;
2560   switch (model) {
2561     case TLSModel::GeneralDynamic: {
2562       // Load the GOT offset of the tls_index (module ID / per-symbol offset).
2563       SystemZConstantPoolValue *CPV =
2564         SystemZConstantPoolValue::Create(GV, SystemZCP::TLSGD);
2565 
2566       Offset = DAG.getConstantPool(CPV, PtrVT, 8);
2567       Offset = DAG.getLoad(
2568           PtrVT, DL, DAG.getEntryNode(), Offset,
2569           MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2570           false, false, 0);
2571 
2572       // Call __tls_get_offset to retrieve the offset.
2573       Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_GDCALL, Offset);
2574       break;
2575     }
2576 
2577     case TLSModel::LocalDynamic: {
2578       // Load the GOT offset of the module ID.
2579       SystemZConstantPoolValue *CPV =
2580         SystemZConstantPoolValue::Create(GV, SystemZCP::TLSLDM);
2581 
2582       Offset = DAG.getConstantPool(CPV, PtrVT, 8);
2583       Offset = DAG.getLoad(
2584           PtrVT, DL, DAG.getEntryNode(), Offset,
2585           MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2586           false, false, 0);
2587 
2588       // Call __tls_get_offset to retrieve the module base offset.
2589       Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_LDCALL, Offset);
2590 
2591       // Note: The SystemZLDCleanupPass will remove redundant computations
2592       // of the module base offset.  Count total number of local-dynamic
2593       // accesses to trigger execution of that pass.
2594       SystemZMachineFunctionInfo* MFI =
2595         DAG.getMachineFunction().getInfo<SystemZMachineFunctionInfo>();
2596       MFI->incNumLocalDynamicTLSAccesses();
2597 
2598       // Add the per-symbol offset.
2599       CPV = SystemZConstantPoolValue::Create(GV, SystemZCP::DTPOFF);
2600 
2601       SDValue DTPOffset = DAG.getConstantPool(CPV, PtrVT, 8);
2602       DTPOffset = DAG.getLoad(
2603           PtrVT, DL, DAG.getEntryNode(), DTPOffset,
2604           MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2605           false, false, 0);
2606 
2607       Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Offset, DTPOffset);
2608       break;
2609     }
2610 
2611     case TLSModel::InitialExec: {
2612       // Load the offset from the GOT.
2613       Offset = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
2614                                           SystemZII::MO_INDNTPOFF);
2615       Offset = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Offset);
2616       Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Offset,
2617                            MachinePointerInfo::getGOT(DAG.getMachineFunction()),
2618                            false, false, false, 0);
2619       break;
2620     }
2621 
2622     case TLSModel::LocalExec: {
2623       // Force the offset into the constant pool and load it from there.
2624       SystemZConstantPoolValue *CPV =
2625         SystemZConstantPoolValue::Create(GV, SystemZCP::NTPOFF);
2626 
2627       Offset = DAG.getConstantPool(CPV, PtrVT, 8);
2628       Offset = DAG.getLoad(
2629           PtrVT, DL, DAG.getEntryNode(), Offset,
2630           MachinePointerInfo::getConstantPool(DAG.getMachineFunction()), false,
2631           false, false, 0);
2632       break;
2633     }
2634   }
2635 
2636   // Add the base and offset together.
2637   return DAG.getNode(ISD::ADD, DL, PtrVT, TP, Offset);
2638 }
2639 
2640 SDValue SystemZTargetLowering::lowerBlockAddress(BlockAddressSDNode *Node,
2641                                                  SelectionDAG &DAG) const {
2642   SDLoc DL(Node);
2643   const BlockAddress *BA = Node->getBlockAddress();
2644   int64_t Offset = Node->getOffset();
2645   EVT PtrVT = getPointerTy(DAG.getDataLayout());
2646 
2647   SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset);
2648   Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2649   return Result;
2650 }
2651 
2652 SDValue SystemZTargetLowering::lowerJumpTable(JumpTableSDNode *JT,
2653                                               SelectionDAG &DAG) const {
2654   SDLoc DL(JT);
2655   EVT PtrVT = getPointerTy(DAG.getDataLayout());
2656   SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
2657 
2658   // Use LARL to load the address of the table.
2659   return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2660 }
2661 
2662 SDValue SystemZTargetLowering::lowerConstantPool(ConstantPoolSDNode *CP,
2663                                                  SelectionDAG &DAG) const {
2664   SDLoc DL(CP);
2665   EVT PtrVT = getPointerTy(DAG.getDataLayout());
2666 
2667   SDValue Result;
2668   if (CP->isMachineConstantPoolEntry())
2669     Result = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
2670                                        CP->getAlignment());
2671   else
2672     Result = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
2673                                        CP->getAlignment(), CP->getOffset());
2674 
2675   // Use LARL to load the address of the constant pool entry.
2676   return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
2677 }
2678 
2679 SDValue SystemZTargetLowering::lowerBITCAST(SDValue Op,
2680                                             SelectionDAG &DAG) const {
2681   SDLoc DL(Op);
2682   SDValue In = Op.getOperand(0);
2683   EVT InVT = In.getValueType();
2684   EVT ResVT = Op.getValueType();
2685 
2686   // Convert loads directly.  This is normally done by DAGCombiner,
2687   // but we need this case for bitcasts that are created during lowering
2688   // and which are then lowered themselves.
2689   if (auto *LoadN = dyn_cast<LoadSDNode>(In))
2690     return DAG.getLoad(ResVT, DL, LoadN->getChain(), LoadN->getBasePtr(),
2691                        LoadN->getMemOperand());
2692 
2693   if (InVT == MVT::i32 && ResVT == MVT::f32) {
2694     SDValue In64;
2695     if (Subtarget.hasHighWord()) {
2696       SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL,
2697                                        MVT::i64);
2698       In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL,
2699                                        MVT::i64, SDValue(U64, 0), In);
2700     } else {
2701       In64 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, In);
2702       In64 = DAG.getNode(ISD::SHL, DL, MVT::i64, In64,
2703                          DAG.getConstant(32, DL, MVT::i64));
2704     }
2705     SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::f64, In64);
2706     return DAG.getTargetExtractSubreg(SystemZ::subreg_r32,
2707                                       DL, MVT::f32, Out64);
2708   }
2709   if (InVT == MVT::f32 && ResVT == MVT::i32) {
2710     SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::f64);
2711     SDValue In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_r32, DL,
2712                                              MVT::f64, SDValue(U64, 0), In);
2713     SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::i64, In64);
2714     if (Subtarget.hasHighWord())
2715       return DAG.getTargetExtractSubreg(SystemZ::subreg_h32, DL,
2716                                         MVT::i32, Out64);
2717     SDValue Shift = DAG.getNode(ISD::SRL, DL, MVT::i64, Out64,
2718                                 DAG.getConstant(32, DL, MVT::i64));
2719     return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Shift);
2720   }
2721   llvm_unreachable("Unexpected bitcast combination");
2722 }
2723 
2724 SDValue SystemZTargetLowering::lowerVASTART(SDValue Op,
2725                                             SelectionDAG &DAG) const {
2726   MachineFunction &MF = DAG.getMachineFunction();
2727   SystemZMachineFunctionInfo *FuncInfo =
2728     MF.getInfo<SystemZMachineFunctionInfo>();
2729   EVT PtrVT = getPointerTy(DAG.getDataLayout());
2730 
2731   SDValue Chain   = Op.getOperand(0);
2732   SDValue Addr    = Op.getOperand(1);
2733   const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
2734   SDLoc DL(Op);
2735 
2736   // The initial values of each field.
2737   const unsigned NumFields = 4;
2738   SDValue Fields[NumFields] = {
2739     DAG.getConstant(FuncInfo->getVarArgsFirstGPR(), DL, PtrVT),
2740     DAG.getConstant(FuncInfo->getVarArgsFirstFPR(), DL, PtrVT),
2741     DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT),
2742     DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), PtrVT)
2743   };
2744 
2745   // Store each field into its respective slot.
2746   SDValue MemOps[NumFields];
2747   unsigned Offset = 0;
2748   for (unsigned I = 0; I < NumFields; ++I) {
2749     SDValue FieldAddr = Addr;
2750     if (Offset != 0)
2751       FieldAddr = DAG.getNode(ISD::ADD, DL, PtrVT, FieldAddr,
2752                               DAG.getIntPtrConstant(Offset, DL));
2753     MemOps[I] = DAG.getStore(Chain, DL, Fields[I], FieldAddr,
2754                              MachinePointerInfo(SV, Offset),
2755                              false, false, 0);
2756     Offset += 8;
2757   }
2758   return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps);
2759 }
2760 
2761 SDValue SystemZTargetLowering::lowerVACOPY(SDValue Op,
2762                                            SelectionDAG &DAG) const {
2763   SDValue Chain      = Op.getOperand(0);
2764   SDValue DstPtr     = Op.getOperand(1);
2765   SDValue SrcPtr     = Op.getOperand(2);
2766   const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
2767   const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
2768   SDLoc DL(Op);
2769 
2770   return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr, DAG.getIntPtrConstant(32, DL),
2771                        /*Align*/8, /*isVolatile*/false, /*AlwaysInline*/false,
2772                        /*isTailCall*/false,
2773                        MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
2774 }
2775 
2776 SDValue SystemZTargetLowering::
2777 lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
2778   const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
2779   bool RealignOpt = !DAG.getMachineFunction().getFunction()->
2780     hasFnAttribute("no-realign-stack");
2781 
2782   SDValue Chain = Op.getOperand(0);
2783   SDValue Size  = Op.getOperand(1);
2784   SDValue Align = Op.getOperand(2);
2785   SDLoc DL(Op);
2786 
2787   // If user has set the no alignment function attribute, ignore
2788   // alloca alignments.
2789   uint64_t AlignVal = (RealignOpt ?
2790                        dyn_cast<ConstantSDNode>(Align)->getZExtValue() : 0);
2791 
2792   uint64_t StackAlign = TFI->getStackAlignment();
2793   uint64_t RequiredAlign = std::max(AlignVal, StackAlign);
2794   uint64_t ExtraAlignSpace = RequiredAlign - StackAlign;
2795 
2796   unsigned SPReg = getStackPointerRegisterToSaveRestore();
2797   SDValue NeededSpace = Size;
2798 
2799   // Get a reference to the stack pointer.
2800   SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i64);
2801 
2802   // Add extra space for alignment if needed.
2803   if (ExtraAlignSpace)
2804     NeededSpace = DAG.getNode(ISD::ADD, DL, MVT::i64, NeededSpace,
2805                               DAG.getConstant(ExtraAlignSpace, DL, MVT::i64));
2806 
2807   // Get the new stack pointer value.
2808   SDValue NewSP = DAG.getNode(ISD::SUB, DL, MVT::i64, OldSP, NeededSpace);
2809 
2810   // Copy the new stack pointer back.
2811   Chain = DAG.getCopyToReg(Chain, DL, SPReg, NewSP);
2812 
2813   // The allocated data lives above the 160 bytes allocated for the standard
2814   // frame, plus any outgoing stack arguments.  We don't know how much that
2815   // amounts to yet, so emit a special ADJDYNALLOC placeholder.
2816   SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
2817   SDValue Result = DAG.getNode(ISD::ADD, DL, MVT::i64, NewSP, ArgAdjust);
2818 
2819   // Dynamically realign if needed.
2820   if (RequiredAlign > StackAlign) {
2821     Result =
2822       DAG.getNode(ISD::ADD, DL, MVT::i64, Result,
2823                   DAG.getConstant(ExtraAlignSpace, DL, MVT::i64));
2824     Result =
2825       DAG.getNode(ISD::AND, DL, MVT::i64, Result,
2826                   DAG.getConstant(~(RequiredAlign - 1), DL, MVT::i64));
2827   }
2828 
2829   SDValue Ops[2] = { Result, Chain };
2830   return DAG.getMergeValues(Ops, DL);
2831 }
2832 
2833 SDValue SystemZTargetLowering::lowerSMUL_LOHI(SDValue Op,
2834                                               SelectionDAG &DAG) const {
2835   EVT VT = Op.getValueType();
2836   SDLoc DL(Op);
2837   SDValue Ops[2];
2838   if (is32Bit(VT))
2839     // Just do a normal 64-bit multiplication and extract the results.
2840     // We define this so that it can be used for constant division.
2841     lowerMUL_LOHI32(DAG, DL, ISD::SIGN_EXTEND, Op.getOperand(0),
2842                     Op.getOperand(1), Ops[1], Ops[0]);
2843   else {
2844     // Do a full 128-bit multiplication based on UMUL_LOHI64:
2845     //
2846     //   (ll * rl) + ((lh * rl) << 64) + ((ll * rh) << 64)
2847     //
2848     // but using the fact that the upper halves are either all zeros
2849     // or all ones:
2850     //
2851     //   (ll * rl) - ((lh & rl) << 64) - ((ll & rh) << 64)
2852     //
2853     // and grouping the right terms together since they are quicker than the
2854     // multiplication:
2855     //
2856     //   (ll * rl) - (((lh & rl) + (ll & rh)) << 64)
2857     SDValue C63 = DAG.getConstant(63, DL, MVT::i64);
2858     SDValue LL = Op.getOperand(0);
2859     SDValue RL = Op.getOperand(1);
2860     SDValue LH = DAG.getNode(ISD::SRA, DL, VT, LL, C63);
2861     SDValue RH = DAG.getNode(ISD::SRA, DL, VT, RL, C63);
2862     // UMUL_LOHI64 returns the low result in the odd register and the high
2863     // result in the even register.  SMUL_LOHI is defined to return the
2864     // low half first, so the results are in reverse order.
2865     lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
2866                      LL, RL, Ops[1], Ops[0]);
2867     SDValue NegLLTimesRH = DAG.getNode(ISD::AND, DL, VT, LL, RH);
2868     SDValue NegLHTimesRL = DAG.getNode(ISD::AND, DL, VT, LH, RL);
2869     SDValue NegSum = DAG.getNode(ISD::ADD, DL, VT, NegLLTimesRH, NegLHTimesRL);
2870     Ops[1] = DAG.getNode(ISD::SUB, DL, VT, Ops[1], NegSum);
2871   }
2872   return DAG.getMergeValues(Ops, DL);
2873 }
2874 
2875 SDValue SystemZTargetLowering::lowerUMUL_LOHI(SDValue Op,
2876                                               SelectionDAG &DAG) const {
2877   EVT VT = Op.getValueType();
2878   SDLoc DL(Op);
2879   SDValue Ops[2];
2880   if (is32Bit(VT))
2881     // Just do a normal 64-bit multiplication and extract the results.
2882     // We define this so that it can be used for constant division.
2883     lowerMUL_LOHI32(DAG, DL, ISD::ZERO_EXTEND, Op.getOperand(0),
2884                     Op.getOperand(1), Ops[1], Ops[0]);
2885   else
2886     // UMUL_LOHI64 returns the low result in the odd register and the high
2887     // result in the even register.  UMUL_LOHI is defined to return the
2888     // low half first, so the results are in reverse order.
2889     lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
2890                      Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
2891   return DAG.getMergeValues(Ops, DL);
2892 }
2893 
2894 SDValue SystemZTargetLowering::lowerSDIVREM(SDValue Op,
2895                                             SelectionDAG &DAG) const {
2896   SDValue Op0 = Op.getOperand(0);
2897   SDValue Op1 = Op.getOperand(1);
2898   EVT VT = Op.getValueType();
2899   SDLoc DL(Op);
2900   unsigned Opcode;
2901 
2902   // We use DSGF for 32-bit division.
2903   if (is32Bit(VT)) {
2904     Op0 = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, Op0);
2905     Opcode = SystemZISD::SDIVREM32;
2906   } else if (DAG.ComputeNumSignBits(Op1) > 32) {
2907     Op1 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op1);
2908     Opcode = SystemZISD::SDIVREM32;
2909   } else
2910     Opcode = SystemZISD::SDIVREM64;
2911 
2912   // DSG(F) takes a 64-bit dividend, so the even register in the GR128
2913   // input is "don't care".  The instruction returns the remainder in
2914   // the even register and the quotient in the odd register.
2915   SDValue Ops[2];
2916   lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, Opcode,
2917                    Op0, Op1, Ops[1], Ops[0]);
2918   return DAG.getMergeValues(Ops, DL);
2919 }
2920 
2921 SDValue SystemZTargetLowering::lowerUDIVREM(SDValue Op,
2922                                             SelectionDAG &DAG) const {
2923   EVT VT = Op.getValueType();
2924   SDLoc DL(Op);
2925 
2926   // DL(G) uses a double-width dividend, so we need to clear the even
2927   // register in the GR128 input.  The instruction returns the remainder
2928   // in the even register and the quotient in the odd register.
2929   SDValue Ops[2];
2930   if (is32Bit(VT))
2931     lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_32, SystemZISD::UDIVREM32,
2932                      Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
2933   else
2934     lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_64, SystemZISD::UDIVREM64,
2935                      Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
2936   return DAG.getMergeValues(Ops, DL);
2937 }
2938 
2939 SDValue SystemZTargetLowering::lowerOR(SDValue Op, SelectionDAG &DAG) const {
2940   assert(Op.getValueType() == MVT::i64 && "Should be 64-bit operation");
2941 
2942   // Get the known-zero masks for each operand.
2943   SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) };
2944   APInt KnownZero[2], KnownOne[2];
2945   DAG.computeKnownBits(Ops[0], KnownZero[0], KnownOne[0]);
2946   DAG.computeKnownBits(Ops[1], KnownZero[1], KnownOne[1]);
2947 
2948   // See if the upper 32 bits of one operand and the lower 32 bits of the
2949   // other are known zero.  They are the low and high operands respectively.
2950   uint64_t Masks[] = { KnownZero[0].getZExtValue(),
2951                        KnownZero[1].getZExtValue() };
2952   unsigned High, Low;
2953   if ((Masks[0] >> 32) == 0xffffffff && uint32_t(Masks[1]) == 0xffffffff)
2954     High = 1, Low = 0;
2955   else if ((Masks[1] >> 32) == 0xffffffff && uint32_t(Masks[0]) == 0xffffffff)
2956     High = 0, Low = 1;
2957   else
2958     return Op;
2959 
2960   SDValue LowOp = Ops[Low];
2961   SDValue HighOp = Ops[High];
2962 
2963   // If the high part is a constant, we're better off using IILH.
2964   if (HighOp.getOpcode() == ISD::Constant)
2965     return Op;
2966 
2967   // If the low part is a constant that is outside the range of LHI,
2968   // then we're better off using IILF.
2969   if (LowOp.getOpcode() == ISD::Constant) {
2970     int64_t Value = int32_t(cast<ConstantSDNode>(LowOp)->getZExtValue());
2971     if (!isInt<16>(Value))
2972       return Op;
2973   }
2974 
2975   // Check whether the high part is an AND that doesn't change the
2976   // high 32 bits and just masks out low bits.  We can skip it if so.
2977   if (HighOp.getOpcode() == ISD::AND &&
2978       HighOp.getOperand(1).getOpcode() == ISD::Constant) {
2979     SDValue HighOp0 = HighOp.getOperand(0);
2980     uint64_t Mask = cast<ConstantSDNode>(HighOp.getOperand(1))->getZExtValue();
2981     if (DAG.MaskedValueIsZero(HighOp0, APInt(64, ~(Mask | 0xffffffff))))
2982       HighOp = HighOp0;
2983   }
2984 
2985   // Take advantage of the fact that all GR32 operations only change the
2986   // low 32 bits by truncating Low to an i32 and inserting it directly
2987   // using a subreg.  The interesting cases are those where the truncation
2988   // can be folded.
2989   SDLoc DL(Op);
2990   SDValue Low32 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, LowOp);
2991   return DAG.getTargetInsertSubreg(SystemZ::subreg_l32, DL,
2992                                    MVT::i64, HighOp, Low32);
2993 }
2994 
2995 SDValue SystemZTargetLowering::lowerCTPOP(SDValue Op,
2996                                           SelectionDAG &DAG) const {
2997   EVT VT = Op.getValueType();
2998   SDLoc DL(Op);
2999   Op = Op.getOperand(0);
3000 
3001   // Handle vector types via VPOPCT.
3002   if (VT.isVector()) {
3003     Op = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Op);
3004     Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::v16i8, Op);
3005     switch (VT.getVectorElementType().getSizeInBits()) {
3006     case 8:
3007       break;
3008     case 16: {
3009       Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
3010       SDValue Shift = DAG.getConstant(8, DL, MVT::i32);
3011       SDValue Tmp = DAG.getNode(SystemZISD::VSHL_BY_SCALAR, DL, VT, Op, Shift);
3012       Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
3013       Op = DAG.getNode(SystemZISD::VSRL_BY_SCALAR, DL, VT, Op, Shift);
3014       break;
3015     }
3016     case 32: {
3017       SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
3018                                 DAG.getConstant(0, DL, MVT::i32));
3019       Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
3020       break;
3021     }
3022     case 64: {
3023       SDValue Tmp = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
3024                                 DAG.getConstant(0, DL, MVT::i32));
3025       Op = DAG.getNode(SystemZISD::VSUM, DL, MVT::v4i32, Op, Tmp);
3026       Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
3027       break;
3028     }
3029     default:
3030       llvm_unreachable("Unexpected type");
3031     }
3032     return Op;
3033   }
3034 
3035   // Get the known-zero mask for the operand.
3036   APInt KnownZero, KnownOne;
3037   DAG.computeKnownBits(Op, KnownZero, KnownOne);
3038   unsigned NumSignificantBits = (~KnownZero).getActiveBits();
3039   if (NumSignificantBits == 0)
3040     return DAG.getConstant(0, DL, VT);
3041 
3042   // Skip known-zero high parts of the operand.
3043   int64_t OrigBitSize = VT.getSizeInBits();
3044   int64_t BitSize = (int64_t)1 << Log2_32_Ceil(NumSignificantBits);
3045   BitSize = std::min(BitSize, OrigBitSize);
3046 
3047   // The POPCNT instruction counts the number of bits in each byte.
3048   Op = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op);
3049   Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::i64, Op);
3050   Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
3051 
3052   // Add up per-byte counts in a binary tree.  All bits of Op at
3053   // position larger than BitSize remain zero throughout.
3054   for (int64_t I = BitSize / 2; I >= 8; I = I / 2) {
3055     SDValue Tmp = DAG.getNode(ISD::SHL, DL, VT, Op, DAG.getConstant(I, DL, VT));
3056     if (BitSize != OrigBitSize)
3057       Tmp = DAG.getNode(ISD::AND, DL, VT, Tmp,
3058                         DAG.getConstant(((uint64_t)1 << BitSize) - 1, DL, VT));
3059     Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
3060   }
3061 
3062   // Extract overall result from high byte.
3063   if (BitSize > 8)
3064     Op = DAG.getNode(ISD::SRL, DL, VT, Op,
3065                      DAG.getConstant(BitSize - 8, DL, VT));
3066 
3067   return Op;
3068 }
3069 
3070 // Op is an atomic load.  Lower it into a normal volatile load.
3071 SDValue SystemZTargetLowering::lowerATOMIC_LOAD(SDValue Op,
3072                                                 SelectionDAG &DAG) const {
3073   auto *Node = cast<AtomicSDNode>(Op.getNode());
3074   return DAG.getExtLoad(ISD::EXTLOAD, SDLoc(Op), Op.getValueType(),
3075                         Node->getChain(), Node->getBasePtr(),
3076                         Node->getMemoryVT(), Node->getMemOperand());
3077 }
3078 
3079 // Op is an atomic store.  Lower it into a normal volatile store followed
3080 // by a serialization.
3081 SDValue SystemZTargetLowering::lowerATOMIC_STORE(SDValue Op,
3082                                                  SelectionDAG &DAG) const {
3083   auto *Node = cast<AtomicSDNode>(Op.getNode());
3084   SDValue Chain = DAG.getTruncStore(Node->getChain(), SDLoc(Op), Node->getVal(),
3085                                     Node->getBasePtr(), Node->getMemoryVT(),
3086                                     Node->getMemOperand());
3087   return SDValue(DAG.getMachineNode(SystemZ::Serialize, SDLoc(Op), MVT::Other,
3088                                     Chain), 0);
3089 }
3090 
3091 // Op is an 8-, 16-bit or 32-bit ATOMIC_LOAD_* operation.  Lower the first
3092 // two into the fullword ATOMIC_LOADW_* operation given by Opcode.
3093 SDValue SystemZTargetLowering::lowerATOMIC_LOAD_OP(SDValue Op,
3094                                                    SelectionDAG &DAG,
3095                                                    unsigned Opcode) const {
3096   auto *Node = cast<AtomicSDNode>(Op.getNode());
3097 
3098   // 32-bit operations need no code outside the main loop.
3099   EVT NarrowVT = Node->getMemoryVT();
3100   EVT WideVT = MVT::i32;
3101   if (NarrowVT == WideVT)
3102     return Op;
3103 
3104   int64_t BitSize = NarrowVT.getSizeInBits();
3105   SDValue ChainIn = Node->getChain();
3106   SDValue Addr = Node->getBasePtr();
3107   SDValue Src2 = Node->getVal();
3108   MachineMemOperand *MMO = Node->getMemOperand();
3109   SDLoc DL(Node);
3110   EVT PtrVT = Addr.getValueType();
3111 
3112   // Convert atomic subtracts of constants into additions.
3113   if (Opcode == SystemZISD::ATOMIC_LOADW_SUB)
3114     if (auto *Const = dyn_cast<ConstantSDNode>(Src2)) {
3115       Opcode = SystemZISD::ATOMIC_LOADW_ADD;
3116       Src2 = DAG.getConstant(-Const->getSExtValue(), DL, Src2.getValueType());
3117     }
3118 
3119   // Get the address of the containing word.
3120   SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
3121                                     DAG.getConstant(-4, DL, PtrVT));
3122 
3123   // Get the number of bits that the word must be rotated left in order
3124   // to bring the field to the top bits of a GR32.
3125   SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
3126                                  DAG.getConstant(3, DL, PtrVT));
3127   BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
3128 
3129   // Get the complementing shift amount, for rotating a field in the top
3130   // bits back to its proper position.
3131   SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
3132                                     DAG.getConstant(0, DL, WideVT), BitShift);
3133 
3134   // Extend the source operand to 32 bits and prepare it for the inner loop.
3135   // ATOMIC_SWAPW uses RISBG to rotate the field left, but all other
3136   // operations require the source to be shifted in advance.  (This shift
3137   // can be folded if the source is constant.)  For AND and NAND, the lower
3138   // bits must be set, while for other opcodes they should be left clear.
3139   if (Opcode != SystemZISD::ATOMIC_SWAPW)
3140     Src2 = DAG.getNode(ISD::SHL, DL, WideVT, Src2,
3141                        DAG.getConstant(32 - BitSize, DL, WideVT));
3142   if (Opcode == SystemZISD::ATOMIC_LOADW_AND ||
3143       Opcode == SystemZISD::ATOMIC_LOADW_NAND)
3144     Src2 = DAG.getNode(ISD::OR, DL, WideVT, Src2,
3145                        DAG.getConstant(uint32_t(-1) >> BitSize, DL, WideVT));
3146 
3147   // Construct the ATOMIC_LOADW_* node.
3148   SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
3149   SDValue Ops[] = { ChainIn, AlignedAddr, Src2, BitShift, NegBitShift,
3150                     DAG.getConstant(BitSize, DL, WideVT) };
3151   SDValue AtomicOp = DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops,
3152                                              NarrowVT, MMO);
3153 
3154   // Rotate the result of the final CS so that the field is in the lower
3155   // bits of a GR32, then truncate it.
3156   SDValue ResultShift = DAG.getNode(ISD::ADD, DL, WideVT, BitShift,
3157                                     DAG.getConstant(BitSize, DL, WideVT));
3158   SDValue Result = DAG.getNode(ISD::ROTL, DL, WideVT, AtomicOp, ResultShift);
3159 
3160   SDValue RetOps[2] = { Result, AtomicOp.getValue(1) };
3161   return DAG.getMergeValues(RetOps, DL);
3162 }
3163 
3164 // Op is an ATOMIC_LOAD_SUB operation.  Lower 8- and 16-bit operations
3165 // into ATOMIC_LOADW_SUBs and decide whether to convert 32- and 64-bit
3166 // operations into additions.
3167 SDValue SystemZTargetLowering::lowerATOMIC_LOAD_SUB(SDValue Op,
3168                                                     SelectionDAG &DAG) const {
3169   auto *Node = cast<AtomicSDNode>(Op.getNode());
3170   EVT MemVT = Node->getMemoryVT();
3171   if (MemVT == MVT::i32 || MemVT == MVT::i64) {
3172     // A full-width operation.
3173     assert(Op.getValueType() == MemVT && "Mismatched VTs");
3174     SDValue Src2 = Node->getVal();
3175     SDValue NegSrc2;
3176     SDLoc DL(Src2);
3177 
3178     if (auto *Op2 = dyn_cast<ConstantSDNode>(Src2)) {
3179       // Use an addition if the operand is constant and either LAA(G) is
3180       // available or the negative value is in the range of A(G)FHI.
3181       int64_t Value = (-Op2->getAPIntValue()).getSExtValue();
3182       if (isInt<32>(Value) || Subtarget.hasInterlockedAccess1())
3183         NegSrc2 = DAG.getConstant(Value, DL, MemVT);
3184     } else if (Subtarget.hasInterlockedAccess1())
3185       // Use LAA(G) if available.
3186       NegSrc2 = DAG.getNode(ISD::SUB, DL, MemVT, DAG.getConstant(0, DL, MemVT),
3187                             Src2);
3188 
3189     if (NegSrc2.getNode())
3190       return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, MemVT,
3191                            Node->getChain(), Node->getBasePtr(), NegSrc2,
3192                            Node->getMemOperand(), Node->getOrdering(),
3193                            Node->getSynchScope());
3194 
3195     // Use the node as-is.
3196     return Op;
3197   }
3198 
3199   return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_SUB);
3200 }
3201 
3202 // Node is an 8- or 16-bit ATOMIC_CMP_SWAP operation.  Lower the first two
3203 // into a fullword ATOMIC_CMP_SWAPW operation.
3204 SDValue SystemZTargetLowering::lowerATOMIC_CMP_SWAP(SDValue Op,
3205                                                     SelectionDAG &DAG) const {
3206   auto *Node = cast<AtomicSDNode>(Op.getNode());
3207 
3208   // We have native support for 32-bit compare and swap.
3209   EVT NarrowVT = Node->getMemoryVT();
3210   EVT WideVT = MVT::i32;
3211   if (NarrowVT == WideVT)
3212     return Op;
3213 
3214   int64_t BitSize = NarrowVT.getSizeInBits();
3215   SDValue ChainIn = Node->getOperand(0);
3216   SDValue Addr = Node->getOperand(1);
3217   SDValue CmpVal = Node->getOperand(2);
3218   SDValue SwapVal = Node->getOperand(3);
3219   MachineMemOperand *MMO = Node->getMemOperand();
3220   SDLoc DL(Node);
3221   EVT PtrVT = Addr.getValueType();
3222 
3223   // Get the address of the containing word.
3224   SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
3225                                     DAG.getConstant(-4, DL, PtrVT));
3226 
3227   // Get the number of bits that the word must be rotated left in order
3228   // to bring the field to the top bits of a GR32.
3229   SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
3230                                  DAG.getConstant(3, DL, PtrVT));
3231   BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
3232 
3233   // Get the complementing shift amount, for rotating a field in the top
3234   // bits back to its proper position.
3235   SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
3236                                     DAG.getConstant(0, DL, WideVT), BitShift);
3237 
3238   // Construct the ATOMIC_CMP_SWAPW node.
3239   SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
3240   SDValue Ops[] = { ChainIn, AlignedAddr, CmpVal, SwapVal, BitShift,
3241                     NegBitShift, DAG.getConstant(BitSize, DL, WideVT) };
3242   SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAPW, DL,
3243                                              VTList, Ops, NarrowVT, MMO);
3244   return AtomicOp;
3245 }
3246 
3247 SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op,
3248                                               SelectionDAG &DAG) const {
3249   MachineFunction &MF = DAG.getMachineFunction();
3250   MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
3251   return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op),
3252                             SystemZ::R15D, Op.getValueType());
3253 }
3254 
3255 SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op,
3256                                                  SelectionDAG &DAG) const {
3257   MachineFunction &MF = DAG.getMachineFunction();
3258   MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
3259   return DAG.getCopyToReg(Op.getOperand(0), SDLoc(Op),
3260                           SystemZ::R15D, Op.getOperand(1));
3261 }
3262 
3263 SDValue SystemZTargetLowering::lowerPREFETCH(SDValue Op,
3264                                              SelectionDAG &DAG) const {
3265   bool IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
3266   if (!IsData)
3267     // Just preserve the chain.
3268     return Op.getOperand(0);
3269 
3270   SDLoc DL(Op);
3271   bool IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
3272   unsigned Code = IsWrite ? SystemZ::PFD_WRITE : SystemZ::PFD_READ;
3273   auto *Node = cast<MemIntrinsicSDNode>(Op.getNode());
3274   SDValue Ops[] = {
3275     Op.getOperand(0),
3276     DAG.getConstant(Code, DL, MVT::i32),
3277     Op.getOperand(1)
3278   };
3279   return DAG.getMemIntrinsicNode(SystemZISD::PREFETCH, DL,
3280                                  Node->getVTList(), Ops,
3281                                  Node->getMemoryVT(), Node->getMemOperand());
3282 }
3283 
3284 // Return an i32 that contains the value of CC immediately after After,
3285 // whose final operand must be MVT::Glue.
3286 static SDValue getCCResult(SelectionDAG &DAG, SDNode *After) {
3287   SDLoc DL(After);
3288   SDValue Glue = SDValue(After, After->getNumValues() - 1);
3289   SDValue IPM = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
3290   return DAG.getNode(ISD::SRL, DL, MVT::i32, IPM,
3291                      DAG.getConstant(SystemZ::IPM_CC, DL, MVT::i32));
3292 }
3293 
3294 SDValue
3295 SystemZTargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
3296                                               SelectionDAG &DAG) const {
3297   unsigned Opcode, CCValid;
3298   if (isIntrinsicWithCCAndChain(Op, Opcode, CCValid)) {
3299     assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
3300     SDValue Glued = emitIntrinsicWithChainAndGlue(DAG, Op, Opcode);
3301     SDValue CC = getCCResult(DAG, Glued.getNode());
3302     DAG.ReplaceAllUsesOfValueWith(SDValue(Op.getNode(), 0), CC);
3303     return SDValue();
3304   }
3305 
3306   return SDValue();
3307 }
3308 
3309 SDValue
3310 SystemZTargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
3311                                                SelectionDAG &DAG) const {
3312   unsigned Opcode, CCValid;
3313   if (isIntrinsicWithCC(Op, Opcode, CCValid)) {
3314     SDValue Glued = emitIntrinsicWithGlue(DAG, Op, Opcode);
3315     SDValue CC = getCCResult(DAG, Glued.getNode());
3316     if (Op->getNumValues() == 1)
3317       return CC;
3318     assert(Op->getNumValues() == 2 && "Expected a CC and non-CC result");
3319     return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), Op->getVTList(), Glued,
3320                        CC);
3321   }
3322 
3323   unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
3324   switch (Id) {
3325   case Intrinsic::s390_vpdi:
3326     return DAG.getNode(SystemZISD::PERMUTE_DWORDS, SDLoc(Op), Op.getValueType(),
3327                        Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
3328 
3329   case Intrinsic::s390_vperm:
3330     return DAG.getNode(SystemZISD::PERMUTE, SDLoc(Op), Op.getValueType(),
3331                        Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
3332 
3333   case Intrinsic::s390_vuphb:
3334   case Intrinsic::s390_vuphh:
3335   case Intrinsic::s390_vuphf:
3336     return DAG.getNode(SystemZISD::UNPACK_HIGH, SDLoc(Op), Op.getValueType(),
3337                        Op.getOperand(1));
3338 
3339   case Intrinsic::s390_vuplhb:
3340   case Intrinsic::s390_vuplhh:
3341   case Intrinsic::s390_vuplhf:
3342     return DAG.getNode(SystemZISD::UNPACKL_HIGH, SDLoc(Op), Op.getValueType(),
3343                        Op.getOperand(1));
3344 
3345   case Intrinsic::s390_vuplb:
3346   case Intrinsic::s390_vuplhw:
3347   case Intrinsic::s390_vuplf:
3348     return DAG.getNode(SystemZISD::UNPACK_LOW, SDLoc(Op), Op.getValueType(),
3349                        Op.getOperand(1));
3350 
3351   case Intrinsic::s390_vupllb:
3352   case Intrinsic::s390_vupllh:
3353   case Intrinsic::s390_vupllf:
3354     return DAG.getNode(SystemZISD::UNPACKL_LOW, SDLoc(Op), Op.getValueType(),
3355                        Op.getOperand(1));
3356 
3357   case Intrinsic::s390_vsumb:
3358   case Intrinsic::s390_vsumh:
3359   case Intrinsic::s390_vsumgh:
3360   case Intrinsic::s390_vsumgf:
3361   case Intrinsic::s390_vsumqf:
3362   case Intrinsic::s390_vsumqg:
3363     return DAG.getNode(SystemZISD::VSUM, SDLoc(Op), Op.getValueType(),
3364                        Op.getOperand(1), Op.getOperand(2));
3365   }
3366 
3367   return SDValue();
3368 }
3369 
3370 namespace {
3371 // Says that SystemZISD operation Opcode can be used to perform the equivalent
3372 // of a VPERM with permute vector Bytes.  If Opcode takes three operands,
3373 // Operand is the constant third operand, otherwise it is the number of
3374 // bytes in each element of the result.
3375 struct Permute {
3376   unsigned Opcode;
3377   unsigned Operand;
3378   unsigned char Bytes[SystemZ::VectorBytes];
3379 };
3380 }
3381 
3382 static const Permute PermuteForms[] = {
3383   // VMRHG
3384   { SystemZISD::MERGE_HIGH, 8,
3385     { 0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23 } },
3386   // VMRHF
3387   { SystemZISD::MERGE_HIGH, 4,
3388     { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } },
3389   // VMRHH
3390   { SystemZISD::MERGE_HIGH, 2,
3391     { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } },
3392   // VMRHB
3393   { SystemZISD::MERGE_HIGH, 1,
3394     { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } },
3395   // VMRLG
3396   { SystemZISD::MERGE_LOW, 8,
3397     { 8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31 } },
3398   // VMRLF
3399   { SystemZISD::MERGE_LOW, 4,
3400     { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } },
3401   // VMRLH
3402   { SystemZISD::MERGE_LOW, 2,
3403     { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
3404   // VMRLB
3405   { SystemZISD::MERGE_LOW, 1,
3406     { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
3407   // VPKG
3408   { SystemZISD::PACK, 4,
3409     { 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31 } },
3410   // VPKF
3411   { SystemZISD::PACK, 2,
3412     { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
3413   // VPKH
3414   { SystemZISD::PACK, 1,
3415     { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
3416   // VPDI V1, V2, 4  (low half of V1, high half of V2)
3417   { SystemZISD::PERMUTE_DWORDS, 4,
3418     { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 } },
3419   // VPDI V1, V2, 1  (high half of V1, low half of V2)
3420   { SystemZISD::PERMUTE_DWORDS, 1,
3421     { 0, 1, 2, 3, 4, 5, 6, 7, 24, 25, 26, 27, 28, 29, 30, 31 } }
3422 };
3423 
3424 // Called after matching a vector shuffle against a particular pattern.
3425 // Both the original shuffle and the pattern have two vector operands.
3426 // OpNos[0] is the operand of the original shuffle that should be used for
3427 // operand 0 of the pattern, or -1 if operand 0 of the pattern can be anything.
3428 // OpNos[1] is the same for operand 1 of the pattern.  Resolve these -1s and
3429 // set OpNo0 and OpNo1 to the shuffle operands that should actually be used
3430 // for operands 0 and 1 of the pattern.
3431 static bool chooseShuffleOpNos(int *OpNos, unsigned &OpNo0, unsigned &OpNo1) {
3432   if (OpNos[0] < 0) {
3433     if (OpNos[1] < 0)
3434       return false;
3435     OpNo0 = OpNo1 = OpNos[1];
3436   } else if (OpNos[1] < 0) {
3437     OpNo0 = OpNo1 = OpNos[0];
3438   } else {
3439     OpNo0 = OpNos[0];
3440     OpNo1 = OpNos[1];
3441   }
3442   return true;
3443 }
3444 
3445 // Bytes is a VPERM-like permute vector, except that -1 is used for
3446 // undefined bytes.  Return true if the VPERM can be implemented using P.
3447 // When returning true set OpNo0 to the VPERM operand that should be
3448 // used for operand 0 of P and likewise OpNo1 for operand 1 of P.
3449 //
3450 // For example, if swapping the VPERM operands allows P to match, OpNo0
3451 // will be 1 and OpNo1 will be 0.  If instead Bytes only refers to one
3452 // operand, but rewriting it to use two duplicated operands allows it to
3453 // match P, then OpNo0 and OpNo1 will be the same.
3454 static bool matchPermute(const SmallVectorImpl<int> &Bytes, const Permute &P,
3455                          unsigned &OpNo0, unsigned &OpNo1) {
3456   int OpNos[] = { -1, -1 };
3457   for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) {
3458     int Elt = Bytes[I];
3459     if (Elt >= 0) {
3460       // Make sure that the two permute vectors use the same suboperand
3461       // byte number.  Only the operand numbers (the high bits) are
3462       // allowed to differ.
3463       if ((Elt ^ P.Bytes[I]) & (SystemZ::VectorBytes - 1))
3464         return false;
3465       int ModelOpNo = P.Bytes[I] / SystemZ::VectorBytes;
3466       int RealOpNo = unsigned(Elt) / SystemZ::VectorBytes;
3467       // Make sure that the operand mappings are consistent with previous
3468       // elements.
3469       if (OpNos[ModelOpNo] == 1 - RealOpNo)
3470         return false;
3471       OpNos[ModelOpNo] = RealOpNo;
3472     }
3473   }
3474   return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
3475 }
3476 
3477 // As above, but search for a matching permute.
3478 static const Permute *matchPermute(const SmallVectorImpl<int> &Bytes,
3479                                    unsigned &OpNo0, unsigned &OpNo1) {
3480   for (auto &P : PermuteForms)
3481     if (matchPermute(Bytes, P, OpNo0, OpNo1))
3482       return &P;
3483   return nullptr;
3484 }
3485 
3486 // Bytes is a VPERM-like permute vector, except that -1 is used for
3487 // undefined bytes.  This permute is an operand of an outer permute.
3488 // See whether redistributing the -1 bytes gives a shuffle that can be
3489 // implemented using P.  If so, set Transform to a VPERM-like permute vector
3490 // that, when applied to the result of P, gives the original permute in Bytes.
3491 static bool matchDoublePermute(const SmallVectorImpl<int> &Bytes,
3492                                const Permute &P,
3493                                SmallVectorImpl<int> &Transform) {
3494   unsigned To = 0;
3495   for (unsigned From = 0; From < SystemZ::VectorBytes; ++From) {
3496     int Elt = Bytes[From];
3497     if (Elt < 0)
3498       // Byte number From of the result is undefined.
3499       Transform[From] = -1;
3500     else {
3501       while (P.Bytes[To] != Elt) {
3502         To += 1;
3503         if (To == SystemZ::VectorBytes)
3504           return false;
3505       }
3506       Transform[From] = To;
3507     }
3508   }
3509   return true;
3510 }
3511 
3512 // As above, but search for a matching permute.
3513 static const Permute *matchDoublePermute(const SmallVectorImpl<int> &Bytes,
3514                                          SmallVectorImpl<int> &Transform) {
3515   for (auto &P : PermuteForms)
3516     if (matchDoublePermute(Bytes, P, Transform))
3517       return &P;
3518   return nullptr;
3519 }
3520 
3521 // Convert the mask of the given VECTOR_SHUFFLE into a byte-level mask,
3522 // as if it had type vNi8.
3523 static void getVPermMask(ShuffleVectorSDNode *VSN,
3524                          SmallVectorImpl<int> &Bytes) {
3525   EVT VT = VSN->getValueType(0);
3526   unsigned NumElements = VT.getVectorNumElements();
3527   unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3528   Bytes.resize(NumElements * BytesPerElement, -1);
3529   for (unsigned I = 0; I < NumElements; ++I) {
3530     int Index = VSN->getMaskElt(I);
3531     if (Index >= 0)
3532       for (unsigned J = 0; J < BytesPerElement; ++J)
3533         Bytes[I * BytesPerElement + J] = Index * BytesPerElement + J;
3534   }
3535 }
3536 
3537 // Bytes is a VPERM-like permute vector, except that -1 is used for
3538 // undefined bytes.  See whether bytes [Start, Start + BytesPerElement) of
3539 // the result come from a contiguous sequence of bytes from one input.
3540 // Set Base to the selector for the first byte if so.
3541 static bool getShuffleInput(const SmallVectorImpl<int> &Bytes, unsigned Start,
3542                             unsigned BytesPerElement, int &Base) {
3543   Base = -1;
3544   for (unsigned I = 0; I < BytesPerElement; ++I) {
3545     if (Bytes[Start + I] >= 0) {
3546       unsigned Elem = Bytes[Start + I];
3547       if (Base < 0) {
3548         Base = Elem - I;
3549         // Make sure the bytes would come from one input operand.
3550         if (unsigned(Base) % Bytes.size() + BytesPerElement > Bytes.size())
3551           return false;
3552       } else if (unsigned(Base) != Elem - I)
3553         return false;
3554     }
3555   }
3556   return true;
3557 }
3558 
3559 // Bytes is a VPERM-like permute vector, except that -1 is used for
3560 // undefined bytes.  Return true if it can be performed using VSLDI.
3561 // When returning true, set StartIndex to the shift amount and OpNo0
3562 // and OpNo1 to the VPERM operands that should be used as the first
3563 // and second shift operand respectively.
3564 static bool isShlDoublePermute(const SmallVectorImpl<int> &Bytes,
3565                                unsigned &StartIndex, unsigned &OpNo0,
3566                                unsigned &OpNo1) {
3567   int OpNos[] = { -1, -1 };
3568   int Shift = -1;
3569   for (unsigned I = 0; I < 16; ++I) {
3570     int Index = Bytes[I];
3571     if (Index >= 0) {
3572       int ExpectedShift = (Index - I) % SystemZ::VectorBytes;
3573       int ModelOpNo = unsigned(ExpectedShift + I) / SystemZ::VectorBytes;
3574       int RealOpNo = unsigned(Index) / SystemZ::VectorBytes;
3575       if (Shift < 0)
3576         Shift = ExpectedShift;
3577       else if (Shift != ExpectedShift)
3578         return false;
3579       // Make sure that the operand mappings are consistent with previous
3580       // elements.
3581       if (OpNos[ModelOpNo] == 1 - RealOpNo)
3582         return false;
3583       OpNos[ModelOpNo] = RealOpNo;
3584     }
3585   }
3586   StartIndex = Shift;
3587   return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
3588 }
3589 
3590 // Create a node that performs P on operands Op0 and Op1, casting the
3591 // operands to the appropriate type.  The type of the result is determined by P.
3592 static SDValue getPermuteNode(SelectionDAG &DAG, SDLoc DL,
3593                               const Permute &P, SDValue Op0, SDValue Op1) {
3594   // VPDI (PERMUTE_DWORDS) always operates on v2i64s.  The input
3595   // elements of a PACK are twice as wide as the outputs.
3596   unsigned InBytes = (P.Opcode == SystemZISD::PERMUTE_DWORDS ? 8 :
3597                       P.Opcode == SystemZISD::PACK ? P.Operand * 2 :
3598                       P.Operand);
3599   // Cast both operands to the appropriate type.
3600   MVT InVT = MVT::getVectorVT(MVT::getIntegerVT(InBytes * 8),
3601                               SystemZ::VectorBytes / InBytes);
3602   Op0 = DAG.getNode(ISD::BITCAST, DL, InVT, Op0);
3603   Op1 = DAG.getNode(ISD::BITCAST, DL, InVT, Op1);
3604   SDValue Op;
3605   if (P.Opcode == SystemZISD::PERMUTE_DWORDS) {
3606     SDValue Op2 = DAG.getConstant(P.Operand, DL, MVT::i32);
3607     Op = DAG.getNode(SystemZISD::PERMUTE_DWORDS, DL, InVT, Op0, Op1, Op2);
3608   } else if (P.Opcode == SystemZISD::PACK) {
3609     MVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(P.Operand * 8),
3610                                  SystemZ::VectorBytes / P.Operand);
3611     Op = DAG.getNode(SystemZISD::PACK, DL, OutVT, Op0, Op1);
3612   } else {
3613     Op = DAG.getNode(P.Opcode, DL, InVT, Op0, Op1);
3614   }
3615   return Op;
3616 }
3617 
3618 // Bytes is a VPERM-like permute vector, except that -1 is used for
3619 // undefined bytes.  Implement it on operands Ops[0] and Ops[1] using
3620 // VSLDI or VPERM.
3621 static SDValue getGeneralPermuteNode(SelectionDAG &DAG, SDLoc DL, SDValue *Ops,
3622                                      const SmallVectorImpl<int> &Bytes) {
3623   for (unsigned I = 0; I < 2; ++I)
3624     Ops[I] = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Ops[I]);
3625 
3626   // First see whether VSLDI can be used.
3627   unsigned StartIndex, OpNo0, OpNo1;
3628   if (isShlDoublePermute(Bytes, StartIndex, OpNo0, OpNo1))
3629     return DAG.getNode(SystemZISD::SHL_DOUBLE, DL, MVT::v16i8, Ops[OpNo0],
3630                        Ops[OpNo1], DAG.getConstant(StartIndex, DL, MVT::i32));
3631 
3632   // Fall back on VPERM.  Construct an SDNode for the permute vector.
3633   SDValue IndexNodes[SystemZ::VectorBytes];
3634   for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
3635     if (Bytes[I] >= 0)
3636       IndexNodes[I] = DAG.getConstant(Bytes[I], DL, MVT::i32);
3637     else
3638       IndexNodes[I] = DAG.getUNDEF(MVT::i32);
3639   SDValue Op2 = DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v16i8, IndexNodes);
3640   return DAG.getNode(SystemZISD::PERMUTE, DL, MVT::v16i8, Ops[0], Ops[1], Op2);
3641 }
3642 
3643 namespace {
3644 // Describes a general N-operand vector shuffle.
3645 struct GeneralShuffle {
3646   GeneralShuffle(EVT vt) : VT(vt) {}
3647   void addUndef();
3648   void add(SDValue, unsigned);
3649   SDValue getNode(SelectionDAG &, SDLoc);
3650 
3651   // The operands of the shuffle.
3652   SmallVector<SDValue, SystemZ::VectorBytes> Ops;
3653 
3654   // Index I is -1 if byte I of the result is undefined.  Otherwise the
3655   // result comes from byte Bytes[I] % SystemZ::VectorBytes of operand
3656   // Bytes[I] / SystemZ::VectorBytes.
3657   SmallVector<int, SystemZ::VectorBytes> Bytes;
3658 
3659   // The type of the shuffle result.
3660   EVT VT;
3661 };
3662 }
3663 
3664 // Add an extra undefined element to the shuffle.
3665 void GeneralShuffle::addUndef() {
3666   unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3667   for (unsigned I = 0; I < BytesPerElement; ++I)
3668     Bytes.push_back(-1);
3669 }
3670 
3671 // Add an extra element to the shuffle, taking it from element Elem of Op.
3672 // A null Op indicates a vector input whose value will be calculated later;
3673 // there is at most one such input per shuffle and it always has the same
3674 // type as the result.
3675 void GeneralShuffle::add(SDValue Op, unsigned Elem) {
3676   unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
3677 
3678   // The source vector can have wider elements than the result,
3679   // either through an explicit TRUNCATE or because of type legalization.
3680   // We want the least significant part.
3681   EVT FromVT = Op.getNode() ? Op.getValueType() : VT;
3682   unsigned FromBytesPerElement = FromVT.getVectorElementType().getStoreSize();
3683   assert(FromBytesPerElement >= BytesPerElement &&
3684          "Invalid EXTRACT_VECTOR_ELT");
3685   unsigned Byte = ((Elem * FromBytesPerElement) % SystemZ::VectorBytes +
3686                    (FromBytesPerElement - BytesPerElement));
3687 
3688   // Look through things like shuffles and bitcasts.
3689   while (Op.getNode()) {
3690     if (Op.getOpcode() == ISD::BITCAST)
3691       Op = Op.getOperand(0);
3692     else if (Op.getOpcode() == ISD::VECTOR_SHUFFLE && Op.hasOneUse()) {
3693       // See whether the bytes we need come from a contiguous part of one
3694       // operand.
3695       SmallVector<int, SystemZ::VectorBytes> OpBytes;
3696       getVPermMask(cast<ShuffleVectorSDNode>(Op), OpBytes);
3697       int NewByte;
3698       if (!getShuffleInput(OpBytes, Byte, BytesPerElement, NewByte))
3699         break;
3700       if (NewByte < 0) {
3701         addUndef();
3702         return;
3703       }
3704       Op = Op.getOperand(unsigned(NewByte) / SystemZ::VectorBytes);
3705       Byte = unsigned(NewByte) % SystemZ::VectorBytes;
3706     } else if (Op.getOpcode() == ISD::UNDEF) {
3707       addUndef();
3708       return;
3709     } else
3710       break;
3711   }
3712 
3713   // Make sure that the source of the extraction is in Ops.
3714   unsigned OpNo = 0;
3715   for (; OpNo < Ops.size(); ++OpNo)
3716     if (Ops[OpNo] == Op)
3717       break;
3718   if (OpNo == Ops.size())
3719     Ops.push_back(Op);
3720 
3721   // Add the element to Bytes.
3722   unsigned Base = OpNo * SystemZ::VectorBytes + Byte;
3723   for (unsigned I = 0; I < BytesPerElement; ++I)
3724     Bytes.push_back(Base + I);
3725 }
3726 
3727 // Return SDNodes for the completed shuffle.
3728 SDValue GeneralShuffle::getNode(SelectionDAG &DAG, SDLoc DL) {
3729   assert(Bytes.size() == SystemZ::VectorBytes && "Incomplete vector");
3730 
3731   if (Ops.size() == 0)
3732     return DAG.getUNDEF(VT);
3733 
3734   // Make sure that there are at least two shuffle operands.
3735   if (Ops.size() == 1)
3736     Ops.push_back(DAG.getUNDEF(MVT::v16i8));
3737 
3738   // Create a tree of shuffles, deferring root node until after the loop.
3739   // Try to redistribute the undefined elements of non-root nodes so that
3740   // the non-root shuffles match something like a pack or merge, then adjust
3741   // the parent node's permute vector to compensate for the new order.
3742   // Among other things, this copes with vectors like <2 x i16> that were
3743   // padded with undefined elements during type legalization.
3744   //
3745   // In the best case this redistribution will lead to the whole tree
3746   // using packs and merges.  It should rarely be a loss in other cases.
3747   unsigned Stride = 1;
3748   for (; Stride * 2 < Ops.size(); Stride *= 2) {
3749     for (unsigned I = 0; I < Ops.size() - Stride; I += Stride * 2) {
3750       SDValue SubOps[] = { Ops[I], Ops[I + Stride] };
3751 
3752       // Create a mask for just these two operands.
3753       SmallVector<int, SystemZ::VectorBytes> NewBytes(SystemZ::VectorBytes);
3754       for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
3755         unsigned OpNo = unsigned(Bytes[J]) / SystemZ::VectorBytes;
3756         unsigned Byte = unsigned(Bytes[J]) % SystemZ::VectorBytes;
3757         if (OpNo == I)
3758           NewBytes[J] = Byte;
3759         else if (OpNo == I + Stride)
3760           NewBytes[J] = SystemZ::VectorBytes + Byte;
3761         else
3762           NewBytes[J] = -1;
3763       }
3764       // See if it would be better to reorganize NewMask to avoid using VPERM.
3765       SmallVector<int, SystemZ::VectorBytes> NewBytesMap(SystemZ::VectorBytes);
3766       if (const Permute *P = matchDoublePermute(NewBytes, NewBytesMap)) {
3767         Ops[I] = getPermuteNode(DAG, DL, *P, SubOps[0], SubOps[1]);
3768         // Applying NewBytesMap to Ops[I] gets back to NewBytes.
3769         for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
3770           if (NewBytes[J] >= 0) {
3771             assert(unsigned(NewBytesMap[J]) < SystemZ::VectorBytes &&
3772                    "Invalid double permute");
3773             Bytes[J] = I * SystemZ::VectorBytes + NewBytesMap[J];
3774           } else
3775             assert(NewBytesMap[J] < 0 && "Invalid double permute");
3776         }
3777       } else {
3778         // Just use NewBytes on the operands.
3779         Ops[I] = getGeneralPermuteNode(DAG, DL, SubOps, NewBytes);
3780         for (unsigned J = 0; J < SystemZ::VectorBytes; ++J)
3781           if (NewBytes[J] >= 0)
3782             Bytes[J] = I * SystemZ::VectorBytes + J;
3783       }
3784     }
3785   }
3786 
3787   // Now we just have 2 inputs.  Put the second operand in Ops[1].
3788   if (Stride > 1) {
3789     Ops[1] = Ops[Stride];
3790     for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
3791       if (Bytes[I] >= int(SystemZ::VectorBytes))
3792         Bytes[I] -= (Stride - 1) * SystemZ::VectorBytes;
3793   }
3794 
3795   // Look for an instruction that can do the permute without resorting
3796   // to VPERM.
3797   unsigned OpNo0, OpNo1;
3798   SDValue Op;
3799   if (const Permute *P = matchPermute(Bytes, OpNo0, OpNo1))
3800     Op = getPermuteNode(DAG, DL, *P, Ops[OpNo0], Ops[OpNo1]);
3801   else
3802     Op = getGeneralPermuteNode(DAG, DL, &Ops[0], Bytes);
3803   return DAG.getNode(ISD::BITCAST, DL, VT, Op);
3804 }
3805 
3806 // Return true if the given BUILD_VECTOR is a scalar-to-vector conversion.
3807 static bool isScalarToVector(SDValue Op) {
3808   for (unsigned I = 1, E = Op.getNumOperands(); I != E; ++I)
3809     if (Op.getOperand(I).getOpcode() != ISD::UNDEF)
3810       return false;
3811   return true;
3812 }
3813 
3814 // Return a vector of type VT that contains Value in the first element.
3815 // The other elements don't matter.
3816 static SDValue buildScalarToVector(SelectionDAG &DAG, SDLoc DL, EVT VT,
3817                                    SDValue Value) {
3818   // If we have a constant, replicate it to all elements and let the
3819   // BUILD_VECTOR lowering take care of it.
3820   if (Value.getOpcode() == ISD::Constant ||
3821       Value.getOpcode() == ISD::ConstantFP) {
3822     SmallVector<SDValue, 16> Ops(VT.getVectorNumElements(), Value);
3823     return DAG.getNode(ISD::BUILD_VECTOR, DL, VT, Ops);
3824   }
3825   if (Value.getOpcode() == ISD::UNDEF)
3826     return DAG.getUNDEF(VT);
3827   return DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, Value);
3828 }
3829 
3830 // Return a vector of type VT in which Op0 is in element 0 and Op1 is in
3831 // element 1.  Used for cases in which replication is cheap.
3832 static SDValue buildMergeScalars(SelectionDAG &DAG, SDLoc DL, EVT VT,
3833                                  SDValue Op0, SDValue Op1) {
3834   if (Op0.getOpcode() == ISD::UNDEF) {
3835     if (Op1.getOpcode() == ISD::UNDEF)
3836       return DAG.getUNDEF(VT);
3837     return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op1);
3838   }
3839   if (Op1.getOpcode() == ISD::UNDEF)
3840     return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0);
3841   return DAG.getNode(SystemZISD::MERGE_HIGH, DL, VT,
3842                      buildScalarToVector(DAG, DL, VT, Op0),
3843                      buildScalarToVector(DAG, DL, VT, Op1));
3844 }
3845 
3846 // Extend GPR scalars Op0 and Op1 to doublewords and return a v2i64
3847 // vector for them.
3848 static SDValue joinDwords(SelectionDAG &DAG, SDLoc DL, SDValue Op0,
3849                           SDValue Op1) {
3850   if (Op0.getOpcode() == ISD::UNDEF && Op1.getOpcode() == ISD::UNDEF)
3851     return DAG.getUNDEF(MVT::v2i64);
3852   // If one of the two inputs is undefined then replicate the other one,
3853   // in order to avoid using another register unnecessarily.
3854   if (Op0.getOpcode() == ISD::UNDEF)
3855     Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
3856   else if (Op1.getOpcode() == ISD::UNDEF)
3857     Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
3858   else {
3859     Op0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
3860     Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
3861   }
3862   return DAG.getNode(SystemZISD::JOIN_DWORDS, DL, MVT::v2i64, Op0, Op1);
3863 }
3864 
3865 // Try to represent constant BUILD_VECTOR node BVN using a
3866 // SystemZISD::BYTE_MASK-style mask.  Store the mask value in Mask
3867 // on success.
3868 static bool tryBuildVectorByteMask(BuildVectorSDNode *BVN, uint64_t &Mask) {
3869   EVT ElemVT = BVN->getValueType(0).getVectorElementType();
3870   unsigned BytesPerElement = ElemVT.getStoreSize();
3871   for (unsigned I = 0, E = BVN->getNumOperands(); I != E; ++I) {
3872     SDValue Op = BVN->getOperand(I);
3873     if (Op.getOpcode() != ISD::UNDEF) {
3874       uint64_t Value;
3875       if (Op.getOpcode() == ISD::Constant)
3876         Value = dyn_cast<ConstantSDNode>(Op)->getZExtValue();
3877       else if (Op.getOpcode() == ISD::ConstantFP)
3878         Value = (dyn_cast<ConstantFPSDNode>(Op)->getValueAPF().bitcastToAPInt()
3879                  .getZExtValue());
3880       else
3881         return false;
3882       for (unsigned J = 0; J < BytesPerElement; ++J) {
3883         uint64_t Byte = (Value >> (J * 8)) & 0xff;
3884         if (Byte == 0xff)
3885           Mask |= 1ULL << ((E - I - 1) * BytesPerElement + J);
3886         else if (Byte != 0)
3887           return false;
3888       }
3889     }
3890   }
3891   return true;
3892 }
3893 
3894 // Try to load a vector constant in which BitsPerElement-bit value Value
3895 // is replicated to fill the vector.  VT is the type of the resulting
3896 // constant, which may have elements of a different size from BitsPerElement.
3897 // Return the SDValue of the constant on success, otherwise return
3898 // an empty value.
3899 static SDValue tryBuildVectorReplicate(SelectionDAG &DAG,
3900                                        const SystemZInstrInfo *TII,
3901                                        SDLoc DL, EVT VT, uint64_t Value,
3902                                        unsigned BitsPerElement) {
3903   // Signed 16-bit values can be replicated using VREPI.
3904   int64_t SignedValue = SignExtend64(Value, BitsPerElement);
3905   if (isInt<16>(SignedValue)) {
3906     MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement),
3907                                  SystemZ::VectorBits / BitsPerElement);
3908     SDValue Op = DAG.getNode(SystemZISD::REPLICATE, DL, VecVT,
3909                              DAG.getConstant(SignedValue, DL, MVT::i32));
3910     return DAG.getNode(ISD::BITCAST, DL, VT, Op);
3911   }
3912   // See whether rotating the constant left some N places gives a value that
3913   // is one less than a power of 2 (i.e. all zeros followed by all ones).
3914   // If so we can use VGM.
3915   unsigned Start, End;
3916   if (TII->isRxSBGMask(Value, BitsPerElement, Start, End)) {
3917     // isRxSBGMask returns the bit numbers for a full 64-bit value,
3918     // with 0 denoting 1 << 63 and 63 denoting 1.  Convert them to
3919     // bit numbers for an BitsPerElement value, so that 0 denotes
3920     // 1 << (BitsPerElement-1).
3921     Start -= 64 - BitsPerElement;
3922     End -= 64 - BitsPerElement;
3923     MVT VecVT = MVT::getVectorVT(MVT::getIntegerVT(BitsPerElement),
3924                                  SystemZ::VectorBits / BitsPerElement);
3925     SDValue Op = DAG.getNode(SystemZISD::ROTATE_MASK, DL, VecVT,
3926                              DAG.getConstant(Start, DL, MVT::i32),
3927                              DAG.getConstant(End, DL, MVT::i32));
3928     return DAG.getNode(ISD::BITCAST, DL, VT, Op);
3929   }
3930   return SDValue();
3931 }
3932 
3933 // If a BUILD_VECTOR contains some EXTRACT_VECTOR_ELTs, it's usually
3934 // better to use VECTOR_SHUFFLEs on them, only using BUILD_VECTOR for
3935 // the non-EXTRACT_VECTOR_ELT elements.  See if the given BUILD_VECTOR
3936 // would benefit from this representation and return it if so.
3937 static SDValue tryBuildVectorShuffle(SelectionDAG &DAG,
3938                                      BuildVectorSDNode *BVN) {
3939   EVT VT = BVN->getValueType(0);
3940   unsigned NumElements = VT.getVectorNumElements();
3941 
3942   // Represent the BUILD_VECTOR as an N-operand VECTOR_SHUFFLE-like operation
3943   // on byte vectors.  If there are non-EXTRACT_VECTOR_ELT elements that still
3944   // need a BUILD_VECTOR, add an additional placeholder operand for that
3945   // BUILD_VECTOR and store its operands in ResidueOps.
3946   GeneralShuffle GS(VT);
3947   SmallVector<SDValue, SystemZ::VectorBytes> ResidueOps;
3948   bool FoundOne = false;
3949   for (unsigned I = 0; I < NumElements; ++I) {
3950     SDValue Op = BVN->getOperand(I);
3951     if (Op.getOpcode() == ISD::TRUNCATE)
3952       Op = Op.getOperand(0);
3953     if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
3954         Op.getOperand(1).getOpcode() == ISD::Constant) {
3955       unsigned Elem = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
3956       GS.add(Op.getOperand(0), Elem);
3957       FoundOne = true;
3958     } else if (Op.getOpcode() == ISD::UNDEF) {
3959       GS.addUndef();
3960     } else {
3961       GS.add(SDValue(), ResidueOps.size());
3962       ResidueOps.push_back(BVN->getOperand(I));
3963     }
3964   }
3965 
3966   // Nothing to do if there are no EXTRACT_VECTOR_ELTs.
3967   if (!FoundOne)
3968     return SDValue();
3969 
3970   // Create the BUILD_VECTOR for the remaining elements, if any.
3971   if (!ResidueOps.empty()) {
3972     while (ResidueOps.size() < NumElements)
3973       ResidueOps.push_back(DAG.getUNDEF(ResidueOps[0].getValueType()));
3974     for (auto &Op : GS.Ops) {
3975       if (!Op.getNode()) {
3976         Op = DAG.getNode(ISD::BUILD_VECTOR, SDLoc(BVN), VT, ResidueOps);
3977         break;
3978       }
3979     }
3980   }
3981   return GS.getNode(DAG, SDLoc(BVN));
3982 }
3983 
3984 // Combine GPR scalar values Elems into a vector of type VT.
3985 static SDValue buildVector(SelectionDAG &DAG, SDLoc DL, EVT VT,
3986                            SmallVectorImpl<SDValue> &Elems) {
3987   // See whether there is a single replicated value.
3988   SDValue Single;
3989   unsigned int NumElements = Elems.size();
3990   unsigned int Count = 0;
3991   for (auto Elem : Elems) {
3992     if (Elem.getOpcode() != ISD::UNDEF) {
3993       if (!Single.getNode())
3994         Single = Elem;
3995       else if (Elem != Single) {
3996         Single = SDValue();
3997         break;
3998       }
3999       Count += 1;
4000     }
4001   }
4002   // There are three cases here:
4003   //
4004   // - if the only defined element is a loaded one, the best sequence
4005   //   is a replicating load.
4006   //
4007   // - otherwise, if the only defined element is an i64 value, we will
4008   //   end up with the same VLVGP sequence regardless of whether we short-cut
4009   //   for replication or fall through to the later code.
4010   //
4011   // - otherwise, if the only defined element is an i32 or smaller value,
4012   //   we would need 2 instructions to replicate it: VLVGP followed by VREPx.
4013   //   This is only a win if the single defined element is used more than once.
4014   //   In other cases we're better off using a single VLVGx.
4015   if (Single.getNode() && (Count > 1 || Single.getOpcode() == ISD::LOAD))
4016     return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Single);
4017 
4018   // The best way of building a v2i64 from two i64s is to use VLVGP.
4019   if (VT == MVT::v2i64)
4020     return joinDwords(DAG, DL, Elems[0], Elems[1]);
4021 
4022   // Use a 64-bit merge high to combine two doubles.
4023   if (VT == MVT::v2f64)
4024     return buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
4025 
4026   // Build v4f32 values directly from the FPRs:
4027   //
4028   //   <Axxx> <Bxxx> <Cxxxx> <Dxxx>
4029   //         V              V         VMRHF
4030   //      <ABxx>         <CDxx>
4031   //                V                 VMRHG
4032   //              <ABCD>
4033   if (VT == MVT::v4f32) {
4034     SDValue Op01 = buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
4035     SDValue Op23 = buildMergeScalars(DAG, DL, VT, Elems[2], Elems[3]);
4036     // Avoid unnecessary undefs by reusing the other operand.
4037     if (Op01.getOpcode() == ISD::UNDEF)
4038       Op01 = Op23;
4039     else if (Op23.getOpcode() == ISD::UNDEF)
4040       Op23 = Op01;
4041     // Merging identical replications is a no-op.
4042     if (Op01.getOpcode() == SystemZISD::REPLICATE && Op01 == Op23)
4043       return Op01;
4044     Op01 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op01);
4045     Op23 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op23);
4046     SDValue Op = DAG.getNode(SystemZISD::MERGE_HIGH,
4047                              DL, MVT::v2i64, Op01, Op23);
4048     return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4049   }
4050 
4051   // Collect the constant terms.
4052   SmallVector<SDValue, SystemZ::VectorBytes> Constants(NumElements, SDValue());
4053   SmallVector<bool, SystemZ::VectorBytes> Done(NumElements, false);
4054 
4055   unsigned NumConstants = 0;
4056   for (unsigned I = 0; I < NumElements; ++I) {
4057     SDValue Elem = Elems[I];
4058     if (Elem.getOpcode() == ISD::Constant ||
4059         Elem.getOpcode() == ISD::ConstantFP) {
4060       NumConstants += 1;
4061       Constants[I] = Elem;
4062       Done[I] = true;
4063     }
4064   }
4065   // If there was at least one constant, fill in the other elements of
4066   // Constants with undefs to get a full vector constant and use that
4067   // as the starting point.
4068   SDValue Result;
4069   if (NumConstants > 0) {
4070     for (unsigned I = 0; I < NumElements; ++I)
4071       if (!Constants[I].getNode())
4072         Constants[I] = DAG.getUNDEF(Elems[I].getValueType());
4073     Result = DAG.getNode(ISD::BUILD_VECTOR, DL, VT, Constants);
4074   } else {
4075     // Otherwise try to use VLVGP to start the sequence in order to
4076     // avoid a false dependency on any previous contents of the vector
4077     // register.  This only makes sense if one of the associated elements
4078     // is defined.
4079     unsigned I1 = NumElements / 2 - 1;
4080     unsigned I2 = NumElements - 1;
4081     bool Def1 = (Elems[I1].getOpcode() != ISD::UNDEF);
4082     bool Def2 = (Elems[I2].getOpcode() != ISD::UNDEF);
4083     if (Def1 || Def2) {
4084       SDValue Elem1 = Elems[Def1 ? I1 : I2];
4085       SDValue Elem2 = Elems[Def2 ? I2 : I1];
4086       Result = DAG.getNode(ISD::BITCAST, DL, VT,
4087                            joinDwords(DAG, DL, Elem1, Elem2));
4088       Done[I1] = true;
4089       Done[I2] = true;
4090     } else
4091       Result = DAG.getUNDEF(VT);
4092   }
4093 
4094   // Use VLVGx to insert the other elements.
4095   for (unsigned I = 0; I < NumElements; ++I)
4096     if (!Done[I] && Elems[I].getOpcode() != ISD::UNDEF)
4097       Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, Result, Elems[I],
4098                            DAG.getConstant(I, DL, MVT::i32));
4099   return Result;
4100 }
4101 
4102 SDValue SystemZTargetLowering::lowerBUILD_VECTOR(SDValue Op,
4103                                                  SelectionDAG &DAG) const {
4104   const SystemZInstrInfo *TII =
4105     static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
4106   auto *BVN = cast<BuildVectorSDNode>(Op.getNode());
4107   SDLoc DL(Op);
4108   EVT VT = Op.getValueType();
4109 
4110   if (BVN->isConstant()) {
4111     // Try using VECTOR GENERATE BYTE MASK.  This is the architecturally-
4112     // preferred way of creating all-zero and all-one vectors so give it
4113     // priority over other methods below.
4114     uint64_t Mask = 0;
4115     if (tryBuildVectorByteMask(BVN, Mask)) {
4116       SDValue Op = DAG.getNode(SystemZISD::BYTE_MASK, DL, MVT::v16i8,
4117                                DAG.getConstant(Mask, DL, MVT::i32));
4118       return DAG.getNode(ISD::BITCAST, DL, VT, Op);
4119     }
4120 
4121     // Try using some form of replication.
4122     APInt SplatBits, SplatUndef;
4123     unsigned SplatBitSize;
4124     bool HasAnyUndefs;
4125     if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
4126                              8, true) &&
4127         SplatBitSize <= 64) {
4128       // First try assuming that any undefined bits above the highest set bit
4129       // and below the lowest set bit are 1s.  This increases the likelihood of
4130       // being able to use a sign-extended element value in VECTOR REPLICATE
4131       // IMMEDIATE or a wraparound mask in VECTOR GENERATE MASK.
4132       uint64_t SplatBitsZ = SplatBits.getZExtValue();
4133       uint64_t SplatUndefZ = SplatUndef.getZExtValue();
4134       uint64_t Lower = (SplatUndefZ
4135                         & ((uint64_t(1) << findFirstSet(SplatBitsZ)) - 1));
4136       uint64_t Upper = (SplatUndefZ
4137                         & ~((uint64_t(1) << findLastSet(SplatBitsZ)) - 1));
4138       uint64_t Value = SplatBitsZ | Upper | Lower;
4139       SDValue Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value,
4140                                            SplatBitSize);
4141       if (Op.getNode())
4142         return Op;
4143 
4144       // Now try assuming that any undefined bits between the first and
4145       // last defined set bits are set.  This increases the chances of
4146       // using a non-wraparound mask.
4147       uint64_t Middle = SplatUndefZ & ~Upper & ~Lower;
4148       Value = SplatBitsZ | Middle;
4149       Op = tryBuildVectorReplicate(DAG, TII, DL, VT, Value, SplatBitSize);
4150       if (Op.getNode())
4151         return Op;
4152     }
4153 
4154     // Fall back to loading it from memory.
4155     return SDValue();
4156   }
4157 
4158   // See if we should use shuffles to construct the vector from other vectors.
4159   if (SDValue Res = tryBuildVectorShuffle(DAG, BVN))
4160     return Res;
4161 
4162   // Detect SCALAR_TO_VECTOR conversions.
4163   if (isOperationLegal(ISD::SCALAR_TO_VECTOR, VT) && isScalarToVector(Op))
4164     return buildScalarToVector(DAG, DL, VT, Op.getOperand(0));
4165 
4166   // Otherwise use buildVector to build the vector up from GPRs.
4167   unsigned NumElements = Op.getNumOperands();
4168   SmallVector<SDValue, SystemZ::VectorBytes> Ops(NumElements);
4169   for (unsigned I = 0; I < NumElements; ++I)
4170     Ops[I] = Op.getOperand(I);
4171   return buildVector(DAG, DL, VT, Ops);
4172 }
4173 
4174 SDValue SystemZTargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
4175                                                    SelectionDAG &DAG) const {
4176   auto *VSN = cast<ShuffleVectorSDNode>(Op.getNode());
4177   SDLoc DL(Op);
4178   EVT VT = Op.getValueType();
4179   unsigned NumElements = VT.getVectorNumElements();
4180 
4181   if (VSN->isSplat()) {
4182     SDValue Op0 = Op.getOperand(0);
4183     unsigned Index = VSN->getSplatIndex();
4184     assert(Index < VT.getVectorNumElements() &&
4185            "Splat index should be defined and in first operand");
4186     // See whether the value we're splatting is directly available as a scalar.
4187     if ((Index == 0 && Op0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
4188         Op0.getOpcode() == ISD::BUILD_VECTOR)
4189       return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0.getOperand(Index));
4190     // Otherwise keep it as a vector-to-vector operation.
4191     return DAG.getNode(SystemZISD::SPLAT, DL, VT, Op.getOperand(0),
4192                        DAG.getConstant(Index, DL, MVT::i32));
4193   }
4194 
4195   GeneralShuffle GS(VT);
4196   for (unsigned I = 0; I < NumElements; ++I) {
4197     int Elt = VSN->getMaskElt(I);
4198     if (Elt < 0)
4199       GS.addUndef();
4200     else
4201       GS.add(Op.getOperand(unsigned(Elt) / NumElements),
4202              unsigned(Elt) % NumElements);
4203   }
4204   return GS.getNode(DAG, SDLoc(VSN));
4205 }
4206 
4207 SDValue SystemZTargetLowering::lowerSCALAR_TO_VECTOR(SDValue Op,
4208                                                      SelectionDAG &DAG) const {
4209   SDLoc DL(Op);
4210   // Just insert the scalar into element 0 of an undefined vector.
4211   return DAG.getNode(ISD::INSERT_VECTOR_ELT, DL,
4212                      Op.getValueType(), DAG.getUNDEF(Op.getValueType()),
4213                      Op.getOperand(0), DAG.getConstant(0, DL, MVT::i32));
4214 }
4215 
4216 SDValue SystemZTargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op,
4217                                                       SelectionDAG &DAG) const {
4218   // Handle insertions of floating-point values.
4219   SDLoc DL(Op);
4220   SDValue Op0 = Op.getOperand(0);
4221   SDValue Op1 = Op.getOperand(1);
4222   SDValue Op2 = Op.getOperand(2);
4223   EVT VT = Op.getValueType();
4224 
4225   // Insertions into constant indices of a v2f64 can be done using VPDI.
4226   // However, if the inserted value is a bitcast or a constant then it's
4227   // better to use GPRs, as below.
4228   if (VT == MVT::v2f64 &&
4229       Op1.getOpcode() != ISD::BITCAST &&
4230       Op1.getOpcode() != ISD::ConstantFP &&
4231       Op2.getOpcode() == ISD::Constant) {
4232     uint64_t Index = dyn_cast<ConstantSDNode>(Op2)->getZExtValue();
4233     unsigned Mask = VT.getVectorNumElements() - 1;
4234     if (Index <= Mask)
4235       return Op;
4236   }
4237 
4238   // Otherwise bitcast to the equivalent integer form and insert via a GPR.
4239   MVT IntVT = MVT::getIntegerVT(VT.getVectorElementType().getSizeInBits());
4240   MVT IntVecVT = MVT::getVectorVT(IntVT, VT.getVectorNumElements());
4241   SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, IntVecVT,
4242                             DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0),
4243                             DAG.getNode(ISD::BITCAST, DL, IntVT, Op1), Op2);
4244   return DAG.getNode(ISD::BITCAST, DL, VT, Res);
4245 }
4246 
4247 SDValue
4248 SystemZTargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op,
4249                                                SelectionDAG &DAG) const {
4250   // Handle extractions of floating-point values.
4251   SDLoc DL(Op);
4252   SDValue Op0 = Op.getOperand(0);
4253   SDValue Op1 = Op.getOperand(1);
4254   EVT VT = Op.getValueType();
4255   EVT VecVT = Op0.getValueType();
4256 
4257   // Extractions of constant indices can be done directly.
4258   if (auto *CIndexN = dyn_cast<ConstantSDNode>(Op1)) {
4259     uint64_t Index = CIndexN->getZExtValue();
4260     unsigned Mask = VecVT.getVectorNumElements() - 1;
4261     if (Index <= Mask)
4262       return Op;
4263   }
4264 
4265   // Otherwise bitcast to the equivalent integer form and extract via a GPR.
4266   MVT IntVT = MVT::getIntegerVT(VT.getSizeInBits());
4267   MVT IntVecVT = MVT::getVectorVT(IntVT, VecVT.getVectorNumElements());
4268   SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IntVT,
4269                             DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0), Op1);
4270   return DAG.getNode(ISD::BITCAST, DL, VT, Res);
4271 }
4272 
4273 SDValue
4274 SystemZTargetLowering::lowerExtendVectorInreg(SDValue Op, SelectionDAG &DAG,
4275                                               unsigned UnpackHigh) const {
4276   SDValue PackedOp = Op.getOperand(0);
4277   EVT OutVT = Op.getValueType();
4278   EVT InVT = PackedOp.getValueType();
4279   unsigned ToBits = OutVT.getVectorElementType().getSizeInBits();
4280   unsigned FromBits = InVT.getVectorElementType().getSizeInBits();
4281   do {
4282     FromBits *= 2;
4283     EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(FromBits),
4284                                  SystemZ::VectorBits / FromBits);
4285     PackedOp = DAG.getNode(UnpackHigh, SDLoc(PackedOp), OutVT, PackedOp);
4286   } while (FromBits != ToBits);
4287   return PackedOp;
4288 }
4289 
4290 SDValue SystemZTargetLowering::lowerShift(SDValue Op, SelectionDAG &DAG,
4291                                           unsigned ByScalar) const {
4292   // Look for cases where a vector shift can use the *_BY_SCALAR form.
4293   SDValue Op0 = Op.getOperand(0);
4294   SDValue Op1 = Op.getOperand(1);
4295   SDLoc DL(Op);
4296   EVT VT = Op.getValueType();
4297   unsigned ElemBitSize = VT.getVectorElementType().getSizeInBits();
4298 
4299   // See whether the shift vector is a splat represented as BUILD_VECTOR.
4300   if (auto *BVN = dyn_cast<BuildVectorSDNode>(Op1)) {
4301     APInt SplatBits, SplatUndef;
4302     unsigned SplatBitSize;
4303     bool HasAnyUndefs;
4304     // Check for constant splats.  Use ElemBitSize as the minimum element
4305     // width and reject splats that need wider elements.
4306     if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
4307                              ElemBitSize, true) &&
4308         SplatBitSize == ElemBitSize) {
4309       SDValue Shift = DAG.getConstant(SplatBits.getZExtValue() & 0xfff,
4310                                       DL, MVT::i32);
4311       return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4312     }
4313     // Check for variable splats.
4314     BitVector UndefElements;
4315     SDValue Splat = BVN->getSplatValue(&UndefElements);
4316     if (Splat) {
4317       // Since i32 is the smallest legal type, we either need a no-op
4318       // or a truncation.
4319       SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Splat);
4320       return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4321     }
4322   }
4323 
4324   // See whether the shift vector is a splat represented as SHUFFLE_VECTOR,
4325   // and the shift amount is directly available in a GPR.
4326   if (auto *VSN = dyn_cast<ShuffleVectorSDNode>(Op1)) {
4327     if (VSN->isSplat()) {
4328       SDValue VSNOp0 = VSN->getOperand(0);
4329       unsigned Index = VSN->getSplatIndex();
4330       assert(Index < VT.getVectorNumElements() &&
4331              "Splat index should be defined and in first operand");
4332       if ((Index == 0 && VSNOp0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
4333           VSNOp0.getOpcode() == ISD::BUILD_VECTOR) {
4334         // Since i32 is the smallest legal type, we either need a no-op
4335         // or a truncation.
4336         SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32,
4337                                     VSNOp0.getOperand(Index));
4338         return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
4339       }
4340     }
4341   }
4342 
4343   // Otherwise just treat the current form as legal.
4344   return Op;
4345 }
4346 
4347 SDValue SystemZTargetLowering::LowerOperation(SDValue Op,
4348                                               SelectionDAG &DAG) const {
4349   switch (Op.getOpcode()) {
4350   case ISD::BR_CC:
4351     return lowerBR_CC(Op, DAG);
4352   case ISD::SELECT_CC:
4353     return lowerSELECT_CC(Op, DAG);
4354   case ISD::SETCC:
4355     return lowerSETCC(Op, DAG);
4356   case ISD::GlobalAddress:
4357     return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG);
4358   case ISD::GlobalTLSAddress:
4359     return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG);
4360   case ISD::BlockAddress:
4361     return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG);
4362   case ISD::JumpTable:
4363     return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG);
4364   case ISD::ConstantPool:
4365     return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG);
4366   case ISD::BITCAST:
4367     return lowerBITCAST(Op, DAG);
4368   case ISD::VASTART:
4369     return lowerVASTART(Op, DAG);
4370   case ISD::VACOPY:
4371     return lowerVACOPY(Op, DAG);
4372   case ISD::DYNAMIC_STACKALLOC:
4373     return lowerDYNAMIC_STACKALLOC(Op, DAG);
4374   case ISD::SMUL_LOHI:
4375     return lowerSMUL_LOHI(Op, DAG);
4376   case ISD::UMUL_LOHI:
4377     return lowerUMUL_LOHI(Op, DAG);
4378   case ISD::SDIVREM:
4379     return lowerSDIVREM(Op, DAG);
4380   case ISD::UDIVREM:
4381     return lowerUDIVREM(Op, DAG);
4382   case ISD::OR:
4383     return lowerOR(Op, DAG);
4384   case ISD::CTPOP:
4385     return lowerCTPOP(Op, DAG);
4386   case ISD::CTLZ_ZERO_UNDEF:
4387     return DAG.getNode(ISD::CTLZ, SDLoc(Op),
4388                        Op.getValueType(), Op.getOperand(0));
4389   case ISD::CTTZ_ZERO_UNDEF:
4390     return DAG.getNode(ISD::CTTZ, SDLoc(Op),
4391                        Op.getValueType(), Op.getOperand(0));
4392   case ISD::ATOMIC_SWAP:
4393     return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_SWAPW);
4394   case ISD::ATOMIC_STORE:
4395     return lowerATOMIC_STORE(Op, DAG);
4396   case ISD::ATOMIC_LOAD:
4397     return lowerATOMIC_LOAD(Op, DAG);
4398   case ISD::ATOMIC_LOAD_ADD:
4399     return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD);
4400   case ISD::ATOMIC_LOAD_SUB:
4401     return lowerATOMIC_LOAD_SUB(Op, DAG);
4402   case ISD::ATOMIC_LOAD_AND:
4403     return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_AND);
4404   case ISD::ATOMIC_LOAD_OR:
4405     return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_OR);
4406   case ISD::ATOMIC_LOAD_XOR:
4407     return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR);
4408   case ISD::ATOMIC_LOAD_NAND:
4409     return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND);
4410   case ISD::ATOMIC_LOAD_MIN:
4411     return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN);
4412   case ISD::ATOMIC_LOAD_MAX:
4413     return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX);
4414   case ISD::ATOMIC_LOAD_UMIN:
4415     return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN);
4416   case ISD::ATOMIC_LOAD_UMAX:
4417     return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX);
4418   case ISD::ATOMIC_CMP_SWAP:
4419     return lowerATOMIC_CMP_SWAP(Op, DAG);
4420   case ISD::STACKSAVE:
4421     return lowerSTACKSAVE(Op, DAG);
4422   case ISD::STACKRESTORE:
4423     return lowerSTACKRESTORE(Op, DAG);
4424   case ISD::PREFETCH:
4425     return lowerPREFETCH(Op, DAG);
4426   case ISD::INTRINSIC_W_CHAIN:
4427     return lowerINTRINSIC_W_CHAIN(Op, DAG);
4428   case ISD::INTRINSIC_WO_CHAIN:
4429     return lowerINTRINSIC_WO_CHAIN(Op, DAG);
4430   case ISD::BUILD_VECTOR:
4431     return lowerBUILD_VECTOR(Op, DAG);
4432   case ISD::VECTOR_SHUFFLE:
4433     return lowerVECTOR_SHUFFLE(Op, DAG);
4434   case ISD::SCALAR_TO_VECTOR:
4435     return lowerSCALAR_TO_VECTOR(Op, DAG);
4436   case ISD::INSERT_VECTOR_ELT:
4437     return lowerINSERT_VECTOR_ELT(Op, DAG);
4438   case ISD::EXTRACT_VECTOR_ELT:
4439     return lowerEXTRACT_VECTOR_ELT(Op, DAG);
4440   case ISD::SIGN_EXTEND_VECTOR_INREG:
4441     return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACK_HIGH);
4442   case ISD::ZERO_EXTEND_VECTOR_INREG:
4443     return lowerExtendVectorInreg(Op, DAG, SystemZISD::UNPACKL_HIGH);
4444   case ISD::SHL:
4445     return lowerShift(Op, DAG, SystemZISD::VSHL_BY_SCALAR);
4446   case ISD::SRL:
4447     return lowerShift(Op, DAG, SystemZISD::VSRL_BY_SCALAR);
4448   case ISD::SRA:
4449     return lowerShift(Op, DAG, SystemZISD::VSRA_BY_SCALAR);
4450   default:
4451     llvm_unreachable("Unexpected node to lower");
4452   }
4453 }
4454 
4455 const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
4456 #define OPCODE(NAME) case SystemZISD::NAME: return "SystemZISD::" #NAME
4457   switch ((SystemZISD::NodeType)Opcode) {
4458     case SystemZISD::FIRST_NUMBER: break;
4459     OPCODE(RET_FLAG);
4460     OPCODE(CALL);
4461     OPCODE(SIBCALL);
4462     OPCODE(TLS_GDCALL);
4463     OPCODE(TLS_LDCALL);
4464     OPCODE(PCREL_WRAPPER);
4465     OPCODE(PCREL_OFFSET);
4466     OPCODE(IABS);
4467     OPCODE(ICMP);
4468     OPCODE(FCMP);
4469     OPCODE(TM);
4470     OPCODE(BR_CCMASK);
4471     OPCODE(SELECT_CCMASK);
4472     OPCODE(ADJDYNALLOC);
4473     OPCODE(EXTRACT_ACCESS);
4474     OPCODE(POPCNT);
4475     OPCODE(UMUL_LOHI64);
4476     OPCODE(SDIVREM32);
4477     OPCODE(SDIVREM64);
4478     OPCODE(UDIVREM32);
4479     OPCODE(UDIVREM64);
4480     OPCODE(MVC);
4481     OPCODE(MVC_LOOP);
4482     OPCODE(NC);
4483     OPCODE(NC_LOOP);
4484     OPCODE(OC);
4485     OPCODE(OC_LOOP);
4486     OPCODE(XC);
4487     OPCODE(XC_LOOP);
4488     OPCODE(CLC);
4489     OPCODE(CLC_LOOP);
4490     OPCODE(STPCPY);
4491     OPCODE(STRCMP);
4492     OPCODE(SEARCH_STRING);
4493     OPCODE(IPM);
4494     OPCODE(SERIALIZE);
4495     OPCODE(TBEGIN);
4496     OPCODE(TBEGIN_NOFLOAT);
4497     OPCODE(TEND);
4498     OPCODE(BYTE_MASK);
4499     OPCODE(ROTATE_MASK);
4500     OPCODE(REPLICATE);
4501     OPCODE(JOIN_DWORDS);
4502     OPCODE(SPLAT);
4503     OPCODE(MERGE_HIGH);
4504     OPCODE(MERGE_LOW);
4505     OPCODE(SHL_DOUBLE);
4506     OPCODE(PERMUTE_DWORDS);
4507     OPCODE(PERMUTE);
4508     OPCODE(PACK);
4509     OPCODE(PACKS_CC);
4510     OPCODE(PACKLS_CC);
4511     OPCODE(UNPACK_HIGH);
4512     OPCODE(UNPACKL_HIGH);
4513     OPCODE(UNPACK_LOW);
4514     OPCODE(UNPACKL_LOW);
4515     OPCODE(VSHL_BY_SCALAR);
4516     OPCODE(VSRL_BY_SCALAR);
4517     OPCODE(VSRA_BY_SCALAR);
4518     OPCODE(VSUM);
4519     OPCODE(VICMPE);
4520     OPCODE(VICMPH);
4521     OPCODE(VICMPHL);
4522     OPCODE(VICMPES);
4523     OPCODE(VICMPHS);
4524     OPCODE(VICMPHLS);
4525     OPCODE(VFCMPE);
4526     OPCODE(VFCMPH);
4527     OPCODE(VFCMPHE);
4528     OPCODE(VFCMPES);
4529     OPCODE(VFCMPHS);
4530     OPCODE(VFCMPHES);
4531     OPCODE(VFTCI);
4532     OPCODE(VEXTEND);
4533     OPCODE(VROUND);
4534     OPCODE(VTM);
4535     OPCODE(VFAE_CC);
4536     OPCODE(VFAEZ_CC);
4537     OPCODE(VFEE_CC);
4538     OPCODE(VFEEZ_CC);
4539     OPCODE(VFENE_CC);
4540     OPCODE(VFENEZ_CC);
4541     OPCODE(VISTR_CC);
4542     OPCODE(VSTRC_CC);
4543     OPCODE(VSTRCZ_CC);
4544     OPCODE(ATOMIC_SWAPW);
4545     OPCODE(ATOMIC_LOADW_ADD);
4546     OPCODE(ATOMIC_LOADW_SUB);
4547     OPCODE(ATOMIC_LOADW_AND);
4548     OPCODE(ATOMIC_LOADW_OR);
4549     OPCODE(ATOMIC_LOADW_XOR);
4550     OPCODE(ATOMIC_LOADW_NAND);
4551     OPCODE(ATOMIC_LOADW_MIN);
4552     OPCODE(ATOMIC_LOADW_MAX);
4553     OPCODE(ATOMIC_LOADW_UMIN);
4554     OPCODE(ATOMIC_LOADW_UMAX);
4555     OPCODE(ATOMIC_CMP_SWAPW);
4556     OPCODE(PREFETCH);
4557   }
4558   return nullptr;
4559 #undef OPCODE
4560 }
4561 
4562 // Return true if VT is a vector whose elements are a whole number of bytes
4563 // in width.
4564 static bool canTreatAsByteVector(EVT VT) {
4565   return VT.isVector() && VT.getVectorElementType().getSizeInBits() % 8 == 0;
4566 }
4567 
4568 // Try to simplify an EXTRACT_VECTOR_ELT from a vector of type VecVT
4569 // producing a result of type ResVT.  Op is a possibly bitcast version
4570 // of the input vector and Index is the index (based on type VecVT) that
4571 // should be extracted.  Return the new extraction if a simplification
4572 // was possible or if Force is true.
4573 SDValue SystemZTargetLowering::combineExtract(SDLoc DL, EVT ResVT, EVT VecVT,
4574                                               SDValue Op, unsigned Index,
4575                                               DAGCombinerInfo &DCI,
4576                                               bool Force) const {
4577   SelectionDAG &DAG = DCI.DAG;
4578 
4579   // The number of bytes being extracted.
4580   unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
4581 
4582   for (;;) {
4583     unsigned Opcode = Op.getOpcode();
4584     if (Opcode == ISD::BITCAST)
4585       // Look through bitcasts.
4586       Op = Op.getOperand(0);
4587     else if (Opcode == ISD::VECTOR_SHUFFLE &&
4588              canTreatAsByteVector(Op.getValueType())) {
4589       // Get a VPERM-like permute mask and see whether the bytes covered
4590       // by the extracted element are a contiguous sequence from one
4591       // source operand.
4592       SmallVector<int, SystemZ::VectorBytes> Bytes;
4593       getVPermMask(cast<ShuffleVectorSDNode>(Op), Bytes);
4594       int First;
4595       if (!getShuffleInput(Bytes, Index * BytesPerElement,
4596                            BytesPerElement, First))
4597         break;
4598       if (First < 0)
4599         return DAG.getUNDEF(ResVT);
4600       // Make sure the contiguous sequence starts at a multiple of the
4601       // original element size.
4602       unsigned Byte = unsigned(First) % Bytes.size();
4603       if (Byte % BytesPerElement != 0)
4604         break;
4605       // We can get the extracted value directly from an input.
4606       Index = Byte / BytesPerElement;
4607       Op = Op.getOperand(unsigned(First) / Bytes.size());
4608       Force = true;
4609     } else if (Opcode == ISD::BUILD_VECTOR &&
4610                canTreatAsByteVector(Op.getValueType())) {
4611       // We can only optimize this case if the BUILD_VECTOR elements are
4612       // at least as wide as the extracted value.
4613       EVT OpVT = Op.getValueType();
4614       unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
4615       if (OpBytesPerElement < BytesPerElement)
4616         break;
4617       // Make sure that the least-significant bit of the extracted value
4618       // is the least significant bit of an input.
4619       unsigned End = (Index + 1) * BytesPerElement;
4620       if (End % OpBytesPerElement != 0)
4621         break;
4622       // We're extracting the low part of one operand of the BUILD_VECTOR.
4623       Op = Op.getOperand(End / OpBytesPerElement - 1);
4624       if (!Op.getValueType().isInteger()) {
4625         EVT VT = MVT::getIntegerVT(Op.getValueType().getSizeInBits());
4626         Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
4627         DCI.AddToWorklist(Op.getNode());
4628       }
4629       EVT VT = MVT::getIntegerVT(ResVT.getSizeInBits());
4630       Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
4631       if (VT != ResVT) {
4632         DCI.AddToWorklist(Op.getNode());
4633         Op = DAG.getNode(ISD::BITCAST, DL, ResVT, Op);
4634       }
4635       return Op;
4636     } else if ((Opcode == ISD::SIGN_EXTEND_VECTOR_INREG ||
4637                 Opcode == ISD::ZERO_EXTEND_VECTOR_INREG ||
4638                 Opcode == ISD::ANY_EXTEND_VECTOR_INREG) &&
4639                canTreatAsByteVector(Op.getValueType()) &&
4640                canTreatAsByteVector(Op.getOperand(0).getValueType())) {
4641       // Make sure that only the unextended bits are significant.
4642       EVT ExtVT = Op.getValueType();
4643       EVT OpVT = Op.getOperand(0).getValueType();
4644       unsigned ExtBytesPerElement = ExtVT.getVectorElementType().getStoreSize();
4645       unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
4646       unsigned Byte = Index * BytesPerElement;
4647       unsigned SubByte = Byte % ExtBytesPerElement;
4648       unsigned MinSubByte = ExtBytesPerElement - OpBytesPerElement;
4649       if (SubByte < MinSubByte ||
4650           SubByte + BytesPerElement > ExtBytesPerElement)
4651         break;
4652       // Get the byte offset of the unextended element
4653       Byte = Byte / ExtBytesPerElement * OpBytesPerElement;
4654       // ...then add the byte offset relative to that element.
4655       Byte += SubByte - MinSubByte;
4656       if (Byte % BytesPerElement != 0)
4657         break;
4658       Op = Op.getOperand(0);
4659       Index = Byte / BytesPerElement;
4660       Force = true;
4661     } else
4662       break;
4663   }
4664   if (Force) {
4665     if (Op.getValueType() != VecVT) {
4666       Op = DAG.getNode(ISD::BITCAST, DL, VecVT, Op);
4667       DCI.AddToWorklist(Op.getNode());
4668     }
4669     return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ResVT, Op,
4670                        DAG.getConstant(Index, DL, MVT::i32));
4671   }
4672   return SDValue();
4673 }
4674 
4675 // Optimize vector operations in scalar value Op on the basis that Op
4676 // is truncated to TruncVT.
4677 SDValue
4678 SystemZTargetLowering::combineTruncateExtract(SDLoc DL, EVT TruncVT, SDValue Op,
4679                                               DAGCombinerInfo &DCI) const {
4680   // If we have (trunc (extract_vector_elt X, Y)), try to turn it into
4681   // (extract_vector_elt (bitcast X), Y'), where (bitcast X) has elements
4682   // of type TruncVT.
4683   if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4684       TruncVT.getSizeInBits() % 8 == 0) {
4685     SDValue Vec = Op.getOperand(0);
4686     EVT VecVT = Vec.getValueType();
4687     if (canTreatAsByteVector(VecVT)) {
4688       if (auto *IndexN = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
4689         unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
4690         unsigned TruncBytes = TruncVT.getStoreSize();
4691         if (BytesPerElement % TruncBytes == 0) {
4692           // Calculate the value of Y' in the above description.  We are
4693           // splitting the original elements into Scale equal-sized pieces
4694           // and for truncation purposes want the last (least-significant)
4695           // of these pieces for IndexN.  This is easiest to do by calculating
4696           // the start index of the following element and then subtracting 1.
4697           unsigned Scale = BytesPerElement / TruncBytes;
4698           unsigned NewIndex = (IndexN->getZExtValue() + 1) * Scale - 1;
4699 
4700           // Defer the creation of the bitcast from X to combineExtract,
4701           // which might be able to optimize the extraction.
4702           VecVT = MVT::getVectorVT(MVT::getIntegerVT(TruncBytes * 8),
4703                                    VecVT.getStoreSize() / TruncBytes);
4704           EVT ResVT = (TruncBytes < 4 ? MVT::i32 : TruncVT);
4705           return combineExtract(DL, ResVT, VecVT, Vec, NewIndex, DCI, true);
4706         }
4707       }
4708     }
4709   }
4710   return SDValue();
4711 }
4712 
4713 SDValue SystemZTargetLowering::PerformDAGCombine(SDNode *N,
4714                                                  DAGCombinerInfo &DCI) const {
4715   SelectionDAG &DAG = DCI.DAG;
4716   unsigned Opcode = N->getOpcode();
4717   if (Opcode == ISD::SIGN_EXTEND) {
4718     // Convert (sext (ashr (shl X, C1), C2)) to
4719     // (ashr (shl (anyext X), C1'), C2')), since wider shifts are as
4720     // cheap as narrower ones.
4721     SDValue N0 = N->getOperand(0);
4722     EVT VT = N->getValueType(0);
4723     if (N0.hasOneUse() && N0.getOpcode() == ISD::SRA) {
4724       auto *SraAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1));
4725       SDValue Inner = N0.getOperand(0);
4726       if (SraAmt && Inner.hasOneUse() && Inner.getOpcode() == ISD::SHL) {
4727         if (auto *ShlAmt = dyn_cast<ConstantSDNode>(Inner.getOperand(1))) {
4728           unsigned Extra = (VT.getSizeInBits() -
4729                             N0.getValueType().getSizeInBits());
4730           unsigned NewShlAmt = ShlAmt->getZExtValue() + Extra;
4731           unsigned NewSraAmt = SraAmt->getZExtValue() + Extra;
4732           EVT ShiftVT = N0.getOperand(1).getValueType();
4733           SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SDLoc(Inner), VT,
4734                                     Inner.getOperand(0));
4735           SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(Inner), VT, Ext,
4736                                     DAG.getConstant(NewShlAmt, SDLoc(Inner),
4737                                                     ShiftVT));
4738           return DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl,
4739                              DAG.getConstant(NewSraAmt, SDLoc(N0), ShiftVT));
4740         }
4741       }
4742     }
4743   }
4744   if (Opcode == SystemZISD::MERGE_HIGH ||
4745       Opcode == SystemZISD::MERGE_LOW) {
4746     SDValue Op0 = N->getOperand(0);
4747     SDValue Op1 = N->getOperand(1);
4748     if (Op0.getOpcode() == ISD::BITCAST)
4749       Op0 = Op0.getOperand(0);
4750     if (Op0.getOpcode() == SystemZISD::BYTE_MASK &&
4751         cast<ConstantSDNode>(Op0.getOperand(0))->getZExtValue() == 0) {
4752       // (z_merge_* 0, 0) -> 0.  This is mostly useful for using VLLEZF
4753       // for v4f32.
4754       if (Op1 == N->getOperand(0))
4755         return Op1;
4756       // (z_merge_? 0, X) -> (z_unpackl_? 0, X).
4757       EVT VT = Op1.getValueType();
4758       unsigned ElemBytes = VT.getVectorElementType().getStoreSize();
4759       if (ElemBytes <= 4) {
4760         Opcode = (Opcode == SystemZISD::MERGE_HIGH ?
4761                   SystemZISD::UNPACKL_HIGH : SystemZISD::UNPACKL_LOW);
4762         EVT InVT = VT.changeVectorElementTypeToInteger();
4763         EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(ElemBytes * 16),
4764                                      SystemZ::VectorBytes / ElemBytes / 2);
4765         if (VT != InVT) {
4766           Op1 = DAG.getNode(ISD::BITCAST, SDLoc(N), InVT, Op1);
4767           DCI.AddToWorklist(Op1.getNode());
4768         }
4769         SDValue Op = DAG.getNode(Opcode, SDLoc(N), OutVT, Op1);
4770         DCI.AddToWorklist(Op.getNode());
4771         return DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
4772       }
4773     }
4774   }
4775   // If we have (truncstoreiN (extract_vector_elt X, Y), Z) then it is better
4776   // for the extraction to be done on a vMiN value, so that we can use VSTE.
4777   // If X has wider elements then convert it to:
4778   // (truncstoreiN (extract_vector_elt (bitcast X), Y2), Z).
4779   if (Opcode == ISD::STORE) {
4780     auto *SN = cast<StoreSDNode>(N);
4781     EVT MemVT = SN->getMemoryVT();
4782     if (MemVT.isInteger()) {
4783       if (SDValue Value =
4784               combineTruncateExtract(SDLoc(N), MemVT, SN->getValue(), DCI)) {
4785         DCI.AddToWorklist(Value.getNode());
4786 
4787         // Rewrite the store with the new form of stored value.
4788         return DAG.getTruncStore(SN->getChain(), SDLoc(SN), Value,
4789                                  SN->getBasePtr(), SN->getMemoryVT(),
4790                                  SN->getMemOperand());
4791       }
4792     }
4793   }
4794   // Try to simplify a vector extraction.
4795   if (Opcode == ISD::EXTRACT_VECTOR_ELT) {
4796     if (auto *IndexN = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
4797       SDValue Op0 = N->getOperand(0);
4798       EVT VecVT = Op0.getValueType();
4799       return combineExtract(SDLoc(N), N->getValueType(0), VecVT, Op0,
4800                             IndexN->getZExtValue(), DCI, false);
4801     }
4802   }
4803   // (join_dwords X, X) == (replicate X)
4804   if (Opcode == SystemZISD::JOIN_DWORDS &&
4805       N->getOperand(0) == N->getOperand(1))
4806     return DAG.getNode(SystemZISD::REPLICATE, SDLoc(N), N->getValueType(0),
4807                        N->getOperand(0));
4808   // (fround (extract_vector_elt X 0))
4809   // (fround (extract_vector_elt X 1)) ->
4810   // (extract_vector_elt (VROUND X) 0)
4811   // (extract_vector_elt (VROUND X) 1)
4812   //
4813   // This is a special case since the target doesn't really support v2f32s.
4814   if (Opcode == ISD::FP_ROUND) {
4815     SDValue Op0 = N->getOperand(0);
4816     if (N->getValueType(0) == MVT::f32 &&
4817         Op0.hasOneUse() &&
4818         Op0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4819         Op0.getOperand(0).getValueType() == MVT::v2f64 &&
4820         Op0.getOperand(1).getOpcode() == ISD::Constant &&
4821         cast<ConstantSDNode>(Op0.getOperand(1))->getZExtValue() == 0) {
4822       SDValue Vec = Op0.getOperand(0);
4823       for (auto *U : Vec->uses()) {
4824         if (U != Op0.getNode() &&
4825             U->hasOneUse() &&
4826             U->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
4827             U->getOperand(0) == Vec &&
4828             U->getOperand(1).getOpcode() == ISD::Constant &&
4829             cast<ConstantSDNode>(U->getOperand(1))->getZExtValue() == 1) {
4830           SDValue OtherRound = SDValue(*U->use_begin(), 0);
4831           if (OtherRound.getOpcode() == ISD::FP_ROUND &&
4832               OtherRound.getOperand(0) == SDValue(U, 0) &&
4833               OtherRound.getValueType() == MVT::f32) {
4834             SDValue VRound = DAG.getNode(SystemZISD::VROUND, SDLoc(N),
4835                                          MVT::v4f32, Vec);
4836             DCI.AddToWorklist(VRound.getNode());
4837             SDValue Extract1 =
4838               DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(U), MVT::f32,
4839                           VRound, DAG.getConstant(2, SDLoc(U), MVT::i32));
4840             DCI.AddToWorklist(Extract1.getNode());
4841             DAG.ReplaceAllUsesOfValueWith(OtherRound, Extract1);
4842             SDValue Extract0 =
4843               DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op0), MVT::f32,
4844                           VRound, DAG.getConstant(0, SDLoc(Op0), MVT::i32));
4845             return Extract0;
4846           }
4847         }
4848       }
4849     }
4850   }
4851   return SDValue();
4852 }
4853 
4854 //===----------------------------------------------------------------------===//
4855 // Custom insertion
4856 //===----------------------------------------------------------------------===//
4857 
4858 // Create a new basic block after MBB.
4859 static MachineBasicBlock *emitBlockAfter(MachineBasicBlock *MBB) {
4860   MachineFunction &MF = *MBB->getParent();
4861   MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock());
4862   MF.insert(std::next(MachineFunction::iterator(MBB)), NewMBB);
4863   return NewMBB;
4864 }
4865 
4866 // Split MBB after MI and return the new block (the one that contains
4867 // instructions after MI).
4868 static MachineBasicBlock *splitBlockAfter(MachineInstr *MI,
4869                                           MachineBasicBlock *MBB) {
4870   MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
4871   NewMBB->splice(NewMBB->begin(), MBB,
4872                  std::next(MachineBasicBlock::iterator(MI)), MBB->end());
4873   NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
4874   return NewMBB;
4875 }
4876 
4877 // Split MBB before MI and return the new block (the one that contains MI).
4878 static MachineBasicBlock *splitBlockBefore(MachineInstr *MI,
4879                                            MachineBasicBlock *MBB) {
4880   MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
4881   NewMBB->splice(NewMBB->begin(), MBB, MI, MBB->end());
4882   NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
4883   return NewMBB;
4884 }
4885 
4886 // Force base value Base into a register before MI.  Return the register.
4887 static unsigned forceReg(MachineInstr *MI, MachineOperand &Base,
4888                          const SystemZInstrInfo *TII) {
4889   if (Base.isReg())
4890     return Base.getReg();
4891 
4892   MachineBasicBlock *MBB = MI->getParent();
4893   MachineFunction &MF = *MBB->getParent();
4894   MachineRegisterInfo &MRI = MF.getRegInfo();
4895 
4896   unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
4897   BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LA), Reg)
4898     .addOperand(Base).addImm(0).addReg(0);
4899   return Reg;
4900 }
4901 
4902 // Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI.
4903 MachineBasicBlock *
4904 SystemZTargetLowering::emitSelect(MachineInstr *MI,
4905                                   MachineBasicBlock *MBB) const {
4906   const SystemZInstrInfo *TII =
4907       static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
4908 
4909   unsigned DestReg  = MI->getOperand(0).getReg();
4910   unsigned TrueReg  = MI->getOperand(1).getReg();
4911   unsigned FalseReg = MI->getOperand(2).getReg();
4912   unsigned CCValid  = MI->getOperand(3).getImm();
4913   unsigned CCMask   = MI->getOperand(4).getImm();
4914   DebugLoc DL       = MI->getDebugLoc();
4915 
4916   MachineBasicBlock *StartMBB = MBB;
4917   MachineBasicBlock *JoinMBB  = splitBlockBefore(MI, MBB);
4918   MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
4919 
4920   //  StartMBB:
4921   //   BRC CCMask, JoinMBB
4922   //   # fallthrough to FalseMBB
4923   MBB = StartMBB;
4924   BuildMI(MBB, DL, TII->get(SystemZ::BRC))
4925     .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
4926   MBB->addSuccessor(JoinMBB);
4927   MBB->addSuccessor(FalseMBB);
4928 
4929   //  FalseMBB:
4930   //   # fallthrough to JoinMBB
4931   MBB = FalseMBB;
4932   MBB->addSuccessor(JoinMBB);
4933 
4934   //  JoinMBB:
4935   //   %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ]
4936   //  ...
4937   MBB = JoinMBB;
4938   BuildMI(*MBB, MI, DL, TII->get(SystemZ::PHI), DestReg)
4939     .addReg(TrueReg).addMBB(StartMBB)
4940     .addReg(FalseReg).addMBB(FalseMBB);
4941 
4942   MI->eraseFromParent();
4943   return JoinMBB;
4944 }
4945 
4946 // Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI.
4947 // StoreOpcode is the store to use and Invert says whether the store should
4948 // happen when the condition is false rather than true.  If a STORE ON
4949 // CONDITION is available, STOCOpcode is its opcode, otherwise it is 0.
4950 MachineBasicBlock *
4951 SystemZTargetLowering::emitCondStore(MachineInstr *MI,
4952                                      MachineBasicBlock *MBB,
4953                                      unsigned StoreOpcode, unsigned STOCOpcode,
4954                                      bool Invert) const {
4955   const SystemZInstrInfo *TII =
4956       static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
4957 
4958   unsigned SrcReg     = MI->getOperand(0).getReg();
4959   MachineOperand Base = MI->getOperand(1);
4960   int64_t Disp        = MI->getOperand(2).getImm();
4961   unsigned IndexReg   = MI->getOperand(3).getReg();
4962   unsigned CCValid    = MI->getOperand(4).getImm();
4963   unsigned CCMask     = MI->getOperand(5).getImm();
4964   DebugLoc DL         = MI->getDebugLoc();
4965 
4966   StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp);
4967 
4968   // Use STOCOpcode if possible.  We could use different store patterns in
4969   // order to avoid matching the index register, but the performance trade-offs
4970   // might be more complicated in that case.
4971   if (STOCOpcode && !IndexReg && Subtarget.hasLoadStoreOnCond()) {
4972     if (Invert)
4973       CCMask ^= CCValid;
4974     BuildMI(*MBB, MI, DL, TII->get(STOCOpcode))
4975       .addReg(SrcReg).addOperand(Base).addImm(Disp)
4976       .addImm(CCValid).addImm(CCMask);
4977     MI->eraseFromParent();
4978     return MBB;
4979   }
4980 
4981   // Get the condition needed to branch around the store.
4982   if (!Invert)
4983     CCMask ^= CCValid;
4984 
4985   MachineBasicBlock *StartMBB = MBB;
4986   MachineBasicBlock *JoinMBB  = splitBlockBefore(MI, MBB);
4987   MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
4988 
4989   //  StartMBB:
4990   //   BRC CCMask, JoinMBB
4991   //   # fallthrough to FalseMBB
4992   MBB = StartMBB;
4993   BuildMI(MBB, DL, TII->get(SystemZ::BRC))
4994     .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
4995   MBB->addSuccessor(JoinMBB);
4996   MBB->addSuccessor(FalseMBB);
4997 
4998   //  FalseMBB:
4999   //   store %SrcReg, %Disp(%Index,%Base)
5000   //   # fallthrough to JoinMBB
5001   MBB = FalseMBB;
5002   BuildMI(MBB, DL, TII->get(StoreOpcode))
5003     .addReg(SrcReg).addOperand(Base).addImm(Disp).addReg(IndexReg);
5004   MBB->addSuccessor(JoinMBB);
5005 
5006   MI->eraseFromParent();
5007   return JoinMBB;
5008 }
5009 
5010 // Implement EmitInstrWithCustomInserter for pseudo ATOMIC_LOAD{,W}_*
5011 // or ATOMIC_SWAP{,W} instruction MI.  BinOpcode is the instruction that
5012 // performs the binary operation elided by "*", or 0 for ATOMIC_SWAP{,W}.
5013 // BitSize is the width of the field in bits, or 0 if this is a partword
5014 // ATOMIC_LOADW_* or ATOMIC_SWAPW instruction, in which case the bitsize
5015 // is one of the operands.  Invert says whether the field should be
5016 // inverted after performing BinOpcode (e.g. for NAND).
5017 MachineBasicBlock *
5018 SystemZTargetLowering::emitAtomicLoadBinary(MachineInstr *MI,
5019                                             MachineBasicBlock *MBB,
5020                                             unsigned BinOpcode,
5021                                             unsigned BitSize,
5022                                             bool Invert) const {
5023   MachineFunction &MF = *MBB->getParent();
5024   const SystemZInstrInfo *TII =
5025       static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
5026   MachineRegisterInfo &MRI = MF.getRegInfo();
5027   bool IsSubWord = (BitSize < 32);
5028 
5029   // Extract the operands.  Base can be a register or a frame index.
5030   // Src2 can be a register or immediate.
5031   unsigned Dest        = MI->getOperand(0).getReg();
5032   MachineOperand Base  = earlyUseOperand(MI->getOperand(1));
5033   int64_t Disp         = MI->getOperand(2).getImm();
5034   MachineOperand Src2  = earlyUseOperand(MI->getOperand(3));
5035   unsigned BitShift    = (IsSubWord ? MI->getOperand(4).getReg() : 0);
5036   unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
5037   DebugLoc DL          = MI->getDebugLoc();
5038   if (IsSubWord)
5039     BitSize = MI->getOperand(6).getImm();
5040 
5041   // Subword operations use 32-bit registers.
5042   const TargetRegisterClass *RC = (BitSize <= 32 ?
5043                                    &SystemZ::GR32BitRegClass :
5044                                    &SystemZ::GR64BitRegClass);
5045   unsigned LOpcode  = BitSize <= 32 ? SystemZ::L  : SystemZ::LG;
5046   unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
5047 
5048   // Get the right opcodes for the displacement.
5049   LOpcode  = TII->getOpcodeForOffset(LOpcode,  Disp);
5050   CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
5051   assert(LOpcode && CSOpcode && "Displacement out of range");
5052 
5053   // Create virtual registers for temporary results.
5054   unsigned OrigVal       = MRI.createVirtualRegister(RC);
5055   unsigned OldVal        = MRI.createVirtualRegister(RC);
5056   unsigned NewVal        = (BinOpcode || IsSubWord ?
5057                             MRI.createVirtualRegister(RC) : Src2.getReg());
5058   unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
5059   unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
5060 
5061   // Insert a basic block for the main loop.
5062   MachineBasicBlock *StartMBB = MBB;
5063   MachineBasicBlock *DoneMBB  = splitBlockBefore(MI, MBB);
5064   MachineBasicBlock *LoopMBB  = emitBlockAfter(StartMBB);
5065 
5066   //  StartMBB:
5067   //   ...
5068   //   %OrigVal = L Disp(%Base)
5069   //   # fall through to LoopMMB
5070   MBB = StartMBB;
5071   BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
5072     .addOperand(Base).addImm(Disp).addReg(0);
5073   MBB->addSuccessor(LoopMBB);
5074 
5075   //  LoopMBB:
5076   //   %OldVal        = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ]
5077   //   %RotatedOldVal = RLL %OldVal, 0(%BitShift)
5078   //   %RotatedNewVal = OP %RotatedOldVal, %Src2
5079   //   %NewVal        = RLL %RotatedNewVal, 0(%NegBitShift)
5080   //   %Dest          = CS %OldVal, %NewVal, Disp(%Base)
5081   //   JNE LoopMBB
5082   //   # fall through to DoneMMB
5083   MBB = LoopMBB;
5084   BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5085     .addReg(OrigVal).addMBB(StartMBB)
5086     .addReg(Dest).addMBB(LoopMBB);
5087   if (IsSubWord)
5088     BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
5089       .addReg(OldVal).addReg(BitShift).addImm(0);
5090   if (Invert) {
5091     // Perform the operation normally and then invert every bit of the field.
5092     unsigned Tmp = MRI.createVirtualRegister(RC);
5093     BuildMI(MBB, DL, TII->get(BinOpcode), Tmp)
5094       .addReg(RotatedOldVal).addOperand(Src2);
5095     if (BitSize <= 32)
5096       // XILF with the upper BitSize bits set.
5097       BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal)
5098         .addReg(Tmp).addImm(-1U << (32 - BitSize));
5099     else {
5100       // Use LCGR and add -1 to the result, which is more compact than
5101       // an XILF, XILH pair.
5102       unsigned Tmp2 = MRI.createVirtualRegister(RC);
5103       BuildMI(MBB, DL, TII->get(SystemZ::LCGR), Tmp2).addReg(Tmp);
5104       BuildMI(MBB, DL, TII->get(SystemZ::AGHI), RotatedNewVal)
5105         .addReg(Tmp2).addImm(-1);
5106     }
5107   } else if (BinOpcode)
5108     // A simply binary operation.
5109     BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal)
5110       .addReg(RotatedOldVal).addOperand(Src2);
5111   else if (IsSubWord)
5112     // Use RISBG to rotate Src2 into position and use it to replace the
5113     // field in RotatedOldVal.
5114     BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal)
5115       .addReg(RotatedOldVal).addReg(Src2.getReg())
5116       .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize);
5117   if (IsSubWord)
5118     BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
5119       .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
5120   BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
5121     .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
5122   BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5123     .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
5124   MBB->addSuccessor(LoopMBB);
5125   MBB->addSuccessor(DoneMBB);
5126 
5127   MI->eraseFromParent();
5128   return DoneMBB;
5129 }
5130 
5131 // Implement EmitInstrWithCustomInserter for pseudo
5132 // ATOMIC_LOAD{,W}_{,U}{MIN,MAX} instruction MI.  CompareOpcode is the
5133 // instruction that should be used to compare the current field with the
5134 // minimum or maximum value.  KeepOldMask is the BRC condition-code mask
5135 // for when the current field should be kept.  BitSize is the width of
5136 // the field in bits, or 0 if this is a partword ATOMIC_LOADW_* instruction.
5137 MachineBasicBlock *
5138 SystemZTargetLowering::emitAtomicLoadMinMax(MachineInstr *MI,
5139                                             MachineBasicBlock *MBB,
5140                                             unsigned CompareOpcode,
5141                                             unsigned KeepOldMask,
5142                                             unsigned BitSize) const {
5143   MachineFunction &MF = *MBB->getParent();
5144   const SystemZInstrInfo *TII =
5145       static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
5146   MachineRegisterInfo &MRI = MF.getRegInfo();
5147   bool IsSubWord = (BitSize < 32);
5148 
5149   // Extract the operands.  Base can be a register or a frame index.
5150   unsigned Dest        = MI->getOperand(0).getReg();
5151   MachineOperand Base  = earlyUseOperand(MI->getOperand(1));
5152   int64_t  Disp        = MI->getOperand(2).getImm();
5153   unsigned Src2        = MI->getOperand(3).getReg();
5154   unsigned BitShift    = (IsSubWord ? MI->getOperand(4).getReg() : 0);
5155   unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
5156   DebugLoc DL          = MI->getDebugLoc();
5157   if (IsSubWord)
5158     BitSize = MI->getOperand(6).getImm();
5159 
5160   // Subword operations use 32-bit registers.
5161   const TargetRegisterClass *RC = (BitSize <= 32 ?
5162                                    &SystemZ::GR32BitRegClass :
5163                                    &SystemZ::GR64BitRegClass);
5164   unsigned LOpcode  = BitSize <= 32 ? SystemZ::L  : SystemZ::LG;
5165   unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
5166 
5167   // Get the right opcodes for the displacement.
5168   LOpcode  = TII->getOpcodeForOffset(LOpcode,  Disp);
5169   CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
5170   assert(LOpcode && CSOpcode && "Displacement out of range");
5171 
5172   // Create virtual registers for temporary results.
5173   unsigned OrigVal       = MRI.createVirtualRegister(RC);
5174   unsigned OldVal        = MRI.createVirtualRegister(RC);
5175   unsigned NewVal        = MRI.createVirtualRegister(RC);
5176   unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
5177   unsigned RotatedAltVal = (IsSubWord ? MRI.createVirtualRegister(RC) : Src2);
5178   unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
5179 
5180   // Insert 3 basic blocks for the loop.
5181   MachineBasicBlock *StartMBB  = MBB;
5182   MachineBasicBlock *DoneMBB   = splitBlockBefore(MI, MBB);
5183   MachineBasicBlock *LoopMBB   = emitBlockAfter(StartMBB);
5184   MachineBasicBlock *UseAltMBB = emitBlockAfter(LoopMBB);
5185   MachineBasicBlock *UpdateMBB = emitBlockAfter(UseAltMBB);
5186 
5187   //  StartMBB:
5188   //   ...
5189   //   %OrigVal     = L Disp(%Base)
5190   //   # fall through to LoopMMB
5191   MBB = StartMBB;
5192   BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
5193     .addOperand(Base).addImm(Disp).addReg(0);
5194   MBB->addSuccessor(LoopMBB);
5195 
5196   //  LoopMBB:
5197   //   %OldVal        = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ]
5198   //   %RotatedOldVal = RLL %OldVal, 0(%BitShift)
5199   //   CompareOpcode %RotatedOldVal, %Src2
5200   //   BRC KeepOldMask, UpdateMBB
5201   MBB = LoopMBB;
5202   BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5203     .addReg(OrigVal).addMBB(StartMBB)
5204     .addReg(Dest).addMBB(UpdateMBB);
5205   if (IsSubWord)
5206     BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
5207       .addReg(OldVal).addReg(BitShift).addImm(0);
5208   BuildMI(MBB, DL, TII->get(CompareOpcode))
5209     .addReg(RotatedOldVal).addReg(Src2);
5210   BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5211     .addImm(SystemZ::CCMASK_ICMP).addImm(KeepOldMask).addMBB(UpdateMBB);
5212   MBB->addSuccessor(UpdateMBB);
5213   MBB->addSuccessor(UseAltMBB);
5214 
5215   //  UseAltMBB:
5216   //   %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0
5217   //   # fall through to UpdateMMB
5218   MBB = UseAltMBB;
5219   if (IsSubWord)
5220     BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal)
5221       .addReg(RotatedOldVal).addReg(Src2)
5222       .addImm(32).addImm(31 + BitSize).addImm(0);
5223   MBB->addSuccessor(UpdateMBB);
5224 
5225   //  UpdateMBB:
5226   //   %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ],
5227   //                        [ %RotatedAltVal, UseAltMBB ]
5228   //   %NewVal        = RLL %RotatedNewVal, 0(%NegBitShift)
5229   //   %Dest          = CS %OldVal, %NewVal, Disp(%Base)
5230   //   JNE LoopMBB
5231   //   # fall through to DoneMMB
5232   MBB = UpdateMBB;
5233   BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal)
5234     .addReg(RotatedOldVal).addMBB(LoopMBB)
5235     .addReg(RotatedAltVal).addMBB(UseAltMBB);
5236   if (IsSubWord)
5237     BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
5238       .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
5239   BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
5240     .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
5241   BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5242     .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
5243   MBB->addSuccessor(LoopMBB);
5244   MBB->addSuccessor(DoneMBB);
5245 
5246   MI->eraseFromParent();
5247   return DoneMBB;
5248 }
5249 
5250 // Implement EmitInstrWithCustomInserter for pseudo ATOMIC_CMP_SWAPW
5251 // instruction MI.
5252 MachineBasicBlock *
5253 SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr *MI,
5254                                           MachineBasicBlock *MBB) const {
5255   MachineFunction &MF = *MBB->getParent();
5256   const SystemZInstrInfo *TII =
5257       static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
5258   MachineRegisterInfo &MRI = MF.getRegInfo();
5259 
5260   // Extract the operands.  Base can be a register or a frame index.
5261   unsigned Dest        = MI->getOperand(0).getReg();
5262   MachineOperand Base  = earlyUseOperand(MI->getOperand(1));
5263   int64_t  Disp        = MI->getOperand(2).getImm();
5264   unsigned OrigCmpVal  = MI->getOperand(3).getReg();
5265   unsigned OrigSwapVal = MI->getOperand(4).getReg();
5266   unsigned BitShift    = MI->getOperand(5).getReg();
5267   unsigned NegBitShift = MI->getOperand(6).getReg();
5268   int64_t  BitSize     = MI->getOperand(7).getImm();
5269   DebugLoc DL          = MI->getDebugLoc();
5270 
5271   const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass;
5272 
5273   // Get the right opcodes for the displacement.
5274   unsigned LOpcode  = TII->getOpcodeForOffset(SystemZ::L,  Disp);
5275   unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp);
5276   assert(LOpcode && CSOpcode && "Displacement out of range");
5277 
5278   // Create virtual registers for temporary results.
5279   unsigned OrigOldVal   = MRI.createVirtualRegister(RC);
5280   unsigned OldVal       = MRI.createVirtualRegister(RC);
5281   unsigned CmpVal       = MRI.createVirtualRegister(RC);
5282   unsigned SwapVal      = MRI.createVirtualRegister(RC);
5283   unsigned StoreVal     = MRI.createVirtualRegister(RC);
5284   unsigned RetryOldVal  = MRI.createVirtualRegister(RC);
5285   unsigned RetryCmpVal  = MRI.createVirtualRegister(RC);
5286   unsigned RetrySwapVal = MRI.createVirtualRegister(RC);
5287 
5288   // Insert 2 basic blocks for the loop.
5289   MachineBasicBlock *StartMBB = MBB;
5290   MachineBasicBlock *DoneMBB  = splitBlockBefore(MI, MBB);
5291   MachineBasicBlock *LoopMBB  = emitBlockAfter(StartMBB);
5292   MachineBasicBlock *SetMBB   = emitBlockAfter(LoopMBB);
5293 
5294   //  StartMBB:
5295   //   ...
5296   //   %OrigOldVal     = L Disp(%Base)
5297   //   # fall through to LoopMMB
5298   MBB = StartMBB;
5299   BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal)
5300     .addOperand(Base).addImm(Disp).addReg(0);
5301   MBB->addSuccessor(LoopMBB);
5302 
5303   //  LoopMBB:
5304   //   %OldVal        = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ]
5305   //   %CmpVal        = phi [ %OrigCmpVal, EntryBB ], [ %RetryCmpVal, SetMBB ]
5306   //   %SwapVal       = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ]
5307   //   %Dest          = RLL %OldVal, BitSize(%BitShift)
5308   //                      ^^ The low BitSize bits contain the field
5309   //                         of interest.
5310   //   %RetryCmpVal   = RISBG32 %CmpVal, %Dest, 32, 63-BitSize, 0
5311   //                      ^^ Replace the upper 32-BitSize bits of the
5312   //                         comparison value with those that we loaded,
5313   //                         so that we can use a full word comparison.
5314   //   CR %Dest, %RetryCmpVal
5315   //   JNE DoneMBB
5316   //   # Fall through to SetMBB
5317   MBB = LoopMBB;
5318   BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
5319     .addReg(OrigOldVal).addMBB(StartMBB)
5320     .addReg(RetryOldVal).addMBB(SetMBB);
5321   BuildMI(MBB, DL, TII->get(SystemZ::PHI), CmpVal)
5322     .addReg(OrigCmpVal).addMBB(StartMBB)
5323     .addReg(RetryCmpVal).addMBB(SetMBB);
5324   BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal)
5325     .addReg(OrigSwapVal).addMBB(StartMBB)
5326     .addReg(RetrySwapVal).addMBB(SetMBB);
5327   BuildMI(MBB, DL, TII->get(SystemZ::RLL), Dest)
5328     .addReg(OldVal).addReg(BitShift).addImm(BitSize);
5329   BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetryCmpVal)
5330     .addReg(CmpVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
5331   BuildMI(MBB, DL, TII->get(SystemZ::CR))
5332     .addReg(Dest).addReg(RetryCmpVal);
5333   BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5334     .addImm(SystemZ::CCMASK_ICMP)
5335     .addImm(SystemZ::CCMASK_CMP_NE).addMBB(DoneMBB);
5336   MBB->addSuccessor(DoneMBB);
5337   MBB->addSuccessor(SetMBB);
5338 
5339   //  SetMBB:
5340   //   %RetrySwapVal = RISBG32 %SwapVal, %Dest, 32, 63-BitSize, 0
5341   //                      ^^ Replace the upper 32-BitSize bits of the new
5342   //                         value with those that we loaded.
5343   //   %StoreVal    = RLL %RetrySwapVal, -BitSize(%NegBitShift)
5344   //                      ^^ Rotate the new field to its proper position.
5345   //   %RetryOldVal = CS %Dest, %StoreVal, Disp(%Base)
5346   //   JNE LoopMBB
5347   //   # fall through to ExitMMB
5348   MBB = SetMBB;
5349   BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal)
5350     .addReg(SwapVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
5351   BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal)
5352     .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize);
5353   BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal)
5354     .addReg(OldVal).addReg(StoreVal).addOperand(Base).addImm(Disp);
5355   BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5356     .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
5357   MBB->addSuccessor(LoopMBB);
5358   MBB->addSuccessor(DoneMBB);
5359 
5360   MI->eraseFromParent();
5361   return DoneMBB;
5362 }
5363 
5364 // Emit an extension from a GR32 or GR64 to a GR128.  ClearEven is true
5365 // if the high register of the GR128 value must be cleared or false if
5366 // it's "don't care".  SubReg is subreg_l32 when extending a GR32
5367 // and subreg_l64 when extending a GR64.
5368 MachineBasicBlock *
5369 SystemZTargetLowering::emitExt128(MachineInstr *MI,
5370                                   MachineBasicBlock *MBB,
5371                                   bool ClearEven, unsigned SubReg) const {
5372   MachineFunction &MF = *MBB->getParent();
5373   const SystemZInstrInfo *TII =
5374       static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
5375   MachineRegisterInfo &MRI = MF.getRegInfo();
5376   DebugLoc DL = MI->getDebugLoc();
5377 
5378   unsigned Dest  = MI->getOperand(0).getReg();
5379   unsigned Src   = MI->getOperand(1).getReg();
5380   unsigned In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
5381 
5382   BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128);
5383   if (ClearEven) {
5384     unsigned NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
5385     unsigned Zero64   = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass);
5386 
5387     BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64)
5388       .addImm(0);
5389     BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128)
5390       .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_h64);
5391     In128 = NewIn128;
5392   }
5393   BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest)
5394     .addReg(In128).addReg(Src).addImm(SubReg);
5395 
5396   MI->eraseFromParent();
5397   return MBB;
5398 }
5399 
5400 MachineBasicBlock *
5401 SystemZTargetLowering::emitMemMemWrapper(MachineInstr *MI,
5402                                          MachineBasicBlock *MBB,
5403                                          unsigned Opcode) const {
5404   MachineFunction &MF = *MBB->getParent();
5405   const SystemZInstrInfo *TII =
5406       static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
5407   MachineRegisterInfo &MRI = MF.getRegInfo();
5408   DebugLoc DL = MI->getDebugLoc();
5409 
5410   MachineOperand DestBase = earlyUseOperand(MI->getOperand(0));
5411   uint64_t       DestDisp = MI->getOperand(1).getImm();
5412   MachineOperand SrcBase  = earlyUseOperand(MI->getOperand(2));
5413   uint64_t       SrcDisp  = MI->getOperand(3).getImm();
5414   uint64_t       Length   = MI->getOperand(4).getImm();
5415 
5416   // When generating more than one CLC, all but the last will need to
5417   // branch to the end when a difference is found.
5418   MachineBasicBlock *EndMBB = (Length > 256 && Opcode == SystemZ::CLC ?
5419                                splitBlockAfter(MI, MBB) : nullptr);
5420 
5421   // Check for the loop form, in which operand 5 is the trip count.
5422   if (MI->getNumExplicitOperands() > 5) {
5423     bool HaveSingleBase = DestBase.isIdenticalTo(SrcBase);
5424 
5425     uint64_t StartCountReg = MI->getOperand(5).getReg();
5426     uint64_t StartSrcReg   = forceReg(MI, SrcBase, TII);
5427     uint64_t StartDestReg  = (HaveSingleBase ? StartSrcReg :
5428                               forceReg(MI, DestBase, TII));
5429 
5430     const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass;
5431     uint64_t ThisSrcReg  = MRI.createVirtualRegister(RC);
5432     uint64_t ThisDestReg = (HaveSingleBase ? ThisSrcReg :
5433                             MRI.createVirtualRegister(RC));
5434     uint64_t NextSrcReg  = MRI.createVirtualRegister(RC);
5435     uint64_t NextDestReg = (HaveSingleBase ? NextSrcReg :
5436                             MRI.createVirtualRegister(RC));
5437 
5438     RC = &SystemZ::GR64BitRegClass;
5439     uint64_t ThisCountReg = MRI.createVirtualRegister(RC);
5440     uint64_t NextCountReg = MRI.createVirtualRegister(RC);
5441 
5442     MachineBasicBlock *StartMBB = MBB;
5443     MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
5444     MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5445     MachineBasicBlock *NextMBB = (EndMBB ? emitBlockAfter(LoopMBB) : LoopMBB);
5446 
5447     //  StartMBB:
5448     //   # fall through to LoopMMB
5449     MBB->addSuccessor(LoopMBB);
5450 
5451     //  LoopMBB:
5452     //   %ThisDestReg = phi [ %StartDestReg, StartMBB ],
5453     //                      [ %NextDestReg, NextMBB ]
5454     //   %ThisSrcReg = phi [ %StartSrcReg, StartMBB ],
5455     //                     [ %NextSrcReg, NextMBB ]
5456     //   %ThisCountReg = phi [ %StartCountReg, StartMBB ],
5457     //                       [ %NextCountReg, NextMBB ]
5458     //   ( PFD 2, 768+DestDisp(%ThisDestReg) )
5459     //   Opcode DestDisp(256,%ThisDestReg), SrcDisp(%ThisSrcReg)
5460     //   ( JLH EndMBB )
5461     //
5462     // The prefetch is used only for MVC.  The JLH is used only for CLC.
5463     MBB = LoopMBB;
5464 
5465     BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisDestReg)
5466       .addReg(StartDestReg).addMBB(StartMBB)
5467       .addReg(NextDestReg).addMBB(NextMBB);
5468     if (!HaveSingleBase)
5469       BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisSrcReg)
5470         .addReg(StartSrcReg).addMBB(StartMBB)
5471         .addReg(NextSrcReg).addMBB(NextMBB);
5472     BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisCountReg)
5473       .addReg(StartCountReg).addMBB(StartMBB)
5474       .addReg(NextCountReg).addMBB(NextMBB);
5475     if (Opcode == SystemZ::MVC)
5476       BuildMI(MBB, DL, TII->get(SystemZ::PFD))
5477         .addImm(SystemZ::PFD_WRITE)
5478         .addReg(ThisDestReg).addImm(DestDisp + 768).addReg(0);
5479     BuildMI(MBB, DL, TII->get(Opcode))
5480       .addReg(ThisDestReg).addImm(DestDisp).addImm(256)
5481       .addReg(ThisSrcReg).addImm(SrcDisp);
5482     if (EndMBB) {
5483       BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5484         .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
5485         .addMBB(EndMBB);
5486       MBB->addSuccessor(EndMBB);
5487       MBB->addSuccessor(NextMBB);
5488     }
5489 
5490     // NextMBB:
5491     //   %NextDestReg = LA 256(%ThisDestReg)
5492     //   %NextSrcReg = LA 256(%ThisSrcReg)
5493     //   %NextCountReg = AGHI %ThisCountReg, -1
5494     //   CGHI %NextCountReg, 0
5495     //   JLH LoopMBB
5496     //   # fall through to DoneMMB
5497     //
5498     // The AGHI, CGHI and JLH should be converted to BRCTG by later passes.
5499     MBB = NextMBB;
5500 
5501     BuildMI(MBB, DL, TII->get(SystemZ::LA), NextDestReg)
5502       .addReg(ThisDestReg).addImm(256).addReg(0);
5503     if (!HaveSingleBase)
5504       BuildMI(MBB, DL, TII->get(SystemZ::LA), NextSrcReg)
5505         .addReg(ThisSrcReg).addImm(256).addReg(0);
5506     BuildMI(MBB, DL, TII->get(SystemZ::AGHI), NextCountReg)
5507       .addReg(ThisCountReg).addImm(-1);
5508     BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
5509       .addReg(NextCountReg).addImm(0);
5510     BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5511       .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
5512       .addMBB(LoopMBB);
5513     MBB->addSuccessor(LoopMBB);
5514     MBB->addSuccessor(DoneMBB);
5515 
5516     DestBase = MachineOperand::CreateReg(NextDestReg, false);
5517     SrcBase = MachineOperand::CreateReg(NextSrcReg, false);
5518     Length &= 255;
5519     MBB = DoneMBB;
5520   }
5521   // Handle any remaining bytes with straight-line code.
5522   while (Length > 0) {
5523     uint64_t ThisLength = std::min(Length, uint64_t(256));
5524     // The previous iteration might have created out-of-range displacements.
5525     // Apply them using LAY if so.
5526     if (!isUInt<12>(DestDisp)) {
5527       unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
5528       BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg)
5529         .addOperand(DestBase).addImm(DestDisp).addReg(0);
5530       DestBase = MachineOperand::CreateReg(Reg, false);
5531       DestDisp = 0;
5532     }
5533     if (!isUInt<12>(SrcDisp)) {
5534       unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
5535       BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg)
5536         .addOperand(SrcBase).addImm(SrcDisp).addReg(0);
5537       SrcBase = MachineOperand::CreateReg(Reg, false);
5538       SrcDisp = 0;
5539     }
5540     BuildMI(*MBB, MI, DL, TII->get(Opcode))
5541       .addOperand(DestBase).addImm(DestDisp).addImm(ThisLength)
5542       .addOperand(SrcBase).addImm(SrcDisp);
5543     DestDisp += ThisLength;
5544     SrcDisp += ThisLength;
5545     Length -= ThisLength;
5546     // If there's another CLC to go, branch to the end if a difference
5547     // was found.
5548     if (EndMBB && Length > 0) {
5549       MachineBasicBlock *NextMBB = splitBlockBefore(MI, MBB);
5550       BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5551         .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
5552         .addMBB(EndMBB);
5553       MBB->addSuccessor(EndMBB);
5554       MBB->addSuccessor(NextMBB);
5555       MBB = NextMBB;
5556     }
5557   }
5558   if (EndMBB) {
5559     MBB->addSuccessor(EndMBB);
5560     MBB = EndMBB;
5561     MBB->addLiveIn(SystemZ::CC);
5562   }
5563 
5564   MI->eraseFromParent();
5565   return MBB;
5566 }
5567 
5568 // Decompose string pseudo-instruction MI into a loop that continually performs
5569 // Opcode until CC != 3.
5570 MachineBasicBlock *
5571 SystemZTargetLowering::emitStringWrapper(MachineInstr *MI,
5572                                          MachineBasicBlock *MBB,
5573                                          unsigned Opcode) const {
5574   MachineFunction &MF = *MBB->getParent();
5575   const SystemZInstrInfo *TII =
5576       static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
5577   MachineRegisterInfo &MRI = MF.getRegInfo();
5578   DebugLoc DL = MI->getDebugLoc();
5579 
5580   uint64_t End1Reg   = MI->getOperand(0).getReg();
5581   uint64_t Start1Reg = MI->getOperand(1).getReg();
5582   uint64_t Start2Reg = MI->getOperand(2).getReg();
5583   uint64_t CharReg   = MI->getOperand(3).getReg();
5584 
5585   const TargetRegisterClass *RC = &SystemZ::GR64BitRegClass;
5586   uint64_t This1Reg = MRI.createVirtualRegister(RC);
5587   uint64_t This2Reg = MRI.createVirtualRegister(RC);
5588   uint64_t End2Reg  = MRI.createVirtualRegister(RC);
5589 
5590   MachineBasicBlock *StartMBB = MBB;
5591   MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
5592   MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
5593 
5594   //  StartMBB:
5595   //   # fall through to LoopMMB
5596   MBB->addSuccessor(LoopMBB);
5597 
5598   //  LoopMBB:
5599   //   %This1Reg = phi [ %Start1Reg, StartMBB ], [ %End1Reg, LoopMBB ]
5600   //   %This2Reg = phi [ %Start2Reg, StartMBB ], [ %End2Reg, LoopMBB ]
5601   //   R0L = %CharReg
5602   //   %End1Reg, %End2Reg = CLST %This1Reg, %This2Reg -- uses R0L
5603   //   JO LoopMBB
5604   //   # fall through to DoneMMB
5605   //
5606   // The load of R0L can be hoisted by post-RA LICM.
5607   MBB = LoopMBB;
5608 
5609   BuildMI(MBB, DL, TII->get(SystemZ::PHI), This1Reg)
5610     .addReg(Start1Reg).addMBB(StartMBB)
5611     .addReg(End1Reg).addMBB(LoopMBB);
5612   BuildMI(MBB, DL, TII->get(SystemZ::PHI), This2Reg)
5613     .addReg(Start2Reg).addMBB(StartMBB)
5614     .addReg(End2Reg).addMBB(LoopMBB);
5615   BuildMI(MBB, DL, TII->get(TargetOpcode::COPY), SystemZ::R0L).addReg(CharReg);
5616   BuildMI(MBB, DL, TII->get(Opcode))
5617     .addReg(End1Reg, RegState::Define).addReg(End2Reg, RegState::Define)
5618     .addReg(This1Reg).addReg(This2Reg);
5619   BuildMI(MBB, DL, TII->get(SystemZ::BRC))
5620     .addImm(SystemZ::CCMASK_ANY).addImm(SystemZ::CCMASK_3).addMBB(LoopMBB);
5621   MBB->addSuccessor(LoopMBB);
5622   MBB->addSuccessor(DoneMBB);
5623 
5624   DoneMBB->addLiveIn(SystemZ::CC);
5625 
5626   MI->eraseFromParent();
5627   return DoneMBB;
5628 }
5629 
5630 // Update TBEGIN instruction with final opcode and register clobbers.
5631 MachineBasicBlock *
5632 SystemZTargetLowering::emitTransactionBegin(MachineInstr *MI,
5633                                             MachineBasicBlock *MBB,
5634                                             unsigned Opcode,
5635                                             bool NoFloat) const {
5636   MachineFunction &MF = *MBB->getParent();
5637   const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
5638   const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
5639 
5640   // Update opcode.
5641   MI->setDesc(TII->get(Opcode));
5642 
5643   // We cannot handle a TBEGIN that clobbers the stack or frame pointer.
5644   // Make sure to add the corresponding GRSM bits if they are missing.
5645   uint64_t Control = MI->getOperand(2).getImm();
5646   static const unsigned GPRControlBit[16] = {
5647     0x8000, 0x8000, 0x4000, 0x4000, 0x2000, 0x2000, 0x1000, 0x1000,
5648     0x0800, 0x0800, 0x0400, 0x0400, 0x0200, 0x0200, 0x0100, 0x0100
5649   };
5650   Control |= GPRControlBit[15];
5651   if (TFI->hasFP(MF))
5652     Control |= GPRControlBit[11];
5653   MI->getOperand(2).setImm(Control);
5654 
5655   // Add GPR clobbers.
5656   for (int I = 0; I < 16; I++) {
5657     if ((Control & GPRControlBit[I]) == 0) {
5658       unsigned Reg = SystemZMC::GR64Regs[I];
5659       MI->addOperand(MachineOperand::CreateReg(Reg, true, true));
5660     }
5661   }
5662 
5663   // Add FPR/VR clobbers.
5664   if (!NoFloat && (Control & 4) != 0) {
5665     if (Subtarget.hasVector()) {
5666       for (int I = 0; I < 32; I++) {
5667         unsigned Reg = SystemZMC::VR128Regs[I];
5668         MI->addOperand(MachineOperand::CreateReg(Reg, true, true));
5669       }
5670     } else {
5671       for (int I = 0; I < 16; I++) {
5672         unsigned Reg = SystemZMC::FP64Regs[I];
5673         MI->addOperand(MachineOperand::CreateReg(Reg, true, true));
5674       }
5675     }
5676   }
5677 
5678   return MBB;
5679 }
5680 
5681 MachineBasicBlock *
5682 SystemZTargetLowering::emitLoadAndTestCmp0(MachineInstr *MI,
5683                                           MachineBasicBlock *MBB,
5684                                           unsigned Opcode) const {
5685   MachineFunction &MF = *MBB->getParent();
5686   MachineRegisterInfo *MRI = &MF.getRegInfo();
5687   const SystemZInstrInfo *TII =
5688       static_cast<const SystemZInstrInfo *>(Subtarget.getInstrInfo());
5689   DebugLoc DL = MI->getDebugLoc();
5690 
5691   unsigned SrcReg = MI->getOperand(0).getReg();
5692 
5693   // Create new virtual register of the same class as source.
5694   const TargetRegisterClass *RC = MRI->getRegClass(SrcReg);
5695   unsigned DstReg = MRI->createVirtualRegister(RC);
5696 
5697   // Replace pseudo with a normal load-and-test that models the def as
5698   // well.
5699   BuildMI(*MBB, MI, DL, TII->get(Opcode), DstReg)
5700     .addReg(SrcReg);
5701   MI->eraseFromParent();
5702 
5703   return MBB;
5704 }
5705 
5706 MachineBasicBlock *SystemZTargetLowering::
5707 EmitInstrWithCustomInserter(MachineInstr *MI, MachineBasicBlock *MBB) const {
5708   switch (MI->getOpcode()) {
5709   case SystemZ::Select32Mux:
5710   case SystemZ::Select32:
5711   case SystemZ::SelectF32:
5712   case SystemZ::Select64:
5713   case SystemZ::SelectF64:
5714   case SystemZ::SelectF128:
5715     return emitSelect(MI, MBB);
5716 
5717   case SystemZ::CondStore8Mux:
5718     return emitCondStore(MI, MBB, SystemZ::STCMux, 0, false);
5719   case SystemZ::CondStore8MuxInv:
5720     return emitCondStore(MI, MBB, SystemZ::STCMux, 0, true);
5721   case SystemZ::CondStore16Mux:
5722     return emitCondStore(MI, MBB, SystemZ::STHMux, 0, false);
5723   case SystemZ::CondStore16MuxInv:
5724     return emitCondStore(MI, MBB, SystemZ::STHMux, 0, true);
5725   case SystemZ::CondStore8:
5726     return emitCondStore(MI, MBB, SystemZ::STC, 0, false);
5727   case SystemZ::CondStore8Inv:
5728     return emitCondStore(MI, MBB, SystemZ::STC, 0, true);
5729   case SystemZ::CondStore16:
5730     return emitCondStore(MI, MBB, SystemZ::STH, 0, false);
5731   case SystemZ::CondStore16Inv:
5732     return emitCondStore(MI, MBB, SystemZ::STH, 0, true);
5733   case SystemZ::CondStore32:
5734     return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, false);
5735   case SystemZ::CondStore32Inv:
5736     return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, true);
5737   case SystemZ::CondStore64:
5738     return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, false);
5739   case SystemZ::CondStore64Inv:
5740     return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, true);
5741   case SystemZ::CondStoreF32:
5742     return emitCondStore(MI, MBB, SystemZ::STE, 0, false);
5743   case SystemZ::CondStoreF32Inv:
5744     return emitCondStore(MI, MBB, SystemZ::STE, 0, true);
5745   case SystemZ::CondStoreF64:
5746     return emitCondStore(MI, MBB, SystemZ::STD, 0, false);
5747   case SystemZ::CondStoreF64Inv:
5748     return emitCondStore(MI, MBB, SystemZ::STD, 0, true);
5749 
5750   case SystemZ::AEXT128_64:
5751     return emitExt128(MI, MBB, false, SystemZ::subreg_l64);
5752   case SystemZ::ZEXT128_32:
5753     return emitExt128(MI, MBB, true, SystemZ::subreg_l32);
5754   case SystemZ::ZEXT128_64:
5755     return emitExt128(MI, MBB, true, SystemZ::subreg_l64);
5756 
5757   case SystemZ::ATOMIC_SWAPW:
5758     return emitAtomicLoadBinary(MI, MBB, 0, 0);
5759   case SystemZ::ATOMIC_SWAP_32:
5760     return emitAtomicLoadBinary(MI, MBB, 0, 32);
5761   case SystemZ::ATOMIC_SWAP_64:
5762     return emitAtomicLoadBinary(MI, MBB, 0, 64);
5763 
5764   case SystemZ::ATOMIC_LOADW_AR:
5765     return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 0);
5766   case SystemZ::ATOMIC_LOADW_AFI:
5767     return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 0);
5768   case SystemZ::ATOMIC_LOAD_AR:
5769     return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 32);
5770   case SystemZ::ATOMIC_LOAD_AHI:
5771     return emitAtomicLoadBinary(MI, MBB, SystemZ::AHI, 32);
5772   case SystemZ::ATOMIC_LOAD_AFI:
5773     return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 32);
5774   case SystemZ::ATOMIC_LOAD_AGR:
5775     return emitAtomicLoadBinary(MI, MBB, SystemZ::AGR, 64);
5776   case SystemZ::ATOMIC_LOAD_AGHI:
5777     return emitAtomicLoadBinary(MI, MBB, SystemZ::AGHI, 64);
5778   case SystemZ::ATOMIC_LOAD_AGFI:
5779     return emitAtomicLoadBinary(MI, MBB, SystemZ::AGFI, 64);
5780 
5781   case SystemZ::ATOMIC_LOADW_SR:
5782     return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 0);
5783   case SystemZ::ATOMIC_LOAD_SR:
5784     return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 32);
5785   case SystemZ::ATOMIC_LOAD_SGR:
5786     return emitAtomicLoadBinary(MI, MBB, SystemZ::SGR, 64);
5787 
5788   case SystemZ::ATOMIC_LOADW_NR:
5789     return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0);
5790   case SystemZ::ATOMIC_LOADW_NILH:
5791     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0);
5792   case SystemZ::ATOMIC_LOAD_NR:
5793     return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32);
5794   case SystemZ::ATOMIC_LOAD_NILL:
5795     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32);
5796   case SystemZ::ATOMIC_LOAD_NILH:
5797     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32);
5798   case SystemZ::ATOMIC_LOAD_NILF:
5799     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32);
5800   case SystemZ::ATOMIC_LOAD_NGR:
5801     return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64);
5802   case SystemZ::ATOMIC_LOAD_NILL64:
5803     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64);
5804   case SystemZ::ATOMIC_LOAD_NILH64:
5805     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64);
5806   case SystemZ::ATOMIC_LOAD_NIHL64:
5807     return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64);
5808   case SystemZ::ATOMIC_LOAD_NIHH64:
5809     return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64);
5810   case SystemZ::ATOMIC_LOAD_NILF64:
5811     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64);
5812   case SystemZ::ATOMIC_LOAD_NIHF64:
5813     return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64);
5814 
5815   case SystemZ::ATOMIC_LOADW_OR:
5816     return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 0);
5817   case SystemZ::ATOMIC_LOADW_OILH:
5818     return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 0);
5819   case SystemZ::ATOMIC_LOAD_OR:
5820     return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 32);
5821   case SystemZ::ATOMIC_LOAD_OILL:
5822     return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL, 32);
5823   case SystemZ::ATOMIC_LOAD_OILH:
5824     return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 32);
5825   case SystemZ::ATOMIC_LOAD_OILF:
5826     return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF, 32);
5827   case SystemZ::ATOMIC_LOAD_OGR:
5828     return emitAtomicLoadBinary(MI, MBB, SystemZ::OGR, 64);
5829   case SystemZ::ATOMIC_LOAD_OILL64:
5830     return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL64, 64);
5831   case SystemZ::ATOMIC_LOAD_OILH64:
5832     return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH64, 64);
5833   case SystemZ::ATOMIC_LOAD_OIHL64:
5834     return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHL64, 64);
5835   case SystemZ::ATOMIC_LOAD_OIHH64:
5836     return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHH64, 64);
5837   case SystemZ::ATOMIC_LOAD_OILF64:
5838     return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF64, 64);
5839   case SystemZ::ATOMIC_LOAD_OIHF64:
5840     return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHF64, 64);
5841 
5842   case SystemZ::ATOMIC_LOADW_XR:
5843     return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 0);
5844   case SystemZ::ATOMIC_LOADW_XILF:
5845     return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 0);
5846   case SystemZ::ATOMIC_LOAD_XR:
5847     return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 32);
5848   case SystemZ::ATOMIC_LOAD_XILF:
5849     return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 32);
5850   case SystemZ::ATOMIC_LOAD_XGR:
5851     return emitAtomicLoadBinary(MI, MBB, SystemZ::XGR, 64);
5852   case SystemZ::ATOMIC_LOAD_XILF64:
5853     return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF64, 64);
5854   case SystemZ::ATOMIC_LOAD_XIHF64:
5855     return emitAtomicLoadBinary(MI, MBB, SystemZ::XIHF64, 64);
5856 
5857   case SystemZ::ATOMIC_LOADW_NRi:
5858     return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0, true);
5859   case SystemZ::ATOMIC_LOADW_NILHi:
5860     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0, true);
5861   case SystemZ::ATOMIC_LOAD_NRi:
5862     return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32, true);
5863   case SystemZ::ATOMIC_LOAD_NILLi:
5864     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32, true);
5865   case SystemZ::ATOMIC_LOAD_NILHi:
5866     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32, true);
5867   case SystemZ::ATOMIC_LOAD_NILFi:
5868     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32, true);
5869   case SystemZ::ATOMIC_LOAD_NGRi:
5870     return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64, true);
5871   case SystemZ::ATOMIC_LOAD_NILL64i:
5872     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64, true);
5873   case SystemZ::ATOMIC_LOAD_NILH64i:
5874     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64, true);
5875   case SystemZ::ATOMIC_LOAD_NIHL64i:
5876     return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64, true);
5877   case SystemZ::ATOMIC_LOAD_NIHH64i:
5878     return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64, true);
5879   case SystemZ::ATOMIC_LOAD_NILF64i:
5880     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64, true);
5881   case SystemZ::ATOMIC_LOAD_NIHF64i:
5882     return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64, true);
5883 
5884   case SystemZ::ATOMIC_LOADW_MIN:
5885     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
5886                                 SystemZ::CCMASK_CMP_LE, 0);
5887   case SystemZ::ATOMIC_LOAD_MIN_32:
5888     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
5889                                 SystemZ::CCMASK_CMP_LE, 32);
5890   case SystemZ::ATOMIC_LOAD_MIN_64:
5891     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
5892                                 SystemZ::CCMASK_CMP_LE, 64);
5893 
5894   case SystemZ::ATOMIC_LOADW_MAX:
5895     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
5896                                 SystemZ::CCMASK_CMP_GE, 0);
5897   case SystemZ::ATOMIC_LOAD_MAX_32:
5898     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
5899                                 SystemZ::CCMASK_CMP_GE, 32);
5900   case SystemZ::ATOMIC_LOAD_MAX_64:
5901     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
5902                                 SystemZ::CCMASK_CMP_GE, 64);
5903 
5904   case SystemZ::ATOMIC_LOADW_UMIN:
5905     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
5906                                 SystemZ::CCMASK_CMP_LE, 0);
5907   case SystemZ::ATOMIC_LOAD_UMIN_32:
5908     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
5909                                 SystemZ::CCMASK_CMP_LE, 32);
5910   case SystemZ::ATOMIC_LOAD_UMIN_64:
5911     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
5912                                 SystemZ::CCMASK_CMP_LE, 64);
5913 
5914   case SystemZ::ATOMIC_LOADW_UMAX:
5915     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
5916                                 SystemZ::CCMASK_CMP_GE, 0);
5917   case SystemZ::ATOMIC_LOAD_UMAX_32:
5918     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
5919                                 SystemZ::CCMASK_CMP_GE, 32);
5920   case SystemZ::ATOMIC_LOAD_UMAX_64:
5921     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
5922                                 SystemZ::CCMASK_CMP_GE, 64);
5923 
5924   case SystemZ::ATOMIC_CMP_SWAPW:
5925     return emitAtomicCmpSwapW(MI, MBB);
5926   case SystemZ::MVCSequence:
5927   case SystemZ::MVCLoop:
5928     return emitMemMemWrapper(MI, MBB, SystemZ::MVC);
5929   case SystemZ::NCSequence:
5930   case SystemZ::NCLoop:
5931     return emitMemMemWrapper(MI, MBB, SystemZ::NC);
5932   case SystemZ::OCSequence:
5933   case SystemZ::OCLoop:
5934     return emitMemMemWrapper(MI, MBB, SystemZ::OC);
5935   case SystemZ::XCSequence:
5936   case SystemZ::XCLoop:
5937     return emitMemMemWrapper(MI, MBB, SystemZ::XC);
5938   case SystemZ::CLCSequence:
5939   case SystemZ::CLCLoop:
5940     return emitMemMemWrapper(MI, MBB, SystemZ::CLC);
5941   case SystemZ::CLSTLoop:
5942     return emitStringWrapper(MI, MBB, SystemZ::CLST);
5943   case SystemZ::MVSTLoop:
5944     return emitStringWrapper(MI, MBB, SystemZ::MVST);
5945   case SystemZ::SRSTLoop:
5946     return emitStringWrapper(MI, MBB, SystemZ::SRST);
5947   case SystemZ::TBEGIN:
5948     return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, false);
5949   case SystemZ::TBEGIN_nofloat:
5950     return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, true);
5951   case SystemZ::TBEGINC:
5952     return emitTransactionBegin(MI, MBB, SystemZ::TBEGINC, true);
5953   case SystemZ::LTEBRCompare_VecPseudo:
5954     return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTEBR);
5955   case SystemZ::LTDBRCompare_VecPseudo:
5956     return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTDBR);
5957   case SystemZ::LTXBRCompare_VecPseudo:
5958     return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTXBR);
5959 
5960   default:
5961     llvm_unreachable("Unexpected instr type to insert");
5962   }
5963 }
5964