blob: e1b9b59b4095fcb8887e55077382a4e5b05241fb [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"
Edwin Török4d9756a2009-07-08 20:53:28 +000020#include "llvm/Support/ErrorHandling.h"
21#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022using namespace llvm;
23
24//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025// Instruction Selector Implementation
26//===----------------------------------------------------------------------===//
27
28//===--------------------------------------------------------------------===//
29/// SparcDAGToDAGISel - SPARC specific code to select SPARC machine
30/// instructions for SelectionDAG operations.
31///
32namespace {
33class SparcDAGToDAGISel : public SelectionDAGISel {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034 /// 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;
37public:
Dan Gohmanf2b29572008-10-03 16:55:19 +000038 explicit SparcDAGToDAGISel(SparcTargetMachine &TM)
Dan Gohman96eb47a2009-01-15 19:20:50 +000039 : SelectionDAGISel(TM),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040 Subtarget(TM.getSubtarget<SparcSubtarget>()) {
41 }
42
Dan Gohman8181bd12008-07-27 21:46:04 +000043 SDNode *Select(SDValue Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044
45 // Complex Pattern Selectors.
Dan Gohman8181bd12008-07-27 21:46:04 +000046 bool SelectADDRrr(SDValue Op, SDValue N, SDValue &R1, SDValue &R2);
47 bool SelectADDRri(SDValue Op, SDValue N, SDValue &Base,
48 SDValue &Offset);
Anton Korobeynikov05b89552008-10-10 10:14:15 +000049
Anton Korobeynikov984a5172008-10-10 10:14:47 +000050 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
51 /// inline asm expressions.
52 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
53 char ConstraintCode,
54 std::vector<SDValue> &OutOps);
55
Evan Cheng34fd4f32008-06-30 20:45:06 +000056 /// InstructionSelect - This callback is invoked by
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +000058 virtual void InstructionSelect();
Anton Korobeynikov05b89552008-10-10 10:14:15 +000059
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060 virtual const char *getPassName() const {
61 return "SPARC DAG->DAG Pattern Instruction Selection";
Anton Korobeynikov05b89552008-10-10 10:14:15 +000062 }
63
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064 // Include the pieces autogenerated from the target description.
65#include "SparcGenDAGISel.inc"
66};
67} // end anonymous namespace
68
Evan Cheng34fd4f32008-06-30 20:45:06 +000069/// InstructionSelect - This callback is invoked by
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +000071void SparcDAGToDAGISel::InstructionSelect() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 DEBUG(BB->dump());
Anton Korobeynikov05b89552008-10-10 10:14:15 +000073
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074 // Select target instructions for the DAG.
David Greene932618b2008-10-27 21:56:29 +000075 SelectRoot(*CurDAG);
Dan Gohman14a66442008-08-23 02:25:05 +000076 CurDAG->RemoveDeadNodes();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077}
78
Dan Gohman8181bd12008-07-27 21:46:04 +000079bool SparcDAGToDAGISel::SelectADDRri(SDValue Op, SDValue Addr,
80 SDValue &Base, SDValue &Offset) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
82 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
83 Offset = CurDAG->getTargetConstant(0, MVT::i32);
84 return true;
85 }
Bill Wendlingfef06052008-09-16 21:48:12 +000086 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087 Addr.getOpcode() == ISD::TargetGlobalAddress)
88 return false; // direct calls.
Anton Korobeynikov05b89552008-10-10 10:14:15 +000089
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090 if (Addr.getOpcode() == ISD::ADD) {
91 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
92 if (Predicate_simm13(CN)) {
Anton Korobeynikov05b89552008-10-10 10:14:15 +000093 if (FrameIndexSDNode *FIN =
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
95 // Constant offset from frame ref.
96 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
97 } else {
98 Base = Addr.getOperand(0);
99 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000100 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 return true;
102 }
103 }
104 if (Addr.getOperand(0).getOpcode() == SPISD::Lo) {
105 Base = Addr.getOperand(1);
106 Offset = Addr.getOperand(0).getOperand(0);
107 return true;
108 }
109 if (Addr.getOperand(1).getOpcode() == SPISD::Lo) {
110 Base = Addr.getOperand(0);
111 Offset = Addr.getOperand(1).getOperand(0);
112 return true;
113 }
114 }
115 Base = Addr;
116 Offset = CurDAG->getTargetConstant(0, MVT::i32);
117 return true;
118}
119
Dan Gohman8181bd12008-07-27 21:46:04 +0000120bool SparcDAGToDAGISel::SelectADDRrr(SDValue Op, SDValue Addr,
121 SDValue &R1, SDValue &R2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 if (Addr.getOpcode() == ISD::FrameIndex) return false;
Bill Wendlingfef06052008-09-16 21:48:12 +0000123 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 Addr.getOpcode() == ISD::TargetGlobalAddress)
125 return false; // direct calls.
Anton Korobeynikov05b89552008-10-10 10:14:15 +0000126
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127 if (Addr.getOpcode() == ISD::ADD) {
128 if (isa<ConstantSDNode>(Addr.getOperand(1)) &&
Gabor Greif1c80d112008-08-28 21:40:38 +0000129 Predicate_simm13(Addr.getOperand(1).getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 return false; // Let the reg+imm pattern catch this!
131 if (Addr.getOperand(0).getOpcode() == SPISD::Lo ||
132 Addr.getOperand(1).getOpcode() == SPISD::Lo)
133 return false; // Let the reg+imm pattern catch this!
134 R1 = Addr.getOperand(0);
135 R2 = Addr.getOperand(1);
136 return true;
137 }
138
139 R1 = Addr;
140 R2 = CurDAG->getRegister(SP::G0, MVT::i32);
141 return true;
142}
143
Dan Gohman8181bd12008-07-27 21:46:04 +0000144SDNode *SparcDAGToDAGISel::Select(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000145 SDNode *N = Op.getNode();
Dale Johannesen5d398a32009-02-06 19:16:40 +0000146 DebugLoc dl = N->getDebugLoc();
Dan Gohmanbd68c792008-07-17 19:10:17 +0000147 if (N->isMachineOpcode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148 return NULL; // Already selected.
149
150 switch (N->getOpcode()) {
151 default: break;
152 case ISD::SDIV:
153 case ISD::UDIV: {
154 // FIXME: should use a custom expander to expose the SRA to the dag.
Dan Gohman8181bd12008-07-27 21:46:04 +0000155 SDValue DivLHS = N->getOperand(0);
156 SDValue DivRHS = N->getOperand(1);
Anton Korobeynikov05b89552008-10-10 10:14:15 +0000157
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 // Set the Y register to the high-part.
Dan Gohman8181bd12008-07-27 21:46:04 +0000159 SDValue TopPart;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 if (N->getOpcode() == ISD::SDIV) {
Dale Johannesen5d398a32009-02-06 19:16:40 +0000161 TopPart = SDValue(CurDAG->getTargetNode(SP::SRAri, dl, MVT::i32, DivLHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 CurDAG->getTargetConstant(31, MVT::i32)), 0);
163 } else {
164 TopPart = CurDAG->getRegister(SP::G0, MVT::i32);
165 }
Dale Johannesen5d398a32009-02-06 19:16:40 +0000166 TopPart = SDValue(CurDAG->getTargetNode(SP::WRYrr, dl, MVT::Flag, TopPart,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 CurDAG->getRegister(SP::G0, MVT::i32)), 0);
168
169 // FIXME: Handle div by immediate.
170 unsigned Opcode = N->getOpcode() == ISD::SDIV ? SP::SDIVrr : SP::UDIVrr;
171 return CurDAG->SelectNodeTo(N, Opcode, MVT::i32, DivLHS, DivRHS,
172 TopPart);
Anton Korobeynikov05b89552008-10-10 10:14:15 +0000173 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 case ISD::MULHU:
175 case ISD::MULHS: {
176 // FIXME: Handle mul by immediate.
Dan Gohman8181bd12008-07-27 21:46:04 +0000177 SDValue MulLHS = N->getOperand(0);
178 SDValue MulRHS = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 unsigned Opcode = N->getOpcode() == ISD::MULHU ? SP::UMULrr : SP::SMULrr;
Dale Johannesen5d398a32009-02-06 19:16:40 +0000180 SDNode *Mul = CurDAG->getTargetNode(Opcode, dl, MVT::i32, MVT::Flag,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 MulLHS, MulRHS);
182 // The high part is in the Y register.
Dan Gohman8181bd12008-07-27 21:46:04 +0000183 return CurDAG->SelectNodeTo(N, SP::RDY, MVT::i32, SDValue(Mul, 1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000184 return NULL;
185 }
186 }
Anton Korobeynikov05b89552008-10-10 10:14:15 +0000187
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 return SelectCode(Op);
189}
190
191
Anton Korobeynikov984a5172008-10-10 10:14:47 +0000192/// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
193/// inline asm expressions.
194bool
195SparcDAGToDAGISel::SelectInlineAsmMemoryOperand(const SDValue &Op,
196 char ConstraintCode,
197 std::vector<SDValue> &OutOps) {
198 SDValue Op0, Op1;
199 switch (ConstraintCode) {
200 default: return true;
201 case 'm': // memory
202 if (!SelectADDRrr(Op, Op, Op0, Op1))
203 SelectADDRri(Op, Op, Op0, Op1);
204 break;
205 }
206
207 OutOps.push_back(Op0);
208 OutOps.push_back(Op1);
209 return false;
210}
211
Anton Korobeynikov05b89552008-10-10 10:14:15 +0000212/// createSparcISelDag - This pass converts a legalized DAG into a
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213/// SPARC-specific DAG, ready for instruction scheduling.
214///
Dan Gohmanf2b29572008-10-03 16:55:19 +0000215FunctionPass *llvm::createSparcISelDag(SparcTargetMachine &TM) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 return new SparcDAGToDAGISel(TM);
217}