blob: f65484588403fa5dc7c0365b54e299e890577d33 [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
310 case ISD::CopyFromReg:
311 {
312 // Make sure we generate both values.
313 if (Result != notIn)
314 ExprMap[N.getValue(1)] = notIn; // Generate the token
315 else
316 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
317
318 SDOperand Chain = N.getOperand(0);
319
320 Select(Chain);
321 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
322 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
323 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
324 return Result;
325 }
326
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000327 case ISD::LOAD:
328 {
329 // Make sure we generate both values.
330 if (Result != notIn)
331 ExprMap[N.getValue(1)] = notIn; // Generate the token
332 else
333 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
334
335 SDOperand Chain = N.getOperand(0);
336 SDOperand Address = N.getOperand(1);
337
338 if (Address.getOpcode() == ISD::GlobalAddress)
339 {
340 Select(Chain);
341 AlphaLowering.restoreGP(BB);
342 Opc = DestType == MVT::f64 ? Alpha::LDS : Alpha::LDT;
343 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
344 }
345 else
346 {
347 Select(Chain);
348 Tmp2 = SelectExpr(Address);
349 Opc = DestType == MVT::f64 ? Alpha::LDS : Alpha::LDT;
350 BuildMI(BB, Opc, 2, Result).addImm(0).addReg(Tmp2);
351 }
352 return Result;
353 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000354 case ISD::ConstantFP:
355 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
356 if (CN->isExactlyValue(+0.0)) {
357 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
358 } else {
359 abort();
360 }
361 }
362 return Result;
363
364 case ISD::MUL:
365 case ISD::ADD:
366 case ISD::SUB:
367 case ISD::SDIV:
368 switch( opcode ) {
369 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
370 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
371 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
372 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
373 };
374 Tmp1 = SelectExpr(N.getOperand(0));
375 Tmp2 = SelectExpr(N.getOperand(1));
376 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
377 return Result;
378
Andrew Lenharth2c594352005-01-29 15:42:07 +0000379 case ISD::EXTLOAD:
380 //include a conversion sequence for float loads to double
381 if (Result != notIn)
382 ExprMap[N.getValue(1)] = notIn; // Generate the token
383 else
384 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
385
386 Tmp2 = MakeReg(MVT::f32);
387
388 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
389 if (Node->getValueType(0) == MVT::f64) {
390 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
391 "Bad EXTLOAD!");
392 BuildMI(BB, Alpha::LDS, 1, Tmp2).addConstantPoolIndex(CP->getIndex());
393 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp2);
394 return Result;
395 }
396 Select(Node->getOperand(0)); // chain
397 Tmp1 = SelectExpr(Node->getOperand(1));
398 BuildMI(BB, Alpha::LDS, 1, Tmp2).addReg(Tmp1);
399 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp2);
400 return Result;
401
402
403 //case ISD::UINT_TO_FP:
404
405 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000406 {
407 assert (N.getOperand(0).getValueType() == MVT::i64 && "only quads can be loaded from");
408 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth2c594352005-01-29 15:42:07 +0000409
410 //The hard way:
411 // Spill the integer to memory and reload it from there.
412 unsigned Size = MVT::getSizeInBits(MVT::i64)/8;
413 MachineFunction *F = BB->getParent();
414 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
415
416 //STL LDS
417 //STQ LDT
418 Opc = DestType == MVT::f64 ? Alpha::STQ : Alpha::STL;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000419 BuildMI(BB, Opc, 2).addReg(Tmp1).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000420 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000421 BuildMI(BB, Opc, 1, Result).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000422
423 //The easy way: doesn't work
424// //so these instructions are not supported on ev56
425// Opc = DestType == MVT::f64 ? Alpha::ITOFT : Alpha::ITOFS;
426// BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
427// Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
428// BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
429
Andrew Lenharth40831c52005-01-28 06:57:18 +0000430 return Result;
431 }
432 }
433 assert(0 && "should not get here");
434 return 0;
435}
436
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000437unsigned ISel::SelectExpr(SDOperand N) {
438 unsigned Result;
439 unsigned Tmp1, Tmp2, Tmp3;
440 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000441 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000442
443 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000444 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000445
446 unsigned &Reg = ExprMap[N];
447 if (Reg) return Reg;
448
449 if (N.getOpcode() != ISD::CALL)
450 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000451 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000452 else {
453 // If this is a call instruction, make sure to prepare ALL of the result
454 // values as well as the chain.
455 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000456 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000457 else {
458 Result = MakeReg(Node->getValueType(0));
459 ExprMap[N.getValue(0)] = Result;
460 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
461 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000462 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000463 }
464 }
465
Andrew Lenharth40831c52005-01-28 06:57:18 +0000466 if (DestType == MVT::f64 || DestType == MVT::f32)
467 return SelectExprFP(N, Result);
468
469 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000470 default:
471 Node->dump();
472 assert(0 && "Node not handled!\n");
473
Andrew Lenharth2c594352005-01-29 15:42:07 +0000474 case ISD::ConstantPool:
475 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
476 AlphaLowering.restoreGP(BB);
477 BuildMI(BB, Alpha::LOAD, 1, Result).addConstantPoolIndex(Tmp1);
478 return Result;
479
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000480 case ISD::FrameIndex:
481 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Andrew Lenharth684f2292005-01-30 00:35:27 +0000482 BuildMI(BB, Alpha::LDA, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000483 return Result;
484
485 case ISD::EXTLOAD:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000486 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000487 if (Result != notIn)
488 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000489 else
490 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
491
492 Select(Node->getOperand(0)); // chain
493 Tmp1 = SelectExpr(Node->getOperand(1));
494
495 switch(Node->getValueType(0)) {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000496 default: Node->dump(); assert(0 && "Unknown type to sign extend to.");
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000497 case MVT::i64:
498 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
499 default:
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000500 Node->dump();
501 assert(0 && "Bad extend load!");
Andrew Lenharthd279b412005-01-25 19:58:40 +0000502 case MVT::i64:
503 BuildMI(BB, Alpha::LDQ, 2, Result).addImm(0).addReg(Tmp1);
504 break;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000505 case MVT::i32:
506 BuildMI(BB, Alpha::LDL, 2, Result).addImm(0).addReg(Tmp1);
507 break;
508 case MVT::i16:
509 BuildMI(BB, Alpha::LDWU, 2, Result).addImm(0).addReg(Tmp1);
510 break;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000511 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000512 case MVT::i8:
513 BuildMI(BB, Alpha::LDBU, 2, Result).addImm(0).addReg(Tmp1);
514 break;
515 }
516 break;
517 }
518 return Result;
519
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000520 case ISD::SEXTLOAD:
521 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000522 if (Result != notIn)
523 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000524 else
525 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
526
527 Select(Node->getOperand(0)); // chain
528 Tmp1 = SelectExpr(Node->getOperand(1));
529 switch(Node->getValueType(0)) {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000530 default: Node->dump(); assert(0 && "Unknown type to sign extend to.");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000531 case MVT::i64:
532 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
533 default:
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000534 Node->dump();
535 assert(0 && "Bad sign extend!");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000536 case MVT::i32:
537 BuildMI(BB, Alpha::LDL, 2, Result).addImm(0).addReg(Tmp1);
538 break;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000539 }
540 break;
541 }
542 return Result;
543
544 case ISD::ZEXTLOAD:
545 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000546 if (Result != notIn)
547 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000548 else
549 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
550
551 Select(Node->getOperand(0)); // chain
552 Tmp1 = SelectExpr(Node->getOperand(1));
553 switch(Node->getValueType(0)) {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000554 default: Node->dump(); assert(0 && "Unknown type to zero extend to.");
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000555 case MVT::i64:
556 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
557 default:
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000558 Node->dump();
559 assert(0 && "Bad sign extend!");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000560 case MVT::i16:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000561 BuildMI(BB, Alpha::LDWU, 2, Result).addImm(0).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000562 break;
563 case MVT::i8:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000564 BuildMI(BB, Alpha::LDBU, 2, Result).addImm(0).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000565 break;
566 }
567 break;
568 }
569 return Result;
570
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000571
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000572 case ISD::GlobalAddress:
573 AlphaLowering.restoreGP(BB);
574 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
575 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
576 return Result;
577
578 case ISD::CALL:
579 {
580 Select(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000581
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000582 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000583 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000584
585 //grab the arguments
586 std::vector<unsigned> argvregs;
587 assert(Node->getNumOperands() < 8 && "Only 6 args supported");
588 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000589 argvregs.push_back(SelectExpr(N.getOperand(i)));
590
Andrew Lenharth684f2292005-01-30 00:35:27 +0000591 //in reg args
592 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
593 {
594 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
595 Alpha::R19, Alpha::R20, Alpha::R21};
596 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
597 Alpha::F19, Alpha::F20, Alpha::F21};
598 switch(N.getOperand(i+2).getValueType()) {
599 default:
600 Node->dump();
601 N.getOperand(i).Val->dump();
602 std::cerr << "Type for " << i << " is: " << N.getOperand(i+2).getValueType() << "\n";
603 assert(0 && "Unknown value type for call");
604 case MVT::i1:
605 case MVT::i8:
606 case MVT::i16:
607 case MVT::i32:
608 case MVT::i64:
609 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
610 break;
611 case MVT::f32:
612 case MVT::f64:
613 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
614 break;
615 }
616 }
617 //in mem args
618 for (int i = 6, e = argvregs.size(); i < e; ++i)
619 {
620 switch(N.getOperand(i+2).getValueType()) {
621 default:
622 Node->dump();
623 N.getOperand(i).Val->dump();
624 std::cerr << "Type for " << i << " is: " << N.getOperand(i+2).getValueType() << "\n";
625 assert(0 && "Unknown value type for call");
626 case MVT::i1:
627 case MVT::i8:
628 case MVT::i16:
629 case MVT::i32:
630 case MVT::i64:
631 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
632 break;
633 case MVT::f32:
634 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
635 break;
636 case MVT::f64:
637 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
638 break;
639 }
640 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000641 //build the right kind of call
642 if (GlobalAddressSDNode *GASD =
643 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
644 {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000645 AlphaLowering.restoreGP(BB);
646 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal(),true);
647 }
648 else if (ExternalSymbolSDNode *ESSDN =
649 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
650 {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000651 AlphaLowering.restoreGP(BB);
652 BuildMI(BB, Alpha::CALL, 0).addExternalSymbol(ESSDN->getSymbol(), true);
653 }
654 else
655 {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000656 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000657 AlphaLowering.restoreGP(BB);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000658 BuildMI(BB, Alpha::CALL, 1).addReg(Tmp1);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000659 }
660
661 //push the result into a virtual register
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000662
663 switch (Node->getValueType(0)) {
664 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000665 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000666 case MVT::i1:
667 case MVT::i8:
668 case MVT::i16:
669 case MVT::i32:
670 case MVT::i64:
671 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
672 break;
673 case MVT::f32:
674 case MVT::f64:
675 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
676 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000677 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000678 return Result+N.ResNo;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000679 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000680
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000681 case ISD::SIGN_EXTEND:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000682 abort();
683
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000684 case ISD::SIGN_EXTEND_INREG:
685 {
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000686 //Alpha has instructions for a bunch of signed 32 bit stuff
687 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
688 {
689 switch (N.getOperand(0).getOpcode()) {
690 case ISD::ADD:
691 case ISD::SUB:
692 case ISD::MUL:
693 {
694 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
695 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
696 //FIXME: first check for Scaled Adds and Subs!
697 if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
698 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
699 { //Normal imm add/sub
700 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
701 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
702 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
703 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
704 }
705 else
706 { //Normal add/sub
707 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULLi : Alpha::SUBL);
708 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
709 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
710 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
711 }
712 return Result;
713 }
714 default: break; //Fall Though;
715 }
716 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000717 Tmp1 = SelectExpr(N.getOperand(0));
718 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000719 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000720 switch(MVN->getExtraValueType())
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000721 {
722 default:
723 Node->dump();
724 assert(0 && "Sign Extend InReg not there yet");
725 break;
726 case MVT::i32:
727 {
728 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
729 break;
730 }
731 case MVT::i16:
732 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
733 break;
734 case MVT::i8:
735 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
736 break;
737 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000738 return Result;
739 }
740 case ISD::ZERO_EXTEND_INREG:
741 {
742 Tmp1 = SelectExpr(N.getOperand(0));
743 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000744 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000745 switch(MVN->getExtraValueType())
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000746 {
747 default:
748 Node->dump();
749 assert(0 && "Zero Extend InReg not there yet");
750 break;
751 case MVT::i32: Tmp2 = 0xf0; break;
752 case MVT::i16: Tmp2 = 0xfc; break;
753 case MVT::i8: Tmp2 = 0xfe; break;
754 case MVT::i1: //handle this one special
755 BuildMI(BB, Alpha::ANDi, 2, Result).addReg(Tmp1).addImm(1);
756 return Result;
757 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000758 BuildMI(BB, Alpha::ZAPi, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000759 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000760 }
761
762 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000763 {
764 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
765 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
766 bool isConst1 = false;
767 bool isConst2 = false;
768 int dir;
769
770 //Tmp1 = SelectExpr(N.getOperand(0));
771 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000772 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
773 isConst1 = true;
774 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000775 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
776 isConst2 = true;
777
778 switch (SetCC->getCondition()) {
779 default: Node->dump(); assert(0 && "Unknown integer comparison!");
780 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
781 case ISD::SETLT: Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
782 case ISD::SETLE: Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
783 case ISD::SETGT: Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
784 case ISD::SETGE: Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
785 case ISD::SETULT: Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
786 case ISD::SETUGT: Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
787 case ISD::SETULE: Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
788 case ISD::SETUGE: Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000789 case ISD::SETNE: {//Handle this one special
790 //std::cerr << "Alpha does not have a setne.\n";
791 //abort();
792 Tmp1 = SelectExpr(N.getOperand(0));
793 Tmp2 = SelectExpr(N.getOperand(1));
794 Tmp3 = MakeReg(MVT::i64);
795 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
796 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000797 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
798 //BuildMI(BB,Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000799 return Result;
800 }
801 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000802 if (dir == 1) {
803 Tmp1 = SelectExpr(N.getOperand(0));
804 if (isConst2) {
805 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
806 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
807 } else {
808 Tmp2 = SelectExpr(N.getOperand(1));
809 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
810 }
811 } else if (dir == 2) {
812 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +0000813 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000814 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
815 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
816 } else {
817 Tmp2 = SelectExpr(N.getOperand(0));
818 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
819 }
820 } else { //dir == 0
821 if (isConst1) {
822 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
823 Tmp2 = SelectExpr(N.getOperand(1));
824 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
825 } else if (isConst2) {
826 Tmp1 = SelectExpr(N.getOperand(0));
827 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
828 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
829 } else {
830 Tmp1 = SelectExpr(N.getOperand(0));
831 Tmp2 = SelectExpr(N.getOperand(1));
832 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
833 }
834 }
835 }
836 else
837 {
838 Node->dump();
839 assert(0 && "only integer");
840 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000841 }
842 else
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000843 {
844 Node->dump();
845 assert(0 && "Not a setcc in setcc");
846 }
847 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000848 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000849
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000850 case ISD::CopyFromReg:
851 {
Andrew Lenharth40831c52005-01-28 06:57:18 +0000852 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000853 if (Result != notIn)
854 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +0000855 else
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000856 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth40831c52005-01-28 06:57:18 +0000857
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000858 SDOperand Chain = N.getOperand(0);
859
860 Select(Chain);
861 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
862 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
863 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
864 return Result;
865 }
866
Andrew Lenharth2d6f0222005-01-24 19:44:07 +0000867 //Most of the plain arithmetic and logic share the same form, and the same
868 //constant immediate test
869 case ISD::AND:
870 case ISD::OR:
871 case ISD::XOR:
872 case ISD::SHL:
873 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +0000874 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +0000875 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000876 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
877 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +0000878 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
879 {
880 switch(opcode) {
881 case ISD::AND: Opc = Alpha::ANDi; break;
882 case ISD::OR: Opc = Alpha::BISi; break;
883 case ISD::XOR: Opc = Alpha::XORi; break;
884 case ISD::SHL: Opc = Alpha::SLi; break;
885 case ISD::SRL: Opc = Alpha::SRLi; break;
886 case ISD::SRA: Opc = Alpha::SRAi; break;
887 case ISD::MUL: Opc = Alpha::MULQi; break;
888 };
889 Tmp1 = SelectExpr(N.getOperand(0));
890 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
891 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
892 }
893 else
894 {
895 switch(opcode) {
896 case ISD::AND: Opc = Alpha::AND; break;
897 case ISD::OR: Opc = Alpha::BIS; break;
898 case ISD::XOR: Opc = Alpha::XOR; break;
899 case ISD::SHL: Opc = Alpha::SL; break;
900 case ISD::SRL: Opc = Alpha::SRL; break;
901 case ISD::SRA: Opc = Alpha::SRA; break;
902 case ISD::MUL: Opc = Alpha::MULQ; break;
903 };
904 Tmp1 = SelectExpr(N.getOperand(0));
905 Tmp2 = SelectExpr(N.getOperand(1));
906 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
907 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +0000908 return Result;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000909
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000910 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000911 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000912 {
Andrew Lenharth40831c52005-01-28 06:57:18 +0000913 bool isAdd = opcode == ISD::ADD;
914
915 //FIXME: first check for Scaled Adds and Subs!
916 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +0000917 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
918 { //Normal imm add/sub
919 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
920 Tmp1 = SelectExpr(N.getOperand(0));
921 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
922 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
923 }
924 else if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +0000925 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
926 { //LDA //FIXME: expand the above condition a bit
927 Tmp1 = SelectExpr(N.getOperand(0));
928 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
929 if (!isAdd)
930 Tmp2 = -Tmp2;
931 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
932 }
933 else
934 { //Normal add/sub
935 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
936 Tmp1 = SelectExpr(N.getOperand(0));
937 Tmp2 = SelectExpr(N.getOperand(1));
938 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
939 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000940 return Result;
941 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000942
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000943 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +0000944 case ISD::SREM:
945 case ISD::SDIV:
946 case ISD::UDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000947 //FIXME: alpha really doesn't support any of these operations,
948 // the ops are expanded into special library calls with
949 // special calling conventions
950 switch(opcode) {
951 case ISD::UREM: Opc = Alpha::REMQU; break;
952 case ISD::SREM: Opc = Alpha::REMQ; break;
953 case ISD::UDIV: Opc = Alpha::DIVQU; break;
954 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000955 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000956 Tmp1 = SelectExpr(N.getOperand(0));
957 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth02981182005-01-26 01:24:38 +0000958 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000959 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000960// // case ISD::UINT_TO_FP:
961
962// case ISD::FP_TO_SINT:
963// assert (N.getValueType() == MVT::f64 && "Only can convert for doubles");
964// Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
965// Tmp2 = MakeReg(SrcTy);
966// BuildMI(BB, CVTTQ, 1, Tmp2).addReg(Tmp1);
967// BuildMI(BB, FTOIT, 1, Result).addReg(Tmp2);
968// return result;
969
970// // case ISD::FP_TO_UINT:
971
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000972 case ISD::SELECT:
973 {
974 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
975 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
976 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
977 // Get the condition into the zero flag.
978 unsigned dummy = MakeReg(MVT::i64);
979 BuildMI(BB, Alpha::BIS, 2, dummy).addReg(Tmp3).addReg(Tmp3);
980 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
981 return Result;
982 }
983
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000984 case ISD::Constant:
985 {
986 long val = cast<ConstantSDNode>(N)->getValue();
987 BuildMI(BB, Alpha::LOAD_IMM, 1, Result).addImm(val);
988 return Result;
989 }
990
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000991 case ISD::LOAD:
992 {
993 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000994 if (Result != notIn)
995 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000996 else
997 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
998
999 SDOperand Chain = N.getOperand(0);
1000 SDOperand Address = N.getOperand(1);
1001
1002 if (Address.getOpcode() == ISD::GlobalAddress)
1003 {
1004 Select(Chain);
1005 AlphaLowering.restoreGP(BB);
1006 BuildMI(BB, Alpha::LOAD, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1007 }
1008 else
1009 {
1010 Select(Chain);
1011 Tmp2 = SelectExpr(Address);
1012 BuildMI(BB, Alpha::LDQ, 2, Result).addImm(0).addReg(Tmp2);
1013 }
1014 return Result;
1015 }
1016 }
1017
1018 return 0;
1019}
1020
1021void ISel::Select(SDOperand N) {
1022 unsigned Tmp1, Tmp2, Opc;
1023
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001024 // FIXME: Disable for our current expansion model!
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001025 if (/*!N->hasOneUse() &&*/ !ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001026 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001027
1028 SDNode *Node = N.Val;
1029
1030 switch (N.getOpcode()) {
1031
1032 default:
1033 Node->dump(); std::cerr << "\n";
1034 assert(0 && "Node not handled yet!");
1035
1036 case ISD::BRCOND: {
1037 MachineBasicBlock *Dest =
1038 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
1039
1040 Select(N.getOperand(0));
1041 Tmp1 = SelectExpr(N.getOperand(1));
1042 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
1043 return;
1044 }
1045
1046 case ISD::BR: {
1047 MachineBasicBlock *Dest =
1048 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1049
1050 Select(N.getOperand(0));
1051 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
1052 return;
1053 }
1054
1055 case ISD::ImplicitDef:
1056 Select(N.getOperand(0));
1057 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
1058 return;
1059
1060 case ISD::EntryToken: return; // Noop
1061
1062 case ISD::TokenFactor:
1063 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1064 Select(Node->getOperand(i));
1065
1066 //N.Val->dump(); std::cerr << "\n";
1067 //assert(0 && "Node not handled yet!");
1068
1069 return;
1070
1071 case ISD::CopyToReg:
1072 Select(N.getOperand(0));
1073 Tmp1 = SelectExpr(N.getOperand(1));
1074 Tmp2 = cast<RegSDNode>(N)->getReg();
1075
1076 if (Tmp1 != Tmp2) {
1077 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1078 }
1079 return;
1080
1081 case ISD::RET:
1082 switch (N.getNumOperands()) {
1083 default:
1084 std::cerr << N.getNumOperands() << "\n";
1085 for (unsigned i = 0; i < N.getNumOperands(); ++i)
1086 std::cerr << N.getOperand(i).getValueType() << "\n";
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001087 Node->dump();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001088 assert(0 && "Unknown return instruction!");
1089 case 2:
1090 Select(N.getOperand(0));
1091 Tmp1 = SelectExpr(N.getOperand(1));
1092 switch (N.getOperand(1).getValueType()) {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001093 default: Node->dump(); assert(0 && "All other types should have been promoted!!");
1094 case MVT::f64:
1095 case MVT::f32:
1096 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1097 break;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001098 case MVT::i32:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001099 case MVT::i64:
1100 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1101 break;
1102 }
1103 break;
1104 case 1:
1105 Select(N.getOperand(0));
1106 break;
1107 }
1108 //Tmp2 = AlphaLowering.getRetAddr();
1109 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
1110 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
1111 return;
1112
1113 case ISD::STORE:
1114 Select(N.getOperand(0));
1115 Tmp1 = SelectExpr(N.getOperand(1)); //value
1116 if (N.getOperand(2).getOpcode() == ISD::GlobalAddress)
1117 {
1118 AlphaLowering.restoreGP(BB);
1119 BuildMI(BB, Alpha::STORE, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(N.getOperand(2))->getGlobal());
1120 }
1121 else
1122 {
1123 Tmp2 = SelectExpr(N.getOperand(2)); //address
1124 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addImm(0).addReg(Tmp2);
1125 }
1126 return;
1127
1128 case ISD::EXTLOAD:
1129 case ISD::SEXTLOAD:
1130 case ISD::ZEXTLOAD:
1131 case ISD::LOAD:
1132 case ISD::CopyFromReg:
1133 case ISD::CALL:
1134// case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001135 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001136 SelectExpr(N);
1137 return;
1138
1139
1140 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
1141 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001142 if (StoredTy == MVT::i64) {
1143 Node->dump();
1144 assert(StoredTy != MVT::i64 && "Unsupported TRUNCSTORE for this target!");
1145 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001146
1147 Select(N.getOperand(0));
1148 Tmp1 = SelectExpr(N.getOperand(1));
1149 Tmp2 = SelectExpr(N.getOperand(2));
1150
1151 switch (StoredTy) {
Chris Lattnerd7b59d02005-01-30 16:32:48 +00001152 default: Node->dump(); assert(0 && "Unhandled Type");
Andrew Lenharthd279b412005-01-25 19:58:40 +00001153 case MVT::i1: //FIXME: DAG does not promote this load
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001154 case MVT::i8: Opc = Alpha::STB; break;
1155 case MVT::i16: Opc = Alpha::STW; break;
1156 case MVT::i32: Opc = Alpha::STL; break;
1157 }
1158
1159 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(0).addReg(Tmp2);
1160 return;
1161 }
1162
1163 case ISD::ADJCALLSTACKDOWN:
1164 case ISD::ADJCALLSTACKUP:
1165 Select(N.getOperand(0));
1166 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1167
1168 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? Alpha::ADJUSTSTACKDOWN :
1169 Alpha::ADJUSTSTACKUP;
1170 BuildMI(BB, Opc, 1).addImm(Tmp1);
1171 return;
1172 }
1173 assert(0 && "Should not be reached!");
1174}
1175
1176
1177/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1178/// into a machine code representation using pattern matching and a machine
1179/// description file.
1180///
1181FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
1182 return new ISel(TM);
1183}