| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1 | //===-- SystemZISelDAGToDAG.cpp - A dag to dag inst selector for SystemZ --===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines an instruction selector for the SystemZ target. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "SystemZTargetMachine.h" |
| Richard Sandiford | 9784649 | 2013-07-09 09:46:39 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/AliasAnalysis.h" |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 17 | #include "llvm/Support/Debug.h" |
| Craig Topper | d0af7e8 | 2017-04-28 05:31:46 +0000 | [diff] [blame] | 18 | #include "llvm/Support/KnownBits.h" |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
| 20 | |
| 21 | using namespace llvm; |
| 22 | |
| Chandler Carruth | e96dd89 | 2014-04-21 22:55:11 +0000 | [diff] [blame] | 23 | #define DEBUG_TYPE "systemz-isel" |
| 24 | |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 25 | namespace { |
| 26 | // Used to build addressing modes. |
| 27 | struct SystemZAddressingMode { |
| 28 | // The shape of the address. |
| 29 | enum AddrForm { |
| 30 | // base+displacement |
| 31 | FormBD, |
| 32 | |
| 33 | // base+displacement+index for load and store operands |
| 34 | FormBDXNormal, |
| 35 | |
| 36 | // base+displacement+index for load address operands |
| 37 | FormBDXLA, |
| 38 | |
| 39 | // base+displacement+index+ADJDYNALLOC |
| 40 | FormBDXDynAlloc |
| 41 | }; |
| 42 | AddrForm Form; |
| 43 | |
| 44 | // The type of displacement. The enum names here correspond directly |
| 45 | // to the definitions in SystemZOperand.td. We could split them into |
| 46 | // flags -- single/pair, 128-bit, etc. -- but it hardly seems worth it. |
| 47 | enum DispRange { |
| 48 | Disp12Only, |
| 49 | Disp12Pair, |
| 50 | Disp20Only, |
| 51 | Disp20Only128, |
| 52 | Disp20Pair |
| 53 | }; |
| 54 | DispRange DR; |
| 55 | |
| 56 | // The parts of the address. The address is equivalent to: |
| 57 | // |
| 58 | // Base + Disp + Index + (IncludesDynAlloc ? ADJDYNALLOC : 0) |
| 59 | SDValue Base; |
| 60 | int64_t Disp; |
| 61 | SDValue Index; |
| 62 | bool IncludesDynAlloc; |
| 63 | |
| 64 | SystemZAddressingMode(AddrForm form, DispRange dr) |
| 65 | : Form(form), DR(dr), Base(), Disp(0), Index(), |
| 66 | IncludesDynAlloc(false) {} |
| 67 | |
| 68 | // True if the address can have an index register. |
| 69 | bool hasIndexField() { return Form != FormBD; } |
| 70 | |
| 71 | // True if the address can (and must) include ADJDYNALLOC. |
| 72 | bool isDynAlloc() { return Form == FormBDXDynAlloc; } |
| 73 | |
| 74 | void dump() { |
| 75 | errs() << "SystemZAddressingMode " << this << '\n'; |
| 76 | |
| 77 | errs() << " Base "; |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 78 | if (Base.getNode()) |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 79 | Base.getNode()->dump(); |
| 80 | else |
| 81 | errs() << "null\n"; |
| 82 | |
| 83 | if (hasIndexField()) { |
| 84 | errs() << " Index "; |
| Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 85 | if (Index.getNode()) |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 86 | Index.getNode()->dump(); |
| 87 | else |
| 88 | errs() << "null\n"; |
| 89 | } |
| 90 | |
| 91 | errs() << " Disp " << Disp; |
| 92 | if (IncludesDynAlloc) |
| 93 | errs() << " + ADJDYNALLOC"; |
| 94 | errs() << '\n'; |
| 95 | } |
| 96 | }; |
| 97 | |
| Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 98 | // Return a mask with Count low bits set. |
| 99 | static uint64_t allOnes(unsigned int Count) { |
| Ulrich Weigand | 77884bc | 2015-06-25 11:52:36 +0000 | [diff] [blame] | 100 | assert(Count <= 64); |
| Justin Bogner | c97c48a | 2015-06-24 05:59:19 +0000 | [diff] [blame] | 101 | if (Count > 63) |
| 102 | return UINT64_MAX; |
| 103 | return (uint64_t(1) << Count) - 1; |
| Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 106 | // Represents operands 2 to 5 of the ROTATE AND ... SELECTED BITS operation |
| 107 | // given by Opcode. The operands are: Input (R2), Start (I3), End (I4) and |
| 108 | // Rotate (I5). The combined operand value is effectively: |
| 109 | // |
| 110 | // (or (rotl Input, Rotate), ~Mask) |
| 111 | // |
| 112 | // for RNSBG and: |
| 113 | // |
| 114 | // (and (rotl Input, Rotate), Mask) |
| 115 | // |
| Richard Sandiford | 3e38297 | 2013-10-16 13:35:13 +0000 | [diff] [blame] | 116 | // otherwise. The output value has BitSize bits, although Input may be |
| Zhan Jun Liau | 0df3505 | 2016-06-22 16:16:27 +0000 | [diff] [blame] | 117 | // narrower (in which case the upper bits are don't care), or wider (in which |
| 118 | // case the result will be truncated as part of the operation). |
| Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 119 | struct RxSBGOperands { |
| Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 120 | RxSBGOperands(unsigned Op, SDValue N) |
| Sanjay Patel | b1f0a0f | 2016-09-14 16:05:51 +0000 | [diff] [blame] | 121 | : Opcode(Op), BitSize(N.getValueSizeInBits()), |
| Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 122 | Mask(allOnes(BitSize)), Input(N), Start(64 - BitSize), End(63), |
| 123 | Rotate(0) {} |
| Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 124 | |
| Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 125 | unsigned Opcode; |
| Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 126 | unsigned BitSize; |
| 127 | uint64_t Mask; |
| 128 | SDValue Input; |
| 129 | unsigned Start; |
| 130 | unsigned End; |
| 131 | unsigned Rotate; |
| 132 | }; |
| 133 | |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 134 | class SystemZDAGToDAGISel : public SelectionDAGISel { |
| Eric Christopher | a673417 | 2015-01-31 00:06:45 +0000 | [diff] [blame] | 135 | const SystemZSubtarget *Subtarget; |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 136 | |
| 137 | // Used by SystemZOperands.td to create integer constants. |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 138 | inline SDValue getImm(const SDNode *Node, uint64_t Imm) const { |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 139 | return CurDAG->getTargetConstant(Imm, SDLoc(Node), Node->getValueType(0)); |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 142 | const SystemZTargetMachine &getTargetMachine() const { |
| 143 | return static_cast<const SystemZTargetMachine &>(TM); |
| 144 | } |
| 145 | |
| 146 | const SystemZInstrInfo *getInstrInfo() const { |
| Eric Christopher | a673417 | 2015-01-31 00:06:45 +0000 | [diff] [blame] | 147 | return Subtarget->getInstrInfo(); |
| Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 150 | // Try to fold more of the base or index of AM into AM, where IsBase |
| 151 | // selects between the base and index. |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 152 | bool expandAddress(SystemZAddressingMode &AM, bool IsBase) const; |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 153 | |
| 154 | // Try to describe N in AM, returning true on success. |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 155 | bool selectAddress(SDValue N, SystemZAddressingMode &AM) const; |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 156 | |
| 157 | // Extract individual target operands from matched address AM. |
| 158 | void getAddressOperands(const SystemZAddressingMode &AM, EVT VT, |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 159 | SDValue &Base, SDValue &Disp) const; |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 160 | void getAddressOperands(const SystemZAddressingMode &AM, EVT VT, |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 161 | SDValue &Base, SDValue &Disp, SDValue &Index) const; |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 162 | |
| 163 | // Try to match Addr as a FormBD address with displacement type DR. |
| 164 | // Return true on success, storing the base and displacement in |
| 165 | // Base and Disp respectively. |
| 166 | bool selectBDAddr(SystemZAddressingMode::DispRange DR, SDValue Addr, |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 167 | SDValue &Base, SDValue &Disp) const; |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 168 | |
| Richard Sandiford | a481f58 | 2013-08-23 11:18:53 +0000 | [diff] [blame] | 169 | // Try to match Addr as a FormBDX address with displacement type DR. |
| 170 | // Return true on success and if the result had no index. Store the |
| 171 | // base and displacement in Base and Disp respectively. |
| 172 | bool selectMVIAddr(SystemZAddressingMode::DispRange DR, SDValue Addr, |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 173 | SDValue &Base, SDValue &Disp) const; |
| Richard Sandiford | a481f58 | 2013-08-23 11:18:53 +0000 | [diff] [blame] | 174 | |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 175 | // Try to match Addr as a FormBDX* address of form Form with |
| 176 | // displacement type DR. Return true on success, storing the base, |
| 177 | // displacement and index in Base, Disp and Index respectively. |
| 178 | bool selectBDXAddr(SystemZAddressingMode::AddrForm Form, |
| 179 | SystemZAddressingMode::DispRange DR, SDValue Addr, |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 180 | SDValue &Base, SDValue &Disp, SDValue &Index) const; |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 181 | |
| 182 | // PC-relative address matching routines used by SystemZOperands.td. |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 183 | bool selectPCRelAddress(SDValue Addr, SDValue &Target) const { |
| 184 | if (SystemZISD::isPCREL(Addr.getOpcode())) { |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 185 | Target = Addr.getOperand(0); |
| 186 | return true; |
| 187 | } |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | // BD matching routines used by SystemZOperands.td. |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 192 | bool selectBDAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp) const { |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 193 | return selectBDAddr(SystemZAddressingMode::Disp12Only, Addr, Base, Disp); |
| 194 | } |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 195 | bool selectBDAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const { |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 196 | return selectBDAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp); |
| 197 | } |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 198 | bool selectBDAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp) const { |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 199 | return selectBDAddr(SystemZAddressingMode::Disp20Only, Addr, Base, Disp); |
| 200 | } |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 201 | bool selectBDAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const { |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 202 | return selectBDAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp); |
| 203 | } |
| 204 | |
| Richard Sandiford | a481f58 | 2013-08-23 11:18:53 +0000 | [diff] [blame] | 205 | // MVI matching routines used by SystemZOperands.td. |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 206 | bool selectMVIAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const { |
| Richard Sandiford | a481f58 | 2013-08-23 11:18:53 +0000 | [diff] [blame] | 207 | return selectMVIAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp); |
| 208 | } |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 209 | bool selectMVIAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const { |
| Richard Sandiford | a481f58 | 2013-08-23 11:18:53 +0000 | [diff] [blame] | 210 | return selectMVIAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp); |
| 211 | } |
| 212 | |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 213 | // BDX matching routines used by SystemZOperands.td. |
| 214 | bool selectBDXAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp, |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 215 | SDValue &Index) const { |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 216 | return selectBDXAddr(SystemZAddressingMode::FormBDXNormal, |
| 217 | SystemZAddressingMode::Disp12Only, |
| 218 | Addr, Base, Disp, Index); |
| 219 | } |
| 220 | bool selectBDXAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp, |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 221 | SDValue &Index) const { |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 222 | return selectBDXAddr(SystemZAddressingMode::FormBDXNormal, |
| 223 | SystemZAddressingMode::Disp12Pair, |
| 224 | Addr, Base, Disp, Index); |
| 225 | } |
| 226 | bool selectDynAlloc12Only(SDValue Addr, SDValue &Base, SDValue &Disp, |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 227 | SDValue &Index) const { |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 228 | return selectBDXAddr(SystemZAddressingMode::FormBDXDynAlloc, |
| 229 | SystemZAddressingMode::Disp12Only, |
| 230 | Addr, Base, Disp, Index); |
| 231 | } |
| 232 | bool selectBDXAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp, |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 233 | SDValue &Index) const { |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 234 | return selectBDXAddr(SystemZAddressingMode::FormBDXNormal, |
| 235 | SystemZAddressingMode::Disp20Only, |
| 236 | Addr, Base, Disp, Index); |
| 237 | } |
| 238 | bool selectBDXAddr20Only128(SDValue Addr, SDValue &Base, SDValue &Disp, |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 239 | SDValue &Index) const { |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 240 | return selectBDXAddr(SystemZAddressingMode::FormBDXNormal, |
| 241 | SystemZAddressingMode::Disp20Only128, |
| 242 | Addr, Base, Disp, Index); |
| 243 | } |
| 244 | bool selectBDXAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp, |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 245 | SDValue &Index) const { |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 246 | return selectBDXAddr(SystemZAddressingMode::FormBDXNormal, |
| 247 | SystemZAddressingMode::Disp20Pair, |
| 248 | Addr, Base, Disp, Index); |
| 249 | } |
| 250 | bool selectLAAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp, |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 251 | SDValue &Index) const { |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 252 | return selectBDXAddr(SystemZAddressingMode::FormBDXLA, |
| 253 | SystemZAddressingMode::Disp12Pair, |
| 254 | Addr, Base, Disp, Index); |
| 255 | } |
| 256 | bool selectLAAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp, |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 257 | SDValue &Index) const { |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 258 | return selectBDXAddr(SystemZAddressingMode::FormBDXLA, |
| 259 | SystemZAddressingMode::Disp20Pair, |
| 260 | Addr, Base, Disp, Index); |
| 261 | } |
| 262 | |
| Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 263 | // Try to match Addr as an address with a base, 12-bit displacement |
| 264 | // and index, where the index is element Elem of a vector. |
| 265 | // Return true on success, storing the base, displacement and vector |
| 266 | // in Base, Disp and Index respectively. |
| 267 | bool selectBDVAddr12Only(SDValue Addr, SDValue Elem, SDValue &Base, |
| 268 | SDValue &Disp, SDValue &Index) const; |
| 269 | |
| Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 270 | // Check whether (or Op (and X InsertMask)) is effectively an insertion |
| 271 | // of X into bits InsertMask of some Y != Op. Return true if so and |
| 272 | // set Op to that Y. |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 273 | bool detectOrAndInsertion(SDValue &Op, uint64_t InsertMask) const; |
| Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 274 | |
| Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 275 | // Try to update RxSBG so that only the bits of RxSBG.Input in Mask are used. |
| 276 | // Return true on success. |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 277 | bool refineRxSBGMask(RxSBGOperands &RxSBG, uint64_t Mask) const; |
| Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 278 | |
| Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 279 | // Try to fold some of RxSBG.Input into other fields of RxSBG. |
| 280 | // Return true on success. |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 281 | bool expandRxSBG(RxSBGOperands &RxSBG) const; |
| Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 282 | |
| Richard Sandiford | 3ad5a15 | 2013-10-01 14:36:20 +0000 | [diff] [blame] | 283 | // Return an undefined value of type VT. |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 284 | SDValue getUNDEF(const SDLoc &DL, EVT VT) const; |
| Richard Sandiford | 84f54a3 | 2013-07-11 08:59:12 +0000 | [diff] [blame] | 285 | |
| 286 | // Convert N to VT, if it isn't already. |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 287 | SDValue convertTo(const SDLoc &DL, EVT VT, SDValue N) const; |
| Richard Sandiford | 84f54a3 | 2013-07-11 08:59:12 +0000 | [diff] [blame] | 288 | |
| Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 289 | // Try to implement AND or shift node N using RISBG with the zero flag set. |
| 290 | // Return the selected node on success, otherwise return null. |
| Justin Bogner | bbcd223 | 2016-05-10 21:11:26 +0000 | [diff] [blame] | 291 | bool tryRISBGZero(SDNode *N); |
| Richard Sandiford | 84f54a3 | 2013-07-11 08:59:12 +0000 | [diff] [blame] | 292 | |
| Richard Sandiford | 7878b85 | 2013-07-18 10:06:15 +0000 | [diff] [blame] | 293 | // Try to use RISBG or Opcode to implement OR or XOR node N. |
| 294 | // Return the selected node on success, otherwise return null. |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 295 | bool tryRxSBG(SDNode *N, unsigned Opcode); |
| Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 296 | |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 297 | // If Op0 is null, then Node is a constant that can be loaded using: |
| 298 | // |
| 299 | // (Opcode UpperVal LowerVal) |
| 300 | // |
| 301 | // If Op0 is nonnull, then Node can be implemented using: |
| 302 | // |
| 303 | // (Opcode (Opcode Op0 UpperVal) LowerVal) |
| Justin Bogner | ffb273d | 2016-05-09 23:54:23 +0000 | [diff] [blame] | 304 | void splitLargeImmediate(unsigned Opcode, SDNode *Node, SDValue Op0, |
| 305 | uint64_t UpperVal, uint64_t LowerVal); |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 306 | |
| Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 307 | // Try to use gather instruction Opcode to implement vector insertion N. |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 308 | bool tryGather(SDNode *N, unsigned Opcode); |
| Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 309 | |
| 310 | // Try to use scatter instruction Opcode to implement store Store. |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 311 | bool tryScatter(StoreSDNode *Store, unsigned Opcode); |
| Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 312 | |
| Richard Sandiford | 067817e | 2013-09-27 15:29:20 +0000 | [diff] [blame] | 313 | // Return true if Load and Store are loads and stores of the same size |
| 314 | // and are guaranteed not to overlap. Such operations can be implemented |
| 315 | // using block (SS-format) instructions. |
| 316 | // |
| 317 | // Partial overlap would lead to incorrect code, since the block operations |
| 318 | // are logically bytewise, even though they have a fast path for the |
| 319 | // non-overlapping case. We also need to avoid full overlap (i.e. two |
| 320 | // addresses that might be equal at run time) because although that case |
| 321 | // would be handled correctly, it might be implemented by millicode. |
| 322 | bool canUseBlockOperation(StoreSDNode *Store, LoadSDNode *Load) const; |
| 323 | |
| Richard Sandiford | 178273a | 2013-09-05 10:36:45 +0000 | [diff] [blame] | 324 | // N is a (store (load Y), X) pattern. Return true if it can use an MVC |
| 325 | // from Y to X. |
| Richard Sandiford | 9784649 | 2013-07-09 09:46:39 +0000 | [diff] [blame] | 326 | bool storeLoadCanUseMVC(SDNode *N) const; |
| 327 | |
| Richard Sandiford | 178273a | 2013-09-05 10:36:45 +0000 | [diff] [blame] | 328 | // N is a (store (op (load A[0]), (load A[1])), X) pattern. Return true |
| 329 | // if A[1 - I] == X and if N can use a block operation like NC from A[I] |
| 330 | // to X. |
| 331 | bool storeLoadCanUseBlockBinary(SDNode *N, unsigned I) const; |
| 332 | |
| Ulrich Weigand | 849a59f | 2018-01-19 20:52:04 +0000 | [diff] [blame] | 333 | // Try to expand a boolean SELECT_CCMASK using an IPM sequence. |
| 334 | SDValue expandSelectBoolean(SDNode *Node); |
| 335 | |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 336 | public: |
| 337 | SystemZDAGToDAGISel(SystemZTargetMachine &TM, CodeGenOpt::Level OptLevel) |
| Eric Christopher | a673417 | 2015-01-31 00:06:45 +0000 | [diff] [blame] | 338 | : SelectionDAGISel(TM, OptLevel) {} |
| 339 | |
| 340 | bool runOnMachineFunction(MachineFunction &MF) override { |
| 341 | Subtarget = &MF.getSubtarget<SystemZSubtarget>(); |
| 342 | return SelectionDAGISel::runOnMachineFunction(MF); |
| 343 | } |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 344 | |
| 345 | // Override MachineFunctionPass. |
| Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 346 | StringRef getPassName() const override { |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 347 | return "SystemZ DAG->DAG Pattern Instruction Selection"; |
| 348 | } |
| 349 | |
| 350 | // Override SelectionDAGISel. |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 351 | void Select(SDNode *Node) override; |
| Daniel Sanders | 60f1db0 | 2015-03-13 12:45:09 +0000 | [diff] [blame] | 352 | bool SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID, |
| Richard Sandiford | b4d67b5 | 2014-03-06 12:03:36 +0000 | [diff] [blame] | 353 | std::vector<SDValue> &OutOps) override; |
| Ulrich Weigand | b32f365 | 2018-04-30 17:52:32 +0000 | [diff] [blame^] | 354 | bool IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const override; |
| Ulrich Weigand | 849a59f | 2018-01-19 20:52:04 +0000 | [diff] [blame] | 355 | void PreprocessISelDAG() override; |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 356 | |
| 357 | // Include the pieces autogenerated from the target description. |
| 358 | #include "SystemZGenDAGISel.inc" |
| 359 | }; |
| 360 | } // end anonymous namespace |
| 361 | |
| 362 | FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM, |
| 363 | CodeGenOpt::Level OptLevel) { |
| 364 | return new SystemZDAGToDAGISel(TM, OptLevel); |
| 365 | } |
| 366 | |
| 367 | // Return true if Val should be selected as a displacement for an address |
| 368 | // with range DR. Here we're interested in the range of both the instruction |
| 369 | // described by DR and of any pairing instruction. |
| 370 | static bool selectDisp(SystemZAddressingMode::DispRange DR, int64_t Val) { |
| 371 | switch (DR) { |
| 372 | case SystemZAddressingMode::Disp12Only: |
| 373 | return isUInt<12>(Val); |
| 374 | |
| 375 | case SystemZAddressingMode::Disp12Pair: |
| 376 | case SystemZAddressingMode::Disp20Only: |
| 377 | case SystemZAddressingMode::Disp20Pair: |
| 378 | return isInt<20>(Val); |
| 379 | |
| 380 | case SystemZAddressingMode::Disp20Only128: |
| 381 | return isInt<20>(Val) && isInt<20>(Val + 8); |
| 382 | } |
| 383 | llvm_unreachable("Unhandled displacement range"); |
| 384 | } |
| 385 | |
| 386 | // Change the base or index in AM to Value, where IsBase selects |
| 387 | // between the base and index. |
| 388 | static void changeComponent(SystemZAddressingMode &AM, bool IsBase, |
| 389 | SDValue Value) { |
| 390 | if (IsBase) |
| 391 | AM.Base = Value; |
| 392 | else |
| 393 | AM.Index = Value; |
| 394 | } |
| 395 | |
| 396 | // The base or index of AM is equivalent to Value + ADJDYNALLOC, |
| 397 | // where IsBase selects between the base and index. Try to fold the |
| 398 | // ADJDYNALLOC into AM. |
| 399 | static bool expandAdjDynAlloc(SystemZAddressingMode &AM, bool IsBase, |
| 400 | SDValue Value) { |
| 401 | if (AM.isDynAlloc() && !AM.IncludesDynAlloc) { |
| 402 | changeComponent(AM, IsBase, Value); |
| 403 | AM.IncludesDynAlloc = true; |
| 404 | return true; |
| 405 | } |
| 406 | return false; |
| 407 | } |
| 408 | |
| 409 | // The base of AM is equivalent to Base + Index. Try to use Index as |
| 410 | // the index register. |
| 411 | static bool expandIndex(SystemZAddressingMode &AM, SDValue Base, |
| 412 | SDValue Index) { |
| 413 | if (AM.hasIndexField() && !AM.Index.getNode()) { |
| 414 | AM.Base = Base; |
| 415 | AM.Index = Index; |
| 416 | return true; |
| 417 | } |
| 418 | return false; |
| 419 | } |
| 420 | |
| 421 | // The base or index of AM is equivalent to Op0 + Op1, where IsBase selects |
| 422 | // between the base and index. Try to fold Op1 into AM's displacement. |
| 423 | static bool expandDisp(SystemZAddressingMode &AM, bool IsBase, |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 424 | SDValue Op0, uint64_t Op1) { |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 425 | // First try adjusting the displacement. |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 426 | int64_t TestDisp = AM.Disp + Op1; |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 427 | if (selectDisp(AM.DR, TestDisp)) { |
| 428 | changeComponent(AM, IsBase, Op0); |
| 429 | AM.Disp = TestDisp; |
| 430 | return true; |
| 431 | } |
| 432 | |
| 433 | // We could consider forcing the displacement into a register and |
| 434 | // using it as an index, but it would need to be carefully tuned. |
| 435 | return false; |
| 436 | } |
| 437 | |
| 438 | bool SystemZDAGToDAGISel::expandAddress(SystemZAddressingMode &AM, |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 439 | bool IsBase) const { |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 440 | SDValue N = IsBase ? AM.Base : AM.Index; |
| 441 | unsigned Opcode = N.getOpcode(); |
| 442 | if (Opcode == ISD::TRUNCATE) { |
| 443 | N = N.getOperand(0); |
| 444 | Opcode = N.getOpcode(); |
| 445 | } |
| 446 | if (Opcode == ISD::ADD || CurDAG->isBaseWithConstantOffset(N)) { |
| 447 | SDValue Op0 = N.getOperand(0); |
| 448 | SDValue Op1 = N.getOperand(1); |
| 449 | |
| 450 | unsigned Op0Code = Op0->getOpcode(); |
| 451 | unsigned Op1Code = Op1->getOpcode(); |
| 452 | |
| 453 | if (Op0Code == SystemZISD::ADJDYNALLOC) |
| 454 | return expandAdjDynAlloc(AM, IsBase, Op1); |
| 455 | if (Op1Code == SystemZISD::ADJDYNALLOC) |
| 456 | return expandAdjDynAlloc(AM, IsBase, Op0); |
| 457 | |
| 458 | if (Op0Code == ISD::Constant) |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 459 | return expandDisp(AM, IsBase, Op1, |
| 460 | cast<ConstantSDNode>(Op0)->getSExtValue()); |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 461 | if (Op1Code == ISD::Constant) |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 462 | return expandDisp(AM, IsBase, Op0, |
| 463 | cast<ConstantSDNode>(Op1)->getSExtValue()); |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 464 | |
| 465 | if (IsBase && expandIndex(AM, Op0, Op1)) |
| 466 | return true; |
| 467 | } |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 468 | if (Opcode == SystemZISD::PCREL_OFFSET) { |
| 469 | SDValue Full = N.getOperand(0); |
| 470 | SDValue Base = N.getOperand(1); |
| 471 | SDValue Anchor = Base.getOperand(0); |
| 472 | uint64_t Offset = (cast<GlobalAddressSDNode>(Full)->getOffset() - |
| 473 | cast<GlobalAddressSDNode>(Anchor)->getOffset()); |
| 474 | return expandDisp(AM, IsBase, Base, Offset); |
| 475 | } |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 476 | return false; |
| 477 | } |
| 478 | |
| 479 | // Return true if an instruction with displacement range DR should be |
| 480 | // used for displacement value Val. selectDisp(DR, Val) must already hold. |
| 481 | static bool isValidDisp(SystemZAddressingMode::DispRange DR, int64_t Val) { |
| 482 | assert(selectDisp(DR, Val) && "Invalid displacement"); |
| 483 | switch (DR) { |
| 484 | case SystemZAddressingMode::Disp12Only: |
| 485 | case SystemZAddressingMode::Disp20Only: |
| 486 | case SystemZAddressingMode::Disp20Only128: |
| 487 | return true; |
| 488 | |
| 489 | case SystemZAddressingMode::Disp12Pair: |
| 490 | // Use the other instruction if the displacement is too large. |
| 491 | return isUInt<12>(Val); |
| 492 | |
| 493 | case SystemZAddressingMode::Disp20Pair: |
| 494 | // Use the other instruction if the displacement is small enough. |
| 495 | return !isUInt<12>(Val); |
| 496 | } |
| 497 | llvm_unreachable("Unhandled displacement range"); |
| 498 | } |
| 499 | |
| 500 | // Return true if Base + Disp + Index should be performed by LA(Y). |
| 501 | static bool shouldUseLA(SDNode *Base, int64_t Disp, SDNode *Index) { |
| 502 | // Don't use LA(Y) for constants. |
| 503 | if (!Base) |
| 504 | return false; |
| 505 | |
| 506 | // Always use LA(Y) for frame addresses, since we know that the destination |
| 507 | // register is almost always (perhaps always) going to be different from |
| 508 | // the frame register. |
| 509 | if (Base->getOpcode() == ISD::FrameIndex) |
| 510 | return true; |
| 511 | |
| 512 | if (Disp) { |
| 513 | // Always use LA(Y) if there is a base, displacement and index. |
| 514 | if (Index) |
| 515 | return true; |
| 516 | |
| 517 | // Always use LA if the displacement is small enough. It should always |
| 518 | // be no worse than AGHI (and better if it avoids a move). |
| 519 | if (isUInt<12>(Disp)) |
| 520 | return true; |
| 521 | |
| 522 | // For similar reasons, always use LAY if the constant is too big for AGHI. |
| 523 | // LAY should be no worse than AGFI. |
| 524 | if (!isInt<16>(Disp)) |
| 525 | return true; |
| 526 | } else { |
| 527 | // Don't use LA for plain registers. |
| 528 | if (!Index) |
| 529 | return false; |
| 530 | |
| 531 | // Don't use LA for plain addition if the index operand is only used |
| 532 | // once. It should be a natural two-operand addition in that case. |
| 533 | if (Index->hasOneUse()) |
| 534 | return false; |
| 535 | |
| 536 | // Prefer addition if the second operation is sign-extended, in the |
| 537 | // hope of using AGF. |
| 538 | unsigned IndexOpcode = Index->getOpcode(); |
| 539 | if (IndexOpcode == ISD::SIGN_EXTEND || |
| 540 | IndexOpcode == ISD::SIGN_EXTEND_INREG) |
| 541 | return false; |
| 542 | } |
| 543 | |
| 544 | // Don't use LA for two-operand addition if either operand is only |
| 545 | // used once. The addition instructions are better in that case. |
| 546 | if (Base->hasOneUse()) |
| 547 | return false; |
| 548 | |
| 549 | return true; |
| 550 | } |
| 551 | |
| 552 | // Return true if Addr is suitable for AM, updating AM if so. |
| 553 | bool SystemZDAGToDAGISel::selectAddress(SDValue Addr, |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 554 | SystemZAddressingMode &AM) const { |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 555 | // Start out assuming that the address will need to be loaded separately, |
| 556 | // then try to extend it as much as we can. |
| 557 | AM.Base = Addr; |
| 558 | |
| 559 | // First try treating the address as a constant. |
| 560 | if (Addr.getOpcode() == ISD::Constant && |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 561 | expandDisp(AM, true, SDValue(), |
| 562 | cast<ConstantSDNode>(Addr)->getSExtValue())) |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 563 | ; |
| Marcin Koscielnicki | 9de88d9 | 2016-05-04 23:31:26 +0000 | [diff] [blame] | 564 | // Also see if it's a bare ADJDYNALLOC. |
| 565 | else if (Addr.getOpcode() == SystemZISD::ADJDYNALLOC && |
| 566 | expandAdjDynAlloc(AM, true, SDValue())) |
| 567 | ; |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 568 | else |
| 569 | // Otherwise try expanding each component. |
| 570 | while (expandAddress(AM, true) || |
| 571 | (AM.Index.getNode() && expandAddress(AM, false))) |
| 572 | continue; |
| 573 | |
| 574 | // Reject cases where it isn't profitable to use LA(Y). |
| 575 | if (AM.Form == SystemZAddressingMode::FormBDXLA && |
| 576 | !shouldUseLA(AM.Base.getNode(), AM.Disp, AM.Index.getNode())) |
| 577 | return false; |
| 578 | |
| 579 | // Reject cases where the other instruction in a pair should be used. |
| 580 | if (!isValidDisp(AM.DR, AM.Disp)) |
| 581 | return false; |
| 582 | |
| 583 | // Make sure that ADJDYNALLOC is included where necessary. |
| 584 | if (AM.isDynAlloc() && !AM.IncludesDynAlloc) |
| 585 | return false; |
| 586 | |
| 587 | DEBUG(AM.dump()); |
| 588 | return true; |
| 589 | } |
| 590 | |
| 591 | // Insert a node into the DAG at least before Pos. This will reposition |
| 592 | // the node as needed, and will assign it a node ID that is <= Pos's ID. |
| 593 | // Note that this does *not* preserve the uniqueness of node IDs! |
| 594 | // The selection DAG must no longer depend on their uniqueness when this |
| 595 | // function is used. |
| 596 | static void insertDAGNode(SelectionDAG *DAG, SDNode *Pos, SDValue N) { |
| Nirav Dave | 8c5f47a | 2018-03-22 19:32:07 +0000 | [diff] [blame] | 597 | if (N->getNodeId() == -1 || |
| 598 | (SelectionDAGISel::getUninvalidatedNodeId(N.getNode()) > |
| 599 | SelectionDAGISel::getUninvalidatedNodeId(Pos))) { |
| Duncan P. N. Exon Smith | a2c90e4 | 2015-10-20 01:12:46 +0000 | [diff] [blame] | 600 | DAG->RepositionNode(Pos->getIterator(), N.getNode()); |
| Nirav Dave | 3264c1b | 2018-03-19 20:19:46 +0000 | [diff] [blame] | 601 | // Mark Node as invalid for pruning as after this it may be a successor to a |
| 602 | // selected node but otherwise be in the same position of Pos. |
| 603 | // Conservatively mark it with the same -abs(Id) to assure node id |
| 604 | // invariant is preserved. |
| Nirav Dave | 8c5f47a | 2018-03-22 19:32:07 +0000 | [diff] [blame] | 605 | N->setNodeId(Pos->getNodeId()); |
| 606 | SelectionDAGISel::InvalidateNodeId(N.getNode()); |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | |
| 610 | void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM, |
| 611 | EVT VT, SDValue &Base, |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 612 | SDValue &Disp) const { |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 613 | Base = AM.Base; |
| 614 | if (!Base.getNode()) |
| 615 | // Register 0 means "no base". This is mostly useful for shifts. |
| 616 | Base = CurDAG->getRegister(0, VT); |
| 617 | else if (Base.getOpcode() == ISD::FrameIndex) { |
| 618 | // Lower a FrameIndex to a TargetFrameIndex. |
| 619 | int64_t FrameIndex = cast<FrameIndexSDNode>(Base)->getIndex(); |
| 620 | Base = CurDAG->getTargetFrameIndex(FrameIndex, VT); |
| 621 | } else if (Base.getValueType() != VT) { |
| 622 | // Truncate values from i64 to i32, for shifts. |
| 623 | assert(VT == MVT::i32 && Base.getValueType() == MVT::i64 && |
| 624 | "Unexpected truncation"); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 625 | SDLoc DL(Base); |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 626 | SDValue Trunc = CurDAG->getNode(ISD::TRUNCATE, DL, VT, Base); |
| 627 | insertDAGNode(CurDAG, Base.getNode(), Trunc); |
| 628 | Base = Trunc; |
| 629 | } |
| 630 | |
| 631 | // Lower the displacement to a TargetConstant. |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 632 | Disp = CurDAG->getTargetConstant(AM.Disp, SDLoc(Base), VT); |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM, |
| 636 | EVT VT, SDValue &Base, |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 637 | SDValue &Disp, |
| 638 | SDValue &Index) const { |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 639 | getAddressOperands(AM, VT, Base, Disp); |
| 640 | |
| 641 | Index = AM.Index; |
| 642 | if (!Index.getNode()) |
| 643 | // Register 0 means "no index". |
| 644 | Index = CurDAG->getRegister(0, VT); |
| 645 | } |
| 646 | |
| 647 | bool SystemZDAGToDAGISel::selectBDAddr(SystemZAddressingMode::DispRange DR, |
| 648 | SDValue Addr, SDValue &Base, |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 649 | SDValue &Disp) const { |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 650 | SystemZAddressingMode AM(SystemZAddressingMode::FormBD, DR); |
| 651 | if (!selectAddress(Addr, AM)) |
| 652 | return false; |
| 653 | |
| 654 | getAddressOperands(AM, Addr.getValueType(), Base, Disp); |
| 655 | return true; |
| 656 | } |
| 657 | |
| Richard Sandiford | a481f58 | 2013-08-23 11:18:53 +0000 | [diff] [blame] | 658 | bool SystemZDAGToDAGISel::selectMVIAddr(SystemZAddressingMode::DispRange DR, |
| 659 | SDValue Addr, SDValue &Base, |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 660 | SDValue &Disp) const { |
| Richard Sandiford | a481f58 | 2013-08-23 11:18:53 +0000 | [diff] [blame] | 661 | SystemZAddressingMode AM(SystemZAddressingMode::FormBDXNormal, DR); |
| 662 | if (!selectAddress(Addr, AM) || AM.Index.getNode()) |
| 663 | return false; |
| 664 | |
| 665 | getAddressOperands(AM, Addr.getValueType(), Base, Disp); |
| 666 | return true; |
| 667 | } |
| 668 | |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 669 | bool SystemZDAGToDAGISel::selectBDXAddr(SystemZAddressingMode::AddrForm Form, |
| 670 | SystemZAddressingMode::DispRange DR, |
| 671 | SDValue Addr, SDValue &Base, |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 672 | SDValue &Disp, SDValue &Index) const { |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 673 | SystemZAddressingMode AM(Form, DR); |
| 674 | if (!selectAddress(Addr, AM)) |
| 675 | return false; |
| 676 | |
| 677 | getAddressOperands(AM, Addr.getValueType(), Base, Disp, Index); |
| 678 | return true; |
| 679 | } |
| 680 | |
| Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 681 | bool SystemZDAGToDAGISel::selectBDVAddr12Only(SDValue Addr, SDValue Elem, |
| 682 | SDValue &Base, |
| 683 | SDValue &Disp, |
| 684 | SDValue &Index) const { |
| 685 | SDValue Regs[2]; |
| 686 | if (selectBDXAddr12Only(Addr, Regs[0], Disp, Regs[1]) && |
| 687 | Regs[0].getNode() && Regs[1].getNode()) { |
| 688 | for (unsigned int I = 0; I < 2; ++I) { |
| 689 | Base = Regs[I]; |
| 690 | Index = Regs[1 - I]; |
| 691 | // We can't tell here whether the index vector has the right type |
| 692 | // for the access; the caller needs to do that instead. |
| 693 | if (Index.getOpcode() == ISD::ZERO_EXTEND) |
| 694 | Index = Index.getOperand(0); |
| 695 | if (Index.getOpcode() == ISD::EXTRACT_VECTOR_ELT && |
| 696 | Index.getOperand(1) == Elem) { |
| 697 | Index = Index.getOperand(0); |
| 698 | return true; |
| 699 | } |
| 700 | } |
| 701 | } |
| 702 | return false; |
| 703 | } |
| 704 | |
| Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 705 | bool SystemZDAGToDAGISel::detectOrAndInsertion(SDValue &Op, |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 706 | uint64_t InsertMask) const { |
| Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 707 | // We're only interested in cases where the insertion is into some operand |
| 708 | // of Op, rather than into Op itself. The only useful case is an AND. |
| 709 | if (Op.getOpcode() != ISD::AND) |
| 710 | return false; |
| 711 | |
| 712 | // We need a constant mask. |
| Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 713 | auto *MaskNode = dyn_cast<ConstantSDNode>(Op.getOperand(1).getNode()); |
| Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 714 | if (!MaskNode) |
| 715 | return false; |
| 716 | |
| 717 | // It's not an insertion of Op.getOperand(0) if the two masks overlap. |
| 718 | uint64_t AndMask = MaskNode->getZExtValue(); |
| 719 | if (InsertMask & AndMask) |
| 720 | return false; |
| 721 | |
| 722 | // It's only an insertion if all bits are covered or are known to be zero. |
| 723 | // The inner check covers all cases but is more expensive. |
| Sanjay Patel | b1f0a0f | 2016-09-14 16:05:51 +0000 | [diff] [blame] | 724 | uint64_t Used = allOnes(Op.getValueSizeInBits()); |
| Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 725 | if (Used != (AndMask | InsertMask)) { |
| Craig Topper | d0af7e8 | 2017-04-28 05:31:46 +0000 | [diff] [blame] | 726 | KnownBits Known; |
| 727 | CurDAG->computeKnownBits(Op.getOperand(0), Known); |
| 728 | if (Used != (AndMask | InsertMask | Known.Zero.getZExtValue())) |
| Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 729 | return false; |
| 730 | } |
| 731 | |
| 732 | Op = Op.getOperand(0); |
| 733 | return true; |
| 734 | } |
| 735 | |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 736 | bool SystemZDAGToDAGISel::refineRxSBGMask(RxSBGOperands &RxSBG, |
| 737 | uint64_t Mask) const { |
| Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 738 | const SystemZInstrInfo *TII = getInstrInfo(); |
| Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 739 | if (RxSBG.Rotate != 0) |
| 740 | Mask = (Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate)); |
| 741 | Mask &= RxSBG.Mask; |
| Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 742 | if (TII->isRxSBGMask(Mask, RxSBG.BitSize, RxSBG.Start, RxSBG.End)) { |
| Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 743 | RxSBG.Mask = Mask; |
| Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 744 | return true; |
| 745 | } |
| Richard Sandiford | 84f54a3 | 2013-07-11 08:59:12 +0000 | [diff] [blame] | 746 | return false; |
| 747 | } |
| 748 | |
| Richard Sandiford | dd7dd93 | 2013-11-26 10:53:16 +0000 | [diff] [blame] | 749 | // Return true if any bits of (RxSBG.Input & Mask) are significant. |
| 750 | static bool maskMatters(RxSBGOperands &RxSBG, uint64_t Mask) { |
| 751 | // Rotate the mask in the same way as RxSBG.Input is rotated. |
| Richard Sandiford | 297f7d2 | 2013-07-18 10:14:55 +0000 | [diff] [blame] | 752 | if (RxSBG.Rotate != 0) |
| Richard Sandiford | dd7dd93 | 2013-11-26 10:53:16 +0000 | [diff] [blame] | 753 | Mask = ((Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate))); |
| 754 | return (Mask & RxSBG.Mask) != 0; |
| Richard Sandiford | 297f7d2 | 2013-07-18 10:14:55 +0000 | [diff] [blame] | 755 | } |
| 756 | |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 757 | bool SystemZDAGToDAGISel::expandRxSBG(RxSBGOperands &RxSBG) const { |
| Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 758 | SDValue N = RxSBG.Input; |
| Richard Sandiford | 297f7d2 | 2013-07-18 10:14:55 +0000 | [diff] [blame] | 759 | unsigned Opcode = N.getOpcode(); |
| 760 | switch (Opcode) { |
| Zhan Jun Liau | 0df3505 | 2016-06-22 16:16:27 +0000 | [diff] [blame] | 761 | case ISD::TRUNCATE: { |
| 762 | if (RxSBG.Opcode == SystemZ::RNSBG) |
| 763 | return false; |
| Sanjay Patel | b1f0a0f | 2016-09-14 16:05:51 +0000 | [diff] [blame] | 764 | uint64_t BitSize = N.getValueSizeInBits(); |
| Zhan Jun Liau | 0df3505 | 2016-06-22 16:16:27 +0000 | [diff] [blame] | 765 | uint64_t Mask = allOnes(BitSize); |
| 766 | if (!refineRxSBGMask(RxSBG, Mask)) |
| 767 | return false; |
| 768 | RxSBG.Input = N.getOperand(0); |
| 769 | return true; |
| 770 | } |
| Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 771 | case ISD::AND: { |
| Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 772 | if (RxSBG.Opcode == SystemZ::RNSBG) |
| 773 | return false; |
| 774 | |
| Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 775 | auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode()); |
| Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 776 | if (!MaskNode) |
| 777 | return false; |
| 778 | |
| 779 | SDValue Input = N.getOperand(0); |
| 780 | uint64_t Mask = MaskNode->getZExtValue(); |
| Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 781 | if (!refineRxSBGMask(RxSBG, Mask)) { |
| Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 782 | // If some bits of Input are already known zeros, those bits will have |
| 783 | // been removed from the mask. See if adding them back in makes the |
| 784 | // mask suitable. |
| Craig Topper | d0af7e8 | 2017-04-28 05:31:46 +0000 | [diff] [blame] | 785 | KnownBits Known; |
| 786 | CurDAG->computeKnownBits(Input, Known); |
| 787 | Mask |= Known.Zero.getZExtValue(); |
| Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 788 | if (!refineRxSBGMask(RxSBG, Mask)) |
| Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 789 | return false; |
| 790 | } |
| Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 791 | RxSBG.Input = Input; |
| Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 792 | return true; |
| 793 | } |
| 794 | |
| Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 795 | case ISD::OR: { |
| 796 | if (RxSBG.Opcode != SystemZ::RNSBG) |
| 797 | return false; |
| 798 | |
| Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 799 | auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode()); |
| Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 800 | if (!MaskNode) |
| 801 | return false; |
| 802 | |
| 803 | SDValue Input = N.getOperand(0); |
| 804 | uint64_t Mask = ~MaskNode->getZExtValue(); |
| 805 | if (!refineRxSBGMask(RxSBG, Mask)) { |
| 806 | // If some bits of Input are already known ones, those bits will have |
| 807 | // been removed from the mask. See if adding them back in makes the |
| 808 | // mask suitable. |
| Craig Topper | d0af7e8 | 2017-04-28 05:31:46 +0000 | [diff] [blame] | 809 | KnownBits Known; |
| 810 | CurDAG->computeKnownBits(Input, Known); |
| 811 | Mask &= ~Known.One.getZExtValue(); |
| Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 812 | if (!refineRxSBGMask(RxSBG, Mask)) |
| 813 | return false; |
| 814 | } |
| 815 | RxSBG.Input = Input; |
| 816 | return true; |
| 817 | } |
| 818 | |
| Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 819 | case ISD::ROTL: { |
| Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 820 | // Any 64-bit rotate left can be merged into the RxSBG. |
| Richard Sandiford | 3e38297 | 2013-10-16 13:35:13 +0000 | [diff] [blame] | 821 | if (RxSBG.BitSize != 64 || N.getValueType() != MVT::i64) |
| Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 822 | return false; |
| Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 823 | auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode()); |
| Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 824 | if (!CountNode) |
| 825 | return false; |
| 826 | |
| Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 827 | RxSBG.Rotate = (RxSBG.Rotate + CountNode->getZExtValue()) & 63; |
| 828 | RxSBG.Input = N.getOperand(0); |
| Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 829 | return true; |
| 830 | } |
| Simon Pilgrim | 0750c84 | 2015-08-15 13:27:30 +0000 | [diff] [blame] | 831 | |
| Richard Sandiford | 220ee49 | 2013-12-20 11:49:48 +0000 | [diff] [blame] | 832 | case ISD::ANY_EXTEND: |
| 833 | // Bits above the extended operand are don't-care. |
| 834 | RxSBG.Input = N.getOperand(0); |
| 835 | return true; |
| 836 | |
| Richard Sandiford | 3875cb6 | 2014-01-09 11:28:53 +0000 | [diff] [blame] | 837 | case ISD::ZERO_EXTEND: |
| 838 | if (RxSBG.Opcode != SystemZ::RNSBG) { |
| 839 | // Restrict the mask to the extended operand. |
| Sanjay Patel | b1f0a0f | 2016-09-14 16:05:51 +0000 | [diff] [blame] | 840 | unsigned InnerBitSize = N.getOperand(0).getValueSizeInBits(); |
| Richard Sandiford | 3875cb6 | 2014-01-09 11:28:53 +0000 | [diff] [blame] | 841 | if (!refineRxSBGMask(RxSBG, allOnes(InnerBitSize))) |
| 842 | return false; |
| Richard Sandiford | 220ee49 | 2013-12-20 11:49:48 +0000 | [diff] [blame] | 843 | |
| Richard Sandiford | 3875cb6 | 2014-01-09 11:28:53 +0000 | [diff] [blame] | 844 | RxSBG.Input = N.getOperand(0); |
| 845 | return true; |
| 846 | } |
| Justin Bogner | cd1d5aa | 2016-08-17 20:30:52 +0000 | [diff] [blame] | 847 | LLVM_FALLTHROUGH; |
| Simon Pilgrim | 0750c84 | 2015-08-15 13:27:30 +0000 | [diff] [blame] | 848 | |
| Richard Sandiford | 220ee49 | 2013-12-20 11:49:48 +0000 | [diff] [blame] | 849 | case ISD::SIGN_EXTEND: { |
| Richard Sandiford | 3e38297 | 2013-10-16 13:35:13 +0000 | [diff] [blame] | 850 | // Check that the extension bits are don't-care (i.e. are masked out |
| 851 | // by the final mask). |
| Jonas Paulsson | 19380ba | 2017-12-06 13:53:24 +0000 | [diff] [blame] | 852 | unsigned BitSize = N.getValueSizeInBits(); |
| Sanjay Patel | b1f0a0f | 2016-09-14 16:05:51 +0000 | [diff] [blame] | 853 | unsigned InnerBitSize = N.getOperand(0).getValueSizeInBits(); |
| Jonas Paulsson | 19380ba | 2017-12-06 13:53:24 +0000 | [diff] [blame] | 854 | if (maskMatters(RxSBG, allOnes(BitSize) - allOnes(InnerBitSize))) { |
| 855 | // In the case where only the sign bit is active, increase Rotate with |
| 856 | // the extension width. |
| 857 | if (RxSBG.Mask == 1 && RxSBG.Rotate == 1) |
| 858 | RxSBG.Rotate += (BitSize - InnerBitSize); |
| 859 | else |
| 860 | return false; |
| 861 | } |
| Richard Sandiford | 3e38297 | 2013-10-16 13:35:13 +0000 | [diff] [blame] | 862 | |
| 863 | RxSBG.Input = N.getOperand(0); |
| 864 | return true; |
| 865 | } |
| 866 | |
| Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 867 | case ISD::SHL: { |
| Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 868 | auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode()); |
| Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 869 | if (!CountNode) |
| 870 | return false; |
| 871 | |
| 872 | uint64_t Count = CountNode->getZExtValue(); |
| Sanjay Patel | b1f0a0f | 2016-09-14 16:05:51 +0000 | [diff] [blame] | 873 | unsigned BitSize = N.getValueSizeInBits(); |
| Richard Sandiford | 3e38297 | 2013-10-16 13:35:13 +0000 | [diff] [blame] | 874 | if (Count < 1 || Count >= BitSize) |
| Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 875 | return false; |
| 876 | |
| Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 877 | if (RxSBG.Opcode == SystemZ::RNSBG) { |
| 878 | // Treat (shl X, count) as (rotl X, size-count) as long as the bottom |
| 879 | // count bits from RxSBG.Input are ignored. |
| Richard Sandiford | dd7dd93 | 2013-11-26 10:53:16 +0000 | [diff] [blame] | 880 | if (maskMatters(RxSBG, allOnes(Count))) |
| Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 881 | return false; |
| 882 | } else { |
| 883 | // Treat (shl X, count) as (and (rotl X, count), ~0<<count). |
| Richard Sandiford | 3e38297 | 2013-10-16 13:35:13 +0000 | [diff] [blame] | 884 | if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count) << Count)) |
| Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 885 | return false; |
| 886 | } |
| 887 | |
| Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 888 | RxSBG.Rotate = (RxSBG.Rotate + Count) & 63; |
| 889 | RxSBG.Input = N.getOperand(0); |
| Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 890 | return true; |
| 891 | } |
| 892 | |
| Richard Sandiford | 297f7d2 | 2013-07-18 10:14:55 +0000 | [diff] [blame] | 893 | case ISD::SRL: |
| 894 | case ISD::SRA: { |
| Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 895 | auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode()); |
| Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 896 | if (!CountNode) |
| 897 | return false; |
| 898 | |
| 899 | uint64_t Count = CountNode->getZExtValue(); |
| Sanjay Patel | b1f0a0f | 2016-09-14 16:05:51 +0000 | [diff] [blame] | 900 | unsigned BitSize = N.getValueSizeInBits(); |
| Richard Sandiford | 3e38297 | 2013-10-16 13:35:13 +0000 | [diff] [blame] | 901 | if (Count < 1 || Count >= BitSize) |
| Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 902 | return false; |
| 903 | |
| Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 904 | if (RxSBG.Opcode == SystemZ::RNSBG || Opcode == ISD::SRA) { |
| 905 | // Treat (srl|sra X, count) as (rotl X, size-count) as long as the top |
| 906 | // count bits from RxSBG.Input are ignored. |
| Richard Sandiford | dd7dd93 | 2013-11-26 10:53:16 +0000 | [diff] [blame] | 907 | if (maskMatters(RxSBG, allOnes(Count) << (BitSize - Count))) |
| Richard Sandiford | 297f7d2 | 2013-07-18 10:14:55 +0000 | [diff] [blame] | 908 | return false; |
| 909 | } else { |
| 910 | // Treat (srl X, count), mask) as (and (rotl X, size-count), ~0>>count), |
| 911 | // which is similar to SLL above. |
| Richard Sandiford | 3e38297 | 2013-10-16 13:35:13 +0000 | [diff] [blame] | 912 | if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count))) |
| Richard Sandiford | 297f7d2 | 2013-07-18 10:14:55 +0000 | [diff] [blame] | 913 | return false; |
| 914 | } |
| 915 | |
| Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 916 | RxSBG.Rotate = (RxSBG.Rotate - Count) & 63; |
| 917 | RxSBG.Input = N.getOperand(0); |
| Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 918 | return true; |
| 919 | } |
| Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 920 | default: |
| 921 | return false; |
| 922 | } |
| 923 | } |
| 924 | |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 925 | SDValue SystemZDAGToDAGISel::getUNDEF(const SDLoc &DL, EVT VT) const { |
| Richard Sandiford | 3ad5a15 | 2013-10-01 14:36:20 +0000 | [diff] [blame] | 926 | SDNode *N = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, VT); |
| Richard Sandiford | 84f54a3 | 2013-07-11 08:59:12 +0000 | [diff] [blame] | 927 | return SDValue(N, 0); |
| 928 | } |
| 929 | |
| Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 930 | SDValue SystemZDAGToDAGISel::convertTo(const SDLoc &DL, EVT VT, |
| 931 | SDValue N) const { |
| Richard Sandiford | d816320 | 2013-09-13 09:12:44 +0000 | [diff] [blame] | 932 | if (N.getValueType() == MVT::i32 && VT == MVT::i64) |
| Richard Sandiford | 87a4436 | 2013-09-30 10:28:35 +0000 | [diff] [blame] | 933 | return CurDAG->getTargetInsertSubreg(SystemZ::subreg_l32, |
| Richard Sandiford | 3ad5a15 | 2013-10-01 14:36:20 +0000 | [diff] [blame] | 934 | DL, VT, getUNDEF(DL, MVT::i64), N); |
| Richard Sandiford | d816320 | 2013-09-13 09:12:44 +0000 | [diff] [blame] | 935 | if (N.getValueType() == MVT::i64 && VT == MVT::i32) |
| Richard Sandiford | 87a4436 | 2013-09-30 10:28:35 +0000 | [diff] [blame] | 936 | return CurDAG->getTargetExtractSubreg(SystemZ::subreg_l32, DL, VT, N); |
| Richard Sandiford | 84f54a3 | 2013-07-11 08:59:12 +0000 | [diff] [blame] | 937 | assert(N.getValueType() == VT && "Unexpected value types"); |
| 938 | return N; |
| 939 | } |
| 940 | |
| Justin Bogner | bbcd223 | 2016-05-10 21:11:26 +0000 | [diff] [blame] | 941 | bool SystemZDAGToDAGISel::tryRISBGZero(SDNode *N) { |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 942 | SDLoc DL(N); |
| Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 943 | EVT VT = N->getValueType(0); |
| Ulrich Weigand | 77884bc | 2015-06-25 11:52:36 +0000 | [diff] [blame] | 944 | if (!VT.isInteger() || VT.getSizeInBits() > 64) |
| Justin Bogner | bbcd223 | 2016-05-10 21:11:26 +0000 | [diff] [blame] | 945 | return false; |
| Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 946 | RxSBGOperands RISBG(SystemZ::RISBG, SDValue(N, 0)); |
| Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 947 | unsigned Count = 0; |
| Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 948 | while (expandRxSBG(RISBG)) |
| Zhan Jun Liau | 0df3505 | 2016-06-22 16:16:27 +0000 | [diff] [blame] | 949 | // The widening or narrowing is expected to be free. |
| 950 | // Counting widening or narrowing as a saved operation will result in |
| 951 | // preferring an R*SBG over a simple shift/logical instruction. |
| 952 | if (RISBG.Input.getOpcode() != ISD::ANY_EXTEND && |
| 953 | RISBG.Input.getOpcode() != ISD::TRUNCATE) |
| Richard Sandiford | 3e38297 | 2013-10-16 13:35:13 +0000 | [diff] [blame] | 954 | Count += 1; |
| Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 955 | if (Count == 0) |
| Justin Bogner | bbcd223 | 2016-05-10 21:11:26 +0000 | [diff] [blame] | 956 | return false; |
| Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 957 | |
| Ulrich Weigand | 5dc7b67 | 2016-11-11 12:43:51 +0000 | [diff] [blame] | 958 | // Prefer to use normal shift instructions over RISBG, since they can handle |
| 959 | // all cases and are sometimes shorter. |
| 960 | if (Count == 1 && N->getOpcode() != ISD::AND) |
| 961 | return false; |
| 962 | |
| 963 | // Prefer register extensions like LLC over RISBG. Also prefer to start |
| 964 | // out with normal ANDs if one instruction would be enough. We can convert |
| 965 | // these ANDs into an RISBG later if a three-address instruction is useful. |
| 966 | if (RISBG.Rotate == 0) { |
| 967 | bool PreferAnd = false; |
| 968 | // Prefer AND for any 32-bit and-immediate operation. |
| 969 | if (VT == MVT::i32) |
| 970 | PreferAnd = true; |
| 971 | // As well as for any 64-bit operation that can be implemented via LLC(R), |
| 972 | // LLH(R), LLGT(R), or one of the and-immediate instructions. |
| 973 | else if (RISBG.Mask == 0xff || |
| 974 | RISBG.Mask == 0xffff || |
| 975 | RISBG.Mask == 0x7fffffff || |
| 976 | SystemZ::isImmLF(~RISBG.Mask) || |
| 977 | SystemZ::isImmHF(~RISBG.Mask)) |
| 978 | PreferAnd = true; |
| Ulrich Weigand | 92c2c67 | 2016-11-11 12:46:28 +0000 | [diff] [blame] | 979 | // And likewise for the LLZRGF instruction, which doesn't have a register |
| 980 | // to register version. |
| 981 | else if (auto *Load = dyn_cast<LoadSDNode>(RISBG.Input)) { |
| 982 | if (Load->getMemoryVT() == MVT::i32 && |
| 983 | (Load->getExtensionType() == ISD::EXTLOAD || |
| 984 | Load->getExtensionType() == ISD::ZEXTLOAD) && |
| 985 | RISBG.Mask == 0xffffff00 && |
| 986 | Subtarget->hasLoadAndZeroRightmostByte()) |
| 987 | PreferAnd = true; |
| 988 | } |
| Ulrich Weigand | 5dc7b67 | 2016-11-11 12:43:51 +0000 | [diff] [blame] | 989 | if (PreferAnd) { |
| 990 | // Replace the current node with an AND. Note that the current node |
| 991 | // might already be that same AND, in which case it is already CSE'd |
| 992 | // with it, and we must not call ReplaceNode. |
| 993 | SDValue In = convertTo(DL, VT, RISBG.Input); |
| 994 | SDValue Mask = CurDAG->getConstant(RISBG.Mask, DL, VT); |
| 995 | SDValue New = CurDAG->getNode(ISD::AND, DL, VT, In, Mask); |
| 996 | if (N != New.getNode()) { |
| 997 | insertDAGNode(CurDAG, N, Mask); |
| 998 | insertDAGNode(CurDAG, N, New); |
| 999 | ReplaceNode(N, New.getNode()); |
| 1000 | N = New.getNode(); |
| Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 1001 | } |
| Ulrich Weigand | 5dc7b67 | 2016-11-11 12:43:51 +0000 | [diff] [blame] | 1002 | // Now, select the machine opcode to implement this operation. |
| Jonas Paulsson | f268cd0 | 2018-02-27 07:53:23 +0000 | [diff] [blame] | 1003 | if (!N->isMachineOpcode()) |
| 1004 | SelectCode(N); |
| Ulrich Weigand | 5dc7b67 | 2016-11-11 12:43:51 +0000 | [diff] [blame] | 1005 | return true; |
| Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 1006 | } |
| Simon Pilgrim | 0750c84 | 2015-08-15 13:27:30 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
| Richard Sandiford | 3ad5a15 | 2013-10-01 14:36:20 +0000 | [diff] [blame] | 1009 | unsigned Opcode = SystemZ::RISBG; |
| Ulrich Weigand | 371d10a | 2015-03-31 12:58:17 +0000 | [diff] [blame] | 1010 | // Prefer RISBGN if available, since it does not clobber CC. |
| 1011 | if (Subtarget->hasMiscellaneousExtensions()) |
| 1012 | Opcode = SystemZ::RISBGN; |
| Richard Sandiford | 3ad5a15 | 2013-10-01 14:36:20 +0000 | [diff] [blame] | 1013 | EVT OpcodeVT = MVT::i64; |
| Ulrich Weigand | 55b8590 | 2017-11-14 19:20:46 +0000 | [diff] [blame] | 1014 | if (VT == MVT::i32 && Subtarget->hasHighWord() && |
| 1015 | // We can only use the 32-bit instructions if all source bits are |
| 1016 | // in the low 32 bits without wrapping, both after rotation (because |
| 1017 | // of the smaller range for Start and End) and before rotation |
| 1018 | // (because the input value is truncated). |
| 1019 | RISBG.Start >= 32 && RISBG.End >= RISBG.Start && |
| 1020 | ((RISBG.Start + RISBG.Rotate) & 63) >= 32 && |
| 1021 | ((RISBG.End + RISBG.Rotate) & 63) >= |
| 1022 | ((RISBG.Start + RISBG.Rotate) & 63)) { |
| Richard Sandiford | 3ad5a15 | 2013-10-01 14:36:20 +0000 | [diff] [blame] | 1023 | Opcode = SystemZ::RISBMux; |
| 1024 | OpcodeVT = MVT::i32; |
| 1025 | RISBG.Start &= 31; |
| 1026 | RISBG.End &= 31; |
| 1027 | } |
| Richard Sandiford | 84f54a3 | 2013-07-11 08:59:12 +0000 | [diff] [blame] | 1028 | SDValue Ops[5] = { |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1029 | getUNDEF(DL, OpcodeVT), |
| 1030 | convertTo(DL, OpcodeVT, RISBG.Input), |
| 1031 | CurDAG->getTargetConstant(RISBG.Start, DL, MVT::i32), |
| 1032 | CurDAG->getTargetConstant(RISBG.End | 128, DL, MVT::i32), |
| 1033 | CurDAG->getTargetConstant(RISBG.Rotate, DL, MVT::i32) |
| Richard Sandiford | 84f54a3 | 2013-07-11 08:59:12 +0000 | [diff] [blame] | 1034 | }; |
| Justin Bogner | bbcd223 | 2016-05-10 21:11:26 +0000 | [diff] [blame] | 1035 | SDValue New = convertTo( |
| 1036 | DL, VT, SDValue(CurDAG->getMachineNode(Opcode, DL, OpcodeVT, Ops), 0)); |
| Nirav Dave | 3264c1b | 2018-03-19 20:19:46 +0000 | [diff] [blame] | 1037 | ReplaceNode(N, New.getNode()); |
| Justin Bogner | bbcd223 | 2016-05-10 21:11:26 +0000 | [diff] [blame] | 1038 | return true; |
| Richard Sandiford | 84f54a3 | 2013-07-11 08:59:12 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1041 | bool SystemZDAGToDAGISel::tryRxSBG(SDNode *N, unsigned Opcode) { |
| Ulrich Weigand | 77884bc | 2015-06-25 11:52:36 +0000 | [diff] [blame] | 1042 | SDLoc DL(N); |
| 1043 | EVT VT = N->getValueType(0); |
| 1044 | if (!VT.isInteger() || VT.getSizeInBits() > 64) |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1045 | return false; |
| Richard Sandiford | 7878b85 | 2013-07-18 10:06:15 +0000 | [diff] [blame] | 1046 | // Try treating each operand of N as the second operand of the RxSBG |
| Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 1047 | // and see which goes deepest. |
| Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 1048 | RxSBGOperands RxSBG[] = { |
| 1049 | RxSBGOperands(Opcode, N->getOperand(0)), |
| 1050 | RxSBGOperands(Opcode, N->getOperand(1)) |
| 1051 | }; |
| Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 1052 | unsigned Count[] = { 0, 0 }; |
| 1053 | for (unsigned I = 0; I < 2; ++I) |
| Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 1054 | while (expandRxSBG(RxSBG[I])) |
| Zhan Jun Liau | 0df3505 | 2016-06-22 16:16:27 +0000 | [diff] [blame] | 1055 | // The widening or narrowing is expected to be free. |
| 1056 | // Counting widening or narrowing as a saved operation will result in |
| 1057 | // preferring an R*SBG over a simple shift/logical instruction. |
| 1058 | if (RxSBG[I].Input.getOpcode() != ISD::ANY_EXTEND && |
| 1059 | RxSBG[I].Input.getOpcode() != ISD::TRUNCATE) |
| Richard Sandiford | 3e38297 | 2013-10-16 13:35:13 +0000 | [diff] [blame] | 1060 | Count[I] += 1; |
| Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 1061 | |
| 1062 | // Do nothing if neither operand is suitable. |
| 1063 | if (Count[0] == 0 && Count[1] == 0) |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1064 | return false; |
| Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 1065 | |
| 1066 | // Pick the deepest second operand. |
| 1067 | unsigned I = Count[0] > Count[1] ? 0 : 1; |
| 1068 | SDValue Op0 = N->getOperand(I ^ 1); |
| 1069 | |
| 1070 | // Prefer IC for character insertions from memory. |
| Richard Sandiford | 7878b85 | 2013-07-18 10:06:15 +0000 | [diff] [blame] | 1071 | if (Opcode == SystemZ::ROSBG && (RxSBG[I].Mask & 0xff) == 0) |
| Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1072 | if (auto *Load = dyn_cast<LoadSDNode>(Op0.getNode())) |
| Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 1073 | if (Load->getMemoryVT() == MVT::i8) |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1074 | return false; |
| Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 1075 | |
| 1076 | // See whether we can avoid an AND in the first operand by converting |
| 1077 | // ROSBG to RISBG. |
| Ulrich Weigand | 371d10a | 2015-03-31 12:58:17 +0000 | [diff] [blame] | 1078 | if (Opcode == SystemZ::ROSBG && detectOrAndInsertion(Op0, RxSBG[I].Mask)) { |
| Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 1079 | Opcode = SystemZ::RISBG; |
| Ulrich Weigand | 371d10a | 2015-03-31 12:58:17 +0000 | [diff] [blame] | 1080 | // Prefer RISBGN if available, since it does not clobber CC. |
| 1081 | if (Subtarget->hasMiscellaneousExtensions()) |
| 1082 | Opcode = SystemZ::RISBGN; |
| 1083 | } |
| 1084 | |
| Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 1085 | SDValue Ops[5] = { |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1086 | convertTo(DL, MVT::i64, Op0), |
| 1087 | convertTo(DL, MVT::i64, RxSBG[I].Input), |
| 1088 | CurDAG->getTargetConstant(RxSBG[I].Start, DL, MVT::i32), |
| 1089 | CurDAG->getTargetConstant(RxSBG[I].End, DL, MVT::i32), |
| 1090 | CurDAG->getTargetConstant(RxSBG[I].Rotate, DL, MVT::i32) |
| Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 1091 | }; |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1092 | SDValue New = convertTo( |
| 1093 | DL, VT, SDValue(CurDAG->getMachineNode(Opcode, DL, MVT::i64, Ops), 0)); |
| 1094 | ReplaceNode(N, New.getNode()); |
| 1095 | return true; |
| Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
| Justin Bogner | ffb273d | 2016-05-09 23:54:23 +0000 | [diff] [blame] | 1098 | void SystemZDAGToDAGISel::splitLargeImmediate(unsigned Opcode, SDNode *Node, |
| 1099 | SDValue Op0, uint64_t UpperVal, |
| 1100 | uint64_t LowerVal) { |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1101 | EVT VT = Node->getValueType(0); |
| Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1102 | SDLoc DL(Node); |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1103 | SDValue Upper = CurDAG->getConstant(UpperVal, DL, VT); |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1104 | if (Op0.getNode()) |
| 1105 | Upper = CurDAG->getNode(Opcode, DL, VT, Op0, Upper); |
| Justin Bogner | ffb273d | 2016-05-09 23:54:23 +0000 | [diff] [blame] | 1106 | |
| 1107 | { |
| 1108 | // When we haven't passed in Op0, Upper will be a constant. In order to |
| 1109 | // prevent folding back to the large immediate in `Or = getNode(...)` we run |
| 1110 | // SelectCode first and end up with an opaque machine node. This means that |
| 1111 | // we need to use a handle to keep track of Upper in case it gets CSE'd by |
| 1112 | // SelectCode. |
| 1113 | // |
| 1114 | // Note that in the case where Op0 is passed in we could just call |
| 1115 | // SelectCode(Upper) later, along with the SelectCode(Or), and avoid needing |
| 1116 | // the handle at all, but it's fine to do it here. |
| 1117 | // |
| 1118 | // TODO: This is a pretty hacky way to do this. Can we do something that |
| 1119 | // doesn't require a two paragraph explanation? |
| 1120 | HandleSDNode Handle(Upper); |
| 1121 | SelectCode(Upper.getNode()); |
| 1122 | Upper = Handle.getValue(); |
| 1123 | } |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1124 | |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1125 | SDValue Lower = CurDAG->getConstant(LowerVal, DL, VT); |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1126 | SDValue Or = CurDAG->getNode(Opcode, DL, VT, Upper, Lower); |
| Justin Bogner | ffb273d | 2016-05-09 23:54:23 +0000 | [diff] [blame] | 1127 | |
| Nirav Dave | 3264c1b | 2018-03-19 20:19:46 +0000 | [diff] [blame] | 1128 | ReplaceNode(Node, Or.getNode()); |
| Justin Bogner | ffb273d | 2016-05-09 23:54:23 +0000 | [diff] [blame] | 1129 | |
| 1130 | SelectCode(Or.getNode()); |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1133 | bool SystemZDAGToDAGISel::tryGather(SDNode *N, unsigned Opcode) { |
| Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1134 | SDValue ElemV = N->getOperand(2); |
| 1135 | auto *ElemN = dyn_cast<ConstantSDNode>(ElemV); |
| 1136 | if (!ElemN) |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1137 | return false; |
| Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1138 | |
| 1139 | unsigned Elem = ElemN->getZExtValue(); |
| 1140 | EVT VT = N->getValueType(0); |
| 1141 | if (Elem >= VT.getVectorNumElements()) |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1142 | return false; |
| Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1143 | |
| 1144 | auto *Load = dyn_cast<LoadSDNode>(N->getOperand(1)); |
| 1145 | if (!Load || !Load->hasOneUse()) |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1146 | return false; |
| Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1147 | if (Load->getMemoryVT().getSizeInBits() != |
| 1148 | Load->getValueType(0).getSizeInBits()) |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1149 | return false; |
| Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1150 | |
| 1151 | SDValue Base, Disp, Index; |
| 1152 | if (!selectBDVAddr12Only(Load->getBasePtr(), ElemV, Base, Disp, Index) || |
| 1153 | Index.getValueType() != VT.changeVectorElementTypeToInteger()) |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1154 | return false; |
| Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1155 | |
| 1156 | SDLoc DL(Load); |
| 1157 | SDValue Ops[] = { |
| 1158 | N->getOperand(0), Base, Disp, Index, |
| 1159 | CurDAG->getTargetConstant(Elem, DL, MVT::i32), Load->getChain() |
| 1160 | }; |
| 1161 | SDNode *Res = CurDAG->getMachineNode(Opcode, DL, VT, MVT::Other, Ops); |
| 1162 | ReplaceUses(SDValue(Load, 1), SDValue(Res, 1)); |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1163 | ReplaceNode(N, Res); |
| 1164 | return true; |
| Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1167 | bool SystemZDAGToDAGISel::tryScatter(StoreSDNode *Store, unsigned Opcode) { |
| Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1168 | SDValue Value = Store->getValue(); |
| 1169 | if (Value.getOpcode() != ISD::EXTRACT_VECTOR_ELT) |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1170 | return false; |
| Sanjay Patel | b1f0a0f | 2016-09-14 16:05:51 +0000 | [diff] [blame] | 1171 | if (Store->getMemoryVT().getSizeInBits() != Value.getValueSizeInBits()) |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1172 | return false; |
| Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1173 | |
| 1174 | SDValue ElemV = Value.getOperand(1); |
| 1175 | auto *ElemN = dyn_cast<ConstantSDNode>(ElemV); |
| 1176 | if (!ElemN) |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1177 | return false; |
| Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1178 | |
| 1179 | SDValue Vec = Value.getOperand(0); |
| 1180 | EVT VT = Vec.getValueType(); |
| 1181 | unsigned Elem = ElemN->getZExtValue(); |
| 1182 | if (Elem >= VT.getVectorNumElements()) |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1183 | return false; |
| Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1184 | |
| 1185 | SDValue Base, Disp, Index; |
| 1186 | if (!selectBDVAddr12Only(Store->getBasePtr(), ElemV, Base, Disp, Index) || |
| 1187 | Index.getValueType() != VT.changeVectorElementTypeToInteger()) |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1188 | return false; |
| Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1189 | |
| 1190 | SDLoc DL(Store); |
| 1191 | SDValue Ops[] = { |
| 1192 | Vec, Base, Disp, Index, CurDAG->getTargetConstant(Elem, DL, MVT::i32), |
| 1193 | Store->getChain() |
| 1194 | }; |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1195 | ReplaceNode(Store, CurDAG->getMachineNode(Opcode, DL, MVT::Other, Ops)); |
| 1196 | return true; |
| Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
| Richard Sandiford | 067817e | 2013-09-27 15:29:20 +0000 | [diff] [blame] | 1199 | bool SystemZDAGToDAGISel::canUseBlockOperation(StoreSDNode *Store, |
| 1200 | LoadSDNode *Load) const { |
| Richard Sandiford | 178273a | 2013-09-05 10:36:45 +0000 | [diff] [blame] | 1201 | // Check that the two memory operands have the same size. |
| 1202 | if (Load->getMemoryVT() != Store->getMemoryVT()) |
| Richard Sandiford | 9784649 | 2013-07-09 09:46:39 +0000 | [diff] [blame] | 1203 | return false; |
| 1204 | |
| Richard Sandiford | 178273a | 2013-09-05 10:36:45 +0000 | [diff] [blame] | 1205 | // Volatility stops an access from being decomposed. |
| 1206 | if (Load->isVolatile() || Store->isVolatile()) |
| 1207 | return false; |
| Richard Sandiford | 9784649 | 2013-07-09 09:46:39 +0000 | [diff] [blame] | 1208 | |
| 1209 | // There's no chance of overlap if the load is invariant. |
| Justin Lebar | adbf09e | 2016-09-11 01:38:58 +0000 | [diff] [blame] | 1210 | if (Load->isInvariant() && Load->isDereferenceable()) |
| Richard Sandiford | 9784649 | 2013-07-09 09:46:39 +0000 | [diff] [blame] | 1211 | return true; |
| 1212 | |
| Richard Sandiford | 9784649 | 2013-07-09 09:46:39 +0000 | [diff] [blame] | 1213 | // Otherwise we need to check whether there's an alias. |
| Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 1214 | const Value *V1 = Load->getMemOperand()->getValue(); |
| 1215 | const Value *V2 = Store->getMemOperand()->getValue(); |
| Richard Sandiford | 9784649 | 2013-07-09 09:46:39 +0000 | [diff] [blame] | 1216 | if (!V1 || !V2) |
| 1217 | return false; |
| 1218 | |
| Richard Sandiford | 067817e | 2013-09-27 15:29:20 +0000 | [diff] [blame] | 1219 | // Reject equality. |
| 1220 | uint64_t Size = Load->getMemoryVT().getStoreSize(); |
| Richard Sandiford | 9784649 | 2013-07-09 09:46:39 +0000 | [diff] [blame] | 1221 | int64_t End1 = Load->getSrcValueOffset() + Size; |
| 1222 | int64_t End2 = Store->getSrcValueOffset() + Size; |
| Richard Sandiford | 067817e | 2013-09-27 15:29:20 +0000 | [diff] [blame] | 1223 | if (V1 == V2 && End1 == End2) |
| 1224 | return false; |
| 1225 | |
| Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 1226 | return !AA->alias(MemoryLocation(V1, End1, Load->getAAInfo()), |
| 1227 | MemoryLocation(V2, End2, Store->getAAInfo())); |
| Richard Sandiford | 9784649 | 2013-07-09 09:46:39 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
| Richard Sandiford | 178273a | 2013-09-05 10:36:45 +0000 | [diff] [blame] | 1230 | bool SystemZDAGToDAGISel::storeLoadCanUseMVC(SDNode *N) const { |
| Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1231 | auto *Store = cast<StoreSDNode>(N); |
| 1232 | auto *Load = cast<LoadSDNode>(Store->getValue()); |
| Richard Sandiford | 178273a | 2013-09-05 10:36:45 +0000 | [diff] [blame] | 1233 | |
| 1234 | // Prefer not to use MVC if either address can use ... RELATIVE LONG |
| 1235 | // instructions. |
| 1236 | uint64_t Size = Load->getMemoryVT().getStoreSize(); |
| 1237 | if (Size > 1 && Size <= 8) { |
| 1238 | // Prefer LHRL, LRL and LGRL. |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 1239 | if (SystemZISD::isPCREL(Load->getBasePtr().getOpcode())) |
| Richard Sandiford | 178273a | 2013-09-05 10:36:45 +0000 | [diff] [blame] | 1240 | return false; |
| 1241 | // Prefer STHRL, STRL and STGRL. |
| Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 1242 | if (SystemZISD::isPCREL(Store->getBasePtr().getOpcode())) |
| Richard Sandiford | 178273a | 2013-09-05 10:36:45 +0000 | [diff] [blame] | 1243 | return false; |
| 1244 | } |
| 1245 | |
| Richard Sandiford | 067817e | 2013-09-27 15:29:20 +0000 | [diff] [blame] | 1246 | return canUseBlockOperation(Store, Load); |
| Richard Sandiford | 178273a | 2013-09-05 10:36:45 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | bool SystemZDAGToDAGISel::storeLoadCanUseBlockBinary(SDNode *N, |
| 1250 | unsigned I) const { |
| Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1251 | auto *StoreA = cast<StoreSDNode>(N); |
| 1252 | auto *LoadA = cast<LoadSDNode>(StoreA->getValue().getOperand(1 - I)); |
| 1253 | auto *LoadB = cast<LoadSDNode>(StoreA->getValue().getOperand(I)); |
| Richard Sandiford | 067817e | 2013-09-27 15:29:20 +0000 | [diff] [blame] | 1254 | return !LoadA->isVolatile() && canUseBlockOperation(StoreA, LoadB); |
| Richard Sandiford | 178273a | 2013-09-05 10:36:45 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1257 | void SystemZDAGToDAGISel::Select(SDNode *Node) { |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1258 | // If we have a custom node, we already have selected! |
| 1259 | if (Node->isMachineOpcode()) { |
| 1260 | DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n"); |
| Tim Northover | 31d093c | 2013-09-22 08:21:56 +0000 | [diff] [blame] | 1261 | Node->setNodeId(-1); |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1262 | return; |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | unsigned Opcode = Node->getOpcode(); |
| 1266 | switch (Opcode) { |
| 1267 | case ISD::OR: |
| Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 1268 | if (Node->getOperand(1).getOpcode() != ISD::Constant) |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1269 | if (tryRxSBG(Node, SystemZ::ROSBG)) |
| 1270 | return; |
| Richard Sandiford | 7878b85 | 2013-07-18 10:06:15 +0000 | [diff] [blame] | 1271 | goto or_xor; |
| 1272 | |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1273 | case ISD::XOR: |
| Richard Sandiford | 7878b85 | 2013-07-18 10:06:15 +0000 | [diff] [blame] | 1274 | if (Node->getOperand(1).getOpcode() != ISD::Constant) |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1275 | if (tryRxSBG(Node, SystemZ::RXSBG)) |
| 1276 | return; |
| Richard Sandiford | 7878b85 | 2013-07-18 10:06:15 +0000 | [diff] [blame] | 1277 | // Fall through. |
| 1278 | or_xor: |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1279 | // If this is a 64-bit operation in which both 32-bit halves are nonzero, |
| Ulrich Weigand | 5f4373a | 2017-11-14 20:00:34 +0000 | [diff] [blame] | 1280 | // split the operation into two. If both operands here happen to be |
| 1281 | // constant, leave this to common code to optimize. |
| 1282 | if (Node->getValueType(0) == MVT::i64 && |
| 1283 | Node->getOperand(0).getOpcode() != ISD::Constant) |
| Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1284 | if (auto *Op1 = dyn_cast<ConstantSDNode>(Node->getOperand(1))) { |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1285 | uint64_t Val = Op1->getZExtValue(); |
| Justin Bogner | ffb273d | 2016-05-09 23:54:23 +0000 | [diff] [blame] | 1286 | if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val)) { |
| 1287 | splitLargeImmediate(Opcode, Node, Node->getOperand(0), |
| 1288 | Val - uint32_t(Val), uint32_t(Val)); |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1289 | return; |
| Justin Bogner | ffb273d | 2016-05-09 23:54:23 +0000 | [diff] [blame] | 1290 | } |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1291 | } |
| 1292 | break; |
| 1293 | |
| Richard Sandiford | 84f54a3 | 2013-07-11 08:59:12 +0000 | [diff] [blame] | 1294 | case ISD::AND: |
| Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 1295 | if (Node->getOperand(1).getOpcode() != ISD::Constant) |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1296 | if (tryRxSBG(Node, SystemZ::RNSBG)) |
| 1297 | return; |
| Justin Bogner | cd1d5aa | 2016-08-17 20:30:52 +0000 | [diff] [blame] | 1298 | LLVM_FALLTHROUGH; |
| Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 1299 | case ISD::ROTL: |
| 1300 | case ISD::SHL: |
| 1301 | case ISD::SRL: |
| Richard Sandiford | 220ee49 | 2013-12-20 11:49:48 +0000 | [diff] [blame] | 1302 | case ISD::ZERO_EXTEND: |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1303 | if (tryRISBGZero(Node)) |
| 1304 | return; |
| Richard Sandiford | 84f54a3 | 2013-07-11 08:59:12 +0000 | [diff] [blame] | 1305 | break; |
| 1306 | |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1307 | case ISD::Constant: |
| 1308 | // If this is a 64-bit constant that is out of the range of LLILF, |
| 1309 | // LLIHF and LGFI, split it into two 32-bit pieces. |
| 1310 | if (Node->getValueType(0) == MVT::i64) { |
| 1311 | uint64_t Val = cast<ConstantSDNode>(Node)->getZExtValue(); |
| Justin Bogner | ffb273d | 2016-05-09 23:54:23 +0000 | [diff] [blame] | 1312 | if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val) && !isInt<32>(Val)) { |
| 1313 | splitLargeImmediate(ISD::OR, Node, SDValue(), Val - uint32_t(Val), |
| 1314 | uint32_t(Val)); |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1315 | return; |
| Justin Bogner | ffb273d | 2016-05-09 23:54:23 +0000 | [diff] [blame] | 1316 | } |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1317 | } |
| 1318 | break; |
| 1319 | |
| Richard Sandiford | ee83438 | 2013-07-31 12:38:08 +0000 | [diff] [blame] | 1320 | case SystemZISD::SELECT_CCMASK: { |
| 1321 | SDValue Op0 = Node->getOperand(0); |
| 1322 | SDValue Op1 = Node->getOperand(1); |
| 1323 | // Prefer to put any load first, so that it can be matched as a |
| Ulrich Weigand | 524f276 | 2016-11-28 13:34:08 +0000 | [diff] [blame] | 1324 | // conditional load. Likewise for constants in range for LOCHI. |
| 1325 | if ((Op1.getOpcode() == ISD::LOAD && Op0.getOpcode() != ISD::LOAD) || |
| 1326 | (Subtarget->hasLoadStoreOnCond2() && |
| 1327 | Node->getValueType(0).isInteger() && |
| 1328 | Op1.getOpcode() == ISD::Constant && |
| 1329 | isInt<16>(cast<ConstantSDNode>(Op1)->getSExtValue()) && |
| 1330 | !(Op0.getOpcode() == ISD::Constant && |
| 1331 | isInt<16>(cast<ConstantSDNode>(Op0)->getSExtValue())))) { |
| Richard Sandiford | ee83438 | 2013-07-31 12:38:08 +0000 | [diff] [blame] | 1332 | SDValue CCValid = Node->getOperand(2); |
| 1333 | SDValue CCMask = Node->getOperand(3); |
| 1334 | uint64_t ConstCCValid = |
| 1335 | cast<ConstantSDNode>(CCValid.getNode())->getZExtValue(); |
| 1336 | uint64_t ConstCCMask = |
| 1337 | cast<ConstantSDNode>(CCMask.getNode())->getZExtValue(); |
| 1338 | // Invert the condition. |
| Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1339 | CCMask = CurDAG->getConstant(ConstCCValid ^ ConstCCMask, SDLoc(Node), |
| Richard Sandiford | ee83438 | 2013-07-31 12:38:08 +0000 | [diff] [blame] | 1340 | CCMask.getValueType()); |
| 1341 | SDValue Op4 = Node->getOperand(4); |
| 1342 | Node = CurDAG->UpdateNodeOperands(Node, Op1, Op0, CCValid, CCMask, Op4); |
| 1343 | } |
| 1344 | break; |
| 1345 | } |
| Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1346 | |
| 1347 | case ISD::INSERT_VECTOR_ELT: { |
| 1348 | EVT VT = Node->getValueType(0); |
| Sanjay Patel | 1ed771f | 2016-09-14 16:37:15 +0000 | [diff] [blame] | 1349 | unsigned ElemBitSize = VT.getScalarSizeInBits(); |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1350 | if (ElemBitSize == 32) { |
| 1351 | if (tryGather(Node, SystemZ::VGEF)) |
| 1352 | return; |
| 1353 | } else if (ElemBitSize == 64) { |
| 1354 | if (tryGather(Node, SystemZ::VGEG)) |
| 1355 | return; |
| 1356 | } |
| Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1357 | break; |
| 1358 | } |
| 1359 | |
| 1360 | case ISD::STORE: { |
| 1361 | auto *Store = cast<StoreSDNode>(Node); |
| Sanjay Patel | b1f0a0f | 2016-09-14 16:05:51 +0000 | [diff] [blame] | 1362 | unsigned ElemBitSize = Store->getValue().getValueSizeInBits(); |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1363 | if (ElemBitSize == 32) { |
| 1364 | if (tryScatter(Store, SystemZ::VSCEF)) |
| 1365 | return; |
| 1366 | } else if (ElemBitSize == 64) { |
| 1367 | if (tryScatter(Store, SystemZ::VSCEG)) |
| 1368 | return; |
| 1369 | } |
| Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1370 | break; |
| 1371 | } |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
| Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1374 | SelectCode(Node); |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
| 1377 | bool SystemZDAGToDAGISel:: |
| 1378 | SelectInlineAsmMemoryOperand(const SDValue &Op, |
| Daniel Sanders | 60f1db0 | 2015-03-13 12:45:09 +0000 | [diff] [blame] | 1379 | unsigned ConstraintID, |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1380 | std::vector<SDValue> &OutOps) { |
| Ulrich Weigand | daae87aa | 2016-06-13 14:24:05 +0000 | [diff] [blame] | 1381 | SystemZAddressingMode::AddrForm Form; |
| 1382 | SystemZAddressingMode::DispRange DispRange; |
| Ulrich Weigand | 7956461 | 2016-06-09 15:19:16 +0000 | [diff] [blame] | 1383 | SDValue Base, Disp, Index; |
| 1384 | |
| Daniel Sanders | 2eeace2 | 2015-03-17 16:16:14 +0000 | [diff] [blame] | 1385 | switch(ConstraintID) { |
| 1386 | default: |
| 1387 | llvm_unreachable("Unexpected asm memory constraint"); |
| 1388 | case InlineAsm::Constraint_i: |
| Daniel Sanders | 2eeace2 | 2015-03-17 16:16:14 +0000 | [diff] [blame] | 1389 | case InlineAsm::Constraint_Q: |
| Ulrich Weigand | daae87aa | 2016-06-13 14:24:05 +0000 | [diff] [blame] | 1390 | // Accept an address with a short displacement, but no index. |
| 1391 | Form = SystemZAddressingMode::FormBD; |
| 1392 | DispRange = SystemZAddressingMode::Disp12Only; |
| 1393 | break; |
| Daniel Sanders | 2eeace2 | 2015-03-17 16:16:14 +0000 | [diff] [blame] | 1394 | case InlineAsm::Constraint_R: |
| Ulrich Weigand | daae87aa | 2016-06-13 14:24:05 +0000 | [diff] [blame] | 1395 | // Accept an address with a short displacement and an index. |
| 1396 | Form = SystemZAddressingMode::FormBDXNormal; |
| 1397 | DispRange = SystemZAddressingMode::Disp12Only; |
| Daniel Sanders | 2eeace2 | 2015-03-17 16:16:14 +0000 | [diff] [blame] | 1398 | break; |
| Ulrich Weigand | 7956461 | 2016-06-09 15:19:16 +0000 | [diff] [blame] | 1399 | case InlineAsm::Constraint_S: |
| Ulrich Weigand | daae87aa | 2016-06-13 14:24:05 +0000 | [diff] [blame] | 1400 | // Accept an address with a long displacement, but no index. |
| 1401 | Form = SystemZAddressingMode::FormBD; |
| 1402 | DispRange = SystemZAddressingMode::Disp20Only; |
| 1403 | break; |
| Ulrich Weigand | 7956461 | 2016-06-09 15:19:16 +0000 | [diff] [blame] | 1404 | case InlineAsm::Constraint_T: |
| 1405 | case InlineAsm::Constraint_m: |
| Ulrich Weigand | d39e9dc | 2017-11-09 16:31:57 +0000 | [diff] [blame] | 1406 | case InlineAsm::Constraint_o: |
| Ulrich Weigand | daae87aa | 2016-06-13 14:24:05 +0000 | [diff] [blame] | 1407 | // Accept an address with a long displacement and an index. |
| 1408 | // m works the same as T, as this is the most general case. |
| Ulrich Weigand | d39e9dc | 2017-11-09 16:31:57 +0000 | [diff] [blame] | 1409 | // We don't really have any special handling of "offsettable" |
| 1410 | // memory addresses, so just treat o the same as m. |
| Ulrich Weigand | daae87aa | 2016-06-13 14:24:05 +0000 | [diff] [blame] | 1411 | Form = SystemZAddressingMode::FormBDXNormal; |
| 1412 | DispRange = SystemZAddressingMode::Disp20Only; |
| Ulrich Weigand | 7956461 | 2016-06-09 15:19:16 +0000 | [diff] [blame] | 1413 | break; |
| Daniel Sanders | 2eeace2 | 2015-03-17 16:16:14 +0000 | [diff] [blame] | 1414 | } |
| Ulrich Weigand | daae87aa | 2016-06-13 14:24:05 +0000 | [diff] [blame] | 1415 | |
| 1416 | if (selectBDXAddr(Form, DispRange, Op, Base, Disp, Index)) { |
| Zhan Jun Liau | cf2f4b3 | 2016-08-18 21:44:15 +0000 | [diff] [blame] | 1417 | const TargetRegisterClass *TRC = |
| 1418 | Subtarget->getRegisterInfo()->getPointerRegClass(*MF); |
| 1419 | SDLoc DL(Base); |
| 1420 | SDValue RC = CurDAG->getTargetConstant(TRC->getID(), DL, MVT::i32); |
| 1421 | |
| 1422 | // Make sure that the base address doesn't go into %r0. |
| 1423 | // If it's a TargetFrameIndex or a fixed register, we shouldn't do anything. |
| 1424 | if (Base.getOpcode() != ISD::TargetFrameIndex && |
| 1425 | Base.getOpcode() != ISD::Register) { |
| 1426 | Base = |
| 1427 | SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, |
| 1428 | DL, Base.getValueType(), |
| 1429 | Base, RC), 0); |
| 1430 | } |
| 1431 | |
| 1432 | // Make sure that the index register isn't assigned to %r0 either. |
| 1433 | if (Index.getOpcode() != ISD::Register) { |
| 1434 | Index = |
| 1435 | SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, |
| 1436 | DL, Index.getValueType(), |
| 1437 | Index, RC), 0); |
| 1438 | } |
| 1439 | |
| Ulrich Weigand | daae87aa | 2016-06-13 14:24:05 +0000 | [diff] [blame] | 1440 | OutOps.push_back(Base); |
| 1441 | OutOps.push_back(Disp); |
| 1442 | OutOps.push_back(Index); |
| 1443 | return false; |
| 1444 | } |
| 1445 | |
| Daniel Sanders | 2eeace2 | 2015-03-17 16:16:14 +0000 | [diff] [blame] | 1446 | return true; |
| Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1447 | } |
| Ulrich Weigand | 849a59f | 2018-01-19 20:52:04 +0000 | [diff] [blame] | 1448 | |
| Ulrich Weigand | b32f365 | 2018-04-30 17:52:32 +0000 | [diff] [blame^] | 1449 | // IsProfitableToFold - Returns true if is profitable to fold the specific |
| 1450 | // operand node N of U during instruction selection that starts at Root. |
| 1451 | bool |
| 1452 | SystemZDAGToDAGISel::IsProfitableToFold(SDValue N, SDNode *U, |
| 1453 | SDNode *Root) const { |
| 1454 | // We want to avoid folding a LOAD into an ICMP node if as a result |
| 1455 | // we would be forced to spill the condition code into a GPR. |
| 1456 | if (N.getOpcode() == ISD::LOAD && U->getOpcode() == SystemZISD::ICMP) { |
| 1457 | if (!N.hasOneUse() || !U->hasOneUse()) |
| 1458 | return false; |
| 1459 | |
| 1460 | // The user of the CC value will usually be a CopyToReg into the |
| 1461 | // physical CC register, which in turn is glued and chained to the |
| 1462 | // actual instruction that uses the CC value. Bail out if we have |
| 1463 | // anything else than that. |
| 1464 | SDNode *CCUser = *U->use_begin(); |
| 1465 | SDNode *CCRegUser = nullptr; |
| 1466 | if (CCUser->getOpcode() == ISD::CopyToReg || |
| 1467 | cast<RegisterSDNode>(CCUser->getOperand(1))->getReg() == SystemZ::CC) { |
| 1468 | for (auto *U : CCUser->uses()) { |
| 1469 | if (CCRegUser == nullptr) |
| 1470 | CCRegUser = U; |
| 1471 | else if (CCRegUser != U) |
| 1472 | return false; |
| 1473 | } |
| 1474 | } |
| 1475 | if (CCRegUser == nullptr) |
| 1476 | return false; |
| 1477 | |
| 1478 | // If the actual instruction is a branch, the only thing that remains to be |
| 1479 | // checked is whether the CCUser chain is a predecessor of the load. |
| 1480 | if (CCRegUser->isMachineOpcode() && |
| 1481 | CCRegUser->getMachineOpcode() == SystemZ::BRC) |
| 1482 | return !N->isPredecessorOf(CCUser->getOperand(0).getNode()); |
| 1483 | |
| 1484 | // Otherwise, the instruction may have multiple operands, and we need to |
| 1485 | // verify that none of them are a predecessor of the load. This is exactly |
| 1486 | // the same check that would be done by common code if the CC setter were |
| 1487 | // glued to the CC user, so simply invoke that check here. |
| 1488 | if (!IsLegalToFold(N, U, CCRegUser, OptLevel, false)) |
| 1489 | return false; |
| 1490 | } |
| 1491 | |
| 1492 | return true; |
| 1493 | } |
| 1494 | |
| Ulrich Weigand | 849a59f | 2018-01-19 20:52:04 +0000 | [diff] [blame] | 1495 | namespace { |
| 1496 | // Represents a sequence for extracting a 0/1 value from an IPM result: |
| 1497 | // (((X ^ XORValue) + AddValue) >> Bit) |
| 1498 | struct IPMConversion { |
| 1499 | IPMConversion(unsigned xorValue, int64_t addValue, unsigned bit) |
| 1500 | : XORValue(xorValue), AddValue(addValue), Bit(bit) {} |
| 1501 | |
| 1502 | int64_t XORValue; |
| 1503 | int64_t AddValue; |
| 1504 | unsigned Bit; |
| 1505 | }; |
| 1506 | } // end anonymous namespace |
| 1507 | |
| 1508 | // Return a sequence for getting a 1 from an IPM result when CC has a |
| 1509 | // value in CCMask and a 0 when CC has a value in CCValid & ~CCMask. |
| 1510 | // The handling of CC values outside CCValid doesn't matter. |
| 1511 | static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) { |
| 1512 | // Deal with cases where the result can be taken directly from a bit |
| 1513 | // of the IPM result. |
| 1514 | if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3))) |
| 1515 | return IPMConversion(0, 0, SystemZ::IPM_CC); |
| 1516 | if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3))) |
| 1517 | return IPMConversion(0, 0, SystemZ::IPM_CC + 1); |
| 1518 | |
| 1519 | // Deal with cases where we can add a value to force the sign bit |
| 1520 | // to contain the right value. Putting the bit in 31 means we can |
| 1521 | // use SRL rather than RISBG(L), and also makes it easier to get a |
| 1522 | // 0/-1 value, so it has priority over the other tests below. |
| 1523 | // |
| 1524 | // These sequences rely on the fact that the upper two bits of the |
| 1525 | // IPM result are zero. |
| 1526 | uint64_t TopBit = uint64_t(1) << 31; |
| 1527 | if (CCMask == (CCValid & SystemZ::CCMASK_0)) |
| 1528 | return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31); |
| 1529 | if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1))) |
| 1530 | return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31); |
| 1531 | if (CCMask == (CCValid & (SystemZ::CCMASK_0 |
| 1532 | | SystemZ::CCMASK_1 |
| 1533 | | SystemZ::CCMASK_2))) |
| 1534 | return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31); |
| 1535 | if (CCMask == (CCValid & SystemZ::CCMASK_3)) |
| 1536 | return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31); |
| 1537 | if (CCMask == (CCValid & (SystemZ::CCMASK_1 |
| 1538 | | SystemZ::CCMASK_2 |
| 1539 | | SystemZ::CCMASK_3))) |
| 1540 | return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31); |
| 1541 | |
| 1542 | // Next try inverting the value and testing a bit. 0/1 could be |
| 1543 | // handled this way too, but we dealt with that case above. |
| 1544 | if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2))) |
| 1545 | return IPMConversion(-1, 0, SystemZ::IPM_CC); |
| 1546 | |
| 1547 | // Handle cases where adding a value forces a non-sign bit to contain |
| 1548 | // the right value. |
| 1549 | if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2))) |
| 1550 | return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1); |
| 1551 | if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3))) |
| 1552 | return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1); |
| 1553 | |
| 1554 | // The remaining cases are 1, 2, 0/1/3 and 0/2/3. All these are |
| 1555 | // can be done by inverting the low CC bit and applying one of the |
| 1556 | // sign-based extractions above. |
| 1557 | if (CCMask == (CCValid & SystemZ::CCMASK_1)) |
| 1558 | return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31); |
| 1559 | if (CCMask == (CCValid & SystemZ::CCMASK_2)) |
| 1560 | return IPMConversion(1 << SystemZ::IPM_CC, |
| 1561 | TopBit - (3 << SystemZ::IPM_CC), 31); |
| 1562 | if (CCMask == (CCValid & (SystemZ::CCMASK_0 |
| 1563 | | SystemZ::CCMASK_1 |
| 1564 | | SystemZ::CCMASK_3))) |
| 1565 | return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31); |
| 1566 | if (CCMask == (CCValid & (SystemZ::CCMASK_0 |
| 1567 | | SystemZ::CCMASK_2 |
| 1568 | | SystemZ::CCMASK_3))) |
| 1569 | return IPMConversion(1 << SystemZ::IPM_CC, |
| 1570 | TopBit - (1 << SystemZ::IPM_CC), 31); |
| 1571 | |
| 1572 | llvm_unreachable("Unexpected CC combination"); |
| 1573 | } |
| 1574 | |
| 1575 | SDValue SystemZDAGToDAGISel::expandSelectBoolean(SDNode *Node) { |
| 1576 | auto *TrueOp = dyn_cast<ConstantSDNode>(Node->getOperand(0)); |
| 1577 | auto *FalseOp = dyn_cast<ConstantSDNode>(Node->getOperand(1)); |
| 1578 | if (!TrueOp || !FalseOp) |
| 1579 | return SDValue(); |
| 1580 | if (FalseOp->getZExtValue() != 0) |
| 1581 | return SDValue(); |
| 1582 | if (TrueOp->getSExtValue() != 1 && TrueOp->getSExtValue() != -1) |
| 1583 | return SDValue(); |
| 1584 | |
| 1585 | auto *CCValidOp = dyn_cast<ConstantSDNode>(Node->getOperand(2)); |
| 1586 | auto *CCMaskOp = dyn_cast<ConstantSDNode>(Node->getOperand(3)); |
| 1587 | if (!CCValidOp || !CCMaskOp) |
| 1588 | return SDValue(); |
| 1589 | int CCValid = CCValidOp->getZExtValue(); |
| 1590 | int CCMask = CCMaskOp->getZExtValue(); |
| 1591 | |
| 1592 | SDLoc DL(Node); |
| Ulrich Weigand | b32f365 | 2018-04-30 17:52:32 +0000 | [diff] [blame^] | 1593 | SDValue CCReg = Node->getOperand(4); |
| Ulrich Weigand | 849a59f | 2018-01-19 20:52:04 +0000 | [diff] [blame] | 1594 | IPMConversion IPM = getIPMConversion(CCValid, CCMask); |
| Ulrich Weigand | b32f365 | 2018-04-30 17:52:32 +0000 | [diff] [blame^] | 1595 | SDValue Result = CurDAG->getNode(SystemZISD::IPM, DL, MVT::i32, CCReg); |
| Ulrich Weigand | 849a59f | 2018-01-19 20:52:04 +0000 | [diff] [blame] | 1596 | |
| 1597 | if (IPM.XORValue) |
| 1598 | Result = CurDAG->getNode(ISD::XOR, DL, MVT::i32, Result, |
| 1599 | CurDAG->getConstant(IPM.XORValue, DL, MVT::i32)); |
| 1600 | |
| 1601 | if (IPM.AddValue) |
| 1602 | Result = CurDAG->getNode(ISD::ADD, DL, MVT::i32, Result, |
| 1603 | CurDAG->getConstant(IPM.AddValue, DL, MVT::i32)); |
| 1604 | |
| 1605 | EVT VT = Node->getValueType(0); |
| 1606 | if (VT == MVT::i32 && IPM.Bit == 31) { |
| 1607 | unsigned ShiftOp = TrueOp->getSExtValue() == 1 ? ISD::SRL : ISD::SRA; |
| 1608 | Result = CurDAG->getNode(ShiftOp, DL, MVT::i32, Result, |
| 1609 | CurDAG->getConstant(IPM.Bit, DL, MVT::i32)); |
| 1610 | } else { |
| 1611 | if (VT != MVT::i32) |
| 1612 | Result = CurDAG->getNode(ISD::ANY_EXTEND, DL, VT, Result); |
| 1613 | |
| 1614 | if (TrueOp->getSExtValue() == 1) { |
| 1615 | // The SHR/AND sequence should get optimized to an RISBG. |
| 1616 | Result = CurDAG->getNode(ISD::SRL, DL, VT, Result, |
| 1617 | CurDAG->getConstant(IPM.Bit, DL, MVT::i32)); |
| 1618 | Result = CurDAG->getNode(ISD::AND, DL, VT, Result, |
| 1619 | CurDAG->getConstant(1, DL, VT)); |
| 1620 | } else { |
| 1621 | // Sign-extend from IPM.Bit using a pair of shifts. |
| 1622 | int ShlAmt = VT.getSizeInBits() - 1 - IPM.Bit; |
| 1623 | int SraAmt = VT.getSizeInBits() - 1; |
| 1624 | Result = CurDAG->getNode(ISD::SHL, DL, VT, Result, |
| 1625 | CurDAG->getConstant(ShlAmt, DL, MVT::i32)); |
| 1626 | Result = CurDAG->getNode(ISD::SRA, DL, VT, Result, |
| 1627 | CurDAG->getConstant(SraAmt, DL, MVT::i32)); |
| 1628 | } |
| 1629 | } |
| 1630 | |
| 1631 | return Result; |
| 1632 | } |
| 1633 | |
| 1634 | void SystemZDAGToDAGISel::PreprocessISelDAG() { |
| Ulrich Weigand | 426f6be | 2018-01-19 20:56:04 +0000 | [diff] [blame] | 1635 | // If we have conditional immediate loads, we always prefer |
| 1636 | // using those over an IPM sequence. |
| 1637 | if (Subtarget->hasLoadStoreOnCond2()) |
| 1638 | return; |
| 1639 | |
| Ulrich Weigand | 849a59f | 2018-01-19 20:52:04 +0000 | [diff] [blame] | 1640 | bool MadeChange = false; |
| 1641 | |
| 1642 | for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(), |
| 1643 | E = CurDAG->allnodes_end(); |
| 1644 | I != E;) { |
| 1645 | SDNode *N = &*I++; |
| 1646 | if (N->use_empty()) |
| 1647 | continue; |
| 1648 | |
| 1649 | SDValue Res; |
| 1650 | switch (N->getOpcode()) { |
| 1651 | default: break; |
| 1652 | case SystemZISD::SELECT_CCMASK: |
| 1653 | Res = expandSelectBoolean(N); |
| 1654 | break; |
| 1655 | } |
| 1656 | |
| 1657 | if (Res) { |
| 1658 | DEBUG(dbgs() << "SystemZ DAG preprocessing replacing:\nOld: "); |
| 1659 | DEBUG(N->dump(CurDAG)); |
| 1660 | DEBUG(dbgs() << "\nNew: "); |
| 1661 | DEBUG(Res.getNode()->dump(CurDAG)); |
| 1662 | DEBUG(dbgs() << "\n"); |
| 1663 | |
| 1664 | CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Res); |
| 1665 | MadeChange = true; |
| 1666 | } |
| 1667 | } |
| 1668 | |
| 1669 | if (MadeChange) |
| 1670 | CurDAG->RemoveDeadNodes(); |
| 1671 | } |