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 347*0fca6ea1SDimitry Andric // Return the load extension type of a load or atomic load. 348*0fca6ea1SDimitry Andric ISD::LoadExtType getLoadExtType(SDNode *N) const; 349*0fca6ea1SDimitry Andric 3500b57cec5SDimitry Andric // Try to expand a boolean SELECT_CCMASK using an IPM sequence. 3510b57cec5SDimitry Andric SDValue expandSelectBoolean(SDNode *Node); 3520b57cec5SDimitry Andric 353*0fca6ea1SDimitry Andric // Return true if the flags of N and the subtarget allows for 354*0fca6ea1SDimitry Andric // reassociation, in which case a reg/reg opcode is needed as input to the 355*0fca6ea1SDimitry Andric // MachineCombiner. 356*0fca6ea1SDimitry Andric bool shouldSelectForReassoc(SDNode *N) const; 357bdd1243dSDimitry Andric 358*0fca6ea1SDimitry Andric public: 359bdd1243dSDimitry Andric SystemZDAGToDAGISel() = delete; 360bdd1243dSDimitry Andric 3615f757f3fSDimitry Andric SystemZDAGToDAGISel(SystemZTargetMachine &TM, CodeGenOptLevel OptLevel) 362*0fca6ea1SDimitry Andric : SelectionDAGISel(TM, OptLevel) {} 3630b57cec5SDimitry Andric 3640b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override { 3658bcb0991SDimitry Andric const Function &F = MF.getFunction(); 366480093f4SDimitry Andric if (F.getFnAttribute("fentry-call").getValueAsString() != "true") { 367480093f4SDimitry Andric if (F.hasFnAttribute("mnop-mcount")) 3688bcb0991SDimitry Andric report_fatal_error("mnop-mcount only supported with fentry-call"); 369480093f4SDimitry Andric if (F.hasFnAttribute("mrecord-mcount")) 370480093f4SDimitry Andric report_fatal_error("mrecord-mcount only supported with fentry-call"); 371480093f4SDimitry Andric } 3728bcb0991SDimitry Andric 3730b57cec5SDimitry Andric Subtarget = &MF.getSubtarget<SystemZSubtarget>(); 3740b57cec5SDimitry Andric return SelectionDAGISel::runOnMachineFunction(MF); 3750b57cec5SDimitry Andric } 3760b57cec5SDimitry Andric 3770b57cec5SDimitry Andric // Override SelectionDAGISel. 3780b57cec5SDimitry Andric void Select(SDNode *Node) override; 3795f757f3fSDimitry Andric bool SelectInlineAsmMemoryOperand(const SDValue &Op, 3805f757f3fSDimitry Andric InlineAsm::ConstraintCode ConstraintID, 3810b57cec5SDimitry Andric std::vector<SDValue> &OutOps) override; 3820b57cec5SDimitry Andric bool IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const override; 3830b57cec5SDimitry Andric void PreprocessISelDAG() override; 3840b57cec5SDimitry Andric 3850b57cec5SDimitry Andric // Include the pieces autogenerated from the target description. 3860b57cec5SDimitry Andric #include "SystemZGenDAGISel.inc" 3870b57cec5SDimitry Andric }; 388*0fca6ea1SDimitry Andric 389*0fca6ea1SDimitry Andric class SystemZDAGToDAGISelLegacy : public SelectionDAGISelLegacy { 390*0fca6ea1SDimitry Andric public: 391*0fca6ea1SDimitry Andric static char ID; 392*0fca6ea1SDimitry Andric explicit SystemZDAGToDAGISelLegacy(SystemZTargetMachine &TM, 393*0fca6ea1SDimitry Andric CodeGenOptLevel OptLevel) 394*0fca6ea1SDimitry Andric : SelectionDAGISelLegacy( 395*0fca6ea1SDimitry Andric ID, std::make_unique<SystemZDAGToDAGISel>(TM, OptLevel)) {} 396*0fca6ea1SDimitry Andric }; 3970b57cec5SDimitry Andric } // end anonymous namespace 3980b57cec5SDimitry Andric 399*0fca6ea1SDimitry Andric char SystemZDAGToDAGISelLegacy::ID = 0; 400bdd1243dSDimitry Andric 401*0fca6ea1SDimitry Andric INITIALIZE_PASS(SystemZDAGToDAGISelLegacy, DEBUG_TYPE, PASS_NAME, false, false) 402bdd1243dSDimitry Andric 4030b57cec5SDimitry Andric FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM, 4045f757f3fSDimitry Andric CodeGenOptLevel OptLevel) { 405*0fca6ea1SDimitry Andric return new SystemZDAGToDAGISelLegacy(TM, OptLevel); 4060b57cec5SDimitry Andric } 4070b57cec5SDimitry Andric 4080b57cec5SDimitry Andric // Return true if Val should be selected as a displacement for an address 4090b57cec5SDimitry Andric // with range DR. Here we're interested in the range of both the instruction 4100b57cec5SDimitry Andric // described by DR and of any pairing instruction. 4110b57cec5SDimitry Andric static bool selectDisp(SystemZAddressingMode::DispRange DR, int64_t Val) { 4120b57cec5SDimitry Andric switch (DR) { 4130b57cec5SDimitry Andric case SystemZAddressingMode::Disp12Only: 4140b57cec5SDimitry Andric return isUInt<12>(Val); 4150b57cec5SDimitry Andric 4160b57cec5SDimitry Andric case SystemZAddressingMode::Disp12Pair: 4170b57cec5SDimitry Andric case SystemZAddressingMode::Disp20Only: 4180b57cec5SDimitry Andric case SystemZAddressingMode::Disp20Pair: 4190b57cec5SDimitry Andric return isInt<20>(Val); 4200b57cec5SDimitry Andric 4210b57cec5SDimitry Andric case SystemZAddressingMode::Disp20Only128: 4220b57cec5SDimitry Andric return isInt<20>(Val) && isInt<20>(Val + 8); 4230b57cec5SDimitry Andric } 4240b57cec5SDimitry Andric llvm_unreachable("Unhandled displacement range"); 4250b57cec5SDimitry Andric } 4260b57cec5SDimitry Andric 4270b57cec5SDimitry Andric // Change the base or index in AM to Value, where IsBase selects 4280b57cec5SDimitry Andric // between the base and index. 4290b57cec5SDimitry Andric static void changeComponent(SystemZAddressingMode &AM, bool IsBase, 4300b57cec5SDimitry Andric SDValue Value) { 4310b57cec5SDimitry Andric if (IsBase) 4320b57cec5SDimitry Andric AM.Base = Value; 4330b57cec5SDimitry Andric else 4340b57cec5SDimitry Andric AM.Index = Value; 4350b57cec5SDimitry Andric } 4360b57cec5SDimitry Andric 4370b57cec5SDimitry Andric // The base or index of AM is equivalent to Value + ADJDYNALLOC, 4380b57cec5SDimitry Andric // where IsBase selects between the base and index. Try to fold the 4390b57cec5SDimitry Andric // ADJDYNALLOC into AM. 4400b57cec5SDimitry Andric static bool expandAdjDynAlloc(SystemZAddressingMode &AM, bool IsBase, 4410b57cec5SDimitry Andric SDValue Value) { 4420b57cec5SDimitry Andric if (AM.isDynAlloc() && !AM.IncludesDynAlloc) { 4430b57cec5SDimitry Andric changeComponent(AM, IsBase, Value); 4440b57cec5SDimitry Andric AM.IncludesDynAlloc = true; 4450b57cec5SDimitry Andric return true; 4460b57cec5SDimitry Andric } 4470b57cec5SDimitry Andric return false; 4480b57cec5SDimitry Andric } 4490b57cec5SDimitry Andric 4500b57cec5SDimitry Andric // The base of AM is equivalent to Base + Index. Try to use Index as 4510b57cec5SDimitry Andric // the index register. 4520b57cec5SDimitry Andric static bool expandIndex(SystemZAddressingMode &AM, SDValue Base, 4530b57cec5SDimitry Andric SDValue Index) { 4540b57cec5SDimitry Andric if (AM.hasIndexField() && !AM.Index.getNode()) { 4550b57cec5SDimitry Andric AM.Base = Base; 4560b57cec5SDimitry Andric AM.Index = Index; 4570b57cec5SDimitry Andric return true; 4580b57cec5SDimitry Andric } 4590b57cec5SDimitry Andric return false; 4600b57cec5SDimitry Andric } 4610b57cec5SDimitry Andric 4620b57cec5SDimitry Andric // The base or index of AM is equivalent to Op0 + Op1, where IsBase selects 4630b57cec5SDimitry Andric // between the base and index. Try to fold Op1 into AM's displacement. 4640b57cec5SDimitry Andric static bool expandDisp(SystemZAddressingMode &AM, bool IsBase, 4650b57cec5SDimitry Andric SDValue Op0, uint64_t Op1) { 4660b57cec5SDimitry Andric // First try adjusting the displacement. 4670b57cec5SDimitry Andric int64_t TestDisp = AM.Disp + Op1; 4680b57cec5SDimitry Andric if (selectDisp(AM.DR, TestDisp)) { 4690b57cec5SDimitry Andric changeComponent(AM, IsBase, Op0); 4700b57cec5SDimitry Andric AM.Disp = TestDisp; 4710b57cec5SDimitry Andric return true; 4720b57cec5SDimitry Andric } 4730b57cec5SDimitry Andric 4740b57cec5SDimitry Andric // We could consider forcing the displacement into a register and 4750b57cec5SDimitry Andric // using it as an index, but it would need to be carefully tuned. 4760b57cec5SDimitry Andric return false; 4770b57cec5SDimitry Andric } 4780b57cec5SDimitry Andric 4790b57cec5SDimitry Andric bool SystemZDAGToDAGISel::expandAddress(SystemZAddressingMode &AM, 4800b57cec5SDimitry Andric bool IsBase) const { 4810b57cec5SDimitry Andric SDValue N = IsBase ? AM.Base : AM.Index; 4820b57cec5SDimitry Andric unsigned Opcode = N.getOpcode(); 483cb14a3feSDimitry Andric // Look through no-op truncations. 484cb14a3feSDimitry Andric if (Opcode == ISD::TRUNCATE && N.getOperand(0).getValueSizeInBits() <= 64) { 4850b57cec5SDimitry Andric N = N.getOperand(0); 4860b57cec5SDimitry Andric Opcode = N.getOpcode(); 4870b57cec5SDimitry Andric } 4880b57cec5SDimitry Andric if (Opcode == ISD::ADD || CurDAG->isBaseWithConstantOffset(N)) { 4890b57cec5SDimitry Andric SDValue Op0 = N.getOperand(0); 4900b57cec5SDimitry Andric SDValue Op1 = N.getOperand(1); 4910b57cec5SDimitry Andric 4920b57cec5SDimitry Andric unsigned Op0Code = Op0->getOpcode(); 4930b57cec5SDimitry Andric unsigned Op1Code = Op1->getOpcode(); 4940b57cec5SDimitry Andric 4950b57cec5SDimitry Andric if (Op0Code == SystemZISD::ADJDYNALLOC) 4960b57cec5SDimitry Andric return expandAdjDynAlloc(AM, IsBase, Op1); 4970b57cec5SDimitry Andric if (Op1Code == SystemZISD::ADJDYNALLOC) 4980b57cec5SDimitry Andric return expandAdjDynAlloc(AM, IsBase, Op0); 4990b57cec5SDimitry Andric 5000b57cec5SDimitry Andric if (Op0Code == ISD::Constant) 5010b57cec5SDimitry Andric return expandDisp(AM, IsBase, Op1, 5020b57cec5SDimitry Andric cast<ConstantSDNode>(Op0)->getSExtValue()); 5030b57cec5SDimitry Andric if (Op1Code == ISD::Constant) 5040b57cec5SDimitry Andric return expandDisp(AM, IsBase, Op0, 5050b57cec5SDimitry Andric cast<ConstantSDNode>(Op1)->getSExtValue()); 5060b57cec5SDimitry Andric 5070b57cec5SDimitry Andric if (IsBase && expandIndex(AM, Op0, Op1)) 5080b57cec5SDimitry Andric return true; 5090b57cec5SDimitry Andric } 5100b57cec5SDimitry Andric if (Opcode == SystemZISD::PCREL_OFFSET) { 5110b57cec5SDimitry Andric SDValue Full = N.getOperand(0); 5120b57cec5SDimitry Andric SDValue Base = N.getOperand(1); 5130b57cec5SDimitry Andric SDValue Anchor = Base.getOperand(0); 5140b57cec5SDimitry Andric uint64_t Offset = (cast<GlobalAddressSDNode>(Full)->getOffset() - 5150b57cec5SDimitry Andric cast<GlobalAddressSDNode>(Anchor)->getOffset()); 5160b57cec5SDimitry Andric return expandDisp(AM, IsBase, Base, Offset); 5170b57cec5SDimitry Andric } 5180b57cec5SDimitry Andric return false; 5190b57cec5SDimitry Andric } 5200b57cec5SDimitry Andric 5210b57cec5SDimitry Andric // Return true if an instruction with displacement range DR should be 5220b57cec5SDimitry Andric // used for displacement value Val. selectDisp(DR, Val) must already hold. 5230b57cec5SDimitry Andric static bool isValidDisp(SystemZAddressingMode::DispRange DR, int64_t Val) { 5240b57cec5SDimitry Andric assert(selectDisp(DR, Val) && "Invalid displacement"); 5250b57cec5SDimitry Andric switch (DR) { 5260b57cec5SDimitry Andric case SystemZAddressingMode::Disp12Only: 5270b57cec5SDimitry Andric case SystemZAddressingMode::Disp20Only: 5280b57cec5SDimitry Andric case SystemZAddressingMode::Disp20Only128: 5290b57cec5SDimitry Andric return true; 5300b57cec5SDimitry Andric 5310b57cec5SDimitry Andric case SystemZAddressingMode::Disp12Pair: 5320b57cec5SDimitry Andric // Use the other instruction if the displacement is too large. 5330b57cec5SDimitry Andric return isUInt<12>(Val); 5340b57cec5SDimitry Andric 5350b57cec5SDimitry Andric case SystemZAddressingMode::Disp20Pair: 5360b57cec5SDimitry Andric // Use the other instruction if the displacement is small enough. 5370b57cec5SDimitry Andric return !isUInt<12>(Val); 5380b57cec5SDimitry Andric } 5390b57cec5SDimitry Andric llvm_unreachable("Unhandled displacement range"); 5400b57cec5SDimitry Andric } 5410b57cec5SDimitry Andric 5420b57cec5SDimitry Andric // Return true if Base + Disp + Index should be performed by LA(Y). 5430b57cec5SDimitry Andric static bool shouldUseLA(SDNode *Base, int64_t Disp, SDNode *Index) { 5440b57cec5SDimitry Andric // Don't use LA(Y) for constants. 5450b57cec5SDimitry Andric if (!Base) 5460b57cec5SDimitry Andric return false; 5470b57cec5SDimitry Andric 5480b57cec5SDimitry Andric // Always use LA(Y) for frame addresses, since we know that the destination 5490b57cec5SDimitry Andric // register is almost always (perhaps always) going to be different from 5500b57cec5SDimitry Andric // the frame register. 5510b57cec5SDimitry Andric if (Base->getOpcode() == ISD::FrameIndex) 5520b57cec5SDimitry Andric return true; 5530b57cec5SDimitry Andric 5540b57cec5SDimitry Andric if (Disp) { 5550b57cec5SDimitry Andric // Always use LA(Y) if there is a base, displacement and index. 5560b57cec5SDimitry Andric if (Index) 5570b57cec5SDimitry Andric return true; 5580b57cec5SDimitry Andric 5590b57cec5SDimitry Andric // Always use LA if the displacement is small enough. It should always 5600b57cec5SDimitry Andric // be no worse than AGHI (and better if it avoids a move). 5610b57cec5SDimitry Andric if (isUInt<12>(Disp)) 5620b57cec5SDimitry Andric return true; 5630b57cec5SDimitry Andric 5640b57cec5SDimitry Andric // For similar reasons, always use LAY if the constant is too big for AGHI. 5650b57cec5SDimitry Andric // LAY should be no worse than AGFI. 5660b57cec5SDimitry Andric if (!isInt<16>(Disp)) 5670b57cec5SDimitry Andric return true; 5680b57cec5SDimitry Andric } else { 5690b57cec5SDimitry Andric // Don't use LA for plain registers. 5700b57cec5SDimitry Andric if (!Index) 5710b57cec5SDimitry Andric return false; 5720b57cec5SDimitry Andric 5730b57cec5SDimitry Andric // Don't use LA for plain addition if the index operand is only used 5740b57cec5SDimitry Andric // once. It should be a natural two-operand addition in that case. 5750b57cec5SDimitry Andric if (Index->hasOneUse()) 5760b57cec5SDimitry Andric return false; 5770b57cec5SDimitry Andric 5780b57cec5SDimitry Andric // Prefer addition if the second operation is sign-extended, in the 5790b57cec5SDimitry Andric // hope of using AGF. 5800b57cec5SDimitry Andric unsigned IndexOpcode = Index->getOpcode(); 5810b57cec5SDimitry Andric if (IndexOpcode == ISD::SIGN_EXTEND || 5820b57cec5SDimitry Andric IndexOpcode == ISD::SIGN_EXTEND_INREG) 5830b57cec5SDimitry Andric return false; 5840b57cec5SDimitry Andric } 5850b57cec5SDimitry Andric 5860b57cec5SDimitry Andric // Don't use LA for two-operand addition if either operand is only 5870b57cec5SDimitry Andric // used once. The addition instructions are better in that case. 5880b57cec5SDimitry Andric if (Base->hasOneUse()) 5890b57cec5SDimitry Andric return false; 5900b57cec5SDimitry Andric 5910b57cec5SDimitry Andric return true; 5920b57cec5SDimitry Andric } 5930b57cec5SDimitry Andric 5940b57cec5SDimitry Andric // Return true if Addr is suitable for AM, updating AM if so. 5950b57cec5SDimitry Andric bool SystemZDAGToDAGISel::selectAddress(SDValue Addr, 5960b57cec5SDimitry Andric SystemZAddressingMode &AM) const { 5970b57cec5SDimitry Andric // Start out assuming that the address will need to be loaded separately, 5980b57cec5SDimitry Andric // then try to extend it as much as we can. 5990b57cec5SDimitry Andric AM.Base = Addr; 6000b57cec5SDimitry Andric 6010b57cec5SDimitry Andric // First try treating the address as a constant. 6020b57cec5SDimitry Andric if (Addr.getOpcode() == ISD::Constant && 6030b57cec5SDimitry Andric expandDisp(AM, true, SDValue(), 6040b57cec5SDimitry Andric cast<ConstantSDNode>(Addr)->getSExtValue())) 6050b57cec5SDimitry Andric ; 6060b57cec5SDimitry Andric // Also see if it's a bare ADJDYNALLOC. 6070b57cec5SDimitry Andric else if (Addr.getOpcode() == SystemZISD::ADJDYNALLOC && 6080b57cec5SDimitry Andric expandAdjDynAlloc(AM, true, SDValue())) 6090b57cec5SDimitry Andric ; 6100b57cec5SDimitry Andric else 6110b57cec5SDimitry Andric // Otherwise try expanding each component. 6120b57cec5SDimitry Andric while (expandAddress(AM, true) || 6130b57cec5SDimitry Andric (AM.Index.getNode() && expandAddress(AM, false))) 6140b57cec5SDimitry Andric continue; 6150b57cec5SDimitry Andric 6160b57cec5SDimitry Andric // Reject cases where it isn't profitable to use LA(Y). 6170b57cec5SDimitry Andric if (AM.Form == SystemZAddressingMode::FormBDXLA && 6180b57cec5SDimitry Andric !shouldUseLA(AM.Base.getNode(), AM.Disp, AM.Index.getNode())) 6190b57cec5SDimitry Andric return false; 6200b57cec5SDimitry Andric 6210b57cec5SDimitry Andric // Reject cases where the other instruction in a pair should be used. 6220b57cec5SDimitry Andric if (!isValidDisp(AM.DR, AM.Disp)) 6230b57cec5SDimitry Andric return false; 6240b57cec5SDimitry Andric 6250b57cec5SDimitry Andric // Make sure that ADJDYNALLOC is included where necessary. 6260b57cec5SDimitry Andric if (AM.isDynAlloc() && !AM.IncludesDynAlloc) 6270b57cec5SDimitry Andric return false; 6280b57cec5SDimitry Andric 6290b57cec5SDimitry Andric LLVM_DEBUG(AM.dump(CurDAG)); 6300b57cec5SDimitry Andric return true; 6310b57cec5SDimitry Andric } 6320b57cec5SDimitry Andric 6330b57cec5SDimitry Andric // Insert a node into the DAG at least before Pos. This will reposition 6340b57cec5SDimitry Andric // the node as needed, and will assign it a node ID that is <= Pos's ID. 6350b57cec5SDimitry Andric // Note that this does *not* preserve the uniqueness of node IDs! 6360b57cec5SDimitry Andric // The selection DAG must no longer depend on their uniqueness when this 6370b57cec5SDimitry Andric // function is used. 6380b57cec5SDimitry Andric static void insertDAGNode(SelectionDAG *DAG, SDNode *Pos, SDValue N) { 6390b57cec5SDimitry Andric if (N->getNodeId() == -1 || 6400b57cec5SDimitry Andric (SelectionDAGISel::getUninvalidatedNodeId(N.getNode()) > 6410b57cec5SDimitry Andric SelectionDAGISel::getUninvalidatedNodeId(Pos))) { 6420b57cec5SDimitry Andric DAG->RepositionNode(Pos->getIterator(), N.getNode()); 6430b57cec5SDimitry Andric // Mark Node as invalid for pruning as after this it may be a successor to a 6440b57cec5SDimitry Andric // selected node but otherwise be in the same position of Pos. 6450b57cec5SDimitry Andric // Conservatively mark it with the same -abs(Id) to assure node id 6460b57cec5SDimitry Andric // invariant is preserved. 6470b57cec5SDimitry Andric N->setNodeId(Pos->getNodeId()); 6480b57cec5SDimitry Andric SelectionDAGISel::InvalidateNodeId(N.getNode()); 6490b57cec5SDimitry Andric } 6500b57cec5SDimitry Andric } 6510b57cec5SDimitry Andric 6520b57cec5SDimitry Andric void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM, 6530b57cec5SDimitry Andric EVT VT, SDValue &Base, 6540b57cec5SDimitry Andric SDValue &Disp) const { 6550b57cec5SDimitry Andric Base = AM.Base; 6560b57cec5SDimitry Andric if (!Base.getNode()) 6570b57cec5SDimitry Andric // Register 0 means "no base". This is mostly useful for shifts. 6580b57cec5SDimitry Andric Base = CurDAG->getRegister(0, VT); 6590b57cec5SDimitry Andric else if (Base.getOpcode() == ISD::FrameIndex) { 6600b57cec5SDimitry Andric // Lower a FrameIndex to a TargetFrameIndex. 6610b57cec5SDimitry Andric int64_t FrameIndex = cast<FrameIndexSDNode>(Base)->getIndex(); 6620b57cec5SDimitry Andric Base = CurDAG->getTargetFrameIndex(FrameIndex, VT); 6630b57cec5SDimitry Andric } else if (Base.getValueType() != VT) { 6640b57cec5SDimitry Andric // Truncate values from i64 to i32, for shifts. 6650b57cec5SDimitry Andric assert(VT == MVT::i32 && Base.getValueType() == MVT::i64 && 6660b57cec5SDimitry Andric "Unexpected truncation"); 6670b57cec5SDimitry Andric SDLoc DL(Base); 6680b57cec5SDimitry Andric SDValue Trunc = CurDAG->getNode(ISD::TRUNCATE, DL, VT, Base); 6690b57cec5SDimitry Andric insertDAGNode(CurDAG, Base.getNode(), Trunc); 6700b57cec5SDimitry Andric Base = Trunc; 6710b57cec5SDimitry Andric } 6720b57cec5SDimitry Andric 6730b57cec5SDimitry Andric // Lower the displacement to a TargetConstant. 6740b57cec5SDimitry Andric Disp = CurDAG->getTargetConstant(AM.Disp, SDLoc(Base), VT); 6750b57cec5SDimitry Andric } 6760b57cec5SDimitry Andric 6770b57cec5SDimitry Andric void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM, 6780b57cec5SDimitry Andric EVT VT, SDValue &Base, 6790b57cec5SDimitry Andric SDValue &Disp, 6800b57cec5SDimitry Andric SDValue &Index) const { 6810b57cec5SDimitry Andric getAddressOperands(AM, VT, Base, Disp); 6820b57cec5SDimitry Andric 6830b57cec5SDimitry Andric Index = AM.Index; 6840b57cec5SDimitry Andric if (!Index.getNode()) 6850b57cec5SDimitry Andric // Register 0 means "no index". 6860b57cec5SDimitry Andric Index = CurDAG->getRegister(0, VT); 6870b57cec5SDimitry Andric } 6880b57cec5SDimitry Andric 6890b57cec5SDimitry Andric bool SystemZDAGToDAGISel::selectBDAddr(SystemZAddressingMode::DispRange DR, 6900b57cec5SDimitry Andric SDValue Addr, SDValue &Base, 6910b57cec5SDimitry Andric SDValue &Disp) const { 6920b57cec5SDimitry Andric SystemZAddressingMode AM(SystemZAddressingMode::FormBD, DR); 6930b57cec5SDimitry Andric if (!selectAddress(Addr, AM)) 6940b57cec5SDimitry Andric return false; 6950b57cec5SDimitry Andric 6960b57cec5SDimitry Andric getAddressOperands(AM, Addr.getValueType(), Base, Disp); 6970b57cec5SDimitry Andric return true; 6980b57cec5SDimitry Andric } 6990b57cec5SDimitry Andric 7000b57cec5SDimitry Andric bool SystemZDAGToDAGISel::selectMVIAddr(SystemZAddressingMode::DispRange DR, 7010b57cec5SDimitry Andric SDValue Addr, SDValue &Base, 7020b57cec5SDimitry Andric SDValue &Disp) const { 7030b57cec5SDimitry Andric SystemZAddressingMode AM(SystemZAddressingMode::FormBDXNormal, DR); 7040b57cec5SDimitry Andric if (!selectAddress(Addr, AM) || AM.Index.getNode()) 7050b57cec5SDimitry Andric return false; 7060b57cec5SDimitry Andric 7070b57cec5SDimitry Andric getAddressOperands(AM, Addr.getValueType(), Base, Disp); 7080b57cec5SDimitry Andric return true; 7090b57cec5SDimitry Andric } 7100b57cec5SDimitry Andric 7110b57cec5SDimitry Andric bool SystemZDAGToDAGISel::selectBDXAddr(SystemZAddressingMode::AddrForm Form, 7120b57cec5SDimitry Andric SystemZAddressingMode::DispRange DR, 7130b57cec5SDimitry Andric SDValue Addr, SDValue &Base, 7140b57cec5SDimitry Andric SDValue &Disp, SDValue &Index) const { 7150b57cec5SDimitry Andric SystemZAddressingMode AM(Form, DR); 7160b57cec5SDimitry Andric if (!selectAddress(Addr, AM)) 7170b57cec5SDimitry Andric return false; 7180b57cec5SDimitry Andric 7190b57cec5SDimitry Andric getAddressOperands(AM, Addr.getValueType(), Base, Disp, Index); 7200b57cec5SDimitry Andric return true; 7210b57cec5SDimitry Andric } 7220b57cec5SDimitry Andric 7230b57cec5SDimitry Andric bool SystemZDAGToDAGISel::selectBDVAddr12Only(SDValue Addr, SDValue Elem, 7240b57cec5SDimitry Andric SDValue &Base, 7250b57cec5SDimitry Andric SDValue &Disp, 7260b57cec5SDimitry Andric SDValue &Index) const { 7270b57cec5SDimitry Andric SDValue Regs[2]; 7280b57cec5SDimitry Andric if (selectBDXAddr12Only(Addr, Regs[0], Disp, Regs[1]) && 7290b57cec5SDimitry Andric Regs[0].getNode() && Regs[1].getNode()) { 7300b57cec5SDimitry Andric for (unsigned int I = 0; I < 2; ++I) { 7310b57cec5SDimitry Andric Base = Regs[I]; 7320b57cec5SDimitry Andric Index = Regs[1 - I]; 7330b57cec5SDimitry Andric // We can't tell here whether the index vector has the right type 7340b57cec5SDimitry Andric // for the access; the caller needs to do that instead. 7350b57cec5SDimitry Andric if (Index.getOpcode() == ISD::ZERO_EXTEND) 7360b57cec5SDimitry Andric Index = Index.getOperand(0); 7370b57cec5SDimitry Andric if (Index.getOpcode() == ISD::EXTRACT_VECTOR_ELT && 7380b57cec5SDimitry Andric Index.getOperand(1) == Elem) { 7390b57cec5SDimitry Andric Index = Index.getOperand(0); 7400b57cec5SDimitry Andric return true; 7410b57cec5SDimitry Andric } 7420b57cec5SDimitry Andric } 7430b57cec5SDimitry Andric } 7440b57cec5SDimitry Andric return false; 7450b57cec5SDimitry Andric } 7460b57cec5SDimitry Andric 7470b57cec5SDimitry Andric bool SystemZDAGToDAGISel::detectOrAndInsertion(SDValue &Op, 7480b57cec5SDimitry Andric uint64_t InsertMask) const { 7490b57cec5SDimitry Andric // We're only interested in cases where the insertion is into some operand 7500b57cec5SDimitry Andric // of Op, rather than into Op itself. The only useful case is an AND. 7510b57cec5SDimitry Andric if (Op.getOpcode() != ISD::AND) 7520b57cec5SDimitry Andric return false; 7530b57cec5SDimitry Andric 7540b57cec5SDimitry Andric // We need a constant mask. 7550b57cec5SDimitry Andric auto *MaskNode = dyn_cast<ConstantSDNode>(Op.getOperand(1).getNode()); 7560b57cec5SDimitry Andric if (!MaskNode) 7570b57cec5SDimitry Andric return false; 7580b57cec5SDimitry Andric 7590b57cec5SDimitry Andric // It's not an insertion of Op.getOperand(0) if the two masks overlap. 7600b57cec5SDimitry Andric uint64_t AndMask = MaskNode->getZExtValue(); 7610b57cec5SDimitry Andric if (InsertMask & AndMask) 7620b57cec5SDimitry Andric return false; 7630b57cec5SDimitry Andric 7640b57cec5SDimitry Andric // It's only an insertion if all bits are covered or are known to be zero. 7650b57cec5SDimitry Andric // The inner check covers all cases but is more expensive. 7660b57cec5SDimitry Andric uint64_t Used = allOnes(Op.getValueSizeInBits()); 7670b57cec5SDimitry Andric if (Used != (AndMask | InsertMask)) { 7680b57cec5SDimitry Andric KnownBits Known = CurDAG->computeKnownBits(Op.getOperand(0)); 7690b57cec5SDimitry Andric if (Used != (AndMask | InsertMask | Known.Zero.getZExtValue())) 7700b57cec5SDimitry Andric return false; 7710b57cec5SDimitry Andric } 7720b57cec5SDimitry Andric 7730b57cec5SDimitry Andric Op = Op.getOperand(0); 7740b57cec5SDimitry Andric return true; 7750b57cec5SDimitry Andric } 7760b57cec5SDimitry Andric 7770b57cec5SDimitry Andric bool SystemZDAGToDAGISel::refineRxSBGMask(RxSBGOperands &RxSBG, 7780b57cec5SDimitry Andric uint64_t Mask) const { 7790b57cec5SDimitry Andric const SystemZInstrInfo *TII = getInstrInfo(); 7800b57cec5SDimitry Andric if (RxSBG.Rotate != 0) 7810b57cec5SDimitry Andric Mask = (Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate)); 7820b57cec5SDimitry Andric Mask &= RxSBG.Mask; 7830b57cec5SDimitry Andric if (TII->isRxSBGMask(Mask, RxSBG.BitSize, RxSBG.Start, RxSBG.End)) { 7840b57cec5SDimitry Andric RxSBG.Mask = Mask; 7850b57cec5SDimitry Andric return true; 7860b57cec5SDimitry Andric } 7870b57cec5SDimitry Andric return false; 7880b57cec5SDimitry Andric } 7890b57cec5SDimitry Andric 7900b57cec5SDimitry Andric // Return true if any bits of (RxSBG.Input & Mask) are significant. 7910b57cec5SDimitry Andric static bool maskMatters(RxSBGOperands &RxSBG, uint64_t Mask) { 7920b57cec5SDimitry Andric // Rotate the mask in the same way as RxSBG.Input is rotated. 7930b57cec5SDimitry Andric if (RxSBG.Rotate != 0) 7940b57cec5SDimitry Andric Mask = ((Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate))); 7950b57cec5SDimitry Andric return (Mask & RxSBG.Mask) != 0; 7960b57cec5SDimitry Andric } 7970b57cec5SDimitry Andric 7980b57cec5SDimitry Andric bool SystemZDAGToDAGISel::expandRxSBG(RxSBGOperands &RxSBG) const { 7990b57cec5SDimitry Andric SDValue N = RxSBG.Input; 8000b57cec5SDimitry Andric unsigned Opcode = N.getOpcode(); 8010b57cec5SDimitry Andric switch (Opcode) { 8020b57cec5SDimitry Andric case ISD::TRUNCATE: { 8030b57cec5SDimitry Andric if (RxSBG.Opcode == SystemZ::RNSBG) 8040b57cec5SDimitry Andric return false; 8055f757f3fSDimitry Andric if (N.getOperand(0).getValueSizeInBits() > 64) 8065f757f3fSDimitry Andric return false; 8070b57cec5SDimitry Andric uint64_t BitSize = N.getValueSizeInBits(); 8080b57cec5SDimitry Andric uint64_t Mask = allOnes(BitSize); 8090b57cec5SDimitry Andric if (!refineRxSBGMask(RxSBG, Mask)) 8100b57cec5SDimitry Andric return false; 8110b57cec5SDimitry Andric RxSBG.Input = N.getOperand(0); 8120b57cec5SDimitry Andric return true; 8130b57cec5SDimitry Andric } 8140b57cec5SDimitry Andric case ISD::AND: { 8150b57cec5SDimitry Andric if (RxSBG.Opcode == SystemZ::RNSBG) 8160b57cec5SDimitry Andric return false; 8170b57cec5SDimitry Andric 8180b57cec5SDimitry Andric auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode()); 8190b57cec5SDimitry Andric if (!MaskNode) 8200b57cec5SDimitry Andric return false; 8210b57cec5SDimitry Andric 8220b57cec5SDimitry Andric SDValue Input = N.getOperand(0); 8230b57cec5SDimitry Andric uint64_t Mask = MaskNode->getZExtValue(); 8240b57cec5SDimitry Andric if (!refineRxSBGMask(RxSBG, Mask)) { 8250b57cec5SDimitry Andric // If some bits of Input are already known zeros, those bits will have 8260b57cec5SDimitry Andric // been removed from the mask. See if adding them back in makes the 8270b57cec5SDimitry Andric // mask suitable. 8280b57cec5SDimitry Andric KnownBits Known = CurDAG->computeKnownBits(Input); 8290b57cec5SDimitry Andric Mask |= Known.Zero.getZExtValue(); 8300b57cec5SDimitry Andric if (!refineRxSBGMask(RxSBG, Mask)) 8310b57cec5SDimitry Andric return false; 8320b57cec5SDimitry Andric } 8330b57cec5SDimitry Andric RxSBG.Input = Input; 8340b57cec5SDimitry Andric return true; 8350b57cec5SDimitry Andric } 8360b57cec5SDimitry Andric 8370b57cec5SDimitry Andric case ISD::OR: { 8380b57cec5SDimitry Andric if (RxSBG.Opcode != SystemZ::RNSBG) 8390b57cec5SDimitry Andric return false; 8400b57cec5SDimitry Andric 8410b57cec5SDimitry Andric auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode()); 8420b57cec5SDimitry Andric if (!MaskNode) 8430b57cec5SDimitry Andric return false; 8440b57cec5SDimitry Andric 8450b57cec5SDimitry Andric SDValue Input = N.getOperand(0); 8460b57cec5SDimitry Andric uint64_t Mask = ~MaskNode->getZExtValue(); 8470b57cec5SDimitry Andric if (!refineRxSBGMask(RxSBG, Mask)) { 8480b57cec5SDimitry Andric // If some bits of Input are already known ones, those bits will have 8490b57cec5SDimitry Andric // been removed from the mask. See if adding them back in makes the 8500b57cec5SDimitry Andric // mask suitable. 8510b57cec5SDimitry Andric KnownBits Known = CurDAG->computeKnownBits(Input); 8520b57cec5SDimitry Andric Mask &= ~Known.One.getZExtValue(); 8530b57cec5SDimitry Andric if (!refineRxSBGMask(RxSBG, Mask)) 8540b57cec5SDimitry Andric return false; 8550b57cec5SDimitry Andric } 8560b57cec5SDimitry Andric RxSBG.Input = Input; 8570b57cec5SDimitry Andric return true; 8580b57cec5SDimitry Andric } 8590b57cec5SDimitry Andric 8600b57cec5SDimitry Andric case ISD::ROTL: { 8610b57cec5SDimitry Andric // Any 64-bit rotate left can be merged into the RxSBG. 8620b57cec5SDimitry Andric if (RxSBG.BitSize != 64 || N.getValueType() != MVT::i64) 8630b57cec5SDimitry Andric return false; 8640b57cec5SDimitry Andric auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode()); 8650b57cec5SDimitry Andric if (!CountNode) 8660b57cec5SDimitry Andric return false; 8670b57cec5SDimitry Andric 8680b57cec5SDimitry Andric RxSBG.Rotate = (RxSBG.Rotate + CountNode->getZExtValue()) & 63; 8690b57cec5SDimitry Andric RxSBG.Input = N.getOperand(0); 8700b57cec5SDimitry Andric return true; 8710b57cec5SDimitry Andric } 8720b57cec5SDimitry Andric 8730b57cec5SDimitry Andric case ISD::ANY_EXTEND: 8740b57cec5SDimitry Andric // Bits above the extended operand are don't-care. 8750b57cec5SDimitry Andric RxSBG.Input = N.getOperand(0); 8760b57cec5SDimitry Andric return true; 8770b57cec5SDimitry Andric 8780b57cec5SDimitry Andric case ISD::ZERO_EXTEND: 8790b57cec5SDimitry Andric if (RxSBG.Opcode != SystemZ::RNSBG) { 8800b57cec5SDimitry Andric // Restrict the mask to the extended operand. 8810b57cec5SDimitry Andric unsigned InnerBitSize = N.getOperand(0).getValueSizeInBits(); 8820b57cec5SDimitry Andric if (!refineRxSBGMask(RxSBG, allOnes(InnerBitSize))) 8830b57cec5SDimitry Andric return false; 8840b57cec5SDimitry Andric 8850b57cec5SDimitry Andric RxSBG.Input = N.getOperand(0); 8860b57cec5SDimitry Andric return true; 8870b57cec5SDimitry Andric } 888bdd1243dSDimitry Andric [[fallthrough]]; 8890b57cec5SDimitry Andric 8900b57cec5SDimitry Andric case ISD::SIGN_EXTEND: { 8910b57cec5SDimitry Andric // Check that the extension bits are don't-care (i.e. are masked out 8920b57cec5SDimitry Andric // by the final mask). 8930b57cec5SDimitry Andric unsigned BitSize = N.getValueSizeInBits(); 8940b57cec5SDimitry Andric unsigned InnerBitSize = N.getOperand(0).getValueSizeInBits(); 8950b57cec5SDimitry Andric if (maskMatters(RxSBG, allOnes(BitSize) - allOnes(InnerBitSize))) { 8960b57cec5SDimitry Andric // In the case where only the sign bit is active, increase Rotate with 8970b57cec5SDimitry Andric // the extension width. 8980b57cec5SDimitry Andric if (RxSBG.Mask == 1 && RxSBG.Rotate == 1) 8990b57cec5SDimitry Andric RxSBG.Rotate += (BitSize - InnerBitSize); 9000b57cec5SDimitry Andric else 9010b57cec5SDimitry Andric return false; 9020b57cec5SDimitry Andric } 9030b57cec5SDimitry Andric 9040b57cec5SDimitry Andric RxSBG.Input = N.getOperand(0); 9050b57cec5SDimitry Andric return true; 9060b57cec5SDimitry Andric } 9070b57cec5SDimitry Andric 9080b57cec5SDimitry Andric case ISD::SHL: { 9090b57cec5SDimitry Andric auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode()); 9100b57cec5SDimitry Andric if (!CountNode) 9110b57cec5SDimitry Andric return false; 9120b57cec5SDimitry Andric 9130b57cec5SDimitry Andric uint64_t Count = CountNode->getZExtValue(); 9140b57cec5SDimitry Andric unsigned BitSize = N.getValueSizeInBits(); 9150b57cec5SDimitry Andric if (Count < 1 || Count >= BitSize) 9160b57cec5SDimitry Andric return false; 9170b57cec5SDimitry Andric 9180b57cec5SDimitry Andric if (RxSBG.Opcode == SystemZ::RNSBG) { 9190b57cec5SDimitry Andric // Treat (shl X, count) as (rotl X, size-count) as long as the bottom 9200b57cec5SDimitry Andric // count bits from RxSBG.Input are ignored. 9210b57cec5SDimitry Andric if (maskMatters(RxSBG, allOnes(Count))) 9220b57cec5SDimitry Andric return false; 9230b57cec5SDimitry Andric } else { 9240b57cec5SDimitry Andric // Treat (shl X, count) as (and (rotl X, count), ~0<<count). 9250b57cec5SDimitry Andric if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count) << Count)) 9260b57cec5SDimitry Andric return false; 9270b57cec5SDimitry Andric } 9280b57cec5SDimitry Andric 9290b57cec5SDimitry Andric RxSBG.Rotate = (RxSBG.Rotate + Count) & 63; 9300b57cec5SDimitry Andric RxSBG.Input = N.getOperand(0); 9310b57cec5SDimitry Andric return true; 9320b57cec5SDimitry Andric } 9330b57cec5SDimitry Andric 9340b57cec5SDimitry Andric case ISD::SRL: 9350b57cec5SDimitry Andric case ISD::SRA: { 9360b57cec5SDimitry Andric auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode()); 9370b57cec5SDimitry Andric if (!CountNode) 9380b57cec5SDimitry Andric return false; 9390b57cec5SDimitry Andric 9400b57cec5SDimitry Andric uint64_t Count = CountNode->getZExtValue(); 9410b57cec5SDimitry Andric unsigned BitSize = N.getValueSizeInBits(); 9420b57cec5SDimitry Andric if (Count < 1 || Count >= BitSize) 9430b57cec5SDimitry Andric return false; 9440b57cec5SDimitry Andric 9450b57cec5SDimitry Andric if (RxSBG.Opcode == SystemZ::RNSBG || Opcode == ISD::SRA) { 9460b57cec5SDimitry Andric // Treat (srl|sra X, count) as (rotl X, size-count) as long as the top 9470b57cec5SDimitry Andric // count bits from RxSBG.Input are ignored. 9480b57cec5SDimitry Andric if (maskMatters(RxSBG, allOnes(Count) << (BitSize - Count))) 9490b57cec5SDimitry Andric return false; 9500b57cec5SDimitry Andric } else { 9510b57cec5SDimitry Andric // Treat (srl X, count), mask) as (and (rotl X, size-count), ~0>>count), 9520b57cec5SDimitry Andric // which is similar to SLL above. 9530b57cec5SDimitry Andric if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count))) 9540b57cec5SDimitry Andric return false; 9550b57cec5SDimitry Andric } 9560b57cec5SDimitry Andric 9570b57cec5SDimitry Andric RxSBG.Rotate = (RxSBG.Rotate - Count) & 63; 9580b57cec5SDimitry Andric RxSBG.Input = N.getOperand(0); 9590b57cec5SDimitry Andric return true; 9600b57cec5SDimitry Andric } 9610b57cec5SDimitry Andric default: 9620b57cec5SDimitry Andric return false; 9630b57cec5SDimitry Andric } 9640b57cec5SDimitry Andric } 9650b57cec5SDimitry Andric 9660b57cec5SDimitry Andric SDValue SystemZDAGToDAGISel::getUNDEF(const SDLoc &DL, EVT VT) const { 9670b57cec5SDimitry Andric SDNode *N = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, VT); 9680b57cec5SDimitry Andric return SDValue(N, 0); 9690b57cec5SDimitry Andric } 9700b57cec5SDimitry Andric 9710b57cec5SDimitry Andric SDValue SystemZDAGToDAGISel::convertTo(const SDLoc &DL, EVT VT, 9720b57cec5SDimitry Andric SDValue N) const { 9730b57cec5SDimitry Andric if (N.getValueType() == MVT::i32 && VT == MVT::i64) 9740b57cec5SDimitry Andric return CurDAG->getTargetInsertSubreg(SystemZ::subreg_l32, 9750b57cec5SDimitry Andric DL, VT, getUNDEF(DL, MVT::i64), N); 9760b57cec5SDimitry Andric if (N.getValueType() == MVT::i64 && VT == MVT::i32) 9770b57cec5SDimitry Andric return CurDAG->getTargetExtractSubreg(SystemZ::subreg_l32, DL, VT, N); 9780b57cec5SDimitry Andric assert(N.getValueType() == VT && "Unexpected value types"); 9790b57cec5SDimitry Andric return N; 9800b57cec5SDimitry Andric } 9810b57cec5SDimitry Andric 9820b57cec5SDimitry Andric bool SystemZDAGToDAGISel::tryRISBGZero(SDNode *N) { 9830b57cec5SDimitry Andric SDLoc DL(N); 9840b57cec5SDimitry Andric EVT VT = N->getValueType(0); 9850b57cec5SDimitry Andric if (!VT.isInteger() || VT.getSizeInBits() > 64) 9860b57cec5SDimitry Andric return false; 9870b57cec5SDimitry Andric RxSBGOperands RISBG(SystemZ::RISBG, SDValue(N, 0)); 9880b57cec5SDimitry Andric unsigned Count = 0; 9890b57cec5SDimitry Andric while (expandRxSBG(RISBG)) 9900b57cec5SDimitry Andric // The widening or narrowing is expected to be free. 9910b57cec5SDimitry Andric // Counting widening or narrowing as a saved operation will result in 9920b57cec5SDimitry Andric // preferring an R*SBG over a simple shift/logical instruction. 9930b57cec5SDimitry Andric if (RISBG.Input.getOpcode() != ISD::ANY_EXTEND && 9940b57cec5SDimitry Andric RISBG.Input.getOpcode() != ISD::TRUNCATE) 9950b57cec5SDimitry Andric Count += 1; 99681ad6265SDimitry Andric if (Count == 0 || isa<ConstantSDNode>(RISBG.Input)) 9970b57cec5SDimitry Andric return false; 9980b57cec5SDimitry Andric 9990b57cec5SDimitry Andric // Prefer to use normal shift instructions over RISBG, since they can handle 10000b57cec5SDimitry Andric // all cases and are sometimes shorter. 10010b57cec5SDimitry Andric if (Count == 1 && N->getOpcode() != ISD::AND) 10020b57cec5SDimitry Andric return false; 10030b57cec5SDimitry Andric 10040b57cec5SDimitry Andric // Prefer register extensions like LLC over RISBG. Also prefer to start 10050b57cec5SDimitry Andric // out with normal ANDs if one instruction would be enough. We can convert 10060b57cec5SDimitry Andric // these ANDs into an RISBG later if a three-address instruction is useful. 10070b57cec5SDimitry Andric if (RISBG.Rotate == 0) { 10080b57cec5SDimitry Andric bool PreferAnd = false; 10090b57cec5SDimitry Andric // Prefer AND for any 32-bit and-immediate operation. 10100b57cec5SDimitry Andric if (VT == MVT::i32) 10110b57cec5SDimitry Andric PreferAnd = true; 10120b57cec5SDimitry Andric // As well as for any 64-bit operation that can be implemented via LLC(R), 10130b57cec5SDimitry Andric // LLH(R), LLGT(R), or one of the and-immediate instructions. 10140b57cec5SDimitry Andric else if (RISBG.Mask == 0xff || 10150b57cec5SDimitry Andric RISBG.Mask == 0xffff || 10160b57cec5SDimitry Andric RISBG.Mask == 0x7fffffff || 10170b57cec5SDimitry Andric SystemZ::isImmLF(~RISBG.Mask) || 10180b57cec5SDimitry Andric SystemZ::isImmHF(~RISBG.Mask)) 10190b57cec5SDimitry Andric PreferAnd = true; 10200b57cec5SDimitry Andric // And likewise for the LLZRGF instruction, which doesn't have a register 10210b57cec5SDimitry Andric // to register version. 10220b57cec5SDimitry Andric else if (auto *Load = dyn_cast<LoadSDNode>(RISBG.Input)) { 10230b57cec5SDimitry Andric if (Load->getMemoryVT() == MVT::i32 && 10240b57cec5SDimitry Andric (Load->getExtensionType() == ISD::EXTLOAD || 10250b57cec5SDimitry Andric Load->getExtensionType() == ISD::ZEXTLOAD) && 10260b57cec5SDimitry Andric RISBG.Mask == 0xffffff00 && 10270b57cec5SDimitry Andric Subtarget->hasLoadAndZeroRightmostByte()) 10280b57cec5SDimitry Andric PreferAnd = true; 10290b57cec5SDimitry Andric } 10300b57cec5SDimitry Andric if (PreferAnd) { 10310b57cec5SDimitry Andric // Replace the current node with an AND. Note that the current node 10320b57cec5SDimitry Andric // might already be that same AND, in which case it is already CSE'd 10330b57cec5SDimitry Andric // with it, and we must not call ReplaceNode. 10340b57cec5SDimitry Andric SDValue In = convertTo(DL, VT, RISBG.Input); 10350b57cec5SDimitry Andric SDValue Mask = CurDAG->getConstant(RISBG.Mask, DL, VT); 10360b57cec5SDimitry Andric SDValue New = CurDAG->getNode(ISD::AND, DL, VT, In, Mask); 10370b57cec5SDimitry Andric if (N != New.getNode()) { 10380b57cec5SDimitry Andric insertDAGNode(CurDAG, N, Mask); 10390b57cec5SDimitry Andric insertDAGNode(CurDAG, N, New); 10400b57cec5SDimitry Andric ReplaceNode(N, New.getNode()); 10410b57cec5SDimitry Andric N = New.getNode(); 10420b57cec5SDimitry Andric } 10430b57cec5SDimitry Andric // Now, select the machine opcode to implement this operation. 10440b57cec5SDimitry Andric if (!N->isMachineOpcode()) 10450b57cec5SDimitry Andric SelectCode(N); 10460b57cec5SDimitry Andric return true; 10470b57cec5SDimitry Andric } 10480b57cec5SDimitry Andric } 10490b57cec5SDimitry Andric 10500b57cec5SDimitry Andric unsigned Opcode = SystemZ::RISBG; 10510b57cec5SDimitry Andric // Prefer RISBGN if available, since it does not clobber CC. 10520b57cec5SDimitry Andric if (Subtarget->hasMiscellaneousExtensions()) 10530b57cec5SDimitry Andric Opcode = SystemZ::RISBGN; 10540b57cec5SDimitry Andric EVT OpcodeVT = MVT::i64; 10550b57cec5SDimitry Andric if (VT == MVT::i32 && Subtarget->hasHighWord() && 10560b57cec5SDimitry Andric // We can only use the 32-bit instructions if all source bits are 10570b57cec5SDimitry Andric // in the low 32 bits without wrapping, both after rotation (because 10580b57cec5SDimitry Andric // of the smaller range for Start and End) and before rotation 10590b57cec5SDimitry Andric // (because the input value is truncated). 10600b57cec5SDimitry Andric RISBG.Start >= 32 && RISBG.End >= RISBG.Start && 10610b57cec5SDimitry Andric ((RISBG.Start + RISBG.Rotate) & 63) >= 32 && 10620b57cec5SDimitry Andric ((RISBG.End + RISBG.Rotate) & 63) >= 10630b57cec5SDimitry Andric ((RISBG.Start + RISBG.Rotate) & 63)) { 10640b57cec5SDimitry Andric Opcode = SystemZ::RISBMux; 10650b57cec5SDimitry Andric OpcodeVT = MVT::i32; 10660b57cec5SDimitry Andric RISBG.Start &= 31; 10670b57cec5SDimitry Andric RISBG.End &= 31; 10680b57cec5SDimitry Andric } 10690b57cec5SDimitry Andric SDValue Ops[5] = { 10700b57cec5SDimitry Andric getUNDEF(DL, OpcodeVT), 10710b57cec5SDimitry Andric convertTo(DL, OpcodeVT, RISBG.Input), 10720b57cec5SDimitry Andric CurDAG->getTargetConstant(RISBG.Start, DL, MVT::i32), 10730b57cec5SDimitry Andric CurDAG->getTargetConstant(RISBG.End | 128, DL, MVT::i32), 10740b57cec5SDimitry Andric CurDAG->getTargetConstant(RISBG.Rotate, DL, MVT::i32) 10750b57cec5SDimitry Andric }; 10760b57cec5SDimitry Andric SDValue New = convertTo( 10770b57cec5SDimitry Andric DL, VT, SDValue(CurDAG->getMachineNode(Opcode, DL, OpcodeVT, Ops), 0)); 10780b57cec5SDimitry Andric ReplaceNode(N, New.getNode()); 10790b57cec5SDimitry Andric return true; 10800b57cec5SDimitry Andric } 10810b57cec5SDimitry Andric 10820b57cec5SDimitry Andric bool SystemZDAGToDAGISel::tryRxSBG(SDNode *N, unsigned Opcode) { 10830b57cec5SDimitry Andric SDLoc DL(N); 10840b57cec5SDimitry Andric EVT VT = N->getValueType(0); 10850b57cec5SDimitry Andric if (!VT.isInteger() || VT.getSizeInBits() > 64) 10860b57cec5SDimitry Andric return false; 10870b57cec5SDimitry Andric // Try treating each operand of N as the second operand of the RxSBG 10880b57cec5SDimitry Andric // and see which goes deepest. 10890b57cec5SDimitry Andric RxSBGOperands RxSBG[] = { 10900b57cec5SDimitry Andric RxSBGOperands(Opcode, N->getOperand(0)), 10910b57cec5SDimitry Andric RxSBGOperands(Opcode, N->getOperand(1)) 10920b57cec5SDimitry Andric }; 10930b57cec5SDimitry Andric unsigned Count[] = { 0, 0 }; 10940b57cec5SDimitry Andric for (unsigned I = 0; I < 2; ++I) 1095bdd1243dSDimitry Andric while (RxSBG[I].Input->hasOneUse() && expandRxSBG(RxSBG[I])) 1096bdd1243dSDimitry Andric // In cases of multiple users it seems better to keep the simple 1097bdd1243dSDimitry Andric // instruction as they are one cycle faster, and it also helps in cases 1098bdd1243dSDimitry Andric // where both inputs share a common node. 1099bdd1243dSDimitry Andric // The widening or narrowing is expected to be free. Counting widening 1100bdd1243dSDimitry Andric // or narrowing as a saved operation will result in preferring an R*SBG 1101bdd1243dSDimitry Andric // over a simple shift/logical instruction. 11020b57cec5SDimitry Andric if (RxSBG[I].Input.getOpcode() != ISD::ANY_EXTEND && 11030b57cec5SDimitry Andric RxSBG[I].Input.getOpcode() != ISD::TRUNCATE) 11040b57cec5SDimitry Andric Count[I] += 1; 11050b57cec5SDimitry Andric 11060b57cec5SDimitry Andric // Do nothing if neither operand is suitable. 11070b57cec5SDimitry Andric if (Count[0] == 0 && Count[1] == 0) 11080b57cec5SDimitry Andric return false; 11090b57cec5SDimitry Andric 11100b57cec5SDimitry Andric // Pick the deepest second operand. 11110b57cec5SDimitry Andric unsigned I = Count[0] > Count[1] ? 0 : 1; 11120b57cec5SDimitry Andric SDValue Op0 = N->getOperand(I ^ 1); 11130b57cec5SDimitry Andric 11140b57cec5SDimitry Andric // Prefer IC for character insertions from memory. 11150b57cec5SDimitry Andric if (Opcode == SystemZ::ROSBG && (RxSBG[I].Mask & 0xff) == 0) 11160b57cec5SDimitry Andric if (auto *Load = dyn_cast<LoadSDNode>(Op0.getNode())) 11170b57cec5SDimitry Andric if (Load->getMemoryVT() == MVT::i8) 11180b57cec5SDimitry Andric return false; 11190b57cec5SDimitry Andric 11200b57cec5SDimitry Andric // See whether we can avoid an AND in the first operand by converting 11210b57cec5SDimitry Andric // ROSBG to RISBG. 11220b57cec5SDimitry Andric if (Opcode == SystemZ::ROSBG && detectOrAndInsertion(Op0, RxSBG[I].Mask)) { 11230b57cec5SDimitry Andric Opcode = SystemZ::RISBG; 11240b57cec5SDimitry Andric // Prefer RISBGN if available, since it does not clobber CC. 11250b57cec5SDimitry Andric if (Subtarget->hasMiscellaneousExtensions()) 11260b57cec5SDimitry Andric Opcode = SystemZ::RISBGN; 11270b57cec5SDimitry Andric } 11280b57cec5SDimitry Andric 11290b57cec5SDimitry Andric SDValue Ops[5] = { 11300b57cec5SDimitry Andric convertTo(DL, MVT::i64, Op0), 11310b57cec5SDimitry Andric convertTo(DL, MVT::i64, RxSBG[I].Input), 11320b57cec5SDimitry Andric CurDAG->getTargetConstant(RxSBG[I].Start, DL, MVT::i32), 11330b57cec5SDimitry Andric CurDAG->getTargetConstant(RxSBG[I].End, DL, MVT::i32), 11340b57cec5SDimitry Andric CurDAG->getTargetConstant(RxSBG[I].Rotate, DL, MVT::i32) 11350b57cec5SDimitry Andric }; 11360b57cec5SDimitry Andric SDValue New = convertTo( 11370b57cec5SDimitry Andric DL, VT, SDValue(CurDAG->getMachineNode(Opcode, DL, MVT::i64, Ops), 0)); 11380b57cec5SDimitry Andric ReplaceNode(N, New.getNode()); 11390b57cec5SDimitry Andric return true; 11400b57cec5SDimitry Andric } 11410b57cec5SDimitry Andric 11420b57cec5SDimitry Andric void SystemZDAGToDAGISel::splitLargeImmediate(unsigned Opcode, SDNode *Node, 11430b57cec5SDimitry Andric SDValue Op0, uint64_t UpperVal, 11440b57cec5SDimitry Andric uint64_t LowerVal) { 11450b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 11460b57cec5SDimitry Andric SDLoc DL(Node); 11470b57cec5SDimitry Andric SDValue Upper = CurDAG->getConstant(UpperVal, DL, VT); 11480b57cec5SDimitry Andric if (Op0.getNode()) 11490b57cec5SDimitry Andric Upper = CurDAG->getNode(Opcode, DL, VT, Op0, Upper); 11500b57cec5SDimitry Andric 11510b57cec5SDimitry Andric { 11520b57cec5SDimitry Andric // When we haven't passed in Op0, Upper will be a constant. In order to 11530b57cec5SDimitry Andric // prevent folding back to the large immediate in `Or = getNode(...)` we run 11540b57cec5SDimitry Andric // SelectCode first and end up with an opaque machine node. This means that 11550b57cec5SDimitry Andric // we need to use a handle to keep track of Upper in case it gets CSE'd by 11560b57cec5SDimitry Andric // SelectCode. 11570b57cec5SDimitry Andric // 11580b57cec5SDimitry Andric // Note that in the case where Op0 is passed in we could just call 11590b57cec5SDimitry Andric // SelectCode(Upper) later, along with the SelectCode(Or), and avoid needing 11600b57cec5SDimitry Andric // the handle at all, but it's fine to do it here. 11610b57cec5SDimitry Andric // 11620b57cec5SDimitry Andric // TODO: This is a pretty hacky way to do this. Can we do something that 11630b57cec5SDimitry Andric // doesn't require a two paragraph explanation? 11640b57cec5SDimitry Andric HandleSDNode Handle(Upper); 11650b57cec5SDimitry Andric SelectCode(Upper.getNode()); 11660b57cec5SDimitry Andric Upper = Handle.getValue(); 11670b57cec5SDimitry Andric } 11680b57cec5SDimitry Andric 11690b57cec5SDimitry Andric SDValue Lower = CurDAG->getConstant(LowerVal, DL, VT); 11700b57cec5SDimitry Andric SDValue Or = CurDAG->getNode(Opcode, DL, VT, Upper, Lower); 11710b57cec5SDimitry Andric 11720b57cec5SDimitry Andric ReplaceNode(Node, Or.getNode()); 11730b57cec5SDimitry Andric 11740b57cec5SDimitry Andric SelectCode(Or.getNode()); 11750b57cec5SDimitry Andric } 11760b57cec5SDimitry Andric 11770b57cec5SDimitry Andric void SystemZDAGToDAGISel::loadVectorConstant( 11780b57cec5SDimitry Andric const SystemZVectorConstantInfo &VCI, SDNode *Node) { 11790b57cec5SDimitry Andric assert((VCI.Opcode == SystemZISD::BYTE_MASK || 11800b57cec5SDimitry Andric VCI.Opcode == SystemZISD::REPLICATE || 11810b57cec5SDimitry Andric VCI.Opcode == SystemZISD::ROTATE_MASK) && 11820b57cec5SDimitry Andric "Bad opcode!"); 11830b57cec5SDimitry Andric assert(VCI.VecVT.getSizeInBits() == 128 && "Expected a vector type"); 11840b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 11850b57cec5SDimitry Andric SDLoc DL(Node); 11860b57cec5SDimitry Andric SmallVector<SDValue, 2> Ops; 11870b57cec5SDimitry Andric for (unsigned OpVal : VCI.OpVals) 11888bcb0991SDimitry Andric Ops.push_back(CurDAG->getTargetConstant(OpVal, DL, MVT::i32)); 11890b57cec5SDimitry Andric SDValue Op = CurDAG->getNode(VCI.Opcode, DL, VCI.VecVT, Ops); 11900b57cec5SDimitry Andric 11910b57cec5SDimitry Andric if (VCI.VecVT == VT.getSimpleVT()) 11920b57cec5SDimitry Andric ReplaceNode(Node, Op.getNode()); 11930b57cec5SDimitry Andric else if (VT.getSizeInBits() == 128) { 11940b57cec5SDimitry Andric SDValue BitCast = CurDAG->getNode(ISD::BITCAST, DL, VT, Op); 11950b57cec5SDimitry Andric ReplaceNode(Node, BitCast.getNode()); 11960b57cec5SDimitry Andric SelectCode(BitCast.getNode()); 11970b57cec5SDimitry Andric } else { // float or double 11980b57cec5SDimitry Andric unsigned SubRegIdx = 11990b57cec5SDimitry Andric (VT.getSizeInBits() == 32 ? SystemZ::subreg_h32 : SystemZ::subreg_h64); 12000b57cec5SDimitry Andric ReplaceNode( 12010b57cec5SDimitry Andric Node, CurDAG->getTargetExtractSubreg(SubRegIdx, DL, VT, Op).getNode()); 12020b57cec5SDimitry Andric } 12030b57cec5SDimitry Andric SelectCode(Op.getNode()); 12040b57cec5SDimitry Andric } 12050b57cec5SDimitry Andric 12065f757f3fSDimitry Andric SDNode *SystemZDAGToDAGISel::loadPoolVectorConstant(APInt Val, EVT VT, SDLoc DL) { 12075f757f3fSDimitry Andric SDNode *ResNode; 12085f757f3fSDimitry Andric assert (VT.getSizeInBits() == 128); 12095f757f3fSDimitry Andric 12105f757f3fSDimitry Andric SDValue CP = CurDAG->getTargetConstantPool( 12115f757f3fSDimitry Andric ConstantInt::get(Type::getInt128Ty(*CurDAG->getContext()), Val), 12125f757f3fSDimitry Andric TLI->getPointerTy(CurDAG->getDataLayout())); 12135f757f3fSDimitry Andric 12145f757f3fSDimitry Andric EVT PtrVT = CP.getValueType(); 12155f757f3fSDimitry Andric SDValue Ops[] = { 12165f757f3fSDimitry Andric SDValue(CurDAG->getMachineNode(SystemZ::LARL, DL, PtrVT, CP), 0), 12175f757f3fSDimitry Andric CurDAG->getTargetConstant(0, DL, PtrVT), 12185f757f3fSDimitry Andric CurDAG->getRegister(0, PtrVT), 12195f757f3fSDimitry Andric CurDAG->getEntryNode() 12205f757f3fSDimitry Andric }; 12215f757f3fSDimitry Andric ResNode = CurDAG->getMachineNode(SystemZ::VL, DL, VT, MVT::Other, Ops); 12225f757f3fSDimitry Andric 12235f757f3fSDimitry Andric // Annotate ResNode with memory operand information so that MachineInstr 12245f757f3fSDimitry Andric // queries work properly. This e.g. gives the register allocation the 12255f757f3fSDimitry Andric // required information for rematerialization. 12265f757f3fSDimitry Andric MachineFunction& MF = CurDAG->getMachineFunction(); 12275f757f3fSDimitry Andric MachineMemOperand *MemOp = 12285f757f3fSDimitry Andric MF.getMachineMemOperand(MachinePointerInfo::getConstantPool(MF), 12295f757f3fSDimitry Andric MachineMemOperand::MOLoad, 16, Align(8)); 12305f757f3fSDimitry Andric 12315f757f3fSDimitry Andric CurDAG->setNodeMemRefs(cast<MachineSDNode>(ResNode), {MemOp}); 12325f757f3fSDimitry Andric return ResNode; 12335f757f3fSDimitry Andric } 12345f757f3fSDimitry Andric 12350b57cec5SDimitry Andric bool SystemZDAGToDAGISel::tryGather(SDNode *N, unsigned Opcode) { 12360b57cec5SDimitry Andric SDValue ElemV = N->getOperand(2); 12370b57cec5SDimitry Andric auto *ElemN = dyn_cast<ConstantSDNode>(ElemV); 12380b57cec5SDimitry Andric if (!ElemN) 12390b57cec5SDimitry Andric return false; 12400b57cec5SDimitry Andric 12410b57cec5SDimitry Andric unsigned Elem = ElemN->getZExtValue(); 12420b57cec5SDimitry Andric EVT VT = N->getValueType(0); 12430b57cec5SDimitry Andric if (Elem >= VT.getVectorNumElements()) 12440b57cec5SDimitry Andric return false; 12450b57cec5SDimitry Andric 12460b57cec5SDimitry Andric auto *Load = dyn_cast<LoadSDNode>(N->getOperand(1)); 12470b57cec5SDimitry Andric if (!Load || !Load->hasNUsesOfValue(1, 0)) 12480b57cec5SDimitry Andric return false; 12490b57cec5SDimitry Andric if (Load->getMemoryVT().getSizeInBits() != 12500b57cec5SDimitry Andric Load->getValueType(0).getSizeInBits()) 12510b57cec5SDimitry Andric return false; 12520b57cec5SDimitry Andric 12530b57cec5SDimitry Andric SDValue Base, Disp, Index; 12540b57cec5SDimitry Andric if (!selectBDVAddr12Only(Load->getBasePtr(), ElemV, Base, Disp, Index) || 12550b57cec5SDimitry Andric Index.getValueType() != VT.changeVectorElementTypeToInteger()) 12560b57cec5SDimitry Andric return false; 12570b57cec5SDimitry Andric 12580b57cec5SDimitry Andric SDLoc DL(Load); 12590b57cec5SDimitry Andric SDValue Ops[] = { 12600b57cec5SDimitry Andric N->getOperand(0), Base, Disp, Index, 12610b57cec5SDimitry Andric CurDAG->getTargetConstant(Elem, DL, MVT::i32), Load->getChain() 12620b57cec5SDimitry Andric }; 12630b57cec5SDimitry Andric SDNode *Res = CurDAG->getMachineNode(Opcode, DL, VT, MVT::Other, Ops); 12640b57cec5SDimitry Andric ReplaceUses(SDValue(Load, 1), SDValue(Res, 1)); 12650b57cec5SDimitry Andric ReplaceNode(N, Res); 12660b57cec5SDimitry Andric return true; 12670b57cec5SDimitry Andric } 12680b57cec5SDimitry Andric 12690b57cec5SDimitry Andric bool SystemZDAGToDAGISel::tryScatter(StoreSDNode *Store, unsigned Opcode) { 12700b57cec5SDimitry Andric SDValue Value = Store->getValue(); 12710b57cec5SDimitry Andric if (Value.getOpcode() != ISD::EXTRACT_VECTOR_ELT) 12720b57cec5SDimitry Andric return false; 12730b57cec5SDimitry Andric if (Store->getMemoryVT().getSizeInBits() != Value.getValueSizeInBits()) 12740b57cec5SDimitry Andric return false; 12750b57cec5SDimitry Andric 12760b57cec5SDimitry Andric SDValue ElemV = Value.getOperand(1); 12770b57cec5SDimitry Andric auto *ElemN = dyn_cast<ConstantSDNode>(ElemV); 12780b57cec5SDimitry Andric if (!ElemN) 12790b57cec5SDimitry Andric return false; 12800b57cec5SDimitry Andric 12810b57cec5SDimitry Andric SDValue Vec = Value.getOperand(0); 12820b57cec5SDimitry Andric EVT VT = Vec.getValueType(); 12830b57cec5SDimitry Andric unsigned Elem = ElemN->getZExtValue(); 12840b57cec5SDimitry Andric if (Elem >= VT.getVectorNumElements()) 12850b57cec5SDimitry Andric return false; 12860b57cec5SDimitry Andric 12870b57cec5SDimitry Andric SDValue Base, Disp, Index; 12880b57cec5SDimitry Andric if (!selectBDVAddr12Only(Store->getBasePtr(), ElemV, Base, Disp, Index) || 12890b57cec5SDimitry Andric Index.getValueType() != VT.changeVectorElementTypeToInteger()) 12900b57cec5SDimitry Andric return false; 12910b57cec5SDimitry Andric 12920b57cec5SDimitry Andric SDLoc DL(Store); 12930b57cec5SDimitry Andric SDValue Ops[] = { 12940b57cec5SDimitry Andric Vec, Base, Disp, Index, CurDAG->getTargetConstant(Elem, DL, MVT::i32), 12950b57cec5SDimitry Andric Store->getChain() 12960b57cec5SDimitry Andric }; 12970b57cec5SDimitry Andric ReplaceNode(Store, CurDAG->getMachineNode(Opcode, DL, MVT::Other, Ops)); 12980b57cec5SDimitry Andric return true; 12990b57cec5SDimitry Andric } 13000b57cec5SDimitry Andric 13010b57cec5SDimitry Andric // Check whether or not the chain ending in StoreNode is suitable for doing 13020b57cec5SDimitry Andric // the {load; op; store} to modify transformation. 13030b57cec5SDimitry Andric static bool isFusableLoadOpStorePattern(StoreSDNode *StoreNode, 13040b57cec5SDimitry Andric SDValue StoredVal, SelectionDAG *CurDAG, 13050b57cec5SDimitry Andric LoadSDNode *&LoadNode, 13060b57cec5SDimitry Andric SDValue &InputChain) { 13070b57cec5SDimitry Andric // Is the stored value result 0 of the operation? 13080b57cec5SDimitry Andric if (StoredVal.getResNo() != 0) 13090b57cec5SDimitry Andric return false; 13100b57cec5SDimitry Andric 13110b57cec5SDimitry Andric // Are there other uses of the loaded value than the operation? 13120b57cec5SDimitry Andric if (!StoredVal.getNode()->hasNUsesOfValue(1, 0)) 13130b57cec5SDimitry Andric return false; 13140b57cec5SDimitry Andric 13150b57cec5SDimitry Andric // Is the store non-extending and non-indexed? 13160b57cec5SDimitry Andric if (!ISD::isNormalStore(StoreNode) || StoreNode->isNonTemporal()) 13170b57cec5SDimitry Andric return false; 13180b57cec5SDimitry Andric 13190b57cec5SDimitry Andric SDValue Load = StoredVal->getOperand(0); 13200b57cec5SDimitry Andric // Is the stored value a non-extending and non-indexed load? 13210b57cec5SDimitry Andric if (!ISD::isNormalLoad(Load.getNode())) 13220b57cec5SDimitry Andric return false; 13230b57cec5SDimitry Andric 13240b57cec5SDimitry Andric // Return LoadNode by reference. 13250b57cec5SDimitry Andric LoadNode = cast<LoadSDNode>(Load); 13260b57cec5SDimitry Andric 13270b57cec5SDimitry Andric // Is store the only read of the loaded value? 13280b57cec5SDimitry Andric if (!Load.hasOneUse()) 13290b57cec5SDimitry Andric return false; 13300b57cec5SDimitry Andric 13310b57cec5SDimitry Andric // Is the address of the store the same as the load? 13320b57cec5SDimitry Andric if (LoadNode->getBasePtr() != StoreNode->getBasePtr() || 13330b57cec5SDimitry Andric LoadNode->getOffset() != StoreNode->getOffset()) 13340b57cec5SDimitry Andric return false; 13350b57cec5SDimitry Andric 13360b57cec5SDimitry Andric // Check if the chain is produced by the load or is a TokenFactor with 13370b57cec5SDimitry Andric // the load output chain as an operand. Return InputChain by reference. 13380b57cec5SDimitry Andric SDValue Chain = StoreNode->getChain(); 13390b57cec5SDimitry Andric 13400b57cec5SDimitry Andric bool ChainCheck = false; 13410b57cec5SDimitry Andric if (Chain == Load.getValue(1)) { 13420b57cec5SDimitry Andric ChainCheck = true; 13430b57cec5SDimitry Andric InputChain = LoadNode->getChain(); 13440b57cec5SDimitry Andric } else if (Chain.getOpcode() == ISD::TokenFactor) { 13450b57cec5SDimitry Andric SmallVector<SDValue, 4> ChainOps; 13460b57cec5SDimitry Andric SmallVector<const SDNode *, 4> LoopWorklist; 13470b57cec5SDimitry Andric SmallPtrSet<const SDNode *, 16> Visited; 13480b57cec5SDimitry Andric const unsigned int Max = 1024; 13490b57cec5SDimitry Andric for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i) { 13500b57cec5SDimitry Andric SDValue Op = Chain.getOperand(i); 13510b57cec5SDimitry Andric if (Op == Load.getValue(1)) { 13520b57cec5SDimitry Andric ChainCheck = true; 13530b57cec5SDimitry Andric // Drop Load, but keep its chain. No cycle check necessary. 13540b57cec5SDimitry Andric ChainOps.push_back(Load.getOperand(0)); 13550b57cec5SDimitry Andric continue; 13560b57cec5SDimitry Andric } 13570b57cec5SDimitry Andric LoopWorklist.push_back(Op.getNode()); 13580b57cec5SDimitry Andric ChainOps.push_back(Op); 13590b57cec5SDimitry Andric } 13600b57cec5SDimitry Andric 13610b57cec5SDimitry Andric if (ChainCheck) { 13620b57cec5SDimitry Andric // Add the other operand of StoredVal to worklist. 13630b57cec5SDimitry Andric for (SDValue Op : StoredVal->ops()) 13640b57cec5SDimitry Andric if (Op.getNode() != LoadNode) 13650b57cec5SDimitry Andric LoopWorklist.push_back(Op.getNode()); 13660b57cec5SDimitry Andric 13670b57cec5SDimitry Andric // Check if Load is reachable from any of the nodes in the worklist. 13680b57cec5SDimitry Andric if (SDNode::hasPredecessorHelper(Load.getNode(), Visited, LoopWorklist, Max, 13690b57cec5SDimitry Andric true)) 13700b57cec5SDimitry Andric return false; 13710b57cec5SDimitry Andric 13720b57cec5SDimitry Andric // Make a new TokenFactor with all the other input chains except 13730b57cec5SDimitry Andric // for the load. 13740b57cec5SDimitry Andric InputChain = CurDAG->getNode(ISD::TokenFactor, SDLoc(Chain), 13750b57cec5SDimitry Andric MVT::Other, ChainOps); 13760b57cec5SDimitry Andric } 13770b57cec5SDimitry Andric } 13780b57cec5SDimitry Andric if (!ChainCheck) 13790b57cec5SDimitry Andric return false; 13800b57cec5SDimitry Andric 13810b57cec5SDimitry Andric return true; 13820b57cec5SDimitry Andric } 13830b57cec5SDimitry Andric 13840b57cec5SDimitry Andric // Change a chain of {load; op; store} of the same value into a simple op 13850b57cec5SDimitry Andric // through memory of that value, if the uses of the modified value and its 13860b57cec5SDimitry Andric // address are suitable. 13870b57cec5SDimitry Andric // 13880b57cec5SDimitry Andric // The tablegen pattern memory operand pattern is currently not able to match 13890b57cec5SDimitry Andric // the case where the CC on the original operation are used. 13900b57cec5SDimitry Andric // 13910b57cec5SDimitry Andric // See the equivalent routine in X86ISelDAGToDAG for further comments. 13920b57cec5SDimitry Andric bool SystemZDAGToDAGISel::tryFoldLoadStoreIntoMemOperand(SDNode *Node) { 13930b57cec5SDimitry Andric StoreSDNode *StoreNode = cast<StoreSDNode>(Node); 13940b57cec5SDimitry Andric SDValue StoredVal = StoreNode->getOperand(1); 13950b57cec5SDimitry Andric unsigned Opc = StoredVal->getOpcode(); 13960b57cec5SDimitry Andric SDLoc DL(StoreNode); 13970b57cec5SDimitry Andric 13980b57cec5SDimitry Andric // Before we try to select anything, make sure this is memory operand size 13990b57cec5SDimitry Andric // and opcode we can handle. Note that this must match the code below that 14000b57cec5SDimitry Andric // actually lowers the opcodes. 14010b57cec5SDimitry Andric EVT MemVT = StoreNode->getMemoryVT(); 14020b57cec5SDimitry Andric unsigned NewOpc = 0; 14030b57cec5SDimitry Andric bool NegateOperand = false; 14040b57cec5SDimitry Andric switch (Opc) { 14050b57cec5SDimitry Andric default: 14060b57cec5SDimitry Andric return false; 14070b57cec5SDimitry Andric case SystemZISD::SSUBO: 14080b57cec5SDimitry Andric NegateOperand = true; 1409bdd1243dSDimitry Andric [[fallthrough]]; 14100b57cec5SDimitry Andric case SystemZISD::SADDO: 14110b57cec5SDimitry Andric if (MemVT == MVT::i32) 14120b57cec5SDimitry Andric NewOpc = SystemZ::ASI; 14130b57cec5SDimitry Andric else if (MemVT == MVT::i64) 14140b57cec5SDimitry Andric NewOpc = SystemZ::AGSI; 14150b57cec5SDimitry Andric else 14160b57cec5SDimitry Andric return false; 14170b57cec5SDimitry Andric break; 14180b57cec5SDimitry Andric case SystemZISD::USUBO: 14190b57cec5SDimitry Andric NegateOperand = true; 1420bdd1243dSDimitry Andric [[fallthrough]]; 14210b57cec5SDimitry Andric case SystemZISD::UADDO: 14220b57cec5SDimitry Andric if (MemVT == MVT::i32) 14230b57cec5SDimitry Andric NewOpc = SystemZ::ALSI; 14240b57cec5SDimitry Andric else if (MemVT == MVT::i64) 14250b57cec5SDimitry Andric NewOpc = SystemZ::ALGSI; 14260b57cec5SDimitry Andric else 14270b57cec5SDimitry Andric return false; 14280b57cec5SDimitry Andric break; 14290b57cec5SDimitry Andric } 14300b57cec5SDimitry Andric 14310b57cec5SDimitry Andric LoadSDNode *LoadNode = nullptr; 14320b57cec5SDimitry Andric SDValue InputChain; 14330b57cec5SDimitry Andric if (!isFusableLoadOpStorePattern(StoreNode, StoredVal, CurDAG, LoadNode, 14340b57cec5SDimitry Andric InputChain)) 14350b57cec5SDimitry Andric return false; 14360b57cec5SDimitry Andric 14370b57cec5SDimitry Andric SDValue Operand = StoredVal.getOperand(1); 14380b57cec5SDimitry Andric auto *OperandC = dyn_cast<ConstantSDNode>(Operand); 14390b57cec5SDimitry Andric if (!OperandC) 14400b57cec5SDimitry Andric return false; 14410b57cec5SDimitry Andric auto OperandV = OperandC->getAPIntValue(); 14420b57cec5SDimitry Andric if (NegateOperand) 14430b57cec5SDimitry Andric OperandV = -OperandV; 144406c3fb27SDimitry Andric if (OperandV.getSignificantBits() > 8) 14450b57cec5SDimitry Andric return false; 14460b57cec5SDimitry Andric Operand = CurDAG->getTargetConstant(OperandV, DL, MemVT); 14470b57cec5SDimitry Andric 14480b57cec5SDimitry Andric SDValue Base, Disp; 14490b57cec5SDimitry Andric if (!selectBDAddr20Only(StoreNode->getBasePtr(), Base, Disp)) 14500b57cec5SDimitry Andric return false; 14510b57cec5SDimitry Andric 14520b57cec5SDimitry Andric SDValue Ops[] = { Base, Disp, Operand, InputChain }; 14530b57cec5SDimitry Andric MachineSDNode *Result = 14540b57cec5SDimitry Andric CurDAG->getMachineNode(NewOpc, DL, MVT::i32, MVT::Other, Ops); 14550b57cec5SDimitry Andric CurDAG->setNodeMemRefs( 14560b57cec5SDimitry Andric Result, {StoreNode->getMemOperand(), LoadNode->getMemOperand()}); 14570b57cec5SDimitry Andric 14580b57cec5SDimitry Andric ReplaceUses(SDValue(StoreNode, 0), SDValue(Result, 1)); 14590b57cec5SDimitry Andric ReplaceUses(SDValue(StoredVal.getNode(), 1), SDValue(Result, 0)); 14600b57cec5SDimitry Andric CurDAG->RemoveDeadNode(Node); 14610b57cec5SDimitry Andric return true; 14620b57cec5SDimitry Andric } 14630b57cec5SDimitry Andric 14640b57cec5SDimitry Andric bool SystemZDAGToDAGISel::canUseBlockOperation(StoreSDNode *Store, 14650b57cec5SDimitry Andric LoadSDNode *Load) const { 14660b57cec5SDimitry Andric // Check that the two memory operands have the same size. 14670b57cec5SDimitry Andric if (Load->getMemoryVT() != Store->getMemoryVT()) 14680b57cec5SDimitry Andric return false; 14690b57cec5SDimitry Andric 14700b57cec5SDimitry Andric // Volatility stops an access from being decomposed. 14710b57cec5SDimitry Andric if (Load->isVolatile() || Store->isVolatile()) 14720b57cec5SDimitry Andric return false; 14730b57cec5SDimitry Andric 14740b57cec5SDimitry Andric // There's no chance of overlap if the load is invariant. 14750b57cec5SDimitry Andric if (Load->isInvariant() && Load->isDereferenceable()) 14760b57cec5SDimitry Andric return true; 14770b57cec5SDimitry Andric 14780b57cec5SDimitry Andric // Otherwise we need to check whether there's an alias. 14790b57cec5SDimitry Andric const Value *V1 = Load->getMemOperand()->getValue(); 14800b57cec5SDimitry Andric const Value *V2 = Store->getMemOperand()->getValue(); 14810b57cec5SDimitry Andric if (!V1 || !V2) 14820b57cec5SDimitry Andric return false; 14830b57cec5SDimitry Andric 14840b57cec5SDimitry Andric // Reject equality. 14850b57cec5SDimitry Andric uint64_t Size = Load->getMemoryVT().getStoreSize(); 14860b57cec5SDimitry Andric int64_t End1 = Load->getSrcValueOffset() + Size; 14870b57cec5SDimitry Andric int64_t End2 = Store->getSrcValueOffset() + Size; 14880b57cec5SDimitry Andric if (V1 == V2 && End1 == End2) 14890b57cec5SDimitry Andric return false; 14900b57cec5SDimitry Andric 1491fe6060f1SDimitry Andric return AA->isNoAlias(MemoryLocation(V1, End1, Load->getAAInfo()), 14920b57cec5SDimitry Andric MemoryLocation(V2, End2, Store->getAAInfo())); 14930b57cec5SDimitry Andric } 14940b57cec5SDimitry Andric 14950b57cec5SDimitry Andric bool SystemZDAGToDAGISel::storeLoadCanUseMVC(SDNode *N) const { 14960b57cec5SDimitry Andric auto *Store = cast<StoreSDNode>(N); 14970b57cec5SDimitry Andric auto *Load = cast<LoadSDNode>(Store->getValue()); 14980b57cec5SDimitry Andric 14990b57cec5SDimitry Andric // Prefer not to use MVC if either address can use ... RELATIVE LONG 15000b57cec5SDimitry Andric // instructions. 15010b57cec5SDimitry Andric uint64_t Size = Load->getMemoryVT().getStoreSize(); 15020b57cec5SDimitry Andric if (Size > 1 && Size <= 8) { 15030b57cec5SDimitry Andric // Prefer LHRL, LRL and LGRL. 15040b57cec5SDimitry Andric if (SystemZISD::isPCREL(Load->getBasePtr().getOpcode())) 15050b57cec5SDimitry Andric return false; 15060b57cec5SDimitry Andric // Prefer STHRL, STRL and STGRL. 15070b57cec5SDimitry Andric if (SystemZISD::isPCREL(Store->getBasePtr().getOpcode())) 15080b57cec5SDimitry Andric return false; 15090b57cec5SDimitry Andric } 15100b57cec5SDimitry Andric 15110b57cec5SDimitry Andric return canUseBlockOperation(Store, Load); 15120b57cec5SDimitry Andric } 15130b57cec5SDimitry Andric 15140b57cec5SDimitry Andric bool SystemZDAGToDAGISel::storeLoadCanUseBlockBinary(SDNode *N, 15150b57cec5SDimitry Andric unsigned I) const { 15160b57cec5SDimitry Andric auto *StoreA = cast<StoreSDNode>(N); 15170b57cec5SDimitry Andric auto *LoadA = cast<LoadSDNode>(StoreA->getValue().getOperand(1 - I)); 15180b57cec5SDimitry Andric auto *LoadB = cast<LoadSDNode>(StoreA->getValue().getOperand(I)); 15195ffd83dbSDimitry Andric return !LoadA->isVolatile() && LoadA->getMemoryVT() == LoadB->getMemoryVT() && 15205ffd83dbSDimitry Andric canUseBlockOperation(StoreA, LoadB); 15210b57cec5SDimitry Andric } 15220b57cec5SDimitry Andric 1523e8d8bef9SDimitry Andric bool SystemZDAGToDAGISel::storeLoadIsAligned(SDNode *N) const { 1524e8d8bef9SDimitry Andric 1525*0fca6ea1SDimitry Andric auto *MemAccess = cast<MemSDNode>(N); 1526*0fca6ea1SDimitry Andric auto *LdSt = dyn_cast<LSBaseSDNode>(MemAccess); 1527e8d8bef9SDimitry Andric TypeSize StoreSize = MemAccess->getMemoryVT().getStoreSize(); 1528e8d8bef9SDimitry Andric SDValue BasePtr = MemAccess->getBasePtr(); 1529e8d8bef9SDimitry Andric MachineMemOperand *MMO = MemAccess->getMemOperand(); 1530e8d8bef9SDimitry Andric assert(MMO && "Expected a memory operand."); 1531e8d8bef9SDimitry Andric 1532e8d8bef9SDimitry Andric // The memory access must have a proper alignment and no index register. 1533*0fca6ea1SDimitry Andric // Only load and store nodes have the offset operand (atomic loads do not). 153481ad6265SDimitry Andric if (MemAccess->getAlign().value() < StoreSize || 1535*0fca6ea1SDimitry Andric (LdSt && !LdSt->getOffset().isUndef())) 1536e8d8bef9SDimitry Andric return false; 1537e8d8bef9SDimitry Andric 1538e8d8bef9SDimitry Andric // The MMO must not have an unaligned offset. 1539e8d8bef9SDimitry Andric if (MMO->getOffset() % StoreSize != 0) 1540e8d8bef9SDimitry Andric return false; 1541e8d8bef9SDimitry Andric 1542e8d8bef9SDimitry Andric // An access to GOT or the Constant Pool is aligned. 1543e8d8bef9SDimitry Andric if (const PseudoSourceValue *PSV = MMO->getPseudoValue()) 1544e8d8bef9SDimitry Andric if ((PSV->isGOT() || PSV->isConstantPool())) 1545e8d8bef9SDimitry Andric return true; 1546e8d8bef9SDimitry Andric 1547e8d8bef9SDimitry Andric // Check the alignment of a Global Address. 1548e8d8bef9SDimitry Andric if (BasePtr.getNumOperands()) 1549e8d8bef9SDimitry Andric if (GlobalAddressSDNode *GA = 1550e8d8bef9SDimitry Andric dyn_cast<GlobalAddressSDNode>(BasePtr.getOperand(0))) { 1551e8d8bef9SDimitry Andric // The immediate offset must be aligned. 1552e8d8bef9SDimitry Andric if (GA->getOffset() % StoreSize != 0) 1553e8d8bef9SDimitry Andric return false; 1554e8d8bef9SDimitry Andric 1555e8d8bef9SDimitry Andric // The alignment of the symbol itself must be at least the store size. 1556e8d8bef9SDimitry Andric const GlobalValue *GV = GA->getGlobal(); 1557*0fca6ea1SDimitry Andric const DataLayout &DL = GV->getDataLayout(); 1558e8d8bef9SDimitry Andric if (GV->getPointerAlignment(DL).value() < StoreSize) 1559e8d8bef9SDimitry Andric return false; 1560e8d8bef9SDimitry Andric } 1561e8d8bef9SDimitry Andric 1562e8d8bef9SDimitry Andric return true; 1563e8d8bef9SDimitry Andric } 1564e8d8bef9SDimitry Andric 1565*0fca6ea1SDimitry Andric ISD::LoadExtType SystemZDAGToDAGISel::getLoadExtType(SDNode *N) const { 1566*0fca6ea1SDimitry Andric ISD::LoadExtType ETy; 1567*0fca6ea1SDimitry Andric if (auto *L = dyn_cast<LoadSDNode>(N)) 1568*0fca6ea1SDimitry Andric ETy = L->getExtensionType(); 1569*0fca6ea1SDimitry Andric else if (auto *AL = dyn_cast<AtomicSDNode>(N)) 1570*0fca6ea1SDimitry Andric ETy = AL->getExtensionType(); 1571*0fca6ea1SDimitry Andric else 1572*0fca6ea1SDimitry Andric llvm_unreachable("Unkown load node type."); 1573*0fca6ea1SDimitry Andric return ETy; 1574*0fca6ea1SDimitry Andric } 1575*0fca6ea1SDimitry Andric 15760b57cec5SDimitry Andric void SystemZDAGToDAGISel::Select(SDNode *Node) { 15770b57cec5SDimitry Andric // If we have a custom node, we already have selected! 15780b57cec5SDimitry Andric if (Node->isMachineOpcode()) { 15790b57cec5SDimitry Andric LLVM_DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n"); 15800b57cec5SDimitry Andric Node->setNodeId(-1); 15810b57cec5SDimitry Andric return; 15820b57cec5SDimitry Andric } 15830b57cec5SDimitry Andric 15840b57cec5SDimitry Andric unsigned Opcode = Node->getOpcode(); 15850b57cec5SDimitry Andric switch (Opcode) { 15860b57cec5SDimitry Andric case ISD::OR: 15870b57cec5SDimitry Andric if (Node->getOperand(1).getOpcode() != ISD::Constant) 15880b57cec5SDimitry Andric if (tryRxSBG(Node, SystemZ::ROSBG)) 15890b57cec5SDimitry Andric return; 15900b57cec5SDimitry Andric goto or_xor; 15910b57cec5SDimitry Andric 15920b57cec5SDimitry Andric case ISD::XOR: 15930b57cec5SDimitry Andric if (Node->getOperand(1).getOpcode() != ISD::Constant) 15940b57cec5SDimitry Andric if (tryRxSBG(Node, SystemZ::RXSBG)) 15950b57cec5SDimitry Andric return; 15960b57cec5SDimitry Andric // Fall through. 15970b57cec5SDimitry Andric or_xor: 15980b57cec5SDimitry Andric // If this is a 64-bit operation in which both 32-bit halves are nonzero, 15990b57cec5SDimitry Andric // split the operation into two. If both operands here happen to be 16000b57cec5SDimitry Andric // constant, leave this to common code to optimize. 16010b57cec5SDimitry Andric if (Node->getValueType(0) == MVT::i64 && 16020b57cec5SDimitry Andric Node->getOperand(0).getOpcode() != ISD::Constant) 16030b57cec5SDimitry Andric if (auto *Op1 = dyn_cast<ConstantSDNode>(Node->getOperand(1))) { 16040b57cec5SDimitry Andric uint64_t Val = Op1->getZExtValue(); 16050b57cec5SDimitry Andric // Don't split the operation if we can match one of the combined 16060b57cec5SDimitry Andric // logical operations provided by miscellaneous-extensions-3. 16070b57cec5SDimitry Andric if (Subtarget->hasMiscellaneousExtensions3()) { 16080b57cec5SDimitry Andric unsigned ChildOpcode = Node->getOperand(0).getOpcode(); 16090b57cec5SDimitry Andric // Check whether this expression matches NAND/NOR/NXOR. 16100b57cec5SDimitry Andric if (Val == (uint64_t)-1 && Opcode == ISD::XOR) 16110b57cec5SDimitry Andric if (ChildOpcode == ISD::AND || ChildOpcode == ISD::OR || 16120b57cec5SDimitry Andric ChildOpcode == ISD::XOR) 16130b57cec5SDimitry Andric break; 1614480093f4SDimitry Andric // Check whether this expression matches OR-with-complement 1615480093f4SDimitry Andric // (or matches an alternate pattern for NXOR). 1616480093f4SDimitry Andric if (ChildOpcode == ISD::XOR) { 16170b57cec5SDimitry Andric auto Op0 = Node->getOperand(0); 16180b57cec5SDimitry Andric if (auto *Op0Op1 = dyn_cast<ConstantSDNode>(Op0->getOperand(1))) 16190b57cec5SDimitry Andric if (Op0Op1->getZExtValue() == (uint64_t)-1) 16200b57cec5SDimitry Andric break; 16210b57cec5SDimitry Andric } 16220b57cec5SDimitry Andric } 16235f757f3fSDimitry Andric // Don't split an XOR with -1 as LCGR/AGHI is more compact. 16245f757f3fSDimitry Andric if (Opcode == ISD::XOR && Op1->isAllOnes()) 16255f757f3fSDimitry Andric break; 16260b57cec5SDimitry Andric if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val)) { 16270b57cec5SDimitry Andric splitLargeImmediate(Opcode, Node, Node->getOperand(0), 16280b57cec5SDimitry Andric Val - uint32_t(Val), uint32_t(Val)); 16290b57cec5SDimitry Andric return; 16300b57cec5SDimitry Andric } 16310b57cec5SDimitry Andric } 16320b57cec5SDimitry Andric break; 16330b57cec5SDimitry Andric 16340b57cec5SDimitry Andric case ISD::AND: 16350b57cec5SDimitry Andric if (Node->getOperand(1).getOpcode() != ISD::Constant) 16360b57cec5SDimitry Andric if (tryRxSBG(Node, SystemZ::RNSBG)) 16370b57cec5SDimitry Andric return; 1638bdd1243dSDimitry Andric [[fallthrough]]; 16390b57cec5SDimitry Andric case ISD::ROTL: 16400b57cec5SDimitry Andric case ISD::SHL: 16410b57cec5SDimitry Andric case ISD::SRL: 16420b57cec5SDimitry Andric case ISD::ZERO_EXTEND: 16430b57cec5SDimitry Andric if (tryRISBGZero(Node)) 16440b57cec5SDimitry Andric return; 16450b57cec5SDimitry Andric break; 16460b57cec5SDimitry Andric 16475f757f3fSDimitry Andric case ISD::BSWAP: 16485f757f3fSDimitry Andric if (Node->getValueType(0) == MVT::i128) { 16495f757f3fSDimitry Andric SDLoc DL(Node); 16505f757f3fSDimitry Andric SDValue Src = Node->getOperand(0); 16515f757f3fSDimitry Andric Src = CurDAG->getNode(ISD::BITCAST, DL, MVT::v16i8, Src); 16525f757f3fSDimitry Andric 16535f757f3fSDimitry Andric uint64_t Bytes[2] = { 0x0706050403020100ULL, 0x0f0e0d0c0b0a0908ULL }; 16545f757f3fSDimitry Andric SDNode *Mask = loadPoolVectorConstant(APInt(128, Bytes), MVT::v16i8, DL); 16555f757f3fSDimitry Andric SDValue Ops[] = { Src, Src, SDValue(Mask, 0) }; 16565f757f3fSDimitry Andric SDValue Res = SDValue(CurDAG->getMachineNode(SystemZ::VPERM, DL, 16575f757f3fSDimitry Andric MVT::v16i8, Ops), 0); 16585f757f3fSDimitry Andric 16595f757f3fSDimitry Andric Res = CurDAG->getNode(ISD::BITCAST, DL, MVT::i128, Res); 16605f757f3fSDimitry Andric SDNode *ResNode = Res.getNode(); 16615f757f3fSDimitry Andric ReplaceNode(Node, ResNode); 16625f757f3fSDimitry Andric SelectCode(Src.getNode()); 16635f757f3fSDimitry Andric SelectCode(ResNode); 16645f757f3fSDimitry Andric return; 16655f757f3fSDimitry Andric } 16665f757f3fSDimitry Andric break; 16675f757f3fSDimitry Andric 16680b57cec5SDimitry Andric case ISD::Constant: 16690b57cec5SDimitry Andric // If this is a 64-bit constant that is out of the range of LLILF, 16700b57cec5SDimitry Andric // LLIHF and LGFI, split it into two 32-bit pieces. 16710b57cec5SDimitry Andric if (Node->getValueType(0) == MVT::i64) { 16721db9f3b2SDimitry Andric uint64_t Val = Node->getAsZExtVal(); 16730b57cec5SDimitry Andric if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val) && !isInt<32>(Val)) { 16740b57cec5SDimitry Andric splitLargeImmediate(ISD::OR, Node, SDValue(), Val - uint32_t(Val), 16750b57cec5SDimitry Andric uint32_t(Val)); 16760b57cec5SDimitry Andric return; 16770b57cec5SDimitry Andric } 16780b57cec5SDimitry Andric } 16795f757f3fSDimitry Andric if (Node->getValueType(0) == MVT::i128) { 1680297eecfbSDimitry Andric const APInt &Val = Node->getAsAPIntVal(); 16815f757f3fSDimitry Andric SystemZVectorConstantInfo VCI(Val); 16825f757f3fSDimitry Andric if (VCI.isVectorConstantLegal(*Subtarget)) { 16835f757f3fSDimitry Andric loadVectorConstant(VCI, Node); 16845f757f3fSDimitry Andric return; 16855f757f3fSDimitry Andric } 16865f757f3fSDimitry Andric // If we can't materialize the constant we need to use a literal pool. 16875f757f3fSDimitry Andric SDNode *ResNode = loadPoolVectorConstant(Val, MVT::i128, SDLoc(Node)); 16885f757f3fSDimitry Andric ReplaceNode(Node, ResNode); 16895f757f3fSDimitry Andric return; 16905f757f3fSDimitry Andric } 16910b57cec5SDimitry Andric break; 16920b57cec5SDimitry Andric 16930b57cec5SDimitry Andric case SystemZISD::SELECT_CCMASK: { 16940b57cec5SDimitry Andric SDValue Op0 = Node->getOperand(0); 16950b57cec5SDimitry Andric SDValue Op1 = Node->getOperand(1); 16960b57cec5SDimitry Andric // Prefer to put any load first, so that it can be matched as a 16970b57cec5SDimitry Andric // conditional load. Likewise for constants in range for LOCHI. 16980b57cec5SDimitry Andric if ((Op1.getOpcode() == ISD::LOAD && Op0.getOpcode() != ISD::LOAD) || 16990b57cec5SDimitry Andric (Subtarget->hasLoadStoreOnCond2() && 17000b57cec5SDimitry Andric Node->getValueType(0).isInteger() && 17015f757f3fSDimitry Andric Node->getValueType(0).getSizeInBits() <= 64 && 17020b57cec5SDimitry Andric Op1.getOpcode() == ISD::Constant && 17030b57cec5SDimitry Andric isInt<16>(cast<ConstantSDNode>(Op1)->getSExtValue()) && 17040b57cec5SDimitry Andric !(Op0.getOpcode() == ISD::Constant && 17050b57cec5SDimitry Andric isInt<16>(cast<ConstantSDNode>(Op0)->getSExtValue())))) { 17060b57cec5SDimitry Andric SDValue CCValid = Node->getOperand(2); 17070b57cec5SDimitry Andric SDValue CCMask = Node->getOperand(3); 17081db9f3b2SDimitry Andric uint64_t ConstCCValid = CCValid.getNode()->getAsZExtVal(); 17091db9f3b2SDimitry Andric uint64_t ConstCCMask = CCMask.getNode()->getAsZExtVal(); 17100b57cec5SDimitry Andric // Invert the condition. 17118bcb0991SDimitry Andric CCMask = CurDAG->getTargetConstant(ConstCCValid ^ ConstCCMask, 17128bcb0991SDimitry Andric SDLoc(Node), CCMask.getValueType()); 17130b57cec5SDimitry Andric SDValue Op4 = Node->getOperand(4); 17140b57cec5SDimitry Andric SDNode *UpdatedNode = 17150b57cec5SDimitry Andric CurDAG->UpdateNodeOperands(Node, Op1, Op0, CCValid, CCMask, Op4); 17160b57cec5SDimitry Andric if (UpdatedNode != Node) { 17170b57cec5SDimitry Andric // In case this node already exists then replace Node with it. 17180b57cec5SDimitry Andric ReplaceNode(Node, UpdatedNode); 17190b57cec5SDimitry Andric Node = UpdatedNode; 17200b57cec5SDimitry Andric } 17210b57cec5SDimitry Andric } 17220b57cec5SDimitry Andric break; 17230b57cec5SDimitry Andric } 17240b57cec5SDimitry Andric 17250b57cec5SDimitry Andric case ISD::INSERT_VECTOR_ELT: { 17260b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 17270b57cec5SDimitry Andric unsigned ElemBitSize = VT.getScalarSizeInBits(); 17280b57cec5SDimitry Andric if (ElemBitSize == 32) { 17290b57cec5SDimitry Andric if (tryGather(Node, SystemZ::VGEF)) 17300b57cec5SDimitry Andric return; 17310b57cec5SDimitry Andric } else if (ElemBitSize == 64) { 17320b57cec5SDimitry Andric if (tryGather(Node, SystemZ::VGEG)) 17330b57cec5SDimitry Andric return; 17340b57cec5SDimitry Andric } 17350b57cec5SDimitry Andric break; 17360b57cec5SDimitry Andric } 17370b57cec5SDimitry Andric 17380b57cec5SDimitry Andric case ISD::BUILD_VECTOR: { 17390b57cec5SDimitry Andric auto *BVN = cast<BuildVectorSDNode>(Node); 17400b57cec5SDimitry Andric SystemZVectorConstantInfo VCI(BVN); 17410b57cec5SDimitry Andric if (VCI.isVectorConstantLegal(*Subtarget)) { 17420b57cec5SDimitry Andric loadVectorConstant(VCI, Node); 17430b57cec5SDimitry Andric return; 17440b57cec5SDimitry Andric } 17450b57cec5SDimitry Andric break; 17460b57cec5SDimitry Andric } 17470b57cec5SDimitry Andric 17480b57cec5SDimitry Andric case ISD::ConstantFP: { 17490b57cec5SDimitry Andric APFloat Imm = cast<ConstantFPSDNode>(Node)->getValueAPF(); 17500b57cec5SDimitry Andric if (Imm.isZero() || Imm.isNegZero()) 17510b57cec5SDimitry Andric break; 17520b57cec5SDimitry Andric SystemZVectorConstantInfo VCI(Imm); 17530b57cec5SDimitry Andric bool Success = VCI.isVectorConstantLegal(*Subtarget); (void)Success; 17540b57cec5SDimitry Andric assert(Success && "Expected legal FP immediate"); 17550b57cec5SDimitry Andric loadVectorConstant(VCI, Node); 17560b57cec5SDimitry Andric return; 17570b57cec5SDimitry Andric } 17580b57cec5SDimitry Andric 17590b57cec5SDimitry Andric case ISD::STORE: { 17600b57cec5SDimitry Andric if (tryFoldLoadStoreIntoMemOperand(Node)) 17610b57cec5SDimitry Andric return; 17620b57cec5SDimitry Andric auto *Store = cast<StoreSDNode>(Node); 17630b57cec5SDimitry Andric unsigned ElemBitSize = Store->getValue().getValueSizeInBits(); 17640b57cec5SDimitry Andric if (ElemBitSize == 32) { 17650b57cec5SDimitry Andric if (tryScatter(Store, SystemZ::VSCEF)) 17660b57cec5SDimitry Andric return; 17670b57cec5SDimitry Andric } else if (ElemBitSize == 64) { 17680b57cec5SDimitry Andric if (tryScatter(Store, SystemZ::VSCEG)) 17690b57cec5SDimitry Andric return; 17700b57cec5SDimitry Andric } 17710b57cec5SDimitry Andric break; 17720b57cec5SDimitry Andric } 1773*0fca6ea1SDimitry Andric 1774*0fca6ea1SDimitry Andric case ISD::ATOMIC_STORE: { 1775*0fca6ea1SDimitry Andric auto *AtomOp = cast<AtomicSDNode>(Node); 1776*0fca6ea1SDimitry Andric // Replace the atomic_store with a regular store and select it. This is 1777*0fca6ea1SDimitry Andric // ok since we know all store instructions <= 8 bytes are atomic, and the 1778*0fca6ea1SDimitry Andric // 16 byte case is already handled during lowering. 1779*0fca6ea1SDimitry Andric StoreSDNode *St = cast<StoreSDNode>(CurDAG->getTruncStore( 1780*0fca6ea1SDimitry Andric AtomOp->getChain(), SDLoc(AtomOp), AtomOp->getVal(), 1781*0fca6ea1SDimitry Andric AtomOp->getBasePtr(), AtomOp->getMemoryVT(), AtomOp->getMemOperand())); 1782*0fca6ea1SDimitry Andric assert(St->getMemOperand()->isAtomic() && "Broken MMO."); 1783*0fca6ea1SDimitry Andric SDNode *Chain = St; 1784*0fca6ea1SDimitry Andric // We have to enforce sequential consistency by performing a 1785*0fca6ea1SDimitry Andric // serialization operation after the store. 1786*0fca6ea1SDimitry Andric if (AtomOp->getSuccessOrdering() == AtomicOrdering::SequentiallyConsistent) 1787*0fca6ea1SDimitry Andric Chain = CurDAG->getMachineNode(SystemZ::Serialize, SDLoc(AtomOp), 1788*0fca6ea1SDimitry Andric MVT::Other, SDValue(Chain, 0)); 1789*0fca6ea1SDimitry Andric ReplaceNode(Node, Chain); 1790*0fca6ea1SDimitry Andric SelectCode(St); 1791*0fca6ea1SDimitry Andric return; 1792*0fca6ea1SDimitry Andric } 17930b57cec5SDimitry Andric } 17940b57cec5SDimitry Andric 17950b57cec5SDimitry Andric SelectCode(Node); 17960b57cec5SDimitry Andric } 17970b57cec5SDimitry Andric 17985f757f3fSDimitry Andric bool SystemZDAGToDAGISel::SelectInlineAsmMemoryOperand( 17995f757f3fSDimitry Andric const SDValue &Op, InlineAsm::ConstraintCode ConstraintID, 18000b57cec5SDimitry Andric std::vector<SDValue> &OutOps) { 18010b57cec5SDimitry Andric SystemZAddressingMode::AddrForm Form; 18020b57cec5SDimitry Andric SystemZAddressingMode::DispRange DispRange; 18030b57cec5SDimitry Andric SDValue Base, Disp, Index; 18040b57cec5SDimitry Andric 18050b57cec5SDimitry Andric switch(ConstraintID) { 18060b57cec5SDimitry Andric default: 18070b57cec5SDimitry Andric llvm_unreachable("Unexpected asm memory constraint"); 18085f757f3fSDimitry Andric case InlineAsm::ConstraintCode::i: 18095f757f3fSDimitry Andric case InlineAsm::ConstraintCode::Q: 18105f757f3fSDimitry Andric case InlineAsm::ConstraintCode::ZQ: 18110b57cec5SDimitry Andric // Accept an address with a short displacement, but no index. 18120b57cec5SDimitry Andric Form = SystemZAddressingMode::FormBD; 18130b57cec5SDimitry Andric DispRange = SystemZAddressingMode::Disp12Only; 18140b57cec5SDimitry Andric break; 18155f757f3fSDimitry Andric case InlineAsm::ConstraintCode::R: 18165f757f3fSDimitry Andric case InlineAsm::ConstraintCode::ZR: 18170b57cec5SDimitry Andric // Accept an address with a short displacement and an index. 18180b57cec5SDimitry Andric Form = SystemZAddressingMode::FormBDXNormal; 18190b57cec5SDimitry Andric DispRange = SystemZAddressingMode::Disp12Only; 18200b57cec5SDimitry Andric break; 18215f757f3fSDimitry Andric case InlineAsm::ConstraintCode::S: 18225f757f3fSDimitry Andric case InlineAsm::ConstraintCode::ZS: 18230b57cec5SDimitry Andric // Accept an address with a long displacement, but no index. 18240b57cec5SDimitry Andric Form = SystemZAddressingMode::FormBD; 18250b57cec5SDimitry Andric DispRange = SystemZAddressingMode::Disp20Only; 18260b57cec5SDimitry Andric break; 18275f757f3fSDimitry Andric case InlineAsm::ConstraintCode::T: 18285f757f3fSDimitry Andric case InlineAsm::ConstraintCode::m: 18295f757f3fSDimitry Andric case InlineAsm::ConstraintCode::o: 18305f757f3fSDimitry Andric case InlineAsm::ConstraintCode::p: 18315f757f3fSDimitry Andric case InlineAsm::ConstraintCode::ZT: 18320b57cec5SDimitry Andric // Accept an address with a long displacement and an index. 18330b57cec5SDimitry Andric // m works the same as T, as this is the most general case. 18340b57cec5SDimitry Andric // We don't really have any special handling of "offsettable" 18350b57cec5SDimitry Andric // memory addresses, so just treat o the same as m. 18360b57cec5SDimitry Andric Form = SystemZAddressingMode::FormBDXNormal; 18370b57cec5SDimitry Andric DispRange = SystemZAddressingMode::Disp20Only; 18380b57cec5SDimitry Andric break; 18390b57cec5SDimitry Andric } 18400b57cec5SDimitry Andric 18410b57cec5SDimitry Andric if (selectBDXAddr(Form, DispRange, Op, Base, Disp, Index)) { 18420b57cec5SDimitry Andric const TargetRegisterClass *TRC = 18430b57cec5SDimitry Andric Subtarget->getRegisterInfo()->getPointerRegClass(*MF); 18440b57cec5SDimitry Andric SDLoc DL(Base); 18450b57cec5SDimitry Andric SDValue RC = CurDAG->getTargetConstant(TRC->getID(), DL, MVT::i32); 18460b57cec5SDimitry Andric 18470b57cec5SDimitry Andric // Make sure that the base address doesn't go into %r0. 18480b57cec5SDimitry Andric // If it's a TargetFrameIndex or a fixed register, we shouldn't do anything. 18490b57cec5SDimitry Andric if (Base.getOpcode() != ISD::TargetFrameIndex && 18500b57cec5SDimitry Andric Base.getOpcode() != ISD::Register) { 18510b57cec5SDimitry Andric Base = 18520b57cec5SDimitry Andric SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, 18530b57cec5SDimitry Andric DL, Base.getValueType(), 18540b57cec5SDimitry Andric Base, RC), 0); 18550b57cec5SDimitry Andric } 18560b57cec5SDimitry Andric 18570b57cec5SDimitry Andric // Make sure that the index register isn't assigned to %r0 either. 18580b57cec5SDimitry Andric if (Index.getOpcode() != ISD::Register) { 18590b57cec5SDimitry Andric Index = 18600b57cec5SDimitry Andric SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, 18610b57cec5SDimitry Andric DL, Index.getValueType(), 18620b57cec5SDimitry Andric Index, RC), 0); 18630b57cec5SDimitry Andric } 18640b57cec5SDimitry Andric 18650b57cec5SDimitry Andric OutOps.push_back(Base); 18660b57cec5SDimitry Andric OutOps.push_back(Disp); 18670b57cec5SDimitry Andric OutOps.push_back(Index); 18680b57cec5SDimitry Andric return false; 18690b57cec5SDimitry Andric } 18700b57cec5SDimitry Andric 18710b57cec5SDimitry Andric return true; 18720b57cec5SDimitry Andric } 18730b57cec5SDimitry Andric 18740b57cec5SDimitry Andric // IsProfitableToFold - Returns true if is profitable to fold the specific 18750b57cec5SDimitry Andric // operand node N of U during instruction selection that starts at Root. 18760b57cec5SDimitry Andric bool 18770b57cec5SDimitry Andric SystemZDAGToDAGISel::IsProfitableToFold(SDValue N, SDNode *U, 18780b57cec5SDimitry Andric SDNode *Root) const { 18790b57cec5SDimitry Andric // We want to avoid folding a LOAD into an ICMP node if as a result 18800b57cec5SDimitry Andric // we would be forced to spill the condition code into a GPR. 18810b57cec5SDimitry Andric if (N.getOpcode() == ISD::LOAD && U->getOpcode() == SystemZISD::ICMP) { 18820b57cec5SDimitry Andric if (!N.hasOneUse() || !U->hasOneUse()) 18830b57cec5SDimitry Andric return false; 18840b57cec5SDimitry Andric 18850b57cec5SDimitry Andric // The user of the CC value will usually be a CopyToReg into the 18860b57cec5SDimitry Andric // physical CC register, which in turn is glued and chained to the 18870b57cec5SDimitry Andric // actual instruction that uses the CC value. Bail out if we have 18880b57cec5SDimitry Andric // anything else than that. 18890b57cec5SDimitry Andric SDNode *CCUser = *U->use_begin(); 18900b57cec5SDimitry Andric SDNode *CCRegUser = nullptr; 18910b57cec5SDimitry Andric if (CCUser->getOpcode() == ISD::CopyToReg || 18920b57cec5SDimitry Andric cast<RegisterSDNode>(CCUser->getOperand(1))->getReg() == SystemZ::CC) { 18930b57cec5SDimitry Andric for (auto *U : CCUser->uses()) { 18940b57cec5SDimitry Andric if (CCRegUser == nullptr) 18950b57cec5SDimitry Andric CCRegUser = U; 18960b57cec5SDimitry Andric else if (CCRegUser != U) 18970b57cec5SDimitry Andric return false; 18980b57cec5SDimitry Andric } 18990b57cec5SDimitry Andric } 19000b57cec5SDimitry Andric if (CCRegUser == nullptr) 19010b57cec5SDimitry Andric return false; 19020b57cec5SDimitry Andric 19030b57cec5SDimitry Andric // If the actual instruction is a branch, the only thing that remains to be 19040b57cec5SDimitry Andric // checked is whether the CCUser chain is a predecessor of the load. 19050b57cec5SDimitry Andric if (CCRegUser->isMachineOpcode() && 19060b57cec5SDimitry Andric CCRegUser->getMachineOpcode() == SystemZ::BRC) 19070b57cec5SDimitry Andric return !N->isPredecessorOf(CCUser->getOperand(0).getNode()); 19080b57cec5SDimitry Andric 19090b57cec5SDimitry Andric // Otherwise, the instruction may have multiple operands, and we need to 19100b57cec5SDimitry Andric // verify that none of them are a predecessor of the load. This is exactly 19110b57cec5SDimitry Andric // the same check that would be done by common code if the CC setter were 19120b57cec5SDimitry Andric // glued to the CC user, so simply invoke that check here. 19130b57cec5SDimitry Andric if (!IsLegalToFold(N, U, CCRegUser, OptLevel, false)) 19140b57cec5SDimitry Andric return false; 19150b57cec5SDimitry Andric } 19160b57cec5SDimitry Andric 19170b57cec5SDimitry Andric return true; 19180b57cec5SDimitry Andric } 19190b57cec5SDimitry Andric 19200b57cec5SDimitry Andric namespace { 19210b57cec5SDimitry Andric // Represents a sequence for extracting a 0/1 value from an IPM result: 19220b57cec5SDimitry Andric // (((X ^ XORValue) + AddValue) >> Bit) 19230b57cec5SDimitry Andric struct IPMConversion { 19240b57cec5SDimitry Andric IPMConversion(unsigned xorValue, int64_t addValue, unsigned bit) 19250b57cec5SDimitry Andric : XORValue(xorValue), AddValue(addValue), Bit(bit) {} 19260b57cec5SDimitry Andric 19270b57cec5SDimitry Andric int64_t XORValue; 19280b57cec5SDimitry Andric int64_t AddValue; 19290b57cec5SDimitry Andric unsigned Bit; 19300b57cec5SDimitry Andric }; 19310b57cec5SDimitry Andric } // end anonymous namespace 19320b57cec5SDimitry Andric 19330b57cec5SDimitry Andric // Return a sequence for getting a 1 from an IPM result when CC has a 19340b57cec5SDimitry Andric // value in CCMask and a 0 when CC has a value in CCValid & ~CCMask. 19350b57cec5SDimitry Andric // The handling of CC values outside CCValid doesn't matter. 19360b57cec5SDimitry Andric static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) { 19370b57cec5SDimitry Andric // Deal with cases where the result can be taken directly from a bit 19380b57cec5SDimitry Andric // of the IPM result. 19390b57cec5SDimitry Andric if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3))) 19400b57cec5SDimitry Andric return IPMConversion(0, 0, SystemZ::IPM_CC); 19410b57cec5SDimitry Andric if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3))) 19420b57cec5SDimitry Andric return IPMConversion(0, 0, SystemZ::IPM_CC + 1); 19430b57cec5SDimitry Andric 19440b57cec5SDimitry Andric // Deal with cases where we can add a value to force the sign bit 19450b57cec5SDimitry Andric // to contain the right value. Putting the bit in 31 means we can 19460b57cec5SDimitry Andric // use SRL rather than RISBG(L), and also makes it easier to get a 19470b57cec5SDimitry Andric // 0/-1 value, so it has priority over the other tests below. 19480b57cec5SDimitry Andric // 19490b57cec5SDimitry Andric // These sequences rely on the fact that the upper two bits of the 19500b57cec5SDimitry Andric // IPM result are zero. 19510b57cec5SDimitry Andric uint64_t TopBit = uint64_t(1) << 31; 19520b57cec5SDimitry Andric if (CCMask == (CCValid & SystemZ::CCMASK_0)) 19530b57cec5SDimitry Andric return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31); 19540b57cec5SDimitry Andric if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1))) 19550b57cec5SDimitry Andric return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31); 19560b57cec5SDimitry Andric if (CCMask == (CCValid & (SystemZ::CCMASK_0 19570b57cec5SDimitry Andric | SystemZ::CCMASK_1 19580b57cec5SDimitry Andric | SystemZ::CCMASK_2))) 19590b57cec5SDimitry Andric return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31); 19600b57cec5SDimitry Andric if (CCMask == (CCValid & SystemZ::CCMASK_3)) 19610b57cec5SDimitry Andric return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31); 19620b57cec5SDimitry Andric if (CCMask == (CCValid & (SystemZ::CCMASK_1 19630b57cec5SDimitry Andric | SystemZ::CCMASK_2 19640b57cec5SDimitry Andric | SystemZ::CCMASK_3))) 19650b57cec5SDimitry Andric return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31); 19660b57cec5SDimitry Andric 19670b57cec5SDimitry Andric // Next try inverting the value and testing a bit. 0/1 could be 19680b57cec5SDimitry Andric // handled this way too, but we dealt with that case above. 19690b57cec5SDimitry Andric if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2))) 19700b57cec5SDimitry Andric return IPMConversion(-1, 0, SystemZ::IPM_CC); 19710b57cec5SDimitry Andric 19720b57cec5SDimitry Andric // Handle cases where adding a value forces a non-sign bit to contain 19730b57cec5SDimitry Andric // the right value. 19740b57cec5SDimitry Andric if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2))) 19750b57cec5SDimitry Andric return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1); 19760b57cec5SDimitry Andric if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3))) 19770b57cec5SDimitry Andric return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1); 19780b57cec5SDimitry Andric 19790b57cec5SDimitry Andric // The remaining cases are 1, 2, 0/1/3 and 0/2/3. All these are 19800b57cec5SDimitry Andric // can be done by inverting the low CC bit and applying one of the 19810b57cec5SDimitry Andric // sign-based extractions above. 19820b57cec5SDimitry Andric if (CCMask == (CCValid & SystemZ::CCMASK_1)) 19830b57cec5SDimitry Andric return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31); 19840b57cec5SDimitry Andric if (CCMask == (CCValid & SystemZ::CCMASK_2)) 19850b57cec5SDimitry Andric return IPMConversion(1 << SystemZ::IPM_CC, 19860b57cec5SDimitry Andric TopBit - (3 << SystemZ::IPM_CC), 31); 19870b57cec5SDimitry Andric if (CCMask == (CCValid & (SystemZ::CCMASK_0 19880b57cec5SDimitry Andric | SystemZ::CCMASK_1 19890b57cec5SDimitry Andric | SystemZ::CCMASK_3))) 19900b57cec5SDimitry Andric return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31); 19910b57cec5SDimitry Andric if (CCMask == (CCValid & (SystemZ::CCMASK_0 19920b57cec5SDimitry Andric | SystemZ::CCMASK_2 19930b57cec5SDimitry Andric | SystemZ::CCMASK_3))) 19940b57cec5SDimitry Andric return IPMConversion(1 << SystemZ::IPM_CC, 19950b57cec5SDimitry Andric TopBit - (1 << SystemZ::IPM_CC), 31); 19960b57cec5SDimitry Andric 19970b57cec5SDimitry Andric llvm_unreachable("Unexpected CC combination"); 19980b57cec5SDimitry Andric } 19990b57cec5SDimitry Andric 20000b57cec5SDimitry Andric SDValue SystemZDAGToDAGISel::expandSelectBoolean(SDNode *Node) { 20010b57cec5SDimitry Andric auto *TrueOp = dyn_cast<ConstantSDNode>(Node->getOperand(0)); 20020b57cec5SDimitry Andric auto *FalseOp = dyn_cast<ConstantSDNode>(Node->getOperand(1)); 20030b57cec5SDimitry Andric if (!TrueOp || !FalseOp) 20040b57cec5SDimitry Andric return SDValue(); 20050b57cec5SDimitry Andric if (FalseOp->getZExtValue() != 0) 20060b57cec5SDimitry Andric return SDValue(); 20070b57cec5SDimitry Andric if (TrueOp->getSExtValue() != 1 && TrueOp->getSExtValue() != -1) 20080b57cec5SDimitry Andric return SDValue(); 20090b57cec5SDimitry Andric 20100b57cec5SDimitry Andric auto *CCValidOp = dyn_cast<ConstantSDNode>(Node->getOperand(2)); 20110b57cec5SDimitry Andric auto *CCMaskOp = dyn_cast<ConstantSDNode>(Node->getOperand(3)); 20120b57cec5SDimitry Andric if (!CCValidOp || !CCMaskOp) 20130b57cec5SDimitry Andric return SDValue(); 20140b57cec5SDimitry Andric int CCValid = CCValidOp->getZExtValue(); 20150b57cec5SDimitry Andric int CCMask = CCMaskOp->getZExtValue(); 20160b57cec5SDimitry Andric 20170b57cec5SDimitry Andric SDLoc DL(Node); 20180b57cec5SDimitry Andric SDValue CCReg = Node->getOperand(4); 20190b57cec5SDimitry Andric IPMConversion IPM = getIPMConversion(CCValid, CCMask); 20200b57cec5SDimitry Andric SDValue Result = CurDAG->getNode(SystemZISD::IPM, DL, MVT::i32, CCReg); 20210b57cec5SDimitry Andric 20220b57cec5SDimitry Andric if (IPM.XORValue) 20230b57cec5SDimitry Andric Result = CurDAG->getNode(ISD::XOR, DL, MVT::i32, Result, 20240b57cec5SDimitry Andric CurDAG->getConstant(IPM.XORValue, DL, MVT::i32)); 20250b57cec5SDimitry Andric 20260b57cec5SDimitry Andric if (IPM.AddValue) 20270b57cec5SDimitry Andric Result = CurDAG->getNode(ISD::ADD, DL, MVT::i32, Result, 20280b57cec5SDimitry Andric CurDAG->getConstant(IPM.AddValue, DL, MVT::i32)); 20290b57cec5SDimitry Andric 20300b57cec5SDimitry Andric EVT VT = Node->getValueType(0); 20310b57cec5SDimitry Andric if (VT == MVT::i32 && IPM.Bit == 31) { 20320b57cec5SDimitry Andric unsigned ShiftOp = TrueOp->getSExtValue() == 1 ? ISD::SRL : ISD::SRA; 20330b57cec5SDimitry Andric Result = CurDAG->getNode(ShiftOp, DL, MVT::i32, Result, 20340b57cec5SDimitry Andric CurDAG->getConstant(IPM.Bit, DL, MVT::i32)); 20350b57cec5SDimitry Andric } else { 20360b57cec5SDimitry Andric if (VT != MVT::i32) 20370b57cec5SDimitry Andric Result = CurDAG->getNode(ISD::ANY_EXTEND, DL, VT, Result); 20380b57cec5SDimitry Andric 20390b57cec5SDimitry Andric if (TrueOp->getSExtValue() == 1) { 20400b57cec5SDimitry Andric // The SHR/AND sequence should get optimized to an RISBG. 20410b57cec5SDimitry Andric Result = CurDAG->getNode(ISD::SRL, DL, VT, Result, 20420b57cec5SDimitry Andric CurDAG->getConstant(IPM.Bit, DL, MVT::i32)); 20430b57cec5SDimitry Andric Result = CurDAG->getNode(ISD::AND, DL, VT, Result, 20440b57cec5SDimitry Andric CurDAG->getConstant(1, DL, VT)); 20450b57cec5SDimitry Andric } else { 20460b57cec5SDimitry Andric // Sign-extend from IPM.Bit using a pair of shifts. 20470b57cec5SDimitry Andric int ShlAmt = VT.getSizeInBits() - 1 - IPM.Bit; 20480b57cec5SDimitry Andric int SraAmt = VT.getSizeInBits() - 1; 20490b57cec5SDimitry Andric Result = CurDAG->getNode(ISD::SHL, DL, VT, Result, 20500b57cec5SDimitry Andric CurDAG->getConstant(ShlAmt, DL, MVT::i32)); 20510b57cec5SDimitry Andric Result = CurDAG->getNode(ISD::SRA, DL, VT, Result, 20520b57cec5SDimitry Andric CurDAG->getConstant(SraAmt, DL, MVT::i32)); 20530b57cec5SDimitry Andric } 20540b57cec5SDimitry Andric } 20550b57cec5SDimitry Andric 20560b57cec5SDimitry Andric return Result; 20570b57cec5SDimitry Andric } 20580b57cec5SDimitry Andric 2059*0fca6ea1SDimitry Andric bool SystemZDAGToDAGISel::shouldSelectForReassoc(SDNode *N) const { 2060*0fca6ea1SDimitry Andric EVT VT = N->getValueType(0); 2061*0fca6ea1SDimitry Andric assert(VT.isFloatingPoint() && "Expected FP SDNode"); 2062*0fca6ea1SDimitry Andric return N->getFlags().hasAllowReassociation() && 2063*0fca6ea1SDimitry Andric N->getFlags().hasNoSignedZeros() && Subtarget->hasVector() && 2064*0fca6ea1SDimitry Andric (VT != MVT::f32 || Subtarget->hasVectorEnhancements1()) && 2065*0fca6ea1SDimitry Andric !N->isStrictFPOpcode(); 2066*0fca6ea1SDimitry Andric } 2067*0fca6ea1SDimitry Andric 20680b57cec5SDimitry Andric void SystemZDAGToDAGISel::PreprocessISelDAG() { 20690b57cec5SDimitry Andric // If we have conditional immediate loads, we always prefer 20700b57cec5SDimitry Andric // using those over an IPM sequence. 20710b57cec5SDimitry Andric if (Subtarget->hasLoadStoreOnCond2()) 20720b57cec5SDimitry Andric return; 20730b57cec5SDimitry Andric 20740b57cec5SDimitry Andric bool MadeChange = false; 20750b57cec5SDimitry Andric 20760b57cec5SDimitry Andric for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(), 20770b57cec5SDimitry Andric E = CurDAG->allnodes_end(); 20780b57cec5SDimitry Andric I != E;) { 20790b57cec5SDimitry Andric SDNode *N = &*I++; 20800b57cec5SDimitry Andric if (N->use_empty()) 20810b57cec5SDimitry Andric continue; 20820b57cec5SDimitry Andric 20830b57cec5SDimitry Andric SDValue Res; 20840b57cec5SDimitry Andric switch (N->getOpcode()) { 20850b57cec5SDimitry Andric default: break; 20860b57cec5SDimitry Andric case SystemZISD::SELECT_CCMASK: 20870b57cec5SDimitry Andric Res = expandSelectBoolean(N); 20880b57cec5SDimitry Andric break; 20890b57cec5SDimitry Andric } 20900b57cec5SDimitry Andric 20910b57cec5SDimitry Andric if (Res) { 20920b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "SystemZ DAG preprocessing replacing:\nOld: "); 20930b57cec5SDimitry Andric LLVM_DEBUG(N->dump(CurDAG)); 20940b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\nNew: "); 20950b57cec5SDimitry Andric LLVM_DEBUG(Res.getNode()->dump(CurDAG)); 20960b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\n"); 20970b57cec5SDimitry Andric 20980b57cec5SDimitry Andric CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Res); 20990b57cec5SDimitry Andric MadeChange = true; 21000b57cec5SDimitry Andric } 21010b57cec5SDimitry Andric } 21020b57cec5SDimitry Andric 21030b57cec5SDimitry Andric if (MadeChange) 21040b57cec5SDimitry Andric CurDAG->RemoveDeadNodes(); 21050b57cec5SDimitry Andric } 2106