blob: d8b5c364c4b818fdb56d3f4c2c6f95c61d4110ed [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
22namespace {
23// Used to build addressing modes.
24struct SystemZAddressingMode {
25 // The shape of the address.
26 enum AddrForm {
27 // base+displacement
28 FormBD,
29
30 // base+displacement+index for load and store operands
31 FormBDXNormal,
32
33 // base+displacement+index for load address operands
34 FormBDXLA,
35
36 // base+displacement+index+ADJDYNALLOC
37 FormBDXDynAlloc
38 };
39 AddrForm Form;
40
41 // The type of displacement. The enum names here correspond directly
42 // to the definitions in SystemZOperand.td. We could split them into
43 // flags -- single/pair, 128-bit, etc. -- but it hardly seems worth it.
44 enum DispRange {
45 Disp12Only,
46 Disp12Pair,
47 Disp20Only,
48 Disp20Only128,
49 Disp20Pair
50 };
51 DispRange DR;
52
53 // The parts of the address. The address is equivalent to:
54 //
55 // Base + Disp + Index + (IncludesDynAlloc ? ADJDYNALLOC : 0)
56 SDValue Base;
57 int64_t Disp;
58 SDValue Index;
59 bool IncludesDynAlloc;
60
61 SystemZAddressingMode(AddrForm form, DispRange dr)
62 : Form(form), DR(dr), Base(), Disp(0), Index(),
63 IncludesDynAlloc(false) {}
64
65 // True if the address can have an index register.
66 bool hasIndexField() { return Form != FormBD; }
67
68 // True if the address can (and must) include ADJDYNALLOC.
69 bool isDynAlloc() { return Form == FormBDXDynAlloc; }
70
71 void dump() {
72 errs() << "SystemZAddressingMode " << this << '\n';
73
74 errs() << " Base ";
75 if (Base.getNode() != 0)
76 Base.getNode()->dump();
77 else
78 errs() << "null\n";
79
80 if (hasIndexField()) {
81 errs() << " Index ";
82 if (Index.getNode() != 0)
83 Index.getNode()->dump();
84 else
85 errs() << "null\n";
86 }
87
88 errs() << " Disp " << Disp;
89 if (IncludesDynAlloc)
90 errs() << " + ADJDYNALLOC";
91 errs() << '\n';
92 }
93};
94
Richard Sandiford82ec87d2013-07-16 11:02:24 +000095// Return a mask with Count low bits set.
96static uint64_t allOnes(unsigned int Count) {
97 return Count == 0 ? 0 : (uint64_t(1) << (Count - 1) << 1) - 1;
98}
99
Richard Sandiford51093212013-07-18 10:40:35 +0000100// Represents operands 2 to 5 of the ROTATE AND ... SELECTED BITS operation
101// given by Opcode. The operands are: Input (R2), Start (I3), End (I4) and
102// Rotate (I5). The combined operand value is effectively:
103//
104// (or (rotl Input, Rotate), ~Mask)
105//
106// for RNSBG and:
107//
108// (and (rotl Input, Rotate), Mask)
109//
Richard Sandiford3e382972013-10-16 13:35:13 +0000110// otherwise. The output value has BitSize bits, although Input may be
111// narrower (in which case the upper bits are don't care).
Richard Sandiford5cbac962013-07-18 09:45:08 +0000112struct RxSBGOperands {
Richard Sandiford51093212013-07-18 10:40:35 +0000113 RxSBGOperands(unsigned Op, SDValue N)
114 : Opcode(Op), BitSize(N.getValueType().getSizeInBits()),
115 Mask(allOnes(BitSize)), Input(N), Start(64 - BitSize), End(63),
116 Rotate(0) {}
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000117
Richard Sandiford51093212013-07-18 10:40:35 +0000118 unsigned Opcode;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000119 unsigned BitSize;
120 uint64_t Mask;
121 SDValue Input;
122 unsigned Start;
123 unsigned End;
124 unsigned Rotate;
125};
126
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000127class SystemZDAGToDAGISel : public SelectionDAGISel {
128 const SystemZTargetLowering &Lowering;
129 const SystemZSubtarget &Subtarget;
130
131 // Used by SystemZOperands.td to create integer constants.
Richard Sandiford54b36912013-09-27 15:14:04 +0000132 inline SDValue getImm(const SDNode *Node, uint64_t Imm) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000133 return CurDAG->getTargetConstant(Imm, Node->getValueType(0));
134 }
135
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000136 const SystemZTargetMachine &getTargetMachine() const {
137 return static_cast<const SystemZTargetMachine &>(TM);
138 }
139
140 const SystemZInstrInfo *getInstrInfo() const {
141 return getTargetMachine().getInstrInfo();
142 }
143
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000144 // Try to fold more of the base or index of AM into AM, where IsBase
145 // selects between the base and index.
Richard Sandiford54b36912013-09-27 15:14:04 +0000146 bool expandAddress(SystemZAddressingMode &AM, bool IsBase) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000147
148 // Try to describe N in AM, returning true on success.
Richard Sandiford54b36912013-09-27 15:14:04 +0000149 bool selectAddress(SDValue N, SystemZAddressingMode &AM) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000150
151 // Extract individual target operands from matched address AM.
152 void getAddressOperands(const SystemZAddressingMode &AM, EVT VT,
Richard Sandiford54b36912013-09-27 15:14:04 +0000153 SDValue &Base, SDValue &Disp) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000154 void getAddressOperands(const SystemZAddressingMode &AM, EVT VT,
Richard Sandiford54b36912013-09-27 15:14:04 +0000155 SDValue &Base, SDValue &Disp, SDValue &Index) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000156
157 // Try to match Addr as a FormBD address with displacement type DR.
158 // Return true on success, storing the base and displacement in
159 // Base and Disp respectively.
160 bool selectBDAddr(SystemZAddressingMode::DispRange DR, SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000161 SDValue &Base, SDValue &Disp) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000162
Richard Sandiforda481f582013-08-23 11:18:53 +0000163 // Try to match Addr as a FormBDX address with displacement type DR.
164 // Return true on success and if the result had no index. Store the
165 // base and displacement in Base and Disp respectively.
166 bool selectMVIAddr(SystemZAddressingMode::DispRange DR, SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000167 SDValue &Base, SDValue &Disp) const;
Richard Sandiforda481f582013-08-23 11:18:53 +0000168
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000169 // Try to match Addr as a FormBDX* address of form Form with
170 // displacement type DR. Return true on success, storing the base,
171 // displacement and index in Base, Disp and Index respectively.
172 bool selectBDXAddr(SystemZAddressingMode::AddrForm Form,
173 SystemZAddressingMode::DispRange DR, SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000174 SDValue &Base, SDValue &Disp, SDValue &Index) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000175
176 // PC-relative address matching routines used by SystemZOperands.td.
Richard Sandiford54b36912013-09-27 15:14:04 +0000177 bool selectPCRelAddress(SDValue Addr, SDValue &Target) const {
178 if (SystemZISD::isPCREL(Addr.getOpcode())) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000179 Target = Addr.getOperand(0);
180 return true;
181 }
182 return false;
183 }
184
185 // BD matching routines used by SystemZOperands.td.
Richard Sandiford54b36912013-09-27 15:14:04 +0000186 bool selectBDAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000187 return selectBDAddr(SystemZAddressingMode::Disp12Only, Addr, Base, Disp);
188 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000189 bool selectBDAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000190 return selectBDAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp);
191 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000192 bool selectBDAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000193 return selectBDAddr(SystemZAddressingMode::Disp20Only, Addr, Base, Disp);
194 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000195 bool selectBDAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000196 return selectBDAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp);
197 }
198
Richard Sandiforda481f582013-08-23 11:18:53 +0000199 // MVI matching routines used by SystemZOperands.td.
Richard Sandiford54b36912013-09-27 15:14:04 +0000200 bool selectMVIAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Richard Sandiforda481f582013-08-23 11:18:53 +0000201 return selectMVIAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp);
202 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000203 bool selectMVIAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Richard Sandiforda481f582013-08-23 11:18:53 +0000204 return selectMVIAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp);
205 }
206
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000207 // BDX matching routines used by SystemZOperands.td.
208 bool selectBDXAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000209 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000210 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
211 SystemZAddressingMode::Disp12Only,
212 Addr, Base, Disp, Index);
213 }
214 bool selectBDXAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000215 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000216 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
217 SystemZAddressingMode::Disp12Pair,
218 Addr, Base, Disp, Index);
219 }
220 bool selectDynAlloc12Only(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000221 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000222 return selectBDXAddr(SystemZAddressingMode::FormBDXDynAlloc,
223 SystemZAddressingMode::Disp12Only,
224 Addr, Base, Disp, Index);
225 }
226 bool selectBDXAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000227 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000228 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
229 SystemZAddressingMode::Disp20Only,
230 Addr, Base, Disp, Index);
231 }
232 bool selectBDXAddr20Only128(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000233 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000234 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
235 SystemZAddressingMode::Disp20Only128,
236 Addr, Base, Disp, Index);
237 }
238 bool selectBDXAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000239 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000240 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
241 SystemZAddressingMode::Disp20Pair,
242 Addr, Base, Disp, Index);
243 }
244 bool selectLAAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000245 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000246 return selectBDXAddr(SystemZAddressingMode::FormBDXLA,
247 SystemZAddressingMode::Disp12Pair,
248 Addr, Base, Disp, Index);
249 }
250 bool selectLAAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000251 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000252 return selectBDXAddr(SystemZAddressingMode::FormBDXLA,
253 SystemZAddressingMode::Disp20Pair,
254 Addr, Base, Disp, Index);
255 }
256
Richard Sandiford885140c2013-07-16 11:55:57 +0000257 // Check whether (or Op (and X InsertMask)) is effectively an insertion
258 // of X into bits InsertMask of some Y != Op. Return true if so and
259 // set Op to that Y.
Richard Sandiford54b36912013-09-27 15:14:04 +0000260 bool detectOrAndInsertion(SDValue &Op, uint64_t InsertMask) const;
Richard Sandiford885140c2013-07-16 11:55:57 +0000261
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000262 // Try to update RxSBG so that only the bits of RxSBG.Input in Mask are used.
263 // Return true on success.
Richard Sandiford54b36912013-09-27 15:14:04 +0000264 bool refineRxSBGMask(RxSBGOperands &RxSBG, uint64_t Mask) const;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000265
Richard Sandiford5cbac962013-07-18 09:45:08 +0000266 // Try to fold some of RxSBG.Input into other fields of RxSBG.
267 // Return true on success.
Richard Sandiford54b36912013-09-27 15:14:04 +0000268 bool expandRxSBG(RxSBGOperands &RxSBG) const;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000269
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000270 // Return an undefined value of type VT.
271 SDValue getUNDEF(SDLoc DL, EVT VT) const;
Richard Sandiford84f54a32013-07-11 08:59:12 +0000272
273 // Convert N to VT, if it isn't already.
Richard Sandiford54b36912013-09-27 15:14:04 +0000274 SDValue convertTo(SDLoc DL, EVT VT, SDValue N) const;
Richard Sandiford84f54a32013-07-11 08:59:12 +0000275
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000276 // Try to implement AND or shift node N using RISBG with the zero flag set.
277 // Return the selected node on success, otherwise return null.
278 SDNode *tryRISBGZero(SDNode *N);
Richard Sandiford84f54a32013-07-11 08:59:12 +0000279
Richard Sandiford7878b852013-07-18 10:06:15 +0000280 // Try to use RISBG or Opcode to implement OR or XOR node N.
281 // Return the selected node on success, otherwise return null.
282 SDNode *tryRxSBG(SDNode *N, unsigned Opcode);
Richard Sandiford885140c2013-07-16 11:55:57 +0000283
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000284 // If Op0 is null, then Node is a constant that can be loaded using:
285 //
286 // (Opcode UpperVal LowerVal)
287 //
288 // If Op0 is nonnull, then Node can be implemented using:
289 //
290 // (Opcode (Opcode Op0 UpperVal) LowerVal)
291 SDNode *splitLargeImmediate(unsigned Opcode, SDNode *Node, SDValue Op0,
292 uint64_t UpperVal, uint64_t LowerVal);
293
Richard Sandiford067817e2013-09-27 15:29:20 +0000294 // Return true if Load and Store are loads and stores of the same size
295 // and are guaranteed not to overlap. Such operations can be implemented
296 // using block (SS-format) instructions.
297 //
298 // Partial overlap would lead to incorrect code, since the block operations
299 // are logically bytewise, even though they have a fast path for the
300 // non-overlapping case. We also need to avoid full overlap (i.e. two
301 // addresses that might be equal at run time) because although that case
302 // would be handled correctly, it might be implemented by millicode.
303 bool canUseBlockOperation(StoreSDNode *Store, LoadSDNode *Load) const;
304
Richard Sandiford178273a2013-09-05 10:36:45 +0000305 // N is a (store (load Y), X) pattern. Return true if it can use an MVC
306 // from Y to X.
Richard Sandiford97846492013-07-09 09:46:39 +0000307 bool storeLoadCanUseMVC(SDNode *N) const;
308
Richard Sandiford178273a2013-09-05 10:36:45 +0000309 // N is a (store (op (load A[0]), (load A[1])), X) pattern. Return true
310 // if A[1 - I] == X and if N can use a block operation like NC from A[I]
311 // to X.
312 bool storeLoadCanUseBlockBinary(SDNode *N, unsigned I) const;
313
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000314public:
315 SystemZDAGToDAGISel(SystemZTargetMachine &TM, CodeGenOpt::Level OptLevel)
316 : SelectionDAGISel(TM, OptLevel),
317 Lowering(*TM.getTargetLowering()),
318 Subtarget(*TM.getSubtargetImpl()) { }
319
320 // Override MachineFunctionPass.
Craig Topper73156022014-03-02 09:09:27 +0000321 virtual const char *getPassName() const override {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000322 return "SystemZ DAG->DAG Pattern Instruction Selection";
323 }
324
325 // Override SelectionDAGISel.
Craig Topper73156022014-03-02 09:09:27 +0000326 virtual SDNode *Select(SDNode *Node) override;
327 virtual bool
328 SelectInlineAsmMemoryOperand(const SDValue &Op, char ConstraintCode,
329 std::vector<SDValue> &OutOps) override;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000330
331 // Include the pieces autogenerated from the target description.
332 #include "SystemZGenDAGISel.inc"
333};
334} // end anonymous namespace
335
336FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM,
337 CodeGenOpt::Level OptLevel) {
338 return new SystemZDAGToDAGISel(TM, OptLevel);
339}
340
341// Return true if Val should be selected as a displacement for an address
342// with range DR. Here we're interested in the range of both the instruction
343// described by DR and of any pairing instruction.
344static bool selectDisp(SystemZAddressingMode::DispRange DR, int64_t Val) {
345 switch (DR) {
346 case SystemZAddressingMode::Disp12Only:
347 return isUInt<12>(Val);
348
349 case SystemZAddressingMode::Disp12Pair:
350 case SystemZAddressingMode::Disp20Only:
351 case SystemZAddressingMode::Disp20Pair:
352 return isInt<20>(Val);
353
354 case SystemZAddressingMode::Disp20Only128:
355 return isInt<20>(Val) && isInt<20>(Val + 8);
356 }
357 llvm_unreachable("Unhandled displacement range");
358}
359
360// Change the base or index in AM to Value, where IsBase selects
361// between the base and index.
362static void changeComponent(SystemZAddressingMode &AM, bool IsBase,
363 SDValue Value) {
364 if (IsBase)
365 AM.Base = Value;
366 else
367 AM.Index = Value;
368}
369
370// The base or index of AM is equivalent to Value + ADJDYNALLOC,
371// where IsBase selects between the base and index. Try to fold the
372// ADJDYNALLOC into AM.
373static bool expandAdjDynAlloc(SystemZAddressingMode &AM, bool IsBase,
374 SDValue Value) {
375 if (AM.isDynAlloc() && !AM.IncludesDynAlloc) {
376 changeComponent(AM, IsBase, Value);
377 AM.IncludesDynAlloc = true;
378 return true;
379 }
380 return false;
381}
382
383// The base of AM is equivalent to Base + Index. Try to use Index as
384// the index register.
385static bool expandIndex(SystemZAddressingMode &AM, SDValue Base,
386 SDValue Index) {
387 if (AM.hasIndexField() && !AM.Index.getNode()) {
388 AM.Base = Base;
389 AM.Index = Index;
390 return true;
391 }
392 return false;
393}
394
395// The base or index of AM is equivalent to Op0 + Op1, where IsBase selects
396// between the base and index. Try to fold Op1 into AM's displacement.
397static bool expandDisp(SystemZAddressingMode &AM, bool IsBase,
Richard Sandiford54b36912013-09-27 15:14:04 +0000398 SDValue Op0, uint64_t Op1) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000399 // First try adjusting the displacement.
Richard Sandiford54b36912013-09-27 15:14:04 +0000400 int64_t TestDisp = AM.Disp + Op1;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000401 if (selectDisp(AM.DR, TestDisp)) {
402 changeComponent(AM, IsBase, Op0);
403 AM.Disp = TestDisp;
404 return true;
405 }
406
407 // We could consider forcing the displacement into a register and
408 // using it as an index, but it would need to be carefully tuned.
409 return false;
410}
411
412bool SystemZDAGToDAGISel::expandAddress(SystemZAddressingMode &AM,
Richard Sandiford54b36912013-09-27 15:14:04 +0000413 bool IsBase) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000414 SDValue N = IsBase ? AM.Base : AM.Index;
415 unsigned Opcode = N.getOpcode();
416 if (Opcode == ISD::TRUNCATE) {
417 N = N.getOperand(0);
418 Opcode = N.getOpcode();
419 }
420 if (Opcode == ISD::ADD || CurDAG->isBaseWithConstantOffset(N)) {
421 SDValue Op0 = N.getOperand(0);
422 SDValue Op1 = N.getOperand(1);
423
424 unsigned Op0Code = Op0->getOpcode();
425 unsigned Op1Code = Op1->getOpcode();
426
427 if (Op0Code == SystemZISD::ADJDYNALLOC)
428 return expandAdjDynAlloc(AM, IsBase, Op1);
429 if (Op1Code == SystemZISD::ADJDYNALLOC)
430 return expandAdjDynAlloc(AM, IsBase, Op0);
431
432 if (Op0Code == ISD::Constant)
Richard Sandiford54b36912013-09-27 15:14:04 +0000433 return expandDisp(AM, IsBase, Op1,
434 cast<ConstantSDNode>(Op0)->getSExtValue());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000435 if (Op1Code == ISD::Constant)
Richard Sandiford54b36912013-09-27 15:14:04 +0000436 return expandDisp(AM, IsBase, Op0,
437 cast<ConstantSDNode>(Op1)->getSExtValue());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000438
439 if (IsBase && expandIndex(AM, Op0, Op1))
440 return true;
441 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000442 if (Opcode == SystemZISD::PCREL_OFFSET) {
443 SDValue Full = N.getOperand(0);
444 SDValue Base = N.getOperand(1);
445 SDValue Anchor = Base.getOperand(0);
446 uint64_t Offset = (cast<GlobalAddressSDNode>(Full)->getOffset() -
447 cast<GlobalAddressSDNode>(Anchor)->getOffset());
448 return expandDisp(AM, IsBase, Base, Offset);
449 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000450 return false;
451}
452
453// Return true if an instruction with displacement range DR should be
454// used for displacement value Val. selectDisp(DR, Val) must already hold.
455static bool isValidDisp(SystemZAddressingMode::DispRange DR, int64_t Val) {
456 assert(selectDisp(DR, Val) && "Invalid displacement");
457 switch (DR) {
458 case SystemZAddressingMode::Disp12Only:
459 case SystemZAddressingMode::Disp20Only:
460 case SystemZAddressingMode::Disp20Only128:
461 return true;
462
463 case SystemZAddressingMode::Disp12Pair:
464 // Use the other instruction if the displacement is too large.
465 return isUInt<12>(Val);
466
467 case SystemZAddressingMode::Disp20Pair:
468 // Use the other instruction if the displacement is small enough.
469 return !isUInt<12>(Val);
470 }
471 llvm_unreachable("Unhandled displacement range");
472}
473
474// Return true if Base + Disp + Index should be performed by LA(Y).
475static bool shouldUseLA(SDNode *Base, int64_t Disp, SDNode *Index) {
476 // Don't use LA(Y) for constants.
477 if (!Base)
478 return false;
479
480 // Always use LA(Y) for frame addresses, since we know that the destination
481 // register is almost always (perhaps always) going to be different from
482 // the frame register.
483 if (Base->getOpcode() == ISD::FrameIndex)
484 return true;
485
486 if (Disp) {
487 // Always use LA(Y) if there is a base, displacement and index.
488 if (Index)
489 return true;
490
491 // Always use LA if the displacement is small enough. It should always
492 // be no worse than AGHI (and better if it avoids a move).
493 if (isUInt<12>(Disp))
494 return true;
495
496 // For similar reasons, always use LAY if the constant is too big for AGHI.
497 // LAY should be no worse than AGFI.
498 if (!isInt<16>(Disp))
499 return true;
500 } else {
501 // Don't use LA for plain registers.
502 if (!Index)
503 return false;
504
505 // Don't use LA for plain addition if the index operand is only used
506 // once. It should be a natural two-operand addition in that case.
507 if (Index->hasOneUse())
508 return false;
509
510 // Prefer addition if the second operation is sign-extended, in the
511 // hope of using AGF.
512 unsigned IndexOpcode = Index->getOpcode();
513 if (IndexOpcode == ISD::SIGN_EXTEND ||
514 IndexOpcode == ISD::SIGN_EXTEND_INREG)
515 return false;
516 }
517
518 // Don't use LA for two-operand addition if either operand is only
519 // used once. The addition instructions are better in that case.
520 if (Base->hasOneUse())
521 return false;
522
523 return true;
524}
525
526// Return true if Addr is suitable for AM, updating AM if so.
527bool SystemZDAGToDAGISel::selectAddress(SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000528 SystemZAddressingMode &AM) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000529 // Start out assuming that the address will need to be loaded separately,
530 // then try to extend it as much as we can.
531 AM.Base = Addr;
532
533 // First try treating the address as a constant.
534 if (Addr.getOpcode() == ISD::Constant &&
Richard Sandiford54b36912013-09-27 15:14:04 +0000535 expandDisp(AM, true, SDValue(),
536 cast<ConstantSDNode>(Addr)->getSExtValue()))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000537 ;
538 else
539 // Otherwise try expanding each component.
540 while (expandAddress(AM, true) ||
541 (AM.Index.getNode() && expandAddress(AM, false)))
542 continue;
543
544 // Reject cases where it isn't profitable to use LA(Y).
545 if (AM.Form == SystemZAddressingMode::FormBDXLA &&
546 !shouldUseLA(AM.Base.getNode(), AM.Disp, AM.Index.getNode()))
547 return false;
548
549 // Reject cases where the other instruction in a pair should be used.
550 if (!isValidDisp(AM.DR, AM.Disp))
551 return false;
552
553 // Make sure that ADJDYNALLOC is included where necessary.
554 if (AM.isDynAlloc() && !AM.IncludesDynAlloc)
555 return false;
556
557 DEBUG(AM.dump());
558 return true;
559}
560
561// Insert a node into the DAG at least before Pos. This will reposition
562// the node as needed, and will assign it a node ID that is <= Pos's ID.
563// Note that this does *not* preserve the uniqueness of node IDs!
564// The selection DAG must no longer depend on their uniqueness when this
565// function is used.
566static void insertDAGNode(SelectionDAG *DAG, SDNode *Pos, SDValue N) {
567 if (N.getNode()->getNodeId() == -1 ||
568 N.getNode()->getNodeId() > Pos->getNodeId()) {
569 DAG->RepositionNode(Pos, N.getNode());
570 N.getNode()->setNodeId(Pos->getNodeId());
571 }
572}
573
574void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
575 EVT VT, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000576 SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000577 Base = AM.Base;
578 if (!Base.getNode())
579 // Register 0 means "no base". This is mostly useful for shifts.
580 Base = CurDAG->getRegister(0, VT);
581 else if (Base.getOpcode() == ISD::FrameIndex) {
582 // Lower a FrameIndex to a TargetFrameIndex.
583 int64_t FrameIndex = cast<FrameIndexSDNode>(Base)->getIndex();
584 Base = CurDAG->getTargetFrameIndex(FrameIndex, VT);
585 } else if (Base.getValueType() != VT) {
586 // Truncate values from i64 to i32, for shifts.
587 assert(VT == MVT::i32 && Base.getValueType() == MVT::i64 &&
588 "Unexpected truncation");
Andrew Trickef9de2a2013-05-25 02:42:55 +0000589 SDLoc DL(Base);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000590 SDValue Trunc = CurDAG->getNode(ISD::TRUNCATE, DL, VT, Base);
591 insertDAGNode(CurDAG, Base.getNode(), Trunc);
592 Base = Trunc;
593 }
594
595 // Lower the displacement to a TargetConstant.
596 Disp = CurDAG->getTargetConstant(AM.Disp, VT);
597}
598
599void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
600 EVT VT, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000601 SDValue &Disp,
602 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000603 getAddressOperands(AM, VT, Base, Disp);
604
605 Index = AM.Index;
606 if (!Index.getNode())
607 // Register 0 means "no index".
608 Index = CurDAG->getRegister(0, VT);
609}
610
611bool SystemZDAGToDAGISel::selectBDAddr(SystemZAddressingMode::DispRange DR,
612 SDValue Addr, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000613 SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000614 SystemZAddressingMode AM(SystemZAddressingMode::FormBD, DR);
615 if (!selectAddress(Addr, AM))
616 return false;
617
618 getAddressOperands(AM, Addr.getValueType(), Base, Disp);
619 return true;
620}
621
Richard Sandiforda481f582013-08-23 11:18:53 +0000622bool SystemZDAGToDAGISel::selectMVIAddr(SystemZAddressingMode::DispRange DR,
623 SDValue Addr, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000624 SDValue &Disp) const {
Richard Sandiforda481f582013-08-23 11:18:53 +0000625 SystemZAddressingMode AM(SystemZAddressingMode::FormBDXNormal, DR);
626 if (!selectAddress(Addr, AM) || AM.Index.getNode())
627 return false;
628
629 getAddressOperands(AM, Addr.getValueType(), Base, Disp);
630 return true;
631}
632
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000633bool SystemZDAGToDAGISel::selectBDXAddr(SystemZAddressingMode::AddrForm Form,
634 SystemZAddressingMode::DispRange DR,
635 SDValue Addr, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000636 SDValue &Disp, SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000637 SystemZAddressingMode AM(Form, DR);
638 if (!selectAddress(Addr, AM))
639 return false;
640
641 getAddressOperands(AM, Addr.getValueType(), Base, Disp, Index);
642 return true;
643}
644
Richard Sandiford885140c2013-07-16 11:55:57 +0000645bool SystemZDAGToDAGISel::detectOrAndInsertion(SDValue &Op,
Richard Sandiford54b36912013-09-27 15:14:04 +0000646 uint64_t InsertMask) const {
Richard Sandiford885140c2013-07-16 11:55:57 +0000647 // We're only interested in cases where the insertion is into some operand
648 // of Op, rather than into Op itself. The only useful case is an AND.
649 if (Op.getOpcode() != ISD::AND)
650 return false;
651
652 // We need a constant mask.
Richard Sandiford21f5d682014-03-06 11:22:58 +0000653 auto *MaskNode = dyn_cast<ConstantSDNode>(Op.getOperand(1).getNode());
Richard Sandiford885140c2013-07-16 11:55:57 +0000654 if (!MaskNode)
655 return false;
656
657 // It's not an insertion of Op.getOperand(0) if the two masks overlap.
658 uint64_t AndMask = MaskNode->getZExtValue();
659 if (InsertMask & AndMask)
660 return false;
661
662 // It's only an insertion if all bits are covered or are known to be zero.
663 // The inner check covers all cases but is more expensive.
664 uint64_t Used = allOnes(Op.getValueType().getSizeInBits());
665 if (Used != (AndMask | InsertMask)) {
666 APInt KnownZero, KnownOne;
667 CurDAG->ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne);
668 if (Used != (AndMask | InsertMask | KnownZero.getZExtValue()))
669 return false;
670 }
671
672 Op = Op.getOperand(0);
673 return true;
674}
675
Richard Sandiford54b36912013-09-27 15:14:04 +0000676bool SystemZDAGToDAGISel::refineRxSBGMask(RxSBGOperands &RxSBG,
677 uint64_t Mask) const {
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000678 const SystemZInstrInfo *TII = getInstrInfo();
Richard Sandiford5cbac962013-07-18 09:45:08 +0000679 if (RxSBG.Rotate != 0)
680 Mask = (Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate));
681 Mask &= RxSBG.Mask;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000682 if (TII->isRxSBGMask(Mask, RxSBG.BitSize, RxSBG.Start, RxSBG.End)) {
Richard Sandiford5cbac962013-07-18 09:45:08 +0000683 RxSBG.Mask = Mask;
Richard Sandiford5cbac962013-07-18 09:45:08 +0000684 return true;
685 }
Richard Sandiford84f54a32013-07-11 08:59:12 +0000686 return false;
687}
688
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000689// Return true if any bits of (RxSBG.Input & Mask) are significant.
690static bool maskMatters(RxSBGOperands &RxSBG, uint64_t Mask) {
691 // Rotate the mask in the same way as RxSBG.Input is rotated.
Richard Sandiford297f7d22013-07-18 10:14:55 +0000692 if (RxSBG.Rotate != 0)
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000693 Mask = ((Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate)));
694 return (Mask & RxSBG.Mask) != 0;
Richard Sandiford297f7d22013-07-18 10:14:55 +0000695}
696
Richard Sandiford54b36912013-09-27 15:14:04 +0000697bool SystemZDAGToDAGISel::expandRxSBG(RxSBGOperands &RxSBG) const {
Richard Sandiford5cbac962013-07-18 09:45:08 +0000698 SDValue N = RxSBG.Input;
Richard Sandiford297f7d22013-07-18 10:14:55 +0000699 unsigned Opcode = N.getOpcode();
700 switch (Opcode) {
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000701 case ISD::AND: {
Richard Sandiford51093212013-07-18 10:40:35 +0000702 if (RxSBG.Opcode == SystemZ::RNSBG)
703 return false;
704
Richard Sandiford21f5d682014-03-06 11:22:58 +0000705 auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000706 if (!MaskNode)
707 return false;
708
709 SDValue Input = N.getOperand(0);
710 uint64_t Mask = MaskNode->getZExtValue();
Richard Sandiford5cbac962013-07-18 09:45:08 +0000711 if (!refineRxSBGMask(RxSBG, Mask)) {
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000712 // If some bits of Input are already known zeros, those bits will have
713 // been removed from the mask. See if adding them back in makes the
714 // mask suitable.
715 APInt KnownZero, KnownOne;
716 CurDAG->ComputeMaskedBits(Input, KnownZero, KnownOne);
717 Mask |= KnownZero.getZExtValue();
Richard Sandiford5cbac962013-07-18 09:45:08 +0000718 if (!refineRxSBGMask(RxSBG, Mask))
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000719 return false;
720 }
Richard Sandiford5cbac962013-07-18 09:45:08 +0000721 RxSBG.Input = Input;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000722 return true;
723 }
724
Richard Sandiford51093212013-07-18 10:40:35 +0000725 case ISD::OR: {
726 if (RxSBG.Opcode != SystemZ::RNSBG)
727 return false;
728
Richard Sandiford21f5d682014-03-06 11:22:58 +0000729 auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford51093212013-07-18 10:40:35 +0000730 if (!MaskNode)
731 return false;
732
733 SDValue Input = N.getOperand(0);
734 uint64_t Mask = ~MaskNode->getZExtValue();
735 if (!refineRxSBGMask(RxSBG, Mask)) {
736 // If some bits of Input are already known ones, those bits will have
737 // been removed from the mask. See if adding them back in makes the
738 // mask suitable.
739 APInt KnownZero, KnownOne;
740 CurDAG->ComputeMaskedBits(Input, KnownZero, KnownOne);
741 Mask &= ~KnownOne.getZExtValue();
742 if (!refineRxSBGMask(RxSBG, Mask))
743 return false;
744 }
745 RxSBG.Input = Input;
746 return true;
747 }
748
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000749 case ISD::ROTL: {
Richard Sandiford5cbac962013-07-18 09:45:08 +0000750 // Any 64-bit rotate left can be merged into the RxSBG.
Richard Sandiford3e382972013-10-16 13:35:13 +0000751 if (RxSBG.BitSize != 64 || N.getValueType() != MVT::i64)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000752 return false;
Richard Sandiford21f5d682014-03-06 11:22:58 +0000753 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000754 if (!CountNode)
755 return false;
756
Richard Sandiford5cbac962013-07-18 09:45:08 +0000757 RxSBG.Rotate = (RxSBG.Rotate + CountNode->getZExtValue()) & 63;
758 RxSBG.Input = N.getOperand(0);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000759 return true;
760 }
761
Richard Sandiford220ee492013-12-20 11:49:48 +0000762 case ISD::ANY_EXTEND:
763 // Bits above the extended operand are don't-care.
764 RxSBG.Input = N.getOperand(0);
765 return true;
766
Richard Sandiford3875cb62014-01-09 11:28:53 +0000767 case ISD::ZERO_EXTEND:
768 if (RxSBG.Opcode != SystemZ::RNSBG) {
769 // Restrict the mask to the extended operand.
770 unsigned InnerBitSize = N.getOperand(0).getValueType().getSizeInBits();
771 if (!refineRxSBGMask(RxSBG, allOnes(InnerBitSize)))
772 return false;
Richard Sandiford220ee492013-12-20 11:49:48 +0000773
Richard Sandiford3875cb62014-01-09 11:28:53 +0000774 RxSBG.Input = N.getOperand(0);
775 return true;
776 }
777 // Fall through.
Richard Sandiford220ee492013-12-20 11:49:48 +0000778
779 case ISD::SIGN_EXTEND: {
Richard Sandiford3e382972013-10-16 13:35:13 +0000780 // Check that the extension bits are don't-care (i.e. are masked out
781 // by the final mask).
782 unsigned InnerBitSize = N.getOperand(0).getValueType().getSizeInBits();
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000783 if (maskMatters(RxSBG, allOnes(RxSBG.BitSize) - allOnes(InnerBitSize)))
Richard Sandiford3e382972013-10-16 13:35:13 +0000784 return false;
785
786 RxSBG.Input = N.getOperand(0);
787 return true;
788 }
789
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000790 case ISD::SHL: {
Richard Sandiford21f5d682014-03-06 11:22:58 +0000791 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000792 if (!CountNode)
793 return false;
794
795 uint64_t Count = CountNode->getZExtValue();
Richard Sandiford3e382972013-10-16 13:35:13 +0000796 unsigned BitSize = N.getValueType().getSizeInBits();
797 if (Count < 1 || Count >= BitSize)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000798 return false;
799
Richard Sandiford51093212013-07-18 10:40:35 +0000800 if (RxSBG.Opcode == SystemZ::RNSBG) {
801 // Treat (shl X, count) as (rotl X, size-count) as long as the bottom
802 // count bits from RxSBG.Input are ignored.
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000803 if (maskMatters(RxSBG, allOnes(Count)))
Richard Sandiford51093212013-07-18 10:40:35 +0000804 return false;
805 } else {
806 // Treat (shl X, count) as (and (rotl X, count), ~0<<count).
Richard Sandiford3e382972013-10-16 13:35:13 +0000807 if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count) << Count))
Richard Sandiford51093212013-07-18 10:40:35 +0000808 return false;
809 }
810
Richard Sandiford5cbac962013-07-18 09:45:08 +0000811 RxSBG.Rotate = (RxSBG.Rotate + Count) & 63;
812 RxSBG.Input = N.getOperand(0);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000813 return true;
814 }
815
Richard Sandiford297f7d22013-07-18 10:14:55 +0000816 case ISD::SRL:
817 case ISD::SRA: {
Richard Sandiford21f5d682014-03-06 11:22:58 +0000818 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000819 if (!CountNode)
820 return false;
821
822 uint64_t Count = CountNode->getZExtValue();
Richard Sandiford3e382972013-10-16 13:35:13 +0000823 unsigned BitSize = N.getValueType().getSizeInBits();
824 if (Count < 1 || Count >= BitSize)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000825 return false;
826
Richard Sandiford51093212013-07-18 10:40:35 +0000827 if (RxSBG.Opcode == SystemZ::RNSBG || Opcode == ISD::SRA) {
828 // Treat (srl|sra X, count) as (rotl X, size-count) as long as the top
829 // count bits from RxSBG.Input are ignored.
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000830 if (maskMatters(RxSBG, allOnes(Count) << (BitSize - Count)))
Richard Sandiford297f7d22013-07-18 10:14:55 +0000831 return false;
832 } else {
833 // Treat (srl X, count), mask) as (and (rotl X, size-count), ~0>>count),
834 // which is similar to SLL above.
Richard Sandiford3e382972013-10-16 13:35:13 +0000835 if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count)))
Richard Sandiford297f7d22013-07-18 10:14:55 +0000836 return false;
837 }
838
Richard Sandiford5cbac962013-07-18 09:45:08 +0000839 RxSBG.Rotate = (RxSBG.Rotate - Count) & 63;
840 RxSBG.Input = N.getOperand(0);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000841 return true;
842 }
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000843 default:
844 return false;
845 }
846}
847
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000848SDValue SystemZDAGToDAGISel::getUNDEF(SDLoc DL, EVT VT) const {
849 SDNode *N = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, VT);
Richard Sandiford84f54a32013-07-11 08:59:12 +0000850 return SDValue(N, 0);
851}
852
Richard Sandiford54b36912013-09-27 15:14:04 +0000853SDValue SystemZDAGToDAGISel::convertTo(SDLoc DL, EVT VT, SDValue N) const {
Richard Sandifordd8163202013-09-13 09:12:44 +0000854 if (N.getValueType() == MVT::i32 && VT == MVT::i64)
Richard Sandiford87a44362013-09-30 10:28:35 +0000855 return CurDAG->getTargetInsertSubreg(SystemZ::subreg_l32,
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000856 DL, VT, getUNDEF(DL, MVT::i64), N);
Richard Sandifordd8163202013-09-13 09:12:44 +0000857 if (N.getValueType() == MVT::i64 && VT == MVT::i32)
Richard Sandiford87a44362013-09-30 10:28:35 +0000858 return CurDAG->getTargetExtractSubreg(SystemZ::subreg_l32, DL, VT, N);
Richard Sandiford84f54a32013-07-11 08:59:12 +0000859 assert(N.getValueType() == VT && "Unexpected value types");
860 return N;
861}
862
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000863SDNode *SystemZDAGToDAGISel::tryRISBGZero(SDNode *N) {
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000864 EVT VT = N->getValueType(0);
Richard Sandiford51093212013-07-18 10:40:35 +0000865 RxSBGOperands RISBG(SystemZ::RISBG, SDValue(N, 0));
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000866 unsigned Count = 0;
Richard Sandiford5cbac962013-07-18 09:45:08 +0000867 while (expandRxSBG(RISBG))
Richard Sandiford3e382972013-10-16 13:35:13 +0000868 if (RISBG.Input.getOpcode() != ISD::ANY_EXTEND)
869 Count += 1;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000870 if (Count == 0)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000871 return 0;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000872 if (Count == 1) {
873 // Prefer to use normal shift instructions over RISBG, since they can handle
874 // all cases and are sometimes shorter.
875 if (N->getOpcode() != ISD::AND)
876 return 0;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000877
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000878 // Prefer register extensions like LLC over RISBG. Also prefer to start
879 // out with normal ANDs if one instruction would be enough. We can convert
880 // these ANDs into an RISBG later if a three-address instruction is useful.
881 if (VT == MVT::i32 ||
882 RISBG.Mask == 0xff ||
883 RISBG.Mask == 0xffff ||
884 SystemZ::isImmLF(~RISBG.Mask) ||
885 SystemZ::isImmHF(~RISBG.Mask)) {
886 // Force the new mask into the DAG, since it may include known-one bits.
Richard Sandiford21f5d682014-03-06 11:22:58 +0000887 auto *MaskN = cast<ConstantSDNode>(N->getOperand(1).getNode());
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000888 if (MaskN->getZExtValue() != RISBG.Mask) {
889 SDValue NewMask = CurDAG->getConstant(RISBG.Mask, VT);
890 N = CurDAG->UpdateNodeOperands(N, N->getOperand(0), NewMask);
891 return SelectCode(N);
892 }
893 return 0;
894 }
895 }
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000896
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000897 unsigned Opcode = SystemZ::RISBG;
898 EVT OpcodeVT = MVT::i64;
899 if (VT == MVT::i32 && Subtarget.hasHighWord()) {
900 Opcode = SystemZ::RISBMux;
901 OpcodeVT = MVT::i32;
902 RISBG.Start &= 31;
903 RISBG.End &= 31;
904 }
Richard Sandiford84f54a32013-07-11 08:59:12 +0000905 SDValue Ops[5] = {
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000906 getUNDEF(SDLoc(N), OpcodeVT),
907 convertTo(SDLoc(N), OpcodeVT, RISBG.Input),
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000908 CurDAG->getTargetConstant(RISBG.Start, MVT::i32),
909 CurDAG->getTargetConstant(RISBG.End | 128, MVT::i32),
910 CurDAG->getTargetConstant(RISBG.Rotate, MVT::i32)
Richard Sandiford84f54a32013-07-11 08:59:12 +0000911 };
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000912 N = CurDAG->getMachineNode(Opcode, SDLoc(N), OpcodeVT, Ops);
Richard Sandiford84f54a32013-07-11 08:59:12 +0000913 return convertTo(SDLoc(N), VT, SDValue(N, 0)).getNode();
914}
915
Richard Sandiford7878b852013-07-18 10:06:15 +0000916SDNode *SystemZDAGToDAGISel::tryRxSBG(SDNode *N, unsigned Opcode) {
917 // Try treating each operand of N as the second operand of the RxSBG
Richard Sandiford885140c2013-07-16 11:55:57 +0000918 // and see which goes deepest.
Richard Sandiford51093212013-07-18 10:40:35 +0000919 RxSBGOperands RxSBG[] = {
920 RxSBGOperands(Opcode, N->getOperand(0)),
921 RxSBGOperands(Opcode, N->getOperand(1))
922 };
Richard Sandiford885140c2013-07-16 11:55:57 +0000923 unsigned Count[] = { 0, 0 };
924 for (unsigned I = 0; I < 2; ++I)
Richard Sandiford5cbac962013-07-18 09:45:08 +0000925 while (expandRxSBG(RxSBG[I]))
Richard Sandiford3e382972013-10-16 13:35:13 +0000926 if (RxSBG[I].Input.getOpcode() != ISD::ANY_EXTEND)
927 Count[I] += 1;
Richard Sandiford885140c2013-07-16 11:55:57 +0000928
929 // Do nothing if neither operand is suitable.
930 if (Count[0] == 0 && Count[1] == 0)
931 return 0;
932
933 // Pick the deepest second operand.
934 unsigned I = Count[0] > Count[1] ? 0 : 1;
935 SDValue Op0 = N->getOperand(I ^ 1);
936
937 // Prefer IC for character insertions from memory.
Richard Sandiford7878b852013-07-18 10:06:15 +0000938 if (Opcode == SystemZ::ROSBG && (RxSBG[I].Mask & 0xff) == 0)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000939 if (auto *Load = dyn_cast<LoadSDNode>(Op0.getNode()))
Richard Sandiford885140c2013-07-16 11:55:57 +0000940 if (Load->getMemoryVT() == MVT::i8)
941 return 0;
942
943 // See whether we can avoid an AND in the first operand by converting
944 // ROSBG to RISBG.
Richard Sandiford7878b852013-07-18 10:06:15 +0000945 if (Opcode == SystemZ::ROSBG && detectOrAndInsertion(Op0, RxSBG[I].Mask))
Richard Sandiford885140c2013-07-16 11:55:57 +0000946 Opcode = SystemZ::RISBG;
947
948 EVT VT = N->getValueType(0);
949 SDValue Ops[5] = {
950 convertTo(SDLoc(N), MVT::i64, Op0),
Richard Sandiford5cbac962013-07-18 09:45:08 +0000951 convertTo(SDLoc(N), MVT::i64, RxSBG[I].Input),
952 CurDAG->getTargetConstant(RxSBG[I].Start, MVT::i32),
953 CurDAG->getTargetConstant(RxSBG[I].End, MVT::i32),
954 CurDAG->getTargetConstant(RxSBG[I].Rotate, MVT::i32)
Richard Sandiford885140c2013-07-16 11:55:57 +0000955 };
956 N = CurDAG->getMachineNode(Opcode, SDLoc(N), MVT::i64, Ops);
957 return convertTo(SDLoc(N), VT, SDValue(N, 0)).getNode();
958}
959
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000960SDNode *SystemZDAGToDAGISel::splitLargeImmediate(unsigned Opcode, SDNode *Node,
961 SDValue Op0, uint64_t UpperVal,
962 uint64_t LowerVal) {
963 EVT VT = Node->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000964 SDLoc DL(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000965 SDValue Upper = CurDAG->getConstant(UpperVal, VT);
966 if (Op0.getNode())
967 Upper = CurDAG->getNode(Opcode, DL, VT, Op0, Upper);
968 Upper = SDValue(Select(Upper.getNode()), 0);
969
970 SDValue Lower = CurDAG->getConstant(LowerVal, VT);
971 SDValue Or = CurDAG->getNode(Opcode, DL, VT, Upper, Lower);
972 return Or.getNode();
973}
974
Richard Sandiford067817e2013-09-27 15:29:20 +0000975bool SystemZDAGToDAGISel::canUseBlockOperation(StoreSDNode *Store,
976 LoadSDNode *Load) const {
Richard Sandiford178273a2013-09-05 10:36:45 +0000977 // Check that the two memory operands have the same size.
978 if (Load->getMemoryVT() != Store->getMemoryVT())
Richard Sandiford97846492013-07-09 09:46:39 +0000979 return false;
980
Richard Sandiford178273a2013-09-05 10:36:45 +0000981 // Volatility stops an access from being decomposed.
982 if (Load->isVolatile() || Store->isVolatile())
983 return false;
Richard Sandiford97846492013-07-09 09:46:39 +0000984
985 // There's no chance of overlap if the load is invariant.
986 if (Load->isInvariant())
987 return true;
988
Richard Sandiford97846492013-07-09 09:46:39 +0000989 // Otherwise we need to check whether there's an alias.
990 const Value *V1 = Load->getSrcValue();
991 const Value *V2 = Store->getSrcValue();
992 if (!V1 || !V2)
993 return false;
994
Richard Sandiford067817e2013-09-27 15:29:20 +0000995 // Reject equality.
996 uint64_t Size = Load->getMemoryVT().getStoreSize();
Richard Sandiford97846492013-07-09 09:46:39 +0000997 int64_t End1 = Load->getSrcValueOffset() + Size;
998 int64_t End2 = Store->getSrcValueOffset() + Size;
Richard Sandiford067817e2013-09-27 15:29:20 +0000999 if (V1 == V2 && End1 == End2)
1000 return false;
1001
Richard Sandiford97846492013-07-09 09:46:39 +00001002 return !AA->alias(AliasAnalysis::Location(V1, End1, Load->getTBAAInfo()),
1003 AliasAnalysis::Location(V2, End2, Store->getTBAAInfo()));
1004}
1005
Richard Sandiford178273a2013-09-05 10:36:45 +00001006bool SystemZDAGToDAGISel::storeLoadCanUseMVC(SDNode *N) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001007 auto *Store = cast<StoreSDNode>(N);
1008 auto *Load = cast<LoadSDNode>(Store->getValue());
Richard Sandiford178273a2013-09-05 10:36:45 +00001009
1010 // Prefer not to use MVC if either address can use ... RELATIVE LONG
1011 // instructions.
1012 uint64_t Size = Load->getMemoryVT().getStoreSize();
1013 if (Size > 1 && Size <= 8) {
1014 // Prefer LHRL, LRL and LGRL.
Richard Sandiford54b36912013-09-27 15:14:04 +00001015 if (SystemZISD::isPCREL(Load->getBasePtr().getOpcode()))
Richard Sandiford178273a2013-09-05 10:36:45 +00001016 return false;
1017 // Prefer STHRL, STRL and STGRL.
Richard Sandiford54b36912013-09-27 15:14:04 +00001018 if (SystemZISD::isPCREL(Store->getBasePtr().getOpcode()))
Richard Sandiford178273a2013-09-05 10:36:45 +00001019 return false;
1020 }
1021
Richard Sandiford067817e2013-09-27 15:29:20 +00001022 return canUseBlockOperation(Store, Load);
Richard Sandiford178273a2013-09-05 10:36:45 +00001023}
1024
1025bool SystemZDAGToDAGISel::storeLoadCanUseBlockBinary(SDNode *N,
1026 unsigned I) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001027 auto *StoreA = cast<StoreSDNode>(N);
1028 auto *LoadA = cast<LoadSDNode>(StoreA->getValue().getOperand(1 - I));
1029 auto *LoadB = cast<LoadSDNode>(StoreA->getValue().getOperand(I));
Richard Sandiford067817e2013-09-27 15:29:20 +00001030 return !LoadA->isVolatile() && canUseBlockOperation(StoreA, LoadB);
Richard Sandiford178273a2013-09-05 10:36:45 +00001031}
1032
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001033SDNode *SystemZDAGToDAGISel::Select(SDNode *Node) {
1034 // Dump information about the Node being selected
1035 DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n");
1036
1037 // If we have a custom node, we already have selected!
1038 if (Node->isMachineOpcode()) {
1039 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
Tim Northover31d093c2013-09-22 08:21:56 +00001040 Node->setNodeId(-1);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001041 return 0;
1042 }
1043
1044 unsigned Opcode = Node->getOpcode();
Richard Sandiford84f54a32013-07-11 08:59:12 +00001045 SDNode *ResNode = 0;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001046 switch (Opcode) {
1047 case ISD::OR:
Richard Sandiford885140c2013-07-16 11:55:57 +00001048 if (Node->getOperand(1).getOpcode() != ISD::Constant)
Richard Sandiford7878b852013-07-18 10:06:15 +00001049 ResNode = tryRxSBG(Node, SystemZ::ROSBG);
1050 goto or_xor;
1051
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001052 case ISD::XOR:
Richard Sandiford7878b852013-07-18 10:06:15 +00001053 if (Node->getOperand(1).getOpcode() != ISD::Constant)
1054 ResNode = tryRxSBG(Node, SystemZ::RXSBG);
1055 // Fall through.
1056 or_xor:
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001057 // If this is a 64-bit operation in which both 32-bit halves are nonzero,
1058 // split the operation into two.
Richard Sandiford885140c2013-07-16 11:55:57 +00001059 if (!ResNode && Node->getValueType(0) == MVT::i64)
Richard Sandiford21f5d682014-03-06 11:22:58 +00001060 if (auto *Op1 = dyn_cast<ConstantSDNode>(Node->getOperand(1))) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001061 uint64_t Val = Op1->getZExtValue();
1062 if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val))
1063 Node = splitLargeImmediate(Opcode, Node, Node->getOperand(0),
1064 Val - uint32_t(Val), uint32_t(Val));
1065 }
1066 break;
1067
Richard Sandiford84f54a32013-07-11 08:59:12 +00001068 case ISD::AND:
Richard Sandiford51093212013-07-18 10:40:35 +00001069 if (Node->getOperand(1).getOpcode() != ISD::Constant)
1070 ResNode = tryRxSBG(Node, SystemZ::RNSBG);
1071 // Fall through.
Richard Sandiford82ec87d2013-07-16 11:02:24 +00001072 case ISD::ROTL:
1073 case ISD::SHL:
1074 case ISD::SRL:
Richard Sandiford220ee492013-12-20 11:49:48 +00001075 case ISD::ZERO_EXTEND:
Richard Sandiford7878b852013-07-18 10:06:15 +00001076 if (!ResNode)
1077 ResNode = tryRISBGZero(Node);
Richard Sandiford84f54a32013-07-11 08:59:12 +00001078 break;
1079
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001080 case ISD::Constant:
1081 // If this is a 64-bit constant that is out of the range of LLILF,
1082 // LLIHF and LGFI, split it into two 32-bit pieces.
1083 if (Node->getValueType(0) == MVT::i64) {
1084 uint64_t Val = cast<ConstantSDNode>(Node)->getZExtValue();
1085 if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val) && !isInt<32>(Val))
1086 Node = splitLargeImmediate(ISD::OR, Node, SDValue(),
1087 Val - uint32_t(Val), uint32_t(Val));
1088 }
1089 break;
1090
Richard Sandifordee834382013-07-31 12:38:08 +00001091 case SystemZISD::SELECT_CCMASK: {
1092 SDValue Op0 = Node->getOperand(0);
1093 SDValue Op1 = Node->getOperand(1);
1094 // Prefer to put any load first, so that it can be matched as a
1095 // conditional load.
1096 if (Op1.getOpcode() == ISD::LOAD && Op0.getOpcode() != ISD::LOAD) {
1097 SDValue CCValid = Node->getOperand(2);
1098 SDValue CCMask = Node->getOperand(3);
1099 uint64_t ConstCCValid =
1100 cast<ConstantSDNode>(CCValid.getNode())->getZExtValue();
1101 uint64_t ConstCCMask =
1102 cast<ConstantSDNode>(CCMask.getNode())->getZExtValue();
1103 // Invert the condition.
1104 CCMask = CurDAG->getConstant(ConstCCValid ^ ConstCCMask,
1105 CCMask.getValueType());
1106 SDValue Op4 = Node->getOperand(4);
1107 Node = CurDAG->UpdateNodeOperands(Node, Op1, Op0, CCValid, CCMask, Op4);
1108 }
1109 break;
1110 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001111 }
1112
1113 // Select the default instruction
Richard Sandiford84f54a32013-07-11 08:59:12 +00001114 if (!ResNode)
1115 ResNode = SelectCode(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001116
1117 DEBUG(errs() << "=> ";
1118 if (ResNode == NULL || ResNode == Node)
1119 Node->dump(CurDAG);
1120 else
1121 ResNode->dump(CurDAG);
1122 errs() << "\n";
1123 );
1124 return ResNode;
1125}
1126
1127bool SystemZDAGToDAGISel::
1128SelectInlineAsmMemoryOperand(const SDValue &Op,
1129 char ConstraintCode,
1130 std::vector<SDValue> &OutOps) {
1131 assert(ConstraintCode == 'm' && "Unexpected constraint code");
1132 // Accept addresses with short displacements, which are compatible
1133 // with Q, R, S and T. But keep the index operand for future expansion.
1134 SDValue Base, Disp, Index;
1135 if (!selectBDXAddr(SystemZAddressingMode::FormBD,
1136 SystemZAddressingMode::Disp12Only,
1137 Op, Base, Disp, Index))
1138 return true;
1139 OutOps.push_back(Base);
1140 OutOps.push_back(Disp);
1141 OutOps.push_back(Index);
1142 return false;
1143}