blob: 731f30337a92385745c74f28b5c4f11cf378bda0 [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 Lenharth7efadce2005-01-31 01:44:26 +0000415 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000416
417 //The hard way:
418 // Spill the integer to memory and reload it from there.
419 unsigned Size = MVT::getSizeInBits(MVT::i64)/8;
420 MachineFunction *F = BB->getParent();
421 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
422
Andrew Lenharth7efadce2005-01-31 01:44:26 +0000423 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addFrameIndex(FrameIdx).addReg(Alpha::F31);
424 BuildMI(BB, Alpha::LDT, 2, Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
425 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
426 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000427
428 //The easy way: doesn't work
429// //so these instructions are not supported on ev56
430// Opc = DestType == MVT::f64 ? Alpha::ITOFT : Alpha::ITOFS;
431// BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
432// Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
433// BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
434
Andrew Lenharth40831c52005-01-28 06:57:18 +0000435 return Result;
436 }
437 }
438 assert(0 && "should not get here");
439 return 0;
440}
441
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000442unsigned ISel::SelectExpr(SDOperand N) {
443 unsigned Result;
444 unsigned Tmp1, Tmp2, Tmp3;
445 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000446 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000447
448 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000449 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000450
451 unsigned &Reg = ExprMap[N];
452 if (Reg) return Reg;
453
454 if (N.getOpcode() != ISD::CALL)
455 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000456 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000457 else {
458 // If this is a call instruction, make sure to prepare ALL of the result
459 // values as well as the chain.
460 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000461 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000462 else {
463 Result = MakeReg(Node->getValueType(0));
464 ExprMap[N.getValue(0)] = Result;
465 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
466 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000467 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000468 }
469 }
470
Andrew Lenharth40831c52005-01-28 06:57:18 +0000471 if (DestType == MVT::f64 || DestType == MVT::f32)
472 return SelectExprFP(N, Result);
473
474 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000475 default:
476 Node->dump();
477 assert(0 && "Node not handled!\n");
478
Andrew Lenharth2c594352005-01-29 15:42:07 +0000479 case ISD::ConstantPool:
480 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
481 AlphaLowering.restoreGP(BB);
482 BuildMI(BB, Alpha::LOAD, 1, Result).addConstantPoolIndex(Tmp1);
483 return Result;
484
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000485 case ISD::FrameIndex:
486 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Andrew Lenharth684f2292005-01-30 00:35:27 +0000487 BuildMI(BB, Alpha::LDA, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000488 return Result;
489
490 case ISD::EXTLOAD:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000491 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000492 if (Result != notIn)
493 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000494 else
495 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
496
497 Select(Node->getOperand(0)); // chain
498 Tmp1 = SelectExpr(Node->getOperand(1));
499
500 switch(Node->getValueType(0)) {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000501 default: Node->dump(); assert(0 && "Unknown type to sign extend to.");
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000502 case MVT::i64:
503 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
504 default:
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000505 Node->dump();
506 assert(0 && "Bad extend load!");
Andrew Lenharthd279b412005-01-25 19:58:40 +0000507 case MVT::i64:
508 BuildMI(BB, Alpha::LDQ, 2, Result).addImm(0).addReg(Tmp1);
509 break;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000510 case MVT::i32:
511 BuildMI(BB, Alpha::LDL, 2, Result).addImm(0).addReg(Tmp1);
512 break;
513 case MVT::i16:
514 BuildMI(BB, Alpha::LDWU, 2, Result).addImm(0).addReg(Tmp1);
515 break;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000516 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000517 case MVT::i8:
518 BuildMI(BB, Alpha::LDBU, 2, Result).addImm(0).addReg(Tmp1);
519 break;
520 }
521 break;
522 }
523 return Result;
524
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000525 case ISD::SEXTLOAD:
526 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000527 if (Result != notIn)
528 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000529 else
530 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
531
532 Select(Node->getOperand(0)); // chain
533 Tmp1 = SelectExpr(Node->getOperand(1));
534 switch(Node->getValueType(0)) {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000535 default: Node->dump(); assert(0 && "Unknown type to sign extend to.");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000536 case MVT::i64:
537 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
538 default:
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000539 Node->dump();
540 assert(0 && "Bad sign extend!");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000541 case MVT::i32:
542 BuildMI(BB, Alpha::LDL, 2, Result).addImm(0).addReg(Tmp1);
543 break;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000544 }
545 break;
546 }
547 return Result;
548
549 case ISD::ZEXTLOAD:
550 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000551 if (Result != notIn)
552 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000553 else
554 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
555
556 Select(Node->getOperand(0)); // chain
557 Tmp1 = SelectExpr(Node->getOperand(1));
558 switch(Node->getValueType(0)) {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000559 default: Node->dump(); assert(0 && "Unknown type to zero extend to.");
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000560 case MVT::i64:
561 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
562 default:
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000563 Node->dump();
564 assert(0 && "Bad sign extend!");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000565 case MVT::i16:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000566 BuildMI(BB, Alpha::LDWU, 2, Result).addImm(0).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000567 break;
568 case MVT::i8:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000569 BuildMI(BB, Alpha::LDBU, 2, Result).addImm(0).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000570 break;
571 }
572 break;
573 }
574 return Result;
575
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000576
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000577 case ISD::GlobalAddress:
578 AlphaLowering.restoreGP(BB);
579 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
580 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
581 return Result;
582
583 case ISD::CALL:
584 {
585 Select(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000586
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000587 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000588 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000589
590 //grab the arguments
591 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000592 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000593 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000594 argvregs.push_back(SelectExpr(N.getOperand(i)));
595
Andrew Lenharth684f2292005-01-30 00:35:27 +0000596 //in reg args
597 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
598 {
599 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
600 Alpha::R19, Alpha::R20, Alpha::R21};
601 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
602 Alpha::F19, Alpha::F20, Alpha::F21};
603 switch(N.getOperand(i+2).getValueType()) {
604 default:
605 Node->dump();
606 N.getOperand(i).Val->dump();
607 std::cerr << "Type for " << i << " is: " << N.getOperand(i+2).getValueType() << "\n";
608 assert(0 && "Unknown value type for call");
609 case MVT::i1:
610 case MVT::i8:
611 case MVT::i16:
612 case MVT::i32:
613 case MVT::i64:
614 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
615 break;
616 case MVT::f32:
617 case MVT::f64:
618 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
619 break;
620 }
621 }
622 //in mem args
623 for (int i = 6, e = argvregs.size(); i < e; ++i)
624 {
625 switch(N.getOperand(i+2).getValueType()) {
626 default:
627 Node->dump();
628 N.getOperand(i).Val->dump();
629 std::cerr << "Type for " << i << " is: " << N.getOperand(i+2).getValueType() << "\n";
630 assert(0 && "Unknown value type for call");
631 case MVT::i1:
632 case MVT::i8:
633 case MVT::i16:
634 case MVT::i32:
635 case MVT::i64:
636 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
637 break;
638 case MVT::f32:
639 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
640 break;
641 case MVT::f64:
642 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
643 break;
644 }
645 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000646 //build the right kind of call
647 if (GlobalAddressSDNode *GASD =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000648 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
649 {
650 AlphaLowering.restoreGP(BB);
651 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal(),true);
652 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000653 else if (ExternalSymbolSDNode *ESSDN =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000654 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
655 {
656 AlphaLowering.restoreGP(BB);
657 BuildMI(BB, Alpha::CALL, 0).addExternalSymbol(ESSDN->getSymbol(), true);
658 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000659 else
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000660 {
661 //no need to restore GP as we are doing an indirect call
662 Tmp1 = SelectExpr(N.getOperand(1));
663 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Tmp1).addImm(1);
664 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000665
666 //push the result into a virtual register
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000667
668 switch (Node->getValueType(0)) {
669 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000670 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000671 case MVT::i1:
672 case MVT::i8:
673 case MVT::i16:
674 case MVT::i32:
675 case MVT::i64:
676 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
677 break;
678 case MVT::f32:
679 case MVT::f64:
680 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
681 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000682 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000683 return Result+N.ResNo;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000684 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000685
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000686 case ISD::SIGN_EXTEND:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000687 abort();
688
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000689 case ISD::SIGN_EXTEND_INREG:
690 {
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000691 //Alpha has instructions for a bunch of signed 32 bit stuff
692 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
693 {
694 switch (N.getOperand(0).getOpcode()) {
695 case ISD::ADD:
696 case ISD::SUB:
697 case ISD::MUL:
698 {
699 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
700 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
701 //FIXME: first check for Scaled Adds and Subs!
702 if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
703 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
704 { //Normal imm add/sub
705 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
706 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
707 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
708 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
709 }
710 else
711 { //Normal add/sub
712 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULLi : Alpha::SUBL);
713 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
714 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
715 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
716 }
717 return Result;
718 }
719 default: break; //Fall Though;
720 }
721 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000722 Tmp1 = SelectExpr(N.getOperand(0));
723 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000724 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000725 switch(MVN->getExtraValueType())
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000726 {
727 default:
728 Node->dump();
729 assert(0 && "Sign Extend InReg not there yet");
730 break;
731 case MVT::i32:
732 {
733 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
734 break;
735 }
736 case MVT::i16:
737 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
738 break;
739 case MVT::i8:
740 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
741 break;
742 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000743 return Result;
744 }
745 case ISD::ZERO_EXTEND_INREG:
746 {
747 Tmp1 = SelectExpr(N.getOperand(0));
748 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000749 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000750 switch(MVN->getExtraValueType())
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000751 {
752 default:
753 Node->dump();
754 assert(0 && "Zero Extend InReg not there yet");
755 break;
756 case MVT::i32: Tmp2 = 0xf0; break;
757 case MVT::i16: Tmp2 = 0xfc; break;
758 case MVT::i8: Tmp2 = 0xfe; break;
759 case MVT::i1: //handle this one special
760 BuildMI(BB, Alpha::ANDi, 2, Result).addReg(Tmp1).addImm(1);
761 return Result;
762 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000763 BuildMI(BB, Alpha::ZAPi, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000764 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000765 }
766
767 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000768 {
769 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
770 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
771 bool isConst1 = false;
772 bool isConst2 = false;
773 int dir;
774
775 //Tmp1 = SelectExpr(N.getOperand(0));
776 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000777 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
778 isConst1 = true;
779 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000780 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
781 isConst2 = true;
782
783 switch (SetCC->getCondition()) {
784 default: Node->dump(); assert(0 && "Unknown integer comparison!");
785 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
786 case ISD::SETLT: Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
787 case ISD::SETLE: Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
788 case ISD::SETGT: Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
789 case ISD::SETGE: Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
790 case ISD::SETULT: Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
791 case ISD::SETUGT: Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
792 case ISD::SETULE: Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
793 case ISD::SETUGE: Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000794 case ISD::SETNE: {//Handle this one special
795 //std::cerr << "Alpha does not have a setne.\n";
796 //abort();
797 Tmp1 = SelectExpr(N.getOperand(0));
798 Tmp2 = SelectExpr(N.getOperand(1));
799 Tmp3 = MakeReg(MVT::i64);
800 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
801 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000802 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
803 //BuildMI(BB,Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000804 return Result;
805 }
806 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000807 if (dir == 1) {
808 Tmp1 = SelectExpr(N.getOperand(0));
809 if (isConst2) {
810 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
811 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
812 } else {
813 Tmp2 = SelectExpr(N.getOperand(1));
814 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
815 }
816 } else if (dir == 2) {
817 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +0000818 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000819 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
820 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
821 } else {
822 Tmp2 = SelectExpr(N.getOperand(0));
823 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
824 }
825 } else { //dir == 0
826 if (isConst1) {
827 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
828 Tmp2 = SelectExpr(N.getOperand(1));
829 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
830 } else if (isConst2) {
831 Tmp1 = SelectExpr(N.getOperand(0));
832 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
833 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
834 } else {
835 Tmp1 = SelectExpr(N.getOperand(0));
836 Tmp2 = SelectExpr(N.getOperand(1));
837 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
838 }
839 }
840 }
841 else
842 {
843 Node->dump();
844 assert(0 && "only integer");
845 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000846 }
847 else
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000848 {
849 Node->dump();
850 assert(0 && "Not a setcc in setcc");
851 }
852 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000853 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000854
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000855 case ISD::CopyFromReg:
856 {
Andrew Lenharth40831c52005-01-28 06:57:18 +0000857 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000858 if (Result != notIn)
859 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +0000860 else
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000861 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth40831c52005-01-28 06:57:18 +0000862
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000863 SDOperand Chain = N.getOperand(0);
864
865 Select(Chain);
866 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
867 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
868 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
869 return Result;
870 }
871
Andrew Lenharth2d6f0222005-01-24 19:44:07 +0000872 //Most of the plain arithmetic and logic share the same form, and the same
873 //constant immediate test
874 case ISD::AND:
875 case ISD::OR:
876 case ISD::XOR:
877 case ISD::SHL:
878 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +0000879 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +0000880 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000881 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
882 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +0000883 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
884 {
885 switch(opcode) {
886 case ISD::AND: Opc = Alpha::ANDi; break;
887 case ISD::OR: Opc = Alpha::BISi; break;
888 case ISD::XOR: Opc = Alpha::XORi; break;
889 case ISD::SHL: Opc = Alpha::SLi; break;
890 case ISD::SRL: Opc = Alpha::SRLi; break;
891 case ISD::SRA: Opc = Alpha::SRAi; break;
892 case ISD::MUL: Opc = Alpha::MULQi; break;
893 };
894 Tmp1 = SelectExpr(N.getOperand(0));
895 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
896 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
897 }
898 else
899 {
900 switch(opcode) {
901 case ISD::AND: Opc = Alpha::AND; break;
902 case ISD::OR: Opc = Alpha::BIS; break;
903 case ISD::XOR: Opc = Alpha::XOR; break;
904 case ISD::SHL: Opc = Alpha::SL; break;
905 case ISD::SRL: Opc = Alpha::SRL; break;
906 case ISD::SRA: Opc = Alpha::SRA; break;
907 case ISD::MUL: Opc = Alpha::MULQ; break;
908 };
909 Tmp1 = SelectExpr(N.getOperand(0));
910 Tmp2 = SelectExpr(N.getOperand(1));
911 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
912 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +0000913 return Result;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000914
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000915 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000916 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000917 {
Andrew Lenharth40831c52005-01-28 06:57:18 +0000918 bool isAdd = opcode == ISD::ADD;
919
920 //FIXME: first check for Scaled Adds and Subs!
921 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +0000922 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
923 { //Normal imm add/sub
924 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
925 Tmp1 = SelectExpr(N.getOperand(0));
926 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
927 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
928 }
929 else if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +0000930 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
931 { //LDA //FIXME: expand the above condition a bit
932 Tmp1 = SelectExpr(N.getOperand(0));
933 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
934 if (!isAdd)
935 Tmp2 = -Tmp2;
936 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
937 }
938 else
939 { //Normal add/sub
940 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
941 Tmp1 = SelectExpr(N.getOperand(0));
942 Tmp2 = SelectExpr(N.getOperand(1));
943 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
944 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000945 return Result;
946 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000947
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000948 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +0000949 case ISD::SREM:
950 case ISD::SDIV:
951 case ISD::UDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000952 //FIXME: alpha really doesn't support any of these operations,
953 // the ops are expanded into special library calls with
954 // special calling conventions
955 switch(opcode) {
956 case ISD::UREM: Opc = Alpha::REMQU; break;
957 case ISD::SREM: Opc = Alpha::REMQ; break;
958 case ISD::UDIV: Opc = Alpha::DIVQU; break;
959 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000960 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000961 Tmp1 = SelectExpr(N.getOperand(0));
962 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth02981182005-01-26 01:24:38 +0000963 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000964 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000965// // case ISD::UINT_TO_FP:
966
Andrew Lenharth7efadce2005-01-31 01:44:26 +0000967
968 case ISD::FP_TO_SINT:
969 {
970 assert (DestType == MVT::i64 && "only quads can be loaded to");
971 MVT::ValueType SrcType = N.getOperand(0).getValueType();
972 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
973
974 //The hard way:
975 // Spill the integer to memory and reload it from there.
976 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
977 MachineFunction *F = BB->getParent();
978 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
979
980 //CVTTQ STT LDQ
981 //CVTST CVTTQ STT LDQ
982 if (SrcType == MVT::f32)
983 {
984 Tmp2 = MakeReg(MVT::f64);
985 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
986 Tmp1 = Tmp2;
987 }
988 Tmp2 = MakeReg(MVT::f64);
989 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
990 BuildMI(BB, Alpha::STT, 3).addReg(Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
991 BuildMI(BB, Alpha::LDQ, 2, Result).addFrameIndex(FrameIdx).addReg(Alpha::F31);
992
993 return Result;
994 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000995
996// // case ISD::FP_TO_UINT:
997
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000998 case ISD::SELECT:
999 {
1000 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1001 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1002 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1003 // Get the condition into the zero flag.
1004 unsigned dummy = MakeReg(MVT::i64);
1005 BuildMI(BB, Alpha::BIS, 2, dummy).addReg(Tmp3).addReg(Tmp3);
1006 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
1007 return Result;
1008 }
1009
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001010 case ISD::Constant:
1011 {
1012 long val = cast<ConstantSDNode>(N)->getValue();
1013 BuildMI(BB, Alpha::LOAD_IMM, 1, Result).addImm(val);
1014 return Result;
1015 }
1016
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001017 case ISD::LOAD:
1018 {
1019 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001020 if (Result != notIn)
1021 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001022 else
1023 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1024
1025 SDOperand Chain = N.getOperand(0);
1026 SDOperand Address = N.getOperand(1);
1027
1028 if (Address.getOpcode() == ISD::GlobalAddress)
1029 {
1030 Select(Chain);
1031 AlphaLowering.restoreGP(BB);
1032 BuildMI(BB, Alpha::LOAD, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1033 }
1034 else
1035 {
1036 Select(Chain);
1037 Tmp2 = SelectExpr(Address);
1038 BuildMI(BB, Alpha::LDQ, 2, Result).addImm(0).addReg(Tmp2);
1039 }
1040 return Result;
1041 }
1042 }
1043
1044 return 0;
1045}
1046
1047void ISel::Select(SDOperand N) {
1048 unsigned Tmp1, Tmp2, Opc;
1049
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001050 // FIXME: Disable for our current expansion model!
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001051 if (/*!N->hasOneUse() &&*/ !ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001052 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001053
1054 SDNode *Node = N.Val;
1055
1056 switch (N.getOpcode()) {
1057
1058 default:
1059 Node->dump(); std::cerr << "\n";
1060 assert(0 && "Node not handled yet!");
1061
1062 case ISD::BRCOND: {
1063 MachineBasicBlock *Dest =
1064 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
1065
1066 Select(N.getOperand(0));
1067 Tmp1 = SelectExpr(N.getOperand(1));
1068 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
1069 return;
1070 }
1071
1072 case ISD::BR: {
1073 MachineBasicBlock *Dest =
1074 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1075
1076 Select(N.getOperand(0));
1077 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
1078 return;
1079 }
1080
1081 case ISD::ImplicitDef:
1082 Select(N.getOperand(0));
1083 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
1084 return;
1085
1086 case ISD::EntryToken: return; // Noop
1087
1088 case ISD::TokenFactor:
1089 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1090 Select(Node->getOperand(i));
1091
1092 //N.Val->dump(); std::cerr << "\n";
1093 //assert(0 && "Node not handled yet!");
1094
1095 return;
1096
1097 case ISD::CopyToReg:
1098 Select(N.getOperand(0));
1099 Tmp1 = SelectExpr(N.getOperand(1));
1100 Tmp2 = cast<RegSDNode>(N)->getReg();
1101
1102 if (Tmp1 != Tmp2) {
1103 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1104 }
1105 return;
1106
1107 case ISD::RET:
1108 switch (N.getNumOperands()) {
1109 default:
1110 std::cerr << N.getNumOperands() << "\n";
1111 for (unsigned i = 0; i < N.getNumOperands(); ++i)
1112 std::cerr << N.getOperand(i).getValueType() << "\n";
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001113 Node->dump();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001114 assert(0 && "Unknown return instruction!");
1115 case 2:
1116 Select(N.getOperand(0));
1117 Tmp1 = SelectExpr(N.getOperand(1));
1118 switch (N.getOperand(1).getValueType()) {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001119 default: Node->dump(); assert(0 && "All other types should have been promoted!!");
1120 case MVT::f64:
1121 case MVT::f32:
1122 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1123 break;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001124 case MVT::i32:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001125 case MVT::i64:
1126 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1127 break;
1128 }
1129 break;
1130 case 1:
1131 Select(N.getOperand(0));
1132 break;
1133 }
1134 //Tmp2 = AlphaLowering.getRetAddr();
1135 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
1136 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
1137 return;
1138
1139 case ISD::STORE:
1140 Select(N.getOperand(0));
1141 Tmp1 = SelectExpr(N.getOperand(1)); //value
1142 if (N.getOperand(2).getOpcode() == ISD::GlobalAddress)
1143 {
1144 AlphaLowering.restoreGP(BB);
1145 BuildMI(BB, Alpha::STORE, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(N.getOperand(2))->getGlobal());
1146 }
1147 else
1148 {
1149 Tmp2 = SelectExpr(N.getOperand(2)); //address
1150 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addImm(0).addReg(Tmp2);
1151 }
1152 return;
1153
1154 case ISD::EXTLOAD:
1155 case ISD::SEXTLOAD:
1156 case ISD::ZEXTLOAD:
1157 case ISD::LOAD:
1158 case ISD::CopyFromReg:
1159 case ISD::CALL:
1160// case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001161 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001162 SelectExpr(N);
1163 return;
1164
1165
1166 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
1167 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001168 if (StoredTy == MVT::i64) {
1169 Node->dump();
1170 assert(StoredTy != MVT::i64 && "Unsupported TRUNCSTORE for this target!");
1171 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001172
1173 Select(N.getOperand(0));
1174 Tmp1 = SelectExpr(N.getOperand(1));
1175 Tmp2 = SelectExpr(N.getOperand(2));
1176
1177 switch (StoredTy) {
Chris Lattnerd7b59d02005-01-30 16:32:48 +00001178 default: Node->dump(); assert(0 && "Unhandled Type");
Andrew Lenharthd279b412005-01-25 19:58:40 +00001179 case MVT::i1: //FIXME: DAG does not promote this load
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001180 case MVT::i8: Opc = Alpha::STB; break;
1181 case MVT::i16: Opc = Alpha::STW; break;
1182 case MVT::i32: Opc = Alpha::STL; break;
1183 }
1184
1185 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(0).addReg(Tmp2);
1186 return;
1187 }
1188
1189 case ISD::ADJCALLSTACKDOWN:
1190 case ISD::ADJCALLSTACKUP:
1191 Select(N.getOperand(0));
1192 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1193
1194 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? Alpha::ADJUSTSTACKDOWN :
1195 Alpha::ADJUSTSTACKUP;
1196 BuildMI(BB, Opc, 1).addImm(Tmp1);
1197 return;
1198 }
1199 assert(0 && "Should not be reached!");
1200}
1201
1202
1203/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1204/// into a machine code representation using pattern matching and a machine
1205/// description file.
1206///
1207FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
1208 return new ISel(TM);
1209}