blob: 8d6c46ac172334649c4be2ec4374df3440c8e040 [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 Lenharthd3355e22005-04-07 20:11:32 +000056 setSetCCResultContents(ZeroOrOneSetCCResult);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000057
Andrew Lenharth304d0f32005-01-22 23:41:55 +000058 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
59 addRegisterClass(MVT::f64, Alpha::FPRCRegisterClass);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000060 addRegisterClass(MVT::f32, Alpha::FPRCRegisterClass);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000061
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000062 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
63 setOperationAction(ISD::EXTLOAD , MVT::f32 , Promote);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000064
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000065 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
66 setOperationAction(ISD::ZEXTLOAD , MVT::i32 , Expand);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000067
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000068 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
69 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
70 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000071
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000072 setOperationAction(ISD::SREM , MVT::f32 , Expand);
73 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +000074
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000075 //If this didn't legalize into a div....
76 // setOperationAction(ISD::SREM , MVT::i64, Expand);
77 // setOperationAction(ISD::UREM , MVT::i64, Expand);
78
79 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
80 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
81 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
Andrew Lenharth9818c052005-02-05 13:19:12 +000082
Andrew Lenharth33819132005-03-04 20:09:23 +000083 //Doesn't work yet
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000084 setOperationAction(ISD::SETCC , MVT::f32, Promote);
Andrew Lenharth572af902005-02-14 05:41:43 +000085
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000086 computeRegisterProperties();
Andrew Lenharth304d0f32005-01-22 23:41:55 +000087
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000088 addLegalFPImmediate(+0.0); //F31
89 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +000090 }
91
92 /// LowerArguments - This hook must be implemented to indicate how we should
93 /// lower the arguments for the specified function, into the specified DAG.
94 virtual std::vector<SDOperand>
95 LowerArguments(Function &F, SelectionDAG &DAG);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000096
Andrew Lenharth304d0f32005-01-22 23:41:55 +000097 /// LowerCallTo - This hook lowers an abstract call to a function into an
98 /// actual call.
99 virtual std::pair<SDOperand, SDOperand>
Nate Begeman8e21e712005-03-26 01:29:23 +0000100 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
101 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000102
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000103 virtual std::pair<SDOperand, SDOperand>
104 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000105
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000106 virtual std::pair<SDOperand,SDOperand>
107 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
108 const Type *ArgTy, SelectionDAG &DAG);
109
110 virtual std::pair<SDOperand, SDOperand>
111 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
112 SelectionDAG &DAG);
113
114 void restoreGP(MachineBasicBlock* BB)
115 {
116 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
117 }
118 };
119}
120
121//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
122
123//For now, just use variable size stack frame format
124
125//In a standard call, the first six items are passed in registers $16
126//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
127//of argument-to-register correspondence.) The remaining items are
128//collected in a memory argument list that is a naturally aligned
129//array of quadwords. In a standard call, this list, if present, must
130//be passed at 0(SP).
131//7 ... n 0(SP) ... (n-7)*8(SP)
132
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000133// //#define FP $15
134// //#define RA $26
135// //#define PV $27
136// //#define GP $29
137// //#define SP $30
138
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000139std::vector<SDOperand>
140AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
141{
142 std::vector<SDOperand> ArgValues;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000143 std::vector<SDOperand> LS;
144 SDOperand Chain = DAG.getRoot();
145
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000146 // assert(0 && "TODO");
147 MachineFunction &MF = DAG.getMachineFunction();
Andrew Lenharth05380342005-02-07 05:07:00 +0000148 MachineFrameInfo*MFI = MF.getFrameInfo();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000149
150 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
151 MachineBasicBlock& BB = MF.front();
152
153 //Handle the return address
154 //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
155
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000156 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
157 Alpha::R19, Alpha::R20, Alpha::R21};
158 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
159 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000160 int count = 0;
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000161
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000162 //Def incoming registers
163 {
164 Function::arg_iterator I = F.arg_begin();
165 Function::arg_iterator E = F.arg_end();
166 for (int i = 0; i < 6; ++i)
167 {
168 if (F.isVarArg()) {
169 BuildMI(&BB, Alpha::IDEF, 0, args_int[i]);
170 BuildMI(&BB, Alpha::IDEF, 0, args_float[i]);
171 } else if (I != E)
172 {
173 if(MVT::isInteger(getValueType(I->getType())))
174 BuildMI(&BB, Alpha::IDEF, 0, args_int[i]);
175 else
176 BuildMI(&BB, Alpha::IDEF, 0, args_float[i]);
177 ++I;
178 }
179 }
180 }
181
182 BuildMI(&BB, Alpha::IDEF, 0, Alpha::R29);
183 BuildMI(&BB, Alpha::BIS, 2, GP).addReg(Alpha::R29).addReg(Alpha::R29);
184
Chris Lattnere4d5c442005-03-15 04:54:21 +0000185 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000186 {
187 SDOperand newroot, argt;
188 if (count < 6) {
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000189 unsigned Vreg;
190 MVT::ValueType VT = getValueType(I->getType());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000191 switch (getValueType(I->getType())) {
192 default:
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000193 std::cerr << "Unknown Type " << VT << "\n";
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000194 abort();
195 case MVT::f64:
196 case MVT::f32:
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000197 Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(VT));
198 BuildMI(&BB, Alpha::CPYS, 2, Vreg).addReg(args_float[count]).addReg(args_float[count]);
199 argt = newroot = DAG.getCopyFromReg(Vreg,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000200 getValueType(I->getType()),
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000201 Chain);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000202 break;
203 case MVT::i1:
204 case MVT::i8:
205 case MVT::i16:
206 case MVT::i32:
207 case MVT::i64:
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000208 Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
209 BuildMI(&BB, Alpha::BIS, 2, Vreg).addReg(args_int[count]).addReg(args_int[count]);
210 argt = newroot = DAG.getCopyFromReg(Vreg, MVT::i64, Chain);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000211 if (getValueType(I->getType()) != MVT::i64)
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000212 argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()), newroot);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000213 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000214 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000215 } else { //more args
216 // Create the frame index object for this incoming parameter...
217 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
218
219 // Create the SelectionDAG nodes corresponding to a load
220 //from this parameter
221 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
222 argt = newroot = DAG.getLoad(getValueType(I->getType()),
223 DAG.getEntryNode(), FIN);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000224 }
Andrew Lenharth032f2352005-02-22 21:59:48 +0000225 ++count;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000226 LS.push_back(newroot.getValue(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000227 ArgValues.push_back(argt);
228 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000229
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000230 // If the functions takes variable number of arguments, copy all regs to stack
231 if (F.isVarArg())
232 for (int i = 0; i < 6; ++i)
233 {
234 unsigned Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
235 BuildMI(&BB, Alpha::BIS, 2, Vreg).addReg(args_int[i]).addReg(args_int[i]);
236 SDOperand argt = DAG.getCopyFromReg(Vreg, MVT::i64, Chain);
237 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
238 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
239 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, argt, SDFI));
240
241 Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::f64));
242 BuildMI(&BB, Alpha::CPYS, 2, Vreg).addReg(args_float[i]).addReg(args_float[i]);
243 argt = DAG.getCopyFromReg(Vreg, MVT::f64, Chain);
244 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
245 SDFI = DAG.getFrameIndex(FI, MVT::i64);
246 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, argt, SDFI));
247 }
248
249 // If the function takes variable number of arguments, make a frame index for
250 // the start of the first arg value... for expansion of llvm.va_start.
251 // if (F.isVarArg())
252 // VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
253
254 //Set up a token factor with all the stack traffic
255 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
256 //return the arguments
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000257 return ArgValues;
258}
259
260std::pair<SDOperand, SDOperand>
261AlphaTargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman8e21e712005-03-26 01:29:23 +0000262 const Type *RetTy, bool isVarArg,
263 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000264 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000265 if (Args.size() > 6)
266 NumBytes = (Args.size() - 6) * 8;
267
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000268 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
269 DAG.getConstant(NumBytes, getPointerTy()));
270 std::vector<SDOperand> args_to_use;
271 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000272 {
273 switch (getValueType(Args[i].second)) {
274 default: assert(0 && "Unexpected ValueType for argument!");
275 case MVT::i1:
276 case MVT::i8:
277 case MVT::i16:
278 case MVT::i32:
279 // Promote the integer to 64 bits. If the input type is signed use a
280 // sign extend, otherwise use a zero extend.
281 if (Args[i].second->isSigned())
282 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
283 else
284 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
285 break;
286 case MVT::i64:
287 case MVT::f64:
288 case MVT::f32:
289 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000290 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000291 args_to_use.push_back(Args[i].first);
292 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000293
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000294 std::vector<MVT::ValueType> RetVals;
295 MVT::ValueType RetTyVT = getValueType(RetTy);
296 if (RetTyVT != MVT::isVoid)
297 RetVals.push_back(RetTyVT);
298 RetVals.push_back(MVT::Other);
299
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000300 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
301 Chain, Callee, args_to_use), 0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000302 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
303 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
304 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000305 return std::make_pair(TheCall, Chain);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000306}
307
308std::pair<SDOperand, SDOperand>
309AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
310 //vastart just returns the address of the VarArgsFrameIndex slot.
311 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
312}
313
314std::pair<SDOperand,SDOperand> AlphaTargetLowering::
315LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000316 const Type *ArgTy, SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000317 abort();
318}
319
320
321std::pair<SDOperand, SDOperand> AlphaTargetLowering::
322LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
323 SelectionDAG &DAG) {
324 abort();
325}
326
327
328
329
330
331namespace {
332
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000333//===--------------------------------------------------------------------===//
334/// ISel - Alpha specific code to select Alpha machine instructions for
335/// SelectionDAG operations.
336//===--------------------------------------------------------------------===//
337class ISel : public SelectionDAGISel {
338
339 /// AlphaLowering - This object fully describes how to lower LLVM code to an
340 /// Alpha-specific SelectionDAG.
341 AlphaTargetLowering AlphaLowering;
342
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000343 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
344 // for sdiv and udiv until it is put into the future
345 // dag combiner.
346
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000347 /// ExprMap - As shared expressions are codegen'd, we keep track of which
348 /// vreg the value is produced in, so we only emit one copy of each compiled
349 /// tree.
350 static const unsigned notIn = (unsigned)(-1);
351 std::map<SDOperand, unsigned> ExprMap;
352
353 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
354 std::map<SDOperand, unsigned> CCInvMap;
355
356public:
357 ISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM)
358 {}
359
360 /// InstructionSelectBasicBlock - This callback is invoked by
361 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
362 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000363 DEBUG(BB->dump());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000364 // Codegen the basic block.
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000365 ISelDAG = &DAG;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000366 Select(DAG.getRoot());
367
368 // Clear state used for selection.
369 ExprMap.clear();
370 CCInvMap.clear();
371 }
372
373 unsigned SelectExpr(SDOperand N);
374 unsigned SelectExprFP(SDOperand N, unsigned Result);
375 void Select(SDOperand N);
376
377 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
378 void SelectBranchCC(SDOperand N);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000379 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
380 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000381 //returns whether the sense of the comparison was inverted
382 bool SelectFPSetCC(SDOperand N, unsigned dst);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000383
384 // dag -> dag expanders for integer divide by constant
385 SDOperand BuildSDIVSequence(SDOperand N);
386 SDOperand BuildUDIVSequence(SDOperand N);
387
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000388};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000389}
390
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000391//Shamelessly adapted from PPC32
392// Structure used to return the necessary information to codegen an SDIV as
393// a multiply.
394struct ms {
395 int64_t m; // magic number
396 int64_t s; // shift amount
397};
398
399struct mu {
400 uint64_t m; // magic number
401 int64_t a; // add indicator
402 int64_t s; // shift amount
403};
404
405/// magic - calculate the magic numbers required to codegen an integer sdiv as
406/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
407/// or -1.
408static struct ms magic(int64_t d) {
409 int64_t p;
410 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
411 const uint64_t two63 = 9223372036854775808ULL; // 2^63
412 struct ms mag;
413
414 ad = abs(d);
415 t = two63 + ((uint64_t)d >> 63);
416 anc = t - 1 - t%ad; // absolute value of nc
Andrew Lenharth320174f2005-04-07 17:19:16 +0000417 p = 63; // initialize p
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000418 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
419 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
420 q2 = two63/ad; // initialize q2 = 2p/abs(d)
421 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
422 do {
423 p = p + 1;
424 q1 = 2*q1; // update q1 = 2p/abs(nc)
425 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
426 if (r1 >= anc) { // must be unsigned comparison
427 q1 = q1 + 1;
428 r1 = r1 - anc;
429 }
430 q2 = 2*q2; // update q2 = 2p/abs(d)
431 r2 = 2*r2; // update r2 = rem(2p/abs(d))
432 if (r2 >= ad) { // must be unsigned comparison
433 q2 = q2 + 1;
434 r2 = r2 - ad;
435 }
436 delta = ad - r2;
437 } while (q1 < delta || (q1 == delta && r1 == 0));
438
439 mag.m = q2 + 1;
440 if (d < 0) mag.m = -mag.m; // resulting magic number
441 mag.s = p - 64; // resulting shift
442 return mag;
443}
444
445/// magicu - calculate the magic numbers required to codegen an integer udiv as
446/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
447static struct mu magicu(uint64_t d)
448{
449 int64_t p;
450 uint64_t nc, delta, q1, r1, q2, r2;
451 struct mu magu;
452 magu.a = 0; // initialize "add" indicator
453 nc = - 1 - (-d)%d;
Andrew Lenharth320174f2005-04-07 17:19:16 +0000454 p = 63; // initialize p
455 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
456 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
457 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
458 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000459 do {
460 p = p + 1;
461 if (r1 >= nc - r1 ) {
462 q1 = 2*q1 + 1; // update q1
463 r1 = 2*r1 - nc; // update r1
464 }
465 else {
466 q1 = 2*q1; // update q1
467 r1 = 2*r1; // update r1
468 }
469 if (r2 + 1 >= d - r2) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000470 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000471 q2 = 2*q2 + 1; // update q2
472 r2 = 2*r2 + 1 - d; // update r2
473 }
474 else {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000475 if (q2 >= 0x8000000000000000ull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000476 q2 = 2*q2; // update q2
477 r2 = 2*r2 + 1; // update r2
478 }
479 delta = d - 1 - r2;
480 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
481 magu.m = q2 + 1; // resulting magic number
Andrew Lenharth320174f2005-04-07 17:19:16 +0000482 magu.s = p - 64; // resulting shift
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000483 return magu;
484}
485
486/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
487/// return a DAG expression to select that will generate the same value by
488/// multiplying by a magic number. See:
489/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
490SDOperand ISel::BuildSDIVSequence(SDOperand N) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000491 int64_t d = (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000492 ms magics = magic(d);
493 // Multiply the numerator (operand 0) by the magic value
494 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
495 ISelDAG->getConstant(magics.m, MVT::i64));
496 // If d > 0 and m < 0, add the numerator
497 if (d > 0 && magics.m < 0)
498 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
499 // If d < 0 and m > 0, subtract the numerator.
500 if (d < 0 && magics.m > 0)
501 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
502 // Shift right algebraic if shift value is nonzero
503 if (magics.s > 0)
504 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
505 ISelDAG->getConstant(magics.s, MVT::i64));
506 // Extract the sign bit and add it to the quotient
507 SDOperand T =
508 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
509 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
510}
511
512/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
513/// return a DAG expression to select that will generate the same value by
514/// multiplying by a magic number. See:
515/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
516SDOperand ISel::BuildUDIVSequence(SDOperand N) {
517 unsigned d =
518 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
519 mu magics = magicu(d);
520 // Multiply the numerator (operand 0) by the magic value
521 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
522 ISelDAG->getConstant(magics.m, MVT::i64));
523 if (magics.a == 0) {
524 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
525 ISelDAG->getConstant(magics.s, MVT::i64));
526 } else {
527 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
528 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
529 ISelDAG->getConstant(1, MVT::i64));
530 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
531 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
532 ISelDAG->getConstant(magics.s-1, MVT::i64));
533 }
534 return Q;
535}
536
Andrew Lenhartha565c272005-04-06 22:03:13 +0000537//From PPC32
538/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
539/// returns zero when the input is not exactly a power of two.
540static unsigned ExactLog2(uint64_t Val) {
541 if (Val == 0 || (Val & (Val-1))) return 0;
542 unsigned Count = 0;
543 while (Val != 1) {
544 Val >>= 1;
545 ++Count;
546 }
547 return Count;
548}
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000549
550
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000551//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000552static const int IMM_LOW = -32768;
553static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000554static const int IMM_MULT = 65536;
555
556static long getUpper16(long l)
557{
558 long y = l / IMM_MULT;
559 if (l % IMM_MULT > IMM_HIGH)
560 ++y;
561 return y;
562}
563
564static long getLower16(long l)
565{
566 long h = getUpper16(l);
567 return l - h * IMM_MULT;
568}
569
Andrew Lenharth65838902005-02-06 16:22:15 +0000570static unsigned GetSymVersion(unsigned opcode)
571{
572 switch (opcode) {
573 default: assert(0 && "unknown load or store"); return 0;
574 case Alpha::LDQ: return Alpha::LDQ_SYM;
575 case Alpha::LDS: return Alpha::LDS_SYM;
576 case Alpha::LDT: return Alpha::LDT_SYM;
577 case Alpha::LDL: return Alpha::LDL_SYM;
578 case Alpha::LDBU: return Alpha::LDBU_SYM;
579 case Alpha::LDWU: return Alpha::LDWU_SYM;
580 case Alpha::LDW: return Alpha::LDW_SYM;
581 case Alpha::LDB: return Alpha::LDB_SYM;
582 case Alpha::STQ: return Alpha::STQ_SYM;
583 case Alpha::STS: return Alpha::STS_SYM;
584 case Alpha::STT: return Alpha::STT_SYM;
585 case Alpha::STL: return Alpha::STL_SYM;
586 case Alpha::STW: return Alpha::STW_SYM;
587 case Alpha::STB: return Alpha::STB_SYM;
588 }
589}
590
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000591void ISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
592{
593 unsigned Opc;
594 if (EnableAlphaFTOI) {
595 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
596 BuildMI(BB, Opc, 1, dst).addReg(src);
597 } else {
598 //The hard way:
599 // Spill the integer to memory and reload it from there.
600 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
601 MachineFunction *F = BB->getParent();
602 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
603
604 Opc = isDouble ? Alpha::STT : Alpha::STS;
605 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
606 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
607 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
608 }
609}
610
611void ISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
612{
613 unsigned Opc;
614 if (EnableAlphaFTOI) {
615 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
616 BuildMI(BB, Opc, 1, dst).addReg(src);
617 } else {
618 //The hard way:
619 // Spill the integer to memory and reload it from there.
620 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
621 MachineFunction *F = BB->getParent();
622 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
623
624 Opc = isDouble ? Alpha::STQ : Alpha::STL;
625 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
626 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
627 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
628 }
629}
630
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000631bool ISel::SelectFPSetCC(SDOperand N, unsigned dst)
632{
633 SDNode *Node = N.Val;
634 unsigned Opc, Tmp1, Tmp2, Tmp3;
635 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node);
636
637 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
638 bool rev = false;
639 bool inv = false;
640
641 switch (SetCC->getCondition()) {
642 default: Node->dump(); assert(0 && "Unknown FP comparison!");
643 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
644 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
645 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
646 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
647 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
648 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
649 }
650
651 //FIXME: check for constant 0.0
652 ConstantFPSDNode *CN;
653 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
654 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
655 Tmp1 = Alpha::F31;
656 else
657 Tmp1 = SelectExpr(N.getOperand(0));
658
659 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
660 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
661 Tmp2 = Alpha::F31;
662 else
663 Tmp2 = SelectExpr(N.getOperand(1));
664
665 //Can only compare doubles, and dag won't promote for me
666 if (SetCC->getOperand(0).getValueType() == MVT::f32)
667 {
668 //assert(0 && "Setcc On float?\n");
669 std::cerr << "Setcc on float!\n";
670 Tmp3 = MakeReg(MVT::f64);
671 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
672 Tmp1 = Tmp3;
673 }
674 if (SetCC->getOperand(1).getValueType() == MVT::f32)
675 {
676 //assert (0 && "Setcc On float?\n");
677 std::cerr << "Setcc on float!\n";
678 Tmp3 = MakeReg(MVT::f64);
679 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
680 Tmp2 = Tmp3;
681 }
682
683 if (rev) std::swap(Tmp1, Tmp2);
684 //do the comparison
685 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
686 return inv;
687}
688
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000689//Check to see if the load is a constant offset from a base register
690void ISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
691{
692 unsigned opcode = N.getOpcode();
693 if (opcode == ISD::ADD) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000694 if(N.getOperand(1).getOpcode() == ISD::Constant &&
695 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
696 { //Normal imm add
697 Reg = SelectExpr(N.getOperand(0));
698 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
699 return;
700 }
701 else if(N.getOperand(0).getOpcode() == ISD::Constant &&
702 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 32767)
703 {
704 Reg = SelectExpr(N.getOperand(1));
705 offset = cast<ConstantSDNode>(N.getOperand(0))->getValue();
706 return;
707 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000708 }
709 Reg = SelectExpr(N);
710 offset = 0;
711 return;
712}
713
Andrew Lenharth445171a2005-02-08 00:40:03 +0000714void ISel::SelectBranchCC(SDOperand N)
715{
716 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000717 MachineBasicBlock *Dest =
718 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
719 unsigned Opc = Alpha::WTF;
720
Andrew Lenharth445171a2005-02-08 00:40:03 +0000721 Select(N.getOperand(0)); //chain
722 SDOperand CC = N.getOperand(1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000723
Andrew Lenharth445171a2005-02-08 00:40:03 +0000724 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000725 {
726 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
727 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
728 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000729 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
730 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
731 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
732 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000733 bool isNE = false;
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000734
735 //Fix up CC
736 ISD::CondCode cCode= SetCC->getCondition();
737 if (LeftZero && !RightZero) //Swap Operands
738 cCode = ISD::getSetCCSwappedOperands(cCode);
739
740 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000741 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000742
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000743 if (LeftZero || RightZero) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000744 switch (SetCC->getCondition()) {
745 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
746 case ISD::SETEQ: Opc = Alpha::BEQ; break;
747 case ISD::SETLT: Opc = Alpha::BLT; break;
748 case ISD::SETLE: Opc = Alpha::BLE; break;
749 case ISD::SETGT: Opc = Alpha::BGT; break;
750 case ISD::SETGE: Opc = Alpha::BGE; break;
751 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
752 case ISD::SETUGT: Opc = Alpha::BNE; break;
753 case ISD::SETULE: Opc = Alpha::BEQ; break; //Technically you could have this CC
754 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
755 case ISD::SETNE: Opc = Alpha::BNE; break;
756 }
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000757 unsigned Tmp1;
758 if(LeftZero && !RightZero) //swap Operands
759 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
760 else
761 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000762 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
763 return;
764 } else {
765 unsigned Tmp1 = SelectExpr(CC);
766 if (isNE)
767 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
768 else
769 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000770 return;
771 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000772 } else { //FP
773 //Any comparison between 2 values should be codegened as an folded branch, as moving
774 //CC to the integer register is very expensive
775 //for a cmp b: c = a - b;
776 //a = b: c = 0
777 //a < b: c < 0
778 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000779
780 bool invTest = false;
781 unsigned Tmp3;
782
783 ConstantFPSDNode *CN;
784 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
785 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
786 Tmp3 = SelectExpr(SetCC->getOperand(0));
787 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
788 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
789 {
790 Tmp3 = SelectExpr(SetCC->getOperand(1));
791 invTest = true;
792 }
793 else
794 {
795 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
796 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
797 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
798 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
799 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
800 .addReg(Tmp1).addReg(Tmp2);
801 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000802
803 switch (SetCC->getCondition()) {
804 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000805 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
806 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
807 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
808 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
809 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
810 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000811 }
812 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000813 return;
814 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000815 abort(); //Should never be reached
816 } else {
817 //Giveup and do the stupid thing
818 unsigned Tmp1 = SelectExpr(CC);
819 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
820 return;
821 }
Andrew Lenharth445171a2005-02-08 00:40:03 +0000822 abort(); //Should never be reached
823}
824
Andrew Lenharth40831c52005-01-28 06:57:18 +0000825unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
826{
827 unsigned Tmp1, Tmp2, Tmp3;
828 unsigned Opc = 0;
829 SDNode *Node = N.Val;
830 MVT::ValueType DestType = N.getValueType();
831 unsigned opcode = N.getOpcode();
832
833 switch (opcode) {
834 default:
835 Node->dump();
836 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +0000837
Andrew Lenharth7332f3e2005-04-02 19:11:07 +0000838 case ISD::UNDEF: {
839 BuildMI(BB, Alpha::IDEF, 0, Result);
840 return Result;
841 }
842
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000843 case ISD::FNEG:
844 if(ISD::FABS == N.getOperand(0).getOpcode())
845 {
846 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth7332f3e2005-04-02 19:11:07 +0000847 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000848 } else {
849 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth7332f3e2005-04-02 19:11:07 +0000850 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000851 }
852 return Result;
853
854 case ISD::FABS:
855 Tmp1 = SelectExpr(N.getOperand(0));
856 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
857 return Result;
858
Andrew Lenharth9818c052005-02-05 13:19:12 +0000859 case ISD::SELECT:
860 {
Andrew Lenharth45859692005-03-03 21:47:53 +0000861 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
862 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
863 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
864
865 SDOperand CC = N.getOperand(0);
866 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
867
868 if (CC.getOpcode() == ISD::SETCC &&
869 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
870 { //FP Setcc -> Select yay!
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000871
872
Andrew Lenharth45859692005-03-03 21:47:53 +0000873 //for a cmp b: c = a - b;
874 //a = b: c = 0
875 //a < b: c < 0
876 //a > b: c > 0
877
878 bool invTest = false;
879 unsigned Tmp3;
880
881 ConstantFPSDNode *CN;
882 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
883 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
884 Tmp3 = SelectExpr(SetCC->getOperand(0));
885 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
886 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
887 {
888 Tmp3 = SelectExpr(SetCC->getOperand(1));
889 invTest = true;
890 }
891 else
892 {
893 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
894 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
895 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
896 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
897 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
898 .addReg(Tmp1).addReg(Tmp2);
899 }
900
901 switch (SetCC->getCondition()) {
902 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
903 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
904 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
905 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
906 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
907 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
908 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
909 }
Andrew Lenharth33819132005-03-04 20:09:23 +0000910 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
Andrew Lenharth45859692005-03-03 21:47:53 +0000911 return Result;
912 }
913 else
914 {
915 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000916 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV).addReg(Tmp1);
917// // Spill the cond to memory and reload it from there.
918// unsigned Tmp4 = MakeReg(MVT::f64);
919// MoveIntFP(Tmp1, Tmp4, true);
920// //now ideally, we don't have to do anything to the flag...
921// // Get the condition into the zero flag.
922// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
Andrew Lenharth45859692005-03-03 21:47:53 +0000923 return Result;
924 }
Andrew Lenharth9818c052005-02-05 13:19:12 +0000925 }
926
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000927 case ISD::FP_ROUND:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000928 assert (DestType == MVT::f32 &&
929 N.getOperand(0).getValueType() == MVT::f64 &&
930 "only f64 to f32 conversion supported here");
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000931 Tmp1 = SelectExpr(N.getOperand(0));
932 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
933 return Result;
934
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000935 case ISD::FP_EXTEND:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000936 assert (DestType == MVT::f64 &&
937 N.getOperand(0).getValueType() == MVT::f32 &&
938 "only f32 to f64 conversion supported here");
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000939 Tmp1 = SelectExpr(N.getOperand(0));
940 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
941 return Result;
942
Andrew Lenharth2c594352005-01-29 15:42:07 +0000943 case ISD::CopyFromReg:
944 {
945 // Make sure we generate both values.
946 if (Result != notIn)
947 ExprMap[N.getValue(1)] = notIn; // Generate the token
948 else
949 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
950
951 SDOperand Chain = N.getOperand(0);
952
953 Select(Chain);
954 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
955 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
956 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
957 return Result;
958 }
959
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000960 case ISD::LOAD:
961 {
962 // Make sure we generate both values.
963 if (Result != notIn)
964 ExprMap[N.getValue(1)] = notIn; // Generate the token
965 else
966 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000967
Andrew Lenharth29219162005-02-07 06:31:44 +0000968 DestType = N.getValue(0).getValueType();
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000969
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000970 SDOperand Chain = N.getOperand(0);
971 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000972 Select(Chain);
Andrew Lenharth65838902005-02-06 16:22:15 +0000973 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
974
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000975 if (Address.getOpcode() == ISD::GlobalAddress) {
976 AlphaLowering.restoreGP(BB);
977 Opc = GetSymVersion(Opc);
978 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
979 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000980 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000981 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000982 Opc = GetSymVersion(Opc);
Andrew Lenharth97127a12005-02-05 17:41:39 +0000983 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000984 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000985 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000986 BuildMI(BB, Opc, 2, Result)
987 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
988 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000989 } else {
990 long offset;
991 SelectAddr(Address, Tmp1, offset);
992 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
993 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000994 return Result;
995 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000996 case ISD::ConstantFP:
997 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
998 if (CN->isExactlyValue(+0.0)) {
999 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001000 } else if ( CN->isExactlyValue(-0.0)) {
1001 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001002 } else {
1003 abort();
1004 }
1005 }
1006 return Result;
1007
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001008 case ISD::SDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001009 case ISD::MUL:
1010 case ISD::ADD:
1011 case ISD::SUB:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001012 switch( opcode ) {
1013 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
1014 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
1015 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
1016 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
1017 };
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001018
1019 ConstantFPSDNode *CN;
1020 if (opcode == ISD::SUB
1021 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1022 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1023 {
1024 Tmp2 = SelectExpr(N.getOperand(1));
1025 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1026 } else {
1027 Tmp1 = SelectExpr(N.getOperand(0));
1028 Tmp2 = SelectExpr(N.getOperand(1));
1029 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1030 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001031 return Result;
1032
Andrew Lenharth2c594352005-01-29 15:42:07 +00001033 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001034 {
1035 //include a conversion sequence for float loads to double
1036 if (Result != notIn)
1037 ExprMap[N.getValue(1)] = notIn; // Generate the token
1038 else
1039 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1040
Andrew Lenhartha549deb2005-02-07 05:33:15 +00001041 Tmp1 = MakeReg(MVT::f32);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001042
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001043 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
1044 "EXTLOAD not from f32");
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001045 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
1046
1047 SDOperand Chain = N.getOperand(0);
1048 SDOperand Address = N.getOperand(1);
1049 Select(Chain);
1050
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001051 if (Address.getOpcode() == ISD::GlobalAddress) {
1052 AlphaLowering.restoreGP(BB);
1053 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1054 }
1055 else if (ConstantPoolSDNode *CP =
1056 dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
1057 {
1058 AlphaLowering.restoreGP(BB);
1059 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addConstantPoolIndex(CP->getIndex());
1060 }
1061 else if(Address.getOpcode() == ISD::FrameIndex) {
1062 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
Andrew Lenharth032f2352005-02-22 21:59:48 +00001063 BuildMI(BB, Alpha::LDS, 2, Tmp1)
1064 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1065 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001066 } else {
1067 long offset;
1068 SelectAddr(Address, Tmp2, offset);
1069 BuildMI(BB, Alpha::LDS, 1, Tmp1).addImm(offset).addReg(Tmp2);
1070 }
Andrew Lenharth29219162005-02-07 06:31:44 +00001071 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001072 return Result;
1073 }
Andrew Lenharth2c594352005-01-29 15:42:07 +00001074
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001075 case ISD::UINT_TO_FP:
1076 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001077 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001078 assert (N.getOperand(0).getValueType() == MVT::i64
1079 && "only quads can be loaded from");
Andrew Lenharth40831c52005-01-28 06:57:18 +00001080 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001081 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001082 MoveInt2FP(Tmp1, Tmp2, true);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001083 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1084 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001085 return Result;
1086 }
1087 }
1088 assert(0 && "should not get here");
1089 return 0;
1090}
1091
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001092unsigned ISel::SelectExpr(SDOperand N) {
1093 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +00001094 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001095 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001096 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001097
1098 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001099 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001100
1101 unsigned &Reg = ExprMap[N];
1102 if (Reg) return Reg;
1103
1104 if (N.getOpcode() != ISD::CALL)
1105 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001106 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001107 else {
1108 // If this is a call instruction, make sure to prepare ALL of the result
1109 // values as well as the chain.
1110 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001111 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001112 else {
1113 Result = MakeReg(Node->getValueType(0));
1114 ExprMap[N.getValue(0)] = Result;
1115 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1116 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001117 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001118 }
1119 }
1120
Andrew Lenharth22088bb2005-02-02 15:05:33 +00001121 if (DestType == MVT::f64 || DestType == MVT::f32 ||
Andrew Lenharth06342c32005-02-07 06:21:37 +00001122 (
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001123 (opcode == ISD::LOAD || opcode == ISD::CopyFromReg ||
1124 opcode == ISD::EXTLOAD) &&
1125 (N.getValue(0).getValueType() == MVT::f32 ||
1126 N.getValue(0).getValueType() == MVT::f64)
Andrew Lenharth06342c32005-02-07 06:21:37 +00001127 )
1128 )
Andrew Lenharth40831c52005-01-28 06:57:18 +00001129 return SelectExprFP(N, Result);
1130
1131 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001132 default:
1133 Node->dump();
1134 assert(0 && "Node not handled!\n");
1135
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001136 case ISD::MULHU:
1137 Tmp1 = SelectExpr(N.getOperand(0));
1138 Tmp2 = SelectExpr(N.getOperand(1));
1139 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +00001140 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001141 case ISD::MULHS:
1142 {
1143 //MULHU - Ra<63>*Rb - Rb<63>*Ra
1144 Tmp1 = SelectExpr(N.getOperand(0));
1145 Tmp2 = SelectExpr(N.getOperand(1));
1146 Tmp3 = MakeReg(MVT::i64);
1147 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1148 unsigned V1 = MakeReg(MVT::i64);
1149 unsigned V2 = MakeReg(MVT::i64);
1150 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31).addReg(Tmp1);
1151 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31).addReg(Tmp2);
1152 unsigned IRes = MakeReg(MVT::i64);
1153 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
1154 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
1155 return Result;
1156 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001157 case ISD::UNDEF: {
1158 BuildMI(BB, Alpha::IDEF, 0, Result);
1159 return Result;
1160 }
1161
Andrew Lenharth032f2352005-02-22 21:59:48 +00001162 case ISD::DYNAMIC_STACKALLOC:
1163 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +00001164 if (Result != notIn)
1165 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +00001166 else
1167 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1168
1169 // FIXME: We are currently ignoring the requested alignment for handling
1170 // greater than the stack alignment. This will need to be revisited at some
1171 // point. Align = N.getOperand(2);
1172
1173 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1174 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1175 std::cerr << "Cannot allocate stack object with greater alignment than"
1176 << " the stack alignment yet!";
1177 abort();
1178 }
1179
1180 Select(N.getOperand(0));
1181 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1182 {
1183 if (CN->getValue() < 32000)
1184 {
1185 BuildMI(BB, Alpha::LDA, 2, Alpha::R30)
1186 .addImm(-CN->getValue()).addReg(Alpha::R30);
1187 } else {
1188 Tmp1 = SelectExpr(N.getOperand(1));
1189 // Subtract size from stack pointer, thereby allocating some space.
1190 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1191 }
1192 } else {
1193 Tmp1 = SelectExpr(N.getOperand(1));
1194 // Subtract size from stack pointer, thereby allocating some space.
1195 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1196 }
1197
1198 // Put a pointer to the space into the result register, by copying the stack
1199 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +00001200 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +00001201 return Result;
1202
Andrew Lenharth33819132005-03-04 20:09:23 +00001203// case ISD::ConstantPool:
1204// Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1205// AlphaLowering.restoreGP(BB);
1206// BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
1207// return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +00001208
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001209 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001210 BuildMI(BB, Alpha::LDA, 2, Result)
1211 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1212 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001213 return Result;
1214
1215 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001216 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001217 case ISD::SEXTLOAD:
Andrew Lenhartha549deb2005-02-07 05:33:15 +00001218 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001219 {
1220 // Make sure we generate both values.
1221 if (Result != notIn)
1222 ExprMap[N.getValue(1)] = notIn; // Generate the token
1223 else
1224 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001225
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001226 SDOperand Chain = N.getOperand(0);
1227 SDOperand Address = N.getOperand(1);
1228 Select(Chain);
1229
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001230 assert(Node->getValueType(0) == MVT::i64 &&
1231 "Unknown type to sign extend to.");
Andrew Lenharth03824012005-02-07 05:55:55 +00001232 if (opcode == ISD::LOAD)
1233 Opc = Alpha::LDQ;
1234 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001235 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
1236 default: Node->dump(); assert(0 && "Bad sign extend!");
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001237 case MVT::i32: Opc = Alpha::LDL;
1238 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
1239 case MVT::i16: Opc = Alpha::LDWU;
1240 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001241 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001242 case MVT::i8: Opc = Alpha::LDBU;
1243 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001244 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001245
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001246 if (Address.getOpcode() == ISD::GlobalAddress) {
1247 AlphaLowering.restoreGP(BB);
1248 Opc = GetSymVersion(Opc);
1249 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1250 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001251 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1252 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001253 Opc = GetSymVersion(Opc);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001254 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001255 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001256 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001257 BuildMI(BB, Opc, 2, Result)
1258 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1259 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001260 } else {
1261 long offset;
1262 SelectAddr(Address, Tmp1, offset);
1263 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1264 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001265 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001266 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001267
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001268 case ISD::GlobalAddress:
1269 AlphaLowering.restoreGP(BB);
1270 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
1271 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
1272 return Result;
1273
1274 case ISD::CALL:
1275 {
1276 Select(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001277
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001278 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001279 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001280
1281 //grab the arguments
1282 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001283 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001284 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001285 argvregs.push_back(SelectExpr(N.getOperand(i)));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001286
Andrew Lenharth684f2292005-01-30 00:35:27 +00001287 //in reg args
1288 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001289 {
1290 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
1291 Alpha::R19, Alpha::R20, Alpha::R21};
1292 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
1293 Alpha::F19, Alpha::F20, Alpha::F21};
1294 switch(N.getOperand(i+2).getValueType()) {
1295 default:
1296 Node->dump();
1297 N.getOperand(i).Val->dump();
1298 std::cerr << "Type for " << i << " is: " <<
1299 N.getOperand(i+2).getValueType() << "\n";
1300 assert(0 && "Unknown value type for call");
1301 case MVT::i1:
1302 case MVT::i8:
1303 case MVT::i16:
1304 case MVT::i32:
1305 case MVT::i64:
1306 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1307 break;
1308 case MVT::f32:
1309 case MVT::f64:
1310 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1311 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001312 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001313 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001314 //in mem args
1315 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001316 {
1317 switch(N.getOperand(i+2).getValueType()) {
1318 default:
1319 Node->dump();
1320 N.getOperand(i).Val->dump();
1321 std::cerr << "Type for " << i << " is: " <<
1322 N.getOperand(i+2).getValueType() << "\n";
1323 assert(0 && "Unknown value type for call");
1324 case MVT::i1:
1325 case MVT::i8:
1326 case MVT::i16:
1327 case MVT::i32:
1328 case MVT::i64:
1329 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1330 break;
1331 case MVT::f32:
1332 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1333 break;
1334 case MVT::f64:
1335 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1336 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001337 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001338 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001339 //build the right kind of call
1340 if (GlobalAddressSDNode *GASD =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001341 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001342 {
Andrew Lenharth3e315922005-02-10 20:10:38 +00001343 //if (GASD->getGlobal()->isExternal()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001344 //use safe calling convention
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001345 AlphaLowering.restoreGP(BB);
1346 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal(),true);
Andrew Lenharth3e315922005-02-10 20:10:38 +00001347 //} else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001348 //use PC relative branch call
Andrew Lenharth3e315922005-02-10 20:10:38 +00001349 //BuildMI(BB, Alpha::BSR, 1, Alpha::R26).addGlobalAddress(GASD->getGlobal(),true);
1350 //}
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001351 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001352 else if (ExternalSymbolSDNode *ESSDN =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001353 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001354 {
1355 AlphaLowering.restoreGP(BB);
Andrew Lenharthba05ad62005-03-30 18:22:52 +00001356 BuildMI(BB, Alpha::CALL, 1).addExternalSymbol(ESSDN->getSymbol(), true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001357 } else {
1358 //no need to restore GP as we are doing an indirect call
1359 Tmp1 = SelectExpr(N.getOperand(1));
1360 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1361 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1362 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001363
1364 //push the result into a virtual register
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001365
1366 switch (Node->getValueType(0)) {
1367 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001368 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001369 case MVT::i1:
1370 case MVT::i8:
1371 case MVT::i16:
1372 case MVT::i32:
1373 case MVT::i64:
1374 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1375 break;
1376 case MVT::f32:
1377 case MVT::f64:
1378 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1379 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001380 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001381 return Result+N.ResNo;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001382 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001383
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001384 case ISD::SIGN_EXTEND_INREG:
1385 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001386 //do SDIV opt for all levels of ints
Andrew Lenharth5e99dd92005-03-31 22:02:25 +00001387 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001388 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001389 unsigned Tmp4 = MakeReg(MVT::f64);
1390 unsigned Tmp5 = MakeReg(MVT::f64);
1391 unsigned Tmp6 = MakeReg(MVT::f64);
1392 unsigned Tmp7 = MakeReg(MVT::f64);
1393 unsigned Tmp8 = MakeReg(MVT::f64);
1394 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001395
1396 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1397 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1398 MoveInt2FP(Tmp1, Tmp4, true);
1399 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001400 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Tmp4);
1401 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Tmp5);
1402 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
1403 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001404 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001405 return Result;
1406 }
1407
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001408 //Alpha has instructions for a bunch of signed 32 bit stuff
1409 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001410 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001411 switch (N.getOperand(0).getOpcode()) {
1412 case ISD::ADD:
1413 case ISD::SUB:
1414 case ISD::MUL:
1415 {
1416 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1417 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1418 //FIXME: first check for Scaled Adds and Subs!
1419 if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1420 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
1421 { //Normal imm add/sub
1422 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth093f3272005-02-12 21:11:17 +00001423 //if the value was really originally a i32, skip the up conversion
1424 if (N.getOperand(0).getOperand(0).getOpcode() == ISD::SIGN_EXTEND_INREG &&
1425 dyn_cast<MVTSDNode>(N.getOperand(0).getOperand(0).Val)
1426 ->getExtraValueType() == MVT::i32)
1427 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1428 else
1429 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001430 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1431 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001432 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001433 else
1434 { //Normal add/sub
1435 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULLi : Alpha::SUBL);
Andrew Lenharth093f3272005-02-12 21:11:17 +00001436 //if the value was really originally a i32, skip the up conversion
1437 if (N.getOperand(0).getOperand(0).getOpcode() == ISD::SIGN_EXTEND_INREG &&
1438 dyn_cast<MVTSDNode>(N.getOperand(0).getOperand(0).Val)
1439 ->getExtraValueType() == MVT::i32)
1440 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1441 else
1442 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1443 //if the value was really originally a i32, skip the up conversion
1444 if (N.getOperand(0).getOperand(1).getOpcode() == ISD::SIGN_EXTEND_INREG &&
1445 dyn_cast<MVTSDNode>(N.getOperand(0).getOperand(1).Val)
1446 ->getExtraValueType() == MVT::i32)
1447 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1448 else
1449 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1450
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001451 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001452 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1453 }
1454 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001455 }
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001456 case ISD::SEXTLOAD:
1457 //SelectionDag isn't deleting the signextend after sextloads
1458 Reg = Result = SelectExpr(N.getOperand(0));
1459 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001460 default: break; //Fall Though;
1461 }
1462 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001463 Tmp1 = SelectExpr(N.getOperand(0));
1464 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001465 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001466 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001467 {
1468 default:
1469 Node->dump();
1470 assert(0 && "Sign Extend InReg not there yet");
1471 break;
1472 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001473 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001474 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001475 break;
1476 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001477 case MVT::i16:
1478 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
1479 break;
1480 case MVT::i8:
1481 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
1482 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001483 case MVT::i1:
1484 Tmp2 = MakeReg(MVT::i64);
1485 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenharth7536eea2005-02-12 20:42:09 +00001486 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::F31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001487 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001488 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001489 return Result;
1490 }
1491 case ISD::ZERO_EXTEND_INREG:
1492 {
1493 Tmp1 = SelectExpr(N.getOperand(0));
1494 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001495 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001496 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001497 {
1498 default:
1499 Node->dump();
1500 assert(0 && "Zero Extend InReg not there yet");
1501 break;
1502 case MVT::i32: Tmp2 = 0xf0; break;
1503 case MVT::i16: Tmp2 = 0xfc; break;
1504 case MVT::i8: Tmp2 = 0xfe; break;
1505 case MVT::i1: //handle this one special
1506 BuildMI(BB, Alpha::ANDi, 2, Result).addReg(Tmp1).addImm(1);
1507 return Result;
1508 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001509 BuildMI(BB, Alpha::ZAPi, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001510 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001511 }
1512
1513 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001514 {
1515 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1516 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1517 bool isConst1 = false;
1518 bool isConst2 = false;
1519 int dir;
Andrew Lenharth9818c052005-02-05 13:19:12 +00001520
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001521 //Tmp1 = SelectExpr(N.getOperand(0));
1522 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001523 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1524 isConst1 = true;
1525 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001526 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1527 isConst2 = true;
1528
1529 switch (SetCC->getCondition()) {
1530 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1531 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001532 case ISD::SETLT:
1533 Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
1534 case ISD::SETLE:
1535 Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
1536 case ISD::SETGT:
1537 Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
1538 case ISD::SETGE:
1539 Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
1540 case ISD::SETULT:
1541 Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
1542 case ISD::SETUGT:
1543 Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
1544 case ISD::SETULE:
1545 Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
1546 case ISD::SETUGE:
1547 Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001548 case ISD::SETNE: {//Handle this one special
1549 //std::cerr << "Alpha does not have a setne.\n";
1550 //abort();
1551 Tmp1 = SelectExpr(N.getOperand(0));
1552 Tmp2 = SelectExpr(N.getOperand(1));
1553 Tmp3 = MakeReg(MVT::i64);
1554 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001555 //Remeber we have the Inv for this CC
1556 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001557 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001558 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001559 return Result;
1560 }
1561 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001562 if (dir == 1) {
1563 Tmp1 = SelectExpr(N.getOperand(0));
1564 if (isConst2) {
1565 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1566 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1567 } else {
1568 Tmp2 = SelectExpr(N.getOperand(1));
1569 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1570 }
1571 } else if (dir == 2) {
1572 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001573 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001574 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1575 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1576 } else {
1577 Tmp2 = SelectExpr(N.getOperand(0));
1578 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1579 }
1580 } else { //dir == 0
1581 if (isConst1) {
1582 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1583 Tmp2 = SelectExpr(N.getOperand(1));
1584 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
1585 } else if (isConst2) {
1586 Tmp1 = SelectExpr(N.getOperand(0));
1587 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1588 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1589 } else {
1590 Tmp1 = SelectExpr(N.getOperand(0));
1591 Tmp2 = SelectExpr(N.getOperand(1));
1592 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1593 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001594 }
1595 } else {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001596 //do the comparison
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001597 Tmp1 = MakeReg(MVT::f64);
1598 bool inv = SelectFPSetCC(N, Tmp1);
1599
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001600 //now arrange for Result (int) to have a 1 or 0
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001601 Tmp2 = MakeReg(MVT::i64);
1602 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001603 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001604 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001605 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001606 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001607 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001608 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001609
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001610 case ISD::CopyFromReg:
1611 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001612 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001613 if (Result != notIn)
1614 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001615 else
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001616 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth40831c52005-01-28 06:57:18 +00001617
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001618 SDOperand Chain = N.getOperand(0);
1619
1620 Select(Chain);
1621 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1622 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1623 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1624 return Result;
1625 }
1626
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001627 //Most of the plain arithmetic and logic share the same form, and the same
1628 //constant immediate test
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001629 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001630 //Match Not
1631 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1632 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue())
1633 {
1634 Tmp1 = SelectExpr(N.getOperand(0));
1635 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1636 return Result;
1637 }
1638 //Fall through
1639 case ISD::AND:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001640 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001641 //Check operand(0) == Not
1642 if (N.getOperand(0).getOpcode() == ISD::OR &&
1643 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1644 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue())
1645 {
1646 switch(opcode) {
1647 case ISD::AND: Opc = Alpha::BIC; break;
1648 case ISD::OR: Opc = Alpha::ORNOT; break;
1649 case ISD::XOR: Opc = Alpha::EQV; break;
1650 }
1651 Tmp1 = SelectExpr(N.getOperand(1));
1652 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1653 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1654 return Result;
1655 }
1656 //Check operand(1) == Not
1657 if (N.getOperand(1).getOpcode() == ISD::OR &&
1658 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
1659 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->isAllOnesValue())
1660 {
1661 switch(opcode) {
1662 case ISD::AND: Opc = Alpha::BIC; break;
1663 case ISD::OR: Opc = Alpha::ORNOT; break;
1664 case ISD::XOR: Opc = Alpha::EQV; break;
1665 }
1666 Tmp1 = SelectExpr(N.getOperand(0));
1667 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1668 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1669 return Result;
1670 }
1671 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001672 case ISD::SHL:
1673 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001674 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001675 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001676 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1677 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001678 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001679 {
1680 switch(opcode) {
1681 case ISD::AND: Opc = Alpha::ANDi; break;
1682 case ISD::OR: Opc = Alpha::BISi; break;
1683 case ISD::XOR: Opc = Alpha::XORi; break;
1684 case ISD::SHL: Opc = Alpha::SLi; break;
1685 case ISD::SRL: Opc = Alpha::SRLi; break;
1686 case ISD::SRA: Opc = Alpha::SRAi; break;
1687 case ISD::MUL: Opc = Alpha::MULQi; break;
1688 };
1689 Tmp1 = SelectExpr(N.getOperand(0));
1690 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1691 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1692 } else {
1693 switch(opcode) {
1694 case ISD::AND: Opc = Alpha::AND; break;
1695 case ISD::OR: Opc = Alpha::BIS; break;
1696 case ISD::XOR: Opc = Alpha::XOR; break;
1697 case ISD::SHL: Opc = Alpha::SL; break;
1698 case ISD::SRL: Opc = Alpha::SRL; break;
1699 case ISD::SRA: Opc = Alpha::SRA; break;
1700 case ISD::MUL: Opc = Alpha::MULQ; break;
1701 };
1702 Tmp1 = SelectExpr(N.getOperand(0));
1703 Tmp2 = SelectExpr(N.getOperand(1));
1704 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1705 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001706 return Result;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001707
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001708 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001709 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001710 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001711 bool isAdd = opcode == ISD::ADD;
1712
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001713 //first check for Scaled Adds and Subs!
1714 //Valid for add and sub
1715 if(N.getOperand(0).getOpcode() == ISD::SHL &&
1716 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1717 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() == 2)
1718 {
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001719 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001720 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1721 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1722 BuildMI(BB, isAdd?Alpha::S4ADDQi:Alpha::S4SUBQi, 2, Result).addReg(Tmp2)
1723 .addImm(cast<ConstantSDNode>(N.getOperand(1))->getValue());
1724 else {
1725 Tmp1 = SelectExpr(N.getOperand(1));
1726 BuildMI(BB, isAdd?Alpha::S4ADDQ:Alpha::S4SUBQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
1727 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001728 }
1729 else if(N.getOperand(0).getOpcode() == ISD::SHL &&
1730 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1731 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() == 3)
1732 {
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001733 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001734 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1735 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1736 BuildMI(BB, isAdd?Alpha::S8ADDQi:Alpha::S8SUBQi, 2, Result).addReg(Tmp2)
1737 .addImm(cast<ConstantSDNode>(N.getOperand(1))->getValue());
1738 else {
1739 Tmp1 = SelectExpr(N.getOperand(1));
1740 BuildMI(BB, isAdd?Alpha::S8ADDQ:Alpha::S8SUBQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
1741 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001742 }
1743 //Position prevents subs
1744 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &
1745 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
1746 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue() == 2)
1747 {
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001748 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001749 if (N.getOperand(0).getOpcode() == ISD::Constant &&
1750 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1751 BuildMI(BB, Alpha::S4ADDQi, 2, Result).addReg(Tmp2)
1752 .addImm(cast<ConstantSDNode>(N.getOperand(0))->getValue());
1753 else {
1754 Tmp1 = SelectExpr(N.getOperand(0));
1755 BuildMI(BB, Alpha::S4ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
1756 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001757 }
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001758 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001759 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
1760 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue() == 3)
1761 {
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001762 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001763 if (N.getOperand(0).getOpcode() == ISD::Constant &&
1764 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1765 BuildMI(BB, Alpha::S8ADDQi, 2, Result).addReg(Tmp2)
1766 .addImm(cast<ConstantSDNode>(N.getOperand(0))->getValue());
1767 else {
1768 Tmp1 = SelectExpr(N.getOperand(0));
1769 BuildMI(BB, Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
1770 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001771 }
1772 //small addi
1773 else if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001774 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001775 { //Normal imm add/sub
1776 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1777 Tmp1 = SelectExpr(N.getOperand(0));
1778 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1779 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1780 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001781 //larger addi
Andrew Lenharth40831c52005-01-28 06:57:18 +00001782 else if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001783 (cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767 ||
1784 (long)cast<ConstantSDNode>(N.getOperand(1))->getValue() >= -32767))
1785 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001786 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001787 Tmp2 = (long)cast<ConstantSDNode>(N.getOperand(1))->getValue();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001788 if (!isAdd)
1789 Tmp2 = -Tmp2;
1790 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001791 }
1792 //give up and do the operation
1793 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001794 //Normal add/sub
1795 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1796 Tmp1 = SelectExpr(N.getOperand(0));
1797 Tmp2 = SelectExpr(N.getOperand(1));
1798 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1799 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001800 return Result;
1801 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001802
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001803 case ISD::SDIV:
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001804 {
Andrew Lenhartha565c272005-04-06 22:03:13 +00001805 ConstantSDNode* CSD;
1806 //check if we can convert into a shift!
1807 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1808 (int64_t)CSD->getSignExtended() != 0 &&
1809 ExactLog2(abs((int64_t)CSD->getSignExtended())) != 0)
1810 {
1811 unsigned k = ExactLog2(abs(CSD->getSignExtended()));
1812 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001813 if (k == 1)
1814 Tmp2 = Tmp1;
1815 else
1816 {
1817 Tmp2 = MakeReg(MVT::i64);
1818 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1819 }
1820 Tmp3 = MakeReg(MVT::i64);
1821 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1822 unsigned Tmp4 = MakeReg(MVT::i64);
1823 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
1824 if ((int64_t)CSD->getSignExtended() > 0)
1825 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
1826 else
1827 {
1828 unsigned Tmp5 = MakeReg(MVT::i64);
1829 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
1830 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
1831 }
1832 return Result;
1833 }
1834 }
1835 //Else fall through
1836
1837 case ISD::UDIV:
1838 {
1839 ConstantSDNode* CSD;
1840 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1841 ((int64_t)CSD->getSignExtended() >= 2 ||
1842 (int64_t)CSD->getSignExtended() <= -2))
1843 {
1844 // If this is a divide by constant, we can emit code using some magic
1845 // constants to implement it as a multiply instead.
1846 ExprMap.erase(N);
1847 if (opcode == ISD::SDIV)
1848 return SelectExpr(BuildSDIVSequence(N));
1849 else
1850 return SelectExpr(BuildUDIVSequence(N));
1851 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001852 }
1853 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001854 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00001855 case ISD::SREM:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001856 //FIXME: alpha really doesn't support any of these operations,
1857 // the ops are expanded into special library calls with
1858 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001859 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00001860 switch(opcode) {
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001861 case ISD::UREM: Opc = Alpha::REMQU; break;
1862 case ISD::SREM: Opc = Alpha::REMQ; break;
1863 case ISD::UDIV: Opc = Alpha::DIVQU; break;
1864 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001865 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001866 Tmp1 = SelectExpr(N.getOperand(0));
1867 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth33819132005-03-04 20:09:23 +00001868 //set up regs explicitly (helps Reg alloc)
1869 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
1870 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001871 AlphaLowering.restoreGP(BB);
Andrew Lenharth33819132005-03-04 20:09:23 +00001872 BuildMI(BB, Opc, 2).addReg(Alpha::R24).addReg(Alpha::R25);
1873 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001874 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001875
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001876 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001877 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001878 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001879 assert (DestType == MVT::i64 && "only quads can be loaded to");
1880 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001881 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001882 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001883 if (SrcType == MVT::f32)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001884 {
1885 Tmp2 = MakeReg(MVT::f64);
1886 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1887 Tmp1 = Tmp2;
1888 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001889 Tmp2 = MakeReg(MVT::f64);
1890 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001891 MoveFP2Int(Tmp2, Result, true);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001892
1893 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001894 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001895
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001896 case ISD::SELECT:
1897 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001898 //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 +00001899 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001900 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1901 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001902 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001903 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001904
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001905 SDOperand CC = N.getOperand(0);
1906 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
1907
1908 if (CC.getOpcode() == ISD::SETCC &&
1909 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
1910 { //FP Setcc -> Int Select
1911 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001912 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1913 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001914 bool inv = SelectFPSetCC(CC, Tmp1);
1915 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
1916 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
1917 return Result;
1918 }
1919 if (CC.getOpcode() == ISD::SETCC) {
1920 //Int SetCC -> Select
1921 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001922 if((SetCC->getOperand(1).getOpcode() == ISD::Constant &&
1923 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0) ||
1924 (SetCC->getOperand(0).getOpcode() == ISD::Constant &&
1925 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0))
1926 {
1927 //figure out a few things
1928 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
1929 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
1930 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
1931 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
1932 bool LeftConst = N.getOperand(1).getOpcode() == ISD::Constant &&
1933 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255;
1934 bool RightConst = N.getOperand(2).getOpcode() == ISD::Constant &&
1935 cast<ConstantSDNode>(N.getOperand(2))->getValue() <= 255;
1936 bool useImm = LeftConst || RightConst;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001937
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001938 //Fix up CC
1939 ISD::CondCode cCode= SetCC->getCondition();
1940 if (RightConst && !LeftConst) //Invert sense to get Imm field right
1941 cCode = ISD::getSetCCInverse(cCode, true);
1942 if (LeftZero && !RightZero) //Swap Operands
1943 cCode = ISD::getSetCCSwappedOperands(cCode);
1944
1945 //Choose the CMOV
1946 switch (cCode) {
1947 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
1948 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
1949 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
1950 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
1951 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
1952 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
1953 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
1954 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
1955 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break; //Technically you could have this CC
1956 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
1957 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
1958 }
1959 if(LeftZero && !RightZero) //swap Operands
1960 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
1961 else
1962 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
1963
1964 if (LeftConst) {
1965 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1966 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
1967 .addImm(cast<ConstantSDNode>(N.getOperand(1))->getValue())
1968 .addReg(Tmp1);
1969 } else if (RightConst) {
1970 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
1971 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
1972 .addImm(cast<ConstantSDNode>(N.getOperand(2))->getValue())
1973 .addReg(Tmp1);
1974 } else {
1975 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1976 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1977 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
1978 }
1979 return Result;
1980 }
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001981 //Otherwise, fall though
1982 }
1983 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001984 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1985 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001986 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001987
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001988 return Result;
1989 }
1990
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001991 case ISD::Constant:
1992 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00001993 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00001994 if (val <= IMM_HIGH && val >= IMM_LOW) {
1995 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
1996 }
1997 else if (val <= (int64_t)IMM_HIGH + (int64_t)IMM_HIGH * (int64_t)IMM_MULT &&
1998 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
1999 Tmp1 = MakeReg(MVT::i64);
2000 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val)).addReg(Alpha::R31);
2001 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
2002 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002003 else {
2004 MachineConstantPool *CP = BB->getParent()->getConstantPool();
2005 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
2006 unsigned CPI = CP->getConstantPoolIndex(C);
2007 AlphaLowering.restoreGP(BB);
2008 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
2009 }
2010 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002011 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002012 }
2013
2014 return 0;
2015}
2016
2017void ISel::Select(SDOperand N) {
2018 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002019 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002020
Nate Begeman85fdeb22005-03-24 04:39:54 +00002021 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002022 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002023
2024 SDNode *Node = N.Val;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002025
Andrew Lenharth760270d2005-02-07 23:02:23 +00002026 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002027
2028 default:
2029 Node->dump(); std::cerr << "\n";
2030 assert(0 && "Node not handled yet!");
2031
2032 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002033 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002034 return;
2035 }
2036
2037 case ISD::BR: {
2038 MachineBasicBlock *Dest =
2039 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2040
2041 Select(N.getOperand(0));
2042 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2043 return;
2044 }
2045
2046 case ISD::ImplicitDef:
2047 Select(N.getOperand(0));
2048 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2049 return;
2050
2051 case ISD::EntryToken: return; // Noop
2052
2053 case ISD::TokenFactor:
2054 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2055 Select(Node->getOperand(i));
2056
2057 //N.Val->dump(); std::cerr << "\n";
2058 //assert(0 && "Node not handled yet!");
2059
2060 return;
2061
2062 case ISD::CopyToReg:
2063 Select(N.getOperand(0));
2064 Tmp1 = SelectExpr(N.getOperand(1));
2065 Tmp2 = cast<RegSDNode>(N)->getReg();
2066
2067 if (Tmp1 != Tmp2) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002068 if (N.getOperand(1).getValueType() == MVT::f64 ||
2069 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002070 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2071 else
2072 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002073 }
2074 return;
2075
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002076 case ISD::RET:
2077 switch (N.getNumOperands()) {
2078 default:
2079 std::cerr << N.getNumOperands() << "\n";
2080 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2081 std::cerr << N.getOperand(i).getValueType() << "\n";
2082 Node->dump();
2083 assert(0 && "Unknown return instruction!");
2084 case 2:
2085 Select(N.getOperand(0));
2086 Tmp1 = SelectExpr(N.getOperand(1));
2087 switch (N.getOperand(1).getValueType()) {
2088 default: Node->dump();
2089 assert(0 && "All other types should have been promoted!!");
2090 case MVT::f64:
2091 case MVT::f32:
2092 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2093 break;
2094 case MVT::i32:
2095 case MVT::i64:
2096 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2097 break;
2098 }
2099 break;
2100 case 1:
2101 Select(N.getOperand(0));
2102 break;
2103 }
2104 //Tmp2 = AlphaLowering.getRetAddr();
2105 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
2106 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
2107 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002108
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00002109 case ISD::TRUNCSTORE:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002110 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002111 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002112 SDOperand Chain = N.getOperand(0);
2113 SDOperand Value = N.getOperand(1);
2114 SDOperand Address = N.getOperand(2);
2115 Select(Chain);
2116
2117 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002118
2119 if (opcode == ISD::STORE) {
2120 switch(Value.getValueType()) {
2121 default: assert(0 && "unknown Type in store");
2122 case MVT::i64: Opc = Alpha::STQ; break;
2123 case MVT::f64: Opc = Alpha::STT; break;
2124 case MVT::f32: Opc = Alpha::STS; break;
2125 }
2126 } else { //ISD::TRUNCSTORE
2127 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2128 default: assert(0 && "unknown Type in store");
2129 case MVT::i1: //FIXME: DAG does not promote this load
2130 case MVT::i8: Opc = Alpha::STB; break;
2131 case MVT::i16: Opc = Alpha::STW; break;
2132 case MVT::i32: Opc = Alpha::STL; break;
2133 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002134 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002135
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002136 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002137 {
2138 AlphaLowering.restoreGP(BB);
2139 Opc = GetSymVersion(Opc);
2140 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
2141 }
Andrew Lenharth05380342005-02-07 05:07:00 +00002142 else if(Address.getOpcode() == ISD::FrameIndex)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002143 {
Andrew Lenharth032f2352005-02-22 21:59:48 +00002144 BuildMI(BB, Opc, 3).addReg(Tmp1)
2145 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2146 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002147 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002148 else
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002149 {
2150 long offset;
2151 SelectAddr(Address, Tmp2, offset);
2152 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2153 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002154 return;
2155 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002156
2157 case ISD::EXTLOAD:
2158 case ISD::SEXTLOAD:
2159 case ISD::ZEXTLOAD:
2160 case ISD::LOAD:
2161 case ISD::CopyFromReg:
2162 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002163 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002164 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002165 SelectExpr(N);
2166 return;
2167
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002168 case ISD::ADJCALLSTACKDOWN:
2169 case ISD::ADJCALLSTACKUP:
2170 Select(N.getOperand(0));
2171 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2172
2173 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? Alpha::ADJUSTSTACKDOWN :
2174 Alpha::ADJUSTSTACKUP;
2175 BuildMI(BB, Opc, 1).addImm(Tmp1);
2176 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002177
2178 case ISD::PCMARKER:
2179 Select(N.getOperand(0)); //Chain
2180 BuildMI(BB, Alpha::PCLABEL, 2).addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
2181 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002182 }
2183 assert(0 && "Should not be reached!");
2184}
2185
2186
2187/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2188/// into a machine code representation using pattern matching and a machine
2189/// description file.
2190///
2191FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
2192 return new ISel(TM);
2193}