blob: 698923e3c9e083cd5617b087c2c95bf5dc011ada [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
Dan Gohmanf17a25c2007-07-18 16:29:46 +000014#include "SparcTargetMachine.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000015#include "llvm/Intrinsics.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000016#include "llvm/CodeGen/SelectionDAGISel.h"
Chris Lattner93c741a2008-02-03 05:43:57 +000017#include "llvm/Support/Compiler.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018#include "llvm/Support/Debug.h"
Edwin Török4d9756a2009-07-08 20:53:28 +000019#include "llvm/Support/ErrorHandling.h"
20#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021using namespace llvm;
22
23//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024// Instruction Selector Implementation
25//===----------------------------------------------------------------------===//
26
27//===--------------------------------------------------------------------===//
28/// SparcDAGToDAGISel - SPARC specific code to select SPARC machine
29/// instructions for SelectionDAG operations.
30///
31namespace {
32class SparcDAGToDAGISel : public SelectionDAGISel {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033 /// 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;
Chris Lattner49102de2009-09-15 17:46:24 +000036 SparcTargetMachine& TM;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037public:
Chris Lattner49102de2009-09-15 17:46:24 +000038 explicit SparcDAGToDAGISel(SparcTargetMachine &tm)
39 : SelectionDAGISel(tm),
40 Subtarget(tm.getSubtarget<SparcSubtarget>()),
41 TM(tm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042 }
43
Dan Gohman5f082a72010-01-05 01:24:18 +000044 SDNode *Select(SDNode *N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045
46 // Complex Pattern Selectors.
Dan Gohman5f082a72010-01-05 01:24:18 +000047 bool SelectADDRrr(SDNode *Op, SDValue N, SDValue &R1, SDValue &R2);
48 bool SelectADDRri(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman8181bd12008-07-27 21:46:04 +000049 SDValue &Offset);
Anton Korobeynikov05b89552008-10-10 10:14:15 +000050
Anton Korobeynikov984a5172008-10-10 10:14:47 +000051 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
52 /// inline asm expressions.
53 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
54 char ConstraintCode,
55 std::vector<SDValue> &OutOps);
56
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057 virtual const char *getPassName() const {
58 return "SPARC DAG->DAG Pattern Instruction Selection";
Anton Korobeynikov05b89552008-10-10 10:14:15 +000059 }
60
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061 // Include the pieces autogenerated from the target description.
62#include "SparcGenDAGISel.inc"
Chris Lattner49102de2009-09-15 17:46:24 +000063
64private:
65 SDNode* getGlobalBaseReg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066};
67} // end anonymous namespace
68
Chris Lattner49102de2009-09-15 17:46:24 +000069SDNode* SparcDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner49102de2009-09-15 17:46:24 +000070 unsigned GlobalBaseReg = TM.getInstrInfo()->getGlobalBaseReg(MF);
71 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
72}
73
Dan Gohman5f082a72010-01-05 01:24:18 +000074bool SparcDAGToDAGISel::SelectADDRri(SDNode *Op, SDValue Addr,
Dan Gohman8181bd12008-07-27 21:46:04 +000075 SDValue &Base, SDValue &Offset) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +000077 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
78 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079 return true;
80 }
Bill Wendlingfef06052008-09-16 21:48:12 +000081 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082 Addr.getOpcode() == ISD::TargetGlobalAddress)
83 return false; // direct calls.
Anton Korobeynikov05b89552008-10-10 10:14:15 +000084
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085 if (Addr.getOpcode() == ISD::ADD) {
86 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
87 if (Predicate_simm13(CN)) {
Anton Korobeynikov05b89552008-10-10 10:14:15 +000088 if (FrameIndexSDNode *FIN =
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
90 // Constant offset from frame ref.
Owen Anderson36e3a6e2009-08-11 20:47:22 +000091 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092 } else {
93 Base = Addr.getOperand(0);
94 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +000095 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 return true;
97 }
98 }
99 if (Addr.getOperand(0).getOpcode() == SPISD::Lo) {
100 Base = Addr.getOperand(1);
101 Offset = Addr.getOperand(0).getOperand(0);
102 return true;
103 }
104 if (Addr.getOperand(1).getOpcode() == SPISD::Lo) {
105 Base = Addr.getOperand(0);
106 Offset = Addr.getOperand(1).getOperand(0);
107 return true;
108 }
109 }
110 Base = Addr;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000111 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 return true;
113}
114
Dan Gohman5f082a72010-01-05 01:24:18 +0000115bool SparcDAGToDAGISel::SelectADDRrr(SDNode *Op, SDValue Addr,
Dan Gohman8181bd12008-07-27 21:46:04 +0000116 SDValue &R1, SDValue &R2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 if (Addr.getOpcode() == ISD::FrameIndex) return false;
Bill Wendlingfef06052008-09-16 21:48:12 +0000118 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 Addr.getOpcode() == ISD::TargetGlobalAddress)
120 return false; // direct calls.
Anton Korobeynikov05b89552008-10-10 10:14:15 +0000121
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 if (Addr.getOpcode() == ISD::ADD) {
123 if (isa<ConstantSDNode>(Addr.getOperand(1)) &&
Gabor Greif1c80d112008-08-28 21:40:38 +0000124 Predicate_simm13(Addr.getOperand(1).getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125 return false; // Let the reg+imm pattern catch this!
126 if (Addr.getOperand(0).getOpcode() == SPISD::Lo ||
127 Addr.getOperand(1).getOpcode() == SPISD::Lo)
128 return false; // Let the reg+imm pattern catch this!
129 R1 = Addr.getOperand(0);
130 R2 = Addr.getOperand(1);
131 return true;
132 }
133
134 R1 = Addr;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000135 R2 = CurDAG->getRegister(SP::G0, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136 return true;
137}
138
Dan Gohman5f082a72010-01-05 01:24:18 +0000139SDNode *SparcDAGToDAGISel::Select(SDNode *N) {
Dale Johannesen5d398a32009-02-06 19:16:40 +0000140 DebugLoc dl = N->getDebugLoc();
Dan Gohmanbd68c792008-07-17 19:10:17 +0000141 if (N->isMachineOpcode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 return NULL; // Already selected.
143
144 switch (N->getOpcode()) {
145 default: break;
Chris Lattner49102de2009-09-15 17:46:24 +0000146 case SPISD::GLOBAL_BASE_REG:
147 return getGlobalBaseReg();
148
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 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);
Anton Korobeynikov05b89552008-10-10 10:14:15 +0000154
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 // Set the Y register to the high-part.
Dan Gohman8181bd12008-07-27 21:46:04 +0000156 SDValue TopPart;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 if (N->getOpcode() == ISD::SDIV) {
Dan Gohman61fda0d2009-09-25 18:54:59 +0000158 TopPart = SDValue(CurDAG->getMachineNode(SP::SRAri, dl, MVT::i32, DivLHS,
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000159 CurDAG->getTargetConstant(31, MVT::i32)), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 } else {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000161 TopPart = CurDAG->getRegister(SP::G0, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 }
Dan Gohman61fda0d2009-09-25 18:54:59 +0000163 TopPart = SDValue(CurDAG->getMachineNode(SP::WRYrr, dl, MVT::Flag, TopPart,
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000164 CurDAG->getRegister(SP::G0, MVT::i32)), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165
166 // FIXME: Handle div by immediate.
167 unsigned Opcode = N->getOpcode() == ISD::SDIV ? SP::SDIVrr : SP::UDIVrr;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000168 return CurDAG->SelectNodeTo(N, Opcode, MVT::i32, DivLHS, DivRHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 TopPart);
Anton Korobeynikov05b89552008-10-10 10:14:15 +0000170 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 case ISD::MULHU:
172 case ISD::MULHS: {
173 // FIXME: Handle mul by immediate.
Dan Gohman8181bd12008-07-27 21:46:04 +0000174 SDValue MulLHS = N->getOperand(0);
175 SDValue MulRHS = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 unsigned Opcode = N->getOpcode() == ISD::MULHU ? SP::UMULrr : SP::SMULrr;
Dan Gohman61fda0d2009-09-25 18:54:59 +0000177 SDNode *Mul = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::Flag,
178 MulLHS, MulRHS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 // The high part is in the Y register.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000180 return CurDAG->SelectNodeTo(N, SP::RDY, MVT::i32, SDValue(Mul, 1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 return NULL;
182 }
183 }
Anton Korobeynikov05b89552008-10-10 10:14:15 +0000184
Dan Gohman5f082a72010-01-05 01:24:18 +0000185 return SelectCode(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186}
187
188
Anton Korobeynikov984a5172008-10-10 10:14:47 +0000189/// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
190/// inline asm expressions.
191bool
192SparcDAGToDAGISel::SelectInlineAsmMemoryOperand(const SDValue &Op,
193 char ConstraintCode,
194 std::vector<SDValue> &OutOps) {
195 SDValue Op0, Op1;
196 switch (ConstraintCode) {
197 default: return true;
198 case 'm': // memory
Dan Gohman5f082a72010-01-05 01:24:18 +0000199 if (!SelectADDRrr(Op.getNode(), Op, Op0, Op1))
200 SelectADDRri(Op.getNode(), Op, Op0, Op1);
Anton Korobeynikov984a5172008-10-10 10:14:47 +0000201 break;
202 }
203
204 OutOps.push_back(Op0);
205 OutOps.push_back(Op1);
206 return false;
207}
208
Anton Korobeynikov05b89552008-10-10 10:14:15 +0000209/// createSparcISelDag - This pass converts a legalized DAG into a
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210/// SPARC-specific DAG, ready for instruction scheduling.
211///
Dan Gohmanf2b29572008-10-03 16:55:19 +0000212FunctionPass *llvm::createSparcISelDag(SparcTargetMachine &TM) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213 return new SparcDAGToDAGISel(TM);
214}