blob: f9baa3bfb8c121858a109a63249df067bc862372 [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"
Chris Lattner6c18b102005-12-17 07:47:01 +000020using namespace llvm;
21
22//===----------------------------------------------------------------------===//
Chris Lattner6c18b102005-12-17 07:47:01 +000023// Instruction Selector Implementation
24//===----------------------------------------------------------------------===//
25
26//===--------------------------------------------------------------------===//
Chris Lattner7c90f732006-02-05 05:50:24 +000027/// SparcDAGToDAGISel - SPARC specific code to select SPARC machine
Chris Lattner6c18b102005-12-17 07:47:01 +000028/// instructions for SelectionDAG operations.
29///
30namespace {
Chris Lattner7c90f732006-02-05 05:50:24 +000031class SparcDAGToDAGISel : public SelectionDAGISel {
32 SparcTargetLowering Lowering;
Chris Lattner76afdc92006-01-30 05:35:57 +000033
34 /// 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 Gohman1002c022008-07-07 18:00:37 +000038 explicit SparcDAGToDAGISel(TargetMachine &TM)
Chris Lattner7c90f732006-02-05 05:50:24 +000039 : SelectionDAGISel(Lowering), Lowering(TM),
40 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);
Chris Lattnerbc83fd92005-12-17 20:04:49 +000049
Evan Chengdb8d56b2008-06-30 20:45:06 +000050 /// InstructionSelect - This callback is invoked by
Chris Lattner6c18b102005-12-17 07:47:01 +000051 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +000052 virtual void InstructionSelect();
Chris Lattner6c18b102005-12-17 07:47:01 +000053
54 virtual const char *getPassName() const {
Chris Lattner7c90f732006-02-05 05:50:24 +000055 return "SPARC DAG->DAG Pattern Instruction Selection";
Chris Lattner6c18b102005-12-17 07:47:01 +000056 }
57
58 // Include the pieces autogenerated from the target description.
Chris Lattner7c90f732006-02-05 05:50:24 +000059#include "SparcGenDAGISel.inc"
Chris Lattner6c18b102005-12-17 07:47:01 +000060};
61} // end anonymous namespace
62
Evan Chengdb8d56b2008-06-30 20:45:06 +000063/// InstructionSelect - This callback is invoked by
Chris Lattner6c18b102005-12-17 07:47:01 +000064/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohmanf350b272008-08-23 02:25:05 +000065void SparcDAGToDAGISel::InstructionSelect() {
Chris Lattner6c18b102005-12-17 07:47:01 +000066 DEBUG(BB->dump());
67
68 // Select target instructions for the DAG.
Dan Gohmanad3460c2008-08-21 16:36:34 +000069 SelectRoot();
Dan Gohmanf350b272008-08-23 02:25:05 +000070 CurDAG->RemoveDeadNodes();
Chris Lattner6c18b102005-12-17 07:47:01 +000071}
72
Dan Gohman475871a2008-07-27 21:46:04 +000073bool SparcDAGToDAGISel::SelectADDRri(SDValue Op, SDValue Addr,
74 SDValue &Base, SDValue &Offset) {
Chris Lattnerd5aae052005-12-18 07:09:06 +000075 if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) {
76 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
Chris Lattner8fa54dc2005-12-18 06:59:57 +000077 Offset = CurDAG->getTargetConstant(0, MVT::i32);
78 return true;
79 }
Chris Lattnerad7a3e62006-02-10 07:35:42 +000080 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
81 Addr.getOpcode() == ISD::TargetGlobalAddress)
82 return false; // direct calls.
Chris Lattner8fa54dc2005-12-18 06:59:57 +000083
84 if (Addr.getOpcode() == ISD::ADD) {
85 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) {
86 if (Predicate_simm13(CN)) {
Chris Lattnerd5aae052005-12-18 07:09:06 +000087 if (FrameIndexSDNode *FIN =
88 dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) {
Chris Lattner8fa54dc2005-12-18 06:59:57 +000089 // Constant offset from frame ref.
Chris Lattnerd5aae052005-12-18 07:09:06 +000090 Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32);
Chris Lattner8fa54dc2005-12-18 06:59:57 +000091 } else {
Chris Lattnerc26017a2006-02-05 08:35:50 +000092 Base = Addr.getOperand(0);
Chris Lattner8fa54dc2005-12-18 06:59:57 +000093 }
94 Offset = CurDAG->getTargetConstant(CN->getValue(), MVT::i32);
95 return true;
96 }
97 }
Chris Lattner7c90f732006-02-05 05:50:24 +000098 if (Addr.getOperand(0).getOpcode() == SPISD::Lo) {
Chris Lattnerc26017a2006-02-05 08:35:50 +000099 Base = Addr.getOperand(1);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000100 Offset = Addr.getOperand(0).getOperand(0);
101 return true;
102 }
Chris Lattner7c90f732006-02-05 05:50:24 +0000103 if (Addr.getOperand(1).getOpcode() == SPISD::Lo) {
Chris Lattnerc26017a2006-02-05 08:35:50 +0000104 Base = Addr.getOperand(0);
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000105 Offset = Addr.getOperand(1).getOperand(0);
106 return true;
107 }
108 }
Chris Lattnerc26017a2006-02-05 08:35:50 +0000109 Base = Addr;
Chris Lattner8fa54dc2005-12-18 06:59:57 +0000110 Offset = CurDAG->getTargetConstant(0, MVT::i32);
111 return true;
112}
113
Dan Gohman475871a2008-07-27 21:46:04 +0000114bool SparcDAGToDAGISel::SelectADDRrr(SDValue Op, SDValue Addr,
115 SDValue &R1, SDValue &R2) {
Chris Lattnerad7a3e62006-02-10 07:35:42 +0000116 if (Addr.getOpcode() == ISD::FrameIndex) return false;
117 if (Addr.getOpcode() == ISD::TargetExternalSymbol ||
118 Addr.getOpcode() == ISD::TargetGlobalAddress)
119 return false; // direct calls.
120
Chris Lattner9034b882005-12-17 21:25:27 +0000121 if (Addr.getOpcode() == ISD::ADD) {
122 if (isa<ConstantSDNode>(Addr.getOperand(1)) &&
Gabor Greifba36cb52008-08-28 21:40:38 +0000123 Predicate_simm13(Addr.getOperand(1).getNode()))
Chris Lattner9034b882005-12-17 21:25:27 +0000124 return false; // Let the reg+imm pattern catch this!
Chris Lattner7c90f732006-02-05 05:50:24 +0000125 if (Addr.getOperand(0).getOpcode() == SPISD::Lo ||
126 Addr.getOperand(1).getOpcode() == SPISD::Lo)
Chris Lattnere1389ad2005-12-18 02:27:00 +0000127 return false; // Let the reg+imm pattern catch this!
Chris Lattnerc26017a2006-02-05 08:35:50 +0000128 R1 = Addr.getOperand(0);
129 R2 = Addr.getOperand(1);
Chris Lattner9034b882005-12-17 21:25:27 +0000130 return true;
131 }
132
Chris Lattnerc26017a2006-02-05 08:35:50 +0000133 R1 = Addr;
Chris Lattner7c90f732006-02-05 05:50:24 +0000134 R2 = CurDAG->getRegister(SP::G0, MVT::i32);
Chris Lattnerbc83fd92005-12-17 20:04:49 +0000135 return true;
136}
137
Dan Gohman475871a2008-07-27 21:46:04 +0000138SDNode *SparcDAGToDAGISel::Select(SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +0000139 SDNode *N = Op.getNode();
Dan Gohmane8be6c62008-07-17 19:10:17 +0000140 if (N->isMachineOpcode())
Evan Cheng64a752f2006-08-11 09:08:15 +0000141 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +0000142
Chris Lattner6c18b102005-12-17 07:47:01 +0000143 switch (N->getOpcode()) {
144 default: break;
Chris Lattner7087e572005-12-17 22:39:19 +0000145 case ISD::SDIV:
146 case ISD::UDIV: {
147 // FIXME: should use a custom expander to expose the SRA to the dag.
Dan Gohman475871a2008-07-27 21:46:04 +0000148 SDValue DivLHS = N->getOperand(0);
149 SDValue DivRHS = N->getOperand(1);
Evan Cheng6da2f322006-08-26 01:07:58 +0000150 AddToISelQueue(DivLHS);
151 AddToISelQueue(DivRHS);
Chris Lattner7087e572005-12-17 22:39:19 +0000152
153 // Set the Y register to the high-part.
Dan Gohman475871a2008-07-27 21:46:04 +0000154 SDValue TopPart;
Chris Lattner7087e572005-12-17 22:39:19 +0000155 if (N->getOpcode() == ISD::SDIV) {
Dan Gohman475871a2008-07-27 21:46:04 +0000156 TopPart = SDValue(CurDAG->getTargetNode(SP::SRAri, MVT::i32, DivLHS,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000157 CurDAG->getTargetConstant(31, MVT::i32)), 0);
Chris Lattner7087e572005-12-17 22:39:19 +0000158 } else {
Chris Lattner7c90f732006-02-05 05:50:24 +0000159 TopPart = CurDAG->getRegister(SP::G0, MVT::i32);
Chris Lattner7087e572005-12-17 22:39:19 +0000160 }
Dan Gohman475871a2008-07-27 21:46:04 +0000161 TopPart = SDValue(CurDAG->getTargetNode(SP::WRYrr, MVT::Flag, TopPart,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000162 CurDAG->getRegister(SP::G0, MVT::i32)), 0);
Chris Lattner7087e572005-12-17 22:39:19 +0000163
164 // FIXME: Handle div by immediate.
Chris Lattner7c90f732006-02-05 05:50:24 +0000165 unsigned Opcode = N->getOpcode() == ISD::SDIV ? SP::SDIVrr : SP::UDIVrr;
Evan Cheng23329f52006-08-16 07:30:09 +0000166 return CurDAG->SelectNodeTo(N, Opcode, MVT::i32, DivLHS, DivRHS,
Evan Cheng95514ba2006-08-26 08:00:10 +0000167 TopPart);
Chris Lattner7087e572005-12-17 22:39:19 +0000168 }
Chris Lattneree3d5fb2005-12-17 22:30:00 +0000169 case ISD::MULHU:
170 case ISD::MULHS: {
Chris Lattner7087e572005-12-17 22:39:19 +0000171 // FIXME: Handle mul by immediate.
Dan Gohman475871a2008-07-27 21:46:04 +0000172 SDValue MulLHS = N->getOperand(0);
173 SDValue MulRHS = N->getOperand(1);
Evan Cheng6da2f322006-08-26 01:07:58 +0000174 AddToISelQueue(MulLHS);
175 AddToISelQueue(MulRHS);
Chris Lattner7c90f732006-02-05 05:50:24 +0000176 unsigned Opcode = N->getOpcode() == ISD::MULHU ? SP::UMULrr : SP::SMULrr;
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000177 SDNode *Mul = CurDAG->getTargetNode(Opcode, MVT::i32, MVT::Flag,
Chris Lattnerad7a3e62006-02-10 07:35:42 +0000178 MulLHS, MulRHS);
Chris Lattneree3d5fb2005-12-17 22:30:00 +0000179 // The high part is in the Y register.
Dan Gohman475871a2008-07-27 21:46:04 +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 }
184
Evan Cheng9ade2182006-08-26 05:34:46 +0000185 return SelectCode(Op);
Chris Lattner6c18b102005-12-17 07:47:01 +0000186}
187
188
Chris Lattner7c90f732006-02-05 05:50:24 +0000189/// createSparcISelDag - This pass converts a legalized DAG into a
Chris Lattner4dcfaac2006-01-26 07:22:22 +0000190/// SPARC-specific DAG, ready for instruction scheduling.
Chris Lattner6c18b102005-12-17 07:47:01 +0000191///
Chris Lattner7c90f732006-02-05 05:50:24 +0000192FunctionPass *llvm::createSparcISelDag(TargetMachine &TM) {
193 return new SparcDAGToDAGISel(TM);
Chris Lattner6c18b102005-12-17 07:47:01 +0000194}