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