blob: 752be14c37655d38e6d3075c4a087d74cf86d7fa [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);
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000151 argt = newroot = DAG.getCopyFromReg(argVreg[count], getValueType(I->getType()), DAG.getRoot());
Andrew Lenharth684f2292005-01-30 00:35:27 +0000152 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 Lenharthe76797c2005-02-01 20:40:27 +0000182 for (int i = 0; i < std::min(count,6); ++i)
Andrew Lenharth40831c52005-01-28 06:57:18 +0000183 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 Lenharthc1faced2005-02-01 01:37:24 +0000310 case ISD::FP_ROUND:
311 assert (DestType == MVT::f32 && N.getOperand(0).getValueType() == MVT::f64 && "only f64 to f32 conversion supported here");
312 Tmp1 = SelectExpr(N.getOperand(0));
313 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
314 return Result;
315
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000316 case ISD::FP_EXTEND:
317 assert (DestType == MVT::f64 && N.getOperand(0).getValueType() == MVT::f32 && "only f32 to f64 conversion supported here");
318 Tmp1 = SelectExpr(N.getOperand(0));
319 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
320 return Result;
321
Andrew Lenharth2c594352005-01-29 15:42:07 +0000322 case ISD::CopyFromReg:
323 {
324 // Make sure we generate both values.
325 if (Result != notIn)
326 ExprMap[N.getValue(1)] = notIn; // Generate the token
327 else
328 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
329
330 SDOperand Chain = N.getOperand(0);
331
332 Select(Chain);
333 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
334 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
335 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
336 return Result;
337 }
338
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000339 case ISD::LOAD:
340 {
341 // Make sure we generate both values.
342 if (Result != notIn)
343 ExprMap[N.getValue(1)] = notIn; // Generate the token
344 else
345 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
346
347 SDOperand Chain = N.getOperand(0);
348 SDOperand Address = N.getOperand(1);
349
350 if (Address.getOpcode() == ISD::GlobalAddress)
351 {
352 Select(Chain);
353 AlphaLowering.restoreGP(BB);
Andrew Lenharth2afc8212005-02-02 03:36:35 +0000354 Opc = DestType == MVT::f64 ? Alpha::LDS_SYM : Alpha::LDT_SYM;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000355 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
356 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000357 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
358 AlphaLowering.restoreGP(BB);
359 if (DestType == MVT::f64) {
360 BuildMI(BB, Alpha::LDT_SYM, 1, Result).addConstantPoolIndex(CP->getIndex());
361 } else {
362 BuildMI(BB, Alpha::LDS_SYM, 1, Result).addConstantPoolIndex(CP->getIndex());
363 }
364 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000365 else
366 {
367 Select(Chain);
368 Tmp2 = SelectExpr(Address);
369 Opc = DestType == MVT::f64 ? Alpha::LDS : Alpha::LDT;
370 BuildMI(BB, Opc, 2, Result).addImm(0).addReg(Tmp2);
371 }
372 return Result;
373 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000374 case ISD::ConstantFP:
375 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
376 if (CN->isExactlyValue(+0.0)) {
377 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
378 } else {
379 abort();
380 }
381 }
382 return Result;
383
384 case ISD::MUL:
385 case ISD::ADD:
386 case ISD::SUB:
387 case ISD::SDIV:
388 switch( opcode ) {
389 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
390 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
391 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
392 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
393 };
394 Tmp1 = SelectExpr(N.getOperand(0));
395 Tmp2 = SelectExpr(N.getOperand(1));
396 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
397 return Result;
398
Andrew Lenharth2c594352005-01-29 15:42:07 +0000399 case ISD::EXTLOAD:
400 //include a conversion sequence for float loads to double
401 if (Result != notIn)
402 ExprMap[N.getValue(1)] = notIn; // Generate the token
403 else
404 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
405
406 Tmp2 = MakeReg(MVT::f32);
407
408 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
409 if (Node->getValueType(0) == MVT::f64) {
410 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
411 "Bad EXTLOAD!");
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000412 AlphaLowering.restoreGP(BB);
413 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp2).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth2c594352005-01-29 15:42:07 +0000414 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp2);
415 return Result;
416 }
417 Select(Node->getOperand(0)); // chain
418 Tmp1 = SelectExpr(Node->getOperand(1));
419 BuildMI(BB, Alpha::LDS, 1, Tmp2).addReg(Tmp1);
420 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp2);
421 return Result;
422
423
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000424 case ISD::UINT_TO_FP:
425 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000426 {
427 assert (N.getOperand(0).getValueType() == MVT::i64 && "only quads can be loaded from");
428 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +0000429 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000430
431 //The hard way:
432 // Spill the integer to memory and reload it from there.
433 unsigned Size = MVT::getSizeInBits(MVT::i64)/8;
434 MachineFunction *F = BB->getParent();
435 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
436
Andrew Lenharth7efadce2005-01-31 01:44:26 +0000437 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addFrameIndex(FrameIdx).addReg(Alpha::F31);
438 BuildMI(BB, Alpha::LDT, 2, Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
439 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
440 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000441
442 //The easy way: doesn't work
443// //so these instructions are not supported on ev56
444// Opc = DestType == MVT::f64 ? Alpha::ITOFT : Alpha::ITOFS;
445// BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
446// Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
447// BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
448
Andrew Lenharth40831c52005-01-28 06:57:18 +0000449 return Result;
450 }
451 }
452 assert(0 && "should not get here");
453 return 0;
454}
455
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000456unsigned ISel::SelectExpr(SDOperand N) {
457 unsigned Result;
458 unsigned Tmp1, Tmp2, Tmp3;
459 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000460 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000461
462 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000463 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000464
465 unsigned &Reg = ExprMap[N];
466 if (Reg) return Reg;
467
468 if (N.getOpcode() != ISD::CALL)
469 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000470 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000471 else {
472 // If this is a call instruction, make sure to prepare ALL of the result
473 // values as well as the chain.
474 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000475 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000476 else {
477 Result = MakeReg(Node->getValueType(0));
478 ExprMap[N.getValue(0)] = Result;
479 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
480 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000481 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000482 }
483 }
484
Andrew Lenharth40831c52005-01-28 06:57:18 +0000485 if (DestType == MVT::f64 || DestType == MVT::f32)
486 return SelectExprFP(N, Result);
487
488 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000489 default:
490 Node->dump();
491 assert(0 && "Node not handled!\n");
492
Andrew Lenharth2c594352005-01-29 15:42:07 +0000493 case ISD::ConstantPool:
494 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
495 AlphaLowering.restoreGP(BB);
496 BuildMI(BB, Alpha::LOAD, 1, Result).addConstantPoolIndex(Tmp1);
497 return Result;
498
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000499 case ISD::FrameIndex:
500 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Andrew Lenharth684f2292005-01-30 00:35:27 +0000501 BuildMI(BB, Alpha::LDA, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000502 return Result;
503
504 case ISD::EXTLOAD:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000505 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000506 if (Result != notIn)
507 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000508 else
509 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
510
511 Select(Node->getOperand(0)); // chain
512 Tmp1 = SelectExpr(Node->getOperand(1));
513
514 switch(Node->getValueType(0)) {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000515 default: Node->dump(); assert(0 && "Unknown type to sign extend to.");
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000516 case MVT::i64:
517 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
518 default:
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000519 Node->dump();
520 assert(0 && "Bad extend load!");
Andrew Lenharthd279b412005-01-25 19:58:40 +0000521 case MVT::i64:
522 BuildMI(BB, Alpha::LDQ, 2, Result).addImm(0).addReg(Tmp1);
523 break;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000524 case MVT::i32:
525 BuildMI(BB, Alpha::LDL, 2, Result).addImm(0).addReg(Tmp1);
526 break;
527 case MVT::i16:
528 BuildMI(BB, Alpha::LDWU, 2, Result).addImm(0).addReg(Tmp1);
529 break;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000530 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000531 case MVT::i8:
532 BuildMI(BB, Alpha::LDBU, 2, Result).addImm(0).addReg(Tmp1);
533 break;
534 }
535 break;
536 }
537 return Result;
538
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000539 case ISD::SEXTLOAD:
540 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000541 if (Result != notIn)
542 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000543 else
544 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
545
546 Select(Node->getOperand(0)); // chain
547 Tmp1 = SelectExpr(Node->getOperand(1));
548 switch(Node->getValueType(0)) {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000549 default: Node->dump(); assert(0 && "Unknown type to sign extend to.");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000550 case MVT::i64:
551 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
552 default:
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000553 Node->dump();
554 assert(0 && "Bad sign extend!");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000555 case MVT::i32:
556 BuildMI(BB, Alpha::LDL, 2, Result).addImm(0).addReg(Tmp1);
557 break;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000558 }
559 break;
560 }
561 return Result;
562
563 case ISD::ZEXTLOAD:
564 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000565 if (Result != notIn)
566 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000567 else
568 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
569
570 Select(Node->getOperand(0)); // chain
571 Tmp1 = SelectExpr(Node->getOperand(1));
572 switch(Node->getValueType(0)) {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000573 default: Node->dump(); assert(0 && "Unknown type to zero extend to.");
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000574 case MVT::i64:
575 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
576 default:
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000577 Node->dump();
578 assert(0 && "Bad sign extend!");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000579 case MVT::i16:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000580 BuildMI(BB, Alpha::LDWU, 2, Result).addImm(0).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000581 break;
582 case MVT::i8:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000583 BuildMI(BB, Alpha::LDBU, 2, Result).addImm(0).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000584 break;
585 }
586 break;
587 }
588 return Result;
589
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000590
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000591 case ISD::GlobalAddress:
592 AlphaLowering.restoreGP(BB);
593 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
594 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
595 return Result;
596
597 case ISD::CALL:
598 {
599 Select(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000600
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000601 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000602 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000603
604 //grab the arguments
605 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000606 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000607 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000608 argvregs.push_back(SelectExpr(N.getOperand(i)));
609
Andrew Lenharth684f2292005-01-30 00:35:27 +0000610 //in reg args
611 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
612 {
613 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
614 Alpha::R19, Alpha::R20, Alpha::R21};
615 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
616 Alpha::F19, Alpha::F20, Alpha::F21};
617 switch(N.getOperand(i+2).getValueType()) {
618 default:
619 Node->dump();
620 N.getOperand(i).Val->dump();
621 std::cerr << "Type for " << i << " is: " << N.getOperand(i+2).getValueType() << "\n";
622 assert(0 && "Unknown value type for call");
623 case MVT::i1:
624 case MVT::i8:
625 case MVT::i16:
626 case MVT::i32:
627 case MVT::i64:
628 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
629 break;
630 case MVT::f32:
631 case MVT::f64:
632 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
633 break;
634 }
635 }
636 //in mem args
637 for (int i = 6, e = argvregs.size(); i < e; ++i)
638 {
639 switch(N.getOperand(i+2).getValueType()) {
640 default:
641 Node->dump();
642 N.getOperand(i).Val->dump();
643 std::cerr << "Type for " << i << " is: " << N.getOperand(i+2).getValueType() << "\n";
644 assert(0 && "Unknown value type for call");
645 case MVT::i1:
646 case MVT::i8:
647 case MVT::i16:
648 case MVT::i32:
649 case MVT::i64:
650 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
651 break;
652 case MVT::f32:
653 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
654 break;
655 case MVT::f64:
656 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
657 break;
658 }
659 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000660 //build the right kind of call
661 if (GlobalAddressSDNode *GASD =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000662 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
663 {
664 AlphaLowering.restoreGP(BB);
665 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal(),true);
666 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000667 else if (ExternalSymbolSDNode *ESSDN =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000668 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
669 {
670 AlphaLowering.restoreGP(BB);
671 BuildMI(BB, Alpha::CALL, 0).addExternalSymbol(ESSDN->getSymbol(), true);
672 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000673 else
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000674 {
675 //no need to restore GP as we are doing an indirect call
676 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000677 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
678 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000679 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000680
681 //push the result into a virtual register
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000682
683 switch (Node->getValueType(0)) {
684 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000685 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000686 case MVT::i1:
687 case MVT::i8:
688 case MVT::i16:
689 case MVT::i32:
690 case MVT::i64:
691 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
692 break;
693 case MVT::f32:
694 case MVT::f64:
695 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
696 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000697 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000698 return Result+N.ResNo;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000699 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000700
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000701 case ISD::SIGN_EXTEND:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000702 abort();
703
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000704 case ISD::SIGN_EXTEND_INREG:
705 {
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000706 //Alpha has instructions for a bunch of signed 32 bit stuff
707 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
708 {
709 switch (N.getOperand(0).getOpcode()) {
710 case ISD::ADD:
711 case ISD::SUB:
712 case ISD::MUL:
713 {
714 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
715 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
716 //FIXME: first check for Scaled Adds and Subs!
717 if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
718 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
719 { //Normal imm add/sub
720 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
721 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
722 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
723 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
724 }
725 else
726 { //Normal add/sub
727 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULLi : Alpha::SUBL);
728 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
729 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
730 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
731 }
732 return Result;
733 }
734 default: break; //Fall Though;
735 }
736 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000737 Tmp1 = SelectExpr(N.getOperand(0));
738 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000739 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000740 switch(MVN->getExtraValueType())
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000741 {
742 default:
743 Node->dump();
744 assert(0 && "Sign Extend InReg not there yet");
745 break;
746 case MVT::i32:
747 {
748 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
749 break;
750 }
751 case MVT::i16:
752 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
753 break;
754 case MVT::i8:
755 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
756 break;
757 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000758 return Result;
759 }
760 case ISD::ZERO_EXTEND_INREG:
761 {
762 Tmp1 = SelectExpr(N.getOperand(0));
763 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000764 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000765 switch(MVN->getExtraValueType())
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000766 {
767 default:
768 Node->dump();
769 assert(0 && "Zero Extend InReg not there yet");
770 break;
771 case MVT::i32: Tmp2 = 0xf0; break;
772 case MVT::i16: Tmp2 = 0xfc; break;
773 case MVT::i8: Tmp2 = 0xfe; break;
774 case MVT::i1: //handle this one special
775 BuildMI(BB, Alpha::ANDi, 2, Result).addReg(Tmp1).addImm(1);
776 return Result;
777 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000778 BuildMI(BB, Alpha::ZAPi, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000779 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000780 }
781
782 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000783 {
784 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
785 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
786 bool isConst1 = false;
787 bool isConst2 = false;
788 int dir;
789
790 //Tmp1 = SelectExpr(N.getOperand(0));
791 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000792 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
793 isConst1 = true;
794 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000795 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
796 isConst2 = true;
797
798 switch (SetCC->getCondition()) {
799 default: Node->dump(); assert(0 && "Unknown integer comparison!");
800 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
801 case ISD::SETLT: Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
802 case ISD::SETLE: Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
803 case ISD::SETGT: Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
804 case ISD::SETGE: Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
805 case ISD::SETULT: Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
806 case ISD::SETUGT: Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
807 case ISD::SETULE: Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
808 case ISD::SETUGE: Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000809 case ISD::SETNE: {//Handle this one special
810 //std::cerr << "Alpha does not have a setne.\n";
811 //abort();
812 Tmp1 = SelectExpr(N.getOperand(0));
813 Tmp2 = SelectExpr(N.getOperand(1));
814 Tmp3 = MakeReg(MVT::i64);
815 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
816 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000817 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
818 //BuildMI(BB,Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000819 return Result;
820 }
821 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000822 if (dir == 1) {
823 Tmp1 = SelectExpr(N.getOperand(0));
824 if (isConst2) {
825 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
826 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
827 } else {
828 Tmp2 = SelectExpr(N.getOperand(1));
829 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
830 }
831 } else if (dir == 2) {
832 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +0000833 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000834 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
835 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
836 } else {
837 Tmp2 = SelectExpr(N.getOperand(0));
838 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
839 }
840 } else { //dir == 0
841 if (isConst1) {
842 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
843 Tmp2 = SelectExpr(N.getOperand(1));
844 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
845 } else if (isConst2) {
846 Tmp1 = SelectExpr(N.getOperand(0));
847 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
848 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
849 } else {
850 Tmp1 = SelectExpr(N.getOperand(0));
851 Tmp2 = SelectExpr(N.getOperand(1));
852 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
853 }
854 }
855 }
856 else
857 {
858 Node->dump();
859 assert(0 && "only integer");
860 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000861 }
862 else
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000863 {
864 Node->dump();
865 assert(0 && "Not a setcc in setcc");
866 }
867 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000868 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000869
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000870 case ISD::CopyFromReg:
871 {
Andrew Lenharth40831c52005-01-28 06:57:18 +0000872 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000873 if (Result != notIn)
874 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +0000875 else
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000876 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth40831c52005-01-28 06:57:18 +0000877
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000878 SDOperand Chain = N.getOperand(0);
879
880 Select(Chain);
881 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
882 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
883 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
884 return Result;
885 }
886
Andrew Lenharth2d6f0222005-01-24 19:44:07 +0000887 //Most of the plain arithmetic and logic share the same form, and the same
888 //constant immediate test
889 case ISD::AND:
890 case ISD::OR:
891 case ISD::XOR:
892 case ISD::SHL:
893 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +0000894 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +0000895 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000896 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
897 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +0000898 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
899 {
900 switch(opcode) {
901 case ISD::AND: Opc = Alpha::ANDi; break;
902 case ISD::OR: Opc = Alpha::BISi; break;
903 case ISD::XOR: Opc = Alpha::XORi; break;
904 case ISD::SHL: Opc = Alpha::SLi; break;
905 case ISD::SRL: Opc = Alpha::SRLi; break;
906 case ISD::SRA: Opc = Alpha::SRAi; break;
907 case ISD::MUL: Opc = Alpha::MULQi; break;
908 };
909 Tmp1 = SelectExpr(N.getOperand(0));
910 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
911 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
912 }
913 else
914 {
915 switch(opcode) {
916 case ISD::AND: Opc = Alpha::AND; break;
917 case ISD::OR: Opc = Alpha::BIS; break;
918 case ISD::XOR: Opc = Alpha::XOR; break;
919 case ISD::SHL: Opc = Alpha::SL; break;
920 case ISD::SRL: Opc = Alpha::SRL; break;
921 case ISD::SRA: Opc = Alpha::SRA; break;
922 case ISD::MUL: Opc = Alpha::MULQ; break;
923 };
924 Tmp1 = SelectExpr(N.getOperand(0));
925 Tmp2 = SelectExpr(N.getOperand(1));
926 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
927 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +0000928 return Result;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000929
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000930 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000931 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000932 {
Andrew Lenharth40831c52005-01-28 06:57:18 +0000933 bool isAdd = opcode == ISD::ADD;
934
935 //FIXME: first check for Scaled Adds and Subs!
936 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +0000937 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
938 { //Normal imm add/sub
939 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
940 Tmp1 = SelectExpr(N.getOperand(0));
941 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
942 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
943 }
944 else if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +0000945 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
946 { //LDA //FIXME: expand the above condition a bit
947 Tmp1 = SelectExpr(N.getOperand(0));
948 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
949 if (!isAdd)
950 Tmp2 = -Tmp2;
951 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
952 }
953 else
954 { //Normal add/sub
955 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
956 Tmp1 = SelectExpr(N.getOperand(0));
957 Tmp2 = SelectExpr(N.getOperand(1));
958 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
959 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000960 return Result;
961 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000962
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000963 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +0000964 case ISD::SREM:
965 case ISD::SDIV:
966 case ISD::UDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000967 //FIXME: alpha really doesn't support any of these operations,
968 // the ops are expanded into special library calls with
969 // special calling conventions
970 switch(opcode) {
971 case ISD::UREM: Opc = Alpha::REMQU; break;
972 case ISD::SREM: Opc = Alpha::REMQ; break;
973 case ISD::UDIV: Opc = Alpha::DIVQU; break;
974 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000975 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000976 Tmp1 = SelectExpr(N.getOperand(0));
977 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth02981182005-01-26 01:24:38 +0000978 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000979 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000980
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000981 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +0000982 case ISD::FP_TO_SINT:
983 {
984 assert (DestType == MVT::i64 && "only quads can be loaded to");
985 MVT::ValueType SrcType = N.getOperand(0).getValueType();
986 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
987
988 //The hard way:
989 // Spill the integer to memory and reload it from there.
990 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
991 MachineFunction *F = BB->getParent();
992 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
993
994 //CVTTQ STT LDQ
995 //CVTST CVTTQ STT LDQ
996 if (SrcType == MVT::f32)
997 {
998 Tmp2 = MakeReg(MVT::f64);
999 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1000 Tmp1 = Tmp2;
1001 }
1002 Tmp2 = MakeReg(MVT::f64);
1003 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
1004 BuildMI(BB, Alpha::STT, 3).addReg(Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1005 BuildMI(BB, Alpha::LDQ, 2, Result).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1006
1007 return Result;
1008 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001009
1010// // case ISD::FP_TO_UINT:
1011
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001012 case ISD::SELECT:
1013 {
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001014 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001015 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1016 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001017 // Get the condition into the zero flag.
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001018 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001019 return Result;
1020 }
1021
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001022 case ISD::Constant:
1023 {
Andrew Lenharth22d5a412005-02-02 00:51:15 +00001024 unsigned long val = cast<ConstantSDNode>(N)->getValue();
1025 if (val < 32000 && (long)val > -32000)
1026 BuildMI(BB, Alpha::LOAD_IMM, 1, Result).addImm(val);
1027 else
1028 {
1029 MachineConstantPool *CP = BB->getParent()->getConstantPool();
1030 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
1031 unsigned CPI = CP->getConstantPoolIndex(C);
1032 AlphaLowering.restoreGP(BB);
1033 BuildMI(BB, Alpha::LOAD, 1, Result).addConstantPoolIndex(CPI);
1034 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001035 return Result;
1036 }
1037
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001038 case ISD::LOAD:
1039 {
1040 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001041 if (Result != notIn)
1042 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001043 else
1044 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1045
1046 SDOperand Chain = N.getOperand(0);
1047 SDOperand Address = N.getOperand(1);
1048
Andrew Lenharthc23d6962005-02-02 04:35:44 +00001049 assert(N.getValue(0).getValueType() == MVT::i64 && "unknown Load dest type");
Andrew Lenharth2afc8212005-02-02 03:36:35 +00001050
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001051 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth2afc8212005-02-02 03:36:35 +00001052 {
1053 Select(Chain);
1054 AlphaLowering.restoreGP(BB);
1055 BuildMI(BB, Alpha::LOAD, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1056 }
1057 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1058 AlphaLowering.restoreGP(BB);
1059 BuildMI(BB, Alpha::LOAD, 1, Result).addConstantPoolIndex(CP->getIndex());
1060 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001061 else
Andrew Lenharth2afc8212005-02-02 03:36:35 +00001062 {
1063 Select(Chain);
1064 Tmp2 = SelectExpr(Address);
1065 BuildMI(BB, Alpha::LDQ, 2, Result).addImm(0).addReg(Tmp2);
1066 }
1067 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001068 }
1069 }
1070
1071 return 0;
1072}
1073
1074void ISel::Select(SDOperand N) {
1075 unsigned Tmp1, Tmp2, Opc;
1076
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001077 // FIXME: Disable for our current expansion model!
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001078 if (/*!N->hasOneUse() &&*/ !ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001079 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001080
1081 SDNode *Node = N.Val;
1082
1083 switch (N.getOpcode()) {
1084
1085 default:
1086 Node->dump(); std::cerr << "\n";
1087 assert(0 && "Node not handled yet!");
1088
1089 case ISD::BRCOND: {
1090 MachineBasicBlock *Dest =
1091 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
1092
1093 Select(N.getOperand(0));
1094 Tmp1 = SelectExpr(N.getOperand(1));
1095 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
1096 return;
1097 }
1098
1099 case ISD::BR: {
1100 MachineBasicBlock *Dest =
1101 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1102
1103 Select(N.getOperand(0));
1104 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
1105 return;
1106 }
1107
1108 case ISD::ImplicitDef:
1109 Select(N.getOperand(0));
1110 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
1111 return;
1112
1113 case ISD::EntryToken: return; // Noop
1114
1115 case ISD::TokenFactor:
1116 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1117 Select(Node->getOperand(i));
1118
1119 //N.Val->dump(); std::cerr << "\n";
1120 //assert(0 && "Node not handled yet!");
1121
1122 return;
1123
1124 case ISD::CopyToReg:
1125 Select(N.getOperand(0));
1126 Tmp1 = SelectExpr(N.getOperand(1));
1127 Tmp2 = cast<RegSDNode>(N)->getReg();
1128
1129 if (Tmp1 != Tmp2) {
1130 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1131 }
1132 return;
1133
1134 case ISD::RET:
1135 switch (N.getNumOperands()) {
1136 default:
1137 std::cerr << N.getNumOperands() << "\n";
1138 for (unsigned i = 0; i < N.getNumOperands(); ++i)
1139 std::cerr << N.getOperand(i).getValueType() << "\n";
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001140 Node->dump();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001141 assert(0 && "Unknown return instruction!");
1142 case 2:
1143 Select(N.getOperand(0));
1144 Tmp1 = SelectExpr(N.getOperand(1));
1145 switch (N.getOperand(1).getValueType()) {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001146 default: Node->dump(); assert(0 && "All other types should have been promoted!!");
1147 case MVT::f64:
1148 case MVT::f32:
1149 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1150 break;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001151 case MVT::i32:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001152 case MVT::i64:
1153 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1154 break;
1155 }
1156 break;
1157 case 1:
1158 Select(N.getOperand(0));
1159 break;
1160 }
1161 //Tmp2 = AlphaLowering.getRetAddr();
1162 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
1163 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
1164 return;
1165
1166 case ISD::STORE:
1167 Select(N.getOperand(0));
1168 Tmp1 = SelectExpr(N.getOperand(1)); //value
1169 if (N.getOperand(2).getOpcode() == ISD::GlobalAddress)
1170 {
1171 AlphaLowering.restoreGP(BB);
1172 BuildMI(BB, Alpha::STORE, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(N.getOperand(2))->getGlobal());
1173 }
1174 else
1175 {
1176 Tmp2 = SelectExpr(N.getOperand(2)); //address
1177 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addImm(0).addReg(Tmp2);
1178 }
1179 return;
1180
1181 case ISD::EXTLOAD:
1182 case ISD::SEXTLOAD:
1183 case ISD::ZEXTLOAD:
1184 case ISD::LOAD:
1185 case ISD::CopyFromReg:
1186 case ISD::CALL:
1187// case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001188 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001189 SelectExpr(N);
1190 return;
1191
1192
1193 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
1194 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001195 if (StoredTy == MVT::i64) {
1196 Node->dump();
1197 assert(StoredTy != MVT::i64 && "Unsupported TRUNCSTORE for this target!");
1198 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001199
1200 Select(N.getOperand(0));
1201 Tmp1 = SelectExpr(N.getOperand(1));
1202 Tmp2 = SelectExpr(N.getOperand(2));
1203
1204 switch (StoredTy) {
Chris Lattnerd7b59d02005-01-30 16:32:48 +00001205 default: Node->dump(); assert(0 && "Unhandled Type");
Andrew Lenharthd279b412005-01-25 19:58:40 +00001206 case MVT::i1: //FIXME: DAG does not promote this load
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001207 case MVT::i8: Opc = Alpha::STB; break;
1208 case MVT::i16: Opc = Alpha::STW; break;
1209 case MVT::i32: Opc = Alpha::STL; break;
1210 }
1211
1212 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(0).addReg(Tmp2);
1213 return;
1214 }
1215
1216 case ISD::ADJCALLSTACKDOWN:
1217 case ISD::ADJCALLSTACKUP:
1218 Select(N.getOperand(0));
1219 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1220
1221 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? Alpha::ADJUSTSTACKDOWN :
1222 Alpha::ADJUSTSTACKUP;
1223 BuildMI(BB, Opc, 1).addImm(Tmp1);
1224 return;
1225 }
1226 assert(0 && "Should not be reached!");
1227}
1228
1229
1230/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1231/// into a machine code representation using pattern matching and a machine
1232/// description file.
1233///
1234FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
1235 return new ISel(TM);
1236}