blob: ca68f60bbc8636f815affbe63666d20f853c30eb [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();
128 MachineFrameInfo *MFI = MF.getFrameInfo();
129
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 Lenharthcc1b16f2005-01-28 23:17:54 +0000445 else
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000446 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000447 long offset;
448 SelectAddr(Address, Tmp1, offset);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000449 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000450 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000451 return Result;
452 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000453 case ISD::ConstantFP:
454 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
455 if (CN->isExactlyValue(+0.0)) {
456 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000457 } else if ( CN->isExactlyValue(-0.0)) {
458 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +0000459 } else {
460 abort();
461 }
462 }
463 return Result;
464
465 case ISD::MUL:
466 case ISD::ADD:
467 case ISD::SUB:
468 case ISD::SDIV:
469 switch( opcode ) {
470 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
471 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
472 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
473 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
474 };
475 Tmp1 = SelectExpr(N.getOperand(0));
476 Tmp2 = SelectExpr(N.getOperand(1));
477 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
478 return Result;
479
Andrew Lenharth2c594352005-01-29 15:42:07 +0000480 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000481 {
482 //include a conversion sequence for float loads to double
483 if (Result != notIn)
484 ExprMap[N.getValue(1)] = notIn; // Generate the token
485 else
486 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
487
488 Tmp2 = MakeReg(MVT::f32);
489
490 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 && "EXTLOAD not from f32");
491 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
492
493 SDOperand Chain = N.getOperand(0);
494 SDOperand Address = N.getOperand(1);
495 Select(Chain);
496
497 if (Address.getOpcode() == ISD::GlobalAddress)
498 {
499 AlphaLowering.restoreGP(BB);
500 BuildMI(BB, Alpha::LDS_SYM, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
501 }
502 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
503 {
504 AlphaLowering.restoreGP(BB);
505 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp2).addConstantPoolIndex(CP->getIndex());
506 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp2);
507 }
508 else
509 {
510 long offset;
511 SelectAddr(Address, Tmp1, offset);
512 BuildMI(BB, Alpha::LDS, 1, Tmp2).addImm(offset).addReg(Tmp1);
513 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp2);
514 }
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000515 return Result;
516 }
Andrew Lenharth2c594352005-01-29 15:42:07 +0000517
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000518 case ISD::UINT_TO_FP:
519 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000520 {
521 assert (N.getOperand(0).getValueType() == MVT::i64 && "only quads can be loaded from");
522 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +0000523 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000524
525 //The hard way:
526 // Spill the integer to memory and reload it from there.
527 unsigned Size = MVT::getSizeInBits(MVT::i64)/8;
528 MachineFunction *F = BB->getParent();
529 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
530
Andrew Lenharth7efadce2005-01-31 01:44:26 +0000531 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addFrameIndex(FrameIdx).addReg(Alpha::F31);
532 BuildMI(BB, Alpha::LDT, 2, Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
533 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
534 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000535
536 //The easy way: doesn't work
537// //so these instructions are not supported on ev56
538// Opc = DestType == MVT::f64 ? Alpha::ITOFT : Alpha::ITOFS;
539// BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
540// Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
541// BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
542
Andrew Lenharth40831c52005-01-28 06:57:18 +0000543 return Result;
544 }
545 }
546 assert(0 && "should not get here");
547 return 0;
548}
549
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000550unsigned ISel::SelectExpr(SDOperand N) {
551 unsigned Result;
552 unsigned Tmp1, Tmp2, Tmp3;
553 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000554 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000555
556 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000557 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000558
559 unsigned &Reg = ExprMap[N];
560 if (Reg) return Reg;
561
562 if (N.getOpcode() != ISD::CALL)
563 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000564 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000565 else {
566 // If this is a call instruction, make sure to prepare ALL of the result
567 // values as well as the chain.
568 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000569 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000570 else {
571 Result = MakeReg(Node->getValueType(0));
572 ExprMap[N.getValue(0)] = Result;
573 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
574 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000575 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000576 }
577 }
578
Andrew Lenharth22088bb2005-02-02 15:05:33 +0000579 if (DestType == MVT::f64 || DestType == MVT::f32 ||
580 (opcode == ISD::LOAD &&
581 (N.getValue(0).getValueType() == MVT::f32 || N.getValue(0).getValueType() == MVT::f64)))
Andrew Lenharth40831c52005-01-28 06:57:18 +0000582 return SelectExprFP(N, Result);
583
584 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000585 default:
586 Node->dump();
587 assert(0 && "Node not handled!\n");
588
Andrew Lenharth2c594352005-01-29 15:42:07 +0000589 case ISD::ConstantPool:
590 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
591 AlphaLowering.restoreGP(BB);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000592 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000593 return Result;
594
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000595 case ISD::FrameIndex:
596 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Andrew Lenharth684f2292005-01-30 00:35:27 +0000597 BuildMI(BB, Alpha::LDA, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000598 return Result;
599
600 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000601 {
602 // Make sure we generate both values.
603 if (Result != notIn)
604 ExprMap[N.getValue(1)] = notIn; // Generate the token
605 else
606 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
607
608 SDOperand Chain = N.getOperand(0);
609 SDOperand Address = N.getOperand(1);
610 Select(Chain);
611
612 switch(Node->getValueType(0)) {
613 default: Node->dump(); assert(0 && "Unknown type to sign extend to.");
Andrew Lenharthd279b412005-01-25 19:58:40 +0000614 case MVT::i64:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000615 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
616 default:
617 Node->dump();
618 assert(0 && "Bad extend load!");
619 case MVT::i64: Opc = Alpha::LDQ; break;
620 case MVT::i32: Opc = Alpha::LDL; break;
621 case MVT::i16: Opc = Alpha::LDWU; break;
622 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
623 case MVT::i8: Opc = Alpha::LDBU; break;
624 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000625 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000626
627 if (Address.getOpcode() == ISD::GlobalAddress)
628 {
629 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000630 Opc = GetSymVersion(Opc);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000631 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
632 }
633 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address))
634 {
635 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000636 Opc = GetSymVersion(Opc);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000637 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
638 }
639 else
640 {
641 long offset;
642 SelectAddr(Address, Tmp1, offset);
643 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
644 }
645 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000646 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000647
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000648 case ISD::SEXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000649 {
650 // Make sure we generate both values.
651 if (Result != notIn)
652 ExprMap[N.getValue(1)] = notIn; // Generate the token
653 else
654 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000655
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000656 SDOperand Chain = N.getOperand(0);
657 SDOperand Address = N.getOperand(1);
658 Select(Chain);
659
660 switch(Node->getValueType(0)) {
661 default: Node->dump(); assert(0 && "Unknown type to sign extend to.");
662 case MVT::i64:
663 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
664 default: Node->dump(); assert(0 && "Bad sign extend!");
665 case MVT::i32: Opc = Alpha::LDL; break;
666 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000667 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000668
669 if (Address.getOpcode() == ISD::GlobalAddress)
670 {
671 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000672 Opc = GetSymVersion(Opc);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000673 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
674 }
675 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
676 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000677 Opc = GetSymVersion(Opc);
678 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000679 }
680 else
681 {
682 long offset;
683 SelectAddr(Address, Tmp1, offset);
684 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
685 }
686 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000687 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000688
689 case ISD::ZEXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000690 {
691 // Make sure we generate both values.
692 if (Result != notIn)
693 ExprMap[N.getValue(1)] = notIn; // Generate the token
694 else
695 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
696
697 SDOperand Chain = N.getOperand(0);
698 SDOperand Address = N.getOperand(1);
699 Select(Chain);
700
701 switch(Node->getValueType(0)) {
702 default: Node->dump(); assert(0 && "Unknown type to zero extend to.");
703 case MVT::i64:
704 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
705 default: Node->dump(); assert(0 && "Bad sign extend!");
706 case MVT::i16: Opc = Alpha::LDWU; break;
707 case MVT::i8: Opc = Alpha::LDBU; break;
708 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000709 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000710
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000711 if (Address.getOpcode() == ISD::GlobalAddress)
712 {
713 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000714 Opc = GetSymVersion(Opc);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000715 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
716 }
717 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
718 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000719 Opc = GetSymVersion(Opc);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000720 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
721 }
722 else
723 {
724 long offset;
725 SelectAddr(Address, Tmp1, offset);
726 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
727 }
728 return Result;
729 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000730
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000731 case ISD::GlobalAddress:
732 AlphaLowering.restoreGP(BB);
733 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
734 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
735 return Result;
736
737 case ISD::CALL:
738 {
739 Select(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000740
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000741 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000742 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000743
744 //grab the arguments
745 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000746 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000747 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000748 argvregs.push_back(SelectExpr(N.getOperand(i)));
749
Andrew Lenharth684f2292005-01-30 00:35:27 +0000750 //in reg args
751 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
752 {
753 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
754 Alpha::R19, Alpha::R20, Alpha::R21};
755 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
756 Alpha::F19, Alpha::F20, Alpha::F21};
757 switch(N.getOperand(i+2).getValueType()) {
758 default:
759 Node->dump();
760 N.getOperand(i).Val->dump();
761 std::cerr << "Type for " << i << " is: " << N.getOperand(i+2).getValueType() << "\n";
762 assert(0 && "Unknown value type for call");
763 case MVT::i1:
764 case MVT::i8:
765 case MVT::i16:
766 case MVT::i32:
767 case MVT::i64:
768 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
769 break;
770 case MVT::f32:
771 case MVT::f64:
772 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
773 break;
774 }
775 }
776 //in mem args
777 for (int i = 6, e = argvregs.size(); i < e; ++i)
778 {
779 switch(N.getOperand(i+2).getValueType()) {
780 default:
781 Node->dump();
782 N.getOperand(i).Val->dump();
783 std::cerr << "Type for " << i << " is: " << N.getOperand(i+2).getValueType() << "\n";
784 assert(0 && "Unknown value type for call");
785 case MVT::i1:
786 case MVT::i8:
787 case MVT::i16:
788 case MVT::i32:
789 case MVT::i64:
790 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
791 break;
792 case MVT::f32:
793 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
794 break;
795 case MVT::f64:
796 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
797 break;
798 }
799 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000800 //build the right kind of call
801 if (GlobalAddressSDNode *GASD =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000802 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
803 {
804 AlphaLowering.restoreGP(BB);
805 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal(),true);
806 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000807 else if (ExternalSymbolSDNode *ESSDN =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000808 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
809 {
810 AlphaLowering.restoreGP(BB);
811 BuildMI(BB, Alpha::CALL, 0).addExternalSymbol(ESSDN->getSymbol(), true);
812 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000813 else
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000814 {
815 //no need to restore GP as we are doing an indirect call
816 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000817 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
818 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000819 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000820
821 //push the result into a virtual register
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000822
823 switch (Node->getValueType(0)) {
824 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000825 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000826 case MVT::i1:
827 case MVT::i8:
828 case MVT::i16:
829 case MVT::i32:
830 case MVT::i64:
831 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
832 break;
833 case MVT::f32:
834 case MVT::f64:
835 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
836 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000837 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000838 return Result+N.ResNo;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000839 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000840
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000841 case ISD::SIGN_EXTEND:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000842 abort();
843
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000844 case ISD::SIGN_EXTEND_INREG:
845 {
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000846 //Alpha has instructions for a bunch of signed 32 bit stuff
847 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
848 {
849 switch (N.getOperand(0).getOpcode()) {
850 case ISD::ADD:
851 case ISD::SUB:
852 case ISD::MUL:
853 {
854 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
855 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
856 //FIXME: first check for Scaled Adds and Subs!
857 if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
858 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
859 { //Normal imm add/sub
860 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
861 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
862 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
863 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
864 }
865 else
866 { //Normal add/sub
867 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULLi : Alpha::SUBL);
868 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
869 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
870 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
871 }
872 return Result;
873 }
874 default: break; //Fall Though;
875 }
876 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000877 Tmp1 = SelectExpr(N.getOperand(0));
878 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000879 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000880 switch(MVN->getExtraValueType())
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000881 {
882 default:
883 Node->dump();
884 assert(0 && "Sign Extend InReg not there yet");
885 break;
886 case MVT::i32:
887 {
888 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
889 break;
890 }
891 case MVT::i16:
892 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
893 break;
894 case MVT::i8:
895 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
896 break;
897 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000898 return Result;
899 }
900 case ISD::ZERO_EXTEND_INREG:
901 {
902 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 && "Zero Extend InReg not there yet");
910 break;
911 case MVT::i32: Tmp2 = 0xf0; break;
912 case MVT::i16: Tmp2 = 0xfc; break;
913 case MVT::i8: Tmp2 = 0xfe; break;
914 case MVT::i1: //handle this one special
915 BuildMI(BB, Alpha::ANDi, 2, Result).addReg(Tmp1).addImm(1);
916 return Result;
917 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000918 BuildMI(BB, Alpha::ZAPi, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000919 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000920 }
921
922 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000923 {
924 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
925 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
926 bool isConst1 = false;
927 bool isConst2 = false;
928 int dir;
Andrew Lenharth9818c052005-02-05 13:19:12 +0000929
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000930 //Tmp1 = SelectExpr(N.getOperand(0));
931 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000932 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
933 isConst1 = true;
934 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000935 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
936 isConst2 = true;
937
938 switch (SetCC->getCondition()) {
939 default: Node->dump(); assert(0 && "Unknown integer comparison!");
940 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
941 case ISD::SETLT: Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
942 case ISD::SETLE: Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
943 case ISD::SETGT: Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
944 case ISD::SETGE: Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
945 case ISD::SETULT: Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
946 case ISD::SETUGT: Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
947 case ISD::SETULE: Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
948 case ISD::SETUGE: Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000949 case ISD::SETNE: {//Handle this one special
950 //std::cerr << "Alpha does not have a setne.\n";
951 //abort();
952 Tmp1 = SelectExpr(N.getOperand(0));
953 Tmp2 = SelectExpr(N.getOperand(1));
954 Tmp3 = MakeReg(MVT::i64);
955 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
956 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000957 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000958 return Result;
959 }
960 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000961 if (dir == 1) {
962 Tmp1 = SelectExpr(N.getOperand(0));
963 if (isConst2) {
964 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
965 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
966 } else {
967 Tmp2 = SelectExpr(N.getOperand(1));
968 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
969 }
970 } else if (dir == 2) {
971 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +0000972 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000973 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
974 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
975 } else {
976 Tmp2 = SelectExpr(N.getOperand(0));
977 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
978 }
979 } else { //dir == 0
980 if (isConst1) {
981 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
982 Tmp2 = SelectExpr(N.getOperand(1));
983 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
984 } else if (isConst2) {
985 Tmp1 = SelectExpr(N.getOperand(0));
986 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
987 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
988 } else {
989 Tmp1 = SelectExpr(N.getOperand(0));
990 Tmp2 = SelectExpr(N.getOperand(1));
991 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
992 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000993 }
994 } else {
995 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
996 bool rev = false;
997 bool inv = false;
998
999 switch (SetCC->getCondition()) {
1000 default: Node->dump(); assert(0 && "Unknown FP comparison!");
1001 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
1002 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
1003 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
1004 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
1005 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
1006 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
1007 }
1008
1009 Tmp1 = SelectExpr(N.getOperand(0));
1010 Tmp2 = SelectExpr(N.getOperand(1));
1011 //Can only compare doubles, and dag won't promote for me
1012 if (SetCC->getOperand(0).getValueType() == MVT::f32)
1013 {
1014 Tmp3 = MakeReg(MVT::f64);
1015 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
1016 Tmp1 = Tmp3;
1017 }
1018 if (SetCC->getOperand(1).getValueType() == MVT::f32)
1019 {
1020 Tmp3 = MakeReg(MVT::f64);
1021 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
1022 Tmp1 = Tmp2;
1023 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001024
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001025 if (rev) std::swap(Tmp1, Tmp2);
1026 Tmp3 = MakeReg(MVT::f64);
1027 //do the comparison
1028 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1029
1030 //now arrange for Result (int) to have a 1 or 0
1031
1032 // Spill the FP to memory and reload it from there.
1033 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1034 MachineFunction *F = BB->getParent();
1035 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
1036 unsigned Tmp4 = MakeReg(MVT::f64);
1037 BuildMI(BB, Alpha::CVTTQ, 1, Tmp4).addReg(Tmp3);
1038 BuildMI(BB, Alpha::STT, 3).addReg(Tmp4).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1039 unsigned Tmp5 = MakeReg(MVT::i64);
1040 BuildMI(BB, Alpha::LDQ, 2, Tmp5).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharth9818c052005-02-05 13:19:12 +00001041
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001042 //now, set result based on Tmp5
1043 //Set Tmp6 if fp cmp was false
1044 unsigned Tmp6 = MakeReg(MVT::i64);
1045 BuildMI(BB, Alpha::CMPEQ, 2, Tmp6).addReg(Tmp5).addReg(Alpha::R31);
1046 //and invert
1047 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp6).addReg(Alpha::R31);
1048
1049 }
1050 // else
1051 // {
1052 // Node->dump();
1053 // assert(0 && "Not a setcc in setcc");
1054 // }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001055 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001056 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001057 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001058
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001059 case ISD::CopyFromReg:
1060 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001061 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001062 if (Result != notIn)
1063 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001064 else
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001065 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth40831c52005-01-28 06:57:18 +00001066
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001067 SDOperand Chain = N.getOperand(0);
1068
1069 Select(Chain);
1070 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1071 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1072 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1073 return Result;
1074 }
1075
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001076 //Most of the plain arithmetic and logic share the same form, and the same
1077 //constant immediate test
1078 case ISD::AND:
1079 case ISD::OR:
1080 case ISD::XOR:
1081 case ISD::SHL:
1082 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001083 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001084 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001085 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1086 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001087 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1088 {
1089 switch(opcode) {
1090 case ISD::AND: Opc = Alpha::ANDi; break;
1091 case ISD::OR: Opc = Alpha::BISi; break;
1092 case ISD::XOR: Opc = Alpha::XORi; break;
1093 case ISD::SHL: Opc = Alpha::SLi; break;
1094 case ISD::SRL: Opc = Alpha::SRLi; break;
1095 case ISD::SRA: Opc = Alpha::SRAi; break;
1096 case ISD::MUL: Opc = Alpha::MULQi; break;
1097 };
1098 Tmp1 = SelectExpr(N.getOperand(0));
1099 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1100 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1101 }
1102 else
1103 {
1104 switch(opcode) {
1105 case ISD::AND: Opc = Alpha::AND; break;
1106 case ISD::OR: Opc = Alpha::BIS; break;
1107 case ISD::XOR: Opc = Alpha::XOR; break;
1108 case ISD::SHL: Opc = Alpha::SL; break;
1109 case ISD::SRL: Opc = Alpha::SRL; break;
1110 case ISD::SRA: Opc = Alpha::SRA; break;
1111 case ISD::MUL: Opc = Alpha::MULQ; break;
1112 };
1113 Tmp1 = SelectExpr(N.getOperand(0));
1114 Tmp2 = SelectExpr(N.getOperand(1));
1115 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1116 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001117 return Result;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001118
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001119 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001120 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001121 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001122 bool isAdd = opcode == ISD::ADD;
1123
1124 //FIXME: first check for Scaled Adds and Subs!
1125 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001126 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1127 { //Normal imm add/sub
1128 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1129 Tmp1 = SelectExpr(N.getOperand(0));
1130 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1131 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1132 }
1133 else if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001134 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
1135 { //LDA //FIXME: expand the above condition a bit
1136 Tmp1 = SelectExpr(N.getOperand(0));
1137 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1138 if (!isAdd)
1139 Tmp2 = -Tmp2;
1140 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
1141 }
1142 else
1143 { //Normal add/sub
1144 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1145 Tmp1 = SelectExpr(N.getOperand(0));
1146 Tmp2 = SelectExpr(N.getOperand(1));
1147 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1148 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001149 return Result;
1150 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001151
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001152 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00001153 case ISD::SREM:
1154 case ISD::SDIV:
1155 case ISD::UDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001156 //FIXME: alpha really doesn't support any of these operations,
1157 // the ops are expanded into special library calls with
1158 // special calling conventions
1159 switch(opcode) {
1160 case ISD::UREM: Opc = Alpha::REMQU; break;
1161 case ISD::SREM: Opc = Alpha::REMQ; break;
1162 case ISD::UDIV: Opc = Alpha::DIVQU; break;
1163 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001164 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001165 Tmp1 = SelectExpr(N.getOperand(0));
1166 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth02981182005-01-26 01:24:38 +00001167 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001168 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001169
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001170 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001171 case ISD::FP_TO_SINT:
1172 {
1173 assert (DestType == MVT::i64 && "only quads can be loaded to");
1174 MVT::ValueType SrcType = N.getOperand(0).getValueType();
1175 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1176
1177 //The hard way:
1178 // Spill the integer to memory and reload it from there.
1179 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1180 MachineFunction *F = BB->getParent();
1181 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
1182
1183 //CVTTQ STT LDQ
1184 //CVTST CVTTQ STT LDQ
1185 if (SrcType == MVT::f32)
1186 {
1187 Tmp2 = MakeReg(MVT::f64);
1188 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1189 Tmp1 = Tmp2;
1190 }
1191 Tmp2 = MakeReg(MVT::f64);
1192 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
1193 BuildMI(BB, Alpha::STT, 3).addReg(Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1194 BuildMI(BB, Alpha::LDQ, 2, Result).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1195
1196 return Result;
1197 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001198
1199// // case ISD::FP_TO_UINT:
1200
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001201 case ISD::SELECT:
1202 {
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001203 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001204 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1205 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001206 // Get the condition into the zero flag.
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001207 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001208 return Result;
1209 }
1210
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001211 case ISD::Constant:
1212 {
Andrew Lenharth22d5a412005-02-02 00:51:15 +00001213 unsigned long val = cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001214 if (val < 32000 && (long)val > -32000)
1215 BuildMI(BB, Alpha::LOAD_IMM, 1, Result).addImm(val);
1216 else
1217 {
1218 MachineConstantPool *CP = BB->getParent()->getConstantPool();
1219 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
1220 unsigned CPI = CP->getConstantPoolIndex(C);
1221 AlphaLowering.restoreGP(BB);
1222 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
1223 }
1224 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001225 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001226
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001227 case ISD::LOAD:
1228 {
1229 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001230 if (Result != notIn)
1231 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001232 else
1233 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1234
1235 SDOperand Chain = N.getOperand(0);
1236 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001237 Select(Chain);
1238
Andrew Lenharthc23d6962005-02-02 04:35:44 +00001239 assert(N.getValue(0).getValueType() == MVT::i64 && "unknown Load dest type");
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001240
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001241 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth2afc8212005-02-02 03:36:35 +00001242 {
Andrew Lenharth2afc8212005-02-02 03:36:35 +00001243 AlphaLowering.restoreGP(BB);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001244 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
Andrew Lenharth2afc8212005-02-02 03:36:35 +00001245 }
1246 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1247 AlphaLowering.restoreGP(BB);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001248 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth2afc8212005-02-02 03:36:35 +00001249 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001250 else
Andrew Lenharth2afc8212005-02-02 03:36:35 +00001251 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001252 long offset;
1253 SelectAddr(Address, Tmp1, offset);
1254 BuildMI(BB, Alpha::LDQ, 2, Result).addImm(offset).addReg(Tmp1);
Andrew Lenharth2afc8212005-02-02 03:36:35 +00001255 }
1256 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001257 }
1258 }
1259
1260 return 0;
1261}
1262
1263void ISel::Select(SDOperand N) {
1264 unsigned Tmp1, Tmp2, Opc;
1265
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001266 // FIXME: Disable for our current expansion model!
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001267 if (/*!N->hasOneUse() &&*/ !ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001268 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001269
1270 SDNode *Node = N.Val;
1271
1272 switch (N.getOpcode()) {
1273
1274 default:
1275 Node->dump(); std::cerr << "\n";
1276 assert(0 && "Node not handled yet!");
1277
1278 case ISD::BRCOND: {
1279 MachineBasicBlock *Dest =
1280 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
1281
Andrew Lenharth9818c052005-02-05 13:19:12 +00001282 Select(N.getOperand(0)); //chain
1283
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001284 Tmp1 = SelectExpr(N.getOperand(1));
1285 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
1286 return;
1287 }
1288
1289 case ISD::BR: {
1290 MachineBasicBlock *Dest =
1291 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1292
1293 Select(N.getOperand(0));
1294 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
1295 return;
1296 }
1297
1298 case ISD::ImplicitDef:
1299 Select(N.getOperand(0));
1300 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
1301 return;
1302
1303 case ISD::EntryToken: return; // Noop
1304
1305 case ISD::TokenFactor:
1306 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1307 Select(Node->getOperand(i));
1308
1309 //N.Val->dump(); std::cerr << "\n";
1310 //assert(0 && "Node not handled yet!");
1311
1312 return;
1313
1314 case ISD::CopyToReg:
1315 Select(N.getOperand(0));
1316 Tmp1 = SelectExpr(N.getOperand(1));
1317 Tmp2 = cast<RegSDNode>(N)->getReg();
1318
1319 if (Tmp1 != Tmp2) {
1320 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1321 }
1322 return;
1323
1324 case ISD::RET:
1325 switch (N.getNumOperands()) {
1326 default:
1327 std::cerr << N.getNumOperands() << "\n";
1328 for (unsigned i = 0; i < N.getNumOperands(); ++i)
1329 std::cerr << N.getOperand(i).getValueType() << "\n";
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001330 Node->dump();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001331 assert(0 && "Unknown return instruction!");
1332 case 2:
1333 Select(N.getOperand(0));
1334 Tmp1 = SelectExpr(N.getOperand(1));
1335 switch (N.getOperand(1).getValueType()) {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001336 default: Node->dump(); assert(0 && "All other types should have been promoted!!");
1337 case MVT::f64:
1338 case MVT::f32:
1339 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1340 break;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001341 case MVT::i32:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001342 case MVT::i64:
1343 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1344 break;
1345 }
1346 break;
1347 case 1:
1348 Select(N.getOperand(0));
1349 break;
1350 }
1351 //Tmp2 = AlphaLowering.getRetAddr();
1352 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
1353 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
1354 return;
1355
1356 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001357 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001358 SDOperand Chain = N.getOperand(0);
1359 SDOperand Value = N.getOperand(1);
1360 SDOperand Address = N.getOperand(2);
1361 Select(Chain);
1362
1363 Tmp1 = SelectExpr(Value); //value
1364 MVT::ValueType DestType = Value.getValueType();
Andrew Lenharth65838902005-02-06 16:22:15 +00001365 switch(DestType) {
1366 default: assert(0 && "unknown Type in store");
1367 case MVT::i64: Opc = Alpha::STQ; break;
1368 case MVT::f64: Opc = Alpha::STT; break;
1369 case MVT::f32: Opc = Alpha::STS; break;
1370 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001371 if (Address.getOpcode() == ISD::GlobalAddress)
1372 {
1373 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001374 Opc = GetSymVersion(Opc);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001375 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1376 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001377 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001378 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001379 long offset;
1380 SelectAddr(Address, Tmp2, offset);
1381 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1382 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001383 return;
1384 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001385
1386 case ISD::EXTLOAD:
1387 case ISD::SEXTLOAD:
1388 case ISD::ZEXTLOAD:
1389 case ISD::LOAD:
1390 case ISD::CopyFromReg:
1391 case ISD::CALL:
1392// case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001393 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001394 SelectExpr(N);
1395 return;
1396
1397
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001398 case ISD::TRUNCSTORE:
1399 {
1400 SDOperand Chain = N.getOperand(0);
1401 SDOperand Value = N.getOperand(1);
1402 SDOperand Address = N.getOperand(2);
1403 Select(Chain);
1404
1405 MVT::ValueType DestType = cast<MVTSDNode>(Node)->getExtraValueType();
Andrew Lenharth65838902005-02-06 16:22:15 +00001406 switch(DestType) {
1407 default: assert(0 && "unknown Type in store");
1408 case MVT::i1: //FIXME: DAG does not promote this load
1409 case MVT::i8: Opc = Alpha::STB; break;
1410 case MVT::i16: Opc = Alpha::STW; break;
1411 case MVT::i32: Opc = Alpha::STL; break;
1412 }
1413
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001414 Tmp1 = SelectExpr(Value); //value
1415
1416 if (Address.getOpcode() == ISD::GlobalAddress)
1417 {
1418 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001419 Opc = GetSymVersion(Opc);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001420 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1421 }
1422 else
1423 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001424 long offset;
1425 SelectAddr(Address, Tmp2, offset);
1426 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1427 }
1428 return;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001429 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001430
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001431 case ISD::ADJCALLSTACKDOWN:
1432 case ISD::ADJCALLSTACKUP:
1433 Select(N.getOperand(0));
1434 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1435
1436 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? Alpha::ADJUSTSTACKDOWN :
1437 Alpha::ADJUSTSTACKUP;
1438 BuildMI(BB, Opc, 1).addImm(Tmp1);
1439 return;
1440 }
1441 assert(0 && "Should not be reached!");
1442}
1443
1444
1445/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1446/// into a machine code representation using pattern matching and a machine
1447/// description file.
1448///
1449FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
1450 return new ISel(TM);
1451}