blob: 26c9c9b9f67d1d0978130f276ba884cf976ae8a4 [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) {
Justin Bognerc97c48a2015-06-24 05:59:19 +000099 if (Count > 63)
100 return UINT64_MAX;
101 return (uint64_t(1) << Count) - 1;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000102}
103
Richard Sandiford51093212013-07-18 10:40:35 +0000104// Represents operands 2 to 5 of the ROTATE AND ... SELECTED BITS operation
105// given by Opcode. The operands are: Input (R2), Start (I3), End (I4) and
106// Rotate (I5). The combined operand value is effectively:
107//
108// (or (rotl Input, Rotate), ~Mask)
109//
110// for RNSBG and:
111//
112// (and (rotl Input, Rotate), Mask)
113//
Richard Sandiford3e382972013-10-16 13:35:13 +0000114// otherwise. The output value has BitSize bits, although Input may be
115// narrower (in which case the upper bits are don't care).
Richard Sandiford5cbac962013-07-18 09:45:08 +0000116struct RxSBGOperands {
Richard Sandiford51093212013-07-18 10:40:35 +0000117 RxSBGOperands(unsigned Op, SDValue N)
118 : Opcode(Op), BitSize(N.getValueType().getSizeInBits()),
119 Mask(allOnes(BitSize)), Input(N), Start(64 - BitSize), End(63),
120 Rotate(0) {}
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000121
Richard Sandiford51093212013-07-18 10:40:35 +0000122 unsigned Opcode;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000123 unsigned BitSize;
124 uint64_t Mask;
125 SDValue Input;
126 unsigned Start;
127 unsigned End;
128 unsigned Rotate;
129};
130
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000131class SystemZDAGToDAGISel : public SelectionDAGISel {
Eric Christophera6734172015-01-31 00:06:45 +0000132 const SystemZSubtarget *Subtarget;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000133
134 // Used by SystemZOperands.td to create integer constants.
Richard Sandiford54b36912013-09-27 15:14:04 +0000135 inline SDValue getImm(const SDNode *Node, uint64_t Imm) const {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000136 return CurDAG->getTargetConstant(Imm, SDLoc(Node), Node->getValueType(0));
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000137 }
138
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000139 const SystemZTargetMachine &getTargetMachine() const {
140 return static_cast<const SystemZTargetMachine &>(TM);
141 }
142
143 const SystemZInstrInfo *getInstrInfo() const {
Eric Christophera6734172015-01-31 00:06:45 +0000144 return Subtarget->getInstrInfo();
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000145 }
146
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000147 // Try to fold more of the base or index of AM into AM, where IsBase
148 // selects between the base and index.
Richard Sandiford54b36912013-09-27 15:14:04 +0000149 bool expandAddress(SystemZAddressingMode &AM, bool IsBase) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000150
151 // Try to describe N in AM, returning true on success.
Richard Sandiford54b36912013-09-27 15:14:04 +0000152 bool selectAddress(SDValue N, SystemZAddressingMode &AM) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000153
154 // Extract individual target operands from matched address AM.
155 void getAddressOperands(const SystemZAddressingMode &AM, EVT VT,
Richard Sandiford54b36912013-09-27 15:14:04 +0000156 SDValue &Base, SDValue &Disp) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000157 void getAddressOperands(const SystemZAddressingMode &AM, EVT VT,
Richard Sandiford54b36912013-09-27 15:14:04 +0000158 SDValue &Base, SDValue &Disp, SDValue &Index) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000159
160 // Try to match Addr as a FormBD address with displacement type DR.
161 // Return true on success, storing the base and displacement in
162 // Base and Disp respectively.
163 bool selectBDAddr(SystemZAddressingMode::DispRange DR, SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000164 SDValue &Base, SDValue &Disp) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000165
Richard Sandiforda481f582013-08-23 11:18:53 +0000166 // Try to match Addr as a FormBDX address with displacement type DR.
167 // Return true on success and if the result had no index. Store the
168 // base and displacement in Base and Disp respectively.
169 bool selectMVIAddr(SystemZAddressingMode::DispRange DR, SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000170 SDValue &Base, SDValue &Disp) const;
Richard Sandiforda481f582013-08-23 11:18:53 +0000171
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000172 // Try to match Addr as a FormBDX* address of form Form with
173 // displacement type DR. Return true on success, storing the base,
174 // displacement and index in Base, Disp and Index respectively.
175 bool selectBDXAddr(SystemZAddressingMode::AddrForm Form,
176 SystemZAddressingMode::DispRange DR, SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000177 SDValue &Base, SDValue &Disp, SDValue &Index) const;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000178
179 // PC-relative address matching routines used by SystemZOperands.td.
Richard Sandiford54b36912013-09-27 15:14:04 +0000180 bool selectPCRelAddress(SDValue Addr, SDValue &Target) const {
181 if (SystemZISD::isPCREL(Addr.getOpcode())) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000182 Target = Addr.getOperand(0);
183 return true;
184 }
185 return false;
186 }
187
188 // BD matching routines used by SystemZOperands.td.
Richard Sandiford54b36912013-09-27 15:14:04 +0000189 bool selectBDAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000190 return selectBDAddr(SystemZAddressingMode::Disp12Only, Addr, Base, Disp);
191 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000192 bool selectBDAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000193 return selectBDAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp);
194 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000195 bool selectBDAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000196 return selectBDAddr(SystemZAddressingMode::Disp20Only, Addr, Base, Disp);
197 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000198 bool selectBDAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000199 return selectBDAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp);
200 }
201
Richard Sandiforda481f582013-08-23 11:18:53 +0000202 // MVI matching routines used by SystemZOperands.td.
Richard Sandiford54b36912013-09-27 15:14:04 +0000203 bool selectMVIAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Richard Sandiforda481f582013-08-23 11:18:53 +0000204 return selectMVIAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp);
205 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000206 bool selectMVIAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
Richard Sandiforda481f582013-08-23 11:18:53 +0000207 return selectMVIAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp);
208 }
209
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000210 // BDX matching routines used by SystemZOperands.td.
211 bool selectBDXAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000212 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000213 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
214 SystemZAddressingMode::Disp12Only,
215 Addr, Base, Disp, Index);
216 }
217 bool selectBDXAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000218 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000219 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
220 SystemZAddressingMode::Disp12Pair,
221 Addr, Base, Disp, Index);
222 }
223 bool selectDynAlloc12Only(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000224 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000225 return selectBDXAddr(SystemZAddressingMode::FormBDXDynAlloc,
226 SystemZAddressingMode::Disp12Only,
227 Addr, Base, Disp, Index);
228 }
229 bool selectBDXAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000230 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000231 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
232 SystemZAddressingMode::Disp20Only,
233 Addr, Base, Disp, Index);
234 }
235 bool selectBDXAddr20Only128(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000236 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000237 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
238 SystemZAddressingMode::Disp20Only128,
239 Addr, Base, Disp, Index);
240 }
241 bool selectBDXAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000242 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000243 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
244 SystemZAddressingMode::Disp20Pair,
245 Addr, Base, Disp, Index);
246 }
247 bool selectLAAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000248 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000249 return selectBDXAddr(SystemZAddressingMode::FormBDXLA,
250 SystemZAddressingMode::Disp12Pair,
251 Addr, Base, Disp, Index);
252 }
253 bool selectLAAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
Richard Sandiford54b36912013-09-27 15:14:04 +0000254 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000255 return selectBDXAddr(SystemZAddressingMode::FormBDXLA,
256 SystemZAddressingMode::Disp20Pair,
257 Addr, Base, Disp, Index);
258 }
259
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000260 // Try to match Addr as an address with a base, 12-bit displacement
261 // and index, where the index is element Elem of a vector.
262 // Return true on success, storing the base, displacement and vector
263 // in Base, Disp and Index respectively.
264 bool selectBDVAddr12Only(SDValue Addr, SDValue Elem, SDValue &Base,
265 SDValue &Disp, SDValue &Index) const;
266
Richard Sandiford885140c2013-07-16 11:55:57 +0000267 // Check whether (or Op (and X InsertMask)) is effectively an insertion
268 // of X into bits InsertMask of some Y != Op. Return true if so and
269 // set Op to that Y.
Richard Sandiford54b36912013-09-27 15:14:04 +0000270 bool detectOrAndInsertion(SDValue &Op, uint64_t InsertMask) const;
Richard Sandiford885140c2013-07-16 11:55:57 +0000271
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000272 // Try to update RxSBG so that only the bits of RxSBG.Input in Mask are used.
273 // Return true on success.
Richard Sandiford54b36912013-09-27 15:14:04 +0000274 bool refineRxSBGMask(RxSBGOperands &RxSBG, uint64_t Mask) const;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000275
Richard Sandiford5cbac962013-07-18 09:45:08 +0000276 // Try to fold some of RxSBG.Input into other fields of RxSBG.
277 // Return true on success.
Richard Sandiford54b36912013-09-27 15:14:04 +0000278 bool expandRxSBG(RxSBGOperands &RxSBG) const;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000279
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000280 // Return an undefined value of type VT.
281 SDValue getUNDEF(SDLoc DL, EVT VT) const;
Richard Sandiford84f54a32013-07-11 08:59:12 +0000282
283 // Convert N to VT, if it isn't already.
Richard Sandiford54b36912013-09-27 15:14:04 +0000284 SDValue convertTo(SDLoc DL, EVT VT, SDValue N) const;
Richard Sandiford84f54a32013-07-11 08:59:12 +0000285
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000286 // Try to implement AND or shift node N using RISBG with the zero flag set.
287 // Return the selected node on success, otherwise return null.
288 SDNode *tryRISBGZero(SDNode *N);
Richard Sandiford84f54a32013-07-11 08:59:12 +0000289
Richard Sandiford7878b852013-07-18 10:06:15 +0000290 // Try to use RISBG or Opcode to implement OR or XOR node N.
291 // Return the selected node on success, otherwise return null.
292 SDNode *tryRxSBG(SDNode *N, unsigned Opcode);
Richard Sandiford885140c2013-07-16 11:55:57 +0000293
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000294 // If Op0 is null, then Node is a constant that can be loaded using:
295 //
296 // (Opcode UpperVal LowerVal)
297 //
298 // If Op0 is nonnull, then Node can be implemented using:
299 //
300 // (Opcode (Opcode Op0 UpperVal) LowerVal)
301 SDNode *splitLargeImmediate(unsigned Opcode, SDNode *Node, SDValue Op0,
302 uint64_t UpperVal, uint64_t LowerVal);
303
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000304 // Try to use gather instruction Opcode to implement vector insertion N.
305 SDNode *tryGather(SDNode *N, unsigned Opcode);
306
307 // Try to use scatter instruction Opcode to implement store Store.
308 SDNode *tryScatter(StoreSDNode *Store, unsigned Opcode);
309
Richard Sandiford067817e2013-09-27 15:29:20 +0000310 // Return true if Load and Store are loads and stores of the same size
311 // and are guaranteed not to overlap. Such operations can be implemented
312 // using block (SS-format) instructions.
313 //
314 // Partial overlap would lead to incorrect code, since the block operations
315 // are logically bytewise, even though they have a fast path for the
316 // non-overlapping case. We also need to avoid full overlap (i.e. two
317 // addresses that might be equal at run time) because although that case
318 // would be handled correctly, it might be implemented by millicode.
319 bool canUseBlockOperation(StoreSDNode *Store, LoadSDNode *Load) const;
320
Richard Sandiford178273a2013-09-05 10:36:45 +0000321 // N is a (store (load Y), X) pattern. Return true if it can use an MVC
322 // from Y to X.
Richard Sandiford97846492013-07-09 09:46:39 +0000323 bool storeLoadCanUseMVC(SDNode *N) const;
324
Richard Sandiford178273a2013-09-05 10:36:45 +0000325 // N is a (store (op (load A[0]), (load A[1])), X) pattern. Return true
326 // if A[1 - I] == X and if N can use a block operation like NC from A[I]
327 // to X.
328 bool storeLoadCanUseBlockBinary(SDNode *N, unsigned I) const;
329
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000330public:
331 SystemZDAGToDAGISel(SystemZTargetMachine &TM, CodeGenOpt::Level OptLevel)
Eric Christophera6734172015-01-31 00:06:45 +0000332 : SelectionDAGISel(TM, OptLevel) {}
333
334 bool runOnMachineFunction(MachineFunction &MF) override {
335 Subtarget = &MF.getSubtarget<SystemZSubtarget>();
336 return SelectionDAGISel::runOnMachineFunction(MF);
337 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000338
339 // Override MachineFunctionPass.
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000340 const char *getPassName() const override {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000341 return "SystemZ DAG->DAG Pattern Instruction Selection";
342 }
343
344 // Override SelectionDAGISel.
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000345 SDNode *Select(SDNode *Node) override;
Daniel Sanders60f1db02015-03-13 12:45:09 +0000346 bool SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
Richard Sandifordb4d67b52014-03-06 12:03:36 +0000347 std::vector<SDValue> &OutOps) override;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000348
349 // Include the pieces autogenerated from the target description.
350 #include "SystemZGenDAGISel.inc"
351};
352} // end anonymous namespace
353
354FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM,
355 CodeGenOpt::Level OptLevel) {
356 return new SystemZDAGToDAGISel(TM, OptLevel);
357}
358
359// Return true if Val should be selected as a displacement for an address
360// with range DR. Here we're interested in the range of both the instruction
361// described by DR and of any pairing instruction.
362static bool selectDisp(SystemZAddressingMode::DispRange DR, int64_t Val) {
363 switch (DR) {
364 case SystemZAddressingMode::Disp12Only:
365 return isUInt<12>(Val);
366
367 case SystemZAddressingMode::Disp12Pair:
368 case SystemZAddressingMode::Disp20Only:
369 case SystemZAddressingMode::Disp20Pair:
370 return isInt<20>(Val);
371
372 case SystemZAddressingMode::Disp20Only128:
373 return isInt<20>(Val) && isInt<20>(Val + 8);
374 }
375 llvm_unreachable("Unhandled displacement range");
376}
377
378// Change the base or index in AM to Value, where IsBase selects
379// between the base and index.
380static void changeComponent(SystemZAddressingMode &AM, bool IsBase,
381 SDValue Value) {
382 if (IsBase)
383 AM.Base = Value;
384 else
385 AM.Index = Value;
386}
387
388// The base or index of AM is equivalent to Value + ADJDYNALLOC,
389// where IsBase selects between the base and index. Try to fold the
390// ADJDYNALLOC into AM.
391static bool expandAdjDynAlloc(SystemZAddressingMode &AM, bool IsBase,
392 SDValue Value) {
393 if (AM.isDynAlloc() && !AM.IncludesDynAlloc) {
394 changeComponent(AM, IsBase, Value);
395 AM.IncludesDynAlloc = true;
396 return true;
397 }
398 return false;
399}
400
401// The base of AM is equivalent to Base + Index. Try to use Index as
402// the index register.
403static bool expandIndex(SystemZAddressingMode &AM, SDValue Base,
404 SDValue Index) {
405 if (AM.hasIndexField() && !AM.Index.getNode()) {
406 AM.Base = Base;
407 AM.Index = Index;
408 return true;
409 }
410 return false;
411}
412
413// The base or index of AM is equivalent to Op0 + Op1, where IsBase selects
414// between the base and index. Try to fold Op1 into AM's displacement.
415static bool expandDisp(SystemZAddressingMode &AM, bool IsBase,
Richard Sandiford54b36912013-09-27 15:14:04 +0000416 SDValue Op0, uint64_t Op1) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000417 // First try adjusting the displacement.
Richard Sandiford54b36912013-09-27 15:14:04 +0000418 int64_t TestDisp = AM.Disp + Op1;
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000419 if (selectDisp(AM.DR, TestDisp)) {
420 changeComponent(AM, IsBase, Op0);
421 AM.Disp = TestDisp;
422 return true;
423 }
424
425 // We could consider forcing the displacement into a register and
426 // using it as an index, but it would need to be carefully tuned.
427 return false;
428}
429
430bool SystemZDAGToDAGISel::expandAddress(SystemZAddressingMode &AM,
Richard Sandiford54b36912013-09-27 15:14:04 +0000431 bool IsBase) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000432 SDValue N = IsBase ? AM.Base : AM.Index;
433 unsigned Opcode = N.getOpcode();
434 if (Opcode == ISD::TRUNCATE) {
435 N = N.getOperand(0);
436 Opcode = N.getOpcode();
437 }
438 if (Opcode == ISD::ADD || CurDAG->isBaseWithConstantOffset(N)) {
439 SDValue Op0 = N.getOperand(0);
440 SDValue Op1 = N.getOperand(1);
441
442 unsigned Op0Code = Op0->getOpcode();
443 unsigned Op1Code = Op1->getOpcode();
444
445 if (Op0Code == SystemZISD::ADJDYNALLOC)
446 return expandAdjDynAlloc(AM, IsBase, Op1);
447 if (Op1Code == SystemZISD::ADJDYNALLOC)
448 return expandAdjDynAlloc(AM, IsBase, Op0);
449
450 if (Op0Code == ISD::Constant)
Richard Sandiford54b36912013-09-27 15:14:04 +0000451 return expandDisp(AM, IsBase, Op1,
452 cast<ConstantSDNode>(Op0)->getSExtValue());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000453 if (Op1Code == ISD::Constant)
Richard Sandiford54b36912013-09-27 15:14:04 +0000454 return expandDisp(AM, IsBase, Op0,
455 cast<ConstantSDNode>(Op1)->getSExtValue());
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000456
457 if (IsBase && expandIndex(AM, Op0, Op1))
458 return true;
459 }
Richard Sandiford54b36912013-09-27 15:14:04 +0000460 if (Opcode == SystemZISD::PCREL_OFFSET) {
461 SDValue Full = N.getOperand(0);
462 SDValue Base = N.getOperand(1);
463 SDValue Anchor = Base.getOperand(0);
464 uint64_t Offset = (cast<GlobalAddressSDNode>(Full)->getOffset() -
465 cast<GlobalAddressSDNode>(Anchor)->getOffset());
466 return expandDisp(AM, IsBase, Base, Offset);
467 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000468 return false;
469}
470
471// Return true if an instruction with displacement range DR should be
472// used for displacement value Val. selectDisp(DR, Val) must already hold.
473static bool isValidDisp(SystemZAddressingMode::DispRange DR, int64_t Val) {
474 assert(selectDisp(DR, Val) && "Invalid displacement");
475 switch (DR) {
476 case SystemZAddressingMode::Disp12Only:
477 case SystemZAddressingMode::Disp20Only:
478 case SystemZAddressingMode::Disp20Only128:
479 return true;
480
481 case SystemZAddressingMode::Disp12Pair:
482 // Use the other instruction if the displacement is too large.
483 return isUInt<12>(Val);
484
485 case SystemZAddressingMode::Disp20Pair:
486 // Use the other instruction if the displacement is small enough.
487 return !isUInt<12>(Val);
488 }
489 llvm_unreachable("Unhandled displacement range");
490}
491
492// Return true if Base + Disp + Index should be performed by LA(Y).
493static bool shouldUseLA(SDNode *Base, int64_t Disp, SDNode *Index) {
494 // Don't use LA(Y) for constants.
495 if (!Base)
496 return false;
497
498 // Always use LA(Y) for frame addresses, since we know that the destination
499 // register is almost always (perhaps always) going to be different from
500 // the frame register.
501 if (Base->getOpcode() == ISD::FrameIndex)
502 return true;
503
504 if (Disp) {
505 // Always use LA(Y) if there is a base, displacement and index.
506 if (Index)
507 return true;
508
509 // Always use LA if the displacement is small enough. It should always
510 // be no worse than AGHI (and better if it avoids a move).
511 if (isUInt<12>(Disp))
512 return true;
513
514 // For similar reasons, always use LAY if the constant is too big for AGHI.
515 // LAY should be no worse than AGFI.
516 if (!isInt<16>(Disp))
517 return true;
518 } else {
519 // Don't use LA for plain registers.
520 if (!Index)
521 return false;
522
523 // Don't use LA for plain addition if the index operand is only used
524 // once. It should be a natural two-operand addition in that case.
525 if (Index->hasOneUse())
526 return false;
527
528 // Prefer addition if the second operation is sign-extended, in the
529 // hope of using AGF.
530 unsigned IndexOpcode = Index->getOpcode();
531 if (IndexOpcode == ISD::SIGN_EXTEND ||
532 IndexOpcode == ISD::SIGN_EXTEND_INREG)
533 return false;
534 }
535
536 // Don't use LA for two-operand addition if either operand is only
537 // used once. The addition instructions are better in that case.
538 if (Base->hasOneUse())
539 return false;
540
541 return true;
542}
543
544// Return true if Addr is suitable for AM, updating AM if so.
545bool SystemZDAGToDAGISel::selectAddress(SDValue Addr,
Richard Sandiford54b36912013-09-27 15:14:04 +0000546 SystemZAddressingMode &AM) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000547 // Start out assuming that the address will need to be loaded separately,
548 // then try to extend it as much as we can.
549 AM.Base = Addr;
550
551 // First try treating the address as a constant.
552 if (Addr.getOpcode() == ISD::Constant &&
Richard Sandiford54b36912013-09-27 15:14:04 +0000553 expandDisp(AM, true, SDValue(),
554 cast<ConstantSDNode>(Addr)->getSExtValue()))
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000555 ;
556 else
557 // Otherwise try expanding each component.
558 while (expandAddress(AM, true) ||
559 (AM.Index.getNode() && expandAddress(AM, false)))
560 continue;
561
562 // Reject cases where it isn't profitable to use LA(Y).
563 if (AM.Form == SystemZAddressingMode::FormBDXLA &&
564 !shouldUseLA(AM.Base.getNode(), AM.Disp, AM.Index.getNode()))
565 return false;
566
567 // Reject cases where the other instruction in a pair should be used.
568 if (!isValidDisp(AM.DR, AM.Disp))
569 return false;
570
571 // Make sure that ADJDYNALLOC is included where necessary.
572 if (AM.isDynAlloc() && !AM.IncludesDynAlloc)
573 return false;
574
575 DEBUG(AM.dump());
576 return true;
577}
578
579// Insert a node into the DAG at least before Pos. This will reposition
580// the node as needed, and will assign it a node ID that is <= Pos's ID.
581// Note that this does *not* preserve the uniqueness of node IDs!
582// The selection DAG must no longer depend on their uniqueness when this
583// function is used.
584static void insertDAGNode(SelectionDAG *DAG, SDNode *Pos, SDValue N) {
585 if (N.getNode()->getNodeId() == -1 ||
586 N.getNode()->getNodeId() > Pos->getNodeId()) {
587 DAG->RepositionNode(Pos, N.getNode());
588 N.getNode()->setNodeId(Pos->getNodeId());
589 }
590}
591
592void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
593 EVT VT, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000594 SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000595 Base = AM.Base;
596 if (!Base.getNode())
597 // Register 0 means "no base". This is mostly useful for shifts.
598 Base = CurDAG->getRegister(0, VT);
599 else if (Base.getOpcode() == ISD::FrameIndex) {
600 // Lower a FrameIndex to a TargetFrameIndex.
601 int64_t FrameIndex = cast<FrameIndexSDNode>(Base)->getIndex();
602 Base = CurDAG->getTargetFrameIndex(FrameIndex, VT);
603 } else if (Base.getValueType() != VT) {
604 // Truncate values from i64 to i32, for shifts.
605 assert(VT == MVT::i32 && Base.getValueType() == MVT::i64 &&
606 "Unexpected truncation");
Andrew Trickef9de2a2013-05-25 02:42:55 +0000607 SDLoc DL(Base);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000608 SDValue Trunc = CurDAG->getNode(ISD::TRUNCATE, DL, VT, Base);
609 insertDAGNode(CurDAG, Base.getNode(), Trunc);
610 Base = Trunc;
611 }
612
613 // Lower the displacement to a TargetConstant.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000614 Disp = CurDAG->getTargetConstant(AM.Disp, SDLoc(Base), VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000615}
616
617void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
618 EVT VT, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000619 SDValue &Disp,
620 SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000621 getAddressOperands(AM, VT, Base, Disp);
622
623 Index = AM.Index;
624 if (!Index.getNode())
625 // Register 0 means "no index".
626 Index = CurDAG->getRegister(0, VT);
627}
628
629bool SystemZDAGToDAGISel::selectBDAddr(SystemZAddressingMode::DispRange DR,
630 SDValue Addr, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000631 SDValue &Disp) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000632 SystemZAddressingMode AM(SystemZAddressingMode::FormBD, DR);
633 if (!selectAddress(Addr, AM))
634 return false;
635
636 getAddressOperands(AM, Addr.getValueType(), Base, Disp);
637 return true;
638}
639
Richard Sandiforda481f582013-08-23 11:18:53 +0000640bool SystemZDAGToDAGISel::selectMVIAddr(SystemZAddressingMode::DispRange DR,
641 SDValue Addr, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000642 SDValue &Disp) const {
Richard Sandiforda481f582013-08-23 11:18:53 +0000643 SystemZAddressingMode AM(SystemZAddressingMode::FormBDXNormal, DR);
644 if (!selectAddress(Addr, AM) || AM.Index.getNode())
645 return false;
646
647 getAddressOperands(AM, Addr.getValueType(), Base, Disp);
648 return true;
649}
650
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000651bool SystemZDAGToDAGISel::selectBDXAddr(SystemZAddressingMode::AddrForm Form,
652 SystemZAddressingMode::DispRange DR,
653 SDValue Addr, SDValue &Base,
Richard Sandiford54b36912013-09-27 15:14:04 +0000654 SDValue &Disp, SDValue &Index) const {
Ulrich Weigand5f613df2013-05-06 16:15:19 +0000655 SystemZAddressingMode AM(Form, DR);
656 if (!selectAddress(Addr, AM))
657 return false;
658
659 getAddressOperands(AM, Addr.getValueType(), Base, Disp, Index);
660 return true;
661}
662
Ulrich Weigandce4c1092015-05-05 19:25:42 +0000663bool SystemZDAGToDAGISel::selectBDVAddr12Only(SDValue Addr, SDValue Elem,
664 SDValue &Base,
665 SDValue &Disp,
666 SDValue &Index) const {
667 SDValue Regs[2];
668 if (selectBDXAddr12Only(Addr, Regs[0], Disp, Regs[1]) &&
669 Regs[0].getNode() && Regs[1].getNode()) {
670 for (unsigned int I = 0; I < 2; ++I) {
671 Base = Regs[I];
672 Index = Regs[1 - I];
673 // We can't tell here whether the index vector has the right type
674 // for the access; the caller needs to do that instead.
675 if (Index.getOpcode() == ISD::ZERO_EXTEND)
676 Index = Index.getOperand(0);
677 if (Index.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
678 Index.getOperand(1) == Elem) {
679 Index = Index.getOperand(0);
680 return true;
681 }
682 }
683 }
684 return false;
685}
686
Richard Sandiford885140c2013-07-16 11:55:57 +0000687bool SystemZDAGToDAGISel::detectOrAndInsertion(SDValue &Op,
Richard Sandiford54b36912013-09-27 15:14:04 +0000688 uint64_t InsertMask) const {
Richard Sandiford885140c2013-07-16 11:55:57 +0000689 // We're only interested in cases where the insertion is into some operand
690 // of Op, rather than into Op itself. The only useful case is an AND.
691 if (Op.getOpcode() != ISD::AND)
692 return false;
693
694 // We need a constant mask.
Richard Sandiford21f5d682014-03-06 11:22:58 +0000695 auto *MaskNode = dyn_cast<ConstantSDNode>(Op.getOperand(1).getNode());
Richard Sandiford885140c2013-07-16 11:55:57 +0000696 if (!MaskNode)
697 return false;
698
699 // It's not an insertion of Op.getOperand(0) if the two masks overlap.
700 uint64_t AndMask = MaskNode->getZExtValue();
701 if (InsertMask & AndMask)
702 return false;
703
704 // It's only an insertion if all bits are covered or are known to be zero.
705 // The inner check covers all cases but is more expensive.
706 uint64_t Used = allOnes(Op.getValueType().getSizeInBits());
707 if (Used != (AndMask | InsertMask)) {
708 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +0000709 CurDAG->computeKnownBits(Op.getOperand(0), KnownZero, KnownOne);
Richard Sandiford885140c2013-07-16 11:55:57 +0000710 if (Used != (AndMask | InsertMask | KnownZero.getZExtValue()))
711 return false;
712 }
713
714 Op = Op.getOperand(0);
715 return true;
716}
717
Richard Sandiford54b36912013-09-27 15:14:04 +0000718bool SystemZDAGToDAGISel::refineRxSBGMask(RxSBGOperands &RxSBG,
719 uint64_t Mask) const {
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000720 const SystemZInstrInfo *TII = getInstrInfo();
Richard Sandiford5cbac962013-07-18 09:45:08 +0000721 if (RxSBG.Rotate != 0)
722 Mask = (Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate));
723 Mask &= RxSBG.Mask;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000724 if (TII->isRxSBGMask(Mask, RxSBG.BitSize, RxSBG.Start, RxSBG.End)) {
Richard Sandiford5cbac962013-07-18 09:45:08 +0000725 RxSBG.Mask = Mask;
Richard Sandiford5cbac962013-07-18 09:45:08 +0000726 return true;
727 }
Richard Sandiford84f54a32013-07-11 08:59:12 +0000728 return false;
729}
730
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000731// Return true if any bits of (RxSBG.Input & Mask) are significant.
732static bool maskMatters(RxSBGOperands &RxSBG, uint64_t Mask) {
733 // Rotate the mask in the same way as RxSBG.Input is rotated.
Richard Sandiford297f7d22013-07-18 10:14:55 +0000734 if (RxSBG.Rotate != 0)
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000735 Mask = ((Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate)));
736 return (Mask & RxSBG.Mask) != 0;
Richard Sandiford297f7d22013-07-18 10:14:55 +0000737}
738
Richard Sandiford54b36912013-09-27 15:14:04 +0000739bool SystemZDAGToDAGISel::expandRxSBG(RxSBGOperands &RxSBG) const {
Richard Sandiford5cbac962013-07-18 09:45:08 +0000740 SDValue N = RxSBG.Input;
Richard Sandiford297f7d22013-07-18 10:14:55 +0000741 unsigned Opcode = N.getOpcode();
742 switch (Opcode) {
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000743 case ISD::AND: {
Richard Sandiford51093212013-07-18 10:40:35 +0000744 if (RxSBG.Opcode == SystemZ::RNSBG)
745 return false;
746
Richard Sandiford21f5d682014-03-06 11:22:58 +0000747 auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000748 if (!MaskNode)
749 return false;
750
751 SDValue Input = N.getOperand(0);
752 uint64_t Mask = MaskNode->getZExtValue();
Richard Sandiford5cbac962013-07-18 09:45:08 +0000753 if (!refineRxSBGMask(RxSBG, Mask)) {
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000754 // If some bits of Input are already known zeros, those bits will have
755 // been removed from the mask. See if adding them back in makes the
756 // mask suitable.
757 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +0000758 CurDAG->computeKnownBits(Input, KnownZero, KnownOne);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000759 Mask |= KnownZero.getZExtValue();
Richard Sandiford5cbac962013-07-18 09:45:08 +0000760 if (!refineRxSBGMask(RxSBG, Mask))
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000761 return false;
762 }
Richard Sandiford5cbac962013-07-18 09:45:08 +0000763 RxSBG.Input = Input;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000764 return true;
765 }
766
Richard Sandiford51093212013-07-18 10:40:35 +0000767 case ISD::OR: {
768 if (RxSBG.Opcode != SystemZ::RNSBG)
769 return false;
770
Richard Sandiford21f5d682014-03-06 11:22:58 +0000771 auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford51093212013-07-18 10:40:35 +0000772 if (!MaskNode)
773 return false;
774
775 SDValue Input = N.getOperand(0);
776 uint64_t Mask = ~MaskNode->getZExtValue();
777 if (!refineRxSBGMask(RxSBG, Mask)) {
778 // If some bits of Input are already known ones, those bits will have
779 // been removed from the mask. See if adding them back in makes the
780 // mask suitable.
781 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +0000782 CurDAG->computeKnownBits(Input, KnownZero, KnownOne);
Richard Sandiford51093212013-07-18 10:40:35 +0000783 Mask &= ~KnownOne.getZExtValue();
784 if (!refineRxSBGMask(RxSBG, Mask))
785 return false;
786 }
787 RxSBG.Input = Input;
788 return true;
789 }
790
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000791 case ISD::ROTL: {
Richard Sandiford5cbac962013-07-18 09:45:08 +0000792 // Any 64-bit rotate left can be merged into the RxSBG.
Richard Sandiford3e382972013-10-16 13:35:13 +0000793 if (RxSBG.BitSize != 64 || N.getValueType() != MVT::i64)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000794 return false;
Richard Sandiford21f5d682014-03-06 11:22:58 +0000795 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000796 if (!CountNode)
797 return false;
798
Richard Sandiford5cbac962013-07-18 09:45:08 +0000799 RxSBG.Rotate = (RxSBG.Rotate + CountNode->getZExtValue()) & 63;
800 RxSBG.Input = N.getOperand(0);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000801 return true;
802 }
803
Richard Sandiford220ee492013-12-20 11:49:48 +0000804 case ISD::ANY_EXTEND:
805 // Bits above the extended operand are don't-care.
806 RxSBG.Input = N.getOperand(0);
807 return true;
808
Richard Sandiford3875cb62014-01-09 11:28:53 +0000809 case ISD::ZERO_EXTEND:
810 if (RxSBG.Opcode != SystemZ::RNSBG) {
811 // Restrict the mask to the extended operand.
812 unsigned InnerBitSize = N.getOperand(0).getValueType().getSizeInBits();
813 if (!refineRxSBGMask(RxSBG, allOnes(InnerBitSize)))
814 return false;
Richard Sandiford220ee492013-12-20 11:49:48 +0000815
Richard Sandiford3875cb62014-01-09 11:28:53 +0000816 RxSBG.Input = N.getOperand(0);
817 return true;
818 }
819 // Fall through.
Richard Sandiford220ee492013-12-20 11:49:48 +0000820
821 case ISD::SIGN_EXTEND: {
Richard Sandiford3e382972013-10-16 13:35:13 +0000822 // Check that the extension bits are don't-care (i.e. are masked out
823 // by the final mask).
824 unsigned InnerBitSize = N.getOperand(0).getValueType().getSizeInBits();
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000825 if (maskMatters(RxSBG, allOnes(RxSBG.BitSize) - allOnes(InnerBitSize)))
Richard Sandiford3e382972013-10-16 13:35:13 +0000826 return false;
827
828 RxSBG.Input = N.getOperand(0);
829 return true;
830 }
831
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000832 case ISD::SHL: {
Richard Sandiford21f5d682014-03-06 11:22:58 +0000833 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000834 if (!CountNode)
835 return false;
836
837 uint64_t Count = CountNode->getZExtValue();
Richard Sandiford3e382972013-10-16 13:35:13 +0000838 unsigned BitSize = N.getValueType().getSizeInBits();
839 if (Count < 1 || Count >= BitSize)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000840 return false;
841
Richard Sandiford51093212013-07-18 10:40:35 +0000842 if (RxSBG.Opcode == SystemZ::RNSBG) {
843 // Treat (shl X, count) as (rotl X, size-count) as long as the bottom
844 // count bits from RxSBG.Input are ignored.
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000845 if (maskMatters(RxSBG, allOnes(Count)))
Richard Sandiford51093212013-07-18 10:40:35 +0000846 return false;
847 } else {
848 // Treat (shl X, count) as (and (rotl X, count), ~0<<count).
Richard Sandiford3e382972013-10-16 13:35:13 +0000849 if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count) << Count))
Richard Sandiford51093212013-07-18 10:40:35 +0000850 return false;
851 }
852
Richard Sandiford5cbac962013-07-18 09:45:08 +0000853 RxSBG.Rotate = (RxSBG.Rotate + Count) & 63;
854 RxSBG.Input = N.getOperand(0);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000855 return true;
856 }
857
Richard Sandiford297f7d22013-07-18 10:14:55 +0000858 case ISD::SRL:
859 case ISD::SRA: {
Richard Sandiford21f5d682014-03-06 11:22:58 +0000860 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000861 if (!CountNode)
862 return false;
863
864 uint64_t Count = CountNode->getZExtValue();
Richard Sandiford3e382972013-10-16 13:35:13 +0000865 unsigned BitSize = N.getValueType().getSizeInBits();
866 if (Count < 1 || Count >= BitSize)
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000867 return false;
868
Richard Sandiford51093212013-07-18 10:40:35 +0000869 if (RxSBG.Opcode == SystemZ::RNSBG || Opcode == ISD::SRA) {
870 // Treat (srl|sra X, count) as (rotl X, size-count) as long as the top
871 // count bits from RxSBG.Input are ignored.
Richard Sandiforddd7dd932013-11-26 10:53:16 +0000872 if (maskMatters(RxSBG, allOnes(Count) << (BitSize - Count)))
Richard Sandiford297f7d22013-07-18 10:14:55 +0000873 return false;
874 } else {
875 // Treat (srl X, count), mask) as (and (rotl X, size-count), ~0>>count),
876 // which is similar to SLL above.
Richard Sandiford3e382972013-10-16 13:35:13 +0000877 if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count)))
Richard Sandiford297f7d22013-07-18 10:14:55 +0000878 return false;
879 }
880
Richard Sandiford5cbac962013-07-18 09:45:08 +0000881 RxSBG.Rotate = (RxSBG.Rotate - Count) & 63;
882 RxSBG.Input = N.getOperand(0);
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000883 return true;
884 }
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000885 default:
886 return false;
887 }
888}
889
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000890SDValue SystemZDAGToDAGISel::getUNDEF(SDLoc DL, EVT VT) const {
891 SDNode *N = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, VT);
Richard Sandiford84f54a32013-07-11 08:59:12 +0000892 return SDValue(N, 0);
893}
894
Richard Sandiford54b36912013-09-27 15:14:04 +0000895SDValue SystemZDAGToDAGISel::convertTo(SDLoc DL, EVT VT, SDValue N) const {
Richard Sandifordd8163202013-09-13 09:12:44 +0000896 if (N.getValueType() == MVT::i32 && VT == MVT::i64)
Richard Sandiford87a44362013-09-30 10:28:35 +0000897 return CurDAG->getTargetInsertSubreg(SystemZ::subreg_l32,
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000898 DL, VT, getUNDEF(DL, MVT::i64), N);
Richard Sandifordd8163202013-09-13 09:12:44 +0000899 if (N.getValueType() == MVT::i64 && VT == MVT::i32)
Richard Sandiford87a44362013-09-30 10:28:35 +0000900 return CurDAG->getTargetExtractSubreg(SystemZ::subreg_l32, DL, VT, N);
Richard Sandiford84f54a32013-07-11 08:59:12 +0000901 assert(N.getValueType() == VT && "Unexpected value types");
902 return N;
903}
904
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000905SDNode *SystemZDAGToDAGISel::tryRISBGZero(SDNode *N) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000906 SDLoc DL(N);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000907 EVT VT = N->getValueType(0);
Richard Sandiford51093212013-07-18 10:40:35 +0000908 RxSBGOperands RISBG(SystemZ::RISBG, SDValue(N, 0));
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000909 unsigned Count = 0;
Richard Sandiford5cbac962013-07-18 09:45:08 +0000910 while (expandRxSBG(RISBG))
Richard Sandiford3e382972013-10-16 13:35:13 +0000911 if (RISBG.Input.getOpcode() != ISD::ANY_EXTEND)
912 Count += 1;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000913 if (Count == 0)
Craig Topper062a2ba2014-04-25 05:30:21 +0000914 return nullptr;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000915 if (Count == 1) {
916 // Prefer to use normal shift instructions over RISBG, since they can handle
917 // all cases and are sometimes shorter.
918 if (N->getOpcode() != ISD::AND)
Craig Topper062a2ba2014-04-25 05:30:21 +0000919 return nullptr;
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000920
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000921 // Prefer register extensions like LLC over RISBG. Also prefer to start
922 // out with normal ANDs if one instruction would be enough. We can convert
923 // these ANDs into an RISBG later if a three-address instruction is useful.
924 if (VT == MVT::i32 ||
925 RISBG.Mask == 0xff ||
926 RISBG.Mask == 0xffff ||
927 SystemZ::isImmLF(~RISBG.Mask) ||
928 SystemZ::isImmHF(~RISBG.Mask)) {
929 // Force the new mask into the DAG, since it may include known-one bits.
Richard Sandiford21f5d682014-03-06 11:22:58 +0000930 auto *MaskN = cast<ConstantSDNode>(N->getOperand(1).getNode());
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000931 if (MaskN->getZExtValue() != RISBG.Mask) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000932 SDValue NewMask = CurDAG->getConstant(RISBG.Mask, DL, VT);
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000933 N = CurDAG->UpdateNodeOperands(N, N->getOperand(0), NewMask);
934 return SelectCode(N);
935 }
Craig Topper062a2ba2014-04-25 05:30:21 +0000936 return nullptr;
Richard Sandiford6a06ba32013-07-31 11:36:35 +0000937 }
938 }
Richard Sandiford82ec87d2013-07-16 11:02:24 +0000939
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000940 unsigned Opcode = SystemZ::RISBG;
Ulrich Weigand371d10a2015-03-31 12:58:17 +0000941 // Prefer RISBGN if available, since it does not clobber CC.
942 if (Subtarget->hasMiscellaneousExtensions())
943 Opcode = SystemZ::RISBGN;
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000944 EVT OpcodeVT = MVT::i64;
Eric Christophera6734172015-01-31 00:06:45 +0000945 if (VT == MVT::i32 && Subtarget->hasHighWord()) {
Richard Sandiford3ad5a152013-10-01 14:36:20 +0000946 Opcode = SystemZ::RISBMux;
947 OpcodeVT = MVT::i32;
948 RISBG.Start &= 31;
949 RISBG.End &= 31;
950 }
Richard Sandiford84f54a32013-07-11 08:59:12 +0000951 SDValue Ops[5] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000952 getUNDEF(DL, OpcodeVT),
953 convertTo(DL, OpcodeVT, RISBG.Input),
954 CurDAG->getTargetConstant(RISBG.Start, DL, MVT::i32),
955 CurDAG->getTargetConstant(RISBG.End | 128, DL, MVT::i32),
956 CurDAG->getTargetConstant(RISBG.Rotate, DL, MVT::i32)
Richard Sandiford84f54a32013-07-11 08:59:12 +0000957 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000958 N = CurDAG->getMachineNode(Opcode, DL, OpcodeVT, Ops);
959 return convertTo(DL, VT, SDValue(N, 0)).getNode();
Richard Sandiford84f54a32013-07-11 08:59:12 +0000960}
961
Richard Sandiford7878b852013-07-18 10:06:15 +0000962SDNode *SystemZDAGToDAGISel::tryRxSBG(SDNode *N, unsigned Opcode) {
963 // Try treating each operand of N as the second operand of the RxSBG
Richard Sandiford885140c2013-07-16 11:55:57 +0000964 // and see which goes deepest.
Richard Sandiford51093212013-07-18 10:40:35 +0000965 RxSBGOperands RxSBG[] = {
966 RxSBGOperands(Opcode, N->getOperand(0)),
967 RxSBGOperands(Opcode, N->getOperand(1))
968 };
Richard Sandiford885140c2013-07-16 11:55:57 +0000969 unsigned Count[] = { 0, 0 };
970 for (unsigned I = 0; I < 2; ++I)
Richard Sandiford5cbac962013-07-18 09:45:08 +0000971 while (expandRxSBG(RxSBG[I]))
Richard Sandiford3e382972013-10-16 13:35:13 +0000972 if (RxSBG[I].Input.getOpcode() != ISD::ANY_EXTEND)
973 Count[I] += 1;
Richard Sandiford885140c2013-07-16 11:55:57 +0000974
975 // Do nothing if neither operand is suitable.
976 if (Count[0] == 0 && Count[1] == 0)
Craig Topper062a2ba2014-04-25 05:30:21 +0000977 return nullptr;
Richard Sandiford885140c2013-07-16 11:55:57 +0000978
979 // Pick the deepest second operand.
980 unsigned I = Count[0] > Count[1] ? 0 : 1;
981 SDValue Op0 = N->getOperand(I ^ 1);
982
983 // Prefer IC for character insertions from memory.
Richard Sandiford7878b852013-07-18 10:06:15 +0000984 if (Opcode == SystemZ::ROSBG && (RxSBG[I].Mask & 0xff) == 0)
Richard Sandiford21f5d682014-03-06 11:22:58 +0000985 if (auto *Load = dyn_cast<LoadSDNode>(Op0.getNode()))
Richard Sandiford885140c2013-07-16 11:55:57 +0000986 if (Load->getMemoryVT() == MVT::i8)
Craig Topper062a2ba2014-04-25 05:30:21 +0000987 return nullptr;
Richard Sandiford885140c2013-07-16 11:55:57 +0000988
989 // See whether we can avoid an AND in the first operand by converting
990 // ROSBG to RISBG.
Ulrich Weigand371d10a2015-03-31 12:58:17 +0000991 if (Opcode == SystemZ::ROSBG && detectOrAndInsertion(Op0, RxSBG[I].Mask)) {
Richard Sandiford885140c2013-07-16 11:55:57 +0000992 Opcode = SystemZ::RISBG;
Ulrich Weigand371d10a2015-03-31 12:58:17 +0000993 // Prefer RISBGN if available, since it does not clobber CC.
994 if (Subtarget->hasMiscellaneousExtensions())
995 Opcode = SystemZ::RISBGN;
996 }
997
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000998 SDLoc DL(N);
Richard Sandiford885140c2013-07-16 11:55:57 +0000999 EVT VT = N->getValueType(0);
1000 SDValue Ops[5] = {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001001 convertTo(DL, MVT::i64, Op0),
1002 convertTo(DL, MVT::i64, RxSBG[I].Input),
1003 CurDAG->getTargetConstant(RxSBG[I].Start, DL, MVT::i32),
1004 CurDAG->getTargetConstant(RxSBG[I].End, DL, MVT::i32),
1005 CurDAG->getTargetConstant(RxSBG[I].Rotate, DL, MVT::i32)
Richard Sandiford885140c2013-07-16 11:55:57 +00001006 };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001007 N = CurDAG->getMachineNode(Opcode, DL, MVT::i64, Ops);
1008 return convertTo(DL, VT, SDValue(N, 0)).getNode();
Richard Sandiford885140c2013-07-16 11:55:57 +00001009}
1010
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001011SDNode *SystemZDAGToDAGISel::splitLargeImmediate(unsigned Opcode, SDNode *Node,
1012 SDValue Op0, uint64_t UpperVal,
1013 uint64_t LowerVal) {
1014 EVT VT = Node->getValueType(0);
Andrew Trickef9de2a2013-05-25 02:42:55 +00001015 SDLoc DL(Node);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001016 SDValue Upper = CurDAG->getConstant(UpperVal, DL, VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001017 if (Op0.getNode())
1018 Upper = CurDAG->getNode(Opcode, DL, VT, Op0, Upper);
1019 Upper = SDValue(Select(Upper.getNode()), 0);
1020
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001021 SDValue Lower = CurDAG->getConstant(LowerVal, DL, VT);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001022 SDValue Or = CurDAG->getNode(Opcode, DL, VT, Upper, Lower);
1023 return Or.getNode();
1024}
1025
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001026SDNode *SystemZDAGToDAGISel::tryGather(SDNode *N, unsigned Opcode) {
1027 SDValue ElemV = N->getOperand(2);
1028 auto *ElemN = dyn_cast<ConstantSDNode>(ElemV);
1029 if (!ElemN)
1030 return 0;
1031
1032 unsigned Elem = ElemN->getZExtValue();
1033 EVT VT = N->getValueType(0);
1034 if (Elem >= VT.getVectorNumElements())
1035 return 0;
1036
1037 auto *Load = dyn_cast<LoadSDNode>(N->getOperand(1));
1038 if (!Load || !Load->hasOneUse())
1039 return 0;
1040 if (Load->getMemoryVT().getSizeInBits() !=
1041 Load->getValueType(0).getSizeInBits())
1042 return 0;
1043
1044 SDValue Base, Disp, Index;
1045 if (!selectBDVAddr12Only(Load->getBasePtr(), ElemV, Base, Disp, Index) ||
1046 Index.getValueType() != VT.changeVectorElementTypeToInteger())
1047 return 0;
1048
1049 SDLoc DL(Load);
1050 SDValue Ops[] = {
1051 N->getOperand(0), Base, Disp, Index,
1052 CurDAG->getTargetConstant(Elem, DL, MVT::i32), Load->getChain()
1053 };
1054 SDNode *Res = CurDAG->getMachineNode(Opcode, DL, VT, MVT::Other, Ops);
1055 ReplaceUses(SDValue(Load, 1), SDValue(Res, 1));
1056 return Res;
1057}
1058
1059SDNode *SystemZDAGToDAGISel::tryScatter(StoreSDNode *Store, unsigned Opcode) {
1060 SDValue Value = Store->getValue();
1061 if (Value.getOpcode() != ISD::EXTRACT_VECTOR_ELT)
1062 return 0;
1063 if (Store->getMemoryVT().getSizeInBits() !=
1064 Value.getValueType().getSizeInBits())
1065 return 0;
1066
1067 SDValue ElemV = Value.getOperand(1);
1068 auto *ElemN = dyn_cast<ConstantSDNode>(ElemV);
1069 if (!ElemN)
1070 return 0;
1071
1072 SDValue Vec = Value.getOperand(0);
1073 EVT VT = Vec.getValueType();
1074 unsigned Elem = ElemN->getZExtValue();
1075 if (Elem >= VT.getVectorNumElements())
1076 return 0;
1077
1078 SDValue Base, Disp, Index;
1079 if (!selectBDVAddr12Only(Store->getBasePtr(), ElemV, Base, Disp, Index) ||
1080 Index.getValueType() != VT.changeVectorElementTypeToInteger())
1081 return 0;
1082
1083 SDLoc DL(Store);
1084 SDValue Ops[] = {
1085 Vec, Base, Disp, Index, CurDAG->getTargetConstant(Elem, DL, MVT::i32),
1086 Store->getChain()
1087 };
1088 return CurDAG->getMachineNode(Opcode, DL, MVT::Other, Ops);
1089}
1090
Richard Sandiford067817e2013-09-27 15:29:20 +00001091bool SystemZDAGToDAGISel::canUseBlockOperation(StoreSDNode *Store,
1092 LoadSDNode *Load) const {
Richard Sandiford178273a2013-09-05 10:36:45 +00001093 // Check that the two memory operands have the same size.
1094 if (Load->getMemoryVT() != Store->getMemoryVT())
Richard Sandiford97846492013-07-09 09:46:39 +00001095 return false;
1096
Richard Sandiford178273a2013-09-05 10:36:45 +00001097 // Volatility stops an access from being decomposed.
1098 if (Load->isVolatile() || Store->isVolatile())
1099 return false;
Richard Sandiford97846492013-07-09 09:46:39 +00001100
1101 // There's no chance of overlap if the load is invariant.
1102 if (Load->isInvariant())
1103 return true;
1104
Richard Sandiford97846492013-07-09 09:46:39 +00001105 // Otherwise we need to check whether there's an alias.
Nick Lewyckyaad475b2014-04-15 07:22:52 +00001106 const Value *V1 = Load->getMemOperand()->getValue();
1107 const Value *V2 = Store->getMemOperand()->getValue();
Richard Sandiford97846492013-07-09 09:46:39 +00001108 if (!V1 || !V2)
1109 return false;
1110
Richard Sandiford067817e2013-09-27 15:29:20 +00001111 // Reject equality.
1112 uint64_t Size = Load->getMemoryVT().getStoreSize();
Richard Sandiford97846492013-07-09 09:46:39 +00001113 int64_t End1 = Load->getSrcValueOffset() + Size;
1114 int64_t End2 = Store->getSrcValueOffset() + Size;
Richard Sandiford067817e2013-09-27 15:29:20 +00001115 if (V1 == V2 && End1 == End2)
1116 return false;
1117
Chandler Carruthac80dc72015-06-17 07:18:54 +00001118 return !AA->alias(MemoryLocation(V1, End1, Load->getAAInfo()),
1119 MemoryLocation(V2, End2, Store->getAAInfo()));
Richard Sandiford97846492013-07-09 09:46:39 +00001120}
1121
Richard Sandiford178273a2013-09-05 10:36:45 +00001122bool SystemZDAGToDAGISel::storeLoadCanUseMVC(SDNode *N) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001123 auto *Store = cast<StoreSDNode>(N);
1124 auto *Load = cast<LoadSDNode>(Store->getValue());
Richard Sandiford178273a2013-09-05 10:36:45 +00001125
1126 // Prefer not to use MVC if either address can use ... RELATIVE LONG
1127 // instructions.
1128 uint64_t Size = Load->getMemoryVT().getStoreSize();
1129 if (Size > 1 && Size <= 8) {
1130 // Prefer LHRL, LRL and LGRL.
Richard Sandiford54b36912013-09-27 15:14:04 +00001131 if (SystemZISD::isPCREL(Load->getBasePtr().getOpcode()))
Richard Sandiford178273a2013-09-05 10:36:45 +00001132 return false;
1133 // Prefer STHRL, STRL and STGRL.
Richard Sandiford54b36912013-09-27 15:14:04 +00001134 if (SystemZISD::isPCREL(Store->getBasePtr().getOpcode()))
Richard Sandiford178273a2013-09-05 10:36:45 +00001135 return false;
1136 }
1137
Richard Sandiford067817e2013-09-27 15:29:20 +00001138 return canUseBlockOperation(Store, Load);
Richard Sandiford178273a2013-09-05 10:36:45 +00001139}
1140
1141bool SystemZDAGToDAGISel::storeLoadCanUseBlockBinary(SDNode *N,
1142 unsigned I) const {
Richard Sandiford21f5d682014-03-06 11:22:58 +00001143 auto *StoreA = cast<StoreSDNode>(N);
1144 auto *LoadA = cast<LoadSDNode>(StoreA->getValue().getOperand(1 - I));
1145 auto *LoadB = cast<LoadSDNode>(StoreA->getValue().getOperand(I));
Richard Sandiford067817e2013-09-27 15:29:20 +00001146 return !LoadA->isVolatile() && canUseBlockOperation(StoreA, LoadB);
Richard Sandiford178273a2013-09-05 10:36:45 +00001147}
1148
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001149SDNode *SystemZDAGToDAGISel::Select(SDNode *Node) {
1150 // Dump information about the Node being selected
1151 DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n");
1152
1153 // If we have a custom node, we already have selected!
1154 if (Node->isMachineOpcode()) {
1155 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
Tim Northover31d093c2013-09-22 08:21:56 +00001156 Node->setNodeId(-1);
Craig Topper062a2ba2014-04-25 05:30:21 +00001157 return nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001158 }
1159
1160 unsigned Opcode = Node->getOpcode();
Craig Topper062a2ba2014-04-25 05:30:21 +00001161 SDNode *ResNode = nullptr;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001162 switch (Opcode) {
1163 case ISD::OR:
Richard Sandiford885140c2013-07-16 11:55:57 +00001164 if (Node->getOperand(1).getOpcode() != ISD::Constant)
Richard Sandiford7878b852013-07-18 10:06:15 +00001165 ResNode = tryRxSBG(Node, SystemZ::ROSBG);
1166 goto or_xor;
1167
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001168 case ISD::XOR:
Richard Sandiford7878b852013-07-18 10:06:15 +00001169 if (Node->getOperand(1).getOpcode() != ISD::Constant)
1170 ResNode = tryRxSBG(Node, SystemZ::RXSBG);
1171 // Fall through.
1172 or_xor:
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001173 // If this is a 64-bit operation in which both 32-bit halves are nonzero,
1174 // split the operation into two.
Richard Sandiford885140c2013-07-16 11:55:57 +00001175 if (!ResNode && Node->getValueType(0) == MVT::i64)
Richard Sandiford21f5d682014-03-06 11:22:58 +00001176 if (auto *Op1 = dyn_cast<ConstantSDNode>(Node->getOperand(1))) {
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001177 uint64_t Val = Op1->getZExtValue();
1178 if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val))
1179 Node = splitLargeImmediate(Opcode, Node, Node->getOperand(0),
1180 Val - uint32_t(Val), uint32_t(Val));
1181 }
1182 break;
1183
Richard Sandiford84f54a32013-07-11 08:59:12 +00001184 case ISD::AND:
Richard Sandiford51093212013-07-18 10:40:35 +00001185 if (Node->getOperand(1).getOpcode() != ISD::Constant)
1186 ResNode = tryRxSBG(Node, SystemZ::RNSBG);
1187 // Fall through.
Richard Sandiford82ec87d2013-07-16 11:02:24 +00001188 case ISD::ROTL:
1189 case ISD::SHL:
1190 case ISD::SRL:
Richard Sandiford220ee492013-12-20 11:49:48 +00001191 case ISD::ZERO_EXTEND:
Richard Sandiford7878b852013-07-18 10:06:15 +00001192 if (!ResNode)
1193 ResNode = tryRISBGZero(Node);
Richard Sandiford84f54a32013-07-11 08:59:12 +00001194 break;
1195
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001196 case ISD::Constant:
1197 // If this is a 64-bit constant that is out of the range of LLILF,
1198 // LLIHF and LGFI, split it into two 32-bit pieces.
1199 if (Node->getValueType(0) == MVT::i64) {
1200 uint64_t Val = cast<ConstantSDNode>(Node)->getZExtValue();
1201 if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val) && !isInt<32>(Val))
1202 Node = splitLargeImmediate(ISD::OR, Node, SDValue(),
1203 Val - uint32_t(Val), uint32_t(Val));
1204 }
1205 break;
1206
Richard Sandifordee834382013-07-31 12:38:08 +00001207 case SystemZISD::SELECT_CCMASK: {
1208 SDValue Op0 = Node->getOperand(0);
1209 SDValue Op1 = Node->getOperand(1);
1210 // Prefer to put any load first, so that it can be matched as a
1211 // conditional load.
1212 if (Op1.getOpcode() == ISD::LOAD && Op0.getOpcode() != ISD::LOAD) {
1213 SDValue CCValid = Node->getOperand(2);
1214 SDValue CCMask = Node->getOperand(3);
1215 uint64_t ConstCCValid =
1216 cast<ConstantSDNode>(CCValid.getNode())->getZExtValue();
1217 uint64_t ConstCCMask =
1218 cast<ConstantSDNode>(CCMask.getNode())->getZExtValue();
1219 // Invert the condition.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001220 CCMask = CurDAG->getConstant(ConstCCValid ^ ConstCCMask, SDLoc(Node),
Richard Sandifordee834382013-07-31 12:38:08 +00001221 CCMask.getValueType());
1222 SDValue Op4 = Node->getOperand(4);
1223 Node = CurDAG->UpdateNodeOperands(Node, Op1, Op0, CCValid, CCMask, Op4);
1224 }
1225 break;
1226 }
Ulrich Weigandce4c1092015-05-05 19:25:42 +00001227
1228 case ISD::INSERT_VECTOR_ELT: {
1229 EVT VT = Node->getValueType(0);
1230 unsigned ElemBitSize = VT.getVectorElementType().getSizeInBits();
1231 if (ElemBitSize == 32)
1232 ResNode = tryGather(Node, SystemZ::VGEF);
1233 else if (ElemBitSize == 64)
1234 ResNode = tryGather(Node, SystemZ::VGEG);
1235 break;
1236 }
1237
1238 case ISD::STORE: {
1239 auto *Store = cast<StoreSDNode>(Node);
1240 unsigned ElemBitSize = Store->getValue().getValueType().getSizeInBits();
1241 if (ElemBitSize == 32)
1242 ResNode = tryScatter(Store, SystemZ::VSCEF);
1243 else if (ElemBitSize == 64)
1244 ResNode = tryScatter(Store, SystemZ::VSCEG);
1245 break;
1246 }
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001247 }
1248
1249 // Select the default instruction
Richard Sandiford84f54a32013-07-11 08:59:12 +00001250 if (!ResNode)
1251 ResNode = SelectCode(Node);
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001252
1253 DEBUG(errs() << "=> ";
Craig Topper062a2ba2014-04-25 05:30:21 +00001254 if (ResNode == nullptr || ResNode == Node)
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001255 Node->dump(CurDAG);
1256 else
1257 ResNode->dump(CurDAG);
1258 errs() << "\n";
1259 );
1260 return ResNode;
1261}
1262
1263bool SystemZDAGToDAGISel::
1264SelectInlineAsmMemoryOperand(const SDValue &Op,
Daniel Sanders60f1db02015-03-13 12:45:09 +00001265 unsigned ConstraintID,
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001266 std::vector<SDValue> &OutOps) {
Daniel Sanders2eeace22015-03-17 16:16:14 +00001267 switch(ConstraintID) {
1268 default:
1269 llvm_unreachable("Unexpected asm memory constraint");
1270 case InlineAsm::Constraint_i:
1271 case InlineAsm::Constraint_m:
1272 case InlineAsm::Constraint_Q:
1273 case InlineAsm::Constraint_R:
1274 case InlineAsm::Constraint_S:
1275 case InlineAsm::Constraint_T:
1276 // Accept addresses with short displacements, which are compatible
1277 // with Q, R, S and T. But keep the index operand for future expansion.
1278 SDValue Base, Disp, Index;
1279 if (selectBDXAddr(SystemZAddressingMode::FormBD,
1280 SystemZAddressingMode::Disp12Only,
1281 Op, Base, Disp, Index)) {
1282 OutOps.push_back(Base);
1283 OutOps.push_back(Disp);
1284 OutOps.push_back(Index);
1285 return false;
1286 }
1287 break;
1288 }
1289 return true;
Ulrich Weigand5f613df2013-05-06 16:15:19 +00001290}