[SystemZ] Use "auto" for cast results
[oota-llvm.git] / lib / Target / SystemZ / SystemZISelLowering.cpp
1 //===-- SystemZISelLowering.cpp - SystemZ DAG lowering implementation -----===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the SystemZTargetLowering class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "systemz-lower"
15
16 #include "SystemZISelLowering.h"
17 #include "SystemZCallingConv.h"
18 #include "SystemZConstantPoolValue.h"
19 #include "SystemZMachineFunctionInfo.h"
20 #include "SystemZTargetMachine.h"
21 #include "llvm/CodeGen/CallingConvLower.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/MachineRegisterInfo.h"
24 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
25 #include <cctype>
26
27 using namespace llvm;
28
29 namespace {
30 // Represents a sequence for extracting a 0/1 value from an IPM result:
31 // (((X ^ XORValue) + AddValue) >> Bit)
32 struct IPMConversion {
33   IPMConversion(unsigned xorValue, int64_t addValue, unsigned bit)
34     : XORValue(xorValue), AddValue(addValue), Bit(bit) {}
35
36   int64_t XORValue;
37   int64_t AddValue;
38   unsigned Bit;
39 };
40
41 // Represents information about a comparison.
42 struct Comparison {
43   Comparison(SDValue Op0In, SDValue Op1In)
44     : Op0(Op0In), Op1(Op1In), Opcode(0), ICmpType(0), CCValid(0), CCMask(0) {}
45
46   // The operands to the comparison.
47   SDValue Op0, Op1;
48
49   // The opcode that should be used to compare Op0 and Op1.
50   unsigned Opcode;
51
52   // A SystemZICMP value.  Only used for integer comparisons.
53   unsigned ICmpType;
54
55   // The mask of CC values that Opcode can produce.
56   unsigned CCValid;
57
58   // The mask of CC values for which the original condition is true.
59   unsigned CCMask;
60 };
61 } // end anonymous namespace
62
63 // Classify VT as either 32 or 64 bit.
64 static bool is32Bit(EVT VT) {
65   switch (VT.getSimpleVT().SimpleTy) {
66   case MVT::i32:
67     return true;
68   case MVT::i64:
69     return false;
70   default:
71     llvm_unreachable("Unsupported type");
72   }
73 }
74
75 // Return a version of MachineOperand that can be safely used before the
76 // final use.
77 static MachineOperand earlyUseOperand(MachineOperand Op) {
78   if (Op.isReg())
79     Op.setIsKill(false);
80   return Op;
81 }
82
83 SystemZTargetLowering::SystemZTargetLowering(SystemZTargetMachine &tm)
84   : TargetLowering(tm, new TargetLoweringObjectFileELF()),
85     Subtarget(*tm.getSubtargetImpl()), TM(tm) {
86   MVT PtrVT = getPointerTy();
87
88   // Set up the register classes.
89   if (Subtarget.hasHighWord())
90     addRegisterClass(MVT::i32, &SystemZ::GRX32BitRegClass);
91   else
92     addRegisterClass(MVT::i32, &SystemZ::GR32BitRegClass);
93   addRegisterClass(MVT::i64,  &SystemZ::GR64BitRegClass);
94   addRegisterClass(MVT::f32,  &SystemZ::FP32BitRegClass);
95   addRegisterClass(MVT::f64,  &SystemZ::FP64BitRegClass);
96   addRegisterClass(MVT::f128, &SystemZ::FP128BitRegClass);
97
98   // Compute derived properties from the register classes
99   computeRegisterProperties();
100
101   // Set up special registers.
102   setExceptionPointerRegister(SystemZ::R6D);
103   setExceptionSelectorRegister(SystemZ::R7D);
104   setStackPointerRegisterToSaveRestore(SystemZ::R15D);
105
106   // TODO: It may be better to default to latency-oriented scheduling, however
107   // LLVM's current latency-oriented scheduler can't handle physreg definitions
108   // such as SystemZ has with CC, so set this to the register-pressure
109   // scheduler, because it can.
110   setSchedulingPreference(Sched::RegPressure);
111
112   setBooleanContents(ZeroOrOneBooleanContent);
113   setBooleanVectorContents(ZeroOrOneBooleanContent); // FIXME: Is this correct?
114
115   // Instructions are strings of 2-byte aligned 2-byte values.
116   setMinFunctionAlignment(2);
117
118   // Handle operations that are handled in a similar way for all types.
119   for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
120        I <= MVT::LAST_FP_VALUETYPE;
121        ++I) {
122     MVT VT = MVT::SimpleValueType(I);
123     if (isTypeLegal(VT)) {
124       // Lower SET_CC into an IPM-based sequence.
125       setOperationAction(ISD::SETCC, VT, Custom);
126
127       // Expand SELECT(C, A, B) into SELECT_CC(X, 0, A, B, NE).
128       setOperationAction(ISD::SELECT, VT, Expand);
129
130       // Lower SELECT_CC and BR_CC into separate comparisons and branches.
131       setOperationAction(ISD::SELECT_CC, VT, Custom);
132       setOperationAction(ISD::BR_CC,     VT, Custom);
133     }
134   }
135
136   // Expand jump table branches as address arithmetic followed by an
137   // indirect jump.
138   setOperationAction(ISD::BR_JT, MVT::Other, Expand);
139
140   // Expand BRCOND into a BR_CC (see above).
141   setOperationAction(ISD::BRCOND, MVT::Other, Expand);
142
143   // Handle integer types.
144   for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
145        I <= MVT::LAST_INTEGER_VALUETYPE;
146        ++I) {
147     MVT VT = MVT::SimpleValueType(I);
148     if (isTypeLegal(VT)) {
149       // Expand individual DIV and REMs into DIVREMs.
150       setOperationAction(ISD::SDIV, VT, Expand);
151       setOperationAction(ISD::UDIV, VT, Expand);
152       setOperationAction(ISD::SREM, VT, Expand);
153       setOperationAction(ISD::UREM, VT, Expand);
154       setOperationAction(ISD::SDIVREM, VT, Custom);
155       setOperationAction(ISD::UDIVREM, VT, Custom);
156
157       // Lower ATOMIC_LOAD and ATOMIC_STORE into normal volatile loads and
158       // stores, putting a serialization instruction after the stores.
159       setOperationAction(ISD::ATOMIC_LOAD,  VT, Custom);
160       setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
161
162       // Lower ATOMIC_LOAD_SUB into ATOMIC_LOAD_ADD if LAA and LAAG are
163       // available, or if the operand is constant.
164       setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
165
166       // No special instructions for these.
167       setOperationAction(ISD::CTPOP,           VT, Expand);
168       setOperationAction(ISD::CTTZ,            VT, Expand);
169       setOperationAction(ISD::CTTZ_ZERO_UNDEF, VT, Expand);
170       setOperationAction(ISD::CTLZ_ZERO_UNDEF, VT, Expand);
171       setOperationAction(ISD::ROTR,            VT, Expand);
172
173       // Use *MUL_LOHI where possible instead of MULH*.
174       setOperationAction(ISD::MULHS, VT, Expand);
175       setOperationAction(ISD::MULHU, VT, Expand);
176       setOperationAction(ISD::SMUL_LOHI, VT, Custom);
177       setOperationAction(ISD::UMUL_LOHI, VT, Custom);
178
179       // We have instructions for signed but not unsigned FP conversion.
180       setOperationAction(ISD::FP_TO_UINT, VT, Expand);
181     }
182   }
183
184   // Type legalization will convert 8- and 16-bit atomic operations into
185   // forms that operate on i32s (but still keeping the original memory VT).
186   // Lower them into full i32 operations.
187   setOperationAction(ISD::ATOMIC_SWAP,      MVT::i32, Custom);
188   setOperationAction(ISD::ATOMIC_LOAD_ADD,  MVT::i32, Custom);
189   setOperationAction(ISD::ATOMIC_LOAD_SUB,  MVT::i32, Custom);
190   setOperationAction(ISD::ATOMIC_LOAD_AND,  MVT::i32, Custom);
191   setOperationAction(ISD::ATOMIC_LOAD_OR,   MVT::i32, Custom);
192   setOperationAction(ISD::ATOMIC_LOAD_XOR,  MVT::i32, Custom);
193   setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Custom);
194   setOperationAction(ISD::ATOMIC_LOAD_MIN,  MVT::i32, Custom);
195   setOperationAction(ISD::ATOMIC_LOAD_MAX,  MVT::i32, Custom);
196   setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Custom);
197   setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Custom);
198   setOperationAction(ISD::ATOMIC_CMP_SWAP,  MVT::i32, Custom);
199
200   // We have instructions for signed but not unsigned FP conversion.
201   // Handle unsigned 32-bit types as signed 64-bit types.
202   setOperationAction(ISD::UINT_TO_FP, MVT::i32, Promote);
203   setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
204
205   // We have native support for a 64-bit CTLZ, via FLOGR.
206   setOperationAction(ISD::CTLZ, MVT::i32, Promote);
207   setOperationAction(ISD::CTLZ, MVT::i64, Legal);
208
209   // Give LowerOperation the chance to replace 64-bit ORs with subregs.
210   setOperationAction(ISD::OR, MVT::i64, Custom);
211
212   // Give LowerOperation the chance to optimize SIGN_EXTEND sequences.
213   setOperationAction(ISD::SIGN_EXTEND, MVT::i64, Custom);
214
215   // FIXME: Can we support these natively?
216   setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
217   setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
218   setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
219
220   // We have native instructions for i8, i16 and i32 extensions, but not i1.
221   setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
222   setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
223   setLoadExtAction(ISD::EXTLOAD,  MVT::i1, Promote);
224   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
225
226   // Handle the various types of symbolic address.
227   setOperationAction(ISD::ConstantPool,     PtrVT, Custom);
228   setOperationAction(ISD::GlobalAddress,    PtrVT, Custom);
229   setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
230   setOperationAction(ISD::BlockAddress,     PtrVT, Custom);
231   setOperationAction(ISD::JumpTable,        PtrVT, Custom);
232
233   // We need to handle dynamic allocations specially because of the
234   // 160-byte area at the bottom of the stack.
235   setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom);
236
237   // Use custom expanders so that we can force the function to use
238   // a frame pointer.
239   setOperationAction(ISD::STACKSAVE,    MVT::Other, Custom);
240   setOperationAction(ISD::STACKRESTORE, MVT::Other, Custom);
241
242   // Handle prefetches with PFD or PFDRL.
243   setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
244
245   // Handle floating-point types.
246   for (unsigned I = MVT::FIRST_FP_VALUETYPE;
247        I <= MVT::LAST_FP_VALUETYPE;
248        ++I) {
249     MVT VT = MVT::SimpleValueType(I);
250     if (isTypeLegal(VT)) {
251       // We can use FI for FRINT.
252       setOperationAction(ISD::FRINT, VT, Legal);
253
254       // We can use the extended form of FI for other rounding operations.
255       if (Subtarget.hasFPExtension()) {
256         setOperationAction(ISD::FNEARBYINT, VT, Legal);
257         setOperationAction(ISD::FFLOOR, VT, Legal);
258         setOperationAction(ISD::FCEIL, VT, Legal);
259         setOperationAction(ISD::FTRUNC, VT, Legal);
260         setOperationAction(ISD::FROUND, VT, Legal);
261       }
262
263       // No special instructions for these.
264       setOperationAction(ISD::FSIN, VT, Expand);
265       setOperationAction(ISD::FCOS, VT, Expand);
266       setOperationAction(ISD::FREM, VT, Expand);
267     }
268   }
269
270   // We have fused multiply-addition for f32 and f64 but not f128.
271   setOperationAction(ISD::FMA, MVT::f32,  Legal);
272   setOperationAction(ISD::FMA, MVT::f64,  Legal);
273   setOperationAction(ISD::FMA, MVT::f128, Expand);
274
275   // Needed so that we don't try to implement f128 constant loads using
276   // a load-and-extend of a f80 constant (in cases where the constant
277   // would fit in an f80).
278   setLoadExtAction(ISD::EXTLOAD, MVT::f80, Expand);
279
280   // Floating-point truncation and stores need to be done separately.
281   setTruncStoreAction(MVT::f64,  MVT::f32, Expand);
282   setTruncStoreAction(MVT::f128, MVT::f32, Expand);
283   setTruncStoreAction(MVT::f128, MVT::f64, Expand);
284
285   // We have 64-bit FPR<->GPR moves, but need special handling for
286   // 32-bit forms.
287   setOperationAction(ISD::BITCAST, MVT::i32, Custom);
288   setOperationAction(ISD::BITCAST, MVT::f32, Custom);
289
290   // VASTART and VACOPY need to deal with the SystemZ-specific varargs
291   // structure, but VAEND is a no-op.
292   setOperationAction(ISD::VASTART, MVT::Other, Custom);
293   setOperationAction(ISD::VACOPY,  MVT::Other, Custom);
294   setOperationAction(ISD::VAEND,   MVT::Other, Expand);
295
296   // We want to use MVC in preference to even a single load/store pair.
297   MaxStoresPerMemcpy = 0;
298   MaxStoresPerMemcpyOptSize = 0;
299
300   // The main memset sequence is a byte store followed by an MVC.
301   // Two STC or MV..I stores win over that, but the kind of fused stores
302   // generated by target-independent code don't when the byte value is
303   // variable.  E.g.  "STC <reg>;MHI <reg>,257;STH <reg>" is not better
304   // than "STC;MVC".  Handle the choice in target-specific code instead.
305   MaxStoresPerMemset = 0;
306   MaxStoresPerMemsetOptSize = 0;
307 }
308
309 EVT SystemZTargetLowering::getSetCCResultType(LLVMContext &, EVT VT) const {
310   if (!VT.isVector())
311     return MVT::i32;
312   return VT.changeVectorElementTypeToInteger();
313 }
314
315 bool SystemZTargetLowering::isFMAFasterThanFMulAndFAdd(EVT VT) const {
316   VT = VT.getScalarType();
317
318   if (!VT.isSimple())
319     return false;
320
321   switch (VT.getSimpleVT().SimpleTy) {
322   case MVT::f32:
323   case MVT::f64:
324     return true;
325   case MVT::f128:
326     return false;
327   default:
328     break;
329   }
330
331   return false;
332 }
333
334 bool SystemZTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT) const {
335   // We can load zero using LZ?R and negative zero using LZ?R;LC?BR.
336   return Imm.isZero() || Imm.isNegZero();
337 }
338
339 bool SystemZTargetLowering::allowsUnalignedMemoryAccesses(EVT VT,
340                                                           unsigned,
341                                                           bool *Fast) const {
342   // Unaligned accesses should never be slower than the expanded version.
343   // We check specifically for aligned accesses in the few cases where
344   // they are required.
345   if (Fast)
346     *Fast = true;
347   return true;
348 }
349   
350 bool SystemZTargetLowering::isLegalAddressingMode(const AddrMode &AM,
351                                                   Type *Ty) const {
352   // Punt on globals for now, although they can be used in limited
353   // RELATIVE LONG cases.
354   if (AM.BaseGV)
355     return false;
356
357   // Require a 20-bit signed offset.
358   if (!isInt<20>(AM.BaseOffs))
359     return false;
360
361   // Indexing is OK but no scale factor can be applied.
362   return AM.Scale == 0 || AM.Scale == 1;
363 }
364
365 bool SystemZTargetLowering::isTruncateFree(Type *FromType, Type *ToType) const {
366   if (!FromType->isIntegerTy() || !ToType->isIntegerTy())
367     return false;
368   unsigned FromBits = FromType->getPrimitiveSizeInBits();
369   unsigned ToBits = ToType->getPrimitiveSizeInBits();
370   return FromBits > ToBits;
371 }
372
373 bool SystemZTargetLowering::isTruncateFree(EVT FromVT, EVT ToVT) const {
374   if (!FromVT.isInteger() || !ToVT.isInteger())
375     return false;
376   unsigned FromBits = FromVT.getSizeInBits();
377   unsigned ToBits = ToVT.getSizeInBits();
378   return FromBits > ToBits;
379 }
380
381 //===----------------------------------------------------------------------===//
382 // Inline asm support
383 //===----------------------------------------------------------------------===//
384
385 TargetLowering::ConstraintType
386 SystemZTargetLowering::getConstraintType(const std::string &Constraint) const {
387   if (Constraint.size() == 1) {
388     switch (Constraint[0]) {
389     case 'a': // Address register
390     case 'd': // Data register (equivalent to 'r')
391     case 'f': // Floating-point register
392     case 'h': // High-part register
393     case 'r': // General-purpose register
394       return C_RegisterClass;
395
396     case 'Q': // Memory with base and unsigned 12-bit displacement
397     case 'R': // Likewise, plus an index
398     case 'S': // Memory with base and signed 20-bit displacement
399     case 'T': // Likewise, plus an index
400     case 'm': // Equivalent to 'T'.
401       return C_Memory;
402
403     case 'I': // Unsigned 8-bit constant
404     case 'J': // Unsigned 12-bit constant
405     case 'K': // Signed 16-bit constant
406     case 'L': // Signed 20-bit displacement (on all targets we support)
407     case 'M': // 0x7fffffff
408       return C_Other;
409
410     default:
411       break;
412     }
413   }
414   return TargetLowering::getConstraintType(Constraint);
415 }
416
417 TargetLowering::ConstraintWeight SystemZTargetLowering::
418 getSingleConstraintMatchWeight(AsmOperandInfo &info,
419                                const char *constraint) const {
420   ConstraintWeight weight = CW_Invalid;
421   Value *CallOperandVal = info.CallOperandVal;
422   // If we don't have a value, we can't do a match,
423   // but allow it at the lowest weight.
424   if (CallOperandVal == NULL)
425     return CW_Default;
426   Type *type = CallOperandVal->getType();
427   // Look at the constraint type.
428   switch (*constraint) {
429   default:
430     weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
431     break;
432
433   case 'a': // Address register
434   case 'd': // Data register (equivalent to 'r')
435   case 'h': // High-part register
436   case 'r': // General-purpose register
437     if (CallOperandVal->getType()->isIntegerTy())
438       weight = CW_Register;
439     break;
440
441   case 'f': // Floating-point register
442     if (type->isFloatingPointTy())
443       weight = CW_Register;
444     break;
445
446   case 'I': // Unsigned 8-bit constant
447     if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
448       if (isUInt<8>(C->getZExtValue()))
449         weight = CW_Constant;
450     break;
451
452   case 'J': // Unsigned 12-bit constant
453     if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
454       if (isUInt<12>(C->getZExtValue()))
455         weight = CW_Constant;
456     break;
457
458   case 'K': // Signed 16-bit constant
459     if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
460       if (isInt<16>(C->getSExtValue()))
461         weight = CW_Constant;
462     break;
463
464   case 'L': // Signed 20-bit displacement (on all targets we support)
465     if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
466       if (isInt<20>(C->getSExtValue()))
467         weight = CW_Constant;
468     break;
469
470   case 'M': // 0x7fffffff
471     if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
472       if (C->getZExtValue() == 0x7fffffff)
473         weight = CW_Constant;
474     break;
475   }
476   return weight;
477 }
478
479 // Parse a "{tNNN}" register constraint for which the register type "t"
480 // has already been verified.  MC is the class associated with "t" and
481 // Map maps 0-based register numbers to LLVM register numbers.
482 static std::pair<unsigned, const TargetRegisterClass *>
483 parseRegisterNumber(const std::string &Constraint,
484                     const TargetRegisterClass *RC, const unsigned *Map) {
485   assert(*(Constraint.end()-1) == '}' && "Missing '}'");
486   if (isdigit(Constraint[2])) {
487     std::string Suffix(Constraint.data() + 2, Constraint.size() - 2);
488     unsigned Index = atoi(Suffix.c_str());
489     if (Index < 16 && Map[Index])
490       return std::make_pair(Map[Index], RC);
491   }
492   return std::make_pair(0u, static_cast<TargetRegisterClass*>(0));
493 }
494
495 std::pair<unsigned, const TargetRegisterClass *> SystemZTargetLowering::
496 getRegForInlineAsmConstraint(const std::string &Constraint, MVT VT) const {
497   if (Constraint.size() == 1) {
498     // GCC Constraint Letters
499     switch (Constraint[0]) {
500     default: break;
501     case 'd': // Data register (equivalent to 'r')
502     case 'r': // General-purpose register
503       if (VT == MVT::i64)
504         return std::make_pair(0U, &SystemZ::GR64BitRegClass);
505       else if (VT == MVT::i128)
506         return std::make_pair(0U, &SystemZ::GR128BitRegClass);
507       return std::make_pair(0U, &SystemZ::GR32BitRegClass);
508
509     case 'a': // Address register
510       if (VT == MVT::i64)
511         return std::make_pair(0U, &SystemZ::ADDR64BitRegClass);
512       else if (VT == MVT::i128)
513         return std::make_pair(0U, &SystemZ::ADDR128BitRegClass);
514       return std::make_pair(0U, &SystemZ::ADDR32BitRegClass);
515
516     case 'h': // High-part register (an LLVM extension)
517       return std::make_pair(0U, &SystemZ::GRH32BitRegClass);
518
519     case 'f': // Floating-point register
520       if (VT == MVT::f64)
521         return std::make_pair(0U, &SystemZ::FP64BitRegClass);
522       else if (VT == MVT::f128)
523         return std::make_pair(0U, &SystemZ::FP128BitRegClass);
524       return std::make_pair(0U, &SystemZ::FP32BitRegClass);
525     }
526   }
527   if (Constraint[0] == '{') {
528     // We need to override the default register parsing for GPRs and FPRs
529     // because the interpretation depends on VT.  The internal names of
530     // the registers are also different from the external names
531     // (F0D and F0S instead of F0, etc.).
532     if (Constraint[1] == 'r') {
533       if (VT == MVT::i32)
534         return parseRegisterNumber(Constraint, &SystemZ::GR32BitRegClass,
535                                    SystemZMC::GR32Regs);
536       if (VT == MVT::i128)
537         return parseRegisterNumber(Constraint, &SystemZ::GR128BitRegClass,
538                                    SystemZMC::GR128Regs);
539       return parseRegisterNumber(Constraint, &SystemZ::GR64BitRegClass,
540                                  SystemZMC::GR64Regs);
541     }
542     if (Constraint[1] == 'f') {
543       if (VT == MVT::f32)
544         return parseRegisterNumber(Constraint, &SystemZ::FP32BitRegClass,
545                                    SystemZMC::FP32Regs);
546       if (VT == MVT::f128)
547         return parseRegisterNumber(Constraint, &SystemZ::FP128BitRegClass,
548                                    SystemZMC::FP128Regs);
549       return parseRegisterNumber(Constraint, &SystemZ::FP64BitRegClass,
550                                  SystemZMC::FP64Regs);
551     }
552   }
553   return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
554 }
555
556 void SystemZTargetLowering::
557 LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint,
558                              std::vector<SDValue> &Ops,
559                              SelectionDAG &DAG) const {
560   // Only support length 1 constraints for now.
561   if (Constraint.length() == 1) {
562     switch (Constraint[0]) {
563     case 'I': // Unsigned 8-bit constant
564       if (auto *C = dyn_cast<ConstantSDNode>(Op))
565         if (isUInt<8>(C->getZExtValue()))
566           Ops.push_back(DAG.getTargetConstant(C->getZExtValue(),
567                                               Op.getValueType()));
568       return;
569
570     case 'J': // Unsigned 12-bit constant
571       if (auto *C = dyn_cast<ConstantSDNode>(Op))
572         if (isUInt<12>(C->getZExtValue()))
573           Ops.push_back(DAG.getTargetConstant(C->getZExtValue(),
574                                               Op.getValueType()));
575       return;
576
577     case 'K': // Signed 16-bit constant
578       if (auto *C = dyn_cast<ConstantSDNode>(Op))
579         if (isInt<16>(C->getSExtValue()))
580           Ops.push_back(DAG.getTargetConstant(C->getSExtValue(),
581                                               Op.getValueType()));
582       return;
583
584     case 'L': // Signed 20-bit displacement (on all targets we support)
585       if (auto *C = dyn_cast<ConstantSDNode>(Op))
586         if (isInt<20>(C->getSExtValue()))
587           Ops.push_back(DAG.getTargetConstant(C->getSExtValue(),
588                                               Op.getValueType()));
589       return;
590
591     case 'M': // 0x7fffffff
592       if (auto *C = dyn_cast<ConstantSDNode>(Op))
593         if (C->getZExtValue() == 0x7fffffff)
594           Ops.push_back(DAG.getTargetConstant(C->getZExtValue(),
595                                               Op.getValueType()));
596       return;
597     }
598   }
599   TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
600 }
601
602 //===----------------------------------------------------------------------===//
603 // Calling conventions
604 //===----------------------------------------------------------------------===//
605
606 #include "SystemZGenCallingConv.inc"
607
608 bool SystemZTargetLowering::allowTruncateForTailCall(Type *FromType,
609                                                      Type *ToType) const {
610   return isTruncateFree(FromType, ToType);
611 }
612
613 bool SystemZTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
614   if (!CI->isTailCall())
615     return false;
616   return true;
617 }
618
619 // Value is a value that has been passed to us in the location described by VA
620 // (and so has type VA.getLocVT()).  Convert Value to VA.getValVT(), chaining
621 // any loads onto Chain.
622 static SDValue convertLocVTToValVT(SelectionDAG &DAG, SDLoc DL,
623                                    CCValAssign &VA, SDValue Chain,
624                                    SDValue Value) {
625   // If the argument has been promoted from a smaller type, insert an
626   // assertion to capture this.
627   if (VA.getLocInfo() == CCValAssign::SExt)
628     Value = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Value,
629                         DAG.getValueType(VA.getValVT()));
630   else if (VA.getLocInfo() == CCValAssign::ZExt)
631     Value = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Value,
632                         DAG.getValueType(VA.getValVT()));
633
634   if (VA.isExtInLoc())
635     Value = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Value);
636   else if (VA.getLocInfo() == CCValAssign::Indirect)
637     Value = DAG.getLoad(VA.getValVT(), DL, Chain, Value,
638                         MachinePointerInfo(), false, false, false, 0);
639   else
640     assert(VA.getLocInfo() == CCValAssign::Full && "Unsupported getLocInfo");
641   return Value;
642 }
643
644 // Value is a value of type VA.getValVT() that we need to copy into
645 // the location described by VA.  Return a copy of Value converted to
646 // VA.getValVT().  The caller is responsible for handling indirect values.
647 static SDValue convertValVTToLocVT(SelectionDAG &DAG, SDLoc DL,
648                                    CCValAssign &VA, SDValue Value) {
649   switch (VA.getLocInfo()) {
650   case CCValAssign::SExt:
651     return DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Value);
652   case CCValAssign::ZExt:
653     return DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Value);
654   case CCValAssign::AExt:
655     return DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Value);
656   case CCValAssign::Full:
657     return Value;
658   default:
659     llvm_unreachable("Unhandled getLocInfo()");
660   }
661 }
662
663 SDValue SystemZTargetLowering::
664 LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
665                      const SmallVectorImpl<ISD::InputArg> &Ins,
666                      SDLoc DL, SelectionDAG &DAG,
667                      SmallVectorImpl<SDValue> &InVals) const {
668   MachineFunction &MF = DAG.getMachineFunction();
669   MachineFrameInfo *MFI = MF.getFrameInfo();
670   MachineRegisterInfo &MRI = MF.getRegInfo();
671   SystemZMachineFunctionInfo *FuncInfo =
672     MF.getInfo<SystemZMachineFunctionInfo>();
673   auto *TFL = static_cast<const SystemZFrameLowering *>(TM.getFrameLowering());
674
675   // Assign locations to all of the incoming arguments.
676   SmallVector<CCValAssign, 16> ArgLocs;
677   CCState CCInfo(CallConv, IsVarArg, MF, TM, ArgLocs, *DAG.getContext());
678   CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
679
680   unsigned NumFixedGPRs = 0;
681   unsigned NumFixedFPRs = 0;
682   for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
683     SDValue ArgValue;
684     CCValAssign &VA = ArgLocs[I];
685     EVT LocVT = VA.getLocVT();
686     if (VA.isRegLoc()) {
687       // Arguments passed in registers
688       const TargetRegisterClass *RC;
689       switch (LocVT.getSimpleVT().SimpleTy) {
690       default:
691         // Integers smaller than i64 should be promoted to i64.
692         llvm_unreachable("Unexpected argument type");
693       case MVT::i32:
694         NumFixedGPRs += 1;
695         RC = &SystemZ::GR32BitRegClass;
696         break;
697       case MVT::i64:
698         NumFixedGPRs += 1;
699         RC = &SystemZ::GR64BitRegClass;
700         break;
701       case MVT::f32:
702         NumFixedFPRs += 1;
703         RC = &SystemZ::FP32BitRegClass;
704         break;
705       case MVT::f64:
706         NumFixedFPRs += 1;
707         RC = &SystemZ::FP64BitRegClass;
708         break;
709       }
710
711       unsigned VReg = MRI.createVirtualRegister(RC);
712       MRI.addLiveIn(VA.getLocReg(), VReg);
713       ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
714     } else {
715       assert(VA.isMemLoc() && "Argument not register or memory");
716
717       // Create the frame index object for this incoming parameter.
718       int FI = MFI->CreateFixedObject(LocVT.getSizeInBits() / 8,
719                                       VA.getLocMemOffset(), true);
720
721       // Create the SelectionDAG nodes corresponding to a load
722       // from this parameter.  Unpromoted ints and floats are
723       // passed as right-justified 8-byte values.
724       EVT PtrVT = getPointerTy();
725       SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
726       if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
727         FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN, DAG.getIntPtrConstant(4));
728       ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
729                              MachinePointerInfo::getFixedStack(FI),
730                              false, false, false, 0);
731     }
732
733     // Convert the value of the argument register into the value that's
734     // being passed.
735     InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, ArgValue));
736   }
737
738   if (IsVarArg) {
739     // Save the number of non-varargs registers for later use by va_start, etc.
740     FuncInfo->setVarArgsFirstGPR(NumFixedGPRs);
741     FuncInfo->setVarArgsFirstFPR(NumFixedFPRs);
742
743     // Likewise the address (in the form of a frame index) of where the
744     // first stack vararg would be.  The 1-byte size here is arbitrary.
745     int64_t StackSize = CCInfo.getNextStackOffset();
746     FuncInfo->setVarArgsFrameIndex(MFI->CreateFixedObject(1, StackSize, true));
747
748     // ...and a similar frame index for the caller-allocated save area
749     // that will be used to store the incoming registers.
750     int64_t RegSaveOffset = TFL->getOffsetOfLocalArea();
751     unsigned RegSaveIndex = MFI->CreateFixedObject(1, RegSaveOffset, true);
752     FuncInfo->setRegSaveFrameIndex(RegSaveIndex);
753
754     // Store the FPR varargs in the reserved frame slots.  (We store the
755     // GPRs as part of the prologue.)
756     if (NumFixedFPRs < SystemZ::NumArgFPRs) {
757       SDValue MemOps[SystemZ::NumArgFPRs];
758       for (unsigned I = NumFixedFPRs; I < SystemZ::NumArgFPRs; ++I) {
759         unsigned Offset = TFL->getRegSpillOffset(SystemZ::ArgFPRs[I]);
760         int FI = MFI->CreateFixedObject(8, RegSaveOffset + Offset, true);
761         SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
762         unsigned VReg = MF.addLiveIn(SystemZ::ArgFPRs[I],
763                                      &SystemZ::FP64BitRegClass);
764         SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f64);
765         MemOps[I] = DAG.getStore(ArgValue.getValue(1), DL, ArgValue, FIN,
766                                  MachinePointerInfo::getFixedStack(FI),
767                                  false, false, 0);
768
769       }
770       // Join the stores, which are independent of one another.
771       Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
772                           &MemOps[NumFixedFPRs],
773                           SystemZ::NumArgFPRs - NumFixedFPRs);
774     }
775   }
776
777   return Chain;
778 }
779
780 static bool canUseSiblingCall(CCState ArgCCInfo,
781                               SmallVectorImpl<CCValAssign> &ArgLocs) {
782   // Punt if there are any indirect or stack arguments, or if the call
783   // needs the call-saved argument register R6.
784   for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
785     CCValAssign &VA = ArgLocs[I];
786     if (VA.getLocInfo() == CCValAssign::Indirect)
787       return false;
788     if (!VA.isRegLoc())
789       return false;
790     unsigned Reg = VA.getLocReg();
791     if (Reg == SystemZ::R6H || Reg == SystemZ::R6L || Reg == SystemZ::R6D)
792       return false;
793   }
794   return true;
795 }
796
797 SDValue
798 SystemZTargetLowering::LowerCall(CallLoweringInfo &CLI,
799                                  SmallVectorImpl<SDValue> &InVals) const {
800   SelectionDAG &DAG = CLI.DAG;
801   SDLoc &DL = CLI.DL;
802   SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
803   SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
804   SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
805   SDValue Chain = CLI.Chain;
806   SDValue Callee = CLI.Callee;
807   bool &IsTailCall = CLI.IsTailCall;
808   CallingConv::ID CallConv = CLI.CallConv;
809   bool IsVarArg = CLI.IsVarArg;
810   MachineFunction &MF = DAG.getMachineFunction();
811   EVT PtrVT = getPointerTy();
812
813   // Analyze the operands of the call, assigning locations to each operand.
814   SmallVector<CCValAssign, 16> ArgLocs;
815   CCState ArgCCInfo(CallConv, IsVarArg, MF, TM, ArgLocs, *DAG.getContext());
816   ArgCCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
817
818   // We don't support GuaranteedTailCallOpt, only automatically-detected
819   // sibling calls.
820   if (IsTailCall && !canUseSiblingCall(ArgCCInfo, ArgLocs))
821     IsTailCall = false;
822
823   // Get a count of how many bytes are to be pushed on the stack.
824   unsigned NumBytes = ArgCCInfo.getNextStackOffset();
825
826   // Mark the start of the call.
827   if (!IsTailCall)
828     Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(NumBytes, PtrVT, true),
829                                  DL);
830
831   // Copy argument values to their designated locations.
832   SmallVector<std::pair<unsigned, SDValue>, 9> RegsToPass;
833   SmallVector<SDValue, 8> MemOpChains;
834   SDValue StackPtr;
835   for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
836     CCValAssign &VA = ArgLocs[I];
837     SDValue ArgValue = OutVals[I];
838
839     if (VA.getLocInfo() == CCValAssign::Indirect) {
840       // Store the argument in a stack slot and pass its address.
841       SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
842       int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
843       MemOpChains.push_back(DAG.getStore(Chain, DL, ArgValue, SpillSlot,
844                                          MachinePointerInfo::getFixedStack(FI),
845                                          false, false, 0));
846       ArgValue = SpillSlot;
847     } else
848       ArgValue = convertValVTToLocVT(DAG, DL, VA, ArgValue);
849
850     if (VA.isRegLoc())
851       // Queue up the argument copies and emit them at the end.
852       RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
853     else {
854       assert(VA.isMemLoc() && "Argument not register or memory");
855
856       // Work out the address of the stack slot.  Unpromoted ints and
857       // floats are passed as right-justified 8-byte values.
858       if (!StackPtr.getNode())
859         StackPtr = DAG.getCopyFromReg(Chain, DL, SystemZ::R15D, PtrVT);
860       unsigned Offset = SystemZMC::CallFrameSize + VA.getLocMemOffset();
861       if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
862         Offset += 4;
863       SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
864                                     DAG.getIntPtrConstant(Offset));
865
866       // Emit the store.
867       MemOpChains.push_back(DAG.getStore(Chain, DL, ArgValue, Address,
868                                          MachinePointerInfo(),
869                                          false, false, 0));
870     }
871   }
872
873   // Join the stores, which are independent of one another.
874   if (!MemOpChains.empty())
875     Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
876                         &MemOpChains[0], MemOpChains.size());
877
878   // Accept direct calls by converting symbolic call addresses to the
879   // associated Target* opcodes.  Force %r1 to be used for indirect
880   // tail calls.
881   SDValue Glue;
882   if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
883     Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT);
884     Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
885   } else if (auto *E = dyn_cast<ExternalSymbolSDNode>(Callee)) {
886     Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT);
887     Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
888   } else if (IsTailCall) {
889     Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R1D, Callee, Glue);
890     Glue = Chain.getValue(1);
891     Callee = DAG.getRegister(SystemZ::R1D, Callee.getValueType());
892   }
893
894   // Build a sequence of copy-to-reg nodes, chained and glued together.
895   for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) {
896     Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[I].first,
897                              RegsToPass[I].second, Glue);
898     Glue = Chain.getValue(1);
899   }
900
901   // The first call operand is the chain and the second is the target address.
902   SmallVector<SDValue, 8> Ops;
903   Ops.push_back(Chain);
904   Ops.push_back(Callee);
905
906   // Add argument registers to the end of the list so that they are
907   // known live into the call.
908   for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I)
909     Ops.push_back(DAG.getRegister(RegsToPass[I].first,
910                                   RegsToPass[I].second.getValueType()));
911
912   // Glue the call to the argument copies, if any.
913   if (Glue.getNode())
914     Ops.push_back(Glue);
915
916   // Emit the call.
917   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
918   if (IsTailCall)
919     return DAG.getNode(SystemZISD::SIBCALL, DL, NodeTys, &Ops[0], Ops.size());
920   Chain = DAG.getNode(SystemZISD::CALL, DL, NodeTys, &Ops[0], Ops.size());
921   Glue = Chain.getValue(1);
922
923   // Mark the end of the call, which is glued to the call itself.
924   Chain = DAG.getCALLSEQ_END(Chain,
925                              DAG.getConstant(NumBytes, PtrVT, true),
926                              DAG.getConstant(0, PtrVT, true),
927                              Glue, DL);
928   Glue = Chain.getValue(1);
929
930   // Assign locations to each value returned by this call.
931   SmallVector<CCValAssign, 16> RetLocs;
932   CCState RetCCInfo(CallConv, IsVarArg, MF, TM, RetLocs, *DAG.getContext());
933   RetCCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
934
935   // Copy all of the result registers out of their specified physreg.
936   for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
937     CCValAssign &VA = RetLocs[I];
938
939     // Copy the value out, gluing the copy to the end of the call sequence.
940     SDValue RetValue = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(),
941                                           VA.getLocVT(), Glue);
942     Chain = RetValue.getValue(1);
943     Glue = RetValue.getValue(2);
944
945     // Convert the value of the return register into the value that's
946     // being returned.
947     InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, RetValue));
948   }
949
950   return Chain;
951 }
952
953 SDValue
954 SystemZTargetLowering::LowerReturn(SDValue Chain,
955                                    CallingConv::ID CallConv, bool IsVarArg,
956                                    const SmallVectorImpl<ISD::OutputArg> &Outs,
957                                    const SmallVectorImpl<SDValue> &OutVals,
958                                    SDLoc DL, SelectionDAG &DAG) const {
959   MachineFunction &MF = DAG.getMachineFunction();
960
961   // Assign locations to each returned value.
962   SmallVector<CCValAssign, 16> RetLocs;
963   CCState RetCCInfo(CallConv, IsVarArg, MF, TM, RetLocs, *DAG.getContext());
964   RetCCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
965
966   // Quick exit for void returns
967   if (RetLocs.empty())
968     return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, Chain);
969
970   // Copy the result values into the output registers.
971   SDValue Glue;
972   SmallVector<SDValue, 4> RetOps;
973   RetOps.push_back(Chain);
974   for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
975     CCValAssign &VA = RetLocs[I];
976     SDValue RetValue = OutVals[I];
977
978     // Make the return register live on exit.
979     assert(VA.isRegLoc() && "Can only return in registers!");
980
981     // Promote the value as required.
982     RetValue = convertValVTToLocVT(DAG, DL, VA, RetValue);
983
984     // Chain and glue the copies together.
985     unsigned Reg = VA.getLocReg();
986     Chain = DAG.getCopyToReg(Chain, DL, Reg, RetValue, Glue);
987     Glue = Chain.getValue(1);
988     RetOps.push_back(DAG.getRegister(Reg, VA.getLocVT()));
989   }
990
991   // Update chain and glue.
992   RetOps[0] = Chain;
993   if (Glue.getNode())
994     RetOps.push_back(Glue);
995
996   return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other,
997                      RetOps.data(), RetOps.size());
998 }
999
1000 SDValue SystemZTargetLowering::
1001 prepareVolatileOrAtomicLoad(SDValue Chain, SDLoc DL, SelectionDAG &DAG) const {
1002   return DAG.getNode(SystemZISD::SERIALIZE, DL, MVT::Other, Chain);
1003 }
1004
1005 // CC is a comparison that will be implemented using an integer or
1006 // floating-point comparison.  Return the condition code mask for
1007 // a branch on true.  In the integer case, CCMASK_CMP_UO is set for
1008 // unsigned comparisons and clear for signed ones.  In the floating-point
1009 // case, CCMASK_CMP_UO has its normal mask meaning (unordered).
1010 static unsigned CCMaskForCondCode(ISD::CondCode CC) {
1011 #define CONV(X) \
1012   case ISD::SET##X: return SystemZ::CCMASK_CMP_##X; \
1013   case ISD::SETO##X: return SystemZ::CCMASK_CMP_##X; \
1014   case ISD::SETU##X: return SystemZ::CCMASK_CMP_UO | SystemZ::CCMASK_CMP_##X
1015
1016   switch (CC) {
1017   default:
1018     llvm_unreachable("Invalid integer condition!");
1019
1020   CONV(EQ);
1021   CONV(NE);
1022   CONV(GT);
1023   CONV(GE);
1024   CONV(LT);
1025   CONV(LE);
1026
1027   case ISD::SETO:  return SystemZ::CCMASK_CMP_O;
1028   case ISD::SETUO: return SystemZ::CCMASK_CMP_UO;
1029   }
1030 #undef CONV
1031 }
1032
1033 // Return a sequence for getting a 1 from an IPM result when CC has a
1034 // value in CCMask and a 0 when CC has a value in CCValid & ~CCMask.
1035 // The handling of CC values outside CCValid doesn't matter.
1036 static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) {
1037   // Deal with cases where the result can be taken directly from a bit
1038   // of the IPM result.
1039   if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3)))
1040     return IPMConversion(0, 0, SystemZ::IPM_CC);
1041   if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3)))
1042     return IPMConversion(0, 0, SystemZ::IPM_CC + 1);
1043
1044   // Deal with cases where we can add a value to force the sign bit
1045   // to contain the right value.  Putting the bit in 31 means we can
1046   // use SRL rather than RISBG(L), and also makes it easier to get a
1047   // 0/-1 value, so it has priority over the other tests below.
1048   //
1049   // These sequences rely on the fact that the upper two bits of the
1050   // IPM result are zero.
1051   uint64_t TopBit = uint64_t(1) << 31;
1052   if (CCMask == (CCValid & SystemZ::CCMASK_0))
1053     return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31);
1054   if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1)))
1055     return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31);
1056   if (CCMask == (CCValid & (SystemZ::CCMASK_0
1057                             | SystemZ::CCMASK_1
1058                             | SystemZ::CCMASK_2)))
1059     return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31);
1060   if (CCMask == (CCValid & SystemZ::CCMASK_3))
1061     return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31);
1062   if (CCMask == (CCValid & (SystemZ::CCMASK_1
1063                             | SystemZ::CCMASK_2
1064                             | SystemZ::CCMASK_3)))
1065     return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31);
1066
1067   // Next try inverting the value and testing a bit.  0/1 could be
1068   // handled this way too, but we dealt with that case above.
1069   if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2)))
1070     return IPMConversion(-1, 0, SystemZ::IPM_CC);
1071
1072   // Handle cases where adding a value forces a non-sign bit to contain
1073   // the right value.
1074   if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2)))
1075     return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1);
1076   if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3)))
1077     return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1);
1078
1079   // The remaining cases are 1, 2, 0/1/3 and 0/2/3.  All these are
1080   // can be done by inverting the low CC bit and applying one of the
1081   // sign-based extractions above.
1082   if (CCMask == (CCValid & SystemZ::CCMASK_1))
1083     return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31);
1084   if (CCMask == (CCValid & SystemZ::CCMASK_2))
1085     return IPMConversion(1 << SystemZ::IPM_CC,
1086                          TopBit - (3 << SystemZ::IPM_CC), 31);
1087   if (CCMask == (CCValid & (SystemZ::CCMASK_0
1088                             | SystemZ::CCMASK_1
1089                             | SystemZ::CCMASK_3)))
1090     return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31);
1091   if (CCMask == (CCValid & (SystemZ::CCMASK_0
1092                             | SystemZ::CCMASK_2
1093                             | SystemZ::CCMASK_3)))
1094     return IPMConversion(1 << SystemZ::IPM_CC,
1095                          TopBit - (1 << SystemZ::IPM_CC), 31);
1096
1097   llvm_unreachable("Unexpected CC combination");
1098 }
1099
1100 // If C can be converted to a comparison against zero, adjust the operands
1101 // as necessary.
1102 static void adjustZeroCmp(SelectionDAG &DAG, Comparison &C) {
1103   if (C.ICmpType == SystemZICMP::UnsignedOnly)
1104     return;
1105
1106   auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1.getNode());
1107   if (!ConstOp1)
1108     return;
1109
1110   int64_t Value = ConstOp1->getSExtValue();
1111   if ((Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_GT) ||
1112       (Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_LE) ||
1113       (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_LT) ||
1114       (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_GE)) {
1115     C.CCMask ^= SystemZ::CCMASK_CMP_EQ;
1116     C.Op1 = DAG.getConstant(0, C.Op1.getValueType());
1117   }
1118 }
1119
1120 // If a comparison described by C is suitable for CLI(Y), CHHSI or CLHHSI,
1121 // adjust the operands as necessary.
1122 static void adjustSubwordCmp(SelectionDAG &DAG, Comparison &C) {
1123   // For us to make any changes, it must a comparison between a single-use
1124   // load and a constant.
1125   if (!C.Op0.hasOneUse() ||
1126       C.Op0.getOpcode() != ISD::LOAD ||
1127       C.Op1.getOpcode() != ISD::Constant)
1128     return;
1129
1130   // We must have an 8- or 16-bit load.
1131   auto *Load = cast<LoadSDNode>(C.Op0);
1132   unsigned NumBits = Load->getMemoryVT().getStoreSizeInBits();
1133   if (NumBits != 8 && NumBits != 16)
1134     return;
1135
1136   // The load must be an extending one and the constant must be within the
1137   // range of the unextended value.
1138   auto *ConstOp1 = cast<ConstantSDNode>(C.Op1);
1139   uint64_t Value = ConstOp1->getZExtValue();
1140   uint64_t Mask = (1 << NumBits) - 1;
1141   if (Load->getExtensionType() == ISD::SEXTLOAD) {
1142     // Make sure that ConstOp1 is in range of C.Op0.
1143     int64_t SignedValue = ConstOp1->getSExtValue();
1144     if (uint64_t(SignedValue) + (uint64_t(1) << (NumBits - 1)) > Mask)
1145       return;
1146     if (C.ICmpType != SystemZICMP::SignedOnly) {
1147       // Unsigned comparison between two sign-extended values is equivalent
1148       // to unsigned comparison between two zero-extended values.
1149       Value &= Mask;
1150     } else if (NumBits == 8) {
1151       // Try to treat the comparison as unsigned, so that we can use CLI.
1152       // Adjust CCMask and Value as necessary.
1153       if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_LT)
1154         // Test whether the high bit of the byte is set.
1155         Value = 127, C.CCMask = SystemZ::CCMASK_CMP_GT;
1156       else if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_GE)
1157         // Test whether the high bit of the byte is clear.
1158         Value = 128, C.CCMask = SystemZ::CCMASK_CMP_LT;
1159       else
1160         // No instruction exists for this combination.
1161         return;
1162       C.ICmpType = SystemZICMP::UnsignedOnly;
1163     }
1164   } else if (Load->getExtensionType() == ISD::ZEXTLOAD) {
1165     if (Value > Mask)
1166       return;
1167     assert(C.ICmpType == SystemZICMP::Any &&
1168            "Signedness shouldn't matter here.");
1169   } else
1170     return;
1171
1172   // Make sure that the first operand is an i32 of the right extension type.
1173   ISD::LoadExtType ExtType = (C.ICmpType == SystemZICMP::SignedOnly ?
1174                               ISD::SEXTLOAD :
1175                               ISD::ZEXTLOAD);
1176   if (C.Op0.getValueType() != MVT::i32 ||
1177       Load->getExtensionType() != ExtType)
1178     C.Op0 = DAG.getExtLoad(ExtType, SDLoc(Load), MVT::i32,
1179                            Load->getChain(), Load->getBasePtr(),
1180                            Load->getPointerInfo(), Load->getMemoryVT(),
1181                            Load->isVolatile(), Load->isNonTemporal(),
1182                            Load->getAlignment());
1183
1184   // Make sure that the second operand is an i32 with the right value.
1185   if (C.Op1.getValueType() != MVT::i32 ||
1186       Value != ConstOp1->getZExtValue())
1187     C.Op1 = DAG.getConstant(Value, MVT::i32);
1188 }
1189
1190 // Return true if Op is either an unextended load, or a load suitable
1191 // for integer register-memory comparisons of type ICmpType.
1192 static bool isNaturalMemoryOperand(SDValue Op, unsigned ICmpType) {
1193   auto *Load = dyn_cast<LoadSDNode>(Op.getNode());
1194   if (Load) {
1195     // There are no instructions to compare a register with a memory byte.
1196     if (Load->getMemoryVT() == MVT::i8)
1197       return false;
1198     // Otherwise decide on extension type.
1199     switch (Load->getExtensionType()) {
1200     case ISD::NON_EXTLOAD:
1201       return true;
1202     case ISD::SEXTLOAD:
1203       return ICmpType != SystemZICMP::UnsignedOnly;
1204     case ISD::ZEXTLOAD:
1205       return ICmpType != SystemZICMP::SignedOnly;
1206     default:
1207       break;
1208     }
1209   }
1210   return false;
1211 }
1212
1213 // Return true if it is better to swap the operands of C.
1214 static bool shouldSwapCmpOperands(const Comparison &C) {
1215   // Leave f128 comparisons alone, since they have no memory forms.
1216   if (C.Op0.getValueType() == MVT::f128)
1217     return false;
1218
1219   // Always keep a floating-point constant second, since comparisons with
1220   // zero can use LOAD TEST and comparisons with other constants make a
1221   // natural memory operand.
1222   if (isa<ConstantFPSDNode>(C.Op1))
1223     return false;
1224
1225   // Never swap comparisons with zero since there are many ways to optimize
1226   // those later.
1227   auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
1228   if (ConstOp1 && ConstOp1->getZExtValue() == 0)
1229     return false;
1230
1231   // Also keep natural memory operands second if the loaded value is
1232   // only used here.  Several comparisons have memory forms.
1233   if (isNaturalMemoryOperand(C.Op1, C.ICmpType) && C.Op1.hasOneUse())
1234     return false;
1235
1236   // Look for cases where Cmp0 is a single-use load and Cmp1 isn't.
1237   // In that case we generally prefer the memory to be second.
1238   if (isNaturalMemoryOperand(C.Op0, C.ICmpType) && C.Op0.hasOneUse()) {
1239     // The only exceptions are when the second operand is a constant and
1240     // we can use things like CHHSI.
1241     if (!ConstOp1)
1242       return true;
1243     // The unsigned memory-immediate instructions can handle 16-bit
1244     // unsigned integers.
1245     if (C.ICmpType != SystemZICMP::SignedOnly &&
1246         isUInt<16>(ConstOp1->getZExtValue()))
1247       return false;
1248     // The signed memory-immediate instructions can handle 16-bit
1249     // signed integers.
1250     if (C.ICmpType != SystemZICMP::UnsignedOnly &&
1251         isInt<16>(ConstOp1->getSExtValue()))
1252       return false;
1253     return true;
1254   }
1255
1256   // Try to promote the use of CGFR and CLGFR.
1257   unsigned Opcode0 = C.Op0.getOpcode();
1258   if (C.ICmpType != SystemZICMP::UnsignedOnly && Opcode0 == ISD::SIGN_EXTEND)
1259     return true;
1260   if (C.ICmpType != SystemZICMP::SignedOnly && Opcode0 == ISD::ZERO_EXTEND)
1261     return true;
1262   if (C.ICmpType != SystemZICMP::SignedOnly &&
1263       Opcode0 == ISD::AND &&
1264       C.Op0.getOperand(1).getOpcode() == ISD::Constant &&
1265       cast<ConstantSDNode>(C.Op0.getOperand(1))->getZExtValue() == 0xffffffff)
1266     return true;
1267
1268   return false;
1269 }
1270
1271 // Return a version of comparison CC mask CCMask in which the LT and GT
1272 // actions are swapped.
1273 static unsigned reverseCCMask(unsigned CCMask) {
1274   return ((CCMask & SystemZ::CCMASK_CMP_EQ) |
1275           (CCMask & SystemZ::CCMASK_CMP_GT ? SystemZ::CCMASK_CMP_LT : 0) |
1276           (CCMask & SystemZ::CCMASK_CMP_LT ? SystemZ::CCMASK_CMP_GT : 0) |
1277           (CCMask & SystemZ::CCMASK_CMP_UO));
1278 }
1279
1280 // Check whether C tests for equality between X and Y and whether X - Y
1281 // or Y - X is also computed.  In that case it's better to compare the
1282 // result of the subtraction against zero.
1283 static void adjustForSubtraction(SelectionDAG &DAG, Comparison &C) {
1284   if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
1285       C.CCMask == SystemZ::CCMASK_CMP_NE) {
1286     for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
1287       SDNode *N = *I;
1288       if (N->getOpcode() == ISD::SUB &&
1289           ((N->getOperand(0) == C.Op0 && N->getOperand(1) == C.Op1) ||
1290            (N->getOperand(0) == C.Op1 && N->getOperand(1) == C.Op0))) {
1291         C.Op0 = SDValue(N, 0);
1292         C.Op1 = DAG.getConstant(0, N->getValueType(0));
1293         return;
1294       }
1295     }
1296   }
1297 }
1298
1299 // Check whether C compares a floating-point value with zero and if that
1300 // floating-point value is also negated.  In this case we can use the
1301 // negation to set CC, so avoiding separate LOAD AND TEST and
1302 // LOAD (NEGATIVE/COMPLEMENT) instructions.
1303 static void adjustForFNeg(Comparison &C) {
1304   auto *C1 = dyn_cast<ConstantFPSDNode>(C.Op1);
1305   if (C1 && C1->isZero()) {
1306     for (auto I = C.Op0->use_begin(), E = C.Op0->use_end(); I != E; ++I) {
1307       SDNode *N = *I;
1308       if (N->getOpcode() == ISD::FNEG) {
1309         C.Op0 = SDValue(N, 0);
1310         C.CCMask = reverseCCMask(C.CCMask);
1311         return;
1312       }
1313     }
1314   }
1315 }
1316
1317 // Check whether C compares (shl X, 32) with 0 and whether X is
1318 // also sign-extended.  In that case it is better to test the result
1319 // of the sign extension using LTGFR.
1320 //
1321 // This case is important because InstCombine transforms a comparison
1322 // with (sext (trunc X)) into a comparison with (shl X, 32).
1323 static void adjustForLTGFR(Comparison &C) {
1324   // Check for a comparison between (shl X, 32) and 0.
1325   if (C.Op0.getOpcode() == ISD::SHL &&
1326       C.Op0.getValueType() == MVT::i64 &&
1327       C.Op1.getOpcode() == ISD::Constant &&
1328       cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
1329     auto *C1 = dyn_cast<ConstantSDNode>(C.Op0.getOperand(1));
1330     if (C1 && C1->getZExtValue() == 32) {
1331       SDValue ShlOp0 = C.Op0.getOperand(0);
1332       // See whether X has any SIGN_EXTEND_INREG uses.
1333       for (auto I = ShlOp0->use_begin(), E = ShlOp0->use_end(); I != E; ++I) {
1334         SDNode *N = *I;
1335         if (N->getOpcode() == ISD::SIGN_EXTEND_INREG &&
1336             cast<VTSDNode>(N->getOperand(1))->getVT() == MVT::i32) {
1337           C.Op0 = SDValue(N, 0);
1338           return;
1339         }
1340       }
1341     }
1342   }
1343 }
1344
1345 // If C compares the truncation of an extending load, try to compare
1346 // the untruncated value instead.  This exposes more opportunities to
1347 // reuse CC.
1348 static void adjustICmpTruncate(SelectionDAG &DAG, Comparison &C) {
1349   if (C.Op0.getOpcode() == ISD::TRUNCATE &&
1350       C.Op0.getOperand(0).getOpcode() == ISD::LOAD &&
1351       C.Op1.getOpcode() == ISD::Constant &&
1352       cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
1353     auto *L = cast<LoadSDNode>(C.Op0.getOperand(0));
1354     if (L->getMemoryVT().getStoreSizeInBits()
1355         <= C.Op0.getValueType().getSizeInBits()) {
1356       unsigned Type = L->getExtensionType();
1357       if ((Type == ISD::ZEXTLOAD && C.ICmpType != SystemZICMP::SignedOnly) ||
1358           (Type == ISD::SEXTLOAD && C.ICmpType != SystemZICMP::UnsignedOnly)) {
1359         C.Op0 = C.Op0.getOperand(0);
1360         C.Op1 = DAG.getConstant(0, C.Op0.getValueType());
1361       }
1362     }
1363   }
1364 }
1365
1366 // Return true if shift operation N has an in-range constant shift value.
1367 // Store it in ShiftVal if so.
1368 static bool isSimpleShift(SDValue N, unsigned &ShiftVal) {
1369   auto *Shift = dyn_cast<ConstantSDNode>(N.getOperand(1));
1370   if (!Shift)
1371     return false;
1372
1373   uint64_t Amount = Shift->getZExtValue();
1374   if (Amount >= N.getValueType().getSizeInBits())
1375     return false;
1376
1377   ShiftVal = Amount;
1378   return true;
1379 }
1380
1381 // Check whether an AND with Mask is suitable for a TEST UNDER MASK
1382 // instruction and whether the CC value is descriptive enough to handle
1383 // a comparison of type Opcode between the AND result and CmpVal.
1384 // CCMask says which comparison result is being tested and BitSize is
1385 // the number of bits in the operands.  If TEST UNDER MASK can be used,
1386 // return the corresponding CC mask, otherwise return 0.
1387 static unsigned getTestUnderMaskCond(unsigned BitSize, unsigned CCMask,
1388                                      uint64_t Mask, uint64_t CmpVal,
1389                                      unsigned ICmpType) {
1390   assert(Mask != 0 && "ANDs with zero should have been removed by now");
1391
1392   // Check whether the mask is suitable for TMHH, TMHL, TMLH or TMLL.
1393   if (!SystemZ::isImmLL(Mask) && !SystemZ::isImmLH(Mask) &&
1394       !SystemZ::isImmHL(Mask) && !SystemZ::isImmHH(Mask))
1395     return 0;
1396
1397   // Work out the masks for the lowest and highest bits.
1398   unsigned HighShift = 63 - countLeadingZeros(Mask);
1399   uint64_t High = uint64_t(1) << HighShift;
1400   uint64_t Low = uint64_t(1) << countTrailingZeros(Mask);
1401
1402   // Signed ordered comparisons are effectively unsigned if the sign
1403   // bit is dropped.
1404   bool EffectivelyUnsigned = (ICmpType != SystemZICMP::SignedOnly);
1405
1406   // Check for equality comparisons with 0, or the equivalent.
1407   if (CmpVal == 0) {
1408     if (CCMask == SystemZ::CCMASK_CMP_EQ)
1409       return SystemZ::CCMASK_TM_ALL_0;
1410     if (CCMask == SystemZ::CCMASK_CMP_NE)
1411       return SystemZ::CCMASK_TM_SOME_1;
1412   }
1413   if (EffectivelyUnsigned && CmpVal <= Low) {
1414     if (CCMask == SystemZ::CCMASK_CMP_LT)
1415       return SystemZ::CCMASK_TM_ALL_0;
1416     if (CCMask == SystemZ::CCMASK_CMP_GE)
1417       return SystemZ::CCMASK_TM_SOME_1;
1418   }
1419   if (EffectivelyUnsigned && CmpVal < Low) {
1420     if (CCMask == SystemZ::CCMASK_CMP_LE)
1421       return SystemZ::CCMASK_TM_ALL_0;
1422     if (CCMask == SystemZ::CCMASK_CMP_GT)
1423       return SystemZ::CCMASK_TM_SOME_1;
1424   }
1425
1426   // Check for equality comparisons with the mask, or the equivalent.
1427   if (CmpVal == Mask) {
1428     if (CCMask == SystemZ::CCMASK_CMP_EQ)
1429       return SystemZ::CCMASK_TM_ALL_1;
1430     if (CCMask == SystemZ::CCMASK_CMP_NE)
1431       return SystemZ::CCMASK_TM_SOME_0;
1432   }
1433   if (EffectivelyUnsigned && CmpVal >= Mask - Low && CmpVal < Mask) {
1434     if (CCMask == SystemZ::CCMASK_CMP_GT)
1435       return SystemZ::CCMASK_TM_ALL_1;
1436     if (CCMask == SystemZ::CCMASK_CMP_LE)
1437       return SystemZ::CCMASK_TM_SOME_0;
1438   }
1439   if (EffectivelyUnsigned && CmpVal > Mask - Low && CmpVal <= Mask) {
1440     if (CCMask == SystemZ::CCMASK_CMP_GE)
1441       return SystemZ::CCMASK_TM_ALL_1;
1442     if (CCMask == SystemZ::CCMASK_CMP_LT)
1443       return SystemZ::CCMASK_TM_SOME_0;
1444   }
1445
1446   // Check for ordered comparisons with the top bit.
1447   if (EffectivelyUnsigned && CmpVal >= Mask - High && CmpVal < High) {
1448     if (CCMask == SystemZ::CCMASK_CMP_LE)
1449       return SystemZ::CCMASK_TM_MSB_0;
1450     if (CCMask == SystemZ::CCMASK_CMP_GT)
1451       return SystemZ::CCMASK_TM_MSB_1;
1452   }
1453   if (EffectivelyUnsigned && CmpVal > Mask - High && CmpVal <= High) {
1454     if (CCMask == SystemZ::CCMASK_CMP_LT)
1455       return SystemZ::CCMASK_TM_MSB_0;
1456     if (CCMask == SystemZ::CCMASK_CMP_GE)
1457       return SystemZ::CCMASK_TM_MSB_1;
1458   }
1459
1460   // If there are just two bits, we can do equality checks for Low and High
1461   // as well.
1462   if (Mask == Low + High) {
1463     if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == Low)
1464       return SystemZ::CCMASK_TM_MIXED_MSB_0;
1465     if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == Low)
1466       return SystemZ::CCMASK_TM_MIXED_MSB_0 ^ SystemZ::CCMASK_ANY;
1467     if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == High)
1468       return SystemZ::CCMASK_TM_MIXED_MSB_1;
1469     if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == High)
1470       return SystemZ::CCMASK_TM_MIXED_MSB_1 ^ SystemZ::CCMASK_ANY;
1471   }
1472
1473   // Looks like we've exhausted our options.
1474   return 0;
1475 }
1476
1477 // See whether C can be implemented as a TEST UNDER MASK instruction.
1478 // Update the arguments with the TM version if so.
1479 static void adjustForTestUnderMask(SelectionDAG &DAG, Comparison &C) {
1480   // Check that we have a comparison with a constant.
1481   auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
1482   if (!ConstOp1)
1483     return;
1484   uint64_t CmpVal = ConstOp1->getZExtValue();
1485
1486   // Check whether the nonconstant input is an AND with a constant mask.
1487   Comparison NewC(C);
1488   uint64_t MaskVal;
1489   ConstantSDNode *Mask = 0;
1490   if (C.Op0.getOpcode() == ISD::AND) {
1491     NewC.Op0 = C.Op0.getOperand(0);
1492     NewC.Op1 = C.Op0.getOperand(1);
1493     Mask = dyn_cast<ConstantSDNode>(NewC.Op1);
1494     if (!Mask)
1495       return;
1496     MaskVal = Mask->getZExtValue();
1497   } else {
1498     // There is no instruction to compare with a 64-bit immediate
1499     // so use TMHH instead if possible.  We need an unsigned ordered
1500     // comparison with an i64 immediate.
1501     if (NewC.Op0.getValueType() != MVT::i64 ||
1502         NewC.CCMask == SystemZ::CCMASK_CMP_EQ ||
1503         NewC.CCMask == SystemZ::CCMASK_CMP_NE ||
1504         NewC.ICmpType == SystemZICMP::SignedOnly)
1505       return;
1506     // Convert LE and GT comparisons into LT and GE.
1507     if (NewC.CCMask == SystemZ::CCMASK_CMP_LE ||
1508         NewC.CCMask == SystemZ::CCMASK_CMP_GT) {
1509       if (CmpVal == uint64_t(-1))
1510         return;
1511       CmpVal += 1;
1512       NewC.CCMask ^= SystemZ::CCMASK_CMP_EQ;
1513     }
1514     // If the low N bits of Op1 are zero than the low N bits of Op0 can
1515     // be masked off without changing the result.
1516     MaskVal = -(CmpVal & -CmpVal);
1517     NewC.ICmpType = SystemZICMP::UnsignedOnly;
1518   }
1519
1520   // Check whether the combination of mask, comparison value and comparison
1521   // type are suitable.
1522   unsigned BitSize = NewC.Op0.getValueType().getSizeInBits();
1523   unsigned NewCCMask, ShiftVal;
1524   if (NewC.ICmpType != SystemZICMP::SignedOnly &&
1525       NewC.Op0.getOpcode() == ISD::SHL &&
1526       isSimpleShift(NewC.Op0, ShiftVal) &&
1527       (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
1528                                         MaskVal >> ShiftVal,
1529                                         CmpVal >> ShiftVal,
1530                                         SystemZICMP::Any))) {
1531     NewC.Op0 = NewC.Op0.getOperand(0);
1532     MaskVal >>= ShiftVal;
1533   } else if (NewC.ICmpType != SystemZICMP::SignedOnly &&
1534              NewC.Op0.getOpcode() == ISD::SRL &&
1535              isSimpleShift(NewC.Op0, ShiftVal) &&
1536              (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
1537                                                MaskVal << ShiftVal,
1538                                                CmpVal << ShiftVal,
1539                                                SystemZICMP::UnsignedOnly))) {
1540     NewC.Op0 = NewC.Op0.getOperand(0);
1541     MaskVal <<= ShiftVal;
1542   } else {
1543     NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, MaskVal, CmpVal,
1544                                      NewC.ICmpType);
1545     if (!NewCCMask)
1546       return;
1547   }
1548
1549   // Go ahead and make the change.
1550   C.Opcode = SystemZISD::TM;
1551   C.Op0 = NewC.Op0;
1552   if (Mask && Mask->getZExtValue() == MaskVal)
1553     C.Op1 = SDValue(Mask, 0);
1554   else
1555     C.Op1 = DAG.getConstant(MaskVal, C.Op0.getValueType());
1556   C.CCValid = SystemZ::CCMASK_TM;
1557   C.CCMask = NewCCMask;
1558 }
1559
1560 // Decide how to implement a comparison of type Cond between CmpOp0 with CmpOp1.
1561 static Comparison getCmp(SelectionDAG &DAG, SDValue CmpOp0, SDValue CmpOp1,
1562                          ISD::CondCode Cond) {
1563   Comparison C(CmpOp0, CmpOp1);
1564   C.CCMask = CCMaskForCondCode(Cond);
1565   if (C.Op0.getValueType().isFloatingPoint()) {
1566     C.CCValid = SystemZ::CCMASK_FCMP;
1567     C.Opcode = SystemZISD::FCMP;
1568     adjustForFNeg(C);
1569   } else {
1570     C.CCValid = SystemZ::CCMASK_ICMP;
1571     C.Opcode = SystemZISD::ICMP;
1572     // Choose the type of comparison.  Equality and inequality tests can
1573     // use either signed or unsigned comparisons.  The choice also doesn't
1574     // matter if both sign bits are known to be clear.  In those cases we
1575     // want to give the main isel code the freedom to choose whichever
1576     // form fits best.
1577     if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
1578         C.CCMask == SystemZ::CCMASK_CMP_NE ||
1579         (DAG.SignBitIsZero(C.Op0) && DAG.SignBitIsZero(C.Op1)))
1580       C.ICmpType = SystemZICMP::Any;
1581     else if (C.CCMask & SystemZ::CCMASK_CMP_UO)
1582       C.ICmpType = SystemZICMP::UnsignedOnly;
1583     else
1584       C.ICmpType = SystemZICMP::SignedOnly;
1585     C.CCMask &= ~SystemZ::CCMASK_CMP_UO;
1586     adjustZeroCmp(DAG, C);
1587     adjustSubwordCmp(DAG, C);
1588     adjustForSubtraction(DAG, C);
1589     adjustForLTGFR(C);
1590     adjustICmpTruncate(DAG, C);
1591   }
1592
1593   if (shouldSwapCmpOperands(C)) {
1594     std::swap(C.Op0, C.Op1);
1595     C.CCMask = reverseCCMask(C.CCMask);
1596   }
1597
1598   adjustForTestUnderMask(DAG, C);
1599   return C;
1600 }
1601
1602 // Emit the comparison instruction described by C.
1603 static SDValue emitCmp(SelectionDAG &DAG, SDLoc DL, Comparison &C) {
1604   if (C.Opcode == SystemZISD::ICMP)
1605     return DAG.getNode(SystemZISD::ICMP, DL, MVT::Glue, C.Op0, C.Op1,
1606                        DAG.getConstant(C.ICmpType, MVT::i32));
1607   if (C.Opcode == SystemZISD::TM) {
1608     bool RegisterOnly = (bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_0) !=
1609                          bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_1));
1610     return DAG.getNode(SystemZISD::TM, DL, MVT::Glue, C.Op0, C.Op1,
1611                        DAG.getConstant(RegisterOnly, MVT::i32));
1612   }
1613   return DAG.getNode(C.Opcode, DL, MVT::Glue, C.Op0, C.Op1);
1614 }
1615
1616 // Implement a 32-bit *MUL_LOHI operation by extending both operands to
1617 // 64 bits.  Extend is the extension type to use.  Store the high part
1618 // in Hi and the low part in Lo.
1619 static void lowerMUL_LOHI32(SelectionDAG &DAG, SDLoc DL,
1620                             unsigned Extend, SDValue Op0, SDValue Op1,
1621                             SDValue &Hi, SDValue &Lo) {
1622   Op0 = DAG.getNode(Extend, DL, MVT::i64, Op0);
1623   Op1 = DAG.getNode(Extend, DL, MVT::i64, Op1);
1624   SDValue Mul = DAG.getNode(ISD::MUL, DL, MVT::i64, Op0, Op1);
1625   Hi = DAG.getNode(ISD::SRL, DL, MVT::i64, Mul, DAG.getConstant(32, MVT::i64));
1626   Hi = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Hi);
1627   Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mul);
1628 }
1629
1630 // Lower a binary operation that produces two VT results, one in each
1631 // half of a GR128 pair.  Op0 and Op1 are the VT operands to the operation,
1632 // Extend extends Op0 to a GR128, and Opcode performs the GR128 operation
1633 // on the extended Op0 and (unextended) Op1.  Store the even register result
1634 // in Even and the odd register result in Odd.
1635 static void lowerGR128Binary(SelectionDAG &DAG, SDLoc DL, EVT VT,
1636                              unsigned Extend, unsigned Opcode,
1637                              SDValue Op0, SDValue Op1,
1638                              SDValue &Even, SDValue &Odd) {
1639   SDNode *In128 = DAG.getMachineNode(Extend, DL, MVT::Untyped, Op0);
1640   SDValue Result = DAG.getNode(Opcode, DL, MVT::Untyped,
1641                                SDValue(In128, 0), Op1);
1642   bool Is32Bit = is32Bit(VT);
1643   Even = DAG.getTargetExtractSubreg(SystemZ::even128(Is32Bit), DL, VT, Result);
1644   Odd = DAG.getTargetExtractSubreg(SystemZ::odd128(Is32Bit), DL, VT, Result);
1645 }
1646
1647 // Return an i32 value that is 1 if the CC value produced by Glue is
1648 // in the mask CCMask and 0 otherwise.  CC is known to have a value
1649 // in CCValid, so other values can be ignored.
1650 static SDValue emitSETCC(SelectionDAG &DAG, SDLoc DL, SDValue Glue,
1651                          unsigned CCValid, unsigned CCMask) {
1652   IPMConversion Conversion = getIPMConversion(CCValid, CCMask);
1653   SDValue Result = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
1654
1655   if (Conversion.XORValue)
1656     Result = DAG.getNode(ISD::XOR, DL, MVT::i32, Result,
1657                          DAG.getConstant(Conversion.XORValue, MVT::i32));
1658
1659   if (Conversion.AddValue)
1660     Result = DAG.getNode(ISD::ADD, DL, MVT::i32, Result,
1661                          DAG.getConstant(Conversion.AddValue, MVT::i32));
1662
1663   // The SHR/AND sequence should get optimized to an RISBG.
1664   Result = DAG.getNode(ISD::SRL, DL, MVT::i32, Result,
1665                        DAG.getConstant(Conversion.Bit, MVT::i32));
1666   if (Conversion.Bit != 31)
1667     Result = DAG.getNode(ISD::AND, DL, MVT::i32, Result,
1668                          DAG.getConstant(1, MVT::i32));
1669   return Result;
1670 }
1671
1672 SDValue SystemZTargetLowering::lowerSETCC(SDValue Op,
1673                                           SelectionDAG &DAG) const {
1674   SDValue CmpOp0   = Op.getOperand(0);
1675   SDValue CmpOp1   = Op.getOperand(1);
1676   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
1677   SDLoc DL(Op);
1678
1679   Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC));
1680   SDValue Glue = emitCmp(DAG, DL, C);
1681   return emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
1682 }
1683
1684 SDValue SystemZTargetLowering::lowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
1685   SDValue Chain    = Op.getOperand(0);
1686   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
1687   SDValue CmpOp0   = Op.getOperand(2);
1688   SDValue CmpOp1   = Op.getOperand(3);
1689   SDValue Dest     = Op.getOperand(4);
1690   SDLoc DL(Op);
1691
1692   Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC));
1693   SDValue Glue = emitCmp(DAG, DL, C);
1694   return DAG.getNode(SystemZISD::BR_CCMASK, DL, Op.getValueType(),
1695                      Chain, DAG.getConstant(C.CCValid, MVT::i32),
1696                      DAG.getConstant(C.CCMask, MVT::i32), Dest, Glue);
1697 }
1698
1699 // Return true if Pos is CmpOp and Neg is the negative of CmpOp,
1700 // allowing Pos and Neg to be wider than CmpOp.
1701 static bool isAbsolute(SDValue CmpOp, SDValue Pos, SDValue Neg) {
1702   return (Neg.getOpcode() == ISD::SUB &&
1703           Neg.getOperand(0).getOpcode() == ISD::Constant &&
1704           cast<ConstantSDNode>(Neg.getOperand(0))->getZExtValue() == 0 &&
1705           Neg.getOperand(1) == Pos &&
1706           (Pos == CmpOp ||
1707            (Pos.getOpcode() == ISD::SIGN_EXTEND &&
1708             Pos.getOperand(0) == CmpOp)));
1709 }
1710
1711 // Return the absolute or negative absolute of Op; IsNegative decides which.
1712 static SDValue getAbsolute(SelectionDAG &DAG, SDLoc DL, SDValue Op,
1713                            bool IsNegative) {
1714   Op = DAG.getNode(SystemZISD::IABS, DL, Op.getValueType(), Op);
1715   if (IsNegative)
1716     Op = DAG.getNode(ISD::SUB, DL, Op.getValueType(),
1717                      DAG.getConstant(0, Op.getValueType()), Op);
1718   return Op;
1719 }
1720
1721 SDValue SystemZTargetLowering::lowerSELECT_CC(SDValue Op,
1722                                               SelectionDAG &DAG) const {
1723   SDValue CmpOp0   = Op.getOperand(0);
1724   SDValue CmpOp1   = Op.getOperand(1);
1725   SDValue TrueOp   = Op.getOperand(2);
1726   SDValue FalseOp  = Op.getOperand(3);
1727   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
1728   SDLoc DL(Op);
1729
1730   Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC));
1731
1732   // Check for absolute and negative-absolute selections, including those
1733   // where the comparison value is sign-extended (for LPGFR and LNGFR).
1734   // This check supplements the one in DAGCombiner.
1735   if (C.Opcode == SystemZISD::ICMP &&
1736       C.CCMask != SystemZ::CCMASK_CMP_EQ &&
1737       C.CCMask != SystemZ::CCMASK_CMP_NE &&
1738       C.Op1.getOpcode() == ISD::Constant &&
1739       cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
1740     if (isAbsolute(C.Op0, TrueOp, FalseOp))
1741       return getAbsolute(DAG, DL, TrueOp, C.CCMask & SystemZ::CCMASK_CMP_LT);
1742     if (isAbsolute(C.Op0, FalseOp, TrueOp))
1743       return getAbsolute(DAG, DL, FalseOp, C.CCMask & SystemZ::CCMASK_CMP_GT);
1744   }
1745
1746   SDValue Glue = emitCmp(DAG, DL, C);
1747
1748   // Special case for handling -1/0 results.  The shifts we use here
1749   // should get optimized with the IPM conversion sequence.
1750   auto *TrueC = dyn_cast<ConstantSDNode>(TrueOp);
1751   auto *FalseC = dyn_cast<ConstantSDNode>(FalseOp);
1752   if (TrueC && FalseC) {
1753     int64_t TrueVal = TrueC->getSExtValue();
1754     int64_t FalseVal = FalseC->getSExtValue();
1755     if ((TrueVal == -1 && FalseVal == 0) || (TrueVal == 0 && FalseVal == -1)) {
1756       // Invert the condition if we want -1 on false.
1757       if (TrueVal == 0)
1758         C.CCMask ^= C.CCValid;
1759       SDValue Result = emitSETCC(DAG, DL, Glue, C.CCValid, C.CCMask);
1760       EVT VT = Op.getValueType();
1761       // Extend the result to VT.  Upper bits are ignored.
1762       if (!is32Bit(VT))
1763         Result = DAG.getNode(ISD::ANY_EXTEND, DL, VT, Result);
1764       // Sign-extend from the low bit.
1765       SDValue ShAmt = DAG.getConstant(VT.getSizeInBits() - 1, MVT::i32);
1766       SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, Result, ShAmt);
1767       return DAG.getNode(ISD::SRA, DL, VT, Shl, ShAmt);
1768     }
1769   }
1770
1771   SmallVector<SDValue, 5> Ops;
1772   Ops.push_back(TrueOp);
1773   Ops.push_back(FalseOp);
1774   Ops.push_back(DAG.getConstant(C.CCValid, MVT::i32));
1775   Ops.push_back(DAG.getConstant(C.CCMask, MVT::i32));
1776   Ops.push_back(Glue);
1777
1778   SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Glue);
1779   return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VTs, &Ops[0], Ops.size());
1780 }
1781
1782 SDValue SystemZTargetLowering::lowerGlobalAddress(GlobalAddressSDNode *Node,
1783                                                   SelectionDAG &DAG) const {
1784   SDLoc DL(Node);
1785   const GlobalValue *GV = Node->getGlobal();
1786   int64_t Offset = Node->getOffset();
1787   EVT PtrVT = getPointerTy();
1788   Reloc::Model RM = TM.getRelocationModel();
1789   CodeModel::Model CM = TM.getCodeModel();
1790
1791   SDValue Result;
1792   if (Subtarget.isPC32DBLSymbol(GV, RM, CM)) {
1793     // Assign anchors at 1<<12 byte boundaries.
1794     uint64_t Anchor = Offset & ~uint64_t(0xfff);
1795     Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor);
1796     Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1797
1798     // The offset can be folded into the address if it is aligned to a halfword.
1799     Offset -= Anchor;
1800     if (Offset != 0 && (Offset & 1) == 0) {
1801       SDValue Full = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor + Offset);
1802       Result = DAG.getNode(SystemZISD::PCREL_OFFSET, DL, PtrVT, Full, Result);
1803       Offset = 0;
1804     }
1805   } else {
1806     Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, SystemZII::MO_GOT);
1807     Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1808     Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
1809                          MachinePointerInfo::getGOT(), false, false, false, 0);
1810   }
1811
1812   // If there was a non-zero offset that we didn't fold, create an explicit
1813   // addition for it.
1814   if (Offset != 0)
1815     Result = DAG.getNode(ISD::ADD, DL, PtrVT, Result,
1816                          DAG.getConstant(Offset, PtrVT));
1817
1818   return Result;
1819 }
1820
1821 SDValue SystemZTargetLowering::lowerGlobalTLSAddress(GlobalAddressSDNode *Node,
1822                                                      SelectionDAG &DAG) const {
1823   SDLoc DL(Node);
1824   const GlobalValue *GV = Node->getGlobal();
1825   EVT PtrVT = getPointerTy();
1826   TLSModel::Model model = TM.getTLSModel(GV);
1827
1828   if (model != TLSModel::LocalExec)
1829     llvm_unreachable("only local-exec TLS mode supported");
1830
1831   // The high part of the thread pointer is in access register 0.
1832   SDValue TPHi = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
1833                              DAG.getConstant(0, MVT::i32));
1834   TPHi = DAG.getNode(ISD::ANY_EXTEND, DL, PtrVT, TPHi);
1835
1836   // The low part of the thread pointer is in access register 1.
1837   SDValue TPLo = DAG.getNode(SystemZISD::EXTRACT_ACCESS, DL, MVT::i32,
1838                              DAG.getConstant(1, MVT::i32));
1839   TPLo = DAG.getNode(ISD::ZERO_EXTEND, DL, PtrVT, TPLo);
1840
1841   // Merge them into a single 64-bit address.
1842   SDValue TPHiShifted = DAG.getNode(ISD::SHL, DL, PtrVT, TPHi,
1843                                     DAG.getConstant(32, PtrVT));
1844   SDValue TP = DAG.getNode(ISD::OR, DL, PtrVT, TPHiShifted, TPLo);
1845
1846   // Get the offset of GA from the thread pointer.
1847   SystemZConstantPoolValue *CPV =
1848     SystemZConstantPoolValue::Create(GV, SystemZCP::NTPOFF);
1849
1850   // Force the offset into the constant pool and load it from there.
1851   SDValue CPAddr = DAG.getConstantPool(CPV, PtrVT, 8);
1852   SDValue Offset = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(),
1853                                CPAddr, MachinePointerInfo::getConstantPool(),
1854                                false, false, false, 0);
1855
1856   // Add the base and offset together.
1857   return DAG.getNode(ISD::ADD, DL, PtrVT, TP, Offset);
1858 }
1859
1860 SDValue SystemZTargetLowering::lowerBlockAddress(BlockAddressSDNode *Node,
1861                                                  SelectionDAG &DAG) const {
1862   SDLoc DL(Node);
1863   const BlockAddress *BA = Node->getBlockAddress();
1864   int64_t Offset = Node->getOffset();
1865   EVT PtrVT = getPointerTy();
1866
1867   SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset);
1868   Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1869   return Result;
1870 }
1871
1872 SDValue SystemZTargetLowering::lowerJumpTable(JumpTableSDNode *JT,
1873                                               SelectionDAG &DAG) const {
1874   SDLoc DL(JT);
1875   EVT PtrVT = getPointerTy();
1876   SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
1877
1878   // Use LARL to load the address of the table.
1879   return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1880 }
1881
1882 SDValue SystemZTargetLowering::lowerConstantPool(ConstantPoolSDNode *CP,
1883                                                  SelectionDAG &DAG) const {
1884   SDLoc DL(CP);
1885   EVT PtrVT = getPointerTy();
1886
1887   SDValue Result;
1888   if (CP->isMachineConstantPoolEntry())
1889     Result = DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT,
1890                                        CP->getAlignment());
1891   else
1892     Result = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT,
1893                                        CP->getAlignment(), CP->getOffset());
1894
1895   // Use LARL to load the address of the constant pool entry.
1896   return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
1897 }
1898
1899 SDValue SystemZTargetLowering::lowerBITCAST(SDValue Op,
1900                                             SelectionDAG &DAG) const {
1901   SDLoc DL(Op);
1902   SDValue In = Op.getOperand(0);
1903   EVT InVT = In.getValueType();
1904   EVT ResVT = Op.getValueType();
1905
1906   if (InVT == MVT::i32 && ResVT == MVT::f32) {
1907     SDValue In64;
1908     if (Subtarget.hasHighWord()) {
1909       SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL,
1910                                        MVT::i64);
1911       In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL,
1912                                        MVT::i64, SDValue(U64, 0), In);
1913     } else {
1914       In64 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, In);
1915       In64 = DAG.getNode(ISD::SHL, DL, MVT::i64, In64,
1916                          DAG.getConstant(32, MVT::i64));
1917     }
1918     SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::f64, In64);
1919     return DAG.getTargetExtractSubreg(SystemZ::subreg_h32,
1920                                       DL, MVT::f32, Out64);
1921   }
1922   if (InVT == MVT::f32 && ResVT == MVT::i32) {
1923     SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::f64);
1924     SDValue In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL,
1925                                              MVT::f64, SDValue(U64, 0), In);
1926     SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::i64, In64);
1927     if (Subtarget.hasHighWord())
1928       return DAG.getTargetExtractSubreg(SystemZ::subreg_h32, DL,
1929                                         MVT::i32, Out64);
1930     SDValue Shift = DAG.getNode(ISD::SRL, DL, MVT::i64, Out64,
1931                                 DAG.getConstant(32, MVT::i64));
1932     return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Shift);
1933   }
1934   llvm_unreachable("Unexpected bitcast combination");
1935 }
1936
1937 SDValue SystemZTargetLowering::lowerVASTART(SDValue Op,
1938                                             SelectionDAG &DAG) const {
1939   MachineFunction &MF = DAG.getMachineFunction();
1940   SystemZMachineFunctionInfo *FuncInfo =
1941     MF.getInfo<SystemZMachineFunctionInfo>();
1942   EVT PtrVT = getPointerTy();
1943
1944   SDValue Chain   = Op.getOperand(0);
1945   SDValue Addr    = Op.getOperand(1);
1946   const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
1947   SDLoc DL(Op);
1948
1949   // The initial values of each field.
1950   const unsigned NumFields = 4;
1951   SDValue Fields[NumFields] = {
1952     DAG.getConstant(FuncInfo->getVarArgsFirstGPR(), PtrVT),
1953     DAG.getConstant(FuncInfo->getVarArgsFirstFPR(), PtrVT),
1954     DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT),
1955     DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), PtrVT)
1956   };
1957
1958   // Store each field into its respective slot.
1959   SDValue MemOps[NumFields];
1960   unsigned Offset = 0;
1961   for (unsigned I = 0; I < NumFields; ++I) {
1962     SDValue FieldAddr = Addr;
1963     if (Offset != 0)
1964       FieldAddr = DAG.getNode(ISD::ADD, DL, PtrVT, FieldAddr,
1965                               DAG.getIntPtrConstant(Offset));
1966     MemOps[I] = DAG.getStore(Chain, DL, Fields[I], FieldAddr,
1967                              MachinePointerInfo(SV, Offset),
1968                              false, false, 0);
1969     Offset += 8;
1970   }
1971   return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps, NumFields);
1972 }
1973
1974 SDValue SystemZTargetLowering::lowerVACOPY(SDValue Op,
1975                                            SelectionDAG &DAG) const {
1976   SDValue Chain      = Op.getOperand(0);
1977   SDValue DstPtr     = Op.getOperand(1);
1978   SDValue SrcPtr     = Op.getOperand(2);
1979   const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
1980   const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
1981   SDLoc DL(Op);
1982
1983   return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr, DAG.getIntPtrConstant(32),
1984                        /*Align*/8, /*isVolatile*/false, /*AlwaysInline*/false,
1985                        MachinePointerInfo(DstSV), MachinePointerInfo(SrcSV));
1986 }
1987
1988 SDValue SystemZTargetLowering::
1989 lowerDYNAMIC_STACKALLOC(SDValue Op, SelectionDAG &DAG) const {
1990   SDValue Chain = Op.getOperand(0);
1991   SDValue Size  = Op.getOperand(1);
1992   SDLoc DL(Op);
1993
1994   unsigned SPReg = getStackPointerRegisterToSaveRestore();
1995
1996   // Get a reference to the stack pointer.
1997   SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i64);
1998
1999   // Get the new stack pointer value.
2000   SDValue NewSP = DAG.getNode(ISD::SUB, DL, MVT::i64, OldSP, Size);
2001
2002   // Copy the new stack pointer back.
2003   Chain = DAG.getCopyToReg(Chain, DL, SPReg, NewSP);
2004
2005   // The allocated data lives above the 160 bytes allocated for the standard
2006   // frame, plus any outgoing stack arguments.  We don't know how much that
2007   // amounts to yet, so emit a special ADJDYNALLOC placeholder.
2008   SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
2009   SDValue Result = DAG.getNode(ISD::ADD, DL, MVT::i64, NewSP, ArgAdjust);
2010
2011   SDValue Ops[2] = { Result, Chain };
2012   return DAG.getMergeValues(Ops, 2, DL);
2013 }
2014
2015 SDValue SystemZTargetLowering::lowerSMUL_LOHI(SDValue Op,
2016                                               SelectionDAG &DAG) const {
2017   EVT VT = Op.getValueType();
2018   SDLoc DL(Op);
2019   SDValue Ops[2];
2020   if (is32Bit(VT))
2021     // Just do a normal 64-bit multiplication and extract the results.
2022     // We define this so that it can be used for constant division.
2023     lowerMUL_LOHI32(DAG, DL, ISD::SIGN_EXTEND, Op.getOperand(0),
2024                     Op.getOperand(1), Ops[1], Ops[0]);
2025   else {
2026     // Do a full 128-bit multiplication based on UMUL_LOHI64:
2027     //
2028     //   (ll * rl) + ((lh * rl) << 64) + ((ll * rh) << 64)
2029     //
2030     // but using the fact that the upper halves are either all zeros
2031     // or all ones:
2032     //
2033     //   (ll * rl) - ((lh & rl) << 64) - ((ll & rh) << 64)
2034     //
2035     // and grouping the right terms together since they are quicker than the
2036     // multiplication:
2037     //
2038     //   (ll * rl) - (((lh & rl) + (ll & rh)) << 64)
2039     SDValue C63 = DAG.getConstant(63, MVT::i64);
2040     SDValue LL = Op.getOperand(0);
2041     SDValue RL = Op.getOperand(1);
2042     SDValue LH = DAG.getNode(ISD::SRA, DL, VT, LL, C63);
2043     SDValue RH = DAG.getNode(ISD::SRA, DL, VT, RL, C63);
2044     // UMUL_LOHI64 returns the low result in the odd register and the high
2045     // result in the even register.  SMUL_LOHI is defined to return the
2046     // low half first, so the results are in reverse order.
2047     lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
2048                      LL, RL, Ops[1], Ops[0]);
2049     SDValue NegLLTimesRH = DAG.getNode(ISD::AND, DL, VT, LL, RH);
2050     SDValue NegLHTimesRL = DAG.getNode(ISD::AND, DL, VT, LH, RL);
2051     SDValue NegSum = DAG.getNode(ISD::ADD, DL, VT, NegLLTimesRH, NegLHTimesRL);
2052     Ops[1] = DAG.getNode(ISD::SUB, DL, VT, Ops[1], NegSum);
2053   }
2054   return DAG.getMergeValues(Ops, 2, DL);
2055 }
2056
2057 SDValue SystemZTargetLowering::lowerUMUL_LOHI(SDValue Op,
2058                                               SelectionDAG &DAG) const {
2059   EVT VT = Op.getValueType();
2060   SDLoc DL(Op);
2061   SDValue Ops[2];
2062   if (is32Bit(VT))
2063     // Just do a normal 64-bit multiplication and extract the results.
2064     // We define this so that it can be used for constant division.
2065     lowerMUL_LOHI32(DAG, DL, ISD::ZERO_EXTEND, Op.getOperand(0),
2066                     Op.getOperand(1), Ops[1], Ops[0]);
2067   else
2068     // UMUL_LOHI64 returns the low result in the odd register and the high
2069     // result in the even register.  UMUL_LOHI is defined to return the
2070     // low half first, so the results are in reverse order.
2071     lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, SystemZISD::UMUL_LOHI64,
2072                      Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
2073   return DAG.getMergeValues(Ops, 2, DL);
2074 }
2075
2076 SDValue SystemZTargetLowering::lowerSDIVREM(SDValue Op,
2077                                             SelectionDAG &DAG) const {
2078   SDValue Op0 = Op.getOperand(0);
2079   SDValue Op1 = Op.getOperand(1);
2080   EVT VT = Op.getValueType();
2081   SDLoc DL(Op);
2082   unsigned Opcode;
2083
2084   // We use DSGF for 32-bit division.
2085   if (is32Bit(VT)) {
2086     Op0 = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, Op0);
2087     Opcode = SystemZISD::SDIVREM32;
2088   } else if (DAG.ComputeNumSignBits(Op1) > 32) {
2089     Op1 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op1);
2090     Opcode = SystemZISD::SDIVREM32;
2091   } else    
2092     Opcode = SystemZISD::SDIVREM64;
2093
2094   // DSG(F) takes a 64-bit dividend, so the even register in the GR128
2095   // input is "don't care".  The instruction returns the remainder in
2096   // the even register and the quotient in the odd register.
2097   SDValue Ops[2];
2098   lowerGR128Binary(DAG, DL, VT, SystemZ::AEXT128_64, Opcode,
2099                    Op0, Op1, Ops[1], Ops[0]);
2100   return DAG.getMergeValues(Ops, 2, DL);
2101 }
2102
2103 SDValue SystemZTargetLowering::lowerUDIVREM(SDValue Op,
2104                                             SelectionDAG &DAG) const {
2105   EVT VT = Op.getValueType();
2106   SDLoc DL(Op);
2107
2108   // DL(G) uses a double-width dividend, so we need to clear the even
2109   // register in the GR128 input.  The instruction returns the remainder
2110   // in the even register and the quotient in the odd register.
2111   SDValue Ops[2];
2112   if (is32Bit(VT))
2113     lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_32, SystemZISD::UDIVREM32,
2114                      Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
2115   else
2116     lowerGR128Binary(DAG, DL, VT, SystemZ::ZEXT128_64, SystemZISD::UDIVREM64,
2117                      Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
2118   return DAG.getMergeValues(Ops, 2, DL);
2119 }
2120
2121 SDValue SystemZTargetLowering::lowerOR(SDValue Op, SelectionDAG &DAG) const {
2122   assert(Op.getValueType() == MVT::i64 && "Should be 64-bit operation");
2123
2124   // Get the known-zero masks for each operand.
2125   SDValue Ops[] = { Op.getOperand(0), Op.getOperand(1) };
2126   APInt KnownZero[2], KnownOne[2];
2127   DAG.ComputeMaskedBits(Ops[0], KnownZero[0], KnownOne[0]);
2128   DAG.ComputeMaskedBits(Ops[1], KnownZero[1], KnownOne[1]);
2129
2130   // See if the upper 32 bits of one operand and the lower 32 bits of the
2131   // other are known zero.  They are the low and high operands respectively.
2132   uint64_t Masks[] = { KnownZero[0].getZExtValue(),
2133                        KnownZero[1].getZExtValue() };
2134   unsigned High, Low;
2135   if ((Masks[0] >> 32) == 0xffffffff && uint32_t(Masks[1]) == 0xffffffff)
2136     High = 1, Low = 0;
2137   else if ((Masks[1] >> 32) == 0xffffffff && uint32_t(Masks[0]) == 0xffffffff)
2138     High = 0, Low = 1;
2139   else
2140     return Op;
2141
2142   SDValue LowOp = Ops[Low];
2143   SDValue HighOp = Ops[High];
2144
2145   // If the high part is a constant, we're better off using IILH.
2146   if (HighOp.getOpcode() == ISD::Constant)
2147     return Op;
2148
2149   // If the low part is a constant that is outside the range of LHI,
2150   // then we're better off using IILF.
2151   if (LowOp.getOpcode() == ISD::Constant) {
2152     int64_t Value = int32_t(cast<ConstantSDNode>(LowOp)->getZExtValue());
2153     if (!isInt<16>(Value))
2154       return Op;
2155   }
2156
2157   // Check whether the high part is an AND that doesn't change the
2158   // high 32 bits and just masks out low bits.  We can skip it if so.
2159   if (HighOp.getOpcode() == ISD::AND &&
2160       HighOp.getOperand(1).getOpcode() == ISD::Constant) {
2161     SDValue HighOp0 = HighOp.getOperand(0);
2162     uint64_t Mask = cast<ConstantSDNode>(HighOp.getOperand(1))->getZExtValue();
2163     if (DAG.MaskedValueIsZero(HighOp0, APInt(64, ~(Mask | 0xffffffff))))
2164       HighOp = HighOp0;
2165   }
2166
2167   // Take advantage of the fact that all GR32 operations only change the
2168   // low 32 bits by truncating Low to an i32 and inserting it directly
2169   // using a subreg.  The interesting cases are those where the truncation
2170   // can be folded.
2171   SDLoc DL(Op);
2172   SDValue Low32 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, LowOp);
2173   return DAG.getTargetInsertSubreg(SystemZ::subreg_l32, DL,
2174                                    MVT::i64, HighOp, Low32);
2175 }
2176
2177 SDValue SystemZTargetLowering::lowerSIGN_EXTEND(SDValue Op,
2178                                                 SelectionDAG &DAG) const {
2179   // Convert (sext (ashr (shl X, C1), C2)) to
2180   // (ashr (shl (anyext X), C1'), C2')), since wider shifts are as
2181   // cheap as narrower ones.
2182   SDValue N0 = Op.getOperand(0);
2183   EVT VT = Op.getValueType();
2184   if (N0.hasOneUse() && N0.getOpcode() == ISD::SRA) {
2185     auto *SraAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1));
2186     SDValue Inner = N0.getOperand(0);
2187     if (SraAmt && Inner.hasOneUse() && Inner.getOpcode() == ISD::SHL) {
2188       auto *ShlAmt = dyn_cast<ConstantSDNode>(Inner.getOperand(1));
2189       if (ShlAmt) {
2190         unsigned Extra = (VT.getSizeInBits() -
2191                           N0.getValueType().getSizeInBits());
2192         unsigned NewShlAmt = ShlAmt->getZExtValue() + Extra;
2193         unsigned NewSraAmt = SraAmt->getZExtValue() + Extra;
2194         EVT ShiftVT = N0.getOperand(1).getValueType();
2195         SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SDLoc(Inner), VT,
2196                                   Inner.getOperand(0));
2197         SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(Inner), VT, Ext,
2198                                   DAG.getConstant(NewShlAmt, ShiftVT));
2199         return DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl,
2200                            DAG.getConstant(NewSraAmt, ShiftVT));
2201       }
2202     }
2203   }
2204   return SDValue();
2205 }
2206
2207 // Op is an atomic load.  Lower it into a normal volatile load.
2208 SDValue SystemZTargetLowering::lowerATOMIC_LOAD(SDValue Op,
2209                                                 SelectionDAG &DAG) const {
2210   auto *Node = cast<AtomicSDNode>(Op.getNode());
2211   return DAG.getExtLoad(ISD::EXTLOAD, SDLoc(Op), Op.getValueType(),
2212                         Node->getChain(), Node->getBasePtr(),
2213                         Node->getMemoryVT(), Node->getMemOperand());
2214 }
2215
2216 // Op is an atomic store.  Lower it into a normal volatile store followed
2217 // by a serialization.
2218 SDValue SystemZTargetLowering::lowerATOMIC_STORE(SDValue Op,
2219                                                  SelectionDAG &DAG) const {
2220   auto *Node = cast<AtomicSDNode>(Op.getNode());
2221   SDValue Chain = DAG.getTruncStore(Node->getChain(), SDLoc(Op), Node->getVal(),
2222                                     Node->getBasePtr(), Node->getMemoryVT(),
2223                                     Node->getMemOperand());
2224   return SDValue(DAG.getMachineNode(SystemZ::Serialize, SDLoc(Op), MVT::Other,
2225                                     Chain), 0);
2226 }
2227
2228 // Op is an 8-, 16-bit or 32-bit ATOMIC_LOAD_* operation.  Lower the first
2229 // two into the fullword ATOMIC_LOADW_* operation given by Opcode.
2230 SDValue SystemZTargetLowering::lowerATOMIC_LOAD_OP(SDValue Op,
2231                                                    SelectionDAG &DAG,
2232                                                    unsigned Opcode) const {
2233   auto *Node = cast<AtomicSDNode>(Op.getNode());
2234
2235   // 32-bit operations need no code outside the main loop.
2236   EVT NarrowVT = Node->getMemoryVT();
2237   EVT WideVT = MVT::i32;
2238   if (NarrowVT == WideVT)
2239     return Op;
2240
2241   int64_t BitSize = NarrowVT.getSizeInBits();
2242   SDValue ChainIn = Node->getChain();
2243   SDValue Addr = Node->getBasePtr();
2244   SDValue Src2 = Node->getVal();
2245   MachineMemOperand *MMO = Node->getMemOperand();
2246   SDLoc DL(Node);
2247   EVT PtrVT = Addr.getValueType();
2248
2249   // Convert atomic subtracts of constants into additions.
2250   if (Opcode == SystemZISD::ATOMIC_LOADW_SUB)
2251     if (auto *Const = dyn_cast<ConstantSDNode>(Src2)) {
2252       Opcode = SystemZISD::ATOMIC_LOADW_ADD;
2253       Src2 = DAG.getConstant(-Const->getSExtValue(), Src2.getValueType());
2254     }
2255
2256   // Get the address of the containing word.
2257   SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
2258                                     DAG.getConstant(-4, PtrVT));
2259
2260   // Get the number of bits that the word must be rotated left in order
2261   // to bring the field to the top bits of a GR32.
2262   SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
2263                                  DAG.getConstant(3, PtrVT));
2264   BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
2265
2266   // Get the complementing shift amount, for rotating a field in the top
2267   // bits back to its proper position.
2268   SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
2269                                     DAG.getConstant(0, WideVT), BitShift);
2270
2271   // Extend the source operand to 32 bits and prepare it for the inner loop.
2272   // ATOMIC_SWAPW uses RISBG to rotate the field left, but all other
2273   // operations require the source to be shifted in advance.  (This shift
2274   // can be folded if the source is constant.)  For AND and NAND, the lower
2275   // bits must be set, while for other opcodes they should be left clear.
2276   if (Opcode != SystemZISD::ATOMIC_SWAPW)
2277     Src2 = DAG.getNode(ISD::SHL, DL, WideVT, Src2,
2278                        DAG.getConstant(32 - BitSize, WideVT));
2279   if (Opcode == SystemZISD::ATOMIC_LOADW_AND ||
2280       Opcode == SystemZISD::ATOMIC_LOADW_NAND)
2281     Src2 = DAG.getNode(ISD::OR, DL, WideVT, Src2,
2282                        DAG.getConstant(uint32_t(-1) >> BitSize, WideVT));
2283
2284   // Construct the ATOMIC_LOADW_* node.
2285   SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
2286   SDValue Ops[] = { ChainIn, AlignedAddr, Src2, BitShift, NegBitShift,
2287                     DAG.getConstant(BitSize, WideVT) };
2288   SDValue AtomicOp = DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops,
2289                                              array_lengthof(Ops),
2290                                              NarrowVT, MMO);
2291
2292   // Rotate the result of the final CS so that the field is in the lower
2293   // bits of a GR32, then truncate it.
2294   SDValue ResultShift = DAG.getNode(ISD::ADD, DL, WideVT, BitShift,
2295                                     DAG.getConstant(BitSize, WideVT));
2296   SDValue Result = DAG.getNode(ISD::ROTL, DL, WideVT, AtomicOp, ResultShift);
2297
2298   SDValue RetOps[2] = { Result, AtomicOp.getValue(1) };
2299   return DAG.getMergeValues(RetOps, 2, DL);
2300 }
2301
2302 // Op is an ATOMIC_LOAD_SUB operation.  Lower 8- and 16-bit operations
2303 // into ATOMIC_LOADW_SUBs and decide whether to convert 32- and 64-bit
2304 // operations into additions.
2305 SDValue SystemZTargetLowering::lowerATOMIC_LOAD_SUB(SDValue Op,
2306                                                     SelectionDAG &DAG) const {
2307   auto *Node = cast<AtomicSDNode>(Op.getNode());
2308   EVT MemVT = Node->getMemoryVT();
2309   if (MemVT == MVT::i32 || MemVT == MVT::i64) {
2310     // A full-width operation.
2311     assert(Op.getValueType() == MemVT && "Mismatched VTs");
2312     SDValue Src2 = Node->getVal();
2313     SDValue NegSrc2;
2314     SDLoc DL(Src2);
2315
2316     if (auto *Op2 = dyn_cast<ConstantSDNode>(Src2)) {
2317       // Use an addition if the operand is constant and either LAA(G) is
2318       // available or the negative value is in the range of A(G)FHI.
2319       int64_t Value = (-Op2->getAPIntValue()).getSExtValue();
2320       if (isInt<32>(Value) || TM.getSubtargetImpl()->hasInterlockedAccess1())
2321         NegSrc2 = DAG.getConstant(Value, MemVT);
2322     } else if (TM.getSubtargetImpl()->hasInterlockedAccess1())
2323       // Use LAA(G) if available.
2324       NegSrc2 = DAG.getNode(ISD::SUB, DL, MemVT, DAG.getConstant(0, MemVT),
2325                             Src2);
2326
2327     if (NegSrc2.getNode())
2328       return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, MemVT,
2329                            Node->getChain(), Node->getBasePtr(), NegSrc2,
2330                            Node->getMemOperand(), Node->getOrdering(),
2331                            Node->getSynchScope());
2332
2333     // Use the node as-is.
2334     return Op;
2335   }
2336
2337   return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_SUB);
2338 }
2339
2340 // Node is an 8- or 16-bit ATOMIC_CMP_SWAP operation.  Lower the first two
2341 // into a fullword ATOMIC_CMP_SWAPW operation.
2342 SDValue SystemZTargetLowering::lowerATOMIC_CMP_SWAP(SDValue Op,
2343                                                     SelectionDAG &DAG) const {
2344   auto *Node = cast<AtomicSDNode>(Op.getNode());
2345
2346   // We have native support for 32-bit compare and swap.
2347   EVT NarrowVT = Node->getMemoryVT();
2348   EVT WideVT = MVT::i32;
2349   if (NarrowVT == WideVT)
2350     return Op;
2351
2352   int64_t BitSize = NarrowVT.getSizeInBits();
2353   SDValue ChainIn = Node->getOperand(0);
2354   SDValue Addr = Node->getOperand(1);
2355   SDValue CmpVal = Node->getOperand(2);
2356   SDValue SwapVal = Node->getOperand(3);
2357   MachineMemOperand *MMO = Node->getMemOperand();
2358   SDLoc DL(Node);
2359   EVT PtrVT = Addr.getValueType();
2360
2361   // Get the address of the containing word.
2362   SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
2363                                     DAG.getConstant(-4, PtrVT));
2364
2365   // Get the number of bits that the word must be rotated left in order
2366   // to bring the field to the top bits of a GR32.
2367   SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
2368                                  DAG.getConstant(3, PtrVT));
2369   BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
2370
2371   // Get the complementing shift amount, for rotating a field in the top
2372   // bits back to its proper position.
2373   SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
2374                                     DAG.getConstant(0, WideVT), BitShift);
2375
2376   // Construct the ATOMIC_CMP_SWAPW node.
2377   SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
2378   SDValue Ops[] = { ChainIn, AlignedAddr, CmpVal, SwapVal, BitShift,
2379                     NegBitShift, DAG.getConstant(BitSize, WideVT) };
2380   SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAPW, DL,
2381                                              VTList, Ops, array_lengthof(Ops),
2382                                              NarrowVT, MMO);
2383   return AtomicOp;
2384 }
2385
2386 SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op,
2387                                               SelectionDAG &DAG) const {
2388   MachineFunction &MF = DAG.getMachineFunction();
2389   MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
2390   return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op),
2391                             SystemZ::R15D, Op.getValueType());
2392 }
2393
2394 SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op,
2395                                                  SelectionDAG &DAG) const {
2396   MachineFunction &MF = DAG.getMachineFunction();
2397   MF.getInfo<SystemZMachineFunctionInfo>()->setManipulatesSP(true);
2398   return DAG.getCopyToReg(Op.getOperand(0), SDLoc(Op),
2399                           SystemZ::R15D, Op.getOperand(1));
2400 }
2401
2402 SDValue SystemZTargetLowering::lowerPREFETCH(SDValue Op,
2403                                              SelectionDAG &DAG) const {
2404   bool IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
2405   if (!IsData)
2406     // Just preserve the chain.
2407     return Op.getOperand(0);
2408
2409   bool IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
2410   unsigned Code = IsWrite ? SystemZ::PFD_WRITE : SystemZ::PFD_READ;
2411   auto *Node = cast<MemIntrinsicSDNode>(Op.getNode());
2412   SDValue Ops[] = {
2413     Op.getOperand(0),
2414     DAG.getConstant(Code, MVT::i32),
2415     Op.getOperand(1)
2416   };
2417   return DAG.getMemIntrinsicNode(SystemZISD::PREFETCH, SDLoc(Op),
2418                                  Node->getVTList(), Ops, array_lengthof(Ops),
2419                                  Node->getMemoryVT(), Node->getMemOperand());
2420 }
2421
2422 SDValue SystemZTargetLowering::LowerOperation(SDValue Op,
2423                                               SelectionDAG &DAG) const {
2424   switch (Op.getOpcode()) {
2425   case ISD::BR_CC:
2426     return lowerBR_CC(Op, DAG);
2427   case ISD::SELECT_CC:
2428     return lowerSELECT_CC(Op, DAG);
2429   case ISD::SETCC:
2430     return lowerSETCC(Op, DAG);
2431   case ISD::GlobalAddress:
2432     return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG);
2433   case ISD::GlobalTLSAddress:
2434     return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG);
2435   case ISD::BlockAddress:
2436     return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG);
2437   case ISD::JumpTable:
2438     return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG);
2439   case ISD::ConstantPool:
2440     return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG);
2441   case ISD::BITCAST:
2442     return lowerBITCAST(Op, DAG);
2443   case ISD::VASTART:
2444     return lowerVASTART(Op, DAG);
2445   case ISD::VACOPY:
2446     return lowerVACOPY(Op, DAG);
2447   case ISD::DYNAMIC_STACKALLOC:
2448     return lowerDYNAMIC_STACKALLOC(Op, DAG);
2449   case ISD::SMUL_LOHI:
2450     return lowerSMUL_LOHI(Op, DAG);
2451   case ISD::UMUL_LOHI:
2452     return lowerUMUL_LOHI(Op, DAG);
2453   case ISD::SDIVREM:
2454     return lowerSDIVREM(Op, DAG);
2455   case ISD::UDIVREM:
2456     return lowerUDIVREM(Op, DAG);
2457   case ISD::OR:
2458     return lowerOR(Op, DAG);
2459   case ISD::SIGN_EXTEND:
2460     return lowerSIGN_EXTEND(Op, DAG);
2461   case ISD::ATOMIC_SWAP:
2462     return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_SWAPW);
2463   case ISD::ATOMIC_STORE:
2464     return lowerATOMIC_STORE(Op, DAG);
2465   case ISD::ATOMIC_LOAD:
2466     return lowerATOMIC_LOAD(Op, DAG);
2467   case ISD::ATOMIC_LOAD_ADD:
2468     return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD);
2469   case ISD::ATOMIC_LOAD_SUB:
2470     return lowerATOMIC_LOAD_SUB(Op, DAG);
2471   case ISD::ATOMIC_LOAD_AND:
2472     return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_AND);
2473   case ISD::ATOMIC_LOAD_OR:
2474     return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_OR);
2475   case ISD::ATOMIC_LOAD_XOR:
2476     return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR);
2477   case ISD::ATOMIC_LOAD_NAND:
2478     return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND);
2479   case ISD::ATOMIC_LOAD_MIN:
2480     return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN);
2481   case ISD::ATOMIC_LOAD_MAX:
2482     return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX);
2483   case ISD::ATOMIC_LOAD_UMIN:
2484     return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN);
2485   case ISD::ATOMIC_LOAD_UMAX:
2486     return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX);
2487   case ISD::ATOMIC_CMP_SWAP:
2488     return lowerATOMIC_CMP_SWAP(Op, DAG);
2489   case ISD::STACKSAVE:
2490     return lowerSTACKSAVE(Op, DAG);
2491   case ISD::STACKRESTORE:
2492     return lowerSTACKRESTORE(Op, DAG);
2493   case ISD::PREFETCH:
2494     return lowerPREFETCH(Op, DAG);
2495   default:
2496     llvm_unreachable("Unexpected node to lower");
2497   }
2498 }
2499
2500 const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
2501 #define OPCODE(NAME) case SystemZISD::NAME: return "SystemZISD::" #NAME
2502   switch (Opcode) {
2503     OPCODE(RET_FLAG);
2504     OPCODE(CALL);
2505     OPCODE(SIBCALL);
2506     OPCODE(PCREL_WRAPPER);
2507     OPCODE(PCREL_OFFSET);
2508     OPCODE(IABS);
2509     OPCODE(ICMP);
2510     OPCODE(FCMP);
2511     OPCODE(TM);
2512     OPCODE(BR_CCMASK);
2513     OPCODE(SELECT_CCMASK);
2514     OPCODE(ADJDYNALLOC);
2515     OPCODE(EXTRACT_ACCESS);
2516     OPCODE(UMUL_LOHI64);
2517     OPCODE(SDIVREM64);
2518     OPCODE(UDIVREM32);
2519     OPCODE(UDIVREM64);
2520     OPCODE(MVC);
2521     OPCODE(MVC_LOOP);
2522     OPCODE(NC);
2523     OPCODE(NC_LOOP);
2524     OPCODE(OC);
2525     OPCODE(OC_LOOP);
2526     OPCODE(XC);
2527     OPCODE(XC_LOOP);
2528     OPCODE(CLC);
2529     OPCODE(CLC_LOOP);
2530     OPCODE(STRCMP);
2531     OPCODE(STPCPY);
2532     OPCODE(SEARCH_STRING);
2533     OPCODE(IPM);
2534     OPCODE(SERIALIZE);
2535     OPCODE(ATOMIC_SWAPW);
2536     OPCODE(ATOMIC_LOADW_ADD);
2537     OPCODE(ATOMIC_LOADW_SUB);
2538     OPCODE(ATOMIC_LOADW_AND);
2539     OPCODE(ATOMIC_LOADW_OR);
2540     OPCODE(ATOMIC_LOADW_XOR);
2541     OPCODE(ATOMIC_LOADW_NAND);
2542     OPCODE(ATOMIC_LOADW_MIN);
2543     OPCODE(ATOMIC_LOADW_MAX);
2544     OPCODE(ATOMIC_LOADW_UMIN);
2545     OPCODE(ATOMIC_LOADW_UMAX);
2546     OPCODE(ATOMIC_CMP_SWAPW);
2547     OPCODE(PREFETCH);
2548   }
2549   return NULL;
2550 #undef OPCODE
2551 }
2552
2553 //===----------------------------------------------------------------------===//
2554 // Custom insertion
2555 //===----------------------------------------------------------------------===//
2556
2557 // Create a new basic block after MBB.
2558 static MachineBasicBlock *emitBlockAfter(MachineBasicBlock *MBB) {
2559   MachineFunction &MF = *MBB->getParent();
2560   MachineBasicBlock *NewMBB = MF.CreateMachineBasicBlock(MBB->getBasicBlock());
2561   MF.insert(std::next(MachineFunction::iterator(MBB)), NewMBB);
2562   return NewMBB;
2563 }
2564
2565 // Split MBB after MI and return the new block (the one that contains
2566 // instructions after MI).
2567 static MachineBasicBlock *splitBlockAfter(MachineInstr *MI,
2568                                           MachineBasicBlock *MBB) {
2569   MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
2570   NewMBB->splice(NewMBB->begin(), MBB,
2571                  std::next(MachineBasicBlock::iterator(MI)), MBB->end());
2572   NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
2573   return NewMBB;
2574 }
2575
2576 // Split MBB before MI and return the new block (the one that contains MI).
2577 static MachineBasicBlock *splitBlockBefore(MachineInstr *MI,
2578                                            MachineBasicBlock *MBB) {
2579   MachineBasicBlock *NewMBB = emitBlockAfter(MBB);
2580   NewMBB->splice(NewMBB->begin(), MBB, MI, MBB->end());
2581   NewMBB->transferSuccessorsAndUpdatePHIs(MBB);
2582   return NewMBB;
2583 }
2584
2585 // Force base value Base into a register before MI.  Return the register.
2586 static unsigned forceReg(MachineInstr *MI, MachineOperand &Base,
2587                          const SystemZInstrInfo *TII) {
2588   if (Base.isReg())
2589     return Base.getReg();
2590
2591   MachineBasicBlock *MBB = MI->getParent();
2592   MachineFunction &MF = *MBB->getParent();
2593   MachineRegisterInfo &MRI = MF.getRegInfo();
2594
2595   unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
2596   BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LA), Reg)
2597     .addOperand(Base).addImm(0).addReg(0);
2598   return Reg;
2599 }
2600
2601 // Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI.
2602 MachineBasicBlock *
2603 SystemZTargetLowering::emitSelect(MachineInstr *MI,
2604                                   MachineBasicBlock *MBB) const {
2605   const SystemZInstrInfo *TII = TM.getInstrInfo();
2606
2607   unsigned DestReg  = MI->getOperand(0).getReg();
2608   unsigned TrueReg  = MI->getOperand(1).getReg();
2609   unsigned FalseReg = MI->getOperand(2).getReg();
2610   unsigned CCValid  = MI->getOperand(3).getImm();
2611   unsigned CCMask   = MI->getOperand(4).getImm();
2612   DebugLoc DL       = MI->getDebugLoc();
2613
2614   MachineBasicBlock *StartMBB = MBB;
2615   MachineBasicBlock *JoinMBB  = splitBlockBefore(MI, MBB);
2616   MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
2617
2618   //  StartMBB:
2619   //   BRC CCMask, JoinMBB
2620   //   # fallthrough to FalseMBB
2621   MBB = StartMBB;
2622   BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2623     .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
2624   MBB->addSuccessor(JoinMBB);
2625   MBB->addSuccessor(FalseMBB);
2626
2627   //  FalseMBB:
2628   //   # fallthrough to JoinMBB
2629   MBB = FalseMBB;
2630   MBB->addSuccessor(JoinMBB);
2631
2632   //  JoinMBB:
2633   //   %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ]
2634   //  ...
2635   MBB = JoinMBB;
2636   BuildMI(*MBB, MI, DL, TII->get(SystemZ::PHI), DestReg)
2637     .addReg(TrueReg).addMBB(StartMBB)
2638     .addReg(FalseReg).addMBB(FalseMBB);
2639
2640   MI->eraseFromParent();
2641   return JoinMBB;
2642 }
2643
2644 // Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI.
2645 // StoreOpcode is the store to use and Invert says whether the store should
2646 // happen when the condition is false rather than true.  If a STORE ON
2647 // CONDITION is available, STOCOpcode is its opcode, otherwise it is 0.
2648 MachineBasicBlock *
2649 SystemZTargetLowering::emitCondStore(MachineInstr *MI,
2650                                      MachineBasicBlock *MBB,
2651                                      unsigned StoreOpcode, unsigned STOCOpcode,
2652                                      bool Invert) const {
2653   const SystemZInstrInfo *TII = TM.getInstrInfo();
2654
2655   unsigned SrcReg     = MI->getOperand(0).getReg();
2656   MachineOperand Base = MI->getOperand(1);
2657   int64_t Disp        = MI->getOperand(2).getImm();
2658   unsigned IndexReg   = MI->getOperand(3).getReg();
2659   unsigned CCValid    = MI->getOperand(4).getImm();
2660   unsigned CCMask     = MI->getOperand(5).getImm();
2661   DebugLoc DL         = MI->getDebugLoc();
2662
2663   StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp);
2664
2665   // Use STOCOpcode if possible.  We could use different store patterns in
2666   // order to avoid matching the index register, but the performance trade-offs
2667   // might be more complicated in that case.
2668   if (STOCOpcode && !IndexReg && TM.getSubtargetImpl()->hasLoadStoreOnCond()) {
2669     if (Invert)
2670       CCMask ^= CCValid;
2671     BuildMI(*MBB, MI, DL, TII->get(STOCOpcode))
2672       .addReg(SrcReg).addOperand(Base).addImm(Disp)
2673       .addImm(CCValid).addImm(CCMask);
2674     MI->eraseFromParent();
2675     return MBB;
2676   }
2677
2678   // Get the condition needed to branch around the store.
2679   if (!Invert)
2680     CCMask ^= CCValid;
2681
2682   MachineBasicBlock *StartMBB = MBB;
2683   MachineBasicBlock *JoinMBB  = splitBlockBefore(MI, MBB);
2684   MachineBasicBlock *FalseMBB = emitBlockAfter(StartMBB);
2685
2686   //  StartMBB:
2687   //   BRC CCMask, JoinMBB
2688   //   # fallthrough to FalseMBB
2689   MBB = StartMBB;
2690   BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2691     .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
2692   MBB->addSuccessor(JoinMBB);
2693   MBB->addSuccessor(FalseMBB);
2694
2695   //  FalseMBB:
2696   //   store %SrcReg, %Disp(%Index,%Base)
2697   //   # fallthrough to JoinMBB
2698   MBB = FalseMBB;
2699   BuildMI(MBB, DL, TII->get(StoreOpcode))
2700     .addReg(SrcReg).addOperand(Base).addImm(Disp).addReg(IndexReg);
2701   MBB->addSuccessor(JoinMBB);
2702
2703   MI->eraseFromParent();
2704   return JoinMBB;
2705 }
2706
2707 // Implement EmitInstrWithCustomInserter for pseudo ATOMIC_LOAD{,W}_*
2708 // or ATOMIC_SWAP{,W} instruction MI.  BinOpcode is the instruction that
2709 // performs the binary operation elided by "*", or 0 for ATOMIC_SWAP{,W}.
2710 // BitSize is the width of the field in bits, or 0 if this is a partword
2711 // ATOMIC_LOADW_* or ATOMIC_SWAPW instruction, in which case the bitsize
2712 // is one of the operands.  Invert says whether the field should be
2713 // inverted after performing BinOpcode (e.g. for NAND).
2714 MachineBasicBlock *
2715 SystemZTargetLowering::emitAtomicLoadBinary(MachineInstr *MI,
2716                                             MachineBasicBlock *MBB,
2717                                             unsigned BinOpcode,
2718                                             unsigned BitSize,
2719                                             bool Invert) const {
2720   const SystemZInstrInfo *TII = TM.getInstrInfo();
2721   MachineFunction &MF = *MBB->getParent();
2722   MachineRegisterInfo &MRI = MF.getRegInfo();
2723   bool IsSubWord = (BitSize < 32);
2724
2725   // Extract the operands.  Base can be a register or a frame index.
2726   // Src2 can be a register or immediate.
2727   unsigned Dest        = MI->getOperand(0).getReg();
2728   MachineOperand Base  = earlyUseOperand(MI->getOperand(1));
2729   int64_t Disp         = MI->getOperand(2).getImm();
2730   MachineOperand Src2  = earlyUseOperand(MI->getOperand(3));
2731   unsigned BitShift    = (IsSubWord ? MI->getOperand(4).getReg() : 0);
2732   unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
2733   DebugLoc DL          = MI->getDebugLoc();
2734   if (IsSubWord)
2735     BitSize = MI->getOperand(6).getImm();
2736
2737   // Subword operations use 32-bit registers.
2738   const TargetRegisterClass *RC = (BitSize <= 32 ?
2739                                    &SystemZ::GR32BitRegClass :
2740                                    &SystemZ::GR64BitRegClass);
2741   unsigned LOpcode  = BitSize <= 32 ? SystemZ::L  : SystemZ::LG;
2742   unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
2743
2744   // Get the right opcodes for the displacement.
2745   LOpcode  = TII->getOpcodeForOffset(LOpcode,  Disp);
2746   CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
2747   assert(LOpcode && CSOpcode && "Displacement out of range");
2748
2749   // Create virtual registers for temporary results.
2750   unsigned OrigVal       = MRI.createVirtualRegister(RC);
2751   unsigned OldVal        = MRI.createVirtualRegister(RC);
2752   unsigned NewVal        = (BinOpcode || IsSubWord ?
2753                             MRI.createVirtualRegister(RC) : Src2.getReg());
2754   unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
2755   unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
2756
2757   // Insert a basic block for the main loop.
2758   MachineBasicBlock *StartMBB = MBB;
2759   MachineBasicBlock *DoneMBB  = splitBlockBefore(MI, MBB);
2760   MachineBasicBlock *LoopMBB  = emitBlockAfter(StartMBB);
2761
2762   //  StartMBB:
2763   //   ...
2764   //   %OrigVal = L Disp(%Base)
2765   //   # fall through to LoopMMB
2766   MBB = StartMBB;
2767   BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
2768     .addOperand(Base).addImm(Disp).addReg(0);
2769   MBB->addSuccessor(LoopMBB);
2770
2771   //  LoopMBB:
2772   //   %OldVal        = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ]
2773   //   %RotatedOldVal = RLL %OldVal, 0(%BitShift)
2774   //   %RotatedNewVal = OP %RotatedOldVal, %Src2
2775   //   %NewVal        = RLL %RotatedNewVal, 0(%NegBitShift)
2776   //   %Dest          = CS %OldVal, %NewVal, Disp(%Base)
2777   //   JNE LoopMBB
2778   //   # fall through to DoneMMB
2779   MBB = LoopMBB;
2780   BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
2781     .addReg(OrigVal).addMBB(StartMBB)
2782     .addReg(Dest).addMBB(LoopMBB);
2783   if (IsSubWord)
2784     BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
2785       .addReg(OldVal).addReg(BitShift).addImm(0);
2786   if (Invert) {
2787     // Perform the operation normally and then invert every bit of the field.
2788     unsigned Tmp = MRI.createVirtualRegister(RC);
2789     BuildMI(MBB, DL, TII->get(BinOpcode), Tmp)
2790       .addReg(RotatedOldVal).addOperand(Src2);
2791     if (BitSize < 32)
2792       // XILF with the upper BitSize bits set.
2793       BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal)
2794         .addReg(Tmp).addImm(uint32_t(~0 << (32 - BitSize)));
2795     else if (BitSize == 32)
2796       // XILF with every bit set.
2797       BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal)
2798         .addReg(Tmp).addImm(~uint32_t(0));
2799     else {
2800       // Use LCGR and add -1 to the result, which is more compact than
2801       // an XILF, XILH pair.
2802       unsigned Tmp2 = MRI.createVirtualRegister(RC);
2803       BuildMI(MBB, DL, TII->get(SystemZ::LCGR), Tmp2).addReg(Tmp);
2804       BuildMI(MBB, DL, TII->get(SystemZ::AGHI), RotatedNewVal)
2805         .addReg(Tmp2).addImm(-1);
2806     }
2807   } else if (BinOpcode)
2808     // A simply binary operation.
2809     BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal)
2810       .addReg(RotatedOldVal).addOperand(Src2);
2811   else if (IsSubWord)
2812     // Use RISBG to rotate Src2 into position and use it to replace the
2813     // field in RotatedOldVal.
2814     BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal)
2815       .addReg(RotatedOldVal).addReg(Src2.getReg())
2816       .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize);
2817   if (IsSubWord)
2818     BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
2819       .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
2820   BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
2821     .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
2822   BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2823     .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
2824   MBB->addSuccessor(LoopMBB);
2825   MBB->addSuccessor(DoneMBB);
2826
2827   MI->eraseFromParent();
2828   return DoneMBB;
2829 }
2830
2831 // Implement EmitInstrWithCustomInserter for pseudo
2832 // ATOMIC_LOAD{,W}_{,U}{MIN,MAX} instruction MI.  CompareOpcode is the
2833 // instruction that should be used to compare the current field with the
2834 // minimum or maximum value.  KeepOldMask is the BRC condition-code mask
2835 // for when the current field should be kept.  BitSize is the width of
2836 // the field in bits, or 0 if this is a partword ATOMIC_LOADW_* instruction.
2837 MachineBasicBlock *
2838 SystemZTargetLowering::emitAtomicLoadMinMax(MachineInstr *MI,
2839                                             MachineBasicBlock *MBB,
2840                                             unsigned CompareOpcode,
2841                                             unsigned KeepOldMask,
2842                                             unsigned BitSize) const {
2843   const SystemZInstrInfo *TII = TM.getInstrInfo();
2844   MachineFunction &MF = *MBB->getParent();
2845   MachineRegisterInfo &MRI = MF.getRegInfo();
2846   bool IsSubWord = (BitSize < 32);
2847
2848   // Extract the operands.  Base can be a register or a frame index.
2849   unsigned Dest        = MI->getOperand(0).getReg();
2850   MachineOperand Base  = earlyUseOperand(MI->getOperand(1));
2851   int64_t  Disp        = MI->getOperand(2).getImm();
2852   unsigned Src2        = MI->getOperand(3).getReg();
2853   unsigned BitShift    = (IsSubWord ? MI->getOperand(4).getReg() : 0);
2854   unsigned NegBitShift = (IsSubWord ? MI->getOperand(5).getReg() : 0);
2855   DebugLoc DL          = MI->getDebugLoc();
2856   if (IsSubWord)
2857     BitSize = MI->getOperand(6).getImm();
2858
2859   // Subword operations use 32-bit registers.
2860   const TargetRegisterClass *RC = (BitSize <= 32 ?
2861                                    &SystemZ::GR32BitRegClass :
2862                                    &SystemZ::GR64BitRegClass);
2863   unsigned LOpcode  = BitSize <= 32 ? SystemZ::L  : SystemZ::LG;
2864   unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
2865
2866   // Get the right opcodes for the displacement.
2867   LOpcode  = TII->getOpcodeForOffset(LOpcode,  Disp);
2868   CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
2869   assert(LOpcode && CSOpcode && "Displacement out of range");
2870
2871   // Create virtual registers for temporary results.
2872   unsigned OrigVal       = MRI.createVirtualRegister(RC);
2873   unsigned OldVal        = MRI.createVirtualRegister(RC);
2874   unsigned NewVal        = MRI.createVirtualRegister(RC);
2875   unsigned RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
2876   unsigned RotatedAltVal = (IsSubWord ? MRI.createVirtualRegister(RC) : Src2);
2877   unsigned RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
2878
2879   // Insert 3 basic blocks for the loop.
2880   MachineBasicBlock *StartMBB  = MBB;
2881   MachineBasicBlock *DoneMBB   = splitBlockBefore(MI, MBB);
2882   MachineBasicBlock *LoopMBB   = emitBlockAfter(StartMBB);
2883   MachineBasicBlock *UseAltMBB = emitBlockAfter(LoopMBB);
2884   MachineBasicBlock *UpdateMBB = emitBlockAfter(UseAltMBB);
2885
2886   //  StartMBB:
2887   //   ...
2888   //   %OrigVal     = L Disp(%Base)
2889   //   # fall through to LoopMMB
2890   MBB = StartMBB;
2891   BuildMI(MBB, DL, TII->get(LOpcode), OrigVal)
2892     .addOperand(Base).addImm(Disp).addReg(0);
2893   MBB->addSuccessor(LoopMBB);
2894
2895   //  LoopMBB:
2896   //   %OldVal        = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ]
2897   //   %RotatedOldVal = RLL %OldVal, 0(%BitShift)
2898   //   CompareOpcode %RotatedOldVal, %Src2
2899   //   BRC KeepOldMask, UpdateMBB
2900   MBB = LoopMBB;
2901   BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
2902     .addReg(OrigVal).addMBB(StartMBB)
2903     .addReg(Dest).addMBB(UpdateMBB);
2904   if (IsSubWord)
2905     BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
2906       .addReg(OldVal).addReg(BitShift).addImm(0);
2907   BuildMI(MBB, DL, TII->get(CompareOpcode))
2908     .addReg(RotatedOldVal).addReg(Src2);
2909   BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2910     .addImm(SystemZ::CCMASK_ICMP).addImm(KeepOldMask).addMBB(UpdateMBB);
2911   MBB->addSuccessor(UpdateMBB);
2912   MBB->addSuccessor(UseAltMBB);
2913
2914   //  UseAltMBB:
2915   //   %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0
2916   //   # fall through to UpdateMMB
2917   MBB = UseAltMBB;
2918   if (IsSubWord)
2919     BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal)
2920       .addReg(RotatedOldVal).addReg(Src2)
2921       .addImm(32).addImm(31 + BitSize).addImm(0);
2922   MBB->addSuccessor(UpdateMBB);
2923
2924   //  UpdateMBB:
2925   //   %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ],
2926   //                        [ %RotatedAltVal, UseAltMBB ]
2927   //   %NewVal        = RLL %RotatedNewVal, 0(%NegBitShift)
2928   //   %Dest          = CS %OldVal, %NewVal, Disp(%Base)
2929   //   JNE LoopMBB
2930   //   # fall through to DoneMMB
2931   MBB = UpdateMBB;
2932   BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal)
2933     .addReg(RotatedOldVal).addMBB(LoopMBB)
2934     .addReg(RotatedAltVal).addMBB(UseAltMBB);
2935   if (IsSubWord)
2936     BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
2937       .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
2938   BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
2939     .addReg(OldVal).addReg(NewVal).addOperand(Base).addImm(Disp);
2940   BuildMI(MBB, DL, TII->get(SystemZ::BRC))
2941     .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
2942   MBB->addSuccessor(LoopMBB);
2943   MBB->addSuccessor(DoneMBB);
2944
2945   MI->eraseFromParent();
2946   return DoneMBB;
2947 }
2948
2949 // Implement EmitInstrWithCustomInserter for pseudo ATOMIC_CMP_SWAPW
2950 // instruction MI.
2951 MachineBasicBlock *
2952 SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr *MI,
2953                                           MachineBasicBlock *MBB) const {
2954   const SystemZInstrInfo *TII = TM.getInstrInfo();
2955   MachineFunction &MF = *MBB->getParent();
2956   MachineRegisterInfo &MRI = MF.getRegInfo();
2957
2958   // Extract the operands.  Base can be a register or a frame index.
2959   unsigned Dest        = MI->getOperand(0).getReg();
2960   MachineOperand Base  = earlyUseOperand(MI->getOperand(1));
2961   int64_t  Disp        = MI->getOperand(2).getImm();
2962   unsigned OrigCmpVal  = MI->getOperand(3).getReg();
2963   unsigned OrigSwapVal = MI->getOperand(4).getReg();
2964   unsigned BitShift    = MI->getOperand(5).getReg();
2965   unsigned NegBitShift = MI->getOperand(6).getReg();
2966   int64_t  BitSize     = MI->getOperand(7).getImm();
2967   DebugLoc DL          = MI->getDebugLoc();
2968
2969   const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass;
2970
2971   // Get the right opcodes for the displacement.
2972   unsigned LOpcode  = TII->getOpcodeForOffset(SystemZ::L,  Disp);
2973   unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp);
2974   assert(LOpcode && CSOpcode && "Displacement out of range");
2975
2976   // Create virtual registers for temporary results.
2977   unsigned OrigOldVal   = MRI.createVirtualRegister(RC);
2978   unsigned OldVal       = MRI.createVirtualRegister(RC);
2979   unsigned CmpVal       = MRI.createVirtualRegister(RC);
2980   unsigned SwapVal      = MRI.createVirtualRegister(RC);
2981   unsigned StoreVal     = MRI.createVirtualRegister(RC);
2982   unsigned RetryOldVal  = MRI.createVirtualRegister(RC);
2983   unsigned RetryCmpVal  = MRI.createVirtualRegister(RC);
2984   unsigned RetrySwapVal = MRI.createVirtualRegister(RC);
2985
2986   // Insert 2 basic blocks for the loop.
2987   MachineBasicBlock *StartMBB = MBB;
2988   MachineBasicBlock *DoneMBB  = splitBlockBefore(MI, MBB);
2989   MachineBasicBlock *LoopMBB  = emitBlockAfter(StartMBB);
2990   MachineBasicBlock *SetMBB   = emitBlockAfter(LoopMBB);
2991
2992   //  StartMBB:
2993   //   ...
2994   //   %OrigOldVal     = L Disp(%Base)
2995   //   # fall through to LoopMMB
2996   MBB = StartMBB;
2997   BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal)
2998     .addOperand(Base).addImm(Disp).addReg(0);
2999   MBB->addSuccessor(LoopMBB);
3000
3001   //  LoopMBB:
3002   //   %OldVal        = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ]
3003   //   %CmpVal        = phi [ %OrigCmpVal, EntryBB ], [ %RetryCmpVal, SetMBB ]
3004   //   %SwapVal       = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ]
3005   //   %Dest          = RLL %OldVal, BitSize(%BitShift)
3006   //                      ^^ The low BitSize bits contain the field
3007   //                         of interest.
3008   //   %RetryCmpVal   = RISBG32 %CmpVal, %Dest, 32, 63-BitSize, 0
3009   //                      ^^ Replace the upper 32-BitSize bits of the
3010   //                         comparison value with those that we loaded,
3011   //                         so that we can use a full word comparison.
3012   //   CR %Dest, %RetryCmpVal
3013   //   JNE DoneMBB
3014   //   # Fall through to SetMBB
3015   MBB = LoopMBB;
3016   BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
3017     .addReg(OrigOldVal).addMBB(StartMBB)
3018     .addReg(RetryOldVal).addMBB(SetMBB);
3019   BuildMI(MBB, DL, TII->get(SystemZ::PHI), CmpVal)
3020     .addReg(OrigCmpVal).addMBB(StartMBB)
3021     .addReg(RetryCmpVal).addMBB(SetMBB);
3022   BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal)
3023     .addReg(OrigSwapVal).addMBB(StartMBB)
3024     .addReg(RetrySwapVal).addMBB(SetMBB);
3025   BuildMI(MBB, DL, TII->get(SystemZ::RLL), Dest)
3026     .addReg(OldVal).addReg(BitShift).addImm(BitSize);
3027   BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetryCmpVal)
3028     .addReg(CmpVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
3029   BuildMI(MBB, DL, TII->get(SystemZ::CR))
3030     .addReg(Dest).addReg(RetryCmpVal);
3031   BuildMI(MBB, DL, TII->get(SystemZ::BRC))
3032     .addImm(SystemZ::CCMASK_ICMP)
3033     .addImm(SystemZ::CCMASK_CMP_NE).addMBB(DoneMBB);
3034   MBB->addSuccessor(DoneMBB);
3035   MBB->addSuccessor(SetMBB);
3036
3037   //  SetMBB:
3038   //   %RetrySwapVal = RISBG32 %SwapVal, %Dest, 32, 63-BitSize, 0
3039   //                      ^^ Replace the upper 32-BitSize bits of the new
3040   //                         value with those that we loaded.
3041   //   %StoreVal    = RLL %RetrySwapVal, -BitSize(%NegBitShift)
3042   //                      ^^ Rotate the new field to its proper position.
3043   //   %RetryOldVal = CS %Dest, %StoreVal, Disp(%Base)
3044   //   JNE LoopMBB
3045   //   # fall through to ExitMMB
3046   MBB = SetMBB;
3047   BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal)
3048     .addReg(SwapVal).addReg(Dest).addImm(32).addImm(63 - BitSize).addImm(0);
3049   BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal)
3050     .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize);
3051   BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal)
3052     .addReg(OldVal).addReg(StoreVal).addOperand(Base).addImm(Disp);
3053   BuildMI(MBB, DL, TII->get(SystemZ::BRC))
3054     .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
3055   MBB->addSuccessor(LoopMBB);
3056   MBB->addSuccessor(DoneMBB);
3057
3058   MI->eraseFromParent();
3059   return DoneMBB;
3060 }
3061
3062 // Emit an extension from a GR32 or GR64 to a GR128.  ClearEven is true
3063 // if the high register of the GR128 value must be cleared or false if
3064 // it's "don't care".  SubReg is subreg_l32 when extending a GR32
3065 // and subreg_l64 when extending a GR64.
3066 MachineBasicBlock *
3067 SystemZTargetLowering::emitExt128(MachineInstr *MI,
3068                                   MachineBasicBlock *MBB,
3069                                   bool ClearEven, unsigned SubReg) const {
3070   const SystemZInstrInfo *TII = TM.getInstrInfo();
3071   MachineFunction &MF = *MBB->getParent();
3072   MachineRegisterInfo &MRI = MF.getRegInfo();
3073   DebugLoc DL = MI->getDebugLoc();
3074
3075   unsigned Dest  = MI->getOperand(0).getReg();
3076   unsigned Src   = MI->getOperand(1).getReg();
3077   unsigned In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
3078
3079   BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128);
3080   if (ClearEven) {
3081     unsigned NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
3082     unsigned Zero64   = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass);
3083
3084     BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64)
3085       .addImm(0);
3086     BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128)
3087       .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_h64);
3088     In128 = NewIn128;
3089   }
3090   BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest)
3091     .addReg(In128).addReg(Src).addImm(SubReg);
3092
3093   MI->eraseFromParent();
3094   return MBB;
3095 }
3096
3097 MachineBasicBlock *
3098 SystemZTargetLowering::emitMemMemWrapper(MachineInstr *MI,
3099                                          MachineBasicBlock *MBB,
3100                                          unsigned Opcode) const {
3101   const SystemZInstrInfo *TII = TM.getInstrInfo();
3102   MachineFunction &MF = *MBB->getParent();
3103   MachineRegisterInfo &MRI = MF.getRegInfo();
3104   DebugLoc DL = MI->getDebugLoc();
3105
3106   MachineOperand DestBase = earlyUseOperand(MI->getOperand(0));
3107   uint64_t       DestDisp = MI->getOperand(1).getImm();
3108   MachineOperand SrcBase  = earlyUseOperand(MI->getOperand(2));
3109   uint64_t       SrcDisp  = MI->getOperand(3).getImm();
3110   uint64_t       Length   = MI->getOperand(4).getImm();
3111
3112   // When generating more than one CLC, all but the last will need to
3113   // branch to the end when a difference is found.
3114   MachineBasicBlock *EndMBB = (Length > 256 && Opcode == SystemZ::CLC ?
3115                                splitBlockAfter(MI, MBB) : 0);
3116
3117   // Check for the loop form, in which operand 5 is the trip count.
3118   if (MI->getNumExplicitOperands() > 5) {
3119     bool HaveSingleBase = DestBase.isIdenticalTo(SrcBase);
3120
3121     uint64_t StartCountReg = MI->getOperand(5).getReg();
3122     uint64_t StartSrcReg   = forceReg(MI, SrcBase, TII);
3123     uint64_t StartDestReg  = (HaveSingleBase ? StartSrcReg :
3124                               forceReg(MI, DestBase, TII));
3125
3126     const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass;
3127     uint64_t ThisSrcReg  = MRI.createVirtualRegister(RC);
3128     uint64_t ThisDestReg = (HaveSingleBase ? ThisSrcReg :
3129                             MRI.createVirtualRegister(RC));
3130     uint64_t NextSrcReg  = MRI.createVirtualRegister(RC);
3131     uint64_t NextDestReg = (HaveSingleBase ? NextSrcReg :
3132                             MRI.createVirtualRegister(RC));
3133
3134     RC = &SystemZ::GR64BitRegClass;
3135     uint64_t ThisCountReg = MRI.createVirtualRegister(RC);
3136     uint64_t NextCountReg = MRI.createVirtualRegister(RC);
3137
3138     MachineBasicBlock *StartMBB = MBB;
3139     MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
3140     MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
3141     MachineBasicBlock *NextMBB = (EndMBB ? emitBlockAfter(LoopMBB) : LoopMBB);
3142
3143     //  StartMBB:
3144     //   # fall through to LoopMMB
3145     MBB->addSuccessor(LoopMBB);
3146
3147     //  LoopMBB:
3148     //   %ThisDestReg = phi [ %StartDestReg, StartMBB ],
3149     //                      [ %NextDestReg, NextMBB ]
3150     //   %ThisSrcReg = phi [ %StartSrcReg, StartMBB ],
3151     //                     [ %NextSrcReg, NextMBB ]
3152     //   %ThisCountReg = phi [ %StartCountReg, StartMBB ],
3153     //                       [ %NextCountReg, NextMBB ]
3154     //   ( PFD 2, 768+DestDisp(%ThisDestReg) )
3155     //   Opcode DestDisp(256,%ThisDestReg), SrcDisp(%ThisSrcReg)
3156     //   ( JLH EndMBB )
3157     //
3158     // The prefetch is used only for MVC.  The JLH is used only for CLC.
3159     MBB = LoopMBB;
3160
3161     BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisDestReg)
3162       .addReg(StartDestReg).addMBB(StartMBB)
3163       .addReg(NextDestReg).addMBB(NextMBB);
3164     if (!HaveSingleBase)
3165       BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisSrcReg)
3166         .addReg(StartSrcReg).addMBB(StartMBB)
3167         .addReg(NextSrcReg).addMBB(NextMBB);
3168     BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisCountReg)
3169       .addReg(StartCountReg).addMBB(StartMBB)
3170       .addReg(NextCountReg).addMBB(NextMBB);
3171     if (Opcode == SystemZ::MVC)
3172       BuildMI(MBB, DL, TII->get(SystemZ::PFD))
3173         .addImm(SystemZ::PFD_WRITE)
3174         .addReg(ThisDestReg).addImm(DestDisp + 768).addReg(0);
3175     BuildMI(MBB, DL, TII->get(Opcode))
3176       .addReg(ThisDestReg).addImm(DestDisp).addImm(256)
3177       .addReg(ThisSrcReg).addImm(SrcDisp);
3178     if (EndMBB) {
3179       BuildMI(MBB, DL, TII->get(SystemZ::BRC))
3180         .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
3181         .addMBB(EndMBB);
3182       MBB->addSuccessor(EndMBB);
3183       MBB->addSuccessor(NextMBB);
3184     }
3185
3186     // NextMBB:
3187     //   %NextDestReg = LA 256(%ThisDestReg)
3188     //   %NextSrcReg = LA 256(%ThisSrcReg)
3189     //   %NextCountReg = AGHI %ThisCountReg, -1
3190     //   CGHI %NextCountReg, 0
3191     //   JLH LoopMBB
3192     //   # fall through to DoneMMB
3193     //
3194     // The AGHI, CGHI and JLH should be converted to BRCTG by later passes.
3195     MBB = NextMBB;
3196
3197     BuildMI(MBB, DL, TII->get(SystemZ::LA), NextDestReg)
3198       .addReg(ThisDestReg).addImm(256).addReg(0);
3199     if (!HaveSingleBase)
3200       BuildMI(MBB, DL, TII->get(SystemZ::LA), NextSrcReg)
3201         .addReg(ThisSrcReg).addImm(256).addReg(0);
3202     BuildMI(MBB, DL, TII->get(SystemZ::AGHI), NextCountReg)
3203       .addReg(ThisCountReg).addImm(-1);
3204     BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
3205       .addReg(NextCountReg).addImm(0);
3206     BuildMI(MBB, DL, TII->get(SystemZ::BRC))
3207       .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
3208       .addMBB(LoopMBB);
3209     MBB->addSuccessor(LoopMBB);
3210     MBB->addSuccessor(DoneMBB);
3211
3212     DestBase = MachineOperand::CreateReg(NextDestReg, false);
3213     SrcBase = MachineOperand::CreateReg(NextSrcReg, false);
3214     Length &= 255;
3215     MBB = DoneMBB;
3216   }
3217   // Handle any remaining bytes with straight-line code.
3218   while (Length > 0) {
3219     uint64_t ThisLength = std::min(Length, uint64_t(256));
3220     // The previous iteration might have created out-of-range displacements.
3221     // Apply them using LAY if so.
3222     if (!isUInt<12>(DestDisp)) {
3223       unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
3224       BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg)
3225         .addOperand(DestBase).addImm(DestDisp).addReg(0);
3226       DestBase = MachineOperand::CreateReg(Reg, false);
3227       DestDisp = 0;
3228     }
3229     if (!isUInt<12>(SrcDisp)) {
3230       unsigned Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
3231       BuildMI(*MBB, MI, MI->getDebugLoc(), TII->get(SystemZ::LAY), Reg)
3232         .addOperand(SrcBase).addImm(SrcDisp).addReg(0);
3233       SrcBase = MachineOperand::CreateReg(Reg, false);
3234       SrcDisp = 0;
3235     }
3236     BuildMI(*MBB, MI, DL, TII->get(Opcode))
3237       .addOperand(DestBase).addImm(DestDisp).addImm(ThisLength)
3238       .addOperand(SrcBase).addImm(SrcDisp);
3239     DestDisp += ThisLength;
3240     SrcDisp += ThisLength;
3241     Length -= ThisLength;
3242     // If there's another CLC to go, branch to the end if a difference
3243     // was found.
3244     if (EndMBB && Length > 0) {
3245       MachineBasicBlock *NextMBB = splitBlockBefore(MI, MBB);
3246       BuildMI(MBB, DL, TII->get(SystemZ::BRC))
3247         .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
3248         .addMBB(EndMBB);
3249       MBB->addSuccessor(EndMBB);
3250       MBB->addSuccessor(NextMBB);
3251       MBB = NextMBB;
3252     }
3253   }
3254   if (EndMBB) {
3255     MBB->addSuccessor(EndMBB);
3256     MBB = EndMBB;
3257     MBB->addLiveIn(SystemZ::CC);
3258   }
3259
3260   MI->eraseFromParent();
3261   return MBB;
3262 }
3263
3264 // Decompose string pseudo-instruction MI into a loop that continually performs
3265 // Opcode until CC != 3.
3266 MachineBasicBlock *
3267 SystemZTargetLowering::emitStringWrapper(MachineInstr *MI,
3268                                          MachineBasicBlock *MBB,
3269                                          unsigned Opcode) const {
3270   const SystemZInstrInfo *TII = TM.getInstrInfo();
3271   MachineFunction &MF = *MBB->getParent();
3272   MachineRegisterInfo &MRI = MF.getRegInfo();
3273   DebugLoc DL = MI->getDebugLoc();
3274
3275   uint64_t End1Reg   = MI->getOperand(0).getReg();
3276   uint64_t Start1Reg = MI->getOperand(1).getReg();
3277   uint64_t Start2Reg = MI->getOperand(2).getReg();
3278   uint64_t CharReg   = MI->getOperand(3).getReg();
3279
3280   const TargetRegisterClass *RC = &SystemZ::GR64BitRegClass;
3281   uint64_t This1Reg = MRI.createVirtualRegister(RC);
3282   uint64_t This2Reg = MRI.createVirtualRegister(RC);
3283   uint64_t End2Reg  = MRI.createVirtualRegister(RC);
3284
3285   MachineBasicBlock *StartMBB = MBB;
3286   MachineBasicBlock *DoneMBB = splitBlockBefore(MI, MBB);
3287   MachineBasicBlock *LoopMBB = emitBlockAfter(StartMBB);
3288
3289   //  StartMBB:
3290   //   # fall through to LoopMMB
3291   MBB->addSuccessor(LoopMBB);
3292
3293   //  LoopMBB:
3294   //   %This1Reg = phi [ %Start1Reg, StartMBB ], [ %End1Reg, LoopMBB ]
3295   //   %This2Reg = phi [ %Start2Reg, StartMBB ], [ %End2Reg, LoopMBB ]
3296   //   R0L = %CharReg
3297   //   %End1Reg, %End2Reg = CLST %This1Reg, %This2Reg -- uses R0L
3298   //   JO LoopMBB
3299   //   # fall through to DoneMMB
3300   //
3301   // The load of R0L can be hoisted by post-RA LICM.
3302   MBB = LoopMBB;
3303
3304   BuildMI(MBB, DL, TII->get(SystemZ::PHI), This1Reg)
3305     .addReg(Start1Reg).addMBB(StartMBB)
3306     .addReg(End1Reg).addMBB(LoopMBB);
3307   BuildMI(MBB, DL, TII->get(SystemZ::PHI), This2Reg)
3308     .addReg(Start2Reg).addMBB(StartMBB)
3309     .addReg(End2Reg).addMBB(LoopMBB);
3310   BuildMI(MBB, DL, TII->get(TargetOpcode::COPY), SystemZ::R0L).addReg(CharReg);
3311   BuildMI(MBB, DL, TII->get(Opcode))
3312     .addReg(End1Reg, RegState::Define).addReg(End2Reg, RegState::Define)
3313     .addReg(This1Reg).addReg(This2Reg);
3314   BuildMI(MBB, DL, TII->get(SystemZ::BRC))
3315     .addImm(SystemZ::CCMASK_ANY).addImm(SystemZ::CCMASK_3).addMBB(LoopMBB);
3316   MBB->addSuccessor(LoopMBB);
3317   MBB->addSuccessor(DoneMBB);
3318
3319   DoneMBB->addLiveIn(SystemZ::CC);
3320
3321   MI->eraseFromParent();
3322   return DoneMBB;
3323 }
3324
3325 MachineBasicBlock *SystemZTargetLowering::
3326 EmitInstrWithCustomInserter(MachineInstr *MI, MachineBasicBlock *MBB) const {
3327   switch (MI->getOpcode()) {
3328   case SystemZ::Select32Mux:
3329   case SystemZ::Select32:
3330   case SystemZ::SelectF32:
3331   case SystemZ::Select64:
3332   case SystemZ::SelectF64:
3333   case SystemZ::SelectF128:
3334     return emitSelect(MI, MBB);
3335
3336   case SystemZ::CondStore8Mux:
3337     return emitCondStore(MI, MBB, SystemZ::STCMux, 0, false);
3338   case SystemZ::CondStore8MuxInv:
3339     return emitCondStore(MI, MBB, SystemZ::STCMux, 0, true);
3340   case SystemZ::CondStore16Mux:
3341     return emitCondStore(MI, MBB, SystemZ::STHMux, 0, false);
3342   case SystemZ::CondStore16MuxInv:
3343     return emitCondStore(MI, MBB, SystemZ::STHMux, 0, true);
3344   case SystemZ::CondStore8:
3345     return emitCondStore(MI, MBB, SystemZ::STC, 0, false);
3346   case SystemZ::CondStore8Inv:
3347     return emitCondStore(MI, MBB, SystemZ::STC, 0, true);
3348   case SystemZ::CondStore16:
3349     return emitCondStore(MI, MBB, SystemZ::STH, 0, false);
3350   case SystemZ::CondStore16Inv:
3351     return emitCondStore(MI, MBB, SystemZ::STH, 0, true);
3352   case SystemZ::CondStore32:
3353     return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, false);
3354   case SystemZ::CondStore32Inv:
3355     return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, true);
3356   case SystemZ::CondStore64:
3357     return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, false);
3358   case SystemZ::CondStore64Inv:
3359     return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, true);
3360   case SystemZ::CondStoreF32:
3361     return emitCondStore(MI, MBB, SystemZ::STE, 0, false);
3362   case SystemZ::CondStoreF32Inv:
3363     return emitCondStore(MI, MBB, SystemZ::STE, 0, true);
3364   case SystemZ::CondStoreF64:
3365     return emitCondStore(MI, MBB, SystemZ::STD, 0, false);
3366   case SystemZ::CondStoreF64Inv:
3367     return emitCondStore(MI, MBB, SystemZ::STD, 0, true);
3368
3369   case SystemZ::AEXT128_64:
3370     return emitExt128(MI, MBB, false, SystemZ::subreg_l64);
3371   case SystemZ::ZEXT128_32:
3372     return emitExt128(MI, MBB, true, SystemZ::subreg_l32);
3373   case SystemZ::ZEXT128_64:
3374     return emitExt128(MI, MBB, true, SystemZ::subreg_l64);
3375
3376   case SystemZ::ATOMIC_SWAPW:
3377     return emitAtomicLoadBinary(MI, MBB, 0, 0);
3378   case SystemZ::ATOMIC_SWAP_32:
3379     return emitAtomicLoadBinary(MI, MBB, 0, 32);
3380   case SystemZ::ATOMIC_SWAP_64:
3381     return emitAtomicLoadBinary(MI, MBB, 0, 64);
3382
3383   case SystemZ::ATOMIC_LOADW_AR:
3384     return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 0);
3385   case SystemZ::ATOMIC_LOADW_AFI:
3386     return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 0);
3387   case SystemZ::ATOMIC_LOAD_AR:
3388     return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 32);
3389   case SystemZ::ATOMIC_LOAD_AHI:
3390     return emitAtomicLoadBinary(MI, MBB, SystemZ::AHI, 32);
3391   case SystemZ::ATOMIC_LOAD_AFI:
3392     return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 32);
3393   case SystemZ::ATOMIC_LOAD_AGR:
3394     return emitAtomicLoadBinary(MI, MBB, SystemZ::AGR, 64);
3395   case SystemZ::ATOMIC_LOAD_AGHI:
3396     return emitAtomicLoadBinary(MI, MBB, SystemZ::AGHI, 64);
3397   case SystemZ::ATOMIC_LOAD_AGFI:
3398     return emitAtomicLoadBinary(MI, MBB, SystemZ::AGFI, 64);
3399
3400   case SystemZ::ATOMIC_LOADW_SR:
3401     return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 0);
3402   case SystemZ::ATOMIC_LOAD_SR:
3403     return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 32);
3404   case SystemZ::ATOMIC_LOAD_SGR:
3405     return emitAtomicLoadBinary(MI, MBB, SystemZ::SGR, 64);
3406
3407   case SystemZ::ATOMIC_LOADW_NR:
3408     return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0);
3409   case SystemZ::ATOMIC_LOADW_NILH:
3410     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0);
3411   case SystemZ::ATOMIC_LOAD_NR:
3412     return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32);
3413   case SystemZ::ATOMIC_LOAD_NILL:
3414     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32);
3415   case SystemZ::ATOMIC_LOAD_NILH:
3416     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32);
3417   case SystemZ::ATOMIC_LOAD_NILF:
3418     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32);
3419   case SystemZ::ATOMIC_LOAD_NGR:
3420     return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64);
3421   case SystemZ::ATOMIC_LOAD_NILL64:
3422     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64);
3423   case SystemZ::ATOMIC_LOAD_NILH64:
3424     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64);
3425   case SystemZ::ATOMIC_LOAD_NIHL64:
3426     return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64);
3427   case SystemZ::ATOMIC_LOAD_NIHH64:
3428     return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64);
3429   case SystemZ::ATOMIC_LOAD_NILF64:
3430     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64);
3431   case SystemZ::ATOMIC_LOAD_NIHF64:
3432     return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64);
3433
3434   case SystemZ::ATOMIC_LOADW_OR:
3435     return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 0);
3436   case SystemZ::ATOMIC_LOADW_OILH:
3437     return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 0);
3438   case SystemZ::ATOMIC_LOAD_OR:
3439     return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 32);
3440   case SystemZ::ATOMIC_LOAD_OILL:
3441     return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL, 32);
3442   case SystemZ::ATOMIC_LOAD_OILH:
3443     return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 32);
3444   case SystemZ::ATOMIC_LOAD_OILF:
3445     return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF, 32);
3446   case SystemZ::ATOMIC_LOAD_OGR:
3447     return emitAtomicLoadBinary(MI, MBB, SystemZ::OGR, 64);
3448   case SystemZ::ATOMIC_LOAD_OILL64:
3449     return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL64, 64);
3450   case SystemZ::ATOMIC_LOAD_OILH64:
3451     return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH64, 64);
3452   case SystemZ::ATOMIC_LOAD_OIHL64:
3453     return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHL64, 64);
3454   case SystemZ::ATOMIC_LOAD_OIHH64:
3455     return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHH64, 64);
3456   case SystemZ::ATOMIC_LOAD_OILF64:
3457     return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF64, 64);
3458   case SystemZ::ATOMIC_LOAD_OIHF64:
3459     return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHF64, 64);
3460
3461   case SystemZ::ATOMIC_LOADW_XR:
3462     return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 0);
3463   case SystemZ::ATOMIC_LOADW_XILF:
3464     return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 0);
3465   case SystemZ::ATOMIC_LOAD_XR:
3466     return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 32);
3467   case SystemZ::ATOMIC_LOAD_XILF:
3468     return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 32);
3469   case SystemZ::ATOMIC_LOAD_XGR:
3470     return emitAtomicLoadBinary(MI, MBB, SystemZ::XGR, 64);
3471   case SystemZ::ATOMIC_LOAD_XILF64:
3472     return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF64, 64);
3473   case SystemZ::ATOMIC_LOAD_XIHF64:
3474     return emitAtomicLoadBinary(MI, MBB, SystemZ::XIHF64, 64);
3475
3476   case SystemZ::ATOMIC_LOADW_NRi:
3477     return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0, true);
3478   case SystemZ::ATOMIC_LOADW_NILHi:
3479     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0, true);
3480   case SystemZ::ATOMIC_LOAD_NRi:
3481     return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32, true);
3482   case SystemZ::ATOMIC_LOAD_NILLi:
3483     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32, true);
3484   case SystemZ::ATOMIC_LOAD_NILHi:
3485     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32, true);
3486   case SystemZ::ATOMIC_LOAD_NILFi:
3487     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32, true);
3488   case SystemZ::ATOMIC_LOAD_NGRi:
3489     return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64, true);
3490   case SystemZ::ATOMIC_LOAD_NILL64i:
3491     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64, true);
3492   case SystemZ::ATOMIC_LOAD_NILH64i:
3493     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64, true);
3494   case SystemZ::ATOMIC_LOAD_NIHL64i:
3495     return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64, true);
3496   case SystemZ::ATOMIC_LOAD_NIHH64i:
3497     return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64, true);
3498   case SystemZ::ATOMIC_LOAD_NILF64i:
3499     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64, true);
3500   case SystemZ::ATOMIC_LOAD_NIHF64i:
3501     return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64, true);
3502
3503   case SystemZ::ATOMIC_LOADW_MIN:
3504     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
3505                                 SystemZ::CCMASK_CMP_LE, 0);
3506   case SystemZ::ATOMIC_LOAD_MIN_32:
3507     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
3508                                 SystemZ::CCMASK_CMP_LE, 32);
3509   case SystemZ::ATOMIC_LOAD_MIN_64:
3510     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
3511                                 SystemZ::CCMASK_CMP_LE, 64);
3512
3513   case SystemZ::ATOMIC_LOADW_MAX:
3514     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
3515                                 SystemZ::CCMASK_CMP_GE, 0);
3516   case SystemZ::ATOMIC_LOAD_MAX_32:
3517     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
3518                                 SystemZ::CCMASK_CMP_GE, 32);
3519   case SystemZ::ATOMIC_LOAD_MAX_64:
3520     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
3521                                 SystemZ::CCMASK_CMP_GE, 64);
3522
3523   case SystemZ::ATOMIC_LOADW_UMIN:
3524     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
3525                                 SystemZ::CCMASK_CMP_LE, 0);
3526   case SystemZ::ATOMIC_LOAD_UMIN_32:
3527     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
3528                                 SystemZ::CCMASK_CMP_LE, 32);
3529   case SystemZ::ATOMIC_LOAD_UMIN_64:
3530     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
3531                                 SystemZ::CCMASK_CMP_LE, 64);
3532
3533   case SystemZ::ATOMIC_LOADW_UMAX:
3534     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
3535                                 SystemZ::CCMASK_CMP_GE, 0);
3536   case SystemZ::ATOMIC_LOAD_UMAX_32:
3537     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
3538                                 SystemZ::CCMASK_CMP_GE, 32);
3539   case SystemZ::ATOMIC_LOAD_UMAX_64:
3540     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
3541                                 SystemZ::CCMASK_CMP_GE, 64);
3542
3543   case SystemZ::ATOMIC_CMP_SWAPW:
3544     return emitAtomicCmpSwapW(MI, MBB);
3545   case SystemZ::MVCSequence:
3546   case SystemZ::MVCLoop:
3547     return emitMemMemWrapper(MI, MBB, SystemZ::MVC);
3548   case SystemZ::NCSequence:
3549   case SystemZ::NCLoop:
3550     return emitMemMemWrapper(MI, MBB, SystemZ::NC);
3551   case SystemZ::OCSequence:
3552   case SystemZ::OCLoop:
3553     return emitMemMemWrapper(MI, MBB, SystemZ::OC);
3554   case SystemZ::XCSequence:
3555   case SystemZ::XCLoop:
3556     return emitMemMemWrapper(MI, MBB, SystemZ::XC);
3557   case SystemZ::CLCSequence:
3558   case SystemZ::CLCLoop:
3559     return emitMemMemWrapper(MI, MBB, SystemZ::CLC);
3560   case SystemZ::CLSTLoop:
3561     return emitStringWrapper(MI, MBB, SystemZ::CLST);
3562   case SystemZ::MVSTLoop:
3563     return emitStringWrapper(MI, MBB, SystemZ::MVST);
3564   case SystemZ::SRSTLoop:
3565     return emitStringWrapper(MI, MBB, SystemZ::SRST);
3566   default:
3567     llvm_unreachable("Unexpected instr type to insert");
3568   }
3569 }