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