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