blob: 66e143de3fed54f3ee2d1419fc1663a617989a51 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- SparcISelDAGToDAG.cpp - A dag to dag inst selector for Sparc ------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines an instruction selector for the SPARC target.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner36d23442008-03-17 03:21:36 +000014#include "SparcISelLowering.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000015#include "SparcTargetMachine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000016#include "llvm/Intrinsics.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017#include "llvm/CodeGen/SelectionDAGISel.h"
Chris Lattner93c741a2008-02-03 05:43:57 +000018#include "llvm/Support/Compiler.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020using namespace llvm;
21
22//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023// Instruction Selector Implementation
24//===----------------------------------------------------------------------===//
25
26//===--------------------------------------------------------------------===//
27/// SparcDAGToDAGISel - SPARC specific code to select SPARC machine
28/// instructions for SelectionDAG operations.
29///
30namespace {
31class SparcDAGToDAGISel : public SelectionDAGISel {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032 /// Subtarget - Keep a pointer to the Sparc Subtarget around so that we can
33 /// make the right decision when generating code for different targets.
34 const SparcSubtarget &Subtarget;
35public:
Dan Gohmanf2b29572008-10-03 16:55:19 +000036 explicit SparcDAGToDAGISel(SparcTargetMachine &TM)
37 : SelectionDAGISel(*TM.getTargetLowering()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038 Subtarget(TM.getSubtarget<SparcSubtarget>()) {
39 }
40
Dan Gohman8181bd12008-07-27 21:46:04 +000041 SDNode *Select(SDValue Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042
43 // Complex Pattern Selectors.
Dan Gohman8181bd12008-07-27 21:46:04 +000044 bool SelectADDRrr(SDValue Op, SDValue N, SDValue &R1, SDValue &R2);
45 bool SelectADDRri(SDValue Op, SDValue N, SDValue &Base,
46 SDValue &Offset);
Anton Korobeynikov05b89552008-10-10 10:14:15 +000047
Anton Korobeynikov984a5172008-10-10 10:14:47 +000048 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
49 /// inline asm expressions.
50 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
51 char ConstraintCode,
52 std::vector<SDValue> &OutOps);
53
Evan Cheng34fd4f32008-06-30 20:45:06 +000054 /// InstructionSelect - This callback is invoked by
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +000056 virtual void InstructionSelect();
Anton Korobeynikov05b89552008-10-10 10:14:15 +000057
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058 virtual const char *getPassName() const {
59 return "SPARC DAG->DAG Pattern Instruction Selection";
Anton Korobeynikov05b89552008-10-10 10:14:15 +000060 }
61
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062 // Include the pieces autogenerated from the target description.
63#include "SparcGenDAGISel.inc"
64};
65} // end anonymous namespace
66
Evan Cheng34fd4f32008-06-30 20:45:06 +000067/// InstructionSelect - This callback is invoked by
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +000069void SparcDAGToDAGISel::InstructionSelect() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070 DEBUG(BB->dump());
Anton Korobeynikov05b89552008-10-10 10:14:15 +000071
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 // Select target instructions for the DAG.
Dan Gohmanbd3f8822008-08-21 16:36:34 +000073 SelectRoot();
Dan Gohman14a66442008-08-23 02:25:05 +000074 CurDAG->RemoveDeadNodes();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075}
76
Dan Gohman8181bd12008-07-27 21:46:04 +000077bool SparcDAGToDAGISel::SelectADDRri(SDValue Op, SDValue Addr,
78 SDValue &Base, SDValue &Offset) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
80 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
81 Offset = CurDAG->getTargetConstant(0, MVT::i32);
82 return true;
83 }
Bill Wendlingfef06052008-09-16 21:48:12 +000084 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085 Addr.getOpcode() == ISD::TargetGlobalAddress)
86 return false; // direct calls.
Anton Korobeynikov05b89552008-10-10 10:14:15 +000087
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088 if (Addr.getOpcode() == ISD::ADD) {
89 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
90 if (Predicate_simm13(CN)) {
Anton Korobeynikov05b89552008-10-10 10:14:15 +000091 if (FrameIndexSDNode *FIN =
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
93 // Constant offset from frame ref.
94 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
95 } else {
96 Base = Addr.getOperand(0);
97 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +000098 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 return true;
100 }
101 }
102 if (Addr.getOperand(0).getOpcode() == SPISD::Lo) {
103 Base = Addr.getOperand(1);
104 Offset = Addr.getOperand(0).getOperand(0);
105 return true;
106 }
107 if (Addr.getOperand(1).getOpcode() == SPISD::Lo) {
108 Base = Addr.getOperand(0);
109 Offset = Addr.getOperand(1).getOperand(0);
110 return true;
111 }
112 }
113 Base = Addr;
114 Offset = CurDAG->getTargetConstant(0, MVT::i32);
115 return true;
116}
117
Dan Gohman8181bd12008-07-27 21:46:04 +0000118bool SparcDAGToDAGISel::SelectADDRrr(SDValue Op, SDValue Addr,
119 SDValue &R1, SDValue &R2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 if (Addr.getOpcode() == ISD::FrameIndex) return false;
Bill Wendlingfef06052008-09-16 21:48:12 +0000121 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 Addr.getOpcode() == ISD::TargetGlobalAddress)
123 return false; // direct calls.
Anton Korobeynikov05b89552008-10-10 10:14:15 +0000124
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125 if (Addr.getOpcode() == ISD::ADD) {
126 if (isa<ConstantSDNode>(Addr.getOperand(1)) &&
Gabor Greif1c80d112008-08-28 21:40:38 +0000127 Predicate_simm13(Addr.getOperand(1).getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128 return false; // Let the reg+imm pattern catch this!
129 if (Addr.getOperand(0).getOpcode() == SPISD::Lo ||
130 Addr.getOperand(1).getOpcode() == SPISD::Lo)
131 return false; // Let the reg+imm pattern catch this!
132 R1 = Addr.getOperand(0);
133 R2 = Addr.getOperand(1);
134 return true;
135 }
136
137 R1 = Addr;
138 R2 = CurDAG->getRegister(SP::G0, MVT::i32);
139 return true;
140}
141
Dan Gohman8181bd12008-07-27 21:46:04 +0000142SDNode *SparcDAGToDAGISel::Select(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000143 SDNode *N = Op.getNode();
Dan Gohmanbd68c792008-07-17 19:10:17 +0000144 if (N->isMachineOpcode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 return NULL; // Already selected.
146
147 switch (N->getOpcode()) {
148 default: break;
149 case ISD::SDIV:
150 case ISD::UDIV: {
151 // FIXME: should use a custom expander to expose the SRA to the dag.
Dan Gohman8181bd12008-07-27 21:46:04 +0000152 SDValue DivLHS = N->getOperand(0);
153 SDValue DivRHS = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 AddToISelQueue(DivLHS);
155 AddToISelQueue(DivRHS);
Anton Korobeynikov05b89552008-10-10 10:14:15 +0000156
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 // Set the Y register to the high-part.
Dan Gohman8181bd12008-07-27 21:46:04 +0000158 SDValue TopPart;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 if (N->getOpcode() == ISD::SDIV) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000160 TopPart = SDValue(CurDAG->getTargetNode(SP::SRAri, MVT::i32, DivLHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161 CurDAG->getTargetConstant(31, MVT::i32)), 0);
162 } else {
163 TopPart = CurDAG->getRegister(SP::G0, MVT::i32);
164 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000165 TopPart = SDValue(CurDAG->getTargetNode(SP::WRYrr, MVT::Flag, TopPart,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 CurDAG->getRegister(SP::G0, MVT::i32)), 0);
167
168 // FIXME: Handle div by immediate.
169 unsigned Opcode = N->getOpcode() == ISD::SDIV ? SP::SDIVrr : SP::UDIVrr;
170 return CurDAG->SelectNodeTo(N, Opcode, MVT::i32, DivLHS, DivRHS,
171 TopPart);
Anton Korobeynikov05b89552008-10-10 10:14:15 +0000172 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 case ISD::MULHU:
174 case ISD::MULHS: {
175 // FIXME: Handle mul by immediate.
Dan Gohman8181bd12008-07-27 21:46:04 +0000176 SDValue MulLHS = N->getOperand(0);
177 SDValue MulRHS = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 AddToISelQueue(MulLHS);
179 AddToISelQueue(MulRHS);
180 unsigned Opcode = N->getOpcode() == ISD::MULHU ? SP::UMULrr : SP::SMULrr;
181 SDNode *Mul = CurDAG->getTargetNode(Opcode, MVT::i32, MVT::Flag,
182 MulLHS, MulRHS);
183 // The high part is in the Y register.
Dan Gohman8181bd12008-07-27 21:46:04 +0000184 return CurDAG->SelectNodeTo(N, SP::RDY, MVT::i32, SDValue(Mul, 1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185 return NULL;
186 }
187 }
Anton Korobeynikov05b89552008-10-10 10:14:15 +0000188
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000189 return SelectCode(Op);
190}
191
192
Anton Korobeynikov984a5172008-10-10 10:14:47 +0000193/// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
194/// inline asm expressions.
195bool
196SparcDAGToDAGISel::SelectInlineAsmMemoryOperand(const SDValue &Op,
197 char ConstraintCode,
198 std::vector<SDValue> &OutOps) {
199 SDValue Op0, Op1;
200 switch (ConstraintCode) {
201 default: return true;
202 case 'm': // memory
203 if (!SelectADDRrr(Op, Op, Op0, Op1))
204 SelectADDRri(Op, Op, Op0, Op1);
205 break;
206 }
207
208 OutOps.push_back(Op0);
209 OutOps.push_back(Op1);
210 return false;
211}
212
Anton Korobeynikov05b89552008-10-10 10:14:15 +0000213/// createSparcISelDag - This pass converts a legalized DAG into a
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214/// SPARC-specific DAG, ready for instruction scheduling.
215///
Dan Gohmanf2b29572008-10-03 16:55:19 +0000216FunctionPass *llvm::createSparcISelDag(SparcTargetMachine &TM) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 return new SparcDAGToDAGISel(TM);
218}