blob: 034beed576875a11395514346952c34fe1930c28 [file] [log] [blame]
Misha Brukman2a8350a2005-02-05 02:24:26 +00001//===- AlphaISelPattern.cpp - A pattern matching inst selector for Alpha --===//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for Alpha.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Alpha.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000015#include "AlphaRegisterInfo.h"
16#include "llvm/Constants.h" // FIXME: REMOVE
17#include "llvm/Function.h"
18#include "llvm/CodeGen/MachineInstrBuilder.h"
19#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/CodeGen/SSARegMap.h"
25#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetLowering.h"
27#include "llvm/Support/MathExtras.h"
28#include "llvm/ADT/Statistic.h"
29#include <set>
Andrew Lenharth684f2292005-01-30 00:35:27 +000030#include <algorithm>
Andrew Lenharth304d0f32005-01-22 23:41:55 +000031using namespace llvm;
32
33//===----------------------------------------------------------------------===//
34// AlphaTargetLowering - Alpha Implementation of the TargetLowering interface
35namespace {
36 class AlphaTargetLowering : public TargetLowering {
37 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
38 unsigned GP; //GOT vreg
39 public:
40 AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
41 // Set up the TargetLowering object.
Andrew Lenharth3d65d312005-01-27 03:49:45 +000042 //I am having problems with shr n ubyte 1
Andrew Lenharth879ef222005-02-02 17:00:21 +000043 setShiftAmountType(MVT::i64);
44 setSetCCResultType(MVT::i64);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000045
Andrew Lenharth304d0f32005-01-22 23:41:55 +000046 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
47 addRegisterClass(MVT::f64, Alpha::FPRCRegisterClass);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000048 addRegisterClass(MVT::f32, Alpha::FPRCRegisterClass);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000049
Andrew Lenharthd2bb9602005-01-27 07:50:35 +000050 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000051
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +000052 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000053 setOperationAction(ISD::ZEXTLOAD , MVT::i32 , Expand);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000054
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +000055 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000056 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
57 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
58
Andrew Lenharth3d65d312005-01-27 03:49:45 +000059 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); //what is the sign expansion of 1? 1 or -1?
Andrew Lenharth02981182005-01-26 01:24:38 +000060
Andrew Lenharth9818c052005-02-05 13:19:12 +000061 setOperationAction(ISD::SREM , MVT::f32 , Expand);
62 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +000063
Andrew Lenharth8d163d22005-02-02 05:49:42 +000064 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
Andrew Lenharth9818c052005-02-05 13:19:12 +000065 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
66 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
67
Andrew Lenharth3d65d312005-01-27 03:49:45 +000068 computeRegisterProperties();
Andrew Lenharth304d0f32005-01-22 23:41:55 +000069
Andrew Lenharthd2bb9602005-01-27 07:50:35 +000070 addLegalFPImmediate(+0.0); //F31
Andrew Lenharth12dd2622005-02-03 21:01:15 +000071 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +000072 }
73
74 /// LowerArguments - This hook must be implemented to indicate how we should
75 /// lower the arguments for the specified function, into the specified DAG.
76 virtual std::vector<SDOperand>
77 LowerArguments(Function &F, SelectionDAG &DAG);
78
79 /// LowerCallTo - This hook lowers an abstract call to a function into an
80 /// actual call.
81 virtual std::pair<SDOperand, SDOperand>
82 LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
83 ArgListTy &Args, SelectionDAG &DAG);
84
85 virtual std::pair<SDOperand, SDOperand>
86 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
87
88 virtual std::pair<SDOperand,SDOperand>
89 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
90 const Type *ArgTy, SelectionDAG &DAG);
91
92 virtual std::pair<SDOperand, SDOperand>
93 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
94 SelectionDAG &DAG);
95
96 void restoreGP(MachineBasicBlock* BB)
97 {
98 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
99 }
100 };
101}
102
103//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
104
105//For now, just use variable size stack frame format
106
107//In a standard call, the first six items are passed in registers $16
108//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
109//of argument-to-register correspondence.) The remaining items are
110//collected in a memory argument list that is a naturally aligned
111//array of quadwords. In a standard call, this list, if present, must
112//be passed at 0(SP).
113//7 ... n 0(SP) ... (n-7)*8(SP)
114
115std::vector<SDOperand>
116AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
117{
118 std::vector<SDOperand> ArgValues;
119
120 // //#define FP $15
121 // //#define RA $26
122 // //#define PV $27
123 // //#define GP $29
124 // //#define SP $30
125
126 // assert(0 && "TODO");
127 MachineFunction &MF = DAG.getMachineFunction();
128 MachineFrameInfo *MFI = MF.getFrameInfo();
129
130 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
131 MachineBasicBlock& BB = MF.front();
132
133 //Handle the return address
134 //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
135
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000136 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
137 Alpha::R19, Alpha::R20, Alpha::R21};
138 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
139 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000140 std::vector<unsigned> argVreg;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000141 std::vector<unsigned> argPreg;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000142 std::vector<unsigned> argOpc;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000143 int count = 0;
144 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I)
145 {
Andrew Lenharth40831c52005-01-28 06:57:18 +0000146 SDOperand newroot, argt;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000147 if (count < 6) {
148 switch (getValueType(I->getType())) {
149 default: std::cerr << "Unknown Type " << getValueType(I->getType()) << "\n"; abort();
150 case MVT::f64:
151 case MVT::f32:
152 BuildMI(&BB, Alpha::IDEF, 0, args_float[count]);
153 argVreg.push_back(MF.getSSARegMap()->createVirtualRegister(getRegClassFor(getValueType(I->getType()))));
154 argPreg.push_back(args_float[count]);
155 argOpc.push_back(Alpha::CPYS);
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000156 argt = newroot = DAG.getCopyFromReg(argVreg[count], getValueType(I->getType()), DAG.getRoot());
Andrew Lenharth684f2292005-01-30 00:35:27 +0000157 break;
158 case MVT::i1:
159 case MVT::i8:
160 case MVT::i16:
161 case MVT::i32:
162 case MVT::i64:
163 BuildMI(&BB, Alpha::IDEF, 0, args_int[count]);
164 argVreg.push_back(MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64)));
165 argPreg.push_back(args_int[count]);
166 argOpc.push_back(Alpha::BIS);
167 argt = newroot = DAG.getCopyFromReg(argVreg[count], MVT::i64, DAG.getRoot());
168 if (getValueType(I->getType()) != MVT::i64)
169 argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()), newroot);
170 break;
171 }
172 } else { //more args
173 // Create the frame index object for this incoming parameter...
174 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
175
176 // Create the SelectionDAG nodes corresponding to a load from this parameter
177 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
178 argt = newroot = DAG.getLoad(getValueType(I->getType()), DAG.getEntryNode(), FIN);
Andrew Lenharth40831c52005-01-28 06:57:18 +0000179 }
180 DAG.setRoot(newroot.getValue(1));
181 ArgValues.push_back(argt);
Andrew Lenharth684f2292005-01-30 00:35:27 +0000182 ++count;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000183 }
184
185 BuildMI(&BB, Alpha::IDEF, 0, Alpha::R29);
186 BuildMI(&BB, Alpha::BIS, 2, GP).addReg(Alpha::R29).addReg(Alpha::R29);
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000187 for (int i = 0; i < std::min(count,6); ++i)
Andrew Lenharth40831c52005-01-28 06:57:18 +0000188 BuildMI(&BB, argOpc[i], 2, argVreg[i]).addReg(argPreg[i]).addReg(argPreg[i]);
189
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000190 return ArgValues;
191}
192
193std::pair<SDOperand, SDOperand>
194AlphaTargetLowering::LowerCallTo(SDOperand Chain,
195 const Type *RetTy, SDOperand Callee,
196 ArgListTy &Args, SelectionDAG &DAG) {
197 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000198 if (Args.size() > 6)
199 NumBytes = (Args.size() - 6) * 8;
200
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000201 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
202 DAG.getConstant(NumBytes, getPointerTy()));
203 std::vector<SDOperand> args_to_use;
204 for (unsigned i = 0, e = Args.size(); i != e; ++i)
205 {
206 switch (getValueType(Args[i].second)) {
207 default: assert(0 && "Unexpected ValueType for argument!");
208 case MVT::i1:
209 case MVT::i8:
210 case MVT::i16:
211 case MVT::i32:
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000212 // Promote the integer to 64 bits. If the input type is signed use a
213 // sign extend, otherwise use a zero extend.
214 if (Args[i].second->isSigned())
Andrew Lenharth40831c52005-01-28 06:57:18 +0000215 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000216 else
Andrew Lenharth40831c52005-01-28 06:57:18 +0000217 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000218 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000219 case MVT::i64:
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000220 case MVT::f64:
221 case MVT::f32:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000222 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000223 }
224 args_to_use.push_back(Args[i].first);
225 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000226
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000227 std::vector<MVT::ValueType> RetVals;
228 MVT::ValueType RetTyVT = getValueType(RetTy);
229 if (RetTyVT != MVT::isVoid)
230 RetVals.push_back(RetTyVT);
231 RetVals.push_back(MVT::Other);
232
233 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee, args_to_use), 0);
234 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
235 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
236 DAG.getConstant(NumBytes, getPointerTy()));
237 return std::make_pair(TheCall, Chain);
238}
239
240std::pair<SDOperand, SDOperand>
241AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
242 //vastart just returns the address of the VarArgsFrameIndex slot.
243 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
244}
245
246std::pair<SDOperand,SDOperand> AlphaTargetLowering::
247LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
248 const Type *ArgTy, SelectionDAG &DAG) {
249 abort();
250}
251
252
253std::pair<SDOperand, SDOperand> AlphaTargetLowering::
254LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
255 SelectionDAG &DAG) {
256 abort();
257}
258
259
260
261
262
263namespace {
264
265 //===--------------------------------------------------------------------===//
266 /// ISel - Alpha specific code to select Alpha machine instructions for
267 /// SelectionDAG operations.
268 ///
269 class ISel : public SelectionDAGISel {
270
271 /// AlphaLowering - This object fully describes how to lower LLVM code to an
272 /// Alpha-specific SelectionDAG.
273 AlphaTargetLowering AlphaLowering;
274
275
276 /// ExprMap - As shared expressions are codegen'd, we keep track of which
277 /// vreg the value is produced in, so we only emit one copy of each compiled
278 /// tree.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000279 static const unsigned notIn = (unsigned)(-1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000280 std::map<SDOperand, unsigned> ExprMap;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000281
282 public:
283 ISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM) {
284 }
285
286 /// InstructionSelectBasicBlock - This callback is invoked by
287 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
288 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
289 // Codegen the basic block.
290 Select(DAG.getRoot());
291
292 // Clear state used for selection.
293 ExprMap.clear();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000294 }
295
296 unsigned SelectExpr(SDOperand N);
Andrew Lenharth40831c52005-01-28 06:57:18 +0000297 unsigned SelectExprFP(SDOperand N, unsigned Result);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000298 void Select(SDOperand N);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000299
300 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000301 };
302}
303
Andrew Lenharth65838902005-02-06 16:22:15 +0000304static unsigned GetSymVersion(unsigned opcode)
305{
306 switch (opcode) {
307 default: assert(0 && "unknown load or store"); return 0;
308 case Alpha::LDQ: return Alpha::LDQ_SYM;
309 case Alpha::LDS: return Alpha::LDS_SYM;
310 case Alpha::LDT: return Alpha::LDT_SYM;
311 case Alpha::LDL: return Alpha::LDL_SYM;
312 case Alpha::LDBU: return Alpha::LDBU_SYM;
313 case Alpha::LDWU: return Alpha::LDWU_SYM;
314 case Alpha::LDW: return Alpha::LDW_SYM;
315 case Alpha::LDB: return Alpha::LDB_SYM;
316 case Alpha::STQ: return Alpha::STQ_SYM;
317 case Alpha::STS: return Alpha::STS_SYM;
318 case Alpha::STT: return Alpha::STT_SYM;
319 case Alpha::STL: return Alpha::STL_SYM;
320 case Alpha::STW: return Alpha::STW_SYM;
321 case Alpha::STB: return Alpha::STB_SYM;
322 }
323}
324
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000325//Check to see if the load is a constant offset from a base register
326void ISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
327{
328 unsigned opcode = N.getOpcode();
329 if (opcode == ISD::ADD) {
330 if(N.getOperand(1).getOpcode() == ISD::Constant && cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
331 { //Normal imm add
332 Reg = SelectExpr(N.getOperand(0));
333 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
334 return;
335 }
336 else if(N.getOperand(0).getOpcode() == ISD::Constant && cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 32767)
337 {
338 Reg = SelectExpr(N.getOperand(1));
339 offset = cast<ConstantSDNode>(N.getOperand(0))->getValue();
340 return;
341 }
342 }
343 Reg = SelectExpr(N);
344 offset = 0;
345 return;
346}
347
Andrew Lenharth40831c52005-01-28 06:57:18 +0000348unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
349{
350 unsigned Tmp1, Tmp2, Tmp3;
351 unsigned Opc = 0;
352 SDNode *Node = N.Val;
353 MVT::ValueType DestType = N.getValueType();
354 unsigned opcode = N.getOpcode();
355
356 switch (opcode) {
357 default:
358 Node->dump();
359 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +0000360
Andrew Lenharth9818c052005-02-05 13:19:12 +0000361 case ISD::SELECT:
362 {
363 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
364 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
365 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000366
367
368 // Spill the cond to memory and reload it from there.
369 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
370 MachineFunction *F = BB->getParent();
371 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
372 unsigned Tmp4 = MakeReg(MVT::f64);
373 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addFrameIndex(FrameIdx).addReg(Alpha::F31);
374 BuildMI(BB, Alpha::LDT, 2, Tmp4).addFrameIndex(FrameIdx).addReg(Alpha::F31);
375 //now ideally, we don't have to do anything to the flag...
Andrew Lenharth9818c052005-02-05 13:19:12 +0000376 // Get the condition into the zero flag.
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000377 BuildMI(BB, Alpha::FCMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp4);
Andrew Lenharth9818c052005-02-05 13:19:12 +0000378 return Result;
379 }
380
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000381 case ISD::FP_ROUND:
382 assert (DestType == MVT::f32 && N.getOperand(0).getValueType() == MVT::f64 && "only f64 to f32 conversion supported here");
383 Tmp1 = SelectExpr(N.getOperand(0));
384 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
385 return Result;
386
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000387 case ISD::FP_EXTEND:
388 assert (DestType == MVT::f64 && N.getOperand(0).getValueType() == MVT::f32 && "only f32 to f64 conversion supported here");
389 Tmp1 = SelectExpr(N.getOperand(0));
390 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
391 return Result;
392
Andrew Lenharth2c594352005-01-29 15:42:07 +0000393 case ISD::CopyFromReg:
394 {
395 // Make sure we generate both values.
396 if (Result != notIn)
397 ExprMap[N.getValue(1)] = notIn; // Generate the token
398 else
399 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
400
401 SDOperand Chain = N.getOperand(0);
402
403 Select(Chain);
404 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
405 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
406 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
407 return Result;
408 }
409
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000410 case ISD::LOAD:
411 {
412 // Make sure we generate both values.
413 if (Result != notIn)
414 ExprMap[N.getValue(1)] = notIn; // Generate the token
415 else
416 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000417
418 //DestType = N.getValue(0).getValueType();
419
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000420 SDOperand Chain = N.getOperand(0);
421 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000422 Select(Chain);
Andrew Lenharth65838902005-02-06 16:22:15 +0000423 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
424
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000425 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000426 {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000427 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000428 Opc = GetSymVersion(Opc);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000429 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
430 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000431 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000432 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000433 Opc = GetSymVersion(Opc);
Andrew Lenharth97127a12005-02-05 17:41:39 +0000434 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000435 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000436 else
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000437 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000438 long offset;
439 SelectAddr(Address, Tmp1, offset);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000440 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000441 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000442 return Result;
443 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000444 case ISD::ConstantFP:
445 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
446 if (CN->isExactlyValue(+0.0)) {
447 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000448 } else if ( CN->isExactlyValue(-0.0)) {
449 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +0000450 } else {
451 abort();
452 }
453 }
454 return Result;
455
456 case ISD::MUL:
457 case ISD::ADD:
458 case ISD::SUB:
459 case ISD::SDIV:
460 switch( opcode ) {
461 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
462 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
463 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
464 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
465 };
466 Tmp1 = SelectExpr(N.getOperand(0));
467 Tmp2 = SelectExpr(N.getOperand(1));
468 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
469 return Result;
470
Andrew Lenharth2c594352005-01-29 15:42:07 +0000471 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000472 {
473 //include a conversion sequence for float loads to double
474 if (Result != notIn)
475 ExprMap[N.getValue(1)] = notIn; // Generate the token
476 else
477 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
478
479 Tmp2 = MakeReg(MVT::f32);
480
481 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 && "EXTLOAD not from f32");
482 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
483
484 SDOperand Chain = N.getOperand(0);
485 SDOperand Address = N.getOperand(1);
486 Select(Chain);
487
488 if (Address.getOpcode() == ISD::GlobalAddress)
489 {
490 AlphaLowering.restoreGP(BB);
491 BuildMI(BB, Alpha::LDS_SYM, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
492 }
493 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
494 {
495 AlphaLowering.restoreGP(BB);
496 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp2).addConstantPoolIndex(CP->getIndex());
497 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp2);
498 }
499 else
500 {
501 long offset;
502 SelectAddr(Address, Tmp1, offset);
503 BuildMI(BB, Alpha::LDS, 1, Tmp2).addImm(offset).addReg(Tmp1);
504 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp2);
505 }
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000506 return Result;
507 }
Andrew Lenharth2c594352005-01-29 15:42:07 +0000508
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000509 case ISD::UINT_TO_FP:
510 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000511 {
512 assert (N.getOperand(0).getValueType() == MVT::i64 && "only quads can be loaded from");
513 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +0000514 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000515
516 //The hard way:
517 // Spill the integer to memory and reload it from there.
518 unsigned Size = MVT::getSizeInBits(MVT::i64)/8;
519 MachineFunction *F = BB->getParent();
520 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
521
Andrew Lenharth7efadce2005-01-31 01:44:26 +0000522 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addFrameIndex(FrameIdx).addReg(Alpha::F31);
523 BuildMI(BB, Alpha::LDT, 2, Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
524 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
525 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000526
527 //The easy way: doesn't work
528// //so these instructions are not supported on ev56
529// Opc = DestType == MVT::f64 ? Alpha::ITOFT : Alpha::ITOFS;
530// BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
531// Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
532// BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
533
Andrew Lenharth40831c52005-01-28 06:57:18 +0000534 return Result;
535 }
536 }
537 assert(0 && "should not get here");
538 return 0;
539}
540
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000541unsigned ISel::SelectExpr(SDOperand N) {
542 unsigned Result;
543 unsigned Tmp1, Tmp2, Tmp3;
544 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000545 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000546
547 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000548 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000549
550 unsigned &Reg = ExprMap[N];
551 if (Reg) return Reg;
552
553 if (N.getOpcode() != ISD::CALL)
554 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000555 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000556 else {
557 // If this is a call instruction, make sure to prepare ALL of the result
558 // values as well as the chain.
559 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000560 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000561 else {
562 Result = MakeReg(Node->getValueType(0));
563 ExprMap[N.getValue(0)] = Result;
564 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
565 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000566 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000567 }
568 }
569
Andrew Lenharth22088bb2005-02-02 15:05:33 +0000570 if (DestType == MVT::f64 || DestType == MVT::f32 ||
571 (opcode == ISD::LOAD &&
572 (N.getValue(0).getValueType() == MVT::f32 || N.getValue(0).getValueType() == MVT::f64)))
Andrew Lenharth40831c52005-01-28 06:57:18 +0000573 return SelectExprFP(N, Result);
574
575 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000576 default:
577 Node->dump();
578 assert(0 && "Node not handled!\n");
579
Andrew Lenharth2c594352005-01-29 15:42:07 +0000580 case ISD::ConstantPool:
581 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
582 AlphaLowering.restoreGP(BB);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000583 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000584 return Result;
585
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000586 case ISD::FrameIndex:
587 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Andrew Lenharth684f2292005-01-30 00:35:27 +0000588 BuildMI(BB, Alpha::LDA, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000589 return Result;
590
591 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000592 {
593 // Make sure we generate both values.
594 if (Result != notIn)
595 ExprMap[N.getValue(1)] = notIn; // Generate the token
596 else
597 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
598
599 SDOperand Chain = N.getOperand(0);
600 SDOperand Address = N.getOperand(1);
601 Select(Chain);
602
603 switch(Node->getValueType(0)) {
604 default: Node->dump(); assert(0 && "Unknown type to sign extend to.");
Andrew Lenharthd279b412005-01-25 19:58:40 +0000605 case MVT::i64:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000606 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
607 default:
608 Node->dump();
609 assert(0 && "Bad extend load!");
610 case MVT::i64: Opc = Alpha::LDQ; break;
611 case MVT::i32: Opc = Alpha::LDL; break;
612 case MVT::i16: Opc = Alpha::LDWU; break;
613 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
614 case MVT::i8: Opc = Alpha::LDBU; break;
615 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000616 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000617
618 if (Address.getOpcode() == ISD::GlobalAddress)
619 {
620 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000621 Opc = GetSymVersion(Opc);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000622 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
623 }
624 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address))
625 {
626 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000627 Opc = GetSymVersion(Opc);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000628 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
629 }
630 else
631 {
632 long offset;
633 SelectAddr(Address, Tmp1, offset);
634 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
635 }
636 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000637 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000638
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000639 case ISD::SEXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000640 {
641 // Make sure we generate both values.
642 if (Result != notIn)
643 ExprMap[N.getValue(1)] = notIn; // Generate the token
644 else
645 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000646
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000647 SDOperand Chain = N.getOperand(0);
648 SDOperand Address = N.getOperand(1);
649 Select(Chain);
650
651 switch(Node->getValueType(0)) {
652 default: Node->dump(); assert(0 && "Unknown type to sign extend to.");
653 case MVT::i64:
654 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
655 default: Node->dump(); assert(0 && "Bad sign extend!");
656 case MVT::i32: Opc = Alpha::LDL; break;
657 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000658 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000659
660 if (Address.getOpcode() == ISD::GlobalAddress)
661 {
662 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000663 Opc = GetSymVersion(Opc);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000664 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
665 }
666 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
667 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000668 Opc = GetSymVersion(Opc);
669 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000670 }
671 else
672 {
673 long offset;
674 SelectAddr(Address, Tmp1, offset);
675 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
676 }
677 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000678 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000679
680 case ISD::ZEXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000681 {
682 // Make sure we generate both values.
683 if (Result != notIn)
684 ExprMap[N.getValue(1)] = notIn; // Generate the token
685 else
686 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
687
688 SDOperand Chain = N.getOperand(0);
689 SDOperand Address = N.getOperand(1);
690 Select(Chain);
691
692 switch(Node->getValueType(0)) {
693 default: Node->dump(); assert(0 && "Unknown type to zero extend to.");
694 case MVT::i64:
695 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
696 default: Node->dump(); assert(0 && "Bad sign extend!");
697 case MVT::i16: Opc = Alpha::LDWU; break;
698 case MVT::i8: Opc = Alpha::LDBU; break;
699 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000700 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000701
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000702 if (Address.getOpcode() == ISD::GlobalAddress)
703 {
704 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000705 Opc = GetSymVersion(Opc);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000706 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
707 }
708 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
709 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000710 Opc = GetSymVersion(Opc);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000711 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
712 }
713 else
714 {
715 long offset;
716 SelectAddr(Address, Tmp1, offset);
717 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
718 }
719 return Result;
720 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000721
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000722 case ISD::GlobalAddress:
723 AlphaLowering.restoreGP(BB);
724 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
725 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
726 return Result;
727
728 case ISD::CALL:
729 {
730 Select(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000731
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000732 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000733 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000734
735 //grab the arguments
736 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000737 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000738 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000739 argvregs.push_back(SelectExpr(N.getOperand(i)));
740
Andrew Lenharth684f2292005-01-30 00:35:27 +0000741 //in reg args
742 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
743 {
744 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
745 Alpha::R19, Alpha::R20, Alpha::R21};
746 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
747 Alpha::F19, Alpha::F20, Alpha::F21};
748 switch(N.getOperand(i+2).getValueType()) {
749 default:
750 Node->dump();
751 N.getOperand(i).Val->dump();
752 std::cerr << "Type for " << i << " is: " << N.getOperand(i+2).getValueType() << "\n";
753 assert(0 && "Unknown value type for call");
754 case MVT::i1:
755 case MVT::i8:
756 case MVT::i16:
757 case MVT::i32:
758 case MVT::i64:
759 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
760 break;
761 case MVT::f32:
762 case MVT::f64:
763 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
764 break;
765 }
766 }
767 //in mem args
768 for (int i = 6, e = argvregs.size(); i < e; ++i)
769 {
770 switch(N.getOperand(i+2).getValueType()) {
771 default:
772 Node->dump();
773 N.getOperand(i).Val->dump();
774 std::cerr << "Type for " << i << " is: " << N.getOperand(i+2).getValueType() << "\n";
775 assert(0 && "Unknown value type for call");
776 case MVT::i1:
777 case MVT::i8:
778 case MVT::i16:
779 case MVT::i32:
780 case MVT::i64:
781 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
782 break;
783 case MVT::f32:
784 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
785 break;
786 case MVT::f64:
787 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
788 break;
789 }
790 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000791 //build the right kind of call
792 if (GlobalAddressSDNode *GASD =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000793 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
794 {
795 AlphaLowering.restoreGP(BB);
796 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal(),true);
797 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000798 else if (ExternalSymbolSDNode *ESSDN =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000799 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
800 {
801 AlphaLowering.restoreGP(BB);
802 BuildMI(BB, Alpha::CALL, 0).addExternalSymbol(ESSDN->getSymbol(), true);
803 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000804 else
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000805 {
806 //no need to restore GP as we are doing an indirect call
807 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000808 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
809 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000810 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000811
812 //push the result into a virtual register
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000813
814 switch (Node->getValueType(0)) {
815 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000816 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000817 case MVT::i1:
818 case MVT::i8:
819 case MVT::i16:
820 case MVT::i32:
821 case MVT::i64:
822 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
823 break;
824 case MVT::f32:
825 case MVT::f64:
826 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
827 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000828 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000829 return Result+N.ResNo;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000830 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000831
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000832 case ISD::SIGN_EXTEND:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000833 abort();
834
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000835 case ISD::SIGN_EXTEND_INREG:
836 {
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000837 //Alpha has instructions for a bunch of signed 32 bit stuff
838 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
839 {
840 switch (N.getOperand(0).getOpcode()) {
841 case ISD::ADD:
842 case ISD::SUB:
843 case ISD::MUL:
844 {
845 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
846 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
847 //FIXME: first check for Scaled Adds and Subs!
848 if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
849 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
850 { //Normal imm add/sub
851 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
852 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
853 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
854 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
855 }
856 else
857 { //Normal add/sub
858 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULLi : Alpha::SUBL);
859 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
860 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
861 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
862 }
863 return Result;
864 }
865 default: break; //Fall Though;
866 }
867 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000868 Tmp1 = SelectExpr(N.getOperand(0));
869 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000870 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000871 switch(MVN->getExtraValueType())
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000872 {
873 default:
874 Node->dump();
875 assert(0 && "Sign Extend InReg not there yet");
876 break;
877 case MVT::i32:
878 {
879 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
880 break;
881 }
882 case MVT::i16:
883 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
884 break;
885 case MVT::i8:
886 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
887 break;
888 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000889 return Result;
890 }
891 case ISD::ZERO_EXTEND_INREG:
892 {
893 Tmp1 = SelectExpr(N.getOperand(0));
894 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000895 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000896 switch(MVN->getExtraValueType())
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000897 {
898 default:
899 Node->dump();
900 assert(0 && "Zero Extend InReg not there yet");
901 break;
902 case MVT::i32: Tmp2 = 0xf0; break;
903 case MVT::i16: Tmp2 = 0xfc; break;
904 case MVT::i8: Tmp2 = 0xfe; break;
905 case MVT::i1: //handle this one special
906 BuildMI(BB, Alpha::ANDi, 2, Result).addReg(Tmp1).addImm(1);
907 return Result;
908 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000909 BuildMI(BB, Alpha::ZAPi, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000910 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000911 }
912
913 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000914 {
915 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
916 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
917 bool isConst1 = false;
918 bool isConst2 = false;
919 int dir;
Andrew Lenharth9818c052005-02-05 13:19:12 +0000920
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000921 //Tmp1 = SelectExpr(N.getOperand(0));
922 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000923 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
924 isConst1 = true;
925 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000926 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
927 isConst2 = true;
928
929 switch (SetCC->getCondition()) {
930 default: Node->dump(); assert(0 && "Unknown integer comparison!");
931 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
932 case ISD::SETLT: Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
933 case ISD::SETLE: Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
934 case ISD::SETGT: Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
935 case ISD::SETGE: Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
936 case ISD::SETULT: Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
937 case ISD::SETUGT: Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
938 case ISD::SETULE: Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
939 case ISD::SETUGE: Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000940 case ISD::SETNE: {//Handle this one special
941 //std::cerr << "Alpha does not have a setne.\n";
942 //abort();
943 Tmp1 = SelectExpr(N.getOperand(0));
944 Tmp2 = SelectExpr(N.getOperand(1));
945 Tmp3 = MakeReg(MVT::i64);
946 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
947 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000948 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000949 return Result;
950 }
951 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000952 if (dir == 1) {
953 Tmp1 = SelectExpr(N.getOperand(0));
954 if (isConst2) {
955 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
956 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
957 } else {
958 Tmp2 = SelectExpr(N.getOperand(1));
959 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
960 }
961 } else if (dir == 2) {
962 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +0000963 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000964 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
965 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
966 } else {
967 Tmp2 = SelectExpr(N.getOperand(0));
968 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
969 }
970 } else { //dir == 0
971 if (isConst1) {
972 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
973 Tmp2 = SelectExpr(N.getOperand(1));
974 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
975 } else if (isConst2) {
976 Tmp1 = SelectExpr(N.getOperand(0));
977 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
978 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
979 } else {
980 Tmp1 = SelectExpr(N.getOperand(0));
981 Tmp2 = SelectExpr(N.getOperand(1));
982 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
983 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000984 }
985 } else {
986 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
987 bool rev = false;
988 bool inv = false;
989
990 switch (SetCC->getCondition()) {
991 default: Node->dump(); assert(0 && "Unknown FP comparison!");
992 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
993 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
994 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
995 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
996 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
997 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
998 }
999
1000 Tmp1 = SelectExpr(N.getOperand(0));
1001 Tmp2 = SelectExpr(N.getOperand(1));
1002 //Can only compare doubles, and dag won't promote for me
1003 if (SetCC->getOperand(0).getValueType() == MVT::f32)
1004 {
1005 Tmp3 = MakeReg(MVT::f64);
1006 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
1007 Tmp1 = Tmp3;
1008 }
1009 if (SetCC->getOperand(1).getValueType() == MVT::f32)
1010 {
1011 Tmp3 = MakeReg(MVT::f64);
1012 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
1013 Tmp1 = Tmp2;
1014 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001015
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001016 if (rev) std::swap(Tmp1, Tmp2);
1017 Tmp3 = MakeReg(MVT::f64);
1018 //do the comparison
1019 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1020
1021 //now arrange for Result (int) to have a 1 or 0
1022
1023 // Spill the FP to memory and reload it from there.
1024 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1025 MachineFunction *F = BB->getParent();
1026 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
1027 unsigned Tmp4 = MakeReg(MVT::f64);
1028 BuildMI(BB, Alpha::CVTTQ, 1, Tmp4).addReg(Tmp3);
1029 BuildMI(BB, Alpha::STT, 3).addReg(Tmp4).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1030 unsigned Tmp5 = MakeReg(MVT::i64);
1031 BuildMI(BB, Alpha::LDQ, 2, Tmp5).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharth9818c052005-02-05 13:19:12 +00001032
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001033 //now, set result based on Tmp5
1034 //Set Tmp6 if fp cmp was false
1035 unsigned Tmp6 = MakeReg(MVT::i64);
1036 BuildMI(BB, Alpha::CMPEQ, 2, Tmp6).addReg(Tmp5).addReg(Alpha::R31);
1037 //and invert
1038 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp6).addReg(Alpha::R31);
1039
1040 }
1041 // else
1042 // {
1043 // Node->dump();
1044 // assert(0 && "Not a setcc in setcc");
1045 // }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001046 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001047 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001048 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001049
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001050 case ISD::CopyFromReg:
1051 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001052 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001053 if (Result != notIn)
1054 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001055 else
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001056 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth40831c52005-01-28 06:57:18 +00001057
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001058 SDOperand Chain = N.getOperand(0);
1059
1060 Select(Chain);
1061 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1062 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1063 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1064 return Result;
1065 }
1066
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001067 //Most of the plain arithmetic and logic share the same form, and the same
1068 //constant immediate test
1069 case ISD::AND:
1070 case ISD::OR:
1071 case ISD::XOR:
1072 case ISD::SHL:
1073 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001074 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001075 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001076 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1077 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001078 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1079 {
1080 switch(opcode) {
1081 case ISD::AND: Opc = Alpha::ANDi; break;
1082 case ISD::OR: Opc = Alpha::BISi; break;
1083 case ISD::XOR: Opc = Alpha::XORi; break;
1084 case ISD::SHL: Opc = Alpha::SLi; break;
1085 case ISD::SRL: Opc = Alpha::SRLi; break;
1086 case ISD::SRA: Opc = Alpha::SRAi; break;
1087 case ISD::MUL: Opc = Alpha::MULQi; break;
1088 };
1089 Tmp1 = SelectExpr(N.getOperand(0));
1090 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1091 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1092 }
1093 else
1094 {
1095 switch(opcode) {
1096 case ISD::AND: Opc = Alpha::AND; break;
1097 case ISD::OR: Opc = Alpha::BIS; break;
1098 case ISD::XOR: Opc = Alpha::XOR; break;
1099 case ISD::SHL: Opc = Alpha::SL; break;
1100 case ISD::SRL: Opc = Alpha::SRL; break;
1101 case ISD::SRA: Opc = Alpha::SRA; break;
1102 case ISD::MUL: Opc = Alpha::MULQ; break;
1103 };
1104 Tmp1 = SelectExpr(N.getOperand(0));
1105 Tmp2 = SelectExpr(N.getOperand(1));
1106 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1107 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001108 return Result;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001109
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001110 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001111 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001112 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001113 bool isAdd = opcode == ISD::ADD;
1114
1115 //FIXME: first check for Scaled Adds and Subs!
1116 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001117 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1118 { //Normal imm add/sub
1119 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1120 Tmp1 = SelectExpr(N.getOperand(0));
1121 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1122 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1123 }
1124 else if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001125 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
1126 { //LDA //FIXME: expand the above condition a bit
1127 Tmp1 = SelectExpr(N.getOperand(0));
1128 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1129 if (!isAdd)
1130 Tmp2 = -Tmp2;
1131 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
1132 }
1133 else
1134 { //Normal add/sub
1135 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1136 Tmp1 = SelectExpr(N.getOperand(0));
1137 Tmp2 = SelectExpr(N.getOperand(1));
1138 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1139 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001140 return Result;
1141 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001142
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001143 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00001144 case ISD::SREM:
1145 case ISD::SDIV:
1146 case ISD::UDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001147 //FIXME: alpha really doesn't support any of these operations,
1148 // the ops are expanded into special library calls with
1149 // special calling conventions
1150 switch(opcode) {
1151 case ISD::UREM: Opc = Alpha::REMQU; break;
1152 case ISD::SREM: Opc = Alpha::REMQ; break;
1153 case ISD::UDIV: Opc = Alpha::DIVQU; break;
1154 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001155 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001156 Tmp1 = SelectExpr(N.getOperand(0));
1157 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth02981182005-01-26 01:24:38 +00001158 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001159 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001160
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001161 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001162 case ISD::FP_TO_SINT:
1163 {
1164 assert (DestType == MVT::i64 && "only quads can be loaded to");
1165 MVT::ValueType SrcType = N.getOperand(0).getValueType();
1166 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1167
1168 //The hard way:
1169 // Spill the integer to memory and reload it from there.
1170 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1171 MachineFunction *F = BB->getParent();
1172 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
1173
1174 //CVTTQ STT LDQ
1175 //CVTST CVTTQ STT LDQ
1176 if (SrcType == MVT::f32)
1177 {
1178 Tmp2 = MakeReg(MVT::f64);
1179 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1180 Tmp1 = Tmp2;
1181 }
1182 Tmp2 = MakeReg(MVT::f64);
1183 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
1184 BuildMI(BB, Alpha::STT, 3).addReg(Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1185 BuildMI(BB, Alpha::LDQ, 2, Result).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1186
1187 return Result;
1188 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001189
1190// // case ISD::FP_TO_UINT:
1191
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001192 case ISD::SELECT:
1193 {
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001194 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001195 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1196 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001197 // Get the condition into the zero flag.
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001198 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001199 return Result;
1200 }
1201
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001202 case ISD::Constant:
1203 {
Andrew Lenharth22d5a412005-02-02 00:51:15 +00001204 unsigned long val = cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001205 if (val < 32000 && (long)val > -32000)
1206 BuildMI(BB, Alpha::LOAD_IMM, 1, Result).addImm(val);
1207 else
1208 {
1209 MachineConstantPool *CP = BB->getParent()->getConstantPool();
1210 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
1211 unsigned CPI = CP->getConstantPoolIndex(C);
1212 AlphaLowering.restoreGP(BB);
1213 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
1214 }
1215 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001216 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001217
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001218 case ISD::LOAD:
1219 {
1220 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001221 if (Result != notIn)
1222 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001223 else
1224 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1225
1226 SDOperand Chain = N.getOperand(0);
1227 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001228 Select(Chain);
1229
Andrew Lenharthc23d6962005-02-02 04:35:44 +00001230 assert(N.getValue(0).getValueType() == MVT::i64 && "unknown Load dest type");
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001231
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001232 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth2afc8212005-02-02 03:36:35 +00001233 {
Andrew Lenharth2afc8212005-02-02 03:36:35 +00001234 AlphaLowering.restoreGP(BB);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001235 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
Andrew Lenharth2afc8212005-02-02 03:36:35 +00001236 }
1237 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1238 AlphaLowering.restoreGP(BB);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001239 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth2afc8212005-02-02 03:36:35 +00001240 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001241 else
Andrew Lenharth2afc8212005-02-02 03:36:35 +00001242 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001243 long offset;
1244 SelectAddr(Address, Tmp1, offset);
1245 BuildMI(BB, Alpha::LDQ, 2, Result).addImm(offset).addReg(Tmp1);
Andrew Lenharth2afc8212005-02-02 03:36:35 +00001246 }
1247 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001248 }
1249 }
1250
1251 return 0;
1252}
1253
1254void ISel::Select(SDOperand N) {
1255 unsigned Tmp1, Tmp2, Opc;
1256
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001257 // FIXME: Disable for our current expansion model!
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001258 if (/*!N->hasOneUse() &&*/ !ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001259 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001260
1261 SDNode *Node = N.Val;
1262
1263 switch (N.getOpcode()) {
1264
1265 default:
1266 Node->dump(); std::cerr << "\n";
1267 assert(0 && "Node not handled yet!");
1268
1269 case ISD::BRCOND: {
1270 MachineBasicBlock *Dest =
1271 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
1272
Andrew Lenharth9818c052005-02-05 13:19:12 +00001273 Select(N.getOperand(0)); //chain
1274
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001275 Tmp1 = SelectExpr(N.getOperand(1));
1276 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
1277 return;
1278 }
1279
1280 case ISD::BR: {
1281 MachineBasicBlock *Dest =
1282 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1283
1284 Select(N.getOperand(0));
1285 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
1286 return;
1287 }
1288
1289 case ISD::ImplicitDef:
1290 Select(N.getOperand(0));
1291 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
1292 return;
1293
1294 case ISD::EntryToken: return; // Noop
1295
1296 case ISD::TokenFactor:
1297 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1298 Select(Node->getOperand(i));
1299
1300 //N.Val->dump(); std::cerr << "\n";
1301 //assert(0 && "Node not handled yet!");
1302
1303 return;
1304
1305 case ISD::CopyToReg:
1306 Select(N.getOperand(0));
1307 Tmp1 = SelectExpr(N.getOperand(1));
1308 Tmp2 = cast<RegSDNode>(N)->getReg();
1309
1310 if (Tmp1 != Tmp2) {
1311 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1312 }
1313 return;
1314
1315 case ISD::RET:
1316 switch (N.getNumOperands()) {
1317 default:
1318 std::cerr << N.getNumOperands() << "\n";
1319 for (unsigned i = 0; i < N.getNumOperands(); ++i)
1320 std::cerr << N.getOperand(i).getValueType() << "\n";
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001321 Node->dump();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001322 assert(0 && "Unknown return instruction!");
1323 case 2:
1324 Select(N.getOperand(0));
1325 Tmp1 = SelectExpr(N.getOperand(1));
1326 switch (N.getOperand(1).getValueType()) {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001327 default: Node->dump(); assert(0 && "All other types should have been promoted!!");
1328 case MVT::f64:
1329 case MVT::f32:
1330 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1331 break;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001332 case MVT::i32:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001333 case MVT::i64:
1334 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1335 break;
1336 }
1337 break;
1338 case 1:
1339 Select(N.getOperand(0));
1340 break;
1341 }
1342 //Tmp2 = AlphaLowering.getRetAddr();
1343 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
1344 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
1345 return;
1346
1347 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001348 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001349 SDOperand Chain = N.getOperand(0);
1350 SDOperand Value = N.getOperand(1);
1351 SDOperand Address = N.getOperand(2);
1352 Select(Chain);
1353
1354 Tmp1 = SelectExpr(Value); //value
1355 MVT::ValueType DestType = Value.getValueType();
Andrew Lenharth65838902005-02-06 16:22:15 +00001356 switch(DestType) {
1357 default: assert(0 && "unknown Type in store");
1358 case MVT::i64: Opc = Alpha::STQ; break;
1359 case MVT::f64: Opc = Alpha::STT; break;
1360 case MVT::f32: Opc = Alpha::STS; break;
1361 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001362 if (Address.getOpcode() == ISD::GlobalAddress)
1363 {
1364 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001365 Opc = GetSymVersion(Opc);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001366 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1367 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001368 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001369 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001370 long offset;
1371 SelectAddr(Address, Tmp2, offset);
1372 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1373 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001374 return;
1375 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001376
1377 case ISD::EXTLOAD:
1378 case ISD::SEXTLOAD:
1379 case ISD::ZEXTLOAD:
1380 case ISD::LOAD:
1381 case ISD::CopyFromReg:
1382 case ISD::CALL:
1383// case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001384 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001385 SelectExpr(N);
1386 return;
1387
1388
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001389 case ISD::TRUNCSTORE:
1390 {
1391 SDOperand Chain = N.getOperand(0);
1392 SDOperand Value = N.getOperand(1);
1393 SDOperand Address = N.getOperand(2);
1394 Select(Chain);
1395
1396 MVT::ValueType DestType = cast<MVTSDNode>(Node)->getExtraValueType();
Andrew Lenharth65838902005-02-06 16:22:15 +00001397 switch(DestType) {
1398 default: assert(0 && "unknown Type in store");
1399 case MVT::i1: //FIXME: DAG does not promote this load
1400 case MVT::i8: Opc = Alpha::STB; break;
1401 case MVT::i16: Opc = Alpha::STW; break;
1402 case MVT::i32: Opc = Alpha::STL; break;
1403 }
1404
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001405 Tmp1 = SelectExpr(Value); //value
1406
1407 if (Address.getOpcode() == ISD::GlobalAddress)
1408 {
1409 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001410 Opc = GetSymVersion(Opc);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001411 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1412 }
1413 else
1414 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001415 long offset;
1416 SelectAddr(Address, Tmp2, offset);
1417 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1418 }
1419 return;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001420 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001421
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001422 case ISD::ADJCALLSTACKDOWN:
1423 case ISD::ADJCALLSTACKUP:
1424 Select(N.getOperand(0));
1425 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1426
1427 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? Alpha::ADJUSTSTACKDOWN :
1428 Alpha::ADJUSTSTACKUP;
1429 BuildMI(BB, Opc, 1).addImm(Tmp1);
1430 return;
1431 }
1432 assert(0 && "Should not be reached!");
1433}
1434
1435
1436/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1437/// into a machine code representation using pattern matching and a machine
1438/// description file.
1439///
1440FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
1441 return new ISel(TM);
1442}