blob: 838841d1f80884e807323931930ee66697b89afe [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.
Andrew Lenharthd97591a2005-10-20 00:29:02 +000038 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();
Andrew Lenharth93526222005-12-01 01:53:10 +000072 SDOperand getRASaveReg();
Andrew Lenharth756fbeb2005-10-22 22:06:58 +000073 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() {
Andrew Lenharth93526222005-12-01 01:53:10 +000082 return CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
83 AlphaLowering.getVRegGP(),
84 MVT::i64);
85}
86
87/// getRASaveReg - Grab the return address
88///
89SDOperand AlphaDAGToDAGISel::getRASaveReg() {
90 return CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
91 AlphaLowering.getVRegRA(),
92 MVT::i64);
Andrew Lenharth756fbeb2005-10-22 22:06:58 +000093}
94
Andrew Lenharthd97591a2005-10-20 00:29:02 +000095/// InstructionSelectBasicBlock - This callback is invoked by
96/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
97void AlphaDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
98 DEBUG(BB->dump());
99
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000100 // Select target instructions for the DAG.
101 DAG.setRoot(Select(DAG.getRoot()));
102 CodeGenMap.clear();
103 DAG.RemoveDeadNodes();
104
105 // Emit machine code to BB.
106 ScheduleAndEmitDAG(DAG);
107}
108
109// Select - Convert the specified operand from a target-independent to a
110// target-specific node if it hasn't already been changed.
111SDOperand AlphaDAGToDAGISel::Select(SDOperand Op) {
112 SDNode *N = Op.Val;
113 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
114 N->getOpcode() < AlphaISD::FIRST_NUMBER)
115 return Op; // Already selected.
116
117 // If this has already been converted, use it.
118 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
119 if (CGMI != CodeGenMap.end()) return CGMI->second;
120
121 switch (N->getOpcode()) {
122 default: break;
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000123 case ISD::TAILCALL:
124 case ISD::CALL: return SelectCALL(Op);
125
Andrew Lenharth50b37842005-11-22 04:20:06 +0000126 case ISD::DYNAMIC_STACKALLOC: {
127 if (!isa<ConstantSDNode>(N->getOperand(2)) ||
128 cast<ConstantSDNode>(N->getOperand(2))->getValue() != 0) {
129 std::cerr << "Cannot allocate stack object with greater alignment than"
130 << " the stack alignment yet!";
131 abort();
132 }
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000133
Andrew Lenharth50b37842005-11-22 04:20:06 +0000134 SDOperand Chain = Select(N->getOperand(0));
135 SDOperand Amt = Select(N->getOperand(1));
136 SDOperand Reg = CurDAG->getRegister(Alpha::R30, MVT::i64);
137 SDOperand Val = CurDAG->getCopyFromReg(Chain, Alpha::R30, MVT::i64);
138 Chain = Val.getValue(1);
139
140 // Subtract the amount (guaranteed to be a multiple of the stack alignment)
141 // from the stack pointer, giving us the result pointer.
142 SDOperand Result = CurDAG->getTargetNode(Alpha::SUBQ, MVT::i64, Val, Amt);
143
144 // Copy this result back into R30.
145 Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, Chain, Reg, Result);
146
147 // Copy this result back out of R30 to make sure we're not using the stack
148 // space without decrementing the stack pointer.
149 Result = CurDAG->getCopyFromReg(Chain, Alpha::R30, MVT::i64);
150
151 // Finally, replace the DYNAMIC_STACKALLOC with the copyfromreg.
152 CodeGenMap[Op.getValue(0)] = Result;
153 CodeGenMap[Op.getValue(1)] = Result.getValue(1);
154 return SDOperand(Result.Val, Op.ResNo);
155 }
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000156 case ISD::BRCOND: {
Andrew Lenharthf88471d2005-12-06 20:43:30 +0000157 if (N->getOperand(1).getOpcode() == ISD::SETCC &&
158 MVT::isFloatingPoint(N->getOperand(1).getOperand(0).getValueType())) {
159 SDOperand Chain = Select(N->getOperand(0));
160 SDOperand CC1 = Select(N->getOperand(1).getOperand(0));
161 SDOperand CC2 = Select(N->getOperand(1).getOperand(1));
162 ISD::CondCode cCode= cast<CondCodeSDNode>(N->getOperand(1).getOperand(2))->get();
163
164 bool rev = false;
165 bool isNE = false;
166 unsigned Opc = Alpha::WTF;
167 switch(cCode) {
168 default: N->dump(); assert(0 && "Unknown FP comparison!");
169 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
170 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
171 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
172 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
173 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
174 case ISD::SETNE: Opc = Alpha::CMPTEQ; isNE = true; break;
175 };
176 SDOperand cmp = CurDAG->getTargetNode(Opc, MVT::f64,
177 rev?CC2:CC1,
178 rev?CC1:CC2);
179
180 MachineBasicBlock *Dest =
181 cast<BasicBlockSDNode>(N->getOperand(2))->getBasicBlock();
182 if(isNE)
183 return CurDAG->SelectNodeTo(N, Alpha::FBEQ, MVT::Other, cmp,
184 CurDAG->getBasicBlock(Dest), Chain);
185 else
186 return CurDAG->SelectNodeTo(N, Alpha::FBNE, MVT::Other, cmp,
187 CurDAG->getBasicBlock(Dest), Chain);
188 }
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000189 SDOperand Chain = Select(N->getOperand(0));
190 SDOperand CC = Select(N->getOperand(1));
Andrew Lenharth50b37842005-11-22 04:20:06 +0000191 MachineBasicBlock *Dest =
192 cast<BasicBlockSDNode>(N->getOperand(2))->getBasicBlock();
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000193 return CurDAG->SelectNodeTo(N, Alpha::BNE, MVT::Other, CC,
194 CurDAG->getBasicBlock(Dest), Chain);
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000195 }
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000196
Andrew Lenharthf88471d2005-12-06 20:43:30 +0000197 case ISD::BR:
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000198 return CurDAG->SelectNodeTo(N, Alpha::BR_DAG, MVT::Other, N->getOperand(1),
199 Select(N->getOperand(0)));
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000200 case ISD::FrameIndex: {
Andrew Lenharth50b37842005-11-22 04:20:06 +0000201 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000202 return CurDAG->SelectNodeTo(N, Alpha::LDA, MVT::i64,
203 CurDAG->getTargetFrameIndex(FI, MVT::i32),
204 getI64Imm(0));
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000205 }
Andrew Lenharth4e629512005-12-24 05:36:33 +0000206 case AlphaISD::GlobalBaseReg:
207 return getGlobalBaseReg();
208
209 case ISD::TargetConstantPool: {
Andrew Lenharth50b37842005-11-22 04:20:06 +0000210 Constant *C = cast<ConstantPoolSDNode>(N)->get();
211 SDOperand Tmp, CPI = CurDAG->getTargetConstantPool(C, MVT::i64);
212 Tmp = CurDAG->getTargetNode(Alpha::LDAHr, MVT::i64, CPI, getGlobalBaseReg());
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000213 return CurDAG->SelectNodeTo(N, Alpha::LDAr, MVT::i64, CPI, Tmp);
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000214 }
Andrew Lenharth4e629512005-12-24 05:36:33 +0000215 case ISD::TargetGlobalAddress: {
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000216 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
217 SDOperand GA = CurDAG->getTargetGlobalAddress(GV, MVT::i64);
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000218 return CurDAG->SelectNodeTo(N, Alpha::LDQl, MVT::i64, GA,
219 getGlobalBaseReg());
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000220 }
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000221 case ISD::ExternalSymbol:
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000222 return CurDAG->SelectNodeTo(N, Alpha::LDQl, MVT::i64,
223 CurDAG->getTargetExternalSymbol(cast<ExternalSymbolSDNode>(N)->getSymbol(), MVT::i64),
Andrew Lenharth93526222005-12-01 01:53:10 +0000224 getGlobalBaseReg());
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000225
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000226 case ISD::RET: {
227 SDOperand Chain = Select(N->getOperand(0)); // Token chain.
Andrew Lenharth93526222005-12-01 01:53:10 +0000228 SDOperand InFlag;
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000229
230 if (N->getNumOperands() == 2) {
231 SDOperand Val = Select(N->getOperand(1));
232 if (N->getOperand(1).getValueType() == MVT::i64) {
Andrew Lenharth93526222005-12-01 01:53:10 +0000233 Chain = CurDAG->getCopyToReg(Chain, Alpha::R0, Val, InFlag);
234 InFlag = Chain.getValue(1);
Andrew Lenharthe41419f2005-12-11 03:54:31 +0000235 } else if (N->getOperand(1).getValueType() == MVT::f64 ||
236 N->getOperand(1).getValueType() == MVT::f32) {
237 Chain = CurDAG->getCopyToReg(Chain, Alpha::F0, Val, InFlag);
238 InFlag = Chain.getValue(1);
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000239 }
240 }
Andrew Lenharth93526222005-12-01 01:53:10 +0000241 Chain = CurDAG->getCopyToReg(Chain, Alpha::R26, getRASaveReg(), InFlag);
242 InFlag = Chain.getValue(1);
243
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000244 // Finally, select this to a ret instruction.
Andrew Lenharth93526222005-12-01 01:53:10 +0000245 return CurDAG->SelectNodeTo(N, Alpha::RETDAG, MVT::Other, Chain, InFlag);
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000246 }
Andrew Lenharth50b37842005-11-22 04:20:06 +0000247 case ISD::Constant: {
248 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
249 if (val > (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT ||
250 val < (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
251 MachineConstantPool *CP = BB->getParent()->getConstantPool();
252 ConstantUInt *C =
253 ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
254 SDOperand Tmp, CPI = CurDAG->getTargetConstantPool(C, MVT::i64);
255 Tmp = CurDAG->getTargetNode(Alpha::LDAHr, MVT::i64, CPI, getGlobalBaseReg());
Andrew Lenharthb457a932005-12-05 17:51:02 +0000256 return CurDAG->SelectNodeTo(N, Alpha::LDQr, MVT::i64, CPI, Tmp);
Andrew Lenharth50b37842005-11-22 04:20:06 +0000257 }
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000258 break;
Andrew Lenharth50b37842005-11-22 04:20:06 +0000259 }
260 case ISD::ConstantFP:
261 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
262 bool isDouble = N->getValueType(0) == MVT::f64;
263 MVT::ValueType T = isDouble ? MVT::f64 : MVT::f32;
264 if (CN->isExactlyValue(+0.0)) {
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000265 return CurDAG->SelectNodeTo(N, isDouble ? Alpha::CPYST : Alpha::CPYSS,
266 T, CurDAG->getRegister(Alpha::F31, T),
267 CurDAG->getRegister(Alpha::F31, T));
Andrew Lenharth50b37842005-11-22 04:20:06 +0000268 } else if ( CN->isExactlyValue(-0.0)) {
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000269 return CurDAG->SelectNodeTo(N, isDouble ? Alpha::CPYSNT : Alpha::CPYSNS,
270 T, CurDAG->getRegister(Alpha::F31, T),
271 CurDAG->getRegister(Alpha::F31, T));
Andrew Lenharth50b37842005-11-22 04:20:06 +0000272 } else {
273 abort();
274 }
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000275 break;
Andrew Lenharth50b37842005-11-22 04:20:06 +0000276 }
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000277 case ISD::SDIV:
278 case ISD::UDIV:
279 case ISD::UREM:
280 case ISD::SREM:
281 if (MVT::isInteger(N->getValueType(0))) {
282 const char* opstr = 0;
283 switch(N->getOpcode()) {
284 case ISD::UREM: opstr = "__remqu"; break;
285 case ISD::SREM: opstr = "__remq"; break;
286 case ISD::UDIV: opstr = "__divqu"; break;
287 case ISD::SDIV: opstr = "__divq"; break;
288 }
289 SDOperand Tmp1 = Select(N->getOperand(0)),
290 Tmp2 = Select(N->getOperand(1)),
Andrew Lenharthbbe12252005-12-06 23:27:39 +0000291 Addr = Select(CurDAG->getExternalSymbol(opstr,
292 AlphaLowering.getPointerTy()));
293 SDOperand Chain;
294 Chain = CurDAG->getCopyToReg(CurDAG->getEntryNode(), Alpha::R24, Tmp1,
295 SDOperand(0,0));
296 Chain = CurDAG->getCopyToReg(Chain, Alpha::R25, Tmp2, Chain.getValue(1));
297 Chain = CurDAG->getCopyToReg(Chain, Alpha::R27, Addr, Chain.getValue(1));
298 Chain = CurDAG->getTargetNode(Alpha::JSRsDAG, MVT::Other, MVT::Flag,
299 Chain, Chain.getValue(1));
300 return CurDAG->getCopyFromReg(Chain, Alpha::R27, MVT::i64,
301 Chain.getValue(1));
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000302 }
303 break;
304
305 case ISD::SETCC:
306 if (MVT::isFloatingPoint(N->getOperand(0).Val->getValueType(0))) {
307 unsigned Opc = Alpha::WTF;
308 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
309 bool rev = false;
Andrew Lenharthb2156f92005-11-30 17:11:20 +0000310 bool isNE = false;
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000311 switch(CC) {
312 default: N->dump(); assert(0 && "Unknown FP comparison!");
313 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
314 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
315 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
316 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
317 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
Andrew Lenharthb2156f92005-11-30 17:11:20 +0000318 case ISD::SETNE: Opc = Alpha::CMPTEQ; isNE = true; break;
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000319 };
320 SDOperand tmp1 = Select(N->getOperand(0)),
321 tmp2 = Select(N->getOperand(1));
322 SDOperand cmp = CurDAG->getTargetNode(Opc, MVT::f64,
323 rev?tmp2:tmp1,
324 rev?tmp1:tmp2);
Andrew Lenharthb2156f92005-11-30 17:11:20 +0000325 if (isNE)
326 cmp = CurDAG->getTargetNode(Alpha::CMPTEQ, MVT::f64, cmp,
327 CurDAG->getRegister(Alpha::F31, MVT::f64));
328
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000329 SDOperand LD;
330 if (AlphaLowering.hasITOF()) {
331 LD = CurDAG->getNode(AlphaISD::FTOIT_, MVT::i64, cmp);
332 } else {
333 int FrameIdx =
334 CurDAG->getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
335 SDOperand FI = CurDAG->getFrameIndex(FrameIdx, MVT::i64);
336 SDOperand ST = CurDAG->getTargetNode(Alpha::STT, MVT::Other,
337 cmp, FI, CurDAG->getRegister(Alpha::R31, MVT::i64));
338 LD = CurDAG->getTargetNode(Alpha::LDQ, MVT::i64, FI,
339 CurDAG->getRegister(Alpha::R31, MVT::i64),
340 ST);
341 }
342 SDOperand FP = CurDAG->getTargetNode(Alpha::CMPULT, MVT::i64,
343 CurDAG->getRegister(Alpha::R31, MVT::i64),
344 LD);
345 return FP;
346 }
347 break;
Andrew Lenharthcd804962005-11-30 16:10:29 +0000348
Andrew Lenharth361f45a2005-12-12 17:43:52 +0000349 case ISD::SELECT:
350 if (MVT::isFloatingPoint(N->getValueType(0)) &&
351 (N->getOperand(0).getOpcode() != ISD::SETCC ||
352 !MVT::isFloatingPoint(N->getOperand(0).getOperand(1).getValueType()))) {
353 //This should be the condition not covered by the Patterns
354 //FIXME: Don't have SelectCode die, but rather return something testable
355 // so that things like this can be caught in fall though code
356 //move int to fp
357 bool isDouble = N->getValueType(0) == MVT::f64;
358 SDOperand LD,
359 cond = Select(N->getOperand(0)),
360 TV = Select(N->getOperand(1)),
361 FV = Select(N->getOperand(2));
362
363 if (AlphaLowering.hasITOF()) {
364 LD = CurDAG->getNode(AlphaISD::ITOFT_, MVT::f64, cond);
365 } else {
366 int FrameIdx =
367 CurDAG->getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
368 SDOperand FI = CurDAG->getFrameIndex(FrameIdx, MVT::i64);
369 SDOperand ST = CurDAG->getTargetNode(Alpha::STQ, MVT::Other,
370 cond, FI, CurDAG->getRegister(Alpha::R31, MVT::i64));
371 LD = CurDAG->getTargetNode(Alpha::LDT, MVT::f64, FI,
372 CurDAG->getRegister(Alpha::R31, MVT::i64),
373 ST);
374 }
Andrew Lenharth110f2242005-12-12 20:30:09 +0000375 SDOperand FP = CurDAG->getTargetNode(isDouble?Alpha::FCMOVNET:Alpha::FCMOVNES,
Andrew Lenharth361f45a2005-12-12 17:43:52 +0000376 MVT::f64, FV, TV, LD);
377 return FP;
378 }
379 break;
380
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000381 }
Andrew Lenharthcd804962005-11-30 16:10:29 +0000382
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000383 return SelectCode(Op);
384}
385
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000386SDOperand AlphaDAGToDAGISel::SelectCALL(SDOperand Op) {
Andrew Lenharth50b37842005-11-22 04:20:06 +0000387 //TODO: add flag stuff to prevent nondeturministic breakage!
388
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000389 SDNode *N = Op.Val;
390 SDOperand Chain = Select(N->getOperand(0));
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000391 SDOperand Addr = Select(N->getOperand(1));
Andrew Lenharth93526222005-12-01 01:53:10 +0000392 SDOperand InFlag; // Null incoming flag value.
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000393
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000394 std::vector<SDOperand> CallOperands;
395 std::vector<MVT::ValueType> TypeOperands;
396
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000397 //grab the arguments
398 for(int i = 2, e = N->getNumOperands(); i < e; ++i) {
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000399 TypeOperands.push_back(N->getOperand(i).getValueType());
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000400 CallOperands.push_back(Select(N->getOperand(i)));
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000401 }
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000402 int count = N->getNumOperands() - 2;
403
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000404 static const unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
405 Alpha::R19, Alpha::R20, Alpha::R21};
406 static const unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
407 Alpha::F19, Alpha::F20, Alpha::F21};
408
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000409 for (int i = 6; i < count; ++i) {
410 unsigned Opc = Alpha::WTF;
411 if (MVT::isInteger(TypeOperands[i])) {
412 Opc = Alpha::STQ;
413 } else if (TypeOperands[i] == MVT::f32) {
414 Opc = Alpha::STS;
415 } else if (TypeOperands[i] == MVT::f64) {
416 Opc = Alpha::STT;
417 } else
418 assert(0 && "Unknown operand");
419 Chain = CurDAG->getTargetNode(Opc, MVT::Other, CallOperands[i],
420 getI64Imm((i - 6) * 8),
Andrew Lenharth93526222005-12-01 01:53:10 +0000421 CurDAG->getCopyFromReg(Chain, Alpha::R30, MVT::i64),
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000422 Chain);
423 }
Andrew Lenharth93526222005-12-01 01:53:10 +0000424 for (int i = 0; i < std::min(6, count); ++i) {
425 if (MVT::isInteger(TypeOperands[i])) {
426 Chain = CurDAG->getCopyToReg(Chain, args_int[i], CallOperands[i], InFlag);
427 InFlag = Chain.getValue(1);
428 } else if (TypeOperands[i] == MVT::f32 || TypeOperands[i] == MVT::f64) {
429 Chain = CurDAG->getCopyToReg(Chain, args_float[i], CallOperands[i], InFlag);
430 InFlag = Chain.getValue(1);
431 } else
432 assert(0 && "Unknown operand");
433 }
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000434
Andrew Lenharth93526222005-12-01 01:53:10 +0000435
436 Chain = CurDAG->getCopyToReg(Chain, Alpha::R27, Addr, InFlag);
437 InFlag = Chain.getValue(1);
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000438 // Finally, once everything is in registers to pass to the call, emit the
439 // call itself.
Andrew Lenharth93526222005-12-01 01:53:10 +0000440 Chain = CurDAG->getTargetNode(Alpha::JSRDAG, MVT::Other, MVT::Flag,
441 Chain, InFlag );
442 InFlag = Chain.getValue(1);
443
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000444 std::vector<SDOperand> CallResults;
445
446 switch (N->getValueType(0)) {
447 default: assert(0 && "Unexpected ret value!");
448 case MVT::Other: break;
449 case MVT::i64:
Andrew Lenharth93526222005-12-01 01:53:10 +0000450 Chain = CurDAG->getCopyFromReg(Chain, Alpha::R0, MVT::i64, InFlag).getValue(1);
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000451 CallResults.push_back(Chain.getValue(0));
452 break;
Andrew Lenharth50b37842005-11-22 04:20:06 +0000453 case MVT::f32:
Andrew Lenharth93526222005-12-01 01:53:10 +0000454 Chain = CurDAG->getCopyFromReg(Chain, Alpha::F0, MVT::f32, InFlag).getValue(1);
Andrew Lenharth50b37842005-11-22 04:20:06 +0000455 CallResults.push_back(Chain.getValue(0));
456 break;
457 case MVT::f64:
Andrew Lenharth93526222005-12-01 01:53:10 +0000458 Chain = CurDAG->getCopyFromReg(Chain, Alpha::F0, MVT::f64, InFlag).getValue(1);
Andrew Lenharth50b37842005-11-22 04:20:06 +0000459 CallResults.push_back(Chain.getValue(0));
460 break;
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000461 }
462
463 CallResults.push_back(Chain);
464 for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
465 CodeGenMap[Op.getValue(i)] = CallResults[i];
466 return CallResults[Op.ResNo];
467}
468
469
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000470/// createAlphaISelDag - This pass converts a legalized DAG into a
471/// Alpha-specific DAG, ready for instruction scheduling.
472///
473FunctionPass *llvm::createAlphaISelDag(TargetMachine &TM) {
474 return new AlphaDAGToDAGISel(TM);
475}