blob: d4d2812f3962fdfbb50d0a90c4ee319340281031 [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
Andrew Lenharth445171a2005-02-08 00:40:03 +0000291 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
292 std::map<SDOperand, unsigned> CCInvMap;
293
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000294 public:
295 ISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM) {
296 }
297
298 /// InstructionSelectBasicBlock - This callback is invoked by
299 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
300 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
301 // Codegen the basic block.
302 Select(DAG.getRoot());
303
304 // Clear state used for selection.
305 ExprMap.clear();
Andrew Lenharth445171a2005-02-08 00:40:03 +0000306 CCInvMap.clear();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000307 }
308
309 unsigned SelectExpr(SDOperand N);
Andrew Lenharth40831c52005-01-28 06:57:18 +0000310 unsigned SelectExprFP(SDOperand N, unsigned Result);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000311 void Select(SDOperand N);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000312
313 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000314 void SelectBranchCC(SDOperand N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000315 };
316}
317
Andrew Lenharth65838902005-02-06 16:22:15 +0000318static unsigned GetSymVersion(unsigned opcode)
319{
320 switch (opcode) {
321 default: assert(0 && "unknown load or store"); return 0;
322 case Alpha::LDQ: return Alpha::LDQ_SYM;
323 case Alpha::LDS: return Alpha::LDS_SYM;
324 case Alpha::LDT: return Alpha::LDT_SYM;
325 case Alpha::LDL: return Alpha::LDL_SYM;
326 case Alpha::LDBU: return Alpha::LDBU_SYM;
327 case Alpha::LDWU: return Alpha::LDWU_SYM;
328 case Alpha::LDW: return Alpha::LDW_SYM;
329 case Alpha::LDB: return Alpha::LDB_SYM;
330 case Alpha::STQ: return Alpha::STQ_SYM;
331 case Alpha::STS: return Alpha::STS_SYM;
332 case Alpha::STT: return Alpha::STT_SYM;
333 case Alpha::STL: return Alpha::STL_SYM;
334 case Alpha::STW: return Alpha::STW_SYM;
335 case Alpha::STB: return Alpha::STB_SYM;
336 }
337}
338
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000339//Check to see if the load is a constant offset from a base register
340void ISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
341{
342 unsigned opcode = N.getOpcode();
343 if (opcode == ISD::ADD) {
344 if(N.getOperand(1).getOpcode() == ISD::Constant && cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
345 { //Normal imm add
346 Reg = SelectExpr(N.getOperand(0));
347 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
348 return;
349 }
350 else if(N.getOperand(0).getOpcode() == ISD::Constant && cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 32767)
351 {
352 Reg = SelectExpr(N.getOperand(1));
353 offset = cast<ConstantSDNode>(N.getOperand(0))->getValue();
354 return;
355 }
356 }
357 Reg = SelectExpr(N);
358 offset = 0;
359 return;
360}
361
Andrew Lenharth445171a2005-02-08 00:40:03 +0000362void ISel::SelectBranchCC(SDOperand N)
363{
364 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
365 MachineBasicBlock *Dest = cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
366 unsigned Opc;
367
368 Select(N.getOperand(0)); //chain
369 SDOperand CC = N.getOperand(1);
370
371 if (CC.getOpcode() == ISD::SETCC)
372 {
373 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
374 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
375 //Dropping the CC is only useful if we are comparing to 0
376 bool isZero0 = false;
377 bool isZero1 = false;
378 bool isNE = false;
379
380 if(SetCC->getOperand(0).getOpcode() == ISD::Constant &&
381 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0)
382 isZero0 = true;
383 if(SetCC->getOperand(1).getOpcode() == ISD::Constant &&
384 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0)
385 isZero1 = true;
386 if(SetCC->getCondition() == ISD::SETNE)
387 isNE = true;
388
389 if (isZero0)
390 {
391 switch (SetCC->getCondition()) {
392 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
393 case ISD::SETEQ: Opc = Alpha::BEQ; break;
394 case ISD::SETLT: Opc = Alpha::BGT; break;
395 case ISD::SETLE: Opc = Alpha::BGE; break;
396 case ISD::SETGT: Opc = Alpha::BLT; break;
397 case ISD::SETGE: Opc = Alpha::BLE; break;
398 case ISD::SETULT: Opc = Alpha::BNE; break;
399 case ISD::SETUGT: assert(0 && "0 > (unsigned) x is never true"); break;
400 case ISD::SETULE: assert(0 && "0 <= (unsigned) x is always true"); break;
401 case ISD::SETUGE: Opc = Alpha::BEQ; break; //Technically you could have this CC
402 case ISD::SETNE: Opc = Alpha::BNE; break;
403 }
404 unsigned Tmp1 = SelectExpr(SetCC->getOperand(1));
405 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
406 return;
407 }
408 else if (isZero1)
409 {
410 switch (SetCC->getCondition()) {
411 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
412 case ISD::SETEQ: Opc = Alpha::BEQ; break;
413 case ISD::SETLT: Opc = Alpha::BLT; break;
414 case ISD::SETLE: Opc = Alpha::BLE; break;
415 case ISD::SETGT: Opc = Alpha::BGT; break;
416 case ISD::SETGE: Opc = Alpha::BGE; break;
417 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
418 case ISD::SETUGT: Opc = Alpha::BNE; break;
419 case ISD::SETULE: Opc = Alpha::BEQ; break; //Technically you could have this CC
420 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
421 case ISD::SETNE: Opc = Alpha::BNE; break;
422 }
423 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
424 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
425 return;
426 }
427 else
428 {
429 unsigned Tmp1 = SelectExpr(CC);
430 if (isNE)
431 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
432 else
433 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
434 return;
435 }
436 } else { //FP
437 //Any comparison between 2 values should be codegened as an folded branch, as moving
438 //CC to the integer register is very expensive
439 //for a cmp b: c = a - b;
440 //a = b: c = 0
441 //a < b: c < 0
442 //a > b: c > 0
443 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
444 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
445 unsigned Tmp3 = MakeReg(MVT::f64);
446 BuildMI(BB, Alpha::SUBT, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
447
448 switch (SetCC->getCondition()) {
449 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
450 case ISD::SETEQ: Opc = Alpha::FBEQ; break;
451 case ISD::SETLT: Opc = Alpha::FBLT; break;
452 case ISD::SETLE: Opc = Alpha::FBLE; break;
453 case ISD::SETGT: Opc = Alpha::FBGT; break;
454 case ISD::SETGE: Opc = Alpha::FBGE; break;
455 case ISD::SETNE: Opc = Alpha::FBNE; break;
456 }
457 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
458 return;
459 }
460 abort(); //Should never be reached
461 }
462 else
463 { //Giveup and do the stupid thing
464 unsigned Tmp1 = SelectExpr(CC);
465 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
466 return;
467 }
468 abort(); //Should never be reached
469}
470
Andrew Lenharth40831c52005-01-28 06:57:18 +0000471unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
472{
473 unsigned Tmp1, Tmp2, Tmp3;
474 unsigned Opc = 0;
475 SDNode *Node = N.Val;
476 MVT::ValueType DestType = N.getValueType();
477 unsigned opcode = N.getOpcode();
478
479 switch (opcode) {
480 default:
481 Node->dump();
482 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +0000483
Andrew Lenharth9818c052005-02-05 13:19:12 +0000484 case ISD::SELECT:
485 {
486 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
487 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
488 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000489
490
491 // Spill the cond to memory and reload it from there.
492 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
493 MachineFunction *F = BB->getParent();
494 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
495 unsigned Tmp4 = MakeReg(MVT::f64);
496 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addFrameIndex(FrameIdx).addReg(Alpha::F31);
497 BuildMI(BB, Alpha::LDT, 2, Tmp4).addFrameIndex(FrameIdx).addReg(Alpha::F31);
498 //now ideally, we don't have to do anything to the flag...
Andrew Lenharth9818c052005-02-05 13:19:12 +0000499 // Get the condition into the zero flag.
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000500 BuildMI(BB, Alpha::FCMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp4);
Andrew Lenharth9818c052005-02-05 13:19:12 +0000501 return Result;
502 }
503
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000504 case ISD::FP_ROUND:
505 assert (DestType == MVT::f32 && N.getOperand(0).getValueType() == MVT::f64 && "only f64 to f32 conversion supported here");
506 Tmp1 = SelectExpr(N.getOperand(0));
507 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
508 return Result;
509
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000510 case ISD::FP_EXTEND:
511 assert (DestType == MVT::f64 && N.getOperand(0).getValueType() == MVT::f32 && "only f32 to f64 conversion supported here");
512 Tmp1 = SelectExpr(N.getOperand(0));
513 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
514 return Result;
515
Andrew Lenharth2c594352005-01-29 15:42:07 +0000516 case ISD::CopyFromReg:
517 {
518 // Make sure we generate both values.
519 if (Result != notIn)
520 ExprMap[N.getValue(1)] = notIn; // Generate the token
521 else
522 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
523
524 SDOperand Chain = N.getOperand(0);
525
526 Select(Chain);
527 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
528 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
529 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
530 return Result;
531 }
532
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000533 case ISD::LOAD:
534 {
535 // Make sure we generate both values.
536 if (Result != notIn)
537 ExprMap[N.getValue(1)] = notIn; // Generate the token
538 else
539 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000540
Andrew Lenharth29219162005-02-07 06:31:44 +0000541 DestType = N.getValue(0).getValueType();
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000542
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000543 SDOperand Chain = N.getOperand(0);
544 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000545 Select(Chain);
Andrew Lenharth65838902005-02-06 16:22:15 +0000546 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
547
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000548 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000549 {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000550 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000551 Opc = GetSymVersion(Opc);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000552 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
553 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000554 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000555 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000556 Opc = GetSymVersion(Opc);
Andrew Lenharth97127a12005-02-05 17:41:39 +0000557 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000558 }
Andrew Lenharth05380342005-02-07 05:07:00 +0000559 else if(Address.getOpcode() == ISD::FrameIndex)
560 {
561 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
562 BuildMI(BB, Opc, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
563 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000564 else
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000565 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000566 long offset;
567 SelectAddr(Address, Tmp1, offset);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000568 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000569 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000570 return Result;
571 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000572 case ISD::ConstantFP:
573 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
574 if (CN->isExactlyValue(+0.0)) {
575 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000576 } else if ( CN->isExactlyValue(-0.0)) {
577 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +0000578 } else {
579 abort();
580 }
581 }
582 return Result;
583
584 case ISD::MUL:
585 case ISD::ADD:
586 case ISD::SUB:
587 case ISD::SDIV:
588 switch( opcode ) {
589 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
590 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
591 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
592 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
593 };
594 Tmp1 = SelectExpr(N.getOperand(0));
595 Tmp2 = SelectExpr(N.getOperand(1));
596 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
597 return Result;
598
Andrew Lenharth2c594352005-01-29 15:42:07 +0000599 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000600 {
601 //include a conversion sequence for float loads to double
602 if (Result != notIn)
603 ExprMap[N.getValue(1)] = notIn; // Generate the token
604 else
605 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
606
Andrew Lenhartha549deb2005-02-07 05:33:15 +0000607 Tmp1 = MakeReg(MVT::f32);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000608
609 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 && "EXTLOAD not from f32");
610 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
611
612 SDOperand Chain = N.getOperand(0);
613 SDOperand Address = N.getOperand(1);
614 Select(Chain);
615
616 if (Address.getOpcode() == ISD::GlobalAddress)
617 {
618 AlphaLowering.restoreGP(BB);
Andrew Lenhartha549deb2005-02-07 05:33:15 +0000619 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
Andrew Lenhartha549deb2005-02-07 05:33:15 +0000620 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000621 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
622 {
623 AlphaLowering.restoreGP(BB);
Andrew Lenhartha549deb2005-02-07 05:33:15 +0000624 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000625 }
Andrew Lenharth05380342005-02-07 05:07:00 +0000626 else if(Address.getOpcode() == ISD::FrameIndex)
627 {
Andrew Lenhartha549deb2005-02-07 05:33:15 +0000628 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
629 BuildMI(BB, Alpha::LDS, 2, Tmp1).addFrameIndex(Tmp2).addReg(Alpha::F31);
Andrew Lenharth05380342005-02-07 05:07:00 +0000630 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000631 else
632 {
633 long offset;
Andrew Lenhartha549deb2005-02-07 05:33:15 +0000634 SelectAddr(Address, Tmp2, offset);
635 BuildMI(BB, Alpha::LDS, 1, Tmp1).addImm(offset).addReg(Tmp2);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000636 }
Andrew Lenharth29219162005-02-07 06:31:44 +0000637 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000638 return Result;
639 }
Andrew Lenharth2c594352005-01-29 15:42:07 +0000640
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000641 case ISD::UINT_TO_FP:
642 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000643 {
644 assert (N.getOperand(0).getValueType() == MVT::i64 && "only quads can be loaded from");
645 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +0000646 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000647
648 //The hard way:
649 // Spill the integer to memory and reload it from there.
650 unsigned Size = MVT::getSizeInBits(MVT::i64)/8;
651 MachineFunction *F = BB->getParent();
652 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
653
Andrew Lenharth7efadce2005-01-31 01:44:26 +0000654 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addFrameIndex(FrameIdx).addReg(Alpha::F31);
655 BuildMI(BB, Alpha::LDT, 2, Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
656 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
657 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000658
659 //The easy way: doesn't work
660// //so these instructions are not supported on ev56
661// Opc = DestType == MVT::f64 ? Alpha::ITOFT : Alpha::ITOFS;
662// BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
663// Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
664// BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
665
Andrew Lenharth40831c52005-01-28 06:57:18 +0000666 return Result;
667 }
668 }
669 assert(0 && "should not get here");
670 return 0;
671}
672
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000673unsigned ISel::SelectExpr(SDOperand N) {
674 unsigned Result;
675 unsigned Tmp1, Tmp2, Tmp3;
676 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000677 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000678
679 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000680 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000681
682 unsigned &Reg = ExprMap[N];
683 if (Reg) return Reg;
684
685 if (N.getOpcode() != ISD::CALL)
686 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000687 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000688 else {
689 // If this is a call instruction, make sure to prepare ALL of the result
690 // values as well as the chain.
691 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000692 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000693 else {
694 Result = MakeReg(Node->getValueType(0));
695 ExprMap[N.getValue(0)] = Result;
696 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
697 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000698 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000699 }
700 }
701
Andrew Lenharth22088bb2005-02-02 15:05:33 +0000702 if (DestType == MVT::f64 || DestType == MVT::f32 ||
Andrew Lenharth06342c32005-02-07 06:21:37 +0000703 (
704 (opcode == ISD::LOAD || opcode == ISD::CopyFromReg || opcode == ISD::EXTLOAD) &&
705 (N.getValue(0).getValueType() == MVT::f32 || N.getValue(0).getValueType() == MVT::f64)
706 )
707 )
Andrew Lenharth40831c52005-01-28 06:57:18 +0000708 return SelectExprFP(N, Result);
709
710 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000711 default:
712 Node->dump();
713 assert(0 && "Node not handled!\n");
714
Andrew Lenharth2c594352005-01-29 15:42:07 +0000715 case ISD::ConstantPool:
716 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
717 AlphaLowering.restoreGP(BB);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000718 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000719 return Result;
720
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000721 case ISD::FrameIndex:
722 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Andrew Lenharth684f2292005-01-30 00:35:27 +0000723 BuildMI(BB, Alpha::LDA, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000724 return Result;
725
726 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +0000727 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000728 case ISD::SEXTLOAD:
Andrew Lenhartha549deb2005-02-07 05:33:15 +0000729 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000730 {
731 // Make sure we generate both values.
732 if (Result != notIn)
733 ExprMap[N.getValue(1)] = notIn; // Generate the token
734 else
735 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000736
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000737 SDOperand Chain = N.getOperand(0);
738 SDOperand Address = N.getOperand(1);
739 Select(Chain);
740
Andrew Lenharth03824012005-02-07 05:55:55 +0000741 assert(Node->getValueType(0) == MVT::i64 && "Unknown type to sign extend to.");
742 if (opcode == ISD::LOAD)
743 Opc = Alpha::LDQ;
744 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000745 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
746 default: Node->dump(); assert(0 && "Bad sign extend!");
Andrew Lenharthf311e8b2005-02-07 05:18:02 +0000747 case MVT::i32: Opc = Alpha::LDL; assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
748 case MVT::i16: Opc = Alpha::LDWU; assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
749 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
750 case MVT::i8: Opc = Alpha::LDBU; assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000751 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000752
753 if (Address.getOpcode() == ISD::GlobalAddress)
754 {
755 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000756 Opc = GetSymVersion(Opc);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000757 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
758 }
759 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
760 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000761 Opc = GetSymVersion(Opc);
762 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000763 }
Andrew Lenharth05380342005-02-07 05:07:00 +0000764 else if(Address.getOpcode() == ISD::FrameIndex)
765 {
766 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
767 BuildMI(BB, Opc, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
768 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000769 else
770 {
771 long offset;
772 SelectAddr(Address, Tmp1, offset);
773 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
774 }
775 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000776 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000777
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000778 case ISD::GlobalAddress:
779 AlphaLowering.restoreGP(BB);
780 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
781 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
782 return Result;
783
784 case ISD::CALL:
785 {
786 Select(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000787
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000788 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000789 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000790
791 //grab the arguments
792 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000793 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000794 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000795 argvregs.push_back(SelectExpr(N.getOperand(i)));
796
Andrew Lenharth684f2292005-01-30 00:35:27 +0000797 //in reg args
798 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
799 {
800 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
801 Alpha::R19, Alpha::R20, Alpha::R21};
802 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
803 Alpha::F19, Alpha::F20, Alpha::F21};
804 switch(N.getOperand(i+2).getValueType()) {
805 default:
806 Node->dump();
807 N.getOperand(i).Val->dump();
808 std::cerr << "Type for " << i << " is: " << N.getOperand(i+2).getValueType() << "\n";
809 assert(0 && "Unknown value type for call");
810 case MVT::i1:
811 case MVT::i8:
812 case MVT::i16:
813 case MVT::i32:
814 case MVT::i64:
815 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
816 break;
817 case MVT::f32:
818 case MVT::f64:
819 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
820 break;
821 }
822 }
823 //in mem args
824 for (int i = 6, e = argvregs.size(); i < e; ++i)
825 {
826 switch(N.getOperand(i+2).getValueType()) {
827 default:
828 Node->dump();
829 N.getOperand(i).Val->dump();
830 std::cerr << "Type for " << i << " is: " << N.getOperand(i+2).getValueType() << "\n";
831 assert(0 && "Unknown value type for call");
832 case MVT::i1:
833 case MVT::i8:
834 case MVT::i16:
835 case MVT::i32:
836 case MVT::i64:
837 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
838 break;
839 case MVT::f32:
840 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
841 break;
842 case MVT::f64:
843 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
844 break;
845 }
846 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000847 //build the right kind of call
848 if (GlobalAddressSDNode *GASD =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000849 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
850 {
851 AlphaLowering.restoreGP(BB);
852 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal(),true);
853 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000854 else if (ExternalSymbolSDNode *ESSDN =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000855 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
856 {
857 AlphaLowering.restoreGP(BB);
858 BuildMI(BB, Alpha::CALL, 0).addExternalSymbol(ESSDN->getSymbol(), true);
859 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000860 else
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000861 {
862 //no need to restore GP as we are doing an indirect call
863 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth06342c32005-02-07 06:21:37 +0000864 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000865 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000866 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000867
868 //push the result into a virtual register
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000869
870 switch (Node->getValueType(0)) {
871 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000872 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000873 case MVT::i1:
874 case MVT::i8:
875 case MVT::i16:
876 case MVT::i32:
877 case MVT::i64:
878 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
879 break;
880 case MVT::f32:
881 case MVT::f64:
882 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
883 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000884 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000885 return Result+N.ResNo;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000886 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000887
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000888 case ISD::SIGN_EXTEND:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000889 abort();
890
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000891 case ISD::SIGN_EXTEND_INREG:
892 {
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000893 //Alpha has instructions for a bunch of signed 32 bit stuff
894 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
895 {
896 switch (N.getOperand(0).getOpcode()) {
897 case ISD::ADD:
898 case ISD::SUB:
899 case ISD::MUL:
900 {
901 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
902 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
903 //FIXME: first check for Scaled Adds and Subs!
904 if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
905 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
906 { //Normal imm add/sub
907 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
908 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
909 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
910 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
911 }
912 else
913 { //Normal add/sub
914 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULLi : Alpha::SUBL);
915 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
916 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
917 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
918 }
919 return Result;
920 }
921 default: break; //Fall Though;
922 }
923 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000924 Tmp1 = SelectExpr(N.getOperand(0));
925 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000926 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000927 switch(MVN->getExtraValueType())
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000928 {
929 default:
930 Node->dump();
931 assert(0 && "Sign Extend InReg not there yet");
932 break;
933 case MVT::i32:
934 {
935 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
936 break;
937 }
938 case MVT::i16:
939 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
940 break;
941 case MVT::i8:
942 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
943 break;
944 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000945 return Result;
946 }
947 case ISD::ZERO_EXTEND_INREG:
948 {
949 Tmp1 = SelectExpr(N.getOperand(0));
950 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000951 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000952 switch(MVN->getExtraValueType())
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000953 {
954 default:
955 Node->dump();
956 assert(0 && "Zero Extend InReg not there yet");
957 break;
958 case MVT::i32: Tmp2 = 0xf0; break;
959 case MVT::i16: Tmp2 = 0xfc; break;
960 case MVT::i8: Tmp2 = 0xfe; break;
961 case MVT::i1: //handle this one special
962 BuildMI(BB, Alpha::ANDi, 2, Result).addReg(Tmp1).addImm(1);
963 return Result;
964 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000965 BuildMI(BB, Alpha::ZAPi, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000966 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000967 }
968
969 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000970 {
971 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
972 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
973 bool isConst1 = false;
974 bool isConst2 = false;
975 int dir;
Andrew Lenharth9818c052005-02-05 13:19:12 +0000976
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000977 //Tmp1 = SelectExpr(N.getOperand(0));
978 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000979 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
980 isConst1 = true;
981 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000982 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
983 isConst2 = true;
984
985 switch (SetCC->getCondition()) {
986 default: Node->dump(); assert(0 && "Unknown integer comparison!");
987 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
988 case ISD::SETLT: Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
989 case ISD::SETLE: Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
990 case ISD::SETGT: Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
991 case ISD::SETGE: Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
992 case ISD::SETULT: Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
993 case ISD::SETUGT: Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
994 case ISD::SETULE: Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
995 case ISD::SETUGE: Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000996 case ISD::SETNE: {//Handle this one special
997 //std::cerr << "Alpha does not have a setne.\n";
998 //abort();
999 Tmp1 = SelectExpr(N.getOperand(0));
1000 Tmp2 = SelectExpr(N.getOperand(1));
1001 Tmp3 = MakeReg(MVT::i64);
1002 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001003 //Remeber we have the Inv for this CC
1004 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001005 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001006 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001007 return Result;
1008 }
1009 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001010 if (dir == 1) {
1011 Tmp1 = SelectExpr(N.getOperand(0));
1012 if (isConst2) {
1013 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1014 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1015 } else {
1016 Tmp2 = SelectExpr(N.getOperand(1));
1017 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1018 }
1019 } else if (dir == 2) {
1020 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001021 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001022 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1023 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1024 } else {
1025 Tmp2 = SelectExpr(N.getOperand(0));
1026 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1027 }
1028 } else { //dir == 0
1029 if (isConst1) {
1030 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1031 Tmp2 = SelectExpr(N.getOperand(1));
1032 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
1033 } else if (isConst2) {
1034 Tmp1 = SelectExpr(N.getOperand(0));
1035 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1036 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1037 } else {
1038 Tmp1 = SelectExpr(N.getOperand(0));
1039 Tmp2 = SelectExpr(N.getOperand(1));
1040 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1041 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001042 }
1043 } else {
1044 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
1045 bool rev = false;
1046 bool inv = false;
1047
1048 switch (SetCC->getCondition()) {
1049 default: Node->dump(); assert(0 && "Unknown FP comparison!");
1050 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
1051 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
1052 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
1053 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
1054 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
1055 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
1056 }
1057
1058 Tmp1 = SelectExpr(N.getOperand(0));
1059 Tmp2 = SelectExpr(N.getOperand(1));
1060 //Can only compare doubles, and dag won't promote for me
1061 if (SetCC->getOperand(0).getValueType() == MVT::f32)
1062 {
1063 Tmp3 = MakeReg(MVT::f64);
1064 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
1065 Tmp1 = Tmp3;
1066 }
1067 if (SetCC->getOperand(1).getValueType() == MVT::f32)
1068 {
1069 Tmp3 = MakeReg(MVT::f64);
1070 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
1071 Tmp1 = Tmp2;
1072 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001073
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001074 if (rev) std::swap(Tmp1, Tmp2);
1075 Tmp3 = MakeReg(MVT::f64);
1076 //do the comparison
1077 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1078
1079 //now arrange for Result (int) to have a 1 or 0
1080
1081 // Spill the FP to memory and reload it from there.
1082 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1083 MachineFunction *F = BB->getParent();
1084 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
1085 unsigned Tmp4 = MakeReg(MVT::f64);
1086 BuildMI(BB, Alpha::CVTTQ, 1, Tmp4).addReg(Tmp3);
1087 BuildMI(BB, Alpha::STT, 3).addReg(Tmp4).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1088 unsigned Tmp5 = MakeReg(MVT::i64);
1089 BuildMI(BB, Alpha::LDQ, 2, Tmp5).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharth9818c052005-02-05 13:19:12 +00001090
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001091 //now, set result based on Tmp5
1092 //Set Tmp6 if fp cmp was false
1093 unsigned Tmp6 = MakeReg(MVT::i64);
1094 BuildMI(BB, Alpha::CMPEQ, 2, Tmp6).addReg(Tmp5).addReg(Alpha::R31);
1095 //and invert
1096 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp6).addReg(Alpha::R31);
1097
1098 }
1099 // else
1100 // {
1101 // Node->dump();
1102 // assert(0 && "Not a setcc in setcc");
1103 // }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001104 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001105 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001106 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001107
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001108 case ISD::CopyFromReg:
1109 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001110 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001111 if (Result != notIn)
1112 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001113 else
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001114 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth40831c52005-01-28 06:57:18 +00001115
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001116 SDOperand Chain = N.getOperand(0);
1117
1118 Select(Chain);
1119 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1120 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1121 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1122 return Result;
1123 }
1124
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001125 //Most of the plain arithmetic and logic share the same form, and the same
1126 //constant immediate test
1127 case ISD::AND:
1128 case ISD::OR:
1129 case ISD::XOR:
1130 case ISD::SHL:
1131 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001132 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001133 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001134 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1135 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001136 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1137 {
1138 switch(opcode) {
1139 case ISD::AND: Opc = Alpha::ANDi; break;
1140 case ISD::OR: Opc = Alpha::BISi; break;
1141 case ISD::XOR: Opc = Alpha::XORi; break;
1142 case ISD::SHL: Opc = Alpha::SLi; break;
1143 case ISD::SRL: Opc = Alpha::SRLi; break;
1144 case ISD::SRA: Opc = Alpha::SRAi; break;
1145 case ISD::MUL: Opc = Alpha::MULQi; break;
1146 };
1147 Tmp1 = SelectExpr(N.getOperand(0));
1148 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1149 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1150 }
1151 else
1152 {
1153 switch(opcode) {
1154 case ISD::AND: Opc = Alpha::AND; break;
1155 case ISD::OR: Opc = Alpha::BIS; break;
1156 case ISD::XOR: Opc = Alpha::XOR; break;
1157 case ISD::SHL: Opc = Alpha::SL; break;
1158 case ISD::SRL: Opc = Alpha::SRL; break;
1159 case ISD::SRA: Opc = Alpha::SRA; break;
1160 case ISD::MUL: Opc = Alpha::MULQ; break;
1161 };
1162 Tmp1 = SelectExpr(N.getOperand(0));
1163 Tmp2 = SelectExpr(N.getOperand(1));
1164 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1165 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001166 return Result;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001167
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001168 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001169 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001170 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001171 bool isAdd = opcode == ISD::ADD;
1172
1173 //FIXME: first check for Scaled Adds and Subs!
1174 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001175 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1176 { //Normal imm add/sub
1177 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1178 Tmp1 = SelectExpr(N.getOperand(0));
1179 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1180 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1181 }
1182 else if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001183 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
1184 { //LDA //FIXME: expand the above condition a bit
1185 Tmp1 = SelectExpr(N.getOperand(0));
1186 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1187 if (!isAdd)
1188 Tmp2 = -Tmp2;
1189 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
1190 }
1191 else
1192 { //Normal add/sub
1193 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1194 Tmp1 = SelectExpr(N.getOperand(0));
1195 Tmp2 = SelectExpr(N.getOperand(1));
1196 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1197 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001198 return Result;
1199 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001200
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001201 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00001202 case ISD::SREM:
1203 case ISD::SDIV:
1204 case ISD::UDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001205 //FIXME: alpha really doesn't support any of these operations,
1206 // the ops are expanded into special library calls with
1207 // special calling conventions
1208 switch(opcode) {
1209 case ISD::UREM: Opc = Alpha::REMQU; break;
1210 case ISD::SREM: Opc = Alpha::REMQ; break;
1211 case ISD::UDIV: Opc = Alpha::DIVQU; break;
1212 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001213 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001214 Tmp1 = SelectExpr(N.getOperand(0));
1215 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth02981182005-01-26 01:24:38 +00001216 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001217 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001218
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001219 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001220 case ISD::FP_TO_SINT:
1221 {
1222 assert (DestType == MVT::i64 && "only quads can be loaded to");
1223 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001224 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001225 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1226
1227 //The hard way:
1228 // Spill the integer to memory and reload it from there.
1229 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1230 MachineFunction *F = BB->getParent();
1231 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
1232
1233 //CVTTQ STT LDQ
1234 //CVTST CVTTQ STT LDQ
1235 if (SrcType == MVT::f32)
1236 {
1237 Tmp2 = MakeReg(MVT::f64);
1238 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1239 Tmp1 = Tmp2;
1240 }
1241 Tmp2 = MakeReg(MVT::f64);
1242 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
1243 BuildMI(BB, Alpha::STT, 3).addReg(Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1244 BuildMI(BB, Alpha::LDQ, 2, Result).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1245
1246 return Result;
1247 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001248
1249// // case ISD::FP_TO_UINT:
1250
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001251 case ISD::SELECT:
1252 {
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001253 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001254 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1255 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001256 // Get the condition into the zero flag.
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001257 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001258 return Result;
1259 }
1260
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001261 case ISD::Constant:
1262 {
Andrew Lenharth22d5a412005-02-02 00:51:15 +00001263 unsigned long val = cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001264 if (val < 32000 && (long)val > -32000)
1265 BuildMI(BB, Alpha::LOAD_IMM, 1, Result).addImm(val);
1266 else
1267 {
1268 MachineConstantPool *CP = BB->getParent()->getConstantPool();
1269 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
1270 unsigned CPI = CP->getConstantPoolIndex(C);
1271 AlphaLowering.restoreGP(BB);
1272 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
1273 }
1274 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001275 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001276 }
1277
1278 return 0;
1279}
1280
1281void ISel::Select(SDOperand N) {
1282 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00001283 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001284
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001285 // FIXME: Disable for our current expansion model!
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001286 if (/*!N->hasOneUse() &&*/ !ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001287 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001288
1289 SDNode *Node = N.Val;
Andrew Lenharth760270d2005-02-07 23:02:23 +00001290
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001291
Andrew Lenharth760270d2005-02-07 23:02:23 +00001292 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001293
1294 default:
1295 Node->dump(); std::cerr << "\n";
1296 assert(0 && "Node not handled yet!");
1297
1298 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00001299 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001300 return;
1301 }
1302
1303 case ISD::BR: {
1304 MachineBasicBlock *Dest =
1305 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1306
1307 Select(N.getOperand(0));
1308 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
1309 return;
1310 }
1311
1312 case ISD::ImplicitDef:
1313 Select(N.getOperand(0));
1314 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
1315 return;
1316
1317 case ISD::EntryToken: return; // Noop
1318
1319 case ISD::TokenFactor:
1320 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1321 Select(Node->getOperand(i));
1322
1323 //N.Val->dump(); std::cerr << "\n";
1324 //assert(0 && "Node not handled yet!");
1325
1326 return;
1327
1328 case ISD::CopyToReg:
1329 Select(N.getOperand(0));
1330 Tmp1 = SelectExpr(N.getOperand(1));
1331 Tmp2 = cast<RegSDNode>(N)->getReg();
1332
1333 if (Tmp1 != Tmp2) {
Andrew Lenharth29219162005-02-07 06:31:44 +00001334 if (N.getOperand(1).getValueType() == MVT::f64 || N.getOperand(1).getValueType() == MVT::f32)
1335 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1336 else
1337 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001338 }
1339 return;
1340
1341 case ISD::RET:
1342 switch (N.getNumOperands()) {
1343 default:
1344 std::cerr << N.getNumOperands() << "\n";
1345 for (unsigned i = 0; i < N.getNumOperands(); ++i)
1346 std::cerr << N.getOperand(i).getValueType() << "\n";
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001347 Node->dump();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001348 assert(0 && "Unknown return instruction!");
1349 case 2:
1350 Select(N.getOperand(0));
1351 Tmp1 = SelectExpr(N.getOperand(1));
1352 switch (N.getOperand(1).getValueType()) {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001353 default: Node->dump(); assert(0 && "All other types should have been promoted!!");
1354 case MVT::f64:
1355 case MVT::f32:
1356 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1357 break;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001358 case MVT::i32:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001359 case MVT::i64:
1360 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1361 break;
1362 }
1363 break;
1364 case 1:
1365 Select(N.getOperand(0));
1366 break;
1367 }
1368 //Tmp2 = AlphaLowering.getRetAddr();
1369 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
1370 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
1371 return;
1372
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001373 case ISD::TRUNCSTORE:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001374 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001375 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001376 SDOperand Chain = N.getOperand(0);
1377 SDOperand Value = N.getOperand(1);
1378 SDOperand Address = N.getOperand(2);
1379 Select(Chain);
1380
1381 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00001382
1383 if (opcode == ISD::STORE) {
1384 switch(Value.getValueType()) {
1385 default: assert(0 && "unknown Type in store");
1386 case MVT::i64: Opc = Alpha::STQ; break;
1387 case MVT::f64: Opc = Alpha::STT; break;
1388 case MVT::f32: Opc = Alpha::STS; break;
1389 }
1390 } else { //ISD::TRUNCSTORE
1391 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1392 default: assert(0 && "unknown Type in store");
1393 case MVT::i1: //FIXME: DAG does not promote this load
1394 case MVT::i8: Opc = Alpha::STB; break;
1395 case MVT::i16: Opc = Alpha::STW; break;
1396 case MVT::i32: Opc = Alpha::STL; break;
1397 }
Andrew Lenharth65838902005-02-06 16:22:15 +00001398 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00001399
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001400 if (Address.getOpcode() == ISD::GlobalAddress)
1401 {
1402 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001403 Opc = GetSymVersion(Opc);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001404 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1405 }
Andrew Lenharth05380342005-02-07 05:07:00 +00001406 else if(Address.getOpcode() == ISD::FrameIndex)
1407 {
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001408 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1409 BuildMI(BB, Opc, 3).addReg(Tmp1).addFrameIndex(Tmp2).addReg(Alpha::F31);
Andrew Lenharth05380342005-02-07 05:07:00 +00001410 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001411 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001412 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001413 long offset;
1414 SelectAddr(Address, Tmp2, offset);
1415 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1416 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001417 return;
1418 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001419
1420 case ISD::EXTLOAD:
1421 case ISD::SEXTLOAD:
1422 case ISD::ZEXTLOAD:
1423 case ISD::LOAD:
1424 case ISD::CopyFromReg:
1425 case ISD::CALL:
1426// case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001427 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001428 SelectExpr(N);
1429 return;
1430
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001431 case ISD::ADJCALLSTACKDOWN:
1432 case ISD::ADJCALLSTACKUP:
1433 Select(N.getOperand(0));
1434 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1435
1436 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? Alpha::ADJUSTSTACKDOWN :
1437 Alpha::ADJUSTSTACKUP;
1438 BuildMI(BB, Opc, 1).addImm(Tmp1);
1439 return;
1440 }
1441 assert(0 && "Should not be reached!");
1442}
1443
1444
1445/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1446/// into a machine code representation using pattern matching and a machine
1447/// description file.
1448///
1449FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
1450 return new ISel(TM);
1451}