1 //===-- 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"
15 #include "llvm/Analysis/AliasAnalysis.h"
16 #include "llvm/CodeGen/SelectionDAGISel.h"
17 #include "llvm/Support/Debug.h"
18 #include "llvm/Support/raw_ostream.h"
19 
20 using namespace llvm;
21 
22 #define DEBUG_TYPE "systemz-isel"
23 
24 namespace {
25 // Used to build addressing modes.
26 struct 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 ";
77     if (Base.getNode())
78       Base.getNode()->dump();
79     else
80       errs() << "null\n";
81 
82     if (hasIndexField()) {
83       errs() << " Index ";
84       if (Index.getNode())
85         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 
97 // Return a mask with Count low bits set.
98 static uint64_t allOnes(unsigned int Count) {
99   assert(Count <= 64);
100   if (Count > 63)
101     return UINT64_MAX;
102   return (uint64_t(1) << Count) - 1;
103 }
104 
105 // Represents operands 2 to 5 of the ROTATE AND ... SELECTED BITS operation
106 // given by Opcode.  The operands are: Input (R2), Start (I3), End (I4) and
107 // Rotate (I5).  The combined operand value is effectively:
108 //
109 //   (or (rotl Input, Rotate), ~Mask)
110 //
111 // for RNSBG and:
112 //
113 //   (and (rotl Input, Rotate), Mask)
114 //
115 // otherwise.  The output value has BitSize bits, although Input may be
116 // narrower (in which case the upper bits are don't care).
117 struct RxSBGOperands {
118   RxSBGOperands(unsigned Op, SDValue N)
119     : Opcode(Op), BitSize(N.getValueType().getSizeInBits()),
120       Mask(allOnes(BitSize)), Input(N), Start(64 - BitSize), End(63),
121       Rotate(0) {}
122 
123   unsigned Opcode;
124   unsigned BitSize;
125   uint64_t Mask;
126   SDValue Input;
127   unsigned Start;
128   unsigned End;
129   unsigned Rotate;
130 };
131 
132 class SystemZDAGToDAGISel : public SelectionDAGISel {
133   const SystemZSubtarget *Subtarget;
134 
135   // Used by SystemZOperands.td to create integer constants.
136   inline SDValue getImm(const SDNode *Node, uint64_t Imm) const {
137     return CurDAG->getTargetConstant(Imm, SDLoc(Node), Node->getValueType(0));
138   }
139 
140   const SystemZTargetMachine &getTargetMachine() const {
141     return static_cast<const SystemZTargetMachine &>(TM);
142   }
143 
144   const SystemZInstrInfo *getInstrInfo() const {
145     return Subtarget->getInstrInfo();
146   }
147 
148   // Try to fold more of the base or index of AM into AM, where IsBase
149   // selects between the base and index.
150   bool expandAddress(SystemZAddressingMode &AM, bool IsBase) const;
151 
152   // Try to describe N in AM, returning true on success.
153   bool selectAddress(SDValue N, SystemZAddressingMode &AM) const;
154 
155   // Extract individual target operands from matched address AM.
156   void getAddressOperands(const SystemZAddressingMode &AM, EVT VT,
157                           SDValue &Base, SDValue &Disp) const;
158   void getAddressOperands(const SystemZAddressingMode &AM, EVT VT,
159                           SDValue &Base, SDValue &Disp, SDValue &Index) const;
160 
161   // Try to match Addr as a FormBD address with displacement type DR.
162   // Return true on success, storing the base and displacement in
163   // Base and Disp respectively.
164   bool selectBDAddr(SystemZAddressingMode::DispRange DR, SDValue Addr,
165                     SDValue &Base, SDValue &Disp) const;
166 
167   // Try to match Addr as a FormBDX address with displacement type DR.
168   // Return true on success and if the result had no index.  Store the
169   // base and displacement in Base and Disp respectively.
170   bool selectMVIAddr(SystemZAddressingMode::DispRange DR, SDValue Addr,
171                      SDValue &Base, SDValue &Disp) const;
172 
173   // Try to match Addr as a FormBDX* address of form Form with
174   // displacement type DR.  Return true on success, storing the base,
175   // displacement and index in Base, Disp and Index respectively.
176   bool selectBDXAddr(SystemZAddressingMode::AddrForm Form,
177                      SystemZAddressingMode::DispRange DR, SDValue Addr,
178                      SDValue &Base, SDValue &Disp, SDValue &Index) const;
179 
180   // PC-relative address matching routines used by SystemZOperands.td.
181   bool selectPCRelAddress(SDValue Addr, SDValue &Target) const {
182     if (SystemZISD::isPCREL(Addr.getOpcode())) {
183       Target = Addr.getOperand(0);
184       return true;
185     }
186     return false;
187   }
188 
189   // BD matching routines used by SystemZOperands.td.
190   bool selectBDAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp) const {
191     return selectBDAddr(SystemZAddressingMode::Disp12Only, Addr, Base, Disp);
192   }
193   bool selectBDAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
194     return selectBDAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp);
195   }
196   bool selectBDAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp) const {
197     return selectBDAddr(SystemZAddressingMode::Disp20Only, Addr, Base, Disp);
198   }
199   bool selectBDAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
200     return selectBDAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp);
201   }
202 
203   // MVI matching routines used by SystemZOperands.td.
204   bool selectMVIAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
205     return selectMVIAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp);
206   }
207   bool selectMVIAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
208     return selectMVIAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp);
209   }
210 
211   // BDX matching routines used by SystemZOperands.td.
212   bool selectBDXAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp,
213                            SDValue &Index) const {
214     return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
215                          SystemZAddressingMode::Disp12Only,
216                          Addr, Base, Disp, Index);
217   }
218   bool selectBDXAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
219                            SDValue &Index) const {
220     return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
221                          SystemZAddressingMode::Disp12Pair,
222                          Addr, Base, Disp, Index);
223   }
224   bool selectDynAlloc12Only(SDValue Addr, SDValue &Base, SDValue &Disp,
225                             SDValue &Index) const {
226     return selectBDXAddr(SystemZAddressingMode::FormBDXDynAlloc,
227                          SystemZAddressingMode::Disp12Only,
228                          Addr, Base, Disp, Index);
229   }
230   bool selectBDXAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp,
231                            SDValue &Index) const {
232     return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
233                          SystemZAddressingMode::Disp20Only,
234                          Addr, Base, Disp, Index);
235   }
236   bool selectBDXAddr20Only128(SDValue Addr, SDValue &Base, SDValue &Disp,
237                               SDValue &Index) const {
238     return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
239                          SystemZAddressingMode::Disp20Only128,
240                          Addr, Base, Disp, Index);
241   }
242   bool selectBDXAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
243                            SDValue &Index) const {
244     return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
245                          SystemZAddressingMode::Disp20Pair,
246                          Addr, Base, Disp, Index);
247   }
248   bool selectLAAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
249                           SDValue &Index) const {
250     return selectBDXAddr(SystemZAddressingMode::FormBDXLA,
251                          SystemZAddressingMode::Disp12Pair,
252                          Addr, Base, Disp, Index);
253   }
254   bool selectLAAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
255                           SDValue &Index) const {
256     return selectBDXAddr(SystemZAddressingMode::FormBDXLA,
257                          SystemZAddressingMode::Disp20Pair,
258                          Addr, Base, Disp, Index);
259   }
260 
261   // Try to match Addr as an address with a base, 12-bit displacement
262   // and index, where the index is element Elem of a vector.
263   // Return true on success, storing the base, displacement and vector
264   // in Base, Disp and Index respectively.
265   bool selectBDVAddr12Only(SDValue Addr, SDValue Elem, SDValue &Base,
266                            SDValue &Disp, SDValue &Index) const;
267 
268   // Check whether (or Op (and X InsertMask)) is effectively an insertion
269   // of X into bits InsertMask of some Y != Op.  Return true if so and
270   // set Op to that Y.
271   bool detectOrAndInsertion(SDValue &Op, uint64_t InsertMask) const;
272 
273   // Try to update RxSBG so that only the bits of RxSBG.Input in Mask are used.
274   // Return true on success.
275   bool refineRxSBGMask(RxSBGOperands &RxSBG, uint64_t Mask) const;
276 
277   // Try to fold some of RxSBG.Input into other fields of RxSBG.
278   // Return true on success.
279   bool expandRxSBG(RxSBGOperands &RxSBG) const;
280 
281   // Return an undefined value of type VT.
282   SDValue getUNDEF(SDLoc DL, EVT VT) const;
283 
284   // Convert N to VT, if it isn't already.
285   SDValue convertTo(SDLoc DL, EVT VT, SDValue N) const;
286 
287   // Try to implement AND or shift node N using RISBG with the zero flag set.
288   // Return the selected node on success, otherwise return null.
289   bool tryRISBGZero(SDNode *N);
290 
291   // Try to use RISBG or Opcode to implement OR or XOR node N.
292   // Return the selected node on success, otherwise return null.
293   bool tryRxSBG(SDNode *N, unsigned Opcode);
294 
295   // If Op0 is null, then Node is a constant that can be loaded using:
296   //
297   //   (Opcode UpperVal LowerVal)
298   //
299   // If Op0 is nonnull, then Node can be implemented using:
300   //
301   //   (Opcode (Opcode Op0 UpperVal) LowerVal)
302   void splitLargeImmediate(unsigned Opcode, SDNode *Node, SDValue Op0,
303                            uint64_t UpperVal, uint64_t LowerVal);
304 
305   // Try to use gather instruction Opcode to implement vector insertion N.
306   bool tryGather(SDNode *N, unsigned Opcode);
307 
308   // Try to use scatter instruction Opcode to implement store Store.
309   bool tryScatter(StoreSDNode *Store, unsigned Opcode);
310 
311   // Return true if Load and Store are loads and stores of the same size
312   // and are guaranteed not to overlap.  Such operations can be implemented
313   // using block (SS-format) instructions.
314   //
315   // Partial overlap would lead to incorrect code, since the block operations
316   // are logically bytewise, even though they have a fast path for the
317   // non-overlapping case.  We also need to avoid full overlap (i.e. two
318   // addresses that might be equal at run time) because although that case
319   // would be handled correctly, it might be implemented by millicode.
320   bool canUseBlockOperation(StoreSDNode *Store, LoadSDNode *Load) const;
321 
322   // N is a (store (load Y), X) pattern.  Return true if it can use an MVC
323   // from Y to X.
324   bool storeLoadCanUseMVC(SDNode *N) const;
325 
326   // N is a (store (op (load A[0]), (load A[1])), X) pattern.  Return true
327   // if A[1 - I] == X and if N can use a block operation like NC from A[I]
328   // to X.
329   bool storeLoadCanUseBlockBinary(SDNode *N, unsigned I) const;
330 
331 public:
332   SystemZDAGToDAGISel(SystemZTargetMachine &TM, CodeGenOpt::Level OptLevel)
333       : SelectionDAGISel(TM, OptLevel) {}
334 
335   bool runOnMachineFunction(MachineFunction &MF) override {
336     Subtarget = &MF.getSubtarget<SystemZSubtarget>();
337     return SelectionDAGISel::runOnMachineFunction(MF);
338   }
339 
340   // Override MachineFunctionPass.
341   const char *getPassName() const override {
342     return "SystemZ DAG->DAG Pattern Instruction Selection";
343   }
344 
345   // Override SelectionDAGISel.
346   void Select(SDNode *Node) override;
347   bool SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
348                                     std::vector<SDValue> &OutOps) override;
349 
350   // Include the pieces autogenerated from the target description.
351   #include "SystemZGenDAGISel.inc"
352 };
353 } // end anonymous namespace
354 
355 FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM,
356                                          CodeGenOpt::Level OptLevel) {
357   return new SystemZDAGToDAGISel(TM, OptLevel);
358 }
359 
360 // Return true if Val should be selected as a displacement for an address
361 // with range DR.  Here we're interested in the range of both the instruction
362 // described by DR and of any pairing instruction.
363 static bool selectDisp(SystemZAddressingMode::DispRange DR, int64_t Val) {
364   switch (DR) {
365   case SystemZAddressingMode::Disp12Only:
366     return isUInt<12>(Val);
367 
368   case SystemZAddressingMode::Disp12Pair:
369   case SystemZAddressingMode::Disp20Only:
370   case SystemZAddressingMode::Disp20Pair:
371     return isInt<20>(Val);
372 
373   case SystemZAddressingMode::Disp20Only128:
374     return isInt<20>(Val) && isInt<20>(Val + 8);
375   }
376   llvm_unreachable("Unhandled displacement range");
377 }
378 
379 // Change the base or index in AM to Value, where IsBase selects
380 // between the base and index.
381 static void changeComponent(SystemZAddressingMode &AM, bool IsBase,
382                             SDValue Value) {
383   if (IsBase)
384     AM.Base = Value;
385   else
386     AM.Index = Value;
387 }
388 
389 // The base or index of AM is equivalent to Value + ADJDYNALLOC,
390 // where IsBase selects between the base and index.  Try to fold the
391 // ADJDYNALLOC into AM.
392 static bool expandAdjDynAlloc(SystemZAddressingMode &AM, bool IsBase,
393                               SDValue Value) {
394   if (AM.isDynAlloc() && !AM.IncludesDynAlloc) {
395     changeComponent(AM, IsBase, Value);
396     AM.IncludesDynAlloc = true;
397     return true;
398   }
399   return false;
400 }
401 
402 // The base of AM is equivalent to Base + Index.  Try to use Index as
403 // the index register.
404 static bool expandIndex(SystemZAddressingMode &AM, SDValue Base,
405                         SDValue Index) {
406   if (AM.hasIndexField() && !AM.Index.getNode()) {
407     AM.Base = Base;
408     AM.Index = Index;
409     return true;
410   }
411   return false;
412 }
413 
414 // The base or index of AM is equivalent to Op0 + Op1, where IsBase selects
415 // between the base and index.  Try to fold Op1 into AM's displacement.
416 static bool expandDisp(SystemZAddressingMode &AM, bool IsBase,
417                        SDValue Op0, uint64_t Op1) {
418   // First try adjusting the displacement.
419   int64_t TestDisp = AM.Disp + Op1;
420   if (selectDisp(AM.DR, TestDisp)) {
421     changeComponent(AM, IsBase, Op0);
422     AM.Disp = TestDisp;
423     return true;
424   }
425 
426   // We could consider forcing the displacement into a register and
427   // using it as an index, but it would need to be carefully tuned.
428   return false;
429 }
430 
431 bool SystemZDAGToDAGISel::expandAddress(SystemZAddressingMode &AM,
432                                         bool IsBase) const {
433   SDValue N = IsBase ? AM.Base : AM.Index;
434   unsigned Opcode = N.getOpcode();
435   if (Opcode == ISD::TRUNCATE) {
436     N = N.getOperand(0);
437     Opcode = N.getOpcode();
438   }
439   if (Opcode == ISD::ADD || CurDAG->isBaseWithConstantOffset(N)) {
440     SDValue Op0 = N.getOperand(0);
441     SDValue Op1 = N.getOperand(1);
442 
443     unsigned Op0Code = Op0->getOpcode();
444     unsigned Op1Code = Op1->getOpcode();
445 
446     if (Op0Code == SystemZISD::ADJDYNALLOC)
447       return expandAdjDynAlloc(AM, IsBase, Op1);
448     if (Op1Code == SystemZISD::ADJDYNALLOC)
449       return expandAdjDynAlloc(AM, IsBase, Op0);
450 
451     if (Op0Code == ISD::Constant)
452       return expandDisp(AM, IsBase, Op1,
453                         cast<ConstantSDNode>(Op0)->getSExtValue());
454     if (Op1Code == ISD::Constant)
455       return expandDisp(AM, IsBase, Op0,
456                         cast<ConstantSDNode>(Op1)->getSExtValue());
457 
458     if (IsBase && expandIndex(AM, Op0, Op1))
459       return true;
460   }
461   if (Opcode == SystemZISD::PCREL_OFFSET) {
462     SDValue Full = N.getOperand(0);
463     SDValue Base = N.getOperand(1);
464     SDValue Anchor = Base.getOperand(0);
465     uint64_t Offset = (cast<GlobalAddressSDNode>(Full)->getOffset() -
466                        cast<GlobalAddressSDNode>(Anchor)->getOffset());
467     return expandDisp(AM, IsBase, Base, Offset);
468   }
469   return false;
470 }
471 
472 // Return true if an instruction with displacement range DR should be
473 // used for displacement value Val.  selectDisp(DR, Val) must already hold.
474 static bool isValidDisp(SystemZAddressingMode::DispRange DR, int64_t Val) {
475   assert(selectDisp(DR, Val) && "Invalid displacement");
476   switch (DR) {
477   case SystemZAddressingMode::Disp12Only:
478   case SystemZAddressingMode::Disp20Only:
479   case SystemZAddressingMode::Disp20Only128:
480     return true;
481 
482   case SystemZAddressingMode::Disp12Pair:
483     // Use the other instruction if the displacement is too large.
484     return isUInt<12>(Val);
485 
486   case SystemZAddressingMode::Disp20Pair:
487     // Use the other instruction if the displacement is small enough.
488     return !isUInt<12>(Val);
489   }
490   llvm_unreachable("Unhandled displacement range");
491 }
492 
493 // Return true if Base + Disp + Index should be performed by LA(Y).
494 static bool shouldUseLA(SDNode *Base, int64_t Disp, SDNode *Index) {
495   // Don't use LA(Y) for constants.
496   if (!Base)
497     return false;
498 
499   // Always use LA(Y) for frame addresses, since we know that the destination
500   // register is almost always (perhaps always) going to be different from
501   // the frame register.
502   if (Base->getOpcode() == ISD::FrameIndex)
503     return true;
504 
505   if (Disp) {
506     // Always use LA(Y) if there is a base, displacement and index.
507     if (Index)
508       return true;
509 
510     // Always use LA if the displacement is small enough.  It should always
511     // be no worse than AGHI (and better if it avoids a move).
512     if (isUInt<12>(Disp))
513       return true;
514 
515     // For similar reasons, always use LAY if the constant is too big for AGHI.
516     // LAY should be no worse than AGFI.
517     if (!isInt<16>(Disp))
518       return true;
519   } else {
520     // Don't use LA for plain registers.
521     if (!Index)
522       return false;
523 
524     // Don't use LA for plain addition if the index operand is only used
525     // once.  It should be a natural two-operand addition in that case.
526     if (Index->hasOneUse())
527       return false;
528 
529     // Prefer addition if the second operation is sign-extended, in the
530     // hope of using AGF.
531     unsigned IndexOpcode = Index->getOpcode();
532     if (IndexOpcode == ISD::SIGN_EXTEND ||
533         IndexOpcode == ISD::SIGN_EXTEND_INREG)
534       return false;
535   }
536 
537   // Don't use LA for two-operand addition if either operand is only
538   // used once.  The addition instructions are better in that case.
539   if (Base->hasOneUse())
540     return false;
541 
542   return true;
543 }
544 
545 // Return true if Addr is suitable for AM, updating AM if so.
546 bool SystemZDAGToDAGISel::selectAddress(SDValue Addr,
547                                         SystemZAddressingMode &AM) const {
548   // Start out assuming that the address will need to be loaded separately,
549   // then try to extend it as much as we can.
550   AM.Base = Addr;
551 
552   // First try treating the address as a constant.
553   if (Addr.getOpcode() == ISD::Constant &&
554       expandDisp(AM, true, SDValue(),
555                  cast<ConstantSDNode>(Addr)->getSExtValue()))
556     ;
557   // Also see if it's a bare ADJDYNALLOC.
558   else if (Addr.getOpcode() == SystemZISD::ADJDYNALLOC &&
559            expandAdjDynAlloc(AM, true, SDValue()))
560     ;
561   else
562     // Otherwise try expanding each component.
563     while (expandAddress(AM, true) ||
564            (AM.Index.getNode() && expandAddress(AM, false)))
565       continue;
566 
567   // Reject cases where it isn't profitable to use LA(Y).
568   if (AM.Form == SystemZAddressingMode::FormBDXLA &&
569       !shouldUseLA(AM.Base.getNode(), AM.Disp, AM.Index.getNode()))
570     return false;
571 
572   // Reject cases where the other instruction in a pair should be used.
573   if (!isValidDisp(AM.DR, AM.Disp))
574     return false;
575 
576   // Make sure that ADJDYNALLOC is included where necessary.
577   if (AM.isDynAlloc() && !AM.IncludesDynAlloc)
578     return false;
579 
580   DEBUG(AM.dump());
581   return true;
582 }
583 
584 // Insert a node into the DAG at least before Pos.  This will reposition
585 // the node as needed, and will assign it a node ID that is <= Pos's ID.
586 // Note that this does *not* preserve the uniqueness of node IDs!
587 // The selection DAG must no longer depend on their uniqueness when this
588 // function is used.
589 static void insertDAGNode(SelectionDAG *DAG, SDNode *Pos, SDValue N) {
590   if (N.getNode()->getNodeId() == -1 ||
591       N.getNode()->getNodeId() > Pos->getNodeId()) {
592     DAG->RepositionNode(Pos->getIterator(), N.getNode());
593     N.getNode()->setNodeId(Pos->getNodeId());
594   }
595 }
596 
597 void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
598                                              EVT VT, SDValue &Base,
599                                              SDValue &Disp) const {
600   Base = AM.Base;
601   if (!Base.getNode())
602     // Register 0 means "no base".  This is mostly useful for shifts.
603     Base = CurDAG->getRegister(0, VT);
604   else if (Base.getOpcode() == ISD::FrameIndex) {
605     // Lower a FrameIndex to a TargetFrameIndex.
606     int64_t FrameIndex = cast<FrameIndexSDNode>(Base)->getIndex();
607     Base = CurDAG->getTargetFrameIndex(FrameIndex, VT);
608   } else if (Base.getValueType() != VT) {
609     // Truncate values from i64 to i32, for shifts.
610     assert(VT == MVT::i32 && Base.getValueType() == MVT::i64 &&
611            "Unexpected truncation");
612     SDLoc DL(Base);
613     SDValue Trunc = CurDAG->getNode(ISD::TRUNCATE, DL, VT, Base);
614     insertDAGNode(CurDAG, Base.getNode(), Trunc);
615     Base = Trunc;
616   }
617 
618   // Lower the displacement to a TargetConstant.
619   Disp = CurDAG->getTargetConstant(AM.Disp, SDLoc(Base), VT);
620 }
621 
622 void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
623                                              EVT VT, SDValue &Base,
624                                              SDValue &Disp,
625                                              SDValue &Index) const {
626   getAddressOperands(AM, VT, Base, Disp);
627 
628   Index = AM.Index;
629   if (!Index.getNode())
630     // Register 0 means "no index".
631     Index = CurDAG->getRegister(0, VT);
632 }
633 
634 bool SystemZDAGToDAGISel::selectBDAddr(SystemZAddressingMode::DispRange DR,
635                                        SDValue Addr, SDValue &Base,
636                                        SDValue &Disp) const {
637   SystemZAddressingMode AM(SystemZAddressingMode::FormBD, DR);
638   if (!selectAddress(Addr, AM))
639     return false;
640 
641   getAddressOperands(AM, Addr.getValueType(), Base, Disp);
642   return true;
643 }
644 
645 bool SystemZDAGToDAGISel::selectMVIAddr(SystemZAddressingMode::DispRange DR,
646                                         SDValue Addr, SDValue &Base,
647                                         SDValue &Disp) const {
648   SystemZAddressingMode AM(SystemZAddressingMode::FormBDXNormal, DR);
649   if (!selectAddress(Addr, AM) || AM.Index.getNode())
650     return false;
651 
652   getAddressOperands(AM, Addr.getValueType(), Base, Disp);
653   return true;
654 }
655 
656 bool SystemZDAGToDAGISel::selectBDXAddr(SystemZAddressingMode::AddrForm Form,
657                                         SystemZAddressingMode::DispRange DR,
658                                         SDValue Addr, SDValue &Base,
659                                         SDValue &Disp, SDValue &Index) const {
660   SystemZAddressingMode AM(Form, DR);
661   if (!selectAddress(Addr, AM))
662     return false;
663 
664   getAddressOperands(AM, Addr.getValueType(), Base, Disp, Index);
665   return true;
666 }
667 
668 bool SystemZDAGToDAGISel::selectBDVAddr12Only(SDValue Addr, SDValue Elem,
669                                               SDValue &Base,
670                                               SDValue &Disp,
671                                               SDValue &Index) const {
672   SDValue Regs[2];
673   if (selectBDXAddr12Only(Addr, Regs[0], Disp, Regs[1]) &&
674       Regs[0].getNode() && Regs[1].getNode()) {
675     for (unsigned int I = 0; I < 2; ++I) {
676       Base = Regs[I];
677       Index = Regs[1 - I];
678       // We can't tell here whether the index vector has the right type
679       // for the access; the caller needs to do that instead.
680       if (Index.getOpcode() == ISD::ZERO_EXTEND)
681         Index = Index.getOperand(0);
682       if (Index.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
683           Index.getOperand(1) == Elem) {
684         Index = Index.getOperand(0);
685         return true;
686       }
687     }
688   }
689   return false;
690 }
691 
692 bool SystemZDAGToDAGISel::detectOrAndInsertion(SDValue &Op,
693                                                uint64_t InsertMask) const {
694   // We're only interested in cases where the insertion is into some operand
695   // of Op, rather than into Op itself.  The only useful case is an AND.
696   if (Op.getOpcode() != ISD::AND)
697     return false;
698 
699   // We need a constant mask.
700   auto *MaskNode = dyn_cast<ConstantSDNode>(Op.getOperand(1).getNode());
701   if (!MaskNode)
702     return false;
703 
704   // It's not an insertion of Op.getOperand(0) if the two masks overlap.
705   uint64_t AndMask = MaskNode->getZExtValue();
706   if (InsertMask & AndMask)
707     return false;
708 
709   // It's only an insertion if all bits are covered or are known to be zero.
710   // The inner check covers all cases but is more expensive.
711   uint64_t Used = allOnes(Op.getValueType().getSizeInBits());
712   if (Used != (AndMask | InsertMask)) {
713     APInt KnownZero, KnownOne;
714     CurDAG->computeKnownBits(Op.getOperand(0), KnownZero, KnownOne);
715     if (Used != (AndMask | InsertMask | KnownZero.getZExtValue()))
716       return false;
717   }
718 
719   Op = Op.getOperand(0);
720   return true;
721 }
722 
723 bool SystemZDAGToDAGISel::refineRxSBGMask(RxSBGOperands &RxSBG,
724                                           uint64_t Mask) const {
725   const SystemZInstrInfo *TII = getInstrInfo();
726   if (RxSBG.Rotate != 0)
727     Mask = (Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate));
728   Mask &= RxSBG.Mask;
729   if (TII->isRxSBGMask(Mask, RxSBG.BitSize, RxSBG.Start, RxSBG.End)) {
730     RxSBG.Mask = Mask;
731     return true;
732   }
733   return false;
734 }
735 
736 // Return true if any bits of (RxSBG.Input & Mask) are significant.
737 static bool maskMatters(RxSBGOperands &RxSBG, uint64_t Mask) {
738   // Rotate the mask in the same way as RxSBG.Input is rotated.
739   if (RxSBG.Rotate != 0)
740     Mask = ((Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate)));
741   return (Mask & RxSBG.Mask) != 0;
742 }
743 
744 bool SystemZDAGToDAGISel::expandRxSBG(RxSBGOperands &RxSBG) const {
745   SDValue N = RxSBG.Input;
746   unsigned Opcode = N.getOpcode();
747   switch (Opcode) {
748   case ISD::AND: {
749     if (RxSBG.Opcode == SystemZ::RNSBG)
750       return false;
751 
752     auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
753     if (!MaskNode)
754       return false;
755 
756     SDValue Input = N.getOperand(0);
757     uint64_t Mask = MaskNode->getZExtValue();
758     if (!refineRxSBGMask(RxSBG, Mask)) {
759       // If some bits of Input are already known zeros, those bits will have
760       // been removed from the mask.  See if adding them back in makes the
761       // mask suitable.
762       APInt KnownZero, KnownOne;
763       CurDAG->computeKnownBits(Input, KnownZero, KnownOne);
764       Mask |= KnownZero.getZExtValue();
765       if (!refineRxSBGMask(RxSBG, Mask))
766         return false;
767     }
768     RxSBG.Input = Input;
769     return true;
770   }
771 
772   case ISD::OR: {
773     if (RxSBG.Opcode != SystemZ::RNSBG)
774       return false;
775 
776     auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
777     if (!MaskNode)
778       return false;
779 
780     SDValue Input = N.getOperand(0);
781     uint64_t Mask = ~MaskNode->getZExtValue();
782     if (!refineRxSBGMask(RxSBG, Mask)) {
783       // If some bits of Input are already known ones, those bits will have
784       // been removed from the mask.  See if adding them back in makes the
785       // mask suitable.
786       APInt KnownZero, KnownOne;
787       CurDAG->computeKnownBits(Input, KnownZero, KnownOne);
788       Mask &= ~KnownOne.getZExtValue();
789       if (!refineRxSBGMask(RxSBG, Mask))
790         return false;
791     }
792     RxSBG.Input = Input;
793     return true;
794   }
795 
796   case ISD::ROTL: {
797     // Any 64-bit rotate left can be merged into the RxSBG.
798     if (RxSBG.BitSize != 64 || N.getValueType() != MVT::i64)
799       return false;
800     auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
801     if (!CountNode)
802       return false;
803 
804     RxSBG.Rotate = (RxSBG.Rotate + CountNode->getZExtValue()) & 63;
805     RxSBG.Input = N.getOperand(0);
806     return true;
807   }
808 
809   case ISD::ANY_EXTEND:
810     // Bits above the extended operand are don't-care.
811     RxSBG.Input = N.getOperand(0);
812     return true;
813 
814   case ISD::ZERO_EXTEND:
815     if (RxSBG.Opcode != SystemZ::RNSBG) {
816       // Restrict the mask to the extended operand.
817       unsigned InnerBitSize = N.getOperand(0).getValueType().getSizeInBits();
818       if (!refineRxSBGMask(RxSBG, allOnes(InnerBitSize)))
819         return false;
820 
821       RxSBG.Input = N.getOperand(0);
822       return true;
823     }
824     // Fall through.
825 
826   case ISD::SIGN_EXTEND: {
827     // Check that the extension bits are don't-care (i.e. are masked out
828     // by the final mask).
829     unsigned InnerBitSize = N.getOperand(0).getValueType().getSizeInBits();
830     if (maskMatters(RxSBG, allOnes(RxSBG.BitSize) - allOnes(InnerBitSize)))
831       return false;
832 
833     RxSBG.Input = N.getOperand(0);
834     return true;
835   }
836 
837   case ISD::SHL: {
838     auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
839     if (!CountNode)
840       return false;
841 
842     uint64_t Count = CountNode->getZExtValue();
843     unsigned BitSize = N.getValueType().getSizeInBits();
844     if (Count < 1 || Count >= BitSize)
845       return false;
846 
847     if (RxSBG.Opcode == SystemZ::RNSBG) {
848       // Treat (shl X, count) as (rotl X, size-count) as long as the bottom
849       // count bits from RxSBG.Input are ignored.
850       if (maskMatters(RxSBG, allOnes(Count)))
851         return false;
852     } else {
853       // Treat (shl X, count) as (and (rotl X, count), ~0<<count).
854       if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count) << Count))
855         return false;
856     }
857 
858     RxSBG.Rotate = (RxSBG.Rotate + Count) & 63;
859     RxSBG.Input = N.getOperand(0);
860     return true;
861   }
862 
863   case ISD::SRL:
864   case ISD::SRA: {
865     auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
866     if (!CountNode)
867       return false;
868 
869     uint64_t Count = CountNode->getZExtValue();
870     unsigned BitSize = N.getValueType().getSizeInBits();
871     if (Count < 1 || Count >= BitSize)
872       return false;
873 
874     if (RxSBG.Opcode == SystemZ::RNSBG || Opcode == ISD::SRA) {
875       // Treat (srl|sra X, count) as (rotl X, size-count) as long as the top
876       // count bits from RxSBG.Input are ignored.
877       if (maskMatters(RxSBG, allOnes(Count) << (BitSize - Count)))
878         return false;
879     } else {
880       // Treat (srl X, count), mask) as (and (rotl X, size-count), ~0>>count),
881       // which is similar to SLL above.
882       if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count)))
883         return false;
884     }
885 
886     RxSBG.Rotate = (RxSBG.Rotate - Count) & 63;
887     RxSBG.Input = N.getOperand(0);
888     return true;
889   }
890   default:
891     return false;
892   }
893 }
894 
895 SDValue SystemZDAGToDAGISel::getUNDEF(SDLoc DL, EVT VT) const {
896   SDNode *N = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, VT);
897   return SDValue(N, 0);
898 }
899 
900 SDValue SystemZDAGToDAGISel::convertTo(SDLoc DL, EVT VT, SDValue N) const {
901   if (N.getValueType() == MVT::i32 && VT == MVT::i64)
902     return CurDAG->getTargetInsertSubreg(SystemZ::subreg_l32,
903                                          DL, VT, getUNDEF(DL, MVT::i64), N);
904   if (N.getValueType() == MVT::i64 && VT == MVT::i32)
905     return CurDAG->getTargetExtractSubreg(SystemZ::subreg_l32, DL, VT, N);
906   assert(N.getValueType() == VT && "Unexpected value types");
907   return N;
908 }
909 
910 bool SystemZDAGToDAGISel::tryRISBGZero(SDNode *N) {
911   SDLoc DL(N);
912   EVT VT = N->getValueType(0);
913   if (!VT.isInteger() || VT.getSizeInBits() > 64)
914     return false;
915   RxSBGOperands RISBG(SystemZ::RISBG, SDValue(N, 0));
916   unsigned Count = 0;
917   while (expandRxSBG(RISBG))
918     if (RISBG.Input.getOpcode() != ISD::ANY_EXTEND)
919       Count += 1;
920   if (Count == 0)
921     return false;
922   if (Count == 1) {
923     // Prefer to use normal shift instructions over RISBG, since they can handle
924     // all cases and are sometimes shorter.
925     if (N->getOpcode() != ISD::AND)
926       return false;
927 
928     // Prefer register extensions like LLC over RISBG.  Also prefer to start
929     // out with normal ANDs if one instruction would be enough.  We can convert
930     // these ANDs into an RISBG later if a three-address instruction is useful.
931     if (VT == MVT::i32 ||
932         RISBG.Mask == 0xff ||
933         RISBG.Mask == 0xffff ||
934         SystemZ::isImmLF(~RISBG.Mask) ||
935         SystemZ::isImmHF(~RISBG.Mask)) {
936       // Force the new mask into the DAG, since it may include known-one bits.
937       auto *MaskN = cast<ConstantSDNode>(N->getOperand(1).getNode());
938       if (MaskN->getZExtValue() != RISBG.Mask) {
939         SDValue NewMask = CurDAG->getConstant(RISBG.Mask, DL, VT);
940         N = CurDAG->UpdateNodeOperands(N, N->getOperand(0), NewMask);
941         SelectCode(N);
942         return true;
943       }
944       return false;
945     }
946   }
947 
948   // If the RISBG operands require no rotation and just masks the bottom
949   // 8/16 bits, attempt to convert this to a LLC zero extension.
950   if (RISBG.Rotate == 0 && (RISBG.Mask == 0xff || RISBG.Mask == 0xffff)) {
951     unsigned OpCode = (RISBG.Mask == 0xff ? SystemZ::LLGCR : SystemZ::LLGHR);
952     if (VT == MVT::i32) {
953       if (Subtarget->hasHighWord())
954         OpCode = (RISBG.Mask == 0xff ? SystemZ::LLCRMux : SystemZ::LLHRMux);
955       else
956         OpCode = (RISBG.Mask == 0xff ? SystemZ::LLCR : SystemZ::LLHR);
957     }
958 
959     SDValue In = convertTo(DL, VT, RISBG.Input);
960     SDValue New = convertTo(
961         DL, VT, SDValue(CurDAG->getMachineNode(OpCode, DL, VT, In), 0));
962     ReplaceUses(N, New.getNode());
963     CurDAG->RemoveDeadNode(N);
964     return true;
965   }
966 
967   unsigned Opcode = SystemZ::RISBG;
968   // Prefer RISBGN if available, since it does not clobber CC.
969   if (Subtarget->hasMiscellaneousExtensions())
970     Opcode = SystemZ::RISBGN;
971   EVT OpcodeVT = MVT::i64;
972   if (VT == MVT::i32 && Subtarget->hasHighWord()) {
973     Opcode = SystemZ::RISBMux;
974     OpcodeVT = MVT::i32;
975     RISBG.Start &= 31;
976     RISBG.End &= 31;
977   }
978   SDValue Ops[5] = {
979     getUNDEF(DL, OpcodeVT),
980     convertTo(DL, OpcodeVT, RISBG.Input),
981     CurDAG->getTargetConstant(RISBG.Start, DL, MVT::i32),
982     CurDAG->getTargetConstant(RISBG.End | 128, DL, MVT::i32),
983     CurDAG->getTargetConstant(RISBG.Rotate, DL, MVT::i32)
984   };
985   SDValue New = convertTo(
986       DL, VT, SDValue(CurDAG->getMachineNode(Opcode, DL, OpcodeVT, Ops), 0));
987   ReplaceUses(N, New.getNode());
988   CurDAG->RemoveDeadNode(N);
989   return true;
990 }
991 
992 bool SystemZDAGToDAGISel::tryRxSBG(SDNode *N, unsigned Opcode) {
993   SDLoc DL(N);
994   EVT VT = N->getValueType(0);
995   if (!VT.isInteger() || VT.getSizeInBits() > 64)
996     return false;
997   // Try treating each operand of N as the second operand of the RxSBG
998   // and see which goes deepest.
999   RxSBGOperands RxSBG[] = {
1000     RxSBGOperands(Opcode, N->getOperand(0)),
1001     RxSBGOperands(Opcode, N->getOperand(1))
1002   };
1003   unsigned Count[] = { 0, 0 };
1004   for (unsigned I = 0; I < 2; ++I)
1005     while (expandRxSBG(RxSBG[I]))
1006       if (RxSBG[I].Input.getOpcode() != ISD::ANY_EXTEND)
1007         Count[I] += 1;
1008 
1009   // Do nothing if neither operand is suitable.
1010   if (Count[0] == 0 && Count[1] == 0)
1011     return false;
1012 
1013   // Pick the deepest second operand.
1014   unsigned I = Count[0] > Count[1] ? 0 : 1;
1015   SDValue Op0 = N->getOperand(I ^ 1);
1016 
1017   // Prefer IC for character insertions from memory.
1018   if (Opcode == SystemZ::ROSBG && (RxSBG[I].Mask & 0xff) == 0)
1019     if (auto *Load = dyn_cast<LoadSDNode>(Op0.getNode()))
1020       if (Load->getMemoryVT() == MVT::i8)
1021         return false;
1022 
1023   // See whether we can avoid an AND in the first operand by converting
1024   // ROSBG to RISBG.
1025   if (Opcode == SystemZ::ROSBG && detectOrAndInsertion(Op0, RxSBG[I].Mask)) {
1026     Opcode = SystemZ::RISBG;
1027     // Prefer RISBGN if available, since it does not clobber CC.
1028     if (Subtarget->hasMiscellaneousExtensions())
1029       Opcode = SystemZ::RISBGN;
1030   }
1031 
1032   SDValue Ops[5] = {
1033     convertTo(DL, MVT::i64, Op0),
1034     convertTo(DL, MVT::i64, RxSBG[I].Input),
1035     CurDAG->getTargetConstant(RxSBG[I].Start, DL, MVT::i32),
1036     CurDAG->getTargetConstant(RxSBG[I].End, DL, MVT::i32),
1037     CurDAG->getTargetConstant(RxSBG[I].Rotate, DL, MVT::i32)
1038   };
1039   SDValue New = convertTo(
1040       DL, VT, SDValue(CurDAG->getMachineNode(Opcode, DL, MVT::i64, Ops), 0));
1041   ReplaceNode(N, New.getNode());
1042   return true;
1043 }
1044 
1045 void SystemZDAGToDAGISel::splitLargeImmediate(unsigned Opcode, SDNode *Node,
1046                                               SDValue Op0, uint64_t UpperVal,
1047                                               uint64_t LowerVal) {
1048   EVT VT = Node->getValueType(0);
1049   SDLoc DL(Node);
1050   SDValue Upper = CurDAG->getConstant(UpperVal, DL, VT);
1051   if (Op0.getNode())
1052     Upper = CurDAG->getNode(Opcode, DL, VT, Op0, Upper);
1053 
1054   {
1055     // When we haven't passed in Op0, Upper will be a constant. In order to
1056     // prevent folding back to the large immediate in `Or = getNode(...)` we run
1057     // SelectCode first and end up with an opaque machine node. This means that
1058     // we need to use a handle to keep track of Upper in case it gets CSE'd by
1059     // SelectCode.
1060     //
1061     // Note that in the case where Op0 is passed in we could just call
1062     // SelectCode(Upper) later, along with the SelectCode(Or), and avoid needing
1063     // the handle at all, but it's fine to do it here.
1064     //
1065     // TODO: This is a pretty hacky way to do this. Can we do something that
1066     // doesn't require a two paragraph explanation?
1067     HandleSDNode Handle(Upper);
1068     SelectCode(Upper.getNode());
1069     Upper = Handle.getValue();
1070   }
1071 
1072   SDValue Lower = CurDAG->getConstant(LowerVal, DL, VT);
1073   SDValue Or = CurDAG->getNode(Opcode, DL, VT, Upper, Lower);
1074 
1075   ReplaceUses(Node, Or.getNode());
1076   CurDAG->RemoveDeadNode(Node);
1077 
1078   SelectCode(Or.getNode());
1079 }
1080 
1081 bool SystemZDAGToDAGISel::tryGather(SDNode *N, unsigned Opcode) {
1082   SDValue ElemV = N->getOperand(2);
1083   auto *ElemN = dyn_cast<ConstantSDNode>(ElemV);
1084   if (!ElemN)
1085     return false;
1086 
1087   unsigned Elem = ElemN->getZExtValue();
1088   EVT VT = N->getValueType(0);
1089   if (Elem >= VT.getVectorNumElements())
1090     return false;
1091 
1092   auto *Load = dyn_cast<LoadSDNode>(N->getOperand(1));
1093   if (!Load || !Load->hasOneUse())
1094     return false;
1095   if (Load->getMemoryVT().getSizeInBits() !=
1096       Load->getValueType(0).getSizeInBits())
1097     return false;
1098 
1099   SDValue Base, Disp, Index;
1100   if (!selectBDVAddr12Only(Load->getBasePtr(), ElemV, Base, Disp, Index) ||
1101       Index.getValueType() != VT.changeVectorElementTypeToInteger())
1102     return false;
1103 
1104   SDLoc DL(Load);
1105   SDValue Ops[] = {
1106     N->getOperand(0), Base, Disp, Index,
1107     CurDAG->getTargetConstant(Elem, DL, MVT::i32), Load->getChain()
1108   };
1109   SDNode *Res = CurDAG->getMachineNode(Opcode, DL, VT, MVT::Other, Ops);
1110   ReplaceUses(SDValue(Load, 1), SDValue(Res, 1));
1111   ReplaceNode(N, Res);
1112   return true;
1113 }
1114 
1115 bool SystemZDAGToDAGISel::tryScatter(StoreSDNode *Store, unsigned Opcode) {
1116   SDValue Value = Store->getValue();
1117   if (Value.getOpcode() != ISD::EXTRACT_VECTOR_ELT)
1118     return false;
1119   if (Store->getMemoryVT().getSizeInBits() !=
1120       Value.getValueType().getSizeInBits())
1121     return false;
1122 
1123   SDValue ElemV = Value.getOperand(1);
1124   auto *ElemN = dyn_cast<ConstantSDNode>(ElemV);
1125   if (!ElemN)
1126     return false;
1127 
1128   SDValue Vec = Value.getOperand(0);
1129   EVT VT = Vec.getValueType();
1130   unsigned Elem = ElemN->getZExtValue();
1131   if (Elem >= VT.getVectorNumElements())
1132     return false;
1133 
1134   SDValue Base, Disp, Index;
1135   if (!selectBDVAddr12Only(Store->getBasePtr(), ElemV, Base, Disp, Index) ||
1136       Index.getValueType() != VT.changeVectorElementTypeToInteger())
1137     return false;
1138 
1139   SDLoc DL(Store);
1140   SDValue Ops[] = {
1141     Vec, Base, Disp, Index, CurDAG->getTargetConstant(Elem, DL, MVT::i32),
1142     Store->getChain()
1143   };
1144   ReplaceNode(Store, CurDAG->getMachineNode(Opcode, DL, MVT::Other, Ops));
1145   return true;
1146 }
1147 
1148 bool SystemZDAGToDAGISel::canUseBlockOperation(StoreSDNode *Store,
1149                                                LoadSDNode *Load) const {
1150   // Check that the two memory operands have the same size.
1151   if (Load->getMemoryVT() != Store->getMemoryVT())
1152     return false;
1153 
1154   // Volatility stops an access from being decomposed.
1155   if (Load->isVolatile() || Store->isVolatile())
1156     return false;
1157 
1158   // There's no chance of overlap if the load is invariant.
1159   if (Load->isInvariant())
1160     return true;
1161 
1162   // Otherwise we need to check whether there's an alias.
1163   const Value *V1 = Load->getMemOperand()->getValue();
1164   const Value *V2 = Store->getMemOperand()->getValue();
1165   if (!V1 || !V2)
1166     return false;
1167 
1168   // Reject equality.
1169   uint64_t Size = Load->getMemoryVT().getStoreSize();
1170   int64_t End1 = Load->getSrcValueOffset() + Size;
1171   int64_t End2 = Store->getSrcValueOffset() + Size;
1172   if (V1 == V2 && End1 == End2)
1173     return false;
1174 
1175   return !AA->alias(MemoryLocation(V1, End1, Load->getAAInfo()),
1176                     MemoryLocation(V2, End2, Store->getAAInfo()));
1177 }
1178 
1179 bool SystemZDAGToDAGISel::storeLoadCanUseMVC(SDNode *N) const {
1180   auto *Store = cast<StoreSDNode>(N);
1181   auto *Load = cast<LoadSDNode>(Store->getValue());
1182 
1183   // Prefer not to use MVC if either address can use ... RELATIVE LONG
1184   // instructions.
1185   uint64_t Size = Load->getMemoryVT().getStoreSize();
1186   if (Size > 1 && Size <= 8) {
1187     // Prefer LHRL, LRL and LGRL.
1188     if (SystemZISD::isPCREL(Load->getBasePtr().getOpcode()))
1189       return false;
1190     // Prefer STHRL, STRL and STGRL.
1191     if (SystemZISD::isPCREL(Store->getBasePtr().getOpcode()))
1192       return false;
1193   }
1194 
1195   return canUseBlockOperation(Store, Load);
1196 }
1197 
1198 bool SystemZDAGToDAGISel::storeLoadCanUseBlockBinary(SDNode *N,
1199                                                      unsigned I) const {
1200   auto *StoreA = cast<StoreSDNode>(N);
1201   auto *LoadA = cast<LoadSDNode>(StoreA->getValue().getOperand(1 - I));
1202   auto *LoadB = cast<LoadSDNode>(StoreA->getValue().getOperand(I));
1203   return !LoadA->isVolatile() && canUseBlockOperation(StoreA, LoadB);
1204 }
1205 
1206 void SystemZDAGToDAGISel::Select(SDNode *Node) {
1207   // Dump information about the Node being selected
1208   DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n");
1209 
1210   // If we have a custom node, we already have selected!
1211   if (Node->isMachineOpcode()) {
1212     DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
1213     Node->setNodeId(-1);
1214     return;
1215   }
1216 
1217   unsigned Opcode = Node->getOpcode();
1218   switch (Opcode) {
1219   case ISD::OR:
1220     if (Node->getOperand(1).getOpcode() != ISD::Constant)
1221       if (tryRxSBG(Node, SystemZ::ROSBG))
1222         return;
1223     goto or_xor;
1224 
1225   case ISD::XOR:
1226     if (Node->getOperand(1).getOpcode() != ISD::Constant)
1227       if (tryRxSBG(Node, SystemZ::RXSBG))
1228         return;
1229     // Fall through.
1230   or_xor:
1231     // If this is a 64-bit operation in which both 32-bit halves are nonzero,
1232     // split the operation into two.
1233     if (Node->getValueType(0) == MVT::i64)
1234       if (auto *Op1 = dyn_cast<ConstantSDNode>(Node->getOperand(1))) {
1235         uint64_t Val = Op1->getZExtValue();
1236         if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val)) {
1237           splitLargeImmediate(Opcode, Node, Node->getOperand(0),
1238                               Val - uint32_t(Val), uint32_t(Val));
1239           return;
1240         }
1241       }
1242     break;
1243 
1244   case ISD::AND:
1245     if (Node->getOperand(1).getOpcode() != ISD::Constant)
1246       if (tryRxSBG(Node, SystemZ::RNSBG))
1247         return;
1248     // Fall through.
1249   case ISD::ROTL:
1250   case ISD::SHL:
1251   case ISD::SRL:
1252   case ISD::ZERO_EXTEND:
1253     if (tryRISBGZero(Node))
1254       return;
1255     break;
1256 
1257   case ISD::Constant:
1258     // If this is a 64-bit constant that is out of the range of LLILF,
1259     // LLIHF and LGFI, split it into two 32-bit pieces.
1260     if (Node->getValueType(0) == MVT::i64) {
1261       uint64_t Val = cast<ConstantSDNode>(Node)->getZExtValue();
1262       if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val) && !isInt<32>(Val)) {
1263         splitLargeImmediate(ISD::OR, Node, SDValue(), Val - uint32_t(Val),
1264                             uint32_t(Val));
1265         return;
1266       }
1267     }
1268     break;
1269 
1270   case SystemZISD::SELECT_CCMASK: {
1271     SDValue Op0 = Node->getOperand(0);
1272     SDValue Op1 = Node->getOperand(1);
1273     // Prefer to put any load first, so that it can be matched as a
1274     // conditional load.
1275     if (Op1.getOpcode() == ISD::LOAD && Op0.getOpcode() != ISD::LOAD) {
1276       SDValue CCValid = Node->getOperand(2);
1277       SDValue CCMask = Node->getOperand(3);
1278       uint64_t ConstCCValid =
1279         cast<ConstantSDNode>(CCValid.getNode())->getZExtValue();
1280       uint64_t ConstCCMask =
1281         cast<ConstantSDNode>(CCMask.getNode())->getZExtValue();
1282       // Invert the condition.
1283       CCMask = CurDAG->getConstant(ConstCCValid ^ ConstCCMask, SDLoc(Node),
1284                                    CCMask.getValueType());
1285       SDValue Op4 = Node->getOperand(4);
1286       Node = CurDAG->UpdateNodeOperands(Node, Op1, Op0, CCValid, CCMask, Op4);
1287     }
1288     break;
1289   }
1290 
1291   case ISD::INSERT_VECTOR_ELT: {
1292     EVT VT = Node->getValueType(0);
1293     unsigned ElemBitSize = VT.getVectorElementType().getSizeInBits();
1294     if (ElemBitSize == 32) {
1295       if (tryGather(Node, SystemZ::VGEF))
1296         return;
1297     } else if (ElemBitSize == 64) {
1298       if (tryGather(Node, SystemZ::VGEG))
1299         return;
1300     }
1301     break;
1302   }
1303 
1304   case ISD::STORE: {
1305     auto *Store = cast<StoreSDNode>(Node);
1306     unsigned ElemBitSize = Store->getValue().getValueType().getSizeInBits();
1307     if (ElemBitSize == 32) {
1308       if (tryScatter(Store, SystemZ::VSCEF))
1309         return;
1310     } else if (ElemBitSize == 64) {
1311       if (tryScatter(Store, SystemZ::VSCEG))
1312         return;
1313     }
1314     break;
1315   }
1316   }
1317 
1318   SelectCode(Node);
1319 }
1320 
1321 bool SystemZDAGToDAGISel::
1322 SelectInlineAsmMemoryOperand(const SDValue &Op,
1323                              unsigned ConstraintID,
1324                              std::vector<SDValue> &OutOps) {
1325   switch(ConstraintID) {
1326   default:
1327     llvm_unreachable("Unexpected asm memory constraint");
1328   case InlineAsm::Constraint_i:
1329   case InlineAsm::Constraint_m:
1330   case InlineAsm::Constraint_Q:
1331   case InlineAsm::Constraint_R:
1332   case InlineAsm::Constraint_S:
1333   case InlineAsm::Constraint_T:
1334     // Accept addresses with short displacements, which are compatible
1335     // with Q, R, S and T.  But keep the index operand for future expansion.
1336     SDValue Base, Disp, Index;
1337     if (selectBDXAddr(SystemZAddressingMode::FormBD,
1338                       SystemZAddressingMode::Disp12Only,
1339                       Op, Base, Disp, Index)) {
1340       OutOps.push_back(Base);
1341       OutOps.push_back(Disp);
1342       OutOps.push_back(Index);
1343       return false;
1344     }
1345     break;
1346   }
1347   return true;
1348 }
1349