blob: ecd635f43a96a941df824b919dde0a7dcf6a5825 [file] [log] [blame]
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001//===- AlphaISelPattern.cpp - A pattern matching inst selector for Alpha -===//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for Alpha.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Alpha.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000015#include "AlphaRegisterInfo.h"
16#include "llvm/Constants.h" // FIXME: REMOVE
17#include "llvm/Function.h"
18#include "llvm/CodeGen/MachineInstrBuilder.h"
19#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/CodeGen/SSARegMap.h"
25#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetLowering.h"
27#include "llvm/Support/MathExtras.h"
28#include "llvm/ADT/Statistic.h"
29#include <set>
Andrew Lenharth684f2292005-01-30 00:35:27 +000030#include <algorithm>
Andrew Lenharth304d0f32005-01-22 23:41:55 +000031using namespace llvm;
32
33//===----------------------------------------------------------------------===//
34// AlphaTargetLowering - Alpha Implementation of the TargetLowering interface
35namespace {
36 class AlphaTargetLowering : public TargetLowering {
37 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
38 unsigned GP; //GOT vreg
39 public:
40 AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
41 // Set up the TargetLowering object.
Andrew Lenharth3d65d312005-01-27 03:49:45 +000042 //I am having problems with shr n ubyte 1
43 setShiftAmountType(MVT::i64); //are these needed?
44 setSetCCResultType(MVT::i64); //are these needed?
45
Andrew Lenharth304d0f32005-01-22 23:41:55 +000046 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
47 addRegisterClass(MVT::f64, Alpha::FPRCRegisterClass);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000048 addRegisterClass(MVT::f32, Alpha::FPRCRegisterClass);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000049
Andrew Lenharthd2bb9602005-01-27 07:50:35 +000050 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000051
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +000052 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000053 setOperationAction(ISD::ZEXTLOAD , MVT::i32 , Expand);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000054
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +000055 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000056 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
57 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
58
Andrew Lenharth3d65d312005-01-27 03:49:45 +000059 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand); //what is the sign expansion of 1? 1 or -1?
Andrew Lenharth02981182005-01-26 01:24:38 +000060
Andrew Lenharth3d65d312005-01-27 03:49:45 +000061 setOperationAction(ISD::SREM, MVT::f32, Expand);
62 setOperationAction(ISD::SREM, MVT::f64, Expand);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +000063
Andrew Lenharth3d65d312005-01-27 03:49:45 +000064 computeRegisterProperties();
Andrew Lenharth304d0f32005-01-22 23:41:55 +000065
Andrew Lenharthd2bb9602005-01-27 07:50:35 +000066 addLegalFPImmediate(+0.0); //F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +000067 }
68
69 /// LowerArguments - This hook must be implemented to indicate how we should
70 /// lower the arguments for the specified function, into the specified DAG.
71 virtual std::vector<SDOperand>
72 LowerArguments(Function &F, SelectionDAG &DAG);
73
74 /// LowerCallTo - This hook lowers an abstract call to a function into an
75 /// actual call.
76 virtual std::pair<SDOperand, SDOperand>
77 LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
78 ArgListTy &Args, SelectionDAG &DAG);
79
80 virtual std::pair<SDOperand, SDOperand>
81 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
82
83 virtual std::pair<SDOperand,SDOperand>
84 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
85 const Type *ArgTy, SelectionDAG &DAG);
86
87 virtual std::pair<SDOperand, SDOperand>
88 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
89 SelectionDAG &DAG);
90
91 void restoreGP(MachineBasicBlock* BB)
92 {
93 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
94 }
95 };
96}
97
98//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
99
100//For now, just use variable size stack frame format
101
102//In a standard call, the first six items are passed in registers $16
103//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
104//of argument-to-register correspondence.) The remaining items are
105//collected in a memory argument list that is a naturally aligned
106//array of quadwords. In a standard call, this list, if present, must
107//be passed at 0(SP).
108//7 ... n 0(SP) ... (n-7)*8(SP)
109
110std::vector<SDOperand>
111AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
112{
113 std::vector<SDOperand> ArgValues;
114
115 // //#define FP $15
116 // //#define RA $26
117 // //#define PV $27
118 // //#define GP $29
119 // //#define SP $30
120
121 // assert(0 && "TODO");
122 MachineFunction &MF = DAG.getMachineFunction();
123 MachineFrameInfo *MFI = MF.getFrameInfo();
124
125 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
126 MachineBasicBlock& BB = MF.front();
127
128 //Handle the return address
129 //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
130
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000131 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
132 Alpha::R19, Alpha::R20, Alpha::R21};
133 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
134 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000135 std::vector<unsigned> argVreg;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000136 std::vector<unsigned> argPreg;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000137 std::vector<unsigned> argOpc;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000138 int count = 0;
139 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I)
140 {
Andrew Lenharth40831c52005-01-28 06:57:18 +0000141 SDOperand newroot, argt;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000142 if (count < 6) {
143 switch (getValueType(I->getType())) {
144 default: std::cerr << "Unknown Type " << getValueType(I->getType()) << "\n"; abort();
145 case MVT::f64:
146 case MVT::f32:
147 BuildMI(&BB, Alpha::IDEF, 0, args_float[count]);
148 argVreg.push_back(MF.getSSARegMap()->createVirtualRegister(getRegClassFor(getValueType(I->getType()))));
149 argPreg.push_back(args_float[count]);
150 argOpc.push_back(Alpha::CPYS);
151 newroot = DAG.getCopyFromReg(argVreg[count], getValueType(I->getType()), DAG.getRoot());
152 break;
153 case MVT::i1:
154 case MVT::i8:
155 case MVT::i16:
156 case MVT::i32:
157 case MVT::i64:
158 BuildMI(&BB, Alpha::IDEF, 0, args_int[count]);
159 argVreg.push_back(MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64)));
160 argPreg.push_back(args_int[count]);
161 argOpc.push_back(Alpha::BIS);
162 argt = newroot = DAG.getCopyFromReg(argVreg[count], MVT::i64, DAG.getRoot());
163 if (getValueType(I->getType()) != MVT::i64)
164 argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()), newroot);
165 break;
166 }
167 } else { //more args
168 // Create the frame index object for this incoming parameter...
169 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
170
171 // Create the SelectionDAG nodes corresponding to a load from this parameter
172 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
173 argt = newroot = DAG.getLoad(getValueType(I->getType()), DAG.getEntryNode(), FIN);
Andrew Lenharth40831c52005-01-28 06:57:18 +0000174 }
175 DAG.setRoot(newroot.getValue(1));
176 ArgValues.push_back(argt);
Andrew Lenharth684f2292005-01-30 00:35:27 +0000177 ++count;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000178 }
179
180 BuildMI(&BB, Alpha::IDEF, 0, Alpha::R29);
181 BuildMI(&BB, Alpha::BIS, 2, GP).addReg(Alpha::R29).addReg(Alpha::R29);
Andrew Lenharth40831c52005-01-28 06:57:18 +0000182 for (int i = 0; i < count; ++i)
183 BuildMI(&BB, argOpc[i], 2, argVreg[i]).addReg(argPreg[i]).addReg(argPreg[i]);
184
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000185 return ArgValues;
186}
187
188std::pair<SDOperand, SDOperand>
189AlphaTargetLowering::LowerCallTo(SDOperand Chain,
190 const Type *RetTy, SDOperand Callee,
191 ArgListTy &Args, SelectionDAG &DAG) {
192 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000193 if (Args.size() > 6)
194 NumBytes = (Args.size() - 6) * 8;
195
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000196 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
197 DAG.getConstant(NumBytes, getPointerTy()));
198 std::vector<SDOperand> args_to_use;
199 for (unsigned i = 0, e = Args.size(); i != e; ++i)
200 {
201 switch (getValueType(Args[i].second)) {
202 default: assert(0 && "Unexpected ValueType for argument!");
203 case MVT::i1:
204 case MVT::i8:
205 case MVT::i16:
206 case MVT::i32:
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000207 // Promote the integer to 64 bits. If the input type is signed use a
208 // sign extend, otherwise use a zero extend.
209 if (Args[i].second->isSigned())
Andrew Lenharth40831c52005-01-28 06:57:18 +0000210 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000211 else
Andrew Lenharth40831c52005-01-28 06:57:18 +0000212 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000213 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000214 case MVT::i64:
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000215 case MVT::f64:
216 case MVT::f32:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000217 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000218 }
219 args_to_use.push_back(Args[i].first);
220 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000221
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000222 std::vector<MVT::ValueType> RetVals;
223 MVT::ValueType RetTyVT = getValueType(RetTy);
224 if (RetTyVT != MVT::isVoid)
225 RetVals.push_back(RetTyVT);
226 RetVals.push_back(MVT::Other);
227
228 SDOperand TheCall = SDOperand(DAG.getCall(RetVals, Chain, Callee, args_to_use), 0);
229 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
230 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
231 DAG.getConstant(NumBytes, getPointerTy()));
232 return std::make_pair(TheCall, Chain);
233}
234
235std::pair<SDOperand, SDOperand>
236AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
237 //vastart just returns the address of the VarArgsFrameIndex slot.
238 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
239}
240
241std::pair<SDOperand,SDOperand> AlphaTargetLowering::
242LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
243 const Type *ArgTy, SelectionDAG &DAG) {
244 abort();
245}
246
247
248std::pair<SDOperand, SDOperand> AlphaTargetLowering::
249LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
250 SelectionDAG &DAG) {
251 abort();
252}
253
254
255
256
257
258namespace {
259
260 //===--------------------------------------------------------------------===//
261 /// ISel - Alpha specific code to select Alpha machine instructions for
262 /// SelectionDAG operations.
263 ///
264 class ISel : public SelectionDAGISel {
265
266 /// AlphaLowering - This object fully describes how to lower LLVM code to an
267 /// Alpha-specific SelectionDAG.
268 AlphaTargetLowering AlphaLowering;
269
270
271 /// ExprMap - As shared expressions are codegen'd, we keep track of which
272 /// vreg the value is produced in, so we only emit one copy of each compiled
273 /// tree.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000274 static const unsigned notIn = (unsigned)(-1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000275 std::map<SDOperand, unsigned> ExprMap;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000276
277 public:
278 ISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM) {
279 }
280
281 /// InstructionSelectBasicBlock - This callback is invoked by
282 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
283 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
284 // Codegen the basic block.
285 Select(DAG.getRoot());
286
287 // Clear state used for selection.
288 ExprMap.clear();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000289 }
290
291 unsigned SelectExpr(SDOperand N);
Andrew Lenharth40831c52005-01-28 06:57:18 +0000292 unsigned SelectExprFP(SDOperand N, unsigned Result);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000293 void Select(SDOperand N);
294 };
295}
296
Andrew Lenharth40831c52005-01-28 06:57:18 +0000297unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
298{
299 unsigned Tmp1, Tmp2, Tmp3;
300 unsigned Opc = 0;
301 SDNode *Node = N.Val;
302 MVT::ValueType DestType = N.getValueType();
303 unsigned opcode = N.getOpcode();
304
305 switch (opcode) {
306 default:
307 Node->dump();
308 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +0000309
Andrew 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);
354 Opc = DestType == MVT::f64 ? Alpha::LDS : Alpha::LDT;
355 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
356 }
357 else
358 {
359 Select(Chain);
360 Tmp2 = SelectExpr(Address);
361 Opc = DestType == MVT::f64 ? Alpha::LDS : Alpha::LDT;
362 BuildMI(BB, Opc, 2, Result).addImm(0).addReg(Tmp2);
363 }
364 return Result;
365 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000366 case ISD::ConstantFP:
367 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
368 if (CN->isExactlyValue(+0.0)) {
369 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
370 } else {
371 abort();
372 }
373 }
374 return Result;
375
376 case ISD::MUL:
377 case ISD::ADD:
378 case ISD::SUB:
379 case ISD::SDIV:
380 switch( opcode ) {
381 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
382 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
383 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
384 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
385 };
386 Tmp1 = SelectExpr(N.getOperand(0));
387 Tmp2 = SelectExpr(N.getOperand(1));
388 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
389 return Result;
390
Andrew Lenharth2c594352005-01-29 15:42:07 +0000391 case ISD::EXTLOAD:
392 //include a conversion sequence for float loads to double
393 if (Result != notIn)
394 ExprMap[N.getValue(1)] = notIn; // Generate the token
395 else
396 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
397
398 Tmp2 = MakeReg(MVT::f32);
399
400 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
401 if (Node->getValueType(0) == MVT::f64) {
402 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
403 "Bad EXTLOAD!");
404 BuildMI(BB, Alpha::LDS, 1, Tmp2).addConstantPoolIndex(CP->getIndex());
405 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp2);
406 return Result;
407 }
408 Select(Node->getOperand(0)); // chain
409 Tmp1 = SelectExpr(Node->getOperand(1));
410 BuildMI(BB, Alpha::LDS, 1, Tmp2).addReg(Tmp1);
411 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp2);
412 return Result;
413
414
415 //case ISD::UINT_TO_FP:
416
417 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000418 {
419 assert (N.getOperand(0).getValueType() == MVT::i64 && "only quads can be loaded from");
420 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +0000421 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000422
423 //The hard way:
424 // Spill the integer to memory and reload it from there.
425 unsigned Size = MVT::getSizeInBits(MVT::i64)/8;
426 MachineFunction *F = BB->getParent();
427 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
428
Andrew Lenharth7efadce2005-01-31 01:44:26 +0000429 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addFrameIndex(FrameIdx).addReg(Alpha::F31);
430 BuildMI(BB, Alpha::LDT, 2, Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
431 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
432 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000433
434 //The easy way: doesn't work
435// //so these instructions are not supported on ev56
436// Opc = DestType == MVT::f64 ? Alpha::ITOFT : Alpha::ITOFS;
437// BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
438// Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
439// BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
440
Andrew Lenharth40831c52005-01-28 06:57:18 +0000441 return Result;
442 }
443 }
444 assert(0 && "should not get here");
445 return 0;
446}
447
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000448unsigned ISel::SelectExpr(SDOperand N) {
449 unsigned Result;
450 unsigned Tmp1, Tmp2, Tmp3;
451 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000452 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000453
454 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000455 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000456
457 unsigned &Reg = ExprMap[N];
458 if (Reg) return Reg;
459
460 if (N.getOpcode() != ISD::CALL)
461 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000462 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000463 else {
464 // If this is a call instruction, make sure to prepare ALL of the result
465 // values as well as the chain.
466 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000467 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000468 else {
469 Result = MakeReg(Node->getValueType(0));
470 ExprMap[N.getValue(0)] = Result;
471 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
472 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000473 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000474 }
475 }
476
Andrew Lenharth40831c52005-01-28 06:57:18 +0000477 if (DestType == MVT::f64 || DestType == MVT::f32)
478 return SelectExprFP(N, Result);
479
480 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000481 default:
482 Node->dump();
483 assert(0 && "Node not handled!\n");
484
Andrew Lenharth2c594352005-01-29 15:42:07 +0000485 case ISD::ConstantPool:
486 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
487 AlphaLowering.restoreGP(BB);
488 BuildMI(BB, Alpha::LOAD, 1, Result).addConstantPoolIndex(Tmp1);
489 return Result;
490
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000491 case ISD::FrameIndex:
492 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Andrew Lenharth684f2292005-01-30 00:35:27 +0000493 BuildMI(BB, Alpha::LDA, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000494 return Result;
495
496 case ISD::EXTLOAD:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000497 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000498 if (Result != notIn)
499 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000500 else
501 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
502
503 Select(Node->getOperand(0)); // chain
504 Tmp1 = SelectExpr(Node->getOperand(1));
505
506 switch(Node->getValueType(0)) {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000507 default: Node->dump(); assert(0 && "Unknown type to sign extend to.");
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000508 case MVT::i64:
509 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
510 default:
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000511 Node->dump();
512 assert(0 && "Bad extend load!");
Andrew Lenharthd279b412005-01-25 19:58:40 +0000513 case MVT::i64:
514 BuildMI(BB, Alpha::LDQ, 2, Result).addImm(0).addReg(Tmp1);
515 break;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000516 case MVT::i32:
517 BuildMI(BB, Alpha::LDL, 2, Result).addImm(0).addReg(Tmp1);
518 break;
519 case MVT::i16:
520 BuildMI(BB, Alpha::LDWU, 2, Result).addImm(0).addReg(Tmp1);
521 break;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000522 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000523 case MVT::i8:
524 BuildMI(BB, Alpha::LDBU, 2, Result).addImm(0).addReg(Tmp1);
525 break;
526 }
527 break;
528 }
529 return Result;
530
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000531 case ISD::SEXTLOAD:
532 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000533 if (Result != notIn)
534 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000535 else
536 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
537
538 Select(Node->getOperand(0)); // chain
539 Tmp1 = SelectExpr(Node->getOperand(1));
540 switch(Node->getValueType(0)) {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000541 default: Node->dump(); assert(0 && "Unknown type to sign extend to.");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000542 case MVT::i64:
543 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
544 default:
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000545 Node->dump();
546 assert(0 && "Bad sign extend!");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000547 case MVT::i32:
548 BuildMI(BB, Alpha::LDL, 2, Result).addImm(0).addReg(Tmp1);
549 break;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000550 }
551 break;
552 }
553 return Result;
554
555 case ISD::ZEXTLOAD:
556 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000557 if (Result != notIn)
558 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000559 else
560 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
561
562 Select(Node->getOperand(0)); // chain
563 Tmp1 = SelectExpr(Node->getOperand(1));
564 switch(Node->getValueType(0)) {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000565 default: Node->dump(); assert(0 && "Unknown type to zero extend to.");
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000566 case MVT::i64:
567 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
568 default:
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000569 Node->dump();
570 assert(0 && "Bad sign extend!");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000571 case MVT::i16:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000572 BuildMI(BB, Alpha::LDWU, 2, Result).addImm(0).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000573 break;
574 case MVT::i8:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000575 BuildMI(BB, Alpha::LDBU, 2, Result).addImm(0).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000576 break;
577 }
578 break;
579 }
580 return Result;
581
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000582
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000583 case ISD::GlobalAddress:
584 AlphaLowering.restoreGP(BB);
585 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
586 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
587 return Result;
588
589 case ISD::CALL:
590 {
591 Select(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000592
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000593 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000594 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000595
596 //grab the arguments
597 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000598 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000599 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000600 argvregs.push_back(SelectExpr(N.getOperand(i)));
601
Andrew Lenharth684f2292005-01-30 00:35:27 +0000602 //in reg args
603 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
604 {
605 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
606 Alpha::R19, Alpha::R20, Alpha::R21};
607 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
608 Alpha::F19, Alpha::F20, Alpha::F21};
609 switch(N.getOperand(i+2).getValueType()) {
610 default:
611 Node->dump();
612 N.getOperand(i).Val->dump();
613 std::cerr << "Type for " << i << " is: " << N.getOperand(i+2).getValueType() << "\n";
614 assert(0 && "Unknown value type for call");
615 case MVT::i1:
616 case MVT::i8:
617 case MVT::i16:
618 case MVT::i32:
619 case MVT::i64:
620 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
621 break;
622 case MVT::f32:
623 case MVT::f64:
624 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
625 break;
626 }
627 }
628 //in mem args
629 for (int i = 6, e = argvregs.size(); i < e; ++i)
630 {
631 switch(N.getOperand(i+2).getValueType()) {
632 default:
633 Node->dump();
634 N.getOperand(i).Val->dump();
635 std::cerr << "Type for " << i << " is: " << N.getOperand(i+2).getValueType() << "\n";
636 assert(0 && "Unknown value type for call");
637 case MVT::i1:
638 case MVT::i8:
639 case MVT::i16:
640 case MVT::i32:
641 case MVT::i64:
642 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
643 break;
644 case MVT::f32:
645 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
646 break;
647 case MVT::f64:
648 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
649 break;
650 }
651 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000652 //build the right kind of call
653 if (GlobalAddressSDNode *GASD =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000654 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
655 {
656 AlphaLowering.restoreGP(BB);
657 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal(),true);
658 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000659 else if (ExternalSymbolSDNode *ESSDN =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000660 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
661 {
662 AlphaLowering.restoreGP(BB);
663 BuildMI(BB, Alpha::CALL, 0).addExternalSymbol(ESSDN->getSymbol(), true);
664 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000665 else
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000666 {
667 //no need to restore GP as we are doing an indirect call
668 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000669 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
670 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000671 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000672
673 //push the result into a virtual register
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000674
675 switch (Node->getValueType(0)) {
676 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000677 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000678 case MVT::i1:
679 case MVT::i8:
680 case MVT::i16:
681 case MVT::i32:
682 case MVT::i64:
683 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
684 break;
685 case MVT::f32:
686 case MVT::f64:
687 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
688 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000689 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000690 return Result+N.ResNo;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000691 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000692
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000693 case ISD::SIGN_EXTEND:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000694 abort();
695
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000696 case ISD::SIGN_EXTEND_INREG:
697 {
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000698 //Alpha has instructions for a bunch of signed 32 bit stuff
699 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
700 {
701 switch (N.getOperand(0).getOpcode()) {
702 case ISD::ADD:
703 case ISD::SUB:
704 case ISD::MUL:
705 {
706 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
707 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
708 //FIXME: first check for Scaled Adds and Subs!
709 if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
710 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
711 { //Normal imm add/sub
712 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
713 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
714 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
715 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
716 }
717 else
718 { //Normal add/sub
719 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULLi : Alpha::SUBL);
720 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
721 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
722 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
723 }
724 return Result;
725 }
726 default: break; //Fall Though;
727 }
728 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000729 Tmp1 = SelectExpr(N.getOperand(0));
730 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000731 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000732 switch(MVN->getExtraValueType())
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000733 {
734 default:
735 Node->dump();
736 assert(0 && "Sign Extend InReg not there yet");
737 break;
738 case MVT::i32:
739 {
740 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
741 break;
742 }
743 case MVT::i16:
744 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
745 break;
746 case MVT::i8:
747 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
748 break;
749 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000750 return Result;
751 }
752 case ISD::ZERO_EXTEND_INREG:
753 {
754 Tmp1 = SelectExpr(N.getOperand(0));
755 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000756 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000757 switch(MVN->getExtraValueType())
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000758 {
759 default:
760 Node->dump();
761 assert(0 && "Zero Extend InReg not there yet");
762 break;
763 case MVT::i32: Tmp2 = 0xf0; break;
764 case MVT::i16: Tmp2 = 0xfc; break;
765 case MVT::i8: Tmp2 = 0xfe; break;
766 case MVT::i1: //handle this one special
767 BuildMI(BB, Alpha::ANDi, 2, Result).addReg(Tmp1).addImm(1);
768 return Result;
769 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000770 BuildMI(BB, Alpha::ZAPi, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000771 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000772 }
773
774 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000775 {
776 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
777 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
778 bool isConst1 = false;
779 bool isConst2 = false;
780 int dir;
781
782 //Tmp1 = SelectExpr(N.getOperand(0));
783 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000784 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
785 isConst1 = true;
786 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000787 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
788 isConst2 = true;
789
790 switch (SetCC->getCondition()) {
791 default: Node->dump(); assert(0 && "Unknown integer comparison!");
792 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
793 case ISD::SETLT: Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
794 case ISD::SETLE: Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
795 case ISD::SETGT: Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
796 case ISD::SETGE: Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
797 case ISD::SETULT: Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
798 case ISD::SETUGT: Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
799 case ISD::SETULE: Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
800 case ISD::SETUGE: Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000801 case ISD::SETNE: {//Handle this one special
802 //std::cerr << "Alpha does not have a setne.\n";
803 //abort();
804 Tmp1 = SelectExpr(N.getOperand(0));
805 Tmp2 = SelectExpr(N.getOperand(1));
806 Tmp3 = MakeReg(MVT::i64);
807 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
808 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000809 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
810 //BuildMI(BB,Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +0000811 return Result;
812 }
813 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000814 if (dir == 1) {
815 Tmp1 = SelectExpr(N.getOperand(0));
816 if (isConst2) {
817 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
818 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
819 } else {
820 Tmp2 = SelectExpr(N.getOperand(1));
821 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
822 }
823 } else if (dir == 2) {
824 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +0000825 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000826 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
827 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
828 } else {
829 Tmp2 = SelectExpr(N.getOperand(0));
830 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
831 }
832 } else { //dir == 0
833 if (isConst1) {
834 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
835 Tmp2 = SelectExpr(N.getOperand(1));
836 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
837 } else if (isConst2) {
838 Tmp1 = SelectExpr(N.getOperand(0));
839 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
840 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
841 } else {
842 Tmp1 = SelectExpr(N.getOperand(0));
843 Tmp2 = SelectExpr(N.getOperand(1));
844 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
845 }
846 }
847 }
848 else
849 {
850 Node->dump();
851 assert(0 && "only integer");
852 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000853 }
854 else
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000855 {
856 Node->dump();
857 assert(0 && "Not a setcc in setcc");
858 }
859 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000860 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000861
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000862 case ISD::CopyFromReg:
863 {
Andrew Lenharth40831c52005-01-28 06:57:18 +0000864 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000865 if (Result != notIn)
866 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +0000867 else
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000868 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth40831c52005-01-28 06:57:18 +0000869
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000870 SDOperand Chain = N.getOperand(0);
871
872 Select(Chain);
873 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
874 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
875 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
876 return Result;
877 }
878
Andrew Lenharth2d6f0222005-01-24 19:44:07 +0000879 //Most of the plain arithmetic and logic share the same form, and the same
880 //constant immediate test
881 case ISD::AND:
882 case ISD::OR:
883 case ISD::XOR:
884 case ISD::SHL:
885 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +0000886 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +0000887 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000888 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
889 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +0000890 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
891 {
892 switch(opcode) {
893 case ISD::AND: Opc = Alpha::ANDi; break;
894 case ISD::OR: Opc = Alpha::BISi; break;
895 case ISD::XOR: Opc = Alpha::XORi; break;
896 case ISD::SHL: Opc = Alpha::SLi; break;
897 case ISD::SRL: Opc = Alpha::SRLi; break;
898 case ISD::SRA: Opc = Alpha::SRAi; break;
899 case ISD::MUL: Opc = Alpha::MULQi; break;
900 };
901 Tmp1 = SelectExpr(N.getOperand(0));
902 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
903 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
904 }
905 else
906 {
907 switch(opcode) {
908 case ISD::AND: Opc = Alpha::AND; break;
909 case ISD::OR: Opc = Alpha::BIS; break;
910 case ISD::XOR: Opc = Alpha::XOR; break;
911 case ISD::SHL: Opc = Alpha::SL; break;
912 case ISD::SRL: Opc = Alpha::SRL; break;
913 case ISD::SRA: Opc = Alpha::SRA; break;
914 case ISD::MUL: Opc = Alpha::MULQ; break;
915 };
916 Tmp1 = SelectExpr(N.getOperand(0));
917 Tmp2 = SelectExpr(N.getOperand(1));
918 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
919 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +0000920 return Result;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000921
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000922 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000923 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000924 {
Andrew Lenharth40831c52005-01-28 06:57:18 +0000925 bool isAdd = opcode == ISD::ADD;
926
927 //FIXME: first check for Scaled Adds and Subs!
928 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +0000929 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
930 { //Normal imm add/sub
931 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
932 Tmp1 = SelectExpr(N.getOperand(0));
933 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
934 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
935 }
936 else if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +0000937 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
938 { //LDA //FIXME: expand the above condition a bit
939 Tmp1 = SelectExpr(N.getOperand(0));
940 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
941 if (!isAdd)
942 Tmp2 = -Tmp2;
943 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
944 }
945 else
946 { //Normal add/sub
947 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
948 Tmp1 = SelectExpr(N.getOperand(0));
949 Tmp2 = SelectExpr(N.getOperand(1));
950 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
951 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000952 return Result;
953 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000954
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000955 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +0000956 case ISD::SREM:
957 case ISD::SDIV:
958 case ISD::UDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000959 //FIXME: alpha really doesn't support any of these operations,
960 // the ops are expanded into special library calls with
961 // special calling conventions
962 switch(opcode) {
963 case ISD::UREM: Opc = Alpha::REMQU; break;
964 case ISD::SREM: Opc = Alpha::REMQ; break;
965 case ISD::UDIV: Opc = Alpha::DIVQU; break;
966 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000967 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000968 Tmp1 = SelectExpr(N.getOperand(0));
969 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth02981182005-01-26 01:24:38 +0000970 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000971 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000972// // case ISD::UINT_TO_FP:
973
Andrew Lenharth7efadce2005-01-31 01:44:26 +0000974
975 case ISD::FP_TO_SINT:
976 {
977 assert (DestType == MVT::i64 && "only quads can be loaded to");
978 MVT::ValueType SrcType = N.getOperand(0).getValueType();
979 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
980
981 //The hard way:
982 // Spill the integer to memory and reload it from there.
983 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
984 MachineFunction *F = BB->getParent();
985 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
986
987 //CVTTQ STT LDQ
988 //CVTST CVTTQ STT LDQ
989 if (SrcType == MVT::f32)
990 {
991 Tmp2 = MakeReg(MVT::f64);
992 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
993 Tmp1 = Tmp2;
994 }
995 Tmp2 = MakeReg(MVT::f64);
996 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
997 BuildMI(BB, Alpha::STT, 3).addReg(Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
998 BuildMI(BB, Alpha::LDQ, 2, Result).addFrameIndex(FrameIdx).addReg(Alpha::F31);
999
1000 return Result;
1001 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001002
1003// // case ISD::FP_TO_UINT:
1004
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001005 case ISD::SELECT:
1006 {
1007 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1008 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1009 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1010 // Get the condition into the zero flag.
1011 unsigned dummy = MakeReg(MVT::i64);
1012 BuildMI(BB, Alpha::BIS, 2, dummy).addReg(Tmp3).addReg(Tmp3);
1013 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
1014 return Result;
1015 }
1016
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001017 case ISD::Constant:
1018 {
1019 long val = cast<ConstantSDNode>(N)->getValue();
1020 BuildMI(BB, Alpha::LOAD_IMM, 1, Result).addImm(val);
1021 return Result;
1022 }
1023
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001024 case ISD::LOAD:
1025 {
1026 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001027 if (Result != notIn)
1028 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001029 else
1030 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1031
1032 SDOperand Chain = N.getOperand(0);
1033 SDOperand Address = N.getOperand(1);
1034
1035 if (Address.getOpcode() == ISD::GlobalAddress)
1036 {
1037 Select(Chain);
1038 AlphaLowering.restoreGP(BB);
1039 BuildMI(BB, Alpha::LOAD, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1040 }
1041 else
1042 {
1043 Select(Chain);
1044 Tmp2 = SelectExpr(Address);
1045 BuildMI(BB, Alpha::LDQ, 2, Result).addImm(0).addReg(Tmp2);
1046 }
1047 return Result;
1048 }
1049 }
1050
1051 return 0;
1052}
1053
1054void ISel::Select(SDOperand N) {
1055 unsigned Tmp1, Tmp2, Opc;
1056
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001057 // FIXME: Disable for our current expansion model!
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001058 if (/*!N->hasOneUse() &&*/ !ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001059 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001060
1061 SDNode *Node = N.Val;
1062
1063 switch (N.getOpcode()) {
1064
1065 default:
1066 Node->dump(); std::cerr << "\n";
1067 assert(0 && "Node not handled yet!");
1068
1069 case ISD::BRCOND: {
1070 MachineBasicBlock *Dest =
1071 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
1072
1073 Select(N.getOperand(0));
1074 Tmp1 = SelectExpr(N.getOperand(1));
1075 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
1076 return;
1077 }
1078
1079 case ISD::BR: {
1080 MachineBasicBlock *Dest =
1081 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1082
1083 Select(N.getOperand(0));
1084 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
1085 return;
1086 }
1087
1088 case ISD::ImplicitDef:
1089 Select(N.getOperand(0));
1090 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
1091 return;
1092
1093 case ISD::EntryToken: return; // Noop
1094
1095 case ISD::TokenFactor:
1096 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1097 Select(Node->getOperand(i));
1098
1099 //N.Val->dump(); std::cerr << "\n";
1100 //assert(0 && "Node not handled yet!");
1101
1102 return;
1103
1104 case ISD::CopyToReg:
1105 Select(N.getOperand(0));
1106 Tmp1 = SelectExpr(N.getOperand(1));
1107 Tmp2 = cast<RegSDNode>(N)->getReg();
1108
1109 if (Tmp1 != Tmp2) {
1110 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1111 }
1112 return;
1113
1114 case ISD::RET:
1115 switch (N.getNumOperands()) {
1116 default:
1117 std::cerr << N.getNumOperands() << "\n";
1118 for (unsigned i = 0; i < N.getNumOperands(); ++i)
1119 std::cerr << N.getOperand(i).getValueType() << "\n";
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001120 Node->dump();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001121 assert(0 && "Unknown return instruction!");
1122 case 2:
1123 Select(N.getOperand(0));
1124 Tmp1 = SelectExpr(N.getOperand(1));
1125 switch (N.getOperand(1).getValueType()) {
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001126 default: Node->dump(); assert(0 && "All other types should have been promoted!!");
1127 case MVT::f64:
1128 case MVT::f32:
1129 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1130 break;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001131 case MVT::i32:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001132 case MVT::i64:
1133 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1134 break;
1135 }
1136 break;
1137 case 1:
1138 Select(N.getOperand(0));
1139 break;
1140 }
1141 //Tmp2 = AlphaLowering.getRetAddr();
1142 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
1143 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
1144 return;
1145
1146 case ISD::STORE:
1147 Select(N.getOperand(0));
1148 Tmp1 = SelectExpr(N.getOperand(1)); //value
1149 if (N.getOperand(2).getOpcode() == ISD::GlobalAddress)
1150 {
1151 AlphaLowering.restoreGP(BB);
1152 BuildMI(BB, Alpha::STORE, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(N.getOperand(2))->getGlobal());
1153 }
1154 else
1155 {
1156 Tmp2 = SelectExpr(N.getOperand(2)); //address
1157 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addImm(0).addReg(Tmp2);
1158 }
1159 return;
1160
1161 case ISD::EXTLOAD:
1162 case ISD::SEXTLOAD:
1163 case ISD::ZEXTLOAD:
1164 case ISD::LOAD:
1165 case ISD::CopyFromReg:
1166 case ISD::CALL:
1167// case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001168 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001169 SelectExpr(N);
1170 return;
1171
1172
1173 case ISD::TRUNCSTORE: { // truncstore chain, val, ptr :storety
1174 MVT::ValueType StoredTy = cast<MVTSDNode>(Node)->getExtraValueType();
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001175 if (StoredTy == MVT::i64) {
1176 Node->dump();
1177 assert(StoredTy != MVT::i64 && "Unsupported TRUNCSTORE for this target!");
1178 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001179
1180 Select(N.getOperand(0));
1181 Tmp1 = SelectExpr(N.getOperand(1));
1182 Tmp2 = SelectExpr(N.getOperand(2));
1183
1184 switch (StoredTy) {
Chris Lattnerd7b59d02005-01-30 16:32:48 +00001185 default: Node->dump(); assert(0 && "Unhandled Type");
Andrew Lenharthd279b412005-01-25 19:58:40 +00001186 case MVT::i1: //FIXME: DAG does not promote this load
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001187 case MVT::i8: Opc = Alpha::STB; break;
1188 case MVT::i16: Opc = Alpha::STW; break;
1189 case MVT::i32: Opc = Alpha::STL; break;
1190 }
1191
1192 BuildMI(BB, Opc, 2).addReg(Tmp1).addImm(0).addReg(Tmp2);
1193 return;
1194 }
1195
1196 case ISD::ADJCALLSTACKDOWN:
1197 case ISD::ADJCALLSTACKUP:
1198 Select(N.getOperand(0));
1199 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1200
1201 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? Alpha::ADJUSTSTACKDOWN :
1202 Alpha::ADJUSTSTACKUP;
1203 BuildMI(BB, Opc, 1).addImm(Tmp1);
1204 return;
1205 }
1206 assert(0 && "Should not be reached!");
1207}
1208
1209
1210/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1211/// into a machine code representation using pattern matching and a machine
1212/// description file.
1213///
1214FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
1215 return new ISel(TM);
1216}