• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- SystemZISelDAGToDAG.cpp - A dag to dag inst selector for SystemZ --===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines an instruction selector for the SystemZ target.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "SystemZTargetMachine.h"
14 #include "SystemZISelLowering.h"
15 #include "llvm/Analysis/AliasAnalysis.h"
16 #include "llvm/CodeGen/SelectionDAGISel.h"
17 #include "llvm/Support/Debug.h"
18 #include "llvm/Support/KnownBits.h"
19 #include "llvm/Support/raw_ostream.h"
20 
21 using namespace llvm;
22 
23 #define DEBUG_TYPE "systemz-isel"
24 
25 namespace {
26 // Used to build addressing modes.
27 struct SystemZAddressingMode {
28   // The shape of the address.
29   enum AddrForm {
30     // base+displacement
31     FormBD,
32 
33     // base+displacement+index for load and store operands
34     FormBDXNormal,
35 
36     // base+displacement+index for load address operands
37     FormBDXLA,
38 
39     // base+displacement+index+ADJDYNALLOC
40     FormBDXDynAlloc
41   };
42   AddrForm Form;
43 
44   // The type of displacement.  The enum names here correspond directly
45   // to the definitions in SystemZOperand.td.  We could split them into
46   // flags -- single/pair, 128-bit, etc. -- but it hardly seems worth it.
47   enum DispRange {
48     Disp12Only,
49     Disp12Pair,
50     Disp20Only,
51     Disp20Only128,
52     Disp20Pair
53   };
54   DispRange DR;
55 
56   // The parts of the address.  The address is equivalent to:
57   //
58   //     Base + Disp + Index + (IncludesDynAlloc ? ADJDYNALLOC : 0)
59   SDValue Base;
60   int64_t Disp;
61   SDValue Index;
62   bool IncludesDynAlloc;
63 
SystemZAddressingMode__anon9d233d250111::SystemZAddressingMode64   SystemZAddressingMode(AddrForm form, DispRange dr)
65     : Form(form), DR(dr), Base(), Disp(0), Index(),
66       IncludesDynAlloc(false) {}
67 
68   // True if the address can have an index register.
hasIndexField__anon9d233d250111::SystemZAddressingMode69   bool hasIndexField() { return Form != FormBD; }
70 
71   // True if the address can (and must) include ADJDYNALLOC.
isDynAlloc__anon9d233d250111::SystemZAddressingMode72   bool isDynAlloc() { return Form == FormBDXDynAlloc; }
73 
dump__anon9d233d250111::SystemZAddressingMode74   void dump(const llvm::SelectionDAG *DAG) {
75     errs() << "SystemZAddressingMode " << this << '\n';
76 
77     errs() << " Base ";
78     if (Base.getNode())
79       Base.getNode()->dump(DAG);
80     else
81       errs() << "null\n";
82 
83     if (hasIndexField()) {
84       errs() << " Index ";
85       if (Index.getNode())
86         Index.getNode()->dump(DAG);
87       else
88         errs() << "null\n";
89     }
90 
91     errs() << " Disp " << Disp;
92     if (IncludesDynAlloc)
93       errs() << " + ADJDYNALLOC";
94     errs() << '\n';
95   }
96 };
97 
98 // Return a mask with Count low bits set.
allOnes(unsigned int Count)99 static uint64_t allOnes(unsigned int Count) {
100   assert(Count <= 64);
101   if (Count > 63)
102     return UINT64_MAX;
103   return (uint64_t(1) << Count) - 1;
104 }
105 
106 // Represents operands 2 to 5 of the ROTATE AND ... SELECTED BITS operation
107 // given by Opcode.  The operands are: Input (R2), Start (I3), End (I4) and
108 // Rotate (I5).  The combined operand value is effectively:
109 //
110 //   (or (rotl Input, Rotate), ~Mask)
111 //
112 // for RNSBG and:
113 //
114 //   (and (rotl Input, Rotate), Mask)
115 //
116 // otherwise.  The output value has BitSize bits, although Input may be
117 // narrower (in which case the upper bits are don't care), or wider (in which
118 // case the result will be truncated as part of the operation).
119 struct RxSBGOperands {
RxSBGOperands__anon9d233d250111::RxSBGOperands120   RxSBGOperands(unsigned Op, SDValue N)
121     : Opcode(Op), BitSize(N.getValueSizeInBits()),
122       Mask(allOnes(BitSize)), Input(N), Start(64 - BitSize), End(63),
123       Rotate(0) {}
124 
125   unsigned Opcode;
126   unsigned BitSize;
127   uint64_t Mask;
128   SDValue Input;
129   unsigned Start;
130   unsigned End;
131   unsigned Rotate;
132 };
133 
134 class SystemZDAGToDAGISel : public SelectionDAGISel {
135   const SystemZSubtarget *Subtarget;
136 
137   // Used by SystemZOperands.td to create integer constants.
getImm(const SDNode * Node,uint64_t Imm) const138   inline SDValue getImm(const SDNode *Node, uint64_t Imm) const {
139     return CurDAG->getTargetConstant(Imm, SDLoc(Node), Node->getValueType(0));
140   }
141 
getTargetMachine() const142   const SystemZTargetMachine &getTargetMachine() const {
143     return static_cast<const SystemZTargetMachine &>(TM);
144   }
145 
getInstrInfo() const146   const SystemZInstrInfo *getInstrInfo() const {
147     return Subtarget->getInstrInfo();
148   }
149 
150   // Try to fold more of the base or index of AM into AM, where IsBase
151   // selects between the base and index.
152   bool expandAddress(SystemZAddressingMode &AM, bool IsBase) const;
153 
154   // Try to describe N in AM, returning true on success.
155   bool selectAddress(SDValue N, SystemZAddressingMode &AM) const;
156 
157   // Extract individual target operands from matched address AM.
158   void getAddressOperands(const SystemZAddressingMode &AM, EVT VT,
159                           SDValue &Base, SDValue &Disp) const;
160   void getAddressOperands(const SystemZAddressingMode &AM, EVT VT,
161                           SDValue &Base, SDValue &Disp, SDValue &Index) const;
162 
163   // Try to match Addr as a FormBD address with displacement type DR.
164   // Return true on success, storing the base and displacement in
165   // Base and Disp respectively.
166   bool selectBDAddr(SystemZAddressingMode::DispRange DR, SDValue Addr,
167                     SDValue &Base, SDValue &Disp) const;
168 
169   // Try to match Addr as a FormBDX address with displacement type DR.
170   // Return true on success and if the result had no index.  Store the
171   // base and displacement in Base and Disp respectively.
172   bool selectMVIAddr(SystemZAddressingMode::DispRange DR, SDValue Addr,
173                      SDValue &Base, SDValue &Disp) const;
174 
175   // Try to match Addr as a FormBDX* address of form Form with
176   // displacement type DR.  Return true on success, storing the base,
177   // displacement and index in Base, Disp and Index respectively.
178   bool selectBDXAddr(SystemZAddressingMode::AddrForm Form,
179                      SystemZAddressingMode::DispRange DR, SDValue Addr,
180                      SDValue &Base, SDValue &Disp, SDValue &Index) const;
181 
182   // PC-relative address matching routines used by SystemZOperands.td.
selectPCRelAddress(SDValue Addr,SDValue & Target) const183   bool selectPCRelAddress(SDValue Addr, SDValue &Target) const {
184     if (SystemZISD::isPCREL(Addr.getOpcode())) {
185       Target = Addr.getOperand(0);
186       return true;
187     }
188     return false;
189   }
190 
191   // BD matching routines used by SystemZOperands.td.
selectBDAddr12Only(SDValue Addr,SDValue & Base,SDValue & Disp) const192   bool selectBDAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp) const {
193     return selectBDAddr(SystemZAddressingMode::Disp12Only, Addr, Base, Disp);
194   }
selectBDAddr12Pair(SDValue Addr,SDValue & Base,SDValue & Disp) const195   bool selectBDAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
196     return selectBDAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp);
197   }
selectBDAddr20Only(SDValue Addr,SDValue & Base,SDValue & Disp) const198   bool selectBDAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp) const {
199     return selectBDAddr(SystemZAddressingMode::Disp20Only, Addr, Base, Disp);
200   }
selectBDAddr20Pair(SDValue Addr,SDValue & Base,SDValue & Disp) const201   bool selectBDAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
202     return selectBDAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp);
203   }
204 
205   // MVI matching routines used by SystemZOperands.td.
selectMVIAddr12Pair(SDValue Addr,SDValue & Base,SDValue & Disp) const206   bool selectMVIAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
207     return selectMVIAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp);
208   }
selectMVIAddr20Pair(SDValue Addr,SDValue & Base,SDValue & Disp) const209   bool selectMVIAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
210     return selectMVIAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp);
211   }
212 
213   // BDX matching routines used by SystemZOperands.td.
selectBDXAddr12Only(SDValue Addr,SDValue & Base,SDValue & Disp,SDValue & Index) const214   bool selectBDXAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp,
215                            SDValue &Index) const {
216     return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
217                          SystemZAddressingMode::Disp12Only,
218                          Addr, Base, Disp, Index);
219   }
selectBDXAddr12Pair(SDValue Addr,SDValue & Base,SDValue & Disp,SDValue & Index) const220   bool selectBDXAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
221                            SDValue &Index) const {
222     return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
223                          SystemZAddressingMode::Disp12Pair,
224                          Addr, Base, Disp, Index);
225   }
selectDynAlloc12Only(SDValue Addr,SDValue & Base,SDValue & Disp,SDValue & Index) const226   bool selectDynAlloc12Only(SDValue Addr, SDValue &Base, SDValue &Disp,
227                             SDValue &Index) const {
228     return selectBDXAddr(SystemZAddressingMode::FormBDXDynAlloc,
229                          SystemZAddressingMode::Disp12Only,
230                          Addr, Base, Disp, Index);
231   }
selectBDXAddr20Only(SDValue Addr,SDValue & Base,SDValue & Disp,SDValue & Index) const232   bool selectBDXAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp,
233                            SDValue &Index) const {
234     return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
235                          SystemZAddressingMode::Disp20Only,
236                          Addr, Base, Disp, Index);
237   }
selectBDXAddr20Only128(SDValue Addr,SDValue & Base,SDValue & Disp,SDValue & Index) const238   bool selectBDXAddr20Only128(SDValue Addr, SDValue &Base, SDValue &Disp,
239                               SDValue &Index) const {
240     return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
241                          SystemZAddressingMode::Disp20Only128,
242                          Addr, Base, Disp, Index);
243   }
selectBDXAddr20Pair(SDValue Addr,SDValue & Base,SDValue & Disp,SDValue & Index) const244   bool selectBDXAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
245                            SDValue &Index) const {
246     return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
247                          SystemZAddressingMode::Disp20Pair,
248                          Addr, Base, Disp, Index);
249   }
selectLAAddr12Pair(SDValue Addr,SDValue & Base,SDValue & Disp,SDValue & Index) const250   bool selectLAAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
251                           SDValue &Index) const {
252     return selectBDXAddr(SystemZAddressingMode::FormBDXLA,
253                          SystemZAddressingMode::Disp12Pair,
254                          Addr, Base, Disp, Index);
255   }
selectLAAddr20Pair(SDValue Addr,SDValue & Base,SDValue & Disp,SDValue & Index) const256   bool selectLAAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
257                           SDValue &Index) const {
258     return selectBDXAddr(SystemZAddressingMode::FormBDXLA,
259                          SystemZAddressingMode::Disp20Pair,
260                          Addr, Base, Disp, Index);
261   }
262 
263   // Try to match Addr as an address with a base, 12-bit displacement
264   // and index, where the index is element Elem of a vector.
265   // Return true on success, storing the base, displacement and vector
266   // in Base, Disp and Index respectively.
267   bool selectBDVAddr12Only(SDValue Addr, SDValue Elem, SDValue &Base,
268                            SDValue &Disp, SDValue &Index) const;
269 
270   // Check whether (or Op (and X InsertMask)) is effectively an insertion
271   // of X into bits InsertMask of some Y != Op.  Return true if so and
272   // set Op to that Y.
273   bool detectOrAndInsertion(SDValue &Op, uint64_t InsertMask) const;
274 
275   // Try to update RxSBG so that only the bits of RxSBG.Input in Mask are used.
276   // Return true on success.
277   bool refineRxSBGMask(RxSBGOperands &RxSBG, uint64_t Mask) const;
278 
279   // Try to fold some of RxSBG.Input into other fields of RxSBG.
280   // Return true on success.
281   bool expandRxSBG(RxSBGOperands &RxSBG) const;
282 
283   // Return an undefined value of type VT.
284   SDValue getUNDEF(const SDLoc &DL, EVT VT) const;
285 
286   // Convert N to VT, if it isn't already.
287   SDValue convertTo(const SDLoc &DL, EVT VT, SDValue N) const;
288 
289   // Try to implement AND or shift node N using RISBG with the zero flag set.
290   // Return the selected node on success, otherwise return null.
291   bool tryRISBGZero(SDNode *N);
292 
293   // Try to use RISBG or Opcode to implement OR or XOR node N.
294   // Return the selected node on success, otherwise return null.
295   bool tryRxSBG(SDNode *N, unsigned Opcode);
296 
297   // If Op0 is null, then Node is a constant that can be loaded using:
298   //
299   //   (Opcode UpperVal LowerVal)
300   //
301   // If Op0 is nonnull, then Node can be implemented using:
302   //
303   //   (Opcode (Opcode Op0 UpperVal) LowerVal)
304   void splitLargeImmediate(unsigned Opcode, SDNode *Node, SDValue Op0,
305                            uint64_t UpperVal, uint64_t LowerVal);
306 
307   void loadVectorConstant(const SystemZVectorConstantInfo &VCI,
308                           SDNode *Node);
309 
310   // Try to use gather instruction Opcode to implement vector insertion N.
311   bool tryGather(SDNode *N, unsigned Opcode);
312 
313   // Try to use scatter instruction Opcode to implement store Store.
314   bool tryScatter(StoreSDNode *Store, unsigned Opcode);
315 
316   // Change a chain of {load; op; store} of the same value into a simple op
317   // through memory of that value, if the uses of the modified value and its
318   // address are suitable.
319   bool tryFoldLoadStoreIntoMemOperand(SDNode *Node);
320 
321   // Return true if Load and Store are loads and stores of the same size
322   // and are guaranteed not to overlap.  Such operations can be implemented
323   // using block (SS-format) instructions.
324   //
325   // Partial overlap would lead to incorrect code, since the block operations
326   // are logically bytewise, even though they have a fast path for the
327   // non-overlapping case.  We also need to avoid full overlap (i.e. two
328   // addresses that might be equal at run time) because although that case
329   // would be handled correctly, it might be implemented by millicode.
330   bool canUseBlockOperation(StoreSDNode *Store, LoadSDNode *Load) const;
331 
332   // N is a (store (load Y), X) pattern.  Return true if it can use an MVC
333   // from Y to X.
334   bool storeLoadCanUseMVC(SDNode *N) const;
335 
336   // N is a (store (op (load A[0]), (load A[1])), X) pattern.  Return true
337   // if A[1 - I] == X and if N can use a block operation like NC from A[I]
338   // to X.
339   bool storeLoadCanUseBlockBinary(SDNode *N, unsigned I) const;
340 
341   // Try to expand a boolean SELECT_CCMASK using an IPM sequence.
342   SDValue expandSelectBoolean(SDNode *Node);
343 
344 public:
SystemZDAGToDAGISel(SystemZTargetMachine & TM,CodeGenOpt::Level OptLevel)345   SystemZDAGToDAGISel(SystemZTargetMachine &TM, CodeGenOpt::Level OptLevel)
346       : SelectionDAGISel(TM, OptLevel) {}
347 
runOnMachineFunction(MachineFunction & MF)348   bool runOnMachineFunction(MachineFunction &MF) override {
349     const Function &F = MF.getFunction();
350     if (F.getFnAttribute("fentry-call").getValueAsString() != "true") {
351       if (F.hasFnAttribute("mnop-mcount"))
352         report_fatal_error("mnop-mcount only supported with fentry-call");
353       if (F.hasFnAttribute("mrecord-mcount"))
354         report_fatal_error("mrecord-mcount only supported with fentry-call");
355     }
356 
357     Subtarget = &MF.getSubtarget<SystemZSubtarget>();
358     return SelectionDAGISel::runOnMachineFunction(MF);
359   }
360 
361   // Override MachineFunctionPass.
getPassName() const362   StringRef getPassName() const override {
363     return "SystemZ DAG->DAG Pattern Instruction Selection";
364   }
365 
366   // Override SelectionDAGISel.
367   void Select(SDNode *Node) override;
368   bool SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
369                                     std::vector<SDValue> &OutOps) override;
370   bool IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const override;
371   void PreprocessISelDAG() override;
372 
373   // Include the pieces autogenerated from the target description.
374   #include "SystemZGenDAGISel.inc"
375 };
376 } // end anonymous namespace
377 
createSystemZISelDag(SystemZTargetMachine & TM,CodeGenOpt::Level OptLevel)378 FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM,
379                                          CodeGenOpt::Level OptLevel) {
380   return new SystemZDAGToDAGISel(TM, OptLevel);
381 }
382 
383 // Return true if Val should be selected as a displacement for an address
384 // with range DR.  Here we're interested in the range of both the instruction
385 // described by DR and of any pairing instruction.
selectDisp(SystemZAddressingMode::DispRange DR,int64_t Val)386 static bool selectDisp(SystemZAddressingMode::DispRange DR, int64_t Val) {
387   switch (DR) {
388   case SystemZAddressingMode::Disp12Only:
389     return isUInt<12>(Val);
390 
391   case SystemZAddressingMode::Disp12Pair:
392   case SystemZAddressingMode::Disp20Only:
393   case SystemZAddressingMode::Disp20Pair:
394     return isInt<20>(Val);
395 
396   case SystemZAddressingMode::Disp20Only128:
397     return isInt<20>(Val) && isInt<20>(Val + 8);
398   }
399   llvm_unreachable("Unhandled displacement range");
400 }
401 
402 // Change the base or index in AM to Value, where IsBase selects
403 // between the base and index.
changeComponent(SystemZAddressingMode & AM,bool IsBase,SDValue Value)404 static void changeComponent(SystemZAddressingMode &AM, bool IsBase,
405                             SDValue Value) {
406   if (IsBase)
407     AM.Base = Value;
408   else
409     AM.Index = Value;
410 }
411 
412 // The base or index of AM is equivalent to Value + ADJDYNALLOC,
413 // where IsBase selects between the base and index.  Try to fold the
414 // ADJDYNALLOC into AM.
expandAdjDynAlloc(SystemZAddressingMode & AM,bool IsBase,SDValue Value)415 static bool expandAdjDynAlloc(SystemZAddressingMode &AM, bool IsBase,
416                               SDValue Value) {
417   if (AM.isDynAlloc() && !AM.IncludesDynAlloc) {
418     changeComponent(AM, IsBase, Value);
419     AM.IncludesDynAlloc = true;
420     return true;
421   }
422   return false;
423 }
424 
425 // The base of AM is equivalent to Base + Index.  Try to use Index as
426 // the index register.
expandIndex(SystemZAddressingMode & AM,SDValue Base,SDValue Index)427 static bool expandIndex(SystemZAddressingMode &AM, SDValue Base,
428                         SDValue Index) {
429   if (AM.hasIndexField() && !AM.Index.getNode()) {
430     AM.Base = Base;
431     AM.Index = Index;
432     return true;
433   }
434   return false;
435 }
436 
437 // The base or index of AM is equivalent to Op0 + Op1, where IsBase selects
438 // between the base and index.  Try to fold Op1 into AM's displacement.
expandDisp(SystemZAddressingMode & AM,bool IsBase,SDValue Op0,uint64_t Op1)439 static bool expandDisp(SystemZAddressingMode &AM, bool IsBase,
440                        SDValue Op0, uint64_t Op1) {
441   // First try adjusting the displacement.
442   int64_t TestDisp = AM.Disp + Op1;
443   if (selectDisp(AM.DR, TestDisp)) {
444     changeComponent(AM, IsBase, Op0);
445     AM.Disp = TestDisp;
446     return true;
447   }
448 
449   // We could consider forcing the displacement into a register and
450   // using it as an index, but it would need to be carefully tuned.
451   return false;
452 }
453 
expandAddress(SystemZAddressingMode & AM,bool IsBase) const454 bool SystemZDAGToDAGISel::expandAddress(SystemZAddressingMode &AM,
455                                         bool IsBase) const {
456   SDValue N = IsBase ? AM.Base : AM.Index;
457   unsigned Opcode = N.getOpcode();
458   if (Opcode == ISD::TRUNCATE) {
459     N = N.getOperand(0);
460     Opcode = N.getOpcode();
461   }
462   if (Opcode == ISD::ADD || CurDAG->isBaseWithConstantOffset(N)) {
463     SDValue Op0 = N.getOperand(0);
464     SDValue Op1 = N.getOperand(1);
465 
466     unsigned Op0Code = Op0->getOpcode();
467     unsigned Op1Code = Op1->getOpcode();
468 
469     if (Op0Code == SystemZISD::ADJDYNALLOC)
470       return expandAdjDynAlloc(AM, IsBase, Op1);
471     if (Op1Code == SystemZISD::ADJDYNALLOC)
472       return expandAdjDynAlloc(AM, IsBase, Op0);
473 
474     if (Op0Code == ISD::Constant)
475       return expandDisp(AM, IsBase, Op1,
476                         cast<ConstantSDNode>(Op0)->getSExtValue());
477     if (Op1Code == ISD::Constant)
478       return expandDisp(AM, IsBase, Op0,
479                         cast<ConstantSDNode>(Op1)->getSExtValue());
480 
481     if (IsBase && expandIndex(AM, Op0, Op1))
482       return true;
483   }
484   if (Opcode == SystemZISD::PCREL_OFFSET) {
485     SDValue Full = N.getOperand(0);
486     SDValue Base = N.getOperand(1);
487     SDValue Anchor = Base.getOperand(0);
488     uint64_t Offset = (cast<GlobalAddressSDNode>(Full)->getOffset() -
489                        cast<GlobalAddressSDNode>(Anchor)->getOffset());
490     return expandDisp(AM, IsBase, Base, Offset);
491   }
492   return false;
493 }
494 
495 // Return true if an instruction with displacement range DR should be
496 // used for displacement value Val.  selectDisp(DR, Val) must already hold.
isValidDisp(SystemZAddressingMode::DispRange DR,int64_t Val)497 static bool isValidDisp(SystemZAddressingMode::DispRange DR, int64_t Val) {
498   assert(selectDisp(DR, Val) && "Invalid displacement");
499   switch (DR) {
500   case SystemZAddressingMode::Disp12Only:
501   case SystemZAddressingMode::Disp20Only:
502   case SystemZAddressingMode::Disp20Only128:
503     return true;
504 
505   case SystemZAddressingMode::Disp12Pair:
506     // Use the other instruction if the displacement is too large.
507     return isUInt<12>(Val);
508 
509   case SystemZAddressingMode::Disp20Pair:
510     // Use the other instruction if the displacement is small enough.
511     return !isUInt<12>(Val);
512   }
513   llvm_unreachable("Unhandled displacement range");
514 }
515 
516 // Return true if Base + Disp + Index should be performed by LA(Y).
shouldUseLA(SDNode * Base,int64_t Disp,SDNode * Index)517 static bool shouldUseLA(SDNode *Base, int64_t Disp, SDNode *Index) {
518   // Don't use LA(Y) for constants.
519   if (!Base)
520     return false;
521 
522   // Always use LA(Y) for frame addresses, since we know that the destination
523   // register is almost always (perhaps always) going to be different from
524   // the frame register.
525   if (Base->getOpcode() == ISD::FrameIndex)
526     return true;
527 
528   if (Disp) {
529     // Always use LA(Y) if there is a base, displacement and index.
530     if (Index)
531       return true;
532 
533     // Always use LA if the displacement is small enough.  It should always
534     // be no worse than AGHI (and better if it avoids a move).
535     if (isUInt<12>(Disp))
536       return true;
537 
538     // For similar reasons, always use LAY if the constant is too big for AGHI.
539     // LAY should be no worse than AGFI.
540     if (!isInt<16>(Disp))
541       return true;
542   } else {
543     // Don't use LA for plain registers.
544     if (!Index)
545       return false;
546 
547     // Don't use LA for plain addition if the index operand is only used
548     // once.  It should be a natural two-operand addition in that case.
549     if (Index->hasOneUse())
550       return false;
551 
552     // Prefer addition if the second operation is sign-extended, in the
553     // hope of using AGF.
554     unsigned IndexOpcode = Index->getOpcode();
555     if (IndexOpcode == ISD::SIGN_EXTEND ||
556         IndexOpcode == ISD::SIGN_EXTEND_INREG)
557       return false;
558   }
559 
560   // Don't use LA for two-operand addition if either operand is only
561   // used once.  The addition instructions are better in that case.
562   if (Base->hasOneUse())
563     return false;
564 
565   return true;
566 }
567 
568 // Return true if Addr is suitable for AM, updating AM if so.
selectAddress(SDValue Addr,SystemZAddressingMode & AM) const569 bool SystemZDAGToDAGISel::selectAddress(SDValue Addr,
570                                         SystemZAddressingMode &AM) const {
571   // Start out assuming that the address will need to be loaded separately,
572   // then try to extend it as much as we can.
573   AM.Base = Addr;
574 
575   // First try treating the address as a constant.
576   if (Addr.getOpcode() == ISD::Constant &&
577       expandDisp(AM, true, SDValue(),
578                  cast<ConstantSDNode>(Addr)->getSExtValue()))
579     ;
580   // Also see if it's a bare ADJDYNALLOC.
581   else if (Addr.getOpcode() == SystemZISD::ADJDYNALLOC &&
582            expandAdjDynAlloc(AM, true, SDValue()))
583     ;
584   else
585     // Otherwise try expanding each component.
586     while (expandAddress(AM, true) ||
587            (AM.Index.getNode() && expandAddress(AM, false)))
588       continue;
589 
590   // Reject cases where it isn't profitable to use LA(Y).
591   if (AM.Form == SystemZAddressingMode::FormBDXLA &&
592       !shouldUseLA(AM.Base.getNode(), AM.Disp, AM.Index.getNode()))
593     return false;
594 
595   // Reject cases where the other instruction in a pair should be used.
596   if (!isValidDisp(AM.DR, AM.Disp))
597     return false;
598 
599   // Make sure that ADJDYNALLOC is included where necessary.
600   if (AM.isDynAlloc() && !AM.IncludesDynAlloc)
601     return false;
602 
603   LLVM_DEBUG(AM.dump(CurDAG));
604   return true;
605 }
606 
607 // Insert a node into the DAG at least before Pos.  This will reposition
608 // the node as needed, and will assign it a node ID that is <= Pos's ID.
609 // Note that this does *not* preserve the uniqueness of node IDs!
610 // The selection DAG must no longer depend on their uniqueness when this
611 // function is used.
insertDAGNode(SelectionDAG * DAG,SDNode * Pos,SDValue N)612 static void insertDAGNode(SelectionDAG *DAG, SDNode *Pos, SDValue N) {
613   if (N->getNodeId() == -1 ||
614       (SelectionDAGISel::getUninvalidatedNodeId(N.getNode()) >
615        SelectionDAGISel::getUninvalidatedNodeId(Pos))) {
616     DAG->RepositionNode(Pos->getIterator(), N.getNode());
617     // Mark Node as invalid for pruning as after this it may be a successor to a
618     // selected node but otherwise be in the same position of Pos.
619     // Conservatively mark it with the same -abs(Id) to assure node id
620     // invariant is preserved.
621     N->setNodeId(Pos->getNodeId());
622     SelectionDAGISel::InvalidateNodeId(N.getNode());
623   }
624 }
625 
getAddressOperands(const SystemZAddressingMode & AM,EVT VT,SDValue & Base,SDValue & Disp) const626 void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
627                                              EVT VT, SDValue &Base,
628                                              SDValue &Disp) const {
629   Base = AM.Base;
630   if (!Base.getNode())
631     // Register 0 means "no base".  This is mostly useful for shifts.
632     Base = CurDAG->getRegister(0, VT);
633   else if (Base.getOpcode() == ISD::FrameIndex) {
634     // Lower a FrameIndex to a TargetFrameIndex.
635     int64_t FrameIndex = cast<FrameIndexSDNode>(Base)->getIndex();
636     Base = CurDAG->getTargetFrameIndex(FrameIndex, VT);
637   } else if (Base.getValueType() != VT) {
638     // Truncate values from i64 to i32, for shifts.
639     assert(VT == MVT::i32 && Base.getValueType() == MVT::i64 &&
640            "Unexpected truncation");
641     SDLoc DL(Base);
642     SDValue Trunc = CurDAG->getNode(ISD::TRUNCATE, DL, VT, Base);
643     insertDAGNode(CurDAG, Base.getNode(), Trunc);
644     Base = Trunc;
645   }
646 
647   // Lower the displacement to a TargetConstant.
648   Disp = CurDAG->getTargetConstant(AM.Disp, SDLoc(Base), VT);
649 }
650 
getAddressOperands(const SystemZAddressingMode & AM,EVT VT,SDValue & Base,SDValue & Disp,SDValue & Index) const651 void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
652                                              EVT VT, SDValue &Base,
653                                              SDValue &Disp,
654                                              SDValue &Index) const {
655   getAddressOperands(AM, VT, Base, Disp);
656 
657   Index = AM.Index;
658   if (!Index.getNode())
659     // Register 0 means "no index".
660     Index = CurDAG->getRegister(0, VT);
661 }
662 
selectBDAddr(SystemZAddressingMode::DispRange DR,SDValue Addr,SDValue & Base,SDValue & Disp) const663 bool SystemZDAGToDAGISel::selectBDAddr(SystemZAddressingMode::DispRange DR,
664                                        SDValue Addr, SDValue &Base,
665                                        SDValue &Disp) const {
666   SystemZAddressingMode AM(SystemZAddressingMode::FormBD, DR);
667   if (!selectAddress(Addr, AM))
668     return false;
669 
670   getAddressOperands(AM, Addr.getValueType(), Base, Disp);
671   return true;
672 }
673 
selectMVIAddr(SystemZAddressingMode::DispRange DR,SDValue Addr,SDValue & Base,SDValue & Disp) const674 bool SystemZDAGToDAGISel::selectMVIAddr(SystemZAddressingMode::DispRange DR,
675                                         SDValue Addr, SDValue &Base,
676                                         SDValue &Disp) const {
677   SystemZAddressingMode AM(SystemZAddressingMode::FormBDXNormal, DR);
678   if (!selectAddress(Addr, AM) || AM.Index.getNode())
679     return false;
680 
681   getAddressOperands(AM, Addr.getValueType(), Base, Disp);
682   return true;
683 }
684 
selectBDXAddr(SystemZAddressingMode::AddrForm Form,SystemZAddressingMode::DispRange DR,SDValue Addr,SDValue & Base,SDValue & Disp,SDValue & Index) const685 bool SystemZDAGToDAGISel::selectBDXAddr(SystemZAddressingMode::AddrForm Form,
686                                         SystemZAddressingMode::DispRange DR,
687                                         SDValue Addr, SDValue &Base,
688                                         SDValue &Disp, SDValue &Index) const {
689   SystemZAddressingMode AM(Form, DR);
690   if (!selectAddress(Addr, AM))
691     return false;
692 
693   getAddressOperands(AM, Addr.getValueType(), Base, Disp, Index);
694   return true;
695 }
696 
selectBDVAddr12Only(SDValue Addr,SDValue Elem,SDValue & Base,SDValue & Disp,SDValue & Index) const697 bool SystemZDAGToDAGISel::selectBDVAddr12Only(SDValue Addr, SDValue Elem,
698                                               SDValue &Base,
699                                               SDValue &Disp,
700                                               SDValue &Index) const {
701   SDValue Regs[2];
702   if (selectBDXAddr12Only(Addr, Regs[0], Disp, Regs[1]) &&
703       Regs[0].getNode() && Regs[1].getNode()) {
704     for (unsigned int I = 0; I < 2; ++I) {
705       Base = Regs[I];
706       Index = Regs[1 - I];
707       // We can't tell here whether the index vector has the right type
708       // for the access; the caller needs to do that instead.
709       if (Index.getOpcode() == ISD::ZERO_EXTEND)
710         Index = Index.getOperand(0);
711       if (Index.getOpcode() == ISD::EXTRACT_VECTOR_ELT &&
712           Index.getOperand(1) == Elem) {
713         Index = Index.getOperand(0);
714         return true;
715       }
716     }
717   }
718   return false;
719 }
720 
detectOrAndInsertion(SDValue & Op,uint64_t InsertMask) const721 bool SystemZDAGToDAGISel::detectOrAndInsertion(SDValue &Op,
722                                                uint64_t InsertMask) const {
723   // We're only interested in cases where the insertion is into some operand
724   // of Op, rather than into Op itself.  The only useful case is an AND.
725   if (Op.getOpcode() != ISD::AND)
726     return false;
727 
728   // We need a constant mask.
729   auto *MaskNode = dyn_cast<ConstantSDNode>(Op.getOperand(1).getNode());
730   if (!MaskNode)
731     return false;
732 
733   // It's not an insertion of Op.getOperand(0) if the two masks overlap.
734   uint64_t AndMask = MaskNode->getZExtValue();
735   if (InsertMask & AndMask)
736     return false;
737 
738   // It's only an insertion if all bits are covered or are known to be zero.
739   // The inner check covers all cases but is more expensive.
740   uint64_t Used = allOnes(Op.getValueSizeInBits());
741   if (Used != (AndMask | InsertMask)) {
742     KnownBits Known = CurDAG->computeKnownBits(Op.getOperand(0));
743     if (Used != (AndMask | InsertMask | Known.Zero.getZExtValue()))
744       return false;
745   }
746 
747   Op = Op.getOperand(0);
748   return true;
749 }
750 
refineRxSBGMask(RxSBGOperands & RxSBG,uint64_t Mask) const751 bool SystemZDAGToDAGISel::refineRxSBGMask(RxSBGOperands &RxSBG,
752                                           uint64_t Mask) const {
753   const SystemZInstrInfo *TII = getInstrInfo();
754   if (RxSBG.Rotate != 0)
755     Mask = (Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate));
756   Mask &= RxSBG.Mask;
757   if (TII->isRxSBGMask(Mask, RxSBG.BitSize, RxSBG.Start, RxSBG.End)) {
758     RxSBG.Mask = Mask;
759     return true;
760   }
761   return false;
762 }
763 
764 // Return true if any bits of (RxSBG.Input & Mask) are significant.
maskMatters(RxSBGOperands & RxSBG,uint64_t Mask)765 static bool maskMatters(RxSBGOperands &RxSBG, uint64_t Mask) {
766   // Rotate the mask in the same way as RxSBG.Input is rotated.
767   if (RxSBG.Rotate != 0)
768     Mask = ((Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate)));
769   return (Mask & RxSBG.Mask) != 0;
770 }
771 
expandRxSBG(RxSBGOperands & RxSBG) const772 bool SystemZDAGToDAGISel::expandRxSBG(RxSBGOperands &RxSBG) const {
773   SDValue N = RxSBG.Input;
774   unsigned Opcode = N.getOpcode();
775   switch (Opcode) {
776   case ISD::TRUNCATE: {
777     if (RxSBG.Opcode == SystemZ::RNSBG)
778       return false;
779     uint64_t BitSize = N.getValueSizeInBits();
780     uint64_t Mask = allOnes(BitSize);
781     if (!refineRxSBGMask(RxSBG, Mask))
782       return false;
783     RxSBG.Input = N.getOperand(0);
784     return true;
785   }
786   case ISD::AND: {
787     if (RxSBG.Opcode == SystemZ::RNSBG)
788       return false;
789 
790     auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
791     if (!MaskNode)
792       return false;
793 
794     SDValue Input = N.getOperand(0);
795     uint64_t Mask = MaskNode->getZExtValue();
796     if (!refineRxSBGMask(RxSBG, Mask)) {
797       // If some bits of Input are already known zeros, those bits will have
798       // been removed from the mask.  See if adding them back in makes the
799       // mask suitable.
800       KnownBits Known = CurDAG->computeKnownBits(Input);
801       Mask |= Known.Zero.getZExtValue();
802       if (!refineRxSBGMask(RxSBG, Mask))
803         return false;
804     }
805     RxSBG.Input = Input;
806     return true;
807   }
808 
809   case ISD::OR: {
810     if (RxSBG.Opcode != SystemZ::RNSBG)
811       return false;
812 
813     auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
814     if (!MaskNode)
815       return false;
816 
817     SDValue Input = N.getOperand(0);
818     uint64_t Mask = ~MaskNode->getZExtValue();
819     if (!refineRxSBGMask(RxSBG, Mask)) {
820       // If some bits of Input are already known ones, those bits will have
821       // been removed from the mask.  See if adding them back in makes the
822       // mask suitable.
823       KnownBits Known = CurDAG->computeKnownBits(Input);
824       Mask &= ~Known.One.getZExtValue();
825       if (!refineRxSBGMask(RxSBG, Mask))
826         return false;
827     }
828     RxSBG.Input = Input;
829     return true;
830   }
831 
832   case ISD::ROTL: {
833     // Any 64-bit rotate left can be merged into the RxSBG.
834     if (RxSBG.BitSize != 64 || N.getValueType() != MVT::i64)
835       return false;
836     auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
837     if (!CountNode)
838       return false;
839 
840     RxSBG.Rotate = (RxSBG.Rotate + CountNode->getZExtValue()) & 63;
841     RxSBG.Input = N.getOperand(0);
842     return true;
843   }
844 
845   case ISD::ANY_EXTEND:
846     // Bits above the extended operand are don't-care.
847     RxSBG.Input = N.getOperand(0);
848     return true;
849 
850   case ISD::ZERO_EXTEND:
851     if (RxSBG.Opcode != SystemZ::RNSBG) {
852       // Restrict the mask to the extended operand.
853       unsigned InnerBitSize = N.getOperand(0).getValueSizeInBits();
854       if (!refineRxSBGMask(RxSBG, allOnes(InnerBitSize)))
855         return false;
856 
857       RxSBG.Input = N.getOperand(0);
858       return true;
859     }
860     LLVM_FALLTHROUGH;
861 
862   case ISD::SIGN_EXTEND: {
863     // Check that the extension bits are don't-care (i.e. are masked out
864     // by the final mask).
865     unsigned BitSize = N.getValueSizeInBits();
866     unsigned InnerBitSize = N.getOperand(0).getValueSizeInBits();
867     if (maskMatters(RxSBG, allOnes(BitSize) - allOnes(InnerBitSize))) {
868       // In the case where only the sign bit is active, increase Rotate with
869       // the extension width.
870       if (RxSBG.Mask == 1 && RxSBG.Rotate == 1)
871         RxSBG.Rotate += (BitSize - InnerBitSize);
872       else
873         return false;
874     }
875 
876     RxSBG.Input = N.getOperand(0);
877     return true;
878   }
879 
880   case ISD::SHL: {
881     auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
882     if (!CountNode)
883       return false;
884 
885     uint64_t Count = CountNode->getZExtValue();
886     unsigned BitSize = N.getValueSizeInBits();
887     if (Count < 1 || Count >= BitSize)
888       return false;
889 
890     if (RxSBG.Opcode == SystemZ::RNSBG) {
891       // Treat (shl X, count) as (rotl X, size-count) as long as the bottom
892       // count bits from RxSBG.Input are ignored.
893       if (maskMatters(RxSBG, allOnes(Count)))
894         return false;
895     } else {
896       // Treat (shl X, count) as (and (rotl X, count), ~0<<count).
897       if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count) << Count))
898         return false;
899     }
900 
901     RxSBG.Rotate = (RxSBG.Rotate + Count) & 63;
902     RxSBG.Input = N.getOperand(0);
903     return true;
904   }
905 
906   case ISD::SRL:
907   case ISD::SRA: {
908     auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
909     if (!CountNode)
910       return false;
911 
912     uint64_t Count = CountNode->getZExtValue();
913     unsigned BitSize = N.getValueSizeInBits();
914     if (Count < 1 || Count >= BitSize)
915       return false;
916 
917     if (RxSBG.Opcode == SystemZ::RNSBG || Opcode == ISD::SRA) {
918       // Treat (srl|sra X, count) as (rotl X, size-count) as long as the top
919       // count bits from RxSBG.Input are ignored.
920       if (maskMatters(RxSBG, allOnes(Count) << (BitSize - Count)))
921         return false;
922     } else {
923       // Treat (srl X, count), mask) as (and (rotl X, size-count), ~0>>count),
924       // which is similar to SLL above.
925       if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count)))
926         return false;
927     }
928 
929     RxSBG.Rotate = (RxSBG.Rotate - Count) & 63;
930     RxSBG.Input = N.getOperand(0);
931     return true;
932   }
933   default:
934     return false;
935   }
936 }
937 
getUNDEF(const SDLoc & DL,EVT VT) const938 SDValue SystemZDAGToDAGISel::getUNDEF(const SDLoc &DL, EVT VT) const {
939   SDNode *N = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, VT);
940   return SDValue(N, 0);
941 }
942 
convertTo(const SDLoc & DL,EVT VT,SDValue N) const943 SDValue SystemZDAGToDAGISel::convertTo(const SDLoc &DL, EVT VT,
944                                        SDValue N) const {
945   if (N.getValueType() == MVT::i32 && VT == MVT::i64)
946     return CurDAG->getTargetInsertSubreg(SystemZ::subreg_l32,
947                                          DL, VT, getUNDEF(DL, MVT::i64), N);
948   if (N.getValueType() == MVT::i64 && VT == MVT::i32)
949     return CurDAG->getTargetExtractSubreg(SystemZ::subreg_l32, DL, VT, N);
950   assert(N.getValueType() == VT && "Unexpected value types");
951   return N;
952 }
953 
tryRISBGZero(SDNode * N)954 bool SystemZDAGToDAGISel::tryRISBGZero(SDNode *N) {
955   SDLoc DL(N);
956   EVT VT = N->getValueType(0);
957   if (!VT.isInteger() || VT.getSizeInBits() > 64)
958     return false;
959   RxSBGOperands RISBG(SystemZ::RISBG, SDValue(N, 0));
960   unsigned Count = 0;
961   while (expandRxSBG(RISBG))
962     // The widening or narrowing is expected to be free.
963     // Counting widening or narrowing as a saved operation will result in
964     // preferring an R*SBG over a simple shift/logical instruction.
965     if (RISBG.Input.getOpcode() != ISD::ANY_EXTEND &&
966         RISBG.Input.getOpcode() != ISD::TRUNCATE)
967       Count += 1;
968   if (Count == 0)
969     return false;
970 
971   // Prefer to use normal shift instructions over RISBG, since they can handle
972   // all cases and are sometimes shorter.
973   if (Count == 1 && N->getOpcode() != ISD::AND)
974     return false;
975 
976   // Prefer register extensions like LLC over RISBG.  Also prefer to start
977   // out with normal ANDs if one instruction would be enough.  We can convert
978   // these ANDs into an RISBG later if a three-address instruction is useful.
979   if (RISBG.Rotate == 0) {
980     bool PreferAnd = false;
981     // Prefer AND for any 32-bit and-immediate operation.
982     if (VT == MVT::i32)
983       PreferAnd = true;
984     // As well as for any 64-bit operation that can be implemented via LLC(R),
985     // LLH(R), LLGT(R), or one of the and-immediate instructions.
986     else if (RISBG.Mask == 0xff ||
987              RISBG.Mask == 0xffff ||
988              RISBG.Mask == 0x7fffffff ||
989              SystemZ::isImmLF(~RISBG.Mask) ||
990              SystemZ::isImmHF(~RISBG.Mask))
991      PreferAnd = true;
992     // And likewise for the LLZRGF instruction, which doesn't have a register
993     // to register version.
994     else if (auto *Load = dyn_cast<LoadSDNode>(RISBG.Input)) {
995       if (Load->getMemoryVT() == MVT::i32 &&
996           (Load->getExtensionType() == ISD::EXTLOAD ||
997            Load->getExtensionType() == ISD::ZEXTLOAD) &&
998           RISBG.Mask == 0xffffff00 &&
999           Subtarget->hasLoadAndZeroRightmostByte())
1000       PreferAnd = true;
1001     }
1002     if (PreferAnd) {
1003       // Replace the current node with an AND.  Note that the current node
1004       // might already be that same AND, in which case it is already CSE'd
1005       // with it, and we must not call ReplaceNode.
1006       SDValue In = convertTo(DL, VT, RISBG.Input);
1007       SDValue Mask = CurDAG->getConstant(RISBG.Mask, DL, VT);
1008       SDValue New = CurDAG->getNode(ISD::AND, DL, VT, In, Mask);
1009       if (N != New.getNode()) {
1010         insertDAGNode(CurDAG, N, Mask);
1011         insertDAGNode(CurDAG, N, New);
1012         ReplaceNode(N, New.getNode());
1013         N = New.getNode();
1014       }
1015       // Now, select the machine opcode to implement this operation.
1016       if (!N->isMachineOpcode())
1017         SelectCode(N);
1018       return true;
1019     }
1020   }
1021 
1022   unsigned Opcode = SystemZ::RISBG;
1023   // Prefer RISBGN if available, since it does not clobber CC.
1024   if (Subtarget->hasMiscellaneousExtensions())
1025     Opcode = SystemZ::RISBGN;
1026   EVT OpcodeVT = MVT::i64;
1027   if (VT == MVT::i32 && Subtarget->hasHighWord() &&
1028       // We can only use the 32-bit instructions if all source bits are
1029       // in the low 32 bits without wrapping, both after rotation (because
1030       // of the smaller range for Start and End) and before rotation
1031       // (because the input value is truncated).
1032       RISBG.Start >= 32 && RISBG.End >= RISBG.Start &&
1033       ((RISBG.Start + RISBG.Rotate) & 63) >= 32 &&
1034       ((RISBG.End + RISBG.Rotate) & 63) >=
1035       ((RISBG.Start + RISBG.Rotate) & 63)) {
1036     Opcode = SystemZ::RISBMux;
1037     OpcodeVT = MVT::i32;
1038     RISBG.Start &= 31;
1039     RISBG.End &= 31;
1040   }
1041   SDValue Ops[5] = {
1042     getUNDEF(DL, OpcodeVT),
1043     convertTo(DL, OpcodeVT, RISBG.Input),
1044     CurDAG->getTargetConstant(RISBG.Start, DL, MVT::i32),
1045     CurDAG->getTargetConstant(RISBG.End | 128, DL, MVT::i32),
1046     CurDAG->getTargetConstant(RISBG.Rotate, DL, MVT::i32)
1047   };
1048   SDValue New = convertTo(
1049       DL, VT, SDValue(CurDAG->getMachineNode(Opcode, DL, OpcodeVT, Ops), 0));
1050   ReplaceNode(N, New.getNode());
1051   return true;
1052 }
1053 
tryRxSBG(SDNode * N,unsigned Opcode)1054 bool SystemZDAGToDAGISel::tryRxSBG(SDNode *N, unsigned Opcode) {
1055   SDLoc DL(N);
1056   EVT VT = N->getValueType(0);
1057   if (!VT.isInteger() || VT.getSizeInBits() > 64)
1058     return false;
1059   // Try treating each operand of N as the second operand of the RxSBG
1060   // and see which goes deepest.
1061   RxSBGOperands RxSBG[] = {
1062     RxSBGOperands(Opcode, N->getOperand(0)),
1063     RxSBGOperands(Opcode, N->getOperand(1))
1064   };
1065   unsigned Count[] = { 0, 0 };
1066   for (unsigned I = 0; I < 2; ++I)
1067     while (expandRxSBG(RxSBG[I]))
1068       // The widening or narrowing is expected to be free.
1069       // Counting widening or narrowing as a saved operation will result in
1070       // preferring an R*SBG over a simple shift/logical instruction.
1071       if (RxSBG[I].Input.getOpcode() != ISD::ANY_EXTEND &&
1072           RxSBG[I].Input.getOpcode() != ISD::TRUNCATE)
1073         Count[I] += 1;
1074 
1075   // Do nothing if neither operand is suitable.
1076   if (Count[0] == 0 && Count[1] == 0)
1077     return false;
1078 
1079   // Pick the deepest second operand.
1080   unsigned I = Count[0] > Count[1] ? 0 : 1;
1081   SDValue Op0 = N->getOperand(I ^ 1);
1082 
1083   // Prefer IC for character insertions from memory.
1084   if (Opcode == SystemZ::ROSBG && (RxSBG[I].Mask & 0xff) == 0)
1085     if (auto *Load = dyn_cast<LoadSDNode>(Op0.getNode()))
1086       if (Load->getMemoryVT() == MVT::i8)
1087         return false;
1088 
1089   // See whether we can avoid an AND in the first operand by converting
1090   // ROSBG to RISBG.
1091   if (Opcode == SystemZ::ROSBG && detectOrAndInsertion(Op0, RxSBG[I].Mask)) {
1092     Opcode = SystemZ::RISBG;
1093     // Prefer RISBGN if available, since it does not clobber CC.
1094     if (Subtarget->hasMiscellaneousExtensions())
1095       Opcode = SystemZ::RISBGN;
1096   }
1097 
1098   SDValue Ops[5] = {
1099     convertTo(DL, MVT::i64, Op0),
1100     convertTo(DL, MVT::i64, RxSBG[I].Input),
1101     CurDAG->getTargetConstant(RxSBG[I].Start, DL, MVT::i32),
1102     CurDAG->getTargetConstant(RxSBG[I].End, DL, MVT::i32),
1103     CurDAG->getTargetConstant(RxSBG[I].Rotate, DL, MVT::i32)
1104   };
1105   SDValue New = convertTo(
1106       DL, VT, SDValue(CurDAG->getMachineNode(Opcode, DL, MVT::i64, Ops), 0));
1107   ReplaceNode(N, New.getNode());
1108   return true;
1109 }
1110 
splitLargeImmediate(unsigned Opcode,SDNode * Node,SDValue Op0,uint64_t UpperVal,uint64_t LowerVal)1111 void SystemZDAGToDAGISel::splitLargeImmediate(unsigned Opcode, SDNode *Node,
1112                                               SDValue Op0, uint64_t UpperVal,
1113                                               uint64_t LowerVal) {
1114   EVT VT = Node->getValueType(0);
1115   SDLoc DL(Node);
1116   SDValue Upper = CurDAG->getConstant(UpperVal, DL, VT);
1117   if (Op0.getNode())
1118     Upper = CurDAG->getNode(Opcode, DL, VT, Op0, Upper);
1119 
1120   {
1121     // When we haven't passed in Op0, Upper will be a constant. In order to
1122     // prevent folding back to the large immediate in `Or = getNode(...)` we run
1123     // SelectCode first and end up with an opaque machine node. This means that
1124     // we need to use a handle to keep track of Upper in case it gets CSE'd by
1125     // SelectCode.
1126     //
1127     // Note that in the case where Op0 is passed in we could just call
1128     // SelectCode(Upper) later, along with the SelectCode(Or), and avoid needing
1129     // the handle at all, but it's fine to do it here.
1130     //
1131     // TODO: This is a pretty hacky way to do this. Can we do something that
1132     // doesn't require a two paragraph explanation?
1133     HandleSDNode Handle(Upper);
1134     SelectCode(Upper.getNode());
1135     Upper = Handle.getValue();
1136   }
1137 
1138   SDValue Lower = CurDAG->getConstant(LowerVal, DL, VT);
1139   SDValue Or = CurDAG->getNode(Opcode, DL, VT, Upper, Lower);
1140 
1141   ReplaceNode(Node, Or.getNode());
1142 
1143   SelectCode(Or.getNode());
1144 }
1145 
loadVectorConstant(const SystemZVectorConstantInfo & VCI,SDNode * Node)1146 void SystemZDAGToDAGISel::loadVectorConstant(
1147     const SystemZVectorConstantInfo &VCI, SDNode *Node) {
1148   assert((VCI.Opcode == SystemZISD::BYTE_MASK ||
1149           VCI.Opcode == SystemZISD::REPLICATE ||
1150           VCI.Opcode == SystemZISD::ROTATE_MASK) &&
1151          "Bad opcode!");
1152   assert(VCI.VecVT.getSizeInBits() == 128 && "Expected a vector type");
1153   EVT VT = Node->getValueType(0);
1154   SDLoc DL(Node);
1155   SmallVector<SDValue, 2> Ops;
1156   for (unsigned OpVal : VCI.OpVals)
1157     Ops.push_back(CurDAG->getTargetConstant(OpVal, DL, MVT::i32));
1158   SDValue Op = CurDAG->getNode(VCI.Opcode, DL, VCI.VecVT, Ops);
1159 
1160   if (VCI.VecVT == VT.getSimpleVT())
1161     ReplaceNode(Node, Op.getNode());
1162   else if (VT.getSizeInBits() == 128) {
1163     SDValue BitCast = CurDAG->getNode(ISD::BITCAST, DL, VT, Op);
1164     ReplaceNode(Node, BitCast.getNode());
1165     SelectCode(BitCast.getNode());
1166   } else { // float or double
1167     unsigned SubRegIdx =
1168         (VT.getSizeInBits() == 32 ? SystemZ::subreg_h32 : SystemZ::subreg_h64);
1169     ReplaceNode(
1170         Node, CurDAG->getTargetExtractSubreg(SubRegIdx, DL, VT, Op).getNode());
1171   }
1172   SelectCode(Op.getNode());
1173 }
1174 
tryGather(SDNode * N,unsigned Opcode)1175 bool SystemZDAGToDAGISel::tryGather(SDNode *N, unsigned Opcode) {
1176   SDValue ElemV = N->getOperand(2);
1177   auto *ElemN = dyn_cast<ConstantSDNode>(ElemV);
1178   if (!ElemN)
1179     return false;
1180 
1181   unsigned Elem = ElemN->getZExtValue();
1182   EVT VT = N->getValueType(0);
1183   if (Elem >= VT.getVectorNumElements())
1184     return false;
1185 
1186   auto *Load = dyn_cast<LoadSDNode>(N->getOperand(1));
1187   if (!Load || !Load->hasNUsesOfValue(1, 0))
1188     return false;
1189   if (Load->getMemoryVT().getSizeInBits() !=
1190       Load->getValueType(0).getSizeInBits())
1191     return false;
1192 
1193   SDValue Base, Disp, Index;
1194   if (!selectBDVAddr12Only(Load->getBasePtr(), ElemV, Base, Disp, Index) ||
1195       Index.getValueType() != VT.changeVectorElementTypeToInteger())
1196     return false;
1197 
1198   SDLoc DL(Load);
1199   SDValue Ops[] = {
1200     N->getOperand(0), Base, Disp, Index,
1201     CurDAG->getTargetConstant(Elem, DL, MVT::i32), Load->getChain()
1202   };
1203   SDNode *Res = CurDAG->getMachineNode(Opcode, DL, VT, MVT::Other, Ops);
1204   ReplaceUses(SDValue(Load, 1), SDValue(Res, 1));
1205   ReplaceNode(N, Res);
1206   return true;
1207 }
1208 
tryScatter(StoreSDNode * Store,unsigned Opcode)1209 bool SystemZDAGToDAGISel::tryScatter(StoreSDNode *Store, unsigned Opcode) {
1210   SDValue Value = Store->getValue();
1211   if (Value.getOpcode() != ISD::EXTRACT_VECTOR_ELT)
1212     return false;
1213   if (Store->getMemoryVT().getSizeInBits() != Value.getValueSizeInBits())
1214     return false;
1215 
1216   SDValue ElemV = Value.getOperand(1);
1217   auto *ElemN = dyn_cast<ConstantSDNode>(ElemV);
1218   if (!ElemN)
1219     return false;
1220 
1221   SDValue Vec = Value.getOperand(0);
1222   EVT VT = Vec.getValueType();
1223   unsigned Elem = ElemN->getZExtValue();
1224   if (Elem >= VT.getVectorNumElements())
1225     return false;
1226 
1227   SDValue Base, Disp, Index;
1228   if (!selectBDVAddr12Only(Store->getBasePtr(), ElemV, Base, Disp, Index) ||
1229       Index.getValueType() != VT.changeVectorElementTypeToInteger())
1230     return false;
1231 
1232   SDLoc DL(Store);
1233   SDValue Ops[] = {
1234     Vec, Base, Disp, Index, CurDAG->getTargetConstant(Elem, DL, MVT::i32),
1235     Store->getChain()
1236   };
1237   ReplaceNode(Store, CurDAG->getMachineNode(Opcode, DL, MVT::Other, Ops));
1238   return true;
1239 }
1240 
1241 // Check whether or not the chain ending in StoreNode is suitable for doing
1242 // the {load; op; store} to modify transformation.
isFusableLoadOpStorePattern(StoreSDNode * StoreNode,SDValue StoredVal,SelectionDAG * CurDAG,LoadSDNode * & LoadNode,SDValue & InputChain)1243 static bool isFusableLoadOpStorePattern(StoreSDNode *StoreNode,
1244                                         SDValue StoredVal, SelectionDAG *CurDAG,
1245                                         LoadSDNode *&LoadNode,
1246                                         SDValue &InputChain) {
1247   // Is the stored value result 0 of the operation?
1248   if (StoredVal.getResNo() != 0)
1249     return false;
1250 
1251   // Are there other uses of the loaded value than the operation?
1252   if (!StoredVal.getNode()->hasNUsesOfValue(1, 0))
1253     return false;
1254 
1255   // Is the store non-extending and non-indexed?
1256   if (!ISD::isNormalStore(StoreNode) || StoreNode->isNonTemporal())
1257     return false;
1258 
1259   SDValue Load = StoredVal->getOperand(0);
1260   // Is the stored value a non-extending and non-indexed load?
1261   if (!ISD::isNormalLoad(Load.getNode()))
1262     return false;
1263 
1264   // Return LoadNode by reference.
1265   LoadNode = cast<LoadSDNode>(Load);
1266 
1267   // Is store the only read of the loaded value?
1268   if (!Load.hasOneUse())
1269     return false;
1270 
1271   // Is the address of the store the same as the load?
1272   if (LoadNode->getBasePtr() != StoreNode->getBasePtr() ||
1273       LoadNode->getOffset() != StoreNode->getOffset())
1274     return false;
1275 
1276   // Check if the chain is produced by the load or is a TokenFactor with
1277   // the load output chain as an operand. Return InputChain by reference.
1278   SDValue Chain = StoreNode->getChain();
1279 
1280   bool ChainCheck = false;
1281   if (Chain == Load.getValue(1)) {
1282     ChainCheck = true;
1283     InputChain = LoadNode->getChain();
1284   } else if (Chain.getOpcode() == ISD::TokenFactor) {
1285     SmallVector<SDValue, 4> ChainOps;
1286     SmallVector<const SDNode *, 4> LoopWorklist;
1287     SmallPtrSet<const SDNode *, 16> Visited;
1288     const unsigned int Max = 1024;
1289     for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i) {
1290       SDValue Op = Chain.getOperand(i);
1291       if (Op == Load.getValue(1)) {
1292         ChainCheck = true;
1293         // Drop Load, but keep its chain. No cycle check necessary.
1294         ChainOps.push_back(Load.getOperand(0));
1295         continue;
1296       }
1297       LoopWorklist.push_back(Op.getNode());
1298       ChainOps.push_back(Op);
1299     }
1300 
1301     if (ChainCheck) {
1302       // Add the other operand of StoredVal to worklist.
1303       for (SDValue Op : StoredVal->ops())
1304         if (Op.getNode() != LoadNode)
1305           LoopWorklist.push_back(Op.getNode());
1306 
1307       // Check if Load is reachable from any of the nodes in the worklist.
1308       if (SDNode::hasPredecessorHelper(Load.getNode(), Visited, LoopWorklist, Max,
1309                                        true))
1310         return false;
1311 
1312       // Make a new TokenFactor with all the other input chains except
1313       // for the load.
1314       InputChain = CurDAG->getNode(ISD::TokenFactor, SDLoc(Chain),
1315                                    MVT::Other, ChainOps);
1316     }
1317   }
1318   if (!ChainCheck)
1319     return false;
1320 
1321   return true;
1322 }
1323 
1324 // Change a chain of {load; op; store} of the same value into a simple op
1325 // through memory of that value, if the uses of the modified value and its
1326 // address are suitable.
1327 //
1328 // The tablegen pattern memory operand pattern is currently not able to match
1329 // the case where the CC on the original operation are used.
1330 //
1331 // See the equivalent routine in X86ISelDAGToDAG for further comments.
tryFoldLoadStoreIntoMemOperand(SDNode * Node)1332 bool SystemZDAGToDAGISel::tryFoldLoadStoreIntoMemOperand(SDNode *Node) {
1333   StoreSDNode *StoreNode = cast<StoreSDNode>(Node);
1334   SDValue StoredVal = StoreNode->getOperand(1);
1335   unsigned Opc = StoredVal->getOpcode();
1336   SDLoc DL(StoreNode);
1337 
1338   // Before we try to select anything, make sure this is memory operand size
1339   // and opcode we can handle. Note that this must match the code below that
1340   // actually lowers the opcodes.
1341   EVT MemVT = StoreNode->getMemoryVT();
1342   unsigned NewOpc = 0;
1343   bool NegateOperand = false;
1344   switch (Opc) {
1345   default:
1346     return false;
1347   case SystemZISD::SSUBO:
1348     NegateOperand = true;
1349     LLVM_FALLTHROUGH;
1350   case SystemZISD::SADDO:
1351     if (MemVT == MVT::i32)
1352       NewOpc = SystemZ::ASI;
1353     else if (MemVT == MVT::i64)
1354       NewOpc = SystemZ::AGSI;
1355     else
1356       return false;
1357     break;
1358   case SystemZISD::USUBO:
1359     NegateOperand = true;
1360     LLVM_FALLTHROUGH;
1361   case SystemZISD::UADDO:
1362     if (MemVT == MVT::i32)
1363       NewOpc = SystemZ::ALSI;
1364     else if (MemVT == MVT::i64)
1365       NewOpc = SystemZ::ALGSI;
1366     else
1367       return false;
1368     break;
1369   }
1370 
1371   LoadSDNode *LoadNode = nullptr;
1372   SDValue InputChain;
1373   if (!isFusableLoadOpStorePattern(StoreNode, StoredVal, CurDAG, LoadNode,
1374                                    InputChain))
1375     return false;
1376 
1377   SDValue Operand = StoredVal.getOperand(1);
1378   auto *OperandC = dyn_cast<ConstantSDNode>(Operand);
1379   if (!OperandC)
1380     return false;
1381   auto OperandV = OperandC->getAPIntValue();
1382   if (NegateOperand)
1383     OperandV = -OperandV;
1384   if (OperandV.getMinSignedBits() > 8)
1385     return false;
1386   Operand = CurDAG->getTargetConstant(OperandV, DL, MemVT);
1387 
1388   SDValue Base, Disp;
1389   if (!selectBDAddr20Only(StoreNode->getBasePtr(), Base, Disp))
1390     return false;
1391 
1392   SDValue Ops[] = { Base, Disp, Operand, InputChain };
1393   MachineSDNode *Result =
1394     CurDAG->getMachineNode(NewOpc, DL, MVT::i32, MVT::Other, Ops);
1395   CurDAG->setNodeMemRefs(
1396       Result, {StoreNode->getMemOperand(), LoadNode->getMemOperand()});
1397 
1398   ReplaceUses(SDValue(StoreNode, 0), SDValue(Result, 1));
1399   ReplaceUses(SDValue(StoredVal.getNode(), 1), SDValue(Result, 0));
1400   CurDAG->RemoveDeadNode(Node);
1401   return true;
1402 }
1403 
canUseBlockOperation(StoreSDNode * Store,LoadSDNode * Load) const1404 bool SystemZDAGToDAGISel::canUseBlockOperation(StoreSDNode *Store,
1405                                                LoadSDNode *Load) const {
1406   // Check that the two memory operands have the same size.
1407   if (Load->getMemoryVT() != Store->getMemoryVT())
1408     return false;
1409 
1410   // Volatility stops an access from being decomposed.
1411   if (Load->isVolatile() || Store->isVolatile())
1412     return false;
1413 
1414   // There's no chance of overlap if the load is invariant.
1415   if (Load->isInvariant() && Load->isDereferenceable())
1416     return true;
1417 
1418   // Otherwise we need to check whether there's an alias.
1419   const Value *V1 = Load->getMemOperand()->getValue();
1420   const Value *V2 = Store->getMemOperand()->getValue();
1421   if (!V1 || !V2)
1422     return false;
1423 
1424   // Reject equality.
1425   uint64_t Size = Load->getMemoryVT().getStoreSize();
1426   int64_t End1 = Load->getSrcValueOffset() + Size;
1427   int64_t End2 = Store->getSrcValueOffset() + Size;
1428   if (V1 == V2 && End1 == End2)
1429     return false;
1430 
1431   return !AA->alias(MemoryLocation(V1, End1, Load->getAAInfo()),
1432                     MemoryLocation(V2, End2, Store->getAAInfo()));
1433 }
1434 
storeLoadCanUseMVC(SDNode * N) const1435 bool SystemZDAGToDAGISel::storeLoadCanUseMVC(SDNode *N) const {
1436   auto *Store = cast<StoreSDNode>(N);
1437   auto *Load = cast<LoadSDNode>(Store->getValue());
1438 
1439   // Prefer not to use MVC if either address can use ... RELATIVE LONG
1440   // instructions.
1441   uint64_t Size = Load->getMemoryVT().getStoreSize();
1442   if (Size > 1 && Size <= 8) {
1443     // Prefer LHRL, LRL and LGRL.
1444     if (SystemZISD::isPCREL(Load->getBasePtr().getOpcode()))
1445       return false;
1446     // Prefer STHRL, STRL and STGRL.
1447     if (SystemZISD::isPCREL(Store->getBasePtr().getOpcode()))
1448       return false;
1449   }
1450 
1451   return canUseBlockOperation(Store, Load);
1452 }
1453 
storeLoadCanUseBlockBinary(SDNode * N,unsigned I) const1454 bool SystemZDAGToDAGISel::storeLoadCanUseBlockBinary(SDNode *N,
1455                                                      unsigned I) const {
1456   auto *StoreA = cast<StoreSDNode>(N);
1457   auto *LoadA = cast<LoadSDNode>(StoreA->getValue().getOperand(1 - I));
1458   auto *LoadB = cast<LoadSDNode>(StoreA->getValue().getOperand(I));
1459   return !LoadA->isVolatile() && canUseBlockOperation(StoreA, LoadB);
1460 }
1461 
Select(SDNode * Node)1462 void SystemZDAGToDAGISel::Select(SDNode *Node) {
1463   // If we have a custom node, we already have selected!
1464   if (Node->isMachineOpcode()) {
1465     LLVM_DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
1466     Node->setNodeId(-1);
1467     return;
1468   }
1469 
1470   unsigned Opcode = Node->getOpcode();
1471   switch (Opcode) {
1472   case ISD::OR:
1473     if (Node->getOperand(1).getOpcode() != ISD::Constant)
1474       if (tryRxSBG(Node, SystemZ::ROSBG))
1475         return;
1476     goto or_xor;
1477 
1478   case ISD::XOR:
1479     if (Node->getOperand(1).getOpcode() != ISD::Constant)
1480       if (tryRxSBG(Node, SystemZ::RXSBG))
1481         return;
1482     // Fall through.
1483   or_xor:
1484     // If this is a 64-bit operation in which both 32-bit halves are nonzero,
1485     // split the operation into two.  If both operands here happen to be
1486     // constant, leave this to common code to optimize.
1487     if (Node->getValueType(0) == MVT::i64 &&
1488         Node->getOperand(0).getOpcode() != ISD::Constant)
1489       if (auto *Op1 = dyn_cast<ConstantSDNode>(Node->getOperand(1))) {
1490         uint64_t Val = Op1->getZExtValue();
1491         // Don't split the operation if we can match one of the combined
1492         // logical operations provided by miscellaneous-extensions-3.
1493         if (Subtarget->hasMiscellaneousExtensions3()) {
1494           unsigned ChildOpcode = Node->getOperand(0).getOpcode();
1495           // Check whether this expression matches NAND/NOR/NXOR.
1496           if (Val == (uint64_t)-1 && Opcode == ISD::XOR)
1497             if (ChildOpcode == ISD::AND || ChildOpcode == ISD::OR ||
1498                 ChildOpcode == ISD::XOR)
1499               break;
1500           // Check whether this expression matches OR-with-complement
1501           // (or matches an alternate pattern for NXOR).
1502           if (ChildOpcode == ISD::XOR) {
1503             auto Op0 = Node->getOperand(0);
1504             if (auto *Op0Op1 = dyn_cast<ConstantSDNode>(Op0->getOperand(1)))
1505               if (Op0Op1->getZExtValue() == (uint64_t)-1)
1506                 break;
1507           }
1508         }
1509         if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val)) {
1510           splitLargeImmediate(Opcode, Node, Node->getOperand(0),
1511                               Val - uint32_t(Val), uint32_t(Val));
1512           return;
1513         }
1514       }
1515     break;
1516 
1517   case ISD::AND:
1518     if (Node->getOperand(1).getOpcode() != ISD::Constant)
1519       if (tryRxSBG(Node, SystemZ::RNSBG))
1520         return;
1521     LLVM_FALLTHROUGH;
1522   case ISD::ROTL:
1523   case ISD::SHL:
1524   case ISD::SRL:
1525   case ISD::ZERO_EXTEND:
1526     if (tryRISBGZero(Node))
1527       return;
1528     break;
1529 
1530   case ISD::Constant:
1531     // If this is a 64-bit constant that is out of the range of LLILF,
1532     // LLIHF and LGFI, split it into two 32-bit pieces.
1533     if (Node->getValueType(0) == MVT::i64) {
1534       uint64_t Val = cast<ConstantSDNode>(Node)->getZExtValue();
1535       if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val) && !isInt<32>(Val)) {
1536         splitLargeImmediate(ISD::OR, Node, SDValue(), Val - uint32_t(Val),
1537                             uint32_t(Val));
1538         return;
1539       }
1540     }
1541     break;
1542 
1543   case SystemZISD::SELECT_CCMASK: {
1544     SDValue Op0 = Node->getOperand(0);
1545     SDValue Op1 = Node->getOperand(1);
1546     // Prefer to put any load first, so that it can be matched as a
1547     // conditional load.  Likewise for constants in range for LOCHI.
1548     if ((Op1.getOpcode() == ISD::LOAD && Op0.getOpcode() != ISD::LOAD) ||
1549         (Subtarget->hasLoadStoreOnCond2() &&
1550          Node->getValueType(0).isInteger() &&
1551          Op1.getOpcode() == ISD::Constant &&
1552          isInt<16>(cast<ConstantSDNode>(Op1)->getSExtValue()) &&
1553          !(Op0.getOpcode() == ISD::Constant &&
1554            isInt<16>(cast<ConstantSDNode>(Op0)->getSExtValue())))) {
1555       SDValue CCValid = Node->getOperand(2);
1556       SDValue CCMask = Node->getOperand(3);
1557       uint64_t ConstCCValid =
1558         cast<ConstantSDNode>(CCValid.getNode())->getZExtValue();
1559       uint64_t ConstCCMask =
1560         cast<ConstantSDNode>(CCMask.getNode())->getZExtValue();
1561       // Invert the condition.
1562       CCMask = CurDAG->getTargetConstant(ConstCCValid ^ ConstCCMask,
1563                                          SDLoc(Node), CCMask.getValueType());
1564       SDValue Op4 = Node->getOperand(4);
1565       SDNode *UpdatedNode =
1566         CurDAG->UpdateNodeOperands(Node, Op1, Op0, CCValid, CCMask, Op4);
1567       if (UpdatedNode != Node) {
1568         // In case this node already exists then replace Node with it.
1569         ReplaceNode(Node, UpdatedNode);
1570         Node = UpdatedNode;
1571       }
1572     }
1573     break;
1574   }
1575 
1576   case ISD::INSERT_VECTOR_ELT: {
1577     EVT VT = Node->getValueType(0);
1578     unsigned ElemBitSize = VT.getScalarSizeInBits();
1579     if (ElemBitSize == 32) {
1580       if (tryGather(Node, SystemZ::VGEF))
1581         return;
1582     } else if (ElemBitSize == 64) {
1583       if (tryGather(Node, SystemZ::VGEG))
1584         return;
1585     }
1586     break;
1587   }
1588 
1589   case ISD::BUILD_VECTOR: {
1590     auto *BVN = cast<BuildVectorSDNode>(Node);
1591     SystemZVectorConstantInfo VCI(BVN);
1592     if (VCI.isVectorConstantLegal(*Subtarget)) {
1593       loadVectorConstant(VCI, Node);
1594       return;
1595     }
1596     break;
1597   }
1598 
1599   case ISD::ConstantFP: {
1600     APFloat Imm = cast<ConstantFPSDNode>(Node)->getValueAPF();
1601     if (Imm.isZero() || Imm.isNegZero())
1602       break;
1603     SystemZVectorConstantInfo VCI(Imm);
1604     bool Success = VCI.isVectorConstantLegal(*Subtarget); (void)Success;
1605     assert(Success && "Expected legal FP immediate");
1606     loadVectorConstant(VCI, Node);
1607     return;
1608   }
1609 
1610   case ISD::STORE: {
1611     if (tryFoldLoadStoreIntoMemOperand(Node))
1612       return;
1613     auto *Store = cast<StoreSDNode>(Node);
1614     unsigned ElemBitSize = Store->getValue().getValueSizeInBits();
1615     if (ElemBitSize == 32) {
1616       if (tryScatter(Store, SystemZ::VSCEF))
1617         return;
1618     } else if (ElemBitSize == 64) {
1619       if (tryScatter(Store, SystemZ::VSCEG))
1620         return;
1621     }
1622     break;
1623   }
1624   }
1625 
1626   SelectCode(Node);
1627 }
1628 
1629 bool SystemZDAGToDAGISel::
SelectInlineAsmMemoryOperand(const SDValue & Op,unsigned ConstraintID,std::vector<SDValue> & OutOps)1630 SelectInlineAsmMemoryOperand(const SDValue &Op,
1631                              unsigned ConstraintID,
1632                              std::vector<SDValue> &OutOps) {
1633   SystemZAddressingMode::AddrForm Form;
1634   SystemZAddressingMode::DispRange DispRange;
1635   SDValue Base, Disp, Index;
1636 
1637   switch(ConstraintID) {
1638   default:
1639     llvm_unreachable("Unexpected asm memory constraint");
1640   case InlineAsm::Constraint_i:
1641   case InlineAsm::Constraint_Q:
1642     // Accept an address with a short displacement, but no index.
1643     Form = SystemZAddressingMode::FormBD;
1644     DispRange = SystemZAddressingMode::Disp12Only;
1645     break;
1646   case InlineAsm::Constraint_R:
1647     // Accept an address with a short displacement and an index.
1648     Form = SystemZAddressingMode::FormBDXNormal;
1649     DispRange = SystemZAddressingMode::Disp12Only;
1650     break;
1651   case InlineAsm::Constraint_S:
1652     // Accept an address with a long displacement, but no index.
1653     Form = SystemZAddressingMode::FormBD;
1654     DispRange = SystemZAddressingMode::Disp20Only;
1655     break;
1656   case InlineAsm::Constraint_T:
1657   case InlineAsm::Constraint_m:
1658   case InlineAsm::Constraint_o:
1659     // Accept an address with a long displacement and an index.
1660     // m works the same as T, as this is the most general case.
1661     // We don't really have any special handling of "offsettable"
1662     // memory addresses, so just treat o the same as m.
1663     Form = SystemZAddressingMode::FormBDXNormal;
1664     DispRange = SystemZAddressingMode::Disp20Only;
1665     break;
1666   }
1667 
1668   if (selectBDXAddr(Form, DispRange, Op, Base, Disp, Index)) {
1669     const TargetRegisterClass *TRC =
1670       Subtarget->getRegisterInfo()->getPointerRegClass(*MF);
1671     SDLoc DL(Base);
1672     SDValue RC = CurDAG->getTargetConstant(TRC->getID(), DL, MVT::i32);
1673 
1674     // Make sure that the base address doesn't go into %r0.
1675     // If it's a TargetFrameIndex or a fixed register, we shouldn't do anything.
1676     if (Base.getOpcode() != ISD::TargetFrameIndex &&
1677         Base.getOpcode() != ISD::Register) {
1678       Base =
1679         SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
1680                                        DL, Base.getValueType(),
1681                                        Base, RC), 0);
1682     }
1683 
1684     // Make sure that the index register isn't assigned to %r0 either.
1685     if (Index.getOpcode() != ISD::Register) {
1686       Index =
1687         SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS,
1688                                        DL, Index.getValueType(),
1689                                        Index, RC), 0);
1690     }
1691 
1692     OutOps.push_back(Base);
1693     OutOps.push_back(Disp);
1694     OutOps.push_back(Index);
1695     return false;
1696   }
1697 
1698   return true;
1699 }
1700 
1701 // IsProfitableToFold - Returns true if is profitable to fold the specific
1702 // operand node N of U during instruction selection that starts at Root.
1703 bool
IsProfitableToFold(SDValue N,SDNode * U,SDNode * Root) const1704 SystemZDAGToDAGISel::IsProfitableToFold(SDValue N, SDNode *U,
1705                                         SDNode *Root) const {
1706   // We want to avoid folding a LOAD into an ICMP node if as a result
1707   // we would be forced to spill the condition code into a GPR.
1708   if (N.getOpcode() == ISD::LOAD && U->getOpcode() == SystemZISD::ICMP) {
1709     if (!N.hasOneUse() || !U->hasOneUse())
1710       return false;
1711 
1712     // The user of the CC value will usually be a CopyToReg into the
1713     // physical CC register, which in turn is glued and chained to the
1714     // actual instruction that uses the CC value.  Bail out if we have
1715     // anything else than that.
1716     SDNode *CCUser = *U->use_begin();
1717     SDNode *CCRegUser = nullptr;
1718     if (CCUser->getOpcode() == ISD::CopyToReg ||
1719         cast<RegisterSDNode>(CCUser->getOperand(1))->getReg() == SystemZ::CC) {
1720       for (auto *U : CCUser->uses()) {
1721         if (CCRegUser == nullptr)
1722           CCRegUser = U;
1723         else if (CCRegUser != U)
1724           return false;
1725       }
1726     }
1727     if (CCRegUser == nullptr)
1728       return false;
1729 
1730     // If the actual instruction is a branch, the only thing that remains to be
1731     // checked is whether the CCUser chain is a predecessor of the load.
1732     if (CCRegUser->isMachineOpcode() &&
1733         CCRegUser->getMachineOpcode() == SystemZ::BRC)
1734       return !N->isPredecessorOf(CCUser->getOperand(0).getNode());
1735 
1736     // Otherwise, the instruction may have multiple operands, and we need to
1737     // verify that none of them are a predecessor of the load.  This is exactly
1738     // the same check that would be done by common code if the CC setter were
1739     // glued to the CC user, so simply invoke that check here.
1740     if (!IsLegalToFold(N, U, CCRegUser, OptLevel, false))
1741       return false;
1742   }
1743 
1744   return true;
1745 }
1746 
1747 namespace {
1748 // Represents a sequence for extracting a 0/1 value from an IPM result:
1749 // (((X ^ XORValue) + AddValue) >> Bit)
1750 struct IPMConversion {
IPMConversion__anon9d233d250211::IPMConversion1751   IPMConversion(unsigned xorValue, int64_t addValue, unsigned bit)
1752     : XORValue(xorValue), AddValue(addValue), Bit(bit) {}
1753 
1754   int64_t XORValue;
1755   int64_t AddValue;
1756   unsigned Bit;
1757 };
1758 } // end anonymous namespace
1759 
1760 // Return a sequence for getting a 1 from an IPM result when CC has a
1761 // value in CCMask and a 0 when CC has a value in CCValid & ~CCMask.
1762 // The handling of CC values outside CCValid doesn't matter.
getIPMConversion(unsigned CCValid,unsigned CCMask)1763 static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) {
1764   // Deal with cases where the result can be taken directly from a bit
1765   // of the IPM result.
1766   if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3)))
1767     return IPMConversion(0, 0, SystemZ::IPM_CC);
1768   if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3)))
1769     return IPMConversion(0, 0, SystemZ::IPM_CC + 1);
1770 
1771   // Deal with cases where we can add a value to force the sign bit
1772   // to contain the right value.  Putting the bit in 31 means we can
1773   // use SRL rather than RISBG(L), and also makes it easier to get a
1774   // 0/-1 value, so it has priority over the other tests below.
1775   //
1776   // These sequences rely on the fact that the upper two bits of the
1777   // IPM result are zero.
1778   uint64_t TopBit = uint64_t(1) << 31;
1779   if (CCMask == (CCValid & SystemZ::CCMASK_0))
1780     return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31);
1781   if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1)))
1782     return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31);
1783   if (CCMask == (CCValid & (SystemZ::CCMASK_0
1784                             | SystemZ::CCMASK_1
1785                             | SystemZ::CCMASK_2)))
1786     return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31);
1787   if (CCMask == (CCValid & SystemZ::CCMASK_3))
1788     return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31);
1789   if (CCMask == (CCValid & (SystemZ::CCMASK_1
1790                             | SystemZ::CCMASK_2
1791                             | SystemZ::CCMASK_3)))
1792     return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31);
1793 
1794   // Next try inverting the value and testing a bit.  0/1 could be
1795   // handled this way too, but we dealt with that case above.
1796   if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2)))
1797     return IPMConversion(-1, 0, SystemZ::IPM_CC);
1798 
1799   // Handle cases where adding a value forces a non-sign bit to contain
1800   // the right value.
1801   if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2)))
1802     return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1);
1803   if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3)))
1804     return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1);
1805 
1806   // The remaining cases are 1, 2, 0/1/3 and 0/2/3.  All these are
1807   // can be done by inverting the low CC bit and applying one of the
1808   // sign-based extractions above.
1809   if (CCMask == (CCValid & SystemZ::CCMASK_1))
1810     return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31);
1811   if (CCMask == (CCValid & SystemZ::CCMASK_2))
1812     return IPMConversion(1 << SystemZ::IPM_CC,
1813                          TopBit - (3 << SystemZ::IPM_CC), 31);
1814   if (CCMask == (CCValid & (SystemZ::CCMASK_0
1815                             | SystemZ::CCMASK_1
1816                             | SystemZ::CCMASK_3)))
1817     return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31);
1818   if (CCMask == (CCValid & (SystemZ::CCMASK_0
1819                             | SystemZ::CCMASK_2
1820                             | SystemZ::CCMASK_3)))
1821     return IPMConversion(1 << SystemZ::IPM_CC,
1822                          TopBit - (1 << SystemZ::IPM_CC), 31);
1823 
1824   llvm_unreachable("Unexpected CC combination");
1825 }
1826 
expandSelectBoolean(SDNode * Node)1827 SDValue SystemZDAGToDAGISel::expandSelectBoolean(SDNode *Node) {
1828   auto *TrueOp = dyn_cast<ConstantSDNode>(Node->getOperand(0));
1829   auto *FalseOp = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1830   if (!TrueOp || !FalseOp)
1831     return SDValue();
1832   if (FalseOp->getZExtValue() != 0)
1833     return SDValue();
1834   if (TrueOp->getSExtValue() != 1 && TrueOp->getSExtValue() != -1)
1835     return SDValue();
1836 
1837   auto *CCValidOp = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1838   auto *CCMaskOp = dyn_cast<ConstantSDNode>(Node->getOperand(3));
1839   if (!CCValidOp || !CCMaskOp)
1840     return SDValue();
1841   int CCValid = CCValidOp->getZExtValue();
1842   int CCMask = CCMaskOp->getZExtValue();
1843 
1844   SDLoc DL(Node);
1845   SDValue CCReg = Node->getOperand(4);
1846   IPMConversion IPM = getIPMConversion(CCValid, CCMask);
1847   SDValue Result = CurDAG->getNode(SystemZISD::IPM, DL, MVT::i32, CCReg);
1848 
1849   if (IPM.XORValue)
1850     Result = CurDAG->getNode(ISD::XOR, DL, MVT::i32, Result,
1851                              CurDAG->getConstant(IPM.XORValue, DL, MVT::i32));
1852 
1853   if (IPM.AddValue)
1854     Result = CurDAG->getNode(ISD::ADD, DL, MVT::i32, Result,
1855                              CurDAG->getConstant(IPM.AddValue, DL, MVT::i32));
1856 
1857   EVT VT = Node->getValueType(0);
1858   if (VT == MVT::i32 && IPM.Bit == 31) {
1859     unsigned ShiftOp = TrueOp->getSExtValue() == 1 ? ISD::SRL : ISD::SRA;
1860     Result = CurDAG->getNode(ShiftOp, DL, MVT::i32, Result,
1861                              CurDAG->getConstant(IPM.Bit, DL, MVT::i32));
1862   } else {
1863     if (VT != MVT::i32)
1864       Result = CurDAG->getNode(ISD::ANY_EXTEND, DL, VT, Result);
1865 
1866     if (TrueOp->getSExtValue() == 1) {
1867       // The SHR/AND sequence should get optimized to an RISBG.
1868       Result = CurDAG->getNode(ISD::SRL, DL, VT, Result,
1869                                CurDAG->getConstant(IPM.Bit, DL, MVT::i32));
1870       Result = CurDAG->getNode(ISD::AND, DL, VT, Result,
1871                                CurDAG->getConstant(1, DL, VT));
1872     } else {
1873       // Sign-extend from IPM.Bit using a pair of shifts.
1874       int ShlAmt = VT.getSizeInBits() - 1 - IPM.Bit;
1875       int SraAmt = VT.getSizeInBits() - 1;
1876       Result = CurDAG->getNode(ISD::SHL, DL, VT, Result,
1877                                CurDAG->getConstant(ShlAmt, DL, MVT::i32));
1878       Result = CurDAG->getNode(ISD::SRA, DL, VT, Result,
1879                                CurDAG->getConstant(SraAmt, DL, MVT::i32));
1880     }
1881   }
1882 
1883   return Result;
1884 }
1885 
PreprocessISelDAG()1886 void SystemZDAGToDAGISel::PreprocessISelDAG() {
1887   // If we have conditional immediate loads, we always prefer
1888   // using those over an IPM sequence.
1889   if (Subtarget->hasLoadStoreOnCond2())
1890     return;
1891 
1892   bool MadeChange = false;
1893 
1894   for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
1895                                        E = CurDAG->allnodes_end();
1896        I != E;) {
1897     SDNode *N = &*I++;
1898     if (N->use_empty())
1899       continue;
1900 
1901     SDValue Res;
1902     switch (N->getOpcode()) {
1903     default: break;
1904     case SystemZISD::SELECT_CCMASK:
1905       Res = expandSelectBoolean(N);
1906       break;
1907     }
1908 
1909     if (Res) {
1910       LLVM_DEBUG(dbgs() << "SystemZ DAG preprocessing replacing:\nOld:    ");
1911       LLVM_DEBUG(N->dump(CurDAG));
1912       LLVM_DEBUG(dbgs() << "\nNew: ");
1913       LLVM_DEBUG(Res.getNode()->dump(CurDAG));
1914       LLVM_DEBUG(dbgs() << "\n");
1915 
1916       CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Res);
1917       MadeChange = true;
1918     }
1919   }
1920 
1921   if (MadeChange)
1922     CurDAG->RemoveDeadNodes();
1923 }
1924