blob: a92aa1b2c6d25979d1f392ac250a2db80e943f7a [file] [log] [blame]
Andrew Lenharth2d6f0222005-01-24 19:44:07 +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
43 setShiftAmountType(MVT::i64); //are these needed?
44 setSetCCResultType(MVT::i64); //are these needed?
45
Andrew Lenharth304d0f32005-01-22 23:41:55 +000046 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
47 addRegisterClass(MVT::f64, Alpha::FPRCRegisterClass);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000048 addRegisterClass(MVT::f32, Alpha::FPRCRegisterClass);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000049
Andrew Lenharthd2bb9602005-01-27 07:50:35 +000050 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000051
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +000052 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000053 setOperationAction(ISD::ZEXTLOAD , MVT::i32 , Expand);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000054
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +000055 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000056 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
57 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
58
Andrew Lenharth3d65d312005-01-27 03:49:45 +000059 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); //what is the sign expansion of 1? 1 or -1?
Andrew Lenharth02981182005-01-26 01:24:38 +000060
Andrew Lenharth3d65d312005-01-27 03:49:45 +000061 setOperationAction(ISD::SREM, MVT::f32, Expand);
62 setOperationAction(ISD::SREM, MVT::f64, Expand);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +000063
Andrew Lenharth3d65d312005-01-27 03:49:45 +000064 computeRegisterProperties();
Andrew Lenharth304d0f32005-01-22 23:41:55 +000065
Andrew Lenharthd2bb9602005-01-27 07:50:35 +000066 addLegalFPImmediate(+0.0); //F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +000067 }
68
69 /// LowerArguments - This hook must be implemented to indicate how we should
70 /// lower the arguments for the specified function, into the specified DAG.
71 virtual std::vector<SDOperand>
72 LowerArguments(Function &F, SelectionDAG &DAG);
73
74 /// LowerCallTo - This hook lowers an abstract call to a function into an
75 /// actual call.
76 virtual std::pair<SDOperand, SDOperand>
77 LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
78 ArgListTy &Args, SelectionDAG &DAG);
79
80 virtual std::pair<SDOperand, SDOperand>
81 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
82
83 virtual std::pair<SDOperand,SDOperand>
84 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
85 const Type *ArgTy, SelectionDAG &DAG);
86
87 virtual std::pair<SDOperand, SDOperand>
88 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
89 SelectionDAG &DAG);
90
91 void restoreGP(MachineBasicBlock* BB)
92 {
93 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
94 }
95 };
96}
97
98//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
99
100//For now, just use variable size stack frame format
101
102//In a standard call, the first six items are passed in registers $16
103//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
104//of argument-to-register correspondence.) The remaining items are
105//collected in a memory argument list that is a naturally aligned
106//array of quadwords. In a standard call, this list, if present, must
107//be passed at 0(SP).
108//7 ... n 0(SP) ... (n-7)*8(SP)
109
110std::vector<SDOperand>
111AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
112{
113 std::vector<SDOperand> ArgValues;
114
115 // //#define FP $15
116 // //#define RA $26
117 // //#define PV $27
118 // //#define GP $29
119 // //#define SP $30
120
121 // assert(0 && "TODO");
122 MachineFunction &MF = DAG.getMachineFunction();
123 MachineFrameInfo *MFI = MF.getFrameInfo();
124
125 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
126 MachineBasicBlock& BB = MF.front();
127
128 //Handle the return address
129 //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
130
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000131 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
132 Alpha::R19, Alpha::R20, Alpha::R21};
133 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
134 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000135 std::vector<unsigned> argVreg;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000136 std::vector<unsigned> argPreg;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000137 std::vector<unsigned> argOpc;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000138 int count = 0;
139 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I)
140 {
Andrew Lenharth40831c52005-01-28 06:57:18 +0000141 SDOperand newroot, argt;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000142 if (count < 6) {
143 switch (getValueType(I->getType())) {
144 default: std::cerr << "Unknown Type " << getValueType(I->getType()) << "\n"; abort();
145 case MVT::f64:
146 case MVT::f32:
147 BuildMI(&BB, Alpha::IDEF, 0, args_float[count]);
148 argVreg.push_back(MF.getSSARegMap()->createVirtualRegister(getRegClassFor(getValueType(I->getType()))));
149 argPreg.push_back(args_float[count]);
150 argOpc.push_back(Alpha::CPYS);
151 newroot = DAG.getCopyFromReg(argVreg[count], getValueType(I->getType()), DAG.getRoot());
152 break;
153 case MVT::i1:
154 case MVT::i8:
155 case MVT::i16:
156 case MVT::i32:
157 case MVT::i64:
158 BuildMI(&BB, Alpha::IDEF, 0, args_int[count]);
159 argVreg.push_back(MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64)));
160 argPreg.push_back(args_int[count]);
161 argOpc.push_back(Alpha::BIS);
162 argt = newroot = DAG.getCopyFromReg(argVreg[count], MVT::i64, DAG.getRoot());
163 if (getValueType(I->getType()) != MVT::i64)
164 argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()), newroot);
165 break;
166 }
167 } else { //more args
168 // Create the frame index object for this incoming parameter...
169 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
170
171 // Create the SelectionDAG nodes corresponding to a load from this parameter
172 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
173 argt = newroot = DAG.getLoad(getValueType(I->getType()), DAG.getEntryNode(), FIN);
Andrew Lenharth40831c52005-01-28 06:57:18 +0000174 }
175 DAG.setRoot(newroot.getValue(1));
176 ArgValues.push_back(argt);
Andrew Lenharth684f2292005-01-30 00:35:27 +0000177 ++count;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000178 }
179
180 BuildMI(&BB, Alpha::IDEF, 0, Alpha::R29);
181 BuildMI(&BB, Alpha::BIS, 2, GP).addReg(Alpha::R29).addReg(Alpha::R29);
Andrew Lenharth40831c52005-01-28 06:57:18 +0000182 for (int i = 0; i < count; ++i)
183 BuildMI(&BB, argOpc[i], 2, argVreg[i]).addReg(argPreg[i]).addReg(argPreg[i]);
184
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000185 return ArgValues;
186}
187
188std::pair<SDOperand, SDOperand>
189AlphaTargetLowering::LowerCallTo(SDOperand Chain,
190 const Type *RetTy, SDOperand Callee,
191 ArgListTy &Args, SelectionDAG &DAG) {
192 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000193 if (Args.size() > 6)
194 NumBytes = (Args.size() - 6) * 8;
195
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000196 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
197 DAG.getConstant(NumBytes, getPointerTy()));
198 std::vector<SDOperand> args_to_use;
199 for (unsigned i = 0, e = Args.size(); i != e; ++i)
200 {
201 switch (getValueType(Args[i].second)) {
202 default: assert(0 && "Unexpected ValueType for argument!");
203 case MVT::i1:
204 case MVT::i8:
205 case MVT::i16:
206 case MVT::i32:
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000207 // Promote the integer to 64 bits. If the input type is signed use a
208 // sign extend, otherwise use a zero extend.
209 if (Args[i].second->isSigned())
Andrew Lenharth40831c52005-01-28 06:57:18 +0000210 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000211 else
Andrew Lenharth40831c52005-01-28 06:57:18 +0000212 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000213 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000214 case MVT::i64:
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000215 case MVT::f64:
216 case MVT::f32:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000217 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000218 }
219 args_to_use.push_back(Args[i].first);
220 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000221
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000222 std::vector<MVT::ValueType> RetVals;
223 MVT::ValueType RetTyVT = getValueType(RetTy);
224 if (RetTyVT != MVT::isVoid)
225 RetVals.push_back(RetTyVT);
226 RetVals.push_back(MVT::Other);
227
228 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee, args_to_use), 0);
229 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
230 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
231 DAG.getConstant(NumBytes, getPointerTy()));
232 return std::make_pair(TheCall, Chain);
233}
234
235std::pair<SDOperand, SDOperand>
236AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
237 //vastart just returns the address of the VarArgsFrameIndex slot.
238 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
239}
240
241std::pair<SDOperand,SDOperand> AlphaTargetLowering::
242LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
243 const Type *ArgTy, SelectionDAG &DAG) {
244 abort();
245}
246
247
248std::pair<SDOperand, SDOperand> AlphaTargetLowering::
249LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
250 SelectionDAG &DAG) {
251 abort();
252}
253
254
255
256
257
258namespace {
259
260 //===--------------------------------------------------------------------===//
261 /// ISel - Alpha specific code to select Alpha machine instructions for
262 /// SelectionDAG operations.
263 ///
264 class ISel : public SelectionDAGISel {
265
266 /// AlphaLowering - This object fully describes how to lower LLVM code to an
267 /// Alpha-specific SelectionDAG.
268 AlphaTargetLowering AlphaLowering;
269
270
271 /// ExprMap - As shared expressions are codegen'd, we keep track of which
272 /// vreg the value is produced in, so we only emit one copy of each compiled
273 /// tree.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000274 static const unsigned notIn = (unsigned)(-1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000275 std::map<SDOperand, unsigned> ExprMap;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000276
277 public:
278 ISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM) {
279 }
280
281 /// InstructionSelectBasicBlock - This callback is invoked by
282 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
283 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
284 // Codegen the basic block.
285 Select(DAG.getRoot());
286
287 // Clear state used for selection.
288 ExprMap.clear();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000289 }
290
291 unsigned SelectExpr(SDOperand N);
Andrew Lenharth40831c52005-01-28 06:57:18 +0000292 unsigned SelectExprFP(SDOperand N, unsigned Result);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000293 void Select(SDOperand N);
294 };
295}
296
Andrew Lenharth40831c52005-01-28 06:57:18 +0000297unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
298{
299 unsigned Tmp1, Tmp2, Tmp3;
300 unsigned Opc = 0;
301 SDNode *Node = N.Val;
302 MVT::ValueType DestType = N.getValueType();
303 unsigned opcode = N.getOpcode();
304
305 switch (opcode) {
306 default:
307 Node->dump();
308 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +0000309
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000310 case ISD::FP_EXTEND:
311 assert (DestType == MVT::f64 && N.getOperand(0).getValueType() == MVT::f32 && "only f32 to f64 conversion supported here");
312 Tmp1 = SelectExpr(N.getOperand(0));
313 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
314 return Result;
315
Andrew Lenharth2c594352005-01-29 15:42:07 +0000316 case ISD::CopyFromReg:
317 {
318 // Make sure we generate both values.
319 if (Result != notIn)
320 ExprMap[N.getValue(1)] = notIn; // Generate the token
321 else
322 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
323
324 SDOperand Chain = N.getOperand(0);
325
326 Select(Chain);
327 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
328 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
329 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
330 return Result;
331 }
332
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000333 case ISD::LOAD:
334 {
335 // Make sure we generate both values.
336 if (Result != notIn)
337 ExprMap[N.getValue(1)] = notIn; // Generate the token
338 else
339 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
340
341 SDOperand Chain = N.getOperand(0);
342 SDOperand Address = N.getOperand(1);
343
344 if (Address.getOpcode() == ISD::GlobalAddress)
345 {
346 Select(Chain);
347 AlphaLowering.restoreGP(BB);
348 Opc = DestType == MVT::f64 ? Alpha::LDS : Alpha::LDT;
349 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
350 }
351 else
352 {
353 Select(Chain);
354 Tmp2 = SelectExpr(Address);
355 Opc = DestType == MVT::f64 ? Alpha::LDS : Alpha::LDT;
356 BuildMI(BB, Opc, 2, Result).addImm(0).addReg(Tmp2);
357 }
358 return Result;
359 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000360 case ISD::ConstantFP:
361 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
362 if (CN->isExactlyValue(+0.0)) {
363 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
364 } else {
365 abort();
366 }
367 }
368 return Result;
369
370 case ISD::MUL:
371 case ISD::ADD:
372 case ISD::SUB:
373 case ISD::SDIV:
374 switch( opcode ) {
375 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
376 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
377 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
378 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
379 };
380 Tmp1 = SelectExpr(N.getOperand(0));
381 Tmp2 = SelectExpr(N.getOperand(1));
382 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
383 return Result;
384
Andrew Lenharth2c594352005-01-29 15:42:07 +0000385 case ISD::EXTLOAD:
386 //include a conversion sequence for float loads to double
387 if (Result != notIn)
388 ExprMap[N.getValue(1)] = notIn; // Generate the token
389 else
390 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
391
392 Tmp2 = MakeReg(MVT::f32);
393
394 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
395 if (Node->getValueType(0) == MVT::f64) {
396 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
397 "Bad EXTLOAD!");
398 BuildMI(BB, Alpha::LDS, 1, Tmp2).addConstantPoolIndex(CP->getIndex());
399 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp2);
400 return Result;
401 }
402 Select(Node->getOperand(0)); // chain
403 Tmp1 = SelectExpr(Node->getOperand(1));
404 BuildMI(BB, Alpha::LDS, 1, Tmp2).addReg(Tmp1);
405 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp2);
406 return Result;
407
408
409 //case ISD::UINT_TO_FP:
410
411 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000412 {
413 assert (N.getOperand(0).getValueType() == MVT::i64 && "only quads can be loaded from");
414 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth2c594352005-01-29 15:42:07 +0000415
416 //The hard way:
417 // Spill the integer to memory and reload it from there.
418 unsigned Size = MVT::getSizeInBits(MVT::i64)/8;
419 MachineFunction *F = BB->getParent();
420 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
421
422 //STL LDS
423 //STQ LDT
424 Opc = DestType == MVT::f64 ? Alpha::STQ : Alpha::STL;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000425 BuildMI(BB, Opc, 2).addReg(Tmp1).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000426 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000427 BuildMI(BB, Opc, 1, Result).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000428
429 //The easy way: doesn't work
430// //so these instructions are not supported on ev56
431// Opc = DestType == MVT::f64 ? Alpha::ITOFT : Alpha::ITOFS;
432// BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
433// Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
434// BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
435
Andrew Lenharth40831c52005-01-28 06:57:18 +0000436 return Result;
437 }
438 }
439 assert(0 && "should not get here");
440 return 0;
441}
442
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000443unsigned ISel::SelectExpr(SDOperand N) {
444 unsigned Result;
445 unsigned Tmp1, Tmp2, Tmp3;
446 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000447 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000448
449 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000450 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000451
452 unsigned &Reg = ExprMap[N];
453 if (Reg) return Reg;
454
455 if (N.getOpcode() != ISD::CALL)
456 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000457 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000458 else {
459 // If this is a call instruction, make sure to prepare ALL of the result
460 // values as well as the chain.
461 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000462 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000463 else {
464 Result = MakeReg(Node->getValueType(0));
465 ExprMap[N.getValue(0)] = Result;
466 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
467 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000468 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000469 }
470 }
471
Andrew Lenharth40831c52005-01-28 06:57:18 +0000472 if (DestType == MVT::f64 || DestType == MVT::f32)
473 return SelectExprFP(N, Result);
474
475 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000476 default:
477 Node->dump();
478 assert(0 && "Node not handled!\n");
479
Andrew Lenharth2c594352005-01-29 15:42:07 +0000480 case ISD::ConstantPool:
481 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
482 AlphaLowering.restoreGP(BB);
483 BuildMI(BB, Alpha::LOAD, 1, Result).addConstantPoolIndex(Tmp1);
484 return Result;
485
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000486 case ISD::FrameIndex:
487 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Andrew Lenharth684f2292005-01-30 00:35:27 +0000488 BuildMI(BB, Alpha::LDA, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000489 return Result;
490
491 case ISD::EXTLOAD:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000492 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000493 if (Result != notIn)
494 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000495 else
496 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
497
498 Select(Node->getOperand(0)); // chain
499 Tmp1 = SelectExpr(Node->getOperand(1));
500
501 switch(Node->getValueType(0)) {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000502 default: Node->dump(); assert(0 && "Unknown type to sign extend to.");
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000503 case MVT::i64:
504 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
505 default:
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000506 Node->dump();
507 assert(0 && "Bad extend load!");
Andrew Lenharthd279b412005-01-25 19:58:40 +0000508 case MVT::i64:
509 BuildMI(BB, Alpha::LDQ, 2, Result).addImm(0).addReg(Tmp1);
510 break;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000511 case MVT::i32:
512 BuildMI(BB, Alpha::LDL, 2, Result).addImm(0).addReg(Tmp1);
513 break;
514 case MVT::i16:
515 BuildMI(BB, Alpha::LDWU, 2, Result).addImm(0).addReg(Tmp1);
516 break;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000517 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000518 case MVT::i8:
519 BuildMI(BB, Alpha::LDBU, 2, Result).addImm(0).addReg(Tmp1);
520 break;
521 }
522 break;
523 }
524 return Result;
525
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000526 case ISD::SEXTLOAD:
527 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000528 if (Result != notIn)
529 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000530 else
531 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
532
533 Select(Node->getOperand(0)); // chain
534 Tmp1 = SelectExpr(Node->getOperand(1));
535 switch(Node->getValueType(0)) {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000536 default: Node->dump(); assert(0 && "Unknown type to sign extend to.");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000537 case MVT::i64:
538 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
539 default:
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000540 Node->dump();
541 assert(0 && "Bad sign extend!");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000542 case MVT::i32:
543 BuildMI(BB, Alpha::LDL, 2, Result).addImm(0).addReg(Tmp1);
544 break;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000545 }
546 break;
547 }
548 return Result;
549
550 case ISD::ZEXTLOAD:
551 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000552 if (Result != notIn)
553 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000554 else
555 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
556
557 Select(Node->getOperand(0)); // chain
558 Tmp1 = SelectExpr(Node->getOperand(1));
559 switch(Node->getValueType(0)) {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000560 default: Node->dump(); assert(0 && "Unknown type to zero extend to.");
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000561 case MVT::i64:
562 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
563 default:
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000564 Node->dump();
565 assert(0 && "Bad sign extend!");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000566 case MVT::i16:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000567 BuildMI(BB, Alpha::LDWU, 2, Result).addImm(0).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000568 break;
569 case MVT::i8:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000570 BuildMI(BB, Alpha::LDBU, 2, Result).addImm(0).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000571 break;
572 }
573 break;
574 }
575 return Result;
576
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000577
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000578 case ISD::GlobalAddress:
579 AlphaLowering.restoreGP(BB);
580 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
581 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
582 return Result;
583
584 case ISD::CALL:
585 {
586 Select(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000587
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000588 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000589 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000590
591 //grab the arguments
592 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000593 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000594 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000595 argvregs.push_back(SelectExpr(N.getOperand(i)));
596
Andrew Lenharth684f2292005-01-30 00:35:27 +0000597 //in reg args
598 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
599 {
600 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
601 Alpha::R19, Alpha::R20, Alpha::R21};
602 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
603 Alpha::F19, Alpha::F20, Alpha::F21};
604 switch(N.getOperand(i+2).getValueType()) {
605 default:
606 Node->dump();
607 N.getOperand(i).Val->dump();
608 std::cerr << "Type for " << i << " is: " << N.getOperand(i+2).getValueType() << "\n";
609 assert(0 && "Unknown value type for call");
610 case MVT::i1:
611 case MVT::i8:
612 case MVT::i16:
613 case MVT::i32:
614 case MVT::i64:
615 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
616 break;
617 case MVT::f32:
618 case MVT::f64:
619 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
620 break;
621 }
622 }
623 //in mem args
624 for (int i = 6, e = argvregs.size(); i < e; ++i)
625 {
626 switch(N.getOperand(i+2).getValueType()) {
627 default:
628 Node->dump();
629 N.getOperand(i).Val->dump();
630 std::cerr << "Type for " << i << " is: " << N.getOperand(i+2).getValueType() << "\n";
631 assert(0 && "Unknown value type for call");
632 case MVT::i1:
633 case MVT::i8:
634 case MVT::i16:
635 case MVT::i32:
636 case MVT::i64:
637 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
638 break;
639 case MVT::f32:
640 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
641 break;
642 case MVT::f64:
643 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
644 break;
645 }
646 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000647 //build the right kind of call
648 if (GlobalAddressSDNode *GASD =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000649 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
650 {
651 AlphaLowering.restoreGP(BB);
652 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal(),true);
653 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000654 else if (ExternalSymbolSDNode *ESSDN =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000655 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
656 {
657 AlphaLowering.restoreGP(BB);
658 BuildMI(BB, Alpha::CALL, 0).addExternalSymbol(ESSDN->getSymbol(), true);
659 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000660 else
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000661 {
662 //no need to restore GP as we are doing an indirect call
663 Tmp1 = SelectExpr(N.getOperand(1));
664 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Tmp1).addImm(1);
665 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000666
667 //push the result into a virtual register
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000668
669 switch (Node->getValueType(0)) {
670 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000671 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000672 case MVT::i1:
673 case MVT::i8:
674 case MVT::i16:
675 case MVT::i32:
676 case MVT::i64:
677 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
678 break;
679 case MVT::f32:
680 case MVT::f64:
681 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
682 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000683 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000684 return Result+N.ResNo;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000685 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000686
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000687 case ISD::SIGN_EXTEND:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000688 abort();
689
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000690 case ISD::SIGN_EXTEND_INREG:
691 {
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000692 //Alpha has instructions for a bunch of signed 32 bit stuff
693 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
694 {
695 switch (N.getOperand(0).getOpcode()) {
696 case ISD::ADD:
697 case ISD::SUB:
698 case ISD::MUL:
699 {
700 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
701 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
702 //FIXME: first check for Scaled Adds and Subs!
703 if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
704 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
705 { //Normal imm add/sub
706 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
707 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
708 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
709 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
710 }
711 else
712 { //Normal add/sub
713 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULLi : Alpha::SUBL);
714 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
715 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
716 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
717 }
718 return Result;
719 }
720 default: break; //Fall Though;
721 }
722 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000723 Tmp1 = SelectExpr(N.getOperand(0));
724 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000725 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000726 switch(MVN->getExtraValueType())
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000727 {
728 default:
729 Node->dump();
730 assert(0 && "Sign Extend InReg not there yet");
731 break;
732 case MVT::i32:
733 {
734 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
735 break;
736 }
737 case MVT::i16:
738 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
739 break;
740 case MVT::i8:
741 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
742 break;
743 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000744 return Result;
745 }
746 case ISD::ZERO_EXTEND_INREG:
747 {
748 Tmp1 = SelectExpr(N.getOperand(0));
749 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000750 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000751 switch(MVN->getExtraValueType())
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000752 {
753 default:
754 Node->dump();
755 assert(0 && "Zero Extend InReg not there yet");
756 break;
757 case MVT::i32: Tmp2 = 0xf0; break;
758 case MVT::i16: Tmp2 = 0xfc; break;
759 case MVT::i8: Tmp2 = 0xfe; break;
760 case MVT::i1: //handle this one special
761 BuildMI(BB, Alpha::ANDi, 2, Result).addReg(Tmp1).addImm(1);
762 return Result;
763 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000764 BuildMI(BB, Alpha::ZAPi, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000765 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000766 }
767
768 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000769 {
770 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
771 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
772 bool isConst1 = false;
773 bool isConst2 = false;
774 int dir;
775
776 //Tmp1 = SelectExpr(N.getOperand(0));
777 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000778 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
779 isConst1 = true;
780 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000781 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
782 isConst2 = true;
783
784 switch (SetCC->getCondition()) {
785 default: Node->dump(); assert(0 && "Unknown integer comparison!");
786 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
787 case ISD::SETLT: Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
788 case ISD::SETLE: Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
789 case ISD::SETGT: Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
790 case ISD::SETGE: Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
791 case ISD::SETULT: Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
792 case ISD::SETUGT: Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
793 case ISD::SETULE: Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
794 case ISD::SETUGE: Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000795 case ISD::SETNE: {//Handle this one special
796 //std::cerr << "Alpha does not have a setne.\n";
797 //abort();
798 Tmp1 = SelectExpr(N.getOperand(0));
799 Tmp2 = SelectExpr(N.getOperand(1));
800 Tmp3 = MakeReg(MVT::i64);
801 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
802 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000803 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
804 //BuildMI(BB,Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000805 return Result;
806 }
807 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000808 if (dir == 1) {
809 Tmp1 = SelectExpr(N.getOperand(0));
810 if (isConst2) {
811 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
812 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
813 } else {
814 Tmp2 = SelectExpr(N.getOperand(1));
815 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
816 }
817 } else if (dir == 2) {
818 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +0000819 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000820 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
821 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
822 } else {
823 Tmp2 = SelectExpr(N.getOperand(0));
824 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
825 }
826 } else { //dir == 0
827 if (isConst1) {
828 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
829 Tmp2 = SelectExpr(N.getOperand(1));
830 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
831 } else if (isConst2) {
832 Tmp1 = SelectExpr(N.getOperand(0));
833 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
834 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
835 } else {
836 Tmp1 = SelectExpr(N.getOperand(0));
837 Tmp2 = SelectExpr(N.getOperand(1));
838 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
839 }
840 }
841 }
842 else
843 {
844 Node->dump();
845 assert(0 && "only integer");
846 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000847 }
848 else
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000849 {
850 Node->dump();
851 assert(0 && "Not a setcc in setcc");
852 }
853 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000854 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000855
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000856 case ISD::CopyFromReg:
857 {
Andrew Lenharth40831c52005-01-28 06:57:18 +0000858 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000859 if (Result != notIn)
860 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +0000861 else
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000862 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth40831c52005-01-28 06:57:18 +0000863
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000864 SDOperand Chain = N.getOperand(0);
865
866 Select(Chain);
867 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
868 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
869 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
870 return Result;
871 }
872
Andrew Lenharth2d6f0222005-01-24 19:44:07 +0000873 //Most of the plain arithmetic and logic share the same form, and the same
874 //constant immediate test
875 case ISD::AND:
876 case ISD::OR:
877 case ISD::XOR:
878 case ISD::SHL:
879 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +0000880 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +0000881 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000882 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
883 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +0000884 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
885 {
886 switch(opcode) {
887 case ISD::AND: Opc = Alpha::ANDi; break;
888 case ISD::OR: Opc = Alpha::BISi; break;
889 case ISD::XOR: Opc = Alpha::XORi; break;
890 case ISD::SHL: Opc = Alpha::SLi; break;
891 case ISD::SRL: Opc = Alpha::SRLi; break;
892 case ISD::SRA: Opc = Alpha::SRAi; break;
893 case ISD::MUL: Opc = Alpha::MULQi; break;
894 };
895 Tmp1 = SelectExpr(N.getOperand(0));
896 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
897 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
898 }
899 else
900 {
901 switch(opcode) {
902 case ISD::AND: Opc = Alpha::AND; break;
903 case ISD::OR: Opc = Alpha::BIS; break;
904 case ISD::XOR: Opc = Alpha::XOR; break;
905 case ISD::SHL: Opc = Alpha::SL; break;
906 case ISD::SRL: Opc = Alpha::SRL; break;
907 case ISD::SRA: Opc = Alpha::SRA; break;
908 case ISD::MUL: Opc = Alpha::MULQ; break;
909 };
910 Tmp1 = SelectExpr(N.getOperand(0));
911 Tmp2 = SelectExpr(N.getOperand(1));
912 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
913 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +0000914 return Result;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000915
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000916 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000917 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000918 {
Andrew Lenharth40831c52005-01-28 06:57:18 +0000919 bool isAdd = opcode == ISD::ADD;
920
921 //FIXME: first check for Scaled Adds and Subs!
922 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +0000923 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
924 { //Normal imm add/sub
925 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
926 Tmp1 = SelectExpr(N.getOperand(0));
927 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
928 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
929 }
930 else if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +0000931 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
932 { //LDA //FIXME: expand the above condition a bit
933 Tmp1 = SelectExpr(N.getOperand(0));
934 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
935 if (!isAdd)
936 Tmp2 = -Tmp2;
937 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
938 }
939 else
940 { //Normal add/sub
941 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
942 Tmp1 = SelectExpr(N.getOperand(0));
943 Tmp2 = SelectExpr(N.getOperand(1));
944 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
945 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000946 return Result;
947 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000948
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000949 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +0000950 case ISD::SREM:
951 case ISD::SDIV:
952 case ISD::UDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000953 //FIXME: alpha really doesn't support any of these operations,
954 // the ops are expanded into special library calls with
955 // special calling conventions
956 switch(opcode) {
957 case ISD::UREM: Opc = Alpha::REMQU; break;
958 case ISD::SREM: Opc = Alpha::REMQ; break;
959 case ISD::UDIV: Opc = Alpha::DIVQU; break;
960 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000961 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000962 Tmp1 = SelectExpr(N.getOperand(0));
963 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth02981182005-01-26 01:24:38 +0000964 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000965 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000966// // case ISD::UINT_TO_FP:
967
968// case ISD::FP_TO_SINT:
969// assert (N.getValueType() == MVT::f64 && "Only can convert for doubles");
970// Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
971// Tmp2 = MakeReg(SrcTy);
972// BuildMI(BB, CVTTQ, 1, Tmp2).addReg(Tmp1);
973// BuildMI(BB, FTOIT, 1, Result).addReg(Tmp2);
974// return result;
975
976// // case ISD::FP_TO_UINT:
977
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000978 case ISD::SELECT:
979 {
980 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
981 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
982 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
983 // Get the condition into the zero flag.
984 unsigned dummy = MakeReg(MVT::i64);
985 BuildMI(BB, Alpha::BIS, 2, dummy).addReg(Tmp3).addReg(Tmp3);
986 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
987 return Result;
988 }
989
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000990 case ISD::Constant:
991 {
992 long val = cast<ConstantSDNode>(N)->getValue();
993 BuildMI(BB, Alpha::LOAD_IMM, 1, Result).addImm(val);
994 return Result;
995 }
996
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000997 case ISD::LOAD:
998 {
999 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001000 if (Result != notIn)
1001 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001002 else
1003 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1004
1005 SDOperand Chain = N.getOperand(0);
1006 SDOperand Address = N.getOperand(1);
1007
1008 if (Address.getOpcode() == ISD::GlobalAddress)
1009 {
1010 Select(Chain);
1011 AlphaLowering.restoreGP(BB);
1012 BuildMI(BB, Alpha::LOAD, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1013 }
1014 else
1015 {
1016 Select(Chain);
1017 Tmp2 = SelectExpr(Address);
1018 BuildMI(BB, Alpha::LDQ, 2, Result).addImm(0).addReg(Tmp2);
1019 }
1020 return Result;
1021 }
1022 }
1023
1024 return 0;
1025}
1026
1027void ISel::Select(SDOperand N) {
1028 unsigned Tmp1, Tmp2, Opc;
1029
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001030 // FIXME: Disable for our current expansion model!
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001031 if (/*!N->hasOneUse() &&*/ !ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001032 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001033
1034 SDNode *Node = N.Val;
1035
1036 switch (N.getOpcode()) {
1037
1038 default:
1039 Node->dump(); std::cerr << "\n";
1040 assert(0 && "Node not handled yet!");
1041
1042 case ISD::BRCOND: {
1043 MachineBasicBlock *Dest =
1044 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
1045
1046 Select(N.getOperand(0));
1047 Tmp1 = SelectExpr(N.getOperand(1));
1048 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
1049 return;
1050 }
1051
1052 case ISD::BR: {
1053 MachineBasicBlock *Dest =
1054 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1055
1056 Select(N.getOperand(0));
1057 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
1058 return;
1059 }
1060
1061 case ISD::ImplicitDef:
1062 Select(N.getOperand(0));
1063 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
1064 return;
1065
1066 case ISD::EntryToken: return; // Noop
1067
1068 case ISD::TokenFactor:
1069 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1070 Select(Node->getOperand(i));
1071
1072 //N.Val->dump(); std::cerr << "\n";
1073 //assert(0 && "Node not handled yet!");
1074
1075 return;
1076
1077 case ISD::CopyToReg:
1078 Select(N.getOperand(0));
1079 Tmp1 = SelectExpr(N.getOperand(1));
1080 Tmp2 = cast<RegSDNode>(N)->getReg();
1081
1082 if (Tmp1 != Tmp2) {
1083 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1084 }
1085 return;
1086
1087 case ISD::RET:
1088 switch (N.getNumOperands()) {
1089 default:
1090 std::cerr << N.getNumOperands() << "\n";
1091 for (unsigned i = 0; i < N.getNumOperands(); ++i)
1092 std::cerr << N.getOperand(i).getValueType() << "\n";
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001093 Node->dump();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001094 assert(0 && "Unknown return instruction!");
1095 case 2:
1096 Select(N.getOperand(0));
1097 Tmp1 = SelectExpr(N.getOperand(1));
1098 switch (N.getOperand(1).getValueType()) {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001099 default: Node->dump(); assert(0 && "All other types should have been promoted!!");
1100 case MVT::f64:
1101 case MVT::f32:
1102 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1103 break;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001104 case MVT::i32:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001105 case MVT::i64:
1106 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1107 break;
1108 }
1109 break;
1110 case 1:
1111 Select(N.getOperand(0));
1112 break;
1113 }
1114 //Tmp2 = AlphaLowering.getRetAddr();
1115 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
1116 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
1117 return;
1118
1119 case ISD::STORE:
1120 Select(N.getOperand(0));
1121 Tmp1 = SelectExpr(N.getOperand(1)); //value
1122 if (N.getOperand(2).getOpcode() == ISD::GlobalAddress)
1123 {
1124 AlphaLowering.restoreGP(BB);
1125 BuildMI(BB, Alpha::STORE, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(N.getOperand(2))->getGlobal());
1126 }
1127 else
1128 {
1129 Tmp2 = SelectExpr(N.getOperand(2)); //address
1130 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addImm(0).addReg(Tmp2);
1131 }
1132 return;
1133
1134 case ISD::EXTLOAD:
1135 case ISD::SEXTLOAD:
1136 case ISD::ZEXTLOAD:
1137 case ISD::LOAD:
1138 case ISD::CopyFromReg:
1139 case ISD::CALL:
1140// case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001141 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001142 SelectExpr(N);
1143 return;
1144
1145
1146 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
1147 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001148 if (StoredTy == MVT::i64) {
1149 Node->dump();
1150 assert(StoredTy != MVT::i64 && "Unsupported TRUNCSTORE for this target!");
1151 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001152
1153 Select(N.getOperand(0));
1154 Tmp1 = SelectExpr(N.getOperand(1));
1155 Tmp2 = SelectExpr(N.getOperand(2));
1156
1157 switch (StoredTy) {
Chris Lattnerd7b59d02005-01-30 16:32:48 +00001158 default: Node->dump(); assert(0 && "Unhandled Type");
Andrew Lenharthd279b412005-01-25 19:58:40 +00001159 case MVT::i1: //FIXME: DAG does not promote this load
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001160 case MVT::i8: Opc = Alpha::STB; break;
1161 case MVT::i16: Opc = Alpha::STW; break;
1162 case MVT::i32: Opc = Alpha::STL; break;
1163 }
1164
1165 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(0).addReg(Tmp2);
1166 return;
1167 }
1168
1169 case ISD::ADJCALLSTACKDOWN:
1170 case ISD::ADJCALLSTACKUP:
1171 Select(N.getOperand(0));
1172 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1173
1174 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? Alpha::ADJUSTSTACKDOWN :
1175 Alpha::ADJUSTSTACKUP;
1176 BuildMI(BB, Opc, 1).addImm(Tmp1);
1177 return;
1178 }
1179 assert(0 && "Should not be reached!");
1180}
1181
1182
1183/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1184/// into a machine code representation using pattern matching and a machine
1185/// description file.
1186///
1187FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
1188 return new ISel(TM);
1189}