blob: d7aa08f8ca666c8c907d08fd797d3b0e77780ffb [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"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020using namespace llvm;
21
22//===----------------------------------------------------------------------===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000032 /// Subtarget - Keep a pointer to the Sparc Subtarget around so that we can
33 /// make the right decision when generating code for different targets.
34 const SparcSubtarget &Subtarget;
35public:
Dan Gohmanf2b29572008-10-03 16:55:19 +000036 explicit SparcDAGToDAGISel(SparcTargetMachine &TM)
37 : SelectionDAGISel(*TM.getTargetLowering()),
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038 Subtarget(TM.getSubtarget<SparcSubtarget>()) {
39 }
40
Dan Gohman8181bd12008-07-27 21:46:04 +000041 SDNode *Select(SDValue Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042
43 // Complex Pattern Selectors.
Dan Gohman8181bd12008-07-27 21:46:04 +000044 bool SelectADDRrr(SDValue Op, SDValue N, SDValue &R1, SDValue &R2);
45 bool SelectADDRri(SDValue Op, SDValue N, SDValue &Base,
46 SDValue &Offset);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047
Evan Cheng34fd4f32008-06-30 20:45:06 +000048 /// InstructionSelect - This callback is invoked by
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +000050 virtual void InstructionSelect();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051
52 virtual const char *getPassName() const {
53 return "SPARC DAG->DAG Pattern Instruction Selection";
54 }
55
56 // Include the pieces autogenerated from the target description.
57#include "SparcGenDAGISel.inc"
58};
59} // end anonymous namespace
60
Evan Cheng34fd4f32008-06-30 20:45:06 +000061/// InstructionSelect - This callback is invoked by
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +000063void SparcDAGToDAGISel::InstructionSelect() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064 DEBUG(BB->dump());
65
66 // Select target instructions for the DAG.
Dan Gohmanbd3f8822008-08-21 16:36:34 +000067 SelectRoot();
Dan Gohman14a66442008-08-23 02:25:05 +000068 CurDAG->RemoveDeadNodes();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069}
70
Dan Gohman8181bd12008-07-27 21:46:04 +000071bool SparcDAGToDAGISel::SelectADDRri(SDValue Op, SDValue Addr,
72 SDValue &Base, SDValue &Offset) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
74 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
75 Offset = CurDAG->getTargetConstant(0, MVT::i32);
76 return true;
77 }
Bill Wendlingfef06052008-09-16 21:48:12 +000078 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079 Addr.getOpcode() == ISD::TargetGlobalAddress)
80 return false; // direct calls.
81
82 if (Addr.getOpcode() == ISD::ADD) {
83 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
84 if (Predicate_simm13(CN)) {
85 if (FrameIndexSDNode *FIN =
86 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
87 // Constant offset from frame ref.
88 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
89 } else {
90 Base = Addr.getOperand(0);
91 }
Dan Gohmanfaeb4a32008-09-12 16:56:44 +000092 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), MVT::i32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093 return true;
94 }
95 }
96 if (Addr.getOperand(0).getOpcode() == SPISD::Lo) {
97 Base = Addr.getOperand(1);
98 Offset = Addr.getOperand(0).getOperand(0);
99 return true;
100 }
101 if (Addr.getOperand(1).getOpcode() == SPISD::Lo) {
102 Base = Addr.getOperand(0);
103 Offset = Addr.getOperand(1).getOperand(0);
104 return true;
105 }
106 }
107 Base = Addr;
108 Offset = CurDAG->getTargetConstant(0, MVT::i32);
109 return true;
110}
111
Dan Gohman8181bd12008-07-27 21:46:04 +0000112bool SparcDAGToDAGISel::SelectADDRrr(SDValue Op, SDValue Addr,
113 SDValue &R1, SDValue &R2) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 if (Addr.getOpcode() == ISD::FrameIndex) return false;
Bill Wendlingfef06052008-09-16 21:48:12 +0000115 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 Addr.getOpcode() == ISD::TargetGlobalAddress)
117 return false; // direct calls.
118
119 if (Addr.getOpcode() == ISD::ADD) {
120 if (isa<ConstantSDNode>(Addr.getOperand(1)) &&
Gabor Greif1c80d112008-08-28 21:40:38 +0000121 Predicate_simm13(Addr.getOperand(1).getNode()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 return false; // Let the reg+imm pattern catch this!
123 if (Addr.getOperand(0).getOpcode() == SPISD::Lo ||
124 Addr.getOperand(1).getOpcode() == SPISD::Lo)
125 return false; // Let the reg+imm pattern catch this!
126 R1 = Addr.getOperand(0);
127 R2 = Addr.getOperand(1);
128 return true;
129 }
130
131 R1 = Addr;
132 R2 = CurDAG->getRegister(SP::G0, MVT::i32);
133 return true;
134}
135
Dan Gohman8181bd12008-07-27 21:46:04 +0000136SDNode *SparcDAGToDAGISel::Select(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000137 SDNode *N = Op.getNode();
Dan Gohmanbd68c792008-07-17 19:10:17 +0000138 if (N->isMachineOpcode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 return NULL; // Already selected.
140
141 switch (N->getOpcode()) {
142 default: break;
143 case ISD::SDIV:
144 case ISD::UDIV: {
145 // FIXME: should use a custom expander to expose the SRA to the dag.
Dan Gohman8181bd12008-07-27 21:46:04 +0000146 SDValue DivLHS = N->getOperand(0);
147 SDValue DivRHS = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148 AddToISelQueue(DivLHS);
149 AddToISelQueue(DivRHS);
150
151 // Set the Y register to the high-part.
Dan Gohman8181bd12008-07-27 21:46:04 +0000152 SDValue TopPart;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153 if (N->getOpcode() == ISD::SDIV) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000154 TopPart = SDValue(CurDAG->getTargetNode(SP::SRAri, MVT::i32, DivLHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 CurDAG->getTargetConstant(31, MVT::i32)), 0);
156 } else {
157 TopPart = CurDAG->getRegister(SP::G0, MVT::i32);
158 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000159 TopPart = SDValue(CurDAG->getTargetNode(SP::WRYrr, MVT::Flag, TopPart,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 CurDAG->getRegister(SP::G0, MVT::i32)), 0);
161
162 // FIXME: Handle div by immediate.
163 unsigned Opcode = N->getOpcode() == ISD::SDIV ? SP::SDIVrr : SP::UDIVrr;
164 return CurDAG->SelectNodeTo(N, Opcode, MVT::i32, DivLHS, DivRHS,
165 TopPart);
166 }
167 case ISD::MULHU:
168 case ISD::MULHS: {
169 // FIXME: Handle mul by immediate.
Dan Gohman8181bd12008-07-27 21:46:04 +0000170 SDValue MulLHS = N->getOperand(0);
171 SDValue MulRHS = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 AddToISelQueue(MulLHS);
173 AddToISelQueue(MulRHS);
174 unsigned Opcode = N->getOpcode() == ISD::MULHU ? SP::UMULrr : SP::SMULrr;
175 SDNode *Mul = CurDAG->getTargetNode(Opcode, MVT::i32, MVT::Flag,
176 MulLHS, MulRHS);
177 // The high part is in the Y register.
Dan Gohman8181bd12008-07-27 21:46:04 +0000178 return CurDAG->SelectNodeTo(N, SP::RDY, MVT::i32, SDValue(Mul, 1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 return NULL;
180 }
181 }
182
183 return SelectCode(Op);
184}
185
186
187/// createSparcISelDag - This pass converts a legalized DAG into a
188/// SPARC-specific DAG, ready for instruction scheduling.
189///
Dan Gohmanf2b29572008-10-03 16:55:19 +0000190FunctionPass *llvm::createSparcISelDag(SparcTargetMachine &TM) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 return new SparcDAGToDAGISel(TM);
192}