blob: cd0cac69c9bee4929116919937c747239e249d52 [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) {
99 return Count == 0 ? 0 : (uint64_t(1) << (Count - 1) << 1) - 1;
100}
101
Richard Sandiford51093212013-07-18 10:40:35 +0000102// Represents operands 2 to 5 of the ROTATE AND ... SELECTED BITS operation
103// given by Opcode. The operands are: Input (R2), Start (I3), End (I4) and
104// Rotate (I5). The combined operand value is effectively:
105//
106// (or (rotl Input, Rotate), ~Mask)
107//
108// for RNSBG and:
109//
110// (and (rotl Input, Rotate), Mask)
111//
Richard Sandiford3e382972013-10-16 13:35:13 +0000112// otherwise. The output value has BitSize bits, although Input may be
113// narrower (in which case the upper bits are don't care).
Richard Sandiford5cbac962013-07-18 09:45:08 +0000114struct RxSBGOperands {
Richard Sandiford51093212013-07-18 10:40:35 +0000115 RxSBGOperands(unsigned Op, SDValue N)
116 : Opcode(Op), BitSize(N.getValueType().getSizeInBits()),
117 Mask(allOnes(BitSize)), Input(N), Start(64 - BitSize), End(63),
118 Rotate(0) {}
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000119
Richard Sandiford51093212013-07-18 10:40:35 +0000120 unsigned Opcode;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000121 unsigned BitSize;
122 uint64_t Mask;
123 SDValue Input;
124 unsigned Start;
125 unsigned End;
126 unsigned Rotate;
127};
128
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000129class SystemZDAGToDAGISel : public SelectionDAGISel {
Eric Christophera6734172015-01-31 00:06:45 +0000130 const SystemZSubtarget *Subtarget;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000131
132 // Used by SystemZOperands.td to create integer constants.
Richard Sandiford54b36912013-09-27 15:14:04 +0000133 inline SDValue getImm(const SDNode *Node, uint64_t Imm) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000134 return CurDAG->getTargetConstant(Imm, Node->getValueType(0));
135 }
136
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000137 const SystemZTargetMachine &getTargetMachine() const {
138 return static_cast<const SystemZTargetMachine &>(TM);
139 }
140
141 const SystemZInstrInfo *getInstrInfo() const {
Eric Christophera6734172015-01-31 00:06:45 +0000142 return Subtarget->getInstrInfo();
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000143 }
144
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000145 // Try to fold more of the base or index of AM into AM, where IsBase
146 // selects between the base and index.
Richard Sandiford54b36912013-09-27 15:14:04 +0000147 bool expandAddress(SystemZAddressingMode &AM, bool IsBase) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000148
149 // Try to describe N in AM, returning true on success.
Richard Sandiford54b36912013-09-27 15:14:04 +0000150 bool selectAddress(SDValue N, SystemZAddressingMode &AM) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000151
152 // Extract individual target operands from matched address AM.
153 void getAddressOperands(const SystemZAddressingMode &AM, EVT VT,
Richard Sandiford54b36912013-09-27 15:14:04 +0000154 SDValue &Base, SDValue &Disp) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000155 void getAddressOperands(const SystemZAddressingMode &AM, EVT VT,
Richard Sandiford54b36912013-09-27 15:14:04 +0000156 SDValue &Base, SDValue &Disp, SDValue &Index) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000157
158 // Try to match Addr as a FormBD address with displacement type DR.
159 // Return true on success, storing the base and displacement in
160 // Base and Disp respectively.
161 bool selectBDAddr(SystemZAddressingMode::DispRange DR, SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000162 SDValue &Base, SDValue &Disp) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000163
Richard Sandiforda481f582013-08-23 11:18:53 +0000164 // Try to match Addr as a FormBDX address with displacement type DR.
165 // Return true on success and if the result had no index. Store the
166 // base and displacement in Base and Disp respectively.
167 bool selectMVIAddr(SystemZAddressingMode::DispRange DR, SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000168 SDValue &Base, SDValue &Disp) const;
Richard Sandiforda481f582013-08-23 11:18:53 +0000169
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000170 // Try to match Addr as a FormBDX* address of form Form with
171 // displacement type DR. Return true on success, storing the base,
172 // displacement and index in Base, Disp and Index respectively.
173 bool selectBDXAddr(SystemZAddressingMode::AddrForm Form,
174 SystemZAddressingMode::DispRange DR, SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000175 SDValue &Base, SDValue &Disp, SDValue &Index) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000176
177 // PC-relative address matching routines used by SystemZOperands.td.
Richard Sandiford54b36912013-09-27 15:14:04 +0000178 bool selectPCRelAddress(SDValue Addr, SDValue &Target) const {
179 if (SystemZISD::isPCREL(Addr.getOpcode())) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000180 Target = Addr.getOperand(0);
181 return true;
182 }
183 return false;
184 }
185
186 // BD matching routines used by SystemZOperands.td.
Richard Sandiford54b36912013-09-27 15:14:04 +0000187 bool selectBDAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000188 return selectBDAddr(SystemZAddressingMode::Disp12Only, Addr, Base, Disp);
189 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000190 bool selectBDAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000191 return selectBDAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp);
192 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000193 bool selectBDAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000194 return selectBDAddr(SystemZAddressingMode::Disp20Only, Addr, Base, Disp);
195 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000196 bool selectBDAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000197 return selectBDAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp);
198 }
199
Richard Sandiforda481f582013-08-23 11:18:53 +0000200 // MVI matching routines used by SystemZOperands.td.
Richard Sandiford54b36912013-09-27 15:14:04 +0000201 bool selectMVIAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Richard Sandiforda481f582013-08-23 11:18:53 +0000202 return selectMVIAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp);
203 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000204 bool selectMVIAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Richard Sandiforda481f582013-08-23 11:18:53 +0000205 return selectMVIAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp);
206 }
207
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000208 // BDX matching routines used by SystemZOperands.td.
209 bool selectBDXAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000210 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000211 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
212 SystemZAddressingMode::Disp12Only,
213 Addr, Base, Disp, Index);
214 }
215 bool selectBDXAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000216 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000217 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
218 SystemZAddressingMode::Disp12Pair,
219 Addr, Base, Disp, Index);
220 }
221 bool selectDynAlloc12Only(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000222 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000223 return selectBDXAddr(SystemZAddressingMode::FormBDXDynAlloc,
224 SystemZAddressingMode::Disp12Only,
225 Addr, Base, Disp, Index);
226 }
227 bool selectBDXAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000228 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000229 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
230 SystemZAddressingMode::Disp20Only,
231 Addr, Base, Disp, Index);
232 }
233 bool selectBDXAddr20Only128(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000234 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000235 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
236 SystemZAddressingMode::Disp20Only128,
237 Addr, Base, Disp, Index);
238 }
239 bool selectBDXAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000240 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000241 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
242 SystemZAddressingMode::Disp20Pair,
243 Addr, Base, Disp, Index);
244 }
245 bool selectLAAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000246 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000247 return selectBDXAddr(SystemZAddressingMode::FormBDXLA,
248 SystemZAddressingMode::Disp12Pair,
249 Addr, Base, Disp, Index);
250 }
251 bool selectLAAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000252 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000253 return selectBDXAddr(SystemZAddressingMode::FormBDXLA,
254 SystemZAddressingMode::Disp20Pair,
255 Addr, Base, Disp, Index);
256 }
257
Richard Sandiford885140c2013-07-16 11:55:57 +0000258 // Check whether (or Op (and X InsertMask)) is effectively an insertion
259 // of X into bits InsertMask of some Y != Op. Return true if so and
260 // set Op to that Y.
Richard Sandiford54b36912013-09-27 15:14:04 +0000261 bool detectOrAndInsertion(SDValue &Op, uint64_t InsertMask) const;
Richard Sandiford885140c2013-07-16 11:55:57 +0000262
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000263 // Try to update RxSBG so that only the bits of RxSBG.Input in Mask are used.
264 // Return true on success.
Richard Sandiford54b36912013-09-27 15:14:04 +0000265 bool refineRxSBGMask(RxSBGOperands &RxSBG, uint64_t Mask) const;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000266
Richard Sandiford5cbac962013-07-18 09:45:08 +0000267 // Try to fold some of RxSBG.Input into other fields of RxSBG.
268 // Return true on success.
Richard Sandiford54b36912013-09-27 15:14:04 +0000269 bool expandRxSBG(RxSBGOperands &RxSBG) const;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000270
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000271 // Return an undefined value of type VT.
272 SDValue getUNDEF(SDLoc DL, EVT VT) const;
Richard Sandiford84f54a32013-07-11 08:59:12 +0000273
274 // Convert N to VT, if it isn't already.
Richard Sandiford54b36912013-09-27 15:14:04 +0000275 SDValue convertTo(SDLoc DL, EVT VT, SDValue N) const;
Richard Sandiford84f54a32013-07-11 08:59:12 +0000276
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000277 // Try to implement AND or shift node N using RISBG with the zero flag set.
278 // Return the selected node on success, otherwise return null.
279 SDNode *tryRISBGZero(SDNode *N);
Richard Sandiford84f54a32013-07-11 08:59:12 +0000280
Richard Sandiford7878b852013-07-18 10:06:15 +0000281 // Try to use RISBG or Opcode to implement OR or XOR node N.
282 // Return the selected node on success, otherwise return null.
283 SDNode *tryRxSBG(SDNode *N, unsigned Opcode);
Richard Sandiford885140c2013-07-16 11:55:57 +0000284
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000285 // If Op0 is null, then Node is a constant that can be loaded using:
286 //
287 // (Opcode UpperVal LowerVal)
288 //
289 // If Op0 is nonnull, then Node can be implemented using:
290 //
291 // (Opcode (Opcode Op0 UpperVal) LowerVal)
292 SDNode *splitLargeImmediate(unsigned Opcode, SDNode *Node, SDValue Op0,
293 uint64_t UpperVal, uint64_t LowerVal);
294
Richard Sandiford067817e2013-09-27 15:29:20 +0000295 // Return true if Load and Store are loads and stores of the same size
296 // and are guaranteed not to overlap. Such operations can be implemented
297 // using block (SS-format) instructions.
298 //
299 // Partial overlap would lead to incorrect code, since the block operations
300 // are logically bytewise, even though they have a fast path for the
301 // non-overlapping case. We also need to avoid full overlap (i.e. two
302 // addresses that might be equal at run time) because although that case
303 // would be handled correctly, it might be implemented by millicode.
304 bool canUseBlockOperation(StoreSDNode *Store, LoadSDNode *Load) const;
305
Richard Sandiford178273a2013-09-05 10:36:45 +0000306 // N is a (store (load Y), X) pattern. Return true if it can use an MVC
307 // from Y to X.
Richard Sandiford97846492013-07-09 09:46:39 +0000308 bool storeLoadCanUseMVC(SDNode *N) const;
309
Richard Sandiford178273a2013-09-05 10:36:45 +0000310 // N is a (store (op (load A[0]), (load A[1])), X) pattern. Return true
311 // if A[1 - I] == X and if N can use a block operation like NC from A[I]
312 // to X.
313 bool storeLoadCanUseBlockBinary(SDNode *N, unsigned I) const;
314
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000315public:
316 SystemZDAGToDAGISel(SystemZTargetMachine &TM, CodeGenOpt::Level OptLevel)
Eric Christophera6734172015-01-31 00:06:45 +0000317 : SelectionDAGISel(TM, OptLevel) {}
318
319 bool runOnMachineFunction(MachineFunction &MF) override {
320 Subtarget = &MF.getSubtarget<SystemZSubtarget>();
321 return SelectionDAGISel::runOnMachineFunction(MF);
322 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000323
324 // Override MachineFunctionPass.
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000325 const char *getPassName() const override {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000326 return "SystemZ DAG->DAG Pattern Instruction Selection";
327 }
328
329 // Override SelectionDAGISel.
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000330 SDNode *Select(SDNode *Node) override;
Daniel Sanders60f1db02015-03-13 12:45:09 +0000331 bool SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000332 std::vector<SDValue> &OutOps) override;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000333
334 // Include the pieces autogenerated from the target description.
335 #include "SystemZGenDAGISel.inc"
336};
337} // end anonymous namespace
338
339FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM,
340 CodeGenOpt::Level OptLevel) {
341 return new SystemZDAGToDAGISel(TM, OptLevel);
342}
343
344// Return true if Val should be selected as a displacement for an address
345// with range DR. Here we're interested in the range of both the instruction
346// described by DR and of any pairing instruction.
347static bool selectDisp(SystemZAddressingMode::DispRange DR, int64_t Val) {
348 switch (DR) {
349 case SystemZAddressingMode::Disp12Only:
350 return isUInt<12>(Val);
351
352 case SystemZAddressingMode::Disp12Pair:
353 case SystemZAddressingMode::Disp20Only:
354 case SystemZAddressingMode::Disp20Pair:
355 return isInt<20>(Val);
356
357 case SystemZAddressingMode::Disp20Only128:
358 return isInt<20>(Val) && isInt<20>(Val + 8);
359 }
360 llvm_unreachable("Unhandled displacement range");
361}
362
363// Change the base or index in AM to Value, where IsBase selects
364// between the base and index.
365static void changeComponent(SystemZAddressingMode &AM, bool IsBase,
366 SDValue Value) {
367 if (IsBase)
368 AM.Base = Value;
369 else
370 AM.Index = Value;
371}
372
373// The base or index of AM is equivalent to Value + ADJDYNALLOC,
374// where IsBase selects between the base and index. Try to fold the
375// ADJDYNALLOC into AM.
376static bool expandAdjDynAlloc(SystemZAddressingMode &AM, bool IsBase,
377 SDValue Value) {
378 if (AM.isDynAlloc() && !AM.IncludesDynAlloc) {
379 changeComponent(AM, IsBase, Value);
380 AM.IncludesDynAlloc = true;
381 return true;
382 }
383 return false;
384}
385
386// The base of AM is equivalent to Base + Index. Try to use Index as
387// the index register.
388static bool expandIndex(SystemZAddressingMode &AM, SDValue Base,
389 SDValue Index) {
390 if (AM.hasIndexField() && !AM.Index.getNode()) {
391 AM.Base = Base;
392 AM.Index = Index;
393 return true;
394 }
395 return false;
396}
397
398// The base or index of AM is equivalent to Op0 + Op1, where IsBase selects
399// between the base and index. Try to fold Op1 into AM's displacement.
400static bool expandDisp(SystemZAddressingMode &AM, bool IsBase,
Richard Sandiford54b36912013-09-27 15:14:04 +0000401 SDValue Op0, uint64_t Op1) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000402 // First try adjusting the displacement.
Richard Sandiford54b36912013-09-27 15:14:04 +0000403 int64_t TestDisp = AM.Disp + Op1;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000404 if (selectDisp(AM.DR, TestDisp)) {
405 changeComponent(AM, IsBase, Op0);
406 AM.Disp = TestDisp;
407 return true;
408 }
409
410 // We could consider forcing the displacement into a register and
411 // using it as an index, but it would need to be carefully tuned.
412 return false;
413}
414
415bool SystemZDAGToDAGISel::expandAddress(SystemZAddressingMode &AM,
Richard Sandiford54b36912013-09-27 15:14:04 +0000416 bool IsBase) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000417 SDValue N = IsBase ? AM.Base : AM.Index;
418 unsigned Opcode = N.getOpcode();
419 if (Opcode == ISD::TRUNCATE) {
420 N = N.getOperand(0);
421 Opcode = N.getOpcode();
422 }
423 if (Opcode == ISD::ADD || CurDAG->isBaseWithConstantOffset(N)) {
424 SDValue Op0 = N.getOperand(0);
425 SDValue Op1 = N.getOperand(1);
426
427 unsigned Op0Code = Op0->getOpcode();
428 unsigned Op1Code = Op1->getOpcode();
429
430 if (Op0Code == SystemZISD::ADJDYNALLOC)
431 return expandAdjDynAlloc(AM, IsBase, Op1);
432 if (Op1Code == SystemZISD::ADJDYNALLOC)
433 return expandAdjDynAlloc(AM, IsBase, Op0);
434
435 if (Op0Code == ISD::Constant)
Richard Sandiford54b36912013-09-27 15:14:04 +0000436 return expandDisp(AM, IsBase, Op1,
437 cast<ConstantSDNode>(Op0)->getSExtValue());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000438 if (Op1Code == ISD::Constant)
Richard Sandiford54b36912013-09-27 15:14:04 +0000439 return expandDisp(AM, IsBase, Op0,
440 cast<ConstantSDNode>(Op1)->getSExtValue());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000441
442 if (IsBase && expandIndex(AM, Op0, Op1))
443 return true;
444 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000445 if (Opcode == SystemZISD::PCREL_OFFSET) {
446 SDValue Full = N.getOperand(0);
447 SDValue Base = N.getOperand(1);
448 SDValue Anchor = Base.getOperand(0);
449 uint64_t Offset = (cast<GlobalAddressSDNode>(Full)->getOffset() -
450 cast<GlobalAddressSDNode>(Anchor)->getOffset());
451 return expandDisp(AM, IsBase, Base, Offset);
452 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000453 return false;
454}
455
456// Return true if an instruction with displacement range DR should be
457// used for displacement value Val. selectDisp(DR, Val) must already hold.
458static bool isValidDisp(SystemZAddressingMode::DispRange DR, int64_t Val) {
459 assert(selectDisp(DR, Val) && "Invalid displacement");
460 switch (DR) {
461 case SystemZAddressingMode::Disp12Only:
462 case SystemZAddressingMode::Disp20Only:
463 case SystemZAddressingMode::Disp20Only128:
464 return true;
465
466 case SystemZAddressingMode::Disp12Pair:
467 // Use the other instruction if the displacement is too large.
468 return isUInt<12>(Val);
469
470 case SystemZAddressingMode::Disp20Pair:
471 // Use the other instruction if the displacement is small enough.
472 return !isUInt<12>(Val);
473 }
474 llvm_unreachable("Unhandled displacement range");
475}
476
477// Return true if Base + Disp + Index should be performed by LA(Y).
478static bool shouldUseLA(SDNode *Base, int64_t Disp, SDNode *Index) {
479 // Don't use LA(Y) for constants.
480 if (!Base)
481 return false;
482
483 // Always use LA(Y) for frame addresses, since we know that the destination
484 // register is almost always (perhaps always) going to be different from
485 // the frame register.
486 if (Base->getOpcode() == ISD::FrameIndex)
487 return true;
488
489 if (Disp) {
490 // Always use LA(Y) if there is a base, displacement and index.
491 if (Index)
492 return true;
493
494 // Always use LA if the displacement is small enough. It should always
495 // be no worse than AGHI (and better if it avoids a move).
496 if (isUInt<12>(Disp))
497 return true;
498
499 // For similar reasons, always use LAY if the constant is too big for AGHI.
500 // LAY should be no worse than AGFI.
501 if (!isInt<16>(Disp))
502 return true;
503 } else {
504 // Don't use LA for plain registers.
505 if (!Index)
506 return false;
507
508 // Don't use LA for plain addition if the index operand is only used
509 // once. It should be a natural two-operand addition in that case.
510 if (Index->hasOneUse())
511 return false;
512
513 // Prefer addition if the second operation is sign-extended, in the
514 // hope of using AGF.
515 unsigned IndexOpcode = Index->getOpcode();
516 if (IndexOpcode == ISD::SIGN_EXTEND ||
517 IndexOpcode == ISD::SIGN_EXTEND_INREG)
518 return false;
519 }
520
521 // Don't use LA for two-operand addition if either operand is only
522 // used once. The addition instructions are better in that case.
523 if (Base->hasOneUse())
524 return false;
525
526 return true;
527}
528
529// Return true if Addr is suitable for AM, updating AM if so.
530bool SystemZDAGToDAGISel::selectAddress(SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000531 SystemZAddressingMode &AM) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000532 // Start out assuming that the address will need to be loaded separately,
533 // then try to extend it as much as we can.
534 AM.Base = Addr;
535
536 // First try treating the address as a constant.
537 if (Addr.getOpcode() == ISD::Constant &&
Richard Sandiford54b36912013-09-27 15:14:04 +0000538 expandDisp(AM, true, SDValue(),
539 cast<ConstantSDNode>(Addr)->getSExtValue()))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000540 ;
541 else
542 // Otherwise try expanding each component.
543 while (expandAddress(AM, true) ||
544 (AM.Index.getNode() && expandAddress(AM, false)))
545 continue;
546
547 // Reject cases where it isn't profitable to use LA(Y).
548 if (AM.Form == SystemZAddressingMode::FormBDXLA &&
549 !shouldUseLA(AM.Base.getNode(), AM.Disp, AM.Index.getNode()))
550 return false;
551
552 // Reject cases where the other instruction in a pair should be used.
553 if (!isValidDisp(AM.DR, AM.Disp))
554 return false;
555
556 // Make sure that ADJDYNALLOC is included where necessary.
557 if (AM.isDynAlloc() && !AM.IncludesDynAlloc)
558 return false;
559
560 DEBUG(AM.dump());
561 return true;
562}
563
564// Insert a node into the DAG at least before Pos. This will reposition
565// the node as needed, and will assign it a node ID that is <= Pos's ID.
566// Note that this does *not* preserve the uniqueness of node IDs!
567// The selection DAG must no longer depend on their uniqueness when this
568// function is used.
569static void insertDAGNode(SelectionDAG *DAG, SDNode *Pos, SDValue N) {
570 if (N.getNode()->getNodeId() == -1 ||
571 N.getNode()->getNodeId() > Pos->getNodeId()) {
572 DAG->RepositionNode(Pos, N.getNode());
573 N.getNode()->setNodeId(Pos->getNodeId());
574 }
575}
576
577void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
578 EVT VT, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000579 SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000580 Base = AM.Base;
581 if (!Base.getNode())
582 // Register 0 means "no base". This is mostly useful for shifts.
583 Base = CurDAG->getRegister(0, VT);
584 else if (Base.getOpcode() == ISD::FrameIndex) {
585 // Lower a FrameIndex to a TargetFrameIndex.
586 int64_t FrameIndex = cast<FrameIndexSDNode>(Base)->getIndex();
587 Base = CurDAG->getTargetFrameIndex(FrameIndex, VT);
588 } else if (Base.getValueType() != VT) {
589 // Truncate values from i64 to i32, for shifts.
590 assert(VT == MVT::i32 && Base.getValueType() == MVT::i64 &&
591 "Unexpected truncation");
Andrew Trickef9de2a2013-05-25 02:42:55 +0000592 SDLoc DL(Base);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000593 SDValue Trunc = CurDAG->getNode(ISD::TRUNCATE, DL, VT, Base);
594 insertDAGNode(CurDAG, Base.getNode(), Trunc);
595 Base = Trunc;
596 }
597
598 // Lower the displacement to a TargetConstant.
599 Disp = CurDAG->getTargetConstant(AM.Disp, VT);
600}
601
602void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
603 EVT VT, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000604 SDValue &Disp,
605 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000606 getAddressOperands(AM, VT, Base, Disp);
607
608 Index = AM.Index;
609 if (!Index.getNode())
610 // Register 0 means "no index".
611 Index = CurDAG->getRegister(0, VT);
612}
613
614bool SystemZDAGToDAGISel::selectBDAddr(SystemZAddressingMode::DispRange DR,
615 SDValue Addr, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000616 SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000617 SystemZAddressingMode AM(SystemZAddressingMode::FormBD, DR);
618 if (!selectAddress(Addr, AM))
619 return false;
620
621 getAddressOperands(AM, Addr.getValueType(), Base, Disp);
622 return true;
623}
624
Richard Sandiforda481f582013-08-23 11:18:53 +0000625bool SystemZDAGToDAGISel::selectMVIAddr(SystemZAddressingMode::DispRange DR,
626 SDValue Addr, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000627 SDValue &Disp) const {
Richard Sandiforda481f582013-08-23 11:18:53 +0000628 SystemZAddressingMode AM(SystemZAddressingMode::FormBDXNormal, DR);
629 if (!selectAddress(Addr, AM) || AM.Index.getNode())
630 return false;
631
632 getAddressOperands(AM, Addr.getValueType(), Base, Disp);
633 return true;
634}
635
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000636bool SystemZDAGToDAGISel::selectBDXAddr(SystemZAddressingMode::AddrForm Form,
637 SystemZAddressingMode::DispRange DR,
638 SDValue Addr, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000639 SDValue &Disp, SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000640 SystemZAddressingMode AM(Form, DR);
641 if (!selectAddress(Addr, AM))
642 return false;
643
644 getAddressOperands(AM, Addr.getValueType(), Base, Disp, Index);
645 return true;
646}
647
Richard Sandiford885140c2013-07-16 11:55:57 +0000648bool SystemZDAGToDAGISel::detectOrAndInsertion(SDValue &Op,
Richard Sandiford54b36912013-09-27 15:14:04 +0000649 uint64_t InsertMask) const {
Richard Sandiford885140c2013-07-16 11:55:57 +0000650 // We're only interested in cases where the insertion is into some operand
651 // of Op, rather than into Op itself. The only useful case is an AND.
652 if (Op.getOpcode() != ISD::AND)
653 return false;
654
655 // We need a constant mask.
Richard Sandiford21f5d682014-03-06 11:22:58 +0000656 auto *MaskNode = dyn_cast<ConstantSDNode>(Op.getOperand(1).getNode());
Richard Sandiford885140c2013-07-16 11:55:57 +0000657 if (!MaskNode)
658 return false;
659
660 // It's not an insertion of Op.getOperand(0) if the two masks overlap.
661 uint64_t AndMask = MaskNode->getZExtValue();
662 if (InsertMask & AndMask)
663 return false;
664
665 // It's only an insertion if all bits are covered or are known to be zero.
666 // The inner check covers all cases but is more expensive.
667 uint64_t Used = allOnes(Op.getValueType().getSizeInBits());
668 if (Used != (AndMask | InsertMask)) {
669 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +0000670 CurDAG->computeKnownBits(Op.getOperand(0), KnownZero, KnownOne);
Richard Sandiford885140c2013-07-16 11:55:57 +0000671 if (Used != (AndMask | InsertMask | KnownZero.getZExtValue()))
672 return false;
673 }
674
675 Op = Op.getOperand(0);
676 return true;
677}
678
Richard Sandiford54b36912013-09-27 15:14:04 +0000679bool SystemZDAGToDAGISel::refineRxSBGMask(RxSBGOperands &RxSBG,
680 uint64_t Mask) const {
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000681 const SystemZInstrInfo *TII = getInstrInfo();
Richard Sandiford5cbac962013-07-18 09:45:08 +0000682 if (RxSBG.Rotate != 0)
683 Mask = (Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate));
684 Mask &= RxSBG.Mask;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000685 if (TII->isRxSBGMask(Mask, RxSBG.BitSize, RxSBG.Start, RxSBG.End)) {
Richard Sandiford5cbac962013-07-18 09:45:08 +0000686 RxSBG.Mask = Mask;
Richard Sandiford5cbac962013-07-18 09:45:08 +0000687 return true;
688 }
Richard Sandiford84f54a32013-07-11 08:59:12 +0000689 return false;
690}
691
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000692// Return true if any bits of (RxSBG.Input & Mask) are significant.
693static bool maskMatters(RxSBGOperands &RxSBG, uint64_t Mask) {
694 // Rotate the mask in the same way as RxSBG.Input is rotated.
Richard Sandiford297f7d22013-07-18 10:14:55 +0000695 if (RxSBG.Rotate != 0)
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000696 Mask = ((Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate)));
697 return (Mask & RxSBG.Mask) != 0;
Richard Sandiford297f7d22013-07-18 10:14:55 +0000698}
699
Richard Sandiford54b36912013-09-27 15:14:04 +0000700bool SystemZDAGToDAGISel::expandRxSBG(RxSBGOperands &RxSBG) const {
Richard Sandiford5cbac962013-07-18 09:45:08 +0000701 SDValue N = RxSBG.Input;
Richard Sandiford297f7d22013-07-18 10:14:55 +0000702 unsigned Opcode = N.getOpcode();
703 switch (Opcode) {
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000704 case ISD::AND: {
Richard Sandiford51093212013-07-18 10:40:35 +0000705 if (RxSBG.Opcode == SystemZ::RNSBG)
706 return false;
707
Richard Sandiford21f5d682014-03-06 11:22:58 +0000708 auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000709 if (!MaskNode)
710 return false;
711
712 SDValue Input = N.getOperand(0);
713 uint64_t Mask = MaskNode->getZExtValue();
Richard Sandiford5cbac962013-07-18 09:45:08 +0000714 if (!refineRxSBGMask(RxSBG, Mask)) {
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000715 // If some bits of Input are already known zeros, those bits will have
716 // been removed from the mask. See if adding them back in makes the
717 // mask suitable.
718 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +0000719 CurDAG->computeKnownBits(Input, KnownZero, KnownOne);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000720 Mask |= KnownZero.getZExtValue();
Richard Sandiford5cbac962013-07-18 09:45:08 +0000721 if (!refineRxSBGMask(RxSBG, Mask))
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000722 return false;
723 }
Richard Sandiford5cbac962013-07-18 09:45:08 +0000724 RxSBG.Input = Input;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000725 return true;
726 }
727
Richard Sandiford51093212013-07-18 10:40:35 +0000728 case ISD::OR: {
729 if (RxSBG.Opcode != SystemZ::RNSBG)
730 return false;
731
Richard Sandiford21f5d682014-03-06 11:22:58 +0000732 auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford51093212013-07-18 10:40:35 +0000733 if (!MaskNode)
734 return false;
735
736 SDValue Input = N.getOperand(0);
737 uint64_t Mask = ~MaskNode->getZExtValue();
738 if (!refineRxSBGMask(RxSBG, Mask)) {
739 // If some bits of Input are already known ones, those bits will have
740 // been removed from the mask. See if adding them back in makes the
741 // mask suitable.
742 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +0000743 CurDAG->computeKnownBits(Input, KnownZero, KnownOne);
Richard Sandiford51093212013-07-18 10:40:35 +0000744 Mask &= ~KnownOne.getZExtValue();
745 if (!refineRxSBGMask(RxSBG, Mask))
746 return false;
747 }
748 RxSBG.Input = Input;
749 return true;
750 }
751
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000752 case ISD::ROTL: {
Richard Sandiford5cbac962013-07-18 09:45:08 +0000753 // Any 64-bit rotate left can be merged into the RxSBG.
Richard Sandiford3e382972013-10-16 13:35:13 +0000754 if (RxSBG.BitSize != 64 || N.getValueType() != MVT::i64)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000755 return false;
Richard Sandiford21f5d682014-03-06 11:22:58 +0000756 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000757 if (!CountNode)
758 return false;
759
Richard Sandiford5cbac962013-07-18 09:45:08 +0000760 RxSBG.Rotate = (RxSBG.Rotate + CountNode->getZExtValue()) & 63;
761 RxSBG.Input = N.getOperand(0);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000762 return true;
763 }
764
Richard Sandiford220ee492013-12-20 11:49:48 +0000765 case ISD::ANY_EXTEND:
766 // Bits above the extended operand are don't-care.
767 RxSBG.Input = N.getOperand(0);
768 return true;
769
Richard Sandiford3875cb62014-01-09 11:28:53 +0000770 case ISD::ZERO_EXTEND:
771 if (RxSBG.Opcode != SystemZ::RNSBG) {
772 // Restrict the mask to the extended operand.
773 unsigned InnerBitSize = N.getOperand(0).getValueType().getSizeInBits();
774 if (!refineRxSBGMask(RxSBG, allOnes(InnerBitSize)))
775 return false;
Richard Sandiford220ee492013-12-20 11:49:48 +0000776
Richard Sandiford3875cb62014-01-09 11:28:53 +0000777 RxSBG.Input = N.getOperand(0);
778 return true;
779 }
780 // Fall through.
Richard Sandiford220ee492013-12-20 11:49:48 +0000781
782 case ISD::SIGN_EXTEND: {
Richard Sandiford3e382972013-10-16 13:35:13 +0000783 // Check that the extension bits are don't-care (i.e. are masked out
784 // by the final mask).
785 unsigned InnerBitSize = N.getOperand(0).getValueType().getSizeInBits();
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000786 if (maskMatters(RxSBG, allOnes(RxSBG.BitSize) - allOnes(InnerBitSize)))
Richard Sandiford3e382972013-10-16 13:35:13 +0000787 return false;
788
789 RxSBG.Input = N.getOperand(0);
790 return true;
791 }
792
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000793 case ISD::SHL: {
Richard Sandiford21f5d682014-03-06 11:22:58 +0000794 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000795 if (!CountNode)
796 return false;
797
798 uint64_t Count = CountNode->getZExtValue();
Richard Sandiford3e382972013-10-16 13:35:13 +0000799 unsigned BitSize = N.getValueType().getSizeInBits();
800 if (Count < 1 || Count >= BitSize)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000801 return false;
802
Richard Sandiford51093212013-07-18 10:40:35 +0000803 if (RxSBG.Opcode == SystemZ::RNSBG) {
804 // Treat (shl X, count) as (rotl X, size-count) as long as the bottom
805 // count bits from RxSBG.Input are ignored.
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000806 if (maskMatters(RxSBG, allOnes(Count)))
Richard Sandiford51093212013-07-18 10:40:35 +0000807 return false;
808 } else {
809 // Treat (shl X, count) as (and (rotl X, count), ~0<<count).
Richard Sandiford3e382972013-10-16 13:35:13 +0000810 if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count) << Count))
Richard Sandiford51093212013-07-18 10:40:35 +0000811 return false;
812 }
813
Richard Sandiford5cbac962013-07-18 09:45:08 +0000814 RxSBG.Rotate = (RxSBG.Rotate + Count) & 63;
815 RxSBG.Input = N.getOperand(0);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000816 return true;
817 }
818
Richard Sandiford297f7d22013-07-18 10:14:55 +0000819 case ISD::SRL:
820 case ISD::SRA: {
Richard Sandiford21f5d682014-03-06 11:22:58 +0000821 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000822 if (!CountNode)
823 return false;
824
825 uint64_t Count = CountNode->getZExtValue();
Richard Sandiford3e382972013-10-16 13:35:13 +0000826 unsigned BitSize = N.getValueType().getSizeInBits();
827 if (Count < 1 || Count >= BitSize)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000828 return false;
829
Richard Sandiford51093212013-07-18 10:40:35 +0000830 if (RxSBG.Opcode == SystemZ::RNSBG || Opcode == ISD::SRA) {
831 // Treat (srl|sra X, count) as (rotl X, size-count) as long as the top
832 // count bits from RxSBG.Input are ignored.
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000833 if (maskMatters(RxSBG, allOnes(Count) << (BitSize - Count)))
Richard Sandiford297f7d22013-07-18 10:14:55 +0000834 return false;
835 } else {
836 // Treat (srl X, count), mask) as (and (rotl X, size-count), ~0>>count),
837 // which is similar to SLL above.
Richard Sandiford3e382972013-10-16 13:35:13 +0000838 if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count)))
Richard Sandiford297f7d22013-07-18 10:14:55 +0000839 return false;
840 }
841
Richard Sandiford5cbac962013-07-18 09:45:08 +0000842 RxSBG.Rotate = (RxSBG.Rotate - Count) & 63;
843 RxSBG.Input = N.getOperand(0);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000844 return true;
845 }
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000846 default:
847 return false;
848 }
849}
850
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000851SDValue SystemZDAGToDAGISel::getUNDEF(SDLoc DL, EVT VT) const {
852 SDNode *N = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, VT);
Richard Sandiford84f54a32013-07-11 08:59:12 +0000853 return SDValue(N, 0);
854}
855
Richard Sandiford54b36912013-09-27 15:14:04 +0000856SDValue SystemZDAGToDAGISel::convertTo(SDLoc DL, EVT VT, SDValue N) const {
Richard Sandifordd8163202013-09-13 09:12:44 +0000857 if (N.getValueType() == MVT::i32 && VT == MVT::i64)
Richard Sandiford87a44362013-09-30 10:28:35 +0000858 return CurDAG->getTargetInsertSubreg(SystemZ::subreg_l32,
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000859 DL, VT, getUNDEF(DL, MVT::i64), N);
Richard Sandifordd8163202013-09-13 09:12:44 +0000860 if (N.getValueType() == MVT::i64 && VT == MVT::i32)
Richard Sandiford87a44362013-09-30 10:28:35 +0000861 return CurDAG->getTargetExtractSubreg(SystemZ::subreg_l32, DL, VT, N);
Richard Sandiford84f54a32013-07-11 08:59:12 +0000862 assert(N.getValueType() == VT && "Unexpected value types");
863 return N;
864}
865
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000866SDNode *SystemZDAGToDAGISel::tryRISBGZero(SDNode *N) {
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000867 EVT VT = N->getValueType(0);
Richard Sandiford51093212013-07-18 10:40:35 +0000868 RxSBGOperands RISBG(SystemZ::RISBG, SDValue(N, 0));
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000869 unsigned Count = 0;
Richard Sandiford5cbac962013-07-18 09:45:08 +0000870 while (expandRxSBG(RISBG))
Richard Sandiford3e382972013-10-16 13:35:13 +0000871 if (RISBG.Input.getOpcode() != ISD::ANY_EXTEND)
872 Count += 1;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000873 if (Count == 0)
Craig Topper062a2ba2014-04-25 05:30:21 +0000874 return nullptr;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000875 if (Count == 1) {
876 // Prefer to use normal shift instructions over RISBG, since they can handle
877 // all cases and are sometimes shorter.
878 if (N->getOpcode() != ISD::AND)
Craig Topper062a2ba2014-04-25 05:30:21 +0000879 return nullptr;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000880
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000881 // Prefer register extensions like LLC over RISBG. Also prefer to start
882 // out with normal ANDs if one instruction would be enough. We can convert
883 // these ANDs into an RISBG later if a three-address instruction is useful.
884 if (VT == MVT::i32 ||
885 RISBG.Mask == 0xff ||
886 RISBG.Mask == 0xffff ||
887 SystemZ::isImmLF(~RISBG.Mask) ||
888 SystemZ::isImmHF(~RISBG.Mask)) {
889 // Force the new mask into the DAG, since it may include known-one bits.
Richard Sandiford21f5d682014-03-06 11:22:58 +0000890 auto *MaskN = cast<ConstantSDNode>(N->getOperand(1).getNode());
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000891 if (MaskN->getZExtValue() != RISBG.Mask) {
892 SDValue NewMask = CurDAG->getConstant(RISBG.Mask, VT);
893 N = CurDAG->UpdateNodeOperands(N, N->getOperand(0), NewMask);
894 return SelectCode(N);
895 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000896 return nullptr;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000897 }
898 }
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000899
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000900 unsigned Opcode = SystemZ::RISBG;
901 EVT OpcodeVT = MVT::i64;
Eric Christophera6734172015-01-31 00:06:45 +0000902 if (VT == MVT::i32 && Subtarget->hasHighWord()) {
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000903 Opcode = SystemZ::RISBMux;
904 OpcodeVT = MVT::i32;
905 RISBG.Start &= 31;
906 RISBG.End &= 31;
907 }
Richard Sandiford84f54a32013-07-11 08:59:12 +0000908 SDValue Ops[5] = {
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000909 getUNDEF(SDLoc(N), OpcodeVT),
910 convertTo(SDLoc(N), OpcodeVT, RISBG.Input),
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000911 CurDAG->getTargetConstant(RISBG.Start, MVT::i32),
912 CurDAG->getTargetConstant(RISBG.End | 128, MVT::i32),
913 CurDAG->getTargetConstant(RISBG.Rotate, MVT::i32)
Richard Sandiford84f54a32013-07-11 08:59:12 +0000914 };
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000915 N = CurDAG->getMachineNode(Opcode, SDLoc(N), OpcodeVT, Ops);
Richard Sandiford84f54a32013-07-11 08:59:12 +0000916 return convertTo(SDLoc(N), VT, SDValue(N, 0)).getNode();
917}
918
Richard Sandiford7878b852013-07-18 10:06:15 +0000919SDNode *SystemZDAGToDAGISel::tryRxSBG(SDNode *N, unsigned Opcode) {
920 // Try treating each operand of N as the second operand of the RxSBG
Richard Sandiford885140c2013-07-16 11:55:57 +0000921 // and see which goes deepest.
Richard Sandiford51093212013-07-18 10:40:35 +0000922 RxSBGOperands RxSBG[] = {
923 RxSBGOperands(Opcode, N->getOperand(0)),
924 RxSBGOperands(Opcode, N->getOperand(1))
925 };
Richard Sandiford885140c2013-07-16 11:55:57 +0000926 unsigned Count[] = { 0, 0 };
927 for (unsigned I = 0; I < 2; ++I)
Richard Sandiford5cbac962013-07-18 09:45:08 +0000928 while (expandRxSBG(RxSBG[I]))
Richard Sandiford3e382972013-10-16 13:35:13 +0000929 if (RxSBG[I].Input.getOpcode() != ISD::ANY_EXTEND)
930 Count[I] += 1;
Richard Sandiford885140c2013-07-16 11:55:57 +0000931
932 // Do nothing if neither operand is suitable.
933 if (Count[0] == 0 && Count[1] == 0)
Craig Topper062a2ba2014-04-25 05:30:21 +0000934 return nullptr;
Richard Sandiford885140c2013-07-16 11:55:57 +0000935
936 // Pick the deepest second operand.
937 unsigned I = Count[0] > Count[1] ? 0 : 1;
938 SDValue Op0 = N->getOperand(I ^ 1);
939
940 // Prefer IC for character insertions from memory.
Richard Sandiford7878b852013-07-18 10:06:15 +0000941 if (Opcode == SystemZ::ROSBG && (RxSBG[I].Mask & 0xff) == 0)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000942 if (auto *Load = dyn_cast<LoadSDNode>(Op0.getNode()))
Richard Sandiford885140c2013-07-16 11:55:57 +0000943 if (Load->getMemoryVT() == MVT::i8)
Craig Topper062a2ba2014-04-25 05:30:21 +0000944 return nullptr;
Richard Sandiford885140c2013-07-16 11:55:57 +0000945
946 // See whether we can avoid an AND in the first operand by converting
947 // ROSBG to RISBG.
Richard Sandiford7878b852013-07-18 10:06:15 +0000948 if (Opcode == SystemZ::ROSBG && detectOrAndInsertion(Op0, RxSBG[I].Mask))
Richard Sandiford885140c2013-07-16 11:55:57 +0000949 Opcode = SystemZ::RISBG;
950
951 EVT VT = N->getValueType(0);
952 SDValue Ops[5] = {
953 convertTo(SDLoc(N), MVT::i64, Op0),
Richard Sandiford5cbac962013-07-18 09:45:08 +0000954 convertTo(SDLoc(N), MVT::i64, RxSBG[I].Input),
955 CurDAG->getTargetConstant(RxSBG[I].Start, MVT::i32),
956 CurDAG->getTargetConstant(RxSBG[I].End, MVT::i32),
957 CurDAG->getTargetConstant(RxSBG[I].Rotate, MVT::i32)
Richard Sandiford885140c2013-07-16 11:55:57 +0000958 };
959 N = CurDAG->getMachineNode(Opcode, SDLoc(N), MVT::i64, Ops);
960 return convertTo(SDLoc(N), VT, SDValue(N, 0)).getNode();
961}
962
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000963SDNode *SystemZDAGToDAGISel::splitLargeImmediate(unsigned Opcode, SDNode *Node,
964 SDValue Op0, uint64_t UpperVal,
965 uint64_t LowerVal) {
966 EVT VT = Node->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000967 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000968 SDValue Upper = CurDAG->getConstant(UpperVal, VT);
969 if (Op0.getNode())
970 Upper = CurDAG->getNode(Opcode, DL, VT, Op0, Upper);
971 Upper = SDValue(Select(Upper.getNode()), 0);
972
973 SDValue Lower = CurDAG->getConstant(LowerVal, VT);
974 SDValue Or = CurDAG->getNode(Opcode, DL, VT, Upper, Lower);
975 return Or.getNode();
976}
977
Richard Sandiford067817e2013-09-27 15:29:20 +0000978bool SystemZDAGToDAGISel::canUseBlockOperation(StoreSDNode *Store,
979 LoadSDNode *Load) const {
Richard Sandiford178273a2013-09-05 10:36:45 +0000980 // Check that the two memory operands have the same size.
981 if (Load->getMemoryVT() != Store->getMemoryVT())
Richard Sandiford97846492013-07-09 09:46:39 +0000982 return false;
983
Richard Sandiford178273a2013-09-05 10:36:45 +0000984 // Volatility stops an access from being decomposed.
985 if (Load->isVolatile() || Store->isVolatile())
986 return false;
Richard Sandiford97846492013-07-09 09:46:39 +0000987
988 // There's no chance of overlap if the load is invariant.
989 if (Load->isInvariant())
990 return true;
991
Richard Sandiford97846492013-07-09 09:46:39 +0000992 // Otherwise we need to check whether there's an alias.
Nick Lewyckyaad475b2014-04-15 07:22:52 +0000993 const Value *V1 = Load->getMemOperand()->getValue();
994 const Value *V2 = Store->getMemOperand()->getValue();
Richard Sandiford97846492013-07-09 09:46:39 +0000995 if (!V1 || !V2)
996 return false;
997
Richard Sandiford067817e2013-09-27 15:29:20 +0000998 // Reject equality.
999 uint64_t Size = Load->getMemoryVT().getStoreSize();
Richard Sandiford97846492013-07-09 09:46:39 +00001000 int64_t End1 = Load->getSrcValueOffset() + Size;
1001 int64_t End2 = Store->getSrcValueOffset() + Size;
Richard Sandiford067817e2013-09-27 15:29:20 +00001002 if (V1 == V2 && End1 == End2)
1003 return false;
1004
Hal Finkelcc39b672014-07-24 12:16:19 +00001005 return !AA->alias(AliasAnalysis::Location(V1, End1, Load->getAAInfo()),
1006 AliasAnalysis::Location(V2, End2, Store->getAAInfo()));
Richard Sandiford97846492013-07-09 09:46:39 +00001007}
1008
Richard Sandiford178273a2013-09-05 10:36:45 +00001009bool SystemZDAGToDAGISel::storeLoadCanUseMVC(SDNode *N) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001010 auto *Store = cast<StoreSDNode>(N);
1011 auto *Load = cast<LoadSDNode>(Store->getValue());
Richard Sandiford178273a2013-09-05 10:36:45 +00001012
1013 // Prefer not to use MVC if either address can use ... RELATIVE LONG
1014 // instructions.
1015 uint64_t Size = Load->getMemoryVT().getStoreSize();
1016 if (Size > 1 && Size <= 8) {
1017 // Prefer LHRL, LRL and LGRL.
Richard Sandiford54b36912013-09-27 15:14:04 +00001018 if (SystemZISD::isPCREL(Load->getBasePtr().getOpcode()))
Richard Sandiford178273a2013-09-05 10:36:45 +00001019 return false;
1020 // Prefer STHRL, STRL and STGRL.
Richard Sandiford54b36912013-09-27 15:14:04 +00001021 if (SystemZISD::isPCREL(Store->getBasePtr().getOpcode()))
Richard Sandiford178273a2013-09-05 10:36:45 +00001022 return false;
1023 }
1024
Richard Sandiford067817e2013-09-27 15:29:20 +00001025 return canUseBlockOperation(Store, Load);
Richard Sandiford178273a2013-09-05 10:36:45 +00001026}
1027
1028bool SystemZDAGToDAGISel::storeLoadCanUseBlockBinary(SDNode *N,
1029 unsigned I) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001030 auto *StoreA = cast<StoreSDNode>(N);
1031 auto *LoadA = cast<LoadSDNode>(StoreA->getValue().getOperand(1 - I));
1032 auto *LoadB = cast<LoadSDNode>(StoreA->getValue().getOperand(I));
Richard Sandiford067817e2013-09-27 15:29:20 +00001033 return !LoadA->isVolatile() && canUseBlockOperation(StoreA, LoadB);
Richard Sandiford178273a2013-09-05 10:36:45 +00001034}
1035
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001036SDNode *SystemZDAGToDAGISel::Select(SDNode *Node) {
1037 // Dump information about the Node being selected
1038 DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n");
1039
1040 // If we have a custom node, we already have selected!
1041 if (Node->isMachineOpcode()) {
1042 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
Tim Northover31d093c2013-09-22 08:21:56 +00001043 Node->setNodeId(-1);
Craig Topper062a2ba2014-04-25 05:30:21 +00001044 return nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001045 }
1046
1047 unsigned Opcode = Node->getOpcode();
Craig Topper062a2ba2014-04-25 05:30:21 +00001048 SDNode *ResNode = nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001049 switch (Opcode) {
1050 case ISD::OR:
Richard Sandiford885140c2013-07-16 11:55:57 +00001051 if (Node->getOperand(1).getOpcode() != ISD::Constant)
Richard Sandiford7878b852013-07-18 10:06:15 +00001052 ResNode = tryRxSBG(Node, SystemZ::ROSBG);
1053 goto or_xor;
1054
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001055 case ISD::XOR:
Richard Sandiford7878b852013-07-18 10:06:15 +00001056 if (Node->getOperand(1).getOpcode() != ISD::Constant)
1057 ResNode = tryRxSBG(Node, SystemZ::RXSBG);
1058 // Fall through.
1059 or_xor:
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001060 // If this is a 64-bit operation in which both 32-bit halves are nonzero,
1061 // split the operation into two.
Richard Sandiford885140c2013-07-16 11:55:57 +00001062 if (!ResNode && Node->getValueType(0) == MVT::i64)
Richard Sandiford21f5d682014-03-06 11:22:58 +00001063 if (auto *Op1 = dyn_cast<ConstantSDNode>(Node->getOperand(1))) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001064 uint64_t Val = Op1->getZExtValue();
1065 if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val))
1066 Node = splitLargeImmediate(Opcode, Node, Node->getOperand(0),
1067 Val - uint32_t(Val), uint32_t(Val));
1068 }
1069 break;
1070
Richard Sandiford84f54a32013-07-11 08:59:12 +00001071 case ISD::AND:
Richard Sandiford51093212013-07-18 10:40:35 +00001072 if (Node->getOperand(1).getOpcode() != ISD::Constant)
1073 ResNode = tryRxSBG(Node, SystemZ::RNSBG);
1074 // Fall through.
Richard Sandiford82ec87d2013-07-16 11:02:24 +00001075 case ISD::ROTL:
1076 case ISD::SHL:
1077 case ISD::SRL:
Richard Sandiford220ee492013-12-20 11:49:48 +00001078 case ISD::ZERO_EXTEND:
Richard Sandiford7878b852013-07-18 10:06:15 +00001079 if (!ResNode)
1080 ResNode = tryRISBGZero(Node);
Richard Sandiford84f54a32013-07-11 08:59:12 +00001081 break;
1082
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001083 case ISD::Constant:
1084 // If this is a 64-bit constant that is out of the range of LLILF,
1085 // LLIHF and LGFI, split it into two 32-bit pieces.
1086 if (Node->getValueType(0) == MVT::i64) {
1087 uint64_t Val = cast<ConstantSDNode>(Node)->getZExtValue();
1088 if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val) && !isInt<32>(Val))
1089 Node = splitLargeImmediate(ISD::OR, Node, SDValue(),
1090 Val - uint32_t(Val), uint32_t(Val));
1091 }
1092 break;
1093
Richard Sandifordee834382013-07-31 12:38:08 +00001094 case SystemZISD::SELECT_CCMASK: {
1095 SDValue Op0 = Node->getOperand(0);
1096 SDValue Op1 = Node->getOperand(1);
1097 // Prefer to put any load first, so that it can be matched as a
1098 // conditional load.
1099 if (Op1.getOpcode() == ISD::LOAD && Op0.getOpcode() != ISD::LOAD) {
1100 SDValue CCValid = Node->getOperand(2);
1101 SDValue CCMask = Node->getOperand(3);
1102 uint64_t ConstCCValid =
1103 cast<ConstantSDNode>(CCValid.getNode())->getZExtValue();
1104 uint64_t ConstCCMask =
1105 cast<ConstantSDNode>(CCMask.getNode())->getZExtValue();
1106 // Invert the condition.
1107 CCMask = CurDAG->getConstant(ConstCCValid ^ ConstCCMask,
1108 CCMask.getValueType());
1109 SDValue Op4 = Node->getOperand(4);
1110 Node = CurDAG->UpdateNodeOperands(Node, Op1, Op0, CCValid, CCMask, Op4);
1111 }
1112 break;
1113 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001114 }
1115
1116 // Select the default instruction
Richard Sandiford84f54a32013-07-11 08:59:12 +00001117 if (!ResNode)
1118 ResNode = SelectCode(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001119
1120 DEBUG(errs() << "=> ";
Craig Topper062a2ba2014-04-25 05:30:21 +00001121 if (ResNode == nullptr || ResNode == Node)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001122 Node->dump(CurDAG);
1123 else
1124 ResNode->dump(CurDAG);
1125 errs() << "\n";
1126 );
1127 return ResNode;
1128}
1129
1130bool SystemZDAGToDAGISel::
1131SelectInlineAsmMemoryOperand(const SDValue &Op,
Daniel Sanders60f1db02015-03-13 12:45:09 +00001132 unsigned ConstraintID,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001133 std::vector<SDValue> &OutOps) {
Daniel Sanders60f1db02015-03-13 12:45:09 +00001134 assert(ConstraintID == InlineAsm::Constraint_m &&
1135 "Unexpected constraint code");
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001136 // Accept addresses with short displacements, which are compatible
1137 // with Q, R, S and T. But keep the index operand for future expansion.
1138 SDValue Base, Disp, Index;
1139 if (!selectBDXAddr(SystemZAddressingMode::FormBD,
1140 SystemZAddressingMode::Disp12Only,
1141 Op, Base, Disp, Index))
1142 return true;
1143 OutOps.push_back(Base);
1144 OutOps.push_back(Disp);
1145 OutOps.push_back(Index);
1146 return false;
1147}