blob: d046ce6b4c3691b0d886765dbaede00a7ecb1886 [file] [log] [blame]
Misha Brukman2a8350a2005-02-05 02:24:26 +00001//===- AlphaISelPattern.cpp - A pattern matching inst selector for Alpha --===//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for Alpha.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Alpha.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000015#include "AlphaRegisterInfo.h"
16#include "llvm/Constants.h" // FIXME: REMOVE
17#include "llvm/Function.h"
18#include "llvm/CodeGen/MachineInstrBuilder.h"
19#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/CodeGen/SSARegMap.h"
25#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetLowering.h"
27#include "llvm/Support/MathExtras.h"
28#include "llvm/ADT/Statistic.h"
Andrew Lenharth032f2352005-02-22 21:59:48 +000029#include "llvm/Support/Debug.h"
Andrew Lenharth95762122005-03-31 21:24:06 +000030#include "llvm/Support/CommandLine.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000031#include <set>
Andrew Lenharth684f2292005-01-30 00:35:27 +000032#include <algorithm>
Andrew Lenharth304d0f32005-01-22 23:41:55 +000033using namespace llvm;
34
Andrew Lenharth95762122005-03-31 21:24:06 +000035namespace llvm {
36 cl::opt<bool> EnableAlphaIDIV("enable-alpha-intfpdiv",
37 cl::desc("Use the FP div instruction for integer div when possible"),
38 cl::Hidden);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +000039 cl::opt<bool> EnableAlphaFTOI("enable-alpha-ftoi",
Andrew Lenharth5e99dd92005-03-31 22:02:25 +000040 cl::desc("Enable use of ftoi* and itof* instructions (ev6 and higher)"),
Andrew Lenharth95762122005-03-31 21:24:06 +000041 cl::Hidden);
42}
43
Andrew Lenharth304d0f32005-01-22 23:41:55 +000044//===----------------------------------------------------------------------===//
45// AlphaTargetLowering - Alpha Implementation of the TargetLowering interface
46namespace {
47 class AlphaTargetLowering : public TargetLowering {
48 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
49 unsigned GP; //GOT vreg
50 public:
51 AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
52 // Set up the TargetLowering object.
Andrew Lenharth3d65d312005-01-27 03:49:45 +000053 //I am having problems with shr n ubyte 1
Andrew Lenharth879ef222005-02-02 17:00:21 +000054 setShiftAmountType(MVT::i64);
55 setSetCCResultType(MVT::i64);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000056
Andrew Lenharth304d0f32005-01-22 23:41:55 +000057 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
58 addRegisterClass(MVT::f64, Alpha::FPRCRegisterClass);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000059 addRegisterClass(MVT::f32, Alpha::FPRCRegisterClass);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000060
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000061 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
62 setOperationAction(ISD::EXTLOAD , MVT::f32 , Promote);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000063
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000064 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
65 setOperationAction(ISD::ZEXTLOAD , MVT::i32 , Expand);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000066
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000067 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
68 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
69 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000070
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000071 setOperationAction(ISD::SREM , MVT::f32 , Expand);
72 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +000073
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000074 //If this didn't legalize into a div....
75 // setOperationAction(ISD::SREM , MVT::i64, Expand);
76 // setOperationAction(ISD::UREM , MVT::i64, Expand);
77
78 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
79 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
80 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
Andrew Lenharth9818c052005-02-05 13:19:12 +000081
Andrew Lenharth33819132005-03-04 20:09:23 +000082 //Doesn't work yet
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000083 setOperationAction(ISD::SETCC , MVT::f32, Promote);
Andrew Lenharth572af902005-02-14 05:41:43 +000084
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000085 computeRegisterProperties();
Andrew Lenharth304d0f32005-01-22 23:41:55 +000086
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000087 addLegalFPImmediate(+0.0); //F31
88 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +000089 }
90
91 /// LowerArguments - This hook must be implemented to indicate how we should
92 /// lower the arguments for the specified function, into the specified DAG.
93 virtual std::vector<SDOperand>
94 LowerArguments(Function &F, SelectionDAG &DAG);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000095
Andrew Lenharth304d0f32005-01-22 23:41:55 +000096 /// LowerCallTo - This hook lowers an abstract call to a function into an
97 /// actual call.
98 virtual std::pair<SDOperand, SDOperand>
Nate Begeman8e21e712005-03-26 01:29:23 +000099 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
100 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000101
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000102 virtual std::pair<SDOperand, SDOperand>
103 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000104
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000105 virtual std::pair<SDOperand,SDOperand>
106 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
107 const Type *ArgTy, SelectionDAG &DAG);
108
109 virtual std::pair<SDOperand, SDOperand>
110 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
111 SelectionDAG &DAG);
112
113 void restoreGP(MachineBasicBlock* BB)
114 {
115 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
116 }
117 };
118}
119
120//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
121
122//For now, just use variable size stack frame format
123
124//In a standard call, the first six items are passed in registers $16
125//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
126//of argument-to-register correspondence.) The remaining items are
127//collected in a memory argument list that is a naturally aligned
128//array of quadwords. In a standard call, this list, if present, must
129//be passed at 0(SP).
130//7 ... n 0(SP) ... (n-7)*8(SP)
131
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000132// //#define FP $15
133// //#define RA $26
134// //#define PV $27
135// //#define GP $29
136// //#define SP $30
137
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000138std::vector<SDOperand>
139AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
140{
141 std::vector<SDOperand> ArgValues;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000142 std::vector<SDOperand> LS;
143 SDOperand Chain = DAG.getRoot();
144
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000145 // assert(0 && "TODO");
146 MachineFunction &MF = DAG.getMachineFunction();
Andrew Lenharth05380342005-02-07 05:07:00 +0000147 MachineFrameInfo*MFI = MF.getFrameInfo();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000148
149 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
150 MachineBasicBlock& BB = MF.front();
151
152 //Handle the return address
153 //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
154
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000155 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
156 Alpha::R19, Alpha::R20, Alpha::R21};
157 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
158 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000159 int count = 0;
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000160
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000161 //Def incoming registers
162 {
163 Function::arg_iterator I = F.arg_begin();
164 Function::arg_iterator E = F.arg_end();
165 for (int i = 0; i < 6; ++i)
166 {
167 if (F.isVarArg()) {
168 BuildMI(&BB, Alpha::IDEF, 0, args_int[i]);
169 BuildMI(&BB, Alpha::IDEF, 0, args_float[i]);
170 } else if (I != E)
171 {
172 if(MVT::isInteger(getValueType(I->getType())))
173 BuildMI(&BB, Alpha::IDEF, 0, args_int[i]);
174 else
175 BuildMI(&BB, Alpha::IDEF, 0, args_float[i]);
176 ++I;
177 }
178 }
179 }
180
181 BuildMI(&BB, Alpha::IDEF, 0, Alpha::R29);
182 BuildMI(&BB, Alpha::BIS, 2, GP).addReg(Alpha::R29).addReg(Alpha::R29);
183
Chris Lattnere4d5c442005-03-15 04:54:21 +0000184 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000185 {
186 SDOperand newroot, argt;
187 if (count < 6) {
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000188 unsigned Vreg;
189 MVT::ValueType VT = getValueType(I->getType());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000190 switch (getValueType(I->getType())) {
191 default:
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000192 std::cerr << "Unknown Type " << VT << "\n";
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000193 abort();
194 case MVT::f64:
195 case MVT::f32:
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000196 Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(VT));
197 BuildMI(&BB, Alpha::CPYS, 2, Vreg).addReg(args_float[count]).addReg(args_float[count]);
198 argt = newroot = DAG.getCopyFromReg(Vreg,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000199 getValueType(I->getType()),
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000200 Chain);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000201 break;
202 case MVT::i1:
203 case MVT::i8:
204 case MVT::i16:
205 case MVT::i32:
206 case MVT::i64:
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000207 Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
208 BuildMI(&BB, Alpha::BIS, 2, Vreg).addReg(args_int[count]).addReg(args_int[count]);
209 argt = newroot = DAG.getCopyFromReg(Vreg, MVT::i64, Chain);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000210 if (getValueType(I->getType()) != MVT::i64)
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000211 argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()), newroot);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000212 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000213 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000214 } else { //more args
215 // Create the frame index object for this incoming parameter...
216 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
217
218 // Create the SelectionDAG nodes corresponding to a load
219 //from this parameter
220 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
221 argt = newroot = DAG.getLoad(getValueType(I->getType()),
222 DAG.getEntryNode(), FIN);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000223 }
Andrew Lenharth032f2352005-02-22 21:59:48 +0000224 ++count;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000225 LS.push_back(newroot.getValue(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000226 ArgValues.push_back(argt);
227 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000228
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000229 // If the functions takes variable number of arguments, copy all regs to stack
230 if (F.isVarArg())
231 for (int i = 0; i < 6; ++i)
232 {
233 unsigned Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
234 BuildMI(&BB, Alpha::BIS, 2, Vreg).addReg(args_int[i]).addReg(args_int[i]);
235 SDOperand argt = DAG.getCopyFromReg(Vreg, MVT::i64, Chain);
236 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
237 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
238 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, argt, SDFI));
239
240 Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::f64));
241 BuildMI(&BB, Alpha::CPYS, 2, Vreg).addReg(args_float[i]).addReg(args_float[i]);
242 argt = DAG.getCopyFromReg(Vreg, MVT::f64, Chain);
243 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
244 SDFI = DAG.getFrameIndex(FI, MVT::i64);
245 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, argt, SDFI));
246 }
247
248 // If the function takes variable number of arguments, make a frame index for
249 // the start of the first arg value... for expansion of llvm.va_start.
250 // if (F.isVarArg())
251 // VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
252
253 //Set up a token factor with all the stack traffic
254 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
255 //return the arguments
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000256 return ArgValues;
257}
258
259std::pair<SDOperand, SDOperand>
260AlphaTargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman8e21e712005-03-26 01:29:23 +0000261 const Type *RetTy, bool isVarArg,
262 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000263 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000264 if (Args.size() > 6)
265 NumBytes = (Args.size() - 6) * 8;
266
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000267 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
268 DAG.getConstant(NumBytes, getPointerTy()));
269 std::vector<SDOperand> args_to_use;
270 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000271 {
272 switch (getValueType(Args[i].second)) {
273 default: assert(0 && "Unexpected ValueType for argument!");
274 case MVT::i1:
275 case MVT::i8:
276 case MVT::i16:
277 case MVT::i32:
278 // Promote the integer to 64 bits. If the input type is signed use a
279 // sign extend, otherwise use a zero extend.
280 if (Args[i].second->isSigned())
281 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
282 else
283 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
284 break;
285 case MVT::i64:
286 case MVT::f64:
287 case MVT::f32:
288 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000289 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000290 args_to_use.push_back(Args[i].first);
291 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000292
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000293 std::vector<MVT::ValueType> RetVals;
294 MVT::ValueType RetTyVT = getValueType(RetTy);
295 if (RetTyVT != MVT::isVoid)
296 RetVals.push_back(RetTyVT);
297 RetVals.push_back(MVT::Other);
298
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000299 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
300 Chain, Callee, args_to_use), 0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000301 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
302 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
303 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000304 return std::make_pair(TheCall, Chain);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000305}
306
307std::pair<SDOperand, SDOperand>
308AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
309 //vastart just returns the address of the VarArgsFrameIndex slot.
310 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
311}
312
313std::pair<SDOperand,SDOperand> AlphaTargetLowering::
314LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000315 const Type *ArgTy, SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000316 abort();
317}
318
319
320std::pair<SDOperand, SDOperand> AlphaTargetLowering::
321LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
322 SelectionDAG &DAG) {
323 abort();
324}
325
326
327
328
329
330namespace {
331
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000332//===--------------------------------------------------------------------===//
333/// ISel - Alpha specific code to select Alpha machine instructions for
334/// SelectionDAG operations.
335//===--------------------------------------------------------------------===//
336class ISel : public SelectionDAGISel {
337
338 /// AlphaLowering - This object fully describes how to lower LLVM code to an
339 /// Alpha-specific SelectionDAG.
340 AlphaTargetLowering AlphaLowering;
341
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000342 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
343 // for sdiv and udiv until it is put into the future
344 // dag combiner.
345
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000346 /// ExprMap - As shared expressions are codegen'd, we keep track of which
347 /// vreg the value is produced in, so we only emit one copy of each compiled
348 /// tree.
349 static const unsigned notIn = (unsigned)(-1);
350 std::map<SDOperand, unsigned> ExprMap;
351
352 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
353 std::map<SDOperand, unsigned> CCInvMap;
354
355public:
356 ISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM)
357 {}
358
359 /// InstructionSelectBasicBlock - This callback is invoked by
360 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
361 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000362 DEBUG(BB->dump());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000363 // Codegen the basic block.
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000364 ISelDAG = &DAG;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000365 Select(DAG.getRoot());
366
367 // Clear state used for selection.
368 ExprMap.clear();
369 CCInvMap.clear();
370 }
371
372 unsigned SelectExpr(SDOperand N);
373 unsigned SelectExprFP(SDOperand N, unsigned Result);
374 void Select(SDOperand N);
375
376 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
377 void SelectBranchCC(SDOperand N);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000378 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
379 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000380 //returns whether the sense of the comparison was inverted
381 bool SelectFPSetCC(SDOperand N, unsigned dst);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000382
383 // dag -> dag expanders for integer divide by constant
384 SDOperand BuildSDIVSequence(SDOperand N);
385 SDOperand BuildUDIVSequence(SDOperand N);
386
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000387};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000388}
389
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000390//Shamelessly adapted from PPC32
391// Structure used to return the necessary information to codegen an SDIV as
392// a multiply.
393struct ms {
394 int64_t m; // magic number
395 int64_t s; // shift amount
396};
397
398struct mu {
399 uint64_t m; // magic number
400 int64_t a; // add indicator
401 int64_t s; // shift amount
402};
403
404/// magic - calculate the magic numbers required to codegen an integer sdiv as
405/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
406/// or -1.
407static struct ms magic(int64_t d) {
408 int64_t p;
409 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
410 const uint64_t two63 = 9223372036854775808ULL; // 2^63
411 struct ms mag;
412
413 ad = abs(d);
414 t = two63 + ((uint64_t)d >> 63);
415 anc = t - 1 - t%ad; // absolute value of nc
416 p = 31; // initialize p
417 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
418 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
419 q2 = two63/ad; // initialize q2 = 2p/abs(d)
420 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
421 do {
422 p = p + 1;
423 q1 = 2*q1; // update q1 = 2p/abs(nc)
424 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
425 if (r1 >= anc) { // must be unsigned comparison
426 q1 = q1 + 1;
427 r1 = r1 - anc;
428 }
429 q2 = 2*q2; // update q2 = 2p/abs(d)
430 r2 = 2*r2; // update r2 = rem(2p/abs(d))
431 if (r2 >= ad) { // must be unsigned comparison
432 q2 = q2 + 1;
433 r2 = r2 - ad;
434 }
435 delta = ad - r2;
436 } while (q1 < delta || (q1 == delta && r1 == 0));
437
438 mag.m = q2 + 1;
439 if (d < 0) mag.m = -mag.m; // resulting magic number
440 mag.s = p - 64; // resulting shift
441 return mag;
442}
443
444/// magicu - calculate the magic numbers required to codegen an integer udiv as
445/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
446static struct mu magicu(uint64_t d)
447{
448 int64_t p;
449 uint64_t nc, delta, q1, r1, q2, r2;
450 struct mu magu;
451 magu.a = 0; // initialize "add" indicator
452 nc = - 1 - (-d)%d;
453 p = 31; // initialize p
454 q1 = 0x8000000000000000/nc; // initialize q1 = 2p/nc
455 r1 = 0x8000000000000000 - q1*nc; // initialize r1 = rem(2p,nc)
456 q2 = 0x7FFFFFFFFFFFFFFF/d; // initialize q2 = (2p-1)/d
457 r2 = 0x7FFFFFFFFFFFFFFF - q2*d; // initialize r2 = rem((2p-1),d)
458 do {
459 p = p + 1;
460 if (r1 >= nc - r1 ) {
461 q1 = 2*q1 + 1; // update q1
462 r1 = 2*r1 - nc; // update r1
463 }
464 else {
465 q1 = 2*q1; // update q1
466 r1 = 2*r1; // update r1
467 }
468 if (r2 + 1 >= d - r2) {
469 if (q2 >= 0x7FFFFFFFFFFFFFFF) magu.a = 1;
470 q2 = 2*q2 + 1; // update q2
471 r2 = 2*r2 + 1 - d; // update r2
472 }
473 else {
474 if (q2 >= 0x8000000000000000) magu.a = 1;
475 q2 = 2*q2; // update q2
476 r2 = 2*r2 + 1; // update r2
477 }
478 delta = d - 1 - r2;
479 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
480 magu.m = q2 + 1; // resulting magic number
481 magu.s = p - 32; // resulting shift
482 return magu;
483}
484
485/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
486/// return a DAG expression to select that will generate the same value by
487/// multiplying by a magic number. See:
488/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
489SDOperand ISel::BuildSDIVSequence(SDOperand N) {
490 int d = (int)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
491 ms magics = magic(d);
492 // Multiply the numerator (operand 0) by the magic value
493 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
494 ISelDAG->getConstant(magics.m, MVT::i64));
495 // If d > 0 and m < 0, add the numerator
496 if (d > 0 && magics.m < 0)
497 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
498 // If d < 0 and m > 0, subtract the numerator.
499 if (d < 0 && magics.m > 0)
500 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
501 // Shift right algebraic if shift value is nonzero
502 if (magics.s > 0)
503 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
504 ISelDAG->getConstant(magics.s, MVT::i64));
505 // Extract the sign bit and add it to the quotient
506 SDOperand T =
507 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
508 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
509}
510
511/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
512/// return a DAG expression to select that will generate the same value by
513/// multiplying by a magic number. See:
514/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
515SDOperand ISel::BuildUDIVSequence(SDOperand N) {
516 unsigned d =
517 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
518 mu magics = magicu(d);
519 // Multiply the numerator (operand 0) by the magic value
520 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
521 ISelDAG->getConstant(magics.m, MVT::i64));
522 if (magics.a == 0) {
523 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
524 ISelDAG->getConstant(magics.s, MVT::i64));
525 } else {
526 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
527 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
528 ISelDAG->getConstant(1, MVT::i64));
529 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
530 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
531 ISelDAG->getConstant(magics.s-1, MVT::i64));
532 }
533 return Q;
534}
535
536
537
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000538//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000539static const int IMM_LOW = -32768;
540static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000541static const int IMM_MULT = 65536;
542
543static long getUpper16(long l)
544{
545 long y = l / IMM_MULT;
546 if (l % IMM_MULT > IMM_HIGH)
547 ++y;
548 return y;
549}
550
551static long getLower16(long l)
552{
553 long h = getUpper16(l);
554 return l - h * IMM_MULT;
555}
556
Andrew Lenharth65838902005-02-06 16:22:15 +0000557static unsigned GetSymVersion(unsigned opcode)
558{
559 switch (opcode) {
560 default: assert(0 && "unknown load or store"); return 0;
561 case Alpha::LDQ: return Alpha::LDQ_SYM;
562 case Alpha::LDS: return Alpha::LDS_SYM;
563 case Alpha::LDT: return Alpha::LDT_SYM;
564 case Alpha::LDL: return Alpha::LDL_SYM;
565 case Alpha::LDBU: return Alpha::LDBU_SYM;
566 case Alpha::LDWU: return Alpha::LDWU_SYM;
567 case Alpha::LDW: return Alpha::LDW_SYM;
568 case Alpha::LDB: return Alpha::LDB_SYM;
569 case Alpha::STQ: return Alpha::STQ_SYM;
570 case Alpha::STS: return Alpha::STS_SYM;
571 case Alpha::STT: return Alpha::STT_SYM;
572 case Alpha::STL: return Alpha::STL_SYM;
573 case Alpha::STW: return Alpha::STW_SYM;
574 case Alpha::STB: return Alpha::STB_SYM;
575 }
576}
577
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000578void ISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
579{
580 unsigned Opc;
581 if (EnableAlphaFTOI) {
582 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
583 BuildMI(BB, Opc, 1, dst).addReg(src);
584 } else {
585 //The hard way:
586 // Spill the integer to memory and reload it from there.
587 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
588 MachineFunction *F = BB->getParent();
589 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
590
591 Opc = isDouble ? Alpha::STT : Alpha::STS;
592 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
593 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
594 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
595 }
596}
597
598void ISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
599{
600 unsigned Opc;
601 if (EnableAlphaFTOI) {
602 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
603 BuildMI(BB, Opc, 1, dst).addReg(src);
604 } else {
605 //The hard way:
606 // Spill the integer to memory and reload it from there.
607 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
608 MachineFunction *F = BB->getParent();
609 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
610
611 Opc = isDouble ? Alpha::STQ : Alpha::STL;
612 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
613 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
614 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
615 }
616}
617
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000618bool ISel::SelectFPSetCC(SDOperand N, unsigned dst)
619{
620 SDNode *Node = N.Val;
621 unsigned Opc, Tmp1, Tmp2, Tmp3;
622 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node);
623
624 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
625 bool rev = false;
626 bool inv = false;
627
628 switch (SetCC->getCondition()) {
629 default: Node->dump(); assert(0 && "Unknown FP comparison!");
630 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
631 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
632 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
633 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
634 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
635 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
636 }
637
638 //FIXME: check for constant 0.0
639 ConstantFPSDNode *CN;
640 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
641 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
642 Tmp1 = Alpha::F31;
643 else
644 Tmp1 = SelectExpr(N.getOperand(0));
645
646 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
647 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
648 Tmp2 = Alpha::F31;
649 else
650 Tmp2 = SelectExpr(N.getOperand(1));
651
652 //Can only compare doubles, and dag won't promote for me
653 if (SetCC->getOperand(0).getValueType() == MVT::f32)
654 {
655 //assert(0 && "Setcc On float?\n");
656 std::cerr << "Setcc on float!\n";
657 Tmp3 = MakeReg(MVT::f64);
658 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
659 Tmp1 = Tmp3;
660 }
661 if (SetCC->getOperand(1).getValueType() == MVT::f32)
662 {
663 //assert (0 && "Setcc On float?\n");
664 std::cerr << "Setcc on float!\n";
665 Tmp3 = MakeReg(MVT::f64);
666 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
667 Tmp2 = Tmp3;
668 }
669
670 if (rev) std::swap(Tmp1, Tmp2);
671 //do the comparison
672 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
673 return inv;
674}
675
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000676//Check to see if the load is a constant offset from a base register
677void ISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
678{
679 unsigned opcode = N.getOpcode();
680 if (opcode == ISD::ADD) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000681 if(N.getOperand(1).getOpcode() == ISD::Constant &&
682 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
683 { //Normal imm add
684 Reg = SelectExpr(N.getOperand(0));
685 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
686 return;
687 }
688 else if(N.getOperand(0).getOpcode() == ISD::Constant &&
689 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 32767)
690 {
691 Reg = SelectExpr(N.getOperand(1));
692 offset = cast<ConstantSDNode>(N.getOperand(0))->getValue();
693 return;
694 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000695 }
696 Reg = SelectExpr(N);
697 offset = 0;
698 return;
699}
700
Andrew Lenharth445171a2005-02-08 00:40:03 +0000701void ISel::SelectBranchCC(SDOperand N)
702{
703 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000704 MachineBasicBlock *Dest =
705 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
706 unsigned Opc = Alpha::WTF;
707
Andrew Lenharth445171a2005-02-08 00:40:03 +0000708 Select(N.getOperand(0)); //chain
709 SDOperand CC = N.getOperand(1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000710
Andrew Lenharth445171a2005-02-08 00:40:03 +0000711 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000712 {
713 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
714 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
715 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000716 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
717 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
718 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
719 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000720 bool isNE = false;
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000721
722 //Fix up CC
723 ISD::CondCode cCode= SetCC->getCondition();
724 if (LeftZero && !RightZero) //Swap Operands
725 cCode = ISD::getSetCCSwappedOperands(cCode);
726
727 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000728 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000729
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000730 if (LeftZero || RightZero) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000731 switch (SetCC->getCondition()) {
732 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
733 case ISD::SETEQ: Opc = Alpha::BEQ; break;
734 case ISD::SETLT: Opc = Alpha::BLT; break;
735 case ISD::SETLE: Opc = Alpha::BLE; break;
736 case ISD::SETGT: Opc = Alpha::BGT; break;
737 case ISD::SETGE: Opc = Alpha::BGE; break;
738 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
739 case ISD::SETUGT: Opc = Alpha::BNE; break;
740 case ISD::SETULE: Opc = Alpha::BEQ; break; //Technically you could have this CC
741 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
742 case ISD::SETNE: Opc = Alpha::BNE; break;
743 }
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000744 unsigned Tmp1;
745 if(LeftZero && !RightZero) //swap Operands
746 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
747 else
748 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000749 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
750 return;
751 } else {
752 unsigned Tmp1 = SelectExpr(CC);
753 if (isNE)
754 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
755 else
756 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000757 return;
758 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000759 } else { //FP
760 //Any comparison between 2 values should be codegened as an folded branch, as moving
761 //CC to the integer register is very expensive
762 //for a cmp b: c = a - b;
763 //a = b: c = 0
764 //a < b: c < 0
765 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000766
767 bool invTest = false;
768 unsigned Tmp3;
769
770 ConstantFPSDNode *CN;
771 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
772 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
773 Tmp3 = SelectExpr(SetCC->getOperand(0));
774 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
775 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
776 {
777 Tmp3 = SelectExpr(SetCC->getOperand(1));
778 invTest = true;
779 }
780 else
781 {
782 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
783 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
784 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
785 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
786 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
787 .addReg(Tmp1).addReg(Tmp2);
788 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000789
790 switch (SetCC->getCondition()) {
791 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000792 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
793 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
794 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
795 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
796 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
797 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000798 }
799 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000800 return;
801 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000802 abort(); //Should never be reached
803 } else {
804 //Giveup and do the stupid thing
805 unsigned Tmp1 = SelectExpr(CC);
806 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
807 return;
808 }
Andrew Lenharth445171a2005-02-08 00:40:03 +0000809 abort(); //Should never be reached
810}
811
Andrew Lenharth40831c52005-01-28 06:57:18 +0000812unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
813{
814 unsigned Tmp1, Tmp2, Tmp3;
815 unsigned Opc = 0;
816 SDNode *Node = N.Val;
817 MVT::ValueType DestType = N.getValueType();
818 unsigned opcode = N.getOpcode();
819
820 switch (opcode) {
821 default:
822 Node->dump();
823 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +0000824
Andrew Lenharth7332f3e2005-04-02 19:11:07 +0000825 case ISD::UNDEF: {
826 BuildMI(BB, Alpha::IDEF, 0, Result);
827 return Result;
828 }
829
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000830 case ISD::FNEG:
831 if(ISD::FABS == N.getOperand(0).getOpcode())
832 {
833 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth7332f3e2005-04-02 19:11:07 +0000834 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000835 } else {
836 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth7332f3e2005-04-02 19:11:07 +0000837 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000838 }
839 return Result;
840
841 case ISD::FABS:
842 Tmp1 = SelectExpr(N.getOperand(0));
843 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
844 return Result;
845
Andrew Lenharth9818c052005-02-05 13:19:12 +0000846 case ISD::SELECT:
847 {
Andrew Lenharth45859692005-03-03 21:47:53 +0000848 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
849 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
850 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
851
852 SDOperand CC = N.getOperand(0);
853 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
854
855 if (CC.getOpcode() == ISD::SETCC &&
856 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
857 { //FP Setcc -> Select yay!
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000858
859
Andrew Lenharth45859692005-03-03 21:47:53 +0000860 //for a cmp b: c = a - b;
861 //a = b: c = 0
862 //a < b: c < 0
863 //a > b: c > 0
864
865 bool invTest = false;
866 unsigned Tmp3;
867
868 ConstantFPSDNode *CN;
869 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
870 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
871 Tmp3 = SelectExpr(SetCC->getOperand(0));
872 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
873 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
874 {
875 Tmp3 = SelectExpr(SetCC->getOperand(1));
876 invTest = true;
877 }
878 else
879 {
880 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
881 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
882 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
883 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
884 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
885 .addReg(Tmp1).addReg(Tmp2);
886 }
887
888 switch (SetCC->getCondition()) {
889 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
890 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
891 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
892 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
893 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
894 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
895 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
896 }
Andrew Lenharth33819132005-03-04 20:09:23 +0000897 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
Andrew Lenharth45859692005-03-03 21:47:53 +0000898 return Result;
899 }
900 else
901 {
902 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000903 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV).addReg(Tmp1);
904// // Spill the cond to memory and reload it from there.
905// unsigned Tmp4 = MakeReg(MVT::f64);
906// MoveIntFP(Tmp1, Tmp4, true);
907// //now ideally, we don't have to do anything to the flag...
908// // Get the condition into the zero flag.
909// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
Andrew Lenharth45859692005-03-03 21:47:53 +0000910 return Result;
911 }
Andrew Lenharth9818c052005-02-05 13:19:12 +0000912 }
913
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000914 case ISD::FP_ROUND:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000915 assert (DestType == MVT::f32 &&
916 N.getOperand(0).getValueType() == MVT::f64 &&
917 "only f64 to f32 conversion supported here");
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000918 Tmp1 = SelectExpr(N.getOperand(0));
919 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
920 return Result;
921
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000922 case ISD::FP_EXTEND:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000923 assert (DestType == MVT::f64 &&
924 N.getOperand(0).getValueType() == MVT::f32 &&
925 "only f32 to f64 conversion supported here");
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000926 Tmp1 = SelectExpr(N.getOperand(0));
927 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
928 return Result;
929
Andrew Lenharth2c594352005-01-29 15:42:07 +0000930 case ISD::CopyFromReg:
931 {
932 // Make sure we generate both values.
933 if (Result != notIn)
934 ExprMap[N.getValue(1)] = notIn; // Generate the token
935 else
936 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
937
938 SDOperand Chain = N.getOperand(0);
939
940 Select(Chain);
941 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
942 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
943 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
944 return Result;
945 }
946
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000947 case ISD::LOAD:
948 {
949 // Make sure we generate both values.
950 if (Result != notIn)
951 ExprMap[N.getValue(1)] = notIn; // Generate the token
952 else
953 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000954
Andrew Lenharth29219162005-02-07 06:31:44 +0000955 DestType = N.getValue(0).getValueType();
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000956
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000957 SDOperand Chain = N.getOperand(0);
958 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000959 Select(Chain);
Andrew Lenharth65838902005-02-06 16:22:15 +0000960 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
961
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000962 if (Address.getOpcode() == ISD::GlobalAddress) {
963 AlphaLowering.restoreGP(BB);
964 Opc = GetSymVersion(Opc);
965 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
966 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000967 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000968 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +0000969 Opc = GetSymVersion(Opc);
Andrew Lenharth97127a12005-02-05 17:41:39 +0000970 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharthe76797c2005-02-01 20:40:27 +0000971 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000972 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000973 BuildMI(BB, Opc, 2, Result)
974 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
975 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000976 } else {
977 long offset;
978 SelectAddr(Address, Tmp1, offset);
979 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
980 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000981 return Result;
982 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000983 case ISD::ConstantFP:
984 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
985 if (CN->isExactlyValue(+0.0)) {
986 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +0000987 } else if ( CN->isExactlyValue(-0.0)) {
988 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +0000989 } else {
990 abort();
991 }
992 }
993 return Result;
994
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +0000995 case ISD::SDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000996 case ISD::MUL:
997 case ISD::ADD:
998 case ISD::SUB:
Andrew Lenharth40831c52005-01-28 06:57:18 +0000999 switch( opcode ) {
1000 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
1001 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
1002 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
1003 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
1004 };
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001005
1006 ConstantFPSDNode *CN;
1007 if (opcode == ISD::SUB
1008 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1009 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1010 {
1011 Tmp2 = SelectExpr(N.getOperand(1));
1012 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1013 } else {
1014 Tmp1 = SelectExpr(N.getOperand(0));
1015 Tmp2 = SelectExpr(N.getOperand(1));
1016 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1017 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001018 return Result;
1019
Andrew Lenharth2c594352005-01-29 15:42:07 +00001020 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001021 {
1022 //include a conversion sequence for float loads to double
1023 if (Result != notIn)
1024 ExprMap[N.getValue(1)] = notIn; // Generate the token
1025 else
1026 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1027
Andrew Lenhartha549deb2005-02-07 05:33:15 +00001028 Tmp1 = MakeReg(MVT::f32);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001029
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001030 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
1031 "EXTLOAD not from f32");
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001032 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
1033
1034 SDOperand Chain = N.getOperand(0);
1035 SDOperand Address = N.getOperand(1);
1036 Select(Chain);
1037
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001038 if (Address.getOpcode() == ISD::GlobalAddress) {
1039 AlphaLowering.restoreGP(BB);
1040 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1041 }
1042 else if (ConstantPoolSDNode *CP =
1043 dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
1044 {
1045 AlphaLowering.restoreGP(BB);
1046 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addConstantPoolIndex(CP->getIndex());
1047 }
1048 else if(Address.getOpcode() == ISD::FrameIndex) {
1049 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
Andrew Lenharth032f2352005-02-22 21:59:48 +00001050 BuildMI(BB, Alpha::LDS, 2, Tmp1)
1051 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1052 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001053 } else {
1054 long offset;
1055 SelectAddr(Address, Tmp2, offset);
1056 BuildMI(BB, Alpha::LDS, 1, Tmp1).addImm(offset).addReg(Tmp2);
1057 }
Andrew Lenharth29219162005-02-07 06:31:44 +00001058 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001059 return Result;
1060 }
Andrew Lenharth2c594352005-01-29 15:42:07 +00001061
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001062 case ISD::UINT_TO_FP:
1063 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001064 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001065 assert (N.getOperand(0).getValueType() == MVT::i64
1066 && "only quads can be loaded from");
Andrew Lenharth40831c52005-01-28 06:57:18 +00001067 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001068 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001069 MoveInt2FP(Tmp1, Tmp2, true);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001070 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1071 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001072 return Result;
1073 }
1074 }
1075 assert(0 && "should not get here");
1076 return 0;
1077}
1078
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001079unsigned ISel::SelectExpr(SDOperand N) {
1080 unsigned Result;
1081 unsigned Tmp1, Tmp2, Tmp3;
1082 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001083 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001084
1085 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001086 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001087
1088 unsigned &Reg = ExprMap[N];
1089 if (Reg) return Reg;
1090
1091 if (N.getOpcode() != ISD::CALL)
1092 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001093 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001094 else {
1095 // If this is a call instruction, make sure to prepare ALL of the result
1096 // values as well as the chain.
1097 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001098 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001099 else {
1100 Result = MakeReg(Node->getValueType(0));
1101 ExprMap[N.getValue(0)] = Result;
1102 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1103 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001104 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001105 }
1106 }
1107
Andrew Lenharth22088bb2005-02-02 15:05:33 +00001108 if (DestType == MVT::f64 || DestType == MVT::f32 ||
Andrew Lenharth06342c32005-02-07 06:21:37 +00001109 (
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001110 (opcode == ISD::LOAD || opcode == ISD::CopyFromReg ||
1111 opcode == ISD::EXTLOAD) &&
1112 (N.getValue(0).getValueType() == MVT::f32 ||
1113 N.getValue(0).getValueType() == MVT::f64)
Andrew Lenharth06342c32005-02-07 06:21:37 +00001114 )
1115 )
Andrew Lenharth40831c52005-01-28 06:57:18 +00001116 return SelectExprFP(N, Result);
1117
1118 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001119 default:
1120 Node->dump();
1121 assert(0 && "Node not handled!\n");
1122
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001123 case ISD::MULHU:
1124 Tmp1 = SelectExpr(N.getOperand(0));
1125 Tmp2 = SelectExpr(N.getOperand(1));
1126 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
1127 case ISD::MULHS:
1128 {
1129 //MULHU - Ra<63>*Rb - Rb<63>*Ra
1130 Tmp1 = SelectExpr(N.getOperand(0));
1131 Tmp2 = SelectExpr(N.getOperand(1));
1132 Tmp3 = MakeReg(MVT::i64);
1133 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1134 unsigned V1 = MakeReg(MVT::i64);
1135 unsigned V2 = MakeReg(MVT::i64);
1136 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31).addReg(Tmp1);
1137 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31).addReg(Tmp2);
1138 unsigned IRes = MakeReg(MVT::i64);
1139 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
1140 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
1141 return Result;
1142 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001143 case ISD::UNDEF: {
1144 BuildMI(BB, Alpha::IDEF, 0, Result);
1145 return Result;
1146 }
1147
Andrew Lenharth032f2352005-02-22 21:59:48 +00001148 case ISD::DYNAMIC_STACKALLOC:
1149 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +00001150 if (Result != notIn)
1151 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +00001152 else
1153 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1154
1155 // FIXME: We are currently ignoring the requested alignment for handling
1156 // greater than the stack alignment. This will need to be revisited at some
1157 // point. Align = N.getOperand(2);
1158
1159 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1160 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1161 std::cerr << "Cannot allocate stack object with greater alignment than"
1162 << " the stack alignment yet!";
1163 abort();
1164 }
1165
1166 Select(N.getOperand(0));
1167 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1168 {
1169 if (CN->getValue() < 32000)
1170 {
1171 BuildMI(BB, Alpha::LDA, 2, Alpha::R30)
1172 .addImm(-CN->getValue()).addReg(Alpha::R30);
1173 } else {
1174 Tmp1 = SelectExpr(N.getOperand(1));
1175 // Subtract size from stack pointer, thereby allocating some space.
1176 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1177 }
1178 } else {
1179 Tmp1 = SelectExpr(N.getOperand(1));
1180 // Subtract size from stack pointer, thereby allocating some space.
1181 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1182 }
1183
1184 // Put a pointer to the space into the result register, by copying the stack
1185 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +00001186 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +00001187 return Result;
1188
Andrew Lenharth33819132005-03-04 20:09:23 +00001189// case ISD::ConstantPool:
1190// Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1191// AlphaLowering.restoreGP(BB);
1192// BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
1193// return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +00001194
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001195 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001196 BuildMI(BB, Alpha::LDA, 2, Result)
1197 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1198 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001199 return Result;
1200
1201 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001202 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001203 case ISD::SEXTLOAD:
Andrew Lenhartha549deb2005-02-07 05:33:15 +00001204 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001205 {
1206 // Make sure we generate both values.
1207 if (Result != notIn)
1208 ExprMap[N.getValue(1)] = notIn; // Generate the token
1209 else
1210 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001211
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001212 SDOperand Chain = N.getOperand(0);
1213 SDOperand Address = N.getOperand(1);
1214 Select(Chain);
1215
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001216 assert(Node->getValueType(0) == MVT::i64 &&
1217 "Unknown type to sign extend to.");
Andrew Lenharth03824012005-02-07 05:55:55 +00001218 if (opcode == ISD::LOAD)
1219 Opc = Alpha::LDQ;
1220 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001221 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
1222 default: Node->dump(); assert(0 && "Bad sign extend!");
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001223 case MVT::i32: Opc = Alpha::LDL;
1224 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
1225 case MVT::i16: Opc = Alpha::LDWU;
1226 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001227 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001228 case MVT::i8: Opc = Alpha::LDBU;
1229 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001230 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001231
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001232 if (Address.getOpcode() == ISD::GlobalAddress) {
1233 AlphaLowering.restoreGP(BB);
1234 Opc = GetSymVersion(Opc);
1235 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1236 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001237 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1238 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001239 Opc = GetSymVersion(Opc);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001240 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001241 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001242 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001243 BuildMI(BB, Opc, 2, Result)
1244 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1245 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001246 } else {
1247 long offset;
1248 SelectAddr(Address, Tmp1, offset);
1249 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1250 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001251 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001252 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001253
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001254 case ISD::GlobalAddress:
1255 AlphaLowering.restoreGP(BB);
1256 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
1257 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
1258 return Result;
1259
1260 case ISD::CALL:
1261 {
1262 Select(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001263
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001264 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001265 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001266
1267 //grab the arguments
1268 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001269 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001270 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001271 argvregs.push_back(SelectExpr(N.getOperand(i)));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001272
Andrew Lenharth684f2292005-01-30 00:35:27 +00001273 //in reg args
1274 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001275 {
1276 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
1277 Alpha::R19, Alpha::R20, Alpha::R21};
1278 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
1279 Alpha::F19, Alpha::F20, Alpha::F21};
1280 switch(N.getOperand(i+2).getValueType()) {
1281 default:
1282 Node->dump();
1283 N.getOperand(i).Val->dump();
1284 std::cerr << "Type for " << i << " is: " <<
1285 N.getOperand(i+2).getValueType() << "\n";
1286 assert(0 && "Unknown value type for call");
1287 case MVT::i1:
1288 case MVT::i8:
1289 case MVT::i16:
1290 case MVT::i32:
1291 case MVT::i64:
1292 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1293 break;
1294 case MVT::f32:
1295 case MVT::f64:
1296 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1297 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001298 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001299 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001300 //in mem args
1301 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001302 {
1303 switch(N.getOperand(i+2).getValueType()) {
1304 default:
1305 Node->dump();
1306 N.getOperand(i).Val->dump();
1307 std::cerr << "Type for " << i << " is: " <<
1308 N.getOperand(i+2).getValueType() << "\n";
1309 assert(0 && "Unknown value type for call");
1310 case MVT::i1:
1311 case MVT::i8:
1312 case MVT::i16:
1313 case MVT::i32:
1314 case MVT::i64:
1315 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1316 break;
1317 case MVT::f32:
1318 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1319 break;
1320 case MVT::f64:
1321 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1322 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001323 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001324 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001325 //build the right kind of call
1326 if (GlobalAddressSDNode *GASD =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001327 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001328 {
Andrew Lenharth3e315922005-02-10 20:10:38 +00001329 //if (GASD->getGlobal()->isExternal()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001330 //use safe calling convention
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001331 AlphaLowering.restoreGP(BB);
1332 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal(),true);
Andrew Lenharth3e315922005-02-10 20:10:38 +00001333 //} else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001334 //use PC relative branch call
Andrew Lenharth3e315922005-02-10 20:10:38 +00001335 //BuildMI(BB, Alpha::BSR, 1, Alpha::R26).addGlobalAddress(GASD->getGlobal(),true);
1336 //}
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001337 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001338 else if (ExternalSymbolSDNode *ESSDN =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001339 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001340 {
1341 AlphaLowering.restoreGP(BB);
Andrew Lenharthba05ad62005-03-30 18:22:52 +00001342 BuildMI(BB, Alpha::CALL, 1).addExternalSymbol(ESSDN->getSymbol(), true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001343 } else {
1344 //no need to restore GP as we are doing an indirect call
1345 Tmp1 = SelectExpr(N.getOperand(1));
1346 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1347 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1348 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001349
1350 //push the result into a virtual register
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001351
1352 switch (Node->getValueType(0)) {
1353 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001354 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001355 case MVT::i1:
1356 case MVT::i8:
1357 case MVT::i16:
1358 case MVT::i32:
1359 case MVT::i64:
1360 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1361 break;
1362 case MVT::f32:
1363 case MVT::f64:
1364 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1365 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001366 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001367 return Result+N.ResNo;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001368 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001369
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001370 case ISD::SIGN_EXTEND_INREG:
1371 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001372 //do SDIV opt for all levels of ints
Andrew Lenharth5e99dd92005-03-31 22:02:25 +00001373 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001374 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001375 unsigned Tmp4 = MakeReg(MVT::f64);
1376 unsigned Tmp5 = MakeReg(MVT::f64);
1377 unsigned Tmp6 = MakeReg(MVT::f64);
1378 unsigned Tmp7 = MakeReg(MVT::f64);
1379 unsigned Tmp8 = MakeReg(MVT::f64);
1380 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001381
1382 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1383 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1384 MoveInt2FP(Tmp1, Tmp4, true);
1385 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001386 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Tmp4);
1387 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Tmp5);
1388 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
1389 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001390 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001391 return Result;
1392 }
1393
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001394 //Alpha has instructions for a bunch of signed 32 bit stuff
1395 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001396 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001397 switch (N.getOperand(0).getOpcode()) {
1398 case ISD::ADD:
1399 case ISD::SUB:
1400 case ISD::MUL:
1401 {
1402 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1403 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1404 //FIXME: first check for Scaled Adds and Subs!
1405 if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1406 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
1407 { //Normal imm add/sub
1408 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth093f3272005-02-12 21:11:17 +00001409 //if the value was really originally a i32, skip the up conversion
1410 if (N.getOperand(0).getOperand(0).getOpcode() == ISD::SIGN_EXTEND_INREG &&
1411 dyn_cast<MVTSDNode>(N.getOperand(0).getOperand(0).Val)
1412 ->getExtraValueType() == MVT::i32)
1413 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1414 else
1415 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001416 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1417 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001418 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001419 else
1420 { //Normal add/sub
1421 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULLi : Alpha::SUBL);
Andrew Lenharth093f3272005-02-12 21:11:17 +00001422 //if the value was really originally a i32, skip the up conversion
1423 if (N.getOperand(0).getOperand(0).getOpcode() == ISD::SIGN_EXTEND_INREG &&
1424 dyn_cast<MVTSDNode>(N.getOperand(0).getOperand(0).Val)
1425 ->getExtraValueType() == MVT::i32)
1426 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1427 else
1428 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1429 //if the value was really originally a i32, skip the up conversion
1430 if (N.getOperand(0).getOperand(1).getOpcode() == ISD::SIGN_EXTEND_INREG &&
1431 dyn_cast<MVTSDNode>(N.getOperand(0).getOperand(1).Val)
1432 ->getExtraValueType() == MVT::i32)
1433 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1434 else
1435 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1436
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001437 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1438 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1439 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1440 }
1441 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001442 }
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001443 case ISD::SEXTLOAD:
1444 //SelectionDag isn't deleting the signextend after sextloads
1445 Reg = Result = SelectExpr(N.getOperand(0));
1446 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001447 default: break; //Fall Though;
1448 }
1449 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001450 Tmp1 = SelectExpr(N.getOperand(0));
1451 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001452 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001453 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001454 {
1455 default:
1456 Node->dump();
1457 assert(0 && "Sign Extend InReg not there yet");
1458 break;
1459 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001460 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001461 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001462 break;
1463 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001464 case MVT::i16:
1465 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
1466 break;
1467 case MVT::i8:
1468 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
1469 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001470 case MVT::i1:
1471 Tmp2 = MakeReg(MVT::i64);
1472 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenharth7536eea2005-02-12 20:42:09 +00001473 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::F31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001474 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001475 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001476 return Result;
1477 }
1478 case ISD::ZERO_EXTEND_INREG:
1479 {
1480 Tmp1 = SelectExpr(N.getOperand(0));
1481 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001482 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001483 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001484 {
1485 default:
1486 Node->dump();
1487 assert(0 && "Zero Extend InReg not there yet");
1488 break;
1489 case MVT::i32: Tmp2 = 0xf0; break;
1490 case MVT::i16: Tmp2 = 0xfc; break;
1491 case MVT::i8: Tmp2 = 0xfe; break;
1492 case MVT::i1: //handle this one special
1493 BuildMI(BB, Alpha::ANDi, 2, Result).addReg(Tmp1).addImm(1);
1494 return Result;
1495 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001496 BuildMI(BB, Alpha::ZAPi, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001497 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001498 }
1499
1500 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001501 {
1502 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1503 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1504 bool isConst1 = false;
1505 bool isConst2 = false;
1506 int dir;
Andrew Lenharth9818c052005-02-05 13:19:12 +00001507
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001508 //Tmp1 = SelectExpr(N.getOperand(0));
1509 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001510 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1511 isConst1 = true;
1512 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001513 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1514 isConst2 = true;
1515
1516 switch (SetCC->getCondition()) {
1517 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1518 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001519 case ISD::SETLT:
1520 Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
1521 case ISD::SETLE:
1522 Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
1523 case ISD::SETGT:
1524 Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
1525 case ISD::SETGE:
1526 Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
1527 case ISD::SETULT:
1528 Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
1529 case ISD::SETUGT:
1530 Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
1531 case ISD::SETULE:
1532 Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
1533 case ISD::SETUGE:
1534 Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001535 case ISD::SETNE: {//Handle this one special
1536 //std::cerr << "Alpha does not have a setne.\n";
1537 //abort();
1538 Tmp1 = SelectExpr(N.getOperand(0));
1539 Tmp2 = SelectExpr(N.getOperand(1));
1540 Tmp3 = MakeReg(MVT::i64);
1541 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001542 //Remeber we have the Inv for this CC
1543 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001544 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001545 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001546 return Result;
1547 }
1548 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001549 if (dir == 1) {
1550 Tmp1 = SelectExpr(N.getOperand(0));
1551 if (isConst2) {
1552 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1553 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1554 } else {
1555 Tmp2 = SelectExpr(N.getOperand(1));
1556 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1557 }
1558 } else if (dir == 2) {
1559 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001560 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001561 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1562 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1563 } else {
1564 Tmp2 = SelectExpr(N.getOperand(0));
1565 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1566 }
1567 } else { //dir == 0
1568 if (isConst1) {
1569 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1570 Tmp2 = SelectExpr(N.getOperand(1));
1571 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
1572 } else if (isConst2) {
1573 Tmp1 = SelectExpr(N.getOperand(0));
1574 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1575 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1576 } else {
1577 Tmp1 = SelectExpr(N.getOperand(0));
1578 Tmp2 = SelectExpr(N.getOperand(1));
1579 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1580 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001581 }
1582 } else {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001583 //do the comparison
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001584 Tmp1 = MakeReg(MVT::f64);
1585 bool inv = SelectFPSetCC(N, Tmp1);
1586
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001587 //now arrange for Result (int) to have a 1 or 0
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001588 Tmp2 = MakeReg(MVT::i64);
1589 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001590 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001591 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001592 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001593 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001594 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001595 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001596
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001597 case ISD::CopyFromReg:
1598 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001599 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001600 if (Result != notIn)
1601 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001602 else
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001603 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth40831c52005-01-28 06:57:18 +00001604
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001605 SDOperand Chain = N.getOperand(0);
1606
1607 Select(Chain);
1608 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1609 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1610 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1611 return Result;
1612 }
1613
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001614 //Most of the plain arithmetic and logic share the same form, and the same
1615 //constant immediate test
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001616 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001617 //Match Not
1618 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1619 cast<ConstantSDNode>(N.getOperand(1))->isAllOnesValue())
1620 {
1621 Tmp1 = SelectExpr(N.getOperand(0));
1622 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1623 return Result;
1624 }
1625 //Fall through
1626 case ISD::AND:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001627 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001628 //Check operand(0) == Not
1629 if (N.getOperand(0).getOpcode() == ISD::OR &&
1630 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1631 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->isAllOnesValue())
1632 {
1633 switch(opcode) {
1634 case ISD::AND: Opc = Alpha::BIC; break;
1635 case ISD::OR: Opc = Alpha::ORNOT; break;
1636 case ISD::XOR: Opc = Alpha::EQV; break;
1637 }
1638 Tmp1 = SelectExpr(N.getOperand(1));
1639 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1640 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1641 return Result;
1642 }
1643 //Check operand(1) == Not
1644 if (N.getOperand(1).getOpcode() == ISD::OR &&
1645 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
1646 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->isAllOnesValue())
1647 {
1648 switch(opcode) {
1649 case ISD::AND: Opc = Alpha::BIC; break;
1650 case ISD::OR: Opc = Alpha::ORNOT; break;
1651 case ISD::XOR: Opc = Alpha::EQV; break;
1652 }
1653 Tmp1 = SelectExpr(N.getOperand(0));
1654 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1655 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1656 return Result;
1657 }
1658 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001659 case ISD::SHL:
1660 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001661 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001662 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001663 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1664 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001665 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001666 {
1667 switch(opcode) {
1668 case ISD::AND: Opc = Alpha::ANDi; break;
1669 case ISD::OR: Opc = Alpha::BISi; break;
1670 case ISD::XOR: Opc = Alpha::XORi; break;
1671 case ISD::SHL: Opc = Alpha::SLi; break;
1672 case ISD::SRL: Opc = Alpha::SRLi; break;
1673 case ISD::SRA: Opc = Alpha::SRAi; break;
1674 case ISD::MUL: Opc = Alpha::MULQi; break;
1675 };
1676 Tmp1 = SelectExpr(N.getOperand(0));
1677 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1678 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1679 } else {
1680 switch(opcode) {
1681 case ISD::AND: Opc = Alpha::AND; break;
1682 case ISD::OR: Opc = Alpha::BIS; break;
1683 case ISD::XOR: Opc = Alpha::XOR; break;
1684 case ISD::SHL: Opc = Alpha::SL; break;
1685 case ISD::SRL: Opc = Alpha::SRL; break;
1686 case ISD::SRA: Opc = Alpha::SRA; break;
1687 case ISD::MUL: Opc = Alpha::MULQ; break;
1688 };
1689 Tmp1 = SelectExpr(N.getOperand(0));
1690 Tmp2 = SelectExpr(N.getOperand(1));
1691 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1692 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001693 return Result;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001694
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001695 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001696 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001697 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001698 bool isAdd = opcode == ISD::ADD;
1699
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001700 //first check for Scaled Adds and Subs!
1701 //Valid for add and sub
1702 if(N.getOperand(0).getOpcode() == ISD::SHL &&
1703 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1704 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() == 2)
1705 {
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001706 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001707 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1708 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1709 BuildMI(BB, isAdd?Alpha::S4ADDQi:Alpha::S4SUBQi, 2, Result).addReg(Tmp2)
1710 .addImm(cast<ConstantSDNode>(N.getOperand(1))->getValue());
1711 else {
1712 Tmp1 = SelectExpr(N.getOperand(1));
1713 BuildMI(BB, isAdd?Alpha::S4ADDQ:Alpha::S4SUBQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
1714 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001715 }
1716 else if(N.getOperand(0).getOpcode() == ISD::SHL &&
1717 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1718 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() == 3)
1719 {
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001720 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001721 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1722 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1723 BuildMI(BB, isAdd?Alpha::S8ADDQi:Alpha::S8SUBQi, 2, Result).addReg(Tmp2)
1724 .addImm(cast<ConstantSDNode>(N.getOperand(1))->getValue());
1725 else {
1726 Tmp1 = SelectExpr(N.getOperand(1));
1727 BuildMI(BB, isAdd?Alpha::S8ADDQ:Alpha::S8SUBQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
1728 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001729 }
1730 //Position prevents subs
1731 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &
1732 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
1733 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue() == 2)
1734 {
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001735 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001736 if (N.getOperand(0).getOpcode() == ISD::Constant &&
1737 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1738 BuildMI(BB, Alpha::S4ADDQi, 2, Result).addReg(Tmp2)
1739 .addImm(cast<ConstantSDNode>(N.getOperand(0))->getValue());
1740 else {
1741 Tmp1 = SelectExpr(N.getOperand(0));
1742 BuildMI(BB, Alpha::S4ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
1743 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001744 }
1745 else if(N.getOperand(0).getOpcode() == ISD::SHL && isAdd &&
1746 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
1747 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue() == 3)
1748 {
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001749 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001750 if (N.getOperand(0).getOpcode() == ISD::Constant &&
1751 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1752 BuildMI(BB, Alpha::S8ADDQi, 2, Result).addReg(Tmp2)
1753 .addImm(cast<ConstantSDNode>(N.getOperand(0))->getValue());
1754 else {
1755 Tmp1 = SelectExpr(N.getOperand(0));
1756 BuildMI(BB, Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
1757 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001758 }
1759 //small addi
1760 else if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001761 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001762 { //Normal imm add/sub
1763 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1764 Tmp1 = SelectExpr(N.getOperand(0));
1765 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1766 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1767 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001768 //larger addi
Andrew Lenharth40831c52005-01-28 06:57:18 +00001769 else if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001770 (cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767 ||
1771 (long)cast<ConstantSDNode>(N.getOperand(1))->getValue() >= -32767))
1772 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001773 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001774 Tmp2 = (long)cast<ConstantSDNode>(N.getOperand(1))->getValue();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001775 if (!isAdd)
1776 Tmp2 = -Tmp2;
1777 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001778 }
1779 //give up and do the operation
1780 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001781 //Normal add/sub
1782 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1783 Tmp1 = SelectExpr(N.getOperand(0));
1784 Tmp2 = SelectExpr(N.getOperand(1));
1785 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1786 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001787 return Result;
1788 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001789
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001790 case ISD::SDIV:
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001791 case ISD::UDIV:
1792 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1793 ((int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended() >= 2 ||
1794 (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended() <= -2))
1795 {
1796 // If this is a divide by constant, we can emit code using some magic
1797 // constants to implement it as a multiply instead.
1798 ExprMap.erase(N);
1799 if (opcode == ISD::SDIV)
1800 return SelectExpr(BuildSDIVSequence(N));
1801 else
1802 return SelectExpr(BuildUDIVSequence(N));
1803 }
1804 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001805 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00001806 case ISD::SREM:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001807 //FIXME: alpha really doesn't support any of these operations,
1808 // the ops are expanded into special library calls with
1809 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001810 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00001811 switch(opcode) {
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001812 case ISD::UREM: Opc = Alpha::REMQU; break;
1813 case ISD::SREM: Opc = Alpha::REMQ; break;
1814 case ISD::UDIV: Opc = Alpha::DIVQU; break;
1815 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001816 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001817 Tmp1 = SelectExpr(N.getOperand(0));
1818 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth33819132005-03-04 20:09:23 +00001819 //set up regs explicitly (helps Reg alloc)
1820 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
1821 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001822 AlphaLowering.restoreGP(BB);
Andrew Lenharth33819132005-03-04 20:09:23 +00001823 BuildMI(BB, Opc, 2).addReg(Alpha::R24).addReg(Alpha::R25);
1824 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001825 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001826
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001827 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001828 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001829 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001830 assert (DestType == MVT::i64 && "only quads can be loaded to");
1831 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001832 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001833 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001834 if (SrcType == MVT::f32)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001835 {
1836 Tmp2 = MakeReg(MVT::f64);
1837 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1838 Tmp1 = Tmp2;
1839 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001840 Tmp2 = MakeReg(MVT::f64);
1841 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001842 MoveFP2Int(Tmp2, Result, true);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001843
1844 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001845 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001846
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001847 case ISD::SELECT:
1848 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001849 //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 +00001850 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001851 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1852 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001853 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001854 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001855
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001856 SDOperand CC = N.getOperand(0);
1857 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
1858
1859 if (CC.getOpcode() == ISD::SETCC &&
1860 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
1861 { //FP Setcc -> Int Select
1862 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001863 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1864 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001865 bool inv = SelectFPSetCC(CC, Tmp1);
1866 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
1867 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
1868 return Result;
1869 }
1870 if (CC.getOpcode() == ISD::SETCC) {
1871 //Int SetCC -> Select
1872 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001873 if((SetCC->getOperand(1).getOpcode() == ISD::Constant &&
1874 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0) ||
1875 (SetCC->getOperand(0).getOpcode() == ISD::Constant &&
1876 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0))
1877 {
1878 //figure out a few things
1879 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
1880 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
1881 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
1882 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
1883 bool LeftConst = N.getOperand(1).getOpcode() == ISD::Constant &&
1884 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255;
1885 bool RightConst = N.getOperand(2).getOpcode() == ISD::Constant &&
1886 cast<ConstantSDNode>(N.getOperand(2))->getValue() <= 255;
1887 bool useImm = LeftConst || RightConst;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001888
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001889 //Fix up CC
1890 ISD::CondCode cCode= SetCC->getCondition();
1891 if (RightConst && !LeftConst) //Invert sense to get Imm field right
1892 cCode = ISD::getSetCCInverse(cCode, true);
1893 if (LeftZero && !RightZero) //Swap Operands
1894 cCode = ISD::getSetCCSwappedOperands(cCode);
1895
1896 //Choose the CMOV
1897 switch (cCode) {
1898 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
1899 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
1900 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
1901 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
1902 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
1903 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
1904 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
1905 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
1906 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break; //Technically you could have this CC
1907 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
1908 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
1909 }
1910 if(LeftZero && !RightZero) //swap Operands
1911 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
1912 else
1913 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
1914
1915 if (LeftConst) {
1916 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1917 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
1918 .addImm(cast<ConstantSDNode>(N.getOperand(1))->getValue())
1919 .addReg(Tmp1);
1920 } else if (RightConst) {
1921 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
1922 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
1923 .addImm(cast<ConstantSDNode>(N.getOperand(2))->getValue())
1924 .addReg(Tmp1);
1925 } else {
1926 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1927 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1928 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
1929 }
1930 return Result;
1931 }
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001932 //Otherwise, fall though
1933 }
1934 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001935 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1936 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001937 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001938
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001939 return Result;
1940 }
1941
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001942 case ISD::Constant:
1943 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00001944 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00001945 if (val <= IMM_HIGH && val >= IMM_LOW) {
1946 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
1947 }
1948 else if (val <= (int64_t)IMM_HIGH + (int64_t)IMM_HIGH * (int64_t)IMM_MULT &&
1949 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
1950 Tmp1 = MakeReg(MVT::i64);
1951 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val)).addReg(Alpha::R31);
1952 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
1953 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001954 else {
1955 MachineConstantPool *CP = BB->getParent()->getConstantPool();
1956 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
1957 unsigned CPI = CP->getConstantPoolIndex(C);
1958 AlphaLowering.restoreGP(BB);
1959 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
1960 }
1961 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001962 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001963 }
1964
1965 return 0;
1966}
1967
1968void ISel::Select(SDOperand N) {
1969 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00001970 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001971
Nate Begeman85fdeb22005-03-24 04:39:54 +00001972 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001973 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001974
1975 SDNode *Node = N.Val;
Andrew Lenharth760270d2005-02-07 23:02:23 +00001976
Andrew Lenharth760270d2005-02-07 23:02:23 +00001977 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001978
1979 default:
1980 Node->dump(); std::cerr << "\n";
1981 assert(0 && "Node not handled yet!");
1982
1983 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00001984 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001985 return;
1986 }
1987
1988 case ISD::BR: {
1989 MachineBasicBlock *Dest =
1990 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1991
1992 Select(N.getOperand(0));
1993 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
1994 return;
1995 }
1996
1997 case ISD::ImplicitDef:
1998 Select(N.getOperand(0));
1999 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2000 return;
2001
2002 case ISD::EntryToken: return; // Noop
2003
2004 case ISD::TokenFactor:
2005 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2006 Select(Node->getOperand(i));
2007
2008 //N.Val->dump(); std::cerr << "\n";
2009 //assert(0 && "Node not handled yet!");
2010
2011 return;
2012
2013 case ISD::CopyToReg:
2014 Select(N.getOperand(0));
2015 Tmp1 = SelectExpr(N.getOperand(1));
2016 Tmp2 = cast<RegSDNode>(N)->getReg();
2017
2018 if (Tmp1 != Tmp2) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002019 if (N.getOperand(1).getValueType() == MVT::f64 ||
2020 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002021 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2022 else
2023 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002024 }
2025 return;
2026
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002027 case ISD::RET:
2028 switch (N.getNumOperands()) {
2029 default:
2030 std::cerr << N.getNumOperands() << "\n";
2031 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2032 std::cerr << N.getOperand(i).getValueType() << "\n";
2033 Node->dump();
2034 assert(0 && "Unknown return instruction!");
2035 case 2:
2036 Select(N.getOperand(0));
2037 Tmp1 = SelectExpr(N.getOperand(1));
2038 switch (N.getOperand(1).getValueType()) {
2039 default: Node->dump();
2040 assert(0 && "All other types should have been promoted!!");
2041 case MVT::f64:
2042 case MVT::f32:
2043 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2044 break;
2045 case MVT::i32:
2046 case MVT::i64:
2047 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2048 break;
2049 }
2050 break;
2051 case 1:
2052 Select(N.getOperand(0));
2053 break;
2054 }
2055 //Tmp2 = AlphaLowering.getRetAddr();
2056 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
2057 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
2058 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002059
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00002060 case ISD::TRUNCSTORE:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002061 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002062 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002063 SDOperand Chain = N.getOperand(0);
2064 SDOperand Value = N.getOperand(1);
2065 SDOperand Address = N.getOperand(2);
2066 Select(Chain);
2067
2068 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002069
2070 if (opcode == ISD::STORE) {
2071 switch(Value.getValueType()) {
2072 default: assert(0 && "unknown Type in store");
2073 case MVT::i64: Opc = Alpha::STQ; break;
2074 case MVT::f64: Opc = Alpha::STT; break;
2075 case MVT::f32: Opc = Alpha::STS; break;
2076 }
2077 } else { //ISD::TRUNCSTORE
2078 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2079 default: assert(0 && "unknown Type in store");
2080 case MVT::i1: //FIXME: DAG does not promote this load
2081 case MVT::i8: Opc = Alpha::STB; break;
2082 case MVT::i16: Opc = Alpha::STW; break;
2083 case MVT::i32: Opc = Alpha::STL; break;
2084 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002085 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002086
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002087 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002088 {
2089 AlphaLowering.restoreGP(BB);
2090 Opc = GetSymVersion(Opc);
2091 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
2092 }
Andrew Lenharth05380342005-02-07 05:07:00 +00002093 else if(Address.getOpcode() == ISD::FrameIndex)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002094 {
Andrew Lenharth032f2352005-02-22 21:59:48 +00002095 BuildMI(BB, Opc, 3).addReg(Tmp1)
2096 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2097 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002098 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002099 else
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002100 {
2101 long offset;
2102 SelectAddr(Address, Tmp2, offset);
2103 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2104 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002105 return;
2106 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002107
2108 case ISD::EXTLOAD:
2109 case ISD::SEXTLOAD:
2110 case ISD::ZEXTLOAD:
2111 case ISD::LOAD:
2112 case ISD::CopyFromReg:
2113 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002114 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002115 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002116 SelectExpr(N);
2117 return;
2118
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002119 case ISD::ADJCALLSTACKDOWN:
2120 case ISD::ADJCALLSTACKUP:
2121 Select(N.getOperand(0));
2122 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2123
2124 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? Alpha::ADJUSTSTACKDOWN :
2125 Alpha::ADJUSTSTACKUP;
2126 BuildMI(BB, Opc, 1).addImm(Tmp1);
2127 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002128
2129 case ISD::PCMARKER:
2130 Select(N.getOperand(0)); //Chain
2131 BuildMI(BB, Alpha::PCLABEL, 2).addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
2132 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002133 }
2134 assert(0 && "Should not be reached!");
2135}
2136
2137
2138/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2139/// into a machine code representation using pattern matching and a machine
2140/// description file.
2141///
2142FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
2143 return new ISel(TM);
2144}