blob: e1b32998b23fe781025dd2cb5ebc148da8349228 [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;
Chris Lattner49102de2009-09-15 17:46:24 +000037 SparcTargetMachine& TM;
38 MachineBasicBlock *CurBB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039public:
Chris Lattner49102de2009-09-15 17:46:24 +000040 explicit SparcDAGToDAGISel(SparcTargetMachine &tm)
41 : SelectionDAGISel(tm),
42 Subtarget(tm.getSubtarget<SparcSubtarget>()),
43 TM(tm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044 }
45
Dan Gohman5f082a72010-01-05 01:24:18 +000046 SDNode *Select(SDNode *N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047
48 // Complex Pattern Selectors.
Dan Gohman5f082a72010-01-05 01:24:18 +000049 bool SelectADDRrr(SDNode *Op, SDValue N, SDValue &R1, SDValue &R2);
50 bool SelectADDRri(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman8181bd12008-07-27 21:46:04 +000051 SDValue &Offset);
Anton Korobeynikov05b89552008-10-10 10:14:15 +000052
Anton Korobeynikov984a5172008-10-10 10:14:47 +000053 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
54 /// inline asm expressions.
55 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
56 char ConstraintCode,
57 std::vector<SDValue> &OutOps);
58
Evan Cheng34fd4f32008-06-30 20:45:06 +000059 /// InstructionSelect - This callback is invoked by
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +000061 virtual void InstructionSelect();
Anton Korobeynikov05b89552008-10-10 10:14:15 +000062
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063 virtual const char *getPassName() const {
64 return "SPARC DAG->DAG Pattern Instruction Selection";
Anton Korobeynikov05b89552008-10-10 10:14:15 +000065 }
66
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067 // Include the pieces autogenerated from the target description.
68#include "SparcGenDAGISel.inc"
Chris Lattner49102de2009-09-15 17:46:24 +000069
70private:
71 SDNode* getGlobalBaseReg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072};
73} // end anonymous namespace
74
Evan Cheng34fd4f32008-06-30 20:45:06 +000075/// InstructionSelect - This callback is invoked by
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +000077void SparcDAGToDAGISel::InstructionSelect() {
Chris Lattner49102de2009-09-15 17:46:24 +000078 CurBB = BB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079 // Select target instructions for the DAG.
David Greene932618b2008-10-27 21:56:29 +000080 SelectRoot(*CurDAG);
Dan Gohman14a66442008-08-23 02:25:05 +000081 CurDAG->RemoveDeadNodes();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082}
83
Chris Lattner49102de2009-09-15 17:46:24 +000084SDNode* SparcDAGToDAGISel::getGlobalBaseReg() {
85 MachineFunction *MF = CurBB->getParent();
86 unsigned GlobalBaseReg = TM.getInstrInfo()->getGlobalBaseReg(MF);
87 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
88}
89
Dan Gohman5f082a72010-01-05 01:24:18 +000090bool SparcDAGToDAGISel::SelectADDRri(SDNode *Op, SDValue Addr,
Dan Gohman8181bd12008-07-27 21:46:04 +000091 SDValue &Base, SDValue &Offset) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +000093 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
94 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095 return true;
96 }
Bill Wendlingfef06052008-09-16 21:48:12 +000097 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098 Addr.getOpcode() == ISD::TargetGlobalAddress)
99 return false; // direct calls.
Anton Korobeynikov05b89552008-10-10 10:14:15 +0000100
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 if (Addr.getOpcode() == ISD::ADD) {
102 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
103 if (Predicate_simm13(CN)) {
Anton Korobeynikov05b89552008-10-10 10:14:15 +0000104 if (FrameIndexSDNode *FIN =
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
106 // Constant offset from frame ref.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000107 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108 } else {
109 Base = Addr.getOperand(0);
110 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000111 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 return true;
113 }
114 }
115 if (Addr.getOperand(0).getOpcode() == SPISD::Lo) {
116 Base = Addr.getOperand(1);
117 Offset = Addr.getOperand(0).getOperand(0);
118 return true;
119 }
120 if (Addr.getOperand(1).getOpcode() == SPISD::Lo) {
121 Base = Addr.getOperand(0);
122 Offset = Addr.getOperand(1).getOperand(0);
123 return true;
124 }
125 }
126 Base = Addr;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000127 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128 return true;
129}
130
Dan Gohman5f082a72010-01-05 01:24:18 +0000131bool SparcDAGToDAGISel::SelectADDRrr(SDNode *Op, SDValue Addr,
Dan Gohman8181bd12008-07-27 21:46:04 +0000132 SDValue &R1, SDValue &R2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133 if (Addr.getOpcode() == ISD::FrameIndex) return false;
Bill Wendlingfef06052008-09-16 21:48:12 +0000134 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 Addr.getOpcode() == ISD::TargetGlobalAddress)
136 return false; // direct calls.
Anton Korobeynikov05b89552008-10-10 10:14:15 +0000137
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 if (Addr.getOpcode() == ISD::ADD) {
139 if (isa<ConstantSDNode>(Addr.getOperand(1)) &&
Gabor Greif1c80d112008-08-28 21:40:38 +0000140 Predicate_simm13(Addr.getOperand(1).getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000141 return false; // Let the reg+imm pattern catch this!
142 if (Addr.getOperand(0).getOpcode() == SPISD::Lo ||
143 Addr.getOperand(1).getOpcode() == SPISD::Lo)
144 return false; // Let the reg+imm pattern catch this!
145 R1 = Addr.getOperand(0);
146 R2 = Addr.getOperand(1);
147 return true;
148 }
149
150 R1 = Addr;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000151 R2 = CurDAG->getRegister(SP::G0, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152 return true;
153}
154
Dan Gohman5f082a72010-01-05 01:24:18 +0000155SDNode *SparcDAGToDAGISel::Select(SDNode *N) {
Dale Johannesen5d398a32009-02-06 19:16:40 +0000156 DebugLoc dl = N->getDebugLoc();
Dan Gohmanbd68c792008-07-17 19:10:17 +0000157 if (N->isMachineOpcode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 return NULL; // Already selected.
159
160 switch (N->getOpcode()) {
161 default: break;
Chris Lattner49102de2009-09-15 17:46:24 +0000162 case SPISD::GLOBAL_BASE_REG:
163 return getGlobalBaseReg();
164
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 case ISD::SDIV:
166 case ISD::UDIV: {
167 // FIXME: should use a custom expander to expose the SRA to the dag.
Dan Gohman8181bd12008-07-27 21:46:04 +0000168 SDValue DivLHS = N->getOperand(0);
169 SDValue DivRHS = N->getOperand(1);
Anton Korobeynikov05b89552008-10-10 10:14:15 +0000170
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 // Set the Y register to the high-part.
Dan Gohman8181bd12008-07-27 21:46:04 +0000172 SDValue TopPart;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 if (N->getOpcode() == ISD::SDIV) {
Dan Gohman61fda0d2009-09-25 18:54:59 +0000174 TopPart = SDValue(CurDAG->getMachineNode(SP::SRAri, dl, MVT::i32, DivLHS,
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000175 CurDAG->getTargetConstant(31, MVT::i32)), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 } else {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000177 TopPart = CurDAG->getRegister(SP::G0, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 }
Dan Gohman61fda0d2009-09-25 18:54:59 +0000179 TopPart = SDValue(CurDAG->getMachineNode(SP::WRYrr, dl, MVT::Flag, TopPart,
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000180 CurDAG->getRegister(SP::G0, MVT::i32)), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181
182 // FIXME: Handle div by immediate.
183 unsigned Opcode = N->getOpcode() == ISD::SDIV ? SP::SDIVrr : SP::UDIVrr;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000184 return CurDAG->SelectNodeTo(N, Opcode, MVT::i32, DivLHS, DivRHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185 TopPart);
Anton Korobeynikov05b89552008-10-10 10:14:15 +0000186 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 case ISD::MULHU:
188 case ISD::MULHS: {
189 // FIXME: Handle mul by immediate.
Dan Gohman8181bd12008-07-27 21:46:04 +0000190 SDValue MulLHS = N->getOperand(0);
191 SDValue MulRHS = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 unsigned Opcode = N->getOpcode() == ISD::MULHU ? SP::UMULrr : SP::SMULrr;
Dan Gohman61fda0d2009-09-25 18:54:59 +0000193 SDNode *Mul = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::Flag,
194 MulLHS, MulRHS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 // The high part is in the Y register.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000196 return CurDAG->SelectNodeTo(N, SP::RDY, MVT::i32, SDValue(Mul, 1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 return NULL;
198 }
199 }
Anton Korobeynikov05b89552008-10-10 10:14:15 +0000200
Dan Gohman5f082a72010-01-05 01:24:18 +0000201 return SelectCode(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202}
203
204
Anton Korobeynikov984a5172008-10-10 10:14:47 +0000205/// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
206/// inline asm expressions.
207bool
208SparcDAGToDAGISel::SelectInlineAsmMemoryOperand(const SDValue &Op,
209 char ConstraintCode,
210 std::vector<SDValue> &OutOps) {
211 SDValue Op0, Op1;
212 switch (ConstraintCode) {
213 default: return true;
214 case 'm': // memory
Dan Gohman5f082a72010-01-05 01:24:18 +0000215 if (!SelectADDRrr(Op.getNode(), Op, Op0, Op1))
216 SelectADDRri(Op.getNode(), Op, Op0, Op1);
Anton Korobeynikov984a5172008-10-10 10:14:47 +0000217 break;
218 }
219
220 OutOps.push_back(Op0);
221 OutOps.push_back(Op1);
222 return false;
223}
224
Anton Korobeynikov05b89552008-10-10 10:14:15 +0000225/// createSparcISelDag - This pass converts a legalized DAG into a
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226/// SPARC-specific DAG, ready for instruction scheduling.
227///
Dan Gohmanf2b29572008-10-03 16:55:19 +0000228FunctionPass *llvm::createSparcISelDag(SparcTargetMachine &TM) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 return new SparcDAGToDAGISel(TM);
230}