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