blob: 3ef27dd642bf150ba90c4257d1b78aefee4685db [file] [log] [blame]
Chris Lattner158e1f52006-02-05 05:50:24 +00001//===-- SparcISelDAGToDAG.cpp - A dag to dag inst selector for Sparc ------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner158e1f52006-02-05 05:50:24 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines an instruction selector for the SPARC target.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner0a1762e2008-03-17 03:21:36 +000014#include "SparcISelLowering.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000015#include "SparcTargetMachine.h"
Chris Lattner5d70a7c2006-03-25 06:47:10 +000016#include "llvm/Intrinsics.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000017#include "llvm/CodeGen/SelectionDAGISel.h"
Chris Lattner1770fb82008-02-03 05:43:57 +000018#include "llvm/Support/Compiler.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000019#include "llvm/Support/Debug.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000020using namespace llvm;
21
22//===----------------------------------------------------------------------===//
Chris Lattner158e1f52006-02-05 05:50:24 +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 {
32 SparcTargetLowering Lowering;
33
34 /// 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:
38 SparcDAGToDAGISel(TargetMachine &TM)
39 : SelectionDAGISel(Lowering), Lowering(TM),
40 Subtarget(TM.getSubtarget<SparcSubtarget>()) {
41 }
42
Evan Cheng61413a32006-08-26 05:34:46 +000043 SDNode *Select(SDOperand Op);
Chris Lattner158e1f52006-02-05 05:50:24 +000044
45 // Complex Pattern Selectors.
Evan Cheng6cd09092006-11-08 20:34:28 +000046 bool SelectADDRrr(SDOperand Op, SDOperand N, SDOperand &R1, SDOperand &R2);
47 bool SelectADDRri(SDOperand Op, SDOperand N, SDOperand &Base,
48 SDOperand &Offset);
Chris Lattner158e1f52006-02-05 05:50:24 +000049
50 /// InstructionSelectBasicBlock - This callback is invoked by
51 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
52 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
53
54 virtual const char *getPassName() const {
55 return "SPARC DAG->DAG Pattern Instruction Selection";
56 }
57
58 // Include the pieces autogenerated from the target description.
59#include "SparcGenDAGISel.inc"
60};
61} // end anonymous namespace
62
63/// InstructionSelectBasicBlock - This callback is invoked by
64/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
65void SparcDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
66 DEBUG(BB->dump());
67
68 // Select target instructions for the DAG.
Evan Chenga28b7642006-02-05 06:51:51 +000069 DAG.setRoot(SelectRoot(DAG.getRoot()));
Chris Lattner158e1f52006-02-05 05:50:24 +000070 DAG.RemoveDeadNodes();
71
72 // Emit machine code to BB.
73 ScheduleAndEmitDAG(DAG);
74}
75
Evan Cheng6cd09092006-11-08 20:34:28 +000076bool SparcDAGToDAGISel::SelectADDRri(SDOperand Op, SDOperand Addr,
77 SDOperand &Base, SDOperand &Offset) {
Chris Lattner158e1f52006-02-05 05:50:24 +000078 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
79 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
80 Offset = CurDAG->getTargetConstant(0, MVT::i32);
81 return true;
82 }
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +000083 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
84 Addr.getOpcode() == ISD::TargetGlobalAddress)
85 return false; // direct calls.
Chris Lattner158e1f52006-02-05 05:50:24 +000086
87 if (Addr.getOpcode() == ISD::ADD) {
88 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
89 if (Predicate_simm13(CN)) {
90 if (FrameIndexSDNode *FIN =
91 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
92 // Constant offset from frame ref.
93 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
94 } else {
Chris Lattner463fa702006-02-05 08:35:50 +000095 Base = Addr.getOperand(0);
Chris Lattner158e1f52006-02-05 05:50:24 +000096 }
97 Offset = CurDAG->getTargetConstant(CN->getValue(), MVT::i32);
98 return true;
99 }
100 }
101 if (Addr.getOperand(0).getOpcode() == SPISD::Lo) {
Chris Lattner463fa702006-02-05 08:35:50 +0000102 Base = Addr.getOperand(1);
Chris Lattner158e1f52006-02-05 05:50:24 +0000103 Offset = Addr.getOperand(0).getOperand(0);
104 return true;
105 }
106 if (Addr.getOperand(1).getOpcode() == SPISD::Lo) {
Chris Lattner463fa702006-02-05 08:35:50 +0000107 Base = Addr.getOperand(0);
Chris Lattner158e1f52006-02-05 05:50:24 +0000108 Offset = Addr.getOperand(1).getOperand(0);
109 return true;
110 }
111 }
Chris Lattner463fa702006-02-05 08:35:50 +0000112 Base = Addr;
Chris Lattner158e1f52006-02-05 05:50:24 +0000113 Offset = CurDAG->getTargetConstant(0, MVT::i32);
114 return true;
115}
116
Evan Cheng6cd09092006-11-08 20:34:28 +0000117bool SparcDAGToDAGISel::SelectADDRrr(SDOperand Op, SDOperand Addr,
118 SDOperand &R1, SDOperand &R2) {
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000119 if (Addr.getOpcode() == ISD::FrameIndex) return false;
120 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
121 Addr.getOpcode() == ISD::TargetGlobalAddress)
122 return false; // direct calls.
123
Chris Lattner158e1f52006-02-05 05:50:24 +0000124 if (Addr.getOpcode() == ISD::ADD) {
125 if (isa<ConstantSDNode>(Addr.getOperand(1)) &&
126 Predicate_simm13(Addr.getOperand(1).Val))
127 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!
Chris Lattner463fa702006-02-05 08:35:50 +0000131 R1 = Addr.getOperand(0);
132 R2 = Addr.getOperand(1);
Chris Lattner158e1f52006-02-05 05:50:24 +0000133 return true;
134 }
135
Chris Lattner463fa702006-02-05 08:35:50 +0000136 R1 = Addr;
Chris Lattner158e1f52006-02-05 05:50:24 +0000137 R2 = CurDAG->getRegister(SP::G0, MVT::i32);
138 return true;
139}
140
Evan Cheng61413a32006-08-26 05:34:46 +0000141SDNode *SparcDAGToDAGISel::Select(SDOperand Op) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000142 SDNode *N = Op.Val;
143 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
Evan Cheng61413a32006-08-26 05:34:46 +0000144 N->getOpcode() < SPISD::FIRST_NUMBER)
Evan Chengbd1c5a82006-08-11 09:08:15 +0000145 return NULL; // Already selected.
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000146
Chris Lattner158e1f52006-02-05 05:50:24 +0000147 switch (N->getOpcode()) {
148 default: break;
Chris Lattner158e1f52006-02-05 05:50:24 +0000149 case ISD::SDIV:
150 case ISD::UDIV: {
151 // FIXME: should use a custom expander to expose the SRA to the dag.
Evan Chengab8297f2006-08-26 01:07:58 +0000152 SDOperand DivLHS = N->getOperand(0);
153 SDOperand DivRHS = N->getOperand(1);
154 AddToISelQueue(DivLHS);
155 AddToISelQueue(DivRHS);
Chris Lattner158e1f52006-02-05 05:50:24 +0000156
157 // Set the Y register to the high-part.
158 SDOperand TopPart;
159 if (N->getOpcode() == ISD::SDIV) {
Evan Chengd1b82d82006-02-09 07:17:49 +0000160 TopPart = SDOperand(CurDAG->getTargetNode(SP::SRAri, MVT::i32, DivLHS,
161 CurDAG->getTargetConstant(31, MVT::i32)), 0);
Chris Lattner158e1f52006-02-05 05:50:24 +0000162 } else {
163 TopPart = CurDAG->getRegister(SP::G0, MVT::i32);
164 }
Evan Chengd1b82d82006-02-09 07:17:49 +0000165 TopPart = SDOperand(CurDAG->getTargetNode(SP::WRYrr, MVT::Flag, TopPart,
166 CurDAG->getRegister(SP::G0, MVT::i32)), 0);
Chris Lattner158e1f52006-02-05 05:50:24 +0000167
168 // FIXME: Handle div by immediate.
169 unsigned Opcode = N->getOpcode() == ISD::SDIV ? SP::SDIVrr : SP::UDIVrr;
Evan Cheng63d178f2006-08-16 07:30:09 +0000170 return CurDAG->SelectNodeTo(N, Opcode, MVT::i32, DivLHS, DivRHS,
Evan Cheng34b70ee2006-08-26 08:00:10 +0000171 TopPart);
Chris Lattner158e1f52006-02-05 05:50:24 +0000172 }
173 case ISD::MULHU:
174 case ISD::MULHS: {
175 // FIXME: Handle mul by immediate.
Evan Chengab8297f2006-08-26 01:07:58 +0000176 SDOperand MulLHS = N->getOperand(0);
177 SDOperand MulRHS = N->getOperand(1);
178 AddToISelQueue(MulLHS);
179 AddToISelQueue(MulRHS);
Chris Lattner158e1f52006-02-05 05:50:24 +0000180 unsigned Opcode = N->getOpcode() == ISD::MULHU ? SP::UMULrr : SP::SMULrr;
Evan Chengd1b82d82006-02-09 07:17:49 +0000181 SDNode *Mul = CurDAG->getTargetNode(Opcode, MVT::i32, MVT::Flag,
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000182 MulLHS, MulRHS);
Chris Lattner158e1f52006-02-05 05:50:24 +0000183 // The high part is in the Y register.
Evan Cheng34b70ee2006-08-26 08:00:10 +0000184 return CurDAG->SelectNodeTo(N, SP::RDY, MVT::i32, SDOperand(Mul, 1));
Evan Chengbd1c5a82006-08-11 09:08:15 +0000185 return NULL;
Chris Lattner158e1f52006-02-05 05:50:24 +0000186 }
Chris Lattner158e1f52006-02-05 05:50:24 +0000187 }
188
Evan Cheng61413a32006-08-26 05:34:46 +0000189 return SelectCode(Op);
Chris Lattner158e1f52006-02-05 05:50:24 +0000190}
191
192
193/// createSparcISelDag - This pass converts a legalized DAG into a
194/// SPARC-specific DAG, ready for instruction scheduling.
195///
196FunctionPass *llvm::createSparcISelDag(TargetMachine &TM) {
197 return new SparcDAGToDAGISel(TM);
198}