blob: c004ad9e626599c97e42bdb10b54cdefd7534a89 [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);
Eric Christopherb17140d2014-10-08 07:32:17 +000070 return CurDAG->getRegister(GlobalBaseReg, TLI->getPointerTy()).getNode();
Chris Lattner840c7002009-09-15 17:46:24 +000071}
72
Chris Lattner0e023ea2010-09-21 20:31:19 +000073bool SparcDAGToDAGISel::SelectADDRri(SDValue Addr,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000074 SDValue &Base, SDValue &Offset) {
Chris Lattner158e1f52006-02-05 05:50:24 +000075 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
Eric Christopherb17140d2014-10-08 07:32:17 +000076 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), TLI->getPointerTy());
Owen Anderson9f944592009-08-11 20:47:22 +000077 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Chris Lattner158e1f52006-02-05 05:50:24 +000078 return true;
79 }
Bill Wendling24c79f22008-09-16 21:48:12 +000080 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +000081 Addr.getOpcode() == ISD::TargetGlobalAddress ||
82 Addr.getOpcode() == ISD::TargetGlobalTLSAddress)
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +000083 return false; // direct calls.
Anton Korobeynikov1f9487b2008-10-10 10:14:15 +000084
Chris Lattner158e1f52006-02-05 05:50:24 +000085 if (Addr.getOpcode() == ISD::ADD) {
86 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
Jakob Stoklund Olesenf02b4a62010-08-17 18:17:12 +000087 if (isInt<13>(CN->getSExtValue())) {
Anton Korobeynikov1f9487b2008-10-10 10:14:15 +000088 if (FrameIndexSDNode *FIN =
Chris Lattner158e1f52006-02-05 05:50:24 +000089 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
90 // Constant offset from frame ref.
Eric Christopherb17140d2014-10-08 07:32:17 +000091 Base =
92 CurDAG->getTargetFrameIndex(FIN->getIndex(), TLI->getPointerTy());
Chris Lattner158e1f52006-02-05 05:50:24 +000093 } else {
Chris Lattner463fa702006-02-05 08:35:50 +000094 Base = Addr.getOperand(0);
Chris Lattner158e1f52006-02-05 05:50:24 +000095 }
Owen Anderson9f944592009-08-11 20:47:22 +000096 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), MVT::i32);
Chris Lattner158e1f52006-02-05 05:50:24 +000097 return true;
98 }
99 }
100 if (Addr.getOperand(0).getOpcode() == SPISD::Lo) {
Chris Lattner463fa702006-02-05 08:35:50 +0000101 Base = Addr.getOperand(1);
Chris Lattner158e1f52006-02-05 05:50:24 +0000102 Offset = Addr.getOperand(0).getOperand(0);
103 return true;
104 }
105 if (Addr.getOperand(1).getOpcode() == SPISD::Lo) {
Chris Lattner463fa702006-02-05 08:35:50 +0000106 Base = Addr.getOperand(0);
Chris Lattner158e1f52006-02-05 05:50:24 +0000107 Offset = Addr.getOperand(1).getOperand(0);
108 return true;
109 }
110 }
Chris Lattner463fa702006-02-05 08:35:50 +0000111 Base = Addr;
Owen Anderson9f944592009-08-11 20:47:22 +0000112 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Chris Lattner158e1f52006-02-05 05:50:24 +0000113 return true;
114}
115
Chris Lattner0e023ea2010-09-21 20:31:19 +0000116bool SparcDAGToDAGISel::SelectADDRrr(SDValue Addr, SDValue &R1, SDValue &R2) {
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000117 if (Addr.getOpcode() == ISD::FrameIndex) return false;
Bill Wendling24c79f22008-09-16 21:48:12 +0000118 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
Venkatraman Govindarajucb1dca62013-09-22 06:48:52 +0000119 Addr.getOpcode() == ISD::TargetGlobalAddress ||
120 Addr.getOpcode() == ISD::TargetGlobalTLSAddress)
Chris Lattnerfcb8a3a2006-02-10 07:35:42 +0000121 return false; // direct calls.
Anton Korobeynikov1f9487b2008-10-10 10:14:15 +0000122
Chris Lattner158e1f52006-02-05 05:50:24 +0000123 if (Addr.getOpcode() == ISD::ADD) {
Jakob Stoklund Olesenf02b4a62010-08-17 18:17:12 +0000124 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
125 if (isInt<13>(CN->getSExtValue()))
126 return false; // Let the reg+imm pattern catch this!
Chris Lattner158e1f52006-02-05 05:50:24 +0000127 if (Addr.getOperand(0).getOpcode() == SPISD::Lo ||
128 Addr.getOperand(1).getOpcode() == SPISD::Lo)
129 return false; // Let the reg+imm pattern catch this!
Chris Lattner463fa702006-02-05 08:35:50 +0000130 R1 = Addr.getOperand(0);
131 R2 = Addr.getOperand(1);
Chris Lattner158e1f52006-02-05 05:50:24 +0000132 return true;
133 }
134
Chris Lattner463fa702006-02-05 08:35:50 +0000135 R1 = Addr;
Eric Christopherb17140d2014-10-08 07:32:17 +0000136 R2 = CurDAG->getRegister(SP::G0, TLI->getPointerTy());
Chris Lattner158e1f52006-02-05 05:50:24 +0000137 return true;
138}
139
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000140SDNode *SparcDAGToDAGISel::Select(SDNode *N) {
Andrew Trickef9de2a2013-05-25 02:42:55 +0000141 SDLoc dl(N);
Tim Northover31d093c2013-09-22 08:21:56 +0000142 if (N->isMachineOpcode()) {
143 N->setNodeId(-1);
Craig Topper062a2ba2014-04-25 05:30:21 +0000144 return nullptr; // Already selected.
Tim Northover31d093c2013-09-22 08:21:56 +0000145 }
Evan Cheng6dc90ca2006-02-09 00:37:58 +0000146
Chris Lattner158e1f52006-02-05 05:50:24 +0000147 switch (N->getOpcode()) {
148 default: break;
Chris Lattner840c7002009-09-15 17:46:24 +0000149 case SPISD::GLOBAL_BASE_REG:
150 return getGlobalBaseReg();
151
Chris Lattner158e1f52006-02-05 05:50:24 +0000152 case ISD::SDIV:
153 case ISD::UDIV: {
Jakob Stoklund Olesen73d17392013-04-16 02:57:02 +0000154 // sdivx / udivx handle 64-bit divides.
155 if (N->getValueType(0) == MVT::i64)
156 break;
Chris Lattner158e1f52006-02-05 05:50:24 +0000157 // FIXME: should use a custom expander to expose the SRA to the dag.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000158 SDValue DivLHS = N->getOperand(0);
159 SDValue DivRHS = N->getOperand(1);
Anton Korobeynikov1f9487b2008-10-10 10:14:15 +0000160
Chris Lattner158e1f52006-02-05 05:50:24 +0000161 // Set the Y register to the high-part.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000162 SDValue TopPart;
Chris Lattner158e1f52006-02-05 05:50:24 +0000163 if (N->getOpcode() == ISD::SDIV) {
Dan Gohman32f71d72009-09-25 18:54:59 +0000164 TopPart = SDValue(CurDAG->getMachineNode(SP::SRAri, dl, MVT::i32, DivLHS,
Owen Anderson9f944592009-08-11 20:47:22 +0000165 CurDAG->getTargetConstant(31, MVT::i32)), 0);
Chris Lattner158e1f52006-02-05 05:50:24 +0000166 } else {
Owen Anderson9f944592009-08-11 20:47:22 +0000167 TopPart = CurDAG->getRegister(SP::G0, MVT::i32);
Chris Lattner158e1f52006-02-05 05:50:24 +0000168 }
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000169 TopPart = SDValue(CurDAG->getMachineNode(SP::WRYrr, dl, MVT::Glue, TopPart,
Owen Anderson9f944592009-08-11 20:47:22 +0000170 CurDAG->getRegister(SP::G0, MVT::i32)), 0);
Chris Lattner158e1f52006-02-05 05:50:24 +0000171
172 // FIXME: Handle div by immediate.
173 unsigned Opcode = N->getOpcode() == ISD::SDIV ? SP::SDIVrr : SP::UDIVrr;
Owen Anderson9f944592009-08-11 20:47:22 +0000174 return CurDAG->SelectNodeTo(N, Opcode, MVT::i32, DivLHS, DivRHS,
Evan Cheng34b70ee2006-08-26 08:00:10 +0000175 TopPart);
Anton Korobeynikov1f9487b2008-10-10 10:14:15 +0000176 }
Chris Lattner158e1f52006-02-05 05:50:24 +0000177 case ISD::MULHU:
178 case ISD::MULHS: {
179 // FIXME: Handle mul by immediate.
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000180 SDValue MulLHS = N->getOperand(0);
181 SDValue MulRHS = N->getOperand(1);
Chris Lattner158e1f52006-02-05 05:50:24 +0000182 unsigned Opcode = N->getOpcode() == ISD::MULHU ? SP::UMULrr : SP::SMULrr;
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000183 SDNode *Mul = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::Glue,
Dan Gohman32f71d72009-09-25 18:54:59 +0000184 MulLHS, MulRHS);
Chris Lattner158e1f52006-02-05 05:50:24 +0000185 // The high part is in the Y register.
Owen Anderson9f944592009-08-11 20:47:22 +0000186 return CurDAG->SelectNodeTo(N, SP::RDY, MVT::i32, SDValue(Mul, 1));
Chris Lattner158e1f52006-02-05 05:50:24 +0000187 }
Chris Lattner158e1f52006-02-05 05:50:24 +0000188 }
Anton Korobeynikov1f9487b2008-10-10 10:14:15 +0000189
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000190 return SelectCode(N);
Chris Lattner158e1f52006-02-05 05:50:24 +0000191}
192
193
Anton Korobeynikov9aaaa402008-10-10 10:14:47 +0000194/// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
195/// inline asm expressions.
196bool
197SparcDAGToDAGISel::SelectInlineAsmMemoryOperand(const SDValue &Op,
Daniel Sanders60f1db02015-03-13 12:45:09 +0000198 unsigned ConstraintID,
Anton Korobeynikov9aaaa402008-10-10 10:14:47 +0000199 std::vector<SDValue> &OutOps) {
200 SDValue Op0, Op1;
Daniel Sanders60f1db02015-03-13 12:45:09 +0000201 switch (ConstraintID) {
Anton Korobeynikov9aaaa402008-10-10 10:14:47 +0000202 default: return true;
Daniel Sanders60f1db02015-03-13 12:45:09 +0000203 case InlineAsm::Constraint_m: // memory
Chris Lattner0e023ea2010-09-21 20:31:19 +0000204 if (!SelectADDRrr(Op, Op0, Op1))
205 SelectADDRri(Op, Op0, Op1);
Anton Korobeynikov9aaaa402008-10-10 10:14:47 +0000206 break;
207 }
208
209 OutOps.push_back(Op0);
210 OutOps.push_back(Op1);
211 return false;
212}
213
Anton Korobeynikov1f9487b2008-10-10 10:14:15 +0000214/// createSparcISelDag - This pass converts a legalized DAG into a
Chris Lattner158e1f52006-02-05 05:50:24 +0000215/// SPARC-specific DAG, ready for instruction scheduling.
216///
Dan Gohman2c836cf2008-10-03 16:55:19 +0000217FunctionPass *llvm::createSparcISelDag(SparcTargetMachine &TM) {
Chris Lattner158e1f52006-02-05 05:50:24 +0000218 return new SparcDAGToDAGISel(TM);
219}