1 //===-- SparcISelDAGToDAG.cpp - A dag to dag inst selector for Sparc ------===//
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 SPARC target.
12 //===----------------------------------------------------------------------===//
14 #include "SparcISelLowering.h"
15 #include "SparcTargetMachine.h"
16 #include "llvm/Intrinsics.h"
17 #include "llvm/CodeGen/SelectionDAGISel.h"
18 #include "llvm/Support/Compiler.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/raw_ostream.h"
24 //===----------------------------------------------------------------------===//
25 // Instruction Selector Implementation
26 //===----------------------------------------------------------------------===//
28 //===--------------------------------------------------------------------===//
29 /// SparcDAGToDAGISel - SPARC specific code to select SPARC machine
30 /// instructions for SelectionDAG operations.
33 class SparcDAGToDAGISel
: public SelectionDAGISel
{
34 /// Subtarget - Keep a pointer to the Sparc Subtarget around so that we can
35 /// make the right decision when generating code for different targets.
36 const SparcSubtarget
&Subtarget
;
37 SparcTargetMachine
& TM
;
38 MachineBasicBlock
*CurBB
;
40 explicit SparcDAGToDAGISel(SparcTargetMachine
&tm
)
41 : SelectionDAGISel(tm
),
42 Subtarget(tm
.getSubtarget
<SparcSubtarget
>()),
46 SDNode
*Select(SDValue Op
);
48 // Complex Pattern Selectors.
49 bool SelectADDRrr(SDValue Op
, SDValue N
, SDValue
&R1
, SDValue
&R2
);
50 bool SelectADDRri(SDValue Op
, SDValue N
, SDValue
&Base
,
53 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
54 /// inline asm expressions.
55 virtual bool SelectInlineAsmMemoryOperand(const SDValue
&Op
,
57 std::vector
<SDValue
> &OutOps
);
59 /// InstructionSelect - This callback is invoked by
60 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
61 virtual void InstructionSelect();
63 virtual const char *getPassName() const {
64 return "SPARC DAG->DAG Pattern Instruction Selection";
67 // Include the pieces autogenerated from the target description.
68 #include "SparcGenDAGISel.inc"
71 SDNode
* getGlobalBaseReg();
73 } // end anonymous namespace
75 /// InstructionSelect - This callback is invoked by
76 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
77 void SparcDAGToDAGISel::InstructionSelect() {
80 // Select target instructions for the DAG.
82 CurDAG
->RemoveDeadNodes();
85 SDNode
* SparcDAGToDAGISel::getGlobalBaseReg() {
86 MachineFunction
*MF
= CurBB
->getParent();
87 unsigned GlobalBaseReg
= TM
.getInstrInfo()->getGlobalBaseReg(MF
);
88 return CurDAG
->getRegister(GlobalBaseReg
, TLI
.getPointerTy()).getNode();
91 bool SparcDAGToDAGISel::SelectADDRri(SDValue Op
, SDValue Addr
,
92 SDValue
&Base
, SDValue
&Offset
) {
93 if (FrameIndexSDNode
*FIN
= dyn_cast
<FrameIndexSDNode
>(Addr
)) {
94 Base
= CurDAG
->getTargetFrameIndex(FIN
->getIndex(), MVT::i32
);
95 Offset
= CurDAG
->getTargetConstant(0, MVT::i32
);
98 if (Addr
.getOpcode() == ISD::TargetExternalSymbol
||
99 Addr
.getOpcode() == ISD::TargetGlobalAddress
)
100 return false; // direct calls.
102 if (Addr
.getOpcode() == ISD::ADD
) {
103 if (ConstantSDNode
*CN
= dyn_cast
<ConstantSDNode
>(Addr
.getOperand(1))) {
104 if (Predicate_simm13(CN
)) {
105 if (FrameIndexSDNode
*FIN
=
106 dyn_cast
<FrameIndexSDNode
>(Addr
.getOperand(0))) {
107 // Constant offset from frame ref.
108 Base
= CurDAG
->getTargetFrameIndex(FIN
->getIndex(), MVT::i32
);
110 Base
= Addr
.getOperand(0);
112 Offset
= CurDAG
->getTargetConstant(CN
->getZExtValue(), MVT::i32
);
116 if (Addr
.getOperand(0).getOpcode() == SPISD::Lo
) {
117 Base
= Addr
.getOperand(1);
118 Offset
= Addr
.getOperand(0).getOperand(0);
121 if (Addr
.getOperand(1).getOpcode() == SPISD::Lo
) {
122 Base
= Addr
.getOperand(0);
123 Offset
= Addr
.getOperand(1).getOperand(0);
128 Offset
= CurDAG
->getTargetConstant(0, MVT::i32
);
132 bool SparcDAGToDAGISel::SelectADDRrr(SDValue Op
, SDValue Addr
,
133 SDValue
&R1
, SDValue
&R2
) {
134 if (Addr
.getOpcode() == ISD::FrameIndex
) return false;
135 if (Addr
.getOpcode() == ISD::TargetExternalSymbol
||
136 Addr
.getOpcode() == ISD::TargetGlobalAddress
)
137 return false; // direct calls.
139 if (Addr
.getOpcode() == ISD::ADD
) {
140 if (isa
<ConstantSDNode
>(Addr
.getOperand(1)) &&
141 Predicate_simm13(Addr
.getOperand(1).getNode()))
142 return false; // Let the reg+imm pattern catch this!
143 if (Addr
.getOperand(0).getOpcode() == SPISD::Lo
||
144 Addr
.getOperand(1).getOpcode() == SPISD::Lo
)
145 return false; // Let the reg+imm pattern catch this!
146 R1
= Addr
.getOperand(0);
147 R2
= Addr
.getOperand(1);
152 R2
= CurDAG
->getRegister(SP::G0
, MVT::i32
);
156 SDNode
*SparcDAGToDAGISel::Select(SDValue Op
) {
157 SDNode
*N
= Op
.getNode();
158 DebugLoc dl
= N
->getDebugLoc();
159 if (N
->isMachineOpcode())
160 return NULL
; // Already selected.
162 switch (N
->getOpcode()) {
164 case SPISD::GLOBAL_BASE_REG
:
165 return getGlobalBaseReg();
169 // FIXME: should use a custom expander to expose the SRA to the dag.
170 SDValue DivLHS
= N
->getOperand(0);
171 SDValue DivRHS
= N
->getOperand(1);
173 // Set the Y register to the high-part.
175 if (N
->getOpcode() == ISD::SDIV
) {
176 TopPart
= SDValue(CurDAG
->getTargetNode(SP::SRAri
, dl
, MVT::i32
, DivLHS
,
177 CurDAG
->getTargetConstant(31, MVT::i32
)), 0);
179 TopPart
= CurDAG
->getRegister(SP::G0
, MVT::i32
);
181 TopPart
= SDValue(CurDAG
->getTargetNode(SP::WRYrr
, dl
, MVT::Flag
, TopPart
,
182 CurDAG
->getRegister(SP::G0
, MVT::i32
)), 0);
184 // FIXME: Handle div by immediate.
185 unsigned Opcode
= N
->getOpcode() == ISD::SDIV
? SP::SDIVrr
: SP::UDIVrr
;
186 return CurDAG
->SelectNodeTo(N
, Opcode
, MVT::i32
, DivLHS
, DivRHS
,
191 // FIXME: Handle mul by immediate.
192 SDValue MulLHS
= N
->getOperand(0);
193 SDValue MulRHS
= N
->getOperand(1);
194 unsigned Opcode
= N
->getOpcode() == ISD::MULHU
? SP::UMULrr
: SP::SMULrr
;
195 SDNode
*Mul
= CurDAG
->getTargetNode(Opcode
, dl
, MVT::i32
, MVT::Flag
,
197 // The high part is in the Y register.
198 return CurDAG
->SelectNodeTo(N
, SP::RDY
, MVT::i32
, SDValue(Mul
, 1));
203 return SelectCode(Op
);
207 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
208 /// inline asm expressions.
210 SparcDAGToDAGISel::SelectInlineAsmMemoryOperand(const SDValue
&Op
,
212 std::vector
<SDValue
> &OutOps
) {
214 switch (ConstraintCode
) {
215 default: return true;
217 if (!SelectADDRrr(Op
, Op
, Op0
, Op1
))
218 SelectADDRri(Op
, Op
, Op0
, Op1
);
222 OutOps
.push_back(Op0
);
223 OutOps
.push_back(Op1
);
227 /// createSparcISelDag - This pass converts a legalized DAG into a
228 /// SPARC-specific DAG, ready for instruction scheduling.
230 FunctionPass
*llvm::createSparcISelDag(SparcTargetMachine
&TM
) {
231 return new SparcDAGToDAGISel(TM
);