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) { |
| 99 | return Count == 0 ? 0 : (uint64_t(1) << (Count - 1) << 1) - 1; |
| 100 | } |
| 101 | |
Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 102 | // Represents operands 2 to 5 of the ROTATE AND ... SELECTED BITS operation |
| 103 | // given by Opcode. The operands are: Input (R2), Start (I3), End (I4) and |
| 104 | // Rotate (I5). The combined operand value is effectively: |
| 105 | // |
| 106 | // (or (rotl Input, Rotate), ~Mask) |
| 107 | // |
| 108 | // for RNSBG and: |
| 109 | // |
| 110 | // (and (rotl Input, Rotate), Mask) |
| 111 | // |
Richard Sandiford | 3e38297 | 2013-10-16 13:35:13 +0000 | [diff] [blame] | 112 | // otherwise. The output value has BitSize bits, although Input may be |
| 113 | // narrower (in which case the upper bits are don't care). |
Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 114 | struct RxSBGOperands { |
Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 115 | RxSBGOperands(unsigned Op, SDValue N) |
| 116 | : Opcode(Op), BitSize(N.getValueType().getSizeInBits()), |
| 117 | Mask(allOnes(BitSize)), Input(N), Start(64 - BitSize), End(63), |
| 118 | Rotate(0) {} |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 119 | |
Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 120 | unsigned Opcode; |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 121 | unsigned BitSize; |
| 122 | uint64_t Mask; |
| 123 | SDValue Input; |
| 124 | unsigned Start; |
| 125 | unsigned End; |
| 126 | unsigned Rotate; |
| 127 | }; |
| 128 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 129 | class SystemZDAGToDAGISel : public SelectionDAGISel { |
| 130 | const SystemZTargetLowering &Lowering; |
| 131 | const SystemZSubtarget &Subtarget; |
| 132 | |
| 133 | // Used by SystemZOperands.td to create integer constants. |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 134 | inline SDValue getImm(const SDNode *Node, uint64_t Imm) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 135 | return CurDAG->getTargetConstant(Imm, Node->getValueType(0)); |
| 136 | } |
| 137 | |
Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 138 | const SystemZTargetMachine &getTargetMachine() const { |
| 139 | return static_cast<const SystemZTargetMachine &>(TM); |
| 140 | } |
| 141 | |
| 142 | const SystemZInstrInfo *getInstrInfo() const { |
| 143 | return getTargetMachine().getInstrInfo(); |
| 144 | } |
| 145 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 146 | // Try to fold more of the base or index of AM into AM, where IsBase |
| 147 | // selects between the base and index. |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 148 | bool expandAddress(SystemZAddressingMode &AM, bool IsBase) const; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 149 | |
| 150 | // Try to describe N in AM, returning true on success. |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 151 | bool selectAddress(SDValue N, SystemZAddressingMode &AM) const; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 152 | |
| 153 | // Extract individual target operands from matched address AM. |
| 154 | void getAddressOperands(const SystemZAddressingMode &AM, EVT VT, |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 155 | SDValue &Base, SDValue &Disp) const; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 156 | void getAddressOperands(const SystemZAddressingMode &AM, EVT VT, |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 157 | SDValue &Base, SDValue &Disp, SDValue &Index) const; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 158 | |
| 159 | // Try to match Addr as a FormBD address with displacement type DR. |
| 160 | // Return true on success, storing the base and displacement in |
| 161 | // Base and Disp respectively. |
| 162 | bool selectBDAddr(SystemZAddressingMode::DispRange DR, SDValue Addr, |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 163 | SDValue &Base, SDValue &Disp) const; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 164 | |
Richard Sandiford | a481f58 | 2013-08-23 11:18:53 +0000 | [diff] [blame] | 165 | // Try to match Addr as a FormBDX address with displacement type DR. |
| 166 | // Return true on success and if the result had no index. Store the |
| 167 | // base and displacement in Base and Disp respectively. |
| 168 | bool selectMVIAddr(SystemZAddressingMode::DispRange DR, SDValue Addr, |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 169 | SDValue &Base, SDValue &Disp) const; |
Richard Sandiford | a481f58 | 2013-08-23 11:18:53 +0000 | [diff] [blame] | 170 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 171 | // Try to match Addr as a FormBDX* address of form Form with |
| 172 | // displacement type DR. Return true on success, storing the base, |
| 173 | // displacement and index in Base, Disp and Index respectively. |
| 174 | bool selectBDXAddr(SystemZAddressingMode::AddrForm Form, |
| 175 | SystemZAddressingMode::DispRange DR, SDValue Addr, |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 176 | SDValue &Base, SDValue &Disp, SDValue &Index) const; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 177 | |
| 178 | // PC-relative address matching routines used by SystemZOperands.td. |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 179 | bool selectPCRelAddress(SDValue Addr, SDValue &Target) const { |
| 180 | if (SystemZISD::isPCREL(Addr.getOpcode())) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 181 | Target = Addr.getOperand(0); |
| 182 | return true; |
| 183 | } |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | // BD matching routines used by SystemZOperands.td. |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 188 | bool selectBDAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 189 | return selectBDAddr(SystemZAddressingMode::Disp12Only, Addr, Base, Disp); |
| 190 | } |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 191 | bool selectBDAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 192 | return selectBDAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp); |
| 193 | } |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 194 | bool selectBDAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 195 | return selectBDAddr(SystemZAddressingMode::Disp20Only, Addr, Base, Disp); |
| 196 | } |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 197 | bool selectBDAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 198 | return selectBDAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp); |
| 199 | } |
| 200 | |
Richard Sandiford | a481f58 | 2013-08-23 11:18:53 +0000 | [diff] [blame] | 201 | // MVI matching routines used by SystemZOperands.td. |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 202 | bool selectMVIAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const { |
Richard Sandiford | a481f58 | 2013-08-23 11:18:53 +0000 | [diff] [blame] | 203 | return selectMVIAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp); |
| 204 | } |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 205 | bool selectMVIAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const { |
Richard Sandiford | a481f58 | 2013-08-23 11:18:53 +0000 | [diff] [blame] | 206 | return selectMVIAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp); |
| 207 | } |
| 208 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 209 | // BDX matching routines used by SystemZOperands.td. |
| 210 | bool selectBDXAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp, |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 211 | SDValue &Index) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 212 | return selectBDXAddr(SystemZAddressingMode::FormBDXNormal, |
| 213 | SystemZAddressingMode::Disp12Only, |
| 214 | Addr, Base, Disp, Index); |
| 215 | } |
| 216 | bool selectBDXAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp, |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 217 | SDValue &Index) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 218 | return selectBDXAddr(SystemZAddressingMode::FormBDXNormal, |
| 219 | SystemZAddressingMode::Disp12Pair, |
| 220 | Addr, Base, Disp, Index); |
| 221 | } |
| 222 | bool selectDynAlloc12Only(SDValue Addr, SDValue &Base, SDValue &Disp, |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 223 | SDValue &Index) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 224 | return selectBDXAddr(SystemZAddressingMode::FormBDXDynAlloc, |
| 225 | SystemZAddressingMode::Disp12Only, |
| 226 | Addr, Base, Disp, Index); |
| 227 | } |
| 228 | bool selectBDXAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp, |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 229 | SDValue &Index) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 230 | return selectBDXAddr(SystemZAddressingMode::FormBDXNormal, |
| 231 | SystemZAddressingMode::Disp20Only, |
| 232 | Addr, Base, Disp, Index); |
| 233 | } |
| 234 | bool selectBDXAddr20Only128(SDValue Addr, SDValue &Base, SDValue &Disp, |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 235 | SDValue &Index) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 236 | return selectBDXAddr(SystemZAddressingMode::FormBDXNormal, |
| 237 | SystemZAddressingMode::Disp20Only128, |
| 238 | Addr, Base, Disp, Index); |
| 239 | } |
| 240 | bool selectBDXAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp, |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 241 | SDValue &Index) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 242 | return selectBDXAddr(SystemZAddressingMode::FormBDXNormal, |
| 243 | SystemZAddressingMode::Disp20Pair, |
| 244 | Addr, Base, Disp, Index); |
| 245 | } |
| 246 | bool selectLAAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp, |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 247 | SDValue &Index) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 248 | return selectBDXAddr(SystemZAddressingMode::FormBDXLA, |
| 249 | SystemZAddressingMode::Disp12Pair, |
| 250 | Addr, Base, Disp, Index); |
| 251 | } |
| 252 | bool selectLAAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp, |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 253 | SDValue &Index) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 254 | return selectBDXAddr(SystemZAddressingMode::FormBDXLA, |
| 255 | SystemZAddressingMode::Disp20Pair, |
| 256 | Addr, Base, Disp, Index); |
| 257 | } |
| 258 | |
Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 259 | // Check whether (or Op (and X InsertMask)) is effectively an insertion |
| 260 | // of X into bits InsertMask of some Y != Op. Return true if so and |
| 261 | // set Op to that Y. |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 262 | bool detectOrAndInsertion(SDValue &Op, uint64_t InsertMask) const; |
Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 263 | |
Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 264 | // Try to update RxSBG so that only the bits of RxSBG.Input in Mask are used. |
| 265 | // Return true on success. |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 266 | bool refineRxSBGMask(RxSBGOperands &RxSBG, uint64_t Mask) const; |
Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 267 | |
Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 268 | // Try to fold some of RxSBG.Input into other fields of RxSBG. |
| 269 | // Return true on success. |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 270 | bool expandRxSBG(RxSBGOperands &RxSBG) const; |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 271 | |
Richard Sandiford | 3ad5a15 | 2013-10-01 14:36:20 +0000 | [diff] [blame] | 272 | // Return an undefined value of type VT. |
| 273 | SDValue getUNDEF(SDLoc DL, EVT VT) const; |
Richard Sandiford | 84f54a3 | 2013-07-11 08:59:12 +0000 | [diff] [blame] | 274 | |
| 275 | // Convert N to VT, if it isn't already. |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 276 | SDValue convertTo(SDLoc DL, EVT VT, SDValue N) const; |
Richard Sandiford | 84f54a3 | 2013-07-11 08:59:12 +0000 | [diff] [blame] | 277 | |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 278 | // Try to implement AND or shift node N using RISBG with the zero flag set. |
| 279 | // Return the selected node on success, otherwise return null. |
| 280 | SDNode *tryRISBGZero(SDNode *N); |
Richard Sandiford | 84f54a3 | 2013-07-11 08:59:12 +0000 | [diff] [blame] | 281 | |
Richard Sandiford | 7878b85 | 2013-07-18 10:06:15 +0000 | [diff] [blame] | 282 | // Try to use RISBG or Opcode to implement OR or XOR node N. |
| 283 | // Return the selected node on success, otherwise return null. |
| 284 | SDNode *tryRxSBG(SDNode *N, unsigned Opcode); |
Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 285 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 286 | // If Op0 is null, then Node is a constant that can be loaded using: |
| 287 | // |
| 288 | // (Opcode UpperVal LowerVal) |
| 289 | // |
| 290 | // If Op0 is nonnull, then Node can be implemented using: |
| 291 | // |
| 292 | // (Opcode (Opcode Op0 UpperVal) LowerVal) |
| 293 | SDNode *splitLargeImmediate(unsigned Opcode, SDNode *Node, SDValue Op0, |
| 294 | uint64_t UpperVal, uint64_t LowerVal); |
| 295 | |
Richard Sandiford | 067817e | 2013-09-27 15:29:20 +0000 | [diff] [blame] | 296 | // Return true if Load and Store are loads and stores of the same size |
| 297 | // and are guaranteed not to overlap. Such operations can be implemented |
| 298 | // using block (SS-format) instructions. |
| 299 | // |
| 300 | // Partial overlap would lead to incorrect code, since the block operations |
| 301 | // are logically bytewise, even though they have a fast path for the |
| 302 | // non-overlapping case. We also need to avoid full overlap (i.e. two |
| 303 | // addresses that might be equal at run time) because although that case |
| 304 | // would be handled correctly, it might be implemented by millicode. |
| 305 | bool canUseBlockOperation(StoreSDNode *Store, LoadSDNode *Load) const; |
| 306 | |
Richard Sandiford | 178273a | 2013-09-05 10:36:45 +0000 | [diff] [blame] | 307 | // N is a (store (load Y), X) pattern. Return true if it can use an MVC |
| 308 | // from Y to X. |
Richard Sandiford | 9784649 | 2013-07-09 09:46:39 +0000 | [diff] [blame] | 309 | bool storeLoadCanUseMVC(SDNode *N) const; |
| 310 | |
Richard Sandiford | 178273a | 2013-09-05 10:36:45 +0000 | [diff] [blame] | 311 | // N is a (store (op (load A[0]), (load A[1])), X) pattern. Return true |
| 312 | // if A[1 - I] == X and if N can use a block operation like NC from A[I] |
| 313 | // to X. |
| 314 | bool storeLoadCanUseBlockBinary(SDNode *N, unsigned I) const; |
| 315 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 316 | public: |
| 317 | SystemZDAGToDAGISel(SystemZTargetMachine &TM, CodeGenOpt::Level OptLevel) |
| 318 | : SelectionDAGISel(TM, OptLevel), |
| 319 | Lowering(*TM.getTargetLowering()), |
| 320 | Subtarget(*TM.getSubtargetImpl()) { } |
| 321 | |
| 322 | // Override MachineFunctionPass. |
Richard Sandiford | b4d67b5 | 2014-03-06 12:03:36 +0000 | [diff] [blame] | 323 | const char *getPassName() const override { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 324 | return "SystemZ DAG->DAG Pattern Instruction Selection"; |
| 325 | } |
| 326 | |
| 327 | // Override SelectionDAGISel. |
Richard Sandiford | b4d67b5 | 2014-03-06 12:03:36 +0000 | [diff] [blame] | 328 | SDNode *Select(SDNode *Node) override; |
| 329 | bool SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode, |
| 330 | std::vector<SDValue> &OutOps) override; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 331 | |
| 332 | // Include the pieces autogenerated from the target description. |
| 333 | #include "SystemZGenDAGISel.inc" |
| 334 | }; |
| 335 | } // end anonymous namespace |
| 336 | |
| 337 | FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM, |
| 338 | CodeGenOpt::Level OptLevel) { |
| 339 | return new SystemZDAGToDAGISel(TM, OptLevel); |
| 340 | } |
| 341 | |
| 342 | // Return true if Val should be selected as a displacement for an address |
| 343 | // with range DR. Here we're interested in the range of both the instruction |
| 344 | // described by DR and of any pairing instruction. |
| 345 | static bool selectDisp(SystemZAddressingMode::DispRange DR, int64_t Val) { |
| 346 | switch (DR) { |
| 347 | case SystemZAddressingMode::Disp12Only: |
| 348 | return isUInt<12>(Val); |
| 349 | |
| 350 | case SystemZAddressingMode::Disp12Pair: |
| 351 | case SystemZAddressingMode::Disp20Only: |
| 352 | case SystemZAddressingMode::Disp20Pair: |
| 353 | return isInt<20>(Val); |
| 354 | |
| 355 | case SystemZAddressingMode::Disp20Only128: |
| 356 | return isInt<20>(Val) && isInt<20>(Val + 8); |
| 357 | } |
| 358 | llvm_unreachable("Unhandled displacement range"); |
| 359 | } |
| 360 | |
| 361 | // Change the base or index in AM to Value, where IsBase selects |
| 362 | // between the base and index. |
| 363 | static void changeComponent(SystemZAddressingMode &AM, bool IsBase, |
| 364 | SDValue Value) { |
| 365 | if (IsBase) |
| 366 | AM.Base = Value; |
| 367 | else |
| 368 | AM.Index = Value; |
| 369 | } |
| 370 | |
| 371 | // The base or index of AM is equivalent to Value + ADJDYNALLOC, |
| 372 | // where IsBase selects between the base and index. Try to fold the |
| 373 | // ADJDYNALLOC into AM. |
| 374 | static bool expandAdjDynAlloc(SystemZAddressingMode &AM, bool IsBase, |
| 375 | SDValue Value) { |
| 376 | if (AM.isDynAlloc() && !AM.IncludesDynAlloc) { |
| 377 | changeComponent(AM, IsBase, Value); |
| 378 | AM.IncludesDynAlloc = true; |
| 379 | return true; |
| 380 | } |
| 381 | return false; |
| 382 | } |
| 383 | |
| 384 | // The base of AM is equivalent to Base + Index. Try to use Index as |
| 385 | // the index register. |
| 386 | static bool expandIndex(SystemZAddressingMode &AM, SDValue Base, |
| 387 | SDValue Index) { |
| 388 | if (AM.hasIndexField() && !AM.Index.getNode()) { |
| 389 | AM.Base = Base; |
| 390 | AM.Index = Index; |
| 391 | return true; |
| 392 | } |
| 393 | return false; |
| 394 | } |
| 395 | |
| 396 | // The base or index of AM is equivalent to Op0 + Op1, where IsBase selects |
| 397 | // between the base and index. Try to fold Op1 into AM's displacement. |
| 398 | static bool expandDisp(SystemZAddressingMode &AM, bool IsBase, |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 399 | SDValue Op0, uint64_t Op1) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 400 | // First try adjusting the displacement. |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 401 | int64_t TestDisp = AM.Disp + Op1; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 402 | if (selectDisp(AM.DR, TestDisp)) { |
| 403 | changeComponent(AM, IsBase, Op0); |
| 404 | AM.Disp = TestDisp; |
| 405 | return true; |
| 406 | } |
| 407 | |
| 408 | // We could consider forcing the displacement into a register and |
| 409 | // using it as an index, but it would need to be carefully tuned. |
| 410 | return false; |
| 411 | } |
| 412 | |
| 413 | bool SystemZDAGToDAGISel::expandAddress(SystemZAddressingMode &AM, |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 414 | bool IsBase) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 415 | SDValue N = IsBase ? AM.Base : AM.Index; |
| 416 | unsigned Opcode = N.getOpcode(); |
| 417 | if (Opcode == ISD::TRUNCATE) { |
| 418 | N = N.getOperand(0); |
| 419 | Opcode = N.getOpcode(); |
| 420 | } |
| 421 | if (Opcode == ISD::ADD || CurDAG->isBaseWithConstantOffset(N)) { |
| 422 | SDValue Op0 = N.getOperand(0); |
| 423 | SDValue Op1 = N.getOperand(1); |
| 424 | |
| 425 | unsigned Op0Code = Op0->getOpcode(); |
| 426 | unsigned Op1Code = Op1->getOpcode(); |
| 427 | |
| 428 | if (Op0Code == SystemZISD::ADJDYNALLOC) |
| 429 | return expandAdjDynAlloc(AM, IsBase, Op1); |
| 430 | if (Op1Code == SystemZISD::ADJDYNALLOC) |
| 431 | return expandAdjDynAlloc(AM, IsBase, Op0); |
| 432 | |
| 433 | if (Op0Code == ISD::Constant) |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 434 | return expandDisp(AM, IsBase, Op1, |
| 435 | cast<ConstantSDNode>(Op0)->getSExtValue()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 436 | if (Op1Code == ISD::Constant) |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 437 | return expandDisp(AM, IsBase, Op0, |
| 438 | cast<ConstantSDNode>(Op1)->getSExtValue()); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 439 | |
| 440 | if (IsBase && expandIndex(AM, Op0, Op1)) |
| 441 | return true; |
| 442 | } |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 443 | if (Opcode == SystemZISD::PCREL_OFFSET) { |
| 444 | SDValue Full = N.getOperand(0); |
| 445 | SDValue Base = N.getOperand(1); |
| 446 | SDValue Anchor = Base.getOperand(0); |
| 447 | uint64_t Offset = (cast<GlobalAddressSDNode>(Full)->getOffset() - |
| 448 | cast<GlobalAddressSDNode>(Anchor)->getOffset()); |
| 449 | return expandDisp(AM, IsBase, Base, Offset); |
| 450 | } |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 451 | return false; |
| 452 | } |
| 453 | |
| 454 | // Return true if an instruction with displacement range DR should be |
| 455 | // used for displacement value Val. selectDisp(DR, Val) must already hold. |
| 456 | static bool isValidDisp(SystemZAddressingMode::DispRange DR, int64_t Val) { |
| 457 | assert(selectDisp(DR, Val) && "Invalid displacement"); |
| 458 | switch (DR) { |
| 459 | case SystemZAddressingMode::Disp12Only: |
| 460 | case SystemZAddressingMode::Disp20Only: |
| 461 | case SystemZAddressingMode::Disp20Only128: |
| 462 | return true; |
| 463 | |
| 464 | case SystemZAddressingMode::Disp12Pair: |
| 465 | // Use the other instruction if the displacement is too large. |
| 466 | return isUInt<12>(Val); |
| 467 | |
| 468 | case SystemZAddressingMode::Disp20Pair: |
| 469 | // Use the other instruction if the displacement is small enough. |
| 470 | return !isUInt<12>(Val); |
| 471 | } |
| 472 | llvm_unreachable("Unhandled displacement range"); |
| 473 | } |
| 474 | |
| 475 | // Return true if Base + Disp + Index should be performed by LA(Y). |
| 476 | static bool shouldUseLA(SDNode *Base, int64_t Disp, SDNode *Index) { |
| 477 | // Don't use LA(Y) for constants. |
| 478 | if (!Base) |
| 479 | return false; |
| 480 | |
| 481 | // Always use LA(Y) for frame addresses, since we know that the destination |
| 482 | // register is almost always (perhaps always) going to be different from |
| 483 | // the frame register. |
| 484 | if (Base->getOpcode() == ISD::FrameIndex) |
| 485 | return true; |
| 486 | |
| 487 | if (Disp) { |
| 488 | // Always use LA(Y) if there is a base, displacement and index. |
| 489 | if (Index) |
| 490 | return true; |
| 491 | |
| 492 | // Always use LA if the displacement is small enough. It should always |
| 493 | // be no worse than AGHI (and better if it avoids a move). |
| 494 | if (isUInt<12>(Disp)) |
| 495 | return true; |
| 496 | |
| 497 | // For similar reasons, always use LAY if the constant is too big for AGHI. |
| 498 | // LAY should be no worse than AGFI. |
| 499 | if (!isInt<16>(Disp)) |
| 500 | return true; |
| 501 | } else { |
| 502 | // Don't use LA for plain registers. |
| 503 | if (!Index) |
| 504 | return false; |
| 505 | |
| 506 | // Don't use LA for plain addition if the index operand is only used |
| 507 | // once. It should be a natural two-operand addition in that case. |
| 508 | if (Index->hasOneUse()) |
| 509 | return false; |
| 510 | |
| 511 | // Prefer addition if the second operation is sign-extended, in the |
| 512 | // hope of using AGF. |
| 513 | unsigned IndexOpcode = Index->getOpcode(); |
| 514 | if (IndexOpcode == ISD::SIGN_EXTEND || |
| 515 | IndexOpcode == ISD::SIGN_EXTEND_INREG) |
| 516 | return false; |
| 517 | } |
| 518 | |
| 519 | // Don't use LA for two-operand addition if either operand is only |
| 520 | // used once. The addition instructions are better in that case. |
| 521 | if (Base->hasOneUse()) |
| 522 | return false; |
| 523 | |
| 524 | return true; |
| 525 | } |
| 526 | |
| 527 | // Return true if Addr is suitable for AM, updating AM if so. |
| 528 | bool SystemZDAGToDAGISel::selectAddress(SDValue Addr, |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 529 | SystemZAddressingMode &AM) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 530 | // Start out assuming that the address will need to be loaded separately, |
| 531 | // then try to extend it as much as we can. |
| 532 | AM.Base = Addr; |
| 533 | |
| 534 | // First try treating the address as a constant. |
| 535 | if (Addr.getOpcode() == ISD::Constant && |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 536 | expandDisp(AM, true, SDValue(), |
| 537 | cast<ConstantSDNode>(Addr)->getSExtValue())) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 538 | ; |
| 539 | else |
| 540 | // Otherwise try expanding each component. |
| 541 | while (expandAddress(AM, true) || |
| 542 | (AM.Index.getNode() && expandAddress(AM, false))) |
| 543 | continue; |
| 544 | |
| 545 | // Reject cases where it isn't profitable to use LA(Y). |
| 546 | if (AM.Form == SystemZAddressingMode::FormBDXLA && |
| 547 | !shouldUseLA(AM.Base.getNode(), AM.Disp, AM.Index.getNode())) |
| 548 | return false; |
| 549 | |
| 550 | // Reject cases where the other instruction in a pair should be used. |
| 551 | if (!isValidDisp(AM.DR, AM.Disp)) |
| 552 | return false; |
| 553 | |
| 554 | // Make sure that ADJDYNALLOC is included where necessary. |
| 555 | if (AM.isDynAlloc() && !AM.IncludesDynAlloc) |
| 556 | return false; |
| 557 | |
| 558 | DEBUG(AM.dump()); |
| 559 | return true; |
| 560 | } |
| 561 | |
| 562 | // Insert a node into the DAG at least before Pos. This will reposition |
| 563 | // the node as needed, and will assign it a node ID that is <= Pos's ID. |
| 564 | // Note that this does *not* preserve the uniqueness of node IDs! |
| 565 | // The selection DAG must no longer depend on their uniqueness when this |
| 566 | // function is used. |
| 567 | static void insertDAGNode(SelectionDAG *DAG, SDNode *Pos, SDValue N) { |
| 568 | if (N.getNode()->getNodeId() == -1 || |
| 569 | N.getNode()->getNodeId() > Pos->getNodeId()) { |
| 570 | DAG->RepositionNode(Pos, N.getNode()); |
| 571 | N.getNode()->setNodeId(Pos->getNodeId()); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM, |
| 576 | EVT VT, SDValue &Base, |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 577 | SDValue &Disp) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 578 | Base = AM.Base; |
| 579 | if (!Base.getNode()) |
| 580 | // Register 0 means "no base". This is mostly useful for shifts. |
| 581 | Base = CurDAG->getRegister(0, VT); |
| 582 | else if (Base.getOpcode() == ISD::FrameIndex) { |
| 583 | // Lower a FrameIndex to a TargetFrameIndex. |
| 584 | int64_t FrameIndex = cast<FrameIndexSDNode>(Base)->getIndex(); |
| 585 | Base = CurDAG->getTargetFrameIndex(FrameIndex, VT); |
| 586 | } else if (Base.getValueType() != VT) { |
| 587 | // Truncate values from i64 to i32, for shifts. |
| 588 | assert(VT == MVT::i32 && Base.getValueType() == MVT::i64 && |
| 589 | "Unexpected truncation"); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 590 | SDLoc DL(Base); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 591 | SDValue Trunc = CurDAG->getNode(ISD::TRUNCATE, DL, VT, Base); |
| 592 | insertDAGNode(CurDAG, Base.getNode(), Trunc); |
| 593 | Base = Trunc; |
| 594 | } |
| 595 | |
| 596 | // Lower the displacement to a TargetConstant. |
| 597 | Disp = CurDAG->getTargetConstant(AM.Disp, VT); |
| 598 | } |
| 599 | |
| 600 | void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM, |
| 601 | EVT VT, SDValue &Base, |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 602 | SDValue &Disp, |
| 603 | SDValue &Index) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 604 | getAddressOperands(AM, VT, Base, Disp); |
| 605 | |
| 606 | Index = AM.Index; |
| 607 | if (!Index.getNode()) |
| 608 | // Register 0 means "no index". |
| 609 | Index = CurDAG->getRegister(0, VT); |
| 610 | } |
| 611 | |
| 612 | bool SystemZDAGToDAGISel::selectBDAddr(SystemZAddressingMode::DispRange DR, |
| 613 | SDValue Addr, SDValue &Base, |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 614 | SDValue &Disp) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 615 | SystemZAddressingMode AM(SystemZAddressingMode::FormBD, DR); |
| 616 | if (!selectAddress(Addr, AM)) |
| 617 | return false; |
| 618 | |
| 619 | getAddressOperands(AM, Addr.getValueType(), Base, Disp); |
| 620 | return true; |
| 621 | } |
| 622 | |
Richard Sandiford | a481f58 | 2013-08-23 11:18:53 +0000 | [diff] [blame] | 623 | bool SystemZDAGToDAGISel::selectMVIAddr(SystemZAddressingMode::DispRange DR, |
| 624 | SDValue Addr, SDValue &Base, |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 625 | SDValue &Disp) const { |
Richard Sandiford | a481f58 | 2013-08-23 11:18:53 +0000 | [diff] [blame] | 626 | SystemZAddressingMode AM(SystemZAddressingMode::FormBDXNormal, DR); |
| 627 | if (!selectAddress(Addr, AM) || AM.Index.getNode()) |
| 628 | return false; |
| 629 | |
| 630 | getAddressOperands(AM, Addr.getValueType(), Base, Disp); |
| 631 | return true; |
| 632 | } |
| 633 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 634 | bool SystemZDAGToDAGISel::selectBDXAddr(SystemZAddressingMode::AddrForm Form, |
| 635 | SystemZAddressingMode::DispRange DR, |
| 636 | SDValue Addr, SDValue &Base, |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 637 | SDValue &Disp, SDValue &Index) const { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 638 | SystemZAddressingMode AM(Form, DR); |
| 639 | if (!selectAddress(Addr, AM)) |
| 640 | return false; |
| 641 | |
| 642 | getAddressOperands(AM, Addr.getValueType(), Base, Disp, Index); |
| 643 | return true; |
| 644 | } |
| 645 | |
Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 646 | bool SystemZDAGToDAGISel::detectOrAndInsertion(SDValue &Op, |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 647 | uint64_t InsertMask) const { |
Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 648 | // We're only interested in cases where the insertion is into some operand |
| 649 | // of Op, rather than into Op itself. The only useful case is an AND. |
| 650 | if (Op.getOpcode() != ISD::AND) |
| 651 | return false; |
| 652 | |
| 653 | // We need a constant mask. |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 654 | auto *MaskNode = dyn_cast<ConstantSDNode>(Op.getOperand(1).getNode()); |
Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 655 | if (!MaskNode) |
| 656 | return false; |
| 657 | |
| 658 | // It's not an insertion of Op.getOperand(0) if the two masks overlap. |
| 659 | uint64_t AndMask = MaskNode->getZExtValue(); |
| 660 | if (InsertMask & AndMask) |
| 661 | return false; |
| 662 | |
| 663 | // It's only an insertion if all bits are covered or are known to be zero. |
| 664 | // The inner check covers all cases but is more expensive. |
| 665 | uint64_t Used = allOnes(Op.getValueType().getSizeInBits()); |
| 666 | if (Used != (AndMask | InsertMask)) { |
| 667 | APInt KnownZero, KnownOne; |
| 668 | CurDAG->ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne); |
| 669 | if (Used != (AndMask | InsertMask | KnownZero.getZExtValue())) |
| 670 | return false; |
| 671 | } |
| 672 | |
| 673 | Op = Op.getOperand(0); |
| 674 | return true; |
| 675 | } |
| 676 | |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 677 | bool SystemZDAGToDAGISel::refineRxSBGMask(RxSBGOperands &RxSBG, |
| 678 | uint64_t Mask) const { |
Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 679 | const SystemZInstrInfo *TII = getInstrInfo(); |
Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 680 | if (RxSBG.Rotate != 0) |
| 681 | Mask = (Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate)); |
| 682 | Mask &= RxSBG.Mask; |
Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 683 | if (TII->isRxSBGMask(Mask, RxSBG.BitSize, RxSBG.Start, RxSBG.End)) { |
Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 684 | RxSBG.Mask = Mask; |
Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 685 | return true; |
| 686 | } |
Richard Sandiford | 84f54a3 | 2013-07-11 08:59:12 +0000 | [diff] [blame] | 687 | return false; |
| 688 | } |
| 689 | |
Richard Sandiford | dd7dd93 | 2013-11-26 10:53:16 +0000 | [diff] [blame] | 690 | // Return true if any bits of (RxSBG.Input & Mask) are significant. |
| 691 | static bool maskMatters(RxSBGOperands &RxSBG, uint64_t Mask) { |
| 692 | // Rotate the mask in the same way as RxSBG.Input is rotated. |
Richard Sandiford | 297f7d2 | 2013-07-18 10:14:55 +0000 | [diff] [blame] | 693 | if (RxSBG.Rotate != 0) |
Richard Sandiford | dd7dd93 | 2013-11-26 10:53:16 +0000 | [diff] [blame] | 694 | Mask = ((Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate))); |
| 695 | return (Mask & RxSBG.Mask) != 0; |
Richard Sandiford | 297f7d2 | 2013-07-18 10:14:55 +0000 | [diff] [blame] | 696 | } |
| 697 | |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 698 | bool SystemZDAGToDAGISel::expandRxSBG(RxSBGOperands &RxSBG) const { |
Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 699 | SDValue N = RxSBG.Input; |
Richard Sandiford | 297f7d2 | 2013-07-18 10:14:55 +0000 | [diff] [blame] | 700 | unsigned Opcode = N.getOpcode(); |
| 701 | switch (Opcode) { |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 702 | case ISD::AND: { |
Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 703 | if (RxSBG.Opcode == SystemZ::RNSBG) |
| 704 | return false; |
| 705 | |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 706 | auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode()); |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 707 | if (!MaskNode) |
| 708 | return false; |
| 709 | |
| 710 | SDValue Input = N.getOperand(0); |
| 711 | uint64_t Mask = MaskNode->getZExtValue(); |
Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 712 | if (!refineRxSBGMask(RxSBG, Mask)) { |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 713 | // If some bits of Input are already known zeros, those bits will have |
| 714 | // been removed from the mask. See if adding them back in makes the |
| 715 | // mask suitable. |
| 716 | APInt KnownZero, KnownOne; |
| 717 | CurDAG->ComputeMaskedBits(Input, KnownZero, KnownOne); |
| 718 | Mask |= KnownZero.getZExtValue(); |
Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 719 | if (!refineRxSBGMask(RxSBG, Mask)) |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 720 | return false; |
| 721 | } |
Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 722 | RxSBG.Input = Input; |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 723 | return true; |
| 724 | } |
| 725 | |
Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 726 | case ISD::OR: { |
| 727 | if (RxSBG.Opcode != SystemZ::RNSBG) |
| 728 | return false; |
| 729 | |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 730 | auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode()); |
Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 731 | if (!MaskNode) |
| 732 | return false; |
| 733 | |
| 734 | SDValue Input = N.getOperand(0); |
| 735 | uint64_t Mask = ~MaskNode->getZExtValue(); |
| 736 | if (!refineRxSBGMask(RxSBG, Mask)) { |
| 737 | // If some bits of Input are already known ones, those bits will have |
| 738 | // been removed from the mask. See if adding them back in makes the |
| 739 | // mask suitable. |
| 740 | APInt KnownZero, KnownOne; |
| 741 | CurDAG->ComputeMaskedBits(Input, KnownZero, KnownOne); |
| 742 | Mask &= ~KnownOne.getZExtValue(); |
| 743 | if (!refineRxSBGMask(RxSBG, Mask)) |
| 744 | return false; |
| 745 | } |
| 746 | RxSBG.Input = Input; |
| 747 | return true; |
| 748 | } |
| 749 | |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 750 | case ISD::ROTL: { |
Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 751 | // Any 64-bit rotate left can be merged into the RxSBG. |
Richard Sandiford | 3e38297 | 2013-10-16 13:35:13 +0000 | [diff] [blame] | 752 | if (RxSBG.BitSize != 64 || N.getValueType() != MVT::i64) |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 753 | return false; |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 754 | auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode()); |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 755 | if (!CountNode) |
| 756 | return false; |
| 757 | |
Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 758 | RxSBG.Rotate = (RxSBG.Rotate + CountNode->getZExtValue()) & 63; |
| 759 | RxSBG.Input = N.getOperand(0); |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 760 | return true; |
| 761 | } |
| 762 | |
Richard Sandiford | 220ee49 | 2013-12-20 11:49:48 +0000 | [diff] [blame] | 763 | case ISD::ANY_EXTEND: |
| 764 | // Bits above the extended operand are don't-care. |
| 765 | RxSBG.Input = N.getOperand(0); |
| 766 | return true; |
| 767 | |
Richard Sandiford | 3875cb6 | 2014-01-09 11:28:53 +0000 | [diff] [blame] | 768 | case ISD::ZERO_EXTEND: |
| 769 | if (RxSBG.Opcode != SystemZ::RNSBG) { |
| 770 | // Restrict the mask to the extended operand. |
| 771 | unsigned InnerBitSize = N.getOperand(0).getValueType().getSizeInBits(); |
| 772 | if (!refineRxSBGMask(RxSBG, allOnes(InnerBitSize))) |
| 773 | return false; |
Richard Sandiford | 220ee49 | 2013-12-20 11:49:48 +0000 | [diff] [blame] | 774 | |
Richard Sandiford | 3875cb6 | 2014-01-09 11:28:53 +0000 | [diff] [blame] | 775 | RxSBG.Input = N.getOperand(0); |
| 776 | return true; |
| 777 | } |
| 778 | // Fall through. |
Richard Sandiford | 220ee49 | 2013-12-20 11:49:48 +0000 | [diff] [blame] | 779 | |
| 780 | case ISD::SIGN_EXTEND: { |
Richard Sandiford | 3e38297 | 2013-10-16 13:35:13 +0000 | [diff] [blame] | 781 | // Check that the extension bits are don't-care (i.e. are masked out |
| 782 | // by the final mask). |
| 783 | unsigned InnerBitSize = N.getOperand(0).getValueType().getSizeInBits(); |
Richard Sandiford | dd7dd93 | 2013-11-26 10:53:16 +0000 | [diff] [blame] | 784 | if (maskMatters(RxSBG, allOnes(RxSBG.BitSize) - allOnes(InnerBitSize))) |
Richard Sandiford | 3e38297 | 2013-10-16 13:35:13 +0000 | [diff] [blame] | 785 | return false; |
| 786 | |
| 787 | RxSBG.Input = N.getOperand(0); |
| 788 | return true; |
| 789 | } |
| 790 | |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 791 | case ISD::SHL: { |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 792 | auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode()); |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 793 | if (!CountNode) |
| 794 | return false; |
| 795 | |
| 796 | uint64_t Count = CountNode->getZExtValue(); |
Richard Sandiford | 3e38297 | 2013-10-16 13:35:13 +0000 | [diff] [blame] | 797 | unsigned BitSize = N.getValueType().getSizeInBits(); |
| 798 | if (Count < 1 || Count >= BitSize) |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 799 | return false; |
| 800 | |
Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 801 | if (RxSBG.Opcode == SystemZ::RNSBG) { |
| 802 | // Treat (shl X, count) as (rotl X, size-count) as long as the bottom |
| 803 | // count bits from RxSBG.Input are ignored. |
Richard Sandiford | dd7dd93 | 2013-11-26 10:53:16 +0000 | [diff] [blame] | 804 | if (maskMatters(RxSBG, allOnes(Count))) |
Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 805 | return false; |
| 806 | } else { |
| 807 | // Treat (shl X, count) as (and (rotl X, count), ~0<<count). |
Richard Sandiford | 3e38297 | 2013-10-16 13:35:13 +0000 | [diff] [blame] | 808 | if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count) << Count)) |
Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 809 | return false; |
| 810 | } |
| 811 | |
Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 812 | RxSBG.Rotate = (RxSBG.Rotate + Count) & 63; |
| 813 | RxSBG.Input = N.getOperand(0); |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 814 | return true; |
| 815 | } |
| 816 | |
Richard Sandiford | 297f7d2 | 2013-07-18 10:14:55 +0000 | [diff] [blame] | 817 | case ISD::SRL: |
| 818 | case ISD::SRA: { |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 819 | auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode()); |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 820 | if (!CountNode) |
| 821 | return false; |
| 822 | |
| 823 | uint64_t Count = CountNode->getZExtValue(); |
Richard Sandiford | 3e38297 | 2013-10-16 13:35:13 +0000 | [diff] [blame] | 824 | unsigned BitSize = N.getValueType().getSizeInBits(); |
| 825 | if (Count < 1 || Count >= BitSize) |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 826 | return false; |
| 827 | |
Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 828 | if (RxSBG.Opcode == SystemZ::RNSBG || Opcode == ISD::SRA) { |
| 829 | // Treat (srl|sra X, count) as (rotl X, size-count) as long as the top |
| 830 | // count bits from RxSBG.Input are ignored. |
Richard Sandiford | dd7dd93 | 2013-11-26 10:53:16 +0000 | [diff] [blame] | 831 | if (maskMatters(RxSBG, allOnes(Count) << (BitSize - Count))) |
Richard Sandiford | 297f7d2 | 2013-07-18 10:14:55 +0000 | [diff] [blame] | 832 | return false; |
| 833 | } else { |
| 834 | // Treat (srl X, count), mask) as (and (rotl X, size-count), ~0>>count), |
| 835 | // which is similar to SLL above. |
Richard Sandiford | 3e38297 | 2013-10-16 13:35:13 +0000 | [diff] [blame] | 836 | if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count))) |
Richard Sandiford | 297f7d2 | 2013-07-18 10:14:55 +0000 | [diff] [blame] | 837 | return false; |
| 838 | } |
| 839 | |
Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 840 | RxSBG.Rotate = (RxSBG.Rotate - Count) & 63; |
| 841 | RxSBG.Input = N.getOperand(0); |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 842 | return true; |
| 843 | } |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 844 | default: |
| 845 | return false; |
| 846 | } |
| 847 | } |
| 848 | |
Richard Sandiford | 3ad5a15 | 2013-10-01 14:36:20 +0000 | [diff] [blame] | 849 | SDValue SystemZDAGToDAGISel::getUNDEF(SDLoc DL, EVT VT) const { |
| 850 | SDNode *N = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, VT); |
Richard Sandiford | 84f54a3 | 2013-07-11 08:59:12 +0000 | [diff] [blame] | 851 | return SDValue(N, 0); |
| 852 | } |
| 853 | |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 854 | SDValue SystemZDAGToDAGISel::convertTo(SDLoc DL, EVT VT, SDValue N) const { |
Richard Sandiford | d816320 | 2013-09-13 09:12:44 +0000 | [diff] [blame] | 855 | if (N.getValueType() == MVT::i32 && VT == MVT::i64) |
Richard Sandiford | 87a4436 | 2013-09-30 10:28:35 +0000 | [diff] [blame] | 856 | return CurDAG->getTargetInsertSubreg(SystemZ::subreg_l32, |
Richard Sandiford | 3ad5a15 | 2013-10-01 14:36:20 +0000 | [diff] [blame] | 857 | DL, VT, getUNDEF(DL, MVT::i64), N); |
Richard Sandiford | d816320 | 2013-09-13 09:12:44 +0000 | [diff] [blame] | 858 | if (N.getValueType() == MVT::i64 && VT == MVT::i32) |
Richard Sandiford | 87a4436 | 2013-09-30 10:28:35 +0000 | [diff] [blame] | 859 | return CurDAG->getTargetExtractSubreg(SystemZ::subreg_l32, DL, VT, N); |
Richard Sandiford | 84f54a3 | 2013-07-11 08:59:12 +0000 | [diff] [blame] | 860 | assert(N.getValueType() == VT && "Unexpected value types"); |
| 861 | return N; |
| 862 | } |
| 863 | |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 864 | SDNode *SystemZDAGToDAGISel::tryRISBGZero(SDNode *N) { |
Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 865 | EVT VT = N->getValueType(0); |
Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 866 | RxSBGOperands RISBG(SystemZ::RISBG, SDValue(N, 0)); |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 867 | unsigned Count = 0; |
Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 868 | while (expandRxSBG(RISBG)) |
Richard Sandiford | 3e38297 | 2013-10-16 13:35:13 +0000 | [diff] [blame] | 869 | if (RISBG.Input.getOpcode() != ISD::ANY_EXTEND) |
| 870 | Count += 1; |
Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 871 | if (Count == 0) |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame^] | 872 | return nullptr; |
Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 873 | if (Count == 1) { |
| 874 | // Prefer to use normal shift instructions over RISBG, since they can handle |
| 875 | // all cases and are sometimes shorter. |
| 876 | if (N->getOpcode() != ISD::AND) |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame^] | 877 | return nullptr; |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 878 | |
Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 879 | // Prefer register extensions like LLC over RISBG. Also prefer to start |
| 880 | // out with normal ANDs if one instruction would be enough. We can convert |
| 881 | // these ANDs into an RISBG later if a three-address instruction is useful. |
| 882 | if (VT == MVT::i32 || |
| 883 | RISBG.Mask == 0xff || |
| 884 | RISBG.Mask == 0xffff || |
| 885 | SystemZ::isImmLF(~RISBG.Mask) || |
| 886 | SystemZ::isImmHF(~RISBG.Mask)) { |
| 887 | // Force the new mask into the DAG, since it may include known-one bits. |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 888 | auto *MaskN = cast<ConstantSDNode>(N->getOperand(1).getNode()); |
Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 889 | if (MaskN->getZExtValue() != RISBG.Mask) { |
| 890 | SDValue NewMask = CurDAG->getConstant(RISBG.Mask, VT); |
| 891 | N = CurDAG->UpdateNodeOperands(N, N->getOperand(0), NewMask); |
| 892 | return SelectCode(N); |
| 893 | } |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame^] | 894 | return nullptr; |
Richard Sandiford | 6a06ba3 | 2013-07-31 11:36:35 +0000 | [diff] [blame] | 895 | } |
| 896 | } |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 897 | |
Richard Sandiford | 3ad5a15 | 2013-10-01 14:36:20 +0000 | [diff] [blame] | 898 | unsigned Opcode = SystemZ::RISBG; |
| 899 | EVT OpcodeVT = MVT::i64; |
| 900 | if (VT == MVT::i32 && Subtarget.hasHighWord()) { |
| 901 | Opcode = SystemZ::RISBMux; |
| 902 | OpcodeVT = MVT::i32; |
| 903 | RISBG.Start &= 31; |
| 904 | RISBG.End &= 31; |
| 905 | } |
Richard Sandiford | 84f54a3 | 2013-07-11 08:59:12 +0000 | [diff] [blame] | 906 | SDValue Ops[5] = { |
Richard Sandiford | 3ad5a15 | 2013-10-01 14:36:20 +0000 | [diff] [blame] | 907 | getUNDEF(SDLoc(N), OpcodeVT), |
| 908 | convertTo(SDLoc(N), OpcodeVT, RISBG.Input), |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 909 | CurDAG->getTargetConstant(RISBG.Start, MVT::i32), |
| 910 | CurDAG->getTargetConstant(RISBG.End | 128, MVT::i32), |
| 911 | CurDAG->getTargetConstant(RISBG.Rotate, MVT::i32) |
Richard Sandiford | 84f54a3 | 2013-07-11 08:59:12 +0000 | [diff] [blame] | 912 | }; |
Richard Sandiford | 3ad5a15 | 2013-10-01 14:36:20 +0000 | [diff] [blame] | 913 | N = CurDAG->getMachineNode(Opcode, SDLoc(N), OpcodeVT, Ops); |
Richard Sandiford | 84f54a3 | 2013-07-11 08:59:12 +0000 | [diff] [blame] | 914 | return convertTo(SDLoc(N), VT, SDValue(N, 0)).getNode(); |
| 915 | } |
| 916 | |
Richard Sandiford | 7878b85 | 2013-07-18 10:06:15 +0000 | [diff] [blame] | 917 | SDNode *SystemZDAGToDAGISel::tryRxSBG(SDNode *N, unsigned Opcode) { |
| 918 | // 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] | 919 | // and see which goes deepest. |
Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 920 | RxSBGOperands RxSBG[] = { |
| 921 | RxSBGOperands(Opcode, N->getOperand(0)), |
| 922 | RxSBGOperands(Opcode, N->getOperand(1)) |
| 923 | }; |
Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 924 | unsigned Count[] = { 0, 0 }; |
| 925 | for (unsigned I = 0; I < 2; ++I) |
Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 926 | while (expandRxSBG(RxSBG[I])) |
Richard Sandiford | 3e38297 | 2013-10-16 13:35:13 +0000 | [diff] [blame] | 927 | if (RxSBG[I].Input.getOpcode() != ISD::ANY_EXTEND) |
| 928 | Count[I] += 1; |
Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 929 | |
| 930 | // Do nothing if neither operand is suitable. |
| 931 | if (Count[0] == 0 && Count[1] == 0) |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame^] | 932 | return nullptr; |
Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 933 | |
| 934 | // Pick the deepest second operand. |
| 935 | unsigned I = Count[0] > Count[1] ? 0 : 1; |
| 936 | SDValue Op0 = N->getOperand(I ^ 1); |
| 937 | |
| 938 | // Prefer IC for character insertions from memory. |
Richard Sandiford | 7878b85 | 2013-07-18 10:06:15 +0000 | [diff] [blame] | 939 | if (Opcode == SystemZ::ROSBG && (RxSBG[I].Mask & 0xff) == 0) |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 940 | if (auto *Load = dyn_cast<LoadSDNode>(Op0.getNode())) |
Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 941 | if (Load->getMemoryVT() == MVT::i8) |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame^] | 942 | return nullptr; |
Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 943 | |
| 944 | // See whether we can avoid an AND in the first operand by converting |
| 945 | // ROSBG to RISBG. |
Richard Sandiford | 7878b85 | 2013-07-18 10:06:15 +0000 | [diff] [blame] | 946 | if (Opcode == SystemZ::ROSBG && detectOrAndInsertion(Op0, RxSBG[I].Mask)) |
Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 947 | Opcode = SystemZ::RISBG; |
| 948 | |
| 949 | EVT VT = N->getValueType(0); |
| 950 | SDValue Ops[5] = { |
| 951 | convertTo(SDLoc(N), MVT::i64, Op0), |
Richard Sandiford | 5cbac96 | 2013-07-18 09:45:08 +0000 | [diff] [blame] | 952 | convertTo(SDLoc(N), MVT::i64, RxSBG[I].Input), |
| 953 | CurDAG->getTargetConstant(RxSBG[I].Start, MVT::i32), |
| 954 | CurDAG->getTargetConstant(RxSBG[I].End, MVT::i32), |
| 955 | CurDAG->getTargetConstant(RxSBG[I].Rotate, MVT::i32) |
Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 956 | }; |
| 957 | N = CurDAG->getMachineNode(Opcode, SDLoc(N), MVT::i64, Ops); |
| 958 | return convertTo(SDLoc(N), VT, SDValue(N, 0)).getNode(); |
| 959 | } |
| 960 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 961 | SDNode *SystemZDAGToDAGISel::splitLargeImmediate(unsigned Opcode, SDNode *Node, |
| 962 | SDValue Op0, uint64_t UpperVal, |
| 963 | uint64_t LowerVal) { |
| 964 | EVT VT = Node->getValueType(0); |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 965 | SDLoc DL(Node); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 966 | SDValue Upper = CurDAG->getConstant(UpperVal, VT); |
| 967 | if (Op0.getNode()) |
| 968 | Upper = CurDAG->getNode(Opcode, DL, VT, Op0, Upper); |
| 969 | Upper = SDValue(Select(Upper.getNode()), 0); |
| 970 | |
| 971 | SDValue Lower = CurDAG->getConstant(LowerVal, VT); |
| 972 | SDValue Or = CurDAG->getNode(Opcode, DL, VT, Upper, Lower); |
| 973 | return Or.getNode(); |
| 974 | } |
| 975 | |
Richard Sandiford | 067817e | 2013-09-27 15:29:20 +0000 | [diff] [blame] | 976 | bool SystemZDAGToDAGISel::canUseBlockOperation(StoreSDNode *Store, |
| 977 | LoadSDNode *Load) const { |
Richard Sandiford | 178273a | 2013-09-05 10:36:45 +0000 | [diff] [blame] | 978 | // Check that the two memory operands have the same size. |
| 979 | if (Load->getMemoryVT() != Store->getMemoryVT()) |
Richard Sandiford | 9784649 | 2013-07-09 09:46:39 +0000 | [diff] [blame] | 980 | return false; |
| 981 | |
Richard Sandiford | 178273a | 2013-09-05 10:36:45 +0000 | [diff] [blame] | 982 | // Volatility stops an access from being decomposed. |
| 983 | if (Load->isVolatile() || Store->isVolatile()) |
| 984 | return false; |
Richard Sandiford | 9784649 | 2013-07-09 09:46:39 +0000 | [diff] [blame] | 985 | |
| 986 | // There's no chance of overlap if the load is invariant. |
| 987 | if (Load->isInvariant()) |
| 988 | return true; |
| 989 | |
Richard Sandiford | 9784649 | 2013-07-09 09:46:39 +0000 | [diff] [blame] | 990 | // Otherwise we need to check whether there's an alias. |
Nick Lewycky | aad475b | 2014-04-15 07:22:52 +0000 | [diff] [blame] | 991 | const Value *V1 = Load->getMemOperand()->getValue(); |
| 992 | const Value *V2 = Store->getMemOperand()->getValue(); |
Richard Sandiford | 9784649 | 2013-07-09 09:46:39 +0000 | [diff] [blame] | 993 | if (!V1 || !V2) |
| 994 | return false; |
| 995 | |
Richard Sandiford | 067817e | 2013-09-27 15:29:20 +0000 | [diff] [blame] | 996 | // Reject equality. |
| 997 | uint64_t Size = Load->getMemoryVT().getStoreSize(); |
Richard Sandiford | 9784649 | 2013-07-09 09:46:39 +0000 | [diff] [blame] | 998 | int64_t End1 = Load->getSrcValueOffset() + Size; |
| 999 | int64_t End2 = Store->getSrcValueOffset() + Size; |
Richard Sandiford | 067817e | 2013-09-27 15:29:20 +0000 | [diff] [blame] | 1000 | if (V1 == V2 && End1 == End2) |
| 1001 | return false; |
| 1002 | |
Richard Sandiford | 9784649 | 2013-07-09 09:46:39 +0000 | [diff] [blame] | 1003 | return !AA->alias(AliasAnalysis::Location(V1, End1, Load->getTBAAInfo()), |
| 1004 | AliasAnalysis::Location(V2, End2, Store->getTBAAInfo())); |
| 1005 | } |
| 1006 | |
Richard Sandiford | 178273a | 2013-09-05 10:36:45 +0000 | [diff] [blame] | 1007 | bool SystemZDAGToDAGISel::storeLoadCanUseMVC(SDNode *N) const { |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1008 | auto *Store = cast<StoreSDNode>(N); |
| 1009 | auto *Load = cast<LoadSDNode>(Store->getValue()); |
Richard Sandiford | 178273a | 2013-09-05 10:36:45 +0000 | [diff] [blame] | 1010 | |
| 1011 | // Prefer not to use MVC if either address can use ... RELATIVE LONG |
| 1012 | // instructions. |
| 1013 | uint64_t Size = Load->getMemoryVT().getStoreSize(); |
| 1014 | if (Size > 1 && Size <= 8) { |
| 1015 | // Prefer LHRL, LRL and LGRL. |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 1016 | if (SystemZISD::isPCREL(Load->getBasePtr().getOpcode())) |
Richard Sandiford | 178273a | 2013-09-05 10:36:45 +0000 | [diff] [blame] | 1017 | return false; |
| 1018 | // Prefer STHRL, STRL and STGRL. |
Richard Sandiford | 54b3691 | 2013-09-27 15:14:04 +0000 | [diff] [blame] | 1019 | if (SystemZISD::isPCREL(Store->getBasePtr().getOpcode())) |
Richard Sandiford | 178273a | 2013-09-05 10:36:45 +0000 | [diff] [blame] | 1020 | return false; |
| 1021 | } |
| 1022 | |
Richard Sandiford | 067817e | 2013-09-27 15:29:20 +0000 | [diff] [blame] | 1023 | return canUseBlockOperation(Store, Load); |
Richard Sandiford | 178273a | 2013-09-05 10:36:45 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | bool SystemZDAGToDAGISel::storeLoadCanUseBlockBinary(SDNode *N, |
| 1027 | unsigned I) const { |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1028 | auto *StoreA = cast<StoreSDNode>(N); |
| 1029 | auto *LoadA = cast<LoadSDNode>(StoreA->getValue().getOperand(1 - I)); |
| 1030 | auto *LoadB = cast<LoadSDNode>(StoreA->getValue().getOperand(I)); |
Richard Sandiford | 067817e | 2013-09-27 15:29:20 +0000 | [diff] [blame] | 1031 | return !LoadA->isVolatile() && canUseBlockOperation(StoreA, LoadB); |
Richard Sandiford | 178273a | 2013-09-05 10:36:45 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1034 | SDNode *SystemZDAGToDAGISel::Select(SDNode *Node) { |
| 1035 | // Dump information about the Node being selected |
| 1036 | DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n"); |
| 1037 | |
| 1038 | // If we have a custom node, we already have selected! |
| 1039 | if (Node->isMachineOpcode()) { |
| 1040 | DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n"); |
Tim Northover | 31d093c | 2013-09-22 08:21:56 +0000 | [diff] [blame] | 1041 | Node->setNodeId(-1); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame^] | 1042 | return nullptr; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | unsigned Opcode = Node->getOpcode(); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame^] | 1046 | SDNode *ResNode = nullptr; |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1047 | switch (Opcode) { |
| 1048 | case ISD::OR: |
Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 1049 | if (Node->getOperand(1).getOpcode() != ISD::Constant) |
Richard Sandiford | 7878b85 | 2013-07-18 10:06:15 +0000 | [diff] [blame] | 1050 | ResNode = tryRxSBG(Node, SystemZ::ROSBG); |
| 1051 | goto or_xor; |
| 1052 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1053 | case ISD::XOR: |
Richard Sandiford | 7878b85 | 2013-07-18 10:06:15 +0000 | [diff] [blame] | 1054 | if (Node->getOperand(1).getOpcode() != ISD::Constant) |
| 1055 | ResNode = tryRxSBG(Node, SystemZ::RXSBG); |
| 1056 | // Fall through. |
| 1057 | or_xor: |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1058 | // If this is a 64-bit operation in which both 32-bit halves are nonzero, |
| 1059 | // split the operation into two. |
Richard Sandiford | 885140c | 2013-07-16 11:55:57 +0000 | [diff] [blame] | 1060 | if (!ResNode && Node->getValueType(0) == MVT::i64) |
Richard Sandiford | 21f5d68 | 2014-03-06 11:22:58 +0000 | [diff] [blame] | 1061 | if (auto *Op1 = dyn_cast<ConstantSDNode>(Node->getOperand(1))) { |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1062 | uint64_t Val = Op1->getZExtValue(); |
| 1063 | if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val)) |
| 1064 | Node = splitLargeImmediate(Opcode, Node, Node->getOperand(0), |
| 1065 | Val - uint32_t(Val), uint32_t(Val)); |
| 1066 | } |
| 1067 | break; |
| 1068 | |
Richard Sandiford | 84f54a3 | 2013-07-11 08:59:12 +0000 | [diff] [blame] | 1069 | case ISD::AND: |
Richard Sandiford | 5109321 | 2013-07-18 10:40:35 +0000 | [diff] [blame] | 1070 | if (Node->getOperand(1).getOpcode() != ISD::Constant) |
| 1071 | ResNode = tryRxSBG(Node, SystemZ::RNSBG); |
| 1072 | // Fall through. |
Richard Sandiford | 82ec87d | 2013-07-16 11:02:24 +0000 | [diff] [blame] | 1073 | case ISD::ROTL: |
| 1074 | case ISD::SHL: |
| 1075 | case ISD::SRL: |
Richard Sandiford | 220ee49 | 2013-12-20 11:49:48 +0000 | [diff] [blame] | 1076 | case ISD::ZERO_EXTEND: |
Richard Sandiford | 7878b85 | 2013-07-18 10:06:15 +0000 | [diff] [blame] | 1077 | if (!ResNode) |
| 1078 | ResNode = tryRISBGZero(Node); |
Richard Sandiford | 84f54a3 | 2013-07-11 08:59:12 +0000 | [diff] [blame] | 1079 | break; |
| 1080 | |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1081 | case ISD::Constant: |
| 1082 | // If this is a 64-bit constant that is out of the range of LLILF, |
| 1083 | // LLIHF and LGFI, split it into two 32-bit pieces. |
| 1084 | if (Node->getValueType(0) == MVT::i64) { |
| 1085 | uint64_t Val = cast<ConstantSDNode>(Node)->getZExtValue(); |
| 1086 | if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val) && !isInt<32>(Val)) |
| 1087 | Node = splitLargeImmediate(ISD::OR, Node, SDValue(), |
| 1088 | Val - uint32_t(Val), uint32_t(Val)); |
| 1089 | } |
| 1090 | break; |
| 1091 | |
Richard Sandiford | ee83438 | 2013-07-31 12:38:08 +0000 | [diff] [blame] | 1092 | case SystemZISD::SELECT_CCMASK: { |
| 1093 | SDValue Op0 = Node->getOperand(0); |
| 1094 | SDValue Op1 = Node->getOperand(1); |
| 1095 | // Prefer to put any load first, so that it can be matched as a |
| 1096 | // conditional load. |
| 1097 | if (Op1.getOpcode() == ISD::LOAD && Op0.getOpcode() != ISD::LOAD) { |
| 1098 | SDValue CCValid = Node->getOperand(2); |
| 1099 | SDValue CCMask = Node->getOperand(3); |
| 1100 | uint64_t ConstCCValid = |
| 1101 | cast<ConstantSDNode>(CCValid.getNode())->getZExtValue(); |
| 1102 | uint64_t ConstCCMask = |
| 1103 | cast<ConstantSDNode>(CCMask.getNode())->getZExtValue(); |
| 1104 | // Invert the condition. |
| 1105 | CCMask = CurDAG->getConstant(ConstCCValid ^ ConstCCMask, |
| 1106 | CCMask.getValueType()); |
| 1107 | SDValue Op4 = Node->getOperand(4); |
| 1108 | Node = CurDAG->UpdateNodeOperands(Node, Op1, Op0, CCValid, CCMask, Op4); |
| 1109 | } |
| 1110 | break; |
| 1111 | } |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | // Select the default instruction |
Richard Sandiford | 84f54a3 | 2013-07-11 08:59:12 +0000 | [diff] [blame] | 1115 | if (!ResNode) |
| 1116 | ResNode = SelectCode(Node); |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1117 | |
| 1118 | DEBUG(errs() << "=> "; |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame^] | 1119 | if (ResNode == nullptr || ResNode == Node) |
Ulrich Weigand | 5f613df | 2013-05-06 16:15:19 +0000 | [diff] [blame] | 1120 | Node->dump(CurDAG); |
| 1121 | else |
| 1122 | ResNode->dump(CurDAG); |
| 1123 | errs() << "\n"; |
| 1124 | ); |
| 1125 | return ResNode; |
| 1126 | } |
| 1127 | |
| 1128 | bool SystemZDAGToDAGISel:: |
| 1129 | SelectInlineAsmMemoryOperand(const SDValue &Op, |
| 1130 | char ConstraintCode, |
| 1131 | std::vector<SDValue> &OutOps) { |
| 1132 | assert(ConstraintCode == 'm' && "Unexpected constraint code"); |
| 1133 | // Accept addresses with short displacements, which are compatible |
| 1134 | // with Q, R, S and T. But keep the index operand for future expansion. |
| 1135 | SDValue Base, Disp, Index; |
| 1136 | if (!selectBDXAddr(SystemZAddressingMode::FormBD, |
| 1137 | SystemZAddressingMode::Disp12Only, |
| 1138 | Op, Base, Disp, Index)) |
| 1139 | return true; |
| 1140 | OutOps.push_back(Base); |
| 1141 | OutOps.push_back(Disp); |
| 1142 | OutOps.push_back(Index); |
| 1143 | return false; |
| 1144 | } |