blob: 4ea94c4cb560f6ebe783990d181f6c360e561b7b [file] [log] [blame]
Chris Lattner7c90f732006-02-05 05:50:24 +00001//===-- SparcISelDAGToDAG.cpp - A dag to dag inst selector for Sparc ------===//
Chris Lattner6c18b102005-12-17 07:47:01 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Lattner6c18b102005-12-17 07:47:01 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner7c90f732006-02-05 05:50:24 +000010// This file defines an instruction selector for the SPARC target.
Chris Lattner6c18b102005-12-17 07:47:01 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattner7c90f732006-02-05 05:50:24 +000014#include "SparcTargetMachine.h"
Chris Lattner420736d2006-03-25 06:47:10 +000015#include "llvm/Intrinsics.h"
Chris Lattner6c18b102005-12-17 07:47:01 +000016#include "llvm/CodeGen/SelectionDAGISel.h"
Chris Lattner3d62d782008-02-03 05:43:57 +000017#include "llvm/Support/Compiler.h"
Chris Lattner6c18b102005-12-17 07:47:01 +000018#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000019#include "llvm/Support/ErrorHandling.h"
20#include "llvm/Support/raw_ostream.h"
Chris Lattner6c18b102005-12-17 07:47:01 +000021using namespace llvm;
22
23//===----------------------------------------------------------------------===//
Chris Lattner6c18b102005-12-17 07:47:01 +000024// Instruction Selector Implementation
25//===----------------------------------------------------------------------===//
26
27//===--------------------------------------------------------------------===//
Chris Lattner7c90f732006-02-05 05:50:24 +000028/// SparcDAGToDAGISel - SPARC specific code to select SPARC machine
Chris Lattner6c18b102005-12-17 07:47:01 +000029/// instructions for SelectionDAG operations.
30///
31namespace {
Chris Lattner7c90f732006-02-05 05:50:24 +000032class SparcDAGToDAGISel : public SelectionDAGISel {
Chris Lattner76afdc92006-01-30 05:35:57 +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.
Chris Lattner7c90f732006-02-05 05:50:24 +000035 const SparcSubtarget &Subtarget;
Chris Lattnerdb486a62009-09-15 17:46:24 +000036 SparcTargetMachine& TM;
Chris Lattner6c18b102005-12-17 07:47:01 +000037public:
Chris Lattnerdb486a62009-09-15 17:46:24 +000038 explicit SparcDAGToDAGISel(SparcTargetMachine &tm)
39 : SelectionDAGISel(tm),
40 Subtarget(tm.getSubtarget<SparcSubtarget>()),
41 TM(tm) {
Chris Lattner76afdc92006-01-30 05:35:57 +000042 }
Chris Lattner6c18b102005-12-17 07:47:01 +000043
Dan Gohmaneeb3a002010-01-05 01:24:18 +000044 SDNode *Select(SDNode *N);
Chris Lattner6c18b102005-12-17 07:47:01 +000045
Chris Lattnerbc83fd92005-12-17 20:04:49 +000046 // Complex Pattern Selectors.
Dan Gohmaneeb3a002010-01-05 01:24:18 +000047 bool SelectADDRrr(SDNode *Op, SDValue N, SDValue &R1, SDValue &R2);
48 bool SelectADDRri(SDNode *Op, SDValue N, SDValue &Base,
Dan Gohman475871a2008-07-27 21:46:04 +000049 SDValue &Offset);
Anton Korobeynikova43e51d2008-10-10 10:14:15 +000050
Anton Korobeynikov2fcfd832008-10-10 10:14:47 +000051 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
52 /// inline asm expressions.
53 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
54 char ConstraintCode,
55 std::vector<SDValue> &OutOps);
56
Chris Lattner6c18b102005-12-17 07:47:01 +000057 virtual const char *getPassName() const {
Chris Lattner7c90f732006-02-05 05:50:24 +000058 return "SPARC DAG->DAG Pattern Instruction Selection";
Anton Korobeynikova43e51d2008-10-10 10:14:15 +000059 }
60
Chris Lattner6c18b102005-12-17 07:47:01 +000061 // Include the pieces autogenerated from the target description.
Chris Lattner7c90f732006-02-05 05:50:24 +000062#include "SparcGenDAGISel.inc"
Chris Lattnerdb486a62009-09-15 17:46:24 +000063
64private:
65 SDNode* getGlobalBaseReg();
Chris Lattner6c18b102005-12-17 07:47:01 +000066};
67} // end anonymous namespace
68
Chris Lattnerdb486a62009-09-15 17:46:24 +000069SDNode* SparcDAGToDAGISel::getGlobalBaseReg() {
Chris Lattnerdb486a62009-09-15 17:46:24 +000070 unsigned GlobalBaseReg = TM.getInstrInfo()->getGlobalBaseReg(MF);
71 return CurDAG->getRegister(GlobalBaseReg, TLI.getPointerTy()).getNode();
72}
73
Dan Gohmaneeb3a002010-01-05 01:24:18 +000074bool SparcDAGToDAGISel::SelectADDRri(SDNode *Op, SDValue Addr,
Dan Gohman475871a2008-07-27 21:46:04 +000075 SDValue &Base, SDValue &Offset) {
Chris Lattnerd5aae052005-12-18 07:09:06 +000076 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
Owen Anderson825b72b2009-08-11 20:47:22 +000077 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
78 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Chris Lattner8fa54dc2005-12-18 06:59:57 +000079 return true;
80 }
Bill Wendling056292f2008-09-16 21:48:12 +000081 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
Chris Lattnerad7a3e62006-02-10 07:35:42 +000082 Addr.getOpcode() == ISD::TargetGlobalAddress)
83 return false; // direct calls.
Anton Korobeynikova43e51d2008-10-10 10:14:15 +000084
Chris Lattner8fa54dc2005-12-18 06:59:57 +000085 if (Addr.getOpcode() == ISD::ADD) {
86 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
Jakob Stoklund Olesen4bb862d2010-08-17 18:17:12 +000087 if (isInt<13>(CN->getSExtValue())) {
Anton Korobeynikova43e51d2008-10-10 10:14:15 +000088 if (FrameIndexSDNode *FIN =
Chris Lattnerd5aae052005-12-18 07:09:06 +000089 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
Chris Lattner8fa54dc2005-12-18 06:59:57 +000090 // Constant offset from frame ref.
Owen Anderson825b72b2009-08-11 20:47:22 +000091 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
Chris Lattner8fa54dc2005-12-18 06:59:57 +000092 } else {
Chris Lattnerc26017a2006-02-05 08:35:50 +000093 Base = Addr.getOperand(0);
Chris Lattner8fa54dc2005-12-18 06:59:57 +000094 }
Owen Anderson825b72b2009-08-11 20:47:22 +000095 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), MVT::i32);
Chris Lattner8fa54dc2005-12-18 06:59:57 +000096 return true;
97 }
98 }
Chris Lattner7c90f732006-02-05 05:50:24 +000099 if (Addr.getOperand(0).getOpcode() == SPISD::Lo) {
Chris Lattnerc26017a2006-02-05 08:35:50 +0000100 Base = Addr.getOperand(1);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000101 Offset = Addr.getOperand(0).getOperand(0);
102 return true;
103 }
Chris Lattner7c90f732006-02-05 05:50:24 +0000104 if (Addr.getOperand(1).getOpcode() == SPISD::Lo) {
Chris Lattnerc26017a2006-02-05 08:35:50 +0000105 Base = Addr.getOperand(0);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000106 Offset = Addr.getOperand(1).getOperand(0);
107 return true;
108 }
109 }
Chris Lattnerc26017a2006-02-05 08:35:50 +0000110 Base = Addr;
Owen Anderson825b72b2009-08-11 20:47:22 +0000111 Offset = CurDAG->getTargetConstant(0, MVT::i32);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000112 return true;
113}
114
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000115bool SparcDAGToDAGISel::SelectADDRrr(SDNode *Op, SDValue Addr,
Dan Gohman475871a2008-07-27 21:46:04 +0000116 SDValue &R1, SDValue &R2) {
Chris Lattnerad7a3e62006-02-10 07:35:42 +0000117 if (Addr.getOpcode() == ISD::FrameIndex) return false;
Bill Wendling056292f2008-09-16 21:48:12 +0000118 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
Chris Lattnerad7a3e62006-02-10 07:35:42 +0000119 Addr.getOpcode() == ISD::TargetGlobalAddress)
120 return false; // direct calls.
Anton Korobeynikova43e51d2008-10-10 10:14:15 +0000121
Chris Lattner9034b882005-12-17 21:25:27 +0000122 if (Addr.getOpcode() == ISD::ADD) {
Jakob Stoklund Olesen4bb862d2010-08-17 18:17:12 +0000123 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))
124 if (isInt<13>(CN->getSExtValue()))
125 return false; // Let the reg+imm pattern catch this!
Chris Lattner7c90f732006-02-05 05:50:24 +0000126 if (Addr.getOperand(0).getOpcode() == SPISD::Lo ||
127 Addr.getOperand(1).getOpcode() == SPISD::Lo)
Chris Lattnere1389ad2005-12-18 02:27:00 +0000128 return false; // Let the reg+imm pattern catch this!
Chris Lattnerc26017a2006-02-05 08:35:50 +0000129 R1 = Addr.getOperand(0);
130 R2 = Addr.getOperand(1);
Chris Lattner9034b882005-12-17 21:25:27 +0000131 return true;
132 }
133
Chris Lattnerc26017a2006-02-05 08:35:50 +0000134 R1 = Addr;
Owen Anderson825b72b2009-08-11 20:47:22 +0000135 R2 = CurDAG->getRegister(SP::G0, MVT::i32);
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000136 return true;
137}
138
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000139SDNode *SparcDAGToDAGISel::Select(SDNode *N) {
Dale Johannesenf5f5dce2009-02-06 19:16:40 +0000140 DebugLoc dl = N->getDebugLoc();
Dan Gohmane8be6c62008-07-17 19:10:17 +0000141 if (N->isMachineOpcode())
Evan Cheng64a752f2006-08-11 09:08:15 +0000142 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +0000143
Chris Lattner6c18b102005-12-17 07:47:01 +0000144 switch (N->getOpcode()) {
145 default: break;
Chris Lattnerdb486a62009-09-15 17:46:24 +0000146 case SPISD::GLOBAL_BASE_REG:
147 return getGlobalBaseReg();
148
Chris Lattner7087e572005-12-17 22:39:19 +0000149 case ISD::SDIV:
150 case ISD::UDIV: {
151 // FIXME: should use a custom expander to expose the SRA to the dag.
Dan Gohman475871a2008-07-27 21:46:04 +0000152 SDValue DivLHS = N->getOperand(0);
153 SDValue DivRHS = N->getOperand(1);
Anton Korobeynikova43e51d2008-10-10 10:14:15 +0000154
Chris Lattner7087e572005-12-17 22:39:19 +0000155 // Set the Y register to the high-part.
Dan Gohman475871a2008-07-27 21:46:04 +0000156 SDValue TopPart;
Chris Lattner7087e572005-12-17 22:39:19 +0000157 if (N->getOpcode() == ISD::SDIV) {
Dan Gohman602b0c82009-09-25 18:54:59 +0000158 TopPart = SDValue(CurDAG->getMachineNode(SP::SRAri, dl, MVT::i32, DivLHS,
Owen Anderson825b72b2009-08-11 20:47:22 +0000159 CurDAG->getTargetConstant(31, MVT::i32)), 0);
Chris Lattner7087e572005-12-17 22:39:19 +0000160 } else {
Owen Anderson825b72b2009-08-11 20:47:22 +0000161 TopPart = CurDAG->getRegister(SP::G0, MVT::i32);
Chris Lattner7087e572005-12-17 22:39:19 +0000162 }
Dan Gohman602b0c82009-09-25 18:54:59 +0000163 TopPart = SDValue(CurDAG->getMachineNode(SP::WRYrr, dl, MVT::Flag, TopPart,
Owen Anderson825b72b2009-08-11 20:47:22 +0000164 CurDAG->getRegister(SP::G0, MVT::i32)), 0);
Chris Lattner7087e572005-12-17 22:39:19 +0000165
166 // FIXME: Handle div by immediate.
Chris Lattner7c90f732006-02-05 05:50:24 +0000167 unsigned Opcode = N->getOpcode() == ISD::SDIV ? SP::SDIVrr : SP::UDIVrr;
Owen Anderson825b72b2009-08-11 20:47:22 +0000168 return CurDAG->SelectNodeTo(N, Opcode, MVT::i32, DivLHS, DivRHS,
Evan Cheng95514ba2006-08-26 08:00:10 +0000169 TopPart);
Anton Korobeynikova43e51d2008-10-10 10:14:15 +0000170 }
Chris Lattneree3d5fb2005-12-17 22:30:00 +0000171 case ISD::MULHU:
172 case ISD::MULHS: {
Chris Lattner7087e572005-12-17 22:39:19 +0000173 // FIXME: Handle mul by immediate.
Dan Gohman475871a2008-07-27 21:46:04 +0000174 SDValue MulLHS = N->getOperand(0);
175 SDValue MulRHS = N->getOperand(1);
Chris Lattner7c90f732006-02-05 05:50:24 +0000176 unsigned Opcode = N->getOpcode() == ISD::MULHU ? SP::UMULrr : SP::SMULrr;
Dan Gohman602b0c82009-09-25 18:54:59 +0000177 SDNode *Mul = CurDAG->getMachineNode(Opcode, dl, MVT::i32, MVT::Flag,
178 MulLHS, MulRHS);
Chris Lattneree3d5fb2005-12-17 22:30:00 +0000179 // The high part is in the Y register.
Owen Anderson825b72b2009-08-11 20:47:22 +0000180 return CurDAG->SelectNodeTo(N, SP::RDY, MVT::i32, SDValue(Mul, 1));
Evan Cheng64a752f2006-08-11 09:08:15 +0000181 return NULL;
Chris Lattneree3d5fb2005-12-17 22:30:00 +0000182 }
Chris Lattner6c18b102005-12-17 07:47:01 +0000183 }
Anton Korobeynikova43e51d2008-10-10 10:14:15 +0000184
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000185 return SelectCode(N);
Chris Lattner6c18b102005-12-17 07:47:01 +0000186}
187
188
Anton Korobeynikov2fcfd832008-10-10 10:14:47 +0000189/// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
190/// inline asm expressions.
191bool
192SparcDAGToDAGISel::SelectInlineAsmMemoryOperand(const SDValue &Op,
193 char ConstraintCode,
194 std::vector<SDValue> &OutOps) {
195 SDValue Op0, Op1;
196 switch (ConstraintCode) {
197 default: return true;
198 case 'm': // memory
Dan Gohmaneeb3a002010-01-05 01:24:18 +0000199 if (!SelectADDRrr(Op.getNode(), Op, Op0, Op1))
200 SelectADDRri(Op.getNode(), Op, Op0, Op1);
Anton Korobeynikov2fcfd832008-10-10 10:14:47 +0000201 break;
202 }
203
204 OutOps.push_back(Op0);
205 OutOps.push_back(Op1);
206 return false;
207}
208
Anton Korobeynikova43e51d2008-10-10 10:14:15 +0000209/// createSparcISelDag - This pass converts a legalized DAG into a
Chris Lattner4dcfaac2006-01-26 07:22:22 +0000210/// SPARC-specific DAG, ready for instruction scheduling.
Chris Lattner6c18b102005-12-17 07:47:01 +0000211///
Dan Gohmanda8ac5f2008-10-03 16:55:19 +0000212FunctionPass *llvm::createSparcISelDag(SparcTargetMachine &TM) {
Chris Lattner7c90f732006-02-05 05:50:24 +0000213 return new SparcDAGToDAGISel(TM);
Chris Lattner6c18b102005-12-17 07:47:01 +0000214}