xref: /freebsd/contrib/llvm-project/llvm/lib/Target/SystemZ/SystemZISelLowering.cpp (revision a324c34037ef2e1101962fca4ad0c021253288e1)
10b57cec5SDimitry Andric //===-- SystemZISelLowering.cpp - SystemZ DAG lowering implementation -----===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements the SystemZTargetLowering class.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "SystemZISelLowering.h"
140b57cec5SDimitry Andric #include "SystemZCallingConv.h"
150b57cec5SDimitry Andric #include "SystemZConstantPoolValue.h"
160b57cec5SDimitry Andric #include "SystemZMachineFunctionInfo.h"
170b57cec5SDimitry Andric #include "SystemZTargetMachine.h"
180b57cec5SDimitry Andric #include "llvm/CodeGen/CallingConvLower.h"
190b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
200b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
210b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
220b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
23480093f4SDimitry Andric #include "llvm/IR/Intrinsics.h"
24480093f4SDimitry Andric #include "llvm/IR/IntrinsicsS390.h"
250b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
260b57cec5SDimitry Andric #include "llvm/Support/KnownBits.h"
270b57cec5SDimitry Andric #include <cctype>
28bdd1243dSDimitry Andric #include <optional>
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric using namespace llvm;
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric #define DEBUG_TYPE "systemz-lower"
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric namespace {
350b57cec5SDimitry Andric // Represents information about a comparison.
360b57cec5SDimitry Andric struct Comparison {
37480093f4SDimitry Andric   Comparison(SDValue Op0In, SDValue Op1In, SDValue ChainIn)
38480093f4SDimitry Andric     : Op0(Op0In), Op1(Op1In), Chain(ChainIn),
39480093f4SDimitry Andric       Opcode(0), ICmpType(0), CCValid(0), CCMask(0) {}
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric   // The operands to the comparison.
420b57cec5SDimitry Andric   SDValue Op0, Op1;
430b57cec5SDimitry Andric 
44480093f4SDimitry Andric   // Chain if this is a strict floating-point comparison.
45480093f4SDimitry Andric   SDValue Chain;
46480093f4SDimitry Andric 
470b57cec5SDimitry Andric   // The opcode that should be used to compare Op0 and Op1.
480b57cec5SDimitry Andric   unsigned Opcode;
490b57cec5SDimitry Andric 
500b57cec5SDimitry Andric   // A SystemZICMP value.  Only used for integer comparisons.
510b57cec5SDimitry Andric   unsigned ICmpType;
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric   // The mask of CC values that Opcode can produce.
540b57cec5SDimitry Andric   unsigned CCValid;
550b57cec5SDimitry Andric 
560b57cec5SDimitry Andric   // The mask of CC values for which the original condition is true.
570b57cec5SDimitry Andric   unsigned CCMask;
580b57cec5SDimitry Andric };
590b57cec5SDimitry Andric } // end anonymous namespace
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric // Classify VT as either 32 or 64 bit.
620b57cec5SDimitry Andric static bool is32Bit(EVT VT) {
630b57cec5SDimitry Andric   switch (VT.getSimpleVT().SimpleTy) {
640b57cec5SDimitry Andric   case MVT::i32:
650b57cec5SDimitry Andric     return true;
660b57cec5SDimitry Andric   case MVT::i64:
670b57cec5SDimitry Andric     return false;
680b57cec5SDimitry Andric   default:
690b57cec5SDimitry Andric     llvm_unreachable("Unsupported type");
700b57cec5SDimitry Andric   }
710b57cec5SDimitry Andric }
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric // Return a version of MachineOperand that can be safely used before the
740b57cec5SDimitry Andric // final use.
750b57cec5SDimitry Andric static MachineOperand earlyUseOperand(MachineOperand Op) {
760b57cec5SDimitry Andric   if (Op.isReg())
770b57cec5SDimitry Andric     Op.setIsKill(false);
780b57cec5SDimitry Andric   return Op;
790b57cec5SDimitry Andric }
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric SystemZTargetLowering::SystemZTargetLowering(const TargetMachine &TM,
820b57cec5SDimitry Andric                                              const SystemZSubtarget &STI)
830b57cec5SDimitry Andric     : TargetLowering(TM), Subtarget(STI) {
8481ad6265SDimitry Andric   MVT PtrVT = MVT::getIntegerVT(TM.getPointerSizeInBits(0));
850b57cec5SDimitry Andric 
86349cc55cSDimitry Andric   auto *Regs = STI.getSpecialRegisters();
87349cc55cSDimitry Andric 
880b57cec5SDimitry Andric   // Set up the register classes.
890b57cec5SDimitry Andric   if (Subtarget.hasHighWord())
900b57cec5SDimitry Andric     addRegisterClass(MVT::i32, &SystemZ::GRX32BitRegClass);
910b57cec5SDimitry Andric   else
920b57cec5SDimitry Andric     addRegisterClass(MVT::i32, &SystemZ::GR32BitRegClass);
930b57cec5SDimitry Andric   addRegisterClass(MVT::i64, &SystemZ::GR64BitRegClass);
945ffd83dbSDimitry Andric   if (!useSoftFloat()) {
950b57cec5SDimitry Andric     if (Subtarget.hasVector()) {
960b57cec5SDimitry Andric       addRegisterClass(MVT::f32, &SystemZ::VR32BitRegClass);
970b57cec5SDimitry Andric       addRegisterClass(MVT::f64, &SystemZ::VR64BitRegClass);
980b57cec5SDimitry Andric     } else {
990b57cec5SDimitry Andric       addRegisterClass(MVT::f32, &SystemZ::FP32BitRegClass);
1000b57cec5SDimitry Andric       addRegisterClass(MVT::f64, &SystemZ::FP64BitRegClass);
1010b57cec5SDimitry Andric     }
1020b57cec5SDimitry Andric     if (Subtarget.hasVectorEnhancements1())
1030b57cec5SDimitry Andric       addRegisterClass(MVT::f128, &SystemZ::VR128BitRegClass);
1040b57cec5SDimitry Andric     else
1050b57cec5SDimitry Andric       addRegisterClass(MVT::f128, &SystemZ::FP128BitRegClass);
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric     if (Subtarget.hasVector()) {
1080b57cec5SDimitry Andric       addRegisterClass(MVT::v16i8, &SystemZ::VR128BitRegClass);
1090b57cec5SDimitry Andric       addRegisterClass(MVT::v8i16, &SystemZ::VR128BitRegClass);
1100b57cec5SDimitry Andric       addRegisterClass(MVT::v4i32, &SystemZ::VR128BitRegClass);
1110b57cec5SDimitry Andric       addRegisterClass(MVT::v2i64, &SystemZ::VR128BitRegClass);
1120b57cec5SDimitry Andric       addRegisterClass(MVT::v4f32, &SystemZ::VR128BitRegClass);
1130b57cec5SDimitry Andric       addRegisterClass(MVT::v2f64, &SystemZ::VR128BitRegClass);
1140b57cec5SDimitry Andric     }
1155ffd83dbSDimitry Andric   }
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric   // Compute derived properties from the register classes
1180b57cec5SDimitry Andric   computeRegisterProperties(Subtarget.getRegisterInfo());
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric   // Set up special registers.
121349cc55cSDimitry Andric   setStackPointerRegisterToSaveRestore(Regs->getStackPointerRegister());
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric   // TODO: It may be better to default to latency-oriented scheduling, however
1240b57cec5SDimitry Andric   // LLVM's current latency-oriented scheduler can't handle physreg definitions
1250b57cec5SDimitry Andric   // such as SystemZ has with CC, so set this to the register-pressure
1260b57cec5SDimitry Andric   // scheduler, because it can.
1270b57cec5SDimitry Andric   setSchedulingPreference(Sched::RegPressure);
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric   setBooleanContents(ZeroOrOneBooleanContent);
1300b57cec5SDimitry Andric   setBooleanVectorContents(ZeroOrNegativeOneBooleanContent);
1310b57cec5SDimitry Andric 
1320b57cec5SDimitry Andric   // Instructions are strings of 2-byte aligned 2-byte values.
1338bcb0991SDimitry Andric   setMinFunctionAlignment(Align(2));
1340b57cec5SDimitry Andric   // For performance reasons we prefer 16-byte alignment.
1358bcb0991SDimitry Andric   setPrefFunctionAlignment(Align(16));
1360b57cec5SDimitry Andric 
1370b57cec5SDimitry Andric   // Handle operations that are handled in a similar way for all types.
1380b57cec5SDimitry Andric   for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
1390b57cec5SDimitry Andric        I <= MVT::LAST_FP_VALUETYPE;
1400b57cec5SDimitry Andric        ++I) {
1410b57cec5SDimitry Andric     MVT VT = MVT::SimpleValueType(I);
1420b57cec5SDimitry Andric     if (isTypeLegal(VT)) {
1430b57cec5SDimitry Andric       // Lower SET_CC into an IPM-based sequence.
1440b57cec5SDimitry Andric       setOperationAction(ISD::SETCC, VT, Custom);
145480093f4SDimitry Andric       setOperationAction(ISD::STRICT_FSETCC, VT, Custom);
146480093f4SDimitry Andric       setOperationAction(ISD::STRICT_FSETCCS, VT, Custom);
1470b57cec5SDimitry Andric 
1480b57cec5SDimitry Andric       // Expand SELECT(C, A, B) into SELECT_CC(X, 0, A, B, NE).
1490b57cec5SDimitry Andric       setOperationAction(ISD::SELECT, VT, Expand);
1500b57cec5SDimitry Andric 
1510b57cec5SDimitry Andric       // Lower SELECT_CC and BR_CC into separate comparisons and branches.
1520b57cec5SDimitry Andric       setOperationAction(ISD::SELECT_CC, VT, Custom);
1530b57cec5SDimitry Andric       setOperationAction(ISD::BR_CC,     VT, Custom);
1540b57cec5SDimitry Andric     }
1550b57cec5SDimitry Andric   }
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric   // Expand jump table branches as address arithmetic followed by an
1580b57cec5SDimitry Andric   // indirect jump.
1590b57cec5SDimitry Andric   setOperationAction(ISD::BR_JT, MVT::Other, Expand);
1600b57cec5SDimitry Andric 
1610b57cec5SDimitry Andric   // Expand BRCOND into a BR_CC (see above).
1620b57cec5SDimitry Andric   setOperationAction(ISD::BRCOND, MVT::Other, Expand);
1630b57cec5SDimitry Andric 
1640b57cec5SDimitry Andric   // Handle integer types.
1650b57cec5SDimitry Andric   for (unsigned I = MVT::FIRST_INTEGER_VALUETYPE;
1660b57cec5SDimitry Andric        I <= MVT::LAST_INTEGER_VALUETYPE;
1670b57cec5SDimitry Andric        ++I) {
1680b57cec5SDimitry Andric     MVT VT = MVT::SimpleValueType(I);
1690b57cec5SDimitry Andric     if (isTypeLegal(VT)) {
170e8d8bef9SDimitry Andric       setOperationAction(ISD::ABS, VT, Legal);
171e8d8bef9SDimitry Andric 
1720b57cec5SDimitry Andric       // Expand individual DIV and REMs into DIVREMs.
1730b57cec5SDimitry Andric       setOperationAction(ISD::SDIV, VT, Expand);
1740b57cec5SDimitry Andric       setOperationAction(ISD::UDIV, VT, Expand);
1750b57cec5SDimitry Andric       setOperationAction(ISD::SREM, VT, Expand);
1760b57cec5SDimitry Andric       setOperationAction(ISD::UREM, VT, Expand);
1770b57cec5SDimitry Andric       setOperationAction(ISD::SDIVREM, VT, Custom);
1780b57cec5SDimitry Andric       setOperationAction(ISD::UDIVREM, VT, Custom);
1790b57cec5SDimitry Andric 
1800b57cec5SDimitry Andric       // Support addition/subtraction with overflow.
1810b57cec5SDimitry Andric       setOperationAction(ISD::SADDO, VT, Custom);
1820b57cec5SDimitry Andric       setOperationAction(ISD::SSUBO, VT, Custom);
1830b57cec5SDimitry Andric 
1840b57cec5SDimitry Andric       // Support addition/subtraction with carry.
1850b57cec5SDimitry Andric       setOperationAction(ISD::UADDO, VT, Custom);
1860b57cec5SDimitry Andric       setOperationAction(ISD::USUBO, VT, Custom);
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric       // Support carry in as value rather than glue.
1890b57cec5SDimitry Andric       setOperationAction(ISD::ADDCARRY, VT, Custom);
1900b57cec5SDimitry Andric       setOperationAction(ISD::SUBCARRY, VT, Custom);
1910b57cec5SDimitry Andric 
1920b57cec5SDimitry Andric       // Lower ATOMIC_LOAD and ATOMIC_STORE into normal volatile loads and
1930b57cec5SDimitry Andric       // stores, putting a serialization instruction after the stores.
1940b57cec5SDimitry Andric       setOperationAction(ISD::ATOMIC_LOAD,  VT, Custom);
1950b57cec5SDimitry Andric       setOperationAction(ISD::ATOMIC_STORE, VT, Custom);
1960b57cec5SDimitry Andric 
1970b57cec5SDimitry Andric       // Lower ATOMIC_LOAD_SUB into ATOMIC_LOAD_ADD if LAA and LAAG are
1980b57cec5SDimitry Andric       // available, or if the operand is constant.
1990b57cec5SDimitry Andric       setOperationAction(ISD::ATOMIC_LOAD_SUB, VT, Custom);
2000b57cec5SDimitry Andric 
2010b57cec5SDimitry Andric       // Use POPCNT on z196 and above.
2020b57cec5SDimitry Andric       if (Subtarget.hasPopulationCount())
2030b57cec5SDimitry Andric         setOperationAction(ISD::CTPOP, VT, Custom);
2040b57cec5SDimitry Andric       else
2050b57cec5SDimitry Andric         setOperationAction(ISD::CTPOP, VT, Expand);
2060b57cec5SDimitry Andric 
2070b57cec5SDimitry Andric       // No special instructions for these.
2080b57cec5SDimitry Andric       setOperationAction(ISD::CTTZ,            VT, Expand);
2090b57cec5SDimitry Andric       setOperationAction(ISD::ROTR,            VT, Expand);
2100b57cec5SDimitry Andric 
2110b57cec5SDimitry Andric       // Use *MUL_LOHI where possible instead of MULH*.
2120b57cec5SDimitry Andric       setOperationAction(ISD::MULHS, VT, Expand);
2130b57cec5SDimitry Andric       setOperationAction(ISD::MULHU, VT, Expand);
2140b57cec5SDimitry Andric       setOperationAction(ISD::SMUL_LOHI, VT, Custom);
2150b57cec5SDimitry Andric       setOperationAction(ISD::UMUL_LOHI, VT, Custom);
2160b57cec5SDimitry Andric 
2170b57cec5SDimitry Andric       // Only z196 and above have native support for conversions to unsigned.
2180b57cec5SDimitry Andric       // On z10, promoting to i64 doesn't generate an inexact condition for
2190b57cec5SDimitry Andric       // values that are outside the i32 range but in the i64 range, so use
2200b57cec5SDimitry Andric       // the default expansion.
2210b57cec5SDimitry Andric       if (!Subtarget.hasFPExtension())
2220b57cec5SDimitry Andric         setOperationAction(ISD::FP_TO_UINT, VT, Expand);
2238bcb0991SDimitry Andric 
2248bcb0991SDimitry Andric       // Mirror those settings for STRICT_FP_TO_[SU]INT.  Note that these all
2258bcb0991SDimitry Andric       // default to Expand, so need to be modified to Legal where appropriate.
2268bcb0991SDimitry Andric       setOperationAction(ISD::STRICT_FP_TO_SINT, VT, Legal);
2278bcb0991SDimitry Andric       if (Subtarget.hasFPExtension())
2288bcb0991SDimitry Andric         setOperationAction(ISD::STRICT_FP_TO_UINT, VT, Legal);
229480093f4SDimitry Andric 
230480093f4SDimitry Andric       // And similarly for STRICT_[SU]INT_TO_FP.
231480093f4SDimitry Andric       setOperationAction(ISD::STRICT_SINT_TO_FP, VT, Legal);
232480093f4SDimitry Andric       if (Subtarget.hasFPExtension())
233480093f4SDimitry Andric         setOperationAction(ISD::STRICT_UINT_TO_FP, VT, Legal);
2340b57cec5SDimitry Andric     }
2350b57cec5SDimitry Andric   }
2360b57cec5SDimitry Andric 
2370b57cec5SDimitry Andric   // Type legalization will convert 8- and 16-bit atomic operations into
2380b57cec5SDimitry Andric   // forms that operate on i32s (but still keeping the original memory VT).
2390b57cec5SDimitry Andric   // Lower them into full i32 operations.
2400b57cec5SDimitry Andric   setOperationAction(ISD::ATOMIC_SWAP,      MVT::i32, Custom);
2410b57cec5SDimitry Andric   setOperationAction(ISD::ATOMIC_LOAD_ADD,  MVT::i32, Custom);
2420b57cec5SDimitry Andric   setOperationAction(ISD::ATOMIC_LOAD_SUB,  MVT::i32, Custom);
2430b57cec5SDimitry Andric   setOperationAction(ISD::ATOMIC_LOAD_AND,  MVT::i32, Custom);
2440b57cec5SDimitry Andric   setOperationAction(ISD::ATOMIC_LOAD_OR,   MVT::i32, Custom);
2450b57cec5SDimitry Andric   setOperationAction(ISD::ATOMIC_LOAD_XOR,  MVT::i32, Custom);
2460b57cec5SDimitry Andric   setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i32, Custom);
2470b57cec5SDimitry Andric   setOperationAction(ISD::ATOMIC_LOAD_MIN,  MVT::i32, Custom);
2480b57cec5SDimitry Andric   setOperationAction(ISD::ATOMIC_LOAD_MAX,  MVT::i32, Custom);
2490b57cec5SDimitry Andric   setOperationAction(ISD::ATOMIC_LOAD_UMIN, MVT::i32, Custom);
2500b57cec5SDimitry Andric   setOperationAction(ISD::ATOMIC_LOAD_UMAX, MVT::i32, Custom);
2510b57cec5SDimitry Andric 
2520b57cec5SDimitry Andric   // Even though i128 is not a legal type, we still need to custom lower
2530b57cec5SDimitry Andric   // the atomic operations in order to exploit SystemZ instructions.
2540b57cec5SDimitry Andric   setOperationAction(ISD::ATOMIC_LOAD,     MVT::i128, Custom);
2550b57cec5SDimitry Andric   setOperationAction(ISD::ATOMIC_STORE,    MVT::i128, Custom);
2560b57cec5SDimitry Andric 
2570b57cec5SDimitry Andric   // We can use the CC result of compare-and-swap to implement
2580b57cec5SDimitry Andric   // the "success" result of ATOMIC_CMP_SWAP_WITH_SUCCESS.
2590b57cec5SDimitry Andric   setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i32, Custom);
2600b57cec5SDimitry Andric   setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i64, Custom);
2610b57cec5SDimitry Andric   setOperationAction(ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS, MVT::i128, Custom);
2620b57cec5SDimitry Andric 
2630b57cec5SDimitry Andric   setOperationAction(ISD::ATOMIC_FENCE, MVT::Other, Custom);
2640b57cec5SDimitry Andric 
2650b57cec5SDimitry Andric   // Traps are legal, as we will convert them to "j .+2".
2660b57cec5SDimitry Andric   setOperationAction(ISD::TRAP, MVT::Other, Legal);
2670b57cec5SDimitry Andric 
2680b57cec5SDimitry Andric   // z10 has instructions for signed but not unsigned FP conversion.
2690b57cec5SDimitry Andric   // Handle unsigned 32-bit types as signed 64-bit types.
2700b57cec5SDimitry Andric   if (!Subtarget.hasFPExtension()) {
2710b57cec5SDimitry Andric     setOperationAction(ISD::UINT_TO_FP, MVT::i32, Promote);
2720b57cec5SDimitry Andric     setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
273480093f4SDimitry Andric     setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::i32, Promote);
274480093f4SDimitry Andric     setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::i64, Expand);
2750b57cec5SDimitry Andric   }
2760b57cec5SDimitry Andric 
2770b57cec5SDimitry Andric   // We have native support for a 64-bit CTLZ, via FLOGR.
2780b57cec5SDimitry Andric   setOperationAction(ISD::CTLZ, MVT::i32, Promote);
2790b57cec5SDimitry Andric   setOperationAction(ISD::CTLZ_ZERO_UNDEF, MVT::i32, Promote);
2800b57cec5SDimitry Andric   setOperationAction(ISD::CTLZ, MVT::i64, Legal);
2810b57cec5SDimitry Andric 
2828bcb0991SDimitry Andric   // On z15 we have native support for a 64-bit CTPOP.
2830b57cec5SDimitry Andric   if (Subtarget.hasMiscellaneousExtensions3()) {
2840b57cec5SDimitry Andric     setOperationAction(ISD::CTPOP, MVT::i32, Promote);
2850b57cec5SDimitry Andric     setOperationAction(ISD::CTPOP, MVT::i64, Legal);
2860b57cec5SDimitry Andric   }
2870b57cec5SDimitry Andric 
2880b57cec5SDimitry Andric   // Give LowerOperation the chance to replace 64-bit ORs with subregs.
2890b57cec5SDimitry Andric   setOperationAction(ISD::OR, MVT::i64, Custom);
2900b57cec5SDimitry Andric 
29123408297SDimitry Andric   // Expand 128 bit shifts without using a libcall.
2920b57cec5SDimitry Andric   setOperationAction(ISD::SRL_PARTS, MVT::i64, Expand);
2930b57cec5SDimitry Andric   setOperationAction(ISD::SHL_PARTS, MVT::i64, Expand);
2940b57cec5SDimitry Andric   setOperationAction(ISD::SRA_PARTS, MVT::i64, Expand);
29523408297SDimitry Andric   setLibcallName(RTLIB::SRL_I128, nullptr);
29623408297SDimitry Andric   setLibcallName(RTLIB::SHL_I128, nullptr);
29723408297SDimitry Andric   setLibcallName(RTLIB::SRA_I128, nullptr);
2980b57cec5SDimitry Andric 
299349cc55cSDimitry Andric   // Handle bitcast from fp128 to i128.
300349cc55cSDimitry Andric   setOperationAction(ISD::BITCAST, MVT::i128, Custom);
301349cc55cSDimitry Andric 
3020b57cec5SDimitry Andric   // We have native instructions for i8, i16 and i32 extensions, but not i1.
3030b57cec5SDimitry Andric   setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
3040b57cec5SDimitry Andric   for (MVT VT : MVT::integer_valuetypes()) {
3050b57cec5SDimitry Andric     setLoadExtAction(ISD::SEXTLOAD, VT, MVT::i1, Promote);
3060b57cec5SDimitry Andric     setLoadExtAction(ISD::ZEXTLOAD, VT, MVT::i1, Promote);
3070b57cec5SDimitry Andric     setLoadExtAction(ISD::EXTLOAD,  VT, MVT::i1, Promote);
3080b57cec5SDimitry Andric   }
3090b57cec5SDimitry Andric 
3100b57cec5SDimitry Andric   // Handle the various types of symbolic address.
3110b57cec5SDimitry Andric   setOperationAction(ISD::ConstantPool,     PtrVT, Custom);
3120b57cec5SDimitry Andric   setOperationAction(ISD::GlobalAddress,    PtrVT, Custom);
3130b57cec5SDimitry Andric   setOperationAction(ISD::GlobalTLSAddress, PtrVT, Custom);
3140b57cec5SDimitry Andric   setOperationAction(ISD::BlockAddress,     PtrVT, Custom);
3150b57cec5SDimitry Andric   setOperationAction(ISD::JumpTable,        PtrVT, Custom);
3160b57cec5SDimitry Andric 
3170b57cec5SDimitry Andric   // We need to handle dynamic allocations specially because of the
3180b57cec5SDimitry Andric   // 160-byte area at the bottom of the stack.
3190b57cec5SDimitry Andric   setOperationAction(ISD::DYNAMIC_STACKALLOC, PtrVT, Custom);
3200b57cec5SDimitry Andric   setOperationAction(ISD::GET_DYNAMIC_AREA_OFFSET, PtrVT, Custom);
3210b57cec5SDimitry Andric 
3220b57cec5SDimitry Andric   setOperationAction(ISD::STACKSAVE,    MVT::Other, Custom);
3230b57cec5SDimitry Andric   setOperationAction(ISD::STACKRESTORE, MVT::Other, Custom);
3240b57cec5SDimitry Andric 
3250b57cec5SDimitry Andric   // Handle prefetches with PFD or PFDRL.
3260b57cec5SDimitry Andric   setOperationAction(ISD::PREFETCH, MVT::Other, Custom);
3270b57cec5SDimitry Andric 
3288bcb0991SDimitry Andric   for (MVT VT : MVT::fixedlen_vector_valuetypes()) {
3290b57cec5SDimitry Andric     // Assume by default that all vector operations need to be expanded.
3300b57cec5SDimitry Andric     for (unsigned Opcode = 0; Opcode < ISD::BUILTIN_OP_END; ++Opcode)
3310b57cec5SDimitry Andric       if (getOperationAction(Opcode, VT) == Legal)
3320b57cec5SDimitry Andric         setOperationAction(Opcode, VT, Expand);
3330b57cec5SDimitry Andric 
3340b57cec5SDimitry Andric     // Likewise all truncating stores and extending loads.
3358bcb0991SDimitry Andric     for (MVT InnerVT : MVT::fixedlen_vector_valuetypes()) {
3360b57cec5SDimitry Andric       setTruncStoreAction(VT, InnerVT, Expand);
3370b57cec5SDimitry Andric       setLoadExtAction(ISD::SEXTLOAD, VT, InnerVT, Expand);
3380b57cec5SDimitry Andric       setLoadExtAction(ISD::ZEXTLOAD, VT, InnerVT, Expand);
3390b57cec5SDimitry Andric       setLoadExtAction(ISD::EXTLOAD, VT, InnerVT, Expand);
3400b57cec5SDimitry Andric     }
3410b57cec5SDimitry Andric 
3420b57cec5SDimitry Andric     if (isTypeLegal(VT)) {
3430b57cec5SDimitry Andric       // These operations are legal for anything that can be stored in a
3440b57cec5SDimitry Andric       // vector register, even if there is no native support for the format
3450b57cec5SDimitry Andric       // as such.  In particular, we can do these for v4f32 even though there
3460b57cec5SDimitry Andric       // are no specific instructions for that format.
3470b57cec5SDimitry Andric       setOperationAction(ISD::LOAD, VT, Legal);
3480b57cec5SDimitry Andric       setOperationAction(ISD::STORE, VT, Legal);
3490b57cec5SDimitry Andric       setOperationAction(ISD::VSELECT, VT, Legal);
3500b57cec5SDimitry Andric       setOperationAction(ISD::BITCAST, VT, Legal);
3510b57cec5SDimitry Andric       setOperationAction(ISD::UNDEF, VT, Legal);
3520b57cec5SDimitry Andric 
3530b57cec5SDimitry Andric       // Likewise, except that we need to replace the nodes with something
3540b57cec5SDimitry Andric       // more specific.
3550b57cec5SDimitry Andric       setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
3560b57cec5SDimitry Andric       setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
3570b57cec5SDimitry Andric     }
3580b57cec5SDimitry Andric   }
3590b57cec5SDimitry Andric 
3600b57cec5SDimitry Andric   // Handle integer vector types.
3618bcb0991SDimitry Andric   for (MVT VT : MVT::integer_fixedlen_vector_valuetypes()) {
3620b57cec5SDimitry Andric     if (isTypeLegal(VT)) {
3630b57cec5SDimitry Andric       // These operations have direct equivalents.
3640b57cec5SDimitry Andric       setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Legal);
3650b57cec5SDimitry Andric       setOperationAction(ISD::INSERT_VECTOR_ELT, VT, Legal);
3660b57cec5SDimitry Andric       setOperationAction(ISD::ADD, VT, Legal);
3670b57cec5SDimitry Andric       setOperationAction(ISD::SUB, VT, Legal);
3680b57cec5SDimitry Andric       if (VT != MVT::v2i64)
3690b57cec5SDimitry Andric         setOperationAction(ISD::MUL, VT, Legal);
370e8d8bef9SDimitry Andric       setOperationAction(ISD::ABS, VT, Legal);
3710b57cec5SDimitry Andric       setOperationAction(ISD::AND, VT, Legal);
3720b57cec5SDimitry Andric       setOperationAction(ISD::OR, VT, Legal);
3730b57cec5SDimitry Andric       setOperationAction(ISD::XOR, VT, Legal);
3740b57cec5SDimitry Andric       if (Subtarget.hasVectorEnhancements1())
3750b57cec5SDimitry Andric         setOperationAction(ISD::CTPOP, VT, Legal);
3760b57cec5SDimitry Andric       else
3770b57cec5SDimitry Andric         setOperationAction(ISD::CTPOP, VT, Custom);
3780b57cec5SDimitry Andric       setOperationAction(ISD::CTTZ, VT, Legal);
3790b57cec5SDimitry Andric       setOperationAction(ISD::CTLZ, VT, Legal);
3800b57cec5SDimitry Andric 
3810b57cec5SDimitry Andric       // Convert a GPR scalar to a vector by inserting it into element 0.
3820b57cec5SDimitry Andric       setOperationAction(ISD::SCALAR_TO_VECTOR, VT, Custom);
3830b57cec5SDimitry Andric 
3840b57cec5SDimitry Andric       // Use a series of unpacks for extensions.
3850b57cec5SDimitry Andric       setOperationAction(ISD::SIGN_EXTEND_VECTOR_INREG, VT, Custom);
3860b57cec5SDimitry Andric       setOperationAction(ISD::ZERO_EXTEND_VECTOR_INREG, VT, Custom);
3870b57cec5SDimitry Andric 
3880b57cec5SDimitry Andric       // Detect shifts by a scalar amount and convert them into
3890b57cec5SDimitry Andric       // V*_BY_SCALAR.
3900b57cec5SDimitry Andric       setOperationAction(ISD::SHL, VT, Custom);
3910b57cec5SDimitry Andric       setOperationAction(ISD::SRA, VT, Custom);
3920b57cec5SDimitry Andric       setOperationAction(ISD::SRL, VT, Custom);
3930b57cec5SDimitry Andric 
3940b57cec5SDimitry Andric       // At present ROTL isn't matched by DAGCombiner.  ROTR should be
3950b57cec5SDimitry Andric       // converted into ROTL.
3960b57cec5SDimitry Andric       setOperationAction(ISD::ROTL, VT, Expand);
3970b57cec5SDimitry Andric       setOperationAction(ISD::ROTR, VT, Expand);
3980b57cec5SDimitry Andric 
3990b57cec5SDimitry Andric       // Map SETCCs onto one of VCE, VCH or VCHL, swapping the operands
4000b57cec5SDimitry Andric       // and inverting the result as necessary.
4010b57cec5SDimitry Andric       setOperationAction(ISD::SETCC, VT, Custom);
402480093f4SDimitry Andric       setOperationAction(ISD::STRICT_FSETCC, VT, Custom);
403480093f4SDimitry Andric       if (Subtarget.hasVectorEnhancements1())
404480093f4SDimitry Andric         setOperationAction(ISD::STRICT_FSETCCS, VT, Custom);
4050b57cec5SDimitry Andric     }
4060b57cec5SDimitry Andric   }
4070b57cec5SDimitry Andric 
4080b57cec5SDimitry Andric   if (Subtarget.hasVector()) {
4090b57cec5SDimitry Andric     // There should be no need to check for float types other than v2f64
4100b57cec5SDimitry Andric     // since <2 x f32> isn't a legal type.
4110b57cec5SDimitry Andric     setOperationAction(ISD::FP_TO_SINT, MVT::v2i64, Legal);
4120b57cec5SDimitry Andric     setOperationAction(ISD::FP_TO_SINT, MVT::v2f64, Legal);
4130b57cec5SDimitry Andric     setOperationAction(ISD::FP_TO_UINT, MVT::v2i64, Legal);
4140b57cec5SDimitry Andric     setOperationAction(ISD::FP_TO_UINT, MVT::v2f64, Legal);
4150b57cec5SDimitry Andric     setOperationAction(ISD::SINT_TO_FP, MVT::v2i64, Legal);
4160b57cec5SDimitry Andric     setOperationAction(ISD::SINT_TO_FP, MVT::v2f64, Legal);
4170b57cec5SDimitry Andric     setOperationAction(ISD::UINT_TO_FP, MVT::v2i64, Legal);
4180b57cec5SDimitry Andric     setOperationAction(ISD::UINT_TO_FP, MVT::v2f64, Legal);
4198bcb0991SDimitry Andric 
4208bcb0991SDimitry Andric     setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::v2i64, Legal);
4218bcb0991SDimitry Andric     setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::v2f64, Legal);
4228bcb0991SDimitry Andric     setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::v2i64, Legal);
4238bcb0991SDimitry Andric     setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::v2f64, Legal);
424480093f4SDimitry Andric     setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::v2i64, Legal);
425480093f4SDimitry Andric     setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::v2f64, Legal);
426480093f4SDimitry Andric     setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::v2i64, Legal);
427480093f4SDimitry Andric     setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::v2f64, Legal);
4280b57cec5SDimitry Andric   }
4290b57cec5SDimitry Andric 
4300b57cec5SDimitry Andric   if (Subtarget.hasVectorEnhancements2()) {
4310b57cec5SDimitry Andric     setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
4320b57cec5SDimitry Andric     setOperationAction(ISD::FP_TO_SINT, MVT::v4f32, Legal);
4330b57cec5SDimitry Andric     setOperationAction(ISD::FP_TO_UINT, MVT::v4i32, Legal);
4340b57cec5SDimitry Andric     setOperationAction(ISD::FP_TO_UINT, MVT::v4f32, Legal);
4350b57cec5SDimitry Andric     setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
4360b57cec5SDimitry Andric     setOperationAction(ISD::SINT_TO_FP, MVT::v4f32, Legal);
4370b57cec5SDimitry Andric     setOperationAction(ISD::UINT_TO_FP, MVT::v4i32, Legal);
4380b57cec5SDimitry Andric     setOperationAction(ISD::UINT_TO_FP, MVT::v4f32, Legal);
4398bcb0991SDimitry Andric 
4408bcb0991SDimitry Andric     setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::v4i32, Legal);
4418bcb0991SDimitry Andric     setOperationAction(ISD::STRICT_FP_TO_SINT, MVT::v4f32, Legal);
4428bcb0991SDimitry Andric     setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::v4i32, Legal);
4438bcb0991SDimitry Andric     setOperationAction(ISD::STRICT_FP_TO_UINT, MVT::v4f32, Legal);
444480093f4SDimitry Andric     setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::v4i32, Legal);
445480093f4SDimitry Andric     setOperationAction(ISD::STRICT_SINT_TO_FP, MVT::v4f32, Legal);
446480093f4SDimitry Andric     setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::v4i32, Legal);
447480093f4SDimitry Andric     setOperationAction(ISD::STRICT_UINT_TO_FP, MVT::v4f32, Legal);
4480b57cec5SDimitry Andric   }
4490b57cec5SDimitry Andric 
4500b57cec5SDimitry Andric   // Handle floating-point types.
4510b57cec5SDimitry Andric   for (unsigned I = MVT::FIRST_FP_VALUETYPE;
4520b57cec5SDimitry Andric        I <= MVT::LAST_FP_VALUETYPE;
4530b57cec5SDimitry Andric        ++I) {
4540b57cec5SDimitry Andric     MVT VT = MVT::SimpleValueType(I);
4550b57cec5SDimitry Andric     if (isTypeLegal(VT)) {
4560b57cec5SDimitry Andric       // We can use FI for FRINT.
4570b57cec5SDimitry Andric       setOperationAction(ISD::FRINT, VT, Legal);
4580b57cec5SDimitry Andric 
4590b57cec5SDimitry Andric       // We can use the extended form of FI for other rounding operations.
4600b57cec5SDimitry Andric       if (Subtarget.hasFPExtension()) {
4610b57cec5SDimitry Andric         setOperationAction(ISD::FNEARBYINT, VT, Legal);
4620b57cec5SDimitry Andric         setOperationAction(ISD::FFLOOR, VT, Legal);
4630b57cec5SDimitry Andric         setOperationAction(ISD::FCEIL, VT, Legal);
4640b57cec5SDimitry Andric         setOperationAction(ISD::FTRUNC, VT, Legal);
4650b57cec5SDimitry Andric         setOperationAction(ISD::FROUND, VT, Legal);
4660b57cec5SDimitry Andric       }
4670b57cec5SDimitry Andric 
4680b57cec5SDimitry Andric       // No special instructions for these.
4690b57cec5SDimitry Andric       setOperationAction(ISD::FSIN, VT, Expand);
4700b57cec5SDimitry Andric       setOperationAction(ISD::FCOS, VT, Expand);
4710b57cec5SDimitry Andric       setOperationAction(ISD::FSINCOS, VT, Expand);
4720b57cec5SDimitry Andric       setOperationAction(ISD::FREM, VT, Expand);
4730b57cec5SDimitry Andric       setOperationAction(ISD::FPOW, VT, Expand);
4740b57cec5SDimitry Andric 
47581ad6265SDimitry Andric       // Special treatment.
47681ad6265SDimitry Andric       setOperationAction(ISD::IS_FPCLASS, VT, Custom);
47781ad6265SDimitry Andric 
4780b57cec5SDimitry Andric       // Handle constrained floating-point operations.
4790b57cec5SDimitry Andric       setOperationAction(ISD::STRICT_FADD, VT, Legal);
4800b57cec5SDimitry Andric       setOperationAction(ISD::STRICT_FSUB, VT, Legal);
4810b57cec5SDimitry Andric       setOperationAction(ISD::STRICT_FMUL, VT, Legal);
4820b57cec5SDimitry Andric       setOperationAction(ISD::STRICT_FDIV, VT, Legal);
4830b57cec5SDimitry Andric       setOperationAction(ISD::STRICT_FMA, VT, Legal);
4840b57cec5SDimitry Andric       setOperationAction(ISD::STRICT_FSQRT, VT, Legal);
4850b57cec5SDimitry Andric       setOperationAction(ISD::STRICT_FRINT, VT, Legal);
4860b57cec5SDimitry Andric       setOperationAction(ISD::STRICT_FP_ROUND, VT, Legal);
4870b57cec5SDimitry Andric       setOperationAction(ISD::STRICT_FP_EXTEND, VT, Legal);
4880b57cec5SDimitry Andric       if (Subtarget.hasFPExtension()) {
4890b57cec5SDimitry Andric         setOperationAction(ISD::STRICT_FNEARBYINT, VT, Legal);
4900b57cec5SDimitry Andric         setOperationAction(ISD::STRICT_FFLOOR, VT, Legal);
4910b57cec5SDimitry Andric         setOperationAction(ISD::STRICT_FCEIL, VT, Legal);
4920b57cec5SDimitry Andric         setOperationAction(ISD::STRICT_FROUND, VT, Legal);
4930b57cec5SDimitry Andric         setOperationAction(ISD::STRICT_FTRUNC, VT, Legal);
4940b57cec5SDimitry Andric       }
4950b57cec5SDimitry Andric     }
4960b57cec5SDimitry Andric   }
4970b57cec5SDimitry Andric 
4980b57cec5SDimitry Andric   // Handle floating-point vector types.
4990b57cec5SDimitry Andric   if (Subtarget.hasVector()) {
5000b57cec5SDimitry Andric     // Scalar-to-vector conversion is just a subreg.
5010b57cec5SDimitry Andric     setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4f32, Legal);
5020b57cec5SDimitry Andric     setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f64, Legal);
5030b57cec5SDimitry Andric 
5040b57cec5SDimitry Andric     // Some insertions and extractions can be done directly but others
5050b57cec5SDimitry Andric     // need to go via integers.
5060b57cec5SDimitry Andric     setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
5070b57cec5SDimitry Andric     setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
5080b57cec5SDimitry Andric     setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
5090b57cec5SDimitry Andric     setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
5100b57cec5SDimitry Andric 
5110b57cec5SDimitry Andric     // These operations have direct equivalents.
5120b57cec5SDimitry Andric     setOperationAction(ISD::FADD, MVT::v2f64, Legal);
5130b57cec5SDimitry Andric     setOperationAction(ISD::FNEG, MVT::v2f64, Legal);
5140b57cec5SDimitry Andric     setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
5150b57cec5SDimitry Andric     setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
5160b57cec5SDimitry Andric     setOperationAction(ISD::FMA, MVT::v2f64, Legal);
5170b57cec5SDimitry Andric     setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
5180b57cec5SDimitry Andric     setOperationAction(ISD::FABS, MVT::v2f64, Legal);
5190b57cec5SDimitry Andric     setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
5200b57cec5SDimitry Andric     setOperationAction(ISD::FRINT, MVT::v2f64, Legal);
5210b57cec5SDimitry Andric     setOperationAction(ISD::FNEARBYINT, MVT::v2f64, Legal);
5220b57cec5SDimitry Andric     setOperationAction(ISD::FFLOOR, MVT::v2f64, Legal);
5230b57cec5SDimitry Andric     setOperationAction(ISD::FCEIL, MVT::v2f64, Legal);
5240b57cec5SDimitry Andric     setOperationAction(ISD::FTRUNC, MVT::v2f64, Legal);
5250b57cec5SDimitry Andric     setOperationAction(ISD::FROUND, MVT::v2f64, Legal);
5260b57cec5SDimitry Andric 
5270b57cec5SDimitry Andric     // Handle constrained floating-point operations.
5280b57cec5SDimitry Andric     setOperationAction(ISD::STRICT_FADD, MVT::v2f64, Legal);
5290b57cec5SDimitry Andric     setOperationAction(ISD::STRICT_FSUB, MVT::v2f64, Legal);
5300b57cec5SDimitry Andric     setOperationAction(ISD::STRICT_FMUL, MVT::v2f64, Legal);
5310b57cec5SDimitry Andric     setOperationAction(ISD::STRICT_FMA, MVT::v2f64, Legal);
5320b57cec5SDimitry Andric     setOperationAction(ISD::STRICT_FDIV, MVT::v2f64, Legal);
5330b57cec5SDimitry Andric     setOperationAction(ISD::STRICT_FSQRT, MVT::v2f64, Legal);
5340b57cec5SDimitry Andric     setOperationAction(ISD::STRICT_FRINT, MVT::v2f64, Legal);
5350b57cec5SDimitry Andric     setOperationAction(ISD::STRICT_FNEARBYINT, MVT::v2f64, Legal);
5360b57cec5SDimitry Andric     setOperationAction(ISD::STRICT_FFLOOR, MVT::v2f64, Legal);
5370b57cec5SDimitry Andric     setOperationAction(ISD::STRICT_FCEIL, MVT::v2f64, Legal);
5380b57cec5SDimitry Andric     setOperationAction(ISD::STRICT_FTRUNC, MVT::v2f64, Legal);
5390b57cec5SDimitry Andric     setOperationAction(ISD::STRICT_FROUND, MVT::v2f64, Legal);
5400b57cec5SDimitry Andric   }
5410b57cec5SDimitry Andric 
5420b57cec5SDimitry Andric   // The vector enhancements facility 1 has instructions for these.
5430b57cec5SDimitry Andric   if (Subtarget.hasVectorEnhancements1()) {
5440b57cec5SDimitry Andric     setOperationAction(ISD::FADD, MVT::v4f32, Legal);
5450b57cec5SDimitry Andric     setOperationAction(ISD::FNEG, MVT::v4f32, Legal);
5460b57cec5SDimitry Andric     setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
5470b57cec5SDimitry Andric     setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
5480b57cec5SDimitry Andric     setOperationAction(ISD::FMA, MVT::v4f32, Legal);
5490b57cec5SDimitry Andric     setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
5500b57cec5SDimitry Andric     setOperationAction(ISD::FABS, MVT::v4f32, Legal);
5510b57cec5SDimitry Andric     setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
5520b57cec5SDimitry Andric     setOperationAction(ISD::FRINT, MVT::v4f32, Legal);
5530b57cec5SDimitry Andric     setOperationAction(ISD::FNEARBYINT, MVT::v4f32, Legal);
5540b57cec5SDimitry Andric     setOperationAction(ISD::FFLOOR, MVT::v4f32, Legal);
5550b57cec5SDimitry Andric     setOperationAction(ISD::FCEIL, MVT::v4f32, Legal);
5560b57cec5SDimitry Andric     setOperationAction(ISD::FTRUNC, MVT::v4f32, Legal);
5570b57cec5SDimitry Andric     setOperationAction(ISD::FROUND, MVT::v4f32, Legal);
5580b57cec5SDimitry Andric 
5590b57cec5SDimitry Andric     setOperationAction(ISD::FMAXNUM, MVT::f64, Legal);
5600b57cec5SDimitry Andric     setOperationAction(ISD::FMAXIMUM, MVT::f64, Legal);
5610b57cec5SDimitry Andric     setOperationAction(ISD::FMINNUM, MVT::f64, Legal);
5620b57cec5SDimitry Andric     setOperationAction(ISD::FMINIMUM, MVT::f64, Legal);
5630b57cec5SDimitry Andric 
5640b57cec5SDimitry Andric     setOperationAction(ISD::FMAXNUM, MVT::v2f64, Legal);
5650b57cec5SDimitry Andric     setOperationAction(ISD::FMAXIMUM, MVT::v2f64, Legal);
5660b57cec5SDimitry Andric     setOperationAction(ISD::FMINNUM, MVT::v2f64, Legal);
5670b57cec5SDimitry Andric     setOperationAction(ISD::FMINIMUM, MVT::v2f64, Legal);
5680b57cec5SDimitry Andric 
5690b57cec5SDimitry Andric     setOperationAction(ISD::FMAXNUM, MVT::f32, Legal);
5700b57cec5SDimitry Andric     setOperationAction(ISD::FMAXIMUM, MVT::f32, Legal);
5710b57cec5SDimitry Andric     setOperationAction(ISD::FMINNUM, MVT::f32, Legal);
5720b57cec5SDimitry Andric     setOperationAction(ISD::FMINIMUM, MVT::f32, Legal);
5730b57cec5SDimitry Andric 
5740b57cec5SDimitry Andric     setOperationAction(ISD::FMAXNUM, MVT::v4f32, Legal);
5750b57cec5SDimitry Andric     setOperationAction(ISD::FMAXIMUM, MVT::v4f32, Legal);
5760b57cec5SDimitry Andric     setOperationAction(ISD::FMINNUM, MVT::v4f32, Legal);
5770b57cec5SDimitry Andric     setOperationAction(ISD::FMINIMUM, MVT::v4f32, Legal);
5780b57cec5SDimitry Andric 
5790b57cec5SDimitry Andric     setOperationAction(ISD::FMAXNUM, MVT::f128, Legal);
5800b57cec5SDimitry Andric     setOperationAction(ISD::FMAXIMUM, MVT::f128, Legal);
5810b57cec5SDimitry Andric     setOperationAction(ISD::FMINNUM, MVT::f128, Legal);
5820b57cec5SDimitry Andric     setOperationAction(ISD::FMINIMUM, MVT::f128, Legal);
5830b57cec5SDimitry Andric 
5840b57cec5SDimitry Andric     // Handle constrained floating-point operations.
5850b57cec5SDimitry Andric     setOperationAction(ISD::STRICT_FADD, MVT::v4f32, Legal);
5860b57cec5SDimitry Andric     setOperationAction(ISD::STRICT_FSUB, MVT::v4f32, Legal);
5870b57cec5SDimitry Andric     setOperationAction(ISD::STRICT_FMUL, MVT::v4f32, Legal);
5880b57cec5SDimitry Andric     setOperationAction(ISD::STRICT_FMA, MVT::v4f32, Legal);
5890b57cec5SDimitry Andric     setOperationAction(ISD::STRICT_FDIV, MVT::v4f32, Legal);
5900b57cec5SDimitry Andric     setOperationAction(ISD::STRICT_FSQRT, MVT::v4f32, Legal);
5910b57cec5SDimitry Andric     setOperationAction(ISD::STRICT_FRINT, MVT::v4f32, Legal);
5920b57cec5SDimitry Andric     setOperationAction(ISD::STRICT_FNEARBYINT, MVT::v4f32, Legal);
5930b57cec5SDimitry Andric     setOperationAction(ISD::STRICT_FFLOOR, MVT::v4f32, Legal);
5940b57cec5SDimitry Andric     setOperationAction(ISD::STRICT_FCEIL, MVT::v4f32, Legal);
5950b57cec5SDimitry Andric     setOperationAction(ISD::STRICT_FROUND, MVT::v4f32, Legal);
5960b57cec5SDimitry Andric     setOperationAction(ISD::STRICT_FTRUNC, MVT::v4f32, Legal);
5970b57cec5SDimitry Andric     for (auto VT : { MVT::f32, MVT::f64, MVT::f128,
5980b57cec5SDimitry Andric                      MVT::v4f32, MVT::v2f64 }) {
5990b57cec5SDimitry Andric       setOperationAction(ISD::STRICT_FMAXNUM, VT, Legal);
6000b57cec5SDimitry Andric       setOperationAction(ISD::STRICT_FMINNUM, VT, Legal);
601480093f4SDimitry Andric       setOperationAction(ISD::STRICT_FMAXIMUM, VT, Legal);
602480093f4SDimitry Andric       setOperationAction(ISD::STRICT_FMINIMUM, VT, Legal);
6030b57cec5SDimitry Andric     }
6040b57cec5SDimitry Andric   }
6050b57cec5SDimitry Andric 
606480093f4SDimitry Andric   // We only have fused f128 multiply-addition on vector registers.
607480093f4SDimitry Andric   if (!Subtarget.hasVectorEnhancements1()) {
6080b57cec5SDimitry Andric     setOperationAction(ISD::FMA, MVT::f128, Expand);
609480093f4SDimitry Andric     setOperationAction(ISD::STRICT_FMA, MVT::f128, Expand);
610480093f4SDimitry Andric   }
6110b57cec5SDimitry Andric 
6120b57cec5SDimitry Andric   // We don't have a copysign instruction on vector registers.
6130b57cec5SDimitry Andric   if (Subtarget.hasVectorEnhancements1())
6140b57cec5SDimitry Andric     setOperationAction(ISD::FCOPYSIGN, MVT::f128, Expand);
6150b57cec5SDimitry Andric 
6160b57cec5SDimitry Andric   // Needed so that we don't try to implement f128 constant loads using
6170b57cec5SDimitry Andric   // a load-and-extend of a f80 constant (in cases where the constant
6180b57cec5SDimitry Andric   // would fit in an f80).
6190b57cec5SDimitry Andric   for (MVT VT : MVT::fp_valuetypes())
6200b57cec5SDimitry Andric     setLoadExtAction(ISD::EXTLOAD, VT, MVT::f80, Expand);
6210b57cec5SDimitry Andric 
6220b57cec5SDimitry Andric   // We don't have extending load instruction on vector registers.
6230b57cec5SDimitry Andric   if (Subtarget.hasVectorEnhancements1()) {
6240b57cec5SDimitry Andric     setLoadExtAction(ISD::EXTLOAD, MVT::f128, MVT::f32, Expand);
6250b57cec5SDimitry Andric     setLoadExtAction(ISD::EXTLOAD, MVT::f128, MVT::f64, Expand);
6260b57cec5SDimitry Andric   }
6270b57cec5SDimitry Andric 
6280b57cec5SDimitry Andric   // Floating-point truncation and stores need to be done separately.
6290b57cec5SDimitry Andric   setTruncStoreAction(MVT::f64,  MVT::f32, Expand);
6300b57cec5SDimitry Andric   setTruncStoreAction(MVT::f128, MVT::f32, Expand);
6310b57cec5SDimitry Andric   setTruncStoreAction(MVT::f128, MVT::f64, Expand);
6320b57cec5SDimitry Andric 
6330b57cec5SDimitry Andric   // We have 64-bit FPR<->GPR moves, but need special handling for
6340b57cec5SDimitry Andric   // 32-bit forms.
6350b57cec5SDimitry Andric   if (!Subtarget.hasVector()) {
6360b57cec5SDimitry Andric     setOperationAction(ISD::BITCAST, MVT::i32, Custom);
6370b57cec5SDimitry Andric     setOperationAction(ISD::BITCAST, MVT::f32, Custom);
6380b57cec5SDimitry Andric   }
6390b57cec5SDimitry Andric 
6400b57cec5SDimitry Andric   // VASTART and VACOPY need to deal with the SystemZ-specific varargs
6410b57cec5SDimitry Andric   // structure, but VAEND is a no-op.
6420b57cec5SDimitry Andric   setOperationAction(ISD::VASTART, MVT::Other, Custom);
6430b57cec5SDimitry Andric   setOperationAction(ISD::VACOPY,  MVT::Other, Custom);
6440b57cec5SDimitry Andric   setOperationAction(ISD::VAEND,   MVT::Other, Expand);
6450b57cec5SDimitry Andric 
646bdd1243dSDimitry Andric   setOperationAction(ISD::GET_ROUNDING, MVT::i32, Custom);
647bdd1243dSDimitry Andric 
6480b57cec5SDimitry Andric   // Codes for which we want to perform some z-specific combinations.
64981ad6265SDimitry Andric   setTargetDAGCombine({ISD::ZERO_EXTEND,
65081ad6265SDimitry Andric                        ISD::SIGN_EXTEND,
65181ad6265SDimitry Andric                        ISD::SIGN_EXTEND_INREG,
65281ad6265SDimitry Andric                        ISD::LOAD,
65381ad6265SDimitry Andric                        ISD::STORE,
65481ad6265SDimitry Andric                        ISD::VECTOR_SHUFFLE,
65581ad6265SDimitry Andric                        ISD::EXTRACT_VECTOR_ELT,
65681ad6265SDimitry Andric                        ISD::FP_ROUND,
65781ad6265SDimitry Andric                        ISD::STRICT_FP_ROUND,
65881ad6265SDimitry Andric                        ISD::FP_EXTEND,
65981ad6265SDimitry Andric                        ISD::SINT_TO_FP,
66081ad6265SDimitry Andric                        ISD::UINT_TO_FP,
66181ad6265SDimitry Andric                        ISD::STRICT_FP_EXTEND,
66281ad6265SDimitry Andric                        ISD::BSWAP,
66381ad6265SDimitry Andric                        ISD::SDIV,
66481ad6265SDimitry Andric                        ISD::UDIV,
66581ad6265SDimitry Andric                        ISD::SREM,
66681ad6265SDimitry Andric                        ISD::UREM,
66781ad6265SDimitry Andric                        ISD::INTRINSIC_VOID,
66881ad6265SDimitry Andric                        ISD::INTRINSIC_W_CHAIN});
6690b57cec5SDimitry Andric 
6700b57cec5SDimitry Andric   // Handle intrinsics.
6710b57cec5SDimitry Andric   setOperationAction(ISD::INTRINSIC_W_CHAIN, MVT::Other, Custom);
6720b57cec5SDimitry Andric   setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
6730b57cec5SDimitry Andric 
6740b57cec5SDimitry Andric   // We want to use MVC in preference to even a single load/store pair.
67581ad6265SDimitry Andric   MaxStoresPerMemcpy = Subtarget.hasVector() ? 2 : 0;
6760b57cec5SDimitry Andric   MaxStoresPerMemcpyOptSize = 0;
6770b57cec5SDimitry Andric 
6780b57cec5SDimitry Andric   // The main memset sequence is a byte store followed by an MVC.
6790b57cec5SDimitry Andric   // Two STC or MV..I stores win over that, but the kind of fused stores
6800b57cec5SDimitry Andric   // generated by target-independent code don't when the byte value is
6810b57cec5SDimitry Andric   // variable.  E.g.  "STC <reg>;MHI <reg>,257;STH <reg>" is not better
6820b57cec5SDimitry Andric   // than "STC;MVC".  Handle the choice in target-specific code instead.
68381ad6265SDimitry Andric   MaxStoresPerMemset = Subtarget.hasVector() ? 2 : 0;
6840b57cec5SDimitry Andric   MaxStoresPerMemsetOptSize = 0;
685480093f4SDimitry Andric 
686480093f4SDimitry Andric   // Default to having -disable-strictnode-mutation on
687480093f4SDimitry Andric   IsStrictFPEnabled = true;
6880b57cec5SDimitry Andric }
6890b57cec5SDimitry Andric 
6905ffd83dbSDimitry Andric bool SystemZTargetLowering::useSoftFloat() const {
6915ffd83dbSDimitry Andric   return Subtarget.hasSoftFloat();
6925ffd83dbSDimitry Andric }
6935ffd83dbSDimitry Andric 
6940b57cec5SDimitry Andric EVT SystemZTargetLowering::getSetCCResultType(const DataLayout &DL,
6950b57cec5SDimitry Andric                                               LLVMContext &, EVT VT) const {
6960b57cec5SDimitry Andric   if (!VT.isVector())
6970b57cec5SDimitry Andric     return MVT::i32;
6980b57cec5SDimitry Andric   return VT.changeVectorElementTypeToInteger();
6990b57cec5SDimitry Andric }
7000b57cec5SDimitry Andric 
701480093f4SDimitry Andric bool SystemZTargetLowering::isFMAFasterThanFMulAndFAdd(
702480093f4SDimitry Andric     const MachineFunction &MF, EVT VT) const {
7030b57cec5SDimitry Andric   VT = VT.getScalarType();
7040b57cec5SDimitry Andric 
7050b57cec5SDimitry Andric   if (!VT.isSimple())
7060b57cec5SDimitry Andric     return false;
7070b57cec5SDimitry Andric 
7080b57cec5SDimitry Andric   switch (VT.getSimpleVT().SimpleTy) {
7090b57cec5SDimitry Andric   case MVT::f32:
7100b57cec5SDimitry Andric   case MVT::f64:
7110b57cec5SDimitry Andric     return true;
7120b57cec5SDimitry Andric   case MVT::f128:
7130b57cec5SDimitry Andric     return Subtarget.hasVectorEnhancements1();
7140b57cec5SDimitry Andric   default:
7150b57cec5SDimitry Andric     break;
7160b57cec5SDimitry Andric   }
7170b57cec5SDimitry Andric 
7180b57cec5SDimitry Andric   return false;
7190b57cec5SDimitry Andric }
7200b57cec5SDimitry Andric 
7210b57cec5SDimitry Andric // Return true if the constant can be generated with a vector instruction,
7220b57cec5SDimitry Andric // such as VGM, VGMB or VREPI.
7230b57cec5SDimitry Andric bool SystemZVectorConstantInfo::isVectorConstantLegal(
7240b57cec5SDimitry Andric     const SystemZSubtarget &Subtarget) {
72581ad6265SDimitry Andric   const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
7260b57cec5SDimitry Andric   if (!Subtarget.hasVector() ||
7270b57cec5SDimitry Andric       (isFP128 && !Subtarget.hasVectorEnhancements1()))
7280b57cec5SDimitry Andric     return false;
7290b57cec5SDimitry Andric 
7300b57cec5SDimitry Andric   // Try using VECTOR GENERATE BYTE MASK.  This is the architecturally-
7310b57cec5SDimitry Andric   // preferred way of creating all-zero and all-one vectors so give it
7320b57cec5SDimitry Andric   // priority over other methods below.
7330b57cec5SDimitry Andric   unsigned Mask = 0;
7340b57cec5SDimitry Andric   unsigned I = 0;
7350b57cec5SDimitry Andric   for (; I < SystemZ::VectorBytes; ++I) {
7360b57cec5SDimitry Andric     uint64_t Byte = IntBits.lshr(I * 8).trunc(8).getZExtValue();
7370b57cec5SDimitry Andric     if (Byte == 0xff)
7380b57cec5SDimitry Andric       Mask |= 1ULL << I;
7390b57cec5SDimitry Andric     else if (Byte != 0)
7400b57cec5SDimitry Andric       break;
7410b57cec5SDimitry Andric   }
7420b57cec5SDimitry Andric   if (I == SystemZ::VectorBytes) {
7430b57cec5SDimitry Andric     Opcode = SystemZISD::BYTE_MASK;
7440b57cec5SDimitry Andric     OpVals.push_back(Mask);
7450b57cec5SDimitry Andric     VecVT = MVT::getVectorVT(MVT::getIntegerVT(8), 16);
7460b57cec5SDimitry Andric     return true;
7470b57cec5SDimitry Andric   }
7480b57cec5SDimitry Andric 
7490b57cec5SDimitry Andric   if (SplatBitSize > 64)
7500b57cec5SDimitry Andric     return false;
7510b57cec5SDimitry Andric 
7520b57cec5SDimitry Andric   auto tryValue = [&](uint64_t Value) -> bool {
7530b57cec5SDimitry Andric     // Try VECTOR REPLICATE IMMEDIATE
7540b57cec5SDimitry Andric     int64_t SignedValue = SignExtend64(Value, SplatBitSize);
7550b57cec5SDimitry Andric     if (isInt<16>(SignedValue)) {
7560b57cec5SDimitry Andric       OpVals.push_back(((unsigned) SignedValue));
7570b57cec5SDimitry Andric       Opcode = SystemZISD::REPLICATE;
7580b57cec5SDimitry Andric       VecVT = MVT::getVectorVT(MVT::getIntegerVT(SplatBitSize),
7590b57cec5SDimitry Andric                                SystemZ::VectorBits / SplatBitSize);
7600b57cec5SDimitry Andric       return true;
7610b57cec5SDimitry Andric     }
7620b57cec5SDimitry Andric     // Try VECTOR GENERATE MASK
7630b57cec5SDimitry Andric     unsigned Start, End;
7640b57cec5SDimitry Andric     if (TII->isRxSBGMask(Value, SplatBitSize, Start, End)) {
7650b57cec5SDimitry Andric       // isRxSBGMask returns the bit numbers for a full 64-bit value, with 0
7660b57cec5SDimitry Andric       // denoting 1 << 63 and 63 denoting 1.  Convert them to bit numbers for
7670b57cec5SDimitry Andric       // an SplatBitSize value, so that 0 denotes 1 << (SplatBitSize-1).
7680b57cec5SDimitry Andric       OpVals.push_back(Start - (64 - SplatBitSize));
7690b57cec5SDimitry Andric       OpVals.push_back(End - (64 - SplatBitSize));
7700b57cec5SDimitry Andric       Opcode = SystemZISD::ROTATE_MASK;
7710b57cec5SDimitry Andric       VecVT = MVT::getVectorVT(MVT::getIntegerVT(SplatBitSize),
7720b57cec5SDimitry Andric                                SystemZ::VectorBits / SplatBitSize);
7730b57cec5SDimitry Andric       return true;
7740b57cec5SDimitry Andric     }
7750b57cec5SDimitry Andric     return false;
7760b57cec5SDimitry Andric   };
7770b57cec5SDimitry Andric 
7780b57cec5SDimitry Andric   // First try assuming that any undefined bits above the highest set bit
7790b57cec5SDimitry Andric   // and below the lowest set bit are 1s.  This increases the likelihood of
7800b57cec5SDimitry Andric   // being able to use a sign-extended element value in VECTOR REPLICATE
7810b57cec5SDimitry Andric   // IMMEDIATE or a wraparound mask in VECTOR GENERATE MASK.
7820b57cec5SDimitry Andric   uint64_t SplatBitsZ = SplatBits.getZExtValue();
7830b57cec5SDimitry Andric   uint64_t SplatUndefZ = SplatUndef.getZExtValue();
7840b57cec5SDimitry Andric   uint64_t Lower =
7850b57cec5SDimitry Andric       (SplatUndefZ & ((uint64_t(1) << findFirstSet(SplatBitsZ)) - 1));
7860b57cec5SDimitry Andric   uint64_t Upper =
7870b57cec5SDimitry Andric       (SplatUndefZ & ~((uint64_t(1) << findLastSet(SplatBitsZ)) - 1));
7880b57cec5SDimitry Andric   if (tryValue(SplatBitsZ | Upper | Lower))
7890b57cec5SDimitry Andric     return true;
7900b57cec5SDimitry Andric 
7910b57cec5SDimitry Andric   // Now try assuming that any undefined bits between the first and
7920b57cec5SDimitry Andric   // last defined set bits are set.  This increases the chances of
7930b57cec5SDimitry Andric   // using a non-wraparound mask.
7940b57cec5SDimitry Andric   uint64_t Middle = SplatUndefZ & ~Upper & ~Lower;
7950b57cec5SDimitry Andric   return tryValue(SplatBitsZ | Middle);
7960b57cec5SDimitry Andric }
7970b57cec5SDimitry Andric 
79881ad6265SDimitry Andric SystemZVectorConstantInfo::SystemZVectorConstantInfo(APInt IntImm) {
79981ad6265SDimitry Andric   if (IntImm.isSingleWord()) {
80081ad6265SDimitry Andric     IntBits = APInt(128, IntImm.getZExtValue());
80181ad6265SDimitry Andric     IntBits <<= (SystemZ::VectorBits - IntImm.getBitWidth());
80281ad6265SDimitry Andric   } else
80381ad6265SDimitry Andric     IntBits = IntImm;
80481ad6265SDimitry Andric   assert(IntBits.getBitWidth() == 128 && "Unsupported APInt.");
805e8d8bef9SDimitry Andric 
806e8d8bef9SDimitry Andric   // Find the smallest splat.
80781ad6265SDimitry Andric   SplatBits = IntImm;
80881ad6265SDimitry Andric   unsigned Width = SplatBits.getBitWidth();
8090b57cec5SDimitry Andric   while (Width > 8) {
8100b57cec5SDimitry Andric     unsigned HalfSize = Width / 2;
8110b57cec5SDimitry Andric     APInt HighValue = SplatBits.lshr(HalfSize).trunc(HalfSize);
8120b57cec5SDimitry Andric     APInt LowValue = SplatBits.trunc(HalfSize);
8130b57cec5SDimitry Andric 
8140b57cec5SDimitry Andric     // If the two halves do not match, stop here.
8150b57cec5SDimitry Andric     if (HighValue != LowValue || 8 > HalfSize)
8160b57cec5SDimitry Andric       break;
8170b57cec5SDimitry Andric 
8180b57cec5SDimitry Andric     SplatBits = HighValue;
8190b57cec5SDimitry Andric     Width = HalfSize;
8200b57cec5SDimitry Andric   }
8210b57cec5SDimitry Andric   SplatUndef = 0;
8220b57cec5SDimitry Andric   SplatBitSize = Width;
8230b57cec5SDimitry Andric }
8240b57cec5SDimitry Andric 
8250b57cec5SDimitry Andric SystemZVectorConstantInfo::SystemZVectorConstantInfo(BuildVectorSDNode *BVN) {
8260b57cec5SDimitry Andric   assert(BVN->isConstant() && "Expected a constant BUILD_VECTOR");
8270b57cec5SDimitry Andric   bool HasAnyUndefs;
8280b57cec5SDimitry Andric 
8290b57cec5SDimitry Andric   // Get IntBits by finding the 128 bit splat.
8300b57cec5SDimitry Andric   BVN->isConstantSplat(IntBits, SplatUndef, SplatBitSize, HasAnyUndefs, 128,
8310b57cec5SDimitry Andric                        true);
8320b57cec5SDimitry Andric 
8330b57cec5SDimitry Andric   // Get SplatBits by finding the 8 bit or greater splat.
8340b57cec5SDimitry Andric   BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs, 8,
8350b57cec5SDimitry Andric                        true);
8360b57cec5SDimitry Andric }
8370b57cec5SDimitry Andric 
8380b57cec5SDimitry Andric bool SystemZTargetLowering::isFPImmLegal(const APFloat &Imm, EVT VT,
8390b57cec5SDimitry Andric                                          bool ForCodeSize) const {
8400b57cec5SDimitry Andric   // We can load zero using LZ?R and negative zero using LZ?R;LC?BR.
8410b57cec5SDimitry Andric   if (Imm.isZero() || Imm.isNegZero())
8420b57cec5SDimitry Andric     return true;
8430b57cec5SDimitry Andric 
8440b57cec5SDimitry Andric   return SystemZVectorConstantInfo(Imm).isVectorConstantLegal(Subtarget);
8450b57cec5SDimitry Andric }
8460b57cec5SDimitry Andric 
8475ffd83dbSDimitry Andric /// Returns true if stack probing through inline assembly is requested.
848bdd1243dSDimitry Andric bool SystemZTargetLowering::hasInlineStackProbe(const MachineFunction &MF) const {
8495ffd83dbSDimitry Andric   // If the function specifically requests inline stack probes, emit them.
8505ffd83dbSDimitry Andric   if (MF.getFunction().hasFnAttribute("probe-stack"))
8515ffd83dbSDimitry Andric     return MF.getFunction().getFnAttribute("probe-stack").getValueAsString() ==
8525ffd83dbSDimitry Andric            "inline-asm";
8535ffd83dbSDimitry Andric   return false;
8545ffd83dbSDimitry Andric }
8555ffd83dbSDimitry Andric 
8560b57cec5SDimitry Andric bool SystemZTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
8570b57cec5SDimitry Andric   // We can use CGFI or CLGFI.
8580b57cec5SDimitry Andric   return isInt<32>(Imm) || isUInt<32>(Imm);
8590b57cec5SDimitry Andric }
8600b57cec5SDimitry Andric 
8610b57cec5SDimitry Andric bool SystemZTargetLowering::isLegalAddImmediate(int64_t Imm) const {
8620b57cec5SDimitry Andric   // We can use ALGFI or SLGFI.
8630b57cec5SDimitry Andric   return isUInt<32>(Imm) || isUInt<32>(-Imm);
8640b57cec5SDimitry Andric }
8650b57cec5SDimitry Andric 
8660b57cec5SDimitry Andric bool SystemZTargetLowering::allowsMisalignedMemoryAccesses(
867bdd1243dSDimitry Andric     EVT VT, unsigned, Align, MachineMemOperand::Flags, unsigned *Fast) const {
8680b57cec5SDimitry Andric   // Unaligned accesses should never be slower than the expanded version.
8690b57cec5SDimitry Andric   // We check specifically for aligned accesses in the few cases where
8700b57cec5SDimitry Andric   // they are required.
8710b57cec5SDimitry Andric   if (Fast)
872bdd1243dSDimitry Andric     *Fast = 1;
8730b57cec5SDimitry Andric   return true;
8740b57cec5SDimitry Andric }
8750b57cec5SDimitry Andric 
8760b57cec5SDimitry Andric // Information about the addressing mode for a memory access.
8770b57cec5SDimitry Andric struct AddressingMode {
8780b57cec5SDimitry Andric   // True if a long displacement is supported.
8790b57cec5SDimitry Andric   bool LongDisplacement;
8800b57cec5SDimitry Andric 
8810b57cec5SDimitry Andric   // True if use of index register is supported.
8820b57cec5SDimitry Andric   bool IndexReg;
8830b57cec5SDimitry Andric 
8840b57cec5SDimitry Andric   AddressingMode(bool LongDispl, bool IdxReg) :
8850b57cec5SDimitry Andric     LongDisplacement(LongDispl), IndexReg(IdxReg) {}
8860b57cec5SDimitry Andric };
8870b57cec5SDimitry Andric 
8880b57cec5SDimitry Andric // Return the desired addressing mode for a Load which has only one use (in
8890b57cec5SDimitry Andric // the same block) which is a Store.
8900b57cec5SDimitry Andric static AddressingMode getLoadStoreAddrMode(bool HasVector,
8910b57cec5SDimitry Andric                                           Type *Ty) {
8920b57cec5SDimitry Andric   // With vector support a Load->Store combination may be combined to either
8930b57cec5SDimitry Andric   // an MVC or vector operations and it seems to work best to allow the
8940b57cec5SDimitry Andric   // vector addressing mode.
8950b57cec5SDimitry Andric   if (HasVector)
8960b57cec5SDimitry Andric     return AddressingMode(false/*LongDispl*/, true/*IdxReg*/);
8970b57cec5SDimitry Andric 
8980b57cec5SDimitry Andric   // Otherwise only the MVC case is special.
8990b57cec5SDimitry Andric   bool MVC = Ty->isIntegerTy(8);
9000b57cec5SDimitry Andric   return AddressingMode(!MVC/*LongDispl*/, !MVC/*IdxReg*/);
9010b57cec5SDimitry Andric }
9020b57cec5SDimitry Andric 
9030b57cec5SDimitry Andric // Return the addressing mode which seems most desirable given an LLVM
9040b57cec5SDimitry Andric // Instruction pointer.
9050b57cec5SDimitry Andric static AddressingMode
9060b57cec5SDimitry Andric supportedAddressingMode(Instruction *I, bool HasVector) {
9070b57cec5SDimitry Andric   if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
9080b57cec5SDimitry Andric     switch (II->getIntrinsicID()) {
9090b57cec5SDimitry Andric     default: break;
9100b57cec5SDimitry Andric     case Intrinsic::memset:
9110b57cec5SDimitry Andric     case Intrinsic::memmove:
9120b57cec5SDimitry Andric     case Intrinsic::memcpy:
9130b57cec5SDimitry Andric       return AddressingMode(false/*LongDispl*/, false/*IdxReg*/);
9140b57cec5SDimitry Andric     }
9150b57cec5SDimitry Andric   }
9160b57cec5SDimitry Andric 
9170b57cec5SDimitry Andric   if (isa<LoadInst>(I) && I->hasOneUse()) {
9188bcb0991SDimitry Andric     auto *SingleUser = cast<Instruction>(*I->user_begin());
9190b57cec5SDimitry Andric     if (SingleUser->getParent() == I->getParent()) {
9200b57cec5SDimitry Andric       if (isa<ICmpInst>(SingleUser)) {
9210b57cec5SDimitry Andric         if (auto *C = dyn_cast<ConstantInt>(SingleUser->getOperand(1)))
9220b57cec5SDimitry Andric           if (C->getBitWidth() <= 64 &&
9230b57cec5SDimitry Andric               (isInt<16>(C->getSExtValue()) || isUInt<16>(C->getZExtValue())))
9240b57cec5SDimitry Andric             // Comparison of memory with 16 bit signed / unsigned immediate
9250b57cec5SDimitry Andric             return AddressingMode(false/*LongDispl*/, false/*IdxReg*/);
9260b57cec5SDimitry Andric       } else if (isa<StoreInst>(SingleUser))
9270b57cec5SDimitry Andric         // Load->Store
9280b57cec5SDimitry Andric         return getLoadStoreAddrMode(HasVector, I->getType());
9290b57cec5SDimitry Andric     }
9300b57cec5SDimitry Andric   } else if (auto *StoreI = dyn_cast<StoreInst>(I)) {
9310b57cec5SDimitry Andric     if (auto *LoadI = dyn_cast<LoadInst>(StoreI->getValueOperand()))
9320b57cec5SDimitry Andric       if (LoadI->hasOneUse() && LoadI->getParent() == I->getParent())
9330b57cec5SDimitry Andric         // Load->Store
9340b57cec5SDimitry Andric         return getLoadStoreAddrMode(HasVector, LoadI->getType());
9350b57cec5SDimitry Andric   }
9360b57cec5SDimitry Andric 
9370b57cec5SDimitry Andric   if (HasVector && (isa<LoadInst>(I) || isa<StoreInst>(I))) {
9380b57cec5SDimitry Andric 
9390b57cec5SDimitry Andric     // * Use LDE instead of LE/LEY for z13 to avoid partial register
9400b57cec5SDimitry Andric     //   dependencies (LDE only supports small offsets).
9410b57cec5SDimitry Andric     // * Utilize the vector registers to hold floating point
9420b57cec5SDimitry Andric     //   values (vector load / store instructions only support small
9430b57cec5SDimitry Andric     //   offsets).
9440b57cec5SDimitry Andric 
9450b57cec5SDimitry Andric     Type *MemAccessTy = (isa<LoadInst>(I) ? I->getType() :
9460b57cec5SDimitry Andric                          I->getOperand(0)->getType());
9470b57cec5SDimitry Andric     bool IsFPAccess = MemAccessTy->isFloatingPointTy();
9480b57cec5SDimitry Andric     bool IsVectorAccess = MemAccessTy->isVectorTy();
9490b57cec5SDimitry Andric 
9500b57cec5SDimitry Andric     // A store of an extracted vector element will be combined into a VSTE type
9510b57cec5SDimitry Andric     // instruction.
9520b57cec5SDimitry Andric     if (!IsVectorAccess && isa<StoreInst>(I)) {
9530b57cec5SDimitry Andric       Value *DataOp = I->getOperand(0);
9540b57cec5SDimitry Andric       if (isa<ExtractElementInst>(DataOp))
9550b57cec5SDimitry Andric         IsVectorAccess = true;
9560b57cec5SDimitry Andric     }
9570b57cec5SDimitry Andric 
9580b57cec5SDimitry Andric     // A load which gets inserted into a vector element will be combined into a
9590b57cec5SDimitry Andric     // VLE type instruction.
9600b57cec5SDimitry Andric     if (!IsVectorAccess && isa<LoadInst>(I) && I->hasOneUse()) {
9610b57cec5SDimitry Andric       User *LoadUser = *I->user_begin();
9620b57cec5SDimitry Andric       if (isa<InsertElementInst>(LoadUser))
9630b57cec5SDimitry Andric         IsVectorAccess = true;
9640b57cec5SDimitry Andric     }
9650b57cec5SDimitry Andric 
9660b57cec5SDimitry Andric     if (IsFPAccess || IsVectorAccess)
9670b57cec5SDimitry Andric       return AddressingMode(false/*LongDispl*/, true/*IdxReg*/);
9680b57cec5SDimitry Andric   }
9690b57cec5SDimitry Andric 
9700b57cec5SDimitry Andric   return AddressingMode(true/*LongDispl*/, true/*IdxReg*/);
9710b57cec5SDimitry Andric }
9720b57cec5SDimitry Andric 
9730b57cec5SDimitry Andric bool SystemZTargetLowering::isLegalAddressingMode(const DataLayout &DL,
9740b57cec5SDimitry Andric        const AddrMode &AM, Type *Ty, unsigned AS, Instruction *I) const {
9750b57cec5SDimitry Andric   // Punt on globals for now, although they can be used in limited
9760b57cec5SDimitry Andric   // RELATIVE LONG cases.
9770b57cec5SDimitry Andric   if (AM.BaseGV)
9780b57cec5SDimitry Andric     return false;
9790b57cec5SDimitry Andric 
9800b57cec5SDimitry Andric   // Require a 20-bit signed offset.
9810b57cec5SDimitry Andric   if (!isInt<20>(AM.BaseOffs))
9820b57cec5SDimitry Andric     return false;
9830b57cec5SDimitry Andric 
98481ad6265SDimitry Andric   bool RequireD12 = Subtarget.hasVector() && Ty->isVectorTy();
98581ad6265SDimitry Andric   AddressingMode SupportedAM(!RequireD12, true);
9860b57cec5SDimitry Andric   if (I != nullptr)
9870b57cec5SDimitry Andric     SupportedAM = supportedAddressingMode(I, Subtarget.hasVector());
9880b57cec5SDimitry Andric 
9890b57cec5SDimitry Andric   if (!SupportedAM.LongDisplacement && !isUInt<12>(AM.BaseOffs))
9900b57cec5SDimitry Andric     return false;
9910b57cec5SDimitry Andric 
9920b57cec5SDimitry Andric   if (!SupportedAM.IndexReg)
9930b57cec5SDimitry Andric     // No indexing allowed.
9940b57cec5SDimitry Andric     return AM.Scale == 0;
9950b57cec5SDimitry Andric   else
9960b57cec5SDimitry Andric     // Indexing is OK but no scale factor can be applied.
9970b57cec5SDimitry Andric     return AM.Scale == 0 || AM.Scale == 1;
9980b57cec5SDimitry Andric }
9990b57cec5SDimitry Andric 
100081ad6265SDimitry Andric bool SystemZTargetLowering::findOptimalMemOpLowering(
100181ad6265SDimitry Andric     std::vector<EVT> &MemOps, unsigned Limit, const MemOp &Op, unsigned DstAS,
100281ad6265SDimitry Andric     unsigned SrcAS, const AttributeList &FuncAttributes) const {
100381ad6265SDimitry Andric   const int MVCFastLen = 16;
100481ad6265SDimitry Andric 
100581ad6265SDimitry Andric   if (Limit != ~unsigned(0)) {
100681ad6265SDimitry Andric     // Don't expand Op into scalar loads/stores in these cases:
100781ad6265SDimitry Andric     if (Op.isMemcpy() && Op.allowOverlap() && Op.size() <= MVCFastLen)
100881ad6265SDimitry Andric       return false; // Small memcpy: Use MVC
100981ad6265SDimitry Andric     if (Op.isMemset() && Op.size() - 1 <= MVCFastLen)
101081ad6265SDimitry Andric       return false; // Small memset (first byte with STC/MVI): Use MVC
101181ad6265SDimitry Andric     if (Op.isZeroMemset())
101281ad6265SDimitry Andric       return false; // Memset zero: Use XC
101381ad6265SDimitry Andric   }
101481ad6265SDimitry Andric 
101581ad6265SDimitry Andric   return TargetLowering::findOptimalMemOpLowering(MemOps, Limit, Op, DstAS,
101681ad6265SDimitry Andric                                                   SrcAS, FuncAttributes);
101781ad6265SDimitry Andric }
101881ad6265SDimitry Andric 
101981ad6265SDimitry Andric EVT SystemZTargetLowering::getOptimalMemOpType(const MemOp &Op,
102081ad6265SDimitry Andric                                    const AttributeList &FuncAttributes) const {
102181ad6265SDimitry Andric   return Subtarget.hasVector() ? MVT::v2i64 : MVT::Other;
102281ad6265SDimitry Andric }
102381ad6265SDimitry Andric 
10240b57cec5SDimitry Andric bool SystemZTargetLowering::isTruncateFree(Type *FromType, Type *ToType) const {
10250b57cec5SDimitry Andric   if (!FromType->isIntegerTy() || !ToType->isIntegerTy())
10260b57cec5SDimitry Andric     return false;
1027bdd1243dSDimitry Andric   unsigned FromBits = FromType->getPrimitiveSizeInBits().getFixedValue();
1028bdd1243dSDimitry Andric   unsigned ToBits = ToType->getPrimitiveSizeInBits().getFixedValue();
10290b57cec5SDimitry Andric   return FromBits > ToBits;
10300b57cec5SDimitry Andric }
10310b57cec5SDimitry Andric 
10320b57cec5SDimitry Andric bool SystemZTargetLowering::isTruncateFree(EVT FromVT, EVT ToVT) const {
10330b57cec5SDimitry Andric   if (!FromVT.isInteger() || !ToVT.isInteger())
10340b57cec5SDimitry Andric     return false;
1035e8d8bef9SDimitry Andric   unsigned FromBits = FromVT.getFixedSizeInBits();
1036e8d8bef9SDimitry Andric   unsigned ToBits = ToVT.getFixedSizeInBits();
10370b57cec5SDimitry Andric   return FromBits > ToBits;
10380b57cec5SDimitry Andric }
10390b57cec5SDimitry Andric 
10400b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
10410b57cec5SDimitry Andric // Inline asm support
10420b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
10430b57cec5SDimitry Andric 
10440b57cec5SDimitry Andric TargetLowering::ConstraintType
10450b57cec5SDimitry Andric SystemZTargetLowering::getConstraintType(StringRef Constraint) const {
10460b57cec5SDimitry Andric   if (Constraint.size() == 1) {
10470b57cec5SDimitry Andric     switch (Constraint[0]) {
10480b57cec5SDimitry Andric     case 'a': // Address register
10490b57cec5SDimitry Andric     case 'd': // Data register (equivalent to 'r')
10500b57cec5SDimitry Andric     case 'f': // Floating-point register
10510b57cec5SDimitry Andric     case 'h': // High-part register
10520b57cec5SDimitry Andric     case 'r': // General-purpose register
10530b57cec5SDimitry Andric     case 'v': // Vector register
10540b57cec5SDimitry Andric       return C_RegisterClass;
10550b57cec5SDimitry Andric 
10560b57cec5SDimitry Andric     case 'Q': // Memory with base and unsigned 12-bit displacement
10570b57cec5SDimitry Andric     case 'R': // Likewise, plus an index
10580b57cec5SDimitry Andric     case 'S': // Memory with base and signed 20-bit displacement
10590b57cec5SDimitry Andric     case 'T': // Likewise, plus an index
10600b57cec5SDimitry Andric     case 'm': // Equivalent to 'T'.
10610b57cec5SDimitry Andric       return C_Memory;
10620b57cec5SDimitry Andric 
10630b57cec5SDimitry Andric     case 'I': // Unsigned 8-bit constant
10640b57cec5SDimitry Andric     case 'J': // Unsigned 12-bit constant
10650b57cec5SDimitry Andric     case 'K': // Signed 16-bit constant
10660b57cec5SDimitry Andric     case 'L': // Signed 20-bit displacement (on all targets we support)
10670b57cec5SDimitry Andric     case 'M': // 0x7fffffff
10680b57cec5SDimitry Andric       return C_Immediate;
10690b57cec5SDimitry Andric 
10700b57cec5SDimitry Andric     default:
10710b57cec5SDimitry Andric       break;
10720b57cec5SDimitry Andric     }
107381ad6265SDimitry Andric   } else if (Constraint.size() == 2 && Constraint[0] == 'Z') {
107481ad6265SDimitry Andric     switch (Constraint[1]) {
107581ad6265SDimitry Andric     case 'Q': // Address with base and unsigned 12-bit displacement
107681ad6265SDimitry Andric     case 'R': // Likewise, plus an index
107781ad6265SDimitry Andric     case 'S': // Address with base and signed 20-bit displacement
107881ad6265SDimitry Andric     case 'T': // Likewise, plus an index
107981ad6265SDimitry Andric       return C_Address;
108081ad6265SDimitry Andric 
108181ad6265SDimitry Andric     default:
108281ad6265SDimitry Andric       break;
108381ad6265SDimitry Andric     }
10840b57cec5SDimitry Andric   }
10850b57cec5SDimitry Andric   return TargetLowering::getConstraintType(Constraint);
10860b57cec5SDimitry Andric }
10870b57cec5SDimitry Andric 
10880b57cec5SDimitry Andric TargetLowering::ConstraintWeight SystemZTargetLowering::
10890b57cec5SDimitry Andric getSingleConstraintMatchWeight(AsmOperandInfo &info,
10900b57cec5SDimitry Andric                                const char *constraint) const {
10910b57cec5SDimitry Andric   ConstraintWeight weight = CW_Invalid;
10920b57cec5SDimitry Andric   Value *CallOperandVal = info.CallOperandVal;
10930b57cec5SDimitry Andric   // If we don't have a value, we can't do a match,
10940b57cec5SDimitry Andric   // but allow it at the lowest weight.
10950b57cec5SDimitry Andric   if (!CallOperandVal)
10960b57cec5SDimitry Andric     return CW_Default;
10970b57cec5SDimitry Andric   Type *type = CallOperandVal->getType();
10980b57cec5SDimitry Andric   // Look at the constraint type.
10990b57cec5SDimitry Andric   switch (*constraint) {
11000b57cec5SDimitry Andric   default:
11010b57cec5SDimitry Andric     weight = TargetLowering::getSingleConstraintMatchWeight(info, constraint);
11020b57cec5SDimitry Andric     break;
11030b57cec5SDimitry Andric 
11040b57cec5SDimitry Andric   case 'a': // Address register
11050b57cec5SDimitry Andric   case 'd': // Data register (equivalent to 'r')
11060b57cec5SDimitry Andric   case 'h': // High-part register
11070b57cec5SDimitry Andric   case 'r': // General-purpose register
11080b57cec5SDimitry Andric     if (CallOperandVal->getType()->isIntegerTy())
11090b57cec5SDimitry Andric       weight = CW_Register;
11100b57cec5SDimitry Andric     break;
11110b57cec5SDimitry Andric 
11120b57cec5SDimitry Andric   case 'f': // Floating-point register
11130b57cec5SDimitry Andric     if (type->isFloatingPointTy())
11140b57cec5SDimitry Andric       weight = CW_Register;
11150b57cec5SDimitry Andric     break;
11160b57cec5SDimitry Andric 
11170b57cec5SDimitry Andric   case 'v': // Vector register
11180b57cec5SDimitry Andric     if ((type->isVectorTy() || type->isFloatingPointTy()) &&
11190b57cec5SDimitry Andric         Subtarget.hasVector())
11200b57cec5SDimitry Andric       weight = CW_Register;
11210b57cec5SDimitry Andric     break;
11220b57cec5SDimitry Andric 
11230b57cec5SDimitry Andric   case 'I': // Unsigned 8-bit constant
11240b57cec5SDimitry Andric     if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
11250b57cec5SDimitry Andric       if (isUInt<8>(C->getZExtValue()))
11260b57cec5SDimitry Andric         weight = CW_Constant;
11270b57cec5SDimitry Andric     break;
11280b57cec5SDimitry Andric 
11290b57cec5SDimitry Andric   case 'J': // Unsigned 12-bit constant
11300b57cec5SDimitry Andric     if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
11310b57cec5SDimitry Andric       if (isUInt<12>(C->getZExtValue()))
11320b57cec5SDimitry Andric         weight = CW_Constant;
11330b57cec5SDimitry Andric     break;
11340b57cec5SDimitry Andric 
11350b57cec5SDimitry Andric   case 'K': // Signed 16-bit constant
11360b57cec5SDimitry Andric     if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
11370b57cec5SDimitry Andric       if (isInt<16>(C->getSExtValue()))
11380b57cec5SDimitry Andric         weight = CW_Constant;
11390b57cec5SDimitry Andric     break;
11400b57cec5SDimitry Andric 
11410b57cec5SDimitry Andric   case 'L': // Signed 20-bit displacement (on all targets we support)
11420b57cec5SDimitry Andric     if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
11430b57cec5SDimitry Andric       if (isInt<20>(C->getSExtValue()))
11440b57cec5SDimitry Andric         weight = CW_Constant;
11450b57cec5SDimitry Andric     break;
11460b57cec5SDimitry Andric 
11470b57cec5SDimitry Andric   case 'M': // 0x7fffffff
11480b57cec5SDimitry Andric     if (auto *C = dyn_cast<ConstantInt>(CallOperandVal))
11490b57cec5SDimitry Andric       if (C->getZExtValue() == 0x7fffffff)
11500b57cec5SDimitry Andric         weight = CW_Constant;
11510b57cec5SDimitry Andric     break;
11520b57cec5SDimitry Andric   }
11530b57cec5SDimitry Andric   return weight;
11540b57cec5SDimitry Andric }
11550b57cec5SDimitry Andric 
11560b57cec5SDimitry Andric // Parse a "{tNNN}" register constraint for which the register type "t"
11570b57cec5SDimitry Andric // has already been verified.  MC is the class associated with "t" and
11580b57cec5SDimitry Andric // Map maps 0-based register numbers to LLVM register numbers.
11590b57cec5SDimitry Andric static std::pair<unsigned, const TargetRegisterClass *>
11600b57cec5SDimitry Andric parseRegisterNumber(StringRef Constraint, const TargetRegisterClass *RC,
11610b57cec5SDimitry Andric                     const unsigned *Map, unsigned Size) {
11620b57cec5SDimitry Andric   assert(*(Constraint.end()-1) == '}' && "Missing '}'");
11630b57cec5SDimitry Andric   if (isdigit(Constraint[2])) {
11640b57cec5SDimitry Andric     unsigned Index;
11650b57cec5SDimitry Andric     bool Failed =
11660b57cec5SDimitry Andric         Constraint.slice(2, Constraint.size() - 1).getAsInteger(10, Index);
11670b57cec5SDimitry Andric     if (!Failed && Index < Size && Map[Index])
11680b57cec5SDimitry Andric       return std::make_pair(Map[Index], RC);
11690b57cec5SDimitry Andric   }
11700b57cec5SDimitry Andric   return std::make_pair(0U, nullptr);
11710b57cec5SDimitry Andric }
11720b57cec5SDimitry Andric 
11730b57cec5SDimitry Andric std::pair<unsigned, const TargetRegisterClass *>
11740b57cec5SDimitry Andric SystemZTargetLowering::getRegForInlineAsmConstraint(
11750b57cec5SDimitry Andric     const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const {
11760b57cec5SDimitry Andric   if (Constraint.size() == 1) {
11770b57cec5SDimitry Andric     // GCC Constraint Letters
11780b57cec5SDimitry Andric     switch (Constraint[0]) {
11790b57cec5SDimitry Andric     default: break;
11800b57cec5SDimitry Andric     case 'd': // Data register (equivalent to 'r')
11810b57cec5SDimitry Andric     case 'r': // General-purpose register
11820b57cec5SDimitry Andric       if (VT == MVT::i64)
11830b57cec5SDimitry Andric         return std::make_pair(0U, &SystemZ::GR64BitRegClass);
11840b57cec5SDimitry Andric       else if (VT == MVT::i128)
11850b57cec5SDimitry Andric         return std::make_pair(0U, &SystemZ::GR128BitRegClass);
11860b57cec5SDimitry Andric       return std::make_pair(0U, &SystemZ::GR32BitRegClass);
11870b57cec5SDimitry Andric 
11880b57cec5SDimitry Andric     case 'a': // Address register
11890b57cec5SDimitry Andric       if (VT == MVT::i64)
11900b57cec5SDimitry Andric         return std::make_pair(0U, &SystemZ::ADDR64BitRegClass);
11910b57cec5SDimitry Andric       else if (VT == MVT::i128)
11920b57cec5SDimitry Andric         return std::make_pair(0U, &SystemZ::ADDR128BitRegClass);
11930b57cec5SDimitry Andric       return std::make_pair(0U, &SystemZ::ADDR32BitRegClass);
11940b57cec5SDimitry Andric 
11950b57cec5SDimitry Andric     case 'h': // High-part register (an LLVM extension)
11960b57cec5SDimitry Andric       return std::make_pair(0U, &SystemZ::GRH32BitRegClass);
11970b57cec5SDimitry Andric 
11980b57cec5SDimitry Andric     case 'f': // Floating-point register
11995ffd83dbSDimitry Andric       if (!useSoftFloat()) {
12000b57cec5SDimitry Andric         if (VT == MVT::f64)
12010b57cec5SDimitry Andric           return std::make_pair(0U, &SystemZ::FP64BitRegClass);
12020b57cec5SDimitry Andric         else if (VT == MVT::f128)
12030b57cec5SDimitry Andric           return std::make_pair(0U, &SystemZ::FP128BitRegClass);
12040b57cec5SDimitry Andric         return std::make_pair(0U, &SystemZ::FP32BitRegClass);
12055ffd83dbSDimitry Andric       }
12065ffd83dbSDimitry Andric       break;
12070b57cec5SDimitry Andric     case 'v': // Vector register
12080b57cec5SDimitry Andric       if (Subtarget.hasVector()) {
12090b57cec5SDimitry Andric         if (VT == MVT::f32)
12100b57cec5SDimitry Andric           return std::make_pair(0U, &SystemZ::VR32BitRegClass);
12110b57cec5SDimitry Andric         if (VT == MVT::f64)
12120b57cec5SDimitry Andric           return std::make_pair(0U, &SystemZ::VR64BitRegClass);
12130b57cec5SDimitry Andric         return std::make_pair(0U, &SystemZ::VR128BitRegClass);
12140b57cec5SDimitry Andric       }
12150b57cec5SDimitry Andric       break;
12160b57cec5SDimitry Andric     }
12170b57cec5SDimitry Andric   }
12180b57cec5SDimitry Andric   if (Constraint.size() > 0 && Constraint[0] == '{') {
12190b57cec5SDimitry Andric     // We need to override the default register parsing for GPRs and FPRs
12200b57cec5SDimitry Andric     // because the interpretation depends on VT.  The internal names of
12210b57cec5SDimitry Andric     // the registers are also different from the external names
12220b57cec5SDimitry Andric     // (F0D and F0S instead of F0, etc.).
12230b57cec5SDimitry Andric     if (Constraint[1] == 'r') {
12240b57cec5SDimitry Andric       if (VT == MVT::i32)
12250b57cec5SDimitry Andric         return parseRegisterNumber(Constraint, &SystemZ::GR32BitRegClass,
12260b57cec5SDimitry Andric                                    SystemZMC::GR32Regs, 16);
12270b57cec5SDimitry Andric       if (VT == MVT::i128)
12280b57cec5SDimitry Andric         return parseRegisterNumber(Constraint, &SystemZ::GR128BitRegClass,
12290b57cec5SDimitry Andric                                    SystemZMC::GR128Regs, 16);
12300b57cec5SDimitry Andric       return parseRegisterNumber(Constraint, &SystemZ::GR64BitRegClass,
12310b57cec5SDimitry Andric                                  SystemZMC::GR64Regs, 16);
12320b57cec5SDimitry Andric     }
12330b57cec5SDimitry Andric     if (Constraint[1] == 'f') {
12345ffd83dbSDimitry Andric       if (useSoftFloat())
12355ffd83dbSDimitry Andric         return std::make_pair(
12365ffd83dbSDimitry Andric             0u, static_cast<const TargetRegisterClass *>(nullptr));
12370b57cec5SDimitry Andric       if (VT == MVT::f32)
12380b57cec5SDimitry Andric         return parseRegisterNumber(Constraint, &SystemZ::FP32BitRegClass,
12390b57cec5SDimitry Andric                                    SystemZMC::FP32Regs, 16);
12400b57cec5SDimitry Andric       if (VT == MVT::f128)
12410b57cec5SDimitry Andric         return parseRegisterNumber(Constraint, &SystemZ::FP128BitRegClass,
12420b57cec5SDimitry Andric                                    SystemZMC::FP128Regs, 16);
12430b57cec5SDimitry Andric       return parseRegisterNumber(Constraint, &SystemZ::FP64BitRegClass,
12440b57cec5SDimitry Andric                                  SystemZMC::FP64Regs, 16);
12450b57cec5SDimitry Andric     }
12460b57cec5SDimitry Andric     if (Constraint[1] == 'v') {
12475ffd83dbSDimitry Andric       if (!Subtarget.hasVector())
12485ffd83dbSDimitry Andric         return std::make_pair(
12495ffd83dbSDimitry Andric             0u, static_cast<const TargetRegisterClass *>(nullptr));
12500b57cec5SDimitry Andric       if (VT == MVT::f32)
12510b57cec5SDimitry Andric         return parseRegisterNumber(Constraint, &SystemZ::VR32BitRegClass,
12520b57cec5SDimitry Andric                                    SystemZMC::VR32Regs, 32);
12530b57cec5SDimitry Andric       if (VT == MVT::f64)
12540b57cec5SDimitry Andric         return parseRegisterNumber(Constraint, &SystemZ::VR64BitRegClass,
12550b57cec5SDimitry Andric                                    SystemZMC::VR64Regs, 32);
12560b57cec5SDimitry Andric       return parseRegisterNumber(Constraint, &SystemZ::VR128BitRegClass,
12570b57cec5SDimitry Andric                                  SystemZMC::VR128Regs, 32);
12580b57cec5SDimitry Andric     }
12590b57cec5SDimitry Andric   }
12600b57cec5SDimitry Andric   return TargetLowering::getRegForInlineAsmConstraint(TRI, Constraint, VT);
12610b57cec5SDimitry Andric }
12620b57cec5SDimitry Andric 
12635ffd83dbSDimitry Andric // FIXME? Maybe this could be a TableGen attribute on some registers and
12645ffd83dbSDimitry Andric // this table could be generated automatically from RegInfo.
126581ad6265SDimitry Andric Register
126681ad6265SDimitry Andric SystemZTargetLowering::getRegisterByName(const char *RegName, LLT VT,
12675ffd83dbSDimitry Andric                                          const MachineFunction &MF) const {
126881ad6265SDimitry Andric   const SystemZSubtarget *Subtarget = &MF.getSubtarget<SystemZSubtarget>();
12695ffd83dbSDimitry Andric 
127081ad6265SDimitry Andric   Register Reg =
127181ad6265SDimitry Andric       StringSwitch<Register>(RegName)
127281ad6265SDimitry Andric           .Case("r4", Subtarget->isTargetXPLINK64() ? SystemZ::R4D : 0)
127381ad6265SDimitry Andric           .Case("r15", Subtarget->isTargetELF() ? SystemZ::R15D : 0)
12745ffd83dbSDimitry Andric           .Default(0);
127581ad6265SDimitry Andric 
12765ffd83dbSDimitry Andric   if (Reg)
12775ffd83dbSDimitry Andric     return Reg;
12785ffd83dbSDimitry Andric   report_fatal_error("Invalid register name global variable");
12795ffd83dbSDimitry Andric }
12805ffd83dbSDimitry Andric 
12810b57cec5SDimitry Andric void SystemZTargetLowering::
12820b57cec5SDimitry Andric LowerAsmOperandForConstraint(SDValue Op, std::string &Constraint,
12830b57cec5SDimitry Andric                              std::vector<SDValue> &Ops,
12840b57cec5SDimitry Andric                              SelectionDAG &DAG) const {
12850b57cec5SDimitry Andric   // Only support length 1 constraints for now.
12860b57cec5SDimitry Andric   if (Constraint.length() == 1) {
12870b57cec5SDimitry Andric     switch (Constraint[0]) {
12880b57cec5SDimitry Andric     case 'I': // Unsigned 8-bit constant
12890b57cec5SDimitry Andric       if (auto *C = dyn_cast<ConstantSDNode>(Op))
12900b57cec5SDimitry Andric         if (isUInt<8>(C->getZExtValue()))
12910b57cec5SDimitry Andric           Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
12920b57cec5SDimitry Andric                                               Op.getValueType()));
12930b57cec5SDimitry Andric       return;
12940b57cec5SDimitry Andric 
12950b57cec5SDimitry Andric     case 'J': // Unsigned 12-bit constant
12960b57cec5SDimitry Andric       if (auto *C = dyn_cast<ConstantSDNode>(Op))
12970b57cec5SDimitry Andric         if (isUInt<12>(C->getZExtValue()))
12980b57cec5SDimitry Andric           Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
12990b57cec5SDimitry Andric                                               Op.getValueType()));
13000b57cec5SDimitry Andric       return;
13010b57cec5SDimitry Andric 
13020b57cec5SDimitry Andric     case 'K': // Signed 16-bit constant
13030b57cec5SDimitry Andric       if (auto *C = dyn_cast<ConstantSDNode>(Op))
13040b57cec5SDimitry Andric         if (isInt<16>(C->getSExtValue()))
13050b57cec5SDimitry Andric           Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
13060b57cec5SDimitry Andric                                               Op.getValueType()));
13070b57cec5SDimitry Andric       return;
13080b57cec5SDimitry Andric 
13090b57cec5SDimitry Andric     case 'L': // Signed 20-bit displacement (on all targets we support)
13100b57cec5SDimitry Andric       if (auto *C = dyn_cast<ConstantSDNode>(Op))
13110b57cec5SDimitry Andric         if (isInt<20>(C->getSExtValue()))
13120b57cec5SDimitry Andric           Ops.push_back(DAG.getTargetConstant(C->getSExtValue(), SDLoc(Op),
13130b57cec5SDimitry Andric                                               Op.getValueType()));
13140b57cec5SDimitry Andric       return;
13150b57cec5SDimitry Andric 
13160b57cec5SDimitry Andric     case 'M': // 0x7fffffff
13170b57cec5SDimitry Andric       if (auto *C = dyn_cast<ConstantSDNode>(Op))
13180b57cec5SDimitry Andric         if (C->getZExtValue() == 0x7fffffff)
13190b57cec5SDimitry Andric           Ops.push_back(DAG.getTargetConstant(C->getZExtValue(), SDLoc(Op),
13200b57cec5SDimitry Andric                                               Op.getValueType()));
13210b57cec5SDimitry Andric       return;
13220b57cec5SDimitry Andric     }
13230b57cec5SDimitry Andric   }
13240b57cec5SDimitry Andric   TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
13250b57cec5SDimitry Andric }
13260b57cec5SDimitry Andric 
13270b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
13280b57cec5SDimitry Andric // Calling conventions
13290b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
13300b57cec5SDimitry Andric 
13310b57cec5SDimitry Andric #include "SystemZGenCallingConv.inc"
13320b57cec5SDimitry Andric 
13330b57cec5SDimitry Andric const MCPhysReg *SystemZTargetLowering::getScratchRegisters(
13340b57cec5SDimitry Andric   CallingConv::ID) const {
13350b57cec5SDimitry Andric   static const MCPhysReg ScratchRegs[] = { SystemZ::R0D, SystemZ::R1D,
13360b57cec5SDimitry Andric                                            SystemZ::R14D, 0 };
13370b57cec5SDimitry Andric   return ScratchRegs;
13380b57cec5SDimitry Andric }
13390b57cec5SDimitry Andric 
13400b57cec5SDimitry Andric bool SystemZTargetLowering::allowTruncateForTailCall(Type *FromType,
13410b57cec5SDimitry Andric                                                      Type *ToType) const {
13420b57cec5SDimitry Andric   return isTruncateFree(FromType, ToType);
13430b57cec5SDimitry Andric }
13440b57cec5SDimitry Andric 
13450b57cec5SDimitry Andric bool SystemZTargetLowering::mayBeEmittedAsTailCall(const CallInst *CI) const {
13460b57cec5SDimitry Andric   return CI->isTailCall();
13470b57cec5SDimitry Andric }
13480b57cec5SDimitry Andric 
13490b57cec5SDimitry Andric // We do not yet support 128-bit single-element vector types.  If the user
13500b57cec5SDimitry Andric // attempts to use such types as function argument or return type, prefer
13510b57cec5SDimitry Andric // to error out instead of emitting code violating the ABI.
13520b57cec5SDimitry Andric static void VerifyVectorType(MVT VT, EVT ArgVT) {
13530b57cec5SDimitry Andric   if (ArgVT.isVector() && !VT.isVector())
13540b57cec5SDimitry Andric     report_fatal_error("Unsupported vector argument or return type");
13550b57cec5SDimitry Andric }
13560b57cec5SDimitry Andric 
13570b57cec5SDimitry Andric static void VerifyVectorTypes(const SmallVectorImpl<ISD::InputArg> &Ins) {
13580b57cec5SDimitry Andric   for (unsigned i = 0; i < Ins.size(); ++i)
13590b57cec5SDimitry Andric     VerifyVectorType(Ins[i].VT, Ins[i].ArgVT);
13600b57cec5SDimitry Andric }
13610b57cec5SDimitry Andric 
13620b57cec5SDimitry Andric static void VerifyVectorTypes(const SmallVectorImpl<ISD::OutputArg> &Outs) {
13630b57cec5SDimitry Andric   for (unsigned i = 0; i < Outs.size(); ++i)
13640b57cec5SDimitry Andric     VerifyVectorType(Outs[i].VT, Outs[i].ArgVT);
13650b57cec5SDimitry Andric }
13660b57cec5SDimitry Andric 
13670b57cec5SDimitry Andric // Value is a value that has been passed to us in the location described by VA
13680b57cec5SDimitry Andric // (and so has type VA.getLocVT()).  Convert Value to VA.getValVT(), chaining
13690b57cec5SDimitry Andric // any loads onto Chain.
13700b57cec5SDimitry Andric static SDValue convertLocVTToValVT(SelectionDAG &DAG, const SDLoc &DL,
13710b57cec5SDimitry Andric                                    CCValAssign &VA, SDValue Chain,
13720b57cec5SDimitry Andric                                    SDValue Value) {
13730b57cec5SDimitry Andric   // If the argument has been promoted from a smaller type, insert an
13740b57cec5SDimitry Andric   // assertion to capture this.
13750b57cec5SDimitry Andric   if (VA.getLocInfo() == CCValAssign::SExt)
13760b57cec5SDimitry Andric     Value = DAG.getNode(ISD::AssertSext, DL, VA.getLocVT(), Value,
13770b57cec5SDimitry Andric                         DAG.getValueType(VA.getValVT()));
13780b57cec5SDimitry Andric   else if (VA.getLocInfo() == CCValAssign::ZExt)
13790b57cec5SDimitry Andric     Value = DAG.getNode(ISD::AssertZext, DL, VA.getLocVT(), Value,
13800b57cec5SDimitry Andric                         DAG.getValueType(VA.getValVT()));
13810b57cec5SDimitry Andric 
13820b57cec5SDimitry Andric   if (VA.isExtInLoc())
13830b57cec5SDimitry Andric     Value = DAG.getNode(ISD::TRUNCATE, DL, VA.getValVT(), Value);
13840b57cec5SDimitry Andric   else if (VA.getLocInfo() == CCValAssign::BCvt) {
13850b57cec5SDimitry Andric     // If this is a short vector argument loaded from the stack,
13860b57cec5SDimitry Andric     // extend from i64 to full vector size and then bitcast.
13870b57cec5SDimitry Andric     assert(VA.getLocVT() == MVT::i64);
13880b57cec5SDimitry Andric     assert(VA.getValVT().isVector());
13890b57cec5SDimitry Andric     Value = DAG.getBuildVector(MVT::v2i64, DL, {Value, DAG.getUNDEF(MVT::i64)});
13900b57cec5SDimitry Andric     Value = DAG.getNode(ISD::BITCAST, DL, VA.getValVT(), Value);
13910b57cec5SDimitry Andric   } else
13920b57cec5SDimitry Andric     assert(VA.getLocInfo() == CCValAssign::Full && "Unsupported getLocInfo");
13930b57cec5SDimitry Andric   return Value;
13940b57cec5SDimitry Andric }
13950b57cec5SDimitry Andric 
13960b57cec5SDimitry Andric // Value is a value of type VA.getValVT() that we need to copy into
13970b57cec5SDimitry Andric // the location described by VA.  Return a copy of Value converted to
13980b57cec5SDimitry Andric // VA.getValVT().  The caller is responsible for handling indirect values.
13990b57cec5SDimitry Andric static SDValue convertValVTToLocVT(SelectionDAG &DAG, const SDLoc &DL,
14000b57cec5SDimitry Andric                                    CCValAssign &VA, SDValue Value) {
14010b57cec5SDimitry Andric   switch (VA.getLocInfo()) {
14020b57cec5SDimitry Andric   case CCValAssign::SExt:
14030b57cec5SDimitry Andric     return DAG.getNode(ISD::SIGN_EXTEND, DL, VA.getLocVT(), Value);
14040b57cec5SDimitry Andric   case CCValAssign::ZExt:
14050b57cec5SDimitry Andric     return DAG.getNode(ISD::ZERO_EXTEND, DL, VA.getLocVT(), Value);
14060b57cec5SDimitry Andric   case CCValAssign::AExt:
14070b57cec5SDimitry Andric     return DAG.getNode(ISD::ANY_EXTEND, DL, VA.getLocVT(), Value);
1408349cc55cSDimitry Andric   case CCValAssign::BCvt: {
1409349cc55cSDimitry Andric     assert(VA.getLocVT() == MVT::i64 || VA.getLocVT() == MVT::i128);
1410fcaf7f86SDimitry Andric     assert(VA.getValVT().isVector() || VA.getValVT() == MVT::f32 ||
1411fcaf7f86SDimitry Andric            VA.getValVT() == MVT::f64 || VA.getValVT() == MVT::f128);
1412fcaf7f86SDimitry Andric     // For an f32 vararg we need to first promote it to an f64 and then
1413fcaf7f86SDimitry Andric     // bitcast it to an i64.
1414fcaf7f86SDimitry Andric     if (VA.getValVT() == MVT::f32 && VA.getLocVT() == MVT::i64)
1415fcaf7f86SDimitry Andric       Value = DAG.getNode(ISD::FP_EXTEND, DL, MVT::f64, Value);
1416349cc55cSDimitry Andric     MVT BitCastToType = VA.getValVT().isVector() && VA.getLocVT() == MVT::i64
1417349cc55cSDimitry Andric                             ? MVT::v2i64
1418349cc55cSDimitry Andric                             : VA.getLocVT();
1419349cc55cSDimitry Andric     Value = DAG.getNode(ISD::BITCAST, DL, BitCastToType, Value);
1420349cc55cSDimitry Andric     // For ELF, this is a short vector argument to be stored to the stack,
14210b57cec5SDimitry Andric     // bitcast to v2i64 and then extract first element.
1422349cc55cSDimitry Andric     if (BitCastToType == MVT::v2i64)
14230b57cec5SDimitry Andric       return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, VA.getLocVT(), Value,
14240b57cec5SDimitry Andric                          DAG.getConstant(0, DL, MVT::i32));
1425349cc55cSDimitry Andric     return Value;
1426349cc55cSDimitry Andric   }
14270b57cec5SDimitry Andric   case CCValAssign::Full:
14280b57cec5SDimitry Andric     return Value;
14290b57cec5SDimitry Andric   default:
14300b57cec5SDimitry Andric     llvm_unreachable("Unhandled getLocInfo()");
14310b57cec5SDimitry Andric   }
14320b57cec5SDimitry Andric }
14330b57cec5SDimitry Andric 
1434fe6060f1SDimitry Andric static SDValue lowerI128ToGR128(SelectionDAG &DAG, SDValue In) {
1435fe6060f1SDimitry Andric   SDLoc DL(In);
1436fe6060f1SDimitry Andric   SDValue Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i64, In,
1437fe6060f1SDimitry Andric                            DAG.getIntPtrConstant(0, DL));
1438fe6060f1SDimitry Andric   SDValue Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i64, In,
1439fe6060f1SDimitry Andric                            DAG.getIntPtrConstant(1, DL));
1440fe6060f1SDimitry Andric   SDNode *Pair = DAG.getMachineNode(SystemZ::PAIR128, DL,
1441fe6060f1SDimitry Andric                                     MVT::Untyped, Hi, Lo);
1442fe6060f1SDimitry Andric   return SDValue(Pair, 0);
1443fe6060f1SDimitry Andric }
1444fe6060f1SDimitry Andric 
1445fe6060f1SDimitry Andric static SDValue lowerGR128ToI128(SelectionDAG &DAG, SDValue In) {
1446fe6060f1SDimitry Andric   SDLoc DL(In);
1447fe6060f1SDimitry Andric   SDValue Hi = DAG.getTargetExtractSubreg(SystemZ::subreg_h64,
1448fe6060f1SDimitry Andric                                           DL, MVT::i64, In);
1449fe6060f1SDimitry Andric   SDValue Lo = DAG.getTargetExtractSubreg(SystemZ::subreg_l64,
1450fe6060f1SDimitry Andric                                           DL, MVT::i64, In);
1451fe6060f1SDimitry Andric   return DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i128, Lo, Hi);
1452fe6060f1SDimitry Andric }
1453fe6060f1SDimitry Andric 
1454fe6060f1SDimitry Andric bool SystemZTargetLowering::splitValueIntoRegisterParts(
1455fe6060f1SDimitry Andric     SelectionDAG &DAG, const SDLoc &DL, SDValue Val, SDValue *Parts,
1456bdd1243dSDimitry Andric     unsigned NumParts, MVT PartVT, std::optional<CallingConv::ID> CC) const {
1457fe6060f1SDimitry Andric   EVT ValueVT = Val.getValueType();
1458fe6060f1SDimitry Andric   assert((ValueVT != MVT::i128 ||
1459fe6060f1SDimitry Andric           ((NumParts == 1 && PartVT == MVT::Untyped) ||
1460fe6060f1SDimitry Andric            (NumParts == 2 && PartVT == MVT::i64))) &&
1461fe6060f1SDimitry Andric          "Unknown handling of i128 value.");
1462fe6060f1SDimitry Andric   if (ValueVT == MVT::i128 && NumParts == 1) {
1463fe6060f1SDimitry Andric     // Inline assembly operand.
1464fe6060f1SDimitry Andric     Parts[0] = lowerI128ToGR128(DAG, Val);
1465fe6060f1SDimitry Andric     return true;
1466fe6060f1SDimitry Andric   }
1467fe6060f1SDimitry Andric   return false;
1468fe6060f1SDimitry Andric }
1469fe6060f1SDimitry Andric 
1470fe6060f1SDimitry Andric SDValue SystemZTargetLowering::joinRegisterPartsIntoValue(
1471fe6060f1SDimitry Andric     SelectionDAG &DAG, const SDLoc &DL, const SDValue *Parts, unsigned NumParts,
1472bdd1243dSDimitry Andric     MVT PartVT, EVT ValueVT, std::optional<CallingConv::ID> CC) const {
1473fe6060f1SDimitry Andric   assert((ValueVT != MVT::i128 ||
1474fe6060f1SDimitry Andric           ((NumParts == 1 && PartVT == MVT::Untyped) ||
1475fe6060f1SDimitry Andric            (NumParts == 2 && PartVT == MVT::i64))) &&
1476fe6060f1SDimitry Andric          "Unknown handling of i128 value.");
1477fe6060f1SDimitry Andric   if (ValueVT == MVT::i128 && NumParts == 1)
1478fe6060f1SDimitry Andric     // Inline assembly operand.
1479fe6060f1SDimitry Andric     return lowerGR128ToI128(DAG, Parts[0]);
1480fe6060f1SDimitry Andric   return SDValue();
1481fe6060f1SDimitry Andric }
1482fe6060f1SDimitry Andric 
14830b57cec5SDimitry Andric SDValue SystemZTargetLowering::LowerFormalArguments(
14840b57cec5SDimitry Andric     SDValue Chain, CallingConv::ID CallConv, bool IsVarArg,
14850b57cec5SDimitry Andric     const SmallVectorImpl<ISD::InputArg> &Ins, const SDLoc &DL,
14860b57cec5SDimitry Andric     SelectionDAG &DAG, SmallVectorImpl<SDValue> &InVals) const {
14870b57cec5SDimitry Andric   MachineFunction &MF = DAG.getMachineFunction();
14880b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
14890b57cec5SDimitry Andric   MachineRegisterInfo &MRI = MF.getRegInfo();
14900b57cec5SDimitry Andric   SystemZMachineFunctionInfo *FuncInfo =
14910b57cec5SDimitry Andric       MF.getInfo<SystemZMachineFunctionInfo>();
1492349cc55cSDimitry Andric   auto *TFL = Subtarget.getFrameLowering<SystemZELFFrameLowering>();
14930b57cec5SDimitry Andric   EVT PtrVT = getPointerTy(DAG.getDataLayout());
14940b57cec5SDimitry Andric 
14950b57cec5SDimitry Andric   // Detect unsupported vector argument types.
14960b57cec5SDimitry Andric   if (Subtarget.hasVector())
14970b57cec5SDimitry Andric     VerifyVectorTypes(Ins);
14980b57cec5SDimitry Andric 
14990b57cec5SDimitry Andric   // Assign locations to all of the incoming arguments.
15000b57cec5SDimitry Andric   SmallVector<CCValAssign, 16> ArgLocs;
15010b57cec5SDimitry Andric   SystemZCCState CCInfo(CallConv, IsVarArg, MF, ArgLocs, *DAG.getContext());
15020b57cec5SDimitry Andric   CCInfo.AnalyzeFormalArguments(Ins, CC_SystemZ);
15030b57cec5SDimitry Andric 
15040b57cec5SDimitry Andric   unsigned NumFixedGPRs = 0;
15050b57cec5SDimitry Andric   unsigned NumFixedFPRs = 0;
15060b57cec5SDimitry Andric   for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
15070b57cec5SDimitry Andric     SDValue ArgValue;
15080b57cec5SDimitry Andric     CCValAssign &VA = ArgLocs[I];
15090b57cec5SDimitry Andric     EVT LocVT = VA.getLocVT();
15100b57cec5SDimitry Andric     if (VA.isRegLoc()) {
15110b57cec5SDimitry Andric       // Arguments passed in registers
15120b57cec5SDimitry Andric       const TargetRegisterClass *RC;
15130b57cec5SDimitry Andric       switch (LocVT.getSimpleVT().SimpleTy) {
15140b57cec5SDimitry Andric       default:
15150b57cec5SDimitry Andric         // Integers smaller than i64 should be promoted to i64.
15160b57cec5SDimitry Andric         llvm_unreachable("Unexpected argument type");
15170b57cec5SDimitry Andric       case MVT::i32:
15180b57cec5SDimitry Andric         NumFixedGPRs += 1;
15190b57cec5SDimitry Andric         RC = &SystemZ::GR32BitRegClass;
15200b57cec5SDimitry Andric         break;
15210b57cec5SDimitry Andric       case MVT::i64:
15220b57cec5SDimitry Andric         NumFixedGPRs += 1;
15230b57cec5SDimitry Andric         RC = &SystemZ::GR64BitRegClass;
15240b57cec5SDimitry Andric         break;
15250b57cec5SDimitry Andric       case MVT::f32:
15260b57cec5SDimitry Andric         NumFixedFPRs += 1;
15270b57cec5SDimitry Andric         RC = &SystemZ::FP32BitRegClass;
15280b57cec5SDimitry Andric         break;
15290b57cec5SDimitry Andric       case MVT::f64:
15300b57cec5SDimitry Andric         NumFixedFPRs += 1;
15310b57cec5SDimitry Andric         RC = &SystemZ::FP64BitRegClass;
15320b57cec5SDimitry Andric         break;
1533349cc55cSDimitry Andric       case MVT::f128:
1534349cc55cSDimitry Andric         NumFixedFPRs += 2;
1535349cc55cSDimitry Andric         RC = &SystemZ::FP128BitRegClass;
1536349cc55cSDimitry Andric         break;
15370b57cec5SDimitry Andric       case MVT::v16i8:
15380b57cec5SDimitry Andric       case MVT::v8i16:
15390b57cec5SDimitry Andric       case MVT::v4i32:
15400b57cec5SDimitry Andric       case MVT::v2i64:
15410b57cec5SDimitry Andric       case MVT::v4f32:
15420b57cec5SDimitry Andric       case MVT::v2f64:
15430b57cec5SDimitry Andric         RC = &SystemZ::VR128BitRegClass;
15440b57cec5SDimitry Andric         break;
15450b57cec5SDimitry Andric       }
15460b57cec5SDimitry Andric 
15478bcb0991SDimitry Andric       Register VReg = MRI.createVirtualRegister(RC);
15480b57cec5SDimitry Andric       MRI.addLiveIn(VA.getLocReg(), VReg);
15490b57cec5SDimitry Andric       ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, LocVT);
15500b57cec5SDimitry Andric     } else {
15510b57cec5SDimitry Andric       assert(VA.isMemLoc() && "Argument not register or memory");
15520b57cec5SDimitry Andric 
15530b57cec5SDimitry Andric       // Create the frame index object for this incoming parameter.
15540eae32dcSDimitry Andric       // FIXME: Pre-include call frame size in the offset, should not
15550eae32dcSDimitry Andric       // need to manually add it here.
15560eae32dcSDimitry Andric       int64_t ArgSPOffset = VA.getLocMemOffset();
15570eae32dcSDimitry Andric       if (Subtarget.isTargetXPLINK64()) {
15580eae32dcSDimitry Andric         auto &XPRegs =
15590eae32dcSDimitry Andric             Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
15600eae32dcSDimitry Andric         ArgSPOffset += XPRegs.getCallFrameSize();
15610eae32dcSDimitry Andric       }
15620eae32dcSDimitry Andric       int FI =
15630eae32dcSDimitry Andric           MFI.CreateFixedObject(LocVT.getSizeInBits() / 8, ArgSPOffset, true);
15640b57cec5SDimitry Andric 
15650b57cec5SDimitry Andric       // Create the SelectionDAG nodes corresponding to a load
15660b57cec5SDimitry Andric       // from this parameter.  Unpromoted ints and floats are
15670b57cec5SDimitry Andric       // passed as right-justified 8-byte values.
15680b57cec5SDimitry Andric       SDValue FIN = DAG.getFrameIndex(FI, PtrVT);
15690b57cec5SDimitry Andric       if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
15700b57cec5SDimitry Andric         FIN = DAG.getNode(ISD::ADD, DL, PtrVT, FIN,
15710b57cec5SDimitry Andric                           DAG.getIntPtrConstant(4, DL));
15720b57cec5SDimitry Andric       ArgValue = DAG.getLoad(LocVT, DL, Chain, FIN,
15730b57cec5SDimitry Andric                              MachinePointerInfo::getFixedStack(MF, FI));
15740b57cec5SDimitry Andric     }
15750b57cec5SDimitry Andric 
15760b57cec5SDimitry Andric     // Convert the value of the argument register into the value that's
15770b57cec5SDimitry Andric     // being passed.
15780b57cec5SDimitry Andric     if (VA.getLocInfo() == CCValAssign::Indirect) {
15790b57cec5SDimitry Andric       InVals.push_back(DAG.getLoad(VA.getValVT(), DL, Chain, ArgValue,
15800b57cec5SDimitry Andric                                    MachinePointerInfo()));
15810b57cec5SDimitry Andric       // If the original argument was split (e.g. i128), we need
15820b57cec5SDimitry Andric       // to load all parts of it here (using the same address).
15830b57cec5SDimitry Andric       unsigned ArgIndex = Ins[I].OrigArgIndex;
15840b57cec5SDimitry Andric       assert (Ins[I].PartOffset == 0);
15850b57cec5SDimitry Andric       while (I + 1 != E && Ins[I + 1].OrigArgIndex == ArgIndex) {
15860b57cec5SDimitry Andric         CCValAssign &PartVA = ArgLocs[I + 1];
15870b57cec5SDimitry Andric         unsigned PartOffset = Ins[I + 1].PartOffset;
15880b57cec5SDimitry Andric         SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, ArgValue,
15890b57cec5SDimitry Andric                                       DAG.getIntPtrConstant(PartOffset, DL));
15900b57cec5SDimitry Andric         InVals.push_back(DAG.getLoad(PartVA.getValVT(), DL, Chain, Address,
15910b57cec5SDimitry Andric                                      MachinePointerInfo()));
15920b57cec5SDimitry Andric         ++I;
15930b57cec5SDimitry Andric       }
15940b57cec5SDimitry Andric     } else
15950b57cec5SDimitry Andric       InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, ArgValue));
15960b57cec5SDimitry Andric   }
15970b57cec5SDimitry Andric 
1598349cc55cSDimitry Andric   // FIXME: Add support for lowering varargs for XPLINK64 in a later patch.
1599349cc55cSDimitry Andric   if (IsVarArg && Subtarget.isTargetELF()) {
16000b57cec5SDimitry Andric     // Save the number of non-varargs registers for later use by va_start, etc.
16010b57cec5SDimitry Andric     FuncInfo->setVarArgsFirstGPR(NumFixedGPRs);
16020b57cec5SDimitry Andric     FuncInfo->setVarArgsFirstFPR(NumFixedFPRs);
16030b57cec5SDimitry Andric 
16040b57cec5SDimitry Andric     // Likewise the address (in the form of a frame index) of where the
16050b57cec5SDimitry Andric     // first stack vararg would be.  The 1-byte size here is arbitrary.
16060b57cec5SDimitry Andric     int64_t StackSize = CCInfo.getNextStackOffset();
16070b57cec5SDimitry Andric     FuncInfo->setVarArgsFrameIndex(MFI.CreateFixedObject(1, StackSize, true));
16080b57cec5SDimitry Andric 
16090b57cec5SDimitry Andric     // ...and a similar frame index for the caller-allocated save area
16100b57cec5SDimitry Andric     // that will be used to store the incoming registers.
16115ffd83dbSDimitry Andric     int64_t RegSaveOffset =
1612fe6060f1SDimitry Andric       -SystemZMC::ELFCallFrameSize + TFL->getRegSpillOffset(MF, SystemZ::R2D) - 16;
16130b57cec5SDimitry Andric     unsigned RegSaveIndex = MFI.CreateFixedObject(1, RegSaveOffset, true);
16140b57cec5SDimitry Andric     FuncInfo->setRegSaveFrameIndex(RegSaveIndex);
16150b57cec5SDimitry Andric 
16160b57cec5SDimitry Andric     // Store the FPR varargs in the reserved frame slots.  (We store the
16170b57cec5SDimitry Andric     // GPRs as part of the prologue.)
1618fe6060f1SDimitry Andric     if (NumFixedFPRs < SystemZ::ELFNumArgFPRs && !useSoftFloat()) {
1619fe6060f1SDimitry Andric       SDValue MemOps[SystemZ::ELFNumArgFPRs];
1620fe6060f1SDimitry Andric       for (unsigned I = NumFixedFPRs; I < SystemZ::ELFNumArgFPRs; ++I) {
1621fe6060f1SDimitry Andric         unsigned Offset = TFL->getRegSpillOffset(MF, SystemZ::ELFArgFPRs[I]);
16225ffd83dbSDimitry Andric         int FI =
1623fe6060f1SDimitry Andric           MFI.CreateFixedObject(8, -SystemZMC::ELFCallFrameSize + Offset, true);
16240b57cec5SDimitry Andric         SDValue FIN = DAG.getFrameIndex(FI, getPointerTy(DAG.getDataLayout()));
162504eeddc0SDimitry Andric         Register VReg = MF.addLiveIn(SystemZ::ELFArgFPRs[I],
16260b57cec5SDimitry Andric                                      &SystemZ::FP64BitRegClass);
16270b57cec5SDimitry Andric         SDValue ArgValue = DAG.getCopyFromReg(Chain, DL, VReg, MVT::f64);
16280b57cec5SDimitry Andric         MemOps[I] = DAG.getStore(ArgValue.getValue(1), DL, ArgValue, FIN,
16290b57cec5SDimitry Andric                                  MachinePointerInfo::getFixedStack(MF, FI));
16300b57cec5SDimitry Andric       }
16310b57cec5SDimitry Andric       // Join the stores, which are independent of one another.
16320b57cec5SDimitry Andric       Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
1633bdd1243dSDimitry Andric                           ArrayRef(&MemOps[NumFixedFPRs],
1634fe6060f1SDimitry Andric                                    SystemZ::ELFNumArgFPRs - NumFixedFPRs));
16350b57cec5SDimitry Andric     }
16360b57cec5SDimitry Andric   }
16370b57cec5SDimitry Andric 
1638349cc55cSDimitry Andric   // FIXME: For XPLINK64, Add in support for handling incoming "ADA" special
1639349cc55cSDimitry Andric   // register (R5)
16400b57cec5SDimitry Andric   return Chain;
16410b57cec5SDimitry Andric }
16420b57cec5SDimitry Andric 
16430b57cec5SDimitry Andric static bool canUseSiblingCall(const CCState &ArgCCInfo,
16440b57cec5SDimitry Andric                               SmallVectorImpl<CCValAssign> &ArgLocs,
16450b57cec5SDimitry Andric                               SmallVectorImpl<ISD::OutputArg> &Outs) {
16460b57cec5SDimitry Andric   // Punt if there are any indirect or stack arguments, or if the call
16470b57cec5SDimitry Andric   // needs the callee-saved argument register R6, or if the call uses
16480b57cec5SDimitry Andric   // the callee-saved register arguments SwiftSelf and SwiftError.
16490b57cec5SDimitry Andric   for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
16500b57cec5SDimitry Andric     CCValAssign &VA = ArgLocs[I];
16510b57cec5SDimitry Andric     if (VA.getLocInfo() == CCValAssign::Indirect)
16520b57cec5SDimitry Andric       return false;
16530b57cec5SDimitry Andric     if (!VA.isRegLoc())
16540b57cec5SDimitry Andric       return false;
16558bcb0991SDimitry Andric     Register Reg = VA.getLocReg();
16560b57cec5SDimitry Andric     if (Reg == SystemZ::R6H || Reg == SystemZ::R6L || Reg == SystemZ::R6D)
16570b57cec5SDimitry Andric       return false;
16580b57cec5SDimitry Andric     if (Outs[I].Flags.isSwiftSelf() || Outs[I].Flags.isSwiftError())
16590b57cec5SDimitry Andric       return false;
16600b57cec5SDimitry Andric   }
16610b57cec5SDimitry Andric   return true;
16620b57cec5SDimitry Andric }
16630b57cec5SDimitry Andric 
16640b57cec5SDimitry Andric SDValue
16650b57cec5SDimitry Andric SystemZTargetLowering::LowerCall(CallLoweringInfo &CLI,
16660b57cec5SDimitry Andric                                  SmallVectorImpl<SDValue> &InVals) const {
16670b57cec5SDimitry Andric   SelectionDAG &DAG = CLI.DAG;
16680b57cec5SDimitry Andric   SDLoc &DL = CLI.DL;
16690b57cec5SDimitry Andric   SmallVectorImpl<ISD::OutputArg> &Outs = CLI.Outs;
16700b57cec5SDimitry Andric   SmallVectorImpl<SDValue> &OutVals = CLI.OutVals;
16710b57cec5SDimitry Andric   SmallVectorImpl<ISD::InputArg> &Ins = CLI.Ins;
16720b57cec5SDimitry Andric   SDValue Chain = CLI.Chain;
16730b57cec5SDimitry Andric   SDValue Callee = CLI.Callee;
16740b57cec5SDimitry Andric   bool &IsTailCall = CLI.IsTailCall;
16750b57cec5SDimitry Andric   CallingConv::ID CallConv = CLI.CallConv;
16760b57cec5SDimitry Andric   bool IsVarArg = CLI.IsVarArg;
16770b57cec5SDimitry Andric   MachineFunction &MF = DAG.getMachineFunction();
16780b57cec5SDimitry Andric   EVT PtrVT = getPointerTy(MF.getDataLayout());
16794652422eSDimitry Andric   LLVMContext &Ctx = *DAG.getContext();
1680349cc55cSDimitry Andric   SystemZCallingConventionRegisters *Regs = Subtarget.getSpecialRegisters();
1681349cc55cSDimitry Andric 
1682349cc55cSDimitry Andric   // FIXME: z/OS support to be added in later.
1683349cc55cSDimitry Andric   if (Subtarget.isTargetXPLINK64())
1684349cc55cSDimitry Andric     IsTailCall = false;
16850b57cec5SDimitry Andric 
16860b57cec5SDimitry Andric   // Detect unsupported vector argument and return types.
16870b57cec5SDimitry Andric   if (Subtarget.hasVector()) {
16880b57cec5SDimitry Andric     VerifyVectorTypes(Outs);
16890b57cec5SDimitry Andric     VerifyVectorTypes(Ins);
16900b57cec5SDimitry Andric   }
16910b57cec5SDimitry Andric 
16920b57cec5SDimitry Andric   // Analyze the operands of the call, assigning locations to each operand.
16930b57cec5SDimitry Andric   SmallVector<CCValAssign, 16> ArgLocs;
16944652422eSDimitry Andric   SystemZCCState ArgCCInfo(CallConv, IsVarArg, MF, ArgLocs, Ctx);
16950b57cec5SDimitry Andric   ArgCCInfo.AnalyzeCallOperands(Outs, CC_SystemZ);
16960b57cec5SDimitry Andric 
16970b57cec5SDimitry Andric   // We don't support GuaranteedTailCallOpt, only automatically-detected
16980b57cec5SDimitry Andric   // sibling calls.
16990b57cec5SDimitry Andric   if (IsTailCall && !canUseSiblingCall(ArgCCInfo, ArgLocs, Outs))
17000b57cec5SDimitry Andric     IsTailCall = false;
17010b57cec5SDimitry Andric 
17020b57cec5SDimitry Andric   // Get a count of how many bytes are to be pushed on the stack.
17030b57cec5SDimitry Andric   unsigned NumBytes = ArgCCInfo.getNextStackOffset();
17040b57cec5SDimitry Andric 
1705349cc55cSDimitry Andric   if (Subtarget.isTargetXPLINK64())
1706349cc55cSDimitry Andric     // Although the XPLINK specifications for AMODE64 state that minimum size
1707349cc55cSDimitry Andric     // of the param area is minimum 32 bytes and no rounding is otherwise
1708349cc55cSDimitry Andric     // specified, we round this area in 64 bytes increments to be compatible
1709349cc55cSDimitry Andric     // with existing compilers.
1710349cc55cSDimitry Andric     NumBytes = std::max(64U, (unsigned)alignTo(NumBytes, 64));
1711349cc55cSDimitry Andric 
17120b57cec5SDimitry Andric   // Mark the start of the call.
17130b57cec5SDimitry Andric   if (!IsTailCall)
17140b57cec5SDimitry Andric     Chain = DAG.getCALLSEQ_START(Chain, NumBytes, 0, DL);
17150b57cec5SDimitry Andric 
17160b57cec5SDimitry Andric   // Copy argument values to their designated locations.
17170b57cec5SDimitry Andric   SmallVector<std::pair<unsigned, SDValue>, 9> RegsToPass;
17180b57cec5SDimitry Andric   SmallVector<SDValue, 8> MemOpChains;
17190b57cec5SDimitry Andric   SDValue StackPtr;
17200b57cec5SDimitry Andric   for (unsigned I = 0, E = ArgLocs.size(); I != E; ++I) {
17210b57cec5SDimitry Andric     CCValAssign &VA = ArgLocs[I];
17220b57cec5SDimitry Andric     SDValue ArgValue = OutVals[I];
17230b57cec5SDimitry Andric 
17240b57cec5SDimitry Andric     if (VA.getLocInfo() == CCValAssign::Indirect) {
17250b57cec5SDimitry Andric       // Store the argument in a stack slot and pass its address.
17264652422eSDimitry Andric       unsigned ArgIndex = Outs[I].OrigArgIndex;
17274652422eSDimitry Andric       EVT SlotVT;
17284652422eSDimitry Andric       if (I + 1 != E && Outs[I + 1].OrigArgIndex == ArgIndex) {
17294652422eSDimitry Andric         // Allocate the full stack space for a promoted (and split) argument.
17304652422eSDimitry Andric         Type *OrigArgType = CLI.Args[Outs[I].OrigArgIndex].Ty;
17314652422eSDimitry Andric         EVT OrigArgVT = getValueType(MF.getDataLayout(), OrigArgType);
17324652422eSDimitry Andric         MVT PartVT = getRegisterTypeForCallingConv(Ctx, CLI.CallConv, OrigArgVT);
17334652422eSDimitry Andric         unsigned N = getNumRegistersForCallingConv(Ctx, CLI.CallConv, OrigArgVT);
17344652422eSDimitry Andric         SlotVT = EVT::getIntegerVT(Ctx, PartVT.getSizeInBits() * N);
17354652422eSDimitry Andric       } else {
17364652422eSDimitry Andric         SlotVT = Outs[I].ArgVT;
17374652422eSDimitry Andric       }
17384652422eSDimitry Andric       SDValue SpillSlot = DAG.CreateStackTemporary(SlotVT);
17390b57cec5SDimitry Andric       int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
17400b57cec5SDimitry Andric       MemOpChains.push_back(
17410b57cec5SDimitry Andric           DAG.getStore(Chain, DL, ArgValue, SpillSlot,
17420b57cec5SDimitry Andric                        MachinePointerInfo::getFixedStack(MF, FI)));
17430b57cec5SDimitry Andric       // If the original argument was split (e.g. i128), we need
17440b57cec5SDimitry Andric       // to store all parts of it here (and pass just one address).
17450b57cec5SDimitry Andric       assert (Outs[I].PartOffset == 0);
17460b57cec5SDimitry Andric       while (I + 1 != E && Outs[I + 1].OrigArgIndex == ArgIndex) {
17470b57cec5SDimitry Andric         SDValue PartValue = OutVals[I + 1];
17480b57cec5SDimitry Andric         unsigned PartOffset = Outs[I + 1].PartOffset;
17490b57cec5SDimitry Andric         SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, SpillSlot,
17500b57cec5SDimitry Andric                                       DAG.getIntPtrConstant(PartOffset, DL));
17510b57cec5SDimitry Andric         MemOpChains.push_back(
17520b57cec5SDimitry Andric             DAG.getStore(Chain, DL, PartValue, Address,
17530b57cec5SDimitry Andric                          MachinePointerInfo::getFixedStack(MF, FI)));
17544652422eSDimitry Andric         assert((PartOffset + PartValue.getValueType().getStoreSize() <=
17554652422eSDimitry Andric                 SlotVT.getStoreSize()) && "Not enough space for argument part!");
17560b57cec5SDimitry Andric         ++I;
17570b57cec5SDimitry Andric       }
17580b57cec5SDimitry Andric       ArgValue = SpillSlot;
17590b57cec5SDimitry Andric     } else
17600b57cec5SDimitry Andric       ArgValue = convertValVTToLocVT(DAG, DL, VA, ArgValue);
17610b57cec5SDimitry Andric 
1762349cc55cSDimitry Andric     if (VA.isRegLoc()) {
1763349cc55cSDimitry Andric       // In XPLINK64, for the 128-bit vararg case, ArgValue is bitcasted to a
1764349cc55cSDimitry Andric       // MVT::i128 type. We decompose the 128-bit type to a pair of its high
1765349cc55cSDimitry Andric       // and low values.
1766349cc55cSDimitry Andric       if (VA.getLocVT() == MVT::i128)
1767349cc55cSDimitry Andric         ArgValue = lowerI128ToGR128(DAG, ArgValue);
17680b57cec5SDimitry Andric       // Queue up the argument copies and emit them at the end.
17690b57cec5SDimitry Andric       RegsToPass.push_back(std::make_pair(VA.getLocReg(), ArgValue));
1770349cc55cSDimitry Andric     } else {
17710b57cec5SDimitry Andric       assert(VA.isMemLoc() && "Argument not register or memory");
17720b57cec5SDimitry Andric 
17730b57cec5SDimitry Andric       // Work out the address of the stack slot.  Unpromoted ints and
17740b57cec5SDimitry Andric       // floats are passed as right-justified 8-byte values.
17750b57cec5SDimitry Andric       if (!StackPtr.getNode())
1776349cc55cSDimitry Andric         StackPtr = DAG.getCopyFromReg(Chain, DL,
1777349cc55cSDimitry Andric                                       Regs->getStackPointerRegister(), PtrVT);
1778349cc55cSDimitry Andric       unsigned Offset = Regs->getStackPointerBias() + Regs->getCallFrameSize() +
1779349cc55cSDimitry Andric                         VA.getLocMemOffset();
17800b57cec5SDimitry Andric       if (VA.getLocVT() == MVT::i32 || VA.getLocVT() == MVT::f32)
17810b57cec5SDimitry Andric         Offset += 4;
17820b57cec5SDimitry Andric       SDValue Address = DAG.getNode(ISD::ADD, DL, PtrVT, StackPtr,
17830b57cec5SDimitry Andric                                     DAG.getIntPtrConstant(Offset, DL));
17840b57cec5SDimitry Andric 
17850b57cec5SDimitry Andric       // Emit the store.
17860b57cec5SDimitry Andric       MemOpChains.push_back(
17870b57cec5SDimitry Andric           DAG.getStore(Chain, DL, ArgValue, Address, MachinePointerInfo()));
1788349cc55cSDimitry Andric 
1789349cc55cSDimitry Andric       // Although long doubles or vectors are passed through the stack when
1790349cc55cSDimitry Andric       // they are vararg (non-fixed arguments), if a long double or vector
1791349cc55cSDimitry Andric       // occupies the third and fourth slot of the argument list GPR3 should
1792349cc55cSDimitry Andric       // still shadow the third slot of the argument list.
1793349cc55cSDimitry Andric       if (Subtarget.isTargetXPLINK64() && VA.needsCustom()) {
1794349cc55cSDimitry Andric         SDValue ShadowArgValue =
1795349cc55cSDimitry Andric             DAG.getNode(ISD::EXTRACT_ELEMENT, DL, MVT::i64, ArgValue,
1796349cc55cSDimitry Andric                         DAG.getIntPtrConstant(1, DL));
1797349cc55cSDimitry Andric         RegsToPass.push_back(std::make_pair(SystemZ::R3D, ShadowArgValue));
1798349cc55cSDimitry Andric       }
17990b57cec5SDimitry Andric     }
18000b57cec5SDimitry Andric   }
18010b57cec5SDimitry Andric 
18020b57cec5SDimitry Andric   // Join the stores, which are independent of one another.
18030b57cec5SDimitry Andric   if (!MemOpChains.empty())
18040b57cec5SDimitry Andric     Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOpChains);
18050b57cec5SDimitry Andric 
18060b57cec5SDimitry Andric   // Accept direct calls by converting symbolic call addresses to the
18070b57cec5SDimitry Andric   // associated Target* opcodes.  Force %r1 to be used for indirect
18080b57cec5SDimitry Andric   // tail calls.
18090b57cec5SDimitry Andric   SDValue Glue;
1810349cc55cSDimitry Andric   // FIXME: Add support for XPLINK using the ADA register.
18110b57cec5SDimitry Andric   if (auto *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
18120b57cec5SDimitry Andric     Callee = DAG.getTargetGlobalAddress(G->getGlobal(), DL, PtrVT);
18130b57cec5SDimitry Andric     Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
18140b57cec5SDimitry Andric   } else if (auto *E = dyn_cast<ExternalSymbolSDNode>(Callee)) {
18150b57cec5SDimitry Andric     Callee = DAG.getTargetExternalSymbol(E->getSymbol(), PtrVT);
18160b57cec5SDimitry Andric     Callee = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Callee);
18170b57cec5SDimitry Andric   } else if (IsTailCall) {
18180b57cec5SDimitry Andric     Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R1D, Callee, Glue);
18190b57cec5SDimitry Andric     Glue = Chain.getValue(1);
18200b57cec5SDimitry Andric     Callee = DAG.getRegister(SystemZ::R1D, Callee.getValueType());
18210b57cec5SDimitry Andric   }
18220b57cec5SDimitry Andric 
18230b57cec5SDimitry Andric   // Build a sequence of copy-to-reg nodes, chained and glued together.
18240b57cec5SDimitry Andric   for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I) {
18250b57cec5SDimitry Andric     Chain = DAG.getCopyToReg(Chain, DL, RegsToPass[I].first,
18260b57cec5SDimitry Andric                              RegsToPass[I].second, Glue);
18270b57cec5SDimitry Andric     Glue = Chain.getValue(1);
18280b57cec5SDimitry Andric   }
18290b57cec5SDimitry Andric 
18300b57cec5SDimitry Andric   // The first call operand is the chain and the second is the target address.
18310b57cec5SDimitry Andric   SmallVector<SDValue, 8> Ops;
18320b57cec5SDimitry Andric   Ops.push_back(Chain);
18330b57cec5SDimitry Andric   Ops.push_back(Callee);
18340b57cec5SDimitry Andric 
18350b57cec5SDimitry Andric   // Add argument registers to the end of the list so that they are
18360b57cec5SDimitry Andric   // known live into the call.
18370b57cec5SDimitry Andric   for (unsigned I = 0, E = RegsToPass.size(); I != E; ++I)
18380b57cec5SDimitry Andric     Ops.push_back(DAG.getRegister(RegsToPass[I].first,
18390b57cec5SDimitry Andric                                   RegsToPass[I].second.getValueType()));
18400b57cec5SDimitry Andric 
18410b57cec5SDimitry Andric   // Add a register mask operand representing the call-preserved registers.
18420b57cec5SDimitry Andric   const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
18430b57cec5SDimitry Andric   const uint32_t *Mask = TRI->getCallPreservedMask(MF, CallConv);
18440b57cec5SDimitry Andric   assert(Mask && "Missing call preserved mask for calling convention");
18450b57cec5SDimitry Andric   Ops.push_back(DAG.getRegisterMask(Mask));
18460b57cec5SDimitry Andric 
18470b57cec5SDimitry Andric   // Glue the call to the argument copies, if any.
18480b57cec5SDimitry Andric   if (Glue.getNode())
18490b57cec5SDimitry Andric     Ops.push_back(Glue);
18500b57cec5SDimitry Andric 
18510b57cec5SDimitry Andric   // Emit the call.
18520b57cec5SDimitry Andric   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
18530b57cec5SDimitry Andric   if (IsTailCall)
18540b57cec5SDimitry Andric     return DAG.getNode(SystemZISD::SIBCALL, DL, NodeTys, Ops);
18550b57cec5SDimitry Andric   Chain = DAG.getNode(SystemZISD::CALL, DL, NodeTys, Ops);
18565ffd83dbSDimitry Andric   DAG.addNoMergeSiteInfo(Chain.getNode(), CLI.NoMerge);
18570b57cec5SDimitry Andric   Glue = Chain.getValue(1);
18580b57cec5SDimitry Andric 
18590b57cec5SDimitry Andric   // Mark the end of the call, which is glued to the call itself.
1860bdd1243dSDimitry Andric   Chain = DAG.getCALLSEQ_END(Chain, NumBytes, 0, Glue, DL);
18610b57cec5SDimitry Andric   Glue = Chain.getValue(1);
18620b57cec5SDimitry Andric 
18630b57cec5SDimitry Andric   // Assign locations to each value returned by this call.
18640b57cec5SDimitry Andric   SmallVector<CCValAssign, 16> RetLocs;
18654652422eSDimitry Andric   CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, Ctx);
18660b57cec5SDimitry Andric   RetCCInfo.AnalyzeCallResult(Ins, RetCC_SystemZ);
18670b57cec5SDimitry Andric 
18680b57cec5SDimitry Andric   // Copy all of the result registers out of their specified physreg.
18690b57cec5SDimitry Andric   for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
18700b57cec5SDimitry Andric     CCValAssign &VA = RetLocs[I];
18710b57cec5SDimitry Andric 
18720b57cec5SDimitry Andric     // Copy the value out, gluing the copy to the end of the call sequence.
18730b57cec5SDimitry Andric     SDValue RetValue = DAG.getCopyFromReg(Chain, DL, VA.getLocReg(),
18740b57cec5SDimitry Andric                                           VA.getLocVT(), Glue);
18750b57cec5SDimitry Andric     Chain = RetValue.getValue(1);
18760b57cec5SDimitry Andric     Glue = RetValue.getValue(2);
18770b57cec5SDimitry Andric 
18780b57cec5SDimitry Andric     // Convert the value of the return register into the value that's
18790b57cec5SDimitry Andric     // being returned.
18800b57cec5SDimitry Andric     InVals.push_back(convertLocVTToValVT(DAG, DL, VA, Chain, RetValue));
18810b57cec5SDimitry Andric   }
18820b57cec5SDimitry Andric 
18830b57cec5SDimitry Andric   return Chain;
18840b57cec5SDimitry Andric }
18850b57cec5SDimitry Andric 
188681ad6265SDimitry Andric // Generate a call taking the given operands as arguments and returning a
188781ad6265SDimitry Andric // result of type RetVT.
188881ad6265SDimitry Andric std::pair<SDValue, SDValue> SystemZTargetLowering::makeExternalCall(
188981ad6265SDimitry Andric     SDValue Chain, SelectionDAG &DAG, const char *CalleeName, EVT RetVT,
189081ad6265SDimitry Andric     ArrayRef<SDValue> Ops, CallingConv::ID CallConv, bool IsSigned, SDLoc DL,
189181ad6265SDimitry Andric     bool DoesNotReturn, bool IsReturnValueUsed) const {
189281ad6265SDimitry Andric   TargetLowering::ArgListTy Args;
189381ad6265SDimitry Andric   Args.reserve(Ops.size());
189481ad6265SDimitry Andric 
189581ad6265SDimitry Andric   TargetLowering::ArgListEntry Entry;
189681ad6265SDimitry Andric   for (SDValue Op : Ops) {
189781ad6265SDimitry Andric     Entry.Node = Op;
189881ad6265SDimitry Andric     Entry.Ty = Entry.Node.getValueType().getTypeForEVT(*DAG.getContext());
189981ad6265SDimitry Andric     Entry.IsSExt = shouldSignExtendTypeInLibCall(Op.getValueType(), IsSigned);
190081ad6265SDimitry Andric     Entry.IsZExt = !shouldSignExtendTypeInLibCall(Op.getValueType(), IsSigned);
190181ad6265SDimitry Andric     Args.push_back(Entry);
190281ad6265SDimitry Andric   }
190381ad6265SDimitry Andric 
190481ad6265SDimitry Andric   SDValue Callee =
190581ad6265SDimitry Andric       DAG.getExternalSymbol(CalleeName, getPointerTy(DAG.getDataLayout()));
190681ad6265SDimitry Andric 
190781ad6265SDimitry Andric   Type *RetTy = RetVT.getTypeForEVT(*DAG.getContext());
190881ad6265SDimitry Andric   TargetLowering::CallLoweringInfo CLI(DAG);
190981ad6265SDimitry Andric   bool SignExtend = shouldSignExtendTypeInLibCall(RetVT, IsSigned);
191081ad6265SDimitry Andric   CLI.setDebugLoc(DL)
191181ad6265SDimitry Andric       .setChain(Chain)
191281ad6265SDimitry Andric       .setCallee(CallConv, RetTy, Callee, std::move(Args))
191381ad6265SDimitry Andric       .setNoReturn(DoesNotReturn)
191481ad6265SDimitry Andric       .setDiscardResult(!IsReturnValueUsed)
191581ad6265SDimitry Andric       .setSExtResult(SignExtend)
191681ad6265SDimitry Andric       .setZExtResult(!SignExtend);
191781ad6265SDimitry Andric   return LowerCallTo(CLI);
191881ad6265SDimitry Andric }
191981ad6265SDimitry Andric 
19200b57cec5SDimitry Andric bool SystemZTargetLowering::
19210b57cec5SDimitry Andric CanLowerReturn(CallingConv::ID CallConv,
19220b57cec5SDimitry Andric                MachineFunction &MF, bool isVarArg,
19230b57cec5SDimitry Andric                const SmallVectorImpl<ISD::OutputArg> &Outs,
19240b57cec5SDimitry Andric                LLVMContext &Context) const {
19250b57cec5SDimitry Andric   // Detect unsupported vector return types.
19260b57cec5SDimitry Andric   if (Subtarget.hasVector())
19270b57cec5SDimitry Andric     VerifyVectorTypes(Outs);
19280b57cec5SDimitry Andric 
19290b57cec5SDimitry Andric   // Special case that we cannot easily detect in RetCC_SystemZ since
19300b57cec5SDimitry Andric   // i128 is not a legal type.
19310b57cec5SDimitry Andric   for (auto &Out : Outs)
19320b57cec5SDimitry Andric     if (Out.ArgVT == MVT::i128)
19330b57cec5SDimitry Andric       return false;
19340b57cec5SDimitry Andric 
19350b57cec5SDimitry Andric   SmallVector<CCValAssign, 16> RetLocs;
19360b57cec5SDimitry Andric   CCState RetCCInfo(CallConv, isVarArg, MF, RetLocs, Context);
19370b57cec5SDimitry Andric   return RetCCInfo.CheckReturn(Outs, RetCC_SystemZ);
19380b57cec5SDimitry Andric }
19390b57cec5SDimitry Andric 
19400b57cec5SDimitry Andric SDValue
19410b57cec5SDimitry Andric SystemZTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
19420b57cec5SDimitry Andric                                    bool IsVarArg,
19430b57cec5SDimitry Andric                                    const SmallVectorImpl<ISD::OutputArg> &Outs,
19440b57cec5SDimitry Andric                                    const SmallVectorImpl<SDValue> &OutVals,
19450b57cec5SDimitry Andric                                    const SDLoc &DL, SelectionDAG &DAG) const {
19460b57cec5SDimitry Andric   MachineFunction &MF = DAG.getMachineFunction();
19470b57cec5SDimitry Andric 
19480b57cec5SDimitry Andric   // Detect unsupported vector return types.
19490b57cec5SDimitry Andric   if (Subtarget.hasVector())
19500b57cec5SDimitry Andric     VerifyVectorTypes(Outs);
19510b57cec5SDimitry Andric 
19520b57cec5SDimitry Andric   // Assign locations to each returned value.
19530b57cec5SDimitry Andric   SmallVector<CCValAssign, 16> RetLocs;
19540b57cec5SDimitry Andric   CCState RetCCInfo(CallConv, IsVarArg, MF, RetLocs, *DAG.getContext());
19550b57cec5SDimitry Andric   RetCCInfo.AnalyzeReturn(Outs, RetCC_SystemZ);
19560b57cec5SDimitry Andric 
19570b57cec5SDimitry Andric   // Quick exit for void returns
19580b57cec5SDimitry Andric   if (RetLocs.empty())
19590b57cec5SDimitry Andric     return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, Chain);
19600b57cec5SDimitry Andric 
1961480093f4SDimitry Andric   if (CallConv == CallingConv::GHC)
1962480093f4SDimitry Andric     report_fatal_error("GHC functions return void only");
1963480093f4SDimitry Andric 
19640b57cec5SDimitry Andric   // Copy the result values into the output registers.
19650b57cec5SDimitry Andric   SDValue Glue;
19660b57cec5SDimitry Andric   SmallVector<SDValue, 4> RetOps;
19670b57cec5SDimitry Andric   RetOps.push_back(Chain);
19680b57cec5SDimitry Andric   for (unsigned I = 0, E = RetLocs.size(); I != E; ++I) {
19690b57cec5SDimitry Andric     CCValAssign &VA = RetLocs[I];
19700b57cec5SDimitry Andric     SDValue RetValue = OutVals[I];
19710b57cec5SDimitry Andric 
19720b57cec5SDimitry Andric     // Make the return register live on exit.
19730b57cec5SDimitry Andric     assert(VA.isRegLoc() && "Can only return in registers!");
19740b57cec5SDimitry Andric 
19750b57cec5SDimitry Andric     // Promote the value as required.
19760b57cec5SDimitry Andric     RetValue = convertValVTToLocVT(DAG, DL, VA, RetValue);
19770b57cec5SDimitry Andric 
19780b57cec5SDimitry Andric     // Chain and glue the copies together.
19798bcb0991SDimitry Andric     Register Reg = VA.getLocReg();
19800b57cec5SDimitry Andric     Chain = DAG.getCopyToReg(Chain, DL, Reg, RetValue, Glue);
19810b57cec5SDimitry Andric     Glue = Chain.getValue(1);
19820b57cec5SDimitry Andric     RetOps.push_back(DAG.getRegister(Reg, VA.getLocVT()));
19830b57cec5SDimitry Andric   }
19840b57cec5SDimitry Andric 
19850b57cec5SDimitry Andric   // Update chain and glue.
19860b57cec5SDimitry Andric   RetOps[0] = Chain;
19870b57cec5SDimitry Andric   if (Glue.getNode())
19880b57cec5SDimitry Andric     RetOps.push_back(Glue);
19890b57cec5SDimitry Andric 
19900b57cec5SDimitry Andric   return DAG.getNode(SystemZISD::RET_FLAG, DL, MVT::Other, RetOps);
19910b57cec5SDimitry Andric }
19920b57cec5SDimitry Andric 
19930b57cec5SDimitry Andric // Return true if Op is an intrinsic node with chain that returns the CC value
19940b57cec5SDimitry Andric // as its only (other) argument.  Provide the associated SystemZISD opcode and
19950b57cec5SDimitry Andric // the mask of valid CC values if so.
19960b57cec5SDimitry Andric static bool isIntrinsicWithCCAndChain(SDValue Op, unsigned &Opcode,
19970b57cec5SDimitry Andric                                       unsigned &CCValid) {
19980b57cec5SDimitry Andric   unsigned Id = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
19990b57cec5SDimitry Andric   switch (Id) {
20000b57cec5SDimitry Andric   case Intrinsic::s390_tbegin:
20010b57cec5SDimitry Andric     Opcode = SystemZISD::TBEGIN;
20020b57cec5SDimitry Andric     CCValid = SystemZ::CCMASK_TBEGIN;
20030b57cec5SDimitry Andric     return true;
20040b57cec5SDimitry Andric 
20050b57cec5SDimitry Andric   case Intrinsic::s390_tbegin_nofloat:
20060b57cec5SDimitry Andric     Opcode = SystemZISD::TBEGIN_NOFLOAT;
20070b57cec5SDimitry Andric     CCValid = SystemZ::CCMASK_TBEGIN;
20080b57cec5SDimitry Andric     return true;
20090b57cec5SDimitry Andric 
20100b57cec5SDimitry Andric   case Intrinsic::s390_tend:
20110b57cec5SDimitry Andric     Opcode = SystemZISD::TEND;
20120b57cec5SDimitry Andric     CCValid = SystemZ::CCMASK_TEND;
20130b57cec5SDimitry Andric     return true;
20140b57cec5SDimitry Andric 
20150b57cec5SDimitry Andric   default:
20160b57cec5SDimitry Andric     return false;
20170b57cec5SDimitry Andric   }
20180b57cec5SDimitry Andric }
20190b57cec5SDimitry Andric 
20200b57cec5SDimitry Andric // Return true if Op is an intrinsic node without chain that returns the
20210b57cec5SDimitry Andric // CC value as its final argument.  Provide the associated SystemZISD
20220b57cec5SDimitry Andric // opcode and the mask of valid CC values if so.
20230b57cec5SDimitry Andric static bool isIntrinsicWithCC(SDValue Op, unsigned &Opcode, unsigned &CCValid) {
20240b57cec5SDimitry Andric   unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
20250b57cec5SDimitry Andric   switch (Id) {
20260b57cec5SDimitry Andric   case Intrinsic::s390_vpkshs:
20270b57cec5SDimitry Andric   case Intrinsic::s390_vpksfs:
20280b57cec5SDimitry Andric   case Intrinsic::s390_vpksgs:
20290b57cec5SDimitry Andric     Opcode = SystemZISD::PACKS_CC;
20300b57cec5SDimitry Andric     CCValid = SystemZ::CCMASK_VCMP;
20310b57cec5SDimitry Andric     return true;
20320b57cec5SDimitry Andric 
20330b57cec5SDimitry Andric   case Intrinsic::s390_vpklshs:
20340b57cec5SDimitry Andric   case Intrinsic::s390_vpklsfs:
20350b57cec5SDimitry Andric   case Intrinsic::s390_vpklsgs:
20360b57cec5SDimitry Andric     Opcode = SystemZISD::PACKLS_CC;
20370b57cec5SDimitry Andric     CCValid = SystemZ::CCMASK_VCMP;
20380b57cec5SDimitry Andric     return true;
20390b57cec5SDimitry Andric 
20400b57cec5SDimitry Andric   case Intrinsic::s390_vceqbs:
20410b57cec5SDimitry Andric   case Intrinsic::s390_vceqhs:
20420b57cec5SDimitry Andric   case Intrinsic::s390_vceqfs:
20430b57cec5SDimitry Andric   case Intrinsic::s390_vceqgs:
20440b57cec5SDimitry Andric     Opcode = SystemZISD::VICMPES;
20450b57cec5SDimitry Andric     CCValid = SystemZ::CCMASK_VCMP;
20460b57cec5SDimitry Andric     return true;
20470b57cec5SDimitry Andric 
20480b57cec5SDimitry Andric   case Intrinsic::s390_vchbs:
20490b57cec5SDimitry Andric   case Intrinsic::s390_vchhs:
20500b57cec5SDimitry Andric   case Intrinsic::s390_vchfs:
20510b57cec5SDimitry Andric   case Intrinsic::s390_vchgs:
20520b57cec5SDimitry Andric     Opcode = SystemZISD::VICMPHS;
20530b57cec5SDimitry Andric     CCValid = SystemZ::CCMASK_VCMP;
20540b57cec5SDimitry Andric     return true;
20550b57cec5SDimitry Andric 
20560b57cec5SDimitry Andric   case Intrinsic::s390_vchlbs:
20570b57cec5SDimitry Andric   case Intrinsic::s390_vchlhs:
20580b57cec5SDimitry Andric   case Intrinsic::s390_vchlfs:
20590b57cec5SDimitry Andric   case Intrinsic::s390_vchlgs:
20600b57cec5SDimitry Andric     Opcode = SystemZISD::VICMPHLS;
20610b57cec5SDimitry Andric     CCValid = SystemZ::CCMASK_VCMP;
20620b57cec5SDimitry Andric     return true;
20630b57cec5SDimitry Andric 
20640b57cec5SDimitry Andric   case Intrinsic::s390_vtm:
20650b57cec5SDimitry Andric     Opcode = SystemZISD::VTM;
20660b57cec5SDimitry Andric     CCValid = SystemZ::CCMASK_VCMP;
20670b57cec5SDimitry Andric     return true;
20680b57cec5SDimitry Andric 
20690b57cec5SDimitry Andric   case Intrinsic::s390_vfaebs:
20700b57cec5SDimitry Andric   case Intrinsic::s390_vfaehs:
20710b57cec5SDimitry Andric   case Intrinsic::s390_vfaefs:
20720b57cec5SDimitry Andric     Opcode = SystemZISD::VFAE_CC;
20730b57cec5SDimitry Andric     CCValid = SystemZ::CCMASK_ANY;
20740b57cec5SDimitry Andric     return true;
20750b57cec5SDimitry Andric 
20760b57cec5SDimitry Andric   case Intrinsic::s390_vfaezbs:
20770b57cec5SDimitry Andric   case Intrinsic::s390_vfaezhs:
20780b57cec5SDimitry Andric   case Intrinsic::s390_vfaezfs:
20790b57cec5SDimitry Andric     Opcode = SystemZISD::VFAEZ_CC;
20800b57cec5SDimitry Andric     CCValid = SystemZ::CCMASK_ANY;
20810b57cec5SDimitry Andric     return true;
20820b57cec5SDimitry Andric 
20830b57cec5SDimitry Andric   case Intrinsic::s390_vfeebs:
20840b57cec5SDimitry Andric   case Intrinsic::s390_vfeehs:
20850b57cec5SDimitry Andric   case Intrinsic::s390_vfeefs:
20860b57cec5SDimitry Andric     Opcode = SystemZISD::VFEE_CC;
20870b57cec5SDimitry Andric     CCValid = SystemZ::CCMASK_ANY;
20880b57cec5SDimitry Andric     return true;
20890b57cec5SDimitry Andric 
20900b57cec5SDimitry Andric   case Intrinsic::s390_vfeezbs:
20910b57cec5SDimitry Andric   case Intrinsic::s390_vfeezhs:
20920b57cec5SDimitry Andric   case Intrinsic::s390_vfeezfs:
20930b57cec5SDimitry Andric     Opcode = SystemZISD::VFEEZ_CC;
20940b57cec5SDimitry Andric     CCValid = SystemZ::CCMASK_ANY;
20950b57cec5SDimitry Andric     return true;
20960b57cec5SDimitry Andric 
20970b57cec5SDimitry Andric   case Intrinsic::s390_vfenebs:
20980b57cec5SDimitry Andric   case Intrinsic::s390_vfenehs:
20990b57cec5SDimitry Andric   case Intrinsic::s390_vfenefs:
21000b57cec5SDimitry Andric     Opcode = SystemZISD::VFENE_CC;
21010b57cec5SDimitry Andric     CCValid = SystemZ::CCMASK_ANY;
21020b57cec5SDimitry Andric     return true;
21030b57cec5SDimitry Andric 
21040b57cec5SDimitry Andric   case Intrinsic::s390_vfenezbs:
21050b57cec5SDimitry Andric   case Intrinsic::s390_vfenezhs:
21060b57cec5SDimitry Andric   case Intrinsic::s390_vfenezfs:
21070b57cec5SDimitry Andric     Opcode = SystemZISD::VFENEZ_CC;
21080b57cec5SDimitry Andric     CCValid = SystemZ::CCMASK_ANY;
21090b57cec5SDimitry Andric     return true;
21100b57cec5SDimitry Andric 
21110b57cec5SDimitry Andric   case Intrinsic::s390_vistrbs:
21120b57cec5SDimitry Andric   case Intrinsic::s390_vistrhs:
21130b57cec5SDimitry Andric   case Intrinsic::s390_vistrfs:
21140b57cec5SDimitry Andric     Opcode = SystemZISD::VISTR_CC;
21150b57cec5SDimitry Andric     CCValid = SystemZ::CCMASK_0 | SystemZ::CCMASK_3;
21160b57cec5SDimitry Andric     return true;
21170b57cec5SDimitry Andric 
21180b57cec5SDimitry Andric   case Intrinsic::s390_vstrcbs:
21190b57cec5SDimitry Andric   case Intrinsic::s390_vstrchs:
21200b57cec5SDimitry Andric   case Intrinsic::s390_vstrcfs:
21210b57cec5SDimitry Andric     Opcode = SystemZISD::VSTRC_CC;
21220b57cec5SDimitry Andric     CCValid = SystemZ::CCMASK_ANY;
21230b57cec5SDimitry Andric     return true;
21240b57cec5SDimitry Andric 
21250b57cec5SDimitry Andric   case Intrinsic::s390_vstrczbs:
21260b57cec5SDimitry Andric   case Intrinsic::s390_vstrczhs:
21270b57cec5SDimitry Andric   case Intrinsic::s390_vstrczfs:
21280b57cec5SDimitry Andric     Opcode = SystemZISD::VSTRCZ_CC;
21290b57cec5SDimitry Andric     CCValid = SystemZ::CCMASK_ANY;
21300b57cec5SDimitry Andric     return true;
21310b57cec5SDimitry Andric 
21320b57cec5SDimitry Andric   case Intrinsic::s390_vstrsb:
21330b57cec5SDimitry Andric   case Intrinsic::s390_vstrsh:
21340b57cec5SDimitry Andric   case Intrinsic::s390_vstrsf:
21350b57cec5SDimitry Andric     Opcode = SystemZISD::VSTRS_CC;
21360b57cec5SDimitry Andric     CCValid = SystemZ::CCMASK_ANY;
21370b57cec5SDimitry Andric     return true;
21380b57cec5SDimitry Andric 
21390b57cec5SDimitry Andric   case Intrinsic::s390_vstrszb:
21400b57cec5SDimitry Andric   case Intrinsic::s390_vstrszh:
21410b57cec5SDimitry Andric   case Intrinsic::s390_vstrszf:
21420b57cec5SDimitry Andric     Opcode = SystemZISD::VSTRSZ_CC;
21430b57cec5SDimitry Andric     CCValid = SystemZ::CCMASK_ANY;
21440b57cec5SDimitry Andric     return true;
21450b57cec5SDimitry Andric 
21460b57cec5SDimitry Andric   case Intrinsic::s390_vfcedbs:
21470b57cec5SDimitry Andric   case Intrinsic::s390_vfcesbs:
21480b57cec5SDimitry Andric     Opcode = SystemZISD::VFCMPES;
21490b57cec5SDimitry Andric     CCValid = SystemZ::CCMASK_VCMP;
21500b57cec5SDimitry Andric     return true;
21510b57cec5SDimitry Andric 
21520b57cec5SDimitry Andric   case Intrinsic::s390_vfchdbs:
21530b57cec5SDimitry Andric   case Intrinsic::s390_vfchsbs:
21540b57cec5SDimitry Andric     Opcode = SystemZISD::VFCMPHS;
21550b57cec5SDimitry Andric     CCValid = SystemZ::CCMASK_VCMP;
21560b57cec5SDimitry Andric     return true;
21570b57cec5SDimitry Andric 
21580b57cec5SDimitry Andric   case Intrinsic::s390_vfchedbs:
21590b57cec5SDimitry Andric   case Intrinsic::s390_vfchesbs:
21600b57cec5SDimitry Andric     Opcode = SystemZISD::VFCMPHES;
21610b57cec5SDimitry Andric     CCValid = SystemZ::CCMASK_VCMP;
21620b57cec5SDimitry Andric     return true;
21630b57cec5SDimitry Andric 
21640b57cec5SDimitry Andric   case Intrinsic::s390_vftcidb:
21650b57cec5SDimitry Andric   case Intrinsic::s390_vftcisb:
21660b57cec5SDimitry Andric     Opcode = SystemZISD::VFTCI;
21670b57cec5SDimitry Andric     CCValid = SystemZ::CCMASK_VCMP;
21680b57cec5SDimitry Andric     return true;
21690b57cec5SDimitry Andric 
21700b57cec5SDimitry Andric   case Intrinsic::s390_tdc:
21710b57cec5SDimitry Andric     Opcode = SystemZISD::TDC;
21720b57cec5SDimitry Andric     CCValid = SystemZ::CCMASK_TDC;
21730b57cec5SDimitry Andric     return true;
21740b57cec5SDimitry Andric 
21750b57cec5SDimitry Andric   default:
21760b57cec5SDimitry Andric     return false;
21770b57cec5SDimitry Andric   }
21780b57cec5SDimitry Andric }
21790b57cec5SDimitry Andric 
21800b57cec5SDimitry Andric // Emit an intrinsic with chain and an explicit CC register result.
21810b57cec5SDimitry Andric static SDNode *emitIntrinsicWithCCAndChain(SelectionDAG &DAG, SDValue Op,
21820b57cec5SDimitry Andric                                            unsigned Opcode) {
21830b57cec5SDimitry Andric   // Copy all operands except the intrinsic ID.
21840b57cec5SDimitry Andric   unsigned NumOps = Op.getNumOperands();
21850b57cec5SDimitry Andric   SmallVector<SDValue, 6> Ops;
21860b57cec5SDimitry Andric   Ops.reserve(NumOps - 1);
21870b57cec5SDimitry Andric   Ops.push_back(Op.getOperand(0));
21880b57cec5SDimitry Andric   for (unsigned I = 2; I < NumOps; ++I)
21890b57cec5SDimitry Andric     Ops.push_back(Op.getOperand(I));
21900b57cec5SDimitry Andric 
21910b57cec5SDimitry Andric   assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
21920b57cec5SDimitry Andric   SDVTList RawVTs = DAG.getVTList(MVT::i32, MVT::Other);
21930b57cec5SDimitry Andric   SDValue Intr = DAG.getNode(Opcode, SDLoc(Op), RawVTs, Ops);
21940b57cec5SDimitry Andric   SDValue OldChain = SDValue(Op.getNode(), 1);
21950b57cec5SDimitry Andric   SDValue NewChain = SDValue(Intr.getNode(), 1);
21960b57cec5SDimitry Andric   DAG.ReplaceAllUsesOfValueWith(OldChain, NewChain);
21970b57cec5SDimitry Andric   return Intr.getNode();
21980b57cec5SDimitry Andric }
21990b57cec5SDimitry Andric 
22000b57cec5SDimitry Andric // Emit an intrinsic with an explicit CC register result.
22010b57cec5SDimitry Andric static SDNode *emitIntrinsicWithCC(SelectionDAG &DAG, SDValue Op,
22020b57cec5SDimitry Andric                                    unsigned Opcode) {
22030b57cec5SDimitry Andric   // Copy all operands except the intrinsic ID.
22040b57cec5SDimitry Andric   unsigned NumOps = Op.getNumOperands();
22050b57cec5SDimitry Andric   SmallVector<SDValue, 6> Ops;
22060b57cec5SDimitry Andric   Ops.reserve(NumOps - 1);
22070b57cec5SDimitry Andric   for (unsigned I = 1; I < NumOps; ++I)
22080b57cec5SDimitry Andric     Ops.push_back(Op.getOperand(I));
22090b57cec5SDimitry Andric 
22100b57cec5SDimitry Andric   SDValue Intr = DAG.getNode(Opcode, SDLoc(Op), Op->getVTList(), Ops);
22110b57cec5SDimitry Andric   return Intr.getNode();
22120b57cec5SDimitry Andric }
22130b57cec5SDimitry Andric 
22140b57cec5SDimitry Andric // CC is a comparison that will be implemented using an integer or
22150b57cec5SDimitry Andric // floating-point comparison.  Return the condition code mask for
22160b57cec5SDimitry Andric // a branch on true.  In the integer case, CCMASK_CMP_UO is set for
22170b57cec5SDimitry Andric // unsigned comparisons and clear for signed ones.  In the floating-point
22180b57cec5SDimitry Andric // case, CCMASK_CMP_UO has its normal mask meaning (unordered).
22190b57cec5SDimitry Andric static unsigned CCMaskForCondCode(ISD::CondCode CC) {
22200b57cec5SDimitry Andric #define CONV(X) \
22210b57cec5SDimitry Andric   case ISD::SET##X: return SystemZ::CCMASK_CMP_##X; \
22220b57cec5SDimitry Andric   case ISD::SETO##X: return SystemZ::CCMASK_CMP_##X; \
22230b57cec5SDimitry Andric   case ISD::SETU##X: return SystemZ::CCMASK_CMP_UO | SystemZ::CCMASK_CMP_##X
22240b57cec5SDimitry Andric 
22250b57cec5SDimitry Andric   switch (CC) {
22260b57cec5SDimitry Andric   default:
22270b57cec5SDimitry Andric     llvm_unreachable("Invalid integer condition!");
22280b57cec5SDimitry Andric 
22290b57cec5SDimitry Andric   CONV(EQ);
22300b57cec5SDimitry Andric   CONV(NE);
22310b57cec5SDimitry Andric   CONV(GT);
22320b57cec5SDimitry Andric   CONV(GE);
22330b57cec5SDimitry Andric   CONV(LT);
22340b57cec5SDimitry Andric   CONV(LE);
22350b57cec5SDimitry Andric 
22360b57cec5SDimitry Andric   case ISD::SETO:  return SystemZ::CCMASK_CMP_O;
22370b57cec5SDimitry Andric   case ISD::SETUO: return SystemZ::CCMASK_CMP_UO;
22380b57cec5SDimitry Andric   }
22390b57cec5SDimitry Andric #undef CONV
22400b57cec5SDimitry Andric }
22410b57cec5SDimitry Andric 
22420b57cec5SDimitry Andric // If C can be converted to a comparison against zero, adjust the operands
22430b57cec5SDimitry Andric // as necessary.
22440b57cec5SDimitry Andric static void adjustZeroCmp(SelectionDAG &DAG, const SDLoc &DL, Comparison &C) {
22450b57cec5SDimitry Andric   if (C.ICmpType == SystemZICMP::UnsignedOnly)
22460b57cec5SDimitry Andric     return;
22470b57cec5SDimitry Andric 
22480b57cec5SDimitry Andric   auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1.getNode());
22490b57cec5SDimitry Andric   if (!ConstOp1)
22500b57cec5SDimitry Andric     return;
22510b57cec5SDimitry Andric 
22520b57cec5SDimitry Andric   int64_t Value = ConstOp1->getSExtValue();
22530b57cec5SDimitry Andric   if ((Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_GT) ||
22540b57cec5SDimitry Andric       (Value == -1 && C.CCMask == SystemZ::CCMASK_CMP_LE) ||
22550b57cec5SDimitry Andric       (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_LT) ||
22560b57cec5SDimitry Andric       (Value == 1 && C.CCMask == SystemZ::CCMASK_CMP_GE)) {
22570b57cec5SDimitry Andric     C.CCMask ^= SystemZ::CCMASK_CMP_EQ;
22580b57cec5SDimitry Andric     C.Op1 = DAG.getConstant(0, DL, C.Op1.getValueType());
22590b57cec5SDimitry Andric   }
22600b57cec5SDimitry Andric }
22610b57cec5SDimitry Andric 
22620b57cec5SDimitry Andric // If a comparison described by C is suitable for CLI(Y), CHHSI or CLHHSI,
22630b57cec5SDimitry Andric // adjust the operands as necessary.
22640b57cec5SDimitry Andric static void adjustSubwordCmp(SelectionDAG &DAG, const SDLoc &DL,
22650b57cec5SDimitry Andric                              Comparison &C) {
22660b57cec5SDimitry Andric   // For us to make any changes, it must a comparison between a single-use
22670b57cec5SDimitry Andric   // load and a constant.
22680b57cec5SDimitry Andric   if (!C.Op0.hasOneUse() ||
22690b57cec5SDimitry Andric       C.Op0.getOpcode() != ISD::LOAD ||
22700b57cec5SDimitry Andric       C.Op1.getOpcode() != ISD::Constant)
22710b57cec5SDimitry Andric     return;
22720b57cec5SDimitry Andric 
22730b57cec5SDimitry Andric   // We must have an 8- or 16-bit load.
22740b57cec5SDimitry Andric   auto *Load = cast<LoadSDNode>(C.Op0);
22755ffd83dbSDimitry Andric   unsigned NumBits = Load->getMemoryVT().getSizeInBits();
22765ffd83dbSDimitry Andric   if ((NumBits != 8 && NumBits != 16) ||
22775ffd83dbSDimitry Andric       NumBits != Load->getMemoryVT().getStoreSizeInBits())
22780b57cec5SDimitry Andric     return;
22790b57cec5SDimitry Andric 
22800b57cec5SDimitry Andric   // The load must be an extending one and the constant must be within the
22810b57cec5SDimitry Andric   // range of the unextended value.
22820b57cec5SDimitry Andric   auto *ConstOp1 = cast<ConstantSDNode>(C.Op1);
22830b57cec5SDimitry Andric   uint64_t Value = ConstOp1->getZExtValue();
22840b57cec5SDimitry Andric   uint64_t Mask = (1 << NumBits) - 1;
22850b57cec5SDimitry Andric   if (Load->getExtensionType() == ISD::SEXTLOAD) {
22860b57cec5SDimitry Andric     // Make sure that ConstOp1 is in range of C.Op0.
22870b57cec5SDimitry Andric     int64_t SignedValue = ConstOp1->getSExtValue();
22880b57cec5SDimitry Andric     if (uint64_t(SignedValue) + (uint64_t(1) << (NumBits - 1)) > Mask)
22890b57cec5SDimitry Andric       return;
22900b57cec5SDimitry Andric     if (C.ICmpType != SystemZICMP::SignedOnly) {
22910b57cec5SDimitry Andric       // Unsigned comparison between two sign-extended values is equivalent
22920b57cec5SDimitry Andric       // to unsigned comparison between two zero-extended values.
22930b57cec5SDimitry Andric       Value &= Mask;
22940b57cec5SDimitry Andric     } else if (NumBits == 8) {
22950b57cec5SDimitry Andric       // Try to treat the comparison as unsigned, so that we can use CLI.
22960b57cec5SDimitry Andric       // Adjust CCMask and Value as necessary.
22970b57cec5SDimitry Andric       if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_LT)
22980b57cec5SDimitry Andric         // Test whether the high bit of the byte is set.
22990b57cec5SDimitry Andric         Value = 127, C.CCMask = SystemZ::CCMASK_CMP_GT;
23000b57cec5SDimitry Andric       else if (Value == 0 && C.CCMask == SystemZ::CCMASK_CMP_GE)
23010b57cec5SDimitry Andric         // Test whether the high bit of the byte is clear.
23020b57cec5SDimitry Andric         Value = 128, C.CCMask = SystemZ::CCMASK_CMP_LT;
23030b57cec5SDimitry Andric       else
23040b57cec5SDimitry Andric         // No instruction exists for this combination.
23050b57cec5SDimitry Andric         return;
23060b57cec5SDimitry Andric       C.ICmpType = SystemZICMP::UnsignedOnly;
23070b57cec5SDimitry Andric     }
23080b57cec5SDimitry Andric   } else if (Load->getExtensionType() == ISD::ZEXTLOAD) {
23090b57cec5SDimitry Andric     if (Value > Mask)
23100b57cec5SDimitry Andric       return;
23110b57cec5SDimitry Andric     // If the constant is in range, we can use any comparison.
23120b57cec5SDimitry Andric     C.ICmpType = SystemZICMP::Any;
23130b57cec5SDimitry Andric   } else
23140b57cec5SDimitry Andric     return;
23150b57cec5SDimitry Andric 
23160b57cec5SDimitry Andric   // Make sure that the first operand is an i32 of the right extension type.
23170b57cec5SDimitry Andric   ISD::LoadExtType ExtType = (C.ICmpType == SystemZICMP::SignedOnly ?
23180b57cec5SDimitry Andric                               ISD::SEXTLOAD :
23190b57cec5SDimitry Andric                               ISD::ZEXTLOAD);
23200b57cec5SDimitry Andric   if (C.Op0.getValueType() != MVT::i32 ||
23210b57cec5SDimitry Andric       Load->getExtensionType() != ExtType) {
23220b57cec5SDimitry Andric     C.Op0 = DAG.getExtLoad(ExtType, SDLoc(Load), MVT::i32, Load->getChain(),
23230b57cec5SDimitry Andric                            Load->getBasePtr(), Load->getPointerInfo(),
232481ad6265SDimitry Andric                            Load->getMemoryVT(), Load->getAlign(),
23250b57cec5SDimitry Andric                            Load->getMemOperand()->getFlags());
23260b57cec5SDimitry Andric     // Update the chain uses.
23270b57cec5SDimitry Andric     DAG.ReplaceAllUsesOfValueWith(SDValue(Load, 1), C.Op0.getValue(1));
23280b57cec5SDimitry Andric   }
23290b57cec5SDimitry Andric 
23300b57cec5SDimitry Andric   // Make sure that the second operand is an i32 with the right value.
23310b57cec5SDimitry Andric   if (C.Op1.getValueType() != MVT::i32 ||
23320b57cec5SDimitry Andric       Value != ConstOp1->getZExtValue())
23330b57cec5SDimitry Andric     C.Op1 = DAG.getConstant(Value, DL, MVT::i32);
23340b57cec5SDimitry Andric }
23350b57cec5SDimitry Andric 
23360b57cec5SDimitry Andric // Return true if Op is either an unextended load, or a load suitable
23370b57cec5SDimitry Andric // for integer register-memory comparisons of type ICmpType.
23380b57cec5SDimitry Andric static bool isNaturalMemoryOperand(SDValue Op, unsigned ICmpType) {
23390b57cec5SDimitry Andric   auto *Load = dyn_cast<LoadSDNode>(Op.getNode());
23400b57cec5SDimitry Andric   if (Load) {
23410b57cec5SDimitry Andric     // There are no instructions to compare a register with a memory byte.
23420b57cec5SDimitry Andric     if (Load->getMemoryVT() == MVT::i8)
23430b57cec5SDimitry Andric       return false;
23440b57cec5SDimitry Andric     // Otherwise decide on extension type.
23450b57cec5SDimitry Andric     switch (Load->getExtensionType()) {
23460b57cec5SDimitry Andric     case ISD::NON_EXTLOAD:
23470b57cec5SDimitry Andric       return true;
23480b57cec5SDimitry Andric     case ISD::SEXTLOAD:
23490b57cec5SDimitry Andric       return ICmpType != SystemZICMP::UnsignedOnly;
23500b57cec5SDimitry Andric     case ISD::ZEXTLOAD:
23510b57cec5SDimitry Andric       return ICmpType != SystemZICMP::SignedOnly;
23520b57cec5SDimitry Andric     default:
23530b57cec5SDimitry Andric       break;
23540b57cec5SDimitry Andric     }
23550b57cec5SDimitry Andric   }
23560b57cec5SDimitry Andric   return false;
23570b57cec5SDimitry Andric }
23580b57cec5SDimitry Andric 
23590b57cec5SDimitry Andric // Return true if it is better to swap the operands of C.
23600b57cec5SDimitry Andric static bool shouldSwapCmpOperands(const Comparison &C) {
23610b57cec5SDimitry Andric   // Leave f128 comparisons alone, since they have no memory forms.
23620b57cec5SDimitry Andric   if (C.Op0.getValueType() == MVT::f128)
23630b57cec5SDimitry Andric     return false;
23640b57cec5SDimitry Andric 
23650b57cec5SDimitry Andric   // Always keep a floating-point constant second, since comparisons with
23660b57cec5SDimitry Andric   // zero can use LOAD TEST and comparisons with other constants make a
23670b57cec5SDimitry Andric   // natural memory operand.
23680b57cec5SDimitry Andric   if (isa<ConstantFPSDNode>(C.Op1))
23690b57cec5SDimitry Andric     return false;
23700b57cec5SDimitry Andric 
23710b57cec5SDimitry Andric   // Never swap comparisons with zero since there are many ways to optimize
23720b57cec5SDimitry Andric   // those later.
23730b57cec5SDimitry Andric   auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
23740b57cec5SDimitry Andric   if (ConstOp1 && ConstOp1->getZExtValue() == 0)
23750b57cec5SDimitry Andric     return false;
23760b57cec5SDimitry Andric 
23770b57cec5SDimitry Andric   // Also keep natural memory operands second if the loaded value is
23780b57cec5SDimitry Andric   // only used here.  Several comparisons have memory forms.
23790b57cec5SDimitry Andric   if (isNaturalMemoryOperand(C.Op1, C.ICmpType) && C.Op1.hasOneUse())
23800b57cec5SDimitry Andric     return false;
23810b57cec5SDimitry Andric 
23820b57cec5SDimitry Andric   // Look for cases where Cmp0 is a single-use load and Cmp1 isn't.
23830b57cec5SDimitry Andric   // In that case we generally prefer the memory to be second.
23840b57cec5SDimitry Andric   if (isNaturalMemoryOperand(C.Op0, C.ICmpType) && C.Op0.hasOneUse()) {
23850b57cec5SDimitry Andric     // The only exceptions are when the second operand is a constant and
23860b57cec5SDimitry Andric     // we can use things like CHHSI.
23870b57cec5SDimitry Andric     if (!ConstOp1)
23880b57cec5SDimitry Andric       return true;
23890b57cec5SDimitry Andric     // The unsigned memory-immediate instructions can handle 16-bit
23900b57cec5SDimitry Andric     // unsigned integers.
23910b57cec5SDimitry Andric     if (C.ICmpType != SystemZICMP::SignedOnly &&
23920b57cec5SDimitry Andric         isUInt<16>(ConstOp1->getZExtValue()))
23930b57cec5SDimitry Andric       return false;
23940b57cec5SDimitry Andric     // The signed memory-immediate instructions can handle 16-bit
23950b57cec5SDimitry Andric     // signed integers.
23960b57cec5SDimitry Andric     if (C.ICmpType != SystemZICMP::UnsignedOnly &&
23970b57cec5SDimitry Andric         isInt<16>(ConstOp1->getSExtValue()))
23980b57cec5SDimitry Andric       return false;
23990b57cec5SDimitry Andric     return true;
24000b57cec5SDimitry Andric   }
24010b57cec5SDimitry Andric 
24020b57cec5SDimitry Andric   // Try to promote the use of CGFR and CLGFR.
24030b57cec5SDimitry Andric   unsigned Opcode0 = C.Op0.getOpcode();
24040b57cec5SDimitry Andric   if (C.ICmpType != SystemZICMP::UnsignedOnly && Opcode0 == ISD::SIGN_EXTEND)
24050b57cec5SDimitry Andric     return true;
24060b57cec5SDimitry Andric   if (C.ICmpType != SystemZICMP::SignedOnly && Opcode0 == ISD::ZERO_EXTEND)
24070b57cec5SDimitry Andric     return true;
24080b57cec5SDimitry Andric   if (C.ICmpType != SystemZICMP::SignedOnly &&
24090b57cec5SDimitry Andric       Opcode0 == ISD::AND &&
24100b57cec5SDimitry Andric       C.Op0.getOperand(1).getOpcode() == ISD::Constant &&
24110b57cec5SDimitry Andric       cast<ConstantSDNode>(C.Op0.getOperand(1))->getZExtValue() == 0xffffffff)
24120b57cec5SDimitry Andric     return true;
24130b57cec5SDimitry Andric 
24140b57cec5SDimitry Andric   return false;
24150b57cec5SDimitry Andric }
24160b57cec5SDimitry Andric 
24170b57cec5SDimitry Andric // Check whether C tests for equality between X and Y and whether X - Y
24180b57cec5SDimitry Andric // or Y - X is also computed.  In that case it's better to compare the
24190b57cec5SDimitry Andric // result of the subtraction against zero.
24200b57cec5SDimitry Andric static void adjustForSubtraction(SelectionDAG &DAG, const SDLoc &DL,
24210b57cec5SDimitry Andric                                  Comparison &C) {
24220b57cec5SDimitry Andric   if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
24230b57cec5SDimitry Andric       C.CCMask == SystemZ::CCMASK_CMP_NE) {
2424349cc55cSDimitry Andric     for (SDNode *N : C.Op0->uses()) {
24250b57cec5SDimitry Andric       if (N->getOpcode() == ISD::SUB &&
24260b57cec5SDimitry Andric           ((N->getOperand(0) == C.Op0 && N->getOperand(1) == C.Op1) ||
24270b57cec5SDimitry Andric            (N->getOperand(0) == C.Op1 && N->getOperand(1) == C.Op0))) {
24280b57cec5SDimitry Andric         C.Op0 = SDValue(N, 0);
24290b57cec5SDimitry Andric         C.Op1 = DAG.getConstant(0, DL, N->getValueType(0));
24300b57cec5SDimitry Andric         return;
24310b57cec5SDimitry Andric       }
24320b57cec5SDimitry Andric     }
24330b57cec5SDimitry Andric   }
24340b57cec5SDimitry Andric }
24350b57cec5SDimitry Andric 
24360b57cec5SDimitry Andric // Check whether C compares a floating-point value with zero and if that
24370b57cec5SDimitry Andric // floating-point value is also negated.  In this case we can use the
24380b57cec5SDimitry Andric // negation to set CC, so avoiding separate LOAD AND TEST and
24390b57cec5SDimitry Andric // LOAD (NEGATIVE/COMPLEMENT) instructions.
24400b57cec5SDimitry Andric static void adjustForFNeg(Comparison &C) {
2441480093f4SDimitry Andric   // This optimization is invalid for strict comparisons, since FNEG
2442480093f4SDimitry Andric   // does not raise any exceptions.
2443480093f4SDimitry Andric   if (C.Chain)
2444480093f4SDimitry Andric     return;
24450b57cec5SDimitry Andric   auto *C1 = dyn_cast<ConstantFPSDNode>(C.Op1);
24460b57cec5SDimitry Andric   if (C1 && C1->isZero()) {
2447349cc55cSDimitry Andric     for (SDNode *N : C.Op0->uses()) {
24480b57cec5SDimitry Andric       if (N->getOpcode() == ISD::FNEG) {
24490b57cec5SDimitry Andric         C.Op0 = SDValue(N, 0);
24505ffd83dbSDimitry Andric         C.CCMask = SystemZ::reverseCCMask(C.CCMask);
24510b57cec5SDimitry Andric         return;
24520b57cec5SDimitry Andric       }
24530b57cec5SDimitry Andric     }
24540b57cec5SDimitry Andric   }
24550b57cec5SDimitry Andric }
24560b57cec5SDimitry Andric 
24570b57cec5SDimitry Andric // Check whether C compares (shl X, 32) with 0 and whether X is
24580b57cec5SDimitry Andric // also sign-extended.  In that case it is better to test the result
24590b57cec5SDimitry Andric // of the sign extension using LTGFR.
24600b57cec5SDimitry Andric //
24610b57cec5SDimitry Andric // This case is important because InstCombine transforms a comparison
24620b57cec5SDimitry Andric // with (sext (trunc X)) into a comparison with (shl X, 32).
24630b57cec5SDimitry Andric static void adjustForLTGFR(Comparison &C) {
24640b57cec5SDimitry Andric   // Check for a comparison between (shl X, 32) and 0.
24650b57cec5SDimitry Andric   if (C.Op0.getOpcode() == ISD::SHL &&
24660b57cec5SDimitry Andric       C.Op0.getValueType() == MVT::i64 &&
24670b57cec5SDimitry Andric       C.Op1.getOpcode() == ISD::Constant &&
24680b57cec5SDimitry Andric       cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
24690b57cec5SDimitry Andric     auto *C1 = dyn_cast<ConstantSDNode>(C.Op0.getOperand(1));
24700b57cec5SDimitry Andric     if (C1 && C1->getZExtValue() == 32) {
24710b57cec5SDimitry Andric       SDValue ShlOp0 = C.Op0.getOperand(0);
24720b57cec5SDimitry Andric       // See whether X has any SIGN_EXTEND_INREG uses.
2473349cc55cSDimitry Andric       for (SDNode *N : ShlOp0->uses()) {
24740b57cec5SDimitry Andric         if (N->getOpcode() == ISD::SIGN_EXTEND_INREG &&
24750b57cec5SDimitry Andric             cast<VTSDNode>(N->getOperand(1))->getVT() == MVT::i32) {
24760b57cec5SDimitry Andric           C.Op0 = SDValue(N, 0);
24770b57cec5SDimitry Andric           return;
24780b57cec5SDimitry Andric         }
24790b57cec5SDimitry Andric       }
24800b57cec5SDimitry Andric     }
24810b57cec5SDimitry Andric   }
24820b57cec5SDimitry Andric }
24830b57cec5SDimitry Andric 
24840b57cec5SDimitry Andric // If C compares the truncation of an extending load, try to compare
24850b57cec5SDimitry Andric // the untruncated value instead.  This exposes more opportunities to
24860b57cec5SDimitry Andric // reuse CC.
24870b57cec5SDimitry Andric static void adjustICmpTruncate(SelectionDAG &DAG, const SDLoc &DL,
24880b57cec5SDimitry Andric                                Comparison &C) {
24890b57cec5SDimitry Andric   if (C.Op0.getOpcode() == ISD::TRUNCATE &&
24900b57cec5SDimitry Andric       C.Op0.getOperand(0).getOpcode() == ISD::LOAD &&
24910b57cec5SDimitry Andric       C.Op1.getOpcode() == ISD::Constant &&
24920b57cec5SDimitry Andric       cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
24930b57cec5SDimitry Andric     auto *L = cast<LoadSDNode>(C.Op0.getOperand(0));
2494bdd1243dSDimitry Andric     if (L->getMemoryVT().getStoreSizeInBits().getFixedValue() <=
2495bdd1243dSDimitry Andric         C.Op0.getValueSizeInBits().getFixedValue()) {
24960b57cec5SDimitry Andric       unsigned Type = L->getExtensionType();
24970b57cec5SDimitry Andric       if ((Type == ISD::ZEXTLOAD && C.ICmpType != SystemZICMP::SignedOnly) ||
24980b57cec5SDimitry Andric           (Type == ISD::SEXTLOAD && C.ICmpType != SystemZICMP::UnsignedOnly)) {
24990b57cec5SDimitry Andric         C.Op0 = C.Op0.getOperand(0);
25000b57cec5SDimitry Andric         C.Op1 = DAG.getConstant(0, DL, C.Op0.getValueType());
25010b57cec5SDimitry Andric       }
25020b57cec5SDimitry Andric     }
25030b57cec5SDimitry Andric   }
25040b57cec5SDimitry Andric }
25050b57cec5SDimitry Andric 
25060b57cec5SDimitry Andric // Return true if shift operation N has an in-range constant shift value.
25070b57cec5SDimitry Andric // Store it in ShiftVal if so.
25080b57cec5SDimitry Andric static bool isSimpleShift(SDValue N, unsigned &ShiftVal) {
25090b57cec5SDimitry Andric   auto *Shift = dyn_cast<ConstantSDNode>(N.getOperand(1));
25100b57cec5SDimitry Andric   if (!Shift)
25110b57cec5SDimitry Andric     return false;
25120b57cec5SDimitry Andric 
25130b57cec5SDimitry Andric   uint64_t Amount = Shift->getZExtValue();
25140b57cec5SDimitry Andric   if (Amount >= N.getValueSizeInBits())
25150b57cec5SDimitry Andric     return false;
25160b57cec5SDimitry Andric 
25170b57cec5SDimitry Andric   ShiftVal = Amount;
25180b57cec5SDimitry Andric   return true;
25190b57cec5SDimitry Andric }
25200b57cec5SDimitry Andric 
25210b57cec5SDimitry Andric // Check whether an AND with Mask is suitable for a TEST UNDER MASK
25220b57cec5SDimitry Andric // instruction and whether the CC value is descriptive enough to handle
25230b57cec5SDimitry Andric // a comparison of type Opcode between the AND result and CmpVal.
25240b57cec5SDimitry Andric // CCMask says which comparison result is being tested and BitSize is
25250b57cec5SDimitry Andric // the number of bits in the operands.  If TEST UNDER MASK can be used,
25260b57cec5SDimitry Andric // return the corresponding CC mask, otherwise return 0.
25270b57cec5SDimitry Andric static unsigned getTestUnderMaskCond(unsigned BitSize, unsigned CCMask,
25280b57cec5SDimitry Andric                                      uint64_t Mask, uint64_t CmpVal,
25290b57cec5SDimitry Andric                                      unsigned ICmpType) {
25300b57cec5SDimitry Andric   assert(Mask != 0 && "ANDs with zero should have been removed by now");
25310b57cec5SDimitry Andric 
25320b57cec5SDimitry Andric   // Check whether the mask is suitable for TMHH, TMHL, TMLH or TMLL.
25330b57cec5SDimitry Andric   if (!SystemZ::isImmLL(Mask) && !SystemZ::isImmLH(Mask) &&
25340b57cec5SDimitry Andric       !SystemZ::isImmHL(Mask) && !SystemZ::isImmHH(Mask))
25350b57cec5SDimitry Andric     return 0;
25360b57cec5SDimitry Andric 
25370b57cec5SDimitry Andric   // Work out the masks for the lowest and highest bits.
25380b57cec5SDimitry Andric   unsigned HighShift = 63 - countLeadingZeros(Mask);
25390b57cec5SDimitry Andric   uint64_t High = uint64_t(1) << HighShift;
25400b57cec5SDimitry Andric   uint64_t Low = uint64_t(1) << countTrailingZeros(Mask);
25410b57cec5SDimitry Andric 
25420b57cec5SDimitry Andric   // Signed ordered comparisons are effectively unsigned if the sign
25430b57cec5SDimitry Andric   // bit is dropped.
25440b57cec5SDimitry Andric   bool EffectivelyUnsigned = (ICmpType != SystemZICMP::SignedOnly);
25450b57cec5SDimitry Andric 
25460b57cec5SDimitry Andric   // Check for equality comparisons with 0, or the equivalent.
25470b57cec5SDimitry Andric   if (CmpVal == 0) {
25480b57cec5SDimitry Andric     if (CCMask == SystemZ::CCMASK_CMP_EQ)
25490b57cec5SDimitry Andric       return SystemZ::CCMASK_TM_ALL_0;
25500b57cec5SDimitry Andric     if (CCMask == SystemZ::CCMASK_CMP_NE)
25510b57cec5SDimitry Andric       return SystemZ::CCMASK_TM_SOME_1;
25520b57cec5SDimitry Andric   }
25530b57cec5SDimitry Andric   if (EffectivelyUnsigned && CmpVal > 0 && CmpVal <= Low) {
25540b57cec5SDimitry Andric     if (CCMask == SystemZ::CCMASK_CMP_LT)
25550b57cec5SDimitry Andric       return SystemZ::CCMASK_TM_ALL_0;
25560b57cec5SDimitry Andric     if (CCMask == SystemZ::CCMASK_CMP_GE)
25570b57cec5SDimitry Andric       return SystemZ::CCMASK_TM_SOME_1;
25580b57cec5SDimitry Andric   }
25590b57cec5SDimitry Andric   if (EffectivelyUnsigned && CmpVal < Low) {
25600b57cec5SDimitry Andric     if (CCMask == SystemZ::CCMASK_CMP_LE)
25610b57cec5SDimitry Andric       return SystemZ::CCMASK_TM_ALL_0;
25620b57cec5SDimitry Andric     if (CCMask == SystemZ::CCMASK_CMP_GT)
25630b57cec5SDimitry Andric       return SystemZ::CCMASK_TM_SOME_1;
25640b57cec5SDimitry Andric   }
25650b57cec5SDimitry Andric 
25660b57cec5SDimitry Andric   // Check for equality comparisons with the mask, or the equivalent.
25670b57cec5SDimitry Andric   if (CmpVal == Mask) {
25680b57cec5SDimitry Andric     if (CCMask == SystemZ::CCMASK_CMP_EQ)
25690b57cec5SDimitry Andric       return SystemZ::CCMASK_TM_ALL_1;
25700b57cec5SDimitry Andric     if (CCMask == SystemZ::CCMASK_CMP_NE)
25710b57cec5SDimitry Andric       return SystemZ::CCMASK_TM_SOME_0;
25720b57cec5SDimitry Andric   }
25730b57cec5SDimitry Andric   if (EffectivelyUnsigned && CmpVal >= Mask - Low && CmpVal < Mask) {
25740b57cec5SDimitry Andric     if (CCMask == SystemZ::CCMASK_CMP_GT)
25750b57cec5SDimitry Andric       return SystemZ::CCMASK_TM_ALL_1;
25760b57cec5SDimitry Andric     if (CCMask == SystemZ::CCMASK_CMP_LE)
25770b57cec5SDimitry Andric       return SystemZ::CCMASK_TM_SOME_0;
25780b57cec5SDimitry Andric   }
25790b57cec5SDimitry Andric   if (EffectivelyUnsigned && CmpVal > Mask - Low && CmpVal <= Mask) {
25800b57cec5SDimitry Andric     if (CCMask == SystemZ::CCMASK_CMP_GE)
25810b57cec5SDimitry Andric       return SystemZ::CCMASK_TM_ALL_1;
25820b57cec5SDimitry Andric     if (CCMask == SystemZ::CCMASK_CMP_LT)
25830b57cec5SDimitry Andric       return SystemZ::CCMASK_TM_SOME_0;
25840b57cec5SDimitry Andric   }
25850b57cec5SDimitry Andric 
25860b57cec5SDimitry Andric   // Check for ordered comparisons with the top bit.
25870b57cec5SDimitry Andric   if (EffectivelyUnsigned && CmpVal >= Mask - High && CmpVal < High) {
25880b57cec5SDimitry Andric     if (CCMask == SystemZ::CCMASK_CMP_LE)
25890b57cec5SDimitry Andric       return SystemZ::CCMASK_TM_MSB_0;
25900b57cec5SDimitry Andric     if (CCMask == SystemZ::CCMASK_CMP_GT)
25910b57cec5SDimitry Andric       return SystemZ::CCMASK_TM_MSB_1;
25920b57cec5SDimitry Andric   }
25930b57cec5SDimitry Andric   if (EffectivelyUnsigned && CmpVal > Mask - High && CmpVal <= High) {
25940b57cec5SDimitry Andric     if (CCMask == SystemZ::CCMASK_CMP_LT)
25950b57cec5SDimitry Andric       return SystemZ::CCMASK_TM_MSB_0;
25960b57cec5SDimitry Andric     if (CCMask == SystemZ::CCMASK_CMP_GE)
25970b57cec5SDimitry Andric       return SystemZ::CCMASK_TM_MSB_1;
25980b57cec5SDimitry Andric   }
25990b57cec5SDimitry Andric 
26000b57cec5SDimitry Andric   // If there are just two bits, we can do equality checks for Low and High
26010b57cec5SDimitry Andric   // as well.
26020b57cec5SDimitry Andric   if (Mask == Low + High) {
26030b57cec5SDimitry Andric     if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == Low)
26040b57cec5SDimitry Andric       return SystemZ::CCMASK_TM_MIXED_MSB_0;
26050b57cec5SDimitry Andric     if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == Low)
26060b57cec5SDimitry Andric       return SystemZ::CCMASK_TM_MIXED_MSB_0 ^ SystemZ::CCMASK_ANY;
26070b57cec5SDimitry Andric     if (CCMask == SystemZ::CCMASK_CMP_EQ && CmpVal == High)
26080b57cec5SDimitry Andric       return SystemZ::CCMASK_TM_MIXED_MSB_1;
26090b57cec5SDimitry Andric     if (CCMask == SystemZ::CCMASK_CMP_NE && CmpVal == High)
26100b57cec5SDimitry Andric       return SystemZ::CCMASK_TM_MIXED_MSB_1 ^ SystemZ::CCMASK_ANY;
26110b57cec5SDimitry Andric   }
26120b57cec5SDimitry Andric 
26130b57cec5SDimitry Andric   // Looks like we've exhausted our options.
26140b57cec5SDimitry Andric   return 0;
26150b57cec5SDimitry Andric }
26160b57cec5SDimitry Andric 
26170b57cec5SDimitry Andric // See whether C can be implemented as a TEST UNDER MASK instruction.
26180b57cec5SDimitry Andric // Update the arguments with the TM version if so.
26190b57cec5SDimitry Andric static void adjustForTestUnderMask(SelectionDAG &DAG, const SDLoc &DL,
26200b57cec5SDimitry Andric                                    Comparison &C) {
26210b57cec5SDimitry Andric   // Check that we have a comparison with a constant.
26220b57cec5SDimitry Andric   auto *ConstOp1 = dyn_cast<ConstantSDNode>(C.Op1);
26230b57cec5SDimitry Andric   if (!ConstOp1)
26240b57cec5SDimitry Andric     return;
26250b57cec5SDimitry Andric   uint64_t CmpVal = ConstOp1->getZExtValue();
26260b57cec5SDimitry Andric 
26270b57cec5SDimitry Andric   // Check whether the nonconstant input is an AND with a constant mask.
26280b57cec5SDimitry Andric   Comparison NewC(C);
26290b57cec5SDimitry Andric   uint64_t MaskVal;
26300b57cec5SDimitry Andric   ConstantSDNode *Mask = nullptr;
26310b57cec5SDimitry Andric   if (C.Op0.getOpcode() == ISD::AND) {
26320b57cec5SDimitry Andric     NewC.Op0 = C.Op0.getOperand(0);
26330b57cec5SDimitry Andric     NewC.Op1 = C.Op0.getOperand(1);
26340b57cec5SDimitry Andric     Mask = dyn_cast<ConstantSDNode>(NewC.Op1);
26350b57cec5SDimitry Andric     if (!Mask)
26360b57cec5SDimitry Andric       return;
26370b57cec5SDimitry Andric     MaskVal = Mask->getZExtValue();
26380b57cec5SDimitry Andric   } else {
26390b57cec5SDimitry Andric     // There is no instruction to compare with a 64-bit immediate
26400b57cec5SDimitry Andric     // so use TMHH instead if possible.  We need an unsigned ordered
26410b57cec5SDimitry Andric     // comparison with an i64 immediate.
26420b57cec5SDimitry Andric     if (NewC.Op0.getValueType() != MVT::i64 ||
26430b57cec5SDimitry Andric         NewC.CCMask == SystemZ::CCMASK_CMP_EQ ||
26440b57cec5SDimitry Andric         NewC.CCMask == SystemZ::CCMASK_CMP_NE ||
26450b57cec5SDimitry Andric         NewC.ICmpType == SystemZICMP::SignedOnly)
26460b57cec5SDimitry Andric       return;
26470b57cec5SDimitry Andric     // Convert LE and GT comparisons into LT and GE.
26480b57cec5SDimitry Andric     if (NewC.CCMask == SystemZ::CCMASK_CMP_LE ||
26490b57cec5SDimitry Andric         NewC.CCMask == SystemZ::CCMASK_CMP_GT) {
26500b57cec5SDimitry Andric       if (CmpVal == uint64_t(-1))
26510b57cec5SDimitry Andric         return;
26520b57cec5SDimitry Andric       CmpVal += 1;
26530b57cec5SDimitry Andric       NewC.CCMask ^= SystemZ::CCMASK_CMP_EQ;
26540b57cec5SDimitry Andric     }
26550b57cec5SDimitry Andric     // If the low N bits of Op1 are zero than the low N bits of Op0 can
26560b57cec5SDimitry Andric     // be masked off without changing the result.
26570b57cec5SDimitry Andric     MaskVal = -(CmpVal & -CmpVal);
26580b57cec5SDimitry Andric     NewC.ICmpType = SystemZICMP::UnsignedOnly;
26590b57cec5SDimitry Andric   }
26600b57cec5SDimitry Andric   if (!MaskVal)
26610b57cec5SDimitry Andric     return;
26620b57cec5SDimitry Andric 
26630b57cec5SDimitry Andric   // Check whether the combination of mask, comparison value and comparison
26640b57cec5SDimitry Andric   // type are suitable.
26650b57cec5SDimitry Andric   unsigned BitSize = NewC.Op0.getValueSizeInBits();
26660b57cec5SDimitry Andric   unsigned NewCCMask, ShiftVal;
26670b57cec5SDimitry Andric   if (NewC.ICmpType != SystemZICMP::SignedOnly &&
26680b57cec5SDimitry Andric       NewC.Op0.getOpcode() == ISD::SHL &&
26690b57cec5SDimitry Andric       isSimpleShift(NewC.Op0, ShiftVal) &&
26700b57cec5SDimitry Andric       (MaskVal >> ShiftVal != 0) &&
26710b57cec5SDimitry Andric       ((CmpVal >> ShiftVal) << ShiftVal) == CmpVal &&
26720b57cec5SDimitry Andric       (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
26730b57cec5SDimitry Andric                                         MaskVal >> ShiftVal,
26740b57cec5SDimitry Andric                                         CmpVal >> ShiftVal,
26750b57cec5SDimitry Andric                                         SystemZICMP::Any))) {
26760b57cec5SDimitry Andric     NewC.Op0 = NewC.Op0.getOperand(0);
26770b57cec5SDimitry Andric     MaskVal >>= ShiftVal;
26780b57cec5SDimitry Andric   } else if (NewC.ICmpType != SystemZICMP::SignedOnly &&
26790b57cec5SDimitry Andric              NewC.Op0.getOpcode() == ISD::SRL &&
26800b57cec5SDimitry Andric              isSimpleShift(NewC.Op0, ShiftVal) &&
26810b57cec5SDimitry Andric              (MaskVal << ShiftVal != 0) &&
26820b57cec5SDimitry Andric              ((CmpVal << ShiftVal) >> ShiftVal) == CmpVal &&
26830b57cec5SDimitry Andric              (NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask,
26840b57cec5SDimitry Andric                                                MaskVal << ShiftVal,
26850b57cec5SDimitry Andric                                                CmpVal << ShiftVal,
26860b57cec5SDimitry Andric                                                SystemZICMP::UnsignedOnly))) {
26870b57cec5SDimitry Andric     NewC.Op0 = NewC.Op0.getOperand(0);
26880b57cec5SDimitry Andric     MaskVal <<= ShiftVal;
26890b57cec5SDimitry Andric   } else {
26900b57cec5SDimitry Andric     NewCCMask = getTestUnderMaskCond(BitSize, NewC.CCMask, MaskVal, CmpVal,
26910b57cec5SDimitry Andric                                      NewC.ICmpType);
26920b57cec5SDimitry Andric     if (!NewCCMask)
26930b57cec5SDimitry Andric       return;
26940b57cec5SDimitry Andric   }
26950b57cec5SDimitry Andric 
26960b57cec5SDimitry Andric   // Go ahead and make the change.
26970b57cec5SDimitry Andric   C.Opcode = SystemZISD::TM;
26980b57cec5SDimitry Andric   C.Op0 = NewC.Op0;
26990b57cec5SDimitry Andric   if (Mask && Mask->getZExtValue() == MaskVal)
27000b57cec5SDimitry Andric     C.Op1 = SDValue(Mask, 0);
27010b57cec5SDimitry Andric   else
27020b57cec5SDimitry Andric     C.Op1 = DAG.getConstant(MaskVal, DL, C.Op0.getValueType());
27030b57cec5SDimitry Andric   C.CCValid = SystemZ::CCMASK_TM;
27040b57cec5SDimitry Andric   C.CCMask = NewCCMask;
27050b57cec5SDimitry Andric }
27060b57cec5SDimitry Andric 
27070b57cec5SDimitry Andric // See whether the comparison argument contains a redundant AND
27080b57cec5SDimitry Andric // and remove it if so.  This sometimes happens due to the generic
27090b57cec5SDimitry Andric // BRCOND expansion.
27100b57cec5SDimitry Andric static void adjustForRedundantAnd(SelectionDAG &DAG, const SDLoc &DL,
27110b57cec5SDimitry Andric                                   Comparison &C) {
27120b57cec5SDimitry Andric   if (C.Op0.getOpcode() != ISD::AND)
27130b57cec5SDimitry Andric     return;
27140b57cec5SDimitry Andric   auto *Mask = dyn_cast<ConstantSDNode>(C.Op0.getOperand(1));
27150b57cec5SDimitry Andric   if (!Mask)
27160b57cec5SDimitry Andric     return;
27170b57cec5SDimitry Andric   KnownBits Known = DAG.computeKnownBits(C.Op0.getOperand(0));
27180b57cec5SDimitry Andric   if ((~Known.Zero).getZExtValue() & ~Mask->getZExtValue())
27190b57cec5SDimitry Andric     return;
27200b57cec5SDimitry Andric 
27210b57cec5SDimitry Andric   C.Op0 = C.Op0.getOperand(0);
27220b57cec5SDimitry Andric }
27230b57cec5SDimitry Andric 
27240b57cec5SDimitry Andric // Return a Comparison that tests the condition-code result of intrinsic
27250b57cec5SDimitry Andric // node Call against constant integer CC using comparison code Cond.
27260b57cec5SDimitry Andric // Opcode is the opcode of the SystemZISD operation for the intrinsic
27270b57cec5SDimitry Andric // and CCValid is the set of possible condition-code results.
27280b57cec5SDimitry Andric static Comparison getIntrinsicCmp(SelectionDAG &DAG, unsigned Opcode,
27290b57cec5SDimitry Andric                                   SDValue Call, unsigned CCValid, uint64_t CC,
27300b57cec5SDimitry Andric                                   ISD::CondCode Cond) {
2731480093f4SDimitry Andric   Comparison C(Call, SDValue(), SDValue());
27320b57cec5SDimitry Andric   C.Opcode = Opcode;
27330b57cec5SDimitry Andric   C.CCValid = CCValid;
27340b57cec5SDimitry Andric   if (Cond == ISD::SETEQ)
27350b57cec5SDimitry Andric     // bit 3 for CC==0, bit 0 for CC==3, always false for CC>3.
27360b57cec5SDimitry Andric     C.CCMask = CC < 4 ? 1 << (3 - CC) : 0;
27370b57cec5SDimitry Andric   else if (Cond == ISD::SETNE)
27380b57cec5SDimitry Andric     // ...and the inverse of that.
27390b57cec5SDimitry Andric     C.CCMask = CC < 4 ? ~(1 << (3 - CC)) : -1;
27400b57cec5SDimitry Andric   else if (Cond == ISD::SETLT || Cond == ISD::SETULT)
27410b57cec5SDimitry Andric     // bits above bit 3 for CC==0 (always false), bits above bit 0 for CC==3,
27420b57cec5SDimitry Andric     // always true for CC>3.
27430b57cec5SDimitry Andric     C.CCMask = CC < 4 ? ~0U << (4 - CC) : -1;
27440b57cec5SDimitry Andric   else if (Cond == ISD::SETGE || Cond == ISD::SETUGE)
27450b57cec5SDimitry Andric     // ...and the inverse of that.
27460b57cec5SDimitry Andric     C.CCMask = CC < 4 ? ~(~0U << (4 - CC)) : 0;
27470b57cec5SDimitry Andric   else if (Cond == ISD::SETLE || Cond == ISD::SETULE)
27480b57cec5SDimitry Andric     // bit 3 and above for CC==0, bit 0 and above for CC==3 (always true),
27490b57cec5SDimitry Andric     // always true for CC>3.
27500b57cec5SDimitry Andric     C.CCMask = CC < 4 ? ~0U << (3 - CC) : -1;
27510b57cec5SDimitry Andric   else if (Cond == ISD::SETGT || Cond == ISD::SETUGT)
27520b57cec5SDimitry Andric     // ...and the inverse of that.
27530b57cec5SDimitry Andric     C.CCMask = CC < 4 ? ~(~0U << (3 - CC)) : 0;
27540b57cec5SDimitry Andric   else
27550b57cec5SDimitry Andric     llvm_unreachable("Unexpected integer comparison type");
27560b57cec5SDimitry Andric   C.CCMask &= CCValid;
27570b57cec5SDimitry Andric   return C;
27580b57cec5SDimitry Andric }
27590b57cec5SDimitry Andric 
27600b57cec5SDimitry Andric // Decide how to implement a comparison of type Cond between CmpOp0 with CmpOp1.
27610b57cec5SDimitry Andric static Comparison getCmp(SelectionDAG &DAG, SDValue CmpOp0, SDValue CmpOp1,
2762480093f4SDimitry Andric                          ISD::CondCode Cond, const SDLoc &DL,
2763480093f4SDimitry Andric                          SDValue Chain = SDValue(),
2764480093f4SDimitry Andric                          bool IsSignaling = false) {
27650b57cec5SDimitry Andric   if (CmpOp1.getOpcode() == ISD::Constant) {
2766480093f4SDimitry Andric     assert(!Chain);
27670b57cec5SDimitry Andric     uint64_t Constant = cast<ConstantSDNode>(CmpOp1)->getZExtValue();
27680b57cec5SDimitry Andric     unsigned Opcode, CCValid;
27690b57cec5SDimitry Andric     if (CmpOp0.getOpcode() == ISD::INTRINSIC_W_CHAIN &&
27700b57cec5SDimitry Andric         CmpOp0.getResNo() == 0 && CmpOp0->hasNUsesOfValue(1, 0) &&
27710b57cec5SDimitry Andric         isIntrinsicWithCCAndChain(CmpOp0, Opcode, CCValid))
27720b57cec5SDimitry Andric       return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond);
27730b57cec5SDimitry Andric     if (CmpOp0.getOpcode() == ISD::INTRINSIC_WO_CHAIN &&
27740b57cec5SDimitry Andric         CmpOp0.getResNo() == CmpOp0->getNumValues() - 1 &&
27750b57cec5SDimitry Andric         isIntrinsicWithCC(CmpOp0, Opcode, CCValid))
27760b57cec5SDimitry Andric       return getIntrinsicCmp(DAG, Opcode, CmpOp0, CCValid, Constant, Cond);
27770b57cec5SDimitry Andric   }
2778480093f4SDimitry Andric   Comparison C(CmpOp0, CmpOp1, Chain);
27790b57cec5SDimitry Andric   C.CCMask = CCMaskForCondCode(Cond);
27800b57cec5SDimitry Andric   if (C.Op0.getValueType().isFloatingPoint()) {
27810b57cec5SDimitry Andric     C.CCValid = SystemZ::CCMASK_FCMP;
2782480093f4SDimitry Andric     if (!C.Chain)
27830b57cec5SDimitry Andric       C.Opcode = SystemZISD::FCMP;
2784480093f4SDimitry Andric     else if (!IsSignaling)
2785480093f4SDimitry Andric       C.Opcode = SystemZISD::STRICT_FCMP;
2786480093f4SDimitry Andric     else
2787480093f4SDimitry Andric       C.Opcode = SystemZISD::STRICT_FCMPS;
27880b57cec5SDimitry Andric     adjustForFNeg(C);
27890b57cec5SDimitry Andric   } else {
2790480093f4SDimitry Andric     assert(!C.Chain);
27910b57cec5SDimitry Andric     C.CCValid = SystemZ::CCMASK_ICMP;
27920b57cec5SDimitry Andric     C.Opcode = SystemZISD::ICMP;
27930b57cec5SDimitry Andric     // Choose the type of comparison.  Equality and inequality tests can
27940b57cec5SDimitry Andric     // use either signed or unsigned comparisons.  The choice also doesn't
27950b57cec5SDimitry Andric     // matter if both sign bits are known to be clear.  In those cases we
27960b57cec5SDimitry Andric     // want to give the main isel code the freedom to choose whichever
27970b57cec5SDimitry Andric     // form fits best.
27980b57cec5SDimitry Andric     if (C.CCMask == SystemZ::CCMASK_CMP_EQ ||
27990b57cec5SDimitry Andric         C.CCMask == SystemZ::CCMASK_CMP_NE ||
28000b57cec5SDimitry Andric         (DAG.SignBitIsZero(C.Op0) && DAG.SignBitIsZero(C.Op1)))
28010b57cec5SDimitry Andric       C.ICmpType = SystemZICMP::Any;
28020b57cec5SDimitry Andric     else if (C.CCMask & SystemZ::CCMASK_CMP_UO)
28030b57cec5SDimitry Andric       C.ICmpType = SystemZICMP::UnsignedOnly;
28040b57cec5SDimitry Andric     else
28050b57cec5SDimitry Andric       C.ICmpType = SystemZICMP::SignedOnly;
28060b57cec5SDimitry Andric     C.CCMask &= ~SystemZ::CCMASK_CMP_UO;
28070b57cec5SDimitry Andric     adjustForRedundantAnd(DAG, DL, C);
28080b57cec5SDimitry Andric     adjustZeroCmp(DAG, DL, C);
28090b57cec5SDimitry Andric     adjustSubwordCmp(DAG, DL, C);
28100b57cec5SDimitry Andric     adjustForSubtraction(DAG, DL, C);
28110b57cec5SDimitry Andric     adjustForLTGFR(C);
28120b57cec5SDimitry Andric     adjustICmpTruncate(DAG, DL, C);
28130b57cec5SDimitry Andric   }
28140b57cec5SDimitry Andric 
28150b57cec5SDimitry Andric   if (shouldSwapCmpOperands(C)) {
28160b57cec5SDimitry Andric     std::swap(C.Op0, C.Op1);
28175ffd83dbSDimitry Andric     C.CCMask = SystemZ::reverseCCMask(C.CCMask);
28180b57cec5SDimitry Andric   }
28190b57cec5SDimitry Andric 
28200b57cec5SDimitry Andric   adjustForTestUnderMask(DAG, DL, C);
28210b57cec5SDimitry Andric   return C;
28220b57cec5SDimitry Andric }
28230b57cec5SDimitry Andric 
28240b57cec5SDimitry Andric // Emit the comparison instruction described by C.
28250b57cec5SDimitry Andric static SDValue emitCmp(SelectionDAG &DAG, const SDLoc &DL, Comparison &C) {
28260b57cec5SDimitry Andric   if (!C.Op1.getNode()) {
28270b57cec5SDimitry Andric     SDNode *Node;
28280b57cec5SDimitry Andric     switch (C.Op0.getOpcode()) {
28290b57cec5SDimitry Andric     case ISD::INTRINSIC_W_CHAIN:
28300b57cec5SDimitry Andric       Node = emitIntrinsicWithCCAndChain(DAG, C.Op0, C.Opcode);
28310b57cec5SDimitry Andric       return SDValue(Node, 0);
28320b57cec5SDimitry Andric     case ISD::INTRINSIC_WO_CHAIN:
28330b57cec5SDimitry Andric       Node = emitIntrinsicWithCC(DAG, C.Op0, C.Opcode);
28340b57cec5SDimitry Andric       return SDValue(Node, Node->getNumValues() - 1);
28350b57cec5SDimitry Andric     default:
28360b57cec5SDimitry Andric       llvm_unreachable("Invalid comparison operands");
28370b57cec5SDimitry Andric     }
28380b57cec5SDimitry Andric   }
28390b57cec5SDimitry Andric   if (C.Opcode == SystemZISD::ICMP)
28400b57cec5SDimitry Andric     return DAG.getNode(SystemZISD::ICMP, DL, MVT::i32, C.Op0, C.Op1,
28418bcb0991SDimitry Andric                        DAG.getTargetConstant(C.ICmpType, DL, MVT::i32));
28420b57cec5SDimitry Andric   if (C.Opcode == SystemZISD::TM) {
28430b57cec5SDimitry Andric     bool RegisterOnly = (bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_0) !=
28440b57cec5SDimitry Andric                          bool(C.CCMask & SystemZ::CCMASK_TM_MIXED_MSB_1));
28450b57cec5SDimitry Andric     return DAG.getNode(SystemZISD::TM, DL, MVT::i32, C.Op0, C.Op1,
28468bcb0991SDimitry Andric                        DAG.getTargetConstant(RegisterOnly, DL, MVT::i32));
28470b57cec5SDimitry Andric   }
2848480093f4SDimitry Andric   if (C.Chain) {
2849480093f4SDimitry Andric     SDVTList VTs = DAG.getVTList(MVT::i32, MVT::Other);
2850480093f4SDimitry Andric     return DAG.getNode(C.Opcode, DL, VTs, C.Chain, C.Op0, C.Op1);
2851480093f4SDimitry Andric   }
28520b57cec5SDimitry Andric   return DAG.getNode(C.Opcode, DL, MVT::i32, C.Op0, C.Op1);
28530b57cec5SDimitry Andric }
28540b57cec5SDimitry Andric 
28550b57cec5SDimitry Andric // Implement a 32-bit *MUL_LOHI operation by extending both operands to
28560b57cec5SDimitry Andric // 64 bits.  Extend is the extension type to use.  Store the high part
28570b57cec5SDimitry Andric // in Hi and the low part in Lo.
28580b57cec5SDimitry Andric static void lowerMUL_LOHI32(SelectionDAG &DAG, const SDLoc &DL, unsigned Extend,
28590b57cec5SDimitry Andric                             SDValue Op0, SDValue Op1, SDValue &Hi,
28600b57cec5SDimitry Andric                             SDValue &Lo) {
28610b57cec5SDimitry Andric   Op0 = DAG.getNode(Extend, DL, MVT::i64, Op0);
28620b57cec5SDimitry Andric   Op1 = DAG.getNode(Extend, DL, MVT::i64, Op1);
28630b57cec5SDimitry Andric   SDValue Mul = DAG.getNode(ISD::MUL, DL, MVT::i64, Op0, Op1);
28640b57cec5SDimitry Andric   Hi = DAG.getNode(ISD::SRL, DL, MVT::i64, Mul,
28650b57cec5SDimitry Andric                    DAG.getConstant(32, DL, MVT::i64));
28660b57cec5SDimitry Andric   Hi = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Hi);
28670b57cec5SDimitry Andric   Lo = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Mul);
28680b57cec5SDimitry Andric }
28690b57cec5SDimitry Andric 
28700b57cec5SDimitry Andric // Lower a binary operation that produces two VT results, one in each
28710b57cec5SDimitry Andric // half of a GR128 pair.  Op0 and Op1 are the VT operands to the operation,
28720b57cec5SDimitry Andric // and Opcode performs the GR128 operation.  Store the even register result
28730b57cec5SDimitry Andric // in Even and the odd register result in Odd.
28740b57cec5SDimitry Andric static void lowerGR128Binary(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
28750b57cec5SDimitry Andric                              unsigned Opcode, SDValue Op0, SDValue Op1,
28760b57cec5SDimitry Andric                              SDValue &Even, SDValue &Odd) {
28770b57cec5SDimitry Andric   SDValue Result = DAG.getNode(Opcode, DL, MVT::Untyped, Op0, Op1);
28780b57cec5SDimitry Andric   bool Is32Bit = is32Bit(VT);
28790b57cec5SDimitry Andric   Even = DAG.getTargetExtractSubreg(SystemZ::even128(Is32Bit), DL, VT, Result);
28800b57cec5SDimitry Andric   Odd = DAG.getTargetExtractSubreg(SystemZ::odd128(Is32Bit), DL, VT, Result);
28810b57cec5SDimitry Andric }
28820b57cec5SDimitry Andric 
28830b57cec5SDimitry Andric // Return an i32 value that is 1 if the CC value produced by CCReg is
28840b57cec5SDimitry Andric // in the mask CCMask and 0 otherwise.  CC is known to have a value
28850b57cec5SDimitry Andric // in CCValid, so other values can be ignored.
28860b57cec5SDimitry Andric static SDValue emitSETCC(SelectionDAG &DAG, const SDLoc &DL, SDValue CCReg,
28870b57cec5SDimitry Andric                          unsigned CCValid, unsigned CCMask) {
28880b57cec5SDimitry Andric   SDValue Ops[] = {DAG.getConstant(1, DL, MVT::i32),
28890b57cec5SDimitry Andric                    DAG.getConstant(0, DL, MVT::i32),
28908bcb0991SDimitry Andric                    DAG.getTargetConstant(CCValid, DL, MVT::i32),
28918bcb0991SDimitry Andric                    DAG.getTargetConstant(CCMask, DL, MVT::i32), CCReg};
28920b57cec5SDimitry Andric   return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, MVT::i32, Ops);
28930b57cec5SDimitry Andric }
28940b57cec5SDimitry Andric 
28950b57cec5SDimitry Andric // Return the SystemISD vector comparison operation for CC, or 0 if it cannot
2896480093f4SDimitry Andric // be done directly.  Mode is CmpMode::Int for integer comparisons, CmpMode::FP
2897480093f4SDimitry Andric // for regular floating-point comparisons, CmpMode::StrictFP for strict (quiet)
2898480093f4SDimitry Andric // floating-point comparisons, and CmpMode::SignalingFP for strict signaling
2899480093f4SDimitry Andric // floating-point comparisons.
2900480093f4SDimitry Andric enum class CmpMode { Int, FP, StrictFP, SignalingFP };
2901480093f4SDimitry Andric static unsigned getVectorComparison(ISD::CondCode CC, CmpMode Mode) {
29020b57cec5SDimitry Andric   switch (CC) {
29030b57cec5SDimitry Andric   case ISD::SETOEQ:
29040b57cec5SDimitry Andric   case ISD::SETEQ:
2905480093f4SDimitry Andric     switch (Mode) {
2906480093f4SDimitry Andric     case CmpMode::Int:         return SystemZISD::VICMPE;
2907480093f4SDimitry Andric     case CmpMode::FP:          return SystemZISD::VFCMPE;
2908480093f4SDimitry Andric     case CmpMode::StrictFP:    return SystemZISD::STRICT_VFCMPE;
2909480093f4SDimitry Andric     case CmpMode::SignalingFP: return SystemZISD::STRICT_VFCMPES;
2910480093f4SDimitry Andric     }
2911480093f4SDimitry Andric     llvm_unreachable("Bad mode");
29120b57cec5SDimitry Andric 
29130b57cec5SDimitry Andric   case ISD::SETOGE:
29140b57cec5SDimitry Andric   case ISD::SETGE:
2915480093f4SDimitry Andric     switch (Mode) {
2916480093f4SDimitry Andric     case CmpMode::Int:         return 0;
2917480093f4SDimitry Andric     case CmpMode::FP:          return SystemZISD::VFCMPHE;
2918480093f4SDimitry Andric     case CmpMode::StrictFP:    return SystemZISD::STRICT_VFCMPHE;
2919480093f4SDimitry Andric     case CmpMode::SignalingFP: return SystemZISD::STRICT_VFCMPHES;
2920480093f4SDimitry Andric     }
2921480093f4SDimitry Andric     llvm_unreachable("Bad mode");
29220b57cec5SDimitry Andric 
29230b57cec5SDimitry Andric   case ISD::SETOGT:
29240b57cec5SDimitry Andric   case ISD::SETGT:
2925480093f4SDimitry Andric     switch (Mode) {
2926480093f4SDimitry Andric     case CmpMode::Int:         return SystemZISD::VICMPH;
2927480093f4SDimitry Andric     case CmpMode::FP:          return SystemZISD::VFCMPH;
2928480093f4SDimitry Andric     case CmpMode::StrictFP:    return SystemZISD::STRICT_VFCMPH;
2929480093f4SDimitry Andric     case CmpMode::SignalingFP: return SystemZISD::STRICT_VFCMPHS;
2930480093f4SDimitry Andric     }
2931480093f4SDimitry Andric     llvm_unreachable("Bad mode");
29320b57cec5SDimitry Andric 
29330b57cec5SDimitry Andric   case ISD::SETUGT:
2934480093f4SDimitry Andric     switch (Mode) {
2935480093f4SDimitry Andric     case CmpMode::Int:         return SystemZISD::VICMPHL;
2936480093f4SDimitry Andric     case CmpMode::FP:          return 0;
2937480093f4SDimitry Andric     case CmpMode::StrictFP:    return 0;
2938480093f4SDimitry Andric     case CmpMode::SignalingFP: return 0;
2939480093f4SDimitry Andric     }
2940480093f4SDimitry Andric     llvm_unreachable("Bad mode");
29410b57cec5SDimitry Andric 
29420b57cec5SDimitry Andric   default:
29430b57cec5SDimitry Andric     return 0;
29440b57cec5SDimitry Andric   }
29450b57cec5SDimitry Andric }
29460b57cec5SDimitry Andric 
29470b57cec5SDimitry Andric // Return the SystemZISD vector comparison operation for CC or its inverse,
29480b57cec5SDimitry Andric // or 0 if neither can be done directly.  Indicate in Invert whether the
2949480093f4SDimitry Andric // result is for the inverse of CC.  Mode is as above.
2950480093f4SDimitry Andric static unsigned getVectorComparisonOrInvert(ISD::CondCode CC, CmpMode Mode,
29510b57cec5SDimitry Andric                                             bool &Invert) {
2952480093f4SDimitry Andric   if (unsigned Opcode = getVectorComparison(CC, Mode)) {
29530b57cec5SDimitry Andric     Invert = false;
29540b57cec5SDimitry Andric     return Opcode;
29550b57cec5SDimitry Andric   }
29560b57cec5SDimitry Andric 
2957480093f4SDimitry Andric   CC = ISD::getSetCCInverse(CC, Mode == CmpMode::Int ? MVT::i32 : MVT::f32);
2958480093f4SDimitry Andric   if (unsigned Opcode = getVectorComparison(CC, Mode)) {
29590b57cec5SDimitry Andric     Invert = true;
29600b57cec5SDimitry Andric     return Opcode;
29610b57cec5SDimitry Andric   }
29620b57cec5SDimitry Andric 
29630b57cec5SDimitry Andric   return 0;
29640b57cec5SDimitry Andric }
29650b57cec5SDimitry Andric 
29660b57cec5SDimitry Andric // Return a v2f64 that contains the extended form of elements Start and Start+1
2967480093f4SDimitry Andric // of v4f32 value Op.  If Chain is nonnull, return the strict form.
29680b57cec5SDimitry Andric static SDValue expandV4F32ToV2F64(SelectionDAG &DAG, int Start, const SDLoc &DL,
2969480093f4SDimitry Andric                                   SDValue Op, SDValue Chain) {
29700b57cec5SDimitry Andric   int Mask[] = { Start, -1, Start + 1, -1 };
29710b57cec5SDimitry Andric   Op = DAG.getVectorShuffle(MVT::v4f32, DL, Op, DAG.getUNDEF(MVT::v4f32), Mask);
2972480093f4SDimitry Andric   if (Chain) {
2973480093f4SDimitry Andric     SDVTList VTs = DAG.getVTList(MVT::v2f64, MVT::Other);
2974480093f4SDimitry Andric     return DAG.getNode(SystemZISD::STRICT_VEXTEND, DL, VTs, Chain, Op);
2975480093f4SDimitry Andric   }
29760b57cec5SDimitry Andric   return DAG.getNode(SystemZISD::VEXTEND, DL, MVT::v2f64, Op);
29770b57cec5SDimitry Andric }
29780b57cec5SDimitry Andric 
29790b57cec5SDimitry Andric // Build a comparison of vectors CmpOp0 and CmpOp1 using opcode Opcode,
2980480093f4SDimitry Andric // producing a result of type VT.  If Chain is nonnull, return the strict form.
29810b57cec5SDimitry Andric SDValue SystemZTargetLowering::getVectorCmp(SelectionDAG &DAG, unsigned Opcode,
29820b57cec5SDimitry Andric                                             const SDLoc &DL, EVT VT,
29830b57cec5SDimitry Andric                                             SDValue CmpOp0,
2984480093f4SDimitry Andric                                             SDValue CmpOp1,
2985480093f4SDimitry Andric                                             SDValue Chain) const {
29860b57cec5SDimitry Andric   // There is no hardware support for v4f32 (unless we have the vector
29870b57cec5SDimitry Andric   // enhancements facility 1), so extend the vector into two v2f64s
29880b57cec5SDimitry Andric   // and compare those.
29890b57cec5SDimitry Andric   if (CmpOp0.getValueType() == MVT::v4f32 &&
29900b57cec5SDimitry Andric       !Subtarget.hasVectorEnhancements1()) {
2991480093f4SDimitry Andric     SDValue H0 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp0, Chain);
2992480093f4SDimitry Andric     SDValue L0 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp0, Chain);
2993480093f4SDimitry Andric     SDValue H1 = expandV4F32ToV2F64(DAG, 0, DL, CmpOp1, Chain);
2994480093f4SDimitry Andric     SDValue L1 = expandV4F32ToV2F64(DAG, 2, DL, CmpOp1, Chain);
2995480093f4SDimitry Andric     if (Chain) {
2996480093f4SDimitry Andric       SDVTList VTs = DAG.getVTList(MVT::v2i64, MVT::Other);
2997480093f4SDimitry Andric       SDValue HRes = DAG.getNode(Opcode, DL, VTs, Chain, H0, H1);
2998480093f4SDimitry Andric       SDValue LRes = DAG.getNode(Opcode, DL, VTs, Chain, L0, L1);
2999480093f4SDimitry Andric       SDValue Res = DAG.getNode(SystemZISD::PACK, DL, VT, HRes, LRes);
3000480093f4SDimitry Andric       SDValue Chains[6] = { H0.getValue(1), L0.getValue(1),
3001480093f4SDimitry Andric                             H1.getValue(1), L1.getValue(1),
3002480093f4SDimitry Andric                             HRes.getValue(1), LRes.getValue(1) };
3003480093f4SDimitry Andric       SDValue NewChain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chains);
3004480093f4SDimitry Andric       SDValue Ops[2] = { Res, NewChain };
3005480093f4SDimitry Andric       return DAG.getMergeValues(Ops, DL);
3006480093f4SDimitry Andric     }
30070b57cec5SDimitry Andric     SDValue HRes = DAG.getNode(Opcode, DL, MVT::v2i64, H0, H1);
30080b57cec5SDimitry Andric     SDValue LRes = DAG.getNode(Opcode, DL, MVT::v2i64, L0, L1);
30090b57cec5SDimitry Andric     return DAG.getNode(SystemZISD::PACK, DL, VT, HRes, LRes);
30100b57cec5SDimitry Andric   }
3011480093f4SDimitry Andric   if (Chain) {
3012480093f4SDimitry Andric     SDVTList VTs = DAG.getVTList(VT, MVT::Other);
3013480093f4SDimitry Andric     return DAG.getNode(Opcode, DL, VTs, Chain, CmpOp0, CmpOp1);
3014480093f4SDimitry Andric   }
30150b57cec5SDimitry Andric   return DAG.getNode(Opcode, DL, VT, CmpOp0, CmpOp1);
30160b57cec5SDimitry Andric }
30170b57cec5SDimitry Andric 
30180b57cec5SDimitry Andric // Lower a vector comparison of type CC between CmpOp0 and CmpOp1, producing
3019480093f4SDimitry Andric // an integer mask of type VT.  If Chain is nonnull, we have a strict
3020480093f4SDimitry Andric // floating-point comparison.  If in addition IsSignaling is true, we have
3021480093f4SDimitry Andric // a strict signaling floating-point comparison.
30220b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerVectorSETCC(SelectionDAG &DAG,
30230b57cec5SDimitry Andric                                                 const SDLoc &DL, EVT VT,
30240b57cec5SDimitry Andric                                                 ISD::CondCode CC,
30250b57cec5SDimitry Andric                                                 SDValue CmpOp0,
3026480093f4SDimitry Andric                                                 SDValue CmpOp1,
3027480093f4SDimitry Andric                                                 SDValue Chain,
3028480093f4SDimitry Andric                                                 bool IsSignaling) const {
30290b57cec5SDimitry Andric   bool IsFP = CmpOp0.getValueType().isFloatingPoint();
3030480093f4SDimitry Andric   assert (!Chain || IsFP);
3031480093f4SDimitry Andric   assert (!IsSignaling || Chain);
3032480093f4SDimitry Andric   CmpMode Mode = IsSignaling ? CmpMode::SignalingFP :
3033480093f4SDimitry Andric                  Chain ? CmpMode::StrictFP : IsFP ? CmpMode::FP : CmpMode::Int;
30340b57cec5SDimitry Andric   bool Invert = false;
30350b57cec5SDimitry Andric   SDValue Cmp;
30360b57cec5SDimitry Andric   switch (CC) {
30370b57cec5SDimitry Andric     // Handle tests for order using (or (ogt y x) (oge x y)).
30380b57cec5SDimitry Andric   case ISD::SETUO:
30390b57cec5SDimitry Andric     Invert = true;
3040bdd1243dSDimitry Andric     [[fallthrough]];
30410b57cec5SDimitry Andric   case ISD::SETO: {
30420b57cec5SDimitry Andric     assert(IsFP && "Unexpected integer comparison");
3043480093f4SDimitry Andric     SDValue LT = getVectorCmp(DAG, getVectorComparison(ISD::SETOGT, Mode),
3044480093f4SDimitry Andric                               DL, VT, CmpOp1, CmpOp0, Chain);
3045480093f4SDimitry Andric     SDValue GE = getVectorCmp(DAG, getVectorComparison(ISD::SETOGE, Mode),
3046480093f4SDimitry Andric                               DL, VT, CmpOp0, CmpOp1, Chain);
30470b57cec5SDimitry Andric     Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GE);
3048480093f4SDimitry Andric     if (Chain)
3049480093f4SDimitry Andric       Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
3050480093f4SDimitry Andric                           LT.getValue(1), GE.getValue(1));
30510b57cec5SDimitry Andric     break;
30520b57cec5SDimitry Andric   }
30530b57cec5SDimitry Andric 
30540b57cec5SDimitry Andric     // Handle <> tests using (or (ogt y x) (ogt x y)).
30550b57cec5SDimitry Andric   case ISD::SETUEQ:
30560b57cec5SDimitry Andric     Invert = true;
3057bdd1243dSDimitry Andric     [[fallthrough]];
30580b57cec5SDimitry Andric   case ISD::SETONE: {
30590b57cec5SDimitry Andric     assert(IsFP && "Unexpected integer comparison");
3060480093f4SDimitry Andric     SDValue LT = getVectorCmp(DAG, getVectorComparison(ISD::SETOGT, Mode),
3061480093f4SDimitry Andric                               DL, VT, CmpOp1, CmpOp0, Chain);
3062480093f4SDimitry Andric     SDValue GT = getVectorCmp(DAG, getVectorComparison(ISD::SETOGT, Mode),
3063480093f4SDimitry Andric                               DL, VT, CmpOp0, CmpOp1, Chain);
30640b57cec5SDimitry Andric     Cmp = DAG.getNode(ISD::OR, DL, VT, LT, GT);
3065480093f4SDimitry Andric     if (Chain)
3066480093f4SDimitry Andric       Chain = DAG.getNode(ISD::TokenFactor, DL, MVT::Other,
3067480093f4SDimitry Andric                           LT.getValue(1), GT.getValue(1));
30680b57cec5SDimitry Andric     break;
30690b57cec5SDimitry Andric   }
30700b57cec5SDimitry Andric 
30710b57cec5SDimitry Andric     // Otherwise a single comparison is enough.  It doesn't really
30720b57cec5SDimitry Andric     // matter whether we try the inversion or the swap first, since
30730b57cec5SDimitry Andric     // there are no cases where both work.
30740b57cec5SDimitry Andric   default:
3075480093f4SDimitry Andric     if (unsigned Opcode = getVectorComparisonOrInvert(CC, Mode, Invert))
3076480093f4SDimitry Andric       Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp0, CmpOp1, Chain);
30770b57cec5SDimitry Andric     else {
30780b57cec5SDimitry Andric       CC = ISD::getSetCCSwappedOperands(CC);
3079480093f4SDimitry Andric       if (unsigned Opcode = getVectorComparisonOrInvert(CC, Mode, Invert))
3080480093f4SDimitry Andric         Cmp = getVectorCmp(DAG, Opcode, DL, VT, CmpOp1, CmpOp0, Chain);
30810b57cec5SDimitry Andric       else
30820b57cec5SDimitry Andric         llvm_unreachable("Unhandled comparison");
30830b57cec5SDimitry Andric     }
3084480093f4SDimitry Andric     if (Chain)
3085480093f4SDimitry Andric       Chain = Cmp.getValue(1);
30860b57cec5SDimitry Andric     break;
30870b57cec5SDimitry Andric   }
30880b57cec5SDimitry Andric   if (Invert) {
30890b57cec5SDimitry Andric     SDValue Mask =
30900b57cec5SDimitry Andric       DAG.getSplatBuildVector(VT, DL, DAG.getConstant(-1, DL, MVT::i64));
30910b57cec5SDimitry Andric     Cmp = DAG.getNode(ISD::XOR, DL, VT, Cmp, Mask);
30920b57cec5SDimitry Andric   }
3093480093f4SDimitry Andric   if (Chain && Chain.getNode() != Cmp.getNode()) {
3094480093f4SDimitry Andric     SDValue Ops[2] = { Cmp, Chain };
3095480093f4SDimitry Andric     Cmp = DAG.getMergeValues(Ops, DL);
3096480093f4SDimitry Andric   }
30970b57cec5SDimitry Andric   return Cmp;
30980b57cec5SDimitry Andric }
30990b57cec5SDimitry Andric 
31000b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerSETCC(SDValue Op,
31010b57cec5SDimitry Andric                                           SelectionDAG &DAG) const {
31020b57cec5SDimitry Andric   SDValue CmpOp0   = Op.getOperand(0);
31030b57cec5SDimitry Andric   SDValue CmpOp1   = Op.getOperand(1);
31040b57cec5SDimitry Andric   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
31050b57cec5SDimitry Andric   SDLoc DL(Op);
31060b57cec5SDimitry Andric   EVT VT = Op.getValueType();
31070b57cec5SDimitry Andric   if (VT.isVector())
31080b57cec5SDimitry Andric     return lowerVectorSETCC(DAG, DL, VT, CC, CmpOp0, CmpOp1);
31090b57cec5SDimitry Andric 
31100b57cec5SDimitry Andric   Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
31110b57cec5SDimitry Andric   SDValue CCReg = emitCmp(DAG, DL, C);
31120b57cec5SDimitry Andric   return emitSETCC(DAG, DL, CCReg, C.CCValid, C.CCMask);
31130b57cec5SDimitry Andric }
31140b57cec5SDimitry Andric 
3115480093f4SDimitry Andric SDValue SystemZTargetLowering::lowerSTRICT_FSETCC(SDValue Op,
3116480093f4SDimitry Andric                                                   SelectionDAG &DAG,
3117480093f4SDimitry Andric                                                   bool IsSignaling) const {
3118480093f4SDimitry Andric   SDValue Chain    = Op.getOperand(0);
3119480093f4SDimitry Andric   SDValue CmpOp0   = Op.getOperand(1);
3120480093f4SDimitry Andric   SDValue CmpOp1   = Op.getOperand(2);
3121480093f4SDimitry Andric   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(3))->get();
3122480093f4SDimitry Andric   SDLoc DL(Op);
3123480093f4SDimitry Andric   EVT VT = Op.getNode()->getValueType(0);
3124480093f4SDimitry Andric   if (VT.isVector()) {
3125480093f4SDimitry Andric     SDValue Res = lowerVectorSETCC(DAG, DL, VT, CC, CmpOp0, CmpOp1,
3126480093f4SDimitry Andric                                    Chain, IsSignaling);
3127480093f4SDimitry Andric     return Res.getValue(Op.getResNo());
3128480093f4SDimitry Andric   }
3129480093f4SDimitry Andric 
3130480093f4SDimitry Andric   Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL, Chain, IsSignaling));
3131480093f4SDimitry Andric   SDValue CCReg = emitCmp(DAG, DL, C);
3132480093f4SDimitry Andric   CCReg->setFlags(Op->getFlags());
3133480093f4SDimitry Andric   SDValue Result = emitSETCC(DAG, DL, CCReg, C.CCValid, C.CCMask);
3134480093f4SDimitry Andric   SDValue Ops[2] = { Result, CCReg.getValue(1) };
3135480093f4SDimitry Andric   return DAG.getMergeValues(Ops, DL);
3136480093f4SDimitry Andric }
3137480093f4SDimitry Andric 
31380b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerBR_CC(SDValue Op, SelectionDAG &DAG) const {
31390b57cec5SDimitry Andric   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
31400b57cec5SDimitry Andric   SDValue CmpOp0   = Op.getOperand(2);
31410b57cec5SDimitry Andric   SDValue CmpOp1   = Op.getOperand(3);
31420b57cec5SDimitry Andric   SDValue Dest     = Op.getOperand(4);
31430b57cec5SDimitry Andric   SDLoc DL(Op);
31440b57cec5SDimitry Andric 
31450b57cec5SDimitry Andric   Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
31460b57cec5SDimitry Andric   SDValue CCReg = emitCmp(DAG, DL, C);
31478bcb0991SDimitry Andric   return DAG.getNode(
31488bcb0991SDimitry Andric       SystemZISD::BR_CCMASK, DL, Op.getValueType(), Op.getOperand(0),
31498bcb0991SDimitry Andric       DAG.getTargetConstant(C.CCValid, DL, MVT::i32),
31508bcb0991SDimitry Andric       DAG.getTargetConstant(C.CCMask, DL, MVT::i32), Dest, CCReg);
31510b57cec5SDimitry Andric }
31520b57cec5SDimitry Andric 
31530b57cec5SDimitry Andric // Return true if Pos is CmpOp and Neg is the negative of CmpOp,
31540b57cec5SDimitry Andric // allowing Pos and Neg to be wider than CmpOp.
31550b57cec5SDimitry Andric static bool isAbsolute(SDValue CmpOp, SDValue Pos, SDValue Neg) {
31560b57cec5SDimitry Andric   return (Neg.getOpcode() == ISD::SUB &&
31570b57cec5SDimitry Andric           Neg.getOperand(0).getOpcode() == ISD::Constant &&
31580b57cec5SDimitry Andric           cast<ConstantSDNode>(Neg.getOperand(0))->getZExtValue() == 0 &&
31590b57cec5SDimitry Andric           Neg.getOperand(1) == Pos &&
31600b57cec5SDimitry Andric           (Pos == CmpOp ||
31610b57cec5SDimitry Andric            (Pos.getOpcode() == ISD::SIGN_EXTEND &&
31620b57cec5SDimitry Andric             Pos.getOperand(0) == CmpOp)));
31630b57cec5SDimitry Andric }
31640b57cec5SDimitry Andric 
31650b57cec5SDimitry Andric // Return the absolute or negative absolute of Op; IsNegative decides which.
31660b57cec5SDimitry Andric static SDValue getAbsolute(SelectionDAG &DAG, const SDLoc &DL, SDValue Op,
31670b57cec5SDimitry Andric                            bool IsNegative) {
3168e8d8bef9SDimitry Andric   Op = DAG.getNode(ISD::ABS, DL, Op.getValueType(), Op);
31690b57cec5SDimitry Andric   if (IsNegative)
31700b57cec5SDimitry Andric     Op = DAG.getNode(ISD::SUB, DL, Op.getValueType(),
31710b57cec5SDimitry Andric                      DAG.getConstant(0, DL, Op.getValueType()), Op);
31720b57cec5SDimitry Andric   return Op;
31730b57cec5SDimitry Andric }
31740b57cec5SDimitry Andric 
31750b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerSELECT_CC(SDValue Op,
31760b57cec5SDimitry Andric                                               SelectionDAG &DAG) const {
31770b57cec5SDimitry Andric   SDValue CmpOp0   = Op.getOperand(0);
31780b57cec5SDimitry Andric   SDValue CmpOp1   = Op.getOperand(1);
31790b57cec5SDimitry Andric   SDValue TrueOp   = Op.getOperand(2);
31800b57cec5SDimitry Andric   SDValue FalseOp  = Op.getOperand(3);
31810b57cec5SDimitry Andric   ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
31820b57cec5SDimitry Andric   SDLoc DL(Op);
31830b57cec5SDimitry Andric 
31840b57cec5SDimitry Andric   Comparison C(getCmp(DAG, CmpOp0, CmpOp1, CC, DL));
31850b57cec5SDimitry Andric 
31860b57cec5SDimitry Andric   // Check for absolute and negative-absolute selections, including those
31870b57cec5SDimitry Andric   // where the comparison value is sign-extended (for LPGFR and LNGFR).
31880b57cec5SDimitry Andric   // This check supplements the one in DAGCombiner.
31890b57cec5SDimitry Andric   if (C.Opcode == SystemZISD::ICMP &&
31900b57cec5SDimitry Andric       C.CCMask != SystemZ::CCMASK_CMP_EQ &&
31910b57cec5SDimitry Andric       C.CCMask != SystemZ::CCMASK_CMP_NE &&
31920b57cec5SDimitry Andric       C.Op1.getOpcode() == ISD::Constant &&
31930b57cec5SDimitry Andric       cast<ConstantSDNode>(C.Op1)->getZExtValue() == 0) {
31940b57cec5SDimitry Andric     if (isAbsolute(C.Op0, TrueOp, FalseOp))
31950b57cec5SDimitry Andric       return getAbsolute(DAG, DL, TrueOp, C.CCMask & SystemZ::CCMASK_CMP_LT);
31960b57cec5SDimitry Andric     if (isAbsolute(C.Op0, FalseOp, TrueOp))
31970b57cec5SDimitry Andric       return getAbsolute(DAG, DL, FalseOp, C.CCMask & SystemZ::CCMASK_CMP_GT);
31980b57cec5SDimitry Andric   }
31990b57cec5SDimitry Andric 
32000b57cec5SDimitry Andric   SDValue CCReg = emitCmp(DAG, DL, C);
32018bcb0991SDimitry Andric   SDValue Ops[] = {TrueOp, FalseOp,
32028bcb0991SDimitry Andric                    DAG.getTargetConstant(C.CCValid, DL, MVT::i32),
32038bcb0991SDimitry Andric                    DAG.getTargetConstant(C.CCMask, DL, MVT::i32), CCReg};
32040b57cec5SDimitry Andric 
32050b57cec5SDimitry Andric   return DAG.getNode(SystemZISD::SELECT_CCMASK, DL, Op.getValueType(), Ops);
32060b57cec5SDimitry Andric }
32070b57cec5SDimitry Andric 
32080b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerGlobalAddress(GlobalAddressSDNode *Node,
32090b57cec5SDimitry Andric                                                   SelectionDAG &DAG) const {
32100b57cec5SDimitry Andric   SDLoc DL(Node);
32110b57cec5SDimitry Andric   const GlobalValue *GV = Node->getGlobal();
32120b57cec5SDimitry Andric   int64_t Offset = Node->getOffset();
32130b57cec5SDimitry Andric   EVT PtrVT = getPointerTy(DAG.getDataLayout());
32140b57cec5SDimitry Andric   CodeModel::Model CM = DAG.getTarget().getCodeModel();
32150b57cec5SDimitry Andric 
32160b57cec5SDimitry Andric   SDValue Result;
32170b57cec5SDimitry Andric   if (Subtarget.isPC32DBLSymbol(GV, CM)) {
3218480093f4SDimitry Andric     if (isInt<32>(Offset)) {
32190b57cec5SDimitry Andric       // Assign anchors at 1<<12 byte boundaries.
32200b57cec5SDimitry Andric       uint64_t Anchor = Offset & ~uint64_t(0xfff);
32210b57cec5SDimitry Andric       Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor);
32220b57cec5SDimitry Andric       Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
32230b57cec5SDimitry Andric 
3224480093f4SDimitry Andric       // The offset can be folded into the address if it is aligned to a
3225480093f4SDimitry Andric       // halfword.
32260b57cec5SDimitry Andric       Offset -= Anchor;
32270b57cec5SDimitry Andric       if (Offset != 0 && (Offset & 1) == 0) {
3228480093f4SDimitry Andric         SDValue Full =
3229480093f4SDimitry Andric           DAG.getTargetGlobalAddress(GV, DL, PtrVT, Anchor + Offset);
32300b57cec5SDimitry Andric         Result = DAG.getNode(SystemZISD::PCREL_OFFSET, DL, PtrVT, Full, Result);
32310b57cec5SDimitry Andric         Offset = 0;
32320b57cec5SDimitry Andric       }
32330b57cec5SDimitry Andric     } else {
3234480093f4SDimitry Andric       // Conservatively load a constant offset greater than 32 bits into a
3235480093f4SDimitry Andric       // register below.
3236480093f4SDimitry Andric       Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT);
3237480093f4SDimitry Andric       Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
3238480093f4SDimitry Andric     }
3239480093f4SDimitry Andric   } else {
32400b57cec5SDimitry Andric     Result = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0, SystemZII::MO_GOT);
32410b57cec5SDimitry Andric     Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
32420b57cec5SDimitry Andric     Result = DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Result,
32430b57cec5SDimitry Andric                          MachinePointerInfo::getGOT(DAG.getMachineFunction()));
32440b57cec5SDimitry Andric   }
32450b57cec5SDimitry Andric 
32460b57cec5SDimitry Andric   // If there was a non-zero offset that we didn't fold, create an explicit
32470b57cec5SDimitry Andric   // addition for it.
32480b57cec5SDimitry Andric   if (Offset != 0)
32490b57cec5SDimitry Andric     Result = DAG.getNode(ISD::ADD, DL, PtrVT, Result,
32500b57cec5SDimitry Andric                          DAG.getConstant(Offset, DL, PtrVT));
32510b57cec5SDimitry Andric 
32520b57cec5SDimitry Andric   return Result;
32530b57cec5SDimitry Andric }
32540b57cec5SDimitry Andric 
32550b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerTLSGetOffset(GlobalAddressSDNode *Node,
32560b57cec5SDimitry Andric                                                  SelectionDAG &DAG,
32570b57cec5SDimitry Andric                                                  unsigned Opcode,
32580b57cec5SDimitry Andric                                                  SDValue GOTOffset) const {
32590b57cec5SDimitry Andric   SDLoc DL(Node);
32600b57cec5SDimitry Andric   EVT PtrVT = getPointerTy(DAG.getDataLayout());
32610b57cec5SDimitry Andric   SDValue Chain = DAG.getEntryNode();
32620b57cec5SDimitry Andric   SDValue Glue;
32630b57cec5SDimitry Andric 
3264480093f4SDimitry Andric   if (DAG.getMachineFunction().getFunction().getCallingConv() ==
3265480093f4SDimitry Andric       CallingConv::GHC)
3266480093f4SDimitry Andric     report_fatal_error("In GHC calling convention TLS is not supported");
3267480093f4SDimitry Andric 
32680b57cec5SDimitry Andric   // __tls_get_offset takes the GOT offset in %r2 and the GOT in %r12.
32690b57cec5SDimitry Andric   SDValue GOT = DAG.getGLOBAL_OFFSET_TABLE(PtrVT);
32700b57cec5SDimitry Andric   Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R12D, GOT, Glue);
32710b57cec5SDimitry Andric   Glue = Chain.getValue(1);
32720b57cec5SDimitry Andric   Chain = DAG.getCopyToReg(Chain, DL, SystemZ::R2D, GOTOffset, Glue);
32730b57cec5SDimitry Andric   Glue = Chain.getValue(1);
32740b57cec5SDimitry Andric 
32750b57cec5SDimitry Andric   // The first call operand is the chain and the second is the TLS symbol.
32760b57cec5SDimitry Andric   SmallVector<SDValue, 8> Ops;
32770b57cec5SDimitry Andric   Ops.push_back(Chain);
32780b57cec5SDimitry Andric   Ops.push_back(DAG.getTargetGlobalAddress(Node->getGlobal(), DL,
32790b57cec5SDimitry Andric                                            Node->getValueType(0),
32800b57cec5SDimitry Andric                                            0, 0));
32810b57cec5SDimitry Andric 
32820b57cec5SDimitry Andric   // Add argument registers to the end of the list so that they are
32830b57cec5SDimitry Andric   // known live into the call.
32840b57cec5SDimitry Andric   Ops.push_back(DAG.getRegister(SystemZ::R2D, PtrVT));
32850b57cec5SDimitry Andric   Ops.push_back(DAG.getRegister(SystemZ::R12D, PtrVT));
32860b57cec5SDimitry Andric 
32870b57cec5SDimitry Andric   // Add a register mask operand representing the call-preserved registers.
32880b57cec5SDimitry Andric   const TargetRegisterInfo *TRI = Subtarget.getRegisterInfo();
32890b57cec5SDimitry Andric   const uint32_t *Mask =
32900b57cec5SDimitry Andric       TRI->getCallPreservedMask(DAG.getMachineFunction(), CallingConv::C);
32910b57cec5SDimitry Andric   assert(Mask && "Missing call preserved mask for calling convention");
32920b57cec5SDimitry Andric   Ops.push_back(DAG.getRegisterMask(Mask));
32930b57cec5SDimitry Andric 
32940b57cec5SDimitry Andric   // Glue the call to the argument copies.
32950b57cec5SDimitry Andric   Ops.push_back(Glue);
32960b57cec5SDimitry Andric 
32970b57cec5SDimitry Andric   // Emit the call.
32980b57cec5SDimitry Andric   SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
32990b57cec5SDimitry Andric   Chain = DAG.getNode(Opcode, DL, NodeTys, Ops);
33000b57cec5SDimitry Andric   Glue = Chain.getValue(1);
33010b57cec5SDimitry Andric 
33020b57cec5SDimitry Andric   // Copy the return value from %r2.
33030b57cec5SDimitry Andric   return DAG.getCopyFromReg(Chain, DL, SystemZ::R2D, PtrVT, Glue);
33040b57cec5SDimitry Andric }
33050b57cec5SDimitry Andric 
33060b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerThreadPointer(const SDLoc &DL,
33070b57cec5SDimitry Andric                                                   SelectionDAG &DAG) const {
33080b57cec5SDimitry Andric   SDValue Chain = DAG.getEntryNode();
33090b57cec5SDimitry Andric   EVT PtrVT = getPointerTy(DAG.getDataLayout());
33100b57cec5SDimitry Andric 
33110b57cec5SDimitry Andric   // The high part of the thread pointer is in access register 0.
33120b57cec5SDimitry Andric   SDValue TPHi = DAG.getCopyFromReg(Chain, DL, SystemZ::A0, MVT::i32);
33130b57cec5SDimitry Andric   TPHi = DAG.getNode(ISD::ANY_EXTEND, DL, PtrVT, TPHi);
33140b57cec5SDimitry Andric 
33150b57cec5SDimitry Andric   // The low part of the thread pointer is in access register 1.
33160b57cec5SDimitry Andric   SDValue TPLo = DAG.getCopyFromReg(Chain, DL, SystemZ::A1, MVT::i32);
33170b57cec5SDimitry Andric   TPLo = DAG.getNode(ISD::ZERO_EXTEND, DL, PtrVT, TPLo);
33180b57cec5SDimitry Andric 
33190b57cec5SDimitry Andric   // Merge them into a single 64-bit address.
33200b57cec5SDimitry Andric   SDValue TPHiShifted = DAG.getNode(ISD::SHL, DL, PtrVT, TPHi,
33210b57cec5SDimitry Andric                                     DAG.getConstant(32, DL, PtrVT));
33220b57cec5SDimitry Andric   return DAG.getNode(ISD::OR, DL, PtrVT, TPHiShifted, TPLo);
33230b57cec5SDimitry Andric }
33240b57cec5SDimitry Andric 
33250b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerGlobalTLSAddress(GlobalAddressSDNode *Node,
33260b57cec5SDimitry Andric                                                      SelectionDAG &DAG) const {
33270b57cec5SDimitry Andric   if (DAG.getTarget().useEmulatedTLS())
33280b57cec5SDimitry Andric     return LowerToTLSEmulatedModel(Node, DAG);
33290b57cec5SDimitry Andric   SDLoc DL(Node);
33300b57cec5SDimitry Andric   const GlobalValue *GV = Node->getGlobal();
33310b57cec5SDimitry Andric   EVT PtrVT = getPointerTy(DAG.getDataLayout());
33320b57cec5SDimitry Andric   TLSModel::Model model = DAG.getTarget().getTLSModel(GV);
33330b57cec5SDimitry Andric 
3334480093f4SDimitry Andric   if (DAG.getMachineFunction().getFunction().getCallingConv() ==
3335480093f4SDimitry Andric       CallingConv::GHC)
3336480093f4SDimitry Andric     report_fatal_error("In GHC calling convention TLS is not supported");
3337480093f4SDimitry Andric 
33380b57cec5SDimitry Andric   SDValue TP = lowerThreadPointer(DL, DAG);
33390b57cec5SDimitry Andric 
33400b57cec5SDimitry Andric   // Get the offset of GA from the thread pointer, based on the TLS model.
33410b57cec5SDimitry Andric   SDValue Offset;
33420b57cec5SDimitry Andric   switch (model) {
33430b57cec5SDimitry Andric     case TLSModel::GeneralDynamic: {
33440b57cec5SDimitry Andric       // Load the GOT offset of the tls_index (module ID / per-symbol offset).
33450b57cec5SDimitry Andric       SystemZConstantPoolValue *CPV =
33460b57cec5SDimitry Andric         SystemZConstantPoolValue::Create(GV, SystemZCP::TLSGD);
33470b57cec5SDimitry Andric 
33485ffd83dbSDimitry Andric       Offset = DAG.getConstantPool(CPV, PtrVT, Align(8));
33490b57cec5SDimitry Andric       Offset = DAG.getLoad(
33500b57cec5SDimitry Andric           PtrVT, DL, DAG.getEntryNode(), Offset,
33510b57cec5SDimitry Andric           MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
33520b57cec5SDimitry Andric 
33530b57cec5SDimitry Andric       // Call __tls_get_offset to retrieve the offset.
33540b57cec5SDimitry Andric       Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_GDCALL, Offset);
33550b57cec5SDimitry Andric       break;
33560b57cec5SDimitry Andric     }
33570b57cec5SDimitry Andric 
33580b57cec5SDimitry Andric     case TLSModel::LocalDynamic: {
33590b57cec5SDimitry Andric       // Load the GOT offset of the module ID.
33600b57cec5SDimitry Andric       SystemZConstantPoolValue *CPV =
33610b57cec5SDimitry Andric         SystemZConstantPoolValue::Create(GV, SystemZCP::TLSLDM);
33620b57cec5SDimitry Andric 
33635ffd83dbSDimitry Andric       Offset = DAG.getConstantPool(CPV, PtrVT, Align(8));
33640b57cec5SDimitry Andric       Offset = DAG.getLoad(
33650b57cec5SDimitry Andric           PtrVT, DL, DAG.getEntryNode(), Offset,
33660b57cec5SDimitry Andric           MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
33670b57cec5SDimitry Andric 
33680b57cec5SDimitry Andric       // Call __tls_get_offset to retrieve the module base offset.
33690b57cec5SDimitry Andric       Offset = lowerTLSGetOffset(Node, DAG, SystemZISD::TLS_LDCALL, Offset);
33700b57cec5SDimitry Andric 
33710b57cec5SDimitry Andric       // Note: The SystemZLDCleanupPass will remove redundant computations
33720b57cec5SDimitry Andric       // of the module base offset.  Count total number of local-dynamic
33730b57cec5SDimitry Andric       // accesses to trigger execution of that pass.
33740b57cec5SDimitry Andric       SystemZMachineFunctionInfo* MFI =
33750b57cec5SDimitry Andric         DAG.getMachineFunction().getInfo<SystemZMachineFunctionInfo>();
33760b57cec5SDimitry Andric       MFI->incNumLocalDynamicTLSAccesses();
33770b57cec5SDimitry Andric 
33780b57cec5SDimitry Andric       // Add the per-symbol offset.
33790b57cec5SDimitry Andric       CPV = SystemZConstantPoolValue::Create(GV, SystemZCP::DTPOFF);
33800b57cec5SDimitry Andric 
33815ffd83dbSDimitry Andric       SDValue DTPOffset = DAG.getConstantPool(CPV, PtrVT, Align(8));
33820b57cec5SDimitry Andric       DTPOffset = DAG.getLoad(
33830b57cec5SDimitry Andric           PtrVT, DL, DAG.getEntryNode(), DTPOffset,
33840b57cec5SDimitry Andric           MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
33850b57cec5SDimitry Andric 
33860b57cec5SDimitry Andric       Offset = DAG.getNode(ISD::ADD, DL, PtrVT, Offset, DTPOffset);
33870b57cec5SDimitry Andric       break;
33880b57cec5SDimitry Andric     }
33890b57cec5SDimitry Andric 
33900b57cec5SDimitry Andric     case TLSModel::InitialExec: {
33910b57cec5SDimitry Andric       // Load the offset from the GOT.
33920b57cec5SDimitry Andric       Offset = DAG.getTargetGlobalAddress(GV, DL, PtrVT, 0,
33930b57cec5SDimitry Andric                                           SystemZII::MO_INDNTPOFF);
33940b57cec5SDimitry Andric       Offset = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Offset);
33950b57cec5SDimitry Andric       Offset =
33960b57cec5SDimitry Andric           DAG.getLoad(PtrVT, DL, DAG.getEntryNode(), Offset,
33970b57cec5SDimitry Andric                       MachinePointerInfo::getGOT(DAG.getMachineFunction()));
33980b57cec5SDimitry Andric       break;
33990b57cec5SDimitry Andric     }
34000b57cec5SDimitry Andric 
34010b57cec5SDimitry Andric     case TLSModel::LocalExec: {
34020b57cec5SDimitry Andric       // Force the offset into the constant pool and load it from there.
34030b57cec5SDimitry Andric       SystemZConstantPoolValue *CPV =
34040b57cec5SDimitry Andric         SystemZConstantPoolValue::Create(GV, SystemZCP::NTPOFF);
34050b57cec5SDimitry Andric 
34065ffd83dbSDimitry Andric       Offset = DAG.getConstantPool(CPV, PtrVT, Align(8));
34070b57cec5SDimitry Andric       Offset = DAG.getLoad(
34080b57cec5SDimitry Andric           PtrVT, DL, DAG.getEntryNode(), Offset,
34090b57cec5SDimitry Andric           MachinePointerInfo::getConstantPool(DAG.getMachineFunction()));
34100b57cec5SDimitry Andric       break;
34110b57cec5SDimitry Andric     }
34120b57cec5SDimitry Andric   }
34130b57cec5SDimitry Andric 
34140b57cec5SDimitry Andric   // Add the base and offset together.
34150b57cec5SDimitry Andric   return DAG.getNode(ISD::ADD, DL, PtrVT, TP, Offset);
34160b57cec5SDimitry Andric }
34170b57cec5SDimitry Andric 
34180b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerBlockAddress(BlockAddressSDNode *Node,
34190b57cec5SDimitry Andric                                                  SelectionDAG &DAG) const {
34200b57cec5SDimitry Andric   SDLoc DL(Node);
34210b57cec5SDimitry Andric   const BlockAddress *BA = Node->getBlockAddress();
34220b57cec5SDimitry Andric   int64_t Offset = Node->getOffset();
34230b57cec5SDimitry Andric   EVT PtrVT = getPointerTy(DAG.getDataLayout());
34240b57cec5SDimitry Andric 
34250b57cec5SDimitry Andric   SDValue Result = DAG.getTargetBlockAddress(BA, PtrVT, Offset);
34260b57cec5SDimitry Andric   Result = DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
34270b57cec5SDimitry Andric   return Result;
34280b57cec5SDimitry Andric }
34290b57cec5SDimitry Andric 
34300b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerJumpTable(JumpTableSDNode *JT,
34310b57cec5SDimitry Andric                                               SelectionDAG &DAG) const {
34320b57cec5SDimitry Andric   SDLoc DL(JT);
34330b57cec5SDimitry Andric   EVT PtrVT = getPointerTy(DAG.getDataLayout());
34340b57cec5SDimitry Andric   SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), PtrVT);
34350b57cec5SDimitry Andric 
34360b57cec5SDimitry Andric   // Use LARL to load the address of the table.
34370b57cec5SDimitry Andric   return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
34380b57cec5SDimitry Andric }
34390b57cec5SDimitry Andric 
34400b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerConstantPool(ConstantPoolSDNode *CP,
34410b57cec5SDimitry Andric                                                  SelectionDAG &DAG) const {
34420b57cec5SDimitry Andric   SDLoc DL(CP);
34430b57cec5SDimitry Andric   EVT PtrVT = getPointerTy(DAG.getDataLayout());
34440b57cec5SDimitry Andric 
34450b57cec5SDimitry Andric   SDValue Result;
34460b57cec5SDimitry Andric   if (CP->isMachineConstantPoolEntry())
34475ffd83dbSDimitry Andric     Result =
34485ffd83dbSDimitry Andric         DAG.getTargetConstantPool(CP->getMachineCPVal(), PtrVT, CP->getAlign());
34490b57cec5SDimitry Andric   else
34505ffd83dbSDimitry Andric     Result = DAG.getTargetConstantPool(CP->getConstVal(), PtrVT, CP->getAlign(),
34515ffd83dbSDimitry Andric                                        CP->getOffset());
34520b57cec5SDimitry Andric 
34530b57cec5SDimitry Andric   // Use LARL to load the address of the constant pool entry.
34540b57cec5SDimitry Andric   return DAG.getNode(SystemZISD::PCREL_WRAPPER, DL, PtrVT, Result);
34550b57cec5SDimitry Andric }
34560b57cec5SDimitry Andric 
34570b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerFRAMEADDR(SDValue Op,
34580b57cec5SDimitry Andric                                               SelectionDAG &DAG) const {
3459349cc55cSDimitry Andric   auto *TFL = Subtarget.getFrameLowering<SystemZELFFrameLowering>();
34600b57cec5SDimitry Andric   MachineFunction &MF = DAG.getMachineFunction();
34610b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
34620b57cec5SDimitry Andric   MFI.setFrameAddressIsTaken(true);
34630b57cec5SDimitry Andric 
34640b57cec5SDimitry Andric   SDLoc DL(Op);
34650b57cec5SDimitry Andric   unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
34660b57cec5SDimitry Andric   EVT PtrVT = getPointerTy(DAG.getDataLayout());
34670b57cec5SDimitry Andric 
3468fe6060f1SDimitry Andric   // By definition, the frame address is the address of the back chain.  (In
3469fe6060f1SDimitry Andric   // the case of packed stack without backchain, return the address where the
3470fe6060f1SDimitry Andric   // backchain would have been stored. This will either be an unused space or
3471fe6060f1SDimitry Andric   // contain a saved register).
3472480093f4SDimitry Andric   int BackChainIdx = TFL->getOrCreateFramePointerSaveIndex(MF);
34730b57cec5SDimitry Andric   SDValue BackChain = DAG.getFrameIndex(BackChainIdx, PtrVT);
34740b57cec5SDimitry Andric 
34750b57cec5SDimitry Andric   // FIXME The frontend should detect this case.
34760b57cec5SDimitry Andric   if (Depth > 0) {
34770b57cec5SDimitry Andric     report_fatal_error("Unsupported stack frame traversal count");
34780b57cec5SDimitry Andric   }
34790b57cec5SDimitry Andric 
34800b57cec5SDimitry Andric   return BackChain;
34810b57cec5SDimitry Andric }
34820b57cec5SDimitry Andric 
34830b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerRETURNADDR(SDValue Op,
34840b57cec5SDimitry Andric                                                SelectionDAG &DAG) const {
34850b57cec5SDimitry Andric   MachineFunction &MF = DAG.getMachineFunction();
34860b57cec5SDimitry Andric   MachineFrameInfo &MFI = MF.getFrameInfo();
34870b57cec5SDimitry Andric   MFI.setReturnAddressIsTaken(true);
34880b57cec5SDimitry Andric 
34890b57cec5SDimitry Andric   if (verifyReturnAddressArgumentIsConstant(Op, DAG))
34900b57cec5SDimitry Andric     return SDValue();
34910b57cec5SDimitry Andric 
34920b57cec5SDimitry Andric   SDLoc DL(Op);
34930b57cec5SDimitry Andric   unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
34940b57cec5SDimitry Andric   EVT PtrVT = getPointerTy(DAG.getDataLayout());
34950b57cec5SDimitry Andric 
34960b57cec5SDimitry Andric   // FIXME The frontend should detect this case.
34970b57cec5SDimitry Andric   if (Depth > 0) {
34980b57cec5SDimitry Andric     report_fatal_error("Unsupported stack frame traversal count");
34990b57cec5SDimitry Andric   }
35000b57cec5SDimitry Andric 
35010b57cec5SDimitry Andric   // Return R14D, which has the return address. Mark it an implicit live-in.
350204eeddc0SDimitry Andric   Register LinkReg = MF.addLiveIn(SystemZ::R14D, &SystemZ::GR64BitRegClass);
35030b57cec5SDimitry Andric   return DAG.getCopyFromReg(DAG.getEntryNode(), DL, LinkReg, PtrVT);
35040b57cec5SDimitry Andric }
35050b57cec5SDimitry Andric 
35060b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerBITCAST(SDValue Op,
35070b57cec5SDimitry Andric                                             SelectionDAG &DAG) const {
35080b57cec5SDimitry Andric   SDLoc DL(Op);
35090b57cec5SDimitry Andric   SDValue In = Op.getOperand(0);
35100b57cec5SDimitry Andric   EVT InVT = In.getValueType();
35110b57cec5SDimitry Andric   EVT ResVT = Op.getValueType();
35120b57cec5SDimitry Andric 
35130b57cec5SDimitry Andric   // Convert loads directly.  This is normally done by DAGCombiner,
35140b57cec5SDimitry Andric   // but we need this case for bitcasts that are created during lowering
35150b57cec5SDimitry Andric   // and which are then lowered themselves.
35160b57cec5SDimitry Andric   if (auto *LoadN = dyn_cast<LoadSDNode>(In))
35170b57cec5SDimitry Andric     if (ISD::isNormalLoad(LoadN)) {
35180b57cec5SDimitry Andric       SDValue NewLoad = DAG.getLoad(ResVT, DL, LoadN->getChain(),
35190b57cec5SDimitry Andric                                     LoadN->getBasePtr(), LoadN->getMemOperand());
35200b57cec5SDimitry Andric       // Update the chain uses.
35210b57cec5SDimitry Andric       DAG.ReplaceAllUsesOfValueWith(SDValue(LoadN, 1), NewLoad.getValue(1));
35220b57cec5SDimitry Andric       return NewLoad;
35230b57cec5SDimitry Andric     }
35240b57cec5SDimitry Andric 
35250b57cec5SDimitry Andric   if (InVT == MVT::i32 && ResVT == MVT::f32) {
35260b57cec5SDimitry Andric     SDValue In64;
35270b57cec5SDimitry Andric     if (Subtarget.hasHighWord()) {
35280b57cec5SDimitry Andric       SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL,
35290b57cec5SDimitry Andric                                        MVT::i64);
35300b57cec5SDimitry Andric       In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL,
35310b57cec5SDimitry Andric                                        MVT::i64, SDValue(U64, 0), In);
35320b57cec5SDimitry Andric     } else {
35330b57cec5SDimitry Andric       In64 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, In);
35340b57cec5SDimitry Andric       In64 = DAG.getNode(ISD::SHL, DL, MVT::i64, In64,
35350b57cec5SDimitry Andric                          DAG.getConstant(32, DL, MVT::i64));
35360b57cec5SDimitry Andric     }
35370b57cec5SDimitry Andric     SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::f64, In64);
35380b57cec5SDimitry Andric     return DAG.getTargetExtractSubreg(SystemZ::subreg_h32,
35390b57cec5SDimitry Andric                                       DL, MVT::f32, Out64);
35400b57cec5SDimitry Andric   }
35410b57cec5SDimitry Andric   if (InVT == MVT::f32 && ResVT == MVT::i32) {
35420b57cec5SDimitry Andric     SDNode *U64 = DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::f64);
35430b57cec5SDimitry Andric     SDValue In64 = DAG.getTargetInsertSubreg(SystemZ::subreg_h32, DL,
35440b57cec5SDimitry Andric                                              MVT::f64, SDValue(U64, 0), In);
35450b57cec5SDimitry Andric     SDValue Out64 = DAG.getNode(ISD::BITCAST, DL, MVT::i64, In64);
35460b57cec5SDimitry Andric     if (Subtarget.hasHighWord())
35470b57cec5SDimitry Andric       return DAG.getTargetExtractSubreg(SystemZ::subreg_h32, DL,
35480b57cec5SDimitry Andric                                         MVT::i32, Out64);
35490b57cec5SDimitry Andric     SDValue Shift = DAG.getNode(ISD::SRL, DL, MVT::i64, Out64,
35500b57cec5SDimitry Andric                                 DAG.getConstant(32, DL, MVT::i64));
35510b57cec5SDimitry Andric     return DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Shift);
35520b57cec5SDimitry Andric   }
35530b57cec5SDimitry Andric   llvm_unreachable("Unexpected bitcast combination");
35540b57cec5SDimitry Andric }
35550b57cec5SDimitry Andric 
35560b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerVASTART(SDValue Op,
35570b57cec5SDimitry Andric                                             SelectionDAG &DAG) const {
355881ad6265SDimitry Andric 
355981ad6265SDimitry Andric   if (Subtarget.isTargetXPLINK64())
356081ad6265SDimitry Andric     return lowerVASTART_XPLINK(Op, DAG);
356181ad6265SDimitry Andric   else
356281ad6265SDimitry Andric     return lowerVASTART_ELF(Op, DAG);
356381ad6265SDimitry Andric }
356481ad6265SDimitry Andric 
356581ad6265SDimitry Andric SDValue SystemZTargetLowering::lowerVASTART_XPLINK(SDValue Op,
356681ad6265SDimitry Andric                                                    SelectionDAG &DAG) const {
356781ad6265SDimitry Andric   MachineFunction &MF = DAG.getMachineFunction();
356881ad6265SDimitry Andric   SystemZMachineFunctionInfo *FuncInfo =
356981ad6265SDimitry Andric       MF.getInfo<SystemZMachineFunctionInfo>();
357081ad6265SDimitry Andric 
357181ad6265SDimitry Andric   SDLoc DL(Op);
357281ad6265SDimitry Andric 
357381ad6265SDimitry Andric   // vastart just stores the address of the VarArgsFrameIndex slot into the
357481ad6265SDimitry Andric   // memory location argument.
357581ad6265SDimitry Andric   EVT PtrVT = getPointerTy(DAG.getDataLayout());
357681ad6265SDimitry Andric   SDValue FR = DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT);
357781ad6265SDimitry Andric   const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
357881ad6265SDimitry Andric   return DAG.getStore(Op.getOperand(0), DL, FR, Op.getOperand(1),
357981ad6265SDimitry Andric                       MachinePointerInfo(SV));
358081ad6265SDimitry Andric }
358181ad6265SDimitry Andric 
358281ad6265SDimitry Andric SDValue SystemZTargetLowering::lowerVASTART_ELF(SDValue Op,
358381ad6265SDimitry Andric                                                 SelectionDAG &DAG) const {
35840b57cec5SDimitry Andric   MachineFunction &MF = DAG.getMachineFunction();
35850b57cec5SDimitry Andric   SystemZMachineFunctionInfo *FuncInfo =
35860b57cec5SDimitry Andric     MF.getInfo<SystemZMachineFunctionInfo>();
35870b57cec5SDimitry Andric   EVT PtrVT = getPointerTy(DAG.getDataLayout());
35880b57cec5SDimitry Andric 
35890b57cec5SDimitry Andric   SDValue Chain   = Op.getOperand(0);
35900b57cec5SDimitry Andric   SDValue Addr    = Op.getOperand(1);
35910b57cec5SDimitry Andric   const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
35920b57cec5SDimitry Andric   SDLoc DL(Op);
35930b57cec5SDimitry Andric 
35940b57cec5SDimitry Andric   // The initial values of each field.
35950b57cec5SDimitry Andric   const unsigned NumFields = 4;
35960b57cec5SDimitry Andric   SDValue Fields[NumFields] = {
35970b57cec5SDimitry Andric     DAG.getConstant(FuncInfo->getVarArgsFirstGPR(), DL, PtrVT),
35980b57cec5SDimitry Andric     DAG.getConstant(FuncInfo->getVarArgsFirstFPR(), DL, PtrVT),
35990b57cec5SDimitry Andric     DAG.getFrameIndex(FuncInfo->getVarArgsFrameIndex(), PtrVT),
36000b57cec5SDimitry Andric     DAG.getFrameIndex(FuncInfo->getRegSaveFrameIndex(), PtrVT)
36010b57cec5SDimitry Andric   };
36020b57cec5SDimitry Andric 
36030b57cec5SDimitry Andric   // Store each field into its respective slot.
36040b57cec5SDimitry Andric   SDValue MemOps[NumFields];
36050b57cec5SDimitry Andric   unsigned Offset = 0;
36060b57cec5SDimitry Andric   for (unsigned I = 0; I < NumFields; ++I) {
36070b57cec5SDimitry Andric     SDValue FieldAddr = Addr;
36080b57cec5SDimitry Andric     if (Offset != 0)
36090b57cec5SDimitry Andric       FieldAddr = DAG.getNode(ISD::ADD, DL, PtrVT, FieldAddr,
36100b57cec5SDimitry Andric                               DAG.getIntPtrConstant(Offset, DL));
36110b57cec5SDimitry Andric     MemOps[I] = DAG.getStore(Chain, DL, Fields[I], FieldAddr,
36120b57cec5SDimitry Andric                              MachinePointerInfo(SV, Offset));
36130b57cec5SDimitry Andric     Offset += 8;
36140b57cec5SDimitry Andric   }
36150b57cec5SDimitry Andric   return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, MemOps);
36160b57cec5SDimitry Andric }
36170b57cec5SDimitry Andric 
36180b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerVACOPY(SDValue Op,
36190b57cec5SDimitry Andric                                            SelectionDAG &DAG) const {
36200b57cec5SDimitry Andric   SDValue Chain      = Op.getOperand(0);
36210b57cec5SDimitry Andric   SDValue DstPtr     = Op.getOperand(1);
36220b57cec5SDimitry Andric   SDValue SrcPtr     = Op.getOperand(2);
36230b57cec5SDimitry Andric   const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
36240b57cec5SDimitry Andric   const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
36250b57cec5SDimitry Andric   SDLoc DL(Op);
36260b57cec5SDimitry Andric 
362781ad6265SDimitry Andric   uint32_t Sz =
362881ad6265SDimitry Andric       Subtarget.isTargetXPLINK64() ? getTargetMachine().getPointerSize(0) : 32;
362981ad6265SDimitry Andric   return DAG.getMemcpy(Chain, DL, DstPtr, SrcPtr, DAG.getIntPtrConstant(Sz, DL),
36305ffd83dbSDimitry Andric                        Align(8), /*isVolatile*/ false, /*AlwaysInline*/ false,
36315ffd83dbSDimitry Andric                        /*isTailCall*/ false, MachinePointerInfo(DstSV),
36325ffd83dbSDimitry Andric                        MachinePointerInfo(SrcSV));
36330b57cec5SDimitry Andric }
36340b57cec5SDimitry Andric 
363581ad6265SDimitry Andric SDValue
363681ad6265SDimitry Andric SystemZTargetLowering::lowerDYNAMIC_STACKALLOC(SDValue Op,
363781ad6265SDimitry Andric                                                SelectionDAG &DAG) const {
363881ad6265SDimitry Andric   if (Subtarget.isTargetXPLINK64())
363981ad6265SDimitry Andric     return lowerDYNAMIC_STACKALLOC_XPLINK(Op, DAG);
364081ad6265SDimitry Andric   else
364181ad6265SDimitry Andric     return lowerDYNAMIC_STACKALLOC_ELF(Op, DAG);
364281ad6265SDimitry Andric }
364381ad6265SDimitry Andric 
364481ad6265SDimitry Andric SDValue
364581ad6265SDimitry Andric SystemZTargetLowering::lowerDYNAMIC_STACKALLOC_XPLINK(SDValue Op,
364681ad6265SDimitry Andric                                                       SelectionDAG &DAG) const {
364781ad6265SDimitry Andric   const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
364881ad6265SDimitry Andric   MachineFunction &MF = DAG.getMachineFunction();
364981ad6265SDimitry Andric   bool RealignOpt = !MF.getFunction().hasFnAttribute("no-realign-stack");
365081ad6265SDimitry Andric   SDValue Chain = Op.getOperand(0);
365181ad6265SDimitry Andric   SDValue Size = Op.getOperand(1);
365281ad6265SDimitry Andric   SDValue Align = Op.getOperand(2);
365381ad6265SDimitry Andric   SDLoc DL(Op);
365481ad6265SDimitry Andric 
365581ad6265SDimitry Andric   // If user has set the no alignment function attribute, ignore
365681ad6265SDimitry Andric   // alloca alignments.
365781ad6265SDimitry Andric   uint64_t AlignVal =
365881ad6265SDimitry Andric       (RealignOpt ? cast<ConstantSDNode>(Align)->getZExtValue() : 0);
365981ad6265SDimitry Andric 
366081ad6265SDimitry Andric   uint64_t StackAlign = TFI->getStackAlignment();
366181ad6265SDimitry Andric   uint64_t RequiredAlign = std::max(AlignVal, StackAlign);
366281ad6265SDimitry Andric   uint64_t ExtraAlignSpace = RequiredAlign - StackAlign;
366381ad6265SDimitry Andric 
366481ad6265SDimitry Andric   SDValue NeededSpace = Size;
366581ad6265SDimitry Andric 
366681ad6265SDimitry Andric   // Add extra space for alignment if needed.
366781ad6265SDimitry Andric   EVT PtrVT = getPointerTy(MF.getDataLayout());
366881ad6265SDimitry Andric   if (ExtraAlignSpace)
366981ad6265SDimitry Andric     NeededSpace = DAG.getNode(ISD::ADD, DL, PtrVT, NeededSpace,
367081ad6265SDimitry Andric                               DAG.getConstant(ExtraAlignSpace, DL, PtrVT));
367181ad6265SDimitry Andric 
367281ad6265SDimitry Andric   bool IsSigned = false;
367381ad6265SDimitry Andric   bool DoesNotReturn = false;
367481ad6265SDimitry Andric   bool IsReturnValueUsed = false;
367581ad6265SDimitry Andric   EVT VT = Op.getValueType();
367681ad6265SDimitry Andric   SDValue AllocaCall =
3677bdd1243dSDimitry Andric       makeExternalCall(Chain, DAG, "@@ALCAXP", VT, ArrayRef(NeededSpace),
367881ad6265SDimitry Andric                        CallingConv::C, IsSigned, DL, DoesNotReturn,
367981ad6265SDimitry Andric                        IsReturnValueUsed)
368081ad6265SDimitry Andric           .first;
368181ad6265SDimitry Andric 
368281ad6265SDimitry Andric   // Perform a CopyFromReg from %GPR4 (stack pointer register). Chain and Glue
368381ad6265SDimitry Andric   // to end of call in order to ensure it isn't broken up from the call
368481ad6265SDimitry Andric   // sequence.
368581ad6265SDimitry Andric   auto &Regs = Subtarget.getSpecialRegisters<SystemZXPLINK64Registers>();
368681ad6265SDimitry Andric   Register SPReg = Regs.getStackPointerRegister();
368781ad6265SDimitry Andric   Chain = AllocaCall.getValue(1);
368881ad6265SDimitry Andric   SDValue Glue = AllocaCall.getValue(2);
368981ad6265SDimitry Andric   SDValue NewSPRegNode = DAG.getCopyFromReg(Chain, DL, SPReg, PtrVT, Glue);
369081ad6265SDimitry Andric   Chain = NewSPRegNode.getValue(1);
369181ad6265SDimitry Andric 
369281ad6265SDimitry Andric   MVT PtrMVT = getPointerMemTy(MF.getDataLayout());
369381ad6265SDimitry Andric   SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, PtrMVT);
369481ad6265SDimitry Andric   SDValue Result = DAG.getNode(ISD::ADD, DL, PtrMVT, NewSPRegNode, ArgAdjust);
369581ad6265SDimitry Andric 
369681ad6265SDimitry Andric   // Dynamically realign if needed.
369781ad6265SDimitry Andric   if (ExtraAlignSpace) {
369881ad6265SDimitry Andric     Result = DAG.getNode(ISD::ADD, DL, PtrVT, Result,
369981ad6265SDimitry Andric                          DAG.getConstant(ExtraAlignSpace, DL, PtrVT));
370081ad6265SDimitry Andric     Result = DAG.getNode(ISD::AND, DL, PtrVT, Result,
370181ad6265SDimitry Andric                          DAG.getConstant(~(RequiredAlign - 1), DL, PtrVT));
370281ad6265SDimitry Andric   }
370381ad6265SDimitry Andric 
370481ad6265SDimitry Andric   SDValue Ops[2] = {Result, Chain};
370581ad6265SDimitry Andric   return DAG.getMergeValues(Ops, DL);
370681ad6265SDimitry Andric }
370781ad6265SDimitry Andric 
370881ad6265SDimitry Andric SDValue
370981ad6265SDimitry Andric SystemZTargetLowering::lowerDYNAMIC_STACKALLOC_ELF(SDValue Op,
371081ad6265SDimitry Andric                                                    SelectionDAG &DAG) const {
37110b57cec5SDimitry Andric   const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
37120b57cec5SDimitry Andric   MachineFunction &MF = DAG.getMachineFunction();
37130b57cec5SDimitry Andric   bool RealignOpt = !MF.getFunction().hasFnAttribute("no-realign-stack");
37140b57cec5SDimitry Andric   bool StoreBackchain = MF.getFunction().hasFnAttribute("backchain");
37150b57cec5SDimitry Andric 
37160b57cec5SDimitry Andric   SDValue Chain = Op.getOperand(0);
37170b57cec5SDimitry Andric   SDValue Size  = Op.getOperand(1);
37180b57cec5SDimitry Andric   SDValue Align = Op.getOperand(2);
37190b57cec5SDimitry Andric   SDLoc DL(Op);
37200b57cec5SDimitry Andric 
37210b57cec5SDimitry Andric   // If user has set the no alignment function attribute, ignore
37220b57cec5SDimitry Andric   // alloca alignments.
3723e8d8bef9SDimitry Andric   uint64_t AlignVal =
3724e8d8bef9SDimitry Andric       (RealignOpt ? cast<ConstantSDNode>(Align)->getZExtValue() : 0);
37250b57cec5SDimitry Andric 
37260b57cec5SDimitry Andric   uint64_t StackAlign = TFI->getStackAlignment();
37270b57cec5SDimitry Andric   uint64_t RequiredAlign = std::max(AlignVal, StackAlign);
37280b57cec5SDimitry Andric   uint64_t ExtraAlignSpace = RequiredAlign - StackAlign;
37290b57cec5SDimitry Andric 
3730e8d8bef9SDimitry Andric   Register SPReg = getStackPointerRegisterToSaveRestore();
37310b57cec5SDimitry Andric   SDValue NeededSpace = Size;
37320b57cec5SDimitry Andric 
37330b57cec5SDimitry Andric   // Get a reference to the stack pointer.
37340b57cec5SDimitry Andric   SDValue OldSP = DAG.getCopyFromReg(Chain, DL, SPReg, MVT::i64);
37350b57cec5SDimitry Andric 
37360b57cec5SDimitry Andric   // If we need a backchain, save it now.
37370b57cec5SDimitry Andric   SDValue Backchain;
37380b57cec5SDimitry Andric   if (StoreBackchain)
3739e8d8bef9SDimitry Andric     Backchain = DAG.getLoad(MVT::i64, DL, Chain, getBackchainAddress(OldSP, DAG),
3740e8d8bef9SDimitry Andric                             MachinePointerInfo());
37410b57cec5SDimitry Andric 
37420b57cec5SDimitry Andric   // Add extra space for alignment if needed.
37430b57cec5SDimitry Andric   if (ExtraAlignSpace)
37440b57cec5SDimitry Andric     NeededSpace = DAG.getNode(ISD::ADD, DL, MVT::i64, NeededSpace,
37450b57cec5SDimitry Andric                               DAG.getConstant(ExtraAlignSpace, DL, MVT::i64));
37460b57cec5SDimitry Andric 
37470b57cec5SDimitry Andric   // Get the new stack pointer value.
37485ffd83dbSDimitry Andric   SDValue NewSP;
37495ffd83dbSDimitry Andric   if (hasInlineStackProbe(MF)) {
37505ffd83dbSDimitry Andric     NewSP = DAG.getNode(SystemZISD::PROBED_ALLOCA, DL,
37515ffd83dbSDimitry Andric                 DAG.getVTList(MVT::i64, MVT::Other), Chain, OldSP, NeededSpace);
37525ffd83dbSDimitry Andric     Chain = NewSP.getValue(1);
37535ffd83dbSDimitry Andric   }
37545ffd83dbSDimitry Andric   else {
37555ffd83dbSDimitry Andric     NewSP = DAG.getNode(ISD::SUB, DL, MVT::i64, OldSP, NeededSpace);
37560b57cec5SDimitry Andric     // Copy the new stack pointer back.
37570b57cec5SDimitry Andric     Chain = DAG.getCopyToReg(Chain, DL, SPReg, NewSP);
37585ffd83dbSDimitry Andric   }
37590b57cec5SDimitry Andric 
37600b57cec5SDimitry Andric   // The allocated data lives above the 160 bytes allocated for the standard
37610b57cec5SDimitry Andric   // frame, plus any outgoing stack arguments.  We don't know how much that
37620b57cec5SDimitry Andric   // amounts to yet, so emit a special ADJDYNALLOC placeholder.
37630b57cec5SDimitry Andric   SDValue ArgAdjust = DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
37640b57cec5SDimitry Andric   SDValue Result = DAG.getNode(ISD::ADD, DL, MVT::i64, NewSP, ArgAdjust);
37650b57cec5SDimitry Andric 
37660b57cec5SDimitry Andric   // Dynamically realign if needed.
37670b57cec5SDimitry Andric   if (RequiredAlign > StackAlign) {
37680b57cec5SDimitry Andric     Result =
37690b57cec5SDimitry Andric       DAG.getNode(ISD::ADD, DL, MVT::i64, Result,
37700b57cec5SDimitry Andric                   DAG.getConstant(ExtraAlignSpace, DL, MVT::i64));
37710b57cec5SDimitry Andric     Result =
37720b57cec5SDimitry Andric       DAG.getNode(ISD::AND, DL, MVT::i64, Result,
37730b57cec5SDimitry Andric                   DAG.getConstant(~(RequiredAlign - 1), DL, MVT::i64));
37740b57cec5SDimitry Andric   }
37750b57cec5SDimitry Andric 
37760b57cec5SDimitry Andric   if (StoreBackchain)
3777e8d8bef9SDimitry Andric     Chain = DAG.getStore(Chain, DL, Backchain, getBackchainAddress(NewSP, DAG),
3778e8d8bef9SDimitry Andric                          MachinePointerInfo());
37790b57cec5SDimitry Andric 
37800b57cec5SDimitry Andric   SDValue Ops[2] = { Result, Chain };
37810b57cec5SDimitry Andric   return DAG.getMergeValues(Ops, DL);
37820b57cec5SDimitry Andric }
37830b57cec5SDimitry Andric 
37840b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerGET_DYNAMIC_AREA_OFFSET(
37850b57cec5SDimitry Andric     SDValue Op, SelectionDAG &DAG) const {
37860b57cec5SDimitry Andric   SDLoc DL(Op);
37870b57cec5SDimitry Andric 
37880b57cec5SDimitry Andric   return DAG.getNode(SystemZISD::ADJDYNALLOC, DL, MVT::i64);
37890b57cec5SDimitry Andric }
37900b57cec5SDimitry Andric 
37910b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerSMUL_LOHI(SDValue Op,
37920b57cec5SDimitry Andric                                               SelectionDAG &DAG) const {
37930b57cec5SDimitry Andric   EVT VT = Op.getValueType();
37940b57cec5SDimitry Andric   SDLoc DL(Op);
37950b57cec5SDimitry Andric   SDValue Ops[2];
37960b57cec5SDimitry Andric   if (is32Bit(VT))
37970b57cec5SDimitry Andric     // Just do a normal 64-bit multiplication and extract the results.
37980b57cec5SDimitry Andric     // We define this so that it can be used for constant division.
37990b57cec5SDimitry Andric     lowerMUL_LOHI32(DAG, DL, ISD::SIGN_EXTEND, Op.getOperand(0),
38000b57cec5SDimitry Andric                     Op.getOperand(1), Ops[1], Ops[0]);
38010b57cec5SDimitry Andric   else if (Subtarget.hasMiscellaneousExtensions2())
38020b57cec5SDimitry Andric     // SystemZISD::SMUL_LOHI returns the low result in the odd register and
38030b57cec5SDimitry Andric     // the high result in the even register.  ISD::SMUL_LOHI is defined to
38040b57cec5SDimitry Andric     // return the low half first, so the results are in reverse order.
38050b57cec5SDimitry Andric     lowerGR128Binary(DAG, DL, VT, SystemZISD::SMUL_LOHI,
38060b57cec5SDimitry Andric                      Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
38070b57cec5SDimitry Andric   else {
38080b57cec5SDimitry Andric     // Do a full 128-bit multiplication based on SystemZISD::UMUL_LOHI:
38090b57cec5SDimitry Andric     //
38100b57cec5SDimitry Andric     //   (ll * rl) + ((lh * rl) << 64) + ((ll * rh) << 64)
38110b57cec5SDimitry Andric     //
38120b57cec5SDimitry Andric     // but using the fact that the upper halves are either all zeros
38130b57cec5SDimitry Andric     // or all ones:
38140b57cec5SDimitry Andric     //
38150b57cec5SDimitry Andric     //   (ll * rl) - ((lh & rl) << 64) - ((ll & rh) << 64)
38160b57cec5SDimitry Andric     //
38170b57cec5SDimitry Andric     // and grouping the right terms together since they are quicker than the
38180b57cec5SDimitry Andric     // multiplication:
38190b57cec5SDimitry Andric     //
38200b57cec5SDimitry Andric     //   (ll * rl) - (((lh & rl) + (ll & rh)) << 64)
38210b57cec5SDimitry Andric     SDValue C63 = DAG.getConstant(63, DL, MVT::i64);
38220b57cec5SDimitry Andric     SDValue LL = Op.getOperand(0);
38230b57cec5SDimitry Andric     SDValue RL = Op.getOperand(1);
38240b57cec5SDimitry Andric     SDValue LH = DAG.getNode(ISD::SRA, DL, VT, LL, C63);
38250b57cec5SDimitry Andric     SDValue RH = DAG.getNode(ISD::SRA, DL, VT, RL, C63);
38260b57cec5SDimitry Andric     // SystemZISD::UMUL_LOHI returns the low result in the odd register and
38270b57cec5SDimitry Andric     // the high result in the even register.  ISD::SMUL_LOHI is defined to
38280b57cec5SDimitry Andric     // return the low half first, so the results are in reverse order.
38290b57cec5SDimitry Andric     lowerGR128Binary(DAG, DL, VT, SystemZISD::UMUL_LOHI,
38300b57cec5SDimitry Andric                      LL, RL, Ops[1], Ops[0]);
38310b57cec5SDimitry Andric     SDValue NegLLTimesRH = DAG.getNode(ISD::AND, DL, VT, LL, RH);
38320b57cec5SDimitry Andric     SDValue NegLHTimesRL = DAG.getNode(ISD::AND, DL, VT, LH, RL);
38330b57cec5SDimitry Andric     SDValue NegSum = DAG.getNode(ISD::ADD, DL, VT, NegLLTimesRH, NegLHTimesRL);
38340b57cec5SDimitry Andric     Ops[1] = DAG.getNode(ISD::SUB, DL, VT, Ops[1], NegSum);
38350b57cec5SDimitry Andric   }
38360b57cec5SDimitry Andric   return DAG.getMergeValues(Ops, DL);
38370b57cec5SDimitry Andric }
38380b57cec5SDimitry Andric 
38390b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerUMUL_LOHI(SDValue Op,
38400b57cec5SDimitry Andric                                               SelectionDAG &DAG) const {
38410b57cec5SDimitry Andric   EVT VT = Op.getValueType();
38420b57cec5SDimitry Andric   SDLoc DL(Op);
38430b57cec5SDimitry Andric   SDValue Ops[2];
38440b57cec5SDimitry Andric   if (is32Bit(VT))
38450b57cec5SDimitry Andric     // Just do a normal 64-bit multiplication and extract the results.
38460b57cec5SDimitry Andric     // We define this so that it can be used for constant division.
38470b57cec5SDimitry Andric     lowerMUL_LOHI32(DAG, DL, ISD::ZERO_EXTEND, Op.getOperand(0),
38480b57cec5SDimitry Andric                     Op.getOperand(1), Ops[1], Ops[0]);
38490b57cec5SDimitry Andric   else
38500b57cec5SDimitry Andric     // SystemZISD::UMUL_LOHI returns the low result in the odd register and
38510b57cec5SDimitry Andric     // the high result in the even register.  ISD::UMUL_LOHI is defined to
38520b57cec5SDimitry Andric     // return the low half first, so the results are in reverse order.
38530b57cec5SDimitry Andric     lowerGR128Binary(DAG, DL, VT, SystemZISD::UMUL_LOHI,
38540b57cec5SDimitry Andric                      Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
38550b57cec5SDimitry Andric   return DAG.getMergeValues(Ops, DL);
38560b57cec5SDimitry Andric }
38570b57cec5SDimitry Andric 
38580b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerSDIVREM(SDValue Op,
38590b57cec5SDimitry Andric                                             SelectionDAG &DAG) const {
38600b57cec5SDimitry Andric   SDValue Op0 = Op.getOperand(0);
38610b57cec5SDimitry Andric   SDValue Op1 = Op.getOperand(1);
38620b57cec5SDimitry Andric   EVT VT = Op.getValueType();
38630b57cec5SDimitry Andric   SDLoc DL(Op);
38640b57cec5SDimitry Andric 
38650b57cec5SDimitry Andric   // We use DSGF for 32-bit division.  This means the first operand must
38660b57cec5SDimitry Andric   // always be 64-bit, and the second operand should be 32-bit whenever
38670b57cec5SDimitry Andric   // that is possible, to improve performance.
38680b57cec5SDimitry Andric   if (is32Bit(VT))
38690b57cec5SDimitry Andric     Op0 = DAG.getNode(ISD::SIGN_EXTEND, DL, MVT::i64, Op0);
38700b57cec5SDimitry Andric   else if (DAG.ComputeNumSignBits(Op1) > 32)
38710b57cec5SDimitry Andric     Op1 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Op1);
38720b57cec5SDimitry Andric 
38730b57cec5SDimitry Andric   // DSG(F) returns the remainder in the even register and the
38740b57cec5SDimitry Andric   // quotient in the odd register.
38750b57cec5SDimitry Andric   SDValue Ops[2];
38760b57cec5SDimitry Andric   lowerGR128Binary(DAG, DL, VT, SystemZISD::SDIVREM, Op0, Op1, Ops[1], Ops[0]);
38770b57cec5SDimitry Andric   return DAG.getMergeValues(Ops, DL);
38780b57cec5SDimitry Andric }
38790b57cec5SDimitry Andric 
38800b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerUDIVREM(SDValue Op,
38810b57cec5SDimitry Andric                                             SelectionDAG &DAG) const {
38820b57cec5SDimitry Andric   EVT VT = Op.getValueType();
38830b57cec5SDimitry Andric   SDLoc DL(Op);
38840b57cec5SDimitry Andric 
38850b57cec5SDimitry Andric   // DL(G) returns the remainder in the even register and the
38860b57cec5SDimitry Andric   // quotient in the odd register.
38870b57cec5SDimitry Andric   SDValue Ops[2];
38880b57cec5SDimitry Andric   lowerGR128Binary(DAG, DL, VT, SystemZISD::UDIVREM,
38890b57cec5SDimitry Andric                    Op.getOperand(0), Op.getOperand(1), Ops[1], Ops[0]);
38900b57cec5SDimitry Andric   return DAG.getMergeValues(Ops, DL);
38910b57cec5SDimitry Andric }
38920b57cec5SDimitry Andric 
38930b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerOR(SDValue Op, SelectionDAG &DAG) const {
38940b57cec5SDimitry Andric   assert(Op.getValueType() == MVT::i64 && "Should be 64-bit operation");
38950b57cec5SDimitry Andric 
38960b57cec5SDimitry Andric   // Get the known-zero masks for each operand.
38970b57cec5SDimitry Andric   SDValue Ops[] = {Op.getOperand(0), Op.getOperand(1)};
38980b57cec5SDimitry Andric   KnownBits Known[2] = {DAG.computeKnownBits(Ops[0]),
38990b57cec5SDimitry Andric                         DAG.computeKnownBits(Ops[1])};
39000b57cec5SDimitry Andric 
39010b57cec5SDimitry Andric   // See if the upper 32 bits of one operand and the lower 32 bits of the
39020b57cec5SDimitry Andric   // other are known zero.  They are the low and high operands respectively.
39030b57cec5SDimitry Andric   uint64_t Masks[] = { Known[0].Zero.getZExtValue(),
39040b57cec5SDimitry Andric                        Known[1].Zero.getZExtValue() };
39050b57cec5SDimitry Andric   unsigned High, Low;
39060b57cec5SDimitry Andric   if ((Masks[0] >> 32) == 0xffffffff && uint32_t(Masks[1]) == 0xffffffff)
39070b57cec5SDimitry Andric     High = 1, Low = 0;
39080b57cec5SDimitry Andric   else if ((Masks[1] >> 32) == 0xffffffff && uint32_t(Masks[0]) == 0xffffffff)
39090b57cec5SDimitry Andric     High = 0, Low = 1;
39100b57cec5SDimitry Andric   else
39110b57cec5SDimitry Andric     return Op;
39120b57cec5SDimitry Andric 
39130b57cec5SDimitry Andric   SDValue LowOp = Ops[Low];
39140b57cec5SDimitry Andric   SDValue HighOp = Ops[High];
39150b57cec5SDimitry Andric 
39160b57cec5SDimitry Andric   // If the high part is a constant, we're better off using IILH.
39170b57cec5SDimitry Andric   if (HighOp.getOpcode() == ISD::Constant)
39180b57cec5SDimitry Andric     return Op;
39190b57cec5SDimitry Andric 
39200b57cec5SDimitry Andric   // If the low part is a constant that is outside the range of LHI,
39210b57cec5SDimitry Andric   // then we're better off using IILF.
39220b57cec5SDimitry Andric   if (LowOp.getOpcode() == ISD::Constant) {
39230b57cec5SDimitry Andric     int64_t Value = int32_t(cast<ConstantSDNode>(LowOp)->getZExtValue());
39240b57cec5SDimitry Andric     if (!isInt<16>(Value))
39250b57cec5SDimitry Andric       return Op;
39260b57cec5SDimitry Andric   }
39270b57cec5SDimitry Andric 
39280b57cec5SDimitry Andric   // Check whether the high part is an AND that doesn't change the
39290b57cec5SDimitry Andric   // high 32 bits and just masks out low bits.  We can skip it if so.
39300b57cec5SDimitry Andric   if (HighOp.getOpcode() == ISD::AND &&
39310b57cec5SDimitry Andric       HighOp.getOperand(1).getOpcode() == ISD::Constant) {
39320b57cec5SDimitry Andric     SDValue HighOp0 = HighOp.getOperand(0);
39330b57cec5SDimitry Andric     uint64_t Mask = cast<ConstantSDNode>(HighOp.getOperand(1))->getZExtValue();
39340b57cec5SDimitry Andric     if (DAG.MaskedValueIsZero(HighOp0, APInt(64, ~(Mask | 0xffffffff))))
39350b57cec5SDimitry Andric       HighOp = HighOp0;
39360b57cec5SDimitry Andric   }
39370b57cec5SDimitry Andric 
39380b57cec5SDimitry Andric   // Take advantage of the fact that all GR32 operations only change the
39390b57cec5SDimitry Andric   // low 32 bits by truncating Low to an i32 and inserting it directly
39400b57cec5SDimitry Andric   // using a subreg.  The interesting cases are those where the truncation
39410b57cec5SDimitry Andric   // can be folded.
39420b57cec5SDimitry Andric   SDLoc DL(Op);
39430b57cec5SDimitry Andric   SDValue Low32 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, LowOp);
39440b57cec5SDimitry Andric   return DAG.getTargetInsertSubreg(SystemZ::subreg_l32, DL,
39450b57cec5SDimitry Andric                                    MVT::i64, HighOp, Low32);
39460b57cec5SDimitry Andric }
39470b57cec5SDimitry Andric 
39480b57cec5SDimitry Andric // Lower SADDO/SSUBO/UADDO/USUBO nodes.
39490b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerXALUO(SDValue Op,
39500b57cec5SDimitry Andric                                           SelectionDAG &DAG) const {
39510b57cec5SDimitry Andric   SDNode *N = Op.getNode();
39520b57cec5SDimitry Andric   SDValue LHS = N->getOperand(0);
39530b57cec5SDimitry Andric   SDValue RHS = N->getOperand(1);
39540b57cec5SDimitry Andric   SDLoc DL(N);
39550b57cec5SDimitry Andric   unsigned BaseOp = 0;
39560b57cec5SDimitry Andric   unsigned CCValid = 0;
39570b57cec5SDimitry Andric   unsigned CCMask = 0;
39580b57cec5SDimitry Andric 
39590b57cec5SDimitry Andric   switch (Op.getOpcode()) {
39600b57cec5SDimitry Andric   default: llvm_unreachable("Unknown instruction!");
39610b57cec5SDimitry Andric   case ISD::SADDO:
39620b57cec5SDimitry Andric     BaseOp = SystemZISD::SADDO;
39630b57cec5SDimitry Andric     CCValid = SystemZ::CCMASK_ARITH;
39640b57cec5SDimitry Andric     CCMask = SystemZ::CCMASK_ARITH_OVERFLOW;
39650b57cec5SDimitry Andric     break;
39660b57cec5SDimitry Andric   case ISD::SSUBO:
39670b57cec5SDimitry Andric     BaseOp = SystemZISD::SSUBO;
39680b57cec5SDimitry Andric     CCValid = SystemZ::CCMASK_ARITH;
39690b57cec5SDimitry Andric     CCMask = SystemZ::CCMASK_ARITH_OVERFLOW;
39700b57cec5SDimitry Andric     break;
39710b57cec5SDimitry Andric   case ISD::UADDO:
39720b57cec5SDimitry Andric     BaseOp = SystemZISD::UADDO;
39730b57cec5SDimitry Andric     CCValid = SystemZ::CCMASK_LOGICAL;
39740b57cec5SDimitry Andric     CCMask = SystemZ::CCMASK_LOGICAL_CARRY;
39750b57cec5SDimitry Andric     break;
39760b57cec5SDimitry Andric   case ISD::USUBO:
39770b57cec5SDimitry Andric     BaseOp = SystemZISD::USUBO;
39780b57cec5SDimitry Andric     CCValid = SystemZ::CCMASK_LOGICAL;
39790b57cec5SDimitry Andric     CCMask = SystemZ::CCMASK_LOGICAL_BORROW;
39800b57cec5SDimitry Andric     break;
39810b57cec5SDimitry Andric   }
39820b57cec5SDimitry Andric 
39830b57cec5SDimitry Andric   SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
39840b57cec5SDimitry Andric   SDValue Result = DAG.getNode(BaseOp, DL, VTs, LHS, RHS);
39850b57cec5SDimitry Andric 
39860b57cec5SDimitry Andric   SDValue SetCC = emitSETCC(DAG, DL, Result.getValue(1), CCValid, CCMask);
39870b57cec5SDimitry Andric   if (N->getValueType(1) == MVT::i1)
39880b57cec5SDimitry Andric     SetCC = DAG.getNode(ISD::TRUNCATE, DL, MVT::i1, SetCC);
39890b57cec5SDimitry Andric 
39900b57cec5SDimitry Andric   return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Result, SetCC);
39910b57cec5SDimitry Andric }
39920b57cec5SDimitry Andric 
39930b57cec5SDimitry Andric static bool isAddCarryChain(SDValue Carry) {
39940b57cec5SDimitry Andric   while (Carry.getOpcode() == ISD::ADDCARRY)
39950b57cec5SDimitry Andric     Carry = Carry.getOperand(2);
39960b57cec5SDimitry Andric   return Carry.getOpcode() == ISD::UADDO;
39970b57cec5SDimitry Andric }
39980b57cec5SDimitry Andric 
39990b57cec5SDimitry Andric static bool isSubBorrowChain(SDValue Carry) {
40000b57cec5SDimitry Andric   while (Carry.getOpcode() == ISD::SUBCARRY)
40010b57cec5SDimitry Andric     Carry = Carry.getOperand(2);
40020b57cec5SDimitry Andric   return Carry.getOpcode() == ISD::USUBO;
40030b57cec5SDimitry Andric }
40040b57cec5SDimitry Andric 
40050b57cec5SDimitry Andric // Lower ADDCARRY/SUBCARRY nodes.
40060b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerADDSUBCARRY(SDValue Op,
40070b57cec5SDimitry Andric                                                 SelectionDAG &DAG) const {
40080b57cec5SDimitry Andric 
40090b57cec5SDimitry Andric   SDNode *N = Op.getNode();
40100b57cec5SDimitry Andric   MVT VT = N->getSimpleValueType(0);
40110b57cec5SDimitry Andric 
40120b57cec5SDimitry Andric   // Let legalize expand this if it isn't a legal type yet.
40130b57cec5SDimitry Andric   if (!DAG.getTargetLoweringInfo().isTypeLegal(VT))
40140b57cec5SDimitry Andric     return SDValue();
40150b57cec5SDimitry Andric 
40160b57cec5SDimitry Andric   SDValue LHS = N->getOperand(0);
40170b57cec5SDimitry Andric   SDValue RHS = N->getOperand(1);
40180b57cec5SDimitry Andric   SDValue Carry = Op.getOperand(2);
40190b57cec5SDimitry Andric   SDLoc DL(N);
40200b57cec5SDimitry Andric   unsigned BaseOp = 0;
40210b57cec5SDimitry Andric   unsigned CCValid = 0;
40220b57cec5SDimitry Andric   unsigned CCMask = 0;
40230b57cec5SDimitry Andric 
40240b57cec5SDimitry Andric   switch (Op.getOpcode()) {
40250b57cec5SDimitry Andric   default: llvm_unreachable("Unknown instruction!");
40260b57cec5SDimitry Andric   case ISD::ADDCARRY:
40270b57cec5SDimitry Andric     if (!isAddCarryChain(Carry))
40280b57cec5SDimitry Andric       return SDValue();
40290b57cec5SDimitry Andric 
40300b57cec5SDimitry Andric     BaseOp = SystemZISD::ADDCARRY;
40310b57cec5SDimitry Andric     CCValid = SystemZ::CCMASK_LOGICAL;
40320b57cec5SDimitry Andric     CCMask = SystemZ::CCMASK_LOGICAL_CARRY;
40330b57cec5SDimitry Andric     break;
40340b57cec5SDimitry Andric   case ISD::SUBCARRY:
40350b57cec5SDimitry Andric     if (!isSubBorrowChain(Carry))
40360b57cec5SDimitry Andric       return SDValue();
40370b57cec5SDimitry Andric 
40380b57cec5SDimitry Andric     BaseOp = SystemZISD::SUBCARRY;
40390b57cec5SDimitry Andric     CCValid = SystemZ::CCMASK_LOGICAL;
40400b57cec5SDimitry Andric     CCMask = SystemZ::CCMASK_LOGICAL_BORROW;
40410b57cec5SDimitry Andric     break;
40420b57cec5SDimitry Andric   }
40430b57cec5SDimitry Andric 
40440b57cec5SDimitry Andric   // Set the condition code from the carry flag.
40450b57cec5SDimitry Andric   Carry = DAG.getNode(SystemZISD::GET_CCMASK, DL, MVT::i32, Carry,
40460b57cec5SDimitry Andric                       DAG.getConstant(CCValid, DL, MVT::i32),
40470b57cec5SDimitry Andric                       DAG.getConstant(CCMask, DL, MVT::i32));
40480b57cec5SDimitry Andric 
40490b57cec5SDimitry Andric   SDVTList VTs = DAG.getVTList(VT, MVT::i32);
40500b57cec5SDimitry Andric   SDValue Result = DAG.getNode(BaseOp, DL, VTs, LHS, RHS, Carry);
40510b57cec5SDimitry Andric 
40520b57cec5SDimitry Andric   SDValue SetCC = emitSETCC(DAG, DL, Result.getValue(1), CCValid, CCMask);
40530b57cec5SDimitry Andric   if (N->getValueType(1) == MVT::i1)
40540b57cec5SDimitry Andric     SetCC = DAG.getNode(ISD::TRUNCATE, DL, MVT::i1, SetCC);
40550b57cec5SDimitry Andric 
40560b57cec5SDimitry Andric   return DAG.getNode(ISD::MERGE_VALUES, DL, N->getVTList(), Result, SetCC);
40570b57cec5SDimitry Andric }
40580b57cec5SDimitry Andric 
40590b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerCTPOP(SDValue Op,
40600b57cec5SDimitry Andric                                           SelectionDAG &DAG) const {
40610b57cec5SDimitry Andric   EVT VT = Op.getValueType();
40620b57cec5SDimitry Andric   SDLoc DL(Op);
40630b57cec5SDimitry Andric   Op = Op.getOperand(0);
40640b57cec5SDimitry Andric 
40650b57cec5SDimitry Andric   // Handle vector types via VPOPCT.
40660b57cec5SDimitry Andric   if (VT.isVector()) {
40670b57cec5SDimitry Andric     Op = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Op);
40680b57cec5SDimitry Andric     Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::v16i8, Op);
40690b57cec5SDimitry Andric     switch (VT.getScalarSizeInBits()) {
40700b57cec5SDimitry Andric     case 8:
40710b57cec5SDimitry Andric       break;
40720b57cec5SDimitry Andric     case 16: {
40730b57cec5SDimitry Andric       Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
40740b57cec5SDimitry Andric       SDValue Shift = DAG.getConstant(8, DL, MVT::i32);
40750b57cec5SDimitry Andric       SDValue Tmp = DAG.getNode(SystemZISD::VSHL_BY_SCALAR, DL, VT, Op, Shift);
40760b57cec5SDimitry Andric       Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
40770b57cec5SDimitry Andric       Op = DAG.getNode(SystemZISD::VSRL_BY_SCALAR, DL, VT, Op, Shift);
40780b57cec5SDimitry Andric       break;
40790b57cec5SDimitry Andric     }
40800b57cec5SDimitry Andric     case 32: {
40810b57cec5SDimitry Andric       SDValue Tmp = DAG.getSplatBuildVector(MVT::v16i8, DL,
40820b57cec5SDimitry Andric                                             DAG.getConstant(0, DL, MVT::i32));
40830b57cec5SDimitry Andric       Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
40840b57cec5SDimitry Andric       break;
40850b57cec5SDimitry Andric     }
40860b57cec5SDimitry Andric     case 64: {
40870b57cec5SDimitry Andric       SDValue Tmp = DAG.getSplatBuildVector(MVT::v16i8, DL,
40880b57cec5SDimitry Andric                                             DAG.getConstant(0, DL, MVT::i32));
40890b57cec5SDimitry Andric       Op = DAG.getNode(SystemZISD::VSUM, DL, MVT::v4i32, Op, Tmp);
40900b57cec5SDimitry Andric       Op = DAG.getNode(SystemZISD::VSUM, DL, VT, Op, Tmp);
40910b57cec5SDimitry Andric       break;
40920b57cec5SDimitry Andric     }
40930b57cec5SDimitry Andric     default:
40940b57cec5SDimitry Andric       llvm_unreachable("Unexpected type");
40950b57cec5SDimitry Andric     }
40960b57cec5SDimitry Andric     return Op;
40970b57cec5SDimitry Andric   }
40980b57cec5SDimitry Andric 
40990b57cec5SDimitry Andric   // Get the known-zero mask for the operand.
41000b57cec5SDimitry Andric   KnownBits Known = DAG.computeKnownBits(Op);
4101480093f4SDimitry Andric   unsigned NumSignificantBits = Known.getMaxValue().getActiveBits();
41020b57cec5SDimitry Andric   if (NumSignificantBits == 0)
41030b57cec5SDimitry Andric     return DAG.getConstant(0, DL, VT);
41040b57cec5SDimitry Andric 
41050b57cec5SDimitry Andric   // Skip known-zero high parts of the operand.
41060b57cec5SDimitry Andric   int64_t OrigBitSize = VT.getSizeInBits();
4107bdd1243dSDimitry Andric   int64_t BitSize = llvm::bit_ceil(NumSignificantBits);
41080b57cec5SDimitry Andric   BitSize = std::min(BitSize, OrigBitSize);
41090b57cec5SDimitry Andric 
41100b57cec5SDimitry Andric   // The POPCNT instruction counts the number of bits in each byte.
41110b57cec5SDimitry Andric   Op = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op);
41120b57cec5SDimitry Andric   Op = DAG.getNode(SystemZISD::POPCNT, DL, MVT::i64, Op);
41130b57cec5SDimitry Andric   Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
41140b57cec5SDimitry Andric 
41150b57cec5SDimitry Andric   // Add up per-byte counts in a binary tree.  All bits of Op at
41160b57cec5SDimitry Andric   // position larger than BitSize remain zero throughout.
41170b57cec5SDimitry Andric   for (int64_t I = BitSize / 2; I >= 8; I = I / 2) {
41180b57cec5SDimitry Andric     SDValue Tmp = DAG.getNode(ISD::SHL, DL, VT, Op, DAG.getConstant(I, DL, VT));
41190b57cec5SDimitry Andric     if (BitSize != OrigBitSize)
41200b57cec5SDimitry Andric       Tmp = DAG.getNode(ISD::AND, DL, VT, Tmp,
41210b57cec5SDimitry Andric                         DAG.getConstant(((uint64_t)1 << BitSize) - 1, DL, VT));
41220b57cec5SDimitry Andric     Op = DAG.getNode(ISD::ADD, DL, VT, Op, Tmp);
41230b57cec5SDimitry Andric   }
41240b57cec5SDimitry Andric 
41250b57cec5SDimitry Andric   // Extract overall result from high byte.
41260b57cec5SDimitry Andric   if (BitSize > 8)
41270b57cec5SDimitry Andric     Op = DAG.getNode(ISD::SRL, DL, VT, Op,
41280b57cec5SDimitry Andric                      DAG.getConstant(BitSize - 8, DL, VT));
41290b57cec5SDimitry Andric 
41300b57cec5SDimitry Andric   return Op;
41310b57cec5SDimitry Andric }
41320b57cec5SDimitry Andric 
41330b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerATOMIC_FENCE(SDValue Op,
41340b57cec5SDimitry Andric                                                  SelectionDAG &DAG) const {
41350b57cec5SDimitry Andric   SDLoc DL(Op);
41360b57cec5SDimitry Andric   AtomicOrdering FenceOrdering = static_cast<AtomicOrdering>(
41370b57cec5SDimitry Andric     cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue());
41380b57cec5SDimitry Andric   SyncScope::ID FenceSSID = static_cast<SyncScope::ID>(
41390b57cec5SDimitry Andric     cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue());
41400b57cec5SDimitry Andric 
41410b57cec5SDimitry Andric   // The only fence that needs an instruction is a sequentially-consistent
41420b57cec5SDimitry Andric   // cross-thread fence.
41430b57cec5SDimitry Andric   if (FenceOrdering == AtomicOrdering::SequentiallyConsistent &&
41440b57cec5SDimitry Andric       FenceSSID == SyncScope::System) {
41450b57cec5SDimitry Andric     return SDValue(DAG.getMachineNode(SystemZ::Serialize, DL, MVT::Other,
41460b57cec5SDimitry Andric                                       Op.getOperand(0)),
41470b57cec5SDimitry Andric                    0);
41480b57cec5SDimitry Andric   }
41490b57cec5SDimitry Andric 
41500b57cec5SDimitry Andric   // MEMBARRIER is a compiler barrier; it codegens to a no-op.
4151bdd1243dSDimitry Andric   return DAG.getNode(ISD::MEMBARRIER, DL, MVT::Other, Op.getOperand(0));
41520b57cec5SDimitry Andric }
41530b57cec5SDimitry Andric 
41540b57cec5SDimitry Andric // Op is an atomic load.  Lower it into a normal volatile load.
41550b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerATOMIC_LOAD(SDValue Op,
41560b57cec5SDimitry Andric                                                 SelectionDAG &DAG) const {
41570b57cec5SDimitry Andric   auto *Node = cast<AtomicSDNode>(Op.getNode());
41580b57cec5SDimitry Andric   return DAG.getExtLoad(ISD::EXTLOAD, SDLoc(Op), Op.getValueType(),
41590b57cec5SDimitry Andric                         Node->getChain(), Node->getBasePtr(),
41600b57cec5SDimitry Andric                         Node->getMemoryVT(), Node->getMemOperand());
41610b57cec5SDimitry Andric }
41620b57cec5SDimitry Andric 
41630b57cec5SDimitry Andric // Op is an atomic store.  Lower it into a normal volatile store.
41640b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerATOMIC_STORE(SDValue Op,
41650b57cec5SDimitry Andric                                                  SelectionDAG &DAG) const {
41660b57cec5SDimitry Andric   auto *Node = cast<AtomicSDNode>(Op.getNode());
41670b57cec5SDimitry Andric   SDValue Chain = DAG.getTruncStore(Node->getChain(), SDLoc(Op), Node->getVal(),
41680b57cec5SDimitry Andric                                     Node->getBasePtr(), Node->getMemoryVT(),
41690b57cec5SDimitry Andric                                     Node->getMemOperand());
41700b57cec5SDimitry Andric   // We have to enforce sequential consistency by performing a
41710b57cec5SDimitry Andric   // serialization operation after the store.
4172fe6060f1SDimitry Andric   if (Node->getSuccessOrdering() == AtomicOrdering::SequentiallyConsistent)
41730b57cec5SDimitry Andric     Chain = SDValue(DAG.getMachineNode(SystemZ::Serialize, SDLoc(Op),
41740b57cec5SDimitry Andric                                        MVT::Other, Chain), 0);
41750b57cec5SDimitry Andric   return Chain;
41760b57cec5SDimitry Andric }
41770b57cec5SDimitry Andric 
41780b57cec5SDimitry Andric // Op is an 8-, 16-bit or 32-bit ATOMIC_LOAD_* operation.  Lower the first
41790b57cec5SDimitry Andric // two into the fullword ATOMIC_LOADW_* operation given by Opcode.
41800b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerATOMIC_LOAD_OP(SDValue Op,
41810b57cec5SDimitry Andric                                                    SelectionDAG &DAG,
41820b57cec5SDimitry Andric                                                    unsigned Opcode) const {
41830b57cec5SDimitry Andric   auto *Node = cast<AtomicSDNode>(Op.getNode());
41840b57cec5SDimitry Andric 
41850b57cec5SDimitry Andric   // 32-bit operations need no code outside the main loop.
41860b57cec5SDimitry Andric   EVT NarrowVT = Node->getMemoryVT();
41870b57cec5SDimitry Andric   EVT WideVT = MVT::i32;
41880b57cec5SDimitry Andric   if (NarrowVT == WideVT)
41890b57cec5SDimitry Andric     return Op;
41900b57cec5SDimitry Andric 
41910b57cec5SDimitry Andric   int64_t BitSize = NarrowVT.getSizeInBits();
41920b57cec5SDimitry Andric   SDValue ChainIn = Node->getChain();
41930b57cec5SDimitry Andric   SDValue Addr = Node->getBasePtr();
41940b57cec5SDimitry Andric   SDValue Src2 = Node->getVal();
41950b57cec5SDimitry Andric   MachineMemOperand *MMO = Node->getMemOperand();
41960b57cec5SDimitry Andric   SDLoc DL(Node);
41970b57cec5SDimitry Andric   EVT PtrVT = Addr.getValueType();
41980b57cec5SDimitry Andric 
41990b57cec5SDimitry Andric   // Convert atomic subtracts of constants into additions.
42000b57cec5SDimitry Andric   if (Opcode == SystemZISD::ATOMIC_LOADW_SUB)
42010b57cec5SDimitry Andric     if (auto *Const = dyn_cast<ConstantSDNode>(Src2)) {
42020b57cec5SDimitry Andric       Opcode = SystemZISD::ATOMIC_LOADW_ADD;
42030b57cec5SDimitry Andric       Src2 = DAG.getConstant(-Const->getSExtValue(), DL, Src2.getValueType());
42040b57cec5SDimitry Andric     }
42050b57cec5SDimitry Andric 
42060b57cec5SDimitry Andric   // Get the address of the containing word.
42070b57cec5SDimitry Andric   SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
42080b57cec5SDimitry Andric                                     DAG.getConstant(-4, DL, PtrVT));
42090b57cec5SDimitry Andric 
42100b57cec5SDimitry Andric   // Get the number of bits that the word must be rotated left in order
42110b57cec5SDimitry Andric   // to bring the field to the top bits of a GR32.
42120b57cec5SDimitry Andric   SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
42130b57cec5SDimitry Andric                                  DAG.getConstant(3, DL, PtrVT));
42140b57cec5SDimitry Andric   BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
42150b57cec5SDimitry Andric 
42160b57cec5SDimitry Andric   // Get the complementing shift amount, for rotating a field in the top
42170b57cec5SDimitry Andric   // bits back to its proper position.
42180b57cec5SDimitry Andric   SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
42190b57cec5SDimitry Andric                                     DAG.getConstant(0, DL, WideVT), BitShift);
42200b57cec5SDimitry Andric 
42210b57cec5SDimitry Andric   // Extend the source operand to 32 bits and prepare it for the inner loop.
42220b57cec5SDimitry Andric   // ATOMIC_SWAPW uses RISBG to rotate the field left, but all other
42230b57cec5SDimitry Andric   // operations require the source to be shifted in advance.  (This shift
42240b57cec5SDimitry Andric   // can be folded if the source is constant.)  For AND and NAND, the lower
42250b57cec5SDimitry Andric   // bits must be set, while for other opcodes they should be left clear.
42260b57cec5SDimitry Andric   if (Opcode != SystemZISD::ATOMIC_SWAPW)
42270b57cec5SDimitry Andric     Src2 = DAG.getNode(ISD::SHL, DL, WideVT, Src2,
42280b57cec5SDimitry Andric                        DAG.getConstant(32 - BitSize, DL, WideVT));
42290b57cec5SDimitry Andric   if (Opcode == SystemZISD::ATOMIC_LOADW_AND ||
42300b57cec5SDimitry Andric       Opcode == SystemZISD::ATOMIC_LOADW_NAND)
42310b57cec5SDimitry Andric     Src2 = DAG.getNode(ISD::OR, DL, WideVT, Src2,
42320b57cec5SDimitry Andric                        DAG.getConstant(uint32_t(-1) >> BitSize, DL, WideVT));
42330b57cec5SDimitry Andric 
42340b57cec5SDimitry Andric   // Construct the ATOMIC_LOADW_* node.
42350b57cec5SDimitry Andric   SDVTList VTList = DAG.getVTList(WideVT, MVT::Other);
42360b57cec5SDimitry Andric   SDValue Ops[] = { ChainIn, AlignedAddr, Src2, BitShift, NegBitShift,
42370b57cec5SDimitry Andric                     DAG.getConstant(BitSize, DL, WideVT) };
42380b57cec5SDimitry Andric   SDValue AtomicOp = DAG.getMemIntrinsicNode(Opcode, DL, VTList, Ops,
42390b57cec5SDimitry Andric                                              NarrowVT, MMO);
42400b57cec5SDimitry Andric 
42410b57cec5SDimitry Andric   // Rotate the result of the final CS so that the field is in the lower
42420b57cec5SDimitry Andric   // bits of a GR32, then truncate it.
42430b57cec5SDimitry Andric   SDValue ResultShift = DAG.getNode(ISD::ADD, DL, WideVT, BitShift,
42440b57cec5SDimitry Andric                                     DAG.getConstant(BitSize, DL, WideVT));
42450b57cec5SDimitry Andric   SDValue Result = DAG.getNode(ISD::ROTL, DL, WideVT, AtomicOp, ResultShift);
42460b57cec5SDimitry Andric 
42470b57cec5SDimitry Andric   SDValue RetOps[2] = { Result, AtomicOp.getValue(1) };
42480b57cec5SDimitry Andric   return DAG.getMergeValues(RetOps, DL);
42490b57cec5SDimitry Andric }
42500b57cec5SDimitry Andric 
42510b57cec5SDimitry Andric // Op is an ATOMIC_LOAD_SUB operation.  Lower 8- and 16-bit operations
42520b57cec5SDimitry Andric // into ATOMIC_LOADW_SUBs and decide whether to convert 32- and 64-bit
42530b57cec5SDimitry Andric // operations into additions.
42540b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerATOMIC_LOAD_SUB(SDValue Op,
42550b57cec5SDimitry Andric                                                     SelectionDAG &DAG) const {
42560b57cec5SDimitry Andric   auto *Node = cast<AtomicSDNode>(Op.getNode());
42570b57cec5SDimitry Andric   EVT MemVT = Node->getMemoryVT();
42580b57cec5SDimitry Andric   if (MemVT == MVT::i32 || MemVT == MVT::i64) {
42590b57cec5SDimitry Andric     // A full-width operation.
42600b57cec5SDimitry Andric     assert(Op.getValueType() == MemVT && "Mismatched VTs");
42610b57cec5SDimitry Andric     SDValue Src2 = Node->getVal();
42620b57cec5SDimitry Andric     SDValue NegSrc2;
42630b57cec5SDimitry Andric     SDLoc DL(Src2);
42640b57cec5SDimitry Andric 
42650b57cec5SDimitry Andric     if (auto *Op2 = dyn_cast<ConstantSDNode>(Src2)) {
42660b57cec5SDimitry Andric       // Use an addition if the operand is constant and either LAA(G) is
42670b57cec5SDimitry Andric       // available or the negative value is in the range of A(G)FHI.
42680b57cec5SDimitry Andric       int64_t Value = (-Op2->getAPIntValue()).getSExtValue();
42690b57cec5SDimitry Andric       if (isInt<32>(Value) || Subtarget.hasInterlockedAccess1())
42700b57cec5SDimitry Andric         NegSrc2 = DAG.getConstant(Value, DL, MemVT);
42710b57cec5SDimitry Andric     } else if (Subtarget.hasInterlockedAccess1())
42720b57cec5SDimitry Andric       // Use LAA(G) if available.
42730b57cec5SDimitry Andric       NegSrc2 = DAG.getNode(ISD::SUB, DL, MemVT, DAG.getConstant(0, DL, MemVT),
42740b57cec5SDimitry Andric                             Src2);
42750b57cec5SDimitry Andric 
42760b57cec5SDimitry Andric     if (NegSrc2.getNode())
42770b57cec5SDimitry Andric       return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, DL, MemVT,
42780b57cec5SDimitry Andric                            Node->getChain(), Node->getBasePtr(), NegSrc2,
42790b57cec5SDimitry Andric                            Node->getMemOperand());
42800b57cec5SDimitry Andric 
42810b57cec5SDimitry Andric     // Use the node as-is.
42820b57cec5SDimitry Andric     return Op;
42830b57cec5SDimitry Andric   }
42840b57cec5SDimitry Andric 
42850b57cec5SDimitry Andric   return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_SUB);
42860b57cec5SDimitry Andric }
42870b57cec5SDimitry Andric 
42880b57cec5SDimitry Andric // Lower 8/16/32/64-bit ATOMIC_CMP_SWAP_WITH_SUCCESS node.
42890b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerATOMIC_CMP_SWAP(SDValue Op,
42900b57cec5SDimitry Andric                                                     SelectionDAG &DAG) const {
42910b57cec5SDimitry Andric   auto *Node = cast<AtomicSDNode>(Op.getNode());
42920b57cec5SDimitry Andric   SDValue ChainIn = Node->getOperand(0);
42930b57cec5SDimitry Andric   SDValue Addr = Node->getOperand(1);
42940b57cec5SDimitry Andric   SDValue CmpVal = Node->getOperand(2);
42950b57cec5SDimitry Andric   SDValue SwapVal = Node->getOperand(3);
42960b57cec5SDimitry Andric   MachineMemOperand *MMO = Node->getMemOperand();
42970b57cec5SDimitry Andric   SDLoc DL(Node);
42980b57cec5SDimitry Andric 
42990b57cec5SDimitry Andric   // We have native support for 32-bit and 64-bit compare and swap, but we
43000b57cec5SDimitry Andric   // still need to expand extracting the "success" result from the CC.
43010b57cec5SDimitry Andric   EVT NarrowVT = Node->getMemoryVT();
43020b57cec5SDimitry Andric   EVT WideVT = NarrowVT == MVT::i64 ? MVT::i64 : MVT::i32;
43030b57cec5SDimitry Andric   if (NarrowVT == WideVT) {
43040b57cec5SDimitry Andric     SDVTList Tys = DAG.getVTList(WideVT, MVT::i32, MVT::Other);
43050b57cec5SDimitry Andric     SDValue Ops[] = { ChainIn, Addr, CmpVal, SwapVal };
43060b57cec5SDimitry Andric     SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAP,
43070b57cec5SDimitry Andric                                                DL, Tys, Ops, NarrowVT, MMO);
43080b57cec5SDimitry Andric     SDValue Success = emitSETCC(DAG, DL, AtomicOp.getValue(1),
43090b57cec5SDimitry Andric                                 SystemZ::CCMASK_CS, SystemZ::CCMASK_CS_EQ);
43100b57cec5SDimitry Andric 
43110b57cec5SDimitry Andric     DAG.ReplaceAllUsesOfValueWith(Op.getValue(0), AtomicOp.getValue(0));
43120b57cec5SDimitry Andric     DAG.ReplaceAllUsesOfValueWith(Op.getValue(1), Success);
43130b57cec5SDimitry Andric     DAG.ReplaceAllUsesOfValueWith(Op.getValue(2), AtomicOp.getValue(2));
43140b57cec5SDimitry Andric     return SDValue();
43150b57cec5SDimitry Andric   }
43160b57cec5SDimitry Andric 
43170b57cec5SDimitry Andric   // Convert 8-bit and 16-bit compare and swap to a loop, implemented
43180b57cec5SDimitry Andric   // via a fullword ATOMIC_CMP_SWAPW operation.
43190b57cec5SDimitry Andric   int64_t BitSize = NarrowVT.getSizeInBits();
43200b57cec5SDimitry Andric   EVT PtrVT = Addr.getValueType();
43210b57cec5SDimitry Andric 
43220b57cec5SDimitry Andric   // Get the address of the containing word.
43230b57cec5SDimitry Andric   SDValue AlignedAddr = DAG.getNode(ISD::AND, DL, PtrVT, Addr,
43240b57cec5SDimitry Andric                                     DAG.getConstant(-4, DL, PtrVT));
43250b57cec5SDimitry Andric 
43260b57cec5SDimitry Andric   // Get the number of bits that the word must be rotated left in order
43270b57cec5SDimitry Andric   // to bring the field to the top bits of a GR32.
43280b57cec5SDimitry Andric   SDValue BitShift = DAG.getNode(ISD::SHL, DL, PtrVT, Addr,
43290b57cec5SDimitry Andric                                  DAG.getConstant(3, DL, PtrVT));
43300b57cec5SDimitry Andric   BitShift = DAG.getNode(ISD::TRUNCATE, DL, WideVT, BitShift);
43310b57cec5SDimitry Andric 
43320b57cec5SDimitry Andric   // Get the complementing shift amount, for rotating a field in the top
43330b57cec5SDimitry Andric   // bits back to its proper position.
43340b57cec5SDimitry Andric   SDValue NegBitShift = DAG.getNode(ISD::SUB, DL, WideVT,
43350b57cec5SDimitry Andric                                     DAG.getConstant(0, DL, WideVT), BitShift);
43360b57cec5SDimitry Andric 
43370b57cec5SDimitry Andric   // Construct the ATOMIC_CMP_SWAPW node.
43380b57cec5SDimitry Andric   SDVTList VTList = DAG.getVTList(WideVT, MVT::i32, MVT::Other);
43390b57cec5SDimitry Andric   SDValue Ops[] = { ChainIn, AlignedAddr, CmpVal, SwapVal, BitShift,
43400b57cec5SDimitry Andric                     NegBitShift, DAG.getConstant(BitSize, DL, WideVT) };
43410b57cec5SDimitry Andric   SDValue AtomicOp = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAPW, DL,
43420b57cec5SDimitry Andric                                              VTList, Ops, NarrowVT, MMO);
43430b57cec5SDimitry Andric   SDValue Success = emitSETCC(DAG, DL, AtomicOp.getValue(1),
43440b57cec5SDimitry Andric                               SystemZ::CCMASK_ICMP, SystemZ::CCMASK_CMP_EQ);
43450b57cec5SDimitry Andric 
4346fe6060f1SDimitry Andric   // emitAtomicCmpSwapW() will zero extend the result (original value).
4347fe6060f1SDimitry Andric   SDValue OrigVal = DAG.getNode(ISD::AssertZext, DL, WideVT, AtomicOp.getValue(0),
4348fe6060f1SDimitry Andric                                 DAG.getValueType(NarrowVT));
4349fe6060f1SDimitry Andric   DAG.ReplaceAllUsesOfValueWith(Op.getValue(0), OrigVal);
43500b57cec5SDimitry Andric   DAG.ReplaceAllUsesOfValueWith(Op.getValue(1), Success);
43510b57cec5SDimitry Andric   DAG.ReplaceAllUsesOfValueWith(Op.getValue(2), AtomicOp.getValue(2));
43520b57cec5SDimitry Andric   return SDValue();
43530b57cec5SDimitry Andric }
43540b57cec5SDimitry Andric 
43550b57cec5SDimitry Andric MachineMemOperand::Flags
43565ffd83dbSDimitry Andric SystemZTargetLowering::getTargetMMOFlags(const Instruction &I) const {
43570b57cec5SDimitry Andric   // Because of how we convert atomic_load and atomic_store to normal loads and
43580b57cec5SDimitry Andric   // stores in the DAG, we need to ensure that the MMOs are marked volatile
43590b57cec5SDimitry Andric   // since DAGCombine hasn't been updated to account for atomic, but non
43600b57cec5SDimitry Andric   // volatile loads.  (See D57601)
43610b57cec5SDimitry Andric   if (auto *SI = dyn_cast<StoreInst>(&I))
43620b57cec5SDimitry Andric     if (SI->isAtomic())
43630b57cec5SDimitry Andric       return MachineMemOperand::MOVolatile;
43640b57cec5SDimitry Andric   if (auto *LI = dyn_cast<LoadInst>(&I))
43650b57cec5SDimitry Andric     if (LI->isAtomic())
43660b57cec5SDimitry Andric       return MachineMemOperand::MOVolatile;
43670b57cec5SDimitry Andric   if (auto *AI = dyn_cast<AtomicRMWInst>(&I))
43680b57cec5SDimitry Andric     if (AI->isAtomic())
43690b57cec5SDimitry Andric       return MachineMemOperand::MOVolatile;
43700b57cec5SDimitry Andric   if (auto *AI = dyn_cast<AtomicCmpXchgInst>(&I))
43710b57cec5SDimitry Andric     if (AI->isAtomic())
43720b57cec5SDimitry Andric       return MachineMemOperand::MOVolatile;
43730b57cec5SDimitry Andric   return MachineMemOperand::MONone;
43740b57cec5SDimitry Andric }
43750b57cec5SDimitry Andric 
43760b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerSTACKSAVE(SDValue Op,
43770b57cec5SDimitry Andric                                               SelectionDAG &DAG) const {
43780b57cec5SDimitry Andric   MachineFunction &MF = DAG.getMachineFunction();
4379349cc55cSDimitry Andric   const SystemZSubtarget *Subtarget = &MF.getSubtarget<SystemZSubtarget>();
4380349cc55cSDimitry Andric   auto *Regs = Subtarget->getSpecialRegisters();
4381480093f4SDimitry Andric   if (MF.getFunction().getCallingConv() == CallingConv::GHC)
4382480093f4SDimitry Andric     report_fatal_error("Variable-sized stack allocations are not supported "
4383480093f4SDimitry Andric                        "in GHC calling convention");
43840b57cec5SDimitry Andric   return DAG.getCopyFromReg(Op.getOperand(0), SDLoc(Op),
4385349cc55cSDimitry Andric                             Regs->getStackPointerRegister(), Op.getValueType());
43860b57cec5SDimitry Andric }
43870b57cec5SDimitry Andric 
43880b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerSTACKRESTORE(SDValue Op,
43890b57cec5SDimitry Andric                                                  SelectionDAG &DAG) const {
43900b57cec5SDimitry Andric   MachineFunction &MF = DAG.getMachineFunction();
4391349cc55cSDimitry Andric   const SystemZSubtarget *Subtarget = &MF.getSubtarget<SystemZSubtarget>();
4392349cc55cSDimitry Andric   auto *Regs = Subtarget->getSpecialRegisters();
43930b57cec5SDimitry Andric   bool StoreBackchain = MF.getFunction().hasFnAttribute("backchain");
43940b57cec5SDimitry Andric 
4395480093f4SDimitry Andric   if (MF.getFunction().getCallingConv() == CallingConv::GHC)
4396480093f4SDimitry Andric     report_fatal_error("Variable-sized stack allocations are not supported "
4397480093f4SDimitry Andric                        "in GHC calling convention");
4398480093f4SDimitry Andric 
43990b57cec5SDimitry Andric   SDValue Chain = Op.getOperand(0);
44000b57cec5SDimitry Andric   SDValue NewSP = Op.getOperand(1);
44010b57cec5SDimitry Andric   SDValue Backchain;
44020b57cec5SDimitry Andric   SDLoc DL(Op);
44030b57cec5SDimitry Andric 
44040b57cec5SDimitry Andric   if (StoreBackchain) {
4405349cc55cSDimitry Andric     SDValue OldSP = DAG.getCopyFromReg(
4406349cc55cSDimitry Andric         Chain, DL, Regs->getStackPointerRegister(), MVT::i64);
4407e8d8bef9SDimitry Andric     Backchain = DAG.getLoad(MVT::i64, DL, Chain, getBackchainAddress(OldSP, DAG),
4408e8d8bef9SDimitry Andric                             MachinePointerInfo());
44090b57cec5SDimitry Andric   }
44100b57cec5SDimitry Andric 
4411349cc55cSDimitry Andric   Chain = DAG.getCopyToReg(Chain, DL, Regs->getStackPointerRegister(), NewSP);
44120b57cec5SDimitry Andric 
44130b57cec5SDimitry Andric   if (StoreBackchain)
4414e8d8bef9SDimitry Andric     Chain = DAG.getStore(Chain, DL, Backchain, getBackchainAddress(NewSP, DAG),
4415e8d8bef9SDimitry Andric                          MachinePointerInfo());
44160b57cec5SDimitry Andric 
44170b57cec5SDimitry Andric   return Chain;
44180b57cec5SDimitry Andric }
44190b57cec5SDimitry Andric 
44200b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerPREFETCH(SDValue Op,
44210b57cec5SDimitry Andric                                              SelectionDAG &DAG) const {
44220b57cec5SDimitry Andric   bool IsData = cast<ConstantSDNode>(Op.getOperand(4))->getZExtValue();
44230b57cec5SDimitry Andric   if (!IsData)
44240b57cec5SDimitry Andric     // Just preserve the chain.
44250b57cec5SDimitry Andric     return Op.getOperand(0);
44260b57cec5SDimitry Andric 
44270b57cec5SDimitry Andric   SDLoc DL(Op);
44280b57cec5SDimitry Andric   bool IsWrite = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue();
44290b57cec5SDimitry Andric   unsigned Code = IsWrite ? SystemZ::PFD_WRITE : SystemZ::PFD_READ;
44300b57cec5SDimitry Andric   auto *Node = cast<MemIntrinsicSDNode>(Op.getNode());
44318bcb0991SDimitry Andric   SDValue Ops[] = {Op.getOperand(0), DAG.getTargetConstant(Code, DL, MVT::i32),
44328bcb0991SDimitry Andric                    Op.getOperand(1)};
44330b57cec5SDimitry Andric   return DAG.getMemIntrinsicNode(SystemZISD::PREFETCH, DL,
44340b57cec5SDimitry Andric                                  Node->getVTList(), Ops,
44350b57cec5SDimitry Andric                                  Node->getMemoryVT(), Node->getMemOperand());
44360b57cec5SDimitry Andric }
44370b57cec5SDimitry Andric 
44380b57cec5SDimitry Andric // Convert condition code in CCReg to an i32 value.
44390b57cec5SDimitry Andric static SDValue getCCResult(SelectionDAG &DAG, SDValue CCReg) {
44400b57cec5SDimitry Andric   SDLoc DL(CCReg);
44410b57cec5SDimitry Andric   SDValue IPM = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, CCReg);
44420b57cec5SDimitry Andric   return DAG.getNode(ISD::SRL, DL, MVT::i32, IPM,
44430b57cec5SDimitry Andric                      DAG.getConstant(SystemZ::IPM_CC, DL, MVT::i32));
44440b57cec5SDimitry Andric }
44450b57cec5SDimitry Andric 
44460b57cec5SDimitry Andric SDValue
44470b57cec5SDimitry Andric SystemZTargetLowering::lowerINTRINSIC_W_CHAIN(SDValue Op,
44480b57cec5SDimitry Andric                                               SelectionDAG &DAG) const {
44490b57cec5SDimitry Andric   unsigned Opcode, CCValid;
44500b57cec5SDimitry Andric   if (isIntrinsicWithCCAndChain(Op, Opcode, CCValid)) {
44510b57cec5SDimitry Andric     assert(Op->getNumValues() == 2 && "Expected only CC result and chain");
44520b57cec5SDimitry Andric     SDNode *Node = emitIntrinsicWithCCAndChain(DAG, Op, Opcode);
44530b57cec5SDimitry Andric     SDValue CC = getCCResult(DAG, SDValue(Node, 0));
44540b57cec5SDimitry Andric     DAG.ReplaceAllUsesOfValueWith(SDValue(Op.getNode(), 0), CC);
44550b57cec5SDimitry Andric     return SDValue();
44560b57cec5SDimitry Andric   }
44570b57cec5SDimitry Andric 
44580b57cec5SDimitry Andric   return SDValue();
44590b57cec5SDimitry Andric }
44600b57cec5SDimitry Andric 
44610b57cec5SDimitry Andric SDValue
44620b57cec5SDimitry Andric SystemZTargetLowering::lowerINTRINSIC_WO_CHAIN(SDValue Op,
44630b57cec5SDimitry Andric                                                SelectionDAG &DAG) const {
44640b57cec5SDimitry Andric   unsigned Opcode, CCValid;
44650b57cec5SDimitry Andric   if (isIntrinsicWithCC(Op, Opcode, CCValid)) {
44660b57cec5SDimitry Andric     SDNode *Node = emitIntrinsicWithCC(DAG, Op, Opcode);
44670b57cec5SDimitry Andric     if (Op->getNumValues() == 1)
44680b57cec5SDimitry Andric       return getCCResult(DAG, SDValue(Node, 0));
44690b57cec5SDimitry Andric     assert(Op->getNumValues() == 2 && "Expected a CC and non-CC result");
44700b57cec5SDimitry Andric     return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op), Op->getVTList(),
44710b57cec5SDimitry Andric                        SDValue(Node, 0), getCCResult(DAG, SDValue(Node, 1)));
44720b57cec5SDimitry Andric   }
44730b57cec5SDimitry Andric 
44740b57cec5SDimitry Andric   unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
44750b57cec5SDimitry Andric   switch (Id) {
44760b57cec5SDimitry Andric   case Intrinsic::thread_pointer:
44770b57cec5SDimitry Andric     return lowerThreadPointer(SDLoc(Op), DAG);
44780b57cec5SDimitry Andric 
44790b57cec5SDimitry Andric   case Intrinsic::s390_vpdi:
44800b57cec5SDimitry Andric     return DAG.getNode(SystemZISD::PERMUTE_DWORDS, SDLoc(Op), Op.getValueType(),
44810b57cec5SDimitry Andric                        Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
44820b57cec5SDimitry Andric 
44830b57cec5SDimitry Andric   case Intrinsic::s390_vperm:
44840b57cec5SDimitry Andric     return DAG.getNode(SystemZISD::PERMUTE, SDLoc(Op), Op.getValueType(),
44850b57cec5SDimitry Andric                        Op.getOperand(1), Op.getOperand(2), Op.getOperand(3));
44860b57cec5SDimitry Andric 
44870b57cec5SDimitry Andric   case Intrinsic::s390_vuphb:
44880b57cec5SDimitry Andric   case Intrinsic::s390_vuphh:
44890b57cec5SDimitry Andric   case Intrinsic::s390_vuphf:
44900b57cec5SDimitry Andric     return DAG.getNode(SystemZISD::UNPACK_HIGH, SDLoc(Op), Op.getValueType(),
44910b57cec5SDimitry Andric                        Op.getOperand(1));
44920b57cec5SDimitry Andric 
44930b57cec5SDimitry Andric   case Intrinsic::s390_vuplhb:
44940b57cec5SDimitry Andric   case Intrinsic::s390_vuplhh:
44950b57cec5SDimitry Andric   case Intrinsic::s390_vuplhf:
44960b57cec5SDimitry Andric     return DAG.getNode(SystemZISD::UNPACKL_HIGH, SDLoc(Op), Op.getValueType(),
44970b57cec5SDimitry Andric                        Op.getOperand(1));
44980b57cec5SDimitry Andric 
44990b57cec5SDimitry Andric   case Intrinsic::s390_vuplb:
45000b57cec5SDimitry Andric   case Intrinsic::s390_vuplhw:
45010b57cec5SDimitry Andric   case Intrinsic::s390_vuplf:
45020b57cec5SDimitry Andric     return DAG.getNode(SystemZISD::UNPACK_LOW, SDLoc(Op), Op.getValueType(),
45030b57cec5SDimitry Andric                        Op.getOperand(1));
45040b57cec5SDimitry Andric 
45050b57cec5SDimitry Andric   case Intrinsic::s390_vupllb:
45060b57cec5SDimitry Andric   case Intrinsic::s390_vupllh:
45070b57cec5SDimitry Andric   case Intrinsic::s390_vupllf:
45080b57cec5SDimitry Andric     return DAG.getNode(SystemZISD::UNPACKL_LOW, SDLoc(Op), Op.getValueType(),
45090b57cec5SDimitry Andric                        Op.getOperand(1));
45100b57cec5SDimitry Andric 
45110b57cec5SDimitry Andric   case Intrinsic::s390_vsumb:
45120b57cec5SDimitry Andric   case Intrinsic::s390_vsumh:
45130b57cec5SDimitry Andric   case Intrinsic::s390_vsumgh:
45140b57cec5SDimitry Andric   case Intrinsic::s390_vsumgf:
45150b57cec5SDimitry Andric   case Intrinsic::s390_vsumqf:
45160b57cec5SDimitry Andric   case Intrinsic::s390_vsumqg:
45170b57cec5SDimitry Andric     return DAG.getNode(SystemZISD::VSUM, SDLoc(Op), Op.getValueType(),
45180b57cec5SDimitry Andric                        Op.getOperand(1), Op.getOperand(2));
45190b57cec5SDimitry Andric   }
45200b57cec5SDimitry Andric 
45210b57cec5SDimitry Andric   return SDValue();
45220b57cec5SDimitry Andric }
45230b57cec5SDimitry Andric 
45240b57cec5SDimitry Andric namespace {
45250b57cec5SDimitry Andric // Says that SystemZISD operation Opcode can be used to perform the equivalent
45260b57cec5SDimitry Andric // of a VPERM with permute vector Bytes.  If Opcode takes three operands,
45270b57cec5SDimitry Andric // Operand is the constant third operand, otherwise it is the number of
45280b57cec5SDimitry Andric // bytes in each element of the result.
45290b57cec5SDimitry Andric struct Permute {
45300b57cec5SDimitry Andric   unsigned Opcode;
45310b57cec5SDimitry Andric   unsigned Operand;
45320b57cec5SDimitry Andric   unsigned char Bytes[SystemZ::VectorBytes];
45330b57cec5SDimitry Andric };
45340b57cec5SDimitry Andric }
45350b57cec5SDimitry Andric 
45360b57cec5SDimitry Andric static const Permute PermuteForms[] = {
45370b57cec5SDimitry Andric   // VMRHG
45380b57cec5SDimitry Andric   { SystemZISD::MERGE_HIGH, 8,
45390b57cec5SDimitry Andric     { 0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23 } },
45400b57cec5SDimitry Andric   // VMRHF
45410b57cec5SDimitry Andric   { SystemZISD::MERGE_HIGH, 4,
45420b57cec5SDimitry Andric     { 0, 1, 2, 3, 16, 17, 18, 19, 4, 5, 6, 7, 20, 21, 22, 23 } },
45430b57cec5SDimitry Andric   // VMRHH
45440b57cec5SDimitry Andric   { SystemZISD::MERGE_HIGH, 2,
45450b57cec5SDimitry Andric     { 0, 1, 16, 17, 2, 3, 18, 19, 4, 5, 20, 21, 6, 7, 22, 23 } },
45460b57cec5SDimitry Andric   // VMRHB
45470b57cec5SDimitry Andric   { SystemZISD::MERGE_HIGH, 1,
45480b57cec5SDimitry Andric     { 0, 16, 1, 17, 2, 18, 3, 19, 4, 20, 5, 21, 6, 22, 7, 23 } },
45490b57cec5SDimitry Andric   // VMRLG
45500b57cec5SDimitry Andric   { SystemZISD::MERGE_LOW, 8,
45510b57cec5SDimitry Andric     { 8, 9, 10, 11, 12, 13, 14, 15, 24, 25, 26, 27, 28, 29, 30, 31 } },
45520b57cec5SDimitry Andric   // VMRLF
45530b57cec5SDimitry Andric   { SystemZISD::MERGE_LOW, 4,
45540b57cec5SDimitry Andric     { 8, 9, 10, 11, 24, 25, 26, 27, 12, 13, 14, 15, 28, 29, 30, 31 } },
45550b57cec5SDimitry Andric   // VMRLH
45560b57cec5SDimitry Andric   { SystemZISD::MERGE_LOW, 2,
45570b57cec5SDimitry Andric     { 8, 9, 24, 25, 10, 11, 26, 27, 12, 13, 28, 29, 14, 15, 30, 31 } },
45580b57cec5SDimitry Andric   // VMRLB
45590b57cec5SDimitry Andric   { SystemZISD::MERGE_LOW, 1,
45600b57cec5SDimitry Andric     { 8, 24, 9, 25, 10, 26, 11, 27, 12, 28, 13, 29, 14, 30, 15, 31 } },
45610b57cec5SDimitry Andric   // VPKG
45620b57cec5SDimitry Andric   { SystemZISD::PACK, 4,
45630b57cec5SDimitry Andric     { 4, 5, 6, 7, 12, 13, 14, 15, 20, 21, 22, 23, 28, 29, 30, 31 } },
45640b57cec5SDimitry Andric   // VPKF
45650b57cec5SDimitry Andric   { SystemZISD::PACK, 2,
45660b57cec5SDimitry Andric     { 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31 } },
45670b57cec5SDimitry Andric   // VPKH
45680b57cec5SDimitry Andric   { SystemZISD::PACK, 1,
45690b57cec5SDimitry Andric     { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31 } },
45700b57cec5SDimitry Andric   // VPDI V1, V2, 4  (low half of V1, high half of V2)
45710b57cec5SDimitry Andric   { SystemZISD::PERMUTE_DWORDS, 4,
45720b57cec5SDimitry Andric     { 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 } },
45730b57cec5SDimitry Andric   // VPDI V1, V2, 1  (high half of V1, low half of V2)
45740b57cec5SDimitry Andric   { SystemZISD::PERMUTE_DWORDS, 1,
45750b57cec5SDimitry Andric     { 0, 1, 2, 3, 4, 5, 6, 7, 24, 25, 26, 27, 28, 29, 30, 31 } }
45760b57cec5SDimitry Andric };
45770b57cec5SDimitry Andric 
45780b57cec5SDimitry Andric // Called after matching a vector shuffle against a particular pattern.
45790b57cec5SDimitry Andric // Both the original shuffle and the pattern have two vector operands.
45800b57cec5SDimitry Andric // OpNos[0] is the operand of the original shuffle that should be used for
45810b57cec5SDimitry Andric // operand 0 of the pattern, or -1 if operand 0 of the pattern can be anything.
45820b57cec5SDimitry Andric // OpNos[1] is the same for operand 1 of the pattern.  Resolve these -1s and
45830b57cec5SDimitry Andric // set OpNo0 and OpNo1 to the shuffle operands that should actually be used
45840b57cec5SDimitry Andric // for operands 0 and 1 of the pattern.
45850b57cec5SDimitry Andric static bool chooseShuffleOpNos(int *OpNos, unsigned &OpNo0, unsigned &OpNo1) {
45860b57cec5SDimitry Andric   if (OpNos[0] < 0) {
45870b57cec5SDimitry Andric     if (OpNos[1] < 0)
45880b57cec5SDimitry Andric       return false;
45890b57cec5SDimitry Andric     OpNo0 = OpNo1 = OpNos[1];
45900b57cec5SDimitry Andric   } else if (OpNos[1] < 0) {
45910b57cec5SDimitry Andric     OpNo0 = OpNo1 = OpNos[0];
45920b57cec5SDimitry Andric   } else {
45930b57cec5SDimitry Andric     OpNo0 = OpNos[0];
45940b57cec5SDimitry Andric     OpNo1 = OpNos[1];
45950b57cec5SDimitry Andric   }
45960b57cec5SDimitry Andric   return true;
45970b57cec5SDimitry Andric }
45980b57cec5SDimitry Andric 
45990b57cec5SDimitry Andric // Bytes is a VPERM-like permute vector, except that -1 is used for
46000b57cec5SDimitry Andric // undefined bytes.  Return true if the VPERM can be implemented using P.
46010b57cec5SDimitry Andric // When returning true set OpNo0 to the VPERM operand that should be
46020b57cec5SDimitry Andric // used for operand 0 of P and likewise OpNo1 for operand 1 of P.
46030b57cec5SDimitry Andric //
46040b57cec5SDimitry Andric // For example, if swapping the VPERM operands allows P to match, OpNo0
46050b57cec5SDimitry Andric // will be 1 and OpNo1 will be 0.  If instead Bytes only refers to one
46060b57cec5SDimitry Andric // operand, but rewriting it to use two duplicated operands allows it to
46070b57cec5SDimitry Andric // match P, then OpNo0 and OpNo1 will be the same.
46080b57cec5SDimitry Andric static bool matchPermute(const SmallVectorImpl<int> &Bytes, const Permute &P,
46090b57cec5SDimitry Andric                          unsigned &OpNo0, unsigned &OpNo1) {
46100b57cec5SDimitry Andric   int OpNos[] = { -1, -1 };
46110b57cec5SDimitry Andric   for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) {
46120b57cec5SDimitry Andric     int Elt = Bytes[I];
46130b57cec5SDimitry Andric     if (Elt >= 0) {
46140b57cec5SDimitry Andric       // Make sure that the two permute vectors use the same suboperand
46150b57cec5SDimitry Andric       // byte number.  Only the operand numbers (the high bits) are
46160b57cec5SDimitry Andric       // allowed to differ.
46170b57cec5SDimitry Andric       if ((Elt ^ P.Bytes[I]) & (SystemZ::VectorBytes - 1))
46180b57cec5SDimitry Andric         return false;
46190b57cec5SDimitry Andric       int ModelOpNo = P.Bytes[I] / SystemZ::VectorBytes;
46200b57cec5SDimitry Andric       int RealOpNo = unsigned(Elt) / SystemZ::VectorBytes;
46210b57cec5SDimitry Andric       // Make sure that the operand mappings are consistent with previous
46220b57cec5SDimitry Andric       // elements.
46230b57cec5SDimitry Andric       if (OpNos[ModelOpNo] == 1 - RealOpNo)
46240b57cec5SDimitry Andric         return false;
46250b57cec5SDimitry Andric       OpNos[ModelOpNo] = RealOpNo;
46260b57cec5SDimitry Andric     }
46270b57cec5SDimitry Andric   }
46280b57cec5SDimitry Andric   return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
46290b57cec5SDimitry Andric }
46300b57cec5SDimitry Andric 
46310b57cec5SDimitry Andric // As above, but search for a matching permute.
46320b57cec5SDimitry Andric static const Permute *matchPermute(const SmallVectorImpl<int> &Bytes,
46330b57cec5SDimitry Andric                                    unsigned &OpNo0, unsigned &OpNo1) {
46340b57cec5SDimitry Andric   for (auto &P : PermuteForms)
46350b57cec5SDimitry Andric     if (matchPermute(Bytes, P, OpNo0, OpNo1))
46360b57cec5SDimitry Andric       return &P;
46370b57cec5SDimitry Andric   return nullptr;
46380b57cec5SDimitry Andric }
46390b57cec5SDimitry Andric 
46400b57cec5SDimitry Andric // Bytes is a VPERM-like permute vector, except that -1 is used for
46410b57cec5SDimitry Andric // undefined bytes.  This permute is an operand of an outer permute.
46420b57cec5SDimitry Andric // See whether redistributing the -1 bytes gives a shuffle that can be
46430b57cec5SDimitry Andric // implemented using P.  If so, set Transform to a VPERM-like permute vector
46440b57cec5SDimitry Andric // that, when applied to the result of P, gives the original permute in Bytes.
46450b57cec5SDimitry Andric static bool matchDoublePermute(const SmallVectorImpl<int> &Bytes,
46460b57cec5SDimitry Andric                                const Permute &P,
46470b57cec5SDimitry Andric                                SmallVectorImpl<int> &Transform) {
46480b57cec5SDimitry Andric   unsigned To = 0;
46490b57cec5SDimitry Andric   for (unsigned From = 0; From < SystemZ::VectorBytes; ++From) {
46500b57cec5SDimitry Andric     int Elt = Bytes[From];
46510b57cec5SDimitry Andric     if (Elt < 0)
46520b57cec5SDimitry Andric       // Byte number From of the result is undefined.
46530b57cec5SDimitry Andric       Transform[From] = -1;
46540b57cec5SDimitry Andric     else {
46550b57cec5SDimitry Andric       while (P.Bytes[To] != Elt) {
46560b57cec5SDimitry Andric         To += 1;
46570b57cec5SDimitry Andric         if (To == SystemZ::VectorBytes)
46580b57cec5SDimitry Andric           return false;
46590b57cec5SDimitry Andric       }
46600b57cec5SDimitry Andric       Transform[From] = To;
46610b57cec5SDimitry Andric     }
46620b57cec5SDimitry Andric   }
46630b57cec5SDimitry Andric   return true;
46640b57cec5SDimitry Andric }
46650b57cec5SDimitry Andric 
46660b57cec5SDimitry Andric // As above, but search for a matching permute.
46670b57cec5SDimitry Andric static const Permute *matchDoublePermute(const SmallVectorImpl<int> &Bytes,
46680b57cec5SDimitry Andric                                          SmallVectorImpl<int> &Transform) {
46690b57cec5SDimitry Andric   for (auto &P : PermuteForms)
46700b57cec5SDimitry Andric     if (matchDoublePermute(Bytes, P, Transform))
46710b57cec5SDimitry Andric       return &P;
46720b57cec5SDimitry Andric   return nullptr;
46730b57cec5SDimitry Andric }
46740b57cec5SDimitry Andric 
46750b57cec5SDimitry Andric // Convert the mask of the given shuffle op into a byte-level mask,
46760b57cec5SDimitry Andric // as if it had type vNi8.
46770b57cec5SDimitry Andric static bool getVPermMask(SDValue ShuffleOp,
46780b57cec5SDimitry Andric                          SmallVectorImpl<int> &Bytes) {
46790b57cec5SDimitry Andric   EVT VT = ShuffleOp.getValueType();
46800b57cec5SDimitry Andric   unsigned NumElements = VT.getVectorNumElements();
46810b57cec5SDimitry Andric   unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
46820b57cec5SDimitry Andric 
46830b57cec5SDimitry Andric   if (auto *VSN = dyn_cast<ShuffleVectorSDNode>(ShuffleOp)) {
46840b57cec5SDimitry Andric     Bytes.resize(NumElements * BytesPerElement, -1);
46850b57cec5SDimitry Andric     for (unsigned I = 0; I < NumElements; ++I) {
46860b57cec5SDimitry Andric       int Index = VSN->getMaskElt(I);
46870b57cec5SDimitry Andric       if (Index >= 0)
46880b57cec5SDimitry Andric         for (unsigned J = 0; J < BytesPerElement; ++J)
46890b57cec5SDimitry Andric           Bytes[I * BytesPerElement + J] = Index * BytesPerElement + J;
46900b57cec5SDimitry Andric     }
46910b57cec5SDimitry Andric     return true;
46920b57cec5SDimitry Andric   }
46930b57cec5SDimitry Andric   if (SystemZISD::SPLAT == ShuffleOp.getOpcode() &&
46940b57cec5SDimitry Andric       isa<ConstantSDNode>(ShuffleOp.getOperand(1))) {
46950b57cec5SDimitry Andric     unsigned Index = ShuffleOp.getConstantOperandVal(1);
46960b57cec5SDimitry Andric     Bytes.resize(NumElements * BytesPerElement, -1);
46970b57cec5SDimitry Andric     for (unsigned I = 0; I < NumElements; ++I)
46980b57cec5SDimitry Andric       for (unsigned J = 0; J < BytesPerElement; ++J)
46990b57cec5SDimitry Andric         Bytes[I * BytesPerElement + J] = Index * BytesPerElement + J;
47000b57cec5SDimitry Andric     return true;
47010b57cec5SDimitry Andric   }
47020b57cec5SDimitry Andric   return false;
47030b57cec5SDimitry Andric }
47040b57cec5SDimitry Andric 
47050b57cec5SDimitry Andric // Bytes is a VPERM-like permute vector, except that -1 is used for
47060b57cec5SDimitry Andric // undefined bytes.  See whether bytes [Start, Start + BytesPerElement) of
47070b57cec5SDimitry Andric // the result come from a contiguous sequence of bytes from one input.
47080b57cec5SDimitry Andric // Set Base to the selector for the first byte if so.
47090b57cec5SDimitry Andric static bool getShuffleInput(const SmallVectorImpl<int> &Bytes, unsigned Start,
47100b57cec5SDimitry Andric                             unsigned BytesPerElement, int &Base) {
47110b57cec5SDimitry Andric   Base = -1;
47120b57cec5SDimitry Andric   for (unsigned I = 0; I < BytesPerElement; ++I) {
47130b57cec5SDimitry Andric     if (Bytes[Start + I] >= 0) {
47140b57cec5SDimitry Andric       unsigned Elem = Bytes[Start + I];
47150b57cec5SDimitry Andric       if (Base < 0) {
47160b57cec5SDimitry Andric         Base = Elem - I;
47170b57cec5SDimitry Andric         // Make sure the bytes would come from one input operand.
47180b57cec5SDimitry Andric         if (unsigned(Base) % Bytes.size() + BytesPerElement > Bytes.size())
47190b57cec5SDimitry Andric           return false;
47200b57cec5SDimitry Andric       } else if (unsigned(Base) != Elem - I)
47210b57cec5SDimitry Andric         return false;
47220b57cec5SDimitry Andric     }
47230b57cec5SDimitry Andric   }
47240b57cec5SDimitry Andric   return true;
47250b57cec5SDimitry Andric }
47260b57cec5SDimitry Andric 
47270b57cec5SDimitry Andric // Bytes is a VPERM-like permute vector, except that -1 is used for
47285ffd83dbSDimitry Andric // undefined bytes.  Return true if it can be performed using VSLDB.
47290b57cec5SDimitry Andric // When returning true, set StartIndex to the shift amount and OpNo0
47300b57cec5SDimitry Andric // and OpNo1 to the VPERM operands that should be used as the first
47310b57cec5SDimitry Andric // and second shift operand respectively.
47320b57cec5SDimitry Andric static bool isShlDoublePermute(const SmallVectorImpl<int> &Bytes,
47330b57cec5SDimitry Andric                                unsigned &StartIndex, unsigned &OpNo0,
47340b57cec5SDimitry Andric                                unsigned &OpNo1) {
47350b57cec5SDimitry Andric   int OpNos[] = { -1, -1 };
47360b57cec5SDimitry Andric   int Shift = -1;
47370b57cec5SDimitry Andric   for (unsigned I = 0; I < 16; ++I) {
47380b57cec5SDimitry Andric     int Index = Bytes[I];
47390b57cec5SDimitry Andric     if (Index >= 0) {
47400b57cec5SDimitry Andric       int ExpectedShift = (Index - I) % SystemZ::VectorBytes;
47410b57cec5SDimitry Andric       int ModelOpNo = unsigned(ExpectedShift + I) / SystemZ::VectorBytes;
47420b57cec5SDimitry Andric       int RealOpNo = unsigned(Index) / SystemZ::VectorBytes;
47430b57cec5SDimitry Andric       if (Shift < 0)
47440b57cec5SDimitry Andric         Shift = ExpectedShift;
47450b57cec5SDimitry Andric       else if (Shift != ExpectedShift)
47460b57cec5SDimitry Andric         return false;
47470b57cec5SDimitry Andric       // Make sure that the operand mappings are consistent with previous
47480b57cec5SDimitry Andric       // elements.
47490b57cec5SDimitry Andric       if (OpNos[ModelOpNo] == 1 - RealOpNo)
47500b57cec5SDimitry Andric         return false;
47510b57cec5SDimitry Andric       OpNos[ModelOpNo] = RealOpNo;
47520b57cec5SDimitry Andric     }
47530b57cec5SDimitry Andric   }
47540b57cec5SDimitry Andric   StartIndex = Shift;
47550b57cec5SDimitry Andric   return chooseShuffleOpNos(OpNos, OpNo0, OpNo1);
47560b57cec5SDimitry Andric }
47570b57cec5SDimitry Andric 
47580b57cec5SDimitry Andric // Create a node that performs P on operands Op0 and Op1, casting the
47590b57cec5SDimitry Andric // operands to the appropriate type.  The type of the result is determined by P.
47600b57cec5SDimitry Andric static SDValue getPermuteNode(SelectionDAG &DAG, const SDLoc &DL,
47610b57cec5SDimitry Andric                               const Permute &P, SDValue Op0, SDValue Op1) {
47620b57cec5SDimitry Andric   // VPDI (PERMUTE_DWORDS) always operates on v2i64s.  The input
47630b57cec5SDimitry Andric   // elements of a PACK are twice as wide as the outputs.
47640b57cec5SDimitry Andric   unsigned InBytes = (P.Opcode == SystemZISD::PERMUTE_DWORDS ? 8 :
47650b57cec5SDimitry Andric                       P.Opcode == SystemZISD::PACK ? P.Operand * 2 :
47660b57cec5SDimitry Andric                       P.Operand);
47670b57cec5SDimitry Andric   // Cast both operands to the appropriate type.
47680b57cec5SDimitry Andric   MVT InVT = MVT::getVectorVT(MVT::getIntegerVT(InBytes * 8),
47690b57cec5SDimitry Andric                               SystemZ::VectorBytes / InBytes);
47700b57cec5SDimitry Andric   Op0 = DAG.getNode(ISD::BITCAST, DL, InVT, Op0);
47710b57cec5SDimitry Andric   Op1 = DAG.getNode(ISD::BITCAST, DL, InVT, Op1);
47720b57cec5SDimitry Andric   SDValue Op;
47730b57cec5SDimitry Andric   if (P.Opcode == SystemZISD::PERMUTE_DWORDS) {
47748bcb0991SDimitry Andric     SDValue Op2 = DAG.getTargetConstant(P.Operand, DL, MVT::i32);
47750b57cec5SDimitry Andric     Op = DAG.getNode(SystemZISD::PERMUTE_DWORDS, DL, InVT, Op0, Op1, Op2);
47760b57cec5SDimitry Andric   } else if (P.Opcode == SystemZISD::PACK) {
47770b57cec5SDimitry Andric     MVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(P.Operand * 8),
47780b57cec5SDimitry Andric                                  SystemZ::VectorBytes / P.Operand);
47790b57cec5SDimitry Andric     Op = DAG.getNode(SystemZISD::PACK, DL, OutVT, Op0, Op1);
47800b57cec5SDimitry Andric   } else {
47810b57cec5SDimitry Andric     Op = DAG.getNode(P.Opcode, DL, InVT, Op0, Op1);
47820b57cec5SDimitry Andric   }
47830b57cec5SDimitry Andric   return Op;
47840b57cec5SDimitry Andric }
47850b57cec5SDimitry Andric 
47865ffd83dbSDimitry Andric static bool isZeroVector(SDValue N) {
47875ffd83dbSDimitry Andric   if (N->getOpcode() == ISD::BITCAST)
47885ffd83dbSDimitry Andric     N = N->getOperand(0);
47895ffd83dbSDimitry Andric   if (N->getOpcode() == ISD::SPLAT_VECTOR)
47905ffd83dbSDimitry Andric     if (auto *Op = dyn_cast<ConstantSDNode>(N->getOperand(0)))
47915ffd83dbSDimitry Andric       return Op->getZExtValue() == 0;
47925ffd83dbSDimitry Andric   return ISD::isBuildVectorAllZeros(N.getNode());
47935ffd83dbSDimitry Andric }
47945ffd83dbSDimitry Andric 
47955ffd83dbSDimitry Andric // Return the index of the zero/undef vector, or UINT32_MAX if not found.
47965ffd83dbSDimitry Andric static uint32_t findZeroVectorIdx(SDValue *Ops, unsigned Num) {
47975ffd83dbSDimitry Andric   for (unsigned I = 0; I < Num ; I++)
47985ffd83dbSDimitry Andric     if (isZeroVector(Ops[I]))
47995ffd83dbSDimitry Andric       return I;
48005ffd83dbSDimitry Andric   return UINT32_MAX;
48015ffd83dbSDimitry Andric }
48025ffd83dbSDimitry Andric 
48030b57cec5SDimitry Andric // Bytes is a VPERM-like permute vector, except that -1 is used for
48040b57cec5SDimitry Andric // undefined bytes.  Implement it on operands Ops[0] and Ops[1] using
48055ffd83dbSDimitry Andric // VSLDB or VPERM.
48060b57cec5SDimitry Andric static SDValue getGeneralPermuteNode(SelectionDAG &DAG, const SDLoc &DL,
48070b57cec5SDimitry Andric                                      SDValue *Ops,
48080b57cec5SDimitry Andric                                      const SmallVectorImpl<int> &Bytes) {
48090b57cec5SDimitry Andric   for (unsigned I = 0; I < 2; ++I)
48100b57cec5SDimitry Andric     Ops[I] = DAG.getNode(ISD::BITCAST, DL, MVT::v16i8, Ops[I]);
48110b57cec5SDimitry Andric 
48125ffd83dbSDimitry Andric   // First see whether VSLDB can be used.
48130b57cec5SDimitry Andric   unsigned StartIndex, OpNo0, OpNo1;
48140b57cec5SDimitry Andric   if (isShlDoublePermute(Bytes, StartIndex, OpNo0, OpNo1))
48150b57cec5SDimitry Andric     return DAG.getNode(SystemZISD::SHL_DOUBLE, DL, MVT::v16i8, Ops[OpNo0],
48168bcb0991SDimitry Andric                        Ops[OpNo1],
48178bcb0991SDimitry Andric                        DAG.getTargetConstant(StartIndex, DL, MVT::i32));
48180b57cec5SDimitry Andric 
48195ffd83dbSDimitry Andric   // Fall back on VPERM.  Construct an SDNode for the permute vector.  Try to
48205ffd83dbSDimitry Andric   // eliminate a zero vector by reusing any zero index in the permute vector.
48215ffd83dbSDimitry Andric   unsigned ZeroVecIdx = findZeroVectorIdx(&Ops[0], 2);
48225ffd83dbSDimitry Andric   if (ZeroVecIdx != UINT32_MAX) {
48235ffd83dbSDimitry Andric     bool MaskFirst = true;
48245ffd83dbSDimitry Andric     int ZeroIdx = -1;
48255ffd83dbSDimitry Andric     for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) {
48265ffd83dbSDimitry Andric       unsigned OpNo = unsigned(Bytes[I]) / SystemZ::VectorBytes;
48275ffd83dbSDimitry Andric       unsigned Byte = unsigned(Bytes[I]) % SystemZ::VectorBytes;
48285ffd83dbSDimitry Andric       if (OpNo == ZeroVecIdx && I == 0) {
48295ffd83dbSDimitry Andric         // If the first byte is zero, use mask as first operand.
48305ffd83dbSDimitry Andric         ZeroIdx = 0;
48315ffd83dbSDimitry Andric         break;
48325ffd83dbSDimitry Andric       }
48335ffd83dbSDimitry Andric       if (OpNo != ZeroVecIdx && Byte == 0) {
48345ffd83dbSDimitry Andric         // If mask contains a zero, use it by placing that vector first.
48355ffd83dbSDimitry Andric         ZeroIdx = I + SystemZ::VectorBytes;
48365ffd83dbSDimitry Andric         MaskFirst = false;
48375ffd83dbSDimitry Andric         break;
48385ffd83dbSDimitry Andric       }
48395ffd83dbSDimitry Andric     }
48405ffd83dbSDimitry Andric     if (ZeroIdx != -1) {
48415ffd83dbSDimitry Andric       SDValue IndexNodes[SystemZ::VectorBytes];
48425ffd83dbSDimitry Andric       for (unsigned I = 0; I < SystemZ::VectorBytes; ++I) {
48435ffd83dbSDimitry Andric         if (Bytes[I] >= 0) {
48445ffd83dbSDimitry Andric           unsigned OpNo = unsigned(Bytes[I]) / SystemZ::VectorBytes;
48455ffd83dbSDimitry Andric           unsigned Byte = unsigned(Bytes[I]) % SystemZ::VectorBytes;
48465ffd83dbSDimitry Andric           if (OpNo == ZeroVecIdx)
48475ffd83dbSDimitry Andric             IndexNodes[I] = DAG.getConstant(ZeroIdx, DL, MVT::i32);
48485ffd83dbSDimitry Andric           else {
48495ffd83dbSDimitry Andric             unsigned BIdx = MaskFirst ? Byte + SystemZ::VectorBytes : Byte;
48505ffd83dbSDimitry Andric             IndexNodes[I] = DAG.getConstant(BIdx, DL, MVT::i32);
48515ffd83dbSDimitry Andric           }
48525ffd83dbSDimitry Andric         } else
48535ffd83dbSDimitry Andric           IndexNodes[I] = DAG.getUNDEF(MVT::i32);
48545ffd83dbSDimitry Andric       }
48555ffd83dbSDimitry Andric       SDValue Mask = DAG.getBuildVector(MVT::v16i8, DL, IndexNodes);
48565ffd83dbSDimitry Andric       SDValue Src = ZeroVecIdx == 0 ? Ops[1] : Ops[0];
48575ffd83dbSDimitry Andric       if (MaskFirst)
48585ffd83dbSDimitry Andric         return DAG.getNode(SystemZISD::PERMUTE, DL, MVT::v16i8, Mask, Src,
48595ffd83dbSDimitry Andric                            Mask);
48605ffd83dbSDimitry Andric       else
48615ffd83dbSDimitry Andric         return DAG.getNode(SystemZISD::PERMUTE, DL, MVT::v16i8, Src, Mask,
48625ffd83dbSDimitry Andric                            Mask);
48635ffd83dbSDimitry Andric     }
48645ffd83dbSDimitry Andric   }
48655ffd83dbSDimitry Andric 
48660b57cec5SDimitry Andric   SDValue IndexNodes[SystemZ::VectorBytes];
48670b57cec5SDimitry Andric   for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
48680b57cec5SDimitry Andric     if (Bytes[I] >= 0)
48690b57cec5SDimitry Andric       IndexNodes[I] = DAG.getConstant(Bytes[I], DL, MVT::i32);
48700b57cec5SDimitry Andric     else
48710b57cec5SDimitry Andric       IndexNodes[I] = DAG.getUNDEF(MVT::i32);
48720b57cec5SDimitry Andric   SDValue Op2 = DAG.getBuildVector(MVT::v16i8, DL, IndexNodes);
48735ffd83dbSDimitry Andric   return DAG.getNode(SystemZISD::PERMUTE, DL, MVT::v16i8, Ops[0],
48745ffd83dbSDimitry Andric                      (!Ops[1].isUndef() ? Ops[1] : Ops[0]), Op2);
48750b57cec5SDimitry Andric }
48760b57cec5SDimitry Andric 
48770b57cec5SDimitry Andric namespace {
48780b57cec5SDimitry Andric // Describes a general N-operand vector shuffle.
48790b57cec5SDimitry Andric struct GeneralShuffle {
48805ffd83dbSDimitry Andric   GeneralShuffle(EVT vt) : VT(vt), UnpackFromEltSize(UINT_MAX) {}
48810b57cec5SDimitry Andric   void addUndef();
48820b57cec5SDimitry Andric   bool add(SDValue, unsigned);
48830b57cec5SDimitry Andric   SDValue getNode(SelectionDAG &, const SDLoc &);
48845ffd83dbSDimitry Andric   void tryPrepareForUnpack();
48855ffd83dbSDimitry Andric   bool unpackWasPrepared() { return UnpackFromEltSize <= 4; }
48865ffd83dbSDimitry Andric   SDValue insertUnpackIfPrepared(SelectionDAG &DAG, const SDLoc &DL, SDValue Op);
48870b57cec5SDimitry Andric 
48880b57cec5SDimitry Andric   // The operands of the shuffle.
48890b57cec5SDimitry Andric   SmallVector<SDValue, SystemZ::VectorBytes> Ops;
48900b57cec5SDimitry Andric 
48910b57cec5SDimitry Andric   // Index I is -1 if byte I of the result is undefined.  Otherwise the
48920b57cec5SDimitry Andric   // result comes from byte Bytes[I] % SystemZ::VectorBytes of operand
48930b57cec5SDimitry Andric   // Bytes[I] / SystemZ::VectorBytes.
48940b57cec5SDimitry Andric   SmallVector<int, SystemZ::VectorBytes> Bytes;
48950b57cec5SDimitry Andric 
48960b57cec5SDimitry Andric   // The type of the shuffle result.
48970b57cec5SDimitry Andric   EVT VT;
48985ffd83dbSDimitry Andric 
48995ffd83dbSDimitry Andric   // Holds a value of 1, 2 or 4 if a final unpack has been prepared for.
49005ffd83dbSDimitry Andric   unsigned UnpackFromEltSize;
49010b57cec5SDimitry Andric };
49020b57cec5SDimitry Andric }
49030b57cec5SDimitry Andric 
49040b57cec5SDimitry Andric // Add an extra undefined element to the shuffle.
49050b57cec5SDimitry Andric void GeneralShuffle::addUndef() {
49060b57cec5SDimitry Andric   unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
49070b57cec5SDimitry Andric   for (unsigned I = 0; I < BytesPerElement; ++I)
49080b57cec5SDimitry Andric     Bytes.push_back(-1);
49090b57cec5SDimitry Andric }
49100b57cec5SDimitry Andric 
49110b57cec5SDimitry Andric // Add an extra element to the shuffle, taking it from element Elem of Op.
49120b57cec5SDimitry Andric // A null Op indicates a vector input whose value will be calculated later;
49130b57cec5SDimitry Andric // there is at most one such input per shuffle and it always has the same
49140b57cec5SDimitry Andric // type as the result. Aborts and returns false if the source vector elements
49150b57cec5SDimitry Andric // of an EXTRACT_VECTOR_ELT are smaller than the destination elements. Per
49160b57cec5SDimitry Andric // LLVM they become implicitly extended, but this is rare and not optimized.
49170b57cec5SDimitry Andric bool GeneralShuffle::add(SDValue Op, unsigned Elem) {
49180b57cec5SDimitry Andric   unsigned BytesPerElement = VT.getVectorElementType().getStoreSize();
49190b57cec5SDimitry Andric 
49200b57cec5SDimitry Andric   // The source vector can have wider elements than the result,
49210b57cec5SDimitry Andric   // either through an explicit TRUNCATE or because of type legalization.
49220b57cec5SDimitry Andric   // We want the least significant part.
49230b57cec5SDimitry Andric   EVT FromVT = Op.getNode() ? Op.getValueType() : VT;
49240b57cec5SDimitry Andric   unsigned FromBytesPerElement = FromVT.getVectorElementType().getStoreSize();
49250b57cec5SDimitry Andric 
49260b57cec5SDimitry Andric   // Return false if the source elements are smaller than their destination
49270b57cec5SDimitry Andric   // elements.
49280b57cec5SDimitry Andric   if (FromBytesPerElement < BytesPerElement)
49290b57cec5SDimitry Andric     return false;
49300b57cec5SDimitry Andric 
49310b57cec5SDimitry Andric   unsigned Byte = ((Elem * FromBytesPerElement) % SystemZ::VectorBytes +
49320b57cec5SDimitry Andric                    (FromBytesPerElement - BytesPerElement));
49330b57cec5SDimitry Andric 
49340b57cec5SDimitry Andric   // Look through things like shuffles and bitcasts.
49350b57cec5SDimitry Andric   while (Op.getNode()) {
49360b57cec5SDimitry Andric     if (Op.getOpcode() == ISD::BITCAST)
49370b57cec5SDimitry Andric       Op = Op.getOperand(0);
49380b57cec5SDimitry Andric     else if (Op.getOpcode() == ISD::VECTOR_SHUFFLE && Op.hasOneUse()) {
49390b57cec5SDimitry Andric       // See whether the bytes we need come from a contiguous part of one
49400b57cec5SDimitry Andric       // operand.
49410b57cec5SDimitry Andric       SmallVector<int, SystemZ::VectorBytes> OpBytes;
49420b57cec5SDimitry Andric       if (!getVPermMask(Op, OpBytes))
49430b57cec5SDimitry Andric         break;
49440b57cec5SDimitry Andric       int NewByte;
49450b57cec5SDimitry Andric       if (!getShuffleInput(OpBytes, Byte, BytesPerElement, NewByte))
49460b57cec5SDimitry Andric         break;
49470b57cec5SDimitry Andric       if (NewByte < 0) {
49480b57cec5SDimitry Andric         addUndef();
49490b57cec5SDimitry Andric         return true;
49500b57cec5SDimitry Andric       }
49510b57cec5SDimitry Andric       Op = Op.getOperand(unsigned(NewByte) / SystemZ::VectorBytes);
49520b57cec5SDimitry Andric       Byte = unsigned(NewByte) % SystemZ::VectorBytes;
49530b57cec5SDimitry Andric     } else if (Op.isUndef()) {
49540b57cec5SDimitry Andric       addUndef();
49550b57cec5SDimitry Andric       return true;
49560b57cec5SDimitry Andric     } else
49570b57cec5SDimitry Andric       break;
49580b57cec5SDimitry Andric   }
49590b57cec5SDimitry Andric 
49600b57cec5SDimitry Andric   // Make sure that the source of the extraction is in Ops.
49610b57cec5SDimitry Andric   unsigned OpNo = 0;
49620b57cec5SDimitry Andric   for (; OpNo < Ops.size(); ++OpNo)
49630b57cec5SDimitry Andric     if (Ops[OpNo] == Op)
49640b57cec5SDimitry Andric       break;
49650b57cec5SDimitry Andric   if (OpNo == Ops.size())
49660b57cec5SDimitry Andric     Ops.push_back(Op);
49670b57cec5SDimitry Andric 
49680b57cec5SDimitry Andric   // Add the element to Bytes.
49690b57cec5SDimitry Andric   unsigned Base = OpNo * SystemZ::VectorBytes + Byte;
49700b57cec5SDimitry Andric   for (unsigned I = 0; I < BytesPerElement; ++I)
49710b57cec5SDimitry Andric     Bytes.push_back(Base + I);
49720b57cec5SDimitry Andric 
49730b57cec5SDimitry Andric   return true;
49740b57cec5SDimitry Andric }
49750b57cec5SDimitry Andric 
49760b57cec5SDimitry Andric // Return SDNodes for the completed shuffle.
49770b57cec5SDimitry Andric SDValue GeneralShuffle::getNode(SelectionDAG &DAG, const SDLoc &DL) {
49780b57cec5SDimitry Andric   assert(Bytes.size() == SystemZ::VectorBytes && "Incomplete vector");
49790b57cec5SDimitry Andric 
49800b57cec5SDimitry Andric   if (Ops.size() == 0)
49810b57cec5SDimitry Andric     return DAG.getUNDEF(VT);
49820b57cec5SDimitry Andric 
49835ffd83dbSDimitry Andric   // Use a single unpack if possible as the last operation.
49845ffd83dbSDimitry Andric   tryPrepareForUnpack();
49855ffd83dbSDimitry Andric 
49860b57cec5SDimitry Andric   // Make sure that there are at least two shuffle operands.
49870b57cec5SDimitry Andric   if (Ops.size() == 1)
49880b57cec5SDimitry Andric     Ops.push_back(DAG.getUNDEF(MVT::v16i8));
49890b57cec5SDimitry Andric 
49900b57cec5SDimitry Andric   // Create a tree of shuffles, deferring root node until after the loop.
49910b57cec5SDimitry Andric   // Try to redistribute the undefined elements of non-root nodes so that
49920b57cec5SDimitry Andric   // the non-root shuffles match something like a pack or merge, then adjust
49930b57cec5SDimitry Andric   // the parent node's permute vector to compensate for the new order.
49940b57cec5SDimitry Andric   // Among other things, this copes with vectors like <2 x i16> that were
49950b57cec5SDimitry Andric   // padded with undefined elements during type legalization.
49960b57cec5SDimitry Andric   //
49970b57cec5SDimitry Andric   // In the best case this redistribution will lead to the whole tree
49980b57cec5SDimitry Andric   // using packs and merges.  It should rarely be a loss in other cases.
49990b57cec5SDimitry Andric   unsigned Stride = 1;
50000b57cec5SDimitry Andric   for (; Stride * 2 < Ops.size(); Stride *= 2) {
50010b57cec5SDimitry Andric     for (unsigned I = 0; I < Ops.size() - Stride; I += Stride * 2) {
50020b57cec5SDimitry Andric       SDValue SubOps[] = { Ops[I], Ops[I + Stride] };
50030b57cec5SDimitry Andric 
50040b57cec5SDimitry Andric       // Create a mask for just these two operands.
50050b57cec5SDimitry Andric       SmallVector<int, SystemZ::VectorBytes> NewBytes(SystemZ::VectorBytes);
50060b57cec5SDimitry Andric       for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
50070b57cec5SDimitry Andric         unsigned OpNo = unsigned(Bytes[J]) / SystemZ::VectorBytes;
50080b57cec5SDimitry Andric         unsigned Byte = unsigned(Bytes[J]) % SystemZ::VectorBytes;
50090b57cec5SDimitry Andric         if (OpNo == I)
50100b57cec5SDimitry Andric           NewBytes[J] = Byte;
50110b57cec5SDimitry Andric         else if (OpNo == I + Stride)
50120b57cec5SDimitry Andric           NewBytes[J] = SystemZ::VectorBytes + Byte;
50130b57cec5SDimitry Andric         else
50140b57cec5SDimitry Andric           NewBytes[J] = -1;
50150b57cec5SDimitry Andric       }
50160b57cec5SDimitry Andric       // See if it would be better to reorganize NewMask to avoid using VPERM.
50170b57cec5SDimitry Andric       SmallVector<int, SystemZ::VectorBytes> NewBytesMap(SystemZ::VectorBytes);
50180b57cec5SDimitry Andric       if (const Permute *P = matchDoublePermute(NewBytes, NewBytesMap)) {
50190b57cec5SDimitry Andric         Ops[I] = getPermuteNode(DAG, DL, *P, SubOps[0], SubOps[1]);
50200b57cec5SDimitry Andric         // Applying NewBytesMap to Ops[I] gets back to NewBytes.
50210b57cec5SDimitry Andric         for (unsigned J = 0; J < SystemZ::VectorBytes; ++J) {
50220b57cec5SDimitry Andric           if (NewBytes[J] >= 0) {
50230b57cec5SDimitry Andric             assert(unsigned(NewBytesMap[J]) < SystemZ::VectorBytes &&
50240b57cec5SDimitry Andric                    "Invalid double permute");
50250b57cec5SDimitry Andric             Bytes[J] = I * SystemZ::VectorBytes + NewBytesMap[J];
50260b57cec5SDimitry Andric           } else
50270b57cec5SDimitry Andric             assert(NewBytesMap[J] < 0 && "Invalid double permute");
50280b57cec5SDimitry Andric         }
50290b57cec5SDimitry Andric       } else {
50300b57cec5SDimitry Andric         // Just use NewBytes on the operands.
50310b57cec5SDimitry Andric         Ops[I] = getGeneralPermuteNode(DAG, DL, SubOps, NewBytes);
50320b57cec5SDimitry Andric         for (unsigned J = 0; J < SystemZ::VectorBytes; ++J)
50330b57cec5SDimitry Andric           if (NewBytes[J] >= 0)
50340b57cec5SDimitry Andric             Bytes[J] = I * SystemZ::VectorBytes + J;
50350b57cec5SDimitry Andric       }
50360b57cec5SDimitry Andric     }
50370b57cec5SDimitry Andric   }
50380b57cec5SDimitry Andric 
50390b57cec5SDimitry Andric   // Now we just have 2 inputs.  Put the second operand in Ops[1].
50400b57cec5SDimitry Andric   if (Stride > 1) {
50410b57cec5SDimitry Andric     Ops[1] = Ops[Stride];
50420b57cec5SDimitry Andric     for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
50430b57cec5SDimitry Andric       if (Bytes[I] >= int(SystemZ::VectorBytes))
50440b57cec5SDimitry Andric         Bytes[I] -= (Stride - 1) * SystemZ::VectorBytes;
50450b57cec5SDimitry Andric   }
50460b57cec5SDimitry Andric 
50470b57cec5SDimitry Andric   // Look for an instruction that can do the permute without resorting
50480b57cec5SDimitry Andric   // to VPERM.
50490b57cec5SDimitry Andric   unsigned OpNo0, OpNo1;
50500b57cec5SDimitry Andric   SDValue Op;
50515ffd83dbSDimitry Andric   if (unpackWasPrepared() && Ops[1].isUndef())
50525ffd83dbSDimitry Andric     Op = Ops[0];
50535ffd83dbSDimitry Andric   else if (const Permute *P = matchPermute(Bytes, OpNo0, OpNo1))
50540b57cec5SDimitry Andric     Op = getPermuteNode(DAG, DL, *P, Ops[OpNo0], Ops[OpNo1]);
50550b57cec5SDimitry Andric   else
50560b57cec5SDimitry Andric     Op = getGeneralPermuteNode(DAG, DL, &Ops[0], Bytes);
50575ffd83dbSDimitry Andric 
50585ffd83dbSDimitry Andric   Op = insertUnpackIfPrepared(DAG, DL, Op);
50595ffd83dbSDimitry Andric 
50600b57cec5SDimitry Andric   return DAG.getNode(ISD::BITCAST, DL, VT, Op);
50610b57cec5SDimitry Andric }
50620b57cec5SDimitry Andric 
50635ffd83dbSDimitry Andric #ifndef NDEBUG
50645ffd83dbSDimitry Andric static void dumpBytes(const SmallVectorImpl<int> &Bytes, std::string Msg) {
50655ffd83dbSDimitry Andric   dbgs() << Msg.c_str() << " { ";
50665ffd83dbSDimitry Andric   for (unsigned i = 0; i < Bytes.size(); i++)
50675ffd83dbSDimitry Andric     dbgs() << Bytes[i] << " ";
50685ffd83dbSDimitry Andric   dbgs() << "}\n";
50695ffd83dbSDimitry Andric }
50705ffd83dbSDimitry Andric #endif
50715ffd83dbSDimitry Andric 
50725ffd83dbSDimitry Andric // If the Bytes vector matches an unpack operation, prepare to do the unpack
50735ffd83dbSDimitry Andric // after all else by removing the zero vector and the effect of the unpack on
50745ffd83dbSDimitry Andric // Bytes.
50755ffd83dbSDimitry Andric void GeneralShuffle::tryPrepareForUnpack() {
50765ffd83dbSDimitry Andric   uint32_t ZeroVecOpNo = findZeroVectorIdx(&Ops[0], Ops.size());
50775ffd83dbSDimitry Andric   if (ZeroVecOpNo == UINT32_MAX || Ops.size() == 1)
50785ffd83dbSDimitry Andric     return;
50795ffd83dbSDimitry Andric 
50805ffd83dbSDimitry Andric   // Only do this if removing the zero vector reduces the depth, otherwise
50815ffd83dbSDimitry Andric   // the critical path will increase with the final unpack.
50825ffd83dbSDimitry Andric   if (Ops.size() > 2 &&
50835ffd83dbSDimitry Andric       Log2_32_Ceil(Ops.size()) == Log2_32_Ceil(Ops.size() - 1))
50845ffd83dbSDimitry Andric     return;
50855ffd83dbSDimitry Andric 
50865ffd83dbSDimitry Andric   // Find an unpack that would allow removing the zero vector from Ops.
50875ffd83dbSDimitry Andric   UnpackFromEltSize = 1;
50885ffd83dbSDimitry Andric   for (; UnpackFromEltSize <= 4; UnpackFromEltSize *= 2) {
50895ffd83dbSDimitry Andric     bool MatchUnpack = true;
50905ffd83dbSDimitry Andric     SmallVector<int, SystemZ::VectorBytes> SrcBytes;
50915ffd83dbSDimitry Andric     for (unsigned Elt = 0; Elt < SystemZ::VectorBytes; Elt++) {
50925ffd83dbSDimitry Andric       unsigned ToEltSize = UnpackFromEltSize * 2;
50935ffd83dbSDimitry Andric       bool IsZextByte = (Elt % ToEltSize) < UnpackFromEltSize;
50945ffd83dbSDimitry Andric       if (!IsZextByte)
50955ffd83dbSDimitry Andric         SrcBytes.push_back(Bytes[Elt]);
50965ffd83dbSDimitry Andric       if (Bytes[Elt] != -1) {
50975ffd83dbSDimitry Andric         unsigned OpNo = unsigned(Bytes[Elt]) / SystemZ::VectorBytes;
50985ffd83dbSDimitry Andric         if (IsZextByte != (OpNo == ZeroVecOpNo)) {
50995ffd83dbSDimitry Andric           MatchUnpack = false;
51005ffd83dbSDimitry Andric           break;
51015ffd83dbSDimitry Andric         }
51025ffd83dbSDimitry Andric       }
51035ffd83dbSDimitry Andric     }
51045ffd83dbSDimitry Andric     if (MatchUnpack) {
51055ffd83dbSDimitry Andric       if (Ops.size() == 2) {
51065ffd83dbSDimitry Andric         // Don't use unpack if a single source operand needs rearrangement.
51075ffd83dbSDimitry Andric         for (unsigned i = 0; i < SystemZ::VectorBytes / 2; i++)
51085ffd83dbSDimitry Andric           if (SrcBytes[i] != -1 && SrcBytes[i] % 16 != int(i)) {
51095ffd83dbSDimitry Andric             UnpackFromEltSize = UINT_MAX;
51105ffd83dbSDimitry Andric             return;
51115ffd83dbSDimitry Andric           }
51125ffd83dbSDimitry Andric       }
51135ffd83dbSDimitry Andric       break;
51145ffd83dbSDimitry Andric     }
51155ffd83dbSDimitry Andric   }
51165ffd83dbSDimitry Andric   if (UnpackFromEltSize > 4)
51175ffd83dbSDimitry Andric     return;
51185ffd83dbSDimitry Andric 
51195ffd83dbSDimitry Andric   LLVM_DEBUG(dbgs() << "Preparing for final unpack of element size "
51205ffd83dbSDimitry Andric              << UnpackFromEltSize << ". Zero vector is Op#" << ZeroVecOpNo
51215ffd83dbSDimitry Andric              << ".\n";
51225ffd83dbSDimitry Andric              dumpBytes(Bytes, "Original Bytes vector:"););
51235ffd83dbSDimitry Andric 
51245ffd83dbSDimitry Andric   // Apply the unpack in reverse to the Bytes array.
51255ffd83dbSDimitry Andric   unsigned B = 0;
51265ffd83dbSDimitry Andric   for (unsigned Elt = 0; Elt < SystemZ::VectorBytes;) {
51275ffd83dbSDimitry Andric     Elt += UnpackFromEltSize;
51285ffd83dbSDimitry Andric     for (unsigned i = 0; i < UnpackFromEltSize; i++, Elt++, B++)
51295ffd83dbSDimitry Andric       Bytes[B] = Bytes[Elt];
51305ffd83dbSDimitry Andric   }
51315ffd83dbSDimitry Andric   while (B < SystemZ::VectorBytes)
51325ffd83dbSDimitry Andric     Bytes[B++] = -1;
51335ffd83dbSDimitry Andric 
51345ffd83dbSDimitry Andric   // Remove the zero vector from Ops
51355ffd83dbSDimitry Andric   Ops.erase(&Ops[ZeroVecOpNo]);
51365ffd83dbSDimitry Andric   for (unsigned I = 0; I < SystemZ::VectorBytes; ++I)
51375ffd83dbSDimitry Andric     if (Bytes[I] >= 0) {
51385ffd83dbSDimitry Andric       unsigned OpNo = unsigned(Bytes[I]) / SystemZ::VectorBytes;
51395ffd83dbSDimitry Andric       if (OpNo > ZeroVecOpNo)
51405ffd83dbSDimitry Andric         Bytes[I] -= SystemZ::VectorBytes;
51415ffd83dbSDimitry Andric     }
51425ffd83dbSDimitry Andric 
51435ffd83dbSDimitry Andric   LLVM_DEBUG(dumpBytes(Bytes, "Resulting Bytes vector, zero vector removed:");
51445ffd83dbSDimitry Andric              dbgs() << "\n";);
51455ffd83dbSDimitry Andric }
51465ffd83dbSDimitry Andric 
51475ffd83dbSDimitry Andric SDValue GeneralShuffle::insertUnpackIfPrepared(SelectionDAG &DAG,
51485ffd83dbSDimitry Andric                                                const SDLoc &DL,
51495ffd83dbSDimitry Andric                                                SDValue Op) {
51505ffd83dbSDimitry Andric   if (!unpackWasPrepared())
51515ffd83dbSDimitry Andric     return Op;
51525ffd83dbSDimitry Andric   unsigned InBits = UnpackFromEltSize * 8;
51535ffd83dbSDimitry Andric   EVT InVT = MVT::getVectorVT(MVT::getIntegerVT(InBits),
51545ffd83dbSDimitry Andric                                 SystemZ::VectorBits / InBits);
51555ffd83dbSDimitry Andric   SDValue PackedOp = DAG.getNode(ISD::BITCAST, DL, InVT, Op);
51565ffd83dbSDimitry Andric   unsigned OutBits = InBits * 2;
51575ffd83dbSDimitry Andric   EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(OutBits),
51585ffd83dbSDimitry Andric                                SystemZ::VectorBits / OutBits);
51595ffd83dbSDimitry Andric   return DAG.getNode(SystemZISD::UNPACKL_HIGH, DL, OutVT, PackedOp);
51605ffd83dbSDimitry Andric }
51615ffd83dbSDimitry Andric 
51620b57cec5SDimitry Andric // Return true if the given BUILD_VECTOR is a scalar-to-vector conversion.
51630b57cec5SDimitry Andric static bool isScalarToVector(SDValue Op) {
51640b57cec5SDimitry Andric   for (unsigned I = 1, E = Op.getNumOperands(); I != E; ++I)
51650b57cec5SDimitry Andric     if (!Op.getOperand(I).isUndef())
51660b57cec5SDimitry Andric       return false;
51670b57cec5SDimitry Andric   return true;
51680b57cec5SDimitry Andric }
51690b57cec5SDimitry Andric 
51700b57cec5SDimitry Andric // Return a vector of type VT that contains Value in the first element.
51710b57cec5SDimitry Andric // The other elements don't matter.
51720b57cec5SDimitry Andric static SDValue buildScalarToVector(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
51730b57cec5SDimitry Andric                                    SDValue Value) {
51740b57cec5SDimitry Andric   // If we have a constant, replicate it to all elements and let the
51750b57cec5SDimitry Andric   // BUILD_VECTOR lowering take care of it.
51760b57cec5SDimitry Andric   if (Value.getOpcode() == ISD::Constant ||
51770b57cec5SDimitry Andric       Value.getOpcode() == ISD::ConstantFP) {
51780b57cec5SDimitry Andric     SmallVector<SDValue, 16> Ops(VT.getVectorNumElements(), Value);
51790b57cec5SDimitry Andric     return DAG.getBuildVector(VT, DL, Ops);
51800b57cec5SDimitry Andric   }
51810b57cec5SDimitry Andric   if (Value.isUndef())
51820b57cec5SDimitry Andric     return DAG.getUNDEF(VT);
51830b57cec5SDimitry Andric   return DAG.getNode(ISD::SCALAR_TO_VECTOR, DL, VT, Value);
51840b57cec5SDimitry Andric }
51850b57cec5SDimitry Andric 
51860b57cec5SDimitry Andric // Return a vector of type VT in which Op0 is in element 0 and Op1 is in
51870b57cec5SDimitry Andric // element 1.  Used for cases in which replication is cheap.
51880b57cec5SDimitry Andric static SDValue buildMergeScalars(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
51890b57cec5SDimitry Andric                                  SDValue Op0, SDValue Op1) {
51900b57cec5SDimitry Andric   if (Op0.isUndef()) {
51910b57cec5SDimitry Andric     if (Op1.isUndef())
51920b57cec5SDimitry Andric       return DAG.getUNDEF(VT);
51930b57cec5SDimitry Andric     return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op1);
51940b57cec5SDimitry Andric   }
51950b57cec5SDimitry Andric   if (Op1.isUndef())
51960b57cec5SDimitry Andric     return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0);
51970b57cec5SDimitry Andric   return DAG.getNode(SystemZISD::MERGE_HIGH, DL, VT,
51980b57cec5SDimitry Andric                      buildScalarToVector(DAG, DL, VT, Op0),
51990b57cec5SDimitry Andric                      buildScalarToVector(DAG, DL, VT, Op1));
52000b57cec5SDimitry Andric }
52010b57cec5SDimitry Andric 
52020b57cec5SDimitry Andric // Extend GPR scalars Op0 and Op1 to doublewords and return a v2i64
52030b57cec5SDimitry Andric // vector for them.
52040b57cec5SDimitry Andric static SDValue joinDwords(SelectionDAG &DAG, const SDLoc &DL, SDValue Op0,
52050b57cec5SDimitry Andric                           SDValue Op1) {
52060b57cec5SDimitry Andric   if (Op0.isUndef() && Op1.isUndef())
52070b57cec5SDimitry Andric     return DAG.getUNDEF(MVT::v2i64);
52080b57cec5SDimitry Andric   // If one of the two inputs is undefined then replicate the other one,
52090b57cec5SDimitry Andric   // in order to avoid using another register unnecessarily.
52100b57cec5SDimitry Andric   if (Op0.isUndef())
52110b57cec5SDimitry Andric     Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
52120b57cec5SDimitry Andric   else if (Op1.isUndef())
52130b57cec5SDimitry Andric     Op0 = Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
52140b57cec5SDimitry Andric   else {
52150b57cec5SDimitry Andric     Op0 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op0);
52160b57cec5SDimitry Andric     Op1 = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i64, Op1);
52170b57cec5SDimitry Andric   }
52180b57cec5SDimitry Andric   return DAG.getNode(SystemZISD::JOIN_DWORDS, DL, MVT::v2i64, Op0, Op1);
52190b57cec5SDimitry Andric }
52200b57cec5SDimitry Andric 
52210b57cec5SDimitry Andric // If a BUILD_VECTOR contains some EXTRACT_VECTOR_ELTs, it's usually
52220b57cec5SDimitry Andric // better to use VECTOR_SHUFFLEs on them, only using BUILD_VECTOR for
52230b57cec5SDimitry Andric // the non-EXTRACT_VECTOR_ELT elements.  See if the given BUILD_VECTOR
52240b57cec5SDimitry Andric // would benefit from this representation and return it if so.
52250b57cec5SDimitry Andric static SDValue tryBuildVectorShuffle(SelectionDAG &DAG,
52260b57cec5SDimitry Andric                                      BuildVectorSDNode *BVN) {
52270b57cec5SDimitry Andric   EVT VT = BVN->getValueType(0);
52280b57cec5SDimitry Andric   unsigned NumElements = VT.getVectorNumElements();
52290b57cec5SDimitry Andric 
52300b57cec5SDimitry Andric   // Represent the BUILD_VECTOR as an N-operand VECTOR_SHUFFLE-like operation
52310b57cec5SDimitry Andric   // on byte vectors.  If there are non-EXTRACT_VECTOR_ELT elements that still
52320b57cec5SDimitry Andric   // need a BUILD_VECTOR, add an additional placeholder operand for that
52330b57cec5SDimitry Andric   // BUILD_VECTOR and store its operands in ResidueOps.
52340b57cec5SDimitry Andric   GeneralShuffle GS(VT);
52350b57cec5SDimitry Andric   SmallVector<SDValue, SystemZ::VectorBytes> ResidueOps;
52360b57cec5SDimitry Andric   bool FoundOne = false;
52370b57cec5SDimitry Andric   for (unsigned I = 0; I < NumElements; ++I) {
52380b57cec5SDimitry Andric     SDValue Op = BVN->getOperand(I);
52390b57cec5SDimitry Andric     if (Op.getOpcode() == ISD::TRUNCATE)
52400b57cec5SDimitry Andric       Op = Op.getOperand(0);
52410b57cec5SDimitry Andric     if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
52420b57cec5SDimitry Andric         Op.getOperand(1).getOpcode() == ISD::Constant) {
52430b57cec5SDimitry Andric       unsigned Elem = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
52440b57cec5SDimitry Andric       if (!GS.add(Op.getOperand(0), Elem))
52450b57cec5SDimitry Andric         return SDValue();
52460b57cec5SDimitry Andric       FoundOne = true;
52470b57cec5SDimitry Andric     } else if (Op.isUndef()) {
52480b57cec5SDimitry Andric       GS.addUndef();
52490b57cec5SDimitry Andric     } else {
52500b57cec5SDimitry Andric       if (!GS.add(SDValue(), ResidueOps.size()))
52510b57cec5SDimitry Andric         return SDValue();
52520b57cec5SDimitry Andric       ResidueOps.push_back(BVN->getOperand(I));
52530b57cec5SDimitry Andric     }
52540b57cec5SDimitry Andric   }
52550b57cec5SDimitry Andric 
52560b57cec5SDimitry Andric   // Nothing to do if there are no EXTRACT_VECTOR_ELTs.
52570b57cec5SDimitry Andric   if (!FoundOne)
52580b57cec5SDimitry Andric     return SDValue();
52590b57cec5SDimitry Andric 
52600b57cec5SDimitry Andric   // Create the BUILD_VECTOR for the remaining elements, if any.
52610b57cec5SDimitry Andric   if (!ResidueOps.empty()) {
52620b57cec5SDimitry Andric     while (ResidueOps.size() < NumElements)
52630b57cec5SDimitry Andric       ResidueOps.push_back(DAG.getUNDEF(ResidueOps[0].getValueType()));
52640b57cec5SDimitry Andric     for (auto &Op : GS.Ops) {
52650b57cec5SDimitry Andric       if (!Op.getNode()) {
52660b57cec5SDimitry Andric         Op = DAG.getBuildVector(VT, SDLoc(BVN), ResidueOps);
52670b57cec5SDimitry Andric         break;
52680b57cec5SDimitry Andric       }
52690b57cec5SDimitry Andric     }
52700b57cec5SDimitry Andric   }
52710b57cec5SDimitry Andric   return GS.getNode(DAG, SDLoc(BVN));
52720b57cec5SDimitry Andric }
52730b57cec5SDimitry Andric 
52740b57cec5SDimitry Andric bool SystemZTargetLowering::isVectorElementLoad(SDValue Op) const {
52750b57cec5SDimitry Andric   if (Op.getOpcode() == ISD::LOAD && cast<LoadSDNode>(Op)->isUnindexed())
52760b57cec5SDimitry Andric     return true;
52770b57cec5SDimitry Andric   if (Subtarget.hasVectorEnhancements2() && Op.getOpcode() == SystemZISD::LRV)
52780b57cec5SDimitry Andric     return true;
52790b57cec5SDimitry Andric   return false;
52800b57cec5SDimitry Andric }
52810b57cec5SDimitry Andric 
52820b57cec5SDimitry Andric // Combine GPR scalar values Elems into a vector of type VT.
52830b57cec5SDimitry Andric SDValue
52840b57cec5SDimitry Andric SystemZTargetLowering::buildVector(SelectionDAG &DAG, const SDLoc &DL, EVT VT,
52850b57cec5SDimitry Andric                                    SmallVectorImpl<SDValue> &Elems) const {
52860b57cec5SDimitry Andric   // See whether there is a single replicated value.
52870b57cec5SDimitry Andric   SDValue Single;
52880b57cec5SDimitry Andric   unsigned int NumElements = Elems.size();
52890b57cec5SDimitry Andric   unsigned int Count = 0;
52900b57cec5SDimitry Andric   for (auto Elem : Elems) {
52910b57cec5SDimitry Andric     if (!Elem.isUndef()) {
52920b57cec5SDimitry Andric       if (!Single.getNode())
52930b57cec5SDimitry Andric         Single = Elem;
52940b57cec5SDimitry Andric       else if (Elem != Single) {
52950b57cec5SDimitry Andric         Single = SDValue();
52960b57cec5SDimitry Andric         break;
52970b57cec5SDimitry Andric       }
52980b57cec5SDimitry Andric       Count += 1;
52990b57cec5SDimitry Andric     }
53000b57cec5SDimitry Andric   }
53010b57cec5SDimitry Andric   // There are three cases here:
53020b57cec5SDimitry Andric   //
53030b57cec5SDimitry Andric   // - if the only defined element is a loaded one, the best sequence
53040b57cec5SDimitry Andric   //   is a replicating load.
53050b57cec5SDimitry Andric   //
53060b57cec5SDimitry Andric   // - otherwise, if the only defined element is an i64 value, we will
53070b57cec5SDimitry Andric   //   end up with the same VLVGP sequence regardless of whether we short-cut
53080b57cec5SDimitry Andric   //   for replication or fall through to the later code.
53090b57cec5SDimitry Andric   //
53100b57cec5SDimitry Andric   // - otherwise, if the only defined element is an i32 or smaller value,
53110b57cec5SDimitry Andric   //   we would need 2 instructions to replicate it: VLVGP followed by VREPx.
53120b57cec5SDimitry Andric   //   This is only a win if the single defined element is used more than once.
53130b57cec5SDimitry Andric   //   In other cases we're better off using a single VLVGx.
53140b57cec5SDimitry Andric   if (Single.getNode() && (Count > 1 || isVectorElementLoad(Single)))
53150b57cec5SDimitry Andric     return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Single);
53160b57cec5SDimitry Andric 
53170b57cec5SDimitry Andric   // If all elements are loads, use VLREP/VLEs (below).
53180b57cec5SDimitry Andric   bool AllLoads = true;
53190b57cec5SDimitry Andric   for (auto Elem : Elems)
53200b57cec5SDimitry Andric     if (!isVectorElementLoad(Elem)) {
53210b57cec5SDimitry Andric       AllLoads = false;
53220b57cec5SDimitry Andric       break;
53230b57cec5SDimitry Andric     }
53240b57cec5SDimitry Andric 
53250b57cec5SDimitry Andric   // The best way of building a v2i64 from two i64s is to use VLVGP.
53260b57cec5SDimitry Andric   if (VT == MVT::v2i64 && !AllLoads)
53270b57cec5SDimitry Andric     return joinDwords(DAG, DL, Elems[0], Elems[1]);
53280b57cec5SDimitry Andric 
53290b57cec5SDimitry Andric   // Use a 64-bit merge high to combine two doubles.
53300b57cec5SDimitry Andric   if (VT == MVT::v2f64 && !AllLoads)
53310b57cec5SDimitry Andric     return buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
53320b57cec5SDimitry Andric 
53330b57cec5SDimitry Andric   // Build v4f32 values directly from the FPRs:
53340b57cec5SDimitry Andric   //
53350b57cec5SDimitry Andric   //   <Axxx> <Bxxx> <Cxxxx> <Dxxx>
53360b57cec5SDimitry Andric   //         V              V         VMRHF
53370b57cec5SDimitry Andric   //      <ABxx>         <CDxx>
53380b57cec5SDimitry Andric   //                V                 VMRHG
53390b57cec5SDimitry Andric   //              <ABCD>
53400b57cec5SDimitry Andric   if (VT == MVT::v4f32 && !AllLoads) {
53410b57cec5SDimitry Andric     SDValue Op01 = buildMergeScalars(DAG, DL, VT, Elems[0], Elems[1]);
53420b57cec5SDimitry Andric     SDValue Op23 = buildMergeScalars(DAG, DL, VT, Elems[2], Elems[3]);
53430b57cec5SDimitry Andric     // Avoid unnecessary undefs by reusing the other operand.
53440b57cec5SDimitry Andric     if (Op01.isUndef())
53450b57cec5SDimitry Andric       Op01 = Op23;
53460b57cec5SDimitry Andric     else if (Op23.isUndef())
53470b57cec5SDimitry Andric       Op23 = Op01;
53480b57cec5SDimitry Andric     // Merging identical replications is a no-op.
53490b57cec5SDimitry Andric     if (Op01.getOpcode() == SystemZISD::REPLICATE && Op01 == Op23)
53500b57cec5SDimitry Andric       return Op01;
53510b57cec5SDimitry Andric     Op01 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op01);
53520b57cec5SDimitry Andric     Op23 = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Op23);
53530b57cec5SDimitry Andric     SDValue Op = DAG.getNode(SystemZISD::MERGE_HIGH,
53540b57cec5SDimitry Andric                              DL, MVT::v2i64, Op01, Op23);
53550b57cec5SDimitry Andric     return DAG.getNode(ISD::BITCAST, DL, VT, Op);
53560b57cec5SDimitry Andric   }
53570b57cec5SDimitry Andric 
53580b57cec5SDimitry Andric   // Collect the constant terms.
53590b57cec5SDimitry Andric   SmallVector<SDValue, SystemZ::VectorBytes> Constants(NumElements, SDValue());
53600b57cec5SDimitry Andric   SmallVector<bool, SystemZ::VectorBytes> Done(NumElements, false);
53610b57cec5SDimitry Andric 
53620b57cec5SDimitry Andric   unsigned NumConstants = 0;
53630b57cec5SDimitry Andric   for (unsigned I = 0; I < NumElements; ++I) {
53640b57cec5SDimitry Andric     SDValue Elem = Elems[I];
53650b57cec5SDimitry Andric     if (Elem.getOpcode() == ISD::Constant ||
53660b57cec5SDimitry Andric         Elem.getOpcode() == ISD::ConstantFP) {
53670b57cec5SDimitry Andric       NumConstants += 1;
53680b57cec5SDimitry Andric       Constants[I] = Elem;
53690b57cec5SDimitry Andric       Done[I] = true;
53700b57cec5SDimitry Andric     }
53710b57cec5SDimitry Andric   }
53720b57cec5SDimitry Andric   // If there was at least one constant, fill in the other elements of
53730b57cec5SDimitry Andric   // Constants with undefs to get a full vector constant and use that
53740b57cec5SDimitry Andric   // as the starting point.
53750b57cec5SDimitry Andric   SDValue Result;
53760b57cec5SDimitry Andric   SDValue ReplicatedVal;
53770b57cec5SDimitry Andric   if (NumConstants > 0) {
53780b57cec5SDimitry Andric     for (unsigned I = 0; I < NumElements; ++I)
53790b57cec5SDimitry Andric       if (!Constants[I].getNode())
53800b57cec5SDimitry Andric         Constants[I] = DAG.getUNDEF(Elems[I].getValueType());
53810b57cec5SDimitry Andric     Result = DAG.getBuildVector(VT, DL, Constants);
53820b57cec5SDimitry Andric   } else {
53830b57cec5SDimitry Andric     // Otherwise try to use VLREP or VLVGP to start the sequence in order to
53840b57cec5SDimitry Andric     // avoid a false dependency on any previous contents of the vector
53850b57cec5SDimitry Andric     // register.
53860b57cec5SDimitry Andric 
53870b57cec5SDimitry Andric     // Use a VLREP if at least one element is a load. Make sure to replicate
53880b57cec5SDimitry Andric     // the load with the most elements having its value.
53890b57cec5SDimitry Andric     std::map<const SDNode*, unsigned> UseCounts;
53900b57cec5SDimitry Andric     SDNode *LoadMaxUses = nullptr;
53910b57cec5SDimitry Andric     for (unsigned I = 0; I < NumElements; ++I)
53920b57cec5SDimitry Andric       if (isVectorElementLoad(Elems[I])) {
53930b57cec5SDimitry Andric         SDNode *Ld = Elems[I].getNode();
53940b57cec5SDimitry Andric         UseCounts[Ld]++;
53950b57cec5SDimitry Andric         if (LoadMaxUses == nullptr || UseCounts[LoadMaxUses] < UseCounts[Ld])
53960b57cec5SDimitry Andric           LoadMaxUses = Ld;
53970b57cec5SDimitry Andric       }
53980b57cec5SDimitry Andric     if (LoadMaxUses != nullptr) {
53990b57cec5SDimitry Andric       ReplicatedVal = SDValue(LoadMaxUses, 0);
54000b57cec5SDimitry Andric       Result = DAG.getNode(SystemZISD::REPLICATE, DL, VT, ReplicatedVal);
54010b57cec5SDimitry Andric     } else {
54020b57cec5SDimitry Andric       // Try to use VLVGP.
54030b57cec5SDimitry Andric       unsigned I1 = NumElements / 2 - 1;
54040b57cec5SDimitry Andric       unsigned I2 = NumElements - 1;
54050b57cec5SDimitry Andric       bool Def1 = !Elems[I1].isUndef();
54060b57cec5SDimitry Andric       bool Def2 = !Elems[I2].isUndef();
54070b57cec5SDimitry Andric       if (Def1 || Def2) {
54080b57cec5SDimitry Andric         SDValue Elem1 = Elems[Def1 ? I1 : I2];
54090b57cec5SDimitry Andric         SDValue Elem2 = Elems[Def2 ? I2 : I1];
54100b57cec5SDimitry Andric         Result = DAG.getNode(ISD::BITCAST, DL, VT,
54110b57cec5SDimitry Andric                              joinDwords(DAG, DL, Elem1, Elem2));
54120b57cec5SDimitry Andric         Done[I1] = true;
54130b57cec5SDimitry Andric         Done[I2] = true;
54140b57cec5SDimitry Andric       } else
54150b57cec5SDimitry Andric         Result = DAG.getUNDEF(VT);
54160b57cec5SDimitry Andric     }
54170b57cec5SDimitry Andric   }
54180b57cec5SDimitry Andric 
54190b57cec5SDimitry Andric   // Use VLVGx to insert the other elements.
54200b57cec5SDimitry Andric   for (unsigned I = 0; I < NumElements; ++I)
54210b57cec5SDimitry Andric     if (!Done[I] && !Elems[I].isUndef() && Elems[I] != ReplicatedVal)
54220b57cec5SDimitry Andric       Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, Result, Elems[I],
54230b57cec5SDimitry Andric                            DAG.getConstant(I, DL, MVT::i32));
54240b57cec5SDimitry Andric   return Result;
54250b57cec5SDimitry Andric }
54260b57cec5SDimitry Andric 
54270b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerBUILD_VECTOR(SDValue Op,
54280b57cec5SDimitry Andric                                                  SelectionDAG &DAG) const {
54290b57cec5SDimitry Andric   auto *BVN = cast<BuildVectorSDNode>(Op.getNode());
54300b57cec5SDimitry Andric   SDLoc DL(Op);
54310b57cec5SDimitry Andric   EVT VT = Op.getValueType();
54320b57cec5SDimitry Andric 
54330b57cec5SDimitry Andric   if (BVN->isConstant()) {
54340b57cec5SDimitry Andric     if (SystemZVectorConstantInfo(BVN).isVectorConstantLegal(Subtarget))
54350b57cec5SDimitry Andric       return Op;
54360b57cec5SDimitry Andric 
54370b57cec5SDimitry Andric     // Fall back to loading it from memory.
54380b57cec5SDimitry Andric     return SDValue();
54390b57cec5SDimitry Andric   }
54400b57cec5SDimitry Andric 
54410b57cec5SDimitry Andric   // See if we should use shuffles to construct the vector from other vectors.
54420b57cec5SDimitry Andric   if (SDValue Res = tryBuildVectorShuffle(DAG, BVN))
54430b57cec5SDimitry Andric     return Res;
54440b57cec5SDimitry Andric 
54450b57cec5SDimitry Andric   // Detect SCALAR_TO_VECTOR conversions.
54460b57cec5SDimitry Andric   if (isOperationLegal(ISD::SCALAR_TO_VECTOR, VT) && isScalarToVector(Op))
54470b57cec5SDimitry Andric     return buildScalarToVector(DAG, DL, VT, Op.getOperand(0));
54480b57cec5SDimitry Andric 
54490b57cec5SDimitry Andric   // Otherwise use buildVector to build the vector up from GPRs.
54500b57cec5SDimitry Andric   unsigned NumElements = Op.getNumOperands();
54510b57cec5SDimitry Andric   SmallVector<SDValue, SystemZ::VectorBytes> Ops(NumElements);
54520b57cec5SDimitry Andric   for (unsigned I = 0; I < NumElements; ++I)
54530b57cec5SDimitry Andric     Ops[I] = Op.getOperand(I);
54540b57cec5SDimitry Andric   return buildVector(DAG, DL, VT, Ops);
54550b57cec5SDimitry Andric }
54560b57cec5SDimitry Andric 
54570b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerVECTOR_SHUFFLE(SDValue Op,
54580b57cec5SDimitry Andric                                                    SelectionDAG &DAG) const {
54590b57cec5SDimitry Andric   auto *VSN = cast<ShuffleVectorSDNode>(Op.getNode());
54600b57cec5SDimitry Andric   SDLoc DL(Op);
54610b57cec5SDimitry Andric   EVT VT = Op.getValueType();
54620b57cec5SDimitry Andric   unsigned NumElements = VT.getVectorNumElements();
54630b57cec5SDimitry Andric 
54640b57cec5SDimitry Andric   if (VSN->isSplat()) {
54650b57cec5SDimitry Andric     SDValue Op0 = Op.getOperand(0);
54660b57cec5SDimitry Andric     unsigned Index = VSN->getSplatIndex();
54670b57cec5SDimitry Andric     assert(Index < VT.getVectorNumElements() &&
54680b57cec5SDimitry Andric            "Splat index should be defined and in first operand");
54690b57cec5SDimitry Andric     // See whether the value we're splatting is directly available as a scalar.
54700b57cec5SDimitry Andric     if ((Index == 0 && Op0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
54710b57cec5SDimitry Andric         Op0.getOpcode() == ISD::BUILD_VECTOR)
54720b57cec5SDimitry Andric       return DAG.getNode(SystemZISD::REPLICATE, DL, VT, Op0.getOperand(Index));
54730b57cec5SDimitry Andric     // Otherwise keep it as a vector-to-vector operation.
54740b57cec5SDimitry Andric     return DAG.getNode(SystemZISD::SPLAT, DL, VT, Op.getOperand(0),
54758bcb0991SDimitry Andric                        DAG.getTargetConstant(Index, DL, MVT::i32));
54760b57cec5SDimitry Andric   }
54770b57cec5SDimitry Andric 
54780b57cec5SDimitry Andric   GeneralShuffle GS(VT);
54790b57cec5SDimitry Andric   for (unsigned I = 0; I < NumElements; ++I) {
54800b57cec5SDimitry Andric     int Elt = VSN->getMaskElt(I);
54810b57cec5SDimitry Andric     if (Elt < 0)
54820b57cec5SDimitry Andric       GS.addUndef();
54830b57cec5SDimitry Andric     else if (!GS.add(Op.getOperand(unsigned(Elt) / NumElements),
54840b57cec5SDimitry Andric                      unsigned(Elt) % NumElements))
54850b57cec5SDimitry Andric       return SDValue();
54860b57cec5SDimitry Andric   }
54870b57cec5SDimitry Andric   return GS.getNode(DAG, SDLoc(VSN));
54880b57cec5SDimitry Andric }
54890b57cec5SDimitry Andric 
54900b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerSCALAR_TO_VECTOR(SDValue Op,
54910b57cec5SDimitry Andric                                                      SelectionDAG &DAG) const {
54920b57cec5SDimitry Andric   SDLoc DL(Op);
54930b57cec5SDimitry Andric   // Just insert the scalar into element 0 of an undefined vector.
54940b57cec5SDimitry Andric   return DAG.getNode(ISD::INSERT_VECTOR_ELT, DL,
54950b57cec5SDimitry Andric                      Op.getValueType(), DAG.getUNDEF(Op.getValueType()),
54960b57cec5SDimitry Andric                      Op.getOperand(0), DAG.getConstant(0, DL, MVT::i32));
54970b57cec5SDimitry Andric }
54980b57cec5SDimitry Andric 
54990b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerINSERT_VECTOR_ELT(SDValue Op,
55000b57cec5SDimitry Andric                                                       SelectionDAG &DAG) const {
55010b57cec5SDimitry Andric   // Handle insertions of floating-point values.
55020b57cec5SDimitry Andric   SDLoc DL(Op);
55030b57cec5SDimitry Andric   SDValue Op0 = Op.getOperand(0);
55040b57cec5SDimitry Andric   SDValue Op1 = Op.getOperand(1);
55050b57cec5SDimitry Andric   SDValue Op2 = Op.getOperand(2);
55060b57cec5SDimitry Andric   EVT VT = Op.getValueType();
55070b57cec5SDimitry Andric 
55080b57cec5SDimitry Andric   // Insertions into constant indices of a v2f64 can be done using VPDI.
55090b57cec5SDimitry Andric   // However, if the inserted value is a bitcast or a constant then it's
55100b57cec5SDimitry Andric   // better to use GPRs, as below.
55110b57cec5SDimitry Andric   if (VT == MVT::v2f64 &&
55120b57cec5SDimitry Andric       Op1.getOpcode() != ISD::BITCAST &&
55130b57cec5SDimitry Andric       Op1.getOpcode() != ISD::ConstantFP &&
55140b57cec5SDimitry Andric       Op2.getOpcode() == ISD::Constant) {
55150b57cec5SDimitry Andric     uint64_t Index = cast<ConstantSDNode>(Op2)->getZExtValue();
55160b57cec5SDimitry Andric     unsigned Mask = VT.getVectorNumElements() - 1;
55170b57cec5SDimitry Andric     if (Index <= Mask)
55180b57cec5SDimitry Andric       return Op;
55190b57cec5SDimitry Andric   }
55200b57cec5SDimitry Andric 
55210b57cec5SDimitry Andric   // Otherwise bitcast to the equivalent integer form and insert via a GPR.
55220b57cec5SDimitry Andric   MVT IntVT = MVT::getIntegerVT(VT.getScalarSizeInBits());
55230b57cec5SDimitry Andric   MVT IntVecVT = MVT::getVectorVT(IntVT, VT.getVectorNumElements());
55240b57cec5SDimitry Andric   SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, IntVecVT,
55250b57cec5SDimitry Andric                             DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0),
55260b57cec5SDimitry Andric                             DAG.getNode(ISD::BITCAST, DL, IntVT, Op1), Op2);
55270b57cec5SDimitry Andric   return DAG.getNode(ISD::BITCAST, DL, VT, Res);
55280b57cec5SDimitry Andric }
55290b57cec5SDimitry Andric 
55300b57cec5SDimitry Andric SDValue
55310b57cec5SDimitry Andric SystemZTargetLowering::lowerEXTRACT_VECTOR_ELT(SDValue Op,
55320b57cec5SDimitry Andric                                                SelectionDAG &DAG) const {
55330b57cec5SDimitry Andric   // Handle extractions of floating-point values.
55340b57cec5SDimitry Andric   SDLoc DL(Op);
55350b57cec5SDimitry Andric   SDValue Op0 = Op.getOperand(0);
55360b57cec5SDimitry Andric   SDValue Op1 = Op.getOperand(1);
55370b57cec5SDimitry Andric   EVT VT = Op.getValueType();
55380b57cec5SDimitry Andric   EVT VecVT = Op0.getValueType();
55390b57cec5SDimitry Andric 
55400b57cec5SDimitry Andric   // Extractions of constant indices can be done directly.
55410b57cec5SDimitry Andric   if (auto *CIndexN = dyn_cast<ConstantSDNode>(Op1)) {
55420b57cec5SDimitry Andric     uint64_t Index = CIndexN->getZExtValue();
55430b57cec5SDimitry Andric     unsigned Mask = VecVT.getVectorNumElements() - 1;
55440b57cec5SDimitry Andric     if (Index <= Mask)
55450b57cec5SDimitry Andric       return Op;
55460b57cec5SDimitry Andric   }
55470b57cec5SDimitry Andric 
55480b57cec5SDimitry Andric   // Otherwise bitcast to the equivalent integer form and extract via a GPR.
55490b57cec5SDimitry Andric   MVT IntVT = MVT::getIntegerVT(VT.getSizeInBits());
55500b57cec5SDimitry Andric   MVT IntVecVT = MVT::getVectorVT(IntVT, VecVT.getVectorNumElements());
55510b57cec5SDimitry Andric   SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, IntVT,
55520b57cec5SDimitry Andric                             DAG.getNode(ISD::BITCAST, DL, IntVecVT, Op0), Op1);
55530b57cec5SDimitry Andric   return DAG.getNode(ISD::BITCAST, DL, VT, Res);
55540b57cec5SDimitry Andric }
55550b57cec5SDimitry Andric 
55565ffd83dbSDimitry Andric SDValue SystemZTargetLowering::
55575ffd83dbSDimitry Andric lowerSIGN_EXTEND_VECTOR_INREG(SDValue Op, SelectionDAG &DAG) const {
55580b57cec5SDimitry Andric   SDValue PackedOp = Op.getOperand(0);
55590b57cec5SDimitry Andric   EVT OutVT = Op.getValueType();
55600b57cec5SDimitry Andric   EVT InVT = PackedOp.getValueType();
55610b57cec5SDimitry Andric   unsigned ToBits = OutVT.getScalarSizeInBits();
55620b57cec5SDimitry Andric   unsigned FromBits = InVT.getScalarSizeInBits();
55630b57cec5SDimitry Andric   do {
55640b57cec5SDimitry Andric     FromBits *= 2;
55650b57cec5SDimitry Andric     EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(FromBits),
55660b57cec5SDimitry Andric                                  SystemZ::VectorBits / FromBits);
55675ffd83dbSDimitry Andric     PackedOp =
55685ffd83dbSDimitry Andric       DAG.getNode(SystemZISD::UNPACK_HIGH, SDLoc(PackedOp), OutVT, PackedOp);
55690b57cec5SDimitry Andric   } while (FromBits != ToBits);
55700b57cec5SDimitry Andric   return PackedOp;
55710b57cec5SDimitry Andric }
55720b57cec5SDimitry Andric 
55735ffd83dbSDimitry Andric // Lower a ZERO_EXTEND_VECTOR_INREG to a vector shuffle with a zero vector.
55745ffd83dbSDimitry Andric SDValue SystemZTargetLowering::
55755ffd83dbSDimitry Andric lowerZERO_EXTEND_VECTOR_INREG(SDValue Op, SelectionDAG &DAG) const {
55765ffd83dbSDimitry Andric   SDValue PackedOp = Op.getOperand(0);
55775ffd83dbSDimitry Andric   SDLoc DL(Op);
55785ffd83dbSDimitry Andric   EVT OutVT = Op.getValueType();
55795ffd83dbSDimitry Andric   EVT InVT = PackedOp.getValueType();
55805ffd83dbSDimitry Andric   unsigned InNumElts = InVT.getVectorNumElements();
55815ffd83dbSDimitry Andric   unsigned OutNumElts = OutVT.getVectorNumElements();
55825ffd83dbSDimitry Andric   unsigned NumInPerOut = InNumElts / OutNumElts;
55835ffd83dbSDimitry Andric 
55845ffd83dbSDimitry Andric   SDValue ZeroVec =
55855ffd83dbSDimitry Andric     DAG.getSplatVector(InVT, DL, DAG.getConstant(0, DL, InVT.getScalarType()));
55865ffd83dbSDimitry Andric 
55875ffd83dbSDimitry Andric   SmallVector<int, 16> Mask(InNumElts);
55885ffd83dbSDimitry Andric   unsigned ZeroVecElt = InNumElts;
55895ffd83dbSDimitry Andric   for (unsigned PackedElt = 0; PackedElt < OutNumElts; PackedElt++) {
55905ffd83dbSDimitry Andric     unsigned MaskElt = PackedElt * NumInPerOut;
55915ffd83dbSDimitry Andric     unsigned End = MaskElt + NumInPerOut - 1;
55925ffd83dbSDimitry Andric     for (; MaskElt < End; MaskElt++)
55935ffd83dbSDimitry Andric       Mask[MaskElt] = ZeroVecElt++;
55945ffd83dbSDimitry Andric     Mask[MaskElt] = PackedElt;
55955ffd83dbSDimitry Andric   }
55965ffd83dbSDimitry Andric   SDValue Shuf = DAG.getVectorShuffle(InVT, DL, PackedOp, ZeroVec, Mask);
55975ffd83dbSDimitry Andric   return DAG.getNode(ISD::BITCAST, DL, OutVT, Shuf);
55985ffd83dbSDimitry Andric }
55995ffd83dbSDimitry Andric 
56000b57cec5SDimitry Andric SDValue SystemZTargetLowering::lowerShift(SDValue Op, SelectionDAG &DAG,
56010b57cec5SDimitry Andric                                           unsigned ByScalar) const {
56020b57cec5SDimitry Andric   // Look for cases where a vector shift can use the *_BY_SCALAR form.
56030b57cec5SDimitry Andric   SDValue Op0 = Op.getOperand(0);
56040b57cec5SDimitry Andric   SDValue Op1 = Op.getOperand(1);
56050b57cec5SDimitry Andric   SDLoc DL(Op);
56060b57cec5SDimitry Andric   EVT VT = Op.getValueType();
56070b57cec5SDimitry Andric   unsigned ElemBitSize = VT.getScalarSizeInBits();
56080b57cec5SDimitry Andric 
56090b57cec5SDimitry Andric   // See whether the shift vector is a splat represented as BUILD_VECTOR.
56100b57cec5SDimitry Andric   if (auto *BVN = dyn_cast<BuildVectorSDNode>(Op1)) {
56110b57cec5SDimitry Andric     APInt SplatBits, SplatUndef;
56120b57cec5SDimitry Andric     unsigned SplatBitSize;
56130b57cec5SDimitry Andric     bool HasAnyUndefs;
56140b57cec5SDimitry Andric     // Check for constant splats.  Use ElemBitSize as the minimum element
56150b57cec5SDimitry Andric     // width and reject splats that need wider elements.
56160b57cec5SDimitry Andric     if (BVN->isConstantSplat(SplatBits, SplatUndef, SplatBitSize, HasAnyUndefs,
56170b57cec5SDimitry Andric                              ElemBitSize, true) &&
56180b57cec5SDimitry Andric         SplatBitSize == ElemBitSize) {
56190b57cec5SDimitry Andric       SDValue Shift = DAG.getConstant(SplatBits.getZExtValue() & 0xfff,
56200b57cec5SDimitry Andric                                       DL, MVT::i32);
56210b57cec5SDimitry Andric       return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
56220b57cec5SDimitry Andric     }
56230b57cec5SDimitry Andric     // Check for variable splats.
56240b57cec5SDimitry Andric     BitVector UndefElements;
56250b57cec5SDimitry Andric     SDValue Splat = BVN->getSplatValue(&UndefElements);
56260b57cec5SDimitry Andric     if (Splat) {
56270b57cec5SDimitry Andric       // Since i32 is the smallest legal type, we either need a no-op
56280b57cec5SDimitry Andric       // or a truncation.
56290b57cec5SDimitry Andric       SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, Splat);
56300b57cec5SDimitry Andric       return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
56310b57cec5SDimitry Andric     }
56320b57cec5SDimitry Andric   }
56330b57cec5SDimitry Andric 
56340b57cec5SDimitry Andric   // See whether the shift vector is a splat represented as SHUFFLE_VECTOR,
56350b57cec5SDimitry Andric   // and the shift amount is directly available in a GPR.
56360b57cec5SDimitry Andric   if (auto *VSN = dyn_cast<ShuffleVectorSDNode>(Op1)) {
56370b57cec5SDimitry Andric     if (VSN->isSplat()) {
56380b57cec5SDimitry Andric       SDValue VSNOp0 = VSN->getOperand(0);
56390b57cec5SDimitry Andric       unsigned Index = VSN->getSplatIndex();
56400b57cec5SDimitry Andric       assert(Index < VT.getVectorNumElements() &&
56410b57cec5SDimitry Andric              "Splat index should be defined and in first operand");
56420b57cec5SDimitry Andric       if ((Index == 0 && VSNOp0.getOpcode() == ISD::SCALAR_TO_VECTOR) ||
56430b57cec5SDimitry Andric           VSNOp0.getOpcode() == ISD::BUILD_VECTOR) {
56440b57cec5SDimitry Andric         // Since i32 is the smallest legal type, we either need a no-op
56450b57cec5SDimitry Andric         // or a truncation.
56460b57cec5SDimitry Andric         SDValue Shift = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32,
56470b57cec5SDimitry Andric                                     VSNOp0.getOperand(Index));
56480b57cec5SDimitry Andric         return DAG.getNode(ByScalar, DL, VT, Op0, Shift);
56490b57cec5SDimitry Andric       }
56500b57cec5SDimitry Andric     }
56510b57cec5SDimitry Andric   }
56520b57cec5SDimitry Andric 
56530b57cec5SDimitry Andric   // Otherwise just treat the current form as legal.
56540b57cec5SDimitry Andric   return Op;
56550b57cec5SDimitry Andric }
56560b57cec5SDimitry Andric 
565781ad6265SDimitry Andric SDValue SystemZTargetLowering::lowerIS_FPCLASS(SDValue Op,
565881ad6265SDimitry Andric                                                SelectionDAG &DAG) const {
565981ad6265SDimitry Andric   SDLoc DL(Op);
566081ad6265SDimitry Andric   MVT ResultVT = Op.getSimpleValueType();
566181ad6265SDimitry Andric   SDValue Arg = Op.getOperand(0);
566281ad6265SDimitry Andric   auto CNode = cast<ConstantSDNode>(Op.getOperand(1));
566381ad6265SDimitry Andric   unsigned Check = CNode->getZExtValue();
566481ad6265SDimitry Andric 
566581ad6265SDimitry Andric   unsigned TDCMask = 0;
566681ad6265SDimitry Andric   if (Check & fcSNan)
566781ad6265SDimitry Andric     TDCMask |= SystemZ::TDCMASK_SNAN_PLUS | SystemZ::TDCMASK_SNAN_MINUS;
566881ad6265SDimitry Andric   if (Check & fcQNan)
566981ad6265SDimitry Andric     TDCMask |= SystemZ::TDCMASK_QNAN_PLUS | SystemZ::TDCMASK_QNAN_MINUS;
567081ad6265SDimitry Andric   if (Check & fcPosInf)
567181ad6265SDimitry Andric     TDCMask |= SystemZ::TDCMASK_INFINITY_PLUS;
567281ad6265SDimitry Andric   if (Check & fcNegInf)
567381ad6265SDimitry Andric     TDCMask |= SystemZ::TDCMASK_INFINITY_MINUS;
567481ad6265SDimitry Andric   if (Check & fcPosNormal)
567581ad6265SDimitry Andric     TDCMask |= SystemZ::TDCMASK_NORMAL_PLUS;
567681ad6265SDimitry Andric   if (Check & fcNegNormal)
567781ad6265SDimitry Andric     TDCMask |= SystemZ::TDCMASK_NORMAL_MINUS;
567881ad6265SDimitry Andric   if (Check & fcPosSubnormal)
567981ad6265SDimitry Andric     TDCMask |= SystemZ::TDCMASK_SUBNORMAL_PLUS;
568081ad6265SDimitry Andric   if (Check & fcNegSubnormal)
568181ad6265SDimitry Andric     TDCMask |= SystemZ::TDCMASK_SUBNORMAL_MINUS;
568281ad6265SDimitry Andric   if (Check & fcPosZero)
568381ad6265SDimitry Andric     TDCMask |= SystemZ::TDCMASK_ZERO_PLUS;
568481ad6265SDimitry Andric   if (Check & fcNegZero)
568581ad6265SDimitry Andric     TDCMask |= SystemZ::TDCMASK_ZERO_MINUS;
568681ad6265SDimitry Andric   SDValue TDCMaskV = DAG.getConstant(TDCMask, DL, MVT::i64);
568781ad6265SDimitry Andric 
568881ad6265SDimitry Andric   SDValue Intr = DAG.getNode(SystemZISD::TDC, DL, ResultVT, Arg, TDCMaskV);
568981ad6265SDimitry Andric   return getCCResult(DAG, Intr);
569081ad6265SDimitry Andric }
569181ad6265SDimitry Andric 
56920b57cec5SDimitry Andric SDValue SystemZTargetLowering::LowerOperation(SDValue Op,
56930b57cec5SDimitry Andric                                               SelectionDAG &DAG) const {
56940b57cec5SDimitry Andric   switch (Op.getOpcode()) {
56950b57cec5SDimitry Andric   case ISD::FRAMEADDR:
56960b57cec5SDimitry Andric     return lowerFRAMEADDR(Op, DAG);
56970b57cec5SDimitry Andric   case ISD::RETURNADDR:
56980b57cec5SDimitry Andric     return lowerRETURNADDR(Op, DAG);
56990b57cec5SDimitry Andric   case ISD::BR_CC:
57000b57cec5SDimitry Andric     return lowerBR_CC(Op, DAG);
57010b57cec5SDimitry Andric   case ISD::SELECT_CC:
57020b57cec5SDimitry Andric     return lowerSELECT_CC(Op, DAG);
57030b57cec5SDimitry Andric   case ISD::SETCC:
57040b57cec5SDimitry Andric     return lowerSETCC(Op, DAG);
5705480093f4SDimitry Andric   case ISD::STRICT_FSETCC:
5706480093f4SDimitry Andric     return lowerSTRICT_FSETCC(Op, DAG, false);
5707480093f4SDimitry Andric   case ISD::STRICT_FSETCCS:
5708480093f4SDimitry Andric     return lowerSTRICT_FSETCC(Op, DAG, true);
57090b57cec5SDimitry Andric   case ISD::GlobalAddress:
57100b57cec5SDimitry Andric     return lowerGlobalAddress(cast<GlobalAddressSDNode>(Op), DAG);
57110b57cec5SDimitry Andric   case ISD::GlobalTLSAddress:
57120b57cec5SDimitry Andric     return lowerGlobalTLSAddress(cast<GlobalAddressSDNode>(Op), DAG);
57130b57cec5SDimitry Andric   case ISD::BlockAddress:
57140b57cec5SDimitry Andric     return lowerBlockAddress(cast<BlockAddressSDNode>(Op), DAG);
57150b57cec5SDimitry Andric   case ISD::JumpTable:
57160b57cec5SDimitry Andric     return lowerJumpTable(cast<JumpTableSDNode>(Op), DAG);
57170b57cec5SDimitry Andric   case ISD::ConstantPool:
57180b57cec5SDimitry Andric     return lowerConstantPool(cast<ConstantPoolSDNode>(Op), DAG);
57190b57cec5SDimitry Andric   case ISD::BITCAST:
57200b57cec5SDimitry Andric     return lowerBITCAST(Op, DAG);
57210b57cec5SDimitry Andric   case ISD::VASTART:
57220b57cec5SDimitry Andric     return lowerVASTART(Op, DAG);
57230b57cec5SDimitry Andric   case ISD::VACOPY:
57240b57cec5SDimitry Andric     return lowerVACOPY(Op, DAG);
57250b57cec5SDimitry Andric   case ISD::DYNAMIC_STACKALLOC:
57260b57cec5SDimitry Andric     return lowerDYNAMIC_STACKALLOC(Op, DAG);
57270b57cec5SDimitry Andric   case ISD::GET_DYNAMIC_AREA_OFFSET:
57280b57cec5SDimitry Andric     return lowerGET_DYNAMIC_AREA_OFFSET(Op, DAG);
57290b57cec5SDimitry Andric   case ISD::SMUL_LOHI:
57300b57cec5SDimitry Andric     return lowerSMUL_LOHI(Op, DAG);
57310b57cec5SDimitry Andric   case ISD::UMUL_LOHI:
57320b57cec5SDimitry Andric     return lowerUMUL_LOHI(Op, DAG);
57330b57cec5SDimitry Andric   case ISD::SDIVREM:
57340b57cec5SDimitry Andric     return lowerSDIVREM(Op, DAG);
57350b57cec5SDimitry Andric   case ISD::UDIVREM:
57360b57cec5SDimitry Andric     return lowerUDIVREM(Op, DAG);
57370b57cec5SDimitry Andric   case ISD::SADDO:
57380b57cec5SDimitry Andric   case ISD::SSUBO:
57390b57cec5SDimitry Andric   case ISD::UADDO:
57400b57cec5SDimitry Andric   case ISD::USUBO:
57410b57cec5SDimitry Andric     return lowerXALUO(Op, DAG);
57420b57cec5SDimitry Andric   case ISD::ADDCARRY:
57430b57cec5SDimitry Andric   case ISD::SUBCARRY:
57440b57cec5SDimitry Andric     return lowerADDSUBCARRY(Op, DAG);
57450b57cec5SDimitry Andric   case ISD::OR:
57460b57cec5SDimitry Andric     return lowerOR(Op, DAG);
57470b57cec5SDimitry Andric   case ISD::CTPOP:
57480b57cec5SDimitry Andric     return lowerCTPOP(Op, DAG);
57490b57cec5SDimitry Andric   case ISD::ATOMIC_FENCE:
57500b57cec5SDimitry Andric     return lowerATOMIC_FENCE(Op, DAG);
57510b57cec5SDimitry Andric   case ISD::ATOMIC_SWAP:
57520b57cec5SDimitry Andric     return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_SWAPW);
57530b57cec5SDimitry Andric   case ISD::ATOMIC_STORE:
57540b57cec5SDimitry Andric     return lowerATOMIC_STORE(Op, DAG);
57550b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD:
57560b57cec5SDimitry Andric     return lowerATOMIC_LOAD(Op, DAG);
57570b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_ADD:
57580b57cec5SDimitry Andric     return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_ADD);
57590b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_SUB:
57600b57cec5SDimitry Andric     return lowerATOMIC_LOAD_SUB(Op, DAG);
57610b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_AND:
57620b57cec5SDimitry Andric     return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_AND);
57630b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_OR:
57640b57cec5SDimitry Andric     return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_OR);
57650b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_XOR:
57660b57cec5SDimitry Andric     return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_XOR);
57670b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_NAND:
57680b57cec5SDimitry Andric     return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_NAND);
57690b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_MIN:
57700b57cec5SDimitry Andric     return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MIN);
57710b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_MAX:
57720b57cec5SDimitry Andric     return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_MAX);
57730b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_UMIN:
57740b57cec5SDimitry Andric     return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMIN);
57750b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD_UMAX:
57760b57cec5SDimitry Andric     return lowerATOMIC_LOAD_OP(Op, DAG, SystemZISD::ATOMIC_LOADW_UMAX);
57770b57cec5SDimitry Andric   case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS:
57780b57cec5SDimitry Andric     return lowerATOMIC_CMP_SWAP(Op, DAG);
57790b57cec5SDimitry Andric   case ISD::STACKSAVE:
57800b57cec5SDimitry Andric     return lowerSTACKSAVE(Op, DAG);
57810b57cec5SDimitry Andric   case ISD::STACKRESTORE:
57820b57cec5SDimitry Andric     return lowerSTACKRESTORE(Op, DAG);
57830b57cec5SDimitry Andric   case ISD::PREFETCH:
57840b57cec5SDimitry Andric     return lowerPREFETCH(Op, DAG);
57850b57cec5SDimitry Andric   case ISD::INTRINSIC_W_CHAIN:
57860b57cec5SDimitry Andric     return lowerINTRINSIC_W_CHAIN(Op, DAG);
57870b57cec5SDimitry Andric   case ISD::INTRINSIC_WO_CHAIN:
57880b57cec5SDimitry Andric     return lowerINTRINSIC_WO_CHAIN(Op, DAG);
57890b57cec5SDimitry Andric   case ISD::BUILD_VECTOR:
57900b57cec5SDimitry Andric     return lowerBUILD_VECTOR(Op, DAG);
57910b57cec5SDimitry Andric   case ISD::VECTOR_SHUFFLE:
57920b57cec5SDimitry Andric     return lowerVECTOR_SHUFFLE(Op, DAG);
57930b57cec5SDimitry Andric   case ISD::SCALAR_TO_VECTOR:
57940b57cec5SDimitry Andric     return lowerSCALAR_TO_VECTOR(Op, DAG);
57950b57cec5SDimitry Andric   case ISD::INSERT_VECTOR_ELT:
57960b57cec5SDimitry Andric     return lowerINSERT_VECTOR_ELT(Op, DAG);
57970b57cec5SDimitry Andric   case ISD::EXTRACT_VECTOR_ELT:
57980b57cec5SDimitry Andric     return lowerEXTRACT_VECTOR_ELT(Op, DAG);
57990b57cec5SDimitry Andric   case ISD::SIGN_EXTEND_VECTOR_INREG:
58005ffd83dbSDimitry Andric     return lowerSIGN_EXTEND_VECTOR_INREG(Op, DAG);
58010b57cec5SDimitry Andric   case ISD::ZERO_EXTEND_VECTOR_INREG:
58025ffd83dbSDimitry Andric     return lowerZERO_EXTEND_VECTOR_INREG(Op, DAG);
58030b57cec5SDimitry Andric   case ISD::SHL:
58040b57cec5SDimitry Andric     return lowerShift(Op, DAG, SystemZISD::VSHL_BY_SCALAR);
58050b57cec5SDimitry Andric   case ISD::SRL:
58060b57cec5SDimitry Andric     return lowerShift(Op, DAG, SystemZISD::VSRL_BY_SCALAR);
58070b57cec5SDimitry Andric   case ISD::SRA:
58080b57cec5SDimitry Andric     return lowerShift(Op, DAG, SystemZISD::VSRA_BY_SCALAR);
580981ad6265SDimitry Andric   case ISD::IS_FPCLASS:
581081ad6265SDimitry Andric     return lowerIS_FPCLASS(Op, DAG);
5811bdd1243dSDimitry Andric   case ISD::GET_ROUNDING:
5812bdd1243dSDimitry Andric     return lowerGET_ROUNDING(Op, DAG);
58130b57cec5SDimitry Andric   default:
58140b57cec5SDimitry Andric     llvm_unreachable("Unexpected node to lower");
58150b57cec5SDimitry Andric   }
58160b57cec5SDimitry Andric }
58170b57cec5SDimitry Andric 
58180b57cec5SDimitry Andric // Lower operations with invalid operand or result types (currently used
58190b57cec5SDimitry Andric // only for 128-bit integer types).
58200b57cec5SDimitry Andric void
58210b57cec5SDimitry Andric SystemZTargetLowering::LowerOperationWrapper(SDNode *N,
58220b57cec5SDimitry Andric                                              SmallVectorImpl<SDValue> &Results,
58230b57cec5SDimitry Andric                                              SelectionDAG &DAG) const {
58240b57cec5SDimitry Andric   switch (N->getOpcode()) {
58250b57cec5SDimitry Andric   case ISD::ATOMIC_LOAD: {
58260b57cec5SDimitry Andric     SDLoc DL(N);
58270b57cec5SDimitry Andric     SDVTList Tys = DAG.getVTList(MVT::Untyped, MVT::Other);
58280b57cec5SDimitry Andric     SDValue Ops[] = { N->getOperand(0), N->getOperand(1) };
58290b57cec5SDimitry Andric     MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
58300b57cec5SDimitry Andric     SDValue Res = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_LOAD_128,
58310b57cec5SDimitry Andric                                           DL, Tys, Ops, MVT::i128, MMO);
58320b57cec5SDimitry Andric     Results.push_back(lowerGR128ToI128(DAG, Res));
58330b57cec5SDimitry Andric     Results.push_back(Res.getValue(1));
58340b57cec5SDimitry Andric     break;
58350b57cec5SDimitry Andric   }
58360b57cec5SDimitry Andric   case ISD::ATOMIC_STORE: {
58370b57cec5SDimitry Andric     SDLoc DL(N);
58380b57cec5SDimitry Andric     SDVTList Tys = DAG.getVTList(MVT::Other);
58390b57cec5SDimitry Andric     SDValue Ops[] = { N->getOperand(0),
58400b57cec5SDimitry Andric                       lowerI128ToGR128(DAG, N->getOperand(2)),
58410b57cec5SDimitry Andric                       N->getOperand(1) };
58420b57cec5SDimitry Andric     MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
58430b57cec5SDimitry Andric     SDValue Res = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_STORE_128,
58440b57cec5SDimitry Andric                                           DL, Tys, Ops, MVT::i128, MMO);
58450b57cec5SDimitry Andric     // We have to enforce sequential consistency by performing a
58460b57cec5SDimitry Andric     // serialization operation after the store.
5847fe6060f1SDimitry Andric     if (cast<AtomicSDNode>(N)->getSuccessOrdering() ==
58480b57cec5SDimitry Andric         AtomicOrdering::SequentiallyConsistent)
58490b57cec5SDimitry Andric       Res = SDValue(DAG.getMachineNode(SystemZ::Serialize, DL,
58500b57cec5SDimitry Andric                                        MVT::Other, Res), 0);
58510b57cec5SDimitry Andric     Results.push_back(Res);
58520b57cec5SDimitry Andric     break;
58530b57cec5SDimitry Andric   }
58540b57cec5SDimitry Andric   case ISD::ATOMIC_CMP_SWAP_WITH_SUCCESS: {
58550b57cec5SDimitry Andric     SDLoc DL(N);
58560b57cec5SDimitry Andric     SDVTList Tys = DAG.getVTList(MVT::Untyped, MVT::i32, MVT::Other);
58570b57cec5SDimitry Andric     SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
58580b57cec5SDimitry Andric                       lowerI128ToGR128(DAG, N->getOperand(2)),
58590b57cec5SDimitry Andric                       lowerI128ToGR128(DAG, N->getOperand(3)) };
58600b57cec5SDimitry Andric     MachineMemOperand *MMO = cast<AtomicSDNode>(N)->getMemOperand();
58610b57cec5SDimitry Andric     SDValue Res = DAG.getMemIntrinsicNode(SystemZISD::ATOMIC_CMP_SWAP_128,
58620b57cec5SDimitry Andric                                           DL, Tys, Ops, MVT::i128, MMO);
58630b57cec5SDimitry Andric     SDValue Success = emitSETCC(DAG, DL, Res.getValue(1),
58640b57cec5SDimitry Andric                                 SystemZ::CCMASK_CS, SystemZ::CCMASK_CS_EQ);
58650b57cec5SDimitry Andric     Success = DAG.getZExtOrTrunc(Success, DL, N->getValueType(1));
58660b57cec5SDimitry Andric     Results.push_back(lowerGR128ToI128(DAG, Res));
58670b57cec5SDimitry Andric     Results.push_back(Success);
58680b57cec5SDimitry Andric     Results.push_back(Res.getValue(2));
58690b57cec5SDimitry Andric     break;
58700b57cec5SDimitry Andric   }
5871349cc55cSDimitry Andric   case ISD::BITCAST: {
5872349cc55cSDimitry Andric     SDValue Src = N->getOperand(0);
5873349cc55cSDimitry Andric     if (N->getValueType(0) == MVT::i128 && Src.getValueType() == MVT::f128 &&
5874349cc55cSDimitry Andric         !useSoftFloat()) {
5875349cc55cSDimitry Andric       SDLoc DL(N);
5876349cc55cSDimitry Andric       SDValue Lo, Hi;
5877349cc55cSDimitry Andric       if (getRepRegClassFor(MVT::f128) == &SystemZ::VR128BitRegClass) {
5878349cc55cSDimitry Andric         SDValue VecBC = DAG.getNode(ISD::BITCAST, DL, MVT::v2i64, Src);
5879349cc55cSDimitry Andric         Lo = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i64, VecBC,
5880349cc55cSDimitry Andric                          DAG.getConstant(1, DL, MVT::i32));
5881349cc55cSDimitry Andric         Hi = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, MVT::i64, VecBC,
5882349cc55cSDimitry Andric                          DAG.getConstant(0, DL, MVT::i32));
5883349cc55cSDimitry Andric       } else {
5884349cc55cSDimitry Andric         assert(getRepRegClassFor(MVT::f128) == &SystemZ::FP128BitRegClass &&
5885349cc55cSDimitry Andric                "Unrecognized register class for f128.");
5886349cc55cSDimitry Andric         SDValue LoFP = DAG.getTargetExtractSubreg(SystemZ::subreg_l64,
5887349cc55cSDimitry Andric                                                   DL, MVT::f64, Src);
5888349cc55cSDimitry Andric         SDValue HiFP = DAG.getTargetExtractSubreg(SystemZ::subreg_h64,
5889349cc55cSDimitry Andric                                                   DL, MVT::f64, Src);
5890349cc55cSDimitry Andric         Lo = DAG.getNode(ISD::BITCAST, DL, MVT::i64, LoFP);
5891349cc55cSDimitry Andric         Hi = DAG.getNode(ISD::BITCAST, DL, MVT::i64, HiFP);
5892349cc55cSDimitry Andric       }
5893349cc55cSDimitry Andric       Results.push_back(DAG.getNode(ISD::BUILD_PAIR, DL, MVT::i128, Lo, Hi));
5894349cc55cSDimitry Andric     }
5895349cc55cSDimitry Andric     break;
5896349cc55cSDimitry Andric   }
58970b57cec5SDimitry Andric   default:
58980b57cec5SDimitry Andric     llvm_unreachable("Unexpected node to lower");
58990b57cec5SDimitry Andric   }
59000b57cec5SDimitry Andric }
59010b57cec5SDimitry Andric 
59020b57cec5SDimitry Andric void
59030b57cec5SDimitry Andric SystemZTargetLowering::ReplaceNodeResults(SDNode *N,
59040b57cec5SDimitry Andric                                           SmallVectorImpl<SDValue> &Results,
59050b57cec5SDimitry Andric                                           SelectionDAG &DAG) const {
59060b57cec5SDimitry Andric   return LowerOperationWrapper(N, Results, DAG);
59070b57cec5SDimitry Andric }
59080b57cec5SDimitry Andric 
59090b57cec5SDimitry Andric const char *SystemZTargetLowering::getTargetNodeName(unsigned Opcode) const {
59100b57cec5SDimitry Andric #define OPCODE(NAME) case SystemZISD::NAME: return "SystemZISD::" #NAME
59110b57cec5SDimitry Andric   switch ((SystemZISD::NodeType)Opcode) {
59120b57cec5SDimitry Andric     case SystemZISD::FIRST_NUMBER: break;
59130b57cec5SDimitry Andric     OPCODE(RET_FLAG);
59140b57cec5SDimitry Andric     OPCODE(CALL);
59150b57cec5SDimitry Andric     OPCODE(SIBCALL);
59160b57cec5SDimitry Andric     OPCODE(TLS_GDCALL);
59170b57cec5SDimitry Andric     OPCODE(TLS_LDCALL);
59180b57cec5SDimitry Andric     OPCODE(PCREL_WRAPPER);
59190b57cec5SDimitry Andric     OPCODE(PCREL_OFFSET);
59200b57cec5SDimitry Andric     OPCODE(ICMP);
59210b57cec5SDimitry Andric     OPCODE(FCMP);
5922480093f4SDimitry Andric     OPCODE(STRICT_FCMP);
5923480093f4SDimitry Andric     OPCODE(STRICT_FCMPS);
59240b57cec5SDimitry Andric     OPCODE(TM);
59250b57cec5SDimitry Andric     OPCODE(BR_CCMASK);
59260b57cec5SDimitry Andric     OPCODE(SELECT_CCMASK);
59270b57cec5SDimitry Andric     OPCODE(ADJDYNALLOC);
59285ffd83dbSDimitry Andric     OPCODE(PROBED_ALLOCA);
59290b57cec5SDimitry Andric     OPCODE(POPCNT);
59300b57cec5SDimitry Andric     OPCODE(SMUL_LOHI);
59310b57cec5SDimitry Andric     OPCODE(UMUL_LOHI);
59320b57cec5SDimitry Andric     OPCODE(SDIVREM);
59330b57cec5SDimitry Andric     OPCODE(UDIVREM);
59340b57cec5SDimitry Andric     OPCODE(SADDO);
59350b57cec5SDimitry Andric     OPCODE(SSUBO);
59360b57cec5SDimitry Andric     OPCODE(UADDO);
59370b57cec5SDimitry Andric     OPCODE(USUBO);
59380b57cec5SDimitry Andric     OPCODE(ADDCARRY);
59390b57cec5SDimitry Andric     OPCODE(SUBCARRY);
59400b57cec5SDimitry Andric     OPCODE(GET_CCMASK);
59410b57cec5SDimitry Andric     OPCODE(MVC);
59420b57cec5SDimitry Andric     OPCODE(NC);
59430b57cec5SDimitry Andric     OPCODE(OC);
59440b57cec5SDimitry Andric     OPCODE(XC);
59450b57cec5SDimitry Andric     OPCODE(CLC);
59460eae32dcSDimitry Andric     OPCODE(MEMSET_MVC);
59470b57cec5SDimitry Andric     OPCODE(STPCPY);
59480b57cec5SDimitry Andric     OPCODE(STRCMP);
59490b57cec5SDimitry Andric     OPCODE(SEARCH_STRING);
59500b57cec5SDimitry Andric     OPCODE(IPM);
59510b57cec5SDimitry Andric     OPCODE(TBEGIN);
59520b57cec5SDimitry Andric     OPCODE(TBEGIN_NOFLOAT);
59530b57cec5SDimitry Andric     OPCODE(TEND);
59540b57cec5SDimitry Andric     OPCODE(BYTE_MASK);
59550b57cec5SDimitry Andric     OPCODE(ROTATE_MASK);
59560b57cec5SDimitry Andric     OPCODE(REPLICATE);
59570b57cec5SDimitry Andric     OPCODE(JOIN_DWORDS);
59580b57cec5SDimitry Andric     OPCODE(SPLAT);
59590b57cec5SDimitry Andric     OPCODE(MERGE_HIGH);
59600b57cec5SDimitry Andric     OPCODE(MERGE_LOW);
59610b57cec5SDimitry Andric     OPCODE(SHL_DOUBLE);
59620b57cec5SDimitry Andric     OPCODE(PERMUTE_DWORDS);
59630b57cec5SDimitry Andric     OPCODE(PERMUTE);
59640b57cec5SDimitry Andric     OPCODE(PACK);
59650b57cec5SDimitry Andric     OPCODE(PACKS_CC);
59660b57cec5SDimitry Andric     OPCODE(PACKLS_CC);
59670b57cec5SDimitry Andric     OPCODE(UNPACK_HIGH);
59680b57cec5SDimitry Andric     OPCODE(UNPACKL_HIGH);
59690b57cec5SDimitry Andric     OPCODE(UNPACK_LOW);
59700b57cec5SDimitry Andric     OPCODE(UNPACKL_LOW);
59710b57cec5SDimitry Andric     OPCODE(VSHL_BY_SCALAR);
59720b57cec5SDimitry Andric     OPCODE(VSRL_BY_SCALAR);
59730b57cec5SDimitry Andric     OPCODE(VSRA_BY_SCALAR);
59740b57cec5SDimitry Andric     OPCODE(VSUM);
59750b57cec5SDimitry Andric     OPCODE(VICMPE);
59760b57cec5SDimitry Andric     OPCODE(VICMPH);
59770b57cec5SDimitry Andric     OPCODE(VICMPHL);
59780b57cec5SDimitry Andric     OPCODE(VICMPES);
59790b57cec5SDimitry Andric     OPCODE(VICMPHS);
59800b57cec5SDimitry Andric     OPCODE(VICMPHLS);
59810b57cec5SDimitry Andric     OPCODE(VFCMPE);
5982480093f4SDimitry Andric     OPCODE(STRICT_VFCMPE);
5983480093f4SDimitry Andric     OPCODE(STRICT_VFCMPES);
59840b57cec5SDimitry Andric     OPCODE(VFCMPH);
5985480093f4SDimitry Andric     OPCODE(STRICT_VFCMPH);
5986480093f4SDimitry Andric     OPCODE(STRICT_VFCMPHS);
59870b57cec5SDimitry Andric     OPCODE(VFCMPHE);
5988480093f4SDimitry Andric     OPCODE(STRICT_VFCMPHE);
5989480093f4SDimitry Andric     OPCODE(STRICT_VFCMPHES);
59900b57cec5SDimitry Andric     OPCODE(VFCMPES);
59910b57cec5SDimitry Andric     OPCODE(VFCMPHS);
59920b57cec5SDimitry Andric     OPCODE(VFCMPHES);
59930b57cec5SDimitry Andric     OPCODE(VFTCI);
59940b57cec5SDimitry Andric     OPCODE(VEXTEND);
5995480093f4SDimitry Andric     OPCODE(STRICT_VEXTEND);
59960b57cec5SDimitry Andric     OPCODE(VROUND);
5997480093f4SDimitry Andric     OPCODE(STRICT_VROUND);
59980b57cec5SDimitry Andric     OPCODE(VTM);
59990b57cec5SDimitry Andric     OPCODE(VFAE_CC);
60000b57cec5SDimitry Andric     OPCODE(VFAEZ_CC);
60010b57cec5SDimitry Andric     OPCODE(VFEE_CC);
60020b57cec5SDimitry Andric     OPCODE(VFEEZ_CC);
60030b57cec5SDimitry Andric     OPCODE(VFENE_CC);
60040b57cec5SDimitry Andric     OPCODE(VFENEZ_CC);
60050b57cec5SDimitry Andric     OPCODE(VISTR_CC);
60060b57cec5SDimitry Andric     OPCODE(VSTRC_CC);
60070b57cec5SDimitry Andric     OPCODE(VSTRCZ_CC);
60080b57cec5SDimitry Andric     OPCODE(VSTRS_CC);
60090b57cec5SDimitry Andric     OPCODE(VSTRSZ_CC);
60100b57cec5SDimitry Andric     OPCODE(TDC);
60110b57cec5SDimitry Andric     OPCODE(ATOMIC_SWAPW);
60120b57cec5SDimitry Andric     OPCODE(ATOMIC_LOADW_ADD);
60130b57cec5SDimitry Andric     OPCODE(ATOMIC_LOADW_SUB);
60140b57cec5SDimitry Andric     OPCODE(ATOMIC_LOADW_AND);
60150b57cec5SDimitry Andric     OPCODE(ATOMIC_LOADW_OR);
60160b57cec5SDimitry Andric     OPCODE(ATOMIC_LOADW_XOR);
60170b57cec5SDimitry Andric     OPCODE(ATOMIC_LOADW_NAND);
60180b57cec5SDimitry Andric     OPCODE(ATOMIC_LOADW_MIN);
60190b57cec5SDimitry Andric     OPCODE(ATOMIC_LOADW_MAX);
60200b57cec5SDimitry Andric     OPCODE(ATOMIC_LOADW_UMIN);
60210b57cec5SDimitry Andric     OPCODE(ATOMIC_LOADW_UMAX);
60220b57cec5SDimitry Andric     OPCODE(ATOMIC_CMP_SWAPW);
60230b57cec5SDimitry Andric     OPCODE(ATOMIC_CMP_SWAP);
60240b57cec5SDimitry Andric     OPCODE(ATOMIC_LOAD_128);
60250b57cec5SDimitry Andric     OPCODE(ATOMIC_STORE_128);
60260b57cec5SDimitry Andric     OPCODE(ATOMIC_CMP_SWAP_128);
60270b57cec5SDimitry Andric     OPCODE(LRV);
60280b57cec5SDimitry Andric     OPCODE(STRV);
60290b57cec5SDimitry Andric     OPCODE(VLER);
60300b57cec5SDimitry Andric     OPCODE(VSTER);
60310b57cec5SDimitry Andric     OPCODE(PREFETCH);
60320b57cec5SDimitry Andric   }
60330b57cec5SDimitry Andric   return nullptr;
60340b57cec5SDimitry Andric #undef OPCODE
60350b57cec5SDimitry Andric }
60360b57cec5SDimitry Andric 
60370b57cec5SDimitry Andric // Return true if VT is a vector whose elements are a whole number of bytes
60380b57cec5SDimitry Andric // in width. Also check for presence of vector support.
60390b57cec5SDimitry Andric bool SystemZTargetLowering::canTreatAsByteVector(EVT VT) const {
60400b57cec5SDimitry Andric   if (!Subtarget.hasVector())
60410b57cec5SDimitry Andric     return false;
60420b57cec5SDimitry Andric 
60430b57cec5SDimitry Andric   return VT.isVector() && VT.getScalarSizeInBits() % 8 == 0 && VT.isSimple();
60440b57cec5SDimitry Andric }
60450b57cec5SDimitry Andric 
60460b57cec5SDimitry Andric // Try to simplify an EXTRACT_VECTOR_ELT from a vector of type VecVT
60470b57cec5SDimitry Andric // producing a result of type ResVT.  Op is a possibly bitcast version
60480b57cec5SDimitry Andric // of the input vector and Index is the index (based on type VecVT) that
60490b57cec5SDimitry Andric // should be extracted.  Return the new extraction if a simplification
60500b57cec5SDimitry Andric // was possible or if Force is true.
60510b57cec5SDimitry Andric SDValue SystemZTargetLowering::combineExtract(const SDLoc &DL, EVT ResVT,
60520b57cec5SDimitry Andric                                               EVT VecVT, SDValue Op,
60530b57cec5SDimitry Andric                                               unsigned Index,
60540b57cec5SDimitry Andric                                               DAGCombinerInfo &DCI,
60550b57cec5SDimitry Andric                                               bool Force) const {
60560b57cec5SDimitry Andric   SelectionDAG &DAG = DCI.DAG;
60570b57cec5SDimitry Andric 
60580b57cec5SDimitry Andric   // The number of bytes being extracted.
60590b57cec5SDimitry Andric   unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
60600b57cec5SDimitry Andric 
60610b57cec5SDimitry Andric   for (;;) {
60620b57cec5SDimitry Andric     unsigned Opcode = Op.getOpcode();
60630b57cec5SDimitry Andric     if (Opcode == ISD::BITCAST)
60640b57cec5SDimitry Andric       // Look through bitcasts.
60650b57cec5SDimitry Andric       Op = Op.getOperand(0);
60660b57cec5SDimitry Andric     else if ((Opcode == ISD::VECTOR_SHUFFLE || Opcode == SystemZISD::SPLAT) &&
60670b57cec5SDimitry Andric              canTreatAsByteVector(Op.getValueType())) {
60680b57cec5SDimitry Andric       // Get a VPERM-like permute mask and see whether the bytes covered
60690b57cec5SDimitry Andric       // by the extracted element are a contiguous sequence from one
60700b57cec5SDimitry Andric       // source operand.
60710b57cec5SDimitry Andric       SmallVector<int, SystemZ::VectorBytes> Bytes;
60720b57cec5SDimitry Andric       if (!getVPermMask(Op, Bytes))
60730b57cec5SDimitry Andric         break;
60740b57cec5SDimitry Andric       int First;
60750b57cec5SDimitry Andric       if (!getShuffleInput(Bytes, Index * BytesPerElement,
60760b57cec5SDimitry Andric                            BytesPerElement, First))
60770b57cec5SDimitry Andric         break;
60780b57cec5SDimitry Andric       if (First < 0)
60790b57cec5SDimitry Andric         return DAG.getUNDEF(ResVT);
60800b57cec5SDimitry Andric       // Make sure the contiguous sequence starts at a multiple of the
60810b57cec5SDimitry Andric       // original element size.
60820b57cec5SDimitry Andric       unsigned Byte = unsigned(First) % Bytes.size();
60830b57cec5SDimitry Andric       if (Byte % BytesPerElement != 0)
60840b57cec5SDimitry Andric         break;
60850b57cec5SDimitry Andric       // We can get the extracted value directly from an input.
60860b57cec5SDimitry Andric       Index = Byte / BytesPerElement;
60870b57cec5SDimitry Andric       Op = Op.getOperand(unsigned(First) / Bytes.size());
60880b57cec5SDimitry Andric       Force = true;
60890b57cec5SDimitry Andric     } else if (Opcode == ISD::BUILD_VECTOR &&
60900b57cec5SDimitry Andric                canTreatAsByteVector(Op.getValueType())) {
60910b57cec5SDimitry Andric       // We can only optimize this case if the BUILD_VECTOR elements are
60920b57cec5SDimitry Andric       // at least as wide as the extracted value.
60930b57cec5SDimitry Andric       EVT OpVT = Op.getValueType();
60940b57cec5SDimitry Andric       unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
60950b57cec5SDimitry Andric       if (OpBytesPerElement < BytesPerElement)
60960b57cec5SDimitry Andric         break;
60970b57cec5SDimitry Andric       // Make sure that the least-significant bit of the extracted value
60980b57cec5SDimitry Andric       // is the least significant bit of an input.
60990b57cec5SDimitry Andric       unsigned End = (Index + 1) * BytesPerElement;
61000b57cec5SDimitry Andric       if (End % OpBytesPerElement != 0)
61010b57cec5SDimitry Andric         break;
61020b57cec5SDimitry Andric       // We're extracting the low part of one operand of the BUILD_VECTOR.
61030b57cec5SDimitry Andric       Op = Op.getOperand(End / OpBytesPerElement - 1);
61040b57cec5SDimitry Andric       if (!Op.getValueType().isInteger()) {
61050b57cec5SDimitry Andric         EVT VT = MVT::getIntegerVT(Op.getValueSizeInBits());
61060b57cec5SDimitry Andric         Op = DAG.getNode(ISD::BITCAST, DL, VT, Op);
61070b57cec5SDimitry Andric         DCI.AddToWorklist(Op.getNode());
61080b57cec5SDimitry Andric       }
61090b57cec5SDimitry Andric       EVT VT = MVT::getIntegerVT(ResVT.getSizeInBits());
61100b57cec5SDimitry Andric       Op = DAG.getNode(ISD::TRUNCATE, DL, VT, Op);
61110b57cec5SDimitry Andric       if (VT != ResVT) {
61120b57cec5SDimitry Andric         DCI.AddToWorklist(Op.getNode());
61130b57cec5SDimitry Andric         Op = DAG.getNode(ISD::BITCAST, DL, ResVT, Op);
61140b57cec5SDimitry Andric       }
61150b57cec5SDimitry Andric       return Op;
61160b57cec5SDimitry Andric     } else if ((Opcode == ISD::SIGN_EXTEND_VECTOR_INREG ||
61170b57cec5SDimitry Andric                 Opcode == ISD::ZERO_EXTEND_VECTOR_INREG ||
61180b57cec5SDimitry Andric                 Opcode == ISD::ANY_EXTEND_VECTOR_INREG) &&
61190b57cec5SDimitry Andric                canTreatAsByteVector(Op.getValueType()) &&
61200b57cec5SDimitry Andric                canTreatAsByteVector(Op.getOperand(0).getValueType())) {
61210b57cec5SDimitry Andric       // Make sure that only the unextended bits are significant.
61220b57cec5SDimitry Andric       EVT ExtVT = Op.getValueType();
61230b57cec5SDimitry Andric       EVT OpVT = Op.getOperand(0).getValueType();
61240b57cec5SDimitry Andric       unsigned ExtBytesPerElement = ExtVT.getVectorElementType().getStoreSize();
61250b57cec5SDimitry Andric       unsigned OpBytesPerElement = OpVT.getVectorElementType().getStoreSize();
61260b57cec5SDimitry Andric       unsigned Byte = Index * BytesPerElement;
61270b57cec5SDimitry Andric       unsigned SubByte = Byte % ExtBytesPerElement;
61280b57cec5SDimitry Andric       unsigned MinSubByte = ExtBytesPerElement - OpBytesPerElement;
61290b57cec5SDimitry Andric       if (SubByte < MinSubByte ||
61300b57cec5SDimitry Andric           SubByte + BytesPerElement > ExtBytesPerElement)
61310b57cec5SDimitry Andric         break;
61320b57cec5SDimitry Andric       // Get the byte offset of the unextended element
61330b57cec5SDimitry Andric       Byte = Byte / ExtBytesPerElement * OpBytesPerElement;
61340b57cec5SDimitry Andric       // ...then add the byte offset relative to that element.
61350b57cec5SDimitry Andric       Byte += SubByte - MinSubByte;
61360b57cec5SDimitry Andric       if (Byte % BytesPerElement != 0)
61370b57cec5SDimitry Andric         break;
61380b57cec5SDimitry Andric       Op = Op.getOperand(0);
61390b57cec5SDimitry Andric       Index = Byte / BytesPerElement;
61400b57cec5SDimitry Andric       Force = true;
61410b57cec5SDimitry Andric     } else
61420b57cec5SDimitry Andric       break;
61430b57cec5SDimitry Andric   }
61440b57cec5SDimitry Andric   if (Force) {
61450b57cec5SDimitry Andric     if (Op.getValueType() != VecVT) {
61460b57cec5SDimitry Andric       Op = DAG.getNode(ISD::BITCAST, DL, VecVT, Op);
61470b57cec5SDimitry Andric       DCI.AddToWorklist(Op.getNode());
61480b57cec5SDimitry Andric     }
61490b57cec5SDimitry Andric     return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, ResVT, Op,
61500b57cec5SDimitry Andric                        DAG.getConstant(Index, DL, MVT::i32));
61510b57cec5SDimitry Andric   }
61520b57cec5SDimitry Andric   return SDValue();
61530b57cec5SDimitry Andric }
61540b57cec5SDimitry Andric 
61550b57cec5SDimitry Andric // Optimize vector operations in scalar value Op on the basis that Op
61560b57cec5SDimitry Andric // is truncated to TruncVT.
61570b57cec5SDimitry Andric SDValue SystemZTargetLowering::combineTruncateExtract(
61580b57cec5SDimitry Andric     const SDLoc &DL, EVT TruncVT, SDValue Op, DAGCombinerInfo &DCI) const {
61590b57cec5SDimitry Andric   // If we have (trunc (extract_vector_elt X, Y)), try to turn it into
61600b57cec5SDimitry Andric   // (extract_vector_elt (bitcast X), Y'), where (bitcast X) has elements
61610b57cec5SDimitry Andric   // of type TruncVT.
61620b57cec5SDimitry Andric   if (Op.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
61630b57cec5SDimitry Andric       TruncVT.getSizeInBits() % 8 == 0) {
61640b57cec5SDimitry Andric     SDValue Vec = Op.getOperand(0);
61650b57cec5SDimitry Andric     EVT VecVT = Vec.getValueType();
61660b57cec5SDimitry Andric     if (canTreatAsByteVector(VecVT)) {
61670b57cec5SDimitry Andric       if (auto *IndexN = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
61680b57cec5SDimitry Andric         unsigned BytesPerElement = VecVT.getVectorElementType().getStoreSize();
61690b57cec5SDimitry Andric         unsigned TruncBytes = TruncVT.getStoreSize();
61700b57cec5SDimitry Andric         if (BytesPerElement % TruncBytes == 0) {
61710b57cec5SDimitry Andric           // Calculate the value of Y' in the above description.  We are
61720b57cec5SDimitry Andric           // splitting the original elements into Scale equal-sized pieces
61730b57cec5SDimitry Andric           // and for truncation purposes want the last (least-significant)
61740b57cec5SDimitry Andric           // of these pieces for IndexN.  This is easiest to do by calculating
61750b57cec5SDimitry Andric           // the start index of the following element and then subtracting 1.
61760b57cec5SDimitry Andric           unsigned Scale = BytesPerElement / TruncBytes;
61770b57cec5SDimitry Andric           unsigned NewIndex = (IndexN->getZExtValue() + 1) * Scale - 1;
61780b57cec5SDimitry Andric 
61790b57cec5SDimitry Andric           // Defer the creation of the bitcast from X to combineExtract,
61800b57cec5SDimitry Andric           // which might be able to optimize the extraction.
61810b57cec5SDimitry Andric           VecVT = MVT::getVectorVT(MVT::getIntegerVT(TruncBytes * 8),
61820b57cec5SDimitry Andric                                    VecVT.getStoreSize() / TruncBytes);
61830b57cec5SDimitry Andric           EVT ResVT = (TruncBytes < 4 ? MVT::i32 : TruncVT);
61840b57cec5SDimitry Andric           return combineExtract(DL, ResVT, VecVT, Vec, NewIndex, DCI, true);
61850b57cec5SDimitry Andric         }
61860b57cec5SDimitry Andric       }
61870b57cec5SDimitry Andric     }
61880b57cec5SDimitry Andric   }
61890b57cec5SDimitry Andric   return SDValue();
61900b57cec5SDimitry Andric }
61910b57cec5SDimitry Andric 
61920b57cec5SDimitry Andric SDValue SystemZTargetLowering::combineZERO_EXTEND(
61930b57cec5SDimitry Andric     SDNode *N, DAGCombinerInfo &DCI) const {
61940b57cec5SDimitry Andric   // Convert (zext (select_ccmask C1, C2)) into (select_ccmask C1', C2')
61950b57cec5SDimitry Andric   SelectionDAG &DAG = DCI.DAG;
61960b57cec5SDimitry Andric   SDValue N0 = N->getOperand(0);
61970b57cec5SDimitry Andric   EVT VT = N->getValueType(0);
61980b57cec5SDimitry Andric   if (N0.getOpcode() == SystemZISD::SELECT_CCMASK) {
61990b57cec5SDimitry Andric     auto *TrueOp = dyn_cast<ConstantSDNode>(N0.getOperand(0));
62000b57cec5SDimitry Andric     auto *FalseOp = dyn_cast<ConstantSDNode>(N0.getOperand(1));
62010b57cec5SDimitry Andric     if (TrueOp && FalseOp) {
62020b57cec5SDimitry Andric       SDLoc DL(N0);
62030b57cec5SDimitry Andric       SDValue Ops[] = { DAG.getConstant(TrueOp->getZExtValue(), DL, VT),
62040b57cec5SDimitry Andric                         DAG.getConstant(FalseOp->getZExtValue(), DL, VT),
62050b57cec5SDimitry Andric                         N0.getOperand(2), N0.getOperand(3), N0.getOperand(4) };
62060b57cec5SDimitry Andric       SDValue NewSelect = DAG.getNode(SystemZISD::SELECT_CCMASK, DL, VT, Ops);
62070b57cec5SDimitry Andric       // If N0 has multiple uses, change other uses as well.
62080b57cec5SDimitry Andric       if (!N0.hasOneUse()) {
62090b57cec5SDimitry Andric         SDValue TruncSelect =
62100b57cec5SDimitry Andric           DAG.getNode(ISD::TRUNCATE, DL, N0.getValueType(), NewSelect);
62110b57cec5SDimitry Andric         DCI.CombineTo(N0.getNode(), TruncSelect);
62120b57cec5SDimitry Andric       }
62130b57cec5SDimitry Andric       return NewSelect;
62140b57cec5SDimitry Andric     }
62150b57cec5SDimitry Andric   }
62160b57cec5SDimitry Andric   return SDValue();
62170b57cec5SDimitry Andric }
62180b57cec5SDimitry Andric 
62190b57cec5SDimitry Andric SDValue SystemZTargetLowering::combineSIGN_EXTEND_INREG(
62200b57cec5SDimitry Andric     SDNode *N, DAGCombinerInfo &DCI) const {
62210b57cec5SDimitry Andric   // Convert (sext_in_reg (setcc LHS, RHS, COND), i1)
62220b57cec5SDimitry Andric   // and (sext_in_reg (any_extend (setcc LHS, RHS, COND)), i1)
62230b57cec5SDimitry Andric   // into (select_cc LHS, RHS, -1, 0, COND)
62240b57cec5SDimitry Andric   SelectionDAG &DAG = DCI.DAG;
62250b57cec5SDimitry Andric   SDValue N0 = N->getOperand(0);
62260b57cec5SDimitry Andric   EVT VT = N->getValueType(0);
62270b57cec5SDimitry Andric   EVT EVT = cast<VTSDNode>(N->getOperand(1))->getVT();
62280b57cec5SDimitry Andric   if (N0.hasOneUse() && N0.getOpcode() == ISD::ANY_EXTEND)
62290b57cec5SDimitry Andric     N0 = N0.getOperand(0);
62300b57cec5SDimitry Andric   if (EVT == MVT::i1 && N0.hasOneUse() && N0.getOpcode() == ISD::SETCC) {
62310b57cec5SDimitry Andric     SDLoc DL(N0);
62320b57cec5SDimitry Andric     SDValue Ops[] = { N0.getOperand(0), N0.getOperand(1),
62330b57cec5SDimitry Andric                       DAG.getConstant(-1, DL, VT), DAG.getConstant(0, DL, VT),
62340b57cec5SDimitry Andric                       N0.getOperand(2) };
62350b57cec5SDimitry Andric     return DAG.getNode(ISD::SELECT_CC, DL, VT, Ops);
62360b57cec5SDimitry Andric   }
62370b57cec5SDimitry Andric   return SDValue();
62380b57cec5SDimitry Andric }
62390b57cec5SDimitry Andric 
62400b57cec5SDimitry Andric SDValue SystemZTargetLowering::combineSIGN_EXTEND(
62410b57cec5SDimitry Andric     SDNode *N, DAGCombinerInfo &DCI) const {
62420b57cec5SDimitry Andric   // Convert (sext (ashr (shl X, C1), C2)) to
62430b57cec5SDimitry Andric   // (ashr (shl (anyext X), C1'), C2')), since wider shifts are as
62440b57cec5SDimitry Andric   // cheap as narrower ones.
62450b57cec5SDimitry Andric   SelectionDAG &DAG = DCI.DAG;
62460b57cec5SDimitry Andric   SDValue N0 = N->getOperand(0);
62470b57cec5SDimitry Andric   EVT VT = N->getValueType(0);
62480b57cec5SDimitry Andric   if (N0.hasOneUse() && N0.getOpcode() == ISD::SRA) {
62490b57cec5SDimitry Andric     auto *SraAmt = dyn_cast<ConstantSDNode>(N0.getOperand(1));
62500b57cec5SDimitry Andric     SDValue Inner = N0.getOperand(0);
62510b57cec5SDimitry Andric     if (SraAmt && Inner.hasOneUse() && Inner.getOpcode() == ISD::SHL) {
62520b57cec5SDimitry Andric       if (auto *ShlAmt = dyn_cast<ConstantSDNode>(Inner.getOperand(1))) {
62530b57cec5SDimitry Andric         unsigned Extra = (VT.getSizeInBits() - N0.getValueSizeInBits());
62540b57cec5SDimitry Andric         unsigned NewShlAmt = ShlAmt->getZExtValue() + Extra;
62550b57cec5SDimitry Andric         unsigned NewSraAmt = SraAmt->getZExtValue() + Extra;
62560b57cec5SDimitry Andric         EVT ShiftVT = N0.getOperand(1).getValueType();
62570b57cec5SDimitry Andric         SDValue Ext = DAG.getNode(ISD::ANY_EXTEND, SDLoc(Inner), VT,
62580b57cec5SDimitry Andric                                   Inner.getOperand(0));
62590b57cec5SDimitry Andric         SDValue Shl = DAG.getNode(ISD::SHL, SDLoc(Inner), VT, Ext,
62600b57cec5SDimitry Andric                                   DAG.getConstant(NewShlAmt, SDLoc(Inner),
62610b57cec5SDimitry Andric                                                   ShiftVT));
62620b57cec5SDimitry Andric         return DAG.getNode(ISD::SRA, SDLoc(N0), VT, Shl,
62630b57cec5SDimitry Andric                            DAG.getConstant(NewSraAmt, SDLoc(N0), ShiftVT));
62640b57cec5SDimitry Andric       }
62650b57cec5SDimitry Andric     }
62660b57cec5SDimitry Andric   }
62670b57cec5SDimitry Andric   return SDValue();
62680b57cec5SDimitry Andric }
62690b57cec5SDimitry Andric 
62700b57cec5SDimitry Andric SDValue SystemZTargetLowering::combineMERGE(
62710b57cec5SDimitry Andric     SDNode *N, DAGCombinerInfo &DCI) const {
62720b57cec5SDimitry Andric   SelectionDAG &DAG = DCI.DAG;
62730b57cec5SDimitry Andric   unsigned Opcode = N->getOpcode();
62740b57cec5SDimitry Andric   SDValue Op0 = N->getOperand(0);
62750b57cec5SDimitry Andric   SDValue Op1 = N->getOperand(1);
62760b57cec5SDimitry Andric   if (Op0.getOpcode() == ISD::BITCAST)
62770b57cec5SDimitry Andric     Op0 = Op0.getOperand(0);
62780b57cec5SDimitry Andric   if (ISD::isBuildVectorAllZeros(Op0.getNode())) {
62790b57cec5SDimitry Andric     // (z_merge_* 0, 0) -> 0.  This is mostly useful for using VLLEZF
62800b57cec5SDimitry Andric     // for v4f32.
62810b57cec5SDimitry Andric     if (Op1 == N->getOperand(0))
62820b57cec5SDimitry Andric       return Op1;
62830b57cec5SDimitry Andric     // (z_merge_? 0, X) -> (z_unpackl_? 0, X).
62840b57cec5SDimitry Andric     EVT VT = Op1.getValueType();
62850b57cec5SDimitry Andric     unsigned ElemBytes = VT.getVectorElementType().getStoreSize();
62860b57cec5SDimitry Andric     if (ElemBytes <= 4) {
62870b57cec5SDimitry Andric       Opcode = (Opcode == SystemZISD::MERGE_HIGH ?
62880b57cec5SDimitry Andric                 SystemZISD::UNPACKL_HIGH : SystemZISD::UNPACKL_LOW);
62890b57cec5SDimitry Andric       EVT InVT = VT.changeVectorElementTypeToInteger();
62900b57cec5SDimitry Andric       EVT OutVT = MVT::getVectorVT(MVT::getIntegerVT(ElemBytes * 16),
62910b57cec5SDimitry Andric                                    SystemZ::VectorBytes / ElemBytes / 2);
62920b57cec5SDimitry Andric       if (VT != InVT) {
62930b57cec5SDimitry Andric         Op1 = DAG.getNode(ISD::BITCAST, SDLoc(N), InVT, Op1);
62940b57cec5SDimitry Andric         DCI.AddToWorklist(Op1.getNode());
62950b57cec5SDimitry Andric       }
62960b57cec5SDimitry Andric       SDValue Op = DAG.getNode(Opcode, SDLoc(N), OutVT, Op1);
62970b57cec5SDimitry Andric       DCI.AddToWorklist(Op.getNode());
62980b57cec5SDimitry Andric       return DAG.getNode(ISD::BITCAST, SDLoc(N), VT, Op);
62990b57cec5SDimitry Andric     }
63000b57cec5SDimitry Andric   }
63010b57cec5SDimitry Andric   return SDValue();
63020b57cec5SDimitry Andric }
63030b57cec5SDimitry Andric 
63040b57cec5SDimitry Andric SDValue SystemZTargetLowering::combineLOAD(
63050b57cec5SDimitry Andric     SDNode *N, DAGCombinerInfo &DCI) const {
63060b57cec5SDimitry Andric   SelectionDAG &DAG = DCI.DAG;
63070b57cec5SDimitry Andric   EVT LdVT = N->getValueType(0);
63080b57cec5SDimitry Andric   if (LdVT.isVector() || LdVT.isInteger())
63090b57cec5SDimitry Andric     return SDValue();
63100b57cec5SDimitry Andric   // Transform a scalar load that is REPLICATEd as well as having other
63110b57cec5SDimitry Andric   // use(s) to the form where the other use(s) use the first element of the
63120b57cec5SDimitry Andric   // REPLICATE instead of the load. Otherwise instruction selection will not
63130b57cec5SDimitry Andric   // produce a VLREP. Avoid extracting to a GPR, so only do this for floating
63140b57cec5SDimitry Andric   // point loads.
63150b57cec5SDimitry Andric 
63160b57cec5SDimitry Andric   SDValue Replicate;
63170b57cec5SDimitry Andric   SmallVector<SDNode*, 8> OtherUses;
63180b57cec5SDimitry Andric   for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
63190b57cec5SDimitry Andric        UI != UE; ++UI) {
63200b57cec5SDimitry Andric     if (UI->getOpcode() == SystemZISD::REPLICATE) {
63210b57cec5SDimitry Andric       if (Replicate)
63220b57cec5SDimitry Andric         return SDValue(); // Should never happen
63230b57cec5SDimitry Andric       Replicate = SDValue(*UI, 0);
63240b57cec5SDimitry Andric     }
63250b57cec5SDimitry Andric     else if (UI.getUse().getResNo() == 0)
63260b57cec5SDimitry Andric       OtherUses.push_back(*UI);
63270b57cec5SDimitry Andric   }
63280b57cec5SDimitry Andric   if (!Replicate || OtherUses.empty())
63290b57cec5SDimitry Andric     return SDValue();
63300b57cec5SDimitry Andric 
63310b57cec5SDimitry Andric   SDLoc DL(N);
63320b57cec5SDimitry Andric   SDValue Extract0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, LdVT,
63330b57cec5SDimitry Andric                               Replicate, DAG.getConstant(0, DL, MVT::i32));
63340b57cec5SDimitry Andric   // Update uses of the loaded Value while preserving old chains.
63350b57cec5SDimitry Andric   for (SDNode *U : OtherUses) {
63360b57cec5SDimitry Andric     SmallVector<SDValue, 8> Ops;
63370b57cec5SDimitry Andric     for (SDValue Op : U->ops())
63380b57cec5SDimitry Andric       Ops.push_back((Op.getNode() == N && Op.getResNo() == 0) ? Extract0 : Op);
63390b57cec5SDimitry Andric     DAG.UpdateNodeOperands(U, Ops);
63400b57cec5SDimitry Andric   }
63410b57cec5SDimitry Andric   return SDValue(N, 0);
63420b57cec5SDimitry Andric }
63430b57cec5SDimitry Andric 
63440b57cec5SDimitry Andric bool SystemZTargetLowering::canLoadStoreByteSwapped(EVT VT) const {
63450b57cec5SDimitry Andric   if (VT == MVT::i16 || VT == MVT::i32 || VT == MVT::i64)
63460b57cec5SDimitry Andric     return true;
63470b57cec5SDimitry Andric   if (Subtarget.hasVectorEnhancements2())
63480b57cec5SDimitry Andric     if (VT == MVT::v8i16 || VT == MVT::v4i32 || VT == MVT::v2i64)
63490b57cec5SDimitry Andric       return true;
63500b57cec5SDimitry Andric   return false;
63510b57cec5SDimitry Andric }
63520b57cec5SDimitry Andric 
63530b57cec5SDimitry Andric static bool isVectorElementSwap(ArrayRef<int> M, EVT VT) {
63540b57cec5SDimitry Andric   if (!VT.isVector() || !VT.isSimple() ||
63550b57cec5SDimitry Andric       VT.getSizeInBits() != 128 ||
63560b57cec5SDimitry Andric       VT.getScalarSizeInBits() % 8 != 0)
63570b57cec5SDimitry Andric     return false;
63580b57cec5SDimitry Andric 
63590b57cec5SDimitry Andric   unsigned NumElts = VT.getVectorNumElements();
63600b57cec5SDimitry Andric   for (unsigned i = 0; i < NumElts; ++i) {
63610b57cec5SDimitry Andric     if (M[i] < 0) continue; // ignore UNDEF indices
63620b57cec5SDimitry Andric     if ((unsigned) M[i] != NumElts - 1 - i)
63630b57cec5SDimitry Andric       return false;
63640b57cec5SDimitry Andric   }
63650b57cec5SDimitry Andric 
63660b57cec5SDimitry Andric   return true;
63670b57cec5SDimitry Andric }
63680b57cec5SDimitry Andric 
636981ad6265SDimitry Andric static bool isOnlyUsedByStores(SDValue StoredVal, SelectionDAG &DAG) {
637081ad6265SDimitry Andric   for (auto *U : StoredVal->uses()) {
637181ad6265SDimitry Andric     if (StoreSDNode *ST = dyn_cast<StoreSDNode>(U)) {
637281ad6265SDimitry Andric       EVT CurrMemVT = ST->getMemoryVT().getScalarType();
637381ad6265SDimitry Andric       if (CurrMemVT.isRound() && CurrMemVT.getStoreSize() <= 16)
637481ad6265SDimitry Andric         continue;
637581ad6265SDimitry Andric     } else if (isa<BuildVectorSDNode>(U)) {
637681ad6265SDimitry Andric       SDValue BuildVector = SDValue(U, 0);
637781ad6265SDimitry Andric       if (DAG.isSplatValue(BuildVector, true/*AllowUndefs*/) &&
637881ad6265SDimitry Andric           isOnlyUsedByStores(BuildVector, DAG))
637981ad6265SDimitry Andric         continue;
638081ad6265SDimitry Andric     }
638181ad6265SDimitry Andric     return false;
638281ad6265SDimitry Andric   }
638381ad6265SDimitry Andric   return true;
638481ad6265SDimitry Andric }
638581ad6265SDimitry Andric 
63860b57cec5SDimitry Andric SDValue SystemZTargetLowering::combineSTORE(
63870b57cec5SDimitry Andric     SDNode *N, DAGCombinerInfo &DCI) const {
63880b57cec5SDimitry Andric   SelectionDAG &DAG = DCI.DAG;
63890b57cec5SDimitry Andric   auto *SN = cast<StoreSDNode>(N);
63900b57cec5SDimitry Andric   auto &Op1 = N->getOperand(1);
63910b57cec5SDimitry Andric   EVT MemVT = SN->getMemoryVT();
63920b57cec5SDimitry Andric   // If we have (truncstoreiN (extract_vector_elt X, Y), Z) then it is better
63930b57cec5SDimitry Andric   // for the extraction to be done on a vMiN value, so that we can use VSTE.
63940b57cec5SDimitry Andric   // If X has wider elements then convert it to:
63950b57cec5SDimitry Andric   // (truncstoreiN (extract_vector_elt (bitcast X), Y2), Z).
63960b57cec5SDimitry Andric   if (MemVT.isInteger() && SN->isTruncatingStore()) {
63970b57cec5SDimitry Andric     if (SDValue Value =
63980b57cec5SDimitry Andric             combineTruncateExtract(SDLoc(N), MemVT, SN->getValue(), DCI)) {
63990b57cec5SDimitry Andric       DCI.AddToWorklist(Value.getNode());
64000b57cec5SDimitry Andric 
64010b57cec5SDimitry Andric       // Rewrite the store with the new form of stored value.
64020b57cec5SDimitry Andric       return DAG.getTruncStore(SN->getChain(), SDLoc(SN), Value,
64030b57cec5SDimitry Andric                                SN->getBasePtr(), SN->getMemoryVT(),
64040b57cec5SDimitry Andric                                SN->getMemOperand());
64050b57cec5SDimitry Andric     }
64060b57cec5SDimitry Andric   }
64070b57cec5SDimitry Andric   // Combine STORE (BSWAP) into STRVH/STRV/STRVG/VSTBR
64080b57cec5SDimitry Andric   if (!SN->isTruncatingStore() &&
64090b57cec5SDimitry Andric       Op1.getOpcode() == ISD::BSWAP &&
64100b57cec5SDimitry Andric       Op1.getNode()->hasOneUse() &&
64110b57cec5SDimitry Andric       canLoadStoreByteSwapped(Op1.getValueType())) {
64120b57cec5SDimitry Andric 
64130b57cec5SDimitry Andric       SDValue BSwapOp = Op1.getOperand(0);
64140b57cec5SDimitry Andric 
64150b57cec5SDimitry Andric       if (BSwapOp.getValueType() == MVT::i16)
64160b57cec5SDimitry Andric         BSwapOp = DAG.getNode(ISD::ANY_EXTEND, SDLoc(N), MVT::i32, BSwapOp);
64170b57cec5SDimitry Andric 
64180b57cec5SDimitry Andric       SDValue Ops[] = {
64190b57cec5SDimitry Andric         N->getOperand(0), BSwapOp, N->getOperand(2)
64200b57cec5SDimitry Andric       };
64210b57cec5SDimitry Andric 
64220b57cec5SDimitry Andric       return
64230b57cec5SDimitry Andric         DAG.getMemIntrinsicNode(SystemZISD::STRV, SDLoc(N), DAG.getVTList(MVT::Other),
64240b57cec5SDimitry Andric                                 Ops, MemVT, SN->getMemOperand());
64250b57cec5SDimitry Andric     }
64260b57cec5SDimitry Andric   // Combine STORE (element-swap) into VSTER
64270b57cec5SDimitry Andric   if (!SN->isTruncatingStore() &&
64280b57cec5SDimitry Andric       Op1.getOpcode() == ISD::VECTOR_SHUFFLE &&
64290b57cec5SDimitry Andric       Op1.getNode()->hasOneUse() &&
64300b57cec5SDimitry Andric       Subtarget.hasVectorEnhancements2()) {
64310b57cec5SDimitry Andric     ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(Op1.getNode());
64320b57cec5SDimitry Andric     ArrayRef<int> ShuffleMask = SVN->getMask();
64330b57cec5SDimitry Andric     if (isVectorElementSwap(ShuffleMask, Op1.getValueType())) {
64340b57cec5SDimitry Andric       SDValue Ops[] = {
64350b57cec5SDimitry Andric         N->getOperand(0), Op1.getOperand(0), N->getOperand(2)
64360b57cec5SDimitry Andric       };
64370b57cec5SDimitry Andric 
64380b57cec5SDimitry Andric       return DAG.getMemIntrinsicNode(SystemZISD::VSTER, SDLoc(N),
64390b57cec5SDimitry Andric                                      DAG.getVTList(MVT::Other),
64400b57cec5SDimitry Andric                                      Ops, MemVT, SN->getMemOperand());
64410b57cec5SDimitry Andric     }
64420b57cec5SDimitry Andric   }
64430b57cec5SDimitry Andric 
644481ad6265SDimitry Andric   // Replicate a reg or immediate with VREP instead of scalar multiply or
644581ad6265SDimitry Andric   // immediate load. It seems best to do this during the first DAGCombine as
644681ad6265SDimitry Andric   // it is straight-forward to handle the zero-extend node in the initial
644781ad6265SDimitry Andric   // DAG, and also not worry about the keeping the new MemVT legal (e.g. when
644881ad6265SDimitry Andric   // extracting an i16 element from a v16i8 vector).
644981ad6265SDimitry Andric   if (Subtarget.hasVector() && DCI.Level == BeforeLegalizeTypes &&
645081ad6265SDimitry Andric       isOnlyUsedByStores(Op1, DAG)) {
645181ad6265SDimitry Andric     SDValue Word = SDValue();
645281ad6265SDimitry Andric     EVT WordVT;
645381ad6265SDimitry Andric 
645481ad6265SDimitry Andric     // Find a replicated immediate and return it if found in Word and its
645581ad6265SDimitry Andric     // type in WordVT.
645681ad6265SDimitry Andric     auto FindReplicatedImm = [&](ConstantSDNode *C, unsigned TotBytes) {
645781ad6265SDimitry Andric       // Some constants are better handled with a scalar store.
645881ad6265SDimitry Andric       if (C->getAPIntValue().getBitWidth() > 64 || C->isAllOnes() ||
645981ad6265SDimitry Andric           isInt<16>(C->getSExtValue()) || MemVT.getStoreSize() <= 2)
646081ad6265SDimitry Andric         return;
646181ad6265SDimitry Andric       SystemZVectorConstantInfo VCI(APInt(TotBytes * 8, C->getZExtValue()));
646281ad6265SDimitry Andric       if (VCI.isVectorConstantLegal(Subtarget) &&
646381ad6265SDimitry Andric           VCI.Opcode == SystemZISD::REPLICATE) {
646481ad6265SDimitry Andric         Word = DAG.getConstant(VCI.OpVals[0], SDLoc(SN), MVT::i32);
646581ad6265SDimitry Andric         WordVT = VCI.VecVT.getScalarType();
646681ad6265SDimitry Andric       }
646781ad6265SDimitry Andric     };
646881ad6265SDimitry Andric 
646981ad6265SDimitry Andric     // Find a replicated register and return it if found in Word and its type
647081ad6265SDimitry Andric     // in WordVT.
647181ad6265SDimitry Andric     auto FindReplicatedReg = [&](SDValue MulOp) {
647281ad6265SDimitry Andric       EVT MulVT = MulOp.getValueType();
647381ad6265SDimitry Andric       if (MulOp->getOpcode() == ISD::MUL &&
647481ad6265SDimitry Andric           (MulVT == MVT::i16 || MulVT == MVT::i32 || MulVT == MVT::i64)) {
647581ad6265SDimitry Andric         // Find a zero extended value and its type.
647681ad6265SDimitry Andric         SDValue LHS = MulOp->getOperand(0);
647781ad6265SDimitry Andric         if (LHS->getOpcode() == ISD::ZERO_EXTEND)
647881ad6265SDimitry Andric           WordVT = LHS->getOperand(0).getValueType();
647981ad6265SDimitry Andric         else if (LHS->getOpcode() == ISD::AssertZext)
648081ad6265SDimitry Andric           WordVT = cast<VTSDNode>(LHS->getOperand(1))->getVT();
648181ad6265SDimitry Andric         else
648281ad6265SDimitry Andric           return;
648381ad6265SDimitry Andric         // Find a replicating constant, e.g. 0x00010001.
648481ad6265SDimitry Andric         if (auto *C = dyn_cast<ConstantSDNode>(MulOp->getOperand(1))) {
648581ad6265SDimitry Andric           SystemZVectorConstantInfo VCI(
648681ad6265SDimitry Andric               APInt(MulVT.getSizeInBits(), C->getZExtValue()));
648781ad6265SDimitry Andric           if (VCI.isVectorConstantLegal(Subtarget) &&
648881ad6265SDimitry Andric               VCI.Opcode == SystemZISD::REPLICATE && VCI.OpVals[0] == 1 &&
648981ad6265SDimitry Andric               WordVT == VCI.VecVT.getScalarType())
649081ad6265SDimitry Andric             Word = DAG.getZExtOrTrunc(LHS->getOperand(0), SDLoc(SN), WordVT);
649181ad6265SDimitry Andric         }
649281ad6265SDimitry Andric       }
649381ad6265SDimitry Andric     };
649481ad6265SDimitry Andric 
649581ad6265SDimitry Andric     if (isa<BuildVectorSDNode>(Op1) &&
649681ad6265SDimitry Andric         DAG.isSplatValue(Op1, true/*AllowUndefs*/)) {
649781ad6265SDimitry Andric       SDValue SplatVal = Op1->getOperand(0);
649881ad6265SDimitry Andric       if (auto *C = dyn_cast<ConstantSDNode>(SplatVal))
649981ad6265SDimitry Andric         FindReplicatedImm(C, SplatVal.getValueType().getStoreSize());
650081ad6265SDimitry Andric       else
650181ad6265SDimitry Andric         FindReplicatedReg(SplatVal);
650281ad6265SDimitry Andric     } else {
650381ad6265SDimitry Andric       if (auto *C = dyn_cast<ConstantSDNode>(Op1))
650481ad6265SDimitry Andric         FindReplicatedImm(C, MemVT.getStoreSize());
650581ad6265SDimitry Andric       else
650681ad6265SDimitry Andric         FindReplicatedReg(Op1);
650781ad6265SDimitry Andric     }
650881ad6265SDimitry Andric 
650981ad6265SDimitry Andric     if (Word != SDValue()) {
651081ad6265SDimitry Andric       assert(MemVT.getSizeInBits() % WordVT.getSizeInBits() == 0 &&
651181ad6265SDimitry Andric              "Bad type handling");
651281ad6265SDimitry Andric       unsigned NumElts = MemVT.getSizeInBits() / WordVT.getSizeInBits();
651381ad6265SDimitry Andric       EVT SplatVT = EVT::getVectorVT(*DAG.getContext(), WordVT, NumElts);
651481ad6265SDimitry Andric       SDValue SplatVal = DAG.getSplatVector(SplatVT, SDLoc(SN), Word);
651581ad6265SDimitry Andric       return DAG.getStore(SN->getChain(), SDLoc(SN), SplatVal,
651681ad6265SDimitry Andric                           SN->getBasePtr(), SN->getMemOperand());
651781ad6265SDimitry Andric     }
651881ad6265SDimitry Andric   }
651981ad6265SDimitry Andric 
65200b57cec5SDimitry Andric   return SDValue();
65210b57cec5SDimitry Andric }
65220b57cec5SDimitry Andric 
65230b57cec5SDimitry Andric SDValue SystemZTargetLowering::combineVECTOR_SHUFFLE(
65240b57cec5SDimitry Andric     SDNode *N, DAGCombinerInfo &DCI) const {
65250b57cec5SDimitry Andric   SelectionDAG &DAG = DCI.DAG;
65260b57cec5SDimitry Andric   // Combine element-swap (LOAD) into VLER
65270b57cec5SDimitry Andric   if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
65280b57cec5SDimitry Andric       N->getOperand(0).hasOneUse() &&
65290b57cec5SDimitry Andric       Subtarget.hasVectorEnhancements2()) {
65300b57cec5SDimitry Andric     ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
65310b57cec5SDimitry Andric     ArrayRef<int> ShuffleMask = SVN->getMask();
65320b57cec5SDimitry Andric     if (isVectorElementSwap(ShuffleMask, N->getValueType(0))) {
65330b57cec5SDimitry Andric       SDValue Load = N->getOperand(0);
65340b57cec5SDimitry Andric       LoadSDNode *LD = cast<LoadSDNode>(Load);
65350b57cec5SDimitry Andric 
65360b57cec5SDimitry Andric       // Create the element-swapping load.
65370b57cec5SDimitry Andric       SDValue Ops[] = {
65380b57cec5SDimitry Andric         LD->getChain(),    // Chain
65390b57cec5SDimitry Andric         LD->getBasePtr()   // Ptr
65400b57cec5SDimitry Andric       };
65410b57cec5SDimitry Andric       SDValue ESLoad =
65420b57cec5SDimitry Andric         DAG.getMemIntrinsicNode(SystemZISD::VLER, SDLoc(N),
65430b57cec5SDimitry Andric                                 DAG.getVTList(LD->getValueType(0), MVT::Other),
65440b57cec5SDimitry Andric                                 Ops, LD->getMemoryVT(), LD->getMemOperand());
65450b57cec5SDimitry Andric 
65460b57cec5SDimitry Andric       // First, combine the VECTOR_SHUFFLE away.  This makes the value produced
65470b57cec5SDimitry Andric       // by the load dead.
65480b57cec5SDimitry Andric       DCI.CombineTo(N, ESLoad);
65490b57cec5SDimitry Andric 
65500b57cec5SDimitry Andric       // Next, combine the load away, we give it a bogus result value but a real
65510b57cec5SDimitry Andric       // chain result.  The result value is dead because the shuffle is dead.
65520b57cec5SDimitry Andric       DCI.CombineTo(Load.getNode(), ESLoad, ESLoad.getValue(1));
65530b57cec5SDimitry Andric 
65540b57cec5SDimitry Andric       // Return N so it doesn't get rechecked!
65550b57cec5SDimitry Andric       return SDValue(N, 0);
65560b57cec5SDimitry Andric     }
65570b57cec5SDimitry Andric   }
65580b57cec5SDimitry Andric 
65590b57cec5SDimitry Andric   return SDValue();
65600b57cec5SDimitry Andric }
65610b57cec5SDimitry Andric 
65620b57cec5SDimitry Andric SDValue SystemZTargetLowering::combineEXTRACT_VECTOR_ELT(
65630b57cec5SDimitry Andric     SDNode *N, DAGCombinerInfo &DCI) const {
65640b57cec5SDimitry Andric   SelectionDAG &DAG = DCI.DAG;
65650b57cec5SDimitry Andric 
65660b57cec5SDimitry Andric   if (!Subtarget.hasVector())
65670b57cec5SDimitry Andric     return SDValue();
65680b57cec5SDimitry Andric 
65690b57cec5SDimitry Andric   // Look through bitcasts that retain the number of vector elements.
65700b57cec5SDimitry Andric   SDValue Op = N->getOperand(0);
65710b57cec5SDimitry Andric   if (Op.getOpcode() == ISD::BITCAST &&
65720b57cec5SDimitry Andric       Op.getValueType().isVector() &&
65730b57cec5SDimitry Andric       Op.getOperand(0).getValueType().isVector() &&
65740b57cec5SDimitry Andric       Op.getValueType().getVectorNumElements() ==
65750b57cec5SDimitry Andric       Op.getOperand(0).getValueType().getVectorNumElements())
65760b57cec5SDimitry Andric     Op = Op.getOperand(0);
65770b57cec5SDimitry Andric 
65780b57cec5SDimitry Andric   // Pull BSWAP out of a vector extraction.
65790b57cec5SDimitry Andric   if (Op.getOpcode() == ISD::BSWAP && Op.hasOneUse()) {
65800b57cec5SDimitry Andric     EVT VecVT = Op.getValueType();
65810b57cec5SDimitry Andric     EVT EltVT = VecVT.getVectorElementType();
65820b57cec5SDimitry Andric     Op = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(N), EltVT,
65830b57cec5SDimitry Andric                      Op.getOperand(0), N->getOperand(1));
65840b57cec5SDimitry Andric     DCI.AddToWorklist(Op.getNode());
65850b57cec5SDimitry Andric     Op = DAG.getNode(ISD::BSWAP, SDLoc(N), EltVT, Op);
65860b57cec5SDimitry Andric     if (EltVT != N->getValueType(0)) {
65870b57cec5SDimitry Andric       DCI.AddToWorklist(Op.getNode());
65880b57cec5SDimitry Andric       Op = DAG.getNode(ISD::BITCAST, SDLoc(N), N->getValueType(0), Op);
65890b57cec5SDimitry Andric     }
65900b57cec5SDimitry Andric     return Op;
65910b57cec5SDimitry Andric   }
65920b57cec5SDimitry Andric 
65930b57cec5SDimitry Andric   // Try to simplify a vector extraction.
65940b57cec5SDimitry Andric   if (auto *IndexN = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
65950b57cec5SDimitry Andric     SDValue Op0 = N->getOperand(0);
65960b57cec5SDimitry Andric     EVT VecVT = Op0.getValueType();
65970b57cec5SDimitry Andric     return combineExtract(SDLoc(N), N->getValueType(0), VecVT, Op0,
65980b57cec5SDimitry Andric                           IndexN->getZExtValue(), DCI, false);
65990b57cec5SDimitry Andric   }
66000b57cec5SDimitry Andric   return SDValue();
66010b57cec5SDimitry Andric }
66020b57cec5SDimitry Andric 
66030b57cec5SDimitry Andric SDValue SystemZTargetLowering::combineJOIN_DWORDS(
66040b57cec5SDimitry Andric     SDNode *N, DAGCombinerInfo &DCI) const {
66050b57cec5SDimitry Andric   SelectionDAG &DAG = DCI.DAG;
66060b57cec5SDimitry Andric   // (join_dwords X, X) == (replicate X)
66070b57cec5SDimitry Andric   if (N->getOperand(0) == N->getOperand(1))
66080b57cec5SDimitry Andric     return DAG.getNode(SystemZISD::REPLICATE, SDLoc(N), N->getValueType(0),
66090b57cec5SDimitry Andric                        N->getOperand(0));
66100b57cec5SDimitry Andric   return SDValue();
66110b57cec5SDimitry Andric }
66120b57cec5SDimitry Andric 
6613480093f4SDimitry Andric static SDValue MergeInputChains(SDNode *N1, SDNode *N2) {
6614480093f4SDimitry Andric   SDValue Chain1 = N1->getOperand(0);
6615480093f4SDimitry Andric   SDValue Chain2 = N2->getOperand(0);
6616480093f4SDimitry Andric 
6617480093f4SDimitry Andric   // Trivial case: both nodes take the same chain.
6618480093f4SDimitry Andric   if (Chain1 == Chain2)
6619480093f4SDimitry Andric     return Chain1;
6620480093f4SDimitry Andric 
6621480093f4SDimitry Andric   // FIXME - we could handle more complex cases via TokenFactor,
6622480093f4SDimitry Andric   // assuming we can verify that this would not create a cycle.
6623480093f4SDimitry Andric   return SDValue();
6624480093f4SDimitry Andric }
6625480093f4SDimitry Andric 
66260b57cec5SDimitry Andric SDValue SystemZTargetLowering::combineFP_ROUND(
66270b57cec5SDimitry Andric     SDNode *N, DAGCombinerInfo &DCI) const {
66280b57cec5SDimitry Andric 
66290b57cec5SDimitry Andric   if (!Subtarget.hasVector())
66300b57cec5SDimitry Andric     return SDValue();
66310b57cec5SDimitry Andric 
66320b57cec5SDimitry Andric   // (fpround (extract_vector_elt X 0))
66330b57cec5SDimitry Andric   // (fpround (extract_vector_elt X 1)) ->
66340b57cec5SDimitry Andric   // (extract_vector_elt (VROUND X) 0)
66350b57cec5SDimitry Andric   // (extract_vector_elt (VROUND X) 2)
66360b57cec5SDimitry Andric   //
66370b57cec5SDimitry Andric   // This is a special case since the target doesn't really support v2f32s.
6638480093f4SDimitry Andric   unsigned OpNo = N->isStrictFPOpcode() ? 1 : 0;
66390b57cec5SDimitry Andric   SelectionDAG &DAG = DCI.DAG;
6640480093f4SDimitry Andric   SDValue Op0 = N->getOperand(OpNo);
66410b57cec5SDimitry Andric   if (N->getValueType(0) == MVT::f32 &&
66420b57cec5SDimitry Andric       Op0.hasOneUse() &&
66430b57cec5SDimitry Andric       Op0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
66440b57cec5SDimitry Andric       Op0.getOperand(0).getValueType() == MVT::v2f64 &&
66450b57cec5SDimitry Andric       Op0.getOperand(1).getOpcode() == ISD::Constant &&
66460b57cec5SDimitry Andric       cast<ConstantSDNode>(Op0.getOperand(1))->getZExtValue() == 0) {
66470b57cec5SDimitry Andric     SDValue Vec = Op0.getOperand(0);
66480b57cec5SDimitry Andric     for (auto *U : Vec->uses()) {
66490b57cec5SDimitry Andric       if (U != Op0.getNode() &&
66500b57cec5SDimitry Andric           U->hasOneUse() &&
66510b57cec5SDimitry Andric           U->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
66520b57cec5SDimitry Andric           U->getOperand(0) == Vec &&
66530b57cec5SDimitry Andric           U->getOperand(1).getOpcode() == ISD::Constant &&
66540b57cec5SDimitry Andric           cast<ConstantSDNode>(U->getOperand(1))->getZExtValue() == 1) {
66550b57cec5SDimitry Andric         SDValue OtherRound = SDValue(*U->use_begin(), 0);
6656480093f4SDimitry Andric         if (OtherRound.getOpcode() == N->getOpcode() &&
6657480093f4SDimitry Andric             OtherRound.getOperand(OpNo) == SDValue(U, 0) &&
66580b57cec5SDimitry Andric             OtherRound.getValueType() == MVT::f32) {
6659480093f4SDimitry Andric           SDValue VRound, Chain;
6660480093f4SDimitry Andric           if (N->isStrictFPOpcode()) {
6661480093f4SDimitry Andric             Chain = MergeInputChains(N, OtherRound.getNode());
6662480093f4SDimitry Andric             if (!Chain)
6663480093f4SDimitry Andric               continue;
6664480093f4SDimitry Andric             VRound = DAG.getNode(SystemZISD::STRICT_VROUND, SDLoc(N),
6665480093f4SDimitry Andric                                  {MVT::v4f32, MVT::Other}, {Chain, Vec});
6666480093f4SDimitry Andric             Chain = VRound.getValue(1);
6667480093f4SDimitry Andric           } else
6668480093f4SDimitry Andric             VRound = DAG.getNode(SystemZISD::VROUND, SDLoc(N),
66690b57cec5SDimitry Andric                                  MVT::v4f32, Vec);
66700b57cec5SDimitry Andric           DCI.AddToWorklist(VRound.getNode());
66710b57cec5SDimitry Andric           SDValue Extract1 =
66720b57cec5SDimitry Andric             DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(U), MVT::f32,
66730b57cec5SDimitry Andric                         VRound, DAG.getConstant(2, SDLoc(U), MVT::i32));
66740b57cec5SDimitry Andric           DCI.AddToWorklist(Extract1.getNode());
66750b57cec5SDimitry Andric           DAG.ReplaceAllUsesOfValueWith(OtherRound, Extract1);
6676480093f4SDimitry Andric           if (Chain)
6677480093f4SDimitry Andric             DAG.ReplaceAllUsesOfValueWith(OtherRound.getValue(1), Chain);
66780b57cec5SDimitry Andric           SDValue Extract0 =
66790b57cec5SDimitry Andric             DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op0), MVT::f32,
66800b57cec5SDimitry Andric                         VRound, DAG.getConstant(0, SDLoc(Op0), MVT::i32));
6681480093f4SDimitry Andric           if (Chain)
6682480093f4SDimitry Andric             return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op0),
6683480093f4SDimitry Andric                                N->getVTList(), Extract0, Chain);
66840b57cec5SDimitry Andric           return Extract0;
66850b57cec5SDimitry Andric         }
66860b57cec5SDimitry Andric       }
66870b57cec5SDimitry Andric     }
66880b57cec5SDimitry Andric   }
66890b57cec5SDimitry Andric   return SDValue();
66900b57cec5SDimitry Andric }
66910b57cec5SDimitry Andric 
66920b57cec5SDimitry Andric SDValue SystemZTargetLowering::combineFP_EXTEND(
66930b57cec5SDimitry Andric     SDNode *N, DAGCombinerInfo &DCI) const {
66940b57cec5SDimitry Andric 
66950b57cec5SDimitry Andric   if (!Subtarget.hasVector())
66960b57cec5SDimitry Andric     return SDValue();
66970b57cec5SDimitry Andric 
66980b57cec5SDimitry Andric   // (fpextend (extract_vector_elt X 0))
66990b57cec5SDimitry Andric   // (fpextend (extract_vector_elt X 2)) ->
67000b57cec5SDimitry Andric   // (extract_vector_elt (VEXTEND X) 0)
67010b57cec5SDimitry Andric   // (extract_vector_elt (VEXTEND X) 1)
67020b57cec5SDimitry Andric   //
67030b57cec5SDimitry Andric   // This is a special case since the target doesn't really support v2f32s.
6704480093f4SDimitry Andric   unsigned OpNo = N->isStrictFPOpcode() ? 1 : 0;
67050b57cec5SDimitry Andric   SelectionDAG &DAG = DCI.DAG;
6706480093f4SDimitry Andric   SDValue Op0 = N->getOperand(OpNo);
67070b57cec5SDimitry Andric   if (N->getValueType(0) == MVT::f64 &&
67080b57cec5SDimitry Andric       Op0.hasOneUse() &&
67090b57cec5SDimitry Andric       Op0.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
67100b57cec5SDimitry Andric       Op0.getOperand(0).getValueType() == MVT::v4f32 &&
67110b57cec5SDimitry Andric       Op0.getOperand(1).getOpcode() == ISD::Constant &&
67120b57cec5SDimitry Andric       cast<ConstantSDNode>(Op0.getOperand(1))->getZExtValue() == 0) {
67130b57cec5SDimitry Andric     SDValue Vec = Op0.getOperand(0);
67140b57cec5SDimitry Andric     for (auto *U : Vec->uses()) {
67150b57cec5SDimitry Andric       if (U != Op0.getNode() &&
67160b57cec5SDimitry Andric           U->hasOneUse() &&
67170b57cec5SDimitry Andric           U->getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
67180b57cec5SDimitry Andric           U->getOperand(0) == Vec &&
67190b57cec5SDimitry Andric           U->getOperand(1).getOpcode() == ISD::Constant &&
67200b57cec5SDimitry Andric           cast<ConstantSDNode>(U->getOperand(1))->getZExtValue() == 2) {
67210b57cec5SDimitry Andric         SDValue OtherExtend = SDValue(*U->use_begin(), 0);
6722480093f4SDimitry Andric         if (OtherExtend.getOpcode() == N->getOpcode() &&
6723480093f4SDimitry Andric             OtherExtend.getOperand(OpNo) == SDValue(U, 0) &&
67240b57cec5SDimitry Andric             OtherExtend.getValueType() == MVT::f64) {
6725480093f4SDimitry Andric           SDValue VExtend, Chain;
6726480093f4SDimitry Andric           if (N->isStrictFPOpcode()) {
6727480093f4SDimitry Andric             Chain = MergeInputChains(N, OtherExtend.getNode());
6728480093f4SDimitry Andric             if (!Chain)
6729480093f4SDimitry Andric               continue;
6730480093f4SDimitry Andric             VExtend = DAG.getNode(SystemZISD::STRICT_VEXTEND, SDLoc(N),
6731480093f4SDimitry Andric                                   {MVT::v2f64, MVT::Other}, {Chain, Vec});
6732480093f4SDimitry Andric             Chain = VExtend.getValue(1);
6733480093f4SDimitry Andric           } else
6734480093f4SDimitry Andric             VExtend = DAG.getNode(SystemZISD::VEXTEND, SDLoc(N),
67350b57cec5SDimitry Andric                                   MVT::v2f64, Vec);
67360b57cec5SDimitry Andric           DCI.AddToWorklist(VExtend.getNode());
67370b57cec5SDimitry Andric           SDValue Extract1 =
67380b57cec5SDimitry Andric             DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(U), MVT::f64,
67390b57cec5SDimitry Andric                         VExtend, DAG.getConstant(1, SDLoc(U), MVT::i32));
67400b57cec5SDimitry Andric           DCI.AddToWorklist(Extract1.getNode());
67410b57cec5SDimitry Andric           DAG.ReplaceAllUsesOfValueWith(OtherExtend, Extract1);
6742480093f4SDimitry Andric           if (Chain)
6743480093f4SDimitry Andric             DAG.ReplaceAllUsesOfValueWith(OtherExtend.getValue(1), Chain);
67440b57cec5SDimitry Andric           SDValue Extract0 =
67450b57cec5SDimitry Andric             DAG.getNode(ISD::EXTRACT_VECTOR_ELT, SDLoc(Op0), MVT::f64,
67460b57cec5SDimitry Andric                         VExtend, DAG.getConstant(0, SDLoc(Op0), MVT::i32));
6747480093f4SDimitry Andric           if (Chain)
6748480093f4SDimitry Andric             return DAG.getNode(ISD::MERGE_VALUES, SDLoc(Op0),
6749480093f4SDimitry Andric                                N->getVTList(), Extract0, Chain);
67500b57cec5SDimitry Andric           return Extract0;
67510b57cec5SDimitry Andric         }
67520b57cec5SDimitry Andric       }
67530b57cec5SDimitry Andric     }
67540b57cec5SDimitry Andric   }
67550b57cec5SDimitry Andric   return SDValue();
67560b57cec5SDimitry Andric }
67570b57cec5SDimitry Andric 
67585ffd83dbSDimitry Andric SDValue SystemZTargetLowering::combineINT_TO_FP(
67595ffd83dbSDimitry Andric     SDNode *N, DAGCombinerInfo &DCI) const {
67605ffd83dbSDimitry Andric   if (DCI.Level != BeforeLegalizeTypes)
67615ffd83dbSDimitry Andric     return SDValue();
67622a66634dSDimitry Andric   SelectionDAG &DAG = DCI.DAG;
67632a66634dSDimitry Andric   LLVMContext &Ctx = *DAG.getContext();
67645ffd83dbSDimitry Andric   unsigned Opcode = N->getOpcode();
67655ffd83dbSDimitry Andric   EVT OutVT = N->getValueType(0);
67662a66634dSDimitry Andric   Type *OutLLVMTy = OutVT.getTypeForEVT(Ctx);
67675ffd83dbSDimitry Andric   SDValue Op = N->getOperand(0);
67682a66634dSDimitry Andric   unsigned OutScalarBits = OutLLVMTy->getScalarSizeInBits();
67695ffd83dbSDimitry Andric   unsigned InScalarBits = Op->getValueType(0).getScalarSizeInBits();
67705ffd83dbSDimitry Andric 
67715ffd83dbSDimitry Andric   // Insert an extension before type-legalization to avoid scalarization, e.g.:
67725ffd83dbSDimitry Andric   // v2f64 = uint_to_fp v2i16
67735ffd83dbSDimitry Andric   // =>
67745ffd83dbSDimitry Andric   // v2f64 = uint_to_fp (v2i64 zero_extend v2i16)
67752a66634dSDimitry Andric   if (OutLLVMTy->isVectorTy() && OutScalarBits > InScalarBits &&
67762a66634dSDimitry Andric       OutScalarBits <= 64) {
67772a66634dSDimitry Andric     unsigned NumElts = cast<FixedVectorType>(OutLLVMTy)->getNumElements();
67782a66634dSDimitry Andric     EVT ExtVT = EVT::getVectorVT(
67792a66634dSDimitry Andric         Ctx, EVT::getIntegerVT(Ctx, OutLLVMTy->getScalarSizeInBits()), NumElts);
67805ffd83dbSDimitry Andric     unsigned ExtOpcode =
67815ffd83dbSDimitry Andric         (Opcode == ISD::UINT_TO_FP ? ISD::ZERO_EXTEND : ISD::SIGN_EXTEND);
67825ffd83dbSDimitry Andric     SDValue ExtOp = DAG.getNode(ExtOpcode, SDLoc(N), ExtVT, Op);
67835ffd83dbSDimitry Andric     return DAG.getNode(Opcode, SDLoc(N), OutVT, ExtOp);
67845ffd83dbSDimitry Andric   }
67855ffd83dbSDimitry Andric   return SDValue();
67865ffd83dbSDimitry Andric }
67875ffd83dbSDimitry Andric 
67880b57cec5SDimitry Andric SDValue SystemZTargetLowering::combineBSWAP(
67890b57cec5SDimitry Andric     SDNode *N, DAGCombinerInfo &DCI) const {
67900b57cec5SDimitry Andric   SelectionDAG &DAG = DCI.DAG;
67910b57cec5SDimitry Andric   // Combine BSWAP (LOAD) into LRVH/LRV/LRVG/VLBR
67920b57cec5SDimitry Andric   if (ISD::isNON_EXTLoad(N->getOperand(0).getNode()) &&
67930b57cec5SDimitry Andric       N->getOperand(0).hasOneUse() &&
67940b57cec5SDimitry Andric       canLoadStoreByteSwapped(N->getValueType(0))) {
67950b57cec5SDimitry Andric       SDValue Load = N->getOperand(0);
67960b57cec5SDimitry Andric       LoadSDNode *LD = cast<LoadSDNode>(Load);
67970b57cec5SDimitry Andric 
67980b57cec5SDimitry Andric       // Create the byte-swapping load.
67990b57cec5SDimitry Andric       SDValue Ops[] = {
68000b57cec5SDimitry Andric         LD->getChain(),    // Chain
68010b57cec5SDimitry Andric         LD->getBasePtr()   // Ptr
68020b57cec5SDimitry Andric       };
68030b57cec5SDimitry Andric       EVT LoadVT = N->getValueType(0);
68040b57cec5SDimitry Andric       if (LoadVT == MVT::i16)
68050b57cec5SDimitry Andric         LoadVT = MVT::i32;
68060b57cec5SDimitry Andric       SDValue BSLoad =
68070b57cec5SDimitry Andric         DAG.getMemIntrinsicNode(SystemZISD::LRV, SDLoc(N),
68080b57cec5SDimitry Andric                                 DAG.getVTList(LoadVT, MVT::Other),
68090b57cec5SDimitry Andric                                 Ops, LD->getMemoryVT(), LD->getMemOperand());
68100b57cec5SDimitry Andric 
68110b57cec5SDimitry Andric       // If this is an i16 load, insert the truncate.
68120b57cec5SDimitry Andric       SDValue ResVal = BSLoad;
68130b57cec5SDimitry Andric       if (N->getValueType(0) == MVT::i16)
68140b57cec5SDimitry Andric         ResVal = DAG.getNode(ISD::TRUNCATE, SDLoc(N), MVT::i16, BSLoad);
68150b57cec5SDimitry Andric 
68160b57cec5SDimitry Andric       // First, combine the bswap away.  This makes the value produced by the
68170b57cec5SDimitry Andric       // load dead.
68180b57cec5SDimitry Andric       DCI.CombineTo(N, ResVal);
68190b57cec5SDimitry Andric 
68200b57cec5SDimitry Andric       // Next, combine the load away, we give it a bogus result value but a real
68210b57cec5SDimitry Andric       // chain result.  The result value is dead because the bswap is dead.
68220b57cec5SDimitry Andric       DCI.CombineTo(Load.getNode(), ResVal, BSLoad.getValue(1));
68230b57cec5SDimitry Andric 
68240b57cec5SDimitry Andric       // Return N so it doesn't get rechecked!
68250b57cec5SDimitry Andric       return SDValue(N, 0);
68260b57cec5SDimitry Andric     }
68270b57cec5SDimitry Andric 
68280b57cec5SDimitry Andric   // Look through bitcasts that retain the number of vector elements.
68290b57cec5SDimitry Andric   SDValue Op = N->getOperand(0);
68300b57cec5SDimitry Andric   if (Op.getOpcode() == ISD::BITCAST &&
68310b57cec5SDimitry Andric       Op.getValueType().isVector() &&
68320b57cec5SDimitry Andric       Op.getOperand(0).getValueType().isVector() &&
68330b57cec5SDimitry Andric       Op.getValueType().getVectorNumElements() ==
68340b57cec5SDimitry Andric       Op.getOperand(0).getValueType().getVectorNumElements())
68350b57cec5SDimitry Andric     Op = Op.getOperand(0);
68360b57cec5SDimitry Andric 
68370b57cec5SDimitry Andric   // Push BSWAP into a vector insertion if at least one side then simplifies.
68380b57cec5SDimitry Andric   if (Op.getOpcode() == ISD::INSERT_VECTOR_ELT && Op.hasOneUse()) {
68390b57cec5SDimitry Andric     SDValue Vec = Op.getOperand(0);
68400b57cec5SDimitry Andric     SDValue Elt = Op.getOperand(1);
68410b57cec5SDimitry Andric     SDValue Idx = Op.getOperand(2);
68420b57cec5SDimitry Andric 
68430b57cec5SDimitry Andric     if (DAG.isConstantIntBuildVectorOrConstantInt(Vec) ||
68440b57cec5SDimitry Andric         Vec.getOpcode() == ISD::BSWAP || Vec.isUndef() ||
68450b57cec5SDimitry Andric         DAG.isConstantIntBuildVectorOrConstantInt(Elt) ||
68460b57cec5SDimitry Andric         Elt.getOpcode() == ISD::BSWAP || Elt.isUndef() ||
68470b57cec5SDimitry Andric         (canLoadStoreByteSwapped(N->getValueType(0)) &&
68480b57cec5SDimitry Andric          ISD::isNON_EXTLoad(Elt.getNode()) && Elt.hasOneUse())) {
68490b57cec5SDimitry Andric       EVT VecVT = N->getValueType(0);
68500b57cec5SDimitry Andric       EVT EltVT = N->getValueType(0).getVectorElementType();
68510b57cec5SDimitry Andric       if (VecVT != Vec.getValueType()) {
68520b57cec5SDimitry Andric         Vec = DAG.getNode(ISD::BITCAST, SDLoc(N), VecVT, Vec);
68530b57cec5SDimitry Andric         DCI.AddToWorklist(Vec.getNode());
68540b57cec5SDimitry Andric       }
68550b57cec5SDimitry Andric       if (EltVT != Elt.getValueType()) {
68560b57cec5SDimitry Andric         Elt = DAG.getNode(ISD::BITCAST, SDLoc(N), EltVT, Elt);
68570b57cec5SDimitry Andric         DCI.AddToWorklist(Elt.getNode());
68580b57cec5SDimitry Andric       }
68590b57cec5SDimitry Andric       Vec = DAG.getNode(ISD::BSWAP, SDLoc(N), VecVT, Vec);
68600b57cec5SDimitry Andric       DCI.AddToWorklist(Vec.getNode());
68610b57cec5SDimitry Andric       Elt = DAG.getNode(ISD::BSWAP, SDLoc(N), EltVT, Elt);
68620b57cec5SDimitry Andric       DCI.AddToWorklist(Elt.getNode());
68630b57cec5SDimitry Andric       return DAG.getNode(ISD::INSERT_VECTOR_ELT, SDLoc(N), VecVT,
68640b57cec5SDimitry Andric                          Vec, Elt, Idx);
68650b57cec5SDimitry Andric     }
68660b57cec5SDimitry Andric   }
68670b57cec5SDimitry Andric 
68680b57cec5SDimitry Andric   // Push BSWAP into a vector shuffle if at least one side then simplifies.
68690b57cec5SDimitry Andric   ShuffleVectorSDNode *SV = dyn_cast<ShuffleVectorSDNode>(Op);
68700b57cec5SDimitry Andric   if (SV && Op.hasOneUse()) {
68710b57cec5SDimitry Andric     SDValue Op0 = Op.getOperand(0);
68720b57cec5SDimitry Andric     SDValue Op1 = Op.getOperand(1);
68730b57cec5SDimitry Andric 
68740b57cec5SDimitry Andric     if (DAG.isConstantIntBuildVectorOrConstantInt(Op0) ||
68750b57cec5SDimitry Andric         Op0.getOpcode() == ISD::BSWAP || Op0.isUndef() ||
68760b57cec5SDimitry Andric         DAG.isConstantIntBuildVectorOrConstantInt(Op1) ||
68770b57cec5SDimitry Andric         Op1.getOpcode() == ISD::BSWAP || Op1.isUndef()) {
68780b57cec5SDimitry Andric       EVT VecVT = N->getValueType(0);
68790b57cec5SDimitry Andric       if (VecVT != Op0.getValueType()) {
68800b57cec5SDimitry Andric         Op0 = DAG.getNode(ISD::BITCAST, SDLoc(N), VecVT, Op0);
68810b57cec5SDimitry Andric         DCI.AddToWorklist(Op0.getNode());
68820b57cec5SDimitry Andric       }
68830b57cec5SDimitry Andric       if (VecVT != Op1.getValueType()) {
68840b57cec5SDimitry Andric         Op1 = DAG.getNode(ISD::BITCAST, SDLoc(N), VecVT, Op1);
68850b57cec5SDimitry Andric         DCI.AddToWorklist(Op1.getNode());
68860b57cec5SDimitry Andric       }
68870b57cec5SDimitry Andric       Op0 = DAG.getNode(ISD::BSWAP, SDLoc(N), VecVT, Op0);
68880b57cec5SDimitry Andric       DCI.AddToWorklist(Op0.getNode());
68890b57cec5SDimitry Andric       Op1 = DAG.getNode(ISD::BSWAP, SDLoc(N), VecVT, Op1);
68900b57cec5SDimitry Andric       DCI.AddToWorklist(Op1.getNode());
68910b57cec5SDimitry Andric       return DAG.getVectorShuffle(VecVT, SDLoc(N), Op0, Op1, SV->getMask());
68920b57cec5SDimitry Andric     }
68930b57cec5SDimitry Andric   }
68940b57cec5SDimitry Andric 
68950b57cec5SDimitry Andric   return SDValue();
68960b57cec5SDimitry Andric }
68970b57cec5SDimitry Andric 
68980b57cec5SDimitry Andric static bool combineCCMask(SDValue &CCReg, int &CCValid, int &CCMask) {
68990b57cec5SDimitry Andric   // We have a SELECT_CCMASK or BR_CCMASK comparing the condition code
69000b57cec5SDimitry Andric   // set by the CCReg instruction using the CCValid / CCMask masks,
69010b57cec5SDimitry Andric   // If the CCReg instruction is itself a ICMP testing the condition
69020b57cec5SDimitry Andric   // code set by some other instruction, see whether we can directly
69030b57cec5SDimitry Andric   // use that condition code.
69040b57cec5SDimitry Andric 
69050b57cec5SDimitry Andric   // Verify that we have an ICMP against some constant.
69060b57cec5SDimitry Andric   if (CCValid != SystemZ::CCMASK_ICMP)
69070b57cec5SDimitry Andric     return false;
69080b57cec5SDimitry Andric   auto *ICmp = CCReg.getNode();
69090b57cec5SDimitry Andric   if (ICmp->getOpcode() != SystemZISD::ICMP)
69100b57cec5SDimitry Andric     return false;
69110b57cec5SDimitry Andric   auto *CompareLHS = ICmp->getOperand(0).getNode();
69120b57cec5SDimitry Andric   auto *CompareRHS = dyn_cast<ConstantSDNode>(ICmp->getOperand(1));
69130b57cec5SDimitry Andric   if (!CompareRHS)
69140b57cec5SDimitry Andric     return false;
69150b57cec5SDimitry Andric 
69160b57cec5SDimitry Andric   // Optimize the case where CompareLHS is a SELECT_CCMASK.
69170b57cec5SDimitry Andric   if (CompareLHS->getOpcode() == SystemZISD::SELECT_CCMASK) {
69180b57cec5SDimitry Andric     // Verify that we have an appropriate mask for a EQ or NE comparison.
69190b57cec5SDimitry Andric     bool Invert = false;
69200b57cec5SDimitry Andric     if (CCMask == SystemZ::CCMASK_CMP_NE)
69210b57cec5SDimitry Andric       Invert = !Invert;
69220b57cec5SDimitry Andric     else if (CCMask != SystemZ::CCMASK_CMP_EQ)
69230b57cec5SDimitry Andric       return false;
69240b57cec5SDimitry Andric 
69250b57cec5SDimitry Andric     // Verify that the ICMP compares against one of select values.
69260b57cec5SDimitry Andric     auto *TrueVal = dyn_cast<ConstantSDNode>(CompareLHS->getOperand(0));
69270b57cec5SDimitry Andric     if (!TrueVal)
69280b57cec5SDimitry Andric       return false;
69290b57cec5SDimitry Andric     auto *FalseVal = dyn_cast<ConstantSDNode>(CompareLHS->getOperand(1));
69300b57cec5SDimitry Andric     if (!FalseVal)
69310b57cec5SDimitry Andric       return false;
69320b57cec5SDimitry Andric     if (CompareRHS->getZExtValue() == FalseVal->getZExtValue())
69330b57cec5SDimitry Andric       Invert = !Invert;
69340b57cec5SDimitry Andric     else if (CompareRHS->getZExtValue() != TrueVal->getZExtValue())
69350b57cec5SDimitry Andric       return false;
69360b57cec5SDimitry Andric 
69370b57cec5SDimitry Andric     // Compute the effective CC mask for the new branch or select.
69380b57cec5SDimitry Andric     auto *NewCCValid = dyn_cast<ConstantSDNode>(CompareLHS->getOperand(2));
69390b57cec5SDimitry Andric     auto *NewCCMask = dyn_cast<ConstantSDNode>(CompareLHS->getOperand(3));
69400b57cec5SDimitry Andric     if (!NewCCValid || !NewCCMask)
69410b57cec5SDimitry Andric       return false;
69420b57cec5SDimitry Andric     CCValid = NewCCValid->getZExtValue();
69430b57cec5SDimitry Andric     CCMask = NewCCMask->getZExtValue();
69440b57cec5SDimitry Andric     if (Invert)
69450b57cec5SDimitry Andric       CCMask ^= CCValid;
69460b57cec5SDimitry Andric 
69470b57cec5SDimitry Andric     // Return the updated CCReg link.
69480b57cec5SDimitry Andric     CCReg = CompareLHS->getOperand(4);
69490b57cec5SDimitry Andric     return true;
69500b57cec5SDimitry Andric   }
69510b57cec5SDimitry Andric 
69520b57cec5SDimitry Andric   // Optimize the case where CompareRHS is (SRA (SHL (IPM))).
69530b57cec5SDimitry Andric   if (CompareLHS->getOpcode() == ISD::SRA) {
69540b57cec5SDimitry Andric     auto *SRACount = dyn_cast<ConstantSDNode>(CompareLHS->getOperand(1));
69550b57cec5SDimitry Andric     if (!SRACount || SRACount->getZExtValue() != 30)
69560b57cec5SDimitry Andric       return false;
69570b57cec5SDimitry Andric     auto *SHL = CompareLHS->getOperand(0).getNode();
69580b57cec5SDimitry Andric     if (SHL->getOpcode() != ISD::SHL)
69590b57cec5SDimitry Andric       return false;
69600b57cec5SDimitry Andric     auto *SHLCount = dyn_cast<ConstantSDNode>(SHL->getOperand(1));
69610b57cec5SDimitry Andric     if (!SHLCount || SHLCount->getZExtValue() != 30 - SystemZ::IPM_CC)
69620b57cec5SDimitry Andric       return false;
69630b57cec5SDimitry Andric     auto *IPM = SHL->getOperand(0).getNode();
69640b57cec5SDimitry Andric     if (IPM->getOpcode() != SystemZISD::IPM)
69650b57cec5SDimitry Andric       return false;
69660b57cec5SDimitry Andric 
69670b57cec5SDimitry Andric     // Avoid introducing CC spills (because SRA would clobber CC).
69680b57cec5SDimitry Andric     if (!CompareLHS->hasOneUse())
69690b57cec5SDimitry Andric       return false;
69700b57cec5SDimitry Andric     // Verify that the ICMP compares against zero.
69710b57cec5SDimitry Andric     if (CompareRHS->getZExtValue() != 0)
69720b57cec5SDimitry Andric       return false;
69730b57cec5SDimitry Andric 
69740b57cec5SDimitry Andric     // Compute the effective CC mask for the new branch or select.
69755ffd83dbSDimitry Andric     CCMask = SystemZ::reverseCCMask(CCMask);
69760b57cec5SDimitry Andric 
69770b57cec5SDimitry Andric     // Return the updated CCReg link.
69780b57cec5SDimitry Andric     CCReg = IPM->getOperand(0);
69790b57cec5SDimitry Andric     return true;
69800b57cec5SDimitry Andric   }
69810b57cec5SDimitry Andric 
69820b57cec5SDimitry Andric   return false;
69830b57cec5SDimitry Andric }
69840b57cec5SDimitry Andric 
69850b57cec5SDimitry Andric SDValue SystemZTargetLowering::combineBR_CCMASK(
69860b57cec5SDimitry Andric     SDNode *N, DAGCombinerInfo &DCI) const {
69870b57cec5SDimitry Andric   SelectionDAG &DAG = DCI.DAG;
69880b57cec5SDimitry Andric 
69890b57cec5SDimitry Andric   // Combine BR_CCMASK (ICMP (SELECT_CCMASK)) into a single BR_CCMASK.
69900b57cec5SDimitry Andric   auto *CCValid = dyn_cast<ConstantSDNode>(N->getOperand(1));
69910b57cec5SDimitry Andric   auto *CCMask = dyn_cast<ConstantSDNode>(N->getOperand(2));
69920b57cec5SDimitry Andric   if (!CCValid || !CCMask)
69930b57cec5SDimitry Andric     return SDValue();
69940b57cec5SDimitry Andric 
69950b57cec5SDimitry Andric   int CCValidVal = CCValid->getZExtValue();
69960b57cec5SDimitry Andric   int CCMaskVal = CCMask->getZExtValue();
69970b57cec5SDimitry Andric   SDValue Chain = N->getOperand(0);
69980b57cec5SDimitry Andric   SDValue CCReg = N->getOperand(4);
69990b57cec5SDimitry Andric 
70000b57cec5SDimitry Andric   if (combineCCMask(CCReg, CCValidVal, CCMaskVal))
70010b57cec5SDimitry Andric     return DAG.getNode(SystemZISD::BR_CCMASK, SDLoc(N), N->getValueType(0),
70020b57cec5SDimitry Andric                        Chain,
70038bcb0991SDimitry Andric                        DAG.getTargetConstant(CCValidVal, SDLoc(N), MVT::i32),
70048bcb0991SDimitry Andric                        DAG.getTargetConstant(CCMaskVal, SDLoc(N), MVT::i32),
70050b57cec5SDimitry Andric                        N->getOperand(3), CCReg);
70060b57cec5SDimitry Andric   return SDValue();
70070b57cec5SDimitry Andric }
70080b57cec5SDimitry Andric 
70090b57cec5SDimitry Andric SDValue SystemZTargetLowering::combineSELECT_CCMASK(
70100b57cec5SDimitry Andric     SDNode *N, DAGCombinerInfo &DCI) const {
70110b57cec5SDimitry Andric   SelectionDAG &DAG = DCI.DAG;
70120b57cec5SDimitry Andric 
70130b57cec5SDimitry Andric   // Combine SELECT_CCMASK (ICMP (SELECT_CCMASK)) into a single SELECT_CCMASK.
70140b57cec5SDimitry Andric   auto *CCValid = dyn_cast<ConstantSDNode>(N->getOperand(2));
70150b57cec5SDimitry Andric   auto *CCMask = dyn_cast<ConstantSDNode>(N->getOperand(3));
70160b57cec5SDimitry Andric   if (!CCValid || !CCMask)
70170b57cec5SDimitry Andric     return SDValue();
70180b57cec5SDimitry Andric 
70190b57cec5SDimitry Andric   int CCValidVal = CCValid->getZExtValue();
70200b57cec5SDimitry Andric   int CCMaskVal = CCMask->getZExtValue();
70210b57cec5SDimitry Andric   SDValue CCReg = N->getOperand(4);
70220b57cec5SDimitry Andric 
70230b57cec5SDimitry Andric   if (combineCCMask(CCReg, CCValidVal, CCMaskVal))
70240b57cec5SDimitry Andric     return DAG.getNode(SystemZISD::SELECT_CCMASK, SDLoc(N), N->getValueType(0),
70258bcb0991SDimitry Andric                        N->getOperand(0), N->getOperand(1),
70268bcb0991SDimitry Andric                        DAG.getTargetConstant(CCValidVal, SDLoc(N), MVT::i32),
70278bcb0991SDimitry Andric                        DAG.getTargetConstant(CCMaskVal, SDLoc(N), MVT::i32),
70280b57cec5SDimitry Andric                        CCReg);
70290b57cec5SDimitry Andric   return SDValue();
70300b57cec5SDimitry Andric }
70310b57cec5SDimitry Andric 
70320b57cec5SDimitry Andric 
70330b57cec5SDimitry Andric SDValue SystemZTargetLowering::combineGET_CCMASK(
70340b57cec5SDimitry Andric     SDNode *N, DAGCombinerInfo &DCI) const {
70350b57cec5SDimitry Andric 
70360b57cec5SDimitry Andric   // Optimize away GET_CCMASK (SELECT_CCMASK) if the CC masks are compatible
70370b57cec5SDimitry Andric   auto *CCValid = dyn_cast<ConstantSDNode>(N->getOperand(1));
70380b57cec5SDimitry Andric   auto *CCMask = dyn_cast<ConstantSDNode>(N->getOperand(2));
70390b57cec5SDimitry Andric   if (!CCValid || !CCMask)
70400b57cec5SDimitry Andric     return SDValue();
70410b57cec5SDimitry Andric   int CCValidVal = CCValid->getZExtValue();
70420b57cec5SDimitry Andric   int CCMaskVal = CCMask->getZExtValue();
70430b57cec5SDimitry Andric 
70440b57cec5SDimitry Andric   SDValue Select = N->getOperand(0);
7045bdd1243dSDimitry Andric   if (Select->getOpcode() == ISD::TRUNCATE)
7046bdd1243dSDimitry Andric     Select = Select->getOperand(0);
70470b57cec5SDimitry Andric   if (Select->getOpcode() != SystemZISD::SELECT_CCMASK)
70480b57cec5SDimitry Andric     return SDValue();
70490b57cec5SDimitry Andric 
70500b57cec5SDimitry Andric   auto *SelectCCValid = dyn_cast<ConstantSDNode>(Select->getOperand(2));
70510b57cec5SDimitry Andric   auto *SelectCCMask = dyn_cast<ConstantSDNode>(Select->getOperand(3));
70520b57cec5SDimitry Andric   if (!SelectCCValid || !SelectCCMask)
70530b57cec5SDimitry Andric     return SDValue();
70540b57cec5SDimitry Andric   int SelectCCValidVal = SelectCCValid->getZExtValue();
70550b57cec5SDimitry Andric   int SelectCCMaskVal = SelectCCMask->getZExtValue();
70560b57cec5SDimitry Andric 
70570b57cec5SDimitry Andric   auto *TrueVal = dyn_cast<ConstantSDNode>(Select->getOperand(0));
70580b57cec5SDimitry Andric   auto *FalseVal = dyn_cast<ConstantSDNode>(Select->getOperand(1));
70590b57cec5SDimitry Andric   if (!TrueVal || !FalseVal)
70600b57cec5SDimitry Andric     return SDValue();
7061bdd1243dSDimitry Andric   if (TrueVal->getZExtValue() == 1 && FalseVal->getZExtValue() == 0)
70620b57cec5SDimitry Andric     ;
7063bdd1243dSDimitry Andric   else if (TrueVal->getZExtValue() == 0 && FalseVal->getZExtValue() == 1)
70640b57cec5SDimitry Andric     SelectCCMaskVal ^= SelectCCValidVal;
70650b57cec5SDimitry Andric   else
70660b57cec5SDimitry Andric     return SDValue();
70670b57cec5SDimitry Andric 
70680b57cec5SDimitry Andric   if (SelectCCValidVal & ~CCValidVal)
70690b57cec5SDimitry Andric     return SDValue();
70700b57cec5SDimitry Andric   if (SelectCCMaskVal != (CCMaskVal & SelectCCValidVal))
70710b57cec5SDimitry Andric     return SDValue();
70720b57cec5SDimitry Andric 
70730b57cec5SDimitry Andric   return Select->getOperand(4);
70740b57cec5SDimitry Andric }
70750b57cec5SDimitry Andric 
70760b57cec5SDimitry Andric SDValue SystemZTargetLowering::combineIntDIVREM(
70770b57cec5SDimitry Andric     SDNode *N, DAGCombinerInfo &DCI) const {
70780b57cec5SDimitry Andric   SelectionDAG &DAG = DCI.DAG;
70790b57cec5SDimitry Andric   EVT VT = N->getValueType(0);
70800b57cec5SDimitry Andric   // In the case where the divisor is a vector of constants a cheaper
70810b57cec5SDimitry Andric   // sequence of instructions can replace the divide. BuildSDIV is called to
70820b57cec5SDimitry Andric   // do this during DAG combining, but it only succeeds when it can build a
70830b57cec5SDimitry Andric   // multiplication node. The only option for SystemZ is ISD::SMUL_LOHI, and
70840b57cec5SDimitry Andric   // since it is not Legal but Custom it can only happen before
70850b57cec5SDimitry Andric   // legalization. Therefore we must scalarize this early before Combine
70860b57cec5SDimitry Andric   // 1. For widened vectors, this is already the result of type legalization.
70870b57cec5SDimitry Andric   if (DCI.Level == BeforeLegalizeTypes && VT.isVector() && isTypeLegal(VT) &&
70880b57cec5SDimitry Andric       DAG.isConstantIntBuildVectorOrConstantInt(N->getOperand(1)))
70890b57cec5SDimitry Andric     return DAG.UnrollVectorOp(N);
70900b57cec5SDimitry Andric   return SDValue();
70910b57cec5SDimitry Andric }
70920b57cec5SDimitry Andric 
70935ffd83dbSDimitry Andric SDValue SystemZTargetLowering::combineINTRINSIC(
70945ffd83dbSDimitry Andric     SDNode *N, DAGCombinerInfo &DCI) const {
70955ffd83dbSDimitry Andric   SelectionDAG &DAG = DCI.DAG;
70965ffd83dbSDimitry Andric 
70975ffd83dbSDimitry Andric   unsigned Id = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
70985ffd83dbSDimitry Andric   switch (Id) {
70995ffd83dbSDimitry Andric   // VECTOR LOAD (RIGHTMOST) WITH LENGTH with a length operand of 15
71005ffd83dbSDimitry Andric   // or larger is simply a vector load.
71015ffd83dbSDimitry Andric   case Intrinsic::s390_vll:
71025ffd83dbSDimitry Andric   case Intrinsic::s390_vlrl:
71035ffd83dbSDimitry Andric     if (auto *C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
71045ffd83dbSDimitry Andric       if (C->getZExtValue() >= 15)
71055ffd83dbSDimitry Andric         return DAG.getLoad(N->getValueType(0), SDLoc(N), N->getOperand(0),
71065ffd83dbSDimitry Andric                            N->getOperand(3), MachinePointerInfo());
71075ffd83dbSDimitry Andric     break;
71085ffd83dbSDimitry Andric   // Likewise for VECTOR STORE (RIGHTMOST) WITH LENGTH.
71095ffd83dbSDimitry Andric   case Intrinsic::s390_vstl:
71105ffd83dbSDimitry Andric   case Intrinsic::s390_vstrl:
71115ffd83dbSDimitry Andric     if (auto *C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
71125ffd83dbSDimitry Andric       if (C->getZExtValue() >= 15)
71135ffd83dbSDimitry Andric         return DAG.getStore(N->getOperand(0), SDLoc(N), N->getOperand(2),
71145ffd83dbSDimitry Andric                             N->getOperand(4), MachinePointerInfo());
71155ffd83dbSDimitry Andric     break;
71165ffd83dbSDimitry Andric   }
71175ffd83dbSDimitry Andric 
71185ffd83dbSDimitry Andric   return SDValue();
71195ffd83dbSDimitry Andric }
71205ffd83dbSDimitry Andric 
71210b57cec5SDimitry Andric SDValue SystemZTargetLowering::unwrapAddress(SDValue N) const {
71220b57cec5SDimitry Andric   if (N->getOpcode() == SystemZISD::PCREL_WRAPPER)
71230b57cec5SDimitry Andric     return N->getOperand(0);
71240b57cec5SDimitry Andric   return N;
71250b57cec5SDimitry Andric }
71260b57cec5SDimitry Andric 
71270b57cec5SDimitry Andric SDValue SystemZTargetLowering::PerformDAGCombine(SDNode *N,
71280b57cec5SDimitry Andric                                                  DAGCombinerInfo &DCI) const {
71290b57cec5SDimitry Andric   switch(N->getOpcode()) {
71300b57cec5SDimitry Andric   default: break;
71310b57cec5SDimitry Andric   case ISD::ZERO_EXTEND:        return combineZERO_EXTEND(N, DCI);
71320b57cec5SDimitry Andric   case ISD::SIGN_EXTEND:        return combineSIGN_EXTEND(N, DCI);
71330b57cec5SDimitry Andric   case ISD::SIGN_EXTEND_INREG:  return combineSIGN_EXTEND_INREG(N, DCI);
71340b57cec5SDimitry Andric   case SystemZISD::MERGE_HIGH:
71350b57cec5SDimitry Andric   case SystemZISD::MERGE_LOW:   return combineMERGE(N, DCI);
71360b57cec5SDimitry Andric   case ISD::LOAD:               return combineLOAD(N, DCI);
71370b57cec5SDimitry Andric   case ISD::STORE:              return combineSTORE(N, DCI);
71380b57cec5SDimitry Andric   case ISD::VECTOR_SHUFFLE:     return combineVECTOR_SHUFFLE(N, DCI);
71390b57cec5SDimitry Andric   case ISD::EXTRACT_VECTOR_ELT: return combineEXTRACT_VECTOR_ELT(N, DCI);
71400b57cec5SDimitry Andric   case SystemZISD::JOIN_DWORDS: return combineJOIN_DWORDS(N, DCI);
7141480093f4SDimitry Andric   case ISD::STRICT_FP_ROUND:
71420b57cec5SDimitry Andric   case ISD::FP_ROUND:           return combineFP_ROUND(N, DCI);
7143480093f4SDimitry Andric   case ISD::STRICT_FP_EXTEND:
71440b57cec5SDimitry Andric   case ISD::FP_EXTEND:          return combineFP_EXTEND(N, DCI);
71455ffd83dbSDimitry Andric   case ISD::SINT_TO_FP:
71465ffd83dbSDimitry Andric   case ISD::UINT_TO_FP:         return combineINT_TO_FP(N, DCI);
71470b57cec5SDimitry Andric   case ISD::BSWAP:              return combineBSWAP(N, DCI);
71480b57cec5SDimitry Andric   case SystemZISD::BR_CCMASK:   return combineBR_CCMASK(N, DCI);
71490b57cec5SDimitry Andric   case SystemZISD::SELECT_CCMASK: return combineSELECT_CCMASK(N, DCI);
71500b57cec5SDimitry Andric   case SystemZISD::GET_CCMASK:  return combineGET_CCMASK(N, DCI);
71510b57cec5SDimitry Andric   case ISD::SDIV:
71520b57cec5SDimitry Andric   case ISD::UDIV:
71530b57cec5SDimitry Andric   case ISD::SREM:
71540b57cec5SDimitry Andric   case ISD::UREM:               return combineIntDIVREM(N, DCI);
71555ffd83dbSDimitry Andric   case ISD::INTRINSIC_W_CHAIN:
71565ffd83dbSDimitry Andric   case ISD::INTRINSIC_VOID:     return combineINTRINSIC(N, DCI);
71570b57cec5SDimitry Andric   }
71580b57cec5SDimitry Andric 
71590b57cec5SDimitry Andric   return SDValue();
71600b57cec5SDimitry Andric }
71610b57cec5SDimitry Andric 
71620b57cec5SDimitry Andric // Return the demanded elements for the OpNo source operand of Op. DemandedElts
71630b57cec5SDimitry Andric // are for Op.
71640b57cec5SDimitry Andric static APInt getDemandedSrcElements(SDValue Op, const APInt &DemandedElts,
71650b57cec5SDimitry Andric                                     unsigned OpNo) {
71660b57cec5SDimitry Andric   EVT VT = Op.getValueType();
71670b57cec5SDimitry Andric   unsigned NumElts = (VT.isVector() ? VT.getVectorNumElements() : 1);
71680b57cec5SDimitry Andric   APInt SrcDemE;
71690b57cec5SDimitry Andric   unsigned Opcode = Op.getOpcode();
71700b57cec5SDimitry Andric   if (Opcode == ISD::INTRINSIC_WO_CHAIN) {
71710b57cec5SDimitry Andric     unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
71720b57cec5SDimitry Andric     switch (Id) {
71730b57cec5SDimitry Andric     case Intrinsic::s390_vpksh:   // PACKS
71740b57cec5SDimitry Andric     case Intrinsic::s390_vpksf:
71750b57cec5SDimitry Andric     case Intrinsic::s390_vpksg:
71760b57cec5SDimitry Andric     case Intrinsic::s390_vpkshs:  // PACKS_CC
71770b57cec5SDimitry Andric     case Intrinsic::s390_vpksfs:
71780b57cec5SDimitry Andric     case Intrinsic::s390_vpksgs:
71790b57cec5SDimitry Andric     case Intrinsic::s390_vpklsh:  // PACKLS
71800b57cec5SDimitry Andric     case Intrinsic::s390_vpklsf:
71810b57cec5SDimitry Andric     case Intrinsic::s390_vpklsg:
71820b57cec5SDimitry Andric     case Intrinsic::s390_vpklshs: // PACKLS_CC
71830b57cec5SDimitry Andric     case Intrinsic::s390_vpklsfs:
71840b57cec5SDimitry Andric     case Intrinsic::s390_vpklsgs:
71850b57cec5SDimitry Andric       // VECTOR PACK truncates the elements of two source vectors into one.
71860b57cec5SDimitry Andric       SrcDemE = DemandedElts;
71870b57cec5SDimitry Andric       if (OpNo == 2)
71880b57cec5SDimitry Andric         SrcDemE.lshrInPlace(NumElts / 2);
71890b57cec5SDimitry Andric       SrcDemE = SrcDemE.trunc(NumElts / 2);
71900b57cec5SDimitry Andric       break;
71910b57cec5SDimitry Andric       // VECTOR UNPACK extends half the elements of the source vector.
71920b57cec5SDimitry Andric     case Intrinsic::s390_vuphb:  // VECTOR UNPACK HIGH
71930b57cec5SDimitry Andric     case Intrinsic::s390_vuphh:
71940b57cec5SDimitry Andric     case Intrinsic::s390_vuphf:
71950b57cec5SDimitry Andric     case Intrinsic::s390_vuplhb: // VECTOR UNPACK LOGICAL HIGH
71960b57cec5SDimitry Andric     case Intrinsic::s390_vuplhh:
71970b57cec5SDimitry Andric     case Intrinsic::s390_vuplhf:
71980b57cec5SDimitry Andric       SrcDemE = APInt(NumElts * 2, 0);
71990b57cec5SDimitry Andric       SrcDemE.insertBits(DemandedElts, 0);
72000b57cec5SDimitry Andric       break;
72010b57cec5SDimitry Andric     case Intrinsic::s390_vuplb:  // VECTOR UNPACK LOW
72020b57cec5SDimitry Andric     case Intrinsic::s390_vuplhw:
72030b57cec5SDimitry Andric     case Intrinsic::s390_vuplf:
72040b57cec5SDimitry Andric     case Intrinsic::s390_vupllb: // VECTOR UNPACK LOGICAL LOW
72050b57cec5SDimitry Andric     case Intrinsic::s390_vupllh:
72060b57cec5SDimitry Andric     case Intrinsic::s390_vupllf:
72070b57cec5SDimitry Andric       SrcDemE = APInt(NumElts * 2, 0);
72080b57cec5SDimitry Andric       SrcDemE.insertBits(DemandedElts, NumElts);
72090b57cec5SDimitry Andric       break;
72100b57cec5SDimitry Andric     case Intrinsic::s390_vpdi: {
72110b57cec5SDimitry Andric       // VECTOR PERMUTE DWORD IMMEDIATE selects one element from each source.
72120b57cec5SDimitry Andric       SrcDemE = APInt(NumElts, 0);
72130b57cec5SDimitry Andric       if (!DemandedElts[OpNo - 1])
72140b57cec5SDimitry Andric         break;
72150b57cec5SDimitry Andric       unsigned Mask = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
72160b57cec5SDimitry Andric       unsigned MaskBit = ((OpNo - 1) ? 1 : 4);
72170b57cec5SDimitry Andric       // Demand input element 0 or 1, given by the mask bit value.
72180b57cec5SDimitry Andric       SrcDemE.setBit((Mask & MaskBit)? 1 : 0);
72190b57cec5SDimitry Andric       break;
72200b57cec5SDimitry Andric     }
72210b57cec5SDimitry Andric     case Intrinsic::s390_vsldb: {
72220b57cec5SDimitry Andric       // VECTOR SHIFT LEFT DOUBLE BY BYTE
72230b57cec5SDimitry Andric       assert(VT == MVT::v16i8 && "Unexpected type.");
72240b57cec5SDimitry Andric       unsigned FirstIdx = cast<ConstantSDNode>(Op.getOperand(3))->getZExtValue();
72250b57cec5SDimitry Andric       assert (FirstIdx > 0 && FirstIdx < 16 && "Unused operand.");
72260b57cec5SDimitry Andric       unsigned NumSrc0Els = 16 - FirstIdx;
72270b57cec5SDimitry Andric       SrcDemE = APInt(NumElts, 0);
72280b57cec5SDimitry Andric       if (OpNo == 1) {
72290b57cec5SDimitry Andric         APInt DemEls = DemandedElts.trunc(NumSrc0Els);
72300b57cec5SDimitry Andric         SrcDemE.insertBits(DemEls, FirstIdx);
72310b57cec5SDimitry Andric       } else {
72320b57cec5SDimitry Andric         APInt DemEls = DemandedElts.lshr(NumSrc0Els);
72330b57cec5SDimitry Andric         SrcDemE.insertBits(DemEls, 0);
72340b57cec5SDimitry Andric       }
72350b57cec5SDimitry Andric       break;
72360b57cec5SDimitry Andric     }
72370b57cec5SDimitry Andric     case Intrinsic::s390_vperm:
72380b57cec5SDimitry Andric       SrcDemE = APInt(NumElts, 1);
72390b57cec5SDimitry Andric       break;
72400b57cec5SDimitry Andric     default:
72410b57cec5SDimitry Andric       llvm_unreachable("Unhandled intrinsic.");
72420b57cec5SDimitry Andric       break;
72430b57cec5SDimitry Andric     }
72440b57cec5SDimitry Andric   } else {
72450b57cec5SDimitry Andric     switch (Opcode) {
72460b57cec5SDimitry Andric     case SystemZISD::JOIN_DWORDS:
72470b57cec5SDimitry Andric       // Scalar operand.
72480b57cec5SDimitry Andric       SrcDemE = APInt(1, 1);
72490b57cec5SDimitry Andric       break;
72500b57cec5SDimitry Andric     case SystemZISD::SELECT_CCMASK:
72510b57cec5SDimitry Andric       SrcDemE = DemandedElts;
72520b57cec5SDimitry Andric       break;
72530b57cec5SDimitry Andric     default:
72540b57cec5SDimitry Andric       llvm_unreachable("Unhandled opcode.");
72550b57cec5SDimitry Andric       break;
72560b57cec5SDimitry Andric     }
72570b57cec5SDimitry Andric   }
72580b57cec5SDimitry Andric   return SrcDemE;
72590b57cec5SDimitry Andric }
72600b57cec5SDimitry Andric 
72610b57cec5SDimitry Andric static void computeKnownBitsBinOp(const SDValue Op, KnownBits &Known,
72620b57cec5SDimitry Andric                                   const APInt &DemandedElts,
72630b57cec5SDimitry Andric                                   const SelectionDAG &DAG, unsigned Depth,
72640b57cec5SDimitry Andric                                   unsigned OpNo) {
72650b57cec5SDimitry Andric   APInt Src0DemE = getDemandedSrcElements(Op, DemandedElts, OpNo);
72660b57cec5SDimitry Andric   APInt Src1DemE = getDemandedSrcElements(Op, DemandedElts, OpNo + 1);
72670b57cec5SDimitry Andric   KnownBits LHSKnown =
72680b57cec5SDimitry Andric       DAG.computeKnownBits(Op.getOperand(OpNo), Src0DemE, Depth + 1);
72690b57cec5SDimitry Andric   KnownBits RHSKnown =
72700b57cec5SDimitry Andric       DAG.computeKnownBits(Op.getOperand(OpNo + 1), Src1DemE, Depth + 1);
7271e8d8bef9SDimitry Andric   Known = KnownBits::commonBits(LHSKnown, RHSKnown);
72720b57cec5SDimitry Andric }
72730b57cec5SDimitry Andric 
72740b57cec5SDimitry Andric void
72750b57cec5SDimitry Andric SystemZTargetLowering::computeKnownBitsForTargetNode(const SDValue Op,
72760b57cec5SDimitry Andric                                                      KnownBits &Known,
72770b57cec5SDimitry Andric                                                      const APInt &DemandedElts,
72780b57cec5SDimitry Andric                                                      const SelectionDAG &DAG,
72790b57cec5SDimitry Andric                                                      unsigned Depth) const {
72800b57cec5SDimitry Andric   Known.resetAll();
72810b57cec5SDimitry Andric 
72820b57cec5SDimitry Andric   // Intrinsic CC result is returned in the two low bits.
72830b57cec5SDimitry Andric   unsigned tmp0, tmp1; // not used
72840b57cec5SDimitry Andric   if (Op.getResNo() == 1 && isIntrinsicWithCC(Op, tmp0, tmp1)) {
72850b57cec5SDimitry Andric     Known.Zero.setBitsFrom(2);
72860b57cec5SDimitry Andric     return;
72870b57cec5SDimitry Andric   }
72880b57cec5SDimitry Andric   EVT VT = Op.getValueType();
72890b57cec5SDimitry Andric   if (Op.getResNo() != 0 || VT == MVT::Untyped)
72900b57cec5SDimitry Andric     return;
72910b57cec5SDimitry Andric   assert (Known.getBitWidth() == VT.getScalarSizeInBits() &&
72920b57cec5SDimitry Andric           "KnownBits does not match VT in bitwidth");
72930b57cec5SDimitry Andric   assert ((!VT.isVector() ||
72940b57cec5SDimitry Andric            (DemandedElts.getBitWidth() == VT.getVectorNumElements())) &&
72950b57cec5SDimitry Andric           "DemandedElts does not match VT number of elements");
72960b57cec5SDimitry Andric   unsigned BitWidth = Known.getBitWidth();
72970b57cec5SDimitry Andric   unsigned Opcode = Op.getOpcode();
72980b57cec5SDimitry Andric   if (Opcode == ISD::INTRINSIC_WO_CHAIN) {
72990b57cec5SDimitry Andric     bool IsLogical = false;
73000b57cec5SDimitry Andric     unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
73010b57cec5SDimitry Andric     switch (Id) {
73020b57cec5SDimitry Andric     case Intrinsic::s390_vpksh:   // PACKS
73030b57cec5SDimitry Andric     case Intrinsic::s390_vpksf:
73040b57cec5SDimitry Andric     case Intrinsic::s390_vpksg:
73050b57cec5SDimitry Andric     case Intrinsic::s390_vpkshs:  // PACKS_CC
73060b57cec5SDimitry Andric     case Intrinsic::s390_vpksfs:
73070b57cec5SDimitry Andric     case Intrinsic::s390_vpksgs:
73080b57cec5SDimitry Andric     case Intrinsic::s390_vpklsh:  // PACKLS
73090b57cec5SDimitry Andric     case Intrinsic::s390_vpklsf:
73100b57cec5SDimitry Andric     case Intrinsic::s390_vpklsg:
73110b57cec5SDimitry Andric     case Intrinsic::s390_vpklshs: // PACKLS_CC
73120b57cec5SDimitry Andric     case Intrinsic::s390_vpklsfs:
73130b57cec5SDimitry Andric     case Intrinsic::s390_vpklsgs:
73140b57cec5SDimitry Andric     case Intrinsic::s390_vpdi:
73150b57cec5SDimitry Andric     case Intrinsic::s390_vsldb:
73160b57cec5SDimitry Andric     case Intrinsic::s390_vperm:
73170b57cec5SDimitry Andric       computeKnownBitsBinOp(Op, Known, DemandedElts, DAG, Depth, 1);
73180b57cec5SDimitry Andric       break;
73190b57cec5SDimitry Andric     case Intrinsic::s390_vuplhb: // VECTOR UNPACK LOGICAL HIGH
73200b57cec5SDimitry Andric     case Intrinsic::s390_vuplhh:
73210b57cec5SDimitry Andric     case Intrinsic::s390_vuplhf:
73220b57cec5SDimitry Andric     case Intrinsic::s390_vupllb: // VECTOR UNPACK LOGICAL LOW
73230b57cec5SDimitry Andric     case Intrinsic::s390_vupllh:
73240b57cec5SDimitry Andric     case Intrinsic::s390_vupllf:
73250b57cec5SDimitry Andric       IsLogical = true;
7326bdd1243dSDimitry Andric       [[fallthrough]];
73270b57cec5SDimitry Andric     case Intrinsic::s390_vuphb:  // VECTOR UNPACK HIGH
73280b57cec5SDimitry Andric     case Intrinsic::s390_vuphh:
73290b57cec5SDimitry Andric     case Intrinsic::s390_vuphf:
73300b57cec5SDimitry Andric     case Intrinsic::s390_vuplb:  // VECTOR UNPACK LOW
73310b57cec5SDimitry Andric     case Intrinsic::s390_vuplhw:
73320b57cec5SDimitry Andric     case Intrinsic::s390_vuplf: {
73330b57cec5SDimitry Andric       SDValue SrcOp = Op.getOperand(1);
73340b57cec5SDimitry Andric       APInt SrcDemE = getDemandedSrcElements(Op, DemandedElts, 0);
73350b57cec5SDimitry Andric       Known = DAG.computeKnownBits(SrcOp, SrcDemE, Depth + 1);
73360b57cec5SDimitry Andric       if (IsLogical) {
73375ffd83dbSDimitry Andric         Known = Known.zext(BitWidth);
73380b57cec5SDimitry Andric       } else
73390b57cec5SDimitry Andric         Known = Known.sext(BitWidth);
73400b57cec5SDimitry Andric       break;
73410b57cec5SDimitry Andric     }
73420b57cec5SDimitry Andric     default:
73430b57cec5SDimitry Andric       break;
73440b57cec5SDimitry Andric     }
73450b57cec5SDimitry Andric   } else {
73460b57cec5SDimitry Andric     switch (Opcode) {
73470b57cec5SDimitry Andric     case SystemZISD::JOIN_DWORDS:
73480b57cec5SDimitry Andric     case SystemZISD::SELECT_CCMASK:
73490b57cec5SDimitry Andric       computeKnownBitsBinOp(Op, Known, DemandedElts, DAG, Depth, 0);
73500b57cec5SDimitry Andric       break;
73510b57cec5SDimitry Andric     case SystemZISD::REPLICATE: {
73520b57cec5SDimitry Andric       SDValue SrcOp = Op.getOperand(0);
73530b57cec5SDimitry Andric       Known = DAG.computeKnownBits(SrcOp, Depth + 1);
73540b57cec5SDimitry Andric       if (Known.getBitWidth() < BitWidth && isa<ConstantSDNode>(SrcOp))
73550b57cec5SDimitry Andric         Known = Known.sext(BitWidth); // VREPI sign extends the immedate.
73560b57cec5SDimitry Andric       break;
73570b57cec5SDimitry Andric     }
73580b57cec5SDimitry Andric     default:
73590b57cec5SDimitry Andric       break;
73600b57cec5SDimitry Andric     }
73610b57cec5SDimitry Andric   }
73620b57cec5SDimitry Andric 
73630b57cec5SDimitry Andric   // Known has the width of the source operand(s). Adjust if needed to match
73640b57cec5SDimitry Andric   // the passed bitwidth.
73650b57cec5SDimitry Andric   if (Known.getBitWidth() != BitWidth)
73665ffd83dbSDimitry Andric     Known = Known.anyextOrTrunc(BitWidth);
73670b57cec5SDimitry Andric }
73680b57cec5SDimitry Andric 
73690b57cec5SDimitry Andric static unsigned computeNumSignBitsBinOp(SDValue Op, const APInt &DemandedElts,
73700b57cec5SDimitry Andric                                         const SelectionDAG &DAG, unsigned Depth,
73710b57cec5SDimitry Andric                                         unsigned OpNo) {
73720b57cec5SDimitry Andric   APInt Src0DemE = getDemandedSrcElements(Op, DemandedElts, OpNo);
73730b57cec5SDimitry Andric   unsigned LHS = DAG.ComputeNumSignBits(Op.getOperand(OpNo), Src0DemE, Depth + 1);
73740b57cec5SDimitry Andric   if (LHS == 1) return 1; // Early out.
73750b57cec5SDimitry Andric   APInt Src1DemE = getDemandedSrcElements(Op, DemandedElts, OpNo + 1);
73760b57cec5SDimitry Andric   unsigned RHS = DAG.ComputeNumSignBits(Op.getOperand(OpNo + 1), Src1DemE, Depth + 1);
73770b57cec5SDimitry Andric   if (RHS == 1) return 1; // Early out.
73780b57cec5SDimitry Andric   unsigned Common = std::min(LHS, RHS);
73790b57cec5SDimitry Andric   unsigned SrcBitWidth = Op.getOperand(OpNo).getScalarValueSizeInBits();
73800b57cec5SDimitry Andric   EVT VT = Op.getValueType();
73810b57cec5SDimitry Andric   unsigned VTBits = VT.getScalarSizeInBits();
73820b57cec5SDimitry Andric   if (SrcBitWidth > VTBits) { // PACK
73830b57cec5SDimitry Andric     unsigned SrcExtraBits = SrcBitWidth - VTBits;
73840b57cec5SDimitry Andric     if (Common > SrcExtraBits)
73850b57cec5SDimitry Andric       return (Common - SrcExtraBits);
73860b57cec5SDimitry Andric     return 1;
73870b57cec5SDimitry Andric   }
73880b57cec5SDimitry Andric   assert (SrcBitWidth == VTBits && "Expected operands of same bitwidth.");
73890b57cec5SDimitry Andric   return Common;
73900b57cec5SDimitry Andric }
73910b57cec5SDimitry Andric 
73920b57cec5SDimitry Andric unsigned
73930b57cec5SDimitry Andric SystemZTargetLowering::ComputeNumSignBitsForTargetNode(
73940b57cec5SDimitry Andric     SDValue Op, const APInt &DemandedElts, const SelectionDAG &DAG,
73950b57cec5SDimitry Andric     unsigned Depth) const {
73960b57cec5SDimitry Andric   if (Op.getResNo() != 0)
73970b57cec5SDimitry Andric     return 1;
73980b57cec5SDimitry Andric   unsigned Opcode = Op.getOpcode();
73990b57cec5SDimitry Andric   if (Opcode == ISD::INTRINSIC_WO_CHAIN) {
74000b57cec5SDimitry Andric     unsigned Id = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
74010b57cec5SDimitry Andric     switch (Id) {
74020b57cec5SDimitry Andric     case Intrinsic::s390_vpksh:   // PACKS
74030b57cec5SDimitry Andric     case Intrinsic::s390_vpksf:
74040b57cec5SDimitry Andric     case Intrinsic::s390_vpksg:
74050b57cec5SDimitry Andric     case Intrinsic::s390_vpkshs:  // PACKS_CC
74060b57cec5SDimitry Andric     case Intrinsic::s390_vpksfs:
74070b57cec5SDimitry Andric     case Intrinsic::s390_vpksgs:
74080b57cec5SDimitry Andric     case Intrinsic::s390_vpklsh:  // PACKLS
74090b57cec5SDimitry Andric     case Intrinsic::s390_vpklsf:
74100b57cec5SDimitry Andric     case Intrinsic::s390_vpklsg:
74110b57cec5SDimitry Andric     case Intrinsic::s390_vpklshs: // PACKLS_CC
74120b57cec5SDimitry Andric     case Intrinsic::s390_vpklsfs:
74130b57cec5SDimitry Andric     case Intrinsic::s390_vpklsgs:
74140b57cec5SDimitry Andric     case Intrinsic::s390_vpdi:
74150b57cec5SDimitry Andric     case Intrinsic::s390_vsldb:
74160b57cec5SDimitry Andric     case Intrinsic::s390_vperm:
74170b57cec5SDimitry Andric       return computeNumSignBitsBinOp(Op, DemandedElts, DAG, Depth, 1);
74180b57cec5SDimitry Andric     case Intrinsic::s390_vuphb:  // VECTOR UNPACK HIGH
74190b57cec5SDimitry Andric     case Intrinsic::s390_vuphh:
74200b57cec5SDimitry Andric     case Intrinsic::s390_vuphf:
74210b57cec5SDimitry Andric     case Intrinsic::s390_vuplb:  // VECTOR UNPACK LOW
74220b57cec5SDimitry Andric     case Intrinsic::s390_vuplhw:
74230b57cec5SDimitry Andric     case Intrinsic::s390_vuplf: {
74240b57cec5SDimitry Andric       SDValue PackedOp = Op.getOperand(1);
74250b57cec5SDimitry Andric       APInt SrcDemE = getDemandedSrcElements(Op, DemandedElts, 1);
74260b57cec5SDimitry Andric       unsigned Tmp = DAG.ComputeNumSignBits(PackedOp, SrcDemE, Depth + 1);
74270b57cec5SDimitry Andric       EVT VT = Op.getValueType();
74280b57cec5SDimitry Andric       unsigned VTBits = VT.getScalarSizeInBits();
74290b57cec5SDimitry Andric       Tmp += VTBits - PackedOp.getScalarValueSizeInBits();
74300b57cec5SDimitry Andric       return Tmp;
74310b57cec5SDimitry Andric     }
74320b57cec5SDimitry Andric     default:
74330b57cec5SDimitry Andric       break;
74340b57cec5SDimitry Andric     }
74350b57cec5SDimitry Andric   } else {
74360b57cec5SDimitry Andric     switch (Opcode) {
74370b57cec5SDimitry Andric     case SystemZISD::SELECT_CCMASK:
74380b57cec5SDimitry Andric       return computeNumSignBitsBinOp(Op, DemandedElts, DAG, Depth, 0);
74390b57cec5SDimitry Andric     default:
74400b57cec5SDimitry Andric       break;
74410b57cec5SDimitry Andric     }
74420b57cec5SDimitry Andric   }
74430b57cec5SDimitry Andric 
74440b57cec5SDimitry Andric   return 1;
74450b57cec5SDimitry Andric }
74460b57cec5SDimitry Andric 
74475ffd83dbSDimitry Andric unsigned
7448bdd1243dSDimitry Andric SystemZTargetLowering::getStackProbeSize(const MachineFunction &MF) const {
74495ffd83dbSDimitry Andric   const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
74505ffd83dbSDimitry Andric   unsigned StackAlign = TFI->getStackAlignment();
74515ffd83dbSDimitry Andric   assert(StackAlign >=1 && isPowerOf2_32(StackAlign) &&
74525ffd83dbSDimitry Andric          "Unexpected stack alignment");
74535ffd83dbSDimitry Andric   // The default stack probe size is 4096 if the function has no
74545ffd83dbSDimitry Andric   // stack-probe-size attribute.
7455bdd1243dSDimitry Andric   unsigned StackProbeSize =
7456bdd1243dSDimitry Andric       MF.getFunction().getFnAttributeAsParsedInteger("stack-probe-size", 4096);
74575ffd83dbSDimitry Andric   // Round down to the stack alignment.
74585ffd83dbSDimitry Andric   StackProbeSize &= ~(StackAlign - 1);
74595ffd83dbSDimitry Andric   return StackProbeSize ? StackProbeSize : StackAlign;
74605ffd83dbSDimitry Andric }
74615ffd83dbSDimitry Andric 
74620b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
74630b57cec5SDimitry Andric // Custom insertion
74640b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
74650b57cec5SDimitry Andric 
74660b57cec5SDimitry Andric // Force base value Base into a register before MI.  Return the register.
74670b57cec5SDimitry Andric static Register forceReg(MachineInstr &MI, MachineOperand &Base,
74680b57cec5SDimitry Andric                          const SystemZInstrInfo *TII) {
74690b57cec5SDimitry Andric   MachineBasicBlock *MBB = MI.getParent();
74700b57cec5SDimitry Andric   MachineFunction &MF = *MBB->getParent();
74710b57cec5SDimitry Andric   MachineRegisterInfo &MRI = MF.getRegInfo();
74720b57cec5SDimitry Andric 
7473349cc55cSDimitry Andric   if (Base.isReg()) {
7474349cc55cSDimitry Andric     // Copy Base into a new virtual register to help register coalescing in
7475349cc55cSDimitry Andric     // cases with multiple uses.
7476349cc55cSDimitry Andric     Register Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
7477349cc55cSDimitry Andric     BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::COPY), Reg)
7478349cc55cSDimitry Andric       .add(Base);
7479349cc55cSDimitry Andric     return Reg;
7480349cc55cSDimitry Andric   }
7481349cc55cSDimitry Andric 
74820b57cec5SDimitry Andric   Register Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
74830b57cec5SDimitry Andric   BuildMI(*MBB, MI, MI.getDebugLoc(), TII->get(SystemZ::LA), Reg)
74840b57cec5SDimitry Andric       .add(Base)
74850b57cec5SDimitry Andric       .addImm(0)
74860b57cec5SDimitry Andric       .addReg(0);
74870b57cec5SDimitry Andric   return Reg;
74880b57cec5SDimitry Andric }
74890b57cec5SDimitry Andric 
74900b57cec5SDimitry Andric // The CC operand of MI might be missing a kill marker because there
74910b57cec5SDimitry Andric // were multiple uses of CC, and ISel didn't know which to mark.
74920b57cec5SDimitry Andric // Figure out whether MI should have had a kill marker.
74930b57cec5SDimitry Andric static bool checkCCKill(MachineInstr &MI, MachineBasicBlock *MBB) {
74940b57cec5SDimitry Andric   // Scan forward through BB for a use/def of CC.
74950b57cec5SDimitry Andric   MachineBasicBlock::iterator miI(std::next(MachineBasicBlock::iterator(MI)));
74960b57cec5SDimitry Andric   for (MachineBasicBlock::iterator miE = MBB->end(); miI != miE; ++miI) {
74970b57cec5SDimitry Andric     const MachineInstr& mi = *miI;
74980b57cec5SDimitry Andric     if (mi.readsRegister(SystemZ::CC))
74990b57cec5SDimitry Andric       return false;
75000b57cec5SDimitry Andric     if (mi.definesRegister(SystemZ::CC))
75010b57cec5SDimitry Andric       break; // Should have kill-flag - update below.
75020b57cec5SDimitry Andric   }
75030b57cec5SDimitry Andric 
75040b57cec5SDimitry Andric   // If we hit the end of the block, check whether CC is live into a
75050b57cec5SDimitry Andric   // successor.
75060b57cec5SDimitry Andric   if (miI == MBB->end()) {
7507349cc55cSDimitry Andric     for (const MachineBasicBlock *Succ : MBB->successors())
7508349cc55cSDimitry Andric       if (Succ->isLiveIn(SystemZ::CC))
75090b57cec5SDimitry Andric         return false;
75100b57cec5SDimitry Andric   }
75110b57cec5SDimitry Andric 
75120b57cec5SDimitry Andric   return true;
75130b57cec5SDimitry Andric }
75140b57cec5SDimitry Andric 
75150b57cec5SDimitry Andric // Return true if it is OK for this Select pseudo-opcode to be cascaded
75160b57cec5SDimitry Andric // together with other Select pseudo-opcodes into a single basic-block with
75170b57cec5SDimitry Andric // a conditional jump around it.
75180b57cec5SDimitry Andric static bool isSelectPseudo(MachineInstr &MI) {
75190b57cec5SDimitry Andric   switch (MI.getOpcode()) {
75200b57cec5SDimitry Andric   case SystemZ::Select32:
75210b57cec5SDimitry Andric   case SystemZ::Select64:
75220b57cec5SDimitry Andric   case SystemZ::SelectF32:
75230b57cec5SDimitry Andric   case SystemZ::SelectF64:
75240b57cec5SDimitry Andric   case SystemZ::SelectF128:
75250b57cec5SDimitry Andric   case SystemZ::SelectVR32:
75260b57cec5SDimitry Andric   case SystemZ::SelectVR64:
75270b57cec5SDimitry Andric   case SystemZ::SelectVR128:
75280b57cec5SDimitry Andric     return true;
75290b57cec5SDimitry Andric 
75300b57cec5SDimitry Andric   default:
75310b57cec5SDimitry Andric     return false;
75320b57cec5SDimitry Andric   }
75330b57cec5SDimitry Andric }
75340b57cec5SDimitry Andric 
75350b57cec5SDimitry Andric // Helper function, which inserts PHI functions into SinkMBB:
75360b57cec5SDimitry Andric //   %Result(i) = phi [ %FalseValue(i), FalseMBB ], [ %TrueValue(i), TrueMBB ],
75378bcb0991SDimitry Andric // where %FalseValue(i) and %TrueValue(i) are taken from Selects.
75388bcb0991SDimitry Andric static void createPHIsForSelects(SmallVector<MachineInstr*, 8> &Selects,
75390b57cec5SDimitry Andric                                  MachineBasicBlock *TrueMBB,
75400b57cec5SDimitry Andric                                  MachineBasicBlock *FalseMBB,
75410b57cec5SDimitry Andric                                  MachineBasicBlock *SinkMBB) {
75420b57cec5SDimitry Andric   MachineFunction *MF = TrueMBB->getParent();
75430b57cec5SDimitry Andric   const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
75440b57cec5SDimitry Andric 
75458bcb0991SDimitry Andric   MachineInstr *FirstMI = Selects.front();
75468bcb0991SDimitry Andric   unsigned CCValid = FirstMI->getOperand(3).getImm();
75478bcb0991SDimitry Andric   unsigned CCMask = FirstMI->getOperand(4).getImm();
75480b57cec5SDimitry Andric 
75490b57cec5SDimitry Andric   MachineBasicBlock::iterator SinkInsertionPoint = SinkMBB->begin();
75500b57cec5SDimitry Andric 
75510b57cec5SDimitry Andric   // As we are creating the PHIs, we have to be careful if there is more than
75520b57cec5SDimitry Andric   // one.  Later Selects may reference the results of earlier Selects, but later
75530b57cec5SDimitry Andric   // PHIs have to reference the individual true/false inputs from earlier PHIs.
75540b57cec5SDimitry Andric   // That also means that PHI construction must work forward from earlier to
75550b57cec5SDimitry Andric   // later, and that the code must maintain a mapping from earlier PHI's
75560b57cec5SDimitry Andric   // destination registers, and the registers that went into the PHI.
75570b57cec5SDimitry Andric   DenseMap<unsigned, std::pair<unsigned, unsigned>> RegRewriteTable;
75580b57cec5SDimitry Andric 
7559bdd1243dSDimitry Andric   for (auto *MI : Selects) {
75608bcb0991SDimitry Andric     Register DestReg = MI->getOperand(0).getReg();
75618bcb0991SDimitry Andric     Register TrueReg = MI->getOperand(1).getReg();
75628bcb0991SDimitry Andric     Register FalseReg = MI->getOperand(2).getReg();
75630b57cec5SDimitry Andric 
75640b57cec5SDimitry Andric     // If this Select we are generating is the opposite condition from
75650b57cec5SDimitry Andric     // the jump we generated, then we have to swap the operands for the
75660b57cec5SDimitry Andric     // PHI that is going to be generated.
75678bcb0991SDimitry Andric     if (MI->getOperand(4).getImm() == (CCValid ^ CCMask))
75680b57cec5SDimitry Andric       std::swap(TrueReg, FalseReg);
75690b57cec5SDimitry Andric 
75700b57cec5SDimitry Andric     if (RegRewriteTable.find(TrueReg) != RegRewriteTable.end())
75710b57cec5SDimitry Andric       TrueReg = RegRewriteTable[TrueReg].first;
75720b57cec5SDimitry Andric 
75730b57cec5SDimitry Andric     if (RegRewriteTable.find(FalseReg) != RegRewriteTable.end())
75740b57cec5SDimitry Andric       FalseReg = RegRewriteTable[FalseReg].second;
75750b57cec5SDimitry Andric 
75768bcb0991SDimitry Andric     DebugLoc DL = MI->getDebugLoc();
75770b57cec5SDimitry Andric     BuildMI(*SinkMBB, SinkInsertionPoint, DL, TII->get(SystemZ::PHI), DestReg)
75780b57cec5SDimitry Andric       .addReg(TrueReg).addMBB(TrueMBB)
75790b57cec5SDimitry Andric       .addReg(FalseReg).addMBB(FalseMBB);
75800b57cec5SDimitry Andric 
75810b57cec5SDimitry Andric     // Add this PHI to the rewrite table.
75820b57cec5SDimitry Andric     RegRewriteTable[DestReg] = std::make_pair(TrueReg, FalseReg);
75830b57cec5SDimitry Andric   }
75840b57cec5SDimitry Andric 
75850b57cec5SDimitry Andric   MF->getProperties().reset(MachineFunctionProperties::Property::NoPHIs);
75860b57cec5SDimitry Andric }
75870b57cec5SDimitry Andric 
75880b57cec5SDimitry Andric // Implement EmitInstrWithCustomInserter for pseudo Select* instruction MI.
75890b57cec5SDimitry Andric MachineBasicBlock *
75900b57cec5SDimitry Andric SystemZTargetLowering::emitSelect(MachineInstr &MI,
75910b57cec5SDimitry Andric                                   MachineBasicBlock *MBB) const {
75928bcb0991SDimitry Andric   assert(isSelectPseudo(MI) && "Bad call to emitSelect()");
759381ad6265SDimitry Andric   const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
75940b57cec5SDimitry Andric 
75950b57cec5SDimitry Andric   unsigned CCValid = MI.getOperand(3).getImm();
75960b57cec5SDimitry Andric   unsigned CCMask = MI.getOperand(4).getImm();
75970b57cec5SDimitry Andric 
75980b57cec5SDimitry Andric   // If we have a sequence of Select* pseudo instructions using the
75990b57cec5SDimitry Andric   // same condition code value, we want to expand all of them into
76000b57cec5SDimitry Andric   // a single pair of basic blocks using the same condition.
76018bcb0991SDimitry Andric   SmallVector<MachineInstr*, 8> Selects;
76028bcb0991SDimitry Andric   SmallVector<MachineInstr*, 8> DbgValues;
76038bcb0991SDimitry Andric   Selects.push_back(&MI);
76048bcb0991SDimitry Andric   unsigned Count = 0;
7605bdd1243dSDimitry Andric   for (MachineInstr &NextMI : llvm::make_range(
7606bdd1243dSDimitry Andric            std::next(MachineBasicBlock::iterator(MI)), MBB->end())) {
7607bdd1243dSDimitry Andric     if (isSelectPseudo(NextMI)) {
7608bdd1243dSDimitry Andric       assert(NextMI.getOperand(3).getImm() == CCValid &&
76098bcb0991SDimitry Andric              "Bad CCValid operands since CC was not redefined.");
7610bdd1243dSDimitry Andric       if (NextMI.getOperand(4).getImm() == CCMask ||
7611bdd1243dSDimitry Andric           NextMI.getOperand(4).getImm() == (CCValid ^ CCMask)) {
7612bdd1243dSDimitry Andric         Selects.push_back(&NextMI);
76138bcb0991SDimitry Andric         continue;
76148bcb0991SDimitry Andric       }
76158bcb0991SDimitry Andric       break;
76168bcb0991SDimitry Andric     }
7617bdd1243dSDimitry Andric     if (NextMI.definesRegister(SystemZ::CC) || NextMI.usesCustomInsertionHook())
761813138422SDimitry Andric       break;
76198bcb0991SDimitry Andric     bool User = false;
7620bdd1243dSDimitry Andric     for (auto *SelMI : Selects)
7621bdd1243dSDimitry Andric       if (NextMI.readsVirtualRegister(SelMI->getOperand(0).getReg())) {
76228bcb0991SDimitry Andric         User = true;
76238bcb0991SDimitry Andric         break;
76248bcb0991SDimitry Andric       }
7625bdd1243dSDimitry Andric     if (NextMI.isDebugInstr()) {
76268bcb0991SDimitry Andric       if (User) {
7627bdd1243dSDimitry Andric         assert(NextMI.isDebugValue() && "Unhandled debug opcode.");
7628bdd1243dSDimitry Andric         DbgValues.push_back(&NextMI);
76298bcb0991SDimitry Andric       }
7630bdd1243dSDimitry Andric     } else if (User || ++Count > 20)
76318bcb0991SDimitry Andric       break;
76320b57cec5SDimitry Andric   }
76330b57cec5SDimitry Andric 
76348bcb0991SDimitry Andric   MachineInstr *LastMI = Selects.back();
76358bcb0991SDimitry Andric   bool CCKilled =
76368bcb0991SDimitry Andric       (LastMI->killsRegister(SystemZ::CC) || checkCCKill(*LastMI, MBB));
76370b57cec5SDimitry Andric   MachineBasicBlock *StartMBB = MBB;
76385ffd83dbSDimitry Andric   MachineBasicBlock *JoinMBB  = SystemZ::splitBlockAfter(LastMI, MBB);
76395ffd83dbSDimitry Andric   MachineBasicBlock *FalseMBB = SystemZ::emitBlockAfter(StartMBB);
76400b57cec5SDimitry Andric 
76410b57cec5SDimitry Andric   // Unless CC was killed in the last Select instruction, mark it as
76420b57cec5SDimitry Andric   // live-in to both FalseMBB and JoinMBB.
76438bcb0991SDimitry Andric   if (!CCKilled) {
76440b57cec5SDimitry Andric     FalseMBB->addLiveIn(SystemZ::CC);
76450b57cec5SDimitry Andric     JoinMBB->addLiveIn(SystemZ::CC);
76460b57cec5SDimitry Andric   }
76470b57cec5SDimitry Andric 
76480b57cec5SDimitry Andric   //  StartMBB:
76490b57cec5SDimitry Andric   //   BRC CCMask, JoinMBB
76500b57cec5SDimitry Andric   //   # fallthrough to FalseMBB
76510b57cec5SDimitry Andric   MBB = StartMBB;
76528bcb0991SDimitry Andric   BuildMI(MBB, MI.getDebugLoc(), TII->get(SystemZ::BRC))
76530b57cec5SDimitry Andric     .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
76540b57cec5SDimitry Andric   MBB->addSuccessor(JoinMBB);
76550b57cec5SDimitry Andric   MBB->addSuccessor(FalseMBB);
76560b57cec5SDimitry Andric 
76570b57cec5SDimitry Andric   //  FalseMBB:
76580b57cec5SDimitry Andric   //   # fallthrough to JoinMBB
76590b57cec5SDimitry Andric   MBB = FalseMBB;
76600b57cec5SDimitry Andric   MBB->addSuccessor(JoinMBB);
76610b57cec5SDimitry Andric 
76620b57cec5SDimitry Andric   //  JoinMBB:
76630b57cec5SDimitry Andric   //   %Result = phi [ %FalseReg, FalseMBB ], [ %TrueReg, StartMBB ]
76640b57cec5SDimitry Andric   //  ...
76650b57cec5SDimitry Andric   MBB = JoinMBB;
76668bcb0991SDimitry Andric   createPHIsForSelects(Selects, StartMBB, FalseMBB, MBB);
7667bdd1243dSDimitry Andric   for (auto *SelMI : Selects)
76688bcb0991SDimitry Andric     SelMI->eraseFromParent();
76690b57cec5SDimitry Andric 
76708bcb0991SDimitry Andric   MachineBasicBlock::iterator InsertPos = MBB->getFirstNonPHI();
7671bdd1243dSDimitry Andric   for (auto *DbgMI : DbgValues)
76728bcb0991SDimitry Andric     MBB->splice(InsertPos, StartMBB, DbgMI);
76738bcb0991SDimitry Andric 
76740b57cec5SDimitry Andric   return JoinMBB;
76750b57cec5SDimitry Andric }
76760b57cec5SDimitry Andric 
76770b57cec5SDimitry Andric // Implement EmitInstrWithCustomInserter for pseudo CondStore* instruction MI.
76780b57cec5SDimitry Andric // StoreOpcode is the store to use and Invert says whether the store should
76790b57cec5SDimitry Andric // happen when the condition is false rather than true.  If a STORE ON
76800b57cec5SDimitry Andric // CONDITION is available, STOCOpcode is its opcode, otherwise it is 0.
76810b57cec5SDimitry Andric MachineBasicBlock *SystemZTargetLowering::emitCondStore(MachineInstr &MI,
76820b57cec5SDimitry Andric                                                         MachineBasicBlock *MBB,
76830b57cec5SDimitry Andric                                                         unsigned StoreOpcode,
76840b57cec5SDimitry Andric                                                         unsigned STOCOpcode,
76850b57cec5SDimitry Andric                                                         bool Invert) const {
768681ad6265SDimitry Andric   const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
76870b57cec5SDimitry Andric 
76888bcb0991SDimitry Andric   Register SrcReg = MI.getOperand(0).getReg();
76890b57cec5SDimitry Andric   MachineOperand Base = MI.getOperand(1);
76900b57cec5SDimitry Andric   int64_t Disp = MI.getOperand(2).getImm();
76918bcb0991SDimitry Andric   Register IndexReg = MI.getOperand(3).getReg();
76920b57cec5SDimitry Andric   unsigned CCValid = MI.getOperand(4).getImm();
76930b57cec5SDimitry Andric   unsigned CCMask = MI.getOperand(5).getImm();
76940b57cec5SDimitry Andric   DebugLoc DL = MI.getDebugLoc();
76950b57cec5SDimitry Andric 
76960b57cec5SDimitry Andric   StoreOpcode = TII->getOpcodeForOffset(StoreOpcode, Disp);
76970b57cec5SDimitry Andric 
76980b57cec5SDimitry Andric   // ISel pattern matching also adds a load memory operand of the same
76990b57cec5SDimitry Andric   // address, so take special care to find the storing memory operand.
77000b57cec5SDimitry Andric   MachineMemOperand *MMO = nullptr;
77010b57cec5SDimitry Andric   for (auto *I : MI.memoperands())
77020b57cec5SDimitry Andric     if (I->isStore()) {
77030b57cec5SDimitry Andric       MMO = I;
77040b57cec5SDimitry Andric       break;
77050b57cec5SDimitry Andric     }
77060b57cec5SDimitry Andric 
7707e8d8bef9SDimitry Andric   // Use STOCOpcode if possible.  We could use different store patterns in
7708e8d8bef9SDimitry Andric   // order to avoid matching the index register, but the performance trade-offs
7709e8d8bef9SDimitry Andric   // might be more complicated in that case.
7710e8d8bef9SDimitry Andric   if (STOCOpcode && !IndexReg && Subtarget.hasLoadStoreOnCond()) {
7711e8d8bef9SDimitry Andric     if (Invert)
7712e8d8bef9SDimitry Andric       CCMask ^= CCValid;
7713e8d8bef9SDimitry Andric 
77140b57cec5SDimitry Andric     BuildMI(*MBB, MI, DL, TII->get(STOCOpcode))
77150b57cec5SDimitry Andric       .addReg(SrcReg)
77160b57cec5SDimitry Andric       .add(Base)
77170b57cec5SDimitry Andric       .addImm(Disp)
77180b57cec5SDimitry Andric       .addImm(CCValid)
77190b57cec5SDimitry Andric       .addImm(CCMask)
77200b57cec5SDimitry Andric       .addMemOperand(MMO);
77210b57cec5SDimitry Andric 
77220b57cec5SDimitry Andric     MI.eraseFromParent();
77230b57cec5SDimitry Andric     return MBB;
77240b57cec5SDimitry Andric   }
77250b57cec5SDimitry Andric 
77260b57cec5SDimitry Andric   // Get the condition needed to branch around the store.
77270b57cec5SDimitry Andric   if (!Invert)
77280b57cec5SDimitry Andric     CCMask ^= CCValid;
77290b57cec5SDimitry Andric 
77300b57cec5SDimitry Andric   MachineBasicBlock *StartMBB = MBB;
77315ffd83dbSDimitry Andric   MachineBasicBlock *JoinMBB  = SystemZ::splitBlockBefore(MI, MBB);
77325ffd83dbSDimitry Andric   MachineBasicBlock *FalseMBB = SystemZ::emitBlockAfter(StartMBB);
77330b57cec5SDimitry Andric 
77340b57cec5SDimitry Andric   // Unless CC was killed in the CondStore instruction, mark it as
77350b57cec5SDimitry Andric   // live-in to both FalseMBB and JoinMBB.
77360b57cec5SDimitry Andric   if (!MI.killsRegister(SystemZ::CC) && !checkCCKill(MI, JoinMBB)) {
77370b57cec5SDimitry Andric     FalseMBB->addLiveIn(SystemZ::CC);
77380b57cec5SDimitry Andric     JoinMBB->addLiveIn(SystemZ::CC);
77390b57cec5SDimitry Andric   }
77400b57cec5SDimitry Andric 
77410b57cec5SDimitry Andric   //  StartMBB:
77420b57cec5SDimitry Andric   //   BRC CCMask, JoinMBB
77430b57cec5SDimitry Andric   //   # fallthrough to FalseMBB
77440b57cec5SDimitry Andric   MBB = StartMBB;
77450b57cec5SDimitry Andric   BuildMI(MBB, DL, TII->get(SystemZ::BRC))
77460b57cec5SDimitry Andric     .addImm(CCValid).addImm(CCMask).addMBB(JoinMBB);
77470b57cec5SDimitry Andric   MBB->addSuccessor(JoinMBB);
77480b57cec5SDimitry Andric   MBB->addSuccessor(FalseMBB);
77490b57cec5SDimitry Andric 
77500b57cec5SDimitry Andric   //  FalseMBB:
77510b57cec5SDimitry Andric   //   store %SrcReg, %Disp(%Index,%Base)
77520b57cec5SDimitry Andric   //   # fallthrough to JoinMBB
77530b57cec5SDimitry Andric   MBB = FalseMBB;
77540b57cec5SDimitry Andric   BuildMI(MBB, DL, TII->get(StoreOpcode))
77550b57cec5SDimitry Andric       .addReg(SrcReg)
77560b57cec5SDimitry Andric       .add(Base)
77570b57cec5SDimitry Andric       .addImm(Disp)
7758e8d8bef9SDimitry Andric       .addReg(IndexReg)
7759e8d8bef9SDimitry Andric       .addMemOperand(MMO);
77600b57cec5SDimitry Andric   MBB->addSuccessor(JoinMBB);
77610b57cec5SDimitry Andric 
77620b57cec5SDimitry Andric   MI.eraseFromParent();
77630b57cec5SDimitry Andric   return JoinMBB;
77640b57cec5SDimitry Andric }
77650b57cec5SDimitry Andric 
77660b57cec5SDimitry Andric // Implement EmitInstrWithCustomInserter for pseudo ATOMIC_LOAD{,W}_*
77670b57cec5SDimitry Andric // or ATOMIC_SWAP{,W} instruction MI.  BinOpcode is the instruction that
77680b57cec5SDimitry Andric // performs the binary operation elided by "*", or 0 for ATOMIC_SWAP{,W}.
77690b57cec5SDimitry Andric // BitSize is the width of the field in bits, or 0 if this is a partword
77700b57cec5SDimitry Andric // ATOMIC_LOADW_* or ATOMIC_SWAPW instruction, in which case the bitsize
77710b57cec5SDimitry Andric // is one of the operands.  Invert says whether the field should be
77720b57cec5SDimitry Andric // inverted after performing BinOpcode (e.g. for NAND).
77730b57cec5SDimitry Andric MachineBasicBlock *SystemZTargetLowering::emitAtomicLoadBinary(
77740b57cec5SDimitry Andric     MachineInstr &MI, MachineBasicBlock *MBB, unsigned BinOpcode,
77750b57cec5SDimitry Andric     unsigned BitSize, bool Invert) const {
77760b57cec5SDimitry Andric   MachineFunction &MF = *MBB->getParent();
777781ad6265SDimitry Andric   const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
77780b57cec5SDimitry Andric   MachineRegisterInfo &MRI = MF.getRegInfo();
77790b57cec5SDimitry Andric   bool IsSubWord = (BitSize < 32);
77800b57cec5SDimitry Andric 
77810b57cec5SDimitry Andric   // Extract the operands.  Base can be a register or a frame index.
77820b57cec5SDimitry Andric   // Src2 can be a register or immediate.
77838bcb0991SDimitry Andric   Register Dest = MI.getOperand(0).getReg();
77840b57cec5SDimitry Andric   MachineOperand Base = earlyUseOperand(MI.getOperand(1));
77850b57cec5SDimitry Andric   int64_t Disp = MI.getOperand(2).getImm();
77860b57cec5SDimitry Andric   MachineOperand Src2 = earlyUseOperand(MI.getOperand(3));
77870b57cec5SDimitry Andric   Register BitShift = IsSubWord ? MI.getOperand(4).getReg() : Register();
77880b57cec5SDimitry Andric   Register NegBitShift = IsSubWord ? MI.getOperand(5).getReg() : Register();
77890b57cec5SDimitry Andric   DebugLoc DL = MI.getDebugLoc();
77900b57cec5SDimitry Andric   if (IsSubWord)
77910b57cec5SDimitry Andric     BitSize = MI.getOperand(6).getImm();
77920b57cec5SDimitry Andric 
77930b57cec5SDimitry Andric   // Subword operations use 32-bit registers.
77940b57cec5SDimitry Andric   const TargetRegisterClass *RC = (BitSize <= 32 ?
77950b57cec5SDimitry Andric                                    &SystemZ::GR32BitRegClass :
77960b57cec5SDimitry Andric                                    &SystemZ::GR64BitRegClass);
77970b57cec5SDimitry Andric   unsigned LOpcode  = BitSize <= 32 ? SystemZ::L  : SystemZ::LG;
77980b57cec5SDimitry Andric   unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
77990b57cec5SDimitry Andric 
78000b57cec5SDimitry Andric   // Get the right opcodes for the displacement.
78010b57cec5SDimitry Andric   LOpcode  = TII->getOpcodeForOffset(LOpcode,  Disp);
78020b57cec5SDimitry Andric   CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
78030b57cec5SDimitry Andric   assert(LOpcode && CSOpcode && "Displacement out of range");
78040b57cec5SDimitry Andric 
78050b57cec5SDimitry Andric   // Create virtual registers for temporary results.
78060b57cec5SDimitry Andric   Register OrigVal       = MRI.createVirtualRegister(RC);
78070b57cec5SDimitry Andric   Register OldVal        = MRI.createVirtualRegister(RC);
78080b57cec5SDimitry Andric   Register NewVal        = (BinOpcode || IsSubWord ?
78090b57cec5SDimitry Andric                             MRI.createVirtualRegister(RC) : Src2.getReg());
78100b57cec5SDimitry Andric   Register RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
78110b57cec5SDimitry Andric   Register RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
78120b57cec5SDimitry Andric 
78130b57cec5SDimitry Andric   // Insert a basic block for the main loop.
78140b57cec5SDimitry Andric   MachineBasicBlock *StartMBB = MBB;
78155ffd83dbSDimitry Andric   MachineBasicBlock *DoneMBB  = SystemZ::splitBlockBefore(MI, MBB);
78165ffd83dbSDimitry Andric   MachineBasicBlock *LoopMBB  = SystemZ::emitBlockAfter(StartMBB);
78170b57cec5SDimitry Andric 
78180b57cec5SDimitry Andric   //  StartMBB:
78190b57cec5SDimitry Andric   //   ...
78200b57cec5SDimitry Andric   //   %OrigVal = L Disp(%Base)
7821fe6060f1SDimitry Andric   //   # fall through to LoopMBB
78220b57cec5SDimitry Andric   MBB = StartMBB;
78230b57cec5SDimitry Andric   BuildMI(MBB, DL, TII->get(LOpcode), OrigVal).add(Base).addImm(Disp).addReg(0);
78240b57cec5SDimitry Andric   MBB->addSuccessor(LoopMBB);
78250b57cec5SDimitry Andric 
78260b57cec5SDimitry Andric   //  LoopMBB:
78270b57cec5SDimitry Andric   //   %OldVal        = phi [ %OrigVal, StartMBB ], [ %Dest, LoopMBB ]
78280b57cec5SDimitry Andric   //   %RotatedOldVal = RLL %OldVal, 0(%BitShift)
78290b57cec5SDimitry Andric   //   %RotatedNewVal = OP %RotatedOldVal, %Src2
78300b57cec5SDimitry Andric   //   %NewVal        = RLL %RotatedNewVal, 0(%NegBitShift)
78310b57cec5SDimitry Andric   //   %Dest          = CS %OldVal, %NewVal, Disp(%Base)
78320b57cec5SDimitry Andric   //   JNE LoopMBB
7833fe6060f1SDimitry Andric   //   # fall through to DoneMBB
78340b57cec5SDimitry Andric   MBB = LoopMBB;
78350b57cec5SDimitry Andric   BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
78360b57cec5SDimitry Andric     .addReg(OrigVal).addMBB(StartMBB)
78370b57cec5SDimitry Andric     .addReg(Dest).addMBB(LoopMBB);
78380b57cec5SDimitry Andric   if (IsSubWord)
78390b57cec5SDimitry Andric     BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
78400b57cec5SDimitry Andric       .addReg(OldVal).addReg(BitShift).addImm(0);
78410b57cec5SDimitry Andric   if (Invert) {
78420b57cec5SDimitry Andric     // Perform the operation normally and then invert every bit of the field.
78438bcb0991SDimitry Andric     Register Tmp = MRI.createVirtualRegister(RC);
78440b57cec5SDimitry Andric     BuildMI(MBB, DL, TII->get(BinOpcode), Tmp).addReg(RotatedOldVal).add(Src2);
78450b57cec5SDimitry Andric     if (BitSize <= 32)
78460b57cec5SDimitry Andric       // XILF with the upper BitSize bits set.
78470b57cec5SDimitry Andric       BuildMI(MBB, DL, TII->get(SystemZ::XILF), RotatedNewVal)
78480b57cec5SDimitry Andric         .addReg(Tmp).addImm(-1U << (32 - BitSize));
78490b57cec5SDimitry Andric     else {
78500b57cec5SDimitry Andric       // Use LCGR and add -1 to the result, which is more compact than
78510b57cec5SDimitry Andric       // an XILF, XILH pair.
78528bcb0991SDimitry Andric       Register Tmp2 = MRI.createVirtualRegister(RC);
78530b57cec5SDimitry Andric       BuildMI(MBB, DL, TII->get(SystemZ::LCGR), Tmp2).addReg(Tmp);
78540b57cec5SDimitry Andric       BuildMI(MBB, DL, TII->get(SystemZ::AGHI), RotatedNewVal)
78550b57cec5SDimitry Andric         .addReg(Tmp2).addImm(-1);
78560b57cec5SDimitry Andric     }
78570b57cec5SDimitry Andric   } else if (BinOpcode)
78580b57cec5SDimitry Andric     // A simply binary operation.
78590b57cec5SDimitry Andric     BuildMI(MBB, DL, TII->get(BinOpcode), RotatedNewVal)
78600b57cec5SDimitry Andric         .addReg(RotatedOldVal)
78610b57cec5SDimitry Andric         .add(Src2);
78620b57cec5SDimitry Andric   else if (IsSubWord)
78630b57cec5SDimitry Andric     // Use RISBG to rotate Src2 into position and use it to replace the
78640b57cec5SDimitry Andric     // field in RotatedOldVal.
78650b57cec5SDimitry Andric     BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedNewVal)
78660b57cec5SDimitry Andric       .addReg(RotatedOldVal).addReg(Src2.getReg())
78670b57cec5SDimitry Andric       .addImm(32).addImm(31 + BitSize).addImm(32 - BitSize);
78680b57cec5SDimitry Andric   if (IsSubWord)
78690b57cec5SDimitry Andric     BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
78700b57cec5SDimitry Andric       .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
78710b57cec5SDimitry Andric   BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
78720b57cec5SDimitry Andric       .addReg(OldVal)
78730b57cec5SDimitry Andric       .addReg(NewVal)
78740b57cec5SDimitry Andric       .add(Base)
78750b57cec5SDimitry Andric       .addImm(Disp);
78760b57cec5SDimitry Andric   BuildMI(MBB, DL, TII->get(SystemZ::BRC))
78770b57cec5SDimitry Andric     .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
78780b57cec5SDimitry Andric   MBB->addSuccessor(LoopMBB);
78790b57cec5SDimitry Andric   MBB->addSuccessor(DoneMBB);
78800b57cec5SDimitry Andric 
78810b57cec5SDimitry Andric   MI.eraseFromParent();
78820b57cec5SDimitry Andric   return DoneMBB;
78830b57cec5SDimitry Andric }
78840b57cec5SDimitry Andric 
78850b57cec5SDimitry Andric // Implement EmitInstrWithCustomInserter for pseudo
78860b57cec5SDimitry Andric // ATOMIC_LOAD{,W}_{,U}{MIN,MAX} instruction MI.  CompareOpcode is the
78870b57cec5SDimitry Andric // instruction that should be used to compare the current field with the
78880b57cec5SDimitry Andric // minimum or maximum value.  KeepOldMask is the BRC condition-code mask
78890b57cec5SDimitry Andric // for when the current field should be kept.  BitSize is the width of
78900b57cec5SDimitry Andric // the field in bits, or 0 if this is a partword ATOMIC_LOADW_* instruction.
78910b57cec5SDimitry Andric MachineBasicBlock *SystemZTargetLowering::emitAtomicLoadMinMax(
78920b57cec5SDimitry Andric     MachineInstr &MI, MachineBasicBlock *MBB, unsigned CompareOpcode,
78930b57cec5SDimitry Andric     unsigned KeepOldMask, unsigned BitSize) const {
78940b57cec5SDimitry Andric   MachineFunction &MF = *MBB->getParent();
789581ad6265SDimitry Andric   const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
78960b57cec5SDimitry Andric   MachineRegisterInfo &MRI = MF.getRegInfo();
78970b57cec5SDimitry Andric   bool IsSubWord = (BitSize < 32);
78980b57cec5SDimitry Andric 
78990b57cec5SDimitry Andric   // Extract the operands.  Base can be a register or a frame index.
79008bcb0991SDimitry Andric   Register Dest = MI.getOperand(0).getReg();
79010b57cec5SDimitry Andric   MachineOperand Base = earlyUseOperand(MI.getOperand(1));
79020b57cec5SDimitry Andric   int64_t Disp = MI.getOperand(2).getImm();
79030b57cec5SDimitry Andric   Register Src2 = MI.getOperand(3).getReg();
79040b57cec5SDimitry Andric   Register BitShift = (IsSubWord ? MI.getOperand(4).getReg() : Register());
79050b57cec5SDimitry Andric   Register NegBitShift = (IsSubWord ? MI.getOperand(5).getReg() : Register());
79060b57cec5SDimitry Andric   DebugLoc DL = MI.getDebugLoc();
79070b57cec5SDimitry Andric   if (IsSubWord)
79080b57cec5SDimitry Andric     BitSize = MI.getOperand(6).getImm();
79090b57cec5SDimitry Andric 
79100b57cec5SDimitry Andric   // Subword operations use 32-bit registers.
79110b57cec5SDimitry Andric   const TargetRegisterClass *RC = (BitSize <= 32 ?
79120b57cec5SDimitry Andric                                    &SystemZ::GR32BitRegClass :
79130b57cec5SDimitry Andric                                    &SystemZ::GR64BitRegClass);
79140b57cec5SDimitry Andric   unsigned LOpcode  = BitSize <= 32 ? SystemZ::L  : SystemZ::LG;
79150b57cec5SDimitry Andric   unsigned CSOpcode = BitSize <= 32 ? SystemZ::CS : SystemZ::CSG;
79160b57cec5SDimitry Andric 
79170b57cec5SDimitry Andric   // Get the right opcodes for the displacement.
79180b57cec5SDimitry Andric   LOpcode  = TII->getOpcodeForOffset(LOpcode,  Disp);
79190b57cec5SDimitry Andric   CSOpcode = TII->getOpcodeForOffset(CSOpcode, Disp);
79200b57cec5SDimitry Andric   assert(LOpcode && CSOpcode && "Displacement out of range");
79210b57cec5SDimitry Andric 
79220b57cec5SDimitry Andric   // Create virtual registers for temporary results.
79230b57cec5SDimitry Andric   Register OrigVal       = MRI.createVirtualRegister(RC);
79240b57cec5SDimitry Andric   Register OldVal        = MRI.createVirtualRegister(RC);
79250b57cec5SDimitry Andric   Register NewVal        = MRI.createVirtualRegister(RC);
79260b57cec5SDimitry Andric   Register RotatedOldVal = (IsSubWord ? MRI.createVirtualRegister(RC) : OldVal);
79270b57cec5SDimitry Andric   Register RotatedAltVal = (IsSubWord ? MRI.createVirtualRegister(RC) : Src2);
79280b57cec5SDimitry Andric   Register RotatedNewVal = (IsSubWord ? MRI.createVirtualRegister(RC) : NewVal);
79290b57cec5SDimitry Andric 
79300b57cec5SDimitry Andric   // Insert 3 basic blocks for the loop.
79310b57cec5SDimitry Andric   MachineBasicBlock *StartMBB  = MBB;
79325ffd83dbSDimitry Andric   MachineBasicBlock *DoneMBB   = SystemZ::splitBlockBefore(MI, MBB);
79335ffd83dbSDimitry Andric   MachineBasicBlock *LoopMBB   = SystemZ::emitBlockAfter(StartMBB);
79345ffd83dbSDimitry Andric   MachineBasicBlock *UseAltMBB = SystemZ::emitBlockAfter(LoopMBB);
79355ffd83dbSDimitry Andric   MachineBasicBlock *UpdateMBB = SystemZ::emitBlockAfter(UseAltMBB);
79360b57cec5SDimitry Andric 
79370b57cec5SDimitry Andric   //  StartMBB:
79380b57cec5SDimitry Andric   //   ...
79390b57cec5SDimitry Andric   //   %OrigVal     = L Disp(%Base)
7940fe6060f1SDimitry Andric   //   # fall through to LoopMBB
79410b57cec5SDimitry Andric   MBB = StartMBB;
79420b57cec5SDimitry Andric   BuildMI(MBB, DL, TII->get(LOpcode), OrigVal).add(Base).addImm(Disp).addReg(0);
79430b57cec5SDimitry Andric   MBB->addSuccessor(LoopMBB);
79440b57cec5SDimitry Andric 
79450b57cec5SDimitry Andric   //  LoopMBB:
79460b57cec5SDimitry Andric   //   %OldVal        = phi [ %OrigVal, StartMBB ], [ %Dest, UpdateMBB ]
79470b57cec5SDimitry Andric   //   %RotatedOldVal = RLL %OldVal, 0(%BitShift)
79480b57cec5SDimitry Andric   //   CompareOpcode %RotatedOldVal, %Src2
79490b57cec5SDimitry Andric   //   BRC KeepOldMask, UpdateMBB
79500b57cec5SDimitry Andric   MBB = LoopMBB;
79510b57cec5SDimitry Andric   BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
79520b57cec5SDimitry Andric     .addReg(OrigVal).addMBB(StartMBB)
79530b57cec5SDimitry Andric     .addReg(Dest).addMBB(UpdateMBB);
79540b57cec5SDimitry Andric   if (IsSubWord)
79550b57cec5SDimitry Andric     BuildMI(MBB, DL, TII->get(SystemZ::RLL), RotatedOldVal)
79560b57cec5SDimitry Andric       .addReg(OldVal).addReg(BitShift).addImm(0);
79570b57cec5SDimitry Andric   BuildMI(MBB, DL, TII->get(CompareOpcode))
79580b57cec5SDimitry Andric     .addReg(RotatedOldVal).addReg(Src2);
79590b57cec5SDimitry Andric   BuildMI(MBB, DL, TII->get(SystemZ::BRC))
79600b57cec5SDimitry Andric     .addImm(SystemZ::CCMASK_ICMP).addImm(KeepOldMask).addMBB(UpdateMBB);
79610b57cec5SDimitry Andric   MBB->addSuccessor(UpdateMBB);
79620b57cec5SDimitry Andric   MBB->addSuccessor(UseAltMBB);
79630b57cec5SDimitry Andric 
79640b57cec5SDimitry Andric   //  UseAltMBB:
79650b57cec5SDimitry Andric   //   %RotatedAltVal = RISBG %RotatedOldVal, %Src2, 32, 31 + BitSize, 0
7966fe6060f1SDimitry Andric   //   # fall through to UpdateMBB
79670b57cec5SDimitry Andric   MBB = UseAltMBB;
79680b57cec5SDimitry Andric   if (IsSubWord)
79690b57cec5SDimitry Andric     BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RotatedAltVal)
79700b57cec5SDimitry Andric       .addReg(RotatedOldVal).addReg(Src2)
79710b57cec5SDimitry Andric       .addImm(32).addImm(31 + BitSize).addImm(0);
79720b57cec5SDimitry Andric   MBB->addSuccessor(UpdateMBB);
79730b57cec5SDimitry Andric 
79740b57cec5SDimitry Andric   //  UpdateMBB:
79750b57cec5SDimitry Andric   //   %RotatedNewVal = PHI [ %RotatedOldVal, LoopMBB ],
79760b57cec5SDimitry Andric   //                        [ %RotatedAltVal, UseAltMBB ]
79770b57cec5SDimitry Andric   //   %NewVal        = RLL %RotatedNewVal, 0(%NegBitShift)
79780b57cec5SDimitry Andric   //   %Dest          = CS %OldVal, %NewVal, Disp(%Base)
79790b57cec5SDimitry Andric   //   JNE LoopMBB
7980fe6060f1SDimitry Andric   //   # fall through to DoneMBB
79810b57cec5SDimitry Andric   MBB = UpdateMBB;
79820b57cec5SDimitry Andric   BuildMI(MBB, DL, TII->get(SystemZ::PHI), RotatedNewVal)
79830b57cec5SDimitry Andric     .addReg(RotatedOldVal).addMBB(LoopMBB)
79840b57cec5SDimitry Andric     .addReg(RotatedAltVal).addMBB(UseAltMBB);
79850b57cec5SDimitry Andric   if (IsSubWord)
79860b57cec5SDimitry Andric     BuildMI(MBB, DL, TII->get(SystemZ::RLL), NewVal)
79870b57cec5SDimitry Andric       .addReg(RotatedNewVal).addReg(NegBitShift).addImm(0);
79880b57cec5SDimitry Andric   BuildMI(MBB, DL, TII->get(CSOpcode), Dest)
79890b57cec5SDimitry Andric       .addReg(OldVal)
79900b57cec5SDimitry Andric       .addReg(NewVal)
79910b57cec5SDimitry Andric       .add(Base)
79920b57cec5SDimitry Andric       .addImm(Disp);
79930b57cec5SDimitry Andric   BuildMI(MBB, DL, TII->get(SystemZ::BRC))
79940b57cec5SDimitry Andric     .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
79950b57cec5SDimitry Andric   MBB->addSuccessor(LoopMBB);
79960b57cec5SDimitry Andric   MBB->addSuccessor(DoneMBB);
79970b57cec5SDimitry Andric 
79980b57cec5SDimitry Andric   MI.eraseFromParent();
79990b57cec5SDimitry Andric   return DoneMBB;
80000b57cec5SDimitry Andric }
80010b57cec5SDimitry Andric 
80020b57cec5SDimitry Andric // Implement EmitInstrWithCustomInserter for pseudo ATOMIC_CMP_SWAPW
80030b57cec5SDimitry Andric // instruction MI.
80040b57cec5SDimitry Andric MachineBasicBlock *
80050b57cec5SDimitry Andric SystemZTargetLowering::emitAtomicCmpSwapW(MachineInstr &MI,
80060b57cec5SDimitry Andric                                           MachineBasicBlock *MBB) const {
80070b57cec5SDimitry Andric   MachineFunction &MF = *MBB->getParent();
800881ad6265SDimitry Andric   const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
80090b57cec5SDimitry Andric   MachineRegisterInfo &MRI = MF.getRegInfo();
80100b57cec5SDimitry Andric 
80110b57cec5SDimitry Andric   // Extract the operands.  Base can be a register or a frame index.
80128bcb0991SDimitry Andric   Register Dest = MI.getOperand(0).getReg();
80130b57cec5SDimitry Andric   MachineOperand Base = earlyUseOperand(MI.getOperand(1));
80140b57cec5SDimitry Andric   int64_t Disp = MI.getOperand(2).getImm();
8015fe6060f1SDimitry Andric   Register CmpVal = MI.getOperand(3).getReg();
80168bcb0991SDimitry Andric   Register OrigSwapVal = MI.getOperand(4).getReg();
80178bcb0991SDimitry Andric   Register BitShift = MI.getOperand(5).getReg();
80188bcb0991SDimitry Andric   Register NegBitShift = MI.getOperand(6).getReg();
80190b57cec5SDimitry Andric   int64_t BitSize = MI.getOperand(7).getImm();
80200b57cec5SDimitry Andric   DebugLoc DL = MI.getDebugLoc();
80210b57cec5SDimitry Andric 
80220b57cec5SDimitry Andric   const TargetRegisterClass *RC = &SystemZ::GR32BitRegClass;
80230b57cec5SDimitry Andric 
8024fe6060f1SDimitry Andric   // Get the right opcodes for the displacement and zero-extension.
80250b57cec5SDimitry Andric   unsigned LOpcode  = TII->getOpcodeForOffset(SystemZ::L,  Disp);
80260b57cec5SDimitry Andric   unsigned CSOpcode = TII->getOpcodeForOffset(SystemZ::CS, Disp);
8027fe6060f1SDimitry Andric   unsigned ZExtOpcode  = BitSize == 8 ? SystemZ::LLCR : SystemZ::LLHR;
80280b57cec5SDimitry Andric   assert(LOpcode && CSOpcode && "Displacement out of range");
80290b57cec5SDimitry Andric 
80300b57cec5SDimitry Andric   // Create virtual registers for temporary results.
80318bcb0991SDimitry Andric   Register OrigOldVal = MRI.createVirtualRegister(RC);
80328bcb0991SDimitry Andric   Register OldVal = MRI.createVirtualRegister(RC);
80338bcb0991SDimitry Andric   Register SwapVal = MRI.createVirtualRegister(RC);
80348bcb0991SDimitry Andric   Register StoreVal = MRI.createVirtualRegister(RC);
8035fe6060f1SDimitry Andric   Register OldValRot = MRI.createVirtualRegister(RC);
80368bcb0991SDimitry Andric   Register RetryOldVal = MRI.createVirtualRegister(RC);
80378bcb0991SDimitry Andric   Register RetrySwapVal = MRI.createVirtualRegister(RC);
80380b57cec5SDimitry Andric 
80390b57cec5SDimitry Andric   // Insert 2 basic blocks for the loop.
80400b57cec5SDimitry Andric   MachineBasicBlock *StartMBB = MBB;
80415ffd83dbSDimitry Andric   MachineBasicBlock *DoneMBB  = SystemZ::splitBlockBefore(MI, MBB);
80425ffd83dbSDimitry Andric   MachineBasicBlock *LoopMBB  = SystemZ::emitBlockAfter(StartMBB);
80435ffd83dbSDimitry Andric   MachineBasicBlock *SetMBB   = SystemZ::emitBlockAfter(LoopMBB);
80440b57cec5SDimitry Andric 
80450b57cec5SDimitry Andric   //  StartMBB:
80460b57cec5SDimitry Andric   //   ...
80470b57cec5SDimitry Andric   //   %OrigOldVal     = L Disp(%Base)
8048fe6060f1SDimitry Andric   //   # fall through to LoopMBB
80490b57cec5SDimitry Andric   MBB = StartMBB;
80500b57cec5SDimitry Andric   BuildMI(MBB, DL, TII->get(LOpcode), OrigOldVal)
80510b57cec5SDimitry Andric       .add(Base)
80520b57cec5SDimitry Andric       .addImm(Disp)
80530b57cec5SDimitry Andric       .addReg(0);
80540b57cec5SDimitry Andric   MBB->addSuccessor(LoopMBB);
80550b57cec5SDimitry Andric 
80560b57cec5SDimitry Andric   //  LoopMBB:
80570b57cec5SDimitry Andric   //   %OldVal        = phi [ %OrigOldVal, EntryBB ], [ %RetryOldVal, SetMBB ]
80580b57cec5SDimitry Andric   //   %SwapVal       = phi [ %OrigSwapVal, EntryBB ], [ %RetrySwapVal, SetMBB ]
8059fe6060f1SDimitry Andric   //   %OldValRot     = RLL %OldVal, BitSize(%BitShift)
80600b57cec5SDimitry Andric   //                      ^^ The low BitSize bits contain the field
80610b57cec5SDimitry Andric   //                         of interest.
8062fe6060f1SDimitry Andric   //   %RetrySwapVal = RISBG32 %SwapVal, %OldValRot, 32, 63-BitSize, 0
80630b57cec5SDimitry Andric   //                      ^^ Replace the upper 32-BitSize bits of the
8064fe6060f1SDimitry Andric   //                         swap value with those that we loaded and rotated.
8065fe6060f1SDimitry Andric   //   %Dest = LL[CH] %OldValRot
8066fe6060f1SDimitry Andric   //   CR %Dest, %CmpVal
80670b57cec5SDimitry Andric   //   JNE DoneMBB
80680b57cec5SDimitry Andric   //   # Fall through to SetMBB
80690b57cec5SDimitry Andric   MBB = LoopMBB;
80700b57cec5SDimitry Andric   BuildMI(MBB, DL, TII->get(SystemZ::PHI), OldVal)
80710b57cec5SDimitry Andric     .addReg(OrigOldVal).addMBB(StartMBB)
80720b57cec5SDimitry Andric     .addReg(RetryOldVal).addMBB(SetMBB);
80730b57cec5SDimitry Andric   BuildMI(MBB, DL, TII->get(SystemZ::PHI), SwapVal)
80740b57cec5SDimitry Andric     .addReg(OrigSwapVal).addMBB(StartMBB)
80750b57cec5SDimitry Andric     .addReg(RetrySwapVal).addMBB(SetMBB);
8076fe6060f1SDimitry Andric   BuildMI(MBB, DL, TII->get(SystemZ::RLL), OldValRot)
80770b57cec5SDimitry Andric     .addReg(OldVal).addReg(BitShift).addImm(BitSize);
8078fe6060f1SDimitry Andric   BuildMI(MBB, DL, TII->get(SystemZ::RISBG32), RetrySwapVal)
8079fe6060f1SDimitry Andric     .addReg(SwapVal).addReg(OldValRot).addImm(32).addImm(63 - BitSize).addImm(0);
8080fe6060f1SDimitry Andric   BuildMI(MBB, DL, TII->get(ZExtOpcode), Dest)
8081fe6060f1SDimitry Andric     .addReg(OldValRot);
80820b57cec5SDimitry Andric   BuildMI(MBB, DL, TII->get(SystemZ::CR))
8083fe6060f1SDimitry Andric     .addReg(Dest).addReg(CmpVal);
80840b57cec5SDimitry Andric   BuildMI(MBB, DL, TII->get(SystemZ::BRC))
80850b57cec5SDimitry Andric     .addImm(SystemZ::CCMASK_ICMP)
80860b57cec5SDimitry Andric     .addImm(SystemZ::CCMASK_CMP_NE).addMBB(DoneMBB);
80870b57cec5SDimitry Andric   MBB->addSuccessor(DoneMBB);
80880b57cec5SDimitry Andric   MBB->addSuccessor(SetMBB);
80890b57cec5SDimitry Andric 
80900b57cec5SDimitry Andric   //  SetMBB:
80910b57cec5SDimitry Andric   //   %StoreVal     = RLL %RetrySwapVal, -BitSize(%NegBitShift)
80920b57cec5SDimitry Andric   //                      ^^ Rotate the new field to its proper position.
8093fe6060f1SDimitry Andric   //   %RetryOldVal  = CS %OldVal, %StoreVal, Disp(%Base)
80940b57cec5SDimitry Andric   //   JNE LoopMBB
8095fe6060f1SDimitry Andric   //   # fall through to ExitMBB
80960b57cec5SDimitry Andric   MBB = SetMBB;
80970b57cec5SDimitry Andric   BuildMI(MBB, DL, TII->get(SystemZ::RLL), StoreVal)
80980b57cec5SDimitry Andric     .addReg(RetrySwapVal).addReg(NegBitShift).addImm(-BitSize);
80990b57cec5SDimitry Andric   BuildMI(MBB, DL, TII->get(CSOpcode), RetryOldVal)
81000b57cec5SDimitry Andric       .addReg(OldVal)
81010b57cec5SDimitry Andric       .addReg(StoreVal)
81020b57cec5SDimitry Andric       .add(Base)
81030b57cec5SDimitry Andric       .addImm(Disp);
81040b57cec5SDimitry Andric   BuildMI(MBB, DL, TII->get(SystemZ::BRC))
81050b57cec5SDimitry Andric     .addImm(SystemZ::CCMASK_CS).addImm(SystemZ::CCMASK_CS_NE).addMBB(LoopMBB);
81060b57cec5SDimitry Andric   MBB->addSuccessor(LoopMBB);
81070b57cec5SDimitry Andric   MBB->addSuccessor(DoneMBB);
81080b57cec5SDimitry Andric 
81090b57cec5SDimitry Andric   // If the CC def wasn't dead in the ATOMIC_CMP_SWAPW, mark CC as live-in
81100b57cec5SDimitry Andric   // to the block after the loop.  At this point, CC may have been defined
81110b57cec5SDimitry Andric   // either by the CR in LoopMBB or by the CS in SetMBB.
81120b57cec5SDimitry Andric   if (!MI.registerDefIsDead(SystemZ::CC))
81130b57cec5SDimitry Andric     DoneMBB->addLiveIn(SystemZ::CC);
81140b57cec5SDimitry Andric 
81150b57cec5SDimitry Andric   MI.eraseFromParent();
81160b57cec5SDimitry Andric   return DoneMBB;
81170b57cec5SDimitry Andric }
81180b57cec5SDimitry Andric 
81190b57cec5SDimitry Andric // Emit a move from two GR64s to a GR128.
81200b57cec5SDimitry Andric MachineBasicBlock *
81210b57cec5SDimitry Andric SystemZTargetLowering::emitPair128(MachineInstr &MI,
81220b57cec5SDimitry Andric                                    MachineBasicBlock *MBB) const {
81230b57cec5SDimitry Andric   MachineFunction &MF = *MBB->getParent();
812481ad6265SDimitry Andric   const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
81250b57cec5SDimitry Andric   MachineRegisterInfo &MRI = MF.getRegInfo();
81260b57cec5SDimitry Andric   DebugLoc DL = MI.getDebugLoc();
81270b57cec5SDimitry Andric 
81288bcb0991SDimitry Andric   Register Dest = MI.getOperand(0).getReg();
81298bcb0991SDimitry Andric   Register Hi = MI.getOperand(1).getReg();
81308bcb0991SDimitry Andric   Register Lo = MI.getOperand(2).getReg();
81318bcb0991SDimitry Andric   Register Tmp1 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
81328bcb0991SDimitry Andric   Register Tmp2 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
81330b57cec5SDimitry Andric 
81340b57cec5SDimitry Andric   BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), Tmp1);
81350b57cec5SDimitry Andric   BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Tmp2)
81360b57cec5SDimitry Andric     .addReg(Tmp1).addReg(Hi).addImm(SystemZ::subreg_h64);
81370b57cec5SDimitry Andric   BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest)
81380b57cec5SDimitry Andric     .addReg(Tmp2).addReg(Lo).addImm(SystemZ::subreg_l64);
81390b57cec5SDimitry Andric 
81400b57cec5SDimitry Andric   MI.eraseFromParent();
81410b57cec5SDimitry Andric   return MBB;
81420b57cec5SDimitry Andric }
81430b57cec5SDimitry Andric 
81440b57cec5SDimitry Andric // Emit an extension from a GR64 to a GR128.  ClearEven is true
81450b57cec5SDimitry Andric // if the high register of the GR128 value must be cleared or false if
81460b57cec5SDimitry Andric // it's "don't care".
81470b57cec5SDimitry Andric MachineBasicBlock *SystemZTargetLowering::emitExt128(MachineInstr &MI,
81480b57cec5SDimitry Andric                                                      MachineBasicBlock *MBB,
81490b57cec5SDimitry Andric                                                      bool ClearEven) const {
81500b57cec5SDimitry Andric   MachineFunction &MF = *MBB->getParent();
815181ad6265SDimitry Andric   const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
81520b57cec5SDimitry Andric   MachineRegisterInfo &MRI = MF.getRegInfo();
81530b57cec5SDimitry Andric   DebugLoc DL = MI.getDebugLoc();
81540b57cec5SDimitry Andric 
81558bcb0991SDimitry Andric   Register Dest = MI.getOperand(0).getReg();
81568bcb0991SDimitry Andric   Register Src = MI.getOperand(1).getReg();
81578bcb0991SDimitry Andric   Register In128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
81580b57cec5SDimitry Andric 
81590b57cec5SDimitry Andric   BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::IMPLICIT_DEF), In128);
81600b57cec5SDimitry Andric   if (ClearEven) {
81618bcb0991SDimitry Andric     Register NewIn128 = MRI.createVirtualRegister(&SystemZ::GR128BitRegClass);
81628bcb0991SDimitry Andric     Register Zero64 = MRI.createVirtualRegister(&SystemZ::GR64BitRegClass);
81630b57cec5SDimitry Andric 
81640b57cec5SDimitry Andric     BuildMI(*MBB, MI, DL, TII->get(SystemZ::LLILL), Zero64)
81650b57cec5SDimitry Andric       .addImm(0);
81660b57cec5SDimitry Andric     BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), NewIn128)
81670b57cec5SDimitry Andric       .addReg(In128).addReg(Zero64).addImm(SystemZ::subreg_h64);
81680b57cec5SDimitry Andric     In128 = NewIn128;
81690b57cec5SDimitry Andric   }
81700b57cec5SDimitry Andric   BuildMI(*MBB, MI, DL, TII->get(TargetOpcode::INSERT_SUBREG), Dest)
81710b57cec5SDimitry Andric     .addReg(In128).addReg(Src).addImm(SystemZ::subreg_l64);
81720b57cec5SDimitry Andric 
81730b57cec5SDimitry Andric   MI.eraseFromParent();
81740b57cec5SDimitry Andric   return MBB;
81750b57cec5SDimitry Andric }
81760b57cec5SDimitry Andric 
81770eae32dcSDimitry Andric MachineBasicBlock *
81780eae32dcSDimitry Andric SystemZTargetLowering::emitMemMemWrapper(MachineInstr &MI,
81790eae32dcSDimitry Andric                                          MachineBasicBlock *MBB,
81800eae32dcSDimitry Andric                                          unsigned Opcode, bool IsMemset) const {
81810b57cec5SDimitry Andric   MachineFunction &MF = *MBB->getParent();
818281ad6265SDimitry Andric   const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
81830b57cec5SDimitry Andric   MachineRegisterInfo &MRI = MF.getRegInfo();
81840b57cec5SDimitry Andric   DebugLoc DL = MI.getDebugLoc();
81850b57cec5SDimitry Andric 
81860b57cec5SDimitry Andric   MachineOperand DestBase = earlyUseOperand(MI.getOperand(0));
81870b57cec5SDimitry Andric   uint64_t DestDisp = MI.getOperand(1).getImm();
81880eae32dcSDimitry Andric   MachineOperand SrcBase = MachineOperand::CreateReg(0U, false);
81890eae32dcSDimitry Andric   uint64_t SrcDisp;
81900eae32dcSDimitry Andric 
81910eae32dcSDimitry Andric   // Fold the displacement Disp if it is out of range.
81920eae32dcSDimitry Andric   auto foldDisplIfNeeded = [&](MachineOperand &Base, uint64_t &Disp) -> void {
81930eae32dcSDimitry Andric     if (!isUInt<12>(Disp)) {
81940eae32dcSDimitry Andric       Register Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
81950eae32dcSDimitry Andric       unsigned Opcode = TII->getOpcodeForOffset(SystemZ::LA, Disp);
81960eae32dcSDimitry Andric       BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), TII->get(Opcode), Reg)
81970eae32dcSDimitry Andric         .add(Base).addImm(Disp).addReg(0);
81980eae32dcSDimitry Andric       Base = MachineOperand::CreateReg(Reg, false);
81990eae32dcSDimitry Andric       Disp = 0;
82000eae32dcSDimitry Andric     }
82010eae32dcSDimitry Andric   };
82020eae32dcSDimitry Andric 
82030eae32dcSDimitry Andric   if (!IsMemset) {
82040eae32dcSDimitry Andric     SrcBase = earlyUseOperand(MI.getOperand(2));
82050eae32dcSDimitry Andric     SrcDisp = MI.getOperand(3).getImm();
82060eae32dcSDimitry Andric   } else {
82070eae32dcSDimitry Andric     SrcBase = DestBase;
82080eae32dcSDimitry Andric     SrcDisp = DestDisp++;
82090eae32dcSDimitry Andric     foldDisplIfNeeded(DestBase, DestDisp);
82100eae32dcSDimitry Andric   }
82110eae32dcSDimitry Andric 
82120eae32dcSDimitry Andric   MachineOperand &LengthMO = MI.getOperand(IsMemset ? 2 : 4);
8213349cc55cSDimitry Andric   bool IsImmForm = LengthMO.isImm();
8214349cc55cSDimitry Andric   bool IsRegForm = !IsImmForm;
8215349cc55cSDimitry Andric 
82160eae32dcSDimitry Andric   // Build and insert one Opcode of Length, with special treatment for memset.
82170eae32dcSDimitry Andric   auto insertMemMemOp = [&](MachineBasicBlock *InsMBB,
82180eae32dcSDimitry Andric                             MachineBasicBlock::iterator InsPos,
82190eae32dcSDimitry Andric                             MachineOperand DBase, uint64_t DDisp,
82200eae32dcSDimitry Andric                             MachineOperand SBase, uint64_t SDisp,
82210eae32dcSDimitry Andric                             unsigned Length) -> void {
82220eae32dcSDimitry Andric     assert(Length > 0 && Length <= 256 && "Building memory op with bad length.");
82230eae32dcSDimitry Andric     if (IsMemset) {
82240eae32dcSDimitry Andric       MachineOperand ByteMO = earlyUseOperand(MI.getOperand(3));
82250eae32dcSDimitry Andric       if (ByteMO.isImm())
82260eae32dcSDimitry Andric         BuildMI(*InsMBB, InsPos, DL, TII->get(SystemZ::MVI))
82270eae32dcSDimitry Andric           .add(SBase).addImm(SDisp).add(ByteMO);
82280eae32dcSDimitry Andric       else
82290eae32dcSDimitry Andric         BuildMI(*InsMBB, InsPos, DL, TII->get(SystemZ::STC))
82300eae32dcSDimitry Andric           .add(ByteMO).add(SBase).addImm(SDisp).addReg(0);
82310eae32dcSDimitry Andric       if (--Length == 0)
82320eae32dcSDimitry Andric         return;
82330eae32dcSDimitry Andric     }
82340eae32dcSDimitry Andric     BuildMI(*MBB, InsPos, DL, TII->get(Opcode))
82350eae32dcSDimitry Andric       .add(DBase).addImm(DDisp).addImm(Length)
82360eae32dcSDimitry Andric       .add(SBase).addImm(SDisp)
82370eae32dcSDimitry Andric       .setMemRefs(MI.memoperands());
82380eae32dcSDimitry Andric   };
82390eae32dcSDimitry Andric 
8240349cc55cSDimitry Andric   bool NeedsLoop = false;
8241349cc55cSDimitry Andric   uint64_t ImmLength = 0;
82420eae32dcSDimitry Andric   Register LenAdjReg = SystemZ::NoRegister;
8243349cc55cSDimitry Andric   if (IsImmForm) {
8244349cc55cSDimitry Andric     ImmLength = LengthMO.getImm();
82450eae32dcSDimitry Andric     ImmLength += IsMemset ? 2 : 1; // Add back the subtracted adjustment.
8246349cc55cSDimitry Andric     if (ImmLength == 0) {
8247349cc55cSDimitry Andric       MI.eraseFromParent();
8248349cc55cSDimitry Andric       return MBB;
8249349cc55cSDimitry Andric     }
8250349cc55cSDimitry Andric     if (Opcode == SystemZ::CLC) {
8251349cc55cSDimitry Andric       if (ImmLength > 3 * 256)
8252349cc55cSDimitry Andric         // A two-CLC sequence is a clear win over a loop, not least because
8253349cc55cSDimitry Andric         // it needs only one branch.  A three-CLC sequence needs the same
8254349cc55cSDimitry Andric         // number of branches as a loop (i.e. 2), but is shorter.  That
8255349cc55cSDimitry Andric         // brings us to lengths greater than 768 bytes.  It seems relatively
8256349cc55cSDimitry Andric         // likely that a difference will be found within the first 768 bytes,
8257349cc55cSDimitry Andric         // so we just optimize for the smallest number of branch
8258349cc55cSDimitry Andric         // instructions, in order to avoid polluting the prediction buffer
8259349cc55cSDimitry Andric         // too much.
8260349cc55cSDimitry Andric         NeedsLoop = true;
8261349cc55cSDimitry Andric     } else if (ImmLength > 6 * 256)
8262349cc55cSDimitry Andric       // The heuristic we use is to prefer loops for anything that would
8263349cc55cSDimitry Andric       // require 7 or more MVCs.  With these kinds of sizes there isn't much
8264349cc55cSDimitry Andric       // to choose between straight-line code and looping code, since the
8265349cc55cSDimitry Andric       // time will be dominated by the MVCs themselves.
8266349cc55cSDimitry Andric       NeedsLoop = true;
8267349cc55cSDimitry Andric   } else {
8268349cc55cSDimitry Andric     NeedsLoop = true;
82690eae32dcSDimitry Andric     LenAdjReg = LengthMO.getReg();
8270349cc55cSDimitry Andric   }
82710b57cec5SDimitry Andric 
82720b57cec5SDimitry Andric   // When generating more than one CLC, all but the last will need to
82730b57cec5SDimitry Andric   // branch to the end when a difference is found.
8274349cc55cSDimitry Andric   MachineBasicBlock *EndMBB =
8275349cc55cSDimitry Andric       (Opcode == SystemZ::CLC && (ImmLength > 256 || NeedsLoop)
8276fe6060f1SDimitry Andric            ? SystemZ::splitBlockAfter(MI, MBB)
8277fe6060f1SDimitry Andric            : nullptr);
82780b57cec5SDimitry Andric 
8279349cc55cSDimitry Andric   if (NeedsLoop) {
8280349cc55cSDimitry Andric     Register StartCountReg =
8281349cc55cSDimitry Andric       MRI.createVirtualRegister(&SystemZ::GR64BitRegClass);
8282349cc55cSDimitry Andric     if (IsImmForm) {
8283349cc55cSDimitry Andric       TII->loadImmediate(*MBB, MI, StartCountReg, ImmLength / 256);
8284349cc55cSDimitry Andric       ImmLength &= 255;
8285349cc55cSDimitry Andric     } else {
8286349cc55cSDimitry Andric       BuildMI(*MBB, MI, DL, TII->get(SystemZ::SRLG), StartCountReg)
82870eae32dcSDimitry Andric         .addReg(LenAdjReg)
8288349cc55cSDimitry Andric         .addReg(0)
8289349cc55cSDimitry Andric         .addImm(8);
8290349cc55cSDimitry Andric     }
82910b57cec5SDimitry Andric 
82920eae32dcSDimitry Andric     bool HaveSingleBase = DestBase.isIdenticalTo(SrcBase);
8293fe6060f1SDimitry Andric     auto loadZeroAddress = [&]() -> MachineOperand {
8294fe6060f1SDimitry Andric       Register Reg = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
8295fe6060f1SDimitry Andric       BuildMI(*MBB, MI, DL, TII->get(SystemZ::LGHI), Reg).addImm(0);
8296fe6060f1SDimitry Andric       return MachineOperand::CreateReg(Reg, false);
8297fe6060f1SDimitry Andric     };
8298fe6060f1SDimitry Andric     if (DestBase.isReg() && DestBase.getReg() == SystemZ::NoRegister)
8299fe6060f1SDimitry Andric       DestBase = loadZeroAddress();
8300fe6060f1SDimitry Andric     if (SrcBase.isReg() && SrcBase.getReg() == SystemZ::NoRegister)
8301fe6060f1SDimitry Andric       SrcBase = HaveSingleBase ? DestBase : loadZeroAddress();
8302fe6060f1SDimitry Andric 
8303fe6060f1SDimitry Andric     MachineBasicBlock *StartMBB = nullptr;
8304fe6060f1SDimitry Andric     MachineBasicBlock *LoopMBB = nullptr;
8305fe6060f1SDimitry Andric     MachineBasicBlock *NextMBB = nullptr;
8306fe6060f1SDimitry Andric     MachineBasicBlock *DoneMBB = nullptr;
8307fe6060f1SDimitry Andric     MachineBasicBlock *AllDoneMBB = nullptr;
8308fe6060f1SDimitry Andric 
83090b57cec5SDimitry Andric     Register StartSrcReg = forceReg(MI, SrcBase, TII);
8310fe6060f1SDimitry Andric     Register StartDestReg =
8311fe6060f1SDimitry Andric         (HaveSingleBase ? StartSrcReg : forceReg(MI, DestBase, TII));
83120b57cec5SDimitry Andric 
83130b57cec5SDimitry Andric     const TargetRegisterClass *RC = &SystemZ::ADDR64BitRegClass;
83140b57cec5SDimitry Andric     Register ThisSrcReg  = MRI.createVirtualRegister(RC);
8315fe6060f1SDimitry Andric     Register ThisDestReg =
8316fe6060f1SDimitry Andric         (HaveSingleBase ? ThisSrcReg : MRI.createVirtualRegister(RC));
83170b57cec5SDimitry Andric     Register NextSrcReg  = MRI.createVirtualRegister(RC);
8318fe6060f1SDimitry Andric     Register NextDestReg =
8319fe6060f1SDimitry Andric         (HaveSingleBase ? NextSrcReg : MRI.createVirtualRegister(RC));
83200b57cec5SDimitry Andric     RC = &SystemZ::GR64BitRegClass;
83210b57cec5SDimitry Andric     Register ThisCountReg = MRI.createVirtualRegister(RC);
83220b57cec5SDimitry Andric     Register NextCountReg = MRI.createVirtualRegister(RC);
83230b57cec5SDimitry Andric 
8324349cc55cSDimitry Andric     if (IsRegForm) {
8325fe6060f1SDimitry Andric       AllDoneMBB = SystemZ::splitBlockBefore(MI, MBB);
8326fe6060f1SDimitry Andric       StartMBB = SystemZ::emitBlockAfter(MBB);
8327fe6060f1SDimitry Andric       LoopMBB = SystemZ::emitBlockAfter(StartMBB);
8328349cc55cSDimitry Andric       NextMBB = (EndMBB ? SystemZ::emitBlockAfter(LoopMBB) : LoopMBB);
8329349cc55cSDimitry Andric       DoneMBB = SystemZ::emitBlockAfter(NextMBB);
8330fe6060f1SDimitry Andric 
8331fe6060f1SDimitry Andric       //  MBB:
83320eae32dcSDimitry Andric       //   # Jump to AllDoneMBB if LenAdjReg means 0, or fall thru to StartMBB.
8333fe6060f1SDimitry Andric       BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
83340eae32dcSDimitry Andric         .addReg(LenAdjReg).addImm(IsMemset ? -2 : -1);
8335fe6060f1SDimitry Andric       BuildMI(MBB, DL, TII->get(SystemZ::BRC))
8336fe6060f1SDimitry Andric         .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_EQ)
8337fe6060f1SDimitry Andric         .addMBB(AllDoneMBB);
8338fe6060f1SDimitry Andric       MBB->addSuccessor(AllDoneMBB);
83390eae32dcSDimitry Andric       if (!IsMemset)
8340fe6060f1SDimitry Andric         MBB->addSuccessor(StartMBB);
83410eae32dcSDimitry Andric       else {
83420eae32dcSDimitry Andric         // MemsetOneCheckMBB:
83430eae32dcSDimitry Andric         // # Jump to MemsetOneMBB for a memset of length 1, or
83440eae32dcSDimitry Andric         // # fall thru to StartMBB.
83450eae32dcSDimitry Andric         MachineBasicBlock *MemsetOneCheckMBB = SystemZ::emitBlockAfter(MBB);
83460eae32dcSDimitry Andric         MachineBasicBlock *MemsetOneMBB = SystemZ::emitBlockAfter(&*MF.rbegin());
83470eae32dcSDimitry Andric         MBB->addSuccessor(MemsetOneCheckMBB);
83480eae32dcSDimitry Andric         MBB = MemsetOneCheckMBB;
83490eae32dcSDimitry Andric         BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
83500eae32dcSDimitry Andric           .addReg(LenAdjReg).addImm(-1);
83510eae32dcSDimitry Andric         BuildMI(MBB, DL, TII->get(SystemZ::BRC))
83520eae32dcSDimitry Andric           .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_EQ)
83530eae32dcSDimitry Andric           .addMBB(MemsetOneMBB);
83540eae32dcSDimitry Andric         MBB->addSuccessor(MemsetOneMBB, {10, 100});
83550eae32dcSDimitry Andric         MBB->addSuccessor(StartMBB, {90, 100});
83560eae32dcSDimitry Andric 
83570eae32dcSDimitry Andric         // MemsetOneMBB:
83580eae32dcSDimitry Andric         // # Jump back to AllDoneMBB after a single MVI or STC.
83590eae32dcSDimitry Andric         MBB = MemsetOneMBB;
83600eae32dcSDimitry Andric         insertMemMemOp(MBB, MBB->end(),
83610eae32dcSDimitry Andric                        MachineOperand::CreateReg(StartDestReg, false), DestDisp,
83620eae32dcSDimitry Andric                        MachineOperand::CreateReg(StartSrcReg, false), SrcDisp,
83630eae32dcSDimitry Andric                        1);
83640eae32dcSDimitry Andric         BuildMI(MBB, DL, TII->get(SystemZ::J)).addMBB(AllDoneMBB);
83650eae32dcSDimitry Andric         MBB->addSuccessor(AllDoneMBB);
83660eae32dcSDimitry Andric       }
83670b57cec5SDimitry Andric 
83680b57cec5SDimitry Andric       // StartMBB:
8369fe6060f1SDimitry Andric       // # Jump to DoneMBB if %StartCountReg is zero, or fall through to LoopMBB.
8370fe6060f1SDimitry Andric       MBB = StartMBB;
8371fe6060f1SDimitry Andric       BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
8372fe6060f1SDimitry Andric         .addReg(StartCountReg).addImm(0);
8373fe6060f1SDimitry Andric       BuildMI(MBB, DL, TII->get(SystemZ::BRC))
8374fe6060f1SDimitry Andric         .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_EQ)
8375fe6060f1SDimitry Andric         .addMBB(DoneMBB);
8376fe6060f1SDimitry Andric       MBB->addSuccessor(DoneMBB);
83770b57cec5SDimitry Andric       MBB->addSuccessor(LoopMBB);
8378fe6060f1SDimitry Andric     }
8379fe6060f1SDimitry Andric     else {
8380fe6060f1SDimitry Andric       StartMBB = MBB;
8381fe6060f1SDimitry Andric       DoneMBB = SystemZ::splitBlockBefore(MI, MBB);
8382fe6060f1SDimitry Andric       LoopMBB = SystemZ::emitBlockAfter(StartMBB);
8383fe6060f1SDimitry Andric       NextMBB = (EndMBB ? SystemZ::emitBlockAfter(LoopMBB) : LoopMBB);
8384fe6060f1SDimitry Andric 
8385fe6060f1SDimitry Andric       //  StartMBB:
8386fe6060f1SDimitry Andric       //   # fall through to LoopMBB
8387fe6060f1SDimitry Andric       MBB->addSuccessor(LoopMBB);
8388fe6060f1SDimitry Andric 
8389fe6060f1SDimitry Andric       DestBase = MachineOperand::CreateReg(NextDestReg, false);
8390fe6060f1SDimitry Andric       SrcBase = MachineOperand::CreateReg(NextSrcReg, false);
8391fe6060f1SDimitry Andric       if (EndMBB && !ImmLength)
8392fe6060f1SDimitry Andric         // If the loop handled the whole CLC range, DoneMBB will be empty with
8393fe6060f1SDimitry Andric         // CC live-through into EndMBB, so add it as live-in.
8394fe6060f1SDimitry Andric         DoneMBB->addLiveIn(SystemZ::CC);
8395fe6060f1SDimitry Andric     }
83960b57cec5SDimitry Andric 
83970b57cec5SDimitry Andric     //  LoopMBB:
83980b57cec5SDimitry Andric     //   %ThisDestReg = phi [ %StartDestReg, StartMBB ],
83990b57cec5SDimitry Andric     //                      [ %NextDestReg, NextMBB ]
84000b57cec5SDimitry Andric     //   %ThisSrcReg = phi [ %StartSrcReg, StartMBB ],
84010b57cec5SDimitry Andric     //                     [ %NextSrcReg, NextMBB ]
84020b57cec5SDimitry Andric     //   %ThisCountReg = phi [ %StartCountReg, StartMBB ],
84030b57cec5SDimitry Andric     //                       [ %NextCountReg, NextMBB ]
84040b57cec5SDimitry Andric     //   ( PFD 2, 768+DestDisp(%ThisDestReg) )
84050b57cec5SDimitry Andric     //   Opcode DestDisp(256,%ThisDestReg), SrcDisp(%ThisSrcReg)
84060b57cec5SDimitry Andric     //   ( JLH EndMBB )
84070b57cec5SDimitry Andric     //
84080b57cec5SDimitry Andric     // The prefetch is used only for MVC.  The JLH is used only for CLC.
84090b57cec5SDimitry Andric     MBB = LoopMBB;
84100b57cec5SDimitry Andric     BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisDestReg)
84110b57cec5SDimitry Andric       .addReg(StartDestReg).addMBB(StartMBB)
84120b57cec5SDimitry Andric       .addReg(NextDestReg).addMBB(NextMBB);
84130b57cec5SDimitry Andric     if (!HaveSingleBase)
84140b57cec5SDimitry Andric       BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisSrcReg)
84150b57cec5SDimitry Andric         .addReg(StartSrcReg).addMBB(StartMBB)
84160b57cec5SDimitry Andric         .addReg(NextSrcReg).addMBB(NextMBB);
84170b57cec5SDimitry Andric     BuildMI(MBB, DL, TII->get(SystemZ::PHI), ThisCountReg)
84180b57cec5SDimitry Andric       .addReg(StartCountReg).addMBB(StartMBB)
84190b57cec5SDimitry Andric       .addReg(NextCountReg).addMBB(NextMBB);
84200b57cec5SDimitry Andric     if (Opcode == SystemZ::MVC)
84210b57cec5SDimitry Andric       BuildMI(MBB, DL, TII->get(SystemZ::PFD))
84220b57cec5SDimitry Andric         .addImm(SystemZ::PFD_WRITE)
84230eae32dcSDimitry Andric         .addReg(ThisDestReg).addImm(DestDisp - IsMemset + 768).addReg(0);
84240eae32dcSDimitry Andric     insertMemMemOp(MBB, MBB->end(),
84250eae32dcSDimitry Andric                    MachineOperand::CreateReg(ThisDestReg, false), DestDisp,
84260eae32dcSDimitry Andric                    MachineOperand::CreateReg(ThisSrcReg, false), SrcDisp, 256);
84270b57cec5SDimitry Andric     if (EndMBB) {
84280b57cec5SDimitry Andric       BuildMI(MBB, DL, TII->get(SystemZ::BRC))
84290b57cec5SDimitry Andric         .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
84300b57cec5SDimitry Andric         .addMBB(EndMBB);
84310b57cec5SDimitry Andric       MBB->addSuccessor(EndMBB);
84320b57cec5SDimitry Andric       MBB->addSuccessor(NextMBB);
84330b57cec5SDimitry Andric     }
84340b57cec5SDimitry Andric 
84350b57cec5SDimitry Andric     // NextMBB:
84360b57cec5SDimitry Andric     //   %NextDestReg = LA 256(%ThisDestReg)
84370b57cec5SDimitry Andric     //   %NextSrcReg = LA 256(%ThisSrcReg)
84380b57cec5SDimitry Andric     //   %NextCountReg = AGHI %ThisCountReg, -1
84390b57cec5SDimitry Andric     //   CGHI %NextCountReg, 0
84400b57cec5SDimitry Andric     //   JLH LoopMBB
8441fe6060f1SDimitry Andric     //   # fall through to DoneMBB
84420b57cec5SDimitry Andric     //
84430b57cec5SDimitry Andric     // The AGHI, CGHI and JLH should be converted to BRCTG by later passes.
84440b57cec5SDimitry Andric     MBB = NextMBB;
84450b57cec5SDimitry Andric     BuildMI(MBB, DL, TII->get(SystemZ::LA), NextDestReg)
84460b57cec5SDimitry Andric       .addReg(ThisDestReg).addImm(256).addReg(0);
84470b57cec5SDimitry Andric     if (!HaveSingleBase)
84480b57cec5SDimitry Andric       BuildMI(MBB, DL, TII->get(SystemZ::LA), NextSrcReg)
84490b57cec5SDimitry Andric         .addReg(ThisSrcReg).addImm(256).addReg(0);
84500b57cec5SDimitry Andric     BuildMI(MBB, DL, TII->get(SystemZ::AGHI), NextCountReg)
84510b57cec5SDimitry Andric       .addReg(ThisCountReg).addImm(-1);
84520b57cec5SDimitry Andric     BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
84530b57cec5SDimitry Andric       .addReg(NextCountReg).addImm(0);
84540b57cec5SDimitry Andric     BuildMI(MBB, DL, TII->get(SystemZ::BRC))
84550b57cec5SDimitry Andric       .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
84560b57cec5SDimitry Andric       .addMBB(LoopMBB);
84570b57cec5SDimitry Andric     MBB->addSuccessor(LoopMBB);
84580b57cec5SDimitry Andric     MBB->addSuccessor(DoneMBB);
84590b57cec5SDimitry Andric 
84600b57cec5SDimitry Andric     MBB = DoneMBB;
8461349cc55cSDimitry Andric     if (IsRegForm) {
8462fe6060f1SDimitry Andric       // DoneMBB:
8463fe6060f1SDimitry Andric       // # Make PHIs for RemDestReg/RemSrcReg as the loop may or may not run.
8464fe6060f1SDimitry Andric       // # Use EXecute Relative Long for the remainder of the bytes. The target
8465fe6060f1SDimitry Andric       //   instruction of the EXRL will have a length field of 1 since 0 is an
84660eae32dcSDimitry Andric       //   illegal value. The number of bytes processed becomes (%LenAdjReg &
8467fe6060f1SDimitry Andric       //   0xff) + 1.
8468fe6060f1SDimitry Andric       // # Fall through to AllDoneMBB.
8469fe6060f1SDimitry Andric       Register RemSrcReg  = MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
8470fe6060f1SDimitry Andric       Register RemDestReg = HaveSingleBase ? RemSrcReg
8471fe6060f1SDimitry Andric         : MRI.createVirtualRegister(&SystemZ::ADDR64BitRegClass);
8472fe6060f1SDimitry Andric       BuildMI(MBB, DL, TII->get(SystemZ::PHI), RemDestReg)
8473fe6060f1SDimitry Andric         .addReg(StartDestReg).addMBB(StartMBB)
8474349cc55cSDimitry Andric         .addReg(NextDestReg).addMBB(NextMBB);
8475fe6060f1SDimitry Andric       if (!HaveSingleBase)
8476fe6060f1SDimitry Andric         BuildMI(MBB, DL, TII->get(SystemZ::PHI), RemSrcReg)
8477fe6060f1SDimitry Andric           .addReg(StartSrcReg).addMBB(StartMBB)
8478349cc55cSDimitry Andric           .addReg(NextSrcReg).addMBB(NextMBB);
84790eae32dcSDimitry Andric       if (IsMemset)
84800eae32dcSDimitry Andric         insertMemMemOp(MBB, MBB->end(),
84810eae32dcSDimitry Andric                        MachineOperand::CreateReg(RemDestReg, false), DestDisp,
84820eae32dcSDimitry Andric                        MachineOperand::CreateReg(RemSrcReg, false), SrcDisp, 1);
8483349cc55cSDimitry Andric       MachineInstrBuilder EXRL_MIB =
8484fe6060f1SDimitry Andric         BuildMI(MBB, DL, TII->get(SystemZ::EXRL_Pseudo))
8485fe6060f1SDimitry Andric           .addImm(Opcode)
84860eae32dcSDimitry Andric           .addReg(LenAdjReg)
8487fe6060f1SDimitry Andric           .addReg(RemDestReg).addImm(DestDisp)
8488fe6060f1SDimitry Andric           .addReg(RemSrcReg).addImm(SrcDisp);
8489fe6060f1SDimitry Andric       MBB->addSuccessor(AllDoneMBB);
8490fe6060f1SDimitry Andric       MBB = AllDoneMBB;
8491*a324c340SDimitry Andric       if (Opcode != SystemZ::MVC) {
8492349cc55cSDimitry Andric         EXRL_MIB.addReg(SystemZ::CC, RegState::ImplicitDefine);
8493*a324c340SDimitry Andric         if (EndMBB)
8494349cc55cSDimitry Andric           MBB->addLiveIn(SystemZ::CC);
8495349cc55cSDimitry Andric       }
84960b57cec5SDimitry Andric     }
8497*a324c340SDimitry Andric     MF.getProperties().reset(MachineFunctionProperties::Property::NoPHIs);
8498fe6060f1SDimitry Andric   }
8499fe6060f1SDimitry Andric 
85000b57cec5SDimitry Andric   // Handle any remaining bytes with straight-line code.
8501fe6060f1SDimitry Andric   while (ImmLength > 0) {
8502fe6060f1SDimitry Andric     uint64_t ThisLength = std::min(ImmLength, uint64_t(256));
85030b57cec5SDimitry Andric     // The previous iteration might have created out-of-range displacements.
85040eae32dcSDimitry Andric     // Apply them using LA/LAY if so.
85050eae32dcSDimitry Andric     foldDisplIfNeeded(DestBase, DestDisp);
85060eae32dcSDimitry Andric     foldDisplIfNeeded(SrcBase, SrcDisp);
85070eae32dcSDimitry Andric     insertMemMemOp(MBB, MI, DestBase, DestDisp, SrcBase, SrcDisp, ThisLength);
85080b57cec5SDimitry Andric     DestDisp += ThisLength;
85090b57cec5SDimitry Andric     SrcDisp += ThisLength;
8510fe6060f1SDimitry Andric     ImmLength -= ThisLength;
85110b57cec5SDimitry Andric     // If there's another CLC to go, branch to the end if a difference
85120b57cec5SDimitry Andric     // was found.
8513fe6060f1SDimitry Andric     if (EndMBB && ImmLength > 0) {
85145ffd83dbSDimitry Andric       MachineBasicBlock *NextMBB = SystemZ::splitBlockBefore(MI, MBB);
85150b57cec5SDimitry Andric       BuildMI(MBB, DL, TII->get(SystemZ::BRC))
85160b57cec5SDimitry Andric         .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_NE)
85170b57cec5SDimitry Andric         .addMBB(EndMBB);
85180b57cec5SDimitry Andric       MBB->addSuccessor(EndMBB);
85190b57cec5SDimitry Andric       MBB->addSuccessor(NextMBB);
85200b57cec5SDimitry Andric       MBB = NextMBB;
85210b57cec5SDimitry Andric     }
85220b57cec5SDimitry Andric   }
85230b57cec5SDimitry Andric   if (EndMBB) {
85240b57cec5SDimitry Andric     MBB->addSuccessor(EndMBB);
85250b57cec5SDimitry Andric     MBB = EndMBB;
85260b57cec5SDimitry Andric     MBB->addLiveIn(SystemZ::CC);
85270b57cec5SDimitry Andric   }
85280b57cec5SDimitry Andric 
85290b57cec5SDimitry Andric   MI.eraseFromParent();
85300b57cec5SDimitry Andric   return MBB;
85310b57cec5SDimitry Andric }
85320b57cec5SDimitry Andric 
85330b57cec5SDimitry Andric // Decompose string pseudo-instruction MI into a loop that continually performs
85340b57cec5SDimitry Andric // Opcode until CC != 3.
85350b57cec5SDimitry Andric MachineBasicBlock *SystemZTargetLowering::emitStringWrapper(
85360b57cec5SDimitry Andric     MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const {
85370b57cec5SDimitry Andric   MachineFunction &MF = *MBB->getParent();
853881ad6265SDimitry Andric   const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
85390b57cec5SDimitry Andric   MachineRegisterInfo &MRI = MF.getRegInfo();
85400b57cec5SDimitry Andric   DebugLoc DL = MI.getDebugLoc();
85410b57cec5SDimitry Andric 
85420b57cec5SDimitry Andric   uint64_t End1Reg = MI.getOperand(0).getReg();
85430b57cec5SDimitry Andric   uint64_t Start1Reg = MI.getOperand(1).getReg();
85440b57cec5SDimitry Andric   uint64_t Start2Reg = MI.getOperand(2).getReg();
85450b57cec5SDimitry Andric   uint64_t CharReg = MI.getOperand(3).getReg();
85460b57cec5SDimitry Andric 
85470b57cec5SDimitry Andric   const TargetRegisterClass *RC = &SystemZ::GR64BitRegClass;
85480b57cec5SDimitry Andric   uint64_t This1Reg = MRI.createVirtualRegister(RC);
85490b57cec5SDimitry Andric   uint64_t This2Reg = MRI.createVirtualRegister(RC);
85500b57cec5SDimitry Andric   uint64_t End2Reg  = MRI.createVirtualRegister(RC);
85510b57cec5SDimitry Andric 
85520b57cec5SDimitry Andric   MachineBasicBlock *StartMBB = MBB;
85535ffd83dbSDimitry Andric   MachineBasicBlock *DoneMBB = SystemZ::splitBlockBefore(MI, MBB);
85545ffd83dbSDimitry Andric   MachineBasicBlock *LoopMBB = SystemZ::emitBlockAfter(StartMBB);
85550b57cec5SDimitry Andric 
85560b57cec5SDimitry Andric   //  StartMBB:
8557fe6060f1SDimitry Andric   //   # fall through to LoopMBB
85580b57cec5SDimitry Andric   MBB->addSuccessor(LoopMBB);
85590b57cec5SDimitry Andric 
85600b57cec5SDimitry Andric   //  LoopMBB:
85610b57cec5SDimitry Andric   //   %This1Reg = phi [ %Start1Reg, StartMBB ], [ %End1Reg, LoopMBB ]
85620b57cec5SDimitry Andric   //   %This2Reg = phi [ %Start2Reg, StartMBB ], [ %End2Reg, LoopMBB ]
85630b57cec5SDimitry Andric   //   R0L = %CharReg
85640b57cec5SDimitry Andric   //   %End1Reg, %End2Reg = CLST %This1Reg, %This2Reg -- uses R0L
85650b57cec5SDimitry Andric   //   JO LoopMBB
8566fe6060f1SDimitry Andric   //   # fall through to DoneMBB
85670b57cec5SDimitry Andric   //
85680b57cec5SDimitry Andric   // The load of R0L can be hoisted by post-RA LICM.
85690b57cec5SDimitry Andric   MBB = LoopMBB;
85700b57cec5SDimitry Andric 
85710b57cec5SDimitry Andric   BuildMI(MBB, DL, TII->get(SystemZ::PHI), This1Reg)
85720b57cec5SDimitry Andric     .addReg(Start1Reg).addMBB(StartMBB)
85730b57cec5SDimitry Andric     .addReg(End1Reg).addMBB(LoopMBB);
85740b57cec5SDimitry Andric   BuildMI(MBB, DL, TII->get(SystemZ::PHI), This2Reg)
85750b57cec5SDimitry Andric     .addReg(Start2Reg).addMBB(StartMBB)
85760b57cec5SDimitry Andric     .addReg(End2Reg).addMBB(LoopMBB);
85770b57cec5SDimitry Andric   BuildMI(MBB, DL, TII->get(TargetOpcode::COPY), SystemZ::R0L).addReg(CharReg);
85780b57cec5SDimitry Andric   BuildMI(MBB, DL, TII->get(Opcode))
85790b57cec5SDimitry Andric     .addReg(End1Reg, RegState::Define).addReg(End2Reg, RegState::Define)
85800b57cec5SDimitry Andric     .addReg(This1Reg).addReg(This2Reg);
85810b57cec5SDimitry Andric   BuildMI(MBB, DL, TII->get(SystemZ::BRC))
85820b57cec5SDimitry Andric     .addImm(SystemZ::CCMASK_ANY).addImm(SystemZ::CCMASK_3).addMBB(LoopMBB);
85830b57cec5SDimitry Andric   MBB->addSuccessor(LoopMBB);
85840b57cec5SDimitry Andric   MBB->addSuccessor(DoneMBB);
85850b57cec5SDimitry Andric 
85860b57cec5SDimitry Andric   DoneMBB->addLiveIn(SystemZ::CC);
85870b57cec5SDimitry Andric 
85880b57cec5SDimitry Andric   MI.eraseFromParent();
85890b57cec5SDimitry Andric   return DoneMBB;
85900b57cec5SDimitry Andric }
85910b57cec5SDimitry Andric 
85920b57cec5SDimitry Andric // Update TBEGIN instruction with final opcode and register clobbers.
85930b57cec5SDimitry Andric MachineBasicBlock *SystemZTargetLowering::emitTransactionBegin(
85940b57cec5SDimitry Andric     MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode,
85950b57cec5SDimitry Andric     bool NoFloat) const {
85960b57cec5SDimitry Andric   MachineFunction &MF = *MBB->getParent();
85970b57cec5SDimitry Andric   const TargetFrameLowering *TFI = Subtarget.getFrameLowering();
85980b57cec5SDimitry Andric   const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
85990b57cec5SDimitry Andric 
86000b57cec5SDimitry Andric   // Update opcode.
86010b57cec5SDimitry Andric   MI.setDesc(TII->get(Opcode));
86020b57cec5SDimitry Andric 
86030b57cec5SDimitry Andric   // We cannot handle a TBEGIN that clobbers the stack or frame pointer.
86040b57cec5SDimitry Andric   // Make sure to add the corresponding GRSM bits if they are missing.
86050b57cec5SDimitry Andric   uint64_t Control = MI.getOperand(2).getImm();
86060b57cec5SDimitry Andric   static const unsigned GPRControlBit[16] = {
86070b57cec5SDimitry Andric     0x8000, 0x8000, 0x4000, 0x4000, 0x2000, 0x2000, 0x1000, 0x1000,
86080b57cec5SDimitry Andric     0x0800, 0x0800, 0x0400, 0x0400, 0x0200, 0x0200, 0x0100, 0x0100
86090b57cec5SDimitry Andric   };
86100b57cec5SDimitry Andric   Control |= GPRControlBit[15];
86110b57cec5SDimitry Andric   if (TFI->hasFP(MF))
86120b57cec5SDimitry Andric     Control |= GPRControlBit[11];
86130b57cec5SDimitry Andric   MI.getOperand(2).setImm(Control);
86140b57cec5SDimitry Andric 
86150b57cec5SDimitry Andric   // Add GPR clobbers.
86160b57cec5SDimitry Andric   for (int I = 0; I < 16; I++) {
86170b57cec5SDimitry Andric     if ((Control & GPRControlBit[I]) == 0) {
86180b57cec5SDimitry Andric       unsigned Reg = SystemZMC::GR64Regs[I];
86190b57cec5SDimitry Andric       MI.addOperand(MachineOperand::CreateReg(Reg, true, true));
86200b57cec5SDimitry Andric     }
86210b57cec5SDimitry Andric   }
86220b57cec5SDimitry Andric 
86230b57cec5SDimitry Andric   // Add FPR/VR clobbers.
86240b57cec5SDimitry Andric   if (!NoFloat && (Control & 4) != 0) {
86250b57cec5SDimitry Andric     if (Subtarget.hasVector()) {
862604eeddc0SDimitry Andric       for (unsigned Reg : SystemZMC::VR128Regs) {
86270b57cec5SDimitry Andric         MI.addOperand(MachineOperand::CreateReg(Reg, true, true));
86280b57cec5SDimitry Andric       }
86290b57cec5SDimitry Andric     } else {
863004eeddc0SDimitry Andric       for (unsigned Reg : SystemZMC::FP64Regs) {
86310b57cec5SDimitry Andric         MI.addOperand(MachineOperand::CreateReg(Reg, true, true));
86320b57cec5SDimitry Andric       }
86330b57cec5SDimitry Andric     }
86340b57cec5SDimitry Andric   }
86350b57cec5SDimitry Andric 
86360b57cec5SDimitry Andric   return MBB;
86370b57cec5SDimitry Andric }
86380b57cec5SDimitry Andric 
86390b57cec5SDimitry Andric MachineBasicBlock *SystemZTargetLowering::emitLoadAndTestCmp0(
86400b57cec5SDimitry Andric     MachineInstr &MI, MachineBasicBlock *MBB, unsigned Opcode) const {
86410b57cec5SDimitry Andric   MachineFunction &MF = *MBB->getParent();
86420b57cec5SDimitry Andric   MachineRegisterInfo *MRI = &MF.getRegInfo();
864381ad6265SDimitry Andric   const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
86440b57cec5SDimitry Andric   DebugLoc DL = MI.getDebugLoc();
86450b57cec5SDimitry Andric 
86468bcb0991SDimitry Andric   Register SrcReg = MI.getOperand(0).getReg();
86470b57cec5SDimitry Andric 
86480b57cec5SDimitry Andric   // Create new virtual register of the same class as source.
86490b57cec5SDimitry Andric   const TargetRegisterClass *RC = MRI->getRegClass(SrcReg);
86508bcb0991SDimitry Andric   Register DstReg = MRI->createVirtualRegister(RC);
86510b57cec5SDimitry Andric 
86520b57cec5SDimitry Andric   // Replace pseudo with a normal load-and-test that models the def as
86530b57cec5SDimitry Andric   // well.
86540b57cec5SDimitry Andric   BuildMI(*MBB, MI, DL, TII->get(Opcode), DstReg)
8655480093f4SDimitry Andric     .addReg(SrcReg)
8656480093f4SDimitry Andric     .setMIFlags(MI.getFlags());
86570b57cec5SDimitry Andric   MI.eraseFromParent();
86580b57cec5SDimitry Andric 
86590b57cec5SDimitry Andric   return MBB;
86600b57cec5SDimitry Andric }
86610b57cec5SDimitry Andric 
86625ffd83dbSDimitry Andric MachineBasicBlock *SystemZTargetLowering::emitProbedAlloca(
86635ffd83dbSDimitry Andric     MachineInstr &MI, MachineBasicBlock *MBB) const {
86645ffd83dbSDimitry Andric   MachineFunction &MF = *MBB->getParent();
86655ffd83dbSDimitry Andric   MachineRegisterInfo *MRI = &MF.getRegInfo();
866681ad6265SDimitry Andric   const SystemZInstrInfo *TII = Subtarget.getInstrInfo();
86675ffd83dbSDimitry Andric   DebugLoc DL = MI.getDebugLoc();
86685ffd83dbSDimitry Andric   const unsigned ProbeSize = getStackProbeSize(MF);
86695ffd83dbSDimitry Andric   Register DstReg = MI.getOperand(0).getReg();
86705ffd83dbSDimitry Andric   Register SizeReg = MI.getOperand(2).getReg();
86715ffd83dbSDimitry Andric 
86725ffd83dbSDimitry Andric   MachineBasicBlock *StartMBB = MBB;
86735ffd83dbSDimitry Andric   MachineBasicBlock *DoneMBB  = SystemZ::splitBlockAfter(MI, MBB);
86745ffd83dbSDimitry Andric   MachineBasicBlock *LoopTestMBB  = SystemZ::emitBlockAfter(StartMBB);
86755ffd83dbSDimitry Andric   MachineBasicBlock *LoopBodyMBB = SystemZ::emitBlockAfter(LoopTestMBB);
86765ffd83dbSDimitry Andric   MachineBasicBlock *TailTestMBB = SystemZ::emitBlockAfter(LoopBodyMBB);
86775ffd83dbSDimitry Andric   MachineBasicBlock *TailMBB = SystemZ::emitBlockAfter(TailTestMBB);
86785ffd83dbSDimitry Andric 
86795ffd83dbSDimitry Andric   MachineMemOperand *VolLdMMO = MF.getMachineMemOperand(MachinePointerInfo(),
86805ffd83dbSDimitry Andric     MachineMemOperand::MOVolatile | MachineMemOperand::MOLoad, 8, Align(1));
86815ffd83dbSDimitry Andric 
86825ffd83dbSDimitry Andric   Register PHIReg = MRI->createVirtualRegister(&SystemZ::ADDR64BitRegClass);
86835ffd83dbSDimitry Andric   Register IncReg = MRI->createVirtualRegister(&SystemZ::ADDR64BitRegClass);
86845ffd83dbSDimitry Andric 
86855ffd83dbSDimitry Andric   //  LoopTestMBB
86865ffd83dbSDimitry Andric   //  BRC TailTestMBB
86875ffd83dbSDimitry Andric   //  # fallthrough to LoopBodyMBB
86885ffd83dbSDimitry Andric   StartMBB->addSuccessor(LoopTestMBB);
86895ffd83dbSDimitry Andric   MBB = LoopTestMBB;
86905ffd83dbSDimitry Andric   BuildMI(MBB, DL, TII->get(SystemZ::PHI), PHIReg)
86915ffd83dbSDimitry Andric     .addReg(SizeReg)
86925ffd83dbSDimitry Andric     .addMBB(StartMBB)
86935ffd83dbSDimitry Andric     .addReg(IncReg)
86945ffd83dbSDimitry Andric     .addMBB(LoopBodyMBB);
86955ffd83dbSDimitry Andric   BuildMI(MBB, DL, TII->get(SystemZ::CLGFI))
86965ffd83dbSDimitry Andric     .addReg(PHIReg)
86975ffd83dbSDimitry Andric     .addImm(ProbeSize);
86985ffd83dbSDimitry Andric   BuildMI(MBB, DL, TII->get(SystemZ::BRC))
86995ffd83dbSDimitry Andric     .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_LT)
87005ffd83dbSDimitry Andric     .addMBB(TailTestMBB);
87015ffd83dbSDimitry Andric   MBB->addSuccessor(LoopBodyMBB);
87025ffd83dbSDimitry Andric   MBB->addSuccessor(TailTestMBB);
87035ffd83dbSDimitry Andric 
87045ffd83dbSDimitry Andric   //  LoopBodyMBB: Allocate and probe by means of a volatile compare.
87055ffd83dbSDimitry Andric   //  J LoopTestMBB
87065ffd83dbSDimitry Andric   MBB = LoopBodyMBB;
87075ffd83dbSDimitry Andric   BuildMI(MBB, DL, TII->get(SystemZ::SLGFI), IncReg)
87085ffd83dbSDimitry Andric     .addReg(PHIReg)
87095ffd83dbSDimitry Andric     .addImm(ProbeSize);
87105ffd83dbSDimitry Andric   BuildMI(MBB, DL, TII->get(SystemZ::SLGFI), SystemZ::R15D)
87115ffd83dbSDimitry Andric     .addReg(SystemZ::R15D)
87125ffd83dbSDimitry Andric     .addImm(ProbeSize);
87135ffd83dbSDimitry Andric   BuildMI(MBB, DL, TII->get(SystemZ::CG)).addReg(SystemZ::R15D)
87145ffd83dbSDimitry Andric     .addReg(SystemZ::R15D).addImm(ProbeSize - 8).addReg(0)
87155ffd83dbSDimitry Andric     .setMemRefs(VolLdMMO);
87165ffd83dbSDimitry Andric   BuildMI(MBB, DL, TII->get(SystemZ::J)).addMBB(LoopTestMBB);
87175ffd83dbSDimitry Andric   MBB->addSuccessor(LoopTestMBB);
87185ffd83dbSDimitry Andric 
87195ffd83dbSDimitry Andric   //  TailTestMBB
87205ffd83dbSDimitry Andric   //  BRC DoneMBB
87215ffd83dbSDimitry Andric   //  # fallthrough to TailMBB
87225ffd83dbSDimitry Andric   MBB = TailTestMBB;
87235ffd83dbSDimitry Andric   BuildMI(MBB, DL, TII->get(SystemZ::CGHI))
87245ffd83dbSDimitry Andric     .addReg(PHIReg)
87255ffd83dbSDimitry Andric     .addImm(0);
87265ffd83dbSDimitry Andric   BuildMI(MBB, DL, TII->get(SystemZ::BRC))
87275ffd83dbSDimitry Andric     .addImm(SystemZ::CCMASK_ICMP).addImm(SystemZ::CCMASK_CMP_EQ)
87285ffd83dbSDimitry Andric     .addMBB(DoneMBB);
87295ffd83dbSDimitry Andric   MBB->addSuccessor(TailMBB);
87305ffd83dbSDimitry Andric   MBB->addSuccessor(DoneMBB);
87315ffd83dbSDimitry Andric 
87325ffd83dbSDimitry Andric   //  TailMBB
87335ffd83dbSDimitry Andric   //  # fallthrough to DoneMBB
87345ffd83dbSDimitry Andric   MBB = TailMBB;
87355ffd83dbSDimitry Andric   BuildMI(MBB, DL, TII->get(SystemZ::SLGR), SystemZ::R15D)
87365ffd83dbSDimitry Andric     .addReg(SystemZ::R15D)
87375ffd83dbSDimitry Andric     .addReg(PHIReg);
87385ffd83dbSDimitry Andric   BuildMI(MBB, DL, TII->get(SystemZ::CG)).addReg(SystemZ::R15D)
87395ffd83dbSDimitry Andric     .addReg(SystemZ::R15D).addImm(-8).addReg(PHIReg)
87405ffd83dbSDimitry Andric     .setMemRefs(VolLdMMO);
87415ffd83dbSDimitry Andric   MBB->addSuccessor(DoneMBB);
87425ffd83dbSDimitry Andric 
87435ffd83dbSDimitry Andric   //  DoneMBB
87445ffd83dbSDimitry Andric   MBB = DoneMBB;
87455ffd83dbSDimitry Andric   BuildMI(*MBB, MBB->begin(), DL, TII->get(TargetOpcode::COPY), DstReg)
87465ffd83dbSDimitry Andric     .addReg(SystemZ::R15D);
87475ffd83dbSDimitry Andric 
87485ffd83dbSDimitry Andric   MI.eraseFromParent();
87495ffd83dbSDimitry Andric   return DoneMBB;
87505ffd83dbSDimitry Andric }
87515ffd83dbSDimitry Andric 
8752e8d8bef9SDimitry Andric SDValue SystemZTargetLowering::
8753e8d8bef9SDimitry Andric getBackchainAddress(SDValue SP, SelectionDAG &DAG) const {
8754e8d8bef9SDimitry Andric   MachineFunction &MF = DAG.getMachineFunction();
8755349cc55cSDimitry Andric   auto *TFL = Subtarget.getFrameLowering<SystemZELFFrameLowering>();
8756e8d8bef9SDimitry Andric   SDLoc DL(SP);
8757e8d8bef9SDimitry Andric   return DAG.getNode(ISD::ADD, DL, MVT::i64, SP,
8758e8d8bef9SDimitry Andric                      DAG.getIntPtrConstant(TFL->getBackchainOffset(MF), DL));
8759e8d8bef9SDimitry Andric }
8760e8d8bef9SDimitry Andric 
87610b57cec5SDimitry Andric MachineBasicBlock *SystemZTargetLowering::EmitInstrWithCustomInserter(
87620b57cec5SDimitry Andric     MachineInstr &MI, MachineBasicBlock *MBB) const {
87630b57cec5SDimitry Andric   switch (MI.getOpcode()) {
87640b57cec5SDimitry Andric   case SystemZ::Select32:
87650b57cec5SDimitry Andric   case SystemZ::Select64:
87660b57cec5SDimitry Andric   case SystemZ::SelectF32:
87670b57cec5SDimitry Andric   case SystemZ::SelectF64:
87680b57cec5SDimitry Andric   case SystemZ::SelectF128:
87690b57cec5SDimitry Andric   case SystemZ::SelectVR32:
87700b57cec5SDimitry Andric   case SystemZ::SelectVR64:
87710b57cec5SDimitry Andric   case SystemZ::SelectVR128:
87720b57cec5SDimitry Andric     return emitSelect(MI, MBB);
87730b57cec5SDimitry Andric 
87740b57cec5SDimitry Andric   case SystemZ::CondStore8Mux:
87750b57cec5SDimitry Andric     return emitCondStore(MI, MBB, SystemZ::STCMux, 0, false);
87760b57cec5SDimitry Andric   case SystemZ::CondStore8MuxInv:
87770b57cec5SDimitry Andric     return emitCondStore(MI, MBB, SystemZ::STCMux, 0, true);
87780b57cec5SDimitry Andric   case SystemZ::CondStore16Mux:
87790b57cec5SDimitry Andric     return emitCondStore(MI, MBB, SystemZ::STHMux, 0, false);
87800b57cec5SDimitry Andric   case SystemZ::CondStore16MuxInv:
87810b57cec5SDimitry Andric     return emitCondStore(MI, MBB, SystemZ::STHMux, 0, true);
87820b57cec5SDimitry Andric   case SystemZ::CondStore32Mux:
87830b57cec5SDimitry Andric     return emitCondStore(MI, MBB, SystemZ::STMux, SystemZ::STOCMux, false);
87840b57cec5SDimitry Andric   case SystemZ::CondStore32MuxInv:
87850b57cec5SDimitry Andric     return emitCondStore(MI, MBB, SystemZ::STMux, SystemZ::STOCMux, true);
87860b57cec5SDimitry Andric   case SystemZ::CondStore8:
87870b57cec5SDimitry Andric     return emitCondStore(MI, MBB, SystemZ::STC, 0, false);
87880b57cec5SDimitry Andric   case SystemZ::CondStore8Inv:
87890b57cec5SDimitry Andric     return emitCondStore(MI, MBB, SystemZ::STC, 0, true);
87900b57cec5SDimitry Andric   case SystemZ::CondStore16:
87910b57cec5SDimitry Andric     return emitCondStore(MI, MBB, SystemZ::STH, 0, false);
87920b57cec5SDimitry Andric   case SystemZ::CondStore16Inv:
87930b57cec5SDimitry Andric     return emitCondStore(MI, MBB, SystemZ::STH, 0, true);
87940b57cec5SDimitry Andric   case SystemZ::CondStore32:
87950b57cec5SDimitry Andric     return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, false);
87960b57cec5SDimitry Andric   case SystemZ::CondStore32Inv:
87970b57cec5SDimitry Andric     return emitCondStore(MI, MBB, SystemZ::ST, SystemZ::STOC, true);
87980b57cec5SDimitry Andric   case SystemZ::CondStore64:
87990b57cec5SDimitry Andric     return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, false);
88000b57cec5SDimitry Andric   case SystemZ::CondStore64Inv:
88010b57cec5SDimitry Andric     return emitCondStore(MI, MBB, SystemZ::STG, SystemZ::STOCG, true);
88020b57cec5SDimitry Andric   case SystemZ::CondStoreF32:
88030b57cec5SDimitry Andric     return emitCondStore(MI, MBB, SystemZ::STE, 0, false);
88040b57cec5SDimitry Andric   case SystemZ::CondStoreF32Inv:
88050b57cec5SDimitry Andric     return emitCondStore(MI, MBB, SystemZ::STE, 0, true);
88060b57cec5SDimitry Andric   case SystemZ::CondStoreF64:
88070b57cec5SDimitry Andric     return emitCondStore(MI, MBB, SystemZ::STD, 0, false);
88080b57cec5SDimitry Andric   case SystemZ::CondStoreF64Inv:
88090b57cec5SDimitry Andric     return emitCondStore(MI, MBB, SystemZ::STD, 0, true);
88100b57cec5SDimitry Andric 
88110b57cec5SDimitry Andric   case SystemZ::PAIR128:
88120b57cec5SDimitry Andric     return emitPair128(MI, MBB);
88130b57cec5SDimitry Andric   case SystemZ::AEXT128:
88140b57cec5SDimitry Andric     return emitExt128(MI, MBB, false);
88150b57cec5SDimitry Andric   case SystemZ::ZEXT128:
88160b57cec5SDimitry Andric     return emitExt128(MI, MBB, true);
88170b57cec5SDimitry Andric 
88180b57cec5SDimitry Andric   case SystemZ::ATOMIC_SWAPW:
88190b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, 0, 0);
88200b57cec5SDimitry Andric   case SystemZ::ATOMIC_SWAP_32:
88210b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, 0, 32);
88220b57cec5SDimitry Andric   case SystemZ::ATOMIC_SWAP_64:
88230b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, 0, 64);
88240b57cec5SDimitry Andric 
88250b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOADW_AR:
88260b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 0);
88270b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOADW_AFI:
88280b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 0);
88290b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_AR:
88300b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::AR, 32);
88310b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_AHI:
88320b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::AHI, 32);
88330b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_AFI:
88340b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::AFI, 32);
88350b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_AGR:
88360b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::AGR, 64);
88370b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_AGHI:
88380b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::AGHI, 64);
88390b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_AGFI:
88400b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::AGFI, 64);
88410b57cec5SDimitry Andric 
88420b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOADW_SR:
88430b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 0);
88440b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_SR:
88450b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::SR, 32);
88460b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_SGR:
88470b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::SGR, 64);
88480b57cec5SDimitry Andric 
88490b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOADW_NR:
88500b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0);
88510b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOADW_NILH:
88520b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0);
88530b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_NR:
88540b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32);
88550b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_NILL:
88560b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32);
88570b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_NILH:
88580b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32);
88590b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_NILF:
88600b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32);
88610b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_NGR:
88620b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64);
88630b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_NILL64:
88640b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64);
88650b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_NILH64:
88660b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64);
88670b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_NIHL64:
88680b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64);
88690b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_NIHH64:
88700b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64);
88710b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_NILF64:
88720b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64);
88730b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_NIHF64:
88740b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64);
88750b57cec5SDimitry Andric 
88760b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOADW_OR:
88770b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 0);
88780b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOADW_OILH:
88790b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 0);
88800b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_OR:
88810b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::OR, 32);
88820b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_OILL:
88830b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL, 32);
88840b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_OILH:
88850b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH, 32);
88860b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_OILF:
88870b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF, 32);
88880b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_OGR:
88890b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::OGR, 64);
88900b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_OILL64:
88910b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::OILL64, 64);
88920b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_OILH64:
88930b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::OILH64, 64);
88940b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_OIHL64:
88950b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHL64, 64);
88960b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_OIHH64:
88970b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHH64, 64);
88980b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_OILF64:
88990b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::OILF64, 64);
89000b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_OIHF64:
89010b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::OIHF64, 64);
89020b57cec5SDimitry Andric 
89030b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOADW_XR:
89040b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 0);
89050b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOADW_XILF:
89060b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 0);
89070b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_XR:
89080b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::XR, 32);
89090b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_XILF:
89100b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF, 32);
89110b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_XGR:
89120b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::XGR, 64);
89130b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_XILF64:
89140b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::XILF64, 64);
89150b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_XIHF64:
89160b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::XIHF64, 64);
89170b57cec5SDimitry Andric 
89180b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOADW_NRi:
89190b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 0, true);
89200b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOADW_NILHi:
89210b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 0, true);
89220b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_NRi:
89230b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::NR, 32, true);
89240b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_NILLi:
89250b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL, 32, true);
89260b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_NILHi:
89270b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH, 32, true);
89280b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_NILFi:
89290b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF, 32, true);
89300b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_NGRi:
89310b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::NGR, 64, true);
89320b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_NILL64i:
89330b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILL64, 64, true);
89340b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_NILH64i:
89350b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILH64, 64, true);
89360b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_NIHL64i:
89370b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHL64, 64, true);
89380b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_NIHH64i:
89390b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHH64, 64, true);
89400b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_NILF64i:
89410b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::NILF64, 64, true);
89420b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_NIHF64i:
89430b57cec5SDimitry Andric     return emitAtomicLoadBinary(MI, MBB, SystemZ::NIHF64, 64, true);
89440b57cec5SDimitry Andric 
89450b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOADW_MIN:
89460b57cec5SDimitry Andric     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
89470b57cec5SDimitry Andric                                 SystemZ::CCMASK_CMP_LE, 0);
89480b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_MIN_32:
89490b57cec5SDimitry Andric     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
89500b57cec5SDimitry Andric                                 SystemZ::CCMASK_CMP_LE, 32);
89510b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_MIN_64:
89520b57cec5SDimitry Andric     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
89530b57cec5SDimitry Andric                                 SystemZ::CCMASK_CMP_LE, 64);
89540b57cec5SDimitry Andric 
89550b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOADW_MAX:
89560b57cec5SDimitry Andric     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
89570b57cec5SDimitry Andric                                 SystemZ::CCMASK_CMP_GE, 0);
89580b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_MAX_32:
89590b57cec5SDimitry Andric     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CR,
89600b57cec5SDimitry Andric                                 SystemZ::CCMASK_CMP_GE, 32);
89610b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_MAX_64:
89620b57cec5SDimitry Andric     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CGR,
89630b57cec5SDimitry Andric                                 SystemZ::CCMASK_CMP_GE, 64);
89640b57cec5SDimitry Andric 
89650b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOADW_UMIN:
89660b57cec5SDimitry Andric     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
89670b57cec5SDimitry Andric                                 SystemZ::CCMASK_CMP_LE, 0);
89680b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_UMIN_32:
89690b57cec5SDimitry Andric     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
89700b57cec5SDimitry Andric                                 SystemZ::CCMASK_CMP_LE, 32);
89710b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_UMIN_64:
89720b57cec5SDimitry Andric     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
89730b57cec5SDimitry Andric                                 SystemZ::CCMASK_CMP_LE, 64);
89740b57cec5SDimitry Andric 
89750b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOADW_UMAX:
89760b57cec5SDimitry Andric     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
89770b57cec5SDimitry Andric                                 SystemZ::CCMASK_CMP_GE, 0);
89780b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_UMAX_32:
89790b57cec5SDimitry Andric     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLR,
89800b57cec5SDimitry Andric                                 SystemZ::CCMASK_CMP_GE, 32);
89810b57cec5SDimitry Andric   case SystemZ::ATOMIC_LOAD_UMAX_64:
89820b57cec5SDimitry Andric     return emitAtomicLoadMinMax(MI, MBB, SystemZ::CLGR,
89830b57cec5SDimitry Andric                                 SystemZ::CCMASK_CMP_GE, 64);
89840b57cec5SDimitry Andric 
89850b57cec5SDimitry Andric   case SystemZ::ATOMIC_CMP_SWAPW:
89860b57cec5SDimitry Andric     return emitAtomicCmpSwapW(MI, MBB);
8987349cc55cSDimitry Andric   case SystemZ::MVCImm:
8988349cc55cSDimitry Andric   case SystemZ::MVCReg:
89890b57cec5SDimitry Andric     return emitMemMemWrapper(MI, MBB, SystemZ::MVC);
8990349cc55cSDimitry Andric   case SystemZ::NCImm:
89910b57cec5SDimitry Andric     return emitMemMemWrapper(MI, MBB, SystemZ::NC);
8992349cc55cSDimitry Andric   case SystemZ::OCImm:
89930b57cec5SDimitry Andric     return emitMemMemWrapper(MI, MBB, SystemZ::OC);
8994349cc55cSDimitry Andric   case SystemZ::XCImm:
8995349cc55cSDimitry Andric   case SystemZ::XCReg:
89960b57cec5SDimitry Andric     return emitMemMemWrapper(MI, MBB, SystemZ::XC);
8997349cc55cSDimitry Andric   case SystemZ::CLCImm:
8998349cc55cSDimitry Andric   case SystemZ::CLCReg:
89990b57cec5SDimitry Andric     return emitMemMemWrapper(MI, MBB, SystemZ::CLC);
90000eae32dcSDimitry Andric   case SystemZ::MemsetImmImm:
90010eae32dcSDimitry Andric   case SystemZ::MemsetImmReg:
90020eae32dcSDimitry Andric   case SystemZ::MemsetRegImm:
90030eae32dcSDimitry Andric   case SystemZ::MemsetRegReg:
90040eae32dcSDimitry Andric     return emitMemMemWrapper(MI, MBB, SystemZ::MVC, true/*IsMemset*/);
90050b57cec5SDimitry Andric   case SystemZ::CLSTLoop:
90060b57cec5SDimitry Andric     return emitStringWrapper(MI, MBB, SystemZ::CLST);
90070b57cec5SDimitry Andric   case SystemZ::MVSTLoop:
90080b57cec5SDimitry Andric     return emitStringWrapper(MI, MBB, SystemZ::MVST);
90090b57cec5SDimitry Andric   case SystemZ::SRSTLoop:
90100b57cec5SDimitry Andric     return emitStringWrapper(MI, MBB, SystemZ::SRST);
90110b57cec5SDimitry Andric   case SystemZ::TBEGIN:
90120b57cec5SDimitry Andric     return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, false);
90130b57cec5SDimitry Andric   case SystemZ::TBEGIN_nofloat:
90140b57cec5SDimitry Andric     return emitTransactionBegin(MI, MBB, SystemZ::TBEGIN, true);
90150b57cec5SDimitry Andric   case SystemZ::TBEGINC:
90160b57cec5SDimitry Andric     return emitTransactionBegin(MI, MBB, SystemZ::TBEGINC, true);
90170b57cec5SDimitry Andric   case SystemZ::LTEBRCompare_VecPseudo:
90180b57cec5SDimitry Andric     return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTEBR);
90190b57cec5SDimitry Andric   case SystemZ::LTDBRCompare_VecPseudo:
90200b57cec5SDimitry Andric     return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTDBR);
90210b57cec5SDimitry Andric   case SystemZ::LTXBRCompare_VecPseudo:
90220b57cec5SDimitry Andric     return emitLoadAndTestCmp0(MI, MBB, SystemZ::LTXBR);
90230b57cec5SDimitry Andric 
90245ffd83dbSDimitry Andric   case SystemZ::PROBED_ALLOCA:
90255ffd83dbSDimitry Andric     return emitProbedAlloca(MI, MBB);
90265ffd83dbSDimitry Andric 
90270b57cec5SDimitry Andric   case TargetOpcode::STACKMAP:
90280b57cec5SDimitry Andric   case TargetOpcode::PATCHPOINT:
90290b57cec5SDimitry Andric     return emitPatchPoint(MI, MBB);
90300b57cec5SDimitry Andric 
90310b57cec5SDimitry Andric   default:
90320b57cec5SDimitry Andric     llvm_unreachable("Unexpected instr type to insert");
90330b57cec5SDimitry Andric   }
90340b57cec5SDimitry Andric }
90350b57cec5SDimitry Andric 
90360b57cec5SDimitry Andric // This is only used by the isel schedulers, and is needed only to prevent
90370b57cec5SDimitry Andric // compiler from crashing when list-ilp is used.
90380b57cec5SDimitry Andric const TargetRegisterClass *
90390b57cec5SDimitry Andric SystemZTargetLowering::getRepRegClassFor(MVT VT) const {
90400b57cec5SDimitry Andric   if (VT == MVT::Untyped)
90410b57cec5SDimitry Andric     return &SystemZ::ADDR128BitRegClass;
90420b57cec5SDimitry Andric   return TargetLowering::getRepRegClassFor(VT);
90430b57cec5SDimitry Andric }
9044bdd1243dSDimitry Andric 
9045bdd1243dSDimitry Andric SDValue SystemZTargetLowering::lowerGET_ROUNDING(SDValue Op,
9046bdd1243dSDimitry Andric                                                  SelectionDAG &DAG) const {
9047bdd1243dSDimitry Andric   SDLoc dl(Op);
9048bdd1243dSDimitry Andric   /*
9049bdd1243dSDimitry Andric    The rounding method is in FPC Byte 3 bits 6-7, and has the following
9050bdd1243dSDimitry Andric    settings:
9051bdd1243dSDimitry Andric      00 Round to nearest
9052bdd1243dSDimitry Andric      01 Round to 0
9053bdd1243dSDimitry Andric      10 Round to +inf
9054bdd1243dSDimitry Andric      11 Round to -inf
9055bdd1243dSDimitry Andric 
9056bdd1243dSDimitry Andric   FLT_ROUNDS, on the other hand, expects the following:
9057bdd1243dSDimitry Andric     -1 Undefined
9058bdd1243dSDimitry Andric      0 Round to 0
9059bdd1243dSDimitry Andric      1 Round to nearest
9060bdd1243dSDimitry Andric      2 Round to +inf
9061bdd1243dSDimitry Andric      3 Round to -inf
9062bdd1243dSDimitry Andric   */
9063bdd1243dSDimitry Andric 
9064bdd1243dSDimitry Andric   // Save FPC to register.
9065bdd1243dSDimitry Andric   SDValue Chain = Op.getOperand(0);
9066bdd1243dSDimitry Andric   SDValue EFPC(
9067bdd1243dSDimitry Andric       DAG.getMachineNode(SystemZ::EFPC, dl, {MVT::i32, MVT::Other}, Chain), 0);
9068bdd1243dSDimitry Andric   Chain = EFPC.getValue(1);
9069bdd1243dSDimitry Andric 
9070bdd1243dSDimitry Andric   // Transform as necessary
9071bdd1243dSDimitry Andric   SDValue CWD1 = DAG.getNode(ISD::AND, dl, MVT::i32, EFPC,
9072bdd1243dSDimitry Andric                              DAG.getConstant(3, dl, MVT::i32));
9073bdd1243dSDimitry Andric   // RetVal = (CWD1 ^ (CWD1 >> 1)) ^ 1
9074bdd1243dSDimitry Andric   SDValue CWD2 = DAG.getNode(ISD::XOR, dl, MVT::i32, CWD1,
9075bdd1243dSDimitry Andric                              DAG.getNode(ISD::SRL, dl, MVT::i32, CWD1,
9076bdd1243dSDimitry Andric                                          DAG.getConstant(1, dl, MVT::i32)));
9077bdd1243dSDimitry Andric 
9078bdd1243dSDimitry Andric   SDValue RetVal = DAG.getNode(ISD::XOR, dl, MVT::i32, CWD2,
9079bdd1243dSDimitry Andric                                DAG.getConstant(1, dl, MVT::i32));
9080bdd1243dSDimitry Andric   RetVal = DAG.getZExtOrTrunc(RetVal, dl, Op.getValueType());
9081bdd1243dSDimitry Andric 
9082bdd1243dSDimitry Andric   return DAG.getMergeValues({RetVal, Chain}, dl);
9083bdd1243dSDimitry Andric }
9084