blob: 256ae5498982c7bd358ae6e8e5fa5f6cd30810be [file] [log] [blame]
Andrew Lenharthd97591a2005-10-20 00:29:02 +00001//===-- AlphaISelDAGToDAG.cpp - Alpha pattern matching inst selector ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Andrew Lenharth and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for Alpha,
11// converting from a legalized dag to a Alpha dag.
12//
13//===----------------------------------------------------------------------===//
14
15#include "Alpha.h"
16#include "AlphaTargetMachine.h"
17#include "AlphaISelLowering.h"
18#include "llvm/CodeGen/MachineInstrBuilder.h"
19#include "llvm/CodeGen/MachineFunction.h"
20#include "llvm/CodeGen/SSARegMap.h"
21#include "llvm/CodeGen/SelectionDAG.h"
22#include "llvm/CodeGen/SelectionDAGISel.h"
23#include "llvm/Target/TargetOptions.h"
24#include "llvm/ADT/Statistic.h"
25#include "llvm/Constants.h"
26#include "llvm/GlobalValue.h"
27#include "llvm/Support/Debug.h"
28#include "llvm/Support/MathExtras.h"
Andrew Lenharth756fbeb2005-10-22 22:06:58 +000029#include <algorithm>
Andrew Lenharthd97591a2005-10-20 00:29:02 +000030using namespace llvm;
31
32namespace {
33
34 //===--------------------------------------------------------------------===//
35 /// AlphaDAGToDAGISel - Alpha specific code to select Alpha machine
36 /// instructions for SelectionDAG operations.
37 ///
38 class AlphaDAGToDAGISel : public SelectionDAGISel {
39 AlphaTargetLowering AlphaLowering;
40
41 public:
42 AlphaDAGToDAGISel(TargetMachine &TM)
43 : SelectionDAGISel(AlphaLowering), AlphaLowering(TM) {}
44
45 /// getI64Imm - Return a target constant with the specified value, of type
46 /// i64.
Andrew Lenharth756fbeb2005-10-22 22:06:58 +000047 inline SDOperand getI64Imm(int64_t Imm) {
Andrew Lenharthd97591a2005-10-20 00:29:02 +000048 return CurDAG->getTargetConstant(Imm, MVT::i64);
49 }
50
Andrew Lenharthd97591a2005-10-20 00:29:02 +000051 // Select - Convert the specified operand from a target-independent to a
52 // target-specific node if it hasn't already been changed.
53 SDOperand Select(SDOperand Op);
54
55 /// InstructionSelectBasicBlock - This callback is invoked by
56 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
57 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
58
59 virtual const char *getPassName() const {
60 return "Alpha DAG->DAG Pattern Instruction Selection";
61 }
62
63// Include the pieces autogenerated from the target description.
64#include "AlphaGenDAGISel.inc"
65
66private:
Andrew Lenharth756fbeb2005-10-22 22:06:58 +000067 SDOperand getGlobalBaseReg();
68 SDOperand SelectCALL(SDOperand Op);
69
Andrew Lenharthd97591a2005-10-20 00:29:02 +000070 };
71}
72
Andrew Lenharth756fbeb2005-10-22 22:06:58 +000073/// getGlobalBaseReg - Output the instructions required to put the
74/// GOT address into a register.
75///
76SDOperand AlphaDAGToDAGISel::getGlobalBaseReg() {
77 return CurDAG->getRegister(AlphaLowering.getVRegGP(), MVT::i64);
78}
79
Andrew Lenharthd97591a2005-10-20 00:29:02 +000080/// InstructionSelectBasicBlock - This callback is invoked by
81/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
82void AlphaDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
83 DEBUG(BB->dump());
84
Andrew Lenharthd97591a2005-10-20 00:29:02 +000085 // Select target instructions for the DAG.
86 DAG.setRoot(Select(DAG.getRoot()));
87 CodeGenMap.clear();
88 DAG.RemoveDeadNodes();
89
90 // Emit machine code to BB.
91 ScheduleAndEmitDAG(DAG);
92}
93
94// Select - Convert the specified operand from a target-independent to a
95// target-specific node if it hasn't already been changed.
96SDOperand AlphaDAGToDAGISel::Select(SDOperand Op) {
97 SDNode *N = Op.Val;
98 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
99 N->getOpcode() < AlphaISD::FIRST_NUMBER)
100 return Op; // Already selected.
101
102 // If this has already been converted, use it.
103 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
104 if (CGMI != CodeGenMap.end()) return CGMI->second;
105
106 switch (N->getOpcode()) {
107 default: break;
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000108 case ISD::TAILCALL:
109 case ISD::CALL: return SelectCALL(Op);
110
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000111 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000112 assert(0 && "You want these too?");
113
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000114 case ISD::BRCOND: {
115 SDOperand Chain = Select(N->getOperand(0));
116 SDOperand CC = Select(N->getOperand(1));
117 CurDAG->SelectNodeTo(N, Alpha::BNE, MVT::Other, CC, Chain);
118 return SDOperand(N, 0);
119 }
120 case ISD::LOAD:
121 case ISD::EXTLOAD:
122 case ISD::ZEXTLOAD:
123 case ISD::SEXTLOAD: {
124 SDOperand Chain = Select(N->getOperand(0));
125 SDOperand Address = Select(N->getOperand(1));
126 unsigned opcode = N->getOpcode();
127 unsigned Opc = Alpha::WTF;
128 if (opcode == ISD::LOAD)
129 switch (N->getValueType(0)) {
130 default: N->dump(); assert(0 && "Bad load!");
131 case MVT::i64: Opc = Alpha::LDQ; break;
132 case MVT::f64: Opc = Alpha::LDT; break;
133 case MVT::f32: Opc = Alpha::LDS; break;
134 }
135 else
136 switch (cast<VTSDNode>(N->getOperand(3))->getVT()) {
137 default: N->dump(); assert(0 && "Bad sign extend!");
138 case MVT::i32: Opc = Alpha::LDL;
139 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
140 case MVT::i16: Opc = Alpha::LDWU;
141 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
142 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
143 case MVT::i8: Opc = Alpha::LDBU;
144 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
145 }
146
147 CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), MVT::Other,
148 getI64Imm(0), Address, Chain);
149 return SDOperand(N, Op.ResNo);
150 }
151
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000152 case ISD::BR: {
153 CurDAG->SelectNodeTo(N, Alpha::BR_DAG, MVT::Other, N->getOperand(1),
154 Select(N->getOperand(0)));
155 return SDOperand(N, 0);
156 }
157
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000158 case ISD::UNDEF:
159 if (N->getValueType(0) == MVT::i64)
160 CurDAG->SelectNodeTo(N, Alpha::IDEF, MVT::i64);
161// else if (N->getValueType(0) == MVT::f32)
162// CurDAG->SelectNodeTo(N, PPC::IMPLICIT_DEF_F4, MVT::f32);
163// else
164// CurDAG->SelectNodeTo(N, PPC::IMPLICIT_DEF_F8, MVT::f64);
165 return SDOperand(N, 0);
166 case ISD::FrameIndex: {
167// int FI = cast<FrameIndexSDNode>(N)->getIndex();
168// CurDAG->SelectNodeTo(N, Alpha::LDA, MVT::i64,
169// CurDAG->getTargetFrameIndex(FI, MVT::i32),
170// getI32Imm(0));
171// return SDOperand(N, 0);
172 assert(0 && "Frame?, you are suppose to look through the window, not at the frame!");
173 }
174 case ISD::ConstantPool: {
175// Constant *C = cast<ConstantPoolSDNode>(N)->get();
176// SDOperand Tmp, CPI = CurDAG->getTargetConstantPool(C, MVT::i32);
177// if (PICEnabled)
178// Tmp = CurDAG->getTargetNode(PPC::ADDIS, MVT::i32, getGlobalBaseReg(),CPI);
179// else
180// Tmp = CurDAG->getTargetNode(PPC::LIS, MVT::i32, CPI);
181// CurDAG->SelectNodeTo(N, PPC::LA, MVT::i32, Tmp, CPI);
182// return SDOperand(N, 0);
183 assert(0 && "Constants are overrated");
184 }
185 case ISD::GlobalAddress: {
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000186 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
187 SDOperand GA = CurDAG->getTargetGlobalAddress(GV, MVT::i64);
188 CurDAG->SelectNodeTo(N, Alpha::LDQl, MVT::i64, GA, getGlobalBaseReg());
189 return SDOperand(N, 0);
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000190 }
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000191 case ISD::ExternalSymbol:
192 CurDAG->SelectNodeTo(N, Alpha::LDQl, MVT::i64,
193 CurDAG->getTargetExternalSymbol(cast<ExternalSymbolSDNode>(N)->getSymbol(), MVT::i64),
194 CurDAG->getRegister(AlphaLowering.getVRegGP(), MVT::i64));
195 return SDOperand(N, 0);
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000196
197 case ISD::CALLSEQ_START:
198 case ISD::CALLSEQ_END: {
199 unsigned Amt = cast<ConstantSDNode>(N->getOperand(1))->getValue();
200 unsigned Opc = N->getOpcode() == ISD::CALLSEQ_START ?
201 Alpha::ADJUSTSTACKDOWN : Alpha::ADJUSTSTACKUP;
202 CurDAG->SelectNodeTo(N, Opc, MVT::Other,
203 getI64Imm(Amt), Select(N->getOperand(0)));
204 return SDOperand(N, 0);
205 }
206 case ISD::RET: {
207 SDOperand Chain = Select(N->getOperand(0)); // Token chain.
208
209 if (N->getNumOperands() == 2) {
210 SDOperand Val = Select(N->getOperand(1));
211 if (N->getOperand(1).getValueType() == MVT::i64) {
212 Chain = CurDAG->getCopyToReg(Chain, Alpha::R0, Val);
213 }
214 }
215 //BuildMI(BB, Alpha::RET, 2, Alpha::R31).addReg(Alpha::R26).addImm(1);
216
217 // FIXME: add restoring of the RA to R26 to the chain
218 // Finally, select this to a ret instruction.
219 CurDAG->SelectNodeTo(N, Alpha::RETDAG, MVT::Other, Chain);
220 return SDOperand(N, 0);
221 }
222
223
224
225 }
226
227 return SelectCode(Op);
228}
229
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000230SDOperand AlphaDAGToDAGISel::SelectCALL(SDOperand Op) {
231 SDNode *N = Op.Val;
232 SDOperand Chain = Select(N->getOperand(0));
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000233 SDOperand Addr = Select(N->getOperand(1));
234
235// unsigned CallOpcode;
236 std::vector<SDOperand> CallOperands;
237 std::vector<MVT::ValueType> TypeOperands;
238
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000239 //grab the arguments
240 for(int i = 2, e = N->getNumOperands(); i < e; ++i) {
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000241 TypeOperands.push_back(N->getOperand(i).getValueType());
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000242 CallOperands.push_back(Select(N->getOperand(i)));
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000243 }
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000244 int count = N->getNumOperands() - 2;
245
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000246 static const unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
247 Alpha::R19, Alpha::R20, Alpha::R21};
248 static const unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
249 Alpha::F19, Alpha::F20, Alpha::F21};
250
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000251 for (int i = 0; i < std::min(6, count); ++i) {
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000252 if (MVT::isInteger(TypeOperands[i])) {
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000253 Chain = CurDAG->getCopyToReg(Chain, args_int[i], CallOperands[i]);
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000254 } else {
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000255 assert(0 && "No FP support yet");
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000256 }
257 }
258 assert(CallOperands.size() <= 6 && "Too big a call");
259
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000260 Chain = CurDAG->getCopyToReg(Chain, Alpha::R27, Addr);
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000261 // Finally, once everything is in registers to pass to the call, emit the
262 // call itself.
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000263 Chain = CurDAG->getTargetNode(Alpha::JSRDAG, MVT::Other, Chain );
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000264
265 std::vector<SDOperand> CallResults;
266
267 switch (N->getValueType(0)) {
268 default: assert(0 && "Unexpected ret value!");
269 case MVT::Other: break;
270 case MVT::i64:
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000271 Chain = CurDAG->getCopyFromReg(Chain, Alpha::R0, MVT::i64).getValue(1);
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000272 CallResults.push_back(Chain.getValue(0));
273 break;
274 }
275
276 CallResults.push_back(Chain);
277 for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
278 CodeGenMap[Op.getValue(i)] = CallResults[i];
279 return CallResults[Op.ResNo];
280}
281
282
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000283/// createAlphaISelDag - This pass converts a legalized DAG into a
284/// Alpha-specific DAG, ready for instruction scheduling.
285///
286FunctionPass *llvm::createAlphaISelDag(TargetMachine &TM) {
287 return new AlphaDAGToDAGISel(TM);
288}