blob: 1d876ade00ebc861ca27035c4e9558a47e954d06 [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();
Andrew Lenharth93526222005-12-01 01:53:10 +000073 SDOperand getRASaveReg();
Andrew Lenharth756fbeb2005-10-22 22:06:58 +000074 SDOperand SelectCALL(SDOperand Op);
75
Andrew Lenharthd97591a2005-10-20 00:29:02 +000076 };
77}
78
Andrew Lenharth756fbeb2005-10-22 22:06:58 +000079/// getGlobalBaseReg - Output the instructions required to put the
80/// GOT address into a register.
81///
82SDOperand AlphaDAGToDAGISel::getGlobalBaseReg() {
Andrew Lenharth93526222005-12-01 01:53:10 +000083 return CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
84 AlphaLowering.getVRegGP(),
85 MVT::i64);
86}
87
88/// getRASaveReg - Grab the return address
89///
90SDOperand AlphaDAGToDAGISel::getRASaveReg() {
91 return CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
92 AlphaLowering.getVRegRA(),
93 MVT::i64);
Andrew Lenharth756fbeb2005-10-22 22:06:58 +000094}
95
Andrew Lenharthd97591a2005-10-20 00:29:02 +000096/// InstructionSelectBasicBlock - This callback is invoked by
97/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
98void AlphaDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
99 DEBUG(BB->dump());
100
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000101 // Select target instructions for the DAG.
102 DAG.setRoot(Select(DAG.getRoot()));
103 CodeGenMap.clear();
104 DAG.RemoveDeadNodes();
105
106 // Emit machine code to BB.
107 ScheduleAndEmitDAG(DAG);
108}
109
110// Select - Convert the specified operand from a target-independent to a
111// target-specific node if it hasn't already been changed.
112SDOperand AlphaDAGToDAGISel::Select(SDOperand Op) {
113 SDNode *N = Op.Val;
114 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
115 N->getOpcode() < AlphaISD::FIRST_NUMBER)
116 return Op; // Already selected.
117
118 // If this has already been converted, use it.
119 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
120 if (CGMI != CodeGenMap.end()) return CGMI->second;
121
122 switch (N->getOpcode()) {
123 default: break;
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000124 case ISD::TAILCALL:
125 case ISD::CALL: return SelectCALL(Op);
126
Andrew Lenharth50b37842005-11-22 04:20:06 +0000127 case ISD::DYNAMIC_STACKALLOC: {
128 if (!isa<ConstantSDNode>(N->getOperand(2)) ||
129 cast<ConstantSDNode>(N->getOperand(2))->getValue() != 0) {
130 std::cerr << "Cannot allocate stack object with greater alignment than"
131 << " the stack alignment yet!";
132 abort();
133 }
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000134
Andrew Lenharth50b37842005-11-22 04:20:06 +0000135 SDOperand Chain = Select(N->getOperand(0));
136 SDOperand Amt = Select(N->getOperand(1));
137 SDOperand Reg = CurDAG->getRegister(Alpha::R30, MVT::i64);
138 SDOperand Val = CurDAG->getCopyFromReg(Chain, Alpha::R30, MVT::i64);
139 Chain = Val.getValue(1);
140
141 // Subtract the amount (guaranteed to be a multiple of the stack alignment)
142 // from the stack pointer, giving us the result pointer.
143 SDOperand Result = CurDAG->getTargetNode(Alpha::SUBQ, MVT::i64, Val, Amt);
144
145 // Copy this result back into R30.
146 Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, Chain, Reg, Result);
147
148 // Copy this result back out of R30 to make sure we're not using the stack
149 // space without decrementing the stack pointer.
150 Result = CurDAG->getCopyFromReg(Chain, Alpha::R30, MVT::i64);
151
152 // Finally, replace the DYNAMIC_STACKALLOC with the copyfromreg.
153 CodeGenMap[Op.getValue(0)] = Result;
154 CodeGenMap[Op.getValue(1)] = Result.getValue(1);
155 return SDOperand(Result.Val, Op.ResNo);
156 }
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000157 case ISD::BRCOND: {
158 SDOperand Chain = Select(N->getOperand(0));
159 SDOperand CC = Select(N->getOperand(1));
Andrew Lenharth50b37842005-11-22 04:20:06 +0000160 MachineBasicBlock *Dest =
161 cast<BasicBlockSDNode>(N->getOperand(2))->getBasicBlock();
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000162 return CurDAG->SelectNodeTo(N, Alpha::BNE, MVT::Other, CC,
163 CurDAG->getBasicBlock(Dest), Chain);
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000164 }
165 case ISD::LOAD:
166 case ISD::EXTLOAD:
167 case ISD::ZEXTLOAD:
168 case ISD::SEXTLOAD: {
169 SDOperand Chain = Select(N->getOperand(0));
170 SDOperand Address = Select(N->getOperand(1));
171 unsigned opcode = N->getOpcode();
172 unsigned Opc = Alpha::WTF;
173 if (opcode == ISD::LOAD)
174 switch (N->getValueType(0)) {
175 default: N->dump(); assert(0 && "Bad load!");
176 case MVT::i64: Opc = Alpha::LDQ; break;
177 case MVT::f64: Opc = Alpha::LDT; break;
178 case MVT::f32: Opc = Alpha::LDS; break;
179 }
180 else
181 switch (cast<VTSDNode>(N->getOperand(3))->getVT()) {
182 default: N->dump(); assert(0 && "Bad sign extend!");
183 case MVT::i32: Opc = Alpha::LDL;
184 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
185 case MVT::i16: Opc = Alpha::LDWU;
186 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
187 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
188 case MVT::i8: Opc = Alpha::LDBU;
189 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
190 }
191
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000192 return CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), MVT::Other,
193 getI64Imm(0), Address,
194 Chain).getValue(Op.ResNo);
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000195 }
Andrew Lenharth50b37842005-11-22 04:20:06 +0000196 case ISD::STORE:
197 case ISD::TRUNCSTORE: {
198 SDOperand Chain = Select(N->getOperand(0));
199 SDOperand Value = Select(N->getOperand(1));
200 SDOperand Address = Select(N->getOperand(2));
201
202 unsigned Opc = Alpha::WTF;
203
204 if (N->getOpcode() == ISD::STORE) {
205 switch (N->getOperand(1).getValueType()) {
206 case MVT::i64: Opc = Alpha::STQ; break;
207 case MVT::f64: Opc = Alpha::STT; break;
208 case MVT::f32: Opc = Alpha::STS; break;
209 default: assert(0 && "Bad store!");
210 };
211 } else { //TRUNCSTORE
212 switch (cast<VTSDNode>(N->getOperand(4))->getVT()) {
213 case MVT::i32: Opc = Alpha::STL; break;
214 case MVT::i16: Opc = Alpha::STW; break;
215 case MVT::i8: Opc = Alpha::STB; break;
216 default: assert(0 && "Bad truncstore!");
217 };
218 }
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000219 return CurDAG->SelectNodeTo(N, Opc, MVT::Other, Value, getI64Imm(0),
220 Address, Chain);
Andrew Lenharth50b37842005-11-22 04:20:06 +0000221 }
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000222
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000223 case ISD::BR:
224 return CurDAG->SelectNodeTo(N, Alpha::BR_DAG, MVT::Other, N->getOperand(1),
225 Select(N->getOperand(0)));
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000226
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000227 case ISD::FrameIndex: {
Andrew Lenharth50b37842005-11-22 04:20:06 +0000228 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000229 return CurDAG->SelectNodeTo(N, Alpha::LDA, MVT::i64,
230 CurDAG->getTargetFrameIndex(FI, MVT::i32),
231 getI64Imm(0));
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000232 }
233 case ISD::ConstantPool: {
Andrew Lenharth50b37842005-11-22 04:20:06 +0000234 Constant *C = cast<ConstantPoolSDNode>(N)->get();
235 SDOperand Tmp, CPI = CurDAG->getTargetConstantPool(C, MVT::i64);
236 Tmp = CurDAG->getTargetNode(Alpha::LDAHr, MVT::i64, CPI, getGlobalBaseReg());
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000237 return CurDAG->SelectNodeTo(N, Alpha::LDAr, MVT::i64, CPI, Tmp);
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000238 }
239 case ISD::GlobalAddress: {
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000240 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
241 SDOperand GA = CurDAG->getTargetGlobalAddress(GV, MVT::i64);
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000242 return CurDAG->SelectNodeTo(N, Alpha::LDQl, MVT::i64, GA,
243 getGlobalBaseReg());
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000244 }
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000245 case ISD::ExternalSymbol:
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000246 return CurDAG->SelectNodeTo(N, Alpha::LDQl, MVT::i64,
247 CurDAG->getTargetExternalSymbol(cast<ExternalSymbolSDNode>(N)->getSymbol(), MVT::i64),
Andrew Lenharth93526222005-12-01 01:53:10 +0000248 getGlobalBaseReg());
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000249
250 case ISD::CALLSEQ_START:
251 case ISD::CALLSEQ_END: {
252 unsigned Amt = cast<ConstantSDNode>(N->getOperand(1))->getValue();
253 unsigned Opc = N->getOpcode() == ISD::CALLSEQ_START ?
254 Alpha::ADJUSTSTACKDOWN : Alpha::ADJUSTSTACKUP;
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000255 return CurDAG->SelectNodeTo(N, Opc, MVT::Other,
256 getI64Imm(Amt), Select(N->getOperand(0)));
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000257 }
258 case ISD::RET: {
259 SDOperand Chain = Select(N->getOperand(0)); // Token chain.
Andrew Lenharth93526222005-12-01 01:53:10 +0000260 SDOperand InFlag;
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000261
262 if (N->getNumOperands() == 2) {
263 SDOperand Val = Select(N->getOperand(1));
264 if (N->getOperand(1).getValueType() == MVT::i64) {
Andrew Lenharth93526222005-12-01 01:53:10 +0000265 Chain = CurDAG->getCopyToReg(Chain, Alpha::R0, Val, InFlag);
266 InFlag = Chain.getValue(1);
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000267 }
268 }
Andrew Lenharth93526222005-12-01 01:53:10 +0000269 Chain = CurDAG->getCopyToReg(Chain, Alpha::R26, getRASaveReg(), InFlag);
270 InFlag = Chain.getValue(1);
271
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000272 // Finally, select this to a ret instruction.
Andrew Lenharth93526222005-12-01 01:53:10 +0000273 return CurDAG->SelectNodeTo(N, Alpha::RETDAG, MVT::Other, Chain, InFlag);
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000274 }
Andrew Lenharth50b37842005-11-22 04:20:06 +0000275 case ISD::Constant: {
276 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
277 if (val > (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT ||
278 val < (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
279 MachineConstantPool *CP = BB->getParent()->getConstantPool();
280 ConstantUInt *C =
281 ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
282 SDOperand Tmp, CPI = CurDAG->getTargetConstantPool(C, MVT::i64);
283 Tmp = CurDAG->getTargetNode(Alpha::LDAHr, MVT::i64, CPI, getGlobalBaseReg());
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000284 return CurDAG->SelectNodeTo(N, Alpha::LDAr, MVT::i64, CPI, Tmp);
Andrew Lenharth50b37842005-11-22 04:20:06 +0000285 }
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000286 break;
Andrew Lenharth50b37842005-11-22 04:20:06 +0000287 }
288 case ISD::ConstantFP:
289 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
290 bool isDouble = N->getValueType(0) == MVT::f64;
291 MVT::ValueType T = isDouble ? MVT::f64 : MVT::f32;
292 if (CN->isExactlyValue(+0.0)) {
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000293 return CurDAG->SelectNodeTo(N, isDouble ? Alpha::CPYST : Alpha::CPYSS,
294 T, CurDAG->getRegister(Alpha::F31, T),
295 CurDAG->getRegister(Alpha::F31, T));
Andrew Lenharth50b37842005-11-22 04:20:06 +0000296 } else if ( CN->isExactlyValue(-0.0)) {
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000297 return CurDAG->SelectNodeTo(N, isDouble ? Alpha::CPYSNT : Alpha::CPYSNS,
298 T, CurDAG->getRegister(Alpha::F31, T),
299 CurDAG->getRegister(Alpha::F31, T));
Andrew Lenharth50b37842005-11-22 04:20:06 +0000300 } else {
301 abort();
302 }
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000303 break;
Andrew Lenharth50b37842005-11-22 04:20:06 +0000304 }
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000305 case ISD::SDIV:
306 case ISD::UDIV:
307 case ISD::UREM:
308 case ISD::SREM:
309 if (MVT::isInteger(N->getValueType(0))) {
310 const char* opstr = 0;
311 switch(N->getOpcode()) {
312 case ISD::UREM: opstr = "__remqu"; break;
313 case ISD::SREM: opstr = "__remq"; break;
314 case ISD::UDIV: opstr = "__divqu"; break;
315 case ISD::SDIV: opstr = "__divq"; break;
316 }
317 SDOperand Tmp1 = Select(N->getOperand(0)),
318 Tmp2 = Select(N->getOperand(1)),
319 Addr = CurDAG->getExternalSymbol(opstr, AlphaLowering.getPointerTy());
320 SDOperand Tmp3 = Select(Addr);
321 SDOperand Chain = CurDAG->getCopyToReg(CurDAG->getRoot(), Alpha::R24,
322 Tmp1, SDOperand());
323 Chain = CurDAG->getCopyToReg(CurDAG->getRoot(), Alpha::R25,
324 Tmp2, Chain.getValue(1));
325 Chain = CurDAG->getCopyToReg(CurDAG->getRoot(), Alpha::R27,
326 Tmp3, Chain.getValue(1));
327 Chain = CurDAG->getTargetNode(Alpha::JSRs, MVT::i64, MVT::Flag,
328 CurDAG->getRegister(Alpha::R27, MVT::i64),
329 getI64Imm(0));
330 return CurDAG->getCopyFromReg(Chain.getValue(1), Alpha::R27, MVT::i64,
331 Chain.getValue(1));
332 }
333 break;
334
335 case ISD::SETCC:
336 if (MVT::isFloatingPoint(N->getOperand(0).Val->getValueType(0))) {
337 unsigned Opc = Alpha::WTF;
338 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
339 bool rev = false;
Andrew Lenharthb2156f92005-11-30 17:11:20 +0000340 bool isNE = false;
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000341 switch(CC) {
342 default: N->dump(); assert(0 && "Unknown FP comparison!");
343 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
344 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
345 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
346 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
347 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
Andrew Lenharthb2156f92005-11-30 17:11:20 +0000348 case ISD::SETNE: Opc = Alpha::CMPTEQ; isNE = true; break;
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000349 };
350 SDOperand tmp1 = Select(N->getOperand(0)),
351 tmp2 = Select(N->getOperand(1));
352 SDOperand cmp = CurDAG->getTargetNode(Opc, MVT::f64,
353 rev?tmp2:tmp1,
354 rev?tmp1:tmp2);
Andrew Lenharthb2156f92005-11-30 17:11:20 +0000355 if (isNE)
356 cmp = CurDAG->getTargetNode(Alpha::CMPTEQ, MVT::f64, cmp,
357 CurDAG->getRegister(Alpha::F31, MVT::f64));
358
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000359 SDOperand LD;
360 if (AlphaLowering.hasITOF()) {
361 LD = CurDAG->getNode(AlphaISD::FTOIT_, MVT::i64, cmp);
362 } else {
363 int FrameIdx =
364 CurDAG->getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
365 SDOperand FI = CurDAG->getFrameIndex(FrameIdx, MVT::i64);
366 SDOperand ST = CurDAG->getTargetNode(Alpha::STT, MVT::Other,
367 cmp, FI, CurDAG->getRegister(Alpha::R31, MVT::i64));
368 LD = CurDAG->getTargetNode(Alpha::LDQ, MVT::i64, FI,
369 CurDAG->getRegister(Alpha::R31, MVT::i64),
370 ST);
371 }
372 SDOperand FP = CurDAG->getTargetNode(Alpha::CMPULT, MVT::i64,
373 CurDAG->getRegister(Alpha::R31, MVT::i64),
374 LD);
375 return FP;
376 }
377 break;
Andrew Lenharthcd804962005-11-30 16:10:29 +0000378
379 case ISD::SELECT:
380 if (MVT::isFloatingPoint(N->getValueType(0))) {
381 //move int to fp
Andrew Lenharthb2156f92005-11-30 17:11:20 +0000382 bool isDouble = N->getValueType(0) == MVT::f64;
Andrew Lenharthcd804962005-11-30 16:10:29 +0000383 SDOperand LD,
384 cond = Select(N->getOperand(0)),
385 TV = Select(N->getOperand(1)),
386 FV = Select(N->getOperand(2));
387
388 if (AlphaLowering.hasITOF()) {
389 LD = CurDAG->getNode(AlphaISD::ITOFT_, MVT::f64, cond);
390 } else {
391 int FrameIdx =
392 CurDAG->getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
393 SDOperand FI = CurDAG->getFrameIndex(FrameIdx, MVT::i64);
394 SDOperand ST = CurDAG->getTargetNode(Alpha::STQ, MVT::Other,
395 cond, FI, CurDAG->getRegister(Alpha::R31, MVT::i64));
396 LD = CurDAG->getTargetNode(Alpha::LDT, MVT::f64, FI,
397 CurDAG->getRegister(Alpha::R31, MVT::i64),
398 ST);
399 }
Andrew Lenharthb2156f92005-11-30 17:11:20 +0000400 SDOperand FP = CurDAG->getTargetNode(isDouble?Alpha::FCMOVEQT:Alpha::FCMOVEQS,
401 MVT::f64, TV, FV, LD);
Andrew Lenharthcd804962005-11-30 16:10:29 +0000402 return FP;
403 }
404 break;
405
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000406 }
Andrew Lenharthcd804962005-11-30 16:10:29 +0000407
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000408 return SelectCode(Op);
409}
410
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000411SDOperand AlphaDAGToDAGISel::SelectCALL(SDOperand Op) {
Andrew Lenharth50b37842005-11-22 04:20:06 +0000412 //TODO: add flag stuff to prevent nondeturministic breakage!
413
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000414 SDNode *N = Op.Val;
415 SDOperand Chain = Select(N->getOperand(0));
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000416 SDOperand Addr = Select(N->getOperand(1));
Andrew Lenharth93526222005-12-01 01:53:10 +0000417 SDOperand InFlag; // Null incoming flag value.
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000418
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000419 std::vector<SDOperand> CallOperands;
420 std::vector<MVT::ValueType> TypeOperands;
421
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000422 //grab the arguments
423 for(int i = 2, e = N->getNumOperands(); i < e; ++i) {
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000424 TypeOperands.push_back(N->getOperand(i).getValueType());
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000425 CallOperands.push_back(Select(N->getOperand(i)));
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000426 }
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000427 int count = N->getNumOperands() - 2;
428
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000429 static const unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
430 Alpha::R19, Alpha::R20, Alpha::R21};
431 static const unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
432 Alpha::F19, Alpha::F20, Alpha::F21};
433
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000434 for (int i = 6; i < count; ++i) {
435 unsigned Opc = Alpha::WTF;
436 if (MVT::isInteger(TypeOperands[i])) {
437 Opc = Alpha::STQ;
438 } else if (TypeOperands[i] == MVT::f32) {
439 Opc = Alpha::STS;
440 } else if (TypeOperands[i] == MVT::f64) {
441 Opc = Alpha::STT;
442 } else
443 assert(0 && "Unknown operand");
444 Chain = CurDAG->getTargetNode(Opc, MVT::Other, CallOperands[i],
445 getI64Imm((i - 6) * 8),
Andrew Lenharth93526222005-12-01 01:53:10 +0000446 CurDAG->getCopyFromReg(Chain, Alpha::R30, MVT::i64),
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000447 Chain);
448 }
Andrew Lenharth93526222005-12-01 01:53:10 +0000449 for (int i = 0; i < std::min(6, count); ++i) {
450 if (MVT::isInteger(TypeOperands[i])) {
451 Chain = CurDAG->getCopyToReg(Chain, args_int[i], CallOperands[i], InFlag);
452 InFlag = Chain.getValue(1);
453 } else if (TypeOperands[i] == MVT::f32 || TypeOperands[i] == MVT::f64) {
454 Chain = CurDAG->getCopyToReg(Chain, args_float[i], CallOperands[i], InFlag);
455 InFlag = Chain.getValue(1);
456 } else
457 assert(0 && "Unknown operand");
458 }
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000459
Andrew Lenharth93526222005-12-01 01:53:10 +0000460
461 Chain = CurDAG->getCopyToReg(Chain, Alpha::R27, Addr, InFlag);
462 InFlag = Chain.getValue(1);
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000463 // Finally, once everything is in registers to pass to the call, emit the
464 // call itself.
Andrew Lenharth93526222005-12-01 01:53:10 +0000465 Chain = CurDAG->getTargetNode(Alpha::JSRDAG, MVT::Other, MVT::Flag,
466 Chain, InFlag );
467 InFlag = Chain.getValue(1);
468
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000469 std::vector<SDOperand> CallResults;
470
471 switch (N->getValueType(0)) {
472 default: assert(0 && "Unexpected ret value!");
473 case MVT::Other: break;
474 case MVT::i64:
Andrew Lenharth93526222005-12-01 01:53:10 +0000475 Chain = CurDAG->getCopyFromReg(Chain, Alpha::R0, MVT::i64, InFlag).getValue(1);
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000476 CallResults.push_back(Chain.getValue(0));
477 break;
Andrew Lenharth50b37842005-11-22 04:20:06 +0000478 case MVT::f32:
Andrew Lenharth93526222005-12-01 01:53:10 +0000479 Chain = CurDAG->getCopyFromReg(Chain, Alpha::F0, MVT::f32, InFlag).getValue(1);
Andrew Lenharth50b37842005-11-22 04:20:06 +0000480 CallResults.push_back(Chain.getValue(0));
481 break;
482 case MVT::f64:
Andrew Lenharth93526222005-12-01 01:53:10 +0000483 Chain = CurDAG->getCopyFromReg(Chain, Alpha::F0, MVT::f64, InFlag).getValue(1);
Andrew Lenharth50b37842005-11-22 04:20:06 +0000484 CallResults.push_back(Chain.getValue(0));
485 break;
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000486 }
487
488 CallResults.push_back(Chain);
489 for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
490 CodeGenMap[Op.getValue(i)] = CallResults[i];
491 return CallResults[Op.ResNo];
492}
493
494
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000495/// createAlphaISelDag - This pass converts a legalized DAG into a
496/// Alpha-specific DAG, ready for instruction scheduling.
497///
498FunctionPass *llvm::createAlphaISelDag(TargetMachine &TM) {
499 return new AlphaDAGToDAGISel(TM);
500}