blob: b3b029e3632dc35ca07ee1149381daa4f1727499 [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
Craig Topperb0c941b2014-04-29 07:57:13 +000044 SDNode *Select(SDNode *N) override;
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.
Craig Topperb0c941b2014-04-29 07:57:13 +000052 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
53 char ConstraintCode,
54 std::vector<SDValue> &OutOps) override;
Anton Korobeynikov9aaaa402008-10-10 10:14:47 +000055
Craig Topperb0c941b2014-04-29 07:57:13 +000056 const char *getPassName() const override {
Chris Lattner158e1f52006-02-05 05:50:24 +000057 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() {
Eric Christopherd9134482014-08-04 21:25:23 +000069 unsigned GlobalBaseReg =
70 TM.getSubtargetImpl()->getInstrInfo()->getGlobalBaseReg(MF);
Eric Christopherb17140d2014-10-08 07:32:17 +000071 return CurDAG->getRegister(GlobalBaseReg, TLI->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)) {
Eric Christopherb17140d2014-10-08 07:32:17 +000077 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), TLI->getPointerTy());
Owen Anderson9f944592009-08-11 20:47:22 +000078 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Chris Lattner158e1f52006-02-05 05:50:24 +000079 return true;
80 }
Bill Wendling24c79f22008-09-16 21:48:12 +000081 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +000082 Addr.getOpcode() == ISD::TargetGlobalAddress ||
83 Addr.getOpcode() == ISD::TargetGlobalTLSAddress)
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +000084 return false; // direct calls.
Anton Korobeynikov1f9487b2008-10-10 10:14:15 +000085
Chris Lattner158e1f52006-02-05 05:50:24 +000086 if (Addr.getOpcode() == ISD::ADD) {
87 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
Jakob Stoklund Olesenf02b4a62010-08-17 18:17:12 +000088 if (isInt<13>(CN->getSExtValue())) {
Anton Korobeynikov1f9487b2008-10-10 10:14:15 +000089 if (FrameIndexSDNode *FIN =
Chris Lattner158e1f52006-02-05 05:50:24 +000090 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
91 // Constant offset from frame ref.
Eric Christopherb17140d2014-10-08 07:32:17 +000092 Base =
93 CurDAG->getTargetFrameIndex(FIN->getIndex(), TLI->getPointerTy());
Chris Lattner158e1f52006-02-05 05:50:24 +000094 } else {
Chris Lattner463fa702006-02-05 08:35:50 +000095 Base = Addr.getOperand(0);
Chris Lattner158e1f52006-02-05 05:50:24 +000096 }
Owen Anderson9f944592009-08-11 20:47:22 +000097 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), MVT::i32);
Chris Lattner158e1f52006-02-05 05:50:24 +000098 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;
Owen Anderson9f944592009-08-11 20:47:22 +0000113 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Chris Lattner158e1f52006-02-05 05:50:24 +0000114 return true;
115}
116
Chris Lattner0e023ea2010-09-21 20:31:19 +0000117bool SparcDAGToDAGISel::SelectADDRrr(SDValue Addr, SDValue &R1, SDValue &R2) {
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000118 if (Addr.getOpcode() == ISD::FrameIndex) return false;
Bill Wendling24c79f22008-09-16 21:48:12 +0000119 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +0000120 Addr.getOpcode() == ISD::TargetGlobalAddress ||
121 Addr.getOpcode() == ISD::TargetGlobalTLSAddress)
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000122 return false; // direct calls.
Anton Korobeynikov1f9487b2008-10-10 10:14:15 +0000123
Chris Lattner158e1f52006-02-05 05:50:24 +0000124 if (Addr.getOpcode() == ISD::ADD) {
Jakob Stoklund Olesenf02b4a62010-08-17 18:17:12 +0000125 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
126 if (isInt<13>(CN->getSExtValue()))
127 return false; // Let the reg+imm pattern catch this!
Chris Lattner158e1f52006-02-05 05:50:24 +0000128 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;
Eric Christopherb17140d2014-10-08 07:32:17 +0000137 R2 = CurDAG->getRegister(SP::G0, TLI->getPointerTy());
Chris Lattner158e1f52006-02-05 05:50:24 +0000138 return true;
139}
140
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000141SDNode *SparcDAGToDAGISel::Select(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000142 SDLoc dl(N);
Tim Northover31d093c2013-09-22 08:21:56 +0000143 if (N->isMachineOpcode()) {
144 N->setNodeId(-1);
Craig Topper062a2ba2014-04-25 05:30:21 +0000145 return nullptr; // Already selected.
Tim Northover31d093c2013-09-22 08:21:56 +0000146 }
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000147
Chris Lattner158e1f52006-02-05 05:50:24 +0000148 switch (N->getOpcode()) {
149 default: break;
Chris Lattner840c7002009-09-15 17:46:24 +0000150 case SPISD::GLOBAL_BASE_REG:
151 return getGlobalBaseReg();
152
Chris Lattner158e1f52006-02-05 05:50:24 +0000153 case ISD::SDIV:
154 case ISD::UDIV: {
Jakob Stoklund Olesen73d17392013-04-16 02:57:02 +0000155 // sdivx / udivx handle 64-bit divides.
156 if (N->getValueType(0) == MVT::i64)
157 break;
Chris Lattner158e1f52006-02-05 05:50:24 +0000158 // FIXME: should use a custom expander to expose the SRA to the dag.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000159 SDValue DivLHS = N->getOperand(0);
160 SDValue DivRHS = N->getOperand(1);
Anton Korobeynikov1f9487b2008-10-10 10:14:15 +0000161
Chris Lattner158e1f52006-02-05 05:50:24 +0000162 // Set the Y register to the high-part.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000163 SDValue TopPart;
Chris Lattner158e1f52006-02-05 05:50:24 +0000164 if (N->getOpcode() == ISD::SDIV) {
Dan Gohman32f71d72009-09-25 18:54:59 +0000165 TopPart = SDValue(CurDAG->getMachineNode(SP::SRAri, dl, MVT::i32, DivLHS,
Owen Anderson9f944592009-08-11 20:47:22 +0000166 CurDAG->getTargetConstant(31, MVT::i32)), 0);
Chris Lattner158e1f52006-02-05 05:50:24 +0000167 } else {
Owen Anderson9f944592009-08-11 20:47:22 +0000168 TopPart = CurDAG->getRegister(SP::G0, MVT::i32);
Chris Lattner158e1f52006-02-05 05:50:24 +0000169 }
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000170 TopPart = SDValue(CurDAG->getMachineNode(SP::WRYrr, dl, MVT::Glue, TopPart,
Owen Anderson9f944592009-08-11 20:47:22 +0000171 CurDAG->getRegister(SP::G0, MVT::i32)), 0);
Chris Lattner158e1f52006-02-05 05:50:24 +0000172
173 // FIXME: Handle div by immediate.
174 unsigned Opcode = N->getOpcode() == ISD::SDIV ? SP::SDIVrr : SP::UDIVrr;
Owen Anderson9f944592009-08-11 20:47:22 +0000175 return CurDAG->SelectNodeTo(N, Opcode, MVT::i32, DivLHS, DivRHS,
Evan Cheng34b70ee2006-08-26 08:00:10 +0000176 TopPart);
Anton Korobeynikov1f9487b2008-10-10 10:14:15 +0000177 }
Chris Lattner158e1f52006-02-05 05:50:24 +0000178 case ISD::MULHU:
179 case ISD::MULHS: {
180 // FIXME: Handle mul by immediate.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000181 SDValue MulLHS = N->getOperand(0);
182 SDValue MulRHS = N->getOperand(1);
Chris Lattner158e1f52006-02-05 05:50:24 +0000183 unsigned Opcode = N->getOpcode() == ISD::MULHU ? SP::UMULrr : SP::SMULrr;
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000184 SDNode *Mul = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +0000185 MulLHS, MulRHS);
Chris Lattner158e1f52006-02-05 05:50:24 +0000186 // The high part is in the Y register.
Owen Anderson9f944592009-08-11 20:47:22 +0000187 return CurDAG->SelectNodeTo(N, SP::RDY, MVT::i32, SDValue(Mul, 1));
Chris Lattner158e1f52006-02-05 05:50:24 +0000188 }
Chris Lattner158e1f52006-02-05 05:50:24 +0000189 }
Anton Korobeynikov1f9487b2008-10-10 10:14:15 +0000190
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000191 return SelectCode(N);
Chris Lattner158e1f52006-02-05 05:50:24 +0000192}
193
194
Anton Korobeynikov9aaaa402008-10-10 10:14:47 +0000195/// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
196/// inline asm expressions.
197bool
198SparcDAGToDAGISel::SelectInlineAsmMemoryOperand(const SDValue &Op,
199 char ConstraintCode,
200 std::vector<SDValue> &OutOps) {
201 SDValue Op0, Op1;
202 switch (ConstraintCode) {
203 default: return true;
204 case 'm': // memory
Chris Lattner0e023ea2010-09-21 20:31:19 +0000205 if (!SelectADDRrr(Op, Op0, Op1))
206 SelectADDRri(Op, Op0, Op1);
Anton Korobeynikov9aaaa402008-10-10 10:14:47 +0000207 break;
208 }
209
210 OutOps.push_back(Op0);
211 OutOps.push_back(Op1);
212 return false;
213}
214
Anton Korobeynikov1f9487b2008-10-10 10:14:15 +0000215/// createSparcISelDag - This pass converts a legalized DAG into a
Chris Lattner158e1f52006-02-05 05:50:24 +0000216/// SPARC-specific DAG, ready for instruction scheduling.
217///
Dan Gohman2c836cf2008-10-03 16:55:19 +0000218FunctionPass *llvm::createSparcISelDag(SparcTargetMachine &TM) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000219 return new SparcDAGToDAGISel(TM);
220}