blob: e1b9b59b4095fcb8887e55077382a4e5b05241fb [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 Lattnerd23405e2008-03-17 03:21:36 +000014#include "SparcISelLowering.h"
Chris Lattner7c90f732006-02-05 05:50:24 +000015#include "SparcTargetMachine.h"
Chris Lattner420736d2006-03-25 06:47:10 +000016#include "llvm/Intrinsics.h"
Chris Lattner6c18b102005-12-17 07:47:01 +000017#include "llvm/CodeGen/SelectionDAGISel.h"
Chris Lattner3d62d782008-02-03 05:43:57 +000018#include "llvm/Support/Compiler.h"
Chris Lattner6c18b102005-12-17 07:47:01 +000019#include "llvm/Support/Debug.h"
Torok Edwindac237e2009-07-08 20:53:28 +000020#include "llvm/Support/ErrorHandling.h"
21#include "llvm/Support/raw_ostream.h"
Chris Lattner6c18b102005-12-17 07:47:01 +000022using namespace llvm;
23
24//===----------------------------------------------------------------------===//
Chris Lattner6c18b102005-12-17 07:47:01 +000025// Instruction Selector Implementation
26//===----------------------------------------------------------------------===//
27
28//===--------------------------------------------------------------------===//
Chris Lattner7c90f732006-02-05 05:50:24 +000029/// SparcDAGToDAGISel - SPARC specific code to select SPARC machine
Chris Lattner6c18b102005-12-17 07:47:01 +000030/// instructions for SelectionDAG operations.
31///
32namespace {
Chris Lattner7c90f732006-02-05 05:50:24 +000033class SparcDAGToDAGISel : public SelectionDAGISel {
Chris Lattner76afdc92006-01-30 05:35:57 +000034 /// Subtarget - Keep a pointer to the Sparc Subtarget around so that we can
35 /// make the right decision when generating code for different targets.
Chris Lattner7c90f732006-02-05 05:50:24 +000036 const SparcSubtarget &Subtarget;
Chris Lattner6c18b102005-12-17 07:47:01 +000037public:
Dan Gohmanda8ac5f2008-10-03 16:55:19 +000038 explicit SparcDAGToDAGISel(SparcTargetMachine &TM)
Dan Gohman79ce2762009-01-15 19:20:50 +000039 : SelectionDAGISel(TM),
Chris Lattner7c90f732006-02-05 05:50:24 +000040 Subtarget(TM.getSubtarget<SparcSubtarget>()) {
Chris Lattner76afdc92006-01-30 05:35:57 +000041 }
Chris Lattner6c18b102005-12-17 07:47:01 +000042
Dan Gohman475871a2008-07-27 21:46:04 +000043 SDNode *Select(SDValue Op);
Chris Lattner6c18b102005-12-17 07:47:01 +000044
Chris Lattnerbc83fd92005-12-17 20:04:49 +000045 // Complex Pattern Selectors.
Dan Gohman475871a2008-07-27 21:46:04 +000046 bool SelectADDRrr(SDValue Op, SDValue N, SDValue &R1, SDValue &R2);
47 bool SelectADDRri(SDValue Op, SDValue N, SDValue &Base,
48 SDValue &Offset);
Anton Korobeynikova43e51d2008-10-10 10:14:15 +000049
Anton Korobeynikov2fcfd832008-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
Evan Chengdb8d56b2008-06-30 20:45:06 +000056 /// InstructionSelect - This callback is invoked by
Chris Lattner6c18b102005-12-17 07:47:01 +000057 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +000058 virtual void InstructionSelect();
Anton Korobeynikova43e51d2008-10-10 10:14:15 +000059
Chris Lattner6c18b102005-12-17 07:47:01 +000060 virtual const char *getPassName() const {
Chris Lattner7c90f732006-02-05 05:50:24 +000061 return "SPARC DAG->DAG Pattern Instruction Selection";
Anton Korobeynikova43e51d2008-10-10 10:14:15 +000062 }
63
Chris Lattner6c18b102005-12-17 07:47:01 +000064 // Include the pieces autogenerated from the target description.
Chris Lattner7c90f732006-02-05 05:50:24 +000065#include "SparcGenDAGISel.inc"
Chris Lattner6c18b102005-12-17 07:47:01 +000066};
67} // end anonymous namespace
68
Evan Chengdb8d56b2008-06-30 20:45:06 +000069/// InstructionSelect - This callback is invoked by
Chris Lattner6c18b102005-12-17 07:47:01 +000070/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +000071void SparcDAGToDAGISel::InstructionSelect() {
Chris Lattner6c18b102005-12-17 07:47:01 +000072 DEBUG(BB->dump());
Anton Korobeynikova43e51d2008-10-10 10:14:15 +000073
Chris Lattner6c18b102005-12-17 07:47:01 +000074 // Select target instructions for the DAG.
David Greene8ad4c002008-10-27 21:56:29 +000075 SelectRoot(*CurDAG);
Dan Gohmanf350b272008-08-23 02:25:05 +000076 CurDAG->RemoveDeadNodes();
Chris Lattner6c18b102005-12-17 07:47:01 +000077}
78
Dan Gohman475871a2008-07-27 21:46:04 +000079bool SparcDAGToDAGISel::SelectADDRri(SDValue Op, SDValue Addr,
80 SDValue &Base, SDValue &Offset) {
Chris Lattnerd5aae052005-12-18 07:09:06 +000081 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
82 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
Chris Lattner8fa54dc2005-12-18 06:59:57 +000083 Offset = CurDAG->getTargetConstant(0, MVT::i32);
84 return true;
85 }
Bill Wendling056292f2008-09-16 21:48:12 +000086 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
Chris Lattnerad7a3e62006-02-10 07:35:42 +000087 Addr.getOpcode() == ISD::TargetGlobalAddress)
88 return false; // direct calls.
Anton Korobeynikova43e51d2008-10-10 10:14:15 +000089
Chris Lattner8fa54dc2005-12-18 06:59:57 +000090 if (Addr.getOpcode() == ISD::ADD) {
91 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
92 if (Predicate_simm13(CN)) {
Anton Korobeynikova43e51d2008-10-10 10:14:15 +000093 if (FrameIndexSDNode *FIN =
Chris Lattnerd5aae052005-12-18 07:09:06 +000094 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
Chris Lattner8fa54dc2005-12-18 06:59:57 +000095 // Constant offset from frame ref.
Chris Lattnerd5aae052005-12-18 07:09:06 +000096 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
Chris Lattner8fa54dc2005-12-18 06:59:57 +000097 } else {
Chris Lattnerc26017a2006-02-05 08:35:50 +000098 Base = Addr.getOperand(0);
Chris Lattner8fa54dc2005-12-18 06:59:57 +000099 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000100 Offset = CurDAG->getTargetConstant(CN->getZExtValue(), MVT::i32);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000101 return true;
102 }
103 }
Chris Lattner7c90f732006-02-05 05:50:24 +0000104 if (Addr.getOperand(0).getOpcode() == SPISD::Lo) {
Chris Lattnerc26017a2006-02-05 08:35:50 +0000105 Base = Addr.getOperand(1);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000106 Offset = Addr.getOperand(0).getOperand(0);
107 return true;
108 }
Chris Lattner7c90f732006-02-05 05:50:24 +0000109 if (Addr.getOperand(1).getOpcode() == SPISD::Lo) {
Chris Lattnerc26017a2006-02-05 08:35:50 +0000110 Base = Addr.getOperand(0);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000111 Offset = Addr.getOperand(1).getOperand(0);
112 return true;
113 }
114 }
Chris Lattnerc26017a2006-02-05 08:35:50 +0000115 Base = Addr;
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000116 Offset = CurDAG->getTargetConstant(0, MVT::i32);
117 return true;
118}
119
Dan Gohman475871a2008-07-27 21:46:04 +0000120bool SparcDAGToDAGISel::SelectADDRrr(SDValue Op, SDValue Addr,
121 SDValue &R1, SDValue &R2) {
Chris Lattnerad7a3e62006-02-10 07:35:42 +0000122 if (Addr.getOpcode() == ISD::FrameIndex) return false;
Bill Wendling056292f2008-09-16 21:48:12 +0000123 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
Chris Lattnerad7a3e62006-02-10 07:35:42 +0000124 Addr.getOpcode() == ISD::TargetGlobalAddress)
125 return false; // direct calls.
Anton Korobeynikova43e51d2008-10-10 10:14:15 +0000126
Chris Lattner9034b882005-12-17 21:25:27 +0000127 if (Addr.getOpcode() == ISD::ADD) {
128 if (isa<ConstantSDNode>(Addr.getOperand(1)) &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000129 Predicate_simm13(Addr.getOperand(1).getNode()))
Chris Lattner9034b882005-12-17 21:25:27 +0000130 return false; // Let the reg+imm pattern catch this!
Chris Lattner7c90f732006-02-05 05:50:24 +0000131 if (Addr.getOperand(0).getOpcode() == SPISD::Lo ||
132 Addr.getOperand(1).getOpcode() == SPISD::Lo)
Chris Lattnere1389ad2005-12-18 02:27:00 +0000133 return false; // Let the reg+imm pattern catch this!
Chris Lattnerc26017a2006-02-05 08:35:50 +0000134 R1 = Addr.getOperand(0);
135 R2 = Addr.getOperand(1);
Chris Lattner9034b882005-12-17 21:25:27 +0000136 return true;
137 }
138
Chris Lattnerc26017a2006-02-05 08:35:50 +0000139 R1 = Addr;
Chris Lattner7c90f732006-02-05 05:50:24 +0000140 R2 = CurDAG->getRegister(SP::G0, MVT::i32);
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000141 return true;
142}
143
Dan Gohman475871a2008-07-27 21:46:04 +0000144SDNode *SparcDAGToDAGISel::Select(SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000145 SDNode *N = Op.getNode();
Dale Johannesenf5f5dce2009-02-06 19:16:40 +0000146 DebugLoc dl = N->getDebugLoc();
Dan Gohmane8be6c62008-07-17 19:10:17 +0000147 if (N->isMachineOpcode())
Evan Cheng64a752f2006-08-11 09:08:15 +0000148 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +0000149
Chris Lattner6c18b102005-12-17 07:47:01 +0000150 switch (N->getOpcode()) {
151 default: break;
Chris Lattner7087e572005-12-17 22:39:19 +0000152 case ISD::SDIV:
153 case ISD::UDIV: {
154 // FIXME: should use a custom expander to expose the SRA to the dag.
Dan Gohman475871a2008-07-27 21:46:04 +0000155 SDValue DivLHS = N->getOperand(0);
156 SDValue DivRHS = N->getOperand(1);
Anton Korobeynikova43e51d2008-10-10 10:14:15 +0000157
Chris Lattner7087e572005-12-17 22:39:19 +0000158 // Set the Y register to the high-part.
Dan Gohman475871a2008-07-27 21:46:04 +0000159 SDValue TopPart;
Chris Lattner7087e572005-12-17 22:39:19 +0000160 if (N->getOpcode() == ISD::SDIV) {
Dale Johannesenf5f5dce2009-02-06 19:16:40 +0000161 TopPart = SDValue(CurDAG->getTargetNode(SP::SRAri, dl, MVT::i32, DivLHS,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000162 CurDAG->getTargetConstant(31, MVT::i32)), 0);
Chris Lattner7087e572005-12-17 22:39:19 +0000163 } else {
Chris Lattner7c90f732006-02-05 05:50:24 +0000164 TopPart = CurDAG->getRegister(SP::G0, MVT::i32);
Chris Lattner7087e572005-12-17 22:39:19 +0000165 }
Dale Johannesenf5f5dce2009-02-06 19:16:40 +0000166 TopPart = SDValue(CurDAG->getTargetNode(SP::WRYrr, dl, MVT::Flag, TopPart,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000167 CurDAG->getRegister(SP::G0, MVT::i32)), 0);
Chris Lattner7087e572005-12-17 22:39:19 +0000168
169 // FIXME: Handle div by immediate.
Chris Lattner7c90f732006-02-05 05:50:24 +0000170 unsigned Opcode = N->getOpcode() == ISD::SDIV ? SP::SDIVrr : SP::UDIVrr;
Evan Cheng23329f52006-08-16 07:30:09 +0000171 return CurDAG->SelectNodeTo(N, Opcode, MVT::i32, DivLHS, DivRHS,
Evan Cheng95514ba2006-08-26 08:00:10 +0000172 TopPart);
Anton Korobeynikova43e51d2008-10-10 10:14:15 +0000173 }
Chris Lattneree3d5fb2005-12-17 22:30:00 +0000174 case ISD::MULHU:
175 case ISD::MULHS: {
Chris Lattner7087e572005-12-17 22:39:19 +0000176 // FIXME: Handle mul by immediate.
Dan Gohman475871a2008-07-27 21:46:04 +0000177 SDValue MulLHS = N->getOperand(0);
178 SDValue MulRHS = N->getOperand(1);
Chris Lattner7c90f732006-02-05 05:50:24 +0000179 unsigned Opcode = N->getOpcode() == ISD::MULHU ? SP::UMULrr : SP::SMULrr;
Dale Johannesenf5f5dce2009-02-06 19:16:40 +0000180 SDNode *Mul = CurDAG->getTargetNode(Opcode, dl, MVT::i32, MVT::Flag,
Chris Lattnerad7a3e62006-02-10 07:35:42 +0000181 MulLHS, MulRHS);
Chris Lattneree3d5fb2005-12-17 22:30:00 +0000182 // The high part is in the Y register.
Dan Gohman475871a2008-07-27 21:46:04 +0000183 return CurDAG->SelectNodeTo(N, SP::RDY, MVT::i32, SDValue(Mul, 1));
Evan Cheng64a752f2006-08-11 09:08:15 +0000184 return NULL;
Chris Lattneree3d5fb2005-12-17 22:30:00 +0000185 }
Chris Lattner6c18b102005-12-17 07:47:01 +0000186 }
Anton Korobeynikova43e51d2008-10-10 10:14:15 +0000187
Evan Cheng9ade2182006-08-26 05:34:46 +0000188 return SelectCode(Op);
Chris Lattner6c18b102005-12-17 07:47:01 +0000189}
190
191
Anton Korobeynikov2fcfd832008-10-10 10:14:47 +0000192/// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
193/// inline asm expressions.
194bool
195SparcDAGToDAGISel::SelectInlineAsmMemoryOperand(const SDValue &Op,
196 char ConstraintCode,
197 std::vector<SDValue> &OutOps) {
198 SDValue Op0, Op1;
199 switch (ConstraintCode) {
200 default: return true;
201 case 'm': // memory
202 if (!SelectADDRrr(Op, Op, Op0, Op1))
203 SelectADDRri(Op, Op, Op0, Op1);
204 break;
205 }
206
207 OutOps.push_back(Op0);
208 OutOps.push_back(Op1);
209 return false;
210}
211
Anton Korobeynikova43e51d2008-10-10 10:14:15 +0000212/// createSparcISelDag - This pass converts a legalized DAG into a
Chris Lattner4dcfaac2006-01-26 07:22:22 +0000213/// SPARC-specific DAG, ready for instruction scheduling.
Chris Lattner6c18b102005-12-17 07:47:01 +0000214///
Dan Gohmanda8ac5f2008-10-03 16:55:19 +0000215FunctionPass *llvm::createSparcISelDag(SparcTargetMachine &TM) {
Chris Lattner7c90f732006-02-05 05:50:24 +0000216 return new SparcDAGToDAGISel(TM);
Chris Lattner6c18b102005-12-17 07:47:01 +0000217}