blob: 7eaa3e4fc09d2b6e86db28f2f81b72d4fd08914f [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"
Andrew Lenharth032f2352005-02-22 21:59:48 +000029#include "llvm/Support/Debug.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000030#include <set>
Andrew Lenharth684f2292005-01-30 00:35:27 +000031#include <algorithm>
Andrew Lenharth304d0f32005-01-22 23:41:55 +000032using namespace llvm;
33
34//===----------------------------------------------------------------------===//
35// AlphaTargetLowering - Alpha Implementation of the TargetLowering interface
36namespace {
37 class AlphaTargetLowering : public TargetLowering {
38 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
39 unsigned GP; //GOT vreg
40 public:
41 AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
42 // Set up the TargetLowering object.
Andrew Lenharth3d65d312005-01-27 03:49:45 +000043 //I am having problems with shr n ubyte 1
Andrew Lenharth879ef222005-02-02 17:00:21 +000044 setShiftAmountType(MVT::i64);
45 setSetCCResultType(MVT::i64);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000046
Andrew Lenharth304d0f32005-01-22 23:41:55 +000047 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
48 addRegisterClass(MVT::f64, Alpha::FPRCRegisterClass);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000049 addRegisterClass(MVT::f32, Alpha::FPRCRegisterClass);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000050
Andrew Lenharthd2bb9602005-01-27 07:50:35 +000051 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
Andrew Lenharth33819132005-03-04 20:09:23 +000052 setOperationAction(ISD::EXTLOAD , MVT::f32 , Promote);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000053
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +000054 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000055 setOperationAction(ISD::ZEXTLOAD , MVT::i32 , Expand);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000056
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +000057 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000058 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
59 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
60
Andrew Lenharth9818c052005-02-05 13:19:12 +000061 setOperationAction(ISD::SREM , MVT::f32 , Expand);
62 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +000063
Andrew Lenharth8d163d22005-02-02 05:49:42 +000064 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
Andrew Lenharth9818c052005-02-05 13:19:12 +000065 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
66 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
67
Andrew Lenharth33819132005-03-04 20:09:23 +000068 //Doesn't work yet
Andrew Lenharth572af902005-02-14 05:41:43 +000069 setOperationAction(ISD::SETCC , MVT::f32, Promote);
70
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000071 computeRegisterProperties();
Andrew Lenharth304d0f32005-01-22 23:41:55 +000072
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000073 addLegalFPImmediate(+0.0); //F31
74 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +000075 }
76
77 /// LowerArguments - This hook must be implemented to indicate how we should
78 /// lower the arguments for the specified function, into the specified DAG.
79 virtual std::vector<SDOperand>
80 LowerArguments(Function &F, SelectionDAG &DAG);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000081
Andrew Lenharth304d0f32005-01-22 23:41:55 +000082 /// LowerCallTo - This hook lowers an abstract call to a function into an
83 /// actual call.
84 virtual std::pair<SDOperand, SDOperand>
85 LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
86 ArgListTy &Args, SelectionDAG &DAG);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000087
Andrew Lenharth304d0f32005-01-22 23:41:55 +000088 virtual std::pair<SDOperand, SDOperand>
89 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000090
Andrew Lenharth304d0f32005-01-22 23:41:55 +000091 virtual std::pair<SDOperand,SDOperand>
92 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
93 const Type *ArgTy, SelectionDAG &DAG);
94
95 virtual std::pair<SDOperand, SDOperand>
96 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
97 SelectionDAG &DAG);
98
99 void restoreGP(MachineBasicBlock* BB)
100 {
101 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
102 }
103 };
104}
105
106//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
107
108//For now, just use variable size stack frame format
109
110//In a standard call, the first six items are passed in registers $16
111//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
112//of argument-to-register correspondence.) The remaining items are
113//collected in a memory argument list that is a naturally aligned
114//array of quadwords. In a standard call, this list, if present, must
115//be passed at 0(SP).
116//7 ... n 0(SP) ... (n-7)*8(SP)
117
118std::vector<SDOperand>
119AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
120{
121 std::vector<SDOperand> ArgValues;
122
123 // //#define FP $15
124 // //#define RA $26
125 // //#define PV $27
126 // //#define GP $29
127 // //#define SP $30
128
129 // assert(0 && "TODO");
130 MachineFunction &MF = DAG.getMachineFunction();
Andrew Lenharth05380342005-02-07 05:07:00 +0000131 MachineFrameInfo*MFI = MF.getFrameInfo();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000132
133 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
134 MachineBasicBlock& BB = MF.front();
135
136 //Handle the return address
137 //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
138
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000139 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
140 Alpha::R19, Alpha::R20, Alpha::R21};
141 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
142 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000143 unsigned argVreg[6];
144 unsigned argPreg[6];
145 unsigned argOpc[6];
146
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000147 int count = 0;
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000148
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000149 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000150 {
151 SDOperand newroot, argt;
152 if (count < 6) {
153 switch (getValueType(I->getType())) {
154 default:
155 std::cerr << "Unknown Type " << getValueType(I->getType()) << "\n";
156 abort();
157 case MVT::f64:
158 case MVT::f32:
159 BuildMI(&BB, Alpha::IDEF, 0, args_float[count]);
160 argVreg[count] =
161 MF.getSSARegMap()->createVirtualRegister(
Andrew Lenharth032f2352005-02-22 21:59:48 +0000162 getRegClassFor(getValueType(I->getType())));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000163 argPreg[count] = args_float[count];
164 argOpc[count] = Alpha::CPYS;
165 argt = newroot = DAG.getCopyFromReg(argVreg[count],
166 getValueType(I->getType()),
167 DAG.getRoot());
168 break;
169 case MVT::i1:
170 case MVT::i8:
171 case MVT::i16:
172 case MVT::i32:
173 case MVT::i64:
174 BuildMI(&BB, Alpha::IDEF, 0, args_int[count]);
175 argVreg[count] =
176 MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
177 argPreg[count] = args_int[count];
178 argOpc[count] = Alpha::BIS;
179 argt = newroot =
180 DAG.getCopyFromReg(argVreg[count], MVT::i64, DAG.getRoot());
181 if (getValueType(I->getType()) != MVT::i64)
182 argt =
183 DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()), newroot);
184 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000185 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000186 } else { //more args
187 // Create the frame index object for this incoming parameter...
188 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
189
190 // Create the SelectionDAG nodes corresponding to a load
191 //from this parameter
192 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
193 argt = newroot = DAG.getLoad(getValueType(I->getType()),
194 DAG.getEntryNode(), FIN);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000195 }
Andrew Lenharth032f2352005-02-22 21:59:48 +0000196 ++count;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000197 DAG.setRoot(newroot.getValue(1));
198 ArgValues.push_back(argt);
199 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000200
201 BuildMI(&BB, Alpha::IDEF, 0, Alpha::R29);
202 BuildMI(&BB, Alpha::BIS, 2, GP).addReg(Alpha::R29).addReg(Alpha::R29);
Andrew Lenharth032f2352005-02-22 21:59:48 +0000203 for (int i = 0; i < count && i < 6; ++i) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000204 BuildMI(&BB, argOpc[i], 2,
205 argVreg[i]).addReg(argPreg[i]).addReg(argPreg[i]);
206 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000207
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000208 return ArgValues;
209}
210
211std::pair<SDOperand, SDOperand>
212AlphaTargetLowering::LowerCallTo(SDOperand Chain,
213 const Type *RetTy, SDOperand Callee,
214 ArgListTy &Args, SelectionDAG &DAG) {
215 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000216 if (Args.size() > 6)
217 NumBytes = (Args.size() - 6) * 8;
218
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000219 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
220 DAG.getConstant(NumBytes, getPointerTy()));
221 std::vector<SDOperand> args_to_use;
222 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000223 {
224 switch (getValueType(Args[i].second)) {
225 default: assert(0 && "Unexpected ValueType for argument!");
226 case MVT::i1:
227 case MVT::i8:
228 case MVT::i16:
229 case MVT::i32:
230 // Promote the integer to 64 bits. If the input type is signed use a
231 // sign extend, otherwise use a zero extend.
232 if (Args[i].second->isSigned())
233 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
234 else
235 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
236 break;
237 case MVT::i64:
238 case MVT::f64:
239 case MVT::f32:
240 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000241 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000242 args_to_use.push_back(Args[i].first);
243 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000244
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000245 std::vector<MVT::ValueType> RetVals;
246 MVT::ValueType RetTyVT = getValueType(RetTy);
247 if (RetTyVT != MVT::isVoid)
248 RetVals.push_back(RetTyVT);
249 RetVals.push_back(MVT::Other);
250
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000251 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
252 Chain, Callee, args_to_use), 0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000253 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
254 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
255 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000256 return std::make_pair(TheCall, Chain);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000257}
258
259std::pair<SDOperand, SDOperand>
260AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
261 //vastart just returns the address of the VarArgsFrameIndex slot.
262 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
263}
264
265std::pair<SDOperand,SDOperand> AlphaTargetLowering::
266LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000267 const Type *ArgTy, SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000268 abort();
269}
270
271
272std::pair<SDOperand, SDOperand> AlphaTargetLowering::
273LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
274 SelectionDAG &DAG) {
275 abort();
276}
277
278
279
280
281
282namespace {
283
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000284//===--------------------------------------------------------------------===//
285/// ISel - Alpha specific code to select Alpha machine instructions for
286/// SelectionDAG operations.
287//===--------------------------------------------------------------------===//
288class ISel : public SelectionDAGISel {
289
290 /// AlphaLowering - This object fully describes how to lower LLVM code to an
291 /// Alpha-specific SelectionDAG.
292 AlphaTargetLowering AlphaLowering;
293
294
295 /// ExprMap - As shared expressions are codegen'd, we keep track of which
296 /// vreg the value is produced in, so we only emit one copy of each compiled
297 /// tree.
298 static const unsigned notIn = (unsigned)(-1);
299 std::map<SDOperand, unsigned> ExprMap;
300
301 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
302 std::map<SDOperand, unsigned> CCInvMap;
303
304public:
305 ISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM)
306 {}
307
308 /// InstructionSelectBasicBlock - This callback is invoked by
309 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
310 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000311 DEBUG(BB->dump());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000312 // 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
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000452
453 bool invTest = false;
454 unsigned Tmp3;
455
456 ConstantFPSDNode *CN;
457 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
458 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
459 Tmp3 = SelectExpr(SetCC->getOperand(0));
460 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
461 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
462 {
463 Tmp3 = SelectExpr(SetCC->getOperand(1));
464 invTest = true;
465 }
466 else
467 {
468 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
469 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
470 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
471 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
472 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
473 .addReg(Tmp1).addReg(Tmp2);
474 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000475
476 switch (SetCC->getCondition()) {
477 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000478 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
479 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
480 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
481 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
482 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
483 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000484 }
485 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000486 return;
487 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000488 abort(); //Should never be reached
489 } else {
490 //Giveup and do the stupid thing
491 unsigned Tmp1 = SelectExpr(CC);
492 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
493 return;
494 }
Andrew Lenharth445171a2005-02-08 00:40:03 +0000495 abort(); //Should never be reached
496}
497
Andrew Lenharth40831c52005-01-28 06:57:18 +0000498unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
499{
500 unsigned Tmp1, Tmp2, Tmp3;
501 unsigned Opc = 0;
502 SDNode *Node = N.Val;
503 MVT::ValueType DestType = N.getValueType();
504 unsigned opcode = N.getOpcode();
505
506 switch (opcode) {
507 default:
508 Node->dump();
509 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +0000510
Andrew Lenharth9818c052005-02-05 13:19:12 +0000511 case ISD::SELECT:
512 {
Andrew Lenharth45859692005-03-03 21:47:53 +0000513 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
514 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
515 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
516
517 SDOperand CC = N.getOperand(0);
518 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
519
520 if (CC.getOpcode() == ISD::SETCC &&
521 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
522 { //FP Setcc -> Select yay!
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000523
524
Andrew Lenharth45859692005-03-03 21:47:53 +0000525 //for a cmp b: c = a - b;
526 //a = b: c = 0
527 //a < b: c < 0
528 //a > b: c > 0
529
530 bool invTest = false;
531 unsigned Tmp3;
532
533 ConstantFPSDNode *CN;
534 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
535 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
536 Tmp3 = SelectExpr(SetCC->getOperand(0));
537 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
538 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
539 {
540 Tmp3 = SelectExpr(SetCC->getOperand(1));
541 invTest = true;
542 }
543 else
544 {
545 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
546 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
547 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
548 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
549 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
550 .addReg(Tmp1).addReg(Tmp2);
551 }
552
553 switch (SetCC->getCondition()) {
554 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
555 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
556 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
557 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
558 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
559 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
560 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
561 }
Andrew Lenharth33819132005-03-04 20:09:23 +0000562 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
Andrew Lenharth45859692005-03-03 21:47:53 +0000563 return Result;
564 }
565 else
566 {
567 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
568 // Spill the cond to memory and reload it from there.
569 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
570 MachineFunction *F = BB->getParent();
571 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
572 unsigned Tmp4 = MakeReg(MVT::f64);
573 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addFrameIndex(FrameIdx).addReg(Alpha::F31);
574 BuildMI(BB, Alpha::LDT, 2, Tmp4).addFrameIndex(FrameIdx).addReg(Alpha::F31);
575 //now ideally, we don't have to do anything to the flag...
576 // Get the condition into the zero flag.
577 BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
578 return Result;
579 }
Andrew Lenharth9818c052005-02-05 13:19:12 +0000580 }
581
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000582 case ISD::FP_ROUND:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000583 assert (DestType == MVT::f32 &&
584 N.getOperand(0).getValueType() == MVT::f64 &&
585 "only f64 to f32 conversion supported here");
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000586 Tmp1 = SelectExpr(N.getOperand(0));
587 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
588 return Result;
589
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000590 case ISD::FP_EXTEND:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000591 assert (DestType == MVT::f64 &&
592 N.getOperand(0).getValueType() == MVT::f32 &&
593 "only f32 to f64 conversion supported here");
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000594 Tmp1 = SelectExpr(N.getOperand(0));
595 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
596 return Result;
597
Andrew Lenharth2c594352005-01-29 15:42:07 +0000598 case ISD::CopyFromReg:
599 {
600 // Make sure we generate both values.
601 if (Result != notIn)
602 ExprMap[N.getValue(1)] = notIn; // Generate the token
603 else
604 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
605
606 SDOperand Chain = N.getOperand(0);
607
608 Select(Chain);
609 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
610 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
611 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
612 return Result;
613 }
614
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000615 case ISD::LOAD:
616 {
617 // Make sure we generate both values.
618 if (Result != notIn)
619 ExprMap[N.getValue(1)] = notIn; // Generate the token
620 else
621 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000622
Andrew Lenharth29219162005-02-07 06:31:44 +0000623 DestType = N.getValue(0).getValueType();
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000624
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000625 SDOperand Chain = N.getOperand(0);
626 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000627 Select(Chain);
Andrew Lenharth65838902005-02-06 16:22:15 +0000628 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
629
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000630 if (Address.getOpcode() == ISD::GlobalAddress) {
631 AlphaLowering.restoreGP(BB);
632 Opc = GetSymVersion(Opc);
633 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
634 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000635 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000636 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000637 Opc = GetSymVersion(Opc);
Andrew Lenharth97127a12005-02-05 17:41:39 +0000638 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000639 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000640 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000641 BuildMI(BB, Opc, 2, Result)
642 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
643 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000644 } else {
645 long offset;
646 SelectAddr(Address, Tmp1, offset);
647 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
648 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000649 return Result;
650 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000651 case ISD::ConstantFP:
652 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
653 if (CN->isExactlyValue(+0.0)) {
654 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000655 } else if ( CN->isExactlyValue(-0.0)) {
656 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +0000657 } else {
658 abort();
659 }
660 }
661 return Result;
662
663 case ISD::MUL:
664 case ISD::ADD:
665 case ISD::SUB:
666 case ISD::SDIV:
667 switch( opcode ) {
668 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
669 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
670 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
671 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
672 };
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000673
674 ConstantFPSDNode *CN;
675 if (opcode == ISD::SUB
676 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
677 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
678 {
679 Tmp2 = SelectExpr(N.getOperand(1));
680 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
681 } else {
682 Tmp1 = SelectExpr(N.getOperand(0));
683 Tmp2 = SelectExpr(N.getOperand(1));
684 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
685 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000686 return Result;
687
Andrew Lenharth2c594352005-01-29 15:42:07 +0000688 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000689 {
690 //include a conversion sequence for float loads to double
691 if (Result != notIn)
692 ExprMap[N.getValue(1)] = notIn; // Generate the token
693 else
694 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
695
Andrew Lenhartha549deb2005-02-07 05:33:15 +0000696 Tmp1 = MakeReg(MVT::f32);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000697
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000698 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
699 "EXTLOAD not from f32");
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000700 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
701
702 SDOperand Chain = N.getOperand(0);
703 SDOperand Address = N.getOperand(1);
704 Select(Chain);
705
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000706 if (Address.getOpcode() == ISD::GlobalAddress) {
707 AlphaLowering.restoreGP(BB);
708 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
709 }
710 else if (ConstantPoolSDNode *CP =
711 dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
712 {
713 AlphaLowering.restoreGP(BB);
714 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addConstantPoolIndex(CP->getIndex());
715 }
716 else if(Address.getOpcode() == ISD::FrameIndex) {
717 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
Andrew Lenharth032f2352005-02-22 21:59:48 +0000718 BuildMI(BB, Alpha::LDS, 2, Tmp1)
719 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
720 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000721 } else {
722 long offset;
723 SelectAddr(Address, Tmp2, offset);
724 BuildMI(BB, Alpha::LDS, 1, Tmp1).addImm(offset).addReg(Tmp2);
725 }
Andrew Lenharth29219162005-02-07 06:31:44 +0000726 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000727 return Result;
728 }
Andrew Lenharth2c594352005-01-29 15:42:07 +0000729
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000730 case ISD::UINT_TO_FP:
731 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000732 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000733 assert (N.getOperand(0).getValueType() == MVT::i64
734 && "only quads can be loaded from");
Andrew Lenharth40831c52005-01-28 06:57:18 +0000735 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +0000736 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000737
738 //The hard way:
739 // Spill the integer to memory and reload it from there.
740 unsigned Size = MVT::getSizeInBits(MVT::i64)/8;
741 MachineFunction *F = BB->getParent();
742 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, Size);
743
Andrew Lenharth7efadce2005-01-31 01:44:26 +0000744 BuildMI(BB, Alpha::STQ, 3).addReg(Tmp1).addFrameIndex(FrameIdx).addReg(Alpha::F31);
745 BuildMI(BB, Alpha::LDT, 2, Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
746 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
747 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000748
749 //The easy way: doesn't work
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000750 // //so these instructions are not supported on ev56
751 // Opc = DestType == MVT::f64 ? Alpha::ITOFT : Alpha::ITOFS;
752 // BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp1);
753 // Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
754 // BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
Andrew Lenharth2c594352005-01-29 15:42:07 +0000755
Andrew Lenharth40831c52005-01-28 06:57:18 +0000756 return Result;
757 }
758 }
759 assert(0 && "should not get here");
760 return 0;
761}
762
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000763unsigned ISel::SelectExpr(SDOperand N) {
764 unsigned Result;
765 unsigned Tmp1, Tmp2, Tmp3;
766 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000767 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000768
769 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000770 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000771
772 unsigned &Reg = ExprMap[N];
773 if (Reg) return Reg;
774
775 if (N.getOpcode() != ISD::CALL)
776 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000777 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000778 else {
779 // If this is a call instruction, make sure to prepare ALL of the result
780 // values as well as the chain.
781 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000782 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000783 else {
784 Result = MakeReg(Node->getValueType(0));
785 ExprMap[N.getValue(0)] = Result;
786 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
787 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000788 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000789 }
790 }
791
Andrew Lenharth22088bb2005-02-02 15:05:33 +0000792 if (DestType == MVT::f64 || DestType == MVT::f32 ||
Andrew Lenharth06342c32005-02-07 06:21:37 +0000793 (
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000794 (opcode == ISD::LOAD || opcode == ISD::CopyFromReg ||
795 opcode == ISD::EXTLOAD) &&
796 (N.getValue(0).getValueType() == MVT::f32 ||
797 N.getValue(0).getValueType() == MVT::f64)
Andrew Lenharth06342c32005-02-07 06:21:37 +0000798 )
799 )
Andrew Lenharth40831c52005-01-28 06:57:18 +0000800 return SelectExprFP(N, Result);
801
802 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000803 default:
804 Node->dump();
805 assert(0 && "Node not handled!\n");
806
Andrew Lenharth032f2352005-02-22 21:59:48 +0000807 case ISD::DYNAMIC_STACKALLOC:
808 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +0000809 if (Result != notIn)
810 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +0000811 else
812 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
813
814 // FIXME: We are currently ignoring the requested alignment for handling
815 // greater than the stack alignment. This will need to be revisited at some
816 // point. Align = N.getOperand(2);
817
818 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
819 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
820 std::cerr << "Cannot allocate stack object with greater alignment than"
821 << " the stack alignment yet!";
822 abort();
823 }
824
825 Select(N.getOperand(0));
826 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
827 {
828 if (CN->getValue() < 32000)
829 {
830 BuildMI(BB, Alpha::LDA, 2, Alpha::R30)
831 .addImm(-CN->getValue()).addReg(Alpha::R30);
832 } else {
833 Tmp1 = SelectExpr(N.getOperand(1));
834 // Subtract size from stack pointer, thereby allocating some space.
835 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
836 }
837 } else {
838 Tmp1 = SelectExpr(N.getOperand(1));
839 // Subtract size from stack pointer, thereby allocating some space.
840 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
841 }
842
843 // Put a pointer to the space into the result register, by copying the stack
844 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +0000845 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +0000846 return Result;
847
Andrew Lenharth33819132005-03-04 20:09:23 +0000848// case ISD::ConstantPool:
849// Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
850// AlphaLowering.restoreGP(BB);
851// BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
852// return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +0000853
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000854 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +0000855 BuildMI(BB, Alpha::LDA, 2, Result)
856 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
857 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000858 return Result;
859
860 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +0000861 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000862 case ISD::SEXTLOAD:
Andrew Lenhartha549deb2005-02-07 05:33:15 +0000863 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000864 {
865 // Make sure we generate both values.
866 if (Result != notIn)
867 ExprMap[N.getValue(1)] = notIn; // Generate the token
868 else
869 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000870
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000871 SDOperand Chain = N.getOperand(0);
872 SDOperand Address = N.getOperand(1);
873 Select(Chain);
874
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000875 assert(Node->getValueType(0) == MVT::i64 &&
876 "Unknown type to sign extend to.");
Andrew Lenharth03824012005-02-07 05:55:55 +0000877 if (opcode == ISD::LOAD)
878 Opc = Alpha::LDQ;
879 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000880 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
881 default: Node->dump(); assert(0 && "Bad sign extend!");
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000882 case MVT::i32: Opc = Alpha::LDL;
883 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
884 case MVT::i16: Opc = Alpha::LDWU;
885 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +0000886 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000887 case MVT::i8: Opc = Alpha::LDBU;
888 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000889 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000890
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000891 if (Address.getOpcode() == ISD::GlobalAddress) {
892 AlphaLowering.restoreGP(BB);
893 Opc = GetSymVersion(Opc);
894 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
895 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000896 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
897 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000898 Opc = GetSymVersion(Opc);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000899 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000900 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000901 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000902 BuildMI(BB, Opc, 2, Result)
903 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
904 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000905 } else {
906 long offset;
907 SelectAddr(Address, Tmp1, offset);
908 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
909 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000910 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000911 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000912
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000913 case ISD::GlobalAddress:
914 AlphaLowering.restoreGP(BB);
915 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
916 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
917 return Result;
918
919 case ISD::CALL:
920 {
921 Select(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000922
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000923 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000924 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000925
926 //grab the arguments
927 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000928 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000929 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000930 argvregs.push_back(SelectExpr(N.getOperand(i)));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000931
Andrew Lenharth684f2292005-01-30 00:35:27 +0000932 //in reg args
933 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000934 {
935 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
936 Alpha::R19, Alpha::R20, Alpha::R21};
937 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
938 Alpha::F19, Alpha::F20, Alpha::F21};
939 switch(N.getOperand(i+2).getValueType()) {
940 default:
941 Node->dump();
942 N.getOperand(i).Val->dump();
943 std::cerr << "Type for " << i << " is: " <<
944 N.getOperand(i+2).getValueType() << "\n";
945 assert(0 && "Unknown value type for call");
946 case MVT::i1:
947 case MVT::i8:
948 case MVT::i16:
949 case MVT::i32:
950 case MVT::i64:
951 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
952 break;
953 case MVT::f32:
954 case MVT::f64:
955 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
956 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000957 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000958 }
Andrew Lenharth684f2292005-01-30 00:35:27 +0000959 //in mem args
960 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000961 {
962 switch(N.getOperand(i+2).getValueType()) {
963 default:
964 Node->dump();
965 N.getOperand(i).Val->dump();
966 std::cerr << "Type for " << i << " is: " <<
967 N.getOperand(i+2).getValueType() << "\n";
968 assert(0 && "Unknown value type for call");
969 case MVT::i1:
970 case MVT::i8:
971 case MVT::i16:
972 case MVT::i32:
973 case MVT::i64:
974 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
975 break;
976 case MVT::f32:
977 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
978 break;
979 case MVT::f64:
980 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
981 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000982 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000983 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000984 //build the right kind of call
985 if (GlobalAddressSDNode *GASD =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000986 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000987 {
Andrew Lenharth3e315922005-02-10 20:10:38 +0000988 //if (GASD->getGlobal()->isExternal()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000989 //use safe calling convention
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000990 AlphaLowering.restoreGP(BB);
991 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal(),true);
Andrew Lenharth3e315922005-02-10 20:10:38 +0000992 //} else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000993 //use PC relative branch call
Andrew Lenharth3e315922005-02-10 20:10:38 +0000994 //BuildMI(BB, Alpha::BSR, 1, Alpha::R26).addGlobalAddress(GASD->getGlobal(),true);
995 //}
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000996 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000997 else if (ExternalSymbolSDNode *ESSDN =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000998 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000999 {
1000 AlphaLowering.restoreGP(BB);
1001 BuildMI(BB, Alpha::CALL, 0).addExternalSymbol(ESSDN->getSymbol(), true);
1002 } else {
1003 //no need to restore GP as we are doing an indirect call
1004 Tmp1 = SelectExpr(N.getOperand(1));
1005 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1006 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1007 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001008
1009 //push the result into a virtual register
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001010
1011 switch (Node->getValueType(0)) {
1012 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001013 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001014 case MVT::i1:
1015 case MVT::i8:
1016 case MVT::i16:
1017 case MVT::i32:
1018 case MVT::i64:
1019 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1020 break;
1021 case MVT::f32:
1022 case MVT::f64:
1023 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1024 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001025 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001026 return Result+N.ResNo;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001027 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001028
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001029 case ISD::SIGN_EXTEND:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001030 abort();
1031
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001032 case ISD::SIGN_EXTEND_INREG:
1033 {
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001034 //Alpha has instructions for a bunch of signed 32 bit stuff
1035 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001036 {
1037 switch (N.getOperand(0).getOpcode()) {
1038 case ISD::ADD:
1039 case ISD::SUB:
1040 case ISD::MUL:
1041 {
1042 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1043 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1044 //FIXME: first check for Scaled Adds and Subs!
1045 if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1046 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
1047 { //Normal imm add/sub
1048 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth093f3272005-02-12 21:11:17 +00001049 //if the value was really originally a i32, skip the up conversion
1050 if (N.getOperand(0).getOperand(0).getOpcode() == ISD::SIGN_EXTEND_INREG &&
1051 dyn_cast<MVTSDNode>(N.getOperand(0).getOperand(0).Val)
1052 ->getExtraValueType() == MVT::i32)
1053 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1054 else
1055 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001056 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1057 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001058 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001059 else
1060 { //Normal add/sub
1061 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULLi : Alpha::SUBL);
Andrew Lenharth093f3272005-02-12 21:11:17 +00001062 //if the value was really originally a i32, skip the up conversion
1063 if (N.getOperand(0).getOperand(0).getOpcode() == ISD::SIGN_EXTEND_INREG &&
1064 dyn_cast<MVTSDNode>(N.getOperand(0).getOperand(0).Val)
1065 ->getExtraValueType() == MVT::i32)
1066 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1067 else
1068 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1069 //if the value was really originally a i32, skip the up conversion
1070 if (N.getOperand(0).getOperand(1).getOpcode() == ISD::SIGN_EXTEND_INREG &&
1071 dyn_cast<MVTSDNode>(N.getOperand(0).getOperand(1).Val)
1072 ->getExtraValueType() == MVT::i32)
1073 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1074 else
1075 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1076
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001077 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1078 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1079 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1080 }
1081 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001082 }
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001083 case ISD::SEXTLOAD:
1084 //SelectionDag isn't deleting the signextend after sextloads
1085 Reg = Result = SelectExpr(N.getOperand(0));
1086 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001087 default: break; //Fall Though;
1088 }
1089 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001090 Tmp1 = SelectExpr(N.getOperand(0));
1091 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001092 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001093 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001094 {
1095 default:
1096 Node->dump();
1097 assert(0 && "Sign Extend InReg not there yet");
1098 break;
1099 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001100 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001101 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001102 break;
1103 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001104 case MVT::i16:
1105 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
1106 break;
1107 case MVT::i8:
1108 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
1109 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001110 case MVT::i1:
1111 Tmp2 = MakeReg(MVT::i64);
1112 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenharth7536eea2005-02-12 20:42:09 +00001113 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::F31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001114 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001115 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001116 return Result;
1117 }
1118 case ISD::ZERO_EXTEND_INREG:
1119 {
1120 Tmp1 = SelectExpr(N.getOperand(0));
1121 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001122 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001123 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001124 {
1125 default:
1126 Node->dump();
1127 assert(0 && "Zero Extend InReg not there yet");
1128 break;
1129 case MVT::i32: Tmp2 = 0xf0; break;
1130 case MVT::i16: Tmp2 = 0xfc; break;
1131 case MVT::i8: Tmp2 = 0xfe; break;
1132 case MVT::i1: //handle this one special
1133 BuildMI(BB, Alpha::ANDi, 2, Result).addReg(Tmp1).addImm(1);
1134 return Result;
1135 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001136 BuildMI(BB, Alpha::ZAPi, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001137 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001138 }
1139
1140 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001141 {
1142 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1143 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1144 bool isConst1 = false;
1145 bool isConst2 = false;
1146 int dir;
Andrew Lenharth9818c052005-02-05 13:19:12 +00001147
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001148 //Tmp1 = SelectExpr(N.getOperand(0));
1149 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001150 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1151 isConst1 = true;
1152 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001153 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1154 isConst2 = true;
1155
1156 switch (SetCC->getCondition()) {
1157 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1158 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001159 case ISD::SETLT:
1160 Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
1161 case ISD::SETLE:
1162 Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
1163 case ISD::SETGT:
1164 Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
1165 case ISD::SETGE:
1166 Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
1167 case ISD::SETULT:
1168 Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
1169 case ISD::SETUGT:
1170 Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
1171 case ISD::SETULE:
1172 Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
1173 case ISD::SETUGE:
1174 Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001175 case ISD::SETNE: {//Handle this one special
1176 //std::cerr << "Alpha does not have a setne.\n";
1177 //abort();
1178 Tmp1 = SelectExpr(N.getOperand(0));
1179 Tmp2 = SelectExpr(N.getOperand(1));
1180 Tmp3 = MakeReg(MVT::i64);
1181 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001182 //Remeber we have the Inv for this CC
1183 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001184 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001185 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001186 return Result;
1187 }
1188 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001189 if (dir == 1) {
1190 Tmp1 = SelectExpr(N.getOperand(0));
1191 if (isConst2) {
1192 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1193 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1194 } else {
1195 Tmp2 = SelectExpr(N.getOperand(1));
1196 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1197 }
1198 } else if (dir == 2) {
1199 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001200 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001201 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1202 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1203 } else {
1204 Tmp2 = SelectExpr(N.getOperand(0));
1205 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1206 }
1207 } else { //dir == 0
1208 if (isConst1) {
1209 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1210 Tmp2 = SelectExpr(N.getOperand(1));
1211 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
1212 } else if (isConst2) {
1213 Tmp1 = SelectExpr(N.getOperand(0));
1214 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1215 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1216 } else {
1217 Tmp1 = SelectExpr(N.getOperand(0));
1218 Tmp2 = SelectExpr(N.getOperand(1));
1219 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1220 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001221 }
1222 } else {
1223 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
1224 bool rev = false;
1225 bool inv = false;
1226
1227 switch (SetCC->getCondition()) {
1228 default: Node->dump(); assert(0 && "Unknown FP comparison!");
1229 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
1230 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
1231 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
1232 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
1233 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
1234 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
1235 }
1236
1237 Tmp1 = SelectExpr(N.getOperand(0));
1238 Tmp2 = SelectExpr(N.getOperand(1));
1239 //Can only compare doubles, and dag won't promote for me
1240 if (SetCC->getOperand(0).getValueType() == MVT::f32)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001241 {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001242 //assert(0 && "Setcc On float?\n");
1243 std::cerr << "Setcc on float!\n";
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001244 Tmp3 = MakeReg(MVT::f64);
1245 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
1246 Tmp1 = Tmp3;
1247 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001248 if (SetCC->getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001249 {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001250 //assert (0 && "Setcc On float?\n");
1251 std::cerr << "Setcc on float!\n";
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001252 Tmp3 = MakeReg(MVT::f64);
1253 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
Andrew Lenharth572af902005-02-14 05:41:43 +00001254 Tmp2 = Tmp3;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001255 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001256
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001257 if (rev) std::swap(Tmp1, Tmp2);
1258 Tmp3 = MakeReg(MVT::f64);
1259 //do the comparison
1260 BuildMI(BB, Opc, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1261
1262 //now arrange for Result (int) to have a 1 or 0
1263
1264 // Spill the FP to memory and reload it from there.
1265 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1266 MachineFunction *F = BB->getParent();
1267 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
1268 unsigned Tmp4 = MakeReg(MVT::f64);
1269 BuildMI(BB, Alpha::CVTTQ, 1, Tmp4).addReg(Tmp3);
1270 BuildMI(BB, Alpha::STT, 3).addReg(Tmp4).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1271 unsigned Tmp5 = MakeReg(MVT::i64);
1272 BuildMI(BB, Alpha::LDQ, 2, Tmp5).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharth9818c052005-02-05 13:19:12 +00001273
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001274 //now, set result based on Tmp5
1275 //Set Tmp6 if fp cmp was false
1276 unsigned Tmp6 = MakeReg(MVT::i64);
1277 BuildMI(BB, Alpha::CMPEQ, 2, Tmp6).addReg(Tmp5).addReg(Alpha::R31);
1278 //and invert
1279 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp6).addReg(Alpha::R31);
1280
1281 }
1282 // else
1283 // {
1284 // Node->dump();
1285 // assert(0 && "Not a setcc in setcc");
1286 // }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001287 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001288 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001289 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001290
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001291 case ISD::CopyFromReg:
1292 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001293 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001294 if (Result != notIn)
1295 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001296 else
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001297 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth40831c52005-01-28 06:57:18 +00001298
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001299 SDOperand Chain = N.getOperand(0);
1300
1301 Select(Chain);
1302 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1303 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1304 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1305 return Result;
1306 }
1307
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001308 //Most of the plain arithmetic and logic share the same form, and the same
1309 //constant immediate test
1310 case ISD::AND:
1311 case ISD::OR:
1312 case ISD::XOR:
1313 case ISD::SHL:
1314 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001315 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001316 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001317 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1318 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001319 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001320 {
1321 switch(opcode) {
1322 case ISD::AND: Opc = Alpha::ANDi; break;
1323 case ISD::OR: Opc = Alpha::BISi; break;
1324 case ISD::XOR: Opc = Alpha::XORi; break;
1325 case ISD::SHL: Opc = Alpha::SLi; break;
1326 case ISD::SRL: Opc = Alpha::SRLi; break;
1327 case ISD::SRA: Opc = Alpha::SRAi; break;
1328 case ISD::MUL: Opc = Alpha::MULQi; break;
1329 };
1330 Tmp1 = SelectExpr(N.getOperand(0));
1331 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1332 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1333 } else {
1334 switch(opcode) {
1335 case ISD::AND: Opc = Alpha::AND; break;
1336 case ISD::OR: Opc = Alpha::BIS; break;
1337 case ISD::XOR: Opc = Alpha::XOR; break;
1338 case ISD::SHL: Opc = Alpha::SL; break;
1339 case ISD::SRL: Opc = Alpha::SRL; break;
1340 case ISD::SRA: Opc = Alpha::SRA; break;
1341 case ISD::MUL: Opc = Alpha::MULQ; break;
1342 };
1343 Tmp1 = SelectExpr(N.getOperand(0));
1344 Tmp2 = SelectExpr(N.getOperand(1));
1345 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1346 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001347 return Result;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001348
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001349 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001350 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001351 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001352 bool isAdd = opcode == ISD::ADD;
1353
1354 //FIXME: first check for Scaled Adds and Subs!
1355 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001356 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001357 { //Normal imm add/sub
1358 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1359 Tmp1 = SelectExpr(N.getOperand(0));
1360 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1361 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1362 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001363 else if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001364 (cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767 ||
1365 (long)cast<ConstantSDNode>(N.getOperand(1))->getValue() >= -32767))
1366 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001367 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001368 Tmp2 = (long)cast<ConstantSDNode>(N.getOperand(1))->getValue();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001369 if (!isAdd)
1370 Tmp2 = -Tmp2;
1371 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
1372 } else {
1373 //Normal add/sub
1374 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1375 Tmp1 = SelectExpr(N.getOperand(0));
1376 Tmp2 = SelectExpr(N.getOperand(1));
1377 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1378 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001379 return Result;
1380 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001381
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001382 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00001383 case ISD::SREM:
1384 case ISD::SDIV:
1385 case ISD::UDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001386 //FIXME: alpha really doesn't support any of these operations,
1387 // the ops are expanded into special library calls with
1388 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001389 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00001390 switch(opcode) {
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001391 case ISD::UREM: Opc = Alpha::REMQU; break;
1392 case ISD::SREM: Opc = Alpha::REMQ; break;
1393 case ISD::UDIV: Opc = Alpha::DIVQU; break;
1394 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001395 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001396 Tmp1 = SelectExpr(N.getOperand(0));
1397 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth33819132005-03-04 20:09:23 +00001398 //set up regs explicitly (helps Reg alloc)
1399 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
1400 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001401 AlphaLowering.restoreGP(BB);
Andrew Lenharth33819132005-03-04 20:09:23 +00001402 BuildMI(BB, Opc, 2).addReg(Alpha::R24).addReg(Alpha::R25);
1403 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001404 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001405
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001406 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001407 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001408 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001409 assert (DestType == MVT::i64 && "only quads can be loaded to");
1410 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001411 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001412 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1413
1414 //The hard way:
1415 // Spill the integer to memory and reload it from there.
1416 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
1417 MachineFunction *F = BB->getParent();
1418 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
1419
1420 //CVTTQ STT LDQ
1421 //CVTST CVTTQ STT LDQ
1422 if (SrcType == MVT::f32)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001423 {
1424 Tmp2 = MakeReg(MVT::f64);
1425 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1426 Tmp1 = Tmp2;
1427 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001428 Tmp2 = MakeReg(MVT::f64);
1429 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
1430 BuildMI(BB, Alpha::STT, 3).addReg(Tmp2).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1431 BuildMI(BB, Alpha::LDQ, 2, Result).addFrameIndex(FrameIdx).addReg(Alpha::F31);
1432
1433 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001434 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001435
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001436 // // case ISD::FP_TO_UINT:
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001437
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001438 case ISD::SELECT:
1439 {
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001440 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001441 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1442 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001443 // Get the condition into the zero flag.
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001444 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001445 return Result;
1446 }
1447
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001448 case ISD::Constant:
1449 {
Andrew Lenharth22d5a412005-02-02 00:51:15 +00001450 unsigned long val = cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001451 if (val < 32000 && (long)val > -32000)
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001452 BuildMI(BB, Alpha::LOAD_IMM, 1, Result).addImm((long)val);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001453 else {
1454 MachineConstantPool *CP = BB->getParent()->getConstantPool();
1455 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
1456 unsigned CPI = CP->getConstantPoolIndex(C);
1457 AlphaLowering.restoreGP(BB);
1458 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
1459 }
1460 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001461 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001462 }
1463
1464 return 0;
1465}
1466
1467void ISel::Select(SDOperand N) {
1468 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00001469 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001470
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001471 // FIXME: Disable for our current expansion model!
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001472 if (/*!N->hasOneUse() &&*/ !ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001473 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001474
1475 SDNode *Node = N.Val;
Andrew Lenharth760270d2005-02-07 23:02:23 +00001476
Andrew Lenharth760270d2005-02-07 23:02:23 +00001477 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001478
1479 default:
1480 Node->dump(); std::cerr << "\n";
1481 assert(0 && "Node not handled yet!");
1482
1483 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00001484 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001485 return;
1486 }
1487
1488 case ISD::BR: {
1489 MachineBasicBlock *Dest =
1490 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1491
1492 Select(N.getOperand(0));
1493 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
1494 return;
1495 }
1496
1497 case ISD::ImplicitDef:
1498 Select(N.getOperand(0));
1499 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
1500 return;
1501
1502 case ISD::EntryToken: return; // Noop
1503
1504 case ISD::TokenFactor:
1505 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1506 Select(Node->getOperand(i));
1507
1508 //N.Val->dump(); std::cerr << "\n";
1509 //assert(0 && "Node not handled yet!");
1510
1511 return;
1512
1513 case ISD::CopyToReg:
1514 Select(N.getOperand(0));
1515 Tmp1 = SelectExpr(N.getOperand(1));
1516 Tmp2 = cast<RegSDNode>(N)->getReg();
1517
1518 if (Tmp1 != Tmp2) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001519 if (N.getOperand(1).getValueType() == MVT::f64 ||
1520 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00001521 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1522 else
1523 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001524 }
1525 return;
1526
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001527 case ISD::RET:
1528 switch (N.getNumOperands()) {
1529 default:
1530 std::cerr << N.getNumOperands() << "\n";
1531 for (unsigned i = 0; i < N.getNumOperands(); ++i)
1532 std::cerr << N.getOperand(i).getValueType() << "\n";
1533 Node->dump();
1534 assert(0 && "Unknown return instruction!");
1535 case 2:
1536 Select(N.getOperand(0));
1537 Tmp1 = SelectExpr(N.getOperand(1));
1538 switch (N.getOperand(1).getValueType()) {
1539 default: Node->dump();
1540 assert(0 && "All other types should have been promoted!!");
1541 case MVT::f64:
1542 case MVT::f32:
1543 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1544 break;
1545 case MVT::i32:
1546 case MVT::i64:
1547 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1548 break;
1549 }
1550 break;
1551 case 1:
1552 Select(N.getOperand(0));
1553 break;
1554 }
1555 //Tmp2 = AlphaLowering.getRetAddr();
1556 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
1557 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
1558 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001559
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001560 case ISD::TRUNCSTORE:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001561 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001562 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001563 SDOperand Chain = N.getOperand(0);
1564 SDOperand Value = N.getOperand(1);
1565 SDOperand Address = N.getOperand(2);
1566 Select(Chain);
1567
1568 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00001569
1570 if (opcode == ISD::STORE) {
1571 switch(Value.getValueType()) {
1572 default: assert(0 && "unknown Type in store");
1573 case MVT::i64: Opc = Alpha::STQ; break;
1574 case MVT::f64: Opc = Alpha::STT; break;
1575 case MVT::f32: Opc = Alpha::STS; break;
1576 }
1577 } else { //ISD::TRUNCSTORE
1578 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
1579 default: assert(0 && "unknown Type in store");
1580 case MVT::i1: //FIXME: DAG does not promote this load
1581 case MVT::i8: Opc = Alpha::STB; break;
1582 case MVT::i16: Opc = Alpha::STW; break;
1583 case MVT::i32: Opc = Alpha::STL; break;
1584 }
Andrew Lenharth65838902005-02-06 16:22:15 +00001585 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00001586
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001587 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001588 {
1589 AlphaLowering.restoreGP(BB);
1590 Opc = GetSymVersion(Opc);
1591 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1592 }
Andrew Lenharth05380342005-02-07 05:07:00 +00001593 else if(Address.getOpcode() == ISD::FrameIndex)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001594 {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001595 BuildMI(BB, Opc, 3).addReg(Tmp1)
1596 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1597 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001598 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001599 else
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001600 {
1601 long offset;
1602 SelectAddr(Address, Tmp2, offset);
1603 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1604 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001605 return;
1606 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001607
1608 case ISD::EXTLOAD:
1609 case ISD::SEXTLOAD:
1610 case ISD::ZEXTLOAD:
1611 case ISD::LOAD:
1612 case ISD::CopyFromReg:
1613 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001614 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001615 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001616 SelectExpr(N);
1617 return;
1618
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001619 case ISD::ADJCALLSTACKDOWN:
1620 case ISD::ADJCALLSTACKUP:
1621 Select(N.getOperand(0));
1622 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1623
1624 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? Alpha::ADJUSTSTACKDOWN :
1625 Alpha::ADJUSTSTACKUP;
1626 BuildMI(BB, Opc, 1).addImm(Tmp1);
1627 return;
1628 }
1629 assert(0 && "Should not be reached!");
1630}
1631
1632
1633/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1634/// into a machine code representation using pattern matching and a machine
1635/// description file.
1636///
1637FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
1638 return new ISel(TM);
1639}