blob: 726bc28b6db299d3e202c8e69d18b8b182a7a169 [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"
18#include "llvm/Support/raw_ostream.h"
19
20using namespace llvm;
21
Chandler Carruthe96dd892014-04-21 22:55:11 +000022#define DEBUG_TYPE "systemz-isel"
23
Ulrich Weigand5f613df2013-05-06 16:15:19 +000024namespace {
25// Used to build addressing modes.
26struct SystemZAddressingMode {
27 // The shape of the address.
28 enum AddrForm {
29 // base+displacement
30 FormBD,
31
32 // base+displacement+index for load and store operands
33 FormBDXNormal,
34
35 // base+displacement+index for load address operands
36 FormBDXLA,
37
38 // base+displacement+index+ADJDYNALLOC
39 FormBDXDynAlloc
40 };
41 AddrForm Form;
42
43 // The type of displacement. The enum names here correspond directly
44 // to the definitions in SystemZOperand.td. We could split them into
45 // flags -- single/pair, 128-bit, etc. -- but it hardly seems worth it.
46 enum DispRange {
47 Disp12Only,
48 Disp12Pair,
49 Disp20Only,
50 Disp20Only128,
51 Disp20Pair
52 };
53 DispRange DR;
54
55 // The parts of the address. The address is equivalent to:
56 //
57 // Base + Disp + Index + (IncludesDynAlloc ? ADJDYNALLOC : 0)
58 SDValue Base;
59 int64_t Disp;
60 SDValue Index;
61 bool IncludesDynAlloc;
62
63 SystemZAddressingMode(AddrForm form, DispRange dr)
64 : Form(form), DR(dr), Base(), Disp(0), Index(),
65 IncludesDynAlloc(false) {}
66
67 // True if the address can have an index register.
68 bool hasIndexField() { return Form != FormBD; }
69
70 // True if the address can (and must) include ADJDYNALLOC.
71 bool isDynAlloc() { return Form == FormBDXDynAlloc; }
72
73 void dump() {
74 errs() << "SystemZAddressingMode " << this << '\n';
75
76 errs() << " Base ";
Craig Topper062a2ba2014-04-25 05:30:21 +000077 if (Base.getNode())
Ulrich Weigand5f613df2013-05-06 16:15:19 +000078 Base.getNode()->dump();
79 else
80 errs() << "null\n";
81
82 if (hasIndexField()) {
83 errs() << " Index ";
Craig Topper062a2ba2014-04-25 05:30:21 +000084 if (Index.getNode())
Ulrich Weigand5f613df2013-05-06 16:15:19 +000085 Index.getNode()->dump();
86 else
87 errs() << "null\n";
88 }
89
90 errs() << " Disp " << Disp;
91 if (IncludesDynAlloc)
92 errs() << " + ADJDYNALLOC";
93 errs() << '\n';
94 }
95};
96
Richard Sandiford82ec87d2013-07-16 11:02:24 +000097// Return a mask with Count low bits set.
98static uint64_t allOnes(unsigned int Count) {
Ulrich Weigand77884bc2015-06-25 11:52:36 +000099 assert(Count <= 64);
Justin Bognerc97c48a2015-06-24 05:59:19 +0000100 if (Count > 63)
101 return UINT64_MAX;
102 return (uint64_t(1) << Count) - 1;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000103}
104
Richard Sandiford51093212013-07-18 10:40:35 +0000105// 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 Sandiford3e382972013-10-16 13:35:13 +0000115// otherwise. The output value has BitSize bits, although Input may be
116// narrower (in which case the upper bits are don't care).
Richard Sandiford5cbac962013-07-18 09:45:08 +0000117struct RxSBGOperands {
Richard Sandiford51093212013-07-18 10:40:35 +0000118 RxSBGOperands(unsigned Op, SDValue N)
119 : Opcode(Op), BitSize(N.getValueType().getSizeInBits()),
120 Mask(allOnes(BitSize)), Input(N), Start(64 - BitSize), End(63),
121 Rotate(0) {}
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000122
Richard Sandiford51093212013-07-18 10:40:35 +0000123 unsigned Opcode;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000124 unsigned BitSize;
125 uint64_t Mask;
126 SDValue Input;
127 unsigned Start;
128 unsigned End;
129 unsigned Rotate;
130};
131
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000132class SystemZDAGToDAGISel : public SelectionDAGISel {
Eric Christophera6734172015-01-31 00:06:45 +0000133 const SystemZSubtarget *Subtarget;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000134
135 // Used by SystemZOperands.td to create integer constants.
Richard Sandiford54b36912013-09-27 15:14:04 +0000136 inline SDValue getImm(const SDNode *Node, uint64_t Imm) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000137 return CurDAG->getTargetConstant(Imm, SDLoc(Node), Node->getValueType(0));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000138 }
139
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000140 const SystemZTargetMachine &getTargetMachine() const {
141 return static_cast<const SystemZTargetMachine &>(TM);
142 }
143
144 const SystemZInstrInfo *getInstrInfo() const {
Eric Christophera6734172015-01-31 00:06:45 +0000145 return Subtarget->getInstrInfo();
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000146 }
147
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000148 // Try to fold more of the base or index of AM into AM, where IsBase
149 // selects between the base and index.
Richard Sandiford54b36912013-09-27 15:14:04 +0000150 bool expandAddress(SystemZAddressingMode &AM, bool IsBase) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000151
152 // Try to describe N in AM, returning true on success.
Richard Sandiford54b36912013-09-27 15:14:04 +0000153 bool selectAddress(SDValue N, SystemZAddressingMode &AM) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000154
155 // Extract individual target operands from matched address AM.
156 void getAddressOperands(const SystemZAddressingMode &AM, EVT VT,
Richard Sandiford54b36912013-09-27 15:14:04 +0000157 SDValue &Base, SDValue &Disp) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000158 void getAddressOperands(const SystemZAddressingMode &AM, EVT VT,
Richard Sandiford54b36912013-09-27 15:14:04 +0000159 SDValue &Base, SDValue &Disp, SDValue &Index) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000160
161 // Try to match Addr as a FormBD address with displacement type DR.
162 // Return true on success, storing the base and displacement in
163 // Base and Disp respectively.
164 bool selectBDAddr(SystemZAddressingMode::DispRange DR, SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000165 SDValue &Base, SDValue &Disp) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000166
Richard Sandiforda481f582013-08-23 11:18:53 +0000167 // Try to match Addr as a FormBDX address with displacement type DR.
168 // Return true on success and if the result had no index. Store the
169 // base and displacement in Base and Disp respectively.
170 bool selectMVIAddr(SystemZAddressingMode::DispRange DR, SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000171 SDValue &Base, SDValue &Disp) const;
Richard Sandiforda481f582013-08-23 11:18:53 +0000172
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000173 // Try to match Addr as a FormBDX* address of form Form with
174 // displacement type DR. Return true on success, storing the base,
175 // displacement and index in Base, Disp and Index respectively.
176 bool selectBDXAddr(SystemZAddressingMode::AddrForm Form,
177 SystemZAddressingMode::DispRange DR, SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000178 SDValue &Base, SDValue &Disp, SDValue &Index) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000179
180 // PC-relative address matching routines used by SystemZOperands.td.
Richard Sandiford54b36912013-09-27 15:14:04 +0000181 bool selectPCRelAddress(SDValue Addr, SDValue &Target) const {
182 if (SystemZISD::isPCREL(Addr.getOpcode())) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000183 Target = Addr.getOperand(0);
184 return true;
185 }
186 return false;
187 }
188
189 // BD matching routines used by SystemZOperands.td.
Richard Sandiford54b36912013-09-27 15:14:04 +0000190 bool selectBDAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000191 return selectBDAddr(SystemZAddressingMode::Disp12Only, Addr, Base, Disp);
192 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000193 bool selectBDAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000194 return selectBDAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp);
195 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000196 bool selectBDAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000197 return selectBDAddr(SystemZAddressingMode::Disp20Only, Addr, Base, Disp);
198 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000199 bool selectBDAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000200 return selectBDAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp);
201 }
202
Richard Sandiforda481f582013-08-23 11:18:53 +0000203 // MVI matching routines used by SystemZOperands.td.
Richard Sandiford54b36912013-09-27 15:14:04 +0000204 bool selectMVIAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Richard Sandiforda481f582013-08-23 11:18:53 +0000205 return selectMVIAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp);
206 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000207 bool selectMVIAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Richard Sandiforda481f582013-08-23 11:18:53 +0000208 return selectMVIAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp);
209 }
210
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000211 // BDX matching routines used by SystemZOperands.td.
212 bool selectBDXAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000213 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000214 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
215 SystemZAddressingMode::Disp12Only,
216 Addr, Base, Disp, Index);
217 }
218 bool selectBDXAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000219 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000220 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
221 SystemZAddressingMode::Disp12Pair,
222 Addr, Base, Disp, Index);
223 }
224 bool selectDynAlloc12Only(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000225 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000226 return selectBDXAddr(SystemZAddressingMode::FormBDXDynAlloc,
227 SystemZAddressingMode::Disp12Only,
228 Addr, Base, Disp, Index);
229 }
230 bool selectBDXAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000231 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000232 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
233 SystemZAddressingMode::Disp20Only,
234 Addr, Base, Disp, Index);
235 }
236 bool selectBDXAddr20Only128(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000237 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000238 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
239 SystemZAddressingMode::Disp20Only128,
240 Addr, Base, Disp, Index);
241 }
242 bool selectBDXAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000243 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000244 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
245 SystemZAddressingMode::Disp20Pair,
246 Addr, Base, Disp, Index);
247 }
248 bool selectLAAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000249 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000250 return selectBDXAddr(SystemZAddressingMode::FormBDXLA,
251 SystemZAddressingMode::Disp12Pair,
252 Addr, Base, Disp, Index);
253 }
254 bool selectLAAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000255 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000256 return selectBDXAddr(SystemZAddressingMode::FormBDXLA,
257 SystemZAddressingMode::Disp20Pair,
258 Addr, Base, Disp, Index);
259 }
260
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000261 // Try to match Addr as an address with a base, 12-bit displacement
262 // and index, where the index is element Elem of a vector.
263 // Return true on success, storing the base, displacement and vector
264 // in Base, Disp and Index respectively.
265 bool selectBDVAddr12Only(SDValue Addr, SDValue Elem, SDValue &Base,
266 SDValue &Disp, SDValue &Index) const;
267
Richard Sandiford885140c2013-07-16 11:55:57 +0000268 // Check whether (or Op (and X InsertMask)) is effectively an insertion
269 // of X into bits InsertMask of some Y != Op. Return true if so and
270 // set Op to that Y.
Richard Sandiford54b36912013-09-27 15:14:04 +0000271 bool detectOrAndInsertion(SDValue &Op, uint64_t InsertMask) const;
Richard Sandiford885140c2013-07-16 11:55:57 +0000272
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000273 // Try to update RxSBG so that only the bits of RxSBG.Input in Mask are used.
274 // Return true on success.
Richard Sandiford54b36912013-09-27 15:14:04 +0000275 bool refineRxSBGMask(RxSBGOperands &RxSBG, uint64_t Mask) const;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000276
Richard Sandiford5cbac962013-07-18 09:45:08 +0000277 // Try to fold some of RxSBG.Input into other fields of RxSBG.
278 // Return true on success.
Richard Sandiford54b36912013-09-27 15:14:04 +0000279 bool expandRxSBG(RxSBGOperands &RxSBG) const;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000280
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000281 // Return an undefined value of type VT.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000282 SDValue getUNDEF(const SDLoc &DL, EVT VT) const;
Richard Sandiford84f54a32013-07-11 08:59:12 +0000283
284 // Convert N to VT, if it isn't already.
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000285 SDValue convertTo(const SDLoc &DL, EVT VT, SDValue N) const;
Richard Sandiford84f54a32013-07-11 08:59:12 +0000286
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000287 // Try to implement AND or shift node N using RISBG with the zero flag set.
288 // Return the selected node on success, otherwise return null.
Justin Bognerbbcd2232016-05-10 21:11:26 +0000289 bool tryRISBGZero(SDNode *N);
Richard Sandiford84f54a32013-07-11 08:59:12 +0000290
Richard Sandiford7878b852013-07-18 10:06:15 +0000291 // Try to use RISBG or Opcode to implement OR or XOR node N.
292 // Return the selected node on success, otherwise return null.
Justin Bogner9b34e8a2016-05-13 22:42:08 +0000293 bool tryRxSBG(SDNode *N, unsigned Opcode);
Richard Sandiford885140c2013-07-16 11:55:57 +0000294
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000295 // If Op0 is null, then Node is a constant that can be loaded using:
296 //
297 // (Opcode UpperVal LowerVal)
298 //
299 // If Op0 is nonnull, then Node can be implemented using:
300 //
301 // (Opcode (Opcode Op0 UpperVal) LowerVal)
Justin Bognerffb273d2016-05-09 23:54:23 +0000302 void splitLargeImmediate(unsigned Opcode, SDNode *Node, SDValue Op0,
303 uint64_t UpperVal, uint64_t LowerVal);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000304
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000305 // Try to use gather instruction Opcode to implement vector insertion N.
Justin Bogner9b34e8a2016-05-13 22:42:08 +0000306 bool tryGather(SDNode *N, unsigned Opcode);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000307
308 // Try to use scatter instruction Opcode to implement store Store.
Justin Bogner9b34e8a2016-05-13 22:42:08 +0000309 bool tryScatter(StoreSDNode *Store, unsigned Opcode);
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000310
Richard Sandiford067817e2013-09-27 15:29:20 +0000311 // Return true if Load and Store are loads and stores of the same size
312 // and are guaranteed not to overlap. Such operations can be implemented
313 // using block (SS-format) instructions.
314 //
315 // Partial overlap would lead to incorrect code, since the block operations
316 // are logically bytewise, even though they have a fast path for the
317 // non-overlapping case. We also need to avoid full overlap (i.e. two
318 // addresses that might be equal at run time) because although that case
319 // would be handled correctly, it might be implemented by millicode.
320 bool canUseBlockOperation(StoreSDNode *Store, LoadSDNode *Load) const;
321
Richard Sandiford178273a2013-09-05 10:36:45 +0000322 // N is a (store (load Y), X) pattern. Return true if it can use an MVC
323 // from Y to X.
Richard Sandiford97846492013-07-09 09:46:39 +0000324 bool storeLoadCanUseMVC(SDNode *N) const;
325
Richard Sandiford178273a2013-09-05 10:36:45 +0000326 // N is a (store (op (load A[0]), (load A[1])), X) pattern. Return true
327 // if A[1 - I] == X and if N can use a block operation like NC from A[I]
328 // to X.
329 bool storeLoadCanUseBlockBinary(SDNode *N, unsigned I) const;
330
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000331public:
332 SystemZDAGToDAGISel(SystemZTargetMachine &TM, CodeGenOpt::Level OptLevel)
Eric Christophera6734172015-01-31 00:06:45 +0000333 : SelectionDAGISel(TM, OptLevel) {}
334
335 bool runOnMachineFunction(MachineFunction &MF) override {
336 Subtarget = &MF.getSubtarget<SystemZSubtarget>();
337 return SelectionDAGISel::runOnMachineFunction(MF);
338 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000339
340 // Override MachineFunctionPass.
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000341 const char *getPassName() const override {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000342 return "SystemZ DAG->DAG Pattern Instruction Selection";
343 }
344
345 // Override SelectionDAGISel.
Justin Bogner9b34e8a2016-05-13 22:42:08 +0000346 void Select(SDNode *Node) override;
Daniel Sanders60f1db02015-03-13 12:45:09 +0000347 bool SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000348 std::vector<SDValue> &OutOps) override;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000349
350 // Include the pieces autogenerated from the target description.
351 #include "SystemZGenDAGISel.inc"
352};
353} // end anonymous namespace
354
355FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM,
356 CodeGenOpt::Level OptLevel) {
357 return new SystemZDAGToDAGISel(TM, OptLevel);
358}
359
360// Return true if Val should be selected as a displacement for an address
361// with range DR. Here we're interested in the range of both the instruction
362// described by DR and of any pairing instruction.
363static bool selectDisp(SystemZAddressingMode::DispRange DR, int64_t Val) {
364 switch (DR) {
365 case SystemZAddressingMode::Disp12Only:
366 return isUInt<12>(Val);
367
368 case SystemZAddressingMode::Disp12Pair:
369 case SystemZAddressingMode::Disp20Only:
370 case SystemZAddressingMode::Disp20Pair:
371 return isInt<20>(Val);
372
373 case SystemZAddressingMode::Disp20Only128:
374 return isInt<20>(Val) && isInt<20>(Val + 8);
375 }
376 llvm_unreachable("Unhandled displacement range");
377}
378
379// Change the base or index in AM to Value, where IsBase selects
380// between the base and index.
381static void changeComponent(SystemZAddressingMode &AM, bool IsBase,
382 SDValue Value) {
383 if (IsBase)
384 AM.Base = Value;
385 else
386 AM.Index = Value;
387}
388
389// The base or index of AM is equivalent to Value + ADJDYNALLOC,
390// where IsBase selects between the base and index. Try to fold the
391// ADJDYNALLOC into AM.
392static bool expandAdjDynAlloc(SystemZAddressingMode &AM, bool IsBase,
393 SDValue Value) {
394 if (AM.isDynAlloc() && !AM.IncludesDynAlloc) {
395 changeComponent(AM, IsBase, Value);
396 AM.IncludesDynAlloc = true;
397 return true;
398 }
399 return false;
400}
401
402// The base of AM is equivalent to Base + Index. Try to use Index as
403// the index register.
404static bool expandIndex(SystemZAddressingMode &AM, SDValue Base,
405 SDValue Index) {
406 if (AM.hasIndexField() && !AM.Index.getNode()) {
407 AM.Base = Base;
408 AM.Index = Index;
409 return true;
410 }
411 return false;
412}
413
414// The base or index of AM is equivalent to Op0 + Op1, where IsBase selects
415// between the base and index. Try to fold Op1 into AM's displacement.
416static bool expandDisp(SystemZAddressingMode &AM, bool IsBase,
Richard Sandiford54b36912013-09-27 15:14:04 +0000417 SDValue Op0, uint64_t Op1) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000418 // First try adjusting the displacement.
Richard Sandiford54b36912013-09-27 15:14:04 +0000419 int64_t TestDisp = AM.Disp + Op1;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000420 if (selectDisp(AM.DR, TestDisp)) {
421 changeComponent(AM, IsBase, Op0);
422 AM.Disp = TestDisp;
423 return true;
424 }
425
426 // We could consider forcing the displacement into a register and
427 // using it as an index, but it would need to be carefully tuned.
428 return false;
429}
430
431bool SystemZDAGToDAGISel::expandAddress(SystemZAddressingMode &AM,
Richard Sandiford54b36912013-09-27 15:14:04 +0000432 bool IsBase) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000433 SDValue N = IsBase ? AM.Base : AM.Index;
434 unsigned Opcode = N.getOpcode();
435 if (Opcode == ISD::TRUNCATE) {
436 N = N.getOperand(0);
437 Opcode = N.getOpcode();
438 }
439 if (Opcode == ISD::ADD || CurDAG->isBaseWithConstantOffset(N)) {
440 SDValue Op0 = N.getOperand(0);
441 SDValue Op1 = N.getOperand(1);
442
443 unsigned Op0Code = Op0->getOpcode();
444 unsigned Op1Code = Op1->getOpcode();
445
446 if (Op0Code == SystemZISD::ADJDYNALLOC)
447 return expandAdjDynAlloc(AM, IsBase, Op1);
448 if (Op1Code == SystemZISD::ADJDYNALLOC)
449 return expandAdjDynAlloc(AM, IsBase, Op0);
450
451 if (Op0Code == ISD::Constant)
Richard Sandiford54b36912013-09-27 15:14:04 +0000452 return expandDisp(AM, IsBase, Op1,
453 cast<ConstantSDNode>(Op0)->getSExtValue());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000454 if (Op1Code == ISD::Constant)
Richard Sandiford54b36912013-09-27 15:14:04 +0000455 return expandDisp(AM, IsBase, Op0,
456 cast<ConstantSDNode>(Op1)->getSExtValue());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000457
458 if (IsBase && expandIndex(AM, Op0, Op1))
459 return true;
460 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000461 if (Opcode == SystemZISD::PCREL_OFFSET) {
462 SDValue Full = N.getOperand(0);
463 SDValue Base = N.getOperand(1);
464 SDValue Anchor = Base.getOperand(0);
465 uint64_t Offset = (cast<GlobalAddressSDNode>(Full)->getOffset() -
466 cast<GlobalAddressSDNode>(Anchor)->getOffset());
467 return expandDisp(AM, IsBase, Base, Offset);
468 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000469 return false;
470}
471
472// Return true if an instruction with displacement range DR should be
473// used for displacement value Val. selectDisp(DR, Val) must already hold.
474static bool isValidDisp(SystemZAddressingMode::DispRange DR, int64_t Val) {
475 assert(selectDisp(DR, Val) && "Invalid displacement");
476 switch (DR) {
477 case SystemZAddressingMode::Disp12Only:
478 case SystemZAddressingMode::Disp20Only:
479 case SystemZAddressingMode::Disp20Only128:
480 return true;
481
482 case SystemZAddressingMode::Disp12Pair:
483 // Use the other instruction if the displacement is too large.
484 return isUInt<12>(Val);
485
486 case SystemZAddressingMode::Disp20Pair:
487 // Use the other instruction if the displacement is small enough.
488 return !isUInt<12>(Val);
489 }
490 llvm_unreachable("Unhandled displacement range");
491}
492
493// Return true if Base + Disp + Index should be performed by LA(Y).
494static bool shouldUseLA(SDNode *Base, int64_t Disp, SDNode *Index) {
495 // Don't use LA(Y) for constants.
496 if (!Base)
497 return false;
498
499 // Always use LA(Y) for frame addresses, since we know that the destination
500 // register is almost always (perhaps always) going to be different from
501 // the frame register.
502 if (Base->getOpcode() == ISD::FrameIndex)
503 return true;
504
505 if (Disp) {
506 // Always use LA(Y) if there is a base, displacement and index.
507 if (Index)
508 return true;
509
510 // Always use LA if the displacement is small enough. It should always
511 // be no worse than AGHI (and better if it avoids a move).
512 if (isUInt<12>(Disp))
513 return true;
514
515 // For similar reasons, always use LAY if the constant is too big for AGHI.
516 // LAY should be no worse than AGFI.
517 if (!isInt<16>(Disp))
518 return true;
519 } else {
520 // Don't use LA for plain registers.
521 if (!Index)
522 return false;
523
524 // Don't use LA for plain addition if the index operand is only used
525 // once. It should be a natural two-operand addition in that case.
526 if (Index->hasOneUse())
527 return false;
528
529 // Prefer addition if the second operation is sign-extended, in the
530 // hope of using AGF.
531 unsigned IndexOpcode = Index->getOpcode();
532 if (IndexOpcode == ISD::SIGN_EXTEND ||
533 IndexOpcode == ISD::SIGN_EXTEND_INREG)
534 return false;
535 }
536
537 // Don't use LA for two-operand addition if either operand is only
538 // used once. The addition instructions are better in that case.
539 if (Base->hasOneUse())
540 return false;
541
542 return true;
543}
544
545// Return true if Addr is suitable for AM, updating AM if so.
546bool SystemZDAGToDAGISel::selectAddress(SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000547 SystemZAddressingMode &AM) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000548 // Start out assuming that the address will need to be loaded separately,
549 // then try to extend it as much as we can.
550 AM.Base = Addr;
551
552 // First try treating the address as a constant.
553 if (Addr.getOpcode() == ISD::Constant &&
Richard Sandiford54b36912013-09-27 15:14:04 +0000554 expandDisp(AM, true, SDValue(),
555 cast<ConstantSDNode>(Addr)->getSExtValue()))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000556 ;
Marcin Koscielnicki9de88d92016-05-04 23:31:26 +0000557 // Also see if it's a bare ADJDYNALLOC.
558 else if (Addr.getOpcode() == SystemZISD::ADJDYNALLOC &&
559 expandAdjDynAlloc(AM, true, SDValue()))
560 ;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000561 else
562 // Otherwise try expanding each component.
563 while (expandAddress(AM, true) ||
564 (AM.Index.getNode() && expandAddress(AM, false)))
565 continue;
566
567 // Reject cases where it isn't profitable to use LA(Y).
568 if (AM.Form == SystemZAddressingMode::FormBDXLA &&
569 !shouldUseLA(AM.Base.getNode(), AM.Disp, AM.Index.getNode()))
570 return false;
571
572 // Reject cases where the other instruction in a pair should be used.
573 if (!isValidDisp(AM.DR, AM.Disp))
574 return false;
575
576 // Make sure that ADJDYNALLOC is included where necessary.
577 if (AM.isDynAlloc() && !AM.IncludesDynAlloc)
578 return false;
579
580 DEBUG(AM.dump());
581 return true;
582}
583
584// Insert a node into the DAG at least before Pos. This will reposition
585// the node as needed, and will assign it a node ID that is <= Pos's ID.
586// Note that this does *not* preserve the uniqueness of node IDs!
587// The selection DAG must no longer depend on their uniqueness when this
588// function is used.
589static void insertDAGNode(SelectionDAG *DAG, SDNode *Pos, SDValue N) {
590 if (N.getNode()->getNodeId() == -1 ||
591 N.getNode()->getNodeId() > Pos->getNodeId()) {
Duncan P. N. Exon Smitha2c90e42015-10-20 01:12:46 +0000592 DAG->RepositionNode(Pos->getIterator(), N.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000593 N.getNode()->setNodeId(Pos->getNodeId());
594 }
595}
596
597void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
598 EVT VT, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000599 SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000600 Base = AM.Base;
601 if (!Base.getNode())
602 // Register 0 means "no base". This is mostly useful for shifts.
603 Base = CurDAG->getRegister(0, VT);
604 else if (Base.getOpcode() == ISD::FrameIndex) {
605 // Lower a FrameIndex to a TargetFrameIndex.
606 int64_t FrameIndex = cast<FrameIndexSDNode>(Base)->getIndex();
607 Base = CurDAG->getTargetFrameIndex(FrameIndex, VT);
608 } else if (Base.getValueType() != VT) {
609 // Truncate values from i64 to i32, for shifts.
610 assert(VT == MVT::i32 && Base.getValueType() == MVT::i64 &&
611 "Unexpected truncation");
Andrew Trickef9de2a2013-05-25 02:42:55 +0000612 SDLoc DL(Base);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000613 SDValue Trunc = CurDAG->getNode(ISD::TRUNCATE, DL, VT, Base);
614 insertDAGNode(CurDAG, Base.getNode(), Trunc);
615 Base = Trunc;
616 }
617
618 // Lower the displacement to a TargetConstant.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000619 Disp = CurDAG->getTargetConstant(AM.Disp, SDLoc(Base), VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000620}
621
622void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
623 EVT VT, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000624 SDValue &Disp,
625 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000626 getAddressOperands(AM, VT, Base, Disp);
627
628 Index = AM.Index;
629 if (!Index.getNode())
630 // Register 0 means "no index".
631 Index = CurDAG->getRegister(0, VT);
632}
633
634bool SystemZDAGToDAGISel::selectBDAddr(SystemZAddressingMode::DispRange DR,
635 SDValue Addr, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000636 SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000637 SystemZAddressingMode AM(SystemZAddressingMode::FormBD, DR);
638 if (!selectAddress(Addr, AM))
639 return false;
640
641 getAddressOperands(AM, Addr.getValueType(), Base, Disp);
642 return true;
643}
644
Richard Sandiforda481f582013-08-23 11:18:53 +0000645bool SystemZDAGToDAGISel::selectMVIAddr(SystemZAddressingMode::DispRange DR,
646 SDValue Addr, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000647 SDValue &Disp) const {
Richard Sandiforda481f582013-08-23 11:18:53 +0000648 SystemZAddressingMode AM(SystemZAddressingMode::FormBDXNormal, DR);
649 if (!selectAddress(Addr, AM) || AM.Index.getNode())
650 return false;
651
652 getAddressOperands(AM, Addr.getValueType(), Base, Disp);
653 return true;
654}
655
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000656bool SystemZDAGToDAGISel::selectBDXAddr(SystemZAddressingMode::AddrForm Form,
657 SystemZAddressingMode::DispRange DR,
658 SDValue Addr, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000659 SDValue &Disp, SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000660 SystemZAddressingMode AM(Form, DR);
661 if (!selectAddress(Addr, AM))
662 return false;
663
664 getAddressOperands(AM, Addr.getValueType(), Base, Disp, Index);
665 return true;
666}
667
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000668bool SystemZDAGToDAGISel::selectBDVAddr12Only(SDValue Addr, SDValue Elem,
669 SDValue &Base,
670 SDValue &Disp,
671 SDValue &Index) const {
672 SDValue Regs[2];
673 if (selectBDXAddr12Only(Addr, Regs[0], Disp, Regs[1]) &&
674 Regs[0].getNode() && Regs[1].getNode()) {
675 for (unsigned int I = 0; I < 2; ++I) {
676 Base = Regs[I];
677 Index = Regs[1 - I];
678 // We can't tell here whether the index vector has the right type
679 // for the access; the caller needs to do that instead.
680 if (Index.getOpcode() == ISD::ZERO_EXTEND)
681 Index = Index.getOperand(0);
682 if (Index.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
683 Index.getOperand(1) == Elem) {
684 Index = Index.getOperand(0);
685 return true;
686 }
687 }
688 }
689 return false;
690}
691
Richard Sandiford885140c2013-07-16 11:55:57 +0000692bool SystemZDAGToDAGISel::detectOrAndInsertion(SDValue &Op,
Richard Sandiford54b36912013-09-27 15:14:04 +0000693 uint64_t InsertMask) const {
Richard Sandiford885140c2013-07-16 11:55:57 +0000694 // We're only interested in cases where the insertion is into some operand
695 // of Op, rather than into Op itself. The only useful case is an AND.
696 if (Op.getOpcode() != ISD::AND)
697 return false;
698
699 // We need a constant mask.
Richard Sandiford21f5d682014-03-06 11:22:58 +0000700 auto *MaskNode = dyn_cast<ConstantSDNode>(Op.getOperand(1).getNode());
Richard Sandiford885140c2013-07-16 11:55:57 +0000701 if (!MaskNode)
702 return false;
703
704 // It's not an insertion of Op.getOperand(0) if the two masks overlap.
705 uint64_t AndMask = MaskNode->getZExtValue();
706 if (InsertMask & AndMask)
707 return false;
708
709 // It's only an insertion if all bits are covered or are known to be zero.
710 // The inner check covers all cases but is more expensive.
711 uint64_t Used = allOnes(Op.getValueType().getSizeInBits());
712 if (Used != (AndMask | InsertMask)) {
713 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +0000714 CurDAG->computeKnownBits(Op.getOperand(0), KnownZero, KnownOne);
Richard Sandiford885140c2013-07-16 11:55:57 +0000715 if (Used != (AndMask | InsertMask | KnownZero.getZExtValue()))
716 return false;
717 }
718
719 Op = Op.getOperand(0);
720 return true;
721}
722
Richard Sandiford54b36912013-09-27 15:14:04 +0000723bool SystemZDAGToDAGISel::refineRxSBGMask(RxSBGOperands &RxSBG,
724 uint64_t Mask) const {
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000725 const SystemZInstrInfo *TII = getInstrInfo();
Richard Sandiford5cbac962013-07-18 09:45:08 +0000726 if (RxSBG.Rotate != 0)
727 Mask = (Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate));
728 Mask &= RxSBG.Mask;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000729 if (TII->isRxSBGMask(Mask, RxSBG.BitSize, RxSBG.Start, RxSBG.End)) {
Richard Sandiford5cbac962013-07-18 09:45:08 +0000730 RxSBG.Mask = Mask;
Richard Sandiford5cbac962013-07-18 09:45:08 +0000731 return true;
732 }
Richard Sandiford84f54a32013-07-11 08:59:12 +0000733 return false;
734}
735
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000736// Return true if any bits of (RxSBG.Input & Mask) are significant.
737static bool maskMatters(RxSBGOperands &RxSBG, uint64_t Mask) {
738 // Rotate the mask in the same way as RxSBG.Input is rotated.
Richard Sandiford297f7d22013-07-18 10:14:55 +0000739 if (RxSBG.Rotate != 0)
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000740 Mask = ((Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate)));
741 return (Mask & RxSBG.Mask) != 0;
Richard Sandiford297f7d22013-07-18 10:14:55 +0000742}
743
Richard Sandiford54b36912013-09-27 15:14:04 +0000744bool SystemZDAGToDAGISel::expandRxSBG(RxSBGOperands &RxSBG) const {
Richard Sandiford5cbac962013-07-18 09:45:08 +0000745 SDValue N = RxSBG.Input;
Richard Sandiford297f7d22013-07-18 10:14:55 +0000746 unsigned Opcode = N.getOpcode();
747 switch (Opcode) {
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000748 case ISD::AND: {
Richard Sandiford51093212013-07-18 10:40:35 +0000749 if (RxSBG.Opcode == SystemZ::RNSBG)
750 return false;
751
Richard Sandiford21f5d682014-03-06 11:22:58 +0000752 auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000753 if (!MaskNode)
754 return false;
755
756 SDValue Input = N.getOperand(0);
757 uint64_t Mask = MaskNode->getZExtValue();
Richard Sandiford5cbac962013-07-18 09:45:08 +0000758 if (!refineRxSBGMask(RxSBG, Mask)) {
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000759 // If some bits of Input are already known zeros, those bits will have
760 // been removed from the mask. See if adding them back in makes the
761 // mask suitable.
762 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +0000763 CurDAG->computeKnownBits(Input, KnownZero, KnownOne);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000764 Mask |= KnownZero.getZExtValue();
Richard Sandiford5cbac962013-07-18 09:45:08 +0000765 if (!refineRxSBGMask(RxSBG, Mask))
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000766 return false;
767 }
Richard Sandiford5cbac962013-07-18 09:45:08 +0000768 RxSBG.Input = Input;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000769 return true;
770 }
771
Richard Sandiford51093212013-07-18 10:40:35 +0000772 case ISD::OR: {
773 if (RxSBG.Opcode != SystemZ::RNSBG)
774 return false;
775
Richard Sandiford21f5d682014-03-06 11:22:58 +0000776 auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford51093212013-07-18 10:40:35 +0000777 if (!MaskNode)
778 return false;
779
780 SDValue Input = N.getOperand(0);
781 uint64_t Mask = ~MaskNode->getZExtValue();
782 if (!refineRxSBGMask(RxSBG, Mask)) {
783 // If some bits of Input are already known ones, those bits will have
784 // been removed from the mask. See if adding them back in makes the
785 // mask suitable.
786 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +0000787 CurDAG->computeKnownBits(Input, KnownZero, KnownOne);
Richard Sandiford51093212013-07-18 10:40:35 +0000788 Mask &= ~KnownOne.getZExtValue();
789 if (!refineRxSBGMask(RxSBG, Mask))
790 return false;
791 }
792 RxSBG.Input = Input;
793 return true;
794 }
795
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000796 case ISD::ROTL: {
Richard Sandiford5cbac962013-07-18 09:45:08 +0000797 // Any 64-bit rotate left can be merged into the RxSBG.
Richard Sandiford3e382972013-10-16 13:35:13 +0000798 if (RxSBG.BitSize != 64 || N.getValueType() != MVT::i64)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000799 return false;
Richard Sandiford21f5d682014-03-06 11:22:58 +0000800 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000801 if (!CountNode)
802 return false;
803
Richard Sandiford5cbac962013-07-18 09:45:08 +0000804 RxSBG.Rotate = (RxSBG.Rotate + CountNode->getZExtValue()) & 63;
805 RxSBG.Input = N.getOperand(0);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000806 return true;
807 }
Simon Pilgrim0750c842015-08-15 13:27:30 +0000808
Richard Sandiford220ee492013-12-20 11:49:48 +0000809 case ISD::ANY_EXTEND:
810 // Bits above the extended operand are don't-care.
811 RxSBG.Input = N.getOperand(0);
812 return true;
813
Richard Sandiford3875cb62014-01-09 11:28:53 +0000814 case ISD::ZERO_EXTEND:
815 if (RxSBG.Opcode != SystemZ::RNSBG) {
816 // Restrict the mask to the extended operand.
817 unsigned InnerBitSize = N.getOperand(0).getValueType().getSizeInBits();
818 if (!refineRxSBGMask(RxSBG, allOnes(InnerBitSize)))
819 return false;
Richard Sandiford220ee492013-12-20 11:49:48 +0000820
Richard Sandiford3875cb62014-01-09 11:28:53 +0000821 RxSBG.Input = N.getOperand(0);
822 return true;
823 }
824 // Fall through.
Simon Pilgrim0750c842015-08-15 13:27:30 +0000825
Richard Sandiford220ee492013-12-20 11:49:48 +0000826 case ISD::SIGN_EXTEND: {
Richard Sandiford3e382972013-10-16 13:35:13 +0000827 // Check that the extension bits are don't-care (i.e. are masked out
828 // by the final mask).
829 unsigned InnerBitSize = N.getOperand(0).getValueType().getSizeInBits();
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000830 if (maskMatters(RxSBG, allOnes(RxSBG.BitSize) - allOnes(InnerBitSize)))
Richard Sandiford3e382972013-10-16 13:35:13 +0000831 return false;
832
833 RxSBG.Input = N.getOperand(0);
834 return true;
835 }
836
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000837 case ISD::SHL: {
Richard Sandiford21f5d682014-03-06 11:22:58 +0000838 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000839 if (!CountNode)
840 return false;
841
842 uint64_t Count = CountNode->getZExtValue();
Richard Sandiford3e382972013-10-16 13:35:13 +0000843 unsigned BitSize = N.getValueType().getSizeInBits();
844 if (Count < 1 || Count >= BitSize)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000845 return false;
846
Richard Sandiford51093212013-07-18 10:40:35 +0000847 if (RxSBG.Opcode == SystemZ::RNSBG) {
848 // Treat (shl X, count) as (rotl X, size-count) as long as the bottom
849 // count bits from RxSBG.Input are ignored.
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000850 if (maskMatters(RxSBG, allOnes(Count)))
Richard Sandiford51093212013-07-18 10:40:35 +0000851 return false;
852 } else {
853 // Treat (shl X, count) as (and (rotl X, count), ~0<<count).
Richard Sandiford3e382972013-10-16 13:35:13 +0000854 if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count) << Count))
Richard Sandiford51093212013-07-18 10:40:35 +0000855 return false;
856 }
857
Richard Sandiford5cbac962013-07-18 09:45:08 +0000858 RxSBG.Rotate = (RxSBG.Rotate + Count) & 63;
859 RxSBG.Input = N.getOperand(0);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000860 return true;
861 }
862
Richard Sandiford297f7d22013-07-18 10:14:55 +0000863 case ISD::SRL:
864 case ISD::SRA: {
Richard Sandiford21f5d682014-03-06 11:22:58 +0000865 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000866 if (!CountNode)
867 return false;
868
869 uint64_t Count = CountNode->getZExtValue();
Richard Sandiford3e382972013-10-16 13:35:13 +0000870 unsigned BitSize = N.getValueType().getSizeInBits();
871 if (Count < 1 || Count >= BitSize)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000872 return false;
873
Richard Sandiford51093212013-07-18 10:40:35 +0000874 if (RxSBG.Opcode == SystemZ::RNSBG || Opcode == ISD::SRA) {
875 // Treat (srl|sra X, count) as (rotl X, size-count) as long as the top
876 // count bits from RxSBG.Input are ignored.
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000877 if (maskMatters(RxSBG, allOnes(Count) << (BitSize - Count)))
Richard Sandiford297f7d22013-07-18 10:14:55 +0000878 return false;
879 } else {
880 // Treat (srl X, count), mask) as (and (rotl X, size-count), ~0>>count),
881 // which is similar to SLL above.
Richard Sandiford3e382972013-10-16 13:35:13 +0000882 if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count)))
Richard Sandiford297f7d22013-07-18 10:14:55 +0000883 return false;
884 }
885
Richard Sandiford5cbac962013-07-18 09:45:08 +0000886 RxSBG.Rotate = (RxSBG.Rotate - Count) & 63;
887 RxSBG.Input = N.getOperand(0);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000888 return true;
889 }
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000890 default:
891 return false;
892 }
893}
894
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000895SDValue SystemZDAGToDAGISel::getUNDEF(const SDLoc &DL, EVT VT) const {
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000896 SDNode *N = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, VT);
Richard Sandiford84f54a32013-07-11 08:59:12 +0000897 return SDValue(N, 0);
898}
899
Benjamin Kramerbdc49562016-06-12 15:39:02 +0000900SDValue SystemZDAGToDAGISel::convertTo(const SDLoc &DL, EVT VT,
901 SDValue N) const {
Richard Sandifordd8163202013-09-13 09:12:44 +0000902 if (N.getValueType() == MVT::i32 && VT == MVT::i64)
Richard Sandiford87a44362013-09-30 10:28:35 +0000903 return CurDAG->getTargetInsertSubreg(SystemZ::subreg_l32,
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000904 DL, VT, getUNDEF(DL, MVT::i64), N);
Richard Sandifordd8163202013-09-13 09:12:44 +0000905 if (N.getValueType() == MVT::i64 && VT == MVT::i32)
Richard Sandiford87a44362013-09-30 10:28:35 +0000906 return CurDAG->getTargetExtractSubreg(SystemZ::subreg_l32, DL, VT, N);
Richard Sandiford84f54a32013-07-11 08:59:12 +0000907 assert(N.getValueType() == VT && "Unexpected value types");
908 return N;
909}
910
Justin Bognerbbcd2232016-05-10 21:11:26 +0000911bool SystemZDAGToDAGISel::tryRISBGZero(SDNode *N) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000912 SDLoc DL(N);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000913 EVT VT = N->getValueType(0);
Ulrich Weigand77884bc2015-06-25 11:52:36 +0000914 if (!VT.isInteger() || VT.getSizeInBits() > 64)
Justin Bognerbbcd2232016-05-10 21:11:26 +0000915 return false;
Richard Sandiford51093212013-07-18 10:40:35 +0000916 RxSBGOperands RISBG(SystemZ::RISBG, SDValue(N, 0));
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000917 unsigned Count = 0;
Richard Sandiford5cbac962013-07-18 09:45:08 +0000918 while (expandRxSBG(RISBG))
Richard Sandiford3e382972013-10-16 13:35:13 +0000919 if (RISBG.Input.getOpcode() != ISD::ANY_EXTEND)
920 Count += 1;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000921 if (Count == 0)
Justin Bognerbbcd2232016-05-10 21:11:26 +0000922 return false;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000923 if (Count == 1) {
924 // Prefer to use normal shift instructions over RISBG, since they can handle
925 // all cases and are sometimes shorter.
926 if (N->getOpcode() != ISD::AND)
Justin Bognerbbcd2232016-05-10 21:11:26 +0000927 return false;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000928
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000929 // Prefer register extensions like LLC over RISBG. Also prefer to start
930 // out with normal ANDs if one instruction would be enough. We can convert
931 // these ANDs into an RISBG later if a three-address instruction is useful.
932 if (VT == MVT::i32 ||
933 RISBG.Mask == 0xff ||
934 RISBG.Mask == 0xffff ||
935 SystemZ::isImmLF(~RISBG.Mask) ||
936 SystemZ::isImmHF(~RISBG.Mask)) {
937 // Force the new mask into the DAG, since it may include known-one bits.
Richard Sandiford21f5d682014-03-06 11:22:58 +0000938 auto *MaskN = cast<ConstantSDNode>(N->getOperand(1).getNode());
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000939 if (MaskN->getZExtValue() != RISBG.Mask) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000940 SDValue NewMask = CurDAG->getConstant(RISBG.Mask, DL, VT);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000941 N = CurDAG->UpdateNodeOperands(N, N->getOperand(0), NewMask);
Justin Bognerbbcd2232016-05-10 21:11:26 +0000942 SelectCode(N);
943 return true;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000944 }
Justin Bognerbbcd2232016-05-10 21:11:26 +0000945 return false;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000946 }
Simon Pilgrim0750c842015-08-15 13:27:30 +0000947 }
948
949 // If the RISBG operands require no rotation and just masks the bottom
950 // 8/16 bits, attempt to convert this to a LLC zero extension.
951 if (RISBG.Rotate == 0 && (RISBG.Mask == 0xff || RISBG.Mask == 0xffff)) {
952 unsigned OpCode = (RISBG.Mask == 0xff ? SystemZ::LLGCR : SystemZ::LLGHR);
953 if (VT == MVT::i32) {
954 if (Subtarget->hasHighWord())
955 OpCode = (RISBG.Mask == 0xff ? SystemZ::LLCRMux : SystemZ::LLHRMux);
956 else
957 OpCode = (RISBG.Mask == 0xff ? SystemZ::LLCR : SystemZ::LLHR);
958 }
959
960 SDValue In = convertTo(DL, VT, RISBG.Input);
Justin Bognerbbcd2232016-05-10 21:11:26 +0000961 SDValue New = convertTo(
962 DL, VT, SDValue(CurDAG->getMachineNode(OpCode, DL, VT, In), 0));
963 ReplaceUses(N, New.getNode());
964 CurDAG->RemoveDeadNode(N);
965 return true;
Simon Pilgrim0750c842015-08-15 13:27:30 +0000966 }
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000967
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000968 unsigned Opcode = SystemZ::RISBG;
Ulrich Weigand371d10a2015-03-31 12:58:17 +0000969 // Prefer RISBGN if available, since it does not clobber CC.
970 if (Subtarget->hasMiscellaneousExtensions())
971 Opcode = SystemZ::RISBGN;
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000972 EVT OpcodeVT = MVT::i64;
Eric Christophera6734172015-01-31 00:06:45 +0000973 if (VT == MVT::i32 && Subtarget->hasHighWord()) {
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000974 Opcode = SystemZ::RISBMux;
975 OpcodeVT = MVT::i32;
976 RISBG.Start &= 31;
977 RISBG.End &= 31;
978 }
Richard Sandiford84f54a32013-07-11 08:59:12 +0000979 SDValue Ops[5] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000980 getUNDEF(DL, OpcodeVT),
981 convertTo(DL, OpcodeVT, RISBG.Input),
982 CurDAG->getTargetConstant(RISBG.Start, DL, MVT::i32),
983 CurDAG->getTargetConstant(RISBG.End | 128, DL, MVT::i32),
984 CurDAG->getTargetConstant(RISBG.Rotate, DL, MVT::i32)
Richard Sandiford84f54a32013-07-11 08:59:12 +0000985 };
Justin Bognerbbcd2232016-05-10 21:11:26 +0000986 SDValue New = convertTo(
987 DL, VT, SDValue(CurDAG->getMachineNode(Opcode, DL, OpcodeVT, Ops), 0));
988 ReplaceUses(N, New.getNode());
989 CurDAG->RemoveDeadNode(N);
990 return true;
Richard Sandiford84f54a32013-07-11 08:59:12 +0000991}
992
Justin Bogner9b34e8a2016-05-13 22:42:08 +0000993bool SystemZDAGToDAGISel::tryRxSBG(SDNode *N, unsigned Opcode) {
Ulrich Weigand77884bc2015-06-25 11:52:36 +0000994 SDLoc DL(N);
995 EVT VT = N->getValueType(0);
996 if (!VT.isInteger() || VT.getSizeInBits() > 64)
Justin Bogner9b34e8a2016-05-13 22:42:08 +0000997 return false;
Richard Sandiford7878b852013-07-18 10:06:15 +0000998 // Try treating each operand of N as the second operand of the RxSBG
Richard Sandiford885140c2013-07-16 11:55:57 +0000999 // and see which goes deepest.
Richard Sandiford51093212013-07-18 10:40:35 +00001000 RxSBGOperands RxSBG[] = {
1001 RxSBGOperands(Opcode, N->getOperand(0)),
1002 RxSBGOperands(Opcode, N->getOperand(1))
1003 };
Richard Sandiford885140c2013-07-16 11:55:57 +00001004 unsigned Count[] = { 0, 0 };
1005 for (unsigned I = 0; I < 2; ++I)
Richard Sandiford5cbac962013-07-18 09:45:08 +00001006 while (expandRxSBG(RxSBG[I]))
Richard Sandiford3e382972013-10-16 13:35:13 +00001007 if (RxSBG[I].Input.getOpcode() != ISD::ANY_EXTEND)
1008 Count[I] += 1;
Richard Sandiford885140c2013-07-16 11:55:57 +00001009
1010 // Do nothing if neither operand is suitable.
1011 if (Count[0] == 0 && Count[1] == 0)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001012 return false;
Richard Sandiford885140c2013-07-16 11:55:57 +00001013
1014 // Pick the deepest second operand.
1015 unsigned I = Count[0] > Count[1] ? 0 : 1;
1016 SDValue Op0 = N->getOperand(I ^ 1);
1017
1018 // Prefer IC for character insertions from memory.
Richard Sandiford7878b852013-07-18 10:06:15 +00001019 if (Opcode == SystemZ::ROSBG && (RxSBG[I].Mask & 0xff) == 0)
Richard Sandiford21f5d682014-03-06 11:22:58 +00001020 if (auto *Load = dyn_cast<LoadSDNode>(Op0.getNode()))
Richard Sandiford885140c2013-07-16 11:55:57 +00001021 if (Load->getMemoryVT() == MVT::i8)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001022 return false;
Richard Sandiford885140c2013-07-16 11:55:57 +00001023
1024 // See whether we can avoid an AND in the first operand by converting
1025 // ROSBG to RISBG.
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001026 if (Opcode == SystemZ::ROSBG && detectOrAndInsertion(Op0, RxSBG[I].Mask)) {
Richard Sandiford885140c2013-07-16 11:55:57 +00001027 Opcode = SystemZ::RISBG;
Ulrich Weigand371d10a2015-03-31 12:58:17 +00001028 // Prefer RISBGN if available, since it does not clobber CC.
1029 if (Subtarget->hasMiscellaneousExtensions())
1030 Opcode = SystemZ::RISBGN;
1031 }
1032
Richard Sandiford885140c2013-07-16 11:55:57 +00001033 SDValue Ops[5] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001034 convertTo(DL, MVT::i64, Op0),
1035 convertTo(DL, MVT::i64, RxSBG[I].Input),
1036 CurDAG->getTargetConstant(RxSBG[I].Start, DL, MVT::i32),
1037 CurDAG->getTargetConstant(RxSBG[I].End, DL, MVT::i32),
1038 CurDAG->getTargetConstant(RxSBG[I].Rotate, DL, MVT::i32)
Richard Sandiford885140c2013-07-16 11:55:57 +00001039 };
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001040 SDValue New = convertTo(
1041 DL, VT, SDValue(CurDAG->getMachineNode(Opcode, DL, MVT::i64, Ops), 0));
1042 ReplaceNode(N, New.getNode());
1043 return true;
Richard Sandiford885140c2013-07-16 11:55:57 +00001044}
1045
Justin Bognerffb273d2016-05-09 23:54:23 +00001046void SystemZDAGToDAGISel::splitLargeImmediate(unsigned Opcode, SDNode *Node,
1047 SDValue Op0, uint64_t UpperVal,
1048 uint64_t LowerVal) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001049 EVT VT = Node->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001050 SDLoc DL(Node);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001051 SDValue Upper = CurDAG->getConstant(UpperVal, DL, VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001052 if (Op0.getNode())
1053 Upper = CurDAG->getNode(Opcode, DL, VT, Op0, Upper);
Justin Bognerffb273d2016-05-09 23:54:23 +00001054
1055 {
1056 // When we haven't passed in Op0, Upper will be a constant. In order to
1057 // prevent folding back to the large immediate in `Or = getNode(...)` we run
1058 // SelectCode first and end up with an opaque machine node. This means that
1059 // we need to use a handle to keep track of Upper in case it gets CSE'd by
1060 // SelectCode.
1061 //
1062 // Note that in the case where Op0 is passed in we could just call
1063 // SelectCode(Upper) later, along with the SelectCode(Or), and avoid needing
1064 // the handle at all, but it's fine to do it here.
1065 //
1066 // TODO: This is a pretty hacky way to do this. Can we do something that
1067 // doesn't require a two paragraph explanation?
1068 HandleSDNode Handle(Upper);
1069 SelectCode(Upper.getNode());
1070 Upper = Handle.getValue();
1071 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001072
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001073 SDValue Lower = CurDAG->getConstant(LowerVal, DL, VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001074 SDValue Or = CurDAG->getNode(Opcode, DL, VT, Upper, Lower);
Justin Bognerffb273d2016-05-09 23:54:23 +00001075
1076 ReplaceUses(Node, Or.getNode());
1077 CurDAG->RemoveDeadNode(Node);
1078
1079 SelectCode(Or.getNode());
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001080}
1081
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001082bool SystemZDAGToDAGISel::tryGather(SDNode *N, unsigned Opcode) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001083 SDValue ElemV = N->getOperand(2);
1084 auto *ElemN = dyn_cast<ConstantSDNode>(ElemV);
1085 if (!ElemN)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001086 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001087
1088 unsigned Elem = ElemN->getZExtValue();
1089 EVT VT = N->getValueType(0);
1090 if (Elem >= VT.getVectorNumElements())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001091 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001092
1093 auto *Load = dyn_cast<LoadSDNode>(N->getOperand(1));
1094 if (!Load || !Load->hasOneUse())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001095 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001096 if (Load->getMemoryVT().getSizeInBits() !=
1097 Load->getValueType(0).getSizeInBits())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001098 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001099
1100 SDValue Base, Disp, Index;
1101 if (!selectBDVAddr12Only(Load->getBasePtr(), ElemV, Base, Disp, Index) ||
1102 Index.getValueType() != VT.changeVectorElementTypeToInteger())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001103 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001104
1105 SDLoc DL(Load);
1106 SDValue Ops[] = {
1107 N->getOperand(0), Base, Disp, Index,
1108 CurDAG->getTargetConstant(Elem, DL, MVT::i32), Load->getChain()
1109 };
1110 SDNode *Res = CurDAG->getMachineNode(Opcode, DL, VT, MVT::Other, Ops);
1111 ReplaceUses(SDValue(Load, 1), SDValue(Res, 1));
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001112 ReplaceNode(N, Res);
1113 return true;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001114}
1115
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001116bool SystemZDAGToDAGISel::tryScatter(StoreSDNode *Store, unsigned Opcode) {
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001117 SDValue Value = Store->getValue();
1118 if (Value.getOpcode() != ISD::EXTRACT_VECTOR_ELT)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001119 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001120 if (Store->getMemoryVT().getSizeInBits() !=
1121 Value.getValueType().getSizeInBits())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001122 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001123
1124 SDValue ElemV = Value.getOperand(1);
1125 auto *ElemN = dyn_cast<ConstantSDNode>(ElemV);
1126 if (!ElemN)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001127 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001128
1129 SDValue Vec = Value.getOperand(0);
1130 EVT VT = Vec.getValueType();
1131 unsigned Elem = ElemN->getZExtValue();
1132 if (Elem >= VT.getVectorNumElements())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001133 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001134
1135 SDValue Base, Disp, Index;
1136 if (!selectBDVAddr12Only(Store->getBasePtr(), ElemV, Base, Disp, Index) ||
1137 Index.getValueType() != VT.changeVectorElementTypeToInteger())
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001138 return false;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001139
1140 SDLoc DL(Store);
1141 SDValue Ops[] = {
1142 Vec, Base, Disp, Index, CurDAG->getTargetConstant(Elem, DL, MVT::i32),
1143 Store->getChain()
1144 };
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001145 ReplaceNode(Store, CurDAG->getMachineNode(Opcode, DL, MVT::Other, Ops));
1146 return true;
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001147}
1148
Richard Sandiford067817e2013-09-27 15:29:20 +00001149bool SystemZDAGToDAGISel::canUseBlockOperation(StoreSDNode *Store,
1150 LoadSDNode *Load) const {
Richard Sandiford178273a2013-09-05 10:36:45 +00001151 // Check that the two memory operands have the same size.
1152 if (Load->getMemoryVT() != Store->getMemoryVT())
Richard Sandiford97846492013-07-09 09:46:39 +00001153 return false;
1154
Richard Sandiford178273a2013-09-05 10:36:45 +00001155 // Volatility stops an access from being decomposed.
1156 if (Load->isVolatile() || Store->isVolatile())
1157 return false;
Richard Sandiford97846492013-07-09 09:46:39 +00001158
1159 // There's no chance of overlap if the load is invariant.
1160 if (Load->isInvariant())
1161 return true;
1162
Richard Sandiford97846492013-07-09 09:46:39 +00001163 // Otherwise we need to check whether there's an alias.
Nick Lewyckyaad475b2014-04-15 07:22:52 +00001164 const Value *V1 = Load->getMemOperand()->getValue();
1165 const Value *V2 = Store->getMemOperand()->getValue();
Richard Sandiford97846492013-07-09 09:46:39 +00001166 if (!V1 || !V2)
1167 return false;
1168
Richard Sandiford067817e2013-09-27 15:29:20 +00001169 // Reject equality.
1170 uint64_t Size = Load->getMemoryVT().getStoreSize();
Richard Sandiford97846492013-07-09 09:46:39 +00001171 int64_t End1 = Load->getSrcValueOffset() + Size;
1172 int64_t End2 = Store->getSrcValueOffset() + Size;
Richard Sandiford067817e2013-09-27 15:29:20 +00001173 if (V1 == V2 && End1 == End2)
1174 return false;
1175
Chandler Carruthac80dc72015-06-17 07:18:54 +00001176 return !AA->alias(MemoryLocation(V1, End1, Load->getAAInfo()),
1177 MemoryLocation(V2, End2, Store->getAAInfo()));
Richard Sandiford97846492013-07-09 09:46:39 +00001178}
1179
Richard Sandiford178273a2013-09-05 10:36:45 +00001180bool SystemZDAGToDAGISel::storeLoadCanUseMVC(SDNode *N) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001181 auto *Store = cast<StoreSDNode>(N);
1182 auto *Load = cast<LoadSDNode>(Store->getValue());
Richard Sandiford178273a2013-09-05 10:36:45 +00001183
1184 // Prefer not to use MVC if either address can use ... RELATIVE LONG
1185 // instructions.
1186 uint64_t Size = Load->getMemoryVT().getStoreSize();
1187 if (Size > 1 && Size <= 8) {
1188 // Prefer LHRL, LRL and LGRL.
Richard Sandiford54b36912013-09-27 15:14:04 +00001189 if (SystemZISD::isPCREL(Load->getBasePtr().getOpcode()))
Richard Sandiford178273a2013-09-05 10:36:45 +00001190 return false;
1191 // Prefer STHRL, STRL and STGRL.
Richard Sandiford54b36912013-09-27 15:14:04 +00001192 if (SystemZISD::isPCREL(Store->getBasePtr().getOpcode()))
Richard Sandiford178273a2013-09-05 10:36:45 +00001193 return false;
1194 }
1195
Richard Sandiford067817e2013-09-27 15:29:20 +00001196 return canUseBlockOperation(Store, Load);
Richard Sandiford178273a2013-09-05 10:36:45 +00001197}
1198
1199bool SystemZDAGToDAGISel::storeLoadCanUseBlockBinary(SDNode *N,
1200 unsigned I) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001201 auto *StoreA = cast<StoreSDNode>(N);
1202 auto *LoadA = cast<LoadSDNode>(StoreA->getValue().getOperand(1 - I));
1203 auto *LoadB = cast<LoadSDNode>(StoreA->getValue().getOperand(I));
Richard Sandiford067817e2013-09-27 15:29:20 +00001204 return !LoadA->isVolatile() && canUseBlockOperation(StoreA, LoadB);
Richard Sandiford178273a2013-09-05 10:36:45 +00001205}
1206
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001207void SystemZDAGToDAGISel::Select(SDNode *Node) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001208 // Dump information about the Node being selected
1209 DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n");
1210
1211 // If we have a custom node, we already have selected!
1212 if (Node->isMachineOpcode()) {
1213 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
Tim Northover31d093c2013-09-22 08:21:56 +00001214 Node->setNodeId(-1);
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001215 return;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001216 }
1217
1218 unsigned Opcode = Node->getOpcode();
1219 switch (Opcode) {
1220 case ISD::OR:
Richard Sandiford885140c2013-07-16 11:55:57 +00001221 if (Node->getOperand(1).getOpcode() != ISD::Constant)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001222 if (tryRxSBG(Node, SystemZ::ROSBG))
1223 return;
Richard Sandiford7878b852013-07-18 10:06:15 +00001224 goto or_xor;
1225
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001226 case ISD::XOR:
Richard Sandiford7878b852013-07-18 10:06:15 +00001227 if (Node->getOperand(1).getOpcode() != ISD::Constant)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001228 if (tryRxSBG(Node, SystemZ::RXSBG))
1229 return;
Richard Sandiford7878b852013-07-18 10:06:15 +00001230 // Fall through.
1231 or_xor:
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001232 // If this is a 64-bit operation in which both 32-bit halves are nonzero,
1233 // split the operation into two.
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001234 if (Node->getValueType(0) == MVT::i64)
Richard Sandiford21f5d682014-03-06 11:22:58 +00001235 if (auto *Op1 = dyn_cast<ConstantSDNode>(Node->getOperand(1))) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001236 uint64_t Val = Op1->getZExtValue();
Justin Bognerffb273d2016-05-09 23:54:23 +00001237 if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val)) {
1238 splitLargeImmediate(Opcode, Node, Node->getOperand(0),
1239 Val - uint32_t(Val), uint32_t(Val));
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001240 return;
Justin Bognerffb273d2016-05-09 23:54:23 +00001241 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001242 }
1243 break;
1244
Richard Sandiford84f54a32013-07-11 08:59:12 +00001245 case ISD::AND:
Richard Sandiford51093212013-07-18 10:40:35 +00001246 if (Node->getOperand(1).getOpcode() != ISD::Constant)
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001247 if (tryRxSBG(Node, SystemZ::RNSBG))
1248 return;
Richard Sandiford51093212013-07-18 10:40:35 +00001249 // Fall through.
Richard Sandiford82ec87d2013-07-16 11:02:24 +00001250 case ISD::ROTL:
1251 case ISD::SHL:
1252 case ISD::SRL:
Richard Sandiford220ee492013-12-20 11:49:48 +00001253 case ISD::ZERO_EXTEND:
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001254 if (tryRISBGZero(Node))
1255 return;
Richard Sandiford84f54a32013-07-11 08:59:12 +00001256 break;
1257
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001258 case ISD::Constant:
1259 // If this is a 64-bit constant that is out of the range of LLILF,
1260 // LLIHF and LGFI, split it into two 32-bit pieces.
1261 if (Node->getValueType(0) == MVT::i64) {
1262 uint64_t Val = cast<ConstantSDNode>(Node)->getZExtValue();
Justin Bognerffb273d2016-05-09 23:54:23 +00001263 if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val) && !isInt<32>(Val)) {
1264 splitLargeImmediate(ISD::OR, Node, SDValue(), Val - uint32_t(Val),
1265 uint32_t(Val));
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001266 return;
Justin Bognerffb273d2016-05-09 23:54:23 +00001267 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001268 }
1269 break;
1270
Richard Sandifordee834382013-07-31 12:38:08 +00001271 case SystemZISD::SELECT_CCMASK: {
1272 SDValue Op0 = Node->getOperand(0);
1273 SDValue Op1 = Node->getOperand(1);
1274 // Prefer to put any load first, so that it can be matched as a
1275 // conditional load.
1276 if (Op1.getOpcode() == ISD::LOAD && Op0.getOpcode() != ISD::LOAD) {
1277 SDValue CCValid = Node->getOperand(2);
1278 SDValue CCMask = Node->getOperand(3);
1279 uint64_t ConstCCValid =
1280 cast<ConstantSDNode>(CCValid.getNode())->getZExtValue();
1281 uint64_t ConstCCMask =
1282 cast<ConstantSDNode>(CCMask.getNode())->getZExtValue();
1283 // Invert the condition.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001284 CCMask = CurDAG->getConstant(ConstCCValid ^ ConstCCMask, SDLoc(Node),
Richard Sandifordee834382013-07-31 12:38:08 +00001285 CCMask.getValueType());
1286 SDValue Op4 = Node->getOperand(4);
1287 Node = CurDAG->UpdateNodeOperands(Node, Op1, Op0, CCValid, CCMask, Op4);
1288 }
1289 break;
1290 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001291
1292 case ISD::INSERT_VECTOR_ELT: {
1293 EVT VT = Node->getValueType(0);
1294 unsigned ElemBitSize = VT.getVectorElementType().getSizeInBits();
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001295 if (ElemBitSize == 32) {
1296 if (tryGather(Node, SystemZ::VGEF))
1297 return;
1298 } else if (ElemBitSize == 64) {
1299 if (tryGather(Node, SystemZ::VGEG))
1300 return;
1301 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001302 break;
1303 }
1304
1305 case ISD::STORE: {
1306 auto *Store = cast<StoreSDNode>(Node);
1307 unsigned ElemBitSize = Store->getValue().getValueType().getSizeInBits();
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001308 if (ElemBitSize == 32) {
1309 if (tryScatter(Store, SystemZ::VSCEF))
1310 return;
1311 } else if (ElemBitSize == 64) {
1312 if (tryScatter(Store, SystemZ::VSCEG))
1313 return;
1314 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001315 break;
1316 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001317 }
1318
Justin Bogner9b34e8a2016-05-13 22:42:08 +00001319 SelectCode(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001320}
1321
1322bool SystemZDAGToDAGISel::
1323SelectInlineAsmMemoryOperand(const SDValue &Op,
Daniel Sanders60f1db02015-03-13 12:45:09 +00001324 unsigned ConstraintID,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001325 std::vector<SDValue> &OutOps) {
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001326 SystemZAddressingMode::AddrForm Form;
1327 SystemZAddressingMode::DispRange DispRange;
Ulrich Weigand79564612016-06-09 15:19:16 +00001328 SDValue Base, Disp, Index;
1329
Daniel Sanders2eeace22015-03-17 16:16:14 +00001330 switch(ConstraintID) {
1331 default:
1332 llvm_unreachable("Unexpected asm memory constraint");
1333 case InlineAsm::Constraint_i:
Daniel Sanders2eeace22015-03-17 16:16:14 +00001334 case InlineAsm::Constraint_Q:
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001335 // Accept an address with a short displacement, but no index.
1336 Form = SystemZAddressingMode::FormBD;
1337 DispRange = SystemZAddressingMode::Disp12Only;
1338 break;
Daniel Sanders2eeace22015-03-17 16:16:14 +00001339 case InlineAsm::Constraint_R:
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001340 // Accept an address with a short displacement and an index.
1341 Form = SystemZAddressingMode::FormBDXNormal;
1342 DispRange = SystemZAddressingMode::Disp12Only;
Daniel Sanders2eeace22015-03-17 16:16:14 +00001343 break;
Ulrich Weigand79564612016-06-09 15:19:16 +00001344 case InlineAsm::Constraint_S:
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001345 // Accept an address with a long displacement, but no index.
1346 Form = SystemZAddressingMode::FormBD;
1347 DispRange = SystemZAddressingMode::Disp20Only;
1348 break;
Ulrich Weigand79564612016-06-09 15:19:16 +00001349 case InlineAsm::Constraint_T:
1350 case InlineAsm::Constraint_m:
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001351 // Accept an address with a long displacement and an index.
1352 // m works the same as T, as this is the most general case.
1353 Form = SystemZAddressingMode::FormBDXNormal;
1354 DispRange = SystemZAddressingMode::Disp20Only;
Ulrich Weigand79564612016-06-09 15:19:16 +00001355 break;
Daniel Sanders2eeace22015-03-17 16:16:14 +00001356 }
Ulrich Weiganddaae87aa2016-06-13 14:24:05 +00001357
1358 if (selectBDXAddr(Form, DispRange, Op, Base, Disp, Index)) {
1359 OutOps.push_back(Base);
1360 OutOps.push_back(Disp);
1361 OutOps.push_back(Index);
1362 return false;
1363 }
1364
Daniel Sanders2eeace22015-03-17 16:16:14 +00001365 return true;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001366}