blob: b920bc4d8cc69d65646b4cd028b417335b2dc5dc [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
Andrew Lenharth50b37842005-11-22 04:20:06 +000041 static const int IMM_LOW = -32768;
42 static const int IMM_HIGH = 32767;
43 static const int IMM_MULT = 65536;
44
Andrew Lenharthd97591a2005-10-20 00:29:02 +000045 public:
46 AlphaDAGToDAGISel(TargetMachine &TM)
47 : SelectionDAGISel(AlphaLowering), AlphaLowering(TM) {}
48
49 /// getI64Imm - Return a target constant with the specified value, of type
50 /// i64.
Andrew Lenharth756fbeb2005-10-22 22:06:58 +000051 inline SDOperand getI64Imm(int64_t Imm) {
Andrew Lenharthd97591a2005-10-20 00:29:02 +000052 return CurDAG->getTargetConstant(Imm, MVT::i64);
53 }
54
Andrew Lenharthd97591a2005-10-20 00:29:02 +000055 // Select - Convert the specified operand from a target-independent to a
56 // target-specific node if it hasn't already been changed.
57 SDOperand Select(SDOperand Op);
58
59 /// InstructionSelectBasicBlock - This callback is invoked by
60 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
61 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
62
63 virtual const char *getPassName() const {
64 return "Alpha DAG->DAG Pattern Instruction Selection";
65 }
66
67// Include the pieces autogenerated from the target description.
68#include "AlphaGenDAGISel.inc"
69
70private:
Andrew Lenharth756fbeb2005-10-22 22:06:58 +000071 SDOperand getGlobalBaseReg();
72 SDOperand SelectCALL(SDOperand Op);
73
Andrew Lenharthd97591a2005-10-20 00:29:02 +000074 };
75}
76
Andrew Lenharth756fbeb2005-10-22 22:06:58 +000077/// getGlobalBaseReg - Output the instructions required to put the
78/// GOT address into a register.
79///
80SDOperand AlphaDAGToDAGISel::getGlobalBaseReg() {
81 return CurDAG->getRegister(AlphaLowering.getVRegGP(), MVT::i64);
82}
83
Andrew Lenharthd97591a2005-10-20 00:29:02 +000084/// InstructionSelectBasicBlock - This callback is invoked by
85/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
86void AlphaDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
87 DEBUG(BB->dump());
88
Andrew Lenharthd97591a2005-10-20 00:29:02 +000089 // Select target instructions for the DAG.
90 DAG.setRoot(Select(DAG.getRoot()));
91 CodeGenMap.clear();
92 DAG.RemoveDeadNodes();
93
94 // Emit machine code to BB.
95 ScheduleAndEmitDAG(DAG);
96}
97
98// Select - Convert the specified operand from a target-independent to a
99// target-specific node if it hasn't already been changed.
100SDOperand AlphaDAGToDAGISel::Select(SDOperand Op) {
101 SDNode *N = Op.Val;
102 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
103 N->getOpcode() < AlphaISD::FIRST_NUMBER)
104 return Op; // Already selected.
105
106 // If this has already been converted, use it.
107 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
108 if (CGMI != CodeGenMap.end()) return CGMI->second;
109
110 switch (N->getOpcode()) {
111 default: break;
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000112 case ISD::TAILCALL:
113 case ISD::CALL: return SelectCALL(Op);
114
Andrew Lenharth50b37842005-11-22 04:20:06 +0000115 case ISD::DYNAMIC_STACKALLOC: {
116 if (!isa<ConstantSDNode>(N->getOperand(2)) ||
117 cast<ConstantSDNode>(N->getOperand(2))->getValue() != 0) {
118 std::cerr << "Cannot allocate stack object with greater alignment than"
119 << " the stack alignment yet!";
120 abort();
121 }
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000122
Andrew Lenharth50b37842005-11-22 04:20:06 +0000123 SDOperand Chain = Select(N->getOperand(0));
124 SDOperand Amt = Select(N->getOperand(1));
125 SDOperand Reg = CurDAG->getRegister(Alpha::R30, MVT::i64);
126 SDOperand Val = CurDAG->getCopyFromReg(Chain, Alpha::R30, MVT::i64);
127 Chain = Val.getValue(1);
128
129 // Subtract the amount (guaranteed to be a multiple of the stack alignment)
130 // from the stack pointer, giving us the result pointer.
131 SDOperand Result = CurDAG->getTargetNode(Alpha::SUBQ, MVT::i64, Val, Amt);
132
133 // Copy this result back into R30.
134 Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, Chain, Reg, Result);
135
136 // Copy this result back out of R30 to make sure we're not using the stack
137 // space without decrementing the stack pointer.
138 Result = CurDAG->getCopyFromReg(Chain, Alpha::R30, MVT::i64);
139
140 // Finally, replace the DYNAMIC_STACKALLOC with the copyfromreg.
141 CodeGenMap[Op.getValue(0)] = Result;
142 CodeGenMap[Op.getValue(1)] = Result.getValue(1);
143 return SDOperand(Result.Val, Op.ResNo);
144 }
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000145 case ISD::BRCOND: {
146 SDOperand Chain = Select(N->getOperand(0));
147 SDOperand CC = Select(N->getOperand(1));
Andrew Lenharth50b37842005-11-22 04:20:06 +0000148 MachineBasicBlock *Dest =
149 cast<BasicBlockSDNode>(N->getOperand(2))->getBasicBlock();
150 CurDAG->SelectNodeTo(N, Alpha::BNE, MVT::Other, CC, CurDAG->getBasicBlock(Dest), Chain);
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000151 return SDOperand(N, 0);
152 }
153 case ISD::LOAD:
154 case ISD::EXTLOAD:
155 case ISD::ZEXTLOAD:
156 case ISD::SEXTLOAD: {
157 SDOperand Chain = Select(N->getOperand(0));
158 SDOperand Address = Select(N->getOperand(1));
159 unsigned opcode = N->getOpcode();
160 unsigned Opc = Alpha::WTF;
161 if (opcode == ISD::LOAD)
162 switch (N->getValueType(0)) {
163 default: N->dump(); assert(0 && "Bad load!");
164 case MVT::i64: Opc = Alpha::LDQ; break;
165 case MVT::f64: Opc = Alpha::LDT; break;
166 case MVT::f32: Opc = Alpha::LDS; break;
167 }
168 else
169 switch (cast<VTSDNode>(N->getOperand(3))->getVT()) {
170 default: N->dump(); assert(0 && "Bad sign extend!");
171 case MVT::i32: Opc = Alpha::LDL;
172 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
173 case MVT::i16: Opc = Alpha::LDWU;
174 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
175 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
176 case MVT::i8: Opc = Alpha::LDBU;
177 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
178 }
179
180 CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), MVT::Other,
181 getI64Imm(0), Address, Chain);
182 return SDOperand(N, Op.ResNo);
183 }
Andrew Lenharth50b37842005-11-22 04:20:06 +0000184 case ISD::STORE:
185 case ISD::TRUNCSTORE: {
186 SDOperand Chain = Select(N->getOperand(0));
187 SDOperand Value = Select(N->getOperand(1));
188 SDOperand Address = Select(N->getOperand(2));
189
190 unsigned Opc = Alpha::WTF;
191
192 if (N->getOpcode() == ISD::STORE) {
193 switch (N->getOperand(1).getValueType()) {
194 case MVT::i64: Opc = Alpha::STQ; break;
195 case MVT::f64: Opc = Alpha::STT; break;
196 case MVT::f32: Opc = Alpha::STS; break;
197 default: assert(0 && "Bad store!");
198 };
199 } else { //TRUNCSTORE
200 switch (cast<VTSDNode>(N->getOperand(4))->getVT()) {
201 case MVT::i32: Opc = Alpha::STL; break;
202 case MVT::i16: Opc = Alpha::STW; break;
203 case MVT::i8: Opc = Alpha::STB; break;
204 default: assert(0 && "Bad truncstore!");
205 };
206 }
207 CurDAG->SelectNodeTo(N, Opc, MVT::Other, Value, getI64Imm(0), Address,
208 Chain);
209 return SDOperand(N, 0);
210 }
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000211
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000212 case ISD::BR: {
213 CurDAG->SelectNodeTo(N, Alpha::BR_DAG, MVT::Other, N->getOperand(1),
214 Select(N->getOperand(0)));
215 return SDOperand(N, 0);
216 }
217
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000218 case ISD::FrameIndex: {
Andrew Lenharth50b37842005-11-22 04:20:06 +0000219 int FI = cast<FrameIndexSDNode>(N)->getIndex();
220 CurDAG->SelectNodeTo(N, Alpha::LDA, MVT::i64,
221 CurDAG->getTargetFrameIndex(FI, MVT::i32),
222 getI64Imm(0));
223 return SDOperand(N, 0);
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000224 }
225 case ISD::ConstantPool: {
Andrew Lenharth50b37842005-11-22 04:20:06 +0000226 Constant *C = cast<ConstantPoolSDNode>(N)->get();
227 SDOperand Tmp, CPI = CurDAG->getTargetConstantPool(C, MVT::i64);
228 Tmp = CurDAG->getTargetNode(Alpha::LDAHr, MVT::i64, CPI, getGlobalBaseReg());
229 CurDAG->SelectNodeTo(N, Alpha::LDAr, MVT::i64, CPI, Tmp);
230 return SDOperand(N, 0);
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000231 }
232 case ISD::GlobalAddress: {
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000233 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
234 SDOperand GA = CurDAG->getTargetGlobalAddress(GV, MVT::i64);
235 CurDAG->SelectNodeTo(N, Alpha::LDQl, MVT::i64, GA, getGlobalBaseReg());
236 return SDOperand(N, 0);
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000237 }
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000238 case ISD::ExternalSymbol:
239 CurDAG->SelectNodeTo(N, Alpha::LDQl, MVT::i64,
240 CurDAG->getTargetExternalSymbol(cast<ExternalSymbolSDNode>(N)->getSymbol(), MVT::i64),
241 CurDAG->getRegister(AlphaLowering.getVRegGP(), MVT::i64));
242 return SDOperand(N, 0);
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000243
244 case ISD::CALLSEQ_START:
245 case ISD::CALLSEQ_END: {
246 unsigned Amt = cast<ConstantSDNode>(N->getOperand(1))->getValue();
247 unsigned Opc = N->getOpcode() == ISD::CALLSEQ_START ?
248 Alpha::ADJUSTSTACKDOWN : Alpha::ADJUSTSTACKUP;
249 CurDAG->SelectNodeTo(N, Opc, MVT::Other,
250 getI64Imm(Amt), Select(N->getOperand(0)));
251 return SDOperand(N, 0);
252 }
253 case ISD::RET: {
254 SDOperand Chain = Select(N->getOperand(0)); // Token chain.
255
256 if (N->getNumOperands() == 2) {
257 SDOperand Val = Select(N->getOperand(1));
258 if (N->getOperand(1).getValueType() == MVT::i64) {
259 Chain = CurDAG->getCopyToReg(Chain, Alpha::R0, Val);
260 }
261 }
262 //BuildMI(BB, Alpha::RET, 2, Alpha::R31).addReg(Alpha::R26).addImm(1);
263
264 // FIXME: add restoring of the RA to R26 to the chain
265 // Finally, select this to a ret instruction.
266 CurDAG->SelectNodeTo(N, Alpha::RETDAG, MVT::Other, Chain);
267 return SDOperand(N, 0);
268 }
Andrew Lenharth50b37842005-11-22 04:20:06 +0000269 case ISD::Constant: {
270 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
271 if (val > (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT ||
272 val < (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
273 MachineConstantPool *CP = BB->getParent()->getConstantPool();
274 ConstantUInt *C =
275 ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
276 SDOperand Tmp, CPI = CurDAG->getTargetConstantPool(C, MVT::i64);
277 Tmp = CurDAG->getTargetNode(Alpha::LDAHr, MVT::i64, CPI, getGlobalBaseReg());
278 CurDAG->SelectNodeTo(N, Alpha::LDAr, MVT::i64, CPI, Tmp);
279 return SDOperand(N, 0);
280 }
281 }
282 case ISD::ConstantFP:
283 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
284 bool isDouble = N->getValueType(0) == MVT::f64;
285 MVT::ValueType T = isDouble ? MVT::f64 : MVT::f32;
286 if (CN->isExactlyValue(+0.0)) {
287 CurDAG->SelectNodeTo(N, isDouble ? Alpha::CPYST : Alpha::CPYSS, T,
288 CurDAG->getRegister(Alpha::F31, T),
289 CurDAG->getRegister(Alpha::F31, T));
290 return SDOperand(N, 0);
291 } else if ( CN->isExactlyValue(-0.0)) {
292 CurDAG->SelectNodeTo(N, isDouble ? Alpha::CPYSNT : Alpha::CPYSNS, T,
293 CurDAG->getRegister(Alpha::F31, T),
294 CurDAG->getRegister(Alpha::F31, T));
295 return SDOperand(N, 0);
296 } else {
297 abort();
298 }
299 }
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000300 }
301
302 return SelectCode(Op);
303}
304
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000305SDOperand AlphaDAGToDAGISel::SelectCALL(SDOperand Op) {
Andrew Lenharth50b37842005-11-22 04:20:06 +0000306 //TODO: add flag stuff to prevent nondeturministic breakage!
307
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000308 SDNode *N = Op.Val;
309 SDOperand Chain = Select(N->getOperand(0));
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000310 SDOperand Addr = Select(N->getOperand(1));
311
312// unsigned CallOpcode;
313 std::vector<SDOperand> CallOperands;
314 std::vector<MVT::ValueType> TypeOperands;
315
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000316 //grab the arguments
317 for(int i = 2, e = N->getNumOperands(); i < e; ++i) {
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000318 TypeOperands.push_back(N->getOperand(i).getValueType());
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000319 CallOperands.push_back(Select(N->getOperand(i)));
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000320 }
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000321 int count = N->getNumOperands() - 2;
322
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000323 static const unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
324 Alpha::R19, Alpha::R20, Alpha::R21};
325 static const unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
326 Alpha::F19, Alpha::F20, Alpha::F21};
327
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000328 for (int i = 0; i < std::min(6, count); ++i) {
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000329 if (MVT::isInteger(TypeOperands[i])) {
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000330 Chain = CurDAG->getCopyToReg(Chain, args_int[i], CallOperands[i]);
Andrew Lenharth50b37842005-11-22 04:20:06 +0000331 } else if (TypeOperands[i] == MVT::f64 || TypeOperands[i] == MVT::f64) {
332 Chain = CurDAG->getCopyToReg(Chain, args_float[i], CallOperands[i]);
333 } else
334 assert(0 && "Unknown operand");
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000335 }
Andrew Lenharth50b37842005-11-22 04:20:06 +0000336
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000337 assert(CallOperands.size() <= 6 && "Too big a call");
338
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000339 Chain = CurDAG->getCopyToReg(Chain, Alpha::R27, Addr);
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000340 // Finally, once everything is in registers to pass to the call, emit the
341 // call itself.
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000342 Chain = CurDAG->getTargetNode(Alpha::JSRDAG, MVT::Other, Chain );
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000343
344 std::vector<SDOperand> CallResults;
345
346 switch (N->getValueType(0)) {
347 default: assert(0 && "Unexpected ret value!");
348 case MVT::Other: break;
349 case MVT::i64:
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000350 Chain = CurDAG->getCopyFromReg(Chain, Alpha::R0, MVT::i64).getValue(1);
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000351 CallResults.push_back(Chain.getValue(0));
352 break;
Andrew Lenharth50b37842005-11-22 04:20:06 +0000353 case MVT::f32:
354 Chain = CurDAG->getCopyFromReg(Chain, Alpha::F0, MVT::f32).getValue(1);
355 CallResults.push_back(Chain.getValue(0));
356 break;
357 case MVT::f64:
358 Chain = CurDAG->getCopyFromReg(Chain, Alpha::F0, MVT::f64).getValue(1);
359 CallResults.push_back(Chain.getValue(0));
360 break;
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000361 }
362
363 CallResults.push_back(Chain);
364 for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
365 CodeGenMap[Op.getValue(i)] = CallResults[i];
366 return CallResults[Op.ResNo];
367}
368
369
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000370/// createAlphaISelDag - This pass converts a legalized DAG into a
371/// Alpha-specific DAG, ready for instruction scheduling.
372///
373FunctionPass *llvm::createAlphaISelDag(TargetMachine &TM) {
374 return new AlphaDAGToDAGISel(TM);
375}