blob: 381ecdd7b9247cb6ae3d9dc6797bb56f218d1a8c [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
Andrew Lenharth320174f2005-04-07 17:19:16 +0000416 p = 63; // initialize p
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000417 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;
Andrew Lenharth320174f2005-04-07 17:19:16 +0000453 p = 63; // initialize p
454 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
455 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
456 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
457 r2 = 0x7FFFFFFFFFFFFFFFull - 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) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000469 if (q2 >= 0x7FFFFFFFFFFFFFFFull) 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 {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000474 if (q2 >= 0x8000000000000000ull) 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
Andrew Lenharth320174f2005-04-07 17:19:16 +0000481 magu.s = p - 64; // resulting shift
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000482 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) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000490 int64_t d = (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000491 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;
Andrew Lenharth2966e842005-04-07 18:15:28 +00001093 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001094 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));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001451 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1452 }
1453 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001454 }
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001455 case ISD::SEXTLOAD:
1456 //SelectionDag isn't deleting the signextend after sextloads
1457 Reg = Result = SelectExpr(N.getOperand(0));
1458 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001459 default: break; //Fall Though;
1460 }
1461 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001462 Tmp1 = SelectExpr(N.getOperand(0));
1463 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001464 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001465 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001466 {
1467 default:
1468 Node->dump();
1469 assert(0 && "Sign Extend InReg not there yet");
1470 break;
1471 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001472 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001473 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001474 break;
1475 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001476 case MVT::i16:
1477 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
1478 break;
1479 case MVT::i8:
1480 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
1481 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001482 case MVT::i1:
1483 Tmp2 = MakeReg(MVT::i64);
1484 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenharth7536eea2005-02-12 20:42:09 +00001485 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::F31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001486 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001487 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001488 return Result;
1489 }
1490 case ISD::ZERO_EXTEND_INREG:
1491 {
1492 Tmp1 = SelectExpr(N.getOperand(0));
1493 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001494 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001495 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001496 {
1497 default:
1498 Node->dump();
1499 assert(0 && "Zero Extend InReg not there yet");
1500 break;
1501 case MVT::i32: Tmp2 = 0xf0; break;
1502 case MVT::i16: Tmp2 = 0xfc; break;
1503 case MVT::i8: Tmp2 = 0xfe; break;
1504 case MVT::i1: //handle this one special
1505 BuildMI(BB, Alpha::ANDi, 2, Result).addReg(Tmp1).addImm(1);
1506 return Result;
1507 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001508 BuildMI(BB, Alpha::ZAPi, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001509 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001510 }
1511
1512 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001513 {
1514 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1515 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1516 bool isConst1 = false;
1517 bool isConst2 = false;
1518 int dir;
Andrew Lenharth9818c052005-02-05 13:19:12 +00001519
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001520 //Tmp1 = SelectExpr(N.getOperand(0));
1521 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001522 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1523 isConst1 = true;
1524 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001525 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1526 isConst2 = true;
1527
1528 switch (SetCC->getCondition()) {
1529 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1530 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001531 case ISD::SETLT:
1532 Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
1533 case ISD::SETLE:
1534 Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
1535 case ISD::SETGT:
1536 Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
1537 case ISD::SETGE:
1538 Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
1539 case ISD::SETULT:
1540 Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
1541 case ISD::SETUGT:
1542 Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
1543 case ISD::SETULE:
1544 Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
1545 case ISD::SETUGE:
1546 Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001547 case ISD::SETNE: {//Handle this one special
1548 //std::cerr << "Alpha does not have a setne.\n";
1549 //abort();
1550 Tmp1 = SelectExpr(N.getOperand(0));
1551 Tmp2 = SelectExpr(N.getOperand(1));
1552 Tmp3 = MakeReg(MVT::i64);
1553 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001554 //Remeber we have the Inv for this CC
1555 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001556 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001557 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001558 return Result;
1559 }
1560 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001561 if (dir == 1) {
1562 Tmp1 = SelectExpr(N.getOperand(0));
1563 if (isConst2) {
1564 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1565 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1566 } else {
1567 Tmp2 = SelectExpr(N.getOperand(1));
1568 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1569 }
1570 } else if (dir == 2) {
1571 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001572 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001573 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1574 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1575 } else {
1576 Tmp2 = SelectExpr(N.getOperand(0));
1577 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1578 }
1579 } else { //dir == 0
1580 if (isConst1) {
1581 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1582 Tmp2 = SelectExpr(N.getOperand(1));
1583 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
1584 } else if (isConst2) {
1585 Tmp1 = SelectExpr(N.getOperand(0));
1586 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1587 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1588 } else {
1589 Tmp1 = SelectExpr(N.getOperand(0));
1590 Tmp2 = SelectExpr(N.getOperand(1));
1591 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1592 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001593 }
1594 } else {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001595 //do the comparison
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001596 Tmp1 = MakeReg(MVT::f64);
1597 bool inv = SelectFPSetCC(N, Tmp1);
1598
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001599 //now arrange for Result (int) to have a 1 or 0
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001600 Tmp2 = MakeReg(MVT::i64);
1601 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001602 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001603 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001604 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001605 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001606 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001607 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001608
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001609 case ISD::CopyFromReg:
1610 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001611 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001612 if (Result != notIn)
1613 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001614 else
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001615 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth40831c52005-01-28 06:57:18 +00001616
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001617 SDOperand Chain = N.getOperand(0);
1618
1619 Select(Chain);
1620 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1621 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1622 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1623 return Result;
1624 }
1625
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001626 //Most of the plain arithmetic and logic share the same form, and the same
1627 //constant immediate test
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001628 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001629 //Match Not
1630 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1631 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue())
1632 {
1633 Tmp1 = SelectExpr(N.getOperand(0));
1634 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1635 return Result;
1636 }
1637 //Fall through
1638 case ISD::AND:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001639 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001640 //Check operand(0) == Not
1641 if (N.getOperand(0).getOpcode() == ISD::OR &&
1642 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1643 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue())
1644 {
1645 switch(opcode) {
1646 case ISD::AND: Opc = Alpha::BIC; break;
1647 case ISD::OR: Opc = Alpha::ORNOT; break;
1648 case ISD::XOR: Opc = Alpha::EQV; break;
1649 }
1650 Tmp1 = SelectExpr(N.getOperand(1));
1651 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1652 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1653 return Result;
1654 }
1655 //Check operand(1) == Not
1656 if (N.getOperand(1).getOpcode() == ISD::OR &&
1657 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
1658 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->isAllOnesValue())
1659 {
1660 switch(opcode) {
1661 case ISD::AND: Opc = Alpha::BIC; break;
1662 case ISD::OR: Opc = Alpha::ORNOT; break;
1663 case ISD::XOR: Opc = Alpha::EQV; break;
1664 }
1665 Tmp1 = SelectExpr(N.getOperand(0));
1666 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1667 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1668 return Result;
1669 }
1670 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001671 case ISD::SHL:
1672 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001673 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001674 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001675 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1676 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001677 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001678 {
1679 switch(opcode) {
1680 case ISD::AND: Opc = Alpha::ANDi; break;
1681 case ISD::OR: Opc = Alpha::BISi; break;
1682 case ISD::XOR: Opc = Alpha::XORi; break;
1683 case ISD::SHL: Opc = Alpha::SLi; break;
1684 case ISD::SRL: Opc = Alpha::SRLi; break;
1685 case ISD::SRA: Opc = Alpha::SRAi; break;
1686 case ISD::MUL: Opc = Alpha::MULQi; break;
1687 };
1688 Tmp1 = SelectExpr(N.getOperand(0));
1689 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1690 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1691 } else {
1692 switch(opcode) {
1693 case ISD::AND: Opc = Alpha::AND; break;
1694 case ISD::OR: Opc = Alpha::BIS; break;
1695 case ISD::XOR: Opc = Alpha::XOR; break;
1696 case ISD::SHL: Opc = Alpha::SL; break;
1697 case ISD::SRL: Opc = Alpha::SRL; break;
1698 case ISD::SRA: Opc = Alpha::SRA; break;
1699 case ISD::MUL: Opc = Alpha::MULQ; break;
1700 };
1701 Tmp1 = SelectExpr(N.getOperand(0));
1702 Tmp2 = SelectExpr(N.getOperand(1));
1703 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1704 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001705 return Result;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001706
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001707 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001708 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001709 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001710 bool isAdd = opcode == ISD::ADD;
1711
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001712 //first check for Scaled Adds and Subs!
1713 //Valid for add and sub
1714 if(N.getOperand(0).getOpcode() == ISD::SHL &&
1715 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1716 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() == 2)
1717 {
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001718 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001719 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1720 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1721 BuildMI(BB, isAdd?Alpha::S4ADDQi:Alpha::S4SUBQi, 2, Result).addReg(Tmp2)
1722 .addImm(cast<ConstantSDNode>(N.getOperand(1))->getValue());
1723 else {
1724 Tmp1 = SelectExpr(N.getOperand(1));
1725 BuildMI(BB, isAdd?Alpha::S4ADDQ:Alpha::S4SUBQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
1726 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001727 }
1728 else if(N.getOperand(0).getOpcode() == ISD::SHL &&
1729 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1730 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() == 3)
1731 {
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001732 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001733 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1734 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1735 BuildMI(BB, isAdd?Alpha::S8ADDQi:Alpha::S8SUBQi, 2, Result).addReg(Tmp2)
1736 .addImm(cast<ConstantSDNode>(N.getOperand(1))->getValue());
1737 else {
1738 Tmp1 = SelectExpr(N.getOperand(1));
1739 BuildMI(BB, isAdd?Alpha::S8ADDQ:Alpha::S8SUBQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
1740 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001741 }
1742 //Position prevents subs
1743 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &
1744 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
1745 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue() == 2)
1746 {
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001747 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001748 if (N.getOperand(0).getOpcode() == ISD::Constant &&
1749 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1750 BuildMI(BB, Alpha::S4ADDQi, 2, Result).addReg(Tmp2)
1751 .addImm(cast<ConstantSDNode>(N.getOperand(0))->getValue());
1752 else {
1753 Tmp1 = SelectExpr(N.getOperand(0));
1754 BuildMI(BB, Alpha::S4ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
1755 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001756 }
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001757 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001758 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
1759 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue() == 3)
1760 {
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001761 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001762 if (N.getOperand(0).getOpcode() == ISD::Constant &&
1763 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1764 BuildMI(BB, Alpha::S8ADDQi, 2, Result).addReg(Tmp2)
1765 .addImm(cast<ConstantSDNode>(N.getOperand(0))->getValue());
1766 else {
1767 Tmp1 = SelectExpr(N.getOperand(0));
1768 BuildMI(BB, Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
1769 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001770 }
1771 //small addi
1772 else if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001773 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001774 { //Normal imm add/sub
1775 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1776 Tmp1 = SelectExpr(N.getOperand(0));
1777 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1778 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1779 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001780 //larger addi
Andrew Lenharth40831c52005-01-28 06:57:18 +00001781 else if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001782 (cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767 ||
1783 (long)cast<ConstantSDNode>(N.getOperand(1))->getValue() >= -32767))
1784 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001785 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001786 Tmp2 = (long)cast<ConstantSDNode>(N.getOperand(1))->getValue();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001787 if (!isAdd)
1788 Tmp2 = -Tmp2;
1789 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001790 }
1791 //give up and do the operation
1792 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001793 //Normal add/sub
1794 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1795 Tmp1 = SelectExpr(N.getOperand(0));
1796 Tmp2 = SelectExpr(N.getOperand(1));
1797 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1798 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001799 return Result;
1800 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001801
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001802 case ISD::SDIV:
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001803 {
Andrew Lenhartha565c272005-04-06 22:03:13 +00001804 ConstantSDNode* CSD;
1805 //check if we can convert into a shift!
1806 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1807 (int64_t)CSD->getSignExtended() != 0 &&
1808 ExactLog2(abs((int64_t)CSD->getSignExtended())) != 0)
1809 {
1810 unsigned k = ExactLog2(abs(CSD->getSignExtended()));
1811 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001812 if (k == 1)
1813 Tmp2 = Tmp1;
1814 else
1815 {
1816 Tmp2 = MakeReg(MVT::i64);
1817 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1818 }
1819 Tmp3 = MakeReg(MVT::i64);
1820 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1821 unsigned Tmp4 = MakeReg(MVT::i64);
1822 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
1823 if ((int64_t)CSD->getSignExtended() > 0)
1824 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
1825 else
1826 {
1827 unsigned Tmp5 = MakeReg(MVT::i64);
1828 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
1829 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
1830 }
1831 return Result;
1832 }
1833 }
1834 //Else fall through
1835
1836 case ISD::UDIV:
1837 {
1838 ConstantSDNode* CSD;
1839 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1840 ((int64_t)CSD->getSignExtended() >= 2 ||
1841 (int64_t)CSD->getSignExtended() <= -2))
1842 {
1843 // If this is a divide by constant, we can emit code using some magic
1844 // constants to implement it as a multiply instead.
1845 ExprMap.erase(N);
1846 if (opcode == ISD::SDIV)
1847 return SelectExpr(BuildSDIVSequence(N));
1848 else
1849 return SelectExpr(BuildUDIVSequence(N));
1850 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001851 }
1852 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001853 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00001854 case ISD::SREM:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001855 //FIXME: alpha really doesn't support any of these operations,
1856 // the ops are expanded into special library calls with
1857 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001858 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00001859 switch(opcode) {
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001860 case ISD::UREM: Opc = Alpha::REMQU; break;
1861 case ISD::SREM: Opc = Alpha::REMQ; break;
1862 case ISD::UDIV: Opc = Alpha::DIVQU; break;
1863 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001864 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001865 Tmp1 = SelectExpr(N.getOperand(0));
1866 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth33819132005-03-04 20:09:23 +00001867 //set up regs explicitly (helps Reg alloc)
1868 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
1869 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001870 AlphaLowering.restoreGP(BB);
Andrew Lenharth33819132005-03-04 20:09:23 +00001871 BuildMI(BB, Opc, 2).addReg(Alpha::R24).addReg(Alpha::R25);
1872 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001873 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001874
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001875 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001876 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001877 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001878 assert (DestType == MVT::i64 && "only quads can be loaded to");
1879 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001880 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001881 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001882 if (SrcType == MVT::f32)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001883 {
1884 Tmp2 = MakeReg(MVT::f64);
1885 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1886 Tmp1 = Tmp2;
1887 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001888 Tmp2 = MakeReg(MVT::f64);
1889 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001890 MoveFP2Int(Tmp2, Result, true);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001891
1892 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001893 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001894
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001895 case ISD::SELECT:
1896 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001897 //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 +00001898 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001899 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1900 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001901 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001902 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001903
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001904 SDOperand CC = N.getOperand(0);
1905 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
1906
1907 if (CC.getOpcode() == ISD::SETCC &&
1908 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
1909 { //FP Setcc -> Int Select
1910 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001911 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1912 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001913 bool inv = SelectFPSetCC(CC, Tmp1);
1914 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
1915 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
1916 return Result;
1917 }
1918 if (CC.getOpcode() == ISD::SETCC) {
1919 //Int SetCC -> Select
1920 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001921 if((SetCC->getOperand(1).getOpcode() == ISD::Constant &&
1922 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0) ||
1923 (SetCC->getOperand(0).getOpcode() == ISD::Constant &&
1924 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0))
1925 {
1926 //figure out a few things
1927 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
1928 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
1929 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
1930 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
1931 bool LeftConst = N.getOperand(1).getOpcode() == ISD::Constant &&
1932 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255;
1933 bool RightConst = N.getOperand(2).getOpcode() == ISD::Constant &&
1934 cast<ConstantSDNode>(N.getOperand(2))->getValue() <= 255;
1935 bool useImm = LeftConst || RightConst;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001936
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001937 //Fix up CC
1938 ISD::CondCode cCode= SetCC->getCondition();
1939 if (RightConst && !LeftConst) //Invert sense to get Imm field right
1940 cCode = ISD::getSetCCInverse(cCode, true);
1941 if (LeftZero && !RightZero) //Swap Operands
1942 cCode = ISD::getSetCCSwappedOperands(cCode);
1943
1944 //Choose the CMOV
1945 switch (cCode) {
1946 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
1947 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
1948 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
1949 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
1950 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
1951 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
1952 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
1953 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
1954 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break; //Technically you could have this CC
1955 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
1956 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
1957 }
1958 if(LeftZero && !RightZero) //swap Operands
1959 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
1960 else
1961 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
1962
1963 if (LeftConst) {
1964 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1965 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
1966 .addImm(cast<ConstantSDNode>(N.getOperand(1))->getValue())
1967 .addReg(Tmp1);
1968 } else if (RightConst) {
1969 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
1970 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
1971 .addImm(cast<ConstantSDNode>(N.getOperand(2))->getValue())
1972 .addReg(Tmp1);
1973 } else {
1974 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1975 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1976 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
1977 }
1978 return Result;
1979 }
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001980 //Otherwise, fall though
1981 }
1982 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001983 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1984 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001985 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001986
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001987 return Result;
1988 }
1989
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001990 case ISD::Constant:
1991 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00001992 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00001993 if (val <= IMM_HIGH && val >= IMM_LOW) {
1994 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
1995 }
1996 else if (val <= (int64_t)IMM_HIGH + (int64_t)IMM_HIGH * (int64_t)IMM_MULT &&
1997 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
1998 Tmp1 = MakeReg(MVT::i64);
1999 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val)).addReg(Alpha::R31);
2000 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
2001 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002002 else {
2003 MachineConstantPool *CP = BB->getParent()->getConstantPool();
2004 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
2005 unsigned CPI = CP->getConstantPoolIndex(C);
2006 AlphaLowering.restoreGP(BB);
2007 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
2008 }
2009 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002010 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002011 }
2012
2013 return 0;
2014}
2015
2016void ISel::Select(SDOperand N) {
2017 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002018 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002019
Nate Begeman85fdeb22005-03-24 04:39:54 +00002020 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002021 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002022
2023 SDNode *Node = N.Val;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002024
Andrew Lenharth760270d2005-02-07 23:02:23 +00002025 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002026
2027 default:
2028 Node->dump(); std::cerr << "\n";
2029 assert(0 && "Node not handled yet!");
2030
2031 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002032 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002033 return;
2034 }
2035
2036 case ISD::BR: {
2037 MachineBasicBlock *Dest =
2038 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2039
2040 Select(N.getOperand(0));
2041 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2042 return;
2043 }
2044
2045 case ISD::ImplicitDef:
2046 Select(N.getOperand(0));
2047 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2048 return;
2049
2050 case ISD::EntryToken: return; // Noop
2051
2052 case ISD::TokenFactor:
2053 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2054 Select(Node->getOperand(i));
2055
2056 //N.Val->dump(); std::cerr << "\n";
2057 //assert(0 && "Node not handled yet!");
2058
2059 return;
2060
2061 case ISD::CopyToReg:
2062 Select(N.getOperand(0));
2063 Tmp1 = SelectExpr(N.getOperand(1));
2064 Tmp2 = cast<RegSDNode>(N)->getReg();
2065
2066 if (Tmp1 != Tmp2) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002067 if (N.getOperand(1).getValueType() == MVT::f64 ||
2068 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002069 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2070 else
2071 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002072 }
2073 return;
2074
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002075 case ISD::RET:
2076 switch (N.getNumOperands()) {
2077 default:
2078 std::cerr << N.getNumOperands() << "\n";
2079 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2080 std::cerr << N.getOperand(i).getValueType() << "\n";
2081 Node->dump();
2082 assert(0 && "Unknown return instruction!");
2083 case 2:
2084 Select(N.getOperand(0));
2085 Tmp1 = SelectExpr(N.getOperand(1));
2086 switch (N.getOperand(1).getValueType()) {
2087 default: Node->dump();
2088 assert(0 && "All other types should have been promoted!!");
2089 case MVT::f64:
2090 case MVT::f32:
2091 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2092 break;
2093 case MVT::i32:
2094 case MVT::i64:
2095 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2096 break;
2097 }
2098 break;
2099 case 1:
2100 Select(N.getOperand(0));
2101 break;
2102 }
2103 //Tmp2 = AlphaLowering.getRetAddr();
2104 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
2105 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
2106 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002107
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00002108 case ISD::TRUNCSTORE:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002109 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002110 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002111 SDOperand Chain = N.getOperand(0);
2112 SDOperand Value = N.getOperand(1);
2113 SDOperand Address = N.getOperand(2);
2114 Select(Chain);
2115
2116 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002117
2118 if (opcode == ISD::STORE) {
2119 switch(Value.getValueType()) {
2120 default: assert(0 && "unknown Type in store");
2121 case MVT::i64: Opc = Alpha::STQ; break;
2122 case MVT::f64: Opc = Alpha::STT; break;
2123 case MVT::f32: Opc = Alpha::STS; break;
2124 }
2125 } else { //ISD::TRUNCSTORE
2126 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2127 default: assert(0 && "unknown Type in store");
2128 case MVT::i1: //FIXME: DAG does not promote this load
2129 case MVT::i8: Opc = Alpha::STB; break;
2130 case MVT::i16: Opc = Alpha::STW; break;
2131 case MVT::i32: Opc = Alpha::STL; break;
2132 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002133 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002134
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002135 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002136 {
2137 AlphaLowering.restoreGP(BB);
2138 Opc = GetSymVersion(Opc);
2139 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
2140 }
Andrew Lenharth05380342005-02-07 05:07:00 +00002141 else if(Address.getOpcode() == ISD::FrameIndex)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002142 {
Andrew Lenharth032f2352005-02-22 21:59:48 +00002143 BuildMI(BB, Opc, 3).addReg(Tmp1)
2144 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2145 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002146 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002147 else
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002148 {
2149 long offset;
2150 SelectAddr(Address, Tmp2, offset);
2151 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2152 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002153 return;
2154 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002155
2156 case ISD::EXTLOAD:
2157 case ISD::SEXTLOAD:
2158 case ISD::ZEXTLOAD:
2159 case ISD::LOAD:
2160 case ISD::CopyFromReg:
2161 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002162 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002163 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002164 SelectExpr(N);
2165 return;
2166
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002167 case ISD::ADJCALLSTACKDOWN:
2168 case ISD::ADJCALLSTACKUP:
2169 Select(N.getOperand(0));
2170 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2171
2172 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? Alpha::ADJUSTSTACKDOWN :
2173 Alpha::ADJUSTSTACKUP;
2174 BuildMI(BB, Opc, 1).addImm(Tmp1);
2175 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002176
2177 case ISD::PCMARKER:
2178 Select(N.getOperand(0)); //Chain
2179 BuildMI(BB, Alpha::PCLABEL, 2).addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
2180 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002181 }
2182 assert(0 && "Should not be reached!");
2183}
2184
2185
2186/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2187/// into a machine code representation using pattern matching and a machine
2188/// description file.
2189///
2190FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
2191 return new ISel(TM);
2192}