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 //===----------------------------------------------------------------------===//
15 #include "SystemZTargetMachine.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Function.h"
18 #include "llvm/Intrinsics.h"
19 #include "llvm/CallingConv.h"
20 #include "llvm/Constants.h"
21 #include "llvm/CodeGen/MachineFrameInfo.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/CodeGen/SelectionDAG.h"
26 #include "llvm/CodeGen/SelectionDAGISel.h"
27 #include "llvm/Target/TargetLowering.h"
28 #include "llvm/Support/Compiler.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/raw_ostream.h"
34 /// SystemZRRIAddressMode - This corresponds to rriaddr, but uses SDValue's
35 /// instead of register numbers for the leaves of the matched tree.
36 struct SystemZRRIAddressMode
{
42 struct { // This is really a union, discriminated by BaseType!
51 SystemZRRIAddressMode(bool RI
= false)
52 : BaseType(RegBase
), IndexReg(), Disp(0), isRI(RI
) {
56 errs() << "SystemZRRIAddressMode " << this << '\n';
57 if (BaseType
== RegBase
) {
58 errs() << "Base.Reg ";
59 if (Base
.Reg
.getNode() != 0)
60 Base
.Reg
.getNode()->dump();
65 errs() << " Base.FrameIndex " << Base
.FrameIndex
<< '\n';
68 errs() << "IndexReg ";
69 if (IndexReg
.getNode() != 0) IndexReg
.getNode()->dump();
72 errs() << " Disp " << Disp
<< '\n';
77 /// SystemZDAGToDAGISel - SystemZ specific code to select SystemZ machine
78 /// instructions for SelectionDAG operations.
81 class SystemZDAGToDAGISel
: public SelectionDAGISel
{
82 const SystemZTargetLowering
&Lowering
;
83 const SystemZSubtarget
&Subtarget
;
85 void getAddressOperandsRI(const SystemZRRIAddressMode
&AM
,
86 SDValue
&Base
, SDValue
&Disp
);
87 void getAddressOperands(const SystemZRRIAddressMode
&AM
,
88 SDValue
&Base
, SDValue
&Disp
,
92 SystemZDAGToDAGISel(SystemZTargetMachine
&TM
, CodeGenOpt::Level OptLevel
)
93 : SelectionDAGISel(TM
, OptLevel
),
94 Lowering(*TM
.getTargetLowering()),
95 Subtarget(*TM
.getSubtargetImpl()) { }
97 virtual const char *getPassName() const {
98 return "SystemZ DAG->DAG Pattern Instruction Selection";
101 /// getI8Imm - Return a target constant with the specified value, of type
103 inline SDValue
getI8Imm(uint64_t Imm
) {
104 return CurDAG
->getTargetConstant(Imm
, MVT::i8
);
107 /// getI16Imm - Return a target constant with the specified value, of type
109 inline SDValue
getI16Imm(uint64_t Imm
) {
110 return CurDAG
->getTargetConstant(Imm
, MVT::i16
);
113 /// getI32Imm - Return a target constant with the specified value, of type
115 inline SDValue
getI32Imm(uint64_t Imm
) {
116 return CurDAG
->getTargetConstant(Imm
, MVT::i32
);
119 // Include the pieces autogenerated from the target description.
120 #include "SystemZGenDAGISel.inc"
123 bool SelectAddrRI12Only(SDValue
& Addr
,
124 SDValue
&Base
, SDValue
&Disp
);
125 bool SelectAddrRI12(SDValue
& Addr
,
126 SDValue
&Base
, SDValue
&Disp
,
127 bool is12BitOnly
= false);
128 bool SelectAddrRI(SDValue
& Addr
, SDValue
&Base
, SDValue
&Disp
);
129 bool SelectAddrRRI12(SDValue Addr
,
130 SDValue
&Base
, SDValue
&Disp
, SDValue
&Index
);
131 bool SelectAddrRRI20(SDValue Addr
,
132 SDValue
&Base
, SDValue
&Disp
, SDValue
&Index
);
133 bool SelectLAAddr(SDValue Addr
,
134 SDValue
&Base
, SDValue
&Disp
, SDValue
&Index
);
136 SDNode
*Select(SDNode
*Node
);
138 bool TryFoldLoad(SDNode
*P
, SDValue N
,
139 SDValue
&Base
, SDValue
&Disp
, SDValue
&Index
);
141 bool MatchAddress(SDValue N
, SystemZRRIAddressMode
&AM
,
142 bool is12Bit
, unsigned Depth
= 0);
143 bool MatchAddressBase(SDValue N
, SystemZRRIAddressMode
&AM
);
145 } // end anonymous namespace
147 /// createSystemZISelDag - This pass converts a legalized DAG into a
148 /// SystemZ-specific DAG, ready for instruction scheduling.
150 FunctionPass
*llvm::createSystemZISelDag(SystemZTargetMachine
&TM
,
151 CodeGenOpt::Level OptLevel
) {
152 return new SystemZDAGToDAGISel(TM
, OptLevel
);
155 /// isImmSExt20 - This method tests to see if the node is either a 32-bit
156 /// or 64-bit immediate, and if the value can be accurately represented as a
157 /// sign extension from a 20-bit value. If so, this returns true and the
159 static bool isImmSExt20(int64_t Val
, int64_t &Imm
) {
160 if (Val
>= -524288 && Val
<= 524287) {
167 /// isImmZExt12 - This method tests to see if the node is either a 32-bit
168 /// or 64-bit immediate, and if the value can be accurately represented as a
169 /// zero extension from a 12-bit value. If so, this returns true and the
171 static bool isImmZExt12(int64_t Val
, int64_t &Imm
) {
172 if (Val
>= 0 && Val
<= 0xFFF) {
179 /// MatchAddress - Add the specified node to the specified addressing mode,
180 /// returning true if it cannot be done. This just pattern matches for the
182 bool SystemZDAGToDAGISel::MatchAddress(SDValue N
, SystemZRRIAddressMode
&AM
,
183 bool is12Bit
, unsigned Depth
) {
184 DebugLoc dl
= N
.getDebugLoc();
185 DEBUG(errs() << "MatchAddress: "; AM
.dump());
188 return MatchAddressBase(N
, AM
);
190 // FIXME: We can perform better here. If we have something like
191 // (shift (add A, imm), N), we can try to reassociate stuff and fold shift of
192 // imm into addressing mode.
193 switch (N
.getOpcode()) {
195 case ISD::Constant
: {
196 int64_t Val
= cast
<ConstantSDNode
>(N
)->getSExtValue();
198 bool Match
= (is12Bit
?
199 isImmZExt12(AM
.Disp
+ Val
, Imm
) :
200 isImmSExt20(AM
.Disp
+ Val
, Imm
));
208 case ISD::FrameIndex
:
209 if (AM
.BaseType
== SystemZRRIAddressMode::RegBase
&&
210 AM
.Base
.Reg
.getNode() == 0) {
211 AM
.BaseType
= SystemZRRIAddressMode::FrameIndexBase
;
212 AM
.Base
.FrameIndex
= cast
<FrameIndexSDNode
>(N
)->getIndex();
218 // Given A-B, if A can be completely folded into the address and
219 // the index field with the index field unused, use -B as the index.
220 // This is a win if a has multiple parts that can be folded into
221 // the address. Also, this saves a mov if the base register has
222 // other uses, since it avoids a two-address sub instruction, however
223 // it costs an additional mov if the index register has other uses.
225 // Test if the LHS of the sub can be folded.
226 SystemZRRIAddressMode Backup
= AM
;
227 if (MatchAddress(N
.getNode()->getOperand(0), AM
, is12Bit
, Depth
+1)) {
231 // Test if the index field is free for use.
232 if (AM
.IndexReg
.getNode() || AM
.isRI
) {
237 // If the base is a register with multiple uses, this transformation may
238 // save a mov. Otherwise it's probably better not to do it.
239 if (AM
.BaseType
== SystemZRRIAddressMode::RegBase
&&
240 (!AM
.Base
.Reg
.getNode() || AM
.Base
.Reg
.getNode()->hasOneUse())) {
245 // Ok, the transformation is legal and appears profitable. Go for it.
246 SDValue RHS
= N
.getNode()->getOperand(1);
247 SDValue Zero
= CurDAG
->getConstant(0, N
.getValueType());
248 SDValue Neg
= CurDAG
->getNode(ISD::SUB
, dl
, N
.getValueType(), Zero
, RHS
);
251 // Insert the new nodes into the topological ordering.
252 if (Zero
.getNode()->getNodeId() == -1 ||
253 Zero
.getNode()->getNodeId() > N
.getNode()->getNodeId()) {
254 CurDAG
->RepositionNode(N
.getNode(), Zero
.getNode());
255 Zero
.getNode()->setNodeId(N
.getNode()->getNodeId());
257 if (Neg
.getNode()->getNodeId() == -1 ||
258 Neg
.getNode()->getNodeId() > N
.getNode()->getNodeId()) {
259 CurDAG
->RepositionNode(N
.getNode(), Neg
.getNode());
260 Neg
.getNode()->setNodeId(N
.getNode()->getNodeId());
266 SystemZRRIAddressMode Backup
= AM
;
267 if (!MatchAddress(N
.getNode()->getOperand(0), AM
, is12Bit
, Depth
+1) &&
268 !MatchAddress(N
.getNode()->getOperand(1), AM
, is12Bit
, Depth
+1))
271 if (!MatchAddress(N
.getNode()->getOperand(1), AM
, is12Bit
, Depth
+1) &&
272 !MatchAddress(N
.getNode()->getOperand(0), AM
, is12Bit
, Depth
+1))
276 // If we couldn't fold both operands into the address at the same time,
277 // see if we can just put each operand into a register and fold at least
280 AM
.BaseType
== SystemZRRIAddressMode::RegBase
&&
281 !AM
.Base
.Reg
.getNode() && !AM
.IndexReg
.getNode()) {
282 AM
.Base
.Reg
= N
.getNode()->getOperand(0);
283 AM
.IndexReg
= N
.getNode()->getOperand(1);
290 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
291 if (ConstantSDNode
*CN
= dyn_cast
<ConstantSDNode
>(N
.getOperand(1))) {
292 SystemZRRIAddressMode Backup
= AM
;
293 int64_t Offset
= CN
->getSExtValue();
295 bool MatchOffset
= (is12Bit
?
296 isImmZExt12(AM
.Disp
+ Offset
, Imm
) :
297 isImmSExt20(AM
.Disp
+ Offset
, Imm
));
298 // The resultant disp must fit in 12 or 20-bits.
300 // LHS should be an addr mode.
301 !MatchAddress(N
.getOperand(0), AM
, is12Bit
, Depth
+1) &&
302 // Check to see if the LHS & C is zero.
303 CurDAG
->MaskedValueIsZero(N
.getOperand(0), CN
->getAPIntValue())) {
312 return MatchAddressBase(N
, AM
);
315 /// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
316 /// specified addressing mode without any further recursion.
317 bool SystemZDAGToDAGISel::MatchAddressBase(SDValue N
,
318 SystemZRRIAddressMode
&AM
) {
319 // Is the base register already occupied?
320 if (AM
.BaseType
!= SystemZRRIAddressMode::RegBase
|| AM
.Base
.Reg
.getNode()) {
321 // If so, check to see if the index register is set.
322 if (AM
.IndexReg
.getNode() == 0 && !AM
.isRI
) {
327 // Otherwise, we cannot select it.
331 // Default, generate it as a register.
332 AM
.BaseType
= SystemZRRIAddressMode::RegBase
;
337 void SystemZDAGToDAGISel::getAddressOperandsRI(const SystemZRRIAddressMode
&AM
,
338 SDValue
&Base
, SDValue
&Disp
) {
339 if (AM
.BaseType
== SystemZRRIAddressMode::RegBase
)
342 Base
= CurDAG
->getTargetFrameIndex(AM
.Base
.FrameIndex
, TLI
.getPointerTy());
343 Disp
= CurDAG
->getTargetConstant(AM
.Disp
, MVT::i64
);
346 void SystemZDAGToDAGISel::getAddressOperands(const SystemZRRIAddressMode
&AM
,
347 SDValue
&Base
, SDValue
&Disp
,
349 getAddressOperandsRI(AM
, Base
, Disp
);
353 /// Returns true if the address can be represented by a base register plus
354 /// an unsigned 12-bit displacement [r+imm].
355 bool SystemZDAGToDAGISel::SelectAddrRI12Only(SDValue
&Addr
,
356 SDValue
&Base
, SDValue
&Disp
) {
357 return SelectAddrRI12(Addr
, Base
, Disp
, /*is12BitOnly*/true);
360 bool SystemZDAGToDAGISel::SelectAddrRI12(SDValue
&Addr
,
361 SDValue
&Base
, SDValue
&Disp
,
363 SystemZRRIAddressMode
AM20(/*isRI*/true), AM12(/*isRI*/true);
366 if (!Addr
.hasOneUse()) {
367 unsigned Opcode
= Addr
.getOpcode();
368 if (Opcode
!= ISD::Constant
&& Opcode
!= ISD::FrameIndex
) {
369 // If we are able to fold N into addressing mode, then we'll allow it even
370 // if N has multiple uses. In general, addressing computation is used as
371 // addresses by all of its uses. But watch out for CopyToReg uses, that
372 // means the address computation is liveout. It will be computed by a LA
373 // so we want to avoid computing the address twice.
374 for (SDNode::use_iterator UI
= Addr
.getNode()->use_begin(),
375 UE
= Addr
.getNode()->use_end(); UI
!= UE
; ++UI
) {
376 if (UI
->getOpcode() == ISD::CopyToReg
) {
377 MatchAddressBase(Addr
, AM12
);
384 if (!Done
&& MatchAddress(Addr
, AM12
, /* is12Bit */ true))
387 // Check, whether we can match stuff using 20-bit displacements
388 if (!Done
&& !is12BitOnly
&&
389 !MatchAddress(Addr
, AM20
, /* is12Bit */ false))
390 if (AM12
.Disp
== 0 && AM20
.Disp
!= 0)
393 DEBUG(errs() << "MatchAddress (final): "; AM12
.dump());
395 EVT VT
= Addr
.getValueType();
396 if (AM12
.BaseType
== SystemZRRIAddressMode::RegBase
) {
397 if (!AM12
.Base
.Reg
.getNode())
398 AM12
.Base
.Reg
= CurDAG
->getRegister(0, VT
);
401 assert(AM12
.IndexReg
.getNode() == 0 && "Invalid reg-imm address mode!");
403 getAddressOperandsRI(AM12
, Base
, Disp
);
408 /// Returns true if the address can be represented by a base register plus
409 /// a signed 20-bit displacement [r+imm].
410 bool SystemZDAGToDAGISel::SelectAddrRI(SDValue
& Addr
,
411 SDValue
&Base
, SDValue
&Disp
) {
412 SystemZRRIAddressMode
AM(/*isRI*/true);
415 if (!Addr
.hasOneUse()) {
416 unsigned Opcode
= Addr
.getOpcode();
417 if (Opcode
!= ISD::Constant
&& Opcode
!= ISD::FrameIndex
) {
418 // If we are able to fold N into addressing mode, then we'll allow it even
419 // if N has multiple uses. In general, addressing computation is used as
420 // addresses by all of its uses. But watch out for CopyToReg uses, that
421 // means the address computation is liveout. It will be computed by a LA
422 // so we want to avoid computing the address twice.
423 for (SDNode::use_iterator UI
= Addr
.getNode()->use_begin(),
424 UE
= Addr
.getNode()->use_end(); UI
!= UE
; ++UI
) {
425 if (UI
->getOpcode() == ISD::CopyToReg
) {
426 MatchAddressBase(Addr
, AM
);
433 if (!Done
&& MatchAddress(Addr
, AM
, /* is12Bit */ false))
436 DEBUG(errs() << "MatchAddress (final): "; AM
.dump());
438 EVT VT
= Addr
.getValueType();
439 if (AM
.BaseType
== SystemZRRIAddressMode::RegBase
) {
440 if (!AM
.Base
.Reg
.getNode())
441 AM
.Base
.Reg
= CurDAG
->getRegister(0, VT
);
444 assert(AM
.IndexReg
.getNode() == 0 && "Invalid reg-imm address mode!");
446 getAddressOperandsRI(AM
, Base
, Disp
);
451 /// Returns true if the address can be represented by a base register plus
452 /// index register plus an unsigned 12-bit displacement [base + idx + imm].
453 bool SystemZDAGToDAGISel::SelectAddrRRI12(SDValue Addr
,
454 SDValue
&Base
, SDValue
&Disp
, SDValue
&Index
) {
455 SystemZRRIAddressMode AM20
, AM12
;
458 if (!Addr
.hasOneUse()) {
459 unsigned Opcode
= Addr
.getOpcode();
460 if (Opcode
!= ISD::Constant
&& Opcode
!= ISD::FrameIndex
) {
461 // If we are able to fold N into addressing mode, then we'll allow it even
462 // if N has multiple uses. In general, addressing computation is used as
463 // addresses by all of its uses. But watch out for CopyToReg uses, that
464 // means the address computation is liveout. It will be computed by a LA
465 // so we want to avoid computing the address twice.
466 for (SDNode::use_iterator UI
= Addr
.getNode()->use_begin(),
467 UE
= Addr
.getNode()->use_end(); UI
!= UE
; ++UI
) {
468 if (UI
->getOpcode() == ISD::CopyToReg
) {
469 MatchAddressBase(Addr
, AM12
);
476 if (!Done
&& MatchAddress(Addr
, AM12
, /* is12Bit */ true))
479 // Check, whether we can match stuff using 20-bit displacements
480 if (!Done
&& !MatchAddress(Addr
, AM20
, /* is12Bit */ false))
481 if (AM12
.Disp
== 0 && AM20
.Disp
!= 0)
484 DEBUG(errs() << "MatchAddress (final): "; AM12
.dump());
486 EVT VT
= Addr
.getValueType();
487 if (AM12
.BaseType
== SystemZRRIAddressMode::RegBase
) {
488 if (!AM12
.Base
.Reg
.getNode())
489 AM12
.Base
.Reg
= CurDAG
->getRegister(0, VT
);
492 if (!AM12
.IndexReg
.getNode())
493 AM12
.IndexReg
= CurDAG
->getRegister(0, VT
);
495 getAddressOperands(AM12
, Base
, Disp
, Index
);
500 /// Returns true if the address can be represented by a base register plus
501 /// index register plus a signed 20-bit displacement [base + idx + imm].
502 bool SystemZDAGToDAGISel::SelectAddrRRI20(SDValue Addr
,
503 SDValue
&Base
, SDValue
&Disp
, SDValue
&Index
) {
504 SystemZRRIAddressMode AM
;
507 if (!Addr
.hasOneUse()) {
508 unsigned Opcode
= Addr
.getOpcode();
509 if (Opcode
!= ISD::Constant
&& Opcode
!= ISD::FrameIndex
) {
510 // If we are able to fold N into addressing mode, then we'll allow it even
511 // if N has multiple uses. In general, addressing computation is used as
512 // addresses by all of its uses. But watch out for CopyToReg uses, that
513 // means the address computation is liveout. It will be computed by a LA
514 // so we want to avoid computing the address twice.
515 for (SDNode::use_iterator UI
= Addr
.getNode()->use_begin(),
516 UE
= Addr
.getNode()->use_end(); UI
!= UE
; ++UI
) {
517 if (UI
->getOpcode() == ISD::CopyToReg
) {
518 MatchAddressBase(Addr
, AM
);
525 if (!Done
&& MatchAddress(Addr
, AM
, /* is12Bit */ false))
528 DEBUG(errs() << "MatchAddress (final): "; AM
.dump());
530 EVT VT
= Addr
.getValueType();
531 if (AM
.BaseType
== SystemZRRIAddressMode::RegBase
) {
532 if (!AM
.Base
.Reg
.getNode())
533 AM
.Base
.Reg
= CurDAG
->getRegister(0, VT
);
536 if (!AM
.IndexReg
.getNode())
537 AM
.IndexReg
= CurDAG
->getRegister(0, VT
);
539 getAddressOperands(AM
, Base
, Disp
, Index
);
544 /// SelectLAAddr - it calls SelectAddr and determines if the maximal addressing
545 /// mode it matches can be cost effectively emitted as an LA/LAY instruction.
546 bool SystemZDAGToDAGISel::SelectLAAddr(SDValue Addr
,
547 SDValue
&Base
, SDValue
&Disp
, SDValue
&Index
) {
548 SystemZRRIAddressMode AM
;
550 if (MatchAddress(Addr
, AM
, false))
553 EVT VT
= Addr
.getValueType();
554 unsigned Complexity
= 0;
555 if (AM
.BaseType
== SystemZRRIAddressMode::RegBase
)
556 if (AM
.Base
.Reg
.getNode())
559 AM
.Base
.Reg
= CurDAG
->getRegister(0, VT
);
560 else if (AM
.BaseType
== SystemZRRIAddressMode::FrameIndexBase
)
563 if (AM
.IndexReg
.getNode())
566 AM
.IndexReg
= CurDAG
->getRegister(0, VT
);
568 if (AM
.Disp
&& (AM
.Base
.Reg
.getNode() || AM
.IndexReg
.getNode()))
571 if (Complexity
> 2) {
572 getAddressOperands(AM
, Base
, Disp
, Index
);
579 bool SystemZDAGToDAGISel::TryFoldLoad(SDNode
*P
, SDValue N
,
580 SDValue
&Base
, SDValue
&Disp
, SDValue
&Index
) {
581 if (ISD::isNON_EXTLoad(N
.getNode()) &&
582 IsLegalToFold(N
, P
, P
, OptLevel
))
583 return SelectAddrRRI20(N
.getOperand(1), Base
, Disp
, Index
);
587 SDNode
*SystemZDAGToDAGISel::Select(SDNode
*Node
) {
588 EVT NVT
= Node
->getValueType(0);
589 DebugLoc dl
= Node
->getDebugLoc();
590 unsigned Opcode
= Node
->getOpcode();
592 // Dump information about the Node being selected
593 DEBUG(errs() << "Selecting: "; Node
->dump(CurDAG
); errs() << "\n");
595 // If we have a custom node, we already have selected!
596 if (Node
->isMachineOpcode()) {
597 DEBUG(errs() << "== "; Node
->dump(CurDAG
); errs() << "\n");
598 return NULL
; // Already selected.
605 SDValue N0
= Node
->getOperand(0);
606 SDValue N1
= Node
->getOperand(1);
609 bool is32Bit
= false;
610 switch (NVT
.getSimpleVT().SimpleTy
) {
611 default: assert(0 && "Unsupported VT!");
613 Opc
= SystemZ::SDIVREM32r
; MOpc
= SystemZ::SDIVREM32m
;
618 Opc
= SystemZ::SDIVREM64r
; MOpc
= SystemZ::SDIVREM64m
;
623 SDValue Tmp0
, Tmp1
, Tmp2
;
624 bool foldedLoad
= TryFoldLoad(Node
, N1
, Tmp0
, Tmp1
, Tmp2
);
626 // Prepare the dividend
629 Dividend
= CurDAG
->getMachineNode(SystemZ::MOVSX64rr32
, dl
, MVT::i64
, N0
);
631 Dividend
= N0
.getNode();
633 // Insert prepared dividend into suitable 'subreg'
634 SDNode
*Tmp
= CurDAG
->getMachineNode(TargetOpcode::IMPLICIT_DEF
,
637 CurDAG
->getMachineNode(TargetOpcode::INSERT_SUBREG
, dl
, ResVT
,
638 SDValue(Tmp
, 0), SDValue(Dividend
, 0),
639 CurDAG
->getTargetConstant(SystemZ::subreg_odd
, MVT::i32
));
642 SDValue DivVal
= SDValue(Dividend
, 0);
644 SDValue Ops
[] = { DivVal
, Tmp0
, Tmp1
, Tmp2
, N1
.getOperand(0) };
645 Result
= CurDAG
->getMachineNode(MOpc
, dl
, ResVT
, MVT::Other
,
646 Ops
, array_lengthof(Ops
));
648 ReplaceUses(N1
.getValue(1), SDValue(Result
, 1));
650 Result
= CurDAG
->getMachineNode(Opc
, dl
, ResVT
, SDValue(Dividend
, 0), N1
);
653 // Copy the division (odd subreg) result, if it is needed.
654 if (!SDValue(Node
, 0).use_empty()) {
655 unsigned SubRegIdx
= (is32Bit
?
656 SystemZ::subreg_odd32
: SystemZ::subreg_odd
);
657 SDNode
*Div
= CurDAG
->getMachineNode(TargetOpcode::EXTRACT_SUBREG
,
660 CurDAG
->getTargetConstant(SubRegIdx
,
663 ReplaceUses(SDValue(Node
, 0), SDValue(Div
, 0));
664 DEBUG(errs() << "=> "; Result
->dump(CurDAG
); errs() << "\n");
667 // Copy the remainder (even subreg) result, if it is needed.
668 if (!SDValue(Node
, 1).use_empty()) {
669 unsigned SubRegIdx
= (is32Bit
?
670 SystemZ::subreg_32bit
: SystemZ::subreg_even
);
671 SDNode
*Rem
= CurDAG
->getMachineNode(TargetOpcode::EXTRACT_SUBREG
,
674 CurDAG
->getTargetConstant(SubRegIdx
,
677 ReplaceUses(SDValue(Node
, 1), SDValue(Rem
, 0));
678 DEBUG(errs() << "=> "; Result
->dump(CurDAG
); errs() << "\n");
684 unsigned Opc
, MOpc
, ClrOpc
;
685 SDValue N0
= Node
->getOperand(0);
686 SDValue N1
= Node
->getOperand(1);
689 bool is32Bit
= false;
690 switch (NVT
.getSimpleVT().SimpleTy
) {
691 default: assert(0 && "Unsupported VT!");
693 Opc
= SystemZ::UDIVREM32r
; MOpc
= SystemZ::UDIVREM32m
;
694 ClrOpc
= SystemZ::MOV64Pr0_even
;
699 Opc
= SystemZ::UDIVREM64r
; MOpc
= SystemZ::UDIVREM64m
;
700 ClrOpc
= SystemZ::MOV128r0_even
;
705 SDValue Tmp0
, Tmp1
, Tmp2
;
706 bool foldedLoad
= TryFoldLoad(Node
, N1
, Tmp0
, Tmp1
, Tmp2
);
708 // Prepare the dividend
709 SDNode
*Dividend
= N0
.getNode();
711 // Insert prepared dividend into suitable 'subreg'
712 SDNode
*Tmp
= CurDAG
->getMachineNode(TargetOpcode::IMPLICIT_DEF
,
715 unsigned SubRegIdx
= (is32Bit
?
716 SystemZ::subreg_odd32
: SystemZ::subreg_odd
);
718 CurDAG
->getMachineNode(TargetOpcode::INSERT_SUBREG
, dl
, ResVT
,
719 SDValue(Tmp
, 0), SDValue(Dividend
, 0),
720 CurDAG
->getTargetConstant(SubRegIdx
, MVT::i32
));
723 // Zero out even subreg
724 Dividend
= CurDAG
->getMachineNode(ClrOpc
, dl
, ResVT
, SDValue(Dividend
, 0));
726 SDValue DivVal
= SDValue(Dividend
, 0);
729 SDValue Ops
[] = { DivVal
, Tmp0
, Tmp1
, Tmp2
, N1
.getOperand(0) };
730 Result
= CurDAG
->getMachineNode(MOpc
, dl
, ResVT
, MVT::Other
,
731 Ops
, array_lengthof(Ops
));
733 ReplaceUses(N1
.getValue(1), SDValue(Result
, 1));
735 Result
= CurDAG
->getMachineNode(Opc
, dl
, ResVT
, DivVal
, N1
);
738 // Copy the division (odd subreg) result, if it is needed.
739 if (!SDValue(Node
, 0).use_empty()) {
740 unsigned SubRegIdx
= (is32Bit
?
741 SystemZ::subreg_odd32
: SystemZ::subreg_odd
);
742 SDNode
*Div
= CurDAG
->getMachineNode(TargetOpcode::EXTRACT_SUBREG
,
745 CurDAG
->getTargetConstant(SubRegIdx
,
747 ReplaceUses(SDValue(Node
, 0), SDValue(Div
, 0));
748 DEBUG(errs() << "=> "; Result
->dump(CurDAG
); errs() << "\n");
751 // Copy the remainder (even subreg) result, if it is needed.
752 if (!SDValue(Node
, 1).use_empty()) {
753 unsigned SubRegIdx
= (is32Bit
?
754 SystemZ::subreg_32bit
: SystemZ::subreg_even
);
755 SDNode
*Rem
= CurDAG
->getMachineNode(TargetOpcode::EXTRACT_SUBREG
,
758 CurDAG
->getTargetConstant(SubRegIdx
,
760 ReplaceUses(SDValue(Node
, 1), SDValue(Rem
, 0));
761 DEBUG(errs() << "=> "; Result
->dump(CurDAG
); errs() << "\n");
768 // Select the default instruction
769 SDNode
*ResNode
= SelectCode(Node
);
771 DEBUG(errs() << "=> ";
772 if (ResNode
== NULL
|| ResNode
== Node
)
775 ResNode
->dump(CurDAG
);