blob: d88bb5608bf248571123ef6a39aae1f2cb3e7d5d [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"
Andrew Lenharth7f0db912005-11-30 07:19:56 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
Andrew Lenharthd97591a2005-10-20 00:29:02 +000020#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/SSARegMap.h"
22#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/Target/TargetOptions.h"
25#include "llvm/ADT/Statistic.h"
26#include "llvm/Constants.h"
27#include "llvm/GlobalValue.h"
28#include "llvm/Support/Debug.h"
29#include "llvm/Support/MathExtras.h"
Andrew Lenharth756fbeb2005-10-22 22:06:58 +000030#include <algorithm>
Andrew Lenharthd97591a2005-10-20 00:29:02 +000031using namespace llvm;
32
33namespace {
34
35 //===--------------------------------------------------------------------===//
36 /// AlphaDAGToDAGISel - Alpha specific code to select Alpha machine
37 /// instructions for SelectionDAG operations.
38 ///
39 class AlphaDAGToDAGISel : public SelectionDAGISel {
40 AlphaTargetLowering AlphaLowering;
41
Andrew Lenharth50b37842005-11-22 04:20:06 +000042 static const int IMM_LOW = -32768;
43 static const int IMM_HIGH = 32767;
44 static const int IMM_MULT = 65536;
45
Andrew Lenharthd97591a2005-10-20 00:29:02 +000046 public:
47 AlphaDAGToDAGISel(TargetMachine &TM)
48 : SelectionDAGISel(AlphaLowering), AlphaLowering(TM) {}
49
50 /// getI64Imm - Return a target constant with the specified value, of type
51 /// i64.
Andrew Lenharth756fbeb2005-10-22 22:06:58 +000052 inline SDOperand getI64Imm(int64_t Imm) {
Andrew Lenharthd97591a2005-10-20 00:29:02 +000053 return CurDAG->getTargetConstant(Imm, MVT::i64);
54 }
55
Andrew Lenharthd97591a2005-10-20 00:29:02 +000056 // Select - Convert the specified operand from a target-independent to a
57 // target-specific node if it hasn't already been changed.
58 SDOperand Select(SDOperand Op);
59
60 /// InstructionSelectBasicBlock - This callback is invoked by
61 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
62 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
63
64 virtual const char *getPassName() const {
65 return "Alpha DAG->DAG Pattern Instruction Selection";
66 }
67
68// Include the pieces autogenerated from the target description.
69#include "AlphaGenDAGISel.inc"
70
71private:
Andrew Lenharth756fbeb2005-10-22 22:06:58 +000072 SDOperand getGlobalBaseReg();
73 SDOperand SelectCALL(SDOperand Op);
74
Andrew Lenharthd97591a2005-10-20 00:29:02 +000075 };
76}
77
Andrew Lenharth756fbeb2005-10-22 22:06:58 +000078/// getGlobalBaseReg - Output the instructions required to put the
79/// GOT address into a register.
80///
81SDOperand AlphaDAGToDAGISel::getGlobalBaseReg() {
82 return CurDAG->getRegister(AlphaLowering.getVRegGP(), MVT::i64);
83}
84
Andrew Lenharthd97591a2005-10-20 00:29:02 +000085/// InstructionSelectBasicBlock - This callback is invoked by
86/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
87void AlphaDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
88 DEBUG(BB->dump());
89
Andrew Lenharthd97591a2005-10-20 00:29:02 +000090 // Select target instructions for the DAG.
91 DAG.setRoot(Select(DAG.getRoot()));
92 CodeGenMap.clear();
93 DAG.RemoveDeadNodes();
94
95 // Emit machine code to BB.
96 ScheduleAndEmitDAG(DAG);
97}
98
99// Select - Convert the specified operand from a target-independent to a
100// target-specific node if it hasn't already been changed.
101SDOperand AlphaDAGToDAGISel::Select(SDOperand Op) {
102 SDNode *N = Op.Val;
103 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
104 N->getOpcode() < AlphaISD::FIRST_NUMBER)
105 return Op; // Already selected.
106
107 // If this has already been converted, use it.
108 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
109 if (CGMI != CodeGenMap.end()) return CGMI->second;
110
111 switch (N->getOpcode()) {
112 default: break;
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000113 case ISD::TAILCALL:
114 case ISD::CALL: return SelectCALL(Op);
115
Andrew Lenharth50b37842005-11-22 04:20:06 +0000116 case ISD::DYNAMIC_STACKALLOC: {
117 if (!isa<ConstantSDNode>(N->getOperand(2)) ||
118 cast<ConstantSDNode>(N->getOperand(2))->getValue() != 0) {
119 std::cerr << "Cannot allocate stack object with greater alignment than"
120 << " the stack alignment yet!";
121 abort();
122 }
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000123
Andrew Lenharth50b37842005-11-22 04:20:06 +0000124 SDOperand Chain = Select(N->getOperand(0));
125 SDOperand Amt = Select(N->getOperand(1));
126 SDOperand Reg = CurDAG->getRegister(Alpha::R30, MVT::i64);
127 SDOperand Val = CurDAG->getCopyFromReg(Chain, Alpha::R30, MVT::i64);
128 Chain = Val.getValue(1);
129
130 // Subtract the amount (guaranteed to be a multiple of the stack alignment)
131 // from the stack pointer, giving us the result pointer.
132 SDOperand Result = CurDAG->getTargetNode(Alpha::SUBQ, MVT::i64, Val, Amt);
133
134 // Copy this result back into R30.
135 Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, Chain, Reg, Result);
136
137 // Copy this result back out of R30 to make sure we're not using the stack
138 // space without decrementing the stack pointer.
139 Result = CurDAG->getCopyFromReg(Chain, Alpha::R30, MVT::i64);
140
141 // Finally, replace the DYNAMIC_STACKALLOC with the copyfromreg.
142 CodeGenMap[Op.getValue(0)] = Result;
143 CodeGenMap[Op.getValue(1)] = Result.getValue(1);
144 return SDOperand(Result.Val, Op.ResNo);
145 }
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000146 case ISD::BRCOND: {
147 SDOperand Chain = Select(N->getOperand(0));
148 SDOperand CC = Select(N->getOperand(1));
Andrew Lenharth50b37842005-11-22 04:20:06 +0000149 MachineBasicBlock *Dest =
150 cast<BasicBlockSDNode>(N->getOperand(2))->getBasicBlock();
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000151 return CurDAG->SelectNodeTo(N, Alpha::BNE, MVT::Other, CC,
152 CurDAG->getBasicBlock(Dest), Chain);
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000153 }
154 case ISD::LOAD:
155 case ISD::EXTLOAD:
156 case ISD::ZEXTLOAD:
157 case ISD::SEXTLOAD: {
158 SDOperand Chain = Select(N->getOperand(0));
159 SDOperand Address = Select(N->getOperand(1));
160 unsigned opcode = N->getOpcode();
161 unsigned Opc = Alpha::WTF;
162 if (opcode == ISD::LOAD)
163 switch (N->getValueType(0)) {
164 default: N->dump(); assert(0 && "Bad load!");
165 case MVT::i64: Opc = Alpha::LDQ; break;
166 case MVT::f64: Opc = Alpha::LDT; break;
167 case MVT::f32: Opc = Alpha::LDS; break;
168 }
169 else
170 switch (cast<VTSDNode>(N->getOperand(3))->getVT()) {
171 default: N->dump(); assert(0 && "Bad sign extend!");
172 case MVT::i32: Opc = Alpha::LDL;
173 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
174 case MVT::i16: Opc = Alpha::LDWU;
175 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
176 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
177 case MVT::i8: Opc = Alpha::LDBU;
178 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
179 }
180
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000181 return CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), MVT::Other,
182 getI64Imm(0), Address,
183 Chain).getValue(Op.ResNo);
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000184 }
Andrew Lenharth50b37842005-11-22 04:20:06 +0000185 case ISD::STORE:
186 case ISD::TRUNCSTORE: {
187 SDOperand Chain = Select(N->getOperand(0));
188 SDOperand Value = Select(N->getOperand(1));
189 SDOperand Address = Select(N->getOperand(2));
190
191 unsigned Opc = Alpha::WTF;
192
193 if (N->getOpcode() == ISD::STORE) {
194 switch (N->getOperand(1).getValueType()) {
195 case MVT::i64: Opc = Alpha::STQ; break;
196 case MVT::f64: Opc = Alpha::STT; break;
197 case MVT::f32: Opc = Alpha::STS; break;
198 default: assert(0 && "Bad store!");
199 };
200 } else { //TRUNCSTORE
201 switch (cast<VTSDNode>(N->getOperand(4))->getVT()) {
202 case MVT::i32: Opc = Alpha::STL; break;
203 case MVT::i16: Opc = Alpha::STW; break;
204 case MVT::i8: Opc = Alpha::STB; break;
205 default: assert(0 && "Bad truncstore!");
206 };
207 }
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000208 return CurDAG->SelectNodeTo(N, Opc, MVT::Other, Value, getI64Imm(0),
209 Address, Chain);
Andrew Lenharth50b37842005-11-22 04:20:06 +0000210 }
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000211
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000212 case ISD::BR:
213 return CurDAG->SelectNodeTo(N, Alpha::BR_DAG, MVT::Other, N->getOperand(1),
214 Select(N->getOperand(0)));
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000215
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000216 case ISD::FrameIndex: {
Andrew Lenharth50b37842005-11-22 04:20:06 +0000217 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000218 return CurDAG->SelectNodeTo(N, Alpha::LDA, MVT::i64,
219 CurDAG->getTargetFrameIndex(FI, MVT::i32),
220 getI64Imm(0));
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000221 }
222 case ISD::ConstantPool: {
Andrew Lenharth50b37842005-11-22 04:20:06 +0000223 Constant *C = cast<ConstantPoolSDNode>(N)->get();
224 SDOperand Tmp, CPI = CurDAG->getTargetConstantPool(C, MVT::i64);
225 Tmp = CurDAG->getTargetNode(Alpha::LDAHr, MVT::i64, CPI, getGlobalBaseReg());
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000226 return CurDAG->SelectNodeTo(N, Alpha::LDAr, MVT::i64, CPI, Tmp);
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000227 }
228 case ISD::GlobalAddress: {
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000229 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
230 SDOperand GA = CurDAG->getTargetGlobalAddress(GV, MVT::i64);
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000231 return CurDAG->SelectNodeTo(N, Alpha::LDQl, MVT::i64, GA,
232 getGlobalBaseReg());
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000233 }
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000234 case ISD::ExternalSymbol:
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000235 return CurDAG->SelectNodeTo(N, Alpha::LDQl, MVT::i64,
236 CurDAG->getTargetExternalSymbol(cast<ExternalSymbolSDNode>(N)->getSymbol(), MVT::i64),
237 CurDAG->getRegister(AlphaLowering.getVRegGP(), MVT::i64));
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000238
239 case ISD::CALLSEQ_START:
240 case ISD::CALLSEQ_END: {
241 unsigned Amt = cast<ConstantSDNode>(N->getOperand(1))->getValue();
242 unsigned Opc = N->getOpcode() == ISD::CALLSEQ_START ?
243 Alpha::ADJUSTSTACKDOWN : Alpha::ADJUSTSTACKUP;
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000244 return CurDAG->SelectNodeTo(N, Opc, MVT::Other,
245 getI64Imm(Amt), Select(N->getOperand(0)));
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000246 }
247 case ISD::RET: {
248 SDOperand Chain = Select(N->getOperand(0)); // Token chain.
249
250 if (N->getNumOperands() == 2) {
251 SDOperand Val = Select(N->getOperand(1));
252 if (N->getOperand(1).getValueType() == MVT::i64) {
253 Chain = CurDAG->getCopyToReg(Chain, Alpha::R0, Val);
254 }
255 }
256 //BuildMI(BB, Alpha::RET, 2, Alpha::R31).addReg(Alpha::R26).addImm(1);
257
258 // FIXME: add restoring of the RA to R26 to the chain
259 // Finally, select this to a ret instruction.
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000260 return CurDAG->SelectNodeTo(N, Alpha::RETDAG, MVT::Other, Chain);
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000261 }
Andrew Lenharth50b37842005-11-22 04:20:06 +0000262 case ISD::Constant: {
263 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
264 if (val > (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT ||
265 val < (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
266 MachineConstantPool *CP = BB->getParent()->getConstantPool();
267 ConstantUInt *C =
268 ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
269 SDOperand Tmp, CPI = CurDAG->getTargetConstantPool(C, MVT::i64);
270 Tmp = CurDAG->getTargetNode(Alpha::LDAHr, MVT::i64, CPI, getGlobalBaseReg());
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000271 return CurDAG->SelectNodeTo(N, Alpha::LDAr, MVT::i64, CPI, Tmp);
Andrew Lenharth50b37842005-11-22 04:20:06 +0000272 }
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000273 break;
Andrew Lenharth50b37842005-11-22 04:20:06 +0000274 }
275 case ISD::ConstantFP:
276 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
277 bool isDouble = N->getValueType(0) == MVT::f64;
278 MVT::ValueType T = isDouble ? MVT::f64 : MVT::f32;
279 if (CN->isExactlyValue(+0.0)) {
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000280 return CurDAG->SelectNodeTo(N, isDouble ? Alpha::CPYST : Alpha::CPYSS,
281 T, CurDAG->getRegister(Alpha::F31, T),
282 CurDAG->getRegister(Alpha::F31, T));
Andrew Lenharth50b37842005-11-22 04:20:06 +0000283 } else if ( CN->isExactlyValue(-0.0)) {
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000284 return CurDAG->SelectNodeTo(N, isDouble ? Alpha::CPYSNT : Alpha::CPYSNS,
285 T, CurDAG->getRegister(Alpha::F31, T),
286 CurDAG->getRegister(Alpha::F31, T));
Andrew Lenharth50b37842005-11-22 04:20:06 +0000287 } else {
288 abort();
289 }
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000290 break;
Andrew Lenharth50b37842005-11-22 04:20:06 +0000291 }
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000292 case ISD::SDIV:
293 case ISD::UDIV:
294 case ISD::UREM:
295 case ISD::SREM:
296 if (MVT::isInteger(N->getValueType(0))) {
297 const char* opstr = 0;
298 switch(N->getOpcode()) {
299 case ISD::UREM: opstr = "__remqu"; break;
300 case ISD::SREM: opstr = "__remq"; break;
301 case ISD::UDIV: opstr = "__divqu"; break;
302 case ISD::SDIV: opstr = "__divq"; break;
303 }
304 SDOperand Tmp1 = Select(N->getOperand(0)),
305 Tmp2 = Select(N->getOperand(1)),
306 Addr = CurDAG->getExternalSymbol(opstr, AlphaLowering.getPointerTy());
307 SDOperand Tmp3 = Select(Addr);
308 SDOperand Chain = CurDAG->getCopyToReg(CurDAG->getRoot(), Alpha::R24,
309 Tmp1, SDOperand());
310 Chain = CurDAG->getCopyToReg(CurDAG->getRoot(), Alpha::R25,
311 Tmp2, Chain.getValue(1));
312 Chain = CurDAG->getCopyToReg(CurDAG->getRoot(), Alpha::R27,
313 Tmp3, Chain.getValue(1));
314 Chain = CurDAG->getTargetNode(Alpha::JSRs, MVT::i64, MVT::Flag,
315 CurDAG->getRegister(Alpha::R27, MVT::i64),
316 getI64Imm(0));
317 return CurDAG->getCopyFromReg(Chain.getValue(1), Alpha::R27, MVT::i64,
318 Chain.getValue(1));
319 }
320 break;
321
322 case ISD::SETCC:
323 if (MVT::isFloatingPoint(N->getOperand(0).Val->getValueType(0))) {
324 unsigned Opc = Alpha::WTF;
325 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
326 bool rev = false;
Andrew Lenharthb2156f92005-11-30 17:11:20 +0000327 bool isNE = false;
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000328 switch(CC) {
329 default: N->dump(); assert(0 && "Unknown FP comparison!");
330 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
331 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
332 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
333 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
334 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
Andrew Lenharthb2156f92005-11-30 17:11:20 +0000335 case ISD::SETNE: Opc = Alpha::CMPTEQ; isNE = true; break;
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000336 };
337 SDOperand tmp1 = Select(N->getOperand(0)),
338 tmp2 = Select(N->getOperand(1));
339 SDOperand cmp = CurDAG->getTargetNode(Opc, MVT::f64,
340 rev?tmp2:tmp1,
341 rev?tmp1:tmp2);
Andrew Lenharthb2156f92005-11-30 17:11:20 +0000342 if (isNE)
343 cmp = CurDAG->getTargetNode(Alpha::CMPTEQ, MVT::f64, cmp,
344 CurDAG->getRegister(Alpha::F31, MVT::f64));
345
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000346 SDOperand LD;
347 if (AlphaLowering.hasITOF()) {
348 LD = CurDAG->getNode(AlphaISD::FTOIT_, MVT::i64, cmp);
349 } else {
350 int FrameIdx =
351 CurDAG->getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
352 SDOperand FI = CurDAG->getFrameIndex(FrameIdx, MVT::i64);
353 SDOperand ST = CurDAG->getTargetNode(Alpha::STT, MVT::Other,
354 cmp, FI, CurDAG->getRegister(Alpha::R31, MVT::i64));
355 LD = CurDAG->getTargetNode(Alpha::LDQ, MVT::i64, FI,
356 CurDAG->getRegister(Alpha::R31, MVT::i64),
357 ST);
358 }
359 SDOperand FP = CurDAG->getTargetNode(Alpha::CMPULT, MVT::i64,
360 CurDAG->getRegister(Alpha::R31, MVT::i64),
361 LD);
362 return FP;
363 }
364 break;
Andrew Lenharthcd804962005-11-30 16:10:29 +0000365
366 case ISD::SELECT:
367 if (MVT::isFloatingPoint(N->getValueType(0))) {
368 //move int to fp
Andrew Lenharthb2156f92005-11-30 17:11:20 +0000369 bool isDouble = N->getValueType(0) == MVT::f64;
Andrew Lenharthcd804962005-11-30 16:10:29 +0000370 SDOperand LD,
371 cond = Select(N->getOperand(0)),
372 TV = Select(N->getOperand(1)),
373 FV = Select(N->getOperand(2));
374
375 if (AlphaLowering.hasITOF()) {
376 LD = CurDAG->getNode(AlphaISD::ITOFT_, MVT::f64, cond);
377 } else {
378 int FrameIdx =
379 CurDAG->getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
380 SDOperand FI = CurDAG->getFrameIndex(FrameIdx, MVT::i64);
381 SDOperand ST = CurDAG->getTargetNode(Alpha::STQ, MVT::Other,
382 cond, FI, CurDAG->getRegister(Alpha::R31, MVT::i64));
383 LD = CurDAG->getTargetNode(Alpha::LDT, MVT::f64, FI,
384 CurDAG->getRegister(Alpha::R31, MVT::i64),
385 ST);
386 }
Andrew Lenharthb2156f92005-11-30 17:11:20 +0000387 SDOperand FP = CurDAG->getTargetNode(isDouble?Alpha::FCMOVEQT:Alpha::FCMOVEQS,
388 MVT::f64, TV, FV, LD);
Andrew Lenharthcd804962005-11-30 16:10:29 +0000389 return FP;
390 }
391 break;
392
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000393 }
Andrew Lenharthcd804962005-11-30 16:10:29 +0000394
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000395 return SelectCode(Op);
396}
397
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000398SDOperand AlphaDAGToDAGISel::SelectCALL(SDOperand Op) {
Andrew Lenharth50b37842005-11-22 04:20:06 +0000399 //TODO: add flag stuff to prevent nondeturministic breakage!
400
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000401 SDNode *N = Op.Val;
402 SDOperand Chain = Select(N->getOperand(0));
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000403 SDOperand Addr = Select(N->getOperand(1));
404
405// unsigned CallOpcode;
406 std::vector<SDOperand> CallOperands;
407 std::vector<MVT::ValueType> TypeOperands;
408
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000409 //grab the arguments
410 for(int i = 2, e = N->getNumOperands(); i < e; ++i) {
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000411 TypeOperands.push_back(N->getOperand(i).getValueType());
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000412 CallOperands.push_back(Select(N->getOperand(i)));
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000413 }
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000414 int count = N->getNumOperands() - 2;
415
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000416 static const unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
417 Alpha::R19, Alpha::R20, Alpha::R21};
418 static const unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
419 Alpha::F19, Alpha::F20, Alpha::F21};
420
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000421 for (int i = 0; i < std::min(6, count); ++i) {
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000422 if (MVT::isInteger(TypeOperands[i])) {
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000423 Chain = CurDAG->getCopyToReg(Chain, args_int[i], CallOperands[i]);
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000424 } else if (TypeOperands[i] == MVT::f32 || TypeOperands[i] == MVT::f64) {
Andrew Lenharth50b37842005-11-22 04:20:06 +0000425 Chain = CurDAG->getCopyToReg(Chain, args_float[i], CallOperands[i]);
426 } else
427 assert(0 && "Unknown operand");
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000428 }
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000429 for (int i = 6; i < count; ++i) {
430 unsigned Opc = Alpha::WTF;
431 if (MVT::isInteger(TypeOperands[i])) {
432 Opc = Alpha::STQ;
433 } else if (TypeOperands[i] == MVT::f32) {
434 Opc = Alpha::STS;
435 } else if (TypeOperands[i] == MVT::f64) {
436 Opc = Alpha::STT;
437 } else
438 assert(0 && "Unknown operand");
439 Chain = CurDAG->getTargetNode(Opc, MVT::Other, CallOperands[i],
440 getI64Imm((i - 6) * 8),
441 CurDAG->getRegister(Alpha::R30, MVT::i64),
442 Chain);
443 }
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000444
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000445 Chain = CurDAG->getCopyToReg(Chain, Alpha::R27, Addr);
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000446 // Finally, once everything is in registers to pass to the call, emit the
447 // call itself.
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000448 Chain = CurDAG->getTargetNode(Alpha::JSRDAG, MVT::Other, Chain );
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000449
450 std::vector<SDOperand> CallResults;
451
452 switch (N->getValueType(0)) {
453 default: assert(0 && "Unexpected ret value!");
454 case MVT::Other: break;
455 case MVT::i64:
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000456 Chain = CurDAG->getCopyFromReg(Chain, Alpha::R0, MVT::i64).getValue(1);
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000457 CallResults.push_back(Chain.getValue(0));
458 break;
Andrew Lenharth50b37842005-11-22 04:20:06 +0000459 case MVT::f32:
460 Chain = CurDAG->getCopyFromReg(Chain, Alpha::F0, MVT::f32).getValue(1);
461 CallResults.push_back(Chain.getValue(0));
462 break;
463 case MVT::f64:
464 Chain = CurDAG->getCopyFromReg(Chain, Alpha::F0, MVT::f64).getValue(1);
465 CallResults.push_back(Chain.getValue(0));
466 break;
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000467 }
468
469 CallResults.push_back(Chain);
470 for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
471 CodeGenMap[Op.getValue(i)] = CallResults[i];
472 return CallResults[Op.ResNo];
473}
474
475
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000476/// createAlphaISelDag - This pass converts a legalized DAG into a
477/// Alpha-specific DAG, ready for instruction scheduling.
478///
479FunctionPass *llvm::createAlphaISelDag(TargetMachine &TM) {
480 return new AlphaDAGToDAGISel(TM);
481}