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"
23 // Used to build addressing modes.
24 struct SystemZAddressingMode {
25 // The shape of the address.
30 // base+displacement+index for load and store operands
33 // base+displacement+index for load address operands
36 // base+displacement+index+ADJDYNALLOC
41 // The type of displacement. The enum names here correspond directly
42 // to the definitions in SystemZOperand.td. We could split them into
43 // flags -- single/pair, 128-bit, etc. -- but it hardly seems worth it.
53 // The parts of the address. The address is equivalent to:
55 // Base + Disp + Index + (IncludesDynAlloc ? ADJDYNALLOC : 0)
59 bool IncludesDynAlloc;
61 SystemZAddressingMode(AddrForm form, DispRange dr)
62 : Form(form), DR(dr), Base(), Disp(0), Index(),
63 IncludesDynAlloc(false) {}
65 // True if the address can have an index register.
66 bool hasIndexField() { return Form != FormBD; }
68 // True if the address can (and must) include ADJDYNALLOC.
69 bool isDynAlloc() { return Form == FormBDXDynAlloc; }
72 errs() << "SystemZAddressingMode " << this << '\n';
75 if (Base.getNode() != 0)
76 Base.getNode()->dump();
80 if (hasIndexField()) {
82 if (Index.getNode() != 0)
83 Index.getNode()->dump();
88 errs() << " Disp " << Disp;
90 errs() << " + ADJDYNALLOC";
95 // Return a mask with Count low bits set.
96 static uint64_t allOnes(unsigned int Count) {
97 return Count == 0 ? 0 : (uint64_t(1) << (Count - 1) << 1) - 1;
100 // Represents operands 2 to 5 of the ROTATE AND ... SELECTED BITS operation
101 // given by Opcode. The operands are: Input (R2), Start (I3), End (I4) and
102 // Rotate (I5). The combined operand value is effectively:
104 // (or (rotl Input, Rotate), ~Mask)
108 // (and (rotl Input, Rotate), Mask)
110 // otherwise. The value has BitSize bits.
111 struct RxSBGOperands {
112 RxSBGOperands(unsigned Op, SDValue N)
113 : Opcode(Op), BitSize(N.getValueType().getSizeInBits()),
114 Mask(allOnes(BitSize)), Input(N), Start(64 - BitSize), End(63),
126 class SystemZDAGToDAGISel : public SelectionDAGISel {
127 const SystemZTargetLowering &Lowering;
128 const SystemZSubtarget &Subtarget;
130 // Used by SystemZOperands.td to create integer constants.
131 inline SDValue getImm(const SDNode *Node, uint64_t Imm) {
132 return CurDAG->getTargetConstant(Imm, Node->getValueType(0));
135 // Try to fold more of the base or index of AM into AM, where IsBase
136 // selects between the base and index.
137 bool expandAddress(SystemZAddressingMode &AM, bool IsBase);
139 // Try to describe N in AM, returning true on success.
140 bool selectAddress(SDValue N, SystemZAddressingMode &AM);
142 // Extract individual target operands from matched address AM.
143 void getAddressOperands(const SystemZAddressingMode &AM, EVT VT,
144 SDValue &Base, SDValue &Disp);
145 void getAddressOperands(const SystemZAddressingMode &AM, EVT VT,
146 SDValue &Base, SDValue &Disp, SDValue &Index);
148 // Try to match Addr as a FormBD address with displacement type DR.
149 // Return true on success, storing the base and displacement in
150 // Base and Disp respectively.
151 bool selectBDAddr(SystemZAddressingMode::DispRange DR, SDValue Addr,
152 SDValue &Base, SDValue &Disp);
154 // Try to match Addr as a FormBDX* address of form Form with
155 // displacement type DR. Return true on success, storing the base,
156 // displacement and index in Base, Disp and Index respectively.
157 bool selectBDXAddr(SystemZAddressingMode::AddrForm Form,
158 SystemZAddressingMode::DispRange DR, SDValue Addr,
159 SDValue &Base, SDValue &Disp, SDValue &Index);
161 // PC-relative address matching routines used by SystemZOperands.td.
162 bool selectPCRelAddress(SDValue Addr, SDValue &Target) {
163 if (Addr.getOpcode() == SystemZISD::PCREL_WRAPPER) {
164 Target = Addr.getOperand(0);
170 // BD matching routines used by SystemZOperands.td.
171 bool selectBDAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp) {
172 return selectBDAddr(SystemZAddressingMode::Disp12Only, Addr, Base, Disp);
174 bool selectBDAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) {
175 return selectBDAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp);
177 bool selectBDAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp) {
178 return selectBDAddr(SystemZAddressingMode::Disp20Only, Addr, Base, Disp);
180 bool selectBDAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) {
181 return selectBDAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp);
184 // BDX matching routines used by SystemZOperands.td.
185 bool selectBDXAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp,
187 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
188 SystemZAddressingMode::Disp12Only,
189 Addr, Base, Disp, Index);
191 bool selectBDXAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
193 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
194 SystemZAddressingMode::Disp12Pair,
195 Addr, Base, Disp, Index);
197 bool selectDynAlloc12Only(SDValue Addr, SDValue &Base, SDValue &Disp,
199 return selectBDXAddr(SystemZAddressingMode::FormBDXDynAlloc,
200 SystemZAddressingMode::Disp12Only,
201 Addr, Base, Disp, Index);
203 bool selectBDXAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp,
205 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
206 SystemZAddressingMode::Disp20Only,
207 Addr, Base, Disp, Index);
209 bool selectBDXAddr20Only128(SDValue Addr, SDValue &Base, SDValue &Disp,
211 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
212 SystemZAddressingMode::Disp20Only128,
213 Addr, Base, Disp, Index);
215 bool selectBDXAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
217 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal,
218 SystemZAddressingMode::Disp20Pair,
219 Addr, Base, Disp, Index);
221 bool selectLAAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
223 return selectBDXAddr(SystemZAddressingMode::FormBDXLA,
224 SystemZAddressingMode::Disp12Pair,
225 Addr, Base, Disp, Index);
227 bool selectLAAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp,
229 return selectBDXAddr(SystemZAddressingMode::FormBDXLA,
230 SystemZAddressingMode::Disp20Pair,
231 Addr, Base, Disp, Index);
234 // Check whether (or Op (and X InsertMask)) is effectively an insertion
235 // of X into bits InsertMask of some Y != Op. Return true if so and
237 bool detectOrAndInsertion(SDValue &Op, uint64_t InsertMask);
239 // Try to fold some of RxSBG.Input into other fields of RxSBG.
240 // Return true on success.
241 bool expandRxSBG(RxSBGOperands &RxSBG);
243 // Return an undefined i64 value.
244 SDValue getUNDEF64(SDLoc DL);
246 // Convert N to VT, if it isn't already.
247 SDValue convertTo(SDLoc DL, EVT VT, SDValue N);
249 // Try to implement AND or shift node N using RISBG with the zero flag set.
250 // Return the selected node on success, otherwise return null.
251 SDNode *tryRISBGZero(SDNode *N);
253 // Try to use RISBG or Opcode to implement OR or XOR node N.
254 // Return the selected node on success, otherwise return null.
255 SDNode *tryRxSBG(SDNode *N, unsigned Opcode);
257 // If Op0 is null, then Node is a constant that can be loaded using:
259 // (Opcode UpperVal LowerVal)
261 // If Op0 is nonnull, then Node can be implemented using:
263 // (Opcode (Opcode Op0 UpperVal) LowerVal)
264 SDNode *splitLargeImmediate(unsigned Opcode, SDNode *Node, SDValue Op0,
265 uint64_t UpperVal, uint64_t LowerVal);
267 bool storeLoadCanUseMVC(SDNode *N) const;
270 SystemZDAGToDAGISel(SystemZTargetMachine &TM, CodeGenOpt::Level OptLevel)
271 : SelectionDAGISel(TM, OptLevel),
272 Lowering(*TM.getTargetLowering()),
273 Subtarget(*TM.getSubtargetImpl()) { }
275 // Override MachineFunctionPass.
276 virtual const char *getPassName() const LLVM_OVERRIDE {
277 return "SystemZ DAG->DAG Pattern Instruction Selection";
280 // Override SelectionDAGISel.
281 virtual SDNode *Select(SDNode *Node) LLVM_OVERRIDE;
282 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
284 std::vector<SDValue> &OutOps)
287 // Include the pieces autogenerated from the target description.
288 #include "SystemZGenDAGISel.inc"
290 } // end anonymous namespace
292 FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM,
293 CodeGenOpt::Level OptLevel) {
294 return new SystemZDAGToDAGISel(TM, OptLevel);
297 // Return true if Val should be selected as a displacement for an address
298 // with range DR. Here we're interested in the range of both the instruction
299 // described by DR and of any pairing instruction.
300 static bool selectDisp(SystemZAddressingMode::DispRange DR, int64_t Val) {
302 case SystemZAddressingMode::Disp12Only:
303 return isUInt<12>(Val);
305 case SystemZAddressingMode::Disp12Pair:
306 case SystemZAddressingMode::Disp20Only:
307 case SystemZAddressingMode::Disp20Pair:
308 return isInt<20>(Val);
310 case SystemZAddressingMode::Disp20Only128:
311 return isInt<20>(Val) && isInt<20>(Val + 8);
313 llvm_unreachable("Unhandled displacement range");
316 // Change the base or index in AM to Value, where IsBase selects
317 // between the base and index.
318 static void changeComponent(SystemZAddressingMode &AM, bool IsBase,
326 // The base or index of AM is equivalent to Value + ADJDYNALLOC,
327 // where IsBase selects between the base and index. Try to fold the
328 // ADJDYNALLOC into AM.
329 static bool expandAdjDynAlloc(SystemZAddressingMode &AM, bool IsBase,
331 if (AM.isDynAlloc() && !AM.IncludesDynAlloc) {
332 changeComponent(AM, IsBase, Value);
333 AM.IncludesDynAlloc = true;
339 // The base of AM is equivalent to Base + Index. Try to use Index as
340 // the index register.
341 static bool expandIndex(SystemZAddressingMode &AM, SDValue Base,
343 if (AM.hasIndexField() && !AM.Index.getNode()) {
351 // The base or index of AM is equivalent to Op0 + Op1, where IsBase selects
352 // between the base and index. Try to fold Op1 into AM's displacement.
353 static bool expandDisp(SystemZAddressingMode &AM, bool IsBase,
354 SDValue Op0, ConstantSDNode *Op1) {
355 // First try adjusting the displacement.
356 int64_t TestDisp = AM.Disp + Op1->getSExtValue();
357 if (selectDisp(AM.DR, TestDisp)) {
358 changeComponent(AM, IsBase, Op0);
363 // We could consider forcing the displacement into a register and
364 // using it as an index, but it would need to be carefully tuned.
368 bool SystemZDAGToDAGISel::expandAddress(SystemZAddressingMode &AM,
370 SDValue N = IsBase ? AM.Base : AM.Index;
371 unsigned Opcode = N.getOpcode();
372 if (Opcode == ISD::TRUNCATE) {
374 Opcode = N.getOpcode();
376 if (Opcode == ISD::ADD || CurDAG->isBaseWithConstantOffset(N)) {
377 SDValue Op0 = N.getOperand(0);
378 SDValue Op1 = N.getOperand(1);
380 unsigned Op0Code = Op0->getOpcode();
381 unsigned Op1Code = Op1->getOpcode();
383 if (Op0Code == SystemZISD::ADJDYNALLOC)
384 return expandAdjDynAlloc(AM, IsBase, Op1);
385 if (Op1Code == SystemZISD::ADJDYNALLOC)
386 return expandAdjDynAlloc(AM, IsBase, Op0);
388 if (Op0Code == ISD::Constant)
389 return expandDisp(AM, IsBase, Op1, cast<ConstantSDNode>(Op0));
390 if (Op1Code == ISD::Constant)
391 return expandDisp(AM, IsBase, Op0, cast<ConstantSDNode>(Op1));
393 if (IsBase && expandIndex(AM, Op0, Op1))
399 // Return true if an instruction with displacement range DR should be
400 // used for displacement value Val. selectDisp(DR, Val) must already hold.
401 static bool isValidDisp(SystemZAddressingMode::DispRange DR, int64_t Val) {
402 assert(selectDisp(DR, Val) && "Invalid displacement");
404 case SystemZAddressingMode::Disp12Only:
405 case SystemZAddressingMode::Disp20Only:
406 case SystemZAddressingMode::Disp20Only128:
409 case SystemZAddressingMode::Disp12Pair:
410 // Use the other instruction if the displacement is too large.
411 return isUInt<12>(Val);
413 case SystemZAddressingMode::Disp20Pair:
414 // Use the other instruction if the displacement is small enough.
415 return !isUInt<12>(Val);
417 llvm_unreachable("Unhandled displacement range");
420 // Return true if Base + Disp + Index should be performed by LA(Y).
421 static bool shouldUseLA(SDNode *Base, int64_t Disp, SDNode *Index) {
422 // Don't use LA(Y) for constants.
426 // Always use LA(Y) for frame addresses, since we know that the destination
427 // register is almost always (perhaps always) going to be different from
428 // the frame register.
429 if (Base->getOpcode() == ISD::FrameIndex)
433 // Always use LA(Y) if there is a base, displacement and index.
437 // Always use LA if the displacement is small enough. It should always
438 // be no worse than AGHI (and better if it avoids a move).
439 if (isUInt<12>(Disp))
442 // For similar reasons, always use LAY if the constant is too big for AGHI.
443 // LAY should be no worse than AGFI.
444 if (!isInt<16>(Disp))
447 // Don't use LA for plain registers.
451 // Don't use LA for plain addition if the index operand is only used
452 // once. It should be a natural two-operand addition in that case.
453 if (Index->hasOneUse())
456 // Prefer addition if the second operation is sign-extended, in the
457 // hope of using AGF.
458 unsigned IndexOpcode = Index->getOpcode();
459 if (IndexOpcode == ISD::SIGN_EXTEND ||
460 IndexOpcode == ISD::SIGN_EXTEND_INREG)
464 // Don't use LA for two-operand addition if either operand is only
465 // used once. The addition instructions are better in that case.
466 if (Base->hasOneUse())
472 // Return true if Addr is suitable for AM, updating AM if so.
473 bool SystemZDAGToDAGISel::selectAddress(SDValue Addr,
474 SystemZAddressingMode &AM) {
475 // Start out assuming that the address will need to be loaded separately,
476 // then try to extend it as much as we can.
479 // First try treating the address as a constant.
480 if (Addr.getOpcode() == ISD::Constant &&
481 expandDisp(AM, true, SDValue(), cast<ConstantSDNode>(Addr)))
484 // Otherwise try expanding each component.
485 while (expandAddress(AM, true) ||
486 (AM.Index.getNode() && expandAddress(AM, false)))
489 // Reject cases where it isn't profitable to use LA(Y).
490 if (AM.Form == SystemZAddressingMode::FormBDXLA &&
491 !shouldUseLA(AM.Base.getNode(), AM.Disp, AM.Index.getNode()))
494 // Reject cases where the other instruction in a pair should be used.
495 if (!isValidDisp(AM.DR, AM.Disp))
498 // Make sure that ADJDYNALLOC is included where necessary.
499 if (AM.isDynAlloc() && !AM.IncludesDynAlloc)
506 // Insert a node into the DAG at least before Pos. This will reposition
507 // the node as needed, and will assign it a node ID that is <= Pos's ID.
508 // Note that this does *not* preserve the uniqueness of node IDs!
509 // The selection DAG must no longer depend on their uniqueness when this
511 static void insertDAGNode(SelectionDAG *DAG, SDNode *Pos, SDValue N) {
512 if (N.getNode()->getNodeId() == -1 ||
513 N.getNode()->getNodeId() > Pos->getNodeId()) {
514 DAG->RepositionNode(Pos, N.getNode());
515 N.getNode()->setNodeId(Pos->getNodeId());
519 void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
520 EVT VT, SDValue &Base,
524 // Register 0 means "no base". This is mostly useful for shifts.
525 Base = CurDAG->getRegister(0, VT);
526 else if (Base.getOpcode() == ISD::FrameIndex) {
527 // Lower a FrameIndex to a TargetFrameIndex.
528 int64_t FrameIndex = cast<FrameIndexSDNode>(Base)->getIndex();
529 Base = CurDAG->getTargetFrameIndex(FrameIndex, VT);
530 } else if (Base.getValueType() != VT) {
531 // Truncate values from i64 to i32, for shifts.
532 assert(VT == MVT::i32 && Base.getValueType() == MVT::i64 &&
533 "Unexpected truncation");
535 SDValue Trunc = CurDAG->getNode(ISD::TRUNCATE, DL, VT, Base);
536 insertDAGNode(CurDAG, Base.getNode(), Trunc);
540 // Lower the displacement to a TargetConstant.
541 Disp = CurDAG->getTargetConstant(AM.Disp, VT);
544 void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM,
545 EVT VT, SDValue &Base,
546 SDValue &Disp, SDValue &Index) {
547 getAddressOperands(AM, VT, Base, Disp);
550 if (!Index.getNode())
551 // Register 0 means "no index".
552 Index = CurDAG->getRegister(0, VT);
555 bool SystemZDAGToDAGISel::selectBDAddr(SystemZAddressingMode::DispRange DR,
556 SDValue Addr, SDValue &Base,
558 SystemZAddressingMode AM(SystemZAddressingMode::FormBD, DR);
559 if (!selectAddress(Addr, AM))
562 getAddressOperands(AM, Addr.getValueType(), Base, Disp);
566 bool SystemZDAGToDAGISel::selectBDXAddr(SystemZAddressingMode::AddrForm Form,
567 SystemZAddressingMode::DispRange DR,
568 SDValue Addr, SDValue &Base,
569 SDValue &Disp, SDValue &Index) {
570 SystemZAddressingMode AM(Form, DR);
571 if (!selectAddress(Addr, AM))
574 getAddressOperands(AM, Addr.getValueType(), Base, Disp, Index);
578 bool SystemZDAGToDAGISel::detectOrAndInsertion(SDValue &Op,
579 uint64_t InsertMask) {
580 // We're only interested in cases where the insertion is into some operand
581 // of Op, rather than into Op itself. The only useful case is an AND.
582 if (Op.getOpcode() != ISD::AND)
585 // We need a constant mask.
586 ConstantSDNode *MaskNode =
587 dyn_cast<ConstantSDNode>(Op.getOperand(1).getNode());
591 // It's not an insertion of Op.getOperand(0) if the two masks overlap.
592 uint64_t AndMask = MaskNode->getZExtValue();
593 if (InsertMask & AndMask)
596 // It's only an insertion if all bits are covered or are known to be zero.
597 // The inner check covers all cases but is more expensive.
598 uint64_t Used = allOnes(Op.getValueType().getSizeInBits());
599 if (Used != (AndMask | InsertMask)) {
600 APInt KnownZero, KnownOne;
601 CurDAG->ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne);
602 if (Used != (AndMask | InsertMask | KnownZero.getZExtValue()))
606 Op = Op.getOperand(0);
610 // Return true if Mask matches the regexp 0*1+0*, given that zero masks
611 // have already been filtered out. Store the first set bit in LSB and
612 // the number of set bits in Length if so.
613 static bool isStringOfOnes(uint64_t Mask, unsigned &LSB, unsigned &Length) {
614 unsigned First = findFirstSet(Mask);
615 uint64_t Top = (Mask >> First) + 1;
616 if ((Top & -Top) == Top) {
618 Length = findFirstSet(Top);
624 // Try to update RxSBG so that only the bits of RxSBG.Input in Mask are used.
625 // Return true on success.
626 static bool refineRxSBGMask(RxSBGOperands &RxSBG, uint64_t Mask) {
627 if (RxSBG.Rotate != 0)
628 Mask = (Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate));
631 // Reject trivial all-zero masks.
635 // Handle the 1+0+ or 0+1+0* cases. Start then specifies the index of
636 // the msb and End specifies the index of the lsb.
637 unsigned LSB, Length;
638 if (isStringOfOnes(Mask, LSB, Length)) {
640 RxSBG.Start = 63 - (LSB + Length - 1);
641 RxSBG.End = 63 - LSB;
645 // Handle the wrap-around 1+0+1+ cases. Start then specifies the msb
646 // of the low 1s and End specifies the lsb of the high 1s.
647 if (isStringOfOnes(Mask ^ allOnes(RxSBG.BitSize), LSB, Length)) {
648 assert(LSB > 0 && "Bottom bit must be set");
649 assert(LSB + Length < RxSBG.BitSize && "Top bit must be set");
651 RxSBG.Start = 63 - (LSB - 1);
652 RxSBG.End = 63 - (LSB + Length);
659 // RxSBG.Input is a shift of Count bits in the direction given by IsLeft.
660 // Return true if the result depends on the signs or zeros that are
662 static bool shiftedInBitsMatter(RxSBGOperands &RxSBG, uint64_t Count,
664 // Work out which bits of the shift result are zeros or sign copies.
665 uint64_t ShiftedIn = allOnes(Count);
667 ShiftedIn <<= RxSBG.BitSize - Count;
669 // Rotate that mask in the same way as RxSBG.Input is rotated.
670 if (RxSBG.Rotate != 0)
671 ShiftedIn = ((ShiftedIn << RxSBG.Rotate) |
672 (ShiftedIn >> (64 - RxSBG.Rotate)));
674 // Fail if any of the zero or sign bits are used.
675 return (ShiftedIn & RxSBG.Mask) != 0;
678 bool SystemZDAGToDAGISel::expandRxSBG(RxSBGOperands &RxSBG) {
679 SDValue N = RxSBG.Input;
680 unsigned Opcode = N.getOpcode();
683 if (RxSBG.Opcode == SystemZ::RNSBG)
686 ConstantSDNode *MaskNode =
687 dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
691 SDValue Input = N.getOperand(0);
692 uint64_t Mask = MaskNode->getZExtValue();
693 if (!refineRxSBGMask(RxSBG, Mask)) {
694 // If some bits of Input are already known zeros, those bits will have
695 // been removed from the mask. See if adding them back in makes the
697 APInt KnownZero, KnownOne;
698 CurDAG->ComputeMaskedBits(Input, KnownZero, KnownOne);
699 Mask |= KnownZero.getZExtValue();
700 if (!refineRxSBGMask(RxSBG, Mask))
708 if (RxSBG.Opcode != SystemZ::RNSBG)
711 ConstantSDNode *MaskNode =
712 dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
716 SDValue Input = N.getOperand(0);
717 uint64_t Mask = ~MaskNode->getZExtValue();
718 if (!refineRxSBGMask(RxSBG, Mask)) {
719 // If some bits of Input are already known ones, those bits will have
720 // been removed from the mask. See if adding them back in makes the
722 APInt KnownZero, KnownOne;
723 CurDAG->ComputeMaskedBits(Input, KnownZero, KnownOne);
724 Mask &= ~KnownOne.getZExtValue();
725 if (!refineRxSBGMask(RxSBG, Mask))
733 // Any 64-bit rotate left can be merged into the RxSBG.
734 if (RxSBG.BitSize != 64)
736 ConstantSDNode *CountNode
737 = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
741 RxSBG.Rotate = (RxSBG.Rotate + CountNode->getZExtValue()) & 63;
742 RxSBG.Input = N.getOperand(0);
747 ConstantSDNode *CountNode =
748 dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
752 uint64_t Count = CountNode->getZExtValue();
753 if (Count < 1 || Count >= RxSBG.BitSize)
756 if (RxSBG.Opcode == SystemZ::RNSBG) {
757 // Treat (shl X, count) as (rotl X, size-count) as long as the bottom
758 // count bits from RxSBG.Input are ignored.
759 if (shiftedInBitsMatter(RxSBG, Count, true))
762 // Treat (shl X, count) as (and (rotl X, count), ~0<<count).
763 if (!refineRxSBGMask(RxSBG, allOnes(RxSBG.BitSize - Count) << Count))
767 RxSBG.Rotate = (RxSBG.Rotate + Count) & 63;
768 RxSBG.Input = N.getOperand(0);
774 ConstantSDNode *CountNode =
775 dyn_cast<ConstantSDNode>(N.getOperand(1).getNode());
779 uint64_t Count = CountNode->getZExtValue();
780 if (Count < 1 || Count >= RxSBG.BitSize)
783 if (RxSBG.Opcode == SystemZ::RNSBG || Opcode == ISD::SRA) {
784 // Treat (srl|sra X, count) as (rotl X, size-count) as long as the top
785 // count bits from RxSBG.Input are ignored.
786 if (shiftedInBitsMatter(RxSBG, Count, false))
789 // Treat (srl X, count), mask) as (and (rotl X, size-count), ~0>>count),
790 // which is similar to SLL above.
791 if (!refineRxSBGMask(RxSBG, allOnes(RxSBG.BitSize - Count)))
795 RxSBG.Rotate = (RxSBG.Rotate - Count) & 63;
796 RxSBG.Input = N.getOperand(0);
804 SDValue SystemZDAGToDAGISel::getUNDEF64(SDLoc DL) {
805 SDNode *N = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::i64);
806 return SDValue(N, 0);
809 SDValue SystemZDAGToDAGISel::convertTo(SDLoc DL, EVT VT, SDValue N) {
810 if (N.getValueType() == MVT::i32 && VT == MVT::i64) {
811 SDValue Index = CurDAG->getTargetConstant(SystemZ::subreg_32bit, MVT::i64);
812 SDNode *Insert = CurDAG->getMachineNode(TargetOpcode::INSERT_SUBREG,
813 DL, VT, getUNDEF64(DL), N, Index);
814 return SDValue(Insert, 0);
816 if (N.getValueType() == MVT::i64 && VT == MVT::i32) {
817 SDValue Index = CurDAG->getTargetConstant(SystemZ::subreg_32bit, MVT::i64);
818 SDNode *Extract = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
820 return SDValue(Extract, 0);
822 assert(N.getValueType() == VT && "Unexpected value types");
826 SDNode *SystemZDAGToDAGISel::tryRISBGZero(SDNode *N) {
827 RxSBGOperands RISBG(SystemZ::RISBG, SDValue(N, 0));
829 while (expandRxSBG(RISBG))
831 // Prefer to use normal shift instructions over RISBG, since they can handle
832 // all cases and are sometimes shorter. Prefer to use RISBG for ANDs though,
833 // since it is effectively a three-operand instruction in this case,
834 // and since it can handle some masks that AND IMMEDIATE can't.
835 if (Count < (N->getOpcode() == ISD::AND ? 1U : 2U))
838 // Prefer register extensions like LLC over RISBG.
839 if (RISBG.Rotate == 0 &&
840 (RISBG.Start == 32 || RISBG.Start == 48 || RISBG.Start == 56) &&
844 EVT VT = N->getValueType(0);
846 getUNDEF64(SDLoc(N)),
847 convertTo(SDLoc(N), MVT::i64, RISBG.Input),
848 CurDAG->getTargetConstant(RISBG.Start, MVT::i32),
849 CurDAG->getTargetConstant(RISBG.End | 128, MVT::i32),
850 CurDAG->getTargetConstant(RISBG.Rotate, MVT::i32)
852 N = CurDAG->getMachineNode(SystemZ::RISBG, SDLoc(N), MVT::i64, Ops);
853 return convertTo(SDLoc(N), VT, SDValue(N, 0)).getNode();
856 SDNode *SystemZDAGToDAGISel::tryRxSBG(SDNode *N, unsigned Opcode) {
857 // Try treating each operand of N as the second operand of the RxSBG
858 // and see which goes deepest.
859 RxSBGOperands RxSBG[] = {
860 RxSBGOperands(Opcode, N->getOperand(0)),
861 RxSBGOperands(Opcode, N->getOperand(1))
863 unsigned Count[] = { 0, 0 };
864 for (unsigned I = 0; I < 2; ++I)
865 while (expandRxSBG(RxSBG[I]))
868 // Do nothing if neither operand is suitable.
869 if (Count[0] == 0 && Count[1] == 0)
872 // Pick the deepest second operand.
873 unsigned I = Count[0] > Count[1] ? 0 : 1;
874 SDValue Op0 = N->getOperand(I ^ 1);
876 // Prefer IC for character insertions from memory.
877 if (Opcode == SystemZ::ROSBG && (RxSBG[I].Mask & 0xff) == 0)
878 if (LoadSDNode *Load = dyn_cast<LoadSDNode>(Op0.getNode()))
879 if (Load->getMemoryVT() == MVT::i8)
882 // See whether we can avoid an AND in the first operand by converting
884 if (Opcode == SystemZ::ROSBG && detectOrAndInsertion(Op0, RxSBG[I].Mask))
885 Opcode = SystemZ::RISBG;
887 EVT VT = N->getValueType(0);
889 convertTo(SDLoc(N), MVT::i64, Op0),
890 convertTo(SDLoc(N), MVT::i64, RxSBG[I].Input),
891 CurDAG->getTargetConstant(RxSBG[I].Start, MVT::i32),
892 CurDAG->getTargetConstant(RxSBG[I].End, MVT::i32),
893 CurDAG->getTargetConstant(RxSBG[I].Rotate, MVT::i32)
895 N = CurDAG->getMachineNode(Opcode, SDLoc(N), MVT::i64, Ops);
896 return convertTo(SDLoc(N), VT, SDValue(N, 0)).getNode();
899 SDNode *SystemZDAGToDAGISel::splitLargeImmediate(unsigned Opcode, SDNode *Node,
900 SDValue Op0, uint64_t UpperVal,
902 EVT VT = Node->getValueType(0);
904 SDValue Upper = CurDAG->getConstant(UpperVal, VT);
906 Upper = CurDAG->getNode(Opcode, DL, VT, Op0, Upper);
907 Upper = SDValue(Select(Upper.getNode()), 0);
909 SDValue Lower = CurDAG->getConstant(LowerVal, VT);
910 SDValue Or = CurDAG->getNode(Opcode, DL, VT, Upper, Lower);
914 // N is a (store (load ...), ...) pattern. Return true if it can use MVC.
915 bool SystemZDAGToDAGISel::storeLoadCanUseMVC(SDNode *N) const {
916 StoreSDNode *Store = cast<StoreSDNode>(N);
917 LoadSDNode *Load = cast<LoadSDNode>(Store->getValue().getNode());
919 // MVC is logically a bytewise copy, so can't be used for volatile accesses.
920 if (Load->isVolatile() || Store->isVolatile())
923 // Prefer not to use MVC if either address can use ... RELATIVE LONG
925 assert(Load->getMemoryVT() == Store->getMemoryVT() &&
926 "Should already have checked that the types match");
927 uint64_t Size = Load->getMemoryVT().getStoreSize();
928 if (Size > 1 && Size <= 8) {
929 // Prefer LHRL, LRL and LGRL.
930 if (Load->getBasePtr().getOpcode() == SystemZISD::PCREL_WRAPPER)
932 // Prefer STHRL, STRL and STGRL.
933 if (Store->getBasePtr().getOpcode() == SystemZISD::PCREL_WRAPPER)
937 // There's no chance of overlap if the load is invariant.
938 if (Load->isInvariant())
941 // If both operands are aligned, they must be equal or not overlap.
942 if (Load->getAlignment() >= Size && Store->getAlignment() >= Size)
945 // Otherwise we need to check whether there's an alias.
946 const Value *V1 = Load->getSrcValue();
947 const Value *V2 = Store->getSrcValue();
951 int64_t End1 = Load->getSrcValueOffset() + Size;
952 int64_t End2 = Store->getSrcValueOffset() + Size;
953 return !AA->alias(AliasAnalysis::Location(V1, End1, Load->getTBAAInfo()),
954 AliasAnalysis::Location(V2, End2, Store->getTBAAInfo()));
957 SDNode *SystemZDAGToDAGISel::Select(SDNode *Node) {
958 // Dump information about the Node being selected
959 DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n");
961 // If we have a custom node, we already have selected!
962 if (Node->isMachineOpcode()) {
963 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
967 unsigned Opcode = Node->getOpcode();
971 if (Node->getOperand(1).getOpcode() != ISD::Constant)
972 ResNode = tryRxSBG(Node, SystemZ::ROSBG);
976 if (Node->getOperand(1).getOpcode() != ISD::Constant)
977 ResNode = tryRxSBG(Node, SystemZ::RXSBG);
980 // If this is a 64-bit operation in which both 32-bit halves are nonzero,
981 // split the operation into two.
982 if (!ResNode && Node->getValueType(0) == MVT::i64)
983 if (ConstantSDNode *Op1 = dyn_cast<ConstantSDNode>(Node->getOperand(1))) {
984 uint64_t Val = Op1->getZExtValue();
985 if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val))
986 Node = splitLargeImmediate(Opcode, Node, Node->getOperand(0),
987 Val - uint32_t(Val), uint32_t(Val));
992 if (Node->getOperand(1).getOpcode() != ISD::Constant)
993 ResNode = tryRxSBG(Node, SystemZ::RNSBG);
999 ResNode = tryRISBGZero(Node);
1003 // If this is a 64-bit constant that is out of the range of LLILF,
1004 // LLIHF and LGFI, split it into two 32-bit pieces.
1005 if (Node->getValueType(0) == MVT::i64) {
1006 uint64_t Val = cast<ConstantSDNode>(Node)->getZExtValue();
1007 if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val) && !isInt<32>(Val))
1008 Node = splitLargeImmediate(ISD::OR, Node, SDValue(),
1009 Val - uint32_t(Val), uint32_t(Val));
1013 case ISD::ATOMIC_LOAD_SUB:
1014 // Try to convert subtractions of constants to additions.
1015 if (ConstantSDNode *Op2 = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
1016 uint64_t Value = -Op2->getZExtValue();
1017 EVT VT = Node->getValueType(0);
1018 if (VT == MVT::i32 || isInt<32>(Value)) {
1019 SDValue Ops[] = { Node->getOperand(0), Node->getOperand(1),
1020 CurDAG->getConstant(int32_t(Value), VT) };
1021 Node = CurDAG->MorphNodeTo(Node, ISD::ATOMIC_LOAD_ADD,
1022 Node->getVTList(), Ops, array_lengthof(Ops));
1028 // Select the default instruction
1030 ResNode = SelectCode(Node);
1032 DEBUG(errs() << "=> ";
1033 if (ResNode == NULL || ResNode == Node)
1036 ResNode->dump(CurDAG);
1042 bool SystemZDAGToDAGISel::
1043 SelectInlineAsmMemoryOperand(const SDValue &Op,
1044 char ConstraintCode,
1045 std::vector<SDValue> &OutOps) {
1046 assert(ConstraintCode == 'm' && "Unexpected constraint code");
1047 // Accept addresses with short displacements, which are compatible
1048 // with Q, R, S and T. But keep the index operand for future expansion.
1049 SDValue Base, Disp, Index;
1050 if (!selectBDXAddr(SystemZAddressingMode::FormBD,
1051 SystemZAddressingMode::Disp12Only,
1052 Op, Base, Disp, Index))
1054 OutOps.push_back(Base);
1055 OutOps.push_back(Disp);
1056 OutOps.push_back(Index);