xref: /freebsd/contrib/llvm-project/llvm/lib/Target/SystemZ/SystemZISelDAGToDAG.cpp (revision 1db9f3b21e39176dd5b67cf8ac378633b172463e)
10b57cec5SDimitry Andric //===-- SystemZISelDAGToDAG.cpp - A dag to dag inst selector for SystemZ --===//
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 defines an instruction selector for the SystemZ target.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "SystemZTargetMachine.h"
140b57cec5SDimitry Andric #include "SystemZISelLowering.h"
150b57cec5SDimitry Andric #include "llvm/Analysis/AliasAnalysis.h"
160b57cec5SDimitry Andric #include "llvm/CodeGen/SelectionDAGISel.h"
170b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
180b57cec5SDimitry Andric #include "llvm/Support/KnownBits.h"
190b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric using namespace llvm;
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric #define DEBUG_TYPE "systemz-isel"
24bdd1243dSDimitry Andric #define PASS_NAME "SystemZ DAG->DAG Pattern Instruction Selection"
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric namespace {
270b57cec5SDimitry Andric // Used to build addressing modes.
280b57cec5SDimitry Andric struct SystemZAddressingMode {
290b57cec5SDimitry Andric   // The shape of the address.
300b57cec5SDimitry Andric   enum AddrForm {
310b57cec5SDimitry Andric     // base+displacement
320b57cec5SDimitry Andric     FormBD,
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric     // base+displacement+index for load and store operands
350b57cec5SDimitry Andric     FormBDXNormal,
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric     // base+displacement+index for load address operands
380b57cec5SDimitry Andric     FormBDXLA,
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric     // base+displacement+index+ADJDYNALLOC
410b57cec5SDimitry Andric     FormBDXDynAlloc
420b57cec5SDimitry Andric   };
430b57cec5SDimitry Andric   AddrForm Form;
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric   // The type of displacement.  The enum names here correspond directly
460b57cec5SDimitry Andric   // to the definitions in SystemZOperand.td.  We could split them into
470b57cec5SDimitry Andric   // flags -- single/pair, 128-bit, etc. -- but it hardly seems worth it.
480b57cec5SDimitry Andric   enum DispRange {
490b57cec5SDimitry Andric     Disp12Only,
500b57cec5SDimitry Andric     Disp12Pair,
510b57cec5SDimitry Andric     Disp20Only,
520b57cec5SDimitry Andric     Disp20Only128,
530b57cec5SDimitry Andric     Disp20Pair
540b57cec5SDimitry Andric   };
550b57cec5SDimitry Andric   DispRange DR;
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric   // The parts of the address.  The address is equivalent to:
580b57cec5SDimitry Andric   //
590b57cec5SDimitry Andric   //     Base + Disp + Index + (IncludesDynAlloc ? ADJDYNALLOC : 0)
600b57cec5SDimitry Andric   SDValue Base;
610b57cec5SDimitry Andric   int64_t Disp;
620b57cec5SDimitry Andric   SDValue Index;
630b57cec5SDimitry Andric   bool IncludesDynAlloc;
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric   SystemZAddressingMode(AddrForm form, DispRange dr)
6604eeddc0SDimitry Andric       : Form(form), DR(dr), Disp(0), IncludesDynAlloc(false) {}
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric   // True if the address can have an index register.
690b57cec5SDimitry Andric   bool hasIndexField() { return Form != FormBD; }
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric   // True if the address can (and must) include ADJDYNALLOC.
720b57cec5SDimitry Andric   bool isDynAlloc() { return Form == FormBDXDynAlloc; }
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric   void dump(const llvm::SelectionDAG *DAG) {
750b57cec5SDimitry Andric     errs() << "SystemZAddressingMode " << this << '\n';
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric     errs() << " Base ";
780b57cec5SDimitry Andric     if (Base.getNode())
790b57cec5SDimitry Andric       Base.getNode()->dump(DAG);
800b57cec5SDimitry Andric     else
810b57cec5SDimitry Andric       errs() << "null\n";
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric     if (hasIndexField()) {
840b57cec5SDimitry Andric       errs() << " Index ";
850b57cec5SDimitry Andric       if (Index.getNode())
860b57cec5SDimitry Andric         Index.getNode()->dump(DAG);
870b57cec5SDimitry Andric       else
880b57cec5SDimitry Andric         errs() << "null\n";
890b57cec5SDimitry Andric     }
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric     errs() << " Disp " << Disp;
920b57cec5SDimitry Andric     if (IncludesDynAlloc)
930b57cec5SDimitry Andric       errs() << " + ADJDYNALLOC";
940b57cec5SDimitry Andric     errs() << '\n';
950b57cec5SDimitry Andric   }
960b57cec5SDimitry Andric };
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric // Return a mask with Count low bits set.
990b57cec5SDimitry Andric static uint64_t allOnes(unsigned int Count) {
1000b57cec5SDimitry Andric   assert(Count <= 64);
1010b57cec5SDimitry Andric   if (Count > 63)
1020b57cec5SDimitry Andric     return UINT64_MAX;
1030b57cec5SDimitry Andric   return (uint64_t(1) << Count) - 1;
1040b57cec5SDimitry Andric }
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric // Represents operands 2 to 5 of the ROTATE AND ... SELECTED BITS operation
1070b57cec5SDimitry Andric // given by Opcode.  The operands are: Input (R2), Start (I3), End (I4) and
1080b57cec5SDimitry Andric // Rotate (I5).  The combined operand value is effectively:
1090b57cec5SDimitry Andric //
1100b57cec5SDimitry Andric //   (or (rotl Input, Rotate), ~Mask)
1110b57cec5SDimitry Andric //
1120b57cec5SDimitry Andric // for RNSBG and:
1130b57cec5SDimitry Andric //
1140b57cec5SDimitry Andric //   (and (rotl Input, Rotate), Mask)
1150b57cec5SDimitry Andric //
1160b57cec5SDimitry Andric // otherwise.  The output value has BitSize bits, although Input may be
1170b57cec5SDimitry Andric // narrower (in which case the upper bits are don't care), or wider (in which
1180b57cec5SDimitry Andric // case the result will be truncated as part of the operation).
1190b57cec5SDimitry Andric struct RxSBGOperands {
1200b57cec5SDimitry Andric   RxSBGOperands(unsigned Op, SDValue N)
1210b57cec5SDimitry Andric     : Opcode(Op), BitSize(N.getValueSizeInBits()),
1220b57cec5SDimitry Andric       Mask(allOnes(BitSize)), Input(N), Start(64 - BitSize), End(63),
1230b57cec5SDimitry Andric       Rotate(0) {}
1240b57cec5SDimitry Andric 
1250b57cec5SDimitry Andric   unsigned Opcode;
1260b57cec5SDimitry Andric   unsigned BitSize;
1270b57cec5SDimitry Andric   uint64_t Mask;
1280b57cec5SDimitry Andric   SDValue Input;
1290b57cec5SDimitry Andric   unsigned Start;
1300b57cec5SDimitry Andric   unsigned End;
1310b57cec5SDimitry Andric   unsigned Rotate;
1320b57cec5SDimitry Andric };
1330b57cec5SDimitry Andric 
1340b57cec5SDimitry Andric class SystemZDAGToDAGISel : public SelectionDAGISel {
1350b57cec5SDimitry Andric   const SystemZSubtarget *Subtarget;
1360b57cec5SDimitry Andric 
1370b57cec5SDimitry Andric   // Used by SystemZOperands.td to create integer constants.
1380b57cec5SDimitry Andric   inline SDValue getImm(const SDNode *Node, uint64_t Imm) const {
1390b57cec5SDimitry Andric     return CurDAG->getTargetConstant(Imm, SDLoc(Node), Node->getValueType(0));
1400b57cec5SDimitry Andric   }
1410b57cec5SDimitry Andric 
1420b57cec5SDimitry Andric   const SystemZTargetMachine &getTargetMachine() const {
1430b57cec5SDimitry Andric     return static_cast<const SystemZTargetMachine &>(TM);
1440b57cec5SDimitry Andric   }
1450b57cec5SDimitry Andric 
1460b57cec5SDimitry Andric   const SystemZInstrInfo *getInstrInfo() const {
1470b57cec5SDimitry Andric     return Subtarget->getInstrInfo();
1480b57cec5SDimitry Andric   }
1490b57cec5SDimitry Andric 
1500b57cec5SDimitry Andric   // Try to fold more of the base or index of AM into AM, where IsBase
1510b57cec5SDimitry Andric   // selects between the base and index.
1520b57cec5SDimitry Andric   bool expandAddress(SystemZAddressingMode &AM, bool IsBase) const;
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric   // Try to describe N in AM, returning true on success.
1550b57cec5SDimitry Andric   bool selectAddress(SDValue N, SystemZAddressingMode &AM) const;
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric   // Extract individual target operands from matched address AM.
1580b57cec5SDimitry Andric   void getAddressOperands(const SystemZAddressingMode &AM, EVT VT,
1590b57cec5SDimitry Andric                           SDValue &Base, SDValue &Disp) const;
1600b57cec5SDimitry Andric   void getAddressOperands(const SystemZAddressingMode &AM, EVT VT,
1610b57cec5SDimitry Andric                           SDValue &Base, SDValue &Disp, SDValue &Index) const;
1620b57cec5SDimitry Andric 
1630b57cec5SDimitry Andric   // Try to match Addr as a FormBD address with displacement type DR.
1640b57cec5SDimitry Andric   // Return true on success, storing the base and displacement in
1650b57cec5SDimitry Andric   // Base and Disp respectively.
1660b57cec5SDimitry Andric   bool selectBDAddr(SystemZAddressingMode::DispRange DR, SDValue Addr,
1670b57cec5SDimitry Andric                     SDValue &Base, SDValue &Disp) const;
1680b57cec5SDimitry Andric 
1690b57cec5SDimitry Andric   // Try to match Addr as a FormBDX address with displacement type DR.
1700b57cec5SDimitry Andric   // Return true on success and if the result had no index.  Store the
1710b57cec5SDimitry Andric   // base and displacement in Base and Disp respectively.
1720b57cec5SDimitry Andric   bool selectMVIAddr(SystemZAddressingMode::DispRange DR, SDValue Addr,
1730b57cec5SDimitry Andric                      SDValue &Base, SDValue &Disp) const;
1740b57cec5SDimitry Andric 
1750b57cec5SDimitry Andric   // Try to match Addr as a FormBDX* address of form Form with
1760b57cec5SDimitry Andric   // displacement type DR.  Return true on success, storing the base,
1770b57cec5SDimitry Andric   // displacement and index in Base, Disp and Index respectively.
1780b57cec5SDimitry Andric   bool selectBDXAddr(SystemZAddressingMode::AddrForm Form,
1790b57cec5SDimitry Andric                      SystemZAddressingMode::DispRange DR, SDValue Addr,
1800b57cec5SDimitry Andric                      SDValue &Base, SDValue &Disp, SDValue &Index) const;
1810b57cec5SDimitry Andric 
1820b57cec5SDimitry Andric   // PC-relative address matching routines used by SystemZOperands.td.
1830b57cec5SDimitry Andric   bool selectPCRelAddress(SDValue Addr, SDValue &Target) const {
1840b57cec5SDimitry Andric     if (SystemZISD::isPCREL(Addr.getOpcode())) {
1850b57cec5SDimitry Andric       Target = Addr.getOperand(0);
1860b57cec5SDimitry Andric       return true;
1870b57cec5SDimitry Andric     }
1880b57cec5SDimitry Andric     return false;
1890b57cec5SDimitry Andric   }
1900b57cec5SDimitry Andric 
1910b57cec5SDimitry Andric   // BD matching routines used by SystemZOperands.td.
1920b57cec5SDimitry Andric   bool selectBDAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp) const {
1930b57cec5SDimitry Andric     return selectBDAddr(SystemZAddressingMode::Disp12Only, Addr, Base, Disp);
1940b57cec5SDimitry Andric   }
1950b57cec5SDimitry Andric   bool selectBDAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
1960b57cec5SDimitry Andric     return selectBDAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp);
1970b57cec5SDimitry Andric   }
1980b57cec5SDimitry Andric   bool selectBDAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp) const {
1990b57cec5SDimitry Andric     return selectBDAddr(SystemZAddressingMode::Disp20Only, Addr, Base, Disp);
2000b57cec5SDimitry Andric   }
2010b57cec5SDimitry Andric   bool selectBDAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
2020b57cec5SDimitry Andric     return selectBDAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp);
2030b57cec5SDimitry Andric   }
2040b57cec5SDimitry Andric 
2050b57cec5SDimitry Andric   // MVI matching routines used by SystemZOperands.td.
2060b57cec5SDimitry Andric   bool selectMVIAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
2070b57cec5SDimitry Andric     return selectMVIAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp);
2080b57cec5SDimitry Andric   }
2090b57cec5SDimitry Andric   bool selectMVIAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
2100b57cec5SDimitry Andric     return selectMVIAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp);
2110b57cec5SDimitry Andric   }
2120b57cec5SDimitry Andric 
2130b57cec5SDimitry Andric   // BDX matching routines used by SystemZOperands.td.
2140b57cec5SDimitry Andric   bool selectBDXAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp,
2150b57cec5SDimitry Andric                            SDValue &Index) const {
2160b57cec5SDimitry Andric     return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
2170b57cec5SDimitry Andric                          SystemZAddressingMode::Disp12Only,
2180b57cec5SDimitry Andric                          Addr, Base, Disp, Index);
2190b57cec5SDimitry Andric   }
2200b57cec5SDimitry Andric   bool selectBDXAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
2210b57cec5SDimitry Andric                            SDValue &Index) const {
2220b57cec5SDimitry Andric     return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
2230b57cec5SDimitry Andric                          SystemZAddressingMode::Disp12Pair,
2240b57cec5SDimitry Andric                          Addr, Base, Disp, Index);
2250b57cec5SDimitry Andric   }
2260b57cec5SDimitry Andric   bool selectDynAlloc12Only(SDValue Addr, SDValue &Base, SDValue &Disp,
2270b57cec5SDimitry Andric                             SDValue &Index) const {
2280b57cec5SDimitry Andric     return selectBDXAddr(SystemZAddressingMode::FormBDXDynAlloc,
2290b57cec5SDimitry Andric                          SystemZAddressingMode::Disp12Only,
2300b57cec5SDimitry Andric                          Addr, Base, Disp, Index);
2310b57cec5SDimitry Andric   }
2320b57cec5SDimitry Andric   bool selectBDXAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp,
2330b57cec5SDimitry Andric                            SDValue &Index) const {
2340b57cec5SDimitry Andric     return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
2350b57cec5SDimitry Andric                          SystemZAddressingMode::Disp20Only,
2360b57cec5SDimitry Andric                          Addr, Base, Disp, Index);
2370b57cec5SDimitry Andric   }
2380b57cec5SDimitry Andric   bool selectBDXAddr20Only128(SDValue Addr, SDValue &Base, SDValue &Disp,
2390b57cec5SDimitry Andric                               SDValue &Index) const {
2400b57cec5SDimitry Andric     return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
2410b57cec5SDimitry Andric                          SystemZAddressingMode::Disp20Only128,
2420b57cec5SDimitry Andric                          Addr, Base, Disp, Index);
2430b57cec5SDimitry Andric   }
2440b57cec5SDimitry Andric   bool selectBDXAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
2450b57cec5SDimitry Andric                            SDValue &Index) const {
2460b57cec5SDimitry Andric     return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
2470b57cec5SDimitry Andric                          SystemZAddressingMode::Disp20Pair,
2480b57cec5SDimitry Andric                          Addr, Base, Disp, Index);
2490b57cec5SDimitry Andric   }
2500b57cec5SDimitry Andric   bool selectLAAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
2510b57cec5SDimitry Andric                           SDValue &Index) const {
2520b57cec5SDimitry Andric     return selectBDXAddr(SystemZAddressingMode::FormBDXLA,
2530b57cec5SDimitry Andric                          SystemZAddressingMode::Disp12Pair,
2540b57cec5SDimitry Andric                          Addr, Base, Disp, Index);
2550b57cec5SDimitry Andric   }
2560b57cec5SDimitry Andric   bool selectLAAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
2570b57cec5SDimitry Andric                           SDValue &Index) const {
2580b57cec5SDimitry Andric     return selectBDXAddr(SystemZAddressingMode::FormBDXLA,
2590b57cec5SDimitry Andric                          SystemZAddressingMode::Disp20Pair,
2600b57cec5SDimitry Andric                          Addr, Base, Disp, Index);
2610b57cec5SDimitry Andric   }
2620b57cec5SDimitry Andric 
2630b57cec5SDimitry Andric   // Try to match Addr as an address with a base, 12-bit displacement
2640b57cec5SDimitry Andric   // and index, where the index is element Elem of a vector.
2650b57cec5SDimitry Andric   // Return true on success, storing the base, displacement and vector
2660b57cec5SDimitry Andric   // in Base, Disp and Index respectively.
2670b57cec5SDimitry Andric   bool selectBDVAddr12Only(SDValue Addr, SDValue Elem, SDValue &Base,
2680b57cec5SDimitry Andric                            SDValue &Disp, SDValue &Index) const;
2690b57cec5SDimitry Andric 
2700b57cec5SDimitry Andric   // Check whether (or Op (and X InsertMask)) is effectively an insertion
2710b57cec5SDimitry Andric   // of X into bits InsertMask of some Y != Op.  Return true if so and
2720b57cec5SDimitry Andric   // set Op to that Y.
2730b57cec5SDimitry Andric   bool detectOrAndInsertion(SDValue &Op, uint64_t InsertMask) const;
2740b57cec5SDimitry Andric 
2750b57cec5SDimitry Andric   // Try to update RxSBG so that only the bits of RxSBG.Input in Mask are used.
2760b57cec5SDimitry Andric   // Return true on success.
2770b57cec5SDimitry Andric   bool refineRxSBGMask(RxSBGOperands &RxSBG, uint64_t Mask) const;
2780b57cec5SDimitry Andric 
2790b57cec5SDimitry Andric   // Try to fold some of RxSBG.Input into other fields of RxSBG.
2800b57cec5SDimitry Andric   // Return true on success.
2810b57cec5SDimitry Andric   bool expandRxSBG(RxSBGOperands &RxSBG) const;
2820b57cec5SDimitry Andric 
2830b57cec5SDimitry Andric   // Return an undefined value of type VT.
2840b57cec5SDimitry Andric   SDValue getUNDEF(const SDLoc &DL, EVT VT) const;
2850b57cec5SDimitry Andric 
2860b57cec5SDimitry Andric   // Convert N to VT, if it isn't already.
2870b57cec5SDimitry Andric   SDValue convertTo(const SDLoc &DL, EVT VT, SDValue N) const;
2880b57cec5SDimitry Andric 
2890b57cec5SDimitry Andric   // Try to implement AND or shift node N using RISBG with the zero flag set.
2900b57cec5SDimitry Andric   // Return the selected node on success, otherwise return null.
2910b57cec5SDimitry Andric   bool tryRISBGZero(SDNode *N);
2920b57cec5SDimitry Andric 
2930b57cec5SDimitry Andric   // Try to use RISBG or Opcode to implement OR or XOR node N.
2940b57cec5SDimitry Andric   // Return the selected node on success, otherwise return null.
2950b57cec5SDimitry Andric   bool tryRxSBG(SDNode *N, unsigned Opcode);
2960b57cec5SDimitry Andric 
2970b57cec5SDimitry Andric   // If Op0 is null, then Node is a constant that can be loaded using:
2980b57cec5SDimitry Andric   //
2990b57cec5SDimitry Andric   //   (Opcode UpperVal LowerVal)
3000b57cec5SDimitry Andric   //
3010b57cec5SDimitry Andric   // If Op0 is nonnull, then Node can be implemented using:
3020b57cec5SDimitry Andric   //
3030b57cec5SDimitry Andric   //   (Opcode (Opcode Op0 UpperVal) LowerVal)
3040b57cec5SDimitry Andric   void splitLargeImmediate(unsigned Opcode, SDNode *Node, SDValue Op0,
3050b57cec5SDimitry Andric                            uint64_t UpperVal, uint64_t LowerVal);
3060b57cec5SDimitry Andric 
3070b57cec5SDimitry Andric   void loadVectorConstant(const SystemZVectorConstantInfo &VCI,
3080b57cec5SDimitry Andric                           SDNode *Node);
3090b57cec5SDimitry Andric 
3105f757f3fSDimitry Andric   SDNode *loadPoolVectorConstant(APInt Val, EVT VT, SDLoc DL);
3115f757f3fSDimitry Andric 
3120b57cec5SDimitry Andric   // Try to use gather instruction Opcode to implement vector insertion N.
3130b57cec5SDimitry Andric   bool tryGather(SDNode *N, unsigned Opcode);
3140b57cec5SDimitry Andric 
3150b57cec5SDimitry Andric   // Try to use scatter instruction Opcode to implement store Store.
3160b57cec5SDimitry Andric   bool tryScatter(StoreSDNode *Store, unsigned Opcode);
3170b57cec5SDimitry Andric 
3180b57cec5SDimitry Andric   // Change a chain of {load; op; store} of the same value into a simple op
3190b57cec5SDimitry Andric   // through memory of that value, if the uses of the modified value and its
3200b57cec5SDimitry Andric   // address are suitable.
3210b57cec5SDimitry Andric   bool tryFoldLoadStoreIntoMemOperand(SDNode *Node);
3220b57cec5SDimitry Andric 
3230b57cec5SDimitry Andric   // Return true if Load and Store are loads and stores of the same size
3240b57cec5SDimitry Andric   // and are guaranteed not to overlap.  Such operations can be implemented
3250b57cec5SDimitry Andric   // using block (SS-format) instructions.
3260b57cec5SDimitry Andric   //
3270b57cec5SDimitry Andric   // Partial overlap would lead to incorrect code, since the block operations
3280b57cec5SDimitry Andric   // are logically bytewise, even though they have a fast path for the
3290b57cec5SDimitry Andric   // non-overlapping case.  We also need to avoid full overlap (i.e. two
3300b57cec5SDimitry Andric   // addresses that might be equal at run time) because although that case
3310b57cec5SDimitry Andric   // would be handled correctly, it might be implemented by millicode.
3320b57cec5SDimitry Andric   bool canUseBlockOperation(StoreSDNode *Store, LoadSDNode *Load) const;
3330b57cec5SDimitry Andric 
3340b57cec5SDimitry Andric   // N is a (store (load Y), X) pattern.  Return true if it can use an MVC
3350b57cec5SDimitry Andric   // from Y to X.
3360b57cec5SDimitry Andric   bool storeLoadCanUseMVC(SDNode *N) const;
3370b57cec5SDimitry Andric 
3380b57cec5SDimitry Andric   // N is a (store (op (load A[0]), (load A[1])), X) pattern.  Return true
3390b57cec5SDimitry Andric   // if A[1 - I] == X and if N can use a block operation like NC from A[I]
3400b57cec5SDimitry Andric   // to X.
3410b57cec5SDimitry Andric   bool storeLoadCanUseBlockBinary(SDNode *N, unsigned I) const;
3420b57cec5SDimitry Andric 
343e8d8bef9SDimitry Andric   // Return true if N (a load or a store) fullfills the alignment
344e8d8bef9SDimitry Andric   // requirements for a PC-relative access.
345e8d8bef9SDimitry Andric   bool storeLoadIsAligned(SDNode *N) const;
346e8d8bef9SDimitry Andric 
3470b57cec5SDimitry Andric   // Try to expand a boolean SELECT_CCMASK using an IPM sequence.
3480b57cec5SDimitry Andric   SDValue expandSelectBoolean(SDNode *Node);
3490b57cec5SDimitry Andric 
3500b57cec5SDimitry Andric public:
351bdd1243dSDimitry Andric   static char ID;
352bdd1243dSDimitry Andric 
353bdd1243dSDimitry Andric   SystemZDAGToDAGISel() = delete;
354bdd1243dSDimitry Andric 
3555f757f3fSDimitry Andric   SystemZDAGToDAGISel(SystemZTargetMachine &TM, CodeGenOptLevel OptLevel)
356bdd1243dSDimitry Andric       : SelectionDAGISel(ID, TM, OptLevel) {}
3570b57cec5SDimitry Andric 
3580b57cec5SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override {
3598bcb0991SDimitry Andric     const Function &F = MF.getFunction();
360480093f4SDimitry Andric     if (F.getFnAttribute("fentry-call").getValueAsString() != "true") {
361480093f4SDimitry Andric       if (F.hasFnAttribute("mnop-mcount"))
3628bcb0991SDimitry Andric         report_fatal_error("mnop-mcount only supported with fentry-call");
363480093f4SDimitry Andric       if (F.hasFnAttribute("mrecord-mcount"))
364480093f4SDimitry Andric         report_fatal_error("mrecord-mcount only supported with fentry-call");
365480093f4SDimitry Andric     }
3668bcb0991SDimitry Andric 
3670b57cec5SDimitry Andric     Subtarget = &MF.getSubtarget<SystemZSubtarget>();
3680b57cec5SDimitry Andric     return SelectionDAGISel::runOnMachineFunction(MF);
3690b57cec5SDimitry Andric   }
3700b57cec5SDimitry Andric 
3710b57cec5SDimitry Andric   // Override SelectionDAGISel.
3720b57cec5SDimitry Andric   void Select(SDNode *Node) override;
3735f757f3fSDimitry Andric   bool SelectInlineAsmMemoryOperand(const SDValue &Op,
3745f757f3fSDimitry Andric                                     InlineAsm::ConstraintCode ConstraintID,
3750b57cec5SDimitry Andric                                     std::vector<SDValue> &OutOps) override;
3760b57cec5SDimitry Andric   bool IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const override;
3770b57cec5SDimitry Andric   void PreprocessISelDAG() override;
3780b57cec5SDimitry Andric 
3790b57cec5SDimitry Andric   // Include the pieces autogenerated from the target description.
3800b57cec5SDimitry Andric   #include "SystemZGenDAGISel.inc"
3810b57cec5SDimitry Andric };
3820b57cec5SDimitry Andric } // end anonymous namespace
3830b57cec5SDimitry Andric 
384bdd1243dSDimitry Andric char SystemZDAGToDAGISel::ID = 0;
385bdd1243dSDimitry Andric 
386bdd1243dSDimitry Andric INITIALIZE_PASS(SystemZDAGToDAGISel, DEBUG_TYPE, PASS_NAME, false, false)
387bdd1243dSDimitry Andric 
3880b57cec5SDimitry Andric FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM,
3895f757f3fSDimitry Andric                                          CodeGenOptLevel OptLevel) {
3900b57cec5SDimitry Andric   return new SystemZDAGToDAGISel(TM, OptLevel);
3910b57cec5SDimitry Andric }
3920b57cec5SDimitry Andric 
3930b57cec5SDimitry Andric // Return true if Val should be selected as a displacement for an address
3940b57cec5SDimitry Andric // with range DR.  Here we're interested in the range of both the instruction
3950b57cec5SDimitry Andric // described by DR and of any pairing instruction.
3960b57cec5SDimitry Andric static bool selectDisp(SystemZAddressingMode::DispRange DR, int64_t Val) {
3970b57cec5SDimitry Andric   switch (DR) {
3980b57cec5SDimitry Andric   case SystemZAddressingMode::Disp12Only:
3990b57cec5SDimitry Andric     return isUInt<12>(Val);
4000b57cec5SDimitry Andric 
4010b57cec5SDimitry Andric   case SystemZAddressingMode::Disp12Pair:
4020b57cec5SDimitry Andric   case SystemZAddressingMode::Disp20Only:
4030b57cec5SDimitry Andric   case SystemZAddressingMode::Disp20Pair:
4040b57cec5SDimitry Andric     return isInt<20>(Val);
4050b57cec5SDimitry Andric 
4060b57cec5SDimitry Andric   case SystemZAddressingMode::Disp20Only128:
4070b57cec5SDimitry Andric     return isInt<20>(Val) && isInt<20>(Val + 8);
4080b57cec5SDimitry Andric   }
4090b57cec5SDimitry Andric   llvm_unreachable("Unhandled displacement range");
4100b57cec5SDimitry Andric }
4110b57cec5SDimitry Andric 
4120b57cec5SDimitry Andric // Change the base or index in AM to Value, where IsBase selects
4130b57cec5SDimitry Andric // between the base and index.
4140b57cec5SDimitry Andric static void changeComponent(SystemZAddressingMode &AM, bool IsBase,
4150b57cec5SDimitry Andric                             SDValue Value) {
4160b57cec5SDimitry Andric   if (IsBase)
4170b57cec5SDimitry Andric     AM.Base = Value;
4180b57cec5SDimitry Andric   else
4190b57cec5SDimitry Andric     AM.Index = Value;
4200b57cec5SDimitry Andric }
4210b57cec5SDimitry Andric 
4220b57cec5SDimitry Andric // The base or index of AM is equivalent to Value + ADJDYNALLOC,
4230b57cec5SDimitry Andric // where IsBase selects between the base and index.  Try to fold the
4240b57cec5SDimitry Andric // ADJDYNALLOC into AM.
4250b57cec5SDimitry Andric static bool expandAdjDynAlloc(SystemZAddressingMode &AM, bool IsBase,
4260b57cec5SDimitry Andric                               SDValue Value) {
4270b57cec5SDimitry Andric   if (AM.isDynAlloc() && !AM.IncludesDynAlloc) {
4280b57cec5SDimitry Andric     changeComponent(AM, IsBase, Value);
4290b57cec5SDimitry Andric     AM.IncludesDynAlloc = true;
4300b57cec5SDimitry Andric     return true;
4310b57cec5SDimitry Andric   }
4320b57cec5SDimitry Andric   return false;
4330b57cec5SDimitry Andric }
4340b57cec5SDimitry Andric 
4350b57cec5SDimitry Andric // The base of AM is equivalent to Base + Index.  Try to use Index as
4360b57cec5SDimitry Andric // the index register.
4370b57cec5SDimitry Andric static bool expandIndex(SystemZAddressingMode &AM, SDValue Base,
4380b57cec5SDimitry Andric                         SDValue Index) {
4390b57cec5SDimitry Andric   if (AM.hasIndexField() && !AM.Index.getNode()) {
4400b57cec5SDimitry Andric     AM.Base = Base;
4410b57cec5SDimitry Andric     AM.Index = Index;
4420b57cec5SDimitry Andric     return true;
4430b57cec5SDimitry Andric   }
4440b57cec5SDimitry Andric   return false;
4450b57cec5SDimitry Andric }
4460b57cec5SDimitry Andric 
4470b57cec5SDimitry Andric // The base or index of AM is equivalent to Op0 + Op1, where IsBase selects
4480b57cec5SDimitry Andric // between the base and index.  Try to fold Op1 into AM's displacement.
4490b57cec5SDimitry Andric static bool expandDisp(SystemZAddressingMode &AM, bool IsBase,
4500b57cec5SDimitry Andric                        SDValue Op0, uint64_t Op1) {
4510b57cec5SDimitry Andric   // First try adjusting the displacement.
4520b57cec5SDimitry Andric   int64_t TestDisp = AM.Disp + Op1;
4530b57cec5SDimitry Andric   if (selectDisp(AM.DR, TestDisp)) {
4540b57cec5SDimitry Andric     changeComponent(AM, IsBase, Op0);
4550b57cec5SDimitry Andric     AM.Disp = TestDisp;
4560b57cec5SDimitry Andric     return true;
4570b57cec5SDimitry Andric   }
4580b57cec5SDimitry Andric 
4590b57cec5SDimitry Andric   // We could consider forcing the displacement into a register and
4600b57cec5SDimitry Andric   // using it as an index, but it would need to be carefully tuned.
4610b57cec5SDimitry Andric   return false;
4620b57cec5SDimitry Andric }
4630b57cec5SDimitry Andric 
4640b57cec5SDimitry Andric bool SystemZDAGToDAGISel::expandAddress(SystemZAddressingMode &AM,
4650b57cec5SDimitry Andric                                         bool IsBase) const {
4660b57cec5SDimitry Andric   SDValue N = IsBase ? AM.Base : AM.Index;
4670b57cec5SDimitry Andric   unsigned Opcode = N.getOpcode();
468cb14a3feSDimitry Andric   // Look through no-op truncations.
469cb14a3feSDimitry Andric   if (Opcode == ISD::TRUNCATE && N.getOperand(0).getValueSizeInBits() <= 64) {
4700b57cec5SDimitry Andric     N = N.getOperand(0);
4710b57cec5SDimitry Andric     Opcode = N.getOpcode();
4720b57cec5SDimitry Andric   }
4730b57cec5SDimitry Andric   if (Opcode == ISD::ADD || CurDAG->isBaseWithConstantOffset(N)) {
4740b57cec5SDimitry Andric     SDValue Op0 = N.getOperand(0);
4750b57cec5SDimitry Andric     SDValue Op1 = N.getOperand(1);
4760b57cec5SDimitry Andric 
4770b57cec5SDimitry Andric     unsigned Op0Code = Op0->getOpcode();
4780b57cec5SDimitry Andric     unsigned Op1Code = Op1->getOpcode();
4790b57cec5SDimitry Andric 
4800b57cec5SDimitry Andric     if (Op0Code == SystemZISD::ADJDYNALLOC)
4810b57cec5SDimitry Andric       return expandAdjDynAlloc(AM, IsBase, Op1);
4820b57cec5SDimitry Andric     if (Op1Code == SystemZISD::ADJDYNALLOC)
4830b57cec5SDimitry Andric       return expandAdjDynAlloc(AM, IsBase, Op0);
4840b57cec5SDimitry Andric 
4850b57cec5SDimitry Andric     if (Op0Code == ISD::Constant)
4860b57cec5SDimitry Andric       return expandDisp(AM, IsBase, Op1,
4870b57cec5SDimitry Andric                         cast<ConstantSDNode>(Op0)->getSExtValue());
4880b57cec5SDimitry Andric     if (Op1Code == ISD::Constant)
4890b57cec5SDimitry Andric       return expandDisp(AM, IsBase, Op0,
4900b57cec5SDimitry Andric                         cast<ConstantSDNode>(Op1)->getSExtValue());
4910b57cec5SDimitry Andric 
4920b57cec5SDimitry Andric     if (IsBase && expandIndex(AM, Op0, Op1))
4930b57cec5SDimitry Andric       return true;
4940b57cec5SDimitry Andric   }
4950b57cec5SDimitry Andric   if (Opcode == SystemZISD::PCREL_OFFSET) {
4960b57cec5SDimitry Andric     SDValue Full = N.getOperand(0);
4970b57cec5SDimitry Andric     SDValue Base = N.getOperand(1);
4980b57cec5SDimitry Andric     SDValue Anchor = Base.getOperand(0);
4990b57cec5SDimitry Andric     uint64_t Offset = (cast<GlobalAddressSDNode>(Full)->getOffset() -
5000b57cec5SDimitry Andric                        cast<GlobalAddressSDNode>(Anchor)->getOffset());
5010b57cec5SDimitry Andric     return expandDisp(AM, IsBase, Base, Offset);
5020b57cec5SDimitry Andric   }
5030b57cec5SDimitry Andric   return false;
5040b57cec5SDimitry Andric }
5050b57cec5SDimitry Andric 
5060b57cec5SDimitry Andric // Return true if an instruction with displacement range DR should be
5070b57cec5SDimitry Andric // used for displacement value Val.  selectDisp(DR, Val) must already hold.
5080b57cec5SDimitry Andric static bool isValidDisp(SystemZAddressingMode::DispRange DR, int64_t Val) {
5090b57cec5SDimitry Andric   assert(selectDisp(DR, Val) && "Invalid displacement");
5100b57cec5SDimitry Andric   switch (DR) {
5110b57cec5SDimitry Andric   case SystemZAddressingMode::Disp12Only:
5120b57cec5SDimitry Andric   case SystemZAddressingMode::Disp20Only:
5130b57cec5SDimitry Andric   case SystemZAddressingMode::Disp20Only128:
5140b57cec5SDimitry Andric     return true;
5150b57cec5SDimitry Andric 
5160b57cec5SDimitry Andric   case SystemZAddressingMode::Disp12Pair:
5170b57cec5SDimitry Andric     // Use the other instruction if the displacement is too large.
5180b57cec5SDimitry Andric     return isUInt<12>(Val);
5190b57cec5SDimitry Andric 
5200b57cec5SDimitry Andric   case SystemZAddressingMode::Disp20Pair:
5210b57cec5SDimitry Andric     // Use the other instruction if the displacement is small enough.
5220b57cec5SDimitry Andric     return !isUInt<12>(Val);
5230b57cec5SDimitry Andric   }
5240b57cec5SDimitry Andric   llvm_unreachable("Unhandled displacement range");
5250b57cec5SDimitry Andric }
5260b57cec5SDimitry Andric 
5270b57cec5SDimitry Andric // Return true if Base + Disp + Index should be performed by LA(Y).
5280b57cec5SDimitry Andric static bool shouldUseLA(SDNode *Base, int64_t Disp, SDNode *Index) {
5290b57cec5SDimitry Andric   // Don't use LA(Y) for constants.
5300b57cec5SDimitry Andric   if (!Base)
5310b57cec5SDimitry Andric     return false;
5320b57cec5SDimitry Andric 
5330b57cec5SDimitry Andric   // Always use LA(Y) for frame addresses, since we know that the destination
5340b57cec5SDimitry Andric   // register is almost always (perhaps always) going to be different from
5350b57cec5SDimitry Andric   // the frame register.
5360b57cec5SDimitry Andric   if (Base->getOpcode() == ISD::FrameIndex)
5370b57cec5SDimitry Andric     return true;
5380b57cec5SDimitry Andric 
5390b57cec5SDimitry Andric   if (Disp) {
5400b57cec5SDimitry Andric     // Always use LA(Y) if there is a base, displacement and index.
5410b57cec5SDimitry Andric     if (Index)
5420b57cec5SDimitry Andric       return true;
5430b57cec5SDimitry Andric 
5440b57cec5SDimitry Andric     // Always use LA if the displacement is small enough.  It should always
5450b57cec5SDimitry Andric     // be no worse than AGHI (and better if it avoids a move).
5460b57cec5SDimitry Andric     if (isUInt<12>(Disp))
5470b57cec5SDimitry Andric       return true;
5480b57cec5SDimitry Andric 
5490b57cec5SDimitry Andric     // For similar reasons, always use LAY if the constant is too big for AGHI.
5500b57cec5SDimitry Andric     // LAY should be no worse than AGFI.
5510b57cec5SDimitry Andric     if (!isInt<16>(Disp))
5520b57cec5SDimitry Andric       return true;
5530b57cec5SDimitry Andric   } else {
5540b57cec5SDimitry Andric     // Don't use LA for plain registers.
5550b57cec5SDimitry Andric     if (!Index)
5560b57cec5SDimitry Andric       return false;
5570b57cec5SDimitry Andric 
5580b57cec5SDimitry Andric     // Don't use LA for plain addition if the index operand is only used
5590b57cec5SDimitry Andric     // once.  It should be a natural two-operand addition in that case.
5600b57cec5SDimitry Andric     if (Index->hasOneUse())
5610b57cec5SDimitry Andric       return false;
5620b57cec5SDimitry Andric 
5630b57cec5SDimitry Andric     // Prefer addition if the second operation is sign-extended, in the
5640b57cec5SDimitry Andric     // hope of using AGF.
5650b57cec5SDimitry Andric     unsigned IndexOpcode = Index->getOpcode();
5660b57cec5SDimitry Andric     if (IndexOpcode == ISD::SIGN_EXTEND ||
5670b57cec5SDimitry Andric         IndexOpcode == ISD::SIGN_EXTEND_INREG)
5680b57cec5SDimitry Andric       return false;
5690b57cec5SDimitry Andric   }
5700b57cec5SDimitry Andric 
5710b57cec5SDimitry Andric   // Don't use LA for two-operand addition if either operand is only
5720b57cec5SDimitry Andric   // used once.  The addition instructions are better in that case.
5730b57cec5SDimitry Andric   if (Base->hasOneUse())
5740b57cec5SDimitry Andric     return false;
5750b57cec5SDimitry Andric 
5760b57cec5SDimitry Andric   return true;
5770b57cec5SDimitry Andric }
5780b57cec5SDimitry Andric 
5790b57cec5SDimitry Andric // Return true if Addr is suitable for AM, updating AM if so.
5800b57cec5SDimitry Andric bool SystemZDAGToDAGISel::selectAddress(SDValue Addr,
5810b57cec5SDimitry Andric                                         SystemZAddressingMode &AM) const {
5820b57cec5SDimitry Andric   // Start out assuming that the address will need to be loaded separately,
5830b57cec5SDimitry Andric   // then try to extend it as much as we can.
5840b57cec5SDimitry Andric   AM.Base = Addr;
5850b57cec5SDimitry Andric 
5860b57cec5SDimitry Andric   // First try treating the address as a constant.
5870b57cec5SDimitry Andric   if (Addr.getOpcode() == ISD::Constant &&
5880b57cec5SDimitry Andric       expandDisp(AM, true, SDValue(),
5890b57cec5SDimitry Andric                  cast<ConstantSDNode>(Addr)->getSExtValue()))
5900b57cec5SDimitry Andric     ;
5910b57cec5SDimitry Andric   // Also see if it's a bare ADJDYNALLOC.
5920b57cec5SDimitry Andric   else if (Addr.getOpcode() == SystemZISD::ADJDYNALLOC &&
5930b57cec5SDimitry Andric            expandAdjDynAlloc(AM, true, SDValue()))
5940b57cec5SDimitry Andric     ;
5950b57cec5SDimitry Andric   else
5960b57cec5SDimitry Andric     // Otherwise try expanding each component.
5970b57cec5SDimitry Andric     while (expandAddress(AM, true) ||
5980b57cec5SDimitry Andric            (AM.Index.getNode() && expandAddress(AM, false)))
5990b57cec5SDimitry Andric       continue;
6000b57cec5SDimitry Andric 
6010b57cec5SDimitry Andric   // Reject cases where it isn't profitable to use LA(Y).
6020b57cec5SDimitry Andric   if (AM.Form == SystemZAddressingMode::FormBDXLA &&
6030b57cec5SDimitry Andric       !shouldUseLA(AM.Base.getNode(), AM.Disp, AM.Index.getNode()))
6040b57cec5SDimitry Andric     return false;
6050b57cec5SDimitry Andric 
6060b57cec5SDimitry Andric   // Reject cases where the other instruction in a pair should be used.
6070b57cec5SDimitry Andric   if (!isValidDisp(AM.DR, AM.Disp))
6080b57cec5SDimitry Andric     return false;
6090b57cec5SDimitry Andric 
6100b57cec5SDimitry Andric   // Make sure that ADJDYNALLOC is included where necessary.
6110b57cec5SDimitry Andric   if (AM.isDynAlloc() && !AM.IncludesDynAlloc)
6120b57cec5SDimitry Andric     return false;
6130b57cec5SDimitry Andric 
6140b57cec5SDimitry Andric   LLVM_DEBUG(AM.dump(CurDAG));
6150b57cec5SDimitry Andric   return true;
6160b57cec5SDimitry Andric }
6170b57cec5SDimitry Andric 
6180b57cec5SDimitry Andric // Insert a node into the DAG at least before Pos.  This will reposition
6190b57cec5SDimitry Andric // the node as needed, and will assign it a node ID that is <= Pos's ID.
6200b57cec5SDimitry Andric // Note that this does *not* preserve the uniqueness of node IDs!
6210b57cec5SDimitry Andric // The selection DAG must no longer depend on their uniqueness when this
6220b57cec5SDimitry Andric // function is used.
6230b57cec5SDimitry Andric static void insertDAGNode(SelectionDAG *DAG, SDNode *Pos, SDValue N) {
6240b57cec5SDimitry Andric   if (N->getNodeId() == -1 ||
6250b57cec5SDimitry Andric       (SelectionDAGISel::getUninvalidatedNodeId(N.getNode()) >
6260b57cec5SDimitry Andric        SelectionDAGISel::getUninvalidatedNodeId(Pos))) {
6270b57cec5SDimitry Andric     DAG->RepositionNode(Pos->getIterator(), N.getNode());
6280b57cec5SDimitry Andric     // Mark Node as invalid for pruning as after this it may be a successor to a
6290b57cec5SDimitry Andric     // selected node but otherwise be in the same position of Pos.
6300b57cec5SDimitry Andric     // Conservatively mark it with the same -abs(Id) to assure node id
6310b57cec5SDimitry Andric     // invariant is preserved.
6320b57cec5SDimitry Andric     N->setNodeId(Pos->getNodeId());
6330b57cec5SDimitry Andric     SelectionDAGISel::InvalidateNodeId(N.getNode());
6340b57cec5SDimitry Andric   }
6350b57cec5SDimitry Andric }
6360b57cec5SDimitry Andric 
6370b57cec5SDimitry Andric void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
6380b57cec5SDimitry Andric                                              EVT VT, SDValue &Base,
6390b57cec5SDimitry Andric                                              SDValue &Disp) const {
6400b57cec5SDimitry Andric   Base = AM.Base;
6410b57cec5SDimitry Andric   if (!Base.getNode())
6420b57cec5SDimitry Andric     // Register 0 means "no base".  This is mostly useful for shifts.
6430b57cec5SDimitry Andric     Base = CurDAG->getRegister(0, VT);
6440b57cec5SDimitry Andric   else if (Base.getOpcode() == ISD::FrameIndex) {
6450b57cec5SDimitry Andric     // Lower a FrameIndex to a TargetFrameIndex.
6460b57cec5SDimitry Andric     int64_t FrameIndex = cast<FrameIndexSDNode>(Base)->getIndex();
6470b57cec5SDimitry Andric     Base = CurDAG->getTargetFrameIndex(FrameIndex, VT);
6480b57cec5SDimitry Andric   } else if (Base.getValueType() != VT) {
6490b57cec5SDimitry Andric     // Truncate values from i64 to i32, for shifts.
6500b57cec5SDimitry Andric     assert(VT == MVT::i32 && Base.getValueType() == MVT::i64 &&
6510b57cec5SDimitry Andric            "Unexpected truncation");
6520b57cec5SDimitry Andric     SDLoc DL(Base);
6530b57cec5SDimitry Andric     SDValue Trunc = CurDAG->getNode(ISD::TRUNCATE, DL, VT, Base);
6540b57cec5SDimitry Andric     insertDAGNode(CurDAG, Base.getNode(), Trunc);
6550b57cec5SDimitry Andric     Base = Trunc;
6560b57cec5SDimitry Andric   }
6570b57cec5SDimitry Andric 
6580b57cec5SDimitry Andric   // Lower the displacement to a TargetConstant.
6590b57cec5SDimitry Andric   Disp = CurDAG->getTargetConstant(AM.Disp, SDLoc(Base), VT);
6600b57cec5SDimitry Andric }
6610b57cec5SDimitry Andric 
6620b57cec5SDimitry Andric void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
6630b57cec5SDimitry Andric                                              EVT VT, SDValue &Base,
6640b57cec5SDimitry Andric                                              SDValue &Disp,
6650b57cec5SDimitry Andric                                              SDValue &Index) const {
6660b57cec5SDimitry Andric   getAddressOperands(AM, VT, Base, Disp);
6670b57cec5SDimitry Andric 
6680b57cec5SDimitry Andric   Index = AM.Index;
6690b57cec5SDimitry Andric   if (!Index.getNode())
6700b57cec5SDimitry Andric     // Register 0 means "no index".
6710b57cec5SDimitry Andric     Index = CurDAG->getRegister(0, VT);
6720b57cec5SDimitry Andric }
6730b57cec5SDimitry Andric 
6740b57cec5SDimitry Andric bool SystemZDAGToDAGISel::selectBDAddr(SystemZAddressingMode::DispRange DR,
6750b57cec5SDimitry Andric                                        SDValue Addr, SDValue &Base,
6760b57cec5SDimitry Andric                                        SDValue &Disp) const {
6770b57cec5SDimitry Andric   SystemZAddressingMode AM(SystemZAddressingMode::FormBD, DR);
6780b57cec5SDimitry Andric   if (!selectAddress(Addr, AM))
6790b57cec5SDimitry Andric     return false;
6800b57cec5SDimitry Andric 
6810b57cec5SDimitry Andric   getAddressOperands(AM, Addr.getValueType(), Base, Disp);
6820b57cec5SDimitry Andric   return true;
6830b57cec5SDimitry Andric }
6840b57cec5SDimitry Andric 
6850b57cec5SDimitry Andric bool SystemZDAGToDAGISel::selectMVIAddr(SystemZAddressingMode::DispRange DR,
6860b57cec5SDimitry Andric                                         SDValue Addr, SDValue &Base,
6870b57cec5SDimitry Andric                                         SDValue &Disp) const {
6880b57cec5SDimitry Andric   SystemZAddressingMode AM(SystemZAddressingMode::FormBDXNormal, DR);
6890b57cec5SDimitry Andric   if (!selectAddress(Addr, AM) || AM.Index.getNode())
6900b57cec5SDimitry Andric     return false;
6910b57cec5SDimitry Andric 
6920b57cec5SDimitry Andric   getAddressOperands(AM, Addr.getValueType(), Base, Disp);
6930b57cec5SDimitry Andric   return true;
6940b57cec5SDimitry Andric }
6950b57cec5SDimitry Andric 
6960b57cec5SDimitry Andric bool SystemZDAGToDAGISel::selectBDXAddr(SystemZAddressingMode::AddrForm Form,
6970b57cec5SDimitry Andric                                         SystemZAddressingMode::DispRange DR,
6980b57cec5SDimitry Andric                                         SDValue Addr, SDValue &Base,
6990b57cec5SDimitry Andric                                         SDValue &Disp, SDValue &Index) const {
7000b57cec5SDimitry Andric   SystemZAddressingMode AM(Form, DR);
7010b57cec5SDimitry Andric   if (!selectAddress(Addr, AM))
7020b57cec5SDimitry Andric     return false;
7030b57cec5SDimitry Andric 
7040b57cec5SDimitry Andric   getAddressOperands(AM, Addr.getValueType(), Base, Disp, Index);
7050b57cec5SDimitry Andric   return true;
7060b57cec5SDimitry Andric }
7070b57cec5SDimitry Andric 
7080b57cec5SDimitry Andric bool SystemZDAGToDAGISel::selectBDVAddr12Only(SDValue Addr, SDValue Elem,
7090b57cec5SDimitry Andric                                               SDValue &Base,
7100b57cec5SDimitry Andric                                               SDValue &Disp,
7110b57cec5SDimitry Andric                                               SDValue &Index) const {
7120b57cec5SDimitry Andric   SDValue Regs[2];
7130b57cec5SDimitry Andric   if (selectBDXAddr12Only(Addr, Regs[0], Disp, Regs[1]) &&
7140b57cec5SDimitry Andric       Regs[0].getNode() && Regs[1].getNode()) {
7150b57cec5SDimitry Andric     for (unsigned int I = 0; I < 2; ++I) {
7160b57cec5SDimitry Andric       Base = Regs[I];
7170b57cec5SDimitry Andric       Index = Regs[1 - I];
7180b57cec5SDimitry Andric       // We can't tell here whether the index vector has the right type
7190b57cec5SDimitry Andric       // for the access; the caller needs to do that instead.
7200b57cec5SDimitry Andric       if (Index.getOpcode() == ISD::ZERO_EXTEND)
7210b57cec5SDimitry Andric         Index = Index.getOperand(0);
7220b57cec5SDimitry Andric       if (Index.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
7230b57cec5SDimitry Andric           Index.getOperand(1) == Elem) {
7240b57cec5SDimitry Andric         Index = Index.getOperand(0);
7250b57cec5SDimitry Andric         return true;
7260b57cec5SDimitry Andric       }
7270b57cec5SDimitry Andric     }
7280b57cec5SDimitry Andric   }
7290b57cec5SDimitry Andric   return false;
7300b57cec5SDimitry Andric }
7310b57cec5SDimitry Andric 
7320b57cec5SDimitry Andric bool SystemZDAGToDAGISel::detectOrAndInsertion(SDValue &Op,
7330b57cec5SDimitry Andric                                                uint64_t InsertMask) const {
7340b57cec5SDimitry Andric   // We're only interested in cases where the insertion is into some operand
7350b57cec5SDimitry Andric   // of Op, rather than into Op itself.  The only useful case is an AND.
7360b57cec5SDimitry Andric   if (Op.getOpcode() != ISD::AND)
7370b57cec5SDimitry Andric     return false;
7380b57cec5SDimitry Andric 
7390b57cec5SDimitry Andric   // We need a constant mask.
7400b57cec5SDimitry Andric   auto *MaskNode = dyn_cast<ConstantSDNode>(Op.getOperand(1).getNode());
7410b57cec5SDimitry Andric   if (!MaskNode)
7420b57cec5SDimitry Andric     return false;
7430b57cec5SDimitry Andric 
7440b57cec5SDimitry Andric   // It's not an insertion of Op.getOperand(0) if the two masks overlap.
7450b57cec5SDimitry Andric   uint64_t AndMask = MaskNode->getZExtValue();
7460b57cec5SDimitry Andric   if (InsertMask & AndMask)
7470b57cec5SDimitry Andric     return false;
7480b57cec5SDimitry Andric 
7490b57cec5SDimitry Andric   // It's only an insertion if all bits are covered or are known to be zero.
7500b57cec5SDimitry Andric   // The inner check covers all cases but is more expensive.
7510b57cec5SDimitry Andric   uint64_t Used = allOnes(Op.getValueSizeInBits());
7520b57cec5SDimitry Andric   if (Used != (AndMask | InsertMask)) {
7530b57cec5SDimitry Andric     KnownBits Known = CurDAG->computeKnownBits(Op.getOperand(0));
7540b57cec5SDimitry Andric     if (Used != (AndMask | InsertMask | Known.Zero.getZExtValue()))
7550b57cec5SDimitry Andric       return false;
7560b57cec5SDimitry Andric   }
7570b57cec5SDimitry Andric 
7580b57cec5SDimitry Andric   Op = Op.getOperand(0);
7590b57cec5SDimitry Andric   return true;
7600b57cec5SDimitry Andric }
7610b57cec5SDimitry Andric 
7620b57cec5SDimitry Andric bool SystemZDAGToDAGISel::refineRxSBGMask(RxSBGOperands &RxSBG,
7630b57cec5SDimitry Andric                                           uint64_t Mask) const {
7640b57cec5SDimitry Andric   const SystemZInstrInfo *TII = getInstrInfo();
7650b57cec5SDimitry Andric   if (RxSBG.Rotate != 0)
7660b57cec5SDimitry Andric     Mask = (Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate));
7670b57cec5SDimitry Andric   Mask &= RxSBG.Mask;
7680b57cec5SDimitry Andric   if (TII->isRxSBGMask(Mask, RxSBG.BitSize, RxSBG.Start, RxSBG.End)) {
7690b57cec5SDimitry Andric     RxSBG.Mask = Mask;
7700b57cec5SDimitry Andric     return true;
7710b57cec5SDimitry Andric   }
7720b57cec5SDimitry Andric   return false;
7730b57cec5SDimitry Andric }
7740b57cec5SDimitry Andric 
7750b57cec5SDimitry Andric // Return true if any bits of (RxSBG.Input & Mask) are significant.
7760b57cec5SDimitry Andric static bool maskMatters(RxSBGOperands &RxSBG, uint64_t Mask) {
7770b57cec5SDimitry Andric   // Rotate the mask in the same way as RxSBG.Input is rotated.
7780b57cec5SDimitry Andric   if (RxSBG.Rotate != 0)
7790b57cec5SDimitry Andric     Mask = ((Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate)));
7800b57cec5SDimitry Andric   return (Mask & RxSBG.Mask) != 0;
7810b57cec5SDimitry Andric }
7820b57cec5SDimitry Andric 
7830b57cec5SDimitry Andric bool SystemZDAGToDAGISel::expandRxSBG(RxSBGOperands &RxSBG) const {
7840b57cec5SDimitry Andric   SDValue N = RxSBG.Input;
7850b57cec5SDimitry Andric   unsigned Opcode = N.getOpcode();
7860b57cec5SDimitry Andric   switch (Opcode) {
7870b57cec5SDimitry Andric   case ISD::TRUNCATE: {
7880b57cec5SDimitry Andric     if (RxSBG.Opcode == SystemZ::RNSBG)
7890b57cec5SDimitry Andric       return false;
7905f757f3fSDimitry Andric     if (N.getOperand(0).getValueSizeInBits() > 64)
7915f757f3fSDimitry Andric       return false;
7920b57cec5SDimitry Andric     uint64_t BitSize = N.getValueSizeInBits();
7930b57cec5SDimitry Andric     uint64_t Mask = allOnes(BitSize);
7940b57cec5SDimitry Andric     if (!refineRxSBGMask(RxSBG, Mask))
7950b57cec5SDimitry Andric       return false;
7960b57cec5SDimitry Andric     RxSBG.Input = N.getOperand(0);
7970b57cec5SDimitry Andric     return true;
7980b57cec5SDimitry Andric   }
7990b57cec5SDimitry Andric   case ISD::AND: {
8000b57cec5SDimitry Andric     if (RxSBG.Opcode == SystemZ::RNSBG)
8010b57cec5SDimitry Andric       return false;
8020b57cec5SDimitry Andric 
8030b57cec5SDimitry Andric     auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
8040b57cec5SDimitry Andric     if (!MaskNode)
8050b57cec5SDimitry Andric       return false;
8060b57cec5SDimitry Andric 
8070b57cec5SDimitry Andric     SDValue Input = N.getOperand(0);
8080b57cec5SDimitry Andric     uint64_t Mask = MaskNode->getZExtValue();
8090b57cec5SDimitry Andric     if (!refineRxSBGMask(RxSBG, Mask)) {
8100b57cec5SDimitry Andric       // If some bits of Input are already known zeros, those bits will have
8110b57cec5SDimitry Andric       // been removed from the mask.  See if adding them back in makes the
8120b57cec5SDimitry Andric       // mask suitable.
8130b57cec5SDimitry Andric       KnownBits Known = CurDAG->computeKnownBits(Input);
8140b57cec5SDimitry Andric       Mask |= Known.Zero.getZExtValue();
8150b57cec5SDimitry Andric       if (!refineRxSBGMask(RxSBG, Mask))
8160b57cec5SDimitry Andric         return false;
8170b57cec5SDimitry Andric     }
8180b57cec5SDimitry Andric     RxSBG.Input = Input;
8190b57cec5SDimitry Andric     return true;
8200b57cec5SDimitry Andric   }
8210b57cec5SDimitry Andric 
8220b57cec5SDimitry Andric   case ISD::OR: {
8230b57cec5SDimitry Andric     if (RxSBG.Opcode != SystemZ::RNSBG)
8240b57cec5SDimitry Andric       return false;
8250b57cec5SDimitry Andric 
8260b57cec5SDimitry Andric     auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
8270b57cec5SDimitry Andric     if (!MaskNode)
8280b57cec5SDimitry Andric       return false;
8290b57cec5SDimitry Andric 
8300b57cec5SDimitry Andric     SDValue Input = N.getOperand(0);
8310b57cec5SDimitry Andric     uint64_t Mask = ~MaskNode->getZExtValue();
8320b57cec5SDimitry Andric     if (!refineRxSBGMask(RxSBG, Mask)) {
8330b57cec5SDimitry Andric       // If some bits of Input are already known ones, those bits will have
8340b57cec5SDimitry Andric       // been removed from the mask.  See if adding them back in makes the
8350b57cec5SDimitry Andric       // mask suitable.
8360b57cec5SDimitry Andric       KnownBits Known = CurDAG->computeKnownBits(Input);
8370b57cec5SDimitry Andric       Mask &= ~Known.One.getZExtValue();
8380b57cec5SDimitry Andric       if (!refineRxSBGMask(RxSBG, Mask))
8390b57cec5SDimitry Andric         return false;
8400b57cec5SDimitry Andric     }
8410b57cec5SDimitry Andric     RxSBG.Input = Input;
8420b57cec5SDimitry Andric     return true;
8430b57cec5SDimitry Andric   }
8440b57cec5SDimitry Andric 
8450b57cec5SDimitry Andric   case ISD::ROTL: {
8460b57cec5SDimitry Andric     // Any 64-bit rotate left can be merged into the RxSBG.
8470b57cec5SDimitry Andric     if (RxSBG.BitSize != 64 || N.getValueType() != MVT::i64)
8480b57cec5SDimitry Andric       return false;
8490b57cec5SDimitry Andric     auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
8500b57cec5SDimitry Andric     if (!CountNode)
8510b57cec5SDimitry Andric       return false;
8520b57cec5SDimitry Andric 
8530b57cec5SDimitry Andric     RxSBG.Rotate = (RxSBG.Rotate + CountNode->getZExtValue()) & 63;
8540b57cec5SDimitry Andric     RxSBG.Input = N.getOperand(0);
8550b57cec5SDimitry Andric     return true;
8560b57cec5SDimitry Andric   }
8570b57cec5SDimitry Andric 
8580b57cec5SDimitry Andric   case ISD::ANY_EXTEND:
8590b57cec5SDimitry Andric     // Bits above the extended operand are don't-care.
8600b57cec5SDimitry Andric     RxSBG.Input = N.getOperand(0);
8610b57cec5SDimitry Andric     return true;
8620b57cec5SDimitry Andric 
8630b57cec5SDimitry Andric   case ISD::ZERO_EXTEND:
8640b57cec5SDimitry Andric     if (RxSBG.Opcode != SystemZ::RNSBG) {
8650b57cec5SDimitry Andric       // Restrict the mask to the extended operand.
8660b57cec5SDimitry Andric       unsigned InnerBitSize = N.getOperand(0).getValueSizeInBits();
8670b57cec5SDimitry Andric       if (!refineRxSBGMask(RxSBG, allOnes(InnerBitSize)))
8680b57cec5SDimitry Andric         return false;
8690b57cec5SDimitry Andric 
8700b57cec5SDimitry Andric       RxSBG.Input = N.getOperand(0);
8710b57cec5SDimitry Andric       return true;
8720b57cec5SDimitry Andric     }
873bdd1243dSDimitry Andric     [[fallthrough]];
8740b57cec5SDimitry Andric 
8750b57cec5SDimitry Andric   case ISD::SIGN_EXTEND: {
8760b57cec5SDimitry Andric     // Check that the extension bits are don't-care (i.e. are masked out
8770b57cec5SDimitry Andric     // by the final mask).
8780b57cec5SDimitry Andric     unsigned BitSize = N.getValueSizeInBits();
8790b57cec5SDimitry Andric     unsigned InnerBitSize = N.getOperand(0).getValueSizeInBits();
8800b57cec5SDimitry Andric     if (maskMatters(RxSBG, allOnes(BitSize) - allOnes(InnerBitSize))) {
8810b57cec5SDimitry Andric       // In the case where only the sign bit is active, increase Rotate with
8820b57cec5SDimitry Andric       // the extension width.
8830b57cec5SDimitry Andric       if (RxSBG.Mask == 1 && RxSBG.Rotate == 1)
8840b57cec5SDimitry Andric         RxSBG.Rotate += (BitSize - InnerBitSize);
8850b57cec5SDimitry Andric       else
8860b57cec5SDimitry Andric         return false;
8870b57cec5SDimitry Andric     }
8880b57cec5SDimitry Andric 
8890b57cec5SDimitry Andric     RxSBG.Input = N.getOperand(0);
8900b57cec5SDimitry Andric     return true;
8910b57cec5SDimitry Andric   }
8920b57cec5SDimitry Andric 
8930b57cec5SDimitry Andric   case ISD::SHL: {
8940b57cec5SDimitry Andric     auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
8950b57cec5SDimitry Andric     if (!CountNode)
8960b57cec5SDimitry Andric       return false;
8970b57cec5SDimitry Andric 
8980b57cec5SDimitry Andric     uint64_t Count = CountNode->getZExtValue();
8990b57cec5SDimitry Andric     unsigned BitSize = N.getValueSizeInBits();
9000b57cec5SDimitry Andric     if (Count < 1 || Count >= BitSize)
9010b57cec5SDimitry Andric       return false;
9020b57cec5SDimitry Andric 
9030b57cec5SDimitry Andric     if (RxSBG.Opcode == SystemZ::RNSBG) {
9040b57cec5SDimitry Andric       // Treat (shl X, count) as (rotl X, size-count) as long as the bottom
9050b57cec5SDimitry Andric       // count bits from RxSBG.Input are ignored.
9060b57cec5SDimitry Andric       if (maskMatters(RxSBG, allOnes(Count)))
9070b57cec5SDimitry Andric         return false;
9080b57cec5SDimitry Andric     } else {
9090b57cec5SDimitry Andric       // Treat (shl X, count) as (and (rotl X, count), ~0<<count).
9100b57cec5SDimitry Andric       if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count) << Count))
9110b57cec5SDimitry Andric         return false;
9120b57cec5SDimitry Andric     }
9130b57cec5SDimitry Andric 
9140b57cec5SDimitry Andric     RxSBG.Rotate = (RxSBG.Rotate + Count) & 63;
9150b57cec5SDimitry Andric     RxSBG.Input = N.getOperand(0);
9160b57cec5SDimitry Andric     return true;
9170b57cec5SDimitry Andric   }
9180b57cec5SDimitry Andric 
9190b57cec5SDimitry Andric   case ISD::SRL:
9200b57cec5SDimitry Andric   case ISD::SRA: {
9210b57cec5SDimitry Andric     auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
9220b57cec5SDimitry Andric     if (!CountNode)
9230b57cec5SDimitry Andric       return false;
9240b57cec5SDimitry Andric 
9250b57cec5SDimitry Andric     uint64_t Count = CountNode->getZExtValue();
9260b57cec5SDimitry Andric     unsigned BitSize = N.getValueSizeInBits();
9270b57cec5SDimitry Andric     if (Count < 1 || Count >= BitSize)
9280b57cec5SDimitry Andric       return false;
9290b57cec5SDimitry Andric 
9300b57cec5SDimitry Andric     if (RxSBG.Opcode == SystemZ::RNSBG || Opcode == ISD::SRA) {
9310b57cec5SDimitry Andric       // Treat (srl|sra X, count) as (rotl X, size-count) as long as the top
9320b57cec5SDimitry Andric       // count bits from RxSBG.Input are ignored.
9330b57cec5SDimitry Andric       if (maskMatters(RxSBG, allOnes(Count) << (BitSize - Count)))
9340b57cec5SDimitry Andric         return false;
9350b57cec5SDimitry Andric     } else {
9360b57cec5SDimitry Andric       // Treat (srl X, count), mask) as (and (rotl X, size-count), ~0>>count),
9370b57cec5SDimitry Andric       // which is similar to SLL above.
9380b57cec5SDimitry Andric       if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count)))
9390b57cec5SDimitry Andric         return false;
9400b57cec5SDimitry Andric     }
9410b57cec5SDimitry Andric 
9420b57cec5SDimitry Andric     RxSBG.Rotate = (RxSBG.Rotate - Count) & 63;
9430b57cec5SDimitry Andric     RxSBG.Input = N.getOperand(0);
9440b57cec5SDimitry Andric     return true;
9450b57cec5SDimitry Andric   }
9460b57cec5SDimitry Andric   default:
9470b57cec5SDimitry Andric     return false;
9480b57cec5SDimitry Andric   }
9490b57cec5SDimitry Andric }
9500b57cec5SDimitry Andric 
9510b57cec5SDimitry Andric SDValue SystemZDAGToDAGISel::getUNDEF(const SDLoc &DL, EVT VT) const {
9520b57cec5SDimitry Andric   SDNode *N = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, VT);
9530b57cec5SDimitry Andric   return SDValue(N, 0);
9540b57cec5SDimitry Andric }
9550b57cec5SDimitry Andric 
9560b57cec5SDimitry Andric SDValue SystemZDAGToDAGISel::convertTo(const SDLoc &DL, EVT VT,
9570b57cec5SDimitry Andric                                        SDValue N) const {
9580b57cec5SDimitry Andric   if (N.getValueType() == MVT::i32 && VT == MVT::i64)
9590b57cec5SDimitry Andric     return CurDAG->getTargetInsertSubreg(SystemZ::subreg_l32,
9600b57cec5SDimitry Andric                                          DL, VT, getUNDEF(DL, MVT::i64), N);
9610b57cec5SDimitry Andric   if (N.getValueType() == MVT::i64 && VT == MVT::i32)
9620b57cec5SDimitry Andric     return CurDAG->getTargetExtractSubreg(SystemZ::subreg_l32, DL, VT, N);
9630b57cec5SDimitry Andric   assert(N.getValueType() == VT && "Unexpected value types");
9640b57cec5SDimitry Andric   return N;
9650b57cec5SDimitry Andric }
9660b57cec5SDimitry Andric 
9670b57cec5SDimitry Andric bool SystemZDAGToDAGISel::tryRISBGZero(SDNode *N) {
9680b57cec5SDimitry Andric   SDLoc DL(N);
9690b57cec5SDimitry Andric   EVT VT = N->getValueType(0);
9700b57cec5SDimitry Andric   if (!VT.isInteger() || VT.getSizeInBits() > 64)
9710b57cec5SDimitry Andric     return false;
9720b57cec5SDimitry Andric   RxSBGOperands RISBG(SystemZ::RISBG, SDValue(N, 0));
9730b57cec5SDimitry Andric   unsigned Count = 0;
9740b57cec5SDimitry Andric   while (expandRxSBG(RISBG))
9750b57cec5SDimitry Andric     // The widening or narrowing is expected to be free.
9760b57cec5SDimitry Andric     // Counting widening or narrowing as a saved operation will result in
9770b57cec5SDimitry Andric     // preferring an R*SBG over a simple shift/logical instruction.
9780b57cec5SDimitry Andric     if (RISBG.Input.getOpcode() != ISD::ANY_EXTEND &&
9790b57cec5SDimitry Andric         RISBG.Input.getOpcode() != ISD::TRUNCATE)
9800b57cec5SDimitry Andric       Count += 1;
98181ad6265SDimitry Andric   if (Count == 0 || isa<ConstantSDNode>(RISBG.Input))
9820b57cec5SDimitry Andric     return false;
9830b57cec5SDimitry Andric 
9840b57cec5SDimitry Andric   // Prefer to use normal shift instructions over RISBG, since they can handle
9850b57cec5SDimitry Andric   // all cases and are sometimes shorter.
9860b57cec5SDimitry Andric   if (Count == 1 && N->getOpcode() != ISD::AND)
9870b57cec5SDimitry Andric     return false;
9880b57cec5SDimitry Andric 
9890b57cec5SDimitry Andric   // Prefer register extensions like LLC over RISBG.  Also prefer to start
9900b57cec5SDimitry Andric   // out with normal ANDs if one instruction would be enough.  We can convert
9910b57cec5SDimitry Andric   // these ANDs into an RISBG later if a three-address instruction is useful.
9920b57cec5SDimitry Andric   if (RISBG.Rotate == 0) {
9930b57cec5SDimitry Andric     bool PreferAnd = false;
9940b57cec5SDimitry Andric     // Prefer AND for any 32-bit and-immediate operation.
9950b57cec5SDimitry Andric     if (VT == MVT::i32)
9960b57cec5SDimitry Andric       PreferAnd = true;
9970b57cec5SDimitry Andric     // As well as for any 64-bit operation that can be implemented via LLC(R),
9980b57cec5SDimitry Andric     // LLH(R), LLGT(R), or one of the and-immediate instructions.
9990b57cec5SDimitry Andric     else if (RISBG.Mask == 0xff ||
10000b57cec5SDimitry Andric              RISBG.Mask == 0xffff ||
10010b57cec5SDimitry Andric              RISBG.Mask == 0x7fffffff ||
10020b57cec5SDimitry Andric              SystemZ::isImmLF(~RISBG.Mask) ||
10030b57cec5SDimitry Andric              SystemZ::isImmHF(~RISBG.Mask))
10040b57cec5SDimitry Andric      PreferAnd = true;
10050b57cec5SDimitry Andric     // And likewise for the LLZRGF instruction, which doesn't have a register
10060b57cec5SDimitry Andric     // to register version.
10070b57cec5SDimitry Andric     else if (auto *Load = dyn_cast<LoadSDNode>(RISBG.Input)) {
10080b57cec5SDimitry Andric       if (Load->getMemoryVT() == MVT::i32 &&
10090b57cec5SDimitry Andric           (Load->getExtensionType() == ISD::EXTLOAD ||
10100b57cec5SDimitry Andric            Load->getExtensionType() == ISD::ZEXTLOAD) &&
10110b57cec5SDimitry Andric           RISBG.Mask == 0xffffff00 &&
10120b57cec5SDimitry Andric           Subtarget->hasLoadAndZeroRightmostByte())
10130b57cec5SDimitry Andric       PreferAnd = true;
10140b57cec5SDimitry Andric     }
10150b57cec5SDimitry Andric     if (PreferAnd) {
10160b57cec5SDimitry Andric       // Replace the current node with an AND.  Note that the current node
10170b57cec5SDimitry Andric       // might already be that same AND, in which case it is already CSE'd
10180b57cec5SDimitry Andric       // with it, and we must not call ReplaceNode.
10190b57cec5SDimitry Andric       SDValue In = convertTo(DL, VT, RISBG.Input);
10200b57cec5SDimitry Andric       SDValue Mask = CurDAG->getConstant(RISBG.Mask, DL, VT);
10210b57cec5SDimitry Andric       SDValue New = CurDAG->getNode(ISD::AND, DL, VT, In, Mask);
10220b57cec5SDimitry Andric       if (N != New.getNode()) {
10230b57cec5SDimitry Andric         insertDAGNode(CurDAG, N, Mask);
10240b57cec5SDimitry Andric         insertDAGNode(CurDAG, N, New);
10250b57cec5SDimitry Andric         ReplaceNode(N, New.getNode());
10260b57cec5SDimitry Andric         N = New.getNode();
10270b57cec5SDimitry Andric       }
10280b57cec5SDimitry Andric       // Now, select the machine opcode to implement this operation.
10290b57cec5SDimitry Andric       if (!N->isMachineOpcode())
10300b57cec5SDimitry Andric         SelectCode(N);
10310b57cec5SDimitry Andric       return true;
10320b57cec5SDimitry Andric     }
10330b57cec5SDimitry Andric   }
10340b57cec5SDimitry Andric 
10350b57cec5SDimitry Andric   unsigned Opcode = SystemZ::RISBG;
10360b57cec5SDimitry Andric   // Prefer RISBGN if available, since it does not clobber CC.
10370b57cec5SDimitry Andric   if (Subtarget->hasMiscellaneousExtensions())
10380b57cec5SDimitry Andric     Opcode = SystemZ::RISBGN;
10390b57cec5SDimitry Andric   EVT OpcodeVT = MVT::i64;
10400b57cec5SDimitry Andric   if (VT == MVT::i32 && Subtarget->hasHighWord() &&
10410b57cec5SDimitry Andric       // We can only use the 32-bit instructions if all source bits are
10420b57cec5SDimitry Andric       // in the low 32 bits without wrapping, both after rotation (because
10430b57cec5SDimitry Andric       // of the smaller range for Start and End) and before rotation
10440b57cec5SDimitry Andric       // (because the input value is truncated).
10450b57cec5SDimitry Andric       RISBG.Start >= 32 && RISBG.End >= RISBG.Start &&
10460b57cec5SDimitry Andric       ((RISBG.Start + RISBG.Rotate) & 63) >= 32 &&
10470b57cec5SDimitry Andric       ((RISBG.End + RISBG.Rotate) & 63) >=
10480b57cec5SDimitry Andric       ((RISBG.Start + RISBG.Rotate) & 63)) {
10490b57cec5SDimitry Andric     Opcode = SystemZ::RISBMux;
10500b57cec5SDimitry Andric     OpcodeVT = MVT::i32;
10510b57cec5SDimitry Andric     RISBG.Start &= 31;
10520b57cec5SDimitry Andric     RISBG.End &= 31;
10530b57cec5SDimitry Andric   }
10540b57cec5SDimitry Andric   SDValue Ops[5] = {
10550b57cec5SDimitry Andric     getUNDEF(DL, OpcodeVT),
10560b57cec5SDimitry Andric     convertTo(DL, OpcodeVT, RISBG.Input),
10570b57cec5SDimitry Andric     CurDAG->getTargetConstant(RISBG.Start, DL, MVT::i32),
10580b57cec5SDimitry Andric     CurDAG->getTargetConstant(RISBG.End | 128, DL, MVT::i32),
10590b57cec5SDimitry Andric     CurDAG->getTargetConstant(RISBG.Rotate, DL, MVT::i32)
10600b57cec5SDimitry Andric   };
10610b57cec5SDimitry Andric   SDValue New = convertTo(
10620b57cec5SDimitry Andric       DL, VT, SDValue(CurDAG->getMachineNode(Opcode, DL, OpcodeVT, Ops), 0));
10630b57cec5SDimitry Andric   ReplaceNode(N, New.getNode());
10640b57cec5SDimitry Andric   return true;
10650b57cec5SDimitry Andric }
10660b57cec5SDimitry Andric 
10670b57cec5SDimitry Andric bool SystemZDAGToDAGISel::tryRxSBG(SDNode *N, unsigned Opcode) {
10680b57cec5SDimitry Andric   SDLoc DL(N);
10690b57cec5SDimitry Andric   EVT VT = N->getValueType(0);
10700b57cec5SDimitry Andric   if (!VT.isInteger() || VT.getSizeInBits() > 64)
10710b57cec5SDimitry Andric     return false;
10720b57cec5SDimitry Andric   // Try treating each operand of N as the second operand of the RxSBG
10730b57cec5SDimitry Andric   // and see which goes deepest.
10740b57cec5SDimitry Andric   RxSBGOperands RxSBG[] = {
10750b57cec5SDimitry Andric     RxSBGOperands(Opcode, N->getOperand(0)),
10760b57cec5SDimitry Andric     RxSBGOperands(Opcode, N->getOperand(1))
10770b57cec5SDimitry Andric   };
10780b57cec5SDimitry Andric   unsigned Count[] = { 0, 0 };
10790b57cec5SDimitry Andric   for (unsigned I = 0; I < 2; ++I)
1080bdd1243dSDimitry Andric     while (RxSBG[I].Input->hasOneUse() && expandRxSBG(RxSBG[I]))
1081bdd1243dSDimitry Andric       // In cases of multiple users it seems better to keep the simple
1082bdd1243dSDimitry Andric       // instruction as they are one cycle faster, and it also helps in cases
1083bdd1243dSDimitry Andric       // where both inputs share a common node.
1084bdd1243dSDimitry Andric       // The widening or narrowing is expected to be free.  Counting widening
1085bdd1243dSDimitry Andric       // or narrowing as a saved operation will result in preferring an R*SBG
1086bdd1243dSDimitry Andric       // over a simple shift/logical instruction.
10870b57cec5SDimitry Andric       if (RxSBG[I].Input.getOpcode() != ISD::ANY_EXTEND &&
10880b57cec5SDimitry Andric           RxSBG[I].Input.getOpcode() != ISD::TRUNCATE)
10890b57cec5SDimitry Andric         Count[I] += 1;
10900b57cec5SDimitry Andric 
10910b57cec5SDimitry Andric   // Do nothing if neither operand is suitable.
10920b57cec5SDimitry Andric   if (Count[0] == 0 && Count[1] == 0)
10930b57cec5SDimitry Andric     return false;
10940b57cec5SDimitry Andric 
10950b57cec5SDimitry Andric   // Pick the deepest second operand.
10960b57cec5SDimitry Andric   unsigned I = Count[0] > Count[1] ? 0 : 1;
10970b57cec5SDimitry Andric   SDValue Op0 = N->getOperand(I ^ 1);
10980b57cec5SDimitry Andric 
10990b57cec5SDimitry Andric   // Prefer IC for character insertions from memory.
11000b57cec5SDimitry Andric   if (Opcode == SystemZ::ROSBG && (RxSBG[I].Mask & 0xff) == 0)
11010b57cec5SDimitry Andric     if (auto *Load = dyn_cast<LoadSDNode>(Op0.getNode()))
11020b57cec5SDimitry Andric       if (Load->getMemoryVT() == MVT::i8)
11030b57cec5SDimitry Andric         return false;
11040b57cec5SDimitry Andric 
11050b57cec5SDimitry Andric   // See whether we can avoid an AND in the first operand by converting
11060b57cec5SDimitry Andric   // ROSBG to RISBG.
11070b57cec5SDimitry Andric   if (Opcode == SystemZ::ROSBG && detectOrAndInsertion(Op0, RxSBG[I].Mask)) {
11080b57cec5SDimitry Andric     Opcode = SystemZ::RISBG;
11090b57cec5SDimitry Andric     // Prefer RISBGN if available, since it does not clobber CC.
11100b57cec5SDimitry Andric     if (Subtarget->hasMiscellaneousExtensions())
11110b57cec5SDimitry Andric       Opcode = SystemZ::RISBGN;
11120b57cec5SDimitry Andric   }
11130b57cec5SDimitry Andric 
11140b57cec5SDimitry Andric   SDValue Ops[5] = {
11150b57cec5SDimitry Andric     convertTo(DL, MVT::i64, Op0),
11160b57cec5SDimitry Andric     convertTo(DL, MVT::i64, RxSBG[I].Input),
11170b57cec5SDimitry Andric     CurDAG->getTargetConstant(RxSBG[I].Start, DL, MVT::i32),
11180b57cec5SDimitry Andric     CurDAG->getTargetConstant(RxSBG[I].End, DL, MVT::i32),
11190b57cec5SDimitry Andric     CurDAG->getTargetConstant(RxSBG[I].Rotate, DL, MVT::i32)
11200b57cec5SDimitry Andric   };
11210b57cec5SDimitry Andric   SDValue New = convertTo(
11220b57cec5SDimitry Andric       DL, VT, SDValue(CurDAG->getMachineNode(Opcode, DL, MVT::i64, Ops), 0));
11230b57cec5SDimitry Andric   ReplaceNode(N, New.getNode());
11240b57cec5SDimitry Andric   return true;
11250b57cec5SDimitry Andric }
11260b57cec5SDimitry Andric 
11270b57cec5SDimitry Andric void SystemZDAGToDAGISel::splitLargeImmediate(unsigned Opcode, SDNode *Node,
11280b57cec5SDimitry Andric                                               SDValue Op0, uint64_t UpperVal,
11290b57cec5SDimitry Andric                                               uint64_t LowerVal) {
11300b57cec5SDimitry Andric   EVT VT = Node->getValueType(0);
11310b57cec5SDimitry Andric   SDLoc DL(Node);
11320b57cec5SDimitry Andric   SDValue Upper = CurDAG->getConstant(UpperVal, DL, VT);
11330b57cec5SDimitry Andric   if (Op0.getNode())
11340b57cec5SDimitry Andric     Upper = CurDAG->getNode(Opcode, DL, VT, Op0, Upper);
11350b57cec5SDimitry Andric 
11360b57cec5SDimitry Andric   {
11370b57cec5SDimitry Andric     // When we haven't passed in Op0, Upper will be a constant. In order to
11380b57cec5SDimitry Andric     // prevent folding back to the large immediate in `Or = getNode(...)` we run
11390b57cec5SDimitry Andric     // SelectCode first and end up with an opaque machine node. This means that
11400b57cec5SDimitry Andric     // we need to use a handle to keep track of Upper in case it gets CSE'd by
11410b57cec5SDimitry Andric     // SelectCode.
11420b57cec5SDimitry Andric     //
11430b57cec5SDimitry Andric     // Note that in the case where Op0 is passed in we could just call
11440b57cec5SDimitry Andric     // SelectCode(Upper) later, along with the SelectCode(Or), and avoid needing
11450b57cec5SDimitry Andric     // the handle at all, but it's fine to do it here.
11460b57cec5SDimitry Andric     //
11470b57cec5SDimitry Andric     // TODO: This is a pretty hacky way to do this. Can we do something that
11480b57cec5SDimitry Andric     // doesn't require a two paragraph explanation?
11490b57cec5SDimitry Andric     HandleSDNode Handle(Upper);
11500b57cec5SDimitry Andric     SelectCode(Upper.getNode());
11510b57cec5SDimitry Andric     Upper = Handle.getValue();
11520b57cec5SDimitry Andric   }
11530b57cec5SDimitry Andric 
11540b57cec5SDimitry Andric   SDValue Lower = CurDAG->getConstant(LowerVal, DL, VT);
11550b57cec5SDimitry Andric   SDValue Or = CurDAG->getNode(Opcode, DL, VT, Upper, Lower);
11560b57cec5SDimitry Andric 
11570b57cec5SDimitry Andric   ReplaceNode(Node, Or.getNode());
11580b57cec5SDimitry Andric 
11590b57cec5SDimitry Andric   SelectCode(Or.getNode());
11600b57cec5SDimitry Andric }
11610b57cec5SDimitry Andric 
11620b57cec5SDimitry Andric void SystemZDAGToDAGISel::loadVectorConstant(
11630b57cec5SDimitry Andric     const SystemZVectorConstantInfo &VCI, SDNode *Node) {
11640b57cec5SDimitry Andric   assert((VCI.Opcode == SystemZISD::BYTE_MASK ||
11650b57cec5SDimitry Andric           VCI.Opcode == SystemZISD::REPLICATE ||
11660b57cec5SDimitry Andric           VCI.Opcode == SystemZISD::ROTATE_MASK) &&
11670b57cec5SDimitry Andric          "Bad opcode!");
11680b57cec5SDimitry Andric   assert(VCI.VecVT.getSizeInBits() == 128 && "Expected a vector type");
11690b57cec5SDimitry Andric   EVT VT = Node->getValueType(0);
11700b57cec5SDimitry Andric   SDLoc DL(Node);
11710b57cec5SDimitry Andric   SmallVector<SDValue, 2> Ops;
11720b57cec5SDimitry Andric   for (unsigned OpVal : VCI.OpVals)
11738bcb0991SDimitry Andric     Ops.push_back(CurDAG->getTargetConstant(OpVal, DL, MVT::i32));
11740b57cec5SDimitry Andric   SDValue Op = CurDAG->getNode(VCI.Opcode, DL, VCI.VecVT, Ops);
11750b57cec5SDimitry Andric 
11760b57cec5SDimitry Andric   if (VCI.VecVT == VT.getSimpleVT())
11770b57cec5SDimitry Andric     ReplaceNode(Node, Op.getNode());
11780b57cec5SDimitry Andric   else if (VT.getSizeInBits() == 128) {
11790b57cec5SDimitry Andric     SDValue BitCast = CurDAG->getNode(ISD::BITCAST, DL, VT, Op);
11800b57cec5SDimitry Andric     ReplaceNode(Node, BitCast.getNode());
11810b57cec5SDimitry Andric     SelectCode(BitCast.getNode());
11820b57cec5SDimitry Andric   } else { // float or double
11830b57cec5SDimitry Andric     unsigned SubRegIdx =
11840b57cec5SDimitry Andric         (VT.getSizeInBits() == 32 ? SystemZ::subreg_h32 : SystemZ::subreg_h64);
11850b57cec5SDimitry Andric     ReplaceNode(
11860b57cec5SDimitry Andric         Node, CurDAG->getTargetExtractSubreg(SubRegIdx, DL, VT, Op).getNode());
11870b57cec5SDimitry Andric   }
11880b57cec5SDimitry Andric   SelectCode(Op.getNode());
11890b57cec5SDimitry Andric }
11900b57cec5SDimitry Andric 
11915f757f3fSDimitry Andric SDNode *SystemZDAGToDAGISel::loadPoolVectorConstant(APInt Val, EVT VT, SDLoc DL) {
11925f757f3fSDimitry Andric   SDNode *ResNode;
11935f757f3fSDimitry Andric   assert (VT.getSizeInBits() == 128);
11945f757f3fSDimitry Andric 
11955f757f3fSDimitry Andric   SDValue CP = CurDAG->getTargetConstantPool(
11965f757f3fSDimitry Andric       ConstantInt::get(Type::getInt128Ty(*CurDAG->getContext()), Val),
11975f757f3fSDimitry Andric       TLI->getPointerTy(CurDAG->getDataLayout()));
11985f757f3fSDimitry Andric 
11995f757f3fSDimitry Andric   EVT PtrVT = CP.getValueType();
12005f757f3fSDimitry Andric   SDValue Ops[] = {
12015f757f3fSDimitry Andric     SDValue(CurDAG->getMachineNode(SystemZ::LARL, DL, PtrVT, CP), 0),
12025f757f3fSDimitry Andric     CurDAG->getTargetConstant(0, DL, PtrVT),
12035f757f3fSDimitry Andric     CurDAG->getRegister(0, PtrVT),
12045f757f3fSDimitry Andric     CurDAG->getEntryNode()
12055f757f3fSDimitry Andric   };
12065f757f3fSDimitry Andric   ResNode = CurDAG->getMachineNode(SystemZ::VL, DL, VT, MVT::Other, Ops);
12075f757f3fSDimitry Andric 
12085f757f3fSDimitry Andric   // Annotate ResNode with memory operand information so that MachineInstr
12095f757f3fSDimitry Andric   // queries work properly. This e.g. gives the register allocation the
12105f757f3fSDimitry Andric   // required information for rematerialization.
12115f757f3fSDimitry Andric   MachineFunction& MF = CurDAG->getMachineFunction();
12125f757f3fSDimitry Andric   MachineMemOperand *MemOp =
12135f757f3fSDimitry Andric       MF.getMachineMemOperand(MachinePointerInfo::getConstantPool(MF),
12145f757f3fSDimitry Andric                               MachineMemOperand::MOLoad, 16, Align(8));
12155f757f3fSDimitry Andric 
12165f757f3fSDimitry Andric   CurDAG->setNodeMemRefs(cast<MachineSDNode>(ResNode), {MemOp});
12175f757f3fSDimitry Andric   return ResNode;
12185f757f3fSDimitry Andric }
12195f757f3fSDimitry Andric 
12200b57cec5SDimitry Andric bool SystemZDAGToDAGISel::tryGather(SDNode *N, unsigned Opcode) {
12210b57cec5SDimitry Andric   SDValue ElemV = N->getOperand(2);
12220b57cec5SDimitry Andric   auto *ElemN = dyn_cast<ConstantSDNode>(ElemV);
12230b57cec5SDimitry Andric   if (!ElemN)
12240b57cec5SDimitry Andric     return false;
12250b57cec5SDimitry Andric 
12260b57cec5SDimitry Andric   unsigned Elem = ElemN->getZExtValue();
12270b57cec5SDimitry Andric   EVT VT = N->getValueType(0);
12280b57cec5SDimitry Andric   if (Elem >= VT.getVectorNumElements())
12290b57cec5SDimitry Andric     return false;
12300b57cec5SDimitry Andric 
12310b57cec5SDimitry Andric   auto *Load = dyn_cast<LoadSDNode>(N->getOperand(1));
12320b57cec5SDimitry Andric   if (!Load || !Load->hasNUsesOfValue(1, 0))
12330b57cec5SDimitry Andric     return false;
12340b57cec5SDimitry Andric   if (Load->getMemoryVT().getSizeInBits() !=
12350b57cec5SDimitry Andric       Load->getValueType(0).getSizeInBits())
12360b57cec5SDimitry Andric     return false;
12370b57cec5SDimitry Andric 
12380b57cec5SDimitry Andric   SDValue Base, Disp, Index;
12390b57cec5SDimitry Andric   if (!selectBDVAddr12Only(Load->getBasePtr(), ElemV, Base, Disp, Index) ||
12400b57cec5SDimitry Andric       Index.getValueType() != VT.changeVectorElementTypeToInteger())
12410b57cec5SDimitry Andric     return false;
12420b57cec5SDimitry Andric 
12430b57cec5SDimitry Andric   SDLoc DL(Load);
12440b57cec5SDimitry Andric   SDValue Ops[] = {
12450b57cec5SDimitry Andric     N->getOperand(0), Base, Disp, Index,
12460b57cec5SDimitry Andric     CurDAG->getTargetConstant(Elem, DL, MVT::i32), Load->getChain()
12470b57cec5SDimitry Andric   };
12480b57cec5SDimitry Andric   SDNode *Res = CurDAG->getMachineNode(Opcode, DL, VT, MVT::Other, Ops);
12490b57cec5SDimitry Andric   ReplaceUses(SDValue(Load, 1), SDValue(Res, 1));
12500b57cec5SDimitry Andric   ReplaceNode(N, Res);
12510b57cec5SDimitry Andric   return true;
12520b57cec5SDimitry Andric }
12530b57cec5SDimitry Andric 
12540b57cec5SDimitry Andric bool SystemZDAGToDAGISel::tryScatter(StoreSDNode *Store, unsigned Opcode) {
12550b57cec5SDimitry Andric   SDValue Value = Store->getValue();
12560b57cec5SDimitry Andric   if (Value.getOpcode() != ISD::EXTRACT_VECTOR_ELT)
12570b57cec5SDimitry Andric     return false;
12580b57cec5SDimitry Andric   if (Store->getMemoryVT().getSizeInBits() != Value.getValueSizeInBits())
12590b57cec5SDimitry Andric     return false;
12600b57cec5SDimitry Andric 
12610b57cec5SDimitry Andric   SDValue ElemV = Value.getOperand(1);
12620b57cec5SDimitry Andric   auto *ElemN = dyn_cast<ConstantSDNode>(ElemV);
12630b57cec5SDimitry Andric   if (!ElemN)
12640b57cec5SDimitry Andric     return false;
12650b57cec5SDimitry Andric 
12660b57cec5SDimitry Andric   SDValue Vec = Value.getOperand(0);
12670b57cec5SDimitry Andric   EVT VT = Vec.getValueType();
12680b57cec5SDimitry Andric   unsigned Elem = ElemN->getZExtValue();
12690b57cec5SDimitry Andric   if (Elem >= VT.getVectorNumElements())
12700b57cec5SDimitry Andric     return false;
12710b57cec5SDimitry Andric 
12720b57cec5SDimitry Andric   SDValue Base, Disp, Index;
12730b57cec5SDimitry Andric   if (!selectBDVAddr12Only(Store->getBasePtr(), ElemV, Base, Disp, Index) ||
12740b57cec5SDimitry Andric       Index.getValueType() != VT.changeVectorElementTypeToInteger())
12750b57cec5SDimitry Andric     return false;
12760b57cec5SDimitry Andric 
12770b57cec5SDimitry Andric   SDLoc DL(Store);
12780b57cec5SDimitry Andric   SDValue Ops[] = {
12790b57cec5SDimitry Andric     Vec, Base, Disp, Index, CurDAG->getTargetConstant(Elem, DL, MVT::i32),
12800b57cec5SDimitry Andric     Store->getChain()
12810b57cec5SDimitry Andric   };
12820b57cec5SDimitry Andric   ReplaceNode(Store, CurDAG->getMachineNode(Opcode, DL, MVT::Other, Ops));
12830b57cec5SDimitry Andric   return true;
12840b57cec5SDimitry Andric }
12850b57cec5SDimitry Andric 
12860b57cec5SDimitry Andric // Check whether or not the chain ending in StoreNode is suitable for doing
12870b57cec5SDimitry Andric // the {load; op; store} to modify transformation.
12880b57cec5SDimitry Andric static bool isFusableLoadOpStorePattern(StoreSDNode *StoreNode,
12890b57cec5SDimitry Andric                                         SDValue StoredVal, SelectionDAG *CurDAG,
12900b57cec5SDimitry Andric                                         LoadSDNode *&LoadNode,
12910b57cec5SDimitry Andric                                         SDValue &InputChain) {
12920b57cec5SDimitry Andric   // Is the stored value result 0 of the operation?
12930b57cec5SDimitry Andric   if (StoredVal.getResNo() != 0)
12940b57cec5SDimitry Andric     return false;
12950b57cec5SDimitry Andric 
12960b57cec5SDimitry Andric   // Are there other uses of the loaded value than the operation?
12970b57cec5SDimitry Andric   if (!StoredVal.getNode()->hasNUsesOfValue(1, 0))
12980b57cec5SDimitry Andric     return false;
12990b57cec5SDimitry Andric 
13000b57cec5SDimitry Andric   // Is the store non-extending and non-indexed?
13010b57cec5SDimitry Andric   if (!ISD::isNormalStore(StoreNode) || StoreNode->isNonTemporal())
13020b57cec5SDimitry Andric     return false;
13030b57cec5SDimitry Andric 
13040b57cec5SDimitry Andric   SDValue Load = StoredVal->getOperand(0);
13050b57cec5SDimitry Andric   // Is the stored value a non-extending and non-indexed load?
13060b57cec5SDimitry Andric   if (!ISD::isNormalLoad(Load.getNode()))
13070b57cec5SDimitry Andric     return false;
13080b57cec5SDimitry Andric 
13090b57cec5SDimitry Andric   // Return LoadNode by reference.
13100b57cec5SDimitry Andric   LoadNode = cast<LoadSDNode>(Load);
13110b57cec5SDimitry Andric 
13120b57cec5SDimitry Andric   // Is store the only read of the loaded value?
13130b57cec5SDimitry Andric   if (!Load.hasOneUse())
13140b57cec5SDimitry Andric     return false;
13150b57cec5SDimitry Andric 
13160b57cec5SDimitry Andric   // Is the address of the store the same as the load?
13170b57cec5SDimitry Andric   if (LoadNode->getBasePtr() != StoreNode->getBasePtr() ||
13180b57cec5SDimitry Andric       LoadNode->getOffset() != StoreNode->getOffset())
13190b57cec5SDimitry Andric     return false;
13200b57cec5SDimitry Andric 
13210b57cec5SDimitry Andric   // Check if the chain is produced by the load or is a TokenFactor with
13220b57cec5SDimitry Andric   // the load output chain as an operand. Return InputChain by reference.
13230b57cec5SDimitry Andric   SDValue Chain = StoreNode->getChain();
13240b57cec5SDimitry Andric 
13250b57cec5SDimitry Andric   bool ChainCheck = false;
13260b57cec5SDimitry Andric   if (Chain == Load.getValue(1)) {
13270b57cec5SDimitry Andric     ChainCheck = true;
13280b57cec5SDimitry Andric     InputChain = LoadNode->getChain();
13290b57cec5SDimitry Andric   } else if (Chain.getOpcode() == ISD::TokenFactor) {
13300b57cec5SDimitry Andric     SmallVector<SDValue, 4> ChainOps;
13310b57cec5SDimitry Andric     SmallVector<const SDNode *, 4> LoopWorklist;
13320b57cec5SDimitry Andric     SmallPtrSet<const SDNode *, 16> Visited;
13330b57cec5SDimitry Andric     const unsigned int Max = 1024;
13340b57cec5SDimitry Andric     for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i) {
13350b57cec5SDimitry Andric       SDValue Op = Chain.getOperand(i);
13360b57cec5SDimitry Andric       if (Op == Load.getValue(1)) {
13370b57cec5SDimitry Andric         ChainCheck = true;
13380b57cec5SDimitry Andric         // Drop Load, but keep its chain. No cycle check necessary.
13390b57cec5SDimitry Andric         ChainOps.push_back(Load.getOperand(0));
13400b57cec5SDimitry Andric         continue;
13410b57cec5SDimitry Andric       }
13420b57cec5SDimitry Andric       LoopWorklist.push_back(Op.getNode());
13430b57cec5SDimitry Andric       ChainOps.push_back(Op);
13440b57cec5SDimitry Andric     }
13450b57cec5SDimitry Andric 
13460b57cec5SDimitry Andric     if (ChainCheck) {
13470b57cec5SDimitry Andric       // Add the other operand of StoredVal to worklist.
13480b57cec5SDimitry Andric       for (SDValue Op : StoredVal->ops())
13490b57cec5SDimitry Andric         if (Op.getNode() != LoadNode)
13500b57cec5SDimitry Andric           LoopWorklist.push_back(Op.getNode());
13510b57cec5SDimitry Andric 
13520b57cec5SDimitry Andric       // Check if Load is reachable from any of the nodes in the worklist.
13530b57cec5SDimitry Andric       if (SDNode::hasPredecessorHelper(Load.getNode(), Visited, LoopWorklist, Max,
13540b57cec5SDimitry Andric                                        true))
13550b57cec5SDimitry Andric         return false;
13560b57cec5SDimitry Andric 
13570b57cec5SDimitry Andric       // Make a new TokenFactor with all the other input chains except
13580b57cec5SDimitry Andric       // for the load.
13590b57cec5SDimitry Andric       InputChain = CurDAG->getNode(ISD::TokenFactor, SDLoc(Chain),
13600b57cec5SDimitry Andric                                    MVT::Other, ChainOps);
13610b57cec5SDimitry Andric     }
13620b57cec5SDimitry Andric   }
13630b57cec5SDimitry Andric   if (!ChainCheck)
13640b57cec5SDimitry Andric     return false;
13650b57cec5SDimitry Andric 
13660b57cec5SDimitry Andric   return true;
13670b57cec5SDimitry Andric }
13680b57cec5SDimitry Andric 
13690b57cec5SDimitry Andric // Change a chain of {load; op; store} of the same value into a simple op
13700b57cec5SDimitry Andric // through memory of that value, if the uses of the modified value and its
13710b57cec5SDimitry Andric // address are suitable.
13720b57cec5SDimitry Andric //
13730b57cec5SDimitry Andric // The tablegen pattern memory operand pattern is currently not able to match
13740b57cec5SDimitry Andric // the case where the CC on the original operation are used.
13750b57cec5SDimitry Andric //
13760b57cec5SDimitry Andric // See the equivalent routine in X86ISelDAGToDAG for further comments.
13770b57cec5SDimitry Andric bool SystemZDAGToDAGISel::tryFoldLoadStoreIntoMemOperand(SDNode *Node) {
13780b57cec5SDimitry Andric   StoreSDNode *StoreNode = cast<StoreSDNode>(Node);
13790b57cec5SDimitry Andric   SDValue StoredVal = StoreNode->getOperand(1);
13800b57cec5SDimitry Andric   unsigned Opc = StoredVal->getOpcode();
13810b57cec5SDimitry Andric   SDLoc DL(StoreNode);
13820b57cec5SDimitry Andric 
13830b57cec5SDimitry Andric   // Before we try to select anything, make sure this is memory operand size
13840b57cec5SDimitry Andric   // and opcode we can handle. Note that this must match the code below that
13850b57cec5SDimitry Andric   // actually lowers the opcodes.
13860b57cec5SDimitry Andric   EVT MemVT = StoreNode->getMemoryVT();
13870b57cec5SDimitry Andric   unsigned NewOpc = 0;
13880b57cec5SDimitry Andric   bool NegateOperand = false;
13890b57cec5SDimitry Andric   switch (Opc) {
13900b57cec5SDimitry Andric   default:
13910b57cec5SDimitry Andric     return false;
13920b57cec5SDimitry Andric   case SystemZISD::SSUBO:
13930b57cec5SDimitry Andric     NegateOperand = true;
1394bdd1243dSDimitry Andric     [[fallthrough]];
13950b57cec5SDimitry Andric   case SystemZISD::SADDO:
13960b57cec5SDimitry Andric     if (MemVT == MVT::i32)
13970b57cec5SDimitry Andric       NewOpc = SystemZ::ASI;
13980b57cec5SDimitry Andric     else if (MemVT == MVT::i64)
13990b57cec5SDimitry Andric       NewOpc = SystemZ::AGSI;
14000b57cec5SDimitry Andric     else
14010b57cec5SDimitry Andric       return false;
14020b57cec5SDimitry Andric     break;
14030b57cec5SDimitry Andric   case SystemZISD::USUBO:
14040b57cec5SDimitry Andric     NegateOperand = true;
1405bdd1243dSDimitry Andric     [[fallthrough]];
14060b57cec5SDimitry Andric   case SystemZISD::UADDO:
14070b57cec5SDimitry Andric     if (MemVT == MVT::i32)
14080b57cec5SDimitry Andric       NewOpc = SystemZ::ALSI;
14090b57cec5SDimitry Andric     else if (MemVT == MVT::i64)
14100b57cec5SDimitry Andric       NewOpc = SystemZ::ALGSI;
14110b57cec5SDimitry Andric     else
14120b57cec5SDimitry Andric       return false;
14130b57cec5SDimitry Andric     break;
14140b57cec5SDimitry Andric   }
14150b57cec5SDimitry Andric 
14160b57cec5SDimitry Andric   LoadSDNode *LoadNode = nullptr;
14170b57cec5SDimitry Andric   SDValue InputChain;
14180b57cec5SDimitry Andric   if (!isFusableLoadOpStorePattern(StoreNode, StoredVal, CurDAG, LoadNode,
14190b57cec5SDimitry Andric                                    InputChain))
14200b57cec5SDimitry Andric     return false;
14210b57cec5SDimitry Andric 
14220b57cec5SDimitry Andric   SDValue Operand = StoredVal.getOperand(1);
14230b57cec5SDimitry Andric   auto *OperandC = dyn_cast<ConstantSDNode>(Operand);
14240b57cec5SDimitry Andric   if (!OperandC)
14250b57cec5SDimitry Andric     return false;
14260b57cec5SDimitry Andric   auto OperandV = OperandC->getAPIntValue();
14270b57cec5SDimitry Andric   if (NegateOperand)
14280b57cec5SDimitry Andric     OperandV = -OperandV;
142906c3fb27SDimitry Andric   if (OperandV.getSignificantBits() > 8)
14300b57cec5SDimitry Andric     return false;
14310b57cec5SDimitry Andric   Operand = CurDAG->getTargetConstant(OperandV, DL, MemVT);
14320b57cec5SDimitry Andric 
14330b57cec5SDimitry Andric   SDValue Base, Disp;
14340b57cec5SDimitry Andric   if (!selectBDAddr20Only(StoreNode->getBasePtr(), Base, Disp))
14350b57cec5SDimitry Andric     return false;
14360b57cec5SDimitry Andric 
14370b57cec5SDimitry Andric   SDValue Ops[] = { Base, Disp, Operand, InputChain };
14380b57cec5SDimitry Andric   MachineSDNode *Result =
14390b57cec5SDimitry Andric     CurDAG->getMachineNode(NewOpc, DL, MVT::i32, MVT::Other, Ops);
14400b57cec5SDimitry Andric   CurDAG->setNodeMemRefs(
14410b57cec5SDimitry Andric       Result, {StoreNode->getMemOperand(), LoadNode->getMemOperand()});
14420b57cec5SDimitry Andric 
14430b57cec5SDimitry Andric   ReplaceUses(SDValue(StoreNode, 0), SDValue(Result, 1));
14440b57cec5SDimitry Andric   ReplaceUses(SDValue(StoredVal.getNode(), 1), SDValue(Result, 0));
14450b57cec5SDimitry Andric   CurDAG->RemoveDeadNode(Node);
14460b57cec5SDimitry Andric   return true;
14470b57cec5SDimitry Andric }
14480b57cec5SDimitry Andric 
14490b57cec5SDimitry Andric bool SystemZDAGToDAGISel::canUseBlockOperation(StoreSDNode *Store,
14500b57cec5SDimitry Andric                                                LoadSDNode *Load) const {
14510b57cec5SDimitry Andric   // Check that the two memory operands have the same size.
14520b57cec5SDimitry Andric   if (Load->getMemoryVT() != Store->getMemoryVT())
14530b57cec5SDimitry Andric     return false;
14540b57cec5SDimitry Andric 
14550b57cec5SDimitry Andric   // Volatility stops an access from being decomposed.
14560b57cec5SDimitry Andric   if (Load->isVolatile() || Store->isVolatile())
14570b57cec5SDimitry Andric     return false;
14580b57cec5SDimitry Andric 
14590b57cec5SDimitry Andric   // There's no chance of overlap if the load is invariant.
14600b57cec5SDimitry Andric   if (Load->isInvariant() && Load->isDereferenceable())
14610b57cec5SDimitry Andric     return true;
14620b57cec5SDimitry Andric 
14630b57cec5SDimitry Andric   // Otherwise we need to check whether there's an alias.
14640b57cec5SDimitry Andric   const Value *V1 = Load->getMemOperand()->getValue();
14650b57cec5SDimitry Andric   const Value *V2 = Store->getMemOperand()->getValue();
14660b57cec5SDimitry Andric   if (!V1 || !V2)
14670b57cec5SDimitry Andric     return false;
14680b57cec5SDimitry Andric 
14690b57cec5SDimitry Andric   // Reject equality.
14700b57cec5SDimitry Andric   uint64_t Size = Load->getMemoryVT().getStoreSize();
14710b57cec5SDimitry Andric   int64_t End1 = Load->getSrcValueOffset() + Size;
14720b57cec5SDimitry Andric   int64_t End2 = Store->getSrcValueOffset() + Size;
14730b57cec5SDimitry Andric   if (V1 == V2 && End1 == End2)
14740b57cec5SDimitry Andric     return false;
14750b57cec5SDimitry Andric 
1476fe6060f1SDimitry Andric   return AA->isNoAlias(MemoryLocation(V1, End1, Load->getAAInfo()),
14770b57cec5SDimitry Andric                        MemoryLocation(V2, End2, Store->getAAInfo()));
14780b57cec5SDimitry Andric }
14790b57cec5SDimitry Andric 
14800b57cec5SDimitry Andric bool SystemZDAGToDAGISel::storeLoadCanUseMVC(SDNode *N) const {
14810b57cec5SDimitry Andric   auto *Store = cast<StoreSDNode>(N);
14820b57cec5SDimitry Andric   auto *Load = cast<LoadSDNode>(Store->getValue());
14830b57cec5SDimitry Andric 
14840b57cec5SDimitry Andric   // Prefer not to use MVC if either address can use ... RELATIVE LONG
14850b57cec5SDimitry Andric   // instructions.
14860b57cec5SDimitry Andric   uint64_t Size = Load->getMemoryVT().getStoreSize();
14870b57cec5SDimitry Andric   if (Size > 1 && Size <= 8) {
14880b57cec5SDimitry Andric     // Prefer LHRL, LRL and LGRL.
14890b57cec5SDimitry Andric     if (SystemZISD::isPCREL(Load->getBasePtr().getOpcode()))
14900b57cec5SDimitry Andric       return false;
14910b57cec5SDimitry Andric     // Prefer STHRL, STRL and STGRL.
14920b57cec5SDimitry Andric     if (SystemZISD::isPCREL(Store->getBasePtr().getOpcode()))
14930b57cec5SDimitry Andric       return false;
14940b57cec5SDimitry Andric   }
14950b57cec5SDimitry Andric 
14960b57cec5SDimitry Andric   return canUseBlockOperation(Store, Load);
14970b57cec5SDimitry Andric }
14980b57cec5SDimitry Andric 
14990b57cec5SDimitry Andric bool SystemZDAGToDAGISel::storeLoadCanUseBlockBinary(SDNode *N,
15000b57cec5SDimitry Andric                                                      unsigned I) const {
15010b57cec5SDimitry Andric   auto *StoreA = cast<StoreSDNode>(N);
15020b57cec5SDimitry Andric   auto *LoadA = cast<LoadSDNode>(StoreA->getValue().getOperand(1 - I));
15030b57cec5SDimitry Andric   auto *LoadB = cast<LoadSDNode>(StoreA->getValue().getOperand(I));
15045ffd83dbSDimitry Andric   return !LoadA->isVolatile() && LoadA->getMemoryVT() == LoadB->getMemoryVT() &&
15055ffd83dbSDimitry Andric          canUseBlockOperation(StoreA, LoadB);
15060b57cec5SDimitry Andric }
15070b57cec5SDimitry Andric 
1508e8d8bef9SDimitry Andric bool SystemZDAGToDAGISel::storeLoadIsAligned(SDNode *N) const {
1509e8d8bef9SDimitry Andric 
1510e8d8bef9SDimitry Andric   auto *MemAccess = cast<LSBaseSDNode>(N);
1511e8d8bef9SDimitry Andric   TypeSize StoreSize = MemAccess->getMemoryVT().getStoreSize();
1512e8d8bef9SDimitry Andric   SDValue BasePtr = MemAccess->getBasePtr();
1513e8d8bef9SDimitry Andric   MachineMemOperand *MMO = MemAccess->getMemOperand();
1514e8d8bef9SDimitry Andric   assert(MMO && "Expected a memory operand.");
1515e8d8bef9SDimitry Andric 
1516e8d8bef9SDimitry Andric   // The memory access must have a proper alignment and no index register.
151781ad6265SDimitry Andric   if (MemAccess->getAlign().value() < StoreSize ||
1518e8d8bef9SDimitry Andric       !MemAccess->getOffset().isUndef())
1519e8d8bef9SDimitry Andric     return false;
1520e8d8bef9SDimitry Andric 
1521e8d8bef9SDimitry Andric   // The MMO must not have an unaligned offset.
1522e8d8bef9SDimitry Andric   if (MMO->getOffset() % StoreSize != 0)
1523e8d8bef9SDimitry Andric     return false;
1524e8d8bef9SDimitry Andric 
1525e8d8bef9SDimitry Andric   // An access to GOT or the Constant Pool is aligned.
1526e8d8bef9SDimitry Andric   if (const PseudoSourceValue *PSV = MMO->getPseudoValue())
1527e8d8bef9SDimitry Andric     if ((PSV->isGOT() || PSV->isConstantPool()))
1528e8d8bef9SDimitry Andric       return true;
1529e8d8bef9SDimitry Andric 
1530e8d8bef9SDimitry Andric   // Check the alignment of a Global Address.
1531e8d8bef9SDimitry Andric   if (BasePtr.getNumOperands())
1532e8d8bef9SDimitry Andric     if (GlobalAddressSDNode *GA =
1533e8d8bef9SDimitry Andric         dyn_cast<GlobalAddressSDNode>(BasePtr.getOperand(0))) {
1534e8d8bef9SDimitry Andric       // The immediate offset must be aligned.
1535e8d8bef9SDimitry Andric       if (GA->getOffset() % StoreSize != 0)
1536e8d8bef9SDimitry Andric         return false;
1537e8d8bef9SDimitry Andric 
1538e8d8bef9SDimitry Andric       // The alignment of the symbol itself must be at least the store size.
1539e8d8bef9SDimitry Andric       const GlobalValue *GV = GA->getGlobal();
1540e8d8bef9SDimitry Andric       const DataLayout &DL = GV->getParent()->getDataLayout();
1541e8d8bef9SDimitry Andric       if (GV->getPointerAlignment(DL).value() < StoreSize)
1542e8d8bef9SDimitry Andric         return false;
1543e8d8bef9SDimitry Andric     }
1544e8d8bef9SDimitry Andric 
1545e8d8bef9SDimitry Andric   return true;
1546e8d8bef9SDimitry Andric }
1547e8d8bef9SDimitry Andric 
15480b57cec5SDimitry Andric void SystemZDAGToDAGISel::Select(SDNode *Node) {
15490b57cec5SDimitry Andric   // If we have a custom node, we already have selected!
15500b57cec5SDimitry Andric   if (Node->isMachineOpcode()) {
15510b57cec5SDimitry Andric     LLVM_DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
15520b57cec5SDimitry Andric     Node->setNodeId(-1);
15530b57cec5SDimitry Andric     return;
15540b57cec5SDimitry Andric   }
15550b57cec5SDimitry Andric 
15560b57cec5SDimitry Andric   unsigned Opcode = Node->getOpcode();
15570b57cec5SDimitry Andric   switch (Opcode) {
15580b57cec5SDimitry Andric   case ISD::OR:
15590b57cec5SDimitry Andric     if (Node->getOperand(1).getOpcode() != ISD::Constant)
15600b57cec5SDimitry Andric       if (tryRxSBG(Node, SystemZ::ROSBG))
15610b57cec5SDimitry Andric         return;
15620b57cec5SDimitry Andric     goto or_xor;
15630b57cec5SDimitry Andric 
15640b57cec5SDimitry Andric   case ISD::XOR:
15650b57cec5SDimitry Andric     if (Node->getOperand(1).getOpcode() != ISD::Constant)
15660b57cec5SDimitry Andric       if (tryRxSBG(Node, SystemZ::RXSBG))
15670b57cec5SDimitry Andric         return;
15680b57cec5SDimitry Andric     // Fall through.
15690b57cec5SDimitry Andric   or_xor:
15700b57cec5SDimitry Andric     // If this is a 64-bit operation in which both 32-bit halves are nonzero,
15710b57cec5SDimitry Andric     // split the operation into two.  If both operands here happen to be
15720b57cec5SDimitry Andric     // constant, leave this to common code to optimize.
15730b57cec5SDimitry Andric     if (Node->getValueType(0) == MVT::i64 &&
15740b57cec5SDimitry Andric         Node->getOperand(0).getOpcode() != ISD::Constant)
15750b57cec5SDimitry Andric       if (auto *Op1 = dyn_cast<ConstantSDNode>(Node->getOperand(1))) {
15760b57cec5SDimitry Andric         uint64_t Val = Op1->getZExtValue();
15770b57cec5SDimitry Andric         // Don't split the operation if we can match one of the combined
15780b57cec5SDimitry Andric         // logical operations provided by miscellaneous-extensions-3.
15790b57cec5SDimitry Andric         if (Subtarget->hasMiscellaneousExtensions3()) {
15800b57cec5SDimitry Andric           unsigned ChildOpcode = Node->getOperand(0).getOpcode();
15810b57cec5SDimitry Andric           // Check whether this expression matches NAND/NOR/NXOR.
15820b57cec5SDimitry Andric           if (Val == (uint64_t)-1 && Opcode == ISD::XOR)
15830b57cec5SDimitry Andric             if (ChildOpcode == ISD::AND || ChildOpcode == ISD::OR ||
15840b57cec5SDimitry Andric                 ChildOpcode == ISD::XOR)
15850b57cec5SDimitry Andric               break;
1586480093f4SDimitry Andric           // Check whether this expression matches OR-with-complement
1587480093f4SDimitry Andric           // (or matches an alternate pattern for NXOR).
1588480093f4SDimitry Andric           if (ChildOpcode == ISD::XOR) {
15890b57cec5SDimitry Andric             auto Op0 = Node->getOperand(0);
15900b57cec5SDimitry Andric             if (auto *Op0Op1 = dyn_cast<ConstantSDNode>(Op0->getOperand(1)))
15910b57cec5SDimitry Andric               if (Op0Op1->getZExtValue() == (uint64_t)-1)
15920b57cec5SDimitry Andric                 break;
15930b57cec5SDimitry Andric           }
15940b57cec5SDimitry Andric         }
15955f757f3fSDimitry Andric         // Don't split an XOR with -1 as LCGR/AGHI is more compact.
15965f757f3fSDimitry Andric         if (Opcode == ISD::XOR && Op1->isAllOnes())
15975f757f3fSDimitry Andric           break;
15980b57cec5SDimitry Andric         if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val)) {
15990b57cec5SDimitry Andric           splitLargeImmediate(Opcode, Node, Node->getOperand(0),
16000b57cec5SDimitry Andric                               Val - uint32_t(Val), uint32_t(Val));
16010b57cec5SDimitry Andric           return;
16020b57cec5SDimitry Andric         }
16030b57cec5SDimitry Andric       }
16040b57cec5SDimitry Andric     break;
16050b57cec5SDimitry Andric 
16060b57cec5SDimitry Andric   case ISD::AND:
16070b57cec5SDimitry Andric     if (Node->getOperand(1).getOpcode() != ISD::Constant)
16080b57cec5SDimitry Andric       if (tryRxSBG(Node, SystemZ::RNSBG))
16090b57cec5SDimitry Andric         return;
1610bdd1243dSDimitry Andric     [[fallthrough]];
16110b57cec5SDimitry Andric   case ISD::ROTL:
16120b57cec5SDimitry Andric   case ISD::SHL:
16130b57cec5SDimitry Andric   case ISD::SRL:
16140b57cec5SDimitry Andric   case ISD::ZERO_EXTEND:
16150b57cec5SDimitry Andric     if (tryRISBGZero(Node))
16160b57cec5SDimitry Andric       return;
16170b57cec5SDimitry Andric     break;
16180b57cec5SDimitry Andric 
16195f757f3fSDimitry Andric   case ISD::BSWAP:
16205f757f3fSDimitry Andric     if (Node->getValueType(0) == MVT::i128) {
16215f757f3fSDimitry Andric       SDLoc DL(Node);
16225f757f3fSDimitry Andric       SDValue Src = Node->getOperand(0);
16235f757f3fSDimitry Andric       Src = CurDAG->getNode(ISD::BITCAST, DL, MVT::v16i8, Src);
16245f757f3fSDimitry Andric 
16255f757f3fSDimitry Andric       uint64_t Bytes[2] = { 0x0706050403020100ULL, 0x0f0e0d0c0b0a0908ULL };
16265f757f3fSDimitry Andric       SDNode *Mask = loadPoolVectorConstant(APInt(128, Bytes), MVT::v16i8, DL);
16275f757f3fSDimitry Andric       SDValue Ops[] = { Src, Src, SDValue(Mask, 0) };
16285f757f3fSDimitry Andric       SDValue Res = SDValue(CurDAG->getMachineNode(SystemZ::VPERM, DL,
16295f757f3fSDimitry Andric                                                    MVT::v16i8, Ops), 0);
16305f757f3fSDimitry Andric 
16315f757f3fSDimitry Andric       Res = CurDAG->getNode(ISD::BITCAST, DL, MVT::i128, Res);
16325f757f3fSDimitry Andric       SDNode *ResNode = Res.getNode();
16335f757f3fSDimitry Andric       ReplaceNode(Node, ResNode);
16345f757f3fSDimitry Andric       SelectCode(Src.getNode());
16355f757f3fSDimitry Andric       SelectCode(ResNode);
16365f757f3fSDimitry Andric       return;
16375f757f3fSDimitry Andric     }
16385f757f3fSDimitry Andric     break;
16395f757f3fSDimitry Andric 
16400b57cec5SDimitry Andric   case ISD::Constant:
16410b57cec5SDimitry Andric     // If this is a 64-bit constant that is out of the range of LLILF,
16420b57cec5SDimitry Andric     // LLIHF and LGFI, split it into two 32-bit pieces.
16430b57cec5SDimitry Andric     if (Node->getValueType(0) == MVT::i64) {
1644*1db9f3b2SDimitry Andric       uint64_t Val = Node->getAsZExtVal();
16450b57cec5SDimitry Andric       if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val) && !isInt<32>(Val)) {
16460b57cec5SDimitry Andric         splitLargeImmediate(ISD::OR, Node, SDValue(), Val - uint32_t(Val),
16470b57cec5SDimitry Andric                             uint32_t(Val));
16480b57cec5SDimitry Andric         return;
16490b57cec5SDimitry Andric       }
16500b57cec5SDimitry Andric     }
16515f757f3fSDimitry Andric     if (Node->getValueType(0) == MVT::i128) {
16525f757f3fSDimitry Andric       const APInt &Val = cast<ConstantSDNode>(Node)->getAPIntValue();
16535f757f3fSDimitry Andric       SystemZVectorConstantInfo VCI(Val);
16545f757f3fSDimitry Andric       if (VCI.isVectorConstantLegal(*Subtarget)) {
16555f757f3fSDimitry Andric         loadVectorConstant(VCI, Node);
16565f757f3fSDimitry Andric         return;
16575f757f3fSDimitry Andric       }
16585f757f3fSDimitry Andric       // If we can't materialize the constant we need to use a literal pool.
16595f757f3fSDimitry Andric       SDNode *ResNode = loadPoolVectorConstant(Val, MVT::i128, SDLoc(Node));
16605f757f3fSDimitry Andric       ReplaceNode(Node, ResNode);
16615f757f3fSDimitry Andric       return;
16625f757f3fSDimitry Andric     }
16630b57cec5SDimitry Andric     break;
16640b57cec5SDimitry Andric 
16650b57cec5SDimitry Andric   case SystemZISD::SELECT_CCMASK: {
16660b57cec5SDimitry Andric     SDValue Op0 = Node->getOperand(0);
16670b57cec5SDimitry Andric     SDValue Op1 = Node->getOperand(1);
16680b57cec5SDimitry Andric     // Prefer to put any load first, so that it can be matched as a
16690b57cec5SDimitry Andric     // conditional load.  Likewise for constants in range for LOCHI.
16700b57cec5SDimitry Andric     if ((Op1.getOpcode() == ISD::LOAD && Op0.getOpcode() != ISD::LOAD) ||
16710b57cec5SDimitry Andric         (Subtarget->hasLoadStoreOnCond2() &&
16720b57cec5SDimitry Andric          Node->getValueType(0).isInteger() &&
16735f757f3fSDimitry Andric          Node->getValueType(0).getSizeInBits() <= 64 &&
16740b57cec5SDimitry Andric          Op1.getOpcode() == ISD::Constant &&
16750b57cec5SDimitry Andric          isInt<16>(cast<ConstantSDNode>(Op1)->getSExtValue()) &&
16760b57cec5SDimitry Andric          !(Op0.getOpcode() == ISD::Constant &&
16770b57cec5SDimitry Andric            isInt<16>(cast<ConstantSDNode>(Op0)->getSExtValue())))) {
16780b57cec5SDimitry Andric       SDValue CCValid = Node->getOperand(2);
16790b57cec5SDimitry Andric       SDValue CCMask = Node->getOperand(3);
1680*1db9f3b2SDimitry Andric       uint64_t ConstCCValid = CCValid.getNode()->getAsZExtVal();
1681*1db9f3b2SDimitry Andric       uint64_t ConstCCMask = CCMask.getNode()->getAsZExtVal();
16820b57cec5SDimitry Andric       // Invert the condition.
16838bcb0991SDimitry Andric       CCMask = CurDAG->getTargetConstant(ConstCCValid ^ ConstCCMask,
16848bcb0991SDimitry Andric                                          SDLoc(Node), CCMask.getValueType());
16850b57cec5SDimitry Andric       SDValue Op4 = Node->getOperand(4);
16860b57cec5SDimitry Andric       SDNode *UpdatedNode =
16870b57cec5SDimitry Andric         CurDAG->UpdateNodeOperands(Node, Op1, Op0, CCValid, CCMask, Op4);
16880b57cec5SDimitry Andric       if (UpdatedNode != Node) {
16890b57cec5SDimitry Andric         // In case this node already exists then replace Node with it.
16900b57cec5SDimitry Andric         ReplaceNode(Node, UpdatedNode);
16910b57cec5SDimitry Andric         Node = UpdatedNode;
16920b57cec5SDimitry Andric       }
16930b57cec5SDimitry Andric     }
16940b57cec5SDimitry Andric     break;
16950b57cec5SDimitry Andric   }
16960b57cec5SDimitry Andric 
16970b57cec5SDimitry Andric   case ISD::INSERT_VECTOR_ELT: {
16980b57cec5SDimitry Andric     EVT VT = Node->getValueType(0);
16990b57cec5SDimitry Andric     unsigned ElemBitSize = VT.getScalarSizeInBits();
17000b57cec5SDimitry Andric     if (ElemBitSize == 32) {
17010b57cec5SDimitry Andric       if (tryGather(Node, SystemZ::VGEF))
17020b57cec5SDimitry Andric         return;
17030b57cec5SDimitry Andric     } else if (ElemBitSize == 64) {
17040b57cec5SDimitry Andric       if (tryGather(Node, SystemZ::VGEG))
17050b57cec5SDimitry Andric         return;
17060b57cec5SDimitry Andric     }
17070b57cec5SDimitry Andric     break;
17080b57cec5SDimitry Andric   }
17090b57cec5SDimitry Andric 
17100b57cec5SDimitry Andric   case ISD::BUILD_VECTOR: {
17110b57cec5SDimitry Andric     auto *BVN = cast<BuildVectorSDNode>(Node);
17120b57cec5SDimitry Andric     SystemZVectorConstantInfo VCI(BVN);
17130b57cec5SDimitry Andric     if (VCI.isVectorConstantLegal(*Subtarget)) {
17140b57cec5SDimitry Andric       loadVectorConstant(VCI, Node);
17150b57cec5SDimitry Andric       return;
17160b57cec5SDimitry Andric     }
17170b57cec5SDimitry Andric     break;
17180b57cec5SDimitry Andric   }
17190b57cec5SDimitry Andric 
17200b57cec5SDimitry Andric   case ISD::ConstantFP: {
17210b57cec5SDimitry Andric     APFloat Imm = cast<ConstantFPSDNode>(Node)->getValueAPF();
17220b57cec5SDimitry Andric     if (Imm.isZero() || Imm.isNegZero())
17230b57cec5SDimitry Andric       break;
17240b57cec5SDimitry Andric     SystemZVectorConstantInfo VCI(Imm);
17250b57cec5SDimitry Andric     bool Success = VCI.isVectorConstantLegal(*Subtarget); (void)Success;
17260b57cec5SDimitry Andric     assert(Success && "Expected legal FP immediate");
17270b57cec5SDimitry Andric     loadVectorConstant(VCI, Node);
17280b57cec5SDimitry Andric     return;
17290b57cec5SDimitry Andric   }
17300b57cec5SDimitry Andric 
17310b57cec5SDimitry Andric   case ISD::STORE: {
17320b57cec5SDimitry Andric     if (tryFoldLoadStoreIntoMemOperand(Node))
17330b57cec5SDimitry Andric       return;
17340b57cec5SDimitry Andric     auto *Store = cast<StoreSDNode>(Node);
17350b57cec5SDimitry Andric     unsigned ElemBitSize = Store->getValue().getValueSizeInBits();
17360b57cec5SDimitry Andric     if (ElemBitSize == 32) {
17370b57cec5SDimitry Andric       if (tryScatter(Store, SystemZ::VSCEF))
17380b57cec5SDimitry Andric         return;
17390b57cec5SDimitry Andric     } else if (ElemBitSize == 64) {
17400b57cec5SDimitry Andric       if (tryScatter(Store, SystemZ::VSCEG))
17410b57cec5SDimitry Andric         return;
17420b57cec5SDimitry Andric     }
17430b57cec5SDimitry Andric     break;
17440b57cec5SDimitry Andric   }
17450b57cec5SDimitry Andric   }
17460b57cec5SDimitry Andric 
17470b57cec5SDimitry Andric   SelectCode(Node);
17480b57cec5SDimitry Andric }
17490b57cec5SDimitry Andric 
17505f757f3fSDimitry Andric bool SystemZDAGToDAGISel::SelectInlineAsmMemoryOperand(
17515f757f3fSDimitry Andric     const SDValue &Op, InlineAsm::ConstraintCode ConstraintID,
17520b57cec5SDimitry Andric     std::vector<SDValue> &OutOps) {
17530b57cec5SDimitry Andric   SystemZAddressingMode::AddrForm Form;
17540b57cec5SDimitry Andric   SystemZAddressingMode::DispRange DispRange;
17550b57cec5SDimitry Andric   SDValue Base, Disp, Index;
17560b57cec5SDimitry Andric 
17570b57cec5SDimitry Andric   switch(ConstraintID) {
17580b57cec5SDimitry Andric   default:
17590b57cec5SDimitry Andric     llvm_unreachable("Unexpected asm memory constraint");
17605f757f3fSDimitry Andric   case InlineAsm::ConstraintCode::i:
17615f757f3fSDimitry Andric   case InlineAsm::ConstraintCode::Q:
17625f757f3fSDimitry Andric   case InlineAsm::ConstraintCode::ZQ:
17630b57cec5SDimitry Andric     // Accept an address with a short displacement, but no index.
17640b57cec5SDimitry Andric     Form = SystemZAddressingMode::FormBD;
17650b57cec5SDimitry Andric     DispRange = SystemZAddressingMode::Disp12Only;
17660b57cec5SDimitry Andric     break;
17675f757f3fSDimitry Andric   case InlineAsm::ConstraintCode::R:
17685f757f3fSDimitry Andric   case InlineAsm::ConstraintCode::ZR:
17690b57cec5SDimitry Andric     // Accept an address with a short displacement and an index.
17700b57cec5SDimitry Andric     Form = SystemZAddressingMode::FormBDXNormal;
17710b57cec5SDimitry Andric     DispRange = SystemZAddressingMode::Disp12Only;
17720b57cec5SDimitry Andric     break;
17735f757f3fSDimitry Andric   case InlineAsm::ConstraintCode::S:
17745f757f3fSDimitry Andric   case InlineAsm::ConstraintCode::ZS:
17750b57cec5SDimitry Andric     // Accept an address with a long displacement, but no index.
17760b57cec5SDimitry Andric     Form = SystemZAddressingMode::FormBD;
17770b57cec5SDimitry Andric     DispRange = SystemZAddressingMode::Disp20Only;
17780b57cec5SDimitry Andric     break;
17795f757f3fSDimitry Andric   case InlineAsm::ConstraintCode::T:
17805f757f3fSDimitry Andric   case InlineAsm::ConstraintCode::m:
17815f757f3fSDimitry Andric   case InlineAsm::ConstraintCode::o:
17825f757f3fSDimitry Andric   case InlineAsm::ConstraintCode::p:
17835f757f3fSDimitry Andric   case InlineAsm::ConstraintCode::ZT:
17840b57cec5SDimitry Andric     // Accept an address with a long displacement and an index.
17850b57cec5SDimitry Andric     // m works the same as T, as this is the most general case.
17860b57cec5SDimitry Andric     // We don't really have any special handling of "offsettable"
17870b57cec5SDimitry Andric     // memory addresses, so just treat o the same as m.
17880b57cec5SDimitry Andric     Form = SystemZAddressingMode::FormBDXNormal;
17890b57cec5SDimitry Andric     DispRange = SystemZAddressingMode::Disp20Only;
17900b57cec5SDimitry Andric     break;
17910b57cec5SDimitry Andric   }
17920b57cec5SDimitry Andric 
17930b57cec5SDimitry Andric   if (selectBDXAddr(Form, DispRange, Op, Base, Disp, Index)) {
17940b57cec5SDimitry Andric     const TargetRegisterClass *TRC =
17950b57cec5SDimitry Andric       Subtarget->getRegisterInfo()->getPointerRegClass(*MF);
17960b57cec5SDimitry Andric     SDLoc DL(Base);
17970b57cec5SDimitry Andric     SDValue RC = CurDAG->getTargetConstant(TRC->getID(), DL, MVT::i32);
17980b57cec5SDimitry Andric 
17990b57cec5SDimitry Andric     // Make sure that the base address doesn't go into %r0.
18000b57cec5SDimitry Andric     // If it's a TargetFrameIndex or a fixed register, we shouldn't do anything.
18010b57cec5SDimitry Andric     if (Base.getOpcode() != ISD::TargetFrameIndex &&
18020b57cec5SDimitry Andric         Base.getOpcode() != ISD::Register) {
18030b57cec5SDimitry Andric       Base =
18040b57cec5SDimitry Andric         SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
18050b57cec5SDimitry Andric                                        DL, Base.getValueType(),
18060b57cec5SDimitry Andric                                        Base, RC), 0);
18070b57cec5SDimitry Andric     }
18080b57cec5SDimitry Andric 
18090b57cec5SDimitry Andric     // Make sure that the index register isn't assigned to %r0 either.
18100b57cec5SDimitry Andric     if (Index.getOpcode() != ISD::Register) {
18110b57cec5SDimitry Andric       Index =
18120b57cec5SDimitry Andric         SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
18130b57cec5SDimitry Andric                                        DL, Index.getValueType(),
18140b57cec5SDimitry Andric                                        Index, RC), 0);
18150b57cec5SDimitry Andric     }
18160b57cec5SDimitry Andric 
18170b57cec5SDimitry Andric     OutOps.push_back(Base);
18180b57cec5SDimitry Andric     OutOps.push_back(Disp);
18190b57cec5SDimitry Andric     OutOps.push_back(Index);
18200b57cec5SDimitry Andric     return false;
18210b57cec5SDimitry Andric   }
18220b57cec5SDimitry Andric 
18230b57cec5SDimitry Andric   return true;
18240b57cec5SDimitry Andric }
18250b57cec5SDimitry Andric 
18260b57cec5SDimitry Andric // IsProfitableToFold - Returns true if is profitable to fold the specific
18270b57cec5SDimitry Andric // operand node N of U during instruction selection that starts at Root.
18280b57cec5SDimitry Andric bool
18290b57cec5SDimitry Andric SystemZDAGToDAGISel::IsProfitableToFold(SDValue N, SDNode *U,
18300b57cec5SDimitry Andric                                         SDNode *Root) const {
18310b57cec5SDimitry Andric   // We want to avoid folding a LOAD into an ICMP node if as a result
18320b57cec5SDimitry Andric   // we would be forced to spill the condition code into a GPR.
18330b57cec5SDimitry Andric   if (N.getOpcode() == ISD::LOAD && U->getOpcode() == SystemZISD::ICMP) {
18340b57cec5SDimitry Andric     if (!N.hasOneUse() || !U->hasOneUse())
18350b57cec5SDimitry Andric       return false;
18360b57cec5SDimitry Andric 
18370b57cec5SDimitry Andric     // The user of the CC value will usually be a CopyToReg into the
18380b57cec5SDimitry Andric     // physical CC register, which in turn is glued and chained to the
18390b57cec5SDimitry Andric     // actual instruction that uses the CC value.  Bail out if we have
18400b57cec5SDimitry Andric     // anything else than that.
18410b57cec5SDimitry Andric     SDNode *CCUser = *U->use_begin();
18420b57cec5SDimitry Andric     SDNode *CCRegUser = nullptr;
18430b57cec5SDimitry Andric     if (CCUser->getOpcode() == ISD::CopyToReg ||
18440b57cec5SDimitry Andric         cast<RegisterSDNode>(CCUser->getOperand(1))->getReg() == SystemZ::CC) {
18450b57cec5SDimitry Andric       for (auto *U : CCUser->uses()) {
18460b57cec5SDimitry Andric         if (CCRegUser == nullptr)
18470b57cec5SDimitry Andric           CCRegUser = U;
18480b57cec5SDimitry Andric         else if (CCRegUser != U)
18490b57cec5SDimitry Andric           return false;
18500b57cec5SDimitry Andric       }
18510b57cec5SDimitry Andric     }
18520b57cec5SDimitry Andric     if (CCRegUser == nullptr)
18530b57cec5SDimitry Andric       return false;
18540b57cec5SDimitry Andric 
18550b57cec5SDimitry Andric     // If the actual instruction is a branch, the only thing that remains to be
18560b57cec5SDimitry Andric     // checked is whether the CCUser chain is a predecessor of the load.
18570b57cec5SDimitry Andric     if (CCRegUser->isMachineOpcode() &&
18580b57cec5SDimitry Andric         CCRegUser->getMachineOpcode() == SystemZ::BRC)
18590b57cec5SDimitry Andric       return !N->isPredecessorOf(CCUser->getOperand(0).getNode());
18600b57cec5SDimitry Andric 
18610b57cec5SDimitry Andric     // Otherwise, the instruction may have multiple operands, and we need to
18620b57cec5SDimitry Andric     // verify that none of them are a predecessor of the load.  This is exactly
18630b57cec5SDimitry Andric     // the same check that would be done by common code if the CC setter were
18640b57cec5SDimitry Andric     // glued to the CC user, so simply invoke that check here.
18650b57cec5SDimitry Andric     if (!IsLegalToFold(N, U, CCRegUser, OptLevel, false))
18660b57cec5SDimitry Andric       return false;
18670b57cec5SDimitry Andric   }
18680b57cec5SDimitry Andric 
18690b57cec5SDimitry Andric   return true;
18700b57cec5SDimitry Andric }
18710b57cec5SDimitry Andric 
18720b57cec5SDimitry Andric namespace {
18730b57cec5SDimitry Andric // Represents a sequence for extracting a 0/1 value from an IPM result:
18740b57cec5SDimitry Andric // (((X ^ XORValue) + AddValue) >> Bit)
18750b57cec5SDimitry Andric struct IPMConversion {
18760b57cec5SDimitry Andric   IPMConversion(unsigned xorValue, int64_t addValue, unsigned bit)
18770b57cec5SDimitry Andric     : XORValue(xorValue), AddValue(addValue), Bit(bit) {}
18780b57cec5SDimitry Andric 
18790b57cec5SDimitry Andric   int64_t XORValue;
18800b57cec5SDimitry Andric   int64_t AddValue;
18810b57cec5SDimitry Andric   unsigned Bit;
18820b57cec5SDimitry Andric };
18830b57cec5SDimitry Andric } // end anonymous namespace
18840b57cec5SDimitry Andric 
18850b57cec5SDimitry Andric // Return a sequence for getting a 1 from an IPM result when CC has a
18860b57cec5SDimitry Andric // value in CCMask and a 0 when CC has a value in CCValid & ~CCMask.
18870b57cec5SDimitry Andric // The handling of CC values outside CCValid doesn't matter.
18880b57cec5SDimitry Andric static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) {
18890b57cec5SDimitry Andric   // Deal with cases where the result can be taken directly from a bit
18900b57cec5SDimitry Andric   // of the IPM result.
18910b57cec5SDimitry Andric   if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3)))
18920b57cec5SDimitry Andric     return IPMConversion(0, 0, SystemZ::IPM_CC);
18930b57cec5SDimitry Andric   if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3)))
18940b57cec5SDimitry Andric     return IPMConversion(0, 0, SystemZ::IPM_CC + 1);
18950b57cec5SDimitry Andric 
18960b57cec5SDimitry Andric   // Deal with cases where we can add a value to force the sign bit
18970b57cec5SDimitry Andric   // to contain the right value.  Putting the bit in 31 means we can
18980b57cec5SDimitry Andric   // use SRL rather than RISBG(L), and also makes it easier to get a
18990b57cec5SDimitry Andric   // 0/-1 value, so it has priority over the other tests below.
19000b57cec5SDimitry Andric   //
19010b57cec5SDimitry Andric   // These sequences rely on the fact that the upper two bits of the
19020b57cec5SDimitry Andric   // IPM result are zero.
19030b57cec5SDimitry Andric   uint64_t TopBit = uint64_t(1) << 31;
19040b57cec5SDimitry Andric   if (CCMask == (CCValid & SystemZ::CCMASK_0))
19050b57cec5SDimitry Andric     return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31);
19060b57cec5SDimitry Andric   if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1)))
19070b57cec5SDimitry Andric     return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31);
19080b57cec5SDimitry Andric   if (CCMask == (CCValid & (SystemZ::CCMASK_0
19090b57cec5SDimitry Andric                             | SystemZ::CCMASK_1
19100b57cec5SDimitry Andric                             | SystemZ::CCMASK_2)))
19110b57cec5SDimitry Andric     return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31);
19120b57cec5SDimitry Andric   if (CCMask == (CCValid & SystemZ::CCMASK_3))
19130b57cec5SDimitry Andric     return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31);
19140b57cec5SDimitry Andric   if (CCMask == (CCValid & (SystemZ::CCMASK_1
19150b57cec5SDimitry Andric                             | SystemZ::CCMASK_2
19160b57cec5SDimitry Andric                             | SystemZ::CCMASK_3)))
19170b57cec5SDimitry Andric     return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31);
19180b57cec5SDimitry Andric 
19190b57cec5SDimitry Andric   // Next try inverting the value and testing a bit.  0/1 could be
19200b57cec5SDimitry Andric   // handled this way too, but we dealt with that case above.
19210b57cec5SDimitry Andric   if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2)))
19220b57cec5SDimitry Andric     return IPMConversion(-1, 0, SystemZ::IPM_CC);
19230b57cec5SDimitry Andric 
19240b57cec5SDimitry Andric   // Handle cases where adding a value forces a non-sign bit to contain
19250b57cec5SDimitry Andric   // the right value.
19260b57cec5SDimitry Andric   if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2)))
19270b57cec5SDimitry Andric     return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1);
19280b57cec5SDimitry Andric   if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3)))
19290b57cec5SDimitry Andric     return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1);
19300b57cec5SDimitry Andric 
19310b57cec5SDimitry Andric   // The remaining cases are 1, 2, 0/1/3 and 0/2/3.  All these are
19320b57cec5SDimitry Andric   // can be done by inverting the low CC bit and applying one of the
19330b57cec5SDimitry Andric   // sign-based extractions above.
19340b57cec5SDimitry Andric   if (CCMask == (CCValid & SystemZ::CCMASK_1))
19350b57cec5SDimitry Andric     return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31);
19360b57cec5SDimitry Andric   if (CCMask == (CCValid & SystemZ::CCMASK_2))
19370b57cec5SDimitry Andric     return IPMConversion(1 << SystemZ::IPM_CC,
19380b57cec5SDimitry Andric                          TopBit - (3 << SystemZ::IPM_CC), 31);
19390b57cec5SDimitry Andric   if (CCMask == (CCValid & (SystemZ::CCMASK_0
19400b57cec5SDimitry Andric                             | SystemZ::CCMASK_1
19410b57cec5SDimitry Andric                             | SystemZ::CCMASK_3)))
19420b57cec5SDimitry Andric     return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31);
19430b57cec5SDimitry Andric   if (CCMask == (CCValid & (SystemZ::CCMASK_0
19440b57cec5SDimitry Andric                             | SystemZ::CCMASK_2
19450b57cec5SDimitry Andric                             | SystemZ::CCMASK_3)))
19460b57cec5SDimitry Andric     return IPMConversion(1 << SystemZ::IPM_CC,
19470b57cec5SDimitry Andric                          TopBit - (1 << SystemZ::IPM_CC), 31);
19480b57cec5SDimitry Andric 
19490b57cec5SDimitry Andric   llvm_unreachable("Unexpected CC combination");
19500b57cec5SDimitry Andric }
19510b57cec5SDimitry Andric 
19520b57cec5SDimitry Andric SDValue SystemZDAGToDAGISel::expandSelectBoolean(SDNode *Node) {
19530b57cec5SDimitry Andric   auto *TrueOp = dyn_cast<ConstantSDNode>(Node->getOperand(0));
19540b57cec5SDimitry Andric   auto *FalseOp = dyn_cast<ConstantSDNode>(Node->getOperand(1));
19550b57cec5SDimitry Andric   if (!TrueOp || !FalseOp)
19560b57cec5SDimitry Andric     return SDValue();
19570b57cec5SDimitry Andric   if (FalseOp->getZExtValue() != 0)
19580b57cec5SDimitry Andric     return SDValue();
19590b57cec5SDimitry Andric   if (TrueOp->getSExtValue() != 1 && TrueOp->getSExtValue() != -1)
19600b57cec5SDimitry Andric     return SDValue();
19610b57cec5SDimitry Andric 
19620b57cec5SDimitry Andric   auto *CCValidOp = dyn_cast<ConstantSDNode>(Node->getOperand(2));
19630b57cec5SDimitry Andric   auto *CCMaskOp = dyn_cast<ConstantSDNode>(Node->getOperand(3));
19640b57cec5SDimitry Andric   if (!CCValidOp || !CCMaskOp)
19650b57cec5SDimitry Andric     return SDValue();
19660b57cec5SDimitry Andric   int CCValid = CCValidOp->getZExtValue();
19670b57cec5SDimitry Andric   int CCMask = CCMaskOp->getZExtValue();
19680b57cec5SDimitry Andric 
19690b57cec5SDimitry Andric   SDLoc DL(Node);
19700b57cec5SDimitry Andric   SDValue CCReg = Node->getOperand(4);
19710b57cec5SDimitry Andric   IPMConversion IPM = getIPMConversion(CCValid, CCMask);
19720b57cec5SDimitry Andric   SDValue Result = CurDAG->getNode(SystemZISD::IPM, DL, MVT::i32, CCReg);
19730b57cec5SDimitry Andric 
19740b57cec5SDimitry Andric   if (IPM.XORValue)
19750b57cec5SDimitry Andric     Result = CurDAG->getNode(ISD::XOR, DL, MVT::i32, Result,
19760b57cec5SDimitry Andric                              CurDAG->getConstant(IPM.XORValue, DL, MVT::i32));
19770b57cec5SDimitry Andric 
19780b57cec5SDimitry Andric   if (IPM.AddValue)
19790b57cec5SDimitry Andric     Result = CurDAG->getNode(ISD::ADD, DL, MVT::i32, Result,
19800b57cec5SDimitry Andric                              CurDAG->getConstant(IPM.AddValue, DL, MVT::i32));
19810b57cec5SDimitry Andric 
19820b57cec5SDimitry Andric   EVT VT = Node->getValueType(0);
19830b57cec5SDimitry Andric   if (VT == MVT::i32 && IPM.Bit == 31) {
19840b57cec5SDimitry Andric     unsigned ShiftOp = TrueOp->getSExtValue() == 1 ? ISD::SRL : ISD::SRA;
19850b57cec5SDimitry Andric     Result = CurDAG->getNode(ShiftOp, DL, MVT::i32, Result,
19860b57cec5SDimitry Andric                              CurDAG->getConstant(IPM.Bit, DL, MVT::i32));
19870b57cec5SDimitry Andric   } else {
19880b57cec5SDimitry Andric     if (VT != MVT::i32)
19890b57cec5SDimitry Andric       Result = CurDAG->getNode(ISD::ANY_EXTEND, DL, VT, Result);
19900b57cec5SDimitry Andric 
19910b57cec5SDimitry Andric     if (TrueOp->getSExtValue() == 1) {
19920b57cec5SDimitry Andric       // The SHR/AND sequence should get optimized to an RISBG.
19930b57cec5SDimitry Andric       Result = CurDAG->getNode(ISD::SRL, DL, VT, Result,
19940b57cec5SDimitry Andric                                CurDAG->getConstant(IPM.Bit, DL, MVT::i32));
19950b57cec5SDimitry Andric       Result = CurDAG->getNode(ISD::AND, DL, VT, Result,
19960b57cec5SDimitry Andric                                CurDAG->getConstant(1, DL, VT));
19970b57cec5SDimitry Andric     } else {
19980b57cec5SDimitry Andric       // Sign-extend from IPM.Bit using a pair of shifts.
19990b57cec5SDimitry Andric       int ShlAmt = VT.getSizeInBits() - 1 - IPM.Bit;
20000b57cec5SDimitry Andric       int SraAmt = VT.getSizeInBits() - 1;
20010b57cec5SDimitry Andric       Result = CurDAG->getNode(ISD::SHL, DL, VT, Result,
20020b57cec5SDimitry Andric                                CurDAG->getConstant(ShlAmt, DL, MVT::i32));
20030b57cec5SDimitry Andric       Result = CurDAG->getNode(ISD::SRA, DL, VT, Result,
20040b57cec5SDimitry Andric                                CurDAG->getConstant(SraAmt, DL, MVT::i32));
20050b57cec5SDimitry Andric     }
20060b57cec5SDimitry Andric   }
20070b57cec5SDimitry Andric 
20080b57cec5SDimitry Andric   return Result;
20090b57cec5SDimitry Andric }
20100b57cec5SDimitry Andric 
20110b57cec5SDimitry Andric void SystemZDAGToDAGISel::PreprocessISelDAG() {
20120b57cec5SDimitry Andric   // If we have conditional immediate loads, we always prefer
20130b57cec5SDimitry Andric   // using those over an IPM sequence.
20140b57cec5SDimitry Andric   if (Subtarget->hasLoadStoreOnCond2())
20150b57cec5SDimitry Andric     return;
20160b57cec5SDimitry Andric 
20170b57cec5SDimitry Andric   bool MadeChange = false;
20180b57cec5SDimitry Andric 
20190b57cec5SDimitry Andric   for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
20200b57cec5SDimitry Andric                                        E = CurDAG->allnodes_end();
20210b57cec5SDimitry Andric        I != E;) {
20220b57cec5SDimitry Andric     SDNode *N = &*I++;
20230b57cec5SDimitry Andric     if (N->use_empty())
20240b57cec5SDimitry Andric       continue;
20250b57cec5SDimitry Andric 
20260b57cec5SDimitry Andric     SDValue Res;
20270b57cec5SDimitry Andric     switch (N->getOpcode()) {
20280b57cec5SDimitry Andric     default: break;
20290b57cec5SDimitry Andric     case SystemZISD::SELECT_CCMASK:
20300b57cec5SDimitry Andric       Res = expandSelectBoolean(N);
20310b57cec5SDimitry Andric       break;
20320b57cec5SDimitry Andric     }
20330b57cec5SDimitry Andric 
20340b57cec5SDimitry Andric     if (Res) {
20350b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "SystemZ DAG preprocessing replacing:\nOld:    ");
20360b57cec5SDimitry Andric       LLVM_DEBUG(N->dump(CurDAG));
20370b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "\nNew: ");
20380b57cec5SDimitry Andric       LLVM_DEBUG(Res.getNode()->dump(CurDAG));
20390b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "\n");
20400b57cec5SDimitry Andric 
20410b57cec5SDimitry Andric       CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Res);
20420b57cec5SDimitry Andric       MadeChange = true;
20430b57cec5SDimitry Andric     }
20440b57cec5SDimitry Andric   }
20450b57cec5SDimitry Andric 
20460b57cec5SDimitry Andric   if (MadeChange)
20470b57cec5SDimitry Andric     CurDAG->RemoveDeadNodes();
20480b57cec5SDimitry Andric }
2049