blob: b012bfdb01025fde4980ff92d3a691c972ac72d2 [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 Lattner158e1f52006-02-05 05:50:24 +000014#include "SparcTargetMachine.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000015#include "llvm/CodeGen/SelectionDAGISel.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/Intrinsics.h"
Chris Lattner1770fb82008-02-03 05:43:57 +000017#include "llvm/Support/Compiler.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000018#include "llvm/Support/Debug.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000019#include "llvm/Support/ErrorHandling.h"
20#include "llvm/Support/raw_ostream.h"
Chris Lattner158e1f52006-02-05 05:50:24 +000021using namespace llvm;
22
23//===----------------------------------------------------------------------===//
Chris Lattner158e1f52006-02-05 05:50:24 +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 {
Chris Lattner158e1f52006-02-05 05:50:24 +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;
Bill Wendlinga3cd3502013-06-19 21:36:55 +000036 SparcTargetMachine &TM;
Chris Lattner158e1f52006-02-05 05:50:24 +000037public:
Chris Lattner840c7002009-09-15 17:46:24 +000038 explicit SparcDAGToDAGISel(SparcTargetMachine &tm)
39 : SelectionDAGISel(tm),
40 Subtarget(tm.getSubtarget<SparcSubtarget>()),
41 TM(tm) {
Chris Lattner158e1f52006-02-05 05:50:24 +000042 }
43
Dan Gohmanea6f91f2010-01-05 01:24:18 +000044 SDNode *Select(SDNode *N);
Chris Lattner158e1f52006-02-05 05:50:24 +000045
46 // Complex Pattern Selectors.
Chris Lattner0e023ea2010-09-21 20:31:19 +000047 bool SelectADDRrr(SDValue N, SDValue &R1, SDValue &R2);
48 bool SelectADDRri(SDValue N, SDValue &Base, SDValue &Offset);
Anton Korobeynikov1f9487b2008-10-10 10:14:15 +000049
Anton Korobeynikov9aaaa402008-10-10 10:14:47 +000050 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
51 /// inline asm expressions.
52 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
53 char ConstraintCode,
54 std::vector<SDValue> &OutOps);
55
Chris Lattner158e1f52006-02-05 05:50:24 +000056 virtual const char *getPassName() const {
57 return "SPARC DAG->DAG Pattern Instruction Selection";
Anton Korobeynikov1f9487b2008-10-10 10:14:15 +000058 }
59
Chris Lattner158e1f52006-02-05 05:50:24 +000060 // Include the pieces autogenerated from the target description.
61#include "SparcGenDAGISel.inc"
Chris Lattner840c7002009-09-15 17:46:24 +000062
63private:
64 SDNode* getGlobalBaseReg();
Chris Lattner158e1f52006-02-05 05:50:24 +000065};
66} // end anonymous namespace
67
Chris Lattner840c7002009-09-15 17:46:24 +000068SDNode* SparcDAGToDAGISel::getGlobalBaseReg() {
Chris Lattner840c7002009-09-15 17:46:24 +000069 unsigned GlobalBaseReg = TM.getInstrInfo()->getGlobalBaseReg(MF);
Bill Wendlinga3cd3502013-06-19 21:36:55 +000070 return CurDAG->getRegister(GlobalBaseReg,
71 getTargetLowering()->getPointerTy()).getNode();
Chris Lattner840c7002009-09-15 17:46:24 +000072}
73
Chris Lattner0e023ea2010-09-21 20:31:19 +000074bool SparcDAGToDAGISel::SelectADDRri(SDValue Addr,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000075 SDValue &Base, SDValue &Offset) {
Chris Lattner158e1f52006-02-05 05:50:24 +000076 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
Bill Wendlinga3cd3502013-06-19 21:36:55 +000077 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(),
78 getTargetLowering()->getPointerTy());
Owen Anderson9f944592009-08-11 20:47:22 +000079 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Chris Lattner158e1f52006-02-05 05:50:24 +000080 return true;
81 }
Bill Wendling24c79f22008-09-16 21:48:12 +000082 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +000083 Addr.getOpcode() == ISD::TargetGlobalAddress ||
84 Addr.getOpcode() == ISD::TargetGlobalTLSAddress)
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +000085 return false; // direct calls.
Anton Korobeynikov1f9487b2008-10-10 10:14:15 +000086
Chris Lattner158e1f52006-02-05 05:50:24 +000087 if (Addr.getOpcode() == ISD::ADD) {
88 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
Jakob Stoklund Olesenf02b4a62010-08-17 18:17:12 +000089 if (isInt<13>(CN->getSExtValue())) {
Anton Korobeynikov1f9487b2008-10-10 10:14:15 +000090 if (FrameIndexSDNode *FIN =
Chris Lattner158e1f52006-02-05 05:50:24 +000091 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
92 // Constant offset from frame ref.
Jakob Stoklund Olesen6a0a3eb2013-04-13 19:02:16 +000093 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(),
Bill Wendlinga3cd3502013-06-19 21:36:55 +000094 getTargetLowering()->getPointerTy());
Chris Lattner158e1f52006-02-05 05:50:24 +000095 } else {
Chris Lattner463fa702006-02-05 08:35:50 +000096 Base = Addr.getOperand(0);
Chris Lattner158e1f52006-02-05 05:50:24 +000097 }
Owen Anderson9f944592009-08-11 20:47:22 +000098 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), MVT::i32);
Chris Lattner158e1f52006-02-05 05:50:24 +000099 return true;
100 }
101 }
102 if (Addr.getOperand(0).getOpcode() == SPISD::Lo) {
Chris Lattner463fa702006-02-05 08:35:50 +0000103 Base = Addr.getOperand(1);
Chris Lattner158e1f52006-02-05 05:50:24 +0000104 Offset = Addr.getOperand(0).getOperand(0);
105 return true;
106 }
107 if (Addr.getOperand(1).getOpcode() == SPISD::Lo) {
Chris Lattner463fa702006-02-05 08:35:50 +0000108 Base = Addr.getOperand(0);
Chris Lattner158e1f52006-02-05 05:50:24 +0000109 Offset = Addr.getOperand(1).getOperand(0);
110 return true;
111 }
112 }
Chris Lattner463fa702006-02-05 08:35:50 +0000113 Base = Addr;
Owen Anderson9f944592009-08-11 20:47:22 +0000114 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Chris Lattner158e1f52006-02-05 05:50:24 +0000115 return true;
116}
117
Chris Lattner0e023ea2010-09-21 20:31:19 +0000118bool SparcDAGToDAGISel::SelectADDRrr(SDValue Addr, SDValue &R1, SDValue &R2) {
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000119 if (Addr.getOpcode() == ISD::FrameIndex) return false;
Bill Wendling24c79f22008-09-16 21:48:12 +0000120 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +0000121 Addr.getOpcode() == ISD::TargetGlobalAddress ||
122 Addr.getOpcode() == ISD::TargetGlobalTLSAddress)
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000123 return false; // direct calls.
Anton Korobeynikov1f9487b2008-10-10 10:14:15 +0000124
Chris Lattner158e1f52006-02-05 05:50:24 +0000125 if (Addr.getOpcode() == ISD::ADD) {
Jakob Stoklund Olesenf02b4a62010-08-17 18:17:12 +0000126 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
127 if (isInt<13>(CN->getSExtValue()))
128 return false; // Let the reg+imm pattern catch this!
Chris Lattner158e1f52006-02-05 05:50:24 +0000129 if (Addr.getOperand(0).getOpcode() == SPISD::Lo ||
130 Addr.getOperand(1).getOpcode() == SPISD::Lo)
131 return false; // Let the reg+imm pattern catch this!
Chris Lattner463fa702006-02-05 08:35:50 +0000132 R1 = Addr.getOperand(0);
133 R2 = Addr.getOperand(1);
Chris Lattner158e1f52006-02-05 05:50:24 +0000134 return true;
135 }
136
Chris Lattner463fa702006-02-05 08:35:50 +0000137 R1 = Addr;
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000138 R2 = CurDAG->getRegister(SP::G0, getTargetLowering()->getPointerTy());
Chris Lattner158e1f52006-02-05 05:50:24 +0000139 return true;
140}
141
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000142SDNode *SparcDAGToDAGISel::Select(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000143 SDLoc dl(N);
Tim Northover31d093c2013-09-22 08:21:56 +0000144 if (N->isMachineOpcode()) {
145 N->setNodeId(-1);
Evan Chengbd1c5a82006-08-11 09:08:15 +0000146 return NULL; // Already selected.
Tim Northover31d093c2013-09-22 08:21:56 +0000147 }
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000148
Chris Lattner158e1f52006-02-05 05:50:24 +0000149 switch (N->getOpcode()) {
150 default: break;
Chris Lattner840c7002009-09-15 17:46:24 +0000151 case SPISD::GLOBAL_BASE_REG:
152 return getGlobalBaseReg();
153
Chris Lattner158e1f52006-02-05 05:50:24 +0000154 case ISD::SDIV:
155 case ISD::UDIV: {
Jakob Stoklund Olesen73d17392013-04-16 02:57:02 +0000156 // sdivx / udivx handle 64-bit divides.
157 if (N->getValueType(0) == MVT::i64)
158 break;
Chris Lattner158e1f52006-02-05 05:50:24 +0000159 // FIXME: should use a custom expander to expose the SRA to the dag.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000160 SDValue DivLHS = N->getOperand(0);
161 SDValue DivRHS = N->getOperand(1);
Anton Korobeynikov1f9487b2008-10-10 10:14:15 +0000162
Chris Lattner158e1f52006-02-05 05:50:24 +0000163 // Set the Y register to the high-part.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000164 SDValue TopPart;
Chris Lattner158e1f52006-02-05 05:50:24 +0000165 if (N->getOpcode() == ISD::SDIV) {
Dan Gohman32f71d72009-09-25 18:54:59 +0000166 TopPart = SDValue(CurDAG->getMachineNode(SP::SRAri, dl, MVT::i32, DivLHS,
Owen Anderson9f944592009-08-11 20:47:22 +0000167 CurDAG->getTargetConstant(31, MVT::i32)), 0);
Chris Lattner158e1f52006-02-05 05:50:24 +0000168 } else {
Owen Anderson9f944592009-08-11 20:47:22 +0000169 TopPart = CurDAG->getRegister(SP::G0, MVT::i32);
Chris Lattner158e1f52006-02-05 05:50:24 +0000170 }
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000171 TopPart = SDValue(CurDAG->getMachineNode(SP::WRYrr, dl, MVT::Glue, TopPart,
Owen Anderson9f944592009-08-11 20:47:22 +0000172 CurDAG->getRegister(SP::G0, MVT::i32)), 0);
Chris Lattner158e1f52006-02-05 05:50:24 +0000173
174 // FIXME: Handle div by immediate.
175 unsigned Opcode = N->getOpcode() == ISD::SDIV ? SP::SDIVrr : SP::UDIVrr;
Owen Anderson9f944592009-08-11 20:47:22 +0000176 return CurDAG->SelectNodeTo(N, Opcode, MVT::i32, DivLHS, DivRHS,
Evan Cheng34b70ee2006-08-26 08:00:10 +0000177 TopPart);
Anton Korobeynikov1f9487b2008-10-10 10:14:15 +0000178 }
Chris Lattner158e1f52006-02-05 05:50:24 +0000179 case ISD::MULHU:
180 case ISD::MULHS: {
181 // FIXME: Handle mul by immediate.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000182 SDValue MulLHS = N->getOperand(0);
183 SDValue MulRHS = N->getOperand(1);
Chris Lattner158e1f52006-02-05 05:50:24 +0000184 unsigned Opcode = N->getOpcode() == ISD::MULHU ? SP::UMULrr : SP::SMULrr;
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000185 SDNode *Mul = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +0000186 MulLHS, MulRHS);
Chris Lattner158e1f52006-02-05 05:50:24 +0000187 // The high part is in the Y register.
Owen Anderson9f944592009-08-11 20:47:22 +0000188 return CurDAG->SelectNodeTo(N, SP::RDY, MVT::i32, SDValue(Mul, 1));
Chris Lattner158e1f52006-02-05 05:50:24 +0000189 }
Chris Lattner158e1f52006-02-05 05:50:24 +0000190 }
Anton Korobeynikov1f9487b2008-10-10 10:14:15 +0000191
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000192 return SelectCode(N);
Chris Lattner158e1f52006-02-05 05:50:24 +0000193}
194
195
Anton Korobeynikov9aaaa402008-10-10 10:14:47 +0000196/// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
197/// inline asm expressions.
198bool
199SparcDAGToDAGISel::SelectInlineAsmMemoryOperand(const SDValue &Op,
200 char ConstraintCode,
201 std::vector<SDValue> &OutOps) {
202 SDValue Op0, Op1;
203 switch (ConstraintCode) {
204 default: return true;
205 case 'm': // memory
Chris Lattner0e023ea2010-09-21 20:31:19 +0000206 if (!SelectADDRrr(Op, Op0, Op1))
207 SelectADDRri(Op, Op0, Op1);
Anton Korobeynikov9aaaa402008-10-10 10:14:47 +0000208 break;
209 }
210
211 OutOps.push_back(Op0);
212 OutOps.push_back(Op1);
213 return false;
214}
215
Anton Korobeynikov1f9487b2008-10-10 10:14:15 +0000216/// createSparcISelDag - This pass converts a legalized DAG into a
Chris Lattner158e1f52006-02-05 05:50:24 +0000217/// SPARC-specific DAG, ready for instruction scheduling.
218///
Dan Gohman2c836cf2008-10-03 16:55:19 +0000219FunctionPass *llvm::createSparcISelDag(SparcTargetMachine &TM) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000220 return new SparcDAGToDAGISel(TM);
221}