blob: 6c3614d8d37e00a071b1bf1b1251df6a6587a8df [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 Lenharth63f2ab22005-02-10 06:25:22 +000059 //what is the sign expansion of 1? 1 or -1?
60 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Andrew Lenharth02981182005-01-26 01:24:38 +000061
Andrew Lenharth9818c052005-02-05 13:19:12 +000062 setOperationAction(ISD::SREM , MVT::f32 , Expand);
63 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +000064
Andrew Lenharth8d163d22005-02-02 05:49:42 +000065 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
Andrew Lenharth9818c052005-02-05 13:19:12 +000066 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
67 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
68
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000069 computeRegisterProperties();
Andrew Lenharth304d0f32005-01-22 23:41:55 +000070
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000071 addLegalFPImmediate(+0.0); //F31
72 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +000073 }
74
75 /// LowerArguments - This hook must be implemented to indicate how we should
76 /// lower the arguments for the specified function, into the specified DAG.
77 virtual std::vector<SDOperand>
78 LowerArguments(Function &F, SelectionDAG &DAG);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000079
Andrew Lenharth304d0f32005-01-22 23:41:55 +000080 /// LowerCallTo - This hook lowers an abstract call to a function into an
81 /// actual call.
82 virtual std::pair<SDOperand, SDOperand>
83 LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
84 ArgListTy &Args, SelectionDAG &DAG);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000085
Andrew Lenharth304d0f32005-01-22 23:41:55 +000086 virtual std::pair<SDOperand, SDOperand>
87 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000088
Andrew Lenharth304d0f32005-01-22 23:41:55 +000089 virtual std::pair<SDOperand,SDOperand>
90 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
91 const Type *ArgTy, SelectionDAG &DAG);
92
93 virtual std::pair<SDOperand, SDOperand>
94 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
95 SelectionDAG &DAG);
96
97 void restoreGP(MachineBasicBlock* BB)
98 {
99 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
100 }
101 };
102}
103
104//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
105
106//For now, just use variable size stack frame format
107
108//In a standard call, the first six items are passed in registers $16
109//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
110//of argument-to-register correspondence.) The remaining items are
111//collected in a memory argument list that is a naturally aligned
112//array of quadwords. In a standard call, this list, if present, must
113//be passed at 0(SP).
114//7 ... n 0(SP) ... (n-7)*8(SP)
115
116std::vector<SDOperand>
117AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
118{
119 std::vector<SDOperand> ArgValues;
120
121 // //#define FP $15
122 // //#define RA $26
123 // //#define PV $27
124 // //#define GP $29
125 // //#define SP $30
126
127 // assert(0 && "TODO");
128 MachineFunction &MF = DAG.getMachineFunction();
Andrew Lenharth05380342005-02-07 05:07:00 +0000129 MachineFrameInfo*MFI = MF.getFrameInfo();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000130
131 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
132 MachineBasicBlock& BB = MF.front();
133
134 //Handle the return address
135 //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
136
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000137 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
138 Alpha::R19, Alpha::R20, Alpha::R21};
139 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
140 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000141 unsigned argVreg[6];
142 unsigned argPreg[6];
143 unsigned argOpc[6];
144
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000145 int count = 0;
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000146
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000147 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000148 {
149 SDOperand newroot, argt;
150 if (count < 6) {
151 switch (getValueType(I->getType())) {
152 default:
153 std::cerr << "Unknown Type " << getValueType(I->getType()) << "\n";
154 abort();
155 case MVT::f64:
156 case MVT::f32:
157 BuildMI(&BB, Alpha::IDEF, 0, args_float[count]);
158 argVreg[count] =
159 MF.getSSARegMap()->createVirtualRegister(
160 getRegClassFor(getValueType(I->getType())));
161 argPreg[count] = args_float[count];
162 argOpc[count] = Alpha::CPYS;
163 argt = newroot = DAG.getCopyFromReg(argVreg[count],
164 getValueType(I->getType()),
165 DAG.getRoot());
166 break;
167 case MVT::i1:
168 case MVT::i8:
169 case MVT::i16:
170 case MVT::i32:
171 case MVT::i64:
172 BuildMI(&BB, Alpha::IDEF, 0, args_int[count]);
173 argVreg[count] =
174 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
175 argPreg[count] = args_int[count];
176 argOpc[count] = Alpha::BIS;
177 argt = newroot =
178 DAG.getCopyFromReg(argVreg[count], MVT::i64, DAG.getRoot());
179 if (getValueType(I->getType()) != MVT::i64)
180 argt =
181 DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()), newroot);
182 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000183 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000184 ++count;
185 } else { //more args
186 // Create the frame index object for this incoming parameter...
187 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
188
189 // Create the SelectionDAG nodes corresponding to a load
190 //from this parameter
191 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
192 argt = newroot = DAG.getLoad(getValueType(I->getType()),
193 DAG.getEntryNode(), FIN);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000194 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000195 DAG.setRoot(newroot.getValue(1));
196 ArgValues.push_back(argt);
197 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000198
199 BuildMI(&BB, Alpha::IDEF, 0, Alpha::R29);
200 BuildMI(&BB, Alpha::BIS, 2, GP).addReg(Alpha::R29).addReg(Alpha::R29);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000201 for (int i = 0; i < count; ++i) {
202 if (argPreg[i] == Alpha::F16 || argPreg[i] == Alpha::F17 ||
203 argPreg[i] == Alpha::F18 || argPreg[i] == Alpha::F19 ||
204 argPreg[i] == Alpha::F20 || argPreg[i] == Alpha::F21)
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000205 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000206 assert(argOpc[i] == Alpha::CPYS && "Using BIS for a float??");
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000207 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000208 BuildMI(&BB, argOpc[i], 2,
209 argVreg[i]).addReg(argPreg[i]).addReg(argPreg[i]);
210 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000211
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000212 return ArgValues;
213}
214
215std::pair<SDOperand, SDOperand>
216AlphaTargetLowering::LowerCallTo(SDOperand Chain,
217 const Type *RetTy, SDOperand Callee,
218 ArgListTy &Args, SelectionDAG &DAG) {
219 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000220 if (Args.size() > 6)
221 NumBytes = (Args.size() - 6) * 8;
222
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000223 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
224 DAG.getConstant(NumBytes, getPointerTy()));
225 std::vector<SDOperand> args_to_use;
226 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000227 {
228 switch (getValueType(Args[i].second)) {
229 default: assert(0 && "Unexpected ValueType for argument!");
230 case MVT::i1:
231 case MVT::i8:
232 case MVT::i16:
233 case MVT::i32:
234 // Promote the integer to 64 bits. If the input type is signed use a
235 // sign extend, otherwise use a zero extend.
236 if (Args[i].second->isSigned())
237 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
238 else
239 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
240 break;
241 case MVT::i64:
242 case MVT::f64:
243 case MVT::f32:
244 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000245 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000246 args_to_use.push_back(Args[i].first);
247 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000248
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000249 std::vector<MVT::ValueType> RetVals;
250 MVT::ValueType RetTyVT = getValueType(RetTy);
251 if (RetTyVT != MVT::isVoid)
252 RetVals.push_back(RetTyVT);
253 RetVals.push_back(MVT::Other);
254
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000255 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
256 Chain, Callee, args_to_use), 0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000257 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
258 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
259 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000260 return std::make_pair(TheCall, Chain);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000261}
262
263std::pair<SDOperand, SDOperand>
264AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
265 //vastart just returns the address of the VarArgsFrameIndex slot.
266 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
267}
268
269std::pair<SDOperand,SDOperand> AlphaTargetLowering::
270LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000271 const Type *ArgTy, SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000272 abort();
273}
274
275
276std::pair<SDOperand, SDOperand> AlphaTargetLowering::
277LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
278 SelectionDAG &DAG) {
279 abort();
280}
281
282
283
284
285
286namespace {
287
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000288//===--------------------------------------------------------------------===//
289/// ISel - Alpha specific code to select Alpha machine instructions for
290/// SelectionDAG operations.
291//===--------------------------------------------------------------------===//
292class ISel : public SelectionDAGISel {
293
294 /// AlphaLowering - This object fully describes how to lower LLVM code to an
295 /// Alpha-specific SelectionDAG.
296 AlphaTargetLowering AlphaLowering;
297
298
299 /// ExprMap - As shared expressions are codegen'd, we keep track of which
300 /// vreg the value is produced in, so we only emit one copy of each compiled
301 /// tree.
302 static const unsigned notIn = (unsigned)(-1);
303 std::map<SDOperand, unsigned> ExprMap;
304
305 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
306 std::map<SDOperand, unsigned> CCInvMap;
307
308public:
309 ISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM)
310 {}
311
312 /// InstructionSelectBasicBlock - This callback is invoked by
313 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
314 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
315 // Codegen the basic block.
316 Select(DAG.getRoot());
317
318 // Clear state used for selection.
319 ExprMap.clear();
320 CCInvMap.clear();
321 }
322
323 unsigned SelectExpr(SDOperand N);
324 unsigned SelectExprFP(SDOperand N, unsigned Result);
325 void Select(SDOperand N);
326
327 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
328 void SelectBranchCC(SDOperand N);
329};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000330}
331
Andrew Lenharth65838902005-02-06 16:22:15 +0000332static unsigned GetSymVersion(unsigned opcode)
333{
334 switch (opcode) {
335 default: assert(0 && "unknown load or store"); return 0;
336 case Alpha::LDQ: return Alpha::LDQ_SYM;
337 case Alpha::LDS: return Alpha::LDS_SYM;
338 case Alpha::LDT: return Alpha::LDT_SYM;
339 case Alpha::LDL: return Alpha::LDL_SYM;
340 case Alpha::LDBU: return Alpha::LDBU_SYM;
341 case Alpha::LDWU: return Alpha::LDWU_SYM;
342 case Alpha::LDW: return Alpha::LDW_SYM;
343 case Alpha::LDB: return Alpha::LDB_SYM;
344 case Alpha::STQ: return Alpha::STQ_SYM;
345 case Alpha::STS: return Alpha::STS_SYM;
346 case Alpha::STT: return Alpha::STT_SYM;
347 case Alpha::STL: return Alpha::STL_SYM;
348 case Alpha::STW: return Alpha::STW_SYM;
349 case Alpha::STB: return Alpha::STB_SYM;
350 }
351}
352
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000353//Check to see if the load is a constant offset from a base register
354void ISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
355{
356 unsigned opcode = N.getOpcode();
357 if (opcode == ISD::ADD) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000358 if(N.getOperand(1).getOpcode() == ISD::Constant &&
359 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
360 { //Normal imm add
361 Reg = SelectExpr(N.getOperand(0));
362 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
363 return;
364 }
365 else if(N.getOperand(0).getOpcode() == ISD::Constant &&
366 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 32767)
367 {
368 Reg = SelectExpr(N.getOperand(1));
369 offset = cast<ConstantSDNode>(N.getOperand(0))->getValue();
370 return;
371 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000372 }
373 Reg = SelectExpr(N);
374 offset = 0;
375 return;
376}
377
Andrew Lenharth445171a2005-02-08 00:40:03 +0000378void ISel::SelectBranchCC(SDOperand N)
379{
380 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000381 MachineBasicBlock *Dest =
382 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
383 unsigned Opc = Alpha::WTF;
384
Andrew Lenharth445171a2005-02-08 00:40:03 +0000385 Select(N.getOperand(0)); //chain
386 SDOperand CC = N.getOperand(1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000387
Andrew Lenharth445171a2005-02-08 00:40:03 +0000388 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000389 {
390 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
391 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
392 //Dropping the CC is only useful if we are comparing to 0
393 bool isZero0 = false;
394 bool isZero1 = false;
395 bool isNE = false;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000396
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000397 if(SetCC->getOperand(0).getOpcode() == ISD::Constant &&
398 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0)
399 isZero0 = true;
400 if(SetCC->getOperand(1).getOpcode() == ISD::Constant &&
401 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0)
402 isZero1 = true;
403 if(SetCC->getCondition() == ISD::SETNE)
404 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000405
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000406 if (isZero0) {
Andrew Lenharth445171a2005-02-08 00:40:03 +0000407 switch (SetCC->getCondition()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000408 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
409 case ISD::SETEQ: Opc = Alpha::BEQ; break;
410 case ISD::SETLT: Opc = Alpha::BGT; break;
411 case ISD::SETLE: Opc = Alpha::BGE; break;
412 case ISD::SETGT: Opc = Alpha::BLT; break;
413 case ISD::SETGE: Opc = Alpha::BLE; break;
414 case ISD::SETULT: Opc = Alpha::BNE; break;
415 case ISD::SETUGT: assert(0 && "0 > (unsigned) x is never true"); break;
416 case ISD::SETULE: assert(0 && "0 <= (unsigned) x is always true"); break;
417 case ISD::SETUGE: Opc = Alpha::BEQ; break; //Technically you could have this CC
418 case ISD::SETNE: Opc = Alpha::BNE; break;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000419 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000420 unsigned Tmp1 = SelectExpr(SetCC->getOperand(1));
421 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
422 return;
423 } else if (isZero1) {
424 switch (SetCC->getCondition()) {
425 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
426 case ISD::SETEQ: Opc = Alpha::BEQ; break;
427 case ISD::SETLT: Opc = Alpha::BLT; break;
428 case ISD::SETLE: Opc = Alpha::BLE; break;
429 case ISD::SETGT: Opc = Alpha::BGT; break;
430 case ISD::SETGE: Opc = Alpha::BGE; break;
431 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
432 case ISD::SETUGT: Opc = Alpha::BNE; break;
433 case ISD::SETULE: Opc = Alpha::BEQ; break; //Technically you could have this CC
434 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
435 case ISD::SETNE: Opc = Alpha::BNE; break;
436 }
437 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
438 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
439 return;
440 } else {
441 unsigned Tmp1 = SelectExpr(CC);
442 if (isNE)
443 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
444 else
445 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000446 return;
447 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000448 } else { //FP
449 //Any comparison between 2 values should be codegened as an folded branch, as moving
450 //CC to the integer register is very expensive
451 //for a cmp b: c = a - b;
452 //a = b: c = 0
453 //a < b: c < 0
454 //a > b: c > 0
455 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
456 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
457 unsigned Tmp3 = MakeReg(MVT::f64);
458 BuildMI(BB, Alpha::SUBT, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
459
460 switch (SetCC->getCondition()) {
461 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
462 case ISD::SETEQ: Opc = Alpha::FBEQ; break;
463 case ISD::SETLT: Opc = Alpha::FBLT; break;
464 case ISD::SETLE: Opc = Alpha::FBLE; break;
465 case ISD::SETGT: Opc = Alpha::FBGT; break;
466 case ISD::SETGE: Opc = Alpha::FBGE; break;
467 case ISD::SETNE: Opc = Alpha::FBNE; break;
468 }
469 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000470 return;
471 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000472 abort(); //Should never be reached
473 } else {
474 //Giveup and do the stupid thing
475 unsigned Tmp1 = SelectExpr(CC);
476 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
477 return;
478 }
Andrew Lenharth445171a2005-02-08 00:40:03 +0000479 abort(); //Should never be reached
480}
481
Andrew Lenharth40831c52005-01-28 06:57:18 +0000482unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
483{
484 unsigned Tmp1, Tmp2, Tmp3;
485 unsigned Opc = 0;
486 SDNode *Node = N.Val;
487 MVT::ValueType DestType = N.getValueType();
488 unsigned opcode = N.getOpcode();
489
490 switch (opcode) {
491 default:
492 Node->dump();
493 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +0000494
Andrew Lenharth9818c052005-02-05 13:19:12 +0000495 case ISD::SELECT:
496 {
497 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
498 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
499 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000500
501
502 // Spill the cond to memory and reload it from there.
503 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
504 MachineFunction *F = BB->getParent();
505 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
506 unsigned Tmp4 = MakeReg(MVT::f64);
507 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addFrameIndex(FrameIdx).addReg(Alpha::F31);
508 BuildMI(BB, Alpha::LDT, 2, Tmp4).addFrameIndex(FrameIdx).addReg(Alpha::F31);
509 //now ideally, we don't have to do anything to the flag...
Andrew Lenharth9818c052005-02-05 13:19:12 +0000510 // Get the condition into the zero flag.
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000511 BuildMI(BB, Alpha::FCMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp4);
Andrew Lenharth9818c052005-02-05 13:19:12 +0000512 return Result;
513 }
514
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000515 case ISD::FP_ROUND:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000516 assert (DestType == MVT::f32 &&
517 N.getOperand(0).getValueType() == MVT::f64 &&
518 "only f64 to f32 conversion supported here");
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000519 Tmp1 = SelectExpr(N.getOperand(0));
520 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
521 return Result;
522
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000523 case ISD::FP_EXTEND:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000524 assert (DestType == MVT::f64 &&
525 N.getOperand(0).getValueType() == MVT::f32 &&
526 "only f32 to f64 conversion supported here");
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000527 Tmp1 = SelectExpr(N.getOperand(0));
528 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
529 return Result;
530
Andrew Lenharth2c594352005-01-29 15:42:07 +0000531 case ISD::CopyFromReg:
532 {
533 // Make sure we generate both values.
534 if (Result != notIn)
535 ExprMap[N.getValue(1)] = notIn; // Generate the token
536 else
537 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
538
539 SDOperand Chain = N.getOperand(0);
540
541 Select(Chain);
542 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
543 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
544 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
545 return Result;
546 }
547
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000548 case ISD::LOAD:
549 {
550 // Make sure we generate both values.
551 if (Result != notIn)
552 ExprMap[N.getValue(1)] = notIn; // Generate the token
553 else
554 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000555
Andrew Lenharth29219162005-02-07 06:31:44 +0000556 DestType = N.getValue(0).getValueType();
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000557
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000558 SDOperand Chain = N.getOperand(0);
559 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000560 Select(Chain);
Andrew Lenharth65838902005-02-06 16:22:15 +0000561 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
562
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000563 if (Address.getOpcode() == ISD::GlobalAddress) {
564 AlphaLowering.restoreGP(BB);
565 Opc = GetSymVersion(Opc);
566 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
567 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000568 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000569 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000570 Opc = GetSymVersion(Opc);
Andrew Lenharth97127a12005-02-05 17:41:39 +0000571 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000572 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000573 else if(Address.getOpcode() == ISD::FrameIndex) {
574 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
575 BuildMI(BB, Opc, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
576 } else {
577 long offset;
578 SelectAddr(Address, Tmp1, offset);
579 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
580 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000581 return Result;
582 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000583 case ISD::ConstantFP:
584 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
585 if (CN->isExactlyValue(+0.0)) {
586 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000587 } else if ( CN->isExactlyValue(-0.0)) {
588 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +0000589 } else {
590 abort();
591 }
592 }
593 return Result;
594
595 case ISD::MUL:
596 case ISD::ADD:
597 case ISD::SUB:
598 case ISD::SDIV:
599 switch( opcode ) {
600 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
601 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
602 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
603 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
604 };
605 Tmp1 = SelectExpr(N.getOperand(0));
606 Tmp2 = SelectExpr(N.getOperand(1));
607 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
608 return Result;
609
Andrew Lenharth2c594352005-01-29 15:42:07 +0000610 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000611 {
612 //include a conversion sequence for float loads to double
613 if (Result != notIn)
614 ExprMap[N.getValue(1)] = notIn; // Generate the token
615 else
616 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
617
Andrew Lenhartha549deb2005-02-07 05:33:15 +0000618 Tmp1 = MakeReg(MVT::f32);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000619
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000620 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
621 "EXTLOAD not from f32");
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000622 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
623
624 SDOperand Chain = N.getOperand(0);
625 SDOperand Address = N.getOperand(1);
626 Select(Chain);
627
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000628 if (Address.getOpcode() == ISD::GlobalAddress) {
629 AlphaLowering.restoreGP(BB);
630 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
631 }
632 else if (ConstantPoolSDNode *CP =
633 dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
634 {
635 AlphaLowering.restoreGP(BB);
636 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addConstantPoolIndex(CP->getIndex());
637 }
638 else if(Address.getOpcode() == ISD::FrameIndex) {
639 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
640 BuildMI(BB, Alpha::LDS, 2, Tmp1).addFrameIndex(Tmp2).addReg(Alpha::F31);
641 } else {
642 long offset;
643 SelectAddr(Address, Tmp2, offset);
644 BuildMI(BB, Alpha::LDS, 1, Tmp1).addImm(offset).addReg(Tmp2);
645 }
Andrew Lenharth29219162005-02-07 06:31:44 +0000646 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000647 return Result;
648 }
Andrew Lenharth2c594352005-01-29 15:42:07 +0000649
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000650 case ISD::UINT_TO_FP:
651 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000652 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000653 assert (N.getOperand(0).getValueType() == MVT::i64
654 && "only quads can be loaded from");
Andrew Lenharth40831c52005-01-28 06:57:18 +0000655 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +0000656 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000657
658 //The hard way:
659 // Spill the integer to memory and reload it from there.
660 unsigned Size = MVT::getSizeInBits(MVT::i64)/8;
661 MachineFunction *F = BB->getParent();
662 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
663
Andrew Lenharth7efadce2005-01-31 01:44:26 +0000664 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addFrameIndex(FrameIdx).addReg(Alpha::F31);
665 BuildMI(BB, Alpha::LDT, 2, Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
666 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
667 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000668
669 //The easy way: doesn't work
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000670 // //so these instructions are not supported on ev56
671 // Opc = DestType == MVT::f64 ? Alpha::ITOFT : Alpha::ITOFS;
672 // BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
673 // Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
674 // BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000675
Andrew Lenharth40831c52005-01-28 06:57:18 +0000676 return Result;
677 }
678 }
679 assert(0 && "should not get here");
680 return 0;
681}
682
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000683unsigned ISel::SelectExpr(SDOperand N) {
684 unsigned Result;
685 unsigned Tmp1, Tmp2, Tmp3;
686 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000687 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000688
689 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000690 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000691
692 unsigned &Reg = ExprMap[N];
693 if (Reg) return Reg;
694
695 if (N.getOpcode() != ISD::CALL)
696 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000697 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000698 else {
699 // If this is a call instruction, make sure to prepare ALL of the result
700 // values as well as the chain.
701 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000702 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000703 else {
704 Result = MakeReg(Node->getValueType(0));
705 ExprMap[N.getValue(0)] = Result;
706 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
707 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000708 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000709 }
710 }
711
Andrew Lenharth22088bb2005-02-02 15:05:33 +0000712 if (DestType == MVT::f64 || DestType == MVT::f32 ||
Andrew Lenharth06342c32005-02-07 06:21:37 +0000713 (
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000714 (opcode == ISD::LOAD || opcode == ISD::CopyFromReg ||
715 opcode == ISD::EXTLOAD) &&
716 (N.getValue(0).getValueType() == MVT::f32 ||
717 N.getValue(0).getValueType() == MVT::f64)
Andrew Lenharth06342c32005-02-07 06:21:37 +0000718 )
719 )
Andrew Lenharth40831c52005-01-28 06:57:18 +0000720 return SelectExprFP(N, Result);
721
722 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000723 default:
724 Node->dump();
725 assert(0 && "Node not handled!\n");
726
Andrew Lenharth2c594352005-01-29 15:42:07 +0000727 case ISD::ConstantPool:
728 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
729 AlphaLowering.restoreGP(BB);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000730 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000731 return Result;
732
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000733 case ISD::FrameIndex:
734 Tmp1 = cast<FrameIndexSDNode>(N)->getIndex();
Andrew Lenharth684f2292005-01-30 00:35:27 +0000735 BuildMI(BB, Alpha::LDA, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000736 return Result;
737
738 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +0000739 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000740 case ISD::SEXTLOAD:
Andrew Lenhartha549deb2005-02-07 05:33:15 +0000741 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000742 {
743 // Make sure we generate both values.
744 if (Result != notIn)
745 ExprMap[N.getValue(1)] = notIn; // Generate the token
746 else
747 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000748
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000749 SDOperand Chain = N.getOperand(0);
750 SDOperand Address = N.getOperand(1);
751 Select(Chain);
752
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000753 assert(Node->getValueType(0) == MVT::i64 &&
754 "Unknown type to sign extend to.");
Andrew Lenharth03824012005-02-07 05:55:55 +0000755 if (opcode == ISD::LOAD)
756 Opc = Alpha::LDQ;
757 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000758 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
759 default: Node->dump(); assert(0 && "Bad sign extend!");
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000760 case MVT::i32: Opc = Alpha::LDL;
761 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
762 case MVT::i16: Opc = Alpha::LDWU;
763 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +0000764 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000765 case MVT::i8: Opc = Alpha::LDBU;
766 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000767 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000768
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000769 if (Address.getOpcode() == ISD::GlobalAddress) {
770 AlphaLowering.restoreGP(BB);
771 Opc = GetSymVersion(Opc);
772 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
773 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000774 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
775 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000776 Opc = GetSymVersion(Opc);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000777 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000778 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000779 else if(Address.getOpcode() == ISD::FrameIndex) {
780 Tmp1 = cast<FrameIndexSDNode>(Address)->getIndex();
781 BuildMI(BB, Opc, 2, Result).addFrameIndex(Tmp1).addReg(Alpha::F31);
782 } else {
783 long offset;
784 SelectAddr(Address, Tmp1, offset);
785 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
786 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000787 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000788 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000789
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000790 case ISD::GlobalAddress:
791 AlphaLowering.restoreGP(BB);
792 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
793 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
794 return Result;
795
796 case ISD::CALL:
797 {
798 Select(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000799
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000800 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000801 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000802
803 //grab the arguments
804 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000805 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000806 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000807 argvregs.push_back(SelectExpr(N.getOperand(i)));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000808
Andrew Lenharth684f2292005-01-30 00:35:27 +0000809 //in reg args
810 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000811 {
812 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
813 Alpha::R19, Alpha::R20, Alpha::R21};
814 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
815 Alpha::F19, Alpha::F20, Alpha::F21};
816 switch(N.getOperand(i+2).getValueType()) {
817 default:
818 Node->dump();
819 N.getOperand(i).Val->dump();
820 std::cerr << "Type for " << i << " is: " <<
821 N.getOperand(i+2).getValueType() << "\n";
822 assert(0 && "Unknown value type for call");
823 case MVT::i1:
824 case MVT::i8:
825 case MVT::i16:
826 case MVT::i32:
827 case MVT::i64:
828 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
829 break;
830 case MVT::f32:
831 case MVT::f64:
832 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
833 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000834 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000835 }
Andrew Lenharth684f2292005-01-30 00:35:27 +0000836 //in mem args
837 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000838 {
839 switch(N.getOperand(i+2).getValueType()) {
840 default:
841 Node->dump();
842 N.getOperand(i).Val->dump();
843 std::cerr << "Type for " << i << " is: " <<
844 N.getOperand(i+2).getValueType() << "\n";
845 assert(0 && "Unknown value type for call");
846 case MVT::i1:
847 case MVT::i8:
848 case MVT::i16:
849 case MVT::i32:
850 case MVT::i64:
851 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
852 break;
853 case MVT::f32:
854 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
855 break;
856 case MVT::f64:
857 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
858 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000859 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000860 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000861 //build the right kind of call
862 if (GlobalAddressSDNode *GASD =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000863 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000864 {
Andrew Lenharth3e315922005-02-10 20:10:38 +0000865 //if (GASD->getGlobal()->isExternal()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000866 //use safe calling convention
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000867 AlphaLowering.restoreGP(BB);
868 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal(),true);
Andrew Lenharth3e315922005-02-10 20:10:38 +0000869 //} else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000870 //use PC relative branch call
Andrew Lenharth3e315922005-02-10 20:10:38 +0000871 //BuildMI(BB, Alpha::BSR, 1, Alpha::R26).addGlobalAddress(GASD->getGlobal(),true);
872 //}
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000873 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000874 else if (ExternalSymbolSDNode *ESSDN =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000875 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000876 {
877 AlphaLowering.restoreGP(BB);
878 BuildMI(BB, Alpha::CALL, 0).addExternalSymbol(ESSDN->getSymbol(), true);
879 } else {
880 //no need to restore GP as we are doing an indirect call
881 Tmp1 = SelectExpr(N.getOperand(1));
882 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
883 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
884 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000885
886 //push the result into a virtual register
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000887
888 switch (Node->getValueType(0)) {
889 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000890 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000891 case MVT::i1:
892 case MVT::i8:
893 case MVT::i16:
894 case MVT::i32:
895 case MVT::i64:
896 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
897 break;
898 case MVT::f32:
899 case MVT::f64:
900 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
901 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000902 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000903 return Result+N.ResNo;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000904 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000905
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000906 case ISD::SIGN_EXTEND:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000907 abort();
908
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000909 case ISD::SIGN_EXTEND_INREG:
910 {
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000911 //Alpha has instructions for a bunch of signed 32 bit stuff
912 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000913 {
914 switch (N.getOperand(0).getOpcode()) {
915 case ISD::ADD:
916 case ISD::SUB:
917 case ISD::MUL:
918 {
919 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
920 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
921 //FIXME: first check for Scaled Adds and Subs!
922 if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
923 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
924 { //Normal imm add/sub
925 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
926 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
927 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
928 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000929 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000930 else
931 { //Normal add/sub
932 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULLi : Alpha::SUBL);
933 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
934 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
935 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
936 }
937 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000938 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000939 default: break; //Fall Though;
940 }
941 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000942 Tmp1 = SelectExpr(N.getOperand(0));
943 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000944 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000945 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000946 {
947 default:
948 Node->dump();
949 assert(0 && "Sign Extend InReg not there yet");
950 break;
951 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000952 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000953 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000954 break;
955 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000956 case MVT::i16:
957 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
958 break;
959 case MVT::i8:
960 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
961 break;
962 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000963 return Result;
964 }
965 case ISD::ZERO_EXTEND_INREG:
966 {
967 Tmp1 = SelectExpr(N.getOperand(0));
968 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000969 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000970 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000971 {
972 default:
973 Node->dump();
974 assert(0 && "Zero Extend InReg not there yet");
975 break;
976 case MVT::i32: Tmp2 = 0xf0; break;
977 case MVT::i16: Tmp2 = 0xfc; break;
978 case MVT::i8: Tmp2 = 0xfe; break;
979 case MVT::i1: //handle this one special
980 BuildMI(BB, Alpha::ANDi, 2, Result).addReg(Tmp1).addImm(1);
981 return Result;
982 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000983 BuildMI(BB, Alpha::ZAPi, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000984 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000985 }
986
987 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000988 {
989 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
990 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
991 bool isConst1 = false;
992 bool isConst2 = false;
993 int dir;
Andrew Lenharth9818c052005-02-05 13:19:12 +0000994
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000995 //Tmp1 = SelectExpr(N.getOperand(0));
996 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000997 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
998 isConst1 = true;
999 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001000 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1001 isConst2 = true;
1002
1003 switch (SetCC->getCondition()) {
1004 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1005 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001006 case ISD::SETLT:
1007 Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
1008 case ISD::SETLE:
1009 Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
1010 case ISD::SETGT:
1011 Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
1012 case ISD::SETGE:
1013 Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
1014 case ISD::SETULT:
1015 Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
1016 case ISD::SETUGT:
1017 Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
1018 case ISD::SETULE:
1019 Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
1020 case ISD::SETUGE:
1021 Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001022 case ISD::SETNE: {//Handle this one special
1023 //std::cerr << "Alpha does not have a setne.\n";
1024 //abort();
1025 Tmp1 = SelectExpr(N.getOperand(0));
1026 Tmp2 = SelectExpr(N.getOperand(1));
1027 Tmp3 = MakeReg(MVT::i64);
1028 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001029 //Remeber we have the Inv for this CC
1030 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001031 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001032 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001033 return Result;
1034 }
1035 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001036 if (dir == 1) {
1037 Tmp1 = SelectExpr(N.getOperand(0));
1038 if (isConst2) {
1039 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1040 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1041 } else {
1042 Tmp2 = SelectExpr(N.getOperand(1));
1043 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1044 }
1045 } else if (dir == 2) {
1046 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001047 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001048 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1049 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1050 } else {
1051 Tmp2 = SelectExpr(N.getOperand(0));
1052 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1053 }
1054 } else { //dir == 0
1055 if (isConst1) {
1056 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1057 Tmp2 = SelectExpr(N.getOperand(1));
1058 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
1059 } else if (isConst2) {
1060 Tmp1 = SelectExpr(N.getOperand(0));
1061 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1062 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1063 } else {
1064 Tmp1 = SelectExpr(N.getOperand(0));
1065 Tmp2 = SelectExpr(N.getOperand(1));
1066 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1067 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001068 }
1069 } else {
1070 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
1071 bool rev = false;
1072 bool inv = false;
1073
1074 switch (SetCC->getCondition()) {
1075 default: Node->dump(); assert(0 && "Unknown FP comparison!");
1076 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
1077 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
1078 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
1079 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
1080 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
1081 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
1082 }
1083
1084 Tmp1 = SelectExpr(N.getOperand(0));
1085 Tmp2 = SelectExpr(N.getOperand(1));
1086 //Can only compare doubles, and dag won't promote for me
1087 if (SetCC->getOperand(0).getValueType() == MVT::f32)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001088 {
1089 Tmp3 = MakeReg(MVT::f64);
1090 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
1091 Tmp1 = Tmp3;
1092 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001093 if (SetCC->getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001094 {
1095 Tmp3 = MakeReg(MVT::f64);
1096 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
1097 Tmp1 = Tmp2;
1098 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001099
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001100 if (rev) std::swap(Tmp1, Tmp2);
1101 Tmp3 = MakeReg(MVT::f64);
1102 //do the comparison
1103 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1104
1105 //now arrange for Result (int) to have a 1 or 0
1106
1107 // Spill the FP to memory and reload it from there.
1108 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1109 MachineFunction *F = BB->getParent();
1110 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
1111 unsigned Tmp4 = MakeReg(MVT::f64);
1112 BuildMI(BB, Alpha::CVTTQ, 1, Tmp4).addReg(Tmp3);
1113 BuildMI(BB, Alpha::STT, 3).addReg(Tmp4).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1114 unsigned Tmp5 = MakeReg(MVT::i64);
1115 BuildMI(BB, Alpha::LDQ, 2, Tmp5).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharth9818c052005-02-05 13:19:12 +00001116
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001117 //now, set result based on Tmp5
1118 //Set Tmp6 if fp cmp was false
1119 unsigned Tmp6 = MakeReg(MVT::i64);
1120 BuildMI(BB, Alpha::CMPEQ, 2, Tmp6).addReg(Tmp5).addReg(Alpha::R31);
1121 //and invert
1122 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp6).addReg(Alpha::R31);
1123
1124 }
1125 // else
1126 // {
1127 // Node->dump();
1128 // assert(0 && "Not a setcc in setcc");
1129 // }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001130 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001131 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001132 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001133
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001134 case ISD::CopyFromReg:
1135 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001136 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001137 if (Result != notIn)
1138 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001139 else
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001140 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth40831c52005-01-28 06:57:18 +00001141
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001142 SDOperand Chain = N.getOperand(0);
1143
1144 Select(Chain);
1145 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1146 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1147 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1148 return Result;
1149 }
1150
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001151 //Most of the plain arithmetic and logic share the same form, and the same
1152 //constant immediate test
1153 case ISD::AND:
1154 case ISD::OR:
1155 case ISD::XOR:
1156 case ISD::SHL:
1157 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001158 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001159 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001160 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1161 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001162 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001163 {
1164 switch(opcode) {
1165 case ISD::AND: Opc = Alpha::ANDi; break;
1166 case ISD::OR: Opc = Alpha::BISi; break;
1167 case ISD::XOR: Opc = Alpha::XORi; break;
1168 case ISD::SHL: Opc = Alpha::SLi; break;
1169 case ISD::SRL: Opc = Alpha::SRLi; break;
1170 case ISD::SRA: Opc = Alpha::SRAi; break;
1171 case ISD::MUL: Opc = Alpha::MULQi; break;
1172 };
1173 Tmp1 = SelectExpr(N.getOperand(0));
1174 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1175 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1176 } else {
1177 switch(opcode) {
1178 case ISD::AND: Opc = Alpha::AND; break;
1179 case ISD::OR: Opc = Alpha::BIS; break;
1180 case ISD::XOR: Opc = Alpha::XOR; break;
1181 case ISD::SHL: Opc = Alpha::SL; break;
1182 case ISD::SRL: Opc = Alpha::SRL; break;
1183 case ISD::SRA: Opc = Alpha::SRA; break;
1184 case ISD::MUL: Opc = Alpha::MULQ; break;
1185 };
1186 Tmp1 = SelectExpr(N.getOperand(0));
1187 Tmp2 = SelectExpr(N.getOperand(1));
1188 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1189 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001190 return Result;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001191
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001192 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001193 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001194 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001195 bool isAdd = opcode == ISD::ADD;
1196
1197 //FIXME: first check for Scaled Adds and Subs!
1198 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001199 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001200 { //Normal imm add/sub
1201 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1202 Tmp1 = SelectExpr(N.getOperand(0));
1203 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1204 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1205 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001206 else if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001207 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001208 { //LDA //FIXME: expand the above condition a bit
1209 Tmp1 = SelectExpr(N.getOperand(0));
1210 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1211 if (!isAdd)
1212 Tmp2 = -Tmp2;
1213 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
1214 } else {
1215 //Normal add/sub
1216 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1217 Tmp1 = SelectExpr(N.getOperand(0));
1218 Tmp2 = SelectExpr(N.getOperand(1));
1219 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1220 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001221 return Result;
1222 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001223
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001224 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00001225 case ISD::SREM:
1226 case ISD::SDIV:
1227 case ISD::UDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001228 //FIXME: alpha really doesn't support any of these operations,
1229 // the ops are expanded into special library calls with
1230 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001231 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00001232 switch(opcode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001233 case ISD::UREM: AlphaLowering.restoreGP(BB); Opc = Alpha::REMQU; break;
1234 case ISD::SREM: AlphaLowering.restoreGP(BB); Opc = Alpha::REMQ; break;
1235 case ISD::UDIV: AlphaLowering.restoreGP(BB); Opc = Alpha::DIVQU; break;
1236 case ISD::SDIV: AlphaLowering.restoreGP(BB); Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001237 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001238 Tmp1 = SelectExpr(N.getOperand(0));
1239 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth02981182005-01-26 01:24:38 +00001240 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001241 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001242
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001243 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001244 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001245 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001246 assert (DestType == MVT::i64 && "only quads can be loaded to");
1247 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001248 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001249 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1250
1251 //The hard way:
1252 // Spill the integer to memory and reload it from there.
1253 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1254 MachineFunction *F = BB->getParent();
1255 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
1256
1257 //CVTTQ STT LDQ
1258 //CVTST CVTTQ STT LDQ
1259 if (SrcType == MVT::f32)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001260 {
1261 Tmp2 = MakeReg(MVT::f64);
1262 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1263 Tmp1 = Tmp2;
1264 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001265 Tmp2 = MakeReg(MVT::f64);
1266 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
1267 BuildMI(BB, Alpha::STT, 3).addReg(Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1268 BuildMI(BB, Alpha::LDQ, 2, Result).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1269
1270 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001271 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001272
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001273 // // case ISD::FP_TO_UINT:
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001274
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001275 case ISD::SELECT:
1276 {
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001277 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001278 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1279 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001280 // Get the condition into the zero flag.
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001281 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001282 return Result;
1283 }
1284
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001285 case ISD::Constant:
1286 {
Andrew Lenharth22d5a412005-02-02 00:51:15 +00001287 unsigned long val = cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001288 if (val < 32000 && (long)val > -32000)
1289 BuildMI(BB, Alpha::LOAD_IMM, 1, Result).addImm(val);
1290 else {
1291 MachineConstantPool *CP = BB->getParent()->getConstantPool();
1292 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
1293 unsigned CPI = CP->getConstantPoolIndex(C);
1294 AlphaLowering.restoreGP(BB);
1295 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
1296 }
1297 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001298 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001299 }
1300
1301 return 0;
1302}
1303
1304void ISel::Select(SDOperand N) {
1305 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00001306 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001307
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001308 // FIXME: Disable for our current expansion model!
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001309 if (/*!N->hasOneUse() &&*/ !ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001310 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001311
1312 SDNode *Node = N.Val;
Andrew Lenharth760270d2005-02-07 23:02:23 +00001313
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001314
Andrew Lenharth760270d2005-02-07 23:02:23 +00001315 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001316
1317 default:
1318 Node->dump(); std::cerr << "\n";
1319 assert(0 && "Node not handled yet!");
1320
1321 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00001322 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001323 return;
1324 }
1325
1326 case ISD::BR: {
1327 MachineBasicBlock *Dest =
1328 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1329
1330 Select(N.getOperand(0));
1331 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
1332 return;
1333 }
1334
1335 case ISD::ImplicitDef:
1336 Select(N.getOperand(0));
1337 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
1338 return;
1339
1340 case ISD::EntryToken: return; // Noop
1341
1342 case ISD::TokenFactor:
1343 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1344 Select(Node->getOperand(i));
1345
1346 //N.Val->dump(); std::cerr << "\n";
1347 //assert(0 && "Node not handled yet!");
1348
1349 return;
1350
1351 case ISD::CopyToReg:
1352 Select(N.getOperand(0));
1353 Tmp1 = SelectExpr(N.getOperand(1));
1354 Tmp2 = cast<RegSDNode>(N)->getReg();
1355
1356 if (Tmp1 != Tmp2) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001357 if (N.getOperand(1).getValueType() == MVT::f64 ||
1358 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00001359 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1360 else
1361 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001362 }
1363 return;
1364
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001365 case ISD::RET:
1366 switch (N.getNumOperands()) {
1367 default:
1368 std::cerr << N.getNumOperands() << "\n";
1369 for (unsigned i = 0; i < N.getNumOperands(); ++i)
1370 std::cerr << N.getOperand(i).getValueType() << "\n";
1371 Node->dump();
1372 assert(0 && "Unknown return instruction!");
1373 case 2:
1374 Select(N.getOperand(0));
1375 Tmp1 = SelectExpr(N.getOperand(1));
1376 switch (N.getOperand(1).getValueType()) {
1377 default: Node->dump();
1378 assert(0 && "All other types should have been promoted!!");
1379 case MVT::f64:
1380 case MVT::f32:
1381 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1382 break;
1383 case MVT::i32:
1384 case MVT::i64:
1385 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1386 break;
1387 }
1388 break;
1389 case 1:
1390 Select(N.getOperand(0));
1391 break;
1392 }
1393 //Tmp2 = AlphaLowering.getRetAddr();
1394 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
1395 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
1396 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001397
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001398 case ISD::TRUNCSTORE:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001399 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001400 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001401 SDOperand Chain = N.getOperand(0);
1402 SDOperand Value = N.getOperand(1);
1403 SDOperand Address = N.getOperand(2);
1404 Select(Chain);
1405
1406 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00001407
1408 if (opcode == ISD::STORE) {
1409 switch(Value.getValueType()) {
1410 default: assert(0 && "unknown Type in store");
1411 case MVT::i64: Opc = Alpha::STQ; break;
1412 case MVT::f64: Opc = Alpha::STT; break;
1413 case MVT::f32: Opc = Alpha::STS; break;
1414 }
1415 } else { //ISD::TRUNCSTORE
1416 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1417 default: assert(0 && "unknown Type in store");
1418 case MVT::i1: //FIXME: DAG does not promote this load
1419 case MVT::i8: Opc = Alpha::STB; break;
1420 case MVT::i16: Opc = Alpha::STW; break;
1421 case MVT::i32: Opc = Alpha::STL; break;
1422 }
Andrew Lenharth65838902005-02-06 16:22:15 +00001423 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00001424
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001425 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001426 {
1427 AlphaLowering.restoreGP(BB);
1428 Opc = GetSymVersion(Opc);
1429 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1430 }
Andrew Lenharth05380342005-02-07 05:07:00 +00001431 else if(Address.getOpcode() == ISD::FrameIndex)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001432 {
1433 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
1434 BuildMI(BB, Opc, 3).addReg(Tmp1).addFrameIndex(Tmp2).addReg(Alpha::F31);
1435 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001436 else
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001437 {
1438 long offset;
1439 SelectAddr(Address, Tmp2, offset);
1440 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1441 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001442 return;
1443 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001444
1445 case ISD::EXTLOAD:
1446 case ISD::SEXTLOAD:
1447 case ISD::ZEXTLOAD:
1448 case ISD::LOAD:
1449 case ISD::CopyFromReg:
1450 case ISD::CALL:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001451 // case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001452 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001453 SelectExpr(N);
1454 return;
1455
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001456 case ISD::ADJCALLSTACKDOWN:
1457 case ISD::ADJCALLSTACKUP:
1458 Select(N.getOperand(0));
1459 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1460
1461 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? Alpha::ADJUSTSTACKDOWN :
1462 Alpha::ADJUSTSTACKUP;
1463 BuildMI(BB, Opc, 1).addImm(Tmp1);
1464 return;
1465 }
1466 assert(0 && "Should not be reached!");
1467}
1468
1469
1470/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1471/// into a machine code representation using pattern matching and a machine
1472/// description file.
1473///
1474FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
1475 return new ISel(TM);
1476}