blob: a7d1805e2a36b9357b0de8ae2bb32408368973a8 [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;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038public:
Chris Lattner49102de2009-09-15 17:46:24 +000039 explicit SparcDAGToDAGISel(SparcTargetMachine &tm)
40 : SelectionDAGISel(tm),
41 Subtarget(tm.getSubtarget<SparcSubtarget>()),
42 TM(tm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043 }
44
Dan Gohman5f082a72010-01-05 01:24:18 +000045 SDNode *Select(SDNode *N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046
47 // Complex Pattern Selectors.
Dan Gohman5f082a72010-01-05 01:24:18 +000048 bool SelectADDRrr(SDNode *Op, SDValue N, SDValue &R1, SDValue &R2);
49 bool SelectADDRri(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman8181bd12008-07-27 21:46:04 +000050 SDValue &Offset);
Anton Korobeynikov05b89552008-10-10 10:14:15 +000051
Anton Korobeynikov984a5172008-10-10 10:14:47 +000052 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
53 /// inline asm expressions.
54 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
55 char ConstraintCode,
56 std::vector<SDValue> &OutOps);
57
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"
Chris Lattner49102de2009-09-15 17:46:24 +000064
65private:
66 SDNode* getGlobalBaseReg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067};
68} // end anonymous namespace
69
Chris Lattner49102de2009-09-15 17:46:24 +000070SDNode* SparcDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner6411e3e2010-03-02 06:34:30 +000071 MachineFunction *MF = BB->getParent();
Chris Lattner49102de2009-09-15 17:46:24 +000072 unsigned GlobalBaseReg = TM.getInstrInfo()->getGlobalBaseReg(MF);
73 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
74}
75
Dan Gohman5f082a72010-01-05 01:24:18 +000076bool SparcDAGToDAGISel::SelectADDRri(SDNode *Op, SDValue Addr,
Dan Gohman8181bd12008-07-27 21:46:04 +000077 SDValue &Base, SDValue &Offset) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
Owen Anderson36e3a6e2009-08-11 20:47:22 +000079 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
80 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081 return true;
82 }
Bill Wendlingfef06052008-09-16 21:48:12 +000083 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084 Addr.getOpcode() == ISD::TargetGlobalAddress)
85 return false; // direct calls.
Anton Korobeynikov05b89552008-10-10 10:14:15 +000086
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087 if (Addr.getOpcode() == ISD::ADD) {
88 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
89 if (Predicate_simm13(CN)) {
Anton Korobeynikov05b89552008-10-10 10:14:15 +000090 if (FrameIndexSDNode *FIN =
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
92 // Constant offset from frame ref.
Owen Anderson36e3a6e2009-08-11 20:47:22 +000093 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 } else {
95 Base = Addr.getOperand(0);
96 }
Owen Anderson36e3a6e2009-08-11 20:47:22 +000097 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098 return true;
99 }
100 }
101 if (Addr.getOperand(0).getOpcode() == SPISD::Lo) {
102 Base = Addr.getOperand(1);
103 Offset = Addr.getOperand(0).getOperand(0);
104 return true;
105 }
106 if (Addr.getOperand(1).getOpcode() == SPISD::Lo) {
107 Base = Addr.getOperand(0);
108 Offset = Addr.getOperand(1).getOperand(0);
109 return true;
110 }
111 }
112 Base = Addr;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000113 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 return true;
115}
116
Dan Gohman5f082a72010-01-05 01:24:18 +0000117bool SparcDAGToDAGISel::SelectADDRrr(SDNode *Op, SDValue Addr,
Dan Gohman8181bd12008-07-27 21:46:04 +0000118 SDValue &R1, SDValue &R2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 if (Addr.getOpcode() == ISD::FrameIndex) return false;
Bill Wendlingfef06052008-09-16 21:48:12 +0000120 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 Addr.getOpcode() == ISD::TargetGlobalAddress)
122 return false; // direct calls.
Anton Korobeynikov05b89552008-10-10 10:14:15 +0000123
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124 if (Addr.getOpcode() == ISD::ADD) {
125 if (isa<ConstantSDNode>(Addr.getOperand(1)) &&
Gabor Greif1c80d112008-08-28 21:40:38 +0000126 Predicate_simm13(Addr.getOperand(1).getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127 return false; // Let the reg+imm pattern catch this!
128 if (Addr.getOperand(0).getOpcode() == SPISD::Lo ||
129 Addr.getOperand(1).getOpcode() == SPISD::Lo)
130 return false; // Let the reg+imm pattern catch this!
131 R1 = Addr.getOperand(0);
132 R2 = Addr.getOperand(1);
133 return true;
134 }
135
136 R1 = Addr;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000137 R2 = CurDAG->getRegister(SP::G0, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 return true;
139}
140
Dan Gohman5f082a72010-01-05 01:24:18 +0000141SDNode *SparcDAGToDAGISel::Select(SDNode *N) {
Dale Johannesen5d398a32009-02-06 19:16:40 +0000142 DebugLoc dl = N->getDebugLoc();
Dan Gohmanbd68c792008-07-17 19:10:17 +0000143 if (N->isMachineOpcode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 return NULL; // Already selected.
145
146 switch (N->getOpcode()) {
147 default: break;
Chris Lattner49102de2009-09-15 17:46:24 +0000148 case SPISD::GLOBAL_BASE_REG:
149 return getGlobalBaseReg();
150
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 case ISD::SDIV:
152 case ISD::UDIV: {
153 // FIXME: should use a custom expander to expose the SRA to the dag.
Dan Gohman8181bd12008-07-27 21:46:04 +0000154 SDValue DivLHS = N->getOperand(0);
155 SDValue DivRHS = N->getOperand(1);
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 Gohman61fda0d2009-09-25 18:54:59 +0000160 TopPart = SDValue(CurDAG->getMachineNode(SP::SRAri, dl, MVT::i32, DivLHS,
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000161 CurDAG->getTargetConstant(31, MVT::i32)), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 } else {
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000163 TopPart = CurDAG->getRegister(SP::G0, MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164 }
Dan Gohman61fda0d2009-09-25 18:54:59 +0000165 TopPart = SDValue(CurDAG->getMachineNode(SP::WRYrr, dl, MVT::Flag, TopPart,
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000166 CurDAG->getRegister(SP::G0, MVT::i32)), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167
168 // FIXME: Handle div by immediate.
169 unsigned Opcode = N->getOpcode() == ISD::SDIV ? SP::SDIVrr : SP::UDIVrr;
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000170 return CurDAG->SelectNodeTo(N, Opcode, MVT::i32, DivLHS, DivRHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 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 unsigned Opcode = N->getOpcode() == ISD::MULHU ? SP::UMULrr : SP::SMULrr;
Dan Gohman61fda0d2009-09-25 18:54:59 +0000179 SDNode *Mul = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::Flag,
180 MulLHS, MulRHS);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 // The high part is in the Y register.
Owen Anderson36e3a6e2009-08-11 20:47:22 +0000182 return CurDAG->SelectNodeTo(N, SP::RDY, MVT::i32, SDValue(Mul, 1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183 return NULL;
184 }
185 }
Anton Korobeynikov05b89552008-10-10 10:14:15 +0000186
Dan Gohman5f082a72010-01-05 01:24:18 +0000187 return SelectCode(N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188}
189
190
Anton Korobeynikov984a5172008-10-10 10:14:47 +0000191/// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
192/// inline asm expressions.
193bool
194SparcDAGToDAGISel::SelectInlineAsmMemoryOperand(const SDValue &Op,
195 char ConstraintCode,
196 std::vector<SDValue> &OutOps) {
197 SDValue Op0, Op1;
198 switch (ConstraintCode) {
199 default: return true;
200 case 'm': // memory
Dan Gohman5f082a72010-01-05 01:24:18 +0000201 if (!SelectADDRrr(Op.getNode(), Op, Op0, Op1))
202 SelectADDRri(Op.getNode(), Op, Op0, Op1);
Anton Korobeynikov984a5172008-10-10 10:14:47 +0000203 break;
204 }
205
206 OutOps.push_back(Op0);
207 OutOps.push_back(Op1);
208 return false;
209}
210
Anton Korobeynikov05b89552008-10-10 10:14:15 +0000211/// createSparcISelDag - This pass converts a legalized DAG into a
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212/// SPARC-specific DAG, ready for instruction scheduling.
213///
Dan Gohmanf2b29572008-10-03 16:55:19 +0000214FunctionPass *llvm::createSparcISelDag(SparcTargetMachine &TM) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215 return new SparcDAGToDAGISel(TM);
216}