blob: 9b0b2ccbbebfe940b8b86693001c1d8a0fcf2033 [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 Lenharth63f2ab22005-02-10 06:25:22 +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 Lenharth9818c052005-02-05 13:19:12 +000059 setOperationAction(ISD::SREM , MVT::f32 , Expand);
60 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +000061
Andrew Lenharth8d163d22005-02-02 05:49:42 +000062 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
Andrew Lenharth9818c052005-02-05 13:19:12 +000063 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
64 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
65
Andrew Lenharth572af902005-02-14 05:41:43 +000066 setOperationAction(ISD::SETCC , MVT::f32, Promote);
67
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000068 computeRegisterProperties();
Andrew Lenharth304d0f32005-01-22 23:41:55 +000069
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000070 addLegalFPImmediate(+0.0); //F31
71 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);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000078
Andrew Lenharth304d0f32005-01-22 23:41:55 +000079 /// 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);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000084
Andrew Lenharth304d0f32005-01-22 23:41:55 +000085 virtual std::pair<SDOperand, SDOperand>
86 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000087
Andrew Lenharth304d0f32005-01-22 23:41:55 +000088 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)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000147 {
148 SDOperand newroot, argt;
149 if (count < 6) {
150 switch (getValueType(I->getType())) {
151 default:
152 std::cerr << "Unknown Type " << getValueType(I->getType()) << "\n";
153 abort();
154 case MVT::f64:
155 case MVT::f32:
156 BuildMI(&BB, Alpha::IDEF, 0, args_float[count]);
157 argVreg[count] =
158 MF.getSSARegMap()->createVirtualRegister(
159 getRegClassFor(getValueType(I->getType())));
160 argPreg[count] = args_float[count];
161 argOpc[count] = Alpha::CPYS;
162 argt = newroot = DAG.getCopyFromReg(argVreg[count],
163 getValueType(I->getType()),
164 DAG.getRoot());
165 break;
166 case MVT::i1:
167 case MVT::i8:
168 case MVT::i16:
169 case MVT::i32:
170 case MVT::i64:
171 BuildMI(&BB, Alpha::IDEF, 0, args_int[count]);
172 argVreg[count] =
173 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
174 argPreg[count] = args_int[count];
175 argOpc[count] = Alpha::BIS;
176 argt = newroot =
177 DAG.getCopyFromReg(argVreg[count], MVT::i64, DAG.getRoot());
178 if (getValueType(I->getType()) != MVT::i64)
179 argt =
180 DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()), newroot);
181 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000182 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000183 ++count;
184 } else { //more args
185 // Create the frame index object for this incoming parameter...
186 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
187
188 // Create the SelectionDAG nodes corresponding to a load
189 //from this parameter
190 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
191 argt = newroot = DAG.getLoad(getValueType(I->getType()),
192 DAG.getEntryNode(), FIN);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000193 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000194 DAG.setRoot(newroot.getValue(1));
195 ArgValues.push_back(argt);
196 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000197
198 BuildMI(&BB, Alpha::IDEF, 0, Alpha::R29);
199 BuildMI(&BB, Alpha::BIS, 2, GP).addReg(Alpha::R29).addReg(Alpha::R29);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000200 for (int i = 0; i < count; ++i) {
201 if (argPreg[i] == Alpha::F16 || argPreg[i] == Alpha::F17 ||
202 argPreg[i] == Alpha::F18 || argPreg[i] == Alpha::F19 ||
203 argPreg[i] == Alpha::F20 || argPreg[i] == Alpha::F21)
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000204 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000205 assert(argOpc[i] == Alpha::CPYS && "Using BIS for a float??");
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000206 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000207 BuildMI(&BB, argOpc[i], 2,
208 argVreg[i]).addReg(argPreg[i]).addReg(argPreg[i]);
209 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000210
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000211 return ArgValues;
212}
213
214std::pair<SDOperand, SDOperand>
215AlphaTargetLowering::LowerCallTo(SDOperand Chain,
216 const Type *RetTy, SDOperand Callee,
217 ArgListTy &Args, SelectionDAG &DAG) {
218 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000219 if (Args.size() > 6)
220 NumBytes = (Args.size() - 6) * 8;
221
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000222 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
223 DAG.getConstant(NumBytes, getPointerTy()));
224 std::vector<SDOperand> args_to_use;
225 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000226 {
227 switch (getValueType(Args[i].second)) {
228 default: assert(0 && "Unexpected ValueType for argument!");
229 case MVT::i1:
230 case MVT::i8:
231 case MVT::i16:
232 case MVT::i32:
233 // Promote the integer to 64 bits. If the input type is signed use a
234 // sign extend, otherwise use a zero extend.
235 if (Args[i].second->isSigned())
236 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
237 else
238 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
239 break;
240 case MVT::i64:
241 case MVT::f64:
242 case MVT::f32:
243 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000244 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000245 args_to_use.push_back(Args[i].first);
246 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000247
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000248 std::vector<MVT::ValueType> RetVals;
249 MVT::ValueType RetTyVT = getValueType(RetTy);
250 if (RetTyVT != MVT::isVoid)
251 RetVals.push_back(RetTyVT);
252 RetVals.push_back(MVT::Other);
253
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000254 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
255 Chain, Callee, args_to_use), 0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000256 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
257 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
258 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000259 return std::make_pair(TheCall, Chain);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000260}
261
262std::pair<SDOperand, SDOperand>
263AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
264 //vastart just returns the address of the VarArgsFrameIndex slot.
265 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
266}
267
268std::pair<SDOperand,SDOperand> AlphaTargetLowering::
269LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000270 const Type *ArgTy, SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000271 abort();
272}
273
274
275std::pair<SDOperand, SDOperand> AlphaTargetLowering::
276LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
277 SelectionDAG &DAG) {
278 abort();
279}
280
281
282
283
284
285namespace {
286
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000287//===--------------------------------------------------------------------===//
288/// ISel - Alpha specific code to select Alpha machine instructions for
289/// SelectionDAG operations.
290//===--------------------------------------------------------------------===//
291class ISel : public SelectionDAGISel {
292
293 /// AlphaLowering - This object fully describes how to lower LLVM code to an
294 /// Alpha-specific SelectionDAG.
295 AlphaTargetLowering AlphaLowering;
296
297
298 /// ExprMap - As shared expressions are codegen'd, we keep track of which
299 /// vreg the value is produced in, so we only emit one copy of each compiled
300 /// tree.
301 static const unsigned notIn = (unsigned)(-1);
302 std::map<SDOperand, unsigned> ExprMap;
303
304 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
305 std::map<SDOperand, unsigned> CCInvMap;
306
307public:
308 ISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM)
309 {}
310
311 /// InstructionSelectBasicBlock - This callback is invoked by
312 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
313 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
314 // Codegen the basic block.
315 Select(DAG.getRoot());
316
317 // Clear state used for selection.
318 ExprMap.clear();
319 CCInvMap.clear();
320 }
321
322 unsigned SelectExpr(SDOperand N);
323 unsigned SelectExprFP(SDOperand N, unsigned Result);
324 void Select(SDOperand N);
325
326 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
327 void SelectBranchCC(SDOperand N);
328};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000329}
330
Andrew Lenharth65838902005-02-06 16:22:15 +0000331static unsigned GetSymVersion(unsigned opcode)
332{
333 switch (opcode) {
334 default: assert(0 && "unknown load or store"); return 0;
335 case Alpha::LDQ: return Alpha::LDQ_SYM;
336 case Alpha::LDS: return Alpha::LDS_SYM;
337 case Alpha::LDT: return Alpha::LDT_SYM;
338 case Alpha::LDL: return Alpha::LDL_SYM;
339 case Alpha::LDBU: return Alpha::LDBU_SYM;
340 case Alpha::LDWU: return Alpha::LDWU_SYM;
341 case Alpha::LDW: return Alpha::LDW_SYM;
342 case Alpha::LDB: return Alpha::LDB_SYM;
343 case Alpha::STQ: return Alpha::STQ_SYM;
344 case Alpha::STS: return Alpha::STS_SYM;
345 case Alpha::STT: return Alpha::STT_SYM;
346 case Alpha::STL: return Alpha::STL_SYM;
347 case Alpha::STW: return Alpha::STW_SYM;
348 case Alpha::STB: return Alpha::STB_SYM;
349 }
350}
351
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000352//Check to see if the load is a constant offset from a base register
353void ISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
354{
355 unsigned opcode = N.getOpcode();
356 if (opcode == ISD::ADD) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000357 if(N.getOperand(1).getOpcode() == ISD::Constant &&
358 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
359 { //Normal imm add
360 Reg = SelectExpr(N.getOperand(0));
361 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
362 return;
363 }
364 else if(N.getOperand(0).getOpcode() == ISD::Constant &&
365 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 32767)
366 {
367 Reg = SelectExpr(N.getOperand(1));
368 offset = cast<ConstantSDNode>(N.getOperand(0))->getValue();
369 return;
370 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000371 }
372 Reg = SelectExpr(N);
373 offset = 0;
374 return;
375}
376
Andrew Lenharth445171a2005-02-08 00:40:03 +0000377void ISel::SelectBranchCC(SDOperand N)
378{
379 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000380 MachineBasicBlock *Dest =
381 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
382 unsigned Opc = Alpha::WTF;
383
Andrew Lenharth445171a2005-02-08 00:40:03 +0000384 Select(N.getOperand(0)); //chain
385 SDOperand CC = N.getOperand(1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000386
Andrew Lenharth445171a2005-02-08 00:40:03 +0000387 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000388 {
389 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
390 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
391 //Dropping the CC is only useful if we are comparing to 0
392 bool isZero0 = false;
393 bool isZero1 = false;
394 bool isNE = false;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000395
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000396 if(SetCC->getOperand(0).getOpcode() == ISD::Constant &&
397 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0)
398 isZero0 = true;
399 if(SetCC->getOperand(1).getOpcode() == ISD::Constant &&
400 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0)
401 isZero1 = true;
402 if(SetCC->getCondition() == ISD::SETNE)
403 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000404
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000405 if (isZero0) {
Andrew Lenharth445171a2005-02-08 00:40:03 +0000406 switch (SetCC->getCondition()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000407 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
408 case ISD::SETEQ: Opc = Alpha::BEQ; break;
409 case ISD::SETLT: Opc = Alpha::BGT; break;
410 case ISD::SETLE: Opc = Alpha::BGE; break;
411 case ISD::SETGT: Opc = Alpha::BLT; break;
412 case ISD::SETGE: Opc = Alpha::BLE; break;
413 case ISD::SETULT: Opc = Alpha::BNE; break;
414 case ISD::SETUGT: assert(0 && "0 > (unsigned) x is never true"); break;
415 case ISD::SETULE: assert(0 && "0 <= (unsigned) x is always true"); break;
416 case ISD::SETUGE: Opc = Alpha::BEQ; break; //Technically you could have this CC
417 case ISD::SETNE: Opc = Alpha::BNE; break;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000418 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000419 unsigned Tmp1 = SelectExpr(SetCC->getOperand(1));
420 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
421 return;
422 } else if (isZero1) {
423 switch (SetCC->getCondition()) {
424 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
425 case ISD::SETEQ: Opc = Alpha::BEQ; break;
426 case ISD::SETLT: Opc = Alpha::BLT; break;
427 case ISD::SETLE: Opc = Alpha::BLE; break;
428 case ISD::SETGT: Opc = Alpha::BGT; break;
429 case ISD::SETGE: Opc = Alpha::BGE; break;
430 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
431 case ISD::SETUGT: Opc = Alpha::BNE; break;
432 case ISD::SETULE: Opc = Alpha::BEQ; break; //Technically you could have this CC
433 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
434 case ISD::SETNE: Opc = Alpha::BNE; break;
435 }
436 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
437 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
438 return;
439 } else {
440 unsigned Tmp1 = SelectExpr(CC);
441 if (isNE)
442 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
443 else
444 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000445 return;
446 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000447 } else { //FP
448 //Any comparison between 2 values should be codegened as an folded branch, as moving
449 //CC to the integer register is very expensive
450 //for a cmp b: c = a - b;
451 //a = b: c = 0
452 //a < b: c < 0
453 //a > b: c > 0
454 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
455 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
456 unsigned Tmp3 = MakeReg(MVT::f64);
457 BuildMI(BB, Alpha::SUBT, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
458
459 switch (SetCC->getCondition()) {
460 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
461 case ISD::SETEQ: Opc = Alpha::FBEQ; break;
462 case ISD::SETLT: Opc = Alpha::FBLT; break;
463 case ISD::SETLE: Opc = Alpha::FBLE; break;
464 case ISD::SETGT: Opc = Alpha::FBGT; break;
465 case ISD::SETGE: Opc = Alpha::FBGE; break;
466 case ISD::SETNE: Opc = Alpha::FBNE; break;
467 }
468 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000469 return;
470 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000471 abort(); //Should never be reached
472 } else {
473 //Giveup and do the stupid thing
474 unsigned Tmp1 = SelectExpr(CC);
475 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
476 return;
477 }
Andrew Lenharth445171a2005-02-08 00:40:03 +0000478 abort(); //Should never be reached
479}
480
Andrew Lenharth40831c52005-01-28 06:57:18 +0000481unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
482{
483 unsigned Tmp1, Tmp2, Tmp3;
484 unsigned Opc = 0;
485 SDNode *Node = N.Val;
486 MVT::ValueType DestType = N.getValueType();
487 unsigned opcode = N.getOpcode();
488
489 switch (opcode) {
490 default:
491 Node->dump();
492 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +0000493
Andrew Lenharth9818c052005-02-05 13:19:12 +0000494 case ISD::SELECT:
495 {
496 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
497 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
498 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000499
500
501 // Spill the cond to memory and reload it from there.
502 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
503 MachineFunction *F = BB->getParent();
504 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
505 unsigned Tmp4 = MakeReg(MVT::f64);
506 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addFrameIndex(FrameIdx).addReg(Alpha::F31);
507 BuildMI(BB, Alpha::LDT, 2, Tmp4).addFrameIndex(FrameIdx).addReg(Alpha::F31);
508 //now ideally, we don't have to do anything to the flag...
Andrew Lenharth9818c052005-02-05 13:19:12 +0000509 // Get the condition into the zero flag.
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000510 BuildMI(BB, Alpha::FCMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp4);
Andrew Lenharth9818c052005-02-05 13:19:12 +0000511 return Result;
512 }
513
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000514 case ISD::FP_ROUND:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000515 assert (DestType == MVT::f32 &&
516 N.getOperand(0).getValueType() == MVT::f64 &&
517 "only f64 to f32 conversion supported here");
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000518 Tmp1 = SelectExpr(N.getOperand(0));
519 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
520 return Result;
521
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000522 case ISD::FP_EXTEND:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000523 assert (DestType == MVT::f64 &&
524 N.getOperand(0).getValueType() == MVT::f32 &&
525 "only f32 to f64 conversion supported here");
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000526 Tmp1 = SelectExpr(N.getOperand(0));
527 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
528 return Result;
529
Andrew Lenharth2c594352005-01-29 15:42:07 +0000530 case ISD::CopyFromReg:
531 {
532 // Make sure we generate both values.
533 if (Result != notIn)
534 ExprMap[N.getValue(1)] = notIn; // Generate the token
535 else
536 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
537
538 SDOperand Chain = N.getOperand(0);
539
540 Select(Chain);
541 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
542 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
543 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
544 return Result;
545 }
546
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000547 case ISD::LOAD:
548 {
549 // Make sure we generate both values.
550 if (Result != notIn)
551 ExprMap[N.getValue(1)] = notIn; // Generate the token
552 else
553 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000554
Andrew Lenharth29219162005-02-07 06:31:44 +0000555 DestType = N.getValue(0).getValueType();
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000556
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000557 SDOperand Chain = N.getOperand(0);
558 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000559 Select(Chain);
Andrew Lenharth65838902005-02-06 16:22:15 +0000560 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
561
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000562 if (Address.getOpcode() == ISD::GlobalAddress) {
563 AlphaLowering.restoreGP(BB);
564 Opc = GetSymVersion(Opc);
565 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
566 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000567 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000568 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000569 Opc = GetSymVersion(Opc);
Andrew Lenharth97127a12005-02-05 17:41:39 +0000570 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000571 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000572 else if(Address.getOpcode() == ISD::FrameIndex) {
573 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
574 BuildMI(BB, Opc, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
575 } else {
576 long offset;
577 SelectAddr(Address, Tmp1, offset);
578 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
579 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000580 return Result;
581 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000582 case ISD::ConstantFP:
583 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
584 if (CN->isExactlyValue(+0.0)) {
585 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000586 } else if ( CN->isExactlyValue(-0.0)) {
587 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +0000588 } else {
589 abort();
590 }
591 }
592 return Result;
593
594 case ISD::MUL:
595 case ISD::ADD:
596 case ISD::SUB:
597 case ISD::SDIV:
598 switch( opcode ) {
599 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
600 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
601 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
602 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
603 };
604 Tmp1 = SelectExpr(N.getOperand(0));
605 Tmp2 = SelectExpr(N.getOperand(1));
606 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
607 return Result;
608
Andrew Lenharth2c594352005-01-29 15:42:07 +0000609 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000610 {
611 //include a conversion sequence for float loads to double
612 if (Result != notIn)
613 ExprMap[N.getValue(1)] = notIn; // Generate the token
614 else
615 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
616
Andrew Lenhartha549deb2005-02-07 05:33:15 +0000617 Tmp1 = MakeReg(MVT::f32);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000618
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000619 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
620 "EXTLOAD not from f32");
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000621 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
622
623 SDOperand Chain = N.getOperand(0);
624 SDOperand Address = N.getOperand(1);
625 Select(Chain);
626
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000627 if (Address.getOpcode() == ISD::GlobalAddress) {
628 AlphaLowering.restoreGP(BB);
629 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
630 }
631 else if (ConstantPoolSDNode *CP =
632 dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
633 {
634 AlphaLowering.restoreGP(BB);
635 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addConstantPoolIndex(CP->getIndex());
636 }
637 else if(Address.getOpcode() == ISD::FrameIndex) {
638 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
639 BuildMI(BB, Alpha::LDS, 2, Tmp1).addFrameIndex(Tmp2).addReg(Alpha::F31);
640 } else {
641 long offset;
642 SelectAddr(Address, Tmp2, offset);
643 BuildMI(BB, Alpha::LDS, 1, Tmp1).addImm(offset).addReg(Tmp2);
644 }
Andrew Lenharth29219162005-02-07 06:31:44 +0000645 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000646 return Result;
647 }
Andrew Lenharth2c594352005-01-29 15:42:07 +0000648
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000649 case ISD::UINT_TO_FP:
650 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000651 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000652 assert (N.getOperand(0).getValueType() == MVT::i64
653 && "only quads can be loaded from");
Andrew Lenharth40831c52005-01-28 06:57:18 +0000654 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +0000655 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000656
657 //The hard way:
658 // Spill the integer to memory and reload it from there.
659 unsigned Size = MVT::getSizeInBits(MVT::i64)/8;
660 MachineFunction *F = BB->getParent();
661 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
662
Andrew Lenharth7efadce2005-01-31 01:44:26 +0000663 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addFrameIndex(FrameIdx).addReg(Alpha::F31);
664 BuildMI(BB, Alpha::LDT, 2, Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
665 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
666 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000667
668 //The easy way: doesn't work
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000669 // //so these instructions are not supported on ev56
670 // Opc = DestType == MVT::f64 ? Alpha::ITOFT : Alpha::ITOFS;
671 // BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
672 // Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
673 // BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000674
Andrew Lenharth40831c52005-01-28 06:57:18 +0000675 return Result;
676 }
677 }
678 assert(0 && "should not get here");
679 return 0;
680}
681
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000682unsigned ISel::SelectExpr(SDOperand N) {
683 unsigned Result;
684 unsigned Tmp1, Tmp2, Tmp3;
685 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000686 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000687
688 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000689 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000690
691 unsigned &Reg = ExprMap[N];
692 if (Reg) return Reg;
693
694 if (N.getOpcode() != ISD::CALL)
695 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000696 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000697 else {
698 // If this is a call instruction, make sure to prepare ALL of the result
699 // values as well as the chain.
700 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000701 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000702 else {
703 Result = MakeReg(Node->getValueType(0));
704 ExprMap[N.getValue(0)] = Result;
705 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
706 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000707 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000708 }
709 }
710
Andrew Lenharth22088bb2005-02-02 15:05:33 +0000711 if (DestType == MVT::f64 || DestType == MVT::f32 ||
Andrew Lenharth06342c32005-02-07 06:21:37 +0000712 (
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000713 (opcode == ISD::LOAD || opcode == ISD::CopyFromReg ||
714 opcode == ISD::EXTLOAD) &&
715 (N.getValue(0).getValueType() == MVT::f32 ||
716 N.getValue(0).getValueType() == MVT::f64)
Andrew Lenharth06342c32005-02-07 06:21:37 +0000717 )
718 )
Andrew Lenharth40831c52005-01-28 06:57:18 +0000719 return SelectExprFP(N, Result);
720
721 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000722 default:
723 Node->dump();
724 assert(0 && "Node not handled!\n");
725
Andrew Lenharth2c594352005-01-29 15:42:07 +0000726 case ISD::ConstantPool:
727 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
728 AlphaLowering.restoreGP(BB);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000729 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000730 return Result;
731
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000732 case ISD::FrameIndex:
733 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Andrew Lenharth684f2292005-01-30 00:35:27 +0000734 BuildMI(BB, Alpha::LDA, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000735 return Result;
736
737 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +0000738 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000739 case ISD::SEXTLOAD:
Andrew Lenhartha549deb2005-02-07 05:33:15 +0000740 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000741 {
742 // Make sure we generate both values.
743 if (Result != notIn)
744 ExprMap[N.getValue(1)] = notIn; // Generate the token
745 else
746 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000747
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000748 SDOperand Chain = N.getOperand(0);
749 SDOperand Address = N.getOperand(1);
750 Select(Chain);
751
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000752 assert(Node->getValueType(0) == MVT::i64 &&
753 "Unknown type to sign extend to.");
Andrew Lenharth03824012005-02-07 05:55:55 +0000754 if (opcode == ISD::LOAD)
755 Opc = Alpha::LDQ;
756 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000757 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
758 default: Node->dump(); assert(0 && "Bad sign extend!");
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000759 case MVT::i32: Opc = Alpha::LDL;
760 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
761 case MVT::i16: Opc = Alpha::LDWU;
762 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +0000763 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000764 case MVT::i8: Opc = Alpha::LDBU;
765 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000766 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000767
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000768 if (Address.getOpcode() == ISD::GlobalAddress) {
769 AlphaLowering.restoreGP(BB);
770 Opc = GetSymVersion(Opc);
771 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
772 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000773 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
774 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000775 Opc = GetSymVersion(Opc);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000776 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000777 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000778 else if(Address.getOpcode() == ISD::FrameIndex) {
779 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
780 BuildMI(BB, Opc, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
781 } else {
782 long offset;
783 SelectAddr(Address, Tmp1, offset);
784 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
785 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000786 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000787 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000788
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000789 case ISD::GlobalAddress:
790 AlphaLowering.restoreGP(BB);
791 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
792 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
793 return Result;
794
795 case ISD::CALL:
796 {
797 Select(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000798
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000799 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000800 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000801
802 //grab the arguments
803 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000804 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000805 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000806 argvregs.push_back(SelectExpr(N.getOperand(i)));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000807
Andrew Lenharth684f2292005-01-30 00:35:27 +0000808 //in reg args
809 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000810 {
811 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
812 Alpha::R19, Alpha::R20, Alpha::R21};
813 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
814 Alpha::F19, Alpha::F20, Alpha::F21};
815 switch(N.getOperand(i+2).getValueType()) {
816 default:
817 Node->dump();
818 N.getOperand(i).Val->dump();
819 std::cerr << "Type for " << i << " is: " <<
820 N.getOperand(i+2).getValueType() << "\n";
821 assert(0 && "Unknown value type for call");
822 case MVT::i1:
823 case MVT::i8:
824 case MVT::i16:
825 case MVT::i32:
826 case MVT::i64:
827 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
828 break;
829 case MVT::f32:
830 case MVT::f64:
831 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
832 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000833 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000834 }
Andrew Lenharth684f2292005-01-30 00:35:27 +0000835 //in mem args
836 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000837 {
838 switch(N.getOperand(i+2).getValueType()) {
839 default:
840 Node->dump();
841 N.getOperand(i).Val->dump();
842 std::cerr << "Type for " << i << " is: " <<
843 N.getOperand(i+2).getValueType() << "\n";
844 assert(0 && "Unknown value type for call");
845 case MVT::i1:
846 case MVT::i8:
847 case MVT::i16:
848 case MVT::i32:
849 case MVT::i64:
850 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
851 break;
852 case MVT::f32:
853 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
854 break;
855 case MVT::f64:
856 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
857 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000858 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000859 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000860 //build the right kind of call
861 if (GlobalAddressSDNode *GASD =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000862 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000863 {
Andrew Lenharth3e315922005-02-10 20:10:38 +0000864 //if (GASD->getGlobal()->isExternal()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000865 //use safe calling convention
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000866 AlphaLowering.restoreGP(BB);
867 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal(),true);
Andrew Lenharth3e315922005-02-10 20:10:38 +0000868 //} else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000869 //use PC relative branch call
Andrew Lenharth3e315922005-02-10 20:10:38 +0000870 //BuildMI(BB, Alpha::BSR, 1, Alpha::R26).addGlobalAddress(GASD->getGlobal(),true);
871 //}
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000872 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000873 else if (ExternalSymbolSDNode *ESSDN =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000874 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000875 {
876 AlphaLowering.restoreGP(BB);
877 BuildMI(BB, Alpha::CALL, 0).addExternalSymbol(ESSDN->getSymbol(), true);
878 } else {
879 //no need to restore GP as we are doing an indirect call
880 Tmp1 = SelectExpr(N.getOperand(1));
881 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
882 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
883 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000884
885 //push the result into a virtual register
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000886
887 switch (Node->getValueType(0)) {
888 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000889 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000890 case MVT::i1:
891 case MVT::i8:
892 case MVT::i16:
893 case MVT::i32:
894 case MVT::i64:
895 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
896 break;
897 case MVT::f32:
898 case MVT::f64:
899 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
900 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000901 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000902 return Result+N.ResNo;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000903 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000904
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000905 case ISD::SIGN_EXTEND:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000906 abort();
907
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000908 case ISD::SIGN_EXTEND_INREG:
909 {
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000910 //Alpha has instructions for a bunch of signed 32 bit stuff
911 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000912 {
913 switch (N.getOperand(0).getOpcode()) {
914 case ISD::ADD:
915 case ISD::SUB:
916 case ISD::MUL:
917 {
918 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
919 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
920 //FIXME: first check for Scaled Adds and Subs!
921 if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
922 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
923 { //Normal imm add/sub
924 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth093f3272005-02-12 21:11:17 +0000925 //if the value was really originally a i32, skip the up conversion
926 if (N.getOperand(0).getOperand(0).getOpcode() == ISD::SIGN_EXTEND_INREG &&
927 dyn_cast<MVTSDNode>(N.getOperand(0).getOperand(0).Val)
928 ->getExtraValueType() == MVT::i32)
929 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
930 else
931 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000932 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
933 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000934 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000935 else
936 { //Normal add/sub
937 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULLi : Alpha::SUBL);
Andrew Lenharth093f3272005-02-12 21:11:17 +0000938 //if the value was really originally a i32, skip the up conversion
939 if (N.getOperand(0).getOperand(0).getOpcode() == ISD::SIGN_EXTEND_INREG &&
940 dyn_cast<MVTSDNode>(N.getOperand(0).getOperand(0).Val)
941 ->getExtraValueType() == MVT::i32)
942 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
943 else
944 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
945 //if the value was really originally a i32, skip the up conversion
946 if (N.getOperand(0).getOperand(1).getOpcode() == ISD::SIGN_EXTEND_INREG &&
947 dyn_cast<MVTSDNode>(N.getOperand(0).getOperand(1).Val)
948 ->getExtraValueType() == MVT::i32)
949 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
950 else
951 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
952
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000953 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
954 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
955 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
956 }
957 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000958 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000959 default: break; //Fall Though;
960 }
961 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000962 Tmp1 = SelectExpr(N.getOperand(0));
963 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000964 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000965 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000966 {
967 default:
968 Node->dump();
969 assert(0 && "Sign Extend InReg not there yet");
970 break;
971 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000972 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000973 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000974 break;
975 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000976 case MVT::i16:
977 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
978 break;
979 case MVT::i8:
980 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
981 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +0000982 case MVT::i1:
983 Tmp2 = MakeReg(MVT::i64);
984 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenharth7536eea2005-02-12 20:42:09 +0000985 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::F31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +0000986 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000987 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000988 return Result;
989 }
990 case ISD::ZERO_EXTEND_INREG:
991 {
992 Tmp1 = SelectExpr(N.getOperand(0));
993 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000994 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000995 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000996 {
997 default:
998 Node->dump();
999 assert(0 && "Zero Extend InReg not there yet");
1000 break;
1001 case MVT::i32: Tmp2 = 0xf0; break;
1002 case MVT::i16: Tmp2 = 0xfc; break;
1003 case MVT::i8: Tmp2 = 0xfe; break;
1004 case MVT::i1: //handle this one special
1005 BuildMI(BB, Alpha::ANDi, 2, Result).addReg(Tmp1).addImm(1);
1006 return Result;
1007 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001008 BuildMI(BB, Alpha::ZAPi, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001009 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001010 }
1011
1012 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001013 {
1014 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1015 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1016 bool isConst1 = false;
1017 bool isConst2 = false;
1018 int dir;
Andrew Lenharth9818c052005-02-05 13:19:12 +00001019
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001020 //Tmp1 = SelectExpr(N.getOperand(0));
1021 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001022 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1023 isConst1 = true;
1024 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001025 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1026 isConst2 = true;
1027
1028 switch (SetCC->getCondition()) {
1029 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1030 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001031 case ISD::SETLT:
1032 Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
1033 case ISD::SETLE:
1034 Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
1035 case ISD::SETGT:
1036 Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
1037 case ISD::SETGE:
1038 Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
1039 case ISD::SETULT:
1040 Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
1041 case ISD::SETUGT:
1042 Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
1043 case ISD::SETULE:
1044 Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
1045 case ISD::SETUGE:
1046 Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001047 case ISD::SETNE: {//Handle this one special
1048 //std::cerr << "Alpha does not have a setne.\n";
1049 //abort();
1050 Tmp1 = SelectExpr(N.getOperand(0));
1051 Tmp2 = SelectExpr(N.getOperand(1));
1052 Tmp3 = MakeReg(MVT::i64);
1053 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001054 //Remeber we have the Inv for this CC
1055 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001056 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001057 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001058 return Result;
1059 }
1060 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001061 if (dir == 1) {
1062 Tmp1 = SelectExpr(N.getOperand(0));
1063 if (isConst2) {
1064 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1065 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1066 } else {
1067 Tmp2 = SelectExpr(N.getOperand(1));
1068 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1069 }
1070 } else if (dir == 2) {
1071 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001072 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001073 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1074 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1075 } else {
1076 Tmp2 = SelectExpr(N.getOperand(0));
1077 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1078 }
1079 } else { //dir == 0
1080 if (isConst1) {
1081 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1082 Tmp2 = SelectExpr(N.getOperand(1));
1083 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
1084 } else if (isConst2) {
1085 Tmp1 = SelectExpr(N.getOperand(0));
1086 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1087 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1088 } else {
1089 Tmp1 = SelectExpr(N.getOperand(0));
1090 Tmp2 = SelectExpr(N.getOperand(1));
1091 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1092 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001093 }
1094 } else {
1095 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
1096 bool rev = false;
1097 bool inv = false;
1098
1099 switch (SetCC->getCondition()) {
1100 default: Node->dump(); assert(0 && "Unknown FP comparison!");
1101 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
1102 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
1103 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
1104 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
1105 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
1106 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
1107 }
1108
1109 Tmp1 = SelectExpr(N.getOperand(0));
1110 Tmp2 = SelectExpr(N.getOperand(1));
1111 //Can only compare doubles, and dag won't promote for me
1112 if (SetCC->getOperand(0).getValueType() == MVT::f32)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001113 {
Andrew Lenharth572af902005-02-14 05:41:43 +00001114 std::cerr << "Setcc On float?\n";
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001115 Tmp3 = MakeReg(MVT::f64);
1116 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
1117 Tmp1 = Tmp3;
1118 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001119 if (SetCC->getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001120 {
Andrew Lenharth572af902005-02-14 05:41:43 +00001121 std::cerr << "Setcc On float?\n";
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001122 Tmp3 = MakeReg(MVT::f64);
1123 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
Andrew Lenharth572af902005-02-14 05:41:43 +00001124 Tmp2 = Tmp3;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001125 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001126
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001127 if (rev) std::swap(Tmp1, Tmp2);
1128 Tmp3 = MakeReg(MVT::f64);
1129 //do the comparison
1130 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1131
1132 //now arrange for Result (int) to have a 1 or 0
1133
1134 // Spill the FP to memory and reload it from there.
1135 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1136 MachineFunction *F = BB->getParent();
1137 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
1138 unsigned Tmp4 = MakeReg(MVT::f64);
1139 BuildMI(BB, Alpha::CVTTQ, 1, Tmp4).addReg(Tmp3);
1140 BuildMI(BB, Alpha::STT, 3).addReg(Tmp4).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1141 unsigned Tmp5 = MakeReg(MVT::i64);
1142 BuildMI(BB, Alpha::LDQ, 2, Tmp5).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharth9818c052005-02-05 13:19:12 +00001143
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001144 //now, set result based on Tmp5
1145 //Set Tmp6 if fp cmp was false
1146 unsigned Tmp6 = MakeReg(MVT::i64);
1147 BuildMI(BB, Alpha::CMPEQ, 2, Tmp6).addReg(Tmp5).addReg(Alpha::R31);
1148 //and invert
1149 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp6).addReg(Alpha::R31);
1150
1151 }
1152 // else
1153 // {
1154 // Node->dump();
1155 // assert(0 && "Not a setcc in setcc");
1156 // }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001157 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001158 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001159 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001160
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001161 case ISD::CopyFromReg:
1162 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001163 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001164 if (Result != notIn)
1165 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001166 else
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001167 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth40831c52005-01-28 06:57:18 +00001168
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001169 SDOperand Chain = N.getOperand(0);
1170
1171 Select(Chain);
1172 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1173 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1174 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1175 return Result;
1176 }
1177
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001178 //Most of the plain arithmetic and logic share the same form, and the same
1179 //constant immediate test
1180 case ISD::AND:
1181 case ISD::OR:
1182 case ISD::XOR:
1183 case ISD::SHL:
1184 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001185 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001186 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001187 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1188 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001189 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001190 {
1191 switch(opcode) {
1192 case ISD::AND: Opc = Alpha::ANDi; break;
1193 case ISD::OR: Opc = Alpha::BISi; break;
1194 case ISD::XOR: Opc = Alpha::XORi; break;
1195 case ISD::SHL: Opc = Alpha::SLi; break;
1196 case ISD::SRL: Opc = Alpha::SRLi; break;
1197 case ISD::SRA: Opc = Alpha::SRAi; break;
1198 case ISD::MUL: Opc = Alpha::MULQi; break;
1199 };
1200 Tmp1 = SelectExpr(N.getOperand(0));
1201 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1202 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1203 } else {
1204 switch(opcode) {
1205 case ISD::AND: Opc = Alpha::AND; break;
1206 case ISD::OR: Opc = Alpha::BIS; break;
1207 case ISD::XOR: Opc = Alpha::XOR; break;
1208 case ISD::SHL: Opc = Alpha::SL; break;
1209 case ISD::SRL: Opc = Alpha::SRL; break;
1210 case ISD::SRA: Opc = Alpha::SRA; break;
1211 case ISD::MUL: Opc = Alpha::MULQ; break;
1212 };
1213 Tmp1 = SelectExpr(N.getOperand(0));
1214 Tmp2 = SelectExpr(N.getOperand(1));
1215 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1216 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001217 return Result;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001218
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001219 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001220 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001221 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001222 bool isAdd = opcode == ISD::ADD;
1223
1224 //FIXME: first check for Scaled Adds and Subs!
1225 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001226 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001227 { //Normal imm add/sub
1228 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1229 Tmp1 = SelectExpr(N.getOperand(0));
1230 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1231 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1232 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001233 else if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001234 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001235 { //LDA //FIXME: expand the above condition a bit
1236 Tmp1 = SelectExpr(N.getOperand(0));
1237 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1238 if (!isAdd)
1239 Tmp2 = -Tmp2;
1240 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
1241 } else {
1242 //Normal add/sub
1243 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1244 Tmp1 = SelectExpr(N.getOperand(0));
1245 Tmp2 = SelectExpr(N.getOperand(1));
1246 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1247 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001248 return Result;
1249 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001250
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001251 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00001252 case ISD::SREM:
1253 case ISD::SDIV:
1254 case ISD::UDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001255 //FIXME: alpha really doesn't support any of these operations,
1256 // the ops are expanded into special library calls with
1257 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001258 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00001259 switch(opcode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001260 case ISD::UREM: AlphaLowering.restoreGP(BB); Opc = Alpha::REMQU; break;
1261 case ISD::SREM: AlphaLowering.restoreGP(BB); Opc = Alpha::REMQ; break;
1262 case ISD::UDIV: AlphaLowering.restoreGP(BB); Opc = Alpha::DIVQU; break;
1263 case ISD::SDIV: AlphaLowering.restoreGP(BB); Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001264 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001265 Tmp1 = SelectExpr(N.getOperand(0));
1266 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth02981182005-01-26 01:24:38 +00001267 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001268 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001269
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001270 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001271 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001272 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001273 assert (DestType == MVT::i64 && "only quads can be loaded to");
1274 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001275 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001276 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1277
1278 //The hard way:
1279 // Spill the integer to memory and reload it from there.
1280 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1281 MachineFunction *F = BB->getParent();
1282 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
1283
1284 //CVTTQ STT LDQ
1285 //CVTST CVTTQ STT LDQ
1286 if (SrcType == MVT::f32)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001287 {
1288 Tmp2 = MakeReg(MVT::f64);
1289 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1290 Tmp1 = Tmp2;
1291 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001292 Tmp2 = MakeReg(MVT::f64);
1293 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
1294 BuildMI(BB, Alpha::STT, 3).addReg(Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1295 BuildMI(BB, Alpha::LDQ, 2, Result).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1296
1297 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001298 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001299
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001300 // // case ISD::FP_TO_UINT:
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001301
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001302 case ISD::SELECT:
1303 {
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001304 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001305 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1306 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001307 // Get the condition into the zero flag.
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001308 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001309 return Result;
1310 }
1311
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001312 case ISD::Constant:
1313 {
Andrew Lenharth22d5a412005-02-02 00:51:15 +00001314 unsigned long val = cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001315 if (val < 32000 && (long)val > -32000)
1316 BuildMI(BB, Alpha::LOAD_IMM, 1, Result).addImm(val);
1317 else {
1318 MachineConstantPool *CP = BB->getParent()->getConstantPool();
1319 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
1320 unsigned CPI = CP->getConstantPoolIndex(C);
1321 AlphaLowering.restoreGP(BB);
1322 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
1323 }
1324 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001325 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001326 }
1327
1328 return 0;
1329}
1330
1331void ISel::Select(SDOperand N) {
1332 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00001333 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001334
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001335 // FIXME: Disable for our current expansion model!
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001336 if (/*!N->hasOneUse() &&*/ !ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001337 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001338
1339 SDNode *Node = N.Val;
Andrew Lenharth760270d2005-02-07 23:02:23 +00001340
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001341
Andrew Lenharth760270d2005-02-07 23:02:23 +00001342 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001343
1344 default:
1345 Node->dump(); std::cerr << "\n";
1346 assert(0 && "Node not handled yet!");
1347
1348 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00001349 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001350 return;
1351 }
1352
1353 case ISD::BR: {
1354 MachineBasicBlock *Dest =
1355 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1356
1357 Select(N.getOperand(0));
1358 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
1359 return;
1360 }
1361
1362 case ISD::ImplicitDef:
1363 Select(N.getOperand(0));
1364 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
1365 return;
1366
1367 case ISD::EntryToken: return; // Noop
1368
1369 case ISD::TokenFactor:
1370 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1371 Select(Node->getOperand(i));
1372
1373 //N.Val->dump(); std::cerr << "\n";
1374 //assert(0 && "Node not handled yet!");
1375
1376 return;
1377
1378 case ISD::CopyToReg:
1379 Select(N.getOperand(0));
1380 Tmp1 = SelectExpr(N.getOperand(1));
1381 Tmp2 = cast<RegSDNode>(N)->getReg();
1382
1383 if (Tmp1 != Tmp2) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001384 if (N.getOperand(1).getValueType() == MVT::f64 ||
1385 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00001386 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1387 else
1388 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001389 }
1390 return;
1391
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001392 case ISD::RET:
1393 switch (N.getNumOperands()) {
1394 default:
1395 std::cerr << N.getNumOperands() << "\n";
1396 for (unsigned i = 0; i < N.getNumOperands(); ++i)
1397 std::cerr << N.getOperand(i).getValueType() << "\n";
1398 Node->dump();
1399 assert(0 && "Unknown return instruction!");
1400 case 2:
1401 Select(N.getOperand(0));
1402 Tmp1 = SelectExpr(N.getOperand(1));
1403 switch (N.getOperand(1).getValueType()) {
1404 default: Node->dump();
1405 assert(0 && "All other types should have been promoted!!");
1406 case MVT::f64:
1407 case MVT::f32:
1408 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1409 break;
1410 case MVT::i32:
1411 case MVT::i64:
1412 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1413 break;
1414 }
1415 break;
1416 case 1:
1417 Select(N.getOperand(0));
1418 break;
1419 }
1420 //Tmp2 = AlphaLowering.getRetAddr();
1421 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
1422 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
1423 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001424
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001425 case ISD::TRUNCSTORE:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001426 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001427 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001428 SDOperand Chain = N.getOperand(0);
1429 SDOperand Value = N.getOperand(1);
1430 SDOperand Address = N.getOperand(2);
1431 Select(Chain);
1432
1433 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00001434
1435 if (opcode == ISD::STORE) {
1436 switch(Value.getValueType()) {
1437 default: assert(0 && "unknown Type in store");
1438 case MVT::i64: Opc = Alpha::STQ; break;
1439 case MVT::f64: Opc = Alpha::STT; break;
1440 case MVT::f32: Opc = Alpha::STS; break;
1441 }
1442 } else { //ISD::TRUNCSTORE
1443 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1444 default: assert(0 && "unknown Type in store");
1445 case MVT::i1: //FIXME: DAG does not promote this load
1446 case MVT::i8: Opc = Alpha::STB; break;
1447 case MVT::i16: Opc = Alpha::STW; break;
1448 case MVT::i32: Opc = Alpha::STL; break;
1449 }
Andrew Lenharth65838902005-02-06 16:22:15 +00001450 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00001451
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001452 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001453 {
1454 AlphaLowering.restoreGP(BB);
1455 Opc = GetSymVersion(Opc);
1456 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1457 }
Andrew Lenharth05380342005-02-07 05:07:00 +00001458 else if(Address.getOpcode() == ISD::FrameIndex)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001459 {
1460 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1461 BuildMI(BB, Opc, 3).addReg(Tmp1).addFrameIndex(Tmp2).addReg(Alpha::F31);
1462 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001463 else
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001464 {
1465 long offset;
1466 SelectAddr(Address, Tmp2, offset);
1467 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1468 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001469 return;
1470 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001471
1472 case ISD::EXTLOAD:
1473 case ISD::SEXTLOAD:
1474 case ISD::ZEXTLOAD:
1475 case ISD::LOAD:
1476 case ISD::CopyFromReg:
1477 case ISD::CALL:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001478 // case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001479 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001480 SelectExpr(N);
1481 return;
1482
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001483 case ISD::ADJCALLSTACKDOWN:
1484 case ISD::ADJCALLSTACKUP:
1485 Select(N.getOperand(0));
1486 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1487
1488 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? Alpha::ADJUSTSTACKDOWN :
1489 Alpha::ADJUSTSTACKUP;
1490 BuildMI(BB, Opc, 1).addImm(Tmp1);
1491 return;
1492 }
1493 assert(0 && "Should not be reached!");
1494}
1495
1496
1497/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1498/// into a machine code representation using pattern matching and a machine
1499/// description file.
1500///
1501FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
1502 return new ISel(TM);
1503}