blob: 6e4704c3ce5ac8b8121dcaa983ade1ea384631a5 [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 ||
Andrew Lenharth06342c32005-02-07 06:21:37 +0000592 (
593 (opcode == ISD::LOAD || opcode == ISD::CopyFromReg || opcode == ISD::EXTLOAD) &&
594 (N.getValue(0).getValueType() == MVT::f32 || N.getValue(0).getValueType() == MVT::f64)
595 )
596 )
Andrew Lenharth40831c52005-01-28 06:57:18 +0000597 return SelectExprFP(N, Result);
598
599 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000600 default:
601 Node->dump();
602 assert(0 && "Node not handled!\n");
603
Andrew Lenharth2c594352005-01-29 15:42:07 +0000604 case ISD::ConstantPool:
605 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
606 AlphaLowering.restoreGP(BB);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000607 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000608 return Result;
609
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000610 case ISD::FrameIndex:
611 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Andrew Lenharth684f2292005-01-30 00:35:27 +0000612 BuildMI(BB, Alpha::LDA, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000613 return Result;
614
615 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +0000616 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000617 case ISD::SEXTLOAD:
Andrew Lenhartha549deb2005-02-07 05:33:15 +0000618 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000619 {
620 // Make sure we generate both values.
621 if (Result != notIn)
622 ExprMap[N.getValue(1)] = notIn; // Generate the token
623 else
624 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000625
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000626 SDOperand Chain = N.getOperand(0);
627 SDOperand Address = N.getOperand(1);
628 Select(Chain);
629
Andrew Lenharth03824012005-02-07 05:55:55 +0000630 assert(Node->getValueType(0) == MVT::i64 && "Unknown type to sign extend to.");
631 if (opcode == ISD::LOAD)
632 Opc = Alpha::LDQ;
633 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000634 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
635 default: Node->dump(); assert(0 && "Bad sign extend!");
Andrew Lenharthf311e8b2005-02-07 05:18:02 +0000636 case MVT::i32: Opc = Alpha::LDL; assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
637 case MVT::i16: Opc = Alpha::LDWU; assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
638 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
639 case MVT::i8: Opc = Alpha::LDBU; assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000640 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000641
642 if (Address.getOpcode() == ISD::GlobalAddress)
643 {
644 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000645 Opc = GetSymVersion(Opc);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000646 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
647 }
648 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
649 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000650 Opc = GetSymVersion(Opc);
651 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000652 }
Andrew Lenharth05380342005-02-07 05:07:00 +0000653 else if(Address.getOpcode() == ISD::FrameIndex)
654 {
655 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
656 BuildMI(BB, Opc, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
657 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000658 else
659 {
660 long offset;
661 SelectAddr(Address, Tmp1, offset);
662 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
663 }
664 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000665 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000666
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000667 case ISD::GlobalAddress:
668 AlphaLowering.restoreGP(BB);
669 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
670 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
671 return Result;
672
673 case ISD::CALL:
674 {
675 Select(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000676
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000677 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000678 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000679
680 //grab the arguments
681 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000682 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000683 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000684 argvregs.push_back(SelectExpr(N.getOperand(i)));
685
Andrew Lenharth684f2292005-01-30 00:35:27 +0000686 //in reg args
687 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
688 {
689 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
690 Alpha::R19, Alpha::R20, Alpha::R21};
691 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
692 Alpha::F19, Alpha::F20, Alpha::F21};
693 switch(N.getOperand(i+2).getValueType()) {
694 default:
695 Node->dump();
696 N.getOperand(i).Val->dump();
697 std::cerr << "Type for " << i << " is: " << N.getOperand(i+2).getValueType() << "\n";
698 assert(0 && "Unknown value type for call");
699 case MVT::i1:
700 case MVT::i8:
701 case MVT::i16:
702 case MVT::i32:
703 case MVT::i64:
704 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
705 break;
706 case MVT::f32:
707 case MVT::f64:
708 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
709 break;
710 }
711 }
712 //in mem args
713 for (int i = 6, e = argvregs.size(); i < e; ++i)
714 {
715 switch(N.getOperand(i+2).getValueType()) {
716 default:
717 Node->dump();
718 N.getOperand(i).Val->dump();
719 std::cerr << "Type for " << i << " is: " << N.getOperand(i+2).getValueType() << "\n";
720 assert(0 && "Unknown value type for call");
721 case MVT::i1:
722 case MVT::i8:
723 case MVT::i16:
724 case MVT::i32:
725 case MVT::i64:
726 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
727 break;
728 case MVT::f32:
729 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
730 break;
731 case MVT::f64:
732 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
733 break;
734 }
735 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000736 //build the right kind of call
737 if (GlobalAddressSDNode *GASD =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000738 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
739 {
740 AlphaLowering.restoreGP(BB);
741 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal(),true);
742 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000743 else if (ExternalSymbolSDNode *ESSDN =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000744 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
745 {
746 AlphaLowering.restoreGP(BB);
747 BuildMI(BB, Alpha::CALL, 0).addExternalSymbol(ESSDN->getSymbol(), true);
748 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000749 else
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000750 {
751 //no need to restore GP as we are doing an indirect call
752 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth06342c32005-02-07 06:21:37 +0000753 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000754 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000755 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000756
757 //push the result into a virtual register
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000758
759 switch (Node->getValueType(0)) {
760 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000761 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000762 case MVT::i1:
763 case MVT::i8:
764 case MVT::i16:
765 case MVT::i32:
766 case MVT::i64:
767 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
768 break;
769 case MVT::f32:
770 case MVT::f64:
771 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
772 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000773 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000774 return Result+N.ResNo;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000775 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000776
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000777 case ISD::SIGN_EXTEND:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000778 abort();
779
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000780 case ISD::SIGN_EXTEND_INREG:
781 {
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000782 //Alpha has instructions for a bunch of signed 32 bit stuff
783 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
784 {
785 switch (N.getOperand(0).getOpcode()) {
786 case ISD::ADD:
787 case ISD::SUB:
788 case ISD::MUL:
789 {
790 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
791 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
792 //FIXME: first check for Scaled Adds and Subs!
793 if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
794 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
795 { //Normal imm add/sub
796 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
797 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
798 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
799 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
800 }
801 else
802 { //Normal add/sub
803 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULLi : Alpha::SUBL);
804 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
805 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
806 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
807 }
808 return Result;
809 }
810 default: break; //Fall Though;
811 }
812 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000813 Tmp1 = SelectExpr(N.getOperand(0));
814 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000815 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000816 switch(MVN->getExtraValueType())
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000817 {
818 default:
819 Node->dump();
820 assert(0 && "Sign Extend InReg not there yet");
821 break;
822 case MVT::i32:
823 {
824 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
825 break;
826 }
827 case MVT::i16:
828 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
829 break;
830 case MVT::i8:
831 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
832 break;
833 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000834 return Result;
835 }
836 case ISD::ZERO_EXTEND_INREG:
837 {
838 Tmp1 = SelectExpr(N.getOperand(0));
839 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000840 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000841 switch(MVN->getExtraValueType())
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000842 {
843 default:
844 Node->dump();
845 assert(0 && "Zero Extend InReg not there yet");
846 break;
847 case MVT::i32: Tmp2 = 0xf0; break;
848 case MVT::i16: Tmp2 = 0xfc; break;
849 case MVT::i8: Tmp2 = 0xfe; break;
850 case MVT::i1: //handle this one special
851 BuildMI(BB, Alpha::ANDi, 2, Result).addReg(Tmp1).addImm(1);
852 return Result;
853 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000854 BuildMI(BB, Alpha::ZAPi, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000855 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000856 }
857
858 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000859 {
860 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
861 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
862 bool isConst1 = false;
863 bool isConst2 = false;
864 int dir;
Andrew Lenharth9818c052005-02-05 13:19:12 +0000865
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000866 //Tmp1 = SelectExpr(N.getOperand(0));
867 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000868 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
869 isConst1 = true;
870 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000871 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
872 isConst2 = true;
873
874 switch (SetCC->getCondition()) {
875 default: Node->dump(); assert(0 && "Unknown integer comparison!");
876 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
877 case ISD::SETLT: Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
878 case ISD::SETLE: Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
879 case ISD::SETGT: Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
880 case ISD::SETGE: Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
881 case ISD::SETULT: Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
882 case ISD::SETUGT: Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
883 case ISD::SETULE: Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
884 case ISD::SETUGE: Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000885 case ISD::SETNE: {//Handle this one special
886 //std::cerr << "Alpha does not have a setne.\n";
887 //abort();
888 Tmp1 = SelectExpr(N.getOperand(0));
889 Tmp2 = SelectExpr(N.getOperand(1));
890 Tmp3 = MakeReg(MVT::i64);
891 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
892 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000893 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000894 return Result;
895 }
896 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000897 if (dir == 1) {
898 Tmp1 = SelectExpr(N.getOperand(0));
899 if (isConst2) {
900 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
901 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
902 } else {
903 Tmp2 = SelectExpr(N.getOperand(1));
904 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
905 }
906 } else if (dir == 2) {
907 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +0000908 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000909 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
910 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
911 } else {
912 Tmp2 = SelectExpr(N.getOperand(0));
913 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
914 }
915 } else { //dir == 0
916 if (isConst1) {
917 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
918 Tmp2 = SelectExpr(N.getOperand(1));
919 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
920 } else if (isConst2) {
921 Tmp1 = SelectExpr(N.getOperand(0));
922 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
923 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
924 } else {
925 Tmp1 = SelectExpr(N.getOperand(0));
926 Tmp2 = SelectExpr(N.getOperand(1));
927 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
928 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000929 }
930 } else {
931 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
932 bool rev = false;
933 bool inv = false;
934
935 switch (SetCC->getCondition()) {
936 default: Node->dump(); assert(0 && "Unknown FP comparison!");
937 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
938 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
939 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
940 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
941 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
942 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
943 }
944
945 Tmp1 = SelectExpr(N.getOperand(0));
946 Tmp2 = SelectExpr(N.getOperand(1));
947 //Can only compare doubles, and dag won't promote for me
948 if (SetCC->getOperand(0).getValueType() == MVT::f32)
949 {
950 Tmp3 = MakeReg(MVT::f64);
951 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
952 Tmp1 = Tmp3;
953 }
954 if (SetCC->getOperand(1).getValueType() == MVT::f32)
955 {
956 Tmp3 = MakeReg(MVT::f64);
957 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
958 Tmp1 = Tmp2;
959 }
Andrew Lenharth9818c052005-02-05 13:19:12 +0000960
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000961 if (rev) std::swap(Tmp1, Tmp2);
962 Tmp3 = MakeReg(MVT::f64);
963 //do the comparison
964 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
965
966 //now arrange for Result (int) to have a 1 or 0
967
968 // Spill the FP to memory and reload it from there.
969 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
970 MachineFunction *F = BB->getParent();
971 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
972 unsigned Tmp4 = MakeReg(MVT::f64);
973 BuildMI(BB, Alpha::CVTTQ, 1, Tmp4).addReg(Tmp3);
974 BuildMI(BB, Alpha::STT, 3).addReg(Tmp4).addFrameIndex(FrameIdx).addReg(Alpha::F31);
975 unsigned Tmp5 = MakeReg(MVT::i64);
976 BuildMI(BB, Alpha::LDQ, 2, Tmp5).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharth9818c052005-02-05 13:19:12 +0000977
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000978 //now, set result based on Tmp5
979 //Set Tmp6 if fp cmp was false
980 unsigned Tmp6 = MakeReg(MVT::i64);
981 BuildMI(BB, Alpha::CMPEQ, 2, Tmp6).addReg(Tmp5).addReg(Alpha::R31);
982 //and invert
983 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp6).addReg(Alpha::R31);
984
985 }
986 // else
987 // {
988 // Node->dump();
989 // assert(0 && "Not a setcc in setcc");
990 // }
Andrew Lenharth9818c052005-02-05 13:19:12 +0000991 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000992 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000993 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000994
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000995 case ISD::CopyFromReg:
996 {
Andrew Lenharth40831c52005-01-28 06:57:18 +0000997 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000998 if (Result != notIn)
999 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001000 else
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001001 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth40831c52005-01-28 06:57:18 +00001002
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001003 SDOperand Chain = N.getOperand(0);
1004
1005 Select(Chain);
1006 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1007 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1008 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1009 return Result;
1010 }
1011
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001012 //Most of the plain arithmetic and logic share the same form, and the same
1013 //constant immediate test
1014 case ISD::AND:
1015 case ISD::OR:
1016 case ISD::XOR:
1017 case ISD::SHL:
1018 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001019 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001020 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001021 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1022 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001023 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1024 {
1025 switch(opcode) {
1026 case ISD::AND: Opc = Alpha::ANDi; break;
1027 case ISD::OR: Opc = Alpha::BISi; break;
1028 case ISD::XOR: Opc = Alpha::XORi; break;
1029 case ISD::SHL: Opc = Alpha::SLi; break;
1030 case ISD::SRL: Opc = Alpha::SRLi; break;
1031 case ISD::SRA: Opc = Alpha::SRAi; break;
1032 case ISD::MUL: Opc = Alpha::MULQi; break;
1033 };
1034 Tmp1 = SelectExpr(N.getOperand(0));
1035 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1036 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1037 }
1038 else
1039 {
1040 switch(opcode) {
1041 case ISD::AND: Opc = Alpha::AND; break;
1042 case ISD::OR: Opc = Alpha::BIS; break;
1043 case ISD::XOR: Opc = Alpha::XOR; break;
1044 case ISD::SHL: Opc = Alpha::SL; break;
1045 case ISD::SRL: Opc = Alpha::SRL; break;
1046 case ISD::SRA: Opc = Alpha::SRA; break;
1047 case ISD::MUL: Opc = Alpha::MULQ; break;
1048 };
1049 Tmp1 = SelectExpr(N.getOperand(0));
1050 Tmp2 = SelectExpr(N.getOperand(1));
1051 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1052 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001053 return Result;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001054
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001055 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001056 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001057 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001058 bool isAdd = opcode == ISD::ADD;
1059
1060 //FIXME: first check for Scaled Adds and Subs!
1061 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001062 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1063 { //Normal imm add/sub
1064 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1065 Tmp1 = SelectExpr(N.getOperand(0));
1066 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1067 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1068 }
1069 else if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001070 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
1071 { //LDA //FIXME: expand the above condition a bit
1072 Tmp1 = SelectExpr(N.getOperand(0));
1073 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1074 if (!isAdd)
1075 Tmp2 = -Tmp2;
1076 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
1077 }
1078 else
1079 { //Normal add/sub
1080 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1081 Tmp1 = SelectExpr(N.getOperand(0));
1082 Tmp2 = SelectExpr(N.getOperand(1));
1083 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1084 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001085 return Result;
1086 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001087
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001088 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00001089 case ISD::SREM:
1090 case ISD::SDIV:
1091 case ISD::UDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001092 //FIXME: alpha really doesn't support any of these operations,
1093 // the ops are expanded into special library calls with
1094 // special calling conventions
1095 switch(opcode) {
1096 case ISD::UREM: Opc = Alpha::REMQU; break;
1097 case ISD::SREM: Opc = Alpha::REMQ; break;
1098 case ISD::UDIV: Opc = Alpha::DIVQU; break;
1099 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001100 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001101 Tmp1 = SelectExpr(N.getOperand(0));
1102 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth02981182005-01-26 01:24:38 +00001103 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001104 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001105
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001106 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001107 case ISD::FP_TO_SINT:
1108 {
1109 assert (DestType == MVT::i64 && "only quads can be loaded to");
1110 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001111 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001112 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1113
1114 //The hard way:
1115 // Spill the integer to memory and reload it from there.
1116 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1117 MachineFunction *F = BB->getParent();
1118 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
1119
1120 //CVTTQ STT LDQ
1121 //CVTST CVTTQ STT LDQ
1122 if (SrcType == MVT::f32)
1123 {
1124 Tmp2 = MakeReg(MVT::f64);
1125 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1126 Tmp1 = Tmp2;
1127 }
1128 Tmp2 = MakeReg(MVT::f64);
1129 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
1130 BuildMI(BB, Alpha::STT, 3).addReg(Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1131 BuildMI(BB, Alpha::LDQ, 2, Result).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1132
1133 return Result;
1134 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001135
1136// // case ISD::FP_TO_UINT:
1137
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001138 case ISD::SELECT:
1139 {
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001140 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001141 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1142 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001143 // Get the condition into the zero flag.
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001144 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001145 return Result;
1146 }
1147
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001148 case ISD::Constant:
1149 {
Andrew Lenharth22d5a412005-02-02 00:51:15 +00001150 unsigned long val = cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001151 if (val < 32000 && (long)val > -32000)
1152 BuildMI(BB, Alpha::LOAD_IMM, 1, Result).addImm(val);
1153 else
1154 {
1155 MachineConstantPool *CP = BB->getParent()->getConstantPool();
1156 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
1157 unsigned CPI = CP->getConstantPoolIndex(C);
1158 AlphaLowering.restoreGP(BB);
1159 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
1160 }
1161 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001162 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001163 }
1164
1165 return 0;
1166}
1167
1168void ISel::Select(SDOperand N) {
1169 unsigned Tmp1, Tmp2, Opc;
1170
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001171 // FIXME: Disable for our current expansion model!
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001172 if (/*!N->hasOneUse() &&*/ !ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001173 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001174
1175 SDNode *Node = N.Val;
1176
1177 switch (N.getOpcode()) {
1178
1179 default:
1180 Node->dump(); std::cerr << "\n";
1181 assert(0 && "Node not handled yet!");
1182
1183 case ISD::BRCOND: {
1184 MachineBasicBlock *Dest =
1185 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
1186
Andrew Lenharth9818c052005-02-05 13:19:12 +00001187 Select(N.getOperand(0)); //chain
1188
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001189 Tmp1 = SelectExpr(N.getOperand(1));
1190 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
1191 return;
1192 }
1193
1194 case ISD::BR: {
1195 MachineBasicBlock *Dest =
1196 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1197
1198 Select(N.getOperand(0));
1199 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
1200 return;
1201 }
1202
1203 case ISD::ImplicitDef:
1204 Select(N.getOperand(0));
1205 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
1206 return;
1207
1208 case ISD::EntryToken: return; // Noop
1209
1210 case ISD::TokenFactor:
1211 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1212 Select(Node->getOperand(i));
1213
1214 //N.Val->dump(); std::cerr << "\n";
1215 //assert(0 && "Node not handled yet!");
1216
1217 return;
1218
1219 case ISD::CopyToReg:
1220 Select(N.getOperand(0));
1221 Tmp1 = SelectExpr(N.getOperand(1));
1222 Tmp2 = cast<RegSDNode>(N)->getReg();
1223
1224 if (Tmp1 != Tmp2) {
1225 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1226 }
1227 return;
1228
1229 case ISD::RET:
1230 switch (N.getNumOperands()) {
1231 default:
1232 std::cerr << N.getNumOperands() << "\n";
1233 for (unsigned i = 0; i < N.getNumOperands(); ++i)
1234 std::cerr << N.getOperand(i).getValueType() << "\n";
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001235 Node->dump();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001236 assert(0 && "Unknown return instruction!");
1237 case 2:
1238 Select(N.getOperand(0));
1239 Tmp1 = SelectExpr(N.getOperand(1));
1240 switch (N.getOperand(1).getValueType()) {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001241 default: Node->dump(); assert(0 && "All other types should have been promoted!!");
1242 case MVT::f64:
1243 case MVT::f32:
1244 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1245 break;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001246 case MVT::i32:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001247 case MVT::i64:
1248 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1249 break;
1250 }
1251 break;
1252 case 1:
1253 Select(N.getOperand(0));
1254 break;
1255 }
1256 //Tmp2 = AlphaLowering.getRetAddr();
1257 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
1258 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
1259 return;
1260
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001261 case ISD::TRUNCSTORE:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001262 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001263 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001264 SDOperand Chain = N.getOperand(0);
1265 SDOperand Value = N.getOperand(1);
1266 SDOperand Address = N.getOperand(2);
1267 Select(Chain);
1268
1269 Tmp1 = SelectExpr(Value); //value
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001270 switch(Value.getValueType()) {
Andrew Lenharth65838902005-02-06 16:22:15 +00001271 default: assert(0 && "unknown Type in store");
1272 case MVT::i64: Opc = Alpha::STQ; break;
1273 case MVT::f64: Opc = Alpha::STT; break;
1274 case MVT::f32: Opc = Alpha::STS; break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001275 case MVT::i1: //FIXME: DAG does not promote this load
1276 case MVT::i8: Opc = Alpha::STB; break;
1277 case MVT::i16: Opc = Alpha::STW; break;
1278 case MVT::i32: Opc = Alpha::STL; break;
Andrew Lenharth65838902005-02-06 16:22:15 +00001279 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001280 if (Address.getOpcode() == ISD::GlobalAddress)
1281 {
1282 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001283 Opc = GetSymVersion(Opc);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001284 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1285 }
Andrew Lenharth05380342005-02-07 05:07:00 +00001286 else if(Address.getOpcode() == ISD::FrameIndex)
1287 {
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001288 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1289 BuildMI(BB, Opc, 3).addReg(Tmp1).addFrameIndex(Tmp2).addReg(Alpha::F31);
Andrew Lenharth05380342005-02-07 05:07:00 +00001290 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001291 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001292 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001293 long offset;
1294 SelectAddr(Address, Tmp2, offset);
1295 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1296 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001297 return;
1298 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001299
1300 case ISD::EXTLOAD:
1301 case ISD::SEXTLOAD:
1302 case ISD::ZEXTLOAD:
1303 case ISD::LOAD:
1304 case ISD::CopyFromReg:
1305 case ISD::CALL:
1306// case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001307 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001308 SelectExpr(N);
1309 return;
1310
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001311 case ISD::ADJCALLSTACKDOWN:
1312 case ISD::ADJCALLSTACKUP:
1313 Select(N.getOperand(0));
1314 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1315
1316 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? Alpha::ADJUSTSTACKDOWN :
1317 Alpha::ADJUSTSTACKUP;
1318 BuildMI(BB, Opc, 1).addImm(Tmp1);
1319 return;
1320 }
1321 assert(0 && "Should not be reached!");
1322}
1323
1324
1325/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1326/// into a machine code representation using pattern matching and a machine
1327/// description file.
1328///
1329FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
1330 return new ISel(TM);
1331}