blob: 94a9ad521c6434fa582ad6998d4a83377ba49bdb [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
Andrew Lenhartha549deb2005-02-07 05:33:15 +0000493 Tmp1 = MakeReg(MVT::f32);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000494
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);
Andrew Lenhartha549deb2005-02-07 05:33:15 +0000505 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
506 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
507 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000508 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
509 {
510 AlphaLowering.restoreGP(BB);
Andrew Lenhartha549deb2005-02-07 05:33:15 +0000511 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addConstantPoolIndex(CP->getIndex());
512 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000513 }
Andrew Lenharth05380342005-02-07 05:07:00 +0000514 else if(Address.getOpcode() == ISD::FrameIndex)
515 {
Andrew Lenhartha549deb2005-02-07 05:33:15 +0000516 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
517 BuildMI(BB, Alpha::LDS, 2, Tmp1).addFrameIndex(Tmp2).addReg(Alpha::F31);
518 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharth05380342005-02-07 05:07:00 +0000519 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000520 else
521 {
522 long offset;
Andrew Lenhartha549deb2005-02-07 05:33:15 +0000523 SelectAddr(Address, Tmp2, offset);
524 BuildMI(BB, Alpha::LDS, 1, Tmp1).addImm(offset).addReg(Tmp2);
525 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000526 }
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000527 return Result;
528 }
Andrew Lenharth2c594352005-01-29 15:42:07 +0000529
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000530 case ISD::UINT_TO_FP:
531 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000532 {
533 assert (N.getOperand(0).getValueType() == MVT::i64 && "only quads can be loaded from");
534 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +0000535 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000536
537 //The hard way:
538 // Spill the integer to memory and reload it from there.
539 unsigned Size = MVT::getSizeInBits(MVT::i64)/8;
540 MachineFunction *F = BB->getParent();
541 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
542
Andrew Lenharth7efadce2005-01-31 01:44:26 +0000543 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addFrameIndex(FrameIdx).addReg(Alpha::F31);
544 BuildMI(BB, Alpha::LDT, 2, Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
545 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
546 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000547
548 //The easy way: doesn't work
549// //so these instructions are not supported on ev56
550// Opc = DestType == MVT::f64 ? Alpha::ITOFT : Alpha::ITOFS;
551// BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
552// Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
553// BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
554
Andrew Lenharth40831c52005-01-28 06:57:18 +0000555 return Result;
556 }
557 }
558 assert(0 && "should not get here");
559 return 0;
560}
561
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000562unsigned ISel::SelectExpr(SDOperand N) {
563 unsigned Result;
564 unsigned Tmp1, Tmp2, Tmp3;
565 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000566 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000567
568 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000569 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000570
571 unsigned &Reg = ExprMap[N];
572 if (Reg) return Reg;
573
574 if (N.getOpcode() != ISD::CALL)
575 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000576 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000577 else {
578 // If this is a call instruction, make sure to prepare ALL of the result
579 // values as well as the chain.
580 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000581 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000582 else {
583 Result = MakeReg(Node->getValueType(0));
584 ExprMap[N.getValue(0)] = Result;
585 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
586 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000587 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000588 }
589 }
590
Andrew Lenharth22088bb2005-02-02 15:05:33 +0000591 if (DestType == MVT::f64 || DestType == MVT::f32 ||
592 (opcode == ISD::LOAD &&
593 (N.getValue(0).getValueType() == MVT::f32 || N.getValue(0).getValueType() == MVT::f64)))
Andrew Lenharth40831c52005-01-28 06:57:18 +0000594 return SelectExprFP(N, Result);
595
596 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000597 default:
598 Node->dump();
599 assert(0 && "Node not handled!\n");
600
Andrew Lenharth2c594352005-01-29 15:42:07 +0000601 case ISD::ConstantPool:
602 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
603 AlphaLowering.restoreGP(BB);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000604 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000605 return Result;
606
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000607 case ISD::FrameIndex:
608 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Andrew Lenharth684f2292005-01-30 00:35:27 +0000609 BuildMI(BB, Alpha::LDA, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000610 return Result;
611
612 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +0000613 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000614 case ISD::SEXTLOAD:
Andrew Lenhartha549deb2005-02-07 05:33:15 +0000615 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000616 {
617 // Make sure we generate both values.
618 if (Result != notIn)
619 ExprMap[N.getValue(1)] = notIn; // Generate the token
620 else
621 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000622
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000623 SDOperand Chain = N.getOperand(0);
624 SDOperand Address = N.getOperand(1);
625 Select(Chain);
626
Andrew Lenharth03824012005-02-07 05:55:55 +0000627 assert(Node->getValueType(0) == MVT::i64 && "Unknown type to sign extend to.");
628 if (opcode == ISD::LOAD)
629 Opc = Alpha::LDQ;
630 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000631 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
632 default: Node->dump(); assert(0 && "Bad sign extend!");
Andrew Lenharthf311e8b2005-02-07 05:18:02 +0000633 case MVT::i32: Opc = Alpha::LDL; assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
634 case MVT::i16: Opc = Alpha::LDWU; assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
635 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
636 case MVT::i8: Opc = Alpha::LDBU; assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000637 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000638
639 if (Address.getOpcode() == ISD::GlobalAddress)
640 {
641 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000642 Opc = GetSymVersion(Opc);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000643 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
644 }
645 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
646 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000647 Opc = GetSymVersion(Opc);
648 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000649 }
Andrew Lenharth05380342005-02-07 05:07:00 +0000650 else if(Address.getOpcode() == ISD::FrameIndex)
651 {
652 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
653 BuildMI(BB, Opc, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
654 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000655 else
656 {
657 long offset;
658 SelectAddr(Address, Tmp1, offset);
659 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
660 }
661 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000662 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000663
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000664 case ISD::GlobalAddress:
665 AlphaLowering.restoreGP(BB);
666 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
667 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
668 return Result;
669
670 case ISD::CALL:
671 {
672 Select(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000673
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000674 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000675 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000676
677 //grab the arguments
678 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000679 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000680 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000681 argvregs.push_back(SelectExpr(N.getOperand(i)));
682
Andrew Lenharth684f2292005-01-30 00:35:27 +0000683 //in reg args
684 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
685 {
686 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
687 Alpha::R19, Alpha::R20, Alpha::R21};
688 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
689 Alpha::F19, Alpha::F20, Alpha::F21};
690 switch(N.getOperand(i+2).getValueType()) {
691 default:
692 Node->dump();
693 N.getOperand(i).Val->dump();
694 std::cerr << "Type for " << i << " is: " << N.getOperand(i+2).getValueType() << "\n";
695 assert(0 && "Unknown value type for call");
696 case MVT::i1:
697 case MVT::i8:
698 case MVT::i16:
699 case MVT::i32:
700 case MVT::i64:
701 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
702 break;
703 case MVT::f32:
704 case MVT::f64:
705 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
706 break;
707 }
708 }
709 //in mem args
710 for (int i = 6, e = argvregs.size(); i < e; ++i)
711 {
712 switch(N.getOperand(i+2).getValueType()) {
713 default:
714 Node->dump();
715 N.getOperand(i).Val->dump();
716 std::cerr << "Type for " << i << " is: " << N.getOperand(i+2).getValueType() << "\n";
717 assert(0 && "Unknown value type for call");
718 case MVT::i1:
719 case MVT::i8:
720 case MVT::i16:
721 case MVT::i32:
722 case MVT::i64:
723 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
724 break;
725 case MVT::f32:
726 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
727 break;
728 case MVT::f64:
729 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
730 break;
731 }
732 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000733 //build the right kind of call
734 if (GlobalAddressSDNode *GASD =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000735 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
736 {
737 AlphaLowering.restoreGP(BB);
738 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal(),true);
739 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000740 else if (ExternalSymbolSDNode *ESSDN =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000741 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
742 {
743 AlphaLowering.restoreGP(BB);
744 BuildMI(BB, Alpha::CALL, 0).addExternalSymbol(ESSDN->getSymbol(), true);
745 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000746 else
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000747 {
748 //no need to restore GP as we are doing an indirect call
749 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000750 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
751 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000752 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000753
754 //push the result into a virtual register
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000755
756 switch (Node->getValueType(0)) {
757 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000758 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000759 case MVT::i1:
760 case MVT::i8:
761 case MVT::i16:
762 case MVT::i32:
763 case MVT::i64:
764 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
765 break;
766 case MVT::f32:
767 case MVT::f64:
768 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
769 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000770 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000771 return Result+N.ResNo;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000772 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000773
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000774 case ISD::SIGN_EXTEND:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000775 abort();
776
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000777 case ISD::SIGN_EXTEND_INREG:
778 {
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000779 //Alpha has instructions for a bunch of signed 32 bit stuff
780 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
781 {
782 switch (N.getOperand(0).getOpcode()) {
783 case ISD::ADD:
784 case ISD::SUB:
785 case ISD::MUL:
786 {
787 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
788 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
789 //FIXME: first check for Scaled Adds and Subs!
790 if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
791 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
792 { //Normal imm add/sub
793 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
794 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
795 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
796 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
797 }
798 else
799 { //Normal add/sub
800 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULLi : Alpha::SUBL);
801 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
802 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
803 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
804 }
805 return Result;
806 }
807 default: break; //Fall Though;
808 }
809 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000810 Tmp1 = SelectExpr(N.getOperand(0));
811 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000812 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000813 switch(MVN->getExtraValueType())
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000814 {
815 default:
816 Node->dump();
817 assert(0 && "Sign Extend InReg not there yet");
818 break;
819 case MVT::i32:
820 {
821 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
822 break;
823 }
824 case MVT::i16:
825 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
826 break;
827 case MVT::i8:
828 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
829 break;
830 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000831 return Result;
832 }
833 case ISD::ZERO_EXTEND_INREG:
834 {
835 Tmp1 = SelectExpr(N.getOperand(0));
836 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000837 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000838 switch(MVN->getExtraValueType())
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000839 {
840 default:
841 Node->dump();
842 assert(0 && "Zero Extend InReg not there yet");
843 break;
844 case MVT::i32: Tmp2 = 0xf0; break;
845 case MVT::i16: Tmp2 = 0xfc; break;
846 case MVT::i8: Tmp2 = 0xfe; break;
847 case MVT::i1: //handle this one special
848 BuildMI(BB, Alpha::ANDi, 2, Result).addReg(Tmp1).addImm(1);
849 return Result;
850 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000851 BuildMI(BB, Alpha::ZAPi, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000852 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000853 }
854
855 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000856 {
857 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
858 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
859 bool isConst1 = false;
860 bool isConst2 = false;
861 int dir;
Andrew Lenharth9818c052005-02-05 13:19:12 +0000862
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000863 //Tmp1 = SelectExpr(N.getOperand(0));
864 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000865 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
866 isConst1 = true;
867 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000868 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
869 isConst2 = true;
870
871 switch (SetCC->getCondition()) {
872 default: Node->dump(); assert(0 && "Unknown integer comparison!");
873 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
874 case ISD::SETLT: Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
875 case ISD::SETLE: Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
876 case ISD::SETGT: Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
877 case ISD::SETGE: Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
878 case ISD::SETULT: Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
879 case ISD::SETUGT: Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
880 case ISD::SETULE: Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
881 case ISD::SETUGE: Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000882 case ISD::SETNE: {//Handle this one special
883 //std::cerr << "Alpha does not have a setne.\n";
884 //abort();
885 Tmp1 = SelectExpr(N.getOperand(0));
886 Tmp2 = SelectExpr(N.getOperand(1));
887 Tmp3 = MakeReg(MVT::i64);
888 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
889 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000890 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000891 return Result;
892 }
893 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000894 if (dir == 1) {
895 Tmp1 = SelectExpr(N.getOperand(0));
896 if (isConst2) {
897 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
898 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
899 } else {
900 Tmp2 = SelectExpr(N.getOperand(1));
901 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
902 }
903 } else if (dir == 2) {
904 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +0000905 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000906 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
907 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
908 } else {
909 Tmp2 = SelectExpr(N.getOperand(0));
910 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
911 }
912 } else { //dir == 0
913 if (isConst1) {
914 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
915 Tmp2 = SelectExpr(N.getOperand(1));
916 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
917 } else if (isConst2) {
918 Tmp1 = SelectExpr(N.getOperand(0));
919 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
920 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
921 } else {
922 Tmp1 = SelectExpr(N.getOperand(0));
923 Tmp2 = SelectExpr(N.getOperand(1));
924 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
925 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000926 }
927 } else {
928 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
929 bool rev = false;
930 bool inv = false;
931
932 switch (SetCC->getCondition()) {
933 default: Node->dump(); assert(0 && "Unknown FP comparison!");
934 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
935 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
936 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
937 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
938 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
939 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
940 }
941
942 Tmp1 = SelectExpr(N.getOperand(0));
943 Tmp2 = SelectExpr(N.getOperand(1));
944 //Can only compare doubles, and dag won't promote for me
945 if (SetCC->getOperand(0).getValueType() == MVT::f32)
946 {
947 Tmp3 = MakeReg(MVT::f64);
948 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
949 Tmp1 = Tmp3;
950 }
951 if (SetCC->getOperand(1).getValueType() == MVT::f32)
952 {
953 Tmp3 = MakeReg(MVT::f64);
954 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
955 Tmp1 = Tmp2;
956 }
Andrew Lenharth9818c052005-02-05 13:19:12 +0000957
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000958 if (rev) std::swap(Tmp1, Tmp2);
959 Tmp3 = MakeReg(MVT::f64);
960 //do the comparison
961 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
962
963 //now arrange for Result (int) to have a 1 or 0
964
965 // Spill the FP to memory and reload it from there.
966 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
967 MachineFunction *F = BB->getParent();
968 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
969 unsigned Tmp4 = MakeReg(MVT::f64);
970 BuildMI(BB, Alpha::CVTTQ, 1, Tmp4).addReg(Tmp3);
971 BuildMI(BB, Alpha::STT, 3).addReg(Tmp4).addFrameIndex(FrameIdx).addReg(Alpha::F31);
972 unsigned Tmp5 = MakeReg(MVT::i64);
973 BuildMI(BB, Alpha::LDQ, 2, Tmp5).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharth9818c052005-02-05 13:19:12 +0000974
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000975 //now, set result based on Tmp5
976 //Set Tmp6 if fp cmp was false
977 unsigned Tmp6 = MakeReg(MVT::i64);
978 BuildMI(BB, Alpha::CMPEQ, 2, Tmp6).addReg(Tmp5).addReg(Alpha::R31);
979 //and invert
980 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp6).addReg(Alpha::R31);
981
982 }
983 // else
984 // {
985 // Node->dump();
986 // assert(0 && "Not a setcc in setcc");
987 // }
Andrew Lenharth9818c052005-02-05 13:19:12 +0000988 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000989 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000990 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000991
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000992 case ISD::CopyFromReg:
993 {
Andrew Lenharth40831c52005-01-28 06:57:18 +0000994 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000995 if (Result != notIn)
996 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +0000997 else
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000998 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth40831c52005-01-28 06:57:18 +0000999
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001000 SDOperand Chain = N.getOperand(0);
1001
1002 Select(Chain);
1003 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1004 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1005 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1006 return Result;
1007 }
1008
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001009 //Most of the plain arithmetic and logic share the same form, and the same
1010 //constant immediate test
1011 case ISD::AND:
1012 case ISD::OR:
1013 case ISD::XOR:
1014 case ISD::SHL:
1015 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001016 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001017 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001018 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1019 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001020 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1021 {
1022 switch(opcode) {
1023 case ISD::AND: Opc = Alpha::ANDi; break;
1024 case ISD::OR: Opc = Alpha::BISi; break;
1025 case ISD::XOR: Opc = Alpha::XORi; break;
1026 case ISD::SHL: Opc = Alpha::SLi; break;
1027 case ISD::SRL: Opc = Alpha::SRLi; break;
1028 case ISD::SRA: Opc = Alpha::SRAi; break;
1029 case ISD::MUL: Opc = Alpha::MULQi; break;
1030 };
1031 Tmp1 = SelectExpr(N.getOperand(0));
1032 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1033 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1034 }
1035 else
1036 {
1037 switch(opcode) {
1038 case ISD::AND: Opc = Alpha::AND; break;
1039 case ISD::OR: Opc = Alpha::BIS; break;
1040 case ISD::XOR: Opc = Alpha::XOR; break;
1041 case ISD::SHL: Opc = Alpha::SL; break;
1042 case ISD::SRL: Opc = Alpha::SRL; break;
1043 case ISD::SRA: Opc = Alpha::SRA; break;
1044 case ISD::MUL: Opc = Alpha::MULQ; break;
1045 };
1046 Tmp1 = SelectExpr(N.getOperand(0));
1047 Tmp2 = SelectExpr(N.getOperand(1));
1048 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1049 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001050 return Result;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001051
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001052 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001053 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001054 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001055 bool isAdd = opcode == ISD::ADD;
1056
1057 //FIXME: first check for Scaled Adds and Subs!
1058 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001059 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1060 { //Normal imm add/sub
1061 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1062 Tmp1 = SelectExpr(N.getOperand(0));
1063 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1064 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1065 }
1066 else if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001067 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
1068 { //LDA //FIXME: expand the above condition a bit
1069 Tmp1 = SelectExpr(N.getOperand(0));
1070 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1071 if (!isAdd)
1072 Tmp2 = -Tmp2;
1073 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
1074 }
1075 else
1076 { //Normal add/sub
1077 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1078 Tmp1 = SelectExpr(N.getOperand(0));
1079 Tmp2 = SelectExpr(N.getOperand(1));
1080 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1081 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001082 return Result;
1083 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001084
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001085 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00001086 case ISD::SREM:
1087 case ISD::SDIV:
1088 case ISD::UDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001089 //FIXME: alpha really doesn't support any of these operations,
1090 // the ops are expanded into special library calls with
1091 // special calling conventions
1092 switch(opcode) {
1093 case ISD::UREM: Opc = Alpha::REMQU; break;
1094 case ISD::SREM: Opc = Alpha::REMQ; break;
1095 case ISD::UDIV: Opc = Alpha::DIVQU; break;
1096 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001097 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001098 Tmp1 = SelectExpr(N.getOperand(0));
1099 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth02981182005-01-26 01:24:38 +00001100 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001101 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001102
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001103 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001104 case ISD::FP_TO_SINT:
1105 {
1106 assert (DestType == MVT::i64 && "only quads can be loaded to");
1107 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001108 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001109 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1110
1111 //The hard way:
1112 // Spill the integer to memory and reload it from there.
1113 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1114 MachineFunction *F = BB->getParent();
1115 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
1116
1117 //CVTTQ STT LDQ
1118 //CVTST CVTTQ STT LDQ
1119 if (SrcType == MVT::f32)
1120 {
1121 Tmp2 = MakeReg(MVT::f64);
1122 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1123 Tmp1 = Tmp2;
1124 }
1125 Tmp2 = MakeReg(MVT::f64);
1126 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
1127 BuildMI(BB, Alpha::STT, 3).addReg(Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1128 BuildMI(BB, Alpha::LDQ, 2, Result).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1129
1130 return Result;
1131 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001132
1133// // case ISD::FP_TO_UINT:
1134
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001135 case ISD::SELECT:
1136 {
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001137 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001138 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1139 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001140 // Get the condition into the zero flag.
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001141 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001142 return Result;
1143 }
1144
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001145 case ISD::Constant:
1146 {
Andrew Lenharth22d5a412005-02-02 00:51:15 +00001147 unsigned long val = cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001148 if (val < 32000 && (long)val > -32000)
1149 BuildMI(BB, Alpha::LOAD_IMM, 1, Result).addImm(val);
1150 else
1151 {
1152 MachineConstantPool *CP = BB->getParent()->getConstantPool();
1153 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
1154 unsigned CPI = CP->getConstantPoolIndex(C);
1155 AlphaLowering.restoreGP(BB);
1156 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
1157 }
1158 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001159 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001160 }
1161
1162 return 0;
1163}
1164
1165void ISel::Select(SDOperand N) {
1166 unsigned Tmp1, Tmp2, Opc;
1167
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001168 // FIXME: Disable for our current expansion model!
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001169 if (/*!N->hasOneUse() &&*/ !ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001170 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001171
1172 SDNode *Node = N.Val;
1173
1174 switch (N.getOpcode()) {
1175
1176 default:
1177 Node->dump(); std::cerr << "\n";
1178 assert(0 && "Node not handled yet!");
1179
1180 case ISD::BRCOND: {
1181 MachineBasicBlock *Dest =
1182 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
1183
Andrew Lenharth9818c052005-02-05 13:19:12 +00001184 Select(N.getOperand(0)); //chain
1185
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001186 Tmp1 = SelectExpr(N.getOperand(1));
1187 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
1188 return;
1189 }
1190
1191 case ISD::BR: {
1192 MachineBasicBlock *Dest =
1193 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1194
1195 Select(N.getOperand(0));
1196 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
1197 return;
1198 }
1199
1200 case ISD::ImplicitDef:
1201 Select(N.getOperand(0));
1202 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
1203 return;
1204
1205 case ISD::EntryToken: return; // Noop
1206
1207 case ISD::TokenFactor:
1208 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1209 Select(Node->getOperand(i));
1210
1211 //N.Val->dump(); std::cerr << "\n";
1212 //assert(0 && "Node not handled yet!");
1213
1214 return;
1215
1216 case ISD::CopyToReg:
1217 Select(N.getOperand(0));
1218 Tmp1 = SelectExpr(N.getOperand(1));
1219 Tmp2 = cast<RegSDNode>(N)->getReg();
1220
1221 if (Tmp1 != Tmp2) {
1222 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1223 }
1224 return;
1225
1226 case ISD::RET:
1227 switch (N.getNumOperands()) {
1228 default:
1229 std::cerr << N.getNumOperands() << "\n";
1230 for (unsigned i = 0; i < N.getNumOperands(); ++i)
1231 std::cerr << N.getOperand(i).getValueType() << "\n";
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001232 Node->dump();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001233 assert(0 && "Unknown return instruction!");
1234 case 2:
1235 Select(N.getOperand(0));
1236 Tmp1 = SelectExpr(N.getOperand(1));
1237 switch (N.getOperand(1).getValueType()) {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001238 default: Node->dump(); assert(0 && "All other types should have been promoted!!");
1239 case MVT::f64:
1240 case MVT::f32:
1241 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1242 break;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001243 case MVT::i32:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001244 case MVT::i64:
1245 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1246 break;
1247 }
1248 break;
1249 case 1:
1250 Select(N.getOperand(0));
1251 break;
1252 }
1253 //Tmp2 = AlphaLowering.getRetAddr();
1254 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
1255 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
1256 return;
1257
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001258 case ISD::TRUNCSTORE:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001259 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001260 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001261 SDOperand Chain = N.getOperand(0);
1262 SDOperand Value = N.getOperand(1);
1263 SDOperand Address = N.getOperand(2);
1264 Select(Chain);
1265
1266 Tmp1 = SelectExpr(Value); //value
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001267 switch(Value.getValueType()) {
Andrew Lenharth65838902005-02-06 16:22:15 +00001268 default: assert(0 && "unknown Type in store");
1269 case MVT::i64: Opc = Alpha::STQ; break;
1270 case MVT::f64: Opc = Alpha::STT; break;
1271 case MVT::f32: Opc = Alpha::STS; break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001272 case MVT::i1: //FIXME: DAG does not promote this load
1273 case MVT::i8: Opc = Alpha::STB; break;
1274 case MVT::i16: Opc = Alpha::STW; break;
1275 case MVT::i32: Opc = Alpha::STL; break;
Andrew Lenharth65838902005-02-06 16:22:15 +00001276 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001277 if (Address.getOpcode() == ISD::GlobalAddress)
1278 {
1279 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001280 Opc = GetSymVersion(Opc);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001281 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1282 }
Andrew Lenharth05380342005-02-07 05:07:00 +00001283 else if(Address.getOpcode() == ISD::FrameIndex)
1284 {
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001285 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1286 BuildMI(BB, Opc, 3).addReg(Tmp1).addFrameIndex(Tmp2).addReg(Alpha::F31);
Andrew Lenharth05380342005-02-07 05:07:00 +00001287 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001288 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001289 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001290 long offset;
1291 SelectAddr(Address, Tmp2, offset);
1292 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1293 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001294 return;
1295 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001296
1297 case ISD::EXTLOAD:
1298 case ISD::SEXTLOAD:
1299 case ISD::ZEXTLOAD:
1300 case ISD::LOAD:
1301 case ISD::CopyFromReg:
1302 case ISD::CALL:
1303// case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001304 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001305 SelectExpr(N);
1306 return;
1307
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001308 case ISD::ADJCALLSTACKDOWN:
1309 case ISD::ADJCALLSTACKUP:
1310 Select(N.getOperand(0));
1311 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1312
1313 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? Alpha::ADJUSTSTACKDOWN :
1314 Alpha::ADJUSTSTACKUP;
1315 BuildMI(BB, Opc, 1).addImm(Tmp1);
1316 return;
1317 }
1318 assert(0 && "Should not be reached!");
1319}
1320
1321
1322/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1323/// into a machine code representation using pattern matching and a machine
1324/// description file.
1325///
1326FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
1327 return new ISel(TM);
1328}