blob: b641d19ac3ae81d03619ab09069389b6b80851bc [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 Lenharth8d163d22005-02-02 05:49:42 +000064 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
65 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
66 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
67
Andrew Lenharth3d65d312005-01-27 03:49:45 +000068 computeRegisterProperties();
Andrew Lenharth304d0f32005-01-22 23:41:55 +000069
Andrew Lenharthd2bb9602005-01-27 07:50:35 +000070 addLegalFPImmediate(+0.0); //F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +000071 }
72
73 /// LowerArguments - This hook must be implemented to indicate how we should
74 /// lower the arguments for the specified function, into the specified DAG.
75 virtual std::vector<SDOperand>
76 LowerArguments(Function &F, SelectionDAG &DAG);
77
78 /// LowerCallTo - This hook lowers an abstract call to a function into an
79 /// actual call.
80 virtual std::pair<SDOperand, SDOperand>
81 LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
82 ArgListTy &Args, SelectionDAG &DAG);
83
84 virtual std::pair<SDOperand, SDOperand>
85 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
86
87 virtual std::pair<SDOperand,SDOperand>
88 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
89 const Type *ArgTy, SelectionDAG &DAG);
90
91 virtual std::pair<SDOperand, SDOperand>
92 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
93 SelectionDAG &DAG);
94
95 void restoreGP(MachineBasicBlock* BB)
96 {
97 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
98 }
99 };
100}
101
102//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
103
104//For now, just use variable size stack frame format
105
106//In a standard call, the first six items are passed in registers $16
107//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
108//of argument-to-register correspondence.) The remaining items are
109//collected in a memory argument list that is a naturally aligned
110//array of quadwords. In a standard call, this list, if present, must
111//be passed at 0(SP).
112//7 ... n 0(SP) ... (n-7)*8(SP)
113
114std::vector<SDOperand>
115AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
116{
117 std::vector<SDOperand> ArgValues;
118
119 // //#define FP $15
120 // //#define RA $26
121 // //#define PV $27
122 // //#define GP $29
123 // //#define SP $30
124
125 // assert(0 && "TODO");
126 MachineFunction &MF = DAG.getMachineFunction();
127 MachineFrameInfo *MFI = MF.getFrameInfo();
128
129 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
130 MachineBasicBlock& BB = MF.front();
131
132 //Handle the return address
133 //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
134
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000135 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
136 Alpha::R19, Alpha::R20, Alpha::R21};
137 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
138 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000139 std::vector<unsigned> argVreg;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000140 std::vector<unsigned> argPreg;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000141 std::vector<unsigned> argOpc;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000142 int count = 0;
143 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I)
144 {
Andrew Lenharth40831c52005-01-28 06:57:18 +0000145 SDOperand newroot, argt;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000146 if (count < 6) {
147 switch (getValueType(I->getType())) {
148 default: std::cerr << "Unknown Type " << getValueType(I->getType()) << "\n"; abort();
149 case MVT::f64:
150 case MVT::f32:
151 BuildMI(&BB, Alpha::IDEF, 0, args_float[count]);
152 argVreg.push_back(MF.getSSARegMap()->createVirtualRegister(getRegClassFor(getValueType(I->getType()))));
153 argPreg.push_back(args_float[count]);
154 argOpc.push_back(Alpha::CPYS);
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000155 argt = newroot = DAG.getCopyFromReg(argVreg[count], getValueType(I->getType()), DAG.getRoot());
Andrew Lenharth684f2292005-01-30 00:35:27 +0000156 break;
157 case MVT::i1:
158 case MVT::i8:
159 case MVT::i16:
160 case MVT::i32:
161 case MVT::i64:
162 BuildMI(&BB, Alpha::IDEF, 0, args_int[count]);
163 argVreg.push_back(MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64)));
164 argPreg.push_back(args_int[count]);
165 argOpc.push_back(Alpha::BIS);
166 argt = newroot = DAG.getCopyFromReg(argVreg[count], MVT::i64, DAG.getRoot());
167 if (getValueType(I->getType()) != MVT::i64)
168 argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()), newroot);
169 break;
170 }
171 } else { //more args
172 // Create the frame index object for this incoming parameter...
173 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
174
175 // Create the SelectionDAG nodes corresponding to a load from this parameter
176 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
177 argt = newroot = DAG.getLoad(getValueType(I->getType()), DAG.getEntryNode(), FIN);
Andrew Lenharth40831c52005-01-28 06:57:18 +0000178 }
179 DAG.setRoot(newroot.getValue(1));
180 ArgValues.push_back(argt);
Andrew Lenharth684f2292005-01-30 00:35:27 +0000181 ++count;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000182 }
183
184 BuildMI(&BB, Alpha::IDEF, 0, Alpha::R29);
185 BuildMI(&BB, Alpha::BIS, 2, GP).addReg(Alpha::R29).addReg(Alpha::R29);
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000186 for (int i = 0; i < std::min(count,6); ++i)
Andrew Lenharth40831c52005-01-28 06:57:18 +0000187 BuildMI(&BB, argOpc[i], 2, argVreg[i]).addReg(argPreg[i]).addReg(argPreg[i]);
188
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000189 return ArgValues;
190}
191
192std::pair<SDOperand, SDOperand>
193AlphaTargetLowering::LowerCallTo(SDOperand Chain,
194 const Type *RetTy, SDOperand Callee,
195 ArgListTy &Args, SelectionDAG &DAG) {
196 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000197 if (Args.size() > 6)
198 NumBytes = (Args.size() - 6) * 8;
199
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000200 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
201 DAG.getConstant(NumBytes, getPointerTy()));
202 std::vector<SDOperand> args_to_use;
203 for (unsigned i = 0, e = Args.size(); i != e; ++i)
204 {
205 switch (getValueType(Args[i].second)) {
206 default: assert(0 && "Unexpected ValueType for argument!");
207 case MVT::i1:
208 case MVT::i8:
209 case MVT::i16:
210 case MVT::i32:
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000211 // Promote the integer to 64 bits. If the input type is signed use a
212 // sign extend, otherwise use a zero extend.
213 if (Args[i].second->isSigned())
Andrew Lenharth40831c52005-01-28 06:57:18 +0000214 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000215 else
Andrew Lenharth40831c52005-01-28 06:57:18 +0000216 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000217 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000218 case MVT::i64:
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000219 case MVT::f64:
220 case MVT::f32:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000221 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000222 }
223 args_to_use.push_back(Args[i].first);
224 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000225
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000226 std::vector<MVT::ValueType> RetVals;
227 MVT::ValueType RetTyVT = getValueType(RetTy);
228 if (RetTyVT != MVT::isVoid)
229 RetVals.push_back(RetTyVT);
230 RetVals.push_back(MVT::Other);
231
232 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee, args_to_use), 0);
233 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
234 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
235 DAG.getConstant(NumBytes, getPointerTy()));
236 return std::make_pair(TheCall, Chain);
237}
238
239std::pair<SDOperand, SDOperand>
240AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
241 //vastart just returns the address of the VarArgsFrameIndex slot.
242 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
243}
244
245std::pair<SDOperand,SDOperand> AlphaTargetLowering::
246LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
247 const Type *ArgTy, SelectionDAG &DAG) {
248 abort();
249}
250
251
252std::pair<SDOperand, SDOperand> AlphaTargetLowering::
253LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
254 SelectionDAG &DAG) {
255 abort();
256}
257
258
259
260
261
262namespace {
263
264 //===--------------------------------------------------------------------===//
265 /// ISel - Alpha specific code to select Alpha machine instructions for
266 /// SelectionDAG operations.
267 ///
268 class ISel : public SelectionDAGISel {
269
270 /// AlphaLowering - This object fully describes how to lower LLVM code to an
271 /// Alpha-specific SelectionDAG.
272 AlphaTargetLowering AlphaLowering;
273
274
275 /// ExprMap - As shared expressions are codegen'd, we keep track of which
276 /// vreg the value is produced in, so we only emit one copy of each compiled
277 /// tree.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000278 static const unsigned notIn = (unsigned)(-1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000279 std::map<SDOperand, unsigned> ExprMap;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000280
281 public:
282 ISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM) {
283 }
284
285 /// InstructionSelectBasicBlock - This callback is invoked by
286 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
287 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
288 // Codegen the basic block.
289 Select(DAG.getRoot());
290
291 // Clear state used for selection.
292 ExprMap.clear();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000293 }
294
295 unsigned SelectExpr(SDOperand N);
Andrew Lenharth40831c52005-01-28 06:57:18 +0000296 unsigned SelectExprFP(SDOperand N, unsigned Result);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000297 void Select(SDOperand N);
298 };
299}
300
Andrew Lenharth40831c52005-01-28 06:57:18 +0000301unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
302{
303 unsigned Tmp1, Tmp2, Tmp3;
304 unsigned Opc = 0;
305 SDNode *Node = N.Val;
306 MVT::ValueType DestType = N.getValueType();
307 unsigned opcode = N.getOpcode();
308
309 switch (opcode) {
310 default:
311 Node->dump();
312 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +0000313
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000314 case ISD::FP_ROUND:
315 assert (DestType == MVT::f32 && N.getOperand(0).getValueType() == MVT::f64 && "only f64 to f32 conversion supported here");
316 Tmp1 = SelectExpr(N.getOperand(0));
317 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
318 return Result;
319
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000320 case ISD::FP_EXTEND:
321 assert (DestType == MVT::f64 && N.getOperand(0).getValueType() == MVT::f32 && "only f32 to f64 conversion supported here");
322 Tmp1 = SelectExpr(N.getOperand(0));
323 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
324 return Result;
325
Andrew Lenharth2c594352005-01-29 15:42:07 +0000326 case ISD::CopyFromReg:
327 {
328 // Make sure we generate both values.
329 if (Result != notIn)
330 ExprMap[N.getValue(1)] = notIn; // Generate the token
331 else
332 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
333
334 SDOperand Chain = N.getOperand(0);
335
336 Select(Chain);
337 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
338 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
339 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
340 return Result;
341 }
342
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000343 case ISD::LOAD:
344 {
345 // Make sure we generate both values.
346 if (Result != notIn)
347 ExprMap[N.getValue(1)] = notIn; // Generate the token
348 else
349 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
350
351 SDOperand Chain = N.getOperand(0);
352 SDOperand Address = N.getOperand(1);
353
354 if (Address.getOpcode() == ISD::GlobalAddress)
355 {
356 Select(Chain);
357 AlphaLowering.restoreGP(BB);
Andrew Lenharth2afc8212005-02-02 03:36:35 +0000358 Opc = DestType == MVT::f64 ? Alpha::LDS_SYM : Alpha::LDT_SYM;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000359 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
360 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000361 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
362 AlphaLowering.restoreGP(BB);
363 if (DestType == MVT::f64) {
364 BuildMI(BB, Alpha::LDT_SYM, 1, Result).addConstantPoolIndex(CP->getIndex());
365 } else {
366 BuildMI(BB, Alpha::LDS_SYM, 1, Result).addConstantPoolIndex(CP->getIndex());
367 }
368 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000369 else
370 {
371 Select(Chain);
372 Tmp2 = SelectExpr(Address);
373 Opc = DestType == MVT::f64 ? Alpha::LDS : Alpha::LDT;
374 BuildMI(BB, Opc, 2, Result).addImm(0).addReg(Tmp2);
375 }
376 return Result;
377 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000378 case ISD::ConstantFP:
379 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
380 if (CN->isExactlyValue(+0.0)) {
381 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
382 } else {
383 abort();
384 }
385 }
386 return Result;
387
388 case ISD::MUL:
389 case ISD::ADD:
390 case ISD::SUB:
391 case ISD::SDIV:
392 switch( opcode ) {
393 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
394 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
395 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
396 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
397 };
398 Tmp1 = SelectExpr(N.getOperand(0));
399 Tmp2 = SelectExpr(N.getOperand(1));
400 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
401 return Result;
402
Andrew Lenharth2c594352005-01-29 15:42:07 +0000403 case ISD::EXTLOAD:
404 //include a conversion sequence for float loads to double
405 if (Result != notIn)
406 ExprMap[N.getValue(1)] = notIn; // Generate the token
407 else
408 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
409
410 Tmp2 = MakeReg(MVT::f32);
411
412 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
413 if (Node->getValueType(0) == MVT::f64) {
414 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
415 "Bad EXTLOAD!");
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000416 AlphaLowering.restoreGP(BB);
417 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp2).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth2c594352005-01-29 15:42:07 +0000418 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp2);
419 return Result;
420 }
421 Select(Node->getOperand(0)); // chain
422 Tmp1 = SelectExpr(Node->getOperand(1));
423 BuildMI(BB, Alpha::LDS, 1, Tmp2).addReg(Tmp1);
424 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp2);
425 return Result;
426
427
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000428 case ISD::UINT_TO_FP:
429 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000430 {
431 assert (N.getOperand(0).getValueType() == MVT::i64 && "only quads can be loaded from");
432 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +0000433 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000434
435 //The hard way:
436 // Spill the integer to memory and reload it from there.
437 unsigned Size = MVT::getSizeInBits(MVT::i64)/8;
438 MachineFunction *F = BB->getParent();
439 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
440
Andrew Lenharth7efadce2005-01-31 01:44:26 +0000441 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addFrameIndex(FrameIdx).addReg(Alpha::F31);
442 BuildMI(BB, Alpha::LDT, 2, Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
443 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
444 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000445
446 //The easy way: doesn't work
447// //so these instructions are not supported on ev56
448// Opc = DestType == MVT::f64 ? Alpha::ITOFT : Alpha::ITOFS;
449// BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
450// Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
451// BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
452
Andrew Lenharth40831c52005-01-28 06:57:18 +0000453 return Result;
454 }
455 }
456 assert(0 && "should not get here");
457 return 0;
458}
459
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000460unsigned ISel::SelectExpr(SDOperand N) {
461 unsigned Result;
462 unsigned Tmp1, Tmp2, Tmp3;
463 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000464 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000465
466 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000467 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000468
469 unsigned &Reg = ExprMap[N];
470 if (Reg) return Reg;
471
472 if (N.getOpcode() != ISD::CALL)
473 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000474 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000475 else {
476 // If this is a call instruction, make sure to prepare ALL of the result
477 // values as well as the chain.
478 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000479 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000480 else {
481 Result = MakeReg(Node->getValueType(0));
482 ExprMap[N.getValue(0)] = Result;
483 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
484 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000485 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000486 }
487 }
488
Andrew Lenharth40831c52005-01-28 06:57:18 +0000489 if (DestType == MVT::f64 || DestType == MVT::f32)
490 return SelectExprFP(N, Result);
491
492 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000493 default:
494 Node->dump();
495 assert(0 && "Node not handled!\n");
496
Andrew Lenharth2c594352005-01-29 15:42:07 +0000497 case ISD::ConstantPool:
498 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
499 AlphaLowering.restoreGP(BB);
500 BuildMI(BB, Alpha::LOAD, 1, Result).addConstantPoolIndex(Tmp1);
501 return Result;
502
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000503 case ISD::FrameIndex:
504 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Andrew Lenharth684f2292005-01-30 00:35:27 +0000505 BuildMI(BB, Alpha::LDA, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000506 return Result;
507
508 case ISD::EXTLOAD:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000509 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000510 if (Result != notIn)
511 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000512 else
513 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
514
515 Select(Node->getOperand(0)); // chain
516 Tmp1 = SelectExpr(Node->getOperand(1));
517
518 switch(Node->getValueType(0)) {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000519 default: Node->dump(); assert(0 && "Unknown type to sign extend to.");
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000520 case MVT::i64:
521 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
522 default:
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000523 Node->dump();
524 assert(0 && "Bad extend load!");
Andrew Lenharthd279b412005-01-25 19:58:40 +0000525 case MVT::i64:
526 BuildMI(BB, Alpha::LDQ, 2, Result).addImm(0).addReg(Tmp1);
527 break;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000528 case MVT::i32:
529 BuildMI(BB, Alpha::LDL, 2, Result).addImm(0).addReg(Tmp1);
530 break;
531 case MVT::i16:
532 BuildMI(BB, Alpha::LDWU, 2, Result).addImm(0).addReg(Tmp1);
533 break;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000534 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000535 case MVT::i8:
536 BuildMI(BB, Alpha::LDBU, 2, Result).addImm(0).addReg(Tmp1);
537 break;
538 }
539 break;
540 }
541 return Result;
542
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000543 case ISD::SEXTLOAD:
544 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000545 if (Result != notIn)
546 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000547 else
548 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
549
550 Select(Node->getOperand(0)); // chain
551 Tmp1 = SelectExpr(Node->getOperand(1));
552 switch(Node->getValueType(0)) {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000553 default: Node->dump(); assert(0 && "Unknown type to sign extend to.");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000554 case MVT::i64:
555 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
556 default:
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000557 Node->dump();
558 assert(0 && "Bad sign extend!");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000559 case MVT::i32:
560 BuildMI(BB, Alpha::LDL, 2, Result).addImm(0).addReg(Tmp1);
561 break;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000562 }
563 break;
564 }
565 return Result;
566
567 case ISD::ZEXTLOAD:
568 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000569 if (Result != notIn)
570 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000571 else
572 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
573
574 Select(Node->getOperand(0)); // chain
575 Tmp1 = SelectExpr(Node->getOperand(1));
576 switch(Node->getValueType(0)) {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000577 default: Node->dump(); assert(0 && "Unknown type to zero extend to.");
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000578 case MVT::i64:
579 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
580 default:
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000581 Node->dump();
582 assert(0 && "Bad sign extend!");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000583 case MVT::i16:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000584 BuildMI(BB, Alpha::LDWU, 2, Result).addImm(0).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000585 break;
586 case MVT::i8:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000587 BuildMI(BB, Alpha::LDBU, 2, Result).addImm(0).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000588 break;
589 }
590 break;
591 }
592 return Result;
593
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000594
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000595 case ISD::GlobalAddress:
596 AlphaLowering.restoreGP(BB);
597 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
598 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
599 return Result;
600
601 case ISD::CALL:
602 {
603 Select(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000604
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000605 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000606 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000607
608 //grab the arguments
609 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000610 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000611 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000612 argvregs.push_back(SelectExpr(N.getOperand(i)));
613
Andrew Lenharth684f2292005-01-30 00:35:27 +0000614 //in reg args
615 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
616 {
617 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
618 Alpha::R19, Alpha::R20, Alpha::R21};
619 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
620 Alpha::F19, Alpha::F20, Alpha::F21};
621 switch(N.getOperand(i+2).getValueType()) {
622 default:
623 Node->dump();
624 N.getOperand(i).Val->dump();
625 std::cerr << "Type for " << i << " is: " << N.getOperand(i+2).getValueType() << "\n";
626 assert(0 && "Unknown value type for call");
627 case MVT::i1:
628 case MVT::i8:
629 case MVT::i16:
630 case MVT::i32:
631 case MVT::i64:
632 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
633 break;
634 case MVT::f32:
635 case MVT::f64:
636 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
637 break;
638 }
639 }
640 //in mem args
641 for (int i = 6, e = argvregs.size(); i < e; ++i)
642 {
643 switch(N.getOperand(i+2).getValueType()) {
644 default:
645 Node->dump();
646 N.getOperand(i).Val->dump();
647 std::cerr << "Type for " << i << " is: " << N.getOperand(i+2).getValueType() << "\n";
648 assert(0 && "Unknown value type for call");
649 case MVT::i1:
650 case MVT::i8:
651 case MVT::i16:
652 case MVT::i32:
653 case MVT::i64:
654 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
655 break;
656 case MVT::f32:
657 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
658 break;
659 case MVT::f64:
660 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
661 break;
662 }
663 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000664 //build the right kind of call
665 if (GlobalAddressSDNode *GASD =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000666 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
667 {
668 AlphaLowering.restoreGP(BB);
669 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal(),true);
670 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000671 else if (ExternalSymbolSDNode *ESSDN =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000672 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
673 {
674 AlphaLowering.restoreGP(BB);
675 BuildMI(BB, Alpha::CALL, 0).addExternalSymbol(ESSDN->getSymbol(), true);
676 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000677 else
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000678 {
679 //no need to restore GP as we are doing an indirect call
680 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000681 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
682 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000683 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000684
685 //push the result into a virtual register
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000686
687 switch (Node->getValueType(0)) {
688 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000689 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000690 case MVT::i1:
691 case MVT::i8:
692 case MVT::i16:
693 case MVT::i32:
694 case MVT::i64:
695 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
696 break;
697 case MVT::f32:
698 case MVT::f64:
699 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
700 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000701 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000702 return Result+N.ResNo;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000703 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000704
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000705 case ISD::SIGN_EXTEND:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000706 abort();
707
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000708 case ISD::SIGN_EXTEND_INREG:
709 {
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000710 //Alpha has instructions for a bunch of signed 32 bit stuff
711 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
712 {
713 switch (N.getOperand(0).getOpcode()) {
714 case ISD::ADD:
715 case ISD::SUB:
716 case ISD::MUL:
717 {
718 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
719 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
720 //FIXME: first check for Scaled Adds and Subs!
721 if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
722 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
723 { //Normal imm add/sub
724 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
725 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
726 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
727 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
728 }
729 else
730 { //Normal add/sub
731 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULLi : Alpha::SUBL);
732 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
733 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
734 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
735 }
736 return Result;
737 }
738 default: break; //Fall Though;
739 }
740 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000741 Tmp1 = SelectExpr(N.getOperand(0));
742 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000743 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000744 switch(MVN->getExtraValueType())
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000745 {
746 default:
747 Node->dump();
748 assert(0 && "Sign Extend InReg not there yet");
749 break;
750 case MVT::i32:
751 {
752 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
753 break;
754 }
755 case MVT::i16:
756 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
757 break;
758 case MVT::i8:
759 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
760 break;
761 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000762 return Result;
763 }
764 case ISD::ZERO_EXTEND_INREG:
765 {
766 Tmp1 = SelectExpr(N.getOperand(0));
767 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000768 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000769 switch(MVN->getExtraValueType())
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000770 {
771 default:
772 Node->dump();
773 assert(0 && "Zero Extend InReg not there yet");
774 break;
775 case MVT::i32: Tmp2 = 0xf0; break;
776 case MVT::i16: Tmp2 = 0xfc; break;
777 case MVT::i8: Tmp2 = 0xfe; break;
778 case MVT::i1: //handle this one special
779 BuildMI(BB, Alpha::ANDi, 2, Result).addReg(Tmp1).addImm(1);
780 return Result;
781 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000782 BuildMI(BB, Alpha::ZAPi, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000783 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000784 }
785
786 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000787 {
788 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
789 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
790 bool isConst1 = false;
791 bool isConst2 = false;
792 int dir;
793
794 //Tmp1 = SelectExpr(N.getOperand(0));
795 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000796 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
797 isConst1 = true;
798 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000799 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
800 isConst2 = true;
801
802 switch (SetCC->getCondition()) {
803 default: Node->dump(); assert(0 && "Unknown integer comparison!");
804 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
805 case ISD::SETLT: Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
806 case ISD::SETLE: Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
807 case ISD::SETGT: Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
808 case ISD::SETGE: Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
809 case ISD::SETULT: Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
810 case ISD::SETUGT: Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
811 case ISD::SETULE: Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
812 case ISD::SETUGE: Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000813 case ISD::SETNE: {//Handle this one special
814 //std::cerr << "Alpha does not have a setne.\n";
815 //abort();
816 Tmp1 = SelectExpr(N.getOperand(0));
817 Tmp2 = SelectExpr(N.getOperand(1));
818 Tmp3 = MakeReg(MVT::i64);
819 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
820 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000821 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
822 //BuildMI(BB,Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000823 return Result;
824 }
825 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000826 if (dir == 1) {
827 Tmp1 = SelectExpr(N.getOperand(0));
828 if (isConst2) {
829 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
830 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
831 } else {
832 Tmp2 = SelectExpr(N.getOperand(1));
833 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
834 }
835 } else if (dir == 2) {
836 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +0000837 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000838 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
839 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
840 } else {
841 Tmp2 = SelectExpr(N.getOperand(0));
842 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
843 }
844 } else { //dir == 0
845 if (isConst1) {
846 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
847 Tmp2 = SelectExpr(N.getOperand(1));
848 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
849 } else if (isConst2) {
850 Tmp1 = SelectExpr(N.getOperand(0));
851 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
852 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
853 } else {
854 Tmp1 = SelectExpr(N.getOperand(0));
855 Tmp2 = SelectExpr(N.getOperand(1));
856 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
857 }
858 }
859 }
860 else
861 {
862 Node->dump();
863 assert(0 && "only integer");
864 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000865 }
866 else
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000867 {
868 Node->dump();
869 assert(0 && "Not a setcc in setcc");
870 }
871 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000872 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000873
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000874 case ISD::CopyFromReg:
875 {
Andrew Lenharth40831c52005-01-28 06:57:18 +0000876 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000877 if (Result != notIn)
878 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +0000879 else
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000880 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth40831c52005-01-28 06:57:18 +0000881
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000882 SDOperand Chain = N.getOperand(0);
883
884 Select(Chain);
885 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
886 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
887 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
888 return Result;
889 }
890
Andrew Lenharth2d6f0222005-01-24 19:44:07 +0000891 //Most of the plain arithmetic and logic share the same form, and the same
892 //constant immediate test
893 case ISD::AND:
894 case ISD::OR:
895 case ISD::XOR:
896 case ISD::SHL:
897 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +0000898 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +0000899 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000900 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
901 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +0000902 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
903 {
904 switch(opcode) {
905 case ISD::AND: Opc = Alpha::ANDi; break;
906 case ISD::OR: Opc = Alpha::BISi; break;
907 case ISD::XOR: Opc = Alpha::XORi; break;
908 case ISD::SHL: Opc = Alpha::SLi; break;
909 case ISD::SRL: Opc = Alpha::SRLi; break;
910 case ISD::SRA: Opc = Alpha::SRAi; break;
911 case ISD::MUL: Opc = Alpha::MULQi; break;
912 };
913 Tmp1 = SelectExpr(N.getOperand(0));
914 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
915 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
916 }
917 else
918 {
919 switch(opcode) {
920 case ISD::AND: Opc = Alpha::AND; break;
921 case ISD::OR: Opc = Alpha::BIS; break;
922 case ISD::XOR: Opc = Alpha::XOR; break;
923 case ISD::SHL: Opc = Alpha::SL; break;
924 case ISD::SRL: Opc = Alpha::SRL; break;
925 case ISD::SRA: Opc = Alpha::SRA; break;
926 case ISD::MUL: Opc = Alpha::MULQ; break;
927 };
928 Tmp1 = SelectExpr(N.getOperand(0));
929 Tmp2 = SelectExpr(N.getOperand(1));
930 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
931 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +0000932 return Result;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000933
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000934 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000935 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000936 {
Andrew Lenharth40831c52005-01-28 06:57:18 +0000937 bool isAdd = opcode == ISD::ADD;
938
939 //FIXME: first check for Scaled Adds and Subs!
940 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +0000941 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
942 { //Normal imm add/sub
943 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
944 Tmp1 = SelectExpr(N.getOperand(0));
945 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
946 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
947 }
948 else if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +0000949 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
950 { //LDA //FIXME: expand the above condition a bit
951 Tmp1 = SelectExpr(N.getOperand(0));
952 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
953 if (!isAdd)
954 Tmp2 = -Tmp2;
955 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
956 }
957 else
958 { //Normal add/sub
959 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
960 Tmp1 = SelectExpr(N.getOperand(0));
961 Tmp2 = SelectExpr(N.getOperand(1));
962 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
963 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000964 return Result;
965 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000966
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000967 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +0000968 case ISD::SREM:
969 case ISD::SDIV:
970 case ISD::UDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000971 //FIXME: alpha really doesn't support any of these operations,
972 // the ops are expanded into special library calls with
973 // special calling conventions
974 switch(opcode) {
975 case ISD::UREM: Opc = Alpha::REMQU; break;
976 case ISD::SREM: Opc = Alpha::REMQ; break;
977 case ISD::UDIV: Opc = Alpha::DIVQU; break;
978 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000979 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000980 Tmp1 = SelectExpr(N.getOperand(0));
981 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth02981182005-01-26 01:24:38 +0000982 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000983 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000984
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000985 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +0000986 case ISD::FP_TO_SINT:
987 {
988 assert (DestType == MVT::i64 && "only quads can be loaded to");
989 MVT::ValueType SrcType = N.getOperand(0).getValueType();
990 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
991
992 //The hard way:
993 // Spill the integer to memory and reload it from there.
994 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
995 MachineFunction *F = BB->getParent();
996 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
997
998 //CVTTQ STT LDQ
999 //CVTST CVTTQ STT LDQ
1000 if (SrcType == MVT::f32)
1001 {
1002 Tmp2 = MakeReg(MVT::f64);
1003 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1004 Tmp1 = Tmp2;
1005 }
1006 Tmp2 = MakeReg(MVT::f64);
1007 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
1008 BuildMI(BB, Alpha::STT, 3).addReg(Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1009 BuildMI(BB, Alpha::LDQ, 2, Result).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1010
1011 return Result;
1012 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001013
1014// // case ISD::FP_TO_UINT:
1015
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001016 case ISD::SELECT:
1017 {
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001018 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001019 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1020 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001021 // Get the condition into the zero flag.
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001022 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001023 return Result;
1024 }
1025
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001026 case ISD::Constant:
1027 {
Andrew Lenharth22d5a412005-02-02 00:51:15 +00001028 unsigned long val = cast<ConstantSDNode>(N)->getValue();
1029 if (val < 32000 && (long)val > -32000)
1030 BuildMI(BB, Alpha::LOAD_IMM, 1, Result).addImm(val);
1031 else
1032 {
1033 MachineConstantPool *CP = BB->getParent()->getConstantPool();
1034 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
1035 unsigned CPI = CP->getConstantPoolIndex(C);
1036 AlphaLowering.restoreGP(BB);
1037 BuildMI(BB, Alpha::LOAD, 1, Result).addConstantPoolIndex(CPI);
1038 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001039 return Result;
1040 }
1041
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001042 case ISD::LOAD:
1043 {
1044 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001045 if (Result != notIn)
1046 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001047 else
1048 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1049
1050 SDOperand Chain = N.getOperand(0);
1051 SDOperand Address = N.getOperand(1);
1052
Andrew Lenharthc23d6962005-02-02 04:35:44 +00001053 assert(N.getValue(0).getValueType() == MVT::i64 && "unknown Load dest type");
Andrew Lenharth2afc8212005-02-02 03:36:35 +00001054
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001055 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth2afc8212005-02-02 03:36:35 +00001056 {
1057 Select(Chain);
1058 AlphaLowering.restoreGP(BB);
1059 BuildMI(BB, Alpha::LOAD, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1060 }
1061 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1062 AlphaLowering.restoreGP(BB);
1063 BuildMI(BB, Alpha::LOAD, 1, Result).addConstantPoolIndex(CP->getIndex());
1064 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001065 else
Andrew Lenharth2afc8212005-02-02 03:36:35 +00001066 {
1067 Select(Chain);
1068 Tmp2 = SelectExpr(Address);
1069 BuildMI(BB, Alpha::LDQ, 2, Result).addImm(0).addReg(Tmp2);
1070 }
1071 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001072 }
1073 }
1074
1075 return 0;
1076}
1077
1078void ISel::Select(SDOperand N) {
1079 unsigned Tmp1, Tmp2, Opc;
1080
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001081 // FIXME: Disable for our current expansion model!
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001082 if (/*!N->hasOneUse() &&*/ !ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001083 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001084
1085 SDNode *Node = N.Val;
1086
1087 switch (N.getOpcode()) {
1088
1089 default:
1090 Node->dump(); std::cerr << "\n";
1091 assert(0 && "Node not handled yet!");
1092
1093 case ISD::BRCOND: {
1094 MachineBasicBlock *Dest =
1095 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
1096
1097 Select(N.getOperand(0));
1098 Tmp1 = SelectExpr(N.getOperand(1));
1099 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
1100 return;
1101 }
1102
1103 case ISD::BR: {
1104 MachineBasicBlock *Dest =
1105 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1106
1107 Select(N.getOperand(0));
1108 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
1109 return;
1110 }
1111
1112 case ISD::ImplicitDef:
1113 Select(N.getOperand(0));
1114 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
1115 return;
1116
1117 case ISD::EntryToken: return; // Noop
1118
1119 case ISD::TokenFactor:
1120 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1121 Select(Node->getOperand(i));
1122
1123 //N.Val->dump(); std::cerr << "\n";
1124 //assert(0 && "Node not handled yet!");
1125
1126 return;
1127
1128 case ISD::CopyToReg:
1129 Select(N.getOperand(0));
1130 Tmp1 = SelectExpr(N.getOperand(1));
1131 Tmp2 = cast<RegSDNode>(N)->getReg();
1132
1133 if (Tmp1 != Tmp2) {
1134 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1135 }
1136 return;
1137
1138 case ISD::RET:
1139 switch (N.getNumOperands()) {
1140 default:
1141 std::cerr << N.getNumOperands() << "\n";
1142 for (unsigned i = 0; i < N.getNumOperands(); ++i)
1143 std::cerr << N.getOperand(i).getValueType() << "\n";
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001144 Node->dump();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001145 assert(0 && "Unknown return instruction!");
1146 case 2:
1147 Select(N.getOperand(0));
1148 Tmp1 = SelectExpr(N.getOperand(1));
1149 switch (N.getOperand(1).getValueType()) {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001150 default: Node->dump(); assert(0 && "All other types should have been promoted!!");
1151 case MVT::f64:
1152 case MVT::f32:
1153 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1154 break;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001155 case MVT::i32:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001156 case MVT::i64:
1157 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1158 break;
1159 }
1160 break;
1161 case 1:
1162 Select(N.getOperand(0));
1163 break;
1164 }
1165 //Tmp2 = AlphaLowering.getRetAddr();
1166 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
1167 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
1168 return;
1169
1170 case ISD::STORE:
1171 Select(N.getOperand(0));
1172 Tmp1 = SelectExpr(N.getOperand(1)); //value
1173 if (N.getOperand(2).getOpcode() == ISD::GlobalAddress)
1174 {
1175 AlphaLowering.restoreGP(BB);
1176 BuildMI(BB, Alpha::STORE, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(N.getOperand(2))->getGlobal());
1177 }
1178 else
1179 {
1180 Tmp2 = SelectExpr(N.getOperand(2)); //address
1181 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addImm(0).addReg(Tmp2);
1182 }
1183 return;
1184
1185 case ISD::EXTLOAD:
1186 case ISD::SEXTLOAD:
1187 case ISD::ZEXTLOAD:
1188 case ISD::LOAD:
1189 case ISD::CopyFromReg:
1190 case ISD::CALL:
1191// case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001192 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001193 SelectExpr(N);
1194 return;
1195
1196
1197 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
1198 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001199 if (StoredTy == MVT::i64) {
1200 Node->dump();
1201 assert(StoredTy != MVT::i64 && "Unsupported TRUNCSTORE for this target!");
1202 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001203
1204 Select(N.getOperand(0));
1205 Tmp1 = SelectExpr(N.getOperand(1));
1206 Tmp2 = SelectExpr(N.getOperand(2));
1207
1208 switch (StoredTy) {
Chris Lattnerd7b59d02005-01-30 16:32:48 +00001209 default: Node->dump(); assert(0 && "Unhandled Type");
Andrew Lenharthd279b412005-01-25 19:58:40 +00001210 case MVT::i1: //FIXME: DAG does not promote this load
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001211 case MVT::i8: Opc = Alpha::STB; break;
1212 case MVT::i16: Opc = Alpha::STW; break;
1213 case MVT::i32: Opc = Alpha::STL; break;
1214 }
1215
1216 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(0).addReg(Tmp2);
1217 return;
1218 }
1219
1220 case ISD::ADJCALLSTACKDOWN:
1221 case ISD::ADJCALLSTACKUP:
1222 Select(N.getOperand(0));
1223 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1224
1225 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? Alpha::ADJUSTSTACKDOWN :
1226 Alpha::ADJUSTSTACKUP;
1227 BuildMI(BB, Opc, 1).addImm(Tmp1);
1228 return;
1229 }
1230 assert(0 && "Should not be reached!");
1231}
1232
1233
1234/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1235/// into a machine code representation using pattern matching and a machine
1236/// description file.
1237///
1238FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
1239 return new ISel(TM);
1240}