blob: 912bb69d1e9918d2bfe49b8e29522b63960d0283 [file] [log] [blame]
Misha Brukman2a8350a2005-02-05 02:24:26 +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
Andrew Lenharth879ef222005-02-02 17:00:21 +000043 setShiftAmountType(MVT::i64);
44 setSetCCResultType(MVT::i64);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000045
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 Lenharth63f2ab22005-02-10 06:25:22 +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 Lenharth9818c052005-02-05 13:19:12 +000059 setOperationAction(ISD::SREM , MVT::f32 , Expand);
60 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +000061
Andrew Lenharth8d163d22005-02-02 05:49:42 +000062 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
Andrew Lenharth9818c052005-02-05 13:19:12 +000063 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
64 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
65
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000066 computeRegisterProperties();
Andrew Lenharth304d0f32005-01-22 23:41:55 +000067
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000068 addLegalFPImmediate(+0.0); //F31
69 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +000070 }
71
72 /// LowerArguments - This hook must be implemented to indicate how we should
73 /// lower the arguments for the specified function, into the specified DAG.
74 virtual std::vector<SDOperand>
75 LowerArguments(Function &F, SelectionDAG &DAG);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000076
Andrew Lenharth304d0f32005-01-22 23:41:55 +000077 /// LowerCallTo - This hook lowers an abstract call to a function into an
78 /// actual call.
79 virtual std::pair<SDOperand, SDOperand>
80 LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
81 ArgListTy &Args, SelectionDAG &DAG);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000082
Andrew Lenharth304d0f32005-01-22 23:41:55 +000083 virtual std::pair<SDOperand, SDOperand>
84 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000085
Andrew Lenharth304d0f32005-01-22 23:41:55 +000086 virtual std::pair<SDOperand,SDOperand>
87 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
88 const Type *ArgTy, SelectionDAG &DAG);
89
90 virtual std::pair<SDOperand, SDOperand>
91 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
92 SelectionDAG &DAG);
93
94 void restoreGP(MachineBasicBlock* BB)
95 {
96 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
97 }
98 };
99}
100
101//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
102
103//For now, just use variable size stack frame format
104
105//In a standard call, the first six items are passed in registers $16
106//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
107//of argument-to-register correspondence.) The remaining items are
108//collected in a memory argument list that is a naturally aligned
109//array of quadwords. In a standard call, this list, if present, must
110//be passed at 0(SP).
111//7 ... n 0(SP) ... (n-7)*8(SP)
112
113std::vector<SDOperand>
114AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
115{
116 std::vector<SDOperand> ArgValues;
117
118 // //#define FP $15
119 // //#define RA $26
120 // //#define PV $27
121 // //#define GP $29
122 // //#define SP $30
123
124 // assert(0 && "TODO");
125 MachineFunction &MF = DAG.getMachineFunction();
Andrew Lenharth05380342005-02-07 05:07:00 +0000126 MachineFrameInfo*MFI = MF.getFrameInfo();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000127
128 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
129 MachineBasicBlock& BB = MF.front();
130
131 //Handle the return address
132 //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
133
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000134 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
135 Alpha::R19, Alpha::R20, Alpha::R21};
136 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
137 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000138 unsigned argVreg[6];
139 unsigned argPreg[6];
140 unsigned argOpc[6];
141
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000142 int count = 0;
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000143
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000144 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000145 {
146 SDOperand newroot, argt;
147 if (count < 6) {
148 switch (getValueType(I->getType())) {
149 default:
150 std::cerr << "Unknown Type " << getValueType(I->getType()) << "\n";
151 abort();
152 case MVT::f64:
153 case MVT::f32:
154 BuildMI(&BB, Alpha::IDEF, 0, args_float[count]);
155 argVreg[count] =
156 MF.getSSARegMap()->createVirtualRegister(
157 getRegClassFor(getValueType(I->getType())));
158 argPreg[count] = args_float[count];
159 argOpc[count] = Alpha::CPYS;
160 argt = newroot = DAG.getCopyFromReg(argVreg[count],
161 getValueType(I->getType()),
162 DAG.getRoot());
163 break;
164 case MVT::i1:
165 case MVT::i8:
166 case MVT::i16:
167 case MVT::i32:
168 case MVT::i64:
169 BuildMI(&BB, Alpha::IDEF, 0, args_int[count]);
170 argVreg[count] =
171 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
172 argPreg[count] = args_int[count];
173 argOpc[count] = Alpha::BIS;
174 argt = newroot =
175 DAG.getCopyFromReg(argVreg[count], MVT::i64, DAG.getRoot());
176 if (getValueType(I->getType()) != MVT::i64)
177 argt =
178 DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()), newroot);
179 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000180 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000181 ++count;
182 } else { //more args
183 // Create the frame index object for this incoming parameter...
184 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
185
186 // Create the SelectionDAG nodes corresponding to a load
187 //from this parameter
188 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
189 argt = newroot = DAG.getLoad(getValueType(I->getType()),
190 DAG.getEntryNode(), FIN);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000191 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000192 DAG.setRoot(newroot.getValue(1));
193 ArgValues.push_back(argt);
194 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000195
196 BuildMI(&BB, Alpha::IDEF, 0, Alpha::R29);
197 BuildMI(&BB, Alpha::BIS, 2, GP).addReg(Alpha::R29).addReg(Alpha::R29);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000198 for (int i = 0; i < count; ++i) {
199 if (argPreg[i] == Alpha::F16 || argPreg[i] == Alpha::F17 ||
200 argPreg[i] == Alpha::F18 || argPreg[i] == Alpha::F19 ||
201 argPreg[i] == Alpha::F20 || argPreg[i] == Alpha::F21)
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000202 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000203 assert(argOpc[i] == Alpha::CPYS && "Using BIS for a float??");
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000204 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000205 BuildMI(&BB, argOpc[i], 2,
206 argVreg[i]).addReg(argPreg[i]).addReg(argPreg[i]);
207 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000208
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000209 return ArgValues;
210}
211
212std::pair<SDOperand, SDOperand>
213AlphaTargetLowering::LowerCallTo(SDOperand Chain,
214 const Type *RetTy, SDOperand Callee,
215 ArgListTy &Args, SelectionDAG &DAG) {
216 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000217 if (Args.size() > 6)
218 NumBytes = (Args.size() - 6) * 8;
219
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000220 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
221 DAG.getConstant(NumBytes, getPointerTy()));
222 std::vector<SDOperand> args_to_use;
223 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000224 {
225 switch (getValueType(Args[i].second)) {
226 default: assert(0 && "Unexpected ValueType for argument!");
227 case MVT::i1:
228 case MVT::i8:
229 case MVT::i16:
230 case MVT::i32:
231 // Promote the integer to 64 bits. If the input type is signed use a
232 // sign extend, otherwise use a zero extend.
233 if (Args[i].second->isSigned())
234 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
235 else
236 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
237 break;
238 case MVT::i64:
239 case MVT::f64:
240 case MVT::f32:
241 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000242 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000243 args_to_use.push_back(Args[i].first);
244 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000245
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000246 std::vector<MVT::ValueType> RetVals;
247 MVT::ValueType RetTyVT = getValueType(RetTy);
248 if (RetTyVT != MVT::isVoid)
249 RetVals.push_back(RetTyVT);
250 RetVals.push_back(MVT::Other);
251
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000252 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
253 Chain, Callee, args_to_use), 0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000254 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
255 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
256 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000257 return std::make_pair(TheCall, Chain);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000258}
259
260std::pair<SDOperand, SDOperand>
261AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
262 //vastart just returns the address of the VarArgsFrameIndex slot.
263 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
264}
265
266std::pair<SDOperand,SDOperand> AlphaTargetLowering::
267LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000268 const Type *ArgTy, SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000269 abort();
270}
271
272
273std::pair<SDOperand, SDOperand> AlphaTargetLowering::
274LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
275 SelectionDAG &DAG) {
276 abort();
277}
278
279
280
281
282
283namespace {
284
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000285//===--------------------------------------------------------------------===//
286/// ISel - Alpha specific code to select Alpha machine instructions for
287/// SelectionDAG operations.
288//===--------------------------------------------------------------------===//
289class ISel : public SelectionDAGISel {
290
291 /// AlphaLowering - This object fully describes how to lower LLVM code to an
292 /// Alpha-specific SelectionDAG.
293 AlphaTargetLowering AlphaLowering;
294
295
296 /// ExprMap - As shared expressions are codegen'd, we keep track of which
297 /// vreg the value is produced in, so we only emit one copy of each compiled
298 /// tree.
299 static const unsigned notIn = (unsigned)(-1);
300 std::map<SDOperand, unsigned> ExprMap;
301
302 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
303 std::map<SDOperand, unsigned> CCInvMap;
304
305public:
306 ISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM)
307 {}
308
309 /// InstructionSelectBasicBlock - This callback is invoked by
310 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
311 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
312 // Codegen the basic block.
313 Select(DAG.getRoot());
314
315 // Clear state used for selection.
316 ExprMap.clear();
317 CCInvMap.clear();
318 }
319
320 unsigned SelectExpr(SDOperand N);
321 unsigned SelectExprFP(SDOperand N, unsigned Result);
322 void Select(SDOperand N);
323
324 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
325 void SelectBranchCC(SDOperand N);
326};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000327}
328
Andrew Lenharth65838902005-02-06 16:22:15 +0000329static unsigned GetSymVersion(unsigned opcode)
330{
331 switch (opcode) {
332 default: assert(0 && "unknown load or store"); return 0;
333 case Alpha::LDQ: return Alpha::LDQ_SYM;
334 case Alpha::LDS: return Alpha::LDS_SYM;
335 case Alpha::LDT: return Alpha::LDT_SYM;
336 case Alpha::LDL: return Alpha::LDL_SYM;
337 case Alpha::LDBU: return Alpha::LDBU_SYM;
338 case Alpha::LDWU: return Alpha::LDWU_SYM;
339 case Alpha::LDW: return Alpha::LDW_SYM;
340 case Alpha::LDB: return Alpha::LDB_SYM;
341 case Alpha::STQ: return Alpha::STQ_SYM;
342 case Alpha::STS: return Alpha::STS_SYM;
343 case Alpha::STT: return Alpha::STT_SYM;
344 case Alpha::STL: return Alpha::STL_SYM;
345 case Alpha::STW: return Alpha::STW_SYM;
346 case Alpha::STB: return Alpha::STB_SYM;
347 }
348}
349
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000350//Check to see if the load is a constant offset from a base register
351void ISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
352{
353 unsigned opcode = N.getOpcode();
354 if (opcode == ISD::ADD) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000355 if(N.getOperand(1).getOpcode() == ISD::Constant &&
356 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
357 { //Normal imm add
358 Reg = SelectExpr(N.getOperand(0));
359 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
360 return;
361 }
362 else if(N.getOperand(0).getOpcode() == ISD::Constant &&
363 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 32767)
364 {
365 Reg = SelectExpr(N.getOperand(1));
366 offset = cast<ConstantSDNode>(N.getOperand(0))->getValue();
367 return;
368 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000369 }
370 Reg = SelectExpr(N);
371 offset = 0;
372 return;
373}
374
Andrew Lenharth445171a2005-02-08 00:40:03 +0000375void ISel::SelectBranchCC(SDOperand N)
376{
377 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000378 MachineBasicBlock *Dest =
379 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
380 unsigned Opc = Alpha::WTF;
381
Andrew Lenharth445171a2005-02-08 00:40:03 +0000382 Select(N.getOperand(0)); //chain
383 SDOperand CC = N.getOperand(1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000384
Andrew Lenharth445171a2005-02-08 00:40:03 +0000385 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000386 {
387 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
388 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
389 //Dropping the CC is only useful if we are comparing to 0
390 bool isZero0 = false;
391 bool isZero1 = false;
392 bool isNE = false;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000393
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000394 if(SetCC->getOperand(0).getOpcode() == ISD::Constant &&
395 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0)
396 isZero0 = true;
397 if(SetCC->getOperand(1).getOpcode() == ISD::Constant &&
398 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0)
399 isZero1 = true;
400 if(SetCC->getCondition() == ISD::SETNE)
401 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000402
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000403 if (isZero0) {
Andrew Lenharth445171a2005-02-08 00:40:03 +0000404 switch (SetCC->getCondition()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000405 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
406 case ISD::SETEQ: Opc = Alpha::BEQ; break;
407 case ISD::SETLT: Opc = Alpha::BGT; break;
408 case ISD::SETLE: Opc = Alpha::BGE; break;
409 case ISD::SETGT: Opc = Alpha::BLT; break;
410 case ISD::SETGE: Opc = Alpha::BLE; break;
411 case ISD::SETULT: Opc = Alpha::BNE; break;
412 case ISD::SETUGT: assert(0 && "0 > (unsigned) x is never true"); break;
413 case ISD::SETULE: assert(0 && "0 <= (unsigned) x is always true"); break;
414 case ISD::SETUGE: Opc = Alpha::BEQ; break; //Technically you could have this CC
415 case ISD::SETNE: Opc = Alpha::BNE; break;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000416 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000417 unsigned Tmp1 = SelectExpr(SetCC->getOperand(1));
418 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
419 return;
420 } else if (isZero1) {
421 switch (SetCC->getCondition()) {
422 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
423 case ISD::SETEQ: Opc = Alpha::BEQ; break;
424 case ISD::SETLT: Opc = Alpha::BLT; break;
425 case ISD::SETLE: Opc = Alpha::BLE; break;
426 case ISD::SETGT: Opc = Alpha::BGT; break;
427 case ISD::SETGE: Opc = Alpha::BGE; break;
428 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
429 case ISD::SETUGT: Opc = Alpha::BNE; break;
430 case ISD::SETULE: Opc = Alpha::BEQ; break; //Technically you could have this CC
431 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
432 case ISD::SETNE: Opc = Alpha::BNE; break;
433 }
434 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
435 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
436 return;
437 } else {
438 unsigned Tmp1 = SelectExpr(CC);
439 if (isNE)
440 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
441 else
442 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000443 return;
444 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000445 } else { //FP
446 //Any comparison between 2 values should be codegened as an folded branch, as moving
447 //CC to the integer register is very expensive
448 //for a cmp b: c = a - b;
449 //a = b: c = 0
450 //a < b: c < 0
451 //a > b: c > 0
452 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
453 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
454 unsigned Tmp3 = MakeReg(MVT::f64);
455 BuildMI(BB, Alpha::SUBT, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
456
457 switch (SetCC->getCondition()) {
458 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
459 case ISD::SETEQ: Opc = Alpha::FBEQ; break;
460 case ISD::SETLT: Opc = Alpha::FBLT; break;
461 case ISD::SETLE: Opc = Alpha::FBLE; break;
462 case ISD::SETGT: Opc = Alpha::FBGT; break;
463 case ISD::SETGE: Opc = Alpha::FBGE; break;
464 case ISD::SETNE: Opc = Alpha::FBNE; break;
465 }
466 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000467 return;
468 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000469 abort(); //Should never be reached
470 } else {
471 //Giveup and do the stupid thing
472 unsigned Tmp1 = SelectExpr(CC);
473 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
474 return;
475 }
Andrew Lenharth445171a2005-02-08 00:40:03 +0000476 abort(); //Should never be reached
477}
478
Andrew Lenharth40831c52005-01-28 06:57:18 +0000479unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
480{
481 unsigned Tmp1, Tmp2, Tmp3;
482 unsigned Opc = 0;
483 SDNode *Node = N.Val;
484 MVT::ValueType DestType = N.getValueType();
485 unsigned opcode = N.getOpcode();
486
487 switch (opcode) {
488 default:
489 Node->dump();
490 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +0000491
Andrew Lenharth9818c052005-02-05 13:19:12 +0000492 case ISD::SELECT:
493 {
494 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
495 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
496 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000497
498
499 // Spill the cond to memory and reload it from there.
500 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
501 MachineFunction *F = BB->getParent();
502 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
503 unsigned Tmp4 = MakeReg(MVT::f64);
504 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addFrameIndex(FrameIdx).addReg(Alpha::F31);
505 BuildMI(BB, Alpha::LDT, 2, Tmp4).addFrameIndex(FrameIdx).addReg(Alpha::F31);
506 //now ideally, we don't have to do anything to the flag...
Andrew Lenharth9818c052005-02-05 13:19:12 +0000507 // Get the condition into the zero flag.
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000508 BuildMI(BB, Alpha::FCMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp4);
Andrew Lenharth9818c052005-02-05 13:19:12 +0000509 return Result;
510 }
511
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000512 case ISD::FP_ROUND:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000513 assert (DestType == MVT::f32 &&
514 N.getOperand(0).getValueType() == MVT::f64 &&
515 "only f64 to f32 conversion supported here");
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000516 Tmp1 = SelectExpr(N.getOperand(0));
517 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
518 return Result;
519
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000520 case ISD::FP_EXTEND:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000521 assert (DestType == MVT::f64 &&
522 N.getOperand(0).getValueType() == MVT::f32 &&
523 "only f32 to f64 conversion supported here");
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000524 Tmp1 = SelectExpr(N.getOperand(0));
525 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
526 return Result;
527
Andrew Lenharth2c594352005-01-29 15:42:07 +0000528 case ISD::CopyFromReg:
529 {
530 // Make sure we generate both values.
531 if (Result != notIn)
532 ExprMap[N.getValue(1)] = notIn; // Generate the token
533 else
534 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
535
536 SDOperand Chain = N.getOperand(0);
537
538 Select(Chain);
539 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
540 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
541 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
542 return Result;
543 }
544
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000545 case ISD::LOAD:
546 {
547 // Make sure we generate both values.
548 if (Result != notIn)
549 ExprMap[N.getValue(1)] = notIn; // Generate the token
550 else
551 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000552
Andrew Lenharth29219162005-02-07 06:31:44 +0000553 DestType = N.getValue(0).getValueType();
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000554
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000555 SDOperand Chain = N.getOperand(0);
556 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000557 Select(Chain);
Andrew Lenharth65838902005-02-06 16:22:15 +0000558 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
559
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000560 if (Address.getOpcode() == ISD::GlobalAddress) {
561 AlphaLowering.restoreGP(BB);
562 Opc = GetSymVersion(Opc);
563 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
564 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000565 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000566 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000567 Opc = GetSymVersion(Opc);
Andrew Lenharth97127a12005-02-05 17:41:39 +0000568 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000569 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000570 else if(Address.getOpcode() == ISD::FrameIndex) {
571 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
572 BuildMI(BB, Opc, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
573 } else {
574 long offset;
575 SelectAddr(Address, Tmp1, offset);
576 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
577 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000578 return Result;
579 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000580 case ISD::ConstantFP:
581 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
582 if (CN->isExactlyValue(+0.0)) {
583 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000584 } else if ( CN->isExactlyValue(-0.0)) {
585 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +0000586 } else {
587 abort();
588 }
589 }
590 return Result;
591
592 case ISD::MUL:
593 case ISD::ADD:
594 case ISD::SUB:
595 case ISD::SDIV:
596 switch( opcode ) {
597 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
598 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
599 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
600 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
601 };
602 Tmp1 = SelectExpr(N.getOperand(0));
603 Tmp2 = SelectExpr(N.getOperand(1));
604 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
605 return Result;
606
Andrew Lenharth2c594352005-01-29 15:42:07 +0000607 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000608 {
609 //include a conversion sequence for float loads to double
610 if (Result != notIn)
611 ExprMap[N.getValue(1)] = notIn; // Generate the token
612 else
613 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
614
Andrew Lenhartha549deb2005-02-07 05:33:15 +0000615 Tmp1 = MakeReg(MVT::f32);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000616
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000617 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
618 "EXTLOAD not from f32");
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000619 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
620
621 SDOperand Chain = N.getOperand(0);
622 SDOperand Address = N.getOperand(1);
623 Select(Chain);
624
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000625 if (Address.getOpcode() == ISD::GlobalAddress) {
626 AlphaLowering.restoreGP(BB);
627 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
628 }
629 else if (ConstantPoolSDNode *CP =
630 dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
631 {
632 AlphaLowering.restoreGP(BB);
633 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addConstantPoolIndex(CP->getIndex());
634 }
635 else if(Address.getOpcode() == ISD::FrameIndex) {
636 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
637 BuildMI(BB, Alpha::LDS, 2, Tmp1).addFrameIndex(Tmp2).addReg(Alpha::F31);
638 } else {
639 long offset;
640 SelectAddr(Address, Tmp2, offset);
641 BuildMI(BB, Alpha::LDS, 1, Tmp1).addImm(offset).addReg(Tmp2);
642 }
Andrew Lenharth29219162005-02-07 06:31:44 +0000643 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000644 return Result;
645 }
Andrew Lenharth2c594352005-01-29 15:42:07 +0000646
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000647 case ISD::UINT_TO_FP:
648 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000649 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000650 assert (N.getOperand(0).getValueType() == MVT::i64
651 && "only quads can be loaded from");
Andrew Lenharth40831c52005-01-28 06:57:18 +0000652 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +0000653 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000654
655 //The hard way:
656 // Spill the integer to memory and reload it from there.
657 unsigned Size = MVT::getSizeInBits(MVT::i64)/8;
658 MachineFunction *F = BB->getParent();
659 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
660
Andrew Lenharth7efadce2005-01-31 01:44:26 +0000661 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addFrameIndex(FrameIdx).addReg(Alpha::F31);
662 BuildMI(BB, Alpha::LDT, 2, Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
663 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
664 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000665
666 //The easy way: doesn't work
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000667 // //so these instructions are not supported on ev56
668 // Opc = DestType == MVT::f64 ? Alpha::ITOFT : Alpha::ITOFS;
669 // BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
670 // Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
671 // BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000672
Andrew Lenharth40831c52005-01-28 06:57:18 +0000673 return Result;
674 }
675 }
676 assert(0 && "should not get here");
677 return 0;
678}
679
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000680unsigned ISel::SelectExpr(SDOperand N) {
681 unsigned Result;
682 unsigned Tmp1, Tmp2, Tmp3;
683 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000684 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000685
686 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000687 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000688
689 unsigned &Reg = ExprMap[N];
690 if (Reg) return Reg;
691
692 if (N.getOpcode() != ISD::CALL)
693 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000694 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000695 else {
696 // If this is a call instruction, make sure to prepare ALL of the result
697 // values as well as the chain.
698 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000699 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000700 else {
701 Result = MakeReg(Node->getValueType(0));
702 ExprMap[N.getValue(0)] = Result;
703 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
704 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000705 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000706 }
707 }
708
Andrew Lenharth22088bb2005-02-02 15:05:33 +0000709 if (DestType == MVT::f64 || DestType == MVT::f32 ||
Andrew Lenharth06342c32005-02-07 06:21:37 +0000710 (
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000711 (opcode == ISD::LOAD || opcode == ISD::CopyFromReg ||
712 opcode == ISD::EXTLOAD) &&
713 (N.getValue(0).getValueType() == MVT::f32 ||
714 N.getValue(0).getValueType() == MVT::f64)
Andrew Lenharth06342c32005-02-07 06:21:37 +0000715 )
716 )
Andrew Lenharth40831c52005-01-28 06:57:18 +0000717 return SelectExprFP(N, Result);
718
719 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000720 default:
721 Node->dump();
722 assert(0 && "Node not handled!\n");
723
Andrew Lenharth2c594352005-01-29 15:42:07 +0000724 case ISD::ConstantPool:
725 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
726 AlphaLowering.restoreGP(BB);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000727 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000728 return Result;
729
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000730 case ISD::FrameIndex:
731 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Andrew Lenharth684f2292005-01-30 00:35:27 +0000732 BuildMI(BB, Alpha::LDA, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000733 return Result;
734
735 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +0000736 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000737 case ISD::SEXTLOAD:
Andrew Lenhartha549deb2005-02-07 05:33:15 +0000738 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000739 {
740 // Make sure we generate both values.
741 if (Result != notIn)
742 ExprMap[N.getValue(1)] = notIn; // Generate the token
743 else
744 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000745
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000746 SDOperand Chain = N.getOperand(0);
747 SDOperand Address = N.getOperand(1);
748 Select(Chain);
749
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000750 assert(Node->getValueType(0) == MVT::i64 &&
751 "Unknown type to sign extend to.");
Andrew Lenharth03824012005-02-07 05:55:55 +0000752 if (opcode == ISD::LOAD)
753 Opc = Alpha::LDQ;
754 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000755 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
756 default: Node->dump(); assert(0 && "Bad sign extend!");
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000757 case MVT::i32: Opc = Alpha::LDL;
758 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
759 case MVT::i16: Opc = Alpha::LDWU;
760 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +0000761 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000762 case MVT::i8: Opc = Alpha::LDBU;
763 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000764 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000765
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000766 if (Address.getOpcode() == ISD::GlobalAddress) {
767 AlphaLowering.restoreGP(BB);
768 Opc = GetSymVersion(Opc);
769 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
770 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000771 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
772 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000773 Opc = GetSymVersion(Opc);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000774 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000775 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000776 else if(Address.getOpcode() == ISD::FrameIndex) {
777 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
778 BuildMI(BB, Opc, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
779 } else {
780 long offset;
781 SelectAddr(Address, Tmp1, offset);
782 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
783 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000784 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000785 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000786
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000787 case ISD::GlobalAddress:
788 AlphaLowering.restoreGP(BB);
789 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
790 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
791 return Result;
792
793 case ISD::CALL:
794 {
795 Select(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000796
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000797 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000798 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000799
800 //grab the arguments
801 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000802 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000803 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000804 argvregs.push_back(SelectExpr(N.getOperand(i)));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000805
Andrew Lenharth684f2292005-01-30 00:35:27 +0000806 //in reg args
807 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000808 {
809 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
810 Alpha::R19, Alpha::R20, Alpha::R21};
811 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
812 Alpha::F19, Alpha::F20, Alpha::F21};
813 switch(N.getOperand(i+2).getValueType()) {
814 default:
815 Node->dump();
816 N.getOperand(i).Val->dump();
817 std::cerr << "Type for " << i << " is: " <<
818 N.getOperand(i+2).getValueType() << "\n";
819 assert(0 && "Unknown value type for call");
820 case MVT::i1:
821 case MVT::i8:
822 case MVT::i16:
823 case MVT::i32:
824 case MVT::i64:
825 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
826 break;
827 case MVT::f32:
828 case MVT::f64:
829 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
830 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000831 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000832 }
Andrew Lenharth684f2292005-01-30 00:35:27 +0000833 //in mem args
834 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000835 {
836 switch(N.getOperand(i+2).getValueType()) {
837 default:
838 Node->dump();
839 N.getOperand(i).Val->dump();
840 std::cerr << "Type for " << i << " is: " <<
841 N.getOperand(i+2).getValueType() << "\n";
842 assert(0 && "Unknown value type for call");
843 case MVT::i1:
844 case MVT::i8:
845 case MVT::i16:
846 case MVT::i32:
847 case MVT::i64:
848 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
849 break;
850 case MVT::f32:
851 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
852 break;
853 case MVT::f64:
854 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
855 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000856 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000857 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000858 //build the right kind of call
859 if (GlobalAddressSDNode *GASD =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000860 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000861 {
Andrew Lenharth3e315922005-02-10 20:10:38 +0000862 //if (GASD->getGlobal()->isExternal()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000863 //use safe calling convention
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000864 AlphaLowering.restoreGP(BB);
865 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal(),true);
Andrew Lenharth3e315922005-02-10 20:10:38 +0000866 //} else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000867 //use PC relative branch call
Andrew Lenharth3e315922005-02-10 20:10:38 +0000868 //BuildMI(BB, Alpha::BSR, 1, Alpha::R26).addGlobalAddress(GASD->getGlobal(),true);
869 //}
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000870 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000871 else if (ExternalSymbolSDNode *ESSDN =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000872 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000873 {
874 AlphaLowering.restoreGP(BB);
875 BuildMI(BB, Alpha::CALL, 0).addExternalSymbol(ESSDN->getSymbol(), true);
876 } else {
877 //no need to restore GP as we are doing an indirect call
878 Tmp1 = SelectExpr(N.getOperand(1));
879 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
880 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
881 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000882
883 //push the result into a virtual register
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000884
885 switch (Node->getValueType(0)) {
886 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000887 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000888 case MVT::i1:
889 case MVT::i8:
890 case MVT::i16:
891 case MVT::i32:
892 case MVT::i64:
893 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
894 break;
895 case MVT::f32:
896 case MVT::f64:
897 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
898 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000899 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000900 return Result+N.ResNo;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000901 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000902
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000903 case ISD::SIGN_EXTEND:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000904 abort();
905
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000906 case ISD::SIGN_EXTEND_INREG:
907 {
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000908 //Alpha has instructions for a bunch of signed 32 bit stuff
909 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000910 {
911 switch (N.getOperand(0).getOpcode()) {
912 case ISD::ADD:
913 case ISD::SUB:
914 case ISD::MUL:
915 {
916 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
917 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
918 //FIXME: first check for Scaled Adds and Subs!
919 if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
920 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
921 { //Normal imm add/sub
922 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
923 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
924 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
925 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000926 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000927 else
928 { //Normal add/sub
929 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULLi : Alpha::SUBL);
930 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
931 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
932 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
933 }
934 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000935 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000936 default: break; //Fall Though;
937 }
938 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000939 Tmp1 = SelectExpr(N.getOperand(0));
940 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000941 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000942 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000943 {
944 default:
945 Node->dump();
946 assert(0 && "Sign Extend InReg not there yet");
947 break;
948 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000949 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000950 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000951 break;
952 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000953 case MVT::i16:
954 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
955 break;
956 case MVT::i8:
957 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
958 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +0000959 case MVT::i1:
960 Tmp2 = MakeReg(MVT::i64);
961 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
962 BuildMI(BB, Alpha::SUB, 2, Result).addReg(Alpha::F31).addReg(Tmp2);
963 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000964 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000965 return Result;
966 }
967 case ISD::ZERO_EXTEND_INREG:
968 {
969 Tmp1 = SelectExpr(N.getOperand(0));
970 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000971 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000972 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000973 {
974 default:
975 Node->dump();
976 assert(0 && "Zero Extend InReg not there yet");
977 break;
978 case MVT::i32: Tmp2 = 0xf0; break;
979 case MVT::i16: Tmp2 = 0xfc; break;
980 case MVT::i8: Tmp2 = 0xfe; break;
981 case MVT::i1: //handle this one special
982 BuildMI(BB, Alpha::ANDi, 2, Result).addReg(Tmp1).addImm(1);
983 return Result;
984 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000985 BuildMI(BB, Alpha::ZAPi, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000986 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000987 }
988
989 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000990 {
991 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
992 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
993 bool isConst1 = false;
994 bool isConst2 = false;
995 int dir;
Andrew Lenharth9818c052005-02-05 13:19:12 +0000996
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000997 //Tmp1 = SelectExpr(N.getOperand(0));
998 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000999 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1000 isConst1 = true;
1001 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001002 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1003 isConst2 = true;
1004
1005 switch (SetCC->getCondition()) {
1006 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1007 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001008 case ISD::SETLT:
1009 Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
1010 case ISD::SETLE:
1011 Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
1012 case ISD::SETGT:
1013 Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
1014 case ISD::SETGE:
1015 Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
1016 case ISD::SETULT:
1017 Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
1018 case ISD::SETUGT:
1019 Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
1020 case ISD::SETULE:
1021 Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
1022 case ISD::SETUGE:
1023 Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001024 case ISD::SETNE: {//Handle this one special
1025 //std::cerr << "Alpha does not have a setne.\n";
1026 //abort();
1027 Tmp1 = SelectExpr(N.getOperand(0));
1028 Tmp2 = SelectExpr(N.getOperand(1));
1029 Tmp3 = MakeReg(MVT::i64);
1030 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001031 //Remeber we have the Inv for this CC
1032 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001033 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001034 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001035 return Result;
1036 }
1037 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001038 if (dir == 1) {
1039 Tmp1 = SelectExpr(N.getOperand(0));
1040 if (isConst2) {
1041 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1042 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1043 } else {
1044 Tmp2 = SelectExpr(N.getOperand(1));
1045 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1046 }
1047 } else if (dir == 2) {
1048 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001049 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001050 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1051 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1052 } else {
1053 Tmp2 = SelectExpr(N.getOperand(0));
1054 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1055 }
1056 } else { //dir == 0
1057 if (isConst1) {
1058 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1059 Tmp2 = SelectExpr(N.getOperand(1));
1060 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
1061 } else if (isConst2) {
1062 Tmp1 = SelectExpr(N.getOperand(0));
1063 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1064 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1065 } else {
1066 Tmp1 = SelectExpr(N.getOperand(0));
1067 Tmp2 = SelectExpr(N.getOperand(1));
1068 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1069 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001070 }
1071 } else {
1072 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
1073 bool rev = false;
1074 bool inv = false;
1075
1076 switch (SetCC->getCondition()) {
1077 default: Node->dump(); assert(0 && "Unknown FP comparison!");
1078 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
1079 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
1080 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
1081 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
1082 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
1083 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
1084 }
1085
1086 Tmp1 = SelectExpr(N.getOperand(0));
1087 Tmp2 = SelectExpr(N.getOperand(1));
1088 //Can only compare doubles, and dag won't promote for me
1089 if (SetCC->getOperand(0).getValueType() == MVT::f32)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001090 {
1091 Tmp3 = MakeReg(MVT::f64);
1092 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
1093 Tmp1 = Tmp3;
1094 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001095 if (SetCC->getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001096 {
1097 Tmp3 = MakeReg(MVT::f64);
1098 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
1099 Tmp1 = Tmp2;
1100 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001101
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001102 if (rev) std::swap(Tmp1, Tmp2);
1103 Tmp3 = MakeReg(MVT::f64);
1104 //do the comparison
1105 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1106
1107 //now arrange for Result (int) to have a 1 or 0
1108
1109 // Spill the FP to memory and reload it from there.
1110 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1111 MachineFunction *F = BB->getParent();
1112 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
1113 unsigned Tmp4 = MakeReg(MVT::f64);
1114 BuildMI(BB, Alpha::CVTTQ, 1, Tmp4).addReg(Tmp3);
1115 BuildMI(BB, Alpha::STT, 3).addReg(Tmp4).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1116 unsigned Tmp5 = MakeReg(MVT::i64);
1117 BuildMI(BB, Alpha::LDQ, 2, Tmp5).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharth9818c052005-02-05 13:19:12 +00001118
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001119 //now, set result based on Tmp5
1120 //Set Tmp6 if fp cmp was false
1121 unsigned Tmp6 = MakeReg(MVT::i64);
1122 BuildMI(BB, Alpha::CMPEQ, 2, Tmp6).addReg(Tmp5).addReg(Alpha::R31);
1123 //and invert
1124 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp6).addReg(Alpha::R31);
1125
1126 }
1127 // else
1128 // {
1129 // Node->dump();
1130 // assert(0 && "Not a setcc in setcc");
1131 // }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001132 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001133 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001134 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001135
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001136 case ISD::CopyFromReg:
1137 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001138 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001139 if (Result != notIn)
1140 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001141 else
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001142 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth40831c52005-01-28 06:57:18 +00001143
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001144 SDOperand Chain = N.getOperand(0);
1145
1146 Select(Chain);
1147 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1148 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1149 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1150 return Result;
1151 }
1152
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001153 //Most of the plain arithmetic and logic share the same form, and the same
1154 //constant immediate test
1155 case ISD::AND:
1156 case ISD::OR:
1157 case ISD::XOR:
1158 case ISD::SHL:
1159 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001160 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001161 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001162 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1163 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001164 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001165 {
1166 switch(opcode) {
1167 case ISD::AND: Opc = Alpha::ANDi; break;
1168 case ISD::OR: Opc = Alpha::BISi; break;
1169 case ISD::XOR: Opc = Alpha::XORi; break;
1170 case ISD::SHL: Opc = Alpha::SLi; break;
1171 case ISD::SRL: Opc = Alpha::SRLi; break;
1172 case ISD::SRA: Opc = Alpha::SRAi; break;
1173 case ISD::MUL: Opc = Alpha::MULQi; break;
1174 };
1175 Tmp1 = SelectExpr(N.getOperand(0));
1176 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1177 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1178 } else {
1179 switch(opcode) {
1180 case ISD::AND: Opc = Alpha::AND; break;
1181 case ISD::OR: Opc = Alpha::BIS; break;
1182 case ISD::XOR: Opc = Alpha::XOR; break;
1183 case ISD::SHL: Opc = Alpha::SL; break;
1184 case ISD::SRL: Opc = Alpha::SRL; break;
1185 case ISD::SRA: Opc = Alpha::SRA; break;
1186 case ISD::MUL: Opc = Alpha::MULQ; break;
1187 };
1188 Tmp1 = SelectExpr(N.getOperand(0));
1189 Tmp2 = SelectExpr(N.getOperand(1));
1190 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1191 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001192 return Result;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001193
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001194 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001195 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001196 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001197 bool isAdd = opcode == ISD::ADD;
1198
1199 //FIXME: first check for Scaled Adds and Subs!
1200 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001201 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001202 { //Normal imm add/sub
1203 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1204 Tmp1 = SelectExpr(N.getOperand(0));
1205 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1206 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1207 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001208 else if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001209 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001210 { //LDA //FIXME: expand the above condition a bit
1211 Tmp1 = SelectExpr(N.getOperand(0));
1212 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1213 if (!isAdd)
1214 Tmp2 = -Tmp2;
1215 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
1216 } else {
1217 //Normal add/sub
1218 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1219 Tmp1 = SelectExpr(N.getOperand(0));
1220 Tmp2 = SelectExpr(N.getOperand(1));
1221 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1222 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001223 return Result;
1224 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001225
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001226 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00001227 case ISD::SREM:
1228 case ISD::SDIV:
1229 case ISD::UDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001230 //FIXME: alpha really doesn't support any of these operations,
1231 // the ops are expanded into special library calls with
1232 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001233 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00001234 switch(opcode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001235 case ISD::UREM: AlphaLowering.restoreGP(BB); Opc = Alpha::REMQU; break;
1236 case ISD::SREM: AlphaLowering.restoreGP(BB); Opc = Alpha::REMQ; break;
1237 case ISD::UDIV: AlphaLowering.restoreGP(BB); Opc = Alpha::DIVQU; break;
1238 case ISD::SDIV: AlphaLowering.restoreGP(BB); Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001239 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001240 Tmp1 = SelectExpr(N.getOperand(0));
1241 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth02981182005-01-26 01:24:38 +00001242 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001243 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001244
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001245 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001246 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001247 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001248 assert (DestType == MVT::i64 && "only quads can be loaded to");
1249 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001250 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001251 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1252
1253 //The hard way:
1254 // Spill the integer to memory and reload it from there.
1255 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1256 MachineFunction *F = BB->getParent();
1257 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
1258
1259 //CVTTQ STT LDQ
1260 //CVTST CVTTQ STT LDQ
1261 if (SrcType == MVT::f32)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001262 {
1263 Tmp2 = MakeReg(MVT::f64);
1264 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1265 Tmp1 = Tmp2;
1266 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001267 Tmp2 = MakeReg(MVT::f64);
1268 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
1269 BuildMI(BB, Alpha::STT, 3).addReg(Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1270 BuildMI(BB, Alpha::LDQ, 2, Result).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1271
1272 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001273 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001274
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001275 // // case ISD::FP_TO_UINT:
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001276
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001277 case ISD::SELECT:
1278 {
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001279 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001280 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1281 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001282 // Get the condition into the zero flag.
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001283 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001284 return Result;
1285 }
1286
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001287 case ISD::Constant:
1288 {
Andrew Lenharth22d5a412005-02-02 00:51:15 +00001289 unsigned long val = cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001290 if (val < 32000 && (long)val > -32000)
1291 BuildMI(BB, Alpha::LOAD_IMM, 1, Result).addImm(val);
1292 else {
1293 MachineConstantPool *CP = BB->getParent()->getConstantPool();
1294 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
1295 unsigned CPI = CP->getConstantPoolIndex(C);
1296 AlphaLowering.restoreGP(BB);
1297 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
1298 }
1299 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001300 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001301 }
1302
1303 return 0;
1304}
1305
1306void ISel::Select(SDOperand N) {
1307 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00001308 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001309
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001310 // FIXME: Disable for our current expansion model!
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001311 if (/*!N->hasOneUse() &&*/ !ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001312 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001313
1314 SDNode *Node = N.Val;
Andrew Lenharth760270d2005-02-07 23:02:23 +00001315
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001316
Andrew Lenharth760270d2005-02-07 23:02:23 +00001317 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001318
1319 default:
1320 Node->dump(); std::cerr << "\n";
1321 assert(0 && "Node not handled yet!");
1322
1323 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00001324 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001325 return;
1326 }
1327
1328 case ISD::BR: {
1329 MachineBasicBlock *Dest =
1330 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1331
1332 Select(N.getOperand(0));
1333 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
1334 return;
1335 }
1336
1337 case ISD::ImplicitDef:
1338 Select(N.getOperand(0));
1339 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
1340 return;
1341
1342 case ISD::EntryToken: return; // Noop
1343
1344 case ISD::TokenFactor:
1345 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1346 Select(Node->getOperand(i));
1347
1348 //N.Val->dump(); std::cerr << "\n";
1349 //assert(0 && "Node not handled yet!");
1350
1351 return;
1352
1353 case ISD::CopyToReg:
1354 Select(N.getOperand(0));
1355 Tmp1 = SelectExpr(N.getOperand(1));
1356 Tmp2 = cast<RegSDNode>(N)->getReg();
1357
1358 if (Tmp1 != Tmp2) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001359 if (N.getOperand(1).getValueType() == MVT::f64 ||
1360 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00001361 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1362 else
1363 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001364 }
1365 return;
1366
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001367 case ISD::RET:
1368 switch (N.getNumOperands()) {
1369 default:
1370 std::cerr << N.getNumOperands() << "\n";
1371 for (unsigned i = 0; i < N.getNumOperands(); ++i)
1372 std::cerr << N.getOperand(i).getValueType() << "\n";
1373 Node->dump();
1374 assert(0 && "Unknown return instruction!");
1375 case 2:
1376 Select(N.getOperand(0));
1377 Tmp1 = SelectExpr(N.getOperand(1));
1378 switch (N.getOperand(1).getValueType()) {
1379 default: Node->dump();
1380 assert(0 && "All other types should have been promoted!!");
1381 case MVT::f64:
1382 case MVT::f32:
1383 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1384 break;
1385 case MVT::i32:
1386 case MVT::i64:
1387 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1388 break;
1389 }
1390 break;
1391 case 1:
1392 Select(N.getOperand(0));
1393 break;
1394 }
1395 //Tmp2 = AlphaLowering.getRetAddr();
1396 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
1397 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
1398 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001399
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001400 case ISD::TRUNCSTORE:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001401 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001402 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001403 SDOperand Chain = N.getOperand(0);
1404 SDOperand Value = N.getOperand(1);
1405 SDOperand Address = N.getOperand(2);
1406 Select(Chain);
1407
1408 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00001409
1410 if (opcode == ISD::STORE) {
1411 switch(Value.getValueType()) {
1412 default: assert(0 && "unknown Type in store");
1413 case MVT::i64: Opc = Alpha::STQ; break;
1414 case MVT::f64: Opc = Alpha::STT; break;
1415 case MVT::f32: Opc = Alpha::STS; break;
1416 }
1417 } else { //ISD::TRUNCSTORE
1418 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1419 default: assert(0 && "unknown Type in store");
1420 case MVT::i1: //FIXME: DAG does not promote this load
1421 case MVT::i8: Opc = Alpha::STB; break;
1422 case MVT::i16: Opc = Alpha::STW; break;
1423 case MVT::i32: Opc = Alpha::STL; break;
1424 }
Andrew Lenharth65838902005-02-06 16:22:15 +00001425 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00001426
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001427 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001428 {
1429 AlphaLowering.restoreGP(BB);
1430 Opc = GetSymVersion(Opc);
1431 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1432 }
Andrew Lenharth05380342005-02-07 05:07:00 +00001433 else if(Address.getOpcode() == ISD::FrameIndex)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001434 {
1435 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1436 BuildMI(BB, Opc, 3).addReg(Tmp1).addFrameIndex(Tmp2).addReg(Alpha::F31);
1437 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001438 else
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001439 {
1440 long offset;
1441 SelectAddr(Address, Tmp2, offset);
1442 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1443 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001444 return;
1445 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001446
1447 case ISD::EXTLOAD:
1448 case ISD::SEXTLOAD:
1449 case ISD::ZEXTLOAD:
1450 case ISD::LOAD:
1451 case ISD::CopyFromReg:
1452 case ISD::CALL:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001453 // case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001454 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001455 SelectExpr(N);
1456 return;
1457
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001458 case ISD::ADJCALLSTACKDOWN:
1459 case ISD::ADJCALLSTACKUP:
1460 Select(N.getOperand(0));
1461 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1462
1463 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? Alpha::ADJUSTSTACKDOWN :
1464 Alpha::ADJUSTSTACKUP;
1465 BuildMI(BB, Opc, 1).addImm(Tmp1);
1466 return;
1467 }
1468 assert(0 && "Should not be reached!");
1469}
1470
1471
1472/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1473/// into a machine code representation using pattern matching and a machine
1474/// description file.
1475///
1476FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
1477 return new ISel(TM);
1478}