blob: f7e8dfa3ecb6f25593bfc033656e96d274f69f0d [file] [log] [blame]
Misha Brukman2a8350a2005-02-05 02:24:26 +00001//===- AlphaISelPattern.cpp - A pattern matching inst selector for Alpha --===//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for Alpha.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Alpha.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000015#include "AlphaRegisterInfo.h"
16#include "llvm/Constants.h" // FIXME: REMOVE
17#include "llvm/Function.h"
18#include "llvm/CodeGen/MachineInstrBuilder.h"
19#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/CodeGen/SSARegMap.h"
25#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetLowering.h"
27#include "llvm/Support/MathExtras.h"
28#include "llvm/ADT/Statistic.h"
29#include <set>
Andrew Lenharth684f2292005-01-30 00:35:27 +000030#include <algorithm>
Andrew Lenharth304d0f32005-01-22 23:41:55 +000031using namespace llvm;
32
33//===----------------------------------------------------------------------===//
34// AlphaTargetLowering - Alpha Implementation of the TargetLowering interface
35namespace {
36 class AlphaTargetLowering : public TargetLowering {
37 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
38 unsigned GP; //GOT vreg
39 public:
40 AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
41 // Set up the TargetLowering object.
Andrew Lenharth3d65d312005-01-27 03:49:45 +000042 //I am having problems with shr n ubyte 1
Andrew Lenharth879ef222005-02-02 17:00:21 +000043 setShiftAmountType(MVT::i64);
44 setSetCCResultType(MVT::i64);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000045
Andrew Lenharth304d0f32005-01-22 23:41:55 +000046 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
47 addRegisterClass(MVT::f64, Alpha::FPRCRegisterClass);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000048 addRegisterClass(MVT::f32, Alpha::FPRCRegisterClass);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000049
Andrew Lenharthd2bb9602005-01-27 07:50:35 +000050 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000051
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +000052 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000053 setOperationAction(ISD::ZEXTLOAD , MVT::i32 , Expand);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000054
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +000055 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000056 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
57 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
58
Andrew Lenharth3d65d312005-01-27 03:49:45 +000059 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); //what is the sign expansion of 1? 1 or -1?
Andrew Lenharth02981182005-01-26 01:24:38 +000060
Andrew Lenharth9818c052005-02-05 13:19:12 +000061 setOperationAction(ISD::SREM , MVT::f32 , Expand);
62 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +000063
Andrew Lenharth8d163d22005-02-02 05:49:42 +000064 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
Andrew Lenharth9818c052005-02-05 13:19:12 +000065 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
66 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
67
Andrew Lenharth3d65d312005-01-27 03:49:45 +000068 computeRegisterProperties();
Andrew Lenharth304d0f32005-01-22 23:41:55 +000069
Andrew Lenharthd2bb9602005-01-27 07:50:35 +000070 addLegalFPImmediate(+0.0); //F31
Andrew Lenharth12dd2622005-02-03 21:01:15 +000071 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +000072 }
73
74 /// LowerArguments - This hook must be implemented to indicate how we should
75 /// lower the arguments for the specified function, into the specified DAG.
76 virtual std::vector<SDOperand>
77 LowerArguments(Function &F, SelectionDAG &DAG);
78
79 /// LowerCallTo - This hook lowers an abstract call to a function into an
80 /// actual call.
81 virtual std::pair<SDOperand, SDOperand>
82 LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
83 ArgListTy &Args, SelectionDAG &DAG);
84
85 virtual std::pair<SDOperand, SDOperand>
86 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
87
88 virtual std::pair<SDOperand,SDOperand>
89 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
90 const Type *ArgTy, SelectionDAG &DAG);
91
92 virtual std::pair<SDOperand, SDOperand>
93 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
94 SelectionDAG &DAG);
95
96 void restoreGP(MachineBasicBlock* BB)
97 {
98 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
99 }
100 };
101}
102
103//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
104
105//For now, just use variable size stack frame format
106
107//In a standard call, the first six items are passed in registers $16
108//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
109//of argument-to-register correspondence.) The remaining items are
110//collected in a memory argument list that is a naturally aligned
111//array of quadwords. In a standard call, this list, if present, must
112//be passed at 0(SP).
113//7 ... n 0(SP) ... (n-7)*8(SP)
114
115std::vector<SDOperand>
116AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
117{
118 std::vector<SDOperand> ArgValues;
119
120 // //#define FP $15
121 // //#define RA $26
122 // //#define PV $27
123 // //#define GP $29
124 // //#define SP $30
125
126 // assert(0 && "TODO");
127 MachineFunction &MF = DAG.getMachineFunction();
Andrew Lenharth05380342005-02-07 05:07:00 +0000128 MachineFrameInfo*MFI = MF.getFrameInfo();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000129
130 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
131 MachineBasicBlock& BB = MF.front();
132
133 //Handle the return address
134 //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
135
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000136 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
137 Alpha::R19, Alpha::R20, Alpha::R21};
138 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
139 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000140 unsigned argVreg[6];
141 unsigned argPreg[6];
142 unsigned argOpc[6];
143
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000144 int count = 0;
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000145
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000146 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I)
147 {
Andrew Lenharth40831c52005-01-28 06:57:18 +0000148 SDOperand newroot, argt;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000149 if (count < 6) {
150 switch (getValueType(I->getType())) {
151 default: std::cerr << "Unknown Type " << getValueType(I->getType()) << "\n"; abort();
152 case MVT::f64:
153 case MVT::f32:
154 BuildMI(&BB, Alpha::IDEF, 0, args_float[count]);
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000155 argVreg[count] = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(getValueType(I->getType())));
156 argPreg[count] = args_float[count];
157 argOpc[count] = Alpha::CPYS;
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000158 argt = newroot = DAG.getCopyFromReg(argVreg[count], getValueType(I->getType()), DAG.getRoot());
Andrew Lenharth684f2292005-01-30 00:35:27 +0000159 break;
160 case MVT::i1:
161 case MVT::i8:
162 case MVT::i16:
163 case MVT::i32:
164 case MVT::i64:
165 BuildMI(&BB, Alpha::IDEF, 0, args_int[count]);
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000166 argVreg[count] =MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
167 argPreg[count] = args_int[count];
168 argOpc[count] = Alpha::BIS;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000169 argt = newroot = DAG.getCopyFromReg(argVreg[count], MVT::i64, DAG.getRoot());
170 if (getValueType(I->getType()) != MVT::i64)
171 argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()), newroot);
172 break;
173 }
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000174 ++count;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000175 } else { //more args
176 // Create the frame index object for this incoming parameter...
177 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
178
179 // Create the SelectionDAG nodes corresponding to a load from this parameter
180 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
181 argt = newroot = DAG.getLoad(getValueType(I->getType()), DAG.getEntryNode(), FIN);
Andrew Lenharth40831c52005-01-28 06:57:18 +0000182 }
183 DAG.setRoot(newroot.getValue(1));
184 ArgValues.push_back(argt);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000185 }
186
187 BuildMI(&BB, Alpha::IDEF, 0, Alpha::R29);
188 BuildMI(&BB, Alpha::BIS, 2, GP).addReg(Alpha::R29).addReg(Alpha::R29);
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000189 for (int i = 0; i < count; ++i)
190 {
191 if (argPreg[i] == Alpha::F16 || argPreg[i] == Alpha::F17 || argPreg[i] == Alpha::F18 ||
192 argPreg[i] == Alpha::F19 || argPreg[i] == Alpha::F20 || argPreg[i] == Alpha::F21)
193 {
194 assert(argOpc[i] == Alpha::CPYS && "Using BIS for a float??");
195 }
196 BuildMI(&BB, argOpc[i], 2, argVreg[i]).addReg(argPreg[i]).addReg(argPreg[i]);
197 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000198
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000199 return ArgValues;
200}
201
202std::pair<SDOperand, SDOperand>
203AlphaTargetLowering::LowerCallTo(SDOperand Chain,
204 const Type *RetTy, SDOperand Callee,
205 ArgListTy &Args, SelectionDAG &DAG) {
206 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000207 if (Args.size() > 6)
208 NumBytes = (Args.size() - 6) * 8;
209
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000210 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
211 DAG.getConstant(NumBytes, getPointerTy()));
212 std::vector<SDOperand> args_to_use;
213 for (unsigned i = 0, e = Args.size(); i != e; ++i)
214 {
215 switch (getValueType(Args[i].second)) {
216 default: assert(0 && "Unexpected ValueType for argument!");
217 case MVT::i1:
218 case MVT::i8:
219 case MVT::i16:
220 case MVT::i32:
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000221 // Promote the integer to 64 bits. If the input type is signed use a
222 // sign extend, otherwise use a zero extend.
223 if (Args[i].second->isSigned())
Andrew Lenharth40831c52005-01-28 06:57:18 +0000224 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000225 else
Andrew Lenharth40831c52005-01-28 06:57:18 +0000226 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000227 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000228 case MVT::i64:
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000229 case MVT::f64:
230 case MVT::f32:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000231 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000232 }
233 args_to_use.push_back(Args[i].first);
234 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000235
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000236 std::vector<MVT::ValueType> RetVals;
237 MVT::ValueType RetTyVT = getValueType(RetTy);
238 if (RetTyVT != MVT::isVoid)
239 RetVals.push_back(RetTyVT);
240 RetVals.push_back(MVT::Other);
241
242 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee, args_to_use), 0);
243 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
244 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
245 DAG.getConstant(NumBytes, getPointerTy()));
246 return std::make_pair(TheCall, Chain);
247}
248
249std::pair<SDOperand, SDOperand>
250AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
251 //vastart just returns the address of the VarArgsFrameIndex slot.
252 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
253}
254
255std::pair<SDOperand,SDOperand> AlphaTargetLowering::
256LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
257 const Type *ArgTy, SelectionDAG &DAG) {
258 abort();
259}
260
261
262std::pair<SDOperand, SDOperand> AlphaTargetLowering::
263LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
264 SelectionDAG &DAG) {
265 abort();
266}
267
268
269
270
271
272namespace {
273
274 //===--------------------------------------------------------------------===//
275 /// ISel - Alpha specific code to select Alpha machine instructions for
276 /// SelectionDAG operations.
277 ///
278 class ISel : public SelectionDAGISel {
279
280 /// AlphaLowering - This object fully describes how to lower LLVM code to an
281 /// Alpha-specific SelectionDAG.
282 AlphaTargetLowering AlphaLowering;
283
284
285 /// ExprMap - As shared expressions are codegen'd, we keep track of which
286 /// vreg the value is produced in, so we only emit one copy of each compiled
287 /// tree.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000288 static const unsigned notIn = (unsigned)(-1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000289 std::map<SDOperand, unsigned> ExprMap;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000290
291 public:
292 ISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM) {
293 }
294
295 /// InstructionSelectBasicBlock - This callback is invoked by
296 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
297 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
298 // Codegen the basic block.
299 Select(DAG.getRoot());
300
301 // Clear state used for selection.
302 ExprMap.clear();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000303 }
304
305 unsigned SelectExpr(SDOperand N);
Andrew Lenharth40831c52005-01-28 06:57:18 +0000306 unsigned SelectExprFP(SDOperand N, unsigned Result);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000307 void Select(SDOperand N);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000308
309 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000310 };
311}
312
Andrew Lenharth65838902005-02-06 16:22:15 +0000313static unsigned GetSymVersion(unsigned opcode)
314{
315 switch (opcode) {
316 default: assert(0 && "unknown load or store"); return 0;
317 case Alpha::LDQ: return Alpha::LDQ_SYM;
318 case Alpha::LDS: return Alpha::LDS_SYM;
319 case Alpha::LDT: return Alpha::LDT_SYM;
320 case Alpha::LDL: return Alpha::LDL_SYM;
321 case Alpha::LDBU: return Alpha::LDBU_SYM;
322 case Alpha::LDWU: return Alpha::LDWU_SYM;
323 case Alpha::LDW: return Alpha::LDW_SYM;
324 case Alpha::LDB: return Alpha::LDB_SYM;
325 case Alpha::STQ: return Alpha::STQ_SYM;
326 case Alpha::STS: return Alpha::STS_SYM;
327 case Alpha::STT: return Alpha::STT_SYM;
328 case Alpha::STL: return Alpha::STL_SYM;
329 case Alpha::STW: return Alpha::STW_SYM;
330 case Alpha::STB: return Alpha::STB_SYM;
331 }
332}
333
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000334//Check to see if the load is a constant offset from a base register
335void ISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
336{
337 unsigned opcode = N.getOpcode();
338 if (opcode == ISD::ADD) {
339 if(N.getOperand(1).getOpcode() == ISD::Constant && cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
340 { //Normal imm add
341 Reg = SelectExpr(N.getOperand(0));
342 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
343 return;
344 }
345 else if(N.getOperand(0).getOpcode() == ISD::Constant && cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 32767)
346 {
347 Reg = SelectExpr(N.getOperand(1));
348 offset = cast<ConstantSDNode>(N.getOperand(0))->getValue();
349 return;
350 }
351 }
352 Reg = SelectExpr(N);
353 offset = 0;
354 return;
355}
356
Andrew Lenharth40831c52005-01-28 06:57:18 +0000357unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
358{
359 unsigned Tmp1, Tmp2, Tmp3;
360 unsigned Opc = 0;
361 SDNode *Node = N.Val;
362 MVT::ValueType DestType = N.getValueType();
363 unsigned opcode = N.getOpcode();
364
365 switch (opcode) {
366 default:
367 Node->dump();
368 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +0000369
Andrew Lenharth9818c052005-02-05 13:19:12 +0000370 case ISD::SELECT:
371 {
372 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
373 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
374 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000375
376
377 // Spill the cond to memory and reload it from there.
378 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
379 MachineFunction *F = BB->getParent();
380 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
381 unsigned Tmp4 = MakeReg(MVT::f64);
382 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addFrameIndex(FrameIdx).addReg(Alpha::F31);
383 BuildMI(BB, Alpha::LDT, 2, Tmp4).addFrameIndex(FrameIdx).addReg(Alpha::F31);
384 //now ideally, we don't have to do anything to the flag...
Andrew Lenharth9818c052005-02-05 13:19:12 +0000385 // Get the condition into the zero flag.
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000386 BuildMI(BB, Alpha::FCMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp4);
Andrew Lenharth9818c052005-02-05 13:19:12 +0000387 return Result;
388 }
389
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000390 case ISD::FP_ROUND:
391 assert (DestType == MVT::f32 && N.getOperand(0).getValueType() == MVT::f64 && "only f64 to f32 conversion supported here");
392 Tmp1 = SelectExpr(N.getOperand(0));
393 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
394 return Result;
395
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000396 case ISD::FP_EXTEND:
397 assert (DestType == MVT::f64 && N.getOperand(0).getValueType() == MVT::f32 && "only f32 to f64 conversion supported here");
398 Tmp1 = SelectExpr(N.getOperand(0));
399 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
400 return Result;
401
Andrew Lenharth2c594352005-01-29 15:42:07 +0000402 case ISD::CopyFromReg:
403 {
404 // Make sure we generate both values.
405 if (Result != notIn)
406 ExprMap[N.getValue(1)] = notIn; // Generate the token
407 else
408 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
409
410 SDOperand Chain = N.getOperand(0);
411
412 Select(Chain);
413 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
414 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
415 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
416 return Result;
417 }
418
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000419 case ISD::LOAD:
420 {
421 // Make sure we generate both values.
422 if (Result != notIn)
423 ExprMap[N.getValue(1)] = notIn; // Generate the token
424 else
425 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000426
427 //DestType = N.getValue(0).getValueType();
428
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000429 SDOperand Chain = N.getOperand(0);
430 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000431 Select(Chain);
Andrew Lenharth65838902005-02-06 16:22:15 +0000432 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
433
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000434 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000435 {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000436 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000437 Opc = GetSymVersion(Opc);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000438 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
439 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000440 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000441 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000442 Opc = GetSymVersion(Opc);
Andrew Lenharth97127a12005-02-05 17:41:39 +0000443 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000444 }
Andrew Lenharth05380342005-02-07 05:07:00 +0000445 else if(Address.getOpcode() == ISD::FrameIndex)
446 {
447 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
448 BuildMI(BB, Opc, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
449 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000450 else
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000451 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000452 long offset;
453 SelectAddr(Address, Tmp1, offset);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000454 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000455 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000456 return Result;
457 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000458 case ISD::ConstantFP:
459 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
460 if (CN->isExactlyValue(+0.0)) {
461 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000462 } else if ( CN->isExactlyValue(-0.0)) {
463 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +0000464 } else {
465 abort();
466 }
467 }
468 return Result;
469
470 case ISD::MUL:
471 case ISD::ADD:
472 case ISD::SUB:
473 case ISD::SDIV:
474 switch( opcode ) {
475 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
476 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
477 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
478 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
479 };
480 Tmp1 = SelectExpr(N.getOperand(0));
481 Tmp2 = SelectExpr(N.getOperand(1));
482 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
483 return Result;
484
Andrew Lenharth2c594352005-01-29 15:42:07 +0000485 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000486 {
487 //include a conversion sequence for float loads to double
488 if (Result != notIn)
489 ExprMap[N.getValue(1)] = notIn; // Generate the token
490 else
491 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
492
493 Tmp2 = MakeReg(MVT::f32);
494
495 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 && "EXTLOAD not from f32");
496 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
497
498 SDOperand Chain = N.getOperand(0);
499 SDOperand Address = N.getOperand(1);
500 Select(Chain);
501
502 if (Address.getOpcode() == ISD::GlobalAddress)
503 {
504 AlphaLowering.restoreGP(BB);
505 BuildMI(BB, Alpha::LDS_SYM, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
506 }
507 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
508 {
509 AlphaLowering.restoreGP(BB);
510 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp2).addConstantPoolIndex(CP->getIndex());
511 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp2);
512 }
Andrew Lenharth05380342005-02-07 05:07:00 +0000513 else if(Address.getOpcode() == ISD::FrameIndex)
514 {
515 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
516 BuildMI(BB, Opc, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
517 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000518 else
519 {
520 long offset;
521 SelectAddr(Address, Tmp1, offset);
522 BuildMI(BB, Alpha::LDS, 1, Tmp2).addImm(offset).addReg(Tmp1);
523 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp2);
524 }
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000525 return Result;
526 }
Andrew Lenharth2c594352005-01-29 15:42:07 +0000527
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000528 case ISD::UINT_TO_FP:
529 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000530 {
531 assert (N.getOperand(0).getValueType() == MVT::i64 && "only quads can be loaded from");
532 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +0000533 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000534
535 //The hard way:
536 // Spill the integer to memory and reload it from there.
537 unsigned Size = MVT::getSizeInBits(MVT::i64)/8;
538 MachineFunction *F = BB->getParent();
539 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
540
Andrew Lenharth7efadce2005-01-31 01:44:26 +0000541 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addFrameIndex(FrameIdx).addReg(Alpha::F31);
542 BuildMI(BB, Alpha::LDT, 2, Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
543 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
544 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000545
546 //The easy way: doesn't work
547// //so these instructions are not supported on ev56
548// Opc = DestType == MVT::f64 ? Alpha::ITOFT : Alpha::ITOFS;
549// BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
550// Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
551// BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
552
Andrew Lenharth40831c52005-01-28 06:57:18 +0000553 return Result;
554 }
555 }
556 assert(0 && "should not get here");
557 return 0;
558}
559
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000560unsigned ISel::SelectExpr(SDOperand N) {
561 unsigned Result;
562 unsigned Tmp1, Tmp2, Tmp3;
563 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000564 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000565
566 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000567 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000568
569 unsigned &Reg = ExprMap[N];
570 if (Reg) return Reg;
571
572 if (N.getOpcode() != ISD::CALL)
573 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000574 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000575 else {
576 // If this is a call instruction, make sure to prepare ALL of the result
577 // values as well as the chain.
578 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000579 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000580 else {
581 Result = MakeReg(Node->getValueType(0));
582 ExprMap[N.getValue(0)] = Result;
583 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
584 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000585 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000586 }
587 }
588
Andrew Lenharth22088bb2005-02-02 15:05:33 +0000589 if (DestType == MVT::f64 || DestType == MVT::f32 ||
590 (opcode == ISD::LOAD &&
591 (N.getValue(0).getValueType() == MVT::f32 || N.getValue(0).getValueType() == MVT::f64)))
Andrew Lenharth40831c52005-01-28 06:57:18 +0000592 return SelectExprFP(N, Result);
593
594 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000595 default:
596 Node->dump();
597 assert(0 && "Node not handled!\n");
598
Andrew Lenharth2c594352005-01-29 15:42:07 +0000599 case ISD::ConstantPool:
600 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
601 AlphaLowering.restoreGP(BB);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000602 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000603 return Result;
604
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000605 case ISD::FrameIndex:
606 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Andrew Lenharth684f2292005-01-30 00:35:27 +0000607 BuildMI(BB, Alpha::LDA, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000608 return Result;
609
610 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +0000611 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000612 case ISD::SEXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000613 {
614 // Make sure we generate both values.
615 if (Result != notIn)
616 ExprMap[N.getValue(1)] = notIn; // Generate the token
617 else
618 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000619
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000620 SDOperand Chain = N.getOperand(0);
621 SDOperand Address = N.getOperand(1);
622 Select(Chain);
623
624 switch(Node->getValueType(0)) {
625 default: Node->dump(); assert(0 && "Unknown type to sign extend to.");
626 case MVT::i64:
627 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
628 default: Node->dump(); assert(0 && "Bad sign extend!");
Andrew Lenharthf311e8b2005-02-07 05:18:02 +0000629 case MVT::i32: Opc = Alpha::LDL; assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
630 case MVT::i16: Opc = Alpha::LDWU; assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
631 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
632 case MVT::i8: Opc = Alpha::LDBU; assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000633 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000634 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000635
636 if (Address.getOpcode() == ISD::GlobalAddress)
637 {
638 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000639 Opc = GetSymVersion(Opc);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000640 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
641 }
642 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
643 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000644 Opc = GetSymVersion(Opc);
645 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000646 }
Andrew Lenharth05380342005-02-07 05:07:00 +0000647 else if(Address.getOpcode() == ISD::FrameIndex)
648 {
649 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
650 BuildMI(BB, Opc, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
651 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000652 else
653 {
654 long offset;
655 SelectAddr(Address, Tmp1, offset);
656 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
657 }
658 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000659 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000660
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000661 case ISD::GlobalAddress:
662 AlphaLowering.restoreGP(BB);
663 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
664 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
665 return Result;
666
667 case ISD::CALL:
668 {
669 Select(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000670
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000671 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000672 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000673
674 //grab the arguments
675 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000676 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000677 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000678 argvregs.push_back(SelectExpr(N.getOperand(i)));
679
Andrew Lenharth684f2292005-01-30 00:35:27 +0000680 //in reg args
681 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
682 {
683 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
684 Alpha::R19, Alpha::R20, Alpha::R21};
685 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
686 Alpha::F19, Alpha::F20, Alpha::F21};
687 switch(N.getOperand(i+2).getValueType()) {
688 default:
689 Node->dump();
690 N.getOperand(i).Val->dump();
691 std::cerr << "Type for " << i << " is: " << N.getOperand(i+2).getValueType() << "\n";
692 assert(0 && "Unknown value type for call");
693 case MVT::i1:
694 case MVT::i8:
695 case MVT::i16:
696 case MVT::i32:
697 case MVT::i64:
698 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
699 break;
700 case MVT::f32:
701 case MVT::f64:
702 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
703 break;
704 }
705 }
706 //in mem args
707 for (int i = 6, e = argvregs.size(); i < e; ++i)
708 {
709 switch(N.getOperand(i+2).getValueType()) {
710 default:
711 Node->dump();
712 N.getOperand(i).Val->dump();
713 std::cerr << "Type for " << i << " is: " << N.getOperand(i+2).getValueType() << "\n";
714 assert(0 && "Unknown value type for call");
715 case MVT::i1:
716 case MVT::i8:
717 case MVT::i16:
718 case MVT::i32:
719 case MVT::i64:
720 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
721 break;
722 case MVT::f32:
723 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
724 break;
725 case MVT::f64:
726 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
727 break;
728 }
729 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000730 //build the right kind of call
731 if (GlobalAddressSDNode *GASD =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000732 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
733 {
734 AlphaLowering.restoreGP(BB);
735 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal(),true);
736 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000737 else if (ExternalSymbolSDNode *ESSDN =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000738 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
739 {
740 AlphaLowering.restoreGP(BB);
741 BuildMI(BB, Alpha::CALL, 0).addExternalSymbol(ESSDN->getSymbol(), true);
742 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000743 else
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000744 {
745 //no need to restore GP as we are doing an indirect call
746 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000747 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
748 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000749 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000750
751 //push the result into a virtual register
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000752
753 switch (Node->getValueType(0)) {
754 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000755 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000756 case MVT::i1:
757 case MVT::i8:
758 case MVT::i16:
759 case MVT::i32:
760 case MVT::i64:
761 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
762 break;
763 case MVT::f32:
764 case MVT::f64:
765 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
766 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000767 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000768 return Result+N.ResNo;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000769 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000770
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000771 case ISD::SIGN_EXTEND:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000772 abort();
773
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000774 case ISD::SIGN_EXTEND_INREG:
775 {
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000776 //Alpha has instructions for a bunch of signed 32 bit stuff
777 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
778 {
779 switch (N.getOperand(0).getOpcode()) {
780 case ISD::ADD:
781 case ISD::SUB:
782 case ISD::MUL:
783 {
784 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
785 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
786 //FIXME: first check for Scaled Adds and Subs!
787 if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
788 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
789 { //Normal imm add/sub
790 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
791 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
792 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
793 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
794 }
795 else
796 { //Normal add/sub
797 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULLi : Alpha::SUBL);
798 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
799 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
800 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
801 }
802 return Result;
803 }
804 default: break; //Fall Though;
805 }
806 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000807 Tmp1 = SelectExpr(N.getOperand(0));
808 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000809 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000810 switch(MVN->getExtraValueType())
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000811 {
812 default:
813 Node->dump();
814 assert(0 && "Sign Extend InReg not there yet");
815 break;
816 case MVT::i32:
817 {
818 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
819 break;
820 }
821 case MVT::i16:
822 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
823 break;
824 case MVT::i8:
825 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
826 break;
827 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000828 return Result;
829 }
830 case ISD::ZERO_EXTEND_INREG:
831 {
832 Tmp1 = SelectExpr(N.getOperand(0));
833 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000834 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000835 switch(MVN->getExtraValueType())
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000836 {
837 default:
838 Node->dump();
839 assert(0 && "Zero Extend InReg not there yet");
840 break;
841 case MVT::i32: Tmp2 = 0xf0; break;
842 case MVT::i16: Tmp2 = 0xfc; break;
843 case MVT::i8: Tmp2 = 0xfe; break;
844 case MVT::i1: //handle this one special
845 BuildMI(BB, Alpha::ANDi, 2, Result).addReg(Tmp1).addImm(1);
846 return Result;
847 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000848 BuildMI(BB, Alpha::ZAPi, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000849 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000850 }
851
852 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000853 {
854 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
855 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
856 bool isConst1 = false;
857 bool isConst2 = false;
858 int dir;
Andrew Lenharth9818c052005-02-05 13:19:12 +0000859
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000860 //Tmp1 = SelectExpr(N.getOperand(0));
861 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000862 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
863 isConst1 = true;
864 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000865 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
866 isConst2 = true;
867
868 switch (SetCC->getCondition()) {
869 default: Node->dump(); assert(0 && "Unknown integer comparison!");
870 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
871 case ISD::SETLT: Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
872 case ISD::SETLE: Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
873 case ISD::SETGT: Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
874 case ISD::SETGE: Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
875 case ISD::SETULT: Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
876 case ISD::SETUGT: Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
877 case ISD::SETULE: Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
878 case ISD::SETUGE: Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000879 case ISD::SETNE: {//Handle this one special
880 //std::cerr << "Alpha does not have a setne.\n";
881 //abort();
882 Tmp1 = SelectExpr(N.getOperand(0));
883 Tmp2 = SelectExpr(N.getOperand(1));
884 Tmp3 = MakeReg(MVT::i64);
885 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
886 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000887 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000888 return Result;
889 }
890 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000891 if (dir == 1) {
892 Tmp1 = SelectExpr(N.getOperand(0));
893 if (isConst2) {
894 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
895 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
896 } else {
897 Tmp2 = SelectExpr(N.getOperand(1));
898 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
899 }
900 } else if (dir == 2) {
901 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +0000902 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000903 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
904 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
905 } else {
906 Tmp2 = SelectExpr(N.getOperand(0));
907 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
908 }
909 } else { //dir == 0
910 if (isConst1) {
911 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
912 Tmp2 = SelectExpr(N.getOperand(1));
913 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
914 } else if (isConst2) {
915 Tmp1 = SelectExpr(N.getOperand(0));
916 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
917 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
918 } else {
919 Tmp1 = SelectExpr(N.getOperand(0));
920 Tmp2 = SelectExpr(N.getOperand(1));
921 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
922 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000923 }
924 } else {
925 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
926 bool rev = false;
927 bool inv = false;
928
929 switch (SetCC->getCondition()) {
930 default: Node->dump(); assert(0 && "Unknown FP comparison!");
931 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
932 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
933 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
934 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
935 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
936 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
937 }
938
939 Tmp1 = SelectExpr(N.getOperand(0));
940 Tmp2 = SelectExpr(N.getOperand(1));
941 //Can only compare doubles, and dag won't promote for me
942 if (SetCC->getOperand(0).getValueType() == MVT::f32)
943 {
944 Tmp3 = MakeReg(MVT::f64);
945 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
946 Tmp1 = Tmp3;
947 }
948 if (SetCC->getOperand(1).getValueType() == MVT::f32)
949 {
950 Tmp3 = MakeReg(MVT::f64);
951 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
952 Tmp1 = Tmp2;
953 }
Andrew Lenharth9818c052005-02-05 13:19:12 +0000954
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000955 if (rev) std::swap(Tmp1, Tmp2);
956 Tmp3 = MakeReg(MVT::f64);
957 //do the comparison
958 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
959
960 //now arrange for Result (int) to have a 1 or 0
961
962 // Spill the FP to memory and reload it from there.
963 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
964 MachineFunction *F = BB->getParent();
965 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
966 unsigned Tmp4 = MakeReg(MVT::f64);
967 BuildMI(BB, Alpha::CVTTQ, 1, Tmp4).addReg(Tmp3);
968 BuildMI(BB, Alpha::STT, 3).addReg(Tmp4).addFrameIndex(FrameIdx).addReg(Alpha::F31);
969 unsigned Tmp5 = MakeReg(MVT::i64);
970 BuildMI(BB, Alpha::LDQ, 2, Tmp5).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharth9818c052005-02-05 13:19:12 +0000971
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000972 //now, set result based on Tmp5
973 //Set Tmp6 if fp cmp was false
974 unsigned Tmp6 = MakeReg(MVT::i64);
975 BuildMI(BB, Alpha::CMPEQ, 2, Tmp6).addReg(Tmp5).addReg(Alpha::R31);
976 //and invert
977 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp6).addReg(Alpha::R31);
978
979 }
980 // else
981 // {
982 // Node->dump();
983 // assert(0 && "Not a setcc in setcc");
984 // }
Andrew Lenharth9818c052005-02-05 13:19:12 +0000985 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000986 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000987 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000988
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000989 case ISD::CopyFromReg:
990 {
Andrew Lenharth40831c52005-01-28 06:57:18 +0000991 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000992 if (Result != notIn)
993 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +0000994 else
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000995 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth40831c52005-01-28 06:57:18 +0000996
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000997 SDOperand Chain = N.getOperand(0);
998
999 Select(Chain);
1000 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1001 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1002 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1003 return Result;
1004 }
1005
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001006 //Most of the plain arithmetic and logic share the same form, and the same
1007 //constant immediate test
1008 case ISD::AND:
1009 case ISD::OR:
1010 case ISD::XOR:
1011 case ISD::SHL:
1012 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001013 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001014 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001015 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1016 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001017 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1018 {
1019 switch(opcode) {
1020 case ISD::AND: Opc = Alpha::ANDi; break;
1021 case ISD::OR: Opc = Alpha::BISi; break;
1022 case ISD::XOR: Opc = Alpha::XORi; break;
1023 case ISD::SHL: Opc = Alpha::SLi; break;
1024 case ISD::SRL: Opc = Alpha::SRLi; break;
1025 case ISD::SRA: Opc = Alpha::SRAi; break;
1026 case ISD::MUL: Opc = Alpha::MULQi; break;
1027 };
1028 Tmp1 = SelectExpr(N.getOperand(0));
1029 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1030 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1031 }
1032 else
1033 {
1034 switch(opcode) {
1035 case ISD::AND: Opc = Alpha::AND; break;
1036 case ISD::OR: Opc = Alpha::BIS; break;
1037 case ISD::XOR: Opc = Alpha::XOR; break;
1038 case ISD::SHL: Opc = Alpha::SL; break;
1039 case ISD::SRL: Opc = Alpha::SRL; break;
1040 case ISD::SRA: Opc = Alpha::SRA; break;
1041 case ISD::MUL: Opc = Alpha::MULQ; break;
1042 };
1043 Tmp1 = SelectExpr(N.getOperand(0));
1044 Tmp2 = SelectExpr(N.getOperand(1));
1045 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1046 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001047 return Result;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001048
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001049 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001050 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001051 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001052 bool isAdd = opcode == ISD::ADD;
1053
1054 //FIXME: first check for Scaled Adds and Subs!
1055 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001056 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1057 { //Normal imm add/sub
1058 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1059 Tmp1 = SelectExpr(N.getOperand(0));
1060 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1061 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1062 }
1063 else if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001064 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
1065 { //LDA //FIXME: expand the above condition a bit
1066 Tmp1 = SelectExpr(N.getOperand(0));
1067 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1068 if (!isAdd)
1069 Tmp2 = -Tmp2;
1070 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
1071 }
1072 else
1073 { //Normal add/sub
1074 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1075 Tmp1 = SelectExpr(N.getOperand(0));
1076 Tmp2 = SelectExpr(N.getOperand(1));
1077 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1078 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001079 return Result;
1080 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001081
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001082 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00001083 case ISD::SREM:
1084 case ISD::SDIV:
1085 case ISD::UDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001086 //FIXME: alpha really doesn't support any of these operations,
1087 // the ops are expanded into special library calls with
1088 // special calling conventions
1089 switch(opcode) {
1090 case ISD::UREM: Opc = Alpha::REMQU; break;
1091 case ISD::SREM: Opc = Alpha::REMQ; break;
1092 case ISD::UDIV: Opc = Alpha::DIVQU; break;
1093 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001094 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001095 Tmp1 = SelectExpr(N.getOperand(0));
1096 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth02981182005-01-26 01:24:38 +00001097 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001098 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001099
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001100 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001101 case ISD::FP_TO_SINT:
1102 {
1103 assert (DestType == MVT::i64 && "only quads can be loaded to");
1104 MVT::ValueType SrcType = N.getOperand(0).getValueType();
1105 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1106
1107 //The hard way:
1108 // Spill the integer to memory and reload it from there.
1109 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1110 MachineFunction *F = BB->getParent();
1111 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
1112
1113 //CVTTQ STT LDQ
1114 //CVTST CVTTQ STT LDQ
1115 if (SrcType == MVT::f32)
1116 {
1117 Tmp2 = MakeReg(MVT::f64);
1118 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1119 Tmp1 = Tmp2;
1120 }
1121 Tmp2 = MakeReg(MVT::f64);
1122 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
1123 BuildMI(BB, Alpha::STT, 3).addReg(Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1124 BuildMI(BB, Alpha::LDQ, 2, Result).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1125
1126 return Result;
1127 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001128
1129// // case ISD::FP_TO_UINT:
1130
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001131 case ISD::SELECT:
1132 {
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001133 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001134 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1135 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001136 // Get the condition into the zero flag.
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001137 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001138 return Result;
1139 }
1140
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001141 case ISD::Constant:
1142 {
Andrew Lenharth22d5a412005-02-02 00:51:15 +00001143 unsigned long val = cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001144 if (val < 32000 && (long)val > -32000)
1145 BuildMI(BB, Alpha::LOAD_IMM, 1, Result).addImm(val);
1146 else
1147 {
1148 MachineConstantPool *CP = BB->getParent()->getConstantPool();
1149 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
1150 unsigned CPI = CP->getConstantPoolIndex(C);
1151 AlphaLowering.restoreGP(BB);
1152 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
1153 }
1154 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001155 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001156
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001157 case ISD::LOAD:
1158 {
1159 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001160 if (Result != notIn)
1161 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001162 else
1163 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1164
1165 SDOperand Chain = N.getOperand(0);
1166 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001167 Select(Chain);
1168
Andrew Lenharthc23d6962005-02-02 04:35:44 +00001169 assert(N.getValue(0).getValueType() == MVT::i64 && "unknown Load dest type");
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001170
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001171 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth2afc8212005-02-02 03:36:35 +00001172 {
Andrew Lenharth2afc8212005-02-02 03:36:35 +00001173 AlphaLowering.restoreGP(BB);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001174 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
Andrew Lenharth2afc8212005-02-02 03:36:35 +00001175 }
1176 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1177 AlphaLowering.restoreGP(BB);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001178 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth2afc8212005-02-02 03:36:35 +00001179 }
Andrew Lenharth05380342005-02-07 05:07:00 +00001180 else if(Address.getOpcode() == ISD::FrameIndex)
1181 {
1182 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
1183 BuildMI(BB, Alpha::LDQ, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
1184 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001185 else
Andrew Lenharth2afc8212005-02-02 03:36:35 +00001186 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001187 long offset;
1188 SelectAddr(Address, Tmp1, offset);
1189 BuildMI(BB, Alpha::LDQ, 2, Result).addImm(offset).addReg(Tmp1);
Andrew Lenharth2afc8212005-02-02 03:36:35 +00001190 }
1191 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001192 }
1193 }
1194
1195 return 0;
1196}
1197
1198void ISel::Select(SDOperand N) {
1199 unsigned Tmp1, Tmp2, Opc;
1200
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001201 // FIXME: Disable for our current expansion model!
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001202 if (/*!N->hasOneUse() &&*/ !ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001203 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001204
1205 SDNode *Node = N.Val;
1206
1207 switch (N.getOpcode()) {
1208
1209 default:
1210 Node->dump(); std::cerr << "\n";
1211 assert(0 && "Node not handled yet!");
1212
1213 case ISD::BRCOND: {
1214 MachineBasicBlock *Dest =
1215 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
1216
Andrew Lenharth9818c052005-02-05 13:19:12 +00001217 Select(N.getOperand(0)); //chain
1218
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001219 Tmp1 = SelectExpr(N.getOperand(1));
1220 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
1221 return;
1222 }
1223
1224 case ISD::BR: {
1225 MachineBasicBlock *Dest =
1226 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1227
1228 Select(N.getOperand(0));
1229 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
1230 return;
1231 }
1232
1233 case ISD::ImplicitDef:
1234 Select(N.getOperand(0));
1235 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
1236 return;
1237
1238 case ISD::EntryToken: return; // Noop
1239
1240 case ISD::TokenFactor:
1241 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1242 Select(Node->getOperand(i));
1243
1244 //N.Val->dump(); std::cerr << "\n";
1245 //assert(0 && "Node not handled yet!");
1246
1247 return;
1248
1249 case ISD::CopyToReg:
1250 Select(N.getOperand(0));
1251 Tmp1 = SelectExpr(N.getOperand(1));
1252 Tmp2 = cast<RegSDNode>(N)->getReg();
1253
1254 if (Tmp1 != Tmp2) {
1255 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1256 }
1257 return;
1258
1259 case ISD::RET:
1260 switch (N.getNumOperands()) {
1261 default:
1262 std::cerr << N.getNumOperands() << "\n";
1263 for (unsigned i = 0; i < N.getNumOperands(); ++i)
1264 std::cerr << N.getOperand(i).getValueType() << "\n";
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001265 Node->dump();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001266 assert(0 && "Unknown return instruction!");
1267 case 2:
1268 Select(N.getOperand(0));
1269 Tmp1 = SelectExpr(N.getOperand(1));
1270 switch (N.getOperand(1).getValueType()) {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001271 default: Node->dump(); assert(0 && "All other types should have been promoted!!");
1272 case MVT::f64:
1273 case MVT::f32:
1274 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1275 break;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001276 case MVT::i32:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001277 case MVT::i64:
1278 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1279 break;
1280 }
1281 break;
1282 case 1:
1283 Select(N.getOperand(0));
1284 break;
1285 }
1286 //Tmp2 = AlphaLowering.getRetAddr();
1287 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
1288 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
1289 return;
1290
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001291 case ISD::TRUNCSTORE:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001292 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001293 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001294 SDOperand Chain = N.getOperand(0);
1295 SDOperand Value = N.getOperand(1);
1296 SDOperand Address = N.getOperand(2);
1297 Select(Chain);
1298
1299 Tmp1 = SelectExpr(Value); //value
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001300 switch(Value.getValueType()) {
Andrew Lenharth65838902005-02-06 16:22:15 +00001301 default: assert(0 && "unknown Type in store");
1302 case MVT::i64: Opc = Alpha::STQ; break;
1303 case MVT::f64: Opc = Alpha::STT; break;
1304 case MVT::f32: Opc = Alpha::STS; break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001305 case MVT::i1: //FIXME: DAG does not promote this load
1306 case MVT::i8: Opc = Alpha::STB; break;
1307 case MVT::i16: Opc = Alpha::STW; break;
1308 case MVT::i32: Opc = Alpha::STL; break;
Andrew Lenharth65838902005-02-06 16:22:15 +00001309 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001310 if (Address.getOpcode() == ISD::GlobalAddress)
1311 {
1312 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001313 Opc = GetSymVersion(Opc);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001314 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1315 }
Andrew Lenharth05380342005-02-07 05:07:00 +00001316 else if(Address.getOpcode() == ISD::FrameIndex)
1317 {
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001318 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1319 BuildMI(BB, Opc, 3).addReg(Tmp1).addFrameIndex(Tmp2).addReg(Alpha::F31);
Andrew Lenharth05380342005-02-07 05:07:00 +00001320 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001321 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001322 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001323 long offset;
1324 SelectAddr(Address, Tmp2, offset);
1325 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1326 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001327 return;
1328 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001329
1330 case ISD::EXTLOAD:
1331 case ISD::SEXTLOAD:
1332 case ISD::ZEXTLOAD:
1333 case ISD::LOAD:
1334 case ISD::CopyFromReg:
1335 case ISD::CALL:
1336// case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001337 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001338 SelectExpr(N);
1339 return;
1340
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001341 case ISD::ADJCALLSTACKDOWN:
1342 case ISD::ADJCALLSTACKUP:
1343 Select(N.getOperand(0));
1344 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1345
1346 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? Alpha::ADJUSTSTACKDOWN :
1347 Alpha::ADJUSTSTACKUP;
1348 BuildMI(BB, Opc, 1).addImm(Tmp1);
1349 return;
1350 }
1351 assert(0 && "Should not be reached!");
1352}
1353
1354
1355/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1356/// into a machine code representation using pattern matching and a machine
1357/// description file.
1358///
1359FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
1360 return new ISel(TM);
1361}