blob: fc74a71bd0ece31cdefe32ae6d3f4c7885208f37 [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"
Andrew Lenharth032f2352005-02-22 21:59:48 +000029#include "llvm/Support/Debug.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000030#include <set>
Andrew Lenharth684f2292005-01-30 00:35:27 +000031#include <algorithm>
Andrew Lenharth304d0f32005-01-22 23:41:55 +000032using namespace llvm;
33
34//===----------------------------------------------------------------------===//
35// AlphaTargetLowering - Alpha Implementation of the TargetLowering interface
36namespace {
37 class AlphaTargetLowering : public TargetLowering {
38 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
39 unsigned GP; //GOT vreg
40 public:
41 AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
42 // Set up the TargetLowering object.
Andrew Lenharth3d65d312005-01-27 03:49:45 +000043 //I am having problems with shr n ubyte 1
Andrew Lenharth879ef222005-02-02 17:00:21 +000044 setShiftAmountType(MVT::i64);
45 setSetCCResultType(MVT::i64);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000046
Andrew Lenharth304d0f32005-01-22 23:41:55 +000047 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
48 addRegisterClass(MVT::f64, Alpha::FPRCRegisterClass);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000049 addRegisterClass(MVT::f32, Alpha::FPRCRegisterClass);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000050
Andrew Lenharthd2bb9602005-01-27 07:50:35 +000051 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000052
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +000053 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000054 setOperationAction(ISD::ZEXTLOAD , MVT::i32 , Expand);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000055
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +000056 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000057 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
58 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
59
Andrew Lenharth9818c052005-02-05 13:19:12 +000060 setOperationAction(ISD::SREM , MVT::f32 , Expand);
61 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +000062
Andrew Lenharth8d163d22005-02-02 05:49:42 +000063 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
Andrew Lenharth9818c052005-02-05 13:19:12 +000064 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
65 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
66
Andrew Lenharth572af902005-02-14 05:41:43 +000067 setOperationAction(ISD::SETCC , MVT::f32, Promote);
68
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000069 computeRegisterProperties();
Andrew Lenharth304d0f32005-01-22 23:41:55 +000070
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000071 addLegalFPImmediate(+0.0); //F31
72 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +000073 }
74
75 /// LowerArguments - This hook must be implemented to indicate how we should
76 /// lower the arguments for the specified function, into the specified DAG.
77 virtual std::vector<SDOperand>
78 LowerArguments(Function &F, SelectionDAG &DAG);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000079
Andrew Lenharth304d0f32005-01-22 23:41:55 +000080 /// LowerCallTo - This hook lowers an abstract call to a function into an
81 /// actual call.
82 virtual std::pair<SDOperand, SDOperand>
83 LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
84 ArgListTy &Args, SelectionDAG &DAG);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000085
Andrew Lenharth304d0f32005-01-22 23:41:55 +000086 virtual std::pair<SDOperand, SDOperand>
87 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000088
Andrew Lenharth304d0f32005-01-22 23:41:55 +000089 virtual std::pair<SDOperand,SDOperand>
90 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
91 const Type *ArgTy, SelectionDAG &DAG);
92
93 virtual std::pair<SDOperand, SDOperand>
94 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
95 SelectionDAG &DAG);
96
97 void restoreGP(MachineBasicBlock* BB)
98 {
99 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
100 }
101 };
102}
103
104//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
105
106//For now, just use variable size stack frame format
107
108//In a standard call, the first six items are passed in registers $16
109//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
110//of argument-to-register correspondence.) The remaining items are
111//collected in a memory argument list that is a naturally aligned
112//array of quadwords. In a standard call, this list, if present, must
113//be passed at 0(SP).
114//7 ... n 0(SP) ... (n-7)*8(SP)
115
116std::vector<SDOperand>
117AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
118{
119 std::vector<SDOperand> ArgValues;
120
121 // //#define FP $15
122 // //#define RA $26
123 // //#define PV $27
124 // //#define GP $29
125 // //#define SP $30
126
127 // assert(0 && "TODO");
128 MachineFunction &MF = DAG.getMachineFunction();
Andrew Lenharth05380342005-02-07 05:07:00 +0000129 MachineFrameInfo*MFI = MF.getFrameInfo();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000130
131 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
132 MachineBasicBlock& BB = MF.front();
133
134 //Handle the return address
135 //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
136
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000137 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
138 Alpha::R19, Alpha::R20, Alpha::R21};
139 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
140 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000141 unsigned argVreg[6];
142 unsigned argPreg[6];
143 unsigned argOpc[6];
144
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000145 int count = 0;
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000146
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000147 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000148 {
149 SDOperand newroot, argt;
150 if (count < 6) {
151 switch (getValueType(I->getType())) {
152 default:
153 std::cerr << "Unknown Type " << getValueType(I->getType()) << "\n";
154 abort();
155 case MVT::f64:
156 case MVT::f32:
157 BuildMI(&BB, Alpha::IDEF, 0, args_float[count]);
158 argVreg[count] =
159 MF.getSSARegMap()->createVirtualRegister(
Andrew Lenharth032f2352005-02-22 21:59:48 +0000160 getRegClassFor(getValueType(I->getType())));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000161 argPreg[count] = args_float[count];
162 argOpc[count] = Alpha::CPYS;
163 argt = newroot = DAG.getCopyFromReg(argVreg[count],
164 getValueType(I->getType()),
165 DAG.getRoot());
166 break;
167 case MVT::i1:
168 case MVT::i8:
169 case MVT::i16:
170 case MVT::i32:
171 case MVT::i64:
172 BuildMI(&BB, Alpha::IDEF, 0, args_int[count]);
173 argVreg[count] =
174 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
175 argPreg[count] = args_int[count];
176 argOpc[count] = Alpha::BIS;
177 argt = newroot =
178 DAG.getCopyFromReg(argVreg[count], MVT::i64, DAG.getRoot());
179 if (getValueType(I->getType()) != MVT::i64)
180 argt =
181 DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()), newroot);
182 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000183 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000184 } 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 Lenharth032f2352005-02-22 21:59:48 +0000194 ++count;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000195 DAG.setRoot(newroot.getValue(1));
196 ArgValues.push_back(argt);
197 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000198
199 BuildMI(&BB, Alpha::IDEF, 0, Alpha::R29);
200 BuildMI(&BB, Alpha::BIS, 2, GP).addReg(Alpha::R29).addReg(Alpha::R29);
Andrew Lenharth032f2352005-02-22 21:59:48 +0000201 for (int i = 0; i < count && i < 6; ++i) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000202 BuildMI(&BB, argOpc[i], 2,
203 argVreg[i]).addReg(argPreg[i]).addReg(argPreg[i]);
204 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000205
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000206 return ArgValues;
207}
208
209std::pair<SDOperand, SDOperand>
210AlphaTargetLowering::LowerCallTo(SDOperand Chain,
211 const Type *RetTy, SDOperand Callee,
212 ArgListTy &Args, SelectionDAG &DAG) {
213 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000214 if (Args.size() > 6)
215 NumBytes = (Args.size() - 6) * 8;
216
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000217 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
218 DAG.getConstant(NumBytes, getPointerTy()));
219 std::vector<SDOperand> args_to_use;
220 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000221 {
222 switch (getValueType(Args[i].second)) {
223 default: assert(0 && "Unexpected ValueType for argument!");
224 case MVT::i1:
225 case MVT::i8:
226 case MVT::i16:
227 case MVT::i32:
228 // Promote the integer to 64 bits. If the input type is signed use a
229 // sign extend, otherwise use a zero extend.
230 if (Args[i].second->isSigned())
231 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
232 else
233 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
234 break;
235 case MVT::i64:
236 case MVT::f64:
237 case MVT::f32:
238 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000239 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000240 args_to_use.push_back(Args[i].first);
241 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000242
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000243 std::vector<MVT::ValueType> RetVals;
244 MVT::ValueType RetTyVT = getValueType(RetTy);
245 if (RetTyVT != MVT::isVoid)
246 RetVals.push_back(RetTyVT);
247 RetVals.push_back(MVT::Other);
248
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000249 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
250 Chain, Callee, args_to_use), 0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000251 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
252 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
253 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000254 return std::make_pair(TheCall, Chain);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000255}
256
257std::pair<SDOperand, SDOperand>
258AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
259 //vastart just returns the address of the VarArgsFrameIndex slot.
260 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
261}
262
263std::pair<SDOperand,SDOperand> AlphaTargetLowering::
264LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000265 const Type *ArgTy, SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000266 abort();
267}
268
269
270std::pair<SDOperand, SDOperand> AlphaTargetLowering::
271LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
272 SelectionDAG &DAG) {
273 abort();
274}
275
276
277
278
279
280namespace {
281
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000282//===--------------------------------------------------------------------===//
283/// ISel - Alpha specific code to select Alpha machine instructions for
284/// SelectionDAG operations.
285//===--------------------------------------------------------------------===//
286class ISel : public SelectionDAGISel {
287
288 /// AlphaLowering - This object fully describes how to lower LLVM code to an
289 /// Alpha-specific SelectionDAG.
290 AlphaTargetLowering AlphaLowering;
291
292
293 /// ExprMap - As shared expressions are codegen'd, we keep track of which
294 /// vreg the value is produced in, so we only emit one copy of each compiled
295 /// tree.
296 static const unsigned notIn = (unsigned)(-1);
297 std::map<SDOperand, unsigned> ExprMap;
298
299 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
300 std::map<SDOperand, unsigned> CCInvMap;
301
302public:
303 ISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM)
304 {}
305
306 /// InstructionSelectBasicBlock - This callback is invoked by
307 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
308 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000309 DEBUG(BB->dump());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000310 // Codegen the basic block.
311 Select(DAG.getRoot());
312
313 // Clear state used for selection.
314 ExprMap.clear();
315 CCInvMap.clear();
316 }
317
318 unsigned SelectExpr(SDOperand N);
319 unsigned SelectExprFP(SDOperand N, unsigned Result);
320 void Select(SDOperand N);
321
322 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
323 void SelectBranchCC(SDOperand N);
324};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000325}
326
Andrew Lenharth65838902005-02-06 16:22:15 +0000327static unsigned GetSymVersion(unsigned opcode)
328{
329 switch (opcode) {
330 default: assert(0 && "unknown load or store"); return 0;
331 case Alpha::LDQ: return Alpha::LDQ_SYM;
332 case Alpha::LDS: return Alpha::LDS_SYM;
333 case Alpha::LDT: return Alpha::LDT_SYM;
334 case Alpha::LDL: return Alpha::LDL_SYM;
335 case Alpha::LDBU: return Alpha::LDBU_SYM;
336 case Alpha::LDWU: return Alpha::LDWU_SYM;
337 case Alpha::LDW: return Alpha::LDW_SYM;
338 case Alpha::LDB: return Alpha::LDB_SYM;
339 case Alpha::STQ: return Alpha::STQ_SYM;
340 case Alpha::STS: return Alpha::STS_SYM;
341 case Alpha::STT: return Alpha::STT_SYM;
342 case Alpha::STL: return Alpha::STL_SYM;
343 case Alpha::STW: return Alpha::STW_SYM;
344 case Alpha::STB: return Alpha::STB_SYM;
345 }
346}
347
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000348//Check to see if the load is a constant offset from a base register
349void ISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
350{
351 unsigned opcode = N.getOpcode();
352 if (opcode == ISD::ADD) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000353 if(N.getOperand(1).getOpcode() == ISD::Constant &&
354 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
355 { //Normal imm add
356 Reg = SelectExpr(N.getOperand(0));
357 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
358 return;
359 }
360 else if(N.getOperand(0).getOpcode() == ISD::Constant &&
361 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 32767)
362 {
363 Reg = SelectExpr(N.getOperand(1));
364 offset = cast<ConstantSDNode>(N.getOperand(0))->getValue();
365 return;
366 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000367 }
368 Reg = SelectExpr(N);
369 offset = 0;
370 return;
371}
372
Andrew Lenharth445171a2005-02-08 00:40:03 +0000373void ISel::SelectBranchCC(SDOperand N)
374{
375 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000376 MachineBasicBlock *Dest =
377 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
378 unsigned Opc = Alpha::WTF;
379
Andrew Lenharth445171a2005-02-08 00:40:03 +0000380 Select(N.getOperand(0)); //chain
381 SDOperand CC = N.getOperand(1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000382
Andrew Lenharth445171a2005-02-08 00:40:03 +0000383 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000384 {
385 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
386 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
387 //Dropping the CC is only useful if we are comparing to 0
388 bool isZero0 = false;
389 bool isZero1 = false;
390 bool isNE = false;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000391
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000392 if(SetCC->getOperand(0).getOpcode() == ISD::Constant &&
393 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0)
394 isZero0 = true;
395 if(SetCC->getOperand(1).getOpcode() == ISD::Constant &&
396 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0)
397 isZero1 = true;
398 if(SetCC->getCondition() == ISD::SETNE)
399 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000400
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000401 if (isZero0) {
Andrew Lenharth445171a2005-02-08 00:40:03 +0000402 switch (SetCC->getCondition()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000403 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
404 case ISD::SETEQ: Opc = Alpha::BEQ; break;
405 case ISD::SETLT: Opc = Alpha::BGT; break;
406 case ISD::SETLE: Opc = Alpha::BGE; break;
407 case ISD::SETGT: Opc = Alpha::BLT; break;
408 case ISD::SETGE: Opc = Alpha::BLE; break;
409 case ISD::SETULT: Opc = Alpha::BNE; break;
410 case ISD::SETUGT: assert(0 && "0 > (unsigned) x is never true"); break;
411 case ISD::SETULE: assert(0 && "0 <= (unsigned) x is always true"); break;
412 case ISD::SETUGE: Opc = Alpha::BEQ; break; //Technically you could have this CC
413 case ISD::SETNE: Opc = Alpha::BNE; break;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000414 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000415 unsigned Tmp1 = SelectExpr(SetCC->getOperand(1));
416 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
417 return;
418 } else if (isZero1) {
419 switch (SetCC->getCondition()) {
420 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
421 case ISD::SETEQ: Opc = Alpha::BEQ; break;
422 case ISD::SETLT: Opc = Alpha::BLT; break;
423 case ISD::SETLE: Opc = Alpha::BLE; break;
424 case ISD::SETGT: Opc = Alpha::BGT; break;
425 case ISD::SETGE: Opc = Alpha::BGE; break;
426 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
427 case ISD::SETUGT: Opc = Alpha::BNE; break;
428 case ISD::SETULE: Opc = Alpha::BEQ; break; //Technically you could have this CC
429 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
430 case ISD::SETNE: Opc = Alpha::BNE; break;
431 }
432 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
433 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
434 return;
435 } else {
436 unsigned Tmp1 = SelectExpr(CC);
437 if (isNE)
438 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
439 else
440 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000441 return;
442 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000443 } else { //FP
444 //Any comparison between 2 values should be codegened as an folded branch, as moving
445 //CC to the integer register is very expensive
446 //for a cmp b: c = a - b;
447 //a = b: c = 0
448 //a < b: c < 0
449 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000450
451 bool invTest = false;
452 unsigned Tmp3;
453
454 ConstantFPSDNode *CN;
455 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
456 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
457 Tmp3 = SelectExpr(SetCC->getOperand(0));
458 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
459 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
460 {
461 Tmp3 = SelectExpr(SetCC->getOperand(1));
462 invTest = true;
463 }
464 else
465 {
466 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
467 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
468 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
469 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
470 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
471 .addReg(Tmp1).addReg(Tmp2);
472 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000473
474 switch (SetCC->getCondition()) {
475 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000476 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
477 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
478 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
479 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
480 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
481 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000482 }
483 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000484 return;
485 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000486 abort(); //Should never be reached
487 } else {
488 //Giveup and do the stupid thing
489 unsigned Tmp1 = SelectExpr(CC);
490 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
491 return;
492 }
Andrew Lenharth445171a2005-02-08 00:40:03 +0000493 abort(); //Should never be reached
494}
495
Andrew Lenharth40831c52005-01-28 06:57:18 +0000496unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
497{
498 unsigned Tmp1, Tmp2, Tmp3;
499 unsigned Opc = 0;
500 SDNode *Node = N.Val;
501 MVT::ValueType DestType = N.getValueType();
502 unsigned opcode = N.getOpcode();
503
504 switch (opcode) {
505 default:
506 Node->dump();
507 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +0000508
Andrew Lenharth9818c052005-02-05 13:19:12 +0000509 case ISD::SELECT:
510 {
Andrew Lenharth45859692005-03-03 21:47:53 +0000511 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
512 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
513 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
514
515 SDOperand CC = N.getOperand(0);
516 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
517
518 if (CC.getOpcode() == ISD::SETCC &&
519 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
520 { //FP Setcc -> Select yay!
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000521
522
Andrew Lenharth45859692005-03-03 21:47:53 +0000523 //for a cmp b: c = a - b;
524 //a = b: c = 0
525 //a < b: c < 0
526 //a > b: c > 0
527
528 bool invTest = false;
529 unsigned Tmp3;
530
531 ConstantFPSDNode *CN;
532 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
533 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
534 Tmp3 = SelectExpr(SetCC->getOperand(0));
535 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
536 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
537 {
538 Tmp3 = SelectExpr(SetCC->getOperand(1));
539 invTest = true;
540 }
541 else
542 {
543 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
544 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
545 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
546 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
547 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
548 .addReg(Tmp1).addReg(Tmp2);
549 }
550
551 switch (SetCC->getCondition()) {
552 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
553 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
554 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
555 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
556 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
557 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
558 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
559 }
560 BuildMI(BB, Opc, 3, Result).addReg(TV).addReg(FV).addReg(Tmp3);
561 return Result;
562 }
563 else
564 {
565 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
566 // Spill the cond to memory and reload it from there.
567 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
568 MachineFunction *F = BB->getParent();
569 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
570 unsigned Tmp4 = MakeReg(MVT::f64);
571 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addFrameIndex(FrameIdx).addReg(Alpha::F31);
572 BuildMI(BB, Alpha::LDT, 2, Tmp4).addFrameIndex(FrameIdx).addReg(Alpha::F31);
573 //now ideally, we don't have to do anything to the flag...
574 // Get the condition into the zero flag.
575 BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
576 return Result;
577 }
Andrew Lenharth9818c052005-02-05 13:19:12 +0000578 }
579
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000580 case ISD::FP_ROUND:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000581 assert (DestType == MVT::f32 &&
582 N.getOperand(0).getValueType() == MVT::f64 &&
583 "only f64 to f32 conversion supported here");
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000584 Tmp1 = SelectExpr(N.getOperand(0));
585 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
586 return Result;
587
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000588 case ISD::FP_EXTEND:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000589 assert (DestType == MVT::f64 &&
590 N.getOperand(0).getValueType() == MVT::f32 &&
591 "only f32 to f64 conversion supported here");
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000592 Tmp1 = SelectExpr(N.getOperand(0));
593 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
594 return Result;
595
Andrew Lenharth2c594352005-01-29 15:42:07 +0000596 case ISD::CopyFromReg:
597 {
598 // Make sure we generate both values.
599 if (Result != notIn)
600 ExprMap[N.getValue(1)] = notIn; // Generate the token
601 else
602 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
603
604 SDOperand Chain = N.getOperand(0);
605
606 Select(Chain);
607 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
608 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
609 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
610 return Result;
611 }
612
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000613 case ISD::LOAD:
614 {
615 // Make sure we generate both values.
616 if (Result != notIn)
617 ExprMap[N.getValue(1)] = notIn; // Generate the token
618 else
619 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000620
Andrew Lenharth29219162005-02-07 06:31:44 +0000621 DestType = N.getValue(0).getValueType();
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000622
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000623 SDOperand Chain = N.getOperand(0);
624 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000625 Select(Chain);
Andrew Lenharth65838902005-02-06 16:22:15 +0000626 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
627
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000628 if (Address.getOpcode() == ISD::GlobalAddress) {
629 AlphaLowering.restoreGP(BB);
630 Opc = GetSymVersion(Opc);
631 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
632 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000633 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000634 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000635 Opc = GetSymVersion(Opc);
Andrew Lenharth97127a12005-02-05 17:41:39 +0000636 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000637 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000638 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000639 BuildMI(BB, Opc, 2, Result)
640 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
641 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000642 } else {
643 long offset;
644 SelectAddr(Address, Tmp1, offset);
645 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
646 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000647 return Result;
648 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000649 case ISD::ConstantFP:
650 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
651 if (CN->isExactlyValue(+0.0)) {
652 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000653 } else if ( CN->isExactlyValue(-0.0)) {
654 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +0000655 } else {
656 abort();
657 }
658 }
659 return Result;
660
661 case ISD::MUL:
662 case ISD::ADD:
663 case ISD::SUB:
664 case ISD::SDIV:
665 switch( opcode ) {
666 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
667 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
668 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
669 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
670 };
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000671
672 ConstantFPSDNode *CN;
673 if (opcode == ISD::SUB
674 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
675 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
676 {
677 Tmp2 = SelectExpr(N.getOperand(1));
678 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
679 } else {
680 Tmp1 = SelectExpr(N.getOperand(0));
681 Tmp2 = SelectExpr(N.getOperand(1));
682 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
683 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000684 return Result;
685
Andrew Lenharth2c594352005-01-29 15:42:07 +0000686 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000687 {
688 //include a conversion sequence for float loads to double
689 if (Result != notIn)
690 ExprMap[N.getValue(1)] = notIn; // Generate the token
691 else
692 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
693
Andrew Lenhartha549deb2005-02-07 05:33:15 +0000694 Tmp1 = MakeReg(MVT::f32);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000695
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000696 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
697 "EXTLOAD not from f32");
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000698 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
699
700 SDOperand Chain = N.getOperand(0);
701 SDOperand Address = N.getOperand(1);
702 Select(Chain);
703
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000704 if (Address.getOpcode() == ISD::GlobalAddress) {
705 AlphaLowering.restoreGP(BB);
706 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
707 }
708 else if (ConstantPoolSDNode *CP =
709 dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
710 {
711 AlphaLowering.restoreGP(BB);
712 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addConstantPoolIndex(CP->getIndex());
713 }
714 else if(Address.getOpcode() == ISD::FrameIndex) {
715 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
Andrew Lenharth032f2352005-02-22 21:59:48 +0000716 BuildMI(BB, Alpha::LDS, 2, Tmp1)
717 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
718 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000719 } else {
720 long offset;
721 SelectAddr(Address, Tmp2, offset);
722 BuildMI(BB, Alpha::LDS, 1, Tmp1).addImm(offset).addReg(Tmp2);
723 }
Andrew Lenharth29219162005-02-07 06:31:44 +0000724 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000725 return Result;
726 }
Andrew Lenharth2c594352005-01-29 15:42:07 +0000727
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000728 case ISD::UINT_TO_FP:
729 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000730 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000731 assert (N.getOperand(0).getValueType() == MVT::i64
732 && "only quads can be loaded from");
Andrew Lenharth40831c52005-01-28 06:57:18 +0000733 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +0000734 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000735
736 //The hard way:
737 // Spill the integer to memory and reload it from there.
738 unsigned Size = MVT::getSizeInBits(MVT::i64)/8;
739 MachineFunction *F = BB->getParent();
740 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
741
Andrew Lenharth7efadce2005-01-31 01:44:26 +0000742 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addFrameIndex(FrameIdx).addReg(Alpha::F31);
743 BuildMI(BB, Alpha::LDT, 2, Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
744 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
745 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000746
747 //The easy way: doesn't work
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000748 // //so these instructions are not supported on ev56
749 // Opc = DestType == MVT::f64 ? Alpha::ITOFT : Alpha::ITOFS;
750 // BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
751 // Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
752 // BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000753
Andrew Lenharth40831c52005-01-28 06:57:18 +0000754 return Result;
755 }
756 }
757 assert(0 && "should not get here");
758 return 0;
759}
760
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000761unsigned ISel::SelectExpr(SDOperand N) {
762 unsigned Result;
763 unsigned Tmp1, Tmp2, Tmp3;
764 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000765 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000766
767 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000768 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000769
770 unsigned &Reg = ExprMap[N];
771 if (Reg) return Reg;
772
773 if (N.getOpcode() != ISD::CALL)
774 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000775 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000776 else {
777 // If this is a call instruction, make sure to prepare ALL of the result
778 // values as well as the chain.
779 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000780 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000781 else {
782 Result = MakeReg(Node->getValueType(0));
783 ExprMap[N.getValue(0)] = Result;
784 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
785 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000786 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000787 }
788 }
789
Andrew Lenharth22088bb2005-02-02 15:05:33 +0000790 if (DestType == MVT::f64 || DestType == MVT::f32 ||
Andrew Lenharth06342c32005-02-07 06:21:37 +0000791 (
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000792 (opcode == ISD::LOAD || opcode == ISD::CopyFromReg ||
793 opcode == ISD::EXTLOAD) &&
794 (N.getValue(0).getValueType() == MVT::f32 ||
795 N.getValue(0).getValueType() == MVT::f64)
Andrew Lenharth06342c32005-02-07 06:21:37 +0000796 )
797 )
Andrew Lenharth40831c52005-01-28 06:57:18 +0000798 return SelectExprFP(N, Result);
799
800 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000801 default:
802 Node->dump();
803 assert(0 && "Node not handled!\n");
804
Andrew Lenharth032f2352005-02-22 21:59:48 +0000805 case ISD::DYNAMIC_STACKALLOC:
806 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +0000807 if (Result != notIn)
808 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +0000809 else
810 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
811
812 // FIXME: We are currently ignoring the requested alignment for handling
813 // greater than the stack alignment. This will need to be revisited at some
814 // point. Align = N.getOperand(2);
815
816 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
817 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
818 std::cerr << "Cannot allocate stack object with greater alignment than"
819 << " the stack alignment yet!";
820 abort();
821 }
822
823 Select(N.getOperand(0));
824 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
825 {
826 if (CN->getValue() < 32000)
827 {
828 BuildMI(BB, Alpha::LDA, 2, Alpha::R30)
829 .addImm(-CN->getValue()).addReg(Alpha::R30);
830 } else {
831 Tmp1 = SelectExpr(N.getOperand(1));
832 // Subtract size from stack pointer, thereby allocating some space.
833 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
834 }
835 } else {
836 Tmp1 = SelectExpr(N.getOperand(1));
837 // Subtract size from stack pointer, thereby allocating some space.
838 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
839 }
840
841 // Put a pointer to the space into the result register, by copying the stack
842 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +0000843 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +0000844 return Result;
845
Andrew Lenharth2c594352005-01-29 15:42:07 +0000846 case ISD::ConstantPool:
847 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
848 AlphaLowering.restoreGP(BB);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000849 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000850 return Result;
851
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000852 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +0000853 BuildMI(BB, Alpha::LDA, 2, Result)
854 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
855 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000856 return Result;
857
858 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +0000859 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000860 case ISD::SEXTLOAD:
Andrew Lenhartha549deb2005-02-07 05:33:15 +0000861 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000862 {
863 // Make sure we generate both values.
864 if (Result != notIn)
865 ExprMap[N.getValue(1)] = notIn; // Generate the token
866 else
867 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000868
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000869 SDOperand Chain = N.getOperand(0);
870 SDOperand Address = N.getOperand(1);
871 Select(Chain);
872
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000873 assert(Node->getValueType(0) == MVT::i64 &&
874 "Unknown type to sign extend to.");
Andrew Lenharth03824012005-02-07 05:55:55 +0000875 if (opcode == ISD::LOAD)
876 Opc = Alpha::LDQ;
877 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000878 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
879 default: Node->dump(); assert(0 && "Bad sign extend!");
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000880 case MVT::i32: Opc = Alpha::LDL;
881 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
882 case MVT::i16: Opc = Alpha::LDWU;
883 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +0000884 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000885 case MVT::i8: Opc = Alpha::LDBU;
886 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000887 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000888
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000889 if (Address.getOpcode() == ISD::GlobalAddress) {
890 AlphaLowering.restoreGP(BB);
891 Opc = GetSymVersion(Opc);
892 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
893 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000894 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
895 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000896 Opc = GetSymVersion(Opc);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000897 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000898 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000899 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000900 BuildMI(BB, Opc, 2, Result)
901 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
902 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000903 } else {
904 long offset;
905 SelectAddr(Address, Tmp1, offset);
906 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
907 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000908 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000909 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000910
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000911 case ISD::GlobalAddress:
912 AlphaLowering.restoreGP(BB);
913 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
914 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
915 return Result;
916
917 case ISD::CALL:
918 {
919 Select(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000920
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000921 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000922 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000923
924 //grab the arguments
925 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000926 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000927 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000928 argvregs.push_back(SelectExpr(N.getOperand(i)));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000929
Andrew Lenharth684f2292005-01-30 00:35:27 +0000930 //in reg args
931 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000932 {
933 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
934 Alpha::R19, Alpha::R20, Alpha::R21};
935 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
936 Alpha::F19, Alpha::F20, Alpha::F21};
937 switch(N.getOperand(i+2).getValueType()) {
938 default:
939 Node->dump();
940 N.getOperand(i).Val->dump();
941 std::cerr << "Type for " << i << " is: " <<
942 N.getOperand(i+2).getValueType() << "\n";
943 assert(0 && "Unknown value type for call");
944 case MVT::i1:
945 case MVT::i8:
946 case MVT::i16:
947 case MVT::i32:
948 case MVT::i64:
949 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
950 break;
951 case MVT::f32:
952 case MVT::f64:
953 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
954 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000955 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000956 }
Andrew Lenharth684f2292005-01-30 00:35:27 +0000957 //in mem args
958 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000959 {
960 switch(N.getOperand(i+2).getValueType()) {
961 default:
962 Node->dump();
963 N.getOperand(i).Val->dump();
964 std::cerr << "Type for " << i << " is: " <<
965 N.getOperand(i+2).getValueType() << "\n";
966 assert(0 && "Unknown value type for call");
967 case MVT::i1:
968 case MVT::i8:
969 case MVT::i16:
970 case MVT::i32:
971 case MVT::i64:
972 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
973 break;
974 case MVT::f32:
975 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
976 break;
977 case MVT::f64:
978 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
979 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000980 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000981 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000982 //build the right kind of call
983 if (GlobalAddressSDNode *GASD =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000984 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000985 {
Andrew Lenharth3e315922005-02-10 20:10:38 +0000986 //if (GASD->getGlobal()->isExternal()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000987 //use safe calling convention
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000988 AlphaLowering.restoreGP(BB);
989 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal(),true);
Andrew Lenharth3e315922005-02-10 20:10:38 +0000990 //} else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000991 //use PC relative branch call
Andrew Lenharth3e315922005-02-10 20:10:38 +0000992 //BuildMI(BB, Alpha::BSR, 1, Alpha::R26).addGlobalAddress(GASD->getGlobal(),true);
993 //}
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000994 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000995 else if (ExternalSymbolSDNode *ESSDN =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000996 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000997 {
998 AlphaLowering.restoreGP(BB);
999 BuildMI(BB, Alpha::CALL, 0).addExternalSymbol(ESSDN->getSymbol(), true);
1000 } else {
1001 //no need to restore GP as we are doing an indirect call
1002 Tmp1 = SelectExpr(N.getOperand(1));
1003 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1004 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1005 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001006
1007 //push the result into a virtual register
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001008
1009 switch (Node->getValueType(0)) {
1010 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001011 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001012 case MVT::i1:
1013 case MVT::i8:
1014 case MVT::i16:
1015 case MVT::i32:
1016 case MVT::i64:
1017 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1018 break;
1019 case MVT::f32:
1020 case MVT::f64:
1021 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1022 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001023 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001024 return Result+N.ResNo;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001025 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001026
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001027 case ISD::SIGN_EXTEND:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001028 abort();
1029
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001030 case ISD::SIGN_EXTEND_INREG:
1031 {
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001032 //Alpha has instructions for a bunch of signed 32 bit stuff
1033 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001034 {
1035 switch (N.getOperand(0).getOpcode()) {
1036 case ISD::ADD:
1037 case ISD::SUB:
1038 case ISD::MUL:
1039 {
1040 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1041 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1042 //FIXME: first check for Scaled Adds and Subs!
1043 if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1044 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
1045 { //Normal imm add/sub
1046 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth093f3272005-02-12 21:11:17 +00001047 //if the value was really originally a i32, skip the up conversion
1048 if (N.getOperand(0).getOperand(0).getOpcode() == ISD::SIGN_EXTEND_INREG &&
1049 dyn_cast<MVTSDNode>(N.getOperand(0).getOperand(0).Val)
1050 ->getExtraValueType() == MVT::i32)
1051 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1052 else
1053 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001054 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1055 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001056 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001057 else
1058 { //Normal add/sub
1059 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULLi : Alpha::SUBL);
Andrew Lenharth093f3272005-02-12 21:11:17 +00001060 //if the value was really originally a i32, skip the up conversion
1061 if (N.getOperand(0).getOperand(0).getOpcode() == ISD::SIGN_EXTEND_INREG &&
1062 dyn_cast<MVTSDNode>(N.getOperand(0).getOperand(0).Val)
1063 ->getExtraValueType() == MVT::i32)
1064 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1065 else
1066 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1067 //if the value was really originally a i32, skip the up conversion
1068 if (N.getOperand(0).getOperand(1).getOpcode() == ISD::SIGN_EXTEND_INREG &&
1069 dyn_cast<MVTSDNode>(N.getOperand(0).getOperand(1).Val)
1070 ->getExtraValueType() == MVT::i32)
1071 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1072 else
1073 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1074
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001075 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1076 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1077 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1078 }
1079 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001080 }
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001081 case ISD::SEXTLOAD:
1082 //SelectionDag isn't deleting the signextend after sextloads
1083 Reg = Result = SelectExpr(N.getOperand(0));
1084 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001085 default: break; //Fall Though;
1086 }
1087 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001088 Tmp1 = SelectExpr(N.getOperand(0));
1089 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001090 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001091 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001092 {
1093 default:
1094 Node->dump();
1095 assert(0 && "Sign Extend InReg not there yet");
1096 break;
1097 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001098 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001099 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001100 break;
1101 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001102 case MVT::i16:
1103 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
1104 break;
1105 case MVT::i8:
1106 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
1107 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001108 case MVT::i1:
1109 Tmp2 = MakeReg(MVT::i64);
1110 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenharth7536eea2005-02-12 20:42:09 +00001111 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::F31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001112 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001113 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001114 return Result;
1115 }
1116 case ISD::ZERO_EXTEND_INREG:
1117 {
1118 Tmp1 = SelectExpr(N.getOperand(0));
1119 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001120 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001121 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001122 {
1123 default:
1124 Node->dump();
1125 assert(0 && "Zero Extend InReg not there yet");
1126 break;
1127 case MVT::i32: Tmp2 = 0xf0; break;
1128 case MVT::i16: Tmp2 = 0xfc; break;
1129 case MVT::i8: Tmp2 = 0xfe; break;
1130 case MVT::i1: //handle this one special
1131 BuildMI(BB, Alpha::ANDi, 2, Result).addReg(Tmp1).addImm(1);
1132 return Result;
1133 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001134 BuildMI(BB, Alpha::ZAPi, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001135 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001136 }
1137
1138 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001139 {
1140 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1141 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1142 bool isConst1 = false;
1143 bool isConst2 = false;
1144 int dir;
Andrew Lenharth9818c052005-02-05 13:19:12 +00001145
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001146 //Tmp1 = SelectExpr(N.getOperand(0));
1147 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001148 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1149 isConst1 = true;
1150 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001151 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1152 isConst2 = true;
1153
1154 switch (SetCC->getCondition()) {
1155 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1156 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001157 case ISD::SETLT:
1158 Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
1159 case ISD::SETLE:
1160 Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
1161 case ISD::SETGT:
1162 Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
1163 case ISD::SETGE:
1164 Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
1165 case ISD::SETULT:
1166 Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
1167 case ISD::SETUGT:
1168 Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
1169 case ISD::SETULE:
1170 Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
1171 case ISD::SETUGE:
1172 Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001173 case ISD::SETNE: {//Handle this one special
1174 //std::cerr << "Alpha does not have a setne.\n";
1175 //abort();
1176 Tmp1 = SelectExpr(N.getOperand(0));
1177 Tmp2 = SelectExpr(N.getOperand(1));
1178 Tmp3 = MakeReg(MVT::i64);
1179 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001180 //Remeber we have the Inv for this CC
1181 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001182 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001183 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001184 return Result;
1185 }
1186 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001187 if (dir == 1) {
1188 Tmp1 = SelectExpr(N.getOperand(0));
1189 if (isConst2) {
1190 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1191 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1192 } else {
1193 Tmp2 = SelectExpr(N.getOperand(1));
1194 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1195 }
1196 } else if (dir == 2) {
1197 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001198 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001199 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1200 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1201 } else {
1202 Tmp2 = SelectExpr(N.getOperand(0));
1203 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1204 }
1205 } else { //dir == 0
1206 if (isConst1) {
1207 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1208 Tmp2 = SelectExpr(N.getOperand(1));
1209 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
1210 } else if (isConst2) {
1211 Tmp1 = SelectExpr(N.getOperand(0));
1212 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1213 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1214 } else {
1215 Tmp1 = SelectExpr(N.getOperand(0));
1216 Tmp2 = SelectExpr(N.getOperand(1));
1217 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1218 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001219 }
1220 } else {
1221 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
1222 bool rev = false;
1223 bool inv = false;
1224
1225 switch (SetCC->getCondition()) {
1226 default: Node->dump(); assert(0 && "Unknown FP comparison!");
1227 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
1228 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
1229 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
1230 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
1231 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
1232 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
1233 }
1234
1235 Tmp1 = SelectExpr(N.getOperand(0));
1236 Tmp2 = SelectExpr(N.getOperand(1));
1237 //Can only compare doubles, and dag won't promote for me
1238 if (SetCC->getOperand(0).getValueType() == MVT::f32)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001239 {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001240 //assert(0 && "Setcc On float?\n");
1241 std::cerr << "Setcc on float!\n";
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001242 Tmp3 = MakeReg(MVT::f64);
1243 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
1244 Tmp1 = Tmp3;
1245 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001246 if (SetCC->getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001247 {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001248 //assert (0 && "Setcc On float?\n");
1249 std::cerr << "Setcc on float!\n";
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001250 Tmp3 = MakeReg(MVT::f64);
1251 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
Andrew Lenharth572af902005-02-14 05:41:43 +00001252 Tmp2 = Tmp3;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001253 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001254
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001255 if (rev) std::swap(Tmp1, Tmp2);
1256 Tmp3 = MakeReg(MVT::f64);
1257 //do the comparison
1258 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1259
1260 //now arrange for Result (int) to have a 1 or 0
1261
1262 // Spill the FP to memory and reload it from there.
1263 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1264 MachineFunction *F = BB->getParent();
1265 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
1266 unsigned Tmp4 = MakeReg(MVT::f64);
1267 BuildMI(BB, Alpha::CVTTQ, 1, Tmp4).addReg(Tmp3);
1268 BuildMI(BB, Alpha::STT, 3).addReg(Tmp4).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1269 unsigned Tmp5 = MakeReg(MVT::i64);
1270 BuildMI(BB, Alpha::LDQ, 2, Tmp5).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharth9818c052005-02-05 13:19:12 +00001271
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001272 //now, set result based on Tmp5
1273 //Set Tmp6 if fp cmp was false
1274 unsigned Tmp6 = MakeReg(MVT::i64);
1275 BuildMI(BB, Alpha::CMPEQ, 2, Tmp6).addReg(Tmp5).addReg(Alpha::R31);
1276 //and invert
1277 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp6).addReg(Alpha::R31);
1278
1279 }
1280 // else
1281 // {
1282 // Node->dump();
1283 // assert(0 && "Not a setcc in setcc");
1284 // }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001285 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001286 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001287 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001288
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001289 case ISD::CopyFromReg:
1290 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001291 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001292 if (Result != notIn)
1293 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001294 else
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001295 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth40831c52005-01-28 06:57:18 +00001296
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001297 SDOperand Chain = N.getOperand(0);
1298
1299 Select(Chain);
1300 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1301 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1302 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1303 return Result;
1304 }
1305
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001306 //Most of the plain arithmetic and logic share the same form, and the same
1307 //constant immediate test
1308 case ISD::AND:
1309 case ISD::OR:
1310 case ISD::XOR:
1311 case ISD::SHL:
1312 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001313 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001314 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001315 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1316 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001317 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001318 {
1319 switch(opcode) {
1320 case ISD::AND: Opc = Alpha::ANDi; break;
1321 case ISD::OR: Opc = Alpha::BISi; break;
1322 case ISD::XOR: Opc = Alpha::XORi; break;
1323 case ISD::SHL: Opc = Alpha::SLi; break;
1324 case ISD::SRL: Opc = Alpha::SRLi; break;
1325 case ISD::SRA: Opc = Alpha::SRAi; break;
1326 case ISD::MUL: Opc = Alpha::MULQi; break;
1327 };
1328 Tmp1 = SelectExpr(N.getOperand(0));
1329 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1330 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1331 } else {
1332 switch(opcode) {
1333 case ISD::AND: Opc = Alpha::AND; break;
1334 case ISD::OR: Opc = Alpha::BIS; break;
1335 case ISD::XOR: Opc = Alpha::XOR; break;
1336 case ISD::SHL: Opc = Alpha::SL; break;
1337 case ISD::SRL: Opc = Alpha::SRL; break;
1338 case ISD::SRA: Opc = Alpha::SRA; break;
1339 case ISD::MUL: Opc = Alpha::MULQ; break;
1340 };
1341 Tmp1 = SelectExpr(N.getOperand(0));
1342 Tmp2 = SelectExpr(N.getOperand(1));
1343 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1344 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001345 return Result;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001346
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001347 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001348 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001349 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001350 bool isAdd = opcode == ISD::ADD;
1351
1352 //FIXME: first check for Scaled Adds and Subs!
1353 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001354 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001355 { //Normal imm add/sub
1356 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1357 Tmp1 = SelectExpr(N.getOperand(0));
1358 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1359 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1360 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001361 else if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001362 (cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767 ||
1363 (long)cast<ConstantSDNode>(N.getOperand(1))->getValue() >= -32767))
1364 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001365 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001366 Tmp2 = (long)cast<ConstantSDNode>(N.getOperand(1))->getValue();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001367 if (!isAdd)
1368 Tmp2 = -Tmp2;
1369 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
1370 } else {
1371 //Normal add/sub
1372 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1373 Tmp1 = SelectExpr(N.getOperand(0));
1374 Tmp2 = SelectExpr(N.getOperand(1));
1375 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1376 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001377 return Result;
1378 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001379
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001380 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00001381 case ISD::SREM:
1382 case ISD::SDIV:
1383 case ISD::UDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001384 //FIXME: alpha really doesn't support any of these operations,
1385 // the ops are expanded into special library calls with
1386 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001387 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00001388 switch(opcode) {
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001389 case ISD::UREM: Opc = Alpha::REMQU; break;
1390 case ISD::SREM: Opc = Alpha::REMQ; break;
1391 case ISD::UDIV: Opc = Alpha::DIVQU; break;
1392 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001393 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001394 Tmp1 = SelectExpr(N.getOperand(0));
1395 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001396 AlphaLowering.restoreGP(BB);
Andrew Lenharth02981182005-01-26 01:24:38 +00001397 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001398 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001399
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001400 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001401 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001402 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001403 assert (DestType == MVT::i64 && "only quads can be loaded to");
1404 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001405 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001406 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1407
1408 //The hard way:
1409 // Spill the integer to memory and reload it from there.
1410 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1411 MachineFunction *F = BB->getParent();
1412 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
1413
1414 //CVTTQ STT LDQ
1415 //CVTST CVTTQ STT LDQ
1416 if (SrcType == MVT::f32)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001417 {
1418 Tmp2 = MakeReg(MVT::f64);
1419 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1420 Tmp1 = Tmp2;
1421 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001422 Tmp2 = MakeReg(MVT::f64);
1423 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
1424 BuildMI(BB, Alpha::STT, 3).addReg(Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1425 BuildMI(BB, Alpha::LDQ, 2, Result).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1426
1427 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001428 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001429
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001430 // // case ISD::FP_TO_UINT:
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001431
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001432 case ISD::SELECT:
1433 {
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001434 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001435 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1436 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001437 // Get the condition into the zero flag.
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001438 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001439 return Result;
1440 }
1441
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001442 case ISD::Constant:
1443 {
Andrew Lenharth22d5a412005-02-02 00:51:15 +00001444 unsigned long val = cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001445 if (val < 32000 && (long)val > -32000)
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001446 BuildMI(BB, Alpha::LOAD_IMM, 1, Result).addImm((long)val);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001447 else {
1448 MachineConstantPool *CP = BB->getParent()->getConstantPool();
1449 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
1450 unsigned CPI = CP->getConstantPoolIndex(C);
1451 AlphaLowering.restoreGP(BB);
1452 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
1453 }
1454 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001455 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001456 }
1457
1458 return 0;
1459}
1460
1461void ISel::Select(SDOperand N) {
1462 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00001463 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001464
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001465 // FIXME: Disable for our current expansion model!
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001466 if (/*!N->hasOneUse() &&*/ !ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001467 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001468
1469 SDNode *Node = N.Val;
Andrew Lenharth760270d2005-02-07 23:02:23 +00001470
Andrew Lenharth760270d2005-02-07 23:02:23 +00001471 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001472
1473 default:
1474 Node->dump(); std::cerr << "\n";
1475 assert(0 && "Node not handled yet!");
1476
1477 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00001478 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001479 return;
1480 }
1481
1482 case ISD::BR: {
1483 MachineBasicBlock *Dest =
1484 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1485
1486 Select(N.getOperand(0));
1487 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
1488 return;
1489 }
1490
1491 case ISD::ImplicitDef:
1492 Select(N.getOperand(0));
1493 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
1494 return;
1495
1496 case ISD::EntryToken: return; // Noop
1497
1498 case ISD::TokenFactor:
1499 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1500 Select(Node->getOperand(i));
1501
1502 //N.Val->dump(); std::cerr << "\n";
1503 //assert(0 && "Node not handled yet!");
1504
1505 return;
1506
1507 case ISD::CopyToReg:
1508 Select(N.getOperand(0));
1509 Tmp1 = SelectExpr(N.getOperand(1));
1510 Tmp2 = cast<RegSDNode>(N)->getReg();
1511
1512 if (Tmp1 != Tmp2) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001513 if (N.getOperand(1).getValueType() == MVT::f64 ||
1514 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00001515 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1516 else
1517 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001518 }
1519 return;
1520
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001521 case ISD::RET:
1522 switch (N.getNumOperands()) {
1523 default:
1524 std::cerr << N.getNumOperands() << "\n";
1525 for (unsigned i = 0; i < N.getNumOperands(); ++i)
1526 std::cerr << N.getOperand(i).getValueType() << "\n";
1527 Node->dump();
1528 assert(0 && "Unknown return instruction!");
1529 case 2:
1530 Select(N.getOperand(0));
1531 Tmp1 = SelectExpr(N.getOperand(1));
1532 switch (N.getOperand(1).getValueType()) {
1533 default: Node->dump();
1534 assert(0 && "All other types should have been promoted!!");
1535 case MVT::f64:
1536 case MVT::f32:
1537 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1538 break;
1539 case MVT::i32:
1540 case MVT::i64:
1541 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1542 break;
1543 }
1544 break;
1545 case 1:
1546 Select(N.getOperand(0));
1547 break;
1548 }
1549 //Tmp2 = AlphaLowering.getRetAddr();
1550 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
1551 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
1552 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001553
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001554 case ISD::TRUNCSTORE:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001555 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001556 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001557 SDOperand Chain = N.getOperand(0);
1558 SDOperand Value = N.getOperand(1);
1559 SDOperand Address = N.getOperand(2);
1560 Select(Chain);
1561
1562 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00001563
1564 if (opcode == ISD::STORE) {
1565 switch(Value.getValueType()) {
1566 default: assert(0 && "unknown Type in store");
1567 case MVT::i64: Opc = Alpha::STQ; break;
1568 case MVT::f64: Opc = Alpha::STT; break;
1569 case MVT::f32: Opc = Alpha::STS; break;
1570 }
1571 } else { //ISD::TRUNCSTORE
1572 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1573 default: assert(0 && "unknown Type in store");
1574 case MVT::i1: //FIXME: DAG does not promote this load
1575 case MVT::i8: Opc = Alpha::STB; break;
1576 case MVT::i16: Opc = Alpha::STW; break;
1577 case MVT::i32: Opc = Alpha::STL; break;
1578 }
Andrew Lenharth65838902005-02-06 16:22:15 +00001579 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00001580
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001581 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001582 {
1583 AlphaLowering.restoreGP(BB);
1584 Opc = GetSymVersion(Opc);
1585 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1586 }
Andrew Lenharth05380342005-02-07 05:07:00 +00001587 else if(Address.getOpcode() == ISD::FrameIndex)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001588 {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001589 BuildMI(BB, Opc, 3).addReg(Tmp1)
1590 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1591 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001592 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001593 else
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001594 {
1595 long offset;
1596 SelectAddr(Address, Tmp2, offset);
1597 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1598 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001599 return;
1600 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001601
1602 case ISD::EXTLOAD:
1603 case ISD::SEXTLOAD:
1604 case ISD::ZEXTLOAD:
1605 case ISD::LOAD:
1606 case ISD::CopyFromReg:
1607 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001608 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001609 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001610 SelectExpr(N);
1611 return;
1612
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001613 case ISD::ADJCALLSTACKDOWN:
1614 case ISD::ADJCALLSTACKUP:
1615 Select(N.getOperand(0));
1616 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1617
1618 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? Alpha::ADJUSTSTACKDOWN :
1619 Alpha::ADJUSTSTACKUP;
1620 BuildMI(BB, Opc, 1).addImm(Tmp1);
1621 return;
1622 }
1623 assert(0 && "Should not be reached!");
1624}
1625
1626
1627/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1628/// into a machine code representation using pattern matching and a machine
1629/// description file.
1630///
1631FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
1632 return new ISel(TM);
1633}