blob: cd2f708458bfc1ab8c9b25a7487c867193de86e6 [file] [log] [blame]
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001//===-- SystemZISelDAGToDAG.cpp - A dag to dag inst selector for SystemZ --===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines an instruction selector for the SystemZ target.
11//
12//===----------------------------------------------------------------------===//
13
14#include "SystemZTargetMachine.h"
Richard Sandiford97846492013-07-09 09:46:39 +000015#include "llvm/Analysis/AliasAnalysis.h"
Ulrich Weigand5f613df2013-05-06 16:15:19 +000016#include "llvm/CodeGen/SelectionDAGISel.h"
17#include "llvm/Support/Debug.h"
Craig Topperd0af7e82017-04-28 05:31:46 +000018#include "llvm/Support/KnownBits.h"
Ulrich Weigand5f613df2013-05-06 16:15:19 +000019#include "llvm/Support/raw_ostream.h"
20
21using namespace llvm;
22
Chandler Carruthe96dd892014-04-21 22:55:11 +000023#define DEBUG_TYPE "systemz-isel"
24
Ulrich Weigand5f613df2013-05-06 16:15:19 +000025namespace {
26// Used to build addressing modes.
27struct SystemZAddressingMode {
28 // The shape of the address.
29 enum AddrForm {
30 // base+displacement
31 FormBD,
32
33 // base+displacement+index for load and store operands
34 FormBDXNormal,
35
36 // base+displacement+index for load address operands
37 FormBDXLA,
38
39 // base+displacement+index+ADJDYNALLOC
40 FormBDXDynAlloc
41 };
42 AddrForm Form;
43
44 // The type of displacement. The enum names here correspond directly
45 // to the definitions in SystemZOperand.td. We could split them into
46 // flags -- single/pair, 128-bit, etc. -- but it hardly seems worth it.
47 enum DispRange {
48 Disp12Only,
49 Disp12Pair,
50 Disp20Only,
51 Disp20Only128,
52 Disp20Pair
53 };
54 DispRange DR;
55
56 // The parts of the address. The address is equivalent to:
57 //
58 // Base + Disp + Index + (IncludesDynAlloc ? ADJDYNALLOC : 0)
59 SDValue Base;
60 int64_t Disp;
61 SDValue Index;
62 bool IncludesDynAlloc;
63
64 SystemZAddressingMode(AddrForm form, DispRange dr)
65 : Form(form), DR(dr), Base(), Disp(0), Index(),
66 IncludesDynAlloc(false) {}
67
68 // True if the address can have an index register.
69 bool hasIndexField() { return Form != FormBD; }
70
71 // True if the address can (and must) include ADJDYNALLOC.
72 bool isDynAlloc() { return Form == FormBDXDynAlloc; }
73
74 void dump() {
75 errs() << "SystemZAddressingMode " << this << '\n';
76
77 errs() << " Base ";
Craig Topper062a2ba2014-04-25 05:30:21 +000078 if (Base.getNode())
Ulrich Weigand5f613df2013-05-06 16:15:19 +000079 Base.getNode()->dump();
80 else
81 errs() << "null\n";
82
83 if (hasIndexField()) {
84 errs() << " Index ";
Craig Topper062a2ba2014-04-25 05:30:21 +000085 if (Index.getNode())
Ulrich Weigand5f613df2013-05-06 16:15:19 +000086 Index.getNode()->dump();
87 else
88 errs() << "null\n";
89 }
90
91 errs() << " Disp " << Disp;
92 if (IncludesDynAlloc)
93 errs() << " + ADJDYNALLOC";
94 errs() << '\n';
95 }
96};
97
Richard Sandiford82ec87d2013-07-16 11:02:24 +000098// Return a mask with Count low bits set.
99static uint64_t allOnes(unsigned int Count) {
Ulrich Weigand77884bc2015-06-25 11:52:36 +0000100 assert(Count <= 64);
Justin Bognerc97c48a2015-06-24 05:59:19 +0000101 if (Count > 63)
102 return UINT64_MAX;
103 return (uint64_t(1) << Count) - 1;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000104}
105
Richard Sandiford51093212013-07-18 10:40:35 +0000106// Represents operands 2 to 5 of the ROTATE AND ... SELECTED BITS operation
107// given by Opcode. The operands are: Input (R2), Start (I3), End (I4) and
108// Rotate (I5). The combined operand value is effectively:
109//
110// (or (rotl Input, Rotate), ~Mask)
111//
112// for RNSBG and:
113//
114// (and (rotl Input, Rotate), Mask)
115//
Richard Sandiford3e382972013-10-16 13:35:13 +0000116// otherwise. The output value has BitSize bits, although Input may be
Zhan Jun Liau0df35052016-06-22 16:16:27 +0000117// narrower (in which case the upper bits are don't care), or wider (in which
118// case the result will be truncated as part of the operation).
Richard Sandiford5cbac962013-07-18 09:45:08 +0000119struct RxSBGOperands {
Richard Sandiford51093212013-07-18 10:40:35 +0000120 RxSBGOperands(unsigned Op, SDValue N)
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +0000121 : Opcode(Op), BitSize(N.getValueSizeInBits()),
Richard Sandiford51093212013-07-18 10:40:35 +0000122 Mask(allOnes(BitSize)), Input(N), Start(64 - BitSize), End(63),
123 Rotate(0) {}
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000124
Richard Sandiford51093212013-07-18 10:40:35 +0000125 unsigned Opcode;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000126 unsigned BitSize;
127 uint64_t Mask;
128 SDValue Input;
129 unsigned Start;
130 unsigned End;
131 unsigned Rotate;
132};
133
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000134class SystemZDAGToDAGISel : public SelectionDAGISel {
Eric Christophera6734172015-01-31 00:06:45 +0000135 const SystemZSubtarget *Subtarget;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000136
137 // Used by SystemZOperands.td to create integer constants.
Richard Sandiford54b36912013-09-27 15:14:04 +0000138 inline SDValue getImm(const SDNode *Node, uint64_t Imm) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000139 return CurDAG->getTargetConstant(Imm, SDLoc(Node), Node->getValueType(0));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000140 }
141
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000142 const SystemZTargetMachine &getTargetMachine() const {
143 return static_cast<const SystemZTargetMachine &>(TM);
144 }
145
146 const SystemZInstrInfo *getInstrInfo() const {
Eric Christophera6734172015-01-31 00:06:45 +0000147 return Subtarget->getInstrInfo();
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000148 }
149
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000150 // Try to fold more of the base or index of AM into AM, where IsBase
151 // selects between the base and index.
Richard Sandiford54b36912013-09-27 15:14:04 +0000152 bool expandAddress(SystemZAddressingMode &AM, bool IsBase) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000153
154 // Try to describe N in AM, returning true on success.
Richard Sandiford54b36912013-09-27 15:14:04 +0000155 bool selectAddress(SDValue N, SystemZAddressingMode &AM) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000156
157 // Extract individual target operands from matched address AM.
158 void getAddressOperands(const SystemZAddressingMode &AM, EVT VT,
Richard Sandiford54b36912013-09-27 15:14:04 +0000159 SDValue &Base, SDValue &Disp) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000160 void getAddressOperands(const SystemZAddressingMode &AM, EVT VT,
Richard Sandiford54b36912013-09-27 15:14:04 +0000161 SDValue &Base, SDValue &Disp, SDValue &Index) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000162
163 // Try to match Addr as a FormBD address with displacement type DR.
164 // Return true on success, storing the base and displacement in
165 // Base and Disp respectively.
166 bool selectBDAddr(SystemZAddressingMode::DispRange DR, SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000167 SDValue &Base, SDValue &Disp) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000168
Richard Sandiforda481f582013-08-23 11:18:53 +0000169 // Try to match Addr as a FormBDX address with displacement type DR.
170 // Return true on success and if the result had no index. Store the
171 // base and displacement in Base and Disp respectively.
172 bool selectMVIAddr(SystemZAddressingMode::DispRange DR, SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000173 SDValue &Base, SDValue &Disp) const;
Richard Sandiforda481f582013-08-23 11:18:53 +0000174
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000175 // Try to match Addr as a FormBDX* address of form Form with
176 // displacement type DR. Return true on success, storing the base,
177 // displacement and index in Base, Disp and Index respectively.
178 bool selectBDXAddr(SystemZAddressingMode::AddrForm Form,
179 SystemZAddressingMode::DispRange DR, SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000180 SDValue &Base, SDValue &Disp, SDValue &Index) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000181
182 // PC-relative address matching routines used by SystemZOperands.td.
Richard Sandiford54b36912013-09-27 15:14:04 +0000183 bool selectPCRelAddress(SDValue Addr, SDValue &Target) const {
184 if (SystemZISD::isPCREL(Addr.getOpcode())) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000185 Target = Addr.getOperand(0);
186 return true;
187 }
188 return false;
189 }
190
191 // BD matching routines used by SystemZOperands.td.
Richard Sandiford54b36912013-09-27 15:14:04 +0000192 bool selectBDAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000193 return selectBDAddr(SystemZAddressingMode::Disp12Only, Addr, Base, Disp);
194 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000195 bool selectBDAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000196 return selectBDAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp);
197 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000198 bool selectBDAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000199 return selectBDAddr(SystemZAddressingMode::Disp20Only, Addr, Base, Disp);
200 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000201 bool selectBDAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000202 return selectBDAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp);
203 }
204
Richard Sandiforda481f582013-08-23 11:18:53 +0000205 // MVI matching routines used by SystemZOperands.td.
Richard Sandiford54b36912013-09-27 15:14:04 +0000206 bool selectMVIAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Richard Sandiforda481f582013-08-23 11:18:53 +0000207 return selectMVIAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp);
208 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000209 bool selectMVIAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Richard Sandiforda481f582013-08-23 11:18:53 +0000210 return selectMVIAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp);
211 }
212
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000213 // BDX matching routines used by SystemZOperands.td.
214 bool selectBDXAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000215 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000216 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
217 SystemZAddressingMode::Disp12Only,
218 Addr, Base, Disp, Index);
219 }
220 bool selectBDXAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000221 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000222 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
223 SystemZAddressingMode::Disp12Pair,
224 Addr, Base, Disp, Index);
225 }
226 bool selectDynAlloc12Only(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000227 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000228 return selectBDXAddr(SystemZAddressingMode::FormBDXDynAlloc,
229 SystemZAddressingMode::Disp12Only,
230 Addr, Base, Disp, Index);
231 }
232 bool selectBDXAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000233 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000234 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
235 SystemZAddressingMode::Disp20Only,
236 Addr, Base, Disp, Index);
237 }
238 bool selectBDXAddr20Only128(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000239 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000240 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
241 SystemZAddressingMode::Disp20Only128,
242 Addr, Base, Disp, Index);
243 }
244 bool selectBDXAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000245 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000246 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
247 SystemZAddressingMode::Disp20Pair,
248 Addr, Base, Disp, Index);
249 }
250 bool selectLAAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000251 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000252 return selectBDXAddr(SystemZAddressingMode::FormBDXLA,
253 SystemZAddressingMode::Disp12Pair,
254 Addr, Base, Disp, Index);
255 }
256 bool selectLAAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000257 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000258 return selectBDXAddr(SystemZAddressingMode::FormBDXLA,
259 SystemZAddressingMode::Disp20Pair,
260 Addr, Base, Disp, Index);
261 }
262
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000263 // Try to match Addr as an address with a base, 12-bit displacement
264 // and index, where the index is element Elem of a vector.
265 // Return true on success, storing the base, displacement and vector
266 // in Base, Disp and Index respectively.
267 bool selectBDVAddr12Only(SDValue Addr, SDValue Elem, SDValue &Base,
268 SDValue &Disp, SDValue &Index) const;
269
Richard Sandiford885140c2013-07-16 11:55:57 +0000270 // Check whether (or Op (and X InsertMask)) is effectively an insertion
271 // of X into bits InsertMask of some Y != Op. Return true if so and
272 // set Op to that Y.
Richard Sandiford54b36912013-09-27 15:14:04 +0000273 bool detectOrAndInsertion(SDValue &Op, uint64_t InsertMask) const;
Richard Sandiford885140c2013-07-16 11:55:57 +0000274
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000275 // Try to update RxSBG so that only the bits of RxSBG.Input in Mask are used.
276 // Return true on success.
Richard Sandiford54b36912013-09-27 15:14:04 +0000277 bool refineRxSBGMask(RxSBGOperands &RxSBG, uint64_t Mask) const;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000278
Richard Sandiford5cbac962013-07-18 09:45:08 +0000279 // Try to fold some of RxSBG.Input into other fields of RxSBG.
280 // Return true on success.
Richard Sandiford54b36912013-09-27 15:14:04 +0000281 bool expandRxSBG(RxSBGOperands &RxSBG) const;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000282
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000283 // Return an undefined value of type VT.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000284 SDValue getUNDEF(const SDLoc &DL, EVT VT) const;
Richard Sandiford84f54a32013-07-11 08:59:12 +0000285
286 // Convert N to VT, if it isn't already.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000287 SDValue convertTo(const SDLoc &DL, EVT VT, SDValue N) const;
Richard Sandiford84f54a32013-07-11 08:59:12 +0000288
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000289 // Try to implement AND or shift node N using RISBG with the zero flag set.
290 // Return the selected node on success, otherwise return null.
Justin Bognerbbcd2232016-05-10 21:11:26 +0000291 bool tryRISBGZero(SDNode *N);
Richard Sandiford84f54a32013-07-11 08:59:12 +0000292
Richard Sandiford7878b852013-07-18 10:06:15 +0000293 // Try to use RISBG or Opcode to implement OR or XOR node N.
294 // Return the selected node on success, otherwise return null.
Justin Bogner9b34e8a2016-05-13 22:42:08 +0000295 bool tryRxSBG(SDNode *N, unsigned Opcode);
Richard Sandiford885140c2013-07-16 11:55:57 +0000296
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000297 // If Op0 is null, then Node is a constant that can be loaded using:
298 //
299 // (Opcode UpperVal LowerVal)
300 //
301 // If Op0 is nonnull, then Node can be implemented using:
302 //
303 // (Opcode (Opcode Op0 UpperVal) LowerVal)
Justin Bognerffb273d2016-05-09 23:54:23 +0000304 void splitLargeImmediate(unsigned Opcode, SDNode *Node, SDValue Op0,
305 uint64_t UpperVal, uint64_t LowerVal);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000306
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000307 // Try to use gather instruction Opcode to implement vector insertion N.
Justin Bogner9b34e8a2016-05-13 22:42:08 +0000308 bool tryGather(SDNode *N, unsigned Opcode);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000309
310 // Try to use scatter instruction Opcode to implement store Store.
Justin Bogner9b34e8a2016-05-13 22:42:08 +0000311 bool tryScatter(StoreSDNode *Store, unsigned Opcode);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000312
Richard Sandiford067817e2013-09-27 15:29:20 +0000313 // Return true if Load and Store are loads and stores of the same size
314 // and are guaranteed not to overlap. Such operations can be implemented
315 // using block (SS-format) instructions.
316 //
317 // Partial overlap would lead to incorrect code, since the block operations
318 // are logically bytewise, even though they have a fast path for the
319 // non-overlapping case. We also need to avoid full overlap (i.e. two
320 // addresses that might be equal at run time) because although that case
321 // would be handled correctly, it might be implemented by millicode.
322 bool canUseBlockOperation(StoreSDNode *Store, LoadSDNode *Load) const;
323
Richard Sandiford178273a2013-09-05 10:36:45 +0000324 // N is a (store (load Y), X) pattern. Return true if it can use an MVC
325 // from Y to X.
Richard Sandiford97846492013-07-09 09:46:39 +0000326 bool storeLoadCanUseMVC(SDNode *N) const;
327
Richard Sandiford178273a2013-09-05 10:36:45 +0000328 // N is a (store (op (load A[0]), (load A[1])), X) pattern. Return true
329 // if A[1 - I] == X and if N can use a block operation like NC from A[I]
330 // to X.
331 bool storeLoadCanUseBlockBinary(SDNode *N, unsigned I) const;
332
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000333public:
334 SystemZDAGToDAGISel(SystemZTargetMachine &TM, CodeGenOpt::Level OptLevel)
Eric Christophera6734172015-01-31 00:06:45 +0000335 : SelectionDAGISel(TM, OptLevel) {}
336
337 bool runOnMachineFunction(MachineFunction &MF) override {
338 Subtarget = &MF.getSubtarget<SystemZSubtarget>();
339 return SelectionDAGISel::runOnMachineFunction(MF);
340 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000341
342 // Override MachineFunctionPass.
Mehdi Amini117296c2016-10-01 02:56:57 +0000343 StringRef getPassName() const override {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000344 return "SystemZ DAG->DAG Pattern Instruction Selection";
345 }
346
347 // Override SelectionDAGISel.
Justin Bogner9b34e8a2016-05-13 22:42:08 +0000348 void Select(SDNode *Node) override;
Daniel Sanders60f1db02015-03-13 12:45:09 +0000349 bool SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000350 std::vector<SDValue> &OutOps) override;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000351
352 // Include the pieces autogenerated from the target description.
353 #include "SystemZGenDAGISel.inc"
354};
355} // end anonymous namespace
356
357FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM,
358 CodeGenOpt::Level OptLevel) {
359 return new SystemZDAGToDAGISel(TM, OptLevel);
360}
361
362// Return true if Val should be selected as a displacement for an address
363// with range DR. Here we're interested in the range of both the instruction
364// described by DR and of any pairing instruction.
365static bool selectDisp(SystemZAddressingMode::DispRange DR, int64_t Val) {
366 switch (DR) {
367 case SystemZAddressingMode::Disp12Only:
368 return isUInt<12>(Val);
369
370 case SystemZAddressingMode::Disp12Pair:
371 case SystemZAddressingMode::Disp20Only:
372 case SystemZAddressingMode::Disp20Pair:
373 return isInt<20>(Val);
374
375 case SystemZAddressingMode::Disp20Only128:
376 return isInt<20>(Val) && isInt<20>(Val + 8);
377 }
378 llvm_unreachable("Unhandled displacement range");
379}
380
381// Change the base or index in AM to Value, where IsBase selects
382// between the base and index.
383static void changeComponent(SystemZAddressingMode &AM, bool IsBase,
384 SDValue Value) {
385 if (IsBase)
386 AM.Base = Value;
387 else
388 AM.Index = Value;
389}
390
391// The base or index of AM is equivalent to Value + ADJDYNALLOC,
392// where IsBase selects between the base and index. Try to fold the
393// ADJDYNALLOC into AM.
394static bool expandAdjDynAlloc(SystemZAddressingMode &AM, bool IsBase,
395 SDValue Value) {
396 if (AM.isDynAlloc() && !AM.IncludesDynAlloc) {
397 changeComponent(AM, IsBase, Value);
398 AM.IncludesDynAlloc = true;
399 return true;
400 }
401 return false;
402}
403
404// The base of AM is equivalent to Base + Index. Try to use Index as
405// the index register.
406static bool expandIndex(SystemZAddressingMode &AM, SDValue Base,
407 SDValue Index) {
408 if (AM.hasIndexField() && !AM.Index.getNode()) {
409 AM.Base = Base;
410 AM.Index = Index;
411 return true;
412 }
413 return false;
414}
415
416// The base or index of AM is equivalent to Op0 + Op1, where IsBase selects
417// between the base and index. Try to fold Op1 into AM's displacement.
418static bool expandDisp(SystemZAddressingMode &AM, bool IsBase,
Richard Sandiford54b36912013-09-27 15:14:04 +0000419 SDValue Op0, uint64_t Op1) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000420 // First try adjusting the displacement.
Richard Sandiford54b36912013-09-27 15:14:04 +0000421 int64_t TestDisp = AM.Disp + Op1;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000422 if (selectDisp(AM.DR, TestDisp)) {
423 changeComponent(AM, IsBase, Op0);
424 AM.Disp = TestDisp;
425 return true;
426 }
427
428 // We could consider forcing the displacement into a register and
429 // using it as an index, but it would need to be carefully tuned.
430 return false;
431}
432
433bool SystemZDAGToDAGISel::expandAddress(SystemZAddressingMode &AM,
Richard Sandiford54b36912013-09-27 15:14:04 +0000434 bool IsBase) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000435 SDValue N = IsBase ? AM.Base : AM.Index;
436 unsigned Opcode = N.getOpcode();
437 if (Opcode == ISD::TRUNCATE) {
438 N = N.getOperand(0);
439 Opcode = N.getOpcode();
440 }
441 if (Opcode == ISD::ADD || CurDAG->isBaseWithConstantOffset(N)) {
442 SDValue Op0 = N.getOperand(0);
443 SDValue Op1 = N.getOperand(1);
444
445 unsigned Op0Code = Op0->getOpcode();
446 unsigned Op1Code = Op1->getOpcode();
447
448 if (Op0Code == SystemZISD::ADJDYNALLOC)
449 return expandAdjDynAlloc(AM, IsBase, Op1);
450 if (Op1Code == SystemZISD::ADJDYNALLOC)
451 return expandAdjDynAlloc(AM, IsBase, Op0);
452
453 if (Op0Code == ISD::Constant)
Richard Sandiford54b36912013-09-27 15:14:04 +0000454 return expandDisp(AM, IsBase, Op1,
455 cast<ConstantSDNode>(Op0)->getSExtValue());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000456 if (Op1Code == ISD::Constant)
Richard Sandiford54b36912013-09-27 15:14:04 +0000457 return expandDisp(AM, IsBase, Op0,
458 cast<ConstantSDNode>(Op1)->getSExtValue());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000459
460 if (IsBase && expandIndex(AM, Op0, Op1))
461 return true;
462 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000463 if (Opcode == SystemZISD::PCREL_OFFSET) {
464 SDValue Full = N.getOperand(0);
465 SDValue Base = N.getOperand(1);
466 SDValue Anchor = Base.getOperand(0);
467 uint64_t Offset = (cast<GlobalAddressSDNode>(Full)->getOffset() -
468 cast<GlobalAddressSDNode>(Anchor)->getOffset());
469 return expandDisp(AM, IsBase, Base, Offset);
470 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000471 return false;
472}
473
474// Return true if an instruction with displacement range DR should be
475// used for displacement value Val. selectDisp(DR, Val) must already hold.
476static bool isValidDisp(SystemZAddressingMode::DispRange DR, int64_t Val) {
477 assert(selectDisp(DR, Val) && "Invalid displacement");
478 switch (DR) {
479 case SystemZAddressingMode::Disp12Only:
480 case SystemZAddressingMode::Disp20Only:
481 case SystemZAddressingMode::Disp20Only128:
482 return true;
483
484 case SystemZAddressingMode::Disp12Pair:
485 // Use the other instruction if the displacement is too large.
486 return isUInt<12>(Val);
487
488 case SystemZAddressingMode::Disp20Pair:
489 // Use the other instruction if the displacement is small enough.
490 return !isUInt<12>(Val);
491 }
492 llvm_unreachable("Unhandled displacement range");
493}
494
495// Return true if Base + Disp + Index should be performed by LA(Y).
496static bool shouldUseLA(SDNode *Base, int64_t Disp, SDNode *Index) {
497 // Don't use LA(Y) for constants.
498 if (!Base)
499 return false;
500
501 // Always use LA(Y) for frame addresses, since we know that the destination
502 // register is almost always (perhaps always) going to be different from
503 // the frame register.
504 if (Base->getOpcode() == ISD::FrameIndex)
505 return true;
506
507 if (Disp) {
508 // Always use LA(Y) if there is a base, displacement and index.
509 if (Index)
510 return true;
511
512 // Always use LA if the displacement is small enough. It should always
513 // be no worse than AGHI (and better if it avoids a move).
514 if (isUInt<12>(Disp))
515 return true;
516
517 // For similar reasons, always use LAY if the constant is too big for AGHI.
518 // LAY should be no worse than AGFI.
519 if (!isInt<16>(Disp))
520 return true;
521 } else {
522 // Don't use LA for plain registers.
523 if (!Index)
524 return false;
525
526 // Don't use LA for plain addition if the index operand is only used
527 // once. It should be a natural two-operand addition in that case.
528 if (Index->hasOneUse())
529 return false;
530
531 // Prefer addition if the second operation is sign-extended, in the
532 // hope of using AGF.
533 unsigned IndexOpcode = Index->getOpcode();
534 if (IndexOpcode == ISD::SIGN_EXTEND ||
535 IndexOpcode == ISD::SIGN_EXTEND_INREG)
536 return false;
537 }
538
539 // Don't use LA for two-operand addition if either operand is only
540 // used once. The addition instructions are better in that case.
541 if (Base->hasOneUse())
542 return false;
543
544 return true;
545}
546
547// Return true if Addr is suitable for AM, updating AM if so.
548bool SystemZDAGToDAGISel::selectAddress(SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000549 SystemZAddressingMode &AM) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000550 // Start out assuming that the address will need to be loaded separately,
551 // then try to extend it as much as we can.
552 AM.Base = Addr;
553
554 // First try treating the address as a constant.
555 if (Addr.getOpcode() == ISD::Constant &&
Richard Sandiford54b36912013-09-27 15:14:04 +0000556 expandDisp(AM, true, SDValue(),
557 cast<ConstantSDNode>(Addr)->getSExtValue()))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000558 ;
Marcin Koscielnicki9de88d92016-05-04 23:31:26 +0000559 // Also see if it's a bare ADJDYNALLOC.
560 else if (Addr.getOpcode() == SystemZISD::ADJDYNALLOC &&
561 expandAdjDynAlloc(AM, true, SDValue()))
562 ;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000563 else
564 // Otherwise try expanding each component.
565 while (expandAddress(AM, true) ||
566 (AM.Index.getNode() && expandAddress(AM, false)))
567 continue;
568
569 // Reject cases where it isn't profitable to use LA(Y).
570 if (AM.Form == SystemZAddressingMode::FormBDXLA &&
571 !shouldUseLA(AM.Base.getNode(), AM.Disp, AM.Index.getNode()))
572 return false;
573
574 // Reject cases where the other instruction in a pair should be used.
575 if (!isValidDisp(AM.DR, AM.Disp))
576 return false;
577
578 // Make sure that ADJDYNALLOC is included where necessary.
579 if (AM.isDynAlloc() && !AM.IncludesDynAlloc)
580 return false;
581
582 DEBUG(AM.dump());
583 return true;
584}
585
586// Insert a node into the DAG at least before Pos. This will reposition
587// the node as needed, and will assign it a node ID that is <= Pos's ID.
588// Note that this does *not* preserve the uniqueness of node IDs!
589// The selection DAG must no longer depend on their uniqueness when this
590// function is used.
591static void insertDAGNode(SelectionDAG *DAG, SDNode *Pos, SDValue N) {
592 if (N.getNode()->getNodeId() == -1 ||
593 N.getNode()->getNodeId() > Pos->getNodeId()) {
Duncan P. N. Exon Smitha2c90e42015-10-20 01:12:46 +0000594 DAG->RepositionNode(Pos->getIterator(), N.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000595 N.getNode()->setNodeId(Pos->getNodeId());
596 }
597}
598
599void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
600 EVT VT, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000601 SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000602 Base = AM.Base;
603 if (!Base.getNode())
604 // Register 0 means "no base". This is mostly useful for shifts.
605 Base = CurDAG->getRegister(0, VT);
606 else if (Base.getOpcode() == ISD::FrameIndex) {
607 // Lower a FrameIndex to a TargetFrameIndex.
608 int64_t FrameIndex = cast<FrameIndexSDNode>(Base)->getIndex();
609 Base = CurDAG->getTargetFrameIndex(FrameIndex, VT);
610 } else if (Base.getValueType() != VT) {
611 // Truncate values from i64 to i32, for shifts.
612 assert(VT == MVT::i32 && Base.getValueType() == MVT::i64 &&
613 "Unexpected truncation");
Andrew Trickef9de2a2013-05-25 02:42:55 +0000614 SDLoc DL(Base);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000615 SDValue Trunc = CurDAG->getNode(ISD::TRUNCATE, DL, VT, Base);
616 insertDAGNode(CurDAG, Base.getNode(), Trunc);
617 Base = Trunc;
618 }
619
620 // Lower the displacement to a TargetConstant.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000621 Disp = CurDAG->getTargetConstant(AM.Disp, SDLoc(Base), VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000622}
623
624void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
625 EVT VT, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000626 SDValue &Disp,
627 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000628 getAddressOperands(AM, VT, Base, Disp);
629
630 Index = AM.Index;
631 if (!Index.getNode())
632 // Register 0 means "no index".
633 Index = CurDAG->getRegister(0, VT);
634}
635
636bool SystemZDAGToDAGISel::selectBDAddr(SystemZAddressingMode::DispRange DR,
637 SDValue Addr, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000638 SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000639 SystemZAddressingMode AM(SystemZAddressingMode::FormBD, DR);
640 if (!selectAddress(Addr, AM))
641 return false;
642
643 getAddressOperands(AM, Addr.getValueType(), Base, Disp);
644 return true;
645}
646
Richard Sandiforda481f582013-08-23 11:18:53 +0000647bool SystemZDAGToDAGISel::selectMVIAddr(SystemZAddressingMode::DispRange DR,
648 SDValue Addr, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000649 SDValue &Disp) const {
Richard Sandiforda481f582013-08-23 11:18:53 +0000650 SystemZAddressingMode AM(SystemZAddressingMode::FormBDXNormal, DR);
651 if (!selectAddress(Addr, AM) || AM.Index.getNode())
652 return false;
653
654 getAddressOperands(AM, Addr.getValueType(), Base, Disp);
655 return true;
656}
657
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000658bool SystemZDAGToDAGISel::selectBDXAddr(SystemZAddressingMode::AddrForm Form,
659 SystemZAddressingMode::DispRange DR,
660 SDValue Addr, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000661 SDValue &Disp, SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000662 SystemZAddressingMode AM(Form, DR);
663 if (!selectAddress(Addr, AM))
664 return false;
665
666 getAddressOperands(AM, Addr.getValueType(), Base, Disp, Index);
667 return true;
668}
669
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000670bool SystemZDAGToDAGISel::selectBDVAddr12Only(SDValue Addr, SDValue Elem,
671 SDValue &Base,
672 SDValue &Disp,
673 SDValue &Index) const {
674 SDValue Regs[2];
675 if (selectBDXAddr12Only(Addr, Regs[0], Disp, Regs[1]) &&
676 Regs[0].getNode() && Regs[1].getNode()) {
677 for (unsigned int I = 0; I < 2; ++I) {
678 Base = Regs[I];
679 Index = Regs[1 - I];
680 // We can't tell here whether the index vector has the right type
681 // for the access; the caller needs to do that instead.
682 if (Index.getOpcode() == ISD::ZERO_EXTEND)
683 Index = Index.getOperand(0);
684 if (Index.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
685 Index.getOperand(1) == Elem) {
686 Index = Index.getOperand(0);
687 return true;
688 }
689 }
690 }
691 return false;
692}
693
Richard Sandiford885140c2013-07-16 11:55:57 +0000694bool SystemZDAGToDAGISel::detectOrAndInsertion(SDValue &Op,
Richard Sandiford54b36912013-09-27 15:14:04 +0000695 uint64_t InsertMask) const {
Richard Sandiford885140c2013-07-16 11:55:57 +0000696 // We're only interested in cases where the insertion is into some operand
697 // of Op, rather than into Op itself. The only useful case is an AND.
698 if (Op.getOpcode() != ISD::AND)
699 return false;
700
701 // We need a constant mask.
Richard Sandiford21f5d682014-03-06 11:22:58 +0000702 auto *MaskNode = dyn_cast<ConstantSDNode>(Op.getOperand(1).getNode());
Richard Sandiford885140c2013-07-16 11:55:57 +0000703 if (!MaskNode)
704 return false;
705
706 // It's not an insertion of Op.getOperand(0) if the two masks overlap.
707 uint64_t AndMask = MaskNode->getZExtValue();
708 if (InsertMask & AndMask)
709 return false;
710
711 // It's only an insertion if all bits are covered or are known to be zero.
712 // The inner check covers all cases but is more expensive.
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +0000713 uint64_t Used = allOnes(Op.getValueSizeInBits());
Richard Sandiford885140c2013-07-16 11:55:57 +0000714 if (Used != (AndMask | InsertMask)) {
Craig Topperd0af7e82017-04-28 05:31:46 +0000715 KnownBits Known;
716 CurDAG->computeKnownBits(Op.getOperand(0), Known);
717 if (Used != (AndMask | InsertMask | Known.Zero.getZExtValue()))
Richard Sandiford885140c2013-07-16 11:55:57 +0000718 return false;
719 }
720
721 Op = Op.getOperand(0);
722 return true;
723}
724
Richard Sandiford54b36912013-09-27 15:14:04 +0000725bool SystemZDAGToDAGISel::refineRxSBGMask(RxSBGOperands &RxSBG,
726 uint64_t Mask) const {
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000727 const SystemZInstrInfo *TII = getInstrInfo();
Richard Sandiford5cbac962013-07-18 09:45:08 +0000728 if (RxSBG.Rotate != 0)
729 Mask = (Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate));
730 Mask &= RxSBG.Mask;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000731 if (TII->isRxSBGMask(Mask, RxSBG.BitSize, RxSBG.Start, RxSBG.End)) {
Richard Sandiford5cbac962013-07-18 09:45:08 +0000732 RxSBG.Mask = Mask;
Richard Sandiford5cbac962013-07-18 09:45:08 +0000733 return true;
734 }
Richard Sandiford84f54a32013-07-11 08:59:12 +0000735 return false;
736}
737
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000738// Return true if any bits of (RxSBG.Input & Mask) are significant.
739static bool maskMatters(RxSBGOperands &RxSBG, uint64_t Mask) {
740 // Rotate the mask in the same way as RxSBG.Input is rotated.
Richard Sandiford297f7d22013-07-18 10:14:55 +0000741 if (RxSBG.Rotate != 0)
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000742 Mask = ((Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate)));
743 return (Mask & RxSBG.Mask) != 0;
Richard Sandiford297f7d22013-07-18 10:14:55 +0000744}
745
Richard Sandiford54b36912013-09-27 15:14:04 +0000746bool SystemZDAGToDAGISel::expandRxSBG(RxSBGOperands &RxSBG) const {
Richard Sandiford5cbac962013-07-18 09:45:08 +0000747 SDValue N = RxSBG.Input;
Richard Sandiford297f7d22013-07-18 10:14:55 +0000748 unsigned Opcode = N.getOpcode();
749 switch (Opcode) {
Zhan Jun Liau0df35052016-06-22 16:16:27 +0000750 case ISD::TRUNCATE: {
751 if (RxSBG.Opcode == SystemZ::RNSBG)
752 return false;
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +0000753 uint64_t BitSize = N.getValueSizeInBits();
Zhan Jun Liau0df35052016-06-22 16:16:27 +0000754 uint64_t Mask = allOnes(BitSize);
755 if (!refineRxSBGMask(RxSBG, Mask))
756 return false;
757 RxSBG.Input = N.getOperand(0);
758 return true;
759 }
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000760 case ISD::AND: {
Richard Sandiford51093212013-07-18 10:40:35 +0000761 if (RxSBG.Opcode == SystemZ::RNSBG)
762 return false;
763
Richard Sandiford21f5d682014-03-06 11:22:58 +0000764 auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000765 if (!MaskNode)
766 return false;
767
768 SDValue Input = N.getOperand(0);
769 uint64_t Mask = MaskNode->getZExtValue();
Richard Sandiford5cbac962013-07-18 09:45:08 +0000770 if (!refineRxSBGMask(RxSBG, Mask)) {
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000771 // If some bits of Input are already known zeros, those bits will have
772 // been removed from the mask. See if adding them back in makes the
773 // mask suitable.
Craig Topperd0af7e82017-04-28 05:31:46 +0000774 KnownBits Known;
775 CurDAG->computeKnownBits(Input, Known);
776 Mask |= Known.Zero.getZExtValue();
Richard Sandiford5cbac962013-07-18 09:45:08 +0000777 if (!refineRxSBGMask(RxSBG, Mask))
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000778 return false;
779 }
Richard Sandiford5cbac962013-07-18 09:45:08 +0000780 RxSBG.Input = Input;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000781 return true;
782 }
783
Richard Sandiford51093212013-07-18 10:40:35 +0000784 case ISD::OR: {
785 if (RxSBG.Opcode != SystemZ::RNSBG)
786 return false;
787
Richard Sandiford21f5d682014-03-06 11:22:58 +0000788 auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford51093212013-07-18 10:40:35 +0000789 if (!MaskNode)
790 return false;
791
792 SDValue Input = N.getOperand(0);
793 uint64_t Mask = ~MaskNode->getZExtValue();
794 if (!refineRxSBGMask(RxSBG, Mask)) {
795 // If some bits of Input are already known ones, those bits will have
796 // been removed from the mask. See if adding them back in makes the
797 // mask suitable.
Craig Topperd0af7e82017-04-28 05:31:46 +0000798 KnownBits Known;
799 CurDAG->computeKnownBits(Input, Known);
800 Mask &= ~Known.One.getZExtValue();
Richard Sandiford51093212013-07-18 10:40:35 +0000801 if (!refineRxSBGMask(RxSBG, Mask))
802 return false;
803 }
804 RxSBG.Input = Input;
805 return true;
806 }
807
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000808 case ISD::ROTL: {
Richard Sandiford5cbac962013-07-18 09:45:08 +0000809 // Any 64-bit rotate left can be merged into the RxSBG.
Richard Sandiford3e382972013-10-16 13:35:13 +0000810 if (RxSBG.BitSize != 64 || N.getValueType() != MVT::i64)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000811 return false;
Richard Sandiford21f5d682014-03-06 11:22:58 +0000812 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000813 if (!CountNode)
814 return false;
815
Richard Sandiford5cbac962013-07-18 09:45:08 +0000816 RxSBG.Rotate = (RxSBG.Rotate + CountNode->getZExtValue()) & 63;
817 RxSBG.Input = N.getOperand(0);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000818 return true;
819 }
Simon Pilgrim0750c842015-08-15 13:27:30 +0000820
Richard Sandiford220ee492013-12-20 11:49:48 +0000821 case ISD::ANY_EXTEND:
822 // Bits above the extended operand are don't-care.
823 RxSBG.Input = N.getOperand(0);
824 return true;
825
Richard Sandiford3875cb62014-01-09 11:28:53 +0000826 case ISD::ZERO_EXTEND:
827 if (RxSBG.Opcode != SystemZ::RNSBG) {
828 // Restrict the mask to the extended operand.
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +0000829 unsigned InnerBitSize = N.getOperand(0).getValueSizeInBits();
Richard Sandiford3875cb62014-01-09 11:28:53 +0000830 if (!refineRxSBGMask(RxSBG, allOnes(InnerBitSize)))
831 return false;
Richard Sandiford220ee492013-12-20 11:49:48 +0000832
Richard Sandiford3875cb62014-01-09 11:28:53 +0000833 RxSBG.Input = N.getOperand(0);
834 return true;
835 }
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000836 LLVM_FALLTHROUGH;
Simon Pilgrim0750c842015-08-15 13:27:30 +0000837
Richard Sandiford220ee492013-12-20 11:49:48 +0000838 case ISD::SIGN_EXTEND: {
Richard Sandiford3e382972013-10-16 13:35:13 +0000839 // Check that the extension bits are don't-care (i.e. are masked out
840 // by the final mask).
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +0000841 unsigned InnerBitSize = N.getOperand(0).getValueSizeInBits();
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000842 if (maskMatters(RxSBG, allOnes(RxSBG.BitSize) - allOnes(InnerBitSize)))
Richard Sandiford3e382972013-10-16 13:35:13 +0000843 return false;
844
845 RxSBG.Input = N.getOperand(0);
846 return true;
847 }
848
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000849 case ISD::SHL: {
Richard Sandiford21f5d682014-03-06 11:22:58 +0000850 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000851 if (!CountNode)
852 return false;
853
854 uint64_t Count = CountNode->getZExtValue();
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +0000855 unsigned BitSize = N.getValueSizeInBits();
Richard Sandiford3e382972013-10-16 13:35:13 +0000856 if (Count < 1 || Count >= BitSize)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000857 return false;
858
Richard Sandiford51093212013-07-18 10:40:35 +0000859 if (RxSBG.Opcode == SystemZ::RNSBG) {
860 // Treat (shl X, count) as (rotl X, size-count) as long as the bottom
861 // count bits from RxSBG.Input are ignored.
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000862 if (maskMatters(RxSBG, allOnes(Count)))
Richard Sandiford51093212013-07-18 10:40:35 +0000863 return false;
864 } else {
865 // Treat (shl X, count) as (and (rotl X, count), ~0<<count).
Richard Sandiford3e382972013-10-16 13:35:13 +0000866 if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count) << Count))
Richard Sandiford51093212013-07-18 10:40:35 +0000867 return false;
868 }
869
Richard Sandiford5cbac962013-07-18 09:45:08 +0000870 RxSBG.Rotate = (RxSBG.Rotate + Count) & 63;
871 RxSBG.Input = N.getOperand(0);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000872 return true;
873 }
874
Richard Sandiford297f7d22013-07-18 10:14:55 +0000875 case ISD::SRL:
876 case ISD::SRA: {
Richard Sandiford21f5d682014-03-06 11:22:58 +0000877 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000878 if (!CountNode)
879 return false;
880
881 uint64_t Count = CountNode->getZExtValue();
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +0000882 unsigned BitSize = N.getValueSizeInBits();
Richard Sandiford3e382972013-10-16 13:35:13 +0000883 if (Count < 1 || Count >= BitSize)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000884 return false;
885
Richard Sandiford51093212013-07-18 10:40:35 +0000886 if (RxSBG.Opcode == SystemZ::RNSBG || Opcode == ISD::SRA) {
887 // Treat (srl|sra X, count) as (rotl X, size-count) as long as the top
888 // count bits from RxSBG.Input are ignored.
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000889 if (maskMatters(RxSBG, allOnes(Count) << (BitSize - Count)))
Richard Sandiford297f7d22013-07-18 10:14:55 +0000890 return false;
891 } else {
892 // Treat (srl X, count), mask) as (and (rotl X, size-count), ~0>>count),
893 // which is similar to SLL above.
Richard Sandiford3e382972013-10-16 13:35:13 +0000894 if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count)))
Richard Sandiford297f7d22013-07-18 10:14:55 +0000895 return false;
896 }
897
Richard Sandiford5cbac962013-07-18 09:45:08 +0000898 RxSBG.Rotate = (RxSBG.Rotate - Count) & 63;
899 RxSBG.Input = N.getOperand(0);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000900 return true;
901 }
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000902 default:
903 return false;
904 }
905}
906
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000907SDValue SystemZDAGToDAGISel::getUNDEF(const SDLoc &DL, EVT VT) const {
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000908 SDNode *N = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, VT);
Richard Sandiford84f54a32013-07-11 08:59:12 +0000909 return SDValue(N, 0);
910}
911
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000912SDValue SystemZDAGToDAGISel::convertTo(const SDLoc &DL, EVT VT,
913 SDValue N) const {
Richard Sandifordd8163202013-09-13 09:12:44 +0000914 if (N.getValueType() == MVT::i32 && VT == MVT::i64)
Richard Sandiford87a44362013-09-30 10:28:35 +0000915 return CurDAG->getTargetInsertSubreg(SystemZ::subreg_l32,
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000916 DL, VT, getUNDEF(DL, MVT::i64), N);
Richard Sandifordd8163202013-09-13 09:12:44 +0000917 if (N.getValueType() == MVT::i64 && VT == MVT::i32)
Richard Sandiford87a44362013-09-30 10:28:35 +0000918 return CurDAG->getTargetExtractSubreg(SystemZ::subreg_l32, DL, VT, N);
Richard Sandiford84f54a32013-07-11 08:59:12 +0000919 assert(N.getValueType() == VT && "Unexpected value types");
920 return N;
921}
922
Justin Bognerbbcd2232016-05-10 21:11:26 +0000923bool SystemZDAGToDAGISel::tryRISBGZero(SDNode *N) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000924 SDLoc DL(N);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000925 EVT VT = N->getValueType(0);
Ulrich Weigand77884bc2015-06-25 11:52:36 +0000926 if (!VT.isInteger() || VT.getSizeInBits() > 64)
Justin Bognerbbcd2232016-05-10 21:11:26 +0000927 return false;
Richard Sandiford51093212013-07-18 10:40:35 +0000928 RxSBGOperands RISBG(SystemZ::RISBG, SDValue(N, 0));
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000929 unsigned Count = 0;
Richard Sandiford5cbac962013-07-18 09:45:08 +0000930 while (expandRxSBG(RISBG))
Zhan Jun Liau0df35052016-06-22 16:16:27 +0000931 // The widening or narrowing is expected to be free.
932 // Counting widening or narrowing as a saved operation will result in
933 // preferring an R*SBG over a simple shift/logical instruction.
934 if (RISBG.Input.getOpcode() != ISD::ANY_EXTEND &&
935 RISBG.Input.getOpcode() != ISD::TRUNCATE)
Richard Sandiford3e382972013-10-16 13:35:13 +0000936 Count += 1;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000937 if (Count == 0)
Justin Bognerbbcd2232016-05-10 21:11:26 +0000938 return false;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000939
Ulrich Weigand5dc7b672016-11-11 12:43:51 +0000940 // Prefer to use normal shift instructions over RISBG, since they can handle
941 // all cases and are sometimes shorter.
942 if (Count == 1 && N->getOpcode() != ISD::AND)
943 return false;
944
945 // Prefer register extensions like LLC over RISBG. Also prefer to start
946 // out with normal ANDs if one instruction would be enough. We can convert
947 // these ANDs into an RISBG later if a three-address instruction is useful.
948 if (RISBG.Rotate == 0) {
949 bool PreferAnd = false;
950 // Prefer AND for any 32-bit and-immediate operation.
951 if (VT == MVT::i32)
952 PreferAnd = true;
953 // As well as for any 64-bit operation that can be implemented via LLC(R),
954 // LLH(R), LLGT(R), or one of the and-immediate instructions.
955 else if (RISBG.Mask == 0xff ||
956 RISBG.Mask == 0xffff ||
957 RISBG.Mask == 0x7fffffff ||
958 SystemZ::isImmLF(~RISBG.Mask) ||
959 SystemZ::isImmHF(~RISBG.Mask))
960 PreferAnd = true;
Ulrich Weigand92c2c672016-11-11 12:46:28 +0000961 // And likewise for the LLZRGF instruction, which doesn't have a register
962 // to register version.
963 else if (auto *Load = dyn_cast<LoadSDNode>(RISBG.Input)) {
964 if (Load->getMemoryVT() == MVT::i32 &&
965 (Load->getExtensionType() == ISD::EXTLOAD ||
966 Load->getExtensionType() == ISD::ZEXTLOAD) &&
967 RISBG.Mask == 0xffffff00 &&
968 Subtarget->hasLoadAndZeroRightmostByte())
969 PreferAnd = true;
970 }
Ulrich Weigand5dc7b672016-11-11 12:43:51 +0000971 if (PreferAnd) {
972 // Replace the current node with an AND. Note that the current node
973 // might already be that same AND, in which case it is already CSE'd
974 // with it, and we must not call ReplaceNode.
975 SDValue In = convertTo(DL, VT, RISBG.Input);
976 SDValue Mask = CurDAG->getConstant(RISBG.Mask, DL, VT);
977 SDValue New = CurDAG->getNode(ISD::AND, DL, VT, In, Mask);
978 if (N != New.getNode()) {
979 insertDAGNode(CurDAG, N, Mask);
980 insertDAGNode(CurDAG, N, New);
981 ReplaceNode(N, New.getNode());
982 N = New.getNode();
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000983 }
Ulrich Weigand5dc7b672016-11-11 12:43:51 +0000984 // Now, select the machine opcode to implement this operation.
985 SelectCode(N);
986 return true;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000987 }
Simon Pilgrim0750c842015-08-15 13:27:30 +0000988 }
989
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000990 unsigned Opcode = SystemZ::RISBG;
Ulrich Weigand371d10a2015-03-31 12:58:17 +0000991 // Prefer RISBGN if available, since it does not clobber CC.
992 if (Subtarget->hasMiscellaneousExtensions())
993 Opcode = SystemZ::RISBGN;
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000994 EVT OpcodeVT = MVT::i64;
Eric Christophera6734172015-01-31 00:06:45 +0000995 if (VT == MVT::i32 && Subtarget->hasHighWord()) {
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000996 Opcode = SystemZ::RISBMux;
997 OpcodeVT = MVT::i32;
998 RISBG.Start &= 31;
999 RISBG.End &= 31;
1000 }
Richard Sandiford84f54a32013-07-11 08:59:12 +00001001 SDValue Ops[5] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001002 getUNDEF(DL, OpcodeVT),
1003 convertTo(DL, OpcodeVT, RISBG.Input),
1004 CurDAG->getTargetConstant(RISBG.Start, DL, MVT::i32),
1005 CurDAG->getTargetConstant(RISBG.End | 128, DL, MVT::i32),
1006 CurDAG->getTargetConstant(RISBG.Rotate, DL, MVT::i32)
Richard Sandiford84f54a32013-07-11 08:59:12 +00001007 };
Justin Bognerbbcd2232016-05-10 21:11:26 +00001008 SDValue New = convertTo(
1009 DL, VT, SDValue(CurDAG->getMachineNode(Opcode, DL, OpcodeVT, Ops), 0));
1010 ReplaceUses(N, New.getNode());
1011 CurDAG->RemoveDeadNode(N);
1012 return true;
Richard Sandiford84f54a32013-07-11 08:59:12 +00001013}
1014
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001015bool SystemZDAGToDAGISel::tryRxSBG(SDNode *N, unsigned Opcode) {
Ulrich Weigand77884bc2015-06-25 11:52:36 +00001016 SDLoc DL(N);
1017 EVT VT = N->getValueType(0);
1018 if (!VT.isInteger() || VT.getSizeInBits() > 64)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001019 return false;
Richard Sandiford7878b852013-07-18 10:06:15 +00001020 // Try treating each operand of N as the second operand of the RxSBG
Richard Sandiford885140c2013-07-16 11:55:57 +00001021 // and see which goes deepest.
Richard Sandiford51093212013-07-18 10:40:35 +00001022 RxSBGOperands RxSBG[] = {
1023 RxSBGOperands(Opcode, N->getOperand(0)),
1024 RxSBGOperands(Opcode, N->getOperand(1))
1025 };
Richard Sandiford885140c2013-07-16 11:55:57 +00001026 unsigned Count[] = { 0, 0 };
1027 for (unsigned I = 0; I < 2; ++I)
Richard Sandiford5cbac962013-07-18 09:45:08 +00001028 while (expandRxSBG(RxSBG[I]))
Zhan Jun Liau0df35052016-06-22 16:16:27 +00001029 // The widening or narrowing is expected to be free.
1030 // Counting widening or narrowing as a saved operation will result in
1031 // preferring an R*SBG over a simple shift/logical instruction.
1032 if (RxSBG[I].Input.getOpcode() != ISD::ANY_EXTEND &&
1033 RxSBG[I].Input.getOpcode() != ISD::TRUNCATE)
Richard Sandiford3e382972013-10-16 13:35:13 +00001034 Count[I] += 1;
Richard Sandiford885140c2013-07-16 11:55:57 +00001035
1036 // Do nothing if neither operand is suitable.
1037 if (Count[0] == 0 && Count[1] == 0)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001038 return false;
Richard Sandiford885140c2013-07-16 11:55:57 +00001039
1040 // Pick the deepest second operand.
1041 unsigned I = Count[0] > Count[1] ? 0 : 1;
1042 SDValue Op0 = N->getOperand(I ^ 1);
1043
1044 // Prefer IC for character insertions from memory.
Richard Sandiford7878b852013-07-18 10:06:15 +00001045 if (Opcode == SystemZ::ROSBG && (RxSBG[I].Mask & 0xff) == 0)
Richard Sandiford21f5d682014-03-06 11:22:58 +00001046 if (auto *Load = dyn_cast<LoadSDNode>(Op0.getNode()))
Richard Sandiford885140c2013-07-16 11:55:57 +00001047 if (Load->getMemoryVT() == MVT::i8)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001048 return false;
Richard Sandiford885140c2013-07-16 11:55:57 +00001049
1050 // See whether we can avoid an AND in the first operand by converting
1051 // ROSBG to RISBG.
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001052 if (Opcode == SystemZ::ROSBG && detectOrAndInsertion(Op0, RxSBG[I].Mask)) {
Richard Sandiford885140c2013-07-16 11:55:57 +00001053 Opcode = SystemZ::RISBG;
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001054 // Prefer RISBGN if available, since it does not clobber CC.
1055 if (Subtarget->hasMiscellaneousExtensions())
1056 Opcode = SystemZ::RISBGN;
1057 }
1058
Richard Sandiford885140c2013-07-16 11:55:57 +00001059 SDValue Ops[5] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001060 convertTo(DL, MVT::i64, Op0),
1061 convertTo(DL, MVT::i64, RxSBG[I].Input),
1062 CurDAG->getTargetConstant(RxSBG[I].Start, DL, MVT::i32),
1063 CurDAG->getTargetConstant(RxSBG[I].End, DL, MVT::i32),
1064 CurDAG->getTargetConstant(RxSBG[I].Rotate, DL, MVT::i32)
Richard Sandiford885140c2013-07-16 11:55:57 +00001065 };
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001066 SDValue New = convertTo(
1067 DL, VT, SDValue(CurDAG->getMachineNode(Opcode, DL, MVT::i64, Ops), 0));
1068 ReplaceNode(N, New.getNode());
1069 return true;
Richard Sandiford885140c2013-07-16 11:55:57 +00001070}
1071
Justin Bognerffb273d2016-05-09 23:54:23 +00001072void SystemZDAGToDAGISel::splitLargeImmediate(unsigned Opcode, SDNode *Node,
1073 SDValue Op0, uint64_t UpperVal,
1074 uint64_t LowerVal) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001075 EVT VT = Node->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001076 SDLoc DL(Node);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001077 SDValue Upper = CurDAG->getConstant(UpperVal, DL, VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001078 if (Op0.getNode())
1079 Upper = CurDAG->getNode(Opcode, DL, VT, Op0, Upper);
Justin Bognerffb273d2016-05-09 23:54:23 +00001080
1081 {
1082 // When we haven't passed in Op0, Upper will be a constant. In order to
1083 // prevent folding back to the large immediate in `Or = getNode(...)` we run
1084 // SelectCode first and end up with an opaque machine node. This means that
1085 // we need to use a handle to keep track of Upper in case it gets CSE'd by
1086 // SelectCode.
1087 //
1088 // Note that in the case where Op0 is passed in we could just call
1089 // SelectCode(Upper) later, along with the SelectCode(Or), and avoid needing
1090 // the handle at all, but it's fine to do it here.
1091 //
1092 // TODO: This is a pretty hacky way to do this. Can we do something that
1093 // doesn't require a two paragraph explanation?
1094 HandleSDNode Handle(Upper);
1095 SelectCode(Upper.getNode());
1096 Upper = Handle.getValue();
1097 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001098
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001099 SDValue Lower = CurDAG->getConstant(LowerVal, DL, VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001100 SDValue Or = CurDAG->getNode(Opcode, DL, VT, Upper, Lower);
Justin Bognerffb273d2016-05-09 23:54:23 +00001101
1102 ReplaceUses(Node, Or.getNode());
1103 CurDAG->RemoveDeadNode(Node);
1104
1105 SelectCode(Or.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001106}
1107
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001108bool SystemZDAGToDAGISel::tryGather(SDNode *N, unsigned Opcode) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001109 SDValue ElemV = N->getOperand(2);
1110 auto *ElemN = dyn_cast<ConstantSDNode>(ElemV);
1111 if (!ElemN)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001112 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001113
1114 unsigned Elem = ElemN->getZExtValue();
1115 EVT VT = N->getValueType(0);
1116 if (Elem >= VT.getVectorNumElements())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001117 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001118
1119 auto *Load = dyn_cast<LoadSDNode>(N->getOperand(1));
1120 if (!Load || !Load->hasOneUse())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001121 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001122 if (Load->getMemoryVT().getSizeInBits() !=
1123 Load->getValueType(0).getSizeInBits())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001124 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001125
1126 SDValue Base, Disp, Index;
1127 if (!selectBDVAddr12Only(Load->getBasePtr(), ElemV, Base, Disp, Index) ||
1128 Index.getValueType() != VT.changeVectorElementTypeToInteger())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001129 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001130
1131 SDLoc DL(Load);
1132 SDValue Ops[] = {
1133 N->getOperand(0), Base, Disp, Index,
1134 CurDAG->getTargetConstant(Elem, DL, MVT::i32), Load->getChain()
1135 };
1136 SDNode *Res = CurDAG->getMachineNode(Opcode, DL, VT, MVT::Other, Ops);
1137 ReplaceUses(SDValue(Load, 1), SDValue(Res, 1));
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001138 ReplaceNode(N, Res);
1139 return true;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001140}
1141
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001142bool SystemZDAGToDAGISel::tryScatter(StoreSDNode *Store, unsigned Opcode) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001143 SDValue Value = Store->getValue();
1144 if (Value.getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001145 return false;
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00001146 if (Store->getMemoryVT().getSizeInBits() != Value.getValueSizeInBits())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001147 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001148
1149 SDValue ElemV = Value.getOperand(1);
1150 auto *ElemN = dyn_cast<ConstantSDNode>(ElemV);
1151 if (!ElemN)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001152 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001153
1154 SDValue Vec = Value.getOperand(0);
1155 EVT VT = Vec.getValueType();
1156 unsigned Elem = ElemN->getZExtValue();
1157 if (Elem >= VT.getVectorNumElements())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001158 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001159
1160 SDValue Base, Disp, Index;
1161 if (!selectBDVAddr12Only(Store->getBasePtr(), ElemV, Base, Disp, Index) ||
1162 Index.getValueType() != VT.changeVectorElementTypeToInteger())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001163 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001164
1165 SDLoc DL(Store);
1166 SDValue Ops[] = {
1167 Vec, Base, Disp, Index, CurDAG->getTargetConstant(Elem, DL, MVT::i32),
1168 Store->getChain()
1169 };
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001170 ReplaceNode(Store, CurDAG->getMachineNode(Opcode, DL, MVT::Other, Ops));
1171 return true;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001172}
1173
Richard Sandiford067817e2013-09-27 15:29:20 +00001174bool SystemZDAGToDAGISel::canUseBlockOperation(StoreSDNode *Store,
1175 LoadSDNode *Load) const {
Richard Sandiford178273a2013-09-05 10:36:45 +00001176 // Check that the two memory operands have the same size.
1177 if (Load->getMemoryVT() != Store->getMemoryVT())
Richard Sandiford97846492013-07-09 09:46:39 +00001178 return false;
1179
Richard Sandiford178273a2013-09-05 10:36:45 +00001180 // Volatility stops an access from being decomposed.
1181 if (Load->isVolatile() || Store->isVolatile())
1182 return false;
Richard Sandiford97846492013-07-09 09:46:39 +00001183
1184 // There's no chance of overlap if the load is invariant.
Justin Lebaradbf09e2016-09-11 01:38:58 +00001185 if (Load->isInvariant() && Load->isDereferenceable())
Richard Sandiford97846492013-07-09 09:46:39 +00001186 return true;
1187
Richard Sandiford97846492013-07-09 09:46:39 +00001188 // Otherwise we need to check whether there's an alias.
Nick Lewyckyaad475b2014-04-15 07:22:52 +00001189 const Value *V1 = Load->getMemOperand()->getValue();
1190 const Value *V2 = Store->getMemOperand()->getValue();
Richard Sandiford97846492013-07-09 09:46:39 +00001191 if (!V1 || !V2)
1192 return false;
1193
Richard Sandiford067817e2013-09-27 15:29:20 +00001194 // Reject equality.
1195 uint64_t Size = Load->getMemoryVT().getStoreSize();
Richard Sandiford97846492013-07-09 09:46:39 +00001196 int64_t End1 = Load->getSrcValueOffset() + Size;
1197 int64_t End2 = Store->getSrcValueOffset() + Size;
Richard Sandiford067817e2013-09-27 15:29:20 +00001198 if (V1 == V2 && End1 == End2)
1199 return false;
1200
Chandler Carruthac80dc72015-06-17 07:18:54 +00001201 return !AA->alias(MemoryLocation(V1, End1, Load->getAAInfo()),
1202 MemoryLocation(V2, End2, Store->getAAInfo()));
Richard Sandiford97846492013-07-09 09:46:39 +00001203}
1204
Richard Sandiford178273a2013-09-05 10:36:45 +00001205bool SystemZDAGToDAGISel::storeLoadCanUseMVC(SDNode *N) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001206 auto *Store = cast<StoreSDNode>(N);
1207 auto *Load = cast<LoadSDNode>(Store->getValue());
Richard Sandiford178273a2013-09-05 10:36:45 +00001208
1209 // Prefer not to use MVC if either address can use ... RELATIVE LONG
1210 // instructions.
1211 uint64_t Size = Load->getMemoryVT().getStoreSize();
1212 if (Size > 1 && Size <= 8) {
1213 // Prefer LHRL, LRL and LGRL.
Richard Sandiford54b36912013-09-27 15:14:04 +00001214 if (SystemZISD::isPCREL(Load->getBasePtr().getOpcode()))
Richard Sandiford178273a2013-09-05 10:36:45 +00001215 return false;
1216 // Prefer STHRL, STRL and STGRL.
Richard Sandiford54b36912013-09-27 15:14:04 +00001217 if (SystemZISD::isPCREL(Store->getBasePtr().getOpcode()))
Richard Sandiford178273a2013-09-05 10:36:45 +00001218 return false;
1219 }
1220
Richard Sandiford067817e2013-09-27 15:29:20 +00001221 return canUseBlockOperation(Store, Load);
Richard Sandiford178273a2013-09-05 10:36:45 +00001222}
1223
1224bool SystemZDAGToDAGISel::storeLoadCanUseBlockBinary(SDNode *N,
1225 unsigned I) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001226 auto *StoreA = cast<StoreSDNode>(N);
1227 auto *LoadA = cast<LoadSDNode>(StoreA->getValue().getOperand(1 - I));
1228 auto *LoadB = cast<LoadSDNode>(StoreA->getValue().getOperand(I));
Richard Sandiford067817e2013-09-27 15:29:20 +00001229 return !LoadA->isVolatile() && canUseBlockOperation(StoreA, LoadB);
Richard Sandiford178273a2013-09-05 10:36:45 +00001230}
1231
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001232void SystemZDAGToDAGISel::Select(SDNode *Node) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001233 // Dump information about the Node being selected
1234 DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n");
1235
1236 // If we have a custom node, we already have selected!
1237 if (Node->isMachineOpcode()) {
1238 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
Tim Northover31d093c2013-09-22 08:21:56 +00001239 Node->setNodeId(-1);
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001240 return;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001241 }
1242
1243 unsigned Opcode = Node->getOpcode();
1244 switch (Opcode) {
1245 case ISD::OR:
Richard Sandiford885140c2013-07-16 11:55:57 +00001246 if (Node->getOperand(1).getOpcode() != ISD::Constant)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001247 if (tryRxSBG(Node, SystemZ::ROSBG))
1248 return;
Richard Sandiford7878b852013-07-18 10:06:15 +00001249 goto or_xor;
1250
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001251 case ISD::XOR:
Richard Sandiford7878b852013-07-18 10:06:15 +00001252 if (Node->getOperand(1).getOpcode() != ISD::Constant)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001253 if (tryRxSBG(Node, SystemZ::RXSBG))
1254 return;
Richard Sandiford7878b852013-07-18 10:06:15 +00001255 // Fall through.
1256 or_xor:
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001257 // If this is a 64-bit operation in which both 32-bit halves are nonzero,
1258 // split the operation into two.
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001259 if (Node->getValueType(0) == MVT::i64)
Richard Sandiford21f5d682014-03-06 11:22:58 +00001260 if (auto *Op1 = dyn_cast<ConstantSDNode>(Node->getOperand(1))) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001261 uint64_t Val = Op1->getZExtValue();
Justin Bognerffb273d2016-05-09 23:54:23 +00001262 if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val)) {
1263 splitLargeImmediate(Opcode, Node, Node->getOperand(0),
1264 Val - uint32_t(Val), uint32_t(Val));
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001265 return;
Justin Bognerffb273d2016-05-09 23:54:23 +00001266 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001267 }
1268 break;
1269
Richard Sandiford84f54a32013-07-11 08:59:12 +00001270 case ISD::AND:
Richard Sandiford51093212013-07-18 10:40:35 +00001271 if (Node->getOperand(1).getOpcode() != ISD::Constant)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001272 if (tryRxSBG(Node, SystemZ::RNSBG))
1273 return;
Justin Bognercd1d5aa2016-08-17 20:30:52 +00001274 LLVM_FALLTHROUGH;
Richard Sandiford82ec87d2013-07-16 11:02:24 +00001275 case ISD::ROTL:
1276 case ISD::SHL:
1277 case ISD::SRL:
Richard Sandiford220ee492013-12-20 11:49:48 +00001278 case ISD::ZERO_EXTEND:
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001279 if (tryRISBGZero(Node))
1280 return;
Richard Sandiford84f54a32013-07-11 08:59:12 +00001281 break;
1282
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001283 case ISD::Constant:
1284 // If this is a 64-bit constant that is out of the range of LLILF,
1285 // LLIHF and LGFI, split it into two 32-bit pieces.
1286 if (Node->getValueType(0) == MVT::i64) {
1287 uint64_t Val = cast<ConstantSDNode>(Node)->getZExtValue();
Justin Bognerffb273d2016-05-09 23:54:23 +00001288 if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val) && !isInt<32>(Val)) {
1289 splitLargeImmediate(ISD::OR, Node, SDValue(), Val - uint32_t(Val),
1290 uint32_t(Val));
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001291 return;
Justin Bognerffb273d2016-05-09 23:54:23 +00001292 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001293 }
1294 break;
1295
Richard Sandifordee834382013-07-31 12:38:08 +00001296 case SystemZISD::SELECT_CCMASK: {
1297 SDValue Op0 = Node->getOperand(0);
1298 SDValue Op1 = Node->getOperand(1);
1299 // Prefer to put any load first, so that it can be matched as a
Ulrich Weigand524f2762016-11-28 13:34:08 +00001300 // conditional load. Likewise for constants in range for LOCHI.
1301 if ((Op1.getOpcode() == ISD::LOAD && Op0.getOpcode() != ISD::LOAD) ||
1302 (Subtarget->hasLoadStoreOnCond2() &&
1303 Node->getValueType(0).isInteger() &&
1304 Op1.getOpcode() == ISD::Constant &&
1305 isInt<16>(cast<ConstantSDNode>(Op1)->getSExtValue()) &&
1306 !(Op0.getOpcode() == ISD::Constant &&
1307 isInt<16>(cast<ConstantSDNode>(Op0)->getSExtValue())))) {
Richard Sandifordee834382013-07-31 12:38:08 +00001308 SDValue CCValid = Node->getOperand(2);
1309 SDValue CCMask = Node->getOperand(3);
1310 uint64_t ConstCCValid =
1311 cast<ConstantSDNode>(CCValid.getNode())->getZExtValue();
1312 uint64_t ConstCCMask =
1313 cast<ConstantSDNode>(CCMask.getNode())->getZExtValue();
1314 // Invert the condition.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001315 CCMask = CurDAG->getConstant(ConstCCValid ^ ConstCCMask, SDLoc(Node),
Richard Sandifordee834382013-07-31 12:38:08 +00001316 CCMask.getValueType());
1317 SDValue Op4 = Node->getOperand(4);
1318 Node = CurDAG->UpdateNodeOperands(Node, Op1, Op0, CCValid, CCMask, Op4);
1319 }
1320 break;
1321 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001322
1323 case ISD::INSERT_VECTOR_ELT: {
1324 EVT VT = Node->getValueType(0);
Sanjay Patel1ed771f2016-09-14 16:37:15 +00001325 unsigned ElemBitSize = VT.getScalarSizeInBits();
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001326 if (ElemBitSize == 32) {
1327 if (tryGather(Node, SystemZ::VGEF))
1328 return;
1329 } else if (ElemBitSize == 64) {
1330 if (tryGather(Node, SystemZ::VGEG))
1331 return;
1332 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001333 break;
1334 }
1335
1336 case ISD::STORE: {
1337 auto *Store = cast<StoreSDNode>(Node);
Sanjay Patelb1f0a0f2016-09-14 16:05:51 +00001338 unsigned ElemBitSize = Store->getValue().getValueSizeInBits();
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001339 if (ElemBitSize == 32) {
1340 if (tryScatter(Store, SystemZ::VSCEF))
1341 return;
1342 } else if (ElemBitSize == 64) {
1343 if (tryScatter(Store, SystemZ::VSCEG))
1344 return;
1345 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001346 break;
1347 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001348 }
1349
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001350 SelectCode(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001351}
1352
1353bool SystemZDAGToDAGISel::
1354SelectInlineAsmMemoryOperand(const SDValue &Op,
Daniel Sanders60f1db02015-03-13 12:45:09 +00001355 unsigned ConstraintID,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001356 std::vector<SDValue> &OutOps) {
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001357 SystemZAddressingMode::AddrForm Form;
1358 SystemZAddressingMode::DispRange DispRange;
Ulrich Weigand79564612016-06-09 15:19:16 +00001359 SDValue Base, Disp, Index;
1360
Daniel Sanders2eeace22015-03-17 16:16:14 +00001361 switch(ConstraintID) {
1362 default:
1363 llvm_unreachable("Unexpected asm memory constraint");
1364 case InlineAsm::Constraint_i:
Daniel Sanders2eeace22015-03-17 16:16:14 +00001365 case InlineAsm::Constraint_Q:
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001366 // Accept an address with a short displacement, but no index.
1367 Form = SystemZAddressingMode::FormBD;
1368 DispRange = SystemZAddressingMode::Disp12Only;
1369 break;
Daniel Sanders2eeace22015-03-17 16:16:14 +00001370 case InlineAsm::Constraint_R:
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001371 // Accept an address with a short displacement and an index.
1372 Form = SystemZAddressingMode::FormBDXNormal;
1373 DispRange = SystemZAddressingMode::Disp12Only;
Daniel Sanders2eeace22015-03-17 16:16:14 +00001374 break;
Ulrich Weigand79564612016-06-09 15:19:16 +00001375 case InlineAsm::Constraint_S:
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001376 // Accept an address with a long displacement, but no index.
1377 Form = SystemZAddressingMode::FormBD;
1378 DispRange = SystemZAddressingMode::Disp20Only;
1379 break;
Ulrich Weigand79564612016-06-09 15:19:16 +00001380 case InlineAsm::Constraint_T:
1381 case InlineAsm::Constraint_m:
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001382 // Accept an address with a long displacement and an index.
1383 // m works the same as T, as this is the most general case.
1384 Form = SystemZAddressingMode::FormBDXNormal;
1385 DispRange = SystemZAddressingMode::Disp20Only;
Ulrich Weigand79564612016-06-09 15:19:16 +00001386 break;
Daniel Sanders2eeace22015-03-17 16:16:14 +00001387 }
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001388
1389 if (selectBDXAddr(Form, DispRange, Op, Base, Disp, Index)) {
Zhan Jun Liaucf2f4b32016-08-18 21:44:15 +00001390 const TargetRegisterClass *TRC =
1391 Subtarget->getRegisterInfo()->getPointerRegClass(*MF);
1392 SDLoc DL(Base);
1393 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), DL, MVT::i32);
1394
1395 // Make sure that the base address doesn't go into %r0.
1396 // If it's a TargetFrameIndex or a fixed register, we shouldn't do anything.
1397 if (Base.getOpcode() != ISD::TargetFrameIndex &&
1398 Base.getOpcode() != ISD::Register) {
1399 Base =
1400 SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
1401 DL, Base.getValueType(),
1402 Base, RC), 0);
1403 }
1404
1405 // Make sure that the index register isn't assigned to %r0 either.
1406 if (Index.getOpcode() != ISD::Register) {
1407 Index =
1408 SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
1409 DL, Index.getValueType(),
1410 Index, RC), 0);
1411 }
1412
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001413 OutOps.push_back(Base);
1414 OutOps.push_back(Disp);
1415 OutOps.push_back(Index);
1416 return false;
1417 }
1418
Daniel Sanders2eeace22015-03-17 16:16:14 +00001419 return true;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001420}