blob: 30386d8bd810b8662936a02e94bad6da7efae769 [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);
Andrew Lenharth093f3272005-02-12 21:11:17 +0000923 //if the value was really originally a i32, skip the up conversion
924 if (N.getOperand(0).getOperand(0).getOpcode() == ISD::SIGN_EXTEND_INREG &&
925 dyn_cast<MVTSDNode>(N.getOperand(0).getOperand(0).Val)
926 ->getExtraValueType() == MVT::i32)
927 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
928 else
929 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000930 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
931 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000932 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000933 else
934 { //Normal add/sub
935 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULLi : Alpha::SUBL);
Andrew Lenharth093f3272005-02-12 21:11:17 +0000936 //if the value was really originally a i32, skip the up conversion
937 if (N.getOperand(0).getOperand(0).getOpcode() == ISD::SIGN_EXTEND_INREG &&
938 dyn_cast<MVTSDNode>(N.getOperand(0).getOperand(0).Val)
939 ->getExtraValueType() == MVT::i32)
940 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
941 else
942 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
943 //if the value was really originally a i32, skip the up conversion
944 if (N.getOperand(0).getOperand(1).getOpcode() == ISD::SIGN_EXTEND_INREG &&
945 dyn_cast<MVTSDNode>(N.getOperand(0).getOperand(1).Val)
946 ->getExtraValueType() == MVT::i32)
947 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
948 else
949 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
950
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000951 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
952 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
953 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
954 }
955 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000956 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000957 default: break; //Fall Though;
958 }
959 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000960 Tmp1 = SelectExpr(N.getOperand(0));
961 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000962 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000963 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000964 {
965 default:
966 Node->dump();
967 assert(0 && "Sign Extend InReg not there yet");
968 break;
969 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000970 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000971 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000972 break;
973 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000974 case MVT::i16:
975 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
976 break;
977 case MVT::i8:
978 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
979 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +0000980 case MVT::i1:
981 Tmp2 = MakeReg(MVT::i64);
982 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenharth7536eea2005-02-12 20:42:09 +0000983 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::F31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +0000984 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000985 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000986 return Result;
987 }
988 case ISD::ZERO_EXTEND_INREG:
989 {
990 Tmp1 = SelectExpr(N.getOperand(0));
991 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000992 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000993 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000994 {
995 default:
996 Node->dump();
997 assert(0 && "Zero Extend InReg not there yet");
998 break;
999 case MVT::i32: Tmp2 = 0xf0; break;
1000 case MVT::i16: Tmp2 = 0xfc; break;
1001 case MVT::i8: Tmp2 = 0xfe; break;
1002 case MVT::i1: //handle this one special
1003 BuildMI(BB, Alpha::ANDi, 2, Result).addReg(Tmp1).addImm(1);
1004 return Result;
1005 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001006 BuildMI(BB, Alpha::ZAPi, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001007 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001008 }
1009
1010 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001011 {
1012 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1013 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1014 bool isConst1 = false;
1015 bool isConst2 = false;
1016 int dir;
Andrew Lenharth9818c052005-02-05 13:19:12 +00001017
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001018 //Tmp1 = SelectExpr(N.getOperand(0));
1019 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001020 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1021 isConst1 = true;
1022 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001023 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1024 isConst2 = true;
1025
1026 switch (SetCC->getCondition()) {
1027 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1028 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001029 case ISD::SETLT:
1030 Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
1031 case ISD::SETLE:
1032 Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
1033 case ISD::SETGT:
1034 Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
1035 case ISD::SETGE:
1036 Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
1037 case ISD::SETULT:
1038 Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
1039 case ISD::SETUGT:
1040 Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
1041 case ISD::SETULE:
1042 Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
1043 case ISD::SETUGE:
1044 Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001045 case ISD::SETNE: {//Handle this one special
1046 //std::cerr << "Alpha does not have a setne.\n";
1047 //abort();
1048 Tmp1 = SelectExpr(N.getOperand(0));
1049 Tmp2 = SelectExpr(N.getOperand(1));
1050 Tmp3 = MakeReg(MVT::i64);
1051 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001052 //Remeber we have the Inv for this CC
1053 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001054 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001055 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001056 return Result;
1057 }
1058 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001059 if (dir == 1) {
1060 Tmp1 = SelectExpr(N.getOperand(0));
1061 if (isConst2) {
1062 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1063 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1064 } else {
1065 Tmp2 = SelectExpr(N.getOperand(1));
1066 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1067 }
1068 } else if (dir == 2) {
1069 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001070 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001071 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1072 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1073 } else {
1074 Tmp2 = SelectExpr(N.getOperand(0));
1075 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1076 }
1077 } else { //dir == 0
1078 if (isConst1) {
1079 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1080 Tmp2 = SelectExpr(N.getOperand(1));
1081 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
1082 } else if (isConst2) {
1083 Tmp1 = SelectExpr(N.getOperand(0));
1084 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1085 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1086 } else {
1087 Tmp1 = SelectExpr(N.getOperand(0));
1088 Tmp2 = SelectExpr(N.getOperand(1));
1089 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1090 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001091 }
1092 } else {
1093 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
1094 bool rev = false;
1095 bool inv = false;
1096
1097 switch (SetCC->getCondition()) {
1098 default: Node->dump(); assert(0 && "Unknown FP comparison!");
1099 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
1100 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
1101 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
1102 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
1103 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
1104 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
1105 }
1106
1107 Tmp1 = SelectExpr(N.getOperand(0));
1108 Tmp2 = SelectExpr(N.getOperand(1));
1109 //Can only compare doubles, and dag won't promote for me
1110 if (SetCC->getOperand(0).getValueType() == MVT::f32)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001111 {
1112 Tmp3 = MakeReg(MVT::f64);
1113 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
1114 Tmp1 = Tmp3;
1115 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001116 if (SetCC->getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001117 {
1118 Tmp3 = MakeReg(MVT::f64);
1119 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
1120 Tmp1 = Tmp2;
1121 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001122
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001123 if (rev) std::swap(Tmp1, Tmp2);
1124 Tmp3 = MakeReg(MVT::f64);
1125 //do the comparison
1126 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1127
1128 //now arrange for Result (int) to have a 1 or 0
1129
1130 // Spill the FP to memory and reload it from there.
1131 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1132 MachineFunction *F = BB->getParent();
1133 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
1134 unsigned Tmp4 = MakeReg(MVT::f64);
1135 BuildMI(BB, Alpha::CVTTQ, 1, Tmp4).addReg(Tmp3);
1136 BuildMI(BB, Alpha::STT, 3).addReg(Tmp4).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1137 unsigned Tmp5 = MakeReg(MVT::i64);
1138 BuildMI(BB, Alpha::LDQ, 2, Tmp5).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharth9818c052005-02-05 13:19:12 +00001139
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001140 //now, set result based on Tmp5
1141 //Set Tmp6 if fp cmp was false
1142 unsigned Tmp6 = MakeReg(MVT::i64);
1143 BuildMI(BB, Alpha::CMPEQ, 2, Tmp6).addReg(Tmp5).addReg(Alpha::R31);
1144 //and invert
1145 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp6).addReg(Alpha::R31);
1146
1147 }
1148 // else
1149 // {
1150 // Node->dump();
1151 // assert(0 && "Not a setcc in setcc");
1152 // }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001153 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001154 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001155 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001156
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001157 case ISD::CopyFromReg:
1158 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001159 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001160 if (Result != notIn)
1161 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001162 else
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001163 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth40831c52005-01-28 06:57:18 +00001164
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001165 SDOperand Chain = N.getOperand(0);
1166
1167 Select(Chain);
1168 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1169 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1170 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1171 return Result;
1172 }
1173
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001174 //Most of the plain arithmetic and logic share the same form, and the same
1175 //constant immediate test
1176 case ISD::AND:
1177 case ISD::OR:
1178 case ISD::XOR:
1179 case ISD::SHL:
1180 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001181 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001182 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001183 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1184 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001185 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001186 {
1187 switch(opcode) {
1188 case ISD::AND: Opc = Alpha::ANDi; break;
1189 case ISD::OR: Opc = Alpha::BISi; break;
1190 case ISD::XOR: Opc = Alpha::XORi; break;
1191 case ISD::SHL: Opc = Alpha::SLi; break;
1192 case ISD::SRL: Opc = Alpha::SRLi; break;
1193 case ISD::SRA: Opc = Alpha::SRAi; break;
1194 case ISD::MUL: Opc = Alpha::MULQi; break;
1195 };
1196 Tmp1 = SelectExpr(N.getOperand(0));
1197 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1198 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1199 } else {
1200 switch(opcode) {
1201 case ISD::AND: Opc = Alpha::AND; break;
1202 case ISD::OR: Opc = Alpha::BIS; break;
1203 case ISD::XOR: Opc = Alpha::XOR; break;
1204 case ISD::SHL: Opc = Alpha::SL; break;
1205 case ISD::SRL: Opc = Alpha::SRL; break;
1206 case ISD::SRA: Opc = Alpha::SRA; break;
1207 case ISD::MUL: Opc = Alpha::MULQ; break;
1208 };
1209 Tmp1 = SelectExpr(N.getOperand(0));
1210 Tmp2 = SelectExpr(N.getOperand(1));
1211 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1212 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001213 return Result;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001214
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001215 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001216 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001217 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001218 bool isAdd = opcode == ISD::ADD;
1219
1220 //FIXME: first check for Scaled Adds and Subs!
1221 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001222 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001223 { //Normal imm add/sub
1224 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1225 Tmp1 = SelectExpr(N.getOperand(0));
1226 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1227 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1228 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001229 else if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001230 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001231 { //LDA //FIXME: expand the above condition a bit
1232 Tmp1 = SelectExpr(N.getOperand(0));
1233 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1234 if (!isAdd)
1235 Tmp2 = -Tmp2;
1236 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
1237 } else {
1238 //Normal add/sub
1239 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1240 Tmp1 = SelectExpr(N.getOperand(0));
1241 Tmp2 = SelectExpr(N.getOperand(1));
1242 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1243 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001244 return Result;
1245 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001246
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001247 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00001248 case ISD::SREM:
1249 case ISD::SDIV:
1250 case ISD::UDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001251 //FIXME: alpha really doesn't support any of these operations,
1252 // the ops are expanded into special library calls with
1253 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001254 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00001255 switch(opcode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001256 case ISD::UREM: AlphaLowering.restoreGP(BB); Opc = Alpha::REMQU; break;
1257 case ISD::SREM: AlphaLowering.restoreGP(BB); Opc = Alpha::REMQ; break;
1258 case ISD::UDIV: AlphaLowering.restoreGP(BB); Opc = Alpha::DIVQU; break;
1259 case ISD::SDIV: AlphaLowering.restoreGP(BB); Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001260 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001261 Tmp1 = SelectExpr(N.getOperand(0));
1262 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth02981182005-01-26 01:24:38 +00001263 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001264 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001265
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001266 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001267 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001268 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001269 assert (DestType == MVT::i64 && "only quads can be loaded to");
1270 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001271 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001272 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1273
1274 //The hard way:
1275 // Spill the integer to memory and reload it from there.
1276 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1277 MachineFunction *F = BB->getParent();
1278 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
1279
1280 //CVTTQ STT LDQ
1281 //CVTST CVTTQ STT LDQ
1282 if (SrcType == MVT::f32)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001283 {
1284 Tmp2 = MakeReg(MVT::f64);
1285 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1286 Tmp1 = Tmp2;
1287 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001288 Tmp2 = MakeReg(MVT::f64);
1289 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
1290 BuildMI(BB, Alpha::STT, 3).addReg(Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1291 BuildMI(BB, Alpha::LDQ, 2, Result).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1292
1293 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001294 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001295
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001296 // // case ISD::FP_TO_UINT:
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001297
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001298 case ISD::SELECT:
1299 {
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001300 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001301 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1302 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001303 // Get the condition into the zero flag.
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001304 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001305 return Result;
1306 }
1307
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001308 case ISD::Constant:
1309 {
Andrew Lenharth22d5a412005-02-02 00:51:15 +00001310 unsigned long val = cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001311 if (val < 32000 && (long)val > -32000)
1312 BuildMI(BB, Alpha::LOAD_IMM, 1, Result).addImm(val);
1313 else {
1314 MachineConstantPool *CP = BB->getParent()->getConstantPool();
1315 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
1316 unsigned CPI = CP->getConstantPoolIndex(C);
1317 AlphaLowering.restoreGP(BB);
1318 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
1319 }
1320 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001321 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001322 }
1323
1324 return 0;
1325}
1326
1327void ISel::Select(SDOperand N) {
1328 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00001329 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001330
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001331 // FIXME: Disable for our current expansion model!
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001332 if (/*!N->hasOneUse() &&*/ !ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001333 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001334
1335 SDNode *Node = N.Val;
Andrew Lenharth760270d2005-02-07 23:02:23 +00001336
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001337
Andrew Lenharth760270d2005-02-07 23:02:23 +00001338 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001339
1340 default:
1341 Node->dump(); std::cerr << "\n";
1342 assert(0 && "Node not handled yet!");
1343
1344 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00001345 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001346 return;
1347 }
1348
1349 case ISD::BR: {
1350 MachineBasicBlock *Dest =
1351 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1352
1353 Select(N.getOperand(0));
1354 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
1355 return;
1356 }
1357
1358 case ISD::ImplicitDef:
1359 Select(N.getOperand(0));
1360 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
1361 return;
1362
1363 case ISD::EntryToken: return; // Noop
1364
1365 case ISD::TokenFactor:
1366 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1367 Select(Node->getOperand(i));
1368
1369 //N.Val->dump(); std::cerr << "\n";
1370 //assert(0 && "Node not handled yet!");
1371
1372 return;
1373
1374 case ISD::CopyToReg:
1375 Select(N.getOperand(0));
1376 Tmp1 = SelectExpr(N.getOperand(1));
1377 Tmp2 = cast<RegSDNode>(N)->getReg();
1378
1379 if (Tmp1 != Tmp2) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001380 if (N.getOperand(1).getValueType() == MVT::f64 ||
1381 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00001382 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1383 else
1384 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001385 }
1386 return;
1387
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001388 case ISD::RET:
1389 switch (N.getNumOperands()) {
1390 default:
1391 std::cerr << N.getNumOperands() << "\n";
1392 for (unsigned i = 0; i < N.getNumOperands(); ++i)
1393 std::cerr << N.getOperand(i).getValueType() << "\n";
1394 Node->dump();
1395 assert(0 && "Unknown return instruction!");
1396 case 2:
1397 Select(N.getOperand(0));
1398 Tmp1 = SelectExpr(N.getOperand(1));
1399 switch (N.getOperand(1).getValueType()) {
1400 default: Node->dump();
1401 assert(0 && "All other types should have been promoted!!");
1402 case MVT::f64:
1403 case MVT::f32:
1404 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1405 break;
1406 case MVT::i32:
1407 case MVT::i64:
1408 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1409 break;
1410 }
1411 break;
1412 case 1:
1413 Select(N.getOperand(0));
1414 break;
1415 }
1416 //Tmp2 = AlphaLowering.getRetAddr();
1417 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
1418 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
1419 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001420
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001421 case ISD::TRUNCSTORE:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001422 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001423 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001424 SDOperand Chain = N.getOperand(0);
1425 SDOperand Value = N.getOperand(1);
1426 SDOperand Address = N.getOperand(2);
1427 Select(Chain);
1428
1429 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00001430
1431 if (opcode == ISD::STORE) {
1432 switch(Value.getValueType()) {
1433 default: assert(0 && "unknown Type in store");
1434 case MVT::i64: Opc = Alpha::STQ; break;
1435 case MVT::f64: Opc = Alpha::STT; break;
1436 case MVT::f32: Opc = Alpha::STS; break;
1437 }
1438 } else { //ISD::TRUNCSTORE
1439 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1440 default: assert(0 && "unknown Type in store");
1441 case MVT::i1: //FIXME: DAG does not promote this load
1442 case MVT::i8: Opc = Alpha::STB; break;
1443 case MVT::i16: Opc = Alpha::STW; break;
1444 case MVT::i32: Opc = Alpha::STL; break;
1445 }
Andrew Lenharth65838902005-02-06 16:22:15 +00001446 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00001447
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001448 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001449 {
1450 AlphaLowering.restoreGP(BB);
1451 Opc = GetSymVersion(Opc);
1452 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1453 }
Andrew Lenharth05380342005-02-07 05:07:00 +00001454 else if(Address.getOpcode() == ISD::FrameIndex)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001455 {
1456 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1457 BuildMI(BB, Opc, 3).addReg(Tmp1).addFrameIndex(Tmp2).addReg(Alpha::F31);
1458 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001459 else
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001460 {
1461 long offset;
1462 SelectAddr(Address, Tmp2, offset);
1463 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1464 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001465 return;
1466 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001467
1468 case ISD::EXTLOAD:
1469 case ISD::SEXTLOAD:
1470 case ISD::ZEXTLOAD:
1471 case ISD::LOAD:
1472 case ISD::CopyFromReg:
1473 case ISD::CALL:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001474 // case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001475 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001476 SelectExpr(N);
1477 return;
1478
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001479 case ISD::ADJCALLSTACKDOWN:
1480 case ISD::ADJCALLSTACKUP:
1481 Select(N.getOperand(0));
1482 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1483
1484 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? Alpha::ADJUSTSTACKDOWN :
1485 Alpha::ADJUSTSTACKUP;
1486 BuildMI(BB, Opc, 1).addImm(Tmp1);
1487 return;
1488 }
1489 assert(0 && "Should not be reached!");
1490}
1491
1492
1493/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1494/// into a machine code representation using pattern matching and a machine
1495/// description file.
1496///
1497FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
1498 return new ISel(TM);
1499}