1 //===-- SystemZISelDAGToDAG.cpp - A dag to dag inst selector for SystemZ --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines an instruction selector for the SystemZ target.
12 //===----------------------------------------------------------------------===//
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"
22 #define DEBUG_TYPE "systemz-isel"
25 // Used to build addressing modes.
26 struct SystemZAddressingMode {
27 // The shape of the address.
32 // base+displacement+index for load and store operands
35 // base+displacement+index for load address operands
38 // base+displacement+index+ADJDYNALLOC
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.
55 // The parts of the address. The address is equivalent to:
57 // Base + Disp + Index + (IncludesDynAlloc ? ADJDYNALLOC : 0)
61 bool IncludesDynAlloc;
63 SystemZAddressingMode(AddrForm form, DispRange dr)
64 : Form(form), DR(dr), Base(), Disp(0), Index(),
65 IncludesDynAlloc(false) {}
67 // True if the address can have an index register.
68 bool hasIndexField() { return Form != FormBD; }
70 // True if the address can (and must) include ADJDYNALLOC.
71 bool isDynAlloc() { return Form == FormBDXDynAlloc; }
74 errs() << "SystemZAddressingMode " << this << '\n';
78 Base.getNode()->dump();
82 if (hasIndexField()) {
85 Index.getNode()->dump();
90 errs() << " Disp " << Disp;
92 errs() << " + ADJDYNALLOC";
97 // Return a mask with Count low bits set.
98 static uint64_t allOnes(unsigned int Count) {
99 return Count == 0 ? 0 : (uint64_t(1) << (Count - 1) << 1) - 1;
102 // Represents operands 2 to 5 of the ROTATE AND ... SELECTED BITS operation
103 // given by Opcode. The operands are: Input (R2), Start (I3), End (I4) and
104 // Rotate (I5). The combined operand value is effectively:
106 // (or (rotl Input, Rotate), ~Mask)
110 // (and (rotl Input, Rotate), Mask)
112 // otherwise. The output value has BitSize bits, although Input may be
113 // narrower (in which case the upper bits are don't care).
114 struct RxSBGOperands {
115 RxSBGOperands(unsigned Op, SDValue N)
116 : Opcode(Op), BitSize(N.getValueType().getSizeInBits()),
117 Mask(allOnes(BitSize)), Input(N), Start(64 - BitSize), End(63),
129 class SystemZDAGToDAGISel : public SelectionDAGISel {
130 const SystemZSubtarget *Subtarget;
132 // Used by SystemZOperands.td to create integer constants.
133 inline SDValue getImm(const SDNode *Node, uint64_t Imm) const {
134 return CurDAG->getTargetConstant(Imm, Node->getValueType(0));
137 const SystemZTargetMachine &getTargetMachine() const {
138 return static_cast<const SystemZTargetMachine &>(TM);
141 const SystemZInstrInfo *getInstrInfo() const {
142 return Subtarget->getInstrInfo();
145 // Try to fold more of the base or index of AM into AM, where IsBase
146 // selects between the base and index.
147 bool expandAddress(SystemZAddressingMode &AM, bool IsBase) const;
149 // Try to describe N in AM, returning true on success.
150 bool selectAddress(SDValue N, SystemZAddressingMode &AM) const;
152 // Extract individual target operands from matched address AM.
153 void getAddressOperands(const SystemZAddressingMode &AM, EVT VT,
154 SDValue &Base, SDValue &Disp) const;
155 void getAddressOperands(const SystemZAddressingMode &AM, EVT VT,
156 SDValue &Base, SDValue &Disp, SDValue &Index) const;
158 // Try to match Addr as a FormBD address with displacement type DR.
159 // Return true on success, storing the base and displacement in
160 // Base and Disp respectively.
161 bool selectBDAddr(SystemZAddressingMode::DispRange DR, SDValue Addr,
162 SDValue &Base, SDValue &Disp) const;
164 // Try to match Addr as a FormBDX address with displacement type DR.
165 // Return true on success and if the result had no index. Store the
166 // base and displacement in Base and Disp respectively.
167 bool selectMVIAddr(SystemZAddressingMode::DispRange DR, SDValue Addr,
168 SDValue &Base, SDValue &Disp) const;
170 // Try to match Addr as a FormBDX* address of form Form with
171 // displacement type DR. Return true on success, storing the base,
172 // displacement and index in Base, Disp and Index respectively.
173 bool selectBDXAddr(SystemZAddressingMode::AddrForm Form,
174 SystemZAddressingMode::DispRange DR, SDValue Addr,
175 SDValue &Base, SDValue &Disp, SDValue &Index) const;
177 // PC-relative address matching routines used by SystemZOperands.td.
178 bool selectPCRelAddress(SDValue Addr, SDValue &Target) const {
179 if (SystemZISD::isPCREL(Addr.getOpcode())) {
180 Target = Addr.getOperand(0);
186 // BD matching routines used by SystemZOperands.td.
187 bool selectBDAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp) const {
188 return selectBDAddr(SystemZAddressingMode::Disp12Only, Addr, Base, Disp);
190 bool selectBDAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
191 return selectBDAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp);
193 bool selectBDAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp) const {
194 return selectBDAddr(SystemZAddressingMode::Disp20Only, Addr, Base, Disp);
196 bool selectBDAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
197 return selectBDAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp);
200 // MVI matching routines used by SystemZOperands.td.
201 bool selectMVIAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
202 return selectMVIAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp);
204 bool selectMVIAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const {
205 return selectMVIAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp);
208 // BDX matching routines used by SystemZOperands.td.
209 bool selectBDXAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp,
210 SDValue &Index) const {
211 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
212 SystemZAddressingMode::Disp12Only,
213 Addr, Base, Disp, Index);
215 bool selectBDXAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
216 SDValue &Index) const {
217 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
218 SystemZAddressingMode::Disp12Pair,
219 Addr, Base, Disp, Index);
221 bool selectDynAlloc12Only(SDValue Addr, SDValue &Base, SDValue &Disp,
222 SDValue &Index) const {
223 return selectBDXAddr(SystemZAddressingMode::FormBDXDynAlloc,
224 SystemZAddressingMode::Disp12Only,
225 Addr, Base, Disp, Index);
227 bool selectBDXAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp,
228 SDValue &Index) const {
229 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
230 SystemZAddressingMode::Disp20Only,
231 Addr, Base, Disp, Index);
233 bool selectBDXAddr20Only128(SDValue Addr, SDValue &Base, SDValue &Disp,
234 SDValue &Index) const {
235 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
236 SystemZAddressingMode::Disp20Only128,
237 Addr, Base, Disp, Index);
239 bool selectBDXAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
240 SDValue &Index) const {
241 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
242 SystemZAddressingMode::Disp20Pair,
243 Addr, Base, Disp, Index);
245 bool selectLAAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
246 SDValue &Index) const {
247 return selectBDXAddr(SystemZAddressingMode::FormBDXLA,
248 SystemZAddressingMode::Disp12Pair,
249 Addr, Base, Disp, Index);
251 bool selectLAAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
252 SDValue &Index) const {
253 return selectBDXAddr(SystemZAddressingMode::FormBDXLA,
254 SystemZAddressingMode::Disp20Pair,
255 Addr, Base, Disp, Index);
258 // Check whether (or Op (and X InsertMask)) is effectively an insertion
259 // of X into bits InsertMask of some Y != Op. Return true if so and
261 bool detectOrAndInsertion(SDValue &Op, uint64_t InsertMask) const;
263 // Try to update RxSBG so that only the bits of RxSBG.Input in Mask are used.
264 // Return true on success.
265 bool refineRxSBGMask(RxSBGOperands &RxSBG, uint64_t Mask) const;
267 // Try to fold some of RxSBG.Input into other fields of RxSBG.
268 // Return true on success.
269 bool expandRxSBG(RxSBGOperands &RxSBG) const;
271 // Return an undefined value of type VT.
272 SDValue getUNDEF(SDLoc DL, EVT VT) const;
274 // Convert N to VT, if it isn't already.
275 SDValue convertTo(SDLoc DL, EVT VT, SDValue N) const;
277 // Try to implement AND or shift node N using RISBG with the zero flag set.
278 // Return the selected node on success, otherwise return null.
279 SDNode *tryRISBGZero(SDNode *N);
281 // Try to use RISBG or Opcode to implement OR or XOR node N.
282 // Return the selected node on success, otherwise return null.
283 SDNode *tryRxSBG(SDNode *N, unsigned Opcode);
285 // If Op0 is null, then Node is a constant that can be loaded using:
287 // (Opcode UpperVal LowerVal)
289 // If Op0 is nonnull, then Node can be implemented using:
291 // (Opcode (Opcode Op0 UpperVal) LowerVal)
292 SDNode *splitLargeImmediate(unsigned Opcode, SDNode *Node, SDValue Op0,
293 uint64_t UpperVal, uint64_t LowerVal);
295 // Return true if Load and Store are loads and stores of the same size
296 // and are guaranteed not to overlap. Such operations can be implemented
297 // using block (SS-format) instructions.
299 // Partial overlap would lead to incorrect code, since the block operations
300 // are logically bytewise, even though they have a fast path for the
301 // non-overlapping case. We also need to avoid full overlap (i.e. two
302 // addresses that might be equal at run time) because although that case
303 // would be handled correctly, it might be implemented by millicode.
304 bool canUseBlockOperation(StoreSDNode *Store, LoadSDNode *Load) const;
306 // N is a (store (load Y), X) pattern. Return true if it can use an MVC
308 bool storeLoadCanUseMVC(SDNode *N) const;
310 // N is a (store (op (load A[0]), (load A[1])), X) pattern. Return true
311 // if A[1 - I] == X and if N can use a block operation like NC from A[I]
313 bool storeLoadCanUseBlockBinary(SDNode *N, unsigned I) const;
316 SystemZDAGToDAGISel(SystemZTargetMachine &TM, CodeGenOpt::Level OptLevel)
317 : SelectionDAGISel(TM, OptLevel) {}
319 bool runOnMachineFunction(MachineFunction &MF) override {
320 Subtarget = &MF.getSubtarget<SystemZSubtarget>();
321 return SelectionDAGISel::runOnMachineFunction(MF);
324 // Override MachineFunctionPass.
325 const char *getPassName() const override {
326 return "SystemZ DAG->DAG Pattern Instruction Selection";
329 // Override SelectionDAGISel.
330 SDNode *Select(SDNode *Node) override;
331 bool SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
332 std::vector<SDValue> &OutOps) override;
334 // Include the pieces autogenerated from the target description.
335 #include "SystemZGenDAGISel.inc"
337 } // end anonymous namespace
339 FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM,
340 CodeGenOpt::Level OptLevel) {
341 return new SystemZDAGToDAGISel(TM, OptLevel);
344 // Return true if Val should be selected as a displacement for an address
345 // with range DR. Here we're interested in the range of both the instruction
346 // described by DR and of any pairing instruction.
347 static bool selectDisp(SystemZAddressingMode::DispRange DR, int64_t Val) {
349 case SystemZAddressingMode::Disp12Only:
350 return isUInt<12>(Val);
352 case SystemZAddressingMode::Disp12Pair:
353 case SystemZAddressingMode::Disp20Only:
354 case SystemZAddressingMode::Disp20Pair:
355 return isInt<20>(Val);
357 case SystemZAddressingMode::Disp20Only128:
358 return isInt<20>(Val) && isInt<20>(Val + 8);
360 llvm_unreachable("Unhandled displacement range");
363 // Change the base or index in AM to Value, where IsBase selects
364 // between the base and index.
365 static void changeComponent(SystemZAddressingMode &AM, bool IsBase,
373 // The base or index of AM is equivalent to Value + ADJDYNALLOC,
374 // where IsBase selects between the base and index. Try to fold the
375 // ADJDYNALLOC into AM.
376 static bool expandAdjDynAlloc(SystemZAddressingMode &AM, bool IsBase,
378 if (AM.isDynAlloc() && !AM.IncludesDynAlloc) {
379 changeComponent(AM, IsBase, Value);
380 AM.IncludesDynAlloc = true;
386 // The base of AM is equivalent to Base + Index. Try to use Index as
387 // the index register.
388 static bool expandIndex(SystemZAddressingMode &AM, SDValue Base,
390 if (AM.hasIndexField() && !AM.Index.getNode()) {
398 // The base or index of AM is equivalent to Op0 + Op1, where IsBase selects
399 // between the base and index. Try to fold Op1 into AM's displacement.
400 static bool expandDisp(SystemZAddressingMode &AM, bool IsBase,
401 SDValue Op0, uint64_t Op1) {
402 // First try adjusting the displacement.
403 int64_t TestDisp = AM.Disp + Op1;
404 if (selectDisp(AM.DR, TestDisp)) {
405 changeComponent(AM, IsBase, Op0);
410 // We could consider forcing the displacement into a register and
411 // using it as an index, but it would need to be carefully tuned.
415 bool SystemZDAGToDAGISel::expandAddress(SystemZAddressingMode &AM,
417 SDValue N = IsBase ? AM.Base : AM.Index;
418 unsigned Opcode = N.getOpcode();
419 if (Opcode == ISD::TRUNCATE) {
421 Opcode = N.getOpcode();
423 if (Opcode == ISD::ADD || CurDAG->isBaseWithConstantOffset(N)) {
424 SDValue Op0 = N.getOperand(0);
425 SDValue Op1 = N.getOperand(1);
427 unsigned Op0Code = Op0->getOpcode();
428 unsigned Op1Code = Op1->getOpcode();
430 if (Op0Code == SystemZISD::ADJDYNALLOC)
431 return expandAdjDynAlloc(AM, IsBase, Op1);
432 if (Op1Code == SystemZISD::ADJDYNALLOC)
433 return expandAdjDynAlloc(AM, IsBase, Op0);
435 if (Op0Code == ISD::Constant)
436 return expandDisp(AM, IsBase, Op1,
437 cast<ConstantSDNode>(Op0)->getSExtValue());
438 if (Op1Code == ISD::Constant)
439 return expandDisp(AM, IsBase, Op0,
440 cast<ConstantSDNode>(Op1)->getSExtValue());
442 if (IsBase && expandIndex(AM, Op0, Op1))
445 if (Opcode == SystemZISD::PCREL_OFFSET) {
446 SDValue Full = N.getOperand(0);
447 SDValue Base = N.getOperand(1);
448 SDValue Anchor = Base.getOperand(0);
449 uint64_t Offset = (cast<GlobalAddressSDNode>(Full)->getOffset() -
450 cast<GlobalAddressSDNode>(Anchor)->getOffset());
451 return expandDisp(AM, IsBase, Base, Offset);
456 // Return true if an instruction with displacement range DR should be
457 // used for displacement value Val. selectDisp(DR, Val) must already hold.
458 static bool isValidDisp(SystemZAddressingMode::DispRange DR, int64_t Val) {
459 assert(selectDisp(DR, Val) && "Invalid displacement");
461 case SystemZAddressingMode::Disp12Only:
462 case SystemZAddressingMode::Disp20Only:
463 case SystemZAddressingMode::Disp20Only128:
466 case SystemZAddressingMode::Disp12Pair:
467 // Use the other instruction if the displacement is too large.
468 return isUInt<12>(Val);
470 case SystemZAddressingMode::Disp20Pair:
471 // Use the other instruction if the displacement is small enough.
472 return !isUInt<12>(Val);
474 llvm_unreachable("Unhandled displacement range");
477 // Return true if Base + Disp + Index should be performed by LA(Y).
478 static bool shouldUseLA(SDNode *Base, int64_t Disp, SDNode *Index) {
479 // Don't use LA(Y) for constants.
483 // Always use LA(Y) for frame addresses, since we know that the destination
484 // register is almost always (perhaps always) going to be different from
485 // the frame register.
486 if (Base->getOpcode() == ISD::FrameIndex)
490 // Always use LA(Y) if there is a base, displacement and index.
494 // Always use LA if the displacement is small enough. It should always
495 // be no worse than AGHI (and better if it avoids a move).
496 if (isUInt<12>(Disp))
499 // For similar reasons, always use LAY if the constant is too big for AGHI.
500 // LAY should be no worse than AGFI.
501 if (!isInt<16>(Disp))
504 // Don't use LA for plain registers.
508 // Don't use LA for plain addition if the index operand is only used
509 // once. It should be a natural two-operand addition in that case.
510 if (Index->hasOneUse())
513 // Prefer addition if the second operation is sign-extended, in the
514 // hope of using AGF.
515 unsigned IndexOpcode = Index->getOpcode();
516 if (IndexOpcode == ISD::SIGN_EXTEND ||
517 IndexOpcode == ISD::SIGN_EXTEND_INREG)
521 // Don't use LA for two-operand addition if either operand is only
522 // used once. The addition instructions are better in that case.
523 if (Base->hasOneUse())
529 // Return true if Addr is suitable for AM, updating AM if so.
530 bool SystemZDAGToDAGISel::selectAddress(SDValue Addr,
531 SystemZAddressingMode &AM) const {
532 // Start out assuming that the address will need to be loaded separately,
533 // then try to extend it as much as we can.
536 // First try treating the address as a constant.
537 if (Addr.getOpcode() == ISD::Constant &&
538 expandDisp(AM, true, SDValue(),
539 cast<ConstantSDNode>(Addr)->getSExtValue()))
542 // Otherwise try expanding each component.
543 while (expandAddress(AM, true) ||
544 (AM.Index.getNode() && expandAddress(AM, false)))
547 // Reject cases where it isn't profitable to use LA(Y).
548 if (AM.Form == SystemZAddressingMode::FormBDXLA &&
549 !shouldUseLA(AM.Base.getNode(), AM.Disp, AM.Index.getNode()))
552 // Reject cases where the other instruction in a pair should be used.
553 if (!isValidDisp(AM.DR, AM.Disp))
556 // Make sure that ADJDYNALLOC is included where necessary.
557 if (AM.isDynAlloc() && !AM.IncludesDynAlloc)
564 // Insert a node into the DAG at least before Pos. This will reposition
565 // the node as needed, and will assign it a node ID that is <= Pos's ID.
566 // Note that this does *not* preserve the uniqueness of node IDs!
567 // The selection DAG must no longer depend on their uniqueness when this
569 static void insertDAGNode(SelectionDAG *DAG, SDNode *Pos, SDValue N) {
570 if (N.getNode()->getNodeId() == -1 ||
571 N.getNode()->getNodeId() > Pos->getNodeId()) {
572 DAG->RepositionNode(Pos, N.getNode());
573 N.getNode()->setNodeId(Pos->getNodeId());
577 void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
578 EVT VT, SDValue &Base,
579 SDValue &Disp) const {
582 // Register 0 means "no base". This is mostly useful for shifts.
583 Base = CurDAG->getRegister(0, VT);
584 else if (Base.getOpcode() == ISD::FrameIndex) {
585 // Lower a FrameIndex to a TargetFrameIndex.
586 int64_t FrameIndex = cast<FrameIndexSDNode>(Base)->getIndex();
587 Base = CurDAG->getTargetFrameIndex(FrameIndex, VT);
588 } else if (Base.getValueType() != VT) {
589 // Truncate values from i64 to i32, for shifts.
590 assert(VT == MVT::i32 && Base.getValueType() == MVT::i64 &&
591 "Unexpected truncation");
593 SDValue Trunc = CurDAG->getNode(ISD::TRUNCATE, DL, VT, Base);
594 insertDAGNode(CurDAG, Base.getNode(), Trunc);
598 // Lower the displacement to a TargetConstant.
599 Disp = CurDAG->getTargetConstant(AM.Disp, VT);
602 void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
603 EVT VT, SDValue &Base,
605 SDValue &Index) const {
606 getAddressOperands(AM, VT, Base, Disp);
609 if (!Index.getNode())
610 // Register 0 means "no index".
611 Index = CurDAG->getRegister(0, VT);
614 bool SystemZDAGToDAGISel::selectBDAddr(SystemZAddressingMode::DispRange DR,
615 SDValue Addr, SDValue &Base,
616 SDValue &Disp) const {
617 SystemZAddressingMode AM(SystemZAddressingMode::FormBD, DR);
618 if (!selectAddress(Addr, AM))
621 getAddressOperands(AM, Addr.getValueType(), Base, Disp);
625 bool SystemZDAGToDAGISel::selectMVIAddr(SystemZAddressingMode::DispRange DR,
626 SDValue Addr, SDValue &Base,
627 SDValue &Disp) const {
628 SystemZAddressingMode AM(SystemZAddressingMode::FormBDXNormal, DR);
629 if (!selectAddress(Addr, AM) || AM.Index.getNode())
632 getAddressOperands(AM, Addr.getValueType(), Base, Disp);
636 bool SystemZDAGToDAGISel::selectBDXAddr(SystemZAddressingMode::AddrForm Form,
637 SystemZAddressingMode::DispRange DR,
638 SDValue Addr, SDValue &Base,
639 SDValue &Disp, SDValue &Index) const {
640 SystemZAddressingMode AM(Form, DR);
641 if (!selectAddress(Addr, AM))
644 getAddressOperands(AM, Addr.getValueType(), Base, Disp, Index);
648 bool SystemZDAGToDAGISel::detectOrAndInsertion(SDValue &Op,
649 uint64_t InsertMask) const {
650 // We're only interested in cases where the insertion is into some operand
651 // of Op, rather than into Op itself. The only useful case is an AND.
652 if (Op.getOpcode() != ISD::AND)
655 // We need a constant mask.
656 auto *MaskNode = dyn_cast<ConstantSDNode>(Op.getOperand(1).getNode());
660 // It's not an insertion of Op.getOperand(0) if the two masks overlap.
661 uint64_t AndMask = MaskNode->getZExtValue();
662 if (InsertMask & AndMask)
665 // It's only an insertion if all bits are covered or are known to be zero.
666 // The inner check covers all cases but is more expensive.
667 uint64_t Used = allOnes(Op.getValueType().getSizeInBits());
668 if (Used != (AndMask | InsertMask)) {
669 APInt KnownZero, KnownOne;
670 CurDAG->computeKnownBits(Op.getOperand(0), KnownZero, KnownOne);
671 if (Used != (AndMask | InsertMask | KnownZero.getZExtValue()))
675 Op = Op.getOperand(0);
679 bool SystemZDAGToDAGISel::refineRxSBGMask(RxSBGOperands &RxSBG,
680 uint64_t Mask) const {
681 const SystemZInstrInfo *TII = getInstrInfo();
682 if (RxSBG.Rotate != 0)
683 Mask = (Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate));
685 if (TII->isRxSBGMask(Mask, RxSBG.BitSize, RxSBG.Start, RxSBG.End)) {
692 // Return true if any bits of (RxSBG.Input & Mask) are significant.
693 static bool maskMatters(RxSBGOperands &RxSBG, uint64_t Mask) {
694 // Rotate the mask in the same way as RxSBG.Input is rotated.
695 if (RxSBG.Rotate != 0)
696 Mask = ((Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate)));
697 return (Mask & RxSBG.Mask) != 0;
700 bool SystemZDAGToDAGISel::expandRxSBG(RxSBGOperands &RxSBG) const {
701 SDValue N = RxSBG.Input;
702 unsigned Opcode = N.getOpcode();
705 if (RxSBG.Opcode == SystemZ::RNSBG)
708 auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
712 SDValue Input = N.getOperand(0);
713 uint64_t Mask = MaskNode->getZExtValue();
714 if (!refineRxSBGMask(RxSBG, Mask)) {
715 // If some bits of Input are already known zeros, those bits will have
716 // been removed from the mask. See if adding them back in makes the
718 APInt KnownZero, KnownOne;
719 CurDAG->computeKnownBits(Input, KnownZero, KnownOne);
720 Mask |= KnownZero.getZExtValue();
721 if (!refineRxSBGMask(RxSBG, Mask))
729 if (RxSBG.Opcode != SystemZ::RNSBG)
732 auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
736 SDValue Input = N.getOperand(0);
737 uint64_t Mask = ~MaskNode->getZExtValue();
738 if (!refineRxSBGMask(RxSBG, Mask)) {
739 // If some bits of Input are already known ones, those bits will have
740 // been removed from the mask. See if adding them back in makes the
742 APInt KnownZero, KnownOne;
743 CurDAG->computeKnownBits(Input, KnownZero, KnownOne);
744 Mask &= ~KnownOne.getZExtValue();
745 if (!refineRxSBGMask(RxSBG, Mask))
753 // Any 64-bit rotate left can be merged into the RxSBG.
754 if (RxSBG.BitSize != 64 || N.getValueType() != MVT::i64)
756 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
760 RxSBG.Rotate = (RxSBG.Rotate + CountNode->getZExtValue()) & 63;
761 RxSBG.Input = N.getOperand(0);
765 case ISD::ANY_EXTEND:
766 // Bits above the extended operand are don't-care.
767 RxSBG.Input = N.getOperand(0);
770 case ISD::ZERO_EXTEND:
771 if (RxSBG.Opcode != SystemZ::RNSBG) {
772 // Restrict the mask to the extended operand.
773 unsigned InnerBitSize = N.getOperand(0).getValueType().getSizeInBits();
774 if (!refineRxSBGMask(RxSBG, allOnes(InnerBitSize)))
777 RxSBG.Input = N.getOperand(0);
782 case ISD::SIGN_EXTEND: {
783 // Check that the extension bits are don't-care (i.e. are masked out
784 // by the final mask).
785 unsigned InnerBitSize = N.getOperand(0).getValueType().getSizeInBits();
786 if (maskMatters(RxSBG, allOnes(RxSBG.BitSize) - allOnes(InnerBitSize)))
789 RxSBG.Input = N.getOperand(0);
794 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
798 uint64_t Count = CountNode->getZExtValue();
799 unsigned BitSize = N.getValueType().getSizeInBits();
800 if (Count < 1 || Count >= BitSize)
803 if (RxSBG.Opcode == SystemZ::RNSBG) {
804 // Treat (shl X, count) as (rotl X, size-count) as long as the bottom
805 // count bits from RxSBG.Input are ignored.
806 if (maskMatters(RxSBG, allOnes(Count)))
809 // Treat (shl X, count) as (and (rotl X, count), ~0<<count).
810 if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count) << Count))
814 RxSBG.Rotate = (RxSBG.Rotate + Count) & 63;
815 RxSBG.Input = N.getOperand(0);
821 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
825 uint64_t Count = CountNode->getZExtValue();
826 unsigned BitSize = N.getValueType().getSizeInBits();
827 if (Count < 1 || Count >= BitSize)
830 if (RxSBG.Opcode == SystemZ::RNSBG || Opcode == ISD::SRA) {
831 // Treat (srl|sra X, count) as (rotl X, size-count) as long as the top
832 // count bits from RxSBG.Input are ignored.
833 if (maskMatters(RxSBG, allOnes(Count) << (BitSize - Count)))
836 // Treat (srl X, count), mask) as (and (rotl X, size-count), ~0>>count),
837 // which is similar to SLL above.
838 if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count)))
842 RxSBG.Rotate = (RxSBG.Rotate - Count) & 63;
843 RxSBG.Input = N.getOperand(0);
851 SDValue SystemZDAGToDAGISel::getUNDEF(SDLoc DL, EVT VT) const {
852 SDNode *N = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, VT);
853 return SDValue(N, 0);
856 SDValue SystemZDAGToDAGISel::convertTo(SDLoc DL, EVT VT, SDValue N) const {
857 if (N.getValueType() == MVT::i32 && VT == MVT::i64)
858 return CurDAG->getTargetInsertSubreg(SystemZ::subreg_l32,
859 DL, VT, getUNDEF(DL, MVT::i64), N);
860 if (N.getValueType() == MVT::i64 && VT == MVT::i32)
861 return CurDAG->getTargetExtractSubreg(SystemZ::subreg_l32, DL, VT, N);
862 assert(N.getValueType() == VT && "Unexpected value types");
866 SDNode *SystemZDAGToDAGISel::tryRISBGZero(SDNode *N) {
867 EVT VT = N->getValueType(0);
868 RxSBGOperands RISBG(SystemZ::RISBG, SDValue(N, 0));
870 while (expandRxSBG(RISBG))
871 if (RISBG.Input.getOpcode() != ISD::ANY_EXTEND)
876 // Prefer to use normal shift instructions over RISBG, since they can handle
877 // all cases and are sometimes shorter.
878 if (N->getOpcode() != ISD::AND)
881 // Prefer register extensions like LLC over RISBG. Also prefer to start
882 // out with normal ANDs if one instruction would be enough. We can convert
883 // these ANDs into an RISBG later if a three-address instruction is useful.
884 if (VT == MVT::i32 ||
885 RISBG.Mask == 0xff ||
886 RISBG.Mask == 0xffff ||
887 SystemZ::isImmLF(~RISBG.Mask) ||
888 SystemZ::isImmHF(~RISBG.Mask)) {
889 // Force the new mask into the DAG, since it may include known-one bits.
890 auto *MaskN = cast<ConstantSDNode>(N->getOperand(1).getNode());
891 if (MaskN->getZExtValue() != RISBG.Mask) {
892 SDValue NewMask = CurDAG->getConstant(RISBG.Mask, VT);
893 N = CurDAG->UpdateNodeOperands(N, N->getOperand(0), NewMask);
894 return SelectCode(N);
900 unsigned Opcode = SystemZ::RISBG;
901 // Prefer RISBGN if available, since it does not clobber CC.
902 if (Subtarget->hasMiscellaneousExtensions())
903 Opcode = SystemZ::RISBGN;
904 EVT OpcodeVT = MVT::i64;
905 if (VT == MVT::i32 && Subtarget->hasHighWord()) {
906 Opcode = SystemZ::RISBMux;
912 getUNDEF(SDLoc(N), OpcodeVT),
913 convertTo(SDLoc(N), OpcodeVT, RISBG.Input),
914 CurDAG->getTargetConstant(RISBG.Start, MVT::i32),
915 CurDAG->getTargetConstant(RISBG.End | 128, MVT::i32),
916 CurDAG->getTargetConstant(RISBG.Rotate, MVT::i32)
918 N = CurDAG->getMachineNode(Opcode, SDLoc(N), OpcodeVT, Ops);
919 return convertTo(SDLoc(N), VT, SDValue(N, 0)).getNode();
922 SDNode *SystemZDAGToDAGISel::tryRxSBG(SDNode *N, unsigned Opcode) {
923 // Try treating each operand of N as the second operand of the RxSBG
924 // and see which goes deepest.
925 RxSBGOperands RxSBG[] = {
926 RxSBGOperands(Opcode, N->getOperand(0)),
927 RxSBGOperands(Opcode, N->getOperand(1))
929 unsigned Count[] = { 0, 0 };
930 for (unsigned I = 0; I < 2; ++I)
931 while (expandRxSBG(RxSBG[I]))
932 if (RxSBG[I].Input.getOpcode() != ISD::ANY_EXTEND)
935 // Do nothing if neither operand is suitable.
936 if (Count[0] == 0 && Count[1] == 0)
939 // Pick the deepest second operand.
940 unsigned I = Count[0] > Count[1] ? 0 : 1;
941 SDValue Op0 = N->getOperand(I ^ 1);
943 // Prefer IC for character insertions from memory.
944 if (Opcode == SystemZ::ROSBG && (RxSBG[I].Mask & 0xff) == 0)
945 if (auto *Load = dyn_cast<LoadSDNode>(Op0.getNode()))
946 if (Load->getMemoryVT() == MVT::i8)
949 // See whether we can avoid an AND in the first operand by converting
951 if (Opcode == SystemZ::ROSBG && detectOrAndInsertion(Op0, RxSBG[I].Mask)) {
952 Opcode = SystemZ::RISBG;
953 // Prefer RISBGN if available, since it does not clobber CC.
954 if (Subtarget->hasMiscellaneousExtensions())
955 Opcode = SystemZ::RISBGN;
958 EVT VT = N->getValueType(0);
960 convertTo(SDLoc(N), MVT::i64, Op0),
961 convertTo(SDLoc(N), MVT::i64, RxSBG[I].Input),
962 CurDAG->getTargetConstant(RxSBG[I].Start, MVT::i32),
963 CurDAG->getTargetConstant(RxSBG[I].End, MVT::i32),
964 CurDAG->getTargetConstant(RxSBG[I].Rotate, MVT::i32)
966 N = CurDAG->getMachineNode(Opcode, SDLoc(N), MVT::i64, Ops);
967 return convertTo(SDLoc(N), VT, SDValue(N, 0)).getNode();
970 SDNode *SystemZDAGToDAGISel::splitLargeImmediate(unsigned Opcode, SDNode *Node,
971 SDValue Op0, uint64_t UpperVal,
973 EVT VT = Node->getValueType(0);
975 SDValue Upper = CurDAG->getConstant(UpperVal, VT);
977 Upper = CurDAG->getNode(Opcode, DL, VT, Op0, Upper);
978 Upper = SDValue(Select(Upper.getNode()), 0);
980 SDValue Lower = CurDAG->getConstant(LowerVal, VT);
981 SDValue Or = CurDAG->getNode(Opcode, DL, VT, Upper, Lower);
985 bool SystemZDAGToDAGISel::canUseBlockOperation(StoreSDNode *Store,
986 LoadSDNode *Load) const {
987 // Check that the two memory operands have the same size.
988 if (Load->getMemoryVT() != Store->getMemoryVT())
991 // Volatility stops an access from being decomposed.
992 if (Load->isVolatile() || Store->isVolatile())
995 // There's no chance of overlap if the load is invariant.
996 if (Load->isInvariant())
999 // Otherwise we need to check whether there's an alias.
1000 const Value *V1 = Load->getMemOperand()->getValue();
1001 const Value *V2 = Store->getMemOperand()->getValue();
1006 uint64_t Size = Load->getMemoryVT().getStoreSize();
1007 int64_t End1 = Load->getSrcValueOffset() + Size;
1008 int64_t End2 = Store->getSrcValueOffset() + Size;
1009 if (V1 == V2 && End1 == End2)
1012 return !AA->alias(AliasAnalysis::Location(V1, End1, Load->getAAInfo()),
1013 AliasAnalysis::Location(V2, End2, Store->getAAInfo()));
1016 bool SystemZDAGToDAGISel::storeLoadCanUseMVC(SDNode *N) const {
1017 auto *Store = cast<StoreSDNode>(N);
1018 auto *Load = cast<LoadSDNode>(Store->getValue());
1020 // Prefer not to use MVC if either address can use ... RELATIVE LONG
1022 uint64_t Size = Load->getMemoryVT().getStoreSize();
1023 if (Size > 1 && Size <= 8) {
1024 // Prefer LHRL, LRL and LGRL.
1025 if (SystemZISD::isPCREL(Load->getBasePtr().getOpcode()))
1027 // Prefer STHRL, STRL and STGRL.
1028 if (SystemZISD::isPCREL(Store->getBasePtr().getOpcode()))
1032 return canUseBlockOperation(Store, Load);
1035 bool SystemZDAGToDAGISel::storeLoadCanUseBlockBinary(SDNode *N,
1037 auto *StoreA = cast<StoreSDNode>(N);
1038 auto *LoadA = cast<LoadSDNode>(StoreA->getValue().getOperand(1 - I));
1039 auto *LoadB = cast<LoadSDNode>(StoreA->getValue().getOperand(I));
1040 return !LoadA->isVolatile() && canUseBlockOperation(StoreA, LoadB);
1043 SDNode *SystemZDAGToDAGISel::Select(SDNode *Node) {
1044 // Dump information about the Node being selected
1045 DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n");
1047 // If we have a custom node, we already have selected!
1048 if (Node->isMachineOpcode()) {
1049 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
1050 Node->setNodeId(-1);
1054 unsigned Opcode = Node->getOpcode();
1055 SDNode *ResNode = nullptr;
1058 if (Node->getOperand(1).getOpcode() != ISD::Constant)
1059 ResNode = tryRxSBG(Node, SystemZ::ROSBG);
1063 if (Node->getOperand(1).getOpcode() != ISD::Constant)
1064 ResNode = tryRxSBG(Node, SystemZ::RXSBG);
1067 // If this is a 64-bit operation in which both 32-bit halves are nonzero,
1068 // split the operation into two.
1069 if (!ResNode && Node->getValueType(0) == MVT::i64)
1070 if (auto *Op1 = dyn_cast<ConstantSDNode>(Node->getOperand(1))) {
1071 uint64_t Val = Op1->getZExtValue();
1072 if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val))
1073 Node = splitLargeImmediate(Opcode, Node, Node->getOperand(0),
1074 Val - uint32_t(Val), uint32_t(Val));
1079 if (Node->getOperand(1).getOpcode() != ISD::Constant)
1080 ResNode = tryRxSBG(Node, SystemZ::RNSBG);
1085 case ISD::ZERO_EXTEND:
1087 ResNode = tryRISBGZero(Node);
1091 // If this is a 64-bit constant that is out of the range of LLILF,
1092 // LLIHF and LGFI, split it into two 32-bit pieces.
1093 if (Node->getValueType(0) == MVT::i64) {
1094 uint64_t Val = cast<ConstantSDNode>(Node)->getZExtValue();
1095 if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val) && !isInt<32>(Val))
1096 Node = splitLargeImmediate(ISD::OR, Node, SDValue(),
1097 Val - uint32_t(Val), uint32_t(Val));
1101 case SystemZISD::SELECT_CCMASK: {
1102 SDValue Op0 = Node->getOperand(0);
1103 SDValue Op1 = Node->getOperand(1);
1104 // Prefer to put any load first, so that it can be matched as a
1105 // conditional load.
1106 if (Op1.getOpcode() == ISD::LOAD && Op0.getOpcode() != ISD::LOAD) {
1107 SDValue CCValid = Node->getOperand(2);
1108 SDValue CCMask = Node->getOperand(3);
1109 uint64_t ConstCCValid =
1110 cast<ConstantSDNode>(CCValid.getNode())->getZExtValue();
1111 uint64_t ConstCCMask =
1112 cast<ConstantSDNode>(CCMask.getNode())->getZExtValue();
1113 // Invert the condition.
1114 CCMask = CurDAG->getConstant(ConstCCValid ^ ConstCCMask,
1115 CCMask.getValueType());
1116 SDValue Op4 = Node->getOperand(4);
1117 Node = CurDAG->UpdateNodeOperands(Node, Op1, Op0, CCValid, CCMask, Op4);
1123 // Select the default instruction
1125 ResNode = SelectCode(Node);
1127 DEBUG(errs() << "=> ";
1128 if (ResNode == nullptr || ResNode == Node)
1131 ResNode->dump(CurDAG);
1137 bool SystemZDAGToDAGISel::
1138 SelectInlineAsmMemoryOperand(const SDValue &Op,
1139 unsigned ConstraintID,
1140 std::vector<SDValue> &OutOps) {
1141 switch(ConstraintID) {
1143 llvm_unreachable("Unexpected asm memory constraint");
1144 case InlineAsm::Constraint_i:
1145 case InlineAsm::Constraint_m:
1146 case InlineAsm::Constraint_Q:
1147 case InlineAsm::Constraint_R:
1148 case InlineAsm::Constraint_S:
1149 case InlineAsm::Constraint_T:
1150 // Accept addresses with short displacements, which are compatible
1151 // with Q, R, S and T. But keep the index operand for future expansion.
1152 SDValue Base, Disp, Index;
1153 if (selectBDXAddr(SystemZAddressingMode::FormBD,
1154 SystemZAddressingMode::Disp12Only,
1155 Op, Base, Disp, Index)) {
1156 OutOps.push_back(Base);
1157 OutOps.push_back(Disp);
1158 OutOps.push_back(Index);