Remove the TargetMachine forwards for TargetSubtargetInfo based
[oota-llvm.git] / lib / Target / Sparc / SparcISelDAGToDAG.cpp
1 //===-- SparcISelDAGToDAG.cpp - A dag to dag inst selector for Sparc ------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines an instruction selector for the SPARC target.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "SparcTargetMachine.h"
15 #include "llvm/CodeGen/SelectionDAGISel.h"
16 #include "llvm/IR/Intrinsics.h"
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/raw_ostream.h"
21 using namespace llvm;
22
23 //===----------------------------------------------------------------------===//
24 // Instruction Selector Implementation
25 //===----------------------------------------------------------------------===//
26
27 //===--------------------------------------------------------------------===//
28 /// SparcDAGToDAGISel - SPARC specific code to select SPARC machine
29 /// instructions for SelectionDAG operations.
30 ///
31 namespace {
32 class SparcDAGToDAGISel : public SelectionDAGISel {
33   /// Subtarget - Keep a pointer to the Sparc Subtarget around so that we can
34   /// make the right decision when generating code for different targets.
35   const SparcSubtarget &Subtarget;
36   SparcTargetMachine &TM;
37 public:
38   explicit SparcDAGToDAGISel(SparcTargetMachine &tm)
39     : SelectionDAGISel(tm),
40       Subtarget(tm.getSubtarget<SparcSubtarget>()),
41       TM(tm) {
42   }
43
44   SDNode *Select(SDNode *N) override;
45
46   // Complex Pattern Selectors.
47   bool SelectADDRrr(SDValue N, SDValue &R1, SDValue &R2);
48   bool SelectADDRri(SDValue N, SDValue &Base, SDValue &Offset);
49
50   /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
51   /// inline asm expressions.
52   bool SelectInlineAsmMemoryOperand(const SDValue &Op,
53                                     char ConstraintCode,
54                                     std::vector<SDValue> &OutOps) override;
55
56   const char *getPassName() const override {
57     return "SPARC DAG->DAG Pattern Instruction Selection";
58   }
59
60   // Include the pieces autogenerated from the target description.
61 #include "SparcGenDAGISel.inc"
62
63 private:
64   SDNode* getGlobalBaseReg();
65 };
66 }  // end anonymous namespace
67
68 SDNode* SparcDAGToDAGISel::getGlobalBaseReg() {
69   unsigned GlobalBaseReg =
70       TM.getSubtargetImpl()->getInstrInfo()->getGlobalBaseReg(MF);
71   return CurDAG->getRegister(GlobalBaseReg,
72                              getTargetLowering()->getPointerTy()).getNode();
73 }
74
75 bool SparcDAGToDAGISel::SelectADDRri(SDValue Addr,
76                                      SDValue &Base, SDValue &Offset) {
77   if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
78     Base = CurDAG->getTargetFrameIndex(FIN->getIndex(),
79                                        getTargetLowering()->getPointerTy());
80     Offset = CurDAG->getTargetConstant(0, MVT::i32);
81     return true;
82   }
83   if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
84       Addr.getOpcode() == ISD::TargetGlobalAddress ||
85       Addr.getOpcode() == ISD::TargetGlobalTLSAddress)
86     return false;  // direct calls.
87
88   if (Addr.getOpcode() == ISD::ADD) {
89     if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
90       if (isInt<13>(CN->getSExtValue())) {
91         if (FrameIndexSDNode *FIN =
92                 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
93           // Constant offset from frame ref.
94           Base = CurDAG->getTargetFrameIndex(FIN->getIndex(),
95                                            getTargetLowering()->getPointerTy());
96         } else {
97           Base = Addr.getOperand(0);
98         }
99         Offset = CurDAG->getTargetConstant(CN->getZExtValue(), MVT::i32);
100         return true;
101       }
102     }
103     if (Addr.getOperand(0).getOpcode() == SPISD::Lo) {
104       Base = Addr.getOperand(1);
105       Offset = Addr.getOperand(0).getOperand(0);
106       return true;
107     }
108     if (Addr.getOperand(1).getOpcode() == SPISD::Lo) {
109       Base = Addr.getOperand(0);
110       Offset = Addr.getOperand(1).getOperand(0);
111       return true;
112     }
113   }
114   Base = Addr;
115   Offset = CurDAG->getTargetConstant(0, MVT::i32);
116   return true;
117 }
118
119 bool SparcDAGToDAGISel::SelectADDRrr(SDValue Addr, SDValue &R1, SDValue &R2) {
120   if (Addr.getOpcode() == ISD::FrameIndex) return false;
121   if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
122       Addr.getOpcode() == ISD::TargetGlobalAddress ||
123       Addr.getOpcode() == ISD::TargetGlobalTLSAddress)
124     return false;  // direct calls.
125
126   if (Addr.getOpcode() == ISD::ADD) {
127     if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
128       if (isInt<13>(CN->getSExtValue()))
129         return false;  // Let the reg+imm pattern catch this!
130     if (Addr.getOperand(0).getOpcode() == SPISD::Lo ||
131         Addr.getOperand(1).getOpcode() == SPISD::Lo)
132       return false;  // Let the reg+imm pattern catch this!
133     R1 = Addr.getOperand(0);
134     R2 = Addr.getOperand(1);
135     return true;
136   }
137
138   R1 = Addr;
139   R2 = CurDAG->getRegister(SP::G0, getTargetLowering()->getPointerTy());
140   return true;
141 }
142
143 SDNode *SparcDAGToDAGISel::Select(SDNode *N) {
144   SDLoc dl(N);
145   if (N->isMachineOpcode()) {
146     N->setNodeId(-1);
147     return nullptr;   // Already selected.
148   }
149
150   switch (N->getOpcode()) {
151   default: break;
152   case SPISD::GLOBAL_BASE_REG:
153     return getGlobalBaseReg();
154
155   case ISD::SDIV:
156   case ISD::UDIV: {
157     // sdivx / udivx handle 64-bit divides.
158     if (N->getValueType(0) == MVT::i64)
159       break;
160     // FIXME: should use a custom expander to expose the SRA to the dag.
161     SDValue DivLHS = N->getOperand(0);
162     SDValue DivRHS = N->getOperand(1);
163
164     // Set the Y register to the high-part.
165     SDValue TopPart;
166     if (N->getOpcode() == ISD::SDIV) {
167       TopPart = SDValue(CurDAG->getMachineNode(SP::SRAri, dl, MVT::i32, DivLHS,
168                                    CurDAG->getTargetConstant(31, MVT::i32)), 0);
169     } else {
170       TopPart = CurDAG->getRegister(SP::G0, MVT::i32);
171     }
172     TopPart = SDValue(CurDAG->getMachineNode(SP::WRYrr, dl, MVT::Glue, TopPart,
173                                      CurDAG->getRegister(SP::G0, MVT::i32)), 0);
174
175     // FIXME: Handle div by immediate.
176     unsigned Opcode = N->getOpcode() == ISD::SDIV ? SP::SDIVrr : SP::UDIVrr;
177     return CurDAG->SelectNodeTo(N, Opcode, MVT::i32, DivLHS, DivRHS,
178                                 TopPart);
179   }
180   case ISD::MULHU:
181   case ISD::MULHS: {
182     // FIXME: Handle mul by immediate.
183     SDValue MulLHS = N->getOperand(0);
184     SDValue MulRHS = N->getOperand(1);
185     unsigned Opcode = N->getOpcode() == ISD::MULHU ? SP::UMULrr : SP::SMULrr;
186     SDNode *Mul = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::Glue,
187                                          MulLHS, MulRHS);
188     // The high part is in the Y register.
189     return CurDAG->SelectNodeTo(N, SP::RDY, MVT::i32, SDValue(Mul, 1));
190   }
191   }
192
193   return SelectCode(N);
194 }
195
196
197 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
198 /// inline asm expressions.
199 bool
200 SparcDAGToDAGISel::SelectInlineAsmMemoryOperand(const SDValue &Op,
201                                                 char ConstraintCode,
202                                                 std::vector<SDValue> &OutOps) {
203   SDValue Op0, Op1;
204   switch (ConstraintCode) {
205   default: return true;
206   case 'm':   // memory
207    if (!SelectADDRrr(Op, Op0, Op1))
208      SelectADDRri(Op, Op0, Op1);
209    break;
210   }
211
212   OutOps.push_back(Op0);
213   OutOps.push_back(Op1);
214   return false;
215 }
216
217 /// createSparcISelDag - This pass converts a legalized DAG into a
218 /// SPARC-specific DAG, ready for instruction scheduling.
219 ///
220 FunctionPass *llvm::createSparcISelDag(SparcTargetMachine &TM) {
221   return new SparcDAGToDAGISel(TM);
222 }