blob: 340b72e7940f188581cf62993e8f9e497a2a70e1 [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.
Eric Christopherf5e94062015-01-30 23:46:43 +000035 const SparcSubtarget *Subtarget;
Chris Lattner158e1f52006-02-05 05:50:24 +000036public:
Eric Christopherf5e94062015-01-30 23:46:43 +000037 explicit SparcDAGToDAGISel(SparcTargetMachine &tm) : SelectionDAGISel(tm) {}
38
39 bool runOnMachineFunction(MachineFunction &MF) override {
40 Subtarget = &MF.getSubtarget<SparcSubtarget>();
41 return SelectionDAGISel::runOnMachineFunction(MF);
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,
Daniel Sanders60f1db02015-03-13 12:45:09 +000053 unsigned ConstraintID,
Craig Topperb0c941b2014-04-29 07:57:13 +000054 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 Christopherf5e94062015-01-30 23:46:43 +000069 unsigned GlobalBaseReg = Subtarget->getInstrInfo()->getGlobalBaseReg(MF);
Mehdi Amini44ede332015-07-09 02:09:04 +000070 return CurDAG->getRegister(GlobalBaseReg,
71 TLI->getPointerTy(CurDAG->getDataLayout()))
72 .getNode();
Chris Lattner840c7002009-09-15 17:46:24 +000073}
74
Chris Lattner0e023ea2010-09-21 20:31:19 +000075bool SparcDAGToDAGISel::SelectADDRri(SDValue Addr,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000076 SDValue &Base, SDValue &Offset) {
Chris Lattner158e1f52006-02-05 05:50:24 +000077 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
Mehdi Amini44ede332015-07-09 02:09:04 +000078 Base = CurDAG->getTargetFrameIndex(
79 FIN->getIndex(), TLI->getPointerTy(CurDAG->getDataLayout()));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000080 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i32);
Chris Lattner158e1f52006-02-05 05:50:24 +000081 return true;
82 }
Bill Wendling24c79f22008-09-16 21:48:12 +000083 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +000084 Addr.getOpcode() == ISD::TargetGlobalAddress ||
85 Addr.getOpcode() == ISD::TargetGlobalTLSAddress)
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +000086 return false; // direct calls.
Anton Korobeynikov1f9487b2008-10-10 10:14:15 +000087
Chris Lattner158e1f52006-02-05 05:50:24 +000088 if (Addr.getOpcode() == ISD::ADD) {
89 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
Jakob Stoklund Olesenf02b4a62010-08-17 18:17:12 +000090 if (isInt<13>(CN->getSExtValue())) {
Anton Korobeynikov1f9487b2008-10-10 10:14:15 +000091 if (FrameIndexSDNode *FIN =
Chris Lattner158e1f52006-02-05 05:50:24 +000092 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
93 // Constant offset from frame ref.
Mehdi Amini44ede332015-07-09 02:09:04 +000094 Base = CurDAG->getTargetFrameIndex(
95 FIN->getIndex(), TLI->getPointerTy(CurDAG->getDataLayout()));
Chris Lattner158e1f52006-02-05 05:50:24 +000096 } else {
Chris Lattner463fa702006-02-05 08:35:50 +000097 Base = Addr.getOperand(0);
Chris Lattner158e1f52006-02-05 05:50:24 +000098 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +000099 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), SDLoc(Addr),
100 MVT::i32);
Chris Lattner158e1f52006-02-05 05:50:24 +0000101 return true;
102 }
103 }
104 if (Addr.getOperand(0).getOpcode() == SPISD::Lo) {
Chris Lattner463fa702006-02-05 08:35:50 +0000105 Base = Addr.getOperand(1);
Chris Lattner158e1f52006-02-05 05:50:24 +0000106 Offset = Addr.getOperand(0).getOperand(0);
107 return true;
108 }
109 if (Addr.getOperand(1).getOpcode() == SPISD::Lo) {
Chris Lattner463fa702006-02-05 08:35:50 +0000110 Base = Addr.getOperand(0);
Chris Lattner158e1f52006-02-05 05:50:24 +0000111 Offset = Addr.getOperand(1).getOperand(0);
112 return true;
113 }
114 }
Chris Lattner463fa702006-02-05 08:35:50 +0000115 Base = Addr;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000116 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i32);
Chris Lattner158e1f52006-02-05 05:50:24 +0000117 return true;
118}
119
Chris Lattner0e023ea2010-09-21 20:31:19 +0000120bool SparcDAGToDAGISel::SelectADDRrr(SDValue Addr, SDValue &R1, SDValue &R2) {
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000121 if (Addr.getOpcode() == ISD::FrameIndex) return false;
Bill Wendling24c79f22008-09-16 21:48:12 +0000122 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +0000123 Addr.getOpcode() == ISD::TargetGlobalAddress ||
124 Addr.getOpcode() == ISD::TargetGlobalTLSAddress)
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000125 return false; // direct calls.
Anton Korobeynikov1f9487b2008-10-10 10:14:15 +0000126
Chris Lattner158e1f52006-02-05 05:50:24 +0000127 if (Addr.getOpcode() == ISD::ADD) {
Jakob Stoklund Olesenf02b4a62010-08-17 18:17:12 +0000128 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
129 if (isInt<13>(CN->getSExtValue()))
130 return false; // Let the reg+imm pattern catch this!
Chris Lattner158e1f52006-02-05 05:50:24 +0000131 if (Addr.getOperand(0).getOpcode() == SPISD::Lo ||
132 Addr.getOperand(1).getOpcode() == SPISD::Lo)
133 return false; // Let the reg+imm pattern catch this!
Chris Lattner463fa702006-02-05 08:35:50 +0000134 R1 = Addr.getOperand(0);
135 R2 = Addr.getOperand(1);
Chris Lattner158e1f52006-02-05 05:50:24 +0000136 return true;
137 }
138
Chris Lattner463fa702006-02-05 08:35:50 +0000139 R1 = Addr;
Mehdi Amini44ede332015-07-09 02:09:04 +0000140 R2 = CurDAG->getRegister(SP::G0, TLI->getPointerTy(CurDAG->getDataLayout()));
Chris Lattner158e1f52006-02-05 05:50:24 +0000141 return true;
142}
143
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000144SDNode *SparcDAGToDAGISel::Select(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000145 SDLoc dl(N);
Tim Northover31d093c2013-09-22 08:21:56 +0000146 if (N->isMachineOpcode()) {
147 N->setNodeId(-1);
Craig Topper062a2ba2014-04-25 05:30:21 +0000148 return nullptr; // Already selected.
Tim Northover31d093c2013-09-22 08:21:56 +0000149 }
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000150
Chris Lattner158e1f52006-02-05 05:50:24 +0000151 switch (N->getOpcode()) {
152 default: break;
Chris Lattner840c7002009-09-15 17:46:24 +0000153 case SPISD::GLOBAL_BASE_REG:
154 return getGlobalBaseReg();
155
Chris Lattner158e1f52006-02-05 05:50:24 +0000156 case ISD::SDIV:
157 case ISD::UDIV: {
Jakob Stoklund Olesen73d17392013-04-16 02:57:02 +0000158 // sdivx / udivx handle 64-bit divides.
159 if (N->getValueType(0) == MVT::i64)
160 break;
Chris Lattner158e1f52006-02-05 05:50:24 +0000161 // FIXME: should use a custom expander to expose the SRA to the dag.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000162 SDValue DivLHS = N->getOperand(0);
163 SDValue DivRHS = N->getOperand(1);
Anton Korobeynikov1f9487b2008-10-10 10:14:15 +0000164
Chris Lattner158e1f52006-02-05 05:50:24 +0000165 // Set the Y register to the high-part.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000166 SDValue TopPart;
Chris Lattner158e1f52006-02-05 05:50:24 +0000167 if (N->getOpcode() == ISD::SDIV) {
Dan Gohman32f71d72009-09-25 18:54:59 +0000168 TopPart = SDValue(CurDAG->getMachineNode(SP::SRAri, dl, MVT::i32, DivLHS,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000169 CurDAG->getTargetConstant(31, dl, MVT::i32)),
170 0);
Chris Lattner158e1f52006-02-05 05:50:24 +0000171 } else {
Owen Anderson9f944592009-08-11 20:47:22 +0000172 TopPart = CurDAG->getRegister(SP::G0, MVT::i32);
Chris Lattner158e1f52006-02-05 05:50:24 +0000173 }
James Y Knightf238d172015-07-08 16:25:12 +0000174 TopPart = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, SP::Y, TopPart,
175 SDValue())
176 .getValue(1);
Chris Lattner158e1f52006-02-05 05:50:24 +0000177
178 // FIXME: Handle div by immediate.
179 unsigned Opcode = N->getOpcode() == ISD::SDIV ? SP::SDIVrr : SP::UDIVrr;
Owen Anderson9f944592009-08-11 20:47:22 +0000180 return CurDAG->SelectNodeTo(N, Opcode, MVT::i32, DivLHS, DivRHS,
Evan Cheng34b70ee2006-08-26 08:00:10 +0000181 TopPart);
Anton Korobeynikov1f9487b2008-10-10 10:14:15 +0000182 }
Chris Lattner158e1f52006-02-05 05:50:24 +0000183 case ISD::MULHU:
184 case ISD::MULHS: {
185 // FIXME: Handle mul by immediate.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000186 SDValue MulLHS = N->getOperand(0);
187 SDValue MulRHS = N->getOperand(1);
Chris Lattner158e1f52006-02-05 05:50:24 +0000188 unsigned Opcode = N->getOpcode() == ISD::MULHU ? SP::UMULrr : SP::SMULrr;
James Y Knightf238d172015-07-08 16:25:12 +0000189 SDNode *Mul =
190 CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::i32, MulLHS, MulRHS);
191 SDValue ResultHigh = SDValue(Mul, 1);
192 ReplaceUses(SDValue(N, 0), ResultHigh);
193 return nullptr;
Chris Lattner158e1f52006-02-05 05:50:24 +0000194 }
Chris Lattner158e1f52006-02-05 05:50:24 +0000195 }
Anton Korobeynikov1f9487b2008-10-10 10:14:15 +0000196
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000197 return SelectCode(N);
Chris Lattner158e1f52006-02-05 05:50:24 +0000198}
199
200
Anton Korobeynikov9aaaa402008-10-10 10:14:47 +0000201/// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
202/// inline asm expressions.
203bool
204SparcDAGToDAGISel::SelectInlineAsmMemoryOperand(const SDValue &Op,
Daniel Sanders60f1db02015-03-13 12:45:09 +0000205 unsigned ConstraintID,
Anton Korobeynikov9aaaa402008-10-10 10:14:47 +0000206 std::vector<SDValue> &OutOps) {
207 SDValue Op0, Op1;
Daniel Sanders60f1db02015-03-13 12:45:09 +0000208 switch (ConstraintID) {
Anton Korobeynikov9aaaa402008-10-10 10:14:47 +0000209 default: return true;
Daniel Sandersb1fbaca2015-03-19 11:27:23 +0000210 case InlineAsm::Constraint_i:
Daniel Sanders60f1db02015-03-13 12:45:09 +0000211 case InlineAsm::Constraint_m: // memory
Chris Lattner0e023ea2010-09-21 20:31:19 +0000212 if (!SelectADDRrr(Op, Op0, Op1))
213 SelectADDRri(Op, Op0, Op1);
Anton Korobeynikov9aaaa402008-10-10 10:14:47 +0000214 break;
215 }
216
217 OutOps.push_back(Op0);
218 OutOps.push_back(Op1);
219 return false;
220}
221
Anton Korobeynikov1f9487b2008-10-10 10:14:15 +0000222/// createSparcISelDag - This pass converts a legalized DAG into a
Chris Lattner158e1f52006-02-05 05:50:24 +0000223/// SPARC-specific DAG, ready for instruction scheduling.
224///
Dan Gohman2c836cf2008-10-03 16:55:19 +0000225FunctionPass *llvm::createSparcISelDag(SparcTargetMachine &TM) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000226 return new SparcDAGToDAGISel(TM);
227}