blob: 5df40ca154d6e730d79cc7f8a4f8031fe0740c79 [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 Lenharth95762122005-03-31 21:24:06 +000030#include "llvm/Support/CommandLine.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000031#include <set>
Andrew Lenharth684f2292005-01-30 00:35:27 +000032#include <algorithm>
Andrew Lenharth304d0f32005-01-22 23:41:55 +000033using namespace llvm;
34
Andrew Lenharth95762122005-03-31 21:24:06 +000035namespace llvm {
36 cl::opt<bool> EnableAlphaIDIV("enable-alpha-intfpdiv",
37 cl::desc("Use the FP div instruction for integer div when possible"),
38 cl::Hidden);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +000039 cl::opt<bool> EnableAlphaFTOI("enable-alpha-ftoi",
Andrew Lenharth5e99dd92005-03-31 22:02:25 +000040 cl::desc("Enable use of ftoi* and itof* instructions (ev6 and higher)"),
Andrew Lenharth95762122005-03-31 21:24:06 +000041 cl::Hidden);
42}
43
Andrew Lenharth304d0f32005-01-22 23:41:55 +000044//===----------------------------------------------------------------------===//
45// AlphaTargetLowering - Alpha Implementation of the TargetLowering interface
46namespace {
47 class AlphaTargetLowering : public TargetLowering {
48 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
49 unsigned GP; //GOT vreg
50 public:
51 AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
52 // Set up the TargetLowering object.
Andrew Lenharth3d65d312005-01-27 03:49:45 +000053 //I am having problems with shr n ubyte 1
Andrew Lenharth879ef222005-02-02 17:00:21 +000054 setShiftAmountType(MVT::i64);
55 setSetCCResultType(MVT::i64);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000056
Andrew Lenharth304d0f32005-01-22 23:41:55 +000057 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
58 addRegisterClass(MVT::f64, Alpha::FPRCRegisterClass);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000059 addRegisterClass(MVT::f32, Alpha::FPRCRegisterClass);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000060
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000061 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
62 setOperationAction(ISD::EXTLOAD , MVT::f32 , Promote);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000063
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000064 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
65 setOperationAction(ISD::ZEXTLOAD , MVT::i32 , Expand);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000066
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000067 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
68 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
69 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000070
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000071 setOperationAction(ISD::SREM , MVT::f32 , Expand);
72 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +000073
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000074 //If this didn't legalize into a div....
75 // setOperationAction(ISD::SREM , MVT::i64, Expand);
76 // setOperationAction(ISD::UREM , MVT::i64, Expand);
77
78 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
79 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
80 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
Andrew Lenharth9818c052005-02-05 13:19:12 +000081
Andrew Lenharth33819132005-03-04 20:09:23 +000082 //Doesn't work yet
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000083 setOperationAction(ISD::SETCC , MVT::f32, Promote);
Andrew Lenharth572af902005-02-14 05:41:43 +000084
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000085 computeRegisterProperties();
Andrew Lenharth304d0f32005-01-22 23:41:55 +000086
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000087 addLegalFPImmediate(+0.0); //F31
88 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +000089 }
90
91 /// LowerArguments - This hook must be implemented to indicate how we should
92 /// lower the arguments for the specified function, into the specified DAG.
93 virtual std::vector<SDOperand>
94 LowerArguments(Function &F, SelectionDAG &DAG);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000095
Andrew Lenharth304d0f32005-01-22 23:41:55 +000096 /// LowerCallTo - This hook lowers an abstract call to a function into an
97 /// actual call.
98 virtual std::pair<SDOperand, SDOperand>
Nate Begeman8e21e712005-03-26 01:29:23 +000099 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
100 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000101
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000102 virtual std::pair<SDOperand, SDOperand>
103 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000104
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000105 virtual std::pair<SDOperand,SDOperand>
106 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
107 const Type *ArgTy, SelectionDAG &DAG);
108
109 virtual std::pair<SDOperand, SDOperand>
110 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
111 SelectionDAG &DAG);
112
113 void restoreGP(MachineBasicBlock* BB)
114 {
115 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
116 }
117 };
118}
119
120//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
121
122//For now, just use variable size stack frame format
123
124//In a standard call, the first six items are passed in registers $16
125//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
126//of argument-to-register correspondence.) The remaining items are
127//collected in a memory argument list that is a naturally aligned
128//array of quadwords. In a standard call, this list, if present, must
129//be passed at 0(SP).
130//7 ... n 0(SP) ... (n-7)*8(SP)
131
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000132// //#define FP $15
133// //#define RA $26
134// //#define PV $27
135// //#define GP $29
136// //#define SP $30
137
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000138std::vector<SDOperand>
139AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
140{
141 std::vector<SDOperand> ArgValues;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000142 std::vector<SDOperand> LS;
143 SDOperand Chain = DAG.getRoot();
144
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000145 // assert(0 && "TODO");
146 MachineFunction &MF = DAG.getMachineFunction();
Andrew Lenharth05380342005-02-07 05:07:00 +0000147 MachineFrameInfo*MFI = MF.getFrameInfo();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000148
149 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
150 MachineBasicBlock& BB = MF.front();
151
152 //Handle the return address
153 //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
154
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000155 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
156 Alpha::R19, Alpha::R20, Alpha::R21};
157 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
158 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000159 int count = 0;
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000160
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000161 //Def incoming registers
162 {
163 Function::arg_iterator I = F.arg_begin();
164 Function::arg_iterator E = F.arg_end();
165 for (int i = 0; i < 6; ++i)
166 {
167 if (F.isVarArg()) {
168 BuildMI(&BB, Alpha::IDEF, 0, args_int[i]);
169 BuildMI(&BB, Alpha::IDEF, 0, args_float[i]);
170 } else if (I != E)
171 {
172 if(MVT::isInteger(getValueType(I->getType())))
173 BuildMI(&BB, Alpha::IDEF, 0, args_int[i]);
174 else
175 BuildMI(&BB, Alpha::IDEF, 0, args_float[i]);
176 ++I;
177 }
178 }
179 }
180
181 BuildMI(&BB, Alpha::IDEF, 0, Alpha::R29);
182 BuildMI(&BB, Alpha::BIS, 2, GP).addReg(Alpha::R29).addReg(Alpha::R29);
183
Chris Lattnere4d5c442005-03-15 04:54:21 +0000184 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000185 {
186 SDOperand newroot, argt;
187 if (count < 6) {
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000188 unsigned Vreg;
189 MVT::ValueType VT = getValueType(I->getType());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000190 switch (getValueType(I->getType())) {
191 default:
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000192 std::cerr << "Unknown Type " << VT << "\n";
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000193 abort();
194 case MVT::f64:
195 case MVT::f32:
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000196 Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(VT));
197 BuildMI(&BB, Alpha::CPYS, 2, Vreg).addReg(args_float[count]).addReg(args_float[count]);
198 argt = newroot = DAG.getCopyFromReg(Vreg,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000199 getValueType(I->getType()),
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000200 Chain);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000201 break;
202 case MVT::i1:
203 case MVT::i8:
204 case MVT::i16:
205 case MVT::i32:
206 case MVT::i64:
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000207 Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
208 BuildMI(&BB, Alpha::BIS, 2, Vreg).addReg(args_int[count]).addReg(args_int[count]);
209 argt = newroot = DAG.getCopyFromReg(Vreg, MVT::i64, Chain);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000210 if (getValueType(I->getType()) != MVT::i64)
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000211 argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()), newroot);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000212 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000213 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000214 } else { //more args
215 // Create the frame index object for this incoming parameter...
216 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
217
218 // Create the SelectionDAG nodes corresponding to a load
219 //from this parameter
220 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
221 argt = newroot = DAG.getLoad(getValueType(I->getType()),
222 DAG.getEntryNode(), FIN);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000223 }
Andrew Lenharth032f2352005-02-22 21:59:48 +0000224 ++count;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000225 LS.push_back(newroot.getValue(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000226 ArgValues.push_back(argt);
227 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000228
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000229 // If the functions takes variable number of arguments, copy all regs to stack
230 if (F.isVarArg())
231 for (int i = 0; i < 6; ++i)
232 {
233 unsigned Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
234 BuildMI(&BB, Alpha::BIS, 2, Vreg).addReg(args_int[i]).addReg(args_int[i]);
235 SDOperand argt = DAG.getCopyFromReg(Vreg, MVT::i64, Chain);
236 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
237 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
238 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, argt, SDFI));
239
240 Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::f64));
241 BuildMI(&BB, Alpha::CPYS, 2, Vreg).addReg(args_float[i]).addReg(args_float[i]);
242 argt = DAG.getCopyFromReg(Vreg, MVT::f64, Chain);
243 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
244 SDFI = DAG.getFrameIndex(FI, MVT::i64);
245 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, argt, SDFI));
246 }
247
248 // If the function takes variable number of arguments, make a frame index for
249 // the start of the first arg value... for expansion of llvm.va_start.
250 // if (F.isVarArg())
251 // VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
252
253 //Set up a token factor with all the stack traffic
254 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
255 //return the arguments
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000256 return ArgValues;
257}
258
259std::pair<SDOperand, SDOperand>
260AlphaTargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman8e21e712005-03-26 01:29:23 +0000261 const Type *RetTy, bool isVarArg,
262 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000263 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000264 if (Args.size() > 6)
265 NumBytes = (Args.size() - 6) * 8;
266
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000267 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
268 DAG.getConstant(NumBytes, getPointerTy()));
269 std::vector<SDOperand> args_to_use;
270 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000271 {
272 switch (getValueType(Args[i].second)) {
273 default: assert(0 && "Unexpected ValueType for argument!");
274 case MVT::i1:
275 case MVT::i8:
276 case MVT::i16:
277 case MVT::i32:
278 // Promote the integer to 64 bits. If the input type is signed use a
279 // sign extend, otherwise use a zero extend.
280 if (Args[i].second->isSigned())
281 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
282 else
283 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
284 break;
285 case MVT::i64:
286 case MVT::f64:
287 case MVT::f32:
288 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000289 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000290 args_to_use.push_back(Args[i].first);
291 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000292
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000293 std::vector<MVT::ValueType> RetVals;
294 MVT::ValueType RetTyVT = getValueType(RetTy);
295 if (RetTyVT != MVT::isVoid)
296 RetVals.push_back(RetTyVT);
297 RetVals.push_back(MVT::Other);
298
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000299 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
300 Chain, Callee, args_to_use), 0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000301 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
302 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
303 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000304 return std::make_pair(TheCall, Chain);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000305}
306
307std::pair<SDOperand, SDOperand>
308AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
309 //vastart just returns the address of the VarArgsFrameIndex slot.
310 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
311}
312
313std::pair<SDOperand,SDOperand> AlphaTargetLowering::
314LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000315 const Type *ArgTy, SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000316 abort();
317}
318
319
320std::pair<SDOperand, SDOperand> AlphaTargetLowering::
321LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
322 SelectionDAG &DAG) {
323 abort();
324}
325
326
327
328
329
330namespace {
331
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000332//===--------------------------------------------------------------------===//
333/// ISel - Alpha specific code to select Alpha machine instructions for
334/// SelectionDAG operations.
335//===--------------------------------------------------------------------===//
336class ISel : public SelectionDAGISel {
337
338 /// AlphaLowering - This object fully describes how to lower LLVM code to an
339 /// Alpha-specific SelectionDAG.
340 AlphaTargetLowering AlphaLowering;
341
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000342 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
343 // for sdiv and udiv until it is put into the future
344 // dag combiner.
345
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000346 /// ExprMap - As shared expressions are codegen'd, we keep track of which
347 /// vreg the value is produced in, so we only emit one copy of each compiled
348 /// tree.
349 static const unsigned notIn = (unsigned)(-1);
350 std::map<SDOperand, unsigned> ExprMap;
351
352 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
353 std::map<SDOperand, unsigned> CCInvMap;
354
355public:
356 ISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM)
357 {}
358
359 /// InstructionSelectBasicBlock - This callback is invoked by
360 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
361 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000362 DEBUG(BB->dump());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000363 // Codegen the basic block.
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000364 ISelDAG = &DAG;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000365 Select(DAG.getRoot());
366
367 // Clear state used for selection.
368 ExprMap.clear();
369 CCInvMap.clear();
370 }
371
372 unsigned SelectExpr(SDOperand N);
373 unsigned SelectExprFP(SDOperand N, unsigned Result);
374 void Select(SDOperand N);
375
376 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
377 void SelectBranchCC(SDOperand N);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000378 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
379 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000380 //returns whether the sense of the comparison was inverted
381 bool SelectFPSetCC(SDOperand N, unsigned dst);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000382
383 // dag -> dag expanders for integer divide by constant
384 SDOperand BuildSDIVSequence(SDOperand N);
385 SDOperand BuildUDIVSequence(SDOperand N);
386
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000387};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000388}
389
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000390//Shamelessly adapted from PPC32
391// Structure used to return the necessary information to codegen an SDIV as
392// a multiply.
393struct ms {
394 int64_t m; // magic number
395 int64_t s; // shift amount
396};
397
398struct mu {
399 uint64_t m; // magic number
400 int64_t a; // add indicator
401 int64_t s; // shift amount
402};
403
404/// magic - calculate the magic numbers required to codegen an integer sdiv as
405/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
406/// or -1.
407static struct ms magic(int64_t d) {
408 int64_t p;
409 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
410 const uint64_t two63 = 9223372036854775808ULL; // 2^63
411 struct ms mag;
412
413 ad = abs(d);
414 t = two63 + ((uint64_t)d >> 63);
415 anc = t - 1 - t%ad; // absolute value of nc
416 p = 31; // initialize p
417 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
418 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
419 q2 = two63/ad; // initialize q2 = 2p/abs(d)
420 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
421 do {
422 p = p + 1;
423 q1 = 2*q1; // update q1 = 2p/abs(nc)
424 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
425 if (r1 >= anc) { // must be unsigned comparison
426 q1 = q1 + 1;
427 r1 = r1 - anc;
428 }
429 q2 = 2*q2; // update q2 = 2p/abs(d)
430 r2 = 2*r2; // update r2 = rem(2p/abs(d))
431 if (r2 >= ad) { // must be unsigned comparison
432 q2 = q2 + 1;
433 r2 = r2 - ad;
434 }
435 delta = ad - r2;
436 } while (q1 < delta || (q1 == delta && r1 == 0));
437
438 mag.m = q2 + 1;
439 if (d < 0) mag.m = -mag.m; // resulting magic number
440 mag.s = p - 64; // resulting shift
441 return mag;
442}
443
444/// magicu - calculate the magic numbers required to codegen an integer udiv as
445/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
446static struct mu magicu(uint64_t d)
447{
448 int64_t p;
449 uint64_t nc, delta, q1, r1, q2, r2;
450 struct mu magu;
451 magu.a = 0; // initialize "add" indicator
452 nc = - 1 - (-d)%d;
453 p = 31; // initialize p
Alkis Evlogimenosaeca5582005-04-06 22:09:40 +0000454 q1 = 0x8000000000000000ll/nc; // initialize q1 = 2p/nc
455 r1 = 0x8000000000000000ll - q1*nc; // initialize r1 = rem(2p,nc)
456 q2 = 0x7FFFFFFFFFFFFFFFll/d; // initialize q2 = (2p-1)/d
457 r2 = 0x7FFFFFFFFFFFFFFFll - q2*d; // initialize r2 = rem((2p-1),d)
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000458 do {
459 p = p + 1;
460 if (r1 >= nc - r1 ) {
461 q1 = 2*q1 + 1; // update q1
462 r1 = 2*r1 - nc; // update r1
463 }
464 else {
465 q1 = 2*q1; // update q1
466 r1 = 2*r1; // update r1
467 }
468 if (r2 + 1 >= d - r2) {
Alkis Evlogimenosaeca5582005-04-06 22:09:40 +0000469 if (q2 >= 0x7FFFFFFFFFFFFFFFll) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000470 q2 = 2*q2 + 1; // update q2
471 r2 = 2*r2 + 1 - d; // update r2
472 }
473 else {
Alkis Evlogimenosaeca5582005-04-06 22:09:40 +0000474 if (q2 >= 0x8000000000000000ll) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000475 q2 = 2*q2; // update q2
476 r2 = 2*r2 + 1; // update r2
477 }
478 delta = d - 1 - r2;
479 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
480 magu.m = q2 + 1; // resulting magic number
481 magu.s = p - 32; // resulting shift
482 return magu;
483}
484
485/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
486/// return a DAG expression to select that will generate the same value by
487/// multiplying by a magic number. See:
488/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
489SDOperand ISel::BuildSDIVSequence(SDOperand N) {
490 int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
491 ms magics = magic(d);
492 // Multiply the numerator (operand 0) by the magic value
493 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
494 ISelDAG->getConstant(magics.m, MVT::i64));
495 // If d > 0 and m < 0, add the numerator
496 if (d > 0 && magics.m < 0)
497 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
498 // If d < 0 and m > 0, subtract the numerator.
499 if (d < 0 && magics.m > 0)
500 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
501 // Shift right algebraic if shift value is nonzero
502 if (magics.s > 0)
503 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
504 ISelDAG->getConstant(magics.s, MVT::i64));
505 // Extract the sign bit and add it to the quotient
506 SDOperand T =
507 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
508 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
509}
510
511/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
512/// return a DAG expression to select that will generate the same value by
513/// multiplying by a magic number. See:
514/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
515SDOperand ISel::BuildUDIVSequence(SDOperand N) {
516 unsigned d =
517 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
518 mu magics = magicu(d);
519 // Multiply the numerator (operand 0) by the magic value
520 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
521 ISelDAG->getConstant(magics.m, MVT::i64));
522 if (magics.a == 0) {
523 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
524 ISelDAG->getConstant(magics.s, MVT::i64));
525 } else {
526 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
527 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
528 ISelDAG->getConstant(1, MVT::i64));
529 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
530 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
531 ISelDAG->getConstant(magics.s-1, MVT::i64));
532 }
533 return Q;
534}
535
Andrew Lenhartha565c272005-04-06 22:03:13 +0000536//From PPC32
537/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
538/// returns zero when the input is not exactly a power of two.
539static unsigned ExactLog2(uint64_t Val) {
540 if (Val == 0 || (Val & (Val-1))) return 0;
541 unsigned Count = 0;
542 while (Val != 1) {
543 Val >>= 1;
544 ++Count;
545 }
546 return Count;
547}
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000548
549
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000550//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000551static const int IMM_LOW = -32768;
552static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000553static const int IMM_MULT = 65536;
554
555static long getUpper16(long l)
556{
557 long y = l / IMM_MULT;
558 if (l % IMM_MULT > IMM_HIGH)
559 ++y;
560 return y;
561}
562
563static long getLower16(long l)
564{
565 long h = getUpper16(l);
566 return l - h * IMM_MULT;
567}
568
Andrew Lenharth65838902005-02-06 16:22:15 +0000569static unsigned GetSymVersion(unsigned opcode)
570{
571 switch (opcode) {
572 default: assert(0 && "unknown load or store"); return 0;
573 case Alpha::LDQ: return Alpha::LDQ_SYM;
574 case Alpha::LDS: return Alpha::LDS_SYM;
575 case Alpha::LDT: return Alpha::LDT_SYM;
576 case Alpha::LDL: return Alpha::LDL_SYM;
577 case Alpha::LDBU: return Alpha::LDBU_SYM;
578 case Alpha::LDWU: return Alpha::LDWU_SYM;
579 case Alpha::LDW: return Alpha::LDW_SYM;
580 case Alpha::LDB: return Alpha::LDB_SYM;
581 case Alpha::STQ: return Alpha::STQ_SYM;
582 case Alpha::STS: return Alpha::STS_SYM;
583 case Alpha::STT: return Alpha::STT_SYM;
584 case Alpha::STL: return Alpha::STL_SYM;
585 case Alpha::STW: return Alpha::STW_SYM;
586 case Alpha::STB: return Alpha::STB_SYM;
587 }
588}
589
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000590void ISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
591{
592 unsigned Opc;
593 if (EnableAlphaFTOI) {
594 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
595 BuildMI(BB, Opc, 1, dst).addReg(src);
596 } else {
597 //The hard way:
598 // Spill the integer to memory and reload it from there.
599 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
600 MachineFunction *F = BB->getParent();
601 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
602
603 Opc = isDouble ? Alpha::STT : Alpha::STS;
604 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
605 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
606 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
607 }
608}
609
610void ISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
611{
612 unsigned Opc;
613 if (EnableAlphaFTOI) {
614 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
615 BuildMI(BB, Opc, 1, dst).addReg(src);
616 } else {
617 //The hard way:
618 // Spill the integer to memory and reload it from there.
619 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
620 MachineFunction *F = BB->getParent();
621 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
622
623 Opc = isDouble ? Alpha::STQ : Alpha::STL;
624 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
625 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
626 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
627 }
628}
629
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000630bool ISel::SelectFPSetCC(SDOperand N, unsigned dst)
631{
632 SDNode *Node = N.Val;
633 unsigned Opc, Tmp1, Tmp2, Tmp3;
634 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node);
635
636 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
637 bool rev = false;
638 bool inv = false;
639
640 switch (SetCC->getCondition()) {
641 default: Node->dump(); assert(0 && "Unknown FP comparison!");
642 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
643 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
644 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
645 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
646 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
647 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
648 }
649
650 //FIXME: check for constant 0.0
651 ConstantFPSDNode *CN;
652 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
653 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
654 Tmp1 = Alpha::F31;
655 else
656 Tmp1 = SelectExpr(N.getOperand(0));
657
658 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
659 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
660 Tmp2 = Alpha::F31;
661 else
662 Tmp2 = SelectExpr(N.getOperand(1));
663
664 //Can only compare doubles, and dag won't promote for me
665 if (SetCC->getOperand(0).getValueType() == MVT::f32)
666 {
667 //assert(0 && "Setcc On float?\n");
668 std::cerr << "Setcc on float!\n";
669 Tmp3 = MakeReg(MVT::f64);
670 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
671 Tmp1 = Tmp3;
672 }
673 if (SetCC->getOperand(1).getValueType() == MVT::f32)
674 {
675 //assert (0 && "Setcc On float?\n");
676 std::cerr << "Setcc on float!\n";
677 Tmp3 = MakeReg(MVT::f64);
678 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
679 Tmp2 = Tmp3;
680 }
681
682 if (rev) std::swap(Tmp1, Tmp2);
683 //do the comparison
684 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
685 return inv;
686}
687
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000688//Check to see if the load is a constant offset from a base register
689void ISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
690{
691 unsigned opcode = N.getOpcode();
692 if (opcode == ISD::ADD) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000693 if(N.getOperand(1).getOpcode() == ISD::Constant &&
694 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
695 { //Normal imm add
696 Reg = SelectExpr(N.getOperand(0));
697 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
698 return;
699 }
700 else if(N.getOperand(0).getOpcode() == ISD::Constant &&
701 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 32767)
702 {
703 Reg = SelectExpr(N.getOperand(1));
704 offset = cast<ConstantSDNode>(N.getOperand(0))->getValue();
705 return;
706 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000707 }
708 Reg = SelectExpr(N);
709 offset = 0;
710 return;
711}
712
Andrew Lenharth445171a2005-02-08 00:40:03 +0000713void ISel::SelectBranchCC(SDOperand N)
714{
715 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000716 MachineBasicBlock *Dest =
717 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
718 unsigned Opc = Alpha::WTF;
719
Andrew Lenharth445171a2005-02-08 00:40:03 +0000720 Select(N.getOperand(0)); //chain
721 SDOperand CC = N.getOperand(1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000722
Andrew Lenharth445171a2005-02-08 00:40:03 +0000723 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000724 {
725 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
726 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
727 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000728 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
729 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
730 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
731 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000732 bool isNE = false;
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000733
734 //Fix up CC
735 ISD::CondCode cCode= SetCC->getCondition();
736 if (LeftZero && !RightZero) //Swap Operands
737 cCode = ISD::getSetCCSwappedOperands(cCode);
738
739 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000740 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000741
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000742 if (LeftZero || RightZero) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000743 switch (SetCC->getCondition()) {
744 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
745 case ISD::SETEQ: Opc = Alpha::BEQ; break;
746 case ISD::SETLT: Opc = Alpha::BLT; break;
747 case ISD::SETLE: Opc = Alpha::BLE; break;
748 case ISD::SETGT: Opc = Alpha::BGT; break;
749 case ISD::SETGE: Opc = Alpha::BGE; break;
750 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
751 case ISD::SETUGT: Opc = Alpha::BNE; break;
752 case ISD::SETULE: Opc = Alpha::BEQ; break; //Technically you could have this CC
753 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
754 case ISD::SETNE: Opc = Alpha::BNE; break;
755 }
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000756 unsigned Tmp1;
757 if(LeftZero && !RightZero) //swap Operands
758 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
759 else
760 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000761 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
762 return;
763 } else {
764 unsigned Tmp1 = SelectExpr(CC);
765 if (isNE)
766 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
767 else
768 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000769 return;
770 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000771 } else { //FP
772 //Any comparison between 2 values should be codegened as an folded branch, as moving
773 //CC to the integer register is very expensive
774 //for a cmp b: c = a - b;
775 //a = b: c = 0
776 //a < b: c < 0
777 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000778
779 bool invTest = false;
780 unsigned Tmp3;
781
782 ConstantFPSDNode *CN;
783 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
784 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
785 Tmp3 = SelectExpr(SetCC->getOperand(0));
786 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
787 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
788 {
789 Tmp3 = SelectExpr(SetCC->getOperand(1));
790 invTest = true;
791 }
792 else
793 {
794 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
795 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
796 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
797 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
798 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
799 .addReg(Tmp1).addReg(Tmp2);
800 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000801
802 switch (SetCC->getCondition()) {
803 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000804 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
805 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
806 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
807 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
808 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
809 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000810 }
811 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000812 return;
813 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000814 abort(); //Should never be reached
815 } else {
816 //Giveup and do the stupid thing
817 unsigned Tmp1 = SelectExpr(CC);
818 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
819 return;
820 }
Andrew Lenharth445171a2005-02-08 00:40:03 +0000821 abort(); //Should never be reached
822}
823
Andrew Lenharth40831c52005-01-28 06:57:18 +0000824unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
825{
826 unsigned Tmp1, Tmp2, Tmp3;
827 unsigned Opc = 0;
828 SDNode *Node = N.Val;
829 MVT::ValueType DestType = N.getValueType();
830 unsigned opcode = N.getOpcode();
831
832 switch (opcode) {
833 default:
834 Node->dump();
835 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +0000836
Andrew Lenharth7332f3e2005-04-02 19:11:07 +0000837 case ISD::UNDEF: {
838 BuildMI(BB, Alpha::IDEF, 0, Result);
839 return Result;
840 }
841
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000842 case ISD::FNEG:
843 if(ISD::FABS == N.getOperand(0).getOpcode())
844 {
845 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth7332f3e2005-04-02 19:11:07 +0000846 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000847 } else {
848 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth7332f3e2005-04-02 19:11:07 +0000849 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000850 }
851 return Result;
852
853 case ISD::FABS:
854 Tmp1 = SelectExpr(N.getOperand(0));
855 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
856 return Result;
857
Andrew Lenharth9818c052005-02-05 13:19:12 +0000858 case ISD::SELECT:
859 {
Andrew Lenharth45859692005-03-03 21:47:53 +0000860 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
861 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
862 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
863
864 SDOperand CC = N.getOperand(0);
865 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
866
867 if (CC.getOpcode() == ISD::SETCC &&
868 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
869 { //FP Setcc -> Select yay!
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000870
871
Andrew Lenharth45859692005-03-03 21:47:53 +0000872 //for a cmp b: c = a - b;
873 //a = b: c = 0
874 //a < b: c < 0
875 //a > b: c > 0
876
877 bool invTest = false;
878 unsigned Tmp3;
879
880 ConstantFPSDNode *CN;
881 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
882 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
883 Tmp3 = SelectExpr(SetCC->getOperand(0));
884 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
885 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
886 {
887 Tmp3 = SelectExpr(SetCC->getOperand(1));
888 invTest = true;
889 }
890 else
891 {
892 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
893 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
894 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
895 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
896 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
897 .addReg(Tmp1).addReg(Tmp2);
898 }
899
900 switch (SetCC->getCondition()) {
901 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
902 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
903 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
904 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
905 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
906 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
907 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
908 }
Andrew Lenharth33819132005-03-04 20:09:23 +0000909 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
Andrew Lenharth45859692005-03-03 21:47:53 +0000910 return Result;
911 }
912 else
913 {
914 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000915 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV).addReg(Tmp1);
916// // Spill the cond to memory and reload it from there.
917// unsigned Tmp4 = MakeReg(MVT::f64);
918// MoveIntFP(Tmp1, Tmp4, true);
919// //now ideally, we don't have to do anything to the flag...
920// // Get the condition into the zero flag.
921// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
Andrew Lenharth45859692005-03-03 21:47:53 +0000922 return Result;
923 }
Andrew Lenharth9818c052005-02-05 13:19:12 +0000924 }
925
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000926 case ISD::FP_ROUND:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000927 assert (DestType == MVT::f32 &&
928 N.getOperand(0).getValueType() == MVT::f64 &&
929 "only f64 to f32 conversion supported here");
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000930 Tmp1 = SelectExpr(N.getOperand(0));
931 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
932 return Result;
933
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000934 case ISD::FP_EXTEND:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000935 assert (DestType == MVT::f64 &&
936 N.getOperand(0).getValueType() == MVT::f32 &&
937 "only f32 to f64 conversion supported here");
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000938 Tmp1 = SelectExpr(N.getOperand(0));
939 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
940 return Result;
941
Andrew Lenharth2c594352005-01-29 15:42:07 +0000942 case ISD::CopyFromReg:
943 {
944 // Make sure we generate both values.
945 if (Result != notIn)
946 ExprMap[N.getValue(1)] = notIn; // Generate the token
947 else
948 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
949
950 SDOperand Chain = N.getOperand(0);
951
952 Select(Chain);
953 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
954 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
955 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
956 return Result;
957 }
958
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000959 case ISD::LOAD:
960 {
961 // Make sure we generate both values.
962 if (Result != notIn)
963 ExprMap[N.getValue(1)] = notIn; // Generate the token
964 else
965 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000966
Andrew Lenharth29219162005-02-07 06:31:44 +0000967 DestType = N.getValue(0).getValueType();
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000968
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000969 SDOperand Chain = N.getOperand(0);
970 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000971 Select(Chain);
Andrew Lenharth65838902005-02-06 16:22:15 +0000972 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
973
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000974 if (Address.getOpcode() == ISD::GlobalAddress) {
975 AlphaLowering.restoreGP(BB);
976 Opc = GetSymVersion(Opc);
977 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
978 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000979 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000980 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000981 Opc = GetSymVersion(Opc);
Andrew Lenharth97127a12005-02-05 17:41:39 +0000982 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000983 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000984 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000985 BuildMI(BB, Opc, 2, Result)
986 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
987 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000988 } else {
989 long offset;
990 SelectAddr(Address, Tmp1, offset);
991 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
992 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000993 return Result;
994 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000995 case ISD::ConstantFP:
996 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
997 if (CN->isExactlyValue(+0.0)) {
998 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000999 } else if ( CN->isExactlyValue(-0.0)) {
1000 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001001 } else {
1002 abort();
1003 }
1004 }
1005 return Result;
1006
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001007 case ISD::SDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001008 case ISD::MUL:
1009 case ISD::ADD:
1010 case ISD::SUB:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001011 switch( opcode ) {
1012 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
1013 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
1014 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
1015 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
1016 };
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001017
1018 ConstantFPSDNode *CN;
1019 if (opcode == ISD::SUB
1020 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1021 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1022 {
1023 Tmp2 = SelectExpr(N.getOperand(1));
1024 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1025 } else {
1026 Tmp1 = SelectExpr(N.getOperand(0));
1027 Tmp2 = SelectExpr(N.getOperand(1));
1028 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1029 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001030 return Result;
1031
Andrew Lenharth2c594352005-01-29 15:42:07 +00001032 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001033 {
1034 //include a conversion sequence for float loads to double
1035 if (Result != notIn)
1036 ExprMap[N.getValue(1)] = notIn; // Generate the token
1037 else
1038 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1039
Andrew Lenhartha549deb2005-02-07 05:33:15 +00001040 Tmp1 = MakeReg(MVT::f32);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001041
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001042 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
1043 "EXTLOAD not from f32");
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001044 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
1045
1046 SDOperand Chain = N.getOperand(0);
1047 SDOperand Address = N.getOperand(1);
1048 Select(Chain);
1049
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001050 if (Address.getOpcode() == ISD::GlobalAddress) {
1051 AlphaLowering.restoreGP(BB);
1052 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1053 }
1054 else if (ConstantPoolSDNode *CP =
1055 dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
1056 {
1057 AlphaLowering.restoreGP(BB);
1058 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addConstantPoolIndex(CP->getIndex());
1059 }
1060 else if(Address.getOpcode() == ISD::FrameIndex) {
1061 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
Andrew Lenharth032f2352005-02-22 21:59:48 +00001062 BuildMI(BB, Alpha::LDS, 2, Tmp1)
1063 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1064 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001065 } else {
1066 long offset;
1067 SelectAddr(Address, Tmp2, offset);
1068 BuildMI(BB, Alpha::LDS, 1, Tmp1).addImm(offset).addReg(Tmp2);
1069 }
Andrew Lenharth29219162005-02-07 06:31:44 +00001070 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001071 return Result;
1072 }
Andrew Lenharth2c594352005-01-29 15:42:07 +00001073
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001074 case ISD::UINT_TO_FP:
1075 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001076 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001077 assert (N.getOperand(0).getValueType() == MVT::i64
1078 && "only quads can be loaded from");
Andrew Lenharth40831c52005-01-28 06:57:18 +00001079 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001080 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001081 MoveInt2FP(Tmp1, Tmp2, true);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001082 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1083 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001084 return Result;
1085 }
1086 }
1087 assert(0 && "should not get here");
1088 return 0;
1089}
1090
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001091unsigned ISel::SelectExpr(SDOperand N) {
1092 unsigned Result;
1093 unsigned Tmp1, Tmp2, Tmp3;
1094 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001095 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001096
1097 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001098 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001099
1100 unsigned &Reg = ExprMap[N];
1101 if (Reg) return Reg;
1102
1103 if (N.getOpcode() != ISD::CALL)
1104 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001105 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001106 else {
1107 // If this is a call instruction, make sure to prepare ALL of the result
1108 // values as well as the chain.
1109 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001110 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001111 else {
1112 Result = MakeReg(Node->getValueType(0));
1113 ExprMap[N.getValue(0)] = Result;
1114 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1115 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001116 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001117 }
1118 }
1119
Andrew Lenharth22088bb2005-02-02 15:05:33 +00001120 if (DestType == MVT::f64 || DestType == MVT::f32 ||
Andrew Lenharth06342c32005-02-07 06:21:37 +00001121 (
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001122 (opcode == ISD::LOAD || opcode == ISD::CopyFromReg ||
1123 opcode == ISD::EXTLOAD) &&
1124 (N.getValue(0).getValueType() == MVT::f32 ||
1125 N.getValue(0).getValueType() == MVT::f64)
Andrew Lenharth06342c32005-02-07 06:21:37 +00001126 )
1127 )
Andrew Lenharth40831c52005-01-28 06:57:18 +00001128 return SelectExprFP(N, Result);
1129
1130 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001131 default:
1132 Node->dump();
1133 assert(0 && "Node not handled!\n");
1134
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001135 case ISD::MULHU:
1136 Tmp1 = SelectExpr(N.getOperand(0));
1137 Tmp2 = SelectExpr(N.getOperand(1));
1138 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +00001139 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001140 case ISD::MULHS:
1141 {
1142 //MULHU - Ra<63>*Rb - Rb<63>*Ra
1143 Tmp1 = SelectExpr(N.getOperand(0));
1144 Tmp2 = SelectExpr(N.getOperand(1));
1145 Tmp3 = MakeReg(MVT::i64);
1146 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1147 unsigned V1 = MakeReg(MVT::i64);
1148 unsigned V2 = MakeReg(MVT::i64);
1149 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31).addReg(Tmp1);
1150 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31).addReg(Tmp2);
1151 unsigned IRes = MakeReg(MVT::i64);
1152 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
1153 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
1154 return Result;
1155 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001156 case ISD::UNDEF: {
1157 BuildMI(BB, Alpha::IDEF, 0, Result);
1158 return Result;
1159 }
1160
Andrew Lenharth032f2352005-02-22 21:59:48 +00001161 case ISD::DYNAMIC_STACKALLOC:
1162 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +00001163 if (Result != notIn)
1164 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +00001165 else
1166 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1167
1168 // FIXME: We are currently ignoring the requested alignment for handling
1169 // greater than the stack alignment. This will need to be revisited at some
1170 // point. Align = N.getOperand(2);
1171
1172 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1173 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1174 std::cerr << "Cannot allocate stack object with greater alignment than"
1175 << " the stack alignment yet!";
1176 abort();
1177 }
1178
1179 Select(N.getOperand(0));
1180 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1181 {
1182 if (CN->getValue() < 32000)
1183 {
1184 BuildMI(BB, Alpha::LDA, 2, Alpha::R30)
1185 .addImm(-CN->getValue()).addReg(Alpha::R30);
1186 } else {
1187 Tmp1 = SelectExpr(N.getOperand(1));
1188 // Subtract size from stack pointer, thereby allocating some space.
1189 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1190 }
1191 } else {
1192 Tmp1 = SelectExpr(N.getOperand(1));
1193 // Subtract size from stack pointer, thereby allocating some space.
1194 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1195 }
1196
1197 // Put a pointer to the space into the result register, by copying the stack
1198 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +00001199 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +00001200 return Result;
1201
Andrew Lenharth33819132005-03-04 20:09:23 +00001202// case ISD::ConstantPool:
1203// Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1204// AlphaLowering.restoreGP(BB);
1205// BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
1206// return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +00001207
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001208 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001209 BuildMI(BB, Alpha::LDA, 2, Result)
1210 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1211 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001212 return Result;
1213
1214 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001215 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001216 case ISD::SEXTLOAD:
Andrew Lenhartha549deb2005-02-07 05:33:15 +00001217 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001218 {
1219 // Make sure we generate both values.
1220 if (Result != notIn)
1221 ExprMap[N.getValue(1)] = notIn; // Generate the token
1222 else
1223 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001224
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001225 SDOperand Chain = N.getOperand(0);
1226 SDOperand Address = N.getOperand(1);
1227 Select(Chain);
1228
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001229 assert(Node->getValueType(0) == MVT::i64 &&
1230 "Unknown type to sign extend to.");
Andrew Lenharth03824012005-02-07 05:55:55 +00001231 if (opcode == ISD::LOAD)
1232 Opc = Alpha::LDQ;
1233 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001234 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
1235 default: Node->dump(); assert(0 && "Bad sign extend!");
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001236 case MVT::i32: Opc = Alpha::LDL;
1237 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
1238 case MVT::i16: Opc = Alpha::LDWU;
1239 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001240 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001241 case MVT::i8: Opc = Alpha::LDBU;
1242 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001243 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001244
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001245 if (Address.getOpcode() == ISD::GlobalAddress) {
1246 AlphaLowering.restoreGP(BB);
1247 Opc = GetSymVersion(Opc);
1248 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1249 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001250 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1251 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001252 Opc = GetSymVersion(Opc);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001253 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001254 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001255 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001256 BuildMI(BB, Opc, 2, Result)
1257 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1258 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001259 } else {
1260 long offset;
1261 SelectAddr(Address, Tmp1, offset);
1262 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1263 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001264 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001265 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001266
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001267 case ISD::GlobalAddress:
1268 AlphaLowering.restoreGP(BB);
1269 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
1270 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
1271 return Result;
1272
1273 case ISD::CALL:
1274 {
1275 Select(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001276
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001277 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001278 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001279
1280 //grab the arguments
1281 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001282 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001283 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001284 argvregs.push_back(SelectExpr(N.getOperand(i)));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001285
Andrew Lenharth684f2292005-01-30 00:35:27 +00001286 //in reg args
1287 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001288 {
1289 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
1290 Alpha::R19, Alpha::R20, Alpha::R21};
1291 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
1292 Alpha::F19, Alpha::F20, Alpha::F21};
1293 switch(N.getOperand(i+2).getValueType()) {
1294 default:
1295 Node->dump();
1296 N.getOperand(i).Val->dump();
1297 std::cerr << "Type for " << i << " is: " <<
1298 N.getOperand(i+2).getValueType() << "\n";
1299 assert(0 && "Unknown value type for call");
1300 case MVT::i1:
1301 case MVT::i8:
1302 case MVT::i16:
1303 case MVT::i32:
1304 case MVT::i64:
1305 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1306 break;
1307 case MVT::f32:
1308 case MVT::f64:
1309 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1310 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001311 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001312 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001313 //in mem args
1314 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001315 {
1316 switch(N.getOperand(i+2).getValueType()) {
1317 default:
1318 Node->dump();
1319 N.getOperand(i).Val->dump();
1320 std::cerr << "Type for " << i << " is: " <<
1321 N.getOperand(i+2).getValueType() << "\n";
1322 assert(0 && "Unknown value type for call");
1323 case MVT::i1:
1324 case MVT::i8:
1325 case MVT::i16:
1326 case MVT::i32:
1327 case MVT::i64:
1328 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1329 break;
1330 case MVT::f32:
1331 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1332 break;
1333 case MVT::f64:
1334 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1335 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001336 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001337 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001338 //build the right kind of call
1339 if (GlobalAddressSDNode *GASD =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001340 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001341 {
Andrew Lenharth3e315922005-02-10 20:10:38 +00001342 //if (GASD->getGlobal()->isExternal()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001343 //use safe calling convention
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001344 AlphaLowering.restoreGP(BB);
1345 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal(),true);
Andrew Lenharth3e315922005-02-10 20:10:38 +00001346 //} else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001347 //use PC relative branch call
Andrew Lenharth3e315922005-02-10 20:10:38 +00001348 //BuildMI(BB, Alpha::BSR, 1, Alpha::R26).addGlobalAddress(GASD->getGlobal(),true);
1349 //}
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001350 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001351 else if (ExternalSymbolSDNode *ESSDN =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001352 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001353 {
1354 AlphaLowering.restoreGP(BB);
Andrew Lenharthba05ad62005-03-30 18:22:52 +00001355 BuildMI(BB, Alpha::CALL, 1).addExternalSymbol(ESSDN->getSymbol(), true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001356 } else {
1357 //no need to restore GP as we are doing an indirect call
1358 Tmp1 = SelectExpr(N.getOperand(1));
1359 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1360 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1361 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001362
1363 //push the result into a virtual register
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001364
1365 switch (Node->getValueType(0)) {
1366 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001367 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001368 case MVT::i1:
1369 case MVT::i8:
1370 case MVT::i16:
1371 case MVT::i32:
1372 case MVT::i64:
1373 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1374 break;
1375 case MVT::f32:
1376 case MVT::f64:
1377 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1378 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001379 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001380 return Result+N.ResNo;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001381 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001382
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001383 case ISD::SIGN_EXTEND_INREG:
1384 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001385 //do SDIV opt for all levels of ints
Andrew Lenharth5e99dd92005-03-31 22:02:25 +00001386 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001387 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001388 unsigned Tmp4 = MakeReg(MVT::f64);
1389 unsigned Tmp5 = MakeReg(MVT::f64);
1390 unsigned Tmp6 = MakeReg(MVT::f64);
1391 unsigned Tmp7 = MakeReg(MVT::f64);
1392 unsigned Tmp8 = MakeReg(MVT::f64);
1393 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001394
1395 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1396 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1397 MoveInt2FP(Tmp1, Tmp4, true);
1398 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001399 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Tmp4);
1400 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Tmp5);
1401 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
1402 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001403 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001404 return Result;
1405 }
1406
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001407 //Alpha has instructions for a bunch of signed 32 bit stuff
1408 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001409 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001410 switch (N.getOperand(0).getOpcode()) {
1411 case ISD::ADD:
1412 case ISD::SUB:
1413 case ISD::MUL:
1414 {
1415 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1416 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1417 //FIXME: first check for Scaled Adds and Subs!
1418 if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1419 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
1420 { //Normal imm add/sub
1421 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth093f3272005-02-12 21:11:17 +00001422 //if the value was really originally a i32, skip the up conversion
1423 if (N.getOperand(0).getOperand(0).getOpcode() == ISD::SIGN_EXTEND_INREG &&
1424 dyn_cast<MVTSDNode>(N.getOperand(0).getOperand(0).Val)
1425 ->getExtraValueType() == MVT::i32)
1426 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1427 else
1428 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001429 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1430 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001431 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001432 else
1433 { //Normal add/sub
1434 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULLi : Alpha::SUBL);
Andrew Lenharth093f3272005-02-12 21:11:17 +00001435 //if the value was really originally a i32, skip the up conversion
1436 if (N.getOperand(0).getOperand(0).getOpcode() == ISD::SIGN_EXTEND_INREG &&
1437 dyn_cast<MVTSDNode>(N.getOperand(0).getOperand(0).Val)
1438 ->getExtraValueType() == MVT::i32)
1439 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1440 else
1441 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1442 //if the value was really originally a i32, skip the up conversion
1443 if (N.getOperand(0).getOperand(1).getOpcode() == ISD::SIGN_EXTEND_INREG &&
1444 dyn_cast<MVTSDNode>(N.getOperand(0).getOperand(1).Val)
1445 ->getExtraValueType() == MVT::i32)
1446 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1447 else
1448 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1449
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001450 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1451 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1452 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1453 }
1454 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001455 }
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001456 case ISD::SEXTLOAD:
1457 //SelectionDag isn't deleting the signextend after sextloads
1458 Reg = Result = SelectExpr(N.getOperand(0));
1459 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001460 default: break; //Fall Though;
1461 }
1462 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001463 Tmp1 = SelectExpr(N.getOperand(0));
1464 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001465 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001466 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001467 {
1468 default:
1469 Node->dump();
1470 assert(0 && "Sign Extend InReg not there yet");
1471 break;
1472 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001473 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001474 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001475 break;
1476 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001477 case MVT::i16:
1478 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
1479 break;
1480 case MVT::i8:
1481 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
1482 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001483 case MVT::i1:
1484 Tmp2 = MakeReg(MVT::i64);
1485 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenharth7536eea2005-02-12 20:42:09 +00001486 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::F31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001487 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001488 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001489 return Result;
1490 }
1491 case ISD::ZERO_EXTEND_INREG:
1492 {
1493 Tmp1 = SelectExpr(N.getOperand(0));
1494 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001495 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001496 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001497 {
1498 default:
1499 Node->dump();
1500 assert(0 && "Zero Extend InReg not there yet");
1501 break;
1502 case MVT::i32: Tmp2 = 0xf0; break;
1503 case MVT::i16: Tmp2 = 0xfc; break;
1504 case MVT::i8: Tmp2 = 0xfe; break;
1505 case MVT::i1: //handle this one special
1506 BuildMI(BB, Alpha::ANDi, 2, Result).addReg(Tmp1).addImm(1);
1507 return Result;
1508 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001509 BuildMI(BB, Alpha::ZAPi, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001510 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001511 }
1512
1513 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001514 {
1515 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1516 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1517 bool isConst1 = false;
1518 bool isConst2 = false;
1519 int dir;
Andrew Lenharth9818c052005-02-05 13:19:12 +00001520
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001521 //Tmp1 = SelectExpr(N.getOperand(0));
1522 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001523 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1524 isConst1 = true;
1525 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001526 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1527 isConst2 = true;
1528
1529 switch (SetCC->getCondition()) {
1530 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1531 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001532 case ISD::SETLT:
1533 Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
1534 case ISD::SETLE:
1535 Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
1536 case ISD::SETGT:
1537 Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
1538 case ISD::SETGE:
1539 Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
1540 case ISD::SETULT:
1541 Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
1542 case ISD::SETUGT:
1543 Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
1544 case ISD::SETULE:
1545 Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
1546 case ISD::SETUGE:
1547 Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001548 case ISD::SETNE: {//Handle this one special
1549 //std::cerr << "Alpha does not have a setne.\n";
1550 //abort();
1551 Tmp1 = SelectExpr(N.getOperand(0));
1552 Tmp2 = SelectExpr(N.getOperand(1));
1553 Tmp3 = MakeReg(MVT::i64);
1554 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001555 //Remeber we have the Inv for this CC
1556 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001557 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001558 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001559 return Result;
1560 }
1561 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001562 if (dir == 1) {
1563 Tmp1 = SelectExpr(N.getOperand(0));
1564 if (isConst2) {
1565 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1566 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1567 } else {
1568 Tmp2 = SelectExpr(N.getOperand(1));
1569 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1570 }
1571 } else if (dir == 2) {
1572 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001573 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001574 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1575 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1576 } else {
1577 Tmp2 = SelectExpr(N.getOperand(0));
1578 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1579 }
1580 } else { //dir == 0
1581 if (isConst1) {
1582 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1583 Tmp2 = SelectExpr(N.getOperand(1));
1584 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
1585 } else if (isConst2) {
1586 Tmp1 = SelectExpr(N.getOperand(0));
1587 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1588 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1589 } else {
1590 Tmp1 = SelectExpr(N.getOperand(0));
1591 Tmp2 = SelectExpr(N.getOperand(1));
1592 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1593 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001594 }
1595 } else {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001596 //do the comparison
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001597 Tmp1 = MakeReg(MVT::f64);
1598 bool inv = SelectFPSetCC(N, Tmp1);
1599
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001600 //now arrange for Result (int) to have a 1 or 0
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001601 Tmp2 = MakeReg(MVT::i64);
1602 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001603 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001604 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001605 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001606 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001607 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001608 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001609
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001610 case ISD::CopyFromReg:
1611 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001612 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001613 if (Result != notIn)
1614 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001615 else
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001616 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth40831c52005-01-28 06:57:18 +00001617
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001618 SDOperand Chain = N.getOperand(0);
1619
1620 Select(Chain);
1621 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1622 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1623 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1624 return Result;
1625 }
1626
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001627 //Most of the plain arithmetic and logic share the same form, and the same
1628 //constant immediate test
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001629 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001630 //Match Not
1631 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1632 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue())
1633 {
1634 Tmp1 = SelectExpr(N.getOperand(0));
1635 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1636 return Result;
1637 }
1638 //Fall through
1639 case ISD::AND:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001640 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001641 //Check operand(0) == Not
1642 if (N.getOperand(0).getOpcode() == ISD::OR &&
1643 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1644 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue())
1645 {
1646 switch(opcode) {
1647 case ISD::AND: Opc = Alpha::BIC; break;
1648 case ISD::OR: Opc = Alpha::ORNOT; break;
1649 case ISD::XOR: Opc = Alpha::EQV; break;
1650 }
1651 Tmp1 = SelectExpr(N.getOperand(1));
1652 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1653 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1654 return Result;
1655 }
1656 //Check operand(1) == Not
1657 if (N.getOperand(1).getOpcode() == ISD::OR &&
1658 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
1659 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->isAllOnesValue())
1660 {
1661 switch(opcode) {
1662 case ISD::AND: Opc = Alpha::BIC; break;
1663 case ISD::OR: Opc = Alpha::ORNOT; break;
1664 case ISD::XOR: Opc = Alpha::EQV; break;
1665 }
1666 Tmp1 = SelectExpr(N.getOperand(0));
1667 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1668 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1669 return Result;
1670 }
1671 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001672 case ISD::SHL:
1673 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001674 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001675 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001676 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1677 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001678 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001679 {
1680 switch(opcode) {
1681 case ISD::AND: Opc = Alpha::ANDi; break;
1682 case ISD::OR: Opc = Alpha::BISi; break;
1683 case ISD::XOR: Opc = Alpha::XORi; break;
1684 case ISD::SHL: Opc = Alpha::SLi; break;
1685 case ISD::SRL: Opc = Alpha::SRLi; break;
1686 case ISD::SRA: Opc = Alpha::SRAi; break;
1687 case ISD::MUL: Opc = Alpha::MULQi; break;
1688 };
1689 Tmp1 = SelectExpr(N.getOperand(0));
1690 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1691 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1692 } else {
1693 switch(opcode) {
1694 case ISD::AND: Opc = Alpha::AND; break;
1695 case ISD::OR: Opc = Alpha::BIS; break;
1696 case ISD::XOR: Opc = Alpha::XOR; break;
1697 case ISD::SHL: Opc = Alpha::SL; break;
1698 case ISD::SRL: Opc = Alpha::SRL; break;
1699 case ISD::SRA: Opc = Alpha::SRA; break;
1700 case ISD::MUL: Opc = Alpha::MULQ; break;
1701 };
1702 Tmp1 = SelectExpr(N.getOperand(0));
1703 Tmp2 = SelectExpr(N.getOperand(1));
1704 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1705 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001706 return Result;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001707
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001708 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001709 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001710 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001711 bool isAdd = opcode == ISD::ADD;
1712
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001713 //first check for Scaled Adds and Subs!
1714 //Valid for add and sub
1715 if(N.getOperand(0).getOpcode() == ISD::SHL &&
1716 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1717 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() == 2)
1718 {
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001719 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001720 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1721 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1722 BuildMI(BB, isAdd?Alpha::S4ADDQi:Alpha::S4SUBQi, 2, Result).addReg(Tmp2)
1723 .addImm(cast<ConstantSDNode>(N.getOperand(1))->getValue());
1724 else {
1725 Tmp1 = SelectExpr(N.getOperand(1));
1726 BuildMI(BB, isAdd?Alpha::S4ADDQ:Alpha::S4SUBQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
1727 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001728 }
1729 else if(N.getOperand(0).getOpcode() == ISD::SHL &&
1730 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1731 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() == 3)
1732 {
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001733 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001734 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1735 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1736 BuildMI(BB, isAdd?Alpha::S8ADDQi:Alpha::S8SUBQi, 2, Result).addReg(Tmp2)
1737 .addImm(cast<ConstantSDNode>(N.getOperand(1))->getValue());
1738 else {
1739 Tmp1 = SelectExpr(N.getOperand(1));
1740 BuildMI(BB, isAdd?Alpha::S8ADDQ:Alpha::S8SUBQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
1741 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001742 }
1743 //Position prevents subs
1744 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &
1745 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
1746 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue() == 2)
1747 {
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001748 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001749 if (N.getOperand(0).getOpcode() == ISD::Constant &&
1750 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1751 BuildMI(BB, Alpha::S4ADDQi, 2, Result).addReg(Tmp2)
1752 .addImm(cast<ConstantSDNode>(N.getOperand(0))->getValue());
1753 else {
1754 Tmp1 = SelectExpr(N.getOperand(0));
1755 BuildMI(BB, Alpha::S4ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
1756 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001757 }
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001758 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001759 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
1760 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue() == 3)
1761 {
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001762 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001763 if (N.getOperand(0).getOpcode() == ISD::Constant &&
1764 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1765 BuildMI(BB, Alpha::S8ADDQi, 2, Result).addReg(Tmp2)
1766 .addImm(cast<ConstantSDNode>(N.getOperand(0))->getValue());
1767 else {
1768 Tmp1 = SelectExpr(N.getOperand(0));
1769 BuildMI(BB, Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
1770 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001771 }
1772 //small addi
1773 else if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001774 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001775 { //Normal imm add/sub
1776 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1777 Tmp1 = SelectExpr(N.getOperand(0));
1778 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1779 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1780 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001781 //larger addi
Andrew Lenharth40831c52005-01-28 06:57:18 +00001782 else if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001783 (cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767 ||
1784 (long)cast<ConstantSDNode>(N.getOperand(1))->getValue() >= -32767))
1785 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001786 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001787 Tmp2 = (long)cast<ConstantSDNode>(N.getOperand(1))->getValue();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001788 if (!isAdd)
1789 Tmp2 = -Tmp2;
1790 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001791 }
1792 //give up and do the operation
1793 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001794 //Normal add/sub
1795 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1796 Tmp1 = SelectExpr(N.getOperand(0));
1797 Tmp2 = SelectExpr(N.getOperand(1));
1798 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1799 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001800 return Result;
1801 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001802
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001803 case ISD::SDIV:
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001804 {
Andrew Lenhartha565c272005-04-06 22:03:13 +00001805 ConstantSDNode* CSD;
1806 //check if we can convert into a shift!
1807 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1808 (int64_t)CSD->getSignExtended() != 0 &&
1809 ExactLog2(abs((int64_t)CSD->getSignExtended())) != 0)
1810 {
1811 unsigned k = ExactLog2(abs(CSD->getSignExtended()));
1812 Tmp1 = SelectExpr(N.getOperand(0));
1813 Tmp2 = MakeReg(MVT::i64);
1814 if (k == 1)
1815 Tmp2 = Tmp1;
1816 else
1817 {
1818 Tmp2 = MakeReg(MVT::i64);
1819 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1820 }
1821 Tmp3 = MakeReg(MVT::i64);
1822 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1823 unsigned Tmp4 = MakeReg(MVT::i64);
1824 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
1825 if ((int64_t)CSD->getSignExtended() > 0)
1826 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
1827 else
1828 {
1829 unsigned Tmp5 = MakeReg(MVT::i64);
1830 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
1831 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
1832 }
1833 return Result;
1834 }
1835 }
1836 //Else fall through
1837
1838 case ISD::UDIV:
1839 {
1840 ConstantSDNode* CSD;
1841 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1842 ((int64_t)CSD->getSignExtended() >= 2 ||
1843 (int64_t)CSD->getSignExtended() <= -2))
1844 {
1845 // If this is a divide by constant, we can emit code using some magic
1846 // constants to implement it as a multiply instead.
1847 ExprMap.erase(N);
1848 if (opcode == ISD::SDIV)
1849 return SelectExpr(BuildSDIVSequence(N));
1850 else
1851 return SelectExpr(BuildUDIVSequence(N));
1852 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001853 }
1854 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001855 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00001856 case ISD::SREM:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001857 //FIXME: alpha really doesn't support any of these operations,
1858 // the ops are expanded into special library calls with
1859 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001860 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00001861 switch(opcode) {
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001862 case ISD::UREM: Opc = Alpha::REMQU; break;
1863 case ISD::SREM: Opc = Alpha::REMQ; break;
1864 case ISD::UDIV: Opc = Alpha::DIVQU; break;
1865 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001866 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001867 Tmp1 = SelectExpr(N.getOperand(0));
1868 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth33819132005-03-04 20:09:23 +00001869 //set up regs explicitly (helps Reg alloc)
1870 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
1871 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001872 AlphaLowering.restoreGP(BB);
Andrew Lenharth33819132005-03-04 20:09:23 +00001873 BuildMI(BB, Opc, 2).addReg(Alpha::R24).addReg(Alpha::R25);
1874 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001875 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001876
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001877 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001878 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001879 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001880 assert (DestType == MVT::i64 && "only quads can be loaded to");
1881 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001882 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001883 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001884 if (SrcType == MVT::f32)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001885 {
1886 Tmp2 = MakeReg(MVT::f64);
1887 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1888 Tmp1 = Tmp2;
1889 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001890 Tmp2 = MakeReg(MVT::f64);
1891 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001892 MoveFP2Int(Tmp2, Result, true);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001893
1894 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001895 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001896
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001897 case ISD::SELECT:
1898 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001899 //FIXME: look at parent to decide if intCC can be folded, or if setCC(FP) and can save stack use
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001900 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001901 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1902 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001903 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001904 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001905
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001906 SDOperand CC = N.getOperand(0);
1907 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
1908
1909 if (CC.getOpcode() == ISD::SETCC &&
1910 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
1911 { //FP Setcc -> Int Select
1912 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001913 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1914 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001915 bool inv = SelectFPSetCC(CC, Tmp1);
1916 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
1917 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
1918 return Result;
1919 }
1920 if (CC.getOpcode() == ISD::SETCC) {
1921 //Int SetCC -> Select
1922 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001923 if((SetCC->getOperand(1).getOpcode() == ISD::Constant &&
1924 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0) ||
1925 (SetCC->getOperand(0).getOpcode() == ISD::Constant &&
1926 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0))
1927 {
1928 //figure out a few things
1929 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
1930 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
1931 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
1932 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
1933 bool LeftConst = N.getOperand(1).getOpcode() == ISD::Constant &&
1934 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255;
1935 bool RightConst = N.getOperand(2).getOpcode() == ISD::Constant &&
1936 cast<ConstantSDNode>(N.getOperand(2))->getValue() <= 255;
1937 bool useImm = LeftConst || RightConst;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001938
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001939 //Fix up CC
1940 ISD::CondCode cCode= SetCC->getCondition();
1941 if (RightConst && !LeftConst) //Invert sense to get Imm field right
1942 cCode = ISD::getSetCCInverse(cCode, true);
1943 if (LeftZero && !RightZero) //Swap Operands
1944 cCode = ISD::getSetCCSwappedOperands(cCode);
1945
1946 //Choose the CMOV
1947 switch (cCode) {
1948 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
1949 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
1950 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
1951 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
1952 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
1953 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
1954 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
1955 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
1956 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break; //Technically you could have this CC
1957 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
1958 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
1959 }
1960 if(LeftZero && !RightZero) //swap Operands
1961 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
1962 else
1963 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
1964
1965 if (LeftConst) {
1966 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1967 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
1968 .addImm(cast<ConstantSDNode>(N.getOperand(1))->getValue())
1969 .addReg(Tmp1);
1970 } else if (RightConst) {
1971 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
1972 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
1973 .addImm(cast<ConstantSDNode>(N.getOperand(2))->getValue())
1974 .addReg(Tmp1);
1975 } else {
1976 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1977 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1978 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
1979 }
1980 return Result;
1981 }
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001982 //Otherwise, fall though
1983 }
1984 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001985 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1986 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001987 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001988
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001989 return Result;
1990 }
1991
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001992 case ISD::Constant:
1993 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00001994 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00001995 if (val <= IMM_HIGH && val >= IMM_LOW) {
1996 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
1997 }
1998 else if (val <= (int64_t)IMM_HIGH + (int64_t)IMM_HIGH * (int64_t)IMM_MULT &&
1999 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
2000 Tmp1 = MakeReg(MVT::i64);
2001 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val)).addReg(Alpha::R31);
2002 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
2003 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002004 else {
2005 MachineConstantPool *CP = BB->getParent()->getConstantPool();
2006 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
2007 unsigned CPI = CP->getConstantPoolIndex(C);
2008 AlphaLowering.restoreGP(BB);
2009 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
2010 }
2011 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002012 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002013 }
2014
2015 return 0;
2016}
2017
2018void ISel::Select(SDOperand N) {
2019 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002020 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002021
Nate Begeman85fdeb22005-03-24 04:39:54 +00002022 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002023 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002024
2025 SDNode *Node = N.Val;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002026
Andrew Lenharth760270d2005-02-07 23:02:23 +00002027 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002028
2029 default:
2030 Node->dump(); std::cerr << "\n";
2031 assert(0 && "Node not handled yet!");
2032
2033 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002034 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002035 return;
2036 }
2037
2038 case ISD::BR: {
2039 MachineBasicBlock *Dest =
2040 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2041
2042 Select(N.getOperand(0));
2043 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2044 return;
2045 }
2046
2047 case ISD::ImplicitDef:
2048 Select(N.getOperand(0));
2049 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2050 return;
2051
2052 case ISD::EntryToken: return; // Noop
2053
2054 case ISD::TokenFactor:
2055 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2056 Select(Node->getOperand(i));
2057
2058 //N.Val->dump(); std::cerr << "\n";
2059 //assert(0 && "Node not handled yet!");
2060
2061 return;
2062
2063 case ISD::CopyToReg:
2064 Select(N.getOperand(0));
2065 Tmp1 = SelectExpr(N.getOperand(1));
2066 Tmp2 = cast<RegSDNode>(N)->getReg();
2067
2068 if (Tmp1 != Tmp2) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002069 if (N.getOperand(1).getValueType() == MVT::f64 ||
2070 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002071 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2072 else
2073 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002074 }
2075 return;
2076
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002077 case ISD::RET:
2078 switch (N.getNumOperands()) {
2079 default:
2080 std::cerr << N.getNumOperands() << "\n";
2081 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2082 std::cerr << N.getOperand(i).getValueType() << "\n";
2083 Node->dump();
2084 assert(0 && "Unknown return instruction!");
2085 case 2:
2086 Select(N.getOperand(0));
2087 Tmp1 = SelectExpr(N.getOperand(1));
2088 switch (N.getOperand(1).getValueType()) {
2089 default: Node->dump();
2090 assert(0 && "All other types should have been promoted!!");
2091 case MVT::f64:
2092 case MVT::f32:
2093 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2094 break;
2095 case MVT::i32:
2096 case MVT::i64:
2097 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2098 break;
2099 }
2100 break;
2101 case 1:
2102 Select(N.getOperand(0));
2103 break;
2104 }
2105 //Tmp2 = AlphaLowering.getRetAddr();
2106 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
2107 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
2108 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002109
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00002110 case ISD::TRUNCSTORE:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002111 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002112 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002113 SDOperand Chain = N.getOperand(0);
2114 SDOperand Value = N.getOperand(1);
2115 SDOperand Address = N.getOperand(2);
2116 Select(Chain);
2117
2118 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002119
2120 if (opcode == ISD::STORE) {
2121 switch(Value.getValueType()) {
2122 default: assert(0 && "unknown Type in store");
2123 case MVT::i64: Opc = Alpha::STQ; break;
2124 case MVT::f64: Opc = Alpha::STT; break;
2125 case MVT::f32: Opc = Alpha::STS; break;
2126 }
2127 } else { //ISD::TRUNCSTORE
2128 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2129 default: assert(0 && "unknown Type in store");
2130 case MVT::i1: //FIXME: DAG does not promote this load
2131 case MVT::i8: Opc = Alpha::STB; break;
2132 case MVT::i16: Opc = Alpha::STW; break;
2133 case MVT::i32: Opc = Alpha::STL; break;
2134 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002135 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002136
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002137 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002138 {
2139 AlphaLowering.restoreGP(BB);
2140 Opc = GetSymVersion(Opc);
2141 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
2142 }
Andrew Lenharth05380342005-02-07 05:07:00 +00002143 else if(Address.getOpcode() == ISD::FrameIndex)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002144 {
Andrew Lenharth032f2352005-02-22 21:59:48 +00002145 BuildMI(BB, Opc, 3).addReg(Tmp1)
2146 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2147 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002148 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002149 else
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002150 {
2151 long offset;
2152 SelectAddr(Address, Tmp2, offset);
2153 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2154 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002155 return;
2156 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002157
2158 case ISD::EXTLOAD:
2159 case ISD::SEXTLOAD:
2160 case ISD::ZEXTLOAD:
2161 case ISD::LOAD:
2162 case ISD::CopyFromReg:
2163 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002164 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002165 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002166 SelectExpr(N);
2167 return;
2168
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002169 case ISD::ADJCALLSTACKDOWN:
2170 case ISD::ADJCALLSTACKUP:
2171 Select(N.getOperand(0));
2172 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2173
2174 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? Alpha::ADJUSTSTACKDOWN :
2175 Alpha::ADJUSTSTACKUP;
2176 BuildMI(BB, Opc, 1).addImm(Tmp1);
2177 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002178
2179 case ISD::PCMARKER:
2180 Select(N.getOperand(0)); //Chain
2181 BuildMI(BB, Alpha::PCLABEL, 2).addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
2182 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002183 }
2184 assert(0 && "Should not be reached!");
2185}
2186
2187
2188/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2189/// into a machine code representation using pattern matching and a machine
2190/// description file.
2191///
2192FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
2193 return new ISel(TM);
2194}