blob: 63992936813d700f5f9980da4046e0804bca0447 [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 {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000134 return CurDAG->getTargetConstant(Imm, SDLoc(Node), Node->getValueType(0));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000135 }
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
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000258 // Try to match Addr as an address with a base, 12-bit displacement
259 // and index, where the index is element Elem of a vector.
260 // Return true on success, storing the base, displacement and vector
261 // in Base, Disp and Index respectively.
262 bool selectBDVAddr12Only(SDValue Addr, SDValue Elem, SDValue &Base,
263 SDValue &Disp, SDValue &Index) const;
264
Richard Sandiford885140c2013-07-16 11:55:57 +0000265 // Check whether (or Op (and X InsertMask)) is effectively an insertion
266 // of X into bits InsertMask of some Y != Op. Return true if so and
267 // set Op to that Y.
Richard Sandiford54b36912013-09-27 15:14:04 +0000268 bool detectOrAndInsertion(SDValue &Op, uint64_t InsertMask) const;
Richard Sandiford885140c2013-07-16 11:55:57 +0000269
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000270 // Try to update RxSBG so that only the bits of RxSBG.Input in Mask are used.
271 // Return true on success.
Richard Sandiford54b36912013-09-27 15:14:04 +0000272 bool refineRxSBGMask(RxSBGOperands &RxSBG, uint64_t Mask) const;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000273
Richard Sandiford5cbac962013-07-18 09:45:08 +0000274 // Try to fold some of RxSBG.Input into other fields of RxSBG.
275 // Return true on success.
Richard Sandiford54b36912013-09-27 15:14:04 +0000276 bool expandRxSBG(RxSBGOperands &RxSBG) const;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000277
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000278 // Return an undefined value of type VT.
279 SDValue getUNDEF(SDLoc DL, EVT VT) const;
Richard Sandiford84f54a32013-07-11 08:59:12 +0000280
281 // Convert N to VT, if it isn't already.
Richard Sandiford54b36912013-09-27 15:14:04 +0000282 SDValue convertTo(SDLoc DL, EVT VT, SDValue N) const;
Richard Sandiford84f54a32013-07-11 08:59:12 +0000283
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000284 // Try to implement AND or shift node N using RISBG with the zero flag set.
285 // Return the selected node on success, otherwise return null.
286 SDNode *tryRISBGZero(SDNode *N);
Richard Sandiford84f54a32013-07-11 08:59:12 +0000287
Richard Sandiford7878b852013-07-18 10:06:15 +0000288 // Try to use RISBG or Opcode to implement OR or XOR node N.
289 // Return the selected node on success, otherwise return null.
290 SDNode *tryRxSBG(SDNode *N, unsigned Opcode);
Richard Sandiford885140c2013-07-16 11:55:57 +0000291
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000292 // If Op0 is null, then Node is a constant that can be loaded using:
293 //
294 // (Opcode UpperVal LowerVal)
295 //
296 // If Op0 is nonnull, then Node can be implemented using:
297 //
298 // (Opcode (Opcode Op0 UpperVal) LowerVal)
299 SDNode *splitLargeImmediate(unsigned Opcode, SDNode *Node, SDValue Op0,
300 uint64_t UpperVal, uint64_t LowerVal);
301
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000302 // Try to use gather instruction Opcode to implement vector insertion N.
303 SDNode *tryGather(SDNode *N, unsigned Opcode);
304
305 // Try to use scatter instruction Opcode to implement store Store.
306 SDNode *tryScatter(StoreSDNode *Store, unsigned Opcode);
307
Richard Sandiford067817e2013-09-27 15:29:20 +0000308 // Return true if Load and Store are loads and stores of the same size
309 // and are guaranteed not to overlap. Such operations can be implemented
310 // using block (SS-format) instructions.
311 //
312 // Partial overlap would lead to incorrect code, since the block operations
313 // are logically bytewise, even though they have a fast path for the
314 // non-overlapping case. We also need to avoid full overlap (i.e. two
315 // addresses that might be equal at run time) because although that case
316 // would be handled correctly, it might be implemented by millicode.
317 bool canUseBlockOperation(StoreSDNode *Store, LoadSDNode *Load) const;
318
Richard Sandiford178273a2013-09-05 10:36:45 +0000319 // N is a (store (load Y), X) pattern. Return true if it can use an MVC
320 // from Y to X.
Richard Sandiford97846492013-07-09 09:46:39 +0000321 bool storeLoadCanUseMVC(SDNode *N) const;
322
Richard Sandiford178273a2013-09-05 10:36:45 +0000323 // N is a (store (op (load A[0]), (load A[1])), X) pattern. Return true
324 // if A[1 - I] == X and if N can use a block operation like NC from A[I]
325 // to X.
326 bool storeLoadCanUseBlockBinary(SDNode *N, unsigned I) const;
327
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000328public:
329 SystemZDAGToDAGISel(SystemZTargetMachine &TM, CodeGenOpt::Level OptLevel)
Eric Christophera6734172015-01-31 00:06:45 +0000330 : SelectionDAGISel(TM, OptLevel) {}
331
332 bool runOnMachineFunction(MachineFunction &MF) override {
333 Subtarget = &MF.getSubtarget<SystemZSubtarget>();
334 return SelectionDAGISel::runOnMachineFunction(MF);
335 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000336
337 // Override MachineFunctionPass.
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000338 const char *getPassName() const override {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000339 return "SystemZ DAG->DAG Pattern Instruction Selection";
340 }
341
342 // Override SelectionDAGISel.
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000343 SDNode *Select(SDNode *Node) override;
Daniel Sanders60f1db02015-03-13 12:45:09 +0000344 bool SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000345 std::vector<SDValue> &OutOps) override;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000346
347 // Include the pieces autogenerated from the target description.
348 #include "SystemZGenDAGISel.inc"
349};
350} // end anonymous namespace
351
352FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM,
353 CodeGenOpt::Level OptLevel) {
354 return new SystemZDAGToDAGISel(TM, OptLevel);
355}
356
357// Return true if Val should be selected as a displacement for an address
358// with range DR. Here we're interested in the range of both the instruction
359// described by DR and of any pairing instruction.
360static bool selectDisp(SystemZAddressingMode::DispRange DR, int64_t Val) {
361 switch (DR) {
362 case SystemZAddressingMode::Disp12Only:
363 return isUInt<12>(Val);
364
365 case SystemZAddressingMode::Disp12Pair:
366 case SystemZAddressingMode::Disp20Only:
367 case SystemZAddressingMode::Disp20Pair:
368 return isInt<20>(Val);
369
370 case SystemZAddressingMode::Disp20Only128:
371 return isInt<20>(Val) && isInt<20>(Val + 8);
372 }
373 llvm_unreachable("Unhandled displacement range");
374}
375
376// Change the base or index in AM to Value, where IsBase selects
377// between the base and index.
378static void changeComponent(SystemZAddressingMode &AM, bool IsBase,
379 SDValue Value) {
380 if (IsBase)
381 AM.Base = Value;
382 else
383 AM.Index = Value;
384}
385
386// The base or index of AM is equivalent to Value + ADJDYNALLOC,
387// where IsBase selects between the base and index. Try to fold the
388// ADJDYNALLOC into AM.
389static bool expandAdjDynAlloc(SystemZAddressingMode &AM, bool IsBase,
390 SDValue Value) {
391 if (AM.isDynAlloc() && !AM.IncludesDynAlloc) {
392 changeComponent(AM, IsBase, Value);
393 AM.IncludesDynAlloc = true;
394 return true;
395 }
396 return false;
397}
398
399// The base of AM is equivalent to Base + Index. Try to use Index as
400// the index register.
401static bool expandIndex(SystemZAddressingMode &AM, SDValue Base,
402 SDValue Index) {
403 if (AM.hasIndexField() && !AM.Index.getNode()) {
404 AM.Base = Base;
405 AM.Index = Index;
406 return true;
407 }
408 return false;
409}
410
411// The base or index of AM is equivalent to Op0 + Op1, where IsBase selects
412// between the base and index. Try to fold Op1 into AM's displacement.
413static bool expandDisp(SystemZAddressingMode &AM, bool IsBase,
Richard Sandiford54b36912013-09-27 15:14:04 +0000414 SDValue Op0, uint64_t Op1) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000415 // First try adjusting the displacement.
Richard Sandiford54b36912013-09-27 15:14:04 +0000416 int64_t TestDisp = AM.Disp + Op1;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000417 if (selectDisp(AM.DR, TestDisp)) {
418 changeComponent(AM, IsBase, Op0);
419 AM.Disp = TestDisp;
420 return true;
421 }
422
423 // We could consider forcing the displacement into a register and
424 // using it as an index, but it would need to be carefully tuned.
425 return false;
426}
427
428bool SystemZDAGToDAGISel::expandAddress(SystemZAddressingMode &AM,
Richard Sandiford54b36912013-09-27 15:14:04 +0000429 bool IsBase) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000430 SDValue N = IsBase ? AM.Base : AM.Index;
431 unsigned Opcode = N.getOpcode();
432 if (Opcode == ISD::TRUNCATE) {
433 N = N.getOperand(0);
434 Opcode = N.getOpcode();
435 }
436 if (Opcode == ISD::ADD || CurDAG->isBaseWithConstantOffset(N)) {
437 SDValue Op0 = N.getOperand(0);
438 SDValue Op1 = N.getOperand(1);
439
440 unsigned Op0Code = Op0->getOpcode();
441 unsigned Op1Code = Op1->getOpcode();
442
443 if (Op0Code == SystemZISD::ADJDYNALLOC)
444 return expandAdjDynAlloc(AM, IsBase, Op1);
445 if (Op1Code == SystemZISD::ADJDYNALLOC)
446 return expandAdjDynAlloc(AM, IsBase, Op0);
447
448 if (Op0Code == ISD::Constant)
Richard Sandiford54b36912013-09-27 15:14:04 +0000449 return expandDisp(AM, IsBase, Op1,
450 cast<ConstantSDNode>(Op0)->getSExtValue());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000451 if (Op1Code == ISD::Constant)
Richard Sandiford54b36912013-09-27 15:14:04 +0000452 return expandDisp(AM, IsBase, Op0,
453 cast<ConstantSDNode>(Op1)->getSExtValue());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000454
455 if (IsBase && expandIndex(AM, Op0, Op1))
456 return true;
457 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000458 if (Opcode == SystemZISD::PCREL_OFFSET) {
459 SDValue Full = N.getOperand(0);
460 SDValue Base = N.getOperand(1);
461 SDValue Anchor = Base.getOperand(0);
462 uint64_t Offset = (cast<GlobalAddressSDNode>(Full)->getOffset() -
463 cast<GlobalAddressSDNode>(Anchor)->getOffset());
464 return expandDisp(AM, IsBase, Base, Offset);
465 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000466 return false;
467}
468
469// Return true if an instruction with displacement range DR should be
470// used for displacement value Val. selectDisp(DR, Val) must already hold.
471static bool isValidDisp(SystemZAddressingMode::DispRange DR, int64_t Val) {
472 assert(selectDisp(DR, Val) && "Invalid displacement");
473 switch (DR) {
474 case SystemZAddressingMode::Disp12Only:
475 case SystemZAddressingMode::Disp20Only:
476 case SystemZAddressingMode::Disp20Only128:
477 return true;
478
479 case SystemZAddressingMode::Disp12Pair:
480 // Use the other instruction if the displacement is too large.
481 return isUInt<12>(Val);
482
483 case SystemZAddressingMode::Disp20Pair:
484 // Use the other instruction if the displacement is small enough.
485 return !isUInt<12>(Val);
486 }
487 llvm_unreachable("Unhandled displacement range");
488}
489
490// Return true if Base + Disp + Index should be performed by LA(Y).
491static bool shouldUseLA(SDNode *Base, int64_t Disp, SDNode *Index) {
492 // Don't use LA(Y) for constants.
493 if (!Base)
494 return false;
495
496 // Always use LA(Y) for frame addresses, since we know that the destination
497 // register is almost always (perhaps always) going to be different from
498 // the frame register.
499 if (Base->getOpcode() == ISD::FrameIndex)
500 return true;
501
502 if (Disp) {
503 // Always use LA(Y) if there is a base, displacement and index.
504 if (Index)
505 return true;
506
507 // Always use LA if the displacement is small enough. It should always
508 // be no worse than AGHI (and better if it avoids a move).
509 if (isUInt<12>(Disp))
510 return true;
511
512 // For similar reasons, always use LAY if the constant is too big for AGHI.
513 // LAY should be no worse than AGFI.
514 if (!isInt<16>(Disp))
515 return true;
516 } else {
517 // Don't use LA for plain registers.
518 if (!Index)
519 return false;
520
521 // Don't use LA for plain addition if the index operand is only used
522 // once. It should be a natural two-operand addition in that case.
523 if (Index->hasOneUse())
524 return false;
525
526 // Prefer addition if the second operation is sign-extended, in the
527 // hope of using AGF.
528 unsigned IndexOpcode = Index->getOpcode();
529 if (IndexOpcode == ISD::SIGN_EXTEND ||
530 IndexOpcode == ISD::SIGN_EXTEND_INREG)
531 return false;
532 }
533
534 // Don't use LA for two-operand addition if either operand is only
535 // used once. The addition instructions are better in that case.
536 if (Base->hasOneUse())
537 return false;
538
539 return true;
540}
541
542// Return true if Addr is suitable for AM, updating AM if so.
543bool SystemZDAGToDAGISel::selectAddress(SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000544 SystemZAddressingMode &AM) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000545 // Start out assuming that the address will need to be loaded separately,
546 // then try to extend it as much as we can.
547 AM.Base = Addr;
548
549 // First try treating the address as a constant.
550 if (Addr.getOpcode() == ISD::Constant &&
Richard Sandiford54b36912013-09-27 15:14:04 +0000551 expandDisp(AM, true, SDValue(),
552 cast<ConstantSDNode>(Addr)->getSExtValue()))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000553 ;
554 else
555 // Otherwise try expanding each component.
556 while (expandAddress(AM, true) ||
557 (AM.Index.getNode() && expandAddress(AM, false)))
558 continue;
559
560 // Reject cases where it isn't profitable to use LA(Y).
561 if (AM.Form == SystemZAddressingMode::FormBDXLA &&
562 !shouldUseLA(AM.Base.getNode(), AM.Disp, AM.Index.getNode()))
563 return false;
564
565 // Reject cases where the other instruction in a pair should be used.
566 if (!isValidDisp(AM.DR, AM.Disp))
567 return false;
568
569 // Make sure that ADJDYNALLOC is included where necessary.
570 if (AM.isDynAlloc() && !AM.IncludesDynAlloc)
571 return false;
572
573 DEBUG(AM.dump());
574 return true;
575}
576
577// Insert a node into the DAG at least before Pos. This will reposition
578// the node as needed, and will assign it a node ID that is <= Pos's ID.
579// Note that this does *not* preserve the uniqueness of node IDs!
580// The selection DAG must no longer depend on their uniqueness when this
581// function is used.
582static void insertDAGNode(SelectionDAG *DAG, SDNode *Pos, SDValue N) {
583 if (N.getNode()->getNodeId() == -1 ||
584 N.getNode()->getNodeId() > Pos->getNodeId()) {
585 DAG->RepositionNode(Pos, N.getNode());
586 N.getNode()->setNodeId(Pos->getNodeId());
587 }
588}
589
590void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
591 EVT VT, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000592 SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000593 Base = AM.Base;
594 if (!Base.getNode())
595 // Register 0 means "no base". This is mostly useful for shifts.
596 Base = CurDAG->getRegister(0, VT);
597 else if (Base.getOpcode() == ISD::FrameIndex) {
598 // Lower a FrameIndex to a TargetFrameIndex.
599 int64_t FrameIndex = cast<FrameIndexSDNode>(Base)->getIndex();
600 Base = CurDAG->getTargetFrameIndex(FrameIndex, VT);
601 } else if (Base.getValueType() != VT) {
602 // Truncate values from i64 to i32, for shifts.
603 assert(VT == MVT::i32 && Base.getValueType() == MVT::i64 &&
604 "Unexpected truncation");
Andrew Trickef9de2a2013-05-25 02:42:55 +0000605 SDLoc DL(Base);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000606 SDValue Trunc = CurDAG->getNode(ISD::TRUNCATE, DL, VT, Base);
607 insertDAGNode(CurDAG, Base.getNode(), Trunc);
608 Base = Trunc;
609 }
610
611 // Lower the displacement to a TargetConstant.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000612 Disp = CurDAG->getTargetConstant(AM.Disp, SDLoc(Base), VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000613}
614
615void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
616 EVT VT, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000617 SDValue &Disp,
618 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000619 getAddressOperands(AM, VT, Base, Disp);
620
621 Index = AM.Index;
622 if (!Index.getNode())
623 // Register 0 means "no index".
624 Index = CurDAG->getRegister(0, VT);
625}
626
627bool SystemZDAGToDAGISel::selectBDAddr(SystemZAddressingMode::DispRange DR,
628 SDValue Addr, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000629 SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000630 SystemZAddressingMode AM(SystemZAddressingMode::FormBD, DR);
631 if (!selectAddress(Addr, AM))
632 return false;
633
634 getAddressOperands(AM, Addr.getValueType(), Base, Disp);
635 return true;
636}
637
Richard Sandiforda481f582013-08-23 11:18:53 +0000638bool SystemZDAGToDAGISel::selectMVIAddr(SystemZAddressingMode::DispRange DR,
639 SDValue Addr, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000640 SDValue &Disp) const {
Richard Sandiforda481f582013-08-23 11:18:53 +0000641 SystemZAddressingMode AM(SystemZAddressingMode::FormBDXNormal, DR);
642 if (!selectAddress(Addr, AM) || AM.Index.getNode())
643 return false;
644
645 getAddressOperands(AM, Addr.getValueType(), Base, Disp);
646 return true;
647}
648
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000649bool SystemZDAGToDAGISel::selectBDXAddr(SystemZAddressingMode::AddrForm Form,
650 SystemZAddressingMode::DispRange DR,
651 SDValue Addr, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000652 SDValue &Disp, SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000653 SystemZAddressingMode AM(Form, DR);
654 if (!selectAddress(Addr, AM))
655 return false;
656
657 getAddressOperands(AM, Addr.getValueType(), Base, Disp, Index);
658 return true;
659}
660
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000661bool SystemZDAGToDAGISel::selectBDVAddr12Only(SDValue Addr, SDValue Elem,
662 SDValue &Base,
663 SDValue &Disp,
664 SDValue &Index) const {
665 SDValue Regs[2];
666 if (selectBDXAddr12Only(Addr, Regs[0], Disp, Regs[1]) &&
667 Regs[0].getNode() && Regs[1].getNode()) {
668 for (unsigned int I = 0; I < 2; ++I) {
669 Base = Regs[I];
670 Index = Regs[1 - I];
671 // We can't tell here whether the index vector has the right type
672 // for the access; the caller needs to do that instead.
673 if (Index.getOpcode() == ISD::ZERO_EXTEND)
674 Index = Index.getOperand(0);
675 if (Index.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
676 Index.getOperand(1) == Elem) {
677 Index = Index.getOperand(0);
678 return true;
679 }
680 }
681 }
682 return false;
683}
684
Richard Sandiford885140c2013-07-16 11:55:57 +0000685bool SystemZDAGToDAGISel::detectOrAndInsertion(SDValue &Op,
Richard Sandiford54b36912013-09-27 15:14:04 +0000686 uint64_t InsertMask) const {
Richard Sandiford885140c2013-07-16 11:55:57 +0000687 // We're only interested in cases where the insertion is into some operand
688 // of Op, rather than into Op itself. The only useful case is an AND.
689 if (Op.getOpcode() != ISD::AND)
690 return false;
691
692 // We need a constant mask.
Richard Sandiford21f5d682014-03-06 11:22:58 +0000693 auto *MaskNode = dyn_cast<ConstantSDNode>(Op.getOperand(1).getNode());
Richard Sandiford885140c2013-07-16 11:55:57 +0000694 if (!MaskNode)
695 return false;
696
697 // It's not an insertion of Op.getOperand(0) if the two masks overlap.
698 uint64_t AndMask = MaskNode->getZExtValue();
699 if (InsertMask & AndMask)
700 return false;
701
702 // It's only an insertion if all bits are covered or are known to be zero.
703 // The inner check covers all cases but is more expensive.
704 uint64_t Used = allOnes(Op.getValueType().getSizeInBits());
705 if (Used != (AndMask | InsertMask)) {
706 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +0000707 CurDAG->computeKnownBits(Op.getOperand(0), KnownZero, KnownOne);
Richard Sandiford885140c2013-07-16 11:55:57 +0000708 if (Used != (AndMask | InsertMask | KnownZero.getZExtValue()))
709 return false;
710 }
711
712 Op = Op.getOperand(0);
713 return true;
714}
715
Richard Sandiford54b36912013-09-27 15:14:04 +0000716bool SystemZDAGToDAGISel::refineRxSBGMask(RxSBGOperands &RxSBG,
717 uint64_t Mask) const {
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000718 const SystemZInstrInfo *TII = getInstrInfo();
Richard Sandiford5cbac962013-07-18 09:45:08 +0000719 if (RxSBG.Rotate != 0)
720 Mask = (Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate));
721 Mask &= RxSBG.Mask;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000722 if (TII->isRxSBGMask(Mask, RxSBG.BitSize, RxSBG.Start, RxSBG.End)) {
Richard Sandiford5cbac962013-07-18 09:45:08 +0000723 RxSBG.Mask = Mask;
Richard Sandiford5cbac962013-07-18 09:45:08 +0000724 return true;
725 }
Richard Sandiford84f54a32013-07-11 08:59:12 +0000726 return false;
727}
728
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000729// Return true if any bits of (RxSBG.Input & Mask) are significant.
730static bool maskMatters(RxSBGOperands &RxSBG, uint64_t Mask) {
731 // Rotate the mask in the same way as RxSBG.Input is rotated.
Richard Sandiford297f7d22013-07-18 10:14:55 +0000732 if (RxSBG.Rotate != 0)
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000733 Mask = ((Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate)));
734 return (Mask & RxSBG.Mask) != 0;
Richard Sandiford297f7d22013-07-18 10:14:55 +0000735}
736
Richard Sandiford54b36912013-09-27 15:14:04 +0000737bool SystemZDAGToDAGISel::expandRxSBG(RxSBGOperands &RxSBG) const {
Richard Sandiford5cbac962013-07-18 09:45:08 +0000738 SDValue N = RxSBG.Input;
Richard Sandiford297f7d22013-07-18 10:14:55 +0000739 unsigned Opcode = N.getOpcode();
740 switch (Opcode) {
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000741 case ISD::AND: {
Richard Sandiford51093212013-07-18 10:40:35 +0000742 if (RxSBG.Opcode == SystemZ::RNSBG)
743 return false;
744
Richard Sandiford21f5d682014-03-06 11:22:58 +0000745 auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000746 if (!MaskNode)
747 return false;
748
749 SDValue Input = N.getOperand(0);
750 uint64_t Mask = MaskNode->getZExtValue();
Richard Sandiford5cbac962013-07-18 09:45:08 +0000751 if (!refineRxSBGMask(RxSBG, Mask)) {
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000752 // If some bits of Input are already known zeros, those bits will have
753 // been removed from the mask. See if adding them back in makes the
754 // mask suitable.
755 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +0000756 CurDAG->computeKnownBits(Input, KnownZero, KnownOne);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000757 Mask |= KnownZero.getZExtValue();
Richard Sandiford5cbac962013-07-18 09:45:08 +0000758 if (!refineRxSBGMask(RxSBG, Mask))
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000759 return false;
760 }
Richard Sandiford5cbac962013-07-18 09:45:08 +0000761 RxSBG.Input = Input;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000762 return true;
763 }
764
Richard Sandiford51093212013-07-18 10:40:35 +0000765 case ISD::OR: {
766 if (RxSBG.Opcode != SystemZ::RNSBG)
767 return false;
768
Richard Sandiford21f5d682014-03-06 11:22:58 +0000769 auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford51093212013-07-18 10:40:35 +0000770 if (!MaskNode)
771 return false;
772
773 SDValue Input = N.getOperand(0);
774 uint64_t Mask = ~MaskNode->getZExtValue();
775 if (!refineRxSBGMask(RxSBG, Mask)) {
776 // If some bits of Input are already known ones, those bits will have
777 // been removed from the mask. See if adding them back in makes the
778 // mask suitable.
779 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +0000780 CurDAG->computeKnownBits(Input, KnownZero, KnownOne);
Richard Sandiford51093212013-07-18 10:40:35 +0000781 Mask &= ~KnownOne.getZExtValue();
782 if (!refineRxSBGMask(RxSBG, Mask))
783 return false;
784 }
785 RxSBG.Input = Input;
786 return true;
787 }
788
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000789 case ISD::ROTL: {
Richard Sandiford5cbac962013-07-18 09:45:08 +0000790 // Any 64-bit rotate left can be merged into the RxSBG.
Richard Sandiford3e382972013-10-16 13:35:13 +0000791 if (RxSBG.BitSize != 64 || N.getValueType() != MVT::i64)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000792 return false;
Richard Sandiford21f5d682014-03-06 11:22:58 +0000793 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000794 if (!CountNode)
795 return false;
796
Richard Sandiford5cbac962013-07-18 09:45:08 +0000797 RxSBG.Rotate = (RxSBG.Rotate + CountNode->getZExtValue()) & 63;
798 RxSBG.Input = N.getOperand(0);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000799 return true;
800 }
801
Richard Sandiford220ee492013-12-20 11:49:48 +0000802 case ISD::ANY_EXTEND:
803 // Bits above the extended operand are don't-care.
804 RxSBG.Input = N.getOperand(0);
805 return true;
806
Richard Sandiford3875cb62014-01-09 11:28:53 +0000807 case ISD::ZERO_EXTEND:
808 if (RxSBG.Opcode != SystemZ::RNSBG) {
809 // Restrict the mask to the extended operand.
810 unsigned InnerBitSize = N.getOperand(0).getValueType().getSizeInBits();
811 if (!refineRxSBGMask(RxSBG, allOnes(InnerBitSize)))
812 return false;
Richard Sandiford220ee492013-12-20 11:49:48 +0000813
Richard Sandiford3875cb62014-01-09 11:28:53 +0000814 RxSBG.Input = N.getOperand(0);
815 return true;
816 }
817 // Fall through.
Richard Sandiford220ee492013-12-20 11:49:48 +0000818
819 case ISD::SIGN_EXTEND: {
Richard Sandiford3e382972013-10-16 13:35:13 +0000820 // Check that the extension bits are don't-care (i.e. are masked out
821 // by the final mask).
822 unsigned InnerBitSize = N.getOperand(0).getValueType().getSizeInBits();
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000823 if (maskMatters(RxSBG, allOnes(RxSBG.BitSize) - allOnes(InnerBitSize)))
Richard Sandiford3e382972013-10-16 13:35:13 +0000824 return false;
825
826 RxSBG.Input = N.getOperand(0);
827 return true;
828 }
829
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000830 case ISD::SHL: {
Richard Sandiford21f5d682014-03-06 11:22:58 +0000831 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000832 if (!CountNode)
833 return false;
834
835 uint64_t Count = CountNode->getZExtValue();
Richard Sandiford3e382972013-10-16 13:35:13 +0000836 unsigned BitSize = N.getValueType().getSizeInBits();
837 if (Count < 1 || Count >= BitSize)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000838 return false;
839
Richard Sandiford51093212013-07-18 10:40:35 +0000840 if (RxSBG.Opcode == SystemZ::RNSBG) {
841 // Treat (shl X, count) as (rotl X, size-count) as long as the bottom
842 // count bits from RxSBG.Input are ignored.
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000843 if (maskMatters(RxSBG, allOnes(Count)))
Richard Sandiford51093212013-07-18 10:40:35 +0000844 return false;
845 } else {
846 // Treat (shl X, count) as (and (rotl X, count), ~0<<count).
Richard Sandiford3e382972013-10-16 13:35:13 +0000847 if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count) << Count))
Richard Sandiford51093212013-07-18 10:40:35 +0000848 return false;
849 }
850
Richard Sandiford5cbac962013-07-18 09:45:08 +0000851 RxSBG.Rotate = (RxSBG.Rotate + Count) & 63;
852 RxSBG.Input = N.getOperand(0);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000853 return true;
854 }
855
Richard Sandiford297f7d22013-07-18 10:14:55 +0000856 case ISD::SRL:
857 case ISD::SRA: {
Richard Sandiford21f5d682014-03-06 11:22:58 +0000858 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000859 if (!CountNode)
860 return false;
861
862 uint64_t Count = CountNode->getZExtValue();
Richard Sandiford3e382972013-10-16 13:35:13 +0000863 unsigned BitSize = N.getValueType().getSizeInBits();
864 if (Count < 1 || Count >= BitSize)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000865 return false;
866
Richard Sandiford51093212013-07-18 10:40:35 +0000867 if (RxSBG.Opcode == SystemZ::RNSBG || Opcode == ISD::SRA) {
868 // Treat (srl|sra X, count) as (rotl X, size-count) as long as the top
869 // count bits from RxSBG.Input are ignored.
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000870 if (maskMatters(RxSBG, allOnes(Count) << (BitSize - Count)))
Richard Sandiford297f7d22013-07-18 10:14:55 +0000871 return false;
872 } else {
873 // Treat (srl X, count), mask) as (and (rotl X, size-count), ~0>>count),
874 // which is similar to SLL above.
Richard Sandiford3e382972013-10-16 13:35:13 +0000875 if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count)))
Richard Sandiford297f7d22013-07-18 10:14:55 +0000876 return false;
877 }
878
Richard Sandiford5cbac962013-07-18 09:45:08 +0000879 RxSBG.Rotate = (RxSBG.Rotate - Count) & 63;
880 RxSBG.Input = N.getOperand(0);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000881 return true;
882 }
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000883 default:
884 return false;
885 }
886}
887
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000888SDValue SystemZDAGToDAGISel::getUNDEF(SDLoc DL, EVT VT) const {
889 SDNode *N = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, VT);
Richard Sandiford84f54a32013-07-11 08:59:12 +0000890 return SDValue(N, 0);
891}
892
Richard Sandiford54b36912013-09-27 15:14:04 +0000893SDValue SystemZDAGToDAGISel::convertTo(SDLoc DL, EVT VT, SDValue N) const {
Richard Sandifordd8163202013-09-13 09:12:44 +0000894 if (N.getValueType() == MVT::i32 && VT == MVT::i64)
Richard Sandiford87a44362013-09-30 10:28:35 +0000895 return CurDAG->getTargetInsertSubreg(SystemZ::subreg_l32,
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000896 DL, VT, getUNDEF(DL, MVT::i64), N);
Richard Sandifordd8163202013-09-13 09:12:44 +0000897 if (N.getValueType() == MVT::i64 && VT == MVT::i32)
Richard Sandiford87a44362013-09-30 10:28:35 +0000898 return CurDAG->getTargetExtractSubreg(SystemZ::subreg_l32, DL, VT, N);
Richard Sandiford84f54a32013-07-11 08:59:12 +0000899 assert(N.getValueType() == VT && "Unexpected value types");
900 return N;
901}
902
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000903SDNode *SystemZDAGToDAGISel::tryRISBGZero(SDNode *N) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000904 SDLoc DL(N);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000905 EVT VT = N->getValueType(0);
Richard Sandiford51093212013-07-18 10:40:35 +0000906 RxSBGOperands RISBG(SystemZ::RISBG, SDValue(N, 0));
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000907 unsigned Count = 0;
Richard Sandiford5cbac962013-07-18 09:45:08 +0000908 while (expandRxSBG(RISBG))
Richard Sandiford3e382972013-10-16 13:35:13 +0000909 if (RISBG.Input.getOpcode() != ISD::ANY_EXTEND)
910 Count += 1;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000911 if (Count == 0)
Craig Topper062a2ba2014-04-25 05:30:21 +0000912 return nullptr;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000913 if (Count == 1) {
914 // Prefer to use normal shift instructions over RISBG, since they can handle
915 // all cases and are sometimes shorter.
916 if (N->getOpcode() != ISD::AND)
Craig Topper062a2ba2014-04-25 05:30:21 +0000917 return nullptr;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000918
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000919 // Prefer register extensions like LLC over RISBG. Also prefer to start
920 // out with normal ANDs if one instruction would be enough. We can convert
921 // these ANDs into an RISBG later if a three-address instruction is useful.
922 if (VT == MVT::i32 ||
923 RISBG.Mask == 0xff ||
924 RISBG.Mask == 0xffff ||
925 SystemZ::isImmLF(~RISBG.Mask) ||
926 SystemZ::isImmHF(~RISBG.Mask)) {
927 // Force the new mask into the DAG, since it may include known-one bits.
Richard Sandiford21f5d682014-03-06 11:22:58 +0000928 auto *MaskN = cast<ConstantSDNode>(N->getOperand(1).getNode());
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000929 if (MaskN->getZExtValue() != RISBG.Mask) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000930 SDValue NewMask = CurDAG->getConstant(RISBG.Mask, DL, VT);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000931 N = CurDAG->UpdateNodeOperands(N, N->getOperand(0), NewMask);
932 return SelectCode(N);
933 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000934 return nullptr;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000935 }
936 }
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000937
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000938 unsigned Opcode = SystemZ::RISBG;
Ulrich Weigand371d10a2015-03-31 12:58:17 +0000939 // Prefer RISBGN if available, since it does not clobber CC.
940 if (Subtarget->hasMiscellaneousExtensions())
941 Opcode = SystemZ::RISBGN;
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000942 EVT OpcodeVT = MVT::i64;
Eric Christophera6734172015-01-31 00:06:45 +0000943 if (VT == MVT::i32 && Subtarget->hasHighWord()) {
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000944 Opcode = SystemZ::RISBMux;
945 OpcodeVT = MVT::i32;
946 RISBG.Start &= 31;
947 RISBG.End &= 31;
948 }
Richard Sandiford84f54a32013-07-11 08:59:12 +0000949 SDValue Ops[5] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000950 getUNDEF(DL, OpcodeVT),
951 convertTo(DL, OpcodeVT, RISBG.Input),
952 CurDAG->getTargetConstant(RISBG.Start, DL, MVT::i32),
953 CurDAG->getTargetConstant(RISBG.End | 128, DL, MVT::i32),
954 CurDAG->getTargetConstant(RISBG.Rotate, DL, MVT::i32)
Richard Sandiford84f54a32013-07-11 08:59:12 +0000955 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000956 N = CurDAG->getMachineNode(Opcode, DL, OpcodeVT, Ops);
957 return convertTo(DL, VT, SDValue(N, 0)).getNode();
Richard Sandiford84f54a32013-07-11 08:59:12 +0000958}
959
Richard Sandiford7878b852013-07-18 10:06:15 +0000960SDNode *SystemZDAGToDAGISel::tryRxSBG(SDNode *N, unsigned Opcode) {
961 // Try treating each operand of N as the second operand of the RxSBG
Richard Sandiford885140c2013-07-16 11:55:57 +0000962 // and see which goes deepest.
Richard Sandiford51093212013-07-18 10:40:35 +0000963 RxSBGOperands RxSBG[] = {
964 RxSBGOperands(Opcode, N->getOperand(0)),
965 RxSBGOperands(Opcode, N->getOperand(1))
966 };
Richard Sandiford885140c2013-07-16 11:55:57 +0000967 unsigned Count[] = { 0, 0 };
968 for (unsigned I = 0; I < 2; ++I)
Richard Sandiford5cbac962013-07-18 09:45:08 +0000969 while (expandRxSBG(RxSBG[I]))
Richard Sandiford3e382972013-10-16 13:35:13 +0000970 if (RxSBG[I].Input.getOpcode() != ISD::ANY_EXTEND)
971 Count[I] += 1;
Richard Sandiford885140c2013-07-16 11:55:57 +0000972
973 // Do nothing if neither operand is suitable.
974 if (Count[0] == 0 && Count[1] == 0)
Craig Topper062a2ba2014-04-25 05:30:21 +0000975 return nullptr;
Richard Sandiford885140c2013-07-16 11:55:57 +0000976
977 // Pick the deepest second operand.
978 unsigned I = Count[0] > Count[1] ? 0 : 1;
979 SDValue Op0 = N->getOperand(I ^ 1);
980
981 // Prefer IC for character insertions from memory.
Richard Sandiford7878b852013-07-18 10:06:15 +0000982 if (Opcode == SystemZ::ROSBG && (RxSBG[I].Mask & 0xff) == 0)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000983 if (auto *Load = dyn_cast<LoadSDNode>(Op0.getNode()))
Richard Sandiford885140c2013-07-16 11:55:57 +0000984 if (Load->getMemoryVT() == MVT::i8)
Craig Topper062a2ba2014-04-25 05:30:21 +0000985 return nullptr;
Richard Sandiford885140c2013-07-16 11:55:57 +0000986
987 // See whether we can avoid an AND in the first operand by converting
988 // ROSBG to RISBG.
Ulrich Weigand371d10a2015-03-31 12:58:17 +0000989 if (Opcode == SystemZ::ROSBG && detectOrAndInsertion(Op0, RxSBG[I].Mask)) {
Richard Sandiford885140c2013-07-16 11:55:57 +0000990 Opcode = SystemZ::RISBG;
Ulrich Weigand371d10a2015-03-31 12:58:17 +0000991 // Prefer RISBGN if available, since it does not clobber CC.
992 if (Subtarget->hasMiscellaneousExtensions())
993 Opcode = SystemZ::RISBGN;
994 }
995
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000996 SDLoc DL(N);
Richard Sandiford885140c2013-07-16 11:55:57 +0000997 EVT VT = N->getValueType(0);
998 SDValue Ops[5] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000999 convertTo(DL, MVT::i64, Op0),
1000 convertTo(DL, MVT::i64, RxSBG[I].Input),
1001 CurDAG->getTargetConstant(RxSBG[I].Start, DL, MVT::i32),
1002 CurDAG->getTargetConstant(RxSBG[I].End, DL, MVT::i32),
1003 CurDAG->getTargetConstant(RxSBG[I].Rotate, DL, MVT::i32)
Richard Sandiford885140c2013-07-16 11:55:57 +00001004 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001005 N = CurDAG->getMachineNode(Opcode, DL, MVT::i64, Ops);
1006 return convertTo(DL, VT, SDValue(N, 0)).getNode();
Richard Sandiford885140c2013-07-16 11:55:57 +00001007}
1008
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001009SDNode *SystemZDAGToDAGISel::splitLargeImmediate(unsigned Opcode, SDNode *Node,
1010 SDValue Op0, uint64_t UpperVal,
1011 uint64_t LowerVal) {
1012 EVT VT = Node->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001013 SDLoc DL(Node);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001014 SDValue Upper = CurDAG->getConstant(UpperVal, DL, VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001015 if (Op0.getNode())
1016 Upper = CurDAG->getNode(Opcode, DL, VT, Op0, Upper);
1017 Upper = SDValue(Select(Upper.getNode()), 0);
1018
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001019 SDValue Lower = CurDAG->getConstant(LowerVal, DL, VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001020 SDValue Or = CurDAG->getNode(Opcode, DL, VT, Upper, Lower);
1021 return Or.getNode();
1022}
1023
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001024SDNode *SystemZDAGToDAGISel::tryGather(SDNode *N, unsigned Opcode) {
1025 SDValue ElemV = N->getOperand(2);
1026 auto *ElemN = dyn_cast<ConstantSDNode>(ElemV);
1027 if (!ElemN)
1028 return 0;
1029
1030 unsigned Elem = ElemN->getZExtValue();
1031 EVT VT = N->getValueType(0);
1032 if (Elem >= VT.getVectorNumElements())
1033 return 0;
1034
1035 auto *Load = dyn_cast<LoadSDNode>(N->getOperand(1));
1036 if (!Load || !Load->hasOneUse())
1037 return 0;
1038 if (Load->getMemoryVT().getSizeInBits() !=
1039 Load->getValueType(0).getSizeInBits())
1040 return 0;
1041
1042 SDValue Base, Disp, Index;
1043 if (!selectBDVAddr12Only(Load->getBasePtr(), ElemV, Base, Disp, Index) ||
1044 Index.getValueType() != VT.changeVectorElementTypeToInteger())
1045 return 0;
1046
1047 SDLoc DL(Load);
1048 SDValue Ops[] = {
1049 N->getOperand(0), Base, Disp, Index,
1050 CurDAG->getTargetConstant(Elem, DL, MVT::i32), Load->getChain()
1051 };
1052 SDNode *Res = CurDAG->getMachineNode(Opcode, DL, VT, MVT::Other, Ops);
1053 ReplaceUses(SDValue(Load, 1), SDValue(Res, 1));
1054 return Res;
1055}
1056
1057SDNode *SystemZDAGToDAGISel::tryScatter(StoreSDNode *Store, unsigned Opcode) {
1058 SDValue Value = Store->getValue();
1059 if (Value.getOpcode() != ISD::EXTRACT_VECTOR_ELT)
1060 return 0;
1061 if (Store->getMemoryVT().getSizeInBits() !=
1062 Value.getValueType().getSizeInBits())
1063 return 0;
1064
1065 SDValue ElemV = Value.getOperand(1);
1066 auto *ElemN = dyn_cast<ConstantSDNode>(ElemV);
1067 if (!ElemN)
1068 return 0;
1069
1070 SDValue Vec = Value.getOperand(0);
1071 EVT VT = Vec.getValueType();
1072 unsigned Elem = ElemN->getZExtValue();
1073 if (Elem >= VT.getVectorNumElements())
1074 return 0;
1075
1076 SDValue Base, Disp, Index;
1077 if (!selectBDVAddr12Only(Store->getBasePtr(), ElemV, Base, Disp, Index) ||
1078 Index.getValueType() != VT.changeVectorElementTypeToInteger())
1079 return 0;
1080
1081 SDLoc DL(Store);
1082 SDValue Ops[] = {
1083 Vec, Base, Disp, Index, CurDAG->getTargetConstant(Elem, DL, MVT::i32),
1084 Store->getChain()
1085 };
1086 return CurDAG->getMachineNode(Opcode, DL, MVT::Other, Ops);
1087}
1088
Richard Sandiford067817e2013-09-27 15:29:20 +00001089bool SystemZDAGToDAGISel::canUseBlockOperation(StoreSDNode *Store,
1090 LoadSDNode *Load) const {
Richard Sandiford178273a2013-09-05 10:36:45 +00001091 // Check that the two memory operands have the same size.
1092 if (Load->getMemoryVT() != Store->getMemoryVT())
Richard Sandiford97846492013-07-09 09:46:39 +00001093 return false;
1094
Richard Sandiford178273a2013-09-05 10:36:45 +00001095 // Volatility stops an access from being decomposed.
1096 if (Load->isVolatile() || Store->isVolatile())
1097 return false;
Richard Sandiford97846492013-07-09 09:46:39 +00001098
1099 // There's no chance of overlap if the load is invariant.
1100 if (Load->isInvariant())
1101 return true;
1102
Richard Sandiford97846492013-07-09 09:46:39 +00001103 // Otherwise we need to check whether there's an alias.
Nick Lewyckyaad475b2014-04-15 07:22:52 +00001104 const Value *V1 = Load->getMemOperand()->getValue();
1105 const Value *V2 = Store->getMemOperand()->getValue();
Richard Sandiford97846492013-07-09 09:46:39 +00001106 if (!V1 || !V2)
1107 return false;
1108
Richard Sandiford067817e2013-09-27 15:29:20 +00001109 // Reject equality.
1110 uint64_t Size = Load->getMemoryVT().getStoreSize();
Richard Sandiford97846492013-07-09 09:46:39 +00001111 int64_t End1 = Load->getSrcValueOffset() + Size;
1112 int64_t End2 = Store->getSrcValueOffset() + Size;
Richard Sandiford067817e2013-09-27 15:29:20 +00001113 if (V1 == V2 && End1 == End2)
1114 return false;
1115
Hal Finkelcc39b672014-07-24 12:16:19 +00001116 return !AA->alias(AliasAnalysis::Location(V1, End1, Load->getAAInfo()),
1117 AliasAnalysis::Location(V2, End2, Store->getAAInfo()));
Richard Sandiford97846492013-07-09 09:46:39 +00001118}
1119
Richard Sandiford178273a2013-09-05 10:36:45 +00001120bool SystemZDAGToDAGISel::storeLoadCanUseMVC(SDNode *N) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001121 auto *Store = cast<StoreSDNode>(N);
1122 auto *Load = cast<LoadSDNode>(Store->getValue());
Richard Sandiford178273a2013-09-05 10:36:45 +00001123
1124 // Prefer not to use MVC if either address can use ... RELATIVE LONG
1125 // instructions.
1126 uint64_t Size = Load->getMemoryVT().getStoreSize();
1127 if (Size > 1 && Size <= 8) {
1128 // Prefer LHRL, LRL and LGRL.
Richard Sandiford54b36912013-09-27 15:14:04 +00001129 if (SystemZISD::isPCREL(Load->getBasePtr().getOpcode()))
Richard Sandiford178273a2013-09-05 10:36:45 +00001130 return false;
1131 // Prefer STHRL, STRL and STGRL.
Richard Sandiford54b36912013-09-27 15:14:04 +00001132 if (SystemZISD::isPCREL(Store->getBasePtr().getOpcode()))
Richard Sandiford178273a2013-09-05 10:36:45 +00001133 return false;
1134 }
1135
Richard Sandiford067817e2013-09-27 15:29:20 +00001136 return canUseBlockOperation(Store, Load);
Richard Sandiford178273a2013-09-05 10:36:45 +00001137}
1138
1139bool SystemZDAGToDAGISel::storeLoadCanUseBlockBinary(SDNode *N,
1140 unsigned I) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001141 auto *StoreA = cast<StoreSDNode>(N);
1142 auto *LoadA = cast<LoadSDNode>(StoreA->getValue().getOperand(1 - I));
1143 auto *LoadB = cast<LoadSDNode>(StoreA->getValue().getOperand(I));
Richard Sandiford067817e2013-09-27 15:29:20 +00001144 return !LoadA->isVolatile() && canUseBlockOperation(StoreA, LoadB);
Richard Sandiford178273a2013-09-05 10:36:45 +00001145}
1146
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001147SDNode *SystemZDAGToDAGISel::Select(SDNode *Node) {
1148 // Dump information about the Node being selected
1149 DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n");
1150
1151 // If we have a custom node, we already have selected!
1152 if (Node->isMachineOpcode()) {
1153 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
Tim Northover31d093c2013-09-22 08:21:56 +00001154 Node->setNodeId(-1);
Craig Topper062a2ba2014-04-25 05:30:21 +00001155 return nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001156 }
1157
1158 unsigned Opcode = Node->getOpcode();
Craig Topper062a2ba2014-04-25 05:30:21 +00001159 SDNode *ResNode = nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001160 switch (Opcode) {
1161 case ISD::OR:
Richard Sandiford885140c2013-07-16 11:55:57 +00001162 if (Node->getOperand(1).getOpcode() != ISD::Constant)
Richard Sandiford7878b852013-07-18 10:06:15 +00001163 ResNode = tryRxSBG(Node, SystemZ::ROSBG);
1164 goto or_xor;
1165
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001166 case ISD::XOR:
Richard Sandiford7878b852013-07-18 10:06:15 +00001167 if (Node->getOperand(1).getOpcode() != ISD::Constant)
1168 ResNode = tryRxSBG(Node, SystemZ::RXSBG);
1169 // Fall through.
1170 or_xor:
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001171 // If this is a 64-bit operation in which both 32-bit halves are nonzero,
1172 // split the operation into two.
Richard Sandiford885140c2013-07-16 11:55:57 +00001173 if (!ResNode && Node->getValueType(0) == MVT::i64)
Richard Sandiford21f5d682014-03-06 11:22:58 +00001174 if (auto *Op1 = dyn_cast<ConstantSDNode>(Node->getOperand(1))) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001175 uint64_t Val = Op1->getZExtValue();
1176 if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val))
1177 Node = splitLargeImmediate(Opcode, Node, Node->getOperand(0),
1178 Val - uint32_t(Val), uint32_t(Val));
1179 }
1180 break;
1181
Richard Sandiford84f54a32013-07-11 08:59:12 +00001182 case ISD::AND:
Richard Sandiford51093212013-07-18 10:40:35 +00001183 if (Node->getOperand(1).getOpcode() != ISD::Constant)
1184 ResNode = tryRxSBG(Node, SystemZ::RNSBG);
1185 // Fall through.
Richard Sandiford82ec87d2013-07-16 11:02:24 +00001186 case ISD::ROTL:
1187 case ISD::SHL:
1188 case ISD::SRL:
Richard Sandiford220ee492013-12-20 11:49:48 +00001189 case ISD::ZERO_EXTEND:
Richard Sandiford7878b852013-07-18 10:06:15 +00001190 if (!ResNode)
1191 ResNode = tryRISBGZero(Node);
Richard Sandiford84f54a32013-07-11 08:59:12 +00001192 break;
1193
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001194 case ISD::Constant:
1195 // If this is a 64-bit constant that is out of the range of LLILF,
1196 // LLIHF and LGFI, split it into two 32-bit pieces.
1197 if (Node->getValueType(0) == MVT::i64) {
1198 uint64_t Val = cast<ConstantSDNode>(Node)->getZExtValue();
1199 if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val) && !isInt<32>(Val))
1200 Node = splitLargeImmediate(ISD::OR, Node, SDValue(),
1201 Val - uint32_t(Val), uint32_t(Val));
1202 }
1203 break;
1204
Richard Sandifordee834382013-07-31 12:38:08 +00001205 case SystemZISD::SELECT_CCMASK: {
1206 SDValue Op0 = Node->getOperand(0);
1207 SDValue Op1 = Node->getOperand(1);
1208 // Prefer to put any load first, so that it can be matched as a
1209 // conditional load.
1210 if (Op1.getOpcode() == ISD::LOAD && Op0.getOpcode() != ISD::LOAD) {
1211 SDValue CCValid = Node->getOperand(2);
1212 SDValue CCMask = Node->getOperand(3);
1213 uint64_t ConstCCValid =
1214 cast<ConstantSDNode>(CCValid.getNode())->getZExtValue();
1215 uint64_t ConstCCMask =
1216 cast<ConstantSDNode>(CCMask.getNode())->getZExtValue();
1217 // Invert the condition.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001218 CCMask = CurDAG->getConstant(ConstCCValid ^ ConstCCMask, SDLoc(Node),
Richard Sandifordee834382013-07-31 12:38:08 +00001219 CCMask.getValueType());
1220 SDValue Op4 = Node->getOperand(4);
1221 Node = CurDAG->UpdateNodeOperands(Node, Op1, Op0, CCValid, CCMask, Op4);
1222 }
1223 break;
1224 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001225
1226 case ISD::INSERT_VECTOR_ELT: {
1227 EVT VT = Node->getValueType(0);
1228 unsigned ElemBitSize = VT.getVectorElementType().getSizeInBits();
1229 if (ElemBitSize == 32)
1230 ResNode = tryGather(Node, SystemZ::VGEF);
1231 else if (ElemBitSize == 64)
1232 ResNode = tryGather(Node, SystemZ::VGEG);
1233 break;
1234 }
1235
1236 case ISD::STORE: {
1237 auto *Store = cast<StoreSDNode>(Node);
1238 unsigned ElemBitSize = Store->getValue().getValueType().getSizeInBits();
1239 if (ElemBitSize == 32)
1240 ResNode = tryScatter(Store, SystemZ::VSCEF);
1241 else if (ElemBitSize == 64)
1242 ResNode = tryScatter(Store, SystemZ::VSCEG);
1243 break;
1244 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001245 }
1246
1247 // Select the default instruction
Richard Sandiford84f54a32013-07-11 08:59:12 +00001248 if (!ResNode)
1249 ResNode = SelectCode(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001250
1251 DEBUG(errs() << "=> ";
Craig Topper062a2ba2014-04-25 05:30:21 +00001252 if (ResNode == nullptr || ResNode == Node)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001253 Node->dump(CurDAG);
1254 else
1255 ResNode->dump(CurDAG);
1256 errs() << "\n";
1257 );
1258 return ResNode;
1259}
1260
1261bool SystemZDAGToDAGISel::
1262SelectInlineAsmMemoryOperand(const SDValue &Op,
Daniel Sanders60f1db02015-03-13 12:45:09 +00001263 unsigned ConstraintID,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001264 std::vector<SDValue> &OutOps) {
Daniel Sanders2eeace22015-03-17 16:16:14 +00001265 switch(ConstraintID) {
1266 default:
1267 llvm_unreachable("Unexpected asm memory constraint");
1268 case InlineAsm::Constraint_i:
1269 case InlineAsm::Constraint_m:
1270 case InlineAsm::Constraint_Q:
1271 case InlineAsm::Constraint_R:
1272 case InlineAsm::Constraint_S:
1273 case InlineAsm::Constraint_T:
1274 // Accept addresses with short displacements, which are compatible
1275 // with Q, R, S and T. But keep the index operand for future expansion.
1276 SDValue Base, Disp, Index;
1277 if (selectBDXAddr(SystemZAddressingMode::FormBD,
1278 SystemZAddressingMode::Disp12Only,
1279 Op, Base, Disp, Index)) {
1280 OutOps.push_back(Base);
1281 OutOps.push_back(Disp);
1282 OutOps.push_back(Index);
1283 return false;
1284 }
1285 break;
1286 }
1287 return true;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001288}