blob: dd29e912529bc67bf98c434986236b9d7396b8fb [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 Lenharthdcbaf8a2005-12-30 02:30:02 +000041 static const int64_t IMM_LOW = -32768;
42 static const int64_t IMM_HIGH = 32767;
43 static const int64_t IMM_MULT = 65536;
Andrew Lenharth50b37842005-11-22 04:20:06 +000044
Andrew Lenharthd97591a2005-10-20 00:29:02 +000045 public:
46 AlphaDAGToDAGISel(TargetMachine &TM)
Andrew Lenharthdcbaf8a2005-12-30 02:30:02 +000047 : SelectionDAGISel(AlphaLowering), AlphaLowering(TM)
48 {}
Andrew Lenharthd97591a2005-10-20 00:29:02 +000049
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: {
Andrew Lenharthf88471d2005-12-06 20:43:30 +0000158 if (N->getOperand(1).getOpcode() == ISD::SETCC &&
159 MVT::isFloatingPoint(N->getOperand(1).getOperand(0).getValueType())) {
160 SDOperand Chain = Select(N->getOperand(0));
161 SDOperand CC1 = Select(N->getOperand(1).getOperand(0));
162 SDOperand CC2 = Select(N->getOperand(1).getOperand(1));
163 ISD::CondCode cCode= cast<CondCodeSDNode>(N->getOperand(1).getOperand(2))->get();
164
165 bool rev = false;
166 bool isNE = false;
167 unsigned Opc = Alpha::WTF;
168 switch(cCode) {
169 default: N->dump(); assert(0 && "Unknown FP comparison!");
170 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
171 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
172 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
173 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
174 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
175 case ISD::SETNE: Opc = Alpha::CMPTEQ; isNE = true; break;
176 };
177 SDOperand cmp = CurDAG->getTargetNode(Opc, MVT::f64,
178 rev?CC2:CC1,
179 rev?CC1:CC2);
180
181 MachineBasicBlock *Dest =
182 cast<BasicBlockSDNode>(N->getOperand(2))->getBasicBlock();
183 if(isNE)
184 return CurDAG->SelectNodeTo(N, Alpha::FBEQ, MVT::Other, cmp,
185 CurDAG->getBasicBlock(Dest), Chain);
186 else
187 return CurDAG->SelectNodeTo(N, Alpha::FBNE, MVT::Other, cmp,
188 CurDAG->getBasicBlock(Dest), Chain);
189 }
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000190 SDOperand Chain = Select(N->getOperand(0));
191 SDOperand CC = Select(N->getOperand(1));
Andrew Lenharth50b37842005-11-22 04:20:06 +0000192 MachineBasicBlock *Dest =
193 cast<BasicBlockSDNode>(N->getOperand(2))->getBasicBlock();
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000194 return CurDAG->SelectNodeTo(N, Alpha::BNE, MVT::Other, CC,
195 CurDAG->getBasicBlock(Dest), Chain);
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000196 }
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000197
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000198 case ISD::FrameIndex: {
Andrew Lenharth50b37842005-11-22 04:20:06 +0000199 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000200 return CurDAG->SelectNodeTo(N, Alpha::LDA, MVT::i64,
201 CurDAG->getTargetFrameIndex(FI, MVT::i32),
202 getI64Imm(0));
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000203 }
Andrew Lenharth4e629512005-12-24 05:36:33 +0000204 case AlphaISD::GlobalBaseReg:
205 return getGlobalBaseReg();
206
Andrew Lenharth53d89702005-12-25 01:34:27 +0000207 case AlphaISD::DivCall: {
208 SDOperand Chain = CurDAG->getEntryNode();
209 Chain = CurDAG->getCopyToReg(Chain, Alpha::R24, Select(Op.getOperand(1)),
210 SDOperand(0,0));
211 Chain = CurDAG->getCopyToReg(Chain, Alpha::R25, Select(Op.getOperand(2)),
212 Chain.getValue(1));
213 Chain = CurDAG->getCopyToReg(Chain, Alpha::R27, Select(Op.getOperand(0)),
214 Chain.getValue(1));
Andrew Lenhartheececba2005-12-25 17:36:48 +0000215 Chain = CurDAG->getTargetNode(Alpha::JSRs, MVT::Other, MVT::Flag,
Andrew Lenharth53d89702005-12-25 01:34:27 +0000216 Chain, Chain.getValue(1));
217 Chain = CurDAG->getCopyFromReg(Chain, Alpha::R27, MVT::i64,
218 Chain.getValue(1));
219 return CurDAG->SelectNodeTo(N, Alpha::BIS, MVT::i64, Chain, Chain);
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000220 }
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000221
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000222 case ISD::RET: {
223 SDOperand Chain = Select(N->getOperand(0)); // Token chain.
Andrew Lenharth93526222005-12-01 01:53:10 +0000224 SDOperand InFlag;
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000225
226 if (N->getNumOperands() == 2) {
227 SDOperand Val = Select(N->getOperand(1));
228 if (N->getOperand(1).getValueType() == MVT::i64) {
Andrew Lenharth93526222005-12-01 01:53:10 +0000229 Chain = CurDAG->getCopyToReg(Chain, Alpha::R0, Val, InFlag);
230 InFlag = Chain.getValue(1);
Andrew Lenharthe41419f2005-12-11 03:54:31 +0000231 } else if (N->getOperand(1).getValueType() == MVT::f64 ||
232 N->getOperand(1).getValueType() == MVT::f32) {
233 Chain = CurDAG->getCopyToReg(Chain, Alpha::F0, Val, InFlag);
234 InFlag = Chain.getValue(1);
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000235 }
236 }
Andrew Lenharth93526222005-12-01 01:53:10 +0000237 Chain = CurDAG->getCopyToReg(Chain, Alpha::R26, getRASaveReg(), InFlag);
238 InFlag = Chain.getValue(1);
239
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000240 // Finally, select this to a ret instruction.
Andrew Lenharth93526222005-12-01 01:53:10 +0000241 return CurDAG->SelectNodeTo(N, Alpha::RETDAG, MVT::Other, Chain, InFlag);
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000242 }
Andrew Lenharth50b37842005-11-22 04:20:06 +0000243 case ISD::Constant: {
Andrew Lenharthdcbaf8a2005-12-30 02:30:02 +0000244 uint64_t uval = cast<ConstantSDNode>(N)->getValue();
245 int64_t val = (int64_t)uval;
246 int32_t val32 = (int32_t)val;
247 if (val <= IMM_HIGH + IMM_HIGH * IMM_MULT &&
248 val >= IMM_LOW + IMM_LOW * IMM_MULT)
249 break; //(LDAH (LDA))
250 if ((uval >> 32) == 0 && //empty upper bits
251 val32 <= IMM_HIGH + IMM_HIGH * IMM_MULT &&
252 val32 >= IMM_LOW + IMM_LOW * IMM_MULT)
253 break; //(zext (LDAH (LDA)))
254 //Else use the constant pool
255 MachineConstantPool *CP = BB->getParent()->getConstantPool();
256 ConstantUInt *C =
257 ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , uval);
258 SDOperand Tmp, CPI = CurDAG->getTargetConstantPool(C, MVT::i64);
259 Tmp = CurDAG->getTargetNode(Alpha::LDAHr, MVT::i64, CPI, getGlobalBaseReg());
260 return CurDAG->SelectNodeTo(N, Alpha::LDQr, MVT::i64, MVT::Other,
261 CPI, Tmp, CurDAG->getEntryNode());
Andrew Lenharth50b37842005-11-22 04:20:06 +0000262 }
263 case ISD::ConstantFP:
264 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
265 bool isDouble = N->getValueType(0) == MVT::f64;
266 MVT::ValueType T = isDouble ? MVT::f64 : MVT::f32;
267 if (CN->isExactlyValue(+0.0)) {
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000268 return CurDAG->SelectNodeTo(N, isDouble ? Alpha::CPYST : Alpha::CPYSS,
269 T, CurDAG->getRegister(Alpha::F31, T),
270 CurDAG->getRegister(Alpha::F31, T));
Andrew Lenharth50b37842005-11-22 04:20:06 +0000271 } else if ( CN->isExactlyValue(-0.0)) {
Chris Lattnerd5acfb42005-11-30 23:04:38 +0000272 return CurDAG->SelectNodeTo(N, isDouble ? Alpha::CPYSNT : Alpha::CPYSNS,
273 T, CurDAG->getRegister(Alpha::F31, T),
274 CurDAG->getRegister(Alpha::F31, T));
Andrew Lenharth50b37842005-11-22 04:20:06 +0000275 } else {
276 abort();
277 }
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000278 break;
Andrew Lenharth50b37842005-11-22 04:20:06 +0000279 }
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000280
281 case ISD::SETCC:
282 if (MVT::isFloatingPoint(N->getOperand(0).Val->getValueType(0))) {
283 unsigned Opc = Alpha::WTF;
284 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
285 bool rev = false;
Andrew Lenharthb2156f92005-11-30 17:11:20 +0000286 bool isNE = false;
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000287 switch(CC) {
288 default: N->dump(); assert(0 && "Unknown FP comparison!");
289 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
290 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
291 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
292 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
293 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
Andrew Lenharthb2156f92005-11-30 17:11:20 +0000294 case ISD::SETNE: Opc = Alpha::CMPTEQ; isNE = true; break;
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000295 };
296 SDOperand tmp1 = Select(N->getOperand(0)),
297 tmp2 = Select(N->getOperand(1));
298 SDOperand cmp = CurDAG->getTargetNode(Opc, MVT::f64,
299 rev?tmp2:tmp1,
300 rev?tmp1:tmp2);
Andrew Lenharthb2156f92005-11-30 17:11:20 +0000301 if (isNE)
302 cmp = CurDAG->getTargetNode(Alpha::CMPTEQ, MVT::f64, cmp,
303 CurDAG->getRegister(Alpha::F31, MVT::f64));
304
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000305 SDOperand LD;
306 if (AlphaLowering.hasITOF()) {
307 LD = CurDAG->getNode(AlphaISD::FTOIT_, MVT::i64, cmp);
308 } else {
309 int FrameIdx =
310 CurDAG->getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
311 SDOperand FI = CurDAG->getFrameIndex(FrameIdx, MVT::i64);
312 SDOperand ST = CurDAG->getTargetNode(Alpha::STT, MVT::Other,
313 cmp, FI, CurDAG->getRegister(Alpha::R31, MVT::i64));
314 LD = CurDAG->getTargetNode(Alpha::LDQ, MVT::i64, FI,
315 CurDAG->getRegister(Alpha::R31, MVT::i64),
316 ST);
317 }
318 SDOperand FP = CurDAG->getTargetNode(Alpha::CMPULT, MVT::i64,
319 CurDAG->getRegister(Alpha::R31, MVT::i64),
320 LD);
321 return FP;
322 }
323 break;
Andrew Lenharthcd804962005-11-30 16:10:29 +0000324
Andrew Lenharth361f45a2005-12-12 17:43:52 +0000325 case ISD::SELECT:
326 if (MVT::isFloatingPoint(N->getValueType(0)) &&
327 (N->getOperand(0).getOpcode() != ISD::SETCC ||
328 !MVT::isFloatingPoint(N->getOperand(0).getOperand(1).getValueType()))) {
329 //This should be the condition not covered by the Patterns
330 //FIXME: Don't have SelectCode die, but rather return something testable
331 // so that things like this can be caught in fall though code
332 //move int to fp
333 bool isDouble = N->getValueType(0) == MVT::f64;
334 SDOperand LD,
335 cond = Select(N->getOperand(0)),
336 TV = Select(N->getOperand(1)),
337 FV = Select(N->getOperand(2));
338
339 if (AlphaLowering.hasITOF()) {
340 LD = CurDAG->getNode(AlphaISD::ITOFT_, MVT::f64, cond);
341 } else {
342 int FrameIdx =
343 CurDAG->getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
344 SDOperand FI = CurDAG->getFrameIndex(FrameIdx, MVT::i64);
345 SDOperand ST = CurDAG->getTargetNode(Alpha::STQ, MVT::Other,
346 cond, FI, CurDAG->getRegister(Alpha::R31, MVT::i64));
347 LD = CurDAG->getTargetNode(Alpha::LDT, MVT::f64, FI,
348 CurDAG->getRegister(Alpha::R31, MVT::i64),
349 ST);
350 }
Andrew Lenharth110f2242005-12-12 20:30:09 +0000351 SDOperand FP = CurDAG->getTargetNode(isDouble?Alpha::FCMOVNET:Alpha::FCMOVNES,
Andrew Lenharth361f45a2005-12-12 17:43:52 +0000352 MVT::f64, FV, TV, LD);
353 return FP;
354 }
355 break;
356
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000357 }
Andrew Lenharthcd804962005-11-30 16:10:29 +0000358
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000359 return SelectCode(Op);
360}
361
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000362SDOperand AlphaDAGToDAGISel::SelectCALL(SDOperand Op) {
Andrew Lenharth50b37842005-11-22 04:20:06 +0000363 //TODO: add flag stuff to prevent nondeturministic breakage!
364
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000365 SDNode *N = Op.Val;
366 SDOperand Chain = Select(N->getOperand(0));
Andrew Lenhartheececba2005-12-25 17:36:48 +0000367 SDOperand Addr = N->getOperand(1);
Andrew Lenharth93526222005-12-01 01:53:10 +0000368 SDOperand InFlag; // Null incoming flag value.
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000369
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000370 std::vector<SDOperand> CallOperands;
371 std::vector<MVT::ValueType> TypeOperands;
372
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000373 //grab the arguments
374 for(int i = 2, e = N->getNumOperands(); i < e; ++i) {
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000375 TypeOperands.push_back(N->getOperand(i).getValueType());
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000376 CallOperands.push_back(Select(N->getOperand(i)));
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000377 }
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000378 int count = N->getNumOperands() - 2;
379
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000380 static const unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
381 Alpha::R19, Alpha::R20, Alpha::R21};
382 static const unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
383 Alpha::F19, Alpha::F20, Alpha::F21};
384
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000385 for (int i = 6; i < count; ++i) {
386 unsigned Opc = Alpha::WTF;
387 if (MVT::isInteger(TypeOperands[i])) {
388 Opc = Alpha::STQ;
389 } else if (TypeOperands[i] == MVT::f32) {
390 Opc = Alpha::STS;
391 } else if (TypeOperands[i] == MVT::f64) {
392 Opc = Alpha::STT;
393 } else
394 assert(0 && "Unknown operand");
395 Chain = CurDAG->getTargetNode(Opc, MVT::Other, CallOperands[i],
396 getI64Imm((i - 6) * 8),
Andrew Lenharth93526222005-12-01 01:53:10 +0000397 CurDAG->getCopyFromReg(Chain, Alpha::R30, MVT::i64),
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000398 Chain);
399 }
Andrew Lenharth93526222005-12-01 01:53:10 +0000400 for (int i = 0; i < std::min(6, count); ++i) {
401 if (MVT::isInteger(TypeOperands[i])) {
402 Chain = CurDAG->getCopyToReg(Chain, args_int[i], CallOperands[i], InFlag);
403 InFlag = Chain.getValue(1);
404 } else if (TypeOperands[i] == MVT::f32 || TypeOperands[i] == MVT::f64) {
405 Chain = CurDAG->getCopyToReg(Chain, args_float[i], CallOperands[i], InFlag);
406 InFlag = Chain.getValue(1);
407 } else
408 assert(0 && "Unknown operand");
409 }
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000410
Andrew Lenharth93526222005-12-01 01:53:10 +0000411
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000412 // Finally, once everything is in registers to pass to the call, emit the
413 // call itself.
Andrew Lenhartheececba2005-12-25 17:36:48 +0000414 if (Addr.getOpcode() == AlphaISD::GPRelLo) {
415 SDOperand GOT = getGlobalBaseReg();
416 Chain = CurDAG->getCopyToReg(Chain, Alpha::R29, GOT, InFlag);
417 InFlag = Chain.getValue(1);
418 Chain = CurDAG->getTargetNode(Alpha::BSR, MVT::Other, MVT::Flag,
419 Addr.getOperand(0), Chain, InFlag);
420 } else {
421 Chain = CurDAG->getCopyToReg(Chain, Alpha::R27, Select(Addr), InFlag);
422 InFlag = Chain.getValue(1);
423 Chain = CurDAG->getTargetNode(Alpha::JSR, MVT::Other, MVT::Flag,
424 Chain, InFlag );
425 }
Andrew Lenharth93526222005-12-01 01:53:10 +0000426 InFlag = Chain.getValue(1);
427
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000428 std::vector<SDOperand> CallResults;
429
430 switch (N->getValueType(0)) {
431 default: assert(0 && "Unexpected ret value!");
432 case MVT::Other: break;
433 case MVT::i64:
Andrew Lenharth93526222005-12-01 01:53:10 +0000434 Chain = CurDAG->getCopyFromReg(Chain, Alpha::R0, MVT::i64, InFlag).getValue(1);
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000435 CallResults.push_back(Chain.getValue(0));
436 break;
Andrew Lenharth50b37842005-11-22 04:20:06 +0000437 case MVT::f32:
Andrew Lenharth93526222005-12-01 01:53:10 +0000438 Chain = CurDAG->getCopyFromReg(Chain, Alpha::F0, MVT::f32, InFlag).getValue(1);
Andrew Lenharth50b37842005-11-22 04:20:06 +0000439 CallResults.push_back(Chain.getValue(0));
440 break;
441 case MVT::f64:
Andrew Lenharth93526222005-12-01 01:53:10 +0000442 Chain = CurDAG->getCopyFromReg(Chain, Alpha::F0, MVT::f64, InFlag).getValue(1);
Andrew Lenharth50b37842005-11-22 04:20:06 +0000443 CallResults.push_back(Chain.getValue(0));
444 break;
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000445 }
446
447 CallResults.push_back(Chain);
448 for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
449 CodeGenMap[Op.getValue(i)] = CallResults[i];
450 return CallResults[Op.ResNo];
451}
452
453
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000454/// createAlphaISelDag - This pass converts a legalized DAG into a
455/// Alpha-specific DAG, ready for instruction scheduling.
456///
457FunctionPass *llvm::createAlphaISelDag(TargetMachine &TM) {
458 return new AlphaDAGToDAGISel(TM);
459}