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