blob: 2891e7bb1dfcb5a7ec244a65c138da3336f08373 [file] [log] [blame]
Misha Brukman2a8350a2005-02-05 02:24:26 +00001//===- AlphaISelPattern.cpp - A pattern matching inst selector for Alpha --===//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group 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//
12//===----------------------------------------------------------------------===//
13
14#include "Alpha.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000015#include "AlphaRegisterInfo.h"
16#include "llvm/Constants.h" // FIXME: REMOVE
17#include "llvm/Function.h"
18#include "llvm/CodeGen/MachineInstrBuilder.h"
19#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/CodeGen/SSARegMap.h"
25#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetLowering.h"
27#include "llvm/Support/MathExtras.h"
28#include "llvm/ADT/Statistic.h"
29#include <set>
Andrew Lenharth684f2292005-01-30 00:35:27 +000030#include <algorithm>
Andrew Lenharth304d0f32005-01-22 23:41:55 +000031using namespace llvm;
32
33//===----------------------------------------------------------------------===//
34// AlphaTargetLowering - Alpha Implementation of the TargetLowering interface
35namespace {
36 class AlphaTargetLowering : public TargetLowering {
37 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
38 unsigned GP; //GOT vreg
39 public:
40 AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
41 // Set up the TargetLowering object.
Andrew Lenharth3d65d312005-01-27 03:49:45 +000042 //I am having problems with shr n ubyte 1
Andrew Lenharth879ef222005-02-02 17:00:21 +000043 setShiftAmountType(MVT::i64);
44 setSetCCResultType(MVT::i64);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000045
Andrew Lenharth304d0f32005-01-22 23:41:55 +000046 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
47 addRegisterClass(MVT::f64, Alpha::FPRCRegisterClass);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000048 addRegisterClass(MVT::f32, Alpha::FPRCRegisterClass);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000049
Andrew Lenharthd2bb9602005-01-27 07:50:35 +000050 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000051
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +000052 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000053 setOperationAction(ISD::ZEXTLOAD , MVT::i32 , Expand);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000054
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +000055 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000056 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
57 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
58
Andrew Lenharth3d65d312005-01-27 03:49:45 +000059 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); //what is the sign expansion of 1? 1 or -1?
Andrew Lenharth02981182005-01-26 01:24:38 +000060
Andrew Lenharth9818c052005-02-05 13:19:12 +000061 setOperationAction(ISD::SREM , MVT::f32 , Expand);
62 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +000063
Andrew Lenharth8d163d22005-02-02 05:49:42 +000064 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
Andrew Lenharth9818c052005-02-05 13:19:12 +000065 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
66 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
67
Andrew Lenharth3d65d312005-01-27 03:49:45 +000068 computeRegisterProperties();
Andrew Lenharth304d0f32005-01-22 23:41:55 +000069
Andrew Lenharthd2bb9602005-01-27 07:50:35 +000070 addLegalFPImmediate(+0.0); //F31
Andrew Lenharth12dd2622005-02-03 21:01:15 +000071 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +000072 }
73
74 /// LowerArguments - This hook must be implemented to indicate how we should
75 /// lower the arguments for the specified function, into the specified DAG.
76 virtual std::vector<SDOperand>
77 LowerArguments(Function &F, SelectionDAG &DAG);
78
79 /// LowerCallTo - This hook lowers an abstract call to a function into an
80 /// actual call.
81 virtual std::pair<SDOperand, SDOperand>
82 LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
83 ArgListTy &Args, SelectionDAG &DAG);
84
85 virtual std::pair<SDOperand, SDOperand>
86 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
87
88 virtual std::pair<SDOperand,SDOperand>
89 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
90 const Type *ArgTy, SelectionDAG &DAG);
91
92 virtual std::pair<SDOperand, SDOperand>
93 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
94 SelectionDAG &DAG);
95
96 void restoreGP(MachineBasicBlock* BB)
97 {
98 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
99 }
100 };
101}
102
103//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
104
105//For now, just use variable size stack frame format
106
107//In a standard call, the first six items are passed in registers $16
108//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
109//of argument-to-register correspondence.) The remaining items are
110//collected in a memory argument list that is a naturally aligned
111//array of quadwords. In a standard call, this list, if present, must
112//be passed at 0(SP).
113//7 ... n 0(SP) ... (n-7)*8(SP)
114
115std::vector<SDOperand>
116AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
117{
118 std::vector<SDOperand> ArgValues;
119
120 // //#define FP $15
121 // //#define RA $26
122 // //#define PV $27
123 // //#define GP $29
124 // //#define SP $30
125
126 // assert(0 && "TODO");
127 MachineFunction &MF = DAG.getMachineFunction();
Andrew Lenharth05380342005-02-07 05:07:00 +0000128 MachineFrameInfo*MFI = MF.getFrameInfo();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000129
130 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
131 MachineBasicBlock& BB = MF.front();
132
133 //Handle the return address
134 //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
135
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000136 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
137 Alpha::R19, Alpha::R20, Alpha::R21};
138 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
139 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000140 unsigned argVreg[6];
141 unsigned argPreg[6];
142 unsigned argOpc[6];
143
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000144 int count = 0;
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000145
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000146 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I)
147 {
Andrew Lenharth40831c52005-01-28 06:57:18 +0000148 SDOperand newroot, argt;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000149 if (count < 6) {
150 switch (getValueType(I->getType())) {
151 default: std::cerr << "Unknown Type " << getValueType(I->getType()) << "\n"; abort();
152 case MVT::f64:
153 case MVT::f32:
154 BuildMI(&BB, Alpha::IDEF, 0, args_float[count]);
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000155 argVreg[count] = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(getValueType(I->getType())));
156 argPreg[count] = args_float[count];
157 argOpc[count] = Alpha::CPYS;
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000158 argt = newroot = DAG.getCopyFromReg(argVreg[count], getValueType(I->getType()), DAG.getRoot());
Andrew Lenharth684f2292005-01-30 00:35:27 +0000159 break;
160 case MVT::i1:
161 case MVT::i8:
162 case MVT::i16:
163 case MVT::i32:
164 case MVT::i64:
165 BuildMI(&BB, Alpha::IDEF, 0, args_int[count]);
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000166 argVreg[count] =MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
167 argPreg[count] = args_int[count];
168 argOpc[count] = Alpha::BIS;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000169 argt = newroot = DAG.getCopyFromReg(argVreg[count], MVT::i64, DAG.getRoot());
170 if (getValueType(I->getType()) != MVT::i64)
171 argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()), newroot);
172 break;
173 }
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000174 ++count;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000175 } else { //more args
176 // Create the frame index object for this incoming parameter...
177 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
178
179 // Create the SelectionDAG nodes corresponding to a load from this parameter
180 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
181 argt = newroot = DAG.getLoad(getValueType(I->getType()), DAG.getEntryNode(), FIN);
Andrew Lenharth40831c52005-01-28 06:57:18 +0000182 }
183 DAG.setRoot(newroot.getValue(1));
184 ArgValues.push_back(argt);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000185 }
186
187 BuildMI(&BB, Alpha::IDEF, 0, Alpha::R29);
188 BuildMI(&BB, Alpha::BIS, 2, GP).addReg(Alpha::R29).addReg(Alpha::R29);
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000189 for (int i = 0; i < count; ++i)
190 {
191 if (argPreg[i] == Alpha::F16 || argPreg[i] == Alpha::F17 || argPreg[i] == Alpha::F18 ||
192 argPreg[i] == Alpha::F19 || argPreg[i] == Alpha::F20 || argPreg[i] == Alpha::F21)
193 {
194 assert(argOpc[i] == Alpha::CPYS && "Using BIS for a float??");
195 }
196 BuildMI(&BB, argOpc[i], 2, argVreg[i]).addReg(argPreg[i]).addReg(argPreg[i]);
197 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000198
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000199 return ArgValues;
200}
201
202std::pair<SDOperand, SDOperand>
203AlphaTargetLowering::LowerCallTo(SDOperand Chain,
204 const Type *RetTy, SDOperand Callee,
205 ArgListTy &Args, SelectionDAG &DAG) {
206 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000207 if (Args.size() > 6)
208 NumBytes = (Args.size() - 6) * 8;
209
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000210 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
211 DAG.getConstant(NumBytes, getPointerTy()));
212 std::vector<SDOperand> args_to_use;
213 for (unsigned i = 0, e = Args.size(); i != e; ++i)
214 {
215 switch (getValueType(Args[i].second)) {
216 default: assert(0 && "Unexpected ValueType for argument!");
217 case MVT::i1:
218 case MVT::i8:
219 case MVT::i16:
220 case MVT::i32:
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000221 // Promote the integer to 64 bits. If the input type is signed use a
222 // sign extend, otherwise use a zero extend.
223 if (Args[i].second->isSigned())
Andrew Lenharth40831c52005-01-28 06:57:18 +0000224 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000225 else
Andrew Lenharth40831c52005-01-28 06:57:18 +0000226 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000227 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000228 case MVT::i64:
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000229 case MVT::f64:
230 case MVT::f32:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000231 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000232 }
233 args_to_use.push_back(Args[i].first);
234 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000235
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000236 std::vector<MVT::ValueType> RetVals;
237 MVT::ValueType RetTyVT = getValueType(RetTy);
238 if (RetTyVT != MVT::isVoid)
239 RetVals.push_back(RetTyVT);
240 RetVals.push_back(MVT::Other);
241
242 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee, args_to_use), 0);
243 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
244 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
245 DAG.getConstant(NumBytes, getPointerTy()));
246 return std::make_pair(TheCall, Chain);
247}
248
249std::pair<SDOperand, SDOperand>
250AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
251 //vastart just returns the address of the VarArgsFrameIndex slot.
252 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
253}
254
255std::pair<SDOperand,SDOperand> AlphaTargetLowering::
256LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
257 const Type *ArgTy, SelectionDAG &DAG) {
258 abort();
259}
260
261
262std::pair<SDOperand, SDOperand> AlphaTargetLowering::
263LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
264 SelectionDAG &DAG) {
265 abort();
266}
267
268
269
270
271
272namespace {
273
274 //===--------------------------------------------------------------------===//
275 /// ISel - Alpha specific code to select Alpha machine instructions for
276 /// SelectionDAG operations.
277 ///
278 class ISel : public SelectionDAGISel {
279
280 /// AlphaLowering - This object fully describes how to lower LLVM code to an
281 /// Alpha-specific SelectionDAG.
282 AlphaTargetLowering AlphaLowering;
283
284
285 /// ExprMap - As shared expressions are codegen'd, we keep track of which
286 /// vreg the value is produced in, so we only emit one copy of each compiled
287 /// tree.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000288 static const unsigned notIn = (unsigned)(-1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000289 std::map<SDOperand, unsigned> ExprMap;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000290
291 public:
292 ISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM) {
293 }
294
295 /// InstructionSelectBasicBlock - This callback is invoked by
296 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
297 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
298 // Codegen the basic block.
299 Select(DAG.getRoot());
300
301 // Clear state used for selection.
302 ExprMap.clear();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000303 }
304
305 unsigned SelectExpr(SDOperand N);
Andrew Lenharth40831c52005-01-28 06:57:18 +0000306 unsigned SelectExprFP(SDOperand N, unsigned Result);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000307 void Select(SDOperand N);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000308
309 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000310 };
311}
312
Andrew Lenharth65838902005-02-06 16:22:15 +0000313static unsigned GetSymVersion(unsigned opcode)
314{
315 switch (opcode) {
316 default: assert(0 && "unknown load or store"); return 0;
317 case Alpha::LDQ: return Alpha::LDQ_SYM;
318 case Alpha::LDS: return Alpha::LDS_SYM;
319 case Alpha::LDT: return Alpha::LDT_SYM;
320 case Alpha::LDL: return Alpha::LDL_SYM;
321 case Alpha::LDBU: return Alpha::LDBU_SYM;
322 case Alpha::LDWU: return Alpha::LDWU_SYM;
323 case Alpha::LDW: return Alpha::LDW_SYM;
324 case Alpha::LDB: return Alpha::LDB_SYM;
325 case Alpha::STQ: return Alpha::STQ_SYM;
326 case Alpha::STS: return Alpha::STS_SYM;
327 case Alpha::STT: return Alpha::STT_SYM;
328 case Alpha::STL: return Alpha::STL_SYM;
329 case Alpha::STW: return Alpha::STW_SYM;
330 case Alpha::STB: return Alpha::STB_SYM;
331 }
332}
333
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000334//Check to see if the load is a constant offset from a base register
335void ISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
336{
337 unsigned opcode = N.getOpcode();
338 if (opcode == ISD::ADD) {
339 if(N.getOperand(1).getOpcode() == ISD::Constant && cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
340 { //Normal imm add
341 Reg = SelectExpr(N.getOperand(0));
342 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
343 return;
344 }
345 else if(N.getOperand(0).getOpcode() == ISD::Constant && cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 32767)
346 {
347 Reg = SelectExpr(N.getOperand(1));
348 offset = cast<ConstantSDNode>(N.getOperand(0))->getValue();
349 return;
350 }
351 }
352 Reg = SelectExpr(N);
353 offset = 0;
354 return;
355}
356
Andrew Lenharth40831c52005-01-28 06:57:18 +0000357unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
358{
359 unsigned Tmp1, Tmp2, Tmp3;
360 unsigned Opc = 0;
361 SDNode *Node = N.Val;
362 MVT::ValueType DestType = N.getValueType();
363 unsigned opcode = N.getOpcode();
364
365 switch (opcode) {
366 default:
367 Node->dump();
368 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +0000369
Andrew Lenharth9818c052005-02-05 13:19:12 +0000370 case ISD::SELECT:
371 {
372 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
373 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
374 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000375
376
377 // Spill the cond to memory and reload it from there.
378 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
379 MachineFunction *F = BB->getParent();
380 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
381 unsigned Tmp4 = MakeReg(MVT::f64);
382 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addFrameIndex(FrameIdx).addReg(Alpha::F31);
383 BuildMI(BB, Alpha::LDT, 2, Tmp4).addFrameIndex(FrameIdx).addReg(Alpha::F31);
384 //now ideally, we don't have to do anything to the flag...
Andrew Lenharth9818c052005-02-05 13:19:12 +0000385 // Get the condition into the zero flag.
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000386 BuildMI(BB, Alpha::FCMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp4);
Andrew Lenharth9818c052005-02-05 13:19:12 +0000387 return Result;
388 }
389
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000390 case ISD::FP_ROUND:
391 assert (DestType == MVT::f32 && N.getOperand(0).getValueType() == MVT::f64 && "only f64 to f32 conversion supported here");
392 Tmp1 = SelectExpr(N.getOperand(0));
393 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
394 return Result;
395
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000396 case ISD::FP_EXTEND:
397 assert (DestType == MVT::f64 && N.getOperand(0).getValueType() == MVT::f32 && "only f32 to f64 conversion supported here");
398 Tmp1 = SelectExpr(N.getOperand(0));
399 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
400 return Result;
401
Andrew Lenharth2c594352005-01-29 15:42:07 +0000402 case ISD::CopyFromReg:
403 {
404 // Make sure we generate both values.
405 if (Result != notIn)
406 ExprMap[N.getValue(1)] = notIn; // Generate the token
407 else
408 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
409
410 SDOperand Chain = N.getOperand(0);
411
412 Select(Chain);
413 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
414 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
415 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
416 return Result;
417 }
418
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000419 case ISD::LOAD:
420 {
421 // Make sure we generate both values.
422 if (Result != notIn)
423 ExprMap[N.getValue(1)] = notIn; // Generate the token
424 else
425 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000426
427 //DestType = N.getValue(0).getValueType();
428
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000429 SDOperand Chain = N.getOperand(0);
430 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000431 Select(Chain);
Andrew Lenharth65838902005-02-06 16:22:15 +0000432 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
433
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000434 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000435 {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000436 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000437 Opc = GetSymVersion(Opc);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000438 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
439 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000440 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000441 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000442 Opc = GetSymVersion(Opc);
Andrew Lenharth97127a12005-02-05 17:41:39 +0000443 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000444 }
Andrew Lenharth05380342005-02-07 05:07:00 +0000445 else if(Address.getOpcode() == ISD::FrameIndex)
446 {
447 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
448 BuildMI(BB, Opc, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
449 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000450 else
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000451 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000452 long offset;
453 SelectAddr(Address, Tmp1, offset);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000454 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000455 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000456 return Result;
457 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000458 case ISD::ConstantFP:
459 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
460 if (CN->isExactlyValue(+0.0)) {
461 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000462 } else if ( CN->isExactlyValue(-0.0)) {
463 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +0000464 } else {
465 abort();
466 }
467 }
468 return Result;
469
470 case ISD::MUL:
471 case ISD::ADD:
472 case ISD::SUB:
473 case ISD::SDIV:
474 switch( opcode ) {
475 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
476 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
477 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
478 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
479 };
480 Tmp1 = SelectExpr(N.getOperand(0));
481 Tmp2 = SelectExpr(N.getOperand(1));
482 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
483 return Result;
484
Andrew Lenharth2c594352005-01-29 15:42:07 +0000485 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000486 {
487 //include a conversion sequence for float loads to double
488 if (Result != notIn)
489 ExprMap[N.getValue(1)] = notIn; // Generate the token
490 else
491 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
492
493 Tmp2 = MakeReg(MVT::f32);
494
495 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 && "EXTLOAD not from f32");
496 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
497
498 SDOperand Chain = N.getOperand(0);
499 SDOperand Address = N.getOperand(1);
500 Select(Chain);
501
502 if (Address.getOpcode() == ISD::GlobalAddress)
503 {
504 AlphaLowering.restoreGP(BB);
505 BuildMI(BB, Alpha::LDS_SYM, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
506 }
507 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
508 {
509 AlphaLowering.restoreGP(BB);
510 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp2).addConstantPoolIndex(CP->getIndex());
511 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp2);
512 }
Andrew Lenharth05380342005-02-07 05:07:00 +0000513 else if(Address.getOpcode() == ISD::FrameIndex)
514 {
515 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
516 BuildMI(BB, Opc, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
517 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000518 else
519 {
520 long offset;
521 SelectAddr(Address, Tmp1, offset);
522 BuildMI(BB, Alpha::LDS, 1, Tmp2).addImm(offset).addReg(Tmp1);
523 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp2);
524 }
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000525 return Result;
526 }
Andrew Lenharth2c594352005-01-29 15:42:07 +0000527
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000528 case ISD::UINT_TO_FP:
529 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000530 {
531 assert (N.getOperand(0).getValueType() == MVT::i64 && "only quads can be loaded from");
532 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +0000533 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000534
535 //The hard way:
536 // Spill the integer to memory and reload it from there.
537 unsigned Size = MVT::getSizeInBits(MVT::i64)/8;
538 MachineFunction *F = BB->getParent();
539 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
540
Andrew Lenharth7efadce2005-01-31 01:44:26 +0000541 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addFrameIndex(FrameIdx).addReg(Alpha::F31);
542 BuildMI(BB, Alpha::LDT, 2, Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
543 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
544 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000545
546 //The easy way: doesn't work
547// //so these instructions are not supported on ev56
548// Opc = DestType == MVT::f64 ? Alpha::ITOFT : Alpha::ITOFS;
549// BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
550// Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
551// BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
552
Andrew Lenharth40831c52005-01-28 06:57:18 +0000553 return Result;
554 }
555 }
556 assert(0 && "should not get here");
557 return 0;
558}
559
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000560unsigned ISel::SelectExpr(SDOperand N) {
561 unsigned Result;
562 unsigned Tmp1, Tmp2, Tmp3;
563 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000564 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000565
566 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000567 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000568
569 unsigned &Reg = ExprMap[N];
570 if (Reg) return Reg;
571
572 if (N.getOpcode() != ISD::CALL)
573 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000574 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000575 else {
576 // If this is a call instruction, make sure to prepare ALL of the result
577 // values as well as the chain.
578 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000579 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000580 else {
581 Result = MakeReg(Node->getValueType(0));
582 ExprMap[N.getValue(0)] = Result;
583 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
584 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000585 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000586 }
587 }
588
Andrew Lenharth22088bb2005-02-02 15:05:33 +0000589 if (DestType == MVT::f64 || DestType == MVT::f32 ||
590 (opcode == ISD::LOAD &&
591 (N.getValue(0).getValueType() == MVT::f32 || N.getValue(0).getValueType() == MVT::f64)))
Andrew Lenharth40831c52005-01-28 06:57:18 +0000592 return SelectExprFP(N, Result);
593
594 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000595 default:
596 Node->dump();
597 assert(0 && "Node not handled!\n");
598
Andrew Lenharth2c594352005-01-29 15:42:07 +0000599 case ISD::ConstantPool:
600 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
601 AlphaLowering.restoreGP(BB);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000602 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000603 return Result;
604
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000605 case ISD::FrameIndex:
606 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Andrew Lenharth684f2292005-01-30 00:35:27 +0000607 BuildMI(BB, Alpha::LDA, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000608 return Result;
609
610 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000611 {
612 // Make sure we generate both values.
613 if (Result != notIn)
614 ExprMap[N.getValue(1)] = notIn; // Generate the token
615 else
616 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
617
618 SDOperand Chain = N.getOperand(0);
619 SDOperand Address = N.getOperand(1);
620 Select(Chain);
621
622 switch(Node->getValueType(0)) {
623 default: Node->dump(); assert(0 && "Unknown type to sign extend to.");
Andrew Lenharthd279b412005-01-25 19:58:40 +0000624 case MVT::i64:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000625 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
626 default:
627 Node->dump();
628 assert(0 && "Bad extend load!");
629 case MVT::i64: Opc = Alpha::LDQ; break;
630 case MVT::i32: Opc = Alpha::LDL; break;
631 case MVT::i16: Opc = Alpha::LDWU; break;
632 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
633 case MVT::i8: Opc = Alpha::LDBU; break;
634 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000635 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000636
637 if (Address.getOpcode() == ISD::GlobalAddress)
638 {
639 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000640 Opc = GetSymVersion(Opc);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000641 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
642 }
643 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address))
644 {
645 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000646 Opc = GetSymVersion(Opc);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000647 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
648 }
Andrew Lenharth05380342005-02-07 05:07:00 +0000649 else if(Address.getOpcode() == ISD::FrameIndex)
650 {
651 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
652 BuildMI(BB, Opc, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
653 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000654 else
655 {
656 long offset;
657 SelectAddr(Address, Tmp1, offset);
658 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
659 }
660 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000661 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000662
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000663 case ISD::SEXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000664 {
665 // Make sure we generate both values.
666 if (Result != notIn)
667 ExprMap[N.getValue(1)] = notIn; // Generate the token
668 else
669 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000670
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000671 SDOperand Chain = N.getOperand(0);
672 SDOperand Address = N.getOperand(1);
673 Select(Chain);
674
675 switch(Node->getValueType(0)) {
676 default: Node->dump(); assert(0 && "Unknown type to sign extend to.");
677 case MVT::i64:
678 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
679 default: Node->dump(); assert(0 && "Bad sign extend!");
680 case MVT::i32: Opc = Alpha::LDL; break;
681 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000682 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000683
684 if (Address.getOpcode() == ISD::GlobalAddress)
685 {
686 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000687 Opc = GetSymVersion(Opc);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000688 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
689 }
690 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
691 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000692 Opc = GetSymVersion(Opc);
693 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000694 }
Andrew Lenharth05380342005-02-07 05:07:00 +0000695 else if(Address.getOpcode() == ISD::FrameIndex)
696 {
697 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
698 BuildMI(BB, Opc, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
699 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000700 else
701 {
702 long offset;
703 SelectAddr(Address, Tmp1, offset);
704 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
705 }
706 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000707 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000708
709 case ISD::ZEXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000710 {
711 // Make sure we generate both values.
712 if (Result != notIn)
713 ExprMap[N.getValue(1)] = notIn; // Generate the token
714 else
715 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
716
717 SDOperand Chain = N.getOperand(0);
718 SDOperand Address = N.getOperand(1);
719 Select(Chain);
720
721 switch(Node->getValueType(0)) {
722 default: Node->dump(); assert(0 && "Unknown type to zero extend to.");
723 case MVT::i64:
724 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
725 default: Node->dump(); assert(0 && "Bad sign extend!");
726 case MVT::i16: Opc = Alpha::LDWU; break;
727 case MVT::i8: Opc = Alpha::LDBU; break;
728 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000729 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000730
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000731 if (Address.getOpcode() == ISD::GlobalAddress)
732 {
733 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000734 Opc = GetSymVersion(Opc);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000735 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
736 }
737 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
738 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000739 Opc = GetSymVersion(Opc);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000740 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
741 }
Andrew Lenharth05380342005-02-07 05:07:00 +0000742 else if(Address.getOpcode() == ISD::FrameIndex)
743 {
744 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
745 BuildMI(BB, Opc, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
746 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000747 else
748 {
749 long offset;
750 SelectAddr(Address, Tmp1, offset);
751 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
752 }
753 return Result;
754 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000755
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000756 case ISD::GlobalAddress:
757 AlphaLowering.restoreGP(BB);
758 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
759 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
760 return Result;
761
762 case ISD::CALL:
763 {
764 Select(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000765
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000766 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000767 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000768
769 //grab the arguments
770 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000771 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000772 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000773 argvregs.push_back(SelectExpr(N.getOperand(i)));
774
Andrew Lenharth684f2292005-01-30 00:35:27 +0000775 //in reg args
776 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
777 {
778 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
779 Alpha::R19, Alpha::R20, Alpha::R21};
780 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
781 Alpha::F19, Alpha::F20, Alpha::F21};
782 switch(N.getOperand(i+2).getValueType()) {
783 default:
784 Node->dump();
785 N.getOperand(i).Val->dump();
786 std::cerr << "Type for " << i << " is: " << N.getOperand(i+2).getValueType() << "\n";
787 assert(0 && "Unknown value type for call");
788 case MVT::i1:
789 case MVT::i8:
790 case MVT::i16:
791 case MVT::i32:
792 case MVT::i64:
793 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
794 break;
795 case MVT::f32:
796 case MVT::f64:
797 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
798 break;
799 }
800 }
801 //in mem args
802 for (int i = 6, e = argvregs.size(); i < e; ++i)
803 {
804 switch(N.getOperand(i+2).getValueType()) {
805 default:
806 Node->dump();
807 N.getOperand(i).Val->dump();
808 std::cerr << "Type for " << i << " is: " << N.getOperand(i+2).getValueType() << "\n";
809 assert(0 && "Unknown value type for call");
810 case MVT::i1:
811 case MVT::i8:
812 case MVT::i16:
813 case MVT::i32:
814 case MVT::i64:
815 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
816 break;
817 case MVT::f32:
818 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
819 break;
820 case MVT::f64:
821 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
822 break;
823 }
824 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000825 //build the right kind of call
826 if (GlobalAddressSDNode *GASD =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000827 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
828 {
829 AlphaLowering.restoreGP(BB);
830 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal(),true);
831 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000832 else if (ExternalSymbolSDNode *ESSDN =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000833 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
834 {
835 AlphaLowering.restoreGP(BB);
836 BuildMI(BB, Alpha::CALL, 0).addExternalSymbol(ESSDN->getSymbol(), true);
837 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000838 else
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000839 {
840 //no need to restore GP as we are doing an indirect call
841 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000842 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
843 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000844 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000845
846 //push the result into a virtual register
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000847
848 switch (Node->getValueType(0)) {
849 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000850 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000851 case MVT::i1:
852 case MVT::i8:
853 case MVT::i16:
854 case MVT::i32:
855 case MVT::i64:
856 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
857 break;
858 case MVT::f32:
859 case MVT::f64:
860 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
861 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000862 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000863 return Result+N.ResNo;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000864 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000865
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000866 case ISD::SIGN_EXTEND:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000867 abort();
868
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000869 case ISD::SIGN_EXTEND_INREG:
870 {
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000871 //Alpha has instructions for a bunch of signed 32 bit stuff
872 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
873 {
874 switch (N.getOperand(0).getOpcode()) {
875 case ISD::ADD:
876 case ISD::SUB:
877 case ISD::MUL:
878 {
879 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
880 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
881 //FIXME: first check for Scaled Adds and Subs!
882 if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
883 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
884 { //Normal imm add/sub
885 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
886 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
887 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
888 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
889 }
890 else
891 { //Normal add/sub
892 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULLi : Alpha::SUBL);
893 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
894 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
895 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
896 }
897 return Result;
898 }
899 default: break; //Fall Though;
900 }
901 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000902 Tmp1 = SelectExpr(N.getOperand(0));
903 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000904 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000905 switch(MVN->getExtraValueType())
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000906 {
907 default:
908 Node->dump();
909 assert(0 && "Sign Extend InReg not there yet");
910 break;
911 case MVT::i32:
912 {
913 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
914 break;
915 }
916 case MVT::i16:
917 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
918 break;
919 case MVT::i8:
920 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
921 break;
922 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000923 return Result;
924 }
925 case ISD::ZERO_EXTEND_INREG:
926 {
927 Tmp1 = SelectExpr(N.getOperand(0));
928 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000929 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000930 switch(MVN->getExtraValueType())
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000931 {
932 default:
933 Node->dump();
934 assert(0 && "Zero Extend InReg not there yet");
935 break;
936 case MVT::i32: Tmp2 = 0xf0; break;
937 case MVT::i16: Tmp2 = 0xfc; break;
938 case MVT::i8: Tmp2 = 0xfe; break;
939 case MVT::i1: //handle this one special
940 BuildMI(BB, Alpha::ANDi, 2, Result).addReg(Tmp1).addImm(1);
941 return Result;
942 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000943 BuildMI(BB, Alpha::ZAPi, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000944 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000945 }
946
947 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000948 {
949 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
950 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
951 bool isConst1 = false;
952 bool isConst2 = false;
953 int dir;
Andrew Lenharth9818c052005-02-05 13:19:12 +0000954
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000955 //Tmp1 = SelectExpr(N.getOperand(0));
956 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000957 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
958 isConst1 = true;
959 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000960 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
961 isConst2 = true;
962
963 switch (SetCC->getCondition()) {
964 default: Node->dump(); assert(0 && "Unknown integer comparison!");
965 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
966 case ISD::SETLT: Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
967 case ISD::SETLE: Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
968 case ISD::SETGT: Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
969 case ISD::SETGE: Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
970 case ISD::SETULT: Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
971 case ISD::SETUGT: Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
972 case ISD::SETULE: Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
973 case ISD::SETUGE: Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000974 case ISD::SETNE: {//Handle this one special
975 //std::cerr << "Alpha does not have a setne.\n";
976 //abort();
977 Tmp1 = SelectExpr(N.getOperand(0));
978 Tmp2 = SelectExpr(N.getOperand(1));
979 Tmp3 = MakeReg(MVT::i64);
980 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
981 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000982 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000983 return Result;
984 }
985 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000986 if (dir == 1) {
987 Tmp1 = SelectExpr(N.getOperand(0));
988 if (isConst2) {
989 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
990 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
991 } else {
992 Tmp2 = SelectExpr(N.getOperand(1));
993 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
994 }
995 } else if (dir == 2) {
996 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +0000997 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000998 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
999 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1000 } else {
1001 Tmp2 = SelectExpr(N.getOperand(0));
1002 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1003 }
1004 } else { //dir == 0
1005 if (isConst1) {
1006 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1007 Tmp2 = SelectExpr(N.getOperand(1));
1008 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
1009 } else if (isConst2) {
1010 Tmp1 = SelectExpr(N.getOperand(0));
1011 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1012 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1013 } else {
1014 Tmp1 = SelectExpr(N.getOperand(0));
1015 Tmp2 = SelectExpr(N.getOperand(1));
1016 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1017 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001018 }
1019 } else {
1020 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
1021 bool rev = false;
1022 bool inv = false;
1023
1024 switch (SetCC->getCondition()) {
1025 default: Node->dump(); assert(0 && "Unknown FP comparison!");
1026 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
1027 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
1028 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
1029 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
1030 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
1031 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
1032 }
1033
1034 Tmp1 = SelectExpr(N.getOperand(0));
1035 Tmp2 = SelectExpr(N.getOperand(1));
1036 //Can only compare doubles, and dag won't promote for me
1037 if (SetCC->getOperand(0).getValueType() == MVT::f32)
1038 {
1039 Tmp3 = MakeReg(MVT::f64);
1040 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
1041 Tmp1 = Tmp3;
1042 }
1043 if (SetCC->getOperand(1).getValueType() == MVT::f32)
1044 {
1045 Tmp3 = MakeReg(MVT::f64);
1046 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
1047 Tmp1 = Tmp2;
1048 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001049
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001050 if (rev) std::swap(Tmp1, Tmp2);
1051 Tmp3 = MakeReg(MVT::f64);
1052 //do the comparison
1053 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1054
1055 //now arrange for Result (int) to have a 1 or 0
1056
1057 // Spill the FP to memory and reload it from there.
1058 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1059 MachineFunction *F = BB->getParent();
1060 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
1061 unsigned Tmp4 = MakeReg(MVT::f64);
1062 BuildMI(BB, Alpha::CVTTQ, 1, Tmp4).addReg(Tmp3);
1063 BuildMI(BB, Alpha::STT, 3).addReg(Tmp4).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1064 unsigned Tmp5 = MakeReg(MVT::i64);
1065 BuildMI(BB, Alpha::LDQ, 2, Tmp5).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharth9818c052005-02-05 13:19:12 +00001066
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001067 //now, set result based on Tmp5
1068 //Set Tmp6 if fp cmp was false
1069 unsigned Tmp6 = MakeReg(MVT::i64);
1070 BuildMI(BB, Alpha::CMPEQ, 2, Tmp6).addReg(Tmp5).addReg(Alpha::R31);
1071 //and invert
1072 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp6).addReg(Alpha::R31);
1073
1074 }
1075 // else
1076 // {
1077 // Node->dump();
1078 // assert(0 && "Not a setcc in setcc");
1079 // }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001080 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001081 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001082 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001083
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001084 case ISD::CopyFromReg:
1085 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001086 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001087 if (Result != notIn)
1088 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001089 else
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001090 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth40831c52005-01-28 06:57:18 +00001091
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001092 SDOperand Chain = N.getOperand(0);
1093
1094 Select(Chain);
1095 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1096 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1097 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1098 return Result;
1099 }
1100
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001101 //Most of the plain arithmetic and logic share the same form, and the same
1102 //constant immediate test
1103 case ISD::AND:
1104 case ISD::OR:
1105 case ISD::XOR:
1106 case ISD::SHL:
1107 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001108 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001109 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001110 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1111 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001112 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1113 {
1114 switch(opcode) {
1115 case ISD::AND: Opc = Alpha::ANDi; break;
1116 case ISD::OR: Opc = Alpha::BISi; break;
1117 case ISD::XOR: Opc = Alpha::XORi; break;
1118 case ISD::SHL: Opc = Alpha::SLi; break;
1119 case ISD::SRL: Opc = Alpha::SRLi; break;
1120 case ISD::SRA: Opc = Alpha::SRAi; break;
1121 case ISD::MUL: Opc = Alpha::MULQi; break;
1122 };
1123 Tmp1 = SelectExpr(N.getOperand(0));
1124 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1125 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1126 }
1127 else
1128 {
1129 switch(opcode) {
1130 case ISD::AND: Opc = Alpha::AND; break;
1131 case ISD::OR: Opc = Alpha::BIS; break;
1132 case ISD::XOR: Opc = Alpha::XOR; break;
1133 case ISD::SHL: Opc = Alpha::SL; break;
1134 case ISD::SRL: Opc = Alpha::SRL; break;
1135 case ISD::SRA: Opc = Alpha::SRA; break;
1136 case ISD::MUL: Opc = Alpha::MULQ; break;
1137 };
1138 Tmp1 = SelectExpr(N.getOperand(0));
1139 Tmp2 = SelectExpr(N.getOperand(1));
1140 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1141 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001142 return Result;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001143
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001144 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001145 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001146 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001147 bool isAdd = opcode == ISD::ADD;
1148
1149 //FIXME: first check for Scaled Adds and Subs!
1150 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001151 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1152 { //Normal imm add/sub
1153 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1154 Tmp1 = SelectExpr(N.getOperand(0));
1155 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1156 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1157 }
1158 else if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001159 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
1160 { //LDA //FIXME: expand the above condition a bit
1161 Tmp1 = SelectExpr(N.getOperand(0));
1162 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1163 if (!isAdd)
1164 Tmp2 = -Tmp2;
1165 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
1166 }
1167 else
1168 { //Normal add/sub
1169 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1170 Tmp1 = SelectExpr(N.getOperand(0));
1171 Tmp2 = SelectExpr(N.getOperand(1));
1172 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1173 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001174 return Result;
1175 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001176
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001177 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00001178 case ISD::SREM:
1179 case ISD::SDIV:
1180 case ISD::UDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001181 //FIXME: alpha really doesn't support any of these operations,
1182 // the ops are expanded into special library calls with
1183 // special calling conventions
1184 switch(opcode) {
1185 case ISD::UREM: Opc = Alpha::REMQU; break;
1186 case ISD::SREM: Opc = Alpha::REMQ; break;
1187 case ISD::UDIV: Opc = Alpha::DIVQU; break;
1188 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001189 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001190 Tmp1 = SelectExpr(N.getOperand(0));
1191 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth02981182005-01-26 01:24:38 +00001192 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001193 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001194
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001195 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001196 case ISD::FP_TO_SINT:
1197 {
1198 assert (DestType == MVT::i64 && "only quads can be loaded to");
1199 MVT::ValueType SrcType = N.getOperand(0).getValueType();
1200 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1201
1202 //The hard way:
1203 // Spill the integer to memory and reload it from there.
1204 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1205 MachineFunction *F = BB->getParent();
1206 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
1207
1208 //CVTTQ STT LDQ
1209 //CVTST CVTTQ STT LDQ
1210 if (SrcType == MVT::f32)
1211 {
1212 Tmp2 = MakeReg(MVT::f64);
1213 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1214 Tmp1 = Tmp2;
1215 }
1216 Tmp2 = MakeReg(MVT::f64);
1217 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
1218 BuildMI(BB, Alpha::STT, 3).addReg(Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1219 BuildMI(BB, Alpha::LDQ, 2, Result).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1220
1221 return Result;
1222 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001223
1224// // case ISD::FP_TO_UINT:
1225
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001226 case ISD::SELECT:
1227 {
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001228 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001229 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1230 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001231 // Get the condition into the zero flag.
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001232 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001233 return Result;
1234 }
1235
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001236 case ISD::Constant:
1237 {
Andrew Lenharth22d5a412005-02-02 00:51:15 +00001238 unsigned long val = cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001239 if (val < 32000 && (long)val > -32000)
1240 BuildMI(BB, Alpha::LOAD_IMM, 1, Result).addImm(val);
1241 else
1242 {
1243 MachineConstantPool *CP = BB->getParent()->getConstantPool();
1244 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
1245 unsigned CPI = CP->getConstantPoolIndex(C);
1246 AlphaLowering.restoreGP(BB);
1247 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
1248 }
1249 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001250 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001251
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001252 case ISD::LOAD:
1253 {
1254 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001255 if (Result != notIn)
1256 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001257 else
1258 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1259
1260 SDOperand Chain = N.getOperand(0);
1261 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001262 Select(Chain);
1263
Andrew Lenharthc23d6962005-02-02 04:35:44 +00001264 assert(N.getValue(0).getValueType() == MVT::i64 && "unknown Load dest type");
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001265
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001266 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth2afc8212005-02-02 03:36:35 +00001267 {
Andrew Lenharth2afc8212005-02-02 03:36:35 +00001268 AlphaLowering.restoreGP(BB);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001269 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
Andrew Lenharth2afc8212005-02-02 03:36:35 +00001270 }
1271 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1272 AlphaLowering.restoreGP(BB);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001273 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth2afc8212005-02-02 03:36:35 +00001274 }
Andrew Lenharth05380342005-02-07 05:07:00 +00001275 else if(Address.getOpcode() == ISD::FrameIndex)
1276 {
1277 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1278 BuildMI(BB, Alpha::LDQ, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
1279 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001280 else
Andrew Lenharth2afc8212005-02-02 03:36:35 +00001281 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001282 long offset;
1283 SelectAddr(Address, Tmp1, offset);
1284 BuildMI(BB, Alpha::LDQ, 2, Result).addImm(offset).addReg(Tmp1);
Andrew Lenharth2afc8212005-02-02 03:36:35 +00001285 }
1286 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001287 }
1288 }
1289
1290 return 0;
1291}
1292
1293void ISel::Select(SDOperand N) {
1294 unsigned Tmp1, Tmp2, Opc;
1295
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001296 // FIXME: Disable for our current expansion model!
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001297 if (/*!N->hasOneUse() &&*/ !ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001298 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001299
1300 SDNode *Node = N.Val;
1301
1302 switch (N.getOpcode()) {
1303
1304 default:
1305 Node->dump(); std::cerr << "\n";
1306 assert(0 && "Node not handled yet!");
1307
1308 case ISD::BRCOND: {
1309 MachineBasicBlock *Dest =
1310 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
1311
Andrew Lenharth9818c052005-02-05 13:19:12 +00001312 Select(N.getOperand(0)); //chain
1313
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001314 Tmp1 = SelectExpr(N.getOperand(1));
1315 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
1316 return;
1317 }
1318
1319 case ISD::BR: {
1320 MachineBasicBlock *Dest =
1321 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1322
1323 Select(N.getOperand(0));
1324 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
1325 return;
1326 }
1327
1328 case ISD::ImplicitDef:
1329 Select(N.getOperand(0));
1330 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
1331 return;
1332
1333 case ISD::EntryToken: return; // Noop
1334
1335 case ISD::TokenFactor:
1336 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1337 Select(Node->getOperand(i));
1338
1339 //N.Val->dump(); std::cerr << "\n";
1340 //assert(0 && "Node not handled yet!");
1341
1342 return;
1343
1344 case ISD::CopyToReg:
1345 Select(N.getOperand(0));
1346 Tmp1 = SelectExpr(N.getOperand(1));
1347 Tmp2 = cast<RegSDNode>(N)->getReg();
1348
1349 if (Tmp1 != Tmp2) {
1350 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1351 }
1352 return;
1353
1354 case ISD::RET:
1355 switch (N.getNumOperands()) {
1356 default:
1357 std::cerr << N.getNumOperands() << "\n";
1358 for (unsigned i = 0; i < N.getNumOperands(); ++i)
1359 std::cerr << N.getOperand(i).getValueType() << "\n";
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001360 Node->dump();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001361 assert(0 && "Unknown return instruction!");
1362 case 2:
1363 Select(N.getOperand(0));
1364 Tmp1 = SelectExpr(N.getOperand(1));
1365 switch (N.getOperand(1).getValueType()) {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001366 default: Node->dump(); assert(0 && "All other types should have been promoted!!");
1367 case MVT::f64:
1368 case MVT::f32:
1369 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1370 break;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001371 case MVT::i32:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001372 case MVT::i64:
1373 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1374 break;
1375 }
1376 break;
1377 case 1:
1378 Select(N.getOperand(0));
1379 break;
1380 }
1381 //Tmp2 = AlphaLowering.getRetAddr();
1382 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
1383 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
1384 return;
1385
1386 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001387 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001388 SDOperand Chain = N.getOperand(0);
1389 SDOperand Value = N.getOperand(1);
1390 SDOperand Address = N.getOperand(2);
1391 Select(Chain);
1392
1393 Tmp1 = SelectExpr(Value); //value
1394 MVT::ValueType DestType = Value.getValueType();
Andrew Lenharth65838902005-02-06 16:22:15 +00001395 switch(DestType) {
1396 default: assert(0 && "unknown Type in store");
1397 case MVT::i64: Opc = Alpha::STQ; break;
1398 case MVT::f64: Opc = Alpha::STT; break;
1399 case MVT::f32: Opc = Alpha::STS; break;
1400 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001401 if (Address.getOpcode() == ISD::GlobalAddress)
1402 {
1403 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001404 Opc = GetSymVersion(Opc);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001405 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1406 }
Andrew Lenharth05380342005-02-07 05:07:00 +00001407 else if(Address.getOpcode() == ISD::FrameIndex)
1408 {
1409 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1410 BuildMI(BB, Opc, 3).addReg(Tmp1).addFrameIndex(Tmp1).addReg(Alpha::F31);
1411 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001412 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001413 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001414 long offset;
1415 SelectAddr(Address, Tmp2, offset);
1416 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1417 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001418 return;
1419 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001420
1421 case ISD::EXTLOAD:
1422 case ISD::SEXTLOAD:
1423 case ISD::ZEXTLOAD:
1424 case ISD::LOAD:
1425 case ISD::CopyFromReg:
1426 case ISD::CALL:
1427// case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001428 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001429 SelectExpr(N);
1430 return;
1431
1432
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001433 case ISD::TRUNCSTORE:
1434 {
1435 SDOperand Chain = N.getOperand(0);
1436 SDOperand Value = N.getOperand(1);
1437 SDOperand Address = N.getOperand(2);
1438 Select(Chain);
1439
1440 MVT::ValueType DestType = cast<MVTSDNode>(Node)->getExtraValueType();
Andrew Lenharth65838902005-02-06 16:22:15 +00001441 switch(DestType) {
1442 default: assert(0 && "unknown Type in store");
1443 case MVT::i1: //FIXME: DAG does not promote this load
1444 case MVT::i8: Opc = Alpha::STB; break;
1445 case MVT::i16: Opc = Alpha::STW; break;
1446 case MVT::i32: Opc = Alpha::STL; break;
1447 }
1448
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001449 Tmp1 = SelectExpr(Value); //value
1450
1451 if (Address.getOpcode() == ISD::GlobalAddress)
1452 {
1453 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001454 Opc = GetSymVersion(Opc);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001455 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1456 }
Andrew Lenharth05380342005-02-07 05:07:00 +00001457 else if(Address.getOpcode() == ISD::FrameIndex)
1458 {
1459 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1460 BuildMI(BB, Opc, 3).addReg(Tmp1).addFrameIndex(Tmp1).addReg(Alpha::F31);
1461 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001462 else
1463 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001464 long offset;
1465 SelectAddr(Address, Tmp2, offset);
1466 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1467 }
1468 return;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001469 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001470
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001471 case ISD::ADJCALLSTACKDOWN:
1472 case ISD::ADJCALLSTACKUP:
1473 Select(N.getOperand(0));
1474 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1475
1476 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? Alpha::ADJUSTSTACKDOWN :
1477 Alpha::ADJUSTSTACKUP;
1478 BuildMI(BB, Opc, 1).addImm(Tmp1);
1479 return;
1480 }
1481 assert(0 && "Should not be reached!");
1482}
1483
1484
1485/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1486/// into a machine code representation using pattern matching and a machine
1487/// description file.
1488///
1489FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
1490 return new ISel(TM);
1491}