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 | 849a59f | 2018-01-19 20:52:04 +0000 | [diff] [blame^] | 354 | void PreprocessISelDAG() override; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 355 | |
| 356 | // Include the pieces autogenerated from the target description. |
| 357 | #include "SystemZGenDAGISel.inc" |
| 358 | }; |
| 359 | } // end anonymous namespace |
| 360 | |
| 361 | FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM, |
| 362 | CodeGenOpt::Level OptLevel) { |
| 363 | return new SystemZDAGToDAGISel(TM, OptLevel); |
| 364 | } |
| 365 | |
| 366 | // Return true if Val should be selected as a displacement for an address |
| 367 | // with range DR. Here we're interested in the range of both the instruction |
| 368 | // described by DR and of any pairing instruction. |
| 369 | static bool selectDisp(SystemZAddressingMode::DispRange DR, int64_t Val) { |
| 370 | switch (DR) { |
| 371 | case SystemZAddressingMode::Disp12Only: |
| 372 | return isUInt<12>(Val); |
| 373 | |
| 374 | case SystemZAddressingMode::Disp12Pair: |
| 375 | case SystemZAddressingMode::Disp20Only: |
| 376 | case SystemZAddressingMode::Disp20Pair: |
| 377 | return isInt<20>(Val); |
| 378 | |
| 379 | case SystemZAddressingMode::Disp20Only128: |
| 380 | return isInt<20>(Val) && isInt<20>(Val + 8); |
| 381 | } |
| 382 | llvm_unreachable("Unhandled displacement range"); |
| 383 | } |
| 384 | |
| 385 | // Change the base or index in AM to Value, where IsBase selects |
| 386 | // between the base and index. |
| 387 | static void changeComponent(SystemZAddressingMode &AM, bool IsBase, |
| 388 | SDValue Value) { |
| 389 | if (IsBase) |
| 390 | AM.Base = Value; |
| 391 | else |
| 392 | AM.Index = Value; |
| 393 | } |
| 394 | |
| 395 | // The base or index of AM is equivalent to Value + ADJDYNALLOC, |
| 396 | // where IsBase selects between the base and index. Try to fold the |
| 397 | // ADJDYNALLOC into AM. |
| 398 | static bool expandAdjDynAlloc(SystemZAddressingMode &AM, bool IsBase, |
| 399 | SDValue Value) { |
| 400 | if (AM.isDynAlloc() && !AM.IncludesDynAlloc) { |
| 401 | changeComponent(AM, IsBase, Value); |
| 402 | AM.IncludesDynAlloc = true; |
| 403 | return true; |
| 404 | } |
| 405 | return false; |
| 406 | } |
| 407 | |
| 408 | // The base of AM is equivalent to Base + Index. Try to use Index as |
| 409 | // the index register. |
| 410 | static bool expandIndex(SystemZAddressingMode &AM, SDValue Base, |
| 411 | SDValue Index) { |
| 412 | if (AM.hasIndexField() && !AM.Index.getNode()) { |
| 413 | AM.Base = Base; |
| 414 | AM.Index = Index; |
| 415 | return true; |
| 416 | } |
| 417 | return false; |
| 418 | } |
| 419 | |
| 420 | // The base or index of AM is equivalent to Op0 + Op1, where IsBase selects |
| 421 | // between the base and index. Try to fold Op1 into AM's displacement. |
| 422 | static bool expandDisp(SystemZAddressingMode &AM, bool IsBase, |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 423 | SDValue Op0, uint64_t Op1) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 424 | // First try adjusting the displacement. |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 425 | int64_t TestDisp = AM.Disp + Op1; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 426 | if (selectDisp(AM.DR, TestDisp)) { |
| 427 | changeComponent(AM, IsBase, Op0); |
| 428 | AM.Disp = TestDisp; |
| 429 | return true; |
| 430 | } |
| 431 | |
| 432 | // We could consider forcing the displacement into a register and |
| 433 | // using it as an index, but it would need to be carefully tuned. |
| 434 | return false; |
| 435 | } |
| 436 | |
| 437 | bool SystemZDAGToDAGISel::expandAddress(SystemZAddressingMode &AM, |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 438 | bool IsBase) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 439 | SDValue N = IsBase ? AM.Base : AM.Index; |
| 440 | unsigned Opcode = N.getOpcode(); |
| 441 | if (Opcode == ISD::TRUNCATE) { |
| 442 | N = N.getOperand(0); |
| 443 | Opcode = N.getOpcode(); |
| 444 | } |
| 445 | if (Opcode == ISD::ADD || CurDAG->isBaseWithConstantOffset(N)) { |
| 446 | SDValue Op0 = N.getOperand(0); |
| 447 | SDValue Op1 = N.getOperand(1); |
| 448 | |
| 449 | unsigned Op0Code = Op0->getOpcode(); |
| 450 | unsigned Op1Code = Op1->getOpcode(); |
| 451 | |
| 452 | if (Op0Code == SystemZISD::ADJDYNALLOC) |
| 453 | return expandAdjDynAlloc(AM, IsBase, Op1); |
| 454 | if (Op1Code == SystemZISD::ADJDYNALLOC) |
| 455 | return expandAdjDynAlloc(AM, IsBase, Op0); |
| 456 | |
| 457 | if (Op0Code == ISD::Constant) |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 458 | return expandDisp(AM, IsBase, Op1, |
| 459 | cast<ConstantSDNode>(Op0)->getSExtValue()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 460 | if (Op1Code == ISD::Constant) |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 461 | return expandDisp(AM, IsBase, Op0, |
| 462 | cast<ConstantSDNode>(Op1)->getSExtValue()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 463 | |
| 464 | if (IsBase && expandIndex(AM, Op0, Op1)) |
| 465 | return true; |
| 466 | } |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 467 | if (Opcode == SystemZISD::PCREL_OFFSET) { |
| 468 | SDValue Full = N.getOperand(0); |
| 469 | SDValue Base = N.getOperand(1); |
| 470 | SDValue Anchor = Base.getOperand(0); |
| 471 | uint64_t Offset = (cast<GlobalAddressSDNode>(Full)->getOffset() - |
| 472 | cast<GlobalAddressSDNode>(Anchor)->getOffset()); |
| 473 | return expandDisp(AM, IsBase, Base, Offset); |
| 474 | } |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 475 | return false; |
| 476 | } |
| 477 | |
| 478 | // Return true if an instruction with displacement range DR should be |
| 479 | // used for displacement value Val. selectDisp(DR, Val) must already hold. |
| 480 | static bool isValidDisp(SystemZAddressingMode::DispRange DR, int64_t Val) { |
| 481 | assert(selectDisp(DR, Val) && "Invalid displacement"); |
| 482 | switch (DR) { |
| 483 | case SystemZAddressingMode::Disp12Only: |
| 484 | case SystemZAddressingMode::Disp20Only: |
| 485 | case SystemZAddressingMode::Disp20Only128: |
| 486 | return true; |
| 487 | |
| 488 | case SystemZAddressingMode::Disp12Pair: |
| 489 | // Use the other instruction if the displacement is too large. |
| 490 | return isUInt<12>(Val); |
| 491 | |
| 492 | case SystemZAddressingMode::Disp20Pair: |
| 493 | // Use the other instruction if the displacement is small enough. |
| 494 | return !isUInt<12>(Val); |
| 495 | } |
| 496 | llvm_unreachable("Unhandled displacement range"); |
| 497 | } |
| 498 | |
| 499 | // Return true if Base + Disp + Index should be performed by LA(Y). |
| 500 | static bool shouldUseLA(SDNode *Base, int64_t Disp, SDNode *Index) { |
| 501 | // Don't use LA(Y) for constants. |
| 502 | if (!Base) |
| 503 | return false; |
| 504 | |
| 505 | // Always use LA(Y) for frame addresses, since we know that the destination |
| 506 | // register is almost always (perhaps always) going to be different from |
| 507 | // the frame register. |
| 508 | if (Base->getOpcode() == ISD::FrameIndex) |
| 509 | return true; |
| 510 | |
| 511 | if (Disp) { |
| 512 | // Always use LA(Y) if there is a base, displacement and index. |
| 513 | if (Index) |
| 514 | return true; |
| 515 | |
| 516 | // Always use LA if the displacement is small enough. It should always |
| 517 | // be no worse than AGHI (and better if it avoids a move). |
| 518 | if (isUInt<12>(Disp)) |
| 519 | return true; |
| 520 | |
| 521 | // For similar reasons, always use LAY if the constant is too big for AGHI. |
| 522 | // LAY should be no worse than AGFI. |
| 523 | if (!isInt<16>(Disp)) |
| 524 | return true; |
| 525 | } else { |
| 526 | // Don't use LA for plain registers. |
| 527 | if (!Index) |
| 528 | return false; |
| 529 | |
| 530 | // Don't use LA for plain addition if the index operand is only used |
| 531 | // once. It should be a natural two-operand addition in that case. |
| 532 | if (Index->hasOneUse()) |
| 533 | return false; |
| 534 | |
| 535 | // Prefer addition if the second operation is sign-extended, in the |
| 536 | // hope of using AGF. |
| 537 | unsigned IndexOpcode = Index->getOpcode(); |
| 538 | if (IndexOpcode == ISD::SIGN_EXTEND || |
| 539 | IndexOpcode == ISD::SIGN_EXTEND_INREG) |
| 540 | return false; |
| 541 | } |
| 542 | |
| 543 | // Don't use LA for two-operand addition if either operand is only |
| 544 | // used once. The addition instructions are better in that case. |
| 545 | if (Base->hasOneUse()) |
| 546 | return false; |
| 547 | |
| 548 | return true; |
| 549 | } |
| 550 | |
| 551 | // Return true if Addr is suitable for AM, updating AM if so. |
| 552 | bool SystemZDAGToDAGISel::selectAddress(SDValue Addr, |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 553 | SystemZAddressingMode &AM) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 554 | // Start out assuming that the address will need to be loaded separately, |
| 555 | // then try to extend it as much as we can. |
| 556 | AM.Base = Addr; |
| 557 | |
| 558 | // First try treating the address as a constant. |
| 559 | if (Addr.getOpcode() == ISD::Constant && |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 560 | expandDisp(AM, true, SDValue(), |
| 561 | cast<ConstantSDNode>(Addr)->getSExtValue())) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 562 | ; |
Marcin Koscielnicki | 9de88d9 | 2016-05-04 23:31:26 +0000 | [diff] [blame] | 563 | // Also see if it's a bare ADJDYNALLOC. |
| 564 | else if (Addr.getOpcode() == SystemZISD::ADJDYNALLOC && |
| 565 | expandAdjDynAlloc(AM, true, SDValue())) |
| 566 | ; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 567 | else |
| 568 | // Otherwise try expanding each component. |
| 569 | while (expandAddress(AM, true) || |
| 570 | (AM.Index.getNode() && expandAddress(AM, false))) |
| 571 | continue; |
| 572 | |
| 573 | // Reject cases where it isn't profitable to use LA(Y). |
| 574 | if (AM.Form == SystemZAddressingMode::FormBDXLA && |
| 575 | !shouldUseLA(AM.Base.getNode(), AM.Disp, AM.Index.getNode())) |
| 576 | return false; |
| 577 | |
| 578 | // Reject cases where the other instruction in a pair should be used. |
| 579 | if (!isValidDisp(AM.DR, AM.Disp)) |
| 580 | return false; |
| 581 | |
| 582 | // Make sure that ADJDYNALLOC is included where necessary. |
| 583 | if (AM.isDynAlloc() && !AM.IncludesDynAlloc) |
| 584 | return false; |
| 585 | |
| 586 | DEBUG(AM.dump()); |
| 587 | return true; |
| 588 | } |
| 589 | |
| 590 | // Insert a node into the DAG at least before Pos. This will reposition |
| 591 | // the node as needed, and will assign it a node ID that is <= Pos's ID. |
| 592 | // Note that this does *not* preserve the uniqueness of node IDs! |
| 593 | // The selection DAG must no longer depend on their uniqueness when this |
| 594 | // function is used. |
| 595 | static void insertDAGNode(SelectionDAG *DAG, SDNode *Pos, SDValue N) { |
| 596 | if (N.getNode()->getNodeId() == -1 || |
| 597 | N.getNode()->getNodeId() > Pos->getNodeId()) { |
Duncan P. N. Exon Smith | a2c90e4 | 2015-10-20 01:12:46 +0000 | [diff] [blame] | 598 | DAG->RepositionNode(Pos->getIterator(), N.getNode()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 599 | N.getNode()->setNodeId(Pos->getNodeId()); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM, |
| 604 | EVT VT, SDValue &Base, |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 605 | SDValue &Disp) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 606 | Base = AM.Base; |
| 607 | if (!Base.getNode()) |
| 608 | // Register 0 means "no base". This is mostly useful for shifts. |
| 609 | Base = CurDAG->getRegister(0, VT); |
| 610 | else if (Base.getOpcode() == ISD::FrameIndex) { |
| 611 | // Lower a FrameIndex to a TargetFrameIndex. |
| 612 | int64_t FrameIndex = cast<FrameIndexSDNode>(Base)->getIndex(); |
| 613 | Base = CurDAG->getTargetFrameIndex(FrameIndex, VT); |
| 614 | } else if (Base.getValueType() != VT) { |
| 615 | // Truncate values from i64 to i32, for shifts. |
| 616 | assert(VT == MVT::i32 && Base.getValueType() == MVT::i64 && |
| 617 | "Unexpected truncation"); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 618 | SDLoc DL(Base); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 619 | SDValue Trunc = CurDAG->getNode(ISD::TRUNCATE, DL, VT, Base); |
| 620 | insertDAGNode(CurDAG, Base.getNode(), Trunc); |
| 621 | Base = Trunc; |
| 622 | } |
| 623 | |
| 624 | // Lower the displacement to a TargetConstant. |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 625 | Disp = CurDAG->getTargetConstant(AM.Disp, SDLoc(Base), VT); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM, |
| 629 | EVT VT, SDValue &Base, |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 630 | SDValue &Disp, |
| 631 | SDValue &Index) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 632 | getAddressOperands(AM, VT, Base, Disp); |
| 633 | |
| 634 | Index = AM.Index; |
| 635 | if (!Index.getNode()) |
| 636 | // Register 0 means "no index". |
| 637 | Index = CurDAG->getRegister(0, VT); |
| 638 | } |
| 639 | |
| 640 | bool SystemZDAGToDAGISel::selectBDAddr(SystemZAddressingMode::DispRange DR, |
| 641 | SDValue Addr, SDValue &Base, |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 642 | SDValue &Disp) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 643 | SystemZAddressingMode AM(SystemZAddressingMode::FormBD, DR); |
| 644 | if (!selectAddress(Addr, AM)) |
| 645 | return false; |
| 646 | |
| 647 | getAddressOperands(AM, Addr.getValueType(), Base, Disp); |
| 648 | return true; |
| 649 | } |
| 650 | |
Richard Sandiford | a481f58 | 2013-08-23 11:18:53 +0000 | [diff] [blame] | 651 | bool SystemZDAGToDAGISel::selectMVIAddr(SystemZAddressingMode::DispRange DR, |
| 652 | SDValue Addr, SDValue &Base, |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 653 | SDValue &Disp) const { |
Richard Sandiford | a481f58 | 2013-08-23 11:18:53 +0000 | [diff] [blame] | 654 | SystemZAddressingMode AM(SystemZAddressingMode::FormBDXNormal, DR); |
| 655 | if (!selectAddress(Addr, AM) || AM.Index.getNode()) |
| 656 | return false; |
| 657 | |
| 658 | getAddressOperands(AM, Addr.getValueType(), Base, Disp); |
| 659 | return true; |
| 660 | } |
| 661 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 662 | bool SystemZDAGToDAGISel::selectBDXAddr(SystemZAddressingMode::AddrForm Form, |
| 663 | SystemZAddressingMode::DispRange DR, |
| 664 | SDValue Addr, SDValue &Base, |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 665 | SDValue &Disp, SDValue &Index) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 666 | SystemZAddressingMode AM(Form, DR); |
| 667 | if (!selectAddress(Addr, AM)) |
| 668 | return false; |
| 669 | |
| 670 | getAddressOperands(AM, Addr.getValueType(), Base, Disp, Index); |
| 671 | return true; |
| 672 | } |
| 673 | |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 674 | bool SystemZDAGToDAGISel::selectBDVAddr12Only(SDValue Addr, SDValue Elem, |
| 675 | SDValue &Base, |
| 676 | SDValue &Disp, |
| 677 | SDValue &Index) const { |
| 678 | SDValue Regs[2]; |
| 679 | if (selectBDXAddr12Only(Addr, Regs[0], Disp, Regs[1]) && |
| 680 | Regs[0].getNode() && Regs[1].getNode()) { |
| 681 | for (unsigned int I = 0; I < 2; ++I) { |
| 682 | Base = Regs[I]; |
| 683 | Index = Regs[1 - I]; |
| 684 | // We can't tell here whether the index vector has the right type |
| 685 | // for the access; the caller needs to do that instead. |
| 686 | if (Index.getOpcode() == ISD::ZERO_EXTEND) |
| 687 | Index = Index.getOperand(0); |
| 688 | if (Index.getOpcode() == ISD::EXTRACT_VECTOR_ELT && |
| 689 | Index.getOperand(1) == Elem) { |
| 690 | Index = Index.getOperand(0); |
| 691 | return true; |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | return false; |
| 696 | } |
| 697 | |
Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 698 | bool SystemZDAGToDAGISel::detectOrAndInsertion(SDValue &Op, |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 699 | uint64_t InsertMask) const { |
Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 700 | // We're only interested in cases where the insertion is into some operand |
| 701 | // of Op, rather than into Op itself. The only useful case is an AND. |
| 702 | if (Op.getOpcode() != ISD::AND) |
| 703 | return false; |
| 704 | |
| 705 | // We need a constant mask. |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 706 | auto *MaskNode = dyn_cast<ConstantSDNode>(Op.getOperand(1).getNode()); |
Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 707 | if (!MaskNode) |
| 708 | return false; |
| 709 | |
| 710 | // It's not an insertion of Op.getOperand(0) if the two masks overlap. |
| 711 | uint64_t AndMask = MaskNode->getZExtValue(); |
| 712 | if (InsertMask & AndMask) |
| 713 | return false; |
| 714 | |
| 715 | // It's only an insertion if all bits are covered or are known to be zero. |
| 716 | // The inner check covers all cases but is more expensive. |
Sanjay Patel | b1f0a0f | 2016-09-14 16:05:51 +0000 | [diff] [blame] | 717 | uint64_t Used = allOnes(Op.getValueSizeInBits()); |
Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 718 | if (Used != (AndMask | InsertMask)) { |
Craig Topper | d0af7e8 | 2017-04-28 05:31:46 +0000 | [diff] [blame] | 719 | KnownBits Known; |
| 720 | CurDAG->computeKnownBits(Op.getOperand(0), Known); |
| 721 | if (Used != (AndMask | InsertMask | Known.Zero.getZExtValue())) |
Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 722 | return false; |
| 723 | } |
| 724 | |
| 725 | Op = Op.getOperand(0); |
| 726 | return true; |
| 727 | } |
| 728 | |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 729 | bool SystemZDAGToDAGISel::refineRxSBGMask(RxSBGOperands &RxSBG, |
| 730 | uint64_t Mask) const { |
Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 731 | const SystemZInstrInfo *TII = getInstrInfo(); |
Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 732 | if (RxSBG.Rotate != 0) |
| 733 | Mask = (Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate)); |
| 734 | Mask &= RxSBG.Mask; |
Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 735 | if (TII->isRxSBGMask(Mask, RxSBG.BitSize, RxSBG.Start, RxSBG.End)) { |
Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 736 | RxSBG.Mask = Mask; |
Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 737 | return true; |
| 738 | } |
Richard Sandiford | 84f54a3 | 2013-07-11 08:59:12 +0000 | [diff] [blame] | 739 | return false; |
| 740 | } |
| 741 | |
Richard Sandiford | dd7dd93 | 2013-11-26 10:53:16 +0000 | [diff] [blame] | 742 | // Return true if any bits of (RxSBG.Input & Mask) are significant. |
| 743 | static bool maskMatters(RxSBGOperands &RxSBG, uint64_t Mask) { |
| 744 | // Rotate the mask in the same way as RxSBG.Input is rotated. |
Richard Sandiford | 297f7d2 | 2013-07-18 10:14:55 +0000 | [diff] [blame] | 745 | if (RxSBG.Rotate != 0) |
Richard Sandiford | dd7dd93 | 2013-11-26 10:53:16 +0000 | [diff] [blame] | 746 | Mask = ((Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate))); |
| 747 | return (Mask & RxSBG.Mask) != 0; |
Richard Sandiford | 297f7d2 | 2013-07-18 10:14:55 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 750 | bool SystemZDAGToDAGISel::expandRxSBG(RxSBGOperands &RxSBG) const { |
Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 751 | SDValue N = RxSBG.Input; |
Richard Sandiford | 297f7d2 | 2013-07-18 10:14:55 +0000 | [diff] [blame] | 752 | unsigned Opcode = N.getOpcode(); |
| 753 | switch (Opcode) { |
Zhan Jun Liau | 0df3505 | 2016-06-22 16:16:27 +0000 | [diff] [blame] | 754 | case ISD::TRUNCATE: { |
| 755 | if (RxSBG.Opcode == SystemZ::RNSBG) |
| 756 | return false; |
Sanjay Patel | b1f0a0f | 2016-09-14 16:05:51 +0000 | [diff] [blame] | 757 | uint64_t BitSize = N.getValueSizeInBits(); |
Zhan Jun Liau | 0df3505 | 2016-06-22 16:16:27 +0000 | [diff] [blame] | 758 | uint64_t Mask = allOnes(BitSize); |
| 759 | if (!refineRxSBGMask(RxSBG, Mask)) |
| 760 | return false; |
| 761 | RxSBG.Input = N.getOperand(0); |
| 762 | return true; |
| 763 | } |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 764 | case ISD::AND: { |
Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 765 | if (RxSBG.Opcode == SystemZ::RNSBG) |
| 766 | return false; |
| 767 | |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 768 | auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode()); |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 769 | if (!MaskNode) |
| 770 | return false; |
| 771 | |
| 772 | SDValue Input = N.getOperand(0); |
| 773 | uint64_t Mask = MaskNode->getZExtValue(); |
Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 774 | if (!refineRxSBGMask(RxSBG, Mask)) { |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 775 | // If some bits of Input are already known zeros, those bits will have |
| 776 | // been removed from the mask. See if adding them back in makes the |
| 777 | // mask suitable. |
Craig Topper | d0af7e8 | 2017-04-28 05:31:46 +0000 | [diff] [blame] | 778 | KnownBits Known; |
| 779 | CurDAG->computeKnownBits(Input, Known); |
| 780 | Mask |= Known.Zero.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 | return false; |
| 783 | } |
Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 784 | RxSBG.Input = Input; |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 785 | return true; |
| 786 | } |
| 787 | |
Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 788 | case ISD::OR: { |
| 789 | if (RxSBG.Opcode != SystemZ::RNSBG) |
| 790 | return false; |
| 791 | |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 792 | auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode()); |
Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 793 | if (!MaskNode) |
| 794 | return false; |
| 795 | |
| 796 | SDValue Input = N.getOperand(0); |
| 797 | uint64_t Mask = ~MaskNode->getZExtValue(); |
| 798 | if (!refineRxSBGMask(RxSBG, Mask)) { |
| 799 | // If some bits of Input are already known ones, those bits will have |
| 800 | // been removed from the mask. See if adding them back in makes the |
| 801 | // mask suitable. |
Craig Topper | d0af7e8 | 2017-04-28 05:31:46 +0000 | [diff] [blame] | 802 | KnownBits Known; |
| 803 | CurDAG->computeKnownBits(Input, Known); |
| 804 | Mask &= ~Known.One.getZExtValue(); |
Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 805 | if (!refineRxSBGMask(RxSBG, Mask)) |
| 806 | return false; |
| 807 | } |
| 808 | RxSBG.Input = Input; |
| 809 | return true; |
| 810 | } |
| 811 | |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 812 | case ISD::ROTL: { |
Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 813 | // Any 64-bit rotate left can be merged into the RxSBG. |
Richard Sandiford | 3e38297 | 2013-10-16 13:35:13 +0000 | [diff] [blame] | 814 | if (RxSBG.BitSize != 64 || N.getValueType() != MVT::i64) |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 815 | return false; |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 816 | auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode()); |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 817 | if (!CountNode) |
| 818 | return false; |
| 819 | |
Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 820 | RxSBG.Rotate = (RxSBG.Rotate + CountNode->getZExtValue()) & 63; |
| 821 | RxSBG.Input = N.getOperand(0); |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 822 | return true; |
| 823 | } |
Simon Pilgrim | 0750c84 | 2015-08-15 13:27:30 +0000 | [diff] [blame] | 824 | |
Richard Sandiford | 220ee49 | 2013-12-20 11:49:48 +0000 | [diff] [blame] | 825 | case ISD::ANY_EXTEND: |
| 826 | // Bits above the extended operand are don't-care. |
| 827 | RxSBG.Input = N.getOperand(0); |
| 828 | return true; |
| 829 | |
Richard Sandiford | 3875cb6 | 2014-01-09 11:28:53 +0000 | [diff] [blame] | 830 | case ISD::ZERO_EXTEND: |
| 831 | if (RxSBG.Opcode != SystemZ::RNSBG) { |
| 832 | // Restrict the mask to the extended operand. |
Sanjay Patel | b1f0a0f | 2016-09-14 16:05:51 +0000 | [diff] [blame] | 833 | unsigned InnerBitSize = N.getOperand(0).getValueSizeInBits(); |
Richard Sandiford | 3875cb6 | 2014-01-09 11:28:53 +0000 | [diff] [blame] | 834 | if (!refineRxSBGMask(RxSBG, allOnes(InnerBitSize))) |
| 835 | return false; |
Richard Sandiford | 220ee49 | 2013-12-20 11:49:48 +0000 | [diff] [blame] | 836 | |
Richard Sandiford | 3875cb6 | 2014-01-09 11:28:53 +0000 | [diff] [blame] | 837 | RxSBG.Input = N.getOperand(0); |
| 838 | return true; |
| 839 | } |
Justin Bogner | cd1d5aa | 2016-08-17 20:30:52 +0000 | [diff] [blame] | 840 | LLVM_FALLTHROUGH; |
Simon Pilgrim | 0750c84 | 2015-08-15 13:27:30 +0000 | [diff] [blame] | 841 | |
Richard Sandiford | 220ee49 | 2013-12-20 11:49:48 +0000 | [diff] [blame] | 842 | case ISD::SIGN_EXTEND: { |
Richard Sandiford | 3e38297 | 2013-10-16 13:35:13 +0000 | [diff] [blame] | 843 | // Check that the extension bits are don't-care (i.e. are masked out |
| 844 | // by the final mask). |
Jonas Paulsson | 19380ba | 2017-12-06 13:53:24 +0000 | [diff] [blame] | 845 | unsigned BitSize = N.getValueSizeInBits(); |
Sanjay Patel | b1f0a0f | 2016-09-14 16:05:51 +0000 | [diff] [blame] | 846 | unsigned InnerBitSize = N.getOperand(0).getValueSizeInBits(); |
Jonas Paulsson | 19380ba | 2017-12-06 13:53:24 +0000 | [diff] [blame] | 847 | if (maskMatters(RxSBG, allOnes(BitSize) - allOnes(InnerBitSize))) { |
| 848 | // In the case where only the sign bit is active, increase Rotate with |
| 849 | // the extension width. |
| 850 | if (RxSBG.Mask == 1 && RxSBG.Rotate == 1) |
| 851 | RxSBG.Rotate += (BitSize - InnerBitSize); |
| 852 | else |
| 853 | return false; |
| 854 | } |
Richard Sandiford | 3e38297 | 2013-10-16 13:35:13 +0000 | [diff] [blame] | 855 | |
| 856 | RxSBG.Input = N.getOperand(0); |
| 857 | return true; |
| 858 | } |
| 859 | |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 860 | case ISD::SHL: { |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 861 | auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode()); |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 862 | if (!CountNode) |
| 863 | return false; |
| 864 | |
| 865 | uint64_t Count = CountNode->getZExtValue(); |
Sanjay Patel | b1f0a0f | 2016-09-14 16:05:51 +0000 | [diff] [blame] | 866 | unsigned BitSize = N.getValueSizeInBits(); |
Richard Sandiford | 3e38297 | 2013-10-16 13:35:13 +0000 | [diff] [blame] | 867 | if (Count < 1 || Count >= BitSize) |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 868 | return false; |
| 869 | |
Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 870 | if (RxSBG.Opcode == SystemZ::RNSBG) { |
| 871 | // Treat (shl X, count) as (rotl X, size-count) as long as the bottom |
| 872 | // count bits from RxSBG.Input are ignored. |
Richard Sandiford | dd7dd93 | 2013-11-26 10:53:16 +0000 | [diff] [blame] | 873 | if (maskMatters(RxSBG, allOnes(Count))) |
Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 874 | return false; |
| 875 | } else { |
| 876 | // Treat (shl X, count) as (and (rotl X, count), ~0<<count). |
Richard Sandiford | 3e38297 | 2013-10-16 13:35:13 +0000 | [diff] [blame] | 877 | if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count) << Count)) |
Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 878 | return false; |
| 879 | } |
| 880 | |
Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 881 | RxSBG.Rotate = (RxSBG.Rotate + Count) & 63; |
| 882 | RxSBG.Input = N.getOperand(0); |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 883 | return true; |
| 884 | } |
| 885 | |
Richard Sandiford | 297f7d2 | 2013-07-18 10:14:55 +0000 | [diff] [blame] | 886 | case ISD::SRL: |
| 887 | case ISD::SRA: { |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 888 | auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode()); |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 889 | if (!CountNode) |
| 890 | return false; |
| 891 | |
| 892 | uint64_t Count = CountNode->getZExtValue(); |
Sanjay Patel | b1f0a0f | 2016-09-14 16:05:51 +0000 | [diff] [blame] | 893 | unsigned BitSize = N.getValueSizeInBits(); |
Richard Sandiford | 3e38297 | 2013-10-16 13:35:13 +0000 | [diff] [blame] | 894 | if (Count < 1 || Count >= BitSize) |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 895 | return false; |
| 896 | |
Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 897 | if (RxSBG.Opcode == SystemZ::RNSBG || Opcode == ISD::SRA) { |
| 898 | // Treat (srl|sra X, count) as (rotl X, size-count) as long as the top |
| 899 | // count bits from RxSBG.Input are ignored. |
Richard Sandiford | dd7dd93 | 2013-11-26 10:53:16 +0000 | [diff] [blame] | 900 | if (maskMatters(RxSBG, allOnes(Count) << (BitSize - Count))) |
Richard Sandiford | 297f7d2 | 2013-07-18 10:14:55 +0000 | [diff] [blame] | 901 | return false; |
| 902 | } else { |
| 903 | // Treat (srl X, count), mask) as (and (rotl X, size-count), ~0>>count), |
| 904 | // which is similar to SLL above. |
Richard Sandiford | 3e38297 | 2013-10-16 13:35:13 +0000 | [diff] [blame] | 905 | if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count))) |
Richard Sandiford | 297f7d2 | 2013-07-18 10:14:55 +0000 | [diff] [blame] | 906 | return false; |
| 907 | } |
| 908 | |
Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 909 | RxSBG.Rotate = (RxSBG.Rotate - Count) & 63; |
| 910 | RxSBG.Input = N.getOperand(0); |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 911 | return true; |
| 912 | } |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 913 | default: |
| 914 | return false; |
| 915 | } |
| 916 | } |
| 917 | |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 918 | SDValue SystemZDAGToDAGISel::getUNDEF(const SDLoc &DL, EVT VT) const { |
Richard Sandiford | 3ad5a15 | 2013-10-01 14:36:20 +0000 | [diff] [blame] | 919 | SDNode *N = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, VT); |
Richard Sandiford | 84f54a3 | 2013-07-11 08:59:12 +0000 | [diff] [blame] | 920 | return SDValue(N, 0); |
| 921 | } |
| 922 | |
Benjamin Kramer | bdc4956 | 2016-06-12 15:39:02 +0000 | [diff] [blame] | 923 | SDValue SystemZDAGToDAGISel::convertTo(const SDLoc &DL, EVT VT, |
| 924 | SDValue N) const { |
Richard Sandiford | d816320 | 2013-09-13 09:12:44 +0000 | [diff] [blame] | 925 | if (N.getValueType() == MVT::i32 && VT == MVT::i64) |
Richard Sandiford | 87a4436 | 2013-09-30 10:28:35 +0000 | [diff] [blame] | 926 | return CurDAG->getTargetInsertSubreg(SystemZ::subreg_l32, |
Richard Sandiford | 3ad5a15 | 2013-10-01 14:36:20 +0000 | [diff] [blame] | 927 | DL, VT, getUNDEF(DL, MVT::i64), N); |
Richard Sandiford | d816320 | 2013-09-13 09:12:44 +0000 | [diff] [blame] | 928 | if (N.getValueType() == MVT::i64 && VT == MVT::i32) |
Richard Sandiford | 87a4436 | 2013-09-30 10:28:35 +0000 | [diff] [blame] | 929 | return CurDAG->getTargetExtractSubreg(SystemZ::subreg_l32, DL, VT, N); |
Richard Sandiford | 84f54a3 | 2013-07-11 08:59:12 +0000 | [diff] [blame] | 930 | assert(N.getValueType() == VT && "Unexpected value types"); |
| 931 | return N; |
| 932 | } |
| 933 | |
Justin Bogner | bbcd223 | 2016-05-10 21:11:26 +0000 | [diff] [blame] | 934 | bool SystemZDAGToDAGISel::tryRISBGZero(SDNode *N) { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 935 | SDLoc DL(N); |
Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 936 | EVT VT = N->getValueType(0); |
Ulrich Weigand | 77884bc | 2015-06-25 11:52:36 +0000 | [diff] [blame] | 937 | if (!VT.isInteger() || VT.getSizeInBits() > 64) |
Justin Bogner | bbcd223 | 2016-05-10 21:11:26 +0000 | [diff] [blame] | 938 | return false; |
Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 939 | RxSBGOperands RISBG(SystemZ::RISBG, SDValue(N, 0)); |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 940 | unsigned Count = 0; |
Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 941 | while (expandRxSBG(RISBG)) |
Zhan Jun Liau | 0df3505 | 2016-06-22 16:16:27 +0000 | [diff] [blame] | 942 | // The widening or narrowing is expected to be free. |
| 943 | // Counting widening or narrowing as a saved operation will result in |
| 944 | // preferring an R*SBG over a simple shift/logical instruction. |
| 945 | if (RISBG.Input.getOpcode() != ISD::ANY_EXTEND && |
| 946 | RISBG.Input.getOpcode() != ISD::TRUNCATE) |
Richard Sandiford | 3e38297 | 2013-10-16 13:35:13 +0000 | [diff] [blame] | 947 | Count += 1; |
Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 948 | if (Count == 0) |
Justin Bogner | bbcd223 | 2016-05-10 21:11:26 +0000 | [diff] [blame] | 949 | return false; |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 950 | |
Ulrich Weigand | 5dc7b67 | 2016-11-11 12:43:51 +0000 | [diff] [blame] | 951 | // Prefer to use normal shift instructions over RISBG, since they can handle |
| 952 | // all cases and are sometimes shorter. |
| 953 | if (Count == 1 && N->getOpcode() != ISD::AND) |
| 954 | return false; |
| 955 | |
| 956 | // Prefer register extensions like LLC over RISBG. Also prefer to start |
| 957 | // out with normal ANDs if one instruction would be enough. We can convert |
| 958 | // these ANDs into an RISBG later if a three-address instruction is useful. |
| 959 | if (RISBG.Rotate == 0) { |
| 960 | bool PreferAnd = false; |
| 961 | // Prefer AND for any 32-bit and-immediate operation. |
| 962 | if (VT == MVT::i32) |
| 963 | PreferAnd = true; |
| 964 | // As well as for any 64-bit operation that can be implemented via LLC(R), |
| 965 | // LLH(R), LLGT(R), or one of the and-immediate instructions. |
| 966 | else if (RISBG.Mask == 0xff || |
| 967 | RISBG.Mask == 0xffff || |
| 968 | RISBG.Mask == 0x7fffffff || |
| 969 | SystemZ::isImmLF(~RISBG.Mask) || |
| 970 | SystemZ::isImmHF(~RISBG.Mask)) |
| 971 | PreferAnd = true; |
Ulrich Weigand | 92c2c67 | 2016-11-11 12:46:28 +0000 | [diff] [blame] | 972 | // And likewise for the LLZRGF instruction, which doesn't have a register |
| 973 | // to register version. |
| 974 | else if (auto *Load = dyn_cast<LoadSDNode>(RISBG.Input)) { |
| 975 | if (Load->getMemoryVT() == MVT::i32 && |
| 976 | (Load->getExtensionType() == ISD::EXTLOAD || |
| 977 | Load->getExtensionType() == ISD::ZEXTLOAD) && |
| 978 | RISBG.Mask == 0xffffff00 && |
| 979 | Subtarget->hasLoadAndZeroRightmostByte()) |
| 980 | PreferAnd = true; |
| 981 | } |
Ulrich Weigand | 5dc7b67 | 2016-11-11 12:43:51 +0000 | [diff] [blame] | 982 | if (PreferAnd) { |
| 983 | // Replace the current node with an AND. Note that the current node |
| 984 | // might already be that same AND, in which case it is already CSE'd |
| 985 | // with it, and we must not call ReplaceNode. |
| 986 | SDValue In = convertTo(DL, VT, RISBG.Input); |
| 987 | SDValue Mask = CurDAG->getConstant(RISBG.Mask, DL, VT); |
| 988 | SDValue New = CurDAG->getNode(ISD::AND, DL, VT, In, Mask); |
| 989 | if (N != New.getNode()) { |
| 990 | insertDAGNode(CurDAG, N, Mask); |
| 991 | insertDAGNode(CurDAG, N, New); |
| 992 | ReplaceNode(N, New.getNode()); |
| 993 | N = New.getNode(); |
Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 994 | } |
Ulrich Weigand | 5dc7b67 | 2016-11-11 12:43:51 +0000 | [diff] [blame] | 995 | // Now, select the machine opcode to implement this operation. |
| 996 | SelectCode(N); |
| 997 | return true; |
Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 998 | } |
Simon Pilgrim | 0750c84 | 2015-08-15 13:27:30 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
Richard Sandiford | 3ad5a15 | 2013-10-01 14:36:20 +0000 | [diff] [blame] | 1001 | unsigned Opcode = SystemZ::RISBG; |
Ulrich Weigand | 371d10a | 2015-03-31 12:58:17 +0000 | [diff] [blame] | 1002 | // Prefer RISBGN if available, since it does not clobber CC. |
| 1003 | if (Subtarget->hasMiscellaneousExtensions()) |
| 1004 | Opcode = SystemZ::RISBGN; |
Richard Sandiford | 3ad5a15 | 2013-10-01 14:36:20 +0000 | [diff] [blame] | 1005 | EVT OpcodeVT = MVT::i64; |
Ulrich Weigand | 55b8590 | 2017-11-14 19:20:46 +0000 | [diff] [blame] | 1006 | if (VT == MVT::i32 && Subtarget->hasHighWord() && |
| 1007 | // We can only use the 32-bit instructions if all source bits are |
| 1008 | // in the low 32 bits without wrapping, both after rotation (because |
| 1009 | // of the smaller range for Start and End) and before rotation |
| 1010 | // (because the input value is truncated). |
| 1011 | RISBG.Start >= 32 && RISBG.End >= RISBG.Start && |
| 1012 | ((RISBG.Start + RISBG.Rotate) & 63) >= 32 && |
| 1013 | ((RISBG.End + RISBG.Rotate) & 63) >= |
| 1014 | ((RISBG.Start + RISBG.Rotate) & 63)) { |
Richard Sandiford | 3ad5a15 | 2013-10-01 14:36:20 +0000 | [diff] [blame] | 1015 | Opcode = SystemZ::RISBMux; |
| 1016 | OpcodeVT = MVT::i32; |
| 1017 | RISBG.Start &= 31; |
| 1018 | RISBG.End &= 31; |
| 1019 | } |
Richard Sandiford | 84f54a3 | 2013-07-11 08:59:12 +0000 | [diff] [blame] | 1020 | SDValue Ops[5] = { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1021 | getUNDEF(DL, OpcodeVT), |
| 1022 | convertTo(DL, OpcodeVT, RISBG.Input), |
| 1023 | CurDAG->getTargetConstant(RISBG.Start, DL, MVT::i32), |
| 1024 | CurDAG->getTargetConstant(RISBG.End | 128, DL, MVT::i32), |
| 1025 | CurDAG->getTargetConstant(RISBG.Rotate, DL, MVT::i32) |
Richard Sandiford | 84f54a3 | 2013-07-11 08:59:12 +0000 | [diff] [blame] | 1026 | }; |
Justin Bogner | bbcd223 | 2016-05-10 21:11:26 +0000 | [diff] [blame] | 1027 | SDValue New = convertTo( |
| 1028 | DL, VT, SDValue(CurDAG->getMachineNode(Opcode, DL, OpcodeVT, Ops), 0)); |
| 1029 | ReplaceUses(N, New.getNode()); |
| 1030 | CurDAG->RemoveDeadNode(N); |
| 1031 | return true; |
Richard Sandiford | 84f54a3 | 2013-07-11 08:59:12 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1034 | bool SystemZDAGToDAGISel::tryRxSBG(SDNode *N, unsigned Opcode) { |
Ulrich Weigand | 77884bc | 2015-06-25 11:52:36 +0000 | [diff] [blame] | 1035 | SDLoc DL(N); |
| 1036 | EVT VT = N->getValueType(0); |
| 1037 | if (!VT.isInteger() || VT.getSizeInBits() > 64) |
Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1038 | return false; |
Richard Sandiford | 7878b85 | 2013-07-18 10:06:15 +0000 | [diff] [blame] | 1039 | // 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] | 1040 | // and see which goes deepest. |
Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 1041 | RxSBGOperands RxSBG[] = { |
| 1042 | RxSBGOperands(Opcode, N->getOperand(0)), |
| 1043 | RxSBGOperands(Opcode, N->getOperand(1)) |
| 1044 | }; |
Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 1045 | unsigned Count[] = { 0, 0 }; |
| 1046 | for (unsigned I = 0; I < 2; ++I) |
Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 1047 | while (expandRxSBG(RxSBG[I])) |
Zhan Jun Liau | 0df3505 | 2016-06-22 16:16:27 +0000 | [diff] [blame] | 1048 | // The widening or narrowing is expected to be free. |
| 1049 | // Counting widening or narrowing as a saved operation will result in |
| 1050 | // preferring an R*SBG over a simple shift/logical instruction. |
| 1051 | if (RxSBG[I].Input.getOpcode() != ISD::ANY_EXTEND && |
| 1052 | RxSBG[I].Input.getOpcode() != ISD::TRUNCATE) |
Richard Sandiford | 3e38297 | 2013-10-16 13:35:13 +0000 | [diff] [blame] | 1053 | Count[I] += 1; |
Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 1054 | |
| 1055 | // Do nothing if neither operand is suitable. |
| 1056 | if (Count[0] == 0 && Count[1] == 0) |
Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1057 | return false; |
Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 1058 | |
| 1059 | // Pick the deepest second operand. |
| 1060 | unsigned I = Count[0] > Count[1] ? 0 : 1; |
| 1061 | SDValue Op0 = N->getOperand(I ^ 1); |
| 1062 | |
| 1063 | // Prefer IC for character insertions from memory. |
Richard Sandiford | 7878b85 | 2013-07-18 10:06:15 +0000 | [diff] [blame] | 1064 | if (Opcode == SystemZ::ROSBG && (RxSBG[I].Mask & 0xff) == 0) |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1065 | if (auto *Load = dyn_cast<LoadSDNode>(Op0.getNode())) |
Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 1066 | if (Load->getMemoryVT() == MVT::i8) |
Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1067 | return false; |
Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 1068 | |
| 1069 | // See whether we can avoid an AND in the first operand by converting |
| 1070 | // ROSBG to RISBG. |
Ulrich Weigand | 371d10a | 2015-03-31 12:58:17 +0000 | [diff] [blame] | 1071 | if (Opcode == SystemZ::ROSBG && detectOrAndInsertion(Op0, RxSBG[I].Mask)) { |
Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 1072 | Opcode = SystemZ::RISBG; |
Ulrich Weigand | 371d10a | 2015-03-31 12:58:17 +0000 | [diff] [blame] | 1073 | // Prefer RISBGN if available, since it does not clobber CC. |
| 1074 | if (Subtarget->hasMiscellaneousExtensions()) |
| 1075 | Opcode = SystemZ::RISBGN; |
| 1076 | } |
| 1077 | |
Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 1078 | SDValue Ops[5] = { |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1079 | convertTo(DL, MVT::i64, Op0), |
| 1080 | convertTo(DL, MVT::i64, RxSBG[I].Input), |
| 1081 | CurDAG->getTargetConstant(RxSBG[I].Start, DL, MVT::i32), |
| 1082 | CurDAG->getTargetConstant(RxSBG[I].End, DL, MVT::i32), |
| 1083 | CurDAG->getTargetConstant(RxSBG[I].Rotate, DL, MVT::i32) |
Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 1084 | }; |
Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1085 | SDValue New = convertTo( |
| 1086 | DL, VT, SDValue(CurDAG->getMachineNode(Opcode, DL, MVT::i64, Ops), 0)); |
| 1087 | ReplaceNode(N, New.getNode()); |
| 1088 | return true; |
Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
Justin Bogner | ffb273d | 2016-05-09 23:54:23 +0000 | [diff] [blame] | 1091 | void SystemZDAGToDAGISel::splitLargeImmediate(unsigned Opcode, SDNode *Node, |
| 1092 | SDValue Op0, uint64_t UpperVal, |
| 1093 | uint64_t LowerVal) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1094 | EVT VT = Node->getValueType(0); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 1095 | SDLoc DL(Node); |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1096 | SDValue Upper = CurDAG->getConstant(UpperVal, DL, VT); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1097 | if (Op0.getNode()) |
| 1098 | Upper = CurDAG->getNode(Opcode, DL, VT, Op0, Upper); |
Justin Bogner | ffb273d | 2016-05-09 23:54:23 +0000 | [diff] [blame] | 1099 | |
| 1100 | { |
| 1101 | // When we haven't passed in Op0, Upper will be a constant. In order to |
| 1102 | // prevent folding back to the large immediate in `Or = getNode(...)` we run |
| 1103 | // SelectCode first and end up with an opaque machine node. This means that |
| 1104 | // we need to use a handle to keep track of Upper in case it gets CSE'd by |
| 1105 | // SelectCode. |
| 1106 | // |
| 1107 | // Note that in the case where Op0 is passed in we could just call |
| 1108 | // SelectCode(Upper) later, along with the SelectCode(Or), and avoid needing |
| 1109 | // the handle at all, but it's fine to do it here. |
| 1110 | // |
| 1111 | // TODO: This is a pretty hacky way to do this. Can we do something that |
| 1112 | // doesn't require a two paragraph explanation? |
| 1113 | HandleSDNode Handle(Upper); |
| 1114 | SelectCode(Upper.getNode()); |
| 1115 | Upper = Handle.getValue(); |
| 1116 | } |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1117 | |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1118 | SDValue Lower = CurDAG->getConstant(LowerVal, DL, VT); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1119 | SDValue Or = CurDAG->getNode(Opcode, DL, VT, Upper, Lower); |
Justin Bogner | ffb273d | 2016-05-09 23:54:23 +0000 | [diff] [blame] | 1120 | |
| 1121 | ReplaceUses(Node, Or.getNode()); |
| 1122 | CurDAG->RemoveDeadNode(Node); |
| 1123 | |
| 1124 | SelectCode(Or.getNode()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1127 | bool SystemZDAGToDAGISel::tryGather(SDNode *N, unsigned Opcode) { |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1128 | SDValue ElemV = N->getOperand(2); |
| 1129 | auto *ElemN = dyn_cast<ConstantSDNode>(ElemV); |
| 1130 | if (!ElemN) |
Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1131 | return false; |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1132 | |
| 1133 | unsigned Elem = ElemN->getZExtValue(); |
| 1134 | EVT VT = N->getValueType(0); |
| 1135 | if (Elem >= VT.getVectorNumElements()) |
Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1136 | return false; |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1137 | |
| 1138 | auto *Load = dyn_cast<LoadSDNode>(N->getOperand(1)); |
| 1139 | if (!Load || !Load->hasOneUse()) |
Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1140 | return false; |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1141 | if (Load->getMemoryVT().getSizeInBits() != |
| 1142 | Load->getValueType(0).getSizeInBits()) |
Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1143 | return false; |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1144 | |
| 1145 | SDValue Base, Disp, Index; |
| 1146 | if (!selectBDVAddr12Only(Load->getBasePtr(), ElemV, Base, Disp, Index) || |
| 1147 | Index.getValueType() != VT.changeVectorElementTypeToInteger()) |
Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1148 | return false; |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1149 | |
| 1150 | SDLoc DL(Load); |
| 1151 | SDValue Ops[] = { |
| 1152 | N->getOperand(0), Base, Disp, Index, |
| 1153 | CurDAG->getTargetConstant(Elem, DL, MVT::i32), Load->getChain() |
| 1154 | }; |
| 1155 | SDNode *Res = CurDAG->getMachineNode(Opcode, DL, VT, MVT::Other, Ops); |
| 1156 | ReplaceUses(SDValue(Load, 1), SDValue(Res, 1)); |
Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1157 | ReplaceNode(N, Res); |
| 1158 | return true; |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1161 | bool SystemZDAGToDAGISel::tryScatter(StoreSDNode *Store, unsigned Opcode) { |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1162 | SDValue Value = Store->getValue(); |
| 1163 | if (Value.getOpcode() != ISD::EXTRACT_VECTOR_ELT) |
Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1164 | return false; |
Sanjay Patel | b1f0a0f | 2016-09-14 16:05:51 +0000 | [diff] [blame] | 1165 | if (Store->getMemoryVT().getSizeInBits() != Value.getValueSizeInBits()) |
Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1166 | return false; |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1167 | |
| 1168 | SDValue ElemV = Value.getOperand(1); |
| 1169 | auto *ElemN = dyn_cast<ConstantSDNode>(ElemV); |
| 1170 | if (!ElemN) |
Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1171 | return false; |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1172 | |
| 1173 | SDValue Vec = Value.getOperand(0); |
| 1174 | EVT VT = Vec.getValueType(); |
| 1175 | unsigned Elem = ElemN->getZExtValue(); |
| 1176 | if (Elem >= VT.getVectorNumElements()) |
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 Base, Disp, Index; |
| 1180 | if (!selectBDVAddr12Only(Store->getBasePtr(), ElemV, Base, Disp, Index) || |
| 1181 | Index.getValueType() != VT.changeVectorElementTypeToInteger()) |
Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1182 | return false; |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1183 | |
| 1184 | SDLoc DL(Store); |
| 1185 | SDValue Ops[] = { |
| 1186 | Vec, Base, Disp, Index, CurDAG->getTargetConstant(Elem, DL, MVT::i32), |
| 1187 | Store->getChain() |
| 1188 | }; |
Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1189 | ReplaceNode(Store, CurDAG->getMachineNode(Opcode, DL, MVT::Other, Ops)); |
| 1190 | return true; |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
Richard Sandiford | 067817e | 2013-09-27 15:29:20 +0000 | [diff] [blame] | 1193 | bool SystemZDAGToDAGISel::canUseBlockOperation(StoreSDNode *Store, |
| 1194 | LoadSDNode *Load) const { |
Richard Sandiford | 178273a | 2013-09-05 10:36:45 +0000 | [diff] [blame] | 1195 | // Check that the two memory operands have the same size. |
| 1196 | if (Load->getMemoryVT() != Store->getMemoryVT()) |
Richard Sandiford | 9784649 | 2013-07-09 09:46:39 +0000 | [diff] [blame] | 1197 | return false; |
| 1198 | |
Richard Sandiford | 178273a | 2013-09-05 10:36:45 +0000 | [diff] [blame] | 1199 | // Volatility stops an access from being decomposed. |
| 1200 | if (Load->isVolatile() || Store->isVolatile()) |
| 1201 | return false; |
Richard Sandiford | 9784649 | 2013-07-09 09:46:39 +0000 | [diff] [blame] | 1202 | |
| 1203 | // There's no chance of overlap if the load is invariant. |
Justin Lebar | adbf09e | 2016-09-11 01:38:58 +0000 | [diff] [blame] | 1204 | if (Load->isInvariant() && Load->isDereferenceable()) |
Richard Sandiford | 9784649 | 2013-07-09 09:46:39 +0000 | [diff] [blame] | 1205 | return true; |
| 1206 | |
Richard Sandiford | 9784649 | 2013-07-09 09:46:39 +0000 | [diff] [blame] | 1207 | // Otherwise we need to check whether there's an alias. |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 1208 | const Value *V1 = Load->getMemOperand()->getValue(); |
| 1209 | const Value *V2 = Store->getMemOperand()->getValue(); |
Richard Sandiford | 9784649 | 2013-07-09 09:46:39 +0000 | [diff] [blame] | 1210 | if (!V1 || !V2) |
| 1211 | return false; |
| 1212 | |
Richard Sandiford | 067817e | 2013-09-27 15:29:20 +0000 | [diff] [blame] | 1213 | // Reject equality. |
| 1214 | uint64_t Size = Load->getMemoryVT().getStoreSize(); |
Richard Sandiford | 9784649 | 2013-07-09 09:46:39 +0000 | [diff] [blame] | 1215 | int64_t End1 = Load->getSrcValueOffset() + Size; |
| 1216 | int64_t End2 = Store->getSrcValueOffset() + Size; |
Richard Sandiford | 067817e | 2013-09-27 15:29:20 +0000 | [diff] [blame] | 1217 | if (V1 == V2 && End1 == End2) |
| 1218 | return false; |
| 1219 | |
Chandler Carruth | ac80dc7 | 2015-06-17 07:18:54 +0000 | [diff] [blame] | 1220 | return !AA->alias(MemoryLocation(V1, End1, Load->getAAInfo()), |
| 1221 | MemoryLocation(V2, End2, Store->getAAInfo())); |
Richard Sandiford | 9784649 | 2013-07-09 09:46:39 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
Richard Sandiford | 178273a | 2013-09-05 10:36:45 +0000 | [diff] [blame] | 1224 | bool SystemZDAGToDAGISel::storeLoadCanUseMVC(SDNode *N) const { |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1225 | auto *Store = cast<StoreSDNode>(N); |
| 1226 | auto *Load = cast<LoadSDNode>(Store->getValue()); |
Richard Sandiford | 178273a | 2013-09-05 10:36:45 +0000 | [diff] [blame] | 1227 | |
| 1228 | // Prefer not to use MVC if either address can use ... RELATIVE LONG |
| 1229 | // instructions. |
| 1230 | uint64_t Size = Load->getMemoryVT().getStoreSize(); |
| 1231 | if (Size > 1 && Size <= 8) { |
| 1232 | // Prefer LHRL, LRL and LGRL. |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 1233 | if (SystemZISD::isPCREL(Load->getBasePtr().getOpcode())) |
Richard Sandiford | 178273a | 2013-09-05 10:36:45 +0000 | [diff] [blame] | 1234 | return false; |
| 1235 | // Prefer STHRL, STRL and STGRL. |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 1236 | if (SystemZISD::isPCREL(Store->getBasePtr().getOpcode())) |
Richard Sandiford | 178273a | 2013-09-05 10:36:45 +0000 | [diff] [blame] | 1237 | return false; |
| 1238 | } |
| 1239 | |
Richard Sandiford | 067817e | 2013-09-27 15:29:20 +0000 | [diff] [blame] | 1240 | return canUseBlockOperation(Store, Load); |
Richard Sandiford | 178273a | 2013-09-05 10:36:45 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | bool SystemZDAGToDAGISel::storeLoadCanUseBlockBinary(SDNode *N, |
| 1244 | unsigned I) const { |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1245 | auto *StoreA = cast<StoreSDNode>(N); |
| 1246 | auto *LoadA = cast<LoadSDNode>(StoreA->getValue().getOperand(1 - I)); |
| 1247 | auto *LoadB = cast<LoadSDNode>(StoreA->getValue().getOperand(I)); |
Richard Sandiford | 067817e | 2013-09-27 15:29:20 +0000 | [diff] [blame] | 1248 | return !LoadA->isVolatile() && canUseBlockOperation(StoreA, LoadB); |
Richard Sandiford | 178273a | 2013-09-05 10:36:45 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1251 | void SystemZDAGToDAGISel::Select(SDNode *Node) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1252 | // Dump information about the Node being selected |
| 1253 | DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n"); |
| 1254 | |
| 1255 | // If we have a custom node, we already have selected! |
| 1256 | if (Node->isMachineOpcode()) { |
| 1257 | DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n"); |
Tim Northover | 31d093c | 2013-09-22 08:21:56 +0000 | [diff] [blame] | 1258 | Node->setNodeId(-1); |
Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1259 | return; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | unsigned Opcode = Node->getOpcode(); |
| 1263 | switch (Opcode) { |
| 1264 | case ISD::OR: |
Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 1265 | if (Node->getOperand(1).getOpcode() != ISD::Constant) |
Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1266 | if (tryRxSBG(Node, SystemZ::ROSBG)) |
| 1267 | return; |
Richard Sandiford | 7878b85 | 2013-07-18 10:06:15 +0000 | [diff] [blame] | 1268 | goto or_xor; |
| 1269 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1270 | case ISD::XOR: |
Richard Sandiford | 7878b85 | 2013-07-18 10:06:15 +0000 | [diff] [blame] | 1271 | if (Node->getOperand(1).getOpcode() != ISD::Constant) |
Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1272 | if (tryRxSBG(Node, SystemZ::RXSBG)) |
| 1273 | return; |
Richard Sandiford | 7878b85 | 2013-07-18 10:06:15 +0000 | [diff] [blame] | 1274 | // Fall through. |
| 1275 | or_xor: |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1276 | // 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] | 1277 | // split the operation into two. If both operands here happen to be |
| 1278 | // constant, leave this to common code to optimize. |
| 1279 | if (Node->getValueType(0) == MVT::i64 && |
| 1280 | Node->getOperand(0).getOpcode() != ISD::Constant) |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1281 | if (auto *Op1 = dyn_cast<ConstantSDNode>(Node->getOperand(1))) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1282 | uint64_t Val = Op1->getZExtValue(); |
Justin Bogner | ffb273d | 2016-05-09 23:54:23 +0000 | [diff] [blame] | 1283 | if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val)) { |
| 1284 | splitLargeImmediate(Opcode, Node, Node->getOperand(0), |
| 1285 | Val - uint32_t(Val), uint32_t(Val)); |
Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1286 | return; |
Justin Bogner | ffb273d | 2016-05-09 23:54:23 +0000 | [diff] [blame] | 1287 | } |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1288 | } |
| 1289 | break; |
| 1290 | |
Richard Sandiford | 84f54a3 | 2013-07-11 08:59:12 +0000 | [diff] [blame] | 1291 | case ISD::AND: |
Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 1292 | if (Node->getOperand(1).getOpcode() != ISD::Constant) |
Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1293 | if (tryRxSBG(Node, SystemZ::RNSBG)) |
| 1294 | return; |
Justin Bogner | cd1d5aa | 2016-08-17 20:30:52 +0000 | [diff] [blame] | 1295 | LLVM_FALLTHROUGH; |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 1296 | case ISD::ROTL: |
| 1297 | case ISD::SHL: |
| 1298 | case ISD::SRL: |
Richard Sandiford | 220ee49 | 2013-12-20 11:49:48 +0000 | [diff] [blame] | 1299 | case ISD::ZERO_EXTEND: |
Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1300 | if (tryRISBGZero(Node)) |
| 1301 | return; |
Richard Sandiford | 84f54a3 | 2013-07-11 08:59:12 +0000 | [diff] [blame] | 1302 | break; |
| 1303 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1304 | case ISD::Constant: |
| 1305 | // If this is a 64-bit constant that is out of the range of LLILF, |
| 1306 | // LLIHF and LGFI, split it into two 32-bit pieces. |
| 1307 | if (Node->getValueType(0) == MVT::i64) { |
| 1308 | uint64_t Val = cast<ConstantSDNode>(Node)->getZExtValue(); |
Justin Bogner | ffb273d | 2016-05-09 23:54:23 +0000 | [diff] [blame] | 1309 | if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val) && !isInt<32>(Val)) { |
| 1310 | splitLargeImmediate(ISD::OR, Node, SDValue(), Val - uint32_t(Val), |
| 1311 | uint32_t(Val)); |
Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1312 | return; |
Justin Bogner | ffb273d | 2016-05-09 23:54:23 +0000 | [diff] [blame] | 1313 | } |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1314 | } |
| 1315 | break; |
| 1316 | |
Richard Sandiford | ee83438 | 2013-07-31 12:38:08 +0000 | [diff] [blame] | 1317 | case SystemZISD::SELECT_CCMASK: { |
| 1318 | SDValue Op0 = Node->getOperand(0); |
| 1319 | SDValue Op1 = Node->getOperand(1); |
| 1320 | // 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] | 1321 | // conditional load. Likewise for constants in range for LOCHI. |
| 1322 | if ((Op1.getOpcode() == ISD::LOAD && Op0.getOpcode() != ISD::LOAD) || |
| 1323 | (Subtarget->hasLoadStoreOnCond2() && |
| 1324 | Node->getValueType(0).isInteger() && |
| 1325 | Op1.getOpcode() == ISD::Constant && |
| 1326 | isInt<16>(cast<ConstantSDNode>(Op1)->getSExtValue()) && |
| 1327 | !(Op0.getOpcode() == ISD::Constant && |
| 1328 | isInt<16>(cast<ConstantSDNode>(Op0)->getSExtValue())))) { |
Richard Sandiford | ee83438 | 2013-07-31 12:38:08 +0000 | [diff] [blame] | 1329 | SDValue CCValid = Node->getOperand(2); |
| 1330 | SDValue CCMask = Node->getOperand(3); |
| 1331 | uint64_t ConstCCValid = |
| 1332 | cast<ConstantSDNode>(CCValid.getNode())->getZExtValue(); |
| 1333 | uint64_t ConstCCMask = |
| 1334 | cast<ConstantSDNode>(CCMask.getNode())->getZExtValue(); |
| 1335 | // Invert the condition. |
Sergey Dmitrouk | 842a51b | 2015-04-28 14:05:47 +0000 | [diff] [blame] | 1336 | CCMask = CurDAG->getConstant(ConstCCValid ^ ConstCCMask, SDLoc(Node), |
Richard Sandiford | ee83438 | 2013-07-31 12:38:08 +0000 | [diff] [blame] | 1337 | CCMask.getValueType()); |
| 1338 | SDValue Op4 = Node->getOperand(4); |
| 1339 | Node = CurDAG->UpdateNodeOperands(Node, Op1, Op0, CCValid, CCMask, Op4); |
| 1340 | } |
| 1341 | break; |
| 1342 | } |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1343 | |
| 1344 | case ISD::INSERT_VECTOR_ELT: { |
| 1345 | EVT VT = Node->getValueType(0); |
Sanjay Patel | 1ed771f | 2016-09-14 16:37:15 +0000 | [diff] [blame] | 1346 | unsigned ElemBitSize = VT.getScalarSizeInBits(); |
Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1347 | if (ElemBitSize == 32) { |
| 1348 | if (tryGather(Node, SystemZ::VGEF)) |
| 1349 | return; |
| 1350 | } else if (ElemBitSize == 64) { |
| 1351 | if (tryGather(Node, SystemZ::VGEG)) |
| 1352 | return; |
| 1353 | } |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1354 | break; |
| 1355 | } |
| 1356 | |
| 1357 | case ISD::STORE: { |
| 1358 | auto *Store = cast<StoreSDNode>(Node); |
Sanjay Patel | b1f0a0f | 2016-09-14 16:05:51 +0000 | [diff] [blame] | 1359 | unsigned ElemBitSize = Store->getValue().getValueSizeInBits(); |
Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1360 | if (ElemBitSize == 32) { |
| 1361 | if (tryScatter(Store, SystemZ::VSCEF)) |
| 1362 | return; |
| 1363 | } else if (ElemBitSize == 64) { |
| 1364 | if (tryScatter(Store, SystemZ::VSCEG)) |
| 1365 | return; |
| 1366 | } |
Ulrich Weigand | ce4c109 | 2015-05-05 19:25:42 +0000 | [diff] [blame] | 1367 | break; |
| 1368 | } |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1369 | } |
| 1370 | |
Justin Bogner | 9b34e8a | 2016-05-13 22:42:08 +0000 | [diff] [blame] | 1371 | SelectCode(Node); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | bool SystemZDAGToDAGISel:: |
| 1375 | SelectInlineAsmMemoryOperand(const SDValue &Op, |
Daniel Sanders | 60f1db0 | 2015-03-13 12:45:09 +0000 | [diff] [blame] | 1376 | unsigned ConstraintID, |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1377 | std::vector<SDValue> &OutOps) { |
Ulrich Weigand | daae87aa | 2016-06-13 14:24:05 +0000 | [diff] [blame] | 1378 | SystemZAddressingMode::AddrForm Form; |
| 1379 | SystemZAddressingMode::DispRange DispRange; |
Ulrich Weigand | 7956461 | 2016-06-09 15:19:16 +0000 | [diff] [blame] | 1380 | SDValue Base, Disp, Index; |
| 1381 | |
Daniel Sanders | 2eeace2 | 2015-03-17 16:16:14 +0000 | [diff] [blame] | 1382 | switch(ConstraintID) { |
| 1383 | default: |
| 1384 | llvm_unreachable("Unexpected asm memory constraint"); |
| 1385 | case InlineAsm::Constraint_i: |
Daniel Sanders | 2eeace2 | 2015-03-17 16:16:14 +0000 | [diff] [blame] | 1386 | case InlineAsm::Constraint_Q: |
Ulrich Weigand | daae87aa | 2016-06-13 14:24:05 +0000 | [diff] [blame] | 1387 | // Accept an address with a short displacement, but no index. |
| 1388 | Form = SystemZAddressingMode::FormBD; |
| 1389 | DispRange = SystemZAddressingMode::Disp12Only; |
| 1390 | break; |
Daniel Sanders | 2eeace2 | 2015-03-17 16:16:14 +0000 | [diff] [blame] | 1391 | case InlineAsm::Constraint_R: |
Ulrich Weigand | daae87aa | 2016-06-13 14:24:05 +0000 | [diff] [blame] | 1392 | // Accept an address with a short displacement and an index. |
| 1393 | Form = SystemZAddressingMode::FormBDXNormal; |
| 1394 | DispRange = SystemZAddressingMode::Disp12Only; |
Daniel Sanders | 2eeace2 | 2015-03-17 16:16:14 +0000 | [diff] [blame] | 1395 | break; |
Ulrich Weigand | 7956461 | 2016-06-09 15:19:16 +0000 | [diff] [blame] | 1396 | case InlineAsm::Constraint_S: |
Ulrich Weigand | daae87aa | 2016-06-13 14:24:05 +0000 | [diff] [blame] | 1397 | // Accept an address with a long displacement, but no index. |
| 1398 | Form = SystemZAddressingMode::FormBD; |
| 1399 | DispRange = SystemZAddressingMode::Disp20Only; |
| 1400 | break; |
Ulrich Weigand | 7956461 | 2016-06-09 15:19:16 +0000 | [diff] [blame] | 1401 | case InlineAsm::Constraint_T: |
| 1402 | case InlineAsm::Constraint_m: |
Ulrich Weigand | d39e9dc | 2017-11-09 16:31:57 +0000 | [diff] [blame] | 1403 | case InlineAsm::Constraint_o: |
Ulrich Weigand | daae87aa | 2016-06-13 14:24:05 +0000 | [diff] [blame] | 1404 | // Accept an address with a long displacement and an index. |
| 1405 | // 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] | 1406 | // We don't really have any special handling of "offsettable" |
| 1407 | // memory addresses, so just treat o the same as m. |
Ulrich Weigand | daae87aa | 2016-06-13 14:24:05 +0000 | [diff] [blame] | 1408 | Form = SystemZAddressingMode::FormBDXNormal; |
| 1409 | DispRange = SystemZAddressingMode::Disp20Only; |
Ulrich Weigand | 7956461 | 2016-06-09 15:19:16 +0000 | [diff] [blame] | 1410 | break; |
Daniel Sanders | 2eeace2 | 2015-03-17 16:16:14 +0000 | [diff] [blame] | 1411 | } |
Ulrich Weigand | daae87aa | 2016-06-13 14:24:05 +0000 | [diff] [blame] | 1412 | |
| 1413 | if (selectBDXAddr(Form, DispRange, Op, Base, Disp, Index)) { |
Zhan Jun Liau | cf2f4b3 | 2016-08-18 21:44:15 +0000 | [diff] [blame] | 1414 | const TargetRegisterClass *TRC = |
| 1415 | Subtarget->getRegisterInfo()->getPointerRegClass(*MF); |
| 1416 | SDLoc DL(Base); |
| 1417 | SDValue RC = CurDAG->getTargetConstant(TRC->getID(), DL, MVT::i32); |
| 1418 | |
| 1419 | // Make sure that the base address doesn't go into %r0. |
| 1420 | // If it's a TargetFrameIndex or a fixed register, we shouldn't do anything. |
| 1421 | if (Base.getOpcode() != ISD::TargetFrameIndex && |
| 1422 | Base.getOpcode() != ISD::Register) { |
| 1423 | Base = |
| 1424 | SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, |
| 1425 | DL, Base.getValueType(), |
| 1426 | Base, RC), 0); |
| 1427 | } |
| 1428 | |
| 1429 | // Make sure that the index register isn't assigned to %r0 either. |
| 1430 | if (Index.getOpcode() != ISD::Register) { |
| 1431 | Index = |
| 1432 | SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, |
| 1433 | DL, Index.getValueType(), |
| 1434 | Index, RC), 0); |
| 1435 | } |
| 1436 | |
Ulrich Weigand | daae87aa | 2016-06-13 14:24:05 +0000 | [diff] [blame] | 1437 | OutOps.push_back(Base); |
| 1438 | OutOps.push_back(Disp); |
| 1439 | OutOps.push_back(Index); |
| 1440 | return false; |
| 1441 | } |
| 1442 | |
Daniel Sanders | 2eeace2 | 2015-03-17 16:16:14 +0000 | [diff] [blame] | 1443 | return true; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1444 | } |
Ulrich Weigand | 849a59f | 2018-01-19 20:52:04 +0000 | [diff] [blame^] | 1445 | |
| 1446 | namespace { |
| 1447 | // Represents a sequence for extracting a 0/1 value from an IPM result: |
| 1448 | // (((X ^ XORValue) + AddValue) >> Bit) |
| 1449 | struct IPMConversion { |
| 1450 | IPMConversion(unsigned xorValue, int64_t addValue, unsigned bit) |
| 1451 | : XORValue(xorValue), AddValue(addValue), Bit(bit) {} |
| 1452 | |
| 1453 | int64_t XORValue; |
| 1454 | int64_t AddValue; |
| 1455 | unsigned Bit; |
| 1456 | }; |
| 1457 | } // end anonymous namespace |
| 1458 | |
| 1459 | // Return a sequence for getting a 1 from an IPM result when CC has a |
| 1460 | // value in CCMask and a 0 when CC has a value in CCValid & ~CCMask. |
| 1461 | // The handling of CC values outside CCValid doesn't matter. |
| 1462 | static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) { |
| 1463 | // Deal with cases where the result can be taken directly from a bit |
| 1464 | // of the IPM result. |
| 1465 | if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3))) |
| 1466 | return IPMConversion(0, 0, SystemZ::IPM_CC); |
| 1467 | if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3))) |
| 1468 | return IPMConversion(0, 0, SystemZ::IPM_CC + 1); |
| 1469 | |
| 1470 | // Deal with cases where we can add a value to force the sign bit |
| 1471 | // to contain the right value. Putting the bit in 31 means we can |
| 1472 | // use SRL rather than RISBG(L), and also makes it easier to get a |
| 1473 | // 0/-1 value, so it has priority over the other tests below. |
| 1474 | // |
| 1475 | // These sequences rely on the fact that the upper two bits of the |
| 1476 | // IPM result are zero. |
| 1477 | uint64_t TopBit = uint64_t(1) << 31; |
| 1478 | if (CCMask == (CCValid & SystemZ::CCMASK_0)) |
| 1479 | return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31); |
| 1480 | if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1))) |
| 1481 | return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31); |
| 1482 | if (CCMask == (CCValid & (SystemZ::CCMASK_0 |
| 1483 | | SystemZ::CCMASK_1 |
| 1484 | | SystemZ::CCMASK_2))) |
| 1485 | return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31); |
| 1486 | if (CCMask == (CCValid & SystemZ::CCMASK_3)) |
| 1487 | return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31); |
| 1488 | if (CCMask == (CCValid & (SystemZ::CCMASK_1 |
| 1489 | | SystemZ::CCMASK_2 |
| 1490 | | SystemZ::CCMASK_3))) |
| 1491 | return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31); |
| 1492 | |
| 1493 | // Next try inverting the value and testing a bit. 0/1 could be |
| 1494 | // handled this way too, but we dealt with that case above. |
| 1495 | if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2))) |
| 1496 | return IPMConversion(-1, 0, SystemZ::IPM_CC); |
| 1497 | |
| 1498 | // Handle cases where adding a value forces a non-sign bit to contain |
| 1499 | // the right value. |
| 1500 | if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2))) |
| 1501 | return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1); |
| 1502 | if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3))) |
| 1503 | return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1); |
| 1504 | |
| 1505 | // The remaining cases are 1, 2, 0/1/3 and 0/2/3. All these are |
| 1506 | // can be done by inverting the low CC bit and applying one of the |
| 1507 | // sign-based extractions above. |
| 1508 | if (CCMask == (CCValid & SystemZ::CCMASK_1)) |
| 1509 | return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31); |
| 1510 | if (CCMask == (CCValid & SystemZ::CCMASK_2)) |
| 1511 | return IPMConversion(1 << SystemZ::IPM_CC, |
| 1512 | TopBit - (3 << SystemZ::IPM_CC), 31); |
| 1513 | if (CCMask == (CCValid & (SystemZ::CCMASK_0 |
| 1514 | | SystemZ::CCMASK_1 |
| 1515 | | SystemZ::CCMASK_3))) |
| 1516 | return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31); |
| 1517 | if (CCMask == (CCValid & (SystemZ::CCMASK_0 |
| 1518 | | SystemZ::CCMASK_2 |
| 1519 | | SystemZ::CCMASK_3))) |
| 1520 | return IPMConversion(1 << SystemZ::IPM_CC, |
| 1521 | TopBit - (1 << SystemZ::IPM_CC), 31); |
| 1522 | |
| 1523 | llvm_unreachable("Unexpected CC combination"); |
| 1524 | } |
| 1525 | |
| 1526 | SDValue SystemZDAGToDAGISel::expandSelectBoolean(SDNode *Node) { |
| 1527 | auto *TrueOp = dyn_cast<ConstantSDNode>(Node->getOperand(0)); |
| 1528 | auto *FalseOp = dyn_cast<ConstantSDNode>(Node->getOperand(1)); |
| 1529 | if (!TrueOp || !FalseOp) |
| 1530 | return SDValue(); |
| 1531 | if (FalseOp->getZExtValue() != 0) |
| 1532 | return SDValue(); |
| 1533 | if (TrueOp->getSExtValue() != 1 && TrueOp->getSExtValue() != -1) |
| 1534 | return SDValue(); |
| 1535 | |
| 1536 | auto *CCValidOp = dyn_cast<ConstantSDNode>(Node->getOperand(2)); |
| 1537 | auto *CCMaskOp = dyn_cast<ConstantSDNode>(Node->getOperand(3)); |
| 1538 | if (!CCValidOp || !CCMaskOp) |
| 1539 | return SDValue(); |
| 1540 | int CCValid = CCValidOp->getZExtValue(); |
| 1541 | int CCMask = CCMaskOp->getZExtValue(); |
| 1542 | |
| 1543 | SDLoc DL(Node); |
| 1544 | SDValue Glue = Node->getOperand(4); |
| 1545 | IPMConversion IPM = getIPMConversion(CCValid, CCMask); |
| 1546 | SDValue Result = CurDAG->getNode(SystemZISD::IPM, DL, MVT::i32, Glue); |
| 1547 | |
| 1548 | if (IPM.XORValue) |
| 1549 | Result = CurDAG->getNode(ISD::XOR, DL, MVT::i32, Result, |
| 1550 | CurDAG->getConstant(IPM.XORValue, DL, MVT::i32)); |
| 1551 | |
| 1552 | if (IPM.AddValue) |
| 1553 | Result = CurDAG->getNode(ISD::ADD, DL, MVT::i32, Result, |
| 1554 | CurDAG->getConstant(IPM.AddValue, DL, MVT::i32)); |
| 1555 | |
| 1556 | EVT VT = Node->getValueType(0); |
| 1557 | if (VT == MVT::i32 && IPM.Bit == 31) { |
| 1558 | unsigned ShiftOp = TrueOp->getSExtValue() == 1 ? ISD::SRL : ISD::SRA; |
| 1559 | Result = CurDAG->getNode(ShiftOp, DL, MVT::i32, Result, |
| 1560 | CurDAG->getConstant(IPM.Bit, DL, MVT::i32)); |
| 1561 | } else { |
| 1562 | if (VT != MVT::i32) |
| 1563 | Result = CurDAG->getNode(ISD::ANY_EXTEND, DL, VT, Result); |
| 1564 | |
| 1565 | if (TrueOp->getSExtValue() == 1) { |
| 1566 | // The SHR/AND sequence should get optimized to an RISBG. |
| 1567 | Result = CurDAG->getNode(ISD::SRL, DL, VT, Result, |
| 1568 | CurDAG->getConstant(IPM.Bit, DL, MVT::i32)); |
| 1569 | Result = CurDAG->getNode(ISD::AND, DL, VT, Result, |
| 1570 | CurDAG->getConstant(1, DL, VT)); |
| 1571 | } else { |
| 1572 | // Sign-extend from IPM.Bit using a pair of shifts. |
| 1573 | int ShlAmt = VT.getSizeInBits() - 1 - IPM.Bit; |
| 1574 | int SraAmt = VT.getSizeInBits() - 1; |
| 1575 | Result = CurDAG->getNode(ISD::SHL, DL, VT, Result, |
| 1576 | CurDAG->getConstant(ShlAmt, DL, MVT::i32)); |
| 1577 | Result = CurDAG->getNode(ISD::SRA, DL, VT, Result, |
| 1578 | CurDAG->getConstant(SraAmt, DL, MVT::i32)); |
| 1579 | } |
| 1580 | } |
| 1581 | |
| 1582 | return Result; |
| 1583 | } |
| 1584 | |
| 1585 | void SystemZDAGToDAGISel::PreprocessISelDAG() { |
| 1586 | bool MadeChange = false; |
| 1587 | |
| 1588 | for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(), |
| 1589 | E = CurDAG->allnodes_end(); |
| 1590 | I != E;) { |
| 1591 | SDNode *N = &*I++; |
| 1592 | if (N->use_empty()) |
| 1593 | continue; |
| 1594 | |
| 1595 | SDValue Res; |
| 1596 | switch (N->getOpcode()) { |
| 1597 | default: break; |
| 1598 | case SystemZISD::SELECT_CCMASK: |
| 1599 | Res = expandSelectBoolean(N); |
| 1600 | break; |
| 1601 | } |
| 1602 | |
| 1603 | if (Res) { |
| 1604 | DEBUG(dbgs() << "SystemZ DAG preprocessing replacing:\nOld: "); |
| 1605 | DEBUG(N->dump(CurDAG)); |
| 1606 | DEBUG(dbgs() << "\nNew: "); |
| 1607 | DEBUG(Res.getNode()->dump(CurDAG)); |
| 1608 | DEBUG(dbgs() << "\n"); |
| 1609 | |
| 1610 | CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Res); |
| 1611 | MadeChange = true; |
| 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | if (MadeChange) |
| 1616 | CurDAG->RemoveDeadNodes(); |
| 1617 | } |
| 1618 | |